[dpdk-dev,v6,13/17] pci: add a helper to update a device

Message ID 1468303282-2806-14-git-send-email-shreyansh.jain@nxp.com (mailing list archive)
State Superseded, archived
Headers

Commit Message

Shreyansh Jain July 12, 2016, 6:01 a.m. UTC
  This helper updates a pci device object with latest information it can
find.
It will be used mainly for hotplug code.

Signed-off-by: David Marchand <david.marchand@6wind.com>
Signed-off-by: Shreyansh Jain <shreyansh.jain@nxp.com>
---
 lib/librte_eal/bsdapp/eal/eal_pci.c     | 49 +++++++++++++++++++++++++++++++++
 lib/librte_eal/common/eal_common_pci.c  |  2 --
 lib/librte_eal/common/eal_private.h     | 13 +++++++++
 lib/librte_eal/common/include/rte_pci.h |  3 ++
 lib/librte_eal/linuxapp/eal/eal_pci.c   | 13 +++++++++
 5 files changed, 78 insertions(+), 2 deletions(-)
  

Comments

Jan Viktorin July 14, 2016, 5:01 p.m. UTC | #1
On Tue, 12 Jul 2016 11:31:18 +0530
Shreyansh Jain <shreyansh.jain@nxp.com> wrote:

> This helper updates a pci device object with latest information it can
> find.
> It will be used mainly for hotplug code.
> 
> Signed-off-by: David Marchand <david.marchand@6wind.com>
> Signed-off-by: Shreyansh Jain <shreyansh.jain@nxp.com>
> ---
>  lib/librte_eal/bsdapp/eal/eal_pci.c     | 49 +++++++++++++++++++++++++++++++++
>  lib/librte_eal/common/eal_common_pci.c  |  2 --
>  lib/librte_eal/common/eal_private.h     | 13 +++++++++
>  lib/librte_eal/common/include/rte_pci.h |  3 ++
>  lib/librte_eal/linuxapp/eal/eal_pci.c   | 13 +++++++++
>  5 files changed, 78 insertions(+), 2 deletions(-)
> 
> diff --git a/lib/librte_eal/bsdapp/eal/eal_pci.c b/lib/librte_eal/bsdapp/eal/eal_pci.c
> index a73cbb0..1d91c78 100644
> --- a/lib/librte_eal/bsdapp/eal/eal_pci.c
> +++ b/lib/librte_eal/bsdapp/eal/eal_pci.c
> @@ -406,6 +406,55 @@ error:
>  	return -1;
>  }
>  
> +int
> +pci_update_device(const struct rte_pci_addr *addr)
> +{
> +	int fd;
> +	struct pci_conf matches[2];
> +	struct pci_match_conf match = {
> +		.pc_sel = {
> +			.pc_domain = addr->domain,
> +			.pc_bus = addr->bus,
> +			.pc_dev = addr->devid,
> +			.pc_func = addr->function,
> +		},
> +	};
> +	struct pci_conf_io conf_io = {
> +		.pat_buf_len = 0,
> +		.num_patterns = 1,
> +		.patterns = &match,
> +		.match_buf_len = sizeof(matches),
> +		.matches = &matches[0],
> +	};
> +
> +	fd = open("/dev/pci", O_RDONLY);
> +	if (fd < 0) {
> +		RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
> +		goto error;
> +	}
> +
> +	if (ioctl(fd, PCIOCGETCONF, &conf_io) < 0) {
> +		RTE_LOG(ERR, EAL, "%s(): error with ioctl on /dev/pci: %s\n",
> +				__func__, strerror(errno));
> +		goto error;
> +	}
> +
> +	if (conf_io.num_matches != 1)
> +		goto error;
> +
> +	if (pci_scan_one(fd, &matches[0]) < 0)
> +		goto error;
> +
> +	close(fd);
> +
> +	return 0;
> +
> +error:
> +	if (fd >= 0)
> +		close(fd);
> +	return -1;
> +}
> +
>  /* Read PCI config space. */
>  int rte_eal_pci_read_config(const struct rte_pci_device *dev,
>  			    void *buf, size_t len, off_t offset)
> diff --git a/lib/librte_eal/common/eal_common_pci.c b/lib/librte_eal/common/eal_common_pci.c
> index 6a0f6ac..58f0c74 100644
> --- a/lib/librte_eal/common/eal_common_pci.c
> +++ b/lib/librte_eal/common/eal_common_pci.c
> @@ -87,8 +87,6 @@ struct pci_driver_list pci_driver_list =
>  struct pci_device_list pci_device_list =
>  	TAILQ_HEAD_INITIALIZER(pci_device_list);
>  
> -#define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
> -

