git: 013d817b5393 - stable/14 - igc: Add NVM/firmware prints and sysctl

From: Kevin Bowling <kbowling_at_FreeBSD.org>
Date: Sun, 06 Oct 2024 03:49:26 UTC
The branch stable/14 has been updated by kbowling:

URL: https://cgit.FreeBSD.org/src/commit/?id=013d817b539325017e00ede97d58c8adc16dbee3

commit 013d817b539325017e00ede97d58c8adc16dbee3
Author:     Kevin Bowling <kbowling@FreeBSD.org>
AuthorDate: 2024-09-29 09:23:38 +0000
Commit:     Kevin Bowling <kbowling@FreeBSD.org>
CommitDate: 2024-10-06 03:49:08 +0000

    igc: Add NVM/firmware prints and sysctl
    
    This chipset suffered an (un)usual number of bugs and iterations. Let's
    add our NVM/firmware code from e1000 and the similar igc_nvm function
    from DPDK to keep track of issues.
    
    Sponsored by:   BBOX.io
    
    (cherry picked from commit 33ed9bdca307bedb3d66a50ed7d4d7b4bf4acf39)
---
 sys/dev/igc/if_igc.c      | 104 ++++++++++++++++++++++++++++++++++++++++++++++
 sys/dev/igc/if_igc.h      |   2 +
 sys/dev/igc/igc_defines.h |  28 +++++++++++--
 sys/dev/igc/igc_nvm.c     |  81 ++++++++++++++++++++++++++++++++++++
 sys/dev/igc/igc_nvm.h     |  18 ++++++++
 5 files changed, 229 insertions(+), 4 deletions(-)

diff --git a/sys/dev/igc/if_igc.c b/sys/dev/igc/if_igc.c
index c78edab5e38a..006fecdab7b0 100644
--- a/sys/dev/igc/if_igc.c
+++ b/sys/dev/igc/if_igc.c
@@ -115,6 +115,10 @@ static void	igc_update_stats_counters(struct igc_adapter *);
 static void	igc_add_hw_stats(struct igc_adapter *adapter);
 static int	igc_if_set_promisc(if_ctx_t ctx, int flags);
 static void	igc_setup_vlan_hw_support(if_ctx_t ctx);
+static void	igc_fw_version(struct igc_adapter *);
+static void	igc_sbuf_fw_version(struct igc_fw_version *, struct sbuf *);
+static void	igc_print_fw_version(struct igc_adapter *);
+static int	igc_sysctl_print_fw_version(SYSCTL_HANDLER_ARGS);
 static int	igc_sysctl_nvm_info(SYSCTL_HANDLER_ARGS);
 static void	igc_print_nvm_info(struct igc_adapter *);
 static int	igc_sysctl_debug_info(SYSCTL_HANDLER_ARGS);
@@ -440,6 +444,12 @@ igc_if_attach_pre(if_ctx_t ctx)
 	    OID_AUTO, "nvm", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
 	    adapter, 0, igc_sysctl_nvm_info, "I", "NVM Information");
 
+	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
+	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
+	    OID_AUTO, "fw_version", CTLTYPE_STRING | CTLFLAG_RD,
+	    adapter, 0, igc_sysctl_print_fw_version, "A",
+	    "Prints FW/NVM Versions");
+
 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
 	    OID_AUTO, "debug", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
@@ -586,6 +596,11 @@ igc_if_attach_pre(if_ctx_t ctx)
 		goto err_late;
 	}
 
+	/* Save the EEPROM/NVM versions */
+	igc_fw_version(adapter);
+
+	igc_print_fw_version(adapter);
+
 	/*
 	 * Get Wake-on-Lan and Management info for later use
 	 */
@@ -2612,6 +2627,95 @@ igc_add_hw_stats(struct igc_adapter *adapter)
 			"Rx Desc Min Thresh Count");
 }
 
