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_READ_STATUS 0x72
149 #define CCW_CMD_SET_IND_ADAPTER 0x73
150 #define CCW_CMD_SET_VIRTIO_REV 0x83
152 #define VIRTIO_CCW_DOING_SET_VQ 0x00010000
153 #define VIRTIO_CCW_DOING_RESET 0x00040000
154 #define VIRTIO_CCW_DOING_READ_FEAT 0x00080000
155 #define VIRTIO_CCW_DOING_WRITE_FEAT 0x00100000
156 #define VIRTIO_CCW_DOING_READ_CONFIG 0x00200000
157 #define VIRTIO_CCW_DOING_WRITE_CONFIG 0x00400000
158 #define VIRTIO_CCW_DOING_WRITE_STATUS 0x00800000
159 #define VIRTIO_CCW_DOING_SET_IND 0x01000000
160 #define VIRTIO_CCW_DOING_READ_VQ_CONF 0x02000000
161 #define VIRTIO_CCW_DOING_SET_CONF_IND 0x04000000
162 #define VIRTIO_CCW_DOING_SET_IND_ADAPTER 0x08000000
163 #define VIRTIO_CCW_DOING_SET_VIRTIO_REV 0x10000000
164 #define VIRTIO_CCW_DOING_READ_STATUS 0x20000000
165 #define VIRTIO_CCW_INTPARM_MASK 0xffff0000
167 static struct virtio_ccw_device
*to_vc_device(struct virtio_device
*vdev
)
169 return container_of(vdev
, struct virtio_ccw_device
, vdev
);
172 static void drop_airq_indicator(struct virtqueue
*vq
, struct airq_info
*info
)
174 unsigned long i
, flags
;
176 write_lock_irqsave(&info
->lock
, flags
);
177 for (i
= 0; i
< airq_iv_end(info
->aiv
); i
++) {
178 if (vq
== (void *)airq_iv_get_ptr(info
->aiv
, i
)) {
179 airq_iv_free_bit(info
->aiv
, i
);
180 airq_iv_set_ptr(info
->aiv
, i
, 0);
184 write_unlock_irqrestore(&info
->lock
, flags
);
187 static void virtio_airq_handler(struct airq_struct
*airq
)
189 struct airq_info
*info
= container_of(airq
, struct airq_info
, airq
);
192 inc_irq_stat(IRQIO_VAI
);
193 read_lock(&info
->lock
);
194 /* Walk through indicators field, summary indicator active. */
196 ai
= airq_iv_scan(info
->aiv
, ai
, airq_iv_end(info
->aiv
));
199 vring_interrupt(0, (void *)airq_iv_get_ptr(info
->aiv
, ai
));
201 info
->summary_indicator
= 0;
203 /* Walk through indicators field, summary indicator not active. */
205 ai
= airq_iv_scan(info
->aiv
, ai
, airq_iv_end(info
->aiv
));
208 vring_interrupt(0, (void *)airq_iv_get_ptr(info
->aiv
, ai
));
210 read_unlock(&info
->lock
);
213 static struct airq_info
*new_airq_info(void)
215 struct airq_info
*info
;
218 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
221 rwlock_init(&info
->lock
);
222 info
->aiv
= airq_iv_create(VIRTIO_IV_BITS
, AIRQ_IV_ALLOC
| AIRQ_IV_PTR
);
227 info
->airq
.handler
= virtio_airq_handler
;
228 info
->airq
.lsi_ptr
= &info
->summary_indicator
;
229 info
->airq
.lsi_mask
= 0xff;
230 info
->airq
.isc
= VIRTIO_AIRQ_ISC
;
231 rc
= register_adapter_interrupt(&info
->airq
);
233 airq_iv_release(info
->aiv
);
240 static unsigned long get_airq_indicator(struct virtqueue
*vqs
[], int nvqs
,
241 u64
*first
, void **airq_info
)
244 struct airq_info
*info
;
245 unsigned long indicator_addr
= 0;
246 unsigned long bit
, flags
;
248 for (i
= 0; i
< MAX_AIRQ_AREAS
&& !indicator_addr
; i
++) {
250 airq_areas
[i
] = new_airq_info();
251 info
= airq_areas
[i
];
254 write_lock_irqsave(&info
->lock
, flags
);
255 bit
= airq_iv_alloc(info
->aiv
, nvqs
);
257 /* Not enough vacancies. */
258 write_unlock_irqrestore(&info
->lock
, flags
);
263 indicator_addr
= (unsigned long)info
->aiv
->vector
;
264 for (j
= 0; j
< nvqs
; j
++) {
265 airq_iv_set_ptr(info
->aiv
, bit
+ j
,
266 (unsigned long)vqs
[j
]);
268 write_unlock_irqrestore(&info
->lock
, flags
);
270 return indicator_addr
;
273 static void virtio_ccw_drop_indicators(struct virtio_ccw_device
*vcdev
)
275 struct virtio_ccw_vq_info
*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
;
303 spin_lock_irqsave(get_ccwdev_lock(vcdev
->cdev
), flags
);
304 ret
= ccw_device_start(vcdev
->cdev
, ccw
, intparm
, 0, 0);
308 vcdev
->curr_io
|= flag
;
310 spin_unlock_irqrestore(get_ccwdev_lock(vcdev
->cdev
), flags
);
312 } while (ret
== -EBUSY
);
313 wait_event(vcdev
->wait_q
, doing_io(vcdev
, flag
) == 0);
314 return ret
? ret
: vcdev
->err
;
317 static void virtio_ccw_drop_indicator(struct virtio_ccw_device
*vcdev
,
321 unsigned long *indicatorp
= NULL
;
322 struct virtio_thinint_area
*thinint_area
= NULL
;
323 struct airq_info
*airq_info
= vcdev
->airq_info
;
325 if (vcdev
->is_thinint
) {
326 thinint_area
= kzalloc(sizeof(*thinint_area
),
327 GFP_DMA
| GFP_KERNEL
);
330 thinint_area
->summary_indicator
=
331 (unsigned long) &airq_info
->summary_indicator
;
332 thinint_area
->isc
= VIRTIO_AIRQ_ISC
;
333 ccw
->cmd_code
= CCW_CMD_SET_IND_ADAPTER
;
334 ccw
->count
= sizeof(*thinint_area
);
335 ccw
->cda
= (__u32
)(unsigned long) thinint_area
;
337 /* payload is the address of the indicators */
338 indicatorp
= kmalloc(sizeof(&vcdev
->indicators
),
339 GFP_DMA
| GFP_KERNEL
);
343 ccw
->cmd_code
= CCW_CMD_SET_IND
;
344 ccw
->count
= sizeof(&vcdev
->indicators
);
345 ccw
->cda
= (__u32
)(unsigned long) indicatorp
;
347 /* Deregister indicators from host. */
348 vcdev
->indicators
= 0;
350 ret
= ccw_io_helper(vcdev
, ccw
,
352 VIRTIO_CCW_DOING_SET_IND_ADAPTER
:
353 VIRTIO_CCW_DOING_SET_IND
);
354 if (ret
&& (ret
!= -ENODEV
))
355 dev_info(&vcdev
->cdev
->dev
,
356 "Failed to deregister indicators (%d)\n", ret
);
357 else if (vcdev
->is_thinint
)
358 virtio_ccw_drop_indicators(vcdev
);
363 static inline long __do_kvm_notify(struct subchannel_id schid
,
364 unsigned long queue_index
,
367 register unsigned long __nr
asm("1") = KVM_S390_VIRTIO_CCW_NOTIFY
;
368 register struct subchannel_id __schid
asm("2") = schid
;
369 register unsigned long __index
asm("3") = queue_index
;
370 register long __rc
asm("2");
371 register long __cookie
asm("4") = cookie
;
373 asm volatile ("diag 2,4,0x500\n"
374 : "=d" (__rc
) : "d" (__nr
), "d" (__schid
), "d" (__index
),
380 static inline long do_kvm_notify(struct subchannel_id schid
,
381 unsigned long queue_index
,
384 diag_stat_inc(DIAG_STAT_X500
);
385 return __do_kvm_notify(schid
, queue_index
, cookie
);
388 static bool virtio_ccw_kvm_notify(struct virtqueue
*vq
)
390 struct virtio_ccw_vq_info
*info
= vq
->priv
;
391 struct virtio_ccw_device
*vcdev
;
392 struct subchannel_id schid
;
394 vcdev
= to_vc_device(info
->vq
->vdev
);
395 ccw_device_get_schid(vcdev
->cdev
, &schid
);
396 info
->cookie
= do_kvm_notify(schid
, vq
->index
, info
->cookie
);
397 if (info
->cookie
< 0)
402 static int virtio_ccw_read_vq_conf(struct virtio_ccw_device
*vcdev
,
403 struct ccw1
*ccw
, int index
)
407 vcdev
->config_block
->index
= index
;
408 ccw
->cmd_code
= CCW_CMD_READ_VQ_CONF
;
410 ccw
->count
= sizeof(struct vq_config_block
);
411 ccw
->cda
= (__u32
)(unsigned long)(vcdev
->config_block
);
412 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_READ_VQ_CONF
);
415 return vcdev
->config_block
->num
;
418 static void virtio_ccw_del_vq(struct virtqueue
*vq
, struct ccw1
*ccw
)
420 struct virtio_ccw_device
*vcdev
= to_vc_device(vq
->vdev
);
421 struct virtio_ccw_vq_info
*info
= vq
->priv
;
425 unsigned int index
= vq
->index
;
427 /* Remove from our list. */
428 spin_lock_irqsave(&vcdev
->lock
, flags
);
429 list_del(&info
->node
);
430 spin_unlock_irqrestore(&vcdev
->lock
, flags
);
432 /* Release from host. */
433 if (vcdev
->revision
== 0) {
434 info
->info_block
->l
.queue
= 0;
435 info
->info_block
->l
.align
= 0;
436 info
->info_block
->l
.index
= index
;
437 info
->info_block
->l
.num
= 0;
438 ccw
->count
= sizeof(info
->info_block
->l
);
440 info
->info_block
->s
.desc
= 0;
441 info
->info_block
->s
.index
= index
;
442 info
->info_block
->s
.num
= 0;
443 info
->info_block
->s
.avail
= 0;
444 info
->info_block
->s
.used
= 0;
445 ccw
->count
= sizeof(info
->info_block
->s
);
447 ccw
->cmd_code
= CCW_CMD_SET_VQ
;
449 ccw
->cda
= (__u32
)(unsigned long)(info
->info_block
);
450 ret
= ccw_io_helper(vcdev
, ccw
,
451 VIRTIO_CCW_DOING_SET_VQ
| index
);
453 * -ENODEV isn't considered an error: The device is gone anyway.
454 * This may happen on device detach.
456 if (ret
&& (ret
!= -ENODEV
))
457 dev_warn(&vq
->vdev
->dev
, "Error %d while deleting queue %d\n",
460 vring_del_virtqueue(vq
);
461 size
= PAGE_ALIGN(vring_size(info
->num
, KVM_VIRTIO_CCW_RING_ALIGN
));
462 free_pages_exact(info
->queue
, size
);
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
,
490 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
492 struct virtqueue
*vq
= NULL
;
493 struct virtio_ccw_vq_info
*info
;
494 unsigned long size
= 0; /* silence the compiler */
497 /* Allocate queue. */
498 info
= kzalloc(sizeof(struct virtio_ccw_vq_info
), GFP_KERNEL
);
500 dev_warn(&vcdev
->cdev
->dev
, "no info\n");
504 info
->info_block
= kzalloc(sizeof(*info
->info_block
),
505 GFP_DMA
| GFP_KERNEL
);
506 if (!info
->info_block
) {
507 dev_warn(&vcdev
->cdev
->dev
, "no info block\n");
511 info
->num
= virtio_ccw_read_vq_conf(vcdev
, ccw
, i
);
516 size
= PAGE_ALIGN(vring_size(info
->num
, KVM_VIRTIO_CCW_RING_ALIGN
));
517 info
->queue
= alloc_pages_exact(size
, GFP_KERNEL
| __GFP_ZERO
);
518 if (info
->queue
== NULL
) {
519 dev_warn(&vcdev
->cdev
->dev
, "no queue\n");
524 vq
= vring_new_virtqueue(i
, info
->num
, KVM_VIRTIO_CCW_RING_ALIGN
, vdev
,
525 true, info
->queue
, virtio_ccw_kvm_notify
,
528 /* For now, we fail if we can't get the requested size. */
529 dev_warn(&vcdev
->cdev
->dev
, "no vq\n");
534 /* Register it with the host. */
535 if (vcdev
->revision
== 0) {
536 info
->info_block
->l
.queue
= (__u64
)info
->queue
;
537 info
->info_block
->l
.align
= KVM_VIRTIO_CCW_RING_ALIGN
;
538 info
->info_block
->l
.index
= i
;
539 info
->info_block
->l
.num
= info
->num
;
540 ccw
->count
= sizeof(info
->info_block
->l
);
542 info
->info_block
->s
.desc
= (__u64
)info
->queue
;
543 info
->info_block
->s
.index
= i
;
544 info
->info_block
->s
.num
= info
->num
;
545 info
->info_block
->s
.avail
= (__u64
)virtqueue_get_avail(vq
);
546 info
->info_block
->s
.used
= (__u64
)virtqueue_get_used(vq
);
547 ccw
->count
= sizeof(info
->info_block
->s
);
549 ccw
->cmd_code
= CCW_CMD_SET_VQ
;
551 ccw
->cda
= (__u32
)(unsigned long)(info
->info_block
);
552 err
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_SET_VQ
| i
);
554 dev_warn(&vcdev
->cdev
->dev
, "SET_VQ failed\n");
561 /* Save it to our list. */
562 spin_lock_irqsave(&vcdev
->lock
, flags
);
563 list_add(&info
->node
, &vcdev
->virtqueues
);
564 spin_unlock_irqrestore(&vcdev
->lock
, flags
);
570 vring_del_virtqueue(vq
);
573 free_pages_exact(info
->queue
, size
);
574 kfree(info
->info_block
);
580 static int virtio_ccw_register_adapter_ind(struct virtio_ccw_device
*vcdev
,
581 struct virtqueue
*vqs
[], int nvqs
,
585 struct virtio_thinint_area
*thinint_area
= NULL
;
586 struct airq_info
*info
;
588 thinint_area
= kzalloc(sizeof(*thinint_area
), GFP_DMA
| GFP_KERNEL
);
593 /* Try to get an indicator. */
594 thinint_area
->indicator
= get_airq_indicator(vqs
, nvqs
,
595 &thinint_area
->bit_nr
,
597 if (!thinint_area
->indicator
) {
601 info
= vcdev
->airq_info
;
602 thinint_area
->summary_indicator
=
603 (unsigned long) &info
->summary_indicator
;
604 thinint_area
->isc
= VIRTIO_AIRQ_ISC
;
605 ccw
->cmd_code
= CCW_CMD_SET_IND_ADAPTER
;
606 ccw
->flags
= CCW_FLAG_SLI
;
607 ccw
->count
= sizeof(*thinint_area
);
608 ccw
->cda
= (__u32
)(unsigned long)thinint_area
;
609 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_SET_IND_ADAPTER
);
611 if (ret
== -EOPNOTSUPP
) {
613 * The host does not support adapter interrupts
614 * for virtio-ccw, stop trying.
616 virtio_ccw_use_airq
= 0;
617 pr_info("Adapter interrupts unsupported on host\n");
619 dev_warn(&vcdev
->cdev
->dev
,
620 "enabling adapter interrupts = %d\n", ret
);
621 virtio_ccw_drop_indicators(vcdev
);
628 static int virtio_ccw_find_vqs(struct virtio_device
*vdev
, unsigned nvqs
,
629 struct virtqueue
*vqs
[],
630 vq_callback_t
*callbacks
[],
631 const char * const names
[])
633 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
634 unsigned long *indicatorp
= NULL
;
638 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
642 for (i
= 0; i
< nvqs
; ++i
) {
643 vqs
[i
] = virtio_ccw_setup_vq(vdev
, i
, callbacks
[i
], names
[i
],
645 if (IS_ERR(vqs
[i
])) {
646 ret
= PTR_ERR(vqs
[i
]);
653 * We need a data area under 2G to communicate. Our payload is
654 * the address of the indicators.
656 indicatorp
= kmalloc(sizeof(&vcdev
->indicators
), GFP_DMA
| GFP_KERNEL
);
659 *indicatorp
= (unsigned long) &vcdev
->indicators
;
660 if (vcdev
->is_thinint
) {
661 ret
= virtio_ccw_register_adapter_ind(vcdev
, vqs
, nvqs
, ccw
);
663 /* no error, just fall back to legacy interrupts */
664 vcdev
->is_thinint
= false;
666 if (!vcdev
->is_thinint
) {
667 /* Register queue indicators with host. */
668 vcdev
->indicators
= 0;
669 ccw
->cmd_code
= CCW_CMD_SET_IND
;
671 ccw
->count
= sizeof(&vcdev
->indicators
);
672 ccw
->cda
= (__u32
)(unsigned long) indicatorp
;
673 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_SET_IND
);
677 /* Register indicators2 with host for config changes */
678 *indicatorp
= (unsigned long) &vcdev
->indicators2
;
679 vcdev
->indicators2
= 0;
680 ccw
->cmd_code
= CCW_CMD_SET_CONF_IND
;
682 ccw
->count
= sizeof(&vcdev
->indicators2
);
683 ccw
->cda
= (__u32
)(unsigned long) indicatorp
;
684 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_SET_CONF_IND
);
694 virtio_ccw_del_vqs(vdev
);
698 static void virtio_ccw_reset(struct virtio_device
*vdev
)
700 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
703 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
707 /* Zero status bits. */
710 /* Send a reset ccw on device. */
711 ccw
->cmd_code
= CCW_CMD_VDEV_RESET
;
715 ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_RESET
);
719 static u64
virtio_ccw_get_features(struct virtio_device
*vdev
)
721 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
722 struct virtio_feature_desc
*features
;
727 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
731 features
= kzalloc(sizeof(*features
), GFP_DMA
| GFP_KERNEL
);
736 /* Read the feature bits from the host. */
738 ccw
->cmd_code
= CCW_CMD_READ_FEAT
;
740 ccw
->count
= sizeof(*features
);
741 ccw
->cda
= (__u32
)(unsigned long)features
;
742 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_READ_FEAT
);
748 rc
= le32_to_cpu(features
->features
);
750 if (vcdev
->revision
== 0)
753 /* Read second half of the feature bits from the host. */
755 ccw
->cmd_code
= CCW_CMD_READ_FEAT
;
757 ccw
->count
= sizeof(*features
);
758 ccw
->cda
= (__u32
)(unsigned long)features
;
759 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_READ_FEAT
);
761 rc
|= (u64
)le32_to_cpu(features
->features
) << 32;
769 static int virtio_ccw_finalize_features(struct virtio_device
*vdev
)
771 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
772 struct virtio_feature_desc
*features
;
776 if (vcdev
->revision
>= 1 &&
777 !__virtio_test_bit(vdev
, VIRTIO_F_VERSION_1
)) {
778 dev_err(&vdev
->dev
, "virtio: device uses revision 1 "
779 "but does not have VIRTIO_F_VERSION_1\n");
783 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
787 features
= kzalloc(sizeof(*features
), GFP_DMA
| GFP_KERNEL
);
792 /* Give virtio_ring a chance to accept features. */
793 vring_transport_features(vdev
);
796 features
->features
= cpu_to_le32((u32
)vdev
->features
);
797 /* Write the first half of the feature bits to the host. */
798 ccw
->cmd_code
= CCW_CMD_WRITE_FEAT
;
800 ccw
->count
= sizeof(*features
);
801 ccw
->cda
= (__u32
)(unsigned long)features
;
802 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_WRITE_FEAT
);
806 if (vcdev
->revision
== 0)
810 features
->features
= cpu_to_le32(vdev
->features
>> 32);
811 /* Write the second 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
);
825 static void virtio_ccw_get_config(struct virtio_device
*vdev
,
826 unsigned int offset
, void *buf
, unsigned len
)
828 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
833 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
837 config_area
= kzalloc(VIRTIO_CCW_CONFIG_SIZE
, GFP_DMA
| GFP_KERNEL
);
841 /* Read the config area from the host. */
842 ccw
->cmd_code
= CCW_CMD_READ_CONF
;
844 ccw
->count
= offset
+ len
;
845 ccw
->cda
= (__u32
)(unsigned long)config_area
;
846 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_READ_CONFIG
);
850 memcpy(vcdev
->config
, config_area
, offset
+ len
);
852 memcpy(buf
, &vcdev
->config
[offset
], len
);
853 if (vcdev
->config_ready
< offset
+ len
)
854 vcdev
->config_ready
= offset
+ len
;
861 static void virtio_ccw_set_config(struct virtio_device
*vdev
,
862 unsigned int offset
, const void *buf
,
865 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
869 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
873 config_area
= kzalloc(VIRTIO_CCW_CONFIG_SIZE
, GFP_DMA
| GFP_KERNEL
);
877 /* Make sure we don't overwrite fields. */
878 if (vcdev
->config_ready
< offset
)
879 virtio_ccw_get_config(vdev
, 0, NULL
, offset
);
880 memcpy(&vcdev
->config
[offset
], buf
, len
);
881 /* Write the config area to the host. */
882 memcpy(config_area
, vcdev
->config
, sizeof(vcdev
->config
));
883 ccw
->cmd_code
= CCW_CMD_WRITE_CONF
;
885 ccw
->count
= offset
+ len
;
886 ccw
->cda
= (__u32
)(unsigned long)config_area
;
887 ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_WRITE_CONFIG
);
894 static u8
virtio_ccw_get_status(struct virtio_device
*vdev
)
896 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
897 u8 old_status
= *vcdev
->status
;
900 if (vcdev
->revision
< 1)
901 return *vcdev
->status
;
903 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
907 ccw
->cmd_code
= CCW_CMD_READ_STATUS
;
909 ccw
->count
= sizeof(*vcdev
->status
);
910 ccw
->cda
= (__u32
)(unsigned long)vcdev
->status
;
911 ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_READ_STATUS
);
913 * If the channel program failed (should only happen if the device
914 * was hotunplugged, and then we clean up via the machine check
915 * handler anyway), vcdev->status was not overwritten and we just
916 * return the old status, which is fine.
920 return *vcdev
->status
;
923 static void virtio_ccw_set_status(struct virtio_device
*vdev
, u8 status
)
925 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
926 u8 old_status
= *vcdev
->status
;
930 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
934 /* Write the status to the host. */
935 *vcdev
->status
= status
;
936 ccw
->cmd_code
= CCW_CMD_WRITE_STATUS
;
938 ccw
->count
= sizeof(status
);
939 ccw
->cda
= (__u32
)(unsigned long)vcdev
->status
;
940 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_WRITE_STATUS
);
941 /* Write failed? We assume status is unchanged. */
943 *vcdev
->status
= old_status
;
947 static const struct virtio_config_ops virtio_ccw_config_ops
= {
948 .get_features
= virtio_ccw_get_features
,
949 .finalize_features
= virtio_ccw_finalize_features
,
950 .get
= virtio_ccw_get_config
,
951 .set
= virtio_ccw_set_config
,
952 .get_status
= virtio_ccw_get_status
,
953 .set_status
= virtio_ccw_set_status
,
954 .reset
= virtio_ccw_reset
,
955 .find_vqs
= virtio_ccw_find_vqs
,
956 .del_vqs
= virtio_ccw_del_vqs
,
961 * ccw bus driver related functions
964 static void virtio_ccw_release_dev(struct device
*_d
)
966 struct virtio_device
*dev
= dev_to_virtio(_d
);
967 struct virtio_ccw_device
*vcdev
= to_vc_device(dev
);
969 kfree(vcdev
->status
);
970 kfree(vcdev
->config_block
);
974 static int irb_is_error(struct irb
*irb
)
976 if (scsw_cstat(&irb
->scsw
) != 0)
978 if (scsw_dstat(&irb
->scsw
) & ~(DEV_STAT_CHN_END
| DEV_STAT_DEV_END
))
980 if (scsw_cc(&irb
->scsw
) != 0)
985 static struct virtqueue
*virtio_ccw_vq_by_ind(struct virtio_ccw_device
*vcdev
,
988 struct virtio_ccw_vq_info
*info
;
990 struct virtqueue
*vq
;
993 spin_lock_irqsave(&vcdev
->lock
, flags
);
994 list_for_each_entry(info
, &vcdev
->virtqueues
, node
) {
995 if (info
->vq
->index
== index
) {
1000 spin_unlock_irqrestore(&vcdev
->lock
, flags
);
1004 static void virtio_ccw_check_activity(struct virtio_ccw_device
*vcdev
,
1007 if (vcdev
->curr_io
& activity
) {
1009 case VIRTIO_CCW_DOING_READ_FEAT
:
1010 case VIRTIO_CCW_DOING_WRITE_FEAT
:
1011 case VIRTIO_CCW_DOING_READ_CONFIG
:
1012 case VIRTIO_CCW_DOING_WRITE_CONFIG
:
1013 case VIRTIO_CCW_DOING_WRITE_STATUS
:
1014 case VIRTIO_CCW_DOING_READ_STATUS
:
1015 case VIRTIO_CCW_DOING_SET_VQ
:
1016 case VIRTIO_CCW_DOING_SET_IND
:
1017 case VIRTIO_CCW_DOING_SET_CONF_IND
:
1018 case VIRTIO_CCW_DOING_RESET
:
1019 case VIRTIO_CCW_DOING_READ_VQ_CONF
:
1020 case VIRTIO_CCW_DOING_SET_IND_ADAPTER
:
1021 case VIRTIO_CCW_DOING_SET_VIRTIO_REV
:
1022 vcdev
->curr_io
&= ~activity
;
1023 wake_up(&vcdev
->wait_q
);
1026 /* don't know what to do... */
1027 dev_warn(&vcdev
->cdev
->dev
,
1028 "Suspicious activity '%08x'\n", activity
);
1035 static void virtio_ccw_int_handler(struct ccw_device
*cdev
,
1036 unsigned long intparm
,
1039 __u32 activity
= intparm
& VIRTIO_CCW_INTPARM_MASK
;
1040 struct virtio_ccw_device
*vcdev
= dev_get_drvdata(&cdev
->dev
);
1042 struct virtqueue
*vq
;
1047 vcdev
->err
= PTR_ERR(irb
);
1048 virtio_ccw_check_activity(vcdev
, activity
);
1049 /* Don't poke around indicators, something's wrong. */
1052 /* Check if it's a notification from the host. */
1053 if ((intparm
== 0) &&
1054 (scsw_stctl(&irb
->scsw
) ==
1055 (SCSW_STCTL_ALERT_STATUS
| SCSW_STCTL_STATUS_PEND
))) {
1058 if (irb_is_error(irb
)) {
1059 /* Command reject? */
1060 if ((scsw_dstat(&irb
->scsw
) & DEV_STAT_UNIT_CHECK
) &&
1061 (irb
->ecw
[0] & SNS0_CMD_REJECT
))
1062 vcdev
->err
= -EOPNOTSUPP
;
1064 /* Map everything else to -EIO. */
1067 virtio_ccw_check_activity(vcdev
, activity
);
1068 for_each_set_bit(i
, &vcdev
->indicators
,
1069 sizeof(vcdev
->indicators
) * BITS_PER_BYTE
) {
1070 /* The bit clear must happen before the vring kick. */
1071 clear_bit(i
, &vcdev
->indicators
);
1073 vq
= virtio_ccw_vq_by_ind(vcdev
, i
);
1074 vring_interrupt(0, vq
);
1076 if (test_bit(0, &vcdev
->indicators2
)) {
1077 virtio_config_changed(&vcdev
->vdev
);
1078 clear_bit(0, &vcdev
->indicators2
);
1083 * We usually want to autoonline all devices, but give the admin
1084 * a way to exempt devices from this.
1086 #define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
1088 static unsigned long devs_no_auto
[__MAX_SSID
+ 1][__DEV_WORDS
];
1090 static char *no_auto
= "";
1092 module_param(no_auto
, charp
, 0444);
1093 MODULE_PARM_DESC(no_auto
, "list of ccw bus id ranges not to be auto-onlined");
1095 static int virtio_ccw_check_autoonline(struct ccw_device
*cdev
)
1097 struct ccw_dev_id id
;
1099 ccw_device_get_id(cdev
, &id
);
1100 if (test_bit(id
.devno
, devs_no_auto
[id
.ssid
]))
1105 static void virtio_ccw_auto_online(void *data
, async_cookie_t cookie
)
1107 struct ccw_device
*cdev
= data
;
1110 ret
= ccw_device_set_online(cdev
);
1112 dev_warn(&cdev
->dev
, "Failed to set online: %d\n", ret
);
1115 static int virtio_ccw_probe(struct ccw_device
*cdev
)
1117 cdev
->handler
= virtio_ccw_int_handler
;
1119 if (virtio_ccw_check_autoonline(cdev
))
1120 async_schedule(virtio_ccw_auto_online
, cdev
);
1124 static struct virtio_ccw_device
*virtio_grab_drvdata(struct ccw_device
*cdev
)
1126 unsigned long flags
;
1127 struct virtio_ccw_device
*vcdev
;
1129 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1130 vcdev
= dev_get_drvdata(&cdev
->dev
);
1131 if (!vcdev
|| vcdev
->going_away
) {
1132 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1135 vcdev
->going_away
= true;
1136 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1140 static void virtio_ccw_remove(struct ccw_device
*cdev
)
1142 unsigned long flags
;
1143 struct virtio_ccw_device
*vcdev
= virtio_grab_drvdata(cdev
);
1145 if (vcdev
&& cdev
->online
) {
1146 if (vcdev
->device_lost
)
1147 virtio_break_device(&vcdev
->vdev
);
1148 unregister_virtio_device(&vcdev
->vdev
);
1149 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1150 dev_set_drvdata(&cdev
->dev
, NULL
);
1151 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1153 cdev
->handler
= NULL
;
1156 static int virtio_ccw_offline(struct ccw_device
*cdev
)
1158 unsigned long flags
;
1159 struct virtio_ccw_device
*vcdev
= virtio_grab_drvdata(cdev
);
1163 if (vcdev
->device_lost
)
1164 virtio_break_device(&vcdev
->vdev
);
1165 unregister_virtio_device(&vcdev
->vdev
);
1166 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1167 dev_set_drvdata(&cdev
->dev
, NULL
);
1168 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1172 static int virtio_ccw_set_transport_rev(struct virtio_ccw_device
*vcdev
)
1174 struct virtio_rev_info
*rev
;
1178 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
1181 rev
= kzalloc(sizeof(*rev
), GFP_DMA
| GFP_KERNEL
);
1187 /* Set transport revision */
1188 ccw
->cmd_code
= CCW_CMD_SET_VIRTIO_REV
;
1190 ccw
->count
= sizeof(*rev
);
1191 ccw
->cda
= (__u32
)(unsigned long)rev
;
1193 vcdev
->revision
= VIRTIO_CCW_REV_MAX
;
1195 rev
->revision
= vcdev
->revision
;
1196 /* none of our supported revisions carry payload */
1198 ret
= ccw_io_helper(vcdev
, ccw
,
1199 VIRTIO_CCW_DOING_SET_VIRTIO_REV
);
1200 if (ret
== -EOPNOTSUPP
) {
1201 if (vcdev
->revision
== 0)
1203 * The host device does not support setting
1204 * the revision: let's operate it in legacy
1211 } while (ret
== -EOPNOTSUPP
);
1218 static int virtio_ccw_online(struct ccw_device
*cdev
)
1221 struct virtio_ccw_device
*vcdev
;
1222 unsigned long flags
;
1224 vcdev
= kzalloc(sizeof(*vcdev
), GFP_KERNEL
);
1226 dev_warn(&cdev
->dev
, "Could not get memory for virtio\n");
1230 vcdev
->config_block
= kzalloc(sizeof(*vcdev
->config_block
),
1231 GFP_DMA
| GFP_KERNEL
);
1232 if (!vcdev
->config_block
) {
1236 vcdev
->status
= kzalloc(sizeof(*vcdev
->status
), GFP_DMA
| GFP_KERNEL
);
1237 if (!vcdev
->status
) {
1242 vcdev
->is_thinint
= virtio_ccw_use_airq
; /* at least try */
1244 vcdev
->vdev
.dev
.parent
= &cdev
->dev
;
1245 vcdev
->vdev
.dev
.release
= virtio_ccw_release_dev
;
1246 vcdev
->vdev
.config
= &virtio_ccw_config_ops
;
1248 init_waitqueue_head(&vcdev
->wait_q
);
1249 INIT_LIST_HEAD(&vcdev
->virtqueues
);
1250 spin_lock_init(&vcdev
->lock
);
1252 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1253 dev_set_drvdata(&cdev
->dev
, vcdev
);
1254 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1255 vcdev
->vdev
.id
.vendor
= cdev
->id
.cu_type
;
1256 vcdev
->vdev
.id
.device
= cdev
->id
.cu_model
;
1258 ret
= virtio_ccw_set_transport_rev(vcdev
);
1262 ret
= register_virtio_device(&vcdev
->vdev
);
1264 dev_warn(&cdev
->dev
, "Failed to register virtio device: %d\n",
1270 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1271 dev_set_drvdata(&cdev
->dev
, NULL
);
1272 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1273 put_device(&vcdev
->vdev
.dev
);
1277 kfree(vcdev
->status
);
1278 kfree(vcdev
->config_block
);
1284 static int virtio_ccw_cio_notify(struct ccw_device
*cdev
, int event
)
1287 struct virtio_ccw_device
*vcdev
= dev_get_drvdata(&cdev
->dev
);
1290 * Make sure vcdev is set
1291 * i.e. set_offline/remove callback not already running
1298 vcdev
->device_lost
= true;
1308 static struct ccw_device_id virtio_ids
[] = {
1309 { CCW_DEVICE(0x3832, 0) },
1313 static struct ccw_driver virtio_ccw_driver
= {
1315 .owner
= THIS_MODULE
,
1316 .name
= "virtio_ccw",
1319 .probe
= virtio_ccw_probe
,
1320 .remove
= virtio_ccw_remove
,
1321 .set_offline
= virtio_ccw_offline
,
1322 .set_online
= virtio_ccw_online
,
1323 .notify
= virtio_ccw_cio_notify
,
1324 .int_class
= IRQIO_VIR
,
1327 static int __init
pure_hex(char **cp
, unsigned int *val
, int min_digit
,
1328 int max_digit
, int max_val
)
1335 while (diff
<= max_digit
) {
1336 int value
= hex_to_bin(**cp
);
1340 *val
= *val
* 16 + value
;
1345 if ((diff
< min_digit
) || (diff
> max_digit
) || (*val
> max_val
))
1351 static int __init
parse_busid(char *str
, unsigned int *cssid
,
1352 unsigned int *ssid
, unsigned int *devno
)
1363 ret
= pure_hex(&str_work
, cssid
, 1, 2, __MAX_CSSID
);
1364 if (ret
|| (str_work
[0] != '.'))
1367 ret
= pure_hex(&str_work
, ssid
, 1, 1, __MAX_SSID
);
1368 if (ret
|| (str_work
[0] != '.'))
1371 ret
= pure_hex(&str_work
, devno
, 4, 4, __MAX_SUBCHANNEL
);
1372 if (ret
|| (str_work
[0] != '\0'))
1380 static void __init
no_auto_parse(void)
1382 unsigned int from_cssid
, to_cssid
, from_ssid
, to_ssid
, from
, to
;
1387 while ((parm
= strsep(&str
, ","))) {
1388 rc
= parse_busid(strsep(&parm
, "-"), &from_cssid
,
1393 rc
= parse_busid(parm
, &to_cssid
,
1395 if ((from_ssid
> to_ssid
) ||
1396 ((from_ssid
== to_ssid
) && (from
> to
)))
1399 to_cssid
= from_cssid
;
1400 to_ssid
= from_ssid
;
1405 while ((from_ssid
< to_ssid
) ||
1406 ((from_ssid
== to_ssid
) && (from
<= to
))) {
1407 set_bit(from
, devs_no_auto
[from_ssid
]);
1409 if (from
> __MAX_SUBCHANNEL
) {
1417 static int __init
virtio_ccw_init(void)
1419 /* parse no_auto string before we do anything further */
1421 return ccw_driver_register(&virtio_ccw_driver
);
1423 device_initcall(virtio_ccw_init
);