Is this delete correct? The SYSFS_PCI_DEVICES was moved here from
rte_pci.h. A relict of some rebase?

>  const char *pci_get_sysfs_path(void)
>  {
>  	const char *path = NULL;
> diff --git a/lib/librte_eal/common/eal_private.h b/lib/librte_eal/common/eal_private.h
> index 06a68f6..b8ff9c5 100644
> --- a/lib/librte_eal/common/eal_private.h
> +++ b/lib/librte_eal/common/eal_private.h
> @@ -152,6 +152,19 @@ struct rte_pci_driver;
>  struct rte_pci_device;
>  
>  /**
> + * Update a pci device object by asking the kernel for the latest information.
> + *
> + * This function is private to EAL.
> + *
> + * @param addr
> + *	The PCI Bus-Device-Function address to look for
> + * @return
> + *   - 0 on success.
> + *   - negative on error.
> + */
> +int pci_update_device(const struct rte_pci_addr *addr);
> +
> +/**
>   * Unbind kernel driver for this device
>   *
>   * This function is private to EAL.
> diff --git a/lib/librte_eal/common/include/rte_pci.h b/lib/librte_eal/common/include/rte_pci.h
> index 06508fa..5c2062c 100644
> --- a/lib/librte_eal/common/include/rte_pci.h
> +++ b/lib/librte_eal/common/include/rte_pci.h
> @@ -107,6 +107,9 @@ const char *pci_get_sysfs_path(void);
>  /** Nb. of values in PCI resource format. */
>  #define PCI_RESOURCE_FMT_NVAL 3
>  
> +/** Default sysfs path for PCI device search. */
> +#define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
> +

Why adding SYSFS_PCI_DEVICES here? The pci_get_sysfs_path is used for
this purpose. Again, a rebase issue?

>  /**
>   * A structure describing a PCI resource.
>   */
> diff --git a/lib/librte_eal/linuxapp/eal/eal_pci.c b/lib/librte_eal/linuxapp/eal/eal_pci.c
> index f0215ee..8f3ef20 100644
> --- a/lib/librte_eal/linuxapp/eal/eal_pci.c
> +++ b/lib/librte_eal/linuxapp/eal/eal_pci.c
> @@ -417,6 +417,19 @@ pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
>  	return 0;
>  }
>  
> +int
> +pci_update_device(const struct rte_pci_addr *addr)
> +{
> +	char filename[PATH_MAX];
> +
> +	snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT,
> +		 SYSFS_PCI_DEVICES, addr->domain, addr->bus, addr->devid,

Use pci_get_sysfs_path here.

> +		 addr->function);
> +
> +	return pci_scan_one(filename, addr->domain, addr->bus, addr->devid,
> +			    addr->function);
> +}
> +
>  /*
>   * split up a pci address into its constituent parts.
>   */
  
