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_pci_addr_cmp(&dev->addr, &addr))
90 static inline const char *
91 get_u8_pciaddr_field(const char *in, void *_u8, char dlm)
98 val = strtoul(in, &end, 16);
99 if (errno != 0 || end[0] != dlm || val > UINT8_MAX) {
100 errno = errno ? errno : EINVAL;
109 rte_pci_bdf_parse(const char *input, struct rte_pci_addr *dev_addr)
111 const char *in = input;
113 dev_addr->domain = 0;
114 in = get_u8_pciaddr_field(in, &dev_addr->bus, ':');
117 in = get_u8_pciaddr_field(in, &dev_addr->devid, '.');
120 in = get_u8_pciaddr_field(in, &dev_addr->function, '\0');
127 eal_parse_pci_BDF(const char *input, struct rte_pci_addr *dev_addr)
129 return rte_pci_bdf_parse(input, dev_addr);
133 rte_pci_dbdf_parse(const char *input, struct rte_pci_addr *dev_addr)
135 const char *in = input;
140 val = strtoul(in, &end, 16);
141 if (errno != 0 || end[0] != ':' || val > UINT16_MAX)
143 dev_addr->domain = (uint16_t)val;
145 in = get_u8_pciaddr_field(in, &dev_addr->bus, ':');
148 in = get_u8_pciaddr_field(in, &dev_addr->devid, '.');
151 in = get_u8_pciaddr_field(in, &dev_addr->function, '\0');
158 eal_parse_pci_DomBDF(const char *input, struct rte_pci_addr *dev_addr)
160 return rte_pci_dbdf_parse(input, dev_addr);
164 rte_pci_device_name(const struct rte_pci_addr *addr,
165 char *output, size_t size)
167 RTE_VERIFY(size >= PCI_PRI_STR_SIZE);
168 RTE_VERIFY(snprintf(output, size, PCI_PRI_FMT,
169 addr->domain, addr->bus,
170 addr->devid, addr->function) >= 0);
174 rte_pci_addr_cmp(const struct rte_pci_addr *addr,
175 const struct rte_pci_addr *addr2)
177 uint64_t dev_addr, dev_addr2;
179 if ((addr == NULL) || (addr2 == NULL))
182 dev_addr = ((uint64_t)addr->domain << 24) |
183 (addr->bus << 16) | (addr->devid << 8) | addr->function;
184 dev_addr2 = ((uint64_t)addr2->domain << 24) |
185 (addr2->bus << 16) | (addr2->devid << 8) | addr2->function;
187 if (dev_addr > dev_addr2)
189 else if (dev_addr < dev_addr2)
196 rte_eal_compare_pci_addr(const struct rte_pci_addr *addr,
197 const struct rte_pci_addr *addr2)
199 return rte_pci_addr_cmp(addr, addr2);
203 rte_pci_addr_parse(const char *str, struct rte_pci_addr *addr)
205 if (rte_pci_bdf_parse(str, addr) == 0 ||
206 rte_pci_dbdf_parse(str, addr) == 0)
212 pci_name_set(struct rte_pci_device *dev)
214 struct rte_devargs *devargs;
216 /* Each device has its internal, canonical name set. */
217 rte_pci_device_name(&dev->addr,
218 dev->name, sizeof(dev->name));
219 devargs = pci_devargs_lookup(dev);
220 dev->device.devargs = devargs;
221 /* In blacklist mode, if the device is not blacklisted, no
222 * rte_devargs exists for it.
225 /* If an rte_devargs exists, the generic rte_device uses the
226 * given name as its namea
228 dev->device.name = dev->device.devargs->name;
230 /* Otherwise, it uses the internal, canonical form. */
231 dev->device.name = dev->name;
234 /* map a particular resource from a file */
236 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
237 int additional_flags)
241 /* Map the PCI memory resource of device */
242 mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
243 MAP_SHARED | additional_flags, fd, offset);
244 if (mapaddr == MAP_FAILED) {
245 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n",
246 __func__, fd, requested_addr,
247 (unsigned long)size, (unsigned long)offset,
248 strerror(errno), mapaddr);
250 RTE_LOG(DEBUG, EAL, " PCI memory mapped at %p\n", mapaddr);
255 /* unmap a particular resource */
257 pci_unmap_resource(void *requested_addr, size_t size)
259 if (requested_addr == NULL)
262 /* Unmap the PCI memory resource of device */
263 if (munmap(requested_addr, size)) {
264 RTE_LOG(ERR, EAL, "%s(): cannot munmap(%p, 0x%lx): %s\n",
265 __func__, requested_addr, (unsigned long)size,
268 RTE_LOG(DEBUG, EAL, " PCI memory unmapped at %p\n",
273 * Match the PCI Driver and Device using the ID Table
276 rte_pci_match(const struct rte_pci_driver *pci_drv,
277 const struct rte_pci_device *pci_dev)
279 const struct rte_pci_id *id_table;
281 for (id_table = pci_drv->id_table; id_table->vendor_id != 0;
283 /* check if device's identifiers match the driver's ones */
284 if (id_table->vendor_id != pci_dev->id.vendor_id &&
285 id_table->vendor_id != PCI_ANY_ID)
287 if (id_table->device_id != pci_dev->id.device_id &&
288 id_table->device_id != PCI_ANY_ID)
290 if (id_table->subsystem_vendor_id !=
291 pci_dev->id.subsystem_vendor_id &&
292 id_table->subsystem_vendor_id != PCI_ANY_ID)
294 if (id_table->subsystem_device_id !=
295 pci_dev->id.subsystem_device_id &&
296 id_table->subsystem_device_id != PCI_ANY_ID)
298 if (id_table->class_id != pci_dev->id.class_id &&
299 id_table->class_id != RTE_CLASS_ANY_ID)
309 * If vendor/device ID match, call the probe() function of the
313 rte_pci_probe_one_driver(struct rte_pci_driver *dr,
314 struct rte_pci_device *dev)
317 struct rte_pci_addr *loc;
319 if ((dr == NULL) || (dev == NULL))
324 /* The device is not blacklisted; Check if driver supports it */
325 if (!rte_pci_match(dr, dev))
326 /* Match of device and driver failed */
329 RTE_LOG(INFO, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
330 loc->domain, loc->bus, loc->devid, loc->function,
331 dev->device.numa_node);
333 /* no initialization when blacklisted, return without error */
334 if (dev->device.devargs != NULL &&
335 dev->device.devargs->policy ==
336 RTE_DEV_BLACKLISTED) {
337 RTE_LOG(INFO, EAL, " Device is blacklisted, not"
342 if (dev->device.numa_node < 0) {
343 RTE_LOG(WARNING, EAL, " Invalid NUMA socket, default to 0\n");
344 dev->device.numa_node = 0;
347 RTE_LOG(INFO, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
348 dev->id.device_id, dr->driver.name);
350 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
351 /* map resources for devices that use igb_uio */
352 ret = rte_pci_map_device(dev);
357 /* reference driver structure */
359 dev->device.driver = &dr->driver;
361 /* call the driver probe() function */
362 ret = dr->probe(dr, dev);
365 dev->device.driver = NULL;
366 if ((dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) &&
367 /* Don't unmap if device is unsupported and
368 * driver needs mapped resources.
371 (dr->drv_flags & RTE_PCI_DRV_KEEP_MAPPED_RES)))
372 rte_pci_unmap_device(dev);
379 * If vendor/device ID match, call the remove() function of the
383 rte_pci_detach_dev(struct rte_pci_device *dev)
385 struct rte_pci_addr *loc;
386 struct rte_pci_driver *dr;
395 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
396 loc->domain, loc->bus, loc->devid,
397 loc->function, dev->device.numa_node);
399 RTE_LOG(DEBUG, EAL, " remove driver: %x:%x %s\n", dev->id.vendor_id,
400 dev->id.device_id, dr->driver.name);
403 ret = dr->remove(dev);
408 /* clear driver structure */
411 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
412 /* unmap resources for devices that use igb_uio */
413 rte_pci_unmap_device(dev);
419 * If vendor/device ID match, call the probe() function of all
420 * registered driver for the given device. Return -1 if initialization
421 * failed, return 1 if no driver is found for this device.
424 pci_probe_all_drivers(struct rte_pci_device *dev)
426 struct rte_pci_driver *dr = NULL;
432 /* Check if a driver is already loaded */
433 if (dev->driver != NULL)
436 FOREACH_DRIVER_ON_PCIBUS(dr) {
437 rc = rte_pci_probe_one_driver(dr, dev);
439 /* negative value is an error */
442 /* positive value means driver doesn't support it */
450 * Find the pci device specified by pci address, then invoke probe function of
451 * the driver of the device.
454 rte_pci_probe_one(const struct rte_pci_addr *addr)
456 struct rte_pci_device *dev = NULL;
463 /* update current pci device in global list, kernel bindings might have
464 * changed since last time we looked at it.
466 if (pci_update_device(addr) < 0)
469 FOREACH_DEVICE_ON_PCIBUS(dev) {
470 if (rte_pci_addr_cmp(&dev->addr, addr))
473 ret = pci_probe_all_drivers(dev);
481 RTE_LOG(WARNING, EAL,
482 "Requested device " PCI_PRI_FMT " cannot be used\n",
483 addr->domain, addr->bus, addr->devid, addr->function);
488 * Detach device specified by its pci address.
491 rte_pci_detach(const struct rte_pci_addr *addr)
493 struct rte_pci_device *dev = NULL;
499 FOREACH_DEVICE_ON_PCIBUS(dev) {
500 if (rte_pci_addr_cmp(&dev->addr, addr))
503 ret = rte_pci_detach_dev(dev);
505 /* negative value is an error */
508 /* positive value means driver doesn't support it */
511 rte_pci_remove_device(dev);
518 RTE_LOG(WARNING, EAL, "Requested device " PCI_PRI_FMT
519 " cannot be used\n", dev->addr.domain, dev->addr.bus,
520 dev->addr.devid, dev->addr.function);
525 * Scan the content of the PCI bus, and call the probe() function for
526 * all registered drivers that have a matching entry in its id_table
527 * for discovered devices.
532 struct rte_pci_device *dev = NULL;
533 size_t probed = 0, failed = 0;
534 struct rte_devargs *devargs;
538 if (rte_pci_bus.bus.conf.scan_mode != RTE_BUS_SCAN_WHITELIST)
541 FOREACH_DEVICE_ON_PCIBUS(dev) {
544 devargs = dev->device.devargs;
545 /* probe all or only whitelisted devices */
547 ret = pci_probe_all_drivers(dev);
548 else if (devargs != NULL &&
549 devargs->policy == RTE_DEV_WHITELISTED)
550 ret = pci_probe_all_drivers(dev);
552 RTE_LOG(ERR, EAL, "Requested device " PCI_PRI_FMT
553 " cannot be used\n", dev->addr.domain, dev->addr.bus,
554 dev->addr.devid, dev->addr.function);
561 return (probed && probed == failed) ? -1 : 0;
564 /* dump one device */
566 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
570 fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
571 dev->addr.devid, dev->addr.function);
572 fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
575 for (i = 0; i != sizeof(dev->mem_resource) /
576 sizeof(dev->mem_resource[0]); i++) {
577 fprintf(f, " %16.16"PRIx64" %16.16"PRIx64"\n",
578 dev->mem_resource[i].phys_addr,
579 dev->mem_resource[i].len);
584 /* dump devices on the bus */
586 rte_pci_dump(FILE *f)
588 struct rte_pci_device *dev = NULL;
590 FOREACH_DEVICE_ON_PCIBUS(dev) {
591 pci_dump_one_device(f, dev);
596 pci_parse(const char *name, void *addr)
598 struct rte_pci_addr *out = addr;
599 struct rte_pci_addr pci_addr;
602 parse = (rte_pci_addr_parse(name, &pci_addr) == 0);
603 if (parse && addr != NULL)
605 return parse == false;
608 /* register a driver */
610 rte_pci_register(struct rte_pci_driver *driver)
612 TAILQ_INSERT_TAIL(&rte_pci_bus.driver_list, driver, next);
613 driver->bus = &rte_pci_bus;
616 /* unregister a driver */
618 rte_pci_unregister(struct rte_pci_driver *driver)
620 TAILQ_REMOVE(&rte_pci_bus.driver_list, driver, next);
624 /* Add a device to PCI bus */
626 rte_pci_add_device(struct rte_pci_device *pci_dev)
628 TAILQ_INSERT_TAIL(&rte_pci_bus.device_list, pci_dev, next);
631 /* Insert a device into a predefined position in PCI bus */
633 rte_pci_insert_device(struct rte_pci_device *exist_pci_dev,
634 struct rte_pci_device *new_pci_dev)
636 TAILQ_INSERT_BEFORE(exist_pci_dev, new_pci_dev, next);
639 /* Remove a device from PCI bus */
641 rte_pci_remove_device(struct rte_pci_device *pci_dev)
643 TAILQ_REMOVE(&rte_pci_bus.device_list, pci_dev, next);
646 static struct rte_device *
647 pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
650 struct rte_pci_device *dev;
652 FOREACH_DEVICE_ON_PCIBUS(dev) {
653 if (start && &dev->device == start) {
654 start = NULL; /* starting point found */
657 if (cmp(&dev->device, data) == 0)
665 pci_plug(struct rte_device *dev)
667 return pci_probe_all_drivers(RTE_DEV_TO_PCI(dev));
671 pci_unplug(struct rte_device *dev)
673 struct rte_pci_device *pdev;
676 pdev = RTE_DEV_TO_PCI(dev);
677 ret = rte_pci_detach_dev(pdev);
679 rte_pci_remove_device(pdev);
685 struct rte_pci_bus rte_pci_bus = {
687 .scan = rte_pci_scan,
688 .probe = rte_pci_probe,
689 .find_device = pci_find_device,
691 .unplug = pci_unplug,
693 .get_iommu_class = rte_pci_get_iommu_class,
695 .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
696 .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
699 RTE_REGISTER_BUS(pci, rte_pci_bus.bus);