pci: build on Windows
[dpdk.git] / drivers / bus / pci / pci_common.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation.
3  * Copyright 2013-2014 6WIND S.A.
4  */
5
6 #include <string.h>
7 #include <inttypes.h>
8 #include <stdint.h>
9 #include <stdbool.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <sys/queue.h>
13 #include <sys/mman.h>
14
15 #include <rte_errno.h>
16 #include <rte_interrupts.h>
17 #include <rte_log.h>
18 #include <rte_bus.h>
19 #include <rte_pci.h>
20 #include <rte_bus_pci.h>
21 #include <rte_per_lcore.h>
22 #include <rte_memory.h>
23 #include <rte_eal.h>
24 #include <rte_string_fns.h>
25 #include <rte_common.h>
26 #include <rte_devargs.h>
27 #include <rte_vfio.h>
28
29 #include "private.h"
30
31
32 #define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
33
34 const char *rte_pci_get_sysfs_path(void)
35 {
36         const char *path = NULL;
37
38 #ifdef RTE_EXEC_ENV_LINUX
39         path = getenv("SYSFS_PCI_DEVICES");
40         if (path == NULL)
41                 return SYSFS_PCI_DEVICES;
42 #endif
43
44         return path;
45 }
46
47 static struct rte_devargs *
48 pci_devargs_lookup(const struct rte_pci_addr *pci_addr)
49 {
50         struct rte_devargs *devargs;
51         struct rte_pci_addr addr;
52
53         RTE_EAL_DEVARGS_FOREACH("pci", devargs) {
54                 devargs->bus->parse(devargs->name, &addr);
55                 if (!rte_pci_addr_cmp(pci_addr, &addr))
56                         return devargs;
57         }
58         return NULL;
59 }
60
61 void
62 pci_name_set(struct rte_pci_device *dev)
63 {
64         struct rte_devargs *devargs;
65
66         /* Each device has its internal, canonical name set. */
67         rte_pci_device_name(&dev->addr,
68                         dev->name, sizeof(dev->name));
69         devargs = pci_devargs_lookup(&dev->addr);
70         dev->device.devargs = devargs;
71         /* In blacklist mode, if the device is not blacklisted, no
72          * rte_devargs exists for it.
73          */
74         if (devargs != NULL)
75                 /* If an rte_devargs exists, the generic rte_device uses the
76                  * given name as its name.
77                  */
78                 dev->device.name = dev->device.devargs->name;
79         else
80                 /* Otherwise, it uses the internal, canonical form. */
81                 dev->device.name = dev->name;
82 }
83
84 /*
85  * Match the PCI Driver and Device using the ID Table
86  */
87 int
88 rte_pci_match(const struct rte_pci_driver *pci_drv,
89               const struct rte_pci_device *pci_dev)
90 {
91         const struct rte_pci_id *id_table;
92
93         for (id_table = pci_drv->id_table; id_table->vendor_id != 0;
94              id_table++) {
95                 /* check if device's identifiers match the driver's ones */
96                 if (id_table->vendor_id != pci_dev->id.vendor_id &&
97                                 id_table->vendor_id != PCI_ANY_ID)
98                         continue;
99                 if (id_table->device_id != pci_dev->id.device_id &&
100                                 id_table->device_id != PCI_ANY_ID)
101                         continue;
102                 if (id_table->subsystem_vendor_id !=
103                     pci_dev->id.subsystem_vendor_id &&
104                     id_table->subsystem_vendor_id != PCI_ANY_ID)
105                         continue;
106                 if (id_table->subsystem_device_id !=
107                     pci_dev->id.subsystem_device_id &&
108                     id_table->subsystem_device_id != PCI_ANY_ID)
109                         continue;
110                 if (id_table->class_id != pci_dev->id.class_id &&
111                                 id_table->class_id != RTE_CLASS_ANY_ID)
112                         continue;
113
114                 return 1;
115         }
116
117         return 0;
118 }
119
120 /*
121  * If vendor/device ID match, call the probe() function of the
122  * driver.
123  */
124 static int
125 rte_pci_probe_one_driver(struct rte_pci_driver *dr,
126                          struct rte_pci_device *dev)
127 {
128         int ret;
129         bool already_probed;
130         struct rte_pci_addr *loc;
131
132         if ((dr == NULL) || (dev == NULL))
133                 return -EINVAL;
134
135         loc = &dev->addr;
136
137         /* The device is not blacklisted; Check if driver supports it */
138         if (!rte_pci_match(dr, dev))
139                 /* Match of device and driver failed */
140                 return 1;
141
142         RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
143                         loc->domain, loc->bus, loc->devid, loc->function,
144                         dev->device.numa_node);
145
146         /* no initialization when blacklisted, return without error */
147         if (dev->device.devargs != NULL &&
148                 dev->device.devargs->policy ==
149                         RTE_DEV_BLACKLISTED) {
150                 RTE_LOG(INFO, EAL, "  Device is blacklisted, not"
151                         " initializing\n");
152                 return 1;
153         }
154
155         if (dev->device.numa_node < 0) {
156                 RTE_LOG(WARNING, EAL, "  Invalid NUMA socket, default to 0\n");
157                 dev->device.numa_node = 0;
158         }
159
160         already_probed = rte_dev_is_probed(&dev->device);
161         if (already_probed && !(dr->drv_flags & RTE_PCI_DRV_PROBE_AGAIN)) {
162                 RTE_LOG(DEBUG, EAL, "Device %s is already probed\n",
163                                 dev->device.name);
164                 return -EEXIST;
165         }
166
167         RTE_LOG(DEBUG, EAL, "  probe driver: %x:%x %s\n", dev->id.vendor_id,
168                 dev->id.device_id, dr->driver.name);
169
170         /*
171          * reference driver structure
172          * This needs to be before rte_pci_map_device(), as it enables to use
173          * driver flags for adjusting configuration.
174          */
175         if (!already_probed) {
176                 enum rte_iova_mode dev_iova_mode;
177                 enum rte_iova_mode iova_mode;
178
179                 dev_iova_mode = pci_device_iova_mode(dr, dev);
180                 iova_mode = rte_eal_iova_mode();
181                 if (dev_iova_mode != RTE_IOVA_DC &&
182                     dev_iova_mode != iova_mode) {
183                         RTE_LOG(ERR, EAL, "  Expecting '%s' IOVA mode but current mode is '%s', not initializing\n",
184                                 dev_iova_mode == RTE_IOVA_PA ? "PA" : "VA",
185                                 iova_mode == RTE_IOVA_PA ? "PA" : "VA");
186                         return -EINVAL;
187                 }
188
189                 dev->driver = dr;
190         }
191
192         if (!already_probed && (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)) {
193                 /* map resources for devices that use igb_uio */
194                 ret = rte_pci_map_device(dev);
195                 if (ret != 0) {
196                         dev->driver = NULL;
197                         return ret;
198                 }
199         }
200
201         RTE_LOG(INFO, EAL, "Probe PCI driver: %s (%x:%x) device: "PCI_PRI_FMT" (socket %i)\n",
202                         dr->driver.name, dev->id.vendor_id, dev->id.device_id,
203                         loc->domain, loc->bus, loc->devid, loc->function,
204                         dev->device.numa_node);
205         /* call the driver probe() function */
206         ret = dr->probe(dr, dev);
207         if (already_probed)
208                 return ret; /* no rollback if already succeeded earlier */
209         if (ret) {
210                 dev->driver = NULL;
211                 if ((dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) &&
212                         /* Don't unmap if device is unsupported and
213                          * driver needs mapped resources.
214                          */
215                         !(ret > 0 &&
216                                 (dr->drv_flags & RTE_PCI_DRV_KEEP_MAPPED_RES)))
217                         rte_pci_unmap_device(dev);
218         } else {
219                 dev->device.driver = &dr->driver;
220         }
221
222         return ret;
223 }
224
225 /*
226  * If vendor/device ID match, call the remove() function of the
227  * driver.
228  */
229 static int
230 rte_pci_detach_dev(struct rte_pci_device *dev)
231 {
232         struct rte_pci_addr *loc;
233         struct rte_pci_driver *dr;
234         int ret = 0;
235
236         if (dev == NULL)
237                 return -EINVAL;
238
239         dr = dev->driver;
240         loc = &dev->addr;
241
242         RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
243                         loc->domain, loc->bus, loc->devid,
244                         loc->function, dev->device.numa_node);
245
246         RTE_LOG(DEBUG, EAL, "  remove driver: %x:%x %s\n", dev->id.vendor_id,
247                         dev->id.device_id, dr->driver.name);
248
249         if (dr->remove) {
250                 ret = dr->remove(dev);
251                 if (ret < 0)
252                         return ret;
253         }
254
255         /* clear driver structure */
256         dev->driver = NULL;
257         dev->device.driver = NULL;
258
259         if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
260                 /* unmap resources for devices that use igb_uio */
261                 rte_pci_unmap_device(dev);
262
263         return 0;
264 }
265
266 /*
267  * If vendor/device ID match, call the probe() function of all
268  * registered driver for the given device. Return < 0 if initialization
269  * failed, return 1 if no driver is found for this device.
270  */
271 static int
272 pci_probe_all_drivers(struct rte_pci_device *dev)
273 {
274         struct rte_pci_driver *dr = NULL;
275         int rc = 0;
276
277         if (dev == NULL)
278                 return -EINVAL;
279
280         FOREACH_DRIVER_ON_PCIBUS(dr) {
281                 rc = rte_pci_probe_one_driver(dr, dev);
282                 if (rc < 0)
283                         /* negative value is an error */
284                         return rc;
285                 if (rc > 0)
286                         /* positive value means driver doesn't support it */
287                         continue;
288                 return 0;
289         }
290         return 1;
291 }
292
293 /*
294  * Scan the content of the PCI bus, and call the probe() function for
295  * all registered drivers that have a matching entry in its id_table
296  * for discovered devices.
297  */
298 static int
299 pci_probe(void)
300 {
301         struct rte_pci_device *dev = NULL;
302         size_t probed = 0, failed = 0;
303         int ret = 0;
304
305         FOREACH_DEVICE_ON_PCIBUS(dev) {
306                 probed++;
307
308                 ret = pci_probe_all_drivers(dev);
309                 if (ret < 0) {
310                         if (ret != -EEXIST) {
311                                 RTE_LOG(ERR, EAL, "Requested device "
312                                         PCI_PRI_FMT " cannot be used\n",
313                                         dev->addr.domain, dev->addr.bus,
314                                         dev->addr.devid, dev->addr.function);
315                                 rte_errno = errno;
316                                 failed++;
317                         }
318                         ret = 0;
319                 }
320         }
321
322         return (probed && probed == failed) ? -1 : 0;
323 }
324
325 /* dump one device */
326 static int
327 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
328 {
329         int i;
330
331         fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
332                dev->addr.devid, dev->addr.function);
333         fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
334                dev->id.device_id);
335
336         for (i = 0; i != sizeof(dev->mem_resource) /
337                 sizeof(dev->mem_resource[0]); i++) {
338                 fprintf(f, "   %16.16"PRIx64" %16.16"PRIx64"\n",
339                         dev->mem_resource[i].phys_addr,
340                         dev->mem_resource[i].len);
341         }
342         return 0;
343 }
344
345 /* dump devices on the bus */
346 void
347 rte_pci_dump(FILE *f)
348 {
349         struct rte_pci_device *dev = NULL;
350
351         FOREACH_DEVICE_ON_PCIBUS(dev) {
352                 pci_dump_one_device(f, dev);
353         }
354 }
355
356 static int
357 pci_parse(const char *name, void *addr)
358 {
359         struct rte_pci_addr *out = addr;
360         struct rte_pci_addr pci_addr;
361         bool parse;
362
363         parse = (rte_pci_addr_parse(name, &pci_addr) == 0);
364         if (parse && addr != NULL)
365                 *out = pci_addr;
366         return parse == false;
367 }
368
369 /* register a driver */
370 void
371 rte_pci_register(struct rte_pci_driver *driver)
372 {
373         TAILQ_INSERT_TAIL(&rte_pci_bus.driver_list, driver, next);
374         driver->bus = &rte_pci_bus;
375 }
376
377 /* unregister a driver */
378 void
379 rte_pci_unregister(struct rte_pci_driver *driver)
380 {
381         TAILQ_REMOVE(&rte_pci_bus.driver_list, driver, next);
382         driver->bus = NULL;
383 }
384
385 /* Add a device to PCI bus */
386 void
387 rte_pci_add_device(struct rte_pci_device *pci_dev)
388 {
389         TAILQ_INSERT_TAIL(&rte_pci_bus.device_list, pci_dev, next);
390 }
391
392 /* Insert a device into a predefined position in PCI bus */
393 void
394 rte_pci_insert_device(struct rte_pci_device *exist_pci_dev,
395                       struct rte_pci_device *new_pci_dev)
396 {
397         TAILQ_INSERT_BEFORE(exist_pci_dev, new_pci_dev, next);
398 }
399
400 /* Remove a device from PCI bus */
401 static void
402 rte_pci_remove_device(struct rte_pci_device *pci_dev)
403 {
404         TAILQ_REMOVE(&rte_pci_bus.device_list, pci_dev, next);
405 }
406
407 static struct rte_device *
408 pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
409                 const void *data)
410 {
411         const struct rte_pci_device *pstart;
412         struct rte_pci_device *pdev;
413
414         if (start != NULL) {
415                 pstart = RTE_DEV_TO_PCI_CONST(start);
416                 pdev = TAILQ_NEXT(pstart, next);
417         } else {
418                 pdev = TAILQ_FIRST(&rte_pci_bus.device_list);
419         }
420         while (pdev != NULL) {
421                 if (cmp(&pdev->device, data) == 0)
422                         return &pdev->device;
423                 pdev = TAILQ_NEXT(pdev, next);
424         }
425         return NULL;
426 }
427
428 /*
429  * find the device which encounter the failure, by iterate over all device on
430  * PCI bus to check if the memory failure address is located in the range
431  * of the BARs of the device.
432  */
433 static struct rte_pci_device *
434 pci_find_device_by_addr(const void *failure_addr)
435 {
436         struct rte_pci_device *pdev = NULL;
437         uint64_t check_point, start, end, len;
438         int i;
439
440         check_point = (uint64_t)(uintptr_t)failure_addr;
441
442         FOREACH_DEVICE_ON_PCIBUS(pdev) {
443                 for (i = 0; i != RTE_DIM(pdev->mem_resource); i++) {
444                         start = (uint64_t)(uintptr_t)pdev->mem_resource[i].addr;
445                         len = pdev->mem_resource[i].len;
446                         end = start + len;
447                         if (check_point >= start && check_point < end) {
448                                 RTE_LOG(DEBUG, EAL, "Failure address %16.16"
449                                         PRIx64" belongs to device %s!\n",
450                                         check_point, pdev->device.name);
451                                 return pdev;
452                         }
453                 }
454         }
455         return NULL;
456 }
457
458 static int
459 pci_hot_unplug_handler(struct rte_device *dev)
460 {
461         struct rte_pci_device *pdev = NULL;
462         int ret = 0;
463
464         pdev = RTE_DEV_TO_PCI(dev);
465         if (!pdev)
466                 return -1;
467
468         switch (pdev->kdrv) {
469 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
470         case RTE_KDRV_VFIO:
471                 /*
472                  * vfio kernel module guaranty the pci device would not be
473                  * deleted until the user space release the resource, so no
474                  * need to remap BARs resource here, just directly notify
475                  * the req event to the user space to handle it.
476                  */
477                 rte_dev_event_callback_process(dev->name,
478                                                RTE_DEV_EVENT_REMOVE);
479                 break;
480 #endif
481         case RTE_KDRV_IGB_UIO:
482         case RTE_KDRV_UIO_GENERIC:
483         case RTE_KDRV_NIC_UIO:
484                 /* BARs resource is invalid, remap it to be safe. */
485                 ret = pci_uio_remap_resource(pdev);
486                 break;
487         default:
488                 RTE_LOG(DEBUG, EAL,
489                         "Not managed by a supported kernel driver, skipped\n");
490                 ret = -1;
491                 break;
492         }
493
494         return ret;
495 }
496
497 static int
498 pci_sigbus_handler(const void *failure_addr)
499 {
500         struct rte_pci_device *pdev = NULL;
501         int ret = 0;
502
503         pdev = pci_find_device_by_addr(failure_addr);
504         if (!pdev) {
505                 /* It is a generic sigbus error, no bus would handle it. */
506                 ret = 1;
507         } else {
508                 /* The sigbus error is caused of hot-unplug. */
509                 ret = pci_hot_unplug_handler(&pdev->device);
510                 if (ret) {
511                         RTE_LOG(ERR, EAL,
512                                 "Failed to handle hot-unplug for device %s",
513                                 pdev->name);
514                         ret = -1;
515                 }
516         }
517         return ret;
518 }
519
520 static int
521 pci_plug(struct rte_device *dev)
522 {
523         return pci_probe_all_drivers(RTE_DEV_TO_PCI(dev));
524 }
525
526 static int
527 pci_unplug(struct rte_device *dev)
528 {
529         struct rte_pci_device *pdev;
530         int ret;
531
532         pdev = RTE_DEV_TO_PCI(dev);
533         ret = rte_pci_detach_dev(pdev);
534         if (ret == 0) {
535                 rte_pci_remove_device(pdev);
536                 rte_devargs_remove(dev->devargs);
537                 free(pdev);
538         }
539         return ret;
540 }
541
542 static int
543 pci_dma_map(struct rte_device *dev, void *addr, uint64_t iova, size_t len)
544 {
545         struct rte_pci_device *pdev = RTE_DEV_TO_PCI(dev);
546
547         if (!pdev || !pdev->driver) {
548                 rte_errno = EINVAL;
549                 return -1;
550         }
551         if (pdev->driver->dma_map)
552                 return pdev->driver->dma_map(pdev, addr, iova, len);
553         /**
554          *  In case driver don't provides any specific mapping
555          *  try fallback to VFIO.
556          */
557         if (pdev->kdrv == RTE_KDRV_VFIO)
558                 return rte_vfio_container_dma_map
559                                 (RTE_VFIO_DEFAULT_CONTAINER_FD, (uintptr_t)addr,
560                                  iova, len);
561         rte_errno = ENOTSUP;
562         return -1;
563 }
564
565 static int
566 pci_dma_unmap(struct rte_device *dev, void *addr, uint64_t iova, size_t len)
567 {
568         struct rte_pci_device *pdev = RTE_DEV_TO_PCI(dev);
569
570         if (!pdev || !pdev->driver) {
571                 rte_errno = EINVAL;
572                 return -1;
573         }
574         if (pdev->driver->dma_unmap)
575                 return pdev->driver->dma_unmap(pdev, addr, iova, len);
576         /**
577          *  In case driver don't provides any specific mapping
578          *  try fallback to VFIO.
579          */
580         if (pdev->kdrv == RTE_KDRV_VFIO)
581                 return rte_vfio_container_dma_unmap
582                                 (RTE_VFIO_DEFAULT_CONTAINER_FD, (uintptr_t)addr,
583                                  iova, len);
584         rte_errno = ENOTSUP;
585         return -1;
586 }
587
588 bool
589 rte_pci_ignore_device(const struct rte_pci_addr *pci_addr)
590 {
591         struct rte_devargs *devargs = pci_devargs_lookup(pci_addr);
592
593         switch (rte_pci_bus.bus.conf.scan_mode) {
594         case RTE_BUS_SCAN_WHITELIST:
595                 if (devargs && devargs->policy == RTE_DEV_WHITELISTED)
596                         return false;
597                 break;
598         case RTE_BUS_SCAN_UNDEFINED:
599         case RTE_BUS_SCAN_BLACKLIST:
600                 if (devargs == NULL ||
601                     devargs->policy != RTE_DEV_BLACKLISTED)
602                         return false;
603                 break;
604         }
605         return true;
606 }
607
608 enum rte_iova_mode
609 rte_pci_get_iommu_class(void)
610 {
611         enum rte_iova_mode iova_mode = RTE_IOVA_DC;
612         const struct rte_pci_device *dev;
613         const struct rte_pci_driver *drv;
614         bool devices_want_va = false;
615         bool devices_want_pa = false;
616         int iommu_no_va = -1;
617
618         FOREACH_DEVICE_ON_PCIBUS(dev) {
619                 /*
620                  * We can check this only once, because the IOMMU hardware is
621                  * the same for all of them.
622                  */
623                 if (iommu_no_va == -1)
624                         iommu_no_va = pci_device_iommu_support_va(dev)
625                                         ? 0 : 1;
626
627                 if (dev->kdrv == RTE_KDRV_UNKNOWN ||
628                     dev->kdrv == RTE_KDRV_NONE)
629                         continue;
630                 FOREACH_DRIVER_ON_PCIBUS(drv) {
631                         enum rte_iova_mode dev_iova_mode;
632
633                         if (!rte_pci_match(drv, dev))
634                                 continue;
635
636                         dev_iova_mode = pci_device_iova_mode(drv, dev);
637                         RTE_LOG(DEBUG, EAL, "PCI driver %s for device "
638                                 PCI_PRI_FMT " wants IOVA as '%s'\n",
639                                 drv->driver.name,
640                                 dev->addr.domain, dev->addr.bus,
641                                 dev->addr.devid, dev->addr.function,
642                                 dev_iova_mode == RTE_IOVA_DC ? "DC" :
643                                 (dev_iova_mode == RTE_IOVA_PA ? "PA" : "VA"));
644                         if (dev_iova_mode == RTE_IOVA_PA)
645                                 devices_want_pa = true;
646                         else if (dev_iova_mode == RTE_IOVA_VA)
647                                 devices_want_va = true;
648                 }
649         }
650         if (iommu_no_va == 1) {
651                 iova_mode = RTE_IOVA_PA;
652                 if (devices_want_va) {
653                         RTE_LOG(WARNING, EAL, "Some devices want 'VA' but IOMMU does not support 'VA'.\n");
654                         RTE_LOG(WARNING, EAL, "The devices that want 'VA' won't initialize.\n");
655                 }
656         } else if (devices_want_va && !devices_want_pa) {
657                 iova_mode = RTE_IOVA_VA;
658         } else if (devices_want_pa && !devices_want_va) {
659                 iova_mode = RTE_IOVA_PA;
660         } else {
661                 iova_mode = RTE_IOVA_DC;
662                 if (devices_want_va) {
663                         RTE_LOG(WARNING, EAL, "Some devices want 'VA' but forcing 'DC' because other devices want 'PA'.\n");
664                         RTE_LOG(WARNING, EAL, "Depending on the final decision by the EAL, not all devices may be able to initialize.\n");
665                 }
666         }
667         return iova_mode;
668 }
669
670 struct rte_pci_bus rte_pci_bus = {
671         .bus = {
672                 .scan = rte_pci_scan,
673                 .probe = pci_probe,
674                 .find_device = pci_find_device,
675                 .plug = pci_plug,
676                 .unplug = pci_unplug,
677                 .parse = pci_parse,
678                 .dma_map = pci_dma_map,
679                 .dma_unmap = pci_dma_unmap,
680                 .get_iommu_class = rte_pci_get_iommu_class,
681                 .dev_iterate = rte_pci_dev_iterate,
682                 .hot_unplug_handler = pci_hot_unplug_handler,
683                 .sigbus_handler = pci_sigbus_handler,
684         },
685         .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
686         .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
687 };
688
689 RTE_REGISTER_BUS(pci, rte_pci_bus.bus);