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>
47 #include <rte_per_lcore.h>
48 #include <rte_memory.h>
50 #include <rte_string_fns.h>
51 #include <rte_common.h>
55 static inline const char *
56 get_u8_pciaddr_field(const char *in, void *_u8, char dlm)
63 val = strtoul(in, &end, 16);
64 if (errno != 0 || end[0] != dlm || val > UINT8_MAX) {
65 errno = errno ? errno : EINVAL;
73 pci_bdf_parse(const char *input, struct rte_pci_addr *dev_addr)
75 const char *in = input;
78 in = get_u8_pciaddr_field(in, &dev_addr->bus, ':');
81 in = get_u8_pciaddr_field(in, &dev_addr->devid, '.');
84 in = get_u8_pciaddr_field(in, &dev_addr->function, '\0');
91 pci_dbdf_parse(const char *input, struct rte_pci_addr *dev_addr)
93 const char *in = input;
98 val = strtoul(in, &end, 16);
99 if (errno != 0 || end[0] != ':' || val > UINT16_MAX)
101 dev_addr->domain = (uint16_t)val;
103 in = get_u8_pciaddr_field(in, &dev_addr->bus, ':');
106 in = get_u8_pciaddr_field(in, &dev_addr->devid, '.');
109 in = get_u8_pciaddr_field(in, &dev_addr->function, '\0');
116 eal_parse_pci_BDF(const char *input, struct rte_pci_addr *dev_addr)
118 return pci_bdf_parse(input, dev_addr);
122 eal_parse_pci_DomBDF(const char *input, struct rte_pci_addr *dev_addr)
124 return pci_dbdf_parse(input, dev_addr);
128 rte_pci_device_name(const struct rte_pci_addr *addr,
129 char *output, size_t size)
131 RTE_VERIFY(size >= PCI_PRI_STR_SIZE);
132 RTE_VERIFY(snprintf(output, size, PCI_PRI_FMT,
133 addr->domain, addr->bus,
134 addr->devid, addr->function) >= 0);
138 rte_eal_compare_pci_addr(const struct rte_pci_addr *addr,
139 const struct rte_pci_addr *addr2)
141 return rte_pci_addr_cmp(addr, addr2);
145 rte_pci_addr_cmp(const struct rte_pci_addr *addr,
146 const struct rte_pci_addr *addr2)
148 uint64_t dev_addr, dev_addr2;
150 if ((addr == NULL) || (addr2 == NULL))
153 dev_addr = ((uint64_t)addr->domain << 24) |
154 (addr->bus << 16) | (addr->devid << 8) | addr->function;
155 dev_addr2 = ((uint64_t)addr2->domain << 24) |
156 (addr2->bus << 16) | (addr2->devid << 8) | addr2->function;
158 if (dev_addr > dev_addr2)
160 else if (dev_addr < dev_addr2)
167 rte_pci_addr_parse(const char *str, struct rte_pci_addr *addr)
169 if (pci_bdf_parse(str, addr) == 0 ||
170 pci_dbdf_parse(str, addr) == 0)
176 /* map a particular resource from a file */
178 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
179 int additional_flags)
183 /* Map the PCI memory resource of device */
184 mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
185 MAP_SHARED | additional_flags, fd, offset);
186 if (mapaddr == MAP_FAILED) {
187 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n",
188 __func__, fd, requested_addr,
189 (unsigned long)size, (unsigned long)offset,
190 strerror(errno), mapaddr);
192 RTE_LOG(DEBUG, EAL, " PCI memory mapped at %p\n", mapaddr);
197 /* unmap a particular resource */
199 pci_unmap_resource(void *requested_addr, size_t size)
201 if (requested_addr == NULL)
204 /* Unmap the PCI memory resource of device */
205 if (munmap(requested_addr, size)) {
206 RTE_LOG(ERR, EAL, "%s(): cannot munmap(%p, 0x%lx): %s\n",
207 __func__, requested_addr, (unsigned long)size,
210 RTE_LOG(DEBUG, EAL, " PCI memory unmapped at %p\n",