Skip to contentSkip to conversation

amd-xgbe: add support for AMD Crater

PATCH Superseded Raju Rangoju 3 participants 14 messages Dec 12, 2023 – Dec 15, 2023

Subsystems (3)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Raju Rangoju

author maintainer
#0 lore raw

v3 cover amd-xgbe: add support for AMD Crater

Add support for a new AMD Ethernet device called "Crater". It has a new PCI ID, add this to the current list of supported devices in the amd-xgbe devices.Also, the BAR1 addresses cannot be used to access the PCS registers on Crater platform, use the indirect addressing via SMN instead.

Changes since v2:
- Club patches 2 and 3 to avoid build warnings.
- Fix the smatch warnings.
- Fix the build errors on arm64

Raju Rangoju (3):
amd-xgbe: reorganize the code of XPCS access
amd-xgbe: add support for Crater ethernet device
amd-xgbe: use smn functions to avoid race

 drivers/net/ethernet/amd/xgbe/xgbe-common.h |   5 +
 drivers/net/ethernet/amd/xgbe/xgbe-dev.c    | 119 ++++++++++++++++----
 drivers/net/ethernet/amd/xgbe/xgbe-pci.c    |  51 ++++++++-
 drivers/net/ethernet/amd/xgbe/xgbe-smn.h    |  27 +++++
 drivers/net/ethernet/amd/xgbe/xgbe.h        |   6 +
 5 files changed, 180 insertions(+), 28 deletions(-)
 create mode 100644 drivers/net/ethernet/amd/xgbe/xgbe-smn.h
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH v3 1/3 amd-xgbe: reorganize the code of XPCS access

The xgbe_{read/write}_mmd_regs_v* functions have common code which can be moved to helper functions. Also, the xgbe_pci_probe() needs reorganization.

Add new helper functions to calculate the mmd_address for v1/v2 of xpcs access. And, convert if/else statements in xgbe_pci_probe() to switch case. This helps code look cleaner.

---
 drivers/net/ethernet/amd/xgbe/xgbe-dev.c | 43 ++++++++++++------------
 drivers/net/ethernet/amd/xgbe/xgbe-pci.c | 18 +++++++---
 2 files changed, 34 insertions(+), 27 deletions(-)
drivers/net/ethernet/amd/xgbe/xgbe-dev.c +21 -22
index f393228d41c7..6cd003c24a64 100644
@@ -1150,6 +1150,21 @@ static int xgbe_set_gpio(struct xgbe_prv_data *pdata, unsigned int gpio)
 	return 0;
 }
 
