4 * Copyright(c) 2010-2014 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.
40 #include <linux/pci_regs.h>
44 #include <rte_eal_memconfig.h>
45 #include <rte_common.h>
46 #include <rte_malloc.h>
48 #include "eal_filesystem.h"
49 #include "eal_pci_init.h"
51 void *pci_map_addr = NULL;
53 #define OFF_MAX ((uint64_t)(off_t)-1)
56 pci_uio_set_bus_master(int dev_fd)
61 ret = pread(dev_fd, ®, sizeof(reg), PCI_COMMAND);
62 if (ret != sizeof(reg)) {
64 "Cannot read command from PCI config space!\n");
68 /* return if bus mastering is already on */
69 if (reg & PCI_COMMAND_MASTER)
72 reg |= PCI_COMMAND_MASTER;
74 ret = pwrite(dev_fd, ®, sizeof(reg), PCI_COMMAND);
75 if (ret != sizeof(reg)) {
77 "Cannot write command to PCI config space!\n");
85 pci_mknod_uio_dev(const char *sysfs_uio_path, unsigned uio_num)
88 char filename[PATH_MAX];
90 unsigned major, minor;
93 /* get the name of the sysfs file that contains the major and minor
94 * of the uio device and read its content */
95 snprintf(filename, sizeof(filename), "%s/dev", sysfs_uio_path);
97 f = fopen(filename, "r");
99 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs to get major:minor\n",
104 ret = fscanf(f, "%u:%u", &major, &minor);
106 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs to get major:minor\n",
113 /* create the char device "mknod /dev/uioX c major minor" */
114 snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
115 dev = makedev(major, minor);
116 ret = mknod(filename, S_IFCHR | S_IRUSR | S_IWUSR, dev);
118 RTE_LOG(ERR, EAL, "%s(): mknod() failed %s\n",
119 __func__, strerror(errno));
127 * Return the uioX char device used for a pci device. On success, return
128 * the UIO number and fill dstbuf string with the path of the device in
129 * sysfs. On error, return a negative value. In this case dstbuf is
133 pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf,
136 struct rte_pci_addr *loc = &dev->addr;
137 unsigned int uio_num;
140 char dirname[PATH_MAX];
142 /* depending on kernel version, uio can be located in uio/uioX
145 snprintf(dirname, sizeof(dirname),
146 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/uio",
147 loc->domain, loc->bus, loc->devid, loc->function);
149 dir = opendir(dirname);
151 /* retry with the parent directory */
152 snprintf(dirname, sizeof(dirname),
153 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT,
154 loc->domain, loc->bus, loc->devid, loc->function);
155 dir = opendir(dirname);
158 RTE_LOG(ERR, EAL, "Cannot opendir %s\n", dirname);
163 /* take the first file starting with "uio" */
164 while ((e = readdir(dir)) != NULL) {
165 /* format could be uio%d ...*/
166 int shortprefix_len = sizeof("uio") - 1;
167 /* ... or uio:uio%d */
168 int longprefix_len = sizeof("uio:uio") - 1;
171 if (strncmp(e->d_name, "uio", 3) != 0)
174 /* first try uio%d */
176 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
177 if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
178 snprintf(dstbuf, buflen, "%s/uio%u", dirname, uio_num);
182 /* then try uio:uio%d */
184 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
185 if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
186 snprintf(dstbuf, buflen, "%s/uio:uio%u", dirname, uio_num);
192 /* No uio resource found */
196 /* create uio device if we've been asked to */
197 if (internal_config.create_uio_dev &&
198 pci_mknod_uio_dev(dstbuf, uio_num) < 0)
199 RTE_LOG(WARNING, EAL, "Cannot create /dev/uio%u\n", uio_num);
205 pci_uio_free_resource(struct rte_pci_device *dev,
206 struct mapped_pci_resource *uio_res)
210 if (dev->intr_handle.uio_cfg_fd >= 0) {
211 close(dev->intr_handle.uio_cfg_fd);
212 dev->intr_handle.uio_cfg_fd = -1;
214 if (dev->intr_handle.fd) {
215 close(dev->intr_handle.fd);
216 dev->intr_handle.fd = -1;
217 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
222 pci_uio_alloc_resource(struct rte_pci_device *dev,
223 struct mapped_pci_resource **uio_res)
225 char dirname[PATH_MAX];
226 char cfgname[PATH_MAX];
227 char devname[PATH_MAX]; /* contains the /dev/uioX */
229 struct rte_pci_addr *loc;
233 /* find uio resource */
234 uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname));
236 RTE_LOG(WARNING, EAL, " "PCI_PRI_FMT" not managed by UIO driver, "
237 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
240 snprintf(devname, sizeof(devname), "/dev/uio%u", uio_num);
242 /* save fd if in primary process */
243 dev->intr_handle.fd = open(devname, O_RDWR);
244 if (dev->intr_handle.fd < 0) {
245 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
246 devname, strerror(errno));
250 snprintf(cfgname, sizeof(cfgname),
251 "/sys/class/uio/uio%u/device/config", uio_num);
252 dev->intr_handle.uio_cfg_fd = open(cfgname, O_RDWR);
253 if (dev->intr_handle.uio_cfg_fd < 0) {
254 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
255 cfgname, strerror(errno));
259 if (dev->kdrv == RTE_KDRV_IGB_UIO)
260 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
262 dev->intr_handle.type = RTE_INTR_HANDLE_UIO_INTX;
264 /* set bus master that is not done by uio_pci_generic */
265 if (pci_uio_set_bus_master(dev->intr_handle.uio_cfg_fd)) {
266 RTE_LOG(ERR, EAL, "Cannot set up bus mastering!\n");
271 /* allocate the mapping details for secondary processes*/
272 *uio_res = rte_zmalloc("UIO_RES", sizeof(**uio_res), 0);
273 if (*uio_res == NULL) {
275 "%s(): cannot store uio mmap details\n", __func__);
279 snprintf((*uio_res)->path, sizeof((*uio_res)->path), "%s", devname);
280 memcpy(&(*uio_res)->pci_addr, &dev->addr, sizeof((*uio_res)->pci_addr));
285 pci_uio_free_resource(dev, *uio_res);
290 pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
291 struct mapped_pci_resource *uio_res, int map_idx)
294 char devname[PATH_MAX]; /* contains the /dev/uioX */
296 struct rte_pci_addr *loc;
297 struct pci_map *maps;
300 maps = uio_res->maps;
302 /* update devname for mmap */
303 snprintf(devname, sizeof(devname),
304 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/resource%d",
305 loc->domain, loc->bus, loc->devid,
306 loc->function, res_idx);
308 /* allocate memory to keep path */
309 maps[map_idx].path = rte_malloc(NULL, strlen(devname) + 1, 0);
310 if (maps[map_idx].path == NULL) {
311 RTE_LOG(ERR, EAL, "Cannot allocate memory for path: %s\n",
317 * open resource file, to mmap it
319 fd = open(devname, O_RDWR);
321 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
322 devname, strerror(errno));
326 /* try mapping somewhere close to the end of hugepages */
327 if (pci_map_addr == NULL)
328 pci_map_addr = pci_find_max_end_va();
330 mapaddr = pci_map_resource(pci_map_addr, fd, 0,
331 (size_t)dev->mem_resource[res_idx].len, 0);
333 if (mapaddr == MAP_FAILED)
336 pci_map_addr = RTE_PTR_ADD(mapaddr,
337 (size_t)dev->mem_resource[res_idx].len);
339 maps[map_idx].phaddr = dev->mem_resource[res_idx].phys_addr;
340 maps[map_idx].size = dev->mem_resource[res_idx].len;
341 maps[map_idx].addr = mapaddr;
342 maps[map_idx].offset = 0;
343 strcpy(maps[map_idx].path, devname);
344 dev->mem_resource[res_idx].addr = mapaddr;
349 rte_free(maps[map_idx].path);