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