Merge remote-tracking branches 'asoc/fix/tlv320aic3x' and 'asoc/fix/wm8962' into...
[linux-drm-fsl-dcu.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/module.h>
28 #include <linux/io.h>
29 #include <linux/kvm_para.h>
30 #include <linux/notifier.h>
31 #include <asm/setup.h>
32 #include <asm/irq.h>
33 #include <asm/cio.h>
34 #include <asm/ccwdev.h>
35 #include <asm/virtio-ccw.h>
36 #include <asm/isc.h>
37 #include <asm/airq.h>
38
39 /*
40  * virtio related functions
41  */
42
43 struct vq_config_block {
44         __u16 index;
45         __u16 num;
46 } __packed;
47
48 #define VIRTIO_CCW_CONFIG_SIZE 0x100
49 /* same as PCI config space size, should be enough for all drivers */
50
51 struct virtio_ccw_device {
52         struct virtio_device vdev;
53         __u8 *status;
54         __u8 config[VIRTIO_CCW_CONFIG_SIZE];
55         struct ccw_device *cdev;
56         __u32 curr_io;
57         int err;
58         unsigned int revision; /* Transport revision */
59         wait_queue_head_t wait_q;
60         spinlock_t lock;
61         struct list_head virtqueues;
62         unsigned long indicators;
63         unsigned long indicators2;
64         struct vq_config_block *config_block;
65         bool is_thinint;
66         bool going_away;
67         bool device_lost;
68         unsigned int config_ready;
69         void *airq_info;
70 };
71
72 struct vq_info_block_legacy {
73         __u64 queue;
74         __u32 align;
75         __u16 index;
76         __u16 num;
77 } __packed;
78
79 struct vq_info_block {
80         __u64 desc;
81         __u32 res0;
82         __u16 index;
83         __u16 num;
84         __u64 avail;
85         __u64 used;
86 } __packed;
87
88 struct virtio_feature_desc {
89         __u32 features;
90         __u8 index;
91 } __packed;
92
93 struct virtio_thinint_area {
94         unsigned long summary_indicator;
95         unsigned long indicator;
96         u64 bit_nr;
97         u8 isc;
98 } __packed;
99
100 struct virtio_rev_info {
101         __u16 revision;
102         __u16 length;
103         __u8 data[];
104 };
105
106 /* the highest virtio-ccw revision we support */
107 #define VIRTIO_CCW_REV_MAX 1
108
109 struct virtio_ccw_vq_info {
110         struct virtqueue *vq;
111         int num;
112         void *queue;
113         union {
114                 struct vq_info_block s;
115                 struct vq_info_block_legacy l;
116         } *info_block;
117         int bit_nr;
118         struct list_head node;
119         long cookie;
120 };
121
122 #define VIRTIO_AIRQ_ISC IO_SCH_ISC /* inherit from subchannel */
123
124 #define VIRTIO_IV_BITS (L1_CACHE_BYTES * 8)
125 #define MAX_AIRQ_AREAS 20
126
127 static int virtio_ccw_use_airq = 1;
128
129 struct airq_info {
130         rwlock_t lock;
131         u8 summary_indicator;
132         struct airq_struct airq;
133         struct airq_iv *aiv;
134 };
135 static struct airq_info *airq_areas[MAX_AIRQ_AREAS];
136
137 #define CCW_CMD_SET_VQ 0x13
138 #define CCW_CMD_VDEV_RESET 0x33
139 #define CCW_CMD_SET_IND 0x43
140 #define CCW_CMD_SET_CONF_IND 0x53
141 #define CCW_CMD_READ_FEAT 0x12
142 #define CCW_CMD_WRITE_FEAT 0x11
143 #define CCW_CMD_READ_CONF 0x22
144 #define CCW_CMD_WRITE_CONF 0x21
145 #define CCW_CMD_WRITE_STATUS 0x31
146 #define CCW_CMD_READ_VQ_CONF 0x32
147 #define CCW_CMD_SET_IND_ADAPTER 0x73
148 #define CCW_CMD_SET_VIRTIO_REV 0x83
149
150 #define VIRTIO_CCW_DOING_SET_VQ 0x00010000
151 #define VIRTIO_CCW_DOING_RESET 0x00040000
152 #define VIRTIO_CCW_DOING_READ_FEAT 0x00080000
153 #define VIRTIO_CCW_DOING_WRITE_FEAT 0x00100000
154 #define VIRTIO_CCW_DOING_READ_CONFIG 0x00200000
155 #define VIRTIO_CCW_DOING_WRITE_CONFIG 0x00400000
156 #define VIRTIO_CCW_DOING_WRITE_STATUS 0x00800000
157 #define VIRTIO_CCW_DOING_SET_IND 0x01000000
158 #define VIRTIO_CCW_DOING_READ_VQ_CONF 0x02000000
159 #define VIRTIO_CCW_DOING_SET_CONF_IND 0x04000000
160 #define VIRTIO_CCW_DOING_SET_IND_ADAPTER 0x08000000
161 #define VIRTIO_CCW_DOING_SET_VIRTIO_REV 0x10000000
162 #define VIRTIO_CCW_INTPARM_MASK 0xffff0000
163
164 static struct virtio_ccw_device *to_vc_device(struct virtio_device *vdev)
165 {
166         return container_of(vdev, struct virtio_ccw_device, vdev);
167 }
168
169 static void drop_airq_indicator(struct virtqueue *vq, struct airq_info *info)
170 {
171         unsigned long i, flags;
172
173         write_lock_irqsave(&info->lock, flags);
174         for (i = 0; i < airq_iv_end(info->aiv); i++) {
175                 if (vq == (void *)airq_iv_get_ptr(info->aiv, i)) {
176                         airq_iv_free_bit(info->aiv, i);
177                         airq_iv_set_ptr(info->aiv, i, 0);
178                         break;
179                 }
180         }
181         write_unlock_irqrestore(&info->lock, flags);
182 }
183
184 static void virtio_airq_handler(struct airq_struct *airq)
185 {
186         struct airq_info *info = container_of(airq, struct airq_info, airq);
187         unsigned long ai;
188
189         inc_irq_stat(IRQIO_VAI);
190         read_lock(&info->lock);
191         /* Walk through indicators field, summary indicator active. */
192         for (ai = 0;;) {
193                 ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
194                 if (ai == -1UL)
195                         break;
196                 vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
197         }
198         info->summary_indicator = 0;
199         smp_wmb();
200         /* Walk through indicators field, summary indicator not active. */
201         for (ai = 0;;) {
202                 ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
203                 if (ai == -1UL)
204                         break;
205                 vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
206         }
207         read_unlock(&info->lock);
208 }
209
210 static struct airq_info *new_airq_info(void)
211 {
212         struct airq_info *info;
213         int rc;
214
215         info = kzalloc(sizeof(*info), GFP_KERNEL);
216         if (!info)
217                 return NULL;
218         rwlock_init(&info->lock);
219         info->aiv = airq_iv_create(VIRTIO_IV_BITS, AIRQ_IV_ALLOC | AIRQ_IV_PTR);
220         if (!info->aiv) {
221                 kfree(info);
222                 return NULL;
223         }
224         info->airq.handler = virtio_airq_handler;
225         info->airq.lsi_ptr = &info->summary_indicator;
226         info->airq.lsi_mask = 0xff;
227         info->airq.isc = VIRTIO_AIRQ_ISC;
228         rc = register_adapter_interrupt(&info->airq);
229         if (rc) {
230                 airq_iv_release(info->aiv);
231                 kfree(info);
232                 return NULL;
233         }
234         return info;
235 }
236
237 static void destroy_airq_info(struct airq_info *info)
238 {
239         if (!info)
240                 return;
241
242         unregister_adapter_interrupt(&info->airq);
243         airq_iv_release(info->aiv);
244         kfree(info);
245 }
246
247 static unsigned long get_airq_indicator(struct virtqueue *vqs[], int nvqs,
248                                         u64 *first, void **airq_info)
249 {
250         int i, j;
251         struct airq_info *info;
252         unsigned long indicator_addr = 0;
253         unsigned long bit, flags;
254
255         for (i = 0; i < MAX_AIRQ_AREAS && !indicator_addr; i++) {
256                 if (!airq_areas[i])
257                         airq_areas[i] = new_airq_info();
258                 info = airq_areas[i];
259                 if (!info)
260                         return 0;
261                 write_lock_irqsave(&info->lock, flags);
262                 bit = airq_iv_alloc(info->aiv, nvqs);
263                 if (bit == -1UL) {
264                         /* Not enough vacancies. */
265                         write_unlock_irqrestore(&info->lock, flags);
266                         continue;
267                 }
268                 *first = bit;
269                 *airq_info = info;
270                 indicator_addr = (unsigned long)info->aiv->vector;
271                 for (j = 0; j < nvqs; j++) {
272                         airq_iv_set_ptr(info->aiv, bit + j,
273                                         (unsigned long)vqs[j]);
274                 }
275                 write_unlock_irqrestore(&info->lock, flags);
276         }
277         return indicator_addr;
278 }
279
280 static void virtio_ccw_drop_indicators(struct virtio_ccw_device *vcdev)
281 {
282         struct virtio_ccw_vq_info *info;
283
284         list_for_each_entry(info, &vcdev->virtqueues, node)
285                 drop_airq_indicator(info->vq, vcdev->airq_info);
286 }
287
288 static int doing_io(struct virtio_ccw_device *vcdev, __u32 flag)
289 {
290         unsigned long flags;
291         __u32 ret;
292
293         spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
294         if (vcdev->err)
295                 ret = 0;
296         else
297                 ret = vcdev->curr_io & flag;
298         spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
299         return ret;
300 }
301
302 static int ccw_io_helper(struct virtio_ccw_device *vcdev,
303                          struct ccw1 *ccw, __u32 intparm)
304 {
305         int ret;
306         unsigned long flags;
307         int flag = intparm & VIRTIO_CCW_INTPARM_MASK;
308
309         do {
310                 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
311                 ret = ccw_device_start(vcdev->cdev, ccw, intparm, 0, 0);
312                 if (!ret) {
313                         if (!vcdev->curr_io)
314                                 vcdev->err = 0;
315                         vcdev->curr_io |= flag;
316                 }
317                 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
318                 cpu_relax();
319         } while (ret == -EBUSY);
320         wait_event(vcdev->wait_q, doing_io(vcdev, flag) == 0);
321         return ret ? ret : vcdev->err;
322 }
323
324 static void virtio_ccw_drop_indicator(struct virtio_ccw_device *vcdev,
325                                       struct ccw1 *ccw)
326 {
327         int ret;
328         unsigned long *indicatorp = NULL;
329         struct virtio_thinint_area *thinint_area = NULL;
330         struct airq_info *airq_info = vcdev->airq_info;
331
332         if (vcdev->is_thinint) {
333                 thinint_area = kzalloc(sizeof(*thinint_area),
334                                        GFP_DMA | GFP_KERNEL);
335                 if (!thinint_area)
336                         return;
337                 thinint_area->summary_indicator =
338                         (unsigned long) &airq_info->summary_indicator;
339                 thinint_area->isc = VIRTIO_AIRQ_ISC;
340                 ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
341                 ccw->count = sizeof(*thinint_area);
342                 ccw->cda = (__u32)(unsigned long) thinint_area;
343         } else {
344                 indicatorp = kmalloc(sizeof(&vcdev->indicators),
345                                      GFP_DMA | GFP_KERNEL);
346                 if (!indicatorp)
347                         return;
348                 *indicatorp = 0;
349                 ccw->cmd_code = CCW_CMD_SET_IND;
350                 ccw->count = sizeof(vcdev->indicators);
351                 ccw->cda = (__u32)(unsigned long) indicatorp;
352         }
353         /* Deregister indicators from host. */
354         vcdev->indicators = 0;
355         ccw->flags = 0;
356         ret = ccw_io_helper(vcdev, ccw,
357                             vcdev->is_thinint ?
358                             VIRTIO_CCW_DOING_SET_IND_ADAPTER :
359                             VIRTIO_CCW_DOING_SET_IND);
360         if (ret && (ret != -ENODEV))
361                 dev_info(&vcdev->cdev->dev,
362                          "Failed to deregister indicators (%d)\n", ret);
363         else if (vcdev->is_thinint)
364                 virtio_ccw_drop_indicators(vcdev);
365         kfree(indicatorp);
366         kfree(thinint_area);
367 }
368
369 static inline long do_kvm_notify(struct subchannel_id schid,
370                                  unsigned long queue_index,
371                                  long cookie)
372 {
373         register unsigned long __nr asm("1") = KVM_S390_VIRTIO_CCW_NOTIFY;
374         register struct subchannel_id __schid asm("2") = schid;
375         register unsigned long __index asm("3") = queue_index;
376         register long __rc asm("2");
377         register long __cookie asm("4") = cookie;
378
379         asm volatile ("diag 2,4,0x500\n"
380                       : "=d" (__rc) : "d" (__nr), "d" (__schid), "d" (__index),
381                       "d"(__cookie)
382                       : "memory", "cc");
383         return __rc;
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 *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         /* We need a data area under 2G to communicate. */
651         indicatorp = kmalloc(sizeof(&vcdev->indicators), GFP_DMA | GFP_KERNEL);
652         if (!indicatorp)
653                 goto out;
654         *indicatorp = (unsigned long) &vcdev->indicators;
655         if (vcdev->is_thinint) {
656                 ret = virtio_ccw_register_adapter_ind(vcdev, vqs, nvqs, ccw);
657                 if (ret)
658                         /* no error, just fall back to legacy interrupts */
659                         vcdev->is_thinint = 0;
660         }
661         if (!vcdev->is_thinint) {
662                 /* Register queue indicators with host. */
663                 vcdev->indicators = 0;
664                 ccw->cmd_code = CCW_CMD_SET_IND;
665                 ccw->flags = 0;
666                 ccw->count = sizeof(vcdev->indicators);
667                 ccw->cda = (__u32)(unsigned long) indicatorp;
668                 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND);
669                 if (ret)
670                         goto out;
671         }
672         /* Register indicators2 with host for config changes */
673         *indicatorp = (unsigned long) &vcdev->indicators2;
674         vcdev->indicators2 = 0;
675         ccw->cmd_code = CCW_CMD_SET_CONF_IND;
676         ccw->flags = 0;
677         ccw->count = sizeof(vcdev->indicators2);
678         ccw->cda = (__u32)(unsigned long) indicatorp;
679         ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_CONF_IND);
680         if (ret)
681                 goto out;
682
683         kfree(indicatorp);
684         kfree(ccw);
685         return 0;
686 out:
687         kfree(indicatorp);
688         kfree(ccw);
689         virtio_ccw_del_vqs(vdev);
690         return ret;
691 }
692
693 static void virtio_ccw_reset(struct virtio_device *vdev)
694 {
695         struct virtio_ccw_device *vcdev = to_vc_device(vdev);
696         struct ccw1 *ccw;
697
698         ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
699         if (!ccw)
700                 return;
701
702         /* Zero status bits. */
703         *vcdev->status = 0;
704
705         /* Send a reset ccw on device. */
706         ccw->cmd_code = CCW_CMD_VDEV_RESET;
707         ccw->flags = 0;
708         ccw->count = 0;
709         ccw->cda = 0;
710         ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_RESET);
711         kfree(ccw);
712 }
713
714 static u64 virtio_ccw_get_features(struct virtio_device *vdev)
715 {
716         struct virtio_ccw_device *vcdev = to_vc_device(vdev);
717         struct virtio_feature_desc *features;
718         int ret;
719         u64 rc;
720         struct ccw1 *ccw;
721
722         ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
723         if (!ccw)
724                 return 0;
725
726         features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
727         if (!features) {
728                 rc = 0;
729                 goto out_free;
730         }
731         /* Read the feature bits from the host. */
732         features->index = 0;
733         ccw->cmd_code = CCW_CMD_READ_FEAT;
734         ccw->flags = 0;
735         ccw->count = sizeof(*features);
736         ccw->cda = (__u32)(unsigned long)features;
737         ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
738         if (ret) {
739                 rc = 0;
740                 goto out_free;
741         }
742
743         rc = le32_to_cpu(features->features);
744
745         if (vcdev->revision == 0)
746                 goto out_free;
747
748         /* Read second half of the feature bits from the host. */
749         features->index = 1;
750         ccw->cmd_code = CCW_CMD_READ_FEAT;
751         ccw->flags = 0;
752         ccw->count = sizeof(*features);
753         ccw->cda = (__u32)(unsigned long)features;
754         ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
755         if (ret == 0)
756                 rc |= (u64)le32_to_cpu(features->features) << 32;
757
758 out_free:
759         kfree(features);
760         kfree(ccw);
761         return rc;
762 }
763
764 static int virtio_ccw_finalize_features(struct virtio_device *vdev)
765 {
766         struct virtio_ccw_device *vcdev = to_vc_device(vdev);
767         struct virtio_feature_desc *features;
768         struct ccw1 *ccw;
769         int ret;
770
771         if (vcdev->revision >= 1 &&
772             !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
773                 dev_err(&vdev->dev, "virtio: device uses revision 1 "
774                         "but does not have VIRTIO_F_VERSION_1\n");
775                 return -EINVAL;
776         }
777
778         ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
779         if (!ccw)
780                 return -ENOMEM;
781
782         features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
783         if (!features) {
784                 ret = -ENOMEM;
785                 goto out_free;
786         }
787         /* Give virtio_ring a chance to accept features. */
788         vring_transport_features(vdev);
789
790         features->index = 0;
791         features->features = cpu_to_le32((u32)vdev->features);
792         /* Write the first half of the feature bits to the host. */
793         ccw->cmd_code = CCW_CMD_WRITE_FEAT;
794         ccw->flags = 0;
795         ccw->count = sizeof(*features);
796         ccw->cda = (__u32)(unsigned long)features;
797         ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
798         if (ret)
799                 goto out_free;
800
801         if (vcdev->revision == 0)
802                 goto out_free;
803
804         features->index = 1;
805         features->features = cpu_to_le32(vdev->features >> 32);
806         /* Write the second half of the feature bits to the host. */
807         ccw->cmd_code = CCW_CMD_WRITE_FEAT;
808         ccw->flags = 0;
809         ccw->count = sizeof(*features);
810         ccw->cda = (__u32)(unsigned long)features;
811         ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
812
813 out_free:
814         kfree(features);
815         kfree(ccw);
816
817         return ret;
818 }
819
820 static void virtio_ccw_get_config(struct virtio_device *vdev,
821                                   unsigned int offset, void *buf, unsigned len)
822 {
823         struct virtio_ccw_device *vcdev = to_vc_device(vdev);
824         int ret;
825         struct ccw1 *ccw;
826         void *config_area;
827
828         ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
829         if (!ccw)
830                 return;
831
832         config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
833         if (!config_area)
834                 goto out_free;
835
836         /* Read the config area from the host. */
837         ccw->cmd_code = CCW_CMD_READ_CONF;
838         ccw->flags = 0;
839         ccw->count = offset + len;
840         ccw->cda = (__u32)(unsigned long)config_area;
841         ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG);
842         if (ret)
843                 goto out_free;
844
845         memcpy(vcdev->config, config_area, offset + len);
846         if (buf)
847                 memcpy(buf, &vcdev->config[offset], len);
848         if (vcdev->config_ready < offset + len)
849                 vcdev->config_ready = offset + len;
850
851 out_free:
852         kfree(config_area);
853         kfree(ccw);
854 }
855
856 static void virtio_ccw_set_config(struct virtio_device *vdev,
857                                   unsigned int offset, const void *buf,
858                                   unsigned len)
859 {
860         struct virtio_ccw_device *vcdev = to_vc_device(vdev);
861         struct ccw1 *ccw;
862         void *config_area;
863
864         ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
865         if (!ccw)
866                 return;
867
868         config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
869         if (!config_area)
870                 goto out_free;
871
872         /* Make sure we don't overwrite fields. */
873         if (vcdev->config_ready < offset)
874                 virtio_ccw_get_config(vdev, 0, NULL, offset);
875         memcpy(&vcdev->config[offset], buf, len);
876         /* Write the config area to the host. */
877         memcpy(config_area, vcdev->config, sizeof(vcdev->config));
878         ccw->cmd_code = CCW_CMD_WRITE_CONF;
879         ccw->flags = 0;
880         ccw->count = offset + len;
881         ccw->cda = (__u32)(unsigned long)config_area;
882         ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG);
883
884 out_free:
885         kfree(config_area);
886         kfree(ccw);
887 }
888
889 static u8 virtio_ccw_get_status(struct virtio_device *vdev)
890 {
891         struct virtio_ccw_device *vcdev = to_vc_device(vdev);
892
893         return *vcdev->status;
894 }
895
896 static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status)
897 {
898         struct virtio_ccw_device *vcdev = to_vc_device(vdev);
899         u8 old_status = *vcdev->status;
900         struct ccw1 *ccw;
901         int ret;
902
903         ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
904         if (!ccw)
905                 return;
906
907         /* Write the status to the host. */
908         *vcdev->status = status;
909         ccw->cmd_code = CCW_CMD_WRITE_STATUS;
910         ccw->flags = 0;
911         ccw->count = sizeof(status);
912         ccw->cda = (__u32)(unsigned long)vcdev->status;
913         ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS);
914         /* Write failed? We assume status is unchanged. */
915         if (ret)
916                 *vcdev->status = old_status;
917         kfree(ccw);
918 }
919
920 static struct virtio_config_ops virtio_ccw_config_ops = {
921         .get_features = virtio_ccw_get_features,
922         .finalize_features = virtio_ccw_finalize_features,
923         .get = virtio_ccw_get_config,
924         .set = virtio_ccw_set_config,
925         .get_status = virtio_ccw_get_status,
926         .set_status = virtio_ccw_set_status,
927         .reset = virtio_ccw_reset,
928         .find_vqs = virtio_ccw_find_vqs,
929         .del_vqs = virtio_ccw_del_vqs,
930 };
931
932
933 /*
934  * ccw bus driver related functions
935  */
936
937 static void virtio_ccw_release_dev(struct device *_d)
938 {
939         struct virtio_device *dev = container_of(_d, struct virtio_device,
940                                                  dev);
941         struct virtio_ccw_device *vcdev = to_vc_device(dev);
942
943         kfree(vcdev->status);
944         kfree(vcdev->config_block);
945         kfree(vcdev);
946 }
947
948 static int irb_is_error(struct irb *irb)
949 {
950         if (scsw_cstat(&irb->scsw) != 0)
951                 return 1;
952         if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
953                 return 1;
954         if (scsw_cc(&irb->scsw) != 0)
955                 return 1;
956         return 0;
957 }
958
959 static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev,
960                                               int index)
961 {
962         struct virtio_ccw_vq_info *info;
963         unsigned long flags;
964         struct virtqueue *vq;
965
966         vq = NULL;
967         spin_lock_irqsave(&vcdev->lock, flags);
968         list_for_each_entry(info, &vcdev->virtqueues, node) {
969                 if (info->vq->index == index) {
970                         vq = info->vq;
971                         break;
972                 }
973         }
974         spin_unlock_irqrestore(&vcdev->lock, flags);
975         return vq;
976 }
977
978 static void virtio_ccw_int_handler(struct ccw_device *cdev,
979                                    unsigned long intparm,
980                                    struct irb *irb)
981 {
982         __u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK;
983         struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
984         int i;
985         struct virtqueue *vq;
986
987         if (!vcdev)
988                 return;
989         /* Check if it's a notification from the host. */
990         if ((intparm == 0) &&
991             (scsw_stctl(&irb->scsw) ==
992              (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) {
993                 /* OK */
994         }
995         if (irb_is_error(irb)) {
996                 /* Command reject? */
997                 if ((scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) &&
998                     (irb->ecw[0] & SNS0_CMD_REJECT))
999                         vcdev->err = -EOPNOTSUPP;
1000                 else
1001                         /* Map everything else to -EIO. */
1002                         vcdev->err = -EIO;
1003         }
1004         if (vcdev->curr_io & activity) {
1005                 switch (activity) {
1006                 case VIRTIO_CCW_DOING_READ_FEAT:
1007                 case VIRTIO_CCW_DOING_WRITE_FEAT:
1008                 case VIRTIO_CCW_DOING_READ_CONFIG:
1009                 case VIRTIO_CCW_DOING_WRITE_CONFIG:
1010                 case VIRTIO_CCW_DOING_WRITE_STATUS:
1011                 case VIRTIO_CCW_DOING_SET_VQ:
1012                 case VIRTIO_CCW_DOING_SET_IND:
1013                 case VIRTIO_CCW_DOING_SET_CONF_IND:
1014                 case VIRTIO_CCW_DOING_RESET:
1015                 case VIRTIO_CCW_DOING_READ_VQ_CONF:
1016                 case VIRTIO_CCW_DOING_SET_IND_ADAPTER:
1017                 case VIRTIO_CCW_DOING_SET_VIRTIO_REV:
1018                         vcdev->curr_io &= ~activity;
1019                         wake_up(&vcdev->wait_q);
1020                         break;
1021                 default:
1022                         /* don't know what to do... */
1023                         dev_warn(&cdev->dev, "Suspicious activity '%08x'\n",
1024                                  activity);
1025                         WARN_ON(1);
1026                         break;
1027                 }
1028         }
1029         for_each_set_bit(i, &vcdev->indicators,
1030                          sizeof(vcdev->indicators) * BITS_PER_BYTE) {
1031                 /* The bit clear must happen before the vring kick. */
1032                 clear_bit(i, &vcdev->indicators);
1033                 barrier();
1034                 vq = virtio_ccw_vq_by_ind(vcdev, i);
1035                 vring_interrupt(0, vq);
1036         }
1037         if (test_bit(0, &vcdev->indicators2)) {
1038                 virtio_config_changed(&vcdev->vdev);
1039                 clear_bit(0, &vcdev->indicators2);
1040         }
1041 }
1042
1043 /*
1044  * We usually want to autoonline all devices, but give the admin
1045  * a way to exempt devices from this.
1046  */
1047 #define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
1048                      (8*sizeof(long)))
1049 static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS];
1050
1051 static char *no_auto = "";
1052
1053 module_param(no_auto, charp, 0444);
1054 MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined");
1055
1056 static int virtio_ccw_check_autoonline(struct ccw_device *cdev)
1057 {
1058         struct ccw_dev_id id;
1059
1060         ccw_device_get_id(cdev, &id);
1061         if (test_bit(id.devno, devs_no_auto[id.ssid]))
1062                 return 0;
1063         return 1;
1064 }
1065
1066 static void virtio_ccw_auto_online(void *data, async_cookie_t cookie)
1067 {
1068         struct ccw_device *cdev = data;
1069         int ret;
1070
1071         ret = ccw_device_set_online(cdev);
1072         if (ret)
1073                 dev_warn(&cdev->dev, "Failed to set online: %d\n", ret);
1074 }
1075
1076 static int virtio_ccw_probe(struct ccw_device *cdev)
1077 {
1078         cdev->handler = virtio_ccw_int_handler;
1079
1080         if (virtio_ccw_check_autoonline(cdev))
1081                 async_schedule(virtio_ccw_auto_online, cdev);
1082         return 0;
1083 }
1084
1085 static struct virtio_ccw_device *virtio_grab_drvdata(struct ccw_device *cdev)
1086 {
1087         unsigned long flags;
1088         struct virtio_ccw_device *vcdev;
1089
1090         spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1091         vcdev = dev_get_drvdata(&cdev->dev);
1092         if (!vcdev || vcdev->going_away) {
1093                 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1094                 return NULL;
1095         }
1096         vcdev->going_away = true;
1097         spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1098         return vcdev;
1099 }
1100
1101 static void virtio_ccw_remove(struct ccw_device *cdev)
1102 {
1103         unsigned long flags;
1104         struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
1105
1106         if (vcdev && cdev->online) {
1107                 if (vcdev->device_lost)
1108                         virtio_break_device(&vcdev->vdev);
1109                 unregister_virtio_device(&vcdev->vdev);
1110                 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1111                 dev_set_drvdata(&cdev->dev, NULL);
1112                 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1113         }
1114         cdev->handler = NULL;
1115 }
1116
1117 static int virtio_ccw_offline(struct ccw_device *cdev)
1118 {
1119         unsigned long flags;
1120         struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
1121
1122         if (!vcdev)
1123                 return 0;
1124         if (vcdev->device_lost)
1125                 virtio_break_device(&vcdev->vdev);
1126         unregister_virtio_device(&vcdev->vdev);
1127         spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1128         dev_set_drvdata(&cdev->dev, NULL);
1129         spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1130         return 0;
1131 }
1132
1133 static int virtio_ccw_set_transport_rev(struct virtio_ccw_device *vcdev)
1134 {
1135         struct virtio_rev_info *rev;
1136         struct ccw1 *ccw;
1137         int ret;
1138
1139         ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
1140         if (!ccw)
1141                 return -ENOMEM;
1142         rev = kzalloc(sizeof(*rev), GFP_DMA | GFP_KERNEL);
1143         if (!rev) {
1144                 kfree(ccw);
1145                 return -ENOMEM;
1146         }
1147
1148         /* Set transport revision */
1149         ccw->cmd_code = CCW_CMD_SET_VIRTIO_REV;
1150         ccw->flags = 0;
1151         ccw->count = sizeof(*rev);
1152         ccw->cda = (__u32)(unsigned long)rev;
1153
1154         vcdev->revision = VIRTIO_CCW_REV_MAX;
1155         do {
1156                 rev->revision = vcdev->revision;
1157                 /* none of our supported revisions carry payload */
1158                 rev->length = 0;
1159                 ret = ccw_io_helper(vcdev, ccw,
1160                                     VIRTIO_CCW_DOING_SET_VIRTIO_REV);
1161                 if (ret == -EOPNOTSUPP) {
1162                         if (vcdev->revision == 0)
1163                                 /*
1164                                  * The host device does not support setting
1165                                  * the revision: let's operate it in legacy
1166                                  * mode.
1167                                  */
1168                                 ret = 0;
1169                         else
1170                                 vcdev->revision--;
1171                 }
1172         } while (ret == -EOPNOTSUPP);
1173
1174         kfree(ccw);
1175         kfree(rev);
1176         return ret;
1177 }
1178
1179 static int virtio_ccw_online(struct ccw_device *cdev)
1180 {
1181         int ret;
1182         struct virtio_ccw_device *vcdev;
1183         unsigned long flags;
1184
1185         vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL);
1186         if (!vcdev) {
1187                 dev_warn(&cdev->dev, "Could not get memory for virtio\n");
1188                 ret = -ENOMEM;
1189                 goto out_free;
1190         }
1191         vcdev->config_block = kzalloc(sizeof(*vcdev->config_block),
1192                                    GFP_DMA | GFP_KERNEL);
1193         if (!vcdev->config_block) {
1194                 ret = -ENOMEM;
1195                 goto out_free;
1196         }
1197         vcdev->status = kzalloc(sizeof(*vcdev->status), GFP_DMA | GFP_KERNEL);
1198         if (!vcdev->status) {
1199                 ret = -ENOMEM;
1200                 goto out_free;
1201         }
1202
1203         vcdev->is_thinint = virtio_ccw_use_airq; /* at least try */
1204
1205         vcdev->vdev.dev.parent = &cdev->dev;
1206         vcdev->vdev.dev.release = virtio_ccw_release_dev;
1207         vcdev->vdev.config = &virtio_ccw_config_ops;
1208         vcdev->cdev = cdev;
1209         init_waitqueue_head(&vcdev->wait_q);
1210         INIT_LIST_HEAD(&vcdev->virtqueues);
1211         spin_lock_init(&vcdev->lock);
1212
1213         spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1214         dev_set_drvdata(&cdev->dev, vcdev);
1215         spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1216         vcdev->vdev.id.vendor = cdev->id.cu_type;
1217         vcdev->vdev.id.device = cdev->id.cu_model;
1218
1219         ret = virtio_ccw_set_transport_rev(vcdev);
1220         if (ret)
1221                 goto out_free;
1222
1223         ret = register_virtio_device(&vcdev->vdev);
1224         if (ret) {
1225                 dev_warn(&cdev->dev, "Failed to register virtio device: %d\n",
1226                          ret);
1227                 goto out_put;
1228         }
1229         return 0;
1230 out_put:
1231         spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1232         dev_set_drvdata(&cdev->dev, NULL);
1233         spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1234         put_device(&vcdev->vdev.dev);
1235         return ret;
1236 out_free:
1237         if (vcdev) {
1238                 kfree(vcdev->status);
1239                 kfree(vcdev->config_block);
1240         }
1241         kfree(vcdev);
1242         return ret;
1243 }
1244
1245 static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event)
1246 {
1247         int rc;
1248         struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1249
1250         /*
1251          * Make sure vcdev is set
1252          * i.e. set_offline/remove callback not already running
1253          */
1254         if (!vcdev)
1255                 return NOTIFY_DONE;
1256
1257         switch (event) {
1258         case CIO_GONE:
1259                 vcdev->device_lost = true;
1260                 rc = NOTIFY_DONE;
1261                 break;
1262         default:
1263                 rc = NOTIFY_DONE;
1264                 break;
1265         }
1266         return rc;
1267 }
1268
1269 static struct ccw_device_id virtio_ids[] = {
1270         { CCW_DEVICE(0x3832, 0) },
1271         {},
1272 };
1273 MODULE_DEVICE_TABLE(ccw, virtio_ids);
1274
1275 static struct ccw_driver virtio_ccw_driver = {
1276         .driver = {
1277                 .owner = THIS_MODULE,
1278                 .name = "virtio_ccw",
1279         },
1280         .ids = virtio_ids,
1281         .probe = virtio_ccw_probe,
1282         .remove = virtio_ccw_remove,
1283         .set_offline = virtio_ccw_offline,
1284         .set_online = virtio_ccw_online,
1285         .notify = virtio_ccw_cio_notify,
1286         .int_class = IRQIO_VIR,
1287 };
1288
1289 static int __init pure_hex(char **cp, unsigned int *val, int min_digit,
1290                            int max_digit, int max_val)
1291 {
1292         int diff;
1293
1294         diff = 0;
1295         *val = 0;
1296
1297         while (diff <= max_digit) {
1298                 int value = hex_to_bin(**cp);
1299
1300                 if (value < 0)
1301                         break;
1302                 *val = *val * 16 + value;
1303                 (*cp)++;
1304                 diff++;
1305         }
1306
1307         if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
1308                 return 1;
1309
1310         return 0;
1311 }
1312
1313 static int __init parse_busid(char *str, unsigned int *cssid,
1314                               unsigned int *ssid, unsigned int *devno)
1315 {
1316         char *str_work;
1317         int rc, ret;
1318
1319         rc = 1;
1320
1321         if (*str == '\0')
1322                 goto out;
1323
1324         str_work = str;
1325         ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
1326         if (ret || (str_work[0] != '.'))
1327                 goto out;
1328         str_work++;
1329         ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
1330         if (ret || (str_work[0] != '.'))
1331                 goto out;
1332         str_work++;
1333         ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
1334         if (ret || (str_work[0] != '\0'))
1335                 goto out;
1336
1337         rc = 0;
1338 out:
1339         return rc;
1340 }
1341
1342 static void __init no_auto_parse(void)
1343 {
1344         unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
1345         char *parm, *str;
1346         int rc;
1347
1348         str = no_auto;
1349         while ((parm = strsep(&str, ","))) {
1350                 rc = parse_busid(strsep(&parm, "-"), &from_cssid,
1351                                  &from_ssid, &from);
1352                 if (rc)
1353                         continue;
1354                 if (parm != NULL) {
1355                         rc = parse_busid(parm, &to_cssid,
1356                                          &to_ssid, &to);
1357                         if ((from_ssid > to_ssid) ||
1358                             ((from_ssid == to_ssid) && (from > to)))
1359                                 rc = -EINVAL;
1360                 } else {
1361                         to_cssid = from_cssid;
1362                         to_ssid = from_ssid;
1363                         to = from;
1364                 }
1365                 if (rc)
1366                         continue;
1367                 while ((from_ssid < to_ssid) ||
1368                        ((from_ssid == to_ssid) && (from <= to))) {
1369                         set_bit(from, devs_no_auto[from_ssid]);
1370                         from++;
1371                         if (from > __MAX_SUBCHANNEL) {
1372                                 from_ssid++;
1373                                 from = 0;
1374                         }
1375                 }
1376         }
1377 }
1378
1379 static int __init virtio_ccw_init(void)
1380 {
1381         /* parse no_auto string before we do anything further */
1382         no_auto_parse();
1383         return ccw_driver_register(&virtio_ccw_driver);
1384 }
1385 module_init(virtio_ccw_init);
1386
1387 static void __exit virtio_ccw_exit(void)
1388 {
1389         int i;
1390
1391         ccw_driver_unregister(&virtio_ccw_driver);
1392         for (i = 0; i < MAX_AIRQ_AREAS; i++)
1393                 destroy_airq_info(airq_areas[i]);
1394 }
1395 module_exit(virtio_ccw_exit);