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;
115 pci_uio_map_secondary(struct rte_pci_device *dev)
118 struct mapped_pci_resource *uio_res;
120 TAILQ_FOREACH(uio_res, pci_res_list, next) {
122 /* skip this element if it doesn't match our PCI address */
123 if (memcmp(&uio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
126 for (i = 0; i != uio_res->nb_maps; i++) {
128 * open devname, to mmap it
130 fd = open(uio_res->path, O_RDWR);
132 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
133 uio_res->path, strerror(errno));
137 if (pci_map_resource(uio_res->maps[i].addr, fd,
138 (off_t)uio_res->maps[i].offset,
139 (size_t)uio_res->maps[i].size)
140 != uio_res->maps[i].addr) {
142 "Cannot mmap device resource\n");
146 /* fd is not needed in slave process, close it */
152 RTE_LOG(ERR, EAL, "Cannot find resource for device\n");
157 pci_mknod_uio_dev(const char *sysfs_uio_path, unsigned uio_num)
160 char filename[PATH_MAX];
162 unsigned major, minor;
165 /* get the name of the sysfs file that contains the major and minor
166 * of the uio device and read its content */
167 rte_snprintf(filename, sizeof(filename), "%s/dev", sysfs_uio_path);
169 f = fopen(filename, "r");
171 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs to get major:minor\n",
176 ret = fscanf(f, "%d:%d", &major, &minor);
178 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs to get major:minor\n",
185 /* create the char device "mknod /dev/uioX c major minor" */
186 rte_snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
187 dev = makedev(major, minor);
188 ret = mknod(filename, S_IFCHR | S_IRUSR | S_IWUSR, dev);
190 RTE_LOG(ERR, EAL, "%s(): mknod() failed %s\n",
191 __func__, strerror(errno));
199 * Return the uioX char device used for a pci device. On success, return
200 * the UIO number and fill dstbuf string with the path of the device in
201 * sysfs. On error, return a negative value. In this case dstbuf is
205 pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf,
208 struct rte_pci_addr *loc = &dev->addr;
209 unsigned int uio_num;
212 char dirname[PATH_MAX];
214 /* depending on kernel version, uio can be located in uio/uioX
217 rte_snprintf(dirname, sizeof(dirname),
218 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/uio",
219 loc->domain, loc->bus, loc->devid, loc->function);
221 dir = opendir(dirname);
223 /* retry with the parent directory */
224 rte_snprintf(dirname, sizeof(dirname),
225 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT,
226 loc->domain, loc->bus, loc->devid, loc->function);
227 dir = opendir(dirname);
230 RTE_LOG(ERR, EAL, "Cannot opendir %s\n", dirname);
235 /* take the first file starting with "uio" */
236 while ((e = readdir(dir)) != NULL) {
237 /* format could be uio%d ...*/
238 int shortprefix_len = sizeof("uio") - 1;
239 /* ... or uio:uio%d */
240 int longprefix_len = sizeof("uio:uio") - 1;
243 if (strncmp(e->d_name, "uio", 3) != 0)
246 /* first try uio%d */
248 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
249 if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
250 rte_snprintf(dstbuf, buflen, "%s/uio%u", dirname, uio_num);
254 /* then try uio:uio%d */
256 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
257 if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
258 rte_snprintf(dstbuf, buflen, "%s/uio:uio%u", dirname, uio_num);
264 /* No uio resource found */
268 /* create uio device if we've been asked to */
269 if (internal_config.create_uio_dev &&
270 pci_mknod_uio_dev(dstbuf, uio_num) < 0)
271 RTE_LOG(WARNING, EAL, "Cannot create /dev/uio%u\n", uio_num);
276 /* map the PCI resource of a PCI device in virtual memory */
278 pci_uio_map_resource(struct rte_pci_device *dev)
281 char dirname[PATH_MAX];
282 char devname[PATH_MAX]; /* contains the /dev/uioX */
289 struct rte_pci_addr *loc = &dev->addr;
290 struct mapped_pci_resource *uio_res;
291 struct pci_map *maps;
293 dev->intr_handle.fd = -1;
294 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
296 /* secondary processes - use already recorded details */
297 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
298 return pci_uio_map_secondary(dev);
300 /* find uio resource */
301 uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname));
303 RTE_LOG(WARNING, EAL, " "PCI_PRI_FMT" not managed by UIO driver, "
304 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
307 rte_snprintf(devname, sizeof(devname), "/dev/uio%u", uio_num);
309 /* save fd if in primary process */
310 dev->intr_handle.fd = open(devname, O_RDWR);
311 if (dev->intr_handle.fd < 0) {
312 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
313 devname, strerror(errno));
316 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
318 /* allocate the mapping details for secondary processes*/
319 uio_res = rte_zmalloc("UIO_RES", sizeof(*uio_res), 0);
320 if (uio_res == NULL) {
322 "%s(): cannot store uio mmap details\n", __func__);
326 rte_snprintf(uio_res->path, sizeof(uio_res->path), "%s", devname);
327 memcpy(&uio_res->pci_addr, &dev->addr, sizeof(uio_res->pci_addr));
329 /* collect info about device mappings */
330 nb_maps = pci_uio_get_mappings(dirname, uio_res->maps,
331 RTE_DIM(uio_res->maps));
337 uio_res->nb_maps = nb_maps;
340 pagesz = sysconf(_SC_PAGESIZE);
342 maps = uio_res->maps;
343 for (i = 0; i != PCI_MAX_RESOURCE; i++) {
347 phaddr = dev->mem_resource[i].phys_addr;
351 for (j = 0; j != nb_maps && (phaddr != maps[j].phaddr ||
352 dev->mem_resource[i].len != maps[j].size);
356 /* if matching map is found, then use it */
362 * open devname, to mmap it
364 fd = open(devname, O_RDWR);
366 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
367 devname, strerror(errno));
371 if (maps[j].addr != NULL)
374 mapaddr = pci_map_resource(NULL, fd, (off_t)offset,
375 (size_t)maps[j].size);
387 maps[j].addr = mapaddr;
388 maps[j].offset = offset;
389 dev->mem_resource[i].addr = mapaddr;
393 TAILQ_INSERT_TAIL(pci_res_list, uio_res, next);
399 * parse a sysfs file containing one integer value
400 * different to the eal version, as it needs to work with 64-bit values
403 pci_parse_sysfs_value(const char *filename, uint64_t *val)
409 f = fopen(filename, "r");
411 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
416 if (fgets(buf, sizeof(buf), f) == NULL) {
417 RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
422 *val = strtoull(buf, &end, 0);
423 if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
424 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",