[RFC PATCH net-next 02/10] net: hibmcge: Add read/write registers supported through the bar space

From: Jijie Shao
Date: Wed Jul 31 2024 - 05:49:04 EST


Add support for to read and write registers through the pic bar space.

Some driver parameters, such as mac_id, are determined by the
board form. Therefore, these parameters are initialized
from the register as device specifications.

the device specifications register are initialized and writed by bmc.
driver will read these registers when loading.

Signed-off-by: Jijie Shao <shaojijie@xxxxxxxxxx>
---
.../ethernet/hisilicon/hibmcge/hbg_common.h | 26 +++++++
.../net/ethernet/hisilicon/hibmcge/hbg_hw.c | 72 +++++++++++++++++++
.../net/ethernet/hisilicon/hibmcge/hbg_hw.h | 61 ++++++++++++++++
.../net/ethernet/hisilicon/hibmcge/hbg_main.c | 18 +++++
.../net/ethernet/hisilicon/hibmcge/hbg_reg.h | 19 +++++
5 files changed, 196 insertions(+)
create mode 100644 drivers/net/ethernet/hisilicon/hibmcge/hbg_hw.c
create mode 100644 drivers/net/ethernet/hisilicon/hibmcge/hbg_hw.h
create mode 100644 drivers/net/ethernet/hisilicon/hibmcge/hbg_reg.h

diff --git a/drivers/net/ethernet/hisilicon/hibmcge/hbg_common.h b/drivers/net/ethernet/hisilicon/hibmcge/hbg_common.h
index e08e28f25f3c..b56b5179f735 100644
--- a/drivers/net/ethernet/hisilicon/hibmcge/hbg_common.h
+++ b/drivers/net/ethernet/hisilicon/hibmcge/hbg_common.h
@@ -7,10 +7,36 @@
#include <linux/etherdevice.h>
#include <linux/pci.h>

+enum hbg_nic_state {
+ HBG_NIC_STATE_INITED = 0,
+ HBG_NIC_STATE_EVENT_HANDLING,
+};
+
+enum hbg_hw_event_type {
+ HBG_HW_EVENT_NONE = 0,
+ HBG_HW_EVENT_INIT, /* driver is loading */
+};
+
+struct hbg_dev_specs {
+ u32 mac_id;
+ struct sockaddr mac_addr;
+ u32 phy_addr;
+ u32 rx_fifo_num;
+ u32 tx_fifo_num;
+ u32 vlan_layers;
+ u32 max_mtu;
+ u32 min_mtu;
+
+ u32 max_frame_len;
+ u32 rx_buf_size;
+};
+
struct hbg_priv {
struct net_device *netdev;
struct pci_dev *pdev;
u8 __iomem *io_base;
+ struct hbg_dev_specs dev_specs;
+ unsigned long state;
};

