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/device.h>
16 #include <linux/slab.h>
17 #include <linux/mdev.h>
24 #include "vfio_ccw_private.h"
26 struct workqueue_struct *vfio_ccw_work_q;
27 static struct kmem_cache *vfio_ccw_io_region;
28 static struct kmem_cache *vfio_ccw_cmd_region;
29 static struct kmem_cache *vfio_ccw_schib_region;
30 static struct kmem_cache *vfio_ccw_crw_region;
32 debug_info_t *vfio_ccw_debug_msg_id;
33 debug_info_t *vfio_ccw_debug_trace_id;
38 int vfio_ccw_sch_quiesce(struct subchannel *sch)
40 struct vfio_ccw_private *private = dev_get_drvdata(&sch->dev);
41 DECLARE_COMPLETION_ONSTACK(completion);
47 ret = cio_cancel_halt_clear(sch, &iretry);
50 pr_err("vfio_ccw: could not quiesce subchannel 0.%x.%04x!\n",
51 sch->schid.ssid, sch->schid.sch_no);
56 * Flush all I/O and wait for
57 * cancel/halt/clear completion.
59 private->completion = &completion;
60 spin_unlock_irq(sch->lock);
63 wait_for_completion_timeout(&completion, 3*HZ);
65 private->completion = NULL;
66 flush_workqueue(vfio_ccw_work_q);
67 spin_lock_irq(sch->lock);
68 ret = cio_disable_subchannel(sch);
69 } while (ret == -EBUSY);
74 static void vfio_ccw_sch_io_todo(struct work_struct *work)
76 struct vfio_ccw_private *private;
79 bool cp_is_finished = false;
81 private = container_of(work, struct vfio_ccw_private, io_work);
84 is_final = !(scsw_actl(&irb->scsw) &
85 (SCSW_ACTL_DEVACT | SCSW_ACTL_SCHACT));
86 if (scsw_is_solicited(&irb->scsw)) {
87 cp_update_scsw(&private->cp, &irb->scsw);
88 if (is_final && private->state == VFIO_CCW_STATE_CP_PENDING) {
89 cp_free(&private->cp);
90 cp_is_finished = true;
93 mutex_lock(&private->io_mutex);
94 memcpy(private->io_region->irb_area, irb, sizeof(*irb));
95 mutex_unlock(&private->io_mutex);
98 * Reset to IDLE only if processing of a channel program
99 * has finished. Do not overwrite a possible processing
100 * state if the interrupt was unsolicited, or if the final
101 * interrupt was for HSCH or CSCH.
104 private->state = VFIO_CCW_STATE_IDLE;
106 if (private->io_trigger)
107 eventfd_signal(private->io_trigger, 1);
110 static void vfio_ccw_crw_todo(struct work_struct *work)
112 struct vfio_ccw_private *private;
114 private = container_of(work, struct vfio_ccw_private, crw_work);
116 if (!list_empty(&private->crw) && private->crw_trigger)
117 eventfd_signal(private->crw_trigger, 1);
121 * Css driver callbacks
123 static void vfio_ccw_sch_irq(struct subchannel *sch)
125 struct vfio_ccw_private *private = dev_get_drvdata(&sch->dev);
127 inc_irq_stat(IRQIO_CIO);
128 vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_INTERRUPT);
131 static struct vfio_ccw_private *vfio_ccw_alloc_private(struct subchannel *sch)
133 struct vfio_ccw_private *private;
135 private = kzalloc(sizeof(*private), GFP_KERNEL);
137 return ERR_PTR(-ENOMEM);
140 mutex_init(&private->io_mutex);
141 private->state = VFIO_CCW_STATE_STANDBY;
142 INIT_LIST_HEAD(&private->crw);
143 INIT_WORK(&private->io_work, vfio_ccw_sch_io_todo);
144 INIT_WORK(&private->crw_work, vfio_ccw_crw_todo);
145 atomic_set(&private->avail, 1);
147 private->cp.guest_cp = kcalloc(CCWCHAIN_LEN_MAX, sizeof(struct ccw1),
149 if (!private->cp.guest_cp)
150 goto out_free_private;
152 private->io_region = kmem_cache_zalloc(vfio_ccw_io_region,
153 GFP_KERNEL | GFP_DMA);
154 if (!private->io_region)
157 private->cmd_region = kmem_cache_zalloc(vfio_ccw_cmd_region,
158 GFP_KERNEL | GFP_DMA);
159 if (!private->cmd_region)
162 private->schib_region = kmem_cache_zalloc(vfio_ccw_schib_region,
163 GFP_KERNEL | GFP_DMA);
165 if (!private->schib_region)
168 private->crw_region = kmem_cache_zalloc(vfio_ccw_crw_region,
169 GFP_KERNEL | GFP_DMA);
171 if (!private->crw_region)
176 kmem_cache_free(vfio_ccw_schib_region, private->schib_region);
178 kmem_cache_free(vfio_ccw_cmd_region, private->cmd_region);
180 kmem_cache_free(vfio_ccw_io_region, private->io_region);
182 kfree(private->cp.guest_cp);
184 mutex_destroy(&private->io_mutex);
186 return ERR_PTR(-ENOMEM);
189 static void vfio_ccw_free_private(struct vfio_ccw_private *private)
191 struct vfio_ccw_crw *crw, *temp;
193 list_for_each_entry_safe(crw, temp, &private->crw, next) {
194 list_del(&crw->next);
198 kmem_cache_free(vfio_ccw_crw_region, private->crw_region);
199 kmem_cache_free(vfio_ccw_schib_region, private->schib_region);
200 kmem_cache_free(vfio_ccw_cmd_region, private->cmd_region);
201 kmem_cache_free(vfio_ccw_io_region, private->io_region);
202 kfree(private->cp.guest_cp);
203 mutex_destroy(&private->io_mutex);
207 static int vfio_ccw_sch_probe(struct subchannel *sch)
209 struct pmcw *pmcw = &sch->schib.pmcw;
210 struct vfio_ccw_private *private;
214 dev_warn(&sch->dev, "vfio: ccw: does not support QDIO: %s\n",
215 dev_name(&sch->dev));
219 private = vfio_ccw_alloc_private(sch);
221 return PTR_ERR(private);
223 dev_set_drvdata(&sch->dev, private);
225 ret = mdev_register_device(&sch->dev, &vfio_ccw_mdev_driver);
229 VFIO_CCW_MSG_EVENT(4, "bound to subchannel %x.%x.%04x\n",
230 sch->schid.cssid, sch->schid.ssid,
235 dev_set_drvdata(&sch->dev, NULL);
236 vfio_ccw_free_private(private);
240 static void vfio_ccw_sch_remove(struct subchannel *sch)
242 struct vfio_ccw_private *private = dev_get_drvdata(&sch->dev);
244 mdev_unregister_device(&sch->dev);
246 dev_set_drvdata(&sch->dev, NULL);
248 vfio_ccw_free_private(private);
250 VFIO_CCW_MSG_EVENT(4, "unbound from subchannel %x.%x.%04x\n",
251 sch->schid.cssid, sch->schid.ssid,
255 static void vfio_ccw_sch_shutdown(struct subchannel *sch)
257 struct vfio_ccw_private *private = dev_get_drvdata(&sch->dev);
259 vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_CLOSE);
260 vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_NOT_OPER);
264 * vfio_ccw_sch_event - process subchannel event
266 * @process: non-zero if function is called in process context
268 * An unspecified event occurred for this subchannel. Adjust data according
269 * to the current operational state of the subchannel. Return zero when the
270 * event has been handled sufficiently or -EAGAIN when this function should
271 * be called again in process context.
273 static int vfio_ccw_sch_event(struct subchannel *sch, int process)
275 struct vfio_ccw_private *private = dev_get_drvdata(&sch->dev);
279 spin_lock_irqsave(sch->lock, flags);
280 if (!device_is_registered(&sch->dev))
283 if (work_pending(&sch->todo_work))
288 if (cio_update_schib(sch))
289 vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_NOT_OPER);
292 spin_unlock_irqrestore(sch->lock, flags);
297 static void vfio_ccw_queue_crw(struct vfio_ccw_private *private,
302 struct vfio_ccw_crw *crw;
305 * If unable to allocate a CRW, just drop the event and
306 * carry on. The guest will either see a later one or
307 * learn when it issues its own store subchannel.
309 crw = kzalloc(sizeof(*crw), GFP_ATOMIC);
314 * Build the CRW based on the inputs given to us.
318 crw->crw.rsid = rsid;
320 list_add_tail(&crw->next, &private->crw);
321 queue_work(vfio_ccw_work_q, &private->crw_work);
324 static int vfio_ccw_chp_event(struct subchannel *sch,
325 struct chp_link *link, int event)
327 struct vfio_ccw_private *private = dev_get_drvdata(&sch->dev);
328 int mask = chp_ssd_get_mask(&sch->ssd_info, link);
331 if (!private || !mask)
334 trace_vfio_ccw_chp_event(private->sch->schid, mask, event);
335 VFIO_CCW_MSG_EVENT(2, "sch %x.%x.%04x: mask=0x%x event=%d\n",
337 sch->schid.ssid, sch->schid.sch_no,
340 if (cio_update_schib(sch))
345 /* Path logically turned off */
348 if (sch->schib.pmcw.lpum & mask)
349 cio_cancel_halt_clear(sch, &retry);
353 if (sch->schib.pmcw.lpum & mask)
354 cio_cancel_halt_clear(sch, &retry);
355 vfio_ccw_queue_crw(private, CRW_RSC_CPATH, CRW_ERC_PERRN,
359 /* Path logically turned on */
364 /* Path became available */
365 sch->lpm |= mask & sch->opm;
366 vfio_ccw_queue_crw(private, CRW_RSC_CPATH, CRW_ERC_INIT,
374 static struct css_device_id vfio_ccw_sch_ids[] = {
375 { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
376 { /* end of list */ },
378 MODULE_DEVICE_TABLE(css, vfio_ccw_sch_ids);
380 static struct css_driver vfio_ccw_sch_driver = {
383 .owner = THIS_MODULE,
385 .subchannel_type = vfio_ccw_sch_ids,
386 .irq = vfio_ccw_sch_irq,
387 .probe = vfio_ccw_sch_probe,
388 .remove = vfio_ccw_sch_remove,
389 .shutdown = vfio_ccw_sch_shutdown,
390 .sch_event = vfio_ccw_sch_event,
391 .chp_event = vfio_ccw_chp_event,
394 static int __init vfio_ccw_debug_init(void)
396 vfio_ccw_debug_msg_id = debug_register("vfio_ccw_msg", 16, 1,
398 if (!vfio_ccw_debug_msg_id)
400 debug_register_view(vfio_ccw_debug_msg_id, &debug_sprintf_view);
401 debug_set_level(vfio_ccw_debug_msg_id, 2);
402 vfio_ccw_debug_trace_id = debug_register("vfio_ccw_trace", 16, 1, 16);
403 if (!vfio_ccw_debug_trace_id)
405 debug_register_view(vfio_ccw_debug_trace_id, &debug_hex_ascii_view);
406 debug_set_level(vfio_ccw_debug_trace_id, 2);
410 debug_unregister(vfio_ccw_debug_msg_id);
411 debug_unregister(vfio_ccw_debug_trace_id);
415 static void vfio_ccw_debug_exit(void)
417 debug_unregister(vfio_ccw_debug_msg_id);
418 debug_unregister(vfio_ccw_debug_trace_id);
421 static void vfio_ccw_destroy_regions(void)
423 kmem_cache_destroy(vfio_ccw_crw_region);
424 kmem_cache_destroy(vfio_ccw_schib_region);
425 kmem_cache_destroy(vfio_ccw_cmd_region);
426 kmem_cache_destroy(vfio_ccw_io_region);
429 static int __init vfio_ccw_sch_init(void)
433 ret = vfio_ccw_debug_init();
437 vfio_ccw_work_q = create_singlethread_workqueue("vfio-ccw");
438 if (!vfio_ccw_work_q) {
443 vfio_ccw_io_region = kmem_cache_create_usercopy("vfio_ccw_io_region",
444 sizeof(struct ccw_io_region), 0,
446 sizeof(struct ccw_io_region), NULL);
447 if (!vfio_ccw_io_region) {
452 vfio_ccw_cmd_region = kmem_cache_create_usercopy("vfio_ccw_cmd_region",
453 sizeof(struct ccw_cmd_region), 0,
455 sizeof(struct ccw_cmd_region), NULL);
456 if (!vfio_ccw_cmd_region) {
461 vfio_ccw_schib_region = kmem_cache_create_usercopy("vfio_ccw_schib_region",
462 sizeof(struct ccw_schib_region), 0,
464 sizeof(struct ccw_schib_region), NULL);
466 if (!vfio_ccw_schib_region) {
471 vfio_ccw_crw_region = kmem_cache_create_usercopy("vfio_ccw_crw_region",
472 sizeof(struct ccw_crw_region), 0,
474 sizeof(struct ccw_crw_region), NULL);
476 if (!vfio_ccw_crw_region) {
481 ret = mdev_register_driver(&vfio_ccw_mdev_driver);
485 isc_register(VFIO_CCW_ISC);
486 ret = css_driver_register(&vfio_ccw_sch_driver);
488 isc_unregister(VFIO_CCW_ISC);
495 mdev_unregister_driver(&vfio_ccw_mdev_driver);
497 vfio_ccw_destroy_regions();
498 destroy_workqueue(vfio_ccw_work_q);
499 vfio_ccw_debug_exit();
503 static void __exit vfio_ccw_sch_exit(void)
505 css_driver_unregister(&vfio_ccw_sch_driver);
506 mdev_unregister_driver(&vfio_ccw_mdev_driver);
507 isc_unregister(VFIO_CCW_ISC);
508 vfio_ccw_destroy_regions();
509 destroy_workqueue(vfio_ccw_work_q);
510 vfio_ccw_debug_exit();
512 module_init(vfio_ccw_sch_init);
513 module_exit(vfio_ccw_sch_exit);
515 MODULE_LICENSE("GPL v2");