+static void
+igc_fw_version(struct igc_adapter *sc)
+{
+	struct igc_hw *hw = &sc->hw;
+	struct igc_fw_version *fw_ver = &sc->fw_ver;
+
+	*fw_ver = (struct igc_fw_version){0};
+
+	igc_get_fw_version(hw, fw_ver);
+}
+
+static void
+igc_sbuf_fw_version(struct igc_fw_version *fw_ver, struct sbuf *buf)
+{
+	const char *space = "";
+
+	if (fw_ver->eep_major || fw_ver->eep_minor || fw_ver->eep_build) {
+		sbuf_printf(buf, "EEPROM V%d.%d-%d", fw_ver->eep_major,
+			    fw_ver->eep_minor, fw_ver->eep_build);
+		space = " ";
+	}
+
+	if (fw_ver->invm_major || fw_ver->invm_minor || fw_ver->invm_img_type) {
+		sbuf_printf(buf, "%sNVM V%d.%d imgtype%d",
+			    space, fw_ver->invm_major, fw_ver->invm_minor,
+			    fw_ver->invm_img_type);
+		space = " ";
+	}
+
+	if (fw_ver->or_valid) {
+		sbuf_printf(buf, "%sOption ROM V%d-b%d-p%d",
+			    space, fw_ver->or_major, fw_ver->or_build,
+			    fw_ver->or_patch);
+		space = " ";
+	}
+
+	if (fw_ver->etrack_id)
+		sbuf_printf(buf, "%seTrack 0x%08x", space, fw_ver->etrack_id);
+}
+
+static void
+igc_print_fw_version(struct igc_adapter *sc )
+{
+	device_t dev = sc->dev;
+	struct sbuf *buf;
+	int error = 0;
+
+	buf = sbuf_new_auto();
+	if (!buf) {
+		device_printf(dev, "Could not allocate sbuf for output.\n");
+		return;
+	}
+
+	igc_sbuf_fw_version(&sc->fw_ver, buf);
+
+	error = sbuf_finish(buf);
+	if (error)
+		device_printf(dev, "Error finishing sbuf: %d\n", error);
+	else if (sbuf_len(buf))
+		device_printf(dev, "%s\n", sbuf_data(buf));
+
+	sbuf_delete(buf);
+}
+
+static int
+igc_sysctl_print_fw_version(SYSCTL_HANDLER_ARGS)
+{
+	struct igc_adapter *sc = (struct igc_adapter *)arg1;
+	device_t dev = sc->dev;
+	struct sbuf *buf;
+	int error = 0;
+
+	buf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
+	if (!buf) {
+		device_printf(dev, "Could not allocate sbuf for output.\n");
+		return (ENOMEM);
+	}
+
+	igc_sbuf_fw_version(&sc->fw_ver, buf);
+
+	error = sbuf_finish(buf);
+	if (error)
+		device_printf(dev, "Error finishing sbuf: %d\n", error);
+
+	sbuf_delete(buf);
+
+	return (0);
+}
+
 /**********************************************************************
  *
  *  This routine provides a way to dump out the adapter eeprom,
diff --git a/sys/dev/igc/if_igc.h b/sys/dev/igc/if_igc.h
index 92865b5b9eb1..727699baee5f 100644
--- a/sys/dev/igc/if_igc.h
+++ b/sys/dev/igc/if_igc.h
@@ -332,6 +332,8 @@ struct igc_adapter {
 
 	u64		que_mask;
 
+	struct igc_fw_version	fw_ver;
+
 	struct igc_int_delay_info tx_int_delay;
 	struct igc_int_delay_info tx_abs_int_delay;
 	struct igc_int_delay_info rx_int_delay;
diff --git a/sys/dev/igc/igc_defines.h b/sys/dev/igc/igc_defines.h
index 1701918c3a9c..e9ebd23c41c3 100644
--- a/sys/dev/igc/igc_defines.h
+++ b/sys/dev/igc/igc_defines.h
@@ -967,11 +967,31 @@
 #define IGC_FLASH_UPDATES	2000
 
 /* NVM Word Offsets */
