blob: 25aef05b8692dfe2417174179778bdac590558b3 [file] [log] [blame]
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001/*
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>
Alex Williamson8b27ee62013-09-04 11:28:04 -060016#include <linux/file.h>
Alex Williamson89e1f7d2012-07-31 08:16:24 -060017#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>
Alex Williamsonecaa1f62015-04-07 11:14:41 -060028#include <linux/vgaarb.h>
Alex Williamson89e1f7d2012-07-31 08:16:24 -060029
30#include "vfio_pci_private.h"
31
32#define DRIVER_VERSION "0.2"
33#define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>"
34#define DRIVER_DESC "VFIO PCI - User Level meta-driver"
35
36static bool nointxmask;
37module_param_named(nointxmask, nointxmask, bool, S_IRUGO | S_IWUSR);
38MODULE_PARM_DESC(nointxmask,
39 "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.");
40
Alex Williamson88c0dead2015-04-07 11:14:40 -060041#ifdef CONFIG_VFIO_PCI_VGA
42static bool disable_vga;
43module_param(disable_vga, bool, S_IRUGO);
44MODULE_PARM_DESC(disable_vga, "Disable VGA resource access through vfio-pci");
45#endif
46
Alex Williamson61d79252014-08-07 11:12:04 -060047static DEFINE_MUTEX(driver_lock);
48
Alex Williamson88c0dead2015-04-07 11:14:40 -060049static inline bool vfio_vga_disabled(void)
50{
51#ifdef CONFIG_VFIO_PCI_VGA
52 return disable_vga;
53#else
54 return true;
55#endif
56}
57
Alex Williamsonecaa1f62015-04-07 11:14:41 -060058/*
59 * Our VGA arbiter participation is limited since we don't know anything
60 * about the device itself. However, if the device is the only VGA device
61 * downstream of a bridge and VFIO VGA support is disabled, then we can
62 * safely return legacy VGA IO and memory as not decoded since the user
63 * has no way to get to it and routing can be disabled externally at the
64 * bridge.
65 */
66static unsigned int vfio_pci_set_vga_decode(void *opaque, bool single_vga)
67{
68 struct vfio_pci_device *vdev = opaque;
69 struct pci_dev *tmp = NULL, *pdev = vdev->pdev;
70 unsigned char max_busnr;
71 unsigned int decodes;
72
73 if (single_vga || !vfio_vga_disabled() || pci_is_root_bus(pdev->bus))
74 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM |
75 VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
76
77 max_busnr = pci_bus_max_busnr(pdev->bus);
78 decodes = VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
79
80 while ((tmp = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, tmp)) != NULL) {
81 if (tmp == pdev ||
82 pci_domain_nr(tmp->bus) != pci_domain_nr(pdev->bus) ||
83 pci_is_root_bus(tmp->bus))
84 continue;
85
86 if (tmp->bus->number >= pdev->bus->number &&
87 tmp->bus->number <= max_busnr) {
88 pci_dev_put(tmp);
89 decodes |= VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
90 break;
91 }
92 }
93
94 return decodes;
95}
96
97static inline bool vfio_pci_is_vga(struct pci_dev *pdev)
98{
99 return (pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA;
100}
101
Alex Williamsonbc4fba72014-08-07 11:12:07 -0600102static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev);
103
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600104static int vfio_pci_enable(struct vfio_pci_device *vdev)
105{
106 struct pci_dev *pdev = vdev->pdev;
107 int ret;
108 u16 cmd;
109 u8 msix_pos;
110
Alex Williamson9c22e662014-08-07 11:12:02 -0600111 /* Don't allow our initial saved state to include busmaster */
112 pci_clear_master(pdev);
113
Alex Williamson9a92c502012-12-07 13:43:51 -0700114 ret = pci_enable_device(pdev);
115 if (ret)
116 return ret;
117
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600118 vdev->reset_works = (pci_reset_function(pdev) == 0);
119 pci_save_state(pdev);
120 vdev->pci_saved_state = pci_store_saved_state(pdev);
121 if (!vdev->pci_saved_state)
122 pr_debug("%s: Couldn't store %s saved state\n",
123 __func__, dev_name(&pdev->dev));
124
125 ret = vfio_config_init(vdev);
Alex Williamson9a92c502012-12-07 13:43:51 -0700126 if (ret) {
Alex Williamsoneb5685f2014-05-30 11:35:53 -0600127 kfree(vdev->pci_saved_state);
128 vdev->pci_saved_state = NULL;
Alex Williamson9a92c502012-12-07 13:43:51 -0700129 pci_disable_device(pdev);
130 return ret;
131 }
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600132
133 if (likely(!nointxmask))
134 vdev->pci_2_3 = pci_intx_mask_supported(pdev);
135
136 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
137 if (vdev->pci_2_3 && (cmd & PCI_COMMAND_INTX_DISABLE)) {
138 cmd &= ~PCI_COMMAND_INTX_DISABLE;
139 pci_write_config_word(pdev, PCI_COMMAND, cmd);
140 }
141
Bjorn Helgaasa9047f22013-04-18 15:12:58 -0600142 msix_pos = pdev->msix_cap;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600143 if (msix_pos) {
144 u16 flags;
145 u32 table;
146
147 pci_read_config_word(pdev, msix_pos + PCI_MSIX_FLAGS, &flags);
148 pci_read_config_dword(pdev, msix_pos + PCI_MSIX_TABLE, &table);
149
Bjorn Helgaas508d1aa2013-04-18 12:42:58 -0600150 vdev->msix_bar = table & PCI_MSIX_TABLE_BIR;
151 vdev->msix_offset = table & PCI_MSIX_TABLE_OFFSET;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600152 vdev->msix_size = ((flags & PCI_MSIX_FLAGS_QSIZE) + 1) * 16;
153 } else
154 vdev->msix_bar = 0xFF;
155
Alex Williamsonecaa1f62015-04-07 11:14:41 -0600156 if (!vfio_vga_disabled() && vfio_pci_is_vga(pdev))
Alex Williamson84237a82013-02-18 10:11:13 -0700157 vdev->has_vga = true;
Alex Williamson84237a82013-02-18 10:11:13 -0700158
Alex Williamson9a92c502012-12-07 13:43:51 -0700159 return 0;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600160}
161
162static void vfio_pci_disable(struct vfio_pci_device *vdev)
163{
Alex Williamson20077222012-12-07 13:43:50 -0700164 struct pci_dev *pdev = vdev->pdev;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600165 int bar;
166
Alex Williamson9c22e662014-08-07 11:12:02 -0600167 /* Stop the device from further DMA */
168 pci_clear_master(pdev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600169
170 vfio_pci_set_irqs_ioctl(vdev, VFIO_IRQ_SET_DATA_NONE |
171 VFIO_IRQ_SET_ACTION_TRIGGER,
172 vdev->irq_type, 0, 0, NULL);
173
174 vdev->virq_disabled = false;
175
176 vfio_config_free(vdev);
177
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600178 for (bar = PCI_STD_RESOURCES; bar <= PCI_STD_RESOURCE_END; bar++) {
179 if (!vdev->barmap[bar])
180 continue;
Alex Williamson20077222012-12-07 13:43:50 -0700181 pci_iounmap(pdev, vdev->barmap[bar]);
182 pci_release_selected_regions(pdev, 1 << bar);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600183 vdev->barmap[bar] = NULL;
184 }
Alex Williamson20077222012-12-07 13:43:50 -0700185
Alex Williamsonbc4fba72014-08-07 11:12:07 -0600186 vdev->needs_reset = true;
187
Alex Williamson20077222012-12-07 13:43:50 -0700188 /*
189 * If we have saved state, restore it. If we can reset the device,
190 * even better. Resetting with current state seems better than
191 * nothing, but saving and restoring current state without reset
192 * is just busy work.
193 */
194 if (pci_load_and_free_saved_state(pdev, &vdev->pci_saved_state)) {
195 pr_info("%s: Couldn't reload %s saved state\n",
196 __func__, dev_name(&pdev->dev));
197
198 if (!vdev->reset_works)
Alex Williamson9c22e662014-08-07 11:12:02 -0600199 goto out;
Alex Williamson20077222012-12-07 13:43:50 -0700200
201 pci_save_state(pdev);
202 }
203
204 /*
205 * Disable INTx and MSI, presumably to avoid spurious interrupts
206 * during reset. Stolen from pci_reset_function()
207 */
208 pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
209
Alex Williamsond24cdbf2013-06-10 16:40:57 -0600210 /*
Alex Williamson890ed572014-01-14 20:45:09 -0700211 * Try to reset the device. The success of this is dependent on
212 * being able to lock the device, which is not always possible.
Alex Williamsond24cdbf2013-06-10 16:40:57 -0600213 */
214 if (vdev->reset_works) {
Alex Williamson890ed572014-01-14 20:45:09 -0700215 int ret = pci_try_reset_function(pdev);
216 if (ret)
217 pr_warn("%s: Failed to reset device %s (%d)\n",
218 __func__, dev_name(&pdev->dev), ret);
Alex Williamsonbc4fba72014-08-07 11:12:07 -0600219 else
220 vdev->needs_reset = false;
Alex Williamsond24cdbf2013-06-10 16:40:57 -0600221 }
Alex Williamson20077222012-12-07 13:43:50 -0700222
223 pci_restore_state(pdev);
Alex Williamson9c22e662014-08-07 11:12:02 -0600224out:
225 pci_disable_device(pdev);
Alex Williamsonbc4fba72014-08-07 11:12:07 -0600226
227 vfio_pci_try_bus_reset(vdev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600228}
229
230static void vfio_pci_release(void *device_data)
231{
232 struct vfio_pci_device *vdev = device_data;
233
Alex Williamson61d79252014-08-07 11:12:04 -0600234 mutex_lock(&driver_lock);
235
236 if (!(--vdev->refcnt)) {
Gavin Shan1b69be52014-06-10 11:41:57 +1000237 vfio_spapr_pci_eeh_release(vdev->pdev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600238 vfio_pci_disable(vdev);
Gavin Shan1b69be52014-06-10 11:41:57 +1000239 }
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600240
Alex Williamson61d79252014-08-07 11:12:04 -0600241 mutex_unlock(&driver_lock);
242
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600243 module_put(THIS_MODULE);
244}
245
246static int vfio_pci_open(void *device_data)
247{
248 struct vfio_pci_device *vdev = device_data;
Alex Williamson61d79252014-08-07 11:12:04 -0600249 int ret = 0;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600250
251 if (!try_module_get(THIS_MODULE))
252 return -ENODEV;
253
Alex Williamson61d79252014-08-07 11:12:04 -0600254 mutex_lock(&driver_lock);
255
256 if (!vdev->refcnt) {
Gavin Shan1b69be52014-06-10 11:41:57 +1000257 ret = vfio_pci_enable(vdev);
258 if (ret)
259 goto error;
260
Alexey Kardashevskiy9b936c92014-08-08 10:39:16 -0600261 vfio_spapr_pci_eeh_open(vdev->pdev);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600262 }
Alex Williamson61d79252014-08-07 11:12:04 -0600263 vdev->refcnt++;
Gavin Shan1b69be52014-06-10 11:41:57 +1000264error:
Alex Williamson61d79252014-08-07 11:12:04 -0600265 mutex_unlock(&driver_lock);
266 if (ret)
267 module_put(THIS_MODULE);
Gavin Shan1b69be52014-06-10 11:41:57 +1000268 return ret;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600269}
270
271static int vfio_pci_get_irq_count(struct vfio_pci_device *vdev, int irq_type)
272{
273 if (irq_type == VFIO_PCI_INTX_IRQ_INDEX) {
274 u8 pin;
275 pci_read_config_byte(vdev->pdev, PCI_INTERRUPT_PIN, &pin);
Frank Blaschka1d53a3a2014-11-07 09:52:22 -0700276 if (IS_ENABLED(CONFIG_VFIO_PCI_INTX) && pin)
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600277 return 1;
278
279 } else if (irq_type == VFIO_PCI_MSI_IRQ_INDEX) {
280 u8 pos;
281 u16 flags;
282
Bjorn Helgaasa9047f22013-04-18 15:12:58 -0600283 pos = vdev->pdev->msi_cap;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600284 if (pos) {
285 pci_read_config_word(vdev->pdev,
286 pos + PCI_MSI_FLAGS, &flags);
Gavin Shanfd49c812014-05-30 11:35:54 -0600287 return 1 << ((flags & PCI_MSI_FLAGS_QMASK) >> 1);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600288 }
289 } else if (irq_type == VFIO_PCI_MSIX_IRQ_INDEX) {
290 u8 pos;
291 u16 flags;
292
Bjorn Helgaasa9047f22013-04-18 15:12:58 -0600293 pos = vdev->pdev->msix_cap;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600294 if (pos) {
295 pci_read_config_word(vdev->pdev,
296 pos + PCI_MSIX_FLAGS, &flags);
297
298 return (flags & PCI_MSIX_FLAGS_QSIZE) + 1;
299 }
Alex Williamson6140a8f2015-02-06 15:05:08 -0700300 } else if (irq_type == VFIO_PCI_ERR_IRQ_INDEX) {
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600301 if (pci_is_pcie(vdev->pdev))
302 return 1;
Alex Williamson6140a8f2015-02-06 15:05:08 -0700303 } else if (irq_type == VFIO_PCI_REQ_IRQ_INDEX) {
304 return 1;
305 }
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600306
307 return 0;
308}
309
Alex Williamson8b27ee62013-09-04 11:28:04 -0600310static int vfio_pci_count_devs(struct pci_dev *pdev, void *data)
311{
312 (*(int *)data)++;
313 return 0;
314}
315
316struct vfio_pci_fill_info {
317 int max;
318 int cur;
319 struct vfio_pci_dependent_device *devices;
320};
321
322static int vfio_pci_fill_devs(struct pci_dev *pdev, void *data)
323{
324 struct vfio_pci_fill_info *fill = data;
325 struct iommu_group *iommu_group;
326
327 if (fill->cur == fill->max)
328 return -EAGAIN; /* Something changed, try again */
329
330 iommu_group = iommu_group_get(&pdev->dev);
331 if (!iommu_group)
332 return -EPERM; /* Cannot reset non-isolated devices */
333
334 fill->devices[fill->cur].group_id = iommu_group_id(iommu_group);
335 fill->devices[fill->cur].segment = pci_domain_nr(pdev->bus);
336 fill->devices[fill->cur].bus = pdev->bus->number;
337 fill->devices[fill->cur].devfn = pdev->devfn;
338 fill->cur++;
339 iommu_group_put(iommu_group);
340 return 0;
341}
342
343struct vfio_pci_group_entry {
344 struct vfio_group *group;
345 int id;
346};
347
348struct vfio_pci_group_info {
349 int count;
350 struct vfio_pci_group_entry *groups;
351};
352
353static int vfio_pci_validate_devs(struct pci_dev *pdev, void *data)
354{
355 struct vfio_pci_group_info *info = data;
356 struct iommu_group *group;
357 int id, i;
358
359 group = iommu_group_get(&pdev->dev);
360 if (!group)
361 return -EPERM;
362
363 id = iommu_group_id(group);
364
365 for (i = 0; i < info->count; i++)
366 if (info->groups[i].id == id)
367 break;
368
369 iommu_group_put(group);
370
371 return (i == info->count) ? -EINVAL : 0;
372}
373
374static bool vfio_pci_dev_below_slot(struct pci_dev *pdev, struct pci_slot *slot)
375{
376 for (; pdev; pdev = pdev->bus->self)
377 if (pdev->bus == slot->bus)
378 return (pdev->slot == slot);
379 return false;
380}
381
382struct vfio_pci_walk_info {
383 int (*fn)(struct pci_dev *, void *data);
384 void *data;
385 struct pci_dev *pdev;
386 bool slot;
387 int ret;
388};
389
390static int vfio_pci_walk_wrapper(struct pci_dev *pdev, void *data)
391{
392 struct vfio_pci_walk_info *walk = data;
393
394 if (!walk->slot || vfio_pci_dev_below_slot(pdev, walk->pdev->slot))
395 walk->ret = walk->fn(pdev, walk->data);
396
397 return walk->ret;
398}
399
400static int vfio_pci_for_each_slot_or_bus(struct pci_dev *pdev,
401 int (*fn)(struct pci_dev *,
402 void *data), void *data,
403 bool slot)
404{
405 struct vfio_pci_walk_info walk = {
406 .fn = fn, .data = data, .pdev = pdev, .slot = slot, .ret = 0,
407 };
408
409 pci_walk_bus(pdev->bus, vfio_pci_walk_wrapper, &walk);
410
411 return walk.ret;
412}
413
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600414static long vfio_pci_ioctl(void *device_data,
415 unsigned int cmd, unsigned long arg)
416{
417 struct vfio_pci_device *vdev = device_data;
418 unsigned long minsz;
419
420 if (cmd == VFIO_DEVICE_GET_INFO) {
421 struct vfio_device_info info;
422
423 minsz = offsetofend(struct vfio_device_info, num_irqs);
424
425 if (copy_from_user(&info, (void __user *)arg, minsz))
426 return -EFAULT;
427
428 if (info.argsz < minsz)
429 return -EINVAL;
430
431 info.flags = VFIO_DEVICE_FLAGS_PCI;
432
433 if (vdev->reset_works)
434 info.flags |= VFIO_DEVICE_FLAGS_RESET;
435
436 info.num_regions = VFIO_PCI_NUM_REGIONS;
437 info.num_irqs = VFIO_PCI_NUM_IRQS;
438
439 return copy_to_user((void __user *)arg, &info, minsz);
440
441 } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
442 struct pci_dev *pdev = vdev->pdev;
443 struct vfio_region_info info;
444
445 minsz = offsetofend(struct vfio_region_info, offset);
446
447 if (copy_from_user(&info, (void __user *)arg, minsz))
448 return -EFAULT;
449
450 if (info.argsz < minsz)
451 return -EINVAL;
452
453 switch (info.index) {
454 case VFIO_PCI_CONFIG_REGION_INDEX:
455 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
456 info.size = pdev->cfg_size;
457 info.flags = VFIO_REGION_INFO_FLAG_READ |
458 VFIO_REGION_INFO_FLAG_WRITE;
459 break;
460 case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
461 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
462 info.size = pci_resource_len(pdev, info.index);
463 if (!info.size) {
464 info.flags = 0;
465 break;
466 }
467
468 info.flags = VFIO_REGION_INFO_FLAG_READ |
469 VFIO_REGION_INFO_FLAG_WRITE;
Frank Blaschka1d53a3a2014-11-07 09:52:22 -0700470 if (IS_ENABLED(CONFIG_VFIO_PCI_MMAP) &&
471 pci_resource_flags(pdev, info.index) &
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600472 IORESOURCE_MEM && info.size >= PAGE_SIZE)
473 info.flags |= VFIO_REGION_INFO_FLAG_MMAP;
474 break;
475 case VFIO_PCI_ROM_REGION_INDEX:
476 {
477 void __iomem *io;
478 size_t size;
479
480 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
481 info.flags = 0;
482
483 /* Report the BAR size, not the ROM size */
484 info.size = pci_resource_len(pdev, info.index);
485 if (!info.size)
486 break;
487
488 /* Is it really there? */
489 io = pci_map_rom(pdev, &size);
490 if (!io || !size) {
491 info.size = 0;
492 break;
493 }
494 pci_unmap_rom(pdev, io);
495
496 info.flags = VFIO_REGION_INFO_FLAG_READ;
497 break;
498 }
Alex Williamson84237a82013-02-18 10:11:13 -0700499 case VFIO_PCI_VGA_REGION_INDEX:
500 if (!vdev->has_vga)
501 return -EINVAL;
502
503 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
504 info.size = 0xc0000;
505 info.flags = VFIO_REGION_INFO_FLAG_READ |
506 VFIO_REGION_INFO_FLAG_WRITE;
507
508 break;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600509 default:
510 return -EINVAL;
511 }
512
513 return copy_to_user((void __user *)arg, &info, minsz);
514
515 } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
516 struct vfio_irq_info info;
517
518 minsz = offsetofend(struct vfio_irq_info, count);
519
520 if (copy_from_user(&info, (void __user *)arg, minsz))
521 return -EFAULT;
522
523 if (info.argsz < minsz || info.index >= VFIO_PCI_NUM_IRQS)
524 return -EINVAL;
525
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600526 switch (info.index) {
527 case VFIO_PCI_INTX_IRQ_INDEX ... VFIO_PCI_MSIX_IRQ_INDEX:
Alex Williamson6140a8f2015-02-06 15:05:08 -0700528 case VFIO_PCI_REQ_IRQ_INDEX:
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600529 break;
530 case VFIO_PCI_ERR_IRQ_INDEX:
531 if (pci_is_pcie(vdev->pdev))
532 break;
533 /* pass thru to return error */
534 default:
535 return -EINVAL;
536 }
537
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600538 info.flags = VFIO_IRQ_INFO_EVENTFD;
539
540 info.count = vfio_pci_get_irq_count(vdev, info.index);
541
542 if (info.index == VFIO_PCI_INTX_IRQ_INDEX)
543 info.flags |= (VFIO_IRQ_INFO_MASKABLE |
544 VFIO_IRQ_INFO_AUTOMASKED);
545 else
546 info.flags |= VFIO_IRQ_INFO_NORESIZE;
547
548 return copy_to_user((void __user *)arg, &info, minsz);
549
550 } else if (cmd == VFIO_DEVICE_SET_IRQS) {
551 struct vfio_irq_set hdr;
552 u8 *data = NULL;
553 int ret = 0;
554
555 minsz = offsetofend(struct vfio_irq_set, count);
556
557 if (copy_from_user(&hdr, (void __user *)arg, minsz))
558 return -EFAULT;
559
560 if (hdr.argsz < minsz || hdr.index >= VFIO_PCI_NUM_IRQS ||
561 hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
562 VFIO_IRQ_SET_ACTION_TYPE_MASK))
563 return -EINVAL;
564
565 if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
566 size_t size;
Alex Williamson904c6802013-03-26 11:33:16 -0600567 int max = vfio_pci_get_irq_count(vdev, hdr.index);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600568
569 if (hdr.flags & VFIO_IRQ_SET_DATA_BOOL)
570 size = sizeof(uint8_t);
571 else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD)
572 size = sizeof(int32_t);
573 else
574 return -EINVAL;
575
576 if (hdr.argsz - minsz < hdr.count * size ||
Alex Williamson904c6802013-03-26 11:33:16 -0600577 hdr.start >= max || hdr.start + hdr.count > max)
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600578 return -EINVAL;
579
Fengguang Wu3a1f7042012-12-07 13:43:49 -0700580 data = memdup_user((void __user *)(arg + minsz),
581 hdr.count * size);
582 if (IS_ERR(data))
583 return PTR_ERR(data);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600584 }
585
586 mutex_lock(&vdev->igate);
587
588 ret = vfio_pci_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
589 hdr.start, hdr.count, data);
590
591 mutex_unlock(&vdev->igate);
592 kfree(data);
593
594 return ret;
595
Alex Williamson8b27ee62013-09-04 11:28:04 -0600596 } else if (cmd == VFIO_DEVICE_RESET) {
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600597 return vdev->reset_works ?
Alex Williamson890ed572014-01-14 20:45:09 -0700598 pci_try_reset_function(vdev->pdev) : -EINVAL;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600599
Alex Williamson8b27ee62013-09-04 11:28:04 -0600600 } else if (cmd == VFIO_DEVICE_GET_PCI_HOT_RESET_INFO) {
601 struct vfio_pci_hot_reset_info hdr;
602 struct vfio_pci_fill_info fill = { 0 };
603 struct vfio_pci_dependent_device *devices = NULL;
604 bool slot = false;
605 int ret = 0;
606
607 minsz = offsetofend(struct vfio_pci_hot_reset_info, count);
608
609 if (copy_from_user(&hdr, (void __user *)arg, minsz))
610 return -EFAULT;
611
612 if (hdr.argsz < minsz)
613 return -EINVAL;
614
615 hdr.flags = 0;
616
617 /* Can we do a slot or bus reset or neither? */
618 if (!pci_probe_reset_slot(vdev->pdev->slot))
619 slot = true;
620 else if (pci_probe_reset_bus(vdev->pdev->bus))
621 return -ENODEV;
622
623 /* How many devices are affected? */
624 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
625 vfio_pci_count_devs,
626 &fill.max, slot);
627 if (ret)
628 return ret;
629
630 WARN_ON(!fill.max); /* Should always be at least one */
631
632 /*
633 * If there's enough space, fill it now, otherwise return
634 * -ENOSPC and the number of devices affected.
635 */
636 if (hdr.argsz < sizeof(hdr) + (fill.max * sizeof(*devices))) {
637 ret = -ENOSPC;
638 hdr.count = fill.max;
639 goto reset_info_exit;
640 }
641
642 devices = kcalloc(fill.max, sizeof(*devices), GFP_KERNEL);
643 if (!devices)
644 return -ENOMEM;
645
646 fill.devices = devices;
647
648 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
649 vfio_pci_fill_devs,
650 &fill, slot);
651
652 /*
653 * If a device was removed between counting and filling,
654 * we may come up short of fill.max. If a device was
655 * added, we'll have a return of -EAGAIN above.
656 */
657 if (!ret)
658 hdr.count = fill.cur;
659
660reset_info_exit:
661 if (copy_to_user((void __user *)arg, &hdr, minsz))
662 ret = -EFAULT;
663
664 if (!ret) {
665 if (copy_to_user((void __user *)(arg + minsz), devices,
666 hdr.count * sizeof(*devices)))
667 ret = -EFAULT;
668 }
669
670 kfree(devices);
671 return ret;
672
673 } else if (cmd == VFIO_DEVICE_PCI_HOT_RESET) {
674 struct vfio_pci_hot_reset hdr;
675 int32_t *group_fds;
676 struct vfio_pci_group_entry *groups;
677 struct vfio_pci_group_info info;
678 bool slot = false;
679 int i, count = 0, ret = 0;
680
681 minsz = offsetofend(struct vfio_pci_hot_reset, count);
682
683 if (copy_from_user(&hdr, (void __user *)arg, minsz))
684 return -EFAULT;
685
686 if (hdr.argsz < minsz || hdr.flags)
687 return -EINVAL;
688
689 /* Can we do a slot or bus reset or neither? */
690 if (!pci_probe_reset_slot(vdev->pdev->slot))
691 slot = true;
692 else if (pci_probe_reset_bus(vdev->pdev->bus))
693 return -ENODEV;
694
695 /*
696 * We can't let userspace give us an arbitrarily large
697 * buffer to copy, so verify how many we think there
698 * could be. Note groups can have multiple devices so
699 * one group per device is the max.
700 */
701 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
702 vfio_pci_count_devs,
703 &count, slot);
704 if (ret)
705 return ret;
706
707 /* Somewhere between 1 and count is OK */
708 if (!hdr.count || hdr.count > count)
709 return -EINVAL;
710
711 group_fds = kcalloc(hdr.count, sizeof(*group_fds), GFP_KERNEL);
712 groups = kcalloc(hdr.count, sizeof(*groups), GFP_KERNEL);
713 if (!group_fds || !groups) {
714 kfree(group_fds);
715 kfree(groups);
716 return -ENOMEM;
717 }
718
719 if (copy_from_user(group_fds, (void __user *)(arg + minsz),
720 hdr.count * sizeof(*group_fds))) {
721 kfree(group_fds);
722 kfree(groups);
723 return -EFAULT;
724 }
725
726 /*
727 * For each group_fd, get the group through the vfio external
728 * user interface and store the group and iommu ID. This
729 * ensures the group is held across the reset.
730 */
731 for (i = 0; i < hdr.count; i++) {
732 struct vfio_group *group;
733 struct fd f = fdget(group_fds[i]);
734 if (!f.file) {
735 ret = -EBADF;
736 break;
737 }
738
739 group = vfio_group_get_external_user(f.file);
740 fdput(f);
741 if (IS_ERR(group)) {
742 ret = PTR_ERR(group);
743 break;
744 }
745
746 groups[i].group = group;
747 groups[i].id = vfio_external_user_iommu_id(group);
748 }
749
750 kfree(group_fds);
751
752 /* release reference to groups on error */
753 if (ret)
754 goto hot_reset_release;
755
756 info.count = hdr.count;
757 info.groups = groups;
758
759 /*
760 * Test whether all the affected devices are contained
761 * by the set of groups provided by the user.
762 */
763 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
764 vfio_pci_validate_devs,
765 &info, slot);
766 if (!ret)
767 /* User has access, do the reset */
Alex Williamson890ed572014-01-14 20:45:09 -0700768 ret = slot ? pci_try_reset_slot(vdev->pdev->slot) :
769 pci_try_reset_bus(vdev->pdev->bus);
Alex Williamson8b27ee62013-09-04 11:28:04 -0600770
771hot_reset_release:
772 for (i--; i >= 0; i--)
773 vfio_group_put_external_user(groups[i].group);
774
775 kfree(groups);
776 return ret;
777 }
778
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600779 return -ENOTTY;
780}
781
Alex Williamson5b279a12013-02-14 14:02:12 -0700782static ssize_t vfio_pci_rw(void *device_data, char __user *buf,
783 size_t count, loff_t *ppos, bool iswrite)
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600784{
785 unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
786 struct vfio_pci_device *vdev = device_data;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600787
788 if (index >= VFIO_PCI_NUM_REGIONS)
789 return -EINVAL;
790
Alex Williamson5b279a12013-02-14 14:02:12 -0700791 switch (index) {
792 case VFIO_PCI_CONFIG_REGION_INDEX:
Alex Williamson906ee992013-02-14 14:02:12 -0700793 return vfio_pci_config_rw(vdev, buf, count, ppos, iswrite);
794
Alex Williamson5b279a12013-02-14 14:02:12 -0700795 case VFIO_PCI_ROM_REGION_INDEX:
796 if (iswrite)
797 return -EINVAL;
Alex Williamson906ee992013-02-14 14:02:12 -0700798 return vfio_pci_bar_rw(vdev, buf, count, ppos, false);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600799
Alex Williamson5b279a12013-02-14 14:02:12 -0700800 case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
Alex Williamson906ee992013-02-14 14:02:12 -0700801 return vfio_pci_bar_rw(vdev, buf, count, ppos, iswrite);
Alex Williamson84237a82013-02-18 10:11:13 -0700802
803 case VFIO_PCI_VGA_REGION_INDEX:
804 return vfio_pci_vga_rw(vdev, buf, count, ppos, iswrite);
Alex Williamson5b279a12013-02-14 14:02:12 -0700805 }
806
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600807 return -EINVAL;
808}
809
Alex Williamson5b279a12013-02-14 14:02:12 -0700810static ssize_t vfio_pci_read(void *device_data, char __user *buf,
811 size_t count, loff_t *ppos)
812{
Alex Williamson906ee992013-02-14 14:02:12 -0700813 if (!count)
814 return 0;
815
Alex Williamson5b279a12013-02-14 14:02:12 -0700816 return vfio_pci_rw(device_data, buf, count, ppos, false);
817}
818
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600819static ssize_t vfio_pci_write(void *device_data, const char __user *buf,
820 size_t count, loff_t *ppos)
821{
Alex Williamson906ee992013-02-14 14:02:12 -0700822 if (!count)
823 return 0;
824
825 return vfio_pci_rw(device_data, (char __user *)buf, count, ppos, true);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600826}
827
828static int vfio_pci_mmap(void *device_data, struct vm_area_struct *vma)
829{
830 struct vfio_pci_device *vdev = device_data;
831 struct pci_dev *pdev = vdev->pdev;
832 unsigned int index;
Alex Williamson34002f52012-10-10 09:10:31 -0600833 u64 phys_len, req_len, pgoff, req_start;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600834 int ret;
835
836 index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
837
838 if (vma->vm_end < vma->vm_start)
839 return -EINVAL;
840 if ((vma->vm_flags & VM_SHARED) == 0)
841 return -EINVAL;
842 if (index >= VFIO_PCI_ROM_REGION_INDEX)
843 return -EINVAL;
844 if (!(pci_resource_flags(pdev, index) & IORESOURCE_MEM))
845 return -EINVAL;
846
847 phys_len = pci_resource_len(pdev, index);
848 req_len = vma->vm_end - vma->vm_start;
849 pgoff = vma->vm_pgoff &
850 ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
851 req_start = pgoff << PAGE_SHIFT;
852
853 if (phys_len < PAGE_SIZE || req_start + req_len > phys_len)
854 return -EINVAL;
855
856 if (index == vdev->msix_bar) {
857 /*
858 * Disallow mmaps overlapping the MSI-X table; users don't
859 * get to touch this directly. We could find somewhere
860 * else to map the overlap, but page granularity is only
861 * a recommendation, not a requirement, so the user needs
862 * to know which bits are real. Requiring them to mmap
863 * around the table makes that clear.
864 */
865
866 /* If neither entirely above nor below, then it overlaps */
867 if (!(req_start >= vdev->msix_offset + vdev->msix_size ||
868 req_start + req_len <= vdev->msix_offset))
869 return -EINVAL;
870 }
871
872 /*
873 * Even though we don't make use of the barmap for the mmap,
874 * we need to request the region and the barmap tracks that.
875 */
876 if (!vdev->barmap[index]) {
877 ret = pci_request_selected_regions(pdev,
878 1 << index, "vfio-pci");
879 if (ret)
880 return ret;
881
882 vdev->barmap[index] = pci_iomap(pdev, index, 0);
883 }
884
885 vma->vm_private_data = vdev;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600886 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Alex Williamson34002f52012-10-10 09:10:31 -0600887 vma->vm_pgoff = (pci_resource_start(pdev, index) >> PAGE_SHIFT) + pgoff;
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600888
Alex Williamson34002f52012-10-10 09:10:31 -0600889 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600890 req_len, vma->vm_page_prot);
891}
892
Alex Williamson6140a8f2015-02-06 15:05:08 -0700893static void vfio_pci_request(void *device_data, unsigned int count)
894{
895 struct vfio_pci_device *vdev = device_data;
896
897 mutex_lock(&vdev->igate);
898
899 if (vdev->req_trigger) {
900 dev_dbg(&vdev->pdev->dev, "Requesting device from user\n");
901 eventfd_signal(vdev->req_trigger, 1);
902 }
903
904 mutex_unlock(&vdev->igate);
905}
906
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600907static const struct vfio_device_ops vfio_pci_ops = {
908 .name = "vfio-pci",
909 .open = vfio_pci_open,
910 .release = vfio_pci_release,
911 .ioctl = vfio_pci_ioctl,
912 .read = vfio_pci_read,
913 .write = vfio_pci_write,
914 .mmap = vfio_pci_mmap,
Alex Williamson6140a8f2015-02-06 15:05:08 -0700915 .request = vfio_pci_request,
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600916};
917
918static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
919{
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600920 struct vfio_pci_device *vdev;
921 struct iommu_group *group;
922 int ret;
923
Wei Yang7c2e2112015-01-07 10:29:11 -0700924 if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600925 return -EINVAL;
926
927 group = iommu_group_get(&pdev->dev);
928 if (!group)
929 return -EINVAL;
930
931 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
932 if (!vdev) {
933 iommu_group_put(group);
934 return -ENOMEM;
935 }
936
937 vdev->pdev = pdev;
938 vdev->irq_type = VFIO_PCI_NUM_IRQS;
939 mutex_init(&vdev->igate);
940 spin_lock_init(&vdev->irqlock);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600941
942 ret = vfio_add_group_dev(&pdev->dev, &vfio_pci_ops, vdev);
943 if (ret) {
944 iommu_group_put(group);
945 kfree(vdev);
946 }
947
Alex Williamsonecaa1f62015-04-07 11:14:41 -0600948 if (vfio_pci_is_vga(pdev)) {
949 vga_client_register(pdev, vdev, NULL, vfio_pci_set_vga_decode);
950 vga_set_legacy_decoding(pdev,
951 vfio_pci_set_vga_decode(vdev, false));
952 }
953
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600954 return ret;
955}
956
957static void vfio_pci_remove(struct pci_dev *pdev)
958{
959 struct vfio_pci_device *vdev;
960
Alex Williamson61d79252014-08-07 11:12:04 -0600961 vdev = vfio_del_group_dev(&pdev->dev);
Alex Williamsonecaa1f62015-04-07 11:14:41 -0600962 if (!vdev)
963 return;
964
965 iommu_group_put(pdev->dev.iommu_group);
966 kfree(vdev);
967
968 if (vfio_pci_is_vga(pdev)) {
969 vga_client_register(pdev, NULL, NULL, NULL);
970 vga_set_legacy_decoding(pdev,
971 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM |
972 VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM);
Alex Williamson61d79252014-08-07 11:12:04 -0600973 }
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600974}
975
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600976static pci_ers_result_t vfio_pci_aer_err_detected(struct pci_dev *pdev,
977 pci_channel_state_t state)
978{
979 struct vfio_pci_device *vdev;
980 struct vfio_device *device;
981
982 device = vfio_device_get_from_dev(&pdev->dev);
983 if (device == NULL)
984 return PCI_ERS_RESULT_DISCONNECT;
985
986 vdev = vfio_device_data(device);
987 if (vdev == NULL) {
988 vfio_device_put(device);
989 return PCI_ERS_RESULT_DISCONNECT;
990 }
991
Alex Williamson3be3a072014-01-14 16:12:55 -0700992 mutex_lock(&vdev->igate);
993
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600994 if (vdev->err_trigger)
995 eventfd_signal(vdev->err_trigger, 1);
996
Alex Williamson3be3a072014-01-14 16:12:55 -0700997 mutex_unlock(&vdev->igate);
998
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -0600999 vfio_device_put(device);
1000
1001 return PCI_ERS_RESULT_CAN_RECOVER;
1002}
1003
1004static struct pci_error_handlers vfio_err_handlers = {
1005 .error_detected = vfio_pci_aer_err_detected,
1006};
1007
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001008static struct pci_driver vfio_pci_driver = {
1009 .name = "vfio-pci",
1010 .id_table = NULL, /* only dynamic ids */
1011 .probe = vfio_pci_probe,
1012 .remove = vfio_pci_remove,
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -06001013 .err_handler = &vfio_err_handlers,
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001014};
1015
Alex Williamson93899a62014-09-29 17:18:39 -06001016struct vfio_devices {
1017 struct vfio_device **devices;
1018 int cur_index;
1019 int max_index;
1020};
1021
1022static int vfio_pci_get_devs(struct pci_dev *pdev, void *data)
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001023{
Alex Williamson93899a62014-09-29 17:18:39 -06001024 struct vfio_devices *devs = data;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001025 struct pci_driver *pci_drv = ACCESS_ONCE(pdev->driver);
1026
Alex Williamson93899a62014-09-29 17:18:39 -06001027 if (pci_drv != &vfio_pci_driver)
1028 return -EBUSY;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001029
Alex Williamson93899a62014-09-29 17:18:39 -06001030 if (devs->cur_index == devs->max_index)
1031 return -ENOSPC;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001032
Alex Williamson93899a62014-09-29 17:18:39 -06001033 devs->devices[devs->cur_index] = vfio_device_get_from_dev(&pdev->dev);
1034 if (!devs->devices[devs->cur_index])
1035 return -EINVAL;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001036
Alex Williamson93899a62014-09-29 17:18:39 -06001037 devs->cur_index++;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001038 return 0;
1039}
1040
1041/*
1042 * Attempt to do a bus/slot reset if there are devices affected by a reset for
1043 * this device that are needs_reset and all of the affected devices are unused
Alex Williamson93899a62014-09-29 17:18:39 -06001044 * (!refcnt). Callers are required to hold driver_lock when calling this to
1045 * prevent device opens and concurrent bus reset attempts. We prevent device
1046 * unbinds by acquiring and holding a reference to the vfio_device.
1047 *
1048 * NB: vfio-core considers a group to be viable even if some devices are
1049 * bound to drivers like pci-stub or pcieport. Here we require all devices
1050 * to be bound to vfio_pci since that's the only way we can be sure they
1051 * stay put.
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001052 */
1053static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev)
1054{
Alex Williamson93899a62014-09-29 17:18:39 -06001055 struct vfio_devices devs = { .cur_index = 0 };
1056 int i = 0, ret = -EINVAL;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001057 bool needs_reset = false, slot = false;
Alex Williamson93899a62014-09-29 17:18:39 -06001058 struct vfio_pci_device *tmp;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001059
1060 if (!pci_probe_reset_slot(vdev->pdev->slot))
1061 slot = true;
1062 else if (pci_probe_reset_bus(vdev->pdev->bus))
1063 return;
1064
Alex Williamson93899a62014-09-29 17:18:39 -06001065 if (vfio_pci_for_each_slot_or_bus(vdev->pdev, vfio_pci_count_devs,
1066 &i, slot) || !i)
1067 return;
1068
1069 devs.max_index = i;
1070 devs.devices = kcalloc(i, sizeof(struct vfio_device *), GFP_KERNEL);
1071 if (!devs.devices)
1072 return;
1073
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001074 if (vfio_pci_for_each_slot_or_bus(vdev->pdev,
Alex Williamson93899a62014-09-29 17:18:39 -06001075 vfio_pci_get_devs, &devs, slot))
1076 goto put_devs;
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001077
Alex Williamson93899a62014-09-29 17:18:39 -06001078 for (i = 0; i < devs.cur_index; i++) {
1079 tmp = vfio_device_data(devs.devices[i]);
1080 if (tmp->needs_reset)
1081 needs_reset = true;
1082 if (tmp->refcnt)
1083 goto put_devs;
1084 }
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001085
Alex Williamson93899a62014-09-29 17:18:39 -06001086 if (needs_reset)
1087 ret = slot ? pci_try_reset_slot(vdev->pdev->slot) :
1088 pci_try_reset_bus(vdev->pdev->bus);
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001089
Alex Williamson93899a62014-09-29 17:18:39 -06001090put_devs:
1091 for (i = 0; i < devs.cur_index; i++) {
1092 if (!ret) {
1093 tmp = vfio_device_data(devs.devices[i]);
1094 tmp->needs_reset = false;
1095 }
1096 vfio_device_put(devs.devices[i]);
1097 }
1098
1099 kfree(devs.devices);
Alex Williamsonbc4fba72014-08-07 11:12:07 -06001100}
1101
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001102static void __exit vfio_pci_cleanup(void)
1103{
1104 pci_unregister_driver(&vfio_pci_driver);
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001105 vfio_pci_uninit_perm_bits();
1106}
1107
1108static int __init vfio_pci_init(void)
1109{
1110 int ret;
1111
1112 /* Allocate shared config space permision data used by all devices */
1113 ret = vfio_pci_init_perm_bits();
1114 if (ret)
1115 return ret;
1116
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001117 /* Register and scan for devices */
1118 ret = pci_register_driver(&vfio_pci_driver);
1119 if (ret)
1120 goto out_driver;
1121
1122 return 0;
1123
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001124out_driver:
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001125 vfio_pci_uninit_perm_bits();
1126 return ret;
1127}
1128
1129module_init(vfio_pci_init);
1130module_exit(vfio_pci_cleanup);
1131
1132MODULE_VERSION(DRIVER_VERSION);
1133MODULE_LICENSE("GPL v2");
1134MODULE_AUTHOR(DRIVER_AUTHOR);
1135MODULE_DESCRIPTION(DRIVER_DESC);