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