xref: /linux/drivers/vfio/pci/virtio/main.c (revision b8e85e6f3a09fc56b0ff574887798962ef8a8f80)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved
4  */
5 
6 #include <linux/device.h>
7 #include <linux/module.h>
8 #include <linux/mutex.h>
9 #include <linux/pci.h>
10 #include <linux/pm_runtime.h>
11 #include <linux/types.h>
12 #include <linux/uaccess.h>
13 #include <linux/vfio.h>
14 #include <linux/vfio_pci_core.h>
15 #include <linux/virtio_pci.h>
16 #include <linux/virtio_net.h>
17 #include <linux/virtio_pci_admin.h>
18 
19 struct virtiovf_pci_core_device {
20 	struct vfio_pci_core_device core_device;
21 	u8 *bar0_virtual_buf;
22 	/* synchronize access to the virtual buf */
23 	struct mutex bar_mutex;
24 	void __iomem *notify_addr;
25 	u64 notify_offset;
26 	__le32 pci_base_addr_0;
27 	__le16 pci_cmd;
28 	u8 bar0_virtual_buf_size;
29 	u8 notify_bar;
30 };
31 
32 static int
33 virtiovf_issue_legacy_rw_cmd(struct virtiovf_pci_core_device *virtvdev,
34 			     loff_t pos, char __user *buf,
35 			     size_t count, bool read)
36 {
37 	bool msix_enabled =
38 		(virtvdev->core_device.irq_type == VFIO_PCI_MSIX_IRQ_INDEX);
39 	struct pci_dev *pdev = virtvdev->core_device.pdev;
40 	u8 *bar0_buf = virtvdev->bar0_virtual_buf;
41 	bool common;
42 	u8 offset;
43 	int ret;
44 
45 	common = pos < VIRTIO_PCI_CONFIG_OFF(msix_enabled);
46 	/* offset within the relevant configuration area */
47 	offset = common ? pos : pos - VIRTIO_PCI_CONFIG_OFF(msix_enabled);
48 	mutex_lock(&virtvdev->bar_mutex);
49 	if (read) {
50 		if (common)
51 			ret = virtio_pci_admin_legacy_common_io_read(pdev, offset,
52 					count, bar0_buf + pos);
53 		else
54 			ret = virtio_pci_admin_legacy_device_io_read(pdev, offset,
55 					count, bar0_buf + pos);
56 		if (ret)
57 			goto out;
58 		if (copy_to_user(buf, bar0_buf + pos, count))
59 			ret = -EFAULT;
60 	} else {
61 		if (copy_from_user(bar0_buf + pos, buf, count)) {
62 			ret = -EFAULT;
63 			goto out;
64 		}
65 
66 		if (common)
67 			ret = virtio_pci_admin_legacy_common_io_write(pdev, offset,
68 					count, bar0_buf + pos);
69 		else
70 			ret = virtio_pci_admin_legacy_device_io_write(pdev, offset,
71 					count, bar0_buf + pos);
72 	}
73 out:
74 	mutex_unlock(&virtvdev->bar_mutex);
75 	return ret;
76 }
77 
78 static int
79 virtiovf_pci_bar0_rw(struct virtiovf_pci_core_device *virtvdev,
80 		     loff_t pos, char __user *buf,
81 		     size_t count, bool read)
82 {
83 	struct vfio_pci_core_device *core_device = &virtvdev->core_device;
84 	struct pci_dev *pdev = core_device->pdev;
85 	u16 queue_notify;
86 	int ret;
87 
88 	if (!(le16_to_cpu(virtvdev->pci_cmd) & PCI_COMMAND_IO))
89 		return -EIO;
90 
91 	if (pos + count > virtvdev->bar0_virtual_buf_size)
92 		return -EINVAL;
93 
94 	ret = pm_runtime_resume_and_get(&pdev->dev);
95 	if (ret) {
96 		pci_info_ratelimited(pdev, "runtime resume failed %d\n", ret);
97 		return -EIO;
98 	}
99 
100 	switch (pos) {
101 	case VIRTIO_PCI_QUEUE_NOTIFY:
102 		if (count != sizeof(queue_notify)) {
103 			ret = -EINVAL;
104 			goto end;
105 		}
106 		if (read) {
107 			ret = vfio_pci_core_ioread16(core_device, true, &queue_notify,
108 						     virtvdev->notify_addr);
109 			if (ret)
110 				goto end;
111 			if (copy_to_user(buf, &queue_notify,
112 					 sizeof(queue_notify))) {
113 				ret = -EFAULT;
114 				goto end;
115 			}
116 		} else {
117 			if (copy_from_user(&queue_notify, buf, count)) {
118 				ret = -EFAULT;
119 				goto end;
120 			}
121 			ret = vfio_pci_core_iowrite16(core_device, true, queue_notify,
122 						      virtvdev->notify_addr);
123 		}
124 		break;
125 	default:
126 		ret = virtiovf_issue_legacy_rw_cmd(virtvdev, pos, buf, count,
127 						   read);
128 	}
129 
130 end:
131 	pm_runtime_put(&pdev->dev);
132 	return ret ? ret : count;
133 }
134 
135 static bool range_intersect_range(loff_t range1_start, size_t count1,
136 				  loff_t range2_start, size_t count2,
137 				  loff_t *start_offset,
138 				  size_t *intersect_count,
139 				  size_t *register_offset)
140 {
141 	if (range1_start <= range2_start &&
142 	    range1_start + count1 > range2_start) {
143 		*start_offset = range2_start - range1_start;
144 		*intersect_count = min_t(size_t, count2,
145 					 range1_start + count1 - range2_start);
146 		*register_offset = 0;
147 		return true;
148 	}
149 
150 	if (range1_start > range2_start &&
151 	    range1_start < range2_start + count2) {
152 		*start_offset = 0;
153 		*intersect_count = min_t(size_t, count1,
154 					 range2_start + count2 - range1_start);
155 		*register_offset = range1_start - range2_start;
156 		return true;
157 	}
158 
159 	return false;
160 }
161 
162 static ssize_t virtiovf_pci_read_config(struct vfio_device *core_vdev,
163 					char __user *buf, size_t count,
164 					loff_t *ppos)
165 {
166 	struct virtiovf_pci_core_device *virtvdev = container_of(
167 		core_vdev, struct virtiovf_pci_core_device, core_device.vdev);
168 	loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
169 	size_t register_offset;
170 	loff_t copy_offset;
171 	size_t copy_count;
172 	__le32 val32;
173 	__le16 val16;
174 	u8 val8;
175 	int ret;
176 
177 	ret = vfio_pci_core_read(core_vdev, buf, count, ppos);
178 	if (ret < 0)
179 		return ret;
180 
181 	if (range_intersect_range(pos, count, PCI_DEVICE_ID, sizeof(val16),
182 				  &copy_offset, &copy_count, &register_offset)) {
183 		val16 = cpu_to_le16(VIRTIO_TRANS_ID_NET);
184 		if (copy_to_user(buf + copy_offset, (void *)&val16 + register_offset, copy_count))
185 			return -EFAULT;
186 	}
187 
188 	if ((le16_to_cpu(virtvdev->pci_cmd) & PCI_COMMAND_IO) &&
189 	    range_intersect_range(pos, count, PCI_COMMAND, sizeof(val16),
190 				  &copy_offset, &copy_count, &register_offset)) {
191 		if (copy_from_user((void *)&val16 + register_offset, buf + copy_offset,
192 				   copy_count))
193 			return -EFAULT;
194 		val16 |= cpu_to_le16(PCI_COMMAND_IO);
195 		if (copy_to_user(buf + copy_offset, (void *)&val16 + register_offset,
196 				 copy_count))
197 			return -EFAULT;
198 	}
199 
200 	if (range_intersect_range(pos, count, PCI_REVISION_ID, sizeof(val8),
201 				  &copy_offset, &copy_count, &register_offset)) {
202 		/* Transional needs to have revision 0 */
203 		val8 = 0;
204 		if (copy_to_user(buf + copy_offset, &val8, copy_count))
205 			return -EFAULT;
206 	}
207 
208 	if (range_intersect_range(pos, count, PCI_BASE_ADDRESS_0, sizeof(val32),
209 				  &copy_offset, &copy_count, &register_offset)) {
210 		u32 bar_mask = ~(virtvdev->bar0_virtual_buf_size - 1);
211 		u32 pci_base_addr_0 = le32_to_cpu(virtvdev->pci_base_addr_0);
212 
213 		val32 = cpu_to_le32((pci_base_addr_0 & bar_mask) | PCI_BASE_ADDRESS_SPACE_IO);
214 		if (copy_to_user(buf + copy_offset, (void *)&val32 + register_offset, copy_count))
215 			return -EFAULT;
216 	}
217 
218 	if (range_intersect_range(pos, count, PCI_SUBSYSTEM_ID, sizeof(val16),
219 				  &copy_offset, &copy_count, &register_offset)) {
220 		/*
221 		 * Transitional devices use the PCI subsystem device id as
222 		 * virtio device id, same as legacy driver always did.
223 		 */
224 		val16 = cpu_to_le16(VIRTIO_ID_NET);
225 		if (copy_to_user(buf + copy_offset, (void *)&val16 + register_offset,
226 				 copy_count))
227 			return -EFAULT;
228 	}
229 
230 	if (range_intersect_range(pos, count, PCI_SUBSYSTEM_VENDOR_ID, sizeof(val16),
231 				  &copy_offset, &copy_count, &register_offset)) {
232 		val16 = cpu_to_le16(PCI_VENDOR_ID_REDHAT_QUMRANET);
233 		if (copy_to_user(buf + copy_offset, (void *)&val16 + register_offset,
234 				 copy_count))
235 			return -EFAULT;
236 	}
237 
238 	return count;
239 }
240 
241 static ssize_t
242 virtiovf_pci_core_read(struct vfio_device *core_vdev, char __user *buf,
243 		       size_t count, loff_t *ppos)
244 {
245 	struct virtiovf_pci_core_device *virtvdev = container_of(
246 		core_vdev, struct virtiovf_pci_core_device, core_device.vdev);
247 	unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
248 	loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
249 
250 	if (!count)
251 		return 0;
252 
253 	if (index == VFIO_PCI_CONFIG_REGION_INDEX)
254 		return virtiovf_pci_read_config(core_vdev, buf, count, ppos);
255 
256 	if (index == VFIO_PCI_BAR0_REGION_INDEX)
257 		return virtiovf_pci_bar0_rw(virtvdev, pos, buf, count, true);
258 
259 	return vfio_pci_core_read(core_vdev, buf, count, ppos);
260 }
261 
262 static ssize_t virtiovf_pci_write_config(struct vfio_device *core_vdev,
263 					 const char __user *buf, size_t count,
264 					 loff_t *ppos)
265 {
266 	struct virtiovf_pci_core_device *virtvdev = container_of(
267 		core_vdev, struct virtiovf_pci_core_device, core_device.vdev);
268 	loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
269 	size_t register_offset;
270 	loff_t copy_offset;
271 	size_t copy_count;
272 
273 	if (range_intersect_range(pos, count, PCI_COMMAND, sizeof(virtvdev->pci_cmd),
274 				  &copy_offset, &copy_count,
275 				  &register_offset)) {
276 		if (copy_from_user((void *)&virtvdev->pci_cmd + register_offset,
277 				   buf + copy_offset,
278 				   copy_count))
279 			return -EFAULT;
280 	}
281 
282 	if (range_intersect_range(pos, count, PCI_BASE_ADDRESS_0,
283 				  sizeof(virtvdev->pci_base_addr_0),
284 				  &copy_offset, &copy_count,
285 				  &register_offset)) {
286 		if (copy_from_user((void *)&virtvdev->pci_base_addr_0 + register_offset,
287 				   buf + copy_offset,
288 				   copy_count))
289 			return -EFAULT;
290 	}
291 
292 	return vfio_pci_core_write(core_vdev, buf, count, ppos);
293 }
294 
295 static ssize_t
296 virtiovf_pci_core_write(struct vfio_device *core_vdev, const char __user *buf,
297 			size_t count, loff_t *ppos)
298 {
299 	struct virtiovf_pci_core_device *virtvdev = container_of(
300 		core_vdev, struct virtiovf_pci_core_device, core_device.vdev);
301 	unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
302 	loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
303 
304 	if (!count)
305 		return 0;
306 
307 	if (index == VFIO_PCI_CONFIG_REGION_INDEX)
308 		return virtiovf_pci_write_config(core_vdev, buf, count, ppos);
309 
310 	if (index == VFIO_PCI_BAR0_REGION_INDEX)
311 		return virtiovf_pci_bar0_rw(virtvdev, pos, (char __user *)buf, count, false);
312 
313 	return vfio_pci_core_write(core_vdev, buf, count, ppos);
314 }
315 
316 static int
317 virtiovf_pci_ioctl_get_region_info(struct vfio_device *core_vdev,
318 				   unsigned int cmd, unsigned long arg)
319 {
320 	struct virtiovf_pci_core_device *virtvdev = container_of(
321 		core_vdev, struct virtiovf_pci_core_device, core_device.vdev);
322 	unsigned long minsz = offsetofend(struct vfio_region_info, offset);
323 	void __user *uarg = (void __user *)arg;
324 	struct vfio_region_info info = {};
325 
326 	if (copy_from_user(&info, uarg, minsz))
327 		return -EFAULT;
328 
329 	if (info.argsz < minsz)
330 		return -EINVAL;
331 
332 	switch (info.index) {
333 	case VFIO_PCI_BAR0_REGION_INDEX:
334 		info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
335 		info.size = virtvdev->bar0_virtual_buf_size;
336 		info.flags = VFIO_REGION_INFO_FLAG_READ |
337 			     VFIO_REGION_INFO_FLAG_WRITE;
338 		return copy_to_user(uarg, &info, minsz) ? -EFAULT : 0;
339 	default:
340 		return vfio_pci_core_ioctl(core_vdev, cmd, arg);
341 	}
342 }
343 
344 static long
345 virtiovf_vfio_pci_core_ioctl(struct vfio_device *core_vdev, unsigned int cmd,
346 			     unsigned long arg)
347 {
348 	switch (cmd) {
349 	case VFIO_DEVICE_GET_REGION_INFO:
350 		return virtiovf_pci_ioctl_get_region_info(core_vdev, cmd, arg);
351 	default:
352 		return vfio_pci_core_ioctl(core_vdev, cmd, arg);
353 	}
354 }
355 
356 static int
357 virtiovf_set_notify_addr(struct virtiovf_pci_core_device *virtvdev)
358 {
359 	struct vfio_pci_core_device *core_device = &virtvdev->core_device;
360 	int ret;
361 
362 	/*
363 	 * Setup the BAR where the 'notify' exists to be used by vfio as well
364 	 * This will let us mmap it only once and use it when needed.
365 	 */
366 	ret = vfio_pci_core_setup_barmap(core_device,
367 					 virtvdev->notify_bar);
368 	if (ret)
369 		return ret;
370 
371 	virtvdev->notify_addr = core_device->barmap[virtvdev->notify_bar] +
372 			virtvdev->notify_offset;
373 	return 0;
374 }
375 
376 static int virtiovf_pci_open_device(struct vfio_device *core_vdev)
377 {
378 	struct virtiovf_pci_core_device *virtvdev = container_of(
379 		core_vdev, struct virtiovf_pci_core_device, core_device.vdev);
380 	struct vfio_pci_core_device *vdev = &virtvdev->core_device;
381 	int ret;
382 
383 	ret = vfio_pci_core_enable(vdev);
384 	if (ret)
385 		return ret;
386 
387 	if (virtvdev->bar0_virtual_buf) {
388 		/*
389 		 * Upon close_device() the vfio_pci_core_disable() is called
390 		 * and will close all the previous mmaps, so it seems that the
391 		 * valid life cycle for the 'notify' addr is per open/close.
392 		 */
393 		ret = virtiovf_set_notify_addr(virtvdev);
394 		if (ret) {
395 			vfio_pci_core_disable(vdev);
396 			return ret;
397 		}
398 	}
399 
400 	vfio_pci_core_finish_enable(vdev);
401 	return 0;
402 }
403 
404 static int virtiovf_get_device_config_size(unsigned short device)
405 {
406 	/* Network card */
407 	return offsetofend(struct virtio_net_config, status);
408 }
409 
410 static int virtiovf_read_notify_info(struct virtiovf_pci_core_device *virtvdev)
411 {
412 	u64 offset;
413 	int ret;
414 	u8 bar;
415 
416 	ret = virtio_pci_admin_legacy_io_notify_info(virtvdev->core_device.pdev,
417 				VIRTIO_ADMIN_CMD_NOTIFY_INFO_FLAGS_OWNER_MEM,
418 				&bar, &offset);
419 	if (ret)
420 		return ret;
421 
422 	virtvdev->notify_bar = bar;
423 	virtvdev->notify_offset = offset;
424 	return 0;
425 }
426 
427 static int virtiovf_pci_init_device(struct vfio_device *core_vdev)
428 {
429 	struct virtiovf_pci_core_device *virtvdev = container_of(
430 		core_vdev, struct virtiovf_pci_core_device, core_device.vdev);
431 	struct pci_dev *pdev;
432 	int ret;
433 
434 	ret = vfio_pci_core_init_dev(core_vdev);
435 	if (ret)
436 		return ret;
437 
438 	pdev = virtvdev->core_device.pdev;
439 	ret = virtiovf_read_notify_info(virtvdev);
440 	if (ret)
441 		return ret;
442 
443 	virtvdev->bar0_virtual_buf_size = VIRTIO_PCI_CONFIG_OFF(true) +
444 				virtiovf_get_device_config_size(pdev->device);
445 	BUILD_BUG_ON(!is_power_of_2(virtvdev->bar0_virtual_buf_size));
446 	virtvdev->bar0_virtual_buf = kzalloc(virtvdev->bar0_virtual_buf_size,
447 					     GFP_KERNEL);
448 	if (!virtvdev->bar0_virtual_buf)
449 		return -ENOMEM;
450 	mutex_init(&virtvdev->bar_mutex);
451 	return 0;
452 }
453 
454 static void virtiovf_pci_core_release_dev(struct vfio_device *core_vdev)
455 {
456 	struct virtiovf_pci_core_device *virtvdev = container_of(
457 		core_vdev, struct virtiovf_pci_core_device, core_device.vdev);
458 
459 	kfree(virtvdev->bar0_virtual_buf);
460 	vfio_pci_core_release_dev(core_vdev);
461 }
462 
463 static const struct vfio_device_ops virtiovf_vfio_pci_tran_ops = {
464 	.name = "virtio-vfio-pci-trans",
465 	.init = virtiovf_pci_init_device,
466 	.release = virtiovf_pci_core_release_dev,
467 	.open_device = virtiovf_pci_open_device,
468 	.close_device = vfio_pci_core_close_device,
469 	.ioctl = virtiovf_vfio_pci_core_ioctl,
470 	.device_feature = vfio_pci_core_ioctl_feature,
471 	.read = virtiovf_pci_core_read,
472 	.write = virtiovf_pci_core_write,
473 	.mmap = vfio_pci_core_mmap,
474 	.request = vfio_pci_core_request,
475 	.match = vfio_pci_core_match,
476 	.bind_iommufd = vfio_iommufd_physical_bind,
477 	.unbind_iommufd = vfio_iommufd_physical_unbind,
478 	.attach_ioas = vfio_iommufd_physical_attach_ioas,
479 	.detach_ioas = vfio_iommufd_physical_detach_ioas,
480 };
481 
482 static const struct vfio_device_ops virtiovf_vfio_pci_ops = {
483 	.name = "virtio-vfio-pci",
484 	.init = vfio_pci_core_init_dev,
485 	.release = vfio_pci_core_release_dev,
486 	.open_device = virtiovf_pci_open_device,
487 	.close_device = vfio_pci_core_close_device,
488 	.ioctl = vfio_pci_core_ioctl,
489 	.device_feature = vfio_pci_core_ioctl_feature,
490 	.read = vfio_pci_core_read,
491 	.write = vfio_pci_core_write,
492 	.mmap = vfio_pci_core_mmap,
493 	.request = vfio_pci_core_request,
494 	.match = vfio_pci_core_match,
495 	.bind_iommufd = vfio_iommufd_physical_bind,
496 	.unbind_iommufd = vfio_iommufd_physical_unbind,
497 	.attach_ioas = vfio_iommufd_physical_attach_ioas,
498 	.detach_ioas = vfio_iommufd_physical_detach_ioas,
499 };
500 
501 static bool virtiovf_bar0_exists(struct pci_dev *pdev)
502 {
503 	struct resource *res = pdev->resource;
504 
505 	return res->flags;
506 }
507 
508 static int virtiovf_pci_probe(struct pci_dev *pdev,
509 			      const struct pci_device_id *id)
510 {
511 	const struct vfio_device_ops *ops = &virtiovf_vfio_pci_ops;
512 	struct virtiovf_pci_core_device *virtvdev;
513 	int ret;
514 
515 	if (pdev->is_virtfn && virtio_pci_admin_has_legacy_io(pdev) &&
516 	    !virtiovf_bar0_exists(pdev))
517 		ops = &virtiovf_vfio_pci_tran_ops;
518 
519 	virtvdev = vfio_alloc_device(virtiovf_pci_core_device, core_device.vdev,
520 				     &pdev->dev, ops);
521 	if (IS_ERR(virtvdev))
522 		return PTR_ERR(virtvdev);
523 
524 	dev_set_drvdata(&pdev->dev, &virtvdev->core_device);
525 	ret = vfio_pci_core_register_device(&virtvdev->core_device);
526 	if (ret)
527 		goto out;
528 	return 0;
529 out:
530 	vfio_put_device(&virtvdev->core_device.vdev);
531 	return ret;
532 }
533 
534 static void virtiovf_pci_remove(struct pci_dev *pdev)
535 {
536 	struct virtiovf_pci_core_device *virtvdev = dev_get_drvdata(&pdev->dev);
537 
538 	vfio_pci_core_unregister_device(&virtvdev->core_device);
539 	vfio_put_device(&virtvdev->core_device.vdev);
540 }
541 
542 static const struct pci_device_id virtiovf_pci_table[] = {
543 	/* Only virtio-net is supported/tested so far */
544 	{ PCI_DRIVER_OVERRIDE_DEVICE_VFIO(PCI_VENDOR_ID_REDHAT_QUMRANET, 0x1041) },
545 	{}
546 };
547 
548 MODULE_DEVICE_TABLE(pci, virtiovf_pci_table);
549 
550 static void virtiovf_pci_aer_reset_done(struct pci_dev *pdev)
551 {
552 	struct virtiovf_pci_core_device *virtvdev = dev_get_drvdata(&pdev->dev);
553 
554 	virtvdev->pci_cmd = 0;
555 }
556 
557 static const struct pci_error_handlers virtiovf_err_handlers = {
558 	.reset_done = virtiovf_pci_aer_reset_done,
559 	.error_detected = vfio_pci_core_aer_err_detected,
560 };
561 
562 static struct pci_driver virtiovf_pci_driver = {
563 	.name = KBUILD_MODNAME,
564 	.id_table = virtiovf_pci_table,
565 	.probe = virtiovf_pci_probe,
566 	.remove = virtiovf_pci_remove,
567 	.err_handler = &virtiovf_err_handlers,
568 	.driver_managed_dma = true,
569 };
570 
571 module_pci_driver(virtiovf_pci_driver);
572 
573 MODULE_LICENSE("GPL");
574 MODULE_AUTHOR("Yishai Hadas <yishaih@nvidia.com>");
575 MODULE_DESCRIPTION(
576 	"VIRTIO VFIO PCI - User Level meta-driver for VIRTIO NET devices");
577