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;
71 struct vq_info_block_legacy {
78 struct vq_info_block {
87 struct virtio_feature_desc {
92 struct virtio_thinint_area {
93 unsigned long summary_indicator;
94 unsigned long indicator;
99 struct virtio_rev_info {
105 /* the highest virtio-ccw revision we support */
106 #define VIRTIO_CCW_REV_MAX 1
108 struct virtio_ccw_vq_info {
109 struct virtqueue *vq;
113 struct vq_info_block s;
114 struct vq_info_block_legacy l;
117 struct list_head node;
121 #define VIRTIO_AIRQ_ISC IO_SCH_ISC /* inherit from subchannel */
123 #define VIRTIO_IV_BITS (L1_CACHE_BYTES * 8)
124 #define MAX_AIRQ_AREAS 20
126 static int virtio_ccw_use_airq = 1;
130 u8 summary_indicator;
131 struct airq_struct airq;
134 static struct airq_info *airq_areas[MAX_AIRQ_AREAS];
136 #define CCW_CMD_SET_VQ 0x13
137 #define CCW_CMD_VDEV_RESET 0x33
138 #define CCW_CMD_SET_IND 0x43
139 #define CCW_CMD_SET_CONF_IND 0x53
140 #define CCW_CMD_READ_FEAT 0x12
141 #define CCW_CMD_WRITE_FEAT 0x11
142 #define CCW_CMD_READ_CONF 0x22
143 #define CCW_CMD_WRITE_CONF 0x21
144 #define CCW_CMD_WRITE_STATUS 0x31
145 #define CCW_CMD_READ_VQ_CONF 0x32
146 #define CCW_CMD_SET_IND_ADAPTER 0x73
147 #define CCW_CMD_SET_VIRTIO_REV 0x83
149 #define VIRTIO_CCW_DOING_SET_VQ 0x00010000
150 #define VIRTIO_CCW_DOING_RESET 0x00040000
151 #define VIRTIO_CCW_DOING_READ_FEAT 0x00080000
152 #define VIRTIO_CCW_DOING_WRITE_FEAT 0x00100000
153 #define VIRTIO_CCW_DOING_READ_CONFIG 0x00200000
154 #define VIRTIO_CCW_DOING_WRITE_CONFIG 0x00400000
155 #define VIRTIO_CCW_DOING_WRITE_STATUS 0x00800000
156 #define VIRTIO_CCW_DOING_SET_IND 0x01000000
157 #define VIRTIO_CCW_DOING_READ_VQ_CONF 0x02000000
158 #define VIRTIO_CCW_DOING_SET_CONF_IND 0x04000000
159 #define VIRTIO_CCW_DOING_SET_IND_ADAPTER 0x08000000
160 #define VIRTIO_CCW_DOING_SET_VIRTIO_REV 0x10000000
161 #define VIRTIO_CCW_INTPARM_MASK 0xffff0000
163 static struct virtio_ccw_device *to_vc_device(struct virtio_device *vdev)
165 return container_of(vdev, struct virtio_ccw_device, vdev);
168 static void drop_airq_indicator(struct virtqueue *vq, struct airq_info *info)
170 unsigned long i, flags;
172 write_lock_irqsave(&info->lock, flags);
173 for (i = 0; i < airq_iv_end(info->aiv); i++) {
174 if (vq == (void *)airq_iv_get_ptr(info->aiv, i)) {
175 airq_iv_free_bit(info->aiv, i);
176 airq_iv_set_ptr(info->aiv, i, 0);
180 write_unlock_irqrestore(&info->lock, flags);
183 static void virtio_airq_handler(struct airq_struct *airq)
185 struct airq_info *info = container_of(airq, struct airq_info, airq);
188 inc_irq_stat(IRQIO_VAI);
189 read_lock(&info->lock);
190 /* Walk through indicators field, summary indicator active. */
192 ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
195 vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
197 info->summary_indicator = 0;
199 /* Walk through indicators field, summary indicator not active. */
201 ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
204 vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
206 read_unlock(&info->lock);
209 static struct airq_info *new_airq_info(void)
211 struct airq_info *info;
214 info = kzalloc(sizeof(*info), GFP_KERNEL);
217 rwlock_init(&info->lock);
218 info->aiv = airq_iv_create(VIRTIO_IV_BITS, AIRQ_IV_ALLOC | AIRQ_IV_PTR);
223 info->airq.handler = virtio_airq_handler;
224 info->airq.lsi_ptr = &info->summary_indicator;
225 info->airq.lsi_mask = 0xff;
226 info->airq.isc = VIRTIO_AIRQ_ISC;
227 rc = register_adapter_interrupt(&info->airq);
229 airq_iv_release(info->aiv);
236 static void destroy_airq_info(struct airq_info *info)
241 unregister_adapter_interrupt(&info->airq);
242 airq_iv_release(info->aiv);
246 static unsigned long get_airq_indicator(struct virtqueue *vqs[], int nvqs,
247 u64 *first, void **airq_info)
250 struct airq_info *info;
251 unsigned long indicator_addr = 0;
252 unsigned long bit, flags;
254 for (i = 0; i < MAX_AIRQ_AREAS && !indicator_addr; i++) {
256 airq_areas[i] = new_airq_info();
257 info = airq_areas[i];
260 write_lock_irqsave(&info->lock, flags);
261 bit = airq_iv_alloc(info->aiv, nvqs);
263 /* Not enough vacancies. */
264 write_unlock_irqrestore(&info->lock, flags);
269 indicator_addr = (unsigned long)info->aiv->vector;
270 for (j = 0; j < nvqs; j++) {
271 airq_iv_set_ptr(info->aiv, bit + j,
272 (unsigned long)vqs[j]);
274 write_unlock_irqrestore(&info->lock, flags);
276 return indicator_addr;
279 static void virtio_ccw_drop_indicators(struct virtio_ccw_device *vcdev)
281 struct virtio_ccw_vq_info *info;
283 list_for_each_entry(info, &vcdev->virtqueues, node)
284 drop_airq_indicator(info->vq, vcdev->airq_info);
287 static int doing_io(struct virtio_ccw_device *vcdev, __u32 flag)
292 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
296 ret = vcdev->curr_io & flag;
297 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
301 static int ccw_io_helper(struct virtio_ccw_device *vcdev,
302 struct ccw1 *ccw, __u32 intparm)
306 int flag = intparm & VIRTIO_CCW_INTPARM_MASK;
309 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
310 ret = ccw_device_start(vcdev->cdev, ccw, intparm, 0, 0);
314 vcdev->curr_io |= flag;
316 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
318 } while (ret == -EBUSY);
319 wait_event(vcdev->wait_q, doing_io(vcdev, flag) == 0);
320 return ret ? ret : vcdev->err;
323 static void virtio_ccw_drop_indicator(struct virtio_ccw_device *vcdev,
327 unsigned long *indicatorp = NULL;
328 struct virtio_thinint_area *thinint_area = NULL;
329 struct airq_info *airq_info = vcdev->airq_info;
331 if (vcdev->is_thinint) {
332 thinint_area = kzalloc(sizeof(*thinint_area),
333 GFP_DMA | GFP_KERNEL);
336 thinint_area->summary_indicator =
337 (unsigned long) &airq_info->summary_indicator;
338 thinint_area->isc = VIRTIO_AIRQ_ISC;
339 ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
340 ccw->count = sizeof(*thinint_area);
341 ccw->cda = (__u32)(unsigned long) thinint_area;
343 indicatorp = kmalloc(sizeof(&vcdev->indicators),
344 GFP_DMA | GFP_KERNEL);
348 ccw->cmd_code = CCW_CMD_SET_IND;
349 ccw->count = sizeof(vcdev->indicators);
350 ccw->cda = (__u32)(unsigned long) indicatorp;
352 /* Deregister indicators from host. */
353 vcdev->indicators = 0;
355 ret = ccw_io_helper(vcdev, ccw,
357 VIRTIO_CCW_DOING_SET_IND_ADAPTER :
358 VIRTIO_CCW_DOING_SET_IND);
359 if (ret && (ret != -ENODEV))
360 dev_info(&vcdev->cdev->dev,
361 "Failed to deregister indicators (%d)\n", ret);
362 else if (vcdev->is_thinint)
363 virtio_ccw_drop_indicators(vcdev);
368 static inline long do_kvm_notify(struct subchannel_id schid,
369 unsigned long queue_index,
372 register unsigned long __nr asm("1") = KVM_S390_VIRTIO_CCW_NOTIFY;
373 register struct subchannel_id __schid asm("2") = schid;
374 register unsigned long __index asm("3") = queue_index;
375 register long __rc asm("2");
376 register long __cookie asm("4") = cookie;
378 asm volatile ("diag 2,4,0x500\n"
379 : "=d" (__rc) : "d" (__nr), "d" (__schid), "d" (__index),
385 static bool virtio_ccw_kvm_notify(struct virtqueue *vq)
387 struct virtio_ccw_vq_info *info = vq->priv;
388 struct virtio_ccw_device *vcdev;
389 struct subchannel_id schid;
391 vcdev = to_vc_device(info->vq->vdev);
392 ccw_device_get_schid(vcdev->cdev, &schid);
393 info->cookie = do_kvm_notify(schid, vq->index, info->cookie);
394 if (info->cookie < 0)
399 static int virtio_ccw_read_vq_conf(struct virtio_ccw_device *vcdev,
400 struct ccw1 *ccw, int index)
402 vcdev->config_block->index = index;
403 ccw->cmd_code = CCW_CMD_READ_VQ_CONF;
405 ccw->count = sizeof(struct vq_config_block);
406 ccw->cda = (__u32)(unsigned long)(vcdev->config_block);
407 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_VQ_CONF);
408 return vcdev->config_block->num;
411 static void virtio_ccw_del_vq(struct virtqueue *vq, struct ccw1 *ccw)
413 struct virtio_ccw_device *vcdev = to_vc_device(vq->vdev);
414 struct virtio_ccw_vq_info *info = vq->priv;
418 unsigned int index = vq->index;
420 /* Remove from our list. */
421 spin_lock_irqsave(&vcdev->lock, flags);
422 list_del(&info->node);
423 spin_unlock_irqrestore(&vcdev->lock, flags);
425 /* Release from host. */
426 if (vcdev->revision == 0) {
427 info->info_block->l.queue = 0;
428 info->info_block->l.align = 0;
429 info->info_block->l.index = index;
430 info->info_block->l.num = 0;
431 ccw->count = sizeof(info->info_block->l);
433 info->info_block->s.desc = 0;
434 info->info_block->s.index = index;
435 info->info_block->s.num = 0;
436 info->info_block->s.avail = 0;
437 info->info_block->s.used = 0;
438 ccw->count = sizeof(info->info_block->s);
440 ccw->cmd_code = CCW_CMD_SET_VQ;
442 ccw->cda = (__u32)(unsigned long)(info->info_block);
443 ret = ccw_io_helper(vcdev, ccw,
444 VIRTIO_CCW_DOING_SET_VQ | index);
446 * -ENODEV isn't considered an error: The device is gone anyway.
447 * This may happen on device detach.
449 if (ret && (ret != -ENODEV))
450 dev_warn(&vq->vdev->dev, "Error %d while deleting queue %d",
453 vring_del_virtqueue(vq);
454 size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN));
455 free_pages_exact(info->queue, size);
456 kfree(info->info_block);
460 static void virtio_ccw_del_vqs(struct virtio_device *vdev)
462 struct virtqueue *vq, *n;
464 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
466 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
470 virtio_ccw_drop_indicator(vcdev, ccw);
472 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
473 virtio_ccw_del_vq(vq, ccw);
478 static struct virtqueue *virtio_ccw_setup_vq(struct virtio_device *vdev,
479 int i, vq_callback_t *callback,
483 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
485 struct virtqueue *vq = NULL;
486 struct virtio_ccw_vq_info *info;
487 unsigned long size = 0; /* silence the compiler */
490 /* Allocate queue. */
491 info = kzalloc(sizeof(struct virtio_ccw_vq_info), GFP_KERNEL);
493 dev_warn(&vcdev->cdev->dev, "no info\n");
497 info->info_block = kzalloc(sizeof(*info->info_block),
498 GFP_DMA | GFP_KERNEL);
499 if (!info->info_block) {
500 dev_warn(&vcdev->cdev->dev, "no info block\n");
504 info->num = virtio_ccw_read_vq_conf(vcdev, ccw, i);
505 size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN));
506 info->queue = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
507 if (info->queue == NULL) {
508 dev_warn(&vcdev->cdev->dev, "no queue\n");
513 vq = vring_new_virtqueue(i, info->num, KVM_VIRTIO_CCW_RING_ALIGN, vdev,
514 true, info->queue, virtio_ccw_kvm_notify,
517 /* For now, we fail if we can't get the requested size. */
518 dev_warn(&vcdev->cdev->dev, "no vq\n");
523 /* Register it with the host. */
524 if (vcdev->revision == 0) {
525 info->info_block->l.queue = (__u64)info->queue;
526 info->info_block->l.align = KVM_VIRTIO_CCW_RING_ALIGN;
527 info->info_block->l.index = i;
528 info->info_block->l.num = info->num;
529 ccw->count = sizeof(info->info_block->l);
531 info->info_block->s.desc = (__u64)info->queue;
532 info->info_block->s.index = i;
533 info->info_block->s.num = info->num;
534 info->info_block->s.avail = (__u64)virtqueue_get_avail(vq);
535 info->info_block->s.used = (__u64)virtqueue_get_used(vq);
536 ccw->count = sizeof(info->info_block->s);
538 ccw->cmd_code = CCW_CMD_SET_VQ;
540 ccw->cda = (__u32)(unsigned long)(info->info_block);
541 err = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_VQ | i);
543 dev_warn(&vcdev->cdev->dev, "SET_VQ failed\n");
550 /* Save it to our list. */
551 spin_lock_irqsave(&vcdev->lock, flags);
552 list_add(&info->node, &vcdev->virtqueues);
553 spin_unlock_irqrestore(&vcdev->lock, flags);
559 vring_del_virtqueue(vq);
562 free_pages_exact(info->queue, size);
563 kfree(info->info_block);
569 static int virtio_ccw_register_adapter_ind(struct virtio_ccw_device *vcdev,
570 struct virtqueue *vqs[], int nvqs,
574 struct virtio_thinint_area *thinint_area = NULL;
575 struct airq_info *info;
577 thinint_area = kzalloc(sizeof(*thinint_area), GFP_DMA | GFP_KERNEL);
582 /* Try to get an indicator. */
583 thinint_area->indicator = get_airq_indicator(vqs, nvqs,
584 &thinint_area->bit_nr,
586 if (!thinint_area->indicator) {
590 info = vcdev->airq_info;
591 thinint_area->summary_indicator =
592 (unsigned long) &info->summary_indicator;
593 thinint_area->isc = VIRTIO_AIRQ_ISC;
594 ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
595 ccw->flags = CCW_FLAG_SLI;
596 ccw->count = sizeof(*thinint_area);
597 ccw->cda = (__u32)(unsigned long)thinint_area;
598 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND_ADAPTER);
600 if (ret == -EOPNOTSUPP) {
602 * The host does not support adapter interrupts
603 * for virtio-ccw, stop trying.
605 virtio_ccw_use_airq = 0;
606 pr_info("Adapter interrupts unsupported on host\n");
608 dev_warn(&vcdev->cdev->dev,
609 "enabling adapter interrupts = %d\n", ret);
610 virtio_ccw_drop_indicators(vcdev);
617 static int virtio_ccw_find_vqs(struct virtio_device *vdev, unsigned nvqs,
618 struct virtqueue *vqs[],
619 vq_callback_t *callbacks[],
622 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
623 unsigned long *indicatorp = NULL;
627 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
631 for (i = 0; i < nvqs; ++i) {
632 vqs[i] = virtio_ccw_setup_vq(vdev, i, callbacks[i], names[i],
634 if (IS_ERR(vqs[i])) {
635 ret = PTR_ERR(vqs[i]);
641 /* We need a data area under 2G to communicate. */
642 indicatorp = kmalloc(sizeof(&vcdev->indicators), GFP_DMA | GFP_KERNEL);
645 *indicatorp = (unsigned long) &vcdev->indicators;
646 if (vcdev->is_thinint) {
647 ret = virtio_ccw_register_adapter_ind(vcdev, vqs, nvqs, ccw);
649 /* no error, just fall back to legacy interrupts */
650 vcdev->is_thinint = 0;
652 if (!vcdev->is_thinint) {
653 /* Register queue indicators with host. */
654 vcdev->indicators = 0;
655 ccw->cmd_code = CCW_CMD_SET_IND;
657 ccw->count = sizeof(vcdev->indicators);
658 ccw->cda = (__u32)(unsigned long) indicatorp;
659 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND);
663 /* Register indicators2 with host for config changes */
664 *indicatorp = (unsigned long) &vcdev->indicators2;
665 vcdev->indicators2 = 0;
666 ccw->cmd_code = CCW_CMD_SET_CONF_IND;
668 ccw->count = sizeof(vcdev->indicators2);
669 ccw->cda = (__u32)(unsigned long) indicatorp;
670 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_CONF_IND);
680 virtio_ccw_del_vqs(vdev);
684 static void virtio_ccw_reset(struct virtio_device *vdev)
686 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
689 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
693 /* Zero status bits. */
696 /* Send a reset ccw on device. */
697 ccw->cmd_code = CCW_CMD_VDEV_RESET;
701 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_RESET);
705 static u64 virtio_ccw_get_features(struct virtio_device *vdev)
707 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
708 struct virtio_feature_desc *features;
713 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
717 features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
722 /* Read the feature bits from the host. */
724 ccw->cmd_code = CCW_CMD_READ_FEAT;
726 ccw->count = sizeof(*features);
727 ccw->cda = (__u32)(unsigned long)features;
728 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
734 rc = le32_to_cpu(features->features);
736 if (vcdev->revision == 0)
739 /* Read second half of the feature bits from the host. */
741 ccw->cmd_code = CCW_CMD_READ_FEAT;
743 ccw->count = sizeof(*features);
744 ccw->cda = (__u32)(unsigned long)features;
745 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
747 rc |= (u64)le32_to_cpu(features->features) << 32;
755 static int virtio_ccw_finalize_features(struct virtio_device *vdev)
757 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
758 struct virtio_feature_desc *features;
762 if (vcdev->revision >= 1 &&
763 !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
764 dev_err(&vdev->dev, "virtio: device uses revision 1 "
765 "but does not have VIRTIO_F_VERSION_1\n");
769 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
773 features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
778 /* Give virtio_ring a chance to accept features. */
779 vring_transport_features(vdev);
782 features->features = cpu_to_le32((u32)vdev->features);
783 /* Write the first half of the feature bits to the host. */
784 ccw->cmd_code = CCW_CMD_WRITE_FEAT;
786 ccw->count = sizeof(*features);
787 ccw->cda = (__u32)(unsigned long)features;
788 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
792 if (vcdev->revision == 0)
796 features->features = cpu_to_le32(vdev->features >> 32);
797 /* Write the second half of the feature bits to the host. */
798 ccw->cmd_code = CCW_CMD_WRITE_FEAT;
800 ccw->count = sizeof(*features);
801 ccw->cda = (__u32)(unsigned long)features;
802 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
811 static void virtio_ccw_get_config(struct virtio_device *vdev,
812 unsigned int offset, void *buf, unsigned len)
814 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
819 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
823 config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
827 /* Read the config area from the host. */
828 ccw->cmd_code = CCW_CMD_READ_CONF;
830 ccw->count = offset + len;
831 ccw->cda = (__u32)(unsigned long)config_area;
832 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG);
836 memcpy(vcdev->config, config_area, sizeof(vcdev->config));
837 memcpy(buf, &vcdev->config[offset], len);
844 static void virtio_ccw_set_config(struct virtio_device *vdev,
845 unsigned int offset, const void *buf,
848 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
852 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
856 config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
860 memcpy(&vcdev->config[offset], buf, len);
861 /* Write the config area to the host. */
862 memcpy(config_area, vcdev->config, sizeof(vcdev->config));
863 ccw->cmd_code = CCW_CMD_WRITE_CONF;
865 ccw->count = offset + len;
866 ccw->cda = (__u32)(unsigned long)config_area;
867 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG);
874 static u8 virtio_ccw_get_status(struct virtio_device *vdev)
876 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
878 return *vcdev->status;
881 static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status)
883 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
884 u8 old_status = *vcdev->status;
888 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
892 /* Write the status to the host. */
893 *vcdev->status = status;
894 ccw->cmd_code = CCW_CMD_WRITE_STATUS;
896 ccw->count = sizeof(status);
897 ccw->cda = (__u32)(unsigned long)vcdev->status;
898 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS);
899 /* Write failed? We assume status is unchanged. */
901 *vcdev->status = old_status;
905 static struct virtio_config_ops virtio_ccw_config_ops = {
906 .get_features = virtio_ccw_get_features,
907 .finalize_features = virtio_ccw_finalize_features,
908 .get = virtio_ccw_get_config,
909 .set = virtio_ccw_set_config,
910 .get_status = virtio_ccw_get_status,
911 .set_status = virtio_ccw_set_status,
912 .reset = virtio_ccw_reset,
913 .find_vqs = virtio_ccw_find_vqs,
914 .del_vqs = virtio_ccw_del_vqs,
919 * ccw bus driver related functions
922 static void virtio_ccw_release_dev(struct device *_d)
924 struct virtio_device *dev = container_of(_d, struct virtio_device,
926 struct virtio_ccw_device *vcdev = to_vc_device(dev);
928 kfree(vcdev->status);
929 kfree(vcdev->config_block);
933 static int irb_is_error(struct irb *irb)
935 if (scsw_cstat(&irb->scsw) != 0)
937 if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
939 if (scsw_cc(&irb->scsw) != 0)
944 static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev,
947 struct virtio_ccw_vq_info *info;
949 struct virtqueue *vq;
952 spin_lock_irqsave(&vcdev->lock, flags);
953 list_for_each_entry(info, &vcdev->virtqueues, node) {
954 if (info->vq->index == index) {
959 spin_unlock_irqrestore(&vcdev->lock, flags);
963 static void virtio_ccw_int_handler(struct ccw_device *cdev,
964 unsigned long intparm,
967 __u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK;
968 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
970 struct virtqueue *vq;
974 /* Check if it's a notification from the host. */
975 if ((intparm == 0) &&
976 (scsw_stctl(&irb->scsw) ==
977 (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) {
980 if (irb_is_error(irb)) {
981 /* Command reject? */
982 if ((scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) &&
983 (irb->ecw[0] & SNS0_CMD_REJECT))
984 vcdev->err = -EOPNOTSUPP;
986 /* Map everything else to -EIO. */
989 if (vcdev->curr_io & activity) {
991 case VIRTIO_CCW_DOING_READ_FEAT:
992 case VIRTIO_CCW_DOING_WRITE_FEAT:
993 case VIRTIO_CCW_DOING_READ_CONFIG:
994 case VIRTIO_CCW_DOING_WRITE_CONFIG:
995 case VIRTIO_CCW_DOING_WRITE_STATUS:
996 case VIRTIO_CCW_DOING_SET_VQ:
997 case VIRTIO_CCW_DOING_SET_IND:
998 case VIRTIO_CCW_DOING_SET_CONF_IND:
999 case VIRTIO_CCW_DOING_RESET:
1000 case VIRTIO_CCW_DOING_READ_VQ_CONF:
1001 case VIRTIO_CCW_DOING_SET_IND_ADAPTER:
1002 case VIRTIO_CCW_DOING_SET_VIRTIO_REV:
1003 vcdev->curr_io &= ~activity;
1004 wake_up(&vcdev->wait_q);
1007 /* don't know what to do... */
1008 dev_warn(&cdev->dev, "Suspicious activity '%08x'\n",
1014 for_each_set_bit(i, &vcdev->indicators,
1015 sizeof(vcdev->indicators) * BITS_PER_BYTE) {
1016 /* The bit clear must happen before the vring kick. */
1017 clear_bit(i, &vcdev->indicators);
1019 vq = virtio_ccw_vq_by_ind(vcdev, i);
1020 vring_interrupt(0, vq);
1022 if (test_bit(0, &vcdev->indicators2)) {
1023 virtio_config_changed(&vcdev->vdev);
1024 clear_bit(0, &vcdev->indicators2);
1029 * We usually want to autoonline all devices, but give the admin
1030 * a way to exempt devices from this.
1032 #define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
1034 static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS];
1036 static char *no_auto = "";
1038 module_param(no_auto, charp, 0444);
1039 MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined");
1041 static int virtio_ccw_check_autoonline(struct ccw_device *cdev)
1043 struct ccw_dev_id id;
1045 ccw_device_get_id(cdev, &id);
1046 if (test_bit(id.devno, devs_no_auto[id.ssid]))
1051 static void virtio_ccw_auto_online(void *data, async_cookie_t cookie)
1053 struct ccw_device *cdev = data;
1056 ret = ccw_device_set_online(cdev);
1058 dev_warn(&cdev->dev, "Failed to set online: %d\n", ret);
1061 static int virtio_ccw_probe(struct ccw_device *cdev)
1063 cdev->handler = virtio_ccw_int_handler;
1065 if (virtio_ccw_check_autoonline(cdev))
1066 async_schedule(virtio_ccw_auto_online, cdev);
1070 static struct virtio_ccw_device *virtio_grab_drvdata(struct ccw_device *cdev)
1072 unsigned long flags;
1073 struct virtio_ccw_device *vcdev;
1075 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1076 vcdev = dev_get_drvdata(&cdev->dev);
1077 if (!vcdev || vcdev->going_away) {
1078 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1081 vcdev->going_away = true;
1082 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1086 static void virtio_ccw_remove(struct ccw_device *cdev)
1088 unsigned long flags;
1089 struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
1091 if (vcdev && cdev->online) {
1092 if (vcdev->device_lost)
1093 virtio_break_device(&vcdev->vdev);
1094 unregister_virtio_device(&vcdev->vdev);
1095 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1096 dev_set_drvdata(&cdev->dev, NULL);
1097 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1099 cdev->handler = NULL;
1102 static int virtio_ccw_offline(struct ccw_device *cdev)
1104 unsigned long flags;
1105 struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
1109 if (vcdev->device_lost)
1110 virtio_break_device(&vcdev->vdev);
1111 unregister_virtio_device(&vcdev->vdev);
1112 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1113 dev_set_drvdata(&cdev->dev, NULL);
1114 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1118 static int virtio_ccw_set_transport_rev(struct virtio_ccw_device *vcdev)
1120 struct virtio_rev_info *rev;
1124 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
1127 rev = kzalloc(sizeof(*rev), GFP_DMA | GFP_KERNEL);
1133 /* Set transport revision */
1134 ccw->cmd_code = CCW_CMD_SET_VIRTIO_REV;
1136 ccw->count = sizeof(*rev);
1137 ccw->cda = (__u32)(unsigned long)rev;
1139 vcdev->revision = VIRTIO_CCW_REV_MAX;
1141 rev->revision = vcdev->revision;
1142 /* none of our supported revisions carry payload */
1144 ret = ccw_io_helper(vcdev, ccw,
1145 VIRTIO_CCW_DOING_SET_VIRTIO_REV);
1146 if (ret == -EOPNOTSUPP) {
1147 if (vcdev->revision == 0)
1149 * The host device does not support setting
1150 * the revision: let's operate it in legacy
1157 } while (ret == -EOPNOTSUPP);
1164 static int virtio_ccw_online(struct ccw_device *cdev)
1167 struct virtio_ccw_device *vcdev;
1168 unsigned long flags;
1170 vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL);
1172 dev_warn(&cdev->dev, "Could not get memory for virtio\n");
1176 vcdev->config_block = kzalloc(sizeof(*vcdev->config_block),
1177 GFP_DMA | GFP_KERNEL);
1178 if (!vcdev->config_block) {
1182 vcdev->status = kzalloc(sizeof(*vcdev->status), GFP_DMA | GFP_KERNEL);
1183 if (!vcdev->status) {
1188 vcdev->is_thinint = virtio_ccw_use_airq; /* at least try */
1190 vcdev->vdev.dev.parent = &cdev->dev;
1191 vcdev->vdev.dev.release = virtio_ccw_release_dev;
1192 vcdev->vdev.config = &virtio_ccw_config_ops;
1194 init_waitqueue_head(&vcdev->wait_q);
1195 INIT_LIST_HEAD(&vcdev->virtqueues);
1196 spin_lock_init(&vcdev->lock);
1198 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1199 dev_set_drvdata(&cdev->dev, vcdev);
1200 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1201 vcdev->vdev.id.vendor = cdev->id.cu_type;
1202 vcdev->vdev.id.device = cdev->id.cu_model;
1204 ret = virtio_ccw_set_transport_rev(vcdev);
1208 ret = register_virtio_device(&vcdev->vdev);
1210 dev_warn(&cdev->dev, "Failed to register virtio device: %d\n",
1216 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1217 dev_set_drvdata(&cdev->dev, NULL);
1218 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1219 put_device(&vcdev->vdev.dev);
1223 kfree(vcdev->status);
1224 kfree(vcdev->config_block);
1230 static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event)
1233 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1236 * Make sure vcdev is set
1237 * i.e. set_offline/remove callback not already running
1244 vcdev->device_lost = true;
1254 static struct ccw_device_id virtio_ids[] = {
1255 { CCW_DEVICE(0x3832, 0) },
1258 MODULE_DEVICE_TABLE(ccw, virtio_ids);
1260 static struct ccw_driver virtio_ccw_driver = {
1262 .owner = THIS_MODULE,
1263 .name = "virtio_ccw",
1266 .probe = virtio_ccw_probe,
1267 .remove = virtio_ccw_remove,
1268 .set_offline = virtio_ccw_offline,
1269 .set_online = virtio_ccw_online,
1270 .notify = virtio_ccw_cio_notify,
1271 .int_class = IRQIO_VIR,
1274 static int __init pure_hex(char **cp, unsigned int *val, int min_digit,
1275 int max_digit, int max_val)
1282 while (diff <= max_digit) {
1283 int value = hex_to_bin(**cp);
1287 *val = *val * 16 + value;
1292 if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
1298 static int __init parse_busid(char *str, unsigned int *cssid,
1299 unsigned int *ssid, unsigned int *devno)
1310 ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
1311 if (ret || (str_work[0] != '.'))
1314 ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
1315 if (ret || (str_work[0] != '.'))
1318 ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
1319 if (ret || (str_work[0] != '\0'))
1327 static void __init no_auto_parse(void)
1329 unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
1334 while ((parm = strsep(&str, ","))) {
1335 rc = parse_busid(strsep(&parm, "-"), &from_cssid,
1340 rc = parse_busid(parm, &to_cssid,
1342 if ((from_ssid > to_ssid) ||
1343 ((from_ssid == to_ssid) && (from > to)))
1346 to_cssid = from_cssid;
1347 to_ssid = from_ssid;
1352 while ((from_ssid < to_ssid) ||
1353 ((from_ssid == to_ssid) && (from <= to))) {
1354 set_bit(from, devs_no_auto[from_ssid]);
1356 if (from > __MAX_SUBCHANNEL) {
1364 static int __init virtio_ccw_init(void)
1366 /* parse no_auto string before we do anything further */
1368 return ccw_driver_register(&virtio_ccw_driver);
1370 module_init(virtio_ccw_init);
1372 static void __exit virtio_ccw_exit(void)
1376 ccw_driver_unregister(&virtio_ccw_driver);
1377 for (i = 0; i < MAX_AIRQ_AREAS; i++)
1378 destroy_airq_info(airq_areas[i]);
1380 module_exit(virtio_ccw_exit);