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