Shreyansh Jain July 15, 2016, 9:44 a.m. UTC | #2
On Thursday 14 July 2016 10:31 PM, Jan Viktorin wrote:
> On Tue, 12 Jul 2016 11:31:18 +0530
> Shreyansh Jain <shreyansh.jain@nxp.com> wrote:
> 
>> This helper updates a pci device object with latest information it can
>> find.
>> It will be used mainly for hotplug code.
>>
>> Signed-off-by: David Marchand <david.marchand@6wind.com>
>> Signed-off-by: Shreyansh Jain <shreyansh.jain@nxp.com>
>> ---
>>  lib/librte_eal/bsdapp/eal/eal_pci.c     | 49 +++++++++++++++++++++++++++++++++
>>  lib/librte_eal/common/eal_common_pci.c  |  2 --
>>  lib/librte_eal/common/eal_private.h     | 13 +++++++++
>>  lib/librte_eal/common/include/rte_pci.h |  3 ++
>>  lib/librte_eal/linuxapp/eal/eal_pci.c   | 13 +++++++++
>>  5 files changed, 78 insertions(+), 2 deletions(-)
>>
>> diff --git a/lib/librte_eal/bsdapp/eal/eal_pci.c b/lib/librte_eal/bsdapp/eal/eal_pci.c
>> index a73cbb0..1d91c78 100644
>> --- a/lib/librte_eal/bsdapp/eal/eal_pci.c
>> +++ b/lib/librte_eal/bsdapp/eal/eal_pci.c
>> @@ -406,6 +406,55 @@ error:
>>  	return -1;
>>  }
>>  
>> +int
>> +pci_update_device(const struct rte_pci_addr *addr)
>> +{
>> +	int fd;
>> +	struct pci_conf matches[2];
>> +	struct pci_match_conf match = {
>> +		.pc_sel = {
>> +			.pc_domain = addr->domain,
>> +			.pc_bus = addr->bus,
>> +			.pc_dev = addr->devid,
>> +			.pc_func = addr->function,
>> +		},
>> +	};
>> +	struct pci_conf_io conf_io = {
>> +		.pat_buf_len = 0,
>> +		.num_patterns = 1,
>> +		.patterns = &match,
>> +		.match_buf_len = sizeof(matches),
>> +		.matches = &matches[0],
>> +	};
>> +
>> +	fd = open("/dev/pci", O_RDONLY);
>> +	if (fd < 0) {
>> +		RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
>> +		goto error;
>> +	}
>> +
>> +	if (ioctl(fd, PCIOCGETCONF, &conf_io) < 0) {
>> +		RTE_LOG(ERR, EAL, "%s(): error with ioctl on /dev/pci: %s\n",
>> +				__func__, strerror(errno));
>> +		goto error;
>> +	}
>> +
>> +	if (conf_io.num_matches != 1)
>> +		goto error;
>> +
>> +	if (pci_scan_one(fd, &matches[0]) < 0)
>> +		goto error;
>> +
>> +	close(fd);
>> +
>> +	return 0;
>> +
>> +error:
>> +	if (fd >= 0)
>> +		close(fd);
>> +	return -1;
>> +}
>> +
>>  /* Read PCI config space. */
>>  int rte_eal_pci_read_config(const struct rte_pci_device *dev,
>>  			    void *buf, size_t len, off_t offset)
>> diff --git a/lib/librte_eal/common/eal_common_pci.c b/lib/librte_eal/common/eal_common_pci.c
>> index 6a0f6ac..58f0c74 100644
>> --- a/lib/librte_eal/common/eal_common_pci.c
>> +++ b/lib/librte_eal/common/eal_common_pci.c
>> @@ -87,8 +87,6 @@ struct pci_driver_list pci_driver_list =
>>  struct pci_device_list pci_device_list =
>>  	TAILQ_HEAD_INITIALIZER(pci_device_list);
>>  
>> -#define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
>> -
> 
> Is this delete correct? The SYSFS_PCI_DEVICES was moved here from
> rte_pci.h. A relict of some rebase?

My miss - I had though of using pci_get_sysfs_path post v5, (as you have mentioned below) but while re-creating the patches I missed out.

