1 // SPDX-License-Identifier: GPL-2.0
3 * ccw based virtio transport
5 * Copyright IBM Corp. 2012, 2014
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>
39 * virtio related functions
42 struct vq_config_block {
47 #define VIRTIO_CCW_CONFIG_SIZE 0x100
48 /* same as PCI config space size, should be enough for all drivers */
50 struct vcdev_dma_area {
51 unsigned long indicators;
52 unsigned long indicators2;
53 struct vq_config_block config_block;
57 struct virtio_ccw_device {
58 struct virtio_device vdev;
59 __u8 config[VIRTIO_CCW_CONFIG_SIZE];
60 struct ccw_device *cdev;
63 unsigned int revision; /* Transport revision */
64 wait_queue_head_t wait_q;
67 struct mutex io_lock; /* Serializes I/O requests */
68 struct list_head virtqueues;
72 unsigned int config_ready;
74 struct vcdev_dma_area *dma_area;
77 static inline unsigned long *indicators(struct virtio_ccw_device *vcdev)
79 return &vcdev->dma_area->indicators;
82 static inline unsigned long *indicators2(struct virtio_ccw_device *vcdev)
84 return &vcdev->dma_area->indicators2;
87 struct vq_info_block_legacy {
94 struct vq_info_block {
103 struct virtio_feature_desc {
108 struct virtio_thinint_area {
109 unsigned long summary_indicator;
110 unsigned long indicator;
115 struct virtio_rev_info {
121 /* the highest virtio-ccw revision we support */
122 #define VIRTIO_CCW_REV_MAX 2
124 struct virtio_ccw_vq_info {
125 struct virtqueue *vq;
128 struct vq_info_block s;
129 struct vq_info_block_legacy l;
132 struct list_head node;
136 #define VIRTIO_AIRQ_ISC IO_SCH_ISC /* inherit from subchannel */
138 #define VIRTIO_IV_BITS (L1_CACHE_BYTES * 8)
139 #define MAX_AIRQ_AREAS 20
141 static int virtio_ccw_use_airq = 1;
145 u8 summary_indicator_idx;
146 struct airq_struct airq;
149 static struct airq_info *airq_areas[MAX_AIRQ_AREAS];
150 static DEFINE_MUTEX(airq_areas_lock);
152 static u8 *summary_indicators;
154 static inline u8 *get_summary_indicator(struct airq_info *info)
156 return summary_indicators + info->summary_indicator_idx;
159 #define CCW_CMD_SET_VQ 0x13
160 #define CCW_CMD_VDEV_RESET 0x33
161 #define CCW_CMD_SET_IND 0x43
162 #define CCW_CMD_SET_CONF_IND 0x53
163 #define CCW_CMD_READ_FEAT 0x12
164 #define CCW_CMD_WRITE_FEAT 0x11
165 #define CCW_CMD_READ_CONF 0x22
166 #define CCW_CMD_WRITE_CONF 0x21
167 #define CCW_CMD_WRITE_STATUS 0x31
168 #define CCW_CMD_READ_VQ_CONF 0x32
169 #define CCW_CMD_READ_STATUS 0x72
170 #define CCW_CMD_SET_IND_ADAPTER 0x73
171 #define CCW_CMD_SET_VIRTIO_REV 0x83
173 #define VIRTIO_CCW_DOING_SET_VQ 0x00010000
174 #define VIRTIO_CCW_DOING_RESET 0x00040000
175 #define VIRTIO_CCW_DOING_READ_FEAT 0x00080000
176 #define VIRTIO_CCW_DOING_WRITE_FEAT 0x00100000
177 #define VIRTIO_CCW_DOING_READ_CONFIG 0x00200000
178 #define VIRTIO_CCW_DOING_WRITE_CONFIG 0x00400000
179 #define VIRTIO_CCW_DOING_WRITE_STATUS 0x00800000
180 #define VIRTIO_CCW_DOING_SET_IND 0x01000000
181 #define VIRTIO_CCW_DOING_READ_VQ_CONF 0x02000000
182 #define VIRTIO_CCW_DOING_SET_CONF_IND 0x04000000
183 #define VIRTIO_CCW_DOING_SET_IND_ADAPTER 0x08000000
184 #define VIRTIO_CCW_DOING_SET_VIRTIO_REV 0x10000000
185 #define VIRTIO_CCW_DOING_READ_STATUS 0x20000000
186 #define VIRTIO_CCW_INTPARM_MASK 0xffff0000
188 static struct virtio_ccw_device *to_vc_device(struct virtio_device *vdev)
190 return container_of(vdev, struct virtio_ccw_device, vdev);
193 static void drop_airq_indicator(struct virtqueue *vq, struct airq_info *info)
195 unsigned long i, flags;
197 write_lock_irqsave(&info->lock, flags);
198 for (i = 0; i < airq_iv_end(info->aiv); i++) {
199 if (vq == (void *)airq_iv_get_ptr(info->aiv, i)) {
200 airq_iv_free_bit(info->aiv, i);
201 airq_iv_set_ptr(info->aiv, i, 0);
205 write_unlock_irqrestore(&info->lock, flags);
208 static void virtio_airq_handler(struct airq_struct *airq,
209 struct tpi_info *tpi_info)
211 struct airq_info *info = container_of(airq, struct airq_info, airq);
214 inc_irq_stat(IRQIO_VAI);
215 read_lock(&info->lock);
216 /* Walk through indicators field, summary indicator active. */
218 ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
221 vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
223 *(get_summary_indicator(info)) = 0;
225 /* Walk through indicators field, summary indicator not active. */
227 ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
230 vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
232 read_unlock(&info->lock);
235 static struct airq_info *new_airq_info(int index)
237 struct airq_info *info;
240 info = kzalloc(sizeof(*info), GFP_KERNEL);
243 rwlock_init(&info->lock);
244 info->aiv = airq_iv_create(VIRTIO_IV_BITS, AIRQ_IV_ALLOC | AIRQ_IV_PTR
245 | AIRQ_IV_CACHELINE, NULL);
250 info->airq.handler = virtio_airq_handler;
251 info->summary_indicator_idx = index;
252 info->airq.lsi_ptr = get_summary_indicator(info);
253 info->airq.lsi_mask = 0xff;
254 info->airq.isc = VIRTIO_AIRQ_ISC;
255 rc = register_adapter_interrupt(&info->airq);
257 airq_iv_release(info->aiv);
264 static unsigned long get_airq_indicator(struct virtqueue *vqs[], int nvqs,
265 u64 *first, void **airq_info)
268 struct airq_info *info;
269 unsigned long indicator_addr = 0;
270 unsigned long bit, flags;
272 for (i = 0; i < MAX_AIRQ_AREAS && !indicator_addr; i++) {
273 mutex_lock(&airq_areas_lock);
275 airq_areas[i] = new_airq_info(i);
276 info = airq_areas[i];
277 mutex_unlock(&airq_areas_lock);
280 write_lock_irqsave(&info->lock, flags);
281 bit = airq_iv_alloc(info->aiv, nvqs);
283 /* Not enough vacancies. */
284 write_unlock_irqrestore(&info->lock, flags);
289 indicator_addr = (unsigned long)info->aiv->vector;
290 for (j = 0; j < nvqs; j++) {
291 airq_iv_set_ptr(info->aiv, bit + j,
292 (unsigned long)vqs[j]);
294 write_unlock_irqrestore(&info->lock, flags);
296 return indicator_addr;
299 static void virtio_ccw_drop_indicators(struct virtio_ccw_device *vcdev)
301 struct virtio_ccw_vq_info *info;
303 if (!vcdev->airq_info)
305 list_for_each_entry(info, &vcdev->virtqueues, node)
306 drop_airq_indicator(info->vq, vcdev->airq_info);
309 static int doing_io(struct virtio_ccw_device *vcdev, __u32 flag)
314 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
318 ret = vcdev->curr_io & flag;
319 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
323 static int ccw_io_helper(struct virtio_ccw_device *vcdev,
324 struct ccw1 *ccw, __u32 intparm)
328 int flag = intparm & VIRTIO_CCW_INTPARM_MASK;
330 mutex_lock(&vcdev->io_lock);
332 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
333 ret = ccw_device_start(vcdev->cdev, ccw, intparm, 0, 0);
337 vcdev->curr_io |= flag;
339 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
341 } while (ret == -EBUSY);
342 wait_event(vcdev->wait_q, doing_io(vcdev, flag) == 0);
343 ret = ret ? ret : vcdev->err;
344 mutex_unlock(&vcdev->io_lock);
348 static void virtio_ccw_drop_indicator(struct virtio_ccw_device *vcdev,
352 unsigned long *indicatorp = NULL;
353 struct virtio_thinint_area *thinint_area = NULL;
354 struct airq_info *airq_info = vcdev->airq_info;
356 if (vcdev->is_thinint) {
357 thinint_area = ccw_device_dma_zalloc(vcdev->cdev,
358 sizeof(*thinint_area));
361 thinint_area->summary_indicator =
362 (unsigned long) get_summary_indicator(airq_info);
363 thinint_area->isc = VIRTIO_AIRQ_ISC;
364 ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
365 ccw->count = sizeof(*thinint_area);
366 ccw->cda = (__u32)(unsigned long) thinint_area;
368 /* payload is the address of the indicators */
369 indicatorp = ccw_device_dma_zalloc(vcdev->cdev,
370 sizeof(indicators(vcdev)));
374 ccw->cmd_code = CCW_CMD_SET_IND;
375 ccw->count = sizeof(indicators(vcdev));
376 ccw->cda = (__u32)(unsigned long) indicatorp;
378 /* Deregister indicators from host. */
379 *indicators(vcdev) = 0;
381 ret = ccw_io_helper(vcdev, ccw,
383 VIRTIO_CCW_DOING_SET_IND_ADAPTER :
384 VIRTIO_CCW_DOING_SET_IND);
385 if (ret && (ret != -ENODEV))
386 dev_info(&vcdev->cdev->dev,
387 "Failed to deregister indicators (%d)\n", ret);
388 else if (vcdev->is_thinint)
389 virtio_ccw_drop_indicators(vcdev);
390 ccw_device_dma_free(vcdev->cdev, indicatorp, sizeof(indicators(vcdev)));
391 ccw_device_dma_free(vcdev->cdev, thinint_area, sizeof(*thinint_area));
394 static bool virtio_ccw_kvm_notify(struct virtqueue *vq)
396 struct virtio_ccw_vq_info *info = vq->priv;
397 struct virtio_ccw_device *vcdev;
398 struct subchannel_id schid;
400 vcdev = to_vc_device(info->vq->vdev);
401 ccw_device_get_schid(vcdev->cdev, &schid);
402 BUILD_BUG_ON(sizeof(struct subchannel_id) != sizeof(unsigned int));
403 info->cookie = kvm_hypercall3(KVM_S390_VIRTIO_CCW_NOTIFY,
404 *((unsigned int *)&schid),
405 vq->index, info->cookie);
406 if (info->cookie < 0)
411 static int virtio_ccw_read_vq_conf(struct virtio_ccw_device *vcdev,
412 struct ccw1 *ccw, int index)
416 vcdev->dma_area->config_block.index = index;
417 ccw->cmd_code = CCW_CMD_READ_VQ_CONF;
419 ccw->count = sizeof(struct vq_config_block);
420 ccw->cda = (__u32)(unsigned long)(&vcdev->dma_area->config_block);
421 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_VQ_CONF);
424 return vcdev->dma_area->config_block.num ?: -ENOENT;
427 static void virtio_ccw_del_vq(struct virtqueue *vq, struct ccw1 *ccw)
429 struct virtio_ccw_device *vcdev = to_vc_device(vq->vdev);
430 struct virtio_ccw_vq_info *info = vq->priv;
433 unsigned int index = vq->index;
435 /* Remove from our list. */
436 spin_lock_irqsave(&vcdev->lock, flags);
437 list_del(&info->node);
438 spin_unlock_irqrestore(&vcdev->lock, flags);
440 /* Release from host. */
441 if (vcdev->revision == 0) {
442 info->info_block->l.queue = 0;
443 info->info_block->l.align = 0;
444 info->info_block->l.index = index;
445 info->info_block->l.num = 0;
446 ccw->count = sizeof(info->info_block->l);
448 info->info_block->s.desc = 0;
449 info->info_block->s.index = index;
450 info->info_block->s.num = 0;
451 info->info_block->s.avail = 0;
452 info->info_block->s.used = 0;
453 ccw->count = sizeof(info->info_block->s);
455 ccw->cmd_code = CCW_CMD_SET_VQ;
457 ccw->cda = (__u32)(unsigned long)(info->info_block);
458 ret = ccw_io_helper(vcdev, ccw,
459 VIRTIO_CCW_DOING_SET_VQ | index);
461 * -ENODEV isn't considered an error: The device is gone anyway.
462 * This may happen on device detach.
464 if (ret && (ret != -ENODEV))
465 dev_warn(&vq->vdev->dev, "Error %d while deleting queue %d\n",
468 vring_del_virtqueue(vq);
469 ccw_device_dma_free(vcdev->cdev, info->info_block,
470 sizeof(*info->info_block));
474 static void virtio_ccw_del_vqs(struct virtio_device *vdev)
476 struct virtqueue *vq, *n;
478 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
480 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
484 virtio_ccw_drop_indicator(vcdev, ccw);
486 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
487 virtio_ccw_del_vq(vq, ccw);
489 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
492 static struct virtqueue *virtio_ccw_setup_vq(struct virtio_device *vdev,
493 int i, vq_callback_t *callback,
494 const char *name, bool ctx,
497 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
499 struct virtqueue *vq = NULL;
500 struct virtio_ccw_vq_info *info;
505 /* Allocate queue. */
506 info = kzalloc(sizeof(struct virtio_ccw_vq_info), GFP_KERNEL);
508 dev_warn(&vcdev->cdev->dev, "no info\n");
512 info->info_block = ccw_device_dma_zalloc(vcdev->cdev,
513 sizeof(*info->info_block));
514 if (!info->info_block) {
515 dev_warn(&vcdev->cdev->dev, "no info block\n");
519 info->num = virtio_ccw_read_vq_conf(vcdev, ccw, i);
524 may_reduce = vcdev->revision > 0;
525 vq = vring_create_virtqueue(i, info->num, KVM_VIRTIO_CCW_RING_ALIGN,
526 vdev, true, may_reduce, ctx,
527 virtio_ccw_kvm_notify, callback, name);
530 /* For now, we fail if we can't get the requested size. */
531 dev_warn(&vcdev->cdev->dev, "no vq\n");
536 vq->num_max = info->num;
538 /* it may have been reduced */
539 info->num = virtqueue_get_vring_size(vq);
541 /* Register it with the host. */
542 queue = virtqueue_get_desc_addr(vq);
543 if (vcdev->revision == 0) {
544 info->info_block->l.queue = queue;
545 info->info_block->l.align = KVM_VIRTIO_CCW_RING_ALIGN;
546 info->info_block->l.index = i;
547 info->info_block->l.num = info->num;
548 ccw->count = sizeof(info->info_block->l);
550 info->info_block->s.desc = queue;
551 info->info_block->s.index = i;
552 info->info_block->s.num = info->num;
553 info->info_block->s.avail = (__u64)virtqueue_get_avail_addr(vq);
554 info->info_block->s.used = (__u64)virtqueue_get_used_addr(vq);
555 ccw->count = sizeof(info->info_block->s);
557 ccw->cmd_code = CCW_CMD_SET_VQ;
559 ccw->cda = (__u32)(unsigned long)(info->info_block);
560 err = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_VQ | i);
562 dev_warn(&vcdev->cdev->dev, "SET_VQ failed\n");
569 /* Save it to our list. */
570 spin_lock_irqsave(&vcdev->lock, flags);
571 list_add(&info->node, &vcdev->virtqueues);
572 spin_unlock_irqrestore(&vcdev->lock, flags);
578 vring_del_virtqueue(vq);
580 ccw_device_dma_free(vcdev->cdev, info->info_block,
581 sizeof(*info->info_block));
587 static int virtio_ccw_register_adapter_ind(struct virtio_ccw_device *vcdev,
588 struct virtqueue *vqs[], int nvqs,
592 struct virtio_thinint_area *thinint_area = NULL;
593 struct airq_info *info;
595 thinint_area = ccw_device_dma_zalloc(vcdev->cdev,
596 sizeof(*thinint_area));
601 /* Try to get an indicator. */
602 thinint_area->indicator = get_airq_indicator(vqs, nvqs,
603 &thinint_area->bit_nr,
605 if (!thinint_area->indicator) {
609 info = vcdev->airq_info;
610 thinint_area->summary_indicator =
611 (unsigned long) get_summary_indicator(info);
612 thinint_area->isc = VIRTIO_AIRQ_ISC;
613 ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
614 ccw->flags = CCW_FLAG_SLI;
615 ccw->count = sizeof(*thinint_area);
616 ccw->cda = (__u32)(unsigned long)thinint_area;
617 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND_ADAPTER);
619 if (ret == -EOPNOTSUPP) {
621 * The host does not support adapter interrupts
622 * for virtio-ccw, stop trying.
624 virtio_ccw_use_airq = 0;
625 pr_info("Adapter interrupts unsupported on host\n");
627 dev_warn(&vcdev->cdev->dev,
628 "enabling adapter interrupts = %d\n", ret);
629 virtio_ccw_drop_indicators(vcdev);
632 ccw_device_dma_free(vcdev->cdev, thinint_area, sizeof(*thinint_area));
636 static int virtio_ccw_find_vqs(struct virtio_device *vdev, unsigned nvqs,
637 struct virtqueue *vqs[],
638 vq_callback_t *callbacks[],
639 const char * const names[],
641 struct irq_affinity *desc)
643 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
644 unsigned long *indicatorp = NULL;
645 int ret, i, queue_idx = 0;
648 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
652 for (i = 0; i < nvqs; ++i) {
658 vqs[i] = virtio_ccw_setup_vq(vdev, queue_idx++, callbacks[i],
659 names[i], ctx ? ctx[i] : false,
661 if (IS_ERR(vqs[i])) {
662 ret = PTR_ERR(vqs[i]);
669 * We need a data area under 2G to communicate. Our payload is
670 * the address of the indicators.
672 indicatorp = ccw_device_dma_zalloc(vcdev->cdev,
673 sizeof(indicators(vcdev)));
676 *indicatorp = (unsigned long) indicators(vcdev);
677 if (vcdev->is_thinint) {
678 ret = virtio_ccw_register_adapter_ind(vcdev, vqs, nvqs, ccw);
680 /* no error, just fall back to legacy interrupts */
681 vcdev->is_thinint = false;
683 if (!vcdev->is_thinint) {
684 /* Register queue indicators with host. */
685 *indicators(vcdev) = 0;
686 ccw->cmd_code = CCW_CMD_SET_IND;
688 ccw->count = sizeof(indicators(vcdev));
689 ccw->cda = (__u32)(unsigned long) indicatorp;
690 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND);
694 /* Register indicators2 with host for config changes */
695 *indicatorp = (unsigned long) indicators2(vcdev);
696 *indicators2(vcdev) = 0;
697 ccw->cmd_code = CCW_CMD_SET_CONF_IND;
699 ccw->count = sizeof(indicators2(vcdev));
700 ccw->cda = (__u32)(unsigned long) indicatorp;
701 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_CONF_IND);
706 ccw_device_dma_free(vcdev->cdev, indicatorp,
707 sizeof(indicators(vcdev)));
708 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
712 ccw_device_dma_free(vcdev->cdev, indicatorp,
713 sizeof(indicators(vcdev)));
714 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
715 virtio_ccw_del_vqs(vdev);
719 static void virtio_ccw_reset(struct virtio_device *vdev)
721 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
724 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
728 /* Zero status bits. */
729 vcdev->dma_area->status = 0;
731 /* Send a reset ccw on device. */
732 ccw->cmd_code = CCW_CMD_VDEV_RESET;
736 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_RESET);
737 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
740 static u64 virtio_ccw_get_features(struct virtio_device *vdev)
742 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
743 struct virtio_feature_desc *features;
748 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
752 features = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*features));
757 /* Read the feature bits from the host. */
759 ccw->cmd_code = CCW_CMD_READ_FEAT;
761 ccw->count = sizeof(*features);
762 ccw->cda = (__u32)(unsigned long)features;
763 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
769 rc = le32_to_cpu(features->features);
771 if (vcdev->revision == 0)
774 /* Read second half of the feature bits from the host. */
776 ccw->cmd_code = CCW_CMD_READ_FEAT;
778 ccw->count = sizeof(*features);
779 ccw->cda = (__u32)(unsigned long)features;
780 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
782 rc |= (u64)le32_to_cpu(features->features) << 32;
785 ccw_device_dma_free(vcdev->cdev, features, sizeof(*features));
786 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
790 static void ccw_transport_features(struct virtio_device *vdev)
793 * Currently nothing to do here.
797 static int virtio_ccw_finalize_features(struct virtio_device *vdev)
799 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
800 struct virtio_feature_desc *features;
804 if (vcdev->revision >= 1 &&
805 !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
806 dev_err(&vdev->dev, "virtio: device uses revision 1 "
807 "but does not have VIRTIO_F_VERSION_1\n");
811 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
815 features = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*features));
820 /* Give virtio_ring a chance to accept features. */
821 vring_transport_features(vdev);
823 /* Give virtio_ccw a chance to accept features. */
824 ccw_transport_features(vdev);
827 features->features = cpu_to_le32((u32)vdev->features);
828 /* Write the first half of the feature bits to the host. */
829 ccw->cmd_code = CCW_CMD_WRITE_FEAT;
831 ccw->count = sizeof(*features);
832 ccw->cda = (__u32)(unsigned long)features;
833 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
837 if (vcdev->revision == 0)
841 features->features = cpu_to_le32(vdev->features >> 32);
842 /* Write the second half of the feature bits to the host. */
843 ccw->cmd_code = CCW_CMD_WRITE_FEAT;
845 ccw->count = sizeof(*features);
846 ccw->cda = (__u32)(unsigned long)features;
847 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
850 ccw_device_dma_free(vcdev->cdev, features, sizeof(*features));
851 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
856 static void virtio_ccw_get_config(struct virtio_device *vdev,
857 unsigned int offset, void *buf, unsigned len)
859 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
865 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
869 config_area = ccw_device_dma_zalloc(vcdev->cdev,
870 VIRTIO_CCW_CONFIG_SIZE);
874 /* Read the config area from the host. */
875 ccw->cmd_code = CCW_CMD_READ_CONF;
877 ccw->count = offset + len;
878 ccw->cda = (__u32)(unsigned long)config_area;
879 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG);
883 spin_lock_irqsave(&vcdev->lock, flags);
884 memcpy(vcdev->config, config_area, offset + len);
885 if (vcdev->config_ready < offset + len)
886 vcdev->config_ready = offset + len;
887 spin_unlock_irqrestore(&vcdev->lock, flags);
889 memcpy(buf, config_area + offset, len);
892 ccw_device_dma_free(vcdev->cdev, config_area, VIRTIO_CCW_CONFIG_SIZE);
893 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
896 static void virtio_ccw_set_config(struct virtio_device *vdev,
897 unsigned int offset, const void *buf,
900 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
905 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
909 config_area = ccw_device_dma_zalloc(vcdev->cdev,
910 VIRTIO_CCW_CONFIG_SIZE);
914 /* Make sure we don't overwrite fields. */
915 if (vcdev->config_ready < offset)
916 virtio_ccw_get_config(vdev, 0, NULL, offset);
917 spin_lock_irqsave(&vcdev->lock, flags);
918 memcpy(&vcdev->config[offset], buf, len);
919 /* Write the config area to the host. */
920 memcpy(config_area, vcdev->config, sizeof(vcdev->config));
921 spin_unlock_irqrestore(&vcdev->lock, flags);
922 ccw->cmd_code = CCW_CMD_WRITE_CONF;
924 ccw->count = offset + len;
925 ccw->cda = (__u32)(unsigned long)config_area;
926 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG);
929 ccw_device_dma_free(vcdev->cdev, config_area, VIRTIO_CCW_CONFIG_SIZE);
930 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
933 static u8 virtio_ccw_get_status(struct virtio_device *vdev)
935 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
936 u8 old_status = vcdev->dma_area->status;
939 if (vcdev->revision < 2)
940 return vcdev->dma_area->status;
942 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
946 ccw->cmd_code = CCW_CMD_READ_STATUS;
948 ccw->count = sizeof(vcdev->dma_area->status);
949 ccw->cda = (__u32)(unsigned long)&vcdev->dma_area->status;
950 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_STATUS);
952 * If the channel program failed (should only happen if the device
953 * was hotunplugged, and then we clean up via the machine check
954 * handler anyway), vcdev->dma_area->status was not overwritten and we just
955 * return the old status, which is fine.
957 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
959 return vcdev->dma_area->status;
962 static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status)
964 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
965 u8 old_status = vcdev->dma_area->status;
969 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
973 /* Write the status to the host. */
974 vcdev->dma_area->status = status;
975 ccw->cmd_code = CCW_CMD_WRITE_STATUS;
977 ccw->count = sizeof(status);
978 ccw->cda = (__u32)(unsigned long)&vcdev->dma_area->status;
979 /* We use ssch for setting the status which is a serializing
980 * instruction that guarantees the memory writes have
981 * completed before ssch.
983 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS);
984 /* Write failed? We assume status is unchanged. */
986 vcdev->dma_area->status = old_status;
987 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
990 static const char *virtio_ccw_bus_name(struct virtio_device *vdev)
992 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
994 return dev_name(&vcdev->cdev->dev);
997 static void virtio_ccw_synchronize_cbs(struct virtio_device *vdev)
999 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
1000 struct airq_info *info = vcdev->airq_info;
1004 * This device uses adapter interrupts: synchronize with
1005 * vring_interrupt() called by virtio_airq_handler()
1006 * via the indicator area lock.
1008 write_lock_irq(&info->lock);
1009 write_unlock_irq(&info->lock);
1011 /* This device uses classic interrupts: synchronize
1012 * with vring_interrupt() called by
1013 * virtio_ccw_int_handler() via the per-device
1016 write_lock_irq(&vcdev->irq_lock);
1017 write_unlock_irq(&vcdev->irq_lock);
1021 static const struct virtio_config_ops virtio_ccw_config_ops = {
1022 .get_features = virtio_ccw_get_features,
1023 .finalize_features = virtio_ccw_finalize_features,
1024 .get = virtio_ccw_get_config,
1025 .set = virtio_ccw_set_config,
1026 .get_status = virtio_ccw_get_status,
1027 .set_status = virtio_ccw_set_status,
1028 .reset = virtio_ccw_reset,
1029 .find_vqs = virtio_ccw_find_vqs,
1030 .del_vqs = virtio_ccw_del_vqs,
1031 .bus_name = virtio_ccw_bus_name,
1032 .synchronize_cbs = virtio_ccw_synchronize_cbs,
1037 * ccw bus driver related functions
1040 static void virtio_ccw_release_dev(struct device *_d)
1042 struct virtio_device *dev = dev_to_virtio(_d);
1043 struct virtio_ccw_device *vcdev = to_vc_device(dev);
1045 ccw_device_dma_free(vcdev->cdev, vcdev->dma_area,
1046 sizeof(*vcdev->dma_area));
1050 static int irb_is_error(struct irb *irb)
1052 if (scsw_cstat(&irb->scsw) != 0)
1054 if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
1056 if (scsw_cc(&irb->scsw) != 0)
1061 static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev,
1064 struct virtio_ccw_vq_info *info;
1065 unsigned long flags;
1066 struct virtqueue *vq;
1069 spin_lock_irqsave(&vcdev->lock, flags);
1070 list_for_each_entry(info, &vcdev->virtqueues, node) {
1071 if (info->vq->index == index) {
1076 spin_unlock_irqrestore(&vcdev->lock, flags);
1080 static void virtio_ccw_check_activity(struct virtio_ccw_device *vcdev,
1083 if (vcdev->curr_io & activity) {
1085 case VIRTIO_CCW_DOING_READ_FEAT:
1086 case VIRTIO_CCW_DOING_WRITE_FEAT:
1087 case VIRTIO_CCW_DOING_READ_CONFIG:
1088 case VIRTIO_CCW_DOING_WRITE_CONFIG:
1089 case VIRTIO_CCW_DOING_WRITE_STATUS:
1090 case VIRTIO_CCW_DOING_READ_STATUS:
1091 case VIRTIO_CCW_DOING_SET_VQ:
1092 case VIRTIO_CCW_DOING_SET_IND:
1093 case VIRTIO_CCW_DOING_SET_CONF_IND:
1094 case VIRTIO_CCW_DOING_RESET:
1095 case VIRTIO_CCW_DOING_READ_VQ_CONF:
1096 case VIRTIO_CCW_DOING_SET_IND_ADAPTER:
1097 case VIRTIO_CCW_DOING_SET_VIRTIO_REV:
1098 vcdev->curr_io &= ~activity;
1099 wake_up(&vcdev->wait_q);
1102 /* don't know what to do... */
1103 dev_warn(&vcdev->cdev->dev,
1104 "Suspicious activity '%08x'\n", activity);
1111 static void virtio_ccw_int_handler(struct ccw_device *cdev,
1112 unsigned long intparm,
1115 __u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK;
1116 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1118 struct virtqueue *vq;
1123 vcdev->err = PTR_ERR(irb);
1124 virtio_ccw_check_activity(vcdev, activity);
1125 /* Don't poke around indicators, something's wrong. */
1128 /* Check if it's a notification from the host. */
1129 if ((intparm == 0) &&
1130 (scsw_stctl(&irb->scsw) ==
1131 (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) {
1134 if (irb_is_error(irb)) {
1135 /* Command reject? */
1136 if ((scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) &&
1137 (irb->ecw[0] & SNS0_CMD_REJECT))
1138 vcdev->err = -EOPNOTSUPP;
1140 /* Map everything else to -EIO. */
1143 virtio_ccw_check_activity(vcdev, activity);
1144 #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
1146 * Paired with virtio_ccw_synchronize_cbs() and interrupts are
1149 read_lock(&vcdev->irq_lock);
1151 for_each_set_bit(i, indicators(vcdev),
1152 sizeof(*indicators(vcdev)) * BITS_PER_BYTE) {
1153 /* The bit clear must happen before the vring kick. */
1154 clear_bit(i, indicators(vcdev));
1156 vq = virtio_ccw_vq_by_ind(vcdev, i);
1157 vring_interrupt(0, vq);
1159 #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
1160 read_unlock(&vcdev->irq_lock);
1162 if (test_bit(0, indicators2(vcdev))) {
1163 virtio_config_changed(&vcdev->vdev);
1164 clear_bit(0, indicators2(vcdev));
1169 * We usually want to autoonline all devices, but give the admin
1170 * a way to exempt devices from this.
1172 #define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
1174 static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS];
1176 static char *no_auto = "";
1178 module_param(no_auto, charp, 0444);
1179 MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined");
1181 static int virtio_ccw_check_autoonline(struct ccw_device *cdev)
1183 struct ccw_dev_id id;
1185 ccw_device_get_id(cdev, &id);
1186 if (test_bit(id.devno, devs_no_auto[id.ssid]))
1191 static void virtio_ccw_auto_online(void *data, async_cookie_t cookie)
1193 struct ccw_device *cdev = data;
1196 ret = ccw_device_set_online(cdev);
1198 dev_warn(&cdev->dev, "Failed to set online: %d\n", ret);
1201 static int virtio_ccw_probe(struct ccw_device *cdev)
1203 cdev->handler = virtio_ccw_int_handler;
1205 if (virtio_ccw_check_autoonline(cdev))
1206 async_schedule(virtio_ccw_auto_online, cdev);
1210 static struct virtio_ccw_device *virtio_grab_drvdata(struct ccw_device *cdev)
1212 unsigned long flags;
1213 struct virtio_ccw_device *vcdev;
1215 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1216 vcdev = dev_get_drvdata(&cdev->dev);
1217 if (!vcdev || vcdev->going_away) {
1218 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1221 vcdev->going_away = true;
1222 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1226 static void virtio_ccw_remove(struct ccw_device *cdev)
1228 unsigned long flags;
1229 struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
1231 if (vcdev && cdev->online) {
1232 if (vcdev->device_lost)
1233 virtio_break_device(&vcdev->vdev);
1234 unregister_virtio_device(&vcdev->vdev);
1235 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1236 dev_set_drvdata(&cdev->dev, NULL);
1237 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1239 cdev->handler = NULL;
1242 static int virtio_ccw_offline(struct ccw_device *cdev)
1244 unsigned long flags;
1245 struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
1249 if (vcdev->device_lost)
1250 virtio_break_device(&vcdev->vdev);
1251 unregister_virtio_device(&vcdev->vdev);
1252 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1253 dev_set_drvdata(&cdev->dev, NULL);
1254 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1258 static int virtio_ccw_set_transport_rev(struct virtio_ccw_device *vcdev)
1260 struct virtio_rev_info *rev;
1264 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
1267 rev = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*rev));
1269 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
1273 /* Set transport revision */
1274 ccw->cmd_code = CCW_CMD_SET_VIRTIO_REV;
1276 ccw->count = sizeof(*rev);
1277 ccw->cda = (__u32)(unsigned long)rev;
1279 vcdev->revision = VIRTIO_CCW_REV_MAX;
1281 rev->revision = vcdev->revision;
1282 /* none of our supported revisions carry payload */
1284 ret = ccw_io_helper(vcdev, ccw,
1285 VIRTIO_CCW_DOING_SET_VIRTIO_REV);
1286 if (ret == -EOPNOTSUPP) {
1287 if (vcdev->revision == 0)
1289 * The host device does not support setting
1290 * the revision: let's operate it in legacy
1297 } while (ret == -EOPNOTSUPP);
1299 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
1300 ccw_device_dma_free(vcdev->cdev, rev, sizeof(*rev));
1304 static int virtio_ccw_online(struct ccw_device *cdev)
1307 struct virtio_ccw_device *vcdev;
1308 unsigned long flags;
1310 vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL);
1312 dev_warn(&cdev->dev, "Could not get memory for virtio\n");
1316 vcdev->vdev.dev.parent = &cdev->dev;
1318 vcdev->dma_area = ccw_device_dma_zalloc(vcdev->cdev,
1319 sizeof(*vcdev->dma_area));
1320 if (!vcdev->dma_area) {
1325 vcdev->is_thinint = virtio_ccw_use_airq; /* at least try */
1327 vcdev->vdev.dev.release = virtio_ccw_release_dev;
1328 vcdev->vdev.config = &virtio_ccw_config_ops;
1329 init_waitqueue_head(&vcdev->wait_q);
1330 INIT_LIST_HEAD(&vcdev->virtqueues);
1331 spin_lock_init(&vcdev->lock);
1332 rwlock_init(&vcdev->irq_lock);
1333 mutex_init(&vcdev->io_lock);
1335 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1336 dev_set_drvdata(&cdev->dev, vcdev);
1337 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1338 vcdev->vdev.id.vendor = cdev->id.cu_type;
1339 vcdev->vdev.id.device = cdev->id.cu_model;
1341 ret = virtio_ccw_set_transport_rev(vcdev);
1345 ret = register_virtio_device(&vcdev->vdev);
1347 dev_warn(&cdev->dev, "Failed to register virtio device: %d\n",
1353 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1354 dev_set_drvdata(&cdev->dev, NULL);
1355 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1356 put_device(&vcdev->vdev.dev);
1360 ccw_device_dma_free(vcdev->cdev, vcdev->dma_area,
1361 sizeof(*vcdev->dma_area));
1367 static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event)
1370 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1373 * Make sure vcdev is set
1374 * i.e. set_offline/remove callback not already running
1381 vcdev->device_lost = true;
1394 static struct ccw_device_id virtio_ids[] = {
1395 { CCW_DEVICE(0x3832, 0) },
1399 static struct ccw_driver virtio_ccw_driver = {
1401 .owner = THIS_MODULE,
1402 .name = "virtio_ccw",
1405 .probe = virtio_ccw_probe,
1406 .remove = virtio_ccw_remove,
1407 .set_offline = virtio_ccw_offline,
1408 .set_online = virtio_ccw_online,
1409 .notify = virtio_ccw_cio_notify,
1410 .int_class = IRQIO_VIR,
1413 static int __init pure_hex(char **cp, unsigned int *val, int min_digit,
1414 int max_digit, int max_val)
1421 while (diff <= max_digit) {
1422 int value = hex_to_bin(**cp);
1426 *val = *val * 16 + value;
1431 if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
1437 static int __init parse_busid(char *str, unsigned int *cssid,
1438 unsigned int *ssid, unsigned int *devno)
1449 ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
1450 if (ret || (str_work[0] != '.'))
1453 ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
1454 if (ret || (str_work[0] != '.'))
1457 ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
1458 if (ret || (str_work[0] != '\0'))
1466 static void __init no_auto_parse(void)
1468 unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
1473 while ((parm = strsep(&str, ","))) {
1474 rc = parse_busid(strsep(&parm, "-"), &from_cssid,
1479 rc = parse_busid(parm, &to_cssid,
1481 if ((from_ssid > to_ssid) ||
1482 ((from_ssid == to_ssid) && (from > to)))
1485 to_cssid = from_cssid;
1486 to_ssid = from_ssid;
1491 while ((from_ssid < to_ssid) ||
1492 ((from_ssid == to_ssid) && (from <= to))) {
1493 set_bit(from, devs_no_auto[from_ssid]);
1495 if (from > __MAX_SUBCHANNEL) {
1503 static int __init virtio_ccw_init(void)
1507 /* parse no_auto string before we do anything further */
1510 summary_indicators = cio_dma_zalloc(MAX_AIRQ_AREAS);
1511 if (!summary_indicators)
1513 rc = ccw_driver_register(&virtio_ccw_driver);
1515 cio_dma_free(summary_indicators, MAX_AIRQ_AREAS);
1518 device_initcall(virtio_ccw_init);