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