1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2015 Intel Corporation
3 */
4
5 #include <fcntl.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <sys/mman.h>
11
12 #include <rte_eal.h>
13 #include <rte_pci.h>
14 #include <rte_bus_pci.h>
15 #include <rte_tailq.h>
16 #include <rte_log.h>
17 #include <rte_malloc.h>
18
19 #include "private.h"
20
21 static struct rte_tailq_elem rte_uio_tailq = {
22 .name = "UIO_RESOURCE_LIST",
23 };
EAL_REGISTER_TAILQ(rte_uio_tailq)24 EAL_REGISTER_TAILQ(rte_uio_tailq)
25
26 static int
27 pci_uio_map_secondary(struct rte_pci_device *dev)
28 {
29 int fd, i, j;
30 struct mapped_pci_resource *uio_res;
31 struct mapped_pci_res_list *uio_res_list =
32 RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
33
34 TAILQ_FOREACH(uio_res, uio_res_list, next) {
35
36 /* skip this element if it doesn't match our PCI address */
37 if (rte_pci_addr_cmp(&uio_res->pci_addr, &dev->addr))
38 continue;
39
40 for (i = 0; i != uio_res->nb_maps; i++) {
41 /*
42 * open devname, to mmap it
43 */
44 fd = open(uio_res->maps[i].path, O_RDWR);
45 if (fd < 0) {
46 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
47 uio_res->maps[i].path, strerror(errno));
48 return -1;
49 }
50
51 void *mapaddr = pci_map_resource(uio_res->maps[i].addr,
52 fd, (off_t)uio_res->maps[i].offset,
53 (size_t)uio_res->maps[i].size, 0);
54
55 /* fd is not needed in secondary process, close it */
56 close(fd);
57 if (mapaddr != uio_res->maps[i].addr) {
58 RTE_LOG(ERR, EAL,
59 "Cannot mmap device resource file %s to address: %p\n",
60 uio_res->maps[i].path,
61 uio_res->maps[i].addr);
62 if (mapaddr != NULL) {
63 /* unmap addrs correctly mapped */
64 for (j = 0; j < i; j++)
65 pci_unmap_resource(
66 uio_res->maps[j].addr,
67 (size_t)uio_res->maps[j].size);
68 /* unmap addr wrongly mapped */
69 pci_unmap_resource(mapaddr,
70 (size_t)uio_res->maps[i].size);
71 }
72 return -1;
73 }
74 dev->mem_resource[i].addr = mapaddr;
75 }
76 return 0;
77 }
78
79 RTE_LOG(ERR, EAL, "Cannot find resource for device\n");
80 return 1;
81 }
82
83 /* map the PCI resource of a PCI device in virtual memory */
84 int
pci_uio_map_resource(struct rte_pci_device * dev)85 pci_uio_map_resource(struct rte_pci_device *dev)
86 {
87 int i, map_idx = 0, ret;
88 uint64_t phaddr;
89 struct mapped_pci_resource *uio_res = NULL;
90 struct mapped_pci_res_list *uio_res_list =
91 RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
92
93 dev->intr_handle.fd = -1;
94 dev->intr_handle.uio_cfg_fd = -1;
95
96 /* secondary processes - use already recorded details */
97 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
98 return pci_uio_map_secondary(dev);
99
100 /* allocate uio resource */
101 ret = pci_uio_alloc_resource(dev, &uio_res);
102 if (ret)
103 return ret;
104
105 /* Map all BARs */
106 for (i = 0; i != PCI_MAX_RESOURCE; i++) {
107 /* skip empty BAR */
108 phaddr = dev->mem_resource[i].phys_addr;
109 if (phaddr == 0)
110 continue;
111
112 ret = pci_uio_map_resource_by_index(dev, i,
113 uio_res, map_idx);
114 if (ret)
115 goto error;
116
117 map_idx++;
118 }
119
120 uio_res->nb_maps = map_idx;
121
122 TAILQ_INSERT_TAIL(uio_res_list, uio_res, next);
123
124 return 0;
125 error:
126 for (i = 0; i < map_idx; i++) {
127 pci_unmap_resource(uio_res->maps[i].addr,
128 (size_t)uio_res->maps[i].size);
129 rte_free(uio_res->maps[i].path);
130 }
131 pci_uio_free_resource(dev, uio_res);
132 return -1;
133 }
134
135 static void
pci_uio_unmap(struct mapped_pci_resource * uio_res)136 pci_uio_unmap(struct mapped_pci_resource *uio_res)
137 {
138 int i;
139
140 if (uio_res == NULL)
141 return;
142
143 for (i = 0; i != uio_res->nb_maps; i++) {
144 pci_unmap_resource(uio_res->maps[i].addr,
145 (size_t)uio_res->maps[i].size);
146 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
147 rte_free(uio_res->maps[i].path);
148 }
149 }
150
151 /* remap the PCI resource of a PCI device in anonymous virtual memory */
152 int
pci_uio_remap_resource(struct rte_pci_device * dev)153 pci_uio_remap_resource(struct rte_pci_device *dev)
154 {
155 int i;
156 void *map_address;
157
158 if (dev == NULL)
159 return -1;
160
161 /* Remap all BARs */
162 for (i = 0; i != PCI_MAX_RESOURCE; i++) {
163 /* skip empty BAR */
164 if (dev->mem_resource[i].phys_addr == 0)
165 continue;
166 map_address = mmap(dev->mem_resource[i].addr,
167 (size_t)dev->mem_resource[i].len,
168 PROT_READ | PROT_WRITE,
169 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
170 if (map_address == MAP_FAILED) {
171 RTE_LOG(ERR, EAL,
172 "Cannot remap resource for device %s\n",
173 dev->name);
174 return -1;
175 }
176 RTE_LOG(INFO, EAL,
177 "Successful remap resource for device %s\n",
178 dev->name);
179 }
180
181 return 0;
182 }
183
184 static struct mapped_pci_resource *
pci_uio_find_resource(struct rte_pci_device * dev)185 pci_uio_find_resource(struct rte_pci_device *dev)
186 {
187 struct mapped_pci_resource *uio_res;
188 struct mapped_pci_res_list *uio_res_list =
189 RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
190
191 if (dev == NULL)
192 return NULL;
193
194 TAILQ_FOREACH(uio_res, uio_res_list, next) {
195
196 /* skip this element if it doesn't match our PCI address */
197 if (!rte_pci_addr_cmp(&uio_res->pci_addr, &dev->addr))
198 return uio_res;
199 }
200 return NULL;
201 }
202
203 /* unmap the PCI resource of a PCI device in virtual memory */
204 void
pci_uio_unmap_resource(struct rte_pci_device * dev)205 pci_uio_unmap_resource(struct rte_pci_device *dev)
206 {
207 struct mapped_pci_resource *uio_res;
208 struct mapped_pci_res_list *uio_res_list =
209 RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
210
211 if (dev == NULL)
212 return;
213
214 /* find an entry for the device */
215 uio_res = pci_uio_find_resource(dev);
216 if (uio_res == NULL)
217 return;
218
219 /* secondary processes - just free maps */
220 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
221 return pci_uio_unmap(uio_res);
222
223 TAILQ_REMOVE(uio_res_list, uio_res, next);
224
225 /* unmap all resources */
226 pci_uio_unmap(uio_res);
227
228 /* free uio resource */
229 rte_free(uio_res);
230
231 /* close fd if in primary process */
232 close(dev->intr_handle.fd);
233 if (dev->intr_handle.uio_cfg_fd >= 0) {
234 close(dev->intr_handle.uio_cfg_fd);
235 dev->intr_handle.uio_cfg_fd = -1;
236 }
237
238 dev->intr_handle.fd = -1;
239 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
240 }
241