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