2 * ccw based virtio transport
4 * Copyright IBM Corp. 2012, 2014
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License (version 2 only)
8 * as published by the Free Software Foundation.
10 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
13 #include <linux/kernel_stat.h>
14 #include <linux/init.h>
15 #include <linux/bootmem.h>
16 #include <linux/err.h>
17 #include <linux/virtio.h>
18 #include <linux/virtio_config.h>
19 #include <linux/slab.h>
20 #include <linux/interrupt.h>
21 #include <linux/virtio_ring.h>
22 #include <linux/pfn.h>
23 #include <linux/async.h>
24 #include <linux/wait.h>
25 #include <linux/list.h>
26 #include <linux/bitops.h>
27 #include <linux/module.h>
29 #include <linux/kvm_para.h>
30 #include <linux/notifier.h>
31 #include <asm/setup.h>
34 #include <asm/ccwdev.h>
35 #include <asm/virtio-ccw.h>
40 * virtio related functions
43 struct vq_config_block
{
48 #define VIRTIO_CCW_CONFIG_SIZE 0x100
49 /* same as PCI config space size, should be enough for all drivers */
51 struct virtio_ccw_device
{
52 struct virtio_device vdev
;
54 __u8 config
[VIRTIO_CCW_CONFIG_SIZE
];
55 struct ccw_device
*cdev
;
58 unsigned int revision
; /* Transport revision */
59 wait_queue_head_t wait_q
;
61 struct list_head virtqueues
;
62 unsigned long indicators
;
63 unsigned long indicators2
;
64 struct vq_config_block
*config_block
;
68 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
;
114 struct vq_info_block s
;
115 struct vq_info_block_legacy l
;
118 struct list_head node
;
122 #define VIRTIO_AIRQ_ISC IO_SCH_ISC /* inherit from subchannel */
124 #define VIRTIO_IV_BITS (L1_CACHE_BYTES * 8)
125 #define MAX_AIRQ_AREAS 20
127 static int virtio_ccw_use_airq
= 1;
131 u8 summary_indicator
;
132 struct airq_struct airq
;
135 static struct airq_info
*airq_areas
[MAX_AIRQ_AREAS
];
137 #define CCW_CMD_SET_VQ 0x13
138 #define CCW_CMD_VDEV_RESET 0x33
139 #define CCW_CMD_SET_IND 0x43
140 #define CCW_CMD_SET_CONF_IND 0x53
141 #define CCW_CMD_READ_FEAT 0x12
142 #define CCW_CMD_WRITE_FEAT 0x11
143 #define CCW_CMD_READ_CONF 0x22
144 #define CCW_CMD_WRITE_CONF 0x21
145 #define CCW_CMD_WRITE_STATUS 0x31
146 #define CCW_CMD_READ_VQ_CONF 0x32
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_INTPARM_MASK 0xffff0000
164 static struct virtio_ccw_device
*to_vc_device(struct virtio_device
*vdev
)
166 return container_of(vdev
, struct virtio_ccw_device
, vdev
);
169 static void drop_airq_indicator(struct virtqueue
*vq
, struct airq_info
*info
)
171 unsigned long i
, flags
;
173 write_lock_irqsave(&info
->lock
, flags
);
174 for (i
= 0; i
< airq_iv_end(info
->aiv
); i
++) {
175 if (vq
== (void *)airq_iv_get_ptr(info
->aiv
, i
)) {
176 airq_iv_free_bit(info
->aiv
, i
);
177 airq_iv_set_ptr(info
->aiv
, i
, 0);
181 write_unlock_irqrestore(&info
->lock
, flags
);
184 static void virtio_airq_handler(struct airq_struct
*airq
)
186 struct airq_info
*info
= container_of(airq
, struct airq_info
, airq
);
189 inc_irq_stat(IRQIO_VAI
);
190 read_lock(&info
->lock
);
191 /* Walk through indicators field, summary indicator active. */
193 ai
= airq_iv_scan(info
->aiv
, ai
, airq_iv_end(info
->aiv
));
196 vring_interrupt(0, (void *)airq_iv_get_ptr(info
->aiv
, ai
));
198 info
->summary_indicator
= 0;
200 /* Walk through indicators field, summary indicator not active. */
202 ai
= airq_iv_scan(info
->aiv
, ai
, airq_iv_end(info
->aiv
));
205 vring_interrupt(0, (void *)airq_iv_get_ptr(info
->aiv
, ai
));
207 read_unlock(&info
->lock
);
210 static struct airq_info
*new_airq_info(void)
212 struct airq_info
*info
;
215 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
218 rwlock_init(&info
->lock
);
219 info
->aiv
= airq_iv_create(VIRTIO_IV_BITS
, AIRQ_IV_ALLOC
| AIRQ_IV_PTR
);
224 info
->airq
.handler
= virtio_airq_handler
;
225 info
->airq
.lsi_ptr
= &info
->summary_indicator
;
226 info
->airq
.lsi_mask
= 0xff;
227 info
->airq
.isc
= VIRTIO_AIRQ_ISC
;
228 rc
= register_adapter_interrupt(&info
->airq
);
230 airq_iv_release(info
->aiv
);
237 static void destroy_airq_info(struct airq_info
*info
)
242 unregister_adapter_interrupt(&info
->airq
);
243 airq_iv_release(info
->aiv
);
247 static unsigned long get_airq_indicator(struct virtqueue
*vqs
[], int nvqs
,
248 u64
*first
, void **airq_info
)
251 struct airq_info
*info
;
252 unsigned long indicator_addr
= 0;
253 unsigned long bit
, flags
;
255 for (i
= 0; i
< MAX_AIRQ_AREAS
&& !indicator_addr
; i
++) {
257 airq_areas
[i
] = new_airq_info();
258 info
= airq_areas
[i
];
261 write_lock_irqsave(&info
->lock
, flags
);
262 bit
= airq_iv_alloc(info
->aiv
, nvqs
);
264 /* Not enough vacancies. */
265 write_unlock_irqrestore(&info
->lock
, flags
);
270 indicator_addr
= (unsigned long)info
->aiv
->vector
;
271 for (j
= 0; j
< nvqs
; j
++) {
272 airq_iv_set_ptr(info
->aiv
, bit
+ j
,
273 (unsigned long)vqs
[j
]);
275 write_unlock_irqrestore(&info
->lock
, flags
);
277 return indicator_addr
;
280 static void virtio_ccw_drop_indicators(struct virtio_ccw_device
*vcdev
)
282 struct virtio_ccw_vq_info
*info
;
284 list_for_each_entry(info
, &vcdev
->virtqueues
, node
)
285 drop_airq_indicator(info
->vq
, vcdev
->airq_info
);
288 static int doing_io(struct virtio_ccw_device
*vcdev
, __u32 flag
)
293 spin_lock_irqsave(get_ccwdev_lock(vcdev
->cdev
), flags
);
297 ret
= vcdev
->curr_io
& flag
;
298 spin_unlock_irqrestore(get_ccwdev_lock(vcdev
->cdev
), flags
);
302 static int ccw_io_helper(struct virtio_ccw_device
*vcdev
,
303 struct ccw1
*ccw
, __u32 intparm
)
307 int flag
= intparm
& VIRTIO_CCW_INTPARM_MASK
;
310 spin_lock_irqsave(get_ccwdev_lock(vcdev
->cdev
), flags
);
311 ret
= ccw_device_start(vcdev
->cdev
, ccw
, intparm
, 0, 0);
315 vcdev
->curr_io
|= flag
;
317 spin_unlock_irqrestore(get_ccwdev_lock(vcdev
->cdev
), flags
);
319 } while (ret
== -EBUSY
);
320 wait_event(vcdev
->wait_q
, doing_io(vcdev
, flag
) == 0);
321 return ret
? ret
: vcdev
->err
;
324 static void virtio_ccw_drop_indicator(struct virtio_ccw_device
*vcdev
,
328 unsigned long *indicatorp
= NULL
;
329 struct virtio_thinint_area
*thinint_area
= NULL
;
330 struct airq_info
*airq_info
= vcdev
->airq_info
;
332 if (vcdev
->is_thinint
) {
333 thinint_area
= kzalloc(sizeof(*thinint_area
),
334 GFP_DMA
| GFP_KERNEL
);
337 thinint_area
->summary_indicator
=
338 (unsigned long) &airq_info
->summary_indicator
;
339 thinint_area
->isc
= VIRTIO_AIRQ_ISC
;
340 ccw
->cmd_code
= CCW_CMD_SET_IND_ADAPTER
;
341 ccw
->count
= sizeof(*thinint_area
);
342 ccw
->cda
= (__u32
)(unsigned long) thinint_area
;
344 indicatorp
= kmalloc(sizeof(&vcdev
->indicators
),
345 GFP_DMA
| GFP_KERNEL
);
349 ccw
->cmd_code
= CCW_CMD_SET_IND
;
350 ccw
->count
= sizeof(vcdev
->indicators
);
351 ccw
->cda
= (__u32
)(unsigned long) indicatorp
;
353 /* Deregister indicators from host. */
354 vcdev
->indicators
= 0;
356 ret
= ccw_io_helper(vcdev
, ccw
,
358 VIRTIO_CCW_DOING_SET_IND_ADAPTER
:
359 VIRTIO_CCW_DOING_SET_IND
);
360 if (ret
&& (ret
!= -ENODEV
))
361 dev_info(&vcdev
->cdev
->dev
,
362 "Failed to deregister indicators (%d)\n", ret
);
363 else if (vcdev
->is_thinint
)
364 virtio_ccw_drop_indicators(vcdev
);
369 static inline long do_kvm_notify(struct subchannel_id schid
,
370 unsigned long queue_index
,
373 register unsigned long __nr
asm("1") = KVM_S390_VIRTIO_CCW_NOTIFY
;
374 register struct subchannel_id __schid
asm("2") = schid
;
375 register unsigned long __index
asm("3") = queue_index
;
376 register long __rc
asm("2");
377 register long __cookie
asm("4") = cookie
;
379 asm volatile ("diag 2,4,0x500\n"
380 : "=d" (__rc
) : "d" (__nr
), "d" (__schid
), "d" (__index
),
386 static bool virtio_ccw_kvm_notify(struct virtqueue
*vq
)
388 struct virtio_ccw_vq_info
*info
= vq
->priv
;
389 struct virtio_ccw_device
*vcdev
;
390 struct subchannel_id schid
;
392 vcdev
= to_vc_device(info
->vq
->vdev
);
393 ccw_device_get_schid(vcdev
->cdev
, &schid
);
394 info
->cookie
= do_kvm_notify(schid
, vq
->index
, info
->cookie
);
395 if (info
->cookie
< 0)
400 static int virtio_ccw_read_vq_conf(struct virtio_ccw_device
*vcdev
,
401 struct ccw1
*ccw
, int index
)
403 vcdev
->config_block
->index
= index
;
404 ccw
->cmd_code
= CCW_CMD_READ_VQ_CONF
;
406 ccw
->count
= sizeof(struct vq_config_block
);
407 ccw
->cda
= (__u32
)(unsigned long)(vcdev
->config_block
);
408 ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_READ_VQ_CONF
);
409 return vcdev
->config_block
->num
;
412 static void virtio_ccw_del_vq(struct virtqueue
*vq
, struct ccw1
*ccw
)
414 struct virtio_ccw_device
*vcdev
= to_vc_device(vq
->vdev
);
415 struct virtio_ccw_vq_info
*info
= vq
->priv
;
419 unsigned int index
= vq
->index
;
421 /* Remove from our list. */
422 spin_lock_irqsave(&vcdev
->lock
, flags
);
423 list_del(&info
->node
);
424 spin_unlock_irqrestore(&vcdev
->lock
, flags
);
426 /* Release from host. */
427 if (vcdev
->revision
== 0) {
428 info
->info_block
->l
.queue
= 0;
429 info
->info_block
->l
.align
= 0;
430 info
->info_block
->l
.index
= index
;
431 info
->info_block
->l
.num
= 0;
432 ccw
->count
= sizeof(info
->info_block
->l
);
434 info
->info_block
->s
.desc
= 0;
435 info
->info_block
->s
.index
= index
;
436 info
->info_block
->s
.num
= 0;
437 info
->info_block
->s
.avail
= 0;
438 info
->info_block
->s
.used
= 0;
439 ccw
->count
= sizeof(info
->info_block
->s
);
441 ccw
->cmd_code
= CCW_CMD_SET_VQ
;
443 ccw
->cda
= (__u32
)(unsigned long)(info
->info_block
);
444 ret
= ccw_io_helper(vcdev
, ccw
,
445 VIRTIO_CCW_DOING_SET_VQ
| index
);
447 * -ENODEV isn't considered an error: The device is gone anyway.
448 * This may happen on device detach.
450 if (ret
&& (ret
!= -ENODEV
))
451 dev_warn(&vq
->vdev
->dev
, "Error %d while deleting queue %d",
454 vring_del_virtqueue(vq
);
455 size
= PAGE_ALIGN(vring_size(info
->num
, KVM_VIRTIO_CCW_RING_ALIGN
));
456 free_pages_exact(info
->queue
, size
);
457 kfree(info
->info_block
);
461 static void virtio_ccw_del_vqs(struct virtio_device
*vdev
)
463 struct virtqueue
*vq
, *n
;
465 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
467 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
471 virtio_ccw_drop_indicator(vcdev
, ccw
);
473 list_for_each_entry_safe(vq
, n
, &vdev
->vqs
, list
)
474 virtio_ccw_del_vq(vq
, ccw
);
479 static struct virtqueue
*virtio_ccw_setup_vq(struct virtio_device
*vdev
,
480 int i
, vq_callback_t
*callback
,
484 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
486 struct virtqueue
*vq
= NULL
;
487 struct virtio_ccw_vq_info
*info
;
488 unsigned long size
= 0; /* silence the compiler */
491 /* Allocate queue. */
492 info
= kzalloc(sizeof(struct virtio_ccw_vq_info
), GFP_KERNEL
);
494 dev_warn(&vcdev
->cdev
->dev
, "no info\n");
498 info
->info_block
= kzalloc(sizeof(*info
->info_block
),
499 GFP_DMA
| GFP_KERNEL
);
500 if (!info
->info_block
) {
501 dev_warn(&vcdev
->cdev
->dev
, "no info block\n");
505 info
->num
= virtio_ccw_read_vq_conf(vcdev
, ccw
, i
);
506 size
= PAGE_ALIGN(vring_size(info
->num
, KVM_VIRTIO_CCW_RING_ALIGN
));
507 info
->queue
= alloc_pages_exact(size
, GFP_KERNEL
| __GFP_ZERO
);
508 if (info
->queue
== NULL
) {
509 dev_warn(&vcdev
->cdev
->dev
, "no queue\n");
514 vq
= vring_new_virtqueue(i
, info
->num
, KVM_VIRTIO_CCW_RING_ALIGN
, vdev
,
515 true, info
->queue
, virtio_ccw_kvm_notify
,
518 /* For now, we fail if we can't get the requested size. */
519 dev_warn(&vcdev
->cdev
->dev
, "no vq\n");
524 /* Register it with the host. */
525 if (vcdev
->revision
== 0) {
526 info
->info_block
->l
.queue
= (__u64
)info
->queue
;
527 info
->info_block
->l
.align
= KVM_VIRTIO_CCW_RING_ALIGN
;
528 info
->info_block
->l
.index
= i
;
529 info
->info_block
->l
.num
= info
->num
;
530 ccw
->count
= sizeof(info
->info_block
->l
);
532 info
->info_block
->s
.desc
= (__u64
)info
->queue
;
533 info
->info_block
->s
.index
= i
;
534 info
->info_block
->s
.num
= info
->num
;
535 info
->info_block
->s
.avail
= (__u64
)virtqueue_get_avail(vq
);
536 info
->info_block
->s
.used
= (__u64
)virtqueue_get_used(vq
);
537 ccw
->count
= sizeof(info
->info_block
->s
);
539 ccw
->cmd_code
= CCW_CMD_SET_VQ
;
541 ccw
->cda
= (__u32
)(unsigned long)(info
->info_block
);
542 err
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_SET_VQ
| i
);
544 dev_warn(&vcdev
->cdev
->dev
, "SET_VQ failed\n");
551 /* Save it to our list. */
552 spin_lock_irqsave(&vcdev
->lock
, flags
);
553 list_add(&info
->node
, &vcdev
->virtqueues
);
554 spin_unlock_irqrestore(&vcdev
->lock
, flags
);
560 vring_del_virtqueue(vq
);
563 free_pages_exact(info
->queue
, size
);
564 kfree(info
->info_block
);
570 static int virtio_ccw_register_adapter_ind(struct virtio_ccw_device
*vcdev
,
571 struct virtqueue
*vqs
[], int nvqs
,
575 struct virtio_thinint_area
*thinint_area
= NULL
;
576 struct airq_info
*info
;
578 thinint_area
= kzalloc(sizeof(*thinint_area
), GFP_DMA
| GFP_KERNEL
);
583 /* Try to get an indicator. */
584 thinint_area
->indicator
= get_airq_indicator(vqs
, nvqs
,
585 &thinint_area
->bit_nr
,
587 if (!thinint_area
->indicator
) {
591 info
= vcdev
->airq_info
;
592 thinint_area
->summary_indicator
=
593 (unsigned long) &info
->summary_indicator
;
594 thinint_area
->isc
= VIRTIO_AIRQ_ISC
;
595 ccw
->cmd_code
= CCW_CMD_SET_IND_ADAPTER
;
596 ccw
->flags
= CCW_FLAG_SLI
;
597 ccw
->count
= sizeof(*thinint_area
);
598 ccw
->cda
= (__u32
)(unsigned long)thinint_area
;
599 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_SET_IND_ADAPTER
);
601 if (ret
== -EOPNOTSUPP
) {
603 * The host does not support adapter interrupts
604 * for virtio-ccw, stop trying.
606 virtio_ccw_use_airq
= 0;
607 pr_info("Adapter interrupts unsupported on host\n");
609 dev_warn(&vcdev
->cdev
->dev
,
610 "enabling adapter interrupts = %d\n", ret
);
611 virtio_ccw_drop_indicators(vcdev
);
618 static int virtio_ccw_find_vqs(struct virtio_device
*vdev
, unsigned nvqs
,
619 struct virtqueue
*vqs
[],
620 vq_callback_t
*callbacks
[],
623 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
624 unsigned long *indicatorp
= NULL
;
628 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
632 for (i
= 0; i
< nvqs
; ++i
) {
633 vqs
[i
] = virtio_ccw_setup_vq(vdev
, i
, callbacks
[i
], names
[i
],
635 if (IS_ERR(vqs
[i
])) {
636 ret
= PTR_ERR(vqs
[i
]);
642 /* We need a data area under 2G to communicate. */
643 indicatorp
= kmalloc(sizeof(&vcdev
->indicators
), GFP_DMA
| GFP_KERNEL
);
646 *indicatorp
= (unsigned long) &vcdev
->indicators
;
647 if (vcdev
->is_thinint
) {
648 ret
= virtio_ccw_register_adapter_ind(vcdev
, vqs
, nvqs
, ccw
);
650 /* no error, just fall back to legacy interrupts */
651 vcdev
->is_thinint
= 0;
653 if (!vcdev
->is_thinint
) {
654 /* Register queue indicators with host. */
655 vcdev
->indicators
= 0;
656 ccw
->cmd_code
= CCW_CMD_SET_IND
;
658 ccw
->count
= sizeof(vcdev
->indicators
);
659 ccw
->cda
= (__u32
)(unsigned long) indicatorp
;
660 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_SET_IND
);
664 /* Register indicators2 with host for config changes */
665 *indicatorp
= (unsigned long) &vcdev
->indicators2
;
666 vcdev
->indicators2
= 0;
667 ccw
->cmd_code
= CCW_CMD_SET_CONF_IND
;
669 ccw
->count
= sizeof(vcdev
->indicators2
);
670 ccw
->cda
= (__u32
)(unsigned long) indicatorp
;
671 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_SET_CONF_IND
);
681 virtio_ccw_del_vqs(vdev
);
685 static void virtio_ccw_reset(struct virtio_device
*vdev
)
687 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
690 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
694 /* Zero status bits. */
697 /* Send a reset ccw on device. */
698 ccw
->cmd_code
= CCW_CMD_VDEV_RESET
;
702 ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_RESET
);
706 static u64
virtio_ccw_get_features(struct virtio_device
*vdev
)
708 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
709 struct virtio_feature_desc
*features
;
714 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
718 features
= kzalloc(sizeof(*features
), GFP_DMA
| GFP_KERNEL
);
723 /* Read the feature bits from the host. */
725 ccw
->cmd_code
= CCW_CMD_READ_FEAT
;
727 ccw
->count
= sizeof(*features
);
728 ccw
->cda
= (__u32
)(unsigned long)features
;
729 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_READ_FEAT
);
735 rc
= le32_to_cpu(features
->features
);
737 if (vcdev
->revision
== 0)
740 /* Read second half of 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
);
748 rc
|= (u64
)le32_to_cpu(features
->features
) << 32;
756 static int virtio_ccw_finalize_features(struct virtio_device
*vdev
)
758 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
759 struct virtio_feature_desc
*features
;
763 if (vcdev
->revision
>= 1 &&
764 !__virtio_test_bit(vdev
, VIRTIO_F_VERSION_1
)) {
765 dev_err(&vdev
->dev
, "virtio: device uses revision 1 "
766 "but does not have VIRTIO_F_VERSION_1\n");
770 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
774 features
= kzalloc(sizeof(*features
), GFP_DMA
| GFP_KERNEL
);
779 /* Give virtio_ring a chance to accept features. */
780 vring_transport_features(vdev
);
783 features
->features
= cpu_to_le32((u32
)vdev
->features
);
784 /* Write the first half of the feature bits to the host. */
785 ccw
->cmd_code
= CCW_CMD_WRITE_FEAT
;
787 ccw
->count
= sizeof(*features
);
788 ccw
->cda
= (__u32
)(unsigned long)features
;
789 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_WRITE_FEAT
);
793 if (vcdev
->revision
== 0)
797 features
->features
= cpu_to_le32(vdev
->features
>> 32);
798 /* Write the second half of the feature bits to the host. */
799 ccw
->cmd_code
= CCW_CMD_WRITE_FEAT
;
801 ccw
->count
= sizeof(*features
);
802 ccw
->cda
= (__u32
)(unsigned long)features
;
803 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_WRITE_FEAT
);
812 static void virtio_ccw_get_config(struct virtio_device
*vdev
,
813 unsigned int offset
, void *buf
, unsigned len
)
815 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
820 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
824 config_area
= kzalloc(VIRTIO_CCW_CONFIG_SIZE
, GFP_DMA
| GFP_KERNEL
);
828 /* Read the config area from the host. */
829 ccw
->cmd_code
= CCW_CMD_READ_CONF
;
831 ccw
->count
= offset
+ len
;
832 ccw
->cda
= (__u32
)(unsigned long)config_area
;
833 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_READ_CONFIG
);
837 memcpy(vcdev
->config
, config_area
, offset
+ len
);
839 memcpy(buf
, &vcdev
->config
[offset
], len
);
840 if (vcdev
->config_ready
< offset
+ len
)
841 vcdev
->config_ready
= offset
+ len
;
848 static void virtio_ccw_set_config(struct virtio_device
*vdev
,
849 unsigned int offset
, const void *buf
,
852 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
856 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
860 config_area
= kzalloc(VIRTIO_CCW_CONFIG_SIZE
, GFP_DMA
| GFP_KERNEL
);
864 /* Make sure we don't overwrite fields. */
865 if (vcdev
->config_ready
< offset
)
866 virtio_ccw_get_config(vdev
, 0, NULL
, offset
);
867 memcpy(&vcdev
->config
[offset
], buf
, len
);
868 /* Write the config area to the host. */
869 memcpy(config_area
, vcdev
->config
, sizeof(vcdev
->config
));
870 ccw
->cmd_code
= CCW_CMD_WRITE_CONF
;
872 ccw
->count
= offset
+ len
;
873 ccw
->cda
= (__u32
)(unsigned long)config_area
;
874 ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_WRITE_CONFIG
);
881 static u8
virtio_ccw_get_status(struct virtio_device
*vdev
)
883 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
885 return *vcdev
->status
;
888 static void virtio_ccw_set_status(struct virtio_device
*vdev
, u8 status
)
890 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
891 u8 old_status
= *vcdev
->status
;
895 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
899 /* Write the status to the host. */
900 *vcdev
->status
= status
;
901 ccw
->cmd_code
= CCW_CMD_WRITE_STATUS
;
903 ccw
->count
= sizeof(status
);
904 ccw
->cda
= (__u32
)(unsigned long)vcdev
->status
;
905 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_WRITE_STATUS
);
906 /* Write failed? We assume status is unchanged. */
908 *vcdev
->status
= old_status
;
912 static struct virtio_config_ops virtio_ccw_config_ops
= {
913 .get_features
= virtio_ccw_get_features
,
914 .finalize_features
= virtio_ccw_finalize_features
,
915 .get
= virtio_ccw_get_config
,
916 .set
= virtio_ccw_set_config
,
917 .get_status
= virtio_ccw_get_status
,
918 .set_status
= virtio_ccw_set_status
,
919 .reset
= virtio_ccw_reset
,
920 .find_vqs
= virtio_ccw_find_vqs
,
921 .del_vqs
= virtio_ccw_del_vqs
,
926 * ccw bus driver related functions
929 static void virtio_ccw_release_dev(struct device
*_d
)
931 struct virtio_device
*dev
= container_of(_d
, struct virtio_device
,
933 struct virtio_ccw_device
*vcdev
= to_vc_device(dev
);
935 kfree(vcdev
->status
);
936 kfree(vcdev
->config_block
);
940 static int irb_is_error(struct irb
*irb
)
942 if (scsw_cstat(&irb
->scsw
) != 0)
944 if (scsw_dstat(&irb
->scsw
) & ~(DEV_STAT_CHN_END
| DEV_STAT_DEV_END
))
946 if (scsw_cc(&irb
->scsw
) != 0)
951 static struct virtqueue
*virtio_ccw_vq_by_ind(struct virtio_ccw_device
*vcdev
,
954 struct virtio_ccw_vq_info
*info
;
956 struct virtqueue
*vq
;
959 spin_lock_irqsave(&vcdev
->lock
, flags
);
960 list_for_each_entry(info
, &vcdev
->virtqueues
, node
) {
961 if (info
->vq
->index
== index
) {
966 spin_unlock_irqrestore(&vcdev
->lock
, flags
);
970 static void virtio_ccw_int_handler(struct ccw_device
*cdev
,
971 unsigned long intparm
,
974 __u32 activity
= intparm
& VIRTIO_CCW_INTPARM_MASK
;
975 struct virtio_ccw_device
*vcdev
= dev_get_drvdata(&cdev
->dev
);
977 struct virtqueue
*vq
;
981 /* Check if it's a notification from the host. */
982 if ((intparm
== 0) &&
983 (scsw_stctl(&irb
->scsw
) ==
984 (SCSW_STCTL_ALERT_STATUS
| SCSW_STCTL_STATUS_PEND
))) {
987 if (irb_is_error(irb
)) {
988 /* Command reject? */
989 if ((scsw_dstat(&irb
->scsw
) & DEV_STAT_UNIT_CHECK
) &&
990 (irb
->ecw
[0] & SNS0_CMD_REJECT
))
991 vcdev
->err
= -EOPNOTSUPP
;
993 /* Map everything else to -EIO. */
996 if (vcdev
->curr_io
& activity
) {
998 case VIRTIO_CCW_DOING_READ_FEAT
:
999 case VIRTIO_CCW_DOING_WRITE_FEAT
:
1000 case VIRTIO_CCW_DOING_READ_CONFIG
:
1001 case VIRTIO_CCW_DOING_WRITE_CONFIG
:
1002 case VIRTIO_CCW_DOING_WRITE_STATUS
:
1003 case VIRTIO_CCW_DOING_SET_VQ
:
1004 case VIRTIO_CCW_DOING_SET_IND
:
1005 case VIRTIO_CCW_DOING_SET_CONF_IND
:
1006 case VIRTIO_CCW_DOING_RESET
:
1007 case VIRTIO_CCW_DOING_READ_VQ_CONF
:
1008 case VIRTIO_CCW_DOING_SET_IND_ADAPTER
:
1009 case VIRTIO_CCW_DOING_SET_VIRTIO_REV
:
1010 vcdev
->curr_io
&= ~activity
;
1011 wake_up(&vcdev
->wait_q
);
1014 /* don't know what to do... */
1015 dev_warn(&cdev
->dev
, "Suspicious activity '%08x'\n",
1021 for_each_set_bit(i
, &vcdev
->indicators
,
1022 sizeof(vcdev
->indicators
) * BITS_PER_BYTE
) {
1023 /* The bit clear must happen before the vring kick. */
1024 clear_bit(i
, &vcdev
->indicators
);
1026 vq
= virtio_ccw_vq_by_ind(vcdev
, i
);
1027 vring_interrupt(0, vq
);
1029 if (test_bit(0, &vcdev
->indicators2
)) {
1030 virtio_config_changed(&vcdev
->vdev
);
1031 clear_bit(0, &vcdev
->indicators2
);
1036 * We usually want to autoonline all devices, but give the admin
1037 * a way to exempt devices from this.
1039 #define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
1041 static unsigned long devs_no_auto
[__MAX_SSID
+ 1][__DEV_WORDS
];
1043 static char *no_auto
= "";
1045 module_param(no_auto
, charp
, 0444);
1046 MODULE_PARM_DESC(no_auto
, "list of ccw bus id ranges not to be auto-onlined");
1048 static int virtio_ccw_check_autoonline(struct ccw_device
*cdev
)
1050 struct ccw_dev_id id
;
1052 ccw_device_get_id(cdev
, &id
);
1053 if (test_bit(id
.devno
, devs_no_auto
[id
.ssid
]))
1058 static void virtio_ccw_auto_online(void *data
, async_cookie_t cookie
)
1060 struct ccw_device
*cdev
= data
;
1063 ret
= ccw_device_set_online(cdev
);
1065 dev_warn(&cdev
->dev
, "Failed to set online: %d\n", ret
);
1068 static int virtio_ccw_probe(struct ccw_device
*cdev
)
1070 cdev
->handler
= virtio_ccw_int_handler
;
1072 if (virtio_ccw_check_autoonline(cdev
))
1073 async_schedule(virtio_ccw_auto_online
, cdev
);
1077 static struct virtio_ccw_device
*virtio_grab_drvdata(struct ccw_device
*cdev
)
1079 unsigned long flags
;
1080 struct virtio_ccw_device
*vcdev
;
1082 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1083 vcdev
= dev_get_drvdata(&cdev
->dev
);
1084 if (!vcdev
|| vcdev
->going_away
) {
1085 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1088 vcdev
->going_away
= true;
1089 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1093 static void virtio_ccw_remove(struct ccw_device
*cdev
)
1095 unsigned long flags
;
1096 struct virtio_ccw_device
*vcdev
= virtio_grab_drvdata(cdev
);
1098 if (vcdev
&& cdev
->online
) {
1099 if (vcdev
->device_lost
)
1100 virtio_break_device(&vcdev
->vdev
);
1101 unregister_virtio_device(&vcdev
->vdev
);
1102 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1103 dev_set_drvdata(&cdev
->dev
, NULL
);
1104 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1106 cdev
->handler
= NULL
;
1109 static int virtio_ccw_offline(struct ccw_device
*cdev
)
1111 unsigned long flags
;
1112 struct virtio_ccw_device
*vcdev
= virtio_grab_drvdata(cdev
);
1116 if (vcdev
->device_lost
)
1117 virtio_break_device(&vcdev
->vdev
);
1118 unregister_virtio_device(&vcdev
->vdev
);
1119 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1120 dev_set_drvdata(&cdev
->dev
, NULL
);
1121 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1125 static int virtio_ccw_set_transport_rev(struct virtio_ccw_device
*vcdev
)
1127 struct virtio_rev_info
*rev
;
1131 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
1134 rev
= kzalloc(sizeof(*rev
), GFP_DMA
| GFP_KERNEL
);
1140 /* Set transport revision */
1141 ccw
->cmd_code
= CCW_CMD_SET_VIRTIO_REV
;
1143 ccw
->count
= sizeof(*rev
);
1144 ccw
->cda
= (__u32
)(unsigned long)rev
;
1146 vcdev
->revision
= VIRTIO_CCW_REV_MAX
;
1148 rev
->revision
= vcdev
->revision
;
1149 /* none of our supported revisions carry payload */
1151 ret
= ccw_io_helper(vcdev
, ccw
,
1152 VIRTIO_CCW_DOING_SET_VIRTIO_REV
);
1153 if (ret
== -EOPNOTSUPP
) {
1154 if (vcdev
->revision
== 0)
1156 * The host device does not support setting
1157 * the revision: let's operate it in legacy
1164 } while (ret
== -EOPNOTSUPP
);
1171 static int virtio_ccw_online(struct ccw_device
*cdev
)
1174 struct virtio_ccw_device
*vcdev
;
1175 unsigned long flags
;
1177 vcdev
= kzalloc(sizeof(*vcdev
), GFP_KERNEL
);
1179 dev_warn(&cdev
->dev
, "Could not get memory for virtio\n");
1183 vcdev
->config_block
= kzalloc(sizeof(*vcdev
->config_block
),
1184 GFP_DMA
| GFP_KERNEL
);
1185 if (!vcdev
->config_block
) {
1189 vcdev
->status
= kzalloc(sizeof(*vcdev
->status
), GFP_DMA
| GFP_KERNEL
);
1190 if (!vcdev
->status
) {
1195 vcdev
->is_thinint
= virtio_ccw_use_airq
; /* at least try */
1197 vcdev
->vdev
.dev
.parent
= &cdev
->dev
;
1198 vcdev
->vdev
.dev
.release
= virtio_ccw_release_dev
;
1199 vcdev
->vdev
.config
= &virtio_ccw_config_ops
;
1201 init_waitqueue_head(&vcdev
->wait_q
);
1202 INIT_LIST_HEAD(&vcdev
->virtqueues
);
1203 spin_lock_init(&vcdev
->lock
);
1205 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1206 dev_set_drvdata(&cdev
->dev
, vcdev
);
1207 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1208 vcdev
->vdev
.id
.vendor
= cdev
->id
.cu_type
;
1209 vcdev
->vdev
.id
.device
= cdev
->id
.cu_model
;
1211 ret
= virtio_ccw_set_transport_rev(vcdev
);
1215 ret
= register_virtio_device(&vcdev
->vdev
);
1217 dev_warn(&cdev
->dev
, "Failed to register virtio device: %d\n",
1223 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1224 dev_set_drvdata(&cdev
->dev
, NULL
);
1225 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1226 put_device(&vcdev
->vdev
.dev
);
1230 kfree(vcdev
->status
);
1231 kfree(vcdev
->config_block
);
1237 static int virtio_ccw_cio_notify(struct ccw_device
*cdev
, int event
)
1240 struct virtio_ccw_device
*vcdev
= dev_get_drvdata(&cdev
->dev
);
1243 * Make sure vcdev is set
1244 * i.e. set_offline/remove callback not already running
1251 vcdev
->device_lost
= true;
1261 static struct ccw_device_id virtio_ids
[] = {
1262 { CCW_DEVICE(0x3832, 0) },
1265 MODULE_DEVICE_TABLE(ccw
, virtio_ids
);
1267 static struct ccw_driver virtio_ccw_driver
= {
1269 .owner
= THIS_MODULE
,
1270 .name
= "virtio_ccw",
1273 .probe
= virtio_ccw_probe
,
1274 .remove
= virtio_ccw_remove
,
1275 .set_offline
= virtio_ccw_offline
,
1276 .set_online
= virtio_ccw_online
,
1277 .notify
= virtio_ccw_cio_notify
,
1278 .int_class
= IRQIO_VIR
,
1281 static int __init
pure_hex(char **cp
, unsigned int *val
, int min_digit
,
1282 int max_digit
, int max_val
)
1289 while (diff
<= max_digit
) {
1290 int value
= hex_to_bin(**cp
);
1294 *val
= *val
* 16 + value
;
1299 if ((diff
< min_digit
) || (diff
> max_digit
) || (*val
> max_val
))
1305 static int __init
parse_busid(char *str
, unsigned int *cssid
,
1306 unsigned int *ssid
, unsigned int *devno
)
1317 ret
= pure_hex(&str_work
, cssid
, 1, 2, __MAX_CSSID
);
1318 if (ret
|| (str_work
[0] != '.'))
1321 ret
= pure_hex(&str_work
, ssid
, 1, 1, __MAX_SSID
);
1322 if (ret
|| (str_work
[0] != '.'))
1325 ret
= pure_hex(&str_work
, devno
, 4, 4, __MAX_SUBCHANNEL
);
1326 if (ret
|| (str_work
[0] != '\0'))
1334 static void __init
no_auto_parse(void)
1336 unsigned int from_cssid
, to_cssid
, from_ssid
, to_ssid
, from
, to
;
1341 while ((parm
= strsep(&str
, ","))) {
1342 rc
= parse_busid(strsep(&parm
, "-"), &from_cssid
,
1347 rc
= parse_busid(parm
, &to_cssid
,
1349 if ((from_ssid
> to_ssid
) ||
1350 ((from_ssid
== to_ssid
) && (from
> to
)))
1353 to_cssid
= from_cssid
;
1354 to_ssid
= from_ssid
;
1359 while ((from_ssid
< to_ssid
) ||
1360 ((from_ssid
== to_ssid
) && (from
<= to
))) {
1361 set_bit(from
, devs_no_auto
[from_ssid
]);
1363 if (from
> __MAX_SUBCHANNEL
) {
1371 static int __init
virtio_ccw_init(void)
1373 /* parse no_auto string before we do anything further */
1375 return ccw_driver_register(&virtio_ccw_driver
);
1377 module_init(virtio_ccw_init
);
1379 static void __exit
virtio_ccw_exit(void)
1383 ccw_driver_unregister(&virtio_ccw_driver
);
1384 for (i
= 0; i
< MAX_AIRQ_AREAS
; i
++)
1385 destroy_airq_info(airq_areas
[i
]);
1387 module_exit(virtio_ccw_exit
);