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>
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)
305 int i, j, queue_idx, highest_queue_idx = -1;
306 struct airq_info *info;
307 unsigned long *indicator_addr = NULL;
308 unsigned long bit, flags;
310 /* Array entries without an actual queue pointer must be ignored. */
311 for (i = 0; i < nvqs; i++) {
316 for (i = 0; i < MAX_AIRQ_AREAS && !indicator_addr; i++) {
317 mutex_lock(&airq_areas_lock);
319 airq_areas[i] = new_airq_info(i);
320 info = airq_areas[i];
321 mutex_unlock(&airq_areas_lock);
324 write_lock_irqsave(&info->lock, flags);
325 bit = airq_iv_alloc(info->aiv, highest_queue_idx + 1);
327 /* Not enough vacancies. */
328 write_unlock_irqrestore(&info->lock, flags);
333 indicator_addr = info->aiv->vector;
334 for (j = 0, queue_idx = 0; j < nvqs; j++) {
337 airq_iv_set_ptr(info->aiv, bit + queue_idx++,
338 (unsigned long)vqs[j]);
340 write_unlock_irqrestore(&info->lock, flags);
342 return indicator_addr;
345 static void virtio_ccw_drop_indicators(struct virtio_ccw_device *vcdev)
347 struct virtio_ccw_vq_info *info;
349 if (!vcdev->airq_info)
351 list_for_each_entry(info, &vcdev->virtqueues, node)
352 drop_airq_indicator(info->vq, vcdev->airq_info);
355 static int doing_io(struct virtio_ccw_device *vcdev, __u32 flag)
360 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
364 ret = vcdev->curr_io & flag;
365 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
369 static int ccw_io_helper(struct virtio_ccw_device *vcdev,
370 struct ccw1 *ccw, __u32 intparm)
374 int flag = intparm & VIRTIO_CCW_INTPARM_MASK;
376 mutex_lock(&vcdev->io_lock);
378 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
379 ret = ccw_device_start(vcdev->cdev, ccw, intparm, 0, 0);
383 vcdev->curr_io |= flag;
385 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
387 } while (ret == -EBUSY);
388 wait_event(vcdev->wait_q, doing_io(vcdev, flag) == 0);
389 ret = ret ? ret : vcdev->err;
390 mutex_unlock(&vcdev->io_lock);
394 static void virtio_ccw_drop_indicator(struct virtio_ccw_device *vcdev,
398 struct virtio_thinint_area *thinint_area = NULL;
399 struct airq_info *airq_info = vcdev->airq_info;
400 dma64_t *indicatorp = NULL;
402 if (vcdev->is_thinint) {
403 thinint_area = ccw_device_dma_zalloc(vcdev->cdev,
404 sizeof(*thinint_area),
408 thinint_area->summary_indicator =
409 get_summary_indicator_dma(airq_info);
410 thinint_area->isc = VIRTIO_AIRQ_ISC;
411 ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
412 ccw->count = sizeof(*thinint_area);
414 /* payload is the address of the indicators */
415 indicatorp = ccw_device_dma_zalloc(vcdev->cdev,
421 ccw->cmd_code = CCW_CMD_SET_IND;
422 ccw->count = sizeof(*indicatorp);
424 /* Deregister indicators from host. */
425 *indicators(vcdev) = 0;
427 ret = ccw_io_helper(vcdev, ccw,
429 VIRTIO_CCW_DOING_SET_IND_ADAPTER :
430 VIRTIO_CCW_DOING_SET_IND);
431 if (ret && (ret != -ENODEV))
432 dev_info(&vcdev->cdev->dev,
433 "Failed to deregister indicators (%d)\n", ret);
434 else if (vcdev->is_thinint)
435 virtio_ccw_drop_indicators(vcdev);
436 ccw_device_dma_free(vcdev->cdev, indicatorp, sizeof(*indicatorp));
437 ccw_device_dma_free(vcdev->cdev, thinint_area, sizeof(*thinint_area));
440 static inline bool virtio_ccw_do_kvm_notify(struct virtqueue *vq, u32 data)
442 struct virtio_ccw_vq_info *info = vq->priv;
443 struct virtio_ccw_device *vcdev;
444 struct subchannel_id schid;
446 vcdev = to_vc_device(info->vq->vdev);
447 ccw_device_get_schid(vcdev->cdev, &schid);
448 BUILD_BUG_ON(sizeof(struct subchannel_id) != sizeof(unsigned int));
449 info->cookie = kvm_hypercall3(KVM_S390_VIRTIO_CCW_NOTIFY,
450 *((unsigned int *)&schid),
452 if (info->cookie < 0)
457 static bool virtio_ccw_kvm_notify(struct virtqueue *vq)
459 return virtio_ccw_do_kvm_notify(vq, vq->index);
462 static bool virtio_ccw_kvm_notify_with_data(struct virtqueue *vq)
464 return virtio_ccw_do_kvm_notify(vq, vring_notification_data(vq));
467 static int virtio_ccw_read_vq_conf(struct virtio_ccw_device *vcdev,
468 struct ccw1 *ccw, int index)
472 vcdev->dma_area->config_block.index = index;
473 ccw->cmd_code = CCW_CMD_READ_VQ_CONF;
475 ccw->count = sizeof(struct vq_config_block);
476 ccw->cda = config_block_dma(vcdev);
477 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_VQ_CONF);
480 return vcdev->dma_area->config_block.num ?: -ENOENT;
483 static void virtio_ccw_del_vq(struct virtqueue *vq, struct ccw1 *ccw)
485 struct virtio_ccw_device *vcdev = to_vc_device(vq->vdev);
486 struct virtio_ccw_vq_info *info = vq->priv;
489 unsigned int index = vq->index;
491 /* Remove from our list. */
492 spin_lock_irqsave(&vcdev->lock, flags);
493 list_del(&info->node);
494 spin_unlock_irqrestore(&vcdev->lock, flags);
496 /* Release from host. */
497 if (vcdev->revision == 0) {
498 info->info_block->l.queue = 0;
499 info->info_block->l.align = 0;
500 info->info_block->l.index = index;
501 info->info_block->l.num = 0;
502 ccw->count = sizeof(info->info_block->l);
504 info->info_block->s.desc = 0;
505 info->info_block->s.index = index;
506 info->info_block->s.num = 0;
507 info->info_block->s.avail = 0;
508 info->info_block->s.used = 0;
509 ccw->count = sizeof(info->info_block->s);
511 ccw->cmd_code = CCW_CMD_SET_VQ;
513 ccw->cda = info->info_block_addr;
514 ret = ccw_io_helper(vcdev, ccw,
515 VIRTIO_CCW_DOING_SET_VQ | index);
517 * -ENODEV isn't considered an error: The device is gone anyway.
518 * This may happen on device detach.
520 if (ret && (ret != -ENODEV))
521 dev_warn(&vq->vdev->dev, "Error %d while deleting queue %d\n",
524 vring_del_virtqueue(vq);
525 ccw_device_dma_free(vcdev->cdev, info->info_block,
526 sizeof(*info->info_block));
530 static void virtio_ccw_del_vqs(struct virtio_device *vdev)
532 struct virtqueue *vq, *n;
534 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
536 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
540 virtio_ccw_drop_indicator(vcdev, ccw);
542 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
543 virtio_ccw_del_vq(vq, ccw);
545 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
548 static struct virtqueue *virtio_ccw_setup_vq(struct virtio_device *vdev,
549 int i, vq_callback_t *callback,
550 const char *name, bool ctx,
553 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
554 bool (*notify)(struct virtqueue *vq);
556 struct virtqueue *vq = NULL;
557 struct virtio_ccw_vq_info *info;
562 if (__virtio_test_bit(vdev, VIRTIO_F_NOTIFICATION_DATA))
563 notify = virtio_ccw_kvm_notify_with_data;
565 notify = virtio_ccw_kvm_notify;
567 /* Allocate queue. */
568 info = kzalloc(sizeof(struct virtio_ccw_vq_info), GFP_KERNEL);
570 dev_warn(&vcdev->cdev->dev, "no info\n");
574 info->info_block = ccw_device_dma_zalloc(vcdev->cdev,
575 sizeof(*info->info_block),
576 &info->info_block_addr);
577 if (!info->info_block) {
578 dev_warn(&vcdev->cdev->dev, "no info block\n");
582 info->num = virtio_ccw_read_vq_conf(vcdev, ccw, i);
587 may_reduce = vcdev->revision > 0;
588 vq = vring_create_virtqueue(i, info->num, KVM_VIRTIO_CCW_RING_ALIGN,
589 vdev, true, may_reduce, ctx,
590 notify, callback, name);
593 /* For now, we fail if we can't get the requested size. */
594 dev_warn(&vcdev->cdev->dev, "no vq\n");
599 vq->num_max = info->num;
601 /* it may have been reduced */
602 info->num = virtqueue_get_vring_size(vq);
604 /* Register it with the host. */
605 queue = virtqueue_get_desc_addr(vq);
606 if (vcdev->revision == 0) {
607 info->info_block->l.queue = u64_to_dma64(queue);
608 info->info_block->l.align = KVM_VIRTIO_CCW_RING_ALIGN;
609 info->info_block->l.index = i;
610 info->info_block->l.num = info->num;
611 ccw->count = sizeof(info->info_block->l);
613 info->info_block->s.desc = u64_to_dma64(queue);
614 info->info_block->s.index = i;
615 info->info_block->s.num = info->num;
616 info->info_block->s.avail = u64_to_dma64(virtqueue_get_avail_addr(vq));
617 info->info_block->s.used = u64_to_dma64(virtqueue_get_used_addr(vq));
618 ccw->count = sizeof(info->info_block->s);
620 ccw->cmd_code = CCW_CMD_SET_VQ;
622 ccw->cda = info->info_block_addr;
623 err = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_VQ | i);
625 dev_warn(&vcdev->cdev->dev, "SET_VQ failed\n");
632 /* Save it to our list. */
633 spin_lock_irqsave(&vcdev->lock, flags);
634 list_add(&info->node, &vcdev->virtqueues);
635 spin_unlock_irqrestore(&vcdev->lock, flags);
641 vring_del_virtqueue(vq);
643 ccw_device_dma_free(vcdev->cdev, info->info_block,
644 sizeof(*info->info_block));
650 static int virtio_ccw_register_adapter_ind(struct virtio_ccw_device *vcdev,
651 struct virtqueue *vqs[], int nvqs,
655 struct virtio_thinint_area *thinint_area = NULL;
656 unsigned long *indicator_addr;
657 struct airq_info *info;
659 thinint_area = ccw_device_dma_zalloc(vcdev->cdev,
660 sizeof(*thinint_area),
666 /* Try to get an indicator. */
667 indicator_addr = get_airq_indicator(vqs, nvqs,
668 &thinint_area->bit_nr,
670 if (!indicator_addr) {
674 thinint_area->indicator = virt_to_dma64(indicator_addr);
675 info = vcdev->airq_info;
676 thinint_area->summary_indicator = get_summary_indicator_dma(info);
677 thinint_area->isc = VIRTIO_AIRQ_ISC;
678 ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
679 ccw->flags = CCW_FLAG_SLI;
680 ccw->count = sizeof(*thinint_area);
681 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND_ADAPTER);
683 if (ret == -EOPNOTSUPP) {
685 * The host does not support adapter interrupts
686 * for virtio-ccw, stop trying.
688 virtio_ccw_use_airq = 0;
689 pr_info("Adapter interrupts unsupported on host\n");
691 dev_warn(&vcdev->cdev->dev,
692 "enabling adapter interrupts = %d\n", ret);
693 virtio_ccw_drop_indicators(vcdev);
696 ccw_device_dma_free(vcdev->cdev, thinint_area, sizeof(*thinint_area));
700 static int virtio_ccw_find_vqs(struct virtio_device *vdev, unsigned nvqs,
701 struct virtqueue *vqs[],
702 struct virtqueue_info vqs_info[],
703 struct irq_affinity *desc)
705 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
706 dma64_t *indicatorp = NULL;
707 int ret, i, queue_idx = 0;
709 dma32_t indicatorp_dma = 0;
711 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
715 for (i = 0; i < nvqs; ++i) {
716 struct virtqueue_info *vqi = &vqs_info[i];
723 vqs[i] = virtio_ccw_setup_vq(vdev, queue_idx++, vqi->callback,
724 vqi->name, vqi->ctx, ccw);
725 if (IS_ERR(vqs[i])) {
726 ret = PTR_ERR(vqs[i]);
733 * We need a data area under 2G to communicate. Our payload is
734 * the address of the indicators.
736 indicatorp = ccw_device_dma_zalloc(vcdev->cdev,
741 *indicatorp = indicators_dma(vcdev);
742 if (vcdev->is_thinint) {
743 ret = virtio_ccw_register_adapter_ind(vcdev, vqs, nvqs, ccw);
745 /* no error, just fall back to legacy interrupts */
746 vcdev->is_thinint = false;
748 ccw->cda = indicatorp_dma;
749 if (!vcdev->is_thinint) {
750 /* Register queue indicators with host. */
751 *indicators(vcdev) = 0;
752 ccw->cmd_code = CCW_CMD_SET_IND;
754 ccw->count = sizeof(*indicatorp);
755 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND);
759 /* Register indicators2 with host for config changes */
760 *indicatorp = indicators2_dma(vcdev);
761 *indicators2(vcdev) = 0;
762 ccw->cmd_code = CCW_CMD_SET_CONF_IND;
764 ccw->count = sizeof(*indicatorp);
765 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_CONF_IND);
770 ccw_device_dma_free(vcdev->cdev, indicatorp,
771 sizeof(*indicatorp));
772 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
776 ccw_device_dma_free(vcdev->cdev, indicatorp,
777 sizeof(*indicatorp));
778 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
779 virtio_ccw_del_vqs(vdev);
783 static void virtio_ccw_reset(struct virtio_device *vdev)
785 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
788 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
792 /* Zero status bits. */
793 vcdev->dma_area->status = 0;
795 /* Send a reset ccw on device. */
796 ccw->cmd_code = CCW_CMD_VDEV_RESET;
800 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_RESET);
801 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
804 static u64 virtio_ccw_get_features(struct virtio_device *vdev)
806 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
807 struct virtio_feature_desc *features;
812 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
816 features = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*features),
822 /* Read the feature bits from the host. */
824 ccw->cmd_code = CCW_CMD_READ_FEAT;
826 ccw->count = sizeof(*features);
827 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
833 rc = le32_to_cpu(features->features);
835 if (vcdev->revision == 0)
838 /* Read second half of the feature bits from the host. */
840 ccw->cmd_code = CCW_CMD_READ_FEAT;
842 ccw->count = sizeof(*features);
843 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
845 rc |= (u64)le32_to_cpu(features->features) << 32;
848 ccw_device_dma_free(vcdev->cdev, features, sizeof(*features));
849 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
853 static void ccw_transport_features(struct virtio_device *vdev)
856 * Currently nothing to do here.
860 static int virtio_ccw_finalize_features(struct virtio_device *vdev)
862 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
863 struct virtio_feature_desc *features;
867 if (vcdev->revision >= 1 &&
868 !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
869 dev_err(&vdev->dev, "virtio: device uses revision 1 "
870 "but does not have VIRTIO_F_VERSION_1\n");
874 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
878 features = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*features),
884 /* Give virtio_ring a chance to accept features. */
885 vring_transport_features(vdev);
887 /* Give virtio_ccw a chance to accept features. */
888 ccw_transport_features(vdev);
891 features->features = cpu_to_le32((u32)vdev->features);
892 /* Write the first half of the feature bits to the host. */
893 ccw->cmd_code = CCW_CMD_WRITE_FEAT;
895 ccw->count = sizeof(*features);
896 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
900 if (vcdev->revision == 0)
904 features->features = cpu_to_le32(vdev->features >> 32);
905 /* Write the second half of the feature bits to the host. */
906 ccw->cmd_code = CCW_CMD_WRITE_FEAT;
908 ccw->count = sizeof(*features);
909 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
912 ccw_device_dma_free(vcdev->cdev, features, sizeof(*features));
913 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
918 static void virtio_ccw_get_config(struct virtio_device *vdev,
919 unsigned int offset, void *buf, unsigned len)
921 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
927 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
931 config_area = ccw_device_dma_zalloc(vcdev->cdev,
932 VIRTIO_CCW_CONFIG_SIZE,
937 /* Read the config area from the host. */
938 ccw->cmd_code = CCW_CMD_READ_CONF;
940 ccw->count = offset + len;
941 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG);
945 spin_lock_irqsave(&vcdev->lock, flags);
946 memcpy(vcdev->config, config_area, offset + len);
947 if (vcdev->config_ready < offset + len)
948 vcdev->config_ready = offset + len;
949 spin_unlock_irqrestore(&vcdev->lock, flags);
951 memcpy(buf, config_area + offset, len);
954 ccw_device_dma_free(vcdev->cdev, config_area, VIRTIO_CCW_CONFIG_SIZE);
955 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
958 static void virtio_ccw_set_config(struct virtio_device *vdev,
959 unsigned int offset, const void *buf,
962 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
967 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
971 config_area = ccw_device_dma_zalloc(vcdev->cdev,
972 VIRTIO_CCW_CONFIG_SIZE,
977 /* Make sure we don't overwrite fields. */
978 if (vcdev->config_ready < offset)
979 virtio_ccw_get_config(vdev, 0, NULL, offset);
980 spin_lock_irqsave(&vcdev->lock, flags);
981 memcpy(&vcdev->config[offset], buf, len);
982 /* Write the config area to the host. */
983 memcpy(config_area, vcdev->config, sizeof(vcdev->config));
984 spin_unlock_irqrestore(&vcdev->lock, flags);
985 ccw->cmd_code = CCW_CMD_WRITE_CONF;
987 ccw->count = offset + len;
988 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG);
991 ccw_device_dma_free(vcdev->cdev, config_area, VIRTIO_CCW_CONFIG_SIZE);
992 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
995 static u8 virtio_ccw_get_status(struct virtio_device *vdev)
997 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
998 u8 old_status = vcdev->dma_area->status;
1001 if (vcdev->revision < 2)
1002 return vcdev->dma_area->status;
1004 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
1008 ccw->cmd_code = CCW_CMD_READ_STATUS;
1010 ccw->count = sizeof(vcdev->dma_area->status);
1011 ccw->cda = status_dma(vcdev);
1012 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_STATUS);
1014 * If the channel program failed (should only happen if the device
1015 * was hotunplugged, and then we clean up via the machine check
1016 * handler anyway), vcdev->dma_area->status was not overwritten and we just
1017 * return the old status, which is fine.
1019 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
1021 return vcdev->dma_area->status;
1024 static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status)
1026 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
1027 u8 old_status = vcdev->dma_area->status;
1031 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
1035 /* Write the status to the host. */
1036 vcdev->dma_area->status = status;
1037 ccw->cmd_code = CCW_CMD_WRITE_STATUS;
1039 ccw->count = sizeof(status);
1040 /* We use ssch for setting the status which is a serializing
1041 * instruction that guarantees the memory writes have
1042 * completed before ssch.
1044 ccw->cda = status_dma(vcdev);
1045 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS);
1046 /* Write failed? We assume status is unchanged. */
1048 vcdev->dma_area->status = old_status;
1049 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
1052 static const char *virtio_ccw_bus_name(struct virtio_device *vdev)
1054 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
1056 return dev_name(&vcdev->cdev->dev);
1059 static void virtio_ccw_synchronize_cbs(struct virtio_device *vdev)
1061 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
1062 struct airq_info *info = vcdev->airq_info;
1066 * This device uses adapter interrupts: synchronize with
1067 * vring_interrupt() called by virtio_airq_handler()
1068 * via the indicator area lock.
1070 write_lock_irq(&info->lock);
1071 write_unlock_irq(&info->lock);
1073 /* This device uses classic interrupts: synchronize
1074 * with vring_interrupt() called by
1075 * virtio_ccw_int_handler() via the per-device
1078 write_lock_irq(&vcdev->irq_lock);
1079 write_unlock_irq(&vcdev->irq_lock);
1083 static const struct virtio_config_ops virtio_ccw_config_ops = {
1084 .get_features = virtio_ccw_get_features,
1085 .finalize_features = virtio_ccw_finalize_features,
1086 .get = virtio_ccw_get_config,
1087 .set = virtio_ccw_set_config,
1088 .get_status = virtio_ccw_get_status,
1089 .set_status = virtio_ccw_set_status,
1090 .reset = virtio_ccw_reset,
1091 .find_vqs = virtio_ccw_find_vqs,
1092 .del_vqs = virtio_ccw_del_vqs,
1093 .bus_name = virtio_ccw_bus_name,
1094 .synchronize_cbs = virtio_ccw_synchronize_cbs,
1099 * ccw bus driver related functions
1102 static void virtio_ccw_release_dev(struct device *_d)
1104 struct virtio_device *dev = dev_to_virtio(_d);
1105 struct virtio_ccw_device *vcdev = to_vc_device(dev);
1107 ccw_device_dma_free(vcdev->cdev, vcdev->dma_area,
1108 sizeof(*vcdev->dma_area));
1112 static int irb_is_error(struct irb *irb)
1114 if (scsw_cstat(&irb->scsw) != 0)
1116 if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
1118 if (scsw_cc(&irb->scsw) != 0)
1123 static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev,
1126 struct virtio_ccw_vq_info *info;
1127 unsigned long flags;
1128 struct virtqueue *vq;
1131 spin_lock_irqsave(&vcdev->lock, flags);
1132 list_for_each_entry(info, &vcdev->virtqueues, node) {
1133 if (info->vq->index == index) {
1138 spin_unlock_irqrestore(&vcdev->lock, flags);
1142 static void virtio_ccw_check_activity(struct virtio_ccw_device *vcdev,
1145 if (vcdev->curr_io & activity) {
1147 case VIRTIO_CCW_DOING_READ_FEAT:
1148 case VIRTIO_CCW_DOING_WRITE_FEAT:
1149 case VIRTIO_CCW_DOING_READ_CONFIG:
1150 case VIRTIO_CCW_DOING_WRITE_CONFIG:
1151 case VIRTIO_CCW_DOING_WRITE_STATUS:
1152 case VIRTIO_CCW_DOING_READ_STATUS:
1153 case VIRTIO_CCW_DOING_SET_VQ:
1154 case VIRTIO_CCW_DOING_SET_IND:
1155 case VIRTIO_CCW_DOING_SET_CONF_IND:
1156 case VIRTIO_CCW_DOING_RESET:
1157 case VIRTIO_CCW_DOING_READ_VQ_CONF:
1158 case VIRTIO_CCW_DOING_SET_IND_ADAPTER:
1159 case VIRTIO_CCW_DOING_SET_VIRTIO_REV:
1160 vcdev->curr_io &= ~activity;
1161 wake_up(&vcdev->wait_q);
1164 /* don't know what to do... */
1165 dev_warn(&vcdev->cdev->dev,
1166 "Suspicious activity '%08x'\n", activity);
1173 static void virtio_ccw_int_handler(struct ccw_device *cdev,
1174 unsigned long intparm,
1177 __u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK;
1178 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1180 struct virtqueue *vq;
1185 vcdev->err = PTR_ERR(irb);
1186 virtio_ccw_check_activity(vcdev, activity);
1187 /* Don't poke around indicators, something's wrong. */
1190 /* Check if it's a notification from the host. */
1191 if ((intparm == 0) &&
1192 (scsw_stctl(&irb->scsw) ==
1193 (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) {
1196 if (irb_is_error(irb)) {
1197 /* Command reject? */
1198 if ((scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) &&
1199 (irb->ecw[0] & SNS0_CMD_REJECT))
1200 vcdev->err = -EOPNOTSUPP;
1202 /* Map everything else to -EIO. */
1205 virtio_ccw_check_activity(vcdev, activity);
1206 #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
1208 * Paired with virtio_ccw_synchronize_cbs() and interrupts are
1211 read_lock(&vcdev->irq_lock);
1213 for_each_set_bit(i, indicators(vcdev),
1214 sizeof(*indicators(vcdev)) * BITS_PER_BYTE) {
1215 /* The bit clear must happen before the vring kick. */
1216 clear_bit(i, indicators(vcdev));
1218 vq = virtio_ccw_vq_by_ind(vcdev, i);
1219 vring_interrupt(0, vq);
1221 #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
1222 read_unlock(&vcdev->irq_lock);
1224 if (test_bit(0, indicators2(vcdev))) {
1225 virtio_config_changed(&vcdev->vdev);
1226 clear_bit(0, indicators2(vcdev));
1231 * We usually want to autoonline all devices, but give the admin
1232 * a way to exempt devices from this.
1234 #define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
1236 static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS];
1238 static char *no_auto = "";
1240 module_param(no_auto, charp, 0444);
1241 MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined");
1243 static int virtio_ccw_check_autoonline(struct ccw_device *cdev)
1245 struct ccw_dev_id id;
1247 ccw_device_get_id(cdev, &id);
1248 if (test_bit(id.devno, devs_no_auto[id.ssid]))
1253 static void virtio_ccw_auto_online(void *data, async_cookie_t cookie)
1255 struct ccw_device *cdev = data;
1258 ret = ccw_device_set_online(cdev);
1260 dev_warn(&cdev->dev, "Failed to set online: %d\n", ret);
1263 static int virtio_ccw_probe(struct ccw_device *cdev)
1265 cdev->handler = virtio_ccw_int_handler;
1267 if (virtio_ccw_check_autoonline(cdev))
1268 async_schedule(virtio_ccw_auto_online, cdev);
1272 static struct virtio_ccw_device *virtio_grab_drvdata(struct ccw_device *cdev)
1274 unsigned long flags;
1275 struct virtio_ccw_device *vcdev;
1277 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1278 vcdev = dev_get_drvdata(&cdev->dev);
1279 if (!vcdev || vcdev->going_away) {
1280 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1283 vcdev->going_away = true;
1284 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1288 static void virtio_ccw_remove(struct ccw_device *cdev)
1290 unsigned long flags;
1291 struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
1293 if (vcdev && cdev->online) {
1294 if (vcdev->device_lost)
1295 virtio_break_device(&vcdev->vdev);
1296 unregister_virtio_device(&vcdev->vdev);
1297 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1298 dev_set_drvdata(&cdev->dev, NULL);
1299 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1301 cdev->handler = NULL;
1304 static int virtio_ccw_offline(struct ccw_device *cdev)
1306 unsigned long flags;
1307 struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
1311 if (vcdev->device_lost)
1312 virtio_break_device(&vcdev->vdev);
1313 unregister_virtio_device(&vcdev->vdev);
1314 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1315 dev_set_drvdata(&cdev->dev, NULL);
1316 cdev->dev.dma_parms = NULL;
1317 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1321 static int virtio_ccw_set_transport_rev(struct virtio_ccw_device *vcdev)
1323 struct virtio_rev_info *rev;
1327 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
1330 rev = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*rev), &ccw->cda);
1332 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
1336 /* Set transport revision */
1337 ccw->cmd_code = CCW_CMD_SET_VIRTIO_REV;
1339 ccw->count = sizeof(*rev);
1341 vcdev->revision = VIRTIO_CCW_REV_MAX;
1343 rev->revision = vcdev->revision;
1344 /* none of our supported revisions carry payload */
1346 ret = ccw_io_helper(vcdev, ccw,
1347 VIRTIO_CCW_DOING_SET_VIRTIO_REV);
1348 if (ret == -EOPNOTSUPP) {
1349 if (vcdev->revision == 0)
1351 * The host device does not support setting
1352 * the revision: let's operate it in legacy
1359 } while (ret == -EOPNOTSUPP);
1361 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
1362 ccw_device_dma_free(vcdev->cdev, rev, sizeof(*rev));
1366 static int virtio_ccw_online(struct ccw_device *cdev)
1369 struct virtio_ccw_device *vcdev;
1370 unsigned long flags;
1372 vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL);
1374 dev_warn(&cdev->dev, "Could not get memory for virtio\n");
1378 vcdev->vdev.dev.parent = &cdev->dev;
1380 cdev->dev.dma_parms = &vcdev->dma_parms;
1381 vcdev->dma_area = ccw_device_dma_zalloc(vcdev->cdev,
1382 sizeof(*vcdev->dma_area),
1383 &vcdev->dma_area_addr);
1384 if (!vcdev->dma_area) {
1389 vcdev->is_thinint = virtio_ccw_use_airq; /* at least try */
1391 vcdev->vdev.dev.release = virtio_ccw_release_dev;
1392 vcdev->vdev.config = &virtio_ccw_config_ops;
1393 init_waitqueue_head(&vcdev->wait_q);
1394 INIT_LIST_HEAD(&vcdev->virtqueues);
1395 spin_lock_init(&vcdev->lock);
1396 rwlock_init(&vcdev->irq_lock);
1397 mutex_init(&vcdev->io_lock);
1399 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1400 dev_set_drvdata(&cdev->dev, vcdev);
1401 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1402 vcdev->vdev.id.vendor = cdev->id.cu_type;
1403 vcdev->vdev.id.device = cdev->id.cu_model;
1405 ret = virtio_ccw_set_transport_rev(vcdev);
1409 ret = register_virtio_device(&vcdev->vdev);
1411 dev_warn(&cdev->dev, "Failed to register virtio device: %d\n",
1417 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1418 dev_set_drvdata(&cdev->dev, NULL);
1419 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1420 put_device(&vcdev->vdev.dev);
1424 ccw_device_dma_free(vcdev->cdev, vcdev->dma_area,
1425 sizeof(*vcdev->dma_area));
1431 static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event)
1434 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1437 * Make sure vcdev is set
1438 * i.e. set_offline/remove callback not already running
1445 vcdev->device_lost = true;
1458 static struct ccw_device_id virtio_ids[] = {
1459 { CCW_DEVICE(0x3832, 0) },
1463 static struct ccw_driver virtio_ccw_driver = {
1465 .owner = THIS_MODULE,
1466 .name = "virtio_ccw",
1469 .probe = virtio_ccw_probe,
1470 .remove = virtio_ccw_remove,
1471 .set_offline = virtio_ccw_offline,
1472 .set_online = virtio_ccw_online,
1473 .notify = virtio_ccw_cio_notify,
1474 .int_class = IRQIO_VIR,
1477 static int __init pure_hex(char **cp, unsigned int *val, int min_digit,
1478 int max_digit, int max_val)
1485 while (diff <= max_digit) {
1486 int value = hex_to_bin(**cp);
1490 *val = *val * 16 + value;
1495 if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
1501 static int __init parse_busid(char *str, unsigned int *cssid,
1502 unsigned int *ssid, unsigned int *devno)
1513 ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
1514 if (ret || (str_work[0] != '.'))
1517 ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
1518 if (ret || (str_work[0] != '.'))
1521 ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
1522 if (ret || (str_work[0] != '\0'))
1530 static void __init no_auto_parse(void)
1532 unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
1537 while ((parm = strsep(&str, ","))) {
1538 rc = parse_busid(strsep(&parm, "-"), &from_cssid,
1543 rc = parse_busid(parm, &to_cssid,
1545 if ((from_ssid > to_ssid) ||
1546 ((from_ssid == to_ssid) && (from > to)))
1549 to_cssid = from_cssid;
1550 to_ssid = from_ssid;
1555 while ((from_ssid < to_ssid) ||
1556 ((from_ssid == to_ssid) && (from <= to))) {
1557 set_bit(from, devs_no_auto[from_ssid]);
1559 if (from > __MAX_SUBCHANNEL) {
1567 static int __init virtio_ccw_init(void)
1571 /* parse no_auto string before we do anything further */
1574 summary_indicators = cio_dma_zalloc(MAX_AIRQ_AREAS);
1575 if (!summary_indicators)
1577 rc = ccw_driver_register(&virtio_ccw_driver);
1579 cio_dma_free(summary_indicators, MAX_AIRQ_AREAS);
1582 device_initcall(virtio_ccw_init);