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 "eal_filesystem.h"
49 #include "eal_pci_init.h"
51 void *pci_map_addr = NULL;
53 #define OFF_MAX ((uint64_t)(off_t)-1)
56 pci_uio_read_config(const struct rte_intr_handle *intr_handle,
57 void *buf, size_t len, off_t offset)
59 return pread(intr_handle->uio_cfg_fd, buf, len, offset);
63 pci_uio_write_config(const struct rte_intr_handle *intr_handle,
64 const void *buf, size_t len, off_t offset)
66 return pwrite(intr_handle->uio_cfg_fd, buf, len, offset);
70 pci_uio_set_bus_master(int dev_fd)
75 ret = pread(dev_fd, ®, sizeof(reg), PCI_COMMAND);
76 if (ret != sizeof(reg)) {
78 "Cannot read command from PCI config space!\n");
82 /* return if bus mastering is already on */
83 if (reg & PCI_COMMAND_MASTER)
86 reg |= PCI_COMMAND_MASTER;
88 ret = pwrite(dev_fd, ®, sizeof(reg), PCI_COMMAND);
89 if (ret != sizeof(reg)) {
91 "Cannot write command to PCI config space!\n");
99 pci_mknod_uio_dev(const char *sysfs_uio_path, unsigned uio_num)
102 char filename[PATH_MAX];
104 unsigned major, minor;
107 /* get the name of the sysfs file that contains the major and minor
108 * of the uio device and read its content */
109 snprintf(filename, sizeof(filename), "%s/dev", sysfs_uio_path);
111 f = fopen(filename, "r");
113 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs to get major:minor\n",
118 ret = fscanf(f, "%u:%u", &major, &minor);
120 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs to get major:minor\n",
127 /* create the char device "mknod /dev/uioX c major minor" */
128 snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
129 dev = makedev(major, minor);
130 ret = mknod(filename, S_IFCHR | S_IRUSR | S_IWUSR, dev);
132 RTE_LOG(ERR, EAL, "%s(): mknod() failed %s\n",
133 __func__, strerror(errno));
141 * Return the uioX char device used for a pci device. On success, return
142 * the UIO number and fill dstbuf string with the path of the device in
143 * sysfs. On error, return a negative value. In this case dstbuf is
147 pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf,
150 struct rte_pci_addr *loc = &dev->addr;
151 unsigned int uio_num;
154 char dirname[PATH_MAX];
156 /* depending on kernel version, uio can be located in uio/uioX
159 snprintf(dirname, sizeof(dirname),
160 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/uio",
161 loc->domain, loc->bus, loc->devid, loc->function);
163 dir = opendir(dirname);
165 /* retry with the parent directory */
166 snprintf(dirname, sizeof(dirname),
167 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT,
168 loc->domain, loc->bus, loc->devid, loc->function);
169 dir = opendir(dirname);
172 RTE_LOG(ERR, EAL, "Cannot opendir %s\n", dirname);
177 /* take the first file starting with "uio" */
178 while ((e = readdir(dir)) != NULL) {
179 /* format could be uio%d ...*/
180 int shortprefix_len = sizeof("uio") - 1;
181 /* ... or uio:uio%d */
182 int longprefix_len = sizeof("uio:uio") - 1;
185 if (strncmp(e->d_name, "uio", 3) != 0)
188 /* first try uio%d */
190 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
191 if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
192 snprintf(dstbuf, buflen, "%s/uio%u", dirname, uio_num);
196 /* then try uio:uio%d */
198 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
199 if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
200 snprintf(dstbuf, buflen, "%s/uio:uio%u", dirname, uio_num);
206 /* No uio resource found */
210 /* create uio device if we've been asked to */
211 if (internal_config.create_uio_dev &&
212 pci_mknod_uio_dev(dstbuf, uio_num) < 0)
213 RTE_LOG(WARNING, EAL, "Cannot create /dev/uio%u\n", uio_num);
219 pci_uio_free_resource(struct rte_pci_device *dev,
220 struct mapped_pci_resource *uio_res)
224 if (dev->intr_handle.uio_cfg_fd >= 0) {
225 close(dev->intr_handle.uio_cfg_fd);
226 dev->intr_handle.uio_cfg_fd = -1;
228 if (dev->intr_handle.fd) {
229 close(dev->intr_handle.fd);
230 dev->intr_handle.fd = -1;
231 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
236 pci_uio_alloc_resource(struct rte_pci_device *dev,
237 struct mapped_pci_resource **uio_res)
239 char dirname[PATH_MAX];
240 char cfgname[PATH_MAX];
241 char devname[PATH_MAX]; /* contains the /dev/uioX */
243 struct rte_pci_addr *loc;
247 /* find uio resource */
248 uio_num = pci_get_uio_dev(dev, dirname, sizeof(dirname));
250 RTE_LOG(WARNING, EAL, " "PCI_PRI_FMT" not managed by UIO driver, "
251 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
254 snprintf(devname, sizeof(devname), "/dev/uio%u", uio_num);
256 /* save fd if in primary process */
257 dev->intr_handle.fd = open(devname, O_RDWR);
258 if (dev->intr_handle.fd < 0) {
259 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
260 devname, strerror(errno));
264 snprintf(cfgname, sizeof(cfgname),
265 "/sys/class/uio/uio%u/device/config", uio_num);
266 dev->intr_handle.uio_cfg_fd = open(cfgname, O_RDWR);
267 if (dev->intr_handle.uio_cfg_fd < 0) {
268 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
269 cfgname, strerror(errno));
273 if (dev->kdrv == RTE_KDRV_IGB_UIO)
274 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
276 dev->intr_handle.type = RTE_INTR_HANDLE_UIO_INTX;
278 /* set bus master that is not done by uio_pci_generic */
279 if (pci_uio_set_bus_master(dev->intr_handle.uio_cfg_fd)) {
280 RTE_LOG(ERR, EAL, "Cannot set up bus mastering!\n");
285 /* allocate the mapping details for secondary processes*/
286 *uio_res = rte_zmalloc("UIO_RES", sizeof(**uio_res), 0);
287 if (*uio_res == NULL) {
289 "%s(): cannot store uio mmap details\n", __func__);
293 snprintf((*uio_res)->path, sizeof((*uio_res)->path), "%s", devname);
294 memcpy(&(*uio_res)->pci_addr, &dev->addr, sizeof((*uio_res)->pci_addr));
299 pci_uio_free_resource(dev, *uio_res);
304 pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
305 struct mapped_pci_resource *uio_res, int map_idx)
308 char devname[PATH_MAX]; /* contains the /dev/uioX */
310 struct rte_pci_addr *loc;
311 struct pci_map *maps;
314 maps = uio_res->maps;
316 /* update devname for mmap */
317 snprintf(devname, sizeof(devname),
318 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/resource%d",
319 loc->domain, loc->bus, loc->devid,
320 loc->function, res_idx);
322 /* allocate memory to keep path */
323 maps[map_idx].path = rte_malloc(NULL, strlen(devname) + 1, 0);
324 if (maps[map_idx].path == NULL) {
325 RTE_LOG(ERR, EAL, "Cannot allocate memory for path: %s\n",
331 * open resource file, to mmap it
333 fd = open(devname, O_RDWR);
335 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
336 devname, strerror(errno));
340 /* try mapping somewhere close to the end of hugepages */
341 if (pci_map_addr == NULL)
342 pci_map_addr = pci_find_max_end_va();
344 mapaddr = pci_map_resource(pci_map_addr, fd, 0,
345 (size_t)dev->mem_resource[res_idx].len, 0);
347 if (mapaddr == MAP_FAILED)
350 pci_map_addr = RTE_PTR_ADD(mapaddr,
351 (size_t)dev->mem_resource[res_idx].len);
353 maps[map_idx].phaddr = dev->mem_resource[res_idx].phys_addr;
354 maps[map_idx].size = dev->mem_resource[res_idx].len;
355 maps[map_idx].addr = mapaddr;
356 maps[map_idx].offset = 0;
357 strcpy(maps[map_idx].path, devname);
358 dev->mem_resource[res_idx].addr = mapaddr;
363 rte_free(maps[map_idx].path);