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