diff mbox series

[v5,07/24] net/ngbe: set MAC type and LAN id

Message ID 20210602094108.1575640-8-jiawenwu@trustnetic.com (mailing list archive)
State Changes Requested, archived
Delegated to: Andrew Rybchenko
Headers show
Series net: ngbe PMD | expand

Checks

Context Check Description
ci/checkpatch warning coding style issues

Commit Message

Jiawen Wu June 2, 2021, 9:40 a.m. UTC
Initialize the shared code.

Signed-off-by: Jiawen Wu <jiawenwu@trustnetic.com>
---
 drivers/net/ngbe/base/ngbe_dummy.h |  37 +++++++++
 drivers/net/ngbe/base/ngbe_hw.c    | 122 +++++++++++++++++++++++++++++
 drivers/net/ngbe/base/ngbe_hw.h    |   5 ++
 drivers/net/ngbe/base/ngbe_osdep.h |   2 +
 drivers/net/ngbe/base/ngbe_type.h  |  53 +++++++++++++
 drivers/net/ngbe/ngbe_ethdev.c     |   8 ++
 6 files changed, 227 insertions(+)
 create mode 100644 drivers/net/ngbe/base/ngbe_dummy.h
diff mbox series

Patch

diff --git a/drivers/net/ngbe/base/ngbe_dummy.h b/drivers/net/ngbe/base/ngbe_dummy.h
new file mode 100644
index 0000000000..75b4e50bca
--- /dev/null
+++ b/drivers/net/ngbe/base/ngbe_dummy.h
@@ -0,0 +1,37 @@ 
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2018-2020 Beijing WangXun Technology Co., Ltd.
+ */
+
+#ifndef _NGBE_TYPE_DUMMY_H_
+#define _NGBE_TYPE_DUMMY_H_
+
+#ifdef TUP
+#elif defined(__GNUC__)
+#define TUP(x) x##_unused ngbe_unused
+#elif defined(__LCLINT__)
+#define TUP(x) x /*@unused@*/
+#else
+#define TUP(x) x
+#endif /*TUP*/
+#define TUP0 TUP(p0)
+#define TUP1 TUP(p1)
+#define TUP2 TUP(p2)
+#define TUP3 TUP(p3)
+#define TUP4 TUP(p4)
+#define TUP5 TUP(p5)
+#define TUP6 TUP(p6)
+#define TUP7 TUP(p7)
+#define TUP8 TUP(p8)
+#define TUP9 TUP(p9)
+
+/* struct ngbe_bus_operations */
+static inline void ngbe_bus_set_lan_id_dummy(struct ngbe_hw *TUP0)
+{
+}
+static inline void ngbe_init_ops_dummy(struct ngbe_hw *hw)
+{
+	hw->bus.set_lan_id = ngbe_bus_set_lan_id_dummy;
+}
+
+#endif /* _NGBE_TYPE_DUMMY_H_ */
+
diff --git a/drivers/net/ngbe/base/ngbe_hw.c b/drivers/net/ngbe/base/ngbe_hw.c
index 0fab47f272..014bb0faee 100644
--- a/drivers/net/ngbe/base/ngbe_hw.c
+++ b/drivers/net/ngbe/base/ngbe_hw.c
@@ -3,8 +3,74 @@ 
  * Copyright(c) 2010-2017 Intel Corporation
  */
 
+#include "ngbe_type.h"
 #include "ngbe_hw.h"
 
