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