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 <linux/notifier.h>
32 #include <asm/setup.h>
35 #include <asm/ccwdev.h>
36 #include <asm/virtio-ccw.h>
41 * virtio related functions
44 struct vq_config_block {
49 #define VIRTIO_CCW_CONFIG_SIZE 0x100
50 /* same as PCI config space size, should be enough for all drivers */
52 struct virtio_ccw_device {
53 struct virtio_device vdev;
55 __u8 config[VIRTIO_CCW_CONFIG_SIZE];
56 struct ccw_device *cdev;
59 unsigned int revision; /* Transport revision */
60 wait_queue_head_t wait_q;
62 struct list_head virtqueues;
63 unsigned long indicators;
64 unsigned long indicators2;
65 struct vq_config_block *config_block;
69 unsigned int config_ready;
73 struct vq_info_block_legacy {
80 struct vq_info_block {
89 struct virtio_feature_desc {
94 struct virtio_thinint_area {
95 unsigned long summary_indicator;
96 unsigned long indicator;
101 struct virtio_rev_info {
107 /* the highest virtio-ccw revision we support */
108 #define VIRTIO_CCW_REV_MAX 1
110 struct virtio_ccw_vq_info {
111 struct virtqueue *vq;
115 struct vq_info_block s;
116 struct vq_info_block_legacy l;
119 struct list_head node;
123 #define VIRTIO_AIRQ_ISC IO_SCH_ISC /* inherit from subchannel */
125 #define VIRTIO_IV_BITS (L1_CACHE_BYTES * 8)
126 #define MAX_AIRQ_AREAS 20
128 static int virtio_ccw_use_airq = 1;
132 u8 summary_indicator;
133 struct airq_struct airq;
136 static struct airq_info *airq_areas[MAX_AIRQ_AREAS];
138 #define CCW_CMD_SET_VQ 0x13
139 #define CCW_CMD_VDEV_RESET 0x33
140 #define CCW_CMD_SET_IND 0x43
141 #define CCW_CMD_SET_CONF_IND 0x53
142 #define CCW_CMD_READ_FEAT 0x12
143 #define CCW_CMD_WRITE_FEAT 0x11
144 #define CCW_CMD_READ_CONF 0x22
145 #define CCW_CMD_WRITE_CONF 0x21
146 #define CCW_CMD_WRITE_STATUS 0x31
147 #define CCW_CMD_READ_VQ_CONF 0x32
148 #define CCW_CMD_SET_IND_ADAPTER 0x73
149 #define CCW_CMD_SET_VIRTIO_REV 0x83
151 #define VIRTIO_CCW_DOING_SET_VQ 0x00010000
152 #define VIRTIO_CCW_DOING_RESET 0x00040000
153 #define VIRTIO_CCW_DOING_READ_FEAT 0x00080000
154 #define VIRTIO_CCW_DOING_WRITE_FEAT 0x00100000
155 #define VIRTIO_CCW_DOING_READ_CONFIG 0x00200000
156 #define VIRTIO_CCW_DOING_WRITE_CONFIG 0x00400000
157 #define VIRTIO_CCW_DOING_WRITE_STATUS 0x00800000
158 #define VIRTIO_CCW_DOING_SET_IND 0x01000000
159 #define VIRTIO_CCW_DOING_READ_VQ_CONF 0x02000000
160 #define VIRTIO_CCW_DOING_SET_CONF_IND 0x04000000
161 #define VIRTIO_CCW_DOING_SET_IND_ADAPTER 0x08000000
162 #define VIRTIO_CCW_DOING_SET_VIRTIO_REV 0x10000000
163 #define VIRTIO_CCW_INTPARM_MASK 0xffff0000
165 static struct virtio_ccw_device *to_vc_device(struct virtio_device *vdev)
167 return container_of(vdev, struct virtio_ccw_device, vdev);
170 static void drop_airq_indicator(struct virtqueue *vq, struct airq_info *info)
172 unsigned long i, flags;
174 write_lock_irqsave(&info->lock, flags);
175 for (i = 0; i < airq_iv_end(info->aiv); i++) {
176 if (vq == (void *)airq_iv_get_ptr(info->aiv, i)) {
177 airq_iv_free_bit(info->aiv, i);
178 airq_iv_set_ptr(info->aiv, i, 0);
182 write_unlock_irqrestore(&info->lock, flags);
185 static void virtio_airq_handler(struct airq_struct *airq)
187 struct airq_info *info = container_of(airq, struct airq_info, airq);
190 inc_irq_stat(IRQIO_VAI);
191 read_lock(&info->lock);
192 /* Walk through indicators field, summary indicator active. */
194 ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
197 vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
199 info->summary_indicator = 0;
201 /* Walk through indicators field, summary indicator not active. */
203 ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
206 vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
208 read_unlock(&info->lock);
211 static struct airq_info *new_airq_info(void)
213 struct airq_info *info;
216 info = kzalloc(sizeof(*info), GFP_KERNEL);
219 rwlock_init(&info->lock);
220 info->aiv = airq_iv_create(VIRTIO_IV_BITS, AIRQ_IV_ALLOC | AIRQ_IV_PTR);
225 info->airq.handler = virtio_airq_handler;
226 info->airq.lsi_ptr = &info->summary_indicator;
227 info->airq.lsi_mask = 0xff;
228 info->airq.isc = VIRTIO_AIRQ_ISC;
229 rc = register_adapter_interrupt(&info->airq);
231 airq_iv_release(info->aiv);
238 static void destroy_airq_info(struct airq_info *info)
243 unregister_adapter_interrupt(&info->airq);
244 airq_iv_release(info->aiv);
248 static unsigned long get_airq_indicator(struct virtqueue *vqs[], int nvqs,
249 u64 *first, void **airq_info)
252 struct airq_info *info;
253 unsigned long indicator_addr = 0;
254 unsigned long bit, flags;
256 for (i = 0; i < MAX_AIRQ_AREAS && !indicator_addr; i++) {
258 airq_areas[i] = new_airq_info();
259 info = airq_areas[i];
262 write_lock_irqsave(&info->lock, flags);
263 bit = airq_iv_alloc(info->aiv, nvqs);
265 /* Not enough vacancies. */
266 write_unlock_irqrestore(&info->lock, flags);
271 indicator_addr = (unsigned long)info->aiv->vector;
272 for (j = 0; j < nvqs; j++) {
273 airq_iv_set_ptr(info->aiv, bit + j,
274 (unsigned long)vqs[j]);
276 write_unlock_irqrestore(&info->lock, flags);
278 return indicator_addr;
281 static void virtio_ccw_drop_indicators(struct virtio_ccw_device *vcdev)
283 struct virtio_ccw_vq_info *info;
285 list_for_each_entry(info, &vcdev->virtqueues, node)
286 drop_airq_indicator(info->vq, vcdev->airq_info);
289 static int doing_io(struct virtio_ccw_device *vcdev, __u32 flag)
294 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
298 ret = vcdev->curr_io & flag;
299 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
303 static int ccw_io_helper(struct virtio_ccw_device *vcdev,
304 struct ccw1 *ccw, __u32 intparm)
308 int flag = intparm & VIRTIO_CCW_INTPARM_MASK;
311 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
312 ret = ccw_device_start(vcdev->cdev, ccw, intparm, 0, 0);
316 vcdev->curr_io |= flag;
318 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
320 } while (ret == -EBUSY);
321 wait_event(vcdev->wait_q, doing_io(vcdev, flag) == 0);
322 return ret ? ret : vcdev->err;
325 static void virtio_ccw_drop_indicator(struct virtio_ccw_device *vcdev,
329 unsigned long *indicatorp = NULL;
330 struct virtio_thinint_area *thinint_area = NULL;
331 struct airq_info *airq_info = vcdev->airq_info;
333 if (vcdev->is_thinint) {
334 thinint_area = kzalloc(sizeof(*thinint_area),
335 GFP_DMA | GFP_KERNEL);
338 thinint_area->summary_indicator =
339 (unsigned long) &airq_info->summary_indicator;
340 thinint_area->isc = VIRTIO_AIRQ_ISC;
341 ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
342 ccw->count = sizeof(*thinint_area);
343 ccw->cda = (__u32)(unsigned long) thinint_area;
345 /* payload is the address of the indicators */
346 indicatorp = kmalloc(sizeof(&vcdev->indicators),
347 GFP_DMA | GFP_KERNEL);
351 ccw->cmd_code = CCW_CMD_SET_IND;
352 ccw->count = sizeof(&vcdev->indicators);
353 ccw->cda = (__u32)(unsigned long) indicatorp;
355 /* Deregister indicators from host. */
356 vcdev->indicators = 0;
358 ret = ccw_io_helper(vcdev, ccw,
360 VIRTIO_CCW_DOING_SET_IND_ADAPTER :
361 VIRTIO_CCW_DOING_SET_IND);
362 if (ret && (ret != -ENODEV))
363 dev_info(&vcdev->cdev->dev,
364 "Failed to deregister indicators (%d)\n", ret);
365 else if (vcdev->is_thinint)
366 virtio_ccw_drop_indicators(vcdev);
371 static inline long __do_kvm_notify(struct subchannel_id schid,
372 unsigned long queue_index,
375 register unsigned long __nr asm("1") = KVM_S390_VIRTIO_CCW_NOTIFY;
376 register struct subchannel_id __schid asm("2") = schid;
377 register unsigned long __index asm("3") = queue_index;
378 register long __rc asm("2");
379 register long __cookie asm("4") = cookie;
381 asm volatile ("diag 2,4,0x500\n"
382 : "=d" (__rc) : "d" (__nr), "d" (__schid), "d" (__index),
388 static inline long do_kvm_notify(struct subchannel_id schid,
389 unsigned long queue_index,
392 diag_stat_inc(DIAG_STAT_X500);
393 return __do_kvm_notify(schid, queue_index, cookie);
396 static bool virtio_ccw_kvm_notify(struct virtqueue *vq)
398 struct virtio_ccw_vq_info *info = vq->priv;
399 struct virtio_ccw_device *vcdev;
400 struct subchannel_id schid;
402 vcdev = to_vc_device(info->vq->vdev);
403 ccw_device_get_schid(vcdev->cdev, &schid);
404 info->cookie = do_kvm_notify(schid, vq->index, info->cookie);
405 if (info->cookie < 0)
410 static int virtio_ccw_read_vq_conf(struct virtio_ccw_device *vcdev,
411 struct ccw1 *ccw, int index)
415 vcdev->config_block->index = index;
416 ccw->cmd_code = CCW_CMD_READ_VQ_CONF;
418 ccw->count = sizeof(struct vq_config_block);
419 ccw->cda = (__u32)(unsigned long)(vcdev->config_block);
420 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_VQ_CONF);
423 return vcdev->config_block->num;
426 static void virtio_ccw_del_vq(struct virtqueue *vq, struct ccw1 *ccw)
428 struct virtio_ccw_device *vcdev = to_vc_device(vq->vdev);
429 struct virtio_ccw_vq_info *info = vq->priv;
433 unsigned int index = vq->index;
435 /* Remove from our list. */
436 spin_lock_irqsave(&vcdev->lock, flags);
437 list_del(&info->node);
438 spin_unlock_irqrestore(&vcdev->lock, flags);
440 /* Release from host. */
441 if (vcdev->revision == 0) {
442 info->info_block->l.queue = 0;
443 info->info_block->l.align = 0;
444 info->info_block->l.index = index;
445 info->info_block->l.num = 0;
446 ccw->count = sizeof(info->info_block->l);
448 info->info_block->s.desc = 0;
449 info->info_block->s.index = index;
450 info->info_block->s.num = 0;
451 info->info_block->s.avail = 0;
452 info->info_block->s.used = 0;
453 ccw->count = sizeof(info->info_block->s);
455 ccw->cmd_code = CCW_CMD_SET_VQ;
457 ccw->cda = (__u32)(unsigned long)(info->info_block);
458 ret = ccw_io_helper(vcdev, ccw,
459 VIRTIO_CCW_DOING_SET_VQ | index);
461 * -ENODEV isn't considered an error: The device is gone anyway.
462 * This may happen on device detach.
464 if (ret && (ret != -ENODEV))
465 dev_warn(&vq->vdev->dev, "Error %d while deleting queue %d",
468 vring_del_virtqueue(vq);
469 size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN));
470 free_pages_exact(info->queue, size);
471 kfree(info->info_block);
475 static void virtio_ccw_del_vqs(struct virtio_device *vdev)
477 struct virtqueue *vq, *n;
479 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
481 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
485 virtio_ccw_drop_indicator(vcdev, ccw);
487 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
488 virtio_ccw_del_vq(vq, ccw);
493 static struct virtqueue *virtio_ccw_setup_vq(struct virtio_device *vdev,
494 int i, vq_callback_t *callback,
498 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
500 struct virtqueue *vq = NULL;
501 struct virtio_ccw_vq_info *info;
502 unsigned long size = 0; /* silence the compiler */
505 /* Allocate queue. */
506 info = kzalloc(sizeof(struct virtio_ccw_vq_info), GFP_KERNEL);
508 dev_warn(&vcdev->cdev->dev, "no info\n");
512 info->info_block = kzalloc(sizeof(*info->info_block),
513 GFP_DMA | GFP_KERNEL);
514 if (!info->info_block) {
515 dev_warn(&vcdev->cdev->dev, "no info block\n");
519 info->num = virtio_ccw_read_vq_conf(vcdev, ccw, i);
524 size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN));
525 info->queue = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
526 if (info->queue == NULL) {
527 dev_warn(&vcdev->cdev->dev, "no queue\n");
532 vq = vring_new_virtqueue(i, info->num, KVM_VIRTIO_CCW_RING_ALIGN, vdev,
533 true, info->queue, virtio_ccw_kvm_notify,
536 /* For now, we fail if we can't get the requested size. */
537 dev_warn(&vcdev->cdev->dev, "no vq\n");
542 /* Register it with the host. */
543 if (vcdev->revision == 0) {
544 info->info_block->l.queue = (__u64)info->queue;
545 info->info_block->l.align = KVM_VIRTIO_CCW_RING_ALIGN;
546 info->info_block->l.index = i;
547 info->info_block->l.num = info->num;
548 ccw->count = sizeof(info->info_block->l);
550 info->info_block->s.desc = (__u64)info->queue;
551 info->info_block->s.index = i;
552 info->info_block->s.num = info->num;
553 info->info_block->s.avail = (__u64)virtqueue_get_avail(vq);
554 info->info_block->s.used = (__u64)virtqueue_get_used(vq);
555 ccw->count = sizeof(info->info_block->s);
557 ccw->cmd_code = CCW_CMD_SET_VQ;
559 ccw->cda = (__u32)(unsigned long)(info->info_block);
560 err = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_VQ | i);
562 dev_warn(&vcdev->cdev->dev, "SET_VQ failed\n");
569 /* Save it to our list. */
570 spin_lock_irqsave(&vcdev->lock, flags);
571 list_add(&info->node, &vcdev->virtqueues);
572 spin_unlock_irqrestore(&vcdev->lock, flags);
578 vring_del_virtqueue(vq);
581 free_pages_exact(info->queue, size);
582 kfree(info->info_block);
588 static int virtio_ccw_register_adapter_ind(struct virtio_ccw_device *vcdev,
589 struct virtqueue *vqs[], int nvqs,
593 struct virtio_thinint_area *thinint_area = NULL;
594 struct airq_info *info;
596 thinint_area = kzalloc(sizeof(*thinint_area), GFP_DMA | GFP_KERNEL);
601 /* Try to get an indicator. */
602 thinint_area->indicator = get_airq_indicator(vqs, nvqs,
603 &thinint_area->bit_nr,
605 if (!thinint_area->indicator) {
609 info = vcdev->airq_info;
610 thinint_area->summary_indicator =
611 (unsigned long) &info->summary_indicator;
612 thinint_area->isc = VIRTIO_AIRQ_ISC;
613 ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
614 ccw->flags = CCW_FLAG_SLI;
615 ccw->count = sizeof(*thinint_area);
616 ccw->cda = (__u32)(unsigned long)thinint_area;
617 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND_ADAPTER);
619 if (ret == -EOPNOTSUPP) {
621 * The host does not support adapter interrupts
622 * for virtio-ccw, stop trying.
624 virtio_ccw_use_airq = 0;
625 pr_info("Adapter interrupts unsupported on host\n");
627 dev_warn(&vcdev->cdev->dev,
628 "enabling adapter interrupts = %d\n", ret);
629 virtio_ccw_drop_indicators(vcdev);
636 static int virtio_ccw_find_vqs(struct virtio_device *vdev, unsigned nvqs,
637 struct virtqueue *vqs[],
638 vq_callback_t *callbacks[],
639 const char * const names[])
641 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
642 unsigned long *indicatorp = NULL;
646 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
650 for (i = 0; i < nvqs; ++i) {
651 vqs[i] = virtio_ccw_setup_vq(vdev, i, callbacks[i], names[i],
653 if (IS_ERR(vqs[i])) {
654 ret = PTR_ERR(vqs[i]);
661 * We need a data area under 2G to communicate. Our payload is
662 * the address of the indicators.
664 indicatorp = kmalloc(sizeof(&vcdev->indicators), GFP_DMA | GFP_KERNEL);
667 *indicatorp = (unsigned long) &vcdev->indicators;
668 if (vcdev->is_thinint) {
669 ret = virtio_ccw_register_adapter_ind(vcdev, vqs, nvqs, ccw);
671 /* no error, just fall back to legacy interrupts */
672 vcdev->is_thinint = 0;
674 if (!vcdev->is_thinint) {
675 /* Register queue indicators with host. */
676 vcdev->indicators = 0;
677 ccw->cmd_code = CCW_CMD_SET_IND;
679 ccw->count = sizeof(&vcdev->indicators);
680 ccw->cda = (__u32)(unsigned long) indicatorp;
681 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND);
685 /* Register indicators2 with host for config changes */
686 *indicatorp = (unsigned long) &vcdev->indicators2;
687 vcdev->indicators2 = 0;
688 ccw->cmd_code = CCW_CMD_SET_CONF_IND;
690 ccw->count = sizeof(&vcdev->indicators2);
691 ccw->cda = (__u32)(unsigned long) indicatorp;
692 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_CONF_IND);
702 virtio_ccw_del_vqs(vdev);
706 static void virtio_ccw_reset(struct virtio_device *vdev)
708 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
711 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
715 /* Zero status bits. */
718 /* Send a reset ccw on device. */
719 ccw->cmd_code = CCW_CMD_VDEV_RESET;
723 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_RESET);
727 static u64 virtio_ccw_get_features(struct virtio_device *vdev)
729 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
730 struct virtio_feature_desc *features;
735 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
739 features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
744 /* Read the feature bits from the host. */
746 ccw->cmd_code = CCW_CMD_READ_FEAT;
748 ccw->count = sizeof(*features);
749 ccw->cda = (__u32)(unsigned long)features;
750 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
756 rc = le32_to_cpu(features->features);
758 if (vcdev->revision == 0)
761 /* Read second half of the feature bits from the host. */
763 ccw->cmd_code = CCW_CMD_READ_FEAT;
765 ccw->count = sizeof(*features);
766 ccw->cda = (__u32)(unsigned long)features;
767 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
769 rc |= (u64)le32_to_cpu(features->features) << 32;
777 static int virtio_ccw_finalize_features(struct virtio_device *vdev)
779 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
780 struct virtio_feature_desc *features;
784 if (vcdev->revision >= 1 &&
785 !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
786 dev_err(&vdev->dev, "virtio: device uses revision 1 "
787 "but does not have VIRTIO_F_VERSION_1\n");
791 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
795 features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
800 /* Give virtio_ring a chance to accept features. */
801 vring_transport_features(vdev);
804 features->features = cpu_to_le32((u32)vdev->features);
805 /* Write the first half of the feature bits to the host. */
806 ccw->cmd_code = CCW_CMD_WRITE_FEAT;
808 ccw->count = sizeof(*features);
809 ccw->cda = (__u32)(unsigned long)features;
810 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
814 if (vcdev->revision == 0)
818 features->features = cpu_to_le32(vdev->features >> 32);
819 /* Write the second half of the feature bits to the host. */
820 ccw->cmd_code = CCW_CMD_WRITE_FEAT;
822 ccw->count = sizeof(*features);
823 ccw->cda = (__u32)(unsigned long)features;
824 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
833 static void virtio_ccw_get_config(struct virtio_device *vdev,
834 unsigned int offset, void *buf, unsigned len)
836 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
841 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
845 config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
849 /* Read the config area from the host. */
850 ccw->cmd_code = CCW_CMD_READ_CONF;
852 ccw->count = offset + len;
853 ccw->cda = (__u32)(unsigned long)config_area;
854 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG);
858 memcpy(vcdev->config, config_area, offset + len);
860 memcpy(buf, &vcdev->config[offset], len);
861 if (vcdev->config_ready < offset + len)
862 vcdev->config_ready = offset + len;
869 static void virtio_ccw_set_config(struct virtio_device *vdev,
870 unsigned int offset, const void *buf,
873 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
877 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
881 config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
885 /* Make sure we don't overwrite fields. */
886 if (vcdev->config_ready < offset)
887 virtio_ccw_get_config(vdev, 0, NULL, offset);
888 memcpy(&vcdev->config[offset], buf, len);
889 /* Write the config area to the host. */
890 memcpy(config_area, vcdev->config, sizeof(vcdev->config));
891 ccw->cmd_code = CCW_CMD_WRITE_CONF;
893 ccw->count = offset + len;
894 ccw->cda = (__u32)(unsigned long)config_area;
895 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG);
902 static u8 virtio_ccw_get_status(struct virtio_device *vdev)
904 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
906 return *vcdev->status;
909 static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status)
911 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
912 u8 old_status = *vcdev->status;
916 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
920 /* Write the status to the host. */
921 *vcdev->status = status;
922 ccw->cmd_code = CCW_CMD_WRITE_STATUS;
924 ccw->count = sizeof(status);
925 ccw->cda = (__u32)(unsigned long)vcdev->status;
926 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS);
927 /* Write failed? We assume status is unchanged. */
929 *vcdev->status = old_status;
933 static struct virtio_config_ops virtio_ccw_config_ops = {
934 .get_features = virtio_ccw_get_features,
935 .finalize_features = virtio_ccw_finalize_features,
936 .get = virtio_ccw_get_config,
937 .set = virtio_ccw_set_config,
938 .get_status = virtio_ccw_get_status,
939 .set_status = virtio_ccw_set_status,
940 .reset = virtio_ccw_reset,
941 .find_vqs = virtio_ccw_find_vqs,
942 .del_vqs = virtio_ccw_del_vqs,
947 * ccw bus driver related functions
950 static void virtio_ccw_release_dev(struct device *_d)
952 struct virtio_device *dev = dev_to_virtio(_d);
953 struct virtio_ccw_device *vcdev = to_vc_device(dev);
955 kfree(vcdev->status);
956 kfree(vcdev->config_block);
960 static int irb_is_error(struct irb *irb)
962 if (scsw_cstat(&irb->scsw) != 0)
964 if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
966 if (scsw_cc(&irb->scsw) != 0)
971 static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev,
974 struct virtio_ccw_vq_info *info;
976 struct virtqueue *vq;
979 spin_lock_irqsave(&vcdev->lock, flags);
980 list_for_each_entry(info, &vcdev->virtqueues, node) {
981 if (info->vq->index == index) {
986 spin_unlock_irqrestore(&vcdev->lock, flags);
990 static void virtio_ccw_check_activity(struct virtio_ccw_device *vcdev,
993 if (vcdev->curr_io & activity) {
995 case VIRTIO_CCW_DOING_READ_FEAT:
996 case VIRTIO_CCW_DOING_WRITE_FEAT:
997 case VIRTIO_CCW_DOING_READ_CONFIG:
998 case VIRTIO_CCW_DOING_WRITE_CONFIG:
999 case VIRTIO_CCW_DOING_WRITE_STATUS:
1000 case VIRTIO_CCW_DOING_SET_VQ:
1001 case VIRTIO_CCW_DOING_SET_IND:
1002 case VIRTIO_CCW_DOING_SET_CONF_IND:
1003 case VIRTIO_CCW_DOING_RESET:
1004 case VIRTIO_CCW_DOING_READ_VQ_CONF:
1005 case VIRTIO_CCW_DOING_SET_IND_ADAPTER:
1006 case VIRTIO_CCW_DOING_SET_VIRTIO_REV:
1007 vcdev->curr_io &= ~activity;
1008 wake_up(&vcdev->wait_q);
1011 /* don't know what to do... */
1012 dev_warn(&vcdev->cdev->dev,
1013 "Suspicious activity '%08x'\n", activity);
1020 static void virtio_ccw_int_handler(struct ccw_device *cdev,
1021 unsigned long intparm,
1024 __u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK;
1025 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1027 struct virtqueue *vq;
1032 vcdev->err = PTR_ERR(irb);
1033 virtio_ccw_check_activity(vcdev, activity);
1034 /* Don't poke around indicators, something's wrong. */
1037 /* Check if it's a notification from the host. */
1038 if ((intparm == 0) &&
1039 (scsw_stctl(&irb->scsw) ==
1040 (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) {
1043 if (irb_is_error(irb)) {
1044 /* Command reject? */
1045 if ((scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) &&
1046 (irb->ecw[0] & SNS0_CMD_REJECT))
1047 vcdev->err = -EOPNOTSUPP;
1049 /* Map everything else to -EIO. */
1052 virtio_ccw_check_activity(vcdev, activity);
1053 for_each_set_bit(i, &vcdev->indicators,
1054 sizeof(vcdev->indicators) * BITS_PER_BYTE) {
1055 /* The bit clear must happen before the vring kick. */
1056 clear_bit(i, &vcdev->indicators);
1058 vq = virtio_ccw_vq_by_ind(vcdev, i);
1059 vring_interrupt(0, vq);
1061 if (test_bit(0, &vcdev->indicators2)) {
1062 virtio_config_changed(&vcdev->vdev);
1063 clear_bit(0, &vcdev->indicators2);
1068 * We usually want to autoonline all devices, but give the admin
1069 * a way to exempt devices from this.
1071 #define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
1073 static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS];
1075 static char *no_auto = "";
1077 module_param(no_auto, charp, 0444);
1078 MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined");
1080 static int virtio_ccw_check_autoonline(struct ccw_device *cdev)
1082 struct ccw_dev_id id;
1084 ccw_device_get_id(cdev, &id);
1085 if (test_bit(id.devno, devs_no_auto[id.ssid]))
1090 static void virtio_ccw_auto_online(void *data, async_cookie_t cookie)
1092 struct ccw_device *cdev = data;
1095 ret = ccw_device_set_online(cdev);
1097 dev_warn(&cdev->dev, "Failed to set online: %d\n", ret);
1100 static int virtio_ccw_probe(struct ccw_device *cdev)
1102 cdev->handler = virtio_ccw_int_handler;
1104 if (virtio_ccw_check_autoonline(cdev))
1105 async_schedule(virtio_ccw_auto_online, cdev);
1109 static struct virtio_ccw_device *virtio_grab_drvdata(struct ccw_device *cdev)
1111 unsigned long flags;
1112 struct virtio_ccw_device *vcdev;
1114 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1115 vcdev = dev_get_drvdata(&cdev->dev);
1116 if (!vcdev || vcdev->going_away) {
1117 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1120 vcdev->going_away = true;
1121 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1125 static void virtio_ccw_remove(struct ccw_device *cdev)
1127 unsigned long flags;
1128 struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
1130 if (vcdev && cdev->online) {
1131 if (vcdev->device_lost)
1132 virtio_break_device(&vcdev->vdev);
1133 unregister_virtio_device(&vcdev->vdev);
1134 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1135 dev_set_drvdata(&cdev->dev, NULL);
1136 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1138 cdev->handler = NULL;
1141 static int virtio_ccw_offline(struct ccw_device *cdev)
1143 unsigned long flags;
1144 struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
1148 if (vcdev->device_lost)
1149 virtio_break_device(&vcdev->vdev);
1150 unregister_virtio_device(&vcdev->vdev);
1151 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1152 dev_set_drvdata(&cdev->dev, NULL);
1153 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1157 static int virtio_ccw_set_transport_rev(struct virtio_ccw_device *vcdev)
1159 struct virtio_rev_info *rev;
1163 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
1166 rev = kzalloc(sizeof(*rev), GFP_DMA | GFP_KERNEL);
1172 /* Set transport revision */
1173 ccw->cmd_code = CCW_CMD_SET_VIRTIO_REV;
1175 ccw->count = sizeof(*rev);
1176 ccw->cda = (__u32)(unsigned long)rev;
1178 vcdev->revision = VIRTIO_CCW_REV_MAX;
1180 rev->revision = vcdev->revision;
1181 /* none of our supported revisions carry payload */
1183 ret = ccw_io_helper(vcdev, ccw,
1184 VIRTIO_CCW_DOING_SET_VIRTIO_REV);
1185 if (ret == -EOPNOTSUPP) {
1186 if (vcdev->revision == 0)
1188 * The host device does not support setting
1189 * the revision: let's operate it in legacy
1196 } while (ret == -EOPNOTSUPP);
1203 static int virtio_ccw_online(struct ccw_device *cdev)
1206 struct virtio_ccw_device *vcdev;
1207 unsigned long flags;
1209 vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL);
1211 dev_warn(&cdev->dev, "Could not get memory for virtio\n");
1215 vcdev->config_block = kzalloc(sizeof(*vcdev->config_block),
1216 GFP_DMA | GFP_KERNEL);
1217 if (!vcdev->config_block) {
1221 vcdev->status = kzalloc(sizeof(*vcdev->status), GFP_DMA | GFP_KERNEL);
1222 if (!vcdev->status) {
1227 vcdev->is_thinint = virtio_ccw_use_airq; /* at least try */
1229 vcdev->vdev.dev.parent = &cdev->dev;
1230 vcdev->vdev.dev.release = virtio_ccw_release_dev;
1231 vcdev->vdev.config = &virtio_ccw_config_ops;
1233 init_waitqueue_head(&vcdev->wait_q);
1234 INIT_LIST_HEAD(&vcdev->virtqueues);
1235 spin_lock_init(&vcdev->lock);
1237 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1238 dev_set_drvdata(&cdev->dev, vcdev);
1239 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1240 vcdev->vdev.id.vendor = cdev->id.cu_type;
1241 vcdev->vdev.id.device = cdev->id.cu_model;
1243 ret = virtio_ccw_set_transport_rev(vcdev);
1247 ret = register_virtio_device(&vcdev->vdev);
1249 dev_warn(&cdev->dev, "Failed to register virtio device: %d\n",
1255 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1256 dev_set_drvdata(&cdev->dev, NULL);
1257 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1258 put_device(&vcdev->vdev.dev);
1262 kfree(vcdev->status);
1263 kfree(vcdev->config_block);
1269 static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event)
1272 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1275 * Make sure vcdev is set
1276 * i.e. set_offline/remove callback not already running
1283 vcdev->device_lost = true;
1293 static struct ccw_device_id virtio_ids[] = {
1294 { CCW_DEVICE(0x3832, 0) },
1297 MODULE_DEVICE_TABLE(ccw, virtio_ids);
1299 static struct ccw_driver virtio_ccw_driver = {
1301 .owner = THIS_MODULE,
1302 .name = "virtio_ccw",
1305 .probe = virtio_ccw_probe,
1306 .remove = virtio_ccw_remove,
1307 .set_offline = virtio_ccw_offline,
1308 .set_online = virtio_ccw_online,
1309 .notify = virtio_ccw_cio_notify,
1310 .int_class = IRQIO_VIR,
1313 static int __init pure_hex(char **cp, unsigned int *val, int min_digit,
1314 int max_digit, int max_val)
1321 while (diff <= max_digit) {
1322 int value = hex_to_bin(**cp);
1326 *val = *val * 16 + value;
1331 if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
1337 static int __init parse_busid(char *str, unsigned int *cssid,
1338 unsigned int *ssid, unsigned int *devno)
1349 ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
1350 if (ret || (str_work[0] != '.'))
1353 ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
1354 if (ret || (str_work[0] != '.'))
1357 ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
1358 if (ret || (str_work[0] != '\0'))
1366 static void __init no_auto_parse(void)
1368 unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
1373 while ((parm = strsep(&str, ","))) {
1374 rc = parse_busid(strsep(&parm, "-"), &from_cssid,
1379 rc = parse_busid(parm, &to_cssid,
1381 if ((from_ssid > to_ssid) ||
1382 ((from_ssid == to_ssid) && (from > to)))
1385 to_cssid = from_cssid;
1386 to_ssid = from_ssid;
1391 while ((from_ssid < to_ssid) ||
1392 ((from_ssid == to_ssid) && (from <= to))) {
1393 set_bit(from, devs_no_auto[from_ssid]);
1395 if (from > __MAX_SUBCHANNEL) {
1403 static int __init virtio_ccw_init(void)
1405 /* parse no_auto string before we do anything further */
1407 return ccw_driver_register(&virtio_ccw_driver);
1409 module_init(virtio_ccw_init);
1411 static void __exit virtio_ccw_exit(void)
1415 ccw_driver_unregister(&virtio_ccw_driver);
1416 for (i = 0; i < MAX_AIRQ_AREAS; i++)
1417 destroy_airq_info(airq_areas[i]);
1419 module_exit(virtio_ccw_exit);