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.
42 #include <rte_common.h>
43 #include <rte_malloc.h>
44 #include <rte_tailq.h>
46 #include "rte_pci_dev_ids.h"
47 #include "eal_filesystem.h"
48 #include "eal_pci_init.h"
50 static int pci_parse_sysfs_value(const char *filename, uint64_t *val);
52 void *pci_map_addr = NULL;
55 #define OFF_MAX ((uint64_t)(off_t)-1)
57 pci_uio_get_mappings(const char *devname, struct pci_map maps[], int nb_maps)
60 char dirname[PATH_MAX];
61 char filename[PATH_MAX];
62 uint64_t offset, size;
64 for (i = 0; i != nb_maps; i++) {
66 /* check if map directory exists */
67 snprintf(dirname, sizeof(dirname),
68 "%s/maps/map%u", devname, i);
70 if (access(dirname, F_OK) != 0)
73 /* get mapping offset */
74 snprintf(filename, sizeof(filename),
75 "%s/offset", dirname);
76 if (pci_parse_sysfs_value(filename, &offset) < 0) {
78 "%s(): cannot parse offset of %s\n",
83 /* get mapping size */
84 snprintf(filename, sizeof(filename),
86 if (pci_parse_sysfs_value(filename, &size) < 0) {
88 "%s(): cannot parse size of %s\n",
93 /* get mapping physical address */
94 snprintf(filename, sizeof(filename),
96 if (pci_parse_sysfs_value(filename, &maps[i].phaddr) < 0) {
98 "%s(): cannot parse addr of %s\n",
103 if ((offset > OFF_MAX) || (size > SIZE_MAX)) {
105 "%s(): offset/size exceed system max value\n",
110 maps[i].offset = offset;
118 pci_uio_map_secondary(struct rte_pci_device *dev)
121 struct mapped_pci_resource *uio_res;
123 TAILQ_FOREACH(uio_res, pci_res_list, next) {
125 /* skip this element if it doesn't match our PCI address */
126 if (memcmp(&uio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
129 for (i = 0; i != uio_res->nb_maps; i++) {
131 * open devname, to mmap it
133 fd = open(uio_res->path, O_RDWR);
135 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
136 uio_res->path, strerror(errno));
140 if (pci_map_resource(uio_res->maps[i].addr, fd,
141 (off_t)uio_res->maps[i].offset,
142 (size_t)uio_res->maps[i].size)
143 != uio_res->maps[i].addr) {
145 "Cannot mmap device resource\n");
149 /* fd is not needed in slave process, close it */
155 RTE_LOG(ERR, EAL, "Cannot find resource for device\n");
160 pci_mknod_uio_dev(const char *sysfs_uio_path, unsigned uio_num)
163 char filename[PATH_MAX];
165 unsigned major, minor;
168 /* get the name of the sysfs file that contains the major and minor
169 * of the uio device and read its content */
170 snprintf(filename, sizeof(filename), "%s/dev", sysfs_uio_path);
172 f = fopen(filename, "r");
174 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs to get major:minor\n",
179 ret = fscanf(f, "%u:%u", &major, &minor);
181 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs to get major:minor\n",
188 /* create the char device "mknod /dev/uioX c major minor" */
189 snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
190 dev = makedev(major, minor);
191 ret = mknod(filename, S_IFCHR | S_IRUSR | S_IWUSR, dev);
193 RTE_LOG(ERR, EAL, "%s(): mknod() failed %s\n",
194 __func__, strerror(errno));
202 * Return the uioX char device used for a pci device. On success, return
203 * the UIO number and fill dstbuf string with the path of the device in
204 * sysfs. On error, return a negative value. In this case dstbuf is
208 pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf,
211 struct rte_pci_addr *loc = &dev->addr;
212 unsigned int uio_num;
215 char dirname[PATH_MAX];
217 /* depending on kernel version, uio can be located in uio/uioX
220 snprintf(dirname, sizeof(dirname),
221 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/uio",
222 loc->domain, loc->bus, loc->devid, loc->function);
224 dir = opendir(dirname);
226 /* retry with the parent directory */
227 snprintf(dirname, sizeof(dirname),
228 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT,
229 loc->domain, loc->bus, loc->devid, loc->function);
230 dir = opendir(dirname);
233 RTE_LOG(ERR, EAL, "Cannot opendir %s\n", dirname);
238 /* take the first file starting with "uio" */
239 while ((e = readdir(dir)) != NULL) {
240 /* format could be uio%d ...*/
241 int shortprefix_len = sizeof("uio") - 1;
242 /* ... or uio:uio%d */
243 int longprefix_len = sizeof("uio:uio") - 1;
246 if (strncmp(e->d_name, "uio", 3) != 0)
249 /* first try uio%d */
251 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
252 if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
253 snprintf(dstbuf, buflen, "%s/uio%u", dirname, uio_num);
257 /* then try uio:uio%d */
259 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
260 if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
261 snprintf(dstbuf, buflen, "%s/uio:uio%u", dirname, uio_num);
267 /* No uio resource found */
271 /* create uio device if we've been asked to */
272 if (internal_config.create_uio_dev &&
273 pci_mknod_uio_dev(dstbuf, uio_num) < 0)
274 RTE_LOG(WARNING, EAL, "Cannot create /dev/uio%u\n", uio_num);
279 /* map the PCI resource of a PCI device in virtual memory */
281 pci_uio_map_resource(struct rte_pci_device *dev)
284 char dirname[PATH_MAX];
285 char devname[PATH_MAX]; /* contains the /dev/uioX */
292 struct rte_pci_addr *loc = &dev->addr;
293 struct mapped_pci_resource *uio_res;
294 struct pci_map *maps;
296 dev->intr_handle.fd = -1;
297 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
299 /* secondary processes - use already recorded details */
300 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
301 return pci_uio_map_secondary(dev);
303 /* find uio resource */
304 uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname));
306 RTE_LOG(WARNING, EAL, " "PCI_PRI_FMT" not managed by UIO driver, "
307 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
310 snprintf(devname, sizeof(devname), "/dev/uio%u", uio_num);
312 /* save fd if in primary process */
313 dev->intr_handle.fd = open(devname, O_RDWR);
314 if (dev->intr_handle.fd < 0) {
315 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
316 devname, strerror(errno));
319 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
321 /* allocate the mapping details for secondary processes*/
322 uio_res = rte_zmalloc("UIO_RES", sizeof(*uio_res), 0);
323 if (uio_res == NULL) {
325 "%s(): cannot store uio mmap details\n", __func__);
329 snprintf(uio_res->path, sizeof(uio_res->path), "%s", devname);
330 memcpy(&uio_res->pci_addr, &dev->addr, sizeof(uio_res->pci_addr));
332 /* collect info about device mappings */
333 nb_maps = pci_uio_get_mappings(dirname, uio_res->maps,
334 RTE_DIM(uio_res->maps));
340 uio_res->nb_maps = nb_maps;
343 pagesz = sysconf(_SC_PAGESIZE);
345 maps = uio_res->maps;
346 for (i = 0; i != PCI_MAX_RESOURCE; i++) {
350 phaddr = dev->mem_resource[i].phys_addr;
354 for (j = 0; j != nb_maps && (phaddr != maps[j].phaddr ||
355 dev->mem_resource[i].len != maps[j].size);
359 /* if matching map is found, then use it */
365 * open devname, to mmap it
367 fd = open(devname, O_RDWR);
369 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
370 devname, strerror(errno));
374 if (maps[j].addr != NULL)
377 /* try mapping somewhere close to the end of hugepages */
378 if (pci_map_addr == NULL)
379 pci_map_addr = pci_find_max_end_va();
381 mapaddr = pci_map_resource(pci_map_addr, fd, (off_t)offset,
382 (size_t)maps[j].size);
383 if (mapaddr == MAP_FAILED)
386 pci_map_addr = RTE_PTR_ADD(mapaddr, (size_t) maps[j].size);
396 maps[j].addr = mapaddr;
397 maps[j].offset = offset;
398 dev->mem_resource[i].addr = mapaddr;
402 TAILQ_INSERT_TAIL(pci_res_list, uio_res, next);
408 * parse a sysfs file containing one integer value
409 * different to the eal version, as it needs to work with 64-bit values
412 pci_parse_sysfs_value(const char *filename, uint64_t *val)
418 f = fopen(filename, "r");
420 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
425 if (fgets(buf, sizeof(buf), f) == NULL) {
426 RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
431 *val = strtoull(buf, &end, 0);
432 if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
433 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",