Skip to contentSkip to conversation

PCI/VPD: Final extensions and cleanups

PATCH Applied Heiner Kallweit 2 participants 9 messages Aug 26, 2021 – Sep 2, 2021

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover PCI/VPD: Final extensions and cleanups

This series finalizes the VPD extensions and cleanups. It should be applied via the PCI tree.

Heiner Kallweit (7):
PCI/VPD: Stop exporting pci_vpd_find_tag() PCI/VPD: Stop exporting pci_vpd_find_info_keyword() PCI/VPD: Include post-processing in pci_vpd_find_tag() PCI/VPD: Add pci_vpd_find_id_string()
cxgb4: Use pci_vpd_find_id_string() to find VPD id string PCI/VPD: Clean up public VPD defines and inline functions PCI/VPD: Use unaligned access helpers

 drivers/net/ethernet/chelsio/cxgb4/t4_hw.c | 13 ++-
 drivers/pci/vpd.c                          | 71 +++++++++++-----
 include/linux/pci.h                        | 95 ++--------------------
 3 files changed, 61 insertions(+), 118 deletions(-)
Signature · 1 line
2.33.0
#1 lore raw

PATCH 1/7 PCI/VPD: Stop exporting pci_vpd_find_tag()

Now that the last users have been migrated to pci_vpd_find_ro_keyword() we can stop exporting this function. It's still used in VPD core code.

---
 drivers/pci/vpd.c   |  3 +--
 include/linux/pci.h | 11 -----------
 2 files changed, 1 insertion(+), 13 deletions(-)
drivers/pci/vpd.c +1 -2
index 01e575947..5726fbb7a 100644
@@ -296,7 +296,7 @@ void *pci_vpd_alloc(struct pci_dev *dev, unsigned int *size)
 }
 EXPORT_SYMBOL_GPL(pci_vpd_alloc);
 
-int pci_vpd_find_tag(const u8 *buf, unsigned int len, u8 rdt)
+static int pci_vpd_find_tag(const u8 *buf, unsigned int len, u8 rdt)
 {
 	int i = 0;
 
@@ -310,7 +310,6 @@ int pci_vpd_find_tag(const u8 *buf, unsigned int len, u8 rdt)
 
 	return -ENOENT;
 }
-EXPORT_SYMBOL_GPL(pci_vpd_find_tag);
 
 int pci_vpd_find_info_keyword(const u8 *buf, unsigned int off,
 			      unsigned int len, const char *kw)
include/linux/pci.h +0 -11
index 76e3a9254..2c0260884 100644
@@ -2350,17 +2350,6 @@ static inline u8 pci_vpd_info_field_size(const u8 *info_field)
  */
 void *pci_vpd_alloc(struct pci_dev *dev, unsigned int *size);
 
-/**
- * pci_vpd_find_tag - Locates the Resource Data Type tag provided
- * @buf: Pointer to buffered vpd data
- * @len: The length of the vpd buffer
- * @rdt: The Resource Data Type to search for
- *
- * Returns the index where the Resource Data Type was found or
- * -ENOENT otherwise.
- */
-int pci_vpd_find_tag(const u8 *buf, unsigned int len, u8 rdt);
-
 /**
  * pci_vpd_find_info_keyword - Locates an information field keyword in the VPD
  * @buf: Pointer to buffered vpd data
Signature · 1 line
2.33.0
#2 lore raw

PATCH 2/7 PCI/VPD: Stop exporting pci_vpd_find_info_keyword()

In reply to Heiner Kallweit

Now that the last users have been migrated to pci_vpd_find_ro_keyword() we can stop exporting this function. It's still used in VPD core code.

---
 drivers/pci/vpd.c   |  3 +--
 include/linux/pci.h | 13 -------------
 2 files changed, 1 insertion(+), 15 deletions(-)
drivers/pci/vpd.c +1 -2
index 5726fbb7a..0e7a5e8a8 100644
@@ -311,7 +311,7 @@ static int pci_vpd_find_tag(const u8 *buf, unsigned int len, u8 rdt)
 	return -ENOENT;
 }
 
-int pci_vpd_find_info_keyword(const u8 *buf, unsigned int off,
+static int pci_vpd_find_info_keyword(const u8 *buf, unsigned int off,
 			      unsigned int len, const char *kw)
 {
 	int i;
@@ -327,7 +327,6 @@ int pci_vpd_find_info_keyword(const u8 *buf, unsigned int off,
 
 	return -ENOENT;
 }
-EXPORT_SYMBOL_GPL(pci_vpd_find_info_keyword);
 
 /**
  * pci_read_vpd - Read one entry from Vital Product Data
include/linux/pci.h +0 -13
index 2c0260884..0d6c45b1b 100644
@@ -2350,19 +2350,6 @@ static inline u8 pci_vpd_info_field_size(const u8 *info_field)
  */
 void *pci_vpd_alloc(struct pci_dev *dev, unsigned int *size);
 
