1 // SPDX-License-Identifier: GPL-2.0
3 * ccw based virtio transport
5 * Copyright IBM Corp. 2012, 2014
7 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
10 #include <linux/kernel_stat.h>
11 #include <linux/init.h>
12 #include <linux/bootmem.h>
13 #include <linux/err.h>
14 #include <linux/virtio.h>
15 #include <linux/virtio_config.h>
16 #include <linux/slab.h>
17 #include <linux/interrupt.h>
18 #include <linux/virtio_ring.h>
19 #include <linux/pfn.h>
20 #include <linux/async.h>
21 #include <linux/wait.h>
22 #include <linux/list.h>
23 #include <linux/bitops.h>
24 #include <linux/moduleparam.h>
26 #include <linux/kvm_para.h>
27 #include <linux/notifier.h>
29 #include <asm/setup.h>
32 #include <asm/ccwdev.h>
33 #include <asm/virtio-ccw.h>
38 * virtio related functions
41 struct vq_config_block
{
46 #define VIRTIO_CCW_CONFIG_SIZE 0x100
47 /* same as PCI config space size, should be enough for all drivers */
49 struct virtio_ccw_device
{
50 struct virtio_device vdev
;
52 __u8 config
[VIRTIO_CCW_CONFIG_SIZE
];
53 struct ccw_device
*cdev
;
56 unsigned int revision
; /* Transport revision */
57 wait_queue_head_t wait_q
;
59 struct list_head virtqueues
;
60 unsigned long indicators
;
61 unsigned long indicators2
;
62 struct vq_config_block
*config_block
;
66 unsigned int config_ready
;
70 struct vq_info_block_legacy
{
77 struct vq_info_block
{
86 struct virtio_feature_desc
{
91 struct virtio_thinint_area
{
92 unsigned long summary_indicator
;
93 unsigned long indicator
;
98 struct virtio_rev_info
{
104 /* the highest virtio-ccw revision we support */
105 #define VIRTIO_CCW_REV_MAX 1
107 struct virtio_ccw_vq_info
{
108 struct virtqueue
*vq
;
112 struct vq_info_block s
;
113 struct vq_info_block_legacy l
;
116 struct list_head node
;
120 #define VIRTIO_AIRQ_ISC IO_SCH_ISC /* inherit from subchannel */
122 #define VIRTIO_IV_BITS (L1_CACHE_BYTES * 8)
123 #define MAX_AIRQ_AREAS 20
125 static int virtio_ccw_use_airq
= 1;
129 u8 summary_indicator
;
130 struct airq_struct airq
;
133 static struct airq_info
*airq_areas
[MAX_AIRQ_AREAS
];
135 #define CCW_CMD_SET_VQ 0x13
136 #define CCW_CMD_VDEV_RESET 0x33
137 #define CCW_CMD_SET_IND 0x43
138 #define CCW_CMD_SET_CONF_IND 0x53
139 #define CCW_CMD_READ_FEAT 0x12
140 #define CCW_CMD_WRITE_FEAT 0x11
141 #define CCW_CMD_READ_CONF 0x22
142 #define CCW_CMD_WRITE_CONF 0x21
143 #define CCW_CMD_WRITE_STATUS 0x31
144 #define CCW_CMD_READ_VQ_CONF 0x32
145 #define CCW_CMD_READ_STATUS 0x72
146 #define CCW_CMD_SET_IND_ADAPTER 0x73
147 #define CCW_CMD_SET_VIRTIO_REV 0x83
149 #define VIRTIO_CCW_DOING_SET_VQ 0x00010000
150 #define VIRTIO_CCW_DOING_RESET 0x00040000
151 #define VIRTIO_CCW_DOING_READ_FEAT 0x00080000
152 #define VIRTIO_CCW_DOING_WRITE_FEAT 0x00100000
153 #define VIRTIO_CCW_DOING_READ_CONFIG 0x00200000
154 #define VIRTIO_CCW_DOING_WRITE_CONFIG 0x00400000
155 #define VIRTIO_CCW_DOING_WRITE_STATUS 0x00800000
156 #define VIRTIO_CCW_DOING_SET_IND 0x01000000
157 #define VIRTIO_CCW_DOING_READ_VQ_CONF 0x02000000
158 #define VIRTIO_CCW_DOING_SET_CONF_IND 0x04000000
159 #define VIRTIO_CCW_DOING_SET_IND_ADAPTER 0x08000000
160 #define VIRTIO_CCW_DOING_SET_VIRTIO_REV 0x10000000
161 #define VIRTIO_CCW_DOING_READ_STATUS 0x20000000
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 unsigned long get_airq_indicator(struct virtqueue
*vqs
[], int nvqs
,
238 u64
*first
, void **airq_info
)
241 struct airq_info
*info
;
242 unsigned long indicator_addr
= 0;
243 unsigned long bit
, flags
;
245 for (i
= 0; i
< MAX_AIRQ_AREAS
&& !indicator_addr
; i
++) {
247 airq_areas
[i
] = new_airq_info();
248 info
= airq_areas
[i
];
251 write_lock_irqsave(&info
->lock
, flags
);
252 bit
= airq_iv_alloc(info
->aiv
, nvqs
);
254 /* Not enough vacancies. */
255 write_unlock_irqrestore(&info
->lock
, flags
);
260 indicator_addr
= (unsigned long)info
->aiv
->vector
;
261 for (j
= 0; j
< nvqs
; j
++) {
262 airq_iv_set_ptr(info
->aiv
, bit
+ j
,
263 (unsigned long)vqs
[j
]);
265 write_unlock_irqrestore(&info
->lock
, flags
);
267 return indicator_addr
;
270 static void virtio_ccw_drop_indicators(struct virtio_ccw_device
*vcdev
)
272 struct virtio_ccw_vq_info
*info
;
274 list_for_each_entry(info
, &vcdev
->virtqueues
, node
)
275 drop_airq_indicator(info
->vq
, vcdev
->airq_info
);
278 static int doing_io(struct virtio_ccw_device
*vcdev
, __u32 flag
)
283 spin_lock_irqsave(get_ccwdev_lock(vcdev
->cdev
), flags
);
287 ret
= vcdev
->curr_io
& flag
;
288 spin_unlock_irqrestore(get_ccwdev_lock(vcdev
->cdev
), flags
);
292 static int ccw_io_helper(struct virtio_ccw_device
*vcdev
,
293 struct ccw1
*ccw
, __u32 intparm
)
297 int flag
= intparm
& VIRTIO_CCW_INTPARM_MASK
;
300 spin_lock_irqsave(get_ccwdev_lock(vcdev
->cdev
), flags
);
301 ret
= ccw_device_start(vcdev
->cdev
, ccw
, intparm
, 0, 0);
305 vcdev
->curr_io
|= flag
;
307 spin_unlock_irqrestore(get_ccwdev_lock(vcdev
->cdev
), flags
);
309 } while (ret
== -EBUSY
);
310 wait_event(vcdev
->wait_q
, doing_io(vcdev
, flag
) == 0);
311 return ret
? ret
: vcdev
->err
;
314 static void virtio_ccw_drop_indicator(struct virtio_ccw_device
*vcdev
,
318 unsigned long *indicatorp
= NULL
;
319 struct virtio_thinint_area
*thinint_area
= NULL
;
320 struct airq_info
*airq_info
= vcdev
->airq_info
;
322 if (vcdev
->is_thinint
) {
323 thinint_area
= kzalloc(sizeof(*thinint_area
),
324 GFP_DMA
| GFP_KERNEL
);
327 thinint_area
->summary_indicator
=
328 (unsigned long) &airq_info
->summary_indicator
;
329 thinint_area
->isc
= VIRTIO_AIRQ_ISC
;
330 ccw
->cmd_code
= CCW_CMD_SET_IND_ADAPTER
;
331 ccw
->count
= sizeof(*thinint_area
);
332 ccw
->cda
= (__u32
)(unsigned long) thinint_area
;
334 /* payload is the address of the indicators */
335 indicatorp
= kmalloc(sizeof(&vcdev
->indicators
),
336 GFP_DMA
| GFP_KERNEL
);
340 ccw
->cmd_code
= CCW_CMD_SET_IND
;
341 ccw
->count
= sizeof(&vcdev
->indicators
);
342 ccw
->cda
= (__u32
)(unsigned long) indicatorp
;
344 /* Deregister indicators from host. */
345 vcdev
->indicators
= 0;
347 ret
= ccw_io_helper(vcdev
, ccw
,
349 VIRTIO_CCW_DOING_SET_IND_ADAPTER
:
350 VIRTIO_CCW_DOING_SET_IND
);
351 if (ret
&& (ret
!= -ENODEV
))
352 dev_info(&vcdev
->cdev
->dev
,
353 "Failed to deregister indicators (%d)\n", ret
);
354 else if (vcdev
->is_thinint
)
355 virtio_ccw_drop_indicators(vcdev
);
360 static inline long __do_kvm_notify(struct subchannel_id schid
,
361 unsigned long queue_index
,
364 register unsigned long __nr
asm("1") = KVM_S390_VIRTIO_CCW_NOTIFY
;
365 register struct subchannel_id __schid
asm("2") = schid
;
366 register unsigned long __index
asm("3") = queue_index
;
367 register long __rc
asm("2");
368 register long __cookie
asm("4") = cookie
;
370 asm volatile ("diag 2,4,0x500\n"
371 : "=d" (__rc
) : "d" (__nr
), "d" (__schid
), "d" (__index
),
377 static inline long do_kvm_notify(struct subchannel_id schid
,
378 unsigned long queue_index
,
381 diag_stat_inc(DIAG_STAT_X500
);
382 return __do_kvm_notify(schid
, queue_index
, cookie
);
385 static bool virtio_ccw_kvm_notify(struct virtqueue
*vq
)
387 struct virtio_ccw_vq_info
*info
= vq
->priv
;
388 struct virtio_ccw_device
*vcdev
;
389 struct subchannel_id schid
;
391 vcdev
= to_vc_device(info
->vq
->vdev
);
392 ccw_device_get_schid(vcdev
->cdev
, &schid
);
393 info
->cookie
= do_kvm_notify(schid
, vq
->index
, info
->cookie
);
394 if (info
->cookie
< 0)
399 static int virtio_ccw_read_vq_conf(struct virtio_ccw_device
*vcdev
,
400 struct ccw1
*ccw
, int index
)
404 vcdev
->config_block
->index
= index
;
405 ccw
->cmd_code
= CCW_CMD_READ_VQ_CONF
;
407 ccw
->count
= sizeof(struct vq_config_block
);
408 ccw
->cda
= (__u32
)(unsigned long)(vcdev
->config_block
);
409 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_READ_VQ_CONF
);
412 return vcdev
->config_block
->num
;
415 static void virtio_ccw_del_vq(struct virtqueue
*vq
, struct ccw1
*ccw
)
417 struct virtio_ccw_device
*vcdev
= to_vc_device(vq
->vdev
);
418 struct virtio_ccw_vq_info
*info
= vq
->priv
;
422 unsigned int index
= vq
->index
;
424 /* Remove from our list. */
425 spin_lock_irqsave(&vcdev
->lock
, flags
);
426 list_del(&info
->node
);
427 spin_unlock_irqrestore(&vcdev
->lock
, flags
);
429 /* Release from host. */
430 if (vcdev
->revision
== 0) {
431 info
->info_block
->l
.queue
= 0;
432 info
->info_block
->l
.align
= 0;
433 info
->info_block
->l
.index
= index
;
434 info
->info_block
->l
.num
= 0;
435 ccw
->count
= sizeof(info
->info_block
->l
);
437 info
->info_block
->s
.desc
= 0;
438 info
->info_block
->s
.index
= index
;
439 info
->info_block
->s
.num
= 0;
440 info
->info_block
->s
.avail
= 0;
441 info
->info_block
->s
.used
= 0;
442 ccw
->count
= sizeof(info
->info_block
->s
);
444 ccw
->cmd_code
= CCW_CMD_SET_VQ
;
446 ccw
->cda
= (__u32
)(unsigned long)(info
->info_block
);
447 ret
= ccw_io_helper(vcdev
, ccw
,
448 VIRTIO_CCW_DOING_SET_VQ
| index
);
450 * -ENODEV isn't considered an error: The device is gone anyway.
451 * This may happen on device detach.
453 if (ret
&& (ret
!= -ENODEV
))
454 dev_warn(&vq
->vdev
->dev
, "Error %d while deleting queue %d\n",
457 vring_del_virtqueue(vq
);
458 size
= PAGE_ALIGN(vring_size(info
->num
, KVM_VIRTIO_CCW_RING_ALIGN
));
459 free_pages_exact(info
->queue
, size
);
460 kfree(info
->info_block
);
464 static void virtio_ccw_del_vqs(struct virtio_device
*vdev
)
466 struct virtqueue
*vq
, *n
;
468 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
470 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
474 virtio_ccw_drop_indicator(vcdev
, ccw
);
476 list_for_each_entry_safe(vq
, n
, &vdev
->vqs
, list
)
477 virtio_ccw_del_vq(vq
, ccw
);
482 static struct virtqueue
*virtio_ccw_setup_vq(struct virtio_device
*vdev
,
483 int i
, vq_callback_t
*callback
,
484 const char *name
, bool ctx
,
487 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
489 struct virtqueue
*vq
= NULL
;
490 struct virtio_ccw_vq_info
*info
;
491 unsigned long size
= 0; /* silence the compiler */
494 /* Allocate queue. */
495 info
= kzalloc(sizeof(struct virtio_ccw_vq_info
), GFP_KERNEL
);
497 dev_warn(&vcdev
->cdev
->dev
, "no info\n");
501 info
->info_block
= kzalloc(sizeof(*info
->info_block
),
502 GFP_DMA
| GFP_KERNEL
);
503 if (!info
->info_block
) {
504 dev_warn(&vcdev
->cdev
->dev
, "no info block\n");
508 info
->num
= virtio_ccw_read_vq_conf(vcdev
, ccw
, i
);
513 size
= PAGE_ALIGN(vring_size(info
->num
, KVM_VIRTIO_CCW_RING_ALIGN
));
514 info
->queue
= alloc_pages_exact(size
, GFP_KERNEL
| __GFP_ZERO
);
515 if (info
->queue
== NULL
) {
516 dev_warn(&vcdev
->cdev
->dev
, "no queue\n");
521 vq
= vring_new_virtqueue(i
, info
->num
, KVM_VIRTIO_CCW_RING_ALIGN
, vdev
,
522 true, ctx
, info
->queue
, virtio_ccw_kvm_notify
,
525 /* For now, we fail if we can't get the requested size. */
526 dev_warn(&vcdev
->cdev
->dev
, "no vq\n");
531 /* Register it with the host. */
532 if (vcdev
->revision
== 0) {
533 info
->info_block
->l
.queue
= (__u64
)info
->queue
;
534 info
->info_block
->l
.align
= KVM_VIRTIO_CCW_RING_ALIGN
;
535 info
->info_block
->l
.index
= i
;
536 info
->info_block
->l
.num
= info
->num
;
537 ccw
->count
= sizeof(info
->info_block
->l
);
539 info
->info_block
->s
.desc
= (__u64
)info
->queue
;
540 info
->info_block
->s
.index
= i
;
541 info
->info_block
->s
.num
= info
->num
;
542 info
->info_block
->s
.avail
= (__u64
)virtqueue_get_avail(vq
);
543 info
->info_block
->s
.used
= (__u64
)virtqueue_get_used(vq
);
544 ccw
->count
= sizeof(info
->info_block
->s
);
546 ccw
->cmd_code
= CCW_CMD_SET_VQ
;
548 ccw
->cda
= (__u32
)(unsigned long)(info
->info_block
);
549 err
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_SET_VQ
| i
);
551 dev_warn(&vcdev
->cdev
->dev
, "SET_VQ failed\n");
558 /* Save it to our list. */
559 spin_lock_irqsave(&vcdev
->lock
, flags
);
560 list_add(&info
->node
, &vcdev
->virtqueues
);
561 spin_unlock_irqrestore(&vcdev
->lock
, flags
);
567 vring_del_virtqueue(vq
);
570 free_pages_exact(info
->queue
, size
);
571 kfree(info
->info_block
);
577 static int virtio_ccw_register_adapter_ind(struct virtio_ccw_device
*vcdev
,
578 struct virtqueue
*vqs
[], int nvqs
,
582 struct virtio_thinint_area
*thinint_area
= NULL
;
583 struct airq_info
*info
;
585 thinint_area
= kzalloc(sizeof(*thinint_area
), GFP_DMA
| GFP_KERNEL
);
590 /* Try to get an indicator. */
591 thinint_area
->indicator
= get_airq_indicator(vqs
, nvqs
,
592 &thinint_area
->bit_nr
,
594 if (!thinint_area
->indicator
) {
598 info
= vcdev
->airq_info
;
599 thinint_area
->summary_indicator
=
600 (unsigned long) &info
->summary_indicator
;
601 thinint_area
->isc
= VIRTIO_AIRQ_ISC
;
602 ccw
->cmd_code
= CCW_CMD_SET_IND_ADAPTER
;
603 ccw
->flags
= CCW_FLAG_SLI
;
604 ccw
->count
= sizeof(*thinint_area
);
605 ccw
->cda
= (__u32
)(unsigned long)thinint_area
;
606 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_SET_IND_ADAPTER
);
608 if (ret
== -EOPNOTSUPP
) {
610 * The host does not support adapter interrupts
611 * for virtio-ccw, stop trying.
613 virtio_ccw_use_airq
= 0;
614 pr_info("Adapter interrupts unsupported on host\n");
616 dev_warn(&vcdev
->cdev
->dev
,
617 "enabling adapter interrupts = %d\n", ret
);
618 virtio_ccw_drop_indicators(vcdev
);
625 static int virtio_ccw_find_vqs(struct virtio_device
*vdev
, unsigned nvqs
,
626 struct virtqueue
*vqs
[],
627 vq_callback_t
*callbacks
[],
628 const char * const names
[],
630 struct irq_affinity
*desc
)
632 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
633 unsigned long *indicatorp
= NULL
;
637 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
641 for (i
= 0; i
< nvqs
; ++i
) {
642 vqs
[i
] = virtio_ccw_setup_vq(vdev
, i
, callbacks
[i
], names
[i
],
643 ctx
? ctx
[i
] : false, ccw
);
644 if (IS_ERR(vqs
[i
])) {
645 ret
= PTR_ERR(vqs
[i
]);
652 * We need a data area under 2G to communicate. Our payload is
653 * the address of the indicators.
655 indicatorp
= kmalloc(sizeof(&vcdev
->indicators
), GFP_DMA
| GFP_KERNEL
);
658 *indicatorp
= (unsigned long) &vcdev
->indicators
;
659 if (vcdev
->is_thinint
) {
660 ret
= virtio_ccw_register_adapter_ind(vcdev
, vqs
, nvqs
, ccw
);
662 /* no error, just fall back to legacy interrupts */
663 vcdev
->is_thinint
= false;
665 if (!vcdev
->is_thinint
) {
666 /* Register queue indicators with host. */
667 vcdev
->indicators
= 0;
668 ccw
->cmd_code
= CCW_CMD_SET_IND
;
670 ccw
->count
= sizeof(&vcdev
->indicators
);
671 ccw
->cda
= (__u32
)(unsigned long) indicatorp
;
672 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_SET_IND
);
676 /* Register indicators2 with host for config changes */
677 *indicatorp
= (unsigned long) &vcdev
->indicators2
;
678 vcdev
->indicators2
= 0;
679 ccw
->cmd_code
= CCW_CMD_SET_CONF_IND
;
681 ccw
->count
= sizeof(&vcdev
->indicators2
);
682 ccw
->cda
= (__u32
)(unsigned long) indicatorp
;
683 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_SET_CONF_IND
);
693 virtio_ccw_del_vqs(vdev
);
697 static void virtio_ccw_reset(struct virtio_device
*vdev
)
699 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
702 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
706 /* Zero status bits. */
709 /* Send a reset ccw on device. */
710 ccw
->cmd_code
= CCW_CMD_VDEV_RESET
;
714 ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_RESET
);
718 static u64
virtio_ccw_get_features(struct virtio_device
*vdev
)
720 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
721 struct virtio_feature_desc
*features
;
726 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
730 features
= kzalloc(sizeof(*features
), GFP_DMA
| GFP_KERNEL
);
735 /* Read the feature bits from the host. */
737 ccw
->cmd_code
= CCW_CMD_READ_FEAT
;
739 ccw
->count
= sizeof(*features
);
740 ccw
->cda
= (__u32
)(unsigned long)features
;
741 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_READ_FEAT
);
747 rc
= le32_to_cpu(features
->features
);
749 if (vcdev
->revision
== 0)
752 /* Read second half of the feature bits from the host. */
754 ccw
->cmd_code
= CCW_CMD_READ_FEAT
;
756 ccw
->count
= sizeof(*features
);
757 ccw
->cda
= (__u32
)(unsigned long)features
;
758 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_READ_FEAT
);
760 rc
|= (u64
)le32_to_cpu(features
->features
) << 32;
768 static int virtio_ccw_finalize_features(struct virtio_device
*vdev
)
770 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
771 struct virtio_feature_desc
*features
;
775 if (vcdev
->revision
>= 1 &&
776 !__virtio_test_bit(vdev
, VIRTIO_F_VERSION_1
)) {
777 dev_err(&vdev
->dev
, "virtio: device uses revision 1 "
778 "but does not have VIRTIO_F_VERSION_1\n");
782 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
786 features
= kzalloc(sizeof(*features
), GFP_DMA
| GFP_KERNEL
);
791 /* Give virtio_ring a chance to accept features. */
792 vring_transport_features(vdev
);
795 features
->features
= cpu_to_le32((u32
)vdev
->features
);
796 /* Write the first half of the feature bits to the host. */
797 ccw
->cmd_code
= CCW_CMD_WRITE_FEAT
;
799 ccw
->count
= sizeof(*features
);
800 ccw
->cda
= (__u32
)(unsigned long)features
;
801 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_WRITE_FEAT
);
805 if (vcdev
->revision
== 0)
809 features
->features
= cpu_to_le32(vdev
->features
>> 32);
810 /* Write the second half of the feature bits to the host. */
811 ccw
->cmd_code
= CCW_CMD_WRITE_FEAT
;
813 ccw
->count
= sizeof(*features
);
814 ccw
->cda
= (__u32
)(unsigned long)features
;
815 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_WRITE_FEAT
);
824 static void virtio_ccw_get_config(struct virtio_device
*vdev
,
825 unsigned int offset
, void *buf
, unsigned len
)
827 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
832 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
836 config_area
= kzalloc(VIRTIO_CCW_CONFIG_SIZE
, GFP_DMA
| GFP_KERNEL
);
840 /* Read the config area from the host. */
841 ccw
->cmd_code
= CCW_CMD_READ_CONF
;
843 ccw
->count
= offset
+ len
;
844 ccw
->cda
= (__u32
)(unsigned long)config_area
;
845 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_READ_CONFIG
);
849 memcpy(vcdev
->config
, config_area
, offset
+ len
);
851 memcpy(buf
, &vcdev
->config
[offset
], len
);
852 if (vcdev
->config_ready
< offset
+ len
)
853 vcdev
->config_ready
= offset
+ len
;
860 static void virtio_ccw_set_config(struct virtio_device
*vdev
,
861 unsigned int offset
, const void *buf
,
864 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
868 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
872 config_area
= kzalloc(VIRTIO_CCW_CONFIG_SIZE
, GFP_DMA
| GFP_KERNEL
);
876 /* Make sure we don't overwrite fields. */
877 if (vcdev
->config_ready
< offset
)
878 virtio_ccw_get_config(vdev
, 0, NULL
, offset
);
879 memcpy(&vcdev
->config
[offset
], buf
, len
);
880 /* Write the config area to the host. */
881 memcpy(config_area
, vcdev
->config
, sizeof(vcdev
->config
));
882 ccw
->cmd_code
= CCW_CMD_WRITE_CONF
;
884 ccw
->count
= offset
+ len
;
885 ccw
->cda
= (__u32
)(unsigned long)config_area
;
886 ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_WRITE_CONFIG
);
893 static u8
virtio_ccw_get_status(struct virtio_device
*vdev
)
895 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
896 u8 old_status
= *vcdev
->status
;
899 if (vcdev
->revision
< 1)
900 return *vcdev
->status
;
902 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
906 ccw
->cmd_code
= CCW_CMD_READ_STATUS
;
908 ccw
->count
= sizeof(*vcdev
->status
);
909 ccw
->cda
= (__u32
)(unsigned long)vcdev
->status
;
910 ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_READ_STATUS
);
912 * If the channel program failed (should only happen if the device
913 * was hotunplugged, and then we clean up via the machine check
914 * handler anyway), vcdev->status was not overwritten and we just
915 * return the old status, which is fine.
919 return *vcdev
->status
;
922 static void virtio_ccw_set_status(struct virtio_device
*vdev
, u8 status
)
924 struct virtio_ccw_device
*vcdev
= to_vc_device(vdev
);
925 u8 old_status
= *vcdev
->status
;
929 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
933 /* Write the status to the host. */
934 *vcdev
->status
= status
;
935 ccw
->cmd_code
= CCW_CMD_WRITE_STATUS
;
937 ccw
->count
= sizeof(status
);
938 ccw
->cda
= (__u32
)(unsigned long)vcdev
->status
;
939 ret
= ccw_io_helper(vcdev
, ccw
, VIRTIO_CCW_DOING_WRITE_STATUS
);
940 /* Write failed? We assume status is unchanged. */
942 *vcdev
->status
= old_status
;
946 static const struct virtio_config_ops virtio_ccw_config_ops
= {
947 .get_features
= virtio_ccw_get_features
,
948 .finalize_features
= virtio_ccw_finalize_features
,
949 .get
= virtio_ccw_get_config
,
950 .set
= virtio_ccw_set_config
,
951 .get_status
= virtio_ccw_get_status
,
952 .set_status
= virtio_ccw_set_status
,
953 .reset
= virtio_ccw_reset
,
954 .find_vqs
= virtio_ccw_find_vqs
,
955 .del_vqs
= virtio_ccw_del_vqs
,
960 * ccw bus driver related functions
963 static void virtio_ccw_release_dev(struct device
*_d
)
965 struct virtio_device
*dev
= dev_to_virtio(_d
);
966 struct virtio_ccw_device
*vcdev
= to_vc_device(dev
);
968 kfree(vcdev
->status
);
969 kfree(vcdev
->config_block
);
973 static int irb_is_error(struct irb
*irb
)
975 if (scsw_cstat(&irb
->scsw
) != 0)
977 if (scsw_dstat(&irb
->scsw
) & ~(DEV_STAT_CHN_END
| DEV_STAT_DEV_END
))
979 if (scsw_cc(&irb
->scsw
) != 0)
984 static struct virtqueue
*virtio_ccw_vq_by_ind(struct virtio_ccw_device
*vcdev
,
987 struct virtio_ccw_vq_info
*info
;
989 struct virtqueue
*vq
;
992 spin_lock_irqsave(&vcdev
->lock
, flags
);
993 list_for_each_entry(info
, &vcdev
->virtqueues
, node
) {
994 if (info
->vq
->index
== index
) {
999 spin_unlock_irqrestore(&vcdev
->lock
, flags
);
1003 static void virtio_ccw_check_activity(struct virtio_ccw_device
*vcdev
,
1006 if (vcdev
->curr_io
& activity
) {
1008 case VIRTIO_CCW_DOING_READ_FEAT
:
1009 case VIRTIO_CCW_DOING_WRITE_FEAT
:
1010 case VIRTIO_CCW_DOING_READ_CONFIG
:
1011 case VIRTIO_CCW_DOING_WRITE_CONFIG
:
1012 case VIRTIO_CCW_DOING_WRITE_STATUS
:
1013 case VIRTIO_CCW_DOING_READ_STATUS
:
1014 case VIRTIO_CCW_DOING_SET_VQ
:
1015 case VIRTIO_CCW_DOING_SET_IND
:
1016 case VIRTIO_CCW_DOING_SET_CONF_IND
:
1017 case VIRTIO_CCW_DOING_RESET
:
1018 case VIRTIO_CCW_DOING_READ_VQ_CONF
:
1019 case VIRTIO_CCW_DOING_SET_IND_ADAPTER
:
1020 case VIRTIO_CCW_DOING_SET_VIRTIO_REV
:
1021 vcdev
->curr_io
&= ~activity
;
1022 wake_up(&vcdev
->wait_q
);
1025 /* don't know what to do... */
1026 dev_warn(&vcdev
->cdev
->dev
,
1027 "Suspicious activity '%08x'\n", activity
);
1034 static void virtio_ccw_int_handler(struct ccw_device
*cdev
,
1035 unsigned long intparm
,
1038 __u32 activity
= intparm
& VIRTIO_CCW_INTPARM_MASK
;
1039 struct virtio_ccw_device
*vcdev
= dev_get_drvdata(&cdev
->dev
);
1041 struct virtqueue
*vq
;
1046 vcdev
->err
= PTR_ERR(irb
);
1047 virtio_ccw_check_activity(vcdev
, activity
);
1048 /* Don't poke around indicators, something's wrong. */
1051 /* Check if it's a notification from the host. */
1052 if ((intparm
== 0) &&
1053 (scsw_stctl(&irb
->scsw
) ==
1054 (SCSW_STCTL_ALERT_STATUS
| SCSW_STCTL_STATUS_PEND
))) {
1057 if (irb_is_error(irb
)) {
1058 /* Command reject? */
1059 if ((scsw_dstat(&irb
->scsw
) & DEV_STAT_UNIT_CHECK
) &&
1060 (irb
->ecw
[0] & SNS0_CMD_REJECT
))
1061 vcdev
->err
= -EOPNOTSUPP
;
1063 /* Map everything else to -EIO. */
1066 virtio_ccw_check_activity(vcdev
, activity
);
1067 for_each_set_bit(i
, &vcdev
->indicators
,
1068 sizeof(vcdev
->indicators
) * BITS_PER_BYTE
) {
1069 /* The bit clear must happen before the vring kick. */
1070 clear_bit(i
, &vcdev
->indicators
);
1072 vq
= virtio_ccw_vq_by_ind(vcdev
, i
);
1073 vring_interrupt(0, vq
);
1075 if (test_bit(0, &vcdev
->indicators2
)) {
1076 virtio_config_changed(&vcdev
->vdev
);
1077 clear_bit(0, &vcdev
->indicators2
);
1082 * We usually want to autoonline all devices, but give the admin
1083 * a way to exempt devices from this.
1085 #define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
1087 static unsigned long devs_no_auto
[__MAX_SSID
+ 1][__DEV_WORDS
];
1089 static char *no_auto
= "";
1091 module_param(no_auto
, charp
, 0444);
1092 MODULE_PARM_DESC(no_auto
, "list of ccw bus id ranges not to be auto-onlined");
1094 static int virtio_ccw_check_autoonline(struct ccw_device
*cdev
)
1096 struct ccw_dev_id id
;
1098 ccw_device_get_id(cdev
, &id
);
1099 if (test_bit(id
.devno
, devs_no_auto
[id
.ssid
]))
1104 static void virtio_ccw_auto_online(void *data
, async_cookie_t cookie
)
1106 struct ccw_device
*cdev
= data
;
1109 ret
= ccw_device_set_online(cdev
);
1111 dev_warn(&cdev
->dev
, "Failed to set online: %d\n", ret
);
1114 static int virtio_ccw_probe(struct ccw_device
*cdev
)
1116 cdev
->handler
= virtio_ccw_int_handler
;
1118 if (virtio_ccw_check_autoonline(cdev
))
1119 async_schedule(virtio_ccw_auto_online
, cdev
);
1123 static struct virtio_ccw_device
*virtio_grab_drvdata(struct ccw_device
*cdev
)
1125 unsigned long flags
;
1126 struct virtio_ccw_device
*vcdev
;
1128 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1129 vcdev
= dev_get_drvdata(&cdev
->dev
);
1130 if (!vcdev
|| vcdev
->going_away
) {
1131 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1134 vcdev
->going_away
= true;
1135 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1139 static void virtio_ccw_remove(struct ccw_device
*cdev
)
1141 unsigned long flags
;
1142 struct virtio_ccw_device
*vcdev
= virtio_grab_drvdata(cdev
);
1144 if (vcdev
&& cdev
->online
) {
1145 if (vcdev
->device_lost
)
1146 virtio_break_device(&vcdev
->vdev
);
1147 unregister_virtio_device(&vcdev
->vdev
);
1148 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1149 dev_set_drvdata(&cdev
->dev
, NULL
);
1150 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1152 cdev
->handler
= NULL
;
1155 static int virtio_ccw_offline(struct ccw_device
*cdev
)
1157 unsigned long flags
;
1158 struct virtio_ccw_device
*vcdev
= virtio_grab_drvdata(cdev
);
1162 if (vcdev
->device_lost
)
1163 virtio_break_device(&vcdev
->vdev
);
1164 unregister_virtio_device(&vcdev
->vdev
);
1165 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1166 dev_set_drvdata(&cdev
->dev
, NULL
);
1167 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1171 static int virtio_ccw_set_transport_rev(struct virtio_ccw_device
*vcdev
)
1173 struct virtio_rev_info
*rev
;
1177 ccw
= kzalloc(sizeof(*ccw
), GFP_DMA
| GFP_KERNEL
);
1180 rev
= kzalloc(sizeof(*rev
), GFP_DMA
| GFP_KERNEL
);
1186 /* Set transport revision */
1187 ccw
->cmd_code
= CCW_CMD_SET_VIRTIO_REV
;
1189 ccw
->count
= sizeof(*rev
);
1190 ccw
->cda
= (__u32
)(unsigned long)rev
;
1192 vcdev
->revision
= VIRTIO_CCW_REV_MAX
;
1194 rev
->revision
= vcdev
->revision
;
1195 /* none of our supported revisions carry payload */
1197 ret
= ccw_io_helper(vcdev
, ccw
,
1198 VIRTIO_CCW_DOING_SET_VIRTIO_REV
);
1199 if (ret
== -EOPNOTSUPP
) {
1200 if (vcdev
->revision
== 0)
1202 * The host device does not support setting
1203 * the revision: let's operate it in legacy
1210 } while (ret
== -EOPNOTSUPP
);
1217 static int virtio_ccw_online(struct ccw_device
*cdev
)
1220 struct virtio_ccw_device
*vcdev
;
1221 unsigned long flags
;
1223 vcdev
= kzalloc(sizeof(*vcdev
), GFP_KERNEL
);
1225 dev_warn(&cdev
->dev
, "Could not get memory for virtio\n");
1229 vcdev
->config_block
= kzalloc(sizeof(*vcdev
->config_block
),
1230 GFP_DMA
| GFP_KERNEL
);
1231 if (!vcdev
->config_block
) {
1235 vcdev
->status
= kzalloc(sizeof(*vcdev
->status
), GFP_DMA
| GFP_KERNEL
);
1236 if (!vcdev
->status
) {
1241 vcdev
->is_thinint
= virtio_ccw_use_airq
; /* at least try */
1243 vcdev
->vdev
.dev
.parent
= &cdev
->dev
;
1244 vcdev
->vdev
.dev
.release
= virtio_ccw_release_dev
;
1245 vcdev
->vdev
.config
= &virtio_ccw_config_ops
;
1247 init_waitqueue_head(&vcdev
->wait_q
);
1248 INIT_LIST_HEAD(&vcdev
->virtqueues
);
1249 spin_lock_init(&vcdev
->lock
);
1251 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1252 dev_set_drvdata(&cdev
->dev
, vcdev
);
1253 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1254 vcdev
->vdev
.id
.vendor
= cdev
->id
.cu_type
;
1255 vcdev
->vdev
.id
.device
= cdev
->id
.cu_model
;
1257 ret
= virtio_ccw_set_transport_rev(vcdev
);
1261 ret
= register_virtio_device(&vcdev
->vdev
);
1263 dev_warn(&cdev
->dev
, "Failed to register virtio device: %d\n",
1269 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1270 dev_set_drvdata(&cdev
->dev
, NULL
);
1271 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1272 put_device(&vcdev
->vdev
.dev
);
1276 kfree(vcdev
->status
);
1277 kfree(vcdev
->config_block
);
1283 static int virtio_ccw_cio_notify(struct ccw_device
*cdev
, int event
)
1286 struct virtio_ccw_device
*vcdev
= dev_get_drvdata(&cdev
->dev
);
1289 * Make sure vcdev is set
1290 * i.e. set_offline/remove callback not already running
1297 vcdev
->device_lost
= true;
1310 static struct ccw_device_id virtio_ids
[] = {
1311 { CCW_DEVICE(0x3832, 0) },
1315 #ifdef CONFIG_PM_SLEEP
1316 static int virtio_ccw_freeze(struct ccw_device
*cdev
)
1318 struct virtio_ccw_device
*vcdev
= dev_get_drvdata(&cdev
->dev
);
1320 return virtio_device_freeze(&vcdev
->vdev
);
1323 static int virtio_ccw_restore(struct ccw_device
*cdev
)
1325 struct virtio_ccw_device
*vcdev
= dev_get_drvdata(&cdev
->dev
);
1328 ret
= virtio_ccw_set_transport_rev(vcdev
);
1332 return virtio_device_restore(&vcdev
->vdev
);
1336 static struct ccw_driver virtio_ccw_driver
= {
1338 .owner
= THIS_MODULE
,
1339 .name
= "virtio_ccw",
1342 .probe
= virtio_ccw_probe
,
1343 .remove
= virtio_ccw_remove
,
1344 .set_offline
= virtio_ccw_offline
,
1345 .set_online
= virtio_ccw_online
,
1346 .notify
= virtio_ccw_cio_notify
,
1347 .int_class
= IRQIO_VIR
,
1348 #ifdef CONFIG_PM_SLEEP
1349 .freeze
= virtio_ccw_freeze
,
1350 .thaw
= virtio_ccw_restore
,
1351 .restore
= virtio_ccw_restore
,
1355 static int __init
pure_hex(char **cp
, unsigned int *val
, int min_digit
,
1356 int max_digit
, int max_val
)
1363 while (diff
<= max_digit
) {
1364 int value
= hex_to_bin(**cp
);
1368 *val
= *val
* 16 + value
;
1373 if ((diff
< min_digit
) || (diff
> max_digit
) || (*val
> max_val
))
1379 static int __init
parse_busid(char *str
, unsigned int *cssid
,
1380 unsigned int *ssid
, unsigned int *devno
)
1391 ret
= pure_hex(&str_work
, cssid
, 1, 2, __MAX_CSSID
);
1392 if (ret
|| (str_work
[0] != '.'))
1395 ret
= pure_hex(&str_work
, ssid
, 1, 1, __MAX_SSID
);
1396 if (ret
|| (str_work
[0] != '.'))
1399 ret
= pure_hex(&str_work
, devno
, 4, 4, __MAX_SUBCHANNEL
);
1400 if (ret
|| (str_work
[0] != '\0'))
1408 static void __init
no_auto_parse(void)
1410 unsigned int from_cssid
, to_cssid
, from_ssid
, to_ssid
, from
, to
;
1415 while ((parm
= strsep(&str
, ","))) {
1416 rc
= parse_busid(strsep(&parm
, "-"), &from_cssid
,
1421 rc
= parse_busid(parm
, &to_cssid
,
1423 if ((from_ssid
> to_ssid
) ||
1424 ((from_ssid
== to_ssid
) && (from
> to
)))
1427 to_cssid
= from_cssid
;
1428 to_ssid
= from_ssid
;
1433 while ((from_ssid
< to_ssid
) ||
1434 ((from_ssid
== to_ssid
) && (from
<= to
))) {
1435 set_bit(from
, devs_no_auto
[from_ssid
]);
1437 if (from
> __MAX_SUBCHANNEL
) {
1445 static int __init
virtio_ccw_init(void)
1447 /* parse no_auto string before we do anything further */
1449 return ccw_driver_register(&virtio_ccw_driver
);
1451 device_initcall(virtio_ccw_init
);