#endif
diff --git a/drivers/net/ethernet/hisilicon/hibmcge/hbg_hw.c b/drivers/net/ethernet/hisilicon/hibmcge/hbg_hw.c
new file mode 100644
index 000000000000..ca1cb09c90ff
--- /dev/null
+++ b/drivers/net/ethernet/hisilicon/hibmcge/hbg_hw.c
@@ -0,0 +1,72 @@
+// SPDX-License-Identifier: GPL-2.0+
+// Copyright (c) 2024 Hisilicon Limited.
+
+#include <linux/minmax.h>
+#include <linux/netdevice.h>
+#include <linux/pci.h>
+#include "hbg_common.h"
+#include "hbg_hw.h"
+#include "hbg_reg.h"
+
+static bool hbg_hw_spec_is_valid(struct hbg_priv *priv)
+{
+ return hbg_reg_read(priv, HBG_REG_SPEC_VALID_ADDR) &&
+ !hbg_reg_read(priv, HBG_REG_EVENT_REQ_ADDR);
+}
+
+int hbg_hw_event_notify(struct hbg_priv *priv, enum hbg_hw_event_type event_type)
+{
+#define HBG_HW_EVENT_WAIT_TIMEOUT_MS (2 * 1000)
+#define HBG_HW_EVENT_WAIT_INTERVAL_MS 100
+
+ u32 timeout = 0;
+
+ if (test_and_set_bit(HBG_NIC_STATE_EVENT_HANDLING, &priv->state))
+ return -EBUSY;
+
+ /* notify */
+ hbg_reg_write(priv, HBG_REG_EVENT_REQ_ADDR, event_type);
+
+ do {
+ msleep(HBG_HW_EVENT_WAIT_INTERVAL_MS);
+ timeout += HBG_HW_EVENT_WAIT_INTERVAL_MS;
+
+ if (hbg_hw_spec_is_valid(priv))
+ break;
+ } while (timeout <= HBG_HW_EVENT_WAIT_TIMEOUT_MS);
+
+ clear_bit(HBG_NIC_STATE_EVENT_HANDLING, &priv->state);
+
+ if (!hbg_hw_spec_is_valid(priv)) {
+ dev_err(&priv->pdev->dev, "event %d wait timeout\n", event_type);
+ return -ETIMEDOUT;
+ }
+
+ return 0;
+}
+
+int hbg_hw_dev_specs_init(struct hbg_priv *priv)
+{
+ struct hbg_dev_specs *dev_specs = &priv->dev_specs;
+ u64 mac_addr;
+
+ if (!hbg_hw_spec_is_valid(priv)) {
+ dev_err(&priv->pdev->dev, "dev_specs not init\n");
+ return -EINVAL;
+ }
+
+ dev_specs->mac_id = hbg_reg_read(priv, HBG_REG_MAC_ID_ADDR);
+ dev_specs->phy_addr = hbg_reg_read(priv, HBG_REG_PHY_ID_ADDR);
+ dev_specs->max_mtu = hbg_reg_read(priv, HBG_REG_MAX_MTU_ADDR);
+ dev_specs->min_mtu = hbg_reg_read(priv, HBG_REG_MIN_MTU_ADDR);
+ dev_specs->vlan_layers = hbg_reg_read(priv, HBG_REG_VLAN_LAYERS_ADDR);
+ dev_specs->rx_fifo_num = hbg_reg_read(priv, HBG_REG_RX_FIFO_NUM_ADDR);
+ dev_specs->tx_fifo_num = hbg_reg_read(priv, HBG_REG_TX_FIFO_NUM_ADDR);
+ mac_addr = hbg_reg_read64(priv, HBG_REG_MAC_ADDR_ADDR);
+ u64_to_ether_addr(mac_addr, (u8 *)dev_specs->mac_addr.sa_data);
+
+ if (!is_valid_ether_addr((u8 *)dev_specs->mac_addr.sa_data))
+ return -EADDRNOTAVAIL;
+
+ return 0;
+}
diff --git a/drivers/net/ethernet/hisilicon/hibmcge/hbg_hw.h b/drivers/net/ethernet/hisilicon/hibmcge/hbg_hw.h
new file mode 100644
index 000000000000..61c6db948364
--- /dev/null
+++ b/drivers/net/ethernet/hisilicon/hibmcge/hbg_hw.h
@@ -0,0 +1,61 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/* Copyright (c) 2024 Hisilicon Limited. */
+
+#ifndef __HBG_HW_H
+#define __HBG_HW_H
+
+#include <linux/io-64-nonatomic-lo-hi.h>
+
+static inline u32 hbg_reg_read(struct hbg_priv *priv, u32 reg_addr)
+{
+ return readl(priv->io_base + reg_addr);
+}
+
+static inline void hbg_reg_write(struct hbg_priv *priv, u32 reg_addr, u32 value)
+{
+ writel(value, priv->io_base + reg_addr);
+}
+
+static inline u32 hbg_reg_read_field(struct hbg_priv *priv, u32 reg_addr,
+ u32 mask)
+{
+ return FIELD_GET(mask, hbg_reg_read(priv, reg_addr));
+}
+
+static inline void hbg_reg_write_field(struct hbg_priv *priv, u32 reg_addr,
+ u32 mask, u32 val)
+{
+ u32 reg_value = hbg_reg_read(priv, reg_addr);
+
+ reg_value &= ~mask;
+ reg_value |= FIELD_PREP(mask, val);
+ hbg_reg_write(priv, reg_addr, reg_value);
+}
+
+static inline u32 hbg_reg_read_bit(struct hbg_priv *priv, u32 reg_addr,
+ u32 mask)
+{
+ return hbg_reg_read_field(priv, reg_addr, mask);
+}
+
+static inline void hbg_reg_write_bit(struct hbg_priv *priv, u32 reg_addr,
+ u32 mask, u32 val)
+{
+ hbg_reg_write_field(priv, reg_addr, mask, val);
+}
+
+static inline u64 hbg_reg_read64(struct hbg_priv *priv, u32 reg_addr)
+{
+ return lo_hi_readq(priv->io_base + reg_addr);
+}
+
+static inline void hbg_reg_write64(struct hbg_priv *priv, u32 reg_addr,
+ u64 value)
+{
+ lo_hi_writeq(value, priv->io_base + reg_addr);
+}
+
+int hbg_hw_event_notify(struct hbg_priv *priv, enum hbg_hw_event_type event_type);
+int hbg_hw_dev_specs_init(struct hbg_priv *priv);
+
+#endif
diff --git a/drivers/net/ethernet/hisilicon/hibmcge/hbg_main.c b/drivers/net/ethernet/hisilicon/hibmcge/hbg_main.c
index 30cc19e71c54..df0fc6a1059b 100644
--- a/drivers/net/ethernet/hisilicon/hibmcge/hbg_main.c
+++ b/drivers/net/ethernet/hisilicon/hibmcge/hbg_main.c
@@ -5,8 +5,21 @@
#include <linux/netdevice.h>
#include <linux/pci.h>
#include "hbg_common.h"
+#include "hbg_hw.h"
#include "hbg_main.h"