-/**
- * pci_vpd_find_info_keyword - Locates an information field keyword in the VPD
- * @buf: Pointer to buffered vpd data
- * @off: The offset into the buffer at which to begin the search
- * @len: The length of the buffer area, relative to off, in which to search
- * @kw: The keyword to search for
- *
- * Returns the index where the information field keyword was found or
- * -ENOENT otherwise.
- */
-int pci_vpd_find_info_keyword(const u8 *buf, unsigned int off,
-			      unsigned int len, const char *kw);
-
 /**
  * pci_vpd_find_ro_info_keyword - Locate info field keyword in VPD RO section
  * @buf: Pointer to buffered VPD data
Signature · 1 line
2.33.0
#3 lore raw

PATCH 3/7 PCI/VPD: Include post-processing in pci_vpd_find_tag()

In reply to Heiner Kallweit

Move pci_vpd_find_tag() post-processing from pci_vpd_find_ro_info_keyword() to pci_vpd_find_tag(). This simplifies function pci_vpd_find_id_string() that will be added in a subsequent patch.

---
 drivers/pci/vpd.c | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)
drivers/pci/vpd.c +13 -10
index 0e7a5e8a8..b7bf014cc 100644
@@ -296,16 +296,25 @@ void *pci_vpd_alloc(struct pci_dev *dev, unsigned int *size)
 }
 EXPORT_SYMBOL_GPL(pci_vpd_alloc);
 
-static int pci_vpd_find_tag(const u8 *buf, unsigned int len, u8 rdt)
+static int pci_vpd_find_tag(const u8 *buf, unsigned int len, u8 rdt, unsigned int *size)
 {
 	int i = 0;
 
 	/* look for LRDT tags only, end tag is the only SRDT tag */
 	while (i + PCI_VPD_LRDT_TAG_SIZE <= len && buf[i] & PCI_VPD_LRDT) {
-		if (buf[i] == rdt)
+		unsigned int lrdt_len = pci_vpd_lrdt_size(buf + i);
+		u8 tag = buf[i];
+
+		i += PCI_VPD_LRDT_TAG_SIZE;
+		if (tag == rdt) {
+			if (i + lrdt_len > len)
+				lrdt_len = len - i;
+			if (size)
+				*size = lrdt_len;
 			return i;
+		}
 
-		i += PCI_VPD_LRDT_TAG_SIZE + pci_vpd_lrdt_size(buf + i);
+		i += lrdt_len;
 	}
 
 	return -ENOENT;