> 
>>  const char *pci_get_sysfs_path(void)
>>  {
>>  	const char *path = NULL;
>> diff --git a/lib/librte_eal/common/eal_private.h b/lib/librte_eal/common/eal_private.h
>> index 06a68f6..b8ff9c5 100644
>> --- a/lib/librte_eal/common/eal_private.h
>> +++ b/lib/librte_eal/common/eal_private.h
>> @@ -152,6 +152,19 @@ struct rte_pci_driver;
>>  struct rte_pci_device;
>>  
>>  /**
>> + * Update a pci device object by asking the kernel for the latest information.
>> + *
>> + * This function is private to EAL.
>> + *
>> + * @param addr
>> + *	The PCI Bus-Device-Function address to look for
>> + * @return
>> + *   - 0 on success.
>> + *   - negative on error.
>> + */
>> +int pci_update_device(const struct rte_pci_addr *addr);
>> +
>> +/**
>>   * Unbind kernel driver for this device
>>   *
>>   * This function is private to EAL.
>> diff --git a/lib/librte_eal/common/include/rte_pci.h b/lib/librte_eal/common/include/rte_pci.h
>> index 06508fa..5c2062c 100644
>> --- a/lib/librte_eal/common/include/rte_pci.h
>> +++ b/lib/librte_eal/common/include/rte_pci.h
>> @@ -107,6 +107,9 @@ const char *pci_get_sysfs_path(void);
>>  /** Nb. of values in PCI resource format. */
>>  #define PCI_RESOURCE_FMT_NVAL 3
>>  
>> +/** Default sysfs path for PCI device search. */
>> +#define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
>> +
> 
> Why adding SYSFS_PCI_DEVICES here? The pci_get_sysfs_path is used for
> this purpose. Again, a rebase issue?

Once pci_get_sysfs_path is used, this change would be redundant.

> 
>>  /**
>>   * A structure describing a PCI resource.
>>   */
>> diff --git a/lib/librte_eal/linuxapp/eal/eal_pci.c b/lib/librte_eal/linuxapp/eal/eal_pci.c
>> index f0215ee..8f3ef20 100644
>> --- a/lib/librte_eal/linuxapp/eal/eal_pci.c
>> +++ b/lib/librte_eal/linuxapp/eal/eal_pci.c
>> @@ -417,6 +417,19 @@ pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
>>  	return 0;
>>  }
>>  
>> +int
>> +pci_update_device(const struct rte_pci_addr *addr)
>> +{
>> +	char filename[PATH_MAX];
>> +
>> +	snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT,
>> +		 SYSFS_PCI_DEVICES, addr->domain, addr->bus, addr->devid,
> 
> Use pci_get_sysfs_path here.

My miss. I will change this.

> 
>> +		 addr->function);
>> +
>> +	return pci_scan_one(filename, addr->domain, addr->bus, addr->devid,
>> +			    addr->function);
>> +}
>> +
>>  /*
>>   * split up a pci address into its constituent parts.
>>   */
> 
> 
>
  

Patch

diff --git a/lib/librte_eal/bsdapp/eal/eal_pci.c b/lib/librte_eal/bsdapp/eal/eal_pci.c
index a73cbb0..1d91c78 100644
--- a/lib/librte_eal/bsdapp/eal/eal_pci.c
+++ b/lib/librte_eal/bsdapp/eal/eal_pci.c
@@ -406,6 +406,55 @@  error:
 	return -1;
 }
 
