1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright 2013-2014 6WIND S.A.
10 #include <rte_debug.h>
14 static inline const char *
15 get_u8_pciaddr_field(const char *in, void *_u8, char dlm)
21 /* empty string is an error though strtoul() returns 0 */
25 /* PCI field starting with spaces is forbidden.
26 * Negative wrap-around is not reported as an error by strtoul.
28 if (*in == ' ' || *in == '-')
32 val = strtoul(in, &end, 16);
33 if (errno != 0 || end[0] != dlm || val > UINT8_MAX) {
34 errno = errno ? errno : EINVAL;
42 pci_bdf_parse(const char *input, struct rte_pci_addr *dev_addr)
44 const char *in = input;
47 in = get_u8_pciaddr_field(in, &dev_addr->bus, ':');
50 in = get_u8_pciaddr_field(in, &dev_addr->devid, '.');
53 in = get_u8_pciaddr_field(in, &dev_addr->function, '\0');
60 pci_dbdf_parse(const char *input, struct rte_pci_addr *dev_addr)
62 const char *in = input;
66 /* PCI id starting with spaces is forbidden.
67 * Negative wrap-around is not reported as an error by strtoul.
69 if (*in == ' ' || *in == '-')
73 val = strtoul(in, &end, 16);
74 /* Empty string is not an error for strtoul, but the check
76 * will detect the issue.
78 if (errno != 0 || end[0] != ':' || val > UINT32_MAX)
80 dev_addr->domain = (uint32_t)val;
82 in = get_u8_pciaddr_field(in, &dev_addr->bus, ':');
85 in = get_u8_pciaddr_field(in, &dev_addr->devid, '.');
88 in = get_u8_pciaddr_field(in, &dev_addr->function, '\0');
95 rte_pci_device_name(const struct rte_pci_addr *addr,
96 char *output, size_t size)
98 RTE_VERIFY(size >= PCI_PRI_STR_SIZE);
99 RTE_VERIFY(snprintf(output, size, PCI_PRI_FMT,
100 addr->domain, addr->bus,
101 addr->devid, addr->function) >= 0);
105 rte_pci_addr_cmp(const struct rte_pci_addr *addr,
106 const struct rte_pci_addr *addr2)
108 uint64_t dev_addr, dev_addr2;
110 if ((addr == NULL) || (addr2 == NULL))
113 dev_addr = ((uint64_t)addr->domain << 24) |
114 (addr->bus << 16) | (addr->devid << 8) | addr->function;
115 dev_addr2 = ((uint64_t)addr2->domain << 24) |
116 (addr2->bus << 16) | (addr2->devid << 8) | addr2->function;
118 if (dev_addr > dev_addr2)
120 else if (dev_addr < dev_addr2)
127 rte_pci_addr_parse(const char *str, struct rte_pci_addr *addr)
129 if (pci_bdf_parse(str, addr) == 0 ||
130 pci_dbdf_parse(str, addr) == 0)