2 * ccw based virtio transport
4 * Copyright IBM Corp. 2012, 2014
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.
10 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
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>
29 #include <linux/kvm_para.h>
30 #include <asm/setup.h>
33 #include <asm/ccwdev.h>
34 #include <asm/virtio-ccw.h>
39 * virtio related functions
42 struct vq_config_block {
47 #define VIRTIO_CCW_CONFIG_SIZE 0x100
48 /* same as PCI config space size, should be enough for all drivers */
50 struct virtio_ccw_device {
51 struct virtio_device vdev;
53 __u8 config[VIRTIO_CCW_CONFIG_SIZE];
54 struct ccw_device *cdev;
57 wait_queue_head_t wait_q;
59 struct list_head virtqueues;
60 unsigned long indicators;
61 unsigned long indicators2;
62 struct vq_config_block *config_block;
68 struct vq_info_block {
75 struct virtio_feature_desc {
80 struct virtio_thinint_area {
81 unsigned long summary_indicator;
82 unsigned long indicator;
87 struct virtio_ccw_vq_info {
91 struct vq_info_block *info_block;
93 struct list_head node;
97 #define VIRTIO_AIRQ_ISC IO_SCH_ISC /* inherit from subchannel */
99 #define VIRTIO_IV_BITS (L1_CACHE_BYTES * 8)
100 #define MAX_AIRQ_AREAS 20
102 static int virtio_ccw_use_airq = 1;
106 u8 summary_indicator;
107 struct airq_struct airq;
110 static struct airq_info *airq_areas[MAX_AIRQ_AREAS];
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
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
137 static struct virtio_ccw_device *to_vc_device(struct virtio_device *vdev)
139 return container_of(vdev, struct virtio_ccw_device, vdev);
142 static void drop_airq_indicator(struct virtqueue *vq, struct airq_info *info)
144 unsigned long i, flags;
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);
154 write_unlock_irqrestore(&info->lock, flags);
157 static void virtio_airq_handler(struct airq_struct *airq)
159 struct airq_info *info = container_of(airq, struct airq_info, airq);
162 inc_irq_stat(IRQIO_VAI);
163 read_lock(&info->lock);
164 /* Walk through indicators field, summary indicator active. */
166 ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
169 vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
171 info->summary_indicator = 0;
173 /* Walk through indicators field, summary indicator not active. */
175 ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
178 vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
180 read_unlock(&info->lock);
183 static struct airq_info *new_airq_info(void)
185 struct airq_info *info;
188 info = kzalloc(sizeof(*info), GFP_KERNEL);
191 rwlock_init(&info->lock);
192 info->aiv = airq_iv_create(VIRTIO_IV_BITS, AIRQ_IV_ALLOC | AIRQ_IV_PTR);
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);
203 airq_iv_release(info->aiv);
210 static void destroy_airq_info(struct airq_info *info)
215 unregister_adapter_interrupt(&info->airq);
216 airq_iv_release(info->aiv);
220 static unsigned long get_airq_indicator(struct virtqueue *vqs[], int nvqs,
221 u64 *first, void **airq_info)
224 struct airq_info *info;
225 unsigned long indicator_addr = 0;
226 unsigned long bit, flags;
228 for (i = 0; i < MAX_AIRQ_AREAS && !indicator_addr; i++) {
230 airq_areas[i] = new_airq_info();
231 info = airq_areas[i];
234 write_lock_irqsave(&info->lock, flags);
235 bit = airq_iv_alloc(info->aiv, nvqs);
237 /* Not enough vacancies. */
238 write_unlock_irqrestore(&info->lock, flags);
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]);
248 write_unlock_irqrestore(&info->lock, flags);
250 return indicator_addr;
253 static void virtio_ccw_drop_indicators(struct virtio_ccw_device *vcdev)
255 struct virtio_ccw_vq_info *info;
257 list_for_each_entry(info, &vcdev->virtqueues, node)
258 drop_airq_indicator(info->vq, vcdev->airq_info);
261 static int doing_io(struct virtio_ccw_device *vcdev, __u32 flag)
266 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
270 ret = vcdev->curr_io & flag;
271 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
275 static int ccw_io_helper(struct virtio_ccw_device *vcdev,
276 struct ccw1 *ccw, __u32 intparm)
280 int flag = intparm & VIRTIO_CCW_INTPARM_MASK;
283 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
284 ret = ccw_device_start(vcdev->cdev, ccw, intparm, 0, 0);
288 vcdev->curr_io |= flag;
290 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
292 } while (ret == -EBUSY);
293 wait_event(vcdev->wait_q, doing_io(vcdev, flag) == 0);
294 return ret ? ret : vcdev->err;
297 static void virtio_ccw_drop_indicator(struct virtio_ccw_device *vcdev,
301 unsigned long *indicatorp = NULL;
302 struct virtio_thinint_area *thinint_area = NULL;
303 struct airq_info *airq_info = vcdev->airq_info;
305 if (vcdev->is_thinint) {
306 thinint_area = kzalloc(sizeof(*thinint_area),
307 GFP_DMA | GFP_KERNEL);
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;
317 indicatorp = kmalloc(sizeof(&vcdev->indicators),
318 GFP_DMA | GFP_KERNEL);
322 ccw->cmd_code = CCW_CMD_SET_IND;
323 ccw->count = sizeof(vcdev->indicators);
324 ccw->cda = (__u32)(unsigned long) indicatorp;
326 /* Deregister indicators from host. */
327 vcdev->indicators = 0;
329 ret = ccw_io_helper(vcdev, ccw,
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);
342 static inline long do_kvm_notify(struct subchannel_id schid,
343 unsigned long queue_index,
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;
352 asm volatile ("diag 2,4,0x500\n"
353 : "=d" (__rc) : "d" (__nr), "d" (__schid), "d" (__index),
359 static bool virtio_ccw_kvm_notify(struct virtqueue *vq)
361 struct virtio_ccw_vq_info *info = vq->priv;
362 struct virtio_ccw_device *vcdev;
363 struct subchannel_id schid;
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)
373 static int virtio_ccw_read_vq_conf(struct virtio_ccw_device *vcdev,
374 struct ccw1 *ccw, int index)
376 vcdev->config_block->index = index;
377 ccw->cmd_code = CCW_CMD_READ_VQ_CONF;
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;
385 static void virtio_ccw_del_vq(struct virtqueue *vq, struct ccw1 *ccw)
387 struct virtio_ccw_device *vcdev = to_vc_device(vq->vdev);
388 struct virtio_ccw_vq_info *info = vq->priv;
392 unsigned int index = vq->index;
394 /* Remove from our list. */
395 spin_lock_irqsave(&vcdev->lock, flags);
396 list_del(&info->node);
397 spin_unlock_irqrestore(&vcdev->lock, flags);
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;
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);
411 * -ENODEV isn't considered an error: The device is gone anyway.
412 * This may happen on device detach.
414 if (ret && (ret != -ENODEV))
415 dev_warn(&vq->vdev->dev, "Error %d while deleting queue %d",
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);
425 static void virtio_ccw_del_vqs(struct virtio_device *vdev)
427 struct virtqueue *vq, *n;
429 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
431 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
435 virtio_ccw_drop_indicator(vcdev, ccw);
437 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
438 virtio_ccw_del_vq(vq, ccw);
443 static struct virtqueue *virtio_ccw_setup_vq(struct virtio_device *vdev,
444 int i, vq_callback_t *callback,
448 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
450 struct virtqueue *vq = NULL;
451 struct virtio_ccw_vq_info *info;
452 unsigned long size = 0; /* silence the compiler */
455 /* Allocate queue. */
456 info = kzalloc(sizeof(struct virtio_ccw_vq_info), GFP_KERNEL);
458 dev_warn(&vcdev->cdev->dev, "no info\n");
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");
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");
478 vq = vring_new_virtqueue(i, info->num, KVM_VIRTIO_CCW_RING_ALIGN, vdev,
479 true, info->queue, virtio_ccw_kvm_notify,
482 /* For now, we fail if we can't get the requested size. */
483 dev_warn(&vcdev->cdev->dev, "no vq\n");
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;
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);
499 dev_warn(&vcdev->cdev->dev, "SET_VQ failed\n");
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);
515 vring_del_virtqueue(vq);
518 free_pages_exact(info->queue, size);
519 kfree(info->info_block);
525 static int virtio_ccw_register_adapter_ind(struct virtio_ccw_device *vcdev,
526 struct virtqueue *vqs[], int nvqs,
530 struct virtio_thinint_area *thinint_area = NULL;
531 struct airq_info *info;
533 thinint_area = kzalloc(sizeof(*thinint_area), GFP_DMA | GFP_KERNEL);
538 /* Try to get an indicator. */
539 thinint_area->indicator = get_airq_indicator(vqs, nvqs,
540 &thinint_area->bit_nr,
542 if (!thinint_area->indicator) {
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);
556 if (ret == -EOPNOTSUPP) {
558 * The host does not support adapter interrupts
559 * for virtio-ccw, stop trying.
561 virtio_ccw_use_airq = 0;
562 pr_info("Adapter interrupts unsupported on host\n");
564 dev_warn(&vcdev->cdev->dev,
565 "enabling adapter interrupts = %d\n", ret);
566 virtio_ccw_drop_indicators(vcdev);
573 static int virtio_ccw_find_vqs(struct virtio_device *vdev, unsigned nvqs,
574 struct virtqueue *vqs[],
575 vq_callback_t *callbacks[],
578 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
579 unsigned long *indicatorp = NULL;
583 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
587 for (i = 0; i < nvqs; ++i) {
588 vqs[i] = virtio_ccw_setup_vq(vdev, i, callbacks[i], names[i],
590 if (IS_ERR(vqs[i])) {
591 ret = PTR_ERR(vqs[i]);
597 /* We need a data area under 2G to communicate. */
598 indicatorp = kmalloc(sizeof(&vcdev->indicators), GFP_DMA | GFP_KERNEL);
601 *indicatorp = (unsigned long) &vcdev->indicators;
602 if (vcdev->is_thinint) {
603 ret = virtio_ccw_register_adapter_ind(vcdev, vqs, nvqs, ccw);
605 /* no error, just fall back to legacy interrupts */
606 vcdev->is_thinint = 0;
608 if (!vcdev->is_thinint) {
609 /* Register queue indicators with host. */
610 vcdev->indicators = 0;
611 ccw->cmd_code = CCW_CMD_SET_IND;
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);
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;
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);
636 virtio_ccw_del_vqs(vdev);
640 static void virtio_ccw_reset(struct virtio_device *vdev)
642 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
645 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
649 /* Zero status bits. */
652 /* Send a reset ccw on device. */
653 ccw->cmd_code = CCW_CMD_VDEV_RESET;
657 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_RESET);
661 static u32 virtio_ccw_get_features(struct virtio_device *vdev)
663 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
664 struct virtio_feature_desc *features;
668 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
672 features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
677 /* Read the feature bits from the host. */
678 /* TODO: Features > 32 bits */
680 ccw->cmd_code = CCW_CMD_READ_FEAT;
682 ccw->count = sizeof(*features);
683 ccw->cda = (__u32)(unsigned long)features;
684 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
690 rc = le32_to_cpu(features->features);
698 static void virtio_ccw_finalize_features(struct virtio_device *vdev)
700 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
701 struct virtio_feature_desc *features;
705 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
709 features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
713 /* Give virtio_ring a chance to accept features. */
714 vring_transport_features(vdev);
716 for (i = 0; i < sizeof(*vdev->features) / sizeof(features->features);
718 int highbits = i % 2 ? 32 : 0;
720 features->features = cpu_to_le32(vdev->features[i / 2]
722 /* Write the feature bits to the host. */
723 ccw->cmd_code = CCW_CMD_WRITE_FEAT;
725 ccw->count = sizeof(*features);
726 ccw->cda = (__u32)(unsigned long)features;
727 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
734 static void virtio_ccw_get_config(struct virtio_device *vdev,
735 unsigned int offset, void *buf, unsigned len)
737 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
742 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
746 config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
750 /* Read the config area from the host. */
751 ccw->cmd_code = CCW_CMD_READ_CONF;
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);
759 memcpy(vcdev->config, config_area, sizeof(vcdev->config));
760 memcpy(buf, &vcdev->config[offset], len);
767 static void virtio_ccw_set_config(struct virtio_device *vdev,
768 unsigned int offset, const void *buf,
771 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
775 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
779 config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
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;
788 ccw->count = offset + len;
789 ccw->cda = (__u32)(unsigned long)config_area;
790 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG);
797 static u8 virtio_ccw_get_status(struct virtio_device *vdev)
799 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
801 return *vcdev->status;
804 static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status)
806 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
809 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
813 /* Write the status to the host. */
814 *vcdev->status = status;
815 ccw->cmd_code = CCW_CMD_WRITE_STATUS;
817 ccw->count = sizeof(status);
818 ccw->cda = (__u32)(unsigned long)vcdev->status;
819 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS);
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,
837 * ccw bus driver related functions
840 static void virtio_ccw_release_dev(struct device *_d)
842 struct virtio_device *dev = container_of(_d, struct virtio_device,
844 struct virtio_ccw_device *vcdev = to_vc_device(dev);
846 kfree(vcdev->status);
847 kfree(vcdev->config_block);
851 static int irb_is_error(struct irb *irb)
853 if (scsw_cstat(&irb->scsw) != 0)
855 if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
857 if (scsw_cc(&irb->scsw) != 0)
862 static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev,
865 struct virtio_ccw_vq_info *info;
867 struct virtqueue *vq;
870 spin_lock_irqsave(&vcdev->lock, flags);
871 list_for_each_entry(info, &vcdev->virtqueues, node) {
872 if (info->vq->index == index) {
877 spin_unlock_irqrestore(&vcdev->lock, flags);
881 static void virtio_ccw_int_handler(struct ccw_device *cdev,
882 unsigned long intparm,
885 __u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK;
886 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
888 struct virtqueue *vq;
889 struct virtio_driver *drv;
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))) {
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;
905 /* Map everything else to -EIO. */
908 if (vcdev->curr_io & 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);
925 /* don't know what to do... */
926 dev_warn(&cdev->dev, "Suspicious activity '%08x'\n",
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);
937 vq = virtio_ccw_vq_by_ind(vcdev, i);
938 vring_interrupt(0, vq);
940 if (test_bit(0, &vcdev->indicators2)) {
941 drv = container_of(vcdev->vdev.dev.driver,
942 struct virtio_driver, driver);
944 if (drv && drv->config_changed)
945 drv->config_changed(&vcdev->vdev);
946 clear_bit(0, &vcdev->indicators2);
951 * We usually want to autoonline all devices, but give the admin
952 * a way to exempt devices from this.
954 #define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
956 static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS];
958 static char *no_auto = "";
960 module_param(no_auto, charp, 0444);
961 MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined");
963 static int virtio_ccw_check_autoonline(struct ccw_device *cdev)
965 struct ccw_dev_id id;
967 ccw_device_get_id(cdev, &id);
968 if (test_bit(id.devno, devs_no_auto[id.ssid]))
973 static void virtio_ccw_auto_online(void *data, async_cookie_t cookie)
975 struct ccw_device *cdev = data;
978 ret = ccw_device_set_online(cdev);
980 dev_warn(&cdev->dev, "Failed to set online: %d\n", ret);
983 static int virtio_ccw_probe(struct ccw_device *cdev)
985 cdev->handler = virtio_ccw_int_handler;
987 if (virtio_ccw_check_autoonline(cdev))
988 async_schedule(virtio_ccw_auto_online, cdev);
992 static struct virtio_ccw_device *virtio_grab_drvdata(struct ccw_device *cdev)
995 struct virtio_ccw_device *vcdev;
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);
1003 vcdev->going_away = true;
1004 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1008 static void virtio_ccw_remove(struct ccw_device *cdev)
1010 unsigned long flags;
1011 struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
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;
1021 static int virtio_ccw_offline(struct ccw_device *cdev)
1023 unsigned long flags;
1024 struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
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);
1036 static int virtio_ccw_online(struct ccw_device *cdev)
1039 struct virtio_ccw_device *vcdev;
1040 unsigned long flags;
1042 vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL);
1044 dev_warn(&cdev->dev, "Could not get memory for virtio\n");
1048 vcdev->config_block = kzalloc(sizeof(*vcdev->config_block),
1049 GFP_DMA | GFP_KERNEL);
1050 if (!vcdev->config_block) {
1054 vcdev->status = kzalloc(sizeof(*vcdev->status), GFP_DMA | GFP_KERNEL);
1055 if (!vcdev->status) {
1060 vcdev->is_thinint = virtio_ccw_use_airq; /* at least try */
1062 vcdev->vdev.dev.parent = &cdev->dev;
1063 vcdev->vdev.dev.release = virtio_ccw_release_dev;
1064 vcdev->vdev.config = &virtio_ccw_config_ops;
1066 init_waitqueue_head(&vcdev->wait_q);
1067 INIT_LIST_HEAD(&vcdev->virtqueues);
1068 spin_lock_init(&vcdev->lock);
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);
1077 dev_warn(&cdev->dev, "Failed to register virtio device: %d\n",
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);
1090 kfree(vcdev->status);
1091 kfree(vcdev->config_block);
1097 static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event)
1099 /* TODO: Check whether we need special handling here. */
1103 static struct ccw_device_id virtio_ids[] = {
1104 { CCW_DEVICE(0x3832, 0) },
1107 MODULE_DEVICE_TABLE(ccw, virtio_ids);
1109 static struct ccw_driver virtio_ccw_driver = {
1111 .owner = THIS_MODULE,
1112 .name = "virtio_ccw",
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,
1123 static int __init pure_hex(char **cp, unsigned int *val, int min_digit,
1124 int max_digit, int max_val)
1131 while (diff <= max_digit) {
1132 int value = hex_to_bin(**cp);
1136 *val = *val * 16 + value;
1141 if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
1147 static int __init parse_busid(char *str, unsigned int *cssid,
1148 unsigned int *ssid, unsigned int *devno)
1159 ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
1160 if (ret || (str_work[0] != '.'))
1163 ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
1164 if (ret || (str_work[0] != '.'))
1167 ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
1168 if (ret || (str_work[0] != '\0'))
1176 static void __init no_auto_parse(void)
1178 unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
1183 while ((parm = strsep(&str, ","))) {
1184 rc = parse_busid(strsep(&parm, "-"), &from_cssid,
1189 rc = parse_busid(parm, &to_cssid,
1191 if ((from_ssid > to_ssid) ||
1192 ((from_ssid == to_ssid) && (from > to)))
1195 to_cssid = from_cssid;
1196 to_ssid = from_ssid;
1201 while ((from_ssid < to_ssid) ||
1202 ((from_ssid == to_ssid) && (from <= to))) {
1203 set_bit(from, devs_no_auto[from_ssid]);
1205 if (from > __MAX_SUBCHANNEL) {
1213 static int __init virtio_ccw_init(void)
1215 /* parse no_auto string before we do anything further */
1217 return ccw_driver_register(&virtio_ccw_driver);
1219 module_init(virtio_ccw_init);
1221 static void __exit virtio_ccw_exit(void)
1225 ccw_driver_unregister(&virtio_ccw_driver);
1226 for (i = 0; i < MAX_AIRQ_AREAS; i++)
1227 destroy_airq_info(airq_areas[i]);
1229 module_exit(virtio_ccw_exit);