2016-06-14 01:19:17 +02:00
|
|
|
/** Linux PCI helpers
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2017-03-03 20:20:13 -04:00
|
|
|
* @copyright 2017, Steffen Vogel
|
2016-06-14 01:19:17 +02:00
|
|
|
**********************************************************************************/
|
|
|
|
|
2016-10-08 20:10:36 -04:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <string.h>
|
2017-03-03 20:21:33 -04:00
|
|
|
#include <unistd.h>
|
2017-06-15 15:07:04 +02:00
|
|
|
#include <linux/limits.h>
|
2016-06-14 01:19:17 +02:00
|
|
|
|
|
|
|
#include "log.h"
|
2017-03-11 23:50:30 -03:00
|
|
|
#include "utils.h"
|
2016-06-14 01:19:17 +02:00
|
|
|
|
|
|
|
#include "kernel/pci.h"
|
|
|
|
#include "config.h"
|
|
|
|
|
2016-10-08 20:10:36 -04:00
|
|
|
int pci_init(struct pci *p)
|
|
|
|
{
|
2017-07-09 14:36:09 +02:00
|
|
|
struct dirent *e;
|
2016-10-08 20:10:36 -04:00
|
|
|
DIR *dp;
|
|
|
|
FILE *f;
|
2017-06-15 15:07:04 +02:00
|
|
|
char path[PATH_MAX];
|
2016-10-08 20:10:36 -04:00
|
|
|
int ret;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-08 20:10:36 -04:00
|
|
|
snprintf(path, sizeof(path), "%s/bus/pci/devices", SYSFS_PATH);
|
|
|
|
|
|
|
|
dp = opendir(path);
|
|
|
|
if (dp == NULL) {
|
|
|
|
serror("Failed to detect PCI devices");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-07-09 14:36:09 +02:00
|
|
|
while ((e = readdir(dp))) {
|
2017-10-18 15:39:53 +02:00
|
|
|
struct pci_device *d = (struct pci_device *) alloc(sizeof(struct pci_device));
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-08 20:10:36 -04:00
|
|
|
struct { const char *s; int *p; } map[] = {
|
2017-07-09 14:36:09 +02:00
|
|
|
{ "vendor", &d->id.vendor },
|
|
|
|
{ "device", &d->id.device }
|
2016-10-08 20:10:36 -04:00
|
|
|
};
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-08 20:10:36 -04:00
|
|
|
/* Read vendor & device id */
|
|
|
|
for (int i = 0; i < 2; i++) {
|
2017-07-09 14:36:09 +02:00
|
|
|
snprintf(path, sizeof(path), "%s/bus/pci/devices/%s/%s", SYSFS_PATH, e->d_name, map[i].s);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-08 20:10:36 -04:00
|
|
|
f = fopen(path, "r");
|
|
|
|
if (!f)
|
|
|
|
serror("Failed to open '%s'", path);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-08 20:10:36 -04:00
|
|
|
ret = fscanf(f, "%x", map[i].p);
|
|
|
|
if (ret != 1)
|
|
|
|
error("Failed to parse %s ID from: %s", map[i].s, path);
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get slot id */
|
2017-07-09 14:36:09 +02:00
|
|
|
ret = sscanf(e->d_name, "%4x:%2x:%2x.%u", &d->slot.domain, &d->slot.bus, &d->slot.device, &d->slot.function);
|
2016-10-08 20:10:36 -04:00
|
|
|
if (ret != 4)
|
2017-07-09 14:36:09 +02:00
|
|
|
error("Failed to parse PCI slot number: %s", e->d_name);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-07-09 14:36:09 +02:00
|
|
|
list_push(&p->devices, d);
|
2016-10-08 20:10:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dp);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-08 20:10:36 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-18 10:45:05 -05:00
|
|
|
int pci_destroy(struct pci *p)
|
2016-10-08 20:10:36 -04:00
|
|
|
{
|
|
|
|
list_destroy(&p->devices, NULL, true);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-08 20:10:36 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2016-06-19 19:23:19 +02:00
|
|
|
|
2017-03-27 12:57:41 +02:00
|
|
|
int pci_device_parse_slot(struct pci_device *f, const char *s, const char **error)
|
2016-06-19 19:23:19 +02:00
|
|
|
{
|
2016-10-08 20:10:36 -04:00
|
|
|
char *str = strdup(s);
|
|
|
|
char *colon = strrchr(str, ':');
|
|
|
|
char *dot = strchr((colon ? colon + 1 : str), '.');
|
|
|
|
char *mid = str;
|
|
|
|
char *e, *bus, *colon2;
|
|
|
|
|
|
|
|
if (colon) {
|
|
|
|
*colon++ = 0;
|
|
|
|
mid = colon;
|
2016-06-19 19:23:19 +02:00
|
|
|
|
2016-10-08 20:10:36 -04:00
|
|
|
colon2 = strchr(str, ':');
|
|
|
|
if (colon2) {
|
|
|
|
*colon2++ = 0;
|
|
|
|
bus = colon2;
|
2016-06-19 19:23:19 +02:00
|
|
|
|
2016-10-08 20:10:36 -04:00
|
|
|
if (str[0] && strcmp(str, "*")) {
|
|
|
|
long int x = strtol(str, &e, 16);
|
|
|
|
if ((e && *e) || (x < 0 || x > 0x7fffffff)) {
|
|
|
|
*error = "Invalid domain number";
|
|
|
|
goto fail;
|
|
|
|
}
|
2016-06-19 19:23:19 +02:00
|
|
|
|
2016-10-08 20:10:36 -04:00
|
|
|
f->slot.domain = x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
bus = str;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-08 20:10:36 -04:00
|
|
|
if (bus[0] && strcmp(bus, "*")) {
|
|
|
|
long int x = strtol(bus, &e, 16);
|
|
|
|
if ((e && *e) || (x < 0 || x > 0xff)) {
|
|
|
|
*error = "Invalid bus number";
|
|
|
|
goto fail;
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-08 20:10:36 -04:00
|
|
|
f->slot.bus = x;
|
|
|
|
}
|
|
|
|
}
|
2016-06-19 19:23:19 +02:00
|
|
|
|
2016-10-08 20:10:36 -04:00
|
|
|
if (dot)
|
|
|
|
*dot++ = 0;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-08 20:10:36 -04:00
|
|
|
if (mid[0] && strcmp(mid, "*")) {
|
|
|
|
long int x = strtol(mid, &e, 16);
|
2016-06-19 19:23:19 +02:00
|
|
|
|
2016-10-08 20:10:36 -04:00
|
|
|
if ((e && *e) || (x < 0 || x > 0x1f)) {
|
|
|
|
*error = "Invalid slot number";
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
f->slot.device = x;
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-08 20:10:36 -04:00
|
|
|
if (dot && dot[0] && strcmp(dot, "*")) {
|
|
|
|
long int x = strtol(dot, &e, 16);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-08 20:10:36 -04:00
|
|
|
if ((e && *e) || (x < 0 || x > 7)) {
|
|
|
|
*error = "Invalid function number";
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
f->slot.function = x;
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-08 20:10:36 -04:00
|
|
|
free(str);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
free(str);
|
|
|
|
return -1;
|
2016-06-19 19:23:19 +02:00
|
|
|
}
|
|
|
|
|
2016-10-08 20:10:36 -04:00
|
|
|
/* ID filter syntax: [vendor]:[device][:class] */
|
2017-03-27 12:57:41 +02:00
|
|
|
int pci_device_parse_id(struct pci_device *f, const char *str, const char **error)
|
2016-06-19 19:23:19 +02:00
|
|
|
{
|
2016-10-08 20:10:36 -04:00
|
|
|
char *s, *c, *e;
|
|
|
|
|
|
|
|
if (!*str)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
s = strchr(str, ':');
|
|
|
|
if (!s) {
|
|
|
|
*error = "':' expected";
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
*s++ = 0;
|
|
|
|
if (str[0] && strcmp(str, "*")) {
|
|
|
|
long int x = strtol(str, &e, 16);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-08 20:10:36 -04:00
|
|
|
if ((e && *e) || (x < 0 || x > 0xffff)) {
|
|
|
|
*error = "Invalid vendor ID";
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
f->id.vendor = x;
|
2016-06-19 19:23:19 +02:00
|
|
|
}
|
2016-10-08 20:10:36 -04:00
|
|
|
|
|
|
|
c = strchr(s, ':');
|
|
|
|
if (c)
|
|
|
|
*c++ = 0;
|
|
|
|
|
|
|
|
if (s[0] && strcmp(s, "*")) {
|
|
|
|
long int x = strtol(s, &e, 16);
|
|
|
|
if ((e && *e) || (x < 0 || x > 0xffff)) {
|
|
|
|
*error = "Invalid device ID";
|
|
|
|
goto fail;
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-08 20:10:36 -04:00
|
|
|
f->id.device = x;
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-08 20:10:36 -04:00
|
|
|
if (c && c[0] && strcmp(s, "*")) {
|
|
|
|
long int x = strtol(c, &e, 16);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-08 20:10:36 -04:00
|
|
|
if ((e && *e) || (x < 0 || x > 0xffff)) {
|
|
|
|
*error = "Invalid class code";
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
f->id.class = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
return -1;
|
2016-06-19 19:23:19 +02:00
|
|
|
}
|
|
|
|
|
2017-03-27 12:57:41 +02:00
|
|
|
int pci_device_compare(const struct pci_device *d, const struct pci_device *f)
|
2016-06-14 01:19:17 +02:00
|
|
|
{
|
2016-10-08 20:10:36 -04:00
|
|
|
if ((f->slot.domain >= 0 && f->slot.domain != d->slot.domain) ||
|
|
|
|
(f->slot.bus >= 0 && f->slot.bus != d->slot.bus) ||
|
|
|
|
(f->slot.device >= 0 && f->slot.device != d->slot.device) ||
|
|
|
|
(f->slot.function >= 0 && f->slot.function != d->slot.function))
|
|
|
|
return 0;
|
2016-06-14 01:19:17 +02:00
|
|
|
|
2016-10-08 20:10:36 -04:00
|
|
|
if (f->id.device >= 0 || f->id.vendor >= 0) {
|
|
|
|
if ((f->id.device >= 0 && f->id.device != d->id.device) || (f->id.vendor >= 0 && f->id.vendor != d->id.vendor))
|
|
|
|
return 0;
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-08 20:10:36 -04:00
|
|
|
if (f->id.class >= 0) {
|
|
|
|
if (f->id.class != d->id.class)
|
|
|
|
return 0;
|
2016-06-14 01:19:17 +02:00
|
|
|
}
|
|
|
|
|
2016-10-08 20:10:36 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-03-27 12:57:41 +02:00
|
|
|
struct pci_device * pci_lookup_device(struct pci *p, struct pci_device *f)
|
2016-10-08 20:10:36 -04:00
|
|
|
{
|
2017-03-27 12:57:41 +02:00
|
|
|
return list_search(&p->devices, (cmp_cb_t) pci_device_compare, (void *) f);
|
2016-06-14 01:19:17 +02:00
|
|
|
}
|
|
|
|
|
2017-03-27 12:57:41 +02:00
|
|
|
int pci_attach_driver(struct pci_device *d, const char *driver)
|
2016-06-14 01:19:17 +02:00
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
char fn[256];
|
|
|
|
|
|
|
|
/* Add new ID to driver */
|
|
|
|
snprintf(fn, sizeof(fn), "%s/bus/pci/drivers/%s/new_id", SYSFS_PATH, driver);
|
|
|
|
f = fopen(fn, "w");
|
|
|
|
if (!f)
|
|
|
|
serror("Failed to add PCI id to %s driver (%s)", driver, fn);
|
|
|
|
|
2016-10-08 20:10:36 -04:00
|
|
|
debug(5, "Adding ID to %s module: %04x %04x", driver, d->id.vendor, d->id.device);
|
|
|
|
fprintf(f, "%04x %04x", d->id.vendor, d->id.device);
|
2016-06-14 01:19:17 +02:00
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
/* Bind to driver */
|
|
|
|
snprintf(fn, sizeof(fn), "%s/bus/pci/drivers/%s/bind", SYSFS_PATH, driver);
|
|
|
|
f = fopen(fn, "w");
|
|
|
|
if (!f)
|
|
|
|
serror("Failed to bind PCI device to %s driver (%s)", driver, fn);
|
|
|
|
|
|
|
|
debug(5, "Bind device to %s driver", driver);
|
2016-10-08 20:10:36 -04:00
|
|
|
fprintf(f, "%04x:%02x:%02x.%x\n", d->slot.domain, d->slot.bus, d->slot.device, d->slot.function);
|
2016-06-14 01:19:17 +02:00
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-27 12:57:41 +02:00
|
|
|
int pci_get_iommu_group(struct pci_device *d)
|
2016-06-14 01:19:17 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char *group, link[1024], sysfs[1024];
|
|
|
|
|
|
|
|
snprintf(sysfs, sizeof(sysfs), "%s/bus/pci/devices/%04x:%02x:%02x.%x/iommu_group", SYSFS_PATH,
|
2016-10-08 20:10:36 -04:00
|
|
|
d->slot.domain, d->slot.bus, d->slot.device, d->slot.function);
|
2016-06-14 01:19:17 +02:00
|
|
|
|
|
|
|
ret = readlink(sysfs, link, sizeof(link));
|
|
|
|
if (ret < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
group = basename(link);
|
|
|
|
|
|
|
|
return atoi(group);
|
|
|
|
}
|