update copyright date to 2013
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_pci.c
index d8a7bcd..a894a73 100644 (file)
@@ -1,7 +1,7 @@
 /*-
  *   BSD LICENSE
  * 
- *   Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
+ *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
  *   All rights reserved.
  * 
  *   Redistribution and use in source and binary forms, with or without 
 #include <rte_memzone.h>
 #include <rte_tailq.h>
 #include <rte_eal.h>
+#include <rte_eal_memconfig.h>
 #include <rte_per_lcore.h>
 #include <rte_lcore.h>
 #include <rte_malloc.h>
 #include <rte_string_fns.h>
 #include <rte_debug.h>
 
-#include "eal_internal_cfg.h"
+#include "eal_filesystem.h"
 #include "eal_private.h"
 
 /**
@@ -85,8 +86,7 @@
 
 #define IGB_UIO_NAME "igb_uio"
 
-#define UIO_NEWID "/sys/bus/pci/drivers/%s/new_id"
-#define UIO_BIND  "/sys/bus/pci/drivers/%s/bind"
+#define UIO_DRV_PATH  "/sys/bus/pci/drivers/%s"
 
 /* maximum time to wait that /dev/uioX appears */
 #define UIO_DEV_WAIT_TIMEOUT 3 /* seconds */
@@ -108,7 +108,6 @@ struct uio_resource {
 TAILQ_HEAD(uio_res_list, uio_resource);
 
 static struct uio_res_list *uio_res_list = NULL;
-static int pci_parse_sysfs_value(const char *filename, unsigned long *val);
 
 /*
  * Check that a kernel module is loaded. Returns 0 on success, or if the
@@ -126,7 +125,8 @@ pci_uio_check_module(const char *module_name)
 
        f = fopen(PROC_MODULES, "r");
        if (f == NULL) {
-               RTE_LOG(ERR, EAL, "Cannot open "PROC_MODULES"\n");
+               RTE_LOG(ERR, EAL, "Cannot open "PROC_MODULES": %s\n", 
+                               strerror(errno));
                return -1;
        }
 
@@ -143,11 +143,51 @@ pci_uio_check_module(const char *module_name)
                }
        }
        fclose(f);
-       RTE_LOG(ERR, EAL, "Cannot find %s in "PROC_MODULES"\n", module_name);
        return -1;
 }
 
 /* bind a PCI to the kernel module driver */
+static int
+pci_bind_device(struct rte_pci_device *dev, char dr_path[])
+{
+       FILE *f;
+       int n;
+       char buf[BUFSIZ];
+       char dev_bind[PATH_MAX];
+       struct rte_pci_addr *loc = &dev->addr;
+
+       RTE_LOG(DEBUG, EAL, "bind PCI device "PCI_PRI_FMT"\n",
+                       loc->domain, loc->bus, loc->devid, loc->function);
+
+       n = rte_snprintf(dev_bind, sizeof(dev_bind), "%s/bind", dr_path);
+       if ((n < 0) || (n >= (int)sizeof(buf))) {
+               RTE_LOG(ERR, EAL, "Cannot rte_snprintf device bind path\n");
+               return -1;
+       }
+
+       f = fopen(dev_bind, "w");
+       if (f == NULL) {
+               RTE_LOG(ERR, EAL, "Cannot open %s\n", dev->previous_dr);
+               return -1;
+       }
+       n = rte_snprintf(buf, sizeof(buf), PCI_PRI_FMT "\n",
+                        loc->domain, loc->bus, loc->devid, loc->function);
+       if ((n < 0) || (n >= (int)sizeof(buf))) {
+               RTE_LOG(ERR, EAL, "Cannot rte_snprintf PCI infos\n");
+               fclose(f);
+               return -1;
+       }
+       if (fwrite(buf, n, 1, f) == 0) {
+               fclose(f);
+               return -1;
+       }
+
+       RTE_LOG(DEBUG, EAL, "Device bound\n");
+
+       fclose(f);
+       return 0;
+}
+
 static int
 pci_uio_bind_device(struct rte_pci_device *dev, const char *module_name)
 {
@@ -156,17 +196,14 @@ pci_uio_bind_device(struct rte_pci_device *dev, const char *module_name)
        char buf[BUFSIZ];
        char uio_newid[PATH_MAX];
        char uio_bind[PATH_MAX];
-       struct rte_pci_addr *loc = &dev->addr;
-
-       RTE_LOG(DEBUG, EAL, "bind PCI device "PCI_PRI_FMT" to %s driver\n",
-               loc->domain, loc->bus, loc->devid, loc->function, module_name);
 
-       n = rte_snprintf(uio_newid, sizeof(uio_newid), UIO_NEWID, module_name);
+       n = rte_snprintf(uio_newid, sizeof(uio_newid), UIO_DRV_PATH "/new_id", module_name);
        if ((n < 0) || (n >= (int)sizeof(uio_newid))) {
                RTE_LOG(ERR, EAL, "Cannot rte_snprintf uio_newid name\n");
                return -1;
        }
-       n = rte_snprintf(uio_bind, sizeof(uio_bind), UIO_BIND, module_name);
+
+       n = rte_snprintf(uio_bind, sizeof(uio_bind), UIO_DRV_PATH, module_name);
        if ((n < 0) || (n >= (int)sizeof(uio_bind))) {
                RTE_LOG(ERR, EAL, "Cannot rte_snprintf uio_bind name\n");
                return -1;
@@ -190,29 +227,11 @@ pci_uio_bind_device(struct rte_pci_device *dev, const char *module_name)
        }
        fclose(f);
 
-       f = fopen(uio_bind, "w");
-       if (f == NULL) {
-               RTE_LOG(ERR, EAL, "Cannot open %s\n", uio_bind);
-               return -1;
-       }
-       n = rte_snprintf(buf, sizeof(buf), PCI_PRI_FMT "\n",
-                        loc->domain, loc->bus, loc->devid, loc->function);
-       if ((n < 0) || (n >= (int)sizeof(buf))) {
-               RTE_LOG(ERR, EAL, "Cannot rte_snprintf PCI infos\n");
-               fclose(f);
-               return -1;
-       }
-       if (fwrite(buf, n, 1, f) == 0) {
-               fclose(f);
-               return -1;
-       }
-
-       RTE_LOG(DEBUG, EAL, "Device bound\n");
-
-       fclose(f);
+       pci_bind_device(dev, uio_bind);
        return 0;
 }
 
+
 /* map a particular resource from a file */
 static void *
 pci_map_resource(struct rte_pci_device *dev, void *requested_addr, const char *devname,
@@ -366,7 +385,7 @@ pci_uio_map_resource(struct rte_pci_device *dev)
        /* get mapping offset */
        rte_snprintf(filename, sizeof(filename),
                 "%s/maps/map0/offset", dirname2);
-       if (pci_parse_sysfs_value(filename, &offset) < 0) {
+       if (eal_parse_sysfs_value(filename, &offset) < 0) {
                RTE_LOG(ERR, EAL, "%s(): cannot parse offset\n",
                        __func__);
                return -1;
@@ -375,7 +394,7 @@ pci_uio_map_resource(struct rte_pci_device *dev)
        /* get mapping size */
        rte_snprintf(filename, sizeof(filename),
                 "%s/maps/map0/size", dirname2);
-       if (pci_parse_sysfs_value(filename, &size) < 0) {
+       if (eal_parse_sysfs_value(filename, &size) < 0) {
                RTE_LOG(ERR, EAL, "%s(): cannot parse size\n",
                        __func__);
                return -1;
@@ -390,7 +409,7 @@ pci_uio_map_resource(struct rte_pci_device *dev)
 
        /* save the mapping details for secondary processes*/
        uio_res = rte_malloc("UIO_RES", sizeof(*uio_res), 0);
-       if (uio_res == NULL){
+       if (uio_res == NULL) {
                RTE_LOG(ERR, EAL, "%s(): cannot store uio mmap details\n", __func__);
                return -1;
        }
@@ -465,38 +484,21 @@ error:
        return -1;
 }
 
-/* parse a sysfs file containing one integer value */
+
+/* Compare two PCI device addresses. */
 static int
-pci_parse_sysfs_value(const char *filename, unsigned long *val)
+pci_addr_comparison(struct rte_pci_addr *addr, struct rte_pci_addr *addr2)
 {
-       FILE *f;
-       char buf[BUFSIZ];
-       char *end = NULL;
-
-       f = fopen(filename, "r");
-       if (f == NULL) {
-               RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
-                       __func__, filename);
-               return -1;
-       }
+        uint64_t dev_addr = (addr->domain << 24) + (addr->bus << 16) + (addr->devid << 8) + addr->function;
+        uint64_t dev_addr2 = (addr2->domain << 24) + (addr2->bus << 16) + (addr2->devid << 8) + addr2->function;
 
-       if (fgets(buf, sizeof(buf), f) == NULL) {
-               RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
-                       __func__, filename);
-               fclose(f);
-               return -1;
-       }
-       *val = strtoul(buf, &end, 0);
-       if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
-               RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
-                               __func__, filename);
-               fclose(f);
-               return -1;
-       }
-       fclose(f);
-       return 0;
+        if (dev_addr > dev_addr2) 
+                return 1;      
+        else 
+                return 0;
 }
 
+
 /* Scan one pci sysfs entry, and fill the devices list from it. */
 static int
 pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
@@ -519,7 +521,7 @@ pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
 
        /* get vendor id */
        rte_snprintf(filename, sizeof(filename), "%s/vendor", dirname);
-       if (pci_parse_sysfs_value(filename, &tmp) < 0) {
+       if (eal_parse_sysfs_value(filename, &tmp) < 0) {
                free(dev);
                return -1;
        }
@@ -527,7 +529,7 @@ pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
 
        /* get device id */
        rte_snprintf(filename, sizeof(filename), "%s/device", dirname);
-       if (pci_parse_sysfs_value(filename, &tmp) < 0) {
+       if (eal_parse_sysfs_value(filename, &tmp) < 0) {
                free(dev);
                return -1;
        }
@@ -536,7 +538,7 @@ pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
        /* get subsystem_vendor id */
        rte_snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
                 dirname);
-       if (pci_parse_sysfs_value(filename, &tmp) < 0) {
+       if (eal_parse_sysfs_value(filename, &tmp) < 0) {
                free(dev);
                return -1;
        }
@@ -545,7 +547,7 @@ pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
        /* get subsystem_device id */
        rte_snprintf(filename, sizeof(filename), "%s/subsystem_device",
                 dirname);
-       if (pci_parse_sysfs_value(filename, &tmp) < 0) {
+       if (eal_parse_sysfs_value(filename, &tmp) < 0) {
                free(dev);
                return -1;
        }
@@ -559,9 +561,24 @@ pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
                return -1;
        }
 
-       /* device is valid, add in list */
-       TAILQ_INSERT_TAIL(&device_list, dev, next);
+       /* device is valid, add in list (sorted) */
+       if (TAILQ_EMPTY(&device_list)) {
+               TAILQ_INSERT_TAIL(&device_list, dev, next);
+       }       
+       else {
+               struct rte_pci_device *dev2 = NULL;
 
+               TAILQ_FOREACH(dev2, &device_list, next) {
+                       if (pci_addr_comparison(&dev->addr, &dev2->addr))
+                               continue;
+                       else {
+                               TAILQ_INSERT_BEFORE(dev2, dev, next);
+                               return 0;
+                       }
+               }
+               TAILQ_INSERT_TAIL(&device_list, dev, next);
+       }
+                               
        return 0;
 }
 
@@ -680,8 +697,11 @@ pci_unbind_kernel_driver(struct rte_pci_device *dev)
                RTE_LOG(ERR, EAL, "%s(): rte_snprintf failed\n", __func__);
                goto error;
        }
-       if (fwrite(buf, n, 1, f) == 0)
+       if (fwrite(buf, n, 1, f) == 0) {
+               RTE_LOG(ERR, EAL, "%s(): could not write to %s\n", __func__,
+                               filename);
                goto error;
+       }
 
        fclose(f);
        return 0;
@@ -691,6 +711,63 @@ error:
        return -1;
 }
 
+static int
+pci_exit_process(struct rte_pci_device *dev)
+{
+       if (munmap(dev->mem_resource.addr, dev->mem_resource.len) == -1){
+               RTE_LOG(ERR, EAL, "Error with munmap\n");
+               return -1;
+       }
+       if (close(dev->intr_handle.fd) == -1){
+               RTE_LOG(ERR, EAL, "Error closing interrupt handle\n");
+               return -1;
+       }
+       if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
+               if (pci_unbind_kernel_driver(dev) < 0){
+                       RTE_LOG(ERR, EAL, "Error unbinding\n");
+                       return -1;
+               }
+               if (pci_bind_device(dev, dev->previous_dr) < 0){
+                       RTE_LOG(ERR, EAL, "Error binding\n");
+                       return -1;
+               }
+       }
+
+       return 0;
+}
+
+static void
+pci_get_previous_driver_path(struct rte_pci_device *dev)
+{
+       int n;
+       char dev_path[PATH_MAX];
+       char dr_path[PATH_MAX];
+       struct rte_pci_addr *loc = &dev->addr;
+
+       n = rte_snprintf(dev_path, sizeof(dev_path), SYSFS_PCI_DEVICES "/"
+                       PCI_PRI_FMT "/driver", loc->domain, loc->bus, loc->devid, loc->function );
+       if ((n < 0) || (n >= (int)sizeof(dev_path)))
+               RTE_LOG(ERR, EAL, "Cannot rte_snprintf device filepath\n");
+
+       n = readlink(dev_path, dr_path, sizeof(dr_path));
+       if ((n < 0) || (n >= (int)sizeof(dr_path))){
+               RTE_LOG(ERR, EAL, "Cannot readlink driver filepath\n");
+               dev->previous_dr[0] = '\0';
+               return;
+       }
+       dr_path[n] = '\0';
+
+       if (dr_path[0] != '/')
+               n = rte_snprintf(dev->previous_dr, sizeof(dev->previous_dr),
+                               SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/%s",
+                               loc->domain, loc->bus, loc->devid, loc->function, dr_path);
+       else
+               n = rte_snprintf(dev->previous_dr, sizeof(dev->previous_dr),
+                               "%s", dr_path);
+       if ((n < 0) || (n >= (int)sizeof(dev_path)))
+               RTE_LOG(ERR, EAL, "Cannot rte_snprintf driver filepath\n");
+}
+
 /*
  * If vendor/device ID match, call the devinit() function of the
  * driver.
@@ -700,39 +777,49 @@ rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *d
 {
        struct rte_pci_id *id_table;
        const char *module_name = NULL;
-       int ret;
+       int uio_status = -1;
 
        if (dr->drv_flags & RTE_PCI_DRV_NEED_IGB_UIO)
                module_name = IGB_UIO_NAME;
 
-       ret = pci_uio_check_module(module_name);
-       if (ret != 0)
-               rte_exit(1, "The %s module is required by the %s driver\n",
-                               module_name, dr->name);
-
        for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
 
                /* check if device's identifiers match the driver's ones */
                if (id_table->vendor_id != dev->id.vendor_id &&
-                   id_table->vendor_id != PCI_ANY_ID)
+                               id_table->vendor_id != PCI_ANY_ID)
                        continue;
                if (id_table->device_id != dev->id.device_id &&
-                   id_table->device_id != PCI_ANY_ID)
+                               id_table->device_id != PCI_ANY_ID)
                        continue;
                if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
