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>
31 #include <asm/setup.h>
34 #include <asm/ccwdev.h>
35 #include <asm/virtio-ccw.h>
40 * virtio related functions
43 struct vq_config_block {
48 #define VIRTIO_CCW_CONFIG_SIZE 0x100
49 /* same as PCI config space size, should be enough for all drivers */
51 struct virtio_ccw_device {
52 struct virtio_device vdev;
54 __u8 config[VIRTIO_CCW_CONFIG_SIZE];
55 struct ccw_device *cdev;
58 unsigned int revision; /* Transport revision */
59 wait_queue_head_t wait_q;
61 struct list_head virtqueues;
62 unsigned long indicators;
63 unsigned long indicators2;
64 struct vq_config_block *config_block;
68 unsigned int config_ready;
72 struct vq_info_block_legacy {
79 struct vq_info_block {
88 struct virtio_feature_desc {
93 struct virtio_thinint_area {
94 unsigned long summary_indicator;
95 unsigned long indicator;
100 struct virtio_rev_info {
106 /* the highest virtio-ccw revision we support */
107 #define VIRTIO_CCW_REV_MAX 1
109 struct virtio_ccw_vq_info {
110 struct virtqueue *vq;
114 struct vq_info_block s;
115 struct vq_info_block_legacy l;
118 struct list_head node;
122 #define VIRTIO_AIRQ_ISC IO_SCH_ISC /* inherit from subchannel */
124 #define VIRTIO_IV_BITS (L1_CACHE_BYTES * 8)
125 #define MAX_AIRQ_AREAS 20
127 static int virtio_ccw_use_airq = 1;
131 u8 summary_indicator;
132 struct airq_struct airq;
135 static struct airq_info *airq_areas[MAX_AIRQ_AREAS];
137 #define CCW_CMD_SET_VQ 0x13
138 #define CCW_CMD_VDEV_RESET 0x33
139 #define CCW_CMD_SET_IND 0x43
140 #define CCW_CMD_SET_CONF_IND 0x53
141 #define CCW_CMD_READ_FEAT 0x12
142 #define CCW_CMD_WRITE_FEAT 0x11
143 #define CCW_CMD_READ_CONF 0x22
144 #define CCW_CMD_WRITE_CONF 0x21
145 #define CCW_CMD_WRITE_STATUS 0x31
146 #define CCW_CMD_READ_VQ_CONF 0x32
147 #define CCW_CMD_SET_IND_ADAPTER 0x73
148 #define CCW_CMD_SET_VIRTIO_REV 0x83
150 #define VIRTIO_CCW_DOING_SET_VQ 0x00010000
151 #define VIRTIO_CCW_DOING_RESET 0x00040000
152 #define VIRTIO_CCW_DOING_READ_FEAT 0x00080000
153 #define VIRTIO_CCW_DOING_WRITE_FEAT 0x00100000
154 #define VIRTIO_CCW_DOING_READ_CONFIG 0x00200000
155 #define VIRTIO_CCW_DOING_WRITE_CONFIG 0x00400000
156 #define VIRTIO_CCW_DOING_WRITE_STATUS 0x00800000
157 #define VIRTIO_CCW_DOING_SET_IND 0x01000000
158 #define VIRTIO_CCW_DOING_READ_VQ_CONF 0x02000000
159 #define VIRTIO_CCW_DOING_SET_CONF_IND 0x04000000
160 #define VIRTIO_CCW_DOING_SET_IND_ADAPTER 0x08000000
161 #define VIRTIO_CCW_DOING_SET_VIRTIO_REV 0x10000000
162 #define VIRTIO_CCW_INTPARM_MASK 0xffff0000
164 static struct virtio_ccw_device *to_vc_device(struct virtio_device *vdev)
166 return container_of(vdev, struct virtio_ccw_device, vdev);
169 static void drop_airq_indicator(struct virtqueue *vq, struct airq_info *info)
171 unsigned long i, flags;
173 write_lock_irqsave(&info->lock, flags);
174 for (i = 0; i < airq_iv_end(info->aiv); i++) {
175 if (vq == (void *)airq_iv_get_ptr(info->aiv, i)) {
176 airq_iv_free_bit(info->aiv, i);
177 airq_iv_set_ptr(info->aiv, i, 0);
181 write_unlock_irqrestore(&info->lock, flags);
184 static void virtio_airq_handler(struct airq_struct *airq)
186 struct airq_info *info = container_of(airq, struct airq_info, airq);
189 inc_irq_stat(IRQIO_VAI);
190 read_lock(&info->lock);
191 /* Walk through indicators field, summary indicator active. */
193 ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
196 vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
198 info->summary_indicator = 0;
200 /* Walk through indicators field, summary indicator not active. */
202 ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
205 vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
207 read_unlock(&info->lock);
210 static struct airq_info *new_airq_info(void)
212 struct airq_info *info;
215 info = kzalloc(sizeof(*info), GFP_KERNEL);
218 rwlock_init(&info->lock);
219 info->aiv = airq_iv_create(VIRTIO_IV_BITS, AIRQ_IV_ALLOC | AIRQ_IV_PTR);
224 info->airq.handler = virtio_airq_handler;
225 info->airq.lsi_ptr = &info->summary_indicator;
226 info->airq.lsi_mask = 0xff;
227 info->airq.isc = VIRTIO_AIRQ_ISC;
228 rc = register_adapter_interrupt(&info->airq);
230 airq_iv_release(info->aiv);
237 static void destroy_airq_info(struct airq_info *info)
242 unregister_adapter_interrupt(&info->airq);
243 airq_iv_release(info->aiv);
247 static unsigned long get_airq_indicator(struct virtqueue *vqs[], int nvqs,
248 u64 *first, void **airq_info)
251 struct airq_info *info;
252 unsigned long indicator_addr = 0;
253 unsigned long bit, flags;
255 for (i = 0; i < MAX_AIRQ_AREAS && !indicator_addr; i++) {
257 airq_areas[i] = new_airq_info();
258 info = airq_areas[i];
261 write_lock_irqsave(&info->lock, flags);
262 bit = airq_iv_alloc(info->aiv, nvqs);
264 /* Not enough vacancies. */
265 write_unlock_irqrestore(&info->lock, flags);
270 indicator_addr = (unsigned long)info->aiv->vector;
271 for (j = 0; j < nvqs; j++) {
272 airq_iv_set_ptr(info->aiv, bit + j,
273 (unsigned long)vqs[j]);
275 write_unlock_irqrestore(&info->lock, flags);
277 return indicator_addr;
280 static void virtio_ccw_drop_indicators(struct virtio_ccw_device *vcdev)
282 struct virtio_ccw_vq_info *info;
284 list_for_each_entry(info, &vcdev->virtqueues, node)
285 drop_airq_indicator(info->vq, vcdev->airq_info);
288 static int doing_io(struct virtio_ccw_device *vcdev, __u32 flag)
293 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
297 ret = vcdev->curr_io & flag;
298 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
302 static int ccw_io_helper(struct virtio_ccw_device *vcdev,
303 struct ccw1 *ccw, __u32 intparm)
307 int flag = intparm & VIRTIO_CCW_INTPARM_MASK;
310 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
311 ret = ccw_device_start(vcdev->cdev, ccw, intparm, 0, 0);
315 vcdev->curr_io |= flag;
317 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
319 } while (ret == -EBUSY);
320 wait_event(vcdev->wait_q, doing_io(vcdev, flag) == 0);
321 return ret ? ret : vcdev->err;
324 static void virtio_ccw_drop_indicator(struct virtio_ccw_device *vcdev,
328 unsigned long *indicatorp = NULL;
329 struct virtio_thinint_area *thinint_area = NULL;
330 struct airq_info *airq_info = vcdev->airq_info;
332 if (vcdev->is_thinint) {
333 thinint_area = kzalloc(sizeof(*thinint_area),
334 GFP_DMA | GFP_KERNEL);
337 thinint_area->summary_indicator =
338 (unsigned long) &airq_info->summary_indicator;
339 thinint_area->isc = VIRTIO_AIRQ_ISC;
340 ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
341 ccw->count = sizeof(*thinint_area);
342 ccw->cda = (__u32)(unsigned long) thinint_area;
344 indicatorp = kmalloc(sizeof(&vcdev->indicators),
345 GFP_DMA | GFP_KERNEL);
349 ccw->cmd_code = CCW_CMD_SET_IND;
350 ccw->count = sizeof(vcdev->indicators);
351 ccw->cda = (__u32)(unsigned long) indicatorp;
353 /* Deregister indicators from host. */
354 vcdev->indicators = 0;
356 ret = ccw_io_helper(vcdev, ccw,
358 VIRTIO_CCW_DOING_SET_IND_ADAPTER :
359 VIRTIO_CCW_DOING_SET_IND);
360 if (ret && (ret != -ENODEV))
361 dev_info(&vcdev->cdev->dev,
362 "Failed to deregister indicators (%d)\n", ret);
363 else if (vcdev->is_thinint)
364 virtio_ccw_drop_indicators(vcdev);
369 static inline long do_kvm_notify(struct subchannel_id schid,
370 unsigned long queue_index,
373 register unsigned long __nr asm("1") = KVM_S390_VIRTIO_CCW_NOTIFY;
374 register struct subchannel_id __schid asm("2") = schid;
375 register unsigned long __index asm("3") = queue_index;
376 register long __rc asm("2");
377 register long __cookie asm("4") = cookie;
379 asm volatile ("diag 2,4,0x500\n"
380 : "=d" (__rc) : "d" (__nr), "d" (__schid), "d" (__index),
386 static bool virtio_ccw_kvm_notify(struct virtqueue *vq)
388 struct virtio_ccw_vq_info *info = vq->priv;
389 struct virtio_ccw_device *vcdev;
390 struct subchannel_id schid;
392 vcdev = to_vc_device(info->vq->vdev);
393 ccw_device_get_schid(vcdev->cdev, &schid);
394 info->cookie = do_kvm_notify(schid, vq->index, info->cookie);
395 if (info->cookie < 0)
400 static int virtio_ccw_read_vq_conf(struct virtio_ccw_device *vcdev,
401 struct ccw1 *ccw, int index)
405 vcdev->config_block->index = index;
406 ccw->cmd_code = CCW_CMD_READ_VQ_CONF;
408 ccw->count = sizeof(struct vq_config_block);
409 ccw->cda = (__u32)(unsigned long)(vcdev->config_block);
410 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_VQ_CONF);
413 return vcdev->config_block->num;
416 static void virtio_ccw_del_vq(struct virtqueue *vq, struct ccw1 *ccw)
418 struct virtio_ccw_device *vcdev = to_vc_device(vq->vdev);
419 struct virtio_ccw_vq_info *info = vq->priv;
423 unsigned int index = vq->index;
425 /* Remove from our list. */
426 spin_lock_irqsave(&vcdev->lock, flags);
427 list_del(&info->node);
428 spin_unlock_irqrestore(&vcdev->lock, flags);
430 /* Release from host. */
431 if (vcdev->revision == 0) {
432 info->info_block->l.queue = 0;
433 info->info_block->l.align = 0;
434 info->info_block->l.index = index;
435 info->info_block->l.num = 0;
436 ccw->count = sizeof(info->info_block->l);
438 info->info_block->s.desc = 0;
439 info->info_block->s.index = index;
440 info->info_block->s.num = 0;
441 info->info_block->s.avail = 0;
442 info->info_block->s.used = 0;
443 ccw->count = sizeof(info->info_block->s);
445 ccw->cmd_code = CCW_CMD_SET_VQ;
447 ccw->cda = (__u32)(unsigned long)(info->info_block);
448 ret = ccw_io_helper(vcdev, ccw,
449 VIRTIO_CCW_DOING_SET_VQ | index);
451 * -ENODEV isn't considered an error: The device is gone anyway.
452 * This may happen on device detach.
454 if (ret && (ret != -ENODEV))
455 dev_warn(&vq->vdev->dev, "Error %d while deleting queue %d",
458 vring_del_virtqueue(vq);
459 size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN));
460 free_pages_exact(info->queue, size);
461 kfree(info->info_block);
465 static void virtio_ccw_del_vqs(struct virtio_device *vdev)
467 struct virtqueue *vq, *n;
469 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
471 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
475 virtio_ccw_drop_indicator(vcdev, ccw);
477 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
478 virtio_ccw_del_vq(vq, ccw);
483 static struct virtqueue *virtio_ccw_setup_vq(struct virtio_device *vdev,
484 int i, vq_callback_t *callback,
488 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
490 struct virtqueue *vq = NULL;
491 struct virtio_ccw_vq_info *info;
492 unsigned long size = 0; /* silence the compiler */
495 /* Allocate queue. */
496 info = kzalloc(sizeof(struct virtio_ccw_vq_info), GFP_KERNEL);
498 dev_warn(&vcdev->cdev->dev, "no info\n");
502 info->info_block = kzalloc(sizeof(*info->info_block),
503 GFP_DMA | GFP_KERNEL);
504 if (!info->info_block) {
505 dev_warn(&vcdev->cdev->dev, "no info block\n");
509 info->num = virtio_ccw_read_vq_conf(vcdev, ccw, i);
514 size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN));
515 info->queue = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
516 if (info->queue == NULL) {
517 dev_warn(&vcdev->cdev->dev, "no queue\n");
522 vq = vring_new_virtqueue(i, info->num, KVM_VIRTIO_CCW_RING_ALIGN, vdev,
523 true, info->queue, virtio_ccw_kvm_notify,
526 /* For now, we fail if we can't get the requested size. */
527 dev_warn(&vcdev->cdev->dev, "no vq\n");
532 /* Register it with the host. */
533 if (vcdev->revision == 0) {
534 info->info_block->l.queue = (__u64)info->queue;
535 info->info_block->l.align = KVM_VIRTIO_CCW_RING_ALIGN;
536 info->info_block->l.index = i;
537 info->info_block->l.num = info->num;
538 ccw->count = sizeof(info->info_block->l);
540 info->info_block->s.desc = (__u64)info->queue;
541 info->info_block->s.index = i;
542 info->info_block->s.num = info->num;
543 info->info_block->s.avail = (__u64)virtqueue_get_avail(vq);
544 info->info_block->s.used = (__u64)virtqueue_get_used(vq);
545 ccw->count = sizeof(info->info_block->s);
547 ccw->cmd_code = CCW_CMD_SET_VQ;
549 ccw->cda = (__u32)(unsigned long)(info->info_block);
550 err = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_VQ | i);
552 dev_warn(&vcdev->cdev->dev, "SET_VQ failed\n");
559 /* Save it to our list. */
560 spin_lock_irqsave(&vcdev->lock, flags);
561 list_add(&info->node, &vcdev->virtqueues);
562 spin_unlock_irqrestore(&vcdev->lock, flags);
568 vring_del_virtqueue(vq);
571 free_pages_exact(info->queue, size);
572 kfree(info->info_block);
578 static int virtio_ccw_register_adapter_ind(struct virtio_ccw_device *vcdev,
579 struct virtqueue *vqs[], int nvqs,
583 struct virtio_thinint_area *thinint_area = NULL;
584 struct airq_info *info;
586 thinint_area = kzalloc(sizeof(*thinint_area), GFP_DMA | GFP_KERNEL);
591 /* Try to get an indicator. */
592 thinint_area->indicator = get_airq_indicator(vqs, nvqs,
593 &thinint_area->bit_nr,
595 if (!thinint_area->indicator) {
599 info = vcdev->airq_info;
600 thinint_area->summary_indicator =
601 (unsigned long) &info->summary_indicator;
602 thinint_area->isc = VIRTIO_AIRQ_ISC;
603 ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
604 ccw->flags = CCW_FLAG_SLI;
605 ccw->count = sizeof(*thinint_area);
606 ccw->cda = (__u32)(unsigned long)thinint_area;
607 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND_ADAPTER);
609 if (ret == -EOPNOTSUPP) {
611 * The host does not support adapter interrupts
612 * for virtio-ccw, stop trying.
614 virtio_ccw_use_airq = 0;
615 pr_info("Adapter interrupts unsupported on host\n");
617 dev_warn(&vcdev->cdev->dev,
618 "enabling adapter interrupts = %d\n", ret);
619 virtio_ccw_drop_indicators(vcdev);
626 static int virtio_ccw_find_vqs(struct virtio_device *vdev, unsigned nvqs,
627 struct virtqueue *vqs[],
628 vq_callback_t *callbacks[],
631 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
632 unsigned long *indicatorp = NULL;
636 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
640 for (i = 0; i < nvqs; ++i) {
641 vqs[i] = virtio_ccw_setup_vq(vdev, i, callbacks[i], names[i],
643 if (IS_ERR(vqs[i])) {
644 ret = PTR_ERR(vqs[i]);
650 /* We need a data area under 2G to communicate. */
651 indicatorp = kmalloc(sizeof(&vcdev->indicators), GFP_DMA | GFP_KERNEL);
654 *indicatorp = (unsigned long) &vcdev->indicators;
655 if (vcdev->is_thinint) {
656 ret = virtio_ccw_register_adapter_ind(vcdev, vqs, nvqs, ccw);
658 /* no error, just fall back to legacy interrupts */
659 vcdev->is_thinint = 0;
661 if (!vcdev->is_thinint) {
662 /* Register queue indicators with host. */
663 vcdev->indicators = 0;
664 ccw->cmd_code = CCW_CMD_SET_IND;
666 ccw->count = sizeof(vcdev->indicators);
667 ccw->cda = (__u32)(unsigned long) indicatorp;
668 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND);
672 /* Register indicators2 with host for config changes */
673 *indicatorp = (unsigned long) &vcdev->indicators2;
674 vcdev->indicators2 = 0;
675 ccw->cmd_code = CCW_CMD_SET_CONF_IND;
677 ccw->count = sizeof(vcdev->indicators2);
678 ccw->cda = (__u32)(unsigned long) indicatorp;
679 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_CONF_IND);
689 virtio_ccw_del_vqs(vdev);
693 static void virtio_ccw_reset(struct virtio_device *vdev)
695 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
698 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
702 /* Zero status bits. */
705 /* Send a reset ccw on device. */
706 ccw->cmd_code = CCW_CMD_VDEV_RESET;
710 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_RESET);
714 static u64 virtio_ccw_get_features(struct virtio_device *vdev)
716 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
717 struct virtio_feature_desc *features;
722 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
726 features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
731 /* Read the feature bits from the host. */
733 ccw->cmd_code = CCW_CMD_READ_FEAT;
735 ccw->count = sizeof(*features);
736 ccw->cda = (__u32)(unsigned long)features;
737 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
743 rc = le32_to_cpu(features->features);
745 if (vcdev->revision == 0)
748 /* Read second half of the feature bits from the host. */
750 ccw->cmd_code = CCW_CMD_READ_FEAT;
752 ccw->count = sizeof(*features);
753 ccw->cda = (__u32)(unsigned long)features;
754 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
756 rc |= (u64)le32_to_cpu(features->features) << 32;
764 static int virtio_ccw_finalize_features(struct virtio_device *vdev)
766 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
767 struct virtio_feature_desc *features;
771 if (vcdev->revision >= 1 &&
772 !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
773 dev_err(&vdev->dev, "virtio: device uses revision 1 "
774 "but does not have VIRTIO_F_VERSION_1\n");
778 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
782 features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
787 /* Give virtio_ring a chance to accept features. */
788 vring_transport_features(vdev);
791 features->features = cpu_to_le32((u32)vdev->features);
792 /* Write the first half of the feature bits to the host. */
793 ccw->cmd_code = CCW_CMD_WRITE_FEAT;
795 ccw->count = sizeof(*features);
796 ccw->cda = (__u32)(unsigned long)features;
797 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
801 if (vcdev->revision == 0)
805 features->features = cpu_to_le32(vdev->features >> 32);
806 /* Write the second half of the feature bits to the host. */
807 ccw->cmd_code = CCW_CMD_WRITE_FEAT;
809 ccw->count = sizeof(*features);
810 ccw->cda = (__u32)(unsigned long)features;
811 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
820 static void virtio_ccw_get_config(struct virtio_device *vdev,
821 unsigned int offset, void *buf, unsigned len)
823 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
828 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
832 config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
836 /* Read the config area from the host. */
837 ccw->cmd_code = CCW_CMD_READ_CONF;
839 ccw->count = offset + len;
840 ccw->cda = (__u32)(unsigned long)config_area;
841 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG);
845 memcpy(vcdev->config, config_area, offset + len);
847 memcpy(buf, &vcdev->config[offset], len);
848 if (vcdev->config_ready < offset + len)
849 vcdev->config_ready = offset + len;
856 static void virtio_ccw_set_config(struct virtio_device *vdev,
857 unsigned int offset, const void *buf,
860 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
864 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
868 config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
872 /* Make sure we don't overwrite fields. */
873 if (vcdev->config_ready < offset)
874 virtio_ccw_get_config(vdev, 0, NULL, offset);
875 memcpy(&vcdev->config[offset], buf, len);
876 /* Write the config area to the host. */
877 memcpy(config_area, vcdev->config, sizeof(vcdev->config));
878 ccw->cmd_code = CCW_CMD_WRITE_CONF;
880 ccw->count = offset + len;
881 ccw->cda = (__u32)(unsigned long)config_area;
882 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG);
889 static u8 virtio_ccw_get_status(struct virtio_device *vdev)
891 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
893 return *vcdev->status;
896 static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status)
898 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
899 u8 old_status = *vcdev->status;
903 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
907 /* Write the status to the host. */
908 *vcdev->status = status;
909 ccw->cmd_code = CCW_CMD_WRITE_STATUS;
911 ccw->count = sizeof(status);
912 ccw->cda = (__u32)(unsigned long)vcdev->status;
913 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS);
914 /* Write failed? We assume status is unchanged. */
916 *vcdev->status = old_status;
920 static struct virtio_config_ops virtio_ccw_config_ops = {
921 .get_features = virtio_ccw_get_features,
922 .finalize_features = virtio_ccw_finalize_features,
923 .get = virtio_ccw_get_config,
924 .set = virtio_ccw_set_config,
925 .get_status = virtio_ccw_get_status,
926 .set_status = virtio_ccw_set_status,
927 .reset = virtio_ccw_reset,
928 .find_vqs = virtio_ccw_find_vqs,
929 .del_vqs = virtio_ccw_del_vqs,
934 * ccw bus driver related functions
937 static void virtio_ccw_release_dev(struct device *_d)
939 struct virtio_device *dev = container_of(_d, struct virtio_device,
941 struct virtio_ccw_device *vcdev = to_vc_device(dev);
943 kfree(vcdev->status);
944 kfree(vcdev->config_block);
948 static int irb_is_error(struct irb *irb)
950 if (scsw_cstat(&irb->scsw) != 0)
952 if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
954 if (scsw_cc(&irb->scsw) != 0)
959 static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev,
962 struct virtio_ccw_vq_info *info;
964 struct virtqueue *vq;
967 spin_lock_irqsave(&vcdev->lock, flags);
968 list_for_each_entry(info, &vcdev->virtqueues, node) {
969 if (info->vq->index == index) {
974 spin_unlock_irqrestore(&vcdev->lock, flags);
978 static void virtio_ccw_int_handler(struct ccw_device *cdev,
979 unsigned long intparm,
982 __u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK;
983 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
985 struct virtqueue *vq;
989 /* Check if it's a notification from the host. */
990 if ((intparm == 0) &&
991 (scsw_stctl(&irb->scsw) ==
992 (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) {
995 if (irb_is_error(irb)) {
996 /* Command reject? */
997 if ((scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) &&
998 (irb->ecw[0] & SNS0_CMD_REJECT))
999 vcdev->err = -EOPNOTSUPP;
1001 /* Map everything else to -EIO. */
1004 if (vcdev->curr_io & activity) {
1006 case VIRTIO_CCW_DOING_READ_FEAT:
1007 case VIRTIO_CCW_DOING_WRITE_FEAT:
1008 case VIRTIO_CCW_DOING_READ_CONFIG:
1009 case VIRTIO_CCW_DOING_WRITE_CONFIG:
1010 case VIRTIO_CCW_DOING_WRITE_STATUS:
1011 case VIRTIO_CCW_DOING_SET_VQ:
1012 case VIRTIO_CCW_DOING_SET_IND:
1013 case VIRTIO_CCW_DOING_SET_CONF_IND:
1014 case VIRTIO_CCW_DOING_RESET:
1015 case VIRTIO_CCW_DOING_READ_VQ_CONF:
1016 case VIRTIO_CCW_DOING_SET_IND_ADAPTER:
1017 case VIRTIO_CCW_DOING_SET_VIRTIO_REV:
1018 vcdev->curr_io &= ~activity;
1019 wake_up(&vcdev->wait_q);
1022 /* don't know what to do... */
1023 dev_warn(&cdev->dev, "Suspicious activity '%08x'\n",
1029 for_each_set_bit(i, &vcdev->indicators,
1030 sizeof(vcdev->indicators) * BITS_PER_BYTE) {
1031 /* The bit clear must happen before the vring kick. */
1032 clear_bit(i, &vcdev->indicators);
1034 vq = virtio_ccw_vq_by_ind(vcdev, i);
1035 vring_interrupt(0, vq);
1037 if (test_bit(0, &vcdev->indicators2)) {
1038 virtio_config_changed(&vcdev->vdev);
1039 clear_bit(0, &vcdev->indicators2);
1044 * We usually want to autoonline all devices, but give the admin
1045 * a way to exempt devices from this.
1047 #define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
1049 static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS];
1051 static char *no_auto = "";
1053 module_param(no_auto, charp, 0444);
1054 MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined");
1056 static int virtio_ccw_check_autoonline(struct ccw_device *cdev)
1058 struct ccw_dev_id id;
1060 ccw_device_get_id(cdev, &id);
1061 if (test_bit(id.devno, devs_no_auto[id.ssid]))
1066 static void virtio_ccw_auto_online(void *data, async_cookie_t cookie)
1068 struct ccw_device *cdev = data;
1071 ret = ccw_device_set_online(cdev);
1073 dev_warn(&cdev->dev, "Failed to set online: %d\n", ret);
1076 static int virtio_ccw_probe(struct ccw_device *cdev)
1078 cdev->handler = virtio_ccw_int_handler;
1080 if (virtio_ccw_check_autoonline(cdev))
1081 async_schedule(virtio_ccw_auto_online, cdev);
1085 static struct virtio_ccw_device *virtio_grab_drvdata(struct ccw_device *cdev)
1087 unsigned long flags;
1088 struct virtio_ccw_device *vcdev;
1090 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1091 vcdev = dev_get_drvdata(&cdev->dev);
1092 if (!vcdev || vcdev->going_away) {
1093 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1096 vcdev->going_away = true;
1097 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1101 static void virtio_ccw_remove(struct ccw_device *cdev)
1103 unsigned long flags;
1104 struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
1106 if (vcdev && cdev->online) {
1107 if (vcdev->device_lost)
1108 virtio_break_device(&vcdev->vdev);
1109 unregister_virtio_device(&vcdev->vdev);
1110 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1111 dev_set_drvdata(&cdev->dev, NULL);
1112 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1114 cdev->handler = NULL;
1117 static int virtio_ccw_offline(struct ccw_device *cdev)
1119 unsigned long flags;
1120 struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
1124 if (vcdev->device_lost)
1125 virtio_break_device(&vcdev->vdev);
1126 unregister_virtio_device(&vcdev->vdev);
1127 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1128 dev_set_drvdata(&cdev->dev, NULL);
1129 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1133 static int virtio_ccw_set_transport_rev(struct virtio_ccw_device *vcdev)
1135 struct virtio_rev_info *rev;
1139 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
1142 rev = kzalloc(sizeof(*rev), GFP_DMA | GFP_KERNEL);
1148 /* Set transport revision */
1149 ccw->cmd_code = CCW_CMD_SET_VIRTIO_REV;
1151 ccw->count = sizeof(*rev);
1152 ccw->cda = (__u32)(unsigned long)rev;
1154 vcdev->revision = VIRTIO_CCW_REV_MAX;
1156 rev->revision = vcdev->revision;
1157 /* none of our supported revisions carry payload */
1159 ret = ccw_io_helper(vcdev, ccw,
1160 VIRTIO_CCW_DOING_SET_VIRTIO_REV);
1161 if (ret == -EOPNOTSUPP) {
1162 if (vcdev->revision == 0)
1164 * The host device does not support setting
1165 * the revision: let's operate it in legacy
1172 } while (ret == -EOPNOTSUPP);
1179 static int virtio_ccw_online(struct ccw_device *cdev)
1182 struct virtio_ccw_device *vcdev;
1183 unsigned long flags;
1185 vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL);
1187 dev_warn(&cdev->dev, "Could not get memory for virtio\n");
1191 vcdev->config_block = kzalloc(sizeof(*vcdev->config_block),
1192 GFP_DMA | GFP_KERNEL);
1193 if (!vcdev->config_block) {
1197 vcdev->status = kzalloc(sizeof(*vcdev->status), GFP_DMA | GFP_KERNEL);
1198 if (!vcdev->status) {
1203 vcdev->is_thinint = virtio_ccw_use_airq; /* at least try */
1205 vcdev->vdev.dev.parent = &cdev->dev;
1206 vcdev->vdev.dev.release = virtio_ccw_release_dev;
1207 vcdev->vdev.config = &virtio_ccw_config_ops;
1209 init_waitqueue_head(&vcdev->wait_q);
1210 INIT_LIST_HEAD(&vcdev->virtqueues);
1211 spin_lock_init(&vcdev->lock);
1213 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1214 dev_set_drvdata(&cdev->dev, vcdev);
1215 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1216 vcdev->vdev.id.vendor = cdev->id.cu_type;
1217 vcdev->vdev.id.device = cdev->id.cu_model;
1219 ret = virtio_ccw_set_transport_rev(vcdev);
1223 ret = register_virtio_device(&vcdev->vdev);
1225 dev_warn(&cdev->dev, "Failed to register virtio device: %d\n",
1231 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1232 dev_set_drvdata(&cdev->dev, NULL);
1233 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1234 put_device(&vcdev->vdev.dev);
1238 kfree(vcdev->status);
1239 kfree(vcdev->config_block);
1245 static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event)
1248 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1251 * Make sure vcdev is set
1252 * i.e. set_offline/remove callback not already running
1259 vcdev->device_lost = true;
1269 static struct ccw_device_id virtio_ids[] = {
1270 { CCW_DEVICE(0x3832, 0) },
1273 MODULE_DEVICE_TABLE(ccw, virtio_ids);
1275 static struct ccw_driver virtio_ccw_driver = {
1277 .owner = THIS_MODULE,
1278 .name = "virtio_ccw",
1281 .probe = virtio_ccw_probe,
1282 .remove = virtio_ccw_remove,
1283 .set_offline = virtio_ccw_offline,
1284 .set_online = virtio_ccw_online,
1285 .notify = virtio_ccw_cio_notify,
1286 .int_class = IRQIO_VIR,
1289 static int __init pure_hex(char **cp, unsigned int *val, int min_digit,
1290 int max_digit, int max_val)
1297 while (diff <= max_digit) {
1298 int value = hex_to_bin(**cp);
1302 *val = *val * 16 + value;
1307 if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
1313 static int __init parse_busid(char *str, unsigned int *cssid,
1314 unsigned int *ssid, unsigned int *devno)
1325 ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
1326 if (ret || (str_work[0] != '.'))
1329 ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
1330 if (ret || (str_work[0] != '.'))
1333 ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
1334 if (ret || (str_work[0] != '\0'))
1342 static void __init no_auto_parse(void)
1344 unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
1349 while ((parm = strsep(&str, ","))) {
1350 rc = parse_busid(strsep(&parm, "-"), &from_cssid,
1355 rc = parse_busid(parm, &to_cssid,
1357 if ((from_ssid > to_ssid) ||
1358 ((from_ssid == to_ssid) && (from > to)))
1361 to_cssid = from_cssid;
1362 to_ssid = from_ssid;
1367 while ((from_ssid < to_ssid) ||
1368 ((from_ssid == to_ssid) && (from <= to))) {
1369 set_bit(from, devs_no_auto[from_ssid]);
1371 if (from > __MAX_SUBCHANNEL) {
1379 static int __init virtio_ccw_init(void)
1381 /* parse no_auto string before we do anything further */
1383 return ccw_driver_register(&virtio_ccw_driver);
1385 module_init(virtio_ccw_init);
1387 static void __exit virtio_ccw_exit(void)
1391 ccw_driver_unregister(&virtio_ccw_driver);
1392 for (i = 0; i < MAX_AIRQ_AREAS; i++)
1393 destroy_airq_info(airq_areas[i]);
1395 module_exit(virtio_ccw_exit);