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