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;
61 /* we make cdev->dev.dma_parms point to this */
62 struct device_dma_parameters dma_parms;
65 unsigned int revision; /* Transport revision */
66 wait_queue_head_t wait_q;
69 struct mutex io_lock; /* Serializes I/O requests */
70 struct list_head virtqueues;
74 unsigned int config_ready;
76 struct vcdev_dma_area *dma_area;
77 dma32_t dma_area_addr;
80 static inline unsigned long *indicators(struct virtio_ccw_device *vcdev)
82 return &vcdev->dma_area->indicators;
85 static inline unsigned long *indicators2(struct virtio_ccw_device *vcdev)
87 return &vcdev->dma_area->indicators2;
90 /* Spec stipulates a 64 bit address */
91 static inline dma64_t indicators_dma(struct virtio_ccw_device *vcdev)
93 u64 dma_area_addr = dma32_to_u32(vcdev->dma_area_addr);
95 return dma64_add(u64_to_dma64(dma_area_addr),
96 offsetof(struct vcdev_dma_area, indicators));
99 /* Spec stipulates a 64 bit address */
100 static inline dma64_t indicators2_dma(struct virtio_ccw_device *vcdev)
102 u64 dma_area_addr = dma32_to_u32(vcdev->dma_area_addr);
104 return dma64_add(u64_to_dma64(dma_area_addr),
105 offsetof(struct vcdev_dma_area, indicators2));
108 static inline dma32_t config_block_dma(struct virtio_ccw_device *vcdev)
110 return dma32_add(vcdev->dma_area_addr,
111 offsetof(struct vcdev_dma_area, config_block));
114 static inline dma32_t status_dma(struct virtio_ccw_device *vcdev)
116 return dma32_add(vcdev->dma_area_addr,
117 offsetof(struct vcdev_dma_area, status));
120 struct vq_info_block_legacy {
127 struct vq_info_block {
136 struct virtio_feature_desc {
141 struct virtio_thinint_area {
142 dma64_t summary_indicator;
148 struct virtio_rev_info {
154 /* the highest virtio-ccw revision we support */
155 #define VIRTIO_CCW_REV_MAX 2
157 struct virtio_ccw_vq_info {
158 struct virtqueue *vq;
159 dma32_t info_block_addr;
162 struct vq_info_block s;
163 struct vq_info_block_legacy l;
166 struct list_head node;
170 #define VIRTIO_AIRQ_ISC IO_SCH_ISC /* inherit from subchannel */
172 #define VIRTIO_IV_BITS (L1_CACHE_BYTES * 8)
173 #define MAX_AIRQ_AREAS 20
175 static int virtio_ccw_use_airq = 1;
179 u8 summary_indicator_idx;
180 struct airq_struct airq;
183 static struct airq_info *airq_areas[MAX_AIRQ_AREAS];
184 static DEFINE_MUTEX(airq_areas_lock);
186 static u8 *summary_indicators;
188 static inline u8 *get_summary_indicator(struct airq_info *info)
190 return summary_indicators + info->summary_indicator_idx;
193 static inline dma64_t get_summary_indicator_dma(struct airq_info *info)
195 return virt_to_dma64(get_summary_indicator(info));
198 #define CCW_CMD_SET_VQ 0x13
199 #define CCW_CMD_VDEV_RESET 0x33
200 #define CCW_CMD_SET_IND 0x43
201 #define CCW_CMD_SET_CONF_IND 0x53
202 #define CCW_CMD_READ_FEAT 0x12
203 #define CCW_CMD_WRITE_FEAT 0x11
204 #define CCW_CMD_READ_CONF 0x22
205 #define CCW_CMD_WRITE_CONF 0x21
206 #define CCW_CMD_WRITE_STATUS 0x31
207 #define CCW_CMD_READ_VQ_CONF 0x32
208 #define CCW_CMD_READ_STATUS 0x72
209 #define CCW_CMD_SET_IND_ADAPTER 0x73
210 #define CCW_CMD_SET_VIRTIO_REV 0x83
212 #define VIRTIO_CCW_DOING_SET_VQ 0x00010000
213 #define VIRTIO_CCW_DOING_RESET 0x00040000
214 #define VIRTIO_CCW_DOING_READ_FEAT 0x00080000
215 #define VIRTIO_CCW_DOING_WRITE_FEAT 0x00100000
216 #define VIRTIO_CCW_DOING_READ_CONFIG 0x00200000
217 #define VIRTIO_CCW_DOING_WRITE_CONFIG 0x00400000
218 #define VIRTIO_CCW_DOING_WRITE_STATUS 0x00800000
219 #define VIRTIO_CCW_DOING_SET_IND 0x01000000
220 #define VIRTIO_CCW_DOING_READ_VQ_CONF 0x02000000
221 #define VIRTIO_CCW_DOING_SET_CONF_IND 0x04000000
222 #define VIRTIO_CCW_DOING_SET_IND_ADAPTER 0x08000000
223 #define VIRTIO_CCW_DOING_SET_VIRTIO_REV 0x10000000
224 #define VIRTIO_CCW_DOING_READ_STATUS 0x20000000
225 #define VIRTIO_CCW_INTPARM_MASK 0xffff0000
227 static struct virtio_ccw_device *to_vc_device(struct virtio_device *vdev)
229 return container_of(vdev, struct virtio_ccw_device, vdev);
232 static void drop_airq_indicator(struct virtqueue *vq, struct airq_info *info)
234 unsigned long i, flags;
236 write_lock_irqsave(&info->lock, flags);
237 for (i = 0; i < airq_iv_end(info->aiv); i++) {
238 if (vq == (void *)airq_iv_get_ptr(info->aiv, i)) {
239 airq_iv_free_bit(info->aiv, i);
240 airq_iv_set_ptr(info->aiv, i, 0);
244 write_unlock_irqrestore(&info->lock, flags);
247 static void virtio_airq_handler(struct airq_struct *airq,
248 struct tpi_info *tpi_info)
250 struct airq_info *info = container_of(airq, struct airq_info, airq);
253 inc_irq_stat(IRQIO_VAI);
254 read_lock(&info->lock);
255 /* Walk through indicators field, summary indicator active. */
257 ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
260 vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
262 *(get_summary_indicator(info)) = 0;
264 /* Walk through indicators field, summary indicator not active. */
266 ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
269 vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
271 read_unlock(&info->lock);
274 static struct airq_info *new_airq_info(int index)
276 struct airq_info *info;
279 info = kzalloc(sizeof(*info), GFP_KERNEL);
282 rwlock_init(&info->lock);
283 info->aiv = airq_iv_create(VIRTIO_IV_BITS, AIRQ_IV_ALLOC | AIRQ_IV_PTR
284 | AIRQ_IV_CACHELINE, NULL);
289 info->airq.handler = virtio_airq_handler;
290 info->summary_indicator_idx = index;
291 info->airq.lsi_ptr = get_summary_indicator(info);
292 info->airq.isc = VIRTIO_AIRQ_ISC;
293 rc = register_adapter_interrupt(&info->airq);
295 airq_iv_release(info->aiv);
302 static unsigned long *get_airq_indicator(struct virtqueue *vqs[], int nvqs,
303 u64 *first, void **airq_info)
306 struct airq_info *info;
307 unsigned long *indicator_addr = NULL;
308 unsigned long bit, flags;
310 for (i = 0; i < MAX_AIRQ_AREAS && !indicator_addr; i++) {
311 mutex_lock(&airq_areas_lock);
313 airq_areas[i] = new_airq_info(i);
314 info = airq_areas[i];
315 mutex_unlock(&airq_areas_lock);
318 write_lock_irqsave(&info->lock, flags);
319 bit = airq_iv_alloc(info->aiv, nvqs);
321 /* Not enough vacancies. */
322 write_unlock_irqrestore(&info->lock, flags);
327 indicator_addr = info->aiv->vector;
328 for (j = 0; j < nvqs; j++) {
329 airq_iv_set_ptr(info->aiv, bit + j,
330 (unsigned long)vqs[j]);
332 write_unlock_irqrestore(&info->lock, flags);
334 return indicator_addr;
337 static void virtio_ccw_drop_indicators(struct virtio_ccw_device *vcdev)
339 struct virtio_ccw_vq_info *info;
341 if (!vcdev->airq_info)
343 list_for_each_entry(info, &vcdev->virtqueues, node)
344 drop_airq_indicator(info->vq, vcdev->airq_info);
347 static int doing_io(struct virtio_ccw_device *vcdev, __u32 flag)
352 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
356 ret = vcdev->curr_io & flag;
357 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
361 static int ccw_io_helper(struct virtio_ccw_device *vcdev,
362 struct ccw1 *ccw, __u32 intparm)
366 int flag = intparm & VIRTIO_CCW_INTPARM_MASK;
368 mutex_lock(&vcdev->io_lock);
370 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
371 ret = ccw_device_start(vcdev->cdev, ccw, intparm, 0, 0);
375 vcdev->curr_io |= flag;
377 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
379 } while (ret == -EBUSY);
380 wait_event(vcdev->wait_q, doing_io(vcdev, flag) == 0);
381 ret = ret ? ret : vcdev->err;
382 mutex_unlock(&vcdev->io_lock);
386 static void virtio_ccw_drop_indicator(struct virtio_ccw_device *vcdev,
390 struct virtio_thinint_area *thinint_area = NULL;
391 struct airq_info *airq_info = vcdev->airq_info;
392 dma64_t *indicatorp = NULL;
394 if (vcdev->is_thinint) {
395 thinint_area = ccw_device_dma_zalloc(vcdev->cdev,
396 sizeof(*thinint_area),
400 thinint_area->summary_indicator =
401 get_summary_indicator_dma(airq_info);
402 thinint_area->isc = VIRTIO_AIRQ_ISC;
403 ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
404 ccw->count = sizeof(*thinint_area);
406 /* payload is the address of the indicators */
407 indicatorp = ccw_device_dma_zalloc(vcdev->cdev,
413 ccw->cmd_code = CCW_CMD_SET_IND;
414 ccw->count = sizeof(*indicatorp);
416 /* Deregister indicators from host. */
417 *indicators(vcdev) = 0;
419 ret = ccw_io_helper(vcdev, ccw,
421 VIRTIO_CCW_DOING_SET_IND_ADAPTER :
422 VIRTIO_CCW_DOING_SET_IND);
423 if (ret && (ret != -ENODEV))
424 dev_info(&vcdev->cdev->dev,
425 "Failed to deregister indicators (%d)\n", ret);
426 else if (vcdev->is_thinint)
427 virtio_ccw_drop_indicators(vcdev);
428 ccw_device_dma_free(vcdev->cdev, indicatorp, sizeof(*indicatorp));
429 ccw_device_dma_free(vcdev->cdev, thinint_area, sizeof(*thinint_area));
432 static inline bool virtio_ccw_do_kvm_notify(struct virtqueue *vq, u32 data)
434 struct virtio_ccw_vq_info *info = vq->priv;
435 struct virtio_ccw_device *vcdev;
436 struct subchannel_id schid;
438 vcdev = to_vc_device(info->vq->vdev);
439 ccw_device_get_schid(vcdev->cdev, &schid);
440 BUILD_BUG_ON(sizeof(struct subchannel_id) != sizeof(unsigned int));
441 info->cookie = kvm_hypercall3(KVM_S390_VIRTIO_CCW_NOTIFY,
442 *((unsigned int *)&schid),
444 if (info->cookie < 0)
449 static bool virtio_ccw_kvm_notify(struct virtqueue *vq)
451 return virtio_ccw_do_kvm_notify(vq, vq->index);
454 static bool virtio_ccw_kvm_notify_with_data(struct virtqueue *vq)
456 return virtio_ccw_do_kvm_notify(vq, vring_notification_data(vq));
459 static int virtio_ccw_read_vq_conf(struct virtio_ccw_device *vcdev,
460 struct ccw1 *ccw, int index)
464 vcdev->dma_area->config_block.index = index;
465 ccw->cmd_code = CCW_CMD_READ_VQ_CONF;
467 ccw->count = sizeof(struct vq_config_block);
468 ccw->cda = config_block_dma(vcdev);
469 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_VQ_CONF);
472 return vcdev->dma_area->config_block.num ?: -ENOENT;
475 static void virtio_ccw_del_vq(struct virtqueue *vq, struct ccw1 *ccw)
477 struct virtio_ccw_device *vcdev = to_vc_device(vq->vdev);
478 struct virtio_ccw_vq_info *info = vq->priv;
481 unsigned int index = vq->index;
483 /* Remove from our list. */
484 spin_lock_irqsave(&vcdev->lock, flags);
485 list_del(&info->node);
486 spin_unlock_irqrestore(&vcdev->lock, flags);
488 /* Release from host. */
489 if (vcdev->revision == 0) {
490 info->info_block->l.queue = 0;
491 info->info_block->l.align = 0;
492 info->info_block->l.index = index;
493 info->info_block->l.num = 0;
494 ccw->count = sizeof(info->info_block->l);
496 info->info_block->s.desc = 0;
497 info->info_block->s.index = index;
498 info->info_block->s.num = 0;
499 info->info_block->s.avail = 0;
500 info->info_block->s.used = 0;
501 ccw->count = sizeof(info->info_block->s);
503 ccw->cmd_code = CCW_CMD_SET_VQ;
505 ccw->cda = info->info_block_addr;
506 ret = ccw_io_helper(vcdev, ccw,
507 VIRTIO_CCW_DOING_SET_VQ | index);
509 * -ENODEV isn't considered an error: The device is gone anyway.
510 * This may happen on device detach.
512 if (ret && (ret != -ENODEV))
513 dev_warn(&vq->vdev->dev, "Error %d while deleting queue %d\n",
516 vring_del_virtqueue(vq);
517 ccw_device_dma_free(vcdev->cdev, info->info_block,
518 sizeof(*info->info_block));
522 static void virtio_ccw_del_vqs(struct virtio_device *vdev)
524 struct virtqueue *vq, *n;
526 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
528 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
532 virtio_ccw_drop_indicator(vcdev, ccw);
534 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
535 virtio_ccw_del_vq(vq, ccw);
537 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
540 static struct virtqueue *virtio_ccw_setup_vq(struct virtio_device *vdev,
541 int i, vq_callback_t *callback,
542 const char *name, bool ctx,
545 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
546 bool (*notify)(struct virtqueue *vq);
548 struct virtqueue *vq = NULL;
549 struct virtio_ccw_vq_info *info;
554 if (__virtio_test_bit(vdev, VIRTIO_F_NOTIFICATION_DATA))
555 notify = virtio_ccw_kvm_notify_with_data;
557 notify = virtio_ccw_kvm_notify;
559 /* Allocate queue. */
560 info = kzalloc(sizeof(struct virtio_ccw_vq_info), GFP_KERNEL);
562 dev_warn(&vcdev->cdev->dev, "no info\n");
566 info->info_block = ccw_device_dma_zalloc(vcdev->cdev,
567 sizeof(*info->info_block),
568 &info->info_block_addr);
569 if (!info->info_block) {
570 dev_warn(&vcdev->cdev->dev, "no info block\n");
574 info->num = virtio_ccw_read_vq_conf(vcdev, ccw, i);
579 may_reduce = vcdev->revision > 0;
580 vq = vring_create_virtqueue(i, info->num, KVM_VIRTIO_CCW_RING_ALIGN,
581 vdev, true, may_reduce, ctx,
582 notify, callback, name);
585 /* For now, we fail if we can't get the requested size. */
586 dev_warn(&vcdev->cdev->dev, "no vq\n");
591 vq->num_max = info->num;
593 /* it may have been reduced */
594 info->num = virtqueue_get_vring_size(vq);
596 /* Register it with the host. */
597 queue = virtqueue_get_desc_addr(vq);
598 if (vcdev->revision == 0) {
599 info->info_block->l.queue = u64_to_dma64(queue);
600 info->info_block->l.align = KVM_VIRTIO_CCW_RING_ALIGN;
601 info->info_block->l.index = i;
602 info->info_block->l.num = info->num;
603 ccw->count = sizeof(info->info_block->l);
605 info->info_block->s.desc = u64_to_dma64(queue);
606 info->info_block->s.index = i;
607 info->info_block->s.num = info->num;
608 info->info_block->s.avail = u64_to_dma64(virtqueue_get_avail_addr(vq));
609 info->info_block->s.used = u64_to_dma64(virtqueue_get_used_addr(vq));
610 ccw->count = sizeof(info->info_block->s);
612 ccw->cmd_code = CCW_CMD_SET_VQ;
614 ccw->cda = info->info_block_addr;
615 err = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_VQ | i);
617 dev_warn(&vcdev->cdev->dev, "SET_VQ failed\n");
624 /* Save it to our list. */
625 spin_lock_irqsave(&vcdev->lock, flags);
626 list_add(&info->node, &vcdev->virtqueues);
627 spin_unlock_irqrestore(&vcdev->lock, flags);
633 vring_del_virtqueue(vq);
635 ccw_device_dma_free(vcdev->cdev, info->info_block,
636 sizeof(*info->info_block));
642 static int virtio_ccw_register_adapter_ind(struct virtio_ccw_device *vcdev,
643 struct virtqueue *vqs[], int nvqs,
647 struct virtio_thinint_area *thinint_area = NULL;
648 unsigned long *indicator_addr;
649 struct airq_info *info;
651 thinint_area = ccw_device_dma_zalloc(vcdev->cdev,
652 sizeof(*thinint_area),
658 /* Try to get an indicator. */
659 indicator_addr = get_airq_indicator(vqs, nvqs,
660 &thinint_area->bit_nr,
662 if (!indicator_addr) {
666 thinint_area->indicator = virt_to_dma64(indicator_addr);
667 info = vcdev->airq_info;
668 thinint_area->summary_indicator = get_summary_indicator_dma(info);
669 thinint_area->isc = VIRTIO_AIRQ_ISC;
670 ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
671 ccw->flags = CCW_FLAG_SLI;
672 ccw->count = sizeof(*thinint_area);
673 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND_ADAPTER);
675 if (ret == -EOPNOTSUPP) {
677 * The host does not support adapter interrupts
678 * for virtio-ccw, stop trying.
680 virtio_ccw_use_airq = 0;
681 pr_info("Adapter interrupts unsupported on host\n");
683 dev_warn(&vcdev->cdev->dev,
684 "enabling adapter interrupts = %d\n", ret);
685 virtio_ccw_drop_indicators(vcdev);
688 ccw_device_dma_free(vcdev->cdev, thinint_area, sizeof(*thinint_area));
692 static int virtio_ccw_find_vqs(struct virtio_device *vdev, unsigned nvqs,
693 struct virtqueue *vqs[],
694 struct virtqueue_info vqs_info[],
695 struct irq_affinity *desc)
697 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
698 dma64_t *indicatorp = NULL;
699 int ret, i, queue_idx = 0;
701 dma32_t indicatorp_dma = 0;
703 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
707 for (i = 0; i < nvqs; ++i) {
708 struct virtqueue_info *vqi = &vqs_info[i];
715 vqs[i] = virtio_ccw_setup_vq(vdev, queue_idx++, vqi->callback,
716 vqi->name, vqi->ctx, ccw);
717 if (IS_ERR(vqs[i])) {
718 ret = PTR_ERR(vqs[i]);
725 * We need a data area under 2G to communicate. Our payload is
726 * the address of the indicators.
728 indicatorp = ccw_device_dma_zalloc(vcdev->cdev,
733 *indicatorp = indicators_dma(vcdev);
734 if (vcdev->is_thinint) {
735 ret = virtio_ccw_register_adapter_ind(vcdev, vqs, nvqs, ccw);
737 /* no error, just fall back to legacy interrupts */
738 vcdev->is_thinint = false;
740 ccw->cda = indicatorp_dma;
741 if (!vcdev->is_thinint) {
742 /* Register queue indicators with host. */
743 *indicators(vcdev) = 0;
744 ccw->cmd_code = CCW_CMD_SET_IND;
746 ccw->count = sizeof(*indicatorp);
747 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND);
751 /* Register indicators2 with host for config changes */
752 *indicatorp = indicators2_dma(vcdev);
753 *indicators2(vcdev) = 0;
754 ccw->cmd_code = CCW_CMD_SET_CONF_IND;
756 ccw->count = sizeof(*indicatorp);
757 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_CONF_IND);
762 ccw_device_dma_free(vcdev->cdev, indicatorp,
763 sizeof(*indicatorp));
764 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
768 ccw_device_dma_free(vcdev->cdev, indicatorp,
769 sizeof(*indicatorp));
770 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
771 virtio_ccw_del_vqs(vdev);
775 static void virtio_ccw_reset(struct virtio_device *vdev)
777 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
780 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
784 /* Zero status bits. */
785 vcdev->dma_area->status = 0;
787 /* Send a reset ccw on device. */
788 ccw->cmd_code = CCW_CMD_VDEV_RESET;
792 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_RESET);
793 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
796 static u64 virtio_ccw_get_features(struct virtio_device *vdev)
798 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
799 struct virtio_feature_desc *features;
804 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
808 features = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*features),
814 /* Read the feature bits from the host. */
816 ccw->cmd_code = CCW_CMD_READ_FEAT;
818 ccw->count = sizeof(*features);
819 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
825 rc = le32_to_cpu(features->features);
827 if (vcdev->revision == 0)
830 /* Read second half of the feature bits from the host. */
832 ccw->cmd_code = CCW_CMD_READ_FEAT;
834 ccw->count = sizeof(*features);
835 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
837 rc |= (u64)le32_to_cpu(features->features) << 32;
840 ccw_device_dma_free(vcdev->cdev, features, sizeof(*features));
841 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
845 static void ccw_transport_features(struct virtio_device *vdev)
848 * Currently nothing to do here.
852 static int virtio_ccw_finalize_features(struct virtio_device *vdev)
854 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
855 struct virtio_feature_desc *features;
859 if (vcdev->revision >= 1 &&
860 !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
861 dev_err(&vdev->dev, "virtio: device uses revision 1 "
862 "but does not have VIRTIO_F_VERSION_1\n");
866 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
870 features = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*features),
876 /* Give virtio_ring a chance to accept features. */
877 vring_transport_features(vdev);
879 /* Give virtio_ccw a chance to accept features. */
880 ccw_transport_features(vdev);
883 features->features = cpu_to_le32((u32)vdev->features);
884 /* Write the first half of the feature bits to the host. */
885 ccw->cmd_code = CCW_CMD_WRITE_FEAT;
887 ccw->count = sizeof(*features);
888 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
892 if (vcdev->revision == 0)
896 features->features = cpu_to_le32(vdev->features >> 32);
897 /* Write the second half of the feature bits to the host. */
898 ccw->cmd_code = CCW_CMD_WRITE_FEAT;
900 ccw->count = sizeof(*features);
901 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
904 ccw_device_dma_free(vcdev->cdev, features, sizeof(*features));
905 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
910 static void virtio_ccw_get_config(struct virtio_device *vdev,
911 unsigned int offset, void *buf, unsigned len)
913 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
919 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
923 config_area = ccw_device_dma_zalloc(vcdev->cdev,
924 VIRTIO_CCW_CONFIG_SIZE,
929 /* Read the config area from the host. */
930 ccw->cmd_code = CCW_CMD_READ_CONF;
932 ccw->count = offset + len;
933 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG);
937 spin_lock_irqsave(&vcdev->lock, flags);
938 memcpy(vcdev->config, config_area, offset + len);
939 if (vcdev->config_ready < offset + len)
940 vcdev->config_ready = offset + len;
941 spin_unlock_irqrestore(&vcdev->lock, flags);
943 memcpy(buf, config_area + offset, len);
946 ccw_device_dma_free(vcdev->cdev, config_area, VIRTIO_CCW_CONFIG_SIZE);
947 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
950 static void virtio_ccw_set_config(struct virtio_device *vdev,
951 unsigned int offset, const void *buf,
954 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
959 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
963 config_area = ccw_device_dma_zalloc(vcdev->cdev,
964 VIRTIO_CCW_CONFIG_SIZE,
969 /* Make sure we don't overwrite fields. */
970 if (vcdev->config_ready < offset)
971 virtio_ccw_get_config(vdev, 0, NULL, offset);
972 spin_lock_irqsave(&vcdev->lock, flags);
973 memcpy(&vcdev->config[offset], buf, len);
974 /* Write the config area to the host. */
975 memcpy(config_area, vcdev->config, sizeof(vcdev->config));
976 spin_unlock_irqrestore(&vcdev->lock, flags);
977 ccw->cmd_code = CCW_CMD_WRITE_CONF;
979 ccw->count = offset + len;
980 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG);
983 ccw_device_dma_free(vcdev->cdev, config_area, VIRTIO_CCW_CONFIG_SIZE);
984 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
987 static u8 virtio_ccw_get_status(struct virtio_device *vdev)
989 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
990 u8 old_status = vcdev->dma_area->status;
993 if (vcdev->revision < 2)
994 return vcdev->dma_area->status;
996 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
1000 ccw->cmd_code = CCW_CMD_READ_STATUS;
1002 ccw->count = sizeof(vcdev->dma_area->status);
1003 ccw->cda = status_dma(vcdev);
1004 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_STATUS);
1006 * If the channel program failed (should only happen if the device
1007 * was hotunplugged, and then we clean up via the machine check
1008 * handler anyway), vcdev->dma_area->status was not overwritten and we just
1009 * return the old status, which is fine.
1011 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
1013 return vcdev->dma_area->status;
1016 static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status)
1018 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
1019 u8 old_status = vcdev->dma_area->status;
1023 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
1027 /* Write the status to the host. */
1028 vcdev->dma_area->status = status;
1029 ccw->cmd_code = CCW_CMD_WRITE_STATUS;
1031 ccw->count = sizeof(status);
1032 /* We use ssch for setting the status which is a serializing
1033 * instruction that guarantees the memory writes have
1034 * completed before ssch.
1036 ccw->cda = status_dma(vcdev);
1037 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS);
1038 /* Write failed? We assume status is unchanged. */
1040 vcdev->dma_area->status = old_status;
1041 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
1044 static const char *virtio_ccw_bus_name(struct virtio_device *vdev)
1046 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
1048 return dev_name(&vcdev->cdev->dev);
1051 static void virtio_ccw_synchronize_cbs(struct virtio_device *vdev)
1053 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
1054 struct airq_info *info = vcdev->airq_info;
1058 * This device uses adapter interrupts: synchronize with
1059 * vring_interrupt() called by virtio_airq_handler()
1060 * via the indicator area lock.
1062 write_lock_irq(&info->lock);
1063 write_unlock_irq(&info->lock);
1065 /* This device uses classic interrupts: synchronize
1066 * with vring_interrupt() called by
1067 * virtio_ccw_int_handler() via the per-device
1070 write_lock_irq(&vcdev->irq_lock);
1071 write_unlock_irq(&vcdev->irq_lock);
1075 static const struct virtio_config_ops virtio_ccw_config_ops = {
1076 .get_features = virtio_ccw_get_features,
1077 .finalize_features = virtio_ccw_finalize_features,
1078 .get = virtio_ccw_get_config,
1079 .set = virtio_ccw_set_config,
1080 .get_status = virtio_ccw_get_status,
1081 .set_status = virtio_ccw_set_status,
1082 .reset = virtio_ccw_reset,
1083 .find_vqs = virtio_ccw_find_vqs,
1084 .del_vqs = virtio_ccw_del_vqs,
1085 .bus_name = virtio_ccw_bus_name,
1086 .synchronize_cbs = virtio_ccw_synchronize_cbs,
1091 * ccw bus driver related functions
1094 static void virtio_ccw_release_dev(struct device *_d)
1096 struct virtio_device *dev = dev_to_virtio(_d);
1097 struct virtio_ccw_device *vcdev = to_vc_device(dev);
1099 ccw_device_dma_free(vcdev->cdev, vcdev->dma_area,
1100 sizeof(*vcdev->dma_area));
1104 static int irb_is_error(struct irb *irb)
1106 if (scsw_cstat(&irb->scsw) != 0)
1108 if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
1110 if (scsw_cc(&irb->scsw) != 0)
1115 static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev,
1118 struct virtio_ccw_vq_info *info;
1119 unsigned long flags;
1120 struct virtqueue *vq;
1123 spin_lock_irqsave(&vcdev->lock, flags);
1124 list_for_each_entry(info, &vcdev->virtqueues, node) {
1125 if (info->vq->index == index) {
1130 spin_unlock_irqrestore(&vcdev->lock, flags);
1134 static void virtio_ccw_check_activity(struct virtio_ccw_device *vcdev,
1137 if (vcdev->curr_io & activity) {
1139 case VIRTIO_CCW_DOING_READ_FEAT:
1140 case VIRTIO_CCW_DOING_WRITE_FEAT:
1141 case VIRTIO_CCW_DOING_READ_CONFIG:
1142 case VIRTIO_CCW_DOING_WRITE_CONFIG:
1143 case VIRTIO_CCW_DOING_WRITE_STATUS:
1144 case VIRTIO_CCW_DOING_READ_STATUS:
1145 case VIRTIO_CCW_DOING_SET_VQ:
1146 case VIRTIO_CCW_DOING_SET_IND:
1147 case VIRTIO_CCW_DOING_SET_CONF_IND:
1148 case VIRTIO_CCW_DOING_RESET:
1149 case VIRTIO_CCW_DOING_READ_VQ_CONF:
1150 case VIRTIO_CCW_DOING_SET_IND_ADAPTER:
1151 case VIRTIO_CCW_DOING_SET_VIRTIO_REV:
1152 vcdev->curr_io &= ~activity;
1153 wake_up(&vcdev->wait_q);
1156 /* don't know what to do... */
1157 dev_warn(&vcdev->cdev->dev,
1158 "Suspicious activity '%08x'\n", activity);
1165 static void virtio_ccw_int_handler(struct ccw_device *cdev,
1166 unsigned long intparm,
1169 __u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK;
1170 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1172 struct virtqueue *vq;
1177 vcdev->err = PTR_ERR(irb);
1178 virtio_ccw_check_activity(vcdev, activity);
1179 /* Don't poke around indicators, something's wrong. */
1182 /* Check if it's a notification from the host. */
1183 if ((intparm == 0) &&
1184 (scsw_stctl(&irb->scsw) ==
1185 (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) {
1188 if (irb_is_error(irb)) {
1189 /* Command reject? */
1190 if ((scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) &&
1191 (irb->ecw[0] & SNS0_CMD_REJECT))
1192 vcdev->err = -EOPNOTSUPP;
1194 /* Map everything else to -EIO. */
1197 virtio_ccw_check_activity(vcdev, activity);
1198 #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
1200 * Paired with virtio_ccw_synchronize_cbs() and interrupts are
1203 read_lock(&vcdev->irq_lock);
1205 for_each_set_bit(i, indicators(vcdev),
1206 sizeof(*indicators(vcdev)) * BITS_PER_BYTE) {
1207 /* The bit clear must happen before the vring kick. */
1208 clear_bit(i, indicators(vcdev));
1210 vq = virtio_ccw_vq_by_ind(vcdev, i);
1211 vring_interrupt(0, vq);
1213 #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
1214 read_unlock(&vcdev->irq_lock);
1216 if (test_bit(0, indicators2(vcdev))) {
1217 virtio_config_changed(&vcdev->vdev);
1218 clear_bit(0, indicators2(vcdev));
1223 * We usually want to autoonline all devices, but give the admin
1224 * a way to exempt devices from this.
1226 #define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
1228 static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS];
1230 static char *no_auto = "";
1232 module_param(no_auto, charp, 0444);
1233 MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined");
1235 static int virtio_ccw_check_autoonline(struct ccw_device *cdev)
1237 struct ccw_dev_id id;
1239 ccw_device_get_id(cdev, &id);
1240 if (test_bit(id.devno, devs_no_auto[id.ssid]))
1245 static void virtio_ccw_auto_online(void *data, async_cookie_t cookie)
1247 struct ccw_device *cdev = data;
1250 ret = ccw_device_set_online(cdev);
1252 dev_warn(&cdev->dev, "Failed to set online: %d\n", ret);
1255 static int virtio_ccw_probe(struct ccw_device *cdev)
1257 cdev->handler = virtio_ccw_int_handler;
1259 if (virtio_ccw_check_autoonline(cdev))
1260 async_schedule(virtio_ccw_auto_online, cdev);
1264 static struct virtio_ccw_device *virtio_grab_drvdata(struct ccw_device *cdev)
1266 unsigned long flags;
1267 struct virtio_ccw_device *vcdev;
1269 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1270 vcdev = dev_get_drvdata(&cdev->dev);
1271 if (!vcdev || vcdev->going_away) {
1272 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1275 vcdev->going_away = true;
1276 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1280 static void virtio_ccw_remove(struct ccw_device *cdev)
1282 unsigned long flags;
1283 struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
1285 if (vcdev && cdev->online) {
1286 if (vcdev->device_lost)
1287 virtio_break_device(&vcdev->vdev);
1288 unregister_virtio_device(&vcdev->vdev);
1289 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1290 dev_set_drvdata(&cdev->dev, NULL);
1291 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1293 cdev->handler = NULL;
1296 static int virtio_ccw_offline(struct ccw_device *cdev)
1298 unsigned long flags;
1299 struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
1303 if (vcdev->device_lost)
1304 virtio_break_device(&vcdev->vdev);
1305 unregister_virtio_device(&vcdev->vdev);
1306 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1307 dev_set_drvdata(&cdev->dev, NULL);
1308 cdev->dev.dma_parms = NULL;
1309 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1313 static int virtio_ccw_set_transport_rev(struct virtio_ccw_device *vcdev)
1315 struct virtio_rev_info *rev;
1319 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
1322 rev = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*rev), &ccw->cda);
1324 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
1328 /* Set transport revision */
1329 ccw->cmd_code = CCW_CMD_SET_VIRTIO_REV;
1331 ccw->count = sizeof(*rev);
1333 vcdev->revision = VIRTIO_CCW_REV_MAX;
1335 rev->revision = vcdev->revision;
1336 /* none of our supported revisions carry payload */
1338 ret = ccw_io_helper(vcdev, ccw,
1339 VIRTIO_CCW_DOING_SET_VIRTIO_REV);
1340 if (ret == -EOPNOTSUPP) {
1341 if (vcdev->revision == 0)
1343 * The host device does not support setting
1344 * the revision: let's operate it in legacy
1351 } while (ret == -EOPNOTSUPP);
1353 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
1354 ccw_device_dma_free(vcdev->cdev, rev, sizeof(*rev));
1358 static int virtio_ccw_online(struct ccw_device *cdev)
1361 struct virtio_ccw_device *vcdev;
1362 unsigned long flags;
1364 vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL);
1366 dev_warn(&cdev->dev, "Could not get memory for virtio\n");
1370 vcdev->vdev.dev.parent = &cdev->dev;
1372 cdev->dev.dma_parms = &vcdev->dma_parms;
1373 vcdev->dma_area = ccw_device_dma_zalloc(vcdev->cdev,
1374 sizeof(*vcdev->dma_area),
1375 &vcdev->dma_area_addr);
1376 if (!vcdev->dma_area) {
1381 vcdev->is_thinint = virtio_ccw_use_airq; /* at least try */
1383 vcdev->vdev.dev.release = virtio_ccw_release_dev;
1384 vcdev->vdev.config = &virtio_ccw_config_ops;
1385 init_waitqueue_head(&vcdev->wait_q);
1386 INIT_LIST_HEAD(&vcdev->virtqueues);
1387 spin_lock_init(&vcdev->lock);
1388 rwlock_init(&vcdev->irq_lock);
1389 mutex_init(&vcdev->io_lock);
1391 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1392 dev_set_drvdata(&cdev->dev, vcdev);
1393 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1394 vcdev->vdev.id.vendor = cdev->id.cu_type;
1395 vcdev->vdev.id.device = cdev->id.cu_model;
1397 ret = virtio_ccw_set_transport_rev(vcdev);
1401 ret = register_virtio_device(&vcdev->vdev);
1403 dev_warn(&cdev->dev, "Failed to register virtio device: %d\n",
1409 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1410 dev_set_drvdata(&cdev->dev, NULL);
1411 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1412 put_device(&vcdev->vdev.dev);
1416 ccw_device_dma_free(vcdev->cdev, vcdev->dma_area,
1417 sizeof(*vcdev->dma_area));
1423 static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event)
1426 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1429 * Make sure vcdev is set
1430 * i.e. set_offline/remove callback not already running
1437 vcdev->device_lost = true;
1450 static struct ccw_device_id virtio_ids[] = {
1451 { CCW_DEVICE(0x3832, 0) },
1455 static struct ccw_driver virtio_ccw_driver = {
1457 .owner = THIS_MODULE,
1458 .name = "virtio_ccw",
1461 .probe = virtio_ccw_probe,
1462 .remove = virtio_ccw_remove,
1463 .set_offline = virtio_ccw_offline,
1464 .set_online = virtio_ccw_online,
1465 .notify = virtio_ccw_cio_notify,
1466 .int_class = IRQIO_VIR,
1469 static int __init pure_hex(char **cp, unsigned int *val, int min_digit,
1470 int max_digit, int max_val)
1477 while (diff <= max_digit) {
1478 int value = hex_to_bin(**cp);
1482 *val = *val * 16 + value;
1487 if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
1493 static int __init parse_busid(char *str, unsigned int *cssid,
1494 unsigned int *ssid, unsigned int *devno)
1505 ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
1506 if (ret || (str_work[0] != '.'))
1509 ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
1510 if (ret || (str_work[0] != '.'))
1513 ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
1514 if (ret || (str_work[0] != '\0'))
1522 static void __init no_auto_parse(void)
1524 unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
1529 while ((parm = strsep(&str, ","))) {
1530 rc = parse_busid(strsep(&parm, "-"), &from_cssid,
1535 rc = parse_busid(parm, &to_cssid,
1537 if ((from_ssid > to_ssid) ||
1538 ((from_ssid == to_ssid) && (from > to)))
1541 to_cssid = from_cssid;
1542 to_ssid = from_ssid;
1547 while ((from_ssid < to_ssid) ||
1548 ((from_ssid == to_ssid) && (from <= to))) {
1549 set_bit(from, devs_no_auto[from_ssid]);
1551 if (from > __MAX_SUBCHANNEL) {
1559 static int __init virtio_ccw_init(void)
1563 /* parse no_auto string before we do anything further */
1566 summary_indicators = cio_dma_zalloc(MAX_AIRQ_AREAS);
1567 if (!summary_indicators)
1569 rc = ccw_driver_register(&virtio_ccw_driver);
1571 cio_dma_free(summary_indicators, MAX_AIRQ_AREAS);
1574 device_initcall(virtio_ccw_init);