1 // SPDX-License-Identifier: GPL-2.0
3 * VFIO based Physical Subchannel device driver
5 * Copyright IBM Corp. 2017
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/device.h>
14 #include <linux/slab.h>
15 #include <linux/uuid.h>
16 #include <linux/mdev.h>
22 #include "vfio_ccw_private.h"
24 struct workqueue_struct *vfio_ccw_work_q;
25 static struct kmem_cache *vfio_ccw_io_region;
30 int vfio_ccw_sch_quiesce(struct subchannel *sch)
32 struct vfio_ccw_private *private = dev_get_drvdata(&sch->dev);
33 DECLARE_COMPLETION_ONSTACK(completion);
36 spin_lock_irq(sch->lock);
37 if (!sch->schib.pmcw.ena)
39 ret = cio_disable_subchannel(sch);
46 ret = cio_cancel_halt_clear(sch, &iretry);
47 while (ret == -EBUSY) {
49 * Flush all I/O and wait for
50 * cancel/halt/clear completion.
52 private->completion = &completion;
53 spin_unlock_irq(sch->lock);
55 wait_for_completion_timeout(&completion, 3*HZ);
57 spin_lock_irq(sch->lock);
58 private->completion = NULL;
59 flush_workqueue(vfio_ccw_work_q);
60 ret = cio_cancel_halt_clear(sch, &iretry);
63 ret = cio_disable_subchannel(sch);
64 } while (ret == -EBUSY);
66 private->state = VFIO_CCW_STATE_NOT_OPER;
67 spin_unlock_irq(sch->lock);
71 static void vfio_ccw_sch_io_todo(struct work_struct *work)
73 struct vfio_ccw_private *private;
76 private = container_of(work, struct vfio_ccw_private, io_work);
79 if (scsw_is_solicited(&irb->scsw)) {
80 cp_update_scsw(&private->cp, &irb->scsw);
81 cp_free(&private->cp);
83 memcpy(private->io_region->irb_area, irb, sizeof(*irb));
85 if (private->io_trigger)
86 eventfd_signal(private->io_trigger, 1);
89 private->state = VFIO_CCW_STATE_IDLE;
93 * Css driver callbacks
95 static void vfio_ccw_sch_irq(struct subchannel *sch)
97 struct vfio_ccw_private *private = dev_get_drvdata(&sch->dev);
99 inc_irq_stat(IRQIO_CIO);
100 vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_INTERRUPT);
103 static int vfio_ccw_sch_probe(struct subchannel *sch)
105 struct pmcw *pmcw = &sch->schib.pmcw;
106 struct vfio_ccw_private *private;
110 dev_warn(&sch->dev, "vfio: ccw: does not support QDIO: %s\n",
111 dev_name(&sch->dev));
115 private = kzalloc(sizeof(*private), GFP_KERNEL | GFP_DMA);
119 private->io_region = kmem_cache_zalloc(vfio_ccw_io_region,
120 GFP_KERNEL | GFP_DMA);
121 if (!private->io_region) {
127 dev_set_drvdata(&sch->dev, private);
129 spin_lock_irq(sch->lock);
130 private->state = VFIO_CCW_STATE_NOT_OPER;
131 sch->isc = VFIO_CCW_ISC;
132 ret = cio_enable_subchannel(sch, (u32)(unsigned long)sch);
133 spin_unlock_irq(sch->lock);
137 INIT_WORK(&private->io_work, vfio_ccw_sch_io_todo);
138 atomic_set(&private->avail, 1);
139 private->state = VFIO_CCW_STATE_STANDBY;
141 ret = vfio_ccw_mdev_reg(sch);
148 cio_disable_subchannel(sch);
150 dev_set_drvdata(&sch->dev, NULL);
151 kmem_cache_free(vfio_ccw_io_region, private->io_region);
156 static int vfio_ccw_sch_remove(struct subchannel *sch)
158 struct vfio_ccw_private *private = dev_get_drvdata(&sch->dev);
160 vfio_ccw_sch_quiesce(sch);
162 vfio_ccw_mdev_unreg(sch);
164 dev_set_drvdata(&sch->dev, NULL);
166 kmem_cache_free(vfio_ccw_io_region, private->io_region);
172 static void vfio_ccw_sch_shutdown(struct subchannel *sch)
174 vfio_ccw_sch_quiesce(sch);
178 * vfio_ccw_sch_event - process subchannel event
180 * @process: non-zero if function is called in process context
182 * An unspecified event occurred for this subchannel. Adjust data according
183 * to the current operational state of the subchannel. Return zero when the
184 * event has been handled sufficiently or -EAGAIN when this function should
185 * be called again in process context.
187 static int vfio_ccw_sch_event(struct subchannel *sch, int process)
189 struct vfio_ccw_private *private = dev_get_drvdata(&sch->dev);
193 spin_lock_irqsave(sch->lock, flags);
194 if (!device_is_registered(&sch->dev))
197 if (work_pending(&sch->todo_work))
200 if (cio_update_schib(sch)) {
201 vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_NOT_OPER);
206 private = dev_get_drvdata(&sch->dev);
207 if (private->state == VFIO_CCW_STATE_NOT_OPER) {
208 private->state = private->mdev ? VFIO_CCW_STATE_IDLE :
209 VFIO_CCW_STATE_STANDBY;
214 spin_unlock_irqrestore(sch->lock, flags);
219 static struct css_device_id vfio_ccw_sch_ids[] = {
220 { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
221 { /* end of list */ },
223 MODULE_DEVICE_TABLE(css, vfio_ccw_sch_ids);
225 static struct css_driver vfio_ccw_sch_driver = {
228 .owner = THIS_MODULE,
230 .subchannel_type = vfio_ccw_sch_ids,
231 .irq = vfio_ccw_sch_irq,
232 .probe = vfio_ccw_sch_probe,
233 .remove = vfio_ccw_sch_remove,
234 .shutdown = vfio_ccw_sch_shutdown,
235 .sch_event = vfio_ccw_sch_event,
238 static int __init vfio_ccw_sch_init(void)
242 vfio_ccw_work_q = create_singlethread_workqueue("vfio-ccw");
243 if (!vfio_ccw_work_q)
246 vfio_ccw_io_region = kmem_cache_create_usercopy("vfio_ccw_io_region",
247 sizeof(struct ccw_io_region), 0,
249 sizeof(struct ccw_io_region), NULL);
250 if (!vfio_ccw_io_region) {
251 destroy_workqueue(vfio_ccw_work_q);
255 isc_register(VFIO_CCW_ISC);
256 ret = css_driver_register(&vfio_ccw_sch_driver);
258 isc_unregister(VFIO_CCW_ISC);
259 kmem_cache_destroy(vfio_ccw_io_region);
260 destroy_workqueue(vfio_ccw_work_q);
266 static void __exit vfio_ccw_sch_exit(void)
268 css_driver_unregister(&vfio_ccw_sch_driver);
269 isc_unregister(VFIO_CCW_ISC);
270 kmem_cache_destroy(vfio_ccw_io_region);
271 destroy_workqueue(vfio_ccw_work_q);
273 module_init(vfio_ccw_sch_init);
274 module_exit(vfio_ccw_sch_exit);
276 MODULE_LICENSE("GPL v2");