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 2
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 DEFINE_MUTEX(airq_areas_lock);
150 static u8 *summary_indicators;
152 static inline u8 *get_summary_indicator(struct airq_info *info)
154 return summary_indicators + info->summary_indicator_idx;
157 #define CCW_CMD_SET_VQ 0x13
158 #define CCW_CMD_VDEV_RESET 0x33
159 #define CCW_CMD_SET_IND 0x43
160 #define CCW_CMD_SET_CONF_IND 0x53
161 #define CCW_CMD_READ_FEAT 0x12
162 #define CCW_CMD_WRITE_FEAT 0x11
163 #define CCW_CMD_READ_CONF 0x22
164 #define CCW_CMD_WRITE_CONF 0x21
165 #define CCW_CMD_WRITE_STATUS 0x31
166 #define CCW_CMD_READ_VQ_CONF 0x32
167 #define CCW_CMD_READ_STATUS 0x72
168 #define CCW_CMD_SET_IND_ADAPTER 0x73
169 #define CCW_CMD_SET_VIRTIO_REV 0x83
171 #define VIRTIO_CCW_DOING_SET_VQ 0x00010000
172 #define VIRTIO_CCW_DOING_RESET 0x00040000
173 #define VIRTIO_CCW_DOING_READ_FEAT 0x00080000
174 #define VIRTIO_CCW_DOING_WRITE_FEAT 0x00100000
175 #define VIRTIO_CCW_DOING_READ_CONFIG 0x00200000
176 #define VIRTIO_CCW_DOING_WRITE_CONFIG 0x00400000
177 #define VIRTIO_CCW_DOING_WRITE_STATUS 0x00800000
178 #define VIRTIO_CCW_DOING_SET_IND 0x01000000
179 #define VIRTIO_CCW_DOING_READ_VQ_CONF 0x02000000
180 #define VIRTIO_CCW_DOING_SET_CONF_IND 0x04000000
181 #define VIRTIO_CCW_DOING_SET_IND_ADAPTER 0x08000000
182 #define VIRTIO_CCW_DOING_SET_VIRTIO_REV 0x10000000
183 #define VIRTIO_CCW_DOING_READ_STATUS 0x20000000
184 #define VIRTIO_CCW_INTPARM_MASK 0xffff0000
186 static struct virtio_ccw_device *to_vc_device(struct virtio_device *vdev)
188 return container_of(vdev, struct virtio_ccw_device, vdev);
191 static void drop_airq_indicator(struct virtqueue *vq, struct airq_info *info)
193 unsigned long i, flags;
195 write_lock_irqsave(&info->lock, flags);
196 for (i = 0; i < airq_iv_end(info->aiv); i++) {
197 if (vq == (void *)airq_iv_get_ptr(info->aiv, i)) {
198 airq_iv_free_bit(info->aiv, i);
199 airq_iv_set_ptr(info->aiv, i, 0);
203 write_unlock_irqrestore(&info->lock, flags);
206 static void virtio_airq_handler(struct airq_struct *airq, bool floating)
208 struct airq_info *info = container_of(airq, struct airq_info, airq);
211 inc_irq_stat(IRQIO_VAI);
212 read_lock(&info->lock);
213 /* Walk through indicators field, summary indicator active. */
215 ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
218 vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
220 *(get_summary_indicator(info)) = 0;
222 /* Walk through indicators field, summary indicator not active. */
224 ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
227 vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
229 read_unlock(&info->lock);
232 static struct airq_info *new_airq_info(int index)
234 struct airq_info *info;
237 info = kzalloc(sizeof(*info), GFP_KERNEL);
240 rwlock_init(&info->lock);
241 info->aiv = airq_iv_create(VIRTIO_IV_BITS, AIRQ_IV_ALLOC | AIRQ_IV_PTR
242 | AIRQ_IV_CACHELINE);
247 info->airq.handler = virtio_airq_handler;
248 info->summary_indicator_idx = index;
249 info->airq.lsi_ptr = get_summary_indicator(info);
250 info->airq.lsi_mask = 0xff;
251 info->airq.isc = VIRTIO_AIRQ_ISC;
252 rc = register_adapter_interrupt(&info->airq);
254 airq_iv_release(info->aiv);
261 static unsigned long get_airq_indicator(struct virtqueue *vqs[], int nvqs,
262 u64 *first, void **airq_info)
265 struct airq_info *info;
266 unsigned long indicator_addr = 0;
267 unsigned long bit, flags;
269 for (i = 0; i < MAX_AIRQ_AREAS && !indicator_addr; i++) {
270 mutex_lock(&airq_areas_lock);
272 airq_areas[i] = new_airq_info(i);
273 info = airq_areas[i];
274 mutex_unlock(&airq_areas_lock);
277 write_lock_irqsave(&info->lock, flags);
278 bit = airq_iv_alloc(info->aiv, nvqs);
280 /* Not enough vacancies. */
281 write_unlock_irqrestore(&info->lock, flags);
286 indicator_addr = (unsigned long)info->aiv->vector;
287 for (j = 0; j < nvqs; j++) {
288 airq_iv_set_ptr(info->aiv, bit + j,
289 (unsigned long)vqs[j]);
291 write_unlock_irqrestore(&info->lock, flags);
293 return indicator_addr;
296 static void virtio_ccw_drop_indicators(struct virtio_ccw_device *vcdev)
298 struct virtio_ccw_vq_info *info;
300 if (!vcdev->airq_info)
302 list_for_each_entry(info, &vcdev->virtqueues, node)
303 drop_airq_indicator(info->vq, vcdev->airq_info);
306 static int doing_io(struct virtio_ccw_device *vcdev, __u32 flag)
311 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
315 ret = vcdev->curr_io & flag;
316 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
320 static int ccw_io_helper(struct virtio_ccw_device *vcdev,
321 struct ccw1 *ccw, __u32 intparm)
325 int flag = intparm & VIRTIO_CCW_INTPARM_MASK;
327 mutex_lock(&vcdev->io_lock);
329 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
330 ret = ccw_device_start(vcdev->cdev, ccw, intparm, 0, 0);
334 vcdev->curr_io |= flag;
336 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
338 } while (ret == -EBUSY);
339 wait_event(vcdev->wait_q, doing_io(vcdev, flag) == 0);
340 ret = ret ? ret : vcdev->err;
341 mutex_unlock(&vcdev->io_lock);
345 static void virtio_ccw_drop_indicator(struct virtio_ccw_device *vcdev,
349 unsigned long *indicatorp = NULL;
350 struct virtio_thinint_area *thinint_area = NULL;
351 struct airq_info *airq_info = vcdev->airq_info;
353 if (vcdev->is_thinint) {
354 thinint_area = ccw_device_dma_zalloc(vcdev->cdev,
355 sizeof(*thinint_area));
358 thinint_area->summary_indicator =
359 (unsigned long) get_summary_indicator(airq_info);
360 thinint_area->isc = VIRTIO_AIRQ_ISC;
361 ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
362 ccw->count = sizeof(*thinint_area);
363 ccw->cda = (__u32)(unsigned long) thinint_area;
365 /* payload is the address of the indicators */
366 indicatorp = ccw_device_dma_zalloc(vcdev->cdev,
367 sizeof(indicators(vcdev)));
371 ccw->cmd_code = CCW_CMD_SET_IND;
372 ccw->count = sizeof(indicators(vcdev));
373 ccw->cda = (__u32)(unsigned long) indicatorp;
375 /* Deregister indicators from host. */
376 *indicators(vcdev) = 0;
378 ret = ccw_io_helper(vcdev, ccw,
380 VIRTIO_CCW_DOING_SET_IND_ADAPTER :
381 VIRTIO_CCW_DOING_SET_IND);
382 if (ret && (ret != -ENODEV))
383 dev_info(&vcdev->cdev->dev,
384 "Failed to deregister indicators (%d)\n", ret);
385 else if (vcdev->is_thinint)
386 virtio_ccw_drop_indicators(vcdev);
387 ccw_device_dma_free(vcdev->cdev, indicatorp, sizeof(indicators(vcdev)));
388 ccw_device_dma_free(vcdev->cdev, thinint_area, sizeof(*thinint_area));
391 static inline long __do_kvm_notify(struct subchannel_id schid,
392 unsigned long queue_index,
395 register unsigned long __nr asm("1") = KVM_S390_VIRTIO_CCW_NOTIFY;
396 register struct subchannel_id __schid asm("2") = schid;
397 register unsigned long __index asm("3") = queue_index;
398 register long __rc asm("2");
399 register long __cookie asm("4") = cookie;
401 asm volatile ("diag 2,4,0x500\n"
402 : "=d" (__rc) : "d" (__nr), "d" (__schid), "d" (__index),
408 static inline long do_kvm_notify(struct subchannel_id schid,
409 unsigned long queue_index,
412 diag_stat_inc(DIAG_STAT_X500);
413 return __do_kvm_notify(schid, queue_index, cookie);
416 static bool virtio_ccw_kvm_notify(struct virtqueue *vq)
418 struct virtio_ccw_vq_info *info = vq->priv;
419 struct virtio_ccw_device *vcdev;
420 struct subchannel_id schid;
422 vcdev = to_vc_device(info->vq->vdev);
423 ccw_device_get_schid(vcdev->cdev, &schid);
424 info->cookie = do_kvm_notify(schid, vq->index, info->cookie);
425 if (info->cookie < 0)
430 static int virtio_ccw_read_vq_conf(struct virtio_ccw_device *vcdev,
431 struct ccw1 *ccw, int index)
435 vcdev->dma_area->config_block.index = index;
436 ccw->cmd_code = CCW_CMD_READ_VQ_CONF;
438 ccw->count = sizeof(struct vq_config_block);
439 ccw->cda = (__u32)(unsigned long)(&vcdev->dma_area->config_block);
440 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_VQ_CONF);
443 return vcdev->dma_area->config_block.num ?: -ENOENT;
446 static void virtio_ccw_del_vq(struct virtqueue *vq, struct ccw1 *ccw)
448 struct virtio_ccw_device *vcdev = to_vc_device(vq->vdev);
449 struct virtio_ccw_vq_info *info = vq->priv;
452 unsigned int index = vq->index;
454 /* Remove from our list. */
455 spin_lock_irqsave(&vcdev->lock, flags);
456 list_del(&info->node);
457 spin_unlock_irqrestore(&vcdev->lock, flags);
459 /* Release from host. */
460 if (vcdev->revision == 0) {
461 info->info_block->l.queue = 0;
462 info->info_block->l.align = 0;
463 info->info_block->l.index = index;
464 info->info_block->l.num = 0;
465 ccw->count = sizeof(info->info_block->l);
467 info->info_block->s.desc = 0;
468 info->info_block->s.index = index;
469 info->info_block->s.num = 0;
470 info->info_block->s.avail = 0;
471 info->info_block->s.used = 0;
472 ccw->count = sizeof(info->info_block->s);
474 ccw->cmd_code = CCW_CMD_SET_VQ;
476 ccw->cda = (__u32)(unsigned long)(info->info_block);
477 ret = ccw_io_helper(vcdev, ccw,
478 VIRTIO_CCW_DOING_SET_VQ | index);
480 * -ENODEV isn't considered an error: The device is gone anyway.
481 * This may happen on device detach.
483 if (ret && (ret != -ENODEV))
484 dev_warn(&vq->vdev->dev, "Error %d while deleting queue %d\n",
487 vring_del_virtqueue(vq);
488 ccw_device_dma_free(vcdev->cdev, info->info_block,
489 sizeof(*info->info_block));
493 static void virtio_ccw_del_vqs(struct virtio_device *vdev)
495 struct virtqueue *vq, *n;
497 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
499 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
503 virtio_ccw_drop_indicator(vcdev, ccw);
505 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
506 virtio_ccw_del_vq(vq, ccw);
508 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
511 static struct virtqueue *virtio_ccw_setup_vq(struct virtio_device *vdev,
512 int i, vq_callback_t *callback,
513 const char *name, bool ctx,
516 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
518 struct virtqueue *vq = NULL;
519 struct virtio_ccw_vq_info *info;
524 /* Allocate queue. */
525 info = kzalloc(sizeof(struct virtio_ccw_vq_info), GFP_KERNEL);
527 dev_warn(&vcdev->cdev->dev, "no info\n");
531 info->info_block = ccw_device_dma_zalloc(vcdev->cdev,
532 sizeof(*info->info_block));
533 if (!info->info_block) {
534 dev_warn(&vcdev->cdev->dev, "no info block\n");
538 info->num = virtio_ccw_read_vq_conf(vcdev, ccw, i);
543 may_reduce = vcdev->revision > 0;
544 vq = vring_create_virtqueue(i, info->num, KVM_VIRTIO_CCW_RING_ALIGN,
545 vdev, true, may_reduce, ctx,
546 virtio_ccw_kvm_notify, callback, name);
549 /* For now, we fail if we can't get the requested size. */
550 dev_warn(&vcdev->cdev->dev, "no vq\n");
554 /* it may have been reduced */
555 info->num = virtqueue_get_vring_size(vq);
557 /* Register it with the host. */
558 queue = virtqueue_get_desc_addr(vq);
559 if (vcdev->revision == 0) {
560 info->info_block->l.queue = queue;
561 info->info_block->l.align = KVM_VIRTIO_CCW_RING_ALIGN;
562 info->info_block->l.index = i;
563 info->info_block->l.num = info->num;
564 ccw->count = sizeof(info->info_block->l);
566 info->info_block->s.desc = queue;
567 info->info_block->s.index = i;
568 info->info_block->s.num = info->num;
569 info->info_block->s.avail = (__u64)virtqueue_get_avail_addr(vq);
570 info->info_block->s.used = (__u64)virtqueue_get_used_addr(vq);
571 ccw->count = sizeof(info->info_block->s);
573 ccw->cmd_code = CCW_CMD_SET_VQ;
575 ccw->cda = (__u32)(unsigned long)(info->info_block);
576 err = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_VQ | i);
578 dev_warn(&vcdev->cdev->dev, "SET_VQ failed\n");
585 /* Save it to our list. */
586 spin_lock_irqsave(&vcdev->lock, flags);
587 list_add(&info->node, &vcdev->virtqueues);
588 spin_unlock_irqrestore(&vcdev->lock, flags);
594 vring_del_virtqueue(vq);
596 ccw_device_dma_free(vcdev->cdev, info->info_block,
597 sizeof(*info->info_block));
603 static int virtio_ccw_register_adapter_ind(struct virtio_ccw_device *vcdev,
604 struct virtqueue *vqs[], int nvqs,
608 struct virtio_thinint_area *thinint_area = NULL;
609 struct airq_info *info;
611 thinint_area = ccw_device_dma_zalloc(vcdev->cdev,
612 sizeof(*thinint_area));
617 /* Try to get an indicator. */
618 thinint_area->indicator = get_airq_indicator(vqs, nvqs,
619 &thinint_area->bit_nr,
621 if (!thinint_area->indicator) {
625 info = vcdev->airq_info;
626 thinint_area->summary_indicator =
627 (unsigned long) get_summary_indicator(info);
628 thinint_area->isc = VIRTIO_AIRQ_ISC;
629 ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
630 ccw->flags = CCW_FLAG_SLI;
631 ccw->count = sizeof(*thinint_area);
632 ccw->cda = (__u32)(unsigned long)thinint_area;
633 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND_ADAPTER);
635 if (ret == -EOPNOTSUPP) {
637 * The host does not support adapter interrupts
638 * for virtio-ccw, stop trying.
640 virtio_ccw_use_airq = 0;
641 pr_info("Adapter interrupts unsupported on host\n");
643 dev_warn(&vcdev->cdev->dev,
644 "enabling adapter interrupts = %d\n", ret);
645 virtio_ccw_drop_indicators(vcdev);
648 ccw_device_dma_free(vcdev->cdev, thinint_area, sizeof(*thinint_area));
652 static int virtio_ccw_find_vqs(struct virtio_device *vdev, unsigned nvqs,
653 struct virtqueue *vqs[],
654 vq_callback_t *callbacks[],
655 const char * const names[],
657 struct irq_affinity *desc)
659 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
660 unsigned long *indicatorp = NULL;
661 int ret, i, queue_idx = 0;
664 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
668 for (i = 0; i < nvqs; ++i) {
674 vqs[i] = virtio_ccw_setup_vq(vdev, queue_idx++, callbacks[i],
675 names[i], ctx ? ctx[i] : false,
677 if (IS_ERR(vqs[i])) {
678 ret = PTR_ERR(vqs[i]);
685 * We need a data area under 2G to communicate. Our payload is
686 * the address of the indicators.
688 indicatorp = ccw_device_dma_zalloc(vcdev->cdev,
689 sizeof(indicators(vcdev)));
692 *indicatorp = (unsigned long) indicators(vcdev);
693 if (vcdev->is_thinint) {
694 ret = virtio_ccw_register_adapter_ind(vcdev, vqs, nvqs, ccw);
696 /* no error, just fall back to legacy interrupts */
697 vcdev->is_thinint = false;
699 if (!vcdev->is_thinint) {
700 /* Register queue indicators with host. */
701 *indicators(vcdev) = 0;
702 ccw->cmd_code = CCW_CMD_SET_IND;
704 ccw->count = sizeof(indicators(vcdev));
705 ccw->cda = (__u32)(unsigned long) indicatorp;
706 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND);
710 /* Register indicators2 with host for config changes */
711 *indicatorp = (unsigned long) indicators2(vcdev);
712 *indicators2(vcdev) = 0;
713 ccw->cmd_code = CCW_CMD_SET_CONF_IND;
715 ccw->count = sizeof(indicators2(vcdev));
716 ccw->cda = (__u32)(unsigned long) indicatorp;
717 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_CONF_IND);
722 ccw_device_dma_free(vcdev->cdev, indicatorp,
723 sizeof(indicators(vcdev)));
724 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
728 ccw_device_dma_free(vcdev->cdev, indicatorp,
729 sizeof(indicators(vcdev)));
730 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
731 virtio_ccw_del_vqs(vdev);
735 static void virtio_ccw_reset(struct virtio_device *vdev)
737 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
740 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
744 /* Zero status bits. */
745 vcdev->dma_area->status = 0;
747 /* Send a reset ccw on device. */
748 ccw->cmd_code = CCW_CMD_VDEV_RESET;
752 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_RESET);
753 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
756 static u64 virtio_ccw_get_features(struct virtio_device *vdev)
758 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
759 struct virtio_feature_desc *features;
764 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
768 features = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*features));
773 /* Read the feature bits from the host. */
775 ccw->cmd_code = CCW_CMD_READ_FEAT;
777 ccw->count = sizeof(*features);
778 ccw->cda = (__u32)(unsigned long)features;
779 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
785 rc = le32_to_cpu(features->features);
787 if (vcdev->revision == 0)
790 /* Read second half of the feature bits from the host. */
792 ccw->cmd_code = CCW_CMD_READ_FEAT;
794 ccw->count = sizeof(*features);
795 ccw->cda = (__u32)(unsigned long)features;
796 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
798 rc |= (u64)le32_to_cpu(features->features) << 32;
801 ccw_device_dma_free(vcdev->cdev, features, sizeof(*features));
802 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
806 static void ccw_transport_features(struct virtio_device *vdev)
809 * Currently nothing to do here.
813 static int virtio_ccw_finalize_features(struct virtio_device *vdev)
815 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
816 struct virtio_feature_desc *features;
820 if (vcdev->revision >= 1 &&
821 !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
822 dev_err(&vdev->dev, "virtio: device uses revision 1 "
823 "but does not have VIRTIO_F_VERSION_1\n");
827 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
831 features = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*features));
836 /* Give virtio_ring a chance to accept features. */
837 vring_transport_features(vdev);
839 /* Give virtio_ccw a chance to accept features. */
840 ccw_transport_features(vdev);
843 features->features = cpu_to_le32((u32)vdev->features);
844 /* Write the first half of the feature bits to the host. */
845 ccw->cmd_code = CCW_CMD_WRITE_FEAT;
847 ccw->count = sizeof(*features);
848 ccw->cda = (__u32)(unsigned long)features;
849 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
853 if (vcdev->revision == 0)
857 features->features = cpu_to_le32(vdev->features >> 32);
858 /* Write the second half of the feature bits to the host. */
859 ccw->cmd_code = CCW_CMD_WRITE_FEAT;
861 ccw->count = sizeof(*features);
862 ccw->cda = (__u32)(unsigned long)features;
863 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
866 ccw_device_dma_free(vcdev->cdev, features, sizeof(*features));
867 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
872 static void virtio_ccw_get_config(struct virtio_device *vdev,
873 unsigned int offset, void *buf, unsigned len)
875 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
881 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
885 config_area = ccw_device_dma_zalloc(vcdev->cdev,
886 VIRTIO_CCW_CONFIG_SIZE);
890 /* Read the config area from the host. */
891 ccw->cmd_code = CCW_CMD_READ_CONF;
893 ccw->count = offset + len;
894 ccw->cda = (__u32)(unsigned long)config_area;
895 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG);
899 spin_lock_irqsave(&vcdev->lock, flags);
900 memcpy(vcdev->config, config_area, offset + len);
901 if (vcdev->config_ready < offset + len)
902 vcdev->config_ready = offset + len;
903 spin_unlock_irqrestore(&vcdev->lock, flags);
905 memcpy(buf, config_area + offset, len);
908 ccw_device_dma_free(vcdev->cdev, config_area, VIRTIO_CCW_CONFIG_SIZE);
909 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
912 static void virtio_ccw_set_config(struct virtio_device *vdev,
913 unsigned int offset, const void *buf,
916 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
921 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
925 config_area = ccw_device_dma_zalloc(vcdev->cdev,
926 VIRTIO_CCW_CONFIG_SIZE);
930 /* Make sure we don't overwrite fields. */
931 if (vcdev->config_ready < offset)
932 virtio_ccw_get_config(vdev, 0, NULL, offset);
933 spin_lock_irqsave(&vcdev->lock, flags);
934 memcpy(&vcdev->config[offset], buf, len);
935 /* Write the config area to the host. */
936 memcpy(config_area, vcdev->config, sizeof(vcdev->config));
937 spin_unlock_irqrestore(&vcdev->lock, flags);
938 ccw->cmd_code = CCW_CMD_WRITE_CONF;
940 ccw->count = offset + len;
941 ccw->cda = (__u32)(unsigned long)config_area;
942 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG);
945 ccw_device_dma_free(vcdev->cdev, config_area, VIRTIO_CCW_CONFIG_SIZE);
946 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
949 static u8 virtio_ccw_get_status(struct virtio_device *vdev)
951 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
952 u8 old_status = vcdev->dma_area->status;
955 if (vcdev->revision < 2)
956 return vcdev->dma_area->status;
958 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
962 ccw->cmd_code = CCW_CMD_READ_STATUS;
964 ccw->count = sizeof(vcdev->dma_area->status);
965 ccw->cda = (__u32)(unsigned long)&vcdev->dma_area->status;
966 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_STATUS);
968 * If the channel program failed (should only happen if the device
969 * was hotunplugged, and then we clean up via the machine check
970 * handler anyway), vcdev->dma_area->status was not overwritten and we just
971 * return the old status, which is fine.
973 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
975 return vcdev->dma_area->status;
978 static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status)
980 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
981 u8 old_status = vcdev->dma_area->status;
985 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
989 /* Write the status to the host. */
990 vcdev->dma_area->status = status;
991 ccw->cmd_code = CCW_CMD_WRITE_STATUS;
993 ccw->count = sizeof(status);
994 ccw->cda = (__u32)(unsigned long)&vcdev->dma_area->status;
995 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS);
996 /* Write failed? We assume status is unchanged. */
998 vcdev->dma_area->status = old_status;
999 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
1002 static const char *virtio_ccw_bus_name(struct virtio_device *vdev)
1004 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
1006 return dev_name(&vcdev->cdev->dev);
1009 static const struct virtio_config_ops virtio_ccw_config_ops = {
1010 .get_features = virtio_ccw_get_features,
1011 .finalize_features = virtio_ccw_finalize_features,
1012 .get = virtio_ccw_get_config,
1013 .set = virtio_ccw_set_config,
1014 .get_status = virtio_ccw_get_status,
1015 .set_status = virtio_ccw_set_status,
1016 .reset = virtio_ccw_reset,
1017 .find_vqs = virtio_ccw_find_vqs,
1018 .del_vqs = virtio_ccw_del_vqs,
1019 .bus_name = virtio_ccw_bus_name,
1024 * ccw bus driver related functions
1027 static void virtio_ccw_release_dev(struct device *_d)
1029 struct virtio_device *dev = dev_to_virtio(_d);
1030 struct virtio_ccw_device *vcdev = to_vc_device(dev);
1032 ccw_device_dma_free(vcdev->cdev, vcdev->dma_area,
1033 sizeof(*vcdev->dma_area));
1037 static int irb_is_error(struct irb *irb)
1039 if (scsw_cstat(&irb->scsw) != 0)
1041 if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
1043 if (scsw_cc(&irb->scsw) != 0)
1048 static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev,
1051 struct virtio_ccw_vq_info *info;
1052 unsigned long flags;
1053 struct virtqueue *vq;
1056 spin_lock_irqsave(&vcdev->lock, flags);
1057 list_for_each_entry(info, &vcdev->virtqueues, node) {
1058 if (info->vq->index == index) {
1063 spin_unlock_irqrestore(&vcdev->lock, flags);
1067 static void virtio_ccw_check_activity(struct virtio_ccw_device *vcdev,
1070 if (vcdev->curr_io & activity) {
1072 case VIRTIO_CCW_DOING_READ_FEAT:
1073 case VIRTIO_CCW_DOING_WRITE_FEAT:
1074 case VIRTIO_CCW_DOING_READ_CONFIG:
1075 case VIRTIO_CCW_DOING_WRITE_CONFIG:
1076 case VIRTIO_CCW_DOING_WRITE_STATUS:
1077 case VIRTIO_CCW_DOING_READ_STATUS:
1078 case VIRTIO_CCW_DOING_SET_VQ:
1079 case VIRTIO_CCW_DOING_SET_IND:
1080 case VIRTIO_CCW_DOING_SET_CONF_IND:
1081 case VIRTIO_CCW_DOING_RESET:
1082 case VIRTIO_CCW_DOING_READ_VQ_CONF:
1083 case VIRTIO_CCW_DOING_SET_IND_ADAPTER:
1084 case VIRTIO_CCW_DOING_SET_VIRTIO_REV:
1085 vcdev->curr_io &= ~activity;
1086 wake_up(&vcdev->wait_q);
1089 /* don't know what to do... */
1090 dev_warn(&vcdev->cdev->dev,
1091 "Suspicious activity '%08x'\n", activity);
1098 static void virtio_ccw_int_handler(struct ccw_device *cdev,
1099 unsigned long intparm,
1102 __u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK;
1103 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1105 struct virtqueue *vq;
1110 vcdev->err = PTR_ERR(irb);
1111 virtio_ccw_check_activity(vcdev, activity);
1112 /* Don't poke around indicators, something's wrong. */
1115 /* Check if it's a notification from the host. */
1116 if ((intparm == 0) &&
1117 (scsw_stctl(&irb->scsw) ==
1118 (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) {
1121 if (irb_is_error(irb)) {
1122 /* Command reject? */
1123 if ((scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) &&
1124 (irb->ecw[0] & SNS0_CMD_REJECT))
1125 vcdev->err = -EOPNOTSUPP;
1127 /* Map everything else to -EIO. */
1130 virtio_ccw_check_activity(vcdev, activity);
1131 for_each_set_bit(i, indicators(vcdev),
1132 sizeof(*indicators(vcdev)) * BITS_PER_BYTE) {
1133 /* The bit clear must happen before the vring kick. */
1134 clear_bit(i, indicators(vcdev));
1136 vq = virtio_ccw_vq_by_ind(vcdev, i);
1137 vring_interrupt(0, vq);
1139 if (test_bit(0, indicators2(vcdev))) {
1140 virtio_config_changed(&vcdev->vdev);
1141 clear_bit(0, indicators2(vcdev));
1146 * We usually want to autoonline all devices, but give the admin
1147 * a way to exempt devices from this.
1149 #define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
1151 static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS];
1153 static char *no_auto = "";
1155 module_param(no_auto, charp, 0444);
1156 MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined");
1158 static int virtio_ccw_check_autoonline(struct ccw_device *cdev)
1160 struct ccw_dev_id id;
1162 ccw_device_get_id(cdev, &id);
1163 if (test_bit(id.devno, devs_no_auto[id.ssid]))
1168 static void virtio_ccw_auto_online(void *data, async_cookie_t cookie)
1170 struct ccw_device *cdev = data;
1173 ret = ccw_device_set_online(cdev);
1175 dev_warn(&cdev->dev, "Failed to set online: %d\n", ret);
1178 static int virtio_ccw_probe(struct ccw_device *cdev)
1180 cdev->handler = virtio_ccw_int_handler;
1182 if (virtio_ccw_check_autoonline(cdev))
1183 async_schedule(virtio_ccw_auto_online, cdev);
1187 static struct virtio_ccw_device *virtio_grab_drvdata(struct ccw_device *cdev)
1189 unsigned long flags;
1190 struct virtio_ccw_device *vcdev;
1192 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1193 vcdev = dev_get_drvdata(&cdev->dev);
1194 if (!vcdev || vcdev->going_away) {
1195 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1198 vcdev->going_away = true;
1199 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1203 static void virtio_ccw_remove(struct ccw_device *cdev)
1205 unsigned long flags;
1206 struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
1208 if (vcdev && cdev->online) {
1209 if (vcdev->device_lost)
1210 virtio_break_device(&vcdev->vdev);
1211 unregister_virtio_device(&vcdev->vdev);
1212 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1213 dev_set_drvdata(&cdev->dev, NULL);
1214 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1216 cdev->handler = NULL;
1219 static int virtio_ccw_offline(struct ccw_device *cdev)
1221 unsigned long flags;
1222 struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
1226 if (vcdev->device_lost)
1227 virtio_break_device(&vcdev->vdev);
1228 unregister_virtio_device(&vcdev->vdev);
1229 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1230 dev_set_drvdata(&cdev->dev, NULL);
1231 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1235 static int virtio_ccw_set_transport_rev(struct virtio_ccw_device *vcdev)
1237 struct virtio_rev_info *rev;
1241 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw));
1244 rev = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*rev));
1246 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
1250 /* Set transport revision */
1251 ccw->cmd_code = CCW_CMD_SET_VIRTIO_REV;
1253 ccw->count = sizeof(*rev);
1254 ccw->cda = (__u32)(unsigned long)rev;
1256 vcdev->revision = VIRTIO_CCW_REV_MAX;
1258 rev->revision = vcdev->revision;
1259 /* none of our supported revisions carry payload */
1261 ret = ccw_io_helper(vcdev, ccw,
1262 VIRTIO_CCW_DOING_SET_VIRTIO_REV);
1263 if (ret == -EOPNOTSUPP) {
1264 if (vcdev->revision == 0)
1266 * The host device does not support setting
1267 * the revision: let's operate it in legacy
1274 } while (ret == -EOPNOTSUPP);
1276 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
1277 ccw_device_dma_free(vcdev->cdev, rev, sizeof(*rev));
1281 static int virtio_ccw_online(struct ccw_device *cdev)
1284 struct virtio_ccw_device *vcdev;
1285 unsigned long flags;
1287 vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL);
1289 dev_warn(&cdev->dev, "Could not get memory for virtio\n");
1293 vcdev->vdev.dev.parent = &cdev->dev;
1295 vcdev->dma_area = ccw_device_dma_zalloc(vcdev->cdev,
1296 sizeof(*vcdev->dma_area));
1297 if (!vcdev->dma_area) {
1302 vcdev->is_thinint = virtio_ccw_use_airq; /* at least try */
1304 vcdev->vdev.dev.release = virtio_ccw_release_dev;
1305 vcdev->vdev.config = &virtio_ccw_config_ops;
1306 init_waitqueue_head(&vcdev->wait_q);
1307 INIT_LIST_HEAD(&vcdev->virtqueues);
1308 spin_lock_init(&vcdev->lock);
1309 mutex_init(&vcdev->io_lock);
1311 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1312 dev_set_drvdata(&cdev->dev, vcdev);
1313 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1314 vcdev->vdev.id.vendor = cdev->id.cu_type;
1315 vcdev->vdev.id.device = cdev->id.cu_model;
1317 ret = virtio_ccw_set_transport_rev(vcdev);
1321 ret = register_virtio_device(&vcdev->vdev);
1323 dev_warn(&cdev->dev, "Failed to register virtio device: %d\n",
1329 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1330 dev_set_drvdata(&cdev->dev, NULL);
1331 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1332 put_device(&vcdev->vdev.dev);
1336 ccw_device_dma_free(vcdev->cdev, vcdev->dma_area,
1337 sizeof(*vcdev->dma_area));
1343 static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event)
1346 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1349 * Make sure vcdev is set
1350 * i.e. set_offline/remove callback not already running
1357 vcdev->device_lost = true;
1370 static struct ccw_device_id virtio_ids[] = {
1371 { CCW_DEVICE(0x3832, 0) },
1375 static struct ccw_driver virtio_ccw_driver = {
1377 .owner = THIS_MODULE,
1378 .name = "virtio_ccw",
1381 .probe = virtio_ccw_probe,
1382 .remove = virtio_ccw_remove,
1383 .set_offline = virtio_ccw_offline,
1384 .set_online = virtio_ccw_online,
1385 .notify = virtio_ccw_cio_notify,
1386 .int_class = IRQIO_VIR,
1389 static int __init pure_hex(char **cp, unsigned int *val, int min_digit,
1390 int max_digit, int max_val)
1397 while (diff <= max_digit) {
1398 int value = hex_to_bin(**cp);
1402 *val = *val * 16 + value;
1407 if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
1413 static int __init parse_busid(char *str, unsigned int *cssid,
1414 unsigned int *ssid, unsigned int *devno)
1425 ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
1426 if (ret || (str_work[0] != '.'))
1429 ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
1430 if (ret || (str_work[0] != '.'))
1433 ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
1434 if (ret || (str_work[0] != '\0'))
1442 static void __init no_auto_parse(void)
1444 unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
1449 while ((parm = strsep(&str, ","))) {
1450 rc = parse_busid(strsep(&parm, "-"), &from_cssid,
1455 rc = parse_busid(parm, &to_cssid,
1457 if ((from_ssid > to_ssid) ||
1458 ((from_ssid == to_ssid) && (from > to)))
1461 to_cssid = from_cssid;
1462 to_ssid = from_ssid;
1467 while ((from_ssid < to_ssid) ||
1468 ((from_ssid == to_ssid) && (from <= to))) {
1469 set_bit(from, devs_no_auto[from_ssid]);
1471 if (from > __MAX_SUBCHANNEL) {
1479 static int __init virtio_ccw_init(void)
1483 /* parse no_auto string before we do anything further */
1486 summary_indicators = cio_dma_zalloc(MAX_AIRQ_AREAS);
1487 if (!summary_indicators)
1489 rc = ccw_driver_register(&virtio_ccw_driver);
1491 cio_dma_free(summary_indicators, MAX_AIRQ_AREAS);
1494 device_initcall(virtio_ccw_init);