-#define NVM_COMPAT			0x0003
-#define NVM_ID_LED_SETTINGS		0x0004
-#define NVM_FUTURE_INIT_WORD1		0x0019
-#define NVM_COMPAT_VALID_CSUM		0x0001
+#define NVM_COMPAT				0x0003
+#define NVM_ID_LED_SETTINGS			0x0004
+#define NVM_VERSION				0x0005
+#define NVM_FUTURE_INIT_WORD1			0x0019
+#define NVM_COMPAT_VALID_CSUM			0x0001
 #define NVM_FUTURE_INIT_WORD1_VALID_CSUM	0x0040
+#define NVM_ETRACK_WORD				0x0042
+#define NVM_ETRACK_HIWORD			0x0043
+#define NVM_COMB_VER_OFF			0x0083
+#define NVM_COMB_VER_PTR			0x003d
+
+/* NVM version defines */
+#define NVM_MAJOR_MASK			0xF000
+#define NVM_MINOR_MASK			0x0FF0
+#define NVM_IMAGE_ID_MASK		0x000F
+#define NVM_COMB_VER_MASK		0x00FF
+#define NVM_MAJOR_SHIFT			12
+#define NVM_MINOR_SHIFT			4
+#define NVM_COMB_VER_SHFT		8
+#define NVM_VER_INVALID			0xFFFF
+#define NVM_ETRACK_SHIFT		16
+#define NVM_ETRACK_VALID		0x8000
+#define NVM_NEW_DEC_MASK		0x0F00
+#define NVM_HEX_CONV			16
+#define NVM_HEX_TENS			10
 
 #define NVM_INIT_CONTROL2_REG		0x000F
 #define NVM_INIT_CONTROL3_PORT_B	0x0014
diff --git a/sys/dev/igc/igc_nvm.c b/sys/dev/igc/igc_nvm.c
index d86e04ffa0dc..b476a5fdbeac 100644
--- a/sys/dev/igc/igc_nvm.c
+++ b/sys/dev/igc/igc_nvm.c
@@ -716,4 +716,85 @@ static void igc_reload_nvm_generic(struct igc_hw *hw)
 	IGC_WRITE_FLUSH(hw);
 }
 
