1 // SPDX-License-Identifier: GPL-2.0
3 * ccw based virtio transport
5 * Copyright IBM Corp. 2012, 2014
7 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
10 #include <linux/kernel_stat.h>
11 #include <linux/init.h>
12 #include <linux/memblock.h>
13 #include <linux/err.h>
14 #include <linux/virtio.h>
15 #include <linux/virtio_config.h>
16 #include <linux/slab.h>
17 #include <linux/interrupt.h>
18 #include <linux/virtio_ring.h>
19 #include <linux/pfn.h>
20 #include <linux/async.h>
21 #include <linux/wait.h>
22 #include <linux/list.h>
23 #include <linux/bitops.h>
24 #include <linux/moduleparam.h>
26 #include <linux/kvm_para.h>
27 #include <linux/notifier.h>
29 #include <asm/setup.h>
32 #include <asm/ccwdev.h>
33 #include <asm/virtio-ccw.h>
38 * virtio related functions
41 struct vq_config_block {
46 #define VIRTIO_CCW_CONFIG_SIZE 0x100
47 /* same as PCI config space size, should be enough for all drivers */
49 struct vcdev_dma_area {
50 unsigned long indicators;
51 unsigned long indicators2;
52 struct vq_config_block config_block;
56 struct virtio_ccw_device {
57 struct virtio_device vdev;
58 __u8 config[VIRTIO_CCW_CONFIG_SIZE];
59 struct ccw_device *cdev;
62 unsigned int revision; /* Transport revision */
63 wait_queue_head_t wait_q;
66 struct mutex io_lock; /* Serializes I/O requests */
67 struct list_head virtqueues;
71 unsigned int config_ready;
73 struct vcdev_dma_area *dma_area;
76 static inline unsigned long *indicators(struct virtio_ccw_device *vcdev)
78 return &vcdev->dma_area->indicators;
81 static inline unsigned long *indicators2(struct virtio_ccw_device *vcdev)
83 return &vcdev->dma_area->indicators2;
86 struct vq_info_block_legacy {
93 struct vq_info_block {
102 struct virtio_feature_desc {
107 struct virtio_thinint_area {
108 unsigned long summary_indicator;
109 unsigned long indicator;
114 struct virtio_rev_info {
120 /* the highest virtio-ccw revision we support */
121 #define VIRTIO_CCW_REV_MAX 2
123 struct virtio_ccw_vq_info {
124 struct virtqueue *vq;
127 struct vq_info_block s;
128 struct vq_info_block_legacy l;
131 struct list_head node;
135 #define VIRTIO_AIRQ_ISC IO_SCH_ISC /* inherit from subchannel */
137 #define VIRTIO_IV_BITS (L1_CACHE_BYTES * 8)
138 #define MAX_AIRQ_AREAS 20
140 static int virtio_ccw_use_airq = 1;
144 u8 summary_indicator_idx;
145 struct airq_struct airq;
148 static struct airq_info *airq_areas[MAX_AIRQ_AREAS];
149 static DEFINE_MUTEX(airq_areas_lock);
151 static u8 *summary_indicators;
153 static inline u8 *get_summary_indicator(struct airq_info *info)
155 return summary_indicators + info->summary_indicator_idx;
158 #define CCW_CMD_SET_VQ 0x13
159 #define CCW_CMD_VDEV_RESET 0x33
160 #define CCW_CMD_SET_IND 0x43
161 #define CCW_CMD_SET_CONF_IND 0x53
162 #define CCW_CMD_READ_FEAT 0x12
163 #define CCW_CMD_WRITE_FEAT 0x11
164 #define CCW_CMD_READ_CONF 0x22
165 #define CCW_CMD_WRITE_CONF 0x21
166 #define CCW_CMD_WRITE_STATUS 0x31
167 #define CCW_CMD_READ_VQ_CONF 0x32
168 #define CCW_CMD_READ_STATUS 0x72
169 #define CCW_CMD_SET_IND_ADAPTER 0x73
170 #define CCW_CMD_SET_VIRTIO_REV 0x83
172 #define VIRTIO_CCW_DOING_SET_VQ 0x00010000
173 #define VIRTIO_CCW_DOING_RESET 0x00040000
174 #define VIRTIO_CCW_DOING_READ_FEAT 0x00080000
175 #define VIRTIO_CCW_DOING_WRITE_FEAT 0x00100000
176 #define VIRTIO_CCW_DOING_READ_CONFIG 0x00200000
177 #define VIRTIO_CCW_DOING_WRITE_CONFIG 0x00400000
178 #define VIRTIO_CCW_DOING_WRITE_STATUS 0x00800000
179 #define VIRTIO_CCW_DOING_SET_IND 0x01000000
180 #define VIRTIO_CCW_DOING_READ_VQ_CONF 0x02000000
181 #define VIRTIO_CCW_DOING_SET_CONF_IND 0x04000000
182 #define VIRTIO_CCW_DOING_SET_IND_ADAPTER 0x08000000
183 #define VIRTIO_CCW_DOING_SET_VIRTIO_REV 0x10000000
184 #define VIRTIO_CCW_DOING_READ_STATUS 0x20000000
185 #define VIRTIO_CCW_INTPARM_MASK 0xffff0000
187 static struct virtio_ccw_device *to_vc_device(struct virtio_device *vdev)
189 return container_of(vdev, struct virtio_ccw_device, vdev);
192 static void drop_airq_indicator(struct virtqueue *vq, struct airq_info *info)
194 unsigned long i, flags;
196 write_lock_irqsave(&info->lock, flags);
197 for (i = 0; i < airq_iv_end(info->aiv); i++) {
198 if (vq == (void *)airq_iv_get_ptr(info->aiv, i)) {
199 airq_iv_free_bit(info->aiv, i);
200 airq_iv_set_ptr(info->aiv, i, 0);
204 write_unlock_irqrestore(&info->lock, flags);
207 static void virtio_airq_handler(struct airq_struct *airq, bool floating)
209 struct airq_info *info = container_of(airq, struct airq_info, airq);
212 inc_irq_stat(IRQIO_VAI);
213 read_lock(&info->lock);
214 /* Walk through indicators field, summary indicator active. */
216 ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
219 vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
221 *(get_summary_indicator(info)) = 0;
223 /* Walk through indicators field, summary indicator not active. */
225 ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
228 vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
230 read_unlock(&info->lock);
233 static struct airq_info *new_airq_info(int index)
235 struct airq_info *info;
238 info = kzalloc(sizeof(*info), GFP_KERNEL);
241 rwlock_init(&info->lock);
242 info->aiv = airq_iv_create(VIRTIO_IV_BITS, AIRQ_IV_ALLOC | AIRQ_IV_PTR
243 | AIRQ_IV_CACHELINE);
248 info->airq.handler = virtio_airq_handler;
249 info->summary_indicator_idx = index;
250 info->airq.lsi_ptr = get_summary_indicator(info);
251 info->airq.lsi_mask = 0xff;
252 info->airq.isc = VIRTIO_AIRQ_ISC;
253 rc = register_adapter_interrupt(&info->airq);
255 airq_iv_release(info->aiv);
262 static unsigned long get_airq_indicator(struct virtqueue *vqs[], int nvqs,
263 u64 *first, void **airq_info)
266 struct airq_info *info;
267 unsigned long indicator_addr = 0;
268 unsigned long bit, flags;
270 for (i = 0; i < MAX_AIRQ_AREAS && !indicator_addr; i++) {
271 mutex_lock(&airq_areas_lock);
273 airq_areas[i] = new_airq_info(i);
274 info = airq_areas[i];
275 mutex_unlock(&airq_areas_lock);
278 write_lock_irqsave(&info->lock, flags);
279 bit = airq_iv_alloc(info->aiv, nvqs);
281 /* Not enough vacancies. */
282 write_unlock_irqrestore(&info->lock, flags);
287 indicator_addr = (unsigned long)info->aiv->vector;
288 for (j = 0; j < nvqs; j++) {
289 airq_iv_set_ptr(info->aiv, bit + j,
290 (unsigned long)vqs[j]);
292 write_unlock_irqrestore(&info->lock, flags);
294 return indicator_addr;
297 static void virtio_ccw_drop_indicators(struct virtio_ccw_device *vcdev)
299 struct virtio_ccw_vq_info *info;
301 if (!vcdev->airq_info)
303 list_for_each_entry(info, &vcdev->virtqueues, node)
304 drop_airq_indicator(info->vq, vcdev->airq_info);
307 static int doing_io(struct virtio_ccw_device *vcdev, __u32 flag)
312 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
316 ret = vcdev->curr_io & flag;
317 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
321 static int ccw_io_helper(struct virtio_ccw_device *vcdev,
322 struct ccw1 *ccw, __u32 intparm)
326 int flag = intparm & VIRTIO_CCW_INTPARM_MASK;
328 mutex_lock(&vcdev->io_lock);
330 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
331 ret = ccw_device_start(vcdev->cdev, ccw, intparm, 0, 0);
335 vcdev->curr_io |= flag;
337 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
339 } while (ret == -EBUSY);
340 wait_event(vcdev->wait_q, doing_io(vcdev, flag) == 0);
341 ret = ret ? ret : vcdev->err;
342 mutex_unlock(&vcdev->io_lock);
346 static void virtio_ccw_drop_indicator(struct virtio_ccw_device *vcdev,
350 unsigned long *indicatorp = NULL;
351 struct virtio_thinint_area *thinint_area = NULL;
352 struct airq_info *airq_info = vcdev->airq_info;
354 if (vcdev->is_thinint) {
355 thinint_area = ccw_device_dma_zalloc(vcdev->cdev,
356 sizeof(*thinint_area));
359 thinint_area->summary_indicator =
360 (unsigned long) get_summary_indicator(airq_info);
361 thinint_area->isc = VIRTIO_AIRQ_ISC;
362 ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
363 ccw->count = sizeof(*thinint_area);
364 ccw->cda = (__u32)(unsigned long) thinint_area;
366 /* payload is the address of the indicators */
367 indicatorp = ccw_device_dma_zalloc(vcdev->cdev,
368 sizeof(indicators(vcdev)));
372 ccw->cmd_code = CCW_CMD_SET_IND;
373 ccw->count = sizeof(indicators(vcdev));
374 ccw->cda = (__u32)(unsigned long) indicatorp;
376 /* Deregister indicators from host. */
377 *indicators(vcdev) = 0;
379 ret = ccw_io_helper(vcdev, ccw,
381 VIRTIO_CCW_DOING_SET_IND_ADAPTER :
382 VIRTIO_CCW_DOING_SET_IND);
383 if (ret && (ret != -ENODEV))
384 dev_info(&vcdev->cdev->dev,
385 "Failed to deregister indicators (%d)\n", ret);
386 else if (vcdev->is_thinint)
387 virtio_ccw_drop_indicators(vcdev);
388 ccw_device_dma_free(vcdev->cdev, indicatorp, sizeof(indicators(vcdev)));
389 ccw_device_dma_free(vcdev->cdev, thinint_area, sizeof(*thinint_area));
392 static bool virtio_ccw_kvm_notify(struct virtqueue *vq)
394 struct virtio_ccw_vq_info *info = vq->priv;
395 struct virtio_ccw_device *vcdev;
396 struct subchannel_id schid;
398 vcdev = to_vc_device(info->vq->vdev);
399 ccw_device_get_schid(vcdev->cdev, &schid);
400 BUILD_BUG_ON(sizeof(struct subchannel_id) != sizeof(unsigned int));
401 info->cookie = kvm_hypercall3(KVM_S390_VIRTIO_CCW_NOTIFY,
402 *((unsigned int *)&schid),
403 vq->index, info->cookie);
404 if (info->cookie < 0)
409 static int virtio_ccw_read_vq_conf(struct virtio_ccw_device *vcdev,
410 struct ccw1 *ccw, int index)
414 vcdev->dma_area->config_block.index = index;
415 ccw->cmd_code = CCW_CMD_READ_VQ_CONF;
417 ccw->count = sizeof(struct vq_config_block);
418 ccw->cda = (__u32)(unsigned long)(&vcdev->dma_area->config_block);
419 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_VQ_CONF);
422 return vcdev->dma_area->config_block.num ?: -ENOENT;
425 static void virtio_ccw_del_vq(struct virtqueue *vq, struct ccw1 *ccw)
427 struct virtio_ccw_device *vcdev = to_vc_device(vq->vdev);
428 struct virtio_ccw_vq_info *info = vq->priv;
431 unsigned int index = vq->index;
433 /* Remove from our list. */
434 spin_lock_irqsave(&vcdev->lock, flags);
435 list_del(&info->node);
436 spin_unlock_irqrestore(&vcdev->lock, flags);
438 /* Release from host. */
439 if (vcdev->revision == 0) {
440 info->info_block->l.queue = 0;
441 info->info_block->l.align = 0;
442 info->info_block->l.index = index;
443 info->info_block->l.num = 0;
444 ccw->count = sizeof(info->info_block->l);
446 info->info_block->s.desc = 0;
447 info->info_block->s.index = index;
448 info->info_block->s.num = 0;
449 info->info_block->s.avail = 0;
450 info->info_block->s.used = 0;
451 ccw->count = sizeof(info->info_block->s);
453 ccw->cmd_code = CCW_CMD_SET_VQ;
455 ccw->cda = (__u32)(unsigned long)(info->info_block);
456 ret = ccw_io_helper(vcdev, ccw,
457 VIRTIO_CCW_DOING_SET_VQ | index);
459 * -ENODEV isn't considered an error: The device is gone anyway.
460 * This may happen on device detach.
462 if (ret && (ret != -ENODEV))
463 dev_warn(&vq->vdev->dev, "Error %d while deleting queue %d\n",
466 vring_del_virtqueue(vq);
467 ccw_device_dma_free(vcdev->cdev, info->info_block,
468 sizeof(*info->info_block));
472 static void virtio_ccw_del_vqs(struct virtio_device *vdev)
474 struct virtqueue *vq, *n;
476 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
478 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
482 virtio_ccw_drop_indicator(vcdev, ccw);
484 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
485 virtio_ccw_del_vq(vq, ccw);
487 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
490 static struct virtqueue *virtio_ccw_setup_vq(struct virtio_device *vdev,
491 int i, vq_callback_t *callback,
492 const char *name, bool ctx,
495 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
497 struct virtqueue *vq = NULL;
498 struct virtio_ccw_vq_info *info;
503 /* Allocate queue. */
504 info = kzalloc(sizeof(struct virtio_ccw_vq_info), GFP_KERNEL);
506 dev_warn(&vcdev->cdev->dev, "no info\n");
510 info->info_block = ccw_device_dma_zalloc(vcdev->cdev,
511 sizeof(*info->info_block));
512 if (!info->info_block) {
513 dev_warn(&vcdev->cdev->dev, "no info block\n");
517 info->num = virtio_ccw_read_vq_conf(vcdev, ccw, i);
522 may_reduce = vcdev->revision > 0;
523 vq = vring_create_virtqueue(i, info->num, KVM_VIRTIO_CCW_RING_ALIGN,
524 vdev, true, may_reduce, ctx,
525 virtio_ccw_kvm_notify, callback, name);
528 /* For now, we fail if we can't get the requested size. */
529 dev_warn(&vcdev->cdev->dev, "no vq\n");
533 /* it may have been reduced */
534 info->num = virtqueue_get_vring_size(vq);
536 /* Register it with the host. */
537 queue = virtqueue_get_desc_addr(vq);
538 if (vcdev->revision == 0) {
539 info->info_block->l.queue = queue;
540 info->info_block->l.align = KVM_VIRTIO_CCW_RING_ALIGN;
541 info->info_block->l.index = i;
542 info->info_block->l.num = info->num;
543 ccw->count = sizeof(info->info_block->l);
545 info->info_block->s.desc = queue;
546 info->info_block->s.index = i;
547 info->info_block->s.num = info->num;
548 info->info_block->s.avail = (__u64)virtqueue_get_avail_addr(vq);
549 info->info_block->s.used = (__u64)virtqueue_get_used_addr(vq);
550 ccw->count = sizeof(info->info_block->s);
552 ccw->cmd_code = CCW_CMD_SET_VQ;
554 ccw->cda = (__u32)(unsigned long)(info->info_block);
555 err = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_VQ | i);
557 dev_warn(&vcdev->cdev->dev, "SET_VQ failed\n");
564 /* Save it to our list. */
565 spin_lock_irqsave(&vcdev->lock, flags);
566 list_add(&info->node, &vcdev->virtqueues);
567 spin_unlock_irqrestore(&vcdev->lock, flags);
573 vring_del_virtqueue(vq);
575 ccw_device_dma_free(vcdev->cdev, info->info_block,
576 sizeof(*info->info_block));
582 static int virtio_ccw_register_adapter_ind(struct virtio_ccw_device *vcdev,
583 struct virtqueue *vqs[], int nvqs,
587 struct virtio_thinint_area *thinint_area = NULL;
588 struct airq_info *info;
590 thinint_area = ccw_device_dma_zalloc(vcdev->cdev,
591 sizeof(*thinint_area));
596 /* Try to get an indicator. */
597 thinint_area->indicator = get_airq_indicator(vqs, nvqs,
598 &thinint_area->bit_nr,
600 if (!thinint_area->indicator) {
604 info = vcdev->airq_info;
605 thinint_area->summary_indicator =
606 (unsigned long) get_summary_indicator(info);
607 thinint_area->isc = VIRTIO_AIRQ_ISC;
608 ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
609 ccw->flags = CCW_FLAG_SLI;
610 ccw->count = sizeof(*thinint_area);
611 ccw->cda = (__u32)(unsigned long)thinint_area;
612 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND_ADAPTER);
614 if (ret == -EOPNOTSUPP) {
616 * The host does not support adapter interrupts
617 * for virtio-ccw, stop trying.
619 virtio_ccw_use_airq = 0;
620 pr_info("Adapter interrupts unsupported on host\n");
622 dev_warn(&vcdev->cdev->dev,
623 "enabling adapter interrupts = %d\n", ret);
624 virtio_ccw_drop_indicators(vcdev);
627 ccw_device_dma_free(vcdev->cdev, thinint_area, sizeof(*thinint_area));
631 static int virtio_ccw_find_vqs(struct virtio_device *vdev, unsigned nvqs,
632 struct virtqueue *vqs[],
633 vq_callback_t *callbacks[],
634 const char * const names[],
636 struct irq_affinity *desc)
638 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
639 unsigned long *indicatorp = NULL;
640 int ret, i, queue_idx = 0;
643 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
647 for (i = 0; i < nvqs; ++i) {
653 vqs[i] = virtio_ccw_setup_vq(vdev, queue_idx++, callbacks[i],
654 names[i], ctx ? ctx[i] : false,
656 if (IS_ERR(vqs[i])) {
657 ret = PTR_ERR(vqs[i]);
664 * We need a data area under 2G to communicate. Our payload is
665 * the address of the indicators.
667 indicatorp = ccw_device_dma_zalloc(vcdev->cdev,
668 sizeof(indicators(vcdev)));
671 *indicatorp = (unsigned long) indicators(vcdev);
672 if (vcdev->is_thinint) {
673 ret = virtio_ccw_register_adapter_ind(vcdev, vqs, nvqs, ccw);
675 /* no error, just fall back to legacy interrupts */
676 vcdev->is_thinint = false;
678 if (!vcdev->is_thinint) {
679 /* Register queue indicators with host. */
680 *indicators(vcdev) = 0;
681 ccw->cmd_code = CCW_CMD_SET_IND;
683 ccw->count = sizeof(indicators(vcdev));
684 ccw->cda = (__u32)(unsigned long) indicatorp;
685 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND);
689 /* Register indicators2 with host for config changes */
690 *indicatorp = (unsigned long) indicators2(vcdev);
691 *indicators2(vcdev) = 0;
692 ccw->cmd_code = CCW_CMD_SET_CONF_IND;
694 ccw->count = sizeof(indicators2(vcdev));
695 ccw->cda = (__u32)(unsigned long) indicatorp;
696 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_CONF_IND);
701 ccw_device_dma_free(vcdev->cdev, indicatorp,
702 sizeof(indicators(vcdev)));
703 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
707 ccw_device_dma_free(vcdev->cdev, indicatorp,
708 sizeof(indicators(vcdev)));
709 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
710 virtio_ccw_del_vqs(vdev);
714 static void virtio_ccw_reset(struct virtio_device *vdev)
716 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
719 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
723 /* Zero status bits. */
724 vcdev->dma_area->status = 0;
726 /* Send a reset ccw on device. */
727 ccw->cmd_code = CCW_CMD_VDEV_RESET;
731 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_RESET);
732 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
735 static u64 virtio_ccw_get_features(struct virtio_device *vdev)
737 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
738 struct virtio_feature_desc *features;
743 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
747 features = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*features));
752 /* Read the feature bits from the host. */
754 ccw->cmd_code = CCW_CMD_READ_FEAT;
756 ccw->count = sizeof(*features);
757 ccw->cda = (__u32)(unsigned long)features;
758 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
764 rc = le32_to_cpu(features->features);
766 if (vcdev->revision == 0)
769 /* Read second half of the feature bits from the host. */
771 ccw->cmd_code = CCW_CMD_READ_FEAT;
773 ccw->count = sizeof(*features);
774 ccw->cda = (__u32)(unsigned long)features;
775 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
777 rc |= (u64)le32_to_cpu(features->features) << 32;
780 ccw_device_dma_free(vcdev->cdev, features, sizeof(*features));
781 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
785 static void ccw_transport_features(struct virtio_device *vdev)
788 * Currently nothing to do here.
792 static int virtio_ccw_finalize_features(struct virtio_device *vdev)
794 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
795 struct virtio_feature_desc *features;
799 if (vcdev->revision >= 1 &&
800 !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
801 dev_err(&vdev->dev, "virtio: device uses revision 1 "
802 "but does not have VIRTIO_F_VERSION_1\n");
806 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
810 features = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*features));
815 /* Give virtio_ring a chance to accept features. */
816 vring_transport_features(vdev);
818 /* Give virtio_ccw a chance to accept features. */
819 ccw_transport_features(vdev);
822 features->features = cpu_to_le32((u32)vdev->features);
823 /* Write the first half of the feature bits to the host. */
824 ccw->cmd_code = CCW_CMD_WRITE_FEAT;
826 ccw->count = sizeof(*features);
827 ccw->cda = (__u32)(unsigned long)features;
828 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
832 if (vcdev->revision == 0)
836 features->features = cpu_to_le32(vdev->features >> 32);
837 /* Write the second half of the feature bits to the host. */
838 ccw->cmd_code = CCW_CMD_WRITE_FEAT;
840 ccw->count = sizeof(*features);
841 ccw->cda = (__u32)(unsigned long)features;
842 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
845 ccw_device_dma_free(vcdev->cdev, features, sizeof(*features));
846 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
851 static void virtio_ccw_get_config(struct virtio_device *vdev,
852 unsigned int offset, void *buf, unsigned len)
854 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
860 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
864 config_area = ccw_device_dma_zalloc(vcdev->cdev,
865 VIRTIO_CCW_CONFIG_SIZE);
869 /* Read the config area from the host. */
870 ccw->cmd_code = CCW_CMD_READ_CONF;
872 ccw->count = offset + len;
873 ccw->cda = (__u32)(unsigned long)config_area;
874 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG);
878 spin_lock_irqsave(&vcdev->lock, flags);
879 memcpy(vcdev->config, config_area, offset + len);
880 if (vcdev->config_ready < offset + len)
881 vcdev->config_ready = offset + len;
882 spin_unlock_irqrestore(&vcdev->lock, flags);
884 memcpy(buf, config_area + offset, len);
887 ccw_device_dma_free(vcdev->cdev, config_area, VIRTIO_CCW_CONFIG_SIZE);
888 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
891 static void virtio_ccw_set_config(struct virtio_device *vdev,
892 unsigned int offset, const void *buf,
895 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
900 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
904 config_area = ccw_device_dma_zalloc(vcdev->cdev,
905 VIRTIO_CCW_CONFIG_SIZE);
909 /* Make sure we don't overwrite fields. */
910 if (vcdev->config_ready < offset)
911 virtio_ccw_get_config(vdev, 0, NULL, offset);
912 spin_lock_irqsave(&vcdev->lock, flags);
913 memcpy(&vcdev->config[offset], buf, len);
914 /* Write the config area to the host. */
915 memcpy(config_area, vcdev->config, sizeof(vcdev->config));
916 spin_unlock_irqrestore(&vcdev->lock, flags);
917 ccw->cmd_code = CCW_CMD_WRITE_CONF;
919 ccw->count = offset + len;
920 ccw->cda = (__u32)(unsigned long)config_area;
921 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG);
924 ccw_device_dma_free(vcdev->cdev, config_area, VIRTIO_CCW_CONFIG_SIZE);
925 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
928 static u8 virtio_ccw_get_status(struct virtio_device *vdev)
930 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
931 u8 old_status = vcdev->dma_area->status;
934 if (vcdev->revision < 2)
935 return vcdev->dma_area->status;
937 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
941 ccw->cmd_code = CCW_CMD_READ_STATUS;
943 ccw->count = sizeof(vcdev->dma_area->status);
944 ccw->cda = (__u32)(unsigned long)&vcdev->dma_area->status;
945 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_STATUS);
947 * If the channel program failed (should only happen if the device
948 * was hotunplugged, and then we clean up via the machine check
949 * handler anyway), vcdev->dma_area->status was not overwritten and we just
950 * return the old status, which is fine.
952 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
954 return vcdev->dma_area->status;
957 static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status)
959 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
960 u8 old_status = vcdev->dma_area->status;
964 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
968 /* Write the status to the host. */
969 vcdev->dma_area->status = status;
970 ccw->cmd_code = CCW_CMD_WRITE_STATUS;
972 ccw->count = sizeof(status);
973 ccw->cda = (__u32)(unsigned long)&vcdev->dma_area->status;
974 /* We use ssch for setting the status which is a serializing
975 * instruction that guarantees the memory writes have
976 * completed before ssch.
978 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS);
979 /* Write failed? We assume status is unchanged. */
981 vcdev->dma_area->status = old_status;
982 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
985 static const char *virtio_ccw_bus_name(struct virtio_device *vdev)
987 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
989 return dev_name(&vcdev->cdev->dev);
992 static void virtio_ccw_synchronize_cbs(struct virtio_device *vdev)
994 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
995 struct airq_info *info = vcdev->airq_info;
999 * This device uses adapter interrupts: synchronize with
1000 * vring_interrupt() called by virtio_airq_handler()
1001 * via the indicator area lock.
1003 write_lock_irq(&info->lock);
1004 write_unlock_irq(&info->lock);
1006 /* This device uses classic interrupts: synchronize
1007 * with vring_interrupt() called by
1008 * virtio_ccw_int_handler() via the per-device
1011 write_lock_irq(&vcdev->irq_lock);
1012 write_unlock_irq(&vcdev->irq_lock);
1016 static const struct virtio_config_ops virtio_ccw_config_ops = {
1017 .get_features = virtio_ccw_get_features,
1018 .finalize_features = virtio_ccw_finalize_features,
1019 .get = virtio_ccw_get_config,
1020 .set = virtio_ccw_set_config,
1021 .get_status = virtio_ccw_get_status,
1022 .set_status = virtio_ccw_set_status,
1023 .reset = virtio_ccw_reset,
1024 .find_vqs = virtio_ccw_find_vqs,
1025 .del_vqs = virtio_ccw_del_vqs,
1026 .bus_name = virtio_ccw_bus_name,
1027 .synchronize_cbs = virtio_ccw_synchronize_cbs,
1032 * ccw bus driver related functions
1035 static void virtio_ccw_release_dev(struct device *_d)
1037 struct virtio_device *dev = dev_to_virtio(_d);
1038 struct virtio_ccw_device *vcdev = to_vc_device(dev);
1040 ccw_device_dma_free(vcdev->cdev, vcdev->dma_area,
1041 sizeof(*vcdev->dma_area));
1045 static int irb_is_error(struct irb *irb)
1047 if (scsw_cstat(&irb->scsw) != 0)
1049 if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
1051 if (scsw_cc(&irb->scsw) != 0)
1056 static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev,
1059 struct virtio_ccw_vq_info *info;
1060 unsigned long flags;
1061 struct virtqueue *vq;
1064 spin_lock_irqsave(&vcdev->lock, flags);
1065 list_for_each_entry(info, &vcdev->virtqueues, node) {
1066 if (info->vq->index == index) {
1071 spin_unlock_irqrestore(&vcdev->lock, flags);
1075 static void virtio_ccw_check_activity(struct virtio_ccw_device *vcdev,
1078 if (vcdev->curr_io & activity) {
1080 case VIRTIO_CCW_DOING_READ_FEAT:
1081 case VIRTIO_CCW_DOING_WRITE_FEAT:
1082 case VIRTIO_CCW_DOING_READ_CONFIG:
1083 case VIRTIO_CCW_DOING_WRITE_CONFIG:
1084 case VIRTIO_CCW_DOING_WRITE_STATUS:
1085 case VIRTIO_CCW_DOING_READ_STATUS:
1086 case VIRTIO_CCW_DOING_SET_VQ:
1087 case VIRTIO_CCW_DOING_SET_IND:
1088 case VIRTIO_CCW_DOING_SET_CONF_IND:
1089 case VIRTIO_CCW_DOING_RESET:
1090 case VIRTIO_CCW_DOING_READ_VQ_CONF:
1091 case VIRTIO_CCW_DOING_SET_IND_ADAPTER:
1092 case VIRTIO_CCW_DOING_SET_VIRTIO_REV:
1093 vcdev->curr_io &= ~activity;
1094 wake_up(&vcdev->wait_q);
1097 /* don't know what to do... */
1098 dev_warn(&vcdev->cdev->dev,
1099 "Suspicious activity '%08x'\n", activity);
1106 static void virtio_ccw_int_handler(struct ccw_device *cdev,
1107 unsigned long intparm,
1110 __u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK;
1111 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1113 struct virtqueue *vq;
1118 vcdev->err = PTR_ERR(irb);
1119 virtio_ccw_check_activity(vcdev, activity);
1120 /* Don't poke around indicators, something's wrong. */
1123 /* Check if it's a notification from the host. */
1124 if ((intparm == 0) &&
1125 (scsw_stctl(&irb->scsw) ==
1126 (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) {
1129 if (irb_is_error(irb)) {
1130 /* Command reject? */
1131 if ((scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) &&
1132 (irb->ecw[0] & SNS0_CMD_REJECT))
1133 vcdev->err = -EOPNOTSUPP;
1135 /* Map everything else to -EIO. */
1138 virtio_ccw_check_activity(vcdev, activity);
1139 #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
1141 * Paired with virtio_ccw_synchronize_cbs() and interrupts are
1144 read_lock(&vcdev->irq_lock);
1146 for_each_set_bit(i, indicators(vcdev),
1147 sizeof(*indicators(vcdev)) * BITS_PER_BYTE) {
1148 /* The bit clear must happen before the vring kick. */
1149 clear_bit(i, indicators(vcdev));
1151 vq = virtio_ccw_vq_by_ind(vcdev, i);
1152 vring_interrupt(0, vq);
1154 #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
1155 read_unlock(&vcdev->irq_lock);
1157 if (test_bit(0, indicators2(vcdev))) {
1158 virtio_config_changed(&vcdev->vdev);
1159 clear_bit(0, indicators2(vcdev));
1164 * We usually want to autoonline all devices, but give the admin
1165 * a way to exempt devices from this.
1167 #define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
1169 static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS];
1171 static char *no_auto = "";
1173 module_param(no_auto, charp, 0444);
1174 MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined");
1176 static int virtio_ccw_check_autoonline(struct ccw_device *cdev)
1178 struct ccw_dev_id id;
1180 ccw_device_get_id(cdev, &id);
1181 if (test_bit(id.devno, devs_no_auto[id.ssid]))
1186 static void virtio_ccw_auto_online(void *data, async_cookie_t cookie)
1188 struct ccw_device *cdev = data;
1191 ret = ccw_device_set_online(cdev);
1193 dev_warn(&cdev->dev, "Failed to set online: %d\n", ret);
1196 static int virtio_ccw_probe(struct ccw_device *cdev)
1198 cdev->handler = virtio_ccw_int_handler;
1200 if (virtio_ccw_check_autoonline(cdev))
1201 async_schedule(virtio_ccw_auto_online, cdev);
1205 static struct virtio_ccw_device *virtio_grab_drvdata(struct ccw_device *cdev)
1207 unsigned long flags;
1208 struct virtio_ccw_device *vcdev;
1210 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1211 vcdev = dev_get_drvdata(&cdev->dev);
1212 if (!vcdev || vcdev->going_away) {
1213 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1216 vcdev->going_away = true;
1217 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1221 static void virtio_ccw_remove(struct ccw_device *cdev)
1223 unsigned long flags;
1224 struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
1226 if (vcdev && cdev->online) {
1227 if (vcdev->device_lost)
1228 virtio_break_device(&vcdev->vdev);
1229 unregister_virtio_device(&vcdev->vdev);
1230 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1231 dev_set_drvdata(&cdev->dev, NULL);
1232 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1234 cdev->handler = NULL;
1237 static int virtio_ccw_offline(struct ccw_device *cdev)
1239 unsigned long flags;
1240 struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
1244 if (vcdev->device_lost)
1245 virtio_break_device(&vcdev->vdev);
1246 unregister_virtio_device(&vcdev->vdev);
1247 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1248 dev_set_drvdata(&cdev->dev, NULL);
1249 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1253 static int virtio_ccw_set_transport_rev(struct virtio_ccw_device *vcdev)
1255 struct virtio_rev_info *rev;
1259 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
1262 rev = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*rev));
1264 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
1268 /* Set transport revision */
1269 ccw->cmd_code = CCW_CMD_SET_VIRTIO_REV;
1271 ccw->count = sizeof(*rev);
1272 ccw->cda = (__u32)(unsigned long)rev;
1274 vcdev->revision = VIRTIO_CCW_REV_MAX;
1276 rev->revision = vcdev->revision;
1277 /* none of our supported revisions carry payload */
1279 ret = ccw_io_helper(vcdev, ccw,
1280 VIRTIO_CCW_DOING_SET_VIRTIO_REV);
1281 if (ret == -EOPNOTSUPP) {
1282 if (vcdev->revision == 0)
1284 * The host device does not support setting
1285 * the revision: let's operate it in legacy
1292 } while (ret == -EOPNOTSUPP);
1294 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
1295 ccw_device_dma_free(vcdev->cdev, rev, sizeof(*rev));
1299 static int virtio_ccw_online(struct ccw_device *cdev)
1302 struct virtio_ccw_device *vcdev;
1303 unsigned long flags;
1305 vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL);
1307 dev_warn(&cdev->dev, "Could not get memory for virtio\n");
1311 vcdev->vdev.dev.parent = &cdev->dev;
1313 vcdev->dma_area = ccw_device_dma_zalloc(vcdev->cdev,
1314 sizeof(*vcdev->dma_area));
1315 if (!vcdev->dma_area) {
1320 vcdev->is_thinint = virtio_ccw_use_airq; /* at least try */
1322 vcdev->vdev.dev.release = virtio_ccw_release_dev;
1323 vcdev->vdev.config = &virtio_ccw_config_ops;
1324 init_waitqueue_head(&vcdev->wait_q);
1325 INIT_LIST_HEAD(&vcdev->virtqueues);
1326 spin_lock_init(&vcdev->lock);
1327 rwlock_init(&vcdev->irq_lock);
1328 mutex_init(&vcdev->io_lock);
1330 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1331 dev_set_drvdata(&cdev->dev, vcdev);
1332 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1333 vcdev->vdev.id.vendor = cdev->id.cu_type;
1334 vcdev->vdev.id.device = cdev->id.cu_model;
1336 ret = virtio_ccw_set_transport_rev(vcdev);
1340 ret = register_virtio_device(&vcdev->vdev);
1342 dev_warn(&cdev->dev, "Failed to register virtio device: %d\n",
1348 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1349 dev_set_drvdata(&cdev->dev, NULL);
1350 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1351 put_device(&vcdev->vdev.dev);
1355 ccw_device_dma_free(vcdev->cdev, vcdev->dma_area,
1356 sizeof(*vcdev->dma_area));
1362 static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event)
1365 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1368 * Make sure vcdev is set
1369 * i.e. set_offline/remove callback not already running
1376 vcdev->device_lost = true;
1389 static struct ccw_device_id virtio_ids[] = {
1390 { CCW_DEVICE(0x3832, 0) },
1394 static struct ccw_driver virtio_ccw_driver = {
1396 .owner = THIS_MODULE,
1397 .name = "virtio_ccw",
1400 .probe = virtio_ccw_probe,
1401 .remove = virtio_ccw_remove,
1402 .set_offline = virtio_ccw_offline,
1403 .set_online = virtio_ccw_online,
1404 .notify = virtio_ccw_cio_notify,
1405 .int_class = IRQIO_VIR,
1408 static int __init pure_hex(char **cp, unsigned int *val, int min_digit,
1409 int max_digit, int max_val)
1416 while (diff <= max_digit) {
1417 int value = hex_to_bin(**cp);
1421 *val = *val * 16 + value;
1426 if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
1432 static int __init parse_busid(char *str, unsigned int *cssid,
1433 unsigned int *ssid, unsigned int *devno)
1444 ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
1445 if (ret || (str_work[0] != '.'))
1448 ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
1449 if (ret || (str_work[0] != '.'))
1452 ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
1453 if (ret || (str_work[0] != '\0'))
1461 static void __init no_auto_parse(void)
1463 unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
1468 while ((parm = strsep(&str, ","))) {
1469 rc = parse_busid(strsep(&parm, "-"), &from_cssid,
1474 rc = parse_busid(parm, &to_cssid,
1476 if ((from_ssid > to_ssid) ||
1477 ((from_ssid == to_ssid) && (from > to)))
1480 to_cssid = from_cssid;
1481 to_ssid = from_ssid;
1486 while ((from_ssid < to_ssid) ||
1487 ((from_ssid == to_ssid) && (from <= to))) {
1488 set_bit(from, devs_no_auto[from_ssid]);
1490 if (from > __MAX_SUBCHANNEL) {
1498 static int __init virtio_ccw_init(void)
1502 /* parse no_auto string before we do anything further */
1505 summary_indicators = cio_dma_zalloc(MAX_AIRQ_AREAS);
1506 if (!summary_indicators)
1508 rc = ccw_driver_register(&virtio_ccw_driver);
1510 cio_dma_free(summary_indicators, MAX_AIRQ_AREAS);
1513 device_initcall(virtio_ccw_init);