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