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.
39 #include <linux/pci_regs.h>
43 #include <rte_common.h>
44 #include <rte_malloc.h>
45 #include <rte_tailq.h>
47 #include "rte_pci_dev_ids.h"
48 #include "eal_filesystem.h"
49 #include "eal_pci_init.h"
51 void *pci_map_addr = NULL;
54 #define OFF_MAX ((uint64_t)(off_t)-1)
56 pci_uio_get_mappings(struct rte_pci_device *dev,
57 struct pci_map maps[], int nb_maps)
59 struct rte_pci_addr *loc = &dev->addr;
61 char filename[PATH_MAX];
62 unsigned long long start_addr, end_addr, flags;
65 snprintf(filename, sizeof(filename),
66 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/resource",
67 loc->domain, loc->bus, loc->devid, loc->function);
69 f = fopen(filename, "r");
72 "%s(): cannot open sysfs %s\n",
77 while (fscanf(f, "%llx %llx %llx", &start_addr,
78 &end_addr, &flags) == 3 && i < nb_maps) {
79 if (flags & IORESOURCE_MEM) {
81 maps[i].size = end_addr - start_addr + 1;
82 maps[i].phaddr = start_addr;
92 pci_uio_set_bus_master(int dev_fd)
97 ret = pread(dev_fd, ®, sizeof(reg), PCI_COMMAND);
98 if (ret != sizeof(reg)) {
100 "Cannot read command from PCI config space!\n");
104 /* return if bus mastering is already on */
105 if (reg & PCI_COMMAND_MASTER)
108 reg |= PCI_COMMAND_MASTER;
110 ret = pwrite(dev_fd, ®, sizeof(reg), PCI_COMMAND);
111 if (ret != sizeof(reg)) {
113 "Cannot write command to PCI config space!\n");
121 pci_uio_map_secondary(struct rte_pci_device *dev)
124 struct mapped_pci_resource *uio_res;
126 TAILQ_FOREACH(uio_res, pci_res_list, next) {
128 /* skip this element if it doesn't match our PCI address */
129 if (memcmp(&uio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
132 for (i = 0; i != uio_res->nb_maps; i++) {
133 /* ignore mappings unused in primary process */
134 if (uio_res->maps[i].addr == NULL)
138 * open devname, to mmap it
140 fd = open(uio_res->path, O_RDWR);
142 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
143 uio_res->path, strerror(errno));
147 if (pci_map_resource(uio_res->maps[i].addr, fd,
148 (off_t)uio_res->maps[i].offset,
149 (size_t)uio_res->maps[i].size, 0)
150 != uio_res->maps[i].addr) {
152 "Cannot mmap device resource\n");
156 /* fd is not needed in slave process, close it */
162 RTE_LOG(ERR, EAL, "Cannot find resource for device\n");
167 pci_mknod_uio_dev(const char *sysfs_uio_path, unsigned uio_num)
170 char filename[PATH_MAX];
172 unsigned major, minor;
175 /* get the name of the sysfs file that contains the major and minor
176 * of the uio device and read its content */
177 snprintf(filename, sizeof(filename), "%s/dev", sysfs_uio_path);
179 f = fopen(filename, "r");
181 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs to get major:minor\n",
186 ret = fscanf(f, "%u:%u", &major, &minor);
188 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs to get major:minor\n",
195 /* create the char device "mknod /dev/uioX c major minor" */
196 snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
197 dev = makedev(major, minor);
198 ret = mknod(filename, S_IFCHR | S_IRUSR | S_IWUSR, dev);
200 RTE_LOG(ERR, EAL, "%s(): mknod() failed %s\n",
201 __func__, strerror(errno));
209 * Return the uioX char device used for a pci device. On success, return
210 * the UIO number and fill dstbuf string with the path of the device in
211 * sysfs. On error, return a negative value. In this case dstbuf is
215 pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf,
218 struct rte_pci_addr *loc = &dev->addr;
219 unsigned int uio_num;
222 char dirname[PATH_MAX];
224 /* depending on kernel version, uio can be located in uio/uioX
227 snprintf(dirname, sizeof(dirname),
228 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/uio",
229 loc->domain, loc->bus, loc->devid, loc->function);
231 dir = opendir(dirname);
233 /* retry with the parent directory */
234 snprintf(dirname, sizeof(dirname),
235 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT,
236 loc->domain, loc->bus, loc->devid, loc->function);
237 dir = opendir(dirname);
240 RTE_LOG(ERR, EAL, "Cannot opendir %s\n", dirname);
245 /* take the first file starting with "uio" */
246 while ((e = readdir(dir)) != NULL) {
247 /* format could be uio%d ...*/
248 int shortprefix_len = sizeof("uio") - 1;
249 /* ... or uio:uio%d */
250 int longprefix_len = sizeof("uio:uio") - 1;
253 if (strncmp(e->d_name, "uio", 3) != 0)
256 /* first try uio%d */
258 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
259 if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
260 snprintf(dstbuf, buflen, "%s/uio%u", dirname, uio_num);
264 /* then try uio:uio%d */
266 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
267 if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
268 snprintf(dstbuf, buflen, "%s/uio:uio%u", dirname, uio_num);
274 /* No uio resource found */
278 /* create uio device if we've been asked to */
279 if (internal_config.create_uio_dev &&
280 pci_mknod_uio_dev(dstbuf, uio_num) < 0)
281 RTE_LOG(WARNING, EAL, "Cannot create /dev/uio%u\n", uio_num);
286 /* map the PCI resource of a PCI device in virtual memory */
288 pci_uio_map_resource(struct rte_pci_device *dev)
291 char dirname[PATH_MAX];
292 char cfgname[PATH_MAX];
293 char devname[PATH_MAX]; /* contains the /dev/uioX */
300 struct rte_pci_addr *loc = &dev->addr;
301 struct mapped_pci_resource *uio_res;
302 struct pci_map *maps;
304 dev->intr_handle.fd = -1;
305 dev->intr_handle.uio_cfg_fd = -1;
306 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
308 /* secondary processes - use already recorded details */
309 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
310 return pci_uio_map_secondary(dev);
312 /* find uio resource */
313 uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname));
315 RTE_LOG(WARNING, EAL, " "PCI_PRI_FMT" not managed by UIO driver, "
316 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
319 snprintf(devname, sizeof(devname), "/dev/uio%u", uio_num);
321 /* save fd if in primary process */
322 dev->intr_handle.fd = open(devname, O_RDWR);
323 if (dev->intr_handle.fd < 0) {
324 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
325 devname, strerror(errno));
328 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
330 snprintf(cfgname, sizeof(cfgname),
331 "/sys/class/uio/uio%u/device/config", uio_num);
332 dev->intr_handle.uio_cfg_fd = open(cfgname, O_RDWR);
333 if (dev->intr_handle.uio_cfg_fd < 0) {
334 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
335 cfgname, strerror(errno));
339 /* update devname for mmap */
340 snprintf(devname, sizeof(devname),
341 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/resource%d",
342 loc->domain, loc->bus, loc->devid, loc->function, 0);
344 /* set bus master that is not done by uio_pci_generic */
345 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
346 if (pci_uio_set_bus_master(dev->intr_handle.uio_cfg_fd)) {
347 RTE_LOG(ERR, EAL, "Cannot set up bus mastering!\n");
352 /* allocate the mapping details for secondary processes*/
353 uio_res = rte_zmalloc("UIO_RES", sizeof(*uio_res), 0);
354 if (uio_res == NULL) {
356 "%s(): cannot store uio mmap details\n", __func__);
360 snprintf(uio_res->path, sizeof(uio_res->path), "%s", devname);
361 memcpy(&uio_res->pci_addr, &dev->addr, sizeof(uio_res->pci_addr));
363 /* collect info about device mappings */
364 nb_maps = pci_uio_get_mappings(dev, uio_res->maps,
365 RTE_DIM(uio_res->maps));
370 uio_res->nb_maps = nb_maps;
373 pagesz = sysconf(_SC_PAGESIZE);
375 maps = uio_res->maps;
376 for (i = 0; i != PCI_MAX_RESOURCE; i++) {
380 phaddr = dev->mem_resource[i].phys_addr;
384 for (j = 0; j != nb_maps && (phaddr != maps[j].phaddr ||
385 dev->mem_resource[i].len != maps[j].size);
389 /* if matching map is found, then use it */
395 * open devname, to mmap it
397 fd = open(devname, O_RDWR);
399 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
400 devname, strerror(errno));
404 if (maps[j].addr != NULL)
407 /* try mapping somewhere close to the end of hugepages */
408 if (pci_map_addr == NULL)
409 pci_map_addr = pci_find_max_end_va();
411 mapaddr = pci_map_resource(pci_map_addr, fd, (off_t)offset,
412 (size_t)maps[j].size, 0);
413 if (mapaddr == MAP_FAILED)
416 pci_map_addr = RTE_PTR_ADD(mapaddr, (size_t) maps[j].size);
426 maps[j].addr = mapaddr;
427 maps[j].offset = offset;
428 dev->mem_resource[i].addr = mapaddr;
432 TAILQ_INSERT_TAIL(pci_res_list, uio_res, next);