+/**
+ *  ngbe_set_lan_id_multi_port - Set LAN id for PCIe multiple port devices
+ *  @hw: pointer to the HW structure
+ *
+ *  Determines the LAN function id by reading memory-mapped registers and swaps
+ *  the port value if requested, and set MAC instance for devices.
+ **/
+void ngbe_set_lan_id_multi_port(struct ngbe_hw *hw)
+{
+	struct ngbe_bus_info *bus = &hw->bus;
+	u32 reg = 0;
+
+	DEBUGFUNC("ngbe_set_lan_id_multi_port");
+
+	reg = rd32(hw, NGBE_PORTSTAT);
+	bus->lan_id = NGBE_PORTSTAT_ID(reg);
+	bus->func = bus->lan_id;
+}
+
+/**
+ *  ngbe_set_mac_type - Sets MAC type
+ *  @hw: pointer to the HW structure
+ *
+ *  This function sets the mac type of the adapter based on the
+ *  vendor ID and device ID stored in the hw structure.
+ **/
+s32 ngbe_set_mac_type(struct ngbe_hw *hw)
+{
+	s32 err = 0;
+
+	DEBUGFUNC("ngbe_set_mac_type");
+
+	if (hw->vendor_id != PCI_VENDOR_ID_WANGXUN) {
+		DEBUGOUT("Unsupported vendor id: %x", hw->vendor_id);
+		return NGBE_ERR_DEVICE_NOT_SUPPORTED;
+	}
+
+	switch (hw->sub_device_id) {
+	case NGBE_SUB_DEV_ID_EM_RTL_SGMII:
+	case NGBE_SUB_DEV_ID_EM_MVL_RGMII:
+		hw->phy.media_type = ngbe_media_type_copper;
+		hw->mac.type = ngbe_mac_em;
+		break;
+	case NGBE_SUB_DEV_ID_EM_MVL_SFP:
+	case NGBE_SUB_DEV_ID_EM_YT8521S_SFP:
+		hw->phy.media_type = ngbe_media_type_fiber;
+		hw->mac.type = ngbe_mac_em;
+		break;
+	case NGBE_SUB_DEV_ID_EM_VF:
+		hw->phy.media_type = ngbe_media_type_virtual;
+		hw->mac.type = ngbe_mac_em_vf;
+		break;
+	default:
+		err = NGBE_ERR_DEVICE_NOT_SUPPORTED;
+		hw->phy.media_type = ngbe_media_type_unknown;
+		hw->mac.type = ngbe_mac_unknown;
+		DEBUGOUT("Unsupported device id: %x", hw->device_id);
+		break;
+	}
+
+	DEBUGOUT("found mac: %d media: %d, returns: %d\n",
+		  hw->mac.type, hw->phy.media_type, err);
+	return err;
+}
+
 void ngbe_map_device_id(struct ngbe_hw *hw)
 {
 	u16 oem = hw->sub_system_id & NGBE_OEM_MASK;
@@ -58,3 +124,59 @@  void ngbe_map_device_id(struct ngbe_hw *hw)
 	}
 }
 
+/**
+ *  ngbe_init_ops_pf - Inits func ptrs and MAC type
+ *  @hw: pointer to hardware structure
+ *
+ *  Initialize the function pointers and assign the MAC type.
+ *  Does not touch the hardware.
+ **/
+s32 ngbe_init_ops_pf(struct ngbe_hw *hw)
+{
+	struct ngbe_bus_info *bus = &hw->bus;
+
+	DEBUGFUNC("ngbe_init_ops_pf");
+
+	/* BUS */
+	bus->set_lan_id = ngbe_set_lan_id_multi_port;
+
+	return 0;
+}
+
+/**
+ *  ngbe_init_shared_code - Initialize the shared code
+ *  @hw: pointer to hardware structure
+ *
+ *  This will assign function pointers and assign the MAC type and PHY code.
+ *  Does not touch the hardware. This function must be called prior to any
+ *  other function in the shared code. The ngbe_hw structure should be
+ *  memset to 0 prior to calling this function.  The following fields in
+ *  hw structure should be filled in prior to calling this function:
+ *  hw_addr, back, device_id, vendor_id, subsystem_device_id
+ **/
+s32 ngbe_init_shared_code(struct ngbe_hw *hw)
+{
+	s32 status = 0;
+
+	DEBUGFUNC("ngbe_init_shared_code");
+
+	/*
+	 * Set the mac type
+	 */
+	ngbe_set_mac_type(hw);
+
+	ngbe_init_ops_dummy(hw);
+	switch (hw->mac.type) {
+	case ngbe_mac_em:
+		ngbe_init_ops_pf(hw);
+		break;
+	default:
+		status = NGBE_ERR_DEVICE_NOT_SUPPORTED;
+		break;
+	}
+
+	hw->bus.set_lan_id(hw);
+
+	return status;
+}
+
diff --git a/drivers/net/ngbe/base/ngbe_hw.h b/drivers/net/ngbe/base/ngbe_hw.h
index b320d126ec..7d5de49248 100644
--- a/drivers/net/ngbe/base/ngbe_hw.h
+++ b/drivers/net/ngbe/base/ngbe_hw.h
@@ -8,6 +8,11 @@ 
 
 #include "ngbe_type.h"
 
+void ngbe_set_lan_id_multi_port(struct ngbe_hw *hw);
+
+s32 ngbe_init_shared_code(struct ngbe_hw *hw);
+s32 ngbe_set_mac_type(struct ngbe_hw *hw);
+s32 ngbe_init_ops_pf(struct ngbe_hw *hw);
 void ngbe_map_device_id(struct ngbe_hw *hw);
 
 #endif /* _NGBE_HW_H_ */
