4b3749a9a12a6ce485c57a3c3b2beebbee9f193b
[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 <stdlib.h>
10 #include <stdio.h>
11 #include <sys/queue.h>
12 #include <sys/mman.h>
13
14 #include <rte_errno.h>
15 #include <rte_interrupts.h>
16 #include <rte_log.h>
17 #include <rte_bus.h>
18 #include <rte_pci.h>
19 #include <rte_bus_pci.h>
20 #include <rte_per_lcore.h>
21 #include <rte_memory.h>
22 #include <rte_eal.h>
23 #include <rte_string_fns.h>
24 #include <rte_common.h>
25 #include <rte_devargs.h>
26
27 #include "private.h"
28
29
30 #define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
31
32 const char *rte_pci_get_sysfs_path(void)
33 {
34         const char *path = NULL;
35
36         path = getenv("SYSFS_PCI_DEVICES");
37         if (path == NULL)
38                 return SYSFS_PCI_DEVICES;
39
40         return path;
41 }
42
43 static struct rte_devargs *pci_devargs_lookup(struct rte_pci_device *dev)
44 {
45         struct rte_devargs *devargs;
46         struct rte_pci_addr addr;
47
48         RTE_EAL_DEVARGS_FOREACH("pci", devargs) {
49                 devargs->bus->parse(devargs->name, &addr);
50                 if (!rte_pci_addr_cmp(&dev->addr, &addr))
51                         return devargs;
52         }
53         return NULL;
54 }
55
56 void
57 pci_name_set(struct rte_pci_device *dev)
58 {
59         struct rte_devargs *devargs;
60
61         /* Each device has its internal, canonical name set. */
62         rte_pci_device_name(&dev->addr,
63                         dev->name, sizeof(dev->name));
64         devargs = pci_devargs_lookup(dev);
65         dev->device.devargs = devargs;
66         /* In blacklist mode, if the device is not blacklisted, no
67          * rte_devargs exists for it.
68          */
69         if (devargs != NULL)
70                 /* If an rte_devargs exists, the generic rte_device uses the
71                  * given name as its name.
72                  */
73                 dev->device.name = dev->device.devargs->name;
74         else
75                 /* Otherwise, it uses the internal, canonical form. */
76                 dev->device.name = dev->name;
77 }
78
79 /*
80  * Match the PCI Driver and Device using the ID Table
81  */
82 int
83 rte_pci_match(const struct rte_pci_driver *pci_drv,
84               const struct rte_pci_device *pci_dev)
85 {
86         const struct rte_pci_id *id_table;
87
88         for (id_table = pci_drv->id_table; id_table->vendor_id != 0;
89              id_table++) {
90                 /* check if device's identifiers match the driver's ones */
91                 if (id_table->vendor_id != pci_dev->id.vendor_id &&
92                                 id_table->vendor_id != PCI_ANY_ID)
93                         continue;
94                 if (id_table->device_id != pci_dev->id.device_id &&
95                                 id_table->device_id != PCI_ANY_ID)
96                         continue;
97                 if (id_table->subsystem_vendor_id !=
98                     pci_dev->id.subsystem_vendor_id &&
99                     id_table->subsystem_vendor_id != PCI_ANY_ID)
100                         continue;
101                 if (id_table->subsystem_device_id !=
102                     pci_dev->id.subsystem_device_id &&
103                     id_table->subsystem_device_id != PCI_ANY_ID)
104                         continue;
105                 if (id_table->class_id != pci_dev->id.class_id &&
106                                 id_table->class_id != RTE_CLASS_ANY_ID)
107                         continue;
108
109                 return 1;
110         }
111
112         return 0;
113 }
114
115 /*
116  * If vendor/device ID match, call the probe() function of the
117  * driver.
118  */
119 static int
120 rte_pci_probe_one_driver(struct rte_pci_driver *dr,
121                          struct rte_pci_device *dev)
122 {
123         int ret;
124         struct rte_pci_addr *loc;
125
126         if ((dr == NULL) || (dev == NULL))
127                 return -EINVAL;
128
129         loc = &dev->addr;
130
131         /* The device is not blacklisted; Check if driver supports it */
132         if (!rte_pci_match(dr, dev))
133                 /* Match of device and driver failed */
134                 return 1;
135
136         RTE_LOG(INFO, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
137                         loc->domain, loc->bus, loc->devid, loc->function,
138                         dev->device.numa_node);
139
140         /* no initialization when blacklisted, return without error */
141         if (dev->device.devargs != NULL &&
142                 dev->device.devargs->policy ==
143                         RTE_DEV_BLACKLISTED) {
144                 RTE_LOG(INFO, EAL, "  Device is blacklisted, not"
145                         " initializing\n");
146                 return 1;
147         }
148
149         if (dev->device.numa_node < 0) {
150                 RTE_LOG(WARNING, EAL, "  Invalid NUMA socket, default to 0\n");
151                 dev->device.numa_node = 0;
152         }
153
154         RTE_LOG(INFO, EAL, "  probe driver: %x:%x %s\n", dev->id.vendor_id,
155                 dev->id.device_id, dr->driver.name);
156
157         /*
158          * reference driver structure
159          * This needs to be before rte_pci_map_device(), as it enables to use
160          * driver flags for adjusting configuration.
161          */
162         dev->driver = dr;
163         dev->device.driver = &dr->driver;
164
165         if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
166                 /* map resources for devices that use igb_uio */
167                 ret = rte_pci_map_device(dev);
168                 if (ret != 0) {
169                         dev->driver = NULL;
170                         dev->device.driver = NULL;
171                         return ret;
172                 }
173         }
174
175         /* call the driver probe() function */
176         ret = dr->probe(dr, dev);
177         if (ret) {
178                 dev->driver = NULL;
179                 dev->device.driver = NULL;
180                 if ((dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) &&
181                         /* Don't unmap if device is unsupported and
182                          * driver needs mapped resources.
183                          */
184                         !(ret > 0 &&
185                                 (dr->drv_flags & RTE_PCI_DRV_KEEP_MAPPED_RES)))
186                         rte_pci_unmap_device(dev);
187         }
188
189         return ret;
190 }
191
192 /*
193  * If vendor/device ID match, call the remove() function of the
194  * driver.
195  */
196 static int
197 rte_pci_detach_dev(struct rte_pci_device *dev)
198 {
199         struct rte_pci_addr *loc;
200         struct rte_pci_driver *dr;
201         int ret = 0;
202
203         if (dev == NULL)
204                 return -EINVAL;
205
206         dr = dev->driver;
207         loc = &dev->addr;
208
209         RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
210                         loc->domain, loc->bus, loc->devid,
211                         loc->function, dev->device.numa_node);
212
213         RTE_LOG(DEBUG, EAL, "  remove driver: %x:%x %s\n", dev->id.vendor_id,
214                         dev->id.device_id, dr->driver.name);
215
216         if (dr->remove) {
217                 ret = dr->remove(dev);
218                 if (ret < 0)
219                         return ret;
220         }
221
222         /* clear driver structure */
223         dev->driver = NULL;
224
225         if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
226                 /* unmap resources for devices that use igb_uio */
227                 rte_pci_unmap_device(dev);
228
229         return 0;
230 }
231
232 /*
233  * If vendor/device ID match, call the probe() function of all
234  * registered driver for the given device. Return -1 if initialization
235  * failed, return 1 if no driver is found for this device.
236  */
237 static int
238 pci_probe_all_drivers(struct rte_pci_device *dev)
239 {
240         struct rte_pci_driver *dr = NULL;
241         int rc = 0;
242
243         if (dev == NULL)
244                 return -1;
245
246         /* Check if a driver is already loaded */
247         if (dev->driver != NULL)
248                 return 0;
249
250         FOREACH_DRIVER_ON_PCIBUS(dr) {
251                 rc = rte_pci_probe_one_driver(dr, dev);
252                 if (rc < 0)
253                         /* negative value is an error */
254                         return -1;
255                 if (rc > 0)
256                         /* positive value means driver doesn't support it */
257                         continue;
258                 return 0;
259         }
260         return 1;
261 }
262
263 /*
264  * Scan the content of the PCI bus, and call the probe() function for
265  * all registered drivers that have a matching entry in its id_table
266  * for discovered devices.
267  */
268 int
269 rte_pci_probe(void)
270 {
271         struct rte_pci_device *dev = NULL;
272         size_t probed = 0, failed = 0;
273         struct rte_devargs *devargs;
274         int probe_all = 0;
275         int ret = 0;
276
277         if (rte_pci_bus.bus.conf.scan_mode != RTE_BUS_SCAN_WHITELIST)
278                 probe_all = 1;
279
280         FOREACH_DEVICE_ON_PCIBUS(dev) {
281                 probed++;
282
283                 devargs = dev->device.devargs;
284                 /* probe all or only whitelisted devices */
285                 if (probe_all)
286                         ret = pci_probe_all_drivers(dev);
287                 else if (devargs != NULL &&
288                         devargs->policy == RTE_DEV_WHITELISTED)
289                         ret = pci_probe_all_drivers(dev);
290                 if (ret < 0) {
291                         RTE_LOG(ERR, EAL, "Requested device " PCI_PRI_FMT
292                                  " cannot be used\n", dev->addr.domain, dev->addr.bus,
293                                  dev->addr.devid, dev->addr.function);
294                         rte_errno = errno;
295                         failed++;
296                         ret = 0;
297                 }
298         }
299
300         return (probed && probed == failed) ? -1 : 0;
301 }
302
303 /* dump one device */
304 static int
305 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
306 {
307         int i;
308
309         fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
310                dev->addr.devid, dev->addr.function);
311         fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
312                dev->id.device_id);
313
314         for (i = 0; i != sizeof(dev->mem_resource) /
315                 sizeof(dev->mem_resource[0]); i++) {
316                 fprintf(f, "   %16.16"PRIx64" %16.16"PRIx64"\n",
317                         dev->mem_resource[i].phys_addr,
318                         dev->mem_resource[i].len);
319         }
320         return 0;
321 }
322
323 /* dump devices on the bus */
324 void
325 rte_pci_dump(FILE *f)
326 {
327         struct rte_pci_device *dev = NULL;
328
329         FOREACH_DEVICE_ON_PCIBUS(dev) {
330                 pci_dump_one_device(f, dev);
331         }
332 }
333
334 static int
335 pci_parse(const char *name, void *addr)
336 {
337         struct rte_pci_addr *out = addr;
338         struct rte_pci_addr pci_addr;
339         bool parse;
340
341         parse = (rte_pci_addr_parse(name, &pci_addr) == 0);
342         if (parse && addr != NULL)
343                 *out = pci_addr;
344         return parse == false;
345 }
346
347 /* register a driver */
348 void
349 rte_pci_register(struct rte_pci_driver *driver)
350 {
351         TAILQ_INSERT_TAIL(&rte_pci_bus.driver_list, driver, next);
352         driver->bus = &rte_pci_bus;
353 }
354
355 /* unregister a driver */
356 void
357 rte_pci_unregister(struct rte_pci_driver *driver)
358 {
359         TAILQ_REMOVE(&rte_pci_bus.driver_list, driver, next);
360         driver->bus = NULL;
361 }
362
363 /* Add a device to PCI bus */
364 void
365 rte_pci_add_device(struct rte_pci_device *pci_dev)
366 {
367         TAILQ_INSERT_TAIL(&rte_pci_bus.device_list, pci_dev, next);
368 }
369
370 /* Insert a device into a predefined position in PCI bus */
371 void
372 rte_pci_insert_device(struct rte_pci_device *exist_pci_dev,
373                       struct rte_pci_device *new_pci_dev)
374 {
375         TAILQ_INSERT_BEFORE(exist_pci_dev, new_pci_dev, next);
376 }
377
378 /* Remove a device from PCI bus */
379 static void
380 rte_pci_remove_device(struct rte_pci_device *pci_dev)
381 {
382         TAILQ_REMOVE(&rte_pci_bus.device_list, pci_dev, next);
383 }
384
385 static struct rte_device *
386 pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
387                 const void *data)
388 {
389         const struct rte_pci_device *pstart;
390         struct rte_pci_device *pdev;
391
392         if (start != NULL) {
393                 pstart = RTE_DEV_TO_PCI_CONST(start);
394                 pdev = TAILQ_NEXT(pstart, next);
395         } else {
396                 pdev = TAILQ_FIRST(&rte_pci_bus.device_list);
397         }
398         while (pdev != NULL) {
399                 if (cmp(&pdev->device, data) == 0)
400                         return &pdev->device;
401                 pdev = TAILQ_NEXT(pdev, next);
402         }
403         return NULL;
404 }
405
406 /*
407  * find the device which encounter the failure, by iterate over all device on
408  * PCI bus to check if the memory failure address is located in the range
409  * of the BARs of the device.
410  */
411 static struct rte_pci_device *
412 pci_find_device_by_addr(const void *failure_addr)
413 {
414         struct rte_pci_device *pdev = NULL;
415         uint64_t check_point, start, end, len;
416         int i;
417
418         check_point = (uint64_t)(uintptr_t)failure_addr;
419
420         FOREACH_DEVICE_ON_PCIBUS(pdev) {
421                 for (i = 0; i != RTE_DIM(pdev->mem_resource); i++) {
422                         start = (uint64_t)(uintptr_t)pdev->mem_resource[i].addr;
423                         len = pdev->mem_resource[i].len;
424                         end = start + len;
425                         if (check_point >= start && check_point < end) {
426                                 RTE_LOG(DEBUG, EAL, "Failure address %16.16"
427                                         PRIx64" belongs to device %s!\n",
428                                         check_point, pdev->device.name);
429                                 return pdev;
430                         }
431                 }
432         }
433         return NULL;
434 }
435
436 static int
437 pci_hot_unplug_handler(struct rte_device *dev)
438 {
439         struct rte_pci_device *pdev = NULL;
440         int ret = 0;
441
442         pdev = RTE_DEV_TO_PCI(dev);
443         if (!pdev)
444                 return -1;
445
446         switch (pdev->kdrv) {
447 #ifdef HAVE_VFIO_DEV_REQ_INTERFACE
448         case RTE_KDRV_VFIO:
449                 /*
450                  * vfio kernel module guaranty the pci device would not be
451                  * deleted until the user space release the resource, so no
452                  * need to remap BARs resource here, just directly notify
453                  * the req event to the user space to handle it.
454                  */
455                 rte_dev_event_callback_process(dev->name,
456                                                RTE_DEV_EVENT_REMOVE);
457                 break;
458 #endif
459         case RTE_KDRV_IGB_UIO:
460         case RTE_KDRV_UIO_GENERIC:
461         case RTE_KDRV_NIC_UIO:
462                 /* BARs resource is invalid, remap it to be safe. */
463                 ret = pci_uio_remap_resource(pdev);
464                 break;
465         default:
466                 RTE_LOG(DEBUG, EAL,
467                         "Not managed by a supported kernel driver, skipped\n");
468                 ret = -1;
469                 break;
470         }
471
472         return ret;
473 }
474
475 static int
476 pci_sigbus_handler(const void *failure_addr)
477 {
478         struct rte_pci_device *pdev = NULL;
479         int ret = 0;
480
481         pdev = pci_find_device_by_addr(failure_addr);
482         if (!pdev) {
483                 /* It is a generic sigbus error, no bus would handle it. */
484                 ret = 1;
485         } else {
486                 /* The sigbus error is caused of hot-unplug. */
487                 ret = pci_hot_unplug_handler(&pdev->device);
488                 if (ret) {
489                         RTE_LOG(ERR, EAL,
490                                 "Failed to handle hot-unplug for device %s",
491                                 pdev->name);
492                         ret = -1;
493                 }
494         }
495         return ret;
496 }
497
498 static int
499 pci_plug(struct rte_device *dev)
500 {
501         return pci_probe_all_drivers(RTE_DEV_TO_PCI(dev));
502 }
503
504 static int
505 pci_unplug(struct rte_device *dev)
506 {
507         struct rte_pci_device *pdev;
508         int ret;
509
510         pdev = RTE_DEV_TO_PCI(dev);
511         ret = rte_pci_detach_dev(pdev);
512         if (ret == 0) {
513                 rte_pci_remove_device(pdev);
514                 free(pdev);
515         }
516         return ret;
517 }
518
519 struct rte_pci_bus rte_pci_bus = {
520         .bus = {
521                 .scan = rte_pci_scan,
522                 .probe = rte_pci_probe,
523                 .find_device = pci_find_device,
524                 .plug = pci_plug,
525                 .unplug = pci_unplug,
526                 .parse = pci_parse,
527                 .get_iommu_class = rte_pci_get_iommu_class,
528                 .dev_iterate = rte_pci_dev_iterate,
529                 .hot_unplug_handler = pci_hot_unplug_handler,
530                 .sigbus_handler = pci_sigbus_handler,
531         },
532         .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
533         .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
534 };
535
536 RTE_REGISTER_BUS(pci, rte_pci_bus.bus);