]>
Commit | Line | Data |
---|---|---|
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 | ||
80c7e8cc AW |
14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
15 | ||
89e1f7d4 AW |
16 | #include <linux/device.h> |
17 | #include <linux/eventfd.h> | |
8b27ee60 | 18 | #include <linux/file.h> |
89e1f7d4 AW |
19 | #include <linux/interrupt.h> |
20 | #include <linux/iommu.h> | |
21 | #include <linux/module.h> | |
22 | #include <linux/mutex.h> | |
23 | #include <linux/notifier.h> | |
24 | #include <linux/pci.h> | |
25 | #include <linux/pm_runtime.h> | |
26 | #include <linux/slab.h> | |
27 | #include <linux/types.h> | |
28 | #include <linux/uaccess.h> | |
29 | #include <linux/vfio.h> | |
ecaa1f6a | 30 | #include <linux/vgaarb.h> |
89e1f7d4 AW |
31 | |
32 | #include "vfio_pci_private.h" | |
33 | ||
34 | #define DRIVER_VERSION "0.2" | |
35 | #define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>" | |
36 | #define DRIVER_DESC "VFIO PCI - User Level meta-driver" | |
37 | ||
80c7e8cc AW |
38 | static char ids[1024] __initdata; |
39 | module_param_string(ids, ids, sizeof(ids), 0); | |
40 | MODULE_PARM_DESC(ids, "Initial PCI IDs to add to the vfio driver, format is \"vendor:device[:subvendor[:subdevice[:class[:class_mask]]]]\" and multiple comma separated entries can be specified"); | |
41 | ||
89e1f7d4 AW |
42 | static bool nointxmask; |
43 | module_param_named(nointxmask, nointxmask, bool, S_IRUGO | S_IWUSR); | |
44 | MODULE_PARM_DESC(nointxmask, | |
45 | "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."); | |
46 | ||
88c0dead AW |
47 | #ifdef CONFIG_VFIO_PCI_VGA |
48 | static bool disable_vga; | |
49 | module_param(disable_vga, bool, S_IRUGO); | |
50 | MODULE_PARM_DESC(disable_vga, "Disable VGA resource access through vfio-pci"); | |
51 | #endif | |
52 | ||
61d79256 AW |
53 | static DEFINE_MUTEX(driver_lock); |
54 | ||
88c0dead AW |
55 | static inline bool vfio_vga_disabled(void) |
56 | { | |
57 | #ifdef CONFIG_VFIO_PCI_VGA | |
58 | return disable_vga; | |
59 | #else | |
60 | return true; | |
61 | #endif | |
62 | } | |
63 | ||
ecaa1f6a AW |
64 | /* |
65 | * Our VGA arbiter participation is limited since we don't know anything | |
66 | * about the device itself. However, if the device is the only VGA device | |
67 | * downstream of a bridge and VFIO VGA support is disabled, then we can | |
68 | * safely return legacy VGA IO and memory as not decoded since the user | |
69 | * has no way to get to it and routing can be disabled externally at the | |
70 | * bridge. | |
71 | */ | |
72 | static unsigned int vfio_pci_set_vga_decode(void *opaque, bool single_vga) | |
73 | { | |
74 | struct vfio_pci_device *vdev = opaque; | |
75 | struct pci_dev *tmp = NULL, *pdev = vdev->pdev; | |
76 | unsigned char max_busnr; | |
77 | unsigned int decodes; | |
78 | ||
79 | if (single_vga || !vfio_vga_disabled() || pci_is_root_bus(pdev->bus)) | |
80 | return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM | | |
81 | VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM; | |
82 | ||
83 | max_busnr = pci_bus_max_busnr(pdev->bus); | |
84 | decodes = VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM; | |
85 | ||
86 | while ((tmp = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, tmp)) != NULL) { | |
87 | if (tmp == pdev || | |
88 | pci_domain_nr(tmp->bus) != pci_domain_nr(pdev->bus) || | |
89 | pci_is_root_bus(tmp->bus)) | |
90 | continue; | |
91 | ||
92 | if (tmp->bus->number >= pdev->bus->number && | |
93 | tmp->bus->number <= max_busnr) { | |
94 | pci_dev_put(tmp); | |
95 | decodes |= VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM; | |
96 | break; | |
97 | } | |
98 | } | |
99 | ||
100 | return decodes; | |
101 | } | |
102 | ||
103 | static inline bool vfio_pci_is_vga(struct pci_dev *pdev) | |
104 | { | |
105 | return (pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA; | |
106 | } | |
107 | ||
bc4fba77 AW |
108 | static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev); |
109 | ||
89e1f7d4 AW |
110 | static int vfio_pci_enable(struct vfio_pci_device *vdev) |
111 | { | |
112 | struct pci_dev *pdev = vdev->pdev; | |
113 | int ret; | |
114 | u16 cmd; | |
115 | u8 msix_pos; | |
116 | ||
9c22e660 AW |
117 | /* Don't allow our initial saved state to include busmaster */ |
118 | pci_clear_master(pdev); | |
119 | ||
9a92c509 AW |
120 | ret = pci_enable_device(pdev); |
121 | if (ret) | |
122 | return ret; | |
123 | ||
89e1f7d4 AW |
124 | vdev->reset_works = (pci_reset_function(pdev) == 0); |
125 | pci_save_state(pdev); | |
126 | vdev->pci_saved_state = pci_store_saved_state(pdev); | |
127 | if (!vdev->pci_saved_state) | |
128 | pr_debug("%s: Couldn't store %s saved state\n", | |
129 | __func__, dev_name(&pdev->dev)); | |
130 | ||
131 | ret = vfio_config_init(vdev); | |
9a92c509 | 132 | if (ret) { |
eb5685f0 AW |
133 | kfree(vdev->pci_saved_state); |
134 | vdev->pci_saved_state = NULL; | |
9a92c509 AW |
135 | pci_disable_device(pdev); |
136 | return ret; | |
137 | } | |
89e1f7d4 AW |
138 | |
139 | if (likely(!nointxmask)) | |
140 | vdev->pci_2_3 = pci_intx_mask_supported(pdev); | |
141 | ||
142 | pci_read_config_word(pdev, PCI_COMMAND, &cmd); | |
143 | if (vdev->pci_2_3 && (cmd & PCI_COMMAND_INTX_DISABLE)) { | |
144 | cmd &= ~PCI_COMMAND_INTX_DISABLE; | |
145 | pci_write_config_word(pdev, PCI_COMMAND, cmd); | |
146 | } | |
147 | ||
a9047f24 | 148 | msix_pos = pdev->msix_cap; |
89e1f7d4 AW |
149 | if (msix_pos) { |
150 | u16 flags; | |
151 | u32 table; | |
152 | ||
153 | pci_read_config_word(pdev, msix_pos + PCI_MSIX_FLAGS, &flags); | |
154 | pci_read_config_dword(pdev, msix_pos + PCI_MSIX_TABLE, &table); | |
155 | ||
508d1aa6 BH |
156 | vdev->msix_bar = table & PCI_MSIX_TABLE_BIR; |
157 | vdev->msix_offset = table & PCI_MSIX_TABLE_OFFSET; | |
89e1f7d4 AW |
158 | vdev->msix_size = ((flags & PCI_MSIX_FLAGS_QSIZE) + 1) * 16; |
159 | } else | |
160 | vdev->msix_bar = 0xFF; | |
161 | ||
ecaa1f6a | 162 | if (!vfio_vga_disabled() && vfio_pci_is_vga(pdev)) |
84237a82 | 163 | vdev->has_vga = true; |
84237a82 | 164 | |
9a92c509 | 165 | return 0; |
89e1f7d4 AW |
166 | } |
167 | ||
168 | static void vfio_pci_disable(struct vfio_pci_device *vdev) | |
169 | { | |
2007722a | 170 | struct pci_dev *pdev = vdev->pdev; |
89e1f7d4 AW |
171 | int bar; |
172 | ||
9c22e660 AW |
173 | /* Stop the device from further DMA */ |
174 | pci_clear_master(pdev); | |
89e1f7d4 AW |
175 | |
176 | vfio_pci_set_irqs_ioctl(vdev, VFIO_IRQ_SET_DATA_NONE | | |
177 | VFIO_IRQ_SET_ACTION_TRIGGER, | |
178 | vdev->irq_type, 0, 0, NULL); | |
179 | ||
180 | vdev->virq_disabled = false; | |
181 | ||
182 | vfio_config_free(vdev); | |
183 | ||
89e1f7d4 AW |
184 | for (bar = PCI_STD_RESOURCES; bar <= PCI_STD_RESOURCE_END; bar++) { |
185 | if (!vdev->barmap[bar]) | |
186 | continue; | |
2007722a AW |
187 | pci_iounmap(pdev, vdev->barmap[bar]); |
188 | pci_release_selected_regions(pdev, 1 << bar); | |
89e1f7d4 AW |
189 | vdev->barmap[bar] = NULL; |
190 | } | |
2007722a | 191 | |
bc4fba77 AW |
192 | vdev->needs_reset = true; |
193 | ||
2007722a AW |
194 | /* |
195 | * If we have saved state, restore it. If we can reset the device, | |
196 | * even better. Resetting with current state seems better than | |
197 | * nothing, but saving and restoring current state without reset | |
198 | * is just busy work. | |
199 | */ | |
200 | if (pci_load_and_free_saved_state(pdev, &vdev->pci_saved_state)) { | |
201 | pr_info("%s: Couldn't reload %s saved state\n", | |
202 | __func__, dev_name(&pdev->dev)); | |
203 | ||
204 | if (!vdev->reset_works) | |
9c22e660 | 205 | goto out; |
2007722a AW |
206 | |
207 | pci_save_state(pdev); | |
208 | } | |
209 | ||
210 | /* | |
211 | * Disable INTx and MSI, presumably to avoid spurious interrupts | |
212 | * during reset. Stolen from pci_reset_function() | |
213 | */ | |
214 | pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE); | |
215 | ||
d24cdbfd | 216 | /* |
890ed578 AW |
217 | * Try to reset the device. The success of this is dependent on |
218 | * being able to lock the device, which is not always possible. | |
d24cdbfd | 219 | */ |
561d72dd AW |
220 | if (vdev->reset_works && !pci_try_reset_function(pdev)) |
221 | vdev->needs_reset = false; | |
2007722a AW |
222 | |
223 | pci_restore_state(pdev); | |
9c22e660 AW |
224 | out: |
225 | pci_disable_device(pdev); | |
bc4fba77 AW |
226 | |
227 | vfio_pci_try_bus_reset(vdev); | |
89e1f7d4 AW |
228 | } |
229 | ||
230 | static void vfio_pci_release(void *device_data) | |
231 | { | |
232 | struct vfio_pci_device *vdev = device_data; | |
233 | ||
61d79256 AW |
234 | mutex_lock(&driver_lock); |
235 | ||
236 | if (!(--vdev->refcnt)) { | |
1b69be5e | 237 | vfio_spapr_pci_eeh_release(vdev->pdev); |
89e1f7d4 | 238 | vfio_pci_disable(vdev); |
1b69be5e | 239 | } |
89e1f7d4 | 240 | |
61d79256 AW |
241 | mutex_unlock(&driver_lock); |
242 | ||
89e1f7d4 AW |
243 | module_put(THIS_MODULE); |
244 | } | |
245 | ||
246 | static int vfio_pci_open(void *device_data) | |
247 | { | |
248 | struct vfio_pci_device *vdev = device_data; | |
61d79256 | 249 | int ret = 0; |
89e1f7d4 AW |
250 | |
251 | if (!try_module_get(THIS_MODULE)) | |
252 | return -ENODEV; | |
253 | ||
61d79256 AW |
254 | mutex_lock(&driver_lock); |
255 | ||
256 | if (!vdev->refcnt) { | |
1b69be5e GS |
257 | ret = vfio_pci_enable(vdev); |
258 | if (ret) | |
259 | goto error; | |
260 | ||
9b936c96 | 261 | vfio_spapr_pci_eeh_open(vdev->pdev); |
89e1f7d4 | 262 | } |
61d79256 | 263 | vdev->refcnt++; |
1b69be5e | 264 | error: |
61d79256 AW |
265 | mutex_unlock(&driver_lock); |
266 | if (ret) | |
267 | module_put(THIS_MODULE); | |
1b69be5e | 268 | return ret; |
89e1f7d4 AW |
269 | } |
270 | ||
271 | static 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); | |
1d53a3a7 | 276 | if (IS_ENABLED(CONFIG_VFIO_PCI_INTX) && pin) |
89e1f7d4 AW |
277 | return 1; |
278 | ||
279 | } else if (irq_type == VFIO_PCI_MSI_IRQ_INDEX) { | |
280 | u8 pos; | |
281 | u16 flags; | |
282 | ||
a9047f24 | 283 | pos = vdev->pdev->msi_cap; |
89e1f7d4 AW |
284 | if (pos) { |
285 | pci_read_config_word(vdev->pdev, | |
286 | pos + PCI_MSI_FLAGS, &flags); | |
fd49c81f | 287 | return 1 << ((flags & PCI_MSI_FLAGS_QMASK) >> 1); |
89e1f7d4 AW |
288 | } |
289 | } else if (irq_type == VFIO_PCI_MSIX_IRQ_INDEX) { | |
290 | u8 pos; | |
291 | u16 flags; | |
292 | ||
a9047f24 | 293 | pos = vdev->pdev->msix_cap; |
89e1f7d4 AW |
294 | 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 | } | |
6140a8f5 | 300 | } else if (irq_type == VFIO_PCI_ERR_IRQ_INDEX) { |
dad9f897 VMP |
301 | if (pci_is_pcie(vdev->pdev)) |
302 | return 1; | |
6140a8f5 AW |
303 | } else if (irq_type == VFIO_PCI_REQ_IRQ_INDEX) { |
304 | return 1; | |
305 | } | |
89e1f7d4 AW |
306 | |
307 | return 0; | |
308 | } | |
309 | ||
8b27ee60 AW |
310 | static int vfio_pci_count_devs(struct pci_dev *pdev, void *data) |
311 | { | |
312 | (*(int *)data)++; | |
313 | return 0; | |
314 | } | |
315 | ||
316 | struct vfio_pci_fill_info { | |
317 | int max; | |
318 | int cur; | |
319 | struct vfio_pci_dependent_device *devices; | |
320 | }; | |
321 | ||
322 | static 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 | ||
343 | struct vfio_pci_group_entry { | |
344 | struct vfio_group *group; | |
345 | int id; | |
346 | }; | |
347 | ||
348 | struct vfio_pci_group_info { | |
349 | int count; | |
350 | struct vfio_pci_group_entry *groups; | |
351 | }; | |
352 | ||
353 | static 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 | ||
374 | static 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 | ||
382 | struct 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 | ||
390 | static 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 | ||
400 | static 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 | ||
89e1f7d4 AW |
414 | static 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; | |
1d53a3a7 FB |
470 | if (IS_ENABLED(CONFIG_VFIO_PCI_MMAP) && |
471 | pci_resource_flags(pdev, info.index) & | |
89e1f7d4 AW |
472 | 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 | } | |
84237a82 AW |
499 | 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; | |
89e1f7d4 AW |
509 | 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 | ||
dad9f897 VMP |
526 | switch (info.index) { |
527 | case VFIO_PCI_INTX_IRQ_INDEX ... VFIO_PCI_MSIX_IRQ_INDEX: | |
6140a8f5 | 528 | case VFIO_PCI_REQ_IRQ_INDEX: |
dad9f897 VMP |
529 | 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 | ||
89e1f7d4 AW |
538 | 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; | |
904c680c | 567 | int max = vfio_pci_get_irq_count(vdev, hdr.index); |
89e1f7d4 AW |
568 | |
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 || | |
904c680c | 577 | hdr.start >= max || hdr.start + hdr.count > max) |
89e1f7d4 AW |
578 | return -EINVAL; |
579 | ||
3a1f7041 FW |
580 | data = memdup_user((void __user *)(arg + minsz), |
581 | hdr.count * size); | |
582 | if (IS_ERR(data)) | |
583 | return PTR_ERR(data); | |
89e1f7d4 AW |
584 | } |
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 | ||
8b27ee60 | 596 | } else if (cmd == VFIO_DEVICE_RESET) { |
89e1f7d4 | 597 | return vdev->reset_works ? |
890ed578 | 598 | pci_try_reset_function(vdev->pdev) : -EINVAL; |
89e1f7d4 | 599 | |
8b27ee60 AW |
600 | } 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 | ||
660 | reset_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 */ | |
890ed578 AW |
768 | ret = slot ? pci_try_reset_slot(vdev->pdev->slot) : |
769 | pci_try_reset_bus(vdev->pdev->bus); | |
8b27ee60 AW |
770 | |
771 | hot_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 | ||
89e1f7d4 AW |
779 | return -ENOTTY; |
780 | } | |
781 | ||
5b279a11 AW |
782 | static ssize_t vfio_pci_rw(void *device_data, char __user *buf, |
783 | size_t count, loff_t *ppos, bool iswrite) | |
89e1f7d4 AW |
784 | { |
785 | unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos); | |
786 | struct vfio_pci_device *vdev = device_data; | |
89e1f7d4 AW |
787 | |
788 | if (index >= VFIO_PCI_NUM_REGIONS) | |
789 | return -EINVAL; | |
790 | ||
5b279a11 AW |
791 | switch (index) { |
792 | case VFIO_PCI_CONFIG_REGION_INDEX: | |
906ee99d AW |
793 | return vfio_pci_config_rw(vdev, buf, count, ppos, iswrite); |
794 | ||
5b279a11 AW |
795 | case VFIO_PCI_ROM_REGION_INDEX: |
796 | if (iswrite) | |
797 | return -EINVAL; | |
906ee99d | 798 | return vfio_pci_bar_rw(vdev, buf, count, ppos, false); |
89e1f7d4 | 799 | |
5b279a11 | 800 | case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX: |
906ee99d | 801 | return vfio_pci_bar_rw(vdev, buf, count, ppos, iswrite); |
84237a82 AW |
802 | |
803 | case VFIO_PCI_VGA_REGION_INDEX: | |
804 | return vfio_pci_vga_rw(vdev, buf, count, ppos, iswrite); | |
5b279a11 AW |
805 | } |
806 | ||
89e1f7d4 AW |
807 | return -EINVAL; |
808 | } | |
809 | ||
5b279a11 AW |
810 | static ssize_t vfio_pci_read(void *device_data, char __user *buf, |
811 | size_t count, loff_t *ppos) | |
812 | { | |
906ee99d AW |
813 | if (!count) |
814 | return 0; | |
815 | ||
5b279a11 AW |
816 | return vfio_pci_rw(device_data, buf, count, ppos, false); |
817 | } | |
818 | ||
89e1f7d4 AW |
819 | static ssize_t vfio_pci_write(void *device_data, const char __user *buf, |
820 | size_t count, loff_t *ppos) | |
821 | { | |
906ee99d AW |
822 | if (!count) |
823 | return 0; | |
824 | ||
825 | return vfio_pci_rw(device_data, (char __user *)buf, count, ppos, true); | |
89e1f7d4 AW |
826 | } |
827 | ||
828 | static 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; | |
34002f54 | 833 | u64 phys_len, req_len, pgoff, req_start; |
89e1f7d4 AW |
834 | 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; | |
89e1f7d4 | 886 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); |
34002f54 | 887 | vma->vm_pgoff = (pci_resource_start(pdev, index) >> PAGE_SHIFT) + pgoff; |
89e1f7d4 | 888 | |
34002f54 | 889 | return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, |
89e1f7d4 AW |
890 | req_len, vma->vm_page_prot); |
891 | } | |
892 | ||
6140a8f5 AW |
893 | static 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 | ||
89e1f7d4 AW |
907 | static 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, | |
6140a8f5 | 915 | .request = vfio_pci_request, |
89e1f7d4 AW |
916 | }; |
917 | ||
918 | static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) | |
919 | { | |
89e1f7d4 AW |
920 | struct vfio_pci_device *vdev; |
921 | struct iommu_group *group; | |
922 | int ret; | |
923 | ||
7c2e211f | 924 | if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL) |
89e1f7d4 AW |
925 | 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); | |
89e1f7d4 AW |
941 | |
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 | ||
ecaa1f6a AW |
948 | 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 | ||
89e1f7d4 AW |
954 | return ret; |
955 | } | |
956 | ||
957 | static void vfio_pci_remove(struct pci_dev *pdev) | |
958 | { | |
959 | struct vfio_pci_device *vdev; | |
960 | ||
961 | vdev = vfio_del_group_dev(&pdev->dev); | |
ecaa1f6a AW |
962 | 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); | |
61d79256 | 973 | } |
89e1f7d4 AW |
974 | } |
975 | ||
dad9f897 VMP |
976 | static 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 | ||
3be3a074 AW |
992 | mutex_lock(&vdev->igate); |
993 | ||
dad9f897 VMP |
994 | if (vdev->err_trigger) |
995 | eventfd_signal(vdev->err_trigger, 1); | |
996 | ||
3be3a074 AW |
997 | mutex_unlock(&vdev->igate); |
998 | ||
dad9f897 VMP |
999 | vfio_device_put(device); |
1000 | ||
1001 | return PCI_ERS_RESULT_CAN_RECOVER; | |
1002 | } | |
1003 | ||
1004 | static struct pci_error_handlers vfio_err_handlers = { | |
1005 | .error_detected = vfio_pci_aer_err_detected, | |
1006 | }; | |
1007 | ||
89e1f7d4 AW |
1008 | static 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, | |
dad9f897 | 1013 | .err_handler = &vfio_err_handlers, |
89e1f7d4 AW |
1014 | }; |
1015 | ||
93899a67 AW |
1016 | struct vfio_devices { |
1017 | struct vfio_device **devices; | |
1018 | int cur_index; | |
1019 | int max_index; | |
1020 | }; | |
bc4fba77 | 1021 | |
93899a67 | 1022 | static int vfio_pci_get_devs(struct pci_dev *pdev, void *data) |
bc4fba77 | 1023 | { |
93899a67 | 1024 | struct vfio_devices *devs = data; |
bc4fba77 AW |
1025 | struct pci_driver *pci_drv = ACCESS_ONCE(pdev->driver); |
1026 | ||
93899a67 AW |
1027 | if (pci_drv != &vfio_pci_driver) |
1028 | return -EBUSY; | |
bc4fba77 | 1029 | |
93899a67 AW |
1030 | if (devs->cur_index == devs->max_index) |
1031 | return -ENOSPC; | |
bc4fba77 | 1032 | |
93899a67 AW |
1033 | devs->devices[devs->cur_index] = vfio_device_get_from_dev(&pdev->dev); |
1034 | if (!devs->devices[devs->cur_index]) | |
1035 | return -EINVAL; | |
bc4fba77 | 1036 | |
93899a67 | 1037 | devs->cur_index++; |
bc4fba77 AW |
1038 | 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 | |
93899a67 AW |
1044 | * (!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. | |
bc4fba77 AW |
1052 | */ |
1053 | static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev) | |
1054 | { | |
93899a67 AW |
1055 | struct vfio_devices devs = { .cur_index = 0 }; |
1056 | int i = 0, ret = -EINVAL; | |
bc4fba77 | 1057 | bool needs_reset = false, slot = false; |
93899a67 | 1058 | struct vfio_pci_device *tmp; |
bc4fba77 AW |
1059 | |
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 | ||
93899a67 AW |
1065 | if (vfio_pci_for_each_slot_or_bus(vdev->pdev, vfio_pci_count_devs, |
1066 | &i, slot) || !i) | |
bc4fba77 AW |
1067 | return; |
1068 | ||
93899a67 AW |
1069 | devs.max_index = i; |
1070 | devs.devices = kcalloc(i, sizeof(struct vfio_device *), GFP_KERNEL); | |
1071 | if (!devs.devices) | |
bc4fba77 AW |
1072 | return; |
1073 | ||
93899a67 AW |
1074 | if (vfio_pci_for_each_slot_or_bus(vdev->pdev, |
1075 | vfio_pci_get_devs, &devs, slot)) | |
1076 | goto put_devs; | |
1077 | ||
1078 | 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 | } | |
1085 | ||
1086 | if (needs_reset) | |
1087 | ret = slot ? pci_try_reset_slot(vdev->pdev->slot) : | |
1088 | pci_try_reset_bus(vdev->pdev->bus); | |
1089 | ||
1090 | put_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); | |
bc4fba77 AW |
1100 | } |
1101 | ||
89e1f7d4 AW |
1102 | static void __exit vfio_pci_cleanup(void) |
1103 | { | |
1104 | pci_unregister_driver(&vfio_pci_driver); | |
89e1f7d4 AW |
1105 | vfio_pci_uninit_perm_bits(); |
1106 | } | |
1107 | ||
80c7e8cc AW |
1108 | static void __init vfio_pci_fill_ids(void) |
1109 | { | |
1110 | char *p, *id; | |
1111 | int rc; | |
1112 | ||
1113 | /* no ids passed actually */ | |
1114 | if (ids[0] == '\0') | |
1115 | return; | |
1116 | ||
1117 | /* add ids specified in the module parameter */ | |
1118 | p = ids; | |
1119 | while ((id = strsep(&p, ","))) { | |
1120 | unsigned int vendor, device, subvendor = PCI_ANY_ID, | |
1121 | subdevice = PCI_ANY_ID, class = 0, class_mask = 0; | |
1122 | int fields; | |
1123 | ||
1124 | if (!strlen(id)) | |
1125 | continue; | |
1126 | ||
1127 | fields = sscanf(id, "%x:%x:%x:%x:%x:%x", | |
1128 | &vendor, &device, &subvendor, &subdevice, | |
1129 | &class, &class_mask); | |
1130 | ||
1131 | if (fields < 2) { | |
1132 | pr_warn("invalid id string \"%s\"\n", id); | |
1133 | continue; | |
1134 | } | |
1135 | ||
1136 | rc = pci_add_dynid(&vfio_pci_driver, vendor, device, | |
1137 | subvendor, subdevice, class, class_mask, 0); | |
1138 | if (rc) | |
1139 | pr_warn("failed to add dynamic id [%04hx:%04hx[%04hx:%04hx]] class %#08x/%08x (%d)\n", | |
1140 | vendor, device, subvendor, subdevice, | |
1141 | class, class_mask, rc); | |
1142 | else | |
1143 | pr_info("add [%04hx:%04hx[%04hx:%04hx]] class %#08x/%08x\n", | |
1144 | vendor, device, subvendor, subdevice, | |
1145 | class, class_mask); | |
1146 | } | |
1147 | } | |
1148 | ||
89e1f7d4 AW |
1149 | static int __init vfio_pci_init(void) |
1150 | { | |
1151 | int ret; | |
1152 | ||
1153 | /* Allocate shared config space permision data used by all devices */ | |
1154 | ret = vfio_pci_init_perm_bits(); | |
1155 | if (ret) | |
1156 | return ret; | |
1157 | ||
89e1f7d4 AW |
1158 | /* Register and scan for devices */ |
1159 | ret = pci_register_driver(&vfio_pci_driver); | |
1160 | if (ret) | |
1161 | goto out_driver; | |
1162 | ||
80c7e8cc AW |
1163 | vfio_pci_fill_ids(); |
1164 | ||
89e1f7d4 AW |
1165 | return 0; |
1166 | ||
89e1f7d4 AW |
1167 | out_driver: |
1168 | vfio_pci_uninit_perm_bits(); | |
1169 | return ret; | |
1170 | } | |
1171 | ||
1172 | module_init(vfio_pci_init); | |
1173 | module_exit(vfio_pci_cleanup); | |
1174 | ||
1175 | MODULE_VERSION(DRIVER_VERSION); | |
1176 | MODULE_LICENSE("GPL v2"); | |
1177 | MODULE_AUTHOR(DRIVER_AUTHOR); | |
1178 | MODULE_DESCRIPTION(DRIVER_DESC); |