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