mlx4: add MOFED 3.0 compatibility to interface name retrieval

Since Mellanox OFED 3.0 and Linux 3.15, interface port numbers are stored
in dev_port instead of dev_id sysfs files.

Signed-off-by: Or Ami <ora@mellanox.com>
Signed-off-by: Nitzan Weller <nitzanwe@mellanox.com>
Signed-off-by: Adrien Mazarguil <adrien.mazarguil@6wind.com>
This commit is contained in:
Adrien Mazarguil 2015-06-30 11:27:48 +02:00 committed by Thomas Monjalon
parent 53bbf5afbd
commit 68a60cee2f

View File

@ -337,9 +337,11 @@ priv_unlock(struct priv *priv)
static int static int
priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE]) priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
{ {
int ret = -1;
DIR *dir; DIR *dir;
struct dirent *dent; struct dirent *dent;
unsigned int dev_type = 0;
unsigned int dev_port_prev = ~0u;
char match[IF_NAMESIZE] = "";
{ {
MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path); MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
@ -351,7 +353,7 @@ priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
while ((dent = readdir(dir)) != NULL) { while ((dent = readdir(dir)) != NULL) {
char *name = dent->d_name; char *name = dent->d_name;
FILE *file; FILE *file;
unsigned int dev_id; unsigned int dev_port;
int r; int r;
if ((name[0] == '.') && if ((name[0] == '.') &&
@ -359,22 +361,47 @@ priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
((name[1] == '.') && (name[2] == '\0')))) ((name[1] == '.') && (name[2] == '\0'))))
continue; continue;
MKSTR(path, "%s/device/net/%s/dev_id", MKSTR(path, "%s/device/net/%s/%s",
priv->ctx->device->ibdev_path, name); priv->ctx->device->ibdev_path, name,
(dev_type ? "dev_id" : "dev_port"));
file = fopen(path, "rb"); file = fopen(path, "rb");
if (file == NULL) if (file == NULL) {
if (errno != ENOENT)
continue; continue;
r = fscanf(file, "%x", &dev_id); /*
fclose(file); * Switch to dev_id when dev_port does not exist as
if ((r == 1) && (dev_id == (priv->port - 1u))) { * is the case with Linux kernel versions < 3.15.
snprintf(*ifname, sizeof(*ifname), "%s", name); */
ret = 0; try_dev_id:
match[0] = '\0';
if (dev_type)
break; break;
dev_type = 1;
dev_port_prev = ~0u;
rewinddir(dir);
continue;
} }
r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
fclose(file);
if (r != 1)
continue;
/*
* Switch to dev_id when dev_port returns the same value for
* all ports. May happen when using a MOFED release older than
* 3.0 with a Linux kernel >= 3.15.
*/
if (dev_port == dev_port_prev)
goto try_dev_id;
dev_port_prev = dev_port;
if (dev_port == (priv->port - 1u))
snprintf(match, sizeof(match), "%s", name);
} }
closedir(dir); closedir(dir);
return ret; if (match[0] == '\0')
return -1;
strncpy(*ifname, match, sizeof(*ifname));
return 0;
} }
/** /**