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 "rte_pci_dev_ids.h"
49 #include "eal_filesystem.h"
50 #include "eal_pci_init.h"
52 void *pci_map_addr = NULL;
54 static struct rte_tailq_elem rte_uio_tailq = {
55 .name = "UIO_RESOURCE_LIST",
57 EAL_REGISTER_TAILQ(rte_uio_tailq)
59 #define OFF_MAX ((uint64_t)(off_t)-1)
62 pci_uio_set_bus_master(int dev_fd)
67 ret = pread(dev_fd, ®, sizeof(reg), PCI_COMMAND);
68 if (ret != sizeof(reg)) {
70 "Cannot read command from PCI config space!\n");
74 /* return if bus mastering is already on */
75 if (reg & PCI_COMMAND_MASTER)
78 reg |= PCI_COMMAND_MASTER;
80 ret = pwrite(dev_fd, ®, sizeof(reg), PCI_COMMAND);
81 if (ret != sizeof(reg)) {
83 "Cannot write command to PCI config space!\n");
91 pci_uio_map_secondary(struct rte_pci_device *dev)
94 struct mapped_pci_resource *uio_res;
95 struct mapped_pci_res_list *uio_res_list =
96 RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
98 TAILQ_FOREACH(uio_res, uio_res_list, next) {
100 /* skip this element if it doesn't match our PCI address */
101 if (rte_eal_compare_pci_addr(&uio_res->pci_addr, &dev->addr))
104 for (i = 0; i != uio_res->nb_maps; i++) {
106 * open devname, to mmap it
108 fd = open(uio_res->maps[i].path, O_RDWR);
110 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
111 uio_res->maps[i].path, strerror(errno));
115 void *mapaddr = pci_map_resource(uio_res->maps[i].addr,
116 fd, (off_t)uio_res->maps[i].offset,
117 (size_t)uio_res->maps[i].size, 0);
118 /* fd is not needed in slave process, close it */
120 if (mapaddr != uio_res->maps[i].addr) {
122 "Cannot mmap device resource file %s to address: %p\n",
123 uio_res->maps[i].path,
124 uio_res->maps[i].addr);
131 RTE_LOG(ERR, EAL, "Cannot find resource for device\n");
136 pci_mknod_uio_dev(const char *sysfs_uio_path, unsigned uio_num)
139 char filename[PATH_MAX];
141 unsigned major, minor;
144 /* get the name of the sysfs file that contains the major and minor
145 * of the uio device and read its content */
146 snprintf(filename, sizeof(filename), "%s/dev", sysfs_uio_path);
148 f = fopen(filename, "r");
150 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs to get major:minor\n",
155 ret = fscanf(f, "%u:%u", &major, &minor);
157 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs to get major:minor\n",
164 /* create the char device "mknod /dev/uioX c major minor" */
165 snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
166 dev = makedev(major, minor);
167 ret = mknod(filename, S_IFCHR | S_IRUSR | S_IWUSR, dev);
169 RTE_LOG(ERR, EAL, "%s(): mknod() failed %s\n",
170 __func__, strerror(errno));
178 * Return the uioX char device used for a pci device. On success, return
179 * the UIO number and fill dstbuf string with the path of the device in
180 * sysfs. On error, return a negative value. In this case dstbuf is
184 pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf,
187 struct rte_pci_addr *loc = &dev->addr;
188 unsigned int uio_num;
191 char dirname[PATH_MAX];
193 /* depending on kernel version, uio can be located in uio/uioX
196 snprintf(dirname, sizeof(dirname),
197 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/uio",
198 loc->domain, loc->bus, loc->devid, loc->function);
200 dir = opendir(dirname);
202 /* retry with the parent directory */
203 snprintf(dirname, sizeof(dirname),
204 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT,
205 loc->domain, loc->bus, loc->devid, loc->function);
206 dir = opendir(dirname);
209 RTE_LOG(ERR, EAL, "Cannot opendir %s\n", dirname);
214 /* take the first file starting with "uio" */
215 while ((e = readdir(dir)) != NULL) {
216 /* format could be uio%d ...*/
217 int shortprefix_len = sizeof("uio") - 1;
218 /* ... or uio:uio%d */
219 int longprefix_len = sizeof("uio:uio") - 1;
222 if (strncmp(e->d_name, "uio", 3) != 0)
225 /* first try uio%d */
227 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
228 if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
229 snprintf(dstbuf, buflen, "%s/uio%u", dirname, uio_num);
233 /* then try uio:uio%d */
235 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
236 if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
237 snprintf(dstbuf, buflen, "%s/uio:uio%u", dirname, uio_num);
243 /* No uio resource found */
247 /* create uio device if we've been asked to */
248 if (internal_config.create_uio_dev &&
249 pci_mknod_uio_dev(dstbuf, uio_num) < 0)
250 RTE_LOG(WARNING, EAL, "Cannot create /dev/uio%u\n", uio_num);
256 pci_uio_free_resource(struct rte_pci_device *dev,
257 struct mapped_pci_resource *uio_res)
261 if (dev->intr_handle.uio_cfg_fd >= 0) {
262 close(dev->intr_handle.uio_cfg_fd);
263 dev->intr_handle.uio_cfg_fd = -1;
265 if (dev->intr_handle.fd) {
266 close(dev->intr_handle.fd);
267 dev->intr_handle.fd = -1;
268 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
273 pci_uio_alloc_resource(struct rte_pci_device *dev,
274 struct mapped_pci_resource **uio_res)
276 char dirname[PATH_MAX];
277 char cfgname[PATH_MAX];
278 char devname[PATH_MAX]; /* contains the /dev/uioX */
280 struct rte_pci_addr *loc;
284 /* find uio resource */
285 uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname));
287 RTE_LOG(WARNING, EAL, " "PCI_PRI_FMT" not managed by UIO driver, "
288 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
291 snprintf(devname, sizeof(devname), "/dev/uio%u", uio_num);
293 /* save fd if in primary process */
294 dev->intr_handle.fd = open(devname, O_RDWR);
295 if (dev->intr_handle.fd < 0) {
296 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
297 devname, strerror(errno));
301 snprintf(cfgname, sizeof(cfgname),
302 "/sys/class/uio/uio%u/device/config", uio_num);
303 dev->intr_handle.uio_cfg_fd = open(cfgname, O_RDWR);
304 if (dev->intr_handle.uio_cfg_fd < 0) {
305 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
306 cfgname, strerror(errno));
310 if (dev->kdrv == RTE_KDRV_IGB_UIO)
311 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
313 dev->intr_handle.type = RTE_INTR_HANDLE_UIO_INTX;
315 /* set bus master that is not done by uio_pci_generic */
316 if (pci_uio_set_bus_master(dev->intr_handle.uio_cfg_fd)) {
317 RTE_LOG(ERR, EAL, "Cannot set up bus mastering!\n");
322 /* allocate the mapping details for secondary processes*/
323 *uio_res = rte_zmalloc("UIO_RES", sizeof(**uio_res), 0);
324 if (*uio_res == NULL) {
326 "%s(): cannot store uio mmap details\n", __func__);
330 snprintf((*uio_res)->path, sizeof((*uio_res)->path), "%s", devname);
331 memcpy(&(*uio_res)->pci_addr, &dev->addr, sizeof((*uio_res)->pci_addr));
336 pci_uio_free_resource(dev, *uio_res);
340 /* map the PCI resource of a PCI device in virtual memory */
342 pci_uio_map_resource(struct rte_pci_device *dev)
344 int i, map_idx = 0, ret;
345 char devname[PATH_MAX]; /* contains the /dev/uioX */
348 struct rte_pci_addr *loc = &dev->addr;
349 struct mapped_pci_resource *uio_res = NULL;
350 struct mapped_pci_res_list *uio_res_list =
351 RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
352 struct pci_map *maps;
354 dev->intr_handle.fd = -1;
355 dev->intr_handle.uio_cfg_fd = -1;
356 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
358 /* secondary processes - use already recorded details */
359 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
360 return pci_uio_map_secondary(dev);
362 /* allocate uio resource */
363 ret = pci_uio_alloc_resource(dev, &uio_res);
368 maps = uio_res->maps;
369 for (i = 0; i != PCI_MAX_RESOURCE; i++) {
373 phaddr = dev->mem_resource[i].phys_addr;
378 /* update devname for mmap */
379 snprintf(devname, sizeof(devname),
380 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/resource%d",
381 loc->domain, loc->bus, loc->devid, loc->function,
384 /* allocate memory to keep path */
385 maps[map_idx].path = rte_malloc(NULL, strlen(devname) + 1, 0);
386 if (maps[map_idx].path == NULL) {
387 RTE_LOG(ERR, EAL, "Cannot allocate memory for path: %s\n",
393 * open resource file, to mmap it
395 fd = open(devname, O_RDWR);
397 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
398 devname, strerror(errno));
399 rte_free(maps[map_idx].path);
403 /* try mapping somewhere close to the end of hugepages */
404 if (pci_map_addr == NULL)
405 pci_map_addr = pci_find_max_end_va();
407 mapaddr = pci_map_resource(pci_map_addr, fd, 0,
408 (size_t)dev->mem_resource[i].len, 0);
410 if (mapaddr == MAP_FAILED) {
411 rte_free(maps[map_idx].path);
415 pci_map_addr = RTE_PTR_ADD(mapaddr,
416 (size_t)dev->mem_resource[i].len);
418 maps[map_idx].phaddr = dev->mem_resource[i].phys_addr;
419 maps[map_idx].size = dev->mem_resource[i].len;
420 maps[map_idx].addr = mapaddr;
421 maps[map_idx].offset = 0;
422 strcpy(maps[map_idx].path, devname);
424 dev->mem_resource[i].addr = mapaddr;
427 uio_res->nb_maps = map_idx;
429 TAILQ_INSERT_TAIL(uio_res_list, uio_res, next);
434 for (i = 0; i < map_idx; i++) {
435 pci_unmap_resource(uio_res->maps[i].addr,
436 (size_t)uio_res->maps[i].size);
437 rte_free(maps[i].path);
439 pci_uio_free_resource(dev, uio_res);
443 #ifdef RTE_LIBRTE_EAL_HOTPLUG
445 pci_uio_unmap(struct mapped_pci_resource *uio_res)
452 for (i = 0; i != uio_res->nb_maps; i++) {
453 pci_unmap_resource(uio_res->maps[i].addr,
454 (size_t)uio_res->maps[i].size);
455 rte_free(uio_res->maps[i].path);
459 static struct mapped_pci_resource *
460 pci_uio_find_resource(struct rte_pci_device *dev)
462 struct mapped_pci_resource *uio_res;
463 struct mapped_pci_res_list *uio_res_list =
464 RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
469 TAILQ_FOREACH(uio_res, uio_res_list, next) {
471 /* skip this element if it doesn't match our PCI address */
472 if (!rte_eal_compare_pci_addr(&uio_res->pci_addr, &dev->addr))
478 /* unmap the PCI resource of a PCI device in virtual memory */
480 pci_uio_unmap_resource(struct rte_pci_device *dev)
482 struct mapped_pci_resource *uio_res;
483 struct mapped_pci_res_list *uio_res_list =
484 RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
489 /* find an entry for the device */
490 uio_res = pci_uio_find_resource(dev);
494 /* secondary processes - just free maps */
495 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
496 return pci_uio_unmap(uio_res);
498 TAILQ_REMOVE(uio_res_list, uio_res, next);
500 /* unmap all resources */
501 pci_uio_unmap(uio_res);
503 /* free uio resource */
506 /* close fd if in primary process */
507 close(dev->intr_handle.fd);
508 dev->intr_handle.fd = -1;
510 /* close cfg_fd if in primary process */
511 close(dev->intr_handle.uio_cfg_fd);
512 dev->intr_handle.uio_cfg_fd = -1;
514 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
516 #endif /* RTE_LIBRTE_EAL_HOTPLUG */