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