Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2014 Intel Corporation
3 : : */
4 : :
5 : : #include <uapi/linux/vfio.h>
6 : :
7 : : #include <unistd.h>
8 : : #include <string.h>
9 : : #include <fcntl.h>
10 : : #include <sys/eventfd.h>
11 : : #include <sys/socket.h>
12 : : #include <sys/ioctl.h>
13 : : #include <sys/mman.h>
14 : : #include <stdbool.h>
15 : :
16 : : #include <rte_log.h>
17 : : #include <rte_pci.h>
18 : : #include <rte_bus_pci.h>
19 : : #include <rte_eal_paging.h>
20 : : #include <rte_malloc.h>
21 : : #include <dev_vfio.h>
22 : : #include <rte_eal.h>
23 : : #include <rte_errno.h>
24 : : #include <bus_driver.h>
25 : : #include <rte_spinlock.h>
26 : : #include <rte_tailq.h>
27 : :
28 : : #include "eal_filesystem.h"
29 : :
30 : : #include "pci_init.h"
31 : : #include "private.h"
32 : :
33 : : static struct rte_tailq_elem dev_vfio_tailq = {
34 : : .name = "VFIO_RESOURCE_LIST",
35 : : };
36 [ - + ]: 305 : EAL_REGISTER_TAILQ(dev_vfio_tailq)
37 : :
38 : : static int
39 : : pci_vfio_get_region(const struct rte_pci_device *dev, int index,
40 : : uint64_t *size, uint64_t *offset)
41 : : {
42 : : const struct rte_pci_device_internal *pdev =
43 : : RTE_PCI_DEVICE_INTERNAL_CONST(dev);
44 : :
45 [ # # # # ]: 0 : if (index >= VFIO_PCI_NUM_REGIONS || index >= RTE_MAX_PCI_REGIONS)
46 : : return -1;
47 : :
48 [ # # # # : 0 : if (pdev->region[index].size == 0 && pdev->region[index].offset == 0)
# # # # #
# # # # #
# # # # #
# # # #
# ]
49 : : return -1;
50 : :
51 : : *size = pdev->region[index].size;
52 : 0 : *offset = pdev->region[index].offset;
53 : :
54 : 0 : return 0;
55 : : }
56 : :
57 : : int
58 : 0 : pci_vfio_read_config(const struct rte_pci_device *dev,
59 : : void *buf, size_t len, off_t offs)
60 : : {
61 : : uint64_t size, offset;
62 : : int fd;
63 : :
64 : 0 : fd = rte_intr_dev_fd_get(dev->intr_handle);
65 [ # # ]: 0 : if (fd < 0)
66 : : return -1;
67 : :
68 [ # # ]: 0 : if (pci_vfio_get_region(dev, VFIO_PCI_CONFIG_REGION_INDEX,
69 : : &size, &offset) != 0)
70 : : return -1;
71 : :
72 [ # # ]: 0 : if ((uint64_t)len + offs > size)
73 : : return -1;
74 : :
75 [ # # ]: 0 : return pread(fd, buf, len, offset + offs);
76 : : }
77 : :
78 : : int
79 : 0 : pci_vfio_write_config(const struct rte_pci_device *dev,
80 : : const void *buf, size_t len, off_t offs)
81 : : {
82 : : uint64_t size, offset;
83 : : int fd;
84 : :
85 : 0 : fd = rte_intr_dev_fd_get(dev->intr_handle);
86 [ # # ]: 0 : if (fd < 0)
87 : : return -1;
88 : :
89 [ # # ]: 0 : if (pci_vfio_get_region(dev, VFIO_PCI_CONFIG_REGION_INDEX,
90 : : &size, &offset) != 0)
91 : : return -1;
92 : :
93 [ # # ]: 0 : if ((uint64_t)len + offs > size)
94 : : return -1;
95 : :
96 : 0 : return pwrite(fd, buf, len, offset + offs);
97 : : }
98 : :
99 : : /* get PCI BAR number where MSI-X interrupts are */
100 : : static int
101 : 0 : pci_vfio_get_msix_bar(const struct rte_pci_device *dev,
102 : : struct pci_msix_table *msix_table)
103 : : {
104 : : off_t cap_offset;
105 : :
106 : 0 : cap_offset = rte_pci_find_capability(dev, RTE_PCI_CAP_ID_MSIX);
107 [ # # ]: 0 : if (cap_offset < 0)
108 : : return -1;
109 : :
110 [ # # ]: 0 : if (cap_offset != 0) {
111 : : uint16_t flags;
112 : : uint32_t reg;
113 : :
114 [ # # ]: 0 : if (rte_pci_read_config(dev, ®, sizeof(reg), cap_offset +
115 : : RTE_PCI_MSIX_TABLE) < 0) {
116 : 0 : PCI_LOG(ERR, "Cannot read MSIX table from PCI config space!");
117 : 0 : return -1;
118 : : }
119 : :
120 [ # # ]: 0 : if (rte_pci_read_config(dev, &flags, sizeof(flags), cap_offset +
121 : : RTE_PCI_MSIX_FLAGS) < 0) {
122 : 0 : PCI_LOG(ERR, "Cannot read MSIX flags from PCI config space!");
123 : 0 : return -1;
124 : : }
125 : :
126 : 0 : msix_table->bar_index = reg & RTE_PCI_MSIX_TABLE_BIR;
127 : 0 : msix_table->offset = reg & RTE_PCI_MSIX_TABLE_OFFSET;
128 : 0 : msix_table->size = 16 * (1 + (flags & RTE_PCI_MSIX_FLAGS_QSIZE));
129 : : }
130 : :
131 : : return 0;
132 : : }
133 : :
134 : : /* enable PCI bus memory space */
135 : : static int
136 [ # # ]: 0 : pci_vfio_enable_bus_memory(struct rte_pci_device *dev, int dev_fd)
137 : : {
138 : : uint64_t size, offset;
139 : : uint16_t cmd;
140 : : int ret;
141 : :
142 : : if (pci_vfio_get_region(dev, VFIO_PCI_CONFIG_REGION_INDEX,
143 : : &size, &offset) != 0) {
144 : 0 : PCI_LOG(ERR, "Cannot get offset of CONFIG region.");
145 : 0 : return -1;
146 : : }
147 : :
148 : 0 : ret = pread(dev_fd, &cmd, sizeof(cmd), offset + RTE_PCI_COMMAND);
149 : :
150 [ # # ]: 0 : if (ret != sizeof(cmd)) {
151 : 0 : PCI_LOG(ERR, "Cannot read command from PCI config space!");
152 : 0 : return -1;
153 : : }
154 : :
155 [ # # ]: 0 : if (cmd & RTE_PCI_COMMAND_MEMORY)
156 : : return 0;
157 : :
158 : 0 : cmd |= RTE_PCI_COMMAND_MEMORY;
159 : 0 : ret = pwrite(dev_fd, &cmd, sizeof(cmd), offset + RTE_PCI_COMMAND);
160 : :
161 [ # # ]: 0 : if (ret != sizeof(cmd)) {
162 : 0 : PCI_LOG(ERR, "Cannot write command to PCI config space!");
163 : 0 : return -1;
164 : : }
165 : :
166 : : return 0;
167 : : }
168 : :
169 : : /* set up interrupt support (but not enable interrupts) */
170 : : static int
171 : 0 : pci_vfio_setup_interrupts(struct rte_pci_device *dev, int vfio_dev_fd)
172 : : {
173 : : int i, ret, intr_idx;
174 : : enum rte_intr_mode intr_mode;
175 : :
176 : : /* default to invalid index */
177 : : intr_idx = VFIO_PCI_NUM_IRQS;
178 : :
179 : : /* Get default / configured intr_mode */
180 : 0 : intr_mode = rte_eal_vfio_intr_mode();
181 : :
182 : : /* get interrupt type from internal config (MSI-X by default, can be
183 : : * overridden from the command line
184 : : */
185 : : switch (intr_mode) {
186 : : case RTE_INTR_MODE_MSIX:
187 : : intr_idx = VFIO_PCI_MSIX_IRQ_INDEX;
188 : : break;
189 : : case RTE_INTR_MODE_MSI:
190 : : intr_idx = VFIO_PCI_MSI_IRQ_INDEX;
191 : : break;
192 : : case RTE_INTR_MODE_LEGACY:
193 : : intr_idx = VFIO_PCI_INTX_IRQ_INDEX;
194 : : break;
195 : : /* don't do anything if we want to automatically determine interrupt type */
196 : : case RTE_INTR_MODE_NONE:
197 : : break;
198 : 0 : default:
199 : 0 : PCI_LOG(ERR, "Unknown default interrupt type!");
200 : 0 : return -1;
201 : : }
202 : :
203 : : /* start from MSI-X interrupt type */
204 [ # # ]: 0 : for (i = VFIO_PCI_MSIX_IRQ_INDEX; i >= 0; i--) {
205 : 0 : struct vfio_irq_info irq = { .argsz = sizeof(irq) };
206 : : int fd = -1;
207 : :
208 : : /* skip interrupt modes we don't want */
209 : 0 : if (intr_mode != RTE_INTR_MODE_NONE &&
210 [ # # ]: 0 : i != intr_idx)
211 : 0 : continue;
212 : :
213 : 0 : irq.index = i;
214 : :
215 : 0 : ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_IRQ_INFO, &irq);
216 [ # # ]: 0 : if (ret < 0) {
217 : 0 : PCI_LOG(ERR, "Cannot get VFIO IRQ info, error %i (%s)",
218 : : errno, strerror(errno));
219 : 0 : return -1;
220 : : }
221 : :
222 : : /* if this vector cannot be used with eventfd, fail if we explicitly
223 : : * specified interrupt type, otherwise continue */
224 [ # # ]: 0 : if ((irq.flags & VFIO_IRQ_INFO_EVENTFD) == 0) {
225 [ # # ]: 0 : if (intr_mode != RTE_INTR_MODE_NONE) {
226 : 0 : PCI_LOG(ERR, "Interrupt vector does not support eventfd!");
227 : 0 : return -1;
228 : : } else
229 : 0 : continue;
230 : : }
231 : :
232 : : /* Reallocate the efds and elist fields of intr_handle based
233 : : * on PCI device MSIX size.
234 : : */
235 [ # # ]: 0 : if (i == VFIO_PCI_MSIX_IRQ_INDEX &&
236 [ # # # # ]: 0 : (uint32_t)rte_intr_nb_intr_get(dev->intr_handle) < irq.count &&
237 : 0 : rte_intr_event_list_update(dev->intr_handle, irq.count))
238 : : return -1;
239 : :
240 : : /* set up an eventfd for interrupts */
241 : 0 : fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
242 [ # # ]: 0 : if (fd < 0) {
243 : 0 : PCI_LOG(ERR, "Cannot set up eventfd, error %i (%s)",
244 : : errno, strerror(errno));
245 : 0 : return -1;
246 : : }
247 : :
248 [ # # ]: 0 : if (rte_intr_fd_set(dev->intr_handle, fd))
249 : : return -1;
250 : :
251 [ # # # ]: 0 : switch (i) {
252 : 0 : case VFIO_PCI_MSIX_IRQ_INDEX:
253 : : intr_mode = RTE_INTR_MODE_MSIX;
254 : 0 : rte_intr_type_set(dev->intr_handle,
255 : : RTE_INTR_HANDLE_VFIO_MSIX);
256 : 0 : break;
257 : 0 : case VFIO_PCI_MSI_IRQ_INDEX:
258 : : intr_mode = RTE_INTR_MODE_MSI;
259 : 0 : rte_intr_type_set(dev->intr_handle,
260 : : RTE_INTR_HANDLE_VFIO_MSI);
261 : 0 : break;
262 : 0 : case VFIO_PCI_INTX_IRQ_INDEX:
263 : : intr_mode = RTE_INTR_MODE_LEGACY;
264 : 0 : rte_intr_type_set(dev->intr_handle,
265 : : RTE_INTR_HANDLE_VFIO_LEGACY);
266 : 0 : break;
267 : : default:
268 : : PCI_LOG(ERR, "Unknown interrupt type!");
269 : : return -1;
270 : : }
271 : :
272 : : return 0;
273 : : }
274 : :
275 : : /* if we're here, we haven't found a suitable interrupt vector */
276 : : return -1;
277 : : }
278 : :
279 : : /*
280 : : * Spinlock for device hot-unplug failure handling.
281 : : * If it tries to access bus or device, such as handle sigbus on bus
282 : : * or handle memory failure for device, just need to use this lock.
283 : : * It could protect the bus and the device to avoid race condition.
284 : : */
285 : : static rte_spinlock_t failure_handle_lock = RTE_SPINLOCK_INITIALIZER;
286 : :
287 : : static void
288 : 0 : pci_vfio_req_handler(void *param)
289 : : {
290 : : struct rte_bus *bus;
291 : : int ret;
292 : : struct rte_device *device = (struct rte_device *)param;
293 : :
294 : : rte_spinlock_lock(&failure_handle_lock);
295 : 0 : bus = rte_bus_find_by_device(device);
296 [ # # ]: 0 : if (bus == NULL) {
297 : 0 : PCI_LOG(ERR, "Cannot find bus for device (%s)", device->name);
298 : 0 : goto handle_end;
299 : : }
300 : :
301 : : /*
302 : : * vfio kernel module request user space to release allocated
303 : : * resources before device be deleted in kernel, so it can directly
304 : : * call the vfio bus hot-unplug handler to process it.
305 : : */
306 : 0 : ret = bus->hot_unplug_handler(device);
307 [ # # ]: 0 : if (ret)
308 : 0 : PCI_LOG(ERR, "Can not handle hot-unplug for device (%s)", device->name);
309 : 0 : handle_end:
310 : : rte_spinlock_unlock(&failure_handle_lock);
311 : 0 : }
312 : :
313 : : /* enable notifier (only enable req now) */
314 : : static int
315 : 0 : pci_vfio_enable_notifier(struct rte_pci_device *dev, int vfio_dev_fd)
316 : : {
317 : : int ret;
318 : : int fd = -1;
319 : :
320 : : /* set up an eventfd for req notifier */
321 : 0 : fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
322 [ # # ]: 0 : if (fd < 0) {
323 : 0 : PCI_LOG(ERR, "Cannot set up eventfd, error %i (%s)",
324 : : errno, strerror(errno));
325 : 0 : return -1;
326 : : }
327 : :
328 [ # # ]: 0 : if (rte_intr_fd_set(dev->vfio_req_intr_handle, fd))
329 : : return -1;
330 : :
331 [ # # ]: 0 : if (rte_intr_type_set(dev->vfio_req_intr_handle, RTE_INTR_HANDLE_VFIO_REQ))
332 : : return -1;
333 : :
334 [ # # ]: 0 : if (rte_intr_dev_fd_set(dev->vfio_req_intr_handle, vfio_dev_fd))
335 : : return -1;
336 : :
337 : 0 : ret = rte_intr_callback_register(dev->vfio_req_intr_handle,
338 : : pci_vfio_req_handler,
339 : 0 : (void *)&dev->device);
340 [ # # ]: 0 : if (ret) {
341 : 0 : PCI_LOG(ERR, "Fail to register req notifier handler.");
342 : 0 : goto error;
343 : : }
344 : :
345 : 0 : ret = rte_intr_enable(dev->vfio_req_intr_handle);
346 [ # # ]: 0 : if (ret) {
347 : 0 : PCI_LOG(ERR, "Fail to enable req notifier.");
348 : 0 : ret = rte_intr_callback_unregister(dev->vfio_req_intr_handle,
349 : : pci_vfio_req_handler,
350 : : (void *)&dev->device);
351 [ # # ]: 0 : if (ret < 0)
352 : 0 : PCI_LOG(ERR, "Fail to unregister req notifier handler.");
353 : 0 : goto error;
354 : : }
355 : :
356 : : return 0;
357 : 0 : error:
358 : 0 : rte_intr_fd_close(dev->vfio_req_intr_handle);
359 : 0 : rte_intr_type_set(dev->vfio_req_intr_handle, RTE_INTR_HANDLE_UNKNOWN);
360 : : /* vfio_dev_fd is managed by VFIO layer, only clear reference here. */
361 : 0 : rte_intr_dev_fd_set(dev->vfio_req_intr_handle, -1);
362 : :
363 : 0 : return -1;
364 : : }
365 : :
366 : : /* disable notifier (only disable req now) */
367 : : static int
368 : 0 : pci_vfio_disable_notifier(struct rte_pci_device *dev)
369 : : {
370 : : int ret;
371 : :
372 : 0 : ret = rte_intr_disable(dev->vfio_req_intr_handle);
373 [ # # ]: 0 : if (ret) {
374 : 0 : PCI_LOG(ERR, "fail to disable req notifier.");
375 : 0 : return -1;
376 : : }
377 : :
378 : 0 : ret = rte_intr_callback_unregister_sync(dev->vfio_req_intr_handle,
379 : : pci_vfio_req_handler,
380 : 0 : (void *)&dev->device);
381 [ # # ]: 0 : if (ret < 0) {
382 : 0 : PCI_LOG(ERR, "fail to unregister req notifier handler.");
383 : 0 : return -1;
384 : : }
385 : :
386 : 0 : rte_intr_fd_close(dev->vfio_req_intr_handle);
387 : 0 : rte_intr_type_set(dev->vfio_req_intr_handle, RTE_INTR_HANDLE_UNKNOWN);
388 : : /* vfio_dev_fd is managed by VFIO layer, only clear reference here. */
389 : 0 : rte_intr_dev_fd_set(dev->vfio_req_intr_handle, -1);
390 : :
391 : 0 : return 0;
392 : : }
393 : :
394 : : static int
395 [ # # ]: 0 : pci_vfio_is_ioport_bar(const struct rte_pci_device *dev, int vfio_dev_fd,
396 : : int bar_index)
397 : : {
398 : : uint64_t size, offset;
399 : : uint32_t ioport_bar;
400 : : int ret;
401 : :
402 : : if (pci_vfio_get_region(dev, VFIO_PCI_CONFIG_REGION_INDEX,
403 : : &size, &offset) != 0) {
404 : 0 : PCI_LOG(ERR, "Cannot get offset of CONFIG region.");
405 : 0 : return -1;
406 : : }
407 : :
408 : 0 : ret = pread(vfio_dev_fd, &ioport_bar, sizeof(ioport_bar),
409 : 0 : offset + RTE_PCI_BASE_ADDRESS_0 + bar_index * 4);
410 [ # # ]: 0 : if (ret != sizeof(ioport_bar)) {
411 : 0 : PCI_LOG(ERR, "Cannot read command (%x) from config space!",
412 : : RTE_PCI_BASE_ADDRESS_0 + bar_index*4);
413 : 0 : return -1;
414 : : }
415 : :
416 : 0 : return (ioport_bar & RTE_PCI_BASE_ADDRESS_SPACE_IO) != 0;
417 : : }
418 : :
419 : : static int
420 : 0 : pci_vfio_setup_device(struct rte_pci_device *dev, int vfio_dev_fd)
421 : : {
422 [ # # ]: 0 : if (pci_vfio_setup_interrupts(dev, vfio_dev_fd) != 0) {
423 : 0 : PCI_LOG(ERR, "Error setting up interrupts!");
424 : 0 : return -1;
425 : : }
426 : :
427 [ # # ]: 0 : if (pci_vfio_enable_bus_memory(dev, vfio_dev_fd)) {
428 : 0 : PCI_LOG(ERR, "Cannot enable bus memory!");
429 : 0 : return -1;
430 : : }
431 : :
432 [ # # ]: 0 : if (rte_pci_set_bus_master(dev, true)) {
433 : 0 : PCI_LOG(ERR, "Cannot set up bus mastering!");
434 : 0 : return -1;
435 : : }
436 : :
437 : : /*
438 : : * Reset the device. If the device is not capable of resetting,
439 : : * then it updates errno as EINVAL.
440 : : */
441 [ # # # # ]: 0 : if (ioctl(vfio_dev_fd, VFIO_DEVICE_RESET) && errno != EINVAL) {
442 : 0 : PCI_LOG(ERR, "Unable to reset device! Error: %d (%s)", errno, strerror(errno));
443 : 0 : return -1;
444 : : }
445 : :
446 : : return 0;
447 : : }
448 : :
449 : : static int
450 : 0 : pci_vfio_mmap_bar(int vfio_dev_fd, struct mapped_pci_resource *vfio_res,
451 : : int bar_index, int additional_flags)
452 : : {
453 : : struct memreg {
454 : : uint64_t offset;
455 : : size_t size;
456 : : } memreg[2] = {};
457 : : void *bar_addr;
458 : : struct pci_msix_table *msix_table = &vfio_res->msix_table;
459 : : struct pci_map *bar = &vfio_res->maps[bar_index];
460 : :
461 [ # # ]: 0 : if (bar->size == 0) {
462 : 0 : PCI_LOG(DEBUG, "Bar size is 0, skip BAR%d", bar_index);
463 : 0 : return 0;
464 : : }
465 : :
466 [ # # ]: 0 : if (msix_table->bar_index == bar_index) {
467 : : /*
468 : : * VFIO will not let us map the MSI-X table,
469 : : * but we can map around it.
470 : : */
471 : 0 : uint32_t table_start = msix_table->offset;
472 : 0 : uint32_t table_end = table_start + msix_table->size;
473 : 0 : table_end = RTE_ALIGN(table_end, rte_mem_page_size());
474 : 0 : table_start = RTE_ALIGN_FLOOR(table_start, rte_mem_page_size());
475 : :
476 : : /* If page-aligned start of MSI-X table is less than the
477 : : * actual MSI-X table start address, reassign to the actual
478 : : * start address.
479 : : */
480 : 0 : if (table_start < msix_table->offset)
481 : : table_start = msix_table->offset;
482 : :
483 [ # # # # ]: 0 : if (table_start == 0 && table_end >= bar->size) {
484 : : /* Cannot map this BAR */
485 : 0 : PCI_LOG(DEBUG, "Skipping BAR%d", bar_index);
486 : 0 : bar->size = 0;
487 : 0 : bar->addr = 0;
488 : 0 : return 0;
489 : : }
490 : :
491 : 0 : memreg[0].offset = bar->offset;
492 : 0 : memreg[0].size = table_start;
493 [ # # ]: 0 : if (bar->size < table_end) {
494 : : /*
495 : : * If MSI-X table end is beyond BAR end, don't attempt
496 : : * to perform second mapping.
497 : : */
498 : : memreg[1].offset = 0;
499 : : memreg[1].size = 0;
500 : : } else {
501 : 0 : memreg[1].offset = bar->offset + table_end;
502 : 0 : memreg[1].size = bar->size - table_end;
503 : : }
504 : :
505 : 0 : PCI_LOG(DEBUG, "Trying to map BAR%d that contains the MSI-X table. "
506 : : "Trying offsets: 0x%04" PRIx64 ":0x%04zx, 0x%04" PRIx64 ":0x%04zx",
507 : : bar_index,
508 : : memreg[0].offset, memreg[0].size,
509 : : memreg[1].offset, memreg[1].size);
510 : : } else {
511 : 0 : memreg[0].offset = bar->offset;
512 : : memreg[0].size = bar->size;
513 : : }
514 : :
515 : : /* reserve the address using an inaccessible mapping */
516 : 0 : bar_addr = mmap(bar->addr, bar->size, 0, MAP_PRIVATE |
517 : : MAP_ANONYMOUS | additional_flags, -1, 0);
518 [ # # ]: 0 : if (bar_addr != MAP_FAILED) {
519 : : void *map_addr = NULL;
520 [ # # ]: 0 : if (memreg[0].size) {
521 : : /* actual map of first part */
522 : 0 : map_addr = pci_map_resource(bar_addr, vfio_dev_fd,
523 : : memreg[0].offset,
524 : : memreg[0].size,
525 : : RTE_MAP_FORCE_ADDRESS);
526 : : }
527 : :
528 : : /*
529 : : * Regarding "memreg[0].size == 0":
530 : : * If this BAR has MSI-X table, memreg[0].size (the
531 : : * first part or the part before the table) can
532 : : * legitimately be 0 for hardware using vector table
533 : : * offset 0 (i.e. first part does not exist).
534 : : *
535 : : * When memreg[0].size is 0, "mapping the first part"
536 : : * never happens, and map_addr is NULL at this
537 : : * point. So check that mapping has been actually
538 : : * attempted.
539 : : */
540 : : /* if there's a second part, try to map it */
541 [ # # # # ]: 0 : if ((map_addr != NULL || memreg[0].size == 0)
542 [ # # # # ]: 0 : && memreg[1].offset && memreg[1].size) {
543 : 0 : void *second_addr = RTE_PTR_ADD(bar_addr,
544 : : (uintptr_t)(memreg[1].offset -
545 : : bar->offset));
546 : 0 : map_addr = pci_map_resource(second_addr,
547 : : vfio_dev_fd,
548 : : memreg[1].offset,
549 : : memreg[1].size,
550 : : RTE_MAP_FORCE_ADDRESS);
551 : : }
552 : :
553 [ # # ]: 0 : if (map_addr == NULL) {
554 : 0 : munmap(bar_addr, bar->size);
555 : : bar_addr = MAP_FAILED;
556 : 0 : PCI_LOG(ERR, "Failed to map pci BAR%d", bar_index);
557 : 0 : return -1;
558 : : }
559 : : } else {
560 : 0 : PCI_LOG(ERR, "Failed to create inaccessible mapping for BAR%d", bar_index);
561 : 0 : return -1;
562 : : }
563 : :
564 : 0 : bar->addr = bar_addr;
565 : 0 : return 0;
566 : : }
567 : :
568 : : static int
569 : 0 : pci_vfio_sparse_mmap_bar(int vfio_dev_fd, struct mapped_pci_resource *vfio_res,
570 : : int bar_index, int additional_flags)
571 : : {
572 : : struct pci_map *bar = &vfio_res->maps[bar_index];
573 : : struct vfio_region_sparse_mmap_area *sparse;
574 : : void *bar_addr;
575 : : uint32_t i;
576 : :
577 [ # # ]: 0 : if (bar->size == 0) {
578 : 0 : PCI_LOG(DEBUG, "Bar size is 0, skip BAR%d", bar_index);
579 : 0 : return 0;
580 : : }
581 : :
582 : : /* reserve the address using an inaccessible mapping */
583 : 0 : bar_addr = mmap(bar->addr, bar->size, 0, MAP_PRIVATE |
584 : : MAP_ANONYMOUS | additional_flags, -1, 0);
585 [ # # ]: 0 : if (bar_addr != MAP_FAILED) {
586 : : void *map_addr = NULL;
587 [ # # ]: 0 : for (i = 0; i < bar->nr_areas; i++) {
588 : 0 : sparse = &bar->areas[i];
589 [ # # ]: 0 : if (sparse->size) {
590 : 0 : void *addr = RTE_PTR_ADD(bar_addr, (uintptr_t)sparse->offset);
591 : 0 : map_addr = pci_map_resource(addr, vfio_dev_fd,
592 : 0 : bar->offset + sparse->offset, sparse->size,
593 : : RTE_MAP_FORCE_ADDRESS);
594 [ # # ]: 0 : if (map_addr == NULL) {
595 : 0 : munmap(bar_addr, bar->size);
596 : 0 : PCI_LOG(ERR, "Failed to map pci BAR%d", bar_index);
597 : 0 : goto err_map;
598 : : }
599 : : }
600 : : }
601 : : } else {
602 : 0 : PCI_LOG(ERR, "Failed to create inaccessible mapping for BAR%d", bar_index);
603 : 0 : goto err_map;
604 : : }
605 : :
606 : 0 : bar->addr = bar_addr;
607 : 0 : return 0;
608 : :
609 : 0 : err_map:
610 : 0 : bar->nr_areas = 0;
611 : 0 : return -1;
612 : : }
613 : :
614 : : /*
615 : : * region info may contain capability headers, so we need to keep reallocating
616 : : * the memory until we match allocated memory size with argsz.
617 : : */
618 : : static int
619 : 0 : pci_vfio_get_region_info(int vfio_dev_fd, struct vfio_region_info **info,
620 : : int region)
621 : : {
622 : : struct vfio_region_info *ri;
623 : : size_t argsz = sizeof(*ri);
624 : : int ret;
625 : :
626 : 0 : ri = malloc(sizeof(*ri));
627 [ # # ]: 0 : if (ri == NULL) {
628 : 0 : PCI_LOG(ERR, "Cannot allocate memory for VFIO region info");
629 : 0 : return -1;
630 : : }
631 : 0 : again:
632 : : memset(ri, 0, argsz);
633 : 0 : ri->argsz = argsz;
634 : 0 : ri->index = region;
635 : :
636 : 0 : ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_REGION_INFO, ri);
637 [ # # ]: 0 : if (ret < 0) {
638 : 0 : free(ri);
639 : 0 : return ret;
640 : : }
641 [ # # ]: 0 : if (ri->argsz != argsz) {
642 : : struct vfio_region_info *tmp;
643 : :
644 : : argsz = ri->argsz;
645 : 0 : tmp = realloc(ri, argsz);
646 : :
647 [ # # ]: 0 : if (tmp == NULL) {
648 : : /* realloc failed but the ri is still there */
649 : 0 : free(ri);
650 : 0 : PCI_LOG(ERR, "Cannot reallocate memory for VFIO region info");
651 : 0 : return -1;
652 : : }
653 : : ri = tmp;
654 : 0 : goto again;
655 : : }
656 : 0 : *info = ri;
657 : :
658 : 0 : return 0;
659 : : }
660 : :
661 : : static struct vfio_info_cap_header *
662 : : pci_vfio_info_cap(struct vfio_region_info *info, int cap)
663 : : {
664 : : struct vfio_info_cap_header *h;
665 : : size_t offset;
666 : :
667 : 0 : if ((info->flags & VFIO_REGION_INFO_FLAG_CAPS) == 0) {
668 : : /* VFIO info does not advertise capabilities */
669 : : return NULL;
670 : : }
671 : :
672 : 0 : offset = info->cap_offset;
673 [ # # # # ]: 0 : while (offset != 0) {
674 : 0 : h = RTE_PTR_ADD(info, offset);
675 [ # # # # ]: 0 : if (h->id == cap)
676 : : return h;
677 : 0 : offset = h->next;
678 : : }
679 : : return NULL;
680 : : }
681 : :
682 : : static int
683 : 0 : pci_vfio_msix_is_mappable(int vfio_dev_fd, int msix_region)
684 : : {
685 : 0 : struct vfio_region_info *info = NULL;
686 : : int ret;
687 : :
688 : 0 : ret = pci_vfio_get_region_info(vfio_dev_fd, &info, msix_region);
689 [ # # ]: 0 : if (ret < 0)
690 : : return -1;
691 : :
692 [ # # ]: 0 : ret = pci_vfio_info_cap(info, VFIO_REGION_INFO_CAP_MSIX_MAPPABLE) != NULL;
693 : :
694 : : /* cleanup */
695 : 0 : free(info);
696 : :
697 : 0 : return ret;
698 : : }
699 : :
700 : : static int
701 : 0 : pci_vfio_fill_regions(struct rte_pci_device *dev, int vfio_dev_fd,
702 : : struct vfio_device_info *device_info)
703 : : {
704 : : struct rte_pci_device_internal *pdev = RTE_PCI_DEVICE_INTERNAL(dev);
705 : 0 : struct vfio_region_info *reg = NULL;
706 : : int nb_maps, i, ret;
707 : :
708 : 0 : nb_maps = RTE_MIN((int)device_info->num_regions,
709 : : VFIO_PCI_CONFIG_REGION_INDEX + 1);
710 : :
711 [ # # ]: 0 : for (i = 0; i < nb_maps; i++) {
712 : 0 : ret = pci_vfio_get_region_info(vfio_dev_fd, ®, i);
713 [ # # ]: 0 : if (ret < 0) {
714 : 0 : PCI_LOG(DEBUG, "%s cannot get device region info error %i (%s)",
715 : : dev->name, errno, strerror(errno));
716 : 0 : return -1;
717 : : }
718 : :
719 : 0 : pdev->region[i].size = reg->size;
720 : 0 : pdev->region[i].offset = reg->offset;
721 : :
722 : 0 : free(reg);
723 : : }
724 : :
725 : : return 0;
726 : : }
727 : :
728 : : static int
729 : 0 : pci_vfio_map_resource_primary(struct rte_pci_device *dev)
730 : : {
731 : : struct rte_pci_device_internal *pdev = RTE_PCI_DEVICE_INTERNAL(dev);
732 : 0 : struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
733 : 0 : struct vfio_region_info *reg = NULL;
734 : 0 : char pci_addr[PATH_MAX] = {0};
735 : : int vfio_dev_fd;
736 : : struct rte_pci_addr *loc = &dev->addr;
737 : : int i, j, ret;
738 : : struct mapped_pci_resource *vfio_res = NULL;
739 : : struct mapped_pci_res_list *vfio_res_list =
740 : 0 : RTE_TAILQ_CAST(dev_vfio_tailq.head, mapped_pci_res_list);
741 : :
742 : : struct pci_map *maps;
743 : :
744 : : /* store PCI address string */
745 : 0 : snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
746 : 0 : loc->domain, loc->bus, loc->devid, loc->function);
747 : :
748 : 0 : ret = dev_vfio_setup_device(rte_pci_get_sysfs_path(), pci_addr, &vfio_dev_fd);
749 [ # # ]: 0 : if (ret < 0) {
750 : : /* Device not managed by VFIO - skip */
751 [ # # ]: 0 : if (rte_errno == ENODEV)
752 : : ret = 1;
753 : 0 : return ret;
754 : : }
755 : :
756 : 0 : ret = dev_vfio_get_device_info(vfio_dev_fd, &device_info);
757 [ # # ]: 0 : if (ret)
758 : 0 : goto err_vfio_dev_fd;
759 : :
760 [ # # ]: 0 : if (rte_intr_dev_fd_set(dev->intr_handle, vfio_dev_fd))
761 : 0 : goto err_vfio_dev_fd;
762 : :
763 : : /* allocate vfio_res and get region info */
764 : 0 : vfio_res = rte_zmalloc("VFIO_RES", sizeof(*vfio_res), 0);
765 [ # # ]: 0 : if (vfio_res == NULL) {
766 : 0 : PCI_LOG(ERR, "Cannot store VFIO mmap details");
767 : 0 : goto err_vfio_dev_fd;
768 : : }
769 : 0 : memcpy(&vfio_res->pci_addr, &dev->addr, sizeof(vfio_res->pci_addr));
770 : :
771 : : /* get number of registers (up to BAR5) */
772 : 0 : vfio_res->nb_maps = RTE_MIN((int) device_info.num_regions,
773 : : VFIO_PCI_BAR5_REGION_INDEX + 1);
774 : :
775 : : /* map BARs */
776 : 0 : maps = vfio_res->maps;
777 : :
778 : 0 : ret = pci_vfio_get_region_info(vfio_dev_fd, ®,
779 : : VFIO_PCI_CONFIG_REGION_INDEX);
780 [ # # ]: 0 : if (ret < 0) {
781 : 0 : PCI_LOG(ERR, "%s cannot get device region info error %i (%s)",
782 : : dev->name, errno, strerror(errno));
783 : 0 : goto err_vfio_res;
784 : : }
785 : 0 : pdev->region[VFIO_PCI_CONFIG_REGION_INDEX].size = reg->size;
786 : 0 : pdev->region[VFIO_PCI_CONFIG_REGION_INDEX].offset = reg->offset;
787 : 0 : free(reg);
788 : :
789 : 0 : vfio_res->msix_table.bar_index = -1;
790 : : /* get MSI-X BAR, if any (we have to know where it is because we can't
791 : : * easily mmap it when using VFIO)
792 : : */
793 : 0 : ret = pci_vfio_get_msix_bar(dev, &vfio_res->msix_table);
794 [ # # ]: 0 : if (ret < 0) {
795 : 0 : PCI_LOG(ERR, "%s cannot get MSI-X BAR number!", pci_addr);
796 : 0 : goto err_vfio_res;
797 : : }
798 : : /* if we found our MSI-X BAR region, check if we can mmap it */
799 [ # # ]: 0 : if (vfio_res->msix_table.bar_index != -1) {
800 : 0 : ret = pci_vfio_msix_is_mappable(vfio_dev_fd,
801 : : vfio_res->msix_table.bar_index);
802 [ # # ]: 0 : if (ret < 0) {
803 : 0 : PCI_LOG(ERR, "Couldn't check if MSI-X BAR is mappable");
804 : 0 : goto err_vfio_res;
805 [ # # ]: 0 : } else if (ret != 0) {
806 : : /* we can map it, so we don't care where it is */
807 : 0 : PCI_LOG(DEBUG, "VFIO reports MSI-X BAR as mappable");
808 : 0 : vfio_res->msix_table.bar_index = -1;
809 : : }
810 : : }
811 : :
812 [ # # ]: 0 : for (i = 0; i < vfio_res->nb_maps; i++) {
813 : : void *bar_addr;
814 : : struct vfio_info_cap_header *hdr;
815 : : struct vfio_region_info_cap_sparse_mmap *sparse;
816 : :
817 : 0 : ret = pci_vfio_get_region_info(vfio_dev_fd, ®, i);
818 [ # # ]: 0 : if (ret < 0) {
819 : 0 : PCI_LOG(ERR, "%s cannot get device region info error %i (%s)",
820 : : pci_addr, errno, strerror(errno));
821 : 0 : goto err_map;
822 : : }
823 : :
824 : 0 : pdev->region[i].size = reg->size;
825 : 0 : pdev->region[i].offset = reg->offset;
826 : :
827 : : /* chk for io port region */
828 : 0 : ret = pci_vfio_is_ioport_bar(dev, vfio_dev_fd, i);
829 [ # # ]: 0 : if (ret < 0) {
830 : 0 : free(reg);
831 : 0 : goto err_map;
832 [ # # ]: 0 : } else if (ret) {
833 : 0 : PCI_LOG(INFO, "Ignore mapping IO port bar(%d)", i);
834 : 0 : free(reg);
835 : 0 : continue;
836 : : }
837 : :
838 : : /* skip non-mmappable BARs */
839 [ # # ]: 0 : if ((reg->flags & VFIO_REGION_INFO_FLAG_MMAP) == 0) {
840 : 0 : free(reg);
841 : 0 : continue;
842 : : }
843 : :
844 : : /* try mapping somewhere close to the end of hugepages */
845 [ # # ]: 0 : if (pci_map_addr == NULL)
846 : 0 : pci_map_addr = pci_find_max_end_va();
847 : :
848 : 0 : bar_addr = pci_map_addr;
849 : 0 : pci_map_addr = RTE_PTR_ADD(bar_addr, (size_t) reg->size);
850 : :
851 : 0 : pci_map_addr = RTE_PTR_ALIGN(pci_map_addr,
852 : : sysconf(_SC_PAGE_SIZE));
853 : :
854 : 0 : maps[i].addr = bar_addr;
855 : 0 : maps[i].offset = reg->offset;
856 : 0 : maps[i].size = reg->size;
857 [ # # ]: 0 : maps[i].path = NULL; /* vfio doesn't have per-resource paths */
858 : :
859 : : hdr = pci_vfio_info_cap(reg, VFIO_REGION_INFO_CAP_SPARSE_MMAP);
860 : :
861 [ # # ]: 0 : if (hdr != NULL) {
862 : : sparse = container_of(hdr,
863 : : struct vfio_region_info_cap_sparse_mmap, header);
864 [ # # ]: 0 : if (sparse->nr_areas > 0) {
865 : 0 : maps[i].nr_areas = sparse->nr_areas;
866 : 0 : maps[i].areas = rte_zmalloc(NULL,
867 : 0 : sizeof(*maps[i].areas) * maps[i].nr_areas, 0);
868 [ # # ]: 0 : if (maps[i].areas == NULL) {
869 : 0 : PCI_LOG(ERR, "Cannot alloc memory for sparse map areas");
870 : 0 : goto err_map;
871 : : }
872 : 0 : memcpy(maps[i].areas, sparse->areas,
873 : 0 : sizeof(*maps[i].areas) * maps[i].nr_areas);
874 : : }
875 : : }
876 : :
877 [ # # ]: 0 : if (maps[i].nr_areas > 0) {
878 : 0 : ret = pci_vfio_sparse_mmap_bar(vfio_dev_fd, vfio_res, i, 0);
879 [ # # ]: 0 : if (ret < 0) {
880 : 0 : PCI_LOG(ERR, "%s sparse mapping BAR%i failed: %s",
881 : : pci_addr, i, strerror(errno));
882 : 0 : free(reg);
883 : 0 : goto err_map;
884 : : }
885 : : } else {
886 : 0 : ret = pci_vfio_mmap_bar(vfio_dev_fd, vfio_res, i, 0);
887 [ # # ]: 0 : if (ret < 0) {
888 : 0 : PCI_LOG(ERR, "%s mapping BAR%i failed: %s",
889 : : pci_addr, i, strerror(errno));
890 : 0 : free(reg);
891 : 0 : goto err_map;
892 : : }
893 : : }
894 : :
895 : 0 : dev->mem_resource[i].addr = maps[i].addr;
896 : :
897 : 0 : free(reg);
898 : : }
899 : :
900 [ # # ]: 0 : if (pci_vfio_setup_device(dev, vfio_dev_fd) < 0) {
901 : 0 : PCI_LOG(ERR, "%s setup device failed", pci_addr);
902 : 0 : goto err_map;
903 : : }
904 : :
905 [ # # ]: 0 : if (pci_vfio_enable_notifier(dev, vfio_dev_fd) != 0) {
906 : 0 : PCI_LOG(ERR, "Error setting up notifier!");
907 : 0 : goto err_map;
908 : : }
909 : :
910 : 0 : TAILQ_INSERT_TAIL(vfio_res_list, vfio_res, next);
911 : :
912 : 0 : return 0;
913 : 0 : err_map:
914 [ # # ]: 0 : for (j = 0; j < i; j++) {
915 [ # # ]: 0 : if (maps[j].addr)
916 : 0 : pci_unmap_resource(maps[j].addr, maps[j].size);
917 [ # # ]: 0 : if (maps[j].nr_areas > 0)
918 : 0 : rte_free(maps[j].areas);
919 : : }
920 : 0 : err_vfio_res:
921 : 0 : rte_free(vfio_res);
922 : 0 : err_vfio_dev_fd:
923 : 0 : dev_vfio_release_device(rte_pci_get_sysfs_path(),
924 : : pci_addr, vfio_dev_fd);
925 : 0 : return -1;
926 : : }
927 : :
928 : : static int
929 : 0 : pci_vfio_map_resource_secondary(struct rte_pci_device *dev)
930 : : {
931 : 0 : struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
932 : 0 : char pci_addr[PATH_MAX] = {0};
933 : : int vfio_dev_fd;
934 : : struct rte_pci_addr *loc = &dev->addr;
935 : : int j, ret, i = 0;
936 : : struct mapped_pci_resource *vfio_res = NULL;
937 : : struct mapped_pci_res_list *vfio_res_list =
938 : 0 : RTE_TAILQ_CAST(dev_vfio_tailq.head, mapped_pci_res_list);
939 : :
940 : : struct pci_map *maps;
941 : :
942 : : /* store PCI address string */
943 : 0 : snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
944 : 0 : loc->domain, loc->bus, loc->devid, loc->function);
945 : :
946 : : /* if we're in a secondary process, just find our tailq entry */
947 [ # # ]: 0 : TAILQ_FOREACH(vfio_res, vfio_res_list, next) {
948 [ # # ]: 0 : if (rte_pci_addr_cmp(&vfio_res->pci_addr,
949 : 0 : &dev->addr))
950 : : continue;
951 : : break;
952 : : }
953 : : /* if we haven't found our tailq entry, something's wrong */
954 [ # # ]: 0 : if (vfio_res == NULL) {
955 : 0 : PCI_LOG(ERR, "%s cannot find TAILQ entry for PCI device!", pci_addr);
956 : 0 : return -1;
957 : : }
958 : :
959 : 0 : ret = dev_vfio_setup_device(rte_pci_get_sysfs_path(), pci_addr, &vfio_dev_fd);
960 [ # # ]: 0 : if (ret < 0) {
961 : : /* Device not managed by VFIO - skip */
962 [ # # ]: 0 : if (rte_errno == ENODEV)
963 : : ret = 1;
964 : 0 : return ret;
965 : : }
966 : :
967 : 0 : ret = dev_vfio_get_device_info(vfio_dev_fd, &device_info);
968 [ # # ]: 0 : if (ret)
969 : 0 : goto err_vfio_dev_fd;
970 : :
971 : 0 : ret = pci_vfio_fill_regions(dev, vfio_dev_fd, &device_info);
972 [ # # ]: 0 : if (ret)
973 : 0 : goto err_vfio_dev_fd;
974 : :
975 : : /* map BARs */
976 : 0 : maps = vfio_res->maps;
977 : :
978 [ # # ]: 0 : for (i = 0; i < vfio_res->nb_maps; i++) {
979 [ # # ]: 0 : if (maps[i].nr_areas > 0) {
980 : 0 : ret = pci_vfio_sparse_mmap_bar(vfio_dev_fd, vfio_res, i, MAP_FIXED);
981 [ # # ]: 0 : if (ret < 0) {
982 : 0 : PCI_LOG(ERR, "%s sparse mapping BAR%i failed: %s",
983 : : pci_addr, i, strerror(errno));
984 : 0 : goto err_map;
985 : : }
986 : : } else {
987 : 0 : ret = pci_vfio_mmap_bar(vfio_dev_fd, vfio_res, i, MAP_FIXED);
988 [ # # ]: 0 : if (ret < 0) {
989 : 0 : PCI_LOG(ERR, "%s mapping BAR%i failed: %s",
990 : : pci_addr, i, strerror(errno));
991 : 0 : goto err_map;
992 : : }
993 : : }
994 : :
995 : 0 : dev->mem_resource[i].addr = maps[i].addr;
996 : : }
997 : :
998 : : /* we need save vfio_dev_fd, so it can be used during release */
999 [ # # ]: 0 : if (rte_intr_dev_fd_set(dev->intr_handle, vfio_dev_fd))
1000 : 0 : goto err_map;
1001 : :
1002 : : return 0;
1003 : 0 : err_map:
1004 [ # # ]: 0 : for (j = 0; j < i; j++) {
1005 [ # # ]: 0 : if (maps[j].addr)
1006 : 0 : pci_unmap_resource(maps[j].addr, maps[j].size);
1007 : : }
1008 : 0 : err_vfio_dev_fd:
1009 : 0 : dev_vfio_release_device(rte_pci_get_sysfs_path(),
1010 : : pci_addr, vfio_dev_fd);
1011 : 0 : return -1;
1012 : : }
1013 : :
1014 : : /*
1015 : : * map the PCI resources of a PCI device in virtual memory (VFIO version).
1016 : : * primary and secondary processes follow almost exactly the same path
1017 : : */
1018 : : int
1019 : 0 : pci_vfio_map_resource(struct rte_pci_device *dev)
1020 : : {
1021 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1022 : 0 : return pci_vfio_map_resource_primary(dev);
1023 : : else
1024 : 0 : return pci_vfio_map_resource_secondary(dev);
1025 : : }
1026 : :
1027 : : static struct mapped_pci_resource *
1028 : 0 : find_and_unmap_vfio_resource(struct mapped_pci_res_list *vfio_res_list,
1029 : : struct rte_pci_device *dev,
1030 : : const char *pci_addr)
1031 : : {
1032 : : struct mapped_pci_resource *vfio_res = NULL;
1033 : : struct pci_map *maps;
1034 : : int i;
1035 : :
1036 : : /* Get vfio_res */
1037 [ # # ]: 0 : TAILQ_FOREACH(vfio_res, vfio_res_list, next) {
1038 [ # # ]: 0 : if (rte_pci_addr_cmp(&vfio_res->pci_addr, &dev->addr))
1039 : : continue;
1040 : : break;
1041 : : }
1042 : :
1043 [ # # ]: 0 : if (vfio_res == NULL)
1044 : : return vfio_res;
1045 : :
1046 : 0 : PCI_LOG(INFO, "Releasing PCI mapped resource for %s", pci_addr);
1047 : :
1048 : 0 : maps = vfio_res->maps;
1049 [ # # ]: 0 : for (i = 0; i < vfio_res->nb_maps; i++) {
1050 : :
1051 : : /*
1052 : : * We do not need to be aware of MSI-X table BAR mappings as
1053 : : * when mapping. Just using current maps array is enough
1054 : : */
1055 [ # # ]: 0 : if (maps[i].addr) {
1056 : 0 : PCI_LOG(INFO, "Calling pci_unmap_resource for %s at %p",
1057 : : pci_addr, maps[i].addr);
1058 : 0 : pci_unmap_resource(maps[i].addr, maps[i].size);
1059 : : }
1060 : :
1061 [ # # ]: 0 : if (maps[i].nr_areas > 0)
1062 : 0 : rte_free(maps[i].areas);
1063 : : }
1064 : :
1065 : : return vfio_res;
1066 : : }
1067 : :
1068 : : static int
1069 : 0 : pci_vfio_unmap_resource_primary(struct rte_pci_device *dev)
1070 : : {
1071 : 0 : char pci_addr[PATH_MAX] = {0};
1072 : : struct rte_pci_addr *loc = &dev->addr;
1073 : : struct mapped_pci_resource *vfio_res = NULL;
1074 : : struct mapped_pci_res_list *vfio_res_list;
1075 : : int ret, vfio_dev_fd;
1076 : :
1077 : : /* store PCI address string */
1078 : 0 : snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
1079 : 0 : loc->domain, loc->bus, loc->devid, loc->function);
1080 : :
1081 : 0 : ret = pci_vfio_disable_notifier(dev);
1082 [ # # ]: 0 : if (ret) {
1083 : 0 : PCI_LOG(ERR, "fail to disable req notifier.");
1084 : 0 : return -1;
1085 : : }
1086 : :
1087 : 0 : rte_intr_fd_close(dev->intr_handle);
1088 : :
1089 : 0 : vfio_dev_fd = rte_intr_dev_fd_get(dev->intr_handle);
1090 [ # # ]: 0 : if (vfio_dev_fd < 0)
1091 : : return -1;
1092 : :
1093 [ # # ]: 0 : if (rte_pci_set_bus_master(dev, false)) {
1094 : 0 : PCI_LOG(ERR, "%s cannot unset bus mastering for PCI device!", pci_addr);
1095 : 0 : return -1;
1096 : : }
1097 : :
1098 : 0 : ret = dev_vfio_release_device(rte_pci_get_sysfs_path(), pci_addr,
1099 : : vfio_dev_fd);
1100 [ # # ]: 0 : if (ret < 0) {
1101 : 0 : PCI_LOG(ERR, "Cannot release VFIO device");
1102 : 0 : return ret;
1103 : : }
1104 : 0 : rte_intr_dev_fd_set(dev->intr_handle, -1);
1105 : :
1106 : 0 : vfio_res_list =
1107 : 0 : RTE_TAILQ_CAST(dev_vfio_tailq.head, mapped_pci_res_list);
1108 : 0 : vfio_res = find_and_unmap_vfio_resource(vfio_res_list, dev, pci_addr);
1109 : :
1110 : : /* if we haven't found our tailq entry, something's wrong */
1111 [ # # ]: 0 : if (vfio_res == NULL) {
1112 : 0 : PCI_LOG(ERR, "%s cannot find TAILQ entry for PCI device!", pci_addr);
1113 : 0 : return -1;
1114 : : }
1115 : :
1116 [ # # ]: 0 : TAILQ_REMOVE(vfio_res_list, vfio_res, next);
1117 : 0 : rte_free(vfio_res);
1118 : 0 : return 0;
1119 : : }
1120 : :
1121 : : static int
1122 : 0 : pci_vfio_unmap_resource_secondary(struct rte_pci_device *dev)
1123 : : {
1124 : 0 : char pci_addr[PATH_MAX] = {0};
1125 : : struct rte_pci_addr *loc = &dev->addr;
1126 : : struct mapped_pci_resource *vfio_res = NULL;
1127 : : struct mapped_pci_res_list *vfio_res_list;
1128 : : int ret, vfio_dev_fd;
1129 : :
1130 : : /* store PCI address string */
1131 : 0 : snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
1132 : 0 : loc->domain, loc->bus, loc->devid, loc->function);
1133 : :
1134 : 0 : vfio_dev_fd = rte_intr_dev_fd_get(dev->intr_handle);
1135 [ # # ]: 0 : if (vfio_dev_fd < 0)
1136 : : return -1;
1137 : :
1138 : 0 : ret = dev_vfio_release_device(rte_pci_get_sysfs_path(), pci_addr,
1139 : : vfio_dev_fd);
1140 [ # # ]: 0 : if (ret < 0) {
1141 : 0 : PCI_LOG(ERR, "Cannot release VFIO device");
1142 : 0 : return ret;
1143 : : }
1144 : 0 : rte_intr_dev_fd_set(dev->intr_handle, -1);
1145 : :
1146 : 0 : vfio_res_list =
1147 : 0 : RTE_TAILQ_CAST(dev_vfio_tailq.head, mapped_pci_res_list);
1148 : 0 : vfio_res = find_and_unmap_vfio_resource(vfio_res_list, dev, pci_addr);
1149 : :
1150 : : /* if we haven't found our tailq entry, something's wrong */
1151 [ # # ]: 0 : if (vfio_res == NULL) {
1152 : 0 : PCI_LOG(ERR, "%s cannot find TAILQ entry for PCI device!", pci_addr);
1153 : 0 : return -1;
1154 : : }
1155 : :
1156 : : return 0;
1157 : : }
1158 : :
1159 : : int
1160 : 0 : pci_vfio_unmap_resource(struct rte_pci_device *dev)
1161 : : {
1162 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1163 : 0 : return pci_vfio_unmap_resource_primary(dev);
1164 : : else
1165 : 0 : return pci_vfio_unmap_resource_secondary(dev);
1166 : : }
1167 : :
1168 : : int
1169 : 0 : pci_vfio_ioport_map(struct rte_pci_device *dev, int bar,
1170 : : struct rte_pci_ioport *p)
1171 : : {
1172 : : uint64_t size, offset;
1173 : :
1174 [ # # ]: 0 : if (bar < VFIO_PCI_BAR0_REGION_INDEX ||
1175 : : bar > VFIO_PCI_BAR5_REGION_INDEX) {
1176 : 0 : PCI_LOG(ERR, "invalid bar (%d)!", bar);
1177 : 0 : return -1;
1178 : : }
1179 : :
1180 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1181 : 0 : struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
1182 : : char pci_addr[PATH_MAX];
1183 : : int vfio_dev_fd;
1184 : : struct rte_pci_addr *loc = &dev->addr;
1185 : :
1186 : : /* store PCI address string */
1187 : 0 : snprintf(pci_addr, sizeof(pci_addr), PCI_PRI_FMT,
1188 : 0 : loc->domain, loc->bus, loc->devid, loc->function);
1189 : :
1190 : 0 : vfio_dev_fd = rte_intr_dev_fd_get(dev->intr_handle);
1191 [ # # ]: 0 : if (vfio_dev_fd < 0) {
1192 [ # # ]: 0 : if (dev_vfio_setup_device(rte_pci_get_sysfs_path(), pci_addr,
1193 : : &vfio_dev_fd) != 0)
1194 : 0 : return -1;
1195 : : /* save vfio_dev_fd so it can be used during release */
1196 [ # # ]: 0 : if (rte_intr_dev_fd_set(dev->intr_handle, vfio_dev_fd) != 0)
1197 : : return -1;
1198 [ # # ]: 0 : if (dev_vfio_get_device_info(vfio_dev_fd, &device_info) != 0)
1199 : : return -1;
1200 : :
1201 [ # # ]: 0 : if (pci_vfio_fill_regions(dev, vfio_dev_fd, &device_info) != 0)
1202 : : return -1;
1203 : : }
1204 : : }
1205 : :
1206 : : if (pci_vfio_get_region(dev, bar, &size, &offset) != 0) {
1207 : 0 : PCI_LOG(ERR, "Cannot get offset of region %d.", bar);
1208 : 0 : return -1;
1209 : : }
1210 : :
1211 : 0 : p->dev = dev;
1212 : 0 : p->base = offset;
1213 : 0 : return 0;
1214 : : }
1215 : :
1216 : : #define PCI_VFIO_GET_REGION_IDX(x) (x >> 40)
1217 : : void
1218 : 0 : pci_vfio_ioport_read(struct rte_pci_ioport *p,
1219 : : void *data, size_t len, off_t offset)
1220 : : {
1221 : 0 : const struct rte_intr_handle *intr_handle = p->dev->intr_handle;
1222 : 0 : int vfio_dev_fd = rte_intr_dev_fd_get(intr_handle);
1223 : :
1224 [ # # ]: 0 : if (vfio_dev_fd < 0)
1225 : : return;
1226 : :
1227 [ # # ]: 0 : if (pread(vfio_dev_fd, data,
1228 [ # # ]: 0 : len, p->base + offset) <= 0)
1229 : 0 : PCI_LOG(ERR, "Can't read from PCI bar (%" PRIu64 ") : offset (%x)",
1230 : : PCI_VFIO_GET_REGION_IDX(p->base), (int)offset);
1231 : : }
1232 : :
1233 : : void
1234 : 0 : pci_vfio_ioport_write(struct rte_pci_ioport *p,
1235 : : const void *data, size_t len, off_t offset)
1236 : : {
1237 : 0 : const struct rte_intr_handle *intr_handle = p->dev->intr_handle;
1238 : 0 : int vfio_dev_fd = rte_intr_dev_fd_get(intr_handle);
1239 : :
1240 [ # # ]: 0 : if (vfio_dev_fd < 0)
1241 : : return;
1242 : :
1243 [ # # ]: 0 : if (pwrite(vfio_dev_fd, data,
1244 : 0 : len, p->base + offset) <= 0)
1245 : 0 : PCI_LOG(ERR, "Can't write to PCI bar (%" PRIu64 ") : offset (%x)",
1246 : : PCI_VFIO_GET_REGION_IDX(p->base), (int)offset);
1247 : : }
1248 : :
1249 : : int
1250 : 0 : pci_vfio_ioport_unmap(struct rte_pci_ioport *p)
1251 : : {
1252 : : RTE_SET_USED(p);
1253 : 0 : return -1;
1254 : : }
1255 : :
1256 : : int
1257 : 0 : pci_vfio_mmio_read(const struct rte_pci_device *dev, int bar,
1258 : : void *buf, size_t len, off_t offs)
1259 : : {
1260 : : uint64_t size, offset;
1261 : : int fd;
1262 : :
1263 : 0 : fd = rte_intr_dev_fd_get(dev->intr_handle);
1264 [ # # ]: 0 : if (fd < 0)
1265 : : return -1;
1266 : :
1267 [ # # ]: 0 : if (pci_vfio_get_region(dev, bar, &size, &offset) != 0)
1268 : : return -1;
1269 : :
1270 [ # # ]: 0 : if ((uint64_t)len + offs > size)
1271 : : return -1;
1272 : :
1273 [ # # ]: 0 : return pread(fd, buf, len, offset + offs);
1274 : : }
1275 : :
1276 : : int
1277 : 0 : pci_vfio_mmio_write(const struct rte_pci_device *dev, int bar,
1278 : : const void *buf, size_t len, off_t offs)
1279 : : {
1280 : : uint64_t size, offset;
1281 : : int fd;
1282 : :
1283 : 0 : fd = rte_intr_dev_fd_get(dev->intr_handle);
1284 [ # # ]: 0 : if (fd < 0)
1285 : : return -1;
1286 : :
1287 [ # # ]: 0 : if (pci_vfio_get_region(dev, bar, &size, &offset) != 0)
1288 : : return -1;
1289 : :
1290 [ # # ]: 0 : if ((uint64_t)len + offs > size)
1291 : : return -1;
1292 : :
1293 : 0 : return pwrite(fd, buf, len, offset + offs);
1294 : : }
1295 : :
1296 : : int
1297 : 0 : pci_vfio_is_enabled(void)
1298 : : {
1299 : : int status;
1300 : :
1301 [ # # ]: 0 : if (dev_vfio_enable() < 0)
1302 : : return 0;
1303 : :
1304 [ # # # # ]: 0 : status = dev_vfio_is_enabled() &&
1305 : 0 : dev_vfio_module_is_loaded(DEV_VFIO_MODULE_VFIO_PCI);
1306 : : return status;
1307 : : }
|