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