4 * Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
37 #include <sys/types.h>
42 #include <rte_tailq.h>
44 #include <rte_malloc.h>
46 #include "eal_private.h"
48 static struct rte_tailq_elem rte_uio_tailq = {
49 .name = "UIO_RESOURCE_LIST",
51 EAL_REGISTER_TAILQ(rte_uio_tailq)
54 pci_uio_map_secondary(struct rte_pci_device *dev)
57 struct mapped_pci_resource *uio_res;
58 struct mapped_pci_res_list *uio_res_list =
59 RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
61 TAILQ_FOREACH(uio_res, uio_res_list, next) {
63 /* skip this element if it doesn't match our PCI address */
64 if (rte_eal_compare_pci_addr(&uio_res->pci_addr, &dev->addr))
67 for (i = 0; i != uio_res->nb_maps; i++) {
69 * open devname, to mmap it
71 fd = open(uio_res->maps[i].path, O_RDWR);
73 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
74 uio_res->maps[i].path, strerror(errno));
78 void *mapaddr = pci_map_resource(uio_res->maps[i].addr,
79 fd, (off_t)uio_res->maps[i].offset,
80 (size_t)uio_res->maps[i].size, 0);
81 /* fd is not needed in slave process, close it */
83 if (mapaddr != uio_res->maps[i].addr) {
85 "Cannot mmap device resource file %s to address: %p\n",
86 uio_res->maps[i].path,
87 uio_res->maps[i].addr);
94 RTE_LOG(ERR, EAL, "Cannot find resource for device\n");
98 /* map the PCI resource of a PCI device in virtual memory */
100 pci_uio_map_resource(struct rte_pci_device *dev)
102 int i, map_idx = 0, ret;
104 struct mapped_pci_resource *uio_res = NULL;
105 struct mapped_pci_res_list *uio_res_list =
106 RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
108 dev->intr_handle.fd = -1;
109 dev->intr_handle.uio_cfg_fd = -1;
110 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
112 /* secondary processes - use already recorded details */
113 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
114 return pci_uio_map_secondary(dev);
116 /* allocate uio resource */
117 ret = pci_uio_alloc_resource(dev, &uio_res);
122 for (i = 0; i != PCI_MAX_RESOURCE; i++) {
124 phaddr = dev->mem_resource[i].phys_addr;
128 ret = pci_uio_map_resource_by_index(dev, i,
136 uio_res->nb_maps = map_idx;
138 TAILQ_INSERT_TAIL(uio_res_list, uio_res, next);
142 for (i = 0; i < map_idx; i++) {
143 pci_unmap_resource(uio_res->maps[i].addr,
144 (size_t)uio_res->maps[i].size);
145 rte_free(uio_res->maps[i].path);
147 pci_uio_free_resource(dev, uio_res);
152 pci_uio_unmap(struct mapped_pci_resource *uio_res)
159 for (i = 0; i != uio_res->nb_maps; i++) {
160 pci_unmap_resource(uio_res->maps[i].addr,
161 (size_t)uio_res->maps[i].size);
162 rte_free(uio_res->maps[i].path);
166 static struct mapped_pci_resource *
167 pci_uio_find_resource(struct rte_pci_device *dev)
169 struct mapped_pci_resource *uio_res;
170 struct mapped_pci_res_list *uio_res_list =
171 RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
176 TAILQ_FOREACH(uio_res, uio_res_list, next) {
178 /* skip this element if it doesn't match our PCI address */
179 if (!rte_eal_compare_pci_addr(&uio_res->pci_addr, &dev->addr))
185 /* unmap the PCI resource of a PCI device in virtual memory */
187 pci_uio_unmap_resource(struct rte_pci_device *dev)
189 struct mapped_pci_resource *uio_res;
190 struct mapped_pci_res_list *uio_res_list =
191 RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
196 /* find an entry for the device */
197 uio_res = pci_uio_find_resource(dev);
201 /* secondary processes - just free maps */
202 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
203 return pci_uio_unmap(uio_res);
205 TAILQ_REMOVE(uio_res_list, uio_res, next);
207 /* unmap all resources */
208 pci_uio_unmap(uio_res);
210 /* free uio resource */
213 /* close fd if in primary process */
214 close(dev->intr_handle.fd);
215 if (dev->intr_handle.uio_cfg_fd >= 0) {
216 close(dev->intr_handle.uio_cfg_fd);
217 dev->intr_handle.uio_cfg_fd = -1;
220 dev->intr_handle.fd = -1;
221 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;