-                   id_table->subsystem_vendor_id != PCI_ANY_ID)
+                               id_table->subsystem_vendor_id != PCI_ANY_ID)
                        continue;
                if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
-                   id_table->subsystem_device_id != PCI_ANY_ID)
+                               id_table->subsystem_device_id != PCI_ANY_ID)
                        continue;
 
-               RTE_LOG(DEBUG, EAL, "probe driver: %x:%x %s\n",
-               dev->id.vendor_id, dev->id.device_id, dr->name);
+               RTE_LOG(DEBUG, EAL, "probe driver: %x:%x %s\n", dev->id.vendor_id,
+                               dev->id.device_id, dr->name);
+
+               /* reference driver structure */
+               dev->driver = dr;
+
+               /* no initialization when blacklisted, return without error */
+               if (dev->blacklisted)
+                       return 0;
 
                /* Unbind PCI devices if needed */
                if (module_name != NULL) {
+
                        if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
-                       /* unbind current driver, bind ours */
+                               /* check that our driver is loaded */
+                               if (uio_status != 0 &&
+                                               (uio_status = pci_uio_check_module(module_name)) != 0)
+                                       rte_exit(EXIT_FAILURE, "The %s module is required by the "
+                                                       "%s driver\n", module_name, dr->name);
+
+                               /* unbind current driver, bind ours */
+                               pci_get_previous_driver_path(dev);
                                if (pci_unbind_kernel_driver(dev) < 0)
                                        return -1;
                                if (pci_uio_bind_device(dev, module_name) < 0)
@@ -742,20 +829,34 @@ rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *d
                        if (pci_uio_map_resource(dev) < 0)
                                return -1;
                }
+
                /* call the driver devinit() function */
                return dr->devinit(dr, dev);
-
        }
        return -1;
 }
 
+/*Start the exit process for each dev in use*/
+void
+rte_eal_pci_exit(void)
+{
+       struct rte_pci_device *dev = NULL;
+
+       TAILQ_FOREACH(dev, &device_list, next){
+               if (dev->previous_dr[0] == '\0')
+                       continue;
+               if(pci_exit_process(dev) == 1)
+                       RTE_LOG(ERR, EAL, "Exit process failure\n");
+       }
+}
+
 /* Init the PCI EAL subsystem */
 int
 rte_eal_pci_init(void)
 {
        TAILQ_INIT(&driver_list);
        TAILQ_INIT(&device_list);
-       uio_res_list = RTE_TAILQ_RESERVE("PCI_RESOURCE_LIST", uio_res_list);
+       uio_res_list = RTE_TAILQ_RESERVE_BY_IDX(RTE_TAILQ_PCI, uio_res_list);
 
        /* for debug purposes, PCI can be disabled */
        if (internal_config.no_pci)