New upstream version 17.11.1
[deb_dpdk.git] / drivers / bus / pci / pci_common_uio.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <fcntl.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <sys/mman.h>
40
41 #include <rte_eal.h>
42 #include <rte_pci.h>
43 #include <rte_bus_pci.h>
44 #include <rte_tailq.h>
45 #include <rte_log.h>
46 #include <rte_malloc.h>
47
48 #include "private.h"
49
50 static struct rte_tailq_elem rte_uio_tailq = {
51         .name = "UIO_RESOURCE_LIST",
52 };
53 EAL_REGISTER_TAILQ(rte_uio_tailq)
54
55 static int
56 pci_uio_map_secondary(struct rte_pci_device *dev)
57 {
58         int fd, i, j;
59         struct mapped_pci_resource *uio_res;
60         struct mapped_pci_res_list *uio_res_list =
61                         RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
62
63         TAILQ_FOREACH(uio_res, uio_res_list, next) {
64
65                 /* skip this element if it doesn't match our PCI address */
66                 if (rte_pci_addr_cmp(&uio_res->pci_addr, &dev->addr))
67                         continue;
68
69                 for (i = 0; i != uio_res->nb_maps; i++) {
70                         /*
71                          * open devname, to mmap it
72                          */
73                         fd = open(uio_res->maps[i].path, O_RDWR);
74                         if (fd < 0) {
75                                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
76                                         uio_res->maps[i].path, strerror(errno));
77                                 return -1;
78                         }
79
80                         void *mapaddr = pci_map_resource(uio_res->maps[i].addr,
81                                         fd, (off_t)uio_res->maps[i].offset,
82                                         (size_t)uio_res->maps[i].size, 0);
83                         /* fd is not needed in slave process, close it */
84                         close(fd);
85                         if (mapaddr != uio_res->maps[i].addr) {
86                                 RTE_LOG(ERR, EAL,
87                                         "Cannot mmap device resource file %s to address: %p\n",
88                                         uio_res->maps[i].path,
89                                         uio_res->maps[i].addr);
90                                 if (mapaddr != MAP_FAILED) {
91                                         /* unmap addrs correctly mapped */
92                                         for (j = 0; j < i; j++)
93                                                 pci_unmap_resource(
94                                                         uio_res->maps[j].addr,
95                                                         (size_t)uio_res->maps[j].size);
96                                         /* unmap addr wrongly mapped */
97                                         pci_unmap_resource(mapaddr,
98                                                 (size_t)uio_res->maps[i].size);
99                                 }
100                                 return -1;
101                         }
102                 }
103                 return 0;
104         }
105
106         RTE_LOG(ERR, EAL, "Cannot find resource for device\n");
107         return 1;
108 }
109
110 /* map the PCI resource of a PCI device in virtual memory */
111 int
112 pci_uio_map_resource(struct rte_pci_device *dev)
113 {
114         int i, map_idx = 0, ret;
115         uint64_t phaddr;
116         struct mapped_pci_resource *uio_res = NULL;
117         struct mapped_pci_res_list *uio_res_list =
118                 RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
119
120         dev->intr_handle.fd = -1;
121         dev->intr_handle.uio_cfg_fd = -1;
122
123         /* secondary processes - use already recorded details */
124         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
125                 return pci_uio_map_secondary(dev);
126
127         /* allocate uio resource */
128         ret = pci_uio_alloc_resource(dev, &uio_res);
129         if (ret)
130                 return ret;
131
132         /* Map all BARs */
133         for (i = 0; i != PCI_MAX_RESOURCE; i++) {
134                 /* skip empty BAR */
135                 phaddr = dev->mem_resource[i].phys_addr;
136                 if (phaddr == 0)
137                         continue;
138
139                 ret = pci_uio_map_resource_by_index(dev, i,
140                                 uio_res, map_idx);
141                 if (ret)
142                         goto error;
143
144                 map_idx++;
145         }
146
147         uio_res->nb_maps = map_idx;
148
149         TAILQ_INSERT_TAIL(uio_res_list, uio_res, next);
150
151         return 0;
152 error:
153         for (i = 0; i < map_idx; i++) {
154                 pci_unmap_resource(uio_res->maps[i].addr,
155                                 (size_t)uio_res->maps[i].size);
156                 rte_free(uio_res->maps[i].path);
157         }
158         pci_uio_free_resource(dev, uio_res);
159         return -1;
160 }
161
162 static void
163 pci_uio_unmap(struct mapped_pci_resource *uio_res)
164 {
165         int i;
166
167         if (uio_res == NULL)
168                 return;
169
170         for (i = 0; i != uio_res->nb_maps; i++) {
171                 pci_unmap_resource(uio_res->maps[i].addr,
172                                 (size_t)uio_res->maps[i].size);
173                 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
174                         rte_free(uio_res->maps[i].path);
175         }
176 }
177
178 static struct mapped_pci_resource *
179 pci_uio_find_resource(struct rte_pci_device *dev)
180 {
181         struct mapped_pci_resource *uio_res;
182         struct mapped_pci_res_list *uio_res_list =
183                         RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
184
185         if (dev == NULL)
186                 return NULL;
187
188         TAILQ_FOREACH(uio_res, uio_res_list, next) {
189
190                 /* skip this element if it doesn't match our PCI address */
191                 if (!rte_pci_addr_cmp(&uio_res->pci_addr, &dev->addr))
192                         return uio_res;
193         }
194         return NULL;
195 }
196
197 /* unmap the PCI resource of a PCI device in virtual memory */
198 void
199 pci_uio_unmap_resource(struct rte_pci_device *dev)
200 {
201         struct mapped_pci_resource *uio_res;
202         struct mapped_pci_res_list *uio_res_list =
203                         RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
204
205         if (dev == NULL)
206                 return;
207
208         /* find an entry for the device */
209         uio_res = pci_uio_find_resource(dev);
210         if (uio_res == NULL)
211                 return;
212
213         /* secondary processes - just free maps */
214         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
215                 return pci_uio_unmap(uio_res);
216
217         TAILQ_REMOVE(uio_res_list, uio_res, next);
218
219         /* unmap all resources */
220         pci_uio_unmap(uio_res);
221
222         /* free uio resource */
223         rte_free(uio_res);
224
225         /* close fd if in primary process */
226         close(dev->intr_handle.fd);
227         if (dev->intr_handle.uio_cfg_fd >= 0) {
228                 close(dev->intr_handle.uio_cfg_fd);
229                 dev->intr_handle.uio_cfg_fd = -1;
230         }
231
232         dev->intr_handle.fd = -1;
233         dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
234 }