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>
14 #include <rte_errno.h>
15 #include <rte_interrupts.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 */
39 val = strtoul(in, &end, 16);
40 if (errno != 0 || end[0] != dlm || val > UINT8_MAX) {
41 errno = errno ? errno : EINVAL;
49 pci_bdf_parse(const char *input, struct rte_pci_addr *dev_addr)
51 const char *in = input;
54 in = get_u8_pciaddr_field(in, &dev_addr->bus, ':');
57 in = get_u8_pciaddr_field(in, &dev_addr->devid, '.');
60 in = get_u8_pciaddr_field(in, &dev_addr->function, '\0');
67 pci_dbdf_parse(const char *input, struct rte_pci_addr *dev_addr)
69 const char *in = input;
74 val = strtoul(in, &end, 16);
75 if (errno != 0 || end[0] != ':' || val > UINT16_MAX)
77 dev_addr->domain = (uint16_t)val;
79 in = get_u8_pciaddr_field(in, &dev_addr->bus, ':');
82 in = get_u8_pciaddr_field(in, &dev_addr->devid, '.');
85 in = get_u8_pciaddr_field(in, &dev_addr->function, '\0');
92 rte_pci_device_name(const struct rte_pci_addr *addr,
93 char *output, size_t size)
95 RTE_VERIFY(size >= PCI_PRI_STR_SIZE);
96 RTE_VERIFY(snprintf(output, size, PCI_PRI_FMT,
97 addr->domain, addr->bus,
98 addr->devid, addr->function) >= 0);
102 rte_pci_addr_cmp(const struct rte_pci_addr *addr,
103 const struct rte_pci_addr *addr2)
105 uint64_t dev_addr, dev_addr2;
107 if ((addr == NULL) || (addr2 == NULL))
110 dev_addr = ((uint64_t)addr->domain << 24) |
111 (addr->bus << 16) | (addr->devid << 8) | addr->function;
112 dev_addr2 = ((uint64_t)addr2->domain << 24) |
113 (addr2->bus << 16) | (addr2->devid << 8) | addr2->function;
115 if (dev_addr > dev_addr2)
117 else if (dev_addr < dev_addr2)
124 rte_pci_addr_parse(const char *str, struct rte_pci_addr *addr)
126 if (pci_bdf_parse(str, addr) == 0 ||
127 pci_dbdf_parse(str, addr) == 0)
133 /* map a particular resource from a file */
135 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
136 int additional_flags)
140 /* Map the PCI memory resource of device */
141 mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
142 MAP_SHARED | additional_flags, fd, offset);
143 if (mapaddr == MAP_FAILED) {
145 "%s(): cannot mmap(%d, %p, 0x%zx, 0x%llx): %s (%p)\n",
146 __func__, fd, requested_addr, size,
147 (unsigned long long)offset,
148 strerror(errno), mapaddr);
150 RTE_LOG(DEBUG, EAL, " PCI memory mapped at %p\n", mapaddr);
155 /* unmap a particular resource */
157 pci_unmap_resource(void *requested_addr, size_t size)
159 if (requested_addr == NULL)
162 /* Unmap the PCI memory resource of device */
163 if (munmap(requested_addr, size)) {
164 RTE_LOG(ERR, EAL, "%s(): cannot munmap(%p, %#zx): %s\n",
165 __func__, requested_addr, size,
168 RTE_LOG(DEBUG, EAL, " PCI memory unmapped at %p\n",