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