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 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 pci_name_set(struct rte_pci_device *dev)
205 struct rte_devargs *devargs;
207 /* Each device has its internal, canonical name set. */
208 rte_pci_device_name(&dev->addr,
209 dev->name, sizeof(dev->name));
210 devargs = pci_devargs_lookup(dev);
211 dev->device.devargs = devargs;
212 /* In blacklist mode, if the device is not blacklisted, no
213 * rte_devargs exists for it.
216 /* If an rte_devargs exists, the generic rte_device uses the
217 * given name as its namea
219 dev->device.name = dev->device.devargs->name;
221 /* Otherwise, it uses the internal, canonical form. */
222 dev->device.name = dev->name;
225 /* map a particular resource from a file */
227 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
228 int additional_flags)
232 /* Map the PCI memory resource of device */
233 mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
234 MAP_SHARED | additional_flags, fd, offset);
235 if (mapaddr == MAP_FAILED) {
236 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n",
237 __func__, fd, requested_addr,
238 (unsigned long)size, (unsigned long)offset,
239 strerror(errno), mapaddr);
241 RTE_LOG(DEBUG, EAL, " PCI memory mapped at %p\n", mapaddr);
246 /* unmap a particular resource */
248 pci_unmap_resource(void *requested_addr, size_t size)
250 if (requested_addr == NULL)
253 /* Unmap the PCI memory resource of device */
254 if (munmap(requested_addr, size)) {
255 RTE_LOG(ERR, EAL, "%s(): cannot munmap(%p, 0x%lx): %s\n",
256 __func__, requested_addr, (unsigned long)size,
259 RTE_LOG(DEBUG, EAL, " PCI memory unmapped at %p\n",
264 * Match the PCI Driver and Device using the ID Table
267 rte_pci_match(const struct rte_pci_driver *pci_drv,
268 const struct rte_pci_device *pci_dev)
270 const struct rte_pci_id *id_table;
272 for (id_table = pci_drv->id_table; id_table->vendor_id != 0;
274 /* check if device's identifiers match the driver's ones */
275 if (id_table->vendor_id != pci_dev->id.vendor_id &&
276 id_table->vendor_id != PCI_ANY_ID)
278 if (id_table->device_id != pci_dev->id.device_id &&
279 id_table->device_id != PCI_ANY_ID)
281 if (id_table->subsystem_vendor_id !=
282 pci_dev->id.subsystem_vendor_id &&
283 id_table->subsystem_vendor_id != PCI_ANY_ID)
285 if (id_table->subsystem_device_id !=
286 pci_dev->id.subsystem_device_id &&
287 id_table->subsystem_device_id != PCI_ANY_ID)
289 if (id_table->class_id != pci_dev->id.class_id &&
290 id_table->class_id != RTE_CLASS_ANY_ID)
300 * If vendor/device ID match, call the probe() function of the
304 rte_pci_probe_one_driver(struct rte_pci_driver *dr,
305 struct rte_pci_device *dev)
308 struct rte_pci_addr *loc;
310 if ((dr == NULL) || (dev == NULL))
315 /* The device is not blacklisted; Check if driver supports it */
316 if (!rte_pci_match(dr, dev))
317 /* Match of device and driver failed */
320 RTE_LOG(INFO, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
321 loc->domain, loc->bus, loc->devid, loc->function,
322 dev->device.numa_node);
324 /* no initialization when blacklisted, return without error */
325 if (dev->device.devargs != NULL &&
326 dev->device.devargs->policy ==
327 RTE_DEV_BLACKLISTED) {
328 RTE_LOG(INFO, EAL, " Device is blacklisted, not"
333 if (dev->device.numa_node < 0) {
334 RTE_LOG(WARNING, EAL, " Invalid NUMA socket, default to 0\n");
335 dev->device.numa_node = 0;
338 RTE_LOG(INFO, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
339 dev->id.device_id, dr->driver.name);
341 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
342 /* map resources for devices that use igb_uio */
343 ret = rte_pci_map_device(dev);
348 /* reference driver structure */
350 dev->device.driver = &dr->driver;
352 /* call the driver probe() function */
353 ret = dr->probe(dr, dev);
356 dev->device.driver = NULL;
357 if ((dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) &&
358 /* Don't unmap if device is unsupported and
359 * driver needs mapped resources.
362 (dr->drv_flags & RTE_PCI_DRV_KEEP_MAPPED_RES)))
363 rte_pci_unmap_device(dev);
370 * If vendor/device ID match, call the remove() function of the
374 rte_pci_detach_dev(struct rte_pci_device *dev)
376 struct rte_pci_addr *loc;
377 struct rte_pci_driver *dr;
386 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
387 loc->domain, loc->bus, loc->devid,
388 loc->function, dev->device.numa_node);
390 RTE_LOG(DEBUG, EAL, " remove driver: %x:%x %s\n", dev->id.vendor_id,
391 dev->id.device_id, dr->driver.name);
394 ret = dr->remove(dev);
399 /* clear driver structure */
402 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
403 /* unmap resources for devices that use igb_uio */
404 rte_pci_unmap_device(dev);
410 * If vendor/device ID match, call the probe() function of all
411 * registered driver for the given device. Return -1 if initialization
412 * failed, return 1 if no driver is found for this device.
415 pci_probe_all_drivers(struct rte_pci_device *dev)
417 struct rte_pci_driver *dr = NULL;
423 /* Check if a driver is already loaded */
424 if (dev->driver != NULL)
427 FOREACH_DRIVER_ON_PCIBUS(dr) {
428 rc = rte_pci_probe_one_driver(dr, dev);
430 /* negative value is an error */
433 /* positive value means driver doesn't support it */
441 * Find the pci device specified by pci address, then invoke probe function of
442 * the driver of the device.
445 rte_pci_probe_one(const struct rte_pci_addr *addr)
447 struct rte_pci_device *dev = NULL;
454 /* update current pci device in global list, kernel bindings might have
455 * changed since last time we looked at it.
457 if (pci_update_device(addr) < 0)
460 FOREACH_DEVICE_ON_PCIBUS(dev) {
461 if (rte_eal_compare_pci_addr(&dev->addr, addr))
464 ret = pci_probe_all_drivers(dev);
472 RTE_LOG(WARNING, EAL,
473 "Requested device " PCI_PRI_FMT " cannot be used\n",
474 addr->domain, addr->bus, addr->devid, addr->function);
479 * Detach device specified by its pci address.
482 rte_pci_detach(const struct rte_pci_addr *addr)
484 struct rte_pci_device *dev = NULL;
490 FOREACH_DEVICE_ON_PCIBUS(dev) {
491 if (rte_eal_compare_pci_addr(&dev->addr, addr))
494 ret = rte_pci_detach_dev(dev);
496 /* negative value is an error */
499 /* positive value means driver doesn't support it */
502 rte_pci_remove_device(dev);
509 RTE_LOG(WARNING, EAL, "Requested device " PCI_PRI_FMT
510 " cannot be used\n", dev->addr.domain, dev->addr.bus,
511 dev->addr.devid, dev->addr.function);
516 * Scan the content of the PCI bus, and call the probe() function for
517 * all registered drivers that have a matching entry in its id_table
518 * for discovered devices.
523 struct rte_pci_device *dev = NULL;
524 size_t probed = 0, failed = 0;
525 struct rte_devargs *devargs;
529 if (rte_pci_bus.bus.conf.scan_mode != RTE_BUS_SCAN_WHITELIST)
532 FOREACH_DEVICE_ON_PCIBUS(dev) {
535 devargs = dev->device.devargs;
536 /* probe all or only whitelisted devices */
538 ret = pci_probe_all_drivers(dev);
539 else if (devargs != NULL &&
540 devargs->policy == RTE_DEV_WHITELISTED)
541 ret = pci_probe_all_drivers(dev);
543 RTE_LOG(ERR, EAL, "Requested device " PCI_PRI_FMT
544 " cannot be used\n", dev->addr.domain, dev->addr.bus,
545 dev->addr.devid, dev->addr.function);
552 return (probed && probed == failed) ? -1 : 0;
555 /* dump one device */
557 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
561 fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
562 dev->addr.devid, dev->addr.function);
563 fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
566 for (i = 0; i != sizeof(dev->mem_resource) /
567 sizeof(dev->mem_resource[0]); i++) {
568 fprintf(f, " %16.16"PRIx64" %16.16"PRIx64"\n",
569 dev->mem_resource[i].phys_addr,
570 dev->mem_resource[i].len);
575 /* dump devices on the bus */
577 rte_pci_dump(FILE *f)
579 struct rte_pci_device *dev = NULL;
581 FOREACH_DEVICE_ON_PCIBUS(dev) {
582 pci_dump_one_device(f, dev);
587 pci_parse(const char *name, void *addr)
589 struct rte_pci_addr *out = addr;
590 struct rte_pci_addr pci_addr;
593 parse = (eal_parse_pci_BDF(name, &pci_addr) == 0 ||
594 eal_parse_pci_DomBDF(name, &pci_addr) == 0);
595 if (parse && addr != NULL)
597 return parse == false;
600 /* register a driver */
602 rte_pci_register(struct rte_pci_driver *driver)
604 TAILQ_INSERT_TAIL(&rte_pci_bus.driver_list, driver, next);
605 driver->bus = &rte_pci_bus;
608 /* unregister a driver */
610 rte_pci_unregister(struct rte_pci_driver *driver)
612 TAILQ_REMOVE(&rte_pci_bus.driver_list, driver, next);
616 /* Add a device to PCI bus */
618 rte_pci_add_device(struct rte_pci_device *pci_dev)
620 TAILQ_INSERT_TAIL(&rte_pci_bus.device_list, pci_dev, next);
623 /* Insert a device into a predefined position in PCI bus */
625 rte_pci_insert_device(struct rte_pci_device *exist_pci_dev,
626 struct rte_pci_device *new_pci_dev)
628 TAILQ_INSERT_BEFORE(exist_pci_dev, new_pci_dev, next);
631 /* Remove a device from PCI bus */
633 rte_pci_remove_device(struct rte_pci_device *pci_dev)
635 TAILQ_REMOVE(&rte_pci_bus.device_list, pci_dev, next);
638 static struct rte_device *
639 pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
642 struct rte_pci_device *dev;
644 FOREACH_DEVICE_ON_PCIBUS(dev) {
645 if (start && &dev->device == start) {
646 start = NULL; /* starting point found */
649 if (cmp(&dev->device, data) == 0)
657 pci_plug(struct rte_device *dev)
659 return pci_probe_all_drivers(RTE_DEV_TO_PCI(dev));
663 pci_unplug(struct rte_device *dev)
665 struct rte_pci_device *pdev;
668 pdev = RTE_DEV_TO_PCI(dev);
669 ret = rte_pci_detach_dev(pdev);
671 rte_pci_remove_device(pdev);
677 struct rte_pci_bus rte_pci_bus = {
679 .scan = rte_pci_scan,
680 .probe = rte_pci_probe,
681 .find_device = pci_find_device,
683 .unplug = pci_unplug,
685 .get_iommu_class = rte_pci_get_iommu_class,
687 .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
688 .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
691 RTE_REGISTER_BUS(pci, rte_pci_bus.bus);