1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright 2013-2014 6WIND S.A.
11 #include <sys/queue.h>
13 #include <rte_errno.h>
14 #include <rte_interrupts.h>
17 #include <rte_eal_paging.h>
18 #include <rte_per_lcore.h>
19 #include <rte_memory.h>
21 #include <rte_string_fns.h>
22 #include <rte_common.h>
23 #include <rte_debug.h>
27 static inline const char *
28 get_u8_pciaddr_field(const char *in, void *_u8, char dlm)
34 /* empty string is an error though strtoul() returns 0 */
38 /* PCI field starting with spaces is forbidden.
39 * Negative wrap-around is not reported as an error by strtoul.
41 if (*in == ' ' || *in == '-')
45 val = strtoul(in, &end, 16);
46 if (errno != 0 || end[0] != dlm || val > UINT8_MAX) {
47 errno = errno ? errno : EINVAL;
55 pci_bdf_parse(const char *input, struct rte_pci_addr *dev_addr)
57 const char *in = input;
60 in = get_u8_pciaddr_field(in, &dev_addr->bus, ':');
63 in = get_u8_pciaddr_field(in, &dev_addr->devid, '.');
66 in = get_u8_pciaddr_field(in, &dev_addr->function, '\0');
73 pci_dbdf_parse(const char *input, struct rte_pci_addr *dev_addr)
75 const char *in = input;
79 /* PCI id starting with spaces is forbidden.
80 * Negative wrap-around is not reported as an error by strtoul.
82 if (*in == ' ' || *in == '-')
86 val = strtoul(in, &end, 16);
87 /* Empty string is not an error for strtoul, but the check
89 * will detect the issue.
91 if (errno != 0 || end[0] != ':' || val > UINT32_MAX)
93 dev_addr->domain = (uint32_t)val;
95 in = get_u8_pciaddr_field(in, &dev_addr->bus, ':');
98 in = get_u8_pciaddr_field(in, &dev_addr->devid, '.');
101 in = get_u8_pciaddr_field(in, &dev_addr->function, '\0');
108 rte_pci_device_name(const struct rte_pci_addr *addr,
109 char *output, size_t size)
111 RTE_VERIFY(size >= PCI_PRI_STR_SIZE);
112 RTE_VERIFY(snprintf(output, size, PCI_PRI_FMT,
113 addr->domain, addr->bus,
114 addr->devid, addr->function) >= 0);
118 rte_pci_addr_cmp(const struct rte_pci_addr *addr,
119 const struct rte_pci_addr *addr2)
121 uint64_t dev_addr, dev_addr2;
123 if ((addr == NULL) || (addr2 == NULL))
126 dev_addr = ((uint64_t)addr->domain << 24) |
127 (addr->bus << 16) | (addr->devid << 8) | addr->function;
128 dev_addr2 = ((uint64_t)addr2->domain << 24) |
129 (addr2->bus << 16) | (addr2->devid << 8) | addr2->function;
131 if (dev_addr > dev_addr2)
133 else if (dev_addr < dev_addr2)
140 rte_pci_addr_parse(const char *str, struct rte_pci_addr *addr)
142 if (pci_bdf_parse(str, addr) == 0 ||
143 pci_dbdf_parse(str, addr) == 0)
149 /* map a particular resource from a file */
151 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
152 int additional_flags)
156 /* Map the PCI memory resource of device */
157 mapaddr = rte_mem_map(requested_addr, size,
158 RTE_PROT_READ | RTE_PROT_WRITE,
159 RTE_MAP_SHARED | additional_flags, fd, offset);
160 if (mapaddr == NULL) {
162 "%s(): cannot map resource(%d, %p, 0x%zx, 0x%llx): %s (%p)\n",
163 __func__, fd, requested_addr, size,
164 (unsigned long long)offset,
165 rte_strerror(rte_errno), mapaddr);
167 RTE_LOG(DEBUG, EAL, " PCI memory mapped at %p\n", mapaddr);
172 /* unmap a particular resource */
174 pci_unmap_resource(void *requested_addr, size_t size)
176 if (requested_addr == NULL)
179 /* Unmap the PCI memory resource of device */
180 if (rte_mem_unmap(requested_addr, size)) {
181 RTE_LOG(ERR, EAL, "%s(): cannot mem unmap(%p, %#zx): %s\n",
182 __func__, requested_addr, size,
183 rte_strerror(rte_errno));
185 RTE_LOG(DEBUG, EAL, " PCI memory unmapped at %p\n",