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.
41 #include <rte_common.h>
42 #include <rte_malloc.h>
43 #include <rte_tailq.h>
45 #include "rte_pci_dev_ids.h"
46 #include "eal_filesystem.h"
47 #include "eal_pci_init.h"
49 static int pci_parse_sysfs_value(const char *filename, uint64_t *val);
52 #define OFF_MAX ((uint64_t)(off_t)-1)
54 pci_uio_get_mappings(const char *devname, struct pci_map maps[], int nb_maps)
57 char dirname[PATH_MAX];
58 char filename[PATH_MAX];
59 uint64_t offset, size;
61 for (i = 0; i != nb_maps; i++) {
63 /* check if map directory exists */
64 rte_snprintf(dirname, sizeof(dirname),
65 "%s/maps/map%u", devname, i);
67 if (access(dirname, F_OK) != 0)
70 /* get mapping offset */
71 rte_snprintf(filename, sizeof(filename),
72 "%s/offset", dirname);
73 if (pci_parse_sysfs_value(filename, &offset) < 0) {
75 "%s(): cannot parse offset of %s\n",
80 /* get mapping size */
81 rte_snprintf(filename, sizeof(filename),
83 if (pci_parse_sysfs_value(filename, &size) < 0) {
85 "%s(): cannot parse size of %s\n",
90 /* get mapping physical address */
91 rte_snprintf(filename, sizeof(filename),
93 if (pci_parse_sysfs_value(filename, &maps[i].phaddr) < 0) {
95 "%s(): cannot parse addr of %s\n",
100 if ((offset > OFF_MAX) || (size > SIZE_MAX)) {
102 "%s(): offset/size exceed system max value\n",
107 maps[i].offset = offset;
114 pci_uio_map_secondary(struct rte_pci_device *dev)
117 struct mapped_pci_resource *uio_res;
119 TAILQ_FOREACH(uio_res, pci_res_list, next) {
121 /* skip this element if it doesn't match our PCI address */
122 if (memcmp(&uio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
125 for (i = 0; i != uio_res->nb_maps; i++) {
127 * open devname, to mmap it
129 fd = open(uio_res->path, O_RDWR);
131 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
132 uio_res->path, strerror(errno));
136 if (pci_map_resource(uio_res->maps[i].addr, fd,
137 (off_t)uio_res->maps[i].offset,
138 (size_t)uio_res->maps[i].size)
139 != uio_res->maps[i].addr) {
141 "Cannot mmap device resource\n");
145 /* fd is not needed in slave process, close it */
151 RTE_LOG(ERR, EAL, "Cannot find resource for device\n");
156 pci_mknod_uio_dev(const char *sysfs_uio_path, unsigned uio_num)
159 char filename[PATH_MAX];
161 unsigned major, minor;
164 /* get the name of the sysfs file that contains the major and minor
165 * of the uio device and read its content */
166 rte_snprintf(filename, sizeof(filename), "%s/dev", sysfs_uio_path);
168 f = fopen(filename, "r");
170 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs to get major:minor\n",
175 ret = fscanf(f, "%d:%d", &major, &minor);
177 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs to get major:minor\n",
184 /* create the char device "mknod /dev/uioX c major minor" */
185 rte_snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
186 dev = makedev(major, minor);
187 ret = mknod(filename, S_IFCHR | S_IRUSR | S_IWUSR, dev);
189 RTE_LOG(ERR, EAL, "%s(): mknod() failed %s\n",
190 __func__, strerror(errno));
198 * Return the uioX char device used for a pci device. On success, return
199 * the UIO number and fill dstbuf string with the path of the device in
200 * sysfs. On error, return a negative value. In this case dstbuf is
204 pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf,
207 struct rte_pci_addr *loc = &dev->addr;
208 unsigned int uio_num;
211 char dirname[PATH_MAX];
213 /* depending on kernel version, uio can be located in uio/uioX
216 rte_snprintf(dirname, sizeof(dirname),
217 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/uio",
218 loc->domain, loc->bus, loc->devid, loc->function);
220 dir = opendir(dirname);
222 /* retry with the parent directory */
223 rte_snprintf(dirname, sizeof(dirname),
224 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT,
225 loc->domain, loc->bus, loc->devid, loc->function);
226 dir = opendir(dirname);
229 RTE_LOG(ERR, EAL, "Cannot opendir %s\n", dirname);
234 /* take the first file starting with "uio" */
235 while ((e = readdir(dir)) != NULL) {
236 /* format could be uio%d ...*/
237 int shortprefix_len = sizeof("uio") - 1;
238 /* ... or uio:uio%d */
239 int longprefix_len = sizeof("uio:uio") - 1;
242 if (strncmp(e->d_name, "uio", 3) != 0)
245 /* first try uio%d */
247 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
248 if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
249 rte_snprintf(dstbuf, buflen, "%s/uio%u", dirname, uio_num);
253 /* then try uio:uio%d */
255 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
256 if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
257 rte_snprintf(dstbuf, buflen, "%s/uio:uio%u", dirname, uio_num);
263 /* No uio resource found */
267 /* create uio device if we've been asked to */
268 if (internal_config.create_uio_dev && pci_mknod_uio_dev(dstbuf, uio_num) < 0)
269 RTE_LOG(WARNING, EAL, "Cannot create /dev/uio%u\n", uio_num);
274 /* map the PCI resource of a PCI device in virtual memory */
276 pci_uio_map_resource(struct rte_pci_device *dev)
279 char dirname[PATH_MAX];
280 char devname[PATH_MAX]; /* contains the /dev/uioX */
287 struct rte_pci_addr *loc = &dev->addr;
288 struct mapped_pci_resource *uio_res;
289 struct pci_map *maps;
291 dev->intr_handle.fd = -1;
292 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
294 /* secondary processes - use already recorded details */
295 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
296 return (pci_uio_map_secondary(dev));
298 /* find uio resource */
299 uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname));
301 RTE_LOG(WARNING, EAL, " "PCI_PRI_FMT" not managed by UIO driver, "
302 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
305 rte_snprintf(devname, sizeof(devname), "/dev/uio%u", uio_num);
307 /* save fd if in primary process */
308 dev->intr_handle.fd = open(devname, O_RDWR);
309 if (dev->intr_handle.fd < 0) {
310 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
311 devname, strerror(errno));
314 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
316 /* allocate the mapping details for secondary processes*/
317 if ((uio_res = rte_zmalloc("UIO_RES", sizeof (*uio_res), 0)) == NULL) {
319 "%s(): cannot store uio mmap details\n", __func__);
323 rte_snprintf(uio_res->path, sizeof(uio_res->path), "%s", devname);
324 memcpy(&uio_res->pci_addr, &dev->addr, sizeof(uio_res->pci_addr));
326 /* collect info about device mappings */
327 nb_maps = pci_uio_get_mappings(dirname, uio_res->maps,
328 RTE_DIM(uio_res->maps));
334 uio_res->nb_maps = nb_maps;
337 pagesz = sysconf(_SC_PAGESIZE);
339 maps = uio_res->maps;
340 for (i = 0; i != PCI_MAX_RESOURCE; i++) {
344 if ((phaddr = dev->mem_resource[i].phys_addr) == 0)
347 for (j = 0; j != nb_maps && (phaddr != maps[j].phaddr ||
348 dev->mem_resource[i].len != maps[j].size);
352 /* if matching map is found, then use it */
357 * open devname, to mmap it
359 fd = open(devname, O_RDWR);
361 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
362 devname, strerror(errno));
366 if (maps[j].addr != NULL ||
367 (mapaddr = pci_map_resource(NULL, fd,
369 (size_t)maps[j].size)
377 maps[j].addr = mapaddr;
378 maps[j].offset = offset;
379 dev->mem_resource[i].addr = mapaddr;
383 TAILQ_INSERT_TAIL(pci_res_list, uio_res, next);
389 * parse a sysfs file containing one integer value
390 * different to the eal version, as it needs to work with 64-bit values
393 pci_parse_sysfs_value(const char *filename, uint64_t *val)
399 f = fopen(filename, "r");
401 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
406 if (fgets(buf, sizeof(buf), f) == NULL) {
407 RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
412 *val = strtoull(buf, &end, 0);
413 if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
414 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",