+int
+pci_update_device(const struct rte_pci_addr *addr)
+{
+	int fd;
+	struct pci_conf matches[2];
+	struct pci_match_conf match = {
+		.pc_sel = {
+			.pc_domain = addr->domain,
+			.pc_bus = addr->bus,
+			.pc_dev = addr->devid,
+			.pc_func = addr->function,
+		},
+	};
+	struct pci_conf_io conf_io = {
+		.pat_buf_len = 0,
+		.num_patterns = 1,
+		.patterns = &match,
+		.match_buf_len = sizeof(matches),
+		.matches = &matches[0],
+	};
+
+	fd = open("/dev/pci", O_RDONLY);
+	if (fd < 0) {
+		RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
+		goto error;
+	}
+
+	if (ioctl(fd, PCIOCGETCONF, &conf_io) < 0) {
+		RTE_LOG(ERR, EAL, "%s(): error with ioctl on /dev/pci: %s\n",
+				__func__, strerror(errno));
+		goto error;
+	}
+
+	if (conf_io.num_matches != 1)
+		goto error;
+
+	if (pci_scan_one(fd, &matches[0]) < 0)
+		goto error;
+
+	close(fd);
+
+	return 0;
+
+error:
+	if (fd >= 0)
+		close(fd);
+	return -1;
+}
+
 /* Read PCI config space. */
 int rte_eal_pci_read_config(const struct rte_pci_device *dev,
 			    void *buf, size_t len, off_t offset)
diff --git a/lib/librte_eal/common/eal_common_pci.c b/lib/librte_eal/common/eal_common_pci.c
index 6a0f6ac..58f0c74 100644
--- a/lib/librte_eal/common/eal_common_pci.c
+++ b/lib/librte_eal/common/eal_common_pci.c
@@ -87,8 +87,6 @@  struct pci_driver_list pci_driver_list =
 struct pci_device_list pci_device_list =
 	TAILQ_HEAD_INITIALIZER(pci_device_list);
 
-#define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
-
 const char *pci_get_sysfs_path(void)
 {
 	const char *path = NULL;
diff --git a/lib/librte_eal/common/eal_private.h b/lib/librte_eal/common/eal_private.h
index 06a68f6..b8ff9c5 100644
--- a/lib/librte_eal/common/eal_private.h
+++ b/lib/librte_eal/common/eal_private.h
@@ -152,6 +152,19 @@  struct rte_pci_driver;
 struct rte_pci_device;
 
 /**
+ * Update a pci device object by asking the kernel for the latest information.
+ *
+ * This function is private to EAL.
+ *
+ * @param addr
+ *	The PCI Bus-Device-Function address to look for
+ * @return
+ *   - 0 on success.
+ *   - negative on error.
+ */
+int pci_update_device(const struct rte_pci_addr *addr);
+
+/**
  * Unbind kernel driver for this device
  *
  * This function is private to EAL.
diff --git a/lib/librte_eal/common/include/rte_pci.h b/lib/librte_eal/common/include/rte_pci.h
index 06508fa..5c2062c 100644
--- a/lib/librte_eal/common/include/rte_pci.h
+++ b/lib/librte_eal/common/include/rte_pci.h
@@ -107,6 +107,9 @@  const char *pci_get_sysfs_path(void);
 /** Nb. of values in PCI resource format. */
 #define PCI_RESOURCE_FMT_NVAL 3
 
+/** Default sysfs path for PCI device search. */
+#define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
+
 /**
  * A structure describing a PCI resource.
  */
diff --git a/lib/librte_eal/linuxapp/eal/eal_pci.c b/lib/librte_eal/linuxapp/eal/eal_pci.c
index f0215ee..8f3ef20 100644
--- a/lib/librte_eal/linuxapp/eal/eal_pci.c
+++ b/lib/librte_eal/linuxapp/eal/eal_pci.c
@@ -417,6 +417,19 @@  pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
 	return 0;
 }
 
+int
+pci_update_device(const struct rte_pci_addr *addr)
+{
+	char filename[PATH_MAX];
+
+	snprintf(filename, sizeof(filename), "%s/" PCI_PRI_FMT,
+		 SYSFS_PCI_DEVICES, addr->domain, addr->bus, addr->devid,
+		 addr->function);
+
+	return pci_scan_one(filename, addr->domain, addr->bus, addr->devid,
+			    addr->function);
+}
+
 /*
  * split up a pci address into its constituent parts.
  */