4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5 * Copyright 2013-2014 6WIND S.A.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * * Neither the name of Intel Corporation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <sys/queue.h>
43 #include <rte_errno.h>
44 #include <rte_interrupts.h>
48 #include <rte_per_lcore.h>
49 #include <rte_memory.h>
50 #include <rte_memzone.h>
52 #include <rte_string_fns.h>
53 #include <rte_common.h>
54 #include <rte_devargs.h>
56 #include "eal_private.h"
58 extern struct rte_pci_bus rte_pci_bus;
60 #define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
62 const char *pci_get_sysfs_path(void)
64 const char *path = NULL;
66 path = getenv("SYSFS_PCI_DEVICES");
68 return SYSFS_PCI_DEVICES;
73 static struct rte_devargs *pci_devargs_lookup(struct rte_pci_device *dev)
75 struct rte_devargs *devargs;
76 struct rte_pci_addr addr;
79 pbus = rte_bus_find_by_name("pci");
80 TAILQ_FOREACH(devargs, &devargs_list, next) {
81 if (devargs->bus != pbus)
83 devargs->bus->parse(devargs->name, &addr);
84 if (!rte_eal_compare_pci_addr(&dev->addr, &addr))
90 /* Macro used by pci addr parsing functions. **/
91 #define GET_PCIADDR_FIELD(in, fd, lim, dlm) \
96 val = strtoul((in), &end, 16); \
97 if (errno != 0 || end[0] != (dlm) || val > (lim)) \
99 (fd) = (typeof (fd))val; \
105 eal_parse_pci_BDF(const char *input, struct rte_pci_addr *dev_addr)
107 dev_addr->domain = 0;
108 GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
109 GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
110 GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
115 eal_parse_pci_DomBDF(const char *input, struct rte_pci_addr *dev_addr)
117 GET_PCIADDR_FIELD(input, dev_addr->domain, UINT16_MAX, ':');
118 GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
119 GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
120 GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
124 #undef GET_PCIADDR_FIELD
127 rte_pci_device_name(const struct rte_pci_addr *addr,
128 char *output, size_t size)
130 RTE_VERIFY(size >= PCI_PRI_STR_SIZE);
131 RTE_VERIFY(snprintf(output, size, PCI_PRI_FMT,
132 addr->domain, addr->bus,
133 addr->devid, addr->function) >= 0);
137 rte_eal_compare_pci_addr(const struct rte_pci_addr *addr,
138 const struct rte_pci_addr *addr2)
140 uint64_t dev_addr, dev_addr2;
142 if ((addr == NULL) || (addr2 == NULL))
145 dev_addr = ((uint64_t)addr->domain << 24) |
146 (addr->bus << 16) | (addr->devid << 8) | addr->function;
147 dev_addr2 = ((uint64_t)addr2->domain << 24) |
148 (addr2->bus << 16) | (addr2->devid << 8) | addr2->function;
150 if (dev_addr > dev_addr2)
152 else if (dev_addr < dev_addr2)
159 pci_name_set(struct rte_pci_device *dev)
161 struct rte_devargs *devargs;
163 /* Each device has its internal, canonical name set. */
164 rte_pci_device_name(&dev->addr,
165 dev->name, sizeof(dev->name));
166 devargs = pci_devargs_lookup(dev);
167 dev->device.devargs = devargs;
168 /* In blacklist mode, if the device is not blacklisted, no
169 * rte_devargs exists for it.
172 /* If an rte_devargs exists, the generic rte_device uses the
173 * given name as its namea
175 dev->device.name = dev->device.devargs->name;
177 /* Otherwise, it uses the internal, canonical form. */
178 dev->device.name = dev->name;
181 /* map a particular resource from a file */
183 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
184 int additional_flags)
188 /* Map the PCI memory resource of device */
189 mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
190 MAP_SHARED | additional_flags, fd, offset);
191 if (mapaddr == MAP_FAILED) {
192 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n",
193 __func__, fd, requested_addr,
194 (unsigned long)size, (unsigned long)offset,
195 strerror(errno), mapaddr);
197 RTE_LOG(DEBUG, EAL, " PCI memory mapped at %p\n", mapaddr);
202 /* unmap a particular resource */
204 pci_unmap_resource(void *requested_addr, size_t size)
206 if (requested_addr == NULL)
209 /* Unmap the PCI memory resource of device */
210 if (munmap(requested_addr, size)) {
211 RTE_LOG(ERR, EAL, "%s(): cannot munmap(%p, 0x%lx): %s\n",
212 __func__, requested_addr, (unsigned long)size,
215 RTE_LOG(DEBUG, EAL, " PCI memory unmapped at %p\n",
220 * Match the PCI Driver and Device using the ID Table
223 rte_pci_match(const struct rte_pci_driver *pci_drv,
224 const struct rte_pci_device *pci_dev)
226 const struct rte_pci_id *id_table;
228 for (id_table = pci_drv->id_table; id_table->vendor_id != 0;
230 /* check if device's identifiers match the driver's ones */
231 if (id_table->vendor_id != pci_dev->id.vendor_id &&
232 id_table->vendor_id != PCI_ANY_ID)
234 if (id_table->device_id != pci_dev->id.device_id &&
235 id_table->device_id != PCI_ANY_ID)
237 if (id_table->subsystem_vendor_id !=
238 pci_dev->id.subsystem_vendor_id &&
239 id_table->subsystem_vendor_id != PCI_ANY_ID)
241 if (id_table->subsystem_device_id !=
242 pci_dev->id.subsystem_device_id &&
243 id_table->subsystem_device_id != PCI_ANY_ID)
245 if (id_table->class_id != pci_dev->id.class_id &&
246 id_table->class_id != RTE_CLASS_ANY_ID)
256 * If vendor/device ID match, call the probe() function of the
260 rte_pci_probe_one_driver(struct rte_pci_driver *dr,
261 struct rte_pci_device *dev)
264 struct rte_pci_addr *loc;
266 if ((dr == NULL) || (dev == NULL))
271 /* The device is not blacklisted; Check if driver supports it */
272 if (!rte_pci_match(dr, dev))
273 /* Match of device and driver failed */
276 RTE_LOG(INFO, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
277 loc->domain, loc->bus, loc->devid, loc->function,
278 dev->device.numa_node);
280 /* no initialization when blacklisted, return without error */
281 if (dev->device.devargs != NULL &&
282 dev->device.devargs->policy ==
283 RTE_DEV_BLACKLISTED) {
284 RTE_LOG(INFO, EAL, " Device is blacklisted, not"
289 if (dev->device.numa_node < 0) {
290 RTE_LOG(WARNING, EAL, " Invalid NUMA socket, default to 0\n");
291 dev->device.numa_node = 0;
294 RTE_LOG(INFO, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
295 dev->id.device_id, dr->driver.name);
297 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
298 /* map resources for devices that use igb_uio */
299 ret = rte_pci_map_device(dev);
304 /* reference driver structure */
306 dev->device.driver = &dr->driver;
308 /* call the driver probe() function */
309 ret = dr->probe(dr, dev);
312 dev->device.driver = NULL;
313 if ((dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) &&
314 /* Don't unmap if device is unsupported and
315 * driver needs mapped resources.
318 (dr->drv_flags & RTE_PCI_DRV_KEEP_MAPPED_RES)))
319 rte_pci_unmap_device(dev);
326 * If vendor/device ID match, call the remove() function of the
330 rte_pci_detach_dev(struct rte_pci_device *dev)
332 struct rte_pci_addr *loc;
333 struct rte_pci_driver *dr;
342 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
343 loc->domain, loc->bus, loc->devid,
344 loc->function, dev->device.numa_node);
346 RTE_LOG(DEBUG, EAL, " remove driver: %x:%x %s\n", dev->id.vendor_id,
347 dev->id.device_id, dr->driver.name);
350 ret = dr->remove(dev);
355 /* clear driver structure */
358 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
359 /* unmap resources for devices that use igb_uio */
360 rte_pci_unmap_device(dev);
366 * If vendor/device ID match, call the probe() function of all
367 * registered driver for the given device. Return -1 if initialization
368 * failed, return 1 if no driver is found for this device.
371 pci_probe_all_drivers(struct rte_pci_device *dev)
373 struct rte_pci_driver *dr = NULL;
379 /* Check if a driver is already loaded */
380 if (dev->driver != NULL)
383 FOREACH_DRIVER_ON_PCIBUS(dr) {
384 rc = rte_pci_probe_one_driver(dr, dev);
386 /* negative value is an error */
389 /* positive value means driver doesn't support it */
397 * Find the pci device specified by pci address, then invoke probe function of
398 * the driver of the device.
401 rte_pci_probe_one(const struct rte_pci_addr *addr)
403 struct rte_pci_device *dev = NULL;
410 /* update current pci device in global list, kernel bindings might have
411 * changed since last time we looked at it.
413 if (pci_update_device(addr) < 0)
416 FOREACH_DEVICE_ON_PCIBUS(dev) {
417 if (rte_eal_compare_pci_addr(&dev->addr, addr))
420 ret = pci_probe_all_drivers(dev);
428 RTE_LOG(WARNING, EAL,
429 "Requested device " PCI_PRI_FMT " cannot be used\n",
430 addr->domain, addr->bus, addr->devid, addr->function);
435 * Detach device specified by its pci address.
438 rte_pci_detach(const struct rte_pci_addr *addr)
440 struct rte_pci_device *dev = NULL;
446 FOREACH_DEVICE_ON_PCIBUS(dev) {
447 if (rte_eal_compare_pci_addr(&dev->addr, addr))
450 ret = rte_pci_detach_dev(dev);
452 /* negative value is an error */
455 /* positive value means driver doesn't support it */
458 rte_pci_remove_device(dev);
465 RTE_LOG(WARNING, EAL, "Requested device " PCI_PRI_FMT
466 " cannot be used\n", dev->addr.domain, dev->addr.bus,
467 dev->addr.devid, dev->addr.function);
472 * Scan the content of the PCI bus, and call the probe() function for
473 * all registered drivers that have a matching entry in its id_table
474 * for discovered devices.
479 struct rte_pci_device *dev = NULL;
480 size_t probed = 0, failed = 0;
481 struct rte_devargs *devargs;
485 if (rte_pci_bus.bus.conf.scan_mode != RTE_BUS_SCAN_WHITELIST)
488 FOREACH_DEVICE_ON_PCIBUS(dev) {
491 devargs = dev->device.devargs;
492 /* probe all or only whitelisted devices */
494 ret = pci_probe_all_drivers(dev);
495 else if (devargs != NULL &&
496 devargs->policy == RTE_DEV_WHITELISTED)
497 ret = pci_probe_all_drivers(dev);
499 RTE_LOG(ERR, EAL, "Requested device " PCI_PRI_FMT
500 " cannot be used\n", dev->addr.domain, dev->addr.bus,
501 dev->addr.devid, dev->addr.function);
508 return (probed && probed == failed) ? -1 : 0;
511 /* dump one device */
513 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
517 fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
518 dev->addr.devid, dev->addr.function);
519 fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
522 for (i = 0; i != sizeof(dev->mem_resource) /
523 sizeof(dev->mem_resource[0]); i++) {
524 fprintf(f, " %16.16"PRIx64" %16.16"PRIx64"\n",
525 dev->mem_resource[i].phys_addr,
526 dev->mem_resource[i].len);
531 /* dump devices on the bus */
533 rte_pci_dump(FILE *f)
535 struct rte_pci_device *dev = NULL;
537 FOREACH_DEVICE_ON_PCIBUS(dev) {
538 pci_dump_one_device(f, dev);
543 pci_parse(const char *name, void *addr)
545 struct rte_pci_addr *out = addr;
546 struct rte_pci_addr pci_addr;
549 parse = (eal_parse_pci_BDF(name, &pci_addr) == 0 ||
550 eal_parse_pci_DomBDF(name, &pci_addr) == 0);
551 if (parse && addr != NULL)
553 return parse == false;
556 /* register a driver */
558 rte_pci_register(struct rte_pci_driver *driver)
560 TAILQ_INSERT_TAIL(&rte_pci_bus.driver_list, driver, next);
561 driver->bus = &rte_pci_bus;
564 /* unregister a driver */
566 rte_pci_unregister(struct rte_pci_driver *driver)
568 TAILQ_REMOVE(&rte_pci_bus.driver_list, driver, next);
572 /* Add a device to PCI bus */
574 rte_pci_add_device(struct rte_pci_device *pci_dev)
576 TAILQ_INSERT_TAIL(&rte_pci_bus.device_list, pci_dev, next);
579 /* Insert a device into a predefined position in PCI bus */
581 rte_pci_insert_device(struct rte_pci_device *exist_pci_dev,
582 struct rte_pci_device *new_pci_dev)
584 TAILQ_INSERT_BEFORE(exist_pci_dev, new_pci_dev, next);
587 /* Remove a device from PCI bus */
589 rte_pci_remove_device(struct rte_pci_device *pci_dev)
591 TAILQ_REMOVE(&rte_pci_bus.device_list, pci_dev, next);
594 static struct rte_device *
595 pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
598 struct rte_pci_device *dev;
600 FOREACH_DEVICE_ON_PCIBUS(dev) {
601 if (start && &dev->device == start) {
602 start = NULL; /* starting point found */
605 if (cmp(&dev->device, data) == 0)
613 pci_plug(struct rte_device *dev)
615 return pci_probe_all_drivers(RTE_DEV_TO_PCI(dev));
619 pci_unplug(struct rte_device *dev)
621 struct rte_pci_device *pdev;
624 pdev = RTE_DEV_TO_PCI(dev);
625 ret = rte_pci_detach_dev(pdev);
627 rte_pci_remove_device(pdev);
633 struct rte_pci_bus rte_pci_bus = {
635 .scan = rte_pci_scan,
636 .probe = rte_pci_probe,
637 .find_device = pci_find_device,
639 .unplug = pci_unplug,
641 .get_iommu_class = rte_pci_get_iommu_class,
643 .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
644 .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
647 RTE_REGISTER_BUS(pci, rte_pci_bus.bus);