+/**
+ *  igc_get_fw_version - Get firmware version information
+ *  @hw: pointer to the HW structure
+ *  @fw_vers: pointer to output version structure
+ *
+ *  unsupported/not present features return 0 in version structure
+ **/
+void igc_get_fw_version(struct igc_hw *hw, struct igc_fw_version *fw_vers)
+{
+	u16 eeprom_verh, eeprom_verl, etrack_test, fw_version;
+	u8 q, hval, rem, result;
+	u16 comb_verh, comb_verl, comb_offset;
+
+	memset(fw_vers, 0, sizeof(struct igc_fw_version));
 
+	/*
+	 * basic eeprom version numbers, bits used vary by part and by tool
+	 * used to create the nvm images. Check which data format we have.
+	 */
+	switch (hw->mac.type) {
+	case igc_i225:
+		hw->nvm.ops.read(hw, NVM_ETRACK_HIWORD, 1, &etrack_test);
+		/* find combo image version */
+		hw->nvm.ops.read(hw, NVM_COMB_VER_PTR, 1, &comb_offset);
+		if (comb_offset && comb_offset != NVM_VER_INVALID) {
+			hw->nvm.ops.read(hw, NVM_COMB_VER_OFF + comb_offset + 1,
+					1, &comb_verh);
+			hw->nvm.ops.read(hw, NVM_COMB_VER_OFF + comb_offset,
+					1, &comb_verl);
+
+			/* get Option Rom version if it exists and is valid */
+			if (comb_verh && comb_verl &&
+					comb_verh != NVM_VER_INVALID &&
+					comb_verl != NVM_VER_INVALID) {
+				fw_vers->or_valid = true;
+				fw_vers->or_major = comb_verl >>
+						NVM_COMB_VER_SHFT;
+				fw_vers->or_build = (comb_verl <<
+						NVM_COMB_VER_SHFT) |
+						(comb_verh >>
+						NVM_COMB_VER_SHFT);
+				fw_vers->or_patch = comb_verh &
+						NVM_COMB_VER_MASK;
+			}
+		}
+		break;
+	default:
+		hw->nvm.ops.read(hw, NVM_ETRACK_HIWORD, 1, &etrack_test);
+		return;
+	}
+	hw->nvm.ops.read(hw, NVM_VERSION, 1, &fw_version);
+	fw_vers->eep_major = (fw_version & NVM_MAJOR_MASK)
+			      >> NVM_MAJOR_SHIFT;
+
+	/* check for old style version format in newer images*/
+	if ((fw_version & NVM_NEW_DEC_MASK) == 0x0) {
+		eeprom_verl = (fw_version & NVM_COMB_VER_MASK);
+	} else {
+		eeprom_verl = (fw_version & NVM_MINOR_MASK)
+				>> NVM_MINOR_SHIFT;
+	}
+	/* Convert minor value to hex before assigning to output struct
+	 * Val to be converted will not be higher than 99, per tool output
+	 */
+	q = eeprom_verl / NVM_HEX_CONV;
+	hval = q * NVM_HEX_TENS;
+	rem = eeprom_verl % NVM_HEX_CONV;
+	result = hval + rem;
+	fw_vers->eep_minor = result;
+
+	if ((etrack_test &  NVM_MAJOR_MASK) == NVM_ETRACK_VALID) {
+		hw->nvm.ops.read(hw, NVM_ETRACK_WORD, 1, &eeprom_verl);
+		hw->nvm.ops.read(hw, (NVM_ETRACK_WORD + 1), 1, &eeprom_verh);
+		fw_vers->etrack_id = (eeprom_verh << NVM_ETRACK_SHIFT)
+			| eeprom_verl;
+	} else if ((etrack_test & NVM_ETRACK_VALID) == 0) {
+		hw->nvm.ops.read(hw, NVM_ETRACK_WORD, 1, &eeprom_verh);
+		hw->nvm.ops.read(hw, (NVM_ETRACK_WORD + 1), 1, &eeprom_verl);
+		fw_vers->etrack_id = (eeprom_verh << NVM_ETRACK_SHIFT) |
+				     eeprom_verl;
+	}
+}
diff --git a/sys/dev/igc/igc_nvm.h b/sys/dev/igc/igc_nvm.h
index eae0db4b484b..b4b602af6595 100644
--- a/sys/dev/igc/igc_nvm.h
+++ b/sys/dev/igc/igc_nvm.h
@@ -7,6 +7,22 @@
 #ifndef _IGC_NVM_H_
 #define _IGC_NVM_H_
 
+struct igc_fw_version {
+	u32 etrack_id;
+	u16 eep_major;
+	u16 eep_minor;
+	u16 eep_build;
+
+	u8 invm_major;
+	u8 invm_minor;
+	u8 invm_img_type;
+
+	bool or_valid;
+	u16 or_major;
+	u16 or_build;
+	u16 or_patch;
+};
+
 void igc_init_nvm_ops_generic(struct igc_hw *hw);
 s32  igc_null_read_nvm(struct igc_hw *hw, u16 a, u16 b, u16 *c);
 void igc_null_nvm_generic(struct igc_hw *hw);
@@ -26,5 +42,7 @@ s32  igc_write_nvm_spi(struct igc_hw *hw, u16 offset, u16 words,
 			 u16 *data);
 s32  igc_update_nvm_checksum_generic(struct igc_hw *hw);
 void igc_release_nvm_generic(struct igc_hw *hw);
+void igc_get_fw_version(struct igc_hw *hw,
+			  struct igc_fw_version *fw_vers);
 
 #endif