+static unsigned int get_mmd_address(struct xgbe_prv_data *pdata, int mmd_reg)
+{
+	return (mmd_reg & XGBE_ADDR_C45) ?
+		mmd_reg & ~XGBE_ADDR_C45 :
+		(pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
+}
+
+static unsigned int get_index_offset(struct xgbe_prv_data *pdata, unsigned int mmd_address,
+				     unsigned int *index)
+{
+	mmd_address <<= 1;
+	*index = mmd_address & ~pdata->xpcs_window_mask;
+	return pdata->xpcs_window + (mmd_address & pdata->xpcs_window_mask);
+}
+
 static int xgbe_read_mmd_regs_v2(struct xgbe_prv_data *pdata, int prtad,
 				 int mmd_reg)
 {
@@ -1157,10 +1172,7 @@ static int xgbe_read_mmd_regs_v2(struct xgbe_prv_data *pdata, int prtad,
 	unsigned int mmd_address, index, offset;
 	int mmd_data;
 
-	if (mmd_reg & XGBE_ADDR_C45)
-		mmd_address = mmd_reg & ~XGBE_ADDR_C45;
-	else
-		mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
+	mmd_address = get_mmd_address(pdata, mmd_reg);
 
 	/* The PCS registers are accessed using mmio. The underlying
 	 * management interface uses indirect addressing to access the MMD
@@ -1171,9 +1183,7 @@ static int xgbe_read_mmd_regs_v2(struct xgbe_prv_data *pdata, int prtad,
 	 * register offsets must therefore be adjusted by left shifting the
 	 * offset 1 bit and reading 16 bits of data.
 	 */
-	mmd_address <<= 1;
-	index = mmd_address & ~pdata->xpcs_window_mask;
-	offset = pdata->xpcs_window + (mmd_address & pdata->xpcs_window_mask);
+	offset = get_index_offset(pdata, mmd_address, &index);
 
 	spin_lock_irqsave(&pdata->xpcs_lock, flags);
 	XPCS32_IOWRITE(pdata, pdata->xpcs_window_sel_reg, index);
@@ -1189,10 +1199,7 @@ static void xgbe_write_mmd_regs_v2(struct xgbe_prv_data *pdata, int prtad,
 	unsigned long flags;
 	unsigned int mmd_address, index, offset;
 
-	if (mmd_reg & XGBE_ADDR_C45)
-		mmd_address = mmd_reg & ~XGBE_ADDR_C45;
-	else
-		mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
+	mmd_address = get_mmd_address(pdata, mmd_reg);
 
 	/* The PCS registers are accessed using mmio. The underlying
 	 * management interface uses indirect addressing to access the MMD
@@ -1203,9 +1210,7 @@ static void xgbe_write_mmd_regs_v2(struct xgbe_prv_data *pdata, int prtad,
 	 * register offsets must therefore be adjusted by left shifting the
 	 * offset 1 bit and writing 16 bits of data.
 	 */
-	mmd_address <<= 1;
-	index = mmd_address & ~pdata->xpcs_window_mask;
-	offset = pdata->xpcs_window + (mmd_address & pdata->xpcs_window_mask);
+	offset = get_index_offset(pdata, mmd_address, &index);
 
 	spin_lock_irqsave(&pdata->xpcs_lock, flags);
 	XPCS32_IOWRITE(pdata, pdata->xpcs_window_sel_reg, index);
@@ -1220,10 +1225,7 @@ static int xgbe_read_mmd_regs_v1(struct xgbe_prv_data *pdata, int prtad,
 	unsigned int mmd_address;
 	int mmd_data;
 
-	if (mmd_reg & XGBE_ADDR_C45)
-		mmd_address = mmd_reg & ~XGBE_ADDR_C45;
-	else
-		mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
+	mmd_address = get_mmd_address(pdata, mmd_reg);
 
 	/* The PCS registers are accessed using mmio. The underlying APB3
 	 * management interface uses indirect addressing to access the MMD
@@ -1248,10 +1250,7 @@ static void xgbe_write_mmd_regs_v1(struct xgbe_prv_data *pdata, int prtad,
 	unsigned int mmd_address;
 	unsigned long flags;
 
-	if (mmd_reg & XGBE_ADDR_C45)
-		mmd_address = mmd_reg & ~XGBE_ADDR_C45;
-	else
-		mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
+	mmd_address = get_mmd_address(pdata, mmd_reg);
 
 	/* The PCS registers are accessed using mmio. The underlying APB3
 	 * management interface uses indirect addressing to access the MMD
drivers/net/ethernet/amd/xgbe/xgbe-pci.c +13 -5
index f409d7bd1f1e..8b0c1e450b7e 100644
@@ -274,12 +274,18 @@ static int xgbe_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 
 	/* Set the PCS indirect addressing definition registers */
 	rdev = pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(0, 0));
-	if (rdev &&
-	    (rdev->vendor == PCI_VENDOR_ID_AMD) && (rdev->device == 0x15d0)) {
+
+	if (!(rdev && rdev->vendor == PCI_VENDOR_ID_AMD)) {
+		ret = -ENODEV;
+		goto err_pci_enable;
+	}
+
+	switch (rdev->device) {
+	case 0x15d0:
 		pdata->xpcs_window_def_reg = PCS_V2_RV_WINDOW_DEF;
 		pdata->xpcs_window_sel_reg = PCS_V2_RV_WINDOW_SELECT;
-	} else if (rdev && (rdev->vendor == PCI_VENDOR_ID_AMD) &&
-		   (rdev->device == 0x14b5)) {
+		break;
+	case 0x14b5:
 		pdata->xpcs_window_def_reg = PCS_V2_YC_WINDOW_DEF;
 		pdata->xpcs_window_sel_reg = PCS_V2_YC_WINDOW_SELECT;
 
@@ -288,9 +294,11 @@ static int xgbe_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 
 		/* Yellow Carp devices do not need rrc */
 		pdata->vdata->enable_rrc = 0;
-	} else {
+		break;
+	default:
 		pdata->xpcs_window_def_reg = PCS_V2_WINDOW_DEF;
 		pdata->xpcs_window_sel_reg = PCS_V2_WINDOW_SELECT;
+		break;
 	}
 	pci_dev_put(rdev);
 
Signature · 1 line
2.34.1
author maintainer
#6 lore raw

PATCH v3 2/3 amd-xgbe: add support for Crater ethernet device

In reply to Raju Rangoju

Add the necessary support to enable Crater ethernet device. Since the BAR1 address cannot be used to access the XPCS registers on Crater, use the pci_{read/write}_config_dword calls. Also, include the new pci device id 0x1641 to register Crater device with PCIe.

---
 drivers/net/ethernet/amd/xgbe/xgbe-common.h |  5 ++
 drivers/net/ethernet/amd/xgbe/xgbe-dev.c    | 93 +++++++++++++++++++++
 drivers/net/ethernet/amd/xgbe/xgbe-pci.c    | 35 +++++++-
 drivers/net/ethernet/amd/xgbe/xgbe.h        |  6 ++
 4 files changed, 137 insertions(+), 2 deletions(-)
drivers/net/ethernet/amd/xgbe/xgbe-common.h +5 -0
index 3b70f6737633..e1f70f0528ef 100644
@@ -900,6 +900,11 @@
 #define PCS_V2_RV_WINDOW_SELECT		0x1064
 #define PCS_V2_YC_WINDOW_DEF		0x18060
 #define PCS_V2_YC_WINDOW_SELECT		0x18064
+#define PCS_V2_RN_WINDOW_DEF		0xF8078
+#define PCS_V2_RN_WINDOW_SELECT		0xF807c
+
+#define PCS_RN_SMN_BASE_ADDR		0x11E00000
+#define PCS_RN_PORT_ADDR_SIZE		0x100000
 
 /* PCS register entry bit positions and sizes */
 #define PCS_V2_WINDOW_DEF_OFFSET_INDEX	6
drivers/net/ethernet/amd/xgbe/xgbe-dev.c +93 -0

117 lines not on this page · raw message

drivers/net/ethernet/amd/xgbe/xgbe-pci.c +33 -2
index 8b0c1e450b7e..db3e8aac3339 100644
@@ -295,15 +295,28 @@ static int xgbe_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 		/* Yellow Carp devices do not need rrc */
 		pdata->vdata->enable_rrc = 0;
 		break;
+	case 0x1630:
+		pdata->xpcs_window_def_reg = PCS_V2_RN_WINDOW_DEF;
+		pdata->xpcs_window_sel_reg = PCS_V2_RN_WINDOW_SELECT;
+		break;
 	default:
 		pdata->xpcs_window_def_reg = PCS_V2_WINDOW_DEF;
 		pdata->xpcs_window_sel_reg = PCS_V2_WINDOW_SELECT;
 		break;
 	}
-	pci_dev_put(rdev);
 
 	/* Configure the PCS indirect addressing support */
-	reg = XPCS32_IOREAD(pdata, pdata->xpcs_window_def_reg);
+	if (pdata->vdata->xpcs_access == XGBE_XPCS_ACCESS_V3) {
+		reg = XP_IOREAD(pdata, XP_PROP_0);
+		pdata->xphy_base = PCS_RN_SMN_BASE_ADDR +
+				   (PCS_RN_PORT_ADDR_SIZE * XP_GET_BITS(reg, XP_PROP_0, PORT_ID));
+		pci_write_config_dword(rdev, 0x60, pdata->xphy_base + (pdata->xpcs_window_def_reg));
+		pci_read_config_dword(rdev, 0x64, &reg);
+	} else {
+		reg = XPCS32_IOREAD(pdata, pdata->xpcs_window_def_reg);
+	}
+	pci_dev_put(rdev);
+
 	pdata->xpcs_window = XPCS_GET_BITS(reg, PCS_V2_WINDOW_DEF, OFFSET);
 	pdata->xpcs_window <<= 6;
 	pdata->xpcs_window_size = XPCS_GET_BITS(reg, PCS_V2_WINDOW_DEF, SIZE);
@@ -481,6 +494,22 @@ static int __maybe_unused xgbe_pci_resume(struct device *dev)
 	return ret;
 }
 
+static struct xgbe_version_data xgbe_v3 = {
+	.init_function_ptrs_phy_impl	= xgbe_init_function_ptrs_phy_v2,
+	.xpcs_access			= XGBE_XPCS_ACCESS_V3,
+	.mmc_64bit			= 1,
+	.tx_max_fifo_size		= 65536,
+	.rx_max_fifo_size		= 65536,
+	.tx_tstamp_workaround		= 1,
+	.ecc_support			= 1,
+	.i2c_support			= 1,
+	.irq_reissue_support		= 1,
+	.tx_desc_prefetch		= 5,
+	.rx_desc_prefetch		= 5,
+	.an_cdr_workaround		= 0,
+	.enable_rrc			= 0,
+};
+
 static struct xgbe_version_data xgbe_v2a = {
 	.init_function_ptrs_phy_impl	= xgbe_init_function_ptrs_phy_v2,
 	.xpcs_access			= XGBE_XPCS_ACCESS_V2,
@@ -518,6 +547,8 @@ static const struct pci_device_id xgbe_pci_table[] = {
 	  .driver_data = (kernel_ulong_t)&xgbe_v2a },
 	{ PCI_VDEVICE(AMD, 0x1459),
 	  .driver_data = (kernel_ulong_t)&xgbe_v2b },
+	{ PCI_VDEVICE(AMD, 0x1641),
+	  .driver_data = (kernel_ulong_t)&xgbe_v3 },
 	/* Last entry must be zero */
 	{ 0, }
 };
drivers/net/ethernet/amd/xgbe/xgbe.h +6 -0
index ad136ed493ed..dbb1faaf6185 100644
@@ -347,6 +347,10 @@
 		    (_src)->link_modes._sname,		\
 		    __ETHTOOL_LINK_MODE_MASK_NBITS)
 
+/* Generic low and high masks */
+#define XGBE_GEN_HI_MASK	GENMASK(31, 16)
+#define XGBE_GEN_LO_MASK	GENMASK(15, 0)
+
 struct xgbe_prv_data;
 
 struct xgbe_packet_data {
@@ -565,6 +569,7 @@ enum xgbe_speed {
 enum xgbe_xpcs_access {
 	XGBE_XPCS_ACCESS_V1 = 0,
 	XGBE_XPCS_ACCESS_V2,
+	XGBE_XPCS_ACCESS_V3,
 };
 
 enum xgbe_an_mode {
@@ -1056,6 +1061,7 @@ struct xgbe_prv_data {
 	struct device *dev;
 	struct platform_device *phy_platdev;
 	struct device *phy_dev;
+	unsigned int xphy_base;
 
 	/* Version related data */
 	struct xgbe_version_data *vdata;
Signature · 1 line
2.34.1
author maintainer
#10 lore raw

PATCH v3 3/3 amd-xgbe: use smn functions to avoid race

In reply to Raju Rangoju

Some of the ethernet add-in-cards have dual PHY but share a single MDIO line (between the ports). In such cases, link inconsistencies are noticed during the heavy traffic and during reboot stress tests.

So, use the SMN calls to avoid the race conditions.

---
 drivers/net/ethernet/amd/xgbe/xgbe-dev.c | 33 ++++++------------------
 drivers/net/ethernet/amd/xgbe/xgbe-pci.c | 10 +++----
 drivers/net/ethernet/amd/xgbe/xgbe-smn.h | 27 +++++++++++++++++++
 drivers/net/ethernet/amd/xgbe/xgbe.h     |  2 +-
 4 files changed, 41 insertions(+), 31 deletions(-)
 create mode 100644 drivers/net/ethernet/amd/xgbe/xgbe-smn.h
drivers/net/ethernet/amd/xgbe/xgbe-dev.c +8 -25
index a9eb2ffa9f73..8d8876ab258c 100644
@@ -124,6 +124,7 @@
 
 #include "xgbe.h"
 #include "xgbe-common.h"
+#include "xgbe-smn.h"
 
 static inline unsigned int xgbe_get_max_frame(struct xgbe_prv_data *pdata)
 {
@@ -1170,14 +1171,9 @@ static int xgbe_read_mmd_regs_v3(struct xgbe_prv_data *pdata, int prtad,
 				 int mmd_reg)
 {
 	unsigned int mmd_address, index, offset;
-	struct pci_dev *rdev;
 	unsigned long flags;
 	int mmd_data;
 
-	rdev = pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(0, 0));
-	if (!rdev)
-		return 0;
-
 	mmd_address = get_mmd_address(pdata, mmd_reg);
 
 	/* The PCS registers are accessed using mmio. The underlying
@@ -1192,13 +1188,10 @@ static int xgbe_read_mmd_regs_v3(struct xgbe_prv_data *pdata, int prtad,
 	offset = get_index_offset(pdata, mmd_address, &index);
 
 	spin_lock_irqsave(&pdata->xpcs_lock, flags);
-	pci_write_config_dword(rdev, 0x60, (pdata->xphy_base + pdata->xpcs_window_sel_reg));
-	pci_write_config_dword(rdev, 0x64, index);
-	pci_write_config_dword(rdev, 0x60, pdata->xphy_base + offset);
-	pci_read_config_dword(rdev, 0x64, &mmd_data);
+	amd_smn_write(0, (pdata->smn_base + pdata->xpcs_window_sel_reg), index);
+	amd_smn_read(0, pdata->smn_base + offset, &mmd_data);
 	mmd_data = (offset % 4) ? FIELD_GET(XGBE_GEN_HI_MASK, mmd_data) :
 				  FIELD_GET(XGBE_GEN_LO_MASK, mmd_data);
-	pci_dev_put(rdev);
 
 	spin_unlock_irqrestore(&pdata->xpcs_lock, flags);
 
@@ -1209,13 +1202,8 @@ static void xgbe_write_mmd_regs_v3(struct xgbe_prv_data *pdata, int prtad,
 				   int mmd_reg, int mmd_data)
 {
 	unsigned int mmd_address, index, offset, ctr_mmd_data;
-	struct pci_dev *rdev;
 	unsigned long flags;
 
-	rdev = pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(0, 0));
-	if (!rdev)
-		return;
-
 	mmd_address = get_mmd_address(pdata, mmd_reg);
 
 	/* The PCS registers are accessed using mmio. The underlying
@@ -1230,10 +1218,9 @@ static void xgbe_write_mmd_regs_v3(struct xgbe_prv_data *pdata, int prtad,
 	offset = get_index_offset(pdata, mmd_address, &index);
 
 	spin_lock_irqsave(&pdata->xpcs_lock, flags);
-	pci_write_config_dword(rdev, 0x60, (pdata->xphy_base + pdata->xpcs_window_sel_reg));
-	pci_write_config_dword(rdev, 0x64, index);
-	pci_write_config_dword(rdev, 0x60, pdata->xphy_base + offset);
-	pci_read_config_dword(rdev, 0x64, &ctr_mmd_data);
+	amd_smn_write(0, (pdata->smn_base + pdata->xpcs_window_sel_reg), index);
+	amd_smn_read(0, pdata->smn_base + offset, &ctr_mmd_data);
+
 	if (offset % 4) {
 		ctr_mmd_data = FIELD_PREP(XGBE_GEN_HI_MASK, mmd_data) |
 			       FIELD_GET(XGBE_GEN_LO_MASK, ctr_mmd_data);
@@ -1243,12 +1230,8 @@ static void xgbe_write_mmd_regs_v3(struct xgbe_prv_data *pdata, int prtad,
 			       FIELD_GET(XGBE_GEN_LO_MASK, mmd_data);
 	}
 
-	pci_write_config_dword(rdev, 0x60, (pdata->xphy_base + pdata->xpcs_window_sel_reg));
-	pci_write_config_dword(rdev, 0x64, index);
-	pci_write_config_dword(rdev, 0x60, (pdata->xphy_base + offset));
-	pci_write_config_dword(rdev, 0x64, ctr_mmd_data);
-	pci_dev_put(rdev);
-
+	amd_smn_write(0, (pdata->smn_base + pdata->xpcs_window_sel_reg), index);
+	amd_smn_write(0, (pdata->smn_base + offset), ctr_mmd_data);
 	spin_unlock_irqrestore(&pdata->xpcs_lock, flags);
 }
 
drivers/net/ethernet/amd/xgbe/xgbe-pci.c +5 -5
index db3e8aac3339..135128b5be90 100644
@@ -121,6 +121,7 @@
 
 #include "xgbe.h"
 #include "xgbe-common.h"
+#include "xgbe-smn.h"
 
 static int xgbe_config_multi_msi(struct xgbe_prv_data *pdata)
 {
@@ -304,18 +305,17 @@ static int xgbe_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 		pdata->xpcs_window_sel_reg = PCS_V2_WINDOW_SELECT;
 		break;
 	}
+	pci_dev_put(rdev);
 
 	/* Configure the PCS indirect addressing support */
 	if (pdata->vdata->xpcs_access == XGBE_XPCS_ACCESS_V3) {
 		reg = XP_IOREAD(pdata, XP_PROP_0);
-		pdata->xphy_base = PCS_RN_SMN_BASE_ADDR +
-				   (PCS_RN_PORT_ADDR_SIZE * XP_GET_BITS(reg, XP_PROP_0, PORT_ID));
-		pci_write_config_dword(rdev, 0x60, pdata->xphy_base + (pdata->xpcs_window_def_reg));
-		pci_read_config_dword(rdev, 0x64, &reg);
+		pdata->smn_base = PCS_RN_SMN_BASE_ADDR +
+				  (PCS_RN_PORT_ADDR_SIZE * XP_GET_BITS(reg, XP_PROP_0, PORT_ID));
+		amd_smn_read(0, pdata->smn_base + (pdata->xpcs_window_def_reg), &reg);
 	} else {
 		reg = XPCS32_IOREAD(pdata, pdata->xpcs_window_def_reg);
 	}
-	pci_dev_put(rdev);
 
 	pdata->xpcs_window = XPCS_GET_BITS(reg, PCS_V2_WINDOW_DEF, OFFSET);
 	pdata->xpcs_window <<= 6;
drivers/net/ethernet/amd/xgbe/xgbe-smn.h new file +27 -0
new file mode 100644
index 000000000000..bd25ddc7c869
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * AMD 10Gb Ethernet driver
+ *
+ * Copyright (c) 2023, Advanced Micro Devices, Inc.
+ * All Rights Reserved.
+ *
+ * Author: Raju Rangoju <[email protected]>
+ */
+
+#ifdef CONFIG_AMD_NB
+
+#include <asm/amd_nb.h>
+
+#else
+
+static inline int amd_smn_write(u16 node, u32 address, u32 value)
+{
+	return -ENODEV;
+}
+
+static inline int amd_smn_read(u16 node, u32 address, u32 *value)
+{
+	return -ENODEV;
+}
+
+#endif
drivers/net/ethernet/amd/xgbe/xgbe.h +1 -1
index dbb1faaf6185..ba45ab0adb8c 100644
@@ -1061,7 +1061,7 @@ struct xgbe_prv_data {
 	struct device *dev;
 	struct platform_device *phy_platdev;
 	struct device *phy_dev;
-	unsigned int xphy_base;
+	unsigned int smn_base;
 
 	/* Version related data */
 	struct xgbe_version_data *vdata;
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v3 net-next 1/3] amd-xgbe: reorganize the code of XPCS access

In reply to Raju Rangoju
Quoted 141 lines · Raju Rangoju

The xgbe_{read/write}_mmd_regs_v* functions have common code which can be moved to helper functions. Also, the xgbe_pci_probe() needs reorganization.

Add new helper functions to calculate the mmd_address for v1/v2 of xpcs access. And, convert if/else statements in xgbe_pci_probe() to switch case. This helps code look cleaner.

---
 drivers/net/ethernet/amd/xgbe/xgbe-dev.c | 43 ++++++++++++------------
 drivers/net/ethernet/amd/xgbe/xgbe-pci.c | 18 +++++++---
 2 files changed, 34 insertions(+), 27 deletions(-)
drivers/net/ethernet/amd/xgbe/xgbe-dev.c +13 -0
index f393228d41c7..6cd003c24a64 100644
@@ -1150,6 +1150,21 @@ static int xgbe_set_gpio(struct xgbe_prv_data
 *pdata, unsigned int gpio)
        return 0;
 }
 
+static unsigned int get_mmd_address(struct xgbe_prv_data *pdata, int
 mmd_reg)
+{
+       return (mmd_reg & XGBE_ADDR_C45) ?
+               mmd_reg & ~XGBE_ADDR_C45 :
+               (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
+}
+
+static unsigned int get_index_offset(struct xgbe_prv_data *pdata,
 unsigned int mmd_address,
+                                    unsigned int *index)
+{
+       mmd_address <<= 1;
+       *index = mmd_address & ~pdata->xpcs_window_mask;
+       return pdata->xpcs_window + (mmd_address &

pdata->xpcs_window_mask);

drivers/net/ethernet/amd/xgbe/xgbe-dev.c +3 -4
+}
+
 static int xgbe_read_mmd_regs_v2(struct xgbe_prv_data *pdata, int prtad,
                                 int mmd_reg)
 {
@@ -1157,10 +1172,7 @@ static int xgbe_read_mmd_regs_v2(struct
 xgbe_prv_data *pdata, int prtad,
        unsigned int mmd_address, index, offset;
        int mmd_data;
 
-       if (mmd_reg & XGBE_ADDR_C45)
-               mmd_address = mmd_reg & ~XGBE_ADDR_C45;
-       else
-               mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
+       mmd_address = get_mmd_address(pdata, mmd_reg);
 
        /* The PCS registers are accessed using mmio. The underlying

* management interface uses indirect addressing to access the MMD

drivers/net/ethernet/amd/xgbe/xgbe-dev.c +1 -3
@@ -1171,9 +1183,7 @@ static int xgbe_read_mmd_regs_v2(struct
 xgbe_prv_data *pdata, int prtad,
         * register offsets must therefore be adjusted by left shifting the
         * offset 1 bit and reading 16 bits of data.
         */
-       mmd_address <<= 1;
-       index = mmd_address & ~pdata->xpcs_window_mask;
-       offset = pdata->xpcs_window + (mmd_address &
 pdata->xpcs_window_mask);
+       offset = get_index_offset(pdata, mmd_address, &index);
 
        spin_lock_irqsave(&pdata->xpcs_lock, flags);
        XPCS32_IOWRITE(pdata, pdata->xpcs_window_sel_reg, index);
drivers/net/ethernet/amd/xgbe/xgbe-dev.c +1 -4
@@ -1189,10 +1199,7 @@ static void xgbe_write_mmd_regs_v2(struct
 xgbe_prv_data *pdata, int prtad,
        unsigned long flags;
        unsigned int mmd_address, index, offset;
 
-       if (mmd_reg & XGBE_ADDR_C45)
-               mmd_address = mmd_reg & ~XGBE_ADDR_C45;
-       else
-               mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
+       mmd_address = get_mmd_address(pdata, mmd_reg);
 
        /* The PCS registers are accessed using mmio. The underlying

* management interface uses indirect addressing to access the MMD

drivers/net/ethernet/amd/xgbe/xgbe-dev.c +1 -3
@@ -1203,9 +1210,7 @@ static void xgbe_write_mmd_regs_v2(struct
 xgbe_prv_data *pdata, int prtad,
         * register offsets must therefore be adjusted by left shifting the
         * offset 1 bit and writing 16 bits of data.
         */
-       mmd_address <<= 1;
-       index = mmd_address & ~pdata->xpcs_window_mask;
-       offset = pdata->xpcs_window + (mmd_address &
 pdata->xpcs_window_mask);
+       offset = get_index_offset(pdata, mmd_address, &index);
 
        spin_lock_irqsave(&pdata->xpcs_lock, flags);
        XPCS32_IOWRITE(pdata, pdata->xpcs_window_sel_reg, index);
drivers/net/ethernet/amd/xgbe/xgbe-dev.c +1 -4
@@ -1220,10 +1225,7 @@ static int xgbe_read_mmd_regs_v1(struct
 xgbe_prv_data *pdata, int prtad,
        unsigned int mmd_address;
        int mmd_data;
 
-       if (mmd_reg & XGBE_ADDR_C45)
-               mmd_address = mmd_reg & ~XGBE_ADDR_C45;
-       else
-               mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
+       mmd_address = get_mmd_address(pdata, mmd_reg);
 
        /* The PCS registers are accessed using mmio. The underlying APB3

* management interface uses indirect addressing to access the MMD

drivers/net/ethernet/amd/xgbe/xgbe-dev.c +1 -4
@@ -1248,10 +1250,7 @@ static void xgbe_write_mmd_regs_v1(struct
 xgbe_prv_data *pdata, int prtad,
        unsigned int mmd_address;
        unsigned long flags;
 
-       if (mmd_reg & XGBE_ADDR_C45)
-               mmd_address = mmd_reg & ~XGBE_ADDR_C45;
-       else
-               mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
+       mmd_address = get_mmd_address(pdata, mmd_reg);
 
        /* The PCS registers are accessed using mmio. The underlying APB3

* management interface uses indirect addressing to access the MMD

drivers/net/ethernet/amd/xgbe/xgbe-pci.c +5 -2
index f409d7bd1f1e..8b0c1e450b7e 100644
@@ -274,12 +274,18 @@ static int xgbe_pci_probe(struct pci_dev *pdev,
 const struct pci_device_id *id)
 
        /* Set the PCS indirect addressing definition registers */
        rdev = pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(0, 0));
-       if (rdev &&
-           (rdev->vendor == PCI_VENDOR_ID_AMD) && (rdev->device ==
 0x15d0)) {
+
+       if (!(rdev && rdev->vendor == PCI_VENDOR_ID_AMD)) {
+               ret = -ENODEV;
+               goto err_pci_enable;
+       }

drivers/net/ethernet/amd/xgbe/xgbe-pci.c static int xgbe_pci_probe(struct pci_dev *pdev, · line 291

Quoted 10 lines
drivers/net/ethernet/amd/xgbe/xgbe-pci.c +5 -2
+
+       switch (rdev->device) {
+       case 0x15d0:
                pdata->xpcs_window_def_reg = PCS_V2_RV_WINDOW_DEF;
                pdata->xpcs_window_sel_reg = PCS_V2_RV_WINDOW_SELECT;
-       } else if (rdev && (rdev->vendor == PCI_VENDOR_ID_AMD) &&
-                  (rdev->device == 0x14b5)) {
+               break;
+       case 0x14b5:
 

[Kalesh]: Can you use macros for 0x15d0 and 0x14b5

Quoted 22 lines
                pdata->xpcs_window_def_reg = PCS_V2_YC_WINDOW_DEF;
                pdata->xpcs_window_sel_reg = PCS_V2_YC_WINDOW_SELECT;
drivers/net/ethernet/amd/xgbe/xgbe-pci.c +3 -2
@@ -288,9 +294,11 @@ static int xgbe_pci_probe(struct pci_dev *pdev, const
 struct pci_device_id *id)
 
                /* Yellow Carp devices do not need rrc */
                pdata->vdata->enable_rrc = 0;
-       } else {
+               break;
+       default:
                pdata->xpcs_window_def_reg = PCS_V2_WINDOW_DEF;
                pdata->xpcs_window_sel_reg = PCS_V2_WINDOW_SELECT;
+               break;
        }
        pci_dev_put(rdev);
 
--

2.34.1

Signature · 2 lines
Regards,
Kalesh A P
#3 lore raw

Re: [PATCH v3 net-next 1/3] amd-xgbe: reorganize the code of XPCS access

In reply to Raju Rangoju
Quoted 21 lines · Raju Rangoju

The xgbe_{read/write}_mmd_regs_v* functions have common code which can be moved to helper functions. Also, the xgbe_pci_probe() needs reorganization.

Add new helper functions to calculate the mmd_address for v1/v2 of xpcs access. And, convert if/else statements in xgbe_pci_probe() to switch case. This helps code look cleaner.

---
  drivers/net/ethernet/amd/xgbe/xgbe-dev.c | 43 ++++++++++++------------
  drivers/net/ethernet/amd/xgbe/xgbe-pci.c | 18 +++++++---
  2 files changed, 34 insertions(+), 27 deletions(-)
drivers/net/ethernet/amd/xgbe/xgbe-dev.c +0 -0
index f393228d41c7..6cd003c24a64 100644
@@ -1150,6 +1150,21 @@ static int xgbe_set_gpio(struct xgbe_prv_data *pdata, unsigned int gpio)
  	return 0;
  }

drivers/net/ethernet/amd/xgbe/xgbe-dev.c static int xgbe_set_gpio(struct xgbe_prv_data *pdata, unsigned int gpio) · line 1161

Quoted 10 lines
drivers/net/ethernet/amd/xgbe/xgbe-dev.c +9 -0
 
+static unsigned int get_mmd_address(struct xgbe_prv_data *pdata, int mmd_reg)
+{
+	return (mmd_reg & XGBE_ADDR_C45) ?
+		mmd_reg & ~XGBE_ADDR_C45 :
+		(pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
+}
+
+static unsigned int get_index_offset(struct xgbe_prv_data *pdata, unsigned int mmd_address,
+				     unsigned int *index)

Just my opinion, but this looks confusing to me by updating index and returning offset. And the name is confusing, too. I think it would read better as:

static void get_pcs_index_and_offset(struct xgbe_prv_data *pdata, unsigned int mmd_address,
unsigned int *index, unsigned int *offset)

{
	mmd_address <<= 1;
	*index = mmd_address & ~pdata->xpcs_window_mask;
	*offset = pdata->xpcs_window + (mmd_address & pdata->xpcs_window_mask);
}

Or break this into two functions:

static unsigned int get_pcs_index(struct xgbe_prv_data *pdata, unsigned int mmd_address)

{
	return (mmd_address << 1) & ~pdata->xpcs_window_mask;
}

static unsigned int get_pcs_offset(struct xgbe_prv_data *pdata, unsigned int mmd_address)

{
	return pdata->xpcs_window + ((mmd_address << 1) & pdata->xpcs_window_mask);
}
Quoted 22 lines
drivers/net/ethernet/amd/xgbe/xgbe-dev.c +7 -4
+{
+	mmd_address <<= 1;
+	*index = mmd_address & ~pdata->xpcs_window_mask;
+	return pdata->xpcs_window + (mmd_address & pdata->xpcs_window_mask);
+}
+
  static int xgbe_read_mmd_regs_v2(struct xgbe_prv_data *pdata, int prtad,
  				 int mmd_reg)
  {
@@ -1157,10 +1172,7 @@ static int xgbe_read_mmd_regs_v2(struct xgbe_prv_data *pdata, int prtad,
  	unsigned int mmd_address, index, offset;
  	int mmd_data;
 
-	if (mmd_reg & XGBE_ADDR_C45)
-		mmd_address = mmd_reg & ~XGBE_ADDR_C45;
-	else
-		mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
+	mmd_address = get_mmd_address(pdata, mmd_reg);
 
  	/* The PCS registers are accessed using mmio. The underlying
  	 * management interface uses indirect addressing to access the MMD

drivers/net/ethernet/amd/xgbe/xgbe-dev.c static int xgbe_read_mmd_regs_v2(struct xgbe_prv_data *pdata, int prtad, · line 1186

Quoted 7 lines
drivers/net/ethernet/amd/xgbe/xgbe-dev.c +1 -3
@@ -1171,9 +1183,7 @@ static int xgbe_read_mmd_regs_v2(struct xgbe_prv_data *pdata, int prtad,
  	 * register offsets must therefore be adjusted by left shifting the
  	 * offset 1 bit and reading 16 bits of data.
  	 */
-	mmd_address <<= 1;
-	index = mmd_address & ~pdata->xpcs_window_mask;
-	offset = pdata->xpcs_window + (mmd_address & pdata->xpcs_window_mask);
+	offset = get_index_offset(pdata, mmd_address, &index);

The comment above this code should be moved into the new helper and then removed here and below.

Quoted 55 lines
drivers/net/ethernet/amd/xgbe/xgbe-dev.c +4 -15
  	spin_lock_irqsave(&pdata->xpcs_lock, flags);
  	XPCS32_IOWRITE(pdata, pdata->xpcs_window_sel_reg, index);
@@ -1189,10 +1199,7 @@ static void xgbe_write_mmd_regs_v2(struct xgbe_prv_data *pdata, int prtad,
  	unsigned long flags;
  	unsigned int mmd_address, index, offset;
 
-	if (mmd_reg & XGBE_ADDR_C45)
-		mmd_address = mmd_reg & ~XGBE_ADDR_C45;
-	else
-		mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
+	mmd_address = get_mmd_address(pdata, mmd_reg);
 
  	/* The PCS registers are accessed using mmio. The underlying
  	 * management interface uses indirect addressing to access the MMD
@@ -1203,9 +1210,7 @@ static void xgbe_write_mmd_regs_v2(struct xgbe_prv_data *pdata, int prtad,
  	 * register offsets must therefore be adjusted by left shifting the
  	 * offset 1 bit and writing 16 bits of data.
  	 */
-	mmd_address <<= 1;
-	index = mmd_address & ~pdata->xpcs_window_mask;
-	offset = pdata->xpcs_window + (mmd_address & pdata->xpcs_window_mask);
+	offset = get_index_offset(pdata, mmd_address, &index);
 
  	spin_lock_irqsave(&pdata->xpcs_lock, flags);
  	XPCS32_IOWRITE(pdata, pdata->xpcs_window_sel_reg, index);
@@ -1220,10 +1225,7 @@ static int xgbe_read_mmd_regs_v1(struct xgbe_prv_data *pdata, int prtad,
  	unsigned int mmd_address;
  	int mmd_data;
 
-	if (mmd_reg & XGBE_ADDR_C45)
-		mmd_address = mmd_reg & ~XGBE_ADDR_C45;
-	else
-		mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
+	mmd_address = get_mmd_address(pdata, mmd_reg);
 
  	/* The PCS registers are accessed using mmio. The underlying APB3
  	 * management interface uses indirect addressing to access the MMD
@@ -1248,10 +1250,7 @@ static void xgbe_write_mmd_regs_v1(struct xgbe_prv_data *pdata, int prtad,
  	unsigned int mmd_address;
  	unsigned long flags;
 
-	if (mmd_reg & XGBE_ADDR_C45)
-		mmd_address = mmd_reg & ~XGBE_ADDR_C45;
-	else
-		mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
+	mmd_address = get_mmd_address(pdata, mmd_reg);
 
  	/* The PCS registers are accessed using mmio. The underlying APB3
  	 * management interface uses indirect addressing to access the MMD
drivers/net/ethernet/amd/xgbe/xgbe-pci.c +0 -0
index f409d7bd1f1e..8b0c1e450b7e 100644

drivers/net/ethernet/amd/xgbe/xgbe-pci.c static int xgbe_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 281

Quoted 10 lines
drivers/net/ethernet/amd/xgbe/xgbe-pci.c +5 -2
@@ -274,12 +274,18 @@ static int xgbe_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 
  	/* Set the PCS indirect addressing definition registers */
  	rdev = pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(0, 0));
-	if (rdev &&
-	    (rdev->vendor == PCI_VENDOR_ID_AMD) && (rdev->device == 0x15d0)) {
+
+	if (!(rdev && rdev->vendor == PCI_VENDOR_ID_AMD)) {
+		ret = -ENODEV;
+		goto err_pci_enable;
+	}

This is different behavior compared to today. Today, everything would default to the final "else" statement. With this patch you have a possibility of failing probe now.

Thanks,
Tom

Quoted 25 lines
drivers/net/ethernet/amd/xgbe/xgbe-pci.c +8 -3
+
+	switch (rdev->device) {
+	case 0x15d0:
  		pdata->xpcs_window_def_reg = PCS_V2_RV_WINDOW_DEF;
  		pdata->xpcs_window_sel_reg = PCS_V2_RV_WINDOW_SELECT;
-	} else if (rdev && (rdev->vendor == PCI_VENDOR_ID_AMD) &&
-		   (rdev->device == 0x14b5)) {
+		break;
+	case 0x14b5:
  		pdata->xpcs_window_def_reg = PCS_V2_YC_WINDOW_DEF;
  		pdata->xpcs_window_sel_reg = PCS_V2_YC_WINDOW_SELECT;
 
@@ -288,9 +294,11 @@ static int xgbe_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 
  		/* Yellow Carp devices do not need rrc */
  		pdata->vdata->enable_rrc = 0;
-	} else {
+		break;
+	default:
  		pdata->xpcs_window_def_reg = PCS_V2_WINDOW_DEF;
  		pdata->xpcs_window_sel_reg = PCS_V2_WINDOW_SELECT;
+		break;
  	}
  	pci_dev_put(rdev);
 
#7 lore raw

Re: [PATCH v3 net-next 2/3] amd-xgbe: add support for Crater ethernet device

In reply to Raju Rangoju
Quoted 20 lines · Raju Rangoju

Add the necessary support to enable Crater ethernet device. Since the BAR1 address cannot be used to access the XPCS registers on Crater, use the pci_{read/write}_config_dword calls. Also, include the new pci device id 0x1641 to register Crater device with PCIe.

---
  drivers/net/ethernet/amd/xgbe/xgbe-common.h |  5 ++
  drivers/net/ethernet/amd/xgbe/xgbe-dev.c    | 93 +++++++++++++++++++++
  drivers/net/ethernet/amd/xgbe/xgbe-pci.c    | 35 +++++++-
  drivers/net/ethernet/amd/xgbe/xgbe.h        |  6 ++
  4 files changed, 137 insertions(+), 2 deletions(-)
drivers/net/ethernet/amd/xgbe/xgbe-common.h +0 -0
index 3b70f6737633..e1f70f0528ef 100644

drivers/net/ethernet/amd/xgbe/xgbe-common.h line 904

Quoted 5 lines
drivers/net/ethernet/amd/xgbe/xgbe-common.h +2 -0
@@ -900,6 +900,11 @@
  #define PCS_V2_RV_WINDOW_SELECT		0x1064
  #define PCS_V2_YC_WINDOW_DEF		0x18060
  #define PCS_V2_YC_WINDOW_SELECT		0x18064
+#define PCS_V2_RN_WINDOW_DEF		0xF8078
+#define PCS_V2_RN_WINDOW_SELECT		0xF807c

Should this be PCS_V3_ (here and below) ??

drivers/net/ethernet/amd/xgbe/xgbe-common.h line 907

Quoted 3 lines
drivers/net/ethernet/amd/xgbe/xgbe-common.h +3 -0
+
+#define PCS_RN_SMN_BASE_ADDR		0x11E00000
+#define PCS_RN_PORT_ADDR_SIZE		0x100000

All hex characters should be consistent and in lower case.

Quoted 85 lines
drivers/net/ethernet/amd/xgbe/xgbe-common.h +0 -0
  /* PCS register entry bit positions and sizes */
  #define PCS_V2_WINDOW_DEF_OFFSET_INDEX	6
drivers/net/ethernet/amd/xgbe/xgbe-dev.c +67 -0
index 6cd003c24a64..a9eb2ffa9f73 100644
@@ -120,6 +120,7 @@
  #include <linux/bitrev.h>
  #include <linux/crc32.h>
  #include <linux/crc32poly.h>
+#include <linux/pci.h>
 
  #include "xgbe.h"
  #include "xgbe-common.h"
@@ -1165,6 +1166,92 @@ static unsigned int get_index_offset(struct xgbe_prv_data *pdata, unsigned int m
  	return pdata->xpcs_window + (mmd_address & pdata->xpcs_window_mask);
  }
 
+static int xgbe_read_mmd_regs_v3(struct xgbe_prv_data *pdata, int prtad,
+				 int mmd_reg)
+{
+	unsigned int mmd_address, index, offset;
+	struct pci_dev *rdev;
+	unsigned long flags;
+	int mmd_data;
+
+	rdev = pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(0, 0));
+	if (!rdev)
+		return 0;
+
+	mmd_address = get_mmd_address(pdata, mmd_reg);
+
+	/* The PCS registers are accessed using mmio. The underlying
+	 * management interface uses indirect addressing to access the MMD
+	 * register sets. This requires accessing of the PCS register in two
+	 * phases, an address phase and a data phase.
+	 *
+	 * The mmio interface is based on 16-bit offsets and values. All
+	 * register offsets must therefore be adjusted by left shifting the
+	 * offset 1 bit and reading 16 bits of data.
+	 */
+	offset = get_index_offset(pdata, mmd_address, &index);
+
+	spin_lock_irqsave(&pdata->xpcs_lock, flags);
+	pci_write_config_dword(rdev, 0x60, (pdata->xphy_base + pdata->xpcs_window_sel_reg));
+	pci_write_config_dword(rdev, 0x64, index);
+	pci_write_config_dword(rdev, 0x60, pdata->xphy_base + offset);
+	pci_read_config_dword(rdev, 0x64, &mmd_data);
+	mmd_data = (offset % 4) ? FIELD_GET(XGBE_GEN_HI_MASK, mmd_data) :
+				  FIELD_GET(XGBE_GEN_LO_MASK, mmd_data);
+	pci_dev_put(rdev);
+
+	spin_unlock_irqrestore(&pdata->xpcs_lock, flags);
+
+	return mmd_data;
+}
+
+static void xgbe_write_mmd_regs_v3(struct xgbe_prv_data *pdata, int prtad,
+				   int mmd_reg, int mmd_data)
+{
+	unsigned int mmd_address, index, offset, ctr_mmd_data;
+	struct pci_dev *rdev;
+	unsigned long flags;
+
+	rdev = pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(0, 0));
+	if (!rdev)
+		return;
+
+	mmd_address = get_mmd_address(pdata, mmd_reg);
+
+	/* The PCS registers are accessed using mmio. The underlying
+	 * management interface uses indirect addressing to access the MMD
+	 * register sets. This requires accessing of the PCS register in two
+	 * phases, an address phase and a data phase.
+	 *
+	 * The mmio interface is based on 16-bit offsets and values. All
+	 * register offsets must therefore be adjusted by left shifting the
+	 * offset 1 bit and writing 16 bits of data.
+	 */
+	offset = get_index_offset(pdata, mmd_address, &index);
+
+	spin_lock_irqsave(&pdata->xpcs_lock, flags);
+	pci_write_config_dword(rdev, 0x60, (pdata->xphy_base + pdata->xpcs_window_sel_reg));
+	pci_write_config_dword(rdev, 0x64, index);

drivers/net/ethernet/amd/xgbe/xgbe-dev.c static unsigned int get_index_offset(struct xgbe_prv_data *pdata, unsigned int m · line 1244

Quoted 10 lines
drivers/net/ethernet/amd/xgbe/xgbe-dev.c +10 -0
+	pci_write_config_dword(rdev, 0x60, pdata->xphy_base + offset);
+	pci_read_config_dword(rdev, 0x64, &ctr_mmd_data);
+	if (offset % 4) {
+		ctr_mmd_data = FIELD_PREP(XGBE_GEN_HI_MASK, mmd_data) |
+			       FIELD_GET(XGBE_GEN_LO_MASK, ctr_mmd_data);
+	} else {
+		ctr_mmd_data = FIELD_PREP(XGBE_GEN_HI_MASK,
+					  FIELD_GET(XGBE_GEN_HI_MASK, ctr_mmd_data)) |
+			       FIELD_GET(XGBE_GEN_LO_MASK, mmd_data);
+	}

Braces aren't necessary.

Also, just curious, what is the "ctr" prefix meant to imply here?

Quoted 38 lines
drivers/net/ethernet/amd/xgbe/xgbe-dev.c +16 -0
+
+	pci_write_config_dword(rdev, 0x60, (pdata->xphy_base + pdata->xpcs_window_sel_reg));
+	pci_write_config_dword(rdev, 0x64, index);
+	pci_write_config_dword(rdev, 0x60, (pdata->xphy_base + offset));
+	pci_write_config_dword(rdev, 0x64, ctr_mmd_data);
+	pci_dev_put(rdev);
+
+	spin_unlock_irqrestore(&pdata->xpcs_lock, flags);
+}
+
  static int xgbe_read_mmd_regs_v2(struct xgbe_prv_data *pdata, int prtad,
  				 int mmd_reg)
  {
@@ -1274,6 +1361,9 @@ static int xgbe_read_mmd_regs(struct xgbe_prv_data *pdata, int prtad,
  	case XGBE_XPCS_ACCESS_V1:
  		return xgbe_read_mmd_regs_v1(pdata, prtad, mmd_reg);
 
+	case XGBE_XPCS_ACCESS_V3:
+		return xgbe_read_mmd_regs_v3(pdata, prtad, mmd_reg);
+
  	case XGBE_XPCS_ACCESS_V2:
  	default:
  		return xgbe_read_mmd_regs_v2(pdata, prtad, mmd_reg);
@@ -1287,6 +1377,9 @@ static void xgbe_write_mmd_regs(struct xgbe_prv_data *pdata, int prtad,
  	case XGBE_XPCS_ACCESS_V1:
  		return xgbe_write_mmd_regs_v1(pdata, prtad, mmd_reg, mmd_data);
 
+	case XGBE_XPCS_ACCESS_V3:
+		return xgbe_write_mmd_regs_v3(pdata, prtad, mmd_reg, mmd_data);
+
  	case XGBE_XPCS_ACCESS_V2:
  	default:
  		return xgbe_write_mmd_regs_v2(pdata, prtad, mmd_reg, mmd_data);
drivers/net/ethernet/amd/xgbe/xgbe-pci.c +0 -0
index 8b0c1e450b7e..db3e8aac3339 100644

drivers/net/ethernet/amd/xgbe/xgbe-pci.c static int xgbe_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 298

Quoted 4 lines
drivers/net/ethernet/amd/xgbe/xgbe-pci.c +1 -0
@@ -295,15 +295,28 @@ static int xgbe_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  		/* Yellow Carp devices do not need rrc */
  		pdata->vdata->enable_rrc = 0;
  		break;
+	case 0x1630:

What PCI ID is this, it doesn't match the 0x1641 added below?

Thanks,
Tom

Quoted 89 lines
drivers/net/ethernet/amd/xgbe/xgbe-pci.c +32 -2
+		pdata->xpcs_window_def_reg = PCS_V2_RN_WINDOW_DEF;
+		pdata->xpcs_window_sel_reg = PCS_V2_RN_WINDOW_SELECT;
+		break;
  	default:
  		pdata->xpcs_window_def_reg = PCS_V2_WINDOW_DEF;
  		pdata->xpcs_window_sel_reg = PCS_V2_WINDOW_SELECT;
  		break;
  	}
-	pci_dev_put(rdev);
 
  	/* Configure the PCS indirect addressing support */
-	reg = XPCS32_IOREAD(pdata, pdata->xpcs_window_def_reg);
+	if (pdata->vdata->xpcs_access == XGBE_XPCS_ACCESS_V3) {
+		reg = XP_IOREAD(pdata, XP_PROP_0);
+		pdata->xphy_base = PCS_RN_SMN_BASE_ADDR +
+				   (PCS_RN_PORT_ADDR_SIZE * XP_GET_BITS(reg, XP_PROP_0, PORT_ID));
+		pci_write_config_dword(rdev, 0x60, pdata->xphy_base + (pdata->xpcs_window_def_reg));
+		pci_read_config_dword(rdev, 0x64, &reg);
+	} else {
+		reg = XPCS32_IOREAD(pdata, pdata->xpcs_window_def_reg);
+	}
+	pci_dev_put(rdev);
+
  	pdata->xpcs_window = XPCS_GET_BITS(reg, PCS_V2_WINDOW_DEF, OFFSET);
  	pdata->xpcs_window <<= 6;
  	pdata->xpcs_window_size = XPCS_GET_BITS(reg, PCS_V2_WINDOW_DEF, SIZE);
@@ -481,6 +494,22 @@ static int __maybe_unused xgbe_pci_resume(struct device *dev)
  	return ret;
  }
 
+static struct xgbe_version_data xgbe_v3 = {
+	.init_function_ptrs_phy_impl	= xgbe_init_function_ptrs_phy_v2,
+	.xpcs_access			= XGBE_XPCS_ACCESS_V3,
+	.mmc_64bit			= 1,
+	.tx_max_fifo_size		= 65536,
+	.rx_max_fifo_size		= 65536,
+	.tx_tstamp_workaround		= 1,
+	.ecc_support			= 1,
+	.i2c_support			= 1,
+	.irq_reissue_support		= 1,
+	.tx_desc_prefetch		= 5,
+	.rx_desc_prefetch		= 5,
+	.an_cdr_workaround		= 0,
+	.enable_rrc			= 0,
+};
+
  static struct xgbe_version_data xgbe_v2a = {
  	.init_function_ptrs_phy_impl	= xgbe_init_function_ptrs_phy_v2,
  	.xpcs_access			= XGBE_XPCS_ACCESS_V2,
@@ -518,6 +547,8 @@ static const struct pci_device_id xgbe_pci_table[] = {
  	  .driver_data = (kernel_ulong_t)&xgbe_v2a },
  	{ PCI_VDEVICE(AMD, 0x1459),
  	  .driver_data = (kernel_ulong_t)&xgbe_v2b },
+	{ PCI_VDEVICE(AMD, 0x1641),
+	  .driver_data = (kernel_ulong_t)&xgbe_v3 },
  	/* Last entry must be zero */
  	{ 0, }
  };
drivers/net/ethernet/amd/xgbe/xgbe.h +6 -0
index ad136ed493ed..dbb1faaf6185 100644
@@ -347,6 +347,10 @@
  		    (_src)->link_modes._sname,		\
  		    __ETHTOOL_LINK_MODE_MASK_NBITS)
 
+/* Generic low and high masks */
+#define XGBE_GEN_HI_MASK	GENMASK(31, 16)
+#define XGBE_GEN_LO_MASK	GENMASK(15, 0)
+
  struct xgbe_prv_data;
 
  struct xgbe_packet_data {
@@ -565,6 +569,7 @@ enum xgbe_speed {
  enum xgbe_xpcs_access {
  	XGBE_XPCS_ACCESS_V1 = 0,
  	XGBE_XPCS_ACCESS_V2,
+	XGBE_XPCS_ACCESS_V3,
  };
 
  enum xgbe_an_mode {
@@ -1056,6 +1061,7 @@ struct xgbe_prv_data {
  	struct device *dev;
  	struct platform_device *phy_platdev;
  	struct device *phy_dev;
+	unsigned int xphy_base;
 
  	/* Version related data */
  	struct xgbe_version_data *vdata;
#11 lore raw

Re: [PATCH v3 net-next 3/3] amd-xgbe: use smn functions to avoid race

In reply to Raju Rangoju
Quoted 5 lines · Raju Rangoju

Some of the ethernet add-in-cards have dual PHY but share a single MDIO line (between the ports). In such cases, link inconsistencies are noticed during the heavy traffic and during reboot stress tests.

So, use the SMN calls to avoid the race conditions.

So this patch replaces all the PCI accesses you added in patch #2, so why not just do this from the start?

Quoted 137 lines
---
  drivers/net/ethernet/amd/xgbe/xgbe-dev.c | 33 ++++++------------------
  drivers/net/ethernet/amd/xgbe/xgbe-pci.c | 10 +++----
  drivers/net/ethernet/amd/xgbe/xgbe-smn.h | 27 +++++++++++++++++++
  drivers/net/ethernet/amd/xgbe/xgbe.h     |  2 +-
  4 files changed, 41 insertions(+), 31 deletions(-)

create mode 100644 drivers/net/ethernet/amd/xgbe/xgbe-smn.h

drivers/net/ethernet/amd/xgbe/xgbe-dev.c +8 -25
index a9eb2ffa9f73..8d8876ab258c 100644
@@ -124,6 +124,7 @@
 
  #include "xgbe.h"
  #include "xgbe-common.h"
+#include "xgbe-smn.h"
 
  static inline unsigned int xgbe_get_max_frame(struct xgbe_prv_data *pdata)
  {
@@ -1170,14 +1171,9 @@ static int xgbe_read_mmd_regs_v3(struct xgbe_prv_data *pdata, int prtad,
  				 int mmd_reg)
  {
  	unsigned int mmd_address, index, offset;
-	struct pci_dev *rdev;
  	unsigned long flags;
  	int mmd_data;
 
-	rdev = pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(0, 0));
-	if (!rdev)
-		return 0;
-
  	mmd_address = get_mmd_address(pdata, mmd_reg);
 
  	/* The PCS registers are accessed using mmio. The underlying
@@ -1192,13 +1188,10 @@ static int xgbe_read_mmd_regs_v3(struct xgbe_prv_data *pdata, int prtad,
  	offset = get_index_offset(pdata, mmd_address, &index);
 
  	spin_lock_irqsave(&pdata->xpcs_lock, flags);
-	pci_write_config_dword(rdev, 0x60, (pdata->xphy_base + pdata->xpcs_window_sel_reg));
-	pci_write_config_dword(rdev, 0x64, index);
-	pci_write_config_dword(rdev, 0x60, pdata->xphy_base + offset);
-	pci_read_config_dword(rdev, 0x64, &mmd_data);
+	amd_smn_write(0, (pdata->smn_base + pdata->xpcs_window_sel_reg), index);
+	amd_smn_read(0, pdata->smn_base + offset, &mmd_data);
  	mmd_data = (offset % 4) ? FIELD_GET(XGBE_GEN_HI_MASK, mmd_data) :
  				  FIELD_GET(XGBE_GEN_LO_MASK, mmd_data);
-	pci_dev_put(rdev);
 
  	spin_unlock_irqrestore(&pdata->xpcs_lock, flags);
 
@@ -1209,13 +1202,8 @@ static void xgbe_write_mmd_regs_v3(struct xgbe_prv_data *pdata, int prtad,
  				   int mmd_reg, int mmd_data)
  {
  	unsigned int mmd_address, index, offset, ctr_mmd_data;
-	struct pci_dev *rdev;
  	unsigned long flags;
 
-	rdev = pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(0, 0));
-	if (!rdev)
-		return;
-
  	mmd_address = get_mmd_address(pdata, mmd_reg);
 
  	/* The PCS registers are accessed using mmio. The underlying
@@ -1230,10 +1218,9 @@ static void xgbe_write_mmd_regs_v3(struct xgbe_prv_data *pdata, int prtad,
  	offset = get_index_offset(pdata, mmd_address, &index);
 
  	spin_lock_irqsave(&pdata->xpcs_lock, flags);
-	pci_write_config_dword(rdev, 0x60, (pdata->xphy_base + pdata->xpcs_window_sel_reg));
-	pci_write_config_dword(rdev, 0x64, index);
-	pci_write_config_dword(rdev, 0x60, pdata->xphy_base + offset);
-	pci_read_config_dword(rdev, 0x64, &ctr_mmd_data);
+	amd_smn_write(0, (pdata->smn_base + pdata->xpcs_window_sel_reg), index);
+	amd_smn_read(0, pdata->smn_base + offset, &ctr_mmd_data);
+
  	if (offset % 4) {
  		ctr_mmd_data = FIELD_PREP(XGBE_GEN_HI_MASK, mmd_data) |
  			       FIELD_GET(XGBE_GEN_LO_MASK, ctr_mmd_data);
@@ -1243,12 +1230,8 @@ static void xgbe_write_mmd_regs_v3(struct xgbe_prv_data *pdata, int prtad,
  			       FIELD_GET(XGBE_GEN_LO_MASK, mmd_data);
  	}
 
-	pci_write_config_dword(rdev, 0x60, (pdata->xphy_base + pdata->xpcs_window_sel_reg));
-	pci_write_config_dword(rdev, 0x64, index);
-	pci_write_config_dword(rdev, 0x60, (pdata->xphy_base + offset));
-	pci_write_config_dword(rdev, 0x64, ctr_mmd_data);
-	pci_dev_put(rdev);
-
+	amd_smn_write(0, (pdata->smn_base + pdata->xpcs_window_sel_reg), index);
+	amd_smn_write(0, (pdata->smn_base + offset), ctr_mmd_data);
  	spin_unlock_irqrestore(&pdata->xpcs_lock, flags);
  }
 
drivers/net/ethernet/amd/xgbe/xgbe-pci.c +5 -5
index db3e8aac3339..135128b5be90 100644
@@ -121,6 +121,7 @@
 
  #include "xgbe.h"
  #include "xgbe-common.h"
+#include "xgbe-smn.h"
 
  static int xgbe_config_multi_msi(struct xgbe_prv_data *pdata)
  {
@@ -304,18 +305,17 @@ static int xgbe_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  		pdata->xpcs_window_sel_reg = PCS_V2_WINDOW_SELECT;
  		break;
  	}
+	pci_dev_put(rdev);
 
  	/* Configure the PCS indirect addressing support */
  	if (pdata->vdata->xpcs_access == XGBE_XPCS_ACCESS_V3) {
  		reg = XP_IOREAD(pdata, XP_PROP_0);
-		pdata->xphy_base = PCS_RN_SMN_BASE_ADDR +
-				   (PCS_RN_PORT_ADDR_SIZE * XP_GET_BITS(reg, XP_PROP_0, PORT_ID));
-		pci_write_config_dword(rdev, 0x60, pdata->xphy_base + (pdata->xpcs_window_def_reg));
-		pci_read_config_dword(rdev, 0x64, &reg);
+		pdata->smn_base = PCS_RN_SMN_BASE_ADDR +
+				  (PCS_RN_PORT_ADDR_SIZE * XP_GET_BITS(reg, XP_PROP_0, PORT_ID));
+		amd_smn_read(0, pdata->smn_base + (pdata->xpcs_window_def_reg), &reg);
  	} else {
  		reg = XPCS32_IOREAD(pdata, pdata->xpcs_window_def_reg);
  	}
-	pci_dev_put(rdev);
 
  	pdata->xpcs_window = XPCS_GET_BITS(reg, PCS_V2_WINDOW_DEF, OFFSET);
  	pdata->xpcs_window <<= 6;
drivers/net/ethernet/amd/xgbe/xgbe-smn.h new file +0 -0
new file mode 100644
index 000000000000..bd25ddc7c869

drivers/net/ethernet/amd/xgbe/xgbe-smn.h line 9

Quoted 9 lines
drivers/net/ethernet/amd/xgbe/xgbe-smn.h new file +9 -0
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * AMD 10Gb Ethernet driver
+ *
+ * Copyright (c) 2023, Advanced Micro Devices, Inc.
+ * All Rights Reserved.
+ *
+ * Author: Raju Rangoju <[email protected]>
+ */

Shouldn't this license match the license in all the other files?

Also, you need header protection here, e.g.:

#ifndef __XGBE_SMN_H__
#define __XGBE_SMN_H__

and a #endif at the end.

Thanks,
Tom

Quoted 31 lines
drivers/net/ethernet/amd/xgbe/xgbe-smn.h new file +18 -0
+
+#ifdef CONFIG_AMD_NB
+
+#include <asm/amd_nb.h>
+
+#else
+
+static inline int amd_smn_write(u16 node, u32 address, u32 value)
+{
+	return -ENODEV;
+}
+
+static inline int amd_smn_read(u16 node, u32 address, u32 *value)
+{
+	return -ENODEV;
+}
+
+#endif
drivers/net/ethernet/amd/xgbe/xgbe.h +1 -1
index dbb1faaf6185..ba45ab0adb8c 100644
@@ -1061,7 +1061,7 @@ struct xgbe_prv_data {
  	struct device *dev;
  	struct platform_device *phy_platdev;
  	struct device *phy_dev;
-	unsigned int xphy_base;
+	unsigned int smn_base;
 
  	/* Version related data */
  	struct xgbe_version_data *vdata;
author maintainer
#4 lore raw

Re: [PATCH v3 net-next 1/3] amd-xgbe: reorganize the code of XPCS access

In reply to Tom Lendacky
Quoted 38 lines · Tom Lendacky
Quoted 23 lines · Raju Rangoju

The xgbe_{read/write}_mmd_regs_v* functions have common code which can be moved to helper functions. Also, the xgbe_pci_probe() needs reorganization.

Add new helper functions to calculate the mmd_address for v1/v2 of xpcs access. And, convert if/else statements in xgbe_pci_probe() to switch case. This helps code look cleaner.

---
  drivers/net/ethernet/amd/xgbe/xgbe-dev.c | 43 ++++++++++++------------ drivers/net/ethernet/amd/xgbe/xgbe-pci.c | 18 +++++++---

  2 files changed, 34 insertions(+), 27 deletions(-)
drivers/net/ethernet/amd/xgbe/xgbe-dev.c +1 -0
index f393228d41c7..6cd003c24a64 100644
@@ -1150,6 +1150,21 @@ static int xgbe_set_gpio(struct xgbe_prv_data 
 *pdata, unsigned int gpio)
       return 0;
   }
+static unsigned int get_mmd_address(struct xgbe_prv_data *pdata, int 

drivers/net/ethernet/amd/xgbe/xgbe-dev.c static int xgbe_set_gpio(struct xgbe_prv_data · line 1163

Quoted 10 lines
drivers/net/ethernet/amd/xgbe/xgbe-dev.c +8 -0
 mmd_reg)
+{
+    return (mmd_reg & XGBE_ADDR_C45) ?
+        mmd_reg & ~XGBE_ADDR_C45 :
+        (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
+}
+
+static unsigned int get_index_offset(struct xgbe_prv_data *pdata, 
 unsigned int mmd_address,
+                     unsigned int *index)

Just my opinion, but this looks confusing to me by updating index and returning offset. And the name is confusing, too. I think it would read better as:

Quoted 9 lines

static void get_pcs_index_and_offset(struct xgbe_prv_data *pdata, unsigned int mmd_address,
                     unsigned int *index, unsigned int *offset)
{
    mmd_address <<= 1;
    *index = mmd_address & ~pdata->xpcs_window_mask;
    *offset = pdata->xpcs_window + (mmd_address &
pdata->xpcs_window_mask);
}

Sure

Quoted 38 lines

Or break this into two functions:

static unsigned int get_pcs_index(struct xgbe_prv_data *pdata, unsigned int mmd_address) {
    return (mmd_address << 1) & ~pdata->xpcs_window_mask;
}

static unsigned int get_pcs_offset(struct xgbe_prv_data *pdata, unsigned int mmd_address) {
    return pdata->xpcs_window + ((mmd_address << 1) &
pdata->xpcs_window_mask);
}

Quoted 25 lines
drivers/net/ethernet/amd/xgbe/xgbe-dev.c +6 -0
+{
+    mmd_address <<= 1;
+    *index = mmd_address & ~pdata->xpcs_window_mask;
+    return pdata->xpcs_window + (mmd_address & pdata->xpcs_window_mask);
+}
+
   static int xgbe_read_mmd_regs_v2(struct xgbe_prv_data *pdata, int prtad,

                   int mmd_reg)
  {

drivers/net/ethernet/amd/xgbe/xgbe-dev.c +1 -4
@@ -1157,10 +1172,7 @@ static int xgbe_read_mmd_regs_v2(struct 
 xgbe_prv_data *pdata, int prtad,
       unsigned int mmd_address, index, offset;
       int mmd_data;
-    if (mmd_reg & XGBE_ADDR_C45)
-        mmd_address = mmd_reg & ~XGBE_ADDR_C45;
-    else
-        mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
+    mmd_address = get_mmd_address(pdata, mmd_reg);
       /* The PCS registers are accessed using mmio. The underlying
        * management interface uses indirect addressing to access the MMD
@@ -1171,9 +1183,7 @@ static int xgbe_read_mmd_regs_v2(struct 
 xgbe_prv_data *pdata, int prtad,
        * register offsets must therefore be adjusted by left shifting the
        * offset 1 bit and reading 16 bits of data.
        */
Quoted 8 lines

drivers/net/ethernet/amd/xgbe/xgbe-dev.c static int xgbe_read_mmd_regs_v2(struct xgbe_prv_data *pdata, int prtad, · line 1186

Quoted 5 lines
drivers/net/ethernet/amd/xgbe/xgbe-dev.c +1 -3
-    mmd_address <<= 1;
-    index = mmd_address & ~pdata->xpcs_window_mask;
-    offset = pdata->xpcs_window + (mmd_address & 
 pdata->xpcs_window_mask);
+    offset = get_index_offset(pdata, mmd_address, &index);

The comment above this code should be moved into the new helper and then removed here and below.

Sure, I'll take care of this.

Quoted 60 lines
Quoted 60 lines
drivers/net/ethernet/amd/xgbe/xgbe-dev.c +0 -0
       spin_lock_irqsave(&pdata->xpcs_lock, flags);

      XPCS32_IOWRITE(pdata, pdata->xpcs_window_sel_reg, index);

drivers/net/ethernet/amd/xgbe/xgbe-dev.c +2 -7
@@ -1189,10 +1199,7 @@ static void xgbe_write_mmd_regs_v2(struct 
 xgbe_prv_data *pdata, int prtad,
       unsigned long flags;
       unsigned int mmd_address, index, offset;
-    if (mmd_reg & XGBE_ADDR_C45)
-        mmd_address = mmd_reg & ~XGBE_ADDR_C45;
-    else
-        mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
+    mmd_address = get_mmd_address(pdata, mmd_reg);
       /* The PCS registers are accessed using mmio. The underlying
        * management interface uses indirect addressing to access the MMD
@@ -1203,9 +1210,7 @@ static void xgbe_write_mmd_regs_v2(struct 
 xgbe_prv_data *pdata, int prtad,
        * register offsets must therefore be adjusted by left shifting the
        * offset 1 bit and writing 16 bits of data.
        */
-    mmd_address <<= 1;
-    index = mmd_address & ~pdata->xpcs_window_mask;
-    offset = pdata->xpcs_window + (mmd_address & 
 pdata->xpcs_window_mask);
+    offset = get_index_offset(pdata, mmd_address, &index);
       spin_lock_irqsave(&pdata->xpcs_lock, flags);

      XPCS32_IOWRITE(pdata, pdata->xpcs_window_sel_reg, index);

drivers/net/ethernet/amd/xgbe/xgbe-dev.c +2 -8
@@ -1220,10 +1225,7 @@ static int xgbe_read_mmd_regs_v1(struct 
 xgbe_prv_data *pdata, int prtad,
       unsigned int mmd_address;
       int mmd_data;
-    if (mmd_reg & XGBE_ADDR_C45)
-        mmd_address = mmd_reg & ~XGBE_ADDR_C45;
-    else
-        mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
+    mmd_address = get_mmd_address(pdata, mmd_reg);
       /* The PCS registers are accessed using mmio. The underlying APB3
        * management interface uses indirect addressing to access the MMD
@@ -1248,10 +1250,7 @@ static void xgbe_write_mmd_regs_v1(struct 
 xgbe_prv_data *pdata, int prtad,
       unsigned int mmd_address;
       unsigned long flags;
-    if (mmd_reg & XGBE_ADDR_C45)
-        mmd_address = mmd_reg & ~XGBE_ADDR_C45;
-    else
-        mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
+    mmd_address = get_mmd_address(pdata, mmd_reg);
       /* The PCS registers are accessed using mmio. The underlying APB3
        * management interface uses indirect addressing to access the MMD
drivers/net/ethernet/amd/xgbe/xgbe-pci.c +1 -2
index f409d7bd1f1e..8b0c1e450b7e 100644
@@ -274,12 +274,18 @@ static int xgbe_pci_probe(struct pci_dev *pdev, 
 const struct pci_device_id *id)
       /* Set the PCS indirect addressing definition registers */
       rdev = pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(0, 0));
-    if (rdev &&
-        (rdev->vendor == PCI_VENDOR_ID_AMD) && (rdev->device == 
 0x15d0)) {
+
Quoted 8 lines

drivers/net/ethernet/amd/xgbe/xgbe-pci.c static int xgbe_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 281

Quoted 4 lines
drivers/net/ethernet/amd/xgbe/xgbe-pci.c +4 -0
+    if (!(rdev && rdev->vendor == PCI_VENDOR_ID_AMD)) {
+        ret = -ENODEV;
+        goto err_pci_enable;
+    }

This is different behavior compared to today. Today, everything would default to the final "else" statement. With this patch you have a possibility of failing probe now.

This was done to skip the cases where rdev is NULL or vendor != AMD. Not sure if I'm missing something.

Quoted 27 lines

Thanks,
Tom

Quoted 23 lines
drivers/net/ethernet/amd/xgbe/xgbe-pci.c +8 -3
+
+    switch (rdev->device) {
+    case 0x15d0:
           pdata->xpcs_window_def_reg = PCS_V2_RV_WINDOW_DEF;
           pdata->xpcs_window_sel_reg = PCS_V2_RV_WINDOW_SELECT;
-    } else if (rdev && (rdev->vendor == PCI_VENDOR_ID_AMD) &&
-           (rdev->device == 0x14b5)) {
+        break;
+    case 0x14b5:
           pdata->xpcs_window_def_reg = PCS_V2_YC_WINDOW_DEF;
           pdata->xpcs_window_sel_reg = PCS_V2_YC_WINDOW_SELECT;
@@ -288,9 +294,11 @@ static int xgbe_pci_probe(struct pci_dev *pdev, 
 const struct pci_device_id *id)
           /* Yellow Carp devices do not need rrc */
           pdata->vdata->enable_rrc = 0;
-    } else {
+        break;
+    default:
           pdata->xpcs_window_def_reg = PCS_V2_WINDOW_DEF;
           pdata->xpcs_window_sel_reg = PCS_V2_WINDOW_SELECT;
+        break;
       }
       pci_dev_put(rdev);
author maintainer
#8 lore raw

Re: [PATCH v3 net-next 2/3] amd-xgbe: add support for Crater ethernet device

In reply to Tom Lendacky
Quoted 16 lines · Tom Lendacky
Quoted 15 lines · Raju Rangoju

Add the necessary support to enable Crater ethernet device. Since the BAR1 address cannot be used to access the XPCS registers on Crater, use the pci_{read/write}_config_dword calls. Also, include the new pci device id 0x1641 to register Crater device with PCIe.

---
  drivers/net/ethernet/amd/xgbe/xgbe-common.h |  5 ++ drivers/net/ethernet/amd/xgbe/xgbe-dev.c    | 93 +++++++++++++++++++++ drivers/net/ethernet/amd/xgbe/xgbe-pci.c    | 35 +++++++- drivers/net/ethernet/amd/xgbe/xgbe.h        |  6 ++

  4 files changed, 137 insertions(+), 2 deletions(-)
Quoted 12 lines

drivers/net/ethernet/amd/xgbe/xgbe-common.h line 904

Quoted 10 lines
drivers/net/ethernet/amd/xgbe/xgbe-common.h +2 -0
index 3b70f6737633..e1f70f0528ef 100644
@@ -900,6 +900,11 @@
   #define PCS_V2_RV_WINDOW_SELECT        0x1064
   #define PCS_V2_YC_WINDOW_DEF        0x18060
   #define PCS_V2_YC_WINDOW_SELECT        0x18064
+#define PCS_V2_RN_WINDOW_DEF        0xF8078
+#define PCS_V2_RN_WINDOW_SELECT        0xF807c

Should this be PCS_V3_ (here and below) ??

Yes, it should be PCS_V3. I'll take care of it in next version.

Quoted 6 lines

drivers/net/ethernet/amd/xgbe/xgbe-common.h line 907

Quoted 4 lines
drivers/net/ethernet/amd/xgbe/xgbe-common.h +3 -0
+
+#define PCS_RN_SMN_BASE_ADDR        0x11E00000
+#define PCS_RN_PORT_ADDR_SIZE        0x100000

All hex characters should be consistent and in lower case.

Sure

Quoted 100 lines
Quoted 89 lines
drivers/net/ethernet/amd/xgbe/xgbe-common.h +0 -0
   /* PCS register entry bit positions and sizes */
   #define PCS_V2_WINDOW_DEF_OFFSET_INDEX    6
drivers/net/ethernet/amd/xgbe/xgbe-dev.c +1 -0
index 6cd003c24a64..a9eb2ffa9f73 100644
@@ -120,6 +120,7 @@
   #include <linux/bitrev.h>
   #include <linux/crc32.h>
   #include <linux/crc32poly.h>
+#include <linux/pci.h>
   #include "xgbe.h"
   #include "xgbe-common.h"
@@ -1165,6 +1166,92 @@ static unsigned int get_index_offset(struct 

xgbe_prv_data *pdata, unsigned int m
      return pdata->xpcs_window + (mmd_address & pdata->xpcs_window_mask); }

drivers/net/ethernet/amd/xgbe/xgbe-dev.c +27 -0
+static int xgbe_read_mmd_regs_v3(struct xgbe_prv_data *pdata, int prtad,
+                 int mmd_reg)
+{
+    unsigned int mmd_address, index, offset;
+    struct pci_dev *rdev;
+    unsigned long flags;
+    int mmd_data;
+
+    rdev = pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(0, 0));
+    if (!rdev)
+        return 0;
+
+    mmd_address = get_mmd_address(pdata, mmd_reg);
+
+    /* The PCS registers are accessed using mmio. The underlying
+     * management interface uses indirect addressing to access the MMD
+     * register sets. This requires accessing of the PCS register in two
+     * phases, an address phase and a data phase.
+     *
+     * The mmio interface is based on 16-bit offsets and values. All
+     * register offsets must therefore be adjusted by left shifting the
+     * offset 1 bit and reading 16 bits of data.
+     */
+    offset = get_index_offset(pdata, mmd_address, &index);
+
+    spin_lock_irqsave(&pdata->xpcs_lock, flags);
+    pci_write_config_dword(rdev, 0x60, (pdata->xphy_base + 

pdata->xpcs_window_sel_reg));
+    pci_write_config_dword(rdev, 0x64, index);
+    pci_write_config_dword(rdev, 0x60, pdata->xphy_base + offset); +    pci_read_config_dword(rdev, 0x64, &mmd_data);
+    mmd_data = (offset % 4) ? FIELD_GET(XGBE_GEN_HI_MASK, mmd_data) :
+                  FIELD_GET(XGBE_GEN_LO_MASK, mmd_data);
+    pci_dev_put(rdev);

drivers/net/ethernet/amd/xgbe/xgbe-dev.c +7 -0
+
+    spin_unlock_irqrestore(&pdata->xpcs_lock, flags);
+
+    return mmd_data;
+}
+
+static void xgbe_write_mmd_regs_v3(struct xgbe_prv_data *pdata, int 

prtad,
+                   int mmd_reg, int mmd_data)

drivers/net/ethernet/amd/xgbe/xgbe-dev.c +24 -0
+{
+    unsigned int mmd_address, index, offset, ctr_mmd_data;
+    struct pci_dev *rdev;
+    unsigned long flags;
+
+    rdev = pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(0, 0));
+    if (!rdev)
+        return;
+
+    mmd_address = get_mmd_address(pdata, mmd_reg);
+
+    /* The PCS registers are accessed using mmio. The underlying
+     * management interface uses indirect addressing to access the MMD
+     * register sets. This requires accessing of the PCS register in two
+     * phases, an address phase and a data phase.
+     *
+     * The mmio interface is based on 16-bit offsets and values. All
+     * register offsets must therefore be adjusted by left shifting the
+     * offset 1 bit and writing 16 bits of data.
+     */
+    offset = get_index_offset(pdata, mmd_address, &index);
+
+    spin_lock_irqsave(&pdata->xpcs_lock, flags);
+    pci_write_config_dword(rdev, 0x60, (pdata->xphy_base + 

pdata->xpcs_window_sel_reg));
+    pci_write_config_dword(rdev, 0x64, index);
+    pci_write_config_dword(rdev, 0x60, pdata->xphy_base + offset); +    pci_read_config_dword(rdev, 0x64, &ctr_mmd_data);

Quoted 8 lines

+    if (offset % 4) {
+        ctr_mmd_data = FIELD_PREP(XGBE_GEN_HI_MASK, mmd_data) | +                   FIELD_GET(XGBE_GEN_LO_MASK, ctr_mmd_data); +    } else {
+        ctr_mmd_data = FIELD_PREP(XGBE_GEN_HI_MASK,
+                      FIELD_GET(XGBE_GEN_HI_MASK, ctr_mmd_data)) | +                   FIELD_GET(XGBE_GEN_LO_MASK, mmd_data); +    }

Braces aren't necessary.

Quoted 1 line

Also, just curious, what is the "ctr" prefix meant to imply here?

ctr = crater. Will use the full name instead to avoid confusion.

Quoted 40 lines
Quoted 40 lines

+
+    pci_write_config_dword(rdev, 0x60, (pdata->xphy_base + pdata->xpcs_window_sel_reg));
+    pci_write_config_dword(rdev, 0x64, index);
+    pci_write_config_dword(rdev, 0x60, (pdata->xphy_base + offset)); +    pci_write_config_dword(rdev, 0x64, ctr_mmd_data);
+    pci_dev_put(rdev);

drivers/net/ethernet/amd/xgbe/xgbe-dev.c +4 -0
+
+    spin_unlock_irqrestore(&pdata->xpcs_lock, flags);
+}
+

  static int xgbe_read_mmd_regs_v2(struct xgbe_prv_data *pdata, int prtad, int mmd_reg)
  {

drivers/net/ethernet/amd/xgbe/xgbe-dev.c +0 -0
@@ -1274,6 +1361,9 @@ static int xgbe_read_mmd_regs(struct 
 xgbe_prv_data *pdata, int prtad,

      case XGBE_XPCS_ACCESS_V1:
          return xgbe_read_mmd_regs_v1(pdata, prtad, mmd_reg); +    case XGBE_XPCS_ACCESS_V3:
+        return xgbe_read_mmd_regs_v3(pdata, prtad, mmd_reg); +
      case XGBE_XPCS_ACCESS_V2:
      default:
          return xgbe_read_mmd_regs_v2(pdata, prtad, mmd_reg);

drivers/net/ethernet/amd/xgbe/xgbe-dev.c +0 -0
@@ -1287,6 +1377,9 @@ static void xgbe_write_mmd_regs(struct 
 xgbe_prv_data *pdata, int prtad,

      case XGBE_XPCS_ACCESS_V1:
          return xgbe_write_mmd_regs_v1(pdata, prtad, mmd_reg, mmd_data); +    case XGBE_XPCS_ACCESS_V3:
+        return xgbe_write_mmd_regs_v3(pdata, prtad, mmd_reg, mmd_data); +
      case XGBE_XPCS_ACCESS_V2:
      default:
          return xgbe_write_mmd_regs_v2(pdata, prtad, mmd_reg, mmd_data);

drivers/net/ethernet/amd/xgbe/xgbe-pci.c +0 -0
index 8b0c1e450b7e..db3e8aac3339 100644
Quoted 8 lines

drivers/net/ethernet/amd/xgbe/xgbe-pci.c static int xgbe_pci_probe(struct pci_dev *pdev, · line 299

Quoted 6 lines
drivers/net/ethernet/amd/xgbe/xgbe-pci.c +1 -0
@@ -295,15 +295,28 @@ static int xgbe_pci_probe(struct pci_dev *pdev, 
 const struct pci_device_id *id)
           /* Yellow Carp devices do not need rrc */
           pdata->vdata->enable_rrc = 0;
           break;
+    case 0x1630:

What PCI ID is this, it doesn't match the 0x1641 added below?

0x1630 is root hub

Quoted 91 lines

Thanks,
Tom

Quoted 87 lines
drivers/net/ethernet/amd/xgbe/xgbe-pci.c +3 -0
+        pdata->xpcs_window_def_reg = PCS_V2_RN_WINDOW_DEF;
+        pdata->xpcs_window_sel_reg = PCS_V2_RN_WINDOW_SELECT;
+        break;

      default:
          pdata->xpcs_window_def_reg = PCS_V2_WINDOW_DEF; pdata->xpcs_window_sel_reg = PCS_V2_WINDOW_SELECT; break;
      }
-    pci_dev_put(rdev);
      /* Configure the PCS indirect addressing support */
-    reg = XPCS32_IOREAD(pdata, pdata->xpcs_window_def_reg);
+    if (pdata->vdata->xpcs_access == XGBE_XPCS_ACCESS_V3) {
+        reg = XP_IOREAD(pdata, XP_PROP_0);
+        pdata->xphy_base = PCS_RN_SMN_BASE_ADDR +
+                   (PCS_RN_PORT_ADDR_SIZE * XP_GET_BITS(reg, XP_PROP_0, PORT_ID));
+        pci_write_config_dword(rdev, 0x60, pdata->xphy_base + (pdata->xpcs_window_def_reg));
+        pci_read_config_dword(rdev, 0x64, &reg);
+    } else {
+        reg = XPCS32_IOREAD(pdata, pdata->xpcs_window_def_reg); +    }
+    pci_dev_put(rdev);
+
      pdata->xpcs_window = XPCS_GET_BITS(reg, PCS_V2_WINDOW_DEF, OFFSET); pdata->xpcs_window <<= 6;
      pdata->xpcs_window_size = XPCS_GET_BITS(reg, PCS_V2_WINDOW_DEF, SIZE);

drivers/net/ethernet/amd/xgbe/xgbe-pci.c +18 -0
@@ -481,6 +494,22 @@ static int __maybe_unused xgbe_pci_resume(struct 
 device *dev)
       return ret;
   }
+static struct xgbe_version_data xgbe_v3 = {
+    .init_function_ptrs_phy_impl    = xgbe_init_function_ptrs_phy_v2,
+    .xpcs_access            = XGBE_XPCS_ACCESS_V3,
+    .mmc_64bit            = 1,
+    .tx_max_fifo_size        = 65536,
+    .rx_max_fifo_size        = 65536,
+    .tx_tstamp_workaround        = 1,
+    .ecc_support            = 1,
+    .i2c_support            = 1,
+    .irq_reissue_support        = 1,
+    .tx_desc_prefetch        = 5,
+    .rx_desc_prefetch        = 5,
+    .an_cdr_workaround        = 0,
+    .enable_rrc            = 0,
+};
+
   static struct xgbe_version_data xgbe_v2a = {
       .init_function_ptrs_phy_impl    = xgbe_init_function_ptrs_phy_v2,
       .xpcs_access            = XGBE_XPCS_ACCESS_V2,
@@ -518,6 +547,8 @@ static const struct pci_device_id xgbe_pci_table[] 
 = {
         .driver_data = (kernel_ulong_t)&xgbe_v2a },
       { PCI_VDEVICE(AMD, 0x1459),
         .driver_data = (kernel_ulong_t)&xgbe_v2b },
+    { PCI_VDEVICE(AMD, 0x1641),
+      .driver_data = (kernel_ulong_t)&xgbe_v3 },
       /* Last entry must be zero */
       { 0, }

  };

drivers/net/ethernet/amd/xgbe/xgbe.h +6 -0
index ad136ed493ed..dbb1faaf6185 100644
@@ -347,6 +347,10 @@
               (_src)->link_modes._sname,        \
               __ETHTOOL_LINK_MODE_MASK_NBITS)
+/* Generic low and high masks */
+#define XGBE_GEN_HI_MASK    GENMASK(31, 16)
+#define XGBE_GEN_LO_MASK    GENMASK(15, 0)
+
   struct xgbe_prv_data;
   struct xgbe_packet_data {
@@ -565,6 +569,7 @@ enum xgbe_speed {
   enum xgbe_xpcs_access {
       XGBE_XPCS_ACCESS_V1 = 0,
       XGBE_XPCS_ACCESS_V2,
+    XGBE_XPCS_ACCESS_V3,
   };
   enum xgbe_an_mode {
@@ -1056,6 +1061,7 @@ struct xgbe_prv_data {
       struct device *dev;
       struct platform_device *phy_platdev;
       struct device *phy_dev;
+    unsigned int xphy_base;
       /* Version related data */
       struct xgbe_version_data *vdata;
#5 lore raw

Re: [PATCH v3 net-next 1/3] amd-xgbe: reorganize the code of XPCS access

In reply to Raju Rangoju
Quoted 34 lines · Raju Rangoju
Quoted 16 lines · Tom Lendacky
Quoted 14 lines · Raju Rangoju

The xgbe_{read/write}_mmd_regs_v* functions have common code which can be moved to helper functions. Also, the xgbe_pci_probe() needs reorganization.

Add new helper functions to calculate the mmd_address for v1/v2 of xpcs access. And, convert if/else statements in xgbe_pci_probe() to switch case. This helps code look cleaner.

---
  drivers/net/ethernet/amd/xgbe/xgbe-dev.c | 43 ++++++++++++------------ drivers/net/ethernet/amd/xgbe/xgbe-pci.c | 18 +++++++---

  2 files changed, 34 insertions(+), 27 deletions(-)
Quoted 16 lines

drivers/net/ethernet/amd/xgbe/xgbe-pci.c static int xgbe_pci_probe(struct pci_dev *pdev, · line 282

Quoted 12 lines
drivers/net/ethernet/amd/xgbe/xgbe-pci.c +5 -2
@@ -274,12 +274,18 @@ static int xgbe_pci_probe(struct pci_dev *pdev, 
 const struct pci_device_id *id)
       /* Set the PCS indirect addressing definition registers */
       rdev = pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(0, 0));
-    if (rdev &&
-        (rdev->vendor == PCI_VENDOR_ID_AMD) && (rdev->device == 
 0x15d0)) {
+
+    if (!(rdev && rdev->vendor == PCI_VENDOR_ID_AMD)) {
+        ret = -ENODEV;
+        goto err_pci_enable;
+    }

This is different behavior compared to today. Today, everything would default to the final "else" statement. With this patch you have a possibility of failing probe now.

Quoted 1 line

This was done to skip the cases where rdev is NULL or vendor != AMD. Not sure if I'm missing something.

Right, I'm just pointing out that if rdev == NULL or vendor != AMD then, today, the else path is taken. With this patch you'll fail the probe with -ENODEV.

Thanks,
Tom

Quoted 1 line
#9 lore raw

Re: [PATCH v3 net-next 2/3] amd-xgbe: add support for Crater ethernet device

In reply to Raju Rangoju
Quoted 34 lines · Raju Rangoju
Quoted 33 lines · Tom Lendacky
Quoted 31 lines · Raju Rangoju

Add the necessary support to enable Crater ethernet device. Since the BAR1 address cannot be used to access the XPCS registers on Crater, use the pci_{read/write}_config_dword calls. Also, include the new pci device id 0x1641 to register Crater device with PCIe.

---
  drivers/net/ethernet/amd/xgbe/xgbe-common.h |  5 ++ drivers/net/ethernet/amd/xgbe/xgbe-dev.c    | 93 +++++++++++++++++++++ drivers/net/ethernet/amd/xgbe/xgbe-pci.c    | 35 +++++++- drivers/net/ethernet/amd/xgbe/xgbe.h        |  6 ++

  4 files changed, 137 insertions(+), 2 deletions(-)

+
+    spin_lock_irqsave(&pdata->xpcs_lock, flags);
+    pci_write_config_dword(rdev, 0x60, (pdata->xphy_base + pdata->xpcs_window_sel_reg));
+    pci_write_config_dword(rdev, 0x64, index);
+    pci_write_config_dword(rdev, 0x60, pdata->xphy_base + offset); +    pci_read_config_dword(rdev, 0x64, &ctr_mmd_data);
+    if (offset % 4) {
+        ctr_mmd_data = FIELD_PREP(XGBE_GEN_HI_MASK, mmd_data) | +                   FIELD_GET(XGBE_GEN_LO_MASK, ctr_mmd_data); +    } else {
+        ctr_mmd_data = FIELD_PREP(XGBE_GEN_HI_MASK,
+                      FIELD_GET(XGBE_GEN_HI_MASK, ctr_mmd_data)) | +                   FIELD_GET(XGBE_GEN_LO_MASK, mmd_data); +    }

Quoted 5 lines
Quoted 3 lines

Braces aren't necessary.

Also, just curious, what is the "ctr" prefix meant to imply here?

ctr = crater. Will use the full name instead to avoid confusion.

You should probably make it generic and use pci_ or tmp_ (or smn_ if you end up squashing patches 2 and 3), since this may not be "crater" specific if this is used by another device ID in the future.

Quoted 13 lines
Quoted 4 lines
Quoted 8 lines

drivers/net/ethernet/amd/xgbe/xgbe-pci.c static int xgbe_pci_probe(struct pci_dev *pdev, · line 299

Quoted 6 lines
drivers/net/ethernet/amd/xgbe/xgbe-pci.c +1 -0
@@ -295,15 +295,28 @@ static int xgbe_pci_probe(struct pci_dev *pdev, 
 const struct pci_device_id *id)
           /* Yellow Carp devices do not need rrc */
           pdata->vdata->enable_rrc = 0;
           break;
+    case 0x1630:

What PCI ID is this, it doesn't match the 0x1641 added below?

Quoted 1 line

0x1630 is root hub

Ah, right, sorry for the noise.

Thanks,
Tom

Quoted 1 line
author maintainer
#12 lore raw

Re: [PATCH v3 net-next 3/3] amd-xgbe: use smn functions to avoid race

In reply to Tom Lendacky
Quoted 8 lines · Tom Lendacky
Quoted 5 lines · Raju Rangoju

Some of the ethernet add-in-cards have dual PHY but share a single MDIO line (between the ports). In such cases, link inconsistencies are noticed during the heavy traffic and during reboot stress tests.

So, use the SMN calls to avoid the race conditions.

So this patch replaces all the PCI accesses you added in patch #2, so why not just do this from the start?

Yes, that is correct. It was done to maintain the history and that will be reference as to why SMN is used over regular PCI accesses in this case.

Quoted 150 lines
Quoted 138 lines

---

  drivers/net/ethernet/amd/xgbe/xgbe-dev.c | 33 ++++++------------------
  drivers/net/ethernet/amd/xgbe/xgbe-pci.c | 10 +++----
  drivers/net/ethernet/amd/xgbe/xgbe-smn.h | 27 +++++++++++++++++++
  drivers/net/ethernet/amd/xgbe/xgbe.h     |  2 +-
  4 files changed, 41 insertions(+), 31 deletions(-)

  create mode 100644 drivers/net/ethernet/amd/xgbe/xgbe-smn.h

drivers/net/ethernet/amd/xgbe/xgbe-dev.c +3 -9
index a9eb2ffa9f73..8d8876ab258c 100644
@@ -124,6 +124,7 @@
   #include "xgbe.h"
   #include "xgbe-common.h"
+#include "xgbe-smn.h"
   static inline unsigned int xgbe_get_max_frame(struct xgbe_prv_data *pdata)
   {
@@ -1170,14 +1171,9 @@ static int xgbe_read_mmd_regs_v3(struct 
 xgbe_prv_data *pdata, int prtad,
                    int mmd_reg)
   {
       unsigned int mmd_address, index, offset;
-    struct pci_dev *rdev;
       unsigned long flags;
       int mmd_data;
-    rdev = pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(0, 0));
-    if (!rdev)
-        return 0;
-
       mmd_address = get_mmd_address(pdata, mmd_reg);
       /* The PCS registers are accessed using mmio. The underlying
@@ -1192,13 +1188,10 @@ static int xgbe_read_mmd_regs_v3(struct 
 xgbe_prv_data *pdata, int prtad,
       offset = get_index_offset(pdata, mmd_address, &index);
       spin_lock_irqsave(&pdata->xpcs_lock, flags);
-    pci_write_config_dword(rdev, 0x60, (pdata->xphy_base + 
 pdata->xpcs_window_sel_reg));
-    pci_write_config_dword(rdev, 0x64, index);
-    pci_write_config_dword(rdev, 0x60, pdata->xphy_base + offset);
-    pci_read_config_dword(rdev, 0x64, &mmd_data);
+    amd_smn_write(0, (pdata->smn_base + pdata->xpcs_window_sel_reg), 
 index);
+    amd_smn_read(0, pdata->smn_base + offset, &mmd_data);

      mmd_data = (offset % 4) ? FIELD_GET(XGBE_GEN_HI_MASK, mmd_data) :
                    FIELD_GET(XGBE_GEN_LO_MASK, mmd_data);
-    pci_dev_put(rdev); spin_unlock_irqrestore(&pdata->xpcs_lock, flags);

drivers/net/ethernet/amd/xgbe/xgbe-dev.c +3 -9
@@ -1209,13 +1202,8 @@ static void xgbe_write_mmd_regs_v3(struct 
 xgbe_prv_data *pdata, int prtad,
                      int mmd_reg, int mmd_data)
   {
       unsigned int mmd_address, index, offset, ctr_mmd_data;
-    struct pci_dev *rdev;
       unsigned long flags;
-    rdev = pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(0, 0));
-    if (!rdev)
-        return;
-
       mmd_address = get_mmd_address(pdata, mmd_reg);
       /* The PCS registers are accessed using mmio. The underlying
@@ -1230,10 +1218,9 @@ static void xgbe_write_mmd_regs_v3(struct 
 xgbe_prv_data *pdata, int prtad,
       offset = get_index_offset(pdata, mmd_address, &index);
       spin_lock_irqsave(&pdata->xpcs_lock, flags);
-    pci_write_config_dword(rdev, 0x60, (pdata->xphy_base + 
 pdata->xpcs_window_sel_reg));
-    pci_write_config_dword(rdev, 0x64, index);
-    pci_write_config_dword(rdev, 0x60, pdata->xphy_base + offset);
-    pci_read_config_dword(rdev, 0x64, &ctr_mmd_data);
+    amd_smn_write(0, (pdata->smn_base + pdata->xpcs_window_sel_reg), 
 index);
+    amd_smn_read(0, pdata->smn_base + offset, &ctr_mmd_data);
+
       if (offset % 4) {

          ctr_mmd_data = FIELD_PREP(XGBE_GEN_HI_MASK, mmd_data) | FIELD_GET(XGBE_GEN_LO_MASK, ctr_mmd_data);

drivers/net/ethernet/amd/xgbe/xgbe-dev.c +2 -6
@@ -1243,12 +1230,8 @@ static void xgbe_write_mmd_regs_v3(struct 
 xgbe_prv_data *pdata, int prtad,
                      FIELD_GET(XGBE_GEN_LO_MASK, mmd_data);
       }
-    pci_write_config_dword(rdev, 0x60, (pdata->xphy_base + 
 pdata->xpcs_window_sel_reg));
-    pci_write_config_dword(rdev, 0x64, index);
-    pci_write_config_dword(rdev, 0x60, (pdata->xphy_base + offset));
-    pci_write_config_dword(rdev, 0x64, ctr_mmd_data);
-    pci_dev_put(rdev);
-
+    amd_smn_write(0, (pdata->smn_base + pdata->xpcs_window_sel_reg), 
 index);
+    amd_smn_write(0, (pdata->smn_base + offset), ctr_mmd_data);
       spin_unlock_irqrestore(&pdata->xpcs_lock, flags);

  }

drivers/net/ethernet/amd/xgbe/xgbe-pci.c +5 -4
index db3e8aac3339..135128b5be90 100644
@@ -121,6 +121,7 @@
   #include "xgbe.h"
   #include "xgbe-common.h"
+#include "xgbe-smn.h"
   static int xgbe_config_multi_msi(struct xgbe_prv_data *pdata)
   {
@@ -304,18 +305,17 @@ static int xgbe_pci_probe(struct pci_dev *pdev, 
 const struct pci_device_id *id)
           pdata->xpcs_window_sel_reg = PCS_V2_WINDOW_SELECT;
           break;
       }
+    pci_dev_put(rdev);
       /* Configure the PCS indirect addressing support */
       if (pdata->vdata->xpcs_access == XGBE_XPCS_ACCESS_V3) {
           reg = XP_IOREAD(pdata, XP_PROP_0);
-        pdata->xphy_base = PCS_RN_SMN_BASE_ADDR +
-                   (PCS_RN_PORT_ADDR_SIZE * XP_GET_BITS(reg, 
 XP_PROP_0, PORT_ID));
-        pci_write_config_dword(rdev, 0x60, pdata->xphy_base + 
 (pdata->xpcs_window_def_reg));
-        pci_read_config_dword(rdev, 0x64, &reg);
+        pdata->smn_base = PCS_RN_SMN_BASE_ADDR +
+                  (PCS_RN_PORT_ADDR_SIZE * XP_GET_BITS(reg, 
 XP_PROP_0, PORT_ID));
+        amd_smn_read(0, pdata->smn_base + 
 (pdata->xpcs_window_def_reg), &reg);
       } else {
           reg = XPCS32_IOREAD(pdata, pdata->xpcs_window_def_reg);

      }
-    pci_dev_put(rdev);
      pdata->xpcs_window = XPCS_GET_BITS(reg, PCS_V2_WINDOW_DEF, OFFSET); pdata->xpcs_window <<= 6;

drivers/net/ethernet/amd/xgbe/xgbe-smn.h new file +0 -0
new file mode 100644
index 000000000000..bd25ddc7c869

drivers/net/ethernet/amd/xgbe/xgbe-smn.h line 9

Quoted 9 lines
drivers/net/ethernet/amd/xgbe/xgbe-smn.h new file +9 -0
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * AMD 10Gb Ethernet driver
+ *
+ * Copyright (c) 2023, Advanced Micro Devices, Inc.
+ * All Rights Reserved.
+ *
+ * Author: Raju Rangoju <[email protected]>
+ */

Shouldn't this license match the license in all the other files?

Quoted 6 lines

Also, you need header protection here, e.g.:

#ifndef __XGBE_SMN_H__
#define __XGBE_SMN_H__

and a #endif at the end.

Sure Tom, I'll take care of this.

Quoted 34 lines

Thanks,
Tom

Quoted 30 lines
drivers/net/ethernet/amd/xgbe/xgbe-smn.h new file +18 -0
+
+#ifdef CONFIG_AMD_NB
+
+#include <asm/amd_nb.h>
+
+#else
+
+static inline int amd_smn_write(u16 node, u32 address, u32 value)
+{
+    return -ENODEV;
+}
+
+static inline int amd_smn_read(u16 node, u32 address, u32 *value)
+{
+    return -ENODEV;
+}
+
+#endif
drivers/net/ethernet/amd/xgbe/xgbe.h +1 -1
index dbb1faaf6185..ba45ab0adb8c 100644
@@ -1061,7 +1061,7 @@ struct xgbe_prv_data {
       struct device *dev;
       struct platform_device *phy_platdev;
       struct device *phy_dev;
-    unsigned int xphy_base;
+    unsigned int smn_base;
       /* Version related data */
       struct xgbe_version_data *vdata;
#13 lore raw

Re: [PATCH v3 net-next 3/3] amd-xgbe: use smn functions to avoid race

Quoted 8 lines · Raju Rangoju
Quoted 5 lines · Tom Lendacky
Quoted 4 lines · Raju Rangoju

Some of the ethernet add-in-cards have dual PHY but share a single MDIO line (between the ports). In such cases, link inconsistencies are noticed during the heavy traffic and during reboot stress tests.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

So, use the SMN calls to avoid the race conditions.

So this patch replaces all the PCI accesses you added in patch #2, so why not just do this from the start?

Yes, that is correct. It was done to maintain the history and that will be reference as to why SMN is used over regular PCI accesses in this case.

Seems unnecessary to me. Adding a comment in the commit log and the code that states why SMN is used instead of PCI and how it fixes the race condition would be enough. Your call... but since patch #2 enables the device, this could cause a bisect issue.

Thanks,
Tom

Quoted 1 line