+static int hbg_init(struct net_device *netdev)
+{
+ struct hbg_priv *priv = netdev_priv(netdev);
+ int ret;
+
+ ret = hbg_hw_event_notify(priv, HBG_HW_EVENT_INIT);
+ if (ret)
+ return ret;
+
+ return hbg_hw_dev_specs_init(priv);
+}
+
static int hbg_pci_init(struct pci_dev *pdev)
{
struct net_device *netdev = pci_get_drvdata(pdev);
@@ -56,11 +69,16 @@ static int hbg_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
if (ret)
return ret;

+ ret = hbg_init(netdev);
+ if (ret)
+ return ret;
+
ret = devm_register_netdev(&pdev->dev, netdev);
if (ret)
return dev_err_probe(&pdev->dev, ret,
"failed to register netdev\n");

+ set_bit(HBG_NIC_STATE_INITED, &priv->state);
return 0;
}

diff --git a/drivers/net/ethernet/hisilicon/hibmcge/hbg_reg.h b/drivers/net/ethernet/hisilicon/hibmcge/hbg_reg.h
new file mode 100644
index 000000000000..4c34516e0c89
--- /dev/null
+++ b/drivers/net/ethernet/hisilicon/hibmcge/hbg_reg.h
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/* Copyright (c) 2024 Hisilicon Limited. */
+
+#ifndef __HBG_REG_H
+#define __HBG_REG_H
+
+/* DEV SPEC */
+#define HBG_REG_SPEC_VALID_ADDR 0x0000
+#define HBG_REG_EVENT_REQ_ADDR 0x0004
+#define HBG_REG_MAC_ID_ADDR 0x0008
+#define HBG_REG_PHY_ID_ADDR 0x000C
+#define HBG_REG_MAC_ADDR_ADDR 0x0010
+#define HBG_REG_MAX_MTU_ADDR 0x0028
+#define HBG_REG_MIN_MTU_ADDR 0x002C
+#define HBG_REG_TX_FIFO_NUM_ADDR 0x0030
+#define HBG_REG_RX_FIFO_NUM_ADDR 0x0034
+#define HBG_REG_VLAN_LAYERS_ADDR 0x0038
+
+#endif
--
2.33.0