1 // SPDX-License-Identifier: GPL-2.0
3 * VFIO based Physical Subchannel device driver
5 * Copyright IBM Corp. 2017
6 * Copyright Red Hat, Inc. 2019
8 * Author(s): Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com>
9 * Xiao Feng Ren <renxiaof@linux.vnet.ibm.com>
10 * Cornelia Huck <cohuck@redhat.com>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/mdev.h>
23 #include "vfio_ccw_private.h"
25 struct workqueue_struct
*vfio_ccw_work_q
;
26 struct kmem_cache
*vfio_ccw_io_region
;
27 struct kmem_cache
*vfio_ccw_cmd_region
;
28 struct kmem_cache
*vfio_ccw_schib_region
;
29 struct kmem_cache
*vfio_ccw_crw_region
;
31 debug_info_t
*vfio_ccw_debug_msg_id
;
32 debug_info_t
*vfio_ccw_debug_trace_id
;
37 int vfio_ccw_sch_quiesce(struct subchannel
*sch
)
39 struct vfio_ccw_parent
*parent
= dev_get_drvdata(&sch
->dev
);
40 struct vfio_ccw_private
*private = dev_get_drvdata(&parent
->dev
);
41 DECLARE_COMPLETION_ONSTACK(completion
);
45 * Probably an impossible situation, after being called through
46 * FSM callbacks. But in the event it did, register a warning
47 * and return as if things were fine.
49 if (WARN_ON(!private))
55 ret
= cio_cancel_halt_clear(sch
, &iretry
);
58 pr_err("vfio_ccw: could not quiesce subchannel 0.%x.%04x!\n",
59 sch
->schid
.ssid
, sch
->schid
.sch_no
);
64 * Flush all I/O and wait for
65 * cancel/halt/clear completion.
67 private->completion
= &completion
;
68 spin_unlock_irq(&sch
->lock
);
71 wait_for_completion_timeout(&completion
, 3*HZ
);
73 private->completion
= NULL
;
74 flush_workqueue(vfio_ccw_work_q
);
75 spin_lock_irq(&sch
->lock
);
76 ret
= cio_disable_subchannel(sch
);
77 } while (ret
== -EBUSY
);
82 void vfio_ccw_sch_io_todo(struct work_struct
*work
)
84 struct vfio_ccw_private
*private;
87 bool cp_is_finished
= false;
89 private = container_of(work
, struct vfio_ccw_private
, io_work
);
92 is_final
= !(scsw_actl(&irb
->scsw
) &
93 (SCSW_ACTL_DEVACT
| SCSW_ACTL_SCHACT
));
94 if (scsw_is_solicited(&irb
->scsw
)) {
95 cp_update_scsw(&private->cp
, &irb
->scsw
);
96 if (is_final
&& private->state
== VFIO_CCW_STATE_CP_PENDING
) {
97 cp_free(&private->cp
);
98 cp_is_finished
= true;
101 mutex_lock(&private->io_mutex
);
102 memcpy(private->io_region
->irb_area
, irb
, sizeof(*irb
));
103 mutex_unlock(&private->io_mutex
);
106 * Reset to IDLE only if processing of a channel program
107 * has finished. Do not overwrite a possible processing
108 * state if the interrupt was unsolicited, or if the final
109 * interrupt was for HSCH or CSCH.
112 private->state
= VFIO_CCW_STATE_IDLE
;
114 if (private->io_trigger
)
115 eventfd_signal(private->io_trigger
);
118 void vfio_ccw_crw_todo(struct work_struct
*work
)
120 struct vfio_ccw_private
*private;
122 private = container_of(work
, struct vfio_ccw_private
, crw_work
);
124 if (!list_empty(&private->crw
) && private->crw_trigger
)
125 eventfd_signal(private->crw_trigger
);
129 * Css driver callbacks
131 static void vfio_ccw_sch_irq(struct subchannel
*sch
)
133 struct vfio_ccw_parent
*parent
= dev_get_drvdata(&sch
->dev
);
134 struct vfio_ccw_private
*private = dev_get_drvdata(&parent
->dev
);
137 * The subchannel should still be disabled at this point,
138 * so an interrupt would be quite surprising. As with an
139 * interrupt while the FSM is closed, let's attempt to
140 * disable the subchannel again.
143 VFIO_CCW_MSG_EVENT(2, "sch %x.%x.%04x: unexpected interrupt\n",
144 sch
->schid
.cssid
, sch
->schid
.ssid
,
147 cio_disable_subchannel(sch
);
151 inc_irq_stat(IRQIO_CIO
);
152 vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_INTERRUPT
);
155 static void vfio_ccw_free_parent(struct device
*dev
)
157 struct vfio_ccw_parent
*parent
= container_of(dev
, struct vfio_ccw_parent
, dev
);
162 static int vfio_ccw_sch_probe(struct subchannel
*sch
)
164 struct pmcw
*pmcw
= &sch
->schib
.pmcw
;
165 struct vfio_ccw_parent
*parent
;
169 dev_warn(&sch
->dev
, "vfio: ccw: does not support QDIO: %s\n",
170 dev_name(&sch
->dev
));
174 parent
= kzalloc(struct_size(parent
, mdev_types
, 1), GFP_KERNEL
);
178 dev_set_name(&parent
->dev
, "parent");
179 parent
->dev
.parent
= &sch
->dev
;
180 parent
->dev
.release
= &vfio_ccw_free_parent
;
181 ret
= device_register(&parent
->dev
);
185 dev_set_drvdata(&sch
->dev
, parent
);
187 parent
->mdev_type
.sysfs_name
= "io";
188 parent
->mdev_type
.pretty_name
= "I/O subchannel (Non-QDIO)";
189 parent
->mdev_types
[0] = &parent
->mdev_type
;
190 ret
= mdev_register_parent(&parent
->parent
, &sch
->dev
,
191 &vfio_ccw_mdev_driver
,
192 parent
->mdev_types
, 1);
196 VFIO_CCW_MSG_EVENT(4, "bound to subchannel %x.%x.%04x\n",
197 sch
->schid
.cssid
, sch
->schid
.ssid
,
202 device_del(&parent
->dev
);
204 put_device(&parent
->dev
);
205 dev_set_drvdata(&sch
->dev
, NULL
);
209 static void vfio_ccw_sch_remove(struct subchannel
*sch
)
211 struct vfio_ccw_parent
*parent
= dev_get_drvdata(&sch
->dev
);
213 mdev_unregister_parent(&parent
->parent
);
215 device_unregister(&parent
->dev
);
216 dev_set_drvdata(&sch
->dev
, NULL
);
218 VFIO_CCW_MSG_EVENT(4, "unbound from subchannel %x.%x.%04x\n",
219 sch
->schid
.cssid
, sch
->schid
.ssid
,
223 static void vfio_ccw_sch_shutdown(struct subchannel
*sch
)
225 struct vfio_ccw_parent
*parent
= dev_get_drvdata(&sch
->dev
);
226 struct vfio_ccw_private
*private = dev_get_drvdata(&parent
->dev
);
231 vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_CLOSE
);
232 vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_NOT_OPER
);
236 * vfio_ccw_sch_event - process subchannel event
238 * @process: non-zero if function is called in process context
240 * An unspecified event occurred for this subchannel. Adjust data according
241 * to the current operational state of the subchannel. Return zero when the
242 * event has been handled sufficiently or -EAGAIN when this function should
243 * be called again in process context.
245 static int vfio_ccw_sch_event(struct subchannel
*sch
, int process
)
247 struct vfio_ccw_parent
*parent
= dev_get_drvdata(&sch
->dev
);
248 struct vfio_ccw_private
*private = dev_get_drvdata(&parent
->dev
);
252 spin_lock_irqsave(&sch
->lock
, flags
);
253 if (!device_is_registered(&sch
->dev
))
256 if (work_pending(&sch
->todo_work
))
261 if (cio_update_schib(sch
)) {
263 vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_NOT_OPER
);
267 spin_unlock_irqrestore(&sch
->lock
, flags
);
272 static void vfio_ccw_queue_crw(struct vfio_ccw_private
*private,
277 struct vfio_ccw_crw
*crw
;
280 * If unable to allocate a CRW, just drop the event and
281 * carry on. The guest will either see a later one or
282 * learn when it issues its own store subchannel.
284 crw
= kzalloc(sizeof(*crw
), GFP_ATOMIC
);
289 * Build the CRW based on the inputs given to us.
293 crw
->crw
.rsid
= rsid
;
295 list_add_tail(&crw
->next
, &private->crw
);
296 queue_work(vfio_ccw_work_q
, &private->crw_work
);
299 static int vfio_ccw_chp_event(struct subchannel
*sch
,
300 struct chp_link
*link
, int event
)
302 struct vfio_ccw_parent
*parent
= dev_get_drvdata(&sch
->dev
);
303 struct vfio_ccw_private
*private = dev_get_drvdata(&parent
->dev
);
304 int mask
= chp_ssd_get_mask(&sch
->ssd_info
, link
);
307 if (!private || !mask
)
310 trace_vfio_ccw_chp_event(sch
->schid
, mask
, event
);
311 VFIO_CCW_MSG_EVENT(2, "sch %x.%x.%04x: mask=0x%x event=%d\n",
313 sch
->schid
.ssid
, sch
->schid
.sch_no
,
316 if (cio_update_schib(sch
))
321 /* Path logically turned off */
324 if (sch
->schib
.pmcw
.lpum
& mask
)
325 cio_cancel_halt_clear(sch
, &retry
);
329 if (sch
->schib
.pmcw
.lpum
& mask
)
330 cio_cancel_halt_clear(sch
, &retry
);
331 vfio_ccw_queue_crw(private, CRW_RSC_CPATH
, CRW_ERC_PERRN
,
335 /* Path logically turned on */
340 /* Path became available */
341 sch
->lpm
|= mask
& sch
->opm
;
342 vfio_ccw_queue_crw(private, CRW_RSC_CPATH
, CRW_ERC_INIT
,
350 static struct css_device_id vfio_ccw_sch_ids
[] = {
351 { .match_flags
= 0x1, .type
= SUBCHANNEL_TYPE_IO
, },
352 { /* end of list */ },
354 MODULE_DEVICE_TABLE(css
, vfio_ccw_sch_ids
);
356 static struct css_driver vfio_ccw_sch_driver
= {
359 .owner
= THIS_MODULE
,
361 .subchannel_type
= vfio_ccw_sch_ids
,
362 .irq
= vfio_ccw_sch_irq
,
363 .probe
= vfio_ccw_sch_probe
,
364 .remove
= vfio_ccw_sch_remove
,
365 .shutdown
= vfio_ccw_sch_shutdown
,
366 .sch_event
= vfio_ccw_sch_event
,
367 .chp_event
= vfio_ccw_chp_event
,
370 static int __init
vfio_ccw_debug_init(void)
372 vfio_ccw_debug_msg_id
= debug_register("vfio_ccw_msg", 16, 1,
374 if (!vfio_ccw_debug_msg_id
)
376 debug_register_view(vfio_ccw_debug_msg_id
, &debug_sprintf_view
);
377 debug_set_level(vfio_ccw_debug_msg_id
, 2);
378 vfio_ccw_debug_trace_id
= debug_register("vfio_ccw_trace", 16, 1, 16);
379 if (!vfio_ccw_debug_trace_id
)
381 debug_register_view(vfio_ccw_debug_trace_id
, &debug_hex_ascii_view
);
382 debug_set_level(vfio_ccw_debug_trace_id
, 2);
386 debug_unregister(vfio_ccw_debug_msg_id
);
387 debug_unregister(vfio_ccw_debug_trace_id
);
391 static void vfio_ccw_debug_exit(void)
393 debug_unregister(vfio_ccw_debug_msg_id
);
394 debug_unregister(vfio_ccw_debug_trace_id
);
397 static void vfio_ccw_destroy_regions(void)
399 kmem_cache_destroy(vfio_ccw_crw_region
);
400 kmem_cache_destroy(vfio_ccw_schib_region
);
401 kmem_cache_destroy(vfio_ccw_cmd_region
);
402 kmem_cache_destroy(vfio_ccw_io_region
);
405 static int __init
vfio_ccw_sch_init(void)
409 ret
= vfio_ccw_debug_init();
413 vfio_ccw_work_q
= create_singlethread_workqueue("vfio-ccw");
414 if (!vfio_ccw_work_q
) {
419 vfio_ccw_io_region
= kmem_cache_create_usercopy("vfio_ccw_io_region",
420 sizeof(struct ccw_io_region
), 0,
422 sizeof(struct ccw_io_region
), NULL
);
423 if (!vfio_ccw_io_region
) {
428 vfio_ccw_cmd_region
= kmem_cache_create_usercopy("vfio_ccw_cmd_region",
429 sizeof(struct ccw_cmd_region
), 0,
431 sizeof(struct ccw_cmd_region
), NULL
);
432 if (!vfio_ccw_cmd_region
) {
437 vfio_ccw_schib_region
= kmem_cache_create_usercopy("vfio_ccw_schib_region",
438 sizeof(struct ccw_schib_region
), 0,
440 sizeof(struct ccw_schib_region
), NULL
);
442 if (!vfio_ccw_schib_region
) {
447 vfio_ccw_crw_region
= kmem_cache_create_usercopy("vfio_ccw_crw_region",
448 sizeof(struct ccw_crw_region
), 0,
450 sizeof(struct ccw_crw_region
), NULL
);
452 if (!vfio_ccw_crw_region
) {
457 ret
= mdev_register_driver(&vfio_ccw_mdev_driver
);
461 isc_register(VFIO_CCW_ISC
);
462 ret
= css_driver_register(&vfio_ccw_sch_driver
);
464 isc_unregister(VFIO_CCW_ISC
);
471 mdev_unregister_driver(&vfio_ccw_mdev_driver
);
473 vfio_ccw_destroy_regions();
474 destroy_workqueue(vfio_ccw_work_q
);
475 vfio_ccw_debug_exit();
479 static void __exit
vfio_ccw_sch_exit(void)
481 css_driver_unregister(&vfio_ccw_sch_driver
);
482 mdev_unregister_driver(&vfio_ccw_mdev_driver
);
483 isc_unregister(VFIO_CCW_ISC
);
484 vfio_ccw_destroy_regions();
485 destroy_workqueue(vfio_ccw_work_q
);
486 vfio_ccw_debug_exit();
488 module_init(vfio_ccw_sch_init
);
489 module_exit(vfio_ccw_sch_exit
);
491 MODULE_DESCRIPTION("VFIO based Subchannel device driver");
492 MODULE_LICENSE("GPL v2");