@@ -384,16 +393,10 @@ int pci_vpd_find_ro_info_keyword(const void *buf, unsigned int len,
 	int ro_start, infokw_start;
 	unsigned int ro_len, infokw_size;
 
-	ro_start = pci_vpd_find_tag(buf, len, PCI_VPD_LRDT_RO_DATA);
+	ro_start = pci_vpd_find_tag(buf, len, PCI_VPD_LRDT_RO_DATA, &ro_len);
 	if (ro_start < 0)
 		return ro_start;
 
-	ro_len = pci_vpd_lrdt_size(buf + ro_start);
-	ro_start += PCI_VPD_LRDT_TAG_SIZE;
-
-	if (ro_start + ro_len > len)
-		ro_len = len - ro_start;
-
 	infokw_start = pci_vpd_find_info_keyword(buf, ro_start, ro_len, kw);
 	if (infokw_start < 0)
 		return infokw_start;
Signature · 1 line
2.33.0
#4 lore raw

PATCH 4/7 PCI/VPD: Add pci_vpd_find_id_string()

In reply to Heiner Kallweit

Similar to pci_vpd_find_ro_info_keyword() provide an API function to retrieve the ID string from VPD. This way callers don't have to deal with low-level function pci_vpd_lrdt_size() any longer that can be made private to the VPD core in a subsequent patch.

---
 drivers/pci/vpd.c   |  6 ++++++
 include/linux/pci.h | 10 ++++++++++
 2 files changed, 16 insertions(+)
drivers/pci/vpd.c +6 -0
index b7bf014cc..79712b3d1 100644
@@ -320,6 +320,12 @@ static int pci_vpd_find_tag(const u8 *buf, unsigned int len, u8 rdt, unsigned in
 	return -ENOENT;
 }
 
+int pci_vpd_find_id_string(const u8 *buf, unsigned int len, unsigned int *size)
+{
+	return pci_vpd_find_tag(buf, len, PCI_VPD_LRDT_ID_STRING, size);
+}
+EXPORT_SYMBOL_GPL(pci_vpd_find_id_string);
+
 static int pci_vpd_find_info_keyword(const u8 *buf, unsigned int off,
 			      unsigned int len, const char *kw)
 {
include/linux/pci.h +10 -0
index 0d6c45b1b..f83930562 100644
@@ -2350,6 +2350,16 @@ static inline u8 pci_vpd_info_field_size(const u8 *info_field)
  */
 void *pci_vpd_alloc(struct pci_dev *dev, unsigned int *size);
 
+/**
+ * pci_vpd_find_id_string - Locate id string in VPD
+ * @buf: Pointer to buffered VPD data
+ * @len: The length of the buffer area in which to search
+ * @size: Pointer to field where length of id string is returned
+ *
+ * Returns the index of the id string or -ENOENT if not found.
+ */
+int pci_vpd_find_id_string(const u8 *buf, unsigned int len, unsigned int *size);
+
 /**
  * pci_vpd_find_ro_info_keyword - Locate info field keyword in VPD RO section
  * @buf: Pointer to buffered VPD data
Signature · 1 line
2.33.0
#5 lore raw

PATCH 5/7 cxgb4: Use pci_vpd_find_id_string() to find VPD id string

In reply to Heiner Kallweit

Use new VPD API function pci_vpd_find_id_string() for finding the VPD id string. This simplifies the code and avoids using VPD low-level function pci_vpd_lrdt_size().

---
 drivers/net/ethernet/chelsio/cxgb4/t4_hw.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
drivers/net/ethernet/chelsio/cxgb4/t4_hw.c +5 -8
index 5e8ac42ac..64144b617 100644
@@ -2744,7 +2744,7 @@ int t4_seeprom_wp(struct adapter *adapter, bool enable)
 int t4_get_raw_vpd_params(struct adapter *adapter, struct vpd_params *p)
 {
 	unsigned int id_len, pn_len, sn_len, na_len;
-	int sn, pn, na, addr, ret = 0;
+	int id, sn, pn, na, addr, ret = 0;
 	u8 *vpd, base_val = 0;
 
 	vpd = vmalloc(VPD_LEN);
@@ -2764,13 +2764,10 @@ int t4_get_raw_vpd_params(struct adapter *adapter, struct vpd_params *p)
 	if (ret < 0)
 		goto out;
 
-	if (vpd[0] != PCI_VPD_LRDT_ID_STRING) {
-		dev_err(adapter->pdev_dev, "missing VPD ID string\n");
-		ret = -EINVAL;
+	ret = pci_vpd_find_id_string(vpd, VPD_LEN, &id_len);
+	if (ret < 0)
 		goto out;
-	}
-
-	id_len = pci_vpd_lrdt_size(vpd);
+	id = ret;
 
 	ret = pci_vpd_check_csum(vpd, VPD_LEN);
 	if (ret) {
@@ -2796,7 +2793,7 @@ int t4_get_raw_vpd_params(struct adapter *adapter, struct vpd_params *p)
 		goto out;
 	na = ret;
 
-	memcpy(p->id, vpd + PCI_VPD_LRDT_TAG_SIZE, min_t(int, id_len, ID_LEN));
+	memcpy(p->id, vpd + id, min_t(int, id_len, ID_LEN));
 	strim(p->id);
 	memcpy(p->sn, vpd + sn, min_t(int, sn_len, SERNUM_LEN));
 	strim(p->sn);
Signature · 1 line
2.33.0
#6 lore raw

PATCH 6/7 PCI/VPD: Clean up public VPD defines and inline functions

In reply to Heiner Kallweit

After recent introduction of new VPD API functions and user migration these defines and inline functions aren't used outside VPD core any longer.

---
 drivers/pci/vpd.c   | 26 +++++++++++++++++
 include/linux/pci.h | 69 ---------------------------------------------
 2 files changed, 26 insertions(+), 69 deletions(-)
drivers/pci/vpd.c +26 -0
index 79712b3d1..ff600dff4 100644
@@ -11,6 +11,32 @@
 #include <linux/sched/signal.h>
 #include "pci.h"
 
+#define PCI_VPD_LRDT_TAG_SIZE		3
+#define PCI_VPD_SRDT_LEN_MASK		0x07
+#define PCI_VPD_SRDT_TAG_SIZE		1
+#define PCI_VPD_STIN_END		0x0f
+#define PCI_VPD_INFO_FLD_HDR_SIZE	3
+
+static u16 pci_vpd_lrdt_size(const u8 *lrdt)
+{
+	return (u16)lrdt[1] + ((u16)lrdt[2] << 8);
+}
+
+static u8 pci_vpd_srdt_tag(const u8 *srdt)
+{
+	return *srdt >> 3;
+}
+
+static u8 pci_vpd_srdt_size(const u8 *srdt)
+{
+	return *srdt & PCI_VPD_SRDT_LEN_MASK;
+}
+
+static u8 pci_vpd_info_field_size(const u8 *info_field)
+{
+	return info_field[2];
+}
+
 /* VPD access through PCI 2.2+ VPD capability */
 
 static struct pci_dev *pci_get_func0_dev(struct pci_dev *dev)
include/linux/pci.h +0 -69

81 lines not on this page · raw message

Signature · 1 line
2.33.0
#7 lore raw

PATCH 7/7 PCI/VPD: Use unaligned access helpers

In reply to Heiner Kallweit

Use unaligned access helpers to simplify the code.

---
 drivers/pci/vpd.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
drivers/pci/vpd.c +5 -9
index ff600dff4..25557b272 100644
@@ -9,6 +9,7 @@
 #include <linux/delay.h>
 #include <linux/export.h>
 #include <linux/sched/signal.h>
+#include <asm/unaligned.h>
 #include "pci.h"
 
 #define PCI_VPD_LRDT_TAG_SIZE		3
@@ -19,7 +20,7 @@
 
 static u16 pci_vpd_lrdt_size(const u8 *lrdt)
 {
-	return (u16)lrdt[1] + ((u16)lrdt[2] << 8);
+	return get_unaligned_le16(lrdt + 1);
 }
 
 static u8 pci_vpd_srdt_tag(const u8 *srdt)
@@ -218,14 +219,8 @@ static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count,
 		return -EINTR;
 
 	while (pos < end) {
-		u32 val;
-
-		val = *buf++;
-		val |= *buf++ << 8;
-		val |= *buf++ << 16;
-		val |= *buf++ << 24;
-
-		ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA, val);
+		ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA,
+						  get_unaligned_le32(buf));
 		if (ret < 0)
 			break;
 		ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
@@ -237,6 +232,7 @@ static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count,
 		if (ret < 0)
 			break;
 
+		buf += sizeof(u32);
 		pos += sizeof(u32);
 	}
 
Signature · 1 line
2.33.0
#8 lore raw
In reply to Heiner Kallweit
Quoted 12 lines · Heiner Kallweit

This series finalizes the VPD extensions and cleanups. It should be applied via the PCI tree.

Heiner Kallweit (7):
PCI/VPD: Stop exporting pci_vpd_find_tag() PCI/VPD: Stop exporting pci_vpd_find_info_keyword() PCI/VPD: Include post-processing in pci_vpd_find_tag() PCI/VPD: Add pci_vpd_find_id_string()
cxgb4: Use pci_vpd_find_id_string() to find VPD id string PCI/VPD: Clean up public VPD defines and inline functions PCI/VPD: Use unaligned access helpers

Quoted 4 lines
 drivers/net/ethernet/chelsio/cxgb4/t4_hw.c | 13 ++-
 drivers/pci/vpd.c                          | 71 +++++++++++-----
 include/linux/pci.h                        | 95 ++--------------------
 3 files changed, 61 insertions(+), 118 deletions(-)

Applied to pci/vpd for v5.15, thanks!