]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/dpdk/lib/librte_pci/rte_pci.c
import 15.2.0 Octopus source
[ceph.git] / ceph / src / seastar / dpdk / lib / librte_pci / rte_pci.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright 2013-2014 6WIND S.A.
4 */
5
6 #include <string.h>
7 #include <inttypes.h>
8 #include <stdint.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <sys/queue.h>
12 #include <sys/mman.h>
13
14 #include <rte_errno.h>
15 #include <rte_interrupts.h>
16 #include <rte_log.h>
17 #include <rte_bus.h>
18 #include <rte_per_lcore.h>
19 #include <rte_memory.h>
20 #include <rte_eal.h>
21 #include <rte_string_fns.h>
22 #include <rte_common.h>
23
24 #include "rte_pci.h"
25
26 static inline const char *
27 get_u8_pciaddr_field(const char *in, void *_u8, char dlm)
28 {
29 unsigned long val;
30 uint8_t *u8 = _u8;
31 char *end;
32
33 /* empty string is an error though strtoul() returns 0 */
34 if (*in == '\0')
35 return NULL;
36
37 errno = 0;
38 val = strtoul(in, &end, 16);
39 if (errno != 0 || end[0] != dlm || val > UINT8_MAX) {
40 errno = errno ? errno : EINVAL;
41 return NULL;
42 }
43 *u8 = (uint8_t)val;
44 return end + 1;
45 }
46
47 static int
48 pci_bdf_parse(const char *input, struct rte_pci_addr *dev_addr)
49 {
50 const char *in = input;
51
52 dev_addr->domain = 0;
53 in = get_u8_pciaddr_field(in, &dev_addr->bus, ':');
54 if (in == NULL)
55 return -EINVAL;
56 in = get_u8_pciaddr_field(in, &dev_addr->devid, '.');
57 if (in == NULL)
58 return -EINVAL;
59 in = get_u8_pciaddr_field(in, &dev_addr->function, '\0');
60 if (in == NULL)
61 return -EINVAL;
62 return 0;
63 }
64
65 static int
66 pci_dbdf_parse(const char *input, struct rte_pci_addr *dev_addr)
67 {
68 const char *in = input;
69 unsigned long val;
70 char *end;
71
72 errno = 0;
73 val = strtoul(in, &end, 16);
74 if (errno != 0 || end[0] != ':' || val > UINT16_MAX)
75 return -EINVAL;
76 dev_addr->domain = (uint16_t)val;
77 in = end + 1;
78 in = get_u8_pciaddr_field(in, &dev_addr->bus, ':');
79 if (in == NULL)
80 return -EINVAL;
81 in = get_u8_pciaddr_field(in, &dev_addr->devid, '.');
82 if (in == NULL)
83 return -EINVAL;
84 in = get_u8_pciaddr_field(in, &dev_addr->function, '\0');
85 if (in == NULL)
86 return -EINVAL;
87 return 0;
88 }
89
90 int
91 eal_parse_pci_BDF(const char *input, struct rte_pci_addr *dev_addr)
92 {
93 return pci_bdf_parse(input, dev_addr);
94 }
95
96 int
97 eal_parse_pci_DomBDF(const char *input, struct rte_pci_addr *dev_addr)
98 {
99 return pci_dbdf_parse(input, dev_addr);
100 }
101
102 void
103 rte_pci_device_name(const struct rte_pci_addr *addr,
104 char *output, size_t size)
105 {
106 RTE_VERIFY(size >= PCI_PRI_STR_SIZE);
107 RTE_VERIFY(snprintf(output, size, PCI_PRI_FMT,
108 addr->domain, addr->bus,
109 addr->devid, addr->function) >= 0);
110 }
111
112 int
113 rte_eal_compare_pci_addr(const struct rte_pci_addr *addr,
114 const struct rte_pci_addr *addr2)
115 {
116 return rte_pci_addr_cmp(addr, addr2);
117 }
118
119 int
120 rte_pci_addr_cmp(const struct rte_pci_addr *addr,
121 const struct rte_pci_addr *addr2)
122 {
123 uint64_t dev_addr, dev_addr2;
124
125 if ((addr == NULL) || (addr2 == NULL))
126 return -1;
127
128 dev_addr = ((uint64_t)addr->domain << 24) |
129 (addr->bus << 16) | (addr->devid << 8) | addr->function;
130 dev_addr2 = ((uint64_t)addr2->domain << 24) |
131 (addr2->bus << 16) | (addr2->devid << 8) | addr2->function;
132
133 if (dev_addr > dev_addr2)
134 return 1;
135 else if (dev_addr < dev_addr2)
136 return -1;
137 else
138 return 0;
139 }
140
141 int
142 rte_pci_addr_parse(const char *str, struct rte_pci_addr *addr)
143 {
144 if (pci_bdf_parse(str, addr) == 0 ||
145 pci_dbdf_parse(str, addr) == 0)
146 return 0;
147 return -1;
148 }
149
150
151 /* map a particular resource from a file */
152 void *
153 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
154 int additional_flags)
155 {
156 void *mapaddr;
157
158 /* Map the PCI memory resource of device */
159 mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
160 MAP_SHARED | additional_flags, fd, offset);
161 if (mapaddr == MAP_FAILED) {
162 RTE_LOG(ERR, EAL,
163 "%s(): cannot mmap(%d, %p, 0x%zx, 0x%llx): %s (%p)\n",
164 __func__, fd, requested_addr, size,
165 (unsigned long long)offset,
166 strerror(errno), mapaddr);
167 } else
168 RTE_LOG(DEBUG, EAL, " PCI memory mapped at %p\n", mapaddr);
169
170 return mapaddr;
171 }
172
173 /* unmap a particular resource */
174 void
175 pci_unmap_resource(void *requested_addr, size_t size)
176 {
177 if (requested_addr == NULL)
178 return;
179
180 /* Unmap the PCI memory resource of device */
181 if (munmap(requested_addr, size)) {
182 RTE_LOG(ERR, EAL, "%s(): cannot munmap(%p, %#zx): %s\n",
183 __func__, requested_addr, size,
184 strerror(errno));
185 } else
186 RTE_LOG(DEBUG, EAL, " PCI memory unmapped at %p\n",
187 requested_addr);
188 }