]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/vfio/pci/vfio_pci.c
vfio-pci: Use mutex around open, release, and remove
[mirror_ubuntu-artful-kernel.git] / drivers / vfio / pci / vfio_pci.c
CommitLineData
89e1f7d4
AW
1/*
2 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
3 * Author: Alex Williamson <alex.williamson@redhat.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Derived from original vfio:
10 * Copyright 2010 Cisco Systems, Inc. All rights reserved.
11 * Author: Tom Lyon, pugs@cisco.com
12 */
13
14#include <linux/device.h>
15#include <linux/eventfd.h>
8b27ee60 16#include <linux/file.h>
89e1f7d4
AW
17#include <linux/interrupt.h>
18#include <linux/iommu.h>
19#include <linux/module.h>
20#include <linux/mutex.h>
21#include <linux/notifier.h>
22#include <linux/pci.h>
23#include <linux/pm_runtime.h>
24#include <linux/slab.h>
25#include <linux/types.h>
26#include <linux/uaccess.h>
27#include <linux/vfio.h>
28
29#include "vfio_pci_private.h"
30
31#define DRIVER_VERSION "0.2"
32#define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>"
33#define DRIVER_DESC "VFIO PCI - User Level meta-driver"
34
35static bool nointxmask;
36module_param_named(nointxmask, nointxmask, bool, S_IRUGO | S_IWUSR);
37MODULE_PARM_DESC(nointxmask,
38 "Disable support for PCI 2.3 style INTx masking. If this resolves problems for specific devices, report lspci -vvvxxx to linux-pci@vger.kernel.org so the device can be fixed automatically via the broken_intx_masking flag.");
39
61d79256
AW
40static DEFINE_MUTEX(driver_lock);
41
89e1f7d4
AW
42static int vfio_pci_enable(struct vfio_pci_device *vdev)
43{
44 struct pci_dev *pdev = vdev->pdev;
45 int ret;
46 u16 cmd;
47 u8 msix_pos;
48
9c22e660
AW
49 /* Don't allow our initial saved state to include busmaster */
50 pci_clear_master(pdev);
51
9a92c509
AW
52 ret = pci_enable_device(pdev);
53 if (ret)
54 return ret;
55
89e1f7d4
AW
56 vdev->reset_works = (pci_reset_function(pdev) == 0);
57 pci_save_state(pdev);
58 vdev->pci_saved_state = pci_store_saved_state(pdev);
59 if (!vdev->pci_saved_state)
60 pr_debug("%s: Couldn't store %s saved state\n",
61 __func__, dev_name(&pdev->dev));
62
63 ret = vfio_config_init(vdev);
9a92c509 64 if (ret) {
eb5685f0
AW
65 kfree(vdev->pci_saved_state);
66 vdev->pci_saved_state = NULL;
9a92c509
AW
67 pci_disable_device(pdev);
68 return ret;
69 }
89e1f7d4
AW
70
71 if (likely(!nointxmask))
72 vdev->pci_2_3 = pci_intx_mask_supported(pdev);
73
74 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
75 if (vdev->pci_2_3 && (cmd & PCI_COMMAND_INTX_DISABLE)) {
76 cmd &= ~PCI_COMMAND_INTX_DISABLE;
77 pci_write_config_word(pdev, PCI_COMMAND, cmd);
78 }
79
a9047f24 80 msix_pos = pdev->msix_cap;
89e1f7d4
AW
81 if (msix_pos) {
82 u16 flags;
83 u32 table;
84
85 pci_read_config_word(pdev, msix_pos + PCI_MSIX_FLAGS, &flags);
86 pci_read_config_dword(pdev, msix_pos + PCI_MSIX_TABLE, &table);
87
508d1aa6
BH
88 vdev->msix_bar = table & PCI_MSIX_TABLE_BIR;
89 vdev->msix_offset = table & PCI_MSIX_TABLE_OFFSET;
89e1f7d4
AW
90 vdev->msix_size = ((flags & PCI_MSIX_FLAGS_QSIZE) + 1) * 16;
91 } else
92 vdev->msix_bar = 0xFF;
93
84237a82
AW
94#ifdef CONFIG_VFIO_PCI_VGA
95 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
96 vdev->has_vga = true;
97#endif
98
9a92c509 99 return 0;
89e1f7d4
AW
100}
101
102static void vfio_pci_disable(struct vfio_pci_device *vdev)
103{
2007722a 104 struct pci_dev *pdev = vdev->pdev;
89e1f7d4
AW
105 int bar;
106
9c22e660
AW
107 /* Stop the device from further DMA */
108 pci_clear_master(pdev);
89e1f7d4
AW
109
110 vfio_pci_set_irqs_ioctl(vdev, VFIO_IRQ_SET_DATA_NONE |
111 VFIO_IRQ_SET_ACTION_TRIGGER,
112 vdev->irq_type, 0, 0, NULL);
113
114 vdev->virq_disabled = false;
115
116 vfio_config_free(vdev);
117
89e1f7d4
AW
118 for (bar = PCI_STD_RESOURCES; bar <= PCI_STD_RESOURCE_END; bar++) {
119 if (!vdev->barmap[bar])
120 continue;
2007722a
AW
121 pci_iounmap(pdev, vdev->barmap[bar]);
122 pci_release_selected_regions(pdev, 1 << bar);
89e1f7d4
AW
123 vdev->barmap[bar] = NULL;
124 }
2007722a
AW
125
126 /*
127 * If we have saved state, restore it. If we can reset the device,
128 * even better. Resetting with current state seems better than
129 * nothing, but saving and restoring current state without reset
130 * is just busy work.
131 */
132 if (pci_load_and_free_saved_state(pdev, &vdev->pci_saved_state)) {
133 pr_info("%s: Couldn't reload %s saved state\n",
134 __func__, dev_name(&pdev->dev));
135
136 if (!vdev->reset_works)
9c22e660 137 goto out;
2007722a
AW
138
139 pci_save_state(pdev);
140 }
141
142 /*
143 * Disable INTx and MSI, presumably to avoid spurious interrupts
144 * during reset. Stolen from pci_reset_function()
145 */
146 pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
147
d24cdbfd 148 /*
890ed578
AW
149 * Try to reset the device. The success of this is dependent on
150 * being able to lock the device, which is not always possible.
d24cdbfd
AW
151 */
152 if (vdev->reset_works) {
890ed578
AW
153 int ret = pci_try_reset_function(pdev);
154 if (ret)
155 pr_warn("%s: Failed to reset device %s (%d)\n",
156 __func__, dev_name(&pdev->dev), ret);
d24cdbfd 157 }
2007722a
AW
158
159 pci_restore_state(pdev);
9c22e660
AW
160out:
161 pci_disable_device(pdev);
89e1f7d4
AW
162}
163
164static void vfio_pci_release(void *device_data)
165{
166 struct vfio_pci_device *vdev = device_data;
167
61d79256
AW
168 mutex_lock(&driver_lock);
169
170 if (!(--vdev->refcnt)) {
1b69be5e 171 vfio_spapr_pci_eeh_release(vdev->pdev);
89e1f7d4 172 vfio_pci_disable(vdev);
1b69be5e 173 }
89e1f7d4 174
61d79256
AW
175 mutex_unlock(&driver_lock);
176
89e1f7d4
AW
177 module_put(THIS_MODULE);
178}
179
180static int vfio_pci_open(void *device_data)
181{
182 struct vfio_pci_device *vdev = device_data;
61d79256 183 int ret = 0;
89e1f7d4
AW
184
185 if (!try_module_get(THIS_MODULE))
186 return -ENODEV;
187
61d79256
AW
188 mutex_lock(&driver_lock);
189
190 if (!vdev->refcnt) {
1b69be5e
GS
191 ret = vfio_pci_enable(vdev);
192 if (ret)
193 goto error;
194
195 ret = vfio_spapr_pci_eeh_open(vdev->pdev);
89e1f7d4 196 if (ret) {
1b69be5e
GS
197 vfio_pci_disable(vdev);
198 goto error;
89e1f7d4
AW
199 }
200 }
61d79256 201 vdev->refcnt++;
1b69be5e 202error:
61d79256
AW
203 mutex_unlock(&driver_lock);
204 if (ret)
205 module_put(THIS_MODULE);
1b69be5e 206 return ret;
89e1f7d4
AW
207}
208
209static int vfio_pci_get_irq_count(struct vfio_pci_device *vdev, int irq_type)
210{
211 if (irq_type == VFIO_PCI_INTX_IRQ_INDEX) {
212 u8 pin;
213 pci_read_config_byte(vdev->pdev, PCI_INTERRUPT_PIN, &pin);
214 if (pin)
215 return 1;
216
217 } else if (irq_type == VFIO_PCI_MSI_IRQ_INDEX) {
218 u8 pos;
219 u16 flags;
220
a9047f24 221 pos = vdev->pdev->msi_cap;
89e1f7d4
AW
222 if (pos) {
223 pci_read_config_word(vdev->pdev,
224 pos + PCI_MSI_FLAGS, &flags);
fd49c81f 225 return 1 << ((flags & PCI_MSI_FLAGS_QMASK) >> 1);
89e1f7d4
AW
226 }
227 } else if (irq_type == VFIO_PCI_MSIX_IRQ_INDEX) {
228 u8 pos;
229 u16 flags;
230
a9047f24 231 pos = vdev->pdev->msix_cap;
89e1f7d4
AW
232 if (pos) {
233 pci_read_config_word(vdev->pdev,
234 pos + PCI_MSIX_FLAGS, &flags);
235
236 return (flags & PCI_MSIX_FLAGS_QSIZE) + 1;
237 }
dad9f897
VMP
238 } else if (irq_type == VFIO_PCI_ERR_IRQ_INDEX)
239 if (pci_is_pcie(vdev->pdev))
240 return 1;
89e1f7d4
AW
241
242 return 0;
243}
244
8b27ee60
AW
245static int vfio_pci_count_devs(struct pci_dev *pdev, void *data)
246{
247 (*(int *)data)++;
248 return 0;
249}
250
251struct vfio_pci_fill_info {
252 int max;
253 int cur;
254 struct vfio_pci_dependent_device *devices;
255};
256
257static int vfio_pci_fill_devs(struct pci_dev *pdev, void *data)
258{
259 struct vfio_pci_fill_info *fill = data;
260 struct iommu_group *iommu_group;
261
262 if (fill->cur == fill->max)
263 return -EAGAIN; /* Something changed, try again */
264
265 iommu_group = iommu_group_get(&pdev->dev);
266 if (!iommu_group)
267 return -EPERM; /* Cannot reset non-isolated devices */
268
269 fill->devices[fill->cur].group_id = iommu_group_id(iommu_group);
270 fill->devices[fill->cur].segment = pci_domain_nr(pdev->bus);
271 fill->devices[fill->cur].bus = pdev->bus->number;
272 fill->devices[fill->cur].devfn = pdev->devfn;
273 fill->cur++;
274 iommu_group_put(iommu_group);
275 return 0;
276}
277
278struct vfio_pci_group_entry {
279 struct vfio_group *group;
280 int id;
281};
282
283struct vfio_pci_group_info {
284 int count;
285 struct vfio_pci_group_entry *groups;
286};
287
288static int vfio_pci_validate_devs(struct pci_dev *pdev, void *data)
289{
290 struct vfio_pci_group_info *info = data;
291 struct iommu_group *group;
292 int id, i;
293
294 group = iommu_group_get(&pdev->dev);
295 if (!group)
296 return -EPERM;
297
298 id = iommu_group_id(group);
299
300 for (i = 0; i < info->count; i++)
301 if (info->groups[i].id == id)
302 break;
303
304 iommu_group_put(group);
305
306 return (i == info->count) ? -EINVAL : 0;
307}
308
309static bool vfio_pci_dev_below_slot(struct pci_dev *pdev, struct pci_slot *slot)
310{
311 for (; pdev; pdev = pdev->bus->self)
312 if (pdev->bus == slot->bus)
313 return (pdev->slot == slot);
314 return false;
315}
316
317struct vfio_pci_walk_info {
318 int (*fn)(struct pci_dev *, void *data);
319 void *data;
320 struct pci_dev *pdev;
321 bool slot;
322 int ret;
323};
324
325static int vfio_pci_walk_wrapper(struct pci_dev *pdev, void *data)
326{
327 struct vfio_pci_walk_info *walk = data;
328
329 if (!walk->slot || vfio_pci_dev_below_slot(pdev, walk->pdev->slot))
330 walk->ret = walk->fn(pdev, walk->data);
331
332 return walk->ret;
333}
334
335static int vfio_pci_for_each_slot_or_bus(struct pci_dev *pdev,
336 int (*fn)(struct pci_dev *,
337 void *data), void *data,
338 bool slot)
339{
340 struct vfio_pci_walk_info walk = {
341 .fn = fn, .data = data, .pdev = pdev, .slot = slot, .ret = 0,
342 };
343
344 pci_walk_bus(pdev->bus, vfio_pci_walk_wrapper, &walk);
345
346 return walk.ret;
347}
348
89e1f7d4
AW
349static long vfio_pci_ioctl(void *device_data,
350 unsigned int cmd, unsigned long arg)
351{
352 struct vfio_pci_device *vdev = device_data;
353 unsigned long minsz;
354
355 if (cmd == VFIO_DEVICE_GET_INFO) {
356 struct vfio_device_info info;
357
358 minsz = offsetofend(struct vfio_device_info, num_irqs);
359
360 if (copy_from_user(&info, (void __user *)arg, minsz))
361 return -EFAULT;
362
363 if (info.argsz < minsz)
364 return -EINVAL;
365
366 info.flags = VFIO_DEVICE_FLAGS_PCI;
367
368 if (vdev->reset_works)
369 info.flags |= VFIO_DEVICE_FLAGS_RESET;
370
371 info.num_regions = VFIO_PCI_NUM_REGIONS;
372 info.num_irqs = VFIO_PCI_NUM_IRQS;
373
374 return copy_to_user((void __user *)arg, &info, minsz);
375
376 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
377 struct pci_dev *pdev = vdev->pdev;
378 struct vfio_region_info info;
379
380 minsz = offsetofend(struct vfio_region_info, offset);
381
382 if (copy_from_user(&info, (void __user *)arg, minsz))
383 return -EFAULT;
384
385 if (info.argsz < minsz)
386 return -EINVAL;
387
388 switch (info.index) {
389 case VFIO_PCI_CONFIG_REGION_INDEX:
390 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
391 info.size = pdev->cfg_size;
392 info.flags = VFIO_REGION_INFO_FLAG_READ |
393 VFIO_REGION_INFO_FLAG_WRITE;
394 break;
395 case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
396 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
397 info.size = pci_resource_len(pdev, info.index);
398 if (!info.size) {
399 info.flags = 0;
400 break;
401 }
402
403 info.flags = VFIO_REGION_INFO_FLAG_READ |
404 VFIO_REGION_INFO_FLAG_WRITE;
405 if (pci_resource_flags(pdev, info.index) &
406 IORESOURCE_MEM && info.size >= PAGE_SIZE)
407 info.flags |= VFIO_REGION_INFO_FLAG_MMAP;
408 break;
409 case VFIO_PCI_ROM_REGION_INDEX:
410 {
411 void __iomem *io;
412 size_t size;
413
414 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
415 info.flags = 0;
416
417 /* Report the BAR size, not the ROM size */
418 info.size = pci_resource_len(pdev, info.index);
419 if (!info.size)
420 break;
421
422 /* Is it really there? */
423 io = pci_map_rom(pdev, &size);
424 if (!io || !size) {
425 info.size = 0;
426 break;
427 }
428 pci_unmap_rom(pdev, io);
429
430 info.flags = VFIO_REGION_INFO_FLAG_READ;
431 break;
432 }
84237a82
AW
433 case VFIO_PCI_VGA_REGION_INDEX:
434 if (!vdev->has_vga)
435 return -EINVAL;
436
437 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
438 info.size = 0xc0000;
439 info.flags = VFIO_REGION_INFO_FLAG_READ |
440 VFIO_REGION_INFO_FLAG_WRITE;
441
442 break;
89e1f7d4
AW
443 default:
444 return -EINVAL;
445 }
446
447 return copy_to_user((void __user *)arg, &info, minsz);
448
449 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
450 struct vfio_irq_info info;
451
452 minsz = offsetofend(struct vfio_irq_info, count);
453
454 if (copy_from_user(&info, (void __user *)arg, minsz))
455 return -EFAULT;
456
457 if (info.argsz < minsz || info.index >= VFIO_PCI_NUM_IRQS)
458 return -EINVAL;
459
dad9f897
VMP
460 switch (info.index) {
461 case VFIO_PCI_INTX_IRQ_INDEX ... VFIO_PCI_MSIX_IRQ_INDEX:
462 break;
463 case VFIO_PCI_ERR_IRQ_INDEX:
464 if (pci_is_pcie(vdev->pdev))
465 break;
466 /* pass thru to return error */
467 default:
468 return -EINVAL;
469 }
470
89e1f7d4
AW
471 info.flags = VFIO_IRQ_INFO_EVENTFD;
472
473 info.count = vfio_pci_get_irq_count(vdev, info.index);
474
475 if (info.index == VFIO_PCI_INTX_IRQ_INDEX)
476 info.flags |= (VFIO_IRQ_INFO_MASKABLE |
477 VFIO_IRQ_INFO_AUTOMASKED);
478 else
479 info.flags |= VFIO_IRQ_INFO_NORESIZE;
480
481 return copy_to_user((void __user *)arg, &info, minsz);
482
483 } else if (cmd == VFIO_DEVICE_SET_IRQS) {
484 struct vfio_irq_set hdr;
485 u8 *data = NULL;
486 int ret = 0;
487
488 minsz = offsetofend(struct vfio_irq_set, count);
489
490 if (copy_from_user(&hdr, (void __user *)arg, minsz))
491 return -EFAULT;
492
493 if (hdr.argsz < minsz || hdr.index >= VFIO_PCI_NUM_IRQS ||
494 hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
495 VFIO_IRQ_SET_ACTION_TYPE_MASK))
496 return -EINVAL;
497
498 if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
499 size_t size;
904c680c 500 int max = vfio_pci_get_irq_count(vdev, hdr.index);
89e1f7d4
AW
501
502 if (hdr.flags & VFIO_IRQ_SET_DATA_BOOL)
503 size = sizeof(uint8_t);
504 else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD)
505 size = sizeof(int32_t);
506 else
507 return -EINVAL;
508
509 if (hdr.argsz - minsz < hdr.count * size ||
904c680c 510 hdr.start >= max || hdr.start + hdr.count > max)
89e1f7d4
AW
511 return -EINVAL;
512
3a1f7041
FW
513 data = memdup_user((void __user *)(arg + minsz),
514 hdr.count * size);
515 if (IS_ERR(data))
516 return PTR_ERR(data);
89e1f7d4
AW
517 }
518
519 mutex_lock(&vdev->igate);
520
521 ret = vfio_pci_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
522 hdr.start, hdr.count, data);
523
524 mutex_unlock(&vdev->igate);
525 kfree(data);
526
527 return ret;
528
8b27ee60 529 } else if (cmd == VFIO_DEVICE_RESET) {
89e1f7d4 530 return vdev->reset_works ?
890ed578 531 pci_try_reset_function(vdev->pdev) : -EINVAL;
89e1f7d4 532
8b27ee60
AW
533 } else if (cmd == VFIO_DEVICE_GET_PCI_HOT_RESET_INFO) {
534 struct vfio_pci_hot_reset_info hdr;
535 struct vfio_pci_fill_info fill = { 0 };
536 struct vfio_pci_dependent_device *devices = NULL;
537 bool slot = false;
538 int ret = 0;
539
540 minsz = offsetofend(struct vfio_pci_hot_reset_info, count);
541
542 if (copy_from_user(&hdr, (void __user *)arg, minsz))
543 return -EFAULT;
544
545 if (hdr.argsz < minsz)
546 return -EINVAL;
547
548 hdr.flags = 0;
549
550 /* Can we do a slot or bus reset or neither? */
551 if (!pci_probe_reset_slot(vdev->pdev->slot))
552 slot = true;
553 else if (pci_probe_reset_bus(vdev->pdev->bus))
554 return -ENODEV;
555
556 /* How many devices are affected? */
557 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
558 vfio_pci_count_devs,
559 &fill.max, slot);
560 if (ret)
561 return ret;
562
563 WARN_ON(!fill.max); /* Should always be at least one */
564
565 /*
566 * If there's enough space, fill it now, otherwise return
567 * -ENOSPC and the number of devices affected.
568 */
569 if (hdr.argsz < sizeof(hdr) + (fill.max * sizeof(*devices))) {
570 ret = -ENOSPC;
571 hdr.count = fill.max;
572 goto reset_info_exit;
573 }
574
575 devices = kcalloc(fill.max, sizeof(*devices), GFP_KERNEL);
576 if (!devices)
577 return -ENOMEM;
578
579 fill.devices = devices;
580
581 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
582 vfio_pci_fill_devs,
583 &fill, slot);
584
585 /*
586 * If a device was removed between counting and filling,
587 * we may come up short of fill.max. If a device was
588 * added, we'll have a return of -EAGAIN above.
589 */
590 if (!ret)
591 hdr.count = fill.cur;
592
593reset_info_exit:
594 if (copy_to_user((void __user *)arg, &hdr, minsz))
595 ret = -EFAULT;
596
597 if (!ret) {
598 if (copy_to_user((void __user *)(arg + minsz), devices,
599 hdr.count * sizeof(*devices)))
600 ret = -EFAULT;
601 }
602
603 kfree(devices);
604 return ret;
605
606 } else if (cmd == VFIO_DEVICE_PCI_HOT_RESET) {
607 struct vfio_pci_hot_reset hdr;
608 int32_t *group_fds;
609 struct vfio_pci_group_entry *groups;
610 struct vfio_pci_group_info info;
611 bool slot = false;
612 int i, count = 0, ret = 0;
613
614 minsz = offsetofend(struct vfio_pci_hot_reset, count);
615
616 if (copy_from_user(&hdr, (void __user *)arg, minsz))
617 return -EFAULT;
618
619 if (hdr.argsz < minsz || hdr.flags)
620 return -EINVAL;
621
622 /* Can we do a slot or bus reset or neither? */
623 if (!pci_probe_reset_slot(vdev->pdev->slot))
624 slot = true;
625 else if (pci_probe_reset_bus(vdev->pdev->bus))
626 return -ENODEV;
627
628 /*
629 * We can't let userspace give us an arbitrarily large
630 * buffer to copy, so verify how many we think there
631 * could be. Note groups can have multiple devices so
632 * one group per device is the max.
633 */
634 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
635 vfio_pci_count_devs,
636 &count, slot);
637 if (ret)
638 return ret;
639
640 /* Somewhere between 1 and count is OK */
641 if (!hdr.count || hdr.count > count)
642 return -EINVAL;
643
644 group_fds = kcalloc(hdr.count, sizeof(*group_fds), GFP_KERNEL);
645 groups = kcalloc(hdr.count, sizeof(*groups), GFP_KERNEL);
646 if (!group_fds || !groups) {
647 kfree(group_fds);
648 kfree(groups);
649 return -ENOMEM;
650 }
651
652 if (copy_from_user(group_fds, (void __user *)(arg + minsz),
653 hdr.count * sizeof(*group_fds))) {
654 kfree(group_fds);
655 kfree(groups);
656 return -EFAULT;
657 }
658
659 /*
660 * For each group_fd, get the group through the vfio external
661 * user interface and store the group and iommu ID. This
662 * ensures the group is held across the reset.
663 */
664 for (i = 0; i < hdr.count; i++) {
665 struct vfio_group *group;
666 struct fd f = fdget(group_fds[i]);
667 if (!f.file) {
668 ret = -EBADF;
669 break;
670 }
671
672 group = vfio_group_get_external_user(f.file);
673 fdput(f);
674 if (IS_ERR(group)) {
675 ret = PTR_ERR(group);
676 break;
677 }
678
679 groups[i].group = group;
680 groups[i].id = vfio_external_user_iommu_id(group);
681 }
682
683 kfree(group_fds);
684
685 /* release reference to groups on error */
686 if (ret)
687 goto hot_reset_release;
688
689 info.count = hdr.count;
690 info.groups = groups;
691
692 /*
693 * Test whether all the affected devices are contained
694 * by the set of groups provided by the user.
695 */
696 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
697 vfio_pci_validate_devs,
698 &info, slot);
699 if (!ret)
700 /* User has access, do the reset */
890ed578
AW
701 ret = slot ? pci_try_reset_slot(vdev->pdev->slot) :
702 pci_try_reset_bus(vdev->pdev->bus);
8b27ee60
AW
703
704hot_reset_release:
705 for (i--; i >= 0; i--)
706 vfio_group_put_external_user(groups[i].group);
707
708 kfree(groups);
709 return ret;
710 }
711
89e1f7d4
AW
712 return -ENOTTY;
713}
714
5b279a11
AW
715static ssize_t vfio_pci_rw(void *device_data, char __user *buf,
716 size_t count, loff_t *ppos, bool iswrite)
89e1f7d4
AW
717{
718 unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
719 struct vfio_pci_device *vdev = device_data;
89e1f7d4
AW
720
721 if (index >= VFIO_PCI_NUM_REGIONS)
722 return -EINVAL;
723
5b279a11
AW
724 switch (index) {
725 case VFIO_PCI_CONFIG_REGION_INDEX:
906ee99d
AW
726 return vfio_pci_config_rw(vdev, buf, count, ppos, iswrite);
727
5b279a11
AW
728 case VFIO_PCI_ROM_REGION_INDEX:
729 if (iswrite)
730 return -EINVAL;
906ee99d 731 return vfio_pci_bar_rw(vdev, buf, count, ppos, false);
89e1f7d4 732
5b279a11 733 case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
906ee99d 734 return vfio_pci_bar_rw(vdev, buf, count, ppos, iswrite);
84237a82
AW
735
736 case VFIO_PCI_VGA_REGION_INDEX:
737 return vfio_pci_vga_rw(vdev, buf, count, ppos, iswrite);
5b279a11
AW
738 }
739
89e1f7d4
AW
740 return -EINVAL;
741}
742
5b279a11
AW
743static ssize_t vfio_pci_read(void *device_data, char __user *buf,
744 size_t count, loff_t *ppos)
745{
906ee99d
AW
746 if (!count)
747 return 0;
748
5b279a11
AW
749 return vfio_pci_rw(device_data, buf, count, ppos, false);
750}
751
89e1f7d4
AW
752static ssize_t vfio_pci_write(void *device_data, const char __user *buf,
753 size_t count, loff_t *ppos)
754{
906ee99d
AW
755 if (!count)
756 return 0;
757
758 return vfio_pci_rw(device_data, (char __user *)buf, count, ppos, true);
89e1f7d4
AW
759}
760
761static int vfio_pci_mmap(void *device_data, struct vm_area_struct *vma)
762{
763 struct vfio_pci_device *vdev = device_data;
764 struct pci_dev *pdev = vdev->pdev;
765 unsigned int index;
34002f54 766 u64 phys_len, req_len, pgoff, req_start;
89e1f7d4
AW
767 int ret;
768
769 index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
770
771 if (vma->vm_end < vma->vm_start)
772 return -EINVAL;
773 if ((vma->vm_flags & VM_SHARED) == 0)
774 return -EINVAL;
775 if (index >= VFIO_PCI_ROM_REGION_INDEX)
776 return -EINVAL;
777 if (!(pci_resource_flags(pdev, index) & IORESOURCE_MEM))
778 return -EINVAL;
779
780 phys_len = pci_resource_len(pdev, index);
781 req_len = vma->vm_end - vma->vm_start;
782 pgoff = vma->vm_pgoff &
783 ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
784 req_start = pgoff << PAGE_SHIFT;
785
786 if (phys_len < PAGE_SIZE || req_start + req_len > phys_len)
787 return -EINVAL;
788
789 if (index == vdev->msix_bar) {
790 /*
791 * Disallow mmaps overlapping the MSI-X table; users don't
792 * get to touch this directly. We could find somewhere
793 * else to map the overlap, but page granularity is only
794 * a recommendation, not a requirement, so the user needs
795 * to know which bits are real. Requiring them to mmap
796 * around the table makes that clear.
797 */
798
799 /* If neither entirely above nor below, then it overlaps */
800 if (!(req_start >= vdev->msix_offset + vdev->msix_size ||
801 req_start + req_len <= vdev->msix_offset))
802 return -EINVAL;
803 }
804
805 /*
806 * Even though we don't make use of the barmap for the mmap,
807 * we need to request the region and the barmap tracks that.
808 */
809 if (!vdev->barmap[index]) {
810 ret = pci_request_selected_regions(pdev,
811 1 << index, "vfio-pci");
812 if (ret)
813 return ret;
814
815 vdev->barmap[index] = pci_iomap(pdev, index, 0);
816 }
817
818 vma->vm_private_data = vdev;
89e1f7d4 819 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
34002f54 820 vma->vm_pgoff = (pci_resource_start(pdev, index) >> PAGE_SHIFT) + pgoff;
89e1f7d4 821
34002f54 822 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
89e1f7d4
AW
823 req_len, vma->vm_page_prot);
824}
825
826static const struct vfio_device_ops vfio_pci_ops = {
827 .name = "vfio-pci",
828 .open = vfio_pci_open,
829 .release = vfio_pci_release,
830 .ioctl = vfio_pci_ioctl,
831 .read = vfio_pci_read,
832 .write = vfio_pci_write,
833 .mmap = vfio_pci_mmap,
834};
835
836static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
837{
838 u8 type;
839 struct vfio_pci_device *vdev;
840 struct iommu_group *group;
841 int ret;
842
843 pci_read_config_byte(pdev, PCI_HEADER_TYPE, &type);
844 if ((type & PCI_HEADER_TYPE) != PCI_HEADER_TYPE_NORMAL)
845 return -EINVAL;
846
847 group = iommu_group_get(&pdev->dev);
848 if (!group)
849 return -EINVAL;
850
851 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
852 if (!vdev) {
853 iommu_group_put(group);
854 return -ENOMEM;
855 }
856
857 vdev->pdev = pdev;
858 vdev->irq_type = VFIO_PCI_NUM_IRQS;
859 mutex_init(&vdev->igate);
860 spin_lock_init(&vdev->irqlock);
89e1f7d4
AW
861
862 ret = vfio_add_group_dev(&pdev->dev, &vfio_pci_ops, vdev);
863 if (ret) {
864 iommu_group_put(group);
865 kfree(vdev);
866 }
867
868 return ret;
869}
870
871static void vfio_pci_remove(struct pci_dev *pdev)
872{
873 struct vfio_pci_device *vdev;
874
61d79256
AW
875 mutex_lock(&driver_lock);
876
89e1f7d4 877 vdev = vfio_del_group_dev(&pdev->dev);
61d79256
AW
878 if (vdev) {
879 iommu_group_put(pdev->dev.iommu_group);
880 kfree(vdev);
881 }
89e1f7d4 882
61d79256 883 mutex_unlock(&driver_lock);
89e1f7d4
AW
884}
885
dad9f897
VMP
886static pci_ers_result_t vfio_pci_aer_err_detected(struct pci_dev *pdev,
887 pci_channel_state_t state)
888{
889 struct vfio_pci_device *vdev;
890 struct vfio_device *device;
891
892 device = vfio_device_get_from_dev(&pdev->dev);
893 if (device == NULL)
894 return PCI_ERS_RESULT_DISCONNECT;
895
896 vdev = vfio_device_data(device);
897 if (vdev == NULL) {
898 vfio_device_put(device);
899 return PCI_ERS_RESULT_DISCONNECT;
900 }
901
3be3a074
AW
902 mutex_lock(&vdev->igate);
903
dad9f897
VMP
904 if (vdev->err_trigger)
905 eventfd_signal(vdev->err_trigger, 1);
906
3be3a074
AW
907 mutex_unlock(&vdev->igate);
908
dad9f897
VMP
909 vfio_device_put(device);
910
911 return PCI_ERS_RESULT_CAN_RECOVER;
912}
913
914static struct pci_error_handlers vfio_err_handlers = {
915 .error_detected = vfio_pci_aer_err_detected,
916};
917
89e1f7d4
AW
918static struct pci_driver vfio_pci_driver = {
919 .name = "vfio-pci",
920 .id_table = NULL, /* only dynamic ids */
921 .probe = vfio_pci_probe,
922 .remove = vfio_pci_remove,
dad9f897 923 .err_handler = &vfio_err_handlers,
89e1f7d4
AW
924};
925
926static void __exit vfio_pci_cleanup(void)
927{
928 pci_unregister_driver(&vfio_pci_driver);
929 vfio_pci_virqfd_exit();
930 vfio_pci_uninit_perm_bits();
931}
932
933static int __init vfio_pci_init(void)
934{
935 int ret;
936
937 /* Allocate shared config space permision data used by all devices */
938 ret = vfio_pci_init_perm_bits();
939 if (ret)
940 return ret;
941
942 /* Start the virqfd cleanup handler */
943 ret = vfio_pci_virqfd_init();
944 if (ret)
945 goto out_virqfd;
946
947 /* Register and scan for devices */
948 ret = pci_register_driver(&vfio_pci_driver);
949 if (ret)
950 goto out_driver;
951
952 return 0;
953
89e1f7d4 954out_driver:
05bf3aac
JL
955 vfio_pci_virqfd_exit();
956out_virqfd:
89e1f7d4
AW
957 vfio_pci_uninit_perm_bits();
958 return ret;
959}
960
961module_init(vfio_pci_init);
962module_exit(vfio_pci_cleanup);
963
964MODULE_VERSION(DRIVER_VERSION);
965MODULE_LICENSE("GPL v2");
966MODULE_AUTHOR(DRIVER_AUTHOR);
967MODULE_DESCRIPTION(DRIVER_DESC);