6bed0bc9dceeee425e3bbaa8b1a29c5c9628bcb3
[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 extern struct rte_pci_bus rte_pci_bus;
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 namea
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         if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
159                 /* map resources for devices that use igb_uio */
160                 ret = rte_pci_map_device(dev);
161                 if (ret != 0)
162                         return ret;
163         }
164
165         /* reference driver structure */
166         dev->driver = dr;
167         dev->device.driver = &dr->driver;
168
169         /* call the driver probe() function */
170         ret = dr->probe(dr, dev);
171         if (ret) {
172                 dev->driver = NULL;
173                 dev->device.driver = NULL;
174                 if ((dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) &&
175                         /* Don't unmap if device is unsupported and
176                          * driver needs mapped resources.
177                          */
178                         !(ret > 0 &&
179                                 (dr->drv_flags & RTE_PCI_DRV_KEEP_MAPPED_RES)))
180                         rte_pci_unmap_device(dev);
181         }
182
183         return ret;
184 }
185
186 /*
187  * If vendor/device ID match, call the remove() function of the
188  * driver.
189  */
190 static int
191 rte_pci_detach_dev(struct rte_pci_device *dev)
192 {
193         struct rte_pci_addr *loc;
194         struct rte_pci_driver *dr;
195         int ret = 0;
196
197         if (dev == NULL)
198                 return -EINVAL;
199
200         dr = dev->driver;
201         loc = &dev->addr;
202
203         RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
204                         loc->domain, loc->bus, loc->devid,
205                         loc->function, dev->device.numa_node);
206
207         RTE_LOG(DEBUG, EAL, "  remove driver: %x:%x %s\n", dev->id.vendor_id,
208                         dev->id.device_id, dr->driver.name);
209
210         if (dr->remove) {
211                 ret = dr->remove(dev);
212                 if (ret < 0)
213                         return ret;
214         }
215
216         /* clear driver structure */
217         dev->driver = NULL;
218
219         if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
220                 /* unmap resources for devices that use igb_uio */
221                 rte_pci_unmap_device(dev);
222
223         return 0;
224 }
225
226 /*
227  * If vendor/device ID match, call the probe() function of all
228  * registered driver for the given device. Return -1 if initialization
229  * failed, return 1 if no driver is found for this device.
230  */
231 static int
232 pci_probe_all_drivers(struct rte_pci_device *dev)
233 {
234         struct rte_pci_driver *dr = NULL;
235         int rc = 0;
236
237         if (dev == NULL)
238                 return -1;
239
240         /* Check if a driver is already loaded */
241         if (dev->driver != NULL)
242                 return 0;
243
244         FOREACH_DRIVER_ON_PCIBUS(dr) {
245                 rc = rte_pci_probe_one_driver(dr, dev);
246                 if (rc < 0)
247                         /* negative value is an error */
248                         return -1;
249                 if (rc > 0)
250                         /* positive value means driver doesn't support it */
251                         continue;
252                 return 0;
253         }
254         return 1;
255 }
256
257 /*
258  * Find the pci device specified by pci address, then invoke probe function of
259  * the driver of the device.
260  */
261 int
262 rte_pci_probe_one(const struct rte_pci_addr *addr)
263 {
264         struct rte_pci_device *dev = NULL;
265
266         int ret = 0;
267
268         if (addr == NULL)
269                 return -1;
270
271         /* update current pci device in global list, kernel bindings might have
272          * changed since last time we looked at it.
273          */
274         if (pci_update_device(addr) < 0)
275                 goto err_return;
276
277         FOREACH_DEVICE_ON_PCIBUS(dev) {
278                 if (rte_pci_addr_cmp(&dev->addr, addr))
279                         continue;
280
281                 ret = pci_probe_all_drivers(dev);
282                 if (ret)
283                         goto err_return;
284                 return 0;
285         }
286         return -1;
287
288 err_return:
289         RTE_LOG(WARNING, EAL,
290                 "Requested device " PCI_PRI_FMT " cannot be used\n",
291                 addr->domain, addr->bus, addr->devid, addr->function);
292         return -1;
293 }
294
295 /*
296  * Detach device specified by its pci address.
297  */
298 int
299 rte_pci_detach(const struct rte_pci_addr *addr)
300 {
301         struct rte_pci_device *dev = NULL;
302         int ret = 0;
303
304         if (addr == NULL)
305                 return -1;
306
307         FOREACH_DEVICE_ON_PCIBUS(dev) {
308                 if (rte_pci_addr_cmp(&dev->addr, addr))
309                         continue;
310
311                 ret = rte_pci_detach_dev(dev);
312                 if (ret < 0)
313                         /* negative value is an error */
314                         goto err_return;
315                 if (ret > 0)
316                         /* positive value means driver doesn't support it */
317                         continue;
318
319                 rte_pci_remove_device(dev);
320                 free(dev);
321                 return 0;
322         }
323         return -1;
324
325 err_return:
326         RTE_LOG(WARNING, EAL, "Requested device " PCI_PRI_FMT
327                         " cannot be used\n", dev->addr.domain, dev->addr.bus,
328                         dev->addr.devid, dev->addr.function);
329         return -1;
330 }
331
332 /*
333  * Scan the content of the PCI bus, and call the probe() function for
334  * all registered drivers that have a matching entry in its id_table
335  * for discovered devices.
336  */
337 int
338 rte_pci_probe(void)
339 {
340         struct rte_pci_device *dev = NULL;
341         size_t probed = 0, failed = 0;
342         struct rte_devargs *devargs;
343         int probe_all = 0;
344         int ret = 0;
345
346         if (rte_pci_bus.bus.conf.scan_mode != RTE_BUS_SCAN_WHITELIST)
347                 probe_all = 1;
348
349         FOREACH_DEVICE_ON_PCIBUS(dev) {
350                 probed++;
351
352                 devargs = dev->device.devargs;
353                 /* probe all or only whitelisted devices */
354                 if (probe_all)
355                         ret = pci_probe_all_drivers(dev);
356                 else if (devargs != NULL &&
357                         devargs->policy == RTE_DEV_WHITELISTED)
358                         ret = pci_probe_all_drivers(dev);
359                 if (ret < 0) {
360                         RTE_LOG(ERR, EAL, "Requested device " PCI_PRI_FMT
361                                  " cannot be used\n", dev->addr.domain, dev->addr.bus,
362                                  dev->addr.devid, dev->addr.function);
363                         rte_errno = errno;
364                         failed++;
365                         ret = 0;
366                 }
367         }
368
369         return (probed && probed == failed) ? -1 : 0;
370 }
371
372 /* dump one device */
373 static int
374 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
375 {
376         int i;
377
378         fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
379                dev->addr.devid, dev->addr.function);
380         fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
381                dev->id.device_id);
382
383         for (i = 0; i != sizeof(dev->mem_resource) /
384                 sizeof(dev->mem_resource[0]); i++) {
385                 fprintf(f, "   %16.16"PRIx64" %16.16"PRIx64"\n",
386                         dev->mem_resource[i].phys_addr,
387                         dev->mem_resource[i].len);
388         }
389         return 0;
390 }
391
392 /* dump devices on the bus */
393 void
394 rte_pci_dump(FILE *f)
395 {
396         struct rte_pci_device *dev = NULL;
397
398         FOREACH_DEVICE_ON_PCIBUS(dev) {
399                 pci_dump_one_device(f, dev);
400         }
401 }
402
403 static int
404 pci_parse(const char *name, void *addr)
405 {
406         struct rte_pci_addr *out = addr;
407         struct rte_pci_addr pci_addr;
408         bool parse;
409
410         parse = (rte_pci_addr_parse(name, &pci_addr) == 0);
411         if (parse && addr != NULL)
412                 *out = pci_addr;
413         return parse == false;
414 }
415
416 /* register a driver */
417 void
418 rte_pci_register(struct rte_pci_driver *driver)
419 {
420         TAILQ_INSERT_TAIL(&rte_pci_bus.driver_list, driver, next);
421         driver->bus = &rte_pci_bus;
422 }
423
424 /* unregister a driver */
425 void
426 rte_pci_unregister(struct rte_pci_driver *driver)
427 {
428         TAILQ_REMOVE(&rte_pci_bus.driver_list, driver, next);
429         driver->bus = NULL;
430 }
431
432 /* Add a device to PCI bus */
433 void
434 rte_pci_add_device(struct rte_pci_device *pci_dev)
435 {
436         TAILQ_INSERT_TAIL(&rte_pci_bus.device_list, pci_dev, next);
437 }
438
439 /* Insert a device into a predefined position in PCI bus */
440 void
441 rte_pci_insert_device(struct rte_pci_device *exist_pci_dev,
442                       struct rte_pci_device *new_pci_dev)
443 {
444         TAILQ_INSERT_BEFORE(exist_pci_dev, new_pci_dev, next);
445 }
446
447 /* Remove a device from PCI bus */
448 void
449 rte_pci_remove_device(struct rte_pci_device *pci_dev)
450 {
451         TAILQ_REMOVE(&rte_pci_bus.device_list, pci_dev, next);
452 }
453
454 static struct rte_device *
455 pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
456                 const void *data)
457 {
458         struct rte_pci_device *dev;
459
460         FOREACH_DEVICE_ON_PCIBUS(dev) {
461                 if (start && &dev->device == start) {
462                         start = NULL; /* starting point found */
463                         continue;
464                 }
465                 if (cmp(&dev->device, data) == 0)
466                         return &dev->device;
467         }
468
469         return NULL;
470 }
471
472 static int
473 pci_plug(struct rte_device *dev)
474 {
475         return pci_probe_all_drivers(RTE_DEV_TO_PCI(dev));
476 }
477
478 static int
479 pci_unplug(struct rte_device *dev)
480 {
481         struct rte_pci_device *pdev;
482         int ret;
483
484         pdev = RTE_DEV_TO_PCI(dev);
485         ret = rte_pci_detach_dev(pdev);
486         if (ret == 0) {
487                 rte_pci_remove_device(pdev);
488                 free(pdev);
489         }
490         return ret;
491 }
492
493 struct rte_pci_bus rte_pci_bus = {
494         .bus = {
495                 .scan = rte_pci_scan,
496                 .probe = rte_pci_probe,
497                 .find_device = pci_find_device,
498                 .plug = pci_plug,
499                 .unplug = pci_unplug,
500                 .parse = pci_parse,
501                 .get_iommu_class = rte_pci_get_iommu_class,
502         },
503         .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
504         .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
505 };
506
507 RTE_REGISTER_BUS(pci, rte_pci_bus.bus);