• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * VFIO based Physical Subchannel device driver
4  *
5  * Copyright IBM Corp. 2017
6  * Copyright Red Hat, Inc. 2019
7  *
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>
11  */
12 
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/device.h>
16 #include <linux/slab.h>
17 #include <linux/uuid.h>
18 #include <linux/mdev.h>
19 
20 #include <asm/isc.h>
21 
22 #include "ioasm.h"
23 #include "css.h"
24 #include "vfio_ccw_private.h"
25 
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 
30 debug_info_t *vfio_ccw_debug_msg_id;
31 debug_info_t *vfio_ccw_debug_trace_id;
32 
33 /*
34  * Helpers
35  */
vfio_ccw_sch_quiesce(struct subchannel * sch)36 int vfio_ccw_sch_quiesce(struct subchannel *sch)
37 {
38 	struct vfio_ccw_private *private = dev_get_drvdata(&sch->dev);
39 	DECLARE_COMPLETION_ONSTACK(completion);
40 	int iretry, ret = 0;
41 
42 	spin_lock_irq(sch->lock);
43 	if (!sch->schib.pmcw.ena)
44 		goto out_unlock;
45 	ret = cio_disable_subchannel(sch);
46 	if (ret != -EBUSY)
47 		goto out_unlock;
48 
49 	iretry = 255;
50 	do {
51 
52 		ret = cio_cancel_halt_clear(sch, &iretry);
53 
54 		if (ret == -EIO) {
55 			pr_err("vfio_ccw: could not quiesce subchannel 0.%x.%04x!\n",
56 			       sch->schid.ssid, sch->schid.sch_no);
57 			break;
58 		}
59 
60 		/*
61 		 * Flush all I/O and wait for
62 		 * cancel/halt/clear completion.
63 		 */
64 		private->completion = &completion;
65 		spin_unlock_irq(sch->lock);
66 
67 		if (ret == -EBUSY)
68 			wait_for_completion_timeout(&completion, 3*HZ);
69 
70 		private->completion = NULL;
71 		flush_workqueue(vfio_ccw_work_q);
72 		spin_lock_irq(sch->lock);
73 		ret = cio_disable_subchannel(sch);
74 	} while (ret == -EBUSY);
75 out_unlock:
76 	private->state = VFIO_CCW_STATE_NOT_OPER;
77 	spin_unlock_irq(sch->lock);
78 	return ret;
79 }
80 
vfio_ccw_sch_io_todo(struct work_struct * work)81 static void vfio_ccw_sch_io_todo(struct work_struct *work)
82 {
83 	struct vfio_ccw_private *private;
84 	struct irb *irb;
85 	bool is_final;
86 	bool cp_is_finished = false;
87 
88 	private = container_of(work, struct vfio_ccw_private, io_work);
89 	irb = &private->irb;
90 
91 	is_final = !(scsw_actl(&irb->scsw) &
92 		     (SCSW_ACTL_DEVACT | SCSW_ACTL_SCHACT));
93 	if (scsw_is_solicited(&irb->scsw)) {
94 		cp_update_scsw(&private->cp, &irb->scsw);
95 		if (is_final && private->state == VFIO_CCW_STATE_CP_PENDING) {
96 			cp_free(&private->cp);
97 			cp_is_finished = true;
98 		}
99 	}
100 	mutex_lock(&private->io_mutex);
101 	memcpy(private->io_region->irb_area, irb, sizeof(*irb));
102 	mutex_unlock(&private->io_mutex);
103 
104 	/*
105 	 * Reset to IDLE only if processing of a channel program
106 	 * has finished. Do not overwrite a possible processing
107 	 * state if the final interrupt was for HSCH or CSCH.
108 	 */
109 	if (private->mdev && cp_is_finished)
110 		private->state = VFIO_CCW_STATE_IDLE;
111 
112 	if (private->io_trigger)
113 		eventfd_signal(private->io_trigger, 1);
114 }
115 
116 /*
117  * Css driver callbacks
118  */
vfio_ccw_sch_irq(struct subchannel * sch)119 static void vfio_ccw_sch_irq(struct subchannel *sch)
120 {
121 	struct vfio_ccw_private *private = dev_get_drvdata(&sch->dev);
122 
123 	inc_irq_stat(IRQIO_CIO);
124 	vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_INTERRUPT);
125 }
126 
vfio_ccw_sch_probe(struct subchannel * sch)127 static int vfio_ccw_sch_probe(struct subchannel *sch)
128 {
129 	struct pmcw *pmcw = &sch->schib.pmcw;
130 	struct vfio_ccw_private *private;
131 	int ret = -ENOMEM;
132 
133 	if (pmcw->qf) {
134 		dev_warn(&sch->dev, "vfio: ccw: does not support QDIO: %s\n",
135 			 dev_name(&sch->dev));
136 		return -ENODEV;
137 	}
138 
139 	private = kzalloc(sizeof(*private), GFP_KERNEL | GFP_DMA);
140 	if (!private)
141 		return -ENOMEM;
142 
143 	private->cp.guest_cp = kcalloc(CCWCHAIN_LEN_MAX, sizeof(struct ccw1),
144 				       GFP_KERNEL);
145 	if (!private->cp.guest_cp)
146 		goto out_free;
147 
148 	private->io_region = kmem_cache_zalloc(vfio_ccw_io_region,
149 					       GFP_KERNEL | GFP_DMA);
150 	if (!private->io_region)
151 		goto out_free;
152 
153 	private->cmd_region = kmem_cache_zalloc(vfio_ccw_cmd_region,
154 						GFP_KERNEL | GFP_DMA);
155 	if (!private->cmd_region)
156 		goto out_free;
157 
158 	private->sch = sch;
159 	dev_set_drvdata(&sch->dev, private);
160 	mutex_init(&private->io_mutex);
161 
162 	spin_lock_irq(sch->lock);
163 	private->state = VFIO_CCW_STATE_NOT_OPER;
164 	sch->isc = VFIO_CCW_ISC;
165 	ret = cio_enable_subchannel(sch, (u32)(unsigned long)sch);
166 	spin_unlock_irq(sch->lock);
167 	if (ret)
168 		goto out_free;
169 
170 	INIT_WORK(&private->io_work, vfio_ccw_sch_io_todo);
171 	atomic_set(&private->avail, 1);
172 	private->state = VFIO_CCW_STATE_STANDBY;
173 
174 	ret = vfio_ccw_mdev_reg(sch);
175 	if (ret)
176 		goto out_disable;
177 
178 	if (dev_get_uevent_suppress(&sch->dev)) {
179 		dev_set_uevent_suppress(&sch->dev, 0);
180 		kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
181 	}
182 
183 	VFIO_CCW_MSG_EVENT(4, "bound to subchannel %x.%x.%04x\n",
184 			   sch->schid.cssid, sch->schid.ssid,
185 			   sch->schid.sch_no);
186 	return 0;
187 
188 out_disable:
189 	cio_disable_subchannel(sch);
190 out_free:
191 	dev_set_drvdata(&sch->dev, NULL);
192 	if (private->cmd_region)
193 		kmem_cache_free(vfio_ccw_cmd_region, private->cmd_region);
194 	if (private->io_region)
195 		kmem_cache_free(vfio_ccw_io_region, private->io_region);
196 	kfree(private->cp.guest_cp);
197 	kfree(private);
198 	return ret;
199 }
200 
vfio_ccw_sch_remove(struct subchannel * sch)201 static int vfio_ccw_sch_remove(struct subchannel *sch)
202 {
203 	struct vfio_ccw_private *private = dev_get_drvdata(&sch->dev);
204 
205 	vfio_ccw_sch_quiesce(sch);
206 
207 	vfio_ccw_mdev_unreg(sch);
208 
209 	dev_set_drvdata(&sch->dev, NULL);
210 
211 	kmem_cache_free(vfio_ccw_cmd_region, private->cmd_region);
212 	kmem_cache_free(vfio_ccw_io_region, private->io_region);
213 	kfree(private->cp.guest_cp);
214 	kfree(private);
215 
216 	VFIO_CCW_MSG_EVENT(4, "unbound from subchannel %x.%x.%04x\n",
217 			   sch->schid.cssid, sch->schid.ssid,
218 			   sch->schid.sch_no);
219 	return 0;
220 }
221 
vfio_ccw_sch_shutdown(struct subchannel * sch)222 static void vfio_ccw_sch_shutdown(struct subchannel *sch)
223 {
224 	vfio_ccw_sch_quiesce(sch);
225 }
226 
227 /**
228  * vfio_ccw_sch_event - process subchannel event
229  * @sch: subchannel
230  * @process: non-zero if function is called in process context
231  *
232  * An unspecified event occurred for this subchannel. Adjust data according
233  * to the current operational state of the subchannel. Return zero when the
234  * event has been handled sufficiently or -EAGAIN when this function should
235  * be called again in process context.
236  */
vfio_ccw_sch_event(struct subchannel * sch,int process)237 static int vfio_ccw_sch_event(struct subchannel *sch, int process)
238 {
239 	struct vfio_ccw_private *private = dev_get_drvdata(&sch->dev);
240 	unsigned long flags;
241 	int rc = -EAGAIN;
242 
243 	spin_lock_irqsave(sch->lock, flags);
244 	if (!device_is_registered(&sch->dev))
245 		goto out_unlock;
246 
247 	if (work_pending(&sch->todo_work))
248 		goto out_unlock;
249 
250 	rc = 0;
251 
252 	if (cio_update_schib(sch))
253 		vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_NOT_OPER);
254 
255 out_unlock:
256 	spin_unlock_irqrestore(sch->lock, flags);
257 
258 	return rc;
259 }
260 
261 static struct css_device_id vfio_ccw_sch_ids[] = {
262 	{ .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
263 	{ /* end of list */ },
264 };
265 MODULE_DEVICE_TABLE(css, vfio_ccw_sch_ids);
266 
267 static struct css_driver vfio_ccw_sch_driver = {
268 	.drv = {
269 		.name = "vfio_ccw",
270 		.owner = THIS_MODULE,
271 	},
272 	.subchannel_type = vfio_ccw_sch_ids,
273 	.irq = vfio_ccw_sch_irq,
274 	.probe = vfio_ccw_sch_probe,
275 	.remove = vfio_ccw_sch_remove,
276 	.shutdown = vfio_ccw_sch_shutdown,
277 	.sch_event = vfio_ccw_sch_event,
278 };
279 
vfio_ccw_debug_init(void)280 static int __init vfio_ccw_debug_init(void)
281 {
282 	vfio_ccw_debug_msg_id = debug_register("vfio_ccw_msg", 16, 1,
283 					       11 * sizeof(long));
284 	if (!vfio_ccw_debug_msg_id)
285 		goto out_unregister;
286 	debug_register_view(vfio_ccw_debug_msg_id, &debug_sprintf_view);
287 	debug_set_level(vfio_ccw_debug_msg_id, 2);
288 	vfio_ccw_debug_trace_id = debug_register("vfio_ccw_trace", 16, 1, 16);
289 	if (!vfio_ccw_debug_trace_id)
290 		goto out_unregister;
291 	debug_register_view(vfio_ccw_debug_trace_id, &debug_hex_ascii_view);
292 	debug_set_level(vfio_ccw_debug_trace_id, 2);
293 	return 0;
294 
295 out_unregister:
296 	debug_unregister(vfio_ccw_debug_msg_id);
297 	debug_unregister(vfio_ccw_debug_trace_id);
298 	return -1;
299 }
300 
vfio_ccw_debug_exit(void)301 static void vfio_ccw_debug_exit(void)
302 {
303 	debug_unregister(vfio_ccw_debug_msg_id);
304 	debug_unregister(vfio_ccw_debug_trace_id);
305 }
306 
vfio_ccw_sch_init(void)307 static int __init vfio_ccw_sch_init(void)
308 {
309 	int ret;
310 
311 	ret = vfio_ccw_debug_init();
312 	if (ret)
313 		return ret;
314 
315 	vfio_ccw_work_q = create_singlethread_workqueue("vfio-ccw");
316 	if (!vfio_ccw_work_q) {
317 		ret = -ENOMEM;
318 		goto out_err;
319 	}
320 
321 	vfio_ccw_io_region = kmem_cache_create_usercopy("vfio_ccw_io_region",
322 					sizeof(struct ccw_io_region), 0,
323 					SLAB_ACCOUNT, 0,
324 					sizeof(struct ccw_io_region), NULL);
325 	if (!vfio_ccw_io_region) {
326 		ret = -ENOMEM;
327 		goto out_err;
328 	}
329 
330 	vfio_ccw_cmd_region = kmem_cache_create_usercopy("vfio_ccw_cmd_region",
331 					sizeof(struct ccw_cmd_region), 0,
332 					SLAB_ACCOUNT, 0,
333 					sizeof(struct ccw_cmd_region), NULL);
334 	if (!vfio_ccw_cmd_region) {
335 		ret = -ENOMEM;
336 		goto out_err;
337 	}
338 
339 	isc_register(VFIO_CCW_ISC);
340 	ret = css_driver_register(&vfio_ccw_sch_driver);
341 	if (ret) {
342 		isc_unregister(VFIO_CCW_ISC);
343 		goto out_err;
344 	}
345 
346 	return ret;
347 
348 out_err:
349 	kmem_cache_destroy(vfio_ccw_cmd_region);
350 	kmem_cache_destroy(vfio_ccw_io_region);
351 	destroy_workqueue(vfio_ccw_work_q);
352 	vfio_ccw_debug_exit();
353 	return ret;
354 }
355 
vfio_ccw_sch_exit(void)356 static void __exit vfio_ccw_sch_exit(void)
357 {
358 	css_driver_unregister(&vfio_ccw_sch_driver);
359 	isc_unregister(VFIO_CCW_ISC);
360 	kmem_cache_destroy(vfio_ccw_io_region);
361 	kmem_cache_destroy(vfio_ccw_cmd_region);
362 	destroy_workqueue(vfio_ccw_work_q);
363 	vfio_ccw_debug_exit();
364 }
365 module_init(vfio_ccw_sch_init);
366 module_exit(vfio_ccw_sch_exit);
367 
368 MODULE_LICENSE("GPL v2");
369