1 // SPDX-License-Identifier: GPL-2.0
3 * ccw based virtio transport
5 * Copyright IBM Corp. 2012, 2014
7 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
10 #include <linux/kernel_stat.h>
11 #include <linux/init.h>
12 #include <linux/memblock.h>
13 #include <linux/err.h>
14 #include <linux/virtio.h>
15 #include <linux/virtio_config.h>
16 #include <linux/slab.h>
17 #include <linux/interrupt.h>
18 #include <linux/virtio_ring.h>
19 #include <linux/pfn.h>
20 #include <linux/async.h>
21 #include <linux/wait.h>
22 #include <linux/list.h>
23 #include <linux/bitops.h>
24 #include <linux/moduleparam.h>
26 #include <linux/kvm_para.h>
27 #include <linux/notifier.h>
29 #include <asm/setup.h>
32 #include <asm/ccwdev.h>
33 #include <asm/virtio-ccw.h>
38 * virtio related functions
41 struct vq_config_block {
46 #define VIRTIO_CCW_CONFIG_SIZE 0x100
47 /* same as PCI config space size, should be enough for all drivers */
49 struct vcdev_dma_area {
50 unsigned long indicators;
51 unsigned long indicators2;
52 struct vq_config_block config_block;
56 struct virtio_ccw_device {
57 struct virtio_device vdev;
58 __u8 config[VIRTIO_CCW_CONFIG_SIZE];
59 struct ccw_device *cdev;
62 unsigned int revision; /* Transport revision */
63 wait_queue_head_t wait_q;
65 struct mutex io_lock; /* Serializes I/O requests */
66 struct list_head virtqueues;
70 unsigned int config_ready;
72 struct vcdev_dma_area *dma_area;
75 static inline unsigned long *indicators(struct virtio_ccw_device *vcdev)
77 return &vcdev->dma_area->indicators;
80 static inline unsigned long *indicators2(struct virtio_ccw_device *vcdev)
82 return &vcdev->dma_area->indicators2;
85 struct vq_info_block_legacy {
92 struct vq_info_block {
101 struct virtio_feature_desc {
106 struct virtio_thinint_area {
107 unsigned long summary_indicator;
108 unsigned long indicator;
113 struct virtio_rev_info {
119 /* the highest virtio-ccw revision we support */
120 #define VIRTIO_CCW_REV_MAX 1
122 struct virtio_ccw_vq_info {
123 struct virtqueue *vq;
126 struct vq_info_block s;
127 struct vq_info_block_legacy l;
130 struct list_head node;
134 #define VIRTIO_AIRQ_ISC IO_SCH_ISC /* inherit from subchannel */
136 #define VIRTIO_IV_BITS (L1_CACHE_BYTES * 8)
137 #define MAX_AIRQ_AREAS 20
139 static int virtio_ccw_use_airq = 1;
143 u8 summary_indicator_idx;
144 struct airq_struct airq;
147 static struct airq_info *airq_areas[MAX_AIRQ_AREAS];
148 static u8 *summary_indicators;
150 static inline u8 *get_summary_indicator(struct airq_info *info)
152 return summary_indicators + info->summary_indicator_idx;
155 #define CCW_CMD_SET_VQ 0x13
156 #define CCW_CMD_VDEV_RESET 0x33
157 #define CCW_CMD_SET_IND 0x43
158 #define CCW_CMD_SET_CONF_IND 0x53
159 #define CCW_CMD_READ_FEAT 0x12
160 #define CCW_CMD_WRITE_FEAT 0x11
161 #define CCW_CMD_READ_CONF 0x22
162 #define CCW_CMD_WRITE_CONF 0x21
163 #define CCW_CMD_WRITE_STATUS 0x31
164 #define CCW_CMD_READ_VQ_CONF 0x32
165 #define CCW_CMD_READ_STATUS 0x72
166 #define CCW_CMD_SET_IND_ADAPTER 0x73
167 #define CCW_CMD_SET_VIRTIO_REV 0x83
169 #define VIRTIO_CCW_DOING_SET_VQ 0x00010000
170 #define VIRTIO_CCW_DOING_RESET 0x00040000
171 #define VIRTIO_CCW_DOING_READ_FEAT 0x00080000
172 #define VIRTIO_CCW_DOING_WRITE_FEAT 0x00100000
173 #define VIRTIO_CCW_DOING_READ_CONFIG 0x00200000
174 #define VIRTIO_CCW_DOING_WRITE_CONFIG 0x00400000
175 #define VIRTIO_CCW_DOING_WRITE_STATUS 0x00800000
176 #define VIRTIO_CCW_DOING_SET_IND 0x01000000
177 #define VIRTIO_CCW_DOING_READ_VQ_CONF 0x02000000
178 #define VIRTIO_CCW_DOING_SET_CONF_IND 0x04000000
179 #define VIRTIO_CCW_DOING_SET_IND_ADAPTER 0x08000000
180 #define VIRTIO_CCW_DOING_SET_VIRTIO_REV 0x10000000
181 #define VIRTIO_CCW_DOING_READ_STATUS 0x20000000
182 #define VIRTIO_CCW_INTPARM_MASK 0xffff0000
184 static struct virtio_ccw_device *to_vc_device(struct virtio_device *vdev)
186 return container_of(vdev, struct virtio_ccw_device, vdev);
189 static void drop_airq_indicator(struct virtqueue *vq, struct airq_info *info)
191 unsigned long i, flags;
193 write_lock_irqsave(&info->lock, flags);
194 for (i = 0; i < airq_iv_end(info->aiv); i++) {
195 if (vq == (void *)airq_iv_get_ptr(info->aiv, i)) {
196 airq_iv_free_bit(info->aiv, i);
197 airq_iv_set_ptr(info->aiv, i, 0);
201 write_unlock_irqrestore(&info->lock, flags);
204 static void virtio_airq_handler(struct airq_struct *airq, bool floating)
206 struct airq_info *info = container_of(airq, struct airq_info, airq);
209 inc_irq_stat(IRQIO_VAI);
210 read_lock(&info->lock);
211 /* Walk through indicators field, summary indicator active. */
213 ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
216 vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
218 *(get_summary_indicator(info)) = 0;
220 /* Walk through indicators field, summary indicator not active. */
222 ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
225 vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
227 read_unlock(&info->lock);
230 static struct airq_info *new_airq_info(int index)
232 struct airq_info *info;
235 info = kzalloc(sizeof(*info), GFP_KERNEL);
238 rwlock_init(&info->lock);
239 info->aiv = airq_iv_create(VIRTIO_IV_BITS, AIRQ_IV_ALLOC | AIRQ_IV_PTR
240 | AIRQ_IV_CACHELINE);
245 info->airq.handler = virtio_airq_handler;
246 info->summary_indicator_idx = index;
247 info->airq.lsi_ptr = get_summary_indicator(info);
248 info->airq.lsi_mask = 0xff;
249 info->airq.isc = VIRTIO_AIRQ_ISC;
250 rc = register_adapter_interrupt(&info->airq);
252 airq_iv_release(info->aiv);
259 static unsigned long get_airq_indicator(struct virtqueue *vqs[], int nvqs,
260 u64 *first, void **airq_info)
263 struct airq_info *info;
264 unsigned long indicator_addr = 0;
265 unsigned long bit, flags;
267 for (i = 0; i < MAX_AIRQ_AREAS && !indicator_addr; i++) {
269 airq_areas[i] = new_airq_info(i);
270 info = airq_areas[i];
273 write_lock_irqsave(&info->lock, flags);
274 bit = airq_iv_alloc(info->aiv, nvqs);
276 /* Not enough vacancies. */
277 write_unlock_irqrestore(&info->lock, flags);
282 indicator_addr = (unsigned long)info->aiv->vector;
283 for (j = 0; j < nvqs; j++) {
284 airq_iv_set_ptr(info->aiv, bit + j,
285 (unsigned long)vqs[j]);
287 write_unlock_irqrestore(&info->lock, flags);
289 return indicator_addr;
292 static void virtio_ccw_drop_indicators(struct virtio_ccw_device *vcdev)
294 struct virtio_ccw_vq_info *info;
296 if (!vcdev->airq_info)
298 list_for_each_entry(info, &vcdev->virtqueues, node)
299 drop_airq_indicator(info->vq, vcdev->airq_info);
302 static int doing_io(struct virtio_ccw_device *vcdev, __u32 flag)
307 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
311 ret = vcdev->curr_io & flag;
312 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
316 static int ccw_io_helper(struct virtio_ccw_device *vcdev,
317 struct ccw1 *ccw, __u32 intparm)
321 int flag = intparm & VIRTIO_CCW_INTPARM_MASK;
323 mutex_lock(&vcdev->io_lock);
325 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
326 ret = ccw_device_start(vcdev->cdev, ccw, intparm, 0, 0);
330 vcdev->curr_io |= flag;
332 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
334 } while (ret == -EBUSY);
335 wait_event(vcdev->wait_q, doing_io(vcdev, flag) == 0);
336 ret = ret ? ret : vcdev->err;
337 mutex_unlock(&vcdev->io_lock);
341 static void virtio_ccw_drop_indicator(struct virtio_ccw_device *vcdev,
345 unsigned long *indicatorp = NULL;
346 struct virtio_thinint_area *thinint_area = NULL;
347 struct airq_info *airq_info = vcdev->airq_info;
349 if (vcdev->is_thinint) {
350 thinint_area = ccw_device_dma_zalloc(vcdev->cdev,
351 sizeof(*thinint_area));
354 thinint_area->summary_indicator =
355 (unsigned long) get_summary_indicator(airq_info);
356 thinint_area->isc = VIRTIO_AIRQ_ISC;
357 ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
358 ccw->count = sizeof(*thinint_area);
359 ccw->cda = (__u32)(unsigned long) thinint_area;
361 /* payload is the address of the indicators */
362 indicatorp = ccw_device_dma_zalloc(vcdev->cdev,
363 sizeof(indicators(vcdev)));
367 ccw->cmd_code = CCW_CMD_SET_IND;
368 ccw->count = sizeof(indicators(vcdev));
369 ccw->cda = (__u32)(unsigned long) indicatorp;
371 /* Deregister indicators from host. */
372 *indicators(vcdev) = 0;
374 ret = ccw_io_helper(vcdev, ccw,
376 VIRTIO_CCW_DOING_SET_IND_ADAPTER :
377 VIRTIO_CCW_DOING_SET_IND);
378 if (ret && (ret != -ENODEV))
379 dev_info(&vcdev->cdev->dev,
380 "Failed to deregister indicators (%d)\n", ret);
381 else if (vcdev->is_thinint)
382 virtio_ccw_drop_indicators(vcdev);
383 ccw_device_dma_free(vcdev->cdev, indicatorp, sizeof(indicators(vcdev)));
384 ccw_device_dma_free(vcdev->cdev, thinint_area, sizeof(*thinint_area));
387 static inline long __do_kvm_notify(struct subchannel_id schid,
388 unsigned long queue_index,
391 register unsigned long __nr asm("1") = KVM_S390_VIRTIO_CCW_NOTIFY;
392 register struct subchannel_id __schid asm("2") = schid;
393 register unsigned long __index asm("3") = queue_index;
394 register long __rc asm("2");
395 register long __cookie asm("4") = cookie;
397 asm volatile ("diag 2,4,0x500\n"
398 : "=d" (__rc) : "d" (__nr), "d" (__schid), "d" (__index),
404 static inline long do_kvm_notify(struct subchannel_id schid,
405 unsigned long queue_index,
408 diag_stat_inc(DIAG_STAT_X500);
409 return __do_kvm_notify(schid, queue_index, cookie);
412 static bool virtio_ccw_kvm_notify(struct virtqueue *vq)
414 struct virtio_ccw_vq_info *info = vq->priv;
415 struct virtio_ccw_device *vcdev;
416 struct subchannel_id schid;
418 vcdev = to_vc_device(info->vq->vdev);
419 ccw_device_get_schid(vcdev->cdev, &schid);
420 info->cookie = do_kvm_notify(schid, vq->index, info->cookie);
421 if (info->cookie < 0)
426 static int virtio_ccw_read_vq_conf(struct virtio_ccw_device *vcdev,
427 struct ccw1 *ccw, int index)
431 vcdev->dma_area->config_block.index = index;
432 ccw->cmd_code = CCW_CMD_READ_VQ_CONF;
434 ccw->count = sizeof(struct vq_config_block);
435 ccw->cda = (__u32)(unsigned long)(&vcdev->dma_area->config_block);
436 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_VQ_CONF);
439 return vcdev->dma_area->config_block.num ?: -ENOENT;
442 static void virtio_ccw_del_vq(struct virtqueue *vq, struct ccw1 *ccw)
444 struct virtio_ccw_device *vcdev = to_vc_device(vq->vdev);
445 struct virtio_ccw_vq_info *info = vq->priv;
448 unsigned int index = vq->index;
450 /* Remove from our list. */
451 spin_lock_irqsave(&vcdev->lock, flags);
452 list_del(&info->node);
453 spin_unlock_irqrestore(&vcdev->lock, flags);
455 /* Release from host. */
456 if (vcdev->revision == 0) {
457 info->info_block->l.queue = 0;
458 info->info_block->l.align = 0;
459 info->info_block->l.index = index;
460 info->info_block->l.num = 0;
461 ccw->count = sizeof(info->info_block->l);
463 info->info_block->s.desc = 0;
464 info->info_block->s.index = index;
465 info->info_block->s.num = 0;
466 info->info_block->s.avail = 0;
467 info->info_block->s.used = 0;
468 ccw->count = sizeof(info->info_block->s);
470 ccw->cmd_code = CCW_CMD_SET_VQ;
472 ccw->cda = (__u32)(unsigned long)(info->info_block);
473 ret = ccw_io_helper(vcdev, ccw,
474 VIRTIO_CCW_DOING_SET_VQ | index);
476 * -ENODEV isn't considered an error: The device is gone anyway.
477 * This may happen on device detach.
479 if (ret && (ret != -ENODEV))
480 dev_warn(&vq->vdev->dev, "Error %d while deleting queue %d\n",
483 vring_del_virtqueue(vq);
484 ccw_device_dma_free(vcdev->cdev, info->info_block,
485 sizeof(*info->info_block));
489 static void virtio_ccw_del_vqs(struct virtio_device *vdev)
491 struct virtqueue *vq, *n;
493 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
495 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
499 virtio_ccw_drop_indicator(vcdev, ccw);
501 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
502 virtio_ccw_del_vq(vq, ccw);
504 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
507 static struct virtqueue *virtio_ccw_setup_vq(struct virtio_device *vdev,
508 int i, vq_callback_t *callback,
509 const char *name, bool ctx,
512 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
514 struct virtqueue *vq = NULL;
515 struct virtio_ccw_vq_info *info;
520 /* Allocate queue. */
521 info = kzalloc(sizeof(struct virtio_ccw_vq_info), GFP_KERNEL);
523 dev_warn(&vcdev->cdev->dev, "no info\n");
527 info->info_block = ccw_device_dma_zalloc(vcdev->cdev,
528 sizeof(*info->info_block));
529 if (!info->info_block) {
530 dev_warn(&vcdev->cdev->dev, "no info block\n");
534 info->num = virtio_ccw_read_vq_conf(vcdev, ccw, i);
539 may_reduce = vcdev->revision > 0;
540 vq = vring_create_virtqueue(i, info->num, KVM_VIRTIO_CCW_RING_ALIGN,
541 vdev, true, may_reduce, ctx,
542 virtio_ccw_kvm_notify, callback, name);
545 /* For now, we fail if we can't get the requested size. */
546 dev_warn(&vcdev->cdev->dev, "no vq\n");
550 /* it may have been reduced */
551 info->num = virtqueue_get_vring_size(vq);
553 /* Register it with the host. */
554 queue = virtqueue_get_desc_addr(vq);
555 if (vcdev->revision == 0) {
556 info->info_block->l.queue = queue;
557 info->info_block->l.align = KVM_VIRTIO_CCW_RING_ALIGN;
558 info->info_block->l.index = i;
559 info->info_block->l.num = info->num;
560 ccw->count = sizeof(info->info_block->l);
562 info->info_block->s.desc = queue;
563 info->info_block->s.index = i;
564 info->info_block->s.num = info->num;
565 info->info_block->s.avail = (__u64)virtqueue_get_avail_addr(vq);
566 info->info_block->s.used = (__u64)virtqueue_get_used_addr(vq);
567 ccw->count = sizeof(info->info_block->s);
569 ccw->cmd_code = CCW_CMD_SET_VQ;
571 ccw->cda = (__u32)(unsigned long)(info->info_block);
572 err = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_VQ | i);
574 dev_warn(&vcdev->cdev->dev, "SET_VQ failed\n");
581 /* Save it to our list. */
582 spin_lock_irqsave(&vcdev->lock, flags);
583 list_add(&info->node, &vcdev->virtqueues);
584 spin_unlock_irqrestore(&vcdev->lock, flags);
590 vring_del_virtqueue(vq);
592 ccw_device_dma_free(vcdev->cdev, info->info_block,
593 sizeof(*info->info_block));
599 static int virtio_ccw_register_adapter_ind(struct virtio_ccw_device *vcdev,
600 struct virtqueue *vqs[], int nvqs,
604 struct virtio_thinint_area *thinint_area = NULL;
605 struct airq_info *info;
607 thinint_area = ccw_device_dma_zalloc(vcdev->cdev,
608 sizeof(*thinint_area));
613 /* Try to get an indicator. */
614 thinint_area->indicator = get_airq_indicator(vqs, nvqs,
615 &thinint_area->bit_nr,
617 if (!thinint_area->indicator) {
621 info = vcdev->airq_info;
622 thinint_area->summary_indicator =
623 (unsigned long) get_summary_indicator(info);
624 thinint_area->isc = VIRTIO_AIRQ_ISC;
625 ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
626 ccw->flags = CCW_FLAG_SLI;
627 ccw->count = sizeof(*thinint_area);
628 ccw->cda = (__u32)(unsigned long)thinint_area;
629 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND_ADAPTER);
631 if (ret == -EOPNOTSUPP) {
633 * The host does not support adapter interrupts
634 * for virtio-ccw, stop trying.
636 virtio_ccw_use_airq = 0;
637 pr_info("Adapter interrupts unsupported on host\n");
639 dev_warn(&vcdev->cdev->dev,
640 "enabling adapter interrupts = %d\n", ret);
641 virtio_ccw_drop_indicators(vcdev);
644 ccw_device_dma_free(vcdev->cdev, thinint_area, sizeof(*thinint_area));
648 static int virtio_ccw_find_vqs(struct virtio_device *vdev, unsigned nvqs,
649 struct virtqueue *vqs[],
650 vq_callback_t *callbacks[],
651 const char * const names[],
653 struct irq_affinity *desc)
655 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
656 unsigned long *indicatorp = NULL;
657 int ret, i, queue_idx = 0;
660 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
664 for (i = 0; i < nvqs; ++i) {
670 vqs[i] = virtio_ccw_setup_vq(vdev, queue_idx++, callbacks[i],
671 names[i], ctx ? ctx[i] : false,
673 if (IS_ERR(vqs[i])) {
674 ret = PTR_ERR(vqs[i]);
681 * We need a data area under 2G to communicate. Our payload is
682 * the address of the indicators.
684 indicatorp = ccw_device_dma_zalloc(vcdev->cdev,
685 sizeof(indicators(vcdev)));
688 *indicatorp = (unsigned long) indicators(vcdev);
689 if (vcdev->is_thinint) {
690 ret = virtio_ccw_register_adapter_ind(vcdev, vqs, nvqs, ccw);
692 /* no error, just fall back to legacy interrupts */
693 vcdev->is_thinint = false;
695 if (!vcdev->is_thinint) {
696 /* Register queue indicators with host. */
697 *indicators(vcdev) = 0;
698 ccw->cmd_code = CCW_CMD_SET_IND;
700 ccw->count = sizeof(indicators(vcdev));
701 ccw->cda = (__u32)(unsigned long) indicatorp;
702 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND);
706 /* Register indicators2 with host for config changes */
707 *indicatorp = (unsigned long) indicators2(vcdev);
708 *indicators2(vcdev) = 0;
709 ccw->cmd_code = CCW_CMD_SET_CONF_IND;
711 ccw->count = sizeof(indicators2(vcdev));
712 ccw->cda = (__u32)(unsigned long) indicatorp;
713 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_CONF_IND);
718 ccw_device_dma_free(vcdev->cdev, indicatorp,
719 sizeof(indicators(vcdev)));
720 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
724 ccw_device_dma_free(vcdev->cdev, indicatorp,
725 sizeof(indicators(vcdev)));
726 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
727 virtio_ccw_del_vqs(vdev);
731 static void virtio_ccw_reset(struct virtio_device *vdev)
733 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
736 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
740 /* Zero status bits. */
741 vcdev->dma_area->status = 0;
743 /* Send a reset ccw on device. */
744 ccw->cmd_code = CCW_CMD_VDEV_RESET;
748 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_RESET);
749 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
752 static u64 virtio_ccw_get_features(struct virtio_device *vdev)
754 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
755 struct virtio_feature_desc *features;
760 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
764 features = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*features));
769 /* Read the feature bits from the host. */
771 ccw->cmd_code = CCW_CMD_READ_FEAT;
773 ccw->count = sizeof(*features);
774 ccw->cda = (__u32)(unsigned long)features;
775 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
781 rc = le32_to_cpu(features->features);
783 if (vcdev->revision == 0)
786 /* Read second half of the feature bits from the host. */
788 ccw->cmd_code = CCW_CMD_READ_FEAT;
790 ccw->count = sizeof(*features);
791 ccw->cda = (__u32)(unsigned long)features;
792 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
794 rc |= (u64)le32_to_cpu(features->features) << 32;
797 ccw_device_dma_free(vcdev->cdev, features, sizeof(*features));
798 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
802 static void ccw_transport_features(struct virtio_device *vdev)
805 * Currently nothing to do here.
809 static int virtio_ccw_finalize_features(struct virtio_device *vdev)
811 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
812 struct virtio_feature_desc *features;
816 if (vcdev->revision >= 1 &&
817 !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
818 dev_err(&vdev->dev, "virtio: device uses revision 1 "
819 "but does not have VIRTIO_F_VERSION_1\n");
823 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
827 features = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*features));
832 /* Give virtio_ring a chance to accept features. */
833 vring_transport_features(vdev);
835 /* Give virtio_ccw a chance to accept features. */
836 ccw_transport_features(vdev);
839 features->features = cpu_to_le32((u32)vdev->features);
840 /* Write the first half of the feature bits to the host. */
841 ccw->cmd_code = CCW_CMD_WRITE_FEAT;
843 ccw->count = sizeof(*features);
844 ccw->cda = (__u32)(unsigned long)features;
845 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
849 if (vcdev->revision == 0)
853 features->features = cpu_to_le32(vdev->features >> 32);
854 /* Write the second half of the feature bits to the host. */
855 ccw->cmd_code = CCW_CMD_WRITE_FEAT;
857 ccw->count = sizeof(*features);
858 ccw->cda = (__u32)(unsigned long)features;
859 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
862 ccw_device_dma_free(vcdev->cdev, features, sizeof(*features));
863 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
868 static void virtio_ccw_get_config(struct virtio_device *vdev,
869 unsigned int offset, void *buf, unsigned len)
871 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
877 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
881 config_area = ccw_device_dma_zalloc(vcdev->cdev,
882 VIRTIO_CCW_CONFIG_SIZE);
886 /* Read the config area from the host. */
887 ccw->cmd_code = CCW_CMD_READ_CONF;
889 ccw->count = offset + len;
890 ccw->cda = (__u32)(unsigned long)config_area;
891 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG);
895 spin_lock_irqsave(&vcdev->lock, flags);
896 memcpy(vcdev->config, config_area, offset + len);
897 if (vcdev->config_ready < offset + len)
898 vcdev->config_ready = offset + len;
899 spin_unlock_irqrestore(&vcdev->lock, flags);
901 memcpy(buf, config_area + offset, len);
904 ccw_device_dma_free(vcdev->cdev, config_area, VIRTIO_CCW_CONFIG_SIZE);
905 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
908 static void virtio_ccw_set_config(struct virtio_device *vdev,
909 unsigned int offset, const void *buf,
912 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
917 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
921 config_area = ccw_device_dma_zalloc(vcdev->cdev,
922 VIRTIO_CCW_CONFIG_SIZE);
926 /* Make sure we don't overwrite fields. */
927 if (vcdev->config_ready < offset)
928 virtio_ccw_get_config(vdev, 0, NULL, offset);
929 spin_lock_irqsave(&vcdev->lock, flags);
930 memcpy(&vcdev->config[offset], buf, len);
931 /* Write the config area to the host. */
932 memcpy(config_area, vcdev->config, sizeof(vcdev->config));
933 spin_unlock_irqrestore(&vcdev->lock, flags);
934 ccw->cmd_code = CCW_CMD_WRITE_CONF;
936 ccw->count = offset + len;
937 ccw->cda = (__u32)(unsigned long)config_area;
938 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG);
941 ccw_device_dma_free(vcdev->cdev, config_area, VIRTIO_CCW_CONFIG_SIZE);
942 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
945 static u8 virtio_ccw_get_status(struct virtio_device *vdev)
947 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
948 u8 old_status = vcdev->dma_area->status;
951 if (vcdev->revision < 1)
952 return vcdev->dma_area->status;
954 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
958 ccw->cmd_code = CCW_CMD_READ_STATUS;
960 ccw->count = sizeof(vcdev->dma_area->status);
961 ccw->cda = (__u32)(unsigned long)&vcdev->dma_area->status;
962 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_STATUS);
964 * If the channel program failed (should only happen if the device
965 * was hotunplugged, and then we clean up via the machine check
966 * handler anyway), vcdev->dma_area->status was not overwritten and we just
967 * return the old status, which is fine.
969 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
971 return vcdev->dma_area->status;
974 static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status)
976 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
977 u8 old_status = vcdev->dma_area->status;
981 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
985 /* Write the status to the host. */
986 vcdev->dma_area->status = status;
987 ccw->cmd_code = CCW_CMD_WRITE_STATUS;
989 ccw->count = sizeof(status);
990 ccw->cda = (__u32)(unsigned long)&vcdev->dma_area->status;
991 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS);
992 /* Write failed? We assume status is unchanged. */
994 vcdev->dma_area->status = old_status;
995 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
998 static const char *virtio_ccw_bus_name(struct virtio_device *vdev)
1000 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
1002 return dev_name(&vcdev->cdev->dev);
1005 static const struct virtio_config_ops virtio_ccw_config_ops = {
1006 .get_features = virtio_ccw_get_features,
1007 .finalize_features = virtio_ccw_finalize_features,
1008 .get = virtio_ccw_get_config,
1009 .set = virtio_ccw_set_config,
1010 .get_status = virtio_ccw_get_status,
1011 .set_status = virtio_ccw_set_status,
1012 .reset = virtio_ccw_reset,
1013 .find_vqs = virtio_ccw_find_vqs,
1014 .del_vqs = virtio_ccw_del_vqs,
1015 .bus_name = virtio_ccw_bus_name,
1020 * ccw bus driver related functions
1023 static void virtio_ccw_release_dev(struct device *_d)
1025 struct virtio_device *dev = dev_to_virtio(_d);
1026 struct virtio_ccw_device *vcdev = to_vc_device(dev);
1028 ccw_device_dma_free(vcdev->cdev, vcdev->dma_area,
1029 sizeof(*vcdev->dma_area));
1033 static int irb_is_error(struct irb *irb)
1035 if (scsw_cstat(&irb->scsw) != 0)
1037 if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
1039 if (scsw_cc(&irb->scsw) != 0)
1044 static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev,
1047 struct virtio_ccw_vq_info *info;
1048 unsigned long flags;
1049 struct virtqueue *vq;
1052 spin_lock_irqsave(&vcdev->lock, flags);
1053 list_for_each_entry(info, &vcdev->virtqueues, node) {
1054 if (info->vq->index == index) {
1059 spin_unlock_irqrestore(&vcdev->lock, flags);
1063 static void virtio_ccw_check_activity(struct virtio_ccw_device *vcdev,
1066 if (vcdev->curr_io & activity) {
1068 case VIRTIO_CCW_DOING_READ_FEAT:
1069 case VIRTIO_CCW_DOING_WRITE_FEAT:
1070 case VIRTIO_CCW_DOING_READ_CONFIG:
1071 case VIRTIO_CCW_DOING_WRITE_CONFIG:
1072 case VIRTIO_CCW_DOING_WRITE_STATUS:
1073 case VIRTIO_CCW_DOING_READ_STATUS:
1074 case VIRTIO_CCW_DOING_SET_VQ:
1075 case VIRTIO_CCW_DOING_SET_IND:
1076 case VIRTIO_CCW_DOING_SET_CONF_IND:
1077 case VIRTIO_CCW_DOING_RESET:
1078 case VIRTIO_CCW_DOING_READ_VQ_CONF:
1079 case VIRTIO_CCW_DOING_SET_IND_ADAPTER:
1080 case VIRTIO_CCW_DOING_SET_VIRTIO_REV:
1081 vcdev->curr_io &= ~activity;
1082 wake_up(&vcdev->wait_q);
1085 /* don't know what to do... */
1086 dev_warn(&vcdev->cdev->dev,
1087 "Suspicious activity '%08x'\n", activity);
1094 static void virtio_ccw_int_handler(struct ccw_device *cdev,
1095 unsigned long intparm,
1098 __u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK;
1099 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1101 struct virtqueue *vq;
1106 vcdev->err = PTR_ERR(irb);
1107 virtio_ccw_check_activity(vcdev, activity);
1108 /* Don't poke around indicators, something's wrong. */
1111 /* Check if it's a notification from the host. */
1112 if ((intparm == 0) &&
1113 (scsw_stctl(&irb->scsw) ==
1114 (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) {
1117 if (irb_is_error(irb)) {
1118 /* Command reject? */
1119 if ((scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) &&
1120 (irb->ecw[0] & SNS0_CMD_REJECT))
1121 vcdev->err = -EOPNOTSUPP;
1123 /* Map everything else to -EIO. */
1126 virtio_ccw_check_activity(vcdev, activity);
1127 for_each_set_bit(i, indicators(vcdev),
1128 sizeof(*indicators(vcdev)) * BITS_PER_BYTE) {
1129 /* The bit clear must happen before the vring kick. */
1130 clear_bit(i, indicators(vcdev));
1132 vq = virtio_ccw_vq_by_ind(vcdev, i);
1133 vring_interrupt(0, vq);
1135 if (test_bit(0, indicators2(vcdev))) {
1136 virtio_config_changed(&vcdev->vdev);
1137 clear_bit(0, indicators2(vcdev));
1142 * We usually want to autoonline all devices, but give the admin
1143 * a way to exempt devices from this.
1145 #define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
1147 static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS];
1149 static char *no_auto = "";
1151 module_param(no_auto, charp, 0444);
1152 MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined");
1154 static int virtio_ccw_check_autoonline(struct ccw_device *cdev)
1156 struct ccw_dev_id id;
1158 ccw_device_get_id(cdev, &id);
1159 if (test_bit(id.devno, devs_no_auto[id.ssid]))
1164 static void virtio_ccw_auto_online(void *data, async_cookie_t cookie)
1166 struct ccw_device *cdev = data;
1169 ret = ccw_device_set_online(cdev);
1171 dev_warn(&cdev->dev, "Failed to set online: %d\n", ret);
1174 static int virtio_ccw_probe(struct ccw_device *cdev)
1176 cdev->handler = virtio_ccw_int_handler;
1178 if (virtio_ccw_check_autoonline(cdev))
1179 async_schedule(virtio_ccw_auto_online, cdev);
1183 static struct virtio_ccw_device *virtio_grab_drvdata(struct ccw_device *cdev)
1185 unsigned long flags;
1186 struct virtio_ccw_device *vcdev;
1188 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1189 vcdev = dev_get_drvdata(&cdev->dev);
1190 if (!vcdev || vcdev->going_away) {
1191 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1194 vcdev->going_away = true;
1195 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1199 static void virtio_ccw_remove(struct ccw_device *cdev)
1201 unsigned long flags;
1202 struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
1204 if (vcdev && cdev->online) {
1205 if (vcdev->device_lost)
1206 virtio_break_device(&vcdev->vdev);
1207 unregister_virtio_device(&vcdev->vdev);
1208 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1209 dev_set_drvdata(&cdev->dev, NULL);
1210 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1212 cdev->handler = NULL;
1215 static int virtio_ccw_offline(struct ccw_device *cdev)
1217 unsigned long flags;
1218 struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
1222 if (vcdev->device_lost)
1223 virtio_break_device(&vcdev->vdev);
1224 unregister_virtio_device(&vcdev->vdev);
1225 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1226 dev_set_drvdata(&cdev->dev, NULL);
1227 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1231 static int virtio_ccw_set_transport_rev(struct virtio_ccw_device *vcdev)
1233 struct virtio_rev_info *rev;
1237 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
1240 rev = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*rev));
1242 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
1246 /* Set transport revision */
1247 ccw->cmd_code = CCW_CMD_SET_VIRTIO_REV;
1249 ccw->count = sizeof(*rev);
1250 ccw->cda = (__u32)(unsigned long)rev;
1252 vcdev->revision = VIRTIO_CCW_REV_MAX;
1254 rev->revision = vcdev->revision;
1255 /* none of our supported revisions carry payload */
1257 ret = ccw_io_helper(vcdev, ccw,
1258 VIRTIO_CCW_DOING_SET_VIRTIO_REV);
1259 if (ret == -EOPNOTSUPP) {
1260 if (vcdev->revision == 0)
1262 * The host device does not support setting
1263 * the revision: let's operate it in legacy
1270 } while (ret == -EOPNOTSUPP);
1272 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
1273 ccw_device_dma_free(vcdev->cdev, rev, sizeof(*rev));
1277 static int virtio_ccw_online(struct ccw_device *cdev)
1280 struct virtio_ccw_device *vcdev;
1281 unsigned long flags;
1283 vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL);
1285 dev_warn(&cdev->dev, "Could not get memory for virtio\n");
1289 vcdev->vdev.dev.parent = &cdev->dev;
1291 vcdev->dma_area = ccw_device_dma_zalloc(vcdev->cdev,
1292 sizeof(*vcdev->dma_area));
1293 if (!vcdev->dma_area) {
1298 vcdev->is_thinint = virtio_ccw_use_airq; /* at least try */
1300 vcdev->vdev.dev.release = virtio_ccw_release_dev;
1301 vcdev->vdev.config = &virtio_ccw_config_ops;
1302 init_waitqueue_head(&vcdev->wait_q);
1303 INIT_LIST_HEAD(&vcdev->virtqueues);
1304 spin_lock_init(&vcdev->lock);
1305 mutex_init(&vcdev->io_lock);
1307 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1308 dev_set_drvdata(&cdev->dev, vcdev);
1309 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1310 vcdev->vdev.id.vendor = cdev->id.cu_type;
1311 vcdev->vdev.id.device = cdev->id.cu_model;
1313 ret = virtio_ccw_set_transport_rev(vcdev);
1317 ret = register_virtio_device(&vcdev->vdev);
1319 dev_warn(&cdev->dev, "Failed to register virtio device: %d\n",
1325 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1326 dev_set_drvdata(&cdev->dev, NULL);
1327 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1328 put_device(&vcdev->vdev.dev);
1332 ccw_device_dma_free(vcdev->cdev, vcdev->dma_area,
1333 sizeof(*vcdev->dma_area));
1339 static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event)
1342 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1345 * Make sure vcdev is set
1346 * i.e. set_offline/remove callback not already running
1353 vcdev->device_lost = true;
1366 static struct ccw_device_id virtio_ids[] = {
1367 { CCW_DEVICE(0x3832, 0) },
1371 #ifdef CONFIG_PM_SLEEP
1372 static int virtio_ccw_freeze(struct ccw_device *cdev)
1374 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1376 return virtio_device_freeze(&vcdev->vdev);
1379 static int virtio_ccw_restore(struct ccw_device *cdev)
1381 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1384 ret = virtio_ccw_set_transport_rev(vcdev);
1388 return virtio_device_restore(&vcdev->vdev);
1392 static struct ccw_driver virtio_ccw_driver = {
1394 .owner = THIS_MODULE,
1395 .name = "virtio_ccw",
1398 .probe = virtio_ccw_probe,
1399 .remove = virtio_ccw_remove,
1400 .set_offline = virtio_ccw_offline,
1401 .set_online = virtio_ccw_online,
1402 .notify = virtio_ccw_cio_notify,
1403 .int_class = IRQIO_VIR,
1404 #ifdef CONFIG_PM_SLEEP
1405 .freeze = virtio_ccw_freeze,
1406 .thaw = virtio_ccw_restore,
1407 .restore = virtio_ccw_restore,
1411 static int __init pure_hex(char **cp, unsigned int *val, int min_digit,
1412 int max_digit, int max_val)
1419 while (diff <= max_digit) {
1420 int value = hex_to_bin(**cp);
1424 *val = *val * 16 + value;
1429 if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
1435 static int __init parse_busid(char *str, unsigned int *cssid,
1436 unsigned int *ssid, unsigned int *devno)
1447 ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
1448 if (ret || (str_work[0] != '.'))
1451 ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
1452 if (ret || (str_work[0] != '.'))
1455 ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
1456 if (ret || (str_work[0] != '\0'))
1464 static void __init no_auto_parse(void)
1466 unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
1471 while ((parm = strsep(&str, ","))) {
1472 rc = parse_busid(strsep(&parm, "-"), &from_cssid,
1477 rc = parse_busid(parm, &to_cssid,
1479 if ((from_ssid > to_ssid) ||
1480 ((from_ssid == to_ssid) && (from > to)))
1483 to_cssid = from_cssid;
1484 to_ssid = from_ssid;
1489 while ((from_ssid < to_ssid) ||
1490 ((from_ssid == to_ssid) && (from <= to))) {
1491 set_bit(from, devs_no_auto[from_ssid]);
1493 if (from > __MAX_SUBCHANNEL) {
1501 static int __init virtio_ccw_init(void)
1505 /* parse no_auto string before we do anything further */
1508 summary_indicators = cio_dma_zalloc(MAX_AIRQ_AREAS);
1509 if (!summary_indicators)
1511 rc = ccw_driver_register(&virtio_ccw_driver);
1513 cio_dma_free(summary_indicators, MAX_AIRQ_AREAS);
1516 device_initcall(virtio_ccw_init);