-
-fail:
- return NULL;
-}
-
-#define OFF_MAX ((uint64_t)(off_t)-1)
-static ssize_t
-pci_uio_get_mappings(const char *devname, struct uio_map maps[], size_t nb_maps)
-{
- size_t i;
- char dirname[PATH_MAX];
- char filename[PATH_MAX];
- uint64_t offset, size;
-
- for (i = 0; i != nb_maps; i++) {
-
- /* check if map directory exists */
- rte_snprintf(dirname, sizeof(dirname),
- "%s/maps/map%u", devname, i);
-
- if (access(dirname, F_OK) != 0)
- break;
-
- /* get mapping offset */
- rte_snprintf(filename, sizeof(filename),
- "%s/offset", dirname);
- if (pci_parse_sysfs_value(filename, &offset) < 0) {
- RTE_LOG(ERR, EAL,
- "%s(): cannot parse offset of %s\n",
- __func__, dirname);
- return (-1);
- }
-
- /* get mapping size */
- rte_snprintf(filename, sizeof(filename),
- "%s/size", dirname);
- if (pci_parse_sysfs_value(filename, &size) < 0) {
- RTE_LOG(ERR, EAL,
- "%s(): cannot parse size of %s\n",
- __func__, dirname);
- return (-1);
- }
-
- /* get mapping physical address */
- rte_snprintf(filename, sizeof(filename),
- "%s/addr", dirname);
- if (pci_parse_sysfs_value(filename, &maps[i].phaddr) < 0) {
- RTE_LOG(ERR, EAL,
- "%s(): cannot parse addr of %s\n",
- __func__, dirname);
- return (-1);
- }
-
- if ((offset > OFF_MAX) || (size > SIZE_MAX)) {
- RTE_LOG(ERR, EAL,
- "%s(): offset/size exceed system max value\n",
- __func__);
- return (-1);
- }
-
- maps[i].offset = offset;
- maps[i].size = size;
- }
- return (i);
-}
-
-static int
-pci_uio_map_secondary(struct rte_pci_device *dev)
-{
- size_t i;
- struct uio_resource *uio_res;
-
- TAILQ_FOREACH(uio_res, uio_res_list, next) {
-
- /* skip this element if it doesn't match our PCI address */
- if (memcmp(&uio_res->pci_addr, &dev->addr, sizeof(dev->addr)))
- continue;
-
- for (i = 0; i != uio_res->nb_maps; i++) {
- if (pci_map_resource(uio_res->maps[i].addr,
- uio_res->path,
- (off_t)uio_res->maps[i].offset,
- (size_t)uio_res->maps[i].size)
- != uio_res->maps[i].addr) {
- RTE_LOG(ERR, EAL,
- "Cannot mmap device resource\n");
- return (-1);
- }
- }
- return (0);
- }
-
- RTE_LOG(ERR, EAL, "Cannot find resource for device\n");
- return -1;
-}
-
-static int pci_mknod_uio_dev(const char *sysfs_uio_path, unsigned uio_num)
-{
- FILE *f;
- char filename[PATH_MAX];
- int ret;
- unsigned major, minor;
- dev_t dev;
-
- /* get the name of the sysfs file that contains the major and minor
- * of the uio device and read its content */
- rte_snprintf(filename, sizeof(filename), "%s/dev", sysfs_uio_path);
-
- f = fopen(filename, "r");
- if (f == NULL) {
- RTE_LOG(ERR, EAL, "%s(): cannot open sysfs to get major:minor\n",
- __func__);
- return -1;
- }
-
- ret = fscanf(f, "%d:%d", &major, &minor);
- if (ret != 2) {
- RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs to get major:minor\n",
- __func__);
- fclose(f);
- return -1;
- }
- fclose(f);
-
- /* create the char device "mknod /dev/uioX c major minor" */
- rte_snprintf(filename, sizeof(filename), "/dev/uio%u", uio_num);
- dev = makedev(major, minor);
- ret = mknod(filename, S_IFCHR | S_IRUSR | S_IWUSR, dev);
- if (f == NULL) {
- RTE_LOG(ERR, EAL, "%s(): mknod() failed %s\n",
- __func__, strerror(errno));
- return -1;
- }
-
- return ret;
-}
-
-/*
- * Return the uioX char device used for a pci device. On success, return
- * the UIO number and fill dstbuf string with the path of the device in
- * sysfs. On error, return a negative value. In this case dstbuf is
- * invalid.
- */
-static int pci_get_uio_dev(struct rte_pci_device *dev, char *dstbuf,
- unsigned int buflen)
-{
- struct rte_pci_addr *loc = &dev->addr;
- unsigned int uio_num;
- struct dirent *e;
- DIR *dir;
- char dirname[PATH_MAX];
-
- /* depending on kernel version, uio can be located in uio/uioX
- * or uio:uioX */
-
- rte_snprintf(dirname, sizeof(dirname),
- SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/uio",
- loc->domain, loc->bus, loc->devid, loc->function);
-
- dir = opendir(dirname);
- if (dir == NULL) {
- /* retry with the parent directory */
- rte_snprintf(dirname, sizeof(dirname),
- SYSFS_PCI_DEVICES "/" PCI_PRI_FMT,
- loc->domain, loc->bus, loc->devid, loc->function);
- dir = opendir(dirname);
-
- if (dir == NULL) {
- RTE_LOG(ERR, EAL, "Cannot opendir %s\n", dirname);
- return -1;
- }
- }
-
- /* take the first file starting with "uio" */
- while ((e = readdir(dir)) != NULL) {
- /* format could be uio%d ...*/
- int shortprefix_len = sizeof("uio") - 1;
- /* ... or uio:uio%d */
- int longprefix_len = sizeof("uio:uio") - 1;
- char *endptr;
-
- if (strncmp(e->d_name, "uio", 3) != 0)
- continue;
-
- /* first try uio%d */
- errno = 0;
- uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
- if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
- rte_snprintf(dstbuf, buflen, "%s/uio%u", dirname, uio_num);
- break;
- }
-
- /* then try uio:uio%d */
- errno = 0;
- uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
- if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
- rte_snprintf(dstbuf, buflen, "%s/uio:uio%u", dirname, uio_num);
- break;
- }
- }
- closedir(dir);
-
- /* No uio resource found */
- if (e == NULL)
- return -1;
-
- /* create uio device if we've been asked to */
- if (internal_config.create_uio_dev && pci_mknod_uio_dev(dstbuf, uio_num) < 0)
- RTE_LOG(WARNING, EAL, "Cannot create /dev/uio%u\n", uio_num);
-
- return uio_num;