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/moduleparam.h>
29 #include <linux/kvm_para.h>
30 #include <linux/notifier.h>
32 #include <asm/setup.h>
35 #include <asm/ccwdev.h>
36 #include <asm/virtio-ccw.h>
41 * virtio related functions
44 struct vq_config_block
{
49 #define VIRTIO_CCW_CONFIG_SIZE 0x100
50 /* same as PCI config space size, should be enough for all drivers */
52 struct virtio_ccw_device
{
53 struct virtio_device vdev
;
55 __u8 config
[VIRTIO_CCW_CONFIG_SIZE
];
56 struct ccw_device
*cdev
;
59 unsigned int revision
; /* Transport revision */
60 wait_queue_head_t wait_q
;
62 struct list_head virtqueues
;
63 unsigned long indicators
;
64 unsigned long indicators2
;
65 struct vq_config_block
*config_block
;
69 unsigned int config_ready
;
73 struct vq_info_block_legacy
{
80 struct vq_info_block
{
89 struct virtio_feature_desc
{
94 struct virtio_thinint_area
{
95 unsigned long summary_indicator
;
96 unsigned long indicator
;
101 struct virtio_rev_info
{
107 /* the highest virtio-ccw revision we support */
108 #define VIRTIO_CCW_REV_MAX 1
110 struct virtio_ccw_vq_info
{
111 struct virtqueue
*vq
;
115 struct vq_info_block s
;
116 struct vq_info_block_legacy l
;
119 struct list_head node
;
123 #define VIRTIO_AIRQ_ISC IO_SCH_ISC /* inherit from subchannel */
125 #define VIRTIO_IV_BITS (L1_CACHE_BYTES * 8)
126 #define MAX_AIRQ_AREAS 20
128 static int virtio_ccw_use_airq
= 1;
132 u8 summary_indicator
;
133 struct airq_struct airq
;
136 static struct airq_info
*airq_areas
[MAX_AIRQ_AREAS
];
138 #define CCW_CMD_SET_VQ 0x13
139 #define CCW_CMD_VDEV_RESET 0x33
140 #define CCW_CMD_SET_IND 0x43
141 #define CCW_CMD_SET_CONF_IND 0x53
142 #define CCW_CMD_READ_FEAT 0x12
143 #define CCW_CMD_WRITE_FEAT 0x11
144 #define CCW_CMD_READ_CONF 0x22
145 #define CCW_CMD_WRITE_CONF 0x21
146 #define CCW_CMD_WRITE_STATUS 0x31
147 #define CCW_CMD_READ_VQ_CONF 0x32
148 #define CCW_CMD_SET_IND_ADAPTER 0x73
149 #define CCW_CMD_SET_VIRTIO_REV 0x83
151 #define VIRTIO_CCW_DOING_SET_VQ 0x00010000
152 #define VIRTIO_CCW_DOING_RESET 0x00040000
153 #define VIRTIO_CCW_DOING_READ_FEAT 0x00080000
154 #define VIRTIO_CCW_DOING_WRITE_FEAT 0x00100000
155 #define VIRTIO_CCW_DOING_READ_CONFIG 0x00200000
156 #define VIRTIO_CCW_DOING_WRITE_CONFIG 0x00400000
157 #define VIRTIO_CCW_DOING_WRITE_STATUS 0x00800000
158 #define VIRTIO_CCW_DOING_SET_IND 0x01000000
159 #define VIRTIO_CCW_DOING_READ_VQ_CONF 0x02000000
160 #define VIRTIO_CCW_DOING_SET_CONF_IND 0x04000000
161 #define VIRTIO_CCW_DOING_SET_IND_ADAPTER 0x08000000
162 #define VIRTIO_CCW_DOING_SET_VIRTIO_REV 0x10000000
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
)
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 list_for_each_entry(info
, &vcdev
->virtqueues
, node
)
276 drop_airq_indicator(info
->vq
, vcdev
->airq_info
);
279 static int doing_io(struct virtio_ccw_device
*vcdev
, __u32 flag
)
284 spin_lock_irqsave(get_ccwdev_lock(vcdev
->cdev
), flags
);
288 ret
= vcdev
->curr_io
& flag
;
289 spin_unlock_irqrestore(get_ccwdev_lock(vcdev
->cdev
), flags
);
293 static int ccw_io_helper(struct virtio_ccw_device
*vcdev
,
294 struct ccw1
*ccw
, __u32 intparm
)
298 int flag
= intparm
& VIRTIO_CCW_INTPARM_MASK
;
301 spin_lock_irqsave(get_ccwdev_lock(vcdev
->cdev
), flags
);
302 ret
= ccw_device_start(vcdev
->cdev
, ccw
, intparm
, 0, 0);
306 vcdev
->curr_io
|= flag
;
308 spin_unlock_irqrestore(get_ccwdev_lock(vcdev
->cdev
), flags
);
310 } while (ret
== -EBUSY
);
311 wait_event(vcdev
->wait_q
, doing_io(vcdev
, flag
) == 0);
312 return ret
? ret
: vcdev
->err
;
315 static void virtio_ccw_drop_indicator(struct virtio_ccw_device
*vcdev
,
319 unsigned long *indicatorp
= NULL
;
320 struct virtio_thinint_area
*thinint_area
= NULL
;
321 struct airq_info
*airq_info
= vcdev
->airq_info
;
323 if (vcdev
->is_thinint
) {
324 thinint_area
= kzalloc(sizeof(*thinint_area
),
325 GFP_DMA
| GFP_KERNEL
);
328 thinint_area
->summary_indicator
=
329 (unsigned long) &airq_info
->summary_indicator
;
330 thinint_area
->isc
= VIRTIO_AIRQ_ISC
;
331 ccw
->cmd_code
= CCW_CMD_SET_IND_ADAPTER
;
332 ccw
->count
= sizeof(*thinint_area
);
333 ccw
->cda
= (__u32
)(unsigned long) thinint_area
;
335 /* payload is the address of the indicators */
336 indicatorp
= kmalloc(sizeof(&vcdev
->indicators
),
337 GFP_DMA
| GFP_KERNEL
);
341 ccw
->cmd_code
= CCW_CMD_SET_IND
;
342 ccw
->count
= sizeof(&vcdev
->indicators
);
343 ccw
->cda
= (__u32
)(unsigned long) indicatorp
;
345 /* Deregister indicators from host. */
346 vcdev
->indicators
= 0;
348 ret
= ccw_io_helper(vcdev
, ccw
,
350 VIRTIO_CCW_DOING_SET_IND_ADAPTER
:
351 VIRTIO_CCW_DOING_SET_IND
);
352 if (ret
&& (ret
!= -ENODEV
))
353 dev_info(&vcdev
->cdev
->dev
,
354 "Failed to deregister indicators (%d)\n", ret
);
355 else if (vcdev
->is_thinint
)
356 virtio_ccw_drop_indicators(vcdev
);
361 static inline long __do_kvm_notify(struct subchannel_id schid
,
362 unsigned long queue_index
,
365 register unsigned long __nr
asm("1") = KVM_S390_VIRTIO_CCW_NOTIFY
;
366 register struct subchannel_id __schid
asm("2") = schid
;
367 register unsigned long __index
asm("3") = queue_index
;
368 register long __rc
asm("2");
369 register long __cookie
asm("4") = cookie
;
371 asm volatile ("diag 2,4,0x500\n"
372 : "=d" (__rc
) : "d" (__nr
), "d" (__schid
), "d" (__index
),
378 static inline long do_kvm_notify(struct subchannel_id schid
,
379 unsigned long queue_index
,
382 diag_stat_inc(DIAG_STAT_X500
);
383 return __do_kvm_notify(schid
, queue_index
, cookie
);
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
)
405 vcdev
->config_block
->index
= index
;
406 ccw
->cmd_code
= CCW_CMD_READ_VQ_CONF
;
408 ccw
->count
= sizeof(struct vq_config_block
);
409 ccw
->cda
= (__u32
)(unsigned long)(vcdev
->config_block
);
410 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_READ_VQ_CONF
);
413 return vcdev
->config_block
->num
;
416 static void virtio_ccw_del_vq(struct virtqueue
*vq
, struct ccw1
*ccw
)
418 struct virtio_ccw_device
*vcdev
= to_vc_device(vq
->vdev
);
419 struct virtio_ccw_vq_info
*info
= vq
->priv
;
423 unsigned int index
= vq
->index
;
425 /* Remove from our list. */
426 spin_lock_irqsave(&vcdev
->lock
, flags
);
427 list_del(&info
->node
);
428 spin_unlock_irqrestore(&vcdev
->lock
, flags
);
430 /* Release from host. */
431 if (vcdev
->revision
== 0) {
432 info
->info_block
->l
.queue
= 0;
433 info
->info_block
->l
.align
= 0;
434 info
->info_block
->l
.index
= index
;
435 info
->info_block
->l
.num
= 0;
436 ccw
->count
= sizeof(info
->info_block
->l
);
438 info
->info_block
->s
.desc
= 0;
439 info
->info_block
->s
.index
= index
;
440 info
->info_block
->s
.num
= 0;
441 info
->info_block
->s
.avail
= 0;
442 info
->info_block
->s
.used
= 0;
443 ccw
->count
= sizeof(info
->info_block
->s
);
445 ccw
->cmd_code
= CCW_CMD_SET_VQ
;
447 ccw
->cda
= (__u32
)(unsigned long)(info
->info_block
);
448 ret
= ccw_io_helper(vcdev
, ccw
,
449 VIRTIO_CCW_DOING_SET_VQ
| index
);
451 * -ENODEV isn't considered an error: The device is gone anyway.
452 * This may happen on device detach.
454 if (ret
&& (ret
!= -ENODEV
))
455 dev_warn(&vq
->vdev
->dev
, "Error %d while deleting queue %d",
458 vring_del_virtqueue(vq
);
459 size
= PAGE_ALIGN(vring_size(info
->num
, KVM_VIRTIO_CCW_RING_ALIGN
));
460 free_pages_exact(info
->queue
, size
);
461 kfree(info
->info_block
);
465 static void virtio_ccw_del_vqs(struct virtio_device
*vdev
)
467 struct virtqueue
*vq
, *n
;
469 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
471 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
475 virtio_ccw_drop_indicator(vcdev
, ccw
);
477 list_for_each_entry_safe(vq
, n
, &vdev
->vqs
, list
)
478 virtio_ccw_del_vq(vq
, ccw
);
483 static struct virtqueue
*virtio_ccw_setup_vq(struct virtio_device
*vdev
,
484 int i
, vq_callback_t
*callback
,
488 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
490 struct virtqueue
*vq
= NULL
;
491 struct virtio_ccw_vq_info
*info
;
492 unsigned long size
= 0; /* silence the compiler */
495 /* Allocate queue. */
496 info
= kzalloc(sizeof(struct virtio_ccw_vq_info
), GFP_KERNEL
);
498 dev_warn(&vcdev
->cdev
->dev
, "no info\n");
502 info
->info_block
= kzalloc(sizeof(*info
->info_block
),
503 GFP_DMA
| GFP_KERNEL
);
504 if (!info
->info_block
) {
505 dev_warn(&vcdev
->cdev
->dev
, "no info block\n");
509 info
->num
= virtio_ccw_read_vq_conf(vcdev
, ccw
, i
);
514 size
= PAGE_ALIGN(vring_size(info
->num
, KVM_VIRTIO_CCW_RING_ALIGN
));
515 info
->queue
= alloc_pages_exact(size
, GFP_KERNEL
| __GFP_ZERO
);
516 if (info
->queue
== NULL
) {
517 dev_warn(&vcdev
->cdev
->dev
, "no queue\n");
522 vq
= vring_new_virtqueue(i
, info
->num
, KVM_VIRTIO_CCW_RING_ALIGN
, vdev
,
523 true, info
->queue
, virtio_ccw_kvm_notify
,
526 /* For now, we fail if we can't get the requested size. */
527 dev_warn(&vcdev
->cdev
->dev
, "no vq\n");
532 /* Register it with the host. */
533 if (vcdev
->revision
== 0) {
534 info
->info_block
->l
.queue
= (__u64
)info
->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
= (__u64
)info
->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(vq
);
544 info
->info_block
->s
.used
= (__u64
)virtqueue_get_used(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
);
571 free_pages_exact(info
->queue
, size
);
572 kfree(info
->info_block
);
578 static int virtio_ccw_register_adapter_ind(struct virtio_ccw_device
*vcdev
,
579 struct virtqueue
*vqs
[], int nvqs
,
583 struct virtio_thinint_area
*thinint_area
= NULL
;
584 struct airq_info
*info
;
586 thinint_area
= kzalloc(sizeof(*thinint_area
), GFP_DMA
| GFP_KERNEL
);
591 /* Try to get an indicator. */
592 thinint_area
->indicator
= get_airq_indicator(vqs
, nvqs
,
593 &thinint_area
->bit_nr
,
595 if (!thinint_area
->indicator
) {
599 info
= vcdev
->airq_info
;
600 thinint_area
->summary_indicator
=
601 (unsigned long) &info
->summary_indicator
;
602 thinint_area
->isc
= VIRTIO_AIRQ_ISC
;
603 ccw
->cmd_code
= CCW_CMD_SET_IND_ADAPTER
;
604 ccw
->flags
= CCW_FLAG_SLI
;
605 ccw
->count
= sizeof(*thinint_area
);
606 ccw
->cda
= (__u32
)(unsigned long)thinint_area
;
607 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_SET_IND_ADAPTER
);
609 if (ret
== -EOPNOTSUPP
) {
611 * The host does not support adapter interrupts
612 * for virtio-ccw, stop trying.
614 virtio_ccw_use_airq
= 0;
615 pr_info("Adapter interrupts unsupported on host\n");
617 dev_warn(&vcdev
->cdev
->dev
,
618 "enabling adapter interrupts = %d\n", ret
);
619 virtio_ccw_drop_indicators(vcdev
);
626 static int virtio_ccw_find_vqs(struct virtio_device
*vdev
, unsigned nvqs
,
627 struct virtqueue
*vqs
[],
628 vq_callback_t
*callbacks
[],
629 const char * const names
[])
631 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
632 unsigned long *indicatorp
= NULL
;
636 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
640 for (i
= 0; i
< nvqs
; ++i
) {
641 vqs
[i
] = virtio_ccw_setup_vq(vdev
, i
, callbacks
[i
], names
[i
],
643 if (IS_ERR(vqs
[i
])) {
644 ret
= PTR_ERR(vqs
[i
]);
651 * We need a data area under 2G to communicate. Our payload is
652 * the address of the indicators.
654 indicatorp
= kmalloc(sizeof(&vcdev
->indicators
), GFP_DMA
| GFP_KERNEL
);
657 *indicatorp
= (unsigned long) &vcdev
->indicators
;
658 if (vcdev
->is_thinint
) {
659 ret
= virtio_ccw_register_adapter_ind(vcdev
, vqs
, nvqs
, ccw
);
661 /* no error, just fall back to legacy interrupts */
662 vcdev
->is_thinint
= 0;
664 if (!vcdev
->is_thinint
) {
665 /* Register queue indicators with host. */
666 vcdev
->indicators
= 0;
667 ccw
->cmd_code
= CCW_CMD_SET_IND
;
669 ccw
->count
= sizeof(&vcdev
->indicators
);
670 ccw
->cda
= (__u32
)(unsigned long) indicatorp
;
671 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_SET_IND
);
675 /* Register indicators2 with host for config changes */
676 *indicatorp
= (unsigned long) &vcdev
->indicators2
;
677 vcdev
->indicators2
= 0;
678 ccw
->cmd_code
= CCW_CMD_SET_CONF_IND
;
680 ccw
->count
= sizeof(&vcdev
->indicators2
);
681 ccw
->cda
= (__u32
)(unsigned long) indicatorp
;
682 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_SET_CONF_IND
);
692 virtio_ccw_del_vqs(vdev
);
696 static void virtio_ccw_reset(struct virtio_device
*vdev
)
698 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
701 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
705 /* Zero status bits. */
708 /* Send a reset ccw on device. */
709 ccw
->cmd_code
= CCW_CMD_VDEV_RESET
;
713 ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_RESET
);
717 static u64
virtio_ccw_get_features(struct virtio_device
*vdev
)
719 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
720 struct virtio_feature_desc
*features
;
725 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
729 features
= kzalloc(sizeof(*features
), GFP_DMA
| GFP_KERNEL
);
734 /* Read the feature bits from the host. */
736 ccw
->cmd_code
= CCW_CMD_READ_FEAT
;
738 ccw
->count
= sizeof(*features
);
739 ccw
->cda
= (__u32
)(unsigned long)features
;
740 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_READ_FEAT
);
746 rc
= le32_to_cpu(features
->features
);
748 if (vcdev
->revision
== 0)
751 /* Read second half of the feature bits from the host. */
753 ccw
->cmd_code
= CCW_CMD_READ_FEAT
;
755 ccw
->count
= sizeof(*features
);
756 ccw
->cda
= (__u32
)(unsigned long)features
;
757 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_READ_FEAT
);
759 rc
|= (u64
)le32_to_cpu(features
->features
) << 32;
767 static int virtio_ccw_finalize_features(struct virtio_device
*vdev
)
769 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
770 struct virtio_feature_desc
*features
;
774 if (vcdev
->revision
>= 1 &&
775 !__virtio_test_bit(vdev
, VIRTIO_F_VERSION_1
)) {
776 dev_err(&vdev
->dev
, "virtio: device uses revision 1 "
777 "but does not have VIRTIO_F_VERSION_1\n");
781 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
785 features
= kzalloc(sizeof(*features
), GFP_DMA
| GFP_KERNEL
);
790 /* Give virtio_ring a chance to accept features. */
791 vring_transport_features(vdev
);
794 features
->features
= cpu_to_le32((u32
)vdev
->features
);
795 /* Write the first half of the feature bits to the host. */
796 ccw
->cmd_code
= CCW_CMD_WRITE_FEAT
;
798 ccw
->count
= sizeof(*features
);
799 ccw
->cda
= (__u32
)(unsigned long)features
;
800 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_WRITE_FEAT
);
804 if (vcdev
->revision
== 0)
808 features
->features
= cpu_to_le32(vdev
->features
>> 32);
809 /* Write the second half of the feature bits to the host. */
810 ccw
->cmd_code
= CCW_CMD_WRITE_FEAT
;
812 ccw
->count
= sizeof(*features
);
813 ccw
->cda
= (__u32
)(unsigned long)features
;
814 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_WRITE_FEAT
);
823 static void virtio_ccw_get_config(struct virtio_device
*vdev
,
824 unsigned int offset
, void *buf
, unsigned len
)
826 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
831 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
835 config_area
= kzalloc(VIRTIO_CCW_CONFIG_SIZE
, GFP_DMA
| GFP_KERNEL
);
839 /* Read the config area from the host. */
840 ccw
->cmd_code
= CCW_CMD_READ_CONF
;
842 ccw
->count
= offset
+ len
;
843 ccw
->cda
= (__u32
)(unsigned long)config_area
;
844 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_READ_CONFIG
);
848 memcpy(vcdev
->config
, config_area
, offset
+ len
);
850 memcpy(buf
, &vcdev
->config
[offset
], len
);
851 if (vcdev
->config_ready
< offset
+ len
)
852 vcdev
->config_ready
= offset
+ len
;
859 static void virtio_ccw_set_config(struct virtio_device
*vdev
,
860 unsigned int offset
, const void *buf
,
863 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
867 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
871 config_area
= kzalloc(VIRTIO_CCW_CONFIG_SIZE
, GFP_DMA
| GFP_KERNEL
);
875 /* Make sure we don't overwrite fields. */
876 if (vcdev
->config_ready
< offset
)
877 virtio_ccw_get_config(vdev
, 0, NULL
, offset
);
878 memcpy(&vcdev
->config
[offset
], buf
, len
);
879 /* Write the config area to the host. */
880 memcpy(config_area
, vcdev
->config
, sizeof(vcdev
->config
));
881 ccw
->cmd_code
= CCW_CMD_WRITE_CONF
;
883 ccw
->count
= offset
+ len
;
884 ccw
->cda
= (__u32
)(unsigned long)config_area
;
885 ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_WRITE_CONFIG
);
892 static u8
virtio_ccw_get_status(struct virtio_device
*vdev
)
894 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
896 return *vcdev
->status
;
899 static void virtio_ccw_set_status(struct virtio_device
*vdev
, u8 status
)
901 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
902 u8 old_status
= *vcdev
->status
;
906 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
910 /* Write the status to the host. */
911 *vcdev
->status
= status
;
912 ccw
->cmd_code
= CCW_CMD_WRITE_STATUS
;
914 ccw
->count
= sizeof(status
);
915 ccw
->cda
= (__u32
)(unsigned long)vcdev
->status
;
916 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_WRITE_STATUS
);
917 /* Write failed? We assume status is unchanged. */
919 *vcdev
->status
= old_status
;
923 static struct virtio_config_ops virtio_ccw_config_ops
= {
924 .get_features
= virtio_ccw_get_features
,
925 .finalize_features
= virtio_ccw_finalize_features
,
926 .get
= virtio_ccw_get_config
,
927 .set
= virtio_ccw_set_config
,
928 .get_status
= virtio_ccw_get_status
,
929 .set_status
= virtio_ccw_set_status
,
930 .reset
= virtio_ccw_reset
,
931 .find_vqs
= virtio_ccw_find_vqs
,
932 .del_vqs
= virtio_ccw_del_vqs
,
937 * ccw bus driver related functions
940 static void virtio_ccw_release_dev(struct device
*_d
)
942 struct virtio_device
*dev
= dev_to_virtio(_d
);
943 struct virtio_ccw_device
*vcdev
= to_vc_device(dev
);
945 kfree(vcdev
->status
);
946 kfree(vcdev
->config_block
);
950 static int irb_is_error(struct irb
*irb
)
952 if (scsw_cstat(&irb
->scsw
) != 0)
954 if (scsw_dstat(&irb
->scsw
) & ~(DEV_STAT_CHN_END
| DEV_STAT_DEV_END
))
956 if (scsw_cc(&irb
->scsw
) != 0)
961 static struct virtqueue
*virtio_ccw_vq_by_ind(struct virtio_ccw_device
*vcdev
,
964 struct virtio_ccw_vq_info
*info
;
966 struct virtqueue
*vq
;
969 spin_lock_irqsave(&vcdev
->lock
, flags
);
970 list_for_each_entry(info
, &vcdev
->virtqueues
, node
) {
971 if (info
->vq
->index
== index
) {
976 spin_unlock_irqrestore(&vcdev
->lock
, flags
);
980 static void virtio_ccw_check_activity(struct virtio_ccw_device
*vcdev
,
983 if (vcdev
->curr_io
& activity
) {
985 case VIRTIO_CCW_DOING_READ_FEAT
:
986 case VIRTIO_CCW_DOING_WRITE_FEAT
:
987 case VIRTIO_CCW_DOING_READ_CONFIG
:
988 case VIRTIO_CCW_DOING_WRITE_CONFIG
:
989 case VIRTIO_CCW_DOING_WRITE_STATUS
:
990 case VIRTIO_CCW_DOING_SET_VQ
:
991 case VIRTIO_CCW_DOING_SET_IND
:
992 case VIRTIO_CCW_DOING_SET_CONF_IND
:
993 case VIRTIO_CCW_DOING_RESET
:
994 case VIRTIO_CCW_DOING_READ_VQ_CONF
:
995 case VIRTIO_CCW_DOING_SET_IND_ADAPTER
:
996 case VIRTIO_CCW_DOING_SET_VIRTIO_REV
:
997 vcdev
->curr_io
&= ~activity
;
998 wake_up(&vcdev
->wait_q
);
1001 /* don't know what to do... */
1002 dev_warn(&vcdev
->cdev
->dev
,
1003 "Suspicious activity '%08x'\n", activity
);
1010 static void virtio_ccw_int_handler(struct ccw_device
*cdev
,
1011 unsigned long intparm
,
1014 __u32 activity
= intparm
& VIRTIO_CCW_INTPARM_MASK
;
1015 struct virtio_ccw_device
*vcdev
= dev_get_drvdata(&cdev
->dev
);
1017 struct virtqueue
*vq
;
1022 vcdev
->err
= PTR_ERR(irb
);
1023 virtio_ccw_check_activity(vcdev
, activity
);
1024 /* Don't poke around indicators, something's wrong. */
1027 /* Check if it's a notification from the host. */
1028 if ((intparm
== 0) &&
1029 (scsw_stctl(&irb
->scsw
) ==
1030 (SCSW_STCTL_ALERT_STATUS
| SCSW_STCTL_STATUS_PEND
))) {
1033 if (irb_is_error(irb
)) {
1034 /* Command reject? */
1035 if ((scsw_dstat(&irb
->scsw
) & DEV_STAT_UNIT_CHECK
) &&
1036 (irb
->ecw
[0] & SNS0_CMD_REJECT
))
1037 vcdev
->err
= -EOPNOTSUPP
;
1039 /* Map everything else to -EIO. */
1042 virtio_ccw_check_activity(vcdev
, activity
);
1043 for_each_set_bit(i
, &vcdev
->indicators
,
1044 sizeof(vcdev
->indicators
) * BITS_PER_BYTE
) {
1045 /* The bit clear must happen before the vring kick. */
1046 clear_bit(i
, &vcdev
->indicators
);
1048 vq
= virtio_ccw_vq_by_ind(vcdev
, i
);
1049 vring_interrupt(0, vq
);
1051 if (test_bit(0, &vcdev
->indicators2
)) {
1052 virtio_config_changed(&vcdev
->vdev
);
1053 clear_bit(0, &vcdev
->indicators2
);
1058 * We usually want to autoonline all devices, but give the admin
1059 * a way to exempt devices from this.
1061 #define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
1063 static unsigned long devs_no_auto
[__MAX_SSID
+ 1][__DEV_WORDS
];
1065 static char *no_auto
= "";
1067 module_param(no_auto
, charp
, 0444);
1068 MODULE_PARM_DESC(no_auto
, "list of ccw bus id ranges not to be auto-onlined");
1070 static int virtio_ccw_check_autoonline(struct ccw_device
*cdev
)
1072 struct ccw_dev_id id
;
1074 ccw_device_get_id(cdev
, &id
);
1075 if (test_bit(id
.devno
, devs_no_auto
[id
.ssid
]))
1080 static void virtio_ccw_auto_online(void *data
, async_cookie_t cookie
)
1082 struct ccw_device
*cdev
= data
;
1085 ret
= ccw_device_set_online(cdev
);
1087 dev_warn(&cdev
->dev
, "Failed to set online: %d\n", ret
);
1090 static int virtio_ccw_probe(struct ccw_device
*cdev
)
1092 cdev
->handler
= virtio_ccw_int_handler
;
1094 if (virtio_ccw_check_autoonline(cdev
))
1095 async_schedule(virtio_ccw_auto_online
, cdev
);
1099 static struct virtio_ccw_device
*virtio_grab_drvdata(struct ccw_device
*cdev
)
1101 unsigned long flags
;
1102 struct virtio_ccw_device
*vcdev
;
1104 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1105 vcdev
= dev_get_drvdata(&cdev
->dev
);
1106 if (!vcdev
|| vcdev
->going_away
) {
1107 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1110 vcdev
->going_away
= true;
1111 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1115 static void virtio_ccw_remove(struct ccw_device
*cdev
)
1117 unsigned long flags
;
1118 struct virtio_ccw_device
*vcdev
= virtio_grab_drvdata(cdev
);
1120 if (vcdev
&& cdev
->online
) {
1121 if (vcdev
->device_lost
)
1122 virtio_break_device(&vcdev
->vdev
);
1123 unregister_virtio_device(&vcdev
->vdev
);
1124 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1125 dev_set_drvdata(&cdev
->dev
, NULL
);
1126 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1128 cdev
->handler
= NULL
;
1131 static int virtio_ccw_offline(struct ccw_device
*cdev
)
1133 unsigned long flags
;
1134 struct virtio_ccw_device
*vcdev
= virtio_grab_drvdata(cdev
);
1138 if (vcdev
->device_lost
)
1139 virtio_break_device(&vcdev
->vdev
);
1140 unregister_virtio_device(&vcdev
->vdev
);
1141 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1142 dev_set_drvdata(&cdev
->dev
, NULL
);
1143 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1147 static int virtio_ccw_set_transport_rev(struct virtio_ccw_device
*vcdev
)
1149 struct virtio_rev_info
*rev
;
1153 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
1156 rev
= kzalloc(sizeof(*rev
), GFP_DMA
| GFP_KERNEL
);
1162 /* Set transport revision */
1163 ccw
->cmd_code
= CCW_CMD_SET_VIRTIO_REV
;
1165 ccw
->count
= sizeof(*rev
);
1166 ccw
->cda
= (__u32
)(unsigned long)rev
;
1168 vcdev
->revision
= VIRTIO_CCW_REV_MAX
;
1170 rev
->revision
= vcdev
->revision
;
1171 /* none of our supported revisions carry payload */
1173 ret
= ccw_io_helper(vcdev
, ccw
,
1174 VIRTIO_CCW_DOING_SET_VIRTIO_REV
);
1175 if (ret
== -EOPNOTSUPP
) {
1176 if (vcdev
->revision
== 0)
1178 * The host device does not support setting
1179 * the revision: let's operate it in legacy
1186 } while (ret
== -EOPNOTSUPP
);
1193 static int virtio_ccw_online(struct ccw_device
*cdev
)
1196 struct virtio_ccw_device
*vcdev
;
1197 unsigned long flags
;
1199 vcdev
= kzalloc(sizeof(*vcdev
), GFP_KERNEL
);
1201 dev_warn(&cdev
->dev
, "Could not get memory for virtio\n");
1205 vcdev
->config_block
= kzalloc(sizeof(*vcdev
->config_block
),
1206 GFP_DMA
| GFP_KERNEL
);
1207 if (!vcdev
->config_block
) {
1211 vcdev
->status
= kzalloc(sizeof(*vcdev
->status
), GFP_DMA
| GFP_KERNEL
);
1212 if (!vcdev
->status
) {
1217 vcdev
->is_thinint
= virtio_ccw_use_airq
; /* at least try */
1219 vcdev
->vdev
.dev
.parent
= &cdev
->dev
;
1220 vcdev
->vdev
.dev
.release
= virtio_ccw_release_dev
;
1221 vcdev
->vdev
.config
= &virtio_ccw_config_ops
;
1223 init_waitqueue_head(&vcdev
->wait_q
);
1224 INIT_LIST_HEAD(&vcdev
->virtqueues
);
1225 spin_lock_init(&vcdev
->lock
);
1227 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1228 dev_set_drvdata(&cdev
->dev
, vcdev
);
1229 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1230 vcdev
->vdev
.id
.vendor
= cdev
->id
.cu_type
;
1231 vcdev
->vdev
.id
.device
= cdev
->id
.cu_model
;
1233 ret
= virtio_ccw_set_transport_rev(vcdev
);
1237 ret
= register_virtio_device(&vcdev
->vdev
);
1239 dev_warn(&cdev
->dev
, "Failed to register virtio device: %d\n",
1245 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1246 dev_set_drvdata(&cdev
->dev
, NULL
);
1247 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1248 put_device(&vcdev
->vdev
.dev
);
1252 kfree(vcdev
->status
);
1253 kfree(vcdev
->config_block
);
1259 static int virtio_ccw_cio_notify(struct ccw_device
*cdev
, int event
)
1262 struct virtio_ccw_device
*vcdev
= dev_get_drvdata(&cdev
->dev
);
1265 * Make sure vcdev is set
1266 * i.e. set_offline/remove callback not already running
1273 vcdev
->device_lost
= true;
1283 static struct ccw_device_id virtio_ids
[] = {
1284 { CCW_DEVICE(0x3832, 0) },
1288 static struct ccw_driver virtio_ccw_driver
= {
1290 .owner
= THIS_MODULE
,
1291 .name
= "virtio_ccw",
1294 .probe
= virtio_ccw_probe
,
1295 .remove
= virtio_ccw_remove
,
1296 .set_offline
= virtio_ccw_offline
,
1297 .set_online
= virtio_ccw_online
,
1298 .notify
= virtio_ccw_cio_notify
,
1299 .int_class
= IRQIO_VIR
,
1302 static int __init
pure_hex(char **cp
, unsigned int *val
, int min_digit
,
1303 int max_digit
, int max_val
)
1310 while (diff
<= max_digit
) {
1311 int value
= hex_to_bin(**cp
);
1315 *val
= *val
* 16 + value
;
1320 if ((diff
< min_digit
) || (diff
> max_digit
) || (*val
> max_val
))
1326 static int __init
parse_busid(char *str
, unsigned int *cssid
,
1327 unsigned int *ssid
, unsigned int *devno
)
1338 ret
= pure_hex(&str_work
, cssid
, 1, 2, __MAX_CSSID
);
1339 if (ret
|| (str_work
[0] != '.'))
1342 ret
= pure_hex(&str_work
, ssid
, 1, 1, __MAX_SSID
);
1343 if (ret
|| (str_work
[0] != '.'))
1346 ret
= pure_hex(&str_work
, devno
, 4, 4, __MAX_SUBCHANNEL
);
1347 if (ret
|| (str_work
[0] != '\0'))
1355 static void __init
no_auto_parse(void)
1357 unsigned int from_cssid
, to_cssid
, from_ssid
, to_ssid
, from
, to
;
1362 while ((parm
= strsep(&str
, ","))) {
1363 rc
= parse_busid(strsep(&parm
, "-"), &from_cssid
,
1368 rc
= parse_busid(parm
, &to_cssid
,
1370 if ((from_ssid
> to_ssid
) ||
1371 ((from_ssid
== to_ssid
) && (from
> to
)))
1374 to_cssid
= from_cssid
;
1375 to_ssid
= from_ssid
;
1380 while ((from_ssid
< to_ssid
) ||
1381 ((from_ssid
== to_ssid
) && (from
<= to
))) {
1382 set_bit(from
, devs_no_auto
[from_ssid
]);
1384 if (from
> __MAX_SUBCHANNEL
) {
1392 static int __init
virtio_ccw_init(void)
1394 /* parse no_auto string before we do anything further */
1396 return ccw_driver_register(&virtio_ccw_driver
);
1398 device_initcall(virtio_ccw_init
);