Commit | Line | Data |
---|---|---|
31fb1673 | 1 | // SPDX-License-Identifier: GPL-2.0 |
7e64e059 CH |
2 | /* |
3 | * ccw based virtio transport | |
4 | * | |
96b14536 | 5 | * Copyright IBM Corp. 2012, 2014 |
7e64e059 | 6 | * |
7e64e059 CH |
7 | * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com> |
8 | */ | |
9 | ||
10 | #include <linux/kernel_stat.h> | |
11 | #include <linux/init.h> | |
57c8a661 | 12 | #include <linux/memblock.h> |
7e64e059 CH |
13 | #include <linux/err.h> |
14 | #include <linux/virtio.h> | |
15 | #include <linux/virtio_config.h> | |
16 | #include <linux/slab.h> | |
17 | #include <linux/interrupt.h> | |
18 | #include <linux/virtio_ring.h> | |
19 | #include <linux/pfn.h> | |
20 | #include <linux/async.h> | |
21 | #include <linux/wait.h> | |
22 | #include <linux/list.h> | |
23 | #include <linux/bitops.h> | |
acc50ec7 | 24 | #include <linux/moduleparam.h> |
7e64e059 CH |
25 | #include <linux/io.h> |
26 | #include <linux/kvm_para.h> | |
e75279c4 | 27 | #include <linux/notifier.h> |
1ec2772e | 28 | #include <asm/diag.h> |
7e64e059 CH |
29 | #include <asm/setup.h> |
30 | #include <asm/irq.h> | |
31 | #include <asm/cio.h> | |
32 | #include <asm/ccwdev.h> | |
6a773cb8 | 33 | #include <asm/virtio-ccw.h> |
96b14536 CH |
34 | #include <asm/isc.h> |
35 | #include <asm/airq.h> | |
d2197485 | 36 | #include <asm/tpi.h> |
7e64e059 CH |
37 | |
38 | /* | |
39 | * virtio related functions | |
40 | */ | |
41 | ||
42 | struct vq_config_block { | |
43 | __u16 index; | |
44 | __u16 num; | |
45 | } __packed; | |
46 | ||
47 | #define VIRTIO_CCW_CONFIG_SIZE 0x100 | |
48 | /* same as PCI config space size, should be enough for all drivers */ | |
49 | ||
48720ba5 HP |
50 | struct vcdev_dma_area { |
51 | unsigned long indicators; | |
52 | unsigned long indicators2; | |
53 | struct vq_config_block config_block; | |
54 | __u8 status; | |
55 | }; | |
56 | ||
7e64e059 CH |
57 | struct virtio_ccw_device { |
58 | struct virtio_device vdev; | |
7e64e059 CH |
59 | __u8 config[VIRTIO_CCW_CONFIG_SIZE]; |
60 | struct ccw_device *cdev; | |
7e64e059 CH |
61 | __u32 curr_io; |
62 | int err; | |
6bb2c835 | 63 | unsigned int revision; /* Transport revision */ |
7e64e059 CH |
64 | wait_queue_head_t wait_q; |
65 | spinlock_t lock; | |
3a232277 | 66 | rwlock_t irq_lock; |
78b1a52e | 67 | struct mutex io_lock; /* Serializes I/O requests */ |
7e64e059 | 68 | struct list_head virtqueues; |
96b14536 | 69 | bool is_thinint; |
79629b20 | 70 | bool going_away; |
e75279c4 | 71 | bool device_lost; |
431dae77 | 72 | unsigned int config_ready; |
96b14536 | 73 | void *airq_info; |
48720ba5 | 74 | struct vcdev_dma_area *dma_area; |
8adc56b0 | 75 | dma32_t dma_area_addr; |
7e64e059 CH |
76 | }; |
77 | ||
22a4a639 HP |
78 | static inline unsigned long *indicators(struct virtio_ccw_device *vcdev) |
79 | { | |
48720ba5 | 80 | return &vcdev->dma_area->indicators; |
22a4a639 HP |
81 | } |
82 | ||
83 | static inline unsigned long *indicators2(struct virtio_ccw_device *vcdev) | |
84 | { | |
48720ba5 | 85 | return &vcdev->dma_area->indicators2; |
22a4a639 HP |
86 | } |
87 | ||
8adc56b0 HP |
88 | /* Spec stipulates a 64 bit address */ |
89 | static inline dma64_t indicators_dma(struct virtio_ccw_device *vcdev) | |
90 | { | |
91 | u64 dma_area_addr = dma32_to_u32(vcdev->dma_area_addr); | |
92 | ||
93 | return dma64_add(u64_to_dma64(dma_area_addr), | |
94 | offsetof(struct vcdev_dma_area, indicators)); | |
95 | } | |
96 | ||
97 | /* Spec stipulates a 64 bit address */ | |
98 | static inline dma64_t indicators2_dma(struct virtio_ccw_device *vcdev) | |
99 | { | |
100 | u64 dma_area_addr = dma32_to_u32(vcdev->dma_area_addr); | |
101 | ||
102 | return dma64_add(u64_to_dma64(dma_area_addr), | |
103 | offsetof(struct vcdev_dma_area, indicators2)); | |
104 | } | |
105 | ||
106 | static inline dma32_t config_block_dma(struct virtio_ccw_device *vcdev) | |
107 | { | |
108 | return dma32_add(vcdev->dma_area_addr, | |
109 | offsetof(struct vcdev_dma_area, config_block)); | |
110 | } | |
111 | ||
112 | static inline dma32_t status_dma(struct virtio_ccw_device *vcdev) | |
113 | { | |
114 | return dma32_add(vcdev->dma_area_addr, | |
115 | offsetof(struct vcdev_dma_area, status)); | |
116 | } | |
117 | ||
d4674240 | 118 | struct vq_info_block_legacy { |
d5cc4168 | 119 | dma64_t queue; |
7e64e059 CH |
120 | __u32 align; |
121 | __u16 index; | |
122 | __u16 num; | |
123 | } __packed; | |
124 | ||
d4674240 | 125 | struct vq_info_block { |
d5cc4168 | 126 | dma64_t desc; |
d4674240 CH |
127 | __u32 res0; |
128 | __u16 index; | |
129 | __u16 num; | |
d5cc4168 HP |
130 | dma64_t avail; |
131 | dma64_t used; | |
d4674240 CH |
132 | } __packed; |
133 | ||
7e64e059 | 134 | struct virtio_feature_desc { |
fb317002 | 135 | __le32 features; |
7e64e059 CH |
136 | __u8 index; |
137 | } __packed; | |
138 | ||
96b14536 | 139 | struct virtio_thinint_area { |
d5cc4168 HP |
140 | dma64_t summary_indicator; |
141 | dma64_t indicator; | |
96b14536 CH |
142 | u64 bit_nr; |
143 | u8 isc; | |
144 | } __packed; | |
145 | ||
6bb2c835 TH |
146 | struct virtio_rev_info { |
147 | __u16 revision; | |
148 | __u16 length; | |
149 | __u8 data[]; | |
150 | }; | |
151 | ||
152 | /* the highest virtio-ccw revision we support */ | |
182f709c | 153 | #define VIRTIO_CCW_REV_MAX 2 |
6bb2c835 | 154 | |
7e64e059 CH |
155 | struct virtio_ccw_vq_info { |
156 | struct virtqueue *vq; | |
e3e9bda3 | 157 | dma32_t info_block_addr; |
7e64e059 | 158 | int num; |
d4674240 CH |
159 | union { |
160 | struct vq_info_block s; | |
161 | struct vq_info_block_legacy l; | |
162 | } *info_block; | |
96b14536 | 163 | int bit_nr; |
7e64e059 | 164 | struct list_head node; |
07e16933 | 165 | long cookie; |
7e64e059 CH |
166 | }; |
167 | ||
96b14536 CH |
168 | #define VIRTIO_AIRQ_ISC IO_SCH_ISC /* inherit from subchannel */ |
169 | ||
170 | #define VIRTIO_IV_BITS (L1_CACHE_BYTES * 8) | |
171 | #define MAX_AIRQ_AREAS 20 | |
172 | ||
173 | static int virtio_ccw_use_airq = 1; | |
174 | ||
175 | struct airq_info { | |
176 | rwlock_t lock; | |
39c7dcb1 | 177 | u8 summary_indicator_idx; |
96b14536 CH |
178 | struct airq_struct airq; |
179 | struct airq_iv *aiv; | |
180 | }; | |
181 | static struct airq_info *airq_areas[MAX_AIRQ_AREAS]; | |
4f419eb1 HP |
182 | static DEFINE_MUTEX(airq_areas_lock); |
183 | ||
39c7dcb1 HP |
184 | static u8 *summary_indicators; |
185 | ||
186 | static inline u8 *get_summary_indicator(struct airq_info *info) | |
187 | { | |
188 | return summary_indicators + info->summary_indicator_idx; | |
189 | } | |
96b14536 | 190 | |
8adc56b0 HP |
191 | static inline dma64_t get_summary_indicator_dma(struct airq_info *info) |
192 | { | |
193 | return virt_to_dma64(get_summary_indicator(info)); | |
194 | } | |
195 | ||
7e64e059 CH |
196 | #define CCW_CMD_SET_VQ 0x13 |
197 | #define CCW_CMD_VDEV_RESET 0x33 | |
198 | #define CCW_CMD_SET_IND 0x43 | |
199 | #define CCW_CMD_SET_CONF_IND 0x53 | |
200 | #define CCW_CMD_READ_FEAT 0x12 | |
201 | #define CCW_CMD_WRITE_FEAT 0x11 | |
202 | #define CCW_CMD_READ_CONF 0x22 | |
203 | #define CCW_CMD_WRITE_CONF 0x21 | |
204 | #define CCW_CMD_WRITE_STATUS 0x31 | |
205 | #define CCW_CMD_READ_VQ_CONF 0x32 | |
7d3ce5ab | 206 | #define CCW_CMD_READ_STATUS 0x72 |
96b14536 | 207 | #define CCW_CMD_SET_IND_ADAPTER 0x73 |
6bb2c835 | 208 | #define CCW_CMD_SET_VIRTIO_REV 0x83 |
7e64e059 CH |
209 | |
210 | #define VIRTIO_CCW_DOING_SET_VQ 0x00010000 | |
211 | #define VIRTIO_CCW_DOING_RESET 0x00040000 | |
212 | #define VIRTIO_CCW_DOING_READ_FEAT 0x00080000 | |
213 | #define VIRTIO_CCW_DOING_WRITE_FEAT 0x00100000 | |
214 | #define VIRTIO_CCW_DOING_READ_CONFIG 0x00200000 | |
215 | #define VIRTIO_CCW_DOING_WRITE_CONFIG 0x00400000 | |
216 | #define VIRTIO_CCW_DOING_WRITE_STATUS 0x00800000 | |
217 | #define VIRTIO_CCW_DOING_SET_IND 0x01000000 | |
218 | #define VIRTIO_CCW_DOING_READ_VQ_CONF 0x02000000 | |
219 | #define VIRTIO_CCW_DOING_SET_CONF_IND 0x04000000 | |
96b14536 | 220 | #define VIRTIO_CCW_DOING_SET_IND_ADAPTER 0x08000000 |
6bb2c835 | 221 | #define VIRTIO_CCW_DOING_SET_VIRTIO_REV 0x10000000 |
7d3ce5ab | 222 | #define VIRTIO_CCW_DOING_READ_STATUS 0x20000000 |
7e64e059 CH |
223 | #define VIRTIO_CCW_INTPARM_MASK 0xffff0000 |
224 | ||
225 | static struct virtio_ccw_device *to_vc_device(struct virtio_device *vdev) | |
226 | { | |
227 | return container_of(vdev, struct virtio_ccw_device, vdev); | |
228 | } | |
229 | ||
96b14536 CH |
230 | static void drop_airq_indicator(struct virtqueue *vq, struct airq_info *info) |
231 | { | |
232 | unsigned long i, flags; | |
233 | ||
234 | write_lock_irqsave(&info->lock, flags); | |
235 | for (i = 0; i < airq_iv_end(info->aiv); i++) { | |
236 | if (vq == (void *)airq_iv_get_ptr(info->aiv, i)) { | |
237 | airq_iv_free_bit(info->aiv, i); | |
238 | airq_iv_set_ptr(info->aiv, i, 0); | |
239 | break; | |
240 | } | |
241 | } | |
242 | write_unlock_irqrestore(&info->lock, flags); | |
243 | } | |
244 | ||
d2197485 MR |
245 | static void virtio_airq_handler(struct airq_struct *airq, |
246 | struct tpi_info *tpi_info) | |
96b14536 CH |
247 | { |
248 | struct airq_info *info = container_of(airq, struct airq_info, airq); | |
249 | unsigned long ai; | |
250 | ||
251 | inc_irq_stat(IRQIO_VAI); | |
252 | read_lock(&info->lock); | |
253 | /* Walk through indicators field, summary indicator active. */ | |
254 | for (ai = 0;;) { | |
255 | ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv)); | |
256 | if (ai == -1UL) | |
257 | break; | |
258 | vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai)); | |
259 | } | |
39c7dcb1 | 260 | *(get_summary_indicator(info)) = 0; |
96b14536 CH |
261 | smp_wmb(); |
262 | /* Walk through indicators field, summary indicator not active. */ | |
263 | for (ai = 0;;) { | |
264 | ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv)); | |
265 | if (ai == -1UL) | |
266 | break; | |
267 | vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai)); | |
268 | } | |
269 | read_unlock(&info->lock); | |
270 | } | |
271 | ||
39c7dcb1 | 272 | static struct airq_info *new_airq_info(int index) |
96b14536 CH |
273 | { |
274 | struct airq_info *info; | |
275 | int rc; | |
276 | ||
277 | info = kzalloc(sizeof(*info), GFP_KERNEL); | |
278 | if (!info) | |
279 | return NULL; | |
280 | rwlock_init(&info->lock); | |
01b3fb1e | 281 | info->aiv = airq_iv_create(VIRTIO_IV_BITS, AIRQ_IV_ALLOC | AIRQ_IV_PTR |
932b6467 | 282 | | AIRQ_IV_CACHELINE, NULL); |
96b14536 CH |
283 | if (!info->aiv) { |
284 | kfree(info); | |
285 | return NULL; | |
286 | } | |
287 | info->airq.handler = virtio_airq_handler; | |
39c7dcb1 HP |
288 | info->summary_indicator_idx = index; |
289 | info->airq.lsi_ptr = get_summary_indicator(info); | |
96b14536 CH |
290 | info->airq.isc = VIRTIO_AIRQ_ISC; |
291 | rc = register_adapter_interrupt(&info->airq); | |
292 | if (rc) { | |
293 | airq_iv_release(info->aiv); | |
294 | kfree(info); | |
295 | return NULL; | |
296 | } | |
297 | return info; | |
298 | } | |
299 | ||
d5cc4168 HP |
300 | static unsigned long *get_airq_indicator(struct virtqueue *vqs[], int nvqs, |
301 | u64 *first, void **airq_info) | |
96b14536 CH |
302 | { |
303 | int i, j; | |
304 | struct airq_info *info; | |
d5cc4168 | 305 | unsigned long *indicator_addr = NULL; |
96b14536 CH |
306 | unsigned long bit, flags; |
307 | ||
308 | for (i = 0; i < MAX_AIRQ_AREAS && !indicator_addr; i++) { | |
4f419eb1 | 309 | mutex_lock(&airq_areas_lock); |
96b14536 | 310 | if (!airq_areas[i]) |
39c7dcb1 | 311 | airq_areas[i] = new_airq_info(i); |
96b14536 | 312 | info = airq_areas[i]; |
4f419eb1 | 313 | mutex_unlock(&airq_areas_lock); |
96b14536 | 314 | if (!info) |
d5cc4168 | 315 | return NULL; |
96b14536 CH |
316 | write_lock_irqsave(&info->lock, flags); |
317 | bit = airq_iv_alloc(info->aiv, nvqs); | |
318 | if (bit == -1UL) { | |
319 | /* Not enough vacancies. */ | |
320 | write_unlock_irqrestore(&info->lock, flags); | |
321 | continue; | |
322 | } | |
323 | *first = bit; | |
324 | *airq_info = info; | |
d5cc4168 | 325 | indicator_addr = info->aiv->vector; |
96b14536 CH |
326 | for (j = 0; j < nvqs; j++) { |
327 | airq_iv_set_ptr(info->aiv, bit + j, | |
328 | (unsigned long)vqs[j]); | |
329 | } | |
330 | write_unlock_irqrestore(&info->lock, flags); | |
331 | } | |
332 | return indicator_addr; | |
333 | } | |
334 | ||
335 | static void virtio_ccw_drop_indicators(struct virtio_ccw_device *vcdev) | |
336 | { | |
337 | struct virtio_ccw_vq_info *info; | |
338 | ||
3438b2c0 HP |
339 | if (!vcdev->airq_info) |
340 | return; | |
96b14536 CH |
341 | list_for_each_entry(info, &vcdev->virtqueues, node) |
342 | drop_airq_indicator(info->vq, vcdev->airq_info); | |
343 | } | |
344 | ||
7e64e059 CH |
345 | static int doing_io(struct virtio_ccw_device *vcdev, __u32 flag) |
346 | { | |
347 | unsigned long flags; | |
348 | __u32 ret; | |
349 | ||
350 | spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags); | |
351 | if (vcdev->err) | |
352 | ret = 0; | |
353 | else | |
354 | ret = vcdev->curr_io & flag; | |
355 | spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags); | |
356 | return ret; | |
357 | } | |
358 | ||
73fa21ea CH |
359 | static int ccw_io_helper(struct virtio_ccw_device *vcdev, |
360 | struct ccw1 *ccw, __u32 intparm) | |
7e64e059 CH |
361 | { |
362 | int ret; | |
363 | unsigned long flags; | |
364 | int flag = intparm & VIRTIO_CCW_INTPARM_MASK; | |
365 | ||
78b1a52e | 366 | mutex_lock(&vcdev->io_lock); |
b26ba22b CB |
367 | do { |
368 | spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags); | |
369 | ret = ccw_device_start(vcdev->cdev, ccw, intparm, 0, 0); | |
99437a27 CH |
370 | if (!ret) { |
371 | if (!vcdev->curr_io) | |
372 | vcdev->err = 0; | |
b26ba22b | 373 | vcdev->curr_io |= flag; |
99437a27 | 374 | } |
b26ba22b CB |
375 | spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags); |
376 | cpu_relax(); | |
377 | } while (ret == -EBUSY); | |
7e64e059 | 378 | wait_event(vcdev->wait_q, doing_io(vcdev, flag) == 0); |
78b1a52e HP |
379 | ret = ret ? ret : vcdev->err; |
380 | mutex_unlock(&vcdev->io_lock); | |
381 | return ret; | |
7e64e059 CH |
382 | } |
383 | ||
96b14536 CH |
384 | static void virtio_ccw_drop_indicator(struct virtio_ccw_device *vcdev, |
385 | struct ccw1 *ccw) | |
386 | { | |
387 | int ret; | |
96b14536 CH |
388 | struct virtio_thinint_area *thinint_area = NULL; |
389 | struct airq_info *airq_info = vcdev->airq_info; | |
8adc56b0 | 390 | dma64_t *indicatorp = NULL; |
96b14536 CH |
391 | |
392 | if (vcdev->is_thinint) { | |
48720ba5 | 393 | thinint_area = ccw_device_dma_zalloc(vcdev->cdev, |
e3e9bda3 HP |
394 | sizeof(*thinint_area), |
395 | &ccw->cda); | |
96b14536 CH |
396 | if (!thinint_area) |
397 | return; | |
398 | thinint_area->summary_indicator = | |
8adc56b0 | 399 | get_summary_indicator_dma(airq_info); |
96b14536 CH |
400 | thinint_area->isc = VIRTIO_AIRQ_ISC; |
401 | ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER; | |
402 | ccw->count = sizeof(*thinint_area); | |
96b14536 | 403 | } else { |
c235082b | 404 | /* payload is the address of the indicators */ |
48720ba5 | 405 | indicatorp = ccw_device_dma_zalloc(vcdev->cdev, |
8adc56b0 | 406 | sizeof(*indicatorp), |
e3e9bda3 | 407 | &ccw->cda); |
96b14536 CH |
408 | if (!indicatorp) |
409 | return; | |
410 | *indicatorp = 0; | |
411 | ccw->cmd_code = CCW_CMD_SET_IND; | |
8adc56b0 | 412 | ccw->count = sizeof(*indicatorp); |
96b14536 CH |
413 | } |
414 | /* Deregister indicators from host. */ | |
22a4a639 | 415 | *indicators(vcdev) = 0; |
96b14536 CH |
416 | ccw->flags = 0; |
417 | ret = ccw_io_helper(vcdev, ccw, | |
418 | vcdev->is_thinint ? | |
419 | VIRTIO_CCW_DOING_SET_IND_ADAPTER : | |
420 | VIRTIO_CCW_DOING_SET_IND); | |
421 | if (ret && (ret != -ENODEV)) | |
422 | dev_info(&vcdev->cdev->dev, | |
423 | "Failed to deregister indicators (%d)\n", ret); | |
424 | else if (vcdev->is_thinint) | |
425 | virtio_ccw_drop_indicators(vcdev); | |
8adc56b0 | 426 | ccw_device_dma_free(vcdev->cdev, indicatorp, sizeof(*indicatorp)); |
48720ba5 | 427 | ccw_device_dma_free(vcdev->cdev, thinint_area, sizeof(*thinint_area)); |
96b14536 CH |
428 | } |
429 | ||
af8ececd | 430 | static inline bool virtio_ccw_do_kvm_notify(struct virtqueue *vq, u32 data) |
7e64e059 CH |
431 | { |
432 | struct virtio_ccw_vq_info *info = vq->priv; | |
433 | struct virtio_ccw_device *vcdev; | |
434 | struct subchannel_id schid; | |
435 | ||
436 | vcdev = to_vc_device(info->vq->vdev); | |
437 | ccw_device_get_schid(vcdev->cdev, &schid); | |
e5e1bdf0 HC |
438 | BUILD_BUG_ON(sizeof(struct subchannel_id) != sizeof(unsigned int)); |
439 | info->cookie = kvm_hypercall3(KVM_S390_VIRTIO_CCW_NOTIFY, | |
440 | *((unsigned int *)&schid), | |
af8ececd | 441 | data, info->cookie); |
46f9c2b9 HG |
442 | if (info->cookie < 0) |
443 | return false; | |
444 | return true; | |
7e64e059 CH |
445 | } |
446 | ||
af8ececd VP |
447 | static bool virtio_ccw_kvm_notify(struct virtqueue *vq) |
448 | { | |
449 | return virtio_ccw_do_kvm_notify(vq, vq->index); | |
450 | } | |
451 | ||
452 | static bool virtio_ccw_kvm_notify_with_data(struct virtqueue *vq) | |
453 | { | |
454 | return virtio_ccw_do_kvm_notify(vq, vring_notification_data(vq)); | |
455 | } | |
456 | ||
73fa21ea CH |
457 | static int virtio_ccw_read_vq_conf(struct virtio_ccw_device *vcdev, |
458 | struct ccw1 *ccw, int index) | |
7e64e059 | 459 | { |
ad2aa042 PM |
460 | int ret; |
461 | ||
48720ba5 | 462 | vcdev->dma_area->config_block.index = index; |
73fa21ea CH |
463 | ccw->cmd_code = CCW_CMD_READ_VQ_CONF; |
464 | ccw->flags = 0; | |
465 | ccw->count = sizeof(struct vq_config_block); | |
8adc56b0 | 466 | ccw->cda = config_block_dma(vcdev); |
ad2aa042 PM |
467 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_VQ_CONF); |
468 | if (ret) | |
469 | return ret; | |
48720ba5 | 470 | return vcdev->dma_area->config_block.num ?: -ENOENT; |
7e64e059 CH |
471 | } |
472 | ||
73fa21ea | 473 | static void virtio_ccw_del_vq(struct virtqueue *vq, struct ccw1 *ccw) |
7e64e059 CH |
474 | { |
475 | struct virtio_ccw_device *vcdev = to_vc_device(vq->vdev); | |
476 | struct virtio_ccw_vq_info *info = vq->priv; | |
477 | unsigned long flags; | |
7e64e059 | 478 | int ret; |
9d0ca6ed | 479 | unsigned int index = vq->index; |
7e64e059 CH |
480 | |
481 | /* Remove from our list. */ | |
482 | spin_lock_irqsave(&vcdev->lock, flags); | |
483 | list_del(&info->node); | |
484 | spin_unlock_irqrestore(&vcdev->lock, flags); | |
485 | ||
486 | /* Release from host. */ | |
d4674240 CH |
487 | if (vcdev->revision == 0) { |
488 | info->info_block->l.queue = 0; | |
489 | info->info_block->l.align = 0; | |
490 | info->info_block->l.index = index; | |
491 | info->info_block->l.num = 0; | |
492 | ccw->count = sizeof(info->info_block->l); | |
493 | } else { | |
494 | info->info_block->s.desc = 0; | |
495 | info->info_block->s.index = index; | |
496 | info->info_block->s.num = 0; | |
497 | info->info_block->s.avail = 0; | |
498 | info->info_block->s.used = 0; | |
499 | ccw->count = sizeof(info->info_block->s); | |
500 | } | |
73fa21ea CH |
501 | ccw->cmd_code = CCW_CMD_SET_VQ; |
502 | ccw->flags = 0; | |
e3e9bda3 | 503 | ccw->cda = info->info_block_addr; |
73fa21ea CH |
504 | ret = ccw_io_helper(vcdev, ccw, |
505 | VIRTIO_CCW_DOING_SET_VQ | index); | |
7e64e059 CH |
506 | /* |
507 | * -ENODEV isn't considered an error: The device is gone anyway. | |
508 | * This may happen on device detach. | |
509 | */ | |
510 | if (ret && (ret != -ENODEV)) | |
99240622 | 511 | dev_warn(&vq->vdev->dev, "Error %d while deleting queue %d\n", |
7e64e059 CH |
512 | ret, index); |
513 | ||
514 | vring_del_virtqueue(vq); | |
48720ba5 HP |
515 | ccw_device_dma_free(vcdev->cdev, info->info_block, |
516 | sizeof(*info->info_block)); | |
7e64e059 CH |
517 | kfree(info); |
518 | } | |
519 | ||
520 | static void virtio_ccw_del_vqs(struct virtio_device *vdev) | |
521 | { | |
522 | struct virtqueue *vq, *n; | |
73fa21ea | 523 | struct ccw1 *ccw; |
96b14536 | 524 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); |
73fa21ea | 525 | |
e3e9bda3 | 526 | ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL); |
73fa21ea CH |
527 | if (!ccw) |
528 | return; | |
529 | ||
96b14536 | 530 | virtio_ccw_drop_indicator(vcdev, ccw); |
7e64e059 CH |
531 | |
532 | list_for_each_entry_safe(vq, n, &vdev->vqs, list) | |
73fa21ea CH |
533 | virtio_ccw_del_vq(vq, ccw); |
534 | ||
48720ba5 | 535 | ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw)); |
7e64e059 CH |
536 | } |
537 | ||
538 | static struct virtqueue *virtio_ccw_setup_vq(struct virtio_device *vdev, | |
539 | int i, vq_callback_t *callback, | |
f94682dd | 540 | const char *name, bool ctx, |
73fa21ea | 541 | struct ccw1 *ccw) |
7e64e059 CH |
542 | { |
543 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); | |
af8ececd | 544 | bool (*notify)(struct virtqueue *vq); |
7e64e059 | 545 | int err; |
c98d3683 | 546 | struct virtqueue *vq = NULL; |
7e64e059 | 547 | struct virtio_ccw_vq_info *info; |
3279beac | 548 | u64 queue; |
7e64e059 | 549 | unsigned long flags; |
3279beac | 550 | bool may_reduce; |
7e64e059 | 551 | |
af8ececd VP |
552 | if (__virtio_test_bit(vdev, VIRTIO_F_NOTIFICATION_DATA)) |
553 | notify = virtio_ccw_kvm_notify_with_data; | |
554 | else | |
555 | notify = virtio_ccw_kvm_notify; | |
556 | ||
7e64e059 CH |
557 | /* Allocate queue. */ |
558 | info = kzalloc(sizeof(struct virtio_ccw_vq_info), GFP_KERNEL); | |
559 | if (!info) { | |
560 | dev_warn(&vcdev->cdev->dev, "no info\n"); | |
561 | err = -ENOMEM; | |
562 | goto out_err; | |
563 | } | |
48720ba5 | 564 | info->info_block = ccw_device_dma_zalloc(vcdev->cdev, |
e3e9bda3 HP |
565 | sizeof(*info->info_block), |
566 | &info->info_block_addr); | |
7e64e059 CH |
567 | if (!info->info_block) { |
568 | dev_warn(&vcdev->cdev->dev, "no info block\n"); | |
569 | err = -ENOMEM; | |
570 | goto out_err; | |
571 | } | |
73fa21ea | 572 | info->num = virtio_ccw_read_vq_conf(vcdev, ccw, i); |
ad2aa042 PM |
573 | if (info->num < 0) { |
574 | err = info->num; | |
575 | goto out_err; | |
576 | } | |
3279beac HP |
577 | may_reduce = vcdev->revision > 0; |
578 | vq = vring_create_virtqueue(i, info->num, KVM_VIRTIO_CCW_RING_ALIGN, | |
579 | vdev, true, may_reduce, ctx, | |
af8ececd | 580 | notify, callback, name); |
7e64e059 | 581 | |
7e64e059 CH |
582 | if (!vq) { |
583 | /* For now, we fail if we can't get the requested size. */ | |
584 | dev_warn(&vcdev->cdev->dev, "no vq\n"); | |
585 | err = -ENOMEM; | |
7e64e059 CH |
586 | goto out_err; |
587 | } | |
da802961 XZ |
588 | |
589 | vq->num_max = info->num; | |
590 | ||
3279beac HP |
591 | /* it may have been reduced */ |
592 | info->num = virtqueue_get_vring_size(vq); | |
7e64e059 CH |
593 | |
594 | /* Register it with the host. */ | |
3279beac | 595 | queue = virtqueue_get_desc_addr(vq); |
d4674240 | 596 | if (vcdev->revision == 0) { |
d5cc4168 | 597 | info->info_block->l.queue = u64_to_dma64(queue); |
d4674240 CH |
598 | info->info_block->l.align = KVM_VIRTIO_CCW_RING_ALIGN; |
599 | info->info_block->l.index = i; | |
600 | info->info_block->l.num = info->num; | |
601 | ccw->count = sizeof(info->info_block->l); | |
602 | } else { | |
d5cc4168 | 603 | info->info_block->s.desc = u64_to_dma64(queue); |
d4674240 CH |
604 | info->info_block->s.index = i; |
605 | info->info_block->s.num = info->num; | |
d5cc4168 HP |
606 | info->info_block->s.avail = u64_to_dma64(virtqueue_get_avail_addr(vq)); |
607 | info->info_block->s.used = u64_to_dma64(virtqueue_get_used_addr(vq)); | |
d4674240 CH |
608 | ccw->count = sizeof(info->info_block->s); |
609 | } | |
73fa21ea CH |
610 | ccw->cmd_code = CCW_CMD_SET_VQ; |
611 | ccw->flags = 0; | |
e3e9bda3 | 612 | ccw->cda = info->info_block_addr; |
73fa21ea | 613 | err = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_VQ | i); |
7e64e059 CH |
614 | if (err) { |
615 | dev_warn(&vcdev->cdev->dev, "SET_VQ failed\n"); | |
7e64e059 CH |
616 | goto out_err; |
617 | } | |
618 | ||
c98d3683 CH |
619 | info->vq = vq; |
620 | vq->priv = info; | |
621 | ||
7e64e059 CH |
622 | /* Save it to our list. */ |
623 | spin_lock_irqsave(&vcdev->lock, flags); | |
624 | list_add(&info->node, &vcdev->virtqueues); | |
625 | spin_unlock_irqrestore(&vcdev->lock, flags); | |
626 | ||
627 | return vq; | |
628 | ||
629 | out_err: | |
c98d3683 CH |
630 | if (vq) |
631 | vring_del_virtqueue(vq); | |
632 | if (info) { | |
48720ba5 HP |
633 | ccw_device_dma_free(vcdev->cdev, info->info_block, |
634 | sizeof(*info->info_block)); | |
c98d3683 | 635 | } |
7e64e059 CH |
636 | kfree(info); |
637 | return ERR_PTR(err); | |
638 | } | |
639 | ||
96b14536 CH |
640 | static int virtio_ccw_register_adapter_ind(struct virtio_ccw_device *vcdev, |
641 | struct virtqueue *vqs[], int nvqs, | |
642 | struct ccw1 *ccw) | |
643 | { | |
644 | int ret; | |
645 | struct virtio_thinint_area *thinint_area = NULL; | |
d5cc4168 | 646 | unsigned long *indicator_addr; |
96b14536 CH |
647 | struct airq_info *info; |
648 | ||
48720ba5 | 649 | thinint_area = ccw_device_dma_zalloc(vcdev->cdev, |
e3e9bda3 HP |
650 | sizeof(*thinint_area), |
651 | &ccw->cda); | |
96b14536 CH |
652 | if (!thinint_area) { |
653 | ret = -ENOMEM; | |
654 | goto out; | |
655 | } | |
656 | /* Try to get an indicator. */ | |
5fc5b94a AG |
657 | indicator_addr = get_airq_indicator(vqs, nvqs, |
658 | &thinint_area->bit_nr, | |
659 | &vcdev->airq_info); | |
660 | if (!indicator_addr) { | |
96b14536 CH |
661 | ret = -ENOSPC; |
662 | goto out; | |
663 | } | |
d5cc4168 | 664 | thinint_area->indicator = virt_to_dma64(indicator_addr); |
96b14536 | 665 | info = vcdev->airq_info; |
8adc56b0 | 666 | thinint_area->summary_indicator = get_summary_indicator_dma(info); |
96b14536 CH |
667 | thinint_area->isc = VIRTIO_AIRQ_ISC; |
668 | ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER; | |
669 | ccw->flags = CCW_FLAG_SLI; | |
670 | ccw->count = sizeof(*thinint_area); | |
96b14536 CH |
671 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND_ADAPTER); |
672 | if (ret) { | |
673 | if (ret == -EOPNOTSUPP) { | |
674 | /* | |
675 | * The host does not support adapter interrupts | |
676 | * for virtio-ccw, stop trying. | |
677 | */ | |
678 | virtio_ccw_use_airq = 0; | |
679 | pr_info("Adapter interrupts unsupported on host\n"); | |
680 | } else | |
681 | dev_warn(&vcdev->cdev->dev, | |
682 | "enabling adapter interrupts = %d\n", ret); | |
683 | virtio_ccw_drop_indicators(vcdev); | |
684 | } | |
685 | out: | |
48720ba5 | 686 | ccw_device_dma_free(vcdev->cdev, thinint_area, sizeof(*thinint_area)); |
96b14536 CH |
687 | return ret; |
688 | } | |
689 | ||
7e64e059 CH |
690 | static int virtio_ccw_find_vqs(struct virtio_device *vdev, unsigned nvqs, |
691 | struct virtqueue *vqs[], | |
3c93b576 | 692 | struct virtqueue_info vqs_info[], |
fb5e31d9 | 693 | struct irq_affinity *desc) |
7e64e059 CH |
694 | { |
695 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); | |
d5cc4168 | 696 | dma64_t *indicatorp = NULL; |
a229989d | 697 | int ret, i, queue_idx = 0; |
73fa21ea | 698 | struct ccw1 *ccw; |
d8354a1d | 699 | dma32_t indicatorp_dma = 0; |
73fa21ea | 700 | |
e3e9bda3 | 701 | ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL); |
73fa21ea CH |
702 | if (!ccw) |
703 | return -ENOMEM; | |
7e64e059 CH |
704 | |
705 | for (i = 0; i < nvqs; ++i) { | |
3c93b576 JP |
706 | struct virtqueue_info *vqi = &vqs_info[i]; |
707 | ||
708 | if (!vqi->name) { | |
a229989d WW |
709 | vqs[i] = NULL; |
710 | continue; | |
711 | } | |
712 | ||
3c93b576 JP |
713 | vqs[i] = virtio_ccw_setup_vq(vdev, queue_idx++, vqi->callback, |
714 | vqi->name, vqi->ctx, ccw); | |
7e64e059 CH |
715 | if (IS_ERR(vqs[i])) { |
716 | ret = PTR_ERR(vqs[i]); | |
717 | vqs[i] = NULL; | |
718 | goto out; | |
719 | } | |
720 | } | |
721 | ret = -ENOMEM; | |
c235082b CH |
722 | /* |
723 | * We need a data area under 2G to communicate. Our payload is | |
724 | * the address of the indicators. | |
725 | */ | |
48720ba5 | 726 | indicatorp = ccw_device_dma_zalloc(vcdev->cdev, |
8adc56b0 | 727 | sizeof(*indicatorp), |
d8354a1d | 728 | &indicatorp_dma); |
7e64e059 CH |
729 | if (!indicatorp) |
730 | goto out; | |
8adc56b0 | 731 | *indicatorp = indicators_dma(vcdev); |
96b14536 CH |
732 | if (vcdev->is_thinint) { |
733 | ret = virtio_ccw_register_adapter_ind(vcdev, vqs, nvqs, ccw); | |
734 | if (ret) | |
735 | /* no error, just fall back to legacy interrupts */ | |
970ba6ac | 736 | vcdev->is_thinint = false; |
96b14536 | 737 | } |
d8354a1d | 738 | ccw->cda = indicatorp_dma; |
96b14536 CH |
739 | if (!vcdev->is_thinint) { |
740 | /* Register queue indicators with host. */ | |
22a4a639 | 741 | *indicators(vcdev) = 0; |
96b14536 CH |
742 | ccw->cmd_code = CCW_CMD_SET_IND; |
743 | ccw->flags = 0; | |
8adc56b0 | 744 | ccw->count = sizeof(*indicatorp); |
96b14536 CH |
745 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND); |
746 | if (ret) | |
747 | goto out; | |
748 | } | |
7e64e059 | 749 | /* Register indicators2 with host for config changes */ |
8adc56b0 | 750 | *indicatorp = indicators2_dma(vcdev); |
22a4a639 | 751 | *indicators2(vcdev) = 0; |
73fa21ea CH |
752 | ccw->cmd_code = CCW_CMD_SET_CONF_IND; |
753 | ccw->flags = 0; | |
8adc56b0 | 754 | ccw->count = sizeof(*indicatorp); |
73fa21ea | 755 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_CONF_IND); |
7e64e059 CH |
756 | if (ret) |
757 | goto out; | |
758 | ||
48720ba5 HP |
759 | if (indicatorp) |
760 | ccw_device_dma_free(vcdev->cdev, indicatorp, | |
8adc56b0 | 761 | sizeof(*indicatorp)); |
48720ba5 | 762 | ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw)); |
7e64e059 CH |
763 | return 0; |
764 | out: | |
48720ba5 HP |
765 | if (indicatorp) |
766 | ccw_device_dma_free(vcdev->cdev, indicatorp, | |
8adc56b0 | 767 | sizeof(*indicatorp)); |
48720ba5 | 768 | ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw)); |
7e64e059 CH |
769 | virtio_ccw_del_vqs(vdev); |
770 | return ret; | |
771 | } | |
772 | ||
773 | static void virtio_ccw_reset(struct virtio_device *vdev) | |
774 | { | |
775 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); | |
73fa21ea CH |
776 | struct ccw1 *ccw; |
777 | ||
e3e9bda3 | 778 | ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL); |
73fa21ea CH |
779 | if (!ccw) |
780 | return; | |
7e64e059 CH |
781 | |
782 | /* Zero status bits. */ | |
48720ba5 | 783 | vcdev->dma_area->status = 0; |
7e64e059 CH |
784 | |
785 | /* Send a reset ccw on device. */ | |
73fa21ea CH |
786 | ccw->cmd_code = CCW_CMD_VDEV_RESET; |
787 | ccw->flags = 0; | |
788 | ccw->count = 0; | |
789 | ccw->cda = 0; | |
790 | ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_RESET); | |
48720ba5 | 791 | ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw)); |
7e64e059 CH |
792 | } |
793 | ||
d0254773 | 794 | static u64 virtio_ccw_get_features(struct virtio_device *vdev) |
7e64e059 CH |
795 | { |
796 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); | |
73fa21ea | 797 | struct virtio_feature_desc *features; |
732c56e9 MT |
798 | int ret; |
799 | u64 rc; | |
73fa21ea | 800 | struct ccw1 *ccw; |
7e64e059 | 801 | |
e3e9bda3 | 802 | ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL); |
73fa21ea CH |
803 | if (!ccw) |
804 | return 0; | |
805 | ||
e3e9bda3 HP |
806 | features = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*features), |
807 | &ccw->cda); | |
73fa21ea CH |
808 | if (!features) { |
809 | rc = 0; | |
810 | goto out_free; | |
811 | } | |
7e64e059 | 812 | /* Read the feature bits from the host. */ |
73fa21ea CH |
813 | features->index = 0; |
814 | ccw->cmd_code = CCW_CMD_READ_FEAT; | |
815 | ccw->flags = 0; | |
816 | ccw->count = sizeof(*features); | |
73fa21ea CH |
817 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT); |
818 | if (ret) { | |
819 | rc = 0; | |
820 | goto out_free; | |
821 | } | |
822 | ||
823 | rc = le32_to_cpu(features->features); | |
7e64e059 | 824 | |
ce15408f MT |
825 | if (vcdev->revision == 0) |
826 | goto out_free; | |
827 | ||
732c56e9 MT |
828 | /* Read second half of the feature bits from the host. */ |
829 | features->index = 1; | |
830 | ccw->cmd_code = CCW_CMD_READ_FEAT; | |
831 | ccw->flags = 0; | |
832 | ccw->count = sizeof(*features); | |
732c56e9 MT |
833 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT); |
834 | if (ret == 0) | |
835 | rc |= (u64)le32_to_cpu(features->features) << 32; | |
836 | ||
73fa21ea | 837 | out_free: |
48720ba5 HP |
838 | ccw_device_dma_free(vcdev->cdev, features, sizeof(*features)); |
839 | ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw)); | |
73fa21ea | 840 | return rc; |
7e64e059 CH |
841 | } |
842 | ||
3a814fdf TB |
843 | static void ccw_transport_features(struct virtio_device *vdev) |
844 | { | |
845 | /* | |
050f4c4d | 846 | * Currently nothing to do here. |
3a814fdf | 847 | */ |
3a814fdf TB |
848 | } |
849 | ||
5c609a5e | 850 | static int virtio_ccw_finalize_features(struct virtio_device *vdev) |
7e64e059 CH |
851 | { |
852 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); | |
73fa21ea | 853 | struct virtio_feature_desc *features; |
73fa21ea | 854 | struct ccw1 *ccw; |
f01a2a81 | 855 | int ret; |
73fa21ea | 856 | |
f13d8bc2 | 857 | if (vcdev->revision >= 1 && |
4d23676f MT |
858 | !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) { |
859 | dev_err(&vdev->dev, "virtio: device uses revision 1 " | |
860 | "but does not have VIRTIO_F_VERSION_1\n"); | |
861 | return -EINVAL; | |
862 | } | |
863 | ||
e3e9bda3 | 864 | ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL); |
73fa21ea | 865 | if (!ccw) |
f01a2a81 | 866 | return -ENOMEM; |
73fa21ea | 867 | |
e3e9bda3 HP |
868 | features = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*features), |
869 | &ccw->cda); | |
f01a2a81 CH |
870 | if (!features) { |
871 | ret = -ENOMEM; | |
73fa21ea | 872 | goto out_free; |
f01a2a81 | 873 | } |
7e64e059 CH |
874 | /* Give virtio_ring a chance to accept features. */ |
875 | vring_transport_features(vdev); | |
876 | ||
3a814fdf TB |
877 | /* Give virtio_ccw a chance to accept features. */ |
878 | ccw_transport_features(vdev); | |
879 | ||
e16e12be | 880 | features->index = 0; |
732c56e9 MT |
881 | features->features = cpu_to_le32((u32)vdev->features); |
882 | /* Write the first half of the feature bits to the host. */ | |
883 | ccw->cmd_code = CCW_CMD_WRITE_FEAT; | |
884 | ccw->flags = 0; | |
885 | ccw->count = sizeof(*features); | |
f01a2a81 CH |
886 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT); |
887 | if (ret) | |
888 | goto out_free; | |
732c56e9 | 889 | |
ce15408f MT |
890 | if (vcdev->revision == 0) |
891 | goto out_free; | |
892 | ||
732c56e9 MT |
893 | features->index = 1; |
894 | features->features = cpu_to_le32(vdev->features >> 32); | |
895 | /* Write the second half of the feature bits to the host. */ | |
e16e12be MT |
896 | ccw->cmd_code = CCW_CMD_WRITE_FEAT; |
897 | ccw->flags = 0; | |
898 | ccw->count = sizeof(*features); | |
f01a2a81 | 899 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT); |
e16e12be | 900 | |
73fa21ea | 901 | out_free: |
48720ba5 HP |
902 | ccw_device_dma_free(vcdev->cdev, features, sizeof(*features)); |
903 | ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw)); | |
5c609a5e | 904 | |
f01a2a81 | 905 | return ret; |
7e64e059 CH |
906 | } |
907 | ||
908 | static void virtio_ccw_get_config(struct virtio_device *vdev, | |
909 | unsigned int offset, void *buf, unsigned len) | |
910 | { | |
911 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); | |
912 | int ret; | |
73fa21ea CH |
913 | struct ccw1 *ccw; |
914 | void *config_area; | |
2448a299 | 915 | unsigned long flags; |
73fa21ea | 916 | |
e3e9bda3 | 917 | ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL); |
73fa21ea CH |
918 | if (!ccw) |
919 | return; | |
920 | ||
48720ba5 | 921 | config_area = ccw_device_dma_zalloc(vcdev->cdev, |
e3e9bda3 HP |
922 | VIRTIO_CCW_CONFIG_SIZE, |
923 | &ccw->cda); | |
73fa21ea CH |
924 | if (!config_area) |
925 | goto out_free; | |
7e64e059 CH |
926 | |
927 | /* Read the config area from the host. */ | |
73fa21ea CH |
928 | ccw->cmd_code = CCW_CMD_READ_CONF; |
929 | ccw->flags = 0; | |
930 | ccw->count = offset + len; | |
73fa21ea | 931 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG); |
7e64e059 | 932 | if (ret) |
73fa21ea | 933 | goto out_free; |
7e64e059 | 934 | |
2448a299 | 935 | spin_lock_irqsave(&vcdev->lock, flags); |
431dae77 | 936 | memcpy(vcdev->config, config_area, offset + len); |
431dae77 CH |
937 | if (vcdev->config_ready < offset + len) |
938 | vcdev->config_ready = offset + len; | |
2448a299 HP |
939 | spin_unlock_irqrestore(&vcdev->lock, flags); |
940 | if (buf) | |
941 | memcpy(buf, config_area + offset, len); | |
73fa21ea CH |
942 | |
943 | out_free: | |
48720ba5 HP |
944 | ccw_device_dma_free(vcdev->cdev, config_area, VIRTIO_CCW_CONFIG_SIZE); |
945 | ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw)); | |
7e64e059 CH |
946 | } |
947 | ||
948 | static void virtio_ccw_set_config(struct virtio_device *vdev, | |
949 | unsigned int offset, const void *buf, | |
950 | unsigned len) | |
951 | { | |
952 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); | |
73fa21ea CH |
953 | struct ccw1 *ccw; |
954 | void *config_area; | |
2448a299 | 955 | unsigned long flags; |
73fa21ea | 956 | |
e3e9bda3 | 957 | ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL); |
73fa21ea CH |
958 | if (!ccw) |
959 | return; | |
960 | ||
48720ba5 | 961 | config_area = ccw_device_dma_zalloc(vcdev->cdev, |
e3e9bda3 HP |
962 | VIRTIO_CCW_CONFIG_SIZE, |
963 | &ccw->cda); | |
73fa21ea CH |
964 | if (!config_area) |
965 | goto out_free; | |
7e64e059 | 966 | |
431dae77 CH |
967 | /* Make sure we don't overwrite fields. */ |
968 | if (vcdev->config_ready < offset) | |
969 | virtio_ccw_get_config(vdev, 0, NULL, offset); | |
2448a299 | 970 | spin_lock_irqsave(&vcdev->lock, flags); |
7e64e059 CH |
971 | memcpy(&vcdev->config[offset], buf, len); |
972 | /* Write the config area to the host. */ | |
73fa21ea | 973 | memcpy(config_area, vcdev->config, sizeof(vcdev->config)); |
2448a299 | 974 | spin_unlock_irqrestore(&vcdev->lock, flags); |
73fa21ea CH |
975 | ccw->cmd_code = CCW_CMD_WRITE_CONF; |
976 | ccw->flags = 0; | |
977 | ccw->count = offset + len; | |
73fa21ea CH |
978 | ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG); |
979 | ||
980 | out_free: | |
48720ba5 HP |
981 | ccw_device_dma_free(vcdev->cdev, config_area, VIRTIO_CCW_CONFIG_SIZE); |
982 | ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw)); | |
7e64e059 CH |
983 | } |
984 | ||
985 | static u8 virtio_ccw_get_status(struct virtio_device *vdev) | |
986 | { | |
987 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); | |
48720ba5 | 988 | u8 old_status = vcdev->dma_area->status; |
7d3ce5ab PM |
989 | struct ccw1 *ccw; |
990 | ||
182f709c | 991 | if (vcdev->revision < 2) |
48720ba5 | 992 | return vcdev->dma_area->status; |
7d3ce5ab | 993 | |
e3e9bda3 | 994 | ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL); |
7d3ce5ab PM |
995 | if (!ccw) |
996 | return old_status; | |
997 | ||
998 | ccw->cmd_code = CCW_CMD_READ_STATUS; | |
999 | ccw->flags = 0; | |
48720ba5 | 1000 | ccw->count = sizeof(vcdev->dma_area->status); |
8adc56b0 | 1001 | ccw->cda = status_dma(vcdev); |
7d3ce5ab PM |
1002 | ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_STATUS); |
1003 | /* | |
1004 | * If the channel program failed (should only happen if the device | |
1005 | * was hotunplugged, and then we clean up via the machine check | |
48720ba5 | 1006 | * handler anyway), vcdev->dma_area->status was not overwritten and we just |
7d3ce5ab PM |
1007 | * return the old status, which is fine. |
1008 | */ | |
48720ba5 | 1009 | ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw)); |
7e64e059 | 1010 | |
48720ba5 | 1011 | return vcdev->dma_area->status; |
7e64e059 CH |
1012 | } |
1013 | ||
1014 | static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status) | |
1015 | { | |
1016 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); | |
48720ba5 | 1017 | u8 old_status = vcdev->dma_area->status; |
73fa21ea | 1018 | struct ccw1 *ccw; |
48555136 | 1019 | int ret; |
73fa21ea | 1020 | |
e3e9bda3 | 1021 | ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL); |
73fa21ea CH |
1022 | if (!ccw) |
1023 | return; | |
7e64e059 CH |
1024 | |
1025 | /* Write the status to the host. */ | |
48720ba5 | 1026 | vcdev->dma_area->status = status; |
73fa21ea CH |
1027 | ccw->cmd_code = CCW_CMD_WRITE_STATUS; |
1028 | ccw->flags = 0; | |
1029 | ccw->count = sizeof(status); | |
8b4ec69d JW |
1030 | /* We use ssch for setting the status which is a serializing |
1031 | * instruction that guarantees the memory writes have | |
1032 | * completed before ssch. | |
1033 | */ | |
8adc56b0 | 1034 | ccw->cda = status_dma(vcdev); |
48555136 MT |
1035 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS); |
1036 | /* Write failed? We assume status is unchanged. */ | |
1037 | if (ret) | |
48720ba5 HP |
1038 | vcdev->dma_area->status = old_status; |
1039 | ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw)); | |
7e64e059 CH |
1040 | } |
1041 | ||
971bedca CH |
1042 | static const char *virtio_ccw_bus_name(struct virtio_device *vdev) |
1043 | { | |
1044 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); | |
1045 | ||
1046 | return dev_name(&vcdev->cdev->dev); | |
1047 | } | |
1048 | ||
3a232277 JW |
1049 | static void virtio_ccw_synchronize_cbs(struct virtio_device *vdev) |
1050 | { | |
1051 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); | |
1052 | struct airq_info *info = vcdev->airq_info; | |
1053 | ||
1054 | if (info) { | |
1055 | /* | |
1056 | * This device uses adapter interrupts: synchronize with | |
1057 | * vring_interrupt() called by virtio_airq_handler() | |
1058 | * via the indicator area lock. | |
1059 | */ | |
1060 | write_lock_irq(&info->lock); | |
1061 | write_unlock_irq(&info->lock); | |
1062 | } else { | |
1063 | /* This device uses classic interrupts: synchronize | |
1064 | * with vring_interrupt() called by | |
1065 | * virtio_ccw_int_handler() via the per-device | |
1066 | * irq_lock | |
1067 | */ | |
1068 | write_lock_irq(&vcdev->irq_lock); | |
1069 | write_unlock_irq(&vcdev->irq_lock); | |
1070 | } | |
1071 | } | |
1072 | ||
0db1dba5 | 1073 | static const struct virtio_config_ops virtio_ccw_config_ops = { |
7e64e059 CH |
1074 | .get_features = virtio_ccw_get_features, |
1075 | .finalize_features = virtio_ccw_finalize_features, | |
1076 | .get = virtio_ccw_get_config, | |
1077 | .set = virtio_ccw_set_config, | |
1078 | .get_status = virtio_ccw_get_status, | |
1079 | .set_status = virtio_ccw_set_status, | |
1080 | .reset = virtio_ccw_reset, | |
b49503ea | 1081 | .find_vqs = virtio_ccw_find_vqs, |
7e64e059 | 1082 | .del_vqs = virtio_ccw_del_vqs, |
971bedca | 1083 | .bus_name = virtio_ccw_bus_name, |
3a232277 | 1084 | .synchronize_cbs = virtio_ccw_synchronize_cbs, |
7e64e059 CH |
1085 | }; |
1086 | ||
1087 | ||
1088 | /* | |
1089 | * ccw bus driver related functions | |
1090 | */ | |
1091 | ||
1092 | static void virtio_ccw_release_dev(struct device *_d) | |
1093 | { | |
15e90f52 | 1094 | struct virtio_device *dev = dev_to_virtio(_d); |
7e64e059 CH |
1095 | struct virtio_ccw_device *vcdev = to_vc_device(dev); |
1096 | ||
48720ba5 HP |
1097 | ccw_device_dma_free(vcdev->cdev, vcdev->dma_area, |
1098 | sizeof(*vcdev->dma_area)); | |
7e64e059 CH |
1099 | kfree(vcdev); |
1100 | } | |
1101 | ||
1102 | static int irb_is_error(struct irb *irb) | |
1103 | { | |
1104 | if (scsw_cstat(&irb->scsw) != 0) | |
1105 | return 1; | |
1106 | if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END)) | |
1107 | return 1; | |
1108 | if (scsw_cc(&irb->scsw) != 0) | |
1109 | return 1; | |
1110 | return 0; | |
1111 | } | |
1112 | ||
1113 | static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev, | |
1114 | int index) | |
1115 | { | |
1116 | struct virtio_ccw_vq_info *info; | |
1117 | unsigned long flags; | |
1118 | struct virtqueue *vq; | |
1119 | ||
1120 | vq = NULL; | |
1121 | spin_lock_irqsave(&vcdev->lock, flags); | |
1122 | list_for_each_entry(info, &vcdev->virtqueues, node) { | |
9d0ca6ed | 1123 | if (info->vq->index == index) { |
7e64e059 CH |
1124 | vq = info->vq; |
1125 | break; | |
1126 | } | |
1127 | } | |
1128 | spin_unlock_irqrestore(&vcdev->lock, flags); | |
1129 | return vq; | |
1130 | } | |
1131 | ||
74a599f0 CH |
1132 | static void virtio_ccw_check_activity(struct virtio_ccw_device *vcdev, |
1133 | __u32 activity) | |
1134 | { | |
1135 | if (vcdev->curr_io & activity) { | |
1136 | switch (activity) { | |
1137 | case VIRTIO_CCW_DOING_READ_FEAT: | |
1138 | case VIRTIO_CCW_DOING_WRITE_FEAT: | |
1139 | case VIRTIO_CCW_DOING_READ_CONFIG: | |
1140 | case VIRTIO_CCW_DOING_WRITE_CONFIG: | |
1141 | case VIRTIO_CCW_DOING_WRITE_STATUS: | |
7d3ce5ab | 1142 | case VIRTIO_CCW_DOING_READ_STATUS: |
74a599f0 CH |
1143 | case VIRTIO_CCW_DOING_SET_VQ: |
1144 | case VIRTIO_CCW_DOING_SET_IND: | |
1145 | case VIRTIO_CCW_DOING_SET_CONF_IND: | |
1146 | case VIRTIO_CCW_DOING_RESET: | |
1147 | case VIRTIO_CCW_DOING_READ_VQ_CONF: | |
1148 | case VIRTIO_CCW_DOING_SET_IND_ADAPTER: | |
1149 | case VIRTIO_CCW_DOING_SET_VIRTIO_REV: | |
1150 | vcdev->curr_io &= ~activity; | |
1151 | wake_up(&vcdev->wait_q); | |
1152 | break; | |
1153 | default: | |
1154 | /* don't know what to do... */ | |
1155 | dev_warn(&vcdev->cdev->dev, | |
1156 | "Suspicious activity '%08x'\n", activity); | |
1157 | WARN_ON(1); | |
1158 | break; | |
1159 | } | |
1160 | } | |
1161 | } | |
1162 | ||
7e64e059 CH |
1163 | static void virtio_ccw_int_handler(struct ccw_device *cdev, |
1164 | unsigned long intparm, | |
1165 | struct irb *irb) | |
1166 | { | |
1167 | __u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK; | |
1168 | struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev); | |
1169 | int i; | |
1170 | struct virtqueue *vq; | |
7e64e059 | 1171 | |
2e021043 HG |
1172 | if (!vcdev) |
1173 | return; | |
74a599f0 CH |
1174 | if (IS_ERR(irb)) { |
1175 | vcdev->err = PTR_ERR(irb); | |
1176 | virtio_ccw_check_activity(vcdev, activity); | |
1177 | /* Don't poke around indicators, something's wrong. */ | |
1178 | return; | |
1179 | } | |
7e64e059 CH |
1180 | /* Check if it's a notification from the host. */ |
1181 | if ((intparm == 0) && | |
1182 | (scsw_stctl(&irb->scsw) == | |
1183 | (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) { | |
1184 | /* OK */ | |
1185 | } | |
19e4735b CH |
1186 | if (irb_is_error(irb)) { |
1187 | /* Command reject? */ | |
1188 | if ((scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) && | |
1189 | (irb->ecw[0] & SNS0_CMD_REJECT)) | |
1190 | vcdev->err = -EOPNOTSUPP; | |
1191 | else | |
1192 | /* Map everything else to -EIO. */ | |
1193 | vcdev->err = -EIO; | |
1194 | } | |
74a599f0 | 1195 | virtio_ccw_check_activity(vcdev, activity); |
c346dae4 JW |
1196 | #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION |
1197 | /* | |
1198 | * Paired with virtio_ccw_synchronize_cbs() and interrupts are | |
1199 | * disabled here. | |
1200 | */ | |
3a232277 | 1201 | read_lock(&vcdev->irq_lock); |
c346dae4 | 1202 | #endif |
22a4a639 HP |
1203 | for_each_set_bit(i, indicators(vcdev), |
1204 | sizeof(*indicators(vcdev)) * BITS_PER_BYTE) { | |
7e64e059 | 1205 | /* The bit clear must happen before the vring kick. */ |
22a4a639 | 1206 | clear_bit(i, indicators(vcdev)); |
7e64e059 CH |
1207 | barrier(); |
1208 | vq = virtio_ccw_vq_by_ind(vcdev, i); | |
1209 | vring_interrupt(0, vq); | |
1210 | } | |
c346dae4 | 1211 | #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION |
3a232277 | 1212 | read_unlock(&vcdev->irq_lock); |
c346dae4 | 1213 | #endif |
22a4a639 | 1214 | if (test_bit(0, indicators2(vcdev))) { |
016c98c6 | 1215 | virtio_config_changed(&vcdev->vdev); |
22a4a639 | 1216 | clear_bit(0, indicators2(vcdev)); |
7e64e059 CH |
1217 | } |
1218 | } | |
1219 | ||
1220 | /* | |
1221 | * We usually want to autoonline all devices, but give the admin | |
1222 | * a way to exempt devices from this. | |
1223 | */ | |
1224 | #define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \ | |
1225 | (8*sizeof(long))) | |
1226 | static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS]; | |
1227 | ||
1228 | static char *no_auto = ""; | |
1229 | ||
1230 | module_param(no_auto, charp, 0444); | |
1231 | MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined"); | |
1232 | ||
1233 | static int virtio_ccw_check_autoonline(struct ccw_device *cdev) | |
1234 | { | |
1235 | struct ccw_dev_id id; | |
1236 | ||
1237 | ccw_device_get_id(cdev, &id); | |
1238 | if (test_bit(id.devno, devs_no_auto[id.ssid])) | |
1239 | return 0; | |
1240 | return 1; | |
1241 | } | |
1242 | ||
1243 | static void virtio_ccw_auto_online(void *data, async_cookie_t cookie) | |
1244 | { | |
1245 | struct ccw_device *cdev = data; | |
1246 | int ret; | |
1247 | ||
1248 | ret = ccw_device_set_online(cdev); | |
1249 | if (ret) | |
1250 | dev_warn(&cdev->dev, "Failed to set online: %d\n", ret); | |
1251 | } | |
1252 | ||
1253 | static int virtio_ccw_probe(struct ccw_device *cdev) | |
1254 | { | |
1255 | cdev->handler = virtio_ccw_int_handler; | |
1256 | ||
1257 | if (virtio_ccw_check_autoonline(cdev)) | |
1258 | async_schedule(virtio_ccw_auto_online, cdev); | |
1259 | return 0; | |
1260 | } | |
1261 | ||
2e021043 HG |
1262 | static struct virtio_ccw_device *virtio_grab_drvdata(struct ccw_device *cdev) |
1263 | { | |
1264 | unsigned long flags; | |
1265 | struct virtio_ccw_device *vcdev; | |
1266 | ||
1267 | spin_lock_irqsave(get_ccwdev_lock(cdev), flags); | |
1268 | vcdev = dev_get_drvdata(&cdev->dev); | |
79629b20 | 1269 | if (!vcdev || vcdev->going_away) { |
2e021043 HG |
1270 | spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags); |
1271 | return NULL; | |
1272 | } | |
79629b20 | 1273 | vcdev->going_away = true; |
2e021043 HG |
1274 | spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags); |
1275 | return vcdev; | |
1276 | } | |
1277 | ||
7e64e059 CH |
1278 | static void virtio_ccw_remove(struct ccw_device *cdev) |
1279 | { | |
79629b20 | 1280 | unsigned long flags; |
2e021043 | 1281 | struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev); |
7e64e059 | 1282 | |
e75279c4 HG |
1283 | if (vcdev && cdev->online) { |
1284 | if (vcdev->device_lost) | |
1285 | virtio_break_device(&vcdev->vdev); | |
7e64e059 | 1286 | unregister_virtio_device(&vcdev->vdev); |
e75279c4 HG |
1287 | spin_lock_irqsave(get_ccwdev_lock(cdev), flags); |
1288 | dev_set_drvdata(&cdev->dev, NULL); | |
1289 | spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags); | |
1290 | } | |
7e64e059 CH |
1291 | cdev->handler = NULL; |
1292 | } | |
1293 | ||
1294 | static int virtio_ccw_offline(struct ccw_device *cdev) | |
1295 | { | |
79629b20 | 1296 | unsigned long flags; |
2e021043 | 1297 | struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev); |
7e64e059 | 1298 | |
e75279c4 HG |
1299 | if (!vcdev) |
1300 | return 0; | |
1301 | if (vcdev->device_lost) | |
1302 | virtio_break_device(&vcdev->vdev); | |
1303 | unregister_virtio_device(&vcdev->vdev); | |
1304 | spin_lock_irqsave(get_ccwdev_lock(cdev), flags); | |
1305 | dev_set_drvdata(&cdev->dev, NULL); | |
1306 | spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags); | |
7e64e059 CH |
1307 | return 0; |
1308 | } | |
1309 | ||
6bb2c835 TH |
1310 | static int virtio_ccw_set_transport_rev(struct virtio_ccw_device *vcdev) |
1311 | { | |
1312 | struct virtio_rev_info *rev; | |
1313 | struct ccw1 *ccw; | |
1314 | int ret; | |
1315 | ||
e3e9bda3 | 1316 | ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL); |
6bb2c835 TH |
1317 | if (!ccw) |
1318 | return -ENOMEM; | |
e3e9bda3 | 1319 | rev = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*rev), &ccw->cda); |
6bb2c835 | 1320 | if (!rev) { |
48720ba5 | 1321 | ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw)); |
6bb2c835 TH |
1322 | return -ENOMEM; |
1323 | } | |
1324 | ||
1325 | /* Set transport revision */ | |
1326 | ccw->cmd_code = CCW_CMD_SET_VIRTIO_REV; | |
1327 | ccw->flags = 0; | |
1328 | ccw->count = sizeof(*rev); | |
6bb2c835 TH |
1329 | |
1330 | vcdev->revision = VIRTIO_CCW_REV_MAX; | |
1331 | do { | |
1332 | rev->revision = vcdev->revision; | |
1333 | /* none of our supported revisions carry payload */ | |
1334 | rev->length = 0; | |
1335 | ret = ccw_io_helper(vcdev, ccw, | |
1336 | VIRTIO_CCW_DOING_SET_VIRTIO_REV); | |
1337 | if (ret == -EOPNOTSUPP) { | |
1338 | if (vcdev->revision == 0) | |
1339 | /* | |
1340 | * The host device does not support setting | |
1341 | * the revision: let's operate it in legacy | |
1342 | * mode. | |
1343 | */ | |
1344 | ret = 0; | |
1345 | else | |
1346 | vcdev->revision--; | |
1347 | } | |
1348 | } while (ret == -EOPNOTSUPP); | |
1349 | ||
48720ba5 HP |
1350 | ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw)); |
1351 | ccw_device_dma_free(vcdev->cdev, rev, sizeof(*rev)); | |
6bb2c835 TH |
1352 | return ret; |
1353 | } | |
7e64e059 | 1354 | |
7e64e059 CH |
1355 | static int virtio_ccw_online(struct ccw_device *cdev) |
1356 | { | |
1357 | int ret; | |
1358 | struct virtio_ccw_device *vcdev; | |
2e021043 | 1359 | unsigned long flags; |
7e64e059 CH |
1360 | |
1361 | vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL); | |
1362 | if (!vcdev) { | |
1363 | dev_warn(&cdev->dev, "Could not get memory for virtio\n"); | |
1364 | ret = -ENOMEM; | |
1365 | goto out_free; | |
1366 | } | |
f35f54f1 | 1367 | vcdev->vdev.dev.parent = &cdev->dev; |
48720ba5 HP |
1368 | vcdev->cdev = cdev; |
1369 | vcdev->dma_area = ccw_device_dma_zalloc(vcdev->cdev, | |
e3e9bda3 | 1370 | sizeof(*vcdev->dma_area), |
8adc56b0 | 1371 | &vcdev->dma_area_addr); |
48720ba5 | 1372 | if (!vcdev->dma_area) { |
7e64e059 CH |
1373 | ret = -ENOMEM; |
1374 | goto out_free; | |
1375 | } | |
1376 | ||
96b14536 CH |
1377 | vcdev->is_thinint = virtio_ccw_use_airq; /* at least try */ |
1378 | ||
7e64e059 CH |
1379 | vcdev->vdev.dev.release = virtio_ccw_release_dev; |
1380 | vcdev->vdev.config = &virtio_ccw_config_ops; | |
7e64e059 CH |
1381 | init_waitqueue_head(&vcdev->wait_q); |
1382 | INIT_LIST_HEAD(&vcdev->virtqueues); | |
1383 | spin_lock_init(&vcdev->lock); | |
3a232277 | 1384 | rwlock_init(&vcdev->irq_lock); |
78b1a52e | 1385 | mutex_init(&vcdev->io_lock); |
7e64e059 | 1386 | |
2e021043 | 1387 | spin_lock_irqsave(get_ccwdev_lock(cdev), flags); |
7e64e059 | 1388 | dev_set_drvdata(&cdev->dev, vcdev); |
2e021043 | 1389 | spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags); |
7e64e059 CH |
1390 | vcdev->vdev.id.vendor = cdev->id.cu_type; |
1391 | vcdev->vdev.id.device = cdev->id.cu_model; | |
6bb2c835 | 1392 | |
2003a7a5 MT |
1393 | ret = virtio_ccw_set_transport_rev(vcdev); |
1394 | if (ret) | |
1395 | goto out_free; | |
6bb2c835 | 1396 | |
7e64e059 CH |
1397 | ret = register_virtio_device(&vcdev->vdev); |
1398 | if (ret) { | |
1399 | dev_warn(&cdev->dev, "Failed to register virtio device: %d\n", | |
1400 | ret); | |
1401 | goto out_put; | |
1402 | } | |
1403 | return 0; | |
1404 | out_put: | |
2e021043 | 1405 | spin_lock_irqsave(get_ccwdev_lock(cdev), flags); |
7e64e059 | 1406 | dev_set_drvdata(&cdev->dev, NULL); |
2e021043 | 1407 | spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags); |
7e64e059 CH |
1408 | put_device(&vcdev->vdev.dev); |
1409 | return ret; | |
1410 | out_free: | |
1411 | if (vcdev) { | |
48720ba5 HP |
1412 | ccw_device_dma_free(vcdev->cdev, vcdev->dma_area, |
1413 | sizeof(*vcdev->dma_area)); | |
7e64e059 CH |
1414 | } |
1415 | kfree(vcdev); | |
1416 | return ret; | |
1417 | } | |
1418 | ||
1419 | static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event) | |
1420 | { | |
e75279c4 HG |
1421 | int rc; |
1422 | struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev); | |
1423 | ||
1424 | /* | |
1425 | * Make sure vcdev is set | |
1426 | * i.e. set_offline/remove callback not already running | |
1427 | */ | |
1428 | if (!vcdev) | |
1429 | return NOTIFY_DONE; | |
1430 | ||
1431 | switch (event) { | |
1432 | case CIO_GONE: | |
1433 | vcdev->device_lost = true; | |
1434 | rc = NOTIFY_DONE; | |
1435 | break; | |
fa08a3b4 CB |
1436 | case CIO_OPER: |
1437 | rc = NOTIFY_OK; | |
1438 | break; | |
e75279c4 HG |
1439 | default: |
1440 | rc = NOTIFY_DONE; | |
1441 | break; | |
1442 | } | |
1443 | return rc; | |
7e64e059 CH |
1444 | } |
1445 | ||
1446 | static struct ccw_device_id virtio_ids[] = { | |
1447 | { CCW_DEVICE(0x3832, 0) }, | |
1448 | {}, | |
1449 | }; | |
7e64e059 CH |
1450 | |
1451 | static struct ccw_driver virtio_ccw_driver = { | |
1452 | .driver = { | |
1453 | .owner = THIS_MODULE, | |
1454 | .name = "virtio_ccw", | |
1455 | }, | |
1456 | .ids = virtio_ids, | |
1457 | .probe = virtio_ccw_probe, | |
1458 | .remove = virtio_ccw_remove, | |
1459 | .set_offline = virtio_ccw_offline, | |
1460 | .set_online = virtio_ccw_online, | |
1461 | .notify = virtio_ccw_cio_notify, | |
89f88337 | 1462 | .int_class = IRQIO_VIR, |
7e64e059 CH |
1463 | }; |
1464 | ||
1465 | static int __init pure_hex(char **cp, unsigned int *val, int min_digit, | |
1466 | int max_digit, int max_val) | |
1467 | { | |
1468 | int diff; | |
1469 | ||
1470 | diff = 0; | |
1471 | *val = 0; | |
1472 | ||
1473 | while (diff <= max_digit) { | |
1474 | int value = hex_to_bin(**cp); | |
1475 | ||
1476 | if (value < 0) | |
1477 | break; | |
1478 | *val = *val * 16 + value; | |
1479 | (*cp)++; | |
1480 | diff++; | |
1481 | } | |
1482 | ||
1483 | if ((diff < min_digit) || (diff > max_digit) || (*val > max_val)) | |
1484 | return 1; | |
1485 | ||
1486 | return 0; | |
1487 | } | |
1488 | ||
1489 | static int __init parse_busid(char *str, unsigned int *cssid, | |
1490 | unsigned int *ssid, unsigned int *devno) | |
1491 | { | |
1492 | char *str_work; | |
1493 | int rc, ret; | |
1494 | ||
1495 | rc = 1; | |
1496 | ||
1497 | if (*str == '\0') | |
1498 | goto out; | |
1499 | ||
1500 | str_work = str; | |
1501 | ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID); | |
1502 | if (ret || (str_work[0] != '.')) | |
1503 | goto out; | |
1504 | str_work++; | |
1505 | ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID); | |
1506 | if (ret || (str_work[0] != '.')) | |
1507 | goto out; | |
1508 | str_work++; | |
1509 | ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL); | |
1510 | if (ret || (str_work[0] != '\0')) | |
1511 | goto out; | |
1512 | ||
1513 | rc = 0; | |
1514 | out: | |
1515 | return rc; | |
1516 | } | |
1517 | ||
1518 | static void __init no_auto_parse(void) | |
1519 | { | |
1520 | unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to; | |
1521 | char *parm, *str; | |
1522 | int rc; | |
1523 | ||
1524 | str = no_auto; | |
1525 | while ((parm = strsep(&str, ","))) { | |
1526 | rc = parse_busid(strsep(&parm, "-"), &from_cssid, | |
1527 | &from_ssid, &from); | |
1528 | if (rc) | |
1529 | continue; | |
1530 | if (parm != NULL) { | |
1531 | rc = parse_busid(parm, &to_cssid, | |
1532 | &to_ssid, &to); | |
1533 | if ((from_ssid > to_ssid) || | |
1534 | ((from_ssid == to_ssid) && (from > to))) | |
1535 | rc = -EINVAL; | |
1536 | } else { | |
1537 | to_cssid = from_cssid; | |
1538 | to_ssid = from_ssid; | |
1539 | to = from; | |
1540 | } | |
1541 | if (rc) | |
1542 | continue; | |
1543 | while ((from_ssid < to_ssid) || | |
1544 | ((from_ssid == to_ssid) && (from <= to))) { | |
1545 | set_bit(from, devs_no_auto[from_ssid]); | |
1546 | from++; | |
1547 | if (from > __MAX_SUBCHANNEL) { | |
1548 | from_ssid++; | |
1549 | from = 0; | |
1550 | } | |
1551 | } | |
1552 | } | |
1553 | } | |
1554 | ||
1555 | static int __init virtio_ccw_init(void) | |
1556 | { | |
39c7dcb1 HP |
1557 | int rc; |
1558 | ||
7e64e059 CH |
1559 | /* parse no_auto string before we do anything further */ |
1560 | no_auto_parse(); | |
39c7dcb1 HP |
1561 | |
1562 | summary_indicators = cio_dma_zalloc(MAX_AIRQ_AREAS); | |
1563 | if (!summary_indicators) | |
1564 | return -ENOMEM; | |
1565 | rc = ccw_driver_register(&virtio_ccw_driver); | |
1566 | if (rc) | |
1567 | cio_dma_free(summary_indicators, MAX_AIRQ_AREAS); | |
1568 | return rc; | |
7e64e059 | 1569 | } |
acc50ec7 | 1570 | device_initcall(virtio_ccw_init); |