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 virtio_ccw_device
{
50 struct virtio_device vdev
;
52 __u8 config
[VIRTIO_CCW_CONFIG_SIZE
];
53 struct ccw_device
*cdev
;
56 unsigned int revision
; /* Transport revision */
57 wait_queue_head_t wait_q
;
59 struct mutex io_lock
; /* Serializes I/O requests */
60 struct list_head virtqueues
;
61 unsigned long indicators
;
62 unsigned long indicators2
;
63 struct vq_config_block
*config_block
;
67 unsigned int config_ready
;
72 struct vq_info_block_legacy
{
79 struct vq_info_block
{
88 struct virtio_feature_desc
{
93 struct virtio_thinint_area
{
94 unsigned long summary_indicator
;
95 unsigned long indicator
;
100 struct virtio_rev_info
{
106 /* the highest virtio-ccw revision we support */
107 #define VIRTIO_CCW_REV_MAX 1
109 struct virtio_ccw_vq_info
{
110 struct virtqueue
*vq
;
113 struct vq_info_block s
;
114 struct vq_info_block_legacy l
;
117 struct list_head node
;
121 #define VIRTIO_AIRQ_ISC IO_SCH_ISC /* inherit from subchannel */
123 #define VIRTIO_IV_BITS (L1_CACHE_BYTES * 8)
124 #define MAX_AIRQ_AREAS 20
126 static int virtio_ccw_use_airq
= 1;
130 u8 summary_indicator
;
131 struct airq_struct airq
;
134 static struct airq_info
*airq_areas
[MAX_AIRQ_AREAS
];
136 #define CCW_CMD_SET_VQ 0x13
137 #define CCW_CMD_VDEV_RESET 0x33
138 #define CCW_CMD_SET_IND 0x43
139 #define CCW_CMD_SET_CONF_IND 0x53
140 #define CCW_CMD_READ_FEAT 0x12
141 #define CCW_CMD_WRITE_FEAT 0x11
142 #define CCW_CMD_READ_CONF 0x22
143 #define CCW_CMD_WRITE_CONF 0x21
144 #define CCW_CMD_WRITE_STATUS 0x31
145 #define CCW_CMD_READ_VQ_CONF 0x32
146 #define CCW_CMD_READ_STATUS 0x72
147 #define CCW_CMD_SET_IND_ADAPTER 0x73
148 #define CCW_CMD_SET_VIRTIO_REV 0x83
150 #define VIRTIO_CCW_DOING_SET_VQ 0x00010000
151 #define VIRTIO_CCW_DOING_RESET 0x00040000
152 #define VIRTIO_CCW_DOING_READ_FEAT 0x00080000
153 #define VIRTIO_CCW_DOING_WRITE_FEAT 0x00100000
154 #define VIRTIO_CCW_DOING_READ_CONFIG 0x00200000
155 #define VIRTIO_CCW_DOING_WRITE_CONFIG 0x00400000
156 #define VIRTIO_CCW_DOING_WRITE_STATUS 0x00800000
157 #define VIRTIO_CCW_DOING_SET_IND 0x01000000
158 #define VIRTIO_CCW_DOING_READ_VQ_CONF 0x02000000
159 #define VIRTIO_CCW_DOING_SET_CONF_IND 0x04000000
160 #define VIRTIO_CCW_DOING_SET_IND_ADAPTER 0x08000000
161 #define VIRTIO_CCW_DOING_SET_VIRTIO_REV 0x10000000
162 #define VIRTIO_CCW_DOING_READ_STATUS 0x20000000
163 #define VIRTIO_CCW_INTPARM_MASK 0xffff0000
165 static struct virtio_ccw_device
*to_vc_device(struct virtio_device
*vdev
)
167 return container_of(vdev
, struct virtio_ccw_device
, vdev
);
170 static void drop_airq_indicator(struct virtqueue
*vq
, struct airq_info
*info
)
172 unsigned long i
, flags
;
174 write_lock_irqsave(&info
->lock
, flags
);
175 for (i
= 0; i
< airq_iv_end(info
->aiv
); i
++) {
176 if (vq
== (void *)airq_iv_get_ptr(info
->aiv
, i
)) {
177 airq_iv_free_bit(info
->aiv
, i
);
178 airq_iv_set_ptr(info
->aiv
, i
, 0);
182 write_unlock_irqrestore(&info
->lock
, flags
);
185 static void virtio_airq_handler(struct airq_struct
*airq
, bool floating
)
187 struct airq_info
*info
= container_of(airq
, struct airq_info
, airq
);
190 inc_irq_stat(IRQIO_VAI
);
191 read_lock(&info
->lock
);
192 /* Walk through indicators field, summary indicator active. */
194 ai
= airq_iv_scan(info
->aiv
, ai
, airq_iv_end(info
->aiv
));
197 vring_interrupt(0, (void *)airq_iv_get_ptr(info
->aiv
, ai
));
199 info
->summary_indicator
= 0;
201 /* Walk through indicators field, summary indicator not active. */
203 ai
= airq_iv_scan(info
->aiv
, ai
, airq_iv_end(info
->aiv
));
206 vring_interrupt(0, (void *)airq_iv_get_ptr(info
->aiv
, ai
));
208 read_unlock(&info
->lock
);
211 static struct airq_info
*new_airq_info(void)
213 struct airq_info
*info
;
216 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
219 rwlock_init(&info
->lock
);
220 info
->aiv
= airq_iv_create(VIRTIO_IV_BITS
, AIRQ_IV_ALLOC
| AIRQ_IV_PTR
);
225 info
->airq
.handler
= virtio_airq_handler
;
226 info
->airq
.lsi_ptr
= &info
->summary_indicator
;
227 info
->airq
.lsi_mask
= 0xff;
228 info
->airq
.isc
= VIRTIO_AIRQ_ISC
;
229 rc
= register_adapter_interrupt(&info
->airq
);
231 airq_iv_release(info
->aiv
);
238 static unsigned long get_airq_indicator(struct virtqueue
*vqs
[], int nvqs
,
239 u64
*first
, void **airq_info
)
242 struct airq_info
*info
;
243 unsigned long indicator_addr
= 0;
244 unsigned long bit
, flags
;
246 for (i
= 0; i
< MAX_AIRQ_AREAS
&& !indicator_addr
; i
++) {
248 airq_areas
[i
] = new_airq_info();
249 info
= airq_areas
[i
];
252 write_lock_irqsave(&info
->lock
, flags
);
253 bit
= airq_iv_alloc(info
->aiv
, nvqs
);
255 /* Not enough vacancies. */
256 write_unlock_irqrestore(&info
->lock
, flags
);
261 indicator_addr
= (unsigned long)info
->aiv
->vector
;
262 for (j
= 0; j
< nvqs
; j
++) {
263 airq_iv_set_ptr(info
->aiv
, bit
+ j
,
264 (unsigned long)vqs
[j
]);
266 write_unlock_irqrestore(&info
->lock
, flags
);
268 return indicator_addr
;
271 static void virtio_ccw_drop_indicators(struct virtio_ccw_device
*vcdev
)
273 struct virtio_ccw_vq_info
*info
;
275 if (!vcdev
->airq_info
)
277 list_for_each_entry(info
, &vcdev
->virtqueues
, node
)
278 drop_airq_indicator(info
->vq
, vcdev
->airq_info
);
281 static int doing_io(struct virtio_ccw_device
*vcdev
, __u32 flag
)
286 spin_lock_irqsave(get_ccwdev_lock(vcdev
->cdev
), flags
);
290 ret
= vcdev
->curr_io
& flag
;
291 spin_unlock_irqrestore(get_ccwdev_lock(vcdev
->cdev
), flags
);
295 static int ccw_io_helper(struct virtio_ccw_device
*vcdev
,
296 struct ccw1
*ccw
, __u32 intparm
)
300 int flag
= intparm
& VIRTIO_CCW_INTPARM_MASK
;
302 mutex_lock(&vcdev
->io_lock
);
304 spin_lock_irqsave(get_ccwdev_lock(vcdev
->cdev
), flags
);
305 ret
= ccw_device_start(vcdev
->cdev
, ccw
, intparm
, 0, 0);
309 vcdev
->curr_io
|= flag
;
311 spin_unlock_irqrestore(get_ccwdev_lock(vcdev
->cdev
), flags
);
313 } while (ret
== -EBUSY
);
314 wait_event(vcdev
->wait_q
, doing_io(vcdev
, flag
) == 0);
315 ret
= ret
? ret
: vcdev
->err
;
316 mutex_unlock(&vcdev
->io_lock
);
320 static void virtio_ccw_drop_indicator(struct virtio_ccw_device
*vcdev
,
324 unsigned long *indicatorp
= NULL
;
325 struct virtio_thinint_area
*thinint_area
= NULL
;
326 struct airq_info
*airq_info
= vcdev
->airq_info
;
328 if (vcdev
->is_thinint
) {
329 thinint_area
= kzalloc(sizeof(*thinint_area
),
330 GFP_DMA
| GFP_KERNEL
);
333 thinint_area
->summary_indicator
=
334 (unsigned long) &airq_info
->summary_indicator
;
335 thinint_area
->isc
= VIRTIO_AIRQ_ISC
;
336 ccw
->cmd_code
= CCW_CMD_SET_IND_ADAPTER
;
337 ccw
->count
= sizeof(*thinint_area
);
338 ccw
->cda
= (__u32
)(unsigned long) thinint_area
;
340 /* payload is the address of the indicators */
341 indicatorp
= kmalloc(sizeof(&vcdev
->indicators
),
342 GFP_DMA
| GFP_KERNEL
);
346 ccw
->cmd_code
= CCW_CMD_SET_IND
;
347 ccw
->count
= sizeof(&vcdev
->indicators
);
348 ccw
->cda
= (__u32
)(unsigned long) indicatorp
;
350 /* Deregister indicators from host. */
351 vcdev
->indicators
= 0;
353 ret
= ccw_io_helper(vcdev
, ccw
,
355 VIRTIO_CCW_DOING_SET_IND_ADAPTER
:
356 VIRTIO_CCW_DOING_SET_IND
);
357 if (ret
&& (ret
!= -ENODEV
))
358 dev_info(&vcdev
->cdev
->dev
,
359 "Failed to deregister indicators (%d)\n", ret
);
360 else if (vcdev
->is_thinint
)
361 virtio_ccw_drop_indicators(vcdev
);
366 static inline long __do_kvm_notify(struct subchannel_id schid
,
367 unsigned long queue_index
,
370 register unsigned long __nr
asm("1") = KVM_S390_VIRTIO_CCW_NOTIFY
;
371 register struct subchannel_id __schid
asm("2") = schid
;
372 register unsigned long __index
asm("3") = queue_index
;
373 register long __rc
asm("2");
374 register long __cookie
asm("4") = cookie
;
376 asm volatile ("diag 2,4,0x500\n"
377 : "=d" (__rc
) : "d" (__nr
), "d" (__schid
), "d" (__index
),
383 static inline long do_kvm_notify(struct subchannel_id schid
,
384 unsigned long queue_index
,
387 diag_stat_inc(DIAG_STAT_X500
);
388 return __do_kvm_notify(schid
, queue_index
, cookie
);
391 static bool virtio_ccw_kvm_notify(struct virtqueue
*vq
)
393 struct virtio_ccw_vq_info
*info
= vq
->priv
;
394 struct virtio_ccw_device
*vcdev
;
395 struct subchannel_id schid
;
397 vcdev
= to_vc_device(info
->vq
->vdev
);
398 ccw_device_get_schid(vcdev
->cdev
, &schid
);
399 info
->cookie
= do_kvm_notify(schid
, vq
->index
, info
->cookie
);
400 if (info
->cookie
< 0)
405 static int virtio_ccw_read_vq_conf(struct virtio_ccw_device
*vcdev
,
406 struct ccw1
*ccw
, int index
)
410 vcdev
->config_block
->index
= index
;
411 ccw
->cmd_code
= CCW_CMD_READ_VQ_CONF
;
413 ccw
->count
= sizeof(struct vq_config_block
);
414 ccw
->cda
= (__u32
)(unsigned long)(vcdev
->config_block
);
415 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_READ_VQ_CONF
);
418 return vcdev
->config_block
->num
?: -ENOENT
;
421 static void virtio_ccw_del_vq(struct virtqueue
*vq
, struct ccw1
*ccw
)
423 struct virtio_ccw_device
*vcdev
= to_vc_device(vq
->vdev
);
424 struct virtio_ccw_vq_info
*info
= vq
->priv
;
427 unsigned int index
= vq
->index
;
429 /* Remove from our list. */
430 spin_lock_irqsave(&vcdev
->lock
, flags
);
431 list_del(&info
->node
);
432 spin_unlock_irqrestore(&vcdev
->lock
, flags
);
434 /* Release from host. */
435 if (vcdev
->revision
== 0) {
436 info
->info_block
->l
.queue
= 0;
437 info
->info_block
->l
.align
= 0;
438 info
->info_block
->l
.index
= index
;
439 info
->info_block
->l
.num
= 0;
440 ccw
->count
= sizeof(info
->info_block
->l
);
442 info
->info_block
->s
.desc
= 0;
443 info
->info_block
->s
.index
= index
;
444 info
->info_block
->s
.num
= 0;
445 info
->info_block
->s
.avail
= 0;
446 info
->info_block
->s
.used
= 0;
447 ccw
->count
= sizeof(info
->info_block
->s
);
449 ccw
->cmd_code
= CCW_CMD_SET_VQ
;
451 ccw
->cda
= (__u32
)(unsigned long)(info
->info_block
);
452 ret
= ccw_io_helper(vcdev
, ccw
,
453 VIRTIO_CCW_DOING_SET_VQ
| index
);
455 * -ENODEV isn't considered an error: The device is gone anyway.
456 * This may happen on device detach.
458 if (ret
&& (ret
!= -ENODEV
))
459 dev_warn(&vq
->vdev
->dev
, "Error %d while deleting queue %d\n",
462 vring_del_virtqueue(vq
);
463 kfree(info
->info_block
);
467 static void virtio_ccw_del_vqs(struct virtio_device
*vdev
)
469 struct virtqueue
*vq
, *n
;
471 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
473 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
477 virtio_ccw_drop_indicator(vcdev
, ccw
);
479 list_for_each_entry_safe(vq
, n
, &vdev
->vqs
, list
)
480 virtio_ccw_del_vq(vq
, ccw
);
485 static struct virtqueue
*virtio_ccw_setup_vq(struct virtio_device
*vdev
,
486 int i
, vq_callback_t
*callback
,
487 const char *name
, bool ctx
,
490 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
492 struct virtqueue
*vq
= NULL
;
493 struct virtio_ccw_vq_info
*info
;
498 /* Allocate queue. */
499 info
= kzalloc(sizeof(struct virtio_ccw_vq_info
), GFP_KERNEL
);
501 dev_warn(&vcdev
->cdev
->dev
, "no info\n");
505 info
->info_block
= kzalloc(sizeof(*info
->info_block
),
506 GFP_DMA
| GFP_KERNEL
);
507 if (!info
->info_block
) {
508 dev_warn(&vcdev
->cdev
->dev
, "no info block\n");
512 info
->num
= virtio_ccw_read_vq_conf(vcdev
, ccw
, i
);
517 may_reduce
= vcdev
->revision
> 0;
518 vq
= vring_create_virtqueue(i
, info
->num
, KVM_VIRTIO_CCW_RING_ALIGN
,
519 vdev
, true, may_reduce
, ctx
,
520 virtio_ccw_kvm_notify
, callback
, name
);
523 /* For now, we fail if we can't get the requested size. */
524 dev_warn(&vcdev
->cdev
->dev
, "no vq\n");
528 /* it may have been reduced */
529 info
->num
= virtqueue_get_vring_size(vq
);
531 /* Register it with the host. */
532 queue
= virtqueue_get_desc_addr(vq
);
533 if (vcdev
->revision
== 0) {
534 info
->info_block
->l
.queue
= queue
;
535 info
->info_block
->l
.align
= KVM_VIRTIO_CCW_RING_ALIGN
;
536 info
->info_block
->l
.index
= i
;
537 info
->info_block
->l
.num
= info
->num
;
538 ccw
->count
= sizeof(info
->info_block
->l
);
540 info
->info_block
->s
.desc
= queue
;
541 info
->info_block
->s
.index
= i
;
542 info
->info_block
->s
.num
= info
->num
;
543 info
->info_block
->s
.avail
= (__u64
)virtqueue_get_avail_addr(vq
);
544 info
->info_block
->s
.used
= (__u64
)virtqueue_get_used_addr(vq
);
545 ccw
->count
= sizeof(info
->info_block
->s
);
547 ccw
->cmd_code
= CCW_CMD_SET_VQ
;
549 ccw
->cda
= (__u32
)(unsigned long)(info
->info_block
);
550 err
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_SET_VQ
| i
);
552 dev_warn(&vcdev
->cdev
->dev
, "SET_VQ failed\n");
559 /* Save it to our list. */
560 spin_lock_irqsave(&vcdev
->lock
, flags
);
561 list_add(&info
->node
, &vcdev
->virtqueues
);
562 spin_unlock_irqrestore(&vcdev
->lock
, flags
);
568 vring_del_virtqueue(vq
);
570 kfree(info
->info_block
);
576 static int virtio_ccw_register_adapter_ind(struct virtio_ccw_device
*vcdev
,
577 struct virtqueue
*vqs
[], int nvqs
,
581 struct virtio_thinint_area
*thinint_area
= NULL
;
582 struct airq_info
*info
;
584 thinint_area
= kzalloc(sizeof(*thinint_area
), GFP_DMA
| GFP_KERNEL
);
589 /* Try to get an indicator. */
590 thinint_area
->indicator
= get_airq_indicator(vqs
, nvqs
,
591 &thinint_area
->bit_nr
,
593 if (!thinint_area
->indicator
) {
597 info
= vcdev
->airq_info
;
598 thinint_area
->summary_indicator
=
599 (unsigned long) &info
->summary_indicator
;
600 thinint_area
->isc
= VIRTIO_AIRQ_ISC
;
601 ccw
->cmd_code
= CCW_CMD_SET_IND_ADAPTER
;
602 ccw
->flags
= CCW_FLAG_SLI
;
603 ccw
->count
= sizeof(*thinint_area
);
604 ccw
->cda
= (__u32
)(unsigned long)thinint_area
;
605 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_SET_IND_ADAPTER
);
607 if (ret
== -EOPNOTSUPP
) {
609 * The host does not support adapter interrupts
610 * for virtio-ccw, stop trying.
612 virtio_ccw_use_airq
= 0;
613 pr_info("Adapter interrupts unsupported on host\n");
615 dev_warn(&vcdev
->cdev
->dev
,
616 "enabling adapter interrupts = %d\n", ret
);
617 virtio_ccw_drop_indicators(vcdev
);
624 static int virtio_ccw_find_vqs(struct virtio_device
*vdev
, unsigned nvqs
,
625 struct virtqueue
*vqs
[],
626 vq_callback_t
*callbacks
[],
627 const char * const names
[],
629 struct irq_affinity
*desc
)
631 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
632 unsigned long *indicatorp
= NULL
;
633 int ret
, i
, queue_idx
= 0;
636 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
640 for (i
= 0; i
< nvqs
; ++i
) {
646 vqs
[i
] = virtio_ccw_setup_vq(vdev
, queue_idx
++, callbacks
[i
],
647 names
[i
], ctx
? ctx
[i
] : false,
649 if (IS_ERR(vqs
[i
])) {
650 ret
= PTR_ERR(vqs
[i
]);
657 * We need a data area under 2G to communicate. Our payload is
658 * the address of the indicators.
660 indicatorp
= kmalloc(sizeof(&vcdev
->indicators
), GFP_DMA
| GFP_KERNEL
);
663 *indicatorp
= (unsigned long) &vcdev
->indicators
;
664 if (vcdev
->is_thinint
) {
665 ret
= virtio_ccw_register_adapter_ind(vcdev
, vqs
, nvqs
, ccw
);
667 /* no error, just fall back to legacy interrupts */
668 vcdev
->is_thinint
= false;
670 if (!vcdev
->is_thinint
) {
671 /* Register queue indicators with host. */
672 vcdev
->indicators
= 0;
673 ccw
->cmd_code
= CCW_CMD_SET_IND
;
675 ccw
->count
= sizeof(&vcdev
->indicators
);
676 ccw
->cda
= (__u32
)(unsigned long) indicatorp
;
677 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_SET_IND
);
681 /* Register indicators2 with host for config changes */
682 *indicatorp
= (unsigned long) &vcdev
->indicators2
;
683 vcdev
->indicators2
= 0;
684 ccw
->cmd_code
= CCW_CMD_SET_CONF_IND
;
686 ccw
->count
= sizeof(&vcdev
->indicators2
);
687 ccw
->cda
= (__u32
)(unsigned long) indicatorp
;
688 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_SET_CONF_IND
);
698 virtio_ccw_del_vqs(vdev
);
702 static void virtio_ccw_reset(struct virtio_device
*vdev
)
704 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
707 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
711 /* Zero status bits. */
714 /* Send a reset ccw on device. */
715 ccw
->cmd_code
= CCW_CMD_VDEV_RESET
;
719 ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_RESET
);
723 static u64
virtio_ccw_get_features(struct virtio_device
*vdev
)
725 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
726 struct virtio_feature_desc
*features
;
731 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
735 features
= kzalloc(sizeof(*features
), GFP_DMA
| GFP_KERNEL
);
740 /* Read the feature bits from the host. */
742 ccw
->cmd_code
= CCW_CMD_READ_FEAT
;
744 ccw
->count
= sizeof(*features
);
745 ccw
->cda
= (__u32
)(unsigned long)features
;
746 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_READ_FEAT
);
752 rc
= le32_to_cpu(features
->features
);
754 if (vcdev
->revision
== 0)
757 /* Read second half of the feature bits from the host. */
759 ccw
->cmd_code
= CCW_CMD_READ_FEAT
;
761 ccw
->count
= sizeof(*features
);
762 ccw
->cda
= (__u32
)(unsigned long)features
;
763 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_READ_FEAT
);
765 rc
|= (u64
)le32_to_cpu(features
->features
) << 32;
773 static void ccw_transport_features(struct virtio_device
*vdev
)
776 * Currently nothing to do here.
780 static int virtio_ccw_finalize_features(struct virtio_device
*vdev
)
782 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
783 struct virtio_feature_desc
*features
;
787 if (vcdev
->revision
>= 1 &&
788 !__virtio_test_bit(vdev
, VIRTIO_F_VERSION_1
)) {
789 dev_err(&vdev
->dev
, "virtio: device uses revision 1 "
790 "but does not have VIRTIO_F_VERSION_1\n");
794 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
798 features
= kzalloc(sizeof(*features
), GFP_DMA
| GFP_KERNEL
);
803 /* Give virtio_ring a chance to accept features. */
804 vring_transport_features(vdev
);
806 /* Give virtio_ccw a chance to accept features. */
807 ccw_transport_features(vdev
);
810 features
->features
= cpu_to_le32((u32
)vdev
->features
);
811 /* Write the first half of the feature bits to the host. */
812 ccw
->cmd_code
= CCW_CMD_WRITE_FEAT
;
814 ccw
->count
= sizeof(*features
);
815 ccw
->cda
= (__u32
)(unsigned long)features
;
816 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_WRITE_FEAT
);
820 if (vcdev
->revision
== 0)
824 features
->features
= cpu_to_le32(vdev
->features
>> 32);
825 /* Write the second half of the feature bits to the host. */
826 ccw
->cmd_code
= CCW_CMD_WRITE_FEAT
;
828 ccw
->count
= sizeof(*features
);
829 ccw
->cda
= (__u32
)(unsigned long)features
;
830 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_WRITE_FEAT
);
839 static void virtio_ccw_get_config(struct virtio_device
*vdev
,
840 unsigned int offset
, void *buf
, unsigned len
)
842 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
848 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
852 config_area
= kzalloc(VIRTIO_CCW_CONFIG_SIZE
, GFP_DMA
| GFP_KERNEL
);
856 /* Read the config area from the host. */
857 ccw
->cmd_code
= CCW_CMD_READ_CONF
;
859 ccw
->count
= offset
+ len
;
860 ccw
->cda
= (__u32
)(unsigned long)config_area
;
861 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_READ_CONFIG
);
865 spin_lock_irqsave(&vcdev
->lock
, flags
);
866 memcpy(vcdev
->config
, config_area
, offset
+ len
);
867 if (vcdev
->config_ready
< offset
+ len
)
868 vcdev
->config_ready
= offset
+ len
;
869 spin_unlock_irqrestore(&vcdev
->lock
, flags
);
871 memcpy(buf
, config_area
+ offset
, len
);
878 static void virtio_ccw_set_config(struct virtio_device
*vdev
,
879 unsigned int offset
, const void *buf
,
882 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
887 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
891 config_area
= kzalloc(VIRTIO_CCW_CONFIG_SIZE
, GFP_DMA
| GFP_KERNEL
);
895 /* Make sure we don't overwrite fields. */
896 if (vcdev
->config_ready
< offset
)
897 virtio_ccw_get_config(vdev
, 0, NULL
, offset
);
898 spin_lock_irqsave(&vcdev
->lock
, flags
);
899 memcpy(&vcdev
->config
[offset
], buf
, len
);
900 /* Write the config area to the host. */
901 memcpy(config_area
, vcdev
->config
, sizeof(vcdev
->config
));
902 spin_unlock_irqrestore(&vcdev
->lock
, flags
);
903 ccw
->cmd_code
= CCW_CMD_WRITE_CONF
;
905 ccw
->count
= offset
+ len
;
906 ccw
->cda
= (__u32
)(unsigned long)config_area
;
907 ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_WRITE_CONFIG
);
914 static u8
virtio_ccw_get_status(struct virtio_device
*vdev
)
916 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
917 u8 old_status
= *vcdev
->status
;
920 if (vcdev
->revision
< 1)
921 return *vcdev
->status
;
923 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
927 ccw
->cmd_code
= CCW_CMD_READ_STATUS
;
929 ccw
->count
= sizeof(*vcdev
->status
);
930 ccw
->cda
= (__u32
)(unsigned long)vcdev
->status
;
931 ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_READ_STATUS
);
933 * If the channel program failed (should only happen if the device
934 * was hotunplugged, and then we clean up via the machine check
935 * handler anyway), vcdev->status was not overwritten and we just
936 * return the old status, which is fine.
940 return *vcdev
->status
;
943 static void virtio_ccw_set_status(struct virtio_device
*vdev
, u8 status
)
945 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
946 u8 old_status
= *vcdev
->status
;
950 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
954 /* Write the status to the host. */
955 *vcdev
->status
= status
;
956 ccw
->cmd_code
= CCW_CMD_WRITE_STATUS
;
958 ccw
->count
= sizeof(status
);
959 ccw
->cda
= (__u32
)(unsigned long)vcdev
->status
;
960 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_WRITE_STATUS
);
961 /* Write failed? We assume status is unchanged. */
963 *vcdev
->status
= old_status
;
967 static const char *virtio_ccw_bus_name(struct virtio_device
*vdev
)
969 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
971 return dev_name(&vcdev
->cdev
->dev
);
974 static const struct virtio_config_ops virtio_ccw_config_ops
= {
975 .get_features
= virtio_ccw_get_features
,
976 .finalize_features
= virtio_ccw_finalize_features
,
977 .get
= virtio_ccw_get_config
,
978 .set
= virtio_ccw_set_config
,
979 .get_status
= virtio_ccw_get_status
,
980 .set_status
= virtio_ccw_set_status
,
981 .reset
= virtio_ccw_reset
,
982 .find_vqs
= virtio_ccw_find_vqs
,
983 .del_vqs
= virtio_ccw_del_vqs
,
984 .bus_name
= virtio_ccw_bus_name
,
989 * ccw bus driver related functions
992 static void virtio_ccw_release_dev(struct device
*_d
)
994 struct virtio_device
*dev
= dev_to_virtio(_d
);
995 struct virtio_ccw_device
*vcdev
= to_vc_device(dev
);
997 kfree(vcdev
->status
);
998 kfree(vcdev
->config_block
);
1002 static int irb_is_error(struct irb
*irb
)
1004 if (scsw_cstat(&irb
->scsw
) != 0)
1006 if (scsw_dstat(&irb
->scsw
) & ~(DEV_STAT_CHN_END
| DEV_STAT_DEV_END
))
1008 if (scsw_cc(&irb
->scsw
) != 0)
1013 static struct virtqueue
*virtio_ccw_vq_by_ind(struct virtio_ccw_device
*vcdev
,
1016 struct virtio_ccw_vq_info
*info
;
1017 unsigned long flags
;
1018 struct virtqueue
*vq
;
1021 spin_lock_irqsave(&vcdev
->lock
, flags
);
1022 list_for_each_entry(info
, &vcdev
->virtqueues
, node
) {
1023 if (info
->vq
->index
== index
) {
1028 spin_unlock_irqrestore(&vcdev
->lock
, flags
);
1032 static void virtio_ccw_check_activity(struct virtio_ccw_device
*vcdev
,
1035 if (vcdev
->curr_io
& activity
) {
1037 case VIRTIO_CCW_DOING_READ_FEAT
:
1038 case VIRTIO_CCW_DOING_WRITE_FEAT
:
1039 case VIRTIO_CCW_DOING_READ_CONFIG
:
1040 case VIRTIO_CCW_DOING_WRITE_CONFIG
:
1041 case VIRTIO_CCW_DOING_WRITE_STATUS
:
1042 case VIRTIO_CCW_DOING_READ_STATUS
:
1043 case VIRTIO_CCW_DOING_SET_VQ
:
1044 case VIRTIO_CCW_DOING_SET_IND
:
1045 case VIRTIO_CCW_DOING_SET_CONF_IND
:
1046 case VIRTIO_CCW_DOING_RESET
:
1047 case VIRTIO_CCW_DOING_READ_VQ_CONF
:
1048 case VIRTIO_CCW_DOING_SET_IND_ADAPTER
:
1049 case VIRTIO_CCW_DOING_SET_VIRTIO_REV
:
1050 vcdev
->curr_io
&= ~activity
;
1051 wake_up(&vcdev
->wait_q
);
1054 /* don't know what to do... */
1055 dev_warn(&vcdev
->cdev
->dev
,
1056 "Suspicious activity '%08x'\n", activity
);
1063 static void virtio_ccw_int_handler(struct ccw_device
*cdev
,
1064 unsigned long intparm
,
1067 __u32 activity
= intparm
& VIRTIO_CCW_INTPARM_MASK
;
1068 struct virtio_ccw_device
*vcdev
= dev_get_drvdata(&cdev
->dev
);
1070 struct virtqueue
*vq
;
1075 vcdev
->err
= PTR_ERR(irb
);
1076 virtio_ccw_check_activity(vcdev
, activity
);
1077 /* Don't poke around indicators, something's wrong. */
1080 /* Check if it's a notification from the host. */
1081 if ((intparm
== 0) &&
1082 (scsw_stctl(&irb
->scsw
) ==
1083 (SCSW_STCTL_ALERT_STATUS
| SCSW_STCTL_STATUS_PEND
))) {
1086 if (irb_is_error(irb
)) {
1087 /* Command reject? */
1088 if ((scsw_dstat(&irb
->scsw
) & DEV_STAT_UNIT_CHECK
) &&
1089 (irb
->ecw
[0] & SNS0_CMD_REJECT
))
1090 vcdev
->err
= -EOPNOTSUPP
;
1092 /* Map everything else to -EIO. */
1095 virtio_ccw_check_activity(vcdev
, activity
);
1096 for_each_set_bit(i
, &vcdev
->indicators
,
1097 sizeof(vcdev
->indicators
) * BITS_PER_BYTE
) {
1098 /* The bit clear must happen before the vring kick. */
1099 clear_bit(i
, &vcdev
->indicators
);
1101 vq
= virtio_ccw_vq_by_ind(vcdev
, i
);
1102 vring_interrupt(0, vq
);
1104 if (test_bit(0, &vcdev
->indicators2
)) {
1105 virtio_config_changed(&vcdev
->vdev
);
1106 clear_bit(0, &vcdev
->indicators2
);
1111 * We usually want to autoonline all devices, but give the admin
1112 * a way to exempt devices from this.
1114 #define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
1116 static unsigned long devs_no_auto
[__MAX_SSID
+ 1][__DEV_WORDS
];
1118 static char *no_auto
= "";
1120 module_param(no_auto
, charp
, 0444);
1121 MODULE_PARM_DESC(no_auto
, "list of ccw bus id ranges not to be auto-onlined");
1123 static int virtio_ccw_check_autoonline(struct ccw_device
*cdev
)
1125 struct ccw_dev_id id
;
1127 ccw_device_get_id(cdev
, &id
);
1128 if (test_bit(id
.devno
, devs_no_auto
[id
.ssid
]))
1133 static void virtio_ccw_auto_online(void *data
, async_cookie_t cookie
)
1135 struct ccw_device
*cdev
= data
;
1138 ret
= ccw_device_set_online(cdev
);
1140 dev_warn(&cdev
->dev
, "Failed to set online: %d\n", ret
);
1143 static int virtio_ccw_probe(struct ccw_device
*cdev
)
1145 cdev
->handler
= virtio_ccw_int_handler
;
1147 if (virtio_ccw_check_autoonline(cdev
))
1148 async_schedule(virtio_ccw_auto_online
, cdev
);
1152 static struct virtio_ccw_device
*virtio_grab_drvdata(struct ccw_device
*cdev
)
1154 unsigned long flags
;
1155 struct virtio_ccw_device
*vcdev
;
1157 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1158 vcdev
= dev_get_drvdata(&cdev
->dev
);
1159 if (!vcdev
|| vcdev
->going_away
) {
1160 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1163 vcdev
->going_away
= true;
1164 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1168 static void virtio_ccw_remove(struct ccw_device
*cdev
)
1170 unsigned long flags
;
1171 struct virtio_ccw_device
*vcdev
= virtio_grab_drvdata(cdev
);
1173 if (vcdev
&& cdev
->online
) {
1174 if (vcdev
->device_lost
)
1175 virtio_break_device(&vcdev
->vdev
);
1176 unregister_virtio_device(&vcdev
->vdev
);
1177 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1178 dev_set_drvdata(&cdev
->dev
, NULL
);
1179 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1181 cdev
->handler
= NULL
;
1184 static int virtio_ccw_offline(struct ccw_device
*cdev
)
1186 unsigned long flags
;
1187 struct virtio_ccw_device
*vcdev
= virtio_grab_drvdata(cdev
);
1191 if (vcdev
->device_lost
)
1192 virtio_break_device(&vcdev
->vdev
);
1193 unregister_virtio_device(&vcdev
->vdev
);
1194 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1195 dev_set_drvdata(&cdev
->dev
, NULL
);
1196 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1200 static int virtio_ccw_set_transport_rev(struct virtio_ccw_device
*vcdev
)
1202 struct virtio_rev_info
*rev
;
1206 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
1209 rev
= kzalloc(sizeof(*rev
), GFP_DMA
| GFP_KERNEL
);
1215 /* Set transport revision */
1216 ccw
->cmd_code
= CCW_CMD_SET_VIRTIO_REV
;
1218 ccw
->count
= sizeof(*rev
);
1219 ccw
->cda
= (__u32
)(unsigned long)rev
;
1221 vcdev
->revision
= VIRTIO_CCW_REV_MAX
;
1223 rev
->revision
= vcdev
->revision
;
1224 /* none of our supported revisions carry payload */
1226 ret
= ccw_io_helper(vcdev
, ccw
,
1227 VIRTIO_CCW_DOING_SET_VIRTIO_REV
);
1228 if (ret
== -EOPNOTSUPP
) {
1229 if (vcdev
->revision
== 0)
1231 * The host device does not support setting
1232 * the revision: let's operate it in legacy
1239 } while (ret
== -EOPNOTSUPP
);
1246 static int virtio_ccw_online(struct ccw_device
*cdev
)
1249 struct virtio_ccw_device
*vcdev
;
1250 unsigned long flags
;
1252 vcdev
= kzalloc(sizeof(*vcdev
), GFP_KERNEL
);
1254 dev_warn(&cdev
->dev
, "Could not get memory for virtio\n");
1259 vcdev
->vdev
.dev
.parent
= &cdev
->dev
;
1260 cdev
->dev
.dma_mask
= &vcdev
->dma_mask
;
1261 /* we are fine with common virtio infrastructure using 64 bit DMA */
1262 ret
= dma_set_mask_and_coherent(&cdev
->dev
, DMA_BIT_MASK(64));
1264 dev_warn(&cdev
->dev
, "Failed to enable 64-bit DMA.\n");
1268 vcdev
->config_block
= kzalloc(sizeof(*vcdev
->config_block
),
1269 GFP_DMA
| GFP_KERNEL
);
1270 if (!vcdev
->config_block
) {
1274 vcdev
->status
= kzalloc(sizeof(*vcdev
->status
), GFP_DMA
| GFP_KERNEL
);
1275 if (!vcdev
->status
) {
1280 vcdev
->is_thinint
= virtio_ccw_use_airq
; /* at least try */
1282 vcdev
->vdev
.dev
.release
= virtio_ccw_release_dev
;
1283 vcdev
->vdev
.config
= &virtio_ccw_config_ops
;
1285 init_waitqueue_head(&vcdev
->wait_q
);
1286 INIT_LIST_HEAD(&vcdev
->virtqueues
);
1287 spin_lock_init(&vcdev
->lock
);
1288 mutex_init(&vcdev
->io_lock
);
1290 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1291 dev_set_drvdata(&cdev
->dev
, vcdev
);
1292 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1293 vcdev
->vdev
.id
.vendor
= cdev
->id
.cu_type
;
1294 vcdev
->vdev
.id
.device
= cdev
->id
.cu_model
;
1296 ret
= virtio_ccw_set_transport_rev(vcdev
);
1300 ret
= register_virtio_device(&vcdev
->vdev
);
1302 dev_warn(&cdev
->dev
, "Failed to register virtio device: %d\n",
1308 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1309 dev_set_drvdata(&cdev
->dev
, NULL
);
1310 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1311 put_device(&vcdev
->vdev
.dev
);
1315 kfree(vcdev
->status
);
1316 kfree(vcdev
->config_block
);
1322 static int virtio_ccw_cio_notify(struct ccw_device
*cdev
, int event
)
1325 struct virtio_ccw_device
*vcdev
= dev_get_drvdata(&cdev
->dev
);
1328 * Make sure vcdev is set
1329 * i.e. set_offline/remove callback not already running
1336 vcdev
->device_lost
= true;
1349 static struct ccw_device_id virtio_ids
[] = {
1350 { CCW_DEVICE(0x3832, 0) },
1354 #ifdef CONFIG_PM_SLEEP
1355 static int virtio_ccw_freeze(struct ccw_device
*cdev
)
1357 struct virtio_ccw_device
*vcdev
= dev_get_drvdata(&cdev
->dev
);
1359 return virtio_device_freeze(&vcdev
->vdev
);
1362 static int virtio_ccw_restore(struct ccw_device
*cdev
)
1364 struct virtio_ccw_device
*vcdev
= dev_get_drvdata(&cdev
->dev
);
1367 ret
= virtio_ccw_set_transport_rev(vcdev
);
1371 return virtio_device_restore(&vcdev
->vdev
);
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
,
1387 #ifdef CONFIG_PM_SLEEP
1388 .freeze
= virtio_ccw_freeze
,
1389 .thaw
= virtio_ccw_restore
,
1390 .restore
= virtio_ccw_restore
,
1394 static int __init
pure_hex(char **cp
, unsigned int *val
, int min_digit
,
1395 int max_digit
, int max_val
)
1402 while (diff
<= max_digit
) {
1403 int value
= hex_to_bin(**cp
);
1407 *val
= *val
* 16 + value
;
1412 if ((diff
< min_digit
) || (diff
> max_digit
) || (*val
> max_val
))
1418 static int __init
parse_busid(char *str
, unsigned int *cssid
,
1419 unsigned int *ssid
, unsigned int *devno
)
1430 ret
= pure_hex(&str_work
, cssid
, 1, 2, __MAX_CSSID
);
1431 if (ret
|| (str_work
[0] != '.'))
1434 ret
= pure_hex(&str_work
, ssid
, 1, 1, __MAX_SSID
);
1435 if (ret
|| (str_work
[0] != '.'))
1438 ret
= pure_hex(&str_work
, devno
, 4, 4, __MAX_SUBCHANNEL
);
1439 if (ret
|| (str_work
[0] != '\0'))
1447 static void __init
no_auto_parse(void)
1449 unsigned int from_cssid
, to_cssid
, from_ssid
, to_ssid
, from
, to
;
1454 while ((parm
= strsep(&str
, ","))) {
1455 rc
= parse_busid(strsep(&parm
, "-"), &from_cssid
,
1460 rc
= parse_busid(parm
, &to_cssid
,
1462 if ((from_ssid
> to_ssid
) ||
1463 ((from_ssid
== to_ssid
) && (from
> to
)))
1466 to_cssid
= from_cssid
;
1467 to_ssid
= from_ssid
;
1472 while ((from_ssid
< to_ssid
) ||
1473 ((from_ssid
== to_ssid
) && (from
<= to
))) {
1474 set_bit(from
, devs_no_auto
[from_ssid
]);
1476 if (from
> __MAX_SUBCHANNEL
) {
1484 static int __init
virtio_ccw_init(void)
1486 /* parse no_auto string before we do anything further */
1488 return ccw_driver_register(&virtio_ccw_driver
);
1490 device_initcall(virtio_ccw_init
);