diff --git a/drivers/net/ngbe/base/ngbe_osdep.h b/drivers/net/ngbe/base/ngbe_osdep.h
index ef3d3d9180..94cc10315e 100644
--- a/drivers/net/ngbe/base/ngbe_osdep.h
+++ b/drivers/net/ngbe/base/ngbe_osdep.h
@@ -19,6 +19,8 @@ 
 #include <rte_config.h>
 #include <rte_io.h>
 
+#include "../ngbe_logs.h"
+
 #define RTE_LIBRTE_NGBE_TM        DCPV(1, 0)
 #define TMZ_PADDR(mz)  ((mz)->iova)
 #define TMZ_VADDR(mz)  ((mz)->addr)
diff --git a/drivers/net/ngbe/base/ngbe_type.h b/drivers/net/ngbe/base/ngbe_type.h
index 4243e04eef..15f1778d6a 100644
--- a/drivers/net/ngbe/base/ngbe_type.h
+++ b/drivers/net/ngbe/base/ngbe_type.h
@@ -12,8 +12,60 @@ 
 #include "ngbe_osdep.h"
 #include "ngbe_devids.h"
 
+enum ngbe_mac_type {
+	ngbe_mac_unknown = 0,
+	ngbe_mac_em,
+	ngbe_mac_em_vf,
+	ngbe_num_macs
+};
+
+enum ngbe_phy_type {
+	ngbe_phy_unknown = 0,
+	ngbe_phy_none,
+	ngbe_phy_rtl,
+	ngbe_phy_mvl,
+	ngbe_phy_mvl_sfi,
+	ngbe_phy_yt8521s,
+	ngbe_phy_yt8521s_sfi,
+	ngbe_phy_zte,
+	ngbe_phy_cu_mtd,
+};
+
+enum ngbe_media_type {
+	ngbe_media_type_unknown = 0,
+	ngbe_media_type_fiber,
+	ngbe_media_type_fiber_qsfp,
+	ngbe_media_type_copper,
+	ngbe_media_type_backplane,
+	ngbe_media_type_cx4,
+	ngbe_media_type_virtual
+};
+
+struct ngbe_hw;
+
+/* Bus parameters */
+struct ngbe_bus_info {
+	void (*set_lan_id)(struct ngbe_hw *hw);
+
+	u16 func;
+	u8 lan_id;
+};
+
+struct ngbe_mac_info {
+	enum ngbe_mac_type type;
+};
+
+struct ngbe_phy_info {
+	enum ngbe_media_type media_type;
+	enum ngbe_phy_type type;
+};
+
 struct ngbe_hw {
 	void IOMEM *hw_addr;
+	void *back;
+	struct ngbe_mac_info mac;
+	struct ngbe_phy_info phy;
+	struct ngbe_bus_info bus;
 	u16 device_id;
 	u16 vendor_id;
 	u16 sub_device_id;
@@ -27,5 +79,6 @@  struct ngbe_hw {
 };
 
 #include "ngbe_regs.h"
+#include "ngbe_dummy.h"
 
 #endif /* _NGBE_TYPE_H_ */
diff --git a/drivers/net/ngbe/ngbe_ethdev.c b/drivers/net/ngbe/ngbe_ethdev.c
index f24c3e173e..12b80ec695 100644
--- a/drivers/net/ngbe/ngbe_ethdev.c
+++ b/drivers/net/ngbe/ngbe_ethdev.c
@@ -37,6 +37,7 @@  eth_ngbe_dev_init(struct rte_eth_dev *eth_dev, void *init_params __rte_unused)
 	struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
 	struct ngbe_hw *hw = NGBE_DEV_HW(eth_dev);
 	const struct rte_memzone *mz;
+	int err;
 
 	PMD_INIT_FUNC_TRACE();
 
@@ -62,6 +63,13 @@  eth_ngbe_dev_init(struct rte_eth_dev *eth_dev, void *init_params __rte_unused)
 	hw->isb_dma = TMZ_PADDR(mz);
 	hw->isb_mem = TMZ_VADDR(mz);
 
+	/* Initialize the shared code (base driver) */
+	err = ngbe_init_shared_code(hw);
+	if (err != 0) {
+		PMD_INIT_LOG(ERR, "Shared code init failed: %d", err);
+		return -EIO;
+	}
+
 	return 0;
 }