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 = RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
97 TAILQ_FOREACH(uio_res, uio_res_list, next) {
99 /* skip this element if it doesn't match our PCI address */
100 if (rte_eal_compare_pci_addr(&uio_res->pci_addr, &dev->addr))
103 for (i = 0; i != uio_res->nb_maps; i++) {
105 * open devname, to mmap it
107 fd = open(uio_res->maps[i].path, O_RDWR);
109 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
110 uio_res->maps[i].path, strerror(errno));
114 void *mapaddr = pci_map_resource(uio_res->maps[i].addr,
115 fd, (off_t)uio_res->maps[i].offset,
116 (size_t)uio_res->maps[i].size, 0);
117 if (mapaddr != uio_res->maps[i].addr) {
118 if (mapaddr == MAP_FAILED)
120 "Cannot mmap device resource file %s: %s\n",
121 uio_res->maps[i].path,
125 "Cannot mmap device resource file %s to address: %p\n",
126 uio_res->maps[i].path,
127 uio_res->maps[i].addr);
132 /* fd is not needed in slave process, close it */
138 RTE_LOG(ERR, EAL, "Cannot find resource for device\n");
143 pci_mknod_uio_dev(const char *sysfs_uio_path, unsigned uio_num)
146 char filename[PATH_MAX];
148 unsigned major, minor;
151 /* get the name of the sysfs file that contains the major and minor
152 * of the uio device and read its content */
153 snprintf(filename, sizeof(filename), "%s/dev", sysfs_uio_path);
155 f = fopen(filename, "r");
157 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs to get major:minor\n",
162 ret = fscanf(f, "%u:%u", &major, &minor);
164 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs to get major:minor\n",
171 /* create the char device "mknod /dev/uioX c major minor" */
172 snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
173 dev = makedev(major, minor);
174 ret = mknod(filename, S_IFCHR | S_IRUSR | S_IWUSR, dev);
176 RTE_LOG(ERR, EAL, "%s(): mknod() failed %s\n",
177 __func__, strerror(errno));
185 * Return the uioX char device used for a pci device. On success, return
186 * the UIO number and fill dstbuf string with the path of the device in
187 * sysfs. On error, return a negative value. In this case dstbuf is
191 pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf,
194 struct rte_pci_addr *loc = &dev->addr;
195 unsigned int uio_num;
198 char dirname[PATH_MAX];
200 /* depending on kernel version, uio can be located in uio/uioX
203 snprintf(dirname, sizeof(dirname),
204 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/uio",
205 loc->domain, loc->bus, loc->devid, loc->function);
207 dir = opendir(dirname);
209 /* retry with the parent directory */
210 snprintf(dirname, sizeof(dirname),
211 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT,
212 loc->domain, loc->bus, loc->devid, loc->function);
213 dir = opendir(dirname);
216 RTE_LOG(ERR, EAL, "Cannot opendir %s\n", dirname);
221 /* take the first file starting with "uio" */
222 while ((e = readdir(dir)) != NULL) {
223 /* format could be uio%d ...*/
224 int shortprefix_len = sizeof("uio") - 1;
225 /* ... or uio:uio%d */
226 int longprefix_len = sizeof("uio:uio") - 1;
229 if (strncmp(e->d_name, "uio", 3) != 0)
232 /* first try uio%d */
234 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
235 if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
236 snprintf(dstbuf, buflen, "%s/uio%u", dirname, uio_num);
240 /* then try uio:uio%d */
242 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
243 if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
244 snprintf(dstbuf, buflen, "%s/uio:uio%u", dirname, uio_num);
250 /* No uio resource found */
254 /* create uio device if we've been asked to */
255 if (internal_config.create_uio_dev &&
256 pci_mknod_uio_dev(dstbuf, uio_num) < 0)
257 RTE_LOG(WARNING, EAL, "Cannot create /dev/uio%u\n", uio_num);
262 /* map the PCI resource of a PCI device in virtual memory */
264 pci_uio_map_resource(struct rte_pci_device *dev)
267 char dirname[PATH_MAX];
268 char cfgname[PATH_MAX];
269 char devname[PATH_MAX]; /* contains the /dev/uioX */
273 struct rte_pci_addr *loc = &dev->addr;
274 struct mapped_pci_resource *uio_res;
275 struct mapped_pci_res_list *uio_res_list = RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
276 struct pci_map *maps;
278 dev->intr_handle.fd = -1;
279 dev->intr_handle.uio_cfg_fd = -1;
280 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
282 /* secondary processes - use already recorded details */
283 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
284 return pci_uio_map_secondary(dev);
286 /* find uio resource */
287 uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname));
289 RTE_LOG(WARNING, EAL, " "PCI_PRI_FMT" not managed by UIO driver, "
290 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
293 snprintf(devname, sizeof(devname), "/dev/uio%u", uio_num);
295 /* save fd if in primary process */
296 dev->intr_handle.fd = open(devname, O_RDWR);
297 if (dev->intr_handle.fd < 0) {
298 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
299 devname, strerror(errno));
303 snprintf(cfgname, sizeof(cfgname),
304 "/sys/class/uio/uio%u/device/config", uio_num);
305 dev->intr_handle.uio_cfg_fd = open(cfgname, O_RDWR);
306 if (dev->intr_handle.uio_cfg_fd < 0) {
307 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
308 cfgname, strerror(errno));
312 if (dev->kdrv == RTE_KDRV_IGB_UIO)
313 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
315 dev->intr_handle.type = RTE_INTR_HANDLE_UIO_INTX;
317 /* set bus master that is not done by uio_pci_generic */
318 if (pci_uio_set_bus_master(dev->intr_handle.uio_cfg_fd)) {
319 RTE_LOG(ERR, EAL, "Cannot set up bus mastering!\n");
324 /* allocate the mapping details for secondary processes*/
325 uio_res = rte_zmalloc("UIO_RES", sizeof(*uio_res), 0);
326 if (uio_res == NULL) {
328 "%s(): cannot store uio mmap details\n", __func__);
332 snprintf(uio_res->path, sizeof(uio_res->path), "%s", devname);
333 memcpy(&uio_res->pci_addr, &dev->addr, sizeof(uio_res->pci_addr));
336 maps = uio_res->maps;
337 for (i = 0, map_idx = 0; i != PCI_MAX_RESOURCE; i++) {
342 phaddr = dev->mem_resource[i].phys_addr;
347 /* update devname for mmap */
348 snprintf(devname, sizeof(devname),
349 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/resource%d",
350 loc->domain, loc->bus, loc->devid, loc->function,
354 * open resource file, to mmap it
356 fd = open(devname, O_RDWR);
358 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
359 devname, strerror(errno));
363 /* try mapping somewhere close to the end of hugepages */
364 if (pci_map_addr == NULL)
365 pci_map_addr = pci_find_max_end_va();
367 mapaddr = pci_map_resource(pci_map_addr, fd, 0,
368 (size_t)dev->mem_resource[i].len, 0);
369 if (mapaddr == MAP_FAILED)
372 pci_map_addr = RTE_PTR_ADD(mapaddr,
373 (size_t)dev->mem_resource[i].len);
375 maps[map_idx].path = rte_malloc(NULL, strlen(devname) + 1, 0);
376 if (maps[map_idx].path == NULL)
386 maps[map_idx].phaddr = dev->mem_resource[i].phys_addr;
387 maps[map_idx].size = dev->mem_resource[i].len;
388 maps[map_idx].addr = mapaddr;
389 maps[map_idx].offset = 0;
390 strcpy(maps[map_idx].path, devname);
392 dev->mem_resource[i].addr = mapaddr;
395 uio_res->nb_maps = map_idx;
397 TAILQ_INSERT_TAIL(uio_res_list, uio_res, next);
402 #ifdef RTE_LIBRTE_EAL_HOTPLUG
404 pci_uio_unmap(struct mapped_pci_resource *uio_res)
411 for (i = 0; i != uio_res->nb_maps; i++)
412 pci_unmap_resource(uio_res->maps[i].addr,
413 (size_t)uio_res->maps[i].size);
416 static struct mapped_pci_resource *
417 pci_uio_find_resource(struct rte_pci_device *dev)
419 struct mapped_pci_resource *uio_res;
420 struct mapped_pci_res_list *uio_res_list = RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
425 TAILQ_FOREACH(uio_res, uio_res_list, next) {
427 /* skip this element if it doesn't match our PCI address */
428 if (!rte_eal_compare_pci_addr(&uio_res->pci_addr, &dev->addr))
434 /* unmap the PCI resource of a PCI device in virtual memory */
436 pci_uio_unmap_resource(struct rte_pci_device *dev)
438 struct mapped_pci_resource *uio_res;
439 struct mapped_pci_res_list *uio_res_list = RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
444 /* find an entry for the device */
445 uio_res = pci_uio_find_resource(dev);
449 /* secondary processes - just free maps */
450 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
451 return pci_uio_unmap(uio_res);
453 TAILQ_REMOVE(uio_res_list, uio_res, next);
455 /* unmap all resources */
456 pci_uio_unmap(uio_res);
458 /* free uio resource */
461 /* close fd if in primary process */
462 close(dev->intr_handle.fd);
464 dev->intr_handle.fd = -1;
465 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
467 #endif /* RTE_LIBRTE_EAL_HOTPLUG */