Skip to contentSkip to conversation

add hinic3 PMD driver

RFC In review Feifei Wang 4 participants 384 messages Apr 18, 2025 – Sep 26, 2025 Page 6 of 16

Subsystems (2)
lore
Share & tools
lore
#141 lore raw

Re: [V5 08/18] net/hinic3: add module about hardware operation

In reply to Feifei Wang
Quoted 386 lines · Feifei Wang

From: Yi Chen <[email protected]>

Add code and data structure for hardware operation, including

configuration, query, initialization and release.

---

 drivers/net/hinic3/base/hinic3_hw_cfg.c  | 240 ++++++++++
 drivers/net/hinic3/base/hinic3_hw_cfg.h  | 121 +++++
 drivers/net/hinic3/base/hinic3_hw_comm.c | 452 ++++++++++++++++++
 drivers/net/hinic3/base/hinic3_hw_comm.h | 366 +++++++++++++++
 drivers/net/hinic3/base/hinic3_hwdev.c   | 573 +++++++++++++++++++++++
 drivers/net/hinic3/base/hinic3_hwdev.h   | 177 +++++++
 6 files changed, 1929 insertions(+)
 create mode 100644 drivers/net/hinic3/base/hinic3_hw_cfg.c
 create mode 100644 drivers/net/hinic3/base/hinic3_hw_cfg.h
 create mode 100644 drivers/net/hinic3/base/hinic3_hw_comm.c
 create mode 100644 drivers/net/hinic3/base/hinic3_hw_comm.h
 create mode 100644 drivers/net/hinic3/base/hinic3_hwdev.c
 create mode 100644 drivers/net/hinic3/base/hinic3_hwdev.h
drivers/net/hinic3/base/hinic3_hw_cfg.c +0 -0

new file mode 100644

index 0000000000..ebe746a9ae

--- /dev/null

+++ b/drivers/net/hinic3/base/hinic3_hw_cfg.c

drivers/net/hinic3/base/hinic3_hw_cfg.c +165 -0
@@ -0,0 +1,240 @@
 
+/* SPDX-License-Identifier: BSD-3-Clause
 
+ * Copyright(c) 2025 Huawei Technologies Co., Ltd
 
+ */
 
+
 
+#include "hinic3_compat.h"
 
+#include "hinic3_mbox.h"
 
+#include "hinic3_mgmt.h"
 
+#include "hinic3_hw_cfg.h"
 
+#include "hinic3_hwdev.h"
 
+#include "hinic3_hwif.h"
 
+
 
+static void
 
+parse_pub_res_cap(struct service_cap *cap,
 
+		  struct hinic3_cfg_cmd_dev_cap *dev_cap, enum func_type type)
 
+{
 
+	cap->host_id = dev_cap->host_id;
 
+	cap->ep_id = dev_cap->ep_id;
 
+	cap->er_id = dev_cap->er_id;
 
+	cap->port_id = dev_cap->port_id;
 
+
 
+	cap->svc_type = dev_cap->svc_cap_en;
 
+	cap->chip_svc_type = cap->svc_type;
 
+
 
+	cap->cos_valid_bitmap = dev_cap->valid_cos_bitmap;
 
+	cap->flexq_en = dev_cap->flexq_en;
 
+
 
+	cap->host_total_function = dev_cap->host_total_func;
 
+	cap->max_vf = 0;
 
+	if (type == TYPE_PF || type == TYPE_PPF) {
 
+		cap->max_vf = dev_cap->max_vf;
 
+		cap->pf_num = dev_cap->host_pf_num;
 
+		cap->pf_id_start = dev_cap->pf_id_start;
 
+		cap->vf_num = dev_cap->host_vf_num;
 
+		cap->vf_id_start = dev_cap->vf_id_start;
 
+	}
 
+
 
+	PMD_DRV_LOG(INFO, "Get public resource capability: ");
 
+	PMD_DRV_LOG(INFO,
 
+		    "host_id: 0x%x, ep_id: 0x%x, er_id: 0x%x, port_id: 0x%x",
 
+		    cap->host_id, cap->ep_id, cap->er_id, cap->port_id);
 
+	PMD_DRV_LOG(INFO, "host_total_function: 0x%x, max_vf: 0x%x",
 
+		    cap->host_total_function, cap->max_vf);
 
+	PMD_DRV_LOG(INFO,
 
+		    "host_pf_num: 0x%x, pf_id_start: 0x%x, host_vf_num: 0x%x, "
 
+		    "vf_id_start: 0x%x",
 
+		    cap->pf_num, cap->pf_id_start, cap->vf_num,
 
+		    cap->vf_id_start);
 
+}
 
+
 
+static void
 
+parse_l2nic_res_cap(struct service_cap *cap,
 
+		    struct hinic3_cfg_cmd_dev_cap *dev_cap)
 
+{
 
+	struct nic_service_cap *nic_cap = &cap->nic_cap;
 
+
 
+	nic_cap->max_sqs = dev_cap->nic_max_sq_id + 1;
 
+	nic_cap->max_rqs = dev_cap->nic_max_rq_id + 1;
 
+
 
+	PMD_DRV_LOG(INFO,
 
+		    "L2nic resource capbility, "
 
+		    "max_sqs: 0x%x, max_rqs: 0x%x",
 
+		    nic_cap->max_sqs, nic_cap->max_rqs);
 
+}
 
+
 
+static void
 
+parse_dev_cap(struct hinic3_hwdev *dev, struct hinic3_cfg_cmd_dev_cap *dev_cap,
 
+	      enum func_type type)
 
+{
 
+	struct service_cap *cap = &dev->cfg_mgmt->svc_cap;
 
+
 
+	parse_pub_res_cap(cap, dev_cap, type);
 
+
 
+	if (IS_NIC_TYPE(dev))
 
+		parse_l2nic_res_cap(cap, dev_cap);
 
+}
 
+
 
+static int
 
+get_cap_from_fw(struct hinic3_hwdev *hwdev, enum func_type type)
 
+{
 
+	struct hinic3_cfg_cmd_dev_cap dev_cap;
 
+	u16 out_len = sizeof(dev_cap);
 
+	int err;
 
+
 
+	memset(&dev_cap, 0, sizeof(dev_cap));
 
+	dev_cap.func_id = hinic3_global_func_id(hwdev);
 
+	err = hinic3_msg_to_mgmt_sync(hwdev, HINIC3_MOD_CFGM,
 
+				      HINIC3_CFG_CMD_GET_DEV_CAP, &dev_cap,
 
+				      sizeof(dev_cap), &dev_cap, &out_len, 0);
 
+	if (err || dev_cap.status || !out_len) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Get capability from FW failed, "
 
+			    "err: %d, status: 0x%x, out size: 0x%x",
 
+			    err, dev_cap.status, out_len);
 
+		return -EFAULT;
 
+	}
 
+
 
+	parse_dev_cap(hwdev, &dev_cap, type);
 
+	return 0;
 
+}
 
+
 
+static int
 
+get_dev_cap(struct hinic3_hwdev *hwdev)
 
+{
 
+	enum func_type type = HINIC3_FUNC_TYPE(hwdev);
 
+
 
+	switch (type) {
 
+	case TYPE_PF:
 
+	case TYPE_PPF:
 
+	case TYPE_VF:
 
+		if (get_cap_from_fw(hwdev, type) != 0)
 
+			return -EFAULT;
 
+		break;
 
+	default:
 
+		PMD_DRV_LOG(ERR, "Unsupported PCIe function type: %d", type);
 
+		return -EINVAL;
 
+	}
 
+
 
+	return 0;
 
+}
 
+
 
+int
 
+cfg_mbx_vf_proc_msg(void *hwdev, __rte_unused void *pri_handle, u16 cmd,
 
+		    __rte_unused void *buf_in, __rte_unused u16 in_size,
 
+		    __rte_unused void *buf_out, __rte_unused u16 *out_size)
 
+{
 
+	struct hinic3_hwdev *dev = hwdev;
 
+
 
+	if (!dev)
 
+		return -EINVAL;
 
+
 
+	PMD_DRV_LOG(WARNING, "Unsupported cfg mbox vf event %d to process",
 
+		    cmd);
 
+
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_init_cfg_mgmt(void *dev)
 
+{
 
+	struct hinic3_hwdev *hwdev = (struct hinic3_hwdev *)dev;
 
+	struct cfg_mgmt_info *cfg_mgmt = NULL;
 
+
 
+	cfg_mgmt = rte_zmalloc("cfg_mgmt", sizeof(*cfg_mgmt),
 
+			       HINIC3_MEM_ALLOC_ALIGN_MIN);
 
+	if (!cfg_mgmt)
 
+		return -ENOMEM;
 
+
 
+	memset(cfg_mgmt, 0, sizeof(struct cfg_mgmt_info));
 
+	hwdev->cfg_mgmt = cfg_mgmt;
 
+	cfg_mgmt->hwdev = hwdev;
 
+
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_init_capability(void *dev)
 
+{
 
+	struct hinic3_hwdev *hwdev = (struct hinic3_hwdev *)dev;
 
+
 
+	return get_dev_cap(hwdev);
 
+}
 
+
 
+void
 
+hinic3_deinit_cfg_mgmt(void *dev)
 
+{

drivers/net/hinic3/base/hinic3_hw_cfg.c line 170

Quoted 10 lines
drivers/net/hinic3/base/hinic3_hw_cfg.c +5 -0
 
+	rte_free(((struct hinic3_hwdev *)dev)->cfg_mgmt);
 
+	((struct hinic3_hwdev *)dev)->cfg_mgmt = NULL;
 
+}
 
+
 
+#ifdef HINIC3_RELEASE

What the meaning of this macro?

Quoted 3,172 lines
drivers/net/hinic3/base/hinic3_hw_cfg.c +70 -0
+static bool
 
+hinic3_support_nic(void *hwdev, struct nic_service_cap *cap)
 
+{
 
+	struct hinic3_hwdev *dev = (struct hinic3_hwdev *)hwdev;
 
+
 
+	if (!hwdev)
 
+		return false;
 
+
 
+	if (!IS_NIC_TYPE(dev))
 
+		return false;
 
+
 
+	if (cap)
 
+		memcpy(cap, &dev->cfg_mgmt->svc_cap.nic_cap, sizeof(*cap));
 
+
 
+	return true;
 
+}
 
+
 
+static bool
 
+hinic3_func_for_mgmt(void *hwdev)
 
+{
 
+	struct hinic3_hwdev *dev = hwdev;
 
+
 
+	if (!hwdev)
 
+		return false;
 
+
 
+	if (dev->cfg_mgmt->svc_cap.chip_svc_type >= CFG_SVC_NIC_BIT0)
 
+		return false;
 
+	else
 
+		return true;
 
+}
 
+#endif
 
+
 
+u16
 
+hinic3_func_max_sqs(void *hwdev)
 
+{
 
+	struct hinic3_hwdev *dev = hwdev;
 
+
 
+	if (!dev) {
 
+		PMD_DRV_LOG(INFO, "Hwdev is NULL for getting max_sqs");
 
+		return 0;
 
+	}
 
+
 
+	return dev->cfg_mgmt->svc_cap.nic_cap.max_sqs;
 
+}
 
+
 
+u16
 
+hinic3_func_max_rqs(void *hwdev)
 
+{
 
+	struct hinic3_hwdev *dev = hwdev;
 
+
 
+	if (!dev) {
 
+		PMD_DRV_LOG(INFO, "Hwdev is NULL for getting max_rqs");
 
+		return 0;
 
+	}
 
+
 
+	return dev->cfg_mgmt->svc_cap.nic_cap.max_rqs;
 
+}
 
+
 
+u8
 
+hinic3_physical_port_id(void *hwdev)
 
+{
 
+	struct hinic3_hwdev *dev = hwdev;
 
+
 
+	if (!dev) {
 
+		PMD_DRV_LOG(INFO, "Hwdev is NULL for getting physical port id");
 
+		return 0;
 
+	}
 
+
 
+	return dev->cfg_mgmt->svc_cap.port_id;
 
+}
drivers/net/hinic3/base/hinic3_hw_cfg.h +0 -0

new file mode 100644

index 0000000000..8ded52faa9

--- /dev/null

+++ b/drivers/net/hinic3/base/hinic3_hw_cfg.h

drivers/net/hinic3/base/hinic3_hw_cfg.h +121 -0
@@ -0,0 +1,121 @@
 
+/* SPDX-License-Identifier: BSD-3-Clause
 
+ * Copyright(c) 2025 Huawei Technologies Co., Ltd
 
+ */
 
+
 
+#ifndef _HINIC3_HW_CFG_H_
 
+#define _HINIC3_HW_CFG_H_
 
+
 
+#define CFG_MAX_CMD_TIMEOUT 30000 /**< ms */
 
+
 
+#define K_UNIT BIT(10)
 
+#define M_UNIT BIT(20)
 
+#define G_UNIT BIT(30)
 
+
 
+/* Number of PFs and VFs. */
 
+#define HOST_PF_NUM	   4
 
+#define HOST_VF_NUM	   0
 
+#define HOST_OQID_MASK_VAL 2
 
+
 
+#define L2NIC_SQ_DEPTH (4 * K_UNIT)
 
+#define L2NIC_RQ_DEPTH (4 * K_UNIT)
 
+
 
+enum intr_type { INTR_TYPE_MSIX, INTR_TYPE_MSI, INTR_TYPE_INT, INTR_TYPE_NONE };
 
+
 
+/* Service type relates define. */
 
+enum cfg_svc_type_en { CFG_SVC_NIC_BIT0 = 1 };
 
+
 
+struct nic_service_cap {
 
+	u16 max_sqs;
 
+	u16 max_rqs;
 
+};
 
+
 
+/* Device capability. */
 
+struct service_cap {
 
+	enum cfg_svc_type_en svc_type;	    /**< User input service type. */
 
+	enum cfg_svc_type_en chip_svc_type; /**< HW supported service type. */
 
+
 
+	u8 host_id;
 
+	u8 ep_id;
 
+	u8 er_id;   /**< PF/VF's ER. */
 
+	u8 port_id; /**< PF/VF's physical port. */
 
+
 
+	u16 host_total_function;
 
+	u8 pf_num;
 
+	u8 pf_id_start;
 
+	u16 vf_num; /**< Max numbers of vf in current host. */
 
+	u16 vf_id_start;
 
+
 
+	u8 flexq_en;
 
+	u8 cos_valid_bitmap;
 
+	u16 max_vf; /**< Max VF number that PF supported. */
 
+
 
+	struct nic_service_cap nic_cap; /**< NIC capability. */
 
+};
 
+
 
+struct cfg_mgmt_info {
 
+	void *hwdev;
 
+	struct service_cap svc_cap;
 
+};
 
+
 
+enum hinic3_cfg_cmd {
 
+	HINIC3_CFG_CMD_GET_DEV_CAP = 0,
 
+};
 
+
 
+struct hinic3_cfg_cmd_dev_cap {
 
+	u8 status;
 
+	u8 version;
 
+	u8 rsvd0[6];
 
+
 
+	u16 func_id;
 
+	u16 rsvd1;
 
+
 
+	/* Public resource. */
 
+	u8 host_id;
 
+	u8 ep_id;
 
+	u8 er_id;
 
+	u8 port_id;
 
+
 
+	u16 host_total_func;
 
+	u8 host_pf_num;
 
+	u8 pf_id_start;
 
+	u16 host_vf_num;
 
+	u16 vf_id_start;
 
+	u32 rsvd_host;
 
+
 
+	u16 svc_cap_en;
 
+	u16 max_vf;
 
+	u8 flexq_en;
 
+	u8 valid_cos_bitmap;
 
+	/* Reserved for func_valid_cos_bitmap. */
 
+	u16 rsvd_cos;
 
+
 
+	u32 rsvd[11];
 
+
 
+	/* l2nic */
 
+	u16 nic_max_sq_id;
 
+	u16 nic_max_rq_id;
 
+	u32 rsvd_nic[3];
 
+
 
+	u32 rsvd_glb[60];
 
+};
 
+
 
+#define IS_NIC_TYPE(dev) \
 
+	(((u32)(dev)->cfg_mgmt->svc_cap.chip_svc_type) & CFG_SVC_NIC_BIT0)
 
+
 
+int hinic3_init_cfg_mgmt(void *dev);
 
+int hinic3_init_capability(void *dev);
 
+void hinic3_deinit_cfg_mgmt(void *dev);
 
+
 
+u16 hinic3_func_max_sqs(void *hwdev);
 
+u16 hinic3_func_max_rqs(void *hwdev);
 
+
 
+u8 hinic3_physical_port_id(void *hwdev);
 
+
 
+int cfg_mbx_ppf_proc_msg(void *hwdev, void *pri_handle, u16 pf_id, u16 vf_id,
 
+			 u16 cmd, void *buf_in, u16 in_size, void *buf_out,
 
+			 u16 *out_size);
 
+
 
+int cfg_mbx_vf_proc_msg(void *hwdev, void *pri_handle, u16 cmd, void *buf_in,
 
+			u16 in_size, void *buf_out, u16 *out_size);
 
+
 
+#endif /* _HINIC3_HW_CFG_H_ */
drivers/net/hinic3/base/hinic3_hw_comm.c +0 -0

new file mode 100644

index 0000000000..15d298a768

--- /dev/null

+++ b/drivers/net/hinic3/base/hinic3_hw_comm.c

drivers/net/hinic3/base/hinic3_hw_comm.c +452 -0
@@ -0,0 +1,452 @@
 
+/* SPDX-License-Identifier: BSD-3-Clause
 
+ * Copyright(c) 2025 Huawei Technologies Co., Ltd
 
+ */
 
+
 
+#include <rte_bus_pci.h>
 
+#include <rte_hash.h>
 
+#include <rte_jhash.h>
 
+
 
+#include "hinic3_compat.h"
 
+#include "hinic3_cmd.h"
 
+#include "hinic3_cmdq.h"
 
+#include "hinic3_hw_comm.h"
 
+#include "hinic3_hwdev.h"
 
+#include "hinic3_hwif.h"
 
+#include "hinic3_mgmt.h"
 
+#include "hinic3_wq.h"
 
+
 
+/* Buffer sizes in hinic3_convert_rx_buf_size must be in ascending order. */
 
+const u32 hinic3_hw_rx_buf_size[] = {
 
+	HINIC3_RX_BUF_SIZE_32B,
 
+	HINIC3_RX_BUF_SIZE_64B,
 
+	HINIC3_RX_BUF_SIZE_96B,
 
+	HINIC3_RX_BUF_SIZE_128B,
 
+	HINIC3_RX_BUF_SIZE_192B,
 
+	HINIC3_RX_BUF_SIZE_256B,
 
+	HINIC3_RX_BUF_SIZE_384B,
 
+	HINIC3_RX_BUF_SIZE_512B,
 
+	HINIC3_RX_BUF_SIZE_768B,
 
+	HINIC3_RX_BUF_SIZE_1K,
 
+	HINIC3_RX_BUF_SIZE_1_5K,
 
+	HINIC3_RX_BUF_SIZE_2K,
 
+	HINIC3_RX_BUF_SIZE_3K,
 
+	HINIC3_RX_BUF_SIZE_4K,
 
+	HINIC3_RX_BUF_SIZE_8K,
 
+	HINIC3_RX_BUF_SIZE_16K,
 
+};
 
+
 
+int
 
+hinic3_get_interrupt_cfg(void *dev, struct interrupt_info *info)
 
+{
 
+	struct hinic3_hwdev *hwdev = dev;
 
+	struct hinic3_cmd_msix_config msix_cfg;
 
+	u16 out_size = sizeof(msix_cfg);
 
+	int err;
 
+
 
+	if (!hwdev || !info)
 
+		return -EINVAL;
 
+
 
+	memset(&msix_cfg, 0, sizeof(msix_cfg));
 
+	msix_cfg.func_id = hinic3_global_func_id(hwdev);
 
+	msix_cfg.msix_index = info->msix_index;
 
+	msix_cfg.opcode = HINIC3_MGMT_CMD_OP_GET;
 
+
 
+	err = hinic3_msg_to_mgmt_sync(hwdev,
 
+		HINIC3_MOD_COMM, HINIC3_MGMT_CMD_CFG_MSIX_CTRL_REG,
 
+		&msix_cfg, sizeof(msix_cfg), &msix_cfg, &out_size, 0);
 
+	if (err || !out_size || msix_cfg.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Get interrupt config failed, "
 
+			    "err: %d, status: 0x%x, out size: 0x%x",
 
+			    err, msix_cfg.status, out_size);
 
+		return -EINVAL;
 
+	}
 
+
 
+	info->lli_credit_limit = msix_cfg.lli_credit_cnt;
 
+	info->lli_timer_cfg = msix_cfg.lli_tmier_cnt;
 
+	info->pending_limt = msix_cfg.pending_cnt;
 
+	info->coalesce_timer_cfg = msix_cfg.coalesce_timer_cnt;
 
+	info->resend_timer_cfg = msix_cfg.resend_timer_cnt;
 
+
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_set_interrupt_cfg(void *dev, struct interrupt_info info)
 
+{
 
+	struct hinic3_hwdev *hwdev = dev;
 
+	struct hinic3_cmd_msix_config msix_cfg;
 
+	struct interrupt_info temp_info;
 
+	u16 out_size = sizeof(msix_cfg);
 
+	int err;
 
+
 
+	if (!hwdev)
 
+		return -EINVAL;
 
+
 
+	temp_info.msix_index = info.msix_index;
 
+	err = hinic3_get_interrupt_cfg(hwdev, &temp_info);
 
+	if (err)
 
+		return -EIO;
 
+
 
+	memset(&msix_cfg, 0, sizeof(msix_cfg));
 
+	msix_cfg.func_id = hinic3_global_func_id(hwdev);
 
+	msix_cfg.msix_index = (u16)info.msix_index;
 
+	msix_cfg.opcode = HINIC3_MGMT_CMD_OP_SET;
 
+
 
+	msix_cfg.lli_credit_cnt = temp_info.lli_credit_limit;
 
+	msix_cfg.lli_tmier_cnt = temp_info.lli_timer_cfg;
 
+	msix_cfg.pending_cnt = temp_info.pending_limt;
 
+	msix_cfg.coalesce_timer_cnt = temp_info.coalesce_timer_cfg;
 
+	msix_cfg.resend_timer_cnt = temp_info.resend_timer_cfg;
 
+
 
+	if (info.lli_set) {
 
+		msix_cfg.lli_credit_cnt = info.lli_credit_limit;
 
+		msix_cfg.lli_tmier_cnt = info.lli_timer_cfg;
 
+	}
 
+
 
+	if (info.interrupt_coalesce_set) {
 
+		msix_cfg.pending_cnt = info.pending_limt;
 
+		msix_cfg.coalesce_timer_cnt = info.coalesce_timer_cfg;
 
+		msix_cfg.resend_timer_cnt = info.resend_timer_cfg;
 
+	}
 
+
 
+	err = hinic3_msg_to_mgmt_sync(hwdev,
 
+		HINIC3_MOD_COMM, HINIC3_MGMT_CMD_CFG_MSIX_CTRL_REG,
 
+		&msix_cfg, sizeof(msix_cfg), &msix_cfg, &out_size, 0);
 
+	if (err || !out_size || msix_cfg.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Set interrupt config failed, "
 
+			    "err: %d, status: 0x%x, out size: 0x%x",
 
+			    err, msix_cfg.status, out_size);
 
+		return -EIO;
 
+	}
 
+
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_set_wq_page_size(void *hwdev, u16 func_idx, u32 page_size)
 
+{
 
+	struct hinic3_cmd_wq_page_size page_size_info;
 
+	u16 out_size = sizeof(page_size_info);
 
+	int err;
 
+
 
+	memset(&page_size_info, 0, sizeof(page_size_info));
 
+	page_size_info.func_idx = func_idx;
 
+	page_size_info.page_size = HINIC3_PAGE_SIZE_HW(page_size);
 
+	page_size_info.opcode = HINIC3_MGMT_CMD_OP_SET;
 
+
 
+	err = hinic3_msg_to_mgmt_sync(hwdev, HINIC3_MOD_COMM,
 
+				      HINIC3_MGMT_CMD_CFG_PAGESIZE,
 
+				      &page_size_info, sizeof(page_size_info),
 
+				      &page_size_info, &out_size, 0);
 
+	if (err || !out_size || page_size_info.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Set wq page size failed, "
 
+			    "err: %d, status: 0x%x, out_size: 0x%0x",
 
+			    err, page_size_info.status, out_size);
 
+		return -EFAULT;
 
+	}
 
+
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_func_reset(void *hwdev, u64 reset_flag)
 
+{
 
+	struct hinic3_reset func_reset;
 
+	struct hinic3_hwif *hwif = ((struct hinic3_hwdev *)hwdev)->hwif;
 
+	u16 out_size = sizeof(func_reset);
 
+	int err = 0;
 
+
 
+	PMD_DRV_LOG(INFO, "Function is reset");
 
+
 
+	memset(&func_reset, 0, sizeof(func_reset));
 
+	func_reset.func_id = HINIC3_HWIF_GLOBAL_IDX(hwif);
 
+	func_reset.reset_flag = reset_flag;
 
+	err = hinic3_msg_to_mgmt_sync(hwdev,
 
+		HINIC3_MOD_COMM, HINIC3_MGMT_CMD_FUNC_RESET, &func_reset,
 
+		sizeof(func_reset), &func_reset, &out_size, 0);
 
+	if (err || !out_size || func_reset.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Reset func resources failed, "
 
+			    "err: %d, status: 0x%x, out_size: 0x%x",
 
+			    err, func_reset.status, out_size);
 
+		return -EIO;
 
+	}
 
+
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_set_func_svc_used_state(void *hwdev, u16 svc_type, u8 state)
 
+{
 
+	struct comm_cmd_func_svc_used_state used_state;
 
+	u16 out_size = sizeof(used_state);
 
+	int err;
 
+
 
+	if (!hwdev)
 
+		return -EINVAL;
 
+
 
+	memset(&used_state, 0, sizeof(used_state));
 
+	used_state.func_id = hinic3_global_func_id(hwdev);
 
+	used_state.svc_type = svc_type;
 
+	used_state.used_state = state;
 
+
 
+	err = hinic3_msg_to_mgmt_sync(hwdev,
 
+		HINIC3_MOD_COMM, HINIC3_MGMT_CMD_SET_FUNC_SVC_USED_STATE,
 
+		&used_state, sizeof(used_state), &used_state, &out_size, 0);
 
+	if (err || !out_size || used_state.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Failed to set func service used state, "
 
+			    "err: %d, status: 0x%x, out size: 0x%x",
 
+			    err, used_state.status, out_size);
 
+		return -EIO;
 
+	}
 
+
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_convert_rx_buf_size(u32 rx_buf_sz, u32 *match_sz)
 
+{
 
+	u32 i, num_hw_types, best_match_sz;
 
+
 
+	if (unlikely(!match_sz || rx_buf_sz < HINIC3_RX_BUF_SIZE_32B))
 
+		return -EINVAL;
 
+
 
+	if (rx_buf_sz >= HINIC3_RX_BUF_SIZE_16K) {
 
+		best_match_sz = HINIC3_RX_BUF_SIZE_16K;
 
+		goto size_matched;
 
+	}
 
+
 
+	if (rx_buf_sz >= HINIC3_RX_BUF_SIZE_4K) {
 
+		best_match_sz = ((rx_buf_sz >> RX_BUF_SIZE_1K_LEN)
 
+				 << RX_BUF_SIZE_1K_LEN);
 
+		goto size_matched;
 
+	}
 
+
 
+	num_hw_types = sizeof(hinic3_hw_rx_buf_size) /
 
+		       sizeof(hinic3_hw_rx_buf_size[0]);
 
+	best_match_sz = hinic3_hw_rx_buf_size[0];
 
+	for (i = 0; i < num_hw_types; i++) {
 
+		if (rx_buf_sz == hinic3_hw_rx_buf_size[i]) {
 
+			best_match_sz = hinic3_hw_rx_buf_size[i];
 
+			break;
 
+		} else if (rx_buf_sz < hinic3_hw_rx_buf_size[i]) {
 
+			break;
 
+		}
 
+		best_match_sz = hinic3_hw_rx_buf_size[i];
 
+	}
 
+
 
+size_matched:
 
+	*match_sz = best_match_sz;
 
+
 
+	return 0;
 
+}
 
+
 
+static u16
 
+get_hw_rx_buf_size(u32 rx_buf_sz)
 
+{
 
+	u16 num_hw_types = sizeof(hinic3_hw_rx_buf_size) /
 
+			   sizeof(hinic3_hw_rx_buf_size[0]);
 
+	u16 i;
 
+
 
+	for (i = 0; i < num_hw_types; i++) {
 
+		if (hinic3_hw_rx_buf_size[i] == rx_buf_sz)
 
+			return i;
 
+	}
 
+
 
+	PMD_DRV_LOG(WARNING, "Chip can't support rx buf size of %d", rx_buf_sz);
 
+
 
+	return DEFAULT_RX_BUF_SIZE; /**< Default 2K. */
 
+}
 
+
 
+int
 
+hinic3_set_root_ctxt(void *hwdev, u32 rq_depth, u32 sq_depth, u16 rx_buf_sz)
 
+{
 
+	struct hinic3_cmd_root_ctxt root_ctxt;
 
+	u16 out_size = sizeof(root_ctxt);
 
+	int err;
 
+
 
+	if (!hwdev)
 
+		return -EINVAL;
 
+
 
+	memset(&root_ctxt, 0, sizeof(root_ctxt));
 
+	root_ctxt.func_idx = hinic3_global_func_id(hwdev);
 
+	root_ctxt.set_cmdq_depth = 0;
 
+	root_ctxt.cmdq_depth = 0;
 
+	root_ctxt.lro_en = 1;
 
+	root_ctxt.rq_depth = (u16)ilog2(rq_depth);
 
+	root_ctxt.rx_buf_sz = get_hw_rx_buf_size(rx_buf_sz);
 
+	root_ctxt.sq_depth = (u16)ilog2(sq_depth);
 
+
 
+	err = hinic3_msg_to_mgmt_sync(hwdev,
 
+		HINIC3_MOD_COMM, HINIC3_MGMT_CMD_SET_VAT, &root_ctxt,
 
+		sizeof(root_ctxt), &root_ctxt, &out_size, 0);
 
+	if (err || !out_size || root_ctxt.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Set root context failed, "
 
+			    "err: %d, status: 0x%x, out_size: 0x%x",
 
+			    err, root_ctxt.status, out_size);
 
+		return -EFAULT;
 
+	}
 
+
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_clean_root_ctxt(void *hwdev)
 
+{
 
+	struct hinic3_cmd_root_ctxt root_ctxt;
 
+	u16 out_size = sizeof(root_ctxt);
 
+	int err;
 
+
 
+	if (!hwdev)
 
+		return -EINVAL;
 
+
 
+	memset(&root_ctxt, 0, sizeof(root_ctxt));
 
+	root_ctxt.func_idx = hinic3_global_func_id(hwdev);
 
+
 
+	err = hinic3_msg_to_mgmt_sync(hwdev,
 
+		HINIC3_MOD_COMM, HINIC3_MGMT_CMD_SET_VAT, &root_ctxt,
 
+		sizeof(root_ctxt), &root_ctxt, &out_size, 0);
 
+	if (err || !out_size || root_ctxt.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Clean root context failed, "
 
+			    "err: %d, status: 0x%x, out_size: 0x%x",
 
+			    err, root_ctxt.status, out_size);
 
+		return -EFAULT;
 
+	}
 
+
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_set_cmdq_depth(void *hwdev, u16 cmdq_depth)
 
+{
 
+	struct hinic3_cmd_root_ctxt root_ctxt;
 
+	u16 out_size = sizeof(root_ctxt);
 
+	int err;
 
+
 
+	memset(&root_ctxt, 0, sizeof(root_ctxt));
 
+	root_ctxt.func_idx = hinic3_global_func_id(hwdev);
 
+	root_ctxt.set_cmdq_depth = 1;
 
+	root_ctxt.cmdq_depth = (u8)ilog2(cmdq_depth);
 
+
 
+	err = hinic3_msg_to_mgmt_sync(hwdev,
 
+		HINIC3_MOD_COMM, HINIC3_MGMT_CMD_SET_VAT, &root_ctxt,
 
+		sizeof(root_ctxt), &root_ctxt, &out_size, 0);
 
+	if (err || !out_size || root_ctxt.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Set cmdq depth failed, "
 
+			    "err: %d, status: 0x%x, out_size: 0x%x",
 
+			    err, root_ctxt.status, out_size);
 
+		return -EFAULT;
 
+	}
 
+
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_get_mgmt_version(void *hwdev, char *mgmt_ver, int max_mgmt_len)
 
+{
 
+	struct hinic3_cmd_get_fw_version fw_ver;
 
+	u16 out_size = sizeof(fw_ver);
 
+	int err;
 
+
 
+	if (!hwdev || !mgmt_ver)
 
+		return -EINVAL;
 
+
 
+	memset(&fw_ver, 0, sizeof(fw_ver));
 
+	fw_ver.fw_type = HINIC3_FW_VER_TYPE_MPU;
 
+
 
+	err = hinic3_msg_to_mgmt_sync(hwdev, HINIC3_MOD_COMM,
 
+				      HINIC3_MGMT_CMD_GET_FW_VERSION, &fw_ver,
 
+				      sizeof(fw_ver), &fw_ver, &out_size, 0);
 
+	if (MSG_TO_MGMT_SYNC_RETURN_ERR(err, out_size, fw_ver.status)) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Get mgmt version failed, "
 
+			    "err: %d, status: 0x%x, out size: 0x%x",
 
+			    err, fw_ver.status, out_size);
 
+		return -EIO;
 
+	}
 
+
 
+	snprintf(mgmt_ver, max_mgmt_len, "%s", fw_ver.ver);
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_get_board_info(void *hwdev, struct hinic3_board_info *info)
 
+{
 
+	struct hinic3_cmd_board_info board_info;
 
+	u16 out_size = sizeof(board_info);
 
+	int err;
 
+
 
+	if (!hwdev || !info)
 
+		return -EINVAL;
 
+
 
+	memset(&board_info, 0, sizeof(board_info));
 
+	err = hinic3_msg_to_mgmt_sync(hwdev,
 
+		HINIC3_MOD_COMM, HINIC3_MGMT_CMD_GET_BOARD_INFO,
 
+		&board_info, sizeof(board_info), &board_info, &out_size, 0);
 
+	if (err || board_info.status || !out_size) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Get board info failed, "
 
+			    "err: %d, status: 0x%x, out size: 0x%x",
 
+			    err, board_info.status, out_size);
 
+		return -EFAULT;
 
+	}
 
+
 
+	memcpy(info, &board_info.info, sizeof(*info));
 
+
 
+	return 0;
 
+}
 
+
 
+static int
 
+hinic3_comm_features_nego(void *hwdev, u8 opcode, u64 *s_feature, u16 size)
 
+{
 
+	struct comm_cmd_feature_nego feature_nego;
 
+	u16 out_size = sizeof(feature_nego);
 
+	int err;
 
+
 
+	if (!hwdev || !s_feature || size > COMM_MAX_FEATURE_QWORD)
 
+		return -EINVAL;
 
+
 
+	memset(&feature_nego, 0, sizeof(feature_nego));
 
+	feature_nego.func_id = hinic3_global_func_id(hwdev);
 
+	feature_nego.opcode = opcode;
 
+	if (opcode == MGMT_MSG_CMD_OP_SET)
 
+		memcpy(feature_nego.s_feature, s_feature, (size * sizeof(u64)));
 
+
 
+	err = hinic3_msg_to_mgmt_sync(hwdev, HINIC3_MOD_COMM,
 
+				      HINIC3_MGMT_CMD_FEATURE_NEGO,
 
+				      &feature_nego, sizeof(feature_nego),
 
+				      &feature_nego, &out_size, 0);
 
+	if (err || !out_size || feature_nego.head.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Failed to negotiate feature, "
 
+			    "err: %d, status: 0x%x, out size: 0x%x",
 
+			    err, feature_nego.head.status, out_size);
 
+		return -EINVAL;
 
+	}
 
+
 
+	if (opcode == MGMT_MSG_CMD_OP_GET)
 
+		memcpy(s_feature, feature_nego.s_feature, (size * sizeof(u64)));
 
+
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_get_comm_features(void *hwdev, u64 *s_feature, u16 size)
 
+{
 
+	return hinic3_comm_features_nego(hwdev, MGMT_MSG_CMD_OP_GET, s_feature,
 
+					 size);
 
+}
 
+
 
+int
 
+hinic3_set_comm_features(void *hwdev, u64 *s_feature, u16 size)
 
+{
 
+	return hinic3_comm_features_nego(hwdev, MGMT_MSG_CMD_OP_SET, s_feature,
 
+					 size);
 
+}
drivers/net/hinic3/base/hinic3_hw_comm.h +0 -0

new file mode 100644

index 0000000000..c31952b3e6

--- /dev/null

+++ b/drivers/net/hinic3/base/hinic3_hw_comm.h

drivers/net/hinic3/base/hinic3_hw_comm.h +366 -0
@@ -0,0 +1,366 @@
 
+/* SPDX-License-Identifier: BSD-3-Clause
 
+ * Copyright(c) 2025 Huawei Technologies Co., Ltd
 
+ */
 
+
 
+#ifndef _HINIC3_HW_COMM_H_
 
+#define _HINIC3_HW_COMM_H_
 
+
 
+#include "hinic3_hwdev.h"
 
+#include "hinic3_mgmt.h"
 
+#define HINIC3_MGMT_CMD_OP_GET 0
 
+#define HINIC3_MGMT_CMD_OP_SET 1
 
+
 
+#define HINIC3_MSIX_CNT_LLI_TIMER_SHIFT	    0
 
+#define HINIC3_MSIX_CNT_LLI_CREDIT_SHIFT    8
 
+#define HINIC3_MSIX_CNT_COALESCE_TIMER_SHIFT 8
 
+#define HINIC3_MSIX_CNT_PENDING_SHIFT	    8
 
+#define HINIC3_MSIX_CNT_RESEND_TIMER_SHIFT  29
 
+
 
+#define HINIC3_MSIX_CNT_LLI_TIMER_MASK	   0xFFU
 
+#define HINIC3_MSIX_CNT_LLI_CREDIT_MASK	   0xFFU
 
+#define HINIC3_MSIX_CNT_COALESCE_TIMER_MASK 0xFFU
 
+#define HINIC3_MSIX_CNT_PENDING_MASK	   0x1FU
 
+#define HINIC3_MSIX_CNT_RESEND_TIMER_MASK  0x7U
 
+
 
+#define HINIC3_MSIX_CNT_SET(val, member)           \
 
+	(((val) & HINIC3_MSIX_CNT_##member##_MASK) \
 
+	 << HINIC3_MSIX_CNT_##member##_SHIFT)
 
+
 
+#define MSG_TO_MGMT_SYNC_RETURN_ERR(err, out_size, status) \
 
+	((err) || (status) || !(out_size))
 
+
 
+#define DEFAULT_RX_BUF_SIZE ((u16)0xB)
 
+#define RX_BUF_SIZE_1K_LEN  ((u16)0xA)
 
+
 
+enum hinic3_rx_buf_size {
 
+	HINIC3_RX_BUF_SIZE_32B = 0x20,
 
+	HINIC3_RX_BUF_SIZE_64B = 0x40,
 
+	HINIC3_RX_BUF_SIZE_96B = 0x60,
 
+	HINIC3_RX_BUF_SIZE_128B = 0x80,
 
+	HINIC3_RX_BUF_SIZE_192B = 0xC0,
 
+	HINIC3_RX_BUF_SIZE_256B = 0x100,
 
+	HINIC3_RX_BUF_SIZE_384B = 0x180,
 
+	HINIC3_RX_BUF_SIZE_512B = 0x200,
 
+	HINIC3_RX_BUF_SIZE_768B = 0x300,
 
+	HINIC3_RX_BUF_SIZE_1K = 0x400,
 
+	HINIC3_RX_BUF_SIZE_1_5K = 0x600,
 
+	HINIC3_RX_BUF_SIZE_2K = 0x800,
 
+	HINIC3_RX_BUF_SIZE_3K = 0xC00,
 
+	HINIC3_RX_BUF_SIZE_4K = 0x1000,
 
+	HINIC3_RX_BUF_SIZE_8K = 0x2000,
 
+	HINIC3_RX_BUF_SIZE_16K = 0x4000,
 
+};
 
+
 
+struct hinic3_cmd_msix_config {
 
+	u8 status;
 
+	u8 version;
 
+	u8 rsvd0[6];
 
+
 
+	u16 func_id;
 
+	u8 opcode;
 
+	u8 rsvd1;
 
+	u16 msix_index;
 
+	u8 pending_cnt;
 
+	u8 coalesce_timer_cnt;
 
+	u8 resend_timer_cnt;
 
+	u8 lli_tmier_cnt;
 
+	u8 lli_credit_cnt;
 
+	u8 rsvd2[5];
 
+};
 
+
 
+struct hinic3_dma_attr_table {
 
+	struct mgmt_msg_head head;
 
+
 
+	u16 func_id;
 
+	u8 entry_idx;
 
+	u8 st;
 
+	u8 at;
 
+	u8 ph;
 
+	u8 no_snooping;
 
+	u8 tph_en;
 
+	u32 resv1;
 
+};
 
+
 
+#define HINIC3_PAGE_SIZE_HW(pg_size) ((u8)ilog2((u32)((pg_size) >> 12)))
 
+
 
+struct hinic3_cmd_wq_page_size {
 
+	u8 status;
 
+	u8 version;
 
+	u8 rsvd0[6];
 
+
 
+	u16 func_idx;
 
+	u8 opcode;
 
+	/**
 
+	 * Real size is 4KB * 2^page_size, range(0~20) must be checked by
 
+	 * driver.
 
+	 */
 
+	u8 page_size;
 
+
 
+	u32 rsvd1;
 
+};
 
+
 
+struct hinic3_reset {
 
+	u8 status;
 
+	u8 version;
 
+	u8 rsvd0[6];
 
+
 
+	u16 func_id;
 
+	u16 rsvd1[3];
 
+	u64 reset_flag;
 
+};
 
+
 
+struct comm_cmd_func_svc_used_state {
 
+	u8 status;
 
+	u8 version;
 
+	u8 rsvd0[6];
 
+
 
+	u16 func_id;
 
+	u16 svc_type;
 
+	u8 used_state;
 
+	u8 rsvd[35];
 
+};
 
+
 
+struct hinic3_cmd_root_ctxt {
 
+	u8 status;
 
+	u8 version;
 
+	u8 rsvd0[6];
 
+
 
+	u16 func_idx;
 
+	u8 set_cmdq_depth;
 
+	u8 cmdq_depth;
 
+	u16 rx_buf_sz;
 
+	u8 lro_en;
 
+	u8 rsvd1;
 
+	u16 sq_depth;
 
+	u16 rq_depth;
 
+	u64 rsvd2;
 
+};
 
+
 
+enum hinic3_fw_ver_type {
 
+	HINIC3_FW_VER_TYPE_BOOT,
 
+	HINIC3_FW_VER_TYPE_MPU,
 
+	HINIC3_FW_VER_TYPE_NPU,
 
+	HINIC3_FW_VER_TYPE_SMU,
 
+	HINIC3_FW_VER_TYPE_CFG,
 
+};
 
+
 
+#define MGMT_MSG_CMD_OP_SET 1
 
+#define MGMT_MSG_CMD_OP_GET 0
 
+
 
+#define COMM_MAX_FEATURE_QWORD 4
 
+struct comm_cmd_feature_nego {
 
+	struct mgmt_msg_head head;
 
+
 
+	u16 func_id;
 
+	u8 opcode; /**< 1: set, 0: get. */
 
+	u8 rsvd;
 
+	u64 s_feature[COMM_MAX_FEATURE_QWORD];
 
+};
 
+
 
+#define HINIC3_FW_VERSION_LEN	    16
 
+#define HINIC3_FW_COMPILE_TIME_LEN  20
 
+#define HINIC3_MGMT_VERSION_MAX_LEN 32
 
+struct hinic3_cmd_get_fw_version {
 
+	u8 status;
 
+	u8 version;
 
+	u8 rsvd0[6];
 
+
 
+	u16 fw_type;
 
+	u16 rsvd1;
 
+	u8 ver[HINIC3_FW_VERSION_LEN];
 
+	u8 time[HINIC3_FW_COMPILE_TIME_LEN];
 
+};
 
+
 
+struct hinic3_cmd_clear_doorbell {
 
+	u8 status;
 
+	u8 version;
 
+	u8 rsvd0[6];
 
+
 
+	u16 func_idx;
 
+	u16 rsvd1[3];
 
+};
 
+
 
+struct hinic3_cmd_clear_resource {
 
+	u8 status;
 
+	u8 version;
 
+	u8 rsvd0[6];
 
+
 
+	u16 func_idx;
 
+	u16 rsvd1[3];
 
+};
 
+
 
+struct hinic3_cmd_board_info {
 
+	u8 status;
 
+	u8 version;
 
+	u8 rsvd0[6];
 
+
 
+	struct hinic3_board_info info;
 
+
 
+	u32 rsvd1[23];
 
+};
 
+
 
+struct interrupt_info {
 
+	u32 lli_set;
 
+	u32 interrupt_coalesce_set;
 
+	u16 msix_index;
 
+	u8 lli_credit_limit;
 
+	u8 lli_timer_cfg;
 
+	u8 pending_limt;
 
+	u8 coalesce_timer_cfg;
 
+	u8 resend_timer_cfg;
 
+};
 
+
 
+enum cfg_msix_operation {
 
+	CFG_MSIX_OPERATION_FREE = 0,
 
+	CFG_MSIX_OPERATION_ALLOC = 1,
 
+};
 
+
 
+struct comm_cmd_cfg_msix_num {
 
+	u8 status;
 
+	u8 version;
 
+	u8 rsvd0[6];
 
+
 
+	u16 func_id;
 
+	u8 op_code; /**< 1: alloc, 0: free. */
 
+	u8 rsvd1;
 
+
 
+	u16 msix_num;
 
+	u16 rsvd2;
 
+};
 
+
 
+int hinic3_get_interrupt_cfg(void *dev, struct interrupt_info *info);
 
+
 
+/**
 
+ * Set interrupt cfg.
 
+ *
 
+ * @param[in] dev
 
+ * Pointer to ethernet device structure.
 
+ * @param[in] info
 
+ * Interrupt info.
 
+ *
 
+ * @return
 
+ * 0 on success, non-zero on failure.
 
+ */
 
+int hinic3_set_interrupt_cfg(void *dev, struct interrupt_info info);
 
+
 
+int hinic3_set_wq_page_size(void *hwdev, u16 func_idx, u32 page_size);
 
+
 
+/**
 
+ * Send a reset command to hardware.
 
+ *
 
+ * @param[in] hwdev
 
+ * Pointer to hardware device structure.
 
+ * @param[in] reset_flag
 
+ * The flag that specifies the reset behavior.
 
+ *
 
+ * @return
 
+ * 0 on success, non-zero on failure.
 
+ */
 
+int hinic3_func_reset(void *hwdev, u64 reset_flag);
 
+
 
+/**
 
+ * Send a command to management module to set usage state of a specific service
 
+ * for given function.
 
+ *
 
+ * @param[in] hwdev
 
+ * Pointer to hardware device structure.
 
+ * @param[in] svc_type
 
+ * The service type to update.
 
+ * @param[in] state
 
+ * The state to set for the service.
 
+ *
 
+ * @return
 
+ * 0 on success, non-zero on failure.
 
+ */
 
+int hinic3_set_func_svc_used_state(void *hwdev, u16 svc_type, u8 state);
 
+
 
+/**
 
+ * Adjust the requested RX buffer size to the closest valid size supported by
 
+ * the hardware.
 
+ *
 
+ * @param[in] rx_buf_sz
 
+ * The requested RX buffer size.
 
+ * @param[out] match_sz
 
+ * The closest valid RX buffer size.
 
+ *
 
+ * @return
 
+ * 0 on success, non-zero on failure.
 
+ */
 
+int hinic3_convert_rx_buf_size(u32 rx_buf_sz, u32 *match_sz);
 
+
 
+/**
 
+ * Send a command to apply the settings.
 
+ *
 
+ * @param[in] hwdev
 
+ * Pointer to hardware device structure.
 
+ * @param[in] rq_depth
 
+ * The depth of the receive queue.
 
+ * @param[in] sq_depth
 
+ * The depth of the send queue.
 
+ * @param[in] rx_buf_sz
 
+ * The RX buffer size to set.
 
+ *
 
+ * @return
 
+ * 0 on success, non-zero on failure.
 
+ */
 
+int hinic3_set_root_ctxt(void *hwdev, u32 rq_depth, u32 sq_depth,
 
+			 u16 rx_buf_sz);
 
+
 
+/**
 
+ * Send a command to clear any previously set context.
 
+ *
 
+ * @param[in] hwdev
 
+ * Pointer to hardware device structure.
 
+ *
 
+ * @return
 
+ * 0 on success, non-zero on failure.
 
+ */
 
+int hinic3_clean_root_ctxt(void *hwdev);
 
+
 
+/**
 
+ * Send a command to set command queue depth.
 
+ *
 
+ * @param[in] hwdev
 
+ * Pointer to hardware device structure.
 
+ * @param[in] cmdq_depth
 
+ * The desired depth of the command queue, converted to logarithmic value
 
+ * before being set.
 
+ *
 
+ * @return
 
+ * 0 on success, non-zero on failure.
 
+ */
 
+int hinic3_set_cmdq_depth(void *hwdev, u16 cmdq_depth);
 
+
 
+/**
 
+ * Send a command to get firmware version.
 
+ *
 
+ * @param[in] hwdev
 
+ * Pointer to hardware device structure.
 
+ * @param[out] mgmt_ver
 
+ * The buffer to store the retrieved management firmware version.
 
+ * @param[in] max_mgmt_len
 
+ * The maximum length of the `mgmt_ver` buffer.
 
+ *
 
+ * @return
 
+ * 0 on success, non-zero on failure.
 
+ */
 
+int hinic3_get_mgmt_version(void *hwdev, char *mgmt_ver, int max_mgmt_len);
 
+
 
+/**
 
+ * Send a command to get board information.
 
+ *
 
+ * @param[in] hwdev
 
+ * Pointer to hardware device structure.
 
+ * @param[out] info
 
+ * The structure to store the retrieved board information.
 
+ *
 
+ * @return
 
+ * 0 on success, non-zero on failure.
 
+ */
 
+int hinic3_get_board_info(void *hwdev, struct hinic3_board_info *info);
 
+
 
+int hinic3_get_comm_features(void *hwdev, u64 *s_feature, u16 size);
 
+
 
+int hinic3_set_comm_features(void *hwdev, u64 *s_feature, u16 size);
 
+
 
+#endif /* _HINIC3_HW_COMM_H_ */
drivers/net/hinic3/base/hinic3_hwdev.c +0 -0

new file mode 100644

index 0000000000..38b9f88628

--- /dev/null

+++ b/drivers/net/hinic3/base/hinic3_hwdev.c

drivers/net/hinic3/base/hinic3_hwdev.c +553 -0
@@ -0,0 +1,573 @@
 
+/* SPDX-License-Identifier: BSD-3-Clause
 
+ * Copyright(c) 2025 Huawei Technologies Co., Ltd
 
+ */
 
+
 
+#include <rte_bus_pci.h>
 
+#include <rte_vfio.h>
 
+
 
+#include "hinic3_compat.h"
 
+#include "hinic3_cmd.h"
 
+#include "hinic3_cmdq.h"
 
+#include "hinic3_csr.h"
 
+#include "hinic3_eqs.h"
 
+#include "hinic3_hw_cfg.h"
 
+#include "hinic3_hw_comm.h"
 
+#include "hinic3_hwdev.h"
 
+#include "hinic3_hwif.h"
 
+#include "hinic3_mbox.h"
 
+#include "hinic3_mgmt.h"
 
+#include "hinic3_wq.h"
 
+
 
+enum hinic3_pcie_nosnoop { HINIC3_PCIE_SNOOP = 0, HINIC3_PCIE_NO_SNOOP = 1 };
 
+
 
+enum hinic3_pcie_tph {
 
+	HINIC3_PCIE_TPH_DISABLE = 0,
 
+	HINIC3_PCIE_TPH_ENABLE = 1
 
+};
 
+
 
+#define HINIC3_DMA_ATTR_INDIR_IDX_SHIFT 0
 
+
 
+#define HINIC3_DMA_ATTR_INDIR_IDX_MASK 0x3FF
 
+
 
+#define HINIC3_DMA_ATTR_INDIR_IDX_SET(val, member)            \
 
+	(((u32)(val) & HINIC3_DMA_ATTR_INDIR_##member##_MASK) \
 
+	 << HINIC3_DMA_ATTR_INDIR_##member##_SHIFT)
 
+
 
+#define HINIC3_DMA_ATTR_INDIR_IDX_CLEAR(val, member)      \
 
+	((val) & (~(HINIC3_DMA_ATTR_INDIR_##member##_MASK \
 
+		    << HINIC3_DMA_ATTR_INDIR_##member##_SHIFT)))
 
+
 
+#define HINIC3_DMA_ATTR_ENTRY_ST_SHIFT		0
 
+#define HINIC3_DMA_ATTR_ENTRY_AT_SHIFT		8
 
+#define HINIC3_DMA_ATTR_ENTRY_PH_SHIFT		10
 
+#define HINIC3_DMA_ATTR_ENTRY_NO_SNOOPING_SHIFT 12
 
+#define HINIC3_DMA_ATTR_ENTRY_TPH_EN_SHIFT	13
 
+
 
+#define HINIC3_DMA_ATTR_ENTRY_ST_MASK	       0xFF
 
+#define HINIC3_DMA_ATTR_ENTRY_AT_MASK	       0x3
 
+#define HINIC3_DMA_ATTR_ENTRY_PH_MASK	       0x3
 
+#define HINIC3_DMA_ATTR_ENTRY_NO_SNOOPING_MASK 0x1
 
+#define HINIC3_DMA_ATTR_ENTRY_TPH_EN_MASK      0x1
 
+
 
+#define HINIC3_DMA_ATTR_ENTRY_SET(val, member)                \
 
+	(((u32)(val) & HINIC3_DMA_ATTR_ENTRY_##member##_MASK) \
 
+	 << HINIC3_DMA_ATTR_ENTRY_##member##_SHIFT)
 
+
 
+#define HINIC3_DMA_ATTR_ENTRY_CLEAR(val, member)          \
 
+	((val) & (~(HINIC3_DMA_ATTR_ENTRY_##member##_MASK \
 
+		    << HINIC3_DMA_ATTR_ENTRY_##member##_SHIFT)))
 
+
 
+#define HINIC3_PCIE_ST_DISABLE 0
 
+#define HINIC3_PCIE_AT_DISABLE 0
 
+#define HINIC3_PCIE_PH_DISABLE 0
 
+
 
+#define PCIE_MSIX_ATTR_ENTRY 0
 
+
 
+#define HINIC3_CHIP_PRESENT 1
 
+#define HINIC3_CHIP_ABSENT  0
 
+
 
+#define HINIC3_DEFAULT_EQ_MSIX_PENDING_LIMIT	0
 
+#define HINIC3_DEFAULT_EQ_MSIX_COALESCE_TIMER_CFG 0xFF
 
+#define HINIC3_DEFAULT_EQ_MSIX_RESEND_TIMER_CFG	7
 
+
 
+typedef void (*mgmt_event_cb)(void *handle, void *buf_in, u16 in_size,
 
+			      void *buf_out, u16 *out_size);
 
+
 
+struct mgmt_event_handle {
 
+	u16 cmd;
 
+	mgmt_event_cb proc;
 
+};
 
+
 
+bool
 
+hinic3_is_vfio_iommu_enable(const struct rte_eth_dev *rte_dev)
 
+{
 
+	return ((RTE_ETH_DEV_TO_PCI(rte_dev)->kdrv == RTE_PCI_KDRV_VFIO) &&
 
+		(rte_vfio_noiommu_is_enabled() != 1));
 
+}
 
+
 
+int
 
+vf_handle_pf_comm_mbox(void *handle, __rte_unused void *pri_handle,
 
+		       __rte_unused u16 cmd, __rte_unused void *buf_in,
 
+		       __rte_unused u16 in_size, __rte_unused void *buf_out,
 
+		       __rte_unused u16 *out_size)
 
+{
 
+	struct hinic3_hwdev *hwdev = handle;
 
+
 
+	if (!hwdev)
 
+		return -EINVAL;
 
+
 
+	PMD_DRV_LOG(WARNING, "Unsupported pf mbox event %d to process", cmd);
 
+
 
+	return 0;
 
+}
 
+
 
+static void
 
+fault_event_handler(__rte_unused void *hwdev, __rte_unused void *buf_in,
 
+		    __rte_unused u16 in_size, __rte_unused void *buf_out,
 
+		    __rte_unused u16 *out_size)
 
+{
 
+	PMD_DRV_LOG(WARNING, "Unsupported fault event handler");
 
+}
 
+
 
+static void
 
+ffm_event_msg_handler(__rte_unused void *hwdev, void *buf_in, u16 in_size,
 
+		      __rte_unused void *buf_out, u16 *out_size)
 
+{
 
+	struct ffm_intr_info *intr = NULL;
 
+
 
+	if (in_size != sizeof(*intr)) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Invalid fault event report, "
 
+			    "length: %d, should be %zu",
 
+			    in_size, sizeof(*intr));
 
+		return;
 
+	}
 
+
 
+	intr = buf_in;
 
+
 
+	PMD_DRV_LOG(ERR,
 
+		    "node_id: 0x%x, err_type: 0x%x, err_level: %d, "
 
+		    "err_csr_addr: 0x%08x, err_csr_value: 0x%08x",
 
+		    intr->node_id, intr->err_type, intr->err_level,
 
+		    intr->err_csr_addr, intr->err_csr_value);
 
+
 
+	*out_size = sizeof(*intr);
 
+}
 
+
 
+static const struct mgmt_event_handle mgmt_event_proc[] = {
 
+	{
 
+		.cmd = HINIC3_MGMT_CMD_FAULT_REPORT,
 
+		.proc = fault_event_handler,
 
+	},
 
+
 
+	{
 
+		.cmd = HINIC3_MGMT_CMD_FFM_SET,
 
+		.proc = ffm_event_msg_handler,
 
+	},
 
+};
 
+
 
+void
 
+pf_handle_mgmt_comm_event(void *handle, __rte_unused void *pri_handle, u16 cmd,
 
+			  void *buf_in, u16 in_size, void *buf_out,
 
+			  u16 *out_size)
 
+{
 
+	struct hinic3_hwdev *hwdev = handle;
 
+	u32 i, event_num = RTE_DIM(mgmt_event_proc);
 
+
 
+	if (!hwdev)
 
+		return;
 
+
 
+	for (i = 0; i < event_num; i++) {
 
+		if (cmd == mgmt_event_proc[i].cmd) {
 
+			if (mgmt_event_proc[i].proc)
 
+				mgmt_event_proc[i].proc(handle, buf_in, in_size,
 
+							buf_out, out_size);
 
+			return;
 
+		}
 
+	}
 
+
 
+	PMD_DRV_LOG(WARNING, "Unsupported mgmt cpu event %d to process", cmd);
 
+}
 
+
 
+static int
 
+set_dma_attr_entry(__rte_unused struct hinic3_hwdev *hwdev,
 
+		   __rte_unused u8 entry_idx, __rte_unused u8 st,
 
+		   __rte_unused u8 at, __rte_unused u8 ph,
 
+		   __rte_unused enum hinic3_pcie_nosnoop no_snooping,
 
+		   __rte_unused enum hinic3_pcie_tph tph_en)
 
+{
 
+	struct hinic3_dma_attr_table attr;
 
+	u16 out_size = sizeof(attr);
 
+	int err;
 
+
 
+	memset(&attr, 0, sizeof(attr));
 
+	attr.func_id = hinic3_global_func_id(hwdev);
 
+	attr.entry_idx = entry_idx;
 
+	attr.st = st;
 
+	attr.at = at;
 
+	attr.ph = ph;
 
+	attr.no_snooping = no_snooping;
 
+	attr.tph_en = tph_en;
 
+
 
+	err = hinic3_msg_to_mgmt_sync(hwdev, HINIC3_MOD_COMM,
 
+				      HINIC3_MGMT_CMD_SET_DMA_ATTR, &attr,
 
+				      sizeof(attr), &attr, &out_size, 0);
 
+	if (err || !out_size || attr.head.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Set dma attribute failed, err: %d, status: 0x%x, "
 
+			    "out_size: 0x%x",
 
+			    err, attr.head.status, out_size);
 
+		return -EIO;
 
+	}
 
+
 
+	return 0;
 
+}
 
+
 
+/**
 
+ * Initialize the default dma attributes.
 
+ *
 
+ * @param[in] hwdev
 
+ * Pointer to hardware device structure.
 
+ *
 
+ * 0 on success, non-zero on failure.
 
+ */
 
+static int
 
+dma_attr_table_init(struct hinic3_hwdev *hwdev)
 
+{
 
+	return set_dma_attr_entry(hwdev,
 
+		PCIE_MSIX_ATTR_ENTRY, HINIC3_PCIE_ST_DISABLE,
 
+		HINIC3_PCIE_AT_DISABLE, HINIC3_PCIE_PH_DISABLE,
 
+		HINIC3_PCIE_SNOOP, HINIC3_PCIE_TPH_DISABLE);
 
+}
 
+
 
+static int
 
+init_aeqs_msix_attr(struct hinic3_hwdev *hwdev)
 
+{
 
+	struct hinic3_aeqs *aeqs = hwdev->aeqs;
 
+	struct interrupt_info info = {0};
 
+	struct hinic3_eq *eq = NULL;
 
+	u16 q_id;
 
+	int err;
 
+
 
+	info.lli_set = 0;
 
+	info.interrupt_coalesce_set = 1;
 
+	info.pending_limt = HINIC3_DEFAULT_EQ_MSIX_PENDING_LIMIT;
 
+	info.coalesce_timer_cfg = HINIC3_DEFAULT_EQ_MSIX_COALESCE_TIMER_CFG;
 
+	info.resend_timer_cfg = HINIC3_DEFAULT_EQ_MSIX_RESEND_TIMER_CFG;
 
+
 
+	for (q_id = 0; q_id < aeqs->num_aeqs; q_id++) {
 
+		eq = &aeqs->aeq[q_id];
 
+		info.msix_index = eq->eq_irq.msix_entry_idx;
 
+		err = hinic3_set_interrupt_cfg(hwdev, info);
 
+		if (err) {
 
+			PMD_DRV_LOG(ERR, "Set msix attr for aeq %d failed",
 
+				    q_id);
 
+			return -EFAULT;
 
+		}
 
+	}
 
+
 
+	return 0;
 
+}
 
+
 
+static int
 
+hinic3_comm_pf_to_mgmt_init(struct hinic3_hwdev *hwdev)
 
+{
 
+	int err;
 
+
 
+	/* VF does not support send msg to mgmt directly. */
 
+	if (hinic3_func_type(hwdev) == TYPE_VF)
 
+		return 0;
 
+
 
+	err = hinic3_pf_to_mgmt_init(hwdev);
 
+	if (err)
 
+		return err;
 
+
 
+	return 0;
 
+}
 
+
 
+static void
 
+hinic3_comm_pf_to_mgmt_free(struct hinic3_hwdev *hwdev)
 
+{
 
+	/* VF does not support send msg to mgmt directly. */
 
+	if (hinic3_func_type(hwdev) == TYPE_VF)
 
+		return;
 
+
 
+	hinic3_pf_to_mgmt_free(hwdev);
 
+}
 
+
 
+static int
 
+hinic3_comm_cmdqs_init(struct hinic3_hwdev *hwdev)
 
+{
 
+	int err;
 
+
 
+	err = hinic3_cmdqs_init(hwdev);
 
+	if (err) {
 
+		PMD_DRV_LOG(ERR, "Init cmd queues failed");
 
+		return err;
 
+	}
 
+
 
+	err = hinic3_set_cmdq_depth(hwdev, HINIC3_CMDQ_DEPTH);
 
+	if (err) {
 
+		PMD_DRV_LOG(ERR, "Set cmdq depth failed");
 
+		goto set_cmdq_depth_err;
 
+	}
 
+
 
+	return 0;
 
+
 
+set_cmdq_depth_err:
 
+	hinic3_cmdqs_free(hwdev);
 
+
 
+	return err;
 
+}
 
+
 
+static void
 
+hinic3_comm_cmdqs_free(struct hinic3_hwdev *hwdev)
 
+{
 
+	hinic3_cmdqs_free(hwdev);
 
+}
 
+
 
+static void
 
+hinic3_sync_mgmt_func_state(struct hinic3_hwdev *hwdev)
 
+{
 
+	hinic3_set_pf_status(hwdev->hwif, HINIC3_PF_STATUS_ACTIVE_FLAG);
 
+}
 
+
 
+static int
 
+get_func_misc_info(struct hinic3_hwdev *hwdev)
 
[… more lines in the raw message]

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

drivers/net/hinic3/base/hinic3_hwdev.c

Quoted 10 lines
drivers/net/hinic3/base/hinic3_hwdev.c +5 -0
+{
 
+	int err;
 
+
 
+	err = hinic3_get_board_info(hwdev, &hwdev->board_info);
 
+	if (err) {
 
[… more lines in the raw message]

why need this define?

Quoted 397 lines
drivers/net/hinic3/base/hinic3_hwdev.c +15 -0

Lines of this file are not shown. View the raw message.

drivers/net/hinic3/base/hinic3_hwdev.h +0 -0

The rest of this diff is not shown. View the raw message.

new file mode 100644

index 0000000000..080d1400ed

--- /dev/null

+++ b/drivers/net/hinic3/base/hinic3_hwdev.h

drivers/net/hinic3/base/hinic3_hwdev.h +177 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Part of the diff is not shown here. View the raw message.

#142 lore raw

PATCH v5 9/18 net/hinic3: add a NIC business configuration module

In reply to Feifei Wang

From: Yi Chen <[email protected]>

The items of configurations and queries for NIC business include MAC, VLAN, MTU, RSS and so on. These configurations and queries are handled by mgmt module. This patch introduces related data structures and function codes.

---
 drivers/net/hinic3/base/hinic3_nic_cfg.c | 1828 ++++++++++++++++++++++
 drivers/net/hinic3/base/hinic3_nic_cfg.h | 1527 ++++++++++++++++++
 2 files changed, 3355 insertions(+)
 create mode 100644 drivers/net/hinic3/base/hinic3_nic_cfg.c
 create mode 100644 drivers/net/hinic3/base/hinic3_nic_cfg.h
drivers/net/hinic3/base/hinic3_nic_cfg.c new file +1828 -0
new file mode 100644
index 0000000000..918ebc37e9
@@ -0,0 +1,1828 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2025 Huawei Technologies Co., Ltd
+ */
+
+#include <rte_atomic.h>
+#include <rte_ether.h>
+
+#include "hinic3_compat.h"
+#include "hinic3_cmd.h"
+#include "hinic3_cmdq.h"
+#include "hinic3_hw_cfg.h"
+#include "hinic3_hwdev.h"
+#include "hinic3_hwif.h"
+#include "hinic3_mbox.h"
+#include "hinic3_nic_cfg.h"
+#include "hinic3_wq.h"
+
+struct vf_msg_handler {
+	u16 cmd;
+};
+
+static const struct vf_msg_handler vf_cmd_handler[] = {
+	{
+		.cmd = HINIC3_NIC_CMD_VF_REGISTER,
+	},
+
+	{
+		.cmd = HINIC3_NIC_CMD_GET_MAC,
+	},
+
+	{
+		.cmd = HINIC3_NIC_CMD_SET_MAC,
+	},
+
+	{
+		.cmd = HINIC3_NIC_CMD_DEL_MAC,
+	},
+
+	{
+		.cmd = HINIC3_NIC_CMD_UPDATE_MAC,
+	},
+
+	{
+		.cmd = HINIC3_NIC_CMD_VF_COS,
+	},
+};
+
+static const struct vf_msg_handler vf_mag_cmd_handler[] = {
+	{
+		.cmd = MAG_CMD_GET_LINK_STATUS,
+	},
+};
+
+static int mag_msg_to_mgmt_sync(void *hwdev, u16 cmd, void *buf_in, u16 in_size,
+				void *buf_out, u16 *out_size);
+
+/**
+ * If device is VF and command is found in a predefined list, send to PF, else
+ * send to management module.
+ *
+ * @param[in] hwdev
+ * The pointer to the hardware device structure.
+ * @param[in] cmd
+ * The command to send.
+ * @param[in] buf_in
+ * The input buffer containing the request data.
+ * @param[in] in_size
+ * The size of the input buffer.
+ * @param[out] buf_out
+ * The output buffer to receive the response data.
+ * @param[out] out_size
+ * The size of the output buffer on return.
+ *
+ * @return
+ * 0 on success, non-zero on failure.
+ */
+int
+l2nic_msg_to_mgmt_sync(void *hwdev, u16 cmd, void *buf_in, u16 in_size,
+		       void *buf_out, u16 *out_size)
+{
+	u32 i, cmd_cnt = ARRAY_LEN(vf_cmd_handler);
+	bool cmd_to_pf = false;
+
+	if (hinic3_func_type(hwdev) == TYPE_VF) {
+		for (i = 0; i < cmd_cnt; i++) {
+			if (cmd == vf_cmd_handler[i].cmd)
+				cmd_to_pf = true;
+		}
+	}
+
+	if (cmd_to_pf) {
+		return hinic3_mbox_to_pf(hwdev, HINIC3_MOD_L2NIC, cmd, buf_in,
+					 in_size, buf_out, out_size, 0);
+	}
+
+	return hinic3_msg_to_mgmt_sync(hwdev, HINIC3_MOD_L2NIC, cmd, buf_in,
+				       in_size, buf_out, out_size, 0);
+}
+
+/**
+ * Set CI table for a SQ.
+ *
+ * Configure the CI table with attributes like CI address, pending limit,
+ * coalescing time, and optional interrupt settings for specified SQ.
+ *
+ * @param[in] hwdev
+ * Pointer to hardware device structure.
+ * @param[in] attr
+ * Attributes to configure for CI table.
+ *
+ * @return
+ * 0 on success, non-zero on failure.
+ */
+int
+hinic3_set_ci_table(void *hwdev, struct hinic3_sq_attr *attr)
+{
+	struct hinic3_cmd_cons_idx_attr cons_idx_attr;
+	u16 out_size = sizeof(cons_idx_attr);
+	int err;
+
+	if (!hwdev || !attr)
+		return -EINVAL;
+
+	memset(&cons_idx_attr, 0, sizeof(cons_idx_attr));
+	cons_idx_attr.func_idx = hinic3_global_func_id(hwdev);
+	cons_idx_attr.dma_attr_off = attr->dma_attr_off;
+	cons_idx_attr.pending_limit = attr->pending_limit;
+	cons_idx_attr.coalescing_time = attr->coalescing_time;
+
+	if (attr->intr_en) {
+		cons_idx_attr.intr_en = attr->intr_en;
+		cons_idx_attr.intr_idx = attr->intr_idx;
+	}
+
+	cons_idx_attr.l2nic_sqn = attr->l2nic_sqn;
+	cons_idx_attr.ci_addr = attr->ci_dma_base;
+
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_SQ_CI_ATTR_SET,
+				     &cons_idx_attr, sizeof(cons_idx_attr),
+				     &cons_idx_attr, &out_size);
+	if (err || !out_size || cons_idx_attr.msg_head.status) {
+		PMD_DRV_LOG(ERR,
+			    "Set ci attribute table failed, "
+			    "err: %d, status: 0x%x, out_size: 0x%x",
+			    err, cons_idx_attr.msg_head.status, out_size);
+		return -EFAULT;
+	}
+
+	return 0;
+}
+
+#define PF_SET_VF_MAC(hwdev, status)           \
+	(hinic3_func_type(hwdev) == TYPE_VF && \
+	 (status) == HINIC3_PF_SET_VF_ALREADY)
+
+static int
+hinic3_check_mac_info(void *hwdev, u8 status, u16 vlan_id)
+{
+	if ((status && status != HINIC3_MGMT_STATUS_EXIST) ||
+	    ((vlan_id & CHECK_IPSU_15BIT) &&
+	     status == HINIC3_MGMT_STATUS_EXIST)) {
+		if (PF_SET_VF_MAC(hwdev, status))
+			return 0;
+
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+#define VLAN_N_VID 4096
+
+int
+hinic3_set_mac(void *hwdev, const u8 *mac_addr, u16 vlan_id, u16 func_id)
+{
+	struct hinic3_port_mac_set mac_info;
+	u16 out_size = sizeof(mac_info);
+	int err;
+
+	if (!hwdev || !mac_addr)
+		return -EINVAL;
+
+	memset(&mac_info, 0, sizeof(mac_info));
+
+	if (vlan_id >= VLAN_N_VID) {
+		PMD_DRV_LOG(ERR, "Invalid VLAN number: %d", vlan_id);
+		return -EINVAL;
+	}
+
+	mac_info.func_id = func_id;
+	mac_info.vlan_id = vlan_id;
+	memmove(mac_info.mac, mac_addr, ETH_ALEN);
+
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_SET_MAC, &mac_info,
+				     sizeof(mac_info), &mac_info, &out_size);
+	if (err || !out_size ||
+	    hinic3_check_mac_info(hwdev, mac_info.msg_head.status,
+				  mac_info.vlan_id)) {
+		PMD_DRV_LOG(ERR,
+			    "Update MAC failed, "
+			    "err: %d, status: 0x%x, out size: 0x%x",
+			    err, mac_info.msg_head.status, out_size);
+		return -EIO;
+	}
+
+	if (PF_SET_VF_MAC(hwdev, mac_info.msg_head.status)) {
+		PMD_DRV_LOG(WARNING,
+			    "PF has already set VF mac, Ignore set operation");
+		return HINIC3_PF_SET_VF_ALREADY;
+	}
+
+	if (mac_info.msg_head.status == HINIC3_MGMT_STATUS_EXIST) {
+		PMD_DRV_LOG(WARNING,
+			    "MAC is repeated. Ignore update operation");
+		return 0;
+	}
+
+	return 0;
+}
+
+int
+hinic3_del_mac(void *hwdev, const u8 *mac_addr, u16 vlan_id, u16 func_id)
+{
+	struct hinic3_port_mac_set mac_info;
+	u16 out_size = sizeof(mac_info);
+	int err;
+
+	if (!hwdev || !mac_addr)
+		return -EINVAL;
+
+	if (vlan_id >= VLAN_N_VID) {
+		PMD_DRV_LOG(ERR, "Invalid VLAN number: %d", vlan_id);
+		return -EINVAL;
+	}
+
+	memset(&mac_info, 0, sizeof(mac_info));
+	mac_info.func_id = func_id;
+	mac_info.vlan_id = vlan_id;
+	memmove(mac_info.mac, mac_addr, ETH_ALEN);
+
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_DEL_MAC, &mac_info,
+				     sizeof(mac_info), &mac_info, &out_size);
+	if (err || !out_size ||
+	    (mac_info.msg_head.status &&
+	     !PF_SET_VF_MAC(hwdev, mac_info.msg_head.status))) {
+		PMD_DRV_LOG(ERR,
+			    "Delete MAC failed, "
+			    "err: %d, status: 0x%x, out size: 0x%x",
+			    err, mac_info.msg_head.status, out_size);
+		return -EIO;
+	}
+
+	if (PF_SET_VF_MAC(hwdev, mac_info.msg_head.status)) {
+		PMD_DRV_LOG(WARNING,
+			"PF has already set VF mac, Ignore delete operation");
+		return HINIC3_PF_SET_VF_ALREADY;
+	}
+
+	return 0;
+}
+
+int
+hinic3_update_mac(void *hwdev, u8 *old_mac, u8 *new_mac, u16 vlan_id,
+		  u16 func_id)
+{
+	struct hinic3_port_mac_update mac_info;
+	u16 out_size = sizeof(mac_info);
+	int err;
+
+	if (!hwdev || !old_mac || !new_mac)
+		return -EINVAL;
+
+	if (vlan_id >= VLAN_N_VID) {
+		PMD_DRV_LOG(ERR, "Invalid VLAN number: %d", vlan_id);
+		return -EINVAL;
+	}
+
+	memset(&mac_info, 0, sizeof(mac_info));
+	mac_info.func_id = func_id;
+	mac_info.vlan_id = vlan_id;
+	memcpy(mac_info.old_mac, old_mac, ETH_ALEN);
+	memcpy(mac_info.new_mac, new_mac, ETH_ALEN);
+
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_UPDATE_MAC,
+				     &mac_info, sizeof(mac_info), &mac_info,
+				     &out_size);
+	if (err || !out_size ||
+	    hinic3_check_mac_info(hwdev, mac_info.msg_head.status,
+				  mac_info.vlan_id)) {
+		PMD_DRV_LOG(ERR,
+			    "Update MAC failed, "
+			    "err: %d, status: 0x%x, out size: 0x%x",
+			    err, mac_info.msg_head.status, out_size);
+		return -EIO;
+	}
+
+	if (PF_SET_VF_MAC(hwdev, mac_info.msg_head.status)) {
+		PMD_DRV_LOG(WARNING,
+			"PF has already set VF MAC. Ignore update operation");
+		return HINIC3_PF_SET_VF_ALREADY;
+	}
+
+	if (mac_info.msg_head.status == HINIC3_MGMT_STATUS_EXIST) {
+		PMD_DRV_LOG(INFO, "MAC is repeated. Ignore update operation");
+		return 0;
+	}
+
+	return 0;
+}
+
+int
+hinic3_get_default_mac(void *hwdev, u8 *mac_addr, int ether_len)
+{
+	struct hinic3_port_mac_set mac_info;
+	u16 out_size = sizeof(mac_info);
+	int err;
+
+	if (!hwdev || !mac_addr)
+		return -EINVAL;
+
+	memset(&mac_info, 0, sizeof(mac_info));
+	mac_info.func_id = hinic3_global_func_id(hwdev);
+
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_GET_MAC, &mac_info,
+				     sizeof(mac_info), &mac_info, &out_size);
+	if (err || !out_size || mac_info.msg_head.status) {
+		PMD_DRV_LOG(ERR,
+			"Get MAC failed, err: %d, status: 0x%x, out size: 0x%x",
+			err, mac_info.msg_head.status, out_size);
+		return -EINVAL;
+	}
+
+	memmove(mac_addr, mac_info.mac, ether_len);
+
+	return 0;
+}
+
+static int
+hinic3_config_vlan(void *hwdev, u8 opcode, u16 vlan_id, u16 func_id)
+{
+	struct hinic3_cmd_vlan_config vlan_info;
+	u16 out_size = sizeof(vlan_info);
+	int err;
+
+	memset(&vlan_info, 0, sizeof(vlan_info));
+	vlan_info.opcode = opcode;
+	vlan_info.func_id = func_id;
+	vlan_info.vlan_id = vlan_id;
+
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_CFG_FUNC_VLAN,
+				     &vlan_info, sizeof(vlan_info), &vlan_info,
+				     &out_size);
+	if (err || !out_size || vlan_info.msg_head.status) {
+		PMD_DRV_LOG(ERR,
+			"%s vlan failed, err: %d, status: 0x%x, out size: 0x%x",
+			opcode == HINIC3_CMD_OP_ADD ? "Add" : "Delete", err,
+			vlan_info.msg_head.status, out_size);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+int
+hinic3_add_vlan(void *hwdev, u16 vlan_id, u16 func_id)
+{
+	if (!hwdev)
+		return -EINVAL;
+
+	return hinic3_config_vlan(hwdev, HINIC3_CMD_OP_ADD, vlan_id, func_id);
+}
+
+int
+hinic3_del_vlan(void *hwdev, u16 vlan_id, u16 func_id)
+{
+	if (!hwdev)
+		return -EINVAL;
+
+	return hinic3_config_vlan(hwdev, HINIC3_CMD_OP_DEL, vlan_id, func_id);
+}
+
+int
+hinic3_get_port_info(void *hwdev, struct nic_port_info *port_info)
+{
+	struct hinic3_cmd_port_info port_msg;
+	u16 out_size = sizeof(port_msg);
+	int err;
+
+	if (!hwdev || !port_info)
+		return -EINVAL;
+
+	memset(&port_msg, 0, sizeof(port_msg));
+	port_msg.port_id = hinic3_physical_port_id(hwdev);
+
+	err = mag_msg_to_mgmt_sync(hwdev, MAG_CMD_GET_PORT_INFO, &port_msg,
+				   sizeof(port_msg), &port_msg, &out_size);
+	if (err || !out_size || port_msg.msg_head.status) {
+		PMD_DRV_LOG(ERR,
+			    "Get port info failed, "
+			    "err: %d, status: 0x%x, out size: 0x%x",
+			    err, port_msg.msg_head.status, out_size);
+		return -EINVAL;
+	}
+
+	port_info->autoneg_cap = port_msg.autoneg_cap;
+	port_info->autoneg_state = port_msg.autoneg_state;
+	port_info->duplex = port_msg.duplex;
+	port_info->port_type = port_msg.port_type;
+	port_info->speed = port_msg.speed;
+	port_info->fec = port_msg.fec;
+
+	return 0;
+}
+
+int
+hinic3_get_link_state(void *hwdev, u8 *link_state)
+{
+	struct hinic3_cmd_link_state get_link;
+	u16 out_size = sizeof(get_link);
+	int err;
+
+	if (!hwdev || !link_state)
+		return -EINVAL;
+
+	memset(&get_link, 0, sizeof(get_link));
+	get_link.port_id = hinic3_physical_port_id(hwdev);
+	err = mag_msg_to_mgmt_sync(hwdev, MAG_CMD_GET_LINK_STATUS, &get_link,
+				   sizeof(get_link), &get_link, &out_size);
+	if (err || !out_size || get_link.msg_head.status) {
+		PMD_DRV_LOG(ERR,
+			    "Get link state failed, "
+			    "err: %d, status: 0x%x, out size: 0x%x",
+			    err, get_link.msg_head.status, out_size);
+		return -EIO;
+	}
+
+	*link_state = get_link.state;
+
+	return 0;
+}
+
+int
+hinic3_set_vport_enable(void *hwdev, bool enable)
+{
+	struct hinic3_vport_state en_state;
+	u16 out_size = sizeof(en_state);
+	int err;
+
+	if (!hwdev)
+		return -EINVAL;
+
+	memset(&en_state, 0, sizeof(en_state));
+	en_state.func_id = hinic3_global_func_id(hwdev);
+	en_state.state = enable ? 1 : 0;
+
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_SET_VPORT_ENABLE,
+				     &en_state, sizeof(en_state), &en_state,
+				     &out_size);
+	if (err || !out_size || en_state.msg_head.status) {
+		PMD_DRV_LOG(ERR,
+			    "Set vport state failed, "
+			    "err: %d, status: 0x%x, out size: 0x%x",
+			    err, en_state.msg_head.status, out_size);
+		return -EIO;
+	}
+
+	return 0;
+}
+
+int
+hinic3_set_port_enable(void *hwdev, bool enable)
+{
+	struct mag_cmd_set_port_enable en_state;
+	u16 out_size = sizeof(en_state);
+	int err;
+
+	if (!hwdev)
+		return -EINVAL;
+
+	if (hinic3_func_type(hwdev) == TYPE_VF)
+		return 0;
+
+	memset(&en_state, 0, sizeof(en_state));
+	en_state.function_id = hinic3_global_func_id(hwdev);
+	en_state.state = enable ? MAG_CMD_TX_ENABLE | MAG_CMD_RX_ENABLE
+				: MAG_CMD_PORT_DISABLE;
+
+	err = mag_msg_to_mgmt_sync(hwdev, MAG_CMD_SET_PORT_ENABLE, &en_state,
+				   sizeof(en_state), &en_state, &out_size);
+	if (err || !out_size || en_state.head.status) {
+		PMD_DRV_LOG(ERR,
+			    "Set port state failed, "
+			    "err: %d, status: 0x%x, out size: 0x%x",
+			    err, en_state.head.status, out_size);
+		return -EIO;
+	}
+
+	return 0;
+}
+
+int
+hinic3_flush_qps_res(void *hwdev)
+{
+	struct hinic3_cmd_clear_qp_resource sq_res;
+	u16 out_size = sizeof(sq_res);
+	int err;
+
+	if (!hwdev)
+		return -EINVAL;
+
+	memset(&sq_res, 0, sizeof(sq_res));
+	sq_res.func_id = hinic3_global_func_id(hwdev);
+
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_CLEAR_QP_RESOURCE,
+				     &sq_res, sizeof(sq_res), &sq_res,
+				     &out_size);
+	if (err || !out_size || sq_res.msg_head.status) {
+		PMD_DRV_LOG(ERR,
+			    "Clear sq resources failed, "
+			    "err: %d, status: 0x%x, out size: 0x%x",
+			    err, sq_res.msg_head.status, out_size);
+		return -EIO;
+	}
+
+	return 0;
+}
+
+/**
+ * Get or set the flow control (pause frame) settings for NIC.
+ *
+ * @param[in] hwdev
+ * Pointer to the hardware device.
+ * @param[in] opcode
+ * The operation to perform. Use `HINIC3_CMD_OP_SET` to set the pause settings
+ * and `HINIC3_CMD_OP_GET` to get them.
+ * @param[out] nic_pause
+ * Pointer to the `nic_pause_config` structure. This structure contains the flow
+ * control settings (auto-negotiation, Rx pause, and Tx pause). It is updated
+ * when getting settings. When setting, the values in this structure are used.
+ *
+ * @return
+ * 0 on success, non-zero on failure.
+ * - -EIO if the operation failed.
+ */
+static int
+hinic3_cfg_hw_pause(void *hwdev, u8 opcode, struct nic_pause_config *nic_pause)
+{
+	struct hinic3_cmd_pause_config pause_info = {0};
+	u16 out_size = sizeof(pause_info);
+	int err = 0;
+
+	pause_info.port_id = hinic3_physical_port_id(hwdev);
+	pause_info.opcode = opcode;
+	if (opcode == HINIC3_CMD_OP_SET) {
+		pause_info.auto_neg = nic_pause->auto_neg;
+		pause_info.rx_pause = nic_pause->rx_pause;
+		pause_info.tx_pause = nic_pause->tx_pause;
+	}
+
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_CFG_PAUSE_INFO,
+				     &pause_info, sizeof(pause_info),
+				     &pause_info, &out_size);
+	if (err || !out_size || pause_info.msg_head.status) {
+		PMD_DRV_LOG(ERR,
+			    "%s pause info failed, err: %d, status: 0x%x, out "
+			    "size: 0x%x",
+			    opcode == HINIC3_CMD_OP_SET ? "Set" : "Get", err,
+			    pause_info.msg_head.status, out_size);
+		return -EIO;
+	}
+
+	if (opcode == HINIC3_CMD_OP_GET) {
+		nic_pause->auto_neg = pause_info.auto_neg;
+		nic_pause->rx_pause = pause_info.rx_pause;
+		nic_pause->tx_pause = pause_info.tx_pause;
+	}
+
+	return 0;
+}
+
+int
+hinic3_set_pause_info(void *hwdev, struct nic_pause_config nic_pause)
+{
+	if (!hwdev)
+		return -EINVAL;
+
+	return hinic3_cfg_hw_pause(hwdev, HINIC3_CMD_OP_SET, &nic_pause);
+}
+
+int
+hinic3_get_pause_info(void *hwdev, struct nic_pause_config *nic_pause)
+{
+	if (!hwdev || !nic_pause)
+		return -EINVAL;
+
+	return hinic3_cfg_hw_pause(hwdev, HINIC3_CMD_OP_GET, nic_pause);
+}
+
+int
+hinic3_get_vport_stats(void *hwdev, struct hinic3_vport_stats *stats)
+{
+	struct hinic3_port_stats_info stats_info;
+	struct hinic3_cmd_vport_stats vport_stats;
+	u16 out_size = sizeof(vport_stats);
+	int err;
+
+	if (!hwdev || !stats)
+		return -EINVAL;
+
+	memset(&stats_info, 0, sizeof(stats_info));
+	memset(&vport_stats, 0, sizeof(vport_stats));
+
+	stats_info.func_id = hinic3_global_func_id(hwdev);
+
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_GET_VPORT_STAT,
+				     &stats_info, sizeof(stats_info),
+				     &vport_stats, &out_size);
+	if (err || !out_size || vport_stats.msg_head.status) {
+		PMD_DRV_LOG(ERR,
+			    "Get function stats failed, err: %d, status: 0x%x, "
+			    "out size: 0x%x",
+			    err, vport_stats.msg_head.status, out_size);
+		return -EIO;
+	}
+
+	memcpy(stats, &vport_stats.stats, sizeof(*stats));
+
+	return 0;
+}
+
+int
+hinic3_get_phy_port_stats(void *hwdev, struct mag_phy_port_stats *stats)
+{
+	struct mag_cmd_get_port_stat *port_stats = NULL;
+	struct mag_cmd_port_stats_info stats_info;
+	u16 out_size = sizeof(*port_stats);
+	int err;
+
+	port_stats = rte_zmalloc("port_stats", sizeof(*port_stats), 0);
+	if (!port_stats)
+		return -ENOMEM;
+
+	memset(&stats_info, 0, sizeof(stats_info));
+	stats_info.port_id = hinic3_physical_port_id(hwdev);
+
+	err = mag_msg_to_mgmt_sync(hwdev, MAG_CMD_GET_PORT_STAT, &stats_info,
+				   sizeof(stats_info), port_stats, &out_size);
+	if (err || !out_size || port_stats->head.status) {
+		PMD_DRV_LOG(ERR,
+			    "Failed to get port statistics, err: %d, status: "
+			    "0x%x, out size: 0x%x",
+			    err, port_stats->head.status, out_size);
+		err = -EIO;
+		goto out;
+	}
+
+	memcpy(stats, &port_stats->counter, sizeof(*stats));
+
+out:
+	rte_free(port_stats);
+
+	return err;
+}
+
+int
+hinic3_clear_vport_stats(void *hwdev)
+{
+	struct hinic3_cmd_clear_vport_stats clear_vport_stats;
+	u16 out_size = sizeof(clear_vport_stats);
+	int err;
+
+	if (!hwdev) {
+		PMD_DRV_LOG(ERR, "Hwdev is NULL");
+		return -EINVAL;
+	}
+
+	memset(&clear_vport_stats, 0, sizeof(clear_vport_stats));
+	clear_vport_stats.func_id = hinic3_global_func_id(hwdev);
+
+	err = l2nic_msg_to_mgmt_sync(hwdev,
+		HINIC3_NIC_CMD_CLEAN_VPORT_STAT, &clear_vport_stats,
+		sizeof(clear_vport_stats), &clear_vport_stats, &out_size);
+	if (err || !out_size || clear_vport_stats.msg_head.status) {
+		PMD_DRV_LOG(ERR,
+			    "Clear vport stats failed, "
+			    "err: %d, status: 0x%x, out size: 0x%x",
+			    err, clear_vport_stats.msg_head.status, out_size);
+		return -EIO;
+	}
+
+	return 0;
+}
+
+int
+hinic3_clear_phy_port_stats(void *hwdev)
+{
+	struct mag_cmd_port_stats_info port_stats;
+	u16 out_size = sizeof(port_stats);
+	int err;
+
+	port_stats.port_id = hinic3_physical_port_id(hwdev);
+
+	err = mag_msg_to_mgmt_sync(hwdev, MAG_CMD_CLR_PORT_STAT, &port_stats,
+				   sizeof(port_stats), &port_stats, &out_size);
+	if (err || !out_size || port_stats.head.status) {
+		PMD_DRV_LOG(ERR,
+			    "Failed to get port statistics, "
+			    "err: %d, status: 0x%x, out size: 0x%x",
+			    err, port_stats.head.status, out_size);
+		err = -EIO;
+	}
+
+	return err;
+}
+
+static int
+hinic3_set_function_table(void *hwdev, u32 cfg_bitmap,
+			  struct hinic3_func_tbl_cfg *cfg)
+{
+	struct hinic3_cmd_set_func_tbl cmd_func_tbl;
+	u16 out_size = sizeof(cmd_func_tbl);
+	int err;
+
+	memset(&cmd_func_tbl, 0, sizeof(cmd_func_tbl));
+	cmd_func_tbl.func_id = hinic3_global_func_id(hwdev);
+	cmd_func_tbl.cfg_bitmap = cfg_bitmap;
+	cmd_func_tbl.tbl_cfg = *cfg;
+
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_SET_FUNC_TBL,
+				     &cmd_func_tbl, sizeof(cmd_func_tbl),
+				     &cmd_func_tbl, &out_size);
+	if (err || cmd_func_tbl.msg_head.status || !out_size) {
+		PMD_DRV_LOG(ERR,
+			    "Set func table failed, bitmap: 0x%x, err: %d, "
+			    "status: 0x%x, out size: 0x%x",
+			    cfg_bitmap, err, cmd_func_tbl.msg_head.status,
+			    out_size);
+		return -EFAULT;
+	}
+
+	return 0;
+}
+
+int
+hinic3_init_function_table(void *hwdev, u16 rx_buff_len)
+{
+	struct hinic3_func_tbl_cfg func_tbl_cfg;
+	u32 cfg_bitmap = BIT(FUNC_CFG_INIT) | BIT(FUNC_CFG_MTU) |
+			 BIT(FUNC_CFG_RX_BUF_SIZE);
+
+	memset(&func_tbl_cfg, 0, sizeof(func_tbl_cfg));
+	func_tbl_cfg.mtu = 0x3FFF; /**< Default, max mtu */
+	func_tbl_cfg.rx_wqe_buf_size = rx_buff_len;
+
+	return hinic3_set_function_table(hwdev, cfg_bitmap, &func_tbl_cfg);
+}
+
+int
+hinic3_set_port_mtu(void *hwdev, u16 new_mtu)
+{
+	struct hinic3_func_tbl_cfg func_tbl_cfg;
+
+	if (!hwdev)
+		return -EINVAL;
+
+	if (new_mtu < HINIC3_MIN_MTU_SIZE) {
+		PMD_DRV_LOG(ERR,
+			    "Invalid mtu size: %ubytes, mtu size < %ubytes",
+			    new_mtu, HINIC3_MIN_MTU_SIZE);
+		return -EINVAL;
+	}
+
+	if (new_mtu > HINIC3_MAX_JUMBO_FRAME_SIZE) {
+		PMD_DRV_LOG(ERR,
+			    "Invalid mtu size: %ubytes, mtu size > %ubytes",
+			    new_mtu, HINIC3_MAX_JUMBO_FRAME_SIZE);
+		return -EINVAL;
+	}
+
+	memset(&func_tbl_cfg, 0, sizeof(func_tbl_cfg));
+	func_tbl_cfg.mtu = new_mtu;
+	return hinic3_set_function_table(hwdev, BIT(FUNC_CFG_MTU),
+					 &func_tbl_cfg);
+}
+
+static int
+nic_feature_nego(void *hwdev, u8 opcode, u64 *s_feature, u16 size)
+{
+	struct hinic3_cmd_feature_nego feature_nego;
+	u16 out_size = sizeof(feature_nego);
+	int err;
+
+	if (!hwdev || !s_feature || size > MAX_FEATURE_QWORD)
+		return -EINVAL;
+
+	memset(&feature_nego, 0, sizeof(feature_nego));
+	feature_nego.func_id = hinic3_global_func_id(hwdev);
+	feature_nego.opcode = opcode;
+	if (opcode == HINIC3_CMD_OP_SET)
+		memcpy(feature_nego.s_feature, s_feature, size * sizeof(u64));
+
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_FEATURE_NEGO,
+				     &feature_nego, sizeof(feature_nego),
+				     &feature_nego, &out_size);
+	if (err || !out_size || feature_nego.msg_head.status) {
+		PMD_DRV_LOG(ERR,
+			    "Failed to negotiate nic feature, err:%d, status: "
+			    "0x%x, out_size: 0x%x",
+			    err, feature_nego.msg_head.status, out_size);
+		return -EFAULT;
+	}
+
+	if (opcode == HINIC3_CMD_OP_GET)
+		memcpy(s_feature, feature_nego.s_feature, size * sizeof(u64));
+
+	return 0;
+}
+
+int
+hinic3_get_feature_from_hw(void *hwdev, u64 *s_feature, u16 size)
+{
+	return nic_feature_nego(hwdev, HINIC3_CMD_OP_GET, s_feature, size);
+}
+
+int
+hinic3_set_feature_to_hw(void *hwdev, u64 *s_feature, u16 size)
+{
+	return nic_feature_nego(hwdev, HINIC3_CMD_OP_SET, s_feature, size);
+}
+
+static int
+hinic3_vf_func_init(void *hwdev)
+{
+	struct hinic3_cmd_register_vf register_info;
+	u16 out_size = sizeof(register_info);
+	int err;
+
+	if (hinic3_func_type(hwdev) != TYPE_VF)
+		return 0;
+
+	memset(&register_info, 0, sizeof(register_info));
+	register_info.op_register = 1;
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_VF_REGISTER,
+				     &register_info, sizeof(register_info),
+				     &register_info, &out_size);
+	if (err || register_info.msg_head.status || !out_size) {
+		PMD_DRV_LOG(ERR,
+			    "Register VF failed, err: %d, status: 0x%x, out "
+			    "size: 0x%x",
+			    err, register_info.msg_head.status, out_size);
+		return -EFAULT;
+	}
+
+	return 0;
+}
+
+static int
+hinic3_vf_func_free(void *hwdev)
+{
+	struct hinic3_cmd_register_vf unregister;
+	u16 out_size = sizeof(unregister);
+	int err;
+
+	if (hinic3_func_type(hwdev) != TYPE_VF)
+		return 0;
+
+	memset(&unregister, 0, sizeof(unregister));
+	unregister.op_register = 0;
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_VF_REGISTER,
+				     &unregister, sizeof(unregister),
+				     &unregister, &out_size);
+	if (err || unregister.msg_head.status || !out_size) {
+		PMD_DRV_LOG(ERR,
+			    "Unregister VF failed, err: %d, status: 0x%x, out "
+			    "size: 0x%x",
+			    err, unregister.msg_head.status, out_size);
+		return -EFAULT;
+	}
+
+	return 0;
+}
+
+int
+hinic3_init_nic_hwdev(void *hwdev)
+{
+	return hinic3_vf_func_init(hwdev);
+}
+
+void
+hinic3_free_nic_hwdev(void *hwdev)
+{
+	if (!hwdev)
+		return;
+
+	if (hinic3_func_type(hwdev) != TYPE_VF)
+		hinic3_set_link_status_follow(hwdev, HINIC3_LINK_FOLLOW_DEFAULT);
+
+	hinic3_vf_func_free(hwdev);
+}
+
+int
+hinic3_set_rx_mode(void *hwdev, u32 enable)
+{
+	struct hinic3_rx_mode_config rx_mode_cfg;
+	u16 out_size = sizeof(rx_mode_cfg);
+	int err;
+
+	if (!hwdev)
+		return -EINVAL;
+
+	memset(&rx_mode_cfg, 0, sizeof(rx_mode_cfg));
+	rx_mode_cfg.func_id = hinic3_global_func_id(hwdev);
+	rx_mode_cfg.rx_mode = enable;
+
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_SET_RX_MODE,
+				     &rx_mode_cfg, sizeof(rx_mode_cfg),
+				     &rx_mode_cfg, &out_size);
+	if (err || !out_size || rx_mode_cfg.msg_head.status) {
+		PMD_DRV_LOG(ERR,
+			    "Set rx mode failed, err: %d, status: 0x%x, out "
+			    "size: 0x%x",
+			    err, rx_mode_cfg.msg_head.status, out_size);
+		return -EIO;
+	}
+	return 0;
+}
+
+int
+hinic3_set_rx_vlan_offload(void *hwdev, u8 en)
+{
+	struct hinic3_cmd_vlan_offload vlan_cfg;
+	u16 out_size = sizeof(vlan_cfg);
+	int err;
+
+	if (!hwdev)
+		return -EINVAL;
+
+	memset(&vlan_cfg, 0, sizeof(vlan_cfg));
+	vlan_cfg.func_id = hinic3_global_func_id(hwdev);
+	vlan_cfg.vlan_offload = en;
+
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_SET_RX_VLAN_OFFLOAD,
+				     &vlan_cfg, sizeof(vlan_cfg), &vlan_cfg,
+				     &out_size);
+	if (err || !out_size || vlan_cfg.msg_head.status) {
+		PMD_DRV_LOG(ERR,
+			    "Set rx vlan offload failed, err: %d, status: "
+			    "0x%x, out size: 0x%x",
+			    err, vlan_cfg.msg_head.status, out_size);
+		return -EIO;
+	}
+	return 0;
+}
+
+int
+hinic3_set_vlan_filter(void *hwdev, u32 vlan_filter_ctrl)
+{
+	struct hinic3_cmd_set_vlan_filter vlan_filter;
+	u16 out_size = sizeof(vlan_filter);
+	int err;
+
+	if (!hwdev)
+		return -EINVAL;
+
+	memset(&vlan_filter, 0, sizeof(vlan_filter));
+	vlan_filter.func_id = hinic3_global_func_id(hwdev);
+	vlan_filter.vlan_filter_ctrl = vlan_filter_ctrl;
+
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_SET_VLAN_FILTER_EN,
+				     &vlan_filter, sizeof(vlan_filter),
+				     &vlan_filter, &out_size);
+	if (err || !out_size || vlan_filter.msg_head.status) {
+		PMD_DRV_LOG(ERR,
+			    "Failed to set vlan filter, err: %d, status: 0x%x, "
+			    "out size: 0x%x",
+			    err, vlan_filter.msg_head.status, out_size);
+		return -EIO;
+	}
+	return 0;
+}
+
+static int
+hinic3_set_rx_lro(void *hwdev, u8 ipv4_en, u8 ipv6_en, u8 lro_max_pkt_len)
+{
+	struct hinic3_cmd_lro_config lro_cfg = {0};
+	u16 out_size = sizeof(lro_cfg);
+	int err;
+
+	if (!hwdev)
+		return -EINVAL;
+
+	lro_cfg.func_id = hinic3_global_func_id(hwdev);
+	lro_cfg.opcode = HINIC3_CMD_OP_SET;
+	lro_cfg.lro_ipv4_en = ipv4_en;
+	lro_cfg.lro_ipv6_en = ipv6_en;
+	lro_cfg.lro_max_pkt_len = lro_max_pkt_len;
+
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_CFG_RX_LRO, &lro_cfg,
+				     sizeof(lro_cfg), &lro_cfg, &out_size);
+	if (err || !out_size || lro_cfg.msg_head.status) {
+		PMD_DRV_LOG(ERR,
+			    "Set lro offload failed, err: %d, status: 0x%x, "
+			    "out size: 0x%x",
+			    err, lro_cfg.msg_head.status, out_size);
+		return -EIO;
+	}
+
+	return 0;
+}
+
+static int
+hinic3_set_rx_lro_timer(void *hwdev, u32 timer_value)
+{
+	struct hinic3_cmd_lro_timer lro_timer;
+	u16 out_size = sizeof(lro_timer);
+	int err;
+
+	if (!hwdev)
+		return -EINVAL;
+
+	memset(&lro_timer, 0, sizeof(lro_timer));
+	lro_timer.opcode = HINIC3_CMD_OP_SET;
+	lro_timer.timer = timer_value;
+
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_CFG_LRO_TIMER,
+				     &lro_timer, sizeof(lro_timer), &lro_timer,
+				     &out_size);
+	if (err || !out_size || lro_timer.msg_head.status) {
+		PMD_DRV_LOG(ERR,
+			    "Set lro timer failed, err: %d, status: 0x%x, out "
+			    "size: 0x%x",
+			    err, lro_timer.msg_head.status, out_size);
+
+		return -EIO;
+	}
+
+	return 0;
+}
+
+int
+hinic3_set_rx_lro_state(void *hwdev, u8 lro_en, u32 lro_timer,
+			u32 lro_max_pkt_len)
+{
+	u8 ipv4_en = 0, ipv6_en = 0;
+	int err;
+
+	if (!hwdev)
+		return -EINVAL;
+
+	ipv4_en = lro_en ? 1 : 0;
+	ipv6_en = lro_en ? 1 : 0;
+
+	PMD_DRV_LOG(INFO, "Set LRO max coalesce packet size to %uK",
+		    lro_max_pkt_len);
+
+	err = hinic3_set_rx_lro(hwdev, ipv4_en, ipv6_en, (u8)lro_max_pkt_len);
+	if (err)
+		return err;
+
+	/* We don't set LRO timer for VF */
+	if (hinic3_func_type(hwdev) == TYPE_VF)
+		return 0;
+
+	PMD_DRV_LOG(INFO, "Set LRO timer to %u", lro_timer);
+
+	return hinic3_set_rx_lro_timer(hwdev, lro_timer);
+}
+
+/* RSS config */
+int
+hinic3_rss_template_alloc(void *hwdev)
+{
+	struct hinic3_rss_template_mgmt template_mgmt;
+	u16 out_size = sizeof(template_mgmt);
+	int err;
+
+	if (!hwdev)
+		return -EINVAL;
+
+	memset(&template_mgmt, 0, sizeof(struct hinic3_rss_template_mgmt));
+	template_mgmt.func_id = hinic3_global_func_id(hwdev);
+	template_mgmt.cmd = NIC_RSS_CMD_TEMP_ALLOC;
+
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_RSS_TEMP_MGR,
+				     &template_mgmt, sizeof(template_mgmt),
+				     &template_mgmt, &out_size);
+	if (err || !out_size || template_mgmt.msg_head.status) {
+		if (template_mgmt.msg_head.status ==
+		    HINIC3_MGMT_STATUS_TABLE_FULL) {
+			PMD_DRV_LOG(ERR, "There is no more template available");
+			return -ENOSPC;
+		}
+		PMD_DRV_LOG(ERR,
+			    "Alloc rss template failed, err: %d, "
+			    "status: 0x%x, out size: 0x%x",
+			    err, template_mgmt.msg_head.status, out_size);
+		return -EFAULT;
+	}
+
+	return 0;
+}
+
+int
+hinic3_rss_template_free(void *hwdev)
+{
+	struct hinic3_rss_template_mgmt template_mgmt;
+	u16 out_size = sizeof(template_mgmt);
+	int err;
+
+	if (!hwdev)
+		return -EINVAL;
+
+	memset(&template_mgmt, 0, sizeof(struct hinic3_rss_template_mgmt));
+	template_mgmt.func_id = hinic3_global_func_id(hwdev);
+	template_mgmt.cmd = NIC_RSS_CMD_TEMP_FREE;
+
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_RSS_TEMP_MGR,
+				     &template_mgmt, sizeof(template_mgmt),
+				     &template_mgmt, &out_size);
+	if (err || !out_size || template_mgmt.msg_head.status) {
+		PMD_DRV_LOG(ERR,
+			    "Free rss template failed, err: %d, "
+			    "status: 0x%x, out size: 0x%x",
+			    err, template_mgmt.msg_head.status, out_size);
+		return -EFAULT;
+	}
+
+	return 0;
+}
+
+static int
+hinic3_rss_cfg_hash_key(void *hwdev, u8 opcode, u8 *key, u16 key_size)
+{
+	struct hinic3_cmd_rss_hash_key hash_key;
+	u16 out_size = sizeof(hash_key);
+	int err;
+
+	if (!hwdev || !key)
+		return -EINVAL;
+
+	memset(&hash_key, 0, sizeof(struct hinic3_cmd_rss_hash_key));
+	hash_key.func_id = hinic3_global_func_id(hwdev);
+	hash_key.opcode = opcode;
+	if (opcode == HINIC3_CMD_OP_SET)
+		memcpy(hash_key.key, key, key_size);
+
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_CFG_RSS_HASH_KEY,
+				     &hash_key, sizeof(hash_key), &hash_key,
+				     &out_size);
+	if (err || !out_size || hash_key.msg_head.status) {
+		PMD_DRV_LOG(ERR,
+			    "%s hash key failed, err: %d, "
+			    "status: 0x%x, out size: 0x%x",
+			    opcode == HINIC3_CMD_OP_SET ? "Set" : "Get", err,
+			    hash_key.msg_head.status, out_size);
+		return -EFAULT;
+	}
+
+	if (opcode == HINIC3_CMD_OP_GET)
+		memcpy(key, hash_key.key, key_size);
+
+	return 0;
+}
+
+int
+hinic3_rss_set_hash_key(void *hwdev, u8 *key, u16 key_size)
+{
+	if (!hwdev || !key)
+		return -EINVAL;
+
+	return hinic3_rss_cfg_hash_key(hwdev, HINIC3_CMD_OP_SET, key, key_size);
+}
+
+int
+hinic3_rss_get_indir_tbl(void *hwdev, u32 *indir_table, u32 indir_table_size)
+{
+	struct hinic3_cmd_buf *cmd_buf = NULL;
+	u16 *indir_tbl = NULL;
+	int err;
+	u32 i;
+
+	if (!hwdev || !indir_table)
+		return -EINVAL;
+
+	cmd_buf = hinic3_alloc_cmd_buf(hwdev);
+	if (!cmd_buf) {
+		PMD_DRV_LOG(ERR, "Allocate cmd buf failed");
+		return -ENOMEM;
+	}
+
+	cmd_buf->size = sizeof(struct nic_rss_indirect_tbl);
+	err = hinic3_cmdq_detail_resp(hwdev, HINIC3_MOD_L2NIC,
+				      HINIC3_UCODE_CMD_GET_RSS_INDIR_TABLE,
+				      cmd_buf, cmd_buf, 0);
+	if (err) {
+		PMD_DRV_LOG(ERR, "Get rss indir table failed");
+		hinic3_free_cmd_buf(cmd_buf);
+		return err;
+	}
+
+	indir_tbl = (u16 *)cmd_buf->buf;
+	for (i = 0; i < indir_table_size; i++)
+		indir_table[i] = *(indir_tbl + i);
+
+	hinic3_free_cmd_buf(cmd_buf);
+	return 0;
+}
+
+int
+hinic3_rss_set_indir_tbl(void *hwdev, const u32 *indir_table,
+			 u32 indir_table_size)
+{
+	struct nic_rss_indirect_tbl *indir_tbl = NULL;
+	struct hinic3_cmd_buf *cmd_buf = NULL;
+	u32 i, size;
+	u32 *temp = NULL;
+	u64 out_param = 0;
+	int err;
+
+	if (!hwdev || !indir_table)
+		return -EINVAL;
+
+	cmd_buf = hinic3_alloc_cmd_buf(hwdev);
+	if (!cmd_buf) {
+		PMD_DRV_LOG(ERR, "Allocate cmd buf failed");
+		return -ENOMEM;
+	}
+
+	cmd_buf->size = sizeof(struct nic_rss_indirect_tbl);
+	indir_tbl = (struct nic_rss_indirect_tbl *)cmd_buf->buf;
+	memset(indir_tbl, 0, sizeof(*indir_tbl));
+
+	for (i = 0; i < indir_table_size; i++)
+		indir_tbl->entry[i] = (u16)(*(indir_table + i));
+
+	rte_atomic_thread_fence(rte_memory_order_seq_cst);
+	size = sizeof(indir_tbl->entry) / sizeof(u16);
+	temp = (u32 *)indir_tbl->entry;
+	for (i = 0; i < size; i++)
+		temp[i] = cpu_to_be32(temp[i]);
+
+	err = hinic3_cmdq_direct_resp(hwdev, HINIC3_MOD_L2NIC,
+				      HINIC3_UCODE_CMD_SET_RSS_INDIR_TABLE,
+				      cmd_buf, &out_param, 0);
+	if (err || out_param != 0) {
+		PMD_DRV_LOG(ERR, "Set rss indir table failed");
+		err = -EFAULT;
+	}
+
+	hinic3_free_cmd_buf(cmd_buf);
+	return err;
+}
+
+static int
+hinic3_cmdq_set_rss_type(void *hwdev, struct hinic3_rss_type rss_type)
+{
+	struct nic_rss_context_tbl *ctx_tbl = NULL;
+	struct hinic3_cmd_buf *cmd_buf = NULL;
+	u32 ctx = 0;
+	u64 out_param = 0;
+	int err;
+
+	if (!hwdev)
+		return -EINVAL;
+
+	cmd_buf = hinic3_alloc_cmd_buf(hwdev);
+	if (!cmd_buf) {
+		PMD_DRV_LOG(ERR, "Allocate cmd buf failed");
+		return -ENOMEM;
+	}
+
+	ctx |= HINIC3_RSS_TYPE_SET(1, VALID) |
+	       HINIC3_RSS_TYPE_SET(rss_type.ipv4, IPV4) |
+	       HINIC3_RSS_TYPE_SET(rss_type.ipv6, IPV6) |
+	       HINIC3_RSS_TYPE_SET(rss_type.ipv6_ext, IPV6_EXT) |
+	       HINIC3_RSS_TYPE_SET(rss_type.tcp_ipv4, TCP_IPV4) |
+	       HINIC3_RSS_TYPE_SET(rss_type.tcp_ipv6, TCP_IPV6) |
+	       HINIC3_RSS_TYPE_SET(rss_type.tcp_ipv6_ext, TCP_IPV6_EXT) |
+	       HINIC3_RSS_TYPE_SET(rss_type.udp_ipv4, UDP_IPV4) |
+	       HINIC3_RSS_TYPE_SET(rss_type.udp_ipv6, UDP_IPV6);
+
+	cmd_buf->size = sizeof(struct nic_rss_context_tbl);
+	ctx_tbl = (struct nic_rss_context_tbl *)cmd_buf->buf;
+	memset(ctx_tbl, 0, sizeof(*ctx_tbl));
+	rte_atomic_thread_fence(rte_memory_order_seq_cst);
+	ctx_tbl->ctx = cpu_to_be32(ctx);
+
+	/* Cfg the RSS context table by command queue. */
+	err = hinic3_cmdq_direct_resp(hwdev, HINIC3_MOD_L2NIC,
+				      HINIC3_UCODE_CMD_SET_RSS_CONTEXT_TABLE,
+				      cmd_buf, &out_param, 0);
+
+	hinic3_free_cmd_buf(cmd_buf);
+
+	if (err || out_param != 0) {
+		PMD_DRV_LOG(ERR, "Cmdq set rss context table failed, err: %d",
+			    err);
+		return -EFAULT;
+	}
+
+	return 0;
+}
+
+static int
+hinic3_mgmt_set_rss_type(void *hwdev, struct hinic3_rss_type rss_type)
+{
+	struct hinic3_rss_context_table ctx_tbl;
+	u32 ctx = 0;
+	u16 out_size = sizeof(ctx_tbl);
+	int err;
+
+	if (!hwdev)
+		return -EINVAL;
+
+	memset(&ctx_tbl, 0, sizeof(ctx_tbl));
+	ctx_tbl.func_id = hinic3_global_func_id(hwdev);
+	ctx |= HINIC3_RSS_TYPE_SET(1, VALID) |
+	       HINIC3_RSS_TYPE_SET(rss_type.ipv4, IPV4) |
+	       HINIC3_RSS_TYPE_SET(rss_type.ipv6, IPV6) |
+	       HINIC3_RSS_TYPE_SET(rss_type.ipv6_ext, IPV6_EXT) |
+	       HINIC3_RSS_TYPE_SET(rss_type.tcp_ipv4, TCP_IPV4) |
+	       HINIC3_RSS_TYPE_SET(rss_type.tcp_ipv6, TCP_IPV6) |
+	       HINIC3_RSS_TYPE_SET(rss_type.tcp_ipv6_ext, TCP_IPV6_EXT) |
+	       HINIC3_RSS_TYPE_SET(rss_type.udp_ipv4, UDP_IPV4) |
+	       HINIC3_RSS_TYPE_SET(rss_type.udp_ipv6, UDP_IPV6);
+	ctx_tbl.context = ctx;
+
+	err = l2nic_msg_to_mgmt_sync(hwdev,
+		HINIC3_NIC_CMD_SET_RSS_CTX_TBL_INTO_FUNC, &ctx_tbl,
+		sizeof(ctx_tbl), &ctx_tbl, &out_size);
+	if (ctx_tbl.msg_head.status == HINIC3_MGMT_CMD_UNSUPPORTED) {
+		return HINIC3_MGMT_CMD_UNSUPPORTED;
+	} else if (err || !out_size || ctx_tbl.msg_head.status) {
+		PMD_DRV_LOG(ERR,
+			    "Mgmt set rss context table failed, "
+			    "err: %d, status: 0x%x, out size: 0x%x",
+			    err, ctx_tbl.msg_head.status, out_size);
+		return -EINVAL;
+	}
+	return 0;
+}
+
+int
+hinic3_set_rss_type(void *hwdev, struct hinic3_rss_type rss_type)
+{
+	int err;
+	err = hinic3_mgmt_set_rss_type(hwdev, rss_type);
+	if (err == HINIC3_MGMT_CMD_UNSUPPORTED)
+		err = hinic3_cmdq_set_rss_type(hwdev, rss_type);
+	return err;
+}
+
+int
+hinic3_get_rss_type(void *hwdev, struct hinic3_rss_type *rss_type)
+{
+	struct hinic3_rss_context_table ctx_tbl;
+	u16 out_size = sizeof(ctx_tbl);
+	int err;
+
+	if (!hwdev || !rss_type)
+		return -EINVAL;
+
+	memset(&ctx_tbl, 0, sizeof(struct hinic3_rss_context_table));
+	ctx_tbl.func_id = hinic3_global_func_id(hwdev);
+
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_GET_RSS_CTX_TBL,
+				     &ctx_tbl, sizeof(ctx_tbl), &ctx_tbl,
+				     &out_size);
+	if (err || !out_size || ctx_tbl.msg_head.status) {
+		PMD_DRV_LOG(ERR,
+			    "Get hash type failed, "
+			    "err: %d, status: 0x%x, out size: 0x%x",
+			    err, ctx_tbl.msg_head.status, out_size);
+		return -EFAULT;
+	}
+
+	rss_type->ipv4 = HINIC3_RSS_TYPE_GET(ctx_tbl.context, IPV4);
+	rss_type->ipv6 = HINIC3_RSS_TYPE_GET(ctx_tbl.context, IPV6);
+	rss_type->ipv6_ext = HINIC3_RSS_TYPE_GET(ctx_tbl.context, IPV6_EXT);
+	rss_type->tcp_ipv4 = HINIC3_RSS_TYPE_GET(ctx_tbl.context, TCP_IPV4);
+	rss_type->tcp_ipv6 = HINIC3_RSS_TYPE_GET(ctx_tbl.context, TCP_IPV6);
+	rss_type->tcp_ipv6_ext =
+		HINIC3_RSS_TYPE_GET(ctx_tbl.context, TCP_IPV6_EXT);
+	rss_type->udp_ipv4 = HINIC3_RSS_TYPE_GET(ctx_tbl.context, UDP_IPV4);
+	rss_type->udp_ipv6 = HINIC3_RSS_TYPE_GET(ctx_tbl.context, UDP_IPV6);
+
+	return 0;
+}
+
+static int
+hinic3_rss_cfg_hash_engine(void *hwdev, u8 opcode, u8 *type)
+{
+	struct hinic3_cmd_rss_engine_type hash_type;
+	u16 out_size = sizeof(hash_type);
+	int err;
+
+	if (!hwdev || !type)
+		return -EINVAL;
+
+	memset(&hash_type, 0, sizeof(struct hinic3_cmd_rss_engine_type));
+	hash_type.func_id = hinic3_global_func_id(hwdev);
+	hash_type.opcode = opcode;
+	if (opcode == HINIC3_CMD_OP_SET)
+		hash_type.hash_engine = *type;
+
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_CFG_RSS_HASH_ENGINE,
+				     &hash_type, sizeof(hash_type), &hash_type,
+				     &out_size);
+	if (err || !out_size || hash_type.msg_head.status) {
+		PMD_DRV_LOG(ERR,
+			    "%s hash engine failed, "
+			    "err: %d, status: 0x%x, out size: 0x%x",
+			    opcode == HINIC3_CMD_OP_SET ? "Set" : "Get", err,
+			    hash_type.msg_head.status, out_size);
+		return -EFAULT;
+	}
+
+	if (opcode == HINIC3_CMD_OP_GET)
+		*type = hash_type.hash_engine;
+
+	return 0;
+}
+
+int
+hinic3_rss_get_hash_engine(void *hwdev, u8 *type)
+{
+	if (!hwdev || !type)
+		return -EINVAL;
+
+	return hinic3_rss_cfg_hash_engine(hwdev, HINIC3_CMD_OP_GET, type);
+}
+
+int
+hinic3_rss_set_hash_engine(void *hwdev, u8 type)
+{
+	if (!hwdev)
+		return -EINVAL;
+
+	return hinic3_rss_cfg_hash_engine(hwdev, HINIC3_CMD_OP_SET, &type);
+}
+
+int
+hinic3_rss_cfg(void *hwdev, u8 rss_en, u8 tc_num, u8 *prio_tc)
+{
+	struct hinic3_cmd_rss_config rss_cfg;
+	u16 out_size = sizeof(rss_cfg);
+	int err;
+
+	/* Ucode requires number of TC should be power of 2. */
+	if (!hwdev || !prio_tc || (tc_num & (tc_num - 1)))
+		return -EINVAL;
+
+	memset(&rss_cfg, 0, sizeof(struct hinic3_cmd_rss_config));
+	rss_cfg.func_id = hinic3_global_func_id(hwdev);
+	rss_cfg.rss_en = rss_en;
+	rss_cfg.rq_priority_number = tc_num ? (u8)ilog2(tc_num) : 0;
+
+	memcpy(rss_cfg.prio_tc, prio_tc, HINIC3_DCB_UP_MAX);
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_RSS_CFG, &rss_cfg,
+				     sizeof(rss_cfg), &rss_cfg, &out_size);
+	if (err || !out_size || rss_cfg.msg_head.status) {
+		PMD_DRV_LOG(ERR,
+			    "Set rss cfg failed, err: %d, "
+			    "status: 0x%x, out size: 0x%x",
+			    err, rss_cfg.msg_head.status, out_size);
+		return -EFAULT;
+	}
+
+	return 0;
+}
+
+int
+hinic3_vf_get_default_cos(void *hwdev, u8 *cos_id)
+{
+	struct hinic3_cmd_vf_dcb_state vf_dcb;
+	u16 out_size = sizeof(vf_dcb);
+	int err;
+
+	memset(&vf_dcb, 0, sizeof(vf_dcb));
+
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_VF_COS, &vf_dcb,
+				     sizeof(vf_dcb), &vf_dcb, &out_size);
+	if (err || !out_size || vf_dcb.msg_head.status) {
+		PMD_DRV_LOG(ERR,
+			    "Get VF default cos failed, "
+			    "err: %d, status: 0x%x, out size: 0x%x",
+			    err, vf_dcb.msg_head.status, out_size);
+		return -EIO;
+	}
+
+	*cos_id = vf_dcb.state.default_cos;
+
+	return 0;
+}
+
+/**
+ * Set the Ethernet type filtering rule for the FDIR of a NIC.
+ *
+ * @param[in] hwdev
+ * Pointer to hardware device structure.
+ * @param[in] pkt_type
+ * Indicate the packet type.
+ * @param[in] queue_id
+ * Indicate the queue id.
+ * @param[in] en
+ * Indicate whether to add or delete an operation. 1 - add; 0 - delete.
+ *
+ * @return
+ * 0 on success, non-zero on failure.
+ */
+int
+hinic3_set_fdir_ethertype_filter(void *hwdev, u8 pkt_type, u16 queue_id, u8 en)
+{
+	struct hinic3_set_fdir_ethertype_rule ethertype_cmd;
+	u16 out_size = sizeof(ethertype_cmd);
+	int err;
+
+	if (!hwdev)
+		return -EINVAL;
+
+	memset(&ethertype_cmd, 0,
+	       sizeof(struct hinic3_set_fdir_ethertype_rule));
+	ethertype_cmd.func_id = hinic3_global_func_id(hwdev);
+	ethertype_cmd.pkt_type = pkt_type;
+	ethertype_cmd.pkt_type_en = en;
+	ethertype_cmd.qid = (u8)queue_id;
+
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_SET_FDIR_STATUS,
+				     &ethertype_cmd, sizeof(ethertype_cmd),
+				     &ethertype_cmd, &out_size);
+	if (err || ethertype_cmd.head.status || !out_size) {
+		PMD_DRV_LOG(ERR,
+			    "set fdir ethertype rule failed, "
+			    "err: %d, status: 0x%x, out size: 0x%x, func_id %d",
+			    err, ethertype_cmd.head.status, out_size,
+			    ethertype_cmd.func_id);
+		return -EIO;
+	}
+
+	return 0;
+}
+
+int
+hinic3_add_tcam_rule(void *hwdev, struct hinic3_tcam_cfg_rule *tcam_rule,
+		     u8 tcam_rule_type)
+{
+	struct hinic3_fdir_add_rule tcam_cmd;
+	u16 out_size = sizeof(tcam_cmd);
+	int err;
+
+	if (!hwdev || !tcam_rule)
+		return -EINVAL;
+	/* Check whether the index is out of range. */
+	if (tcam_rule->index >= HINIC3_MAX_TCAM_RULES_NUM) {
+		PMD_DRV_LOG(ERR, "Tcam rules num to add is invalid");
+		return -EINVAL;
+	}
+
+	memset(&tcam_cmd, 0, sizeof(struct hinic3_fdir_add_rule));
+	tcam_cmd.func_id = hinic3_global_func_id(hwdev);
+	memcpy((void *)&tcam_cmd.rule, (void *)tcam_rule,
+	       sizeof(struct hinic3_tcam_cfg_rule));
+	tcam_cmd.type = tcam_rule_type;
+
+	/* Synchronize the information to the management module. */
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_ADD_TC_FLOW,
+				     &tcam_cmd, sizeof(tcam_cmd), &tcam_cmd,
+				     &out_size);
+	if (err || tcam_cmd.msg_head.status || !out_size) {
+		PMD_DRV_LOG(ERR,
+			    "Add tcam rule failed, "
+			    "err: %d, status: 0x%x, out size: 0x%x",
+			    err, tcam_cmd.msg_head.status, out_size);
+		return -EIO;
+	}
+
+	return 0;
+}
+
+int
+hinic3_del_tcam_rule(void *hwdev, u32 index, u8 tcam_rule_type)
+{
+	struct hinic3_fdir_del_rule tcam_cmd;
+	u16 out_size = sizeof(tcam_cmd);
+	int err;
+
+	if (!hwdev)
+		return -EINVAL;
+	/* Check whether the index is out of range. */
+	if (index >= HINIC3_MAX_TCAM_RULES_NUM) {
+		PMD_DRV_LOG(ERR, "Tcam rules num to del is invalid");
+		return -EINVAL;
+	}
+
+	memset(&tcam_cmd, 0, sizeof(struct hinic3_fdir_del_rule));
+	tcam_cmd.func_id = hinic3_global_func_id(hwdev);
+	tcam_cmd.index_start = index;
+	tcam_cmd.index_num = 1;
+	tcam_cmd.type = tcam_rule_type;
+
+	/* Synchronize the information to the management module. */
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_DEL_TC_FLOW,
+				     &tcam_cmd, sizeof(tcam_cmd), &tcam_cmd,
+				     &out_size);
+	if (err || tcam_cmd.msg_head.status || !out_size) {
+		PMD_DRV_LOG(ERR,
+			    "Del tcam rule failed, "
+			    "err: %d, status: 0x%x, out size: 0x%x",
+			    err, tcam_cmd.msg_head.status, out_size);
+		return -EIO;
+	}
+
+	return 0;
+}
+
+static int
+hinic3_cfg_tcam_block(void *hwdev, u8 alloc_en, u16 *index)
+{
+	struct hinic3_tcam_block tcam_block_info;
+	u16 out_size = sizeof(tcam_block_info);
+	int err;
+
+	if (!hwdev)
+		return -EINVAL;
+	/* Initialization TCAM block structure. */
+	memset(&tcam_block_info, 0, sizeof(struct hinic3_tcam_block));
+	tcam_block_info.func_id = hinic3_global_func_id(hwdev);
+	tcam_block_info.alloc_en = alloc_en;
+	tcam_block_info.tcam_type = HINIC3_TCAM_BLOCK_NORMAL_TYPE;
+	tcam_block_info.tcam_block_index = *index;
+
+	/* Synchronize the information to the management module. */
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_CFG_TCAM_BLOCK,
+				     &tcam_block_info, sizeof(tcam_block_info),
+				     &tcam_block_info, &out_size);
+	if (err || !out_size || tcam_block_info.msg_head.status) {
+		PMD_DRV_LOG(ERR,
+			    "Set tcam block failed, err: %d, status: 0x%x, out "
+			    "size: 0x%x",
+			    err, tcam_block_info.msg_head.status, out_size);
+		return -EIO;
+	}
+
+	/* Update TCAM block of index. */
+	if (alloc_en)
+		*index = tcam_block_info.tcam_block_index;
+
+	return 0;
+}
+
+int
+hinic3_alloc_tcam_block(void *hwdev, u16 *index)
+{
+	return hinic3_cfg_tcam_block(hwdev, HINIC3_TCAM_BLOCK_ENABLE, index);
+}
+
+int
+hinic3_free_tcam_block(void *hwdev, u16 *index)
+{
+	return hinic3_cfg_tcam_block(hwdev, HINIC3_TCAM_BLOCK_DISABLE, index);
+}
+
+int
+hinic3_flush_tcam_rule(void *hwdev)
+{
+	struct hinic3_flush_tcam_rules tcam_flush;
+	u16 out_size = sizeof(tcam_flush);
+	int err;
+
+	if (!hwdev)
+		return -EINVAL;
+
+	memset(&tcam_flush, 0, sizeof(struct hinic3_flush_tcam_rules));
+	tcam_flush.func_id = hinic3_global_func_id(hwdev);
+
+	err = l2nic_msg_to_mgmt_sync(hwdev,
+		HINIC3_NIC_CMD_FLUSH_TCAM, &tcam_flush,
+		sizeof(struct hinic3_flush_tcam_rules), &tcam_flush, &out_size);
+	if (tcam_flush.msg_head.status == HINIC3_MGMT_CMD_UNSUPPORTED) {
+		err = HINIC3_MGMT_CMD_UNSUPPORTED;
+		PMD_DRV_LOG(INFO,
+			    "Firmware/uP doesn't support flush tcam fdir");
+	} else if (err || (!out_size) || tcam_flush.msg_head.status) {
+		PMD_DRV_LOG(ERR,
+			    "Flush tcam fdir rules failed, err: %d, status: "
+			    "0x%x, out size: 0x%x",
+			    err, tcam_flush.msg_head.status, out_size);
+		err = -EIO;
+	}
+
+	return err;
+}
+
+int
+hinic3_set_fdir_tcam_rule_filter(void *hwdev, bool enable)
+{
+	struct hinic3_port_tcam_info port_tcam_cmd;
+	u16 out_size = sizeof(port_tcam_cmd);
+	int err;
+
+	if (!hwdev)
+		return -EINVAL;
+	/* Initialization information. */
+	memset(&port_tcam_cmd, 0, sizeof(port_tcam_cmd));
+	port_tcam_cmd.func_id = hinic3_global_func_id(hwdev);
+	port_tcam_cmd.tcam_enable = (u8)enable;
+
+	/* Synchronize the information to the management module. */
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_ENABLE_TCAM,
+				     &port_tcam_cmd, sizeof(port_tcam_cmd),
+				     &port_tcam_cmd, &out_size);
+	if ((port_tcam_cmd.msg_head.status != HINIC3_MGMT_CMD_UNSUPPORTED &&
+	     port_tcam_cmd.msg_head.status) ||
+	    err || !out_size) {
+		PMD_DRV_LOG(ERR,
+			    "Set fdir tcam filter failed, err: %d, "
+			    "status: 0x%x, out size: 0x%x, enable: 0x%x",
+			    err, port_tcam_cmd.msg_head.status, out_size,
+			    enable);
+		return -EIO;
+	}
+
+	if (port_tcam_cmd.msg_head.status == HINIC3_MGMT_CMD_UNSUPPORTED) {
+		err = HINIC3_MGMT_CMD_UNSUPPORTED;
+		PMD_DRV_LOG(WARNING,
+			    "Fw doesn't support setting fdir tcam filter");
+	}
+
+	return err;
+}
+
+int
+hinic3_set_rq_flush(void *hwdev, u16 q_id)
+{
+	struct hinic3_cmd_set_rq_flush *rq_flush_msg = NULL;
+	struct hinic3_cmd_buf *cmd_buf = NULL;
+	u64 out_param = EIO;
+	int err;
+
+	cmd_buf = hinic3_alloc_cmd_buf(hwdev);
+	if (!cmd_buf) {
+		PMD_DRV_LOG(ERR, "Failed to allocate cmd buf");
+		return -ENOMEM;
+	}
+
+	cmd_buf->size = sizeof(*rq_flush_msg);
+
+	rq_flush_msg = cmd_buf->buf;
+	rq_flush_msg->local_rq_id = q_id;
+	rte_atomic_thread_fence(rte_memory_order_seq_cst);
+	rq_flush_msg->value = cpu_to_be32(rq_flush_msg->value);
+
+	err = hinic3_cmdq_direct_resp(hwdev, HINIC3_MOD_L2NIC,
+				      HINIC3_UCODE_CMD_SET_RQ_FLUSH, cmd_buf,
+				      &out_param, 0);
+	if (err || out_param != 0) {
+		PMD_DRV_LOG(ERR,
+			"Failed to set rq flush, err:%d, out_param:0x%" PRIx64,
+			err, out_param);
+		err = -EFAULT;
+	}
+
+	hinic3_free_cmd_buf(cmd_buf);
+
+	return err;
+}
+
+static int
+_mag_msg_to_mgmt_sync(void *hwdev, u16 cmd, void *buf_in, u16 in_size,
+		      void *buf_out, u16 *out_size)
+{
+	u32 i, cmd_cnt = ARRAY_LEN(vf_mag_cmd_handler);
+
+	if (hinic3_func_type(hwdev) == TYPE_VF) {
+		for (i = 0; i < cmd_cnt; i++) {
+			if (cmd == vf_mag_cmd_handler[i].cmd)
+				return hinic3_mbox_to_pf(hwdev,
+					HINIC3_MOD_HILINK, cmd, buf_in,
+					in_size, buf_out, out_size, 0);
+		}
+	}
+
+	return hinic3_msg_to_mgmt_sync(hwdev, HINIC3_MOD_HILINK, cmd, buf_in,
+				       in_size, buf_out, out_size, 0);
+}
+
+static int
+mag_msg_to_mgmt_sync(void *hwdev, u16 cmd, void *buf_in, u16 in_size,
+		     void *buf_out, u16 *out_size)
+{
+	return _mag_msg_to_mgmt_sync(hwdev, cmd, buf_in, in_size, buf_out,
+				     out_size);
+}
+
+int
+hinic3_set_link_status_follow(void *hwdev,
+			      enum hinic3_link_follow_status status)
+{
+	struct mag_cmd_set_link_follow follow;
+	u16 out_size = sizeof(follow);
+	int err;
+
+	if (!hwdev)
+		return -EINVAL;
+
+	if (status >= HINIC3_LINK_FOLLOW_STATUS_MAX) {
+		PMD_DRV_LOG(ERR, "Invalid link follow status: %d", status);
+		return -EINVAL;
+	}
+
+	memset(&follow, 0, sizeof(follow));
+	follow.function_id = hinic3_global_func_id(hwdev);
+	follow.follow = status;
+
+	err = mag_msg_to_mgmt_sync(hwdev, MAG_CMD_SET_LINK_FOLLOW, &follow,
+				   sizeof(follow), &follow, &out_size);
+	if ((follow.head.status != HINIC3_MGMT_CMD_UNSUPPORTED &&
+	     follow.head.status) ||
+	    err || !out_size) {
+		PMD_DRV_LOG(ERR,
+			    "Failed to set link status follow port status, "
+			    "err: %d, status: 0x%x, out size: 0x%x",
+			    err, follow.head.status, out_size);
+		return -EFAULT;
+	}
+
+	return follow.head.status;
+}
drivers/net/hinic3/base/hinic3_nic_cfg.h new file +1527 -0

1,172 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.45.1.windows.1

Part of the diff is not shown here. View the raw message.

#143 lore raw

Re: [V5 09/18] net/hinic3: add a NIC business configuration module

Quoted 338 lines · Feifei Wang

From: Yi Chen <[email protected]>

The items of configurations and queries for NIC business include

MAC, VLAN, MTU, RSS and so on. These configurations and queries

are handled by mgmt module. This patch introduces related

data structures and function codes.

---

 drivers/net/hinic3/base/hinic3_nic_cfg.c | 1828 ++++++++++++++++++++++
 drivers/net/hinic3/base/hinic3_nic_cfg.h | 1527 ++++++++++++++++++
 2 files changed, 3355 insertions(+)
 create mode 100644 drivers/net/hinic3/base/hinic3_nic_cfg.c
 create mode 100644 drivers/net/hinic3/base/hinic3_nic_cfg.h
drivers/net/hinic3/base/hinic3_nic_cfg.c +0 -0

new file mode 100644

index 0000000000..918ebc37e9

--- /dev/null

+++ b/drivers/net/hinic3/base/hinic3_nic_cfg.c

drivers/net/hinic3/base/hinic3_nic_cfg.c +147 -0
@@ -0,0 +1,1828 @@
 
+/* SPDX-License-Identifier: BSD-3-Clause
 
+ * Copyright(c) 2025 Huawei Technologies Co., Ltd
 
+ */
 
+
 
+#include <rte_atomic.h>
 
+#include <rte_ether.h>
 
+
 
+#include "hinic3_compat.h"
 
+#include "hinic3_cmd.h"
 
+#include "hinic3_cmdq.h"
 
+#include "hinic3_hw_cfg.h"
 
+#include "hinic3_hwdev.h"
 
+#include "hinic3_hwif.h"
 
+#include "hinic3_mbox.h"
 
+#include "hinic3_nic_cfg.h"
 
+#include "hinic3_wq.h"
 
+
 
+struct vf_msg_handler {
 
+	u16 cmd;
 
+};
 
+
 
+static const struct vf_msg_handler vf_cmd_handler[] = {
 
+	{
 
+		.cmd = HINIC3_NIC_CMD_VF_REGISTER,
 
+	},
 
+
 
+	{
 
+		.cmd = HINIC3_NIC_CMD_GET_MAC,
 
+	},
 
+
 
+	{
 
+		.cmd = HINIC3_NIC_CMD_SET_MAC,
 
+	},
 
+
 
+	{
 
+		.cmd = HINIC3_NIC_CMD_DEL_MAC,
 
+	},
 
+
 
+	{
 
+		.cmd = HINIC3_NIC_CMD_UPDATE_MAC,
 
+	},
 
+
 
+	{
 
+		.cmd = HINIC3_NIC_CMD_VF_COS,
 
+	},
 
+};
 
+
 
+static const struct vf_msg_handler vf_mag_cmd_handler[] = {
 
+	{
 
+		.cmd = MAG_CMD_GET_LINK_STATUS,
 
+	},
 
+};
 
+
 
+static int mag_msg_to_mgmt_sync(void *hwdev, u16 cmd, void *buf_in, u16 in_size,
 
+				void *buf_out, u16 *out_size);
 
+
 
+/**
 
+ * If device is VF and command is found in a predefined list, send to PF, else
 
+ * send to management module.
 
+ *
 
+ * @param[in] hwdev
 
+ * The pointer to the hardware device structure.
 
+ * @param[in] cmd
 
+ * The command to send.
 
+ * @param[in] buf_in
 
+ * The input buffer containing the request data.
 
+ * @param[in] in_size
 
+ * The size of the input buffer.
 
+ * @param[out] buf_out
 
+ * The output buffer to receive the response data.
 
+ * @param[out] out_size
 
+ * The size of the output buffer on return.
 
+ *
 
+ * @return
 
+ * 0 on success, non-zero on failure.
 
+ */
 
+int
 
+l2nic_msg_to_mgmt_sync(void *hwdev, u16 cmd, void *buf_in, u16 in_size,
 
+		       void *buf_out, u16 *out_size)
 
+{
 
+	u32 i, cmd_cnt = ARRAY_LEN(vf_cmd_handler);
 
+	bool cmd_to_pf = false;
 
+
 
+	if (hinic3_func_type(hwdev) == TYPE_VF) {
 
+		for (i = 0; i < cmd_cnt; i++) {
 
+			if (cmd == vf_cmd_handler[i].cmd)
 
+				cmd_to_pf = true;
 
+		}
 
+	}
 
+
 
+	if (cmd_to_pf) {
 
+		return hinic3_mbox_to_pf(hwdev, HINIC3_MOD_L2NIC, cmd, buf_in,
 
+					 in_size, buf_out, out_size, 0);
 
+	}
 
+
 
+	return hinic3_msg_to_mgmt_sync(hwdev, HINIC3_MOD_L2NIC, cmd, buf_in,
 
+				       in_size, buf_out, out_size, 0);
 
+}
 
+
 
+/**
 
+ * Set CI table for a SQ.
 
+ *
 
+ * Configure the CI table with attributes like CI address, pending limit,
 
+ * coalescing time, and optional interrupt settings for specified SQ.
 
+ *
 
+ * @param[in] hwdev
 
+ * Pointer to hardware device structure.
 
+ * @param[in] attr
 
+ * Attributes to configure for CI table.
 
+ *
 
+ * @return
 
+ * 0 on success, non-zero on failure.
 
+ */
 
+int
 
+hinic3_set_ci_table(void *hwdev, struct hinic3_sq_attr *attr)
 
+{
 
+	struct hinic3_cmd_cons_idx_attr cons_idx_attr;
 
+	u16 out_size = sizeof(cons_idx_attr);
 
+	int err;
 
+
 
+	if (!hwdev || !attr)
 
+		return -EINVAL;
 
+
 
+	memset(&cons_idx_attr, 0, sizeof(cons_idx_attr));
 
+	cons_idx_attr.func_idx = hinic3_global_func_id(hwdev);
 
+	cons_idx_attr.dma_attr_off = attr->dma_attr_off;
 
+	cons_idx_attr.pending_limit = attr->pending_limit;
 
+	cons_idx_attr.coalescing_time = attr->coalescing_time;
 
+
 
+	if (attr->intr_en) {
 
+		cons_idx_attr.intr_en = attr->intr_en;
 
+		cons_idx_attr.intr_idx = attr->intr_idx;
 
+	}
 
+
 
+	cons_idx_attr.l2nic_sqn = attr->l2nic_sqn;
 
+	cons_idx_attr.ci_addr = attr->ci_dma_base;
 
+
 
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_SQ_CI_ATTR_SET,
 
+				     &cons_idx_attr, sizeof(cons_idx_attr),
 
+				     &cons_idx_attr, &out_size);
 
+	if (err || !out_size || cons_idx_attr.msg_head.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Set ci attribute table failed, "
 
+			    "err: %d, status: 0x%x, out_size: 0x%x",
 
+			    err, cons_idx_attr.msg_head.status, out_size);
 
+		return -EFAULT;
 
+	}

drivers/net/hinic3/base/hinic3_nic_cfg.c line 152

Quoted 10 lines
drivers/net/hinic3/base/hinic3_nic_cfg.c +5 -0
 
+
 
+	return 0;
 
+}
 
+
 
+#define PF_SET_VF_MAC(hwdev, status)           \

Maybe we need a more readable name?

Quoted 84 lines
drivers/net/hinic3/base/hinic3_nic_cfg.c +42 -0
+	(hinic3_func_type(hwdev) == TYPE_VF && \
 
+	 (status) == HINIC3_PF_SET_VF_ALREADY)
 
+
 
+static int
 
+hinic3_check_mac_info(void *hwdev, u8 status, u16 vlan_id)
 
+{
 
+	if ((status && status != HINIC3_MGMT_STATUS_EXIST) ||
 
+	    ((vlan_id & CHECK_IPSU_15BIT) &&
 
+	     status == HINIC3_MGMT_STATUS_EXIST)) {
 
+		if (PF_SET_VF_MAC(hwdev, status))
 
+			return 0;
 
+
 
+		return -EINVAL;
 
+	}
 
+
 
+	return 0;
 
+}
 
+
 
+#define VLAN_N_VID 4096
 
+
 
+int
 
+hinic3_set_mac(void *hwdev, const u8 *mac_addr, u16 vlan_id, u16 func_id)
 
+{
 
+	struct hinic3_port_mac_set mac_info;
 
+	u16 out_size = sizeof(mac_info);
 
+	int err;
 
+
 
+	if (!hwdev || !mac_addr)
 
+		return -EINVAL;
 
+
 
+	memset(&mac_info, 0, sizeof(mac_info));
 
+
 
+	if (vlan_id >= VLAN_N_VID) {
 
+		PMD_DRV_LOG(ERR, "Invalid VLAN number: %d", vlan_id);
 
+		return -EINVAL;
 
+	}
 
+
 
+	mac_info.func_id = func_id;
 
+	mac_info.vlan_id = vlan_id;
 
+	memmove(mac_info.mac, mac_addr, ETH_ALEN);
 
+
 
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_SET_MAC, &mac_info,

drivers/net/hinic3/base/hinic3_nic_cfg.c line 199

Quoted 10 lines
drivers/net/hinic3/base/hinic3_nic_cfg.c +5 -0
 
+				     sizeof(mac_info), &mac_info, &out_size);
 
+	if (err || !out_size ||
 
+	    hinic3_check_mac_info(hwdev, mac_info.msg_head.status,
 
+				  mac_info.vlan_id)) {
 
+		PMD_DRV_LOG(ERR,

why not have the same indent?

Quoted 70 lines
drivers/net/hinic3/base/hinic3_nic_cfg.c +35 -0
+			    "Update MAC failed, "
 
+			    "err: %d, status: 0x%x, out size: 0x%x",
 
+			    err, mac_info.msg_head.status, out_size);
 
+		return -EIO;
 
+	}
 
+
 
+	if (PF_SET_VF_MAC(hwdev, mac_info.msg_head.status)) {
 
+		PMD_DRV_LOG(WARNING,
 
+			    "PF has already set VF mac, Ignore set operation");
 
+		return HINIC3_PF_SET_VF_ALREADY;
 
+	}
 
+
 
+	if (mac_info.msg_head.status == HINIC3_MGMT_STATUS_EXIST) {
 
+		PMD_DRV_LOG(WARNING,
 
+			    "MAC is repeated. Ignore update operation");
 
+		return 0;
 
+	}
 
+
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_del_mac(void *hwdev, const u8 *mac_addr, u16 vlan_id, u16 func_id)
 
+{
 
+	struct hinic3_port_mac_set mac_info;
 
+	u16 out_size = sizeof(mac_info);
 
+	int err;
 
+
 
+	if (!hwdev || !mac_addr)
 
+		return -EINVAL;
 
+
 
+	if (vlan_id >= VLAN_N_VID) {
 
+		PMD_DRV_LOG(ERR, "Invalid VLAN number: %d", vlan_id);
 
+		return -EINVAL;
 
+	}

drivers/net/hinic3/base/hinic3_nic_cfg.c line 239

Quoted 10 lines
drivers/net/hinic3/base/hinic3_nic_cfg.c +5 -0
 
+
 
+	memset(&mac_info, 0, sizeof(mac_info));
 
+	mac_info.func_id = func_id;
 
+	mac_info.vlan_id = vlan_id;
 
+	memmove(mac_info.mac, mac_addr, ETH_ALEN);

Why use memmove? why not memcpy?

Quoted 178 lines
drivers/net/hinic3/base/hinic3_nic_cfg.c +89 -0
+
 
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_DEL_MAC, &mac_info,
 
+				     sizeof(mac_info), &mac_info, &out_size);
 
+	if (err || !out_size ||
 
+	    (mac_info.msg_head.status &&
 
+	     !PF_SET_VF_MAC(hwdev, mac_info.msg_head.status))) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Delete MAC failed, "
 
+			    "err: %d, status: 0x%x, out size: 0x%x",
 
+			    err, mac_info.msg_head.status, out_size);
 
+		return -EIO;
 
+	}
 
+
 
+	if (PF_SET_VF_MAC(hwdev, mac_info.msg_head.status)) {
 
+		PMD_DRV_LOG(WARNING,
 
+			"PF has already set VF mac, Ignore delete operation");
 
+		return HINIC3_PF_SET_VF_ALREADY;
 
+	}
 
+
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_update_mac(void *hwdev, u8 *old_mac, u8 *new_mac, u16 vlan_id,
 
+		  u16 func_id)
 
+{
 
+	struct hinic3_port_mac_update mac_info;
 
+	u16 out_size = sizeof(mac_info);
 
+	int err;
 
+
 
+	if (!hwdev || !old_mac || !new_mac)
 
+		return -EINVAL;
 
+
 
+	if (vlan_id >= VLAN_N_VID) {
 
+		PMD_DRV_LOG(ERR, "Invalid VLAN number: %d", vlan_id);
 
+		return -EINVAL;
 
+	}
 
+
 
+	memset(&mac_info, 0, sizeof(mac_info));
 
+	mac_info.func_id = func_id;
 
+	mac_info.vlan_id = vlan_id;
 
+	memcpy(mac_info.old_mac, old_mac, ETH_ALEN);
 
+	memcpy(mac_info.new_mac, new_mac, ETH_ALEN);
 
+
 
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_UPDATE_MAC,
 
+				     &mac_info, sizeof(mac_info), &mac_info,
 
+				     &out_size);
 
+	if (err || !out_size ||
 
+	    hinic3_check_mac_info(hwdev, mac_info.msg_head.status,
 
+				  mac_info.vlan_id)) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Update MAC failed, "
 
+			    "err: %d, status: 0x%x, out size: 0x%x",
 
+			    err, mac_info.msg_head.status, out_size);
 
+		return -EIO;
 
+	}
 
+
 
+	if (PF_SET_VF_MAC(hwdev, mac_info.msg_head.status)) {
 
+		PMD_DRV_LOG(WARNING,
 
+			"PF has already set VF MAC. Ignore update operation");
 
+		return HINIC3_PF_SET_VF_ALREADY;
 
+	}
 
+
 
+	if (mac_info.msg_head.status == HINIC3_MGMT_STATUS_EXIST) {
 
+		PMD_DRV_LOG(INFO, "MAC is repeated. Ignore update operation");
 
+		return 0;
 
+	}
 
+
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_get_default_mac(void *hwdev, u8 *mac_addr, int ether_len)
 
+{
 
+	struct hinic3_port_mac_set mac_info;
 
+	u16 out_size = sizeof(mac_info);
 
+	int err;
 
+
 
+	if (!hwdev || !mac_addr)
 
+		return -EINVAL;
 
+
 
+	memset(&mac_info, 0, sizeof(mac_info));
 
+	mac_info.func_id = hinic3_global_func_id(hwdev);
 
+
 
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_GET_MAC, &mac_info,
 
+				     sizeof(mac_info), &mac_info, &out_size);
 
+	if (err || !out_size || mac_info.msg_head.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			"Get MAC failed, err: %d, status: 0x%x, out size: 0x%x",

drivers/net/hinic3/base/hinic3_nic_cfg.c line 333

Quoted 10 lines
drivers/net/hinic3/base/hinic3_nic_cfg.c +5 -0
 
+			err, mac_info.msg_head.status, out_size);
 
+		return -EINVAL;
 
+	}
 
+
 
+	memmove(mac_addr, mac_info.mac, ether_len);

why use memmove?

Quoted 6,057 lines
drivers/net/hinic3/base/hinic3_nic_cfg.c +1495 -0
+
 
+	return 0;
 
+}
 
+
 
+static int
 
+hinic3_config_vlan(void *hwdev, u8 opcode, u16 vlan_id, u16 func_id)
 
+{
 
+	struct hinic3_cmd_vlan_config vlan_info;
 
+	u16 out_size = sizeof(vlan_info);
 
+	int err;
 
+
 
+	memset(&vlan_info, 0, sizeof(vlan_info));
 
+	vlan_info.opcode = opcode;
 
+	vlan_info.func_id = func_id;
 
+	vlan_info.vlan_id = vlan_id;
 
+
 
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_CFG_FUNC_VLAN,
 
+				     &vlan_info, sizeof(vlan_info), &vlan_info,
 
+				     &out_size);
 
+	if (err || !out_size || vlan_info.msg_head.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			"%s vlan failed, err: %d, status: 0x%x, out size: 0x%x",
 
+			opcode == HINIC3_CMD_OP_ADD ? "Add" : "Delete", err,
 
+			vlan_info.msg_head.status, out_size);
 
+		return -EINVAL;
 
+	}
 
+
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_add_vlan(void *hwdev, u16 vlan_id, u16 func_id)
 
+{
 
+	if (!hwdev)
 
+		return -EINVAL;
 
+
 
+	return hinic3_config_vlan(hwdev, HINIC3_CMD_OP_ADD, vlan_id, func_id);
 
+}
 
+
 
+int
 
+hinic3_del_vlan(void *hwdev, u16 vlan_id, u16 func_id)
 
+{
 
+	if (!hwdev)
 
+		return -EINVAL;
 
+
 
+	return hinic3_config_vlan(hwdev, HINIC3_CMD_OP_DEL, vlan_id, func_id);
 
+}
 
+
 
+int
 
+hinic3_get_port_info(void *hwdev, struct nic_port_info *port_info)
 
+{
 
+	struct hinic3_cmd_port_info port_msg;
 
+	u16 out_size = sizeof(port_msg);
 
+	int err;
 
+
 
+	if (!hwdev || !port_info)
 
+		return -EINVAL;
 
+
 
+	memset(&port_msg, 0, sizeof(port_msg));
 
+	port_msg.port_id = hinic3_physical_port_id(hwdev);
 
+
 
+	err = mag_msg_to_mgmt_sync(hwdev, MAG_CMD_GET_PORT_INFO, &port_msg,
 
+				   sizeof(port_msg), &port_msg, &out_size);
 
+	if (err || !out_size || port_msg.msg_head.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Get port info failed, "
 
+			    "err: %d, status: 0x%x, out size: 0x%x",
 
+			    err, port_msg.msg_head.status, out_size);
 
+		return -EINVAL;
 
+	}
 
+
 
+	port_info->autoneg_cap = port_msg.autoneg_cap;
 
+	port_info->autoneg_state = port_msg.autoneg_state;
 
+	port_info->duplex = port_msg.duplex;
 
+	port_info->port_type = port_msg.port_type;
 
+	port_info->speed = port_msg.speed;
 
+	port_info->fec = port_msg.fec;
 
+
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_get_link_state(void *hwdev, u8 *link_state)
 
+{
 
+	struct hinic3_cmd_link_state get_link;
 
+	u16 out_size = sizeof(get_link);
 
+	int err;
 
+
 
+	if (!hwdev || !link_state)
 
+		return -EINVAL;
 
+
 
+	memset(&get_link, 0, sizeof(get_link));
 
+	get_link.port_id = hinic3_physical_port_id(hwdev);
 
+	err = mag_msg_to_mgmt_sync(hwdev, MAG_CMD_GET_LINK_STATUS, &get_link,
 
+				   sizeof(get_link), &get_link, &out_size);
 
+	if (err || !out_size || get_link.msg_head.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Get link state failed, "
 
+			    "err: %d, status: 0x%x, out size: 0x%x",
 
+			    err, get_link.msg_head.status, out_size);
 
+		return -EIO;
 
+	}
 
+
 
+	*link_state = get_link.state;
 
+
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_set_vport_enable(void *hwdev, bool enable)
 
+{
 
+	struct hinic3_vport_state en_state;
 
+	u16 out_size = sizeof(en_state);
 
+	int err;
 
+
 
+	if (!hwdev)
 
+		return -EINVAL;
 
+
 
+	memset(&en_state, 0, sizeof(en_state));
 
+	en_state.func_id = hinic3_global_func_id(hwdev);
 
+	en_state.state = enable ? 1 : 0;
 
+
 
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_SET_VPORT_ENABLE,
 
+				     &en_state, sizeof(en_state), &en_state,
 
+				     &out_size);
 
+	if (err || !out_size || en_state.msg_head.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Set vport state failed, "
 
+			    "err: %d, status: 0x%x, out size: 0x%x",
 
+			    err, en_state.msg_head.status, out_size);
 
+		return -EIO;
 
+	}
 
+
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_set_port_enable(void *hwdev, bool enable)
 
+{
 
+	struct mag_cmd_set_port_enable en_state;
 
+	u16 out_size = sizeof(en_state);
 
+	int err;
 
+
 
+	if (!hwdev)
 
+		return -EINVAL;
 
+
 
+	if (hinic3_func_type(hwdev) == TYPE_VF)
 
+		return 0;
 
+
 
+	memset(&en_state, 0, sizeof(en_state));
 
+	en_state.function_id = hinic3_global_func_id(hwdev);
 
+	en_state.state = enable ? MAG_CMD_TX_ENABLE | MAG_CMD_RX_ENABLE
 
+				: MAG_CMD_PORT_DISABLE;
 
+
 
+	err = mag_msg_to_mgmt_sync(hwdev, MAG_CMD_SET_PORT_ENABLE, &en_state,
 
+				   sizeof(en_state), &en_state, &out_size);
 
+	if (err || !out_size || en_state.head.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Set port state failed, "
 
+			    "err: %d, status: 0x%x, out size: 0x%x",
 
+			    err, en_state.head.status, out_size);
 
+		return -EIO;
 
+	}
 
+
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_flush_qps_res(void *hwdev)
 
+{
 
+	struct hinic3_cmd_clear_qp_resource sq_res;
 
+	u16 out_size = sizeof(sq_res);
 
+	int err;
 
+
 
+	if (!hwdev)
 
+		return -EINVAL;
 
+
 
+	memset(&sq_res, 0, sizeof(sq_res));
 
+	sq_res.func_id = hinic3_global_func_id(hwdev);
 
+
 
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_CLEAR_QP_RESOURCE,
 
+				     &sq_res, sizeof(sq_res), &sq_res,
 
+				     &out_size);
 
+	if (err || !out_size || sq_res.msg_head.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Clear sq resources failed, "
 
+			    "err: %d, status: 0x%x, out size: 0x%x",
 
+			    err, sq_res.msg_head.status, out_size);
 
+		return -EIO;
 
+	}
 
+
 
+	return 0;
 
+}
 
+
 
+/**
 
+ * Get or set the flow control (pause frame) settings for NIC.
 
+ *
 
+ * @param[in] hwdev
 
+ * Pointer to the hardware device.
 
+ * @param[in] opcode
 
+ * The operation to perform. Use `HINIC3_CMD_OP_SET` to set the pause settings
 
+ * and `HINIC3_CMD_OP_GET` to get them.
 
+ * @param[out] nic_pause
 
+ * Pointer to the `nic_pause_config` structure. This structure contains the flow
 
+ * control settings (auto-negotiation, Rx pause, and Tx pause). It is updated
 
+ * when getting settings. When setting, the values in this structure are used.
 
+ *
 
+ * @return
 
+ * 0 on success, non-zero on failure.
 
+ * - -EIO if the operation failed.
 
+ */
 
+static int
 
+hinic3_cfg_hw_pause(void *hwdev, u8 opcode, struct nic_pause_config *nic_pause)
 
+{
 
+	struct hinic3_cmd_pause_config pause_info = {0};
 
+	u16 out_size = sizeof(pause_info);
 
+	int err = 0;
 
+
 
+	pause_info.port_id = hinic3_physical_port_id(hwdev);
 
+	pause_info.opcode = opcode;
 
+	if (opcode == HINIC3_CMD_OP_SET) {
 
+		pause_info.auto_neg = nic_pause->auto_neg;
 
+		pause_info.rx_pause = nic_pause->rx_pause;
 
+		pause_info.tx_pause = nic_pause->tx_pause;
 
+	}
 
+
 
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_CFG_PAUSE_INFO,
 
+				     &pause_info, sizeof(pause_info),
 
+				     &pause_info, &out_size);
 
+	if (err || !out_size || pause_info.msg_head.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "%s pause info failed, err: %d, status: 0x%x, out "
 
+			    "size: 0x%x",
 
+			    opcode == HINIC3_CMD_OP_SET ? "Set" : "Get", err,
 
+			    pause_info.msg_head.status, out_size);
 
+		return -EIO;
 
+	}
 
+
 
+	if (opcode == HINIC3_CMD_OP_GET) {
 
+		nic_pause->auto_neg = pause_info.auto_neg;
 
+		nic_pause->rx_pause = pause_info.rx_pause;
 
+		nic_pause->tx_pause = pause_info.tx_pause;
 
+	}
 
+
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_set_pause_info(void *hwdev, struct nic_pause_config nic_pause)
 
+{
 
+	if (!hwdev)
 
+		return -EINVAL;
 
+
 
+	return hinic3_cfg_hw_pause(hwdev, HINIC3_CMD_OP_SET, &nic_pause);
 
+}
 
+
 
+int
 
+hinic3_get_pause_info(void *hwdev, struct nic_pause_config *nic_pause)
 
+{
 
+	if (!hwdev || !nic_pause)
 
+		return -EINVAL;
 
+
 
+	return hinic3_cfg_hw_pause(hwdev, HINIC3_CMD_OP_GET, nic_pause);
 
+}
 
+
 
+int
 
+hinic3_get_vport_stats(void *hwdev, struct hinic3_vport_stats *stats)
 
+{
 
+	struct hinic3_port_stats_info stats_info;
 
+	struct hinic3_cmd_vport_stats vport_stats;
 
+	u16 out_size = sizeof(vport_stats);
 
+	int err;
 
+
 
+	if (!hwdev || !stats)
 
+		return -EINVAL;
 
+
 
+	memset(&stats_info, 0, sizeof(stats_info));
 
+	memset(&vport_stats, 0, sizeof(vport_stats));
 
+
 
+	stats_info.func_id = hinic3_global_func_id(hwdev);
 
+
 
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_GET_VPORT_STAT,
 
+				     &stats_info, sizeof(stats_info),
 
+				     &vport_stats, &out_size);
 
+	if (err || !out_size || vport_stats.msg_head.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Get function stats failed, err: %d, status: 0x%x, "
 
+			    "out size: 0x%x",
 
+			    err, vport_stats.msg_head.status, out_size);
 
+		return -EIO;
 
+	}
 
+
 
+	memcpy(stats, &vport_stats.stats, sizeof(*stats));
 
+
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_get_phy_port_stats(void *hwdev, struct mag_phy_port_stats *stats)
 
+{
 
+	struct mag_cmd_get_port_stat *port_stats = NULL;
 
+	struct mag_cmd_port_stats_info stats_info;
 
+	u16 out_size = sizeof(*port_stats);
 
+	int err;
 
+
 
+	port_stats = rte_zmalloc("port_stats", sizeof(*port_stats), 0);
 
+	if (!port_stats)
 
+		return -ENOMEM;
 
+
 
+	memset(&stats_info, 0, sizeof(stats_info));
 
+	stats_info.port_id = hinic3_physical_port_id(hwdev);
 
+
 
+	err = mag_msg_to_mgmt_sync(hwdev, MAG_CMD_GET_PORT_STAT, &stats_info,
 
+				   sizeof(stats_info), port_stats, &out_size);
 
+	if (err || !out_size || port_stats->head.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Failed to get port statistics, err: %d, status: "
 
+			    "0x%x, out size: 0x%x",
 
+			    err, port_stats->head.status, out_size);
 
+		err = -EIO;
 
+		goto out;
 
+	}
 
+
 
+	memcpy(stats, &port_stats->counter, sizeof(*stats));
 
+
 
+out:
 
+	rte_free(port_stats);
 
+
 
+	return err;
 
+}
 
+
 
+int
 
+hinic3_clear_vport_stats(void *hwdev)
 
+{
 
+	struct hinic3_cmd_clear_vport_stats clear_vport_stats;
 
+	u16 out_size = sizeof(clear_vport_stats);
 
+	int err;
 
+
 
+	if (!hwdev) {
 
+		PMD_DRV_LOG(ERR, "Hwdev is NULL");
 
+		return -EINVAL;
 
+	}
 
+
 
+	memset(&clear_vport_stats, 0, sizeof(clear_vport_stats));
 
+	clear_vport_stats.func_id = hinic3_global_func_id(hwdev);
 
+
 
+	err = l2nic_msg_to_mgmt_sync(hwdev,
 
+		HINIC3_NIC_CMD_CLEAN_VPORT_STAT, &clear_vport_stats,
 
+		sizeof(clear_vport_stats), &clear_vport_stats, &out_size);
 
+	if (err || !out_size || clear_vport_stats.msg_head.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Clear vport stats failed, "
 
+			    "err: %d, status: 0x%x, out size: 0x%x",
 
+			    err, clear_vport_stats.msg_head.status, out_size);
 
+		return -EIO;
 
+	}
 
+
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_clear_phy_port_stats(void *hwdev)
 
+{
 
+	struct mag_cmd_port_stats_info port_stats;
 
+	u16 out_size = sizeof(port_stats);
 
+	int err;
 
+
 
+	port_stats.port_id = hinic3_physical_port_id(hwdev);
 
+
 
+	err = mag_msg_to_mgmt_sync(hwdev, MAG_CMD_CLR_PORT_STAT, &port_stats,
 
+				   sizeof(port_stats), &port_stats, &out_size);
 
+	if (err || !out_size || port_stats.head.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Failed to get port statistics, "
 
+			    "err: %d, status: 0x%x, out size: 0x%x",
 
+			    err, port_stats.head.status, out_size);
 
+		err = -EIO;
 
+	}
 
+
 
+	return err;
 
+}
 
+
 
+static int
 
+hinic3_set_function_table(void *hwdev, u32 cfg_bitmap,
 
+			  struct hinic3_func_tbl_cfg *cfg)
 
+{
 
+	struct hinic3_cmd_set_func_tbl cmd_func_tbl;
 
+	u16 out_size = sizeof(cmd_func_tbl);
 
+	int err;
 
+
 
+	memset(&cmd_func_tbl, 0, sizeof(cmd_func_tbl));
 
+	cmd_func_tbl.func_id = hinic3_global_func_id(hwdev);
 
+	cmd_func_tbl.cfg_bitmap = cfg_bitmap;
 
+	cmd_func_tbl.tbl_cfg = *cfg;
 
+
 
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_SET_FUNC_TBL,
 
+				     &cmd_func_tbl, sizeof(cmd_func_tbl),
 
+				     &cmd_func_tbl, &out_size);
 
+	if (err || cmd_func_tbl.msg_head.status || !out_size) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Set func table failed, bitmap: 0x%x, err: %d, "
 
+			    "status: 0x%x, out size: 0x%x",
 
+			    cfg_bitmap, err, cmd_func_tbl.msg_head.status,
 
+			    out_size);
 
+		return -EFAULT;
 
+	}
 
+
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_init_function_table(void *hwdev, u16 rx_buff_len)
 
+{
 
+	struct hinic3_func_tbl_cfg func_tbl_cfg;
 
+	u32 cfg_bitmap = BIT(FUNC_CFG_INIT) | BIT(FUNC_CFG_MTU) |
 
+			 BIT(FUNC_CFG_RX_BUF_SIZE);
 
+
 
+	memset(&func_tbl_cfg, 0, sizeof(func_tbl_cfg));
 
+	func_tbl_cfg.mtu = 0x3FFF; /**< Default, max mtu */
 
+	func_tbl_cfg.rx_wqe_buf_size = rx_buff_len;
 
+
 
+	return hinic3_set_function_table(hwdev, cfg_bitmap, &func_tbl_cfg);
 
+}
 
+
 
+int
 
+hinic3_set_port_mtu(void *hwdev, u16 new_mtu)
 
+{
 
+	struct hinic3_func_tbl_cfg func_tbl_cfg;
 
+
 
+	if (!hwdev)
 
+		return -EINVAL;
 
+
 
+	if (new_mtu < HINIC3_MIN_MTU_SIZE) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Invalid mtu size: %ubytes, mtu size < %ubytes",
 
+			    new_mtu, HINIC3_MIN_MTU_SIZE);
 
+		return -EINVAL;
 
+	}
 
+
 
+	if (new_mtu > HINIC3_MAX_JUMBO_FRAME_SIZE) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Invalid mtu size: %ubytes, mtu size > %ubytes",
 
+			    new_mtu, HINIC3_MAX_JUMBO_FRAME_SIZE);
 
+		return -EINVAL;
 
+	}
 
+
 
+	memset(&func_tbl_cfg, 0, sizeof(func_tbl_cfg));
 
+	func_tbl_cfg.mtu = new_mtu;
 
+	return hinic3_set_function_table(hwdev, BIT(FUNC_CFG_MTU),
 
+					 &func_tbl_cfg);
 
+}
 
+
 
+static int
 
+nic_feature_nego(void *hwdev, u8 opcode, u64 *s_feature, u16 size)
 
+{
 
+	struct hinic3_cmd_feature_nego feature_nego;
 
+	u16 out_size = sizeof(feature_nego);
 
+	int err;
 
+
 
+	if (!hwdev || !s_feature || size > MAX_FEATURE_QWORD)
 
+		return -EINVAL;
 
+
 
+	memset(&feature_nego, 0, sizeof(feature_nego));
 
+	feature_nego.func_id = hinic3_global_func_id(hwdev);
 
+	feature_nego.opcode = opcode;
 
+	if (opcode == HINIC3_CMD_OP_SET)
 
+		memcpy(feature_nego.s_feature, s_feature, size * sizeof(u64));
 
+
 
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_FEATURE_NEGO,
 
+				     &feature_nego, sizeof(feature_nego),
 
+				     &feature_nego, &out_size);
 
+	if (err || !out_size || feature_nego.msg_head.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Failed to negotiate nic feature, err:%d, status: "
 
+			    "0x%x, out_size: 0x%x",
 
+			    err, feature_nego.msg_head.status, out_size);
 
+		return -EFAULT;
 
+	}
 
+
 
+	if (opcode == HINIC3_CMD_OP_GET)
 
+		memcpy(s_feature, feature_nego.s_feature, size * sizeof(u64));
 
+
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_get_feature_from_hw(void *hwdev, u64 *s_feature, u16 size)
 
+{
 
+	return nic_feature_nego(hwdev, HINIC3_CMD_OP_GET, s_feature, size);
 
+}
 
+
 
+int
 
+hinic3_set_feature_to_hw(void *hwdev, u64 *s_feature, u16 size)
 
+{
 
+	return nic_feature_nego(hwdev, HINIC3_CMD_OP_SET, s_feature, size);
 
+}
 
+
 
+static int
 
+hinic3_vf_func_init(void *hwdev)
 
+{
 
+	struct hinic3_cmd_register_vf register_info;
 
+	u16 out_size = sizeof(register_info);
 
+	int err;
 
+
 
+	if (hinic3_func_type(hwdev) != TYPE_VF)
 
+		return 0;
 
+
 
+	memset(&register_info, 0, sizeof(register_info));
 
+	register_info.op_register = 1;
 
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_VF_REGISTER,
 
+				     &register_info, sizeof(register_info),
 
+				     &register_info, &out_size);
 
+	if (err || register_info.msg_head.status || !out_size) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Register VF failed, err: %d, status: 0x%x, out "
 
+			    "size: 0x%x",
 
+			    err, register_info.msg_head.status, out_size);
 
+		return -EFAULT;
 
+	}
 
+
 
+	return 0;
 
+}
 
+
 
+static int
 
+hinic3_vf_func_free(void *hwdev)
 
+{
 
+	struct hinic3_cmd_register_vf unregister;
 
+	u16 out_size = sizeof(unregister);
 
+	int err;
 
+
 
+	if (hinic3_func_type(hwdev) != TYPE_VF)
 
+		return 0;
 
+
 
+	memset(&unregister, 0, sizeof(unregister));
 
+	unregister.op_register = 0;
 
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_VF_REGISTER,
 
+				     &unregister, sizeof(unregister),
 
+				     &unregister, &out_size);
 
+	if (err || unregister.msg_head.status || !out_size) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Unregister VF failed, err: %d, status: 0x%x, out "
 
+			    "size: 0x%x",
 
+			    err, unregister.msg_head.status, out_size);
 
+		return -EFAULT;
 
+	}
 
+
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_init_nic_hwdev(void *hwdev)
 
+{
 
+	return hinic3_vf_func_init(hwdev);
 
+}
 
+
 
+void
 
+hinic3_free_nic_hwdev(void *hwdev)
 
+{
 
+	if (!hwdev)
 
+		return;
 
+
 
+	if (hinic3_func_type(hwdev) != TYPE_VF)
 
+		hinic3_set_link_status_follow(hwdev, HINIC3_LINK_FOLLOW_DEFAULT);
 
+
 
+	hinic3_vf_func_free(hwdev);
 
+}
 
+
 
+int
 
+hinic3_set_rx_mode(void *hwdev, u32 enable)
 
+{
 
+	struct hinic3_rx_mode_config rx_mode_cfg;
 
+	u16 out_size = sizeof(rx_mode_cfg);
 
+	int err;
 
+
 
+	if (!hwdev)
 
+		return -EINVAL;
 
+
 
+	memset(&rx_mode_cfg, 0, sizeof(rx_mode_cfg));
 
+	rx_mode_cfg.func_id = hinic3_global_func_id(hwdev);
 
+	rx_mode_cfg.rx_mode = enable;
 
+
 
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_SET_RX_MODE,
 
+				     &rx_mode_cfg, sizeof(rx_mode_cfg),
 
+				     &rx_mode_cfg, &out_size);
 
+	if (err || !out_size || rx_mode_cfg.msg_head.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Set rx mode failed, err: %d, status: 0x%x, out "
 
+			    "size: 0x%x",
 
+			    err, rx_mode_cfg.msg_head.status, out_size);
 
+		return -EIO;
 
+	}
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_set_rx_vlan_offload(void *hwdev, u8 en)
 
+{
 
+	struct hinic3_cmd_vlan_offload vlan_cfg;
 
+	u16 out_size = sizeof(vlan_cfg);
 
+	int err;
 
+
 
+	if (!hwdev)
 
+		return -EINVAL;
 
+
 
+	memset(&vlan_cfg, 0, sizeof(vlan_cfg));
 
+	vlan_cfg.func_id = hinic3_global_func_id(hwdev);
 
+	vlan_cfg.vlan_offload = en;
 
+
 
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_SET_RX_VLAN_OFFLOAD,
 
+				     &vlan_cfg, sizeof(vlan_cfg), &vlan_cfg,
 
+				     &out_size);
 
+	if (err || !out_size || vlan_cfg.msg_head.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Set rx vlan offload failed, err: %d, status: "
 
+			    "0x%x, out size: 0x%x",
 
+			    err, vlan_cfg.msg_head.status, out_size);
 
+		return -EIO;
 
+	}
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_set_vlan_filter(void *hwdev, u32 vlan_filter_ctrl)
 
+{
 
+	struct hinic3_cmd_set_vlan_filter vlan_filter;
 
+	u16 out_size = sizeof(vlan_filter);
 
+	int err;
 
+
 
+	if (!hwdev)
 
+		return -EINVAL;
 
+
 
+	memset(&vlan_filter, 0, sizeof(vlan_filter));
 
+	vlan_filter.func_id = hinic3_global_func_id(hwdev);
 
+	vlan_filter.vlan_filter_ctrl = vlan_filter_ctrl;
 
+
 
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_SET_VLAN_FILTER_EN,
 
+				     &vlan_filter, sizeof(vlan_filter),
 
+				     &vlan_filter, &out_size);
 
+	if (err || !out_size || vlan_filter.msg_head.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Failed to set vlan filter, err: %d, status: 0x%x, "
 
+			    "out size: 0x%x",
 
+			    err, vlan_filter.msg_head.status, out_size);
 
+		return -EIO;
 
+	}
 
+	return 0;
 
+}
 
+
 
+static int
 
+hinic3_set_rx_lro(void *hwdev, u8 ipv4_en, u8 ipv6_en, u8 lro_max_pkt_len)
 
+{
 
+	struct hinic3_cmd_lro_config lro_cfg = {0};
 
+	u16 out_size = sizeof(lro_cfg);
 
+	int err;
 
+
 
+	if (!hwdev)
 
+		return -EINVAL;
 
+
 
+	lro_cfg.func_id = hinic3_global_func_id(hwdev);
 
+	lro_cfg.opcode = HINIC3_CMD_OP_SET;
 
+	lro_cfg.lro_ipv4_en = ipv4_en;
 
+	lro_cfg.lro_ipv6_en = ipv6_en;
 
+	lro_cfg.lro_max_pkt_len = lro_max_pkt_len;
 
+
 
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_CFG_RX_LRO, &lro_cfg,
 
+				     sizeof(lro_cfg), &lro_cfg, &out_size);
 
+	if (err || !out_size || lro_cfg.msg_head.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Set lro offload failed, err: %d, status: 0x%x, "
 
+			    "out size: 0x%x",
 
+			    err, lro_cfg.msg_head.status, out_size);
 
+		return -EIO;
 
+	}
 
+
 
+	return 0;
 
+}
 
+
 
+static int
 
+hinic3_set_rx_lro_timer(void *hwdev, u32 timer_value)
 
+{
 
+	struct hinic3_cmd_lro_timer lro_timer;
 
+	u16 out_size = sizeof(lro_timer);
 
+	int err;
 
+
 
+	if (!hwdev)
 
+		return -EINVAL;
 
+
 
+	memset(&lro_timer, 0, sizeof(lro_timer));
 
+	lro_timer.opcode = HINIC3_CMD_OP_SET;
 
+	lro_timer.timer = timer_value;
 
+
 
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_CFG_LRO_TIMER,
 
+				     &lro_timer, sizeof(lro_timer), &lro_timer,
 
+				     &out_size);
 
+	if (err || !out_size || lro_timer.msg_head.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Set lro timer failed, err: %d, status: 0x%x, out "
 
+			    "size: 0x%x",
 
+			    err, lro_timer.msg_head.status, out_size);
 
+
 
+		return -EIO;
 
+	}
 
+
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_set_rx_lro_state(void *hwdev, u8 lro_en, u32 lro_timer,
 
+			u32 lro_max_pkt_len)
 
+{
 
+	u8 ipv4_en = 0, ipv6_en = 0;
 
+	int err;
 
+
 
+	if (!hwdev)
 
+		return -EINVAL;
 
+
 
+	ipv4_en = lro_en ? 1 : 0;
 
+	ipv6_en = lro_en ? 1 : 0;
 
+
 
+	PMD_DRV_LOG(INFO, "Set LRO max coalesce packet size to %uK",
 
+		    lro_max_pkt_len);
 
+
 
+	err = hinic3_set_rx_lro(hwdev, ipv4_en, ipv6_en, (u8)lro_max_pkt_len);
 
+	if (err)
 
+		return err;
 
+
 
+	/* We don't set LRO timer for VF */
 
+	if (hinic3_func_type(hwdev) == TYPE_VF)
 
+		return 0;
 
+
 
+	PMD_DRV_LOG(INFO, "Set LRO timer to %u", lro_timer);
 
+
 
+	return hinic3_set_rx_lro_timer(hwdev, lro_timer);
 
+}
 
+
 
+/* RSS config */
 
+int
 
+hinic3_rss_template_alloc(void *hwdev)
 
+{
 
+	struct hinic3_rss_template_mgmt template_mgmt;
 
+	u16 out_size = sizeof(template_mgmt);
 
+	int err;
 
+
 
+	if (!hwdev)
 
+		return -EINVAL;
 
+
 
+	memset(&template_mgmt, 0, sizeof(struct hinic3_rss_template_mgmt));
 
+	template_mgmt.func_id = hinic3_global_func_id(hwdev);
 
+	template_mgmt.cmd = NIC_RSS_CMD_TEMP_ALLOC;
 
+
 
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_RSS_TEMP_MGR,
 
+				     &template_mgmt, sizeof(template_mgmt),
 
+				     &template_mgmt, &out_size);
 
+	if (err || !out_size || template_mgmt.msg_head.status) {
 
+		if (template_mgmt.msg_head.status ==
 
+		    HINIC3_MGMT_STATUS_TABLE_FULL) {
 
+			PMD_DRV_LOG(ERR, "There is no more template available");
 
+			return -ENOSPC;
 
+		}
 
+		PMD_DRV_LOG(ERR,
 
+			    "Alloc rss template failed, err: %d, "
 
+			    "status: 0x%x, out size: 0x%x",
 
+			    err, template_mgmt.msg_head.status, out_size);
 
+		return -EFAULT;
 
+	}
 
+
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_rss_template_free(void *hwdev)
 
+{
 
+	struct hinic3_rss_template_mgmt template_mgmt;
 
+	u16 out_size = sizeof(template_mgmt);
 
+	int err;
 
+
 
+	if (!hwdev)
 
+		return -EINVAL;
 
+
 
+	memset(&template_mgmt, 0, sizeof(struct hinic3_rss_template_mgmt));
 
+	template_mgmt.func_id = hinic3_global_func_id(hwdev);
 
+	template_mgmt.cmd = NIC_RSS_CMD_TEMP_FREE;
 
+
 
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_RSS_TEMP_MGR,
 
+				     &template_mgmt, sizeof(template_mgmt),
 
+				     &template_mgmt, &out_size);
 
+	if (err || !out_size || template_mgmt.msg_head.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Free rss template failed, err: %d, "
 
+			    "status: 0x%x, out size: 0x%x",
 
+			    err, template_mgmt.msg_head.status, out_size);
 
+		return -EFAULT;
 
+	}
 
+
 
+	return 0;
 
+}
 
+
 
+static int
 
+hinic3_rss_cfg_hash_key(void *hwdev, u8 opcode, u8 *key, u16 key_size)
 
+{
 
+	struct hinic3_cmd_rss_hash_key hash_key;
 
+	u16 out_size = sizeof(hash_key);
 
+	int err;
 
+
 
+	if (!hwdev || !key)
 
+		return -EINVAL;
 
+
 
+	memset(&hash_key, 0, sizeof(struct hinic3_cmd_rss_hash_key));
 
+	hash_key.func_id = hinic3_global_func_id(hwdev);
 
+	hash_key.opcode = opcode;
 
+	if (opcode == HINIC3_CMD_OP_SET)
 
+		memcpy(hash_key.key, key, key_size);
 
+
 
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_CFG_RSS_HASH_KEY,
 
+				     &hash_key, sizeof(hash_key), &hash_key,
 
+				     &out_size);
 
+	if (err || !out_size || hash_key.msg_head.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "%s hash key failed, err: %d, "
 
+			    "status: 0x%x, out size: 0x%x",
 
+			    opcode == HINIC3_CMD_OP_SET ? "Set" : "Get", err,
 
+			    hash_key.msg_head.status, out_size);
 
+		return -EFAULT;
 
+	}
 
+
 
+	if (opcode == HINIC3_CMD_OP_GET)
 
+		memcpy(key, hash_key.key, key_size);
 
+
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_rss_set_hash_key(void *hwdev, u8 *key, u16 key_size)
 
+{
 
+	if (!hwdev || !key)
 
+		return -EINVAL;
 
+
 
+	return hinic3_rss_cfg_hash_key(hwdev, HINIC3_CMD_OP_SET, key, key_size);
 
+}
 
+
 
+int
 
+hinic3_rss_get_indir_tbl(void *hwdev, u32 *indir_table, u32 indir_table_size)
 
+{
 
+	struct hinic3_cmd_buf *cmd_buf = NULL;
 
+	u16 *indir_tbl = NULL;
 
+	int err;
 
+	u32 i;
 
+
 
+	if (!hwdev || !indir_table)
 
+		return -EINVAL;
 
+
 
+	cmd_buf = hinic3_alloc_cmd_buf(hwdev);
 
+	if (!cmd_buf) {
 
+		PMD_DRV_LOG(ERR, "Allocate cmd buf failed");
 
+		return -ENOMEM;
 
+	}
 
+
 
+	cmd_buf->size = sizeof(struct nic_rss_indirect_tbl);
 
+	err = hinic3_cmdq_detail_resp(hwdev, HINIC3_MOD_L2NIC,
 
+				      HINIC3_UCODE_CMD_GET_RSS_INDIR_TABLE,
 
+				      cmd_buf, cmd_buf, 0);
 
+	if (err) {
 
+		PMD_DRV_LOG(ERR, "Get rss indir table failed");
 
+		hinic3_free_cmd_buf(cmd_buf);
 
+		return err;
 
+	}
 
+
 
+	indir_tbl = (u16 *)cmd_buf->buf;
 
+	for (i = 0; i < indir_table_size; i++)
 
+		indir_table[i] = *(indir_tbl + i);
 
+
 
+	hinic3_free_cmd_buf(cmd_buf);
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_rss_set_indir_tbl(void *hwdev, const u32 *indir_table,
 
+			 u32 indir_table_size)
 
+{
 
+	struct nic_rss_indirect_tbl *indir_tbl = NULL;
 
+	struct hinic3_cmd_buf *cmd_buf = NULL;
 
+	u32 i, size;
 
+	u32 *temp = NULL;
 
+	u64 out_param = 0;
 
+	int err;
 
+
 
+	if (!hwdev || !indir_table)
 
+		return -EINVAL;
 
+
 
+	cmd_buf = hinic3_alloc_cmd_buf(hwdev);
 
+	if (!cmd_buf) {
 
+		PMD_DRV_LOG(ERR, "Allocate cmd buf failed");
 
+		return -ENOMEM;
 
+	}
 
+
 
+	cmd_buf->size = sizeof(struct nic_rss_indirect_tbl);
 
+	indir_tbl = (struct nic_rss_indirect_tbl *)cmd_buf->buf;
 
+	memset(indir_tbl, 0, sizeof(*indir_tbl));
 
+
 
+	for (i = 0; i < indir_table_size; i++)
 
+		indir_tbl->entry[i] = (u16)(*(indir_table + i));
 
+
 
+	rte_atomic_thread_fence(rte_memory_order_seq_cst);
 
+	size = sizeof(indir_tbl->entry) / sizeof(u16);
 
+	temp = (u32 *)indir_tbl->entry;
 
+	for (i = 0; i < size; i++)
 
+		temp[i] = cpu_to_be32(temp[i]);
 
+
 
+	err = hinic3_cmdq_direct_resp(hwdev, HINIC3_MOD_L2NIC,
 
+				      HINIC3_UCODE_CMD_SET_RSS_INDIR_TABLE,
 
+				      cmd_buf, &out_param, 0);
 
+	if (err || out_param != 0) {
 
+		PMD_DRV_LOG(ERR, "Set rss indir table failed");
 
+		err = -EFAULT;
 
+	}
 
+
 
+	hinic3_free_cmd_buf(cmd_buf);
 
+	return err;
 
+}
 
+
 
+static int
 
+hinic3_cmdq_set_rss_type(void *hwdev, struct hinic3_rss_type rss_type)
 
+{
 
+	struct nic_rss_context_tbl *ctx_tbl = NULL;
 
+	struct hinic3_cmd_buf *cmd_buf = NULL;
 
+	u32 ctx = 0;
 
+	u64 out_param = 0;
 
+	int err;
 
+
 
+	if (!hwdev)
 
+		return -EINVAL;
 
+
 
+	cmd_buf = hinic3_alloc_cmd_buf(hwdev);
 
+	if (!cmd_buf) {
 
+		PMD_DRV_LOG(ERR, "Allocate cmd buf failed");
 
+		return -ENOMEM;
 
+	}
 
+
 
+	ctx |= HINIC3_RSS_TYPE_SET(1, VALID) |
 
+	       HINIC3_RSS_TYPE_SET(rss_type.ipv4, IPV4) |
 
+	       HINIC3_RSS_TYPE_SET(rss_type.ipv6, IPV6) |
 
+	       HINIC3_RSS_TYPE_SET(rss_type.ipv6_ext, IPV6_EXT) |
 
+	       HINIC3_RSS_TYPE_SET(rss_type.tcp_ipv4, TCP_IPV4) |
 
+	       HINIC3_RSS_TYPE_SET(rss_type.tcp_ipv6, TCP_IPV6) |
 
+	       HINIC3_RSS_TYPE_SET(rss_type.tcp_ipv6_ext, TCP_IPV6_EXT) |
 
+	       HINIC3_RSS_TYPE_SET(rss_type.udp_ipv4, UDP_IPV4) |
 
+	       HINIC3_RSS_TYPE_SET(rss_type.udp_ipv6, UDP_IPV6);
 
+
 
+	cmd_buf->size = sizeof(struct nic_rss_context_tbl);
 
+	ctx_tbl = (struct nic_rss_context_tbl *)cmd_buf->buf;
 
+	memset(ctx_tbl, 0, sizeof(*ctx_tbl));
 
+	rte_atomic_thread_fence(rte_memory_order_seq_cst);
 
+	ctx_tbl->ctx = cpu_to_be32(ctx);
 
+
 
+	/* Cfg the RSS context table by command queue. */
 
+	err = hinic3_cmdq_direct_resp(hwdev, HINIC3_MOD_L2NIC,
 
+				      HINIC3_UCODE_CMD_SET_RSS_CONTEXT_TABLE,
 
+				      cmd_buf, &out_param, 0);
 
+
 
+	hinic3_free_cmd_buf(cmd_buf);
 
+
 
+	if (err || out_param != 0) {
 
+		PMD_DRV_LOG(ERR, "Cmdq set rss context table failed, err: %d",
 
+			    err);
 
+		return -EFAULT;
 
+	}
 
+
 
+	return 0;
 
+}
 
+
 
+static int
 
+hinic3_mgmt_set_rss_type(void *hwdev, struct hinic3_rss_type rss_type)
 
+{
 
+	struct hinic3_rss_context_table ctx_tbl;
 
+	u32 ctx = 0;
 
+	u16 out_size = sizeof(ctx_tbl);
 
+	int err;
 
+
 
+	if (!hwdev)
 
+		return -EINVAL;
 
+
 
+	memset(&ctx_tbl, 0, sizeof(ctx_tbl));
 
+	ctx_tbl.func_id = hinic3_global_func_id(hwdev);
 
+	ctx |= HINIC3_RSS_TYPE_SET(1, VALID) |
 
+	       HINIC3_RSS_TYPE_SET(rss_type.ipv4, IPV4) |
 
+	       HINIC3_RSS_TYPE_SET(rss_type.ipv6, IPV6) |
 
+	       HINIC3_RSS_TYPE_SET(rss_type.ipv6_ext, IPV6_EXT) |
 
+	       HINIC3_RSS_TYPE_SET(rss_type.tcp_ipv4, TCP_IPV4) |
 
+	       HINIC3_RSS_TYPE_SET(rss_type.tcp_ipv6, TCP_IPV6) |
 
+	       HINIC3_RSS_TYPE_SET(rss_type.tcp_ipv6_ext, TCP_IPV6_EXT) |
 
+	       HINIC3_RSS_TYPE_SET(rss_type.udp_ipv4, UDP_IPV4) |
 
+	       HINIC3_RSS_TYPE_SET(rss_type.udp_ipv6, UDP_IPV6);
 
+	ctx_tbl.context = ctx;
 
+
 
+	err = l2nic_msg_to_mgmt_sync(hwdev,
 
+		HINIC3_NIC_CMD_SET_RSS_CTX_TBL_INTO_FUNC, &ctx_tbl,
 
+		sizeof(ctx_tbl), &ctx_tbl, &out_size);
 
+	if (ctx_tbl.msg_head.status == HINIC3_MGMT_CMD_UNSUPPORTED) {
 
+		return HINIC3_MGMT_CMD_UNSUPPORTED;
 
+	} else if (err || !out_size || ctx_tbl.msg_head.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Mgmt set rss context table failed, "
 
+			    "err: %d, status: 0x%x, out size: 0x%x",
 
+			    err, ctx_tbl.msg_head.status, out_size);
 
+		return -EINVAL;
 
+	}
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_set_rss_type(void *hwdev, struct hinic3_rss_type rss_type)
 
+{
 
+	int err;
 
+	err = hinic3_mgmt_set_rss_type(hwdev, rss_type);
 
+	if (err == HINIC3_MGMT_CMD_UNSUPPORTED)
 
+		err = hinic3_cmdq_set_rss_type(hwdev, rss_type);
 
+	return err;
 
+}
 
+
 
+int
 
+hinic3_get_rss_type(void *hwdev, struct hinic3_rss_type *rss_type)
 
+{
 
+	struct hinic3_rss_context_table ctx_tbl;
 
+	u16 out_size = sizeof(ctx_tbl);
 
+	int err;
 
+
 
+	if (!hwdev || !rss_type)
 
+		return -EINVAL;
 
+
 
+	memset(&ctx_tbl, 0, sizeof(struct hinic3_rss_context_table));
 
+	ctx_tbl.func_id = hinic3_global_func_id(hwdev);
 
+
 
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_GET_RSS_CTX_TBL,
 
+				     &ctx_tbl, sizeof(ctx_tbl), &ctx_tbl,
 
+				     &out_size);
 
+	if (err || !out_size || ctx_tbl.msg_head.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Get hash type failed, "
 
+			    "err: %d, status: 0x%x, out size: 0x%x",
 
+			    err, ctx_tbl.msg_head.status, out_size);
 
+		return -EFAULT;
 
+	}
 
+
 
+	rss_type->ipv4 = HINIC3_RSS_TYPE_GET(ctx_tbl.context, IPV4);
 
+	rss_type->ipv6 = HINIC3_RSS_TYPE_GET(ctx_tbl.context, IPV6);
 
+	rss_type->ipv6_ext = HINIC3_RSS_TYPE_GET(ctx_tbl.context, IPV6_EXT);
 
+	rss_type->tcp_ipv4 = HINIC3_RSS_TYPE_GET(ctx_tbl.context, TCP_IPV4);
 
+	rss_type->tcp_ipv6 = HINIC3_RSS_TYPE_GET(ctx_tbl.context, TCP_IPV6);
 
+	rss_type->tcp_ipv6_ext =
 
+		HINIC3_RSS_TYPE_GET(ctx_tbl.context, TCP_IPV6_EXT);
 
+	rss_type->udp_ipv4 = HINIC3_RSS_TYPE_GET(ctx_tbl.context, UDP_IPV4);
 
+	rss_type->udp_ipv6 = HINIC3_RSS_TYPE_GET(ctx_tbl.context, UDP_IPV6);
 
+
 
+	return 0;
 
+}
 
+
 
+static int
 
+hinic3_rss_cfg_hash_engine(void *hwdev, u8 opcode, u8 *type)
 
+{
 
+	struct hinic3_cmd_rss_engine_type hash_type;
 
+	u16 out_size = sizeof(hash_type);
 
+	int err;
 
+
 
+	if (!hwdev || !type)
 
+		return -EINVAL;
 
+
 
+	memset(&hash_type, 0, sizeof(struct hinic3_cmd_rss_engine_type));
 
+	hash_type.func_id = hinic3_global_func_id(hwdev);
 
+	hash_type.opcode = opcode;
 
+	if (opcode == HINIC3_CMD_OP_SET)
 
+		hash_type.hash_engine = *type;
 
+
 
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_CFG_RSS_HASH_ENGINE,
 
+				     &hash_type, sizeof(hash_type), &hash_type,
 
+				     &out_size);
 
+	if (err || !out_size || hash_type.msg_head.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "%s hash engine failed, "
 
+			    "err: %d, status: 0x%x, out size: 0x%x",
 
+			    opcode == HINIC3_CMD_OP_SET ? "Set" : "Get", err,
 
+			    hash_type.msg_head.status, out_size);
 
+		return -EFAULT;
 
+	}
 
+
 
+	if (opcode == HINIC3_CMD_OP_GET)
 
+		*type = hash_type.hash_engine;
 
+
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_rss_get_hash_engine(void *hwdev, u8 *type)
 
+{
 
+	if (!hwdev || !type)
 
+		return -EINVAL;
 
+
 
+	return hinic3_rss_cfg_hash_engine(hwdev, HINIC3_CMD_OP_GET, type);
 
+}
 
+
 
+int
 
+hinic3_rss_set_hash_engine(void *hwdev, u8 type)
 
+{
 
+	if (!hwdev)
 
+		return -EINVAL;
 
+
 
+	return hinic3_rss_cfg_hash_engine(hwdev, HINIC3_CMD_OP_SET, &type);
 
+}
 
+
 
+int
 
+hinic3_rss_cfg(void *hwdev, u8 rss_en, u8 tc_num, u8 *prio_tc)
 
+{
 
+	struct hinic3_cmd_rss_config rss_cfg;
 
+	u16 out_size = sizeof(rss_cfg);
 
+	int err;
 
+
 
+	/* Ucode requires number of TC should be power of 2. */
 
+	if (!hwdev || !prio_tc || (tc_num & (tc_num - 1)))
 
+		return -EINVAL;
 
+
 
+	memset(&rss_cfg, 0, sizeof(struct hinic3_cmd_rss_config));
 
+	rss_cfg.func_id = hinic3_global_func_id(hwdev);
 
+	rss_cfg.rss_en = rss_en;
 
+	rss_cfg.rq_priority_number = tc_num ? (u8)ilog2(tc_num) : 0;
 
+
 
+	memcpy(rss_cfg.prio_tc, prio_tc, HINIC3_DCB_UP_MAX);
 
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_RSS_CFG, &rss_cfg,
 
+				     sizeof(rss_cfg), &rss_cfg, &out_size);
 
+	if (err || !out_size || rss_cfg.msg_head.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Set rss cfg failed, err: %d, "
 
+			    "status: 0x%x, out size: 0x%x",
 
+			    err, rss_cfg.msg_head.status, out_size);
 
+		return -EFAULT;
 
+	}
 
+
 
+	return 0;
 
+}
 
+
 
+int
 
+hinic3_vf_get_default_cos(void *hwdev, u8 *cos_id)
 
+{
 
+	struct hinic3_cmd_vf_dcb_state vf_dcb;
 
+	u16 out_size = sizeof(vf_dcb);
 
+	int err;
 
+
 
+	memset(&vf_dcb, 0, sizeof(vf_dcb));
 
+
 
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC3_NIC_CMD_VF_COS, &vf_dcb,
 
+				     sizeof(vf_dcb), &vf_dcb, &out_size);
 
+	if (err || !out_size || vf_dcb.msg_head.status) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Get VF default cos failed, "
 
+			    "err: %d, status: 0x%x, out size: 0x%x",
 
+			    err, vf_dcb.msg_head.status, out_size);
 
+		return -EIO;
 
+	}
 
+
 
+	*cos_id = vf_dcb.state.default_cos;
 
+
 
+	return 0;
 
+}
 
+
 
+/**
 
+ * Set the Ethernet type filtering rule for the FDIR of a NIC.
 
+ *
 
+ * @param[in] hwdev
 
+ * Pointer to hardware device structure.
 
+ * @param[in] pkt_type
 
+ * Indicate the packet type.
[… more lines in the raw message]

Lines of this file are not shown. View the raw message.

drivers/net/hinic3/base/hinic3_nic_cfg.h +0 -0

The rest of this diff is not shown. View the raw message.

new file mode 100644

index 0000000000..e6a863eb4d

--- /dev/null

+++ b/drivers/net/hinic3/base/hinic3_nic_cfg.h

drivers/net/hinic3/base/hinic3_nic_cfg.h +1527 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Part of the diff is not shown here. View the raw message.

#144 lore raw

PATCH v5 10/18 net/hinic3: add context and work queue support

In reply to Feifei Wang

From: Xin Wang <[email protected]>

Work queue is used for cmdq and tx/rx buff description. Nic business needs to configure cmdq context and txq/rxq context. This patch adds data structures and function codes for work queue and context.

---
 drivers/net/hinic3/base/hinic3_wq.c | 148 ++++++++++++++++++++++++++++
 drivers/net/hinic3/base/hinic3_wq.h | 109 ++++++++++++++++++++
 2 files changed, 257 insertions(+)
 create mode 100644 drivers/net/hinic3/base/hinic3_wq.c
 create mode 100644 drivers/net/hinic3/base/hinic3_wq.h
drivers/net/hinic3/base/hinic3_wq.c new file +148 -0
new file mode 100644
index 0000000000..9bccb10c9a
@@ -0,0 +1,148 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2025 Huawei Technologies Co., Ltd
+ */
+#include <rte_bus_pci.h>
+#include <rte_errno.h>
+#include <rte_ether.h>
+#include <rte_malloc.h>
+#include <rte_mbuf.h>
+#include <rte_mempool.h>
+#include <rte_pci.h>
+
+#include "hinic3_compat.h"
+#include "hinic3_hwdev.h"
+#include "hinic3_wq.h"
+
+static void
+free_wq_pages(struct hinic3_wq *wq)
+{
+	hinic3_memzone_free(wq->wq_mz);
+
+	wq->queue_buf_paddr = 0;
+	wq->queue_buf_vaddr = 0;
+}
+
+static int
+alloc_wq_pages(struct hinic3_hwdev *hwdev, struct hinic3_wq *wq, int qid)
+{
+	const struct rte_memzone *wq_mz;
+
+	wq_mz = hinic3_dma_zone_reserve(hwdev->eth_dev, "hinic3_wq_mz",
+					(uint16_t)qid, wq->wq_buf_size,
+					RTE_PGSIZE_256K, SOCKET_ID_ANY);
+	if (!wq_mz) {
+		PMD_DRV_LOG(ERR, "Allocate wq[%d] rq_mz failed", qid);
+		return -ENOMEM;
+	}
+
+	memset(wq_mz->addr, 0, wq->wq_buf_size);
+	wq->wq_mz = wq_mz;
+	wq->queue_buf_paddr = wq_mz->iova;
+	wq->queue_buf_vaddr = (u64)(u64 *)wq_mz->addr;
+
+	return 0;
+}
+
+void
+hinic3_put_wqe(struct hinic3_wq *wq, int num_wqebbs)
+{
+	wq->cons_idx += num_wqebbs;
+	rte_atomic_fetch_add_explicit(&wq->delta, num_wqebbs,
+				      rte_memory_order_seq_cst);
+}
+
+void *
+hinic3_read_wqe(struct hinic3_wq *wq, int num_wqebbs, u16 *cons_idx)
+{
+	u16 curr_cons_idx;
+
+	if ((rte_atomic_load_explicit(&wq->delta, rte_memory_order_seq_cst) +
+	     num_wqebbs) > wq->q_depth)
+		return NULL;
+
+	curr_cons_idx = (u16)(wq->cons_idx);
+
+	curr_cons_idx = MASKED_WQE_IDX(wq, curr_cons_idx);
+
+	*cons_idx = curr_cons_idx;
+
+	return WQ_WQE_ADDR(wq, (u32)(*cons_idx));
+}
+
+int
+hinic3_cmdq_alloc(struct hinic3_wq *wq, void *dev, int cmdq_blocks,
+		  u32 wq_buf_size, u32 wqebb_shift, u16 q_depth)
+{
+	struct hinic3_hwdev *hwdev = (struct hinic3_hwdev *)dev;
+	int i, j;
+	int err;
+
+	/* Validate q_depth is power of 2 & wqebb_size is not 0. */
+	for (i = 0; i < cmdq_blocks; i++) {
+		wq[i].wqebb_size = 1U << wqebb_shift;
+		wq[i].wqebb_shift = wqebb_shift;
+		wq[i].wq_buf_size = wq_buf_size;
+		wq[i].q_depth = q_depth;
+
+		err = alloc_wq_pages(hwdev, &wq[i], i);
+		if (err) {
+			PMD_DRV_LOG(ERR, "Failed to alloc CMDQ blocks");
+			goto cmdq_block_err;
+		}
+
+		wq[i].cons_idx = 0;
+		wq[i].prod_idx = 0;
+		rte_atomic_store_explicit(&wq[i].delta, q_depth,
+					  rte_memory_order_seq_cst);
+
+		wq[i].mask = q_depth - 1;
+	}
+
+	return 0;
+
+cmdq_block_err:
+	for (j = 0; j < i; j++)
+		free_wq_pages(&wq[j]);
+
+	return err;
+}
+
+void
+hinic3_cmdq_free(struct hinic3_wq *wq, int cmdq_blocks)
+{
+	int i;
+
+	for (i = 0; i < cmdq_blocks; i++)
+		free_wq_pages(&wq[i]);
+}
+
+void
+hinic3_wq_wqe_pg_clear(struct hinic3_wq *wq)
+{
+	wq->cons_idx = 0;
+	wq->prod_idx = 0;
+
+	memset((void *)wq->queue_buf_vaddr, 0, wq->wq_buf_size);
+}
+
+void *
+hinic3_get_wqe(struct hinic3_wq *wq, int num_wqebbs, u16 *prod_idx)
+{
+	u16 curr_prod_idx;
+
+	rte_atomic_fetch_sub_explicit(&wq->delta, num_wqebbs,
+				      rte_memory_order_seq_cst);
+	curr_prod_idx = (u16)(wq->prod_idx);
+	wq->prod_idx += num_wqebbs;
+	*prod_idx = MASKED_WQE_IDX(wq, curr_prod_idx);
+
+	return WQ_WQE_ADDR(wq, (u32)(*prod_idx));
+}
+
+void
+hinic3_set_sge(struct hinic3_sge *sge, uint64_t addr, u32 len)
+{
+	sge->hi_addr = upper_32_bits(addr);
+	sge->lo_addr = lower_32_bits(addr);
+	sge->len = len;
+}
drivers/net/hinic3/base/hinic3_wq.h new file +109 -0

109 lines not on this page · raw message

Signature · 1 line
2.45.1.windows.1
#145 lore raw

Re: [V5 10/18] net/hinic3: add context and work queue support

Quoted 194 lines · Feifei Wang

From: Xin Wang <[email protected]>

Work queue is used for cmdq and tx/rx buff description.

Nic business needs to configure cmdq context and txq/rxq

context. This patch adds data structures and function codes

for work queue and context.

---

 drivers/net/hinic3/base/hinic3_wq.c | 148 ++++++++++++++++++++++++++++
 drivers/net/hinic3/base/hinic3_wq.h | 109 ++++++++++++++++++++
 2 files changed, 257 insertions(+)
 create mode 100644 drivers/net/hinic3/base/hinic3_wq.c
 create mode 100644 drivers/net/hinic3/base/hinic3_wq.h
drivers/net/hinic3/base/hinic3_wq.c +0 -0

new file mode 100644

index 0000000000..9bccb10c9a

--- /dev/null

+++ b/drivers/net/hinic3/base/hinic3_wq.c

drivers/net/hinic3/base/hinic3_wq.c +75 -0
@@ -0,0 +1,148 @@
 
+/* SPDX-License-Identifier: BSD-3-Clause
 
+ * Copyright(c) 2025 Huawei Technologies Co., Ltd
 
+ */
 
+#include <rte_bus_pci.h>
 
+#include <rte_errno.h>
 
+#include <rte_ether.h>
 
+#include <rte_malloc.h>
 
+#include <rte_mbuf.h>
 
+#include <rte_mempool.h>
 
+#include <rte_pci.h>
 
+
 
+#include "hinic3_compat.h"
 
+#include "hinic3_hwdev.h"
 
+#include "hinic3_wq.h"
 
+
 
+static void
 
+free_wq_pages(struct hinic3_wq *wq)
 
+{
 
+	hinic3_memzone_free(wq->wq_mz);
 
+
 
+	wq->queue_buf_paddr = 0;
 
+	wq->queue_buf_vaddr = 0;
 
+}
 
+
 
+static int
 
+alloc_wq_pages(struct hinic3_hwdev *hwdev, struct hinic3_wq *wq, int qid)
 
+{
 
+	const struct rte_memzone *wq_mz;
 
+
 
+	wq_mz = hinic3_dma_zone_reserve(hwdev->eth_dev, "hinic3_wq_mz",
 
+					(uint16_t)qid, wq->wq_buf_size,
 
+					RTE_PGSIZE_256K, SOCKET_ID_ANY);
 
+	if (!wq_mz) {
 
+		PMD_DRV_LOG(ERR, "Allocate wq[%d] rq_mz failed", qid);
 
+		return -ENOMEM;
 
+	}
 
+
 
+	memset(wq_mz->addr, 0, wq->wq_buf_size);
 
+	wq->wq_mz = wq_mz;
 
+	wq->queue_buf_paddr = wq_mz->iova;
 
+	wq->queue_buf_vaddr = (u64)(u64 *)wq_mz->addr;
 
+
 
+	return 0;
 
+}
 
+
 
+void
 
+hinic3_put_wqe(struct hinic3_wq *wq, int num_wqebbs)
 
+{
 
+	wq->cons_idx += num_wqebbs;
 
+	rte_atomic_fetch_add_explicit(&wq->delta, num_wqebbs,
 
+				      rte_memory_order_seq_cst);
 
+}
 
+
 
+void *
 
+hinic3_read_wqe(struct hinic3_wq *wq, int num_wqebbs, u16 *cons_idx)
 
+{
 
+	u16 curr_cons_idx;
 
+
 
+	if ((rte_atomic_load_explicit(&wq->delta, rte_memory_order_seq_cst) +
 
+	     num_wqebbs) > wq->q_depth)
 
+		return NULL;
 
+
 
+	curr_cons_idx = (u16)(wq->cons_idx);
 
+
 
+	curr_cons_idx = MASKED_WQE_IDX(wq, curr_cons_idx);
 
+
 
+	*cons_idx = curr_cons_idx;
 
+
 
+	return WQ_WQE_ADDR(wq, (u32)(*cons_idx));
 
+}
 
+
 
+int
 
+hinic3_cmdq_alloc(struct hinic3_wq *wq, void *dev, int cmdq_blocks,
 
+		  u32 wq_buf_size, u32 wqebb_shift, u16 q_depth)
 
+{

drivers/net/hinic3/base/hinic3_wq.c line 80

Quoted 10 lines
drivers/net/hinic3/base/hinic3_wq.c +5 -0
 
+	struct hinic3_hwdev *hwdev = (struct hinic3_hwdev *)dev;
 
+	int i, j;
 
+	int err;
 
+
 
+	/* Validate q_depth is power of 2 & wqebb_size is not 0. */

Where the code of validate q_depth and wqebb_size?

Quoted 367 lines
drivers/net/hinic3/base/hinic3_wq.c +68 -0
+	for (i = 0; i < cmdq_blocks; i++) {
 
+		wq[i].wqebb_size = 1U << wqebb_shift;
 
+		wq[i].wqebb_shift = wqebb_shift;
 
+		wq[i].wq_buf_size = wq_buf_size;
 
+		wq[i].q_depth = q_depth;
 
+
 
+		err = alloc_wq_pages(hwdev, &wq[i], i);
 
+		if (err) {
 
+			PMD_DRV_LOG(ERR, "Failed to alloc CMDQ blocks");
 
+			goto cmdq_block_err;
 
+		}
 
+
 
+		wq[i].cons_idx = 0;
 
+		wq[i].prod_idx = 0;
 
+		rte_atomic_store_explicit(&wq[i].delta, q_depth,
 
+					  rte_memory_order_seq_cst);
 
+
 
+		wq[i].mask = q_depth - 1;
 
+	}
 
+
 
+	return 0;
 
+
 
+cmdq_block_err:
 
+	for (j = 0; j < i; j++)
 
+		free_wq_pages(&wq[j]);
 
+
 
+	return err;
 
+}
 
+
 
+void
 
+hinic3_cmdq_free(struct hinic3_wq *wq, int cmdq_blocks)
 
+{
 
+	int i;
 
+
 
+	for (i = 0; i < cmdq_blocks; i++)
 
+		free_wq_pages(&wq[i]);
 
+}
 
+
 
+void
 
+hinic3_wq_wqe_pg_clear(struct hinic3_wq *wq)
 
+{
 
+	wq->cons_idx = 0;
 
+	wq->prod_idx = 0;
 
+
 
+	memset((void *)wq->queue_buf_vaddr, 0, wq->wq_buf_size);
 
+}
 
+
 
+void *
 
+hinic3_get_wqe(struct hinic3_wq *wq, int num_wqebbs, u16 *prod_idx)
 
+{
 
+	u16 curr_prod_idx;
 
+
 
+	rte_atomic_fetch_sub_explicit(&wq->delta, num_wqebbs,
 
+				      rte_memory_order_seq_cst);
 
+	curr_prod_idx = (u16)(wq->prod_idx);
 
+	wq->prod_idx += num_wqebbs;
 
+	*prod_idx = MASKED_WQE_IDX(wq, curr_prod_idx);
 
+
 
+	return WQ_WQE_ADDR(wq, (u32)(*prod_idx));
 
+}
 
+
 
+void
 
+hinic3_set_sge(struct hinic3_sge *sge, uint64_t addr, u32 len)
 
+{
 
+	sge->hi_addr = upper_32_bits(addr);
 
+	sge->lo_addr = lower_32_bits(addr);
 
+	sge->len = len;
 
+}
drivers/net/hinic3/base/hinic3_wq.h +0 -0

new file mode 100644

index 0000000000..84d54c2aeb

--- /dev/null

+++ b/drivers/net/hinic3/base/hinic3_wq.h

drivers/net/hinic3/base/hinic3_wq.h +109 -0
@@ -0,0 +1,109 @@
 
+/* SPDX-License-Identifier: BSD-3-Clause
 
+ * Copyright(c) 2025 Huawei Technologies Co., Ltd
 
+ */
 
+
 
+#ifndef _HINIC3_WQ_H_
 
+#define _HINIC3_WQ_H_
 
+
 
+/* Use 0-level CLA, page size must be: SQ 16B(wqe) * 64k(max_q_depth). */
 
+#define HINIC3_DEFAULT_WQ_PAGE_SIZE 0x100000
 
+#define HINIC3_HW_WQ_PAGE_SIZE	    0x1000
 
+
 
+#define MASKED_WQE_IDX(wq, idx) ((idx) & (wq)->mask)
 
+
 
+#define WQ_WQE_ADDR(wq, idx)                                                           \
 
+	({                                                                             \
 
+		typeof(wq) __wq = (wq);                                                \
 
+		(void *)((u64)(__wq->queue_buf_vaddr) + ((idx) << __wq->wqebb_shift)); \
 
+	})
 
+
 
+struct hinic3_sge {
 
+	u32 hi_addr;
 
+	u32 lo_addr;
 
+	u32 len;
 
+};
 
+
 
+struct hinic3_wq {
 
+	/* The addresses are 64 bit in the HW. */
 
+	u64 queue_buf_vaddr;
 
+
 
+	u16 q_depth;
 
+	u16 mask;
 
+	RTE_ATOMIC(int32_t)delta;
 
+
 
+	u32 cons_idx;
 
+	u32 prod_idx;
 
+
 
+	u64 queue_buf_paddr;
 
+
 
+	u32 wqebb_size;
 
+	u32 wqebb_shift;
 
+
 
+	u32 wq_buf_size;
 
+
 
+	const struct rte_memzone *wq_mz;
 
+
 
+	u32 rsvd[5];
 
+};
 
+
 
+void hinic3_put_wqe(struct hinic3_wq *wq, int num_wqebbs);
 
+
 
+/**
 
+ * Read a WQE and update CI.
 
+ *
 
+ * @param[in] wq
 
+ * The work queue structure.
 
+ * @param[in] num_wqebbs
 
+ * The number of work queue elements to read.
 
+ * @param[out] cons_idx
 
+ * The updated consumer index.
 
+ *
 
+ * @return
 
+ * The address of WQE, or NULL if not enough elements are available.
 
+ */
 
+void *hinic3_read_wqe(struct hinic3_wq *wq, int num_wqebbs, u16 *cons_idx);
 
+
 
+/**
 
+ * Allocate command queue blocks and initialize related parameters.
 
+ *
 
+ * @param[in] wq
 
+ * The cmdq->wq structure.
 
+ * @param[in] dev
 
+ * The device context for the hardware.
 
+ * @param[in] cmdq_blocks
 
+ * The number of command queue blocks to allocate.
 
+ * @param[in] wq_buf_size
 
+ * The size of each work queue buffer.
 
+ * @param[in] wqebb_shift
 
+ * The shift value for determining the work queue element size.
 
+ * @param[in] q_depth
 
+ * The depth of each command queue.
 
+ *
 
+ * @return
 
+ * 0 on success, non-zero on failure.
 
+ */
 
+int hinic3_cmdq_alloc(struct hinic3_wq *wq, void *dev, int cmdq_blocks,
 
+		      u32 wq_buf_size, u32 wqebb_shift, u16 q_depth);
 
+
 
+void hinic3_cmdq_free(struct hinic3_wq *wq, int cmdq_blocks);
 
+
 
+void hinic3_wq_wqe_pg_clear(struct hinic3_wq *wq);
 
+
 
+/**
 
+ * Get WQE and update PI.
 
+ *
 
+ * @param[in] wq
 
+ * The cmdq->wq structure.
 
+ * @param[in] num_wqebbs
 
+ * The number of work queue elements to allocate.
 
+ * @param[out] prod_idx
 
+ * The updated producer index, masked according to the queue size.
 
+ *
 
+ * @return
 
+ * The address of the work queue element.
 
+ */
 
+void *hinic3_get_wqe(struct hinic3_wq *wq, int num_wqebbs, u16 *prod_idx);
 
+
 
+void hinic3_set_sge(struct hinic3_sge *sge, uint64_t addr, u32 len);
 
+
 
+#endif /* _HINIC3_WQ_H_ */
#146 lore raw

PATCH v5 11/18 net/hinic3: add a mailbox communication module

In reply to Feifei Wang

From: Yi Chen <[email protected]>

This patch adds support for mailbox of hinic3 PMD driver, mailbox is used for communication between PF/VF driver and MPU. This patch provides mailbox-related data structures and functional code.

---
 drivers/net/hinic3/base/hinic3_mbox.c | 1399 +++++++++++++++++++++++++
 drivers/net/hinic3/base/hinic3_mbox.h |  199 ++++
 2 files changed, 1598 insertions(+)
 create mode 100644 drivers/net/hinic3/base/hinic3_mbox.c
 create mode 100644 drivers/net/hinic3/base/hinic3_mbox.h
drivers/net/hinic3/base/hinic3_mbox.c new file +1399 -0
new file mode 100644
index 0000000000..845ab9cfc3
@@ -0,0 +1,1399 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2025 Huawei Technologies Co., Ltd
+ */
+
+#include <rte_atomic.h>
+
+#include "hinic3_compat.h"
+#include "hinic3_csr.h"
+#include "hinic3_eqs.h"
+#include "hinic3_hw_cfg.h"
+#include "hinic3_hwdev.h"
+#include "hinic3_hwif.h"
+#include "hinic3_mbox.h"
+#include "hinic3_mgmt.h"
+#include "hinic3_nic_event.h"
+
+#define HINIC3_MBOX_INT_DST_FUNC_SHIFT	    0
+#define HINIC3_MBOX_INT_DST_AEQN_SHIFT	    10
+#define HINIC3_MBOX_INT_SRC_RESP_AEQN_SHIFT 12
+#define HINIC3_MBOX_INT_STAT_DMA_SHIFT	    14
+/* The size of data to be send (unit of 4 bytes). */
+#define HINIC3_MBOX_INT_TX_SIZE_SHIFT 20
+/* SO_RO(strong order, relax order). */
+#define HINIC3_MBOX_INT_STAT_DMA_SO_RO_SHIFT 25
+#define HINIC3_MBOX_INT_WB_EN_SHIFT	     28
+
+#define HINIC3_MBOX_INT_DST_AEQN_MASK	    0x3
+#define HINIC3_MBOX_INT_SRC_RESP_AEQN_MASK  0x3
+#define HINIC3_MBOX_INT_STAT_DMA_MASK	    0x3F
+#define HINIC3_MBOX_INT_TX_SIZE_MASK	    0x1F
+#define HINIC3_MBOX_INT_STAT_DMA_SO_RO_MASK 0x3
+#define HINIC3_MBOX_INT_WB_EN_MASK	    0x1
+
+#define HINIC3_MBOX_INT_SET(val, field)           \
+	(((val) & HINIC3_MBOX_INT_##field##_MASK) \
+	 << HINIC3_MBOX_INT_##field##_SHIFT)
+
+enum hinic3_mbox_tx_status {
+	TX_NOT_DONE = 1,
+};
+
+#define HINIC3_MBOX_CTRL_TRIGGER_AEQE_SHIFT 0
+
+/*
+ * Specifies the issue request for the message data.
+ * 0 - Tx request is done;
+ * 1 - Tx request is in process.
+ */
+#define HINIC3_MBOX_CTRL_TX_STATUS_SHIFT 1
+#define HINIC3_MBOX_CTRL_DST_FUNC_SHIFT	 16
+
+#define HINIC3_MBOX_CTRL_TRIGGER_AEQE_MASK 0x1
+#define HINIC3_MBOX_CTRL_TX_STATUS_MASK	   0x1
+#define HINIC3_MBOX_CTRL_DST_FUNC_MASK	   0x1FFF
+
+#define HINIC3_MBOX_CTRL_SET(val, field)           \
+	(((val) & HINIC3_MBOX_CTRL_##field##_MASK) \
+	 << HINIC3_MBOX_CTRL_##field##_SHIFT)
+
+#define MBOX_SEGLEN_MASK \
+	HINIC3_MSG_HEADER_SET(HINIC3_MSG_HEADER_SEG_LEN_MASK, SEG_LEN)
+
+#define MBOX_MSG_POLLING_TIMEOUT 500000 /* Unit is 10us. */
+#define HINIC3_MBOX_COMP_TIME	 40000U
+
+#define MBOX_MAX_BUF_SZ	      2048UL
+#define MBOX_HEADER_SZ	      8
+#define HINIC3_MBOX_DATA_SIZE (MBOX_MAX_BUF_SZ - MBOX_HEADER_SZ)
+
+#define MBOX_TLP_HEADER_SZ 16
+
+/* Mbox size is 64B, 8B for mbox_header, 8B reserved. */
+#define MBOX_SEG_LEN	   48
+#define MBOX_SEG_LEN_ALIGN 4
+#define MBOX_WB_STATUS_LEN 16UL
+
+/* Mbox write back status is 16B, only first 4B is used. */
+#define MBOX_WB_STATUS_ERRCODE_MASK	 0xFFFF
+#define MBOX_WB_STATUS_MASK		 0xFF
+#define MBOX_WB_ERROR_CODE_MASK		 0xFF00
+#define MBOX_WB_STATUS_FINISHED_SUCCESS	 0xFF
+#define MBOX_WB_STATUS_FINISHED_WITH_ERR 0xFE
+#define MBOX_WB_STATUS_NOT_FINISHED	 0x00
+
+/* Determine the write back status. */
+#define MBOX_STATUS_FINISHED(wb) \
+	(((wb) & MBOX_WB_STATUS_MASK) != MBOX_WB_STATUS_NOT_FINISHED)
+#define MBOX_STATUS_SUCCESS(wb) \
+	(((wb) & MBOX_WB_STATUS_MASK) == MBOX_WB_STATUS_FINISHED_SUCCESS)
+#define MBOX_STATUS_ERRCODE(wb) ((wb) & MBOX_WB_ERROR_CODE_MASK)
+
+/* Indicate the value related to the sequence ID. */
+#define SEQ_ID_START_VAL 0
+#define SEQ_ID_MAX_VAL	 42
+
+#define DST_AEQ_IDX_DEFAULT_VAL 0
+#define SRC_AEQ_IDX_DEFAULT_VAL 0
+#define NO_DMA_ATTRIBUTE_VAL	0
+
+#define MBOX_MSG_NO_DATA_LEN 1
+
+/* Obtain the specified content of the mailbox. */
+#define MBOX_BODY_FROM_HDR(header) ((u8 *)(header) + MBOX_HEADER_SZ)
+#define MBOX_AREA(hwif) \
+	((hwif)->cfg_regs_base + HINIC3_FUNC_CSR_MAILBOX_DATA_OFF)
+
+#define IS_PF_OR_PPF_SRC(src_func_idx) ((src_func_idx) < HINIC3_MAX_PF_FUNCS)
+
+#define MBOX_RESPONSE_ERROR	  0x1
+#define MBOX_MSG_ID_MASK	  0xF
+#define MBOX_MSG_ID(func_to_func) ((func_to_func)->send_msg_id)
+#define MBOX_MSG_ID_INC(func_to_func)                             \
+	({                                                        \
+		typeof(func_to_func) __func = (func_to_func);     \
+		MBOX_MSG_ID(__func) = (MBOX_MSG_ID(__func) + 1) & \
+				      MBOX_MSG_ID_MASK;           \
+	})
+
+/* Max message counter waits to process for one function. */
+#define HINIC3_MAX_MSG_CNT_TO_PROCESS 10
+
+enum mbox_ordering_type {
+	STRONG_ORDER,
+};
+
+enum mbox_write_back_type {
+	WRITE_BACK = 1,
+};
+
+enum mbox_aeq_trig_type {
+	NOT_TRIGGER,
+	TRIGGER,
+};
+
+static int send_mbox_to_func(struct hinic3_mbox *func_to_func,
+			     enum hinic3_mod_type mod, u16 cmd, void *msg,
+			     u16 msg_len, u16 dst_func,
+			     enum hinic3_msg_direction_type direction,
+			     enum hinic3_msg_ack_type ack_type,
+			     struct mbox_msg_info *msg_info);
+static int send_tlp_mbox_to_func(struct hinic3_mbox *func_to_func,
+				 enum hinic3_mod_type mod, u16 cmd, void *msg,
+				 u16 msg_len, u16 dst_func,
+				 enum hinic3_msg_direction_type direction,
+				 enum hinic3_msg_ack_type ack_type,
+				 struct mbox_msg_info *msg_info);
+
+static int
+recv_vf_mbox_handler(struct hinic3_mbox *func_to_func,
+		     struct hinic3_recv_mbox *recv_mbox, void *buf_out,
+		     u16 *out_size, __rte_unused void *param)
+{
+	int err = 0;
+
+	/*
+	 * Invoke the corresponding processing function according to the type of
+	 * the received mailbox.
+	 */
+	switch (recv_mbox->mod) {
+	case HINIC3_MOD_COMM:
+		err = vf_handle_pf_comm_mbox(func_to_func->hwdev, func_to_func,
+					     recv_mbox->cmd, recv_mbox->mbox,
+					     recv_mbox->mbox_len, buf_out,
+					     out_size);
+		break;
+	case HINIC3_MOD_CFGM:
+		err = cfg_mbx_vf_proc_msg(func_to_func->hwdev,
+			func_to_func->hwdev->cfg_mgmt,
+			recv_mbox->cmd, recv_mbox->mbox, recv_mbox->mbox_len,
+			buf_out, out_size);
+		break;
+	case HINIC3_MOD_L2NIC:
+		err = hinic3_vf_event_handler(func_to_func->hwdev,
+			func_to_func->hwdev->cfg_mgmt,
+			recv_mbox->cmd, recv_mbox->mbox, recv_mbox->mbox_len,
+			buf_out, out_size);
+		break;
+	case HINIC3_MOD_HILINK:
+		err = hinic3_vf_mag_event_handler(func_to_func->hwdev,
+			func_to_func->hwdev->cfg_mgmt,
+			recv_mbox->cmd, recv_mbox->mbox, recv_mbox->mbox_len,
+			buf_out, out_size);
+		break;
+	default:
+		PMD_DRV_LOG(ERR, "No handler, mod: %d", recv_mbox->mod);
+		err = HINIC3_MBOX_VF_CMD_ERROR;
+		break;
+	}
+
+	return err;
+}
+
+/**
+ * Respond to the accept packet, construct a response message, and send it.
+ *
+ * @param[in] func_to_func
+ * Context for inter-function communication.
+ * @param[in] recv_mbox
+ * Pointer to the received inter-function mailbox structure.
+ * @param[in] err
+ * Error Code.
+ * @param[in] out_size
+ * Output Size.
+ * @param[in] src_func_idx
+ * Index of the source function.
+ */
+static void
+response_for_recv_func_mbox(struct hinic3_mbox *func_to_func,
+			    struct hinic3_recv_mbox *recv_mbox, int err,
+			    u16 out_size, u16 src_func_idx)
+{
+	struct mbox_msg_info msg_info = {0};
+
+	if (recv_mbox->ack_type == HINIC3_MSG_ACK) {
+		msg_info.msg_id = recv_mbox->msg_info.msg_id;
+		if (err)
+			msg_info.status = HINIC3_MBOX_PF_SEND_ERR;
+
+		/* Select the sending function based on the packet type. */
+		if (IS_TLP_MBX(src_func_idx))
+			send_tlp_mbox_to_func(func_to_func, recv_mbox->mod,
+					      recv_mbox->cmd,
+					      recv_mbox->buf_out, out_size,
+					      src_func_idx, HINIC3_MSG_RESPONSE,
+					      HINIC3_MSG_NO_ACK, &msg_info);
+		else
+			send_mbox_to_func(func_to_func, recv_mbox->mod,
+					  recv_mbox->cmd, recv_mbox->buf_out,
+					  out_size, src_func_idx,
+					  HINIC3_MSG_RESPONSE,
+					  HINIC3_MSG_NO_ACK, &msg_info);
+	}
+}
+
+static bool
+check_func_mbox_ack_first(u8 mod)
+{
+	return mod == HINIC3_MOD_HILINK;
+}
+
+static void
+recv_func_mbox_handler(struct hinic3_mbox *func_to_func,
+		       struct hinic3_recv_mbox *recv_mbox, u16 src_func_idx,
+		       void *param)
+{
+	struct hinic3_hwdev *hwdev = func_to_func->hwdev;
+	void *buf_out = recv_mbox->buf_out;
+	bool ack_first = false;
+	u16 out_size = MBOX_MAX_BUF_SZ;
+	int err = 0;
+	/* Check whether the response is the first ACK message. */
+	ack_first = check_func_mbox_ack_first(recv_mbox->mod);
+	if (ack_first && recv_mbox->ack_type == HINIC3_MSG_ACK) {
+		response_for_recv_func_mbox(func_to_func, recv_mbox, err,
+					    out_size, src_func_idx);
+	}
+
+	/* Processe mailbox information in the VF. */
+	if (HINIC3_IS_VF(hwdev)) {
+		err = recv_vf_mbox_handler(func_to_func, recv_mbox, buf_out,
+					   &out_size, param);
+	} else {
+		err = -EINVAL;
+		PMD_DRV_LOG(ERR,
+			"PMD doesn't support non-VF handle mailbox message");
+	}
+
+	if (!out_size || err)
+		out_size = MBOX_MSG_NO_DATA_LEN;
+
+	if (!ack_first && recv_mbox->ack_type == HINIC3_MSG_ACK) {
+		response_for_recv_func_mbox(func_to_func, recv_mbox, err,
+					    out_size, src_func_idx);
+	}
+}
+
+/**
+ * Processe mailbox responses from functions.
+ *
+ * @param[in] func_to_func
+ * Mailbox for inter-function communication.
+ * @param[in] recv_mbox
+ * Received mailbox message.
+ * @return
+ * 0 on success, non-zero on failure.
+ */
+static int
+resp_mbox_handler(struct hinic3_mbox *func_to_func,
+		  struct hinic3_recv_mbox *recv_mbox)
+{
+	int ret;
+	rte_spinlock_lock(&func_to_func->mbox_lock);
+	if (recv_mbox->msg_info.msg_id == func_to_func->send_msg_id &&
+	    func_to_func->event_flag == EVENT_START) {
+		func_to_func->event_flag = EVENT_SUCCESS;
+		ret = 0;
+	} else {
+		PMD_DRV_LOG(ERR,
+			    "Mbox response timeout, current send msg id(0x%x), "
+			    "recv msg id(0x%x), status(0x%x)",
+			    func_to_func->send_msg_id,
+			    recv_mbox->msg_info.msg_id,
+			    recv_mbox->msg_info.status);
+		ret = HINIC3_MSG_HANDLER_RES;
+	}
+	rte_spinlock_unlock(&func_to_func->mbox_lock);
+	return ret;
+}
+
+/**
+ * Check whether the received mailbox message segment is valid.
+ *
+ * @param[out] recv_mbox
+ * Received mailbox message.
+ * @param[in] mbox_header
+ * Mailbox header.
+ * @return
+ * The value true indicates valid, and the value false indicates invalid.
+ */
+static bool
+check_mbox_segment(struct hinic3_recv_mbox *recv_mbox, u64 mbox_header)
+{
+	u8 seq_id, seg_len, msg_id, mod;
+	u16 src_func_idx, cmd;
+
+	/* Get info from the mailbox header. */
+	seq_id = HINIC3_MSG_HEADER_GET(mbox_header, SEQID);
+	seg_len = HINIC3_MSG_HEADER_GET(mbox_header, SEG_LEN);
+	src_func_idx = HINIC3_MSG_HEADER_GET(mbox_header, SRC_GLB_FUNC_IDX);
+	msg_id = HINIC3_MSG_HEADER_GET(mbox_header, MSG_ID);
+	mod = HINIC3_MSG_HEADER_GET(mbox_header, MODULE);
+	cmd = HINIC3_MSG_HEADER_GET(mbox_header, CMD);
+
+	if (seq_id > SEQ_ID_MAX_VAL || seg_len > MBOX_SEG_LEN)
+		goto seg_err;
+
+	/* New message segment, which saves its information to recv_mbox. */
+	if (seq_id == 0) {
+		recv_mbox->seq_id = seq_id;
+		recv_mbox->msg_info.msg_id = msg_id;
+		recv_mbox->mod = mod;
+		recv_mbox->cmd = cmd;
+	} else {
+		if ((seq_id != recv_mbox->seq_id + 1) ||
+		    msg_id != recv_mbox->msg_info.msg_id ||
+		    mod != recv_mbox->mod || cmd != recv_mbox->cmd)
+			goto seg_err;
+
+		recv_mbox->seq_id = seq_id;
+	}
+
+	return true;
+
+seg_err:
+	PMD_DRV_LOG(ERR,
+		    "Mailbox segment check failed, src func id: 0x%x, "
+		    "front seg info: seq id: 0x%x, msg id: 0x%x, mod: 0x%x, "
+		    "cmd: 0x%x",
+		    src_func_idx, recv_mbox->seq_id, recv_mbox->msg_info.msg_id,
+		    recv_mbox->mod, recv_mbox->cmd);
+	PMD_DRV_LOG(ERR,
+		    "Current seg info: seg len: 0x%x, seq id: 0x%x, "
+		    "msg id: 0x%x, mod: 0x%x, cmd: 0x%x",
+		    seg_len, seq_id, msg_id, mod, cmd);
+
+	return false;
+}
+
+static int
+recv_mbox_handler(struct hinic3_mbox *func_to_func, void *header,
+		  struct hinic3_recv_mbox *recv_mbox, void *param)
+{
+	u64 mbox_header = *((u64 *)header);
+	void *mbox_body = MBOX_BODY_FROM_HDR(header);
+	u16 src_func_idx;
+	int pos;
+	u8 seq_id;
+	/* Obtain information from the mailbox header. */
+	seq_id = HINIC3_MSG_HEADER_GET(mbox_header, SEQID);
+	src_func_idx = HINIC3_MSG_HEADER_GET(mbox_header, SRC_GLB_FUNC_IDX);
+
+	if (!check_mbox_segment(recv_mbox, mbox_header)) {
+		recv_mbox->seq_id = SEQ_ID_MAX_VAL;
+		return HINIC3_MSG_HANDLER_RES;
+	}
+
+	pos = seq_id * MBOX_SEG_LEN;
+	memcpy((void *)((u8 *)recv_mbox->mbox + pos), (void *)mbox_body,
+	       (size_t)HINIC3_MSG_HEADER_GET(mbox_header, SEG_LEN));
+
+	if (!HINIC3_MSG_HEADER_GET(mbox_header, LAST))
+		return HINIC3_MSG_HANDLER_RES;
+	/* Setting the information about the recv mailbox. */
+	recv_mbox->cmd = HINIC3_MSG_HEADER_GET(mbox_header, CMD);
+	recv_mbox->mod = HINIC3_MSG_HEADER_GET(mbox_header, MODULE);
+	recv_mbox->mbox_len = HINIC3_MSG_HEADER_GET(mbox_header, MSG_LEN);
+	recv_mbox->ack_type = HINIC3_MSG_HEADER_GET(mbox_header, NO_ACK);
+	recv_mbox->msg_info.msg_id = HINIC3_MSG_HEADER_GET(mbox_header, MSG_ID);
+	recv_mbox->msg_info.status = HINIC3_MSG_HEADER_GET(mbox_header, STATUS);
+	recv_mbox->seq_id = SEQ_ID_MAX_VAL;
+
+	/*
+	 * If the received message is a response message, call the mbox response
+	 * processing function.
+	 */
+	if (HINIC3_MSG_HEADER_GET(mbox_header, DIRECTION) ==
+	    HINIC3_MSG_RESPONSE) {
+		return resp_mbox_handler(func_to_func, recv_mbox);
+	}
+
+	recv_func_mbox_handler(func_to_func, recv_mbox, src_func_idx, param);
+	return HINIC3_MSG_HANDLER_RES;
+}
+
+static inline int
+hinic3_mbox_get_index(int func)
+{
+	return (func == HINIC3_MGMT_SRC_ID) ? HINIC3_MBOX_MPU_INDEX
+					    : HINIC3_MBOX_PF_INDEX;
+}
+
+int
+hinic3_mbox_func_aeqe_handler(void *handle, u8 *header, __rte_unused u8 size,
+			      void *param)
+{
+	struct hinic3_mbox *func_to_func = NULL;
+	struct hinic3_recv_mbox *recv_mbox = NULL;
+	u64 mbox_header = *((u64 *)header);
+	u64 src, dir;
+	/* Obtain the mailbox for communication between functions. */
+	func_to_func = ((struct hinic3_hwdev *)handle)->func_to_func;
+
+	dir = HINIC3_MSG_HEADER_GET(mbox_header, DIRECTION);
+	src = HINIC3_MSG_HEADER_GET(mbox_header, SRC_GLB_FUNC_IDX);
+
+	src = hinic3_mbox_get_index((int)src);
+	recv_mbox = (dir == HINIC3_MSG_DIRECT_SEND)
+			    ? &func_to_func->mbox_send[src]
+			    : &func_to_func->mbox_resp[src];
+	/* Processing Received Mailbox info. */
+	return recv_mbox_handler(func_to_func, (u64 *)header, recv_mbox, param);
+}
+
+static void
+clear_mbox_status(struct hinic3_send_mbox *mbox)
+{
+	*mbox->wb_status = 0;
+
+	/* Clear mailbox write back status. */
+	rte_atomic_thread_fence(rte_memory_order_release);
+}
+
+static void
+mbox_copy_header(struct hinic3_send_mbox *mbox, u64 *header)
+{
+	u32 *data = (u32 *)header;
+	u32 i, idx_max = MBOX_HEADER_SZ / sizeof(u32);
+
+	for (i = 0; i < idx_max; i++) {
+		rte_write32(cpu_to_be32(*(data + i)),
+			    mbox->data + i * sizeof(u32));
+	}
+}
+
+#define MBOX_DMA_MSG_INIT_XOR_VAL 0x5a5a5a5a
+static u32
+mbox_dma_msg_xor(u32 *data, u16 msg_len)
+{
+	u32 xor = MBOX_DMA_MSG_INIT_XOR_VAL;
+	u16 dw_len = msg_len / sizeof(u32);
+	u16 i;
+
+	for (i = 0; i < dw_len; i++)
+		xor ^= data[i];
+
+	return xor;
+}
+
+static void
+mbox_copy_send_data_addr(struct hinic3_send_mbox *mbox, u16 seg_len)
+{
+	u32 addr_h, addr_l, xor;
+
+	xor = mbox_dma_msg_xor(mbox->sbuff_vaddr, seg_len);
+	addr_h = upper_32_bits(mbox->sbuff_paddr);
+	addr_l = lower_32_bits(mbox->sbuff_paddr);
+
+	rte_write32(cpu_to_be32(xor), mbox->data + MBOX_HEADER_SZ);
+	rte_write32(cpu_to_be32(addr_h),
+		    mbox->data + MBOX_HEADER_SZ + sizeof(u32));
+	rte_write32(cpu_to_be32(addr_l),
+		    mbox->data + MBOX_HEADER_SZ + 0x2 * sizeof(u32));
+	rte_write32(cpu_to_be32((u32)seg_len),
+		    mbox->data + MBOX_HEADER_SZ + 0x3 * sizeof(u32));
+	/* Reserved field. */
+	rte_write32(0, mbox->data + MBOX_HEADER_SZ + 0x4 * sizeof(u32));
+	rte_write32(0, mbox->data + MBOX_HEADER_SZ + 0x5 * sizeof(u32));
+}
+
+static void
+mbox_copy_send_data(struct hinic3_send_mbox *mbox, void *seg, u16 seg_len)
+{
+	u32 *data = seg;
+	u32 data_len, chk_sz = sizeof(u32);
+	u32 i, idx_max;
+	u8 mbox_max_buf[MBOX_SEG_LEN] = {0};
+
+	/* The mbox message should be aligned in 4 bytes. */
+	if (seg_len % chk_sz) {
+		if (seg_len <= sizeof(mbox_max_buf)) {
+			memcpy(mbox_max_buf, seg, seg_len);
+			data = (u32 *)mbox_max_buf;
+		} else {
+			PMD_DRV_LOG(ERR, "mbox_max_buf overflow: seg_len=%u.", seg_len);
+			return;
+		}
+	}
+
+	data_len = seg_len;
+	idx_max = RTE_ALIGN(data_len, chk_sz) / chk_sz;
+
+	for (i = 0; i < idx_max; i++) {
+		rte_write32(cpu_to_be32(*(data + i)),
+			    mbox->data + MBOX_HEADER_SZ + i * sizeof(u32));
+	}
+}
+
+static void
+write_mbox_msg_attr(struct hinic3_mbox *func_to_func, u16 dst_func,
+		    u16 dst_aeqn, u16 seg_len)
+{
+	u32 mbox_int, mbox_ctrl;
+
+	/* If VF, function ids must self-learning by HW(PPF=1 PF=0). */
+	if (HINIC3_IS_VF(func_to_func->hwdev) &&
+	    dst_func != HINIC3_MGMT_SRC_ID) {
+		if (dst_func == HINIC3_HWIF_PPF_IDX(func_to_func->hwdev->hwif))
+			dst_func = 1;
+		else
+			dst_func = 0;
+	}
+	/* Set the interrupt attribute of the mailbox. */
+	mbox_int = HINIC3_MBOX_INT_SET(dst_aeqn, DST_AEQN) |
+		   HINIC3_MBOX_INT_SET(0, SRC_RESP_AEQN) |
+		   HINIC3_MBOX_INT_SET(NO_DMA_ATTRIBUTE_VAL, STAT_DMA) |
+		   HINIC3_MBOX_INT_SET(RTE_ALIGN(seg_len + MBOX_HEADER_SZ,
+						 MBOX_SEG_LEN_ALIGN) >>
+					       2,
+				       TX_SIZE) |
+		   HINIC3_MBOX_INT_SET(STRONG_ORDER, STAT_DMA_SO_RO) |
+		   HINIC3_MBOX_INT_SET(WRITE_BACK, WB_EN);
+
+	/* The interrupt attribute is written to the interrupt register. */
+	hinic3_hwif_write_reg(func_to_func->hwdev->hwif,
+			      HINIC3_FUNC_CSR_MAILBOX_INT_OFFSET_OFF, mbox_int);
+
+	rte_atomic_thread_fence(rte_memory_order_release); /**< Writing the mbox intr attributes */
+
+	/* Set the control attributes of the mailbox and write to register. */
+	mbox_ctrl = HINIC3_MBOX_CTRL_SET(TX_NOT_DONE, TX_STATUS);
+	mbox_ctrl |= HINIC3_MBOX_CTRL_SET(NOT_TRIGGER, TRIGGER_AEQE);
+	mbox_ctrl |= HINIC3_MBOX_CTRL_SET(dst_func, DST_FUNC);
+	hinic3_hwif_write_reg(func_to_func->hwdev->hwif,
+			      HINIC3_FUNC_CSR_MAILBOX_CONTROL_OFF, mbox_ctrl);
+}
+
+/**
+ * Read the value of the mailbox register of the hardware device.
+ *
+ * @param[in] hwdev
+ * Pointer to hardware device structure.
+ */
+static void
+dump_mbox_reg(struct hinic3_hwdev *hwdev)
+{
+	u32 val;
+	/* Read the value of the MBOX control register. */
+	val = hinic3_hwif_read_reg(hwdev->hwif,
+				   HINIC3_FUNC_CSR_MAILBOX_CONTROL_OFF);
+	PMD_DRV_LOG(ERR, "Mailbox control reg: 0x%x", val);
+	/* Read the value of the MBOX interrupt offset register. */
+	val = hinic3_hwif_read_reg(hwdev->hwif,
+				   HINIC3_FUNC_CSR_MAILBOX_INT_OFFSET_OFF);
+	PMD_DRV_LOG(ERR, "Mailbox interrupt offset: 0x%x", val);
+}
+
+static u16
+get_mbox_status(struct hinic3_send_mbox *mbox)
+{
+	/* Write back is 16B, but only use first 4B. */
+	u64 wb_val = be64_to_cpu(*mbox->wb_status);
+
+	rte_atomic_thread_fence(rte_memory_order_acquire); /**< Verify reading before check. */
+
+	return (u16)(wb_val & MBOX_WB_STATUS_ERRCODE_MASK);
+}
+
+/**
+ * Sending Mailbox Message Segment.
+ *
+ * @param[in] func_to_func
+ * Mailbox for inter-function communication.
+ * @param[in] header
+ * Mailbox header.
+ * @param[in] dst_func
+ * Indicate destination func.
+ * @param[in] seg
+ * Segment data to be sent.
+ * @param[in] seg_len
+ * Length of the segment to be sent.
+ * @param[in] msg_info
+ * Indicate the message information.
+ * @return
+ * 0 on success, non-zero on failure.
+ */
+static int
+send_mbox_seg(struct hinic3_mbox *func_to_func, u64 header, u16 dst_func,
+	      void *seg, u16 seg_len, __rte_unused void *msg_info)
+{
+	struct hinic3_send_mbox *send_mbox = &func_to_func->send_mbox;
+	struct hinic3_hwdev *hwdev = func_to_func->hwdev;
+	u8 num_aeqs = hwdev->hwif->attr.num_aeqs;
+	u16 dst_aeqn, wb_status = 0, errcode;
+	u16 seq_dir = HINIC3_MSG_HEADER_GET(header, DIRECTION);
+	u32 cnt = 0;
+
+	/* Mbox to mgmt cpu, hardware doesn't care dst aeq id. */
+	if (num_aeqs >= 2)
+		dst_aeqn = (seq_dir == HINIC3_MSG_DIRECT_SEND)
+				   ? HINIC3_ASYNC_MSG_AEQ
+				   : HINIC3_MBOX_RSP_MSG_AEQ;
+	else
+		dst_aeqn = 0;
+
+	clear_mbox_status(send_mbox);
+	mbox_copy_header(send_mbox, &header);
+	mbox_copy_send_data(send_mbox, seg, seg_len);
+
+	/* Set mailbox msg seg len. */
+	write_mbox_msg_attr(func_to_func, dst_func, dst_aeqn, seg_len);
+	rte_atomic_thread_fence(rte_memory_order_release); /**< Writing the mbox msg attributes. */
+
+	/* Wait until the status of the mailbox changes to Complete. */
+	while (cnt < MBOX_MSG_POLLING_TIMEOUT) {
+		wb_status = get_mbox_status(send_mbox);
+		if (MBOX_STATUS_FINISHED(wb_status))
+			break;
+
+		rte_delay_us(10);
+		cnt++;
+	}
+
+	if (cnt == MBOX_MSG_POLLING_TIMEOUT) {
+		PMD_DRV_LOG(ERR,
+			    "Send mailbox segment timeout, wb status: 0x%x",
+			    wb_status);
+		dump_mbox_reg(hwdev);
+		return -ETIMEDOUT;
+	}
+
+	if (!MBOX_STATUS_SUCCESS(wb_status)) {
+		PMD_DRV_LOG(ERR,
+			    "Send mailbox segment to function %d error, wb "
+			    "status: 0x%x",
+			    dst_func, wb_status);
+		errcode = MBOX_STATUS_ERRCODE(wb_status);
+		return errcode ? errcode : -EFAULT;
+	}
+
+	return 0;
+}
+
+static int
+send_tlp_mbox_seg(struct hinic3_mbox *func_to_func, u64 header, u16 dst_func,
+		  void *seg, u16 seg_len, __rte_unused void *msg_info)
+{
+	struct hinic3_send_mbox *send_mbox = &func_to_func->send_mbox;
+	struct hinic3_hwdev *hwdev = func_to_func->hwdev;
+	u8 num_aeqs = hwdev->hwif->attr.num_aeqs;
+	u16 dst_aeqn, errcode, wb_status = 0;
+	u16 seq_dir = HINIC3_MSG_HEADER_GET(header, DIRECTION);
+	u32 cnt = 0;
+
+	/* Mbox to mgmt cpu, hardware doesn't care dst aeq id. */
+	if (num_aeqs >= 2)
+		dst_aeqn = (seq_dir == HINIC3_MSG_DIRECT_SEND)
+				   ? HINIC3_ASYNC_MSG_AEQ
+				   : HINIC3_MBOX_RSP_MSG_AEQ;
+	else
+		dst_aeqn = 0;
+
+	clear_mbox_status(send_mbox);
+	mbox_copy_header(send_mbox, &header);
+
+	/* Copy data to DMA buffer. */
+	memcpy((void *)send_mbox->sbuff_vaddr, (void *)seg, (size_t)seg_len);
+
+	/*
+	 * Copy data address to mailbox ctrl CSR(Control and Status Register).
+	 */
+	mbox_copy_send_data_addr(send_mbox, seg_len);
+
+	/* Set mailbox msg header size. */
+	write_mbox_msg_attr(func_to_func, dst_func, dst_aeqn,
+			    MBOX_TLP_HEADER_SZ);
+
+	rte_atomic_thread_fence(rte_memory_order_release); /**< Writing the mbox msg attributes. */
+
+	/* Wait until the status of the mailbox changes to Complete. */
+	while (cnt < MBOX_MSG_POLLING_TIMEOUT) {
+		wb_status = get_mbox_status(send_mbox);
+		if (MBOX_STATUS_FINISHED(wb_status))
+			break;
+
+		rte_delay_us(10);
+		cnt++;
+	}
+
+	if (cnt == MBOX_MSG_POLLING_TIMEOUT) {
+		PMD_DRV_LOG(ERR,
+			    "Send mailbox segment timeout, wb status: 0x%x",
+			    wb_status);
+		dump_mbox_reg(hwdev);
+		return -ETIMEDOUT;
+	}
+
+	if (!MBOX_STATUS_SUCCESS(wb_status)) {
+		PMD_DRV_LOG(ERR,
+			    "Send mailbox segment to function %d error, wb "
+			    "status: 0x%x",
+			    dst_func, wb_status);
+		errcode = MBOX_STATUS_ERRCODE(wb_status);
+		return errcode ? errcode : -EFAULT;
+	}
+
+	return 0;
+}
+
+static int
+send_mbox_to_func(struct hinic3_mbox *func_to_func, enum hinic3_mod_type mod,
+		  u16 cmd, void *msg, u16 msg_len, u16 dst_func,
+		  enum hinic3_msg_direction_type direction,
+		  enum hinic3_msg_ack_type ack_type,
+		  struct mbox_msg_info *msg_info)
+{
+	int err = 0;
+	u32 seq_id = 0;
+	u16 seg_len = MBOX_SEG_LEN;
+	u16 rsp_aeq_id, left = msg_len;
+	u8 *msg_seg = (u8 *)msg;
+	u64 header = 0;
+
+	rsp_aeq_id = HINIC3_MBOX_RSP_MSG_AEQ;
+
+	err = hinic3_mutex_lock(&func_to_func->msg_send_mutex);
+	if (err)
+		return err;
+
+	/* Set the header message. */
+	header = HINIC3_MSG_HEADER_SET(msg_len, MSG_LEN) |
+		 HINIC3_MSG_HEADER_SET(mod, MODULE) |
+		 HINIC3_MSG_HEADER_SET(seg_len, SEG_LEN) |
+		 HINIC3_MSG_HEADER_SET(ack_type, NO_ACK) |
+		 HINIC3_MSG_HEADER_SET(HINIC3_DATA_INLINE, DATA_TYPE) |
+		 HINIC3_MSG_HEADER_SET(SEQ_ID_START_VAL, SEQID) |
+		 HINIC3_MSG_HEADER_SET(NOT_LAST_SEGMENT, LAST) |
+		 HINIC3_MSG_HEADER_SET(direction, DIRECTION) |
+		 HINIC3_MSG_HEADER_SET(cmd, CMD) |
+		 /* The VF's offset to it's associated PF. */
+		 HINIC3_MSG_HEADER_SET(msg_info->msg_id, MSG_ID) |
+		 HINIC3_MSG_HEADER_SET(rsp_aeq_id, AEQ_ID) |
+		 HINIC3_MSG_HEADER_SET(HINIC3_MSG_FROM_MBOX, SOURCE) |
+		 HINIC3_MSG_HEADER_SET(!!msg_info->status, STATUS);
+	/* Loop until all messages are sent. */
+	while (!(HINIC3_MSG_HEADER_GET(header, LAST))) {
+		if (left <= MBOX_SEG_LEN) {
+			header &= ~MBOX_SEGLEN_MASK;
+			header |= HINIC3_MSG_HEADER_SET(left, SEG_LEN);
+			header |= HINIC3_MSG_HEADER_SET(LAST_SEGMENT, LAST);
+
+			seg_len = left;
+		}
+
+		err = send_mbox_seg(func_to_func, header, dst_func, msg_seg,
+				    seg_len, msg_info);
+		if (err) {
+			PMD_DRV_LOG(ERR,
+				    "Send mbox seg failed, seq_id: 0x%" PRIx64,
+				    HINIC3_MSG_HEADER_GET(header, SEQID));
+
+			goto send_err;
+		}
+
+		left -= MBOX_SEG_LEN;
+		msg_seg += MBOX_SEG_LEN;
+
+		seq_id++;
+		header &= ~(HINIC3_MSG_HEADER_SET(HINIC3_MSG_HEADER_SEQID_MASK,
+						  SEQID));
+		header |= HINIC3_MSG_HEADER_SET(seq_id, SEQID);
+	}
+
+send_err:
+	hinic3_mutex_unlock(&func_to_func->msg_send_mutex);
+
+	return err;
+}
+
+static int
+send_tlp_mbox_to_func(struct hinic3_mbox *func_to_func,
+		      enum hinic3_mod_type mod, u16 cmd, void *msg, u16 msg_len,
+		      u16 dst_func, enum hinic3_msg_direction_type direction,
+		      enum hinic3_msg_ack_type ack_type,
+		      struct mbox_msg_info *msg_info)
+{
+	struct hinic3_hwdev *hwdev = func_to_func->hwdev;
+	u8 *msg_seg = (u8 *)msg;
+	int err = 0;
+	u16 rsp_aeq_id;
+	u64 header = 0;
+
+	rsp_aeq_id = HINIC3_MBOX_RSP_MSG_AEQ;
+
+	err = hinic3_mutex_lock(&func_to_func->msg_send_mutex);
+	if (err)
+		return err;
+
+	/* Set the header message. */
+	header = HINIC3_MSG_HEADER_SET(MBOX_TLP_HEADER_SZ, MSG_LEN) |
+		 HINIC3_MSG_HEADER_SET(MBOX_TLP_HEADER_SZ, SEG_LEN) |
+		 HINIC3_MSG_HEADER_SET(mod, MODULE) |
+		 HINIC3_MSG_HEADER_SET(LAST_SEGMENT, LAST) |
+		 HINIC3_MSG_HEADER_SET(ack_type, NO_ACK) |
+		 HINIC3_MSG_HEADER_SET(HINIC3_DATA_DMA, DATA_TYPE) |
+		 HINIC3_MSG_HEADER_SET(SEQ_ID_START_VAL, SEQID) |
+		 HINIC3_MSG_HEADER_SET(direction, DIRECTION) |
+		 HINIC3_MSG_HEADER_SET(cmd, CMD) |
+		 /* The VF's offset to it's associated PF. */
+		 HINIC3_MSG_HEADER_SET(msg_info->msg_id, MSG_ID) |
+		 HINIC3_MSG_HEADER_SET(rsp_aeq_id, AEQ_ID) |
+		 HINIC3_MSG_HEADER_SET(HINIC3_MSG_FROM_MBOX, SOURCE) |
+		 HINIC3_MSG_HEADER_SET(!!msg_info->status, STATUS) |
+		 HINIC3_MSG_HEADER_SET(hinic3_global_func_id(hwdev),
+				       SRC_GLB_FUNC_IDX);
+
+	/* Send a message. */
+	err = send_tlp_mbox_seg(func_to_func, header, dst_func, msg_seg,
+				msg_len, msg_info);
+	if (err) {
+		PMD_DRV_LOG(ERR, "Send mbox seg failed, seq_id: 0x%" PRIx64,
+			    HINIC3_MSG_HEADER_GET(header, SEQID));
+	}
+
+	hinic3_mutex_unlock(&func_to_func->msg_send_mutex);
+
+	return err;
+}
+
+/**
+ * Set mailbox F2F(Function to Function) event status.
+ *
+ * @param[out] func_to_func
+ * Context for inter-function communication.
+ * @param[in] event_flag
+ * Event status enumerated value.
+ */
+static void
+set_mbox_to_func_event(struct hinic3_mbox *func_to_func,
+		       enum mbox_event_state event_flag)
+{
+	rte_spinlock_lock(&func_to_func->mbox_lock);
+	func_to_func->event_flag = event_flag;
+	rte_spinlock_unlock(&func_to_func->mbox_lock);
+}
+
+/**
+ * Send data from one function to another and receive responses.
+ *
+ * @param[in] func_to_func
+ * Context for inter-function communication.
+ * @param[in] mod
+ * Command queue module type.
+ * @param[in] cmd
+ * Indicate the command to be executed.
+  the command to be executed.
+ * @param[in] dst_func
+ * Indicate destination func.
+ * @param[in] buf_in
+ * Pointer to the input buffer.
+ * @param[in] in_size
+ * Input buffer size.
+ * @param[out] buf_out
+ * Pointer to the output buffer.
+ * @param[out] out_size
+ * Output buffer size.
+ * @param[in] timeout
+ * Timeout interval for waiting for a response.
+ * @return
+ * 0 on success, non-zero on failure.
+ */
+static int
+hinic3_mbox_to_func(struct hinic3_mbox *func_to_func, enum hinic3_mod_type mod,
+		    u16 cmd, u16 dst_func, void *buf_in, u16 in_size,
+		    void *buf_out, u16 *out_size, u32 timeout)
+{
+	/* Use mbox_resp to hole data which responded from other function. */
+	struct hinic3_recv_mbox *mbox_for_resp = NULL;
+	struct mbox_msg_info msg_info = {0};
+	struct hinic3_eq *aeq = NULL;
+	u16 mbox_rsp_idx;
+	u32 time;
+	int err;
+
+	mbox_rsp_idx = (u16)hinic3_mbox_get_index(dst_func);
+	mbox_for_resp = &func_to_func->mbox_resp[mbox_rsp_idx];
+
+	err = hinic3_mutex_lock(&func_to_func->mbox_send_mutex);
+	if (err)
+		return err;
+
+	/* Set message ID and start event. */
+	msg_info.msg_id = MBOX_MSG_ID_INC(func_to_func);
+	set_mbox_to_func_event(func_to_func, EVENT_START);
+
+	/* Select a function to send messages based on the dst_func type. */
+	if (IS_TLP_MBX(dst_func))
+		err = send_tlp_mbox_to_func(func_to_func,
+			mod, cmd, buf_in, in_size, dst_func,
+			HINIC3_MSG_DIRECT_SEND, HINIC3_MSG_ACK, &msg_info);
+	else
+		err = send_mbox_to_func(func_to_func, mod, cmd, buf_in, in_size,
+					dst_func, HINIC3_MSG_DIRECT_SEND,
+					HINIC3_MSG_ACK, &msg_info);
+
+	if (err) {
+		PMD_DRV_LOG(ERR, "Send mailbox failed, msg_id: %d",
+			    msg_info.msg_id);
+		set_mbox_to_func_event(func_to_func, EVENT_FAIL);
+		goto send_err;
+	}
+
+	/* Wait for the response message. */
+	time = msecs_to_jiffies(timeout ? timeout : HINIC3_MBOX_COMP_TIME);
+	aeq = &func_to_func->hwdev->aeqs->aeq[HINIC3_MBOX_RSP_MSG_AEQ];
+	err = hinic3_aeq_poll_msg(aeq, time, NULL);
+	if (err) {
+		set_mbox_to_func_event(func_to_func, EVENT_TIMEOUT);
+		PMD_DRV_LOG(ERR, "Send mailbox message time out");
+		err = -ETIMEDOUT;
+		goto send_err;
+	}
+
+	/* Check whether mod and command of the rsp message match the sent message. */
+	if (mod != mbox_for_resp->mod || cmd != mbox_for_resp->cmd) {
+		PMD_DRV_LOG(ERR,
+			    "Invalid response mbox message, mod: 0x%x, cmd: "
+			    "0x%x, expect mod: 0x%x, cmd: 0x%x",
+			    mbox_for_resp->mod, mbox_for_resp->cmd, mod, cmd);
+		err = -EFAULT;
+		goto send_err;
+	}
+
+	/* Check the response status. */
+	if (mbox_for_resp->msg_info.status) {
+		err = mbox_for_resp->msg_info.status;
+		goto send_err;
+	}
+
+	/* Check whether the length of the response message is valid. */
+	if (buf_out && out_size) {
+		if (*out_size < mbox_for_resp->mbox_len) {
+			PMD_DRV_LOG(ERR,
+				"Invalid response mbox message length: %d for "
+				"mod: %d cmd: %d, should less than: %d",
+				mbox_for_resp->mbox_len, mod, cmd, *out_size);
+			err = -EFAULT;
+			goto send_err;
+		}
+
+		if (mbox_for_resp->mbox_len)
+			memcpy(buf_out, mbox_for_resp->mbox,
+			       (size_t)(mbox_for_resp->mbox_len));
+
+		*out_size = mbox_for_resp->mbox_len;
+	}
+
+send_err:
+	hinic3_mutex_unlock(&func_to_func->mbox_send_mutex);
+
+	return err;
+}
+
+static int
+mbox_func_params_valid(__rte_unused struct hinic3_mbox *func_to_func,
+		       void *buf_in, u16 in_size)
+{
+	if (!buf_in || !in_size)
+		return -EINVAL;
+
+	if (in_size > HINIC3_MBOX_DATA_SIZE) {
+		PMD_DRV_LOG(ERR, "Mbox msg len(%d) exceed limit(%" PRIu64 ")",
+			    in_size, HINIC3_MBOX_DATA_SIZE);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int
+hinic3_mbox_to_func_no_ack(struct hinic3_hwdev *hwdev, u16 func_idx,
+			   enum hinic3_mod_type mod, u16 cmd, void *buf_in,
+			   u16 in_size)
+{
+	struct hinic3_mbox *func_to_func = hwdev->func_to_func;
+	struct mbox_msg_info msg_info = {0};
+	int err;
+
+	err = mbox_func_params_valid(hwdev->func_to_func, buf_in, in_size);
+	if (err)
+		return err;
+
+	err = hinic3_mutex_lock(&func_to_func->mbox_send_mutex);
+	if (err)
+		return err;
+
+	if (IS_TLP_MBX(func_idx))
+		err = send_tlp_mbox_to_func(func_to_func,
+			mod, cmd, buf_in, in_size, func_idx,
+			HINIC3_MSG_DIRECT_SEND, HINIC3_MSG_NO_ACK, &msg_info);
+	else
+		err = send_mbox_to_func(func_to_func, mod, cmd, buf_in, in_size,
+					func_idx, HINIC3_MSG_DIRECT_SEND,
+					HINIC3_MSG_NO_ACK, &msg_info);
+	if (err)
+		PMD_DRV_LOG(ERR, "Send mailbox no ack failed");
+
+	hinic3_mutex_unlock(&func_to_func->mbox_send_mutex);
+
+	return err;
+}
+
+int
+hinic3_send_mbox_to_mgmt(struct hinic3_hwdev *hwdev, enum hinic3_mod_type mod,
+			 u16 cmd, void *buf_in, u16 in_size, void *buf_out,
+			 u16 *out_size, u32 timeout)
+{
+	struct hinic3_mbox *func_to_func = hwdev->func_to_func;
+	int err;
+	/* Verify the validity of the input parameters. */
+	err = mbox_func_params_valid(func_to_func, buf_in, in_size);
+	if (err)
+		return err;
+
+	return hinic3_mbox_to_func(func_to_func, mod, cmd, HINIC3_MGMT_SRC_ID,
+				   buf_in, in_size, buf_out, out_size, timeout);
+}
+
+void
+hinic3_response_mbox_to_mgmt(struct hinic3_hwdev *hwdev,
+			     enum hinic3_mod_type mod, u16 cmd, void *buf_in,
+			     u16 in_size, u16 msg_id)
+{
+	struct mbox_msg_info msg_info;
+	u16 dst_func;
+
+	msg_info.msg_id = (u8)msg_id;
+	msg_info.status = 0;
+	dst_func = HINIC3_MGMT_SRC_ID;
+
+	if (IS_TLP_MBX(dst_func))
+		send_tlp_mbox_to_func(hwdev->func_to_func, mod, cmd, buf_in,
+				      in_size, HINIC3_MGMT_SRC_ID,
+				      HINIC3_MSG_RESPONSE, HINIC3_MSG_NO_ACK,
+				      &msg_info);
+	else
+		send_mbox_to_func(hwdev->func_to_func, mod, cmd, buf_in,
+				  in_size, HINIC3_MGMT_SRC_ID,
+				  HINIC3_MSG_RESPONSE, HINIC3_MSG_NO_ACK,
+				  &msg_info);
+}
+
+int
+hinic3_send_mbox_to_mgmt_no_ack(struct hinic3_hwdev *hwdev,
+				enum hinic3_mod_type mod, u16 cmd, void *buf_in,
+				u16 in_size)
+{
+	struct hinic3_mbox *func_to_func = hwdev->func_to_func;
+	int err;
+
+	err = mbox_func_params_valid(func_to_func, buf_in, in_size);
+	if (err)
+		return err;
+
+	return hinic3_mbox_to_func_no_ack(hwdev, HINIC3_MGMT_SRC_ID, mod, cmd,
+					  buf_in, in_size);
+}
+
+int
+hinic3_mbox_to_pf(struct hinic3_hwdev *hwdev, enum hinic3_mod_type mod, u16 cmd,
+		  void *buf_in, u16 in_size, void *buf_out, u16 *out_size,
+		  u32 timeout)
+{
+	int err;
+
+	if (!hwdev)
+		return -EINVAL;
+
+	/* Check the validity of parameters. */
+	err = mbox_func_params_valid(hwdev->func_to_func, buf_in, in_size);
+	if (err)
+		return err;
+
+	if (!HINIC3_IS_VF(hwdev)) {
+		PMD_DRV_LOG(ERR, "Params error, func_type: %d",
+			    hinic3_func_type(hwdev));
+		return -EINVAL;
+	}
+
+	/* Sending Email to PF. */
+	return hinic3_mbox_to_func(hwdev->func_to_func, mod, cmd,
+				   hinic3_pf_id_of_vf(hwdev), buf_in, in_size,
+				   buf_out, out_size, timeout);
+}
+
+int
+hinic3_mbox_to_vf(struct hinic3_hwdev *hwdev, enum hinic3_mod_type mod,
+		  u16 vf_id, u16 cmd, void *buf_in, u16 in_size, void *buf_out,
+		  u16 *out_size, u32 timeout)
+{
+	struct hinic3_mbox *func_to_func = NULL;
+	u16 dst_func_idx;
+	int err = 0;
+
+	if (!hwdev)
+		return -EINVAL;
+
+	func_to_func = hwdev->func_to_func;
+	err = mbox_func_params_valid(func_to_func, buf_in, in_size);
+	if (err)
+		return err;
+
+	if (HINIC3_IS_VF(hwdev)) {
+		PMD_DRV_LOG(ERR, "Params error, func_type: %d",
+			    hinic3_func_type(hwdev));
+		return -EINVAL;
+	}
+
+	if (!vf_id) {
+		PMD_DRV_LOG(ERR, "VF id: %d error!", vf_id);
+		return -EINVAL;
+	}
+
+	/*
+	 * The sum of vf_offset_to_pf + vf_id is the VF's global function id of
+	 * VF in this pf.
+	 */
+	dst_func_idx = hinic3_glb_pf_vf_offset(hwdev) + vf_id;
+
+	return hinic3_mbox_to_func(func_to_func, mod, cmd, dst_func_idx, buf_in,
+				   in_size, buf_out, out_size, timeout);
+}
+
+static int
+init_mbox_info(struct hinic3_recv_mbox *mbox_info, int mbox_max_buf_sz)
+{
+	int err;
+
+	mbox_info->seq_id = SEQ_ID_MAX_VAL;
+
+	mbox_info->mbox =
+		rte_zmalloc("mbox", (size_t)mbox_max_buf_sz, 1); /*lint !e571*/
+	if (!mbox_info->mbox)
+		return -ENOMEM;
+
+	mbox_info->buf_out = rte_zmalloc("mbox_buf_out",
+		(size_t)mbox_max_buf_sz, 1); /*lint !e571*/
+	if (!mbox_info->buf_out) {
+		err = -ENOMEM;
+		goto alloc_buf_out_err;
+	}
+
+	return 0;
+
+alloc_buf_out_err:
+	rte_free(mbox_info->mbox);
+
+	return err;
+}
+
+static void
+clean_mbox_info(struct hinic3_recv_mbox *mbox_info)
+{
+	rte_free(mbox_info->buf_out);
+	rte_free(mbox_info->mbox);
+}
+
+static int
+alloc_mbox_info(struct hinic3_recv_mbox *mbox_info, int mbox_max_buf_sz)
+{
+	u16 func_idx, i;
+	int err;
+
+	for (func_idx = 0; func_idx < HINIC3_MAX_FUNCTIONS + 1; func_idx++) {
+		err = init_mbox_info(&mbox_info[func_idx], mbox_max_buf_sz);
+		if (err) {
+			PMD_DRV_LOG(ERR, "Init mbox info failed");
+			goto init_mbox_info_err;
+		}
+	}
+
+	return 0;
+
+init_mbox_info_err:
+	for (i = 0; i < func_idx; i++)
+		clean_mbox_info(&mbox_info[i]);
+
+	return err;
+}
+
+static void
+free_mbox_info(struct hinic3_recv_mbox *mbox_info)
+{
+	u16 func_idx;
+
+	for (func_idx = 0; func_idx < HINIC3_MAX_FUNCTIONS + 1; func_idx++)
+		clean_mbox_info(&mbox_info[func_idx]);
+}
+
+static void
+prepare_send_mbox(struct hinic3_mbox *func_to_func)
+{
+	struct hinic3_send_mbox *send_mbox = &func_to_func->send_mbox;
+
+	send_mbox->data = MBOX_AREA(func_to_func->hwdev->hwif);
+}
+
+/**
+ * Allocate memory for the write-back state of the mailbox and write to
+ * register.
+ *
+ * @param[in] func_to_func
+ * Context for inter-function communication.
+ * @return
+ * 0 on success, non-zero on failure.
+ */
+static int
+alloc_mbox_wb_status(struct hinic3_mbox *func_to_func)
+{
+	struct hinic3_send_mbox *send_mbox = &func_to_func->send_mbox;
+	struct hinic3_hwdev *hwdev = func_to_func->hwdev;
+	u32 addr_h, addr_l;
+
+	/* Reserved DMA area. */
+	send_mbox->wb_mz = hinic3_dma_zone_reserve(hwdev->eth_dev,
+		"wb_mz", 0, MBOX_WB_STATUS_LEN,
+		RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
+	if (!send_mbox->wb_mz)
+		return -ENOMEM;
+
+	send_mbox->wb_vaddr = send_mbox->wb_mz->addr;
+	send_mbox->wb_paddr = send_mbox->wb_mz->iova;
+	send_mbox->wb_status = send_mbox->wb_vaddr;
+
+	addr_h = upper_32_bits(send_mbox->wb_paddr);
+	addr_l = lower_32_bits(send_mbox->wb_paddr);
+
+	/* Write info to the register. */
+	hinic3_hwif_write_reg(hwdev->hwif, HINIC3_FUNC_CSR_MAILBOX_RESULT_H_OFF,
+			      addr_h);
+	hinic3_hwif_write_reg(hwdev->hwif, HINIC3_FUNC_CSR_MAILBOX_RESULT_L_OFF,
+			      addr_l);
+
+	return 0;
+}
+
+static void
+free_mbox_wb_status(struct hinic3_mbox *func_to_func)
+{
+	struct hinic3_send_mbox *send_mbox = &func_to_func->send_mbox;
+	struct hinic3_hwdev *hwdev = func_to_func->hwdev;
+
+	hinic3_hwif_write_reg(hwdev->hwif, HINIC3_FUNC_CSR_MAILBOX_RESULT_H_OFF,
+			      0);
+	hinic3_hwif_write_reg(hwdev->hwif, HINIC3_FUNC_CSR_MAILBOX_RESULT_L_OFF,
+			      0);
+
+	hinic3_memzone_free(send_mbox->wb_mz);
+}
+
+static int
+alloc_mbox_tlp_buffer(struct hinic3_mbox *func_to_func)
+{
+	struct hinic3_send_mbox *send_mbox = &func_to_func->send_mbox;
+	struct hinic3_hwdev *hwdev = func_to_func->hwdev;
+
+	send_mbox->sbuff_mz = hinic3_dma_zone_reserve(hwdev->eth_dev,
+		"sbuff_mz", 0, MBOX_MAX_BUF_SZ, MBOX_MAX_BUF_SZ,
+		SOCKET_ID_ANY);
+	if (!send_mbox->sbuff_mz)
+		return -ENOMEM;
+
+	send_mbox->sbuff_vaddr = send_mbox->sbuff_mz->addr;
+	send_mbox->sbuff_paddr = send_mbox->sbuff_mz->iova;
+
+	return 0;
+}
+
+static void
+free_mbox_tlp_buffer(struct hinic3_mbox *func_to_func)
+{
+	struct hinic3_send_mbox *send_mbox = &func_to_func->send_mbox;
+
+	hinic3_memzone_free(send_mbox->sbuff_mz);
+}
+
+/**
+ * Initialize function to function communication.
+ *
+ * @param[in] hwdev
+ * Pointer to hardware device structure.
+ * @return
+ * 0 on success, non-zero on failure.
+ */
+int
+hinic3_func_to_func_init(struct hinic3_hwdev *hwdev)
+{
+	struct hinic3_mbox *func_to_func;
+	int err;
+
+	func_to_func = rte_zmalloc("func_to_func", sizeof(*func_to_func), 1);
+	if (!func_to_func)
+		return -ENOMEM;
+
+	hwdev->func_to_func = func_to_func;
+	func_to_func->hwdev = hwdev;
+	hinic3_mutex_init(&func_to_func->mbox_send_mutex, NULL);
+	hinic3_mutex_init(&func_to_func->msg_send_mutex, NULL);
+	rte_spinlock_init(&func_to_func->mbox_lock);
+
+	/* Alloc the memory required by the mailbox. */
+	err = alloc_mbox_info(func_to_func->mbox_send, MBOX_MAX_BUF_SZ);
+	if (err) {
+		PMD_DRV_LOG(ERR, "Alloc mem for mbox_active failed");
+		goto alloc_mbox_for_send_err;
+	}
+
+	err = alloc_mbox_info(func_to_func->mbox_resp, MBOX_MAX_BUF_SZ);
+	if (err) {
+		PMD_DRV_LOG(ERR, "Alloc mem for mbox_passive failed");
+		goto alloc_mbox_for_resp_err;
+	}
+
+	err = alloc_mbox_tlp_buffer(func_to_func);
+	if (err) {
+		PMD_DRV_LOG(ERR, "Alloc mbox send buffer failed");
+		goto alloc_tlp_buffer_err;
+	}
+
+	err = alloc_mbox_wb_status(func_to_func);
+	if (err) {
+		PMD_DRV_LOG(ERR, "Alloc mbox write back status failed");
+		goto alloc_wb_status_err;
+	}
+
+	prepare_send_mbox(func_to_func);
+
+	return 0;
+
+alloc_wb_status_err:
+	free_mbox_tlp_buffer(func_to_func);
+
+alloc_tlp_buffer_err:
+	free_mbox_info(func_to_func->mbox_resp);
+
+alloc_mbox_for_resp_err:
+	free_mbox_info(func_to_func->mbox_send);
+
+alloc_mbox_for_send_err:
+	hinic3_mutex_destroy(&func_to_func->msg_send_mutex);
+	hinic3_mutex_destroy(&func_to_func->mbox_send_mutex);
+	rte_free(func_to_func);
+
+	return err;
+}
+
+void
+hinic3_func_to_func_free(struct hinic3_hwdev *hwdev)
+{
+	struct hinic3_mbox *func_to_func = hwdev->func_to_func;
+
+	free_mbox_wb_status(func_to_func);
+	free_mbox_tlp_buffer(func_to_func);
+	free_mbox_info(func_to_func->mbox_resp);
+	free_mbox_info(func_to_func->mbox_send);
+	hinic3_mutex_destroy(&func_to_func->mbox_send_mutex);
+	hinic3_mutex_destroy(&func_to_func->msg_send_mutex);
+
+	rte_free(func_to_func);
+}
drivers/net/hinic3/base/hinic3_mbox.h new file +199 -0

199 lines not on this page · raw message

Signature · 1 line
2.45.1.windows.1
#147 lore raw

Re: [V5 11/18] net/hinic3: add a mailbox communication module

Quoted 6 lines · Feifei Wang

From: Yi Chen <[email protected]>

This patch adds support for mailbox of hinic3 PMD driver,

mailbox is used for communication between PF/VF driver and MPU.

does the MPU mean firmware? please use firmare instead of MPU if yes.

Quoted 320 lines

This patch provides mailbox-related data structures and functional

code.

---

 drivers/net/hinic3/base/hinic3_mbox.c | 1399 +++++++++++++++++++++++++
 drivers/net/hinic3/base/hinic3_mbox.h |  199 ++++
 2 files changed, 1598 insertions(+)
 create mode 100644 drivers/net/hinic3/base/hinic3_mbox.c
 create mode 100644 drivers/net/hinic3/base/hinic3_mbox.h
drivers/net/hinic3/base/hinic3_mbox.c +0 -0

new file mode 100644

index 0000000000..845ab9cfc3

--- /dev/null

+++ b/drivers/net/hinic3/base/hinic3_mbox.c

drivers/net/hinic3/base/hinic3_mbox.c +141 -0
@@ -0,0 +1,1399 @@
 
+/* SPDX-License-Identifier: BSD-3-Clause
 
+ * Copyright(c) 2025 Huawei Technologies Co., Ltd
 
+ */
 
+
 
+#include <rte_atomic.h>
 
+
 
+#include "hinic3_compat.h"
 
+#include "hinic3_csr.h"
 
+#include "hinic3_eqs.h"
 
+#include "hinic3_hw_cfg.h"
 
+#include "hinic3_hwdev.h"
 
+#include "hinic3_hwif.h"
 
+#include "hinic3_mbox.h"
 
+#include "hinic3_mgmt.h"
 
+#include "hinic3_nic_event.h"
 
+
 
+#define HINIC3_MBOX_INT_DST_FUNC_SHIFT	    0
 
+#define HINIC3_MBOX_INT_DST_AEQN_SHIFT	    10
 
+#define HINIC3_MBOX_INT_SRC_RESP_AEQN_SHIFT 12
 
+#define HINIC3_MBOX_INT_STAT_DMA_SHIFT	    14
 
+/* The size of data to be send (unit of 4 bytes). */
 
+#define HINIC3_MBOX_INT_TX_SIZE_SHIFT 20
 
+/* SO_RO(strong order, relax order). */
 
+#define HINIC3_MBOX_INT_STAT_DMA_SO_RO_SHIFT 25
 
+#define HINIC3_MBOX_INT_WB_EN_SHIFT	     28
 
+
 
+#define HINIC3_MBOX_INT_DST_AEQN_MASK	    0x3
 
+#define HINIC3_MBOX_INT_SRC_RESP_AEQN_MASK  0x3
 
+#define HINIC3_MBOX_INT_STAT_DMA_MASK	    0x3F
 
+#define HINIC3_MBOX_INT_TX_SIZE_MASK	    0x1F
 
+#define HINIC3_MBOX_INT_STAT_DMA_SO_RO_MASK 0x3
 
+#define HINIC3_MBOX_INT_WB_EN_MASK	    0x1
 
+
 
+#define HINIC3_MBOX_INT_SET(val, field)           \
 
+	(((val) & HINIC3_MBOX_INT_##field##_MASK) \
 
+	 << HINIC3_MBOX_INT_##field##_SHIFT)
 
+
 
+enum hinic3_mbox_tx_status {
 
+	TX_NOT_DONE = 1,
 
+};
 
+
 
+#define HINIC3_MBOX_CTRL_TRIGGER_AEQE_SHIFT 0
 
+
 
+/*
 
+ * Specifies the issue request for the message data.
 
+ * 0 - Tx request is done;
 
+ * 1 - Tx request is in process.
 
+ */
 
+#define HINIC3_MBOX_CTRL_TX_STATUS_SHIFT 1
 
+#define HINIC3_MBOX_CTRL_DST_FUNC_SHIFT	 16
 
+
 
+#define HINIC3_MBOX_CTRL_TRIGGER_AEQE_MASK 0x1
 
+#define HINIC3_MBOX_CTRL_TX_STATUS_MASK	   0x1
 
+#define HINIC3_MBOX_CTRL_DST_FUNC_MASK	   0x1FFF
 
+
 
+#define HINIC3_MBOX_CTRL_SET(val, field)           \
 
+	(((val) & HINIC3_MBOX_CTRL_##field##_MASK) \
 
+	 << HINIC3_MBOX_CTRL_##field##_SHIFT)
 
+
 
+#define MBOX_SEGLEN_MASK \
 
+	HINIC3_MSG_HEADER_SET(HINIC3_MSG_HEADER_SEG_LEN_MASK, SEG_LEN)
 
+
 
+#define MBOX_MSG_POLLING_TIMEOUT 500000 /* Unit is 10us. */
 
+#define HINIC3_MBOX_COMP_TIME	 40000U
 
+
 
+#define MBOX_MAX_BUF_SZ	      2048UL
 
+#define MBOX_HEADER_SZ	      8
 
+#define HINIC3_MBOX_DATA_SIZE (MBOX_MAX_BUF_SZ - MBOX_HEADER_SZ)
 
+
 
+#define MBOX_TLP_HEADER_SZ 16
 
+
 
+/* Mbox size is 64B, 8B for mbox_header, 8B reserved. */
 
+#define MBOX_SEG_LEN	   48
 
+#define MBOX_SEG_LEN_ALIGN 4
 
+#define MBOX_WB_STATUS_LEN 16UL
 
+
 
+/* Mbox write back status is 16B, only first 4B is used. */
 
+#define MBOX_WB_STATUS_ERRCODE_MASK	 0xFFFF
 
+#define MBOX_WB_STATUS_MASK		 0xFF
 
+#define MBOX_WB_ERROR_CODE_MASK		 0xFF00
 
+#define MBOX_WB_STATUS_FINISHED_SUCCESS	 0xFF
 
+#define MBOX_WB_STATUS_FINISHED_WITH_ERR 0xFE
 
+#define MBOX_WB_STATUS_NOT_FINISHED	 0x00
 
+
 
+/* Determine the write back status. */
 
+#define MBOX_STATUS_FINISHED(wb) \
 
+	(((wb) & MBOX_WB_STATUS_MASK) != MBOX_WB_STATUS_NOT_FINISHED)
 
+#define MBOX_STATUS_SUCCESS(wb) \
 
+	(((wb) & MBOX_WB_STATUS_MASK) == MBOX_WB_STATUS_FINISHED_SUCCESS)
 
+#define MBOX_STATUS_ERRCODE(wb) ((wb) & MBOX_WB_ERROR_CODE_MASK)
 
+
 
+/* Indicate the value related to the sequence ID. */
 
+#define SEQ_ID_START_VAL 0
 
+#define SEQ_ID_MAX_VAL	 42
 
+
 
+#define DST_AEQ_IDX_DEFAULT_VAL 0
 
+#define SRC_AEQ_IDX_DEFAULT_VAL 0
 
+#define NO_DMA_ATTRIBUTE_VAL	0
 
+
 
+#define MBOX_MSG_NO_DATA_LEN 1
 
+
 
+/* Obtain the specified content of the mailbox. */
 
+#define MBOX_BODY_FROM_HDR(header) ((u8 *)(header) + MBOX_HEADER_SZ)
 
+#define MBOX_AREA(hwif) \
 
+	((hwif)->cfg_regs_base + HINIC3_FUNC_CSR_MAILBOX_DATA_OFF)
 
+
 
+#define IS_PF_OR_PPF_SRC(src_func_idx) ((src_func_idx) < HINIC3_MAX_PF_FUNCS)
 
+
 
+#define MBOX_RESPONSE_ERROR	  0x1
 
+#define MBOX_MSG_ID_MASK	  0xF
 
+#define MBOX_MSG_ID(func_to_func) ((func_to_func)->send_msg_id)
 
+#define MBOX_MSG_ID_INC(func_to_func)                             \
 
+	({                                                        \
 
+		typeof(func_to_func) __func = (func_to_func);     \
 
+		MBOX_MSG_ID(__func) = (MBOX_MSG_ID(__func) + 1) & \
 
+				      MBOX_MSG_ID_MASK;           \
 
+	})
 
+
 
+/* Max message counter waits to process for one function. */
 
+#define HINIC3_MAX_MSG_CNT_TO_PROCESS 10
 
+
 
+enum mbox_ordering_type {
 
+	STRONG_ORDER,
 
+};
 
+
 
+enum mbox_write_back_type {
 
+	WRITE_BACK = 1,
 
+};
 
+
 
+enum mbox_aeq_trig_type {
 
+	NOT_TRIGGER,
 
+	TRIGGER,
 
+};
 
+
 
+static int send_mbox_to_func(struct hinic3_mbox *func_to_func,
 
+			     enum hinic3_mod_type mod, u16 cmd, void *msg,
 
+			     u16 msg_len, u16 dst_func,
 
+			     enum hinic3_msg_direction_type direction,
 
+			     enum hinic3_msg_ack_type ack_type,
 
+			     struct mbox_msg_info *msg_info);
 
+static int send_tlp_mbox_to_func(struct hinic3_mbox *func_to_func,

drivers/net/hinic3/base/hinic3_mbox.c line 146

Quoted 10 lines
drivers/net/hinic3/base/hinic3_mbox.c +5 -0
 
+				 enum hinic3_mod_type mod, u16 cmd, void *msg,
 
+				 u16 msg_len, u16 dst_func,
 
+				 enum hinic3_msg_direction_type direction,
 
+				 enum hinic3_msg_ack_type ack_type,
 
+				 struct mbox_msg_info *msg_info);

two much parameters, how about wrap as several struct?

Quoted 274 lines
drivers/net/hinic3/base/hinic3_mbox.c +137 -0
+
 
+static int
 
+recv_vf_mbox_handler(struct hinic3_mbox *func_to_func,
 
+		     struct hinic3_recv_mbox *recv_mbox, void *buf_out,
 
+		     u16 *out_size, __rte_unused void *param)
 
+{
 
+	int err = 0;
 
+
 
+	/*
 
+	 * Invoke the corresponding processing function according to the type of
 
+	 * the received mailbox.
 
+	 */
 
+	switch (recv_mbox->mod) {
 
+	case HINIC3_MOD_COMM:
 
+		err = vf_handle_pf_comm_mbox(func_to_func->hwdev, func_to_func,
 
+					     recv_mbox->cmd, recv_mbox->mbox,
 
+					     recv_mbox->mbox_len, buf_out,
 
+					     out_size);
 
+		break;
 
+	case HINIC3_MOD_CFGM:
 
+		err = cfg_mbx_vf_proc_msg(func_to_func->hwdev,
 
+			func_to_func->hwdev->cfg_mgmt,
 
+			recv_mbox->cmd, recv_mbox->mbox, recv_mbox->mbox_len,
 
+			buf_out, out_size);
 
+		break;
 
+	case HINIC3_MOD_L2NIC:
 
+		err = hinic3_vf_event_handler(func_to_func->hwdev,
 
+			func_to_func->hwdev->cfg_mgmt,
 
+			recv_mbox->cmd, recv_mbox->mbox, recv_mbox->mbox_len,
 
+			buf_out, out_size);
 
+		break;
 
+	case HINIC3_MOD_HILINK:
 
+		err = hinic3_vf_mag_event_handler(func_to_func->hwdev,
 
+			func_to_func->hwdev->cfg_mgmt,
 
+			recv_mbox->cmd, recv_mbox->mbox, recv_mbox->mbox_len,
 
+			buf_out, out_size);
 
+		break;
 
+	default:
 
+		PMD_DRV_LOG(ERR, "No handler, mod: %d", recv_mbox->mod);
 
+		err = HINIC3_MBOX_VF_CMD_ERROR;
 
+		break;
 
+	}
 
+
 
+	return err;
 
+}
 
+
 
+/**
 
+ * Respond to the accept packet, construct a response message, and send it.
 
+ *
 
+ * @param[in] func_to_func
 
+ * Context for inter-function communication.
 
+ * @param[in] recv_mbox
 
+ * Pointer to the received inter-function mailbox structure.
 
+ * @param[in] err
 
+ * Error Code.
 
+ * @param[in] out_size
 
+ * Output Size.
 
+ * @param[in] src_func_idx
 
+ * Index of the source function.
 
+ */
 
+static void
 
+response_for_recv_func_mbox(struct hinic3_mbox *func_to_func,
 
+			    struct hinic3_recv_mbox *recv_mbox, int err,
 
+			    u16 out_size, u16 src_func_idx)
 
+{
 
+	struct mbox_msg_info msg_info = {0};
 
+
 
+	if (recv_mbox->ack_type == HINIC3_MSG_ACK) {
 
+		msg_info.msg_id = recv_mbox->msg_info.msg_id;
 
+		if (err)
 
+			msg_info.status = HINIC3_MBOX_PF_SEND_ERR;
 
+
 
+		/* Select the sending function based on the packet type. */
 
+		if (IS_TLP_MBX(src_func_idx))
 
+			send_tlp_mbox_to_func(func_to_func, recv_mbox->mod,
 
+					      recv_mbox->cmd,
 
+					      recv_mbox->buf_out, out_size,
 
+					      src_func_idx, HINIC3_MSG_RESPONSE,
 
+					      HINIC3_MSG_NO_ACK, &msg_info);
 
+		else
 
+			send_mbox_to_func(func_to_func, recv_mbox->mod,
 
+					  recv_mbox->cmd, recv_mbox->buf_out,
 
+					  out_size, src_func_idx,
 
+					  HINIC3_MSG_RESPONSE,
 
+					  HINIC3_MSG_NO_ACK, &msg_info);
 
+	}
 
+}
 
+
 
+static bool
 
+check_func_mbox_ack_first(u8 mod)
 
+{
 
+	return mod == HINIC3_MOD_HILINK;
 
+}
 
+
 
+static void
 
+recv_func_mbox_handler(struct hinic3_mbox *func_to_func,
 
+		       struct hinic3_recv_mbox *recv_mbox, u16 src_func_idx,
 
+		       void *param)
 
+{
 
+	struct hinic3_hwdev *hwdev = func_to_func->hwdev;
 
+	void *buf_out = recv_mbox->buf_out;
 
+	bool ack_first = false;
 
+	u16 out_size = MBOX_MAX_BUF_SZ;
 
+	int err = 0;
 
+	/* Check whether the response is the first ACK message. */
 
+	ack_first = check_func_mbox_ack_first(recv_mbox->mod);
 
+	if (ack_first && recv_mbox->ack_type == HINIC3_MSG_ACK) {
 
+		response_for_recv_func_mbox(func_to_func, recv_mbox, err,
 
+					    out_size, src_func_idx);
 
+	}
 
+
 
+	/* Processe mailbox information in the VF. */
 
+	if (HINIC3_IS_VF(hwdev)) {
 
+		err = recv_vf_mbox_handler(func_to_func, recv_mbox, buf_out,
 
+					   &out_size, param);
 
+	} else {
 
+		err = -EINVAL;
 
+		PMD_DRV_LOG(ERR,
 
+			"PMD doesn't support non-VF handle mailbox message");
 
+	}
 
+
 
+	if (!out_size || err)
 
+		out_size = MBOX_MSG_NO_DATA_LEN;
 
+
 
+	if (!ack_first && recv_mbox->ack_type == HINIC3_MSG_ACK) {
 
+		response_for_recv_func_mbox(func_to_func, recv_mbox, err,
 
+					    out_size, src_func_idx);
 
+	}
 
+}
 
+
 
+/**
 
+ * Processe mailbox responses from functions.
 
+ *
 
+ * @param[in] func_to_func
 
+ * Mailbox for inter-function communication.
 
+ * @param[in] recv_mbox
 
+ * Received mailbox message.

drivers/net/hinic3/base/hinic3_mbox.c line 288

Quoted 10 lines
drivers/net/hinic3/base/hinic3_mbox.c +5 -0
 
+ * @return
 
+ * 0 on success, non-zero on failure.
 
+ */
 
+static int
 
+resp_mbox_handler(struct hinic3_mbox *func_to_func,

func_to_func ???

Quoted 1,390 lines
drivers/net/hinic3/base/hinic3_mbox.c +695 -0
+		  struct hinic3_recv_mbox *recv_mbox)
 
+{
 
+	int ret;
 
+	rte_spinlock_lock(&func_to_func->mbox_lock);
 
+	if (recv_mbox->msg_info.msg_id == func_to_func->send_msg_id &&
 
+	    func_to_func->event_flag == EVENT_START) {
 
+		func_to_func->event_flag = EVENT_SUCCESS;
 
+		ret = 0;
 
+	} else {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Mbox response timeout, current send msg id(0x%x), "
 
+			    "recv msg id(0x%x), status(0x%x)",
 
+			    func_to_func->send_msg_id,
 
+			    recv_mbox->msg_info.msg_id,
 
+			    recv_mbox->msg_info.status);
 
+		ret = HINIC3_MSG_HANDLER_RES;
 
+	}
 
+	rte_spinlock_unlock(&func_to_func->mbox_lock);
 
+	return ret;
 
+}
 
+
 
+/**
 
+ * Check whether the received mailbox message segment is valid.
 
+ *
 
+ * @param[out] recv_mbox
 
+ * Received mailbox message.
 
+ * @param[in] mbox_header
 
+ * Mailbox header.
 
+ * @return
 
+ * The value true indicates valid, and the value false indicates invalid.
 
+ */
 
+static bool
 
+check_mbox_segment(struct hinic3_recv_mbox *recv_mbox, u64 mbox_header)
 
+{
 
+	u8 seq_id, seg_len, msg_id, mod;
 
+	u16 src_func_idx, cmd;
 
+
 
+	/* Get info from the mailbox header. */
 
+	seq_id = HINIC3_MSG_HEADER_GET(mbox_header, SEQID);
 
+	seg_len = HINIC3_MSG_HEADER_GET(mbox_header, SEG_LEN);
 
+	src_func_idx = HINIC3_MSG_HEADER_GET(mbox_header, SRC_GLB_FUNC_IDX);
 
+	msg_id = HINIC3_MSG_HEADER_GET(mbox_header, MSG_ID);
 
+	mod = HINIC3_MSG_HEADER_GET(mbox_header, MODULE);
 
+	cmd = HINIC3_MSG_HEADER_GET(mbox_header, CMD);
 
+
 
+	if (seq_id > SEQ_ID_MAX_VAL || seg_len > MBOX_SEG_LEN)
 
+		goto seg_err;
 
+
 
+	/* New message segment, which saves its information to recv_mbox. */
 
+	if (seq_id == 0) {
 
+		recv_mbox->seq_id = seq_id;
 
+		recv_mbox->msg_info.msg_id = msg_id;
 
+		recv_mbox->mod = mod;
 
+		recv_mbox->cmd = cmd;
 
+	} else {
 
+		if ((seq_id != recv_mbox->seq_id + 1) ||
 
+		    msg_id != recv_mbox->msg_info.msg_id ||
 
+		    mod != recv_mbox->mod || cmd != recv_mbox->cmd)
 
+			goto seg_err;
 
+
 
+		recv_mbox->seq_id = seq_id;
 
+	}
 
+
 
+	return true;
 
+
 
+seg_err:
 
+	PMD_DRV_LOG(ERR,
 
+		    "Mailbox segment check failed, src func id: 0x%x, "
 
+		    "front seg info: seq id: 0x%x, msg id: 0x%x, mod: 0x%x, "
 
+		    "cmd: 0x%x",
 
+		    src_func_idx, recv_mbox->seq_id, recv_mbox->msg_info.msg_id,
 
+		    recv_mbox->mod, recv_mbox->cmd);
 
+	PMD_DRV_LOG(ERR,
 
+		    "Current seg info: seg len: 0x%x, seq id: 0x%x, "
 
+		    "msg id: 0x%x, mod: 0x%x, cmd: 0x%x",
 
+		    seg_len, seq_id, msg_id, mod, cmd);
 
+
 
+	return false;
 
+}
 
+
 
+static int
 
+recv_mbox_handler(struct hinic3_mbox *func_to_func, void *header,
 
+		  struct hinic3_recv_mbox *recv_mbox, void *param)
 
+{
 
+	u64 mbox_header = *((u64 *)header);
 
+	void *mbox_body = MBOX_BODY_FROM_HDR(header);
 
+	u16 src_func_idx;
 
+	int pos;
 
+	u8 seq_id;
 
+	/* Obtain information from the mailbox header. */
 
+	seq_id = HINIC3_MSG_HEADER_GET(mbox_header, SEQID);
 
+	src_func_idx = HINIC3_MSG_HEADER_GET(mbox_header, SRC_GLB_FUNC_IDX);
 
+
 
+	if (!check_mbox_segment(recv_mbox, mbox_header)) {
 
+		recv_mbox->seq_id = SEQ_ID_MAX_VAL;
 
+		return HINIC3_MSG_HANDLER_RES;
 
+	}
 
+
 
+	pos = seq_id * MBOX_SEG_LEN;
 
+	memcpy((void *)((u8 *)recv_mbox->mbox + pos), (void *)mbox_body,
 
+	       (size_t)HINIC3_MSG_HEADER_GET(mbox_header, SEG_LEN));
 
+
 
+	if (!HINIC3_MSG_HEADER_GET(mbox_header, LAST))
 
+		return HINIC3_MSG_HANDLER_RES;
 
+	/* Setting the information about the recv mailbox. */
 
+	recv_mbox->cmd = HINIC3_MSG_HEADER_GET(mbox_header, CMD);
 
+	recv_mbox->mod = HINIC3_MSG_HEADER_GET(mbox_header, MODULE);
 
+	recv_mbox->mbox_len = HINIC3_MSG_HEADER_GET(mbox_header, MSG_LEN);
 
+	recv_mbox->ack_type = HINIC3_MSG_HEADER_GET(mbox_header, NO_ACK);
 
+	recv_mbox->msg_info.msg_id = HINIC3_MSG_HEADER_GET(mbox_header, MSG_ID);
 
+	recv_mbox->msg_info.status = HINIC3_MSG_HEADER_GET(mbox_header, STATUS);
 
+	recv_mbox->seq_id = SEQ_ID_MAX_VAL;
 
+
 
+	/*
 
+	 * If the received message is a response message, call the mbox response
 
+	 * processing function.
 
+	 */
 
+	if (HINIC3_MSG_HEADER_GET(mbox_header, DIRECTION) ==
 
+	    HINIC3_MSG_RESPONSE) {
 
+		return resp_mbox_handler(func_to_func, recv_mbox);
 
+	}
 
+
 
+	recv_func_mbox_handler(func_to_func, recv_mbox, src_func_idx, param);
 
+	return HINIC3_MSG_HANDLER_RES;
 
+}
 
+
 
+static inline int
 
+hinic3_mbox_get_index(int func)
 
+{
 
+	return (func == HINIC3_MGMT_SRC_ID) ? HINIC3_MBOX_MPU_INDEX
 
+					    : HINIC3_MBOX_PF_INDEX;
 
+}
 
+
 
+int
 
+hinic3_mbox_func_aeqe_handler(void *handle, u8 *header, __rte_unused u8 size,
 
+			      void *param)
 
+{
 
+	struct hinic3_mbox *func_to_func = NULL;
 
+	struct hinic3_recv_mbox *recv_mbox = NULL;
 
+	u64 mbox_header = *((u64 *)header);
 
+	u64 src, dir;
 
+	/* Obtain the mailbox for communication between functions. */
 
+	func_to_func = ((struct hinic3_hwdev *)handle)->func_to_func;
 
+
 
+	dir = HINIC3_MSG_HEADER_GET(mbox_header, DIRECTION);
 
+	src = HINIC3_MSG_HEADER_GET(mbox_header, SRC_GLB_FUNC_IDX);
 
+
 
+	src = hinic3_mbox_get_index((int)src);
 
+	recv_mbox = (dir == HINIC3_MSG_DIRECT_SEND)
 
+			    ? &func_to_func->mbox_send[src]
 
+			    : &func_to_func->mbox_resp[src];
 
+	/* Processing Received Mailbox info. */
 
+	return recv_mbox_handler(func_to_func, (u64 *)header, recv_mbox, param);
 
+}
 
+
 
+static void
 
+clear_mbox_status(struct hinic3_send_mbox *mbox)
 
+{
 
+	*mbox->wb_status = 0;
 
+
 
+	/* Clear mailbox write back status. */
 
+	rte_atomic_thread_fence(rte_memory_order_release);
 
+}
 
+
 
+static void
 
+mbox_copy_header(struct hinic3_send_mbox *mbox, u64 *header)
 
+{
 
+	u32 *data = (u32 *)header;
 
+	u32 i, idx_max = MBOX_HEADER_SZ / sizeof(u32);
 
+
 
+	for (i = 0; i < idx_max; i++) {
 
+		rte_write32(cpu_to_be32(*(data + i)),
 
+			    mbox->data + i * sizeof(u32));
 
+	}
 
+}
 
+
 
+#define MBOX_DMA_MSG_INIT_XOR_VAL 0x5a5a5a5a
 
+static u32
 
+mbox_dma_msg_xor(u32 *data, u16 msg_len)
 
+{
 
+	u32 xor = MBOX_DMA_MSG_INIT_XOR_VAL;
 
+	u16 dw_len = msg_len / sizeof(u32);
 
+	u16 i;
 
+
 
+	for (i = 0; i < dw_len; i++)
 
+		xor ^= data[i];
 
+
 
+	return xor;
 
+}
 
+
 
+static void
 
+mbox_copy_send_data_addr(struct hinic3_send_mbox *mbox, u16 seg_len)
 
+{
 
+	u32 addr_h, addr_l, xor;
 
+
 
+	xor = mbox_dma_msg_xor(mbox->sbuff_vaddr, seg_len);
 
+	addr_h = upper_32_bits(mbox->sbuff_paddr);
 
+	addr_l = lower_32_bits(mbox->sbuff_paddr);
 
+
 
+	rte_write32(cpu_to_be32(xor), mbox->data + MBOX_HEADER_SZ);
 
+	rte_write32(cpu_to_be32(addr_h),
 
+		    mbox->data + MBOX_HEADER_SZ + sizeof(u32));
 
+	rte_write32(cpu_to_be32(addr_l),
 
+		    mbox->data + MBOX_HEADER_SZ + 0x2 * sizeof(u32));
 
+	rte_write32(cpu_to_be32((u32)seg_len),
 
+		    mbox->data + MBOX_HEADER_SZ + 0x3 * sizeof(u32));
 
+	/* Reserved field. */
 
+	rte_write32(0, mbox->data + MBOX_HEADER_SZ + 0x4 * sizeof(u32));
 
+	rte_write32(0, mbox->data + MBOX_HEADER_SZ + 0x5 * sizeof(u32));
 
+}
 
+
 
+static void
 
+mbox_copy_send_data(struct hinic3_send_mbox *mbox, void *seg, u16 seg_len)
 
+{
 
+	u32 *data = seg;
 
+	u32 data_len, chk_sz = sizeof(u32);
 
+	u32 i, idx_max;
 
+	u8 mbox_max_buf[MBOX_SEG_LEN] = {0};
 
+
 
+	/* The mbox message should be aligned in 4 bytes. */
 
+	if (seg_len % chk_sz) {
 
+		if (seg_len <= sizeof(mbox_max_buf)) {
 
+			memcpy(mbox_max_buf, seg, seg_len);
 
+			data = (u32 *)mbox_max_buf;
 
+		} else {
 
+			PMD_DRV_LOG(ERR, "mbox_max_buf overflow: seg_len=%u.", seg_len);
 
+			return;
 
+		}
 
+	}
 
+
 
+	data_len = seg_len;
 
+	idx_max = RTE_ALIGN(data_len, chk_sz) / chk_sz;
 
+
 
+	for (i = 0; i < idx_max; i++) {
 
+		rte_write32(cpu_to_be32(*(data + i)),
 
+			    mbox->data + MBOX_HEADER_SZ + i * sizeof(u32));
 
+	}
 
+}
 
+
 
+static void
 
+write_mbox_msg_attr(struct hinic3_mbox *func_to_func, u16 dst_func,
 
+		    u16 dst_aeqn, u16 seg_len)
 
+{
 
+	u32 mbox_int, mbox_ctrl;
 
+
 
+	/* If VF, function ids must self-learning by HW(PPF=1 PF=0). */
 
+	if (HINIC3_IS_VF(func_to_func->hwdev) &&
 
+	    dst_func != HINIC3_MGMT_SRC_ID) {
 
+		if (dst_func == HINIC3_HWIF_PPF_IDX(func_to_func->hwdev->hwif))
 
+			dst_func = 1;
 
+		else
 
+			dst_func = 0;
 
+	}
 
+	/* Set the interrupt attribute of the mailbox. */
 
+	mbox_int = HINIC3_MBOX_INT_SET(dst_aeqn, DST_AEQN) |
 
+		   HINIC3_MBOX_INT_SET(0, SRC_RESP_AEQN) |
 
+		   HINIC3_MBOX_INT_SET(NO_DMA_ATTRIBUTE_VAL, STAT_DMA) |
 
+		   HINIC3_MBOX_INT_SET(RTE_ALIGN(seg_len + MBOX_HEADER_SZ,
 
+						 MBOX_SEG_LEN_ALIGN) >>
 
+					       2,
 
+				       TX_SIZE) |
 
+		   HINIC3_MBOX_INT_SET(STRONG_ORDER, STAT_DMA_SO_RO) |
 
+		   HINIC3_MBOX_INT_SET(WRITE_BACK, WB_EN);
 
+
 
+	/* The interrupt attribute is written to the interrupt register. */
 
+	hinic3_hwif_write_reg(func_to_func->hwdev->hwif,
 
+			      HINIC3_FUNC_CSR_MAILBOX_INT_OFFSET_OFF, mbox_int);
 
+
 
+	rte_atomic_thread_fence(rte_memory_order_release); /**< Writing the mbox intr attributes */
 
+
 
+	/* Set the control attributes of the mailbox and write to register. */
 
+	mbox_ctrl = HINIC3_MBOX_CTRL_SET(TX_NOT_DONE, TX_STATUS);
 
+	mbox_ctrl |= HINIC3_MBOX_CTRL_SET(NOT_TRIGGER, TRIGGER_AEQE);
 
+	mbox_ctrl |= HINIC3_MBOX_CTRL_SET(dst_func, DST_FUNC);
 
+	hinic3_hwif_write_reg(func_to_func->hwdev->hwif,
 
+			      HINIC3_FUNC_CSR_MAILBOX_CONTROL_OFF, mbox_ctrl);
 
+}
 
+
 
+/**
 
+ * Read the value of the mailbox register of the hardware device.
 
+ *
 
+ * @param[in] hwdev
 
+ * Pointer to hardware device structure.
 
+ */
 
+static void
 
+dump_mbox_reg(struct hinic3_hwdev *hwdev)
 
+{
 
+	u32 val;
 
+	/* Read the value of the MBOX control register. */
 
+	val = hinic3_hwif_read_reg(hwdev->hwif,
 
+				   HINIC3_FUNC_CSR_MAILBOX_CONTROL_OFF);
 
+	PMD_DRV_LOG(ERR, "Mailbox control reg: 0x%x", val);
 
+	/* Read the value of the MBOX interrupt offset register. */
 
+	val = hinic3_hwif_read_reg(hwdev->hwif,
 
+				   HINIC3_FUNC_CSR_MAILBOX_INT_OFFSET_OFF);
 
+	PMD_DRV_LOG(ERR, "Mailbox interrupt offset: 0x%x", val);
 
+}
 
+
 
+static u16
 
+get_mbox_status(struct hinic3_send_mbox *mbox)
 
+{
 
+	/* Write back is 16B, but only use first 4B. */
 
+	u64 wb_val = be64_to_cpu(*mbox->wb_status);
 
+
 
+	rte_atomic_thread_fence(rte_memory_order_acquire); /**< Verify reading before check. */
 
+
 
+	return (u16)(wb_val & MBOX_WB_STATUS_ERRCODE_MASK);
 
+}
 
+
 
+/**
 
+ * Sending Mailbox Message Segment.
 
+ *
 
+ * @param[in] func_to_func
 
+ * Mailbox for inter-function communication.
 
+ * @param[in] header
 
+ * Mailbox header.
 
+ * @param[in] dst_func
 
+ * Indicate destination func.
 
+ * @param[in] seg
 
+ * Segment data to be sent.
 
+ * @param[in] seg_len
 
+ * Length of the segment to be sent.
 
+ * @param[in] msg_info
 
+ * Indicate the message information.
 
+ * @return
 
+ * 0 on success, non-zero on failure.
 
+ */
 
+static int
 
+send_mbox_seg(struct hinic3_mbox *func_to_func, u64 header, u16 dst_func,
 
+	      void *seg, u16 seg_len, __rte_unused void *msg_info)
 
+{
 
+	struct hinic3_send_mbox *send_mbox = &func_to_func->send_mbox;
 
+	struct hinic3_hwdev *hwdev = func_to_func->hwdev;
 
+	u8 num_aeqs = hwdev->hwif->attr.num_aeqs;
 
+	u16 dst_aeqn, wb_status = 0, errcode;
 
+	u16 seq_dir = HINIC3_MSG_HEADER_GET(header, DIRECTION);
 
+	u32 cnt = 0;
 
+
 
+	/* Mbox to mgmt cpu, hardware doesn't care dst aeq id. */
 
+	if (num_aeqs >= 2)
 
+		dst_aeqn = (seq_dir == HINIC3_MSG_DIRECT_SEND)
 
+				   ? HINIC3_ASYNC_MSG_AEQ
 
+				   : HINIC3_MBOX_RSP_MSG_AEQ;
 
+	else
 
+		dst_aeqn = 0;
 
+
 
+	clear_mbox_status(send_mbox);
 
+	mbox_copy_header(send_mbox, &header);
 
+	mbox_copy_send_data(send_mbox, seg, seg_len);
 
+
 
+	/* Set mailbox msg seg len. */
 
+	write_mbox_msg_attr(func_to_func, dst_func, dst_aeqn, seg_len);
 
+	rte_atomic_thread_fence(rte_memory_order_release); /**< Writing the mbox msg attributes. */
 
+
 
+	/* Wait until the status of the mailbox changes to Complete. */
 
+	while (cnt < MBOX_MSG_POLLING_TIMEOUT) {
 
+		wb_status = get_mbox_status(send_mbox);
 
+		if (MBOX_STATUS_FINISHED(wb_status))
 
+			break;
 
+
 
+		rte_delay_us(10);
 
+		cnt++;
 
+	}
 
+
 
+	if (cnt == MBOX_MSG_POLLING_TIMEOUT) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Send mailbox segment timeout, wb status: 0x%x",
 
+			    wb_status);
 
+		dump_mbox_reg(hwdev);
 
+		return -ETIMEDOUT;
 
+	}
 
+
 
+	if (!MBOX_STATUS_SUCCESS(wb_status)) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Send mailbox segment to function %d error, wb "
 
+			    "status: 0x%x",
 
+			    dst_func, wb_status);
 
+		errcode = MBOX_STATUS_ERRCODE(wb_status);
 
+		return errcode ? errcode : -EFAULT;
 
+	}
 
+
 
+	return 0;
 
+}
 
+
 
+static int
 
+send_tlp_mbox_seg(struct hinic3_mbox *func_to_func, u64 header, u16 dst_func,
 
+		  void *seg, u16 seg_len, __rte_unused void *msg_info)
 
+{
 
+	struct hinic3_send_mbox *send_mbox = &func_to_func->send_mbox;
 
+	struct hinic3_hwdev *hwdev = func_to_func->hwdev;
 
+	u8 num_aeqs = hwdev->hwif->attr.num_aeqs;
 
+	u16 dst_aeqn, errcode, wb_status = 0;
 
+	u16 seq_dir = HINIC3_MSG_HEADER_GET(header, DIRECTION);
 
+	u32 cnt = 0;
 
+
 
+	/* Mbox to mgmt cpu, hardware doesn't care dst aeq id. */
 
+	if (num_aeqs >= 2)
 
+		dst_aeqn = (seq_dir == HINIC3_MSG_DIRECT_SEND)
 
+				   ? HINIC3_ASYNC_MSG_AEQ
 
+				   : HINIC3_MBOX_RSP_MSG_AEQ;
 
+	else
 
+		dst_aeqn = 0;
 
+
 
+	clear_mbox_status(send_mbox);
 
+	mbox_copy_header(send_mbox, &header);
 
+
 
+	/* Copy data to DMA buffer. */
 
+	memcpy((void *)send_mbox->sbuff_vaddr, (void *)seg, (size_t)seg_len);
 
+
 
+	/*
 
+	 * Copy data address to mailbox ctrl CSR(Control and Status Register).
 
+	 */
 
+	mbox_copy_send_data_addr(send_mbox, seg_len);
 
+
 
+	/* Set mailbox msg header size. */
 
+	write_mbox_msg_attr(func_to_func, dst_func, dst_aeqn,
 
+			    MBOX_TLP_HEADER_SZ);
 
+
 
+	rte_atomic_thread_fence(rte_memory_order_release); /**< Writing the mbox msg attributes. */
 
+
 
+	/* Wait until the status of the mailbox changes to Complete. */
 
+	while (cnt < MBOX_MSG_POLLING_TIMEOUT) {
 
+		wb_status = get_mbox_status(send_mbox);
 
+		if (MBOX_STATUS_FINISHED(wb_status))
 
+			break;
 
+
 
+		rte_delay_us(10);
 
+		cnt++;
 
+	}
 
+
 
+	if (cnt == MBOX_MSG_POLLING_TIMEOUT) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Send mailbox segment timeout, wb status: 0x%x",
 
+			    wb_status);
 
+		dump_mbox_reg(hwdev);
 
+		return -ETIMEDOUT;
 
+	}
 
+
 
+	if (!MBOX_STATUS_SUCCESS(wb_status)) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Send mailbox segment to function %d error, wb "
 
+			    "status: 0x%x",
 
+			    dst_func, wb_status);
 
+		errcode = MBOX_STATUS_ERRCODE(wb_status);
 
+		return errcode ? errcode : -EFAULT;
 
+	}
 
+
 
+	return 0;
 
+}
 
+
 
+static int
 
+send_mbox_to_func(struct hinic3_mbox *func_to_func, enum hinic3_mod_type mod,
 
+		  u16 cmd, void *msg, u16 msg_len, u16 dst_func,
 
+		  enum hinic3_msg_direction_type direction,
 
+		  enum hinic3_msg_ack_type ack_type,
 
+		  struct mbox_msg_info *msg_info)
 
+{
 
+	int err = 0;
 
+	u32 seq_id = 0;
 
+	u16 seg_len = MBOX_SEG_LEN;
 
+	u16 rsp_aeq_id, left = msg_len;
 
+	u8 *msg_seg = (u8 *)msg;
 
+	u64 header = 0;
 
+
 
+	rsp_aeq_id = HINIC3_MBOX_RSP_MSG_AEQ;
 
+
 
+	err = hinic3_mutex_lock(&func_to_func->msg_send_mutex);
 
+	if (err)
 
+		return err;
 
+
 
+	/* Set the header message. */
 
+	header = HINIC3_MSG_HEADER_SET(msg_len, MSG_LEN) |
 
+		 HINIC3_MSG_HEADER_SET(mod, MODULE) |
 
+		 HINIC3_MSG_HEADER_SET(seg_len, SEG_LEN) |
 
+		 HINIC3_MSG_HEADER_SET(ack_type, NO_ACK) |
 
+		 HINIC3_MSG_HEADER_SET(HINIC3_DATA_INLINE, DATA_TYPE) |
 
+		 HINIC3_MSG_HEADER_SET(SEQ_ID_START_VAL, SEQID) |
 
+		 HINIC3_MSG_HEADER_SET(NOT_LAST_SEGMENT, LAST) |
 
+		 HINIC3_MSG_HEADER_SET(direction, DIRECTION) |
 
+		 HINIC3_MSG_HEADER_SET(cmd, CMD) |
 
+		 /* The VF's offset to it's associated PF. */
 
+		 HINIC3_MSG_HEADER_SET(msg_info->msg_id, MSG_ID) |
 
+		 HINIC3_MSG_HEADER_SET(rsp_aeq_id, AEQ_ID) |
 
+		 HINIC3_MSG_HEADER_SET(HINIC3_MSG_FROM_MBOX, SOURCE) |
 
+		 HINIC3_MSG_HEADER_SET(!!msg_info->status, STATUS);
 
+	/* Loop until all messages are sent. */
 
+	while (!(HINIC3_MSG_HEADER_GET(header, LAST))) {
 
+		if (left <= MBOX_SEG_LEN) {
 
+			header &= ~MBOX_SEGLEN_MASK;
 
+			header |= HINIC3_MSG_HEADER_SET(left, SEG_LEN);
 
+			header |= HINIC3_MSG_HEADER_SET(LAST_SEGMENT, LAST);
 
+
 
+			seg_len = left;
 
+		}
 
+
 
+		err = send_mbox_seg(func_to_func, header, dst_func, msg_seg,
 
+				    seg_len, msg_info);
 
+		if (err) {
 
+			PMD_DRV_LOG(ERR,
 
+				    "Send mbox seg failed, seq_id: 0x%" PRIx64,
 
+				    HINIC3_MSG_HEADER_GET(header, SEQID));
 
+
 
+			goto send_err;
 
+		}
 
+
 
+		left -= MBOX_SEG_LEN;
 
+		msg_seg += MBOX_SEG_LEN;
 
+
 
+		seq_id++;
 
+		header &= ~(HINIC3_MSG_HEADER_SET(HINIC3_MSG_HEADER_SEQID_MASK,
 
+						  SEQID));
 
+		header |= HINIC3_MSG_HEADER_SET(seq_id, SEQID);
 
+	}
 
+
 
+send_err:
 
+	hinic3_mutex_unlock(&func_to_func->msg_send_mutex);
 
+
 
+	return err;
 
+}
 
+
 
+static int
 
+send_tlp_mbox_to_func(struct hinic3_mbox *func_to_func,
 
+		      enum hinic3_mod_type mod, u16 cmd, void *msg, u16 msg_len,
 
+		      u16 dst_func, enum hinic3_msg_direction_type direction,
 
+		      enum hinic3_msg_ack_type ack_type,
 
+		      struct mbox_msg_info *msg_info)
 
+{
 
+	struct hinic3_hwdev *hwdev = func_to_func->hwdev;
 
+	u8 *msg_seg = (u8 *)msg;
 
+	int err = 0;
 
+	u16 rsp_aeq_id;
 
+	u64 header = 0;
 
+
 
+	rsp_aeq_id = HINIC3_MBOX_RSP_MSG_AEQ;
 
+
 
+	err = hinic3_mutex_lock(&func_to_func->msg_send_mutex);
 
+	if (err)
 
+		return err;
 
+
 
+	/* Set the header message. */
 
+	header = HINIC3_MSG_HEADER_SET(MBOX_TLP_HEADER_SZ, MSG_LEN) |
 
+		 HINIC3_MSG_HEADER_SET(MBOX_TLP_HEADER_SZ, SEG_LEN) |
 
+		 HINIC3_MSG_HEADER_SET(mod, MODULE) |
 
+		 HINIC3_MSG_HEADER_SET(LAST_SEGMENT, LAST) |
 
+		 HINIC3_MSG_HEADER_SET(ack_type, NO_ACK) |
 
+		 HINIC3_MSG_HEADER_SET(HINIC3_DATA_DMA, DATA_TYPE) |
 
+		 HINIC3_MSG_HEADER_SET(SEQ_ID_START_VAL, SEQID) |
 
+		 HINIC3_MSG_HEADER_SET(direction, DIRECTION) |
 
+		 HINIC3_MSG_HEADER_SET(cmd, CMD) |
 
+		 /* The VF's offset to it's associated PF. */
 
+		 HINIC3_MSG_HEADER_SET(msg_info->msg_id, MSG_ID) |
 
+		 HINIC3_MSG_HEADER_SET(rsp_aeq_id, AEQ_ID) |
 
+		 HINIC3_MSG_HEADER_SET(HINIC3_MSG_FROM_MBOX, SOURCE) |
 
+		 HINIC3_MSG_HEADER_SET(!!msg_info->status, STATUS) |
 
+		 HINIC3_MSG_HEADER_SET(hinic3_global_func_id(hwdev),
 
+				       SRC_GLB_FUNC_IDX);
 
+
 
+	/* Send a message. */
 
+	err = send_tlp_mbox_seg(func_to_func, header, dst_func, msg_seg,
 
+				msg_len, msg_info);
 
+	if (err) {
 
+		PMD_DRV_LOG(ERR, "Send mbox seg failed, seq_id: 0x%" PRIx64,
 
+			    HINIC3_MSG_HEADER_GET(header, SEQID));
 
+	}
 
+
 
+	hinic3_mutex_unlock(&func_to_func->msg_send_mutex);
 
+
 
+	return err;
 
+}
 
+
 
+/**
 
+ * Set mailbox F2F(Function to Function) event status.
 
+ *
 
+ * @param[out] func_to_func
 
+ * Context for inter-function communication.
 
+ * @param[in] event_flag
 
+ * Event status enumerated value.
 
+ */
 
+static void
 
+set_mbox_to_func_event(struct hinic3_mbox *func_to_func,
 
+		       enum mbox_event_state event_flag)
 
+{
 
+	rte_spinlock_lock(&func_to_func->mbox_lock);
 
+	func_to_func->event_flag = event_flag;
 
+	rte_spinlock_unlock(&func_to_func->mbox_lock);
 
+}
 
+
 
+/**
 
+ * Send data from one function to another and receive responses.
 
+ *
 
+ * @param[in] func_to_func
 
+ * Context for inter-function communication.
 
+ * @param[in] mod
 
+ * Command queue module type.
 
+ * @param[in] cmd
 
+ * Indicate the command to be executed.
 
+  the command to be executed.
 
+ * @param[in] dst_func
 
+ * Indicate destination func.
 
+ * @param[in] buf_in
 
+ * Pointer to the input buffer.
 
+ * @param[in] in_size
 
+ * Input buffer size.
 
+ * @param[out] buf_out
 
+ * Pointer to the output buffer.
 
+ * @param[out] out_size
 
+ * Output buffer size.
 
+ * @param[in] timeout
 
+ * Timeout interval for waiting for a response.
 
+ * @return
 
+ * 0 on success, non-zero on failure.
 
+ */
 
+static int
 
+hinic3_mbox_to_func(struct hinic3_mbox *func_to_func, enum hinic3_mod_type mod,
 
+		    u16 cmd, u16 dst_func, void *buf_in, u16 in_size,
 
+		    void *buf_out, u16 *out_size, u32 timeout)
 
+{
 
+	/* Use mbox_resp to hole data which responded from other function. */
 
+	struct hinic3_recv_mbox *mbox_for_resp = NULL;
 
+	struct mbox_msg_info msg_info = {0};
 
+	struct hinic3_eq *aeq = NULL;
 
+	u16 mbox_rsp_idx;
 
+	u32 time;
 
+	int err;
 
+
 
+	mbox_rsp_idx = (u16)hinic3_mbox_get_index(dst_func);
 
+	mbox_for_resp = &func_to_func->mbox_resp[mbox_rsp_idx];
 
+
 
+	err = hinic3_mutex_lock(&func_to_func->mbox_send_mutex);
 
+	if (err)
 
+		return err;
 
+
 
+	/* Set message ID and start event. */
 
+	msg_info.msg_id = MBOX_MSG_ID_INC(func_to_func);
 
+	set_mbox_to_func_event(func_to_func, EVENT_START);
 
+
 
+	/* Select a function to send messages based on the dst_func type. */
 
+	if (IS_TLP_MBX(dst_func))
 
+		err = send_tlp_mbox_to_func(func_to_func,
 
+			mod, cmd, buf_in, in_size, dst_func,
 
+			HINIC3_MSG_DIRECT_SEND, HINIC3_MSG_ACK, &msg_info);
 
+	else
 
+		err = send_mbox_to_func(func_to_func, mod, cmd, buf_in, in_size,
 
+					dst_func, HINIC3_MSG_DIRECT_SEND,
 
+					HINIC3_MSG_ACK, &msg_info);
 
+
 
+	if (err) {
 
+		PMD_DRV_LOG(ERR, "Send mailbox failed, msg_id: %d",
 
+			    msg_info.msg_id);
 
+		set_mbox_to_func_event(func_to_func, EVENT_FAIL);
 
+		goto send_err;
 
+	}
 
+
 
+	/* Wait for the response message. */
 
+	time = msecs_to_jiffies(timeout ? timeout : HINIC3_MBOX_COMP_TIME);
 
+	aeq = &func_to_func->hwdev->aeqs->aeq[HINIC3_MBOX_RSP_MSG_AEQ];
 
+	err = hinic3_aeq_poll_msg(aeq, time, NULL);
 
+	if (err) {
 
+		set_mbox_to_func_event(func_to_func, EVENT_TIMEOUT);
 
+		PMD_DRV_LOG(ERR, "Send mailbox message time out");
 
+		err = -ETIMEDOUT;
 
+		goto send_err;
 
+	}
 
+
 
+	/* Check whether mod and command of the rsp message match the sent message. */
 
+	if (mod != mbox_for_resp->mod || cmd != mbox_for_resp->cmd) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Invalid response mbox message, mod: 0x%x, cmd: "
 
+			    "0x%x, expect mod: 0x%x, cmd: 0x%x",
 
+			    mbox_for_resp->mod, mbox_for_resp->cmd, mod, cmd);
 
+		err = -EFAULT;
 
+		goto send_err;
 
+	}
 
+
 
+	/* Check the response status. */
 
+	if (mbox_for_resp->msg_info.status) {
 
+		err = mbox_for_resp->msg_info.status;
 
+		goto send_err;
 
+	}
 
+
 
+	/* Check whether the length of the response message is valid. */
 
+	if (buf_out && out_size) {
 
+		if (*out_size < mbox_for_resp->mbox_len) {
 
+			PMD_DRV_LOG(ERR,
 
+				"Invalid response mbox message length: %d for "
 
+				"mod: %d cmd: %d, should less than: %d",
 
+				mbox_for_resp->mbox_len, mod, cmd, *out_size);
 
+			err = -EFAULT;
 
+			goto send_err;
 
+		}
 
+
 
+		if (mbox_for_resp->mbox_len)
 
+			memcpy(buf_out, mbox_for_resp->mbox,
 
+			       (size_t)(mbox_for_resp->mbox_len));
 
+

drivers/net/hinic3/base/hinic3_mbox.c line 988

Quoted 10 lines
drivers/net/hinic3/base/hinic3_mbox.c +5 -0
 
+		*out_size = mbox_for_resp->mbox_len;
 
+	}
 
+
 
+send_err:
 
+	hinic3_mutex_unlock(&func_to_func->mbox_send_mutex);

there are two lock in func_to_func, one is spinlock, one is mutex lock. how to use them?

Quoted 1,233 lines
drivers/net/hinic3/base/hinic3_mbox.c +411 -0
+
 
+	return err;
 
+}
 
+
 
+static int
 
+mbox_func_params_valid(__rte_unused struct hinic3_mbox *func_to_func,
 
+		       void *buf_in, u16 in_size)
 
+{
 
+	if (!buf_in || !in_size)
 
+		return -EINVAL;
 
+
 
+	if (in_size > HINIC3_MBOX_DATA_SIZE) {
 
+		PMD_DRV_LOG(ERR, "Mbox msg len(%d) exceed limit(%" PRIu64 ")",
 
+			    in_size, HINIC3_MBOX_DATA_SIZE);
 
+		return -EINVAL;
 
+	}
 
+
 
+	return 0;
 
+}
 
+
 
+static int
 
+hinic3_mbox_to_func_no_ack(struct hinic3_hwdev *hwdev, u16 func_idx,
 
+			   enum hinic3_mod_type mod, u16 cmd, void *buf_in,
 
+			   u16 in_size)
 
+{
 
+	struct hinic3_mbox *func_to_func = hwdev->func_to_func;
 
+	struct mbox_msg_info msg_info = {0};
 
+	int err;
 
+
 
+	err = mbox_func_params_valid(hwdev->func_to_func, buf_in, in_size);
 
+	if (err)
 
+		return err;
 
+
 
+	err = hinic3_mutex_lock(&func_to_func->mbox_send_mutex);
 
+	if (err)
 
+		return err;
 
+
 
+	if (IS_TLP_MBX(func_idx))
 
+		err = send_tlp_mbox_to_func(func_to_func,
 
+			mod, cmd, buf_in, in_size, func_idx,
 
+			HINIC3_MSG_DIRECT_SEND, HINIC3_MSG_NO_ACK, &msg_info);
 
+	else
 
+		err = send_mbox_to_func(func_to_func, mod, cmd, buf_in, in_size,
 
+					func_idx, HINIC3_MSG_DIRECT_SEND,
 
+					HINIC3_MSG_NO_ACK, &msg_info);
 
+	if (err)
 
+		PMD_DRV_LOG(ERR, "Send mailbox no ack failed");
 
+
 
+	hinic3_mutex_unlock(&func_to_func->mbox_send_mutex);
 
+
 
+	return err;
 
+}
 
+
 
+int
 
+hinic3_send_mbox_to_mgmt(struct hinic3_hwdev *hwdev, enum hinic3_mod_type mod,
 
+			 u16 cmd, void *buf_in, u16 in_size, void *buf_out,
 
+			 u16 *out_size, u32 timeout)
 
+{
 
+	struct hinic3_mbox *func_to_func = hwdev->func_to_func;
 
+	int err;
 
+	/* Verify the validity of the input parameters. */
 
+	err = mbox_func_params_valid(func_to_func, buf_in, in_size);
 
+	if (err)
 
+		return err;
 
+
 
+	return hinic3_mbox_to_func(func_to_func, mod, cmd, HINIC3_MGMT_SRC_ID,
 
+				   buf_in, in_size, buf_out, out_size, timeout);
 
+}
 
+
 
+void
 
+hinic3_response_mbox_to_mgmt(struct hinic3_hwdev *hwdev,
 
+			     enum hinic3_mod_type mod, u16 cmd, void *buf_in,
 
+			     u16 in_size, u16 msg_id)
 
+{
 
+	struct mbox_msg_info msg_info;
 
+	u16 dst_func;
 
+
 
+	msg_info.msg_id = (u8)msg_id;
 
+	msg_info.status = 0;
 
+	dst_func = HINIC3_MGMT_SRC_ID;
 
+
 
+	if (IS_TLP_MBX(dst_func))
 
+		send_tlp_mbox_to_func(hwdev->func_to_func, mod, cmd, buf_in,
 
+				      in_size, HINIC3_MGMT_SRC_ID,
 
+				      HINIC3_MSG_RESPONSE, HINIC3_MSG_NO_ACK,
 
+				      &msg_info);
 
+	else
 
+		send_mbox_to_func(hwdev->func_to_func, mod, cmd, buf_in,
 
+				  in_size, HINIC3_MGMT_SRC_ID,
 
+				  HINIC3_MSG_RESPONSE, HINIC3_MSG_NO_ACK,
 
+				  &msg_info);
 
+}
 
+
 
+int
 
+hinic3_send_mbox_to_mgmt_no_ack(struct hinic3_hwdev *hwdev,
 
+				enum hinic3_mod_type mod, u16 cmd, void *buf_in,
 
+				u16 in_size)
 
+{
 
+	struct hinic3_mbox *func_to_func = hwdev->func_to_func;
 
+	int err;
 
+
 
+	err = mbox_func_params_valid(func_to_func, buf_in, in_size);
 
+	if (err)
 
+		return err;
 
+
 
+	return hinic3_mbox_to_func_no_ack(hwdev, HINIC3_MGMT_SRC_ID, mod, cmd,
 
+					  buf_in, in_size);
 
+}
 
+
 
+int
 
+hinic3_mbox_to_pf(struct hinic3_hwdev *hwdev, enum hinic3_mod_type mod, u16 cmd,
 
+		  void *buf_in, u16 in_size, void *buf_out, u16 *out_size,
 
+		  u32 timeout)
 
+{
 
+	int err;
 
+
 
+	if (!hwdev)
 
+		return -EINVAL;
 
+
 
+	/* Check the validity of parameters. */
 
+	err = mbox_func_params_valid(hwdev->func_to_func, buf_in, in_size);
 
+	if (err)
 
+		return err;
 
+
 
+	if (!HINIC3_IS_VF(hwdev)) {
 
+		PMD_DRV_LOG(ERR, "Params error, func_type: %d",
 
+			    hinic3_func_type(hwdev));
 
+		return -EINVAL;
 
+	}
 
+
 
+	/* Sending Email to PF. */
 
+	return hinic3_mbox_to_func(hwdev->func_to_func, mod, cmd,
 
+				   hinic3_pf_id_of_vf(hwdev), buf_in, in_size,
 
+				   buf_out, out_size, timeout);
 
+}
 
+
 
+int
 
+hinic3_mbox_to_vf(struct hinic3_hwdev *hwdev, enum hinic3_mod_type mod,
 
+		  u16 vf_id, u16 cmd, void *buf_in, u16 in_size, void *buf_out,
 
+		  u16 *out_size, u32 timeout)
 
+{
 
+	struct hinic3_mbox *func_to_func = NULL;
 
+	u16 dst_func_idx;
 
+	int err = 0;
 
+
 
+	if (!hwdev)
 
+		return -EINVAL;
 
+
 
+	func_to_func = hwdev->func_to_func;
 
+	err = mbox_func_params_valid(func_to_func, buf_in, in_size);
 
+	if (err)
 
+		return err;
 
+
 
+	if (HINIC3_IS_VF(hwdev)) {
 
+		PMD_DRV_LOG(ERR, "Params error, func_type: %d",
 
+			    hinic3_func_type(hwdev));
 
+		return -EINVAL;
 
+	}
 
+
 
+	if (!vf_id) {
 
+		PMD_DRV_LOG(ERR, "VF id: %d error!", vf_id);
 
+		return -EINVAL;
 
+	}
 
+
 
+	/*
 
+	 * The sum of vf_offset_to_pf + vf_id is the VF's global function id of
 
+	 * VF in this pf.
 
+	 */
 
+	dst_func_idx = hinic3_glb_pf_vf_offset(hwdev) + vf_id;
 
+
 
+	return hinic3_mbox_to_func(func_to_func, mod, cmd, dst_func_idx, buf_in,
 
+				   in_size, buf_out, out_size, timeout);
 
+}
 
+
 
+static int
 
+init_mbox_info(struct hinic3_recv_mbox *mbox_info, int mbox_max_buf_sz)
 
+{
 
+	int err;
 
+
 
+	mbox_info->seq_id = SEQ_ID_MAX_VAL;
 
+
 
+	mbox_info->mbox =
 
+		rte_zmalloc("mbox", (size_t)mbox_max_buf_sz, 1); /*lint !e571*/
 
+	if (!mbox_info->mbox)
 
+		return -ENOMEM;
 
+
 
+	mbox_info->buf_out = rte_zmalloc("mbox_buf_out",
 
+		(size_t)mbox_max_buf_sz, 1); /*lint !e571*/
 
+	if (!mbox_info->buf_out) {
 
+		err = -ENOMEM;
 
+		goto alloc_buf_out_err;
 
+	}
 
+
 
+	return 0;
 
+
 
+alloc_buf_out_err:
 
+	rte_free(mbox_info->mbox);
 
+
 
+	return err;
 
+}
 
+
 
+static void
 
+clean_mbox_info(struct hinic3_recv_mbox *mbox_info)
 
+{
 
+	rte_free(mbox_info->buf_out);
 
+	rte_free(mbox_info->mbox);
 
+}
 
+
 
+static int
 
+alloc_mbox_info(struct hinic3_recv_mbox *mbox_info, int mbox_max_buf_sz)
 
+{
 
+	u16 func_idx, i;
 
+	int err;
 
+
 
+	for (func_idx = 0; func_idx < HINIC3_MAX_FUNCTIONS + 1; func_idx++) {
 
+		err = init_mbox_info(&mbox_info[func_idx], mbox_max_buf_sz);
 
+		if (err) {
 
+			PMD_DRV_LOG(ERR, "Init mbox info failed");
 
+			goto init_mbox_info_err;
 
+		}
 
+	}
 
+
 
+	return 0;
 
+
 
+init_mbox_info_err:
 
+	for (i = 0; i < func_idx; i++)
 
+		clean_mbox_info(&mbox_info[i]);
 
+
 
+	return err;
 
+}
 
+
 
+static void
 
+free_mbox_info(struct hinic3_recv_mbox *mbox_info)
 
+{
 
+	u16 func_idx;
 
+
 
+	for (func_idx = 0; func_idx < HINIC3_MAX_FUNCTIONS + 1; func_idx++)
 
+		clean_mbox_info(&mbox_info[func_idx]);
 
+}
 
+
 
+static void
 
+prepare_send_mbox(struct hinic3_mbox *func_to_func)
 
+{
 
+	struct hinic3_send_mbox *send_mbox = &func_to_func->send_mbox;
 
+
 
+	send_mbox->data = MBOX_AREA(func_to_func->hwdev->hwif);
 
+}
 
+
 
+/**
 
+ * Allocate memory for the write-back state of the mailbox and write to
 
+ * register.
 
+ *
 
+ * @param[in] func_to_func
 
+ * Context for inter-function communication.
 
+ * @return
 
+ * 0 on success, non-zero on failure.
 
+ */
 
+static int
 
+alloc_mbox_wb_status(struct hinic3_mbox *func_to_func)
 
+{
 
+	struct hinic3_send_mbox *send_mbox = &func_to_func->send_mbox;
 
+	struct hinic3_hwdev *hwdev = func_to_func->hwdev;
 
+	u32 addr_h, addr_l;
 
+
 
+	/* Reserved DMA area. */
 
+	send_mbox->wb_mz = hinic3_dma_zone_reserve(hwdev->eth_dev,
 
+		"wb_mz", 0, MBOX_WB_STATUS_LEN,
 
+		RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
 
+	if (!send_mbox->wb_mz)
 
+		return -ENOMEM;
 
+
 
+	send_mbox->wb_vaddr = send_mbox->wb_mz->addr;
 
+	send_mbox->wb_paddr = send_mbox->wb_mz->iova;
 
+	send_mbox->wb_status = send_mbox->wb_vaddr;
 
+
 
+	addr_h = upper_32_bits(send_mbox->wb_paddr);
 
+	addr_l = lower_32_bits(send_mbox->wb_paddr);
 
+
 
+	/* Write info to the register. */
 
+	hinic3_hwif_write_reg(hwdev->hwif, HINIC3_FUNC_CSR_MAILBOX_RESULT_H_OFF,
 
+			      addr_h);
 
+	hinic3_hwif_write_reg(hwdev->hwif, HINIC3_FUNC_CSR_MAILBOX_RESULT_L_OFF,
 
+			      addr_l);
 
+
 
+	return 0;
 
+}
 
+
 
+static void
 
+free_mbox_wb_status(struct hinic3_mbox *func_to_func)
 
+{
 
+	struct hinic3_send_mbox *send_mbox = &func_to_func->send_mbox;
 
+	struct hinic3_hwdev *hwdev = func_to_func->hwdev;
 
+
 
+	hinic3_hwif_write_reg(hwdev->hwif, HINIC3_FUNC_CSR_MAILBOX_RESULT_H_OFF,
 
+			      0);
 
+	hinic3_hwif_write_reg(hwdev->hwif, HINIC3_FUNC_CSR_MAILBOX_RESULT_L_OFF,
 
+			      0);
 
+
 
+	hinic3_memzone_free(send_mbox->wb_mz);
 
+}
 
+
 
+static int
 
+alloc_mbox_tlp_buffer(struct hinic3_mbox *func_to_func)
 
+{
 
+	struct hinic3_send_mbox *send_mbox = &func_to_func->send_mbox;
 
+	struct hinic3_hwdev *hwdev = func_to_func->hwdev;
 
+
 
+	send_mbox->sbuff_mz = hinic3_dma_zone_reserve(hwdev->eth_dev,
 
+		"sbuff_mz", 0, MBOX_MAX_BUF_SZ, MBOX_MAX_BUF_SZ,
 
+		SOCKET_ID_ANY);
 
+	if (!send_mbox->sbuff_mz)
 
+		return -ENOMEM;
 
+
 
+	send_mbox->sbuff_vaddr = send_mbox->sbuff_mz->addr;
 
+	send_mbox->sbuff_paddr = send_mbox->sbuff_mz->iova;
 
+
 
+	return 0;
 
+}
 
+
 
+static void
 
+free_mbox_tlp_buffer(struct hinic3_mbox *func_to_func)
 
+{
 
+	struct hinic3_send_mbox *send_mbox = &func_to_func->send_mbox;
 
+
 
+	hinic3_memzone_free(send_mbox->sbuff_mz);
 
+}
 
+
 
+/**
 
+ * Initialize function to function communication.
 
+ *
 
+ * @param[in] hwdev
 
+ * Pointer to hardware device structure.
 
+ * @return
 
+ * 0 on success, non-zero on failure.
 
+ */
 
+int
 
+hinic3_func_to_func_init(struct hinic3_hwdev *hwdev)
 
+{
 
+	struct hinic3_mbox *func_to_func;
 
+	int err;
 
+
 
+	func_to_func = rte_zmalloc("func_to_func", sizeof(*func_to_func), 1);
 
+	if (!func_to_func)
 
+		return -ENOMEM;
 
+
 
+	hwdev->func_to_func = func_to_func;
 
+	func_to_func->hwdev = hwdev;
 
+	hinic3_mutex_init(&func_to_func->mbox_send_mutex, NULL);
 
+	hinic3_mutex_init(&func_to_func->msg_send_mutex, NULL);
 
+	rte_spinlock_init(&func_to_func->mbox_lock);
 
+
 
+	/* Alloc the memory required by the mailbox. */
 
+	err = alloc_mbox_info(func_to_func->mbox_send, MBOX_MAX_BUF_SZ);
 
+	if (err) {
 
+		PMD_DRV_LOG(ERR, "Alloc mem for mbox_active failed");
 
+		goto alloc_mbox_for_send_err;
 
+	}
 
+
 
+	err = alloc_mbox_info(func_to_func->mbox_resp, MBOX_MAX_BUF_SZ);
 
+	if (err) {
 
+		PMD_DRV_LOG(ERR, "Alloc mem for mbox_passive failed");
 
+		goto alloc_mbox_for_resp_err;
 
+	}
 
+
 
+	err = alloc_mbox_tlp_buffer(func_to_func);
 
+	if (err) {
 
+		PMD_DRV_LOG(ERR, "Alloc mbox send buffer failed");
 
+		goto alloc_tlp_buffer_err;
 
+	}
 
+
 
+	err = alloc_mbox_wb_status(func_to_func);
 
+	if (err) {
 
+		PMD_DRV_LOG(ERR, "Alloc mbox write back status failed");
 
+		goto alloc_wb_status_err;
 
+	}
 
+
 
+	prepare_send_mbox(func_to_func);
 
+
 
+	return 0;
 
+
 
+alloc_wb_status_err:
 
+	free_mbox_tlp_buffer(func_to_func);
 
+
 
+alloc_tlp_buffer_err:
 
+	free_mbox_info(func_to_func->mbox_resp);
 
+
 
+alloc_mbox_for_resp_err:
 
+	free_mbox_info(func_to_func->mbox_send);
 
+
 
+alloc_mbox_for_send_err:
 
+	hinic3_mutex_destroy(&func_to_func->msg_send_mutex);
 
+	hinic3_mutex_destroy(&func_to_func->mbox_send_mutex);
 
+	rte_free(func_to_func);
 
+
 
+	return err;
 
+}
 
+
 
+void
 
+hinic3_func_to_func_free(struct hinic3_hwdev *hwdev)
 
+{
 
+	struct hinic3_mbox *func_to_func = hwdev->func_to_func;
 
+
 
+	free_mbox_wb_status(func_to_func);
 
+	free_mbox_tlp_buffer(func_to_func);
 
+	free_mbox_info(func_to_func->mbox_resp);
 
+	free_mbox_info(func_to_func->mbox_send);
 
+	hinic3_mutex_destroy(&func_to_func->mbox_send_mutex);
 
+	hinic3_mutex_destroy(&func_to_func->msg_send_mutex);
 
+
 
+	rte_free(func_to_func);
 
+}
drivers/net/hinic3/base/hinic3_mbox.h +0 -0

new file mode 100644

index 0000000000..60336000ab

--- /dev/null

+++ b/drivers/net/hinic3/base/hinic3_mbox.h

drivers/net/hinic3/base/hinic3_mbox.h +199 -0
@@ -0,0 +1,199 @@
 
+/* SPDX-License-Identifier: BSD-3-Clause
 
+ * Copyright(c) 2025 Huawei Technologies Co., Ltd
 
+ */
 
+
 
+#ifndef _HINIC3_MBOX_H_
 
+#define _HINIC3_MBOX_H_
 
+
 
+#include "hinic3_mgmt.h"
 
+
 
+#define HINIC3_MBOX_PF_SEND_ERR	      0x1
 
+#define HINIC3_MBOX_PF_BUSY_ACTIVE_FW 0x2
 
+#define HINIC3_MBOX_VF_CMD_ERROR      0x3
 
+
 
+#define HINIC3_MGMT_SRC_ID 0x1FFF
 
+
 
+#define HINIC3_MAX_PF_FUNCS 32
 
+
 
+/* Message header define. */
 
+#define HINIC3_MSG_HEADER_SRC_GLB_FUNC_IDX_SHIFT 0
 
+#define HINIC3_MSG_HEADER_STATUS_SHIFT		 13
 
+#define HINIC3_MSG_HEADER_SOURCE_SHIFT		 15
 
+#define HINIC3_MSG_HEADER_AEQ_ID_SHIFT		 16
 
+#define HINIC3_MSG_HEADER_MSG_ID_SHIFT		 18
 
+#define HINIC3_MSG_HEADER_CMD_SHIFT		 22
 
+
 
+#define HINIC3_MSG_HEADER_MSG_LEN_SHIFT	  32
 
+#define HINIC3_MSG_HEADER_MODULE_SHIFT	  43
 
+#define HINIC3_MSG_HEADER_SEG_LEN_SHIFT	  48
 
+#define HINIC3_MSG_HEADER_NO_ACK_SHIFT	  54
 
+#define HINIC3_MSG_HEADER_DATA_TYPE_SHIFT 55
 
+#define HINIC3_MSG_HEADER_SEQID_SHIFT	  56
 
+#define HINIC3_MSG_HEADER_LAST_SHIFT	  62
 
+#define HINIC3_MSG_HEADER_DIRECTION_SHIFT 63
 
+
 
+#define HINIC3_MSG_HEADER_CMD_MASK		0x3FF
 
+#define HINIC3_MSG_HEADER_MSG_ID_MASK		0xF
 
+#define HINIC3_MSG_HEADER_AEQ_ID_MASK		0x3
 
+#define HINIC3_MSG_HEADER_SOURCE_MASK		0x1
 
+#define HINIC3_MSG_HEADER_STATUS_MASK		0x1
 
+#define HINIC3_MSG_HEADER_SRC_GLB_FUNC_IDX_MASK 0x1FFF
 
+
 
+#define HINIC3_MSG_HEADER_MSG_LEN_MASK	 0x7FF
 
+#define HINIC3_MSG_HEADER_MODULE_MASK	 0x1F
 
+#define HINIC3_MSG_HEADER_SEG_LEN_MASK	 0x3F
 
+#define HINIC3_MSG_HEADER_NO_ACK_MASK	 0x1
 
+#define HINIC3_MSG_HEADER_DATA_TYPE_MASK 0x1
 
+#define HINIC3_MSG_HEADER_SEQID_MASK	 0x3F
 
+#define HINIC3_MSG_HEADER_LAST_MASK	 0x1
 
+#define HINIC3_MSG_HEADER_DIRECTION_MASK 0x1
 
+
 
+#define HINIC3_MSG_HEADER_GET(val, field)               \
 
+	(((val) >> HINIC3_MSG_HEADER_##field##_SHIFT) & \
 
+	 HINIC3_MSG_HEADER_##field##_MASK)
 
+#define HINIC3_MSG_HEADER_SET(val, field)                       \
 
+	((u64)(((u64)(val)) & HINIC3_MSG_HEADER_##field##_MASK) \
 
+	 << HINIC3_MSG_HEADER_##field##_SHIFT)
 
+
 
+#define IS_TLP_MBX(dst_func) ((dst_func) == HINIC3_MGMT_SRC_ID)
 
+
 
+enum hinic3_msg_direction_type {
 
+	HINIC3_MSG_DIRECT_SEND = 0,
 
+	HINIC3_MSG_RESPONSE = 1
 
+};
 
+
 
+enum hinic3_msg_segment_type { NOT_LAST_SEGMENT = 0, LAST_SEGMENT = 1 };
 
+
 
+enum hinic3_msg_ack_type { HINIC3_MSG_ACK, HINIC3_MSG_NO_ACK };
 
+
 
+enum hinic3_data_type { HINIC3_DATA_INLINE = 0, HINIC3_DATA_DMA = 1 };
 
+
 
+enum hinic3_msg_src_type { HINIC3_MSG_FROM_MGMT = 0, HINIC3_MSG_FROM_MBOX = 1 };
 
+
 
+enum hinic3_msg_aeq_type {
 
+	HINIC3_ASYNC_MSG_AEQ = 0,
 
+	/* Indicate dst_func or mgmt cpu which aeq to response mbox message. */
 
+	HINIC3_MBOX_RSP_MSG_AEQ = 1,
 
+	/* Indicate mgmt cpu which aeq to response api cmd message. */
 
+	HINIC3_MGMT_RSP_MSG_AEQ = 2
 
+};
 
+
 
+enum hinic3_mbox_seg_errcode {
 
+	MBOX_ERRCODE_NO_ERRORS = 0,
 
+	/* VF sends the mailbox data to the wrong destination functions. */
 
+	MBOX_ERRCODE_VF_TO_WRONG_FUNC = 0x100,
 
+	/* PPF sends the mailbox data to the wrong destination functions. */
 
+	MBOX_ERRCODE_PPF_TO_WRONG_FUNC = 0x200,
 
+	/* PF sends the mailbox data to the wrong destination functions. */
 
+	MBOX_ERRCODE_PF_TO_WRONG_FUNC = 0x300,
 
+	/* The mailbox data size is set to all zero. */
 
+	MBOX_ERRCODE_ZERO_DATA_SIZE = 0x400,
 
+	/* The sender func attribute has not been learned by CPI hardware. */
 
+	MBOX_ERRCODE_UNKNOWN_SRC_FUNC = 0x500,
 
+	/* The receiver func attr has not been learned by CPI hardware. */
 
+	MBOX_ERRCODE_UNKNOWN_DES_FUNC = 0x600
 
+};
 
+
 
+enum hinic3_mbox_func_index {
 
+	HINIC3_MBOX_MPU_INDEX = 0,
 
+	HINIC3_MBOX_PF_INDEX = 1,
 
+	HINIC3_MAX_FUNCTIONS = 2,
 
+};
 
+
 
[… more lines in the raw message]

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Part of the diff is not shown here. View the raw message.

#148 lore raw

PATCH v5 12/18 net/hinic3: add device initialization

In reply to Feifei Wang

From: Xin Wang <[email protected]>

This patch contains data structures and function codes related to device initialization.

---
 drivers/net/hinic3/hinic3_ethdev.c | 514 +++++++++++++++++++++++++++++
 drivers/net/hinic3/hinic3_ethdev.h | 119 +++++++
 2 files changed, 633 insertions(+)
 create mode 100644 drivers/net/hinic3/hinic3_ethdev.c
 create mode 100644 drivers/net/hinic3/hinic3_ethdev.h
drivers/net/hinic3/hinic3_ethdev.c new file +514 -0
new file mode 100644
index 0000000000..e6666a4d87
@@ -0,0 +1,514 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2025 Huawei Technologies Co., Ltd
+ */
+
+#include <rte_pci.h>
+#include <rte_bus_pci.h>
+#include <rte_mbuf.h>
+#include <rte_malloc.h>
+#include <rte_mempool.h>
+#include <rte_errno.h>
+#include <rte_ether.h>
+
+#include "base/hinic3_compat.h"
+#include "base/hinic3_csr.h"
+#include "base/hinic3_wq.h"
+#include "base/hinic3_eqs.h"
+#include "base/hinic3_cmdq.h"
+#include "base/hinic3_hwdev.h"
+#include "base/hinic3_hwif.h"
+#include "base/hinic3_hw_cfg.h"
+#include "base/hinic3_hw_comm.h"
+#include "base/hinic3_nic_cfg.h"
+#include "base/hinic3_nic_event.h"
+#include "hinic3_ethdev.h"
+
+/**
+ * Interrupt handler triggered by NIC for handling specific event.
+ *
+ * @param[in] param
+ * The address of parameter (struct rte_eth_dev *) registered before.
+ */
+static void
+hinic3_dev_interrupt_handler(void *param)
+{
+	struct rte_eth_dev *dev = param;
+	struct hinic3_nic_dev *nic_dev = HINIC3_ETH_DEV_TO_PRIVATE_NIC_DEV(dev);
+
+	if (!hinic3_get_bit(HINIC3_DEV_INTR_EN, &nic_dev->dev_status)) {
+		PMD_DRV_LOG(WARNING,
+			    "Intr is disabled, ignore intr event, "
+			    "dev_name: %s, port_id: %d",
+			    nic_dev->dev_name, dev->data->port_id);
+		return;
+	}
+
+	/* Aeq0 msg handler. */
+	hinic3_dev_handle_aeq_event(nic_dev->hwdev, param);
+}
+
+static void
+hinic3_deinit_sw_rxtxqs(struct hinic3_nic_dev *nic_dev)
+{
+	rte_free(nic_dev->txqs);
+	nic_dev->txqs = NULL;
+
+	rte_free(nic_dev->rxqs);
+	nic_dev->rxqs = NULL;
+}
+
+/**
+ * Init mac_vlan table in hardwares.
+ *
+ * @param[in] eth_dev
+ * Pointer to ethernet device structure.
+ *
+ * @return
+ * 0 on success, non-zero on failure.
+ */
+static int
+hinic3_init_mac_table(struct rte_eth_dev *eth_dev)
+{
+	struct hinic3_nic_dev *nic_dev =
+		HINIC3_ETH_DEV_TO_PRIVATE_NIC_DEV(eth_dev);
+	u8 addr_bytes[RTE_ETHER_ADDR_LEN];
+	u16 func_id = 0;
+	int err = 0;
+
+	err = hinic3_get_default_mac(nic_dev->hwdev, addr_bytes,
+				     RTE_ETHER_ADDR_LEN);
+	if (err)
+		return err;
+
+	rte_ether_addr_copy((struct rte_ether_addr *)addr_bytes,
+			    &eth_dev->data->mac_addrs[0]);
+	if (rte_is_zero_ether_addr(&eth_dev->data->mac_addrs[0]))
+		rte_eth_random_addr(eth_dev->data->mac_addrs[0].addr_bytes);
+
+	func_id = hinic3_global_func_id(nic_dev->hwdev);
+	err = hinic3_set_mac(nic_dev->hwdev,
+			     eth_dev->data->mac_addrs[0].addr_bytes, 0,
+			     func_id);
+	if (err && err != HINIC3_PF_SET_VF_ALREADY)
+		return err;
+
+	rte_ether_addr_copy(&eth_dev->data->mac_addrs[0],
+			    &nic_dev->default_addr);
+
+	return 0;
+}
+
+/**
+ * Deinit mac_vlan table in hardware.
+ *
+ * @param[in] eth_dev
+ * Pointer to ethernet device structure.
+ */
+static void
+hinic3_deinit_mac_addr(struct rte_eth_dev *eth_dev)
+{
+	struct hinic3_nic_dev *nic_dev =
+		HINIC3_ETH_DEV_TO_PRIVATE_NIC_DEV(eth_dev);
+	u16 func_id = 0;
+	int err;
+	int i;
+
+	func_id = hinic3_global_func_id(nic_dev->hwdev);
+
+	for (i = 0; i < HINIC3_MAX_UC_MAC_ADDRS; i++) {
+		if (rte_is_zero_ether_addr(&eth_dev->data->mac_addrs[i]))
+			continue;
+
+		err = hinic3_del_mac(nic_dev->hwdev,
+				     eth_dev->data->mac_addrs[i].addr_bytes, 0,
+				     func_id);
+		if (err && err != HINIC3_PF_SET_VF_ALREADY)
+			PMD_DRV_LOG(ERR,
+				    "Delete mac table failed, dev_name: %s",
+				    eth_dev->data->name);
+
+		memset(&eth_dev->data->mac_addrs[i], 0,
+		       sizeof(struct rte_ether_addr));
+	}
+
+	/* Delete multicast mac addrs. */
+	hinic3_delete_mc_addr_list(nic_dev);
+}
+
+/**
+ * Check the valid CoS bitmap to determine the available CoS IDs and set
+ * the default CoS ID to the highest valid one.
+ *
+ * @param[in] hwdev
+ * Pointer to hardware device structure.
+ * @param[out] cos_id
+ * Pointer to store the default CoS ID.
+ *
+ * @return
+ * 0 on success, non-zero on failure.
+ */
+static int
+hinic3_pf_get_default_cos(struct hinic3_hwdev *hwdev, u8 *cos_id)
+{
+	u8 default_cos = 0;
+	u8 valid_cos_bitmap;
+	u8 i;
+
+	valid_cos_bitmap = hwdev->cfg_mgmt->svc_cap.cos_valid_bitmap;
+	if (!valid_cos_bitmap) {
+		PMD_DRV_LOG(ERR, "PF has none cos to support");
+		return -EFAULT;
+	}
+
+	for (i = 0; i < HINIC3_COS_NUM_MAX; i++) {
+		if (valid_cos_bitmap & BIT(i))
+			/* Find max cos id as default cos. */
+			default_cos = i;
+	}
+
+	*cos_id = default_cos;
+
+	return 0;
+}
+
+static int
+hinic3_init_default_cos(struct hinic3_nic_dev *nic_dev)
+{
+	u8 cos_id = 0;
+	int err;
+
+	if (!HINIC3_IS_VF(nic_dev->hwdev)) {
+		err = hinic3_pf_get_default_cos(nic_dev->hwdev, &cos_id);
+		if (err) {
+			PMD_DRV_LOG(ERR, "Get PF default cos failed, err: %d",
+				    err);
+			return err;
+		}
+	} else {
+		err = hinic3_vf_get_default_cos(nic_dev->hwdev, &cos_id);
+		if (err) {
+			PMD_DRV_LOG(ERR, "Get VF default cos failed, err: %d",
+				    err);
+			return err;
+		}
+	}
+
+	nic_dev->default_cos = cos_id;
+	PMD_DRV_LOG(INFO, "Default cos %d", nic_dev->default_cos);
+	return 0;
+}
+
+/**
+ * Initialize Class of Service (CoS). For PF devices, it also sync the link
+ * status with the physical port.
+ *
+ * @param[in] nic_dev
+ * Pointer to NIC device structure.
+ *
+ * @return
+ * 0 on success, non-zero on failure.
+ */
+static int
+hinic3_set_default_hw_feature(struct hinic3_nic_dev *nic_dev)
+{
+	int err;
+
+	err = hinic3_init_default_cos(nic_dev);
+	if (err)
+		return err;
+
+	if (hinic3_func_type(nic_dev->hwdev) == TYPE_VF)
+		return 0;
+
+	err = hinic3_set_link_status_follow(nic_dev->hwdev,
+					    HINIC3_LINK_FOLLOW_PORT);
+	if (err == HINIC3_MGMT_CMD_UNSUPPORTED)
+		PMD_DRV_LOG(WARNING, "Don't support to set link status follow "
+				     "phy port status");
+	else if (err)
+		return err;
+
+	return 0;
+}
+
+/**
+ * Initialize the network function, including hardware configuration, memory
+ * allocation for data structures, MAC address setup, and interrupt enabling.
+ * It also registers interrupt callbacks and sets default hardware features.
+ * If any step fails, appropriate cleanup is performed.
+ *
+ * @param[out] eth_dev
+ * Pointer to ethernet device structure.
+ *
+ * @return
+ * 0 on success, non-zero on failure.
+ */
+static int
+hinic3_func_init(struct rte_eth_dev *eth_dev)
+{
+	struct hinic3_tcam_info *tcam_info = NULL;
+	struct hinic3_nic_dev *nic_dev = NULL;
+	struct rte_pci_device *pci_dev = NULL;
+	int err;
+
+	pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
+
+	/* EAL is secondary and eth_dev is already created. */
+	if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
+		PMD_DRV_LOG(INFO, "Initialize %s in secondary process",
+			    eth_dev->data->name);
+
+		return 0;
+	}
+
+	nic_dev = HINIC3_ETH_DEV_TO_PRIVATE_NIC_DEV(eth_dev);
+	memset(nic_dev, 0, sizeof(*nic_dev));
+	snprintf(nic_dev->dev_name, sizeof(nic_dev->dev_name),
+		     "dbdf-%.4x:%.2x:%.2x.%x", pci_dev->addr.domain,
+		     pci_dev->addr.bus, pci_dev->addr.devid,
+		     pci_dev->addr.function);
+
+	/* Alloc mac_addrs. */
+	eth_dev->data->mac_addrs = rte_zmalloc("hinic3_mac",
+		HINIC3_MAX_UC_MAC_ADDRS * sizeof(struct rte_ether_addr), 0);
+	if (!eth_dev->data->mac_addrs) {
+		PMD_DRV_LOG(ERR,
+			    "Allocate %zx bytes to store MAC addresses "
+			    "failed, dev_name: %s",
+			    HINIC3_MAX_UC_MAC_ADDRS *
+				    sizeof(struct rte_ether_addr),
+			    eth_dev->data->name);
+		err = -ENOMEM;
+		goto alloc_eth_addr_fail;
+	}
+
+	nic_dev->mc_list = rte_zmalloc("hinic3_mc",
+		HINIC3_MAX_MC_MAC_ADDRS * sizeof(struct rte_ether_addr), 0);
+	if (!nic_dev->mc_list) {
+		PMD_DRV_LOG(ERR,
+			    "Allocate %zx bytes to store multicast "
+			    "addresses failed, dev_name: %s",
+			    HINIC3_MAX_MC_MAC_ADDRS *
+				    sizeof(struct rte_ether_addr),
+			    eth_dev->data->name);
+		err = -ENOMEM;
+		goto alloc_mc_list_fail;
+	}
+
+	/* Create hardware device. */
+	nic_dev->hwdev = rte_zmalloc("hinic3_hwdev", sizeof(*nic_dev->hwdev),
+				     RTE_CACHE_LINE_SIZE);
+	if (!nic_dev->hwdev) {
+		PMD_DRV_LOG(ERR, "Allocate hwdev memory failed, dev_name: %s",
+			    eth_dev->data->name);
+		err = -ENOMEM;
+		goto alloc_hwdev_mem_fail;
+	}
+	nic_dev->hwdev->pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
+	nic_dev->hwdev->dev_handle = nic_dev;
+	nic_dev->hwdev->eth_dev = eth_dev;
+	nic_dev->hwdev->port_id = eth_dev->data->port_id;
+
+	err = hinic3_init_hwdev(nic_dev->hwdev);
+	if (err) {
+		PMD_DRV_LOG(ERR, "Init chip hwdev failed, dev_name: %s",
+			    eth_dev->data->name);
+		goto init_hwdev_fail;
+	}
+
+	nic_dev->max_sqs = hinic3_func_max_sqs(nic_dev->hwdev);
+	nic_dev->max_rqs = hinic3_func_max_rqs(nic_dev->hwdev);
+
+	err = hinic3_init_nic_hwdev(nic_dev->hwdev);
+	if (err) {
+		PMD_DRV_LOG(ERR, "Init nic hwdev failed, dev_name: %s",
+			    eth_dev->data->name);
+		goto init_nic_hwdev_fail;
+	}
+
+	err = hinic3_get_feature_from_hw(nic_dev->hwdev, &nic_dev->feature_cap,
+					 1);
+	if (err) {
+		PMD_DRV_LOG(ERR,
+			"Get nic feature from hardware failed, dev_name: %s",
+			eth_dev->data->name);
+		goto get_cap_fail;
+	}
+
+	err = hinic3_init_sw_rxtxqs(nic_dev);
+	if (err) {
+		PMD_DRV_LOG(ERR, "Init sw rxqs or txqs failed, dev_name: %s",
+			    eth_dev->data->name);
+		goto init_sw_rxtxqs_fail;
+	}
+
+	err = hinic3_init_mac_table(eth_dev);
+	if (err) {
+		PMD_DRV_LOG(ERR, "Init mac table failed, dev_name: %s",
+			    eth_dev->data->name);
+		goto init_mac_table_fail;
+	}
+
+	/* Set hardware feature to default status. */
+	err = hinic3_set_default_hw_feature(nic_dev);
+	if (err) {
+		PMD_DRV_LOG(ERR, "Set hw default features failed, dev_name: %s",
+			    eth_dev->data->name);
+		goto set_default_feature_fail;
+	}
+
+	/* Register callback func to eal lib. */
+	err = rte_intr_callback_register(PCI_DEV_TO_INTR_HANDLE(pci_dev),
+					 hinic3_dev_interrupt_handler,
+					 (void *)eth_dev);
+	if (err) {
+		PMD_DRV_LOG(ERR, "Register intr callback failed, dev_name: %s",
+			    eth_dev->data->name);
+		goto reg_intr_cb_fail;
+	}
+
+	/* Enable uio/vfio intr/eventfd mapping. */
+	err = rte_intr_enable(PCI_DEV_TO_INTR_HANDLE(pci_dev));
+	if (err) {
+		PMD_DRV_LOG(ERR, "Enable rte interrupt failed, dev_name: %s",
+			    eth_dev->data->name);
+		goto enable_intr_fail;
+	}
+	tcam_info = &nic_dev->tcam;
+	memset(tcam_info, 0, sizeof(struct hinic3_tcam_info));
+	TAILQ_INIT(&tcam_info->tcam_list);
+	TAILQ_INIT(&tcam_info->tcam_dynamic_info.tcam_dynamic_list);
+	TAILQ_INIT(&nic_dev->filter_ethertype_list);
+	TAILQ_INIT(&nic_dev->filter_fdir_rule_list);
+
+	hinic3_mutex_init(&nic_dev->rx_mode_mutex, NULL);
+
+	hinic3_set_bit(HINIC3_DEV_INTR_EN, &nic_dev->dev_status);
+
+	hinic3_set_bit(HINIC3_DEV_INIT, &nic_dev->dev_status);
+	PMD_DRV_LOG(INFO, "Initialize %s in primary succeed",
+		    eth_dev->data->name);
+
+	/**
+	 * Queue xstats filled automatically by ethdev layer.
+	 */
+	eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
+
+	return 0;
+
+enable_intr_fail:
+	rte_intr_callback_unregister(PCI_DEV_TO_INTR_HANDLE(pci_dev),
+					hinic3_dev_interrupt_handler,
+					(void *)eth_dev);
+
+reg_intr_cb_fail:
+set_default_feature_fail:
+	hinic3_deinit_mac_addr(eth_dev);
+
+init_mac_table_fail:
+	hinic3_deinit_sw_rxtxqs(nic_dev);
+
+init_sw_rxtxqs_fail:
+	hinic3_free_nic_hwdev(nic_dev->hwdev);
+
+get_cap_fail:
+init_nic_hwdev_fail:
+	hinic3_free_hwdev(nic_dev->hwdev);
+	eth_dev->dev_ops = NULL;
+	eth_dev->rx_queue_count = NULL;
+	eth_dev->rx_descriptor_status = NULL;
+	eth_dev->tx_descriptor_status = NULL;
+
+init_hwdev_fail:
+	rte_free(nic_dev->hwdev);
+	nic_dev->hwdev = NULL;
+
+alloc_hwdev_mem_fail:
+	rte_free(nic_dev->mc_list);
+	nic_dev->mc_list = NULL;
+
+alloc_mc_list_fail:
+	rte_free(eth_dev->data->mac_addrs);
+	eth_dev->data->mac_addrs = NULL;
+
+alloc_eth_addr_fail:
+	PMD_DRV_LOG(ERR, "Initialize %s in primary failed",
+		    eth_dev->data->name);
+	return err;
+}
+
+static int
+hinic3_dev_init(struct rte_eth_dev *eth_dev)
+{
+	struct rte_pci_device *pci_dev;
+
+	pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
+
+	PMD_DRV_LOG(INFO, "Initializing %.4x:%.2x:%.2x.%x in %s process",
+		    pci_dev->addr.domain, pci_dev->addr.bus,
+		    pci_dev->addr.devid, pci_dev->addr.function,
+		    (rte_eal_process_type() == RTE_PROC_PRIMARY) ? "primary"
+								 : "secondary");
+
+	PMD_DRV_LOG(INFO, "Network Interface pmd driver version: %s",
+		    HINIC3_PMD_DRV_VERSION);
+
+	return hinic3_func_init(eth_dev);
+}
+
+static int
+hinic3_dev_uninit(struct rte_eth_dev *dev)
+{
+	struct hinic3_nic_dev *nic_dev;
+
+	nic_dev = HINIC3_ETH_DEV_TO_PRIVATE_NIC_DEV(dev);
+	hinic3_clear_bit(HINIC3_DEV_INIT, &nic_dev->dev_status);
+
+	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
+		return 0;
+
+	return hinic3_dev_close(dev);
+}
+
+static const struct rte_pci_id pci_id_hinic3_map[] = {
+#ifdef CONFIG_SP_VID_DID
+	{RTE_PCI_DEVICE(PCI_VENDOR_ID_SPNIC, HINIC3_DEV_ID_STANDARD)},
+	{RTE_PCI_DEVICE(PCI_VENDOR_ID_SPNIC, HINIC3_DEV_ID_VF)},
+#else
+	{RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HINIC3_DEV_ID_STANDARD)},
+	{RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HINIC3_DEV_ID_VF)},
+#endif
+
+	{.vendor_id = 0},
+};
+
+static int
+hinic3_pci_probe(__rte_unused struct rte_pci_driver *pci_drv,
+		 struct rte_pci_device *pci_dev)
+{
+	return rte_eth_dev_pci_generic_probe(pci_dev,
+		sizeof(struct hinic3_nic_dev), hinic3_dev_init);
+}
+
+static int
+hinic3_pci_remove(struct rte_pci_device *pci_dev)
+{
+	return rte_eth_dev_pci_generic_remove(pci_dev, hinic3_dev_uninit);
+}
+
+static struct rte_pci_driver rte_hinic3_pmd = {
+	.id_table = pci_id_hinic3_map,
+	.drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
+	.probe = hinic3_pci_probe,
+	.remove = hinic3_pci_remove,
+};
+
+RTE_PMD_REGISTER_PCI(net_hinic3, rte_hinic3_pmd);
+RTE_PMD_REGISTER_PCI_TABLE(net_hinic3, pci_id_hinic3_map);
+
+RTE_INIT(hinic3_init_log)
+{
+	hinic3_logtype = rte_log_register("pmd.net.hinic3");
+	if (hinic3_logtype >= 0)
+		rte_log_set_level(hinic3_logtype, RTE_LOG_INFO);
+}
drivers/net/hinic3/hinic3_ethdev.h new file +119 -0

119 lines not on this page · raw message

Signature · 1 line
2.45.1.windows.1
#149 lore raw

Re: [V5 12/18] net/hinic3: add device initialization

Quoted 510 lines · Feifei Wang

From: Xin Wang <[email protected]>

This patch contains data structures and function codes

related to device initialization.

---

 drivers/net/hinic3/hinic3_ethdev.c | 514 +++++++++++++++++++++++++++++
 drivers/net/hinic3/hinic3_ethdev.h | 119 +++++++
 2 files changed, 633 insertions(+)
 create mode 100644 drivers/net/hinic3/hinic3_ethdev.c
 create mode 100644 drivers/net/hinic3/hinic3_ethdev.h
drivers/net/hinic3/hinic3_ethdev.c +0 -0

new file mode 100644

index 0000000000..e6666a4d87

--- /dev/null

+++ b/drivers/net/hinic3/hinic3_ethdev.c

drivers/net/hinic3/hinic3_ethdev.c +235 -0
@@ -0,0 +1,514 @@
 
+/* SPDX-License-Identifier: BSD-3-Clause
 
+ * Copyright(c) 2025 Huawei Technologies Co., Ltd
 
+ */
 
+
 
+#include <rte_pci.h>
 
+#include <rte_bus_pci.h>
 
+#include <rte_mbuf.h>
 
+#include <rte_malloc.h>
 
+#include <rte_mempool.h>
 
+#include <rte_errno.h>
 
+#include <rte_ether.h>
 
+
 
+#include "base/hinic3_compat.h"
 
+#include "base/hinic3_csr.h"
 
+#include "base/hinic3_wq.h"
 
+#include "base/hinic3_eqs.h"
 
+#include "base/hinic3_cmdq.h"
 
+#include "base/hinic3_hwdev.h"
 
+#include "base/hinic3_hwif.h"
 
+#include "base/hinic3_hw_cfg.h"
 
+#include "base/hinic3_hw_comm.h"
 
+#include "base/hinic3_nic_cfg.h"
 
+#include "base/hinic3_nic_event.h"
 
+#include "hinic3_ethdev.h"
 
+
 
+/**
 
+ * Interrupt handler triggered by NIC for handling specific event.
 
+ *
 
+ * @param[in] param
 
+ * The address of parameter (struct rte_eth_dev *) registered before.
 
+ */
 
+static void
 
+hinic3_dev_interrupt_handler(void *param)
 
+{
 
+	struct rte_eth_dev *dev = param;
 
+	struct hinic3_nic_dev *nic_dev = HINIC3_ETH_DEV_TO_PRIVATE_NIC_DEV(dev);
 
+
 
+	if (!hinic3_get_bit(HINIC3_DEV_INTR_EN, &nic_dev->dev_status)) {
 
+		PMD_DRV_LOG(WARNING,
 
+			    "Intr is disabled, ignore intr event, "
 
+			    "dev_name: %s, port_id: %d",
 
+			    nic_dev->dev_name, dev->data->port_id);
 
+		return;
 
+	}
 
+
 
+	/* Aeq0 msg handler. */
 
+	hinic3_dev_handle_aeq_event(nic_dev->hwdev, param);
 
+}
 
+
 
+static void
 
+hinic3_deinit_sw_rxtxqs(struct hinic3_nic_dev *nic_dev)
 
+{
 
+	rte_free(nic_dev->txqs);
 
+	nic_dev->txqs = NULL;
 
+
 
+	rte_free(nic_dev->rxqs);
 
+	nic_dev->rxqs = NULL;
 
+}
 
+
 
+/**
 
+ * Init mac_vlan table in hardwares.
 
+ *
 
+ * @param[in] eth_dev
 
+ * Pointer to ethernet device structure.
 
+ *
 
+ * @return
 
+ * 0 on success, non-zero on failure.
 
+ */
 
+static int
 
+hinic3_init_mac_table(struct rte_eth_dev *eth_dev)
 
+{
 
+	struct hinic3_nic_dev *nic_dev =
 
+		HINIC3_ETH_DEV_TO_PRIVATE_NIC_DEV(eth_dev);
 
+	u8 addr_bytes[RTE_ETHER_ADDR_LEN];
 
+	u16 func_id = 0;
 
+	int err = 0;
 
+
 
+	err = hinic3_get_default_mac(nic_dev->hwdev, addr_bytes,
 
+				     RTE_ETHER_ADDR_LEN);
 
+	if (err)
 
+		return err;
 
+
 
+	rte_ether_addr_copy((struct rte_ether_addr *)addr_bytes,
 
+			    &eth_dev->data->mac_addrs[0]);
 
+	if (rte_is_zero_ether_addr(&eth_dev->data->mac_addrs[0]))
 
+		rte_eth_random_addr(eth_dev->data->mac_addrs[0].addr_bytes);
 
+
 
+	func_id = hinic3_global_func_id(nic_dev->hwdev);
 
+	err = hinic3_set_mac(nic_dev->hwdev,
 
+			     eth_dev->data->mac_addrs[0].addr_bytes, 0,
 
+			     func_id);
 
+	if (err && err != HINIC3_PF_SET_VF_ALREADY)
 
+		return err;
 
+
 
+	rte_ether_addr_copy(&eth_dev->data->mac_addrs[0],
 
+			    &nic_dev->default_addr);
 
+
 
+	return 0;
 
+}
 
+
 
+/**
 
+ * Deinit mac_vlan table in hardware.
 
+ *
 
+ * @param[in] eth_dev
 
+ * Pointer to ethernet device structure.
 
+ */
 
+static void
 
+hinic3_deinit_mac_addr(struct rte_eth_dev *eth_dev)
 
+{
 
+	struct hinic3_nic_dev *nic_dev =
 
+		HINIC3_ETH_DEV_TO_PRIVATE_NIC_DEV(eth_dev);
 
+	u16 func_id = 0;
 
+	int err;
 
+	int i;
 
+
 
+	func_id = hinic3_global_func_id(nic_dev->hwdev);
 
+
 
+	for (i = 0; i < HINIC3_MAX_UC_MAC_ADDRS; i++) {
 
+		if (rte_is_zero_ether_addr(&eth_dev->data->mac_addrs[i]))
 
+			continue;
 
+
 
+		err = hinic3_del_mac(nic_dev->hwdev,
 
+				     eth_dev->data->mac_addrs[i].addr_bytes, 0,
 
+				     func_id);
 
+		if (err && err != HINIC3_PF_SET_VF_ALREADY)
 
+			PMD_DRV_LOG(ERR,
 
+				    "Delete mac table failed, dev_name: %s",
 
+				    eth_dev->data->name);
 
+
 
+		memset(&eth_dev->data->mac_addrs[i], 0,
 
+		       sizeof(struct rte_ether_addr));
 
+	}
 
+
 
+	/* Delete multicast mac addrs. */
 
+	hinic3_delete_mc_addr_list(nic_dev);
 
+}
 
+
 
+/**
 
+ * Check the valid CoS bitmap to determine the available CoS IDs and set
 
+ * the default CoS ID to the highest valid one.
 
+ *
 
+ * @param[in] hwdev
 
+ * Pointer to hardware device structure.
 
+ * @param[out] cos_id
 
+ * Pointer to store the default CoS ID.
 
+ *
 
+ * @return
 
+ * 0 on success, non-zero on failure.
 
+ */
 
+static int
 
+hinic3_pf_get_default_cos(struct hinic3_hwdev *hwdev, u8 *cos_id)
 
+{
 
+	u8 default_cos = 0;
 
+	u8 valid_cos_bitmap;
 
+	u8 i;
 
+
 
+	valid_cos_bitmap = hwdev->cfg_mgmt->svc_cap.cos_valid_bitmap;
 
+	if (!valid_cos_bitmap) {
 
+		PMD_DRV_LOG(ERR, "PF has none cos to support");
 
+		return -EFAULT;
 
+	}
 
+
 
+	for (i = 0; i < HINIC3_COS_NUM_MAX; i++) {
 
+		if (valid_cos_bitmap & BIT(i))
 
+			/* Find max cos id as default cos. */
 
+			default_cos = i;
 
+	}
 
+
 
+	*cos_id = default_cos;
 
+
 
+	return 0;
 
+}
 
+
 
+static int
 
+hinic3_init_default_cos(struct hinic3_nic_dev *nic_dev)
 
+{
 
+	u8 cos_id = 0;
 
+	int err;
 
+
 
+	if (!HINIC3_IS_VF(nic_dev->hwdev)) {
 
+		err = hinic3_pf_get_default_cos(nic_dev->hwdev, &cos_id);
 
+		if (err) {
 
+			PMD_DRV_LOG(ERR, "Get PF default cos failed, err: %d",
 
+				    err);
 
+			return err;
 
+		}
 
+	} else {
 
+		err = hinic3_vf_get_default_cos(nic_dev->hwdev, &cos_id);
 
+		if (err) {
 
+			PMD_DRV_LOG(ERR, "Get VF default cos failed, err: %d",
 
+				    err);
 
+			return err;
 
+		}
 
+	}
 
+
 
+	nic_dev->default_cos = cos_id;
 
+	PMD_DRV_LOG(INFO, "Default cos %d", nic_dev->default_cos);
 
+	return 0;
 
+}
 
+
 
+/**
 
+ * Initialize Class of Service (CoS). For PF devices, it also sync the link
 
+ * status with the physical port.
 
+ *
 
+ * @param[in] nic_dev
 
+ * Pointer to NIC device structure.
 
+ *
 
+ * @return
 
+ * 0 on success, non-zero on failure.
 
+ */
 
+static int
 
+hinic3_set_default_hw_feature(struct hinic3_nic_dev *nic_dev)
 
+{
 
+	int err;
 
+
 
+	err = hinic3_init_default_cos(nic_dev);
 
+	if (err)
 
+		return err;
 
+
 
+	if (hinic3_func_type(nic_dev->hwdev) == TYPE_VF)
 
+		return 0;
 
+
 
+	err = hinic3_set_link_status_follow(nic_dev->hwdev,
 
+					    HINIC3_LINK_FOLLOW_PORT);
 
+	if (err == HINIC3_MGMT_CMD_UNSUPPORTED)
 
+		PMD_DRV_LOG(WARNING, "Don't support to set link status follow "
 
+				     "phy port status");
 
+	else if (err)
 
+		return err;
 
+
 
+	return 0;
 
+}
 
+
 
+/**
 
+ * Initialize the network function, including hardware configuration, memory

drivers/net/hinic3/hinic3_ethdev.c line 240

Quoted 10 lines
drivers/net/hinic3/hinic3_ethdev.c +5 -0
 
+ * allocation for data structures, MAC address setup, and interrupt enabling.
 
+ * It also registers interrupt callbacks and sets default hardware features.
 
+ * If any step fails, appropriate cleanup is performed.
 
+ *
 
+ * @param[out] eth_dev

It

Quoted 64 lines

+ * Pointer to ethernet device structure.

+ *

+ * @return

+ * 0 on success, non-zero on failure.

+ */

drivers/net/hinic3/hinic3_ethdev.c +27 -0
+static int
 
+hinic3_func_init(struct rte_eth_dev *eth_dev)
 
+{
 
+	struct hinic3_tcam_info *tcam_info = NULL;
 
+	struct hinic3_nic_dev *nic_dev = NULL;
 
+	struct rte_pci_device *pci_dev = NULL;
 
+	int err;
 
+
 
+	pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
 
+
 
+	/* EAL is secondary and eth_dev is already created. */
 
+	if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
 
+		PMD_DRV_LOG(INFO, "Initialize %s in secondary process",
 
+			    eth_dev->data->name);
 
+
 
+		return 0;
 
+	}
 
+
 
+	nic_dev = HINIC3_ETH_DEV_TO_PRIVATE_NIC_DEV(eth_dev);
 
+	memset(nic_dev, 0, sizeof(*nic_dev));
 
+	snprintf(nic_dev->dev_name, sizeof(nic_dev->dev_name),
 
+		     "dbdf-%.4x:%.2x:%.2x.%x", pci_dev->addr.domain,
 
+		     pci_dev->addr.bus, pci_dev->addr.devid,
 
+		     pci_dev->addr.function);
 
+
 
+	/* Alloc mac_addrs. */
 
+	eth_dev->data->mac_addrs = rte_zmalloc("hinic3_mac",

drivers/net/hinic3/hinic3_ethdev.c line 277

Quoted 10 lines
drivers/net/hinic3/hinic3_ethdev.c +5 -0
 
+		HINIC3_MAX_UC_MAC_ADDRS * sizeof(struct rte_ether_addr), 0);
 
+	if (!eth_dev->data->mac_addrs) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Allocate %zx bytes to store MAC addresses "
 
+			    "failed, dev_name: %s",

Allocate MAC address failed

Quoted 396 lines
drivers/net/hinic3/hinic3_ethdev.c +198 -0
+			    HINIC3_MAX_UC_MAC_ADDRS *
 
+				    sizeof(struct rte_ether_addr),
 
+			    eth_dev->data->name);
 
+		err = -ENOMEM;
 
+		goto alloc_eth_addr_fail;
 
+	}
 
+
 
+	nic_dev->mc_list = rte_zmalloc("hinic3_mc",
 
+		HINIC3_MAX_MC_MAC_ADDRS * sizeof(struct rte_ether_addr), 0);
 
+	if (!nic_dev->mc_list) {
 
+		PMD_DRV_LOG(ERR,
 
+			    "Allocate %zx bytes to store multicast "
 
+			    "addresses failed, dev_name: %s",
 
+			    HINIC3_MAX_MC_MAC_ADDRS *
 
+				    sizeof(struct rte_ether_addr),
 
+			    eth_dev->data->name);
 
+		err = -ENOMEM;
 
+		goto alloc_mc_list_fail;
 
+	}
 
+
 
+	/* Create hardware device. */
 
+	nic_dev->hwdev = rte_zmalloc("hinic3_hwdev", sizeof(*nic_dev->hwdev),
 
+				     RTE_CACHE_LINE_SIZE);
 
+	if (!nic_dev->hwdev) {
 
+		PMD_DRV_LOG(ERR, "Allocate hwdev memory failed, dev_name: %s",
 
+			    eth_dev->data->name);
 
+		err = -ENOMEM;
 
+		goto alloc_hwdev_mem_fail;
 
+	}
 
+	nic_dev->hwdev->pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
 
+	nic_dev->hwdev->dev_handle = nic_dev;
 
+	nic_dev->hwdev->eth_dev = eth_dev;
 
+	nic_dev->hwdev->port_id = eth_dev->data->port_id;
 
+
 
+	err = hinic3_init_hwdev(nic_dev->hwdev);
 
+	if (err) {
 
+		PMD_DRV_LOG(ERR, "Init chip hwdev failed, dev_name: %s",
 
+			    eth_dev->data->name);
 
+		goto init_hwdev_fail;
 
+	}
 
+
 
+	nic_dev->max_sqs = hinic3_func_max_sqs(nic_dev->hwdev);
 
+	nic_dev->max_rqs = hinic3_func_max_rqs(nic_dev->hwdev);
 
+
 
+	err = hinic3_init_nic_hwdev(nic_dev->hwdev);
 
+	if (err) {
 
+		PMD_DRV_LOG(ERR, "Init nic hwdev failed, dev_name: %s",
 
+			    eth_dev->data->name);
 
+		goto init_nic_hwdev_fail;
 
+	}
 
+
 
+	err = hinic3_get_feature_from_hw(nic_dev->hwdev, &nic_dev->feature_cap,
 
+					 1);
 
+	if (err) {
 
+		PMD_DRV_LOG(ERR,
 
+			"Get nic feature from hardware failed, dev_name: %s",
 
+			eth_dev->data->name);
 
+		goto get_cap_fail;
 
+	}
 
+
 
+	err = hinic3_init_sw_rxtxqs(nic_dev);
 
+	if (err) {
 
+		PMD_DRV_LOG(ERR, "Init sw rxqs or txqs failed, dev_name: %s",
 
+			    eth_dev->data->name);
 
+		goto init_sw_rxtxqs_fail;
 
+	}
 
+
 
+	err = hinic3_init_mac_table(eth_dev);
 
+	if (err) {
 
+		PMD_DRV_LOG(ERR, "Init mac table failed, dev_name: %s",
 
+			    eth_dev->data->name);
 
+		goto init_mac_table_fail;
 
+	}
 
+
 
+	/* Set hardware feature to default status. */
 
+	err = hinic3_set_default_hw_feature(nic_dev);
 
+	if (err) {
 
+		PMD_DRV_LOG(ERR, "Set hw default features failed, dev_name: %s",
 
+			    eth_dev->data->name);
 
+		goto set_default_feature_fail;
 
+	}
 
+
 
+	/* Register callback func to eal lib. */
 
+	err = rte_intr_callback_register(PCI_DEV_TO_INTR_HANDLE(pci_dev),
 
+					 hinic3_dev_interrupt_handler,
 
+					 (void *)eth_dev);
 
+	if (err) {
 
+		PMD_DRV_LOG(ERR, "Register intr callback failed, dev_name: %s",
 
+			    eth_dev->data->name);
 
+		goto reg_intr_cb_fail;
 
+	}
 
+
 
+	/* Enable uio/vfio intr/eventfd mapping. */
 
+	err = rte_intr_enable(PCI_DEV_TO_INTR_HANDLE(pci_dev));
 
+	if (err) {
 
+		PMD_DRV_LOG(ERR, "Enable rte interrupt failed, dev_name: %s",
 
+			    eth_dev->data->name);
 
+		goto enable_intr_fail;
 
+	}
 
+	tcam_info = &nic_dev->tcam;
 
+	memset(tcam_info, 0, sizeof(struct hinic3_tcam_info));
 
+	TAILQ_INIT(&tcam_info->tcam_list);
 
+	TAILQ_INIT(&tcam_info->tcam_dynamic_info.tcam_dynamic_list);
 
+	TAILQ_INIT(&nic_dev->filter_ethertype_list);
 
+	TAILQ_INIT(&nic_dev->filter_fdir_rule_list);
 
+
 
+	hinic3_mutex_init(&nic_dev->rx_mode_mutex, NULL);
 
+
 
+	hinic3_set_bit(HINIC3_DEV_INTR_EN, &nic_dev->dev_status);
 
+
 
+	hinic3_set_bit(HINIC3_DEV_INIT, &nic_dev->dev_status);
 
+	PMD_DRV_LOG(INFO, "Initialize %s in primary succeed",
 
+		    eth_dev->data->name);
 
+
 
+	/**
 
+	 * Queue xstats filled automatically by ethdev layer.
 
+	 */
 
+	eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
 
+
 
+	return 0;
 
+
 
+enable_intr_fail:
 
+	rte_intr_callback_unregister(PCI_DEV_TO_INTR_HANDLE(pci_dev),
 
+					hinic3_dev_interrupt_handler,
 
+					(void *)eth_dev);
 
+
 
+reg_intr_cb_fail:
 
+set_default_feature_fail:
 
+	hinic3_deinit_mac_addr(eth_dev);
 
+
 
+init_mac_table_fail:
 
+	hinic3_deinit_sw_rxtxqs(nic_dev);
 
+
 
+init_sw_rxtxqs_fail:
 
+	hinic3_free_nic_hwdev(nic_dev->hwdev);
 
+
 
+get_cap_fail:
 
+init_nic_hwdev_fail:
 
+	hinic3_free_hwdev(nic_dev->hwdev);
 
+	eth_dev->dev_ops = NULL;
 
+	eth_dev->rx_queue_count = NULL;
 
+	eth_dev->rx_descriptor_status = NULL;
 
+	eth_dev->tx_descriptor_status = NULL;
 
+
 
+init_hwdev_fail:
 
+	rte_free(nic_dev->hwdev);
 
+	nic_dev->hwdev = NULL;
 
+
 
+alloc_hwdev_mem_fail:
 
+	rte_free(nic_dev->mc_list);
 
+	nic_dev->mc_list = NULL;
 
+
 
+alloc_mc_list_fail:
 
+	rte_free(eth_dev->data->mac_addrs);
 
+	eth_dev->data->mac_addrs = NULL;
 
+
 
+alloc_eth_addr_fail:
 
+	PMD_DRV_LOG(ERR, "Initialize %s in primary failed",
 
+		    eth_dev->data->name);
 
+	return err;
 
+}
 
+
 
+static int
 
+hinic3_dev_init(struct rte_eth_dev *eth_dev)
 
+{
 
+	struct rte_pci_device *pci_dev;
 
+
 
+	pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
 
+
 
+	PMD_DRV_LOG(INFO, "Initializing %.4x:%.2x:%.2x.%x in %s process",
 
+		    pci_dev->addr.domain, pci_dev->addr.bus,
 
+		    pci_dev->addr.devid, pci_dev->addr.function,
 
+		    (rte_eal_process_type() == RTE_PROC_PRIMARY) ? "primary"
 
+								 : "secondary");
 
+
 
+	PMD_DRV_LOG(INFO, "Network Interface pmd driver version: %s",
 
+		    HINIC3_PMD_DRV_VERSION);
 
+
 
+	return hinic3_func_init(eth_dev);
 
+}
 
+
 
+static int
 
+hinic3_dev_uninit(struct rte_eth_dev *dev)
 
+{
 
+	struct hinic3_nic_dev *nic_dev;
 
+
 
+	nic_dev = HINIC3_ETH_DEV_TO_PRIVATE_NIC_DEV(dev);
 
+	hinic3_clear_bit(HINIC3_DEV_INIT, &nic_dev->dev_status);
 
+
 
+	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
 
+		return 0;
 
+
 
+	return hinic3_dev_close(dev);
 
+}
 
+
 
+static const struct rte_pci_id pci_id_hinic3_map[] = {
 
+#ifdef CONFIG_SP_VID_DID
 
+	{RTE_PCI_DEVICE(PCI_VENDOR_ID_SPNIC, HINIC3_DEV_ID_STANDARD)},

drivers/net/hinic3/hinic3_ethdev.c line 480

Quoted 10 lines
drivers/net/hinic3/hinic3_ethdev.c +5 -0
 
+	{RTE_PCI_DEVICE(PCI_VENDOR_ID_SPNIC, HINIC3_DEV_ID_VF)},
 
+#else
 
+	{RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HINIC3_DEV_ID_STANDARD)},
 
+	{RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HINIC3_DEV_ID_VF)},
 
+#endif

How about support both?

Quoted 56 lines
drivers/net/hinic3/hinic3_ethdev.c +28 -0
+
 
+	{.vendor_id = 0},
 
+};
 
+
 
+static int
 
+hinic3_pci_probe(__rte_unused struct rte_pci_driver *pci_drv,
 
+		 struct rte_pci_device *pci_dev)
 
+{
 
+	return rte_eth_dev_pci_generic_probe(pci_dev,
 
+		sizeof(struct hinic3_nic_dev), hinic3_dev_init);
 
+}
 
+
 
+static int
 
+hinic3_pci_remove(struct rte_pci_device *pci_dev)
 
+{
 
+	return rte_eth_dev_pci_generic_remove(pci_dev, hinic3_dev_uninit);
 
+}
 
+
 
+static struct rte_pci_driver rte_hinic3_pmd = {
 
+	.id_table = pci_id_hinic3_map,
 
+	.drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
 
+	.probe = hinic3_pci_probe,
 
+	.remove = hinic3_pci_remove,
 
+};
 
+
 
+RTE_PMD_REGISTER_PCI(net_hinic3, rte_hinic3_pmd);
 
+RTE_PMD_REGISTER_PCI_TABLE(net_hinic3, pci_id_hinic3_map);
 
+

drivers/net/hinic3/hinic3_ethdev.c line 513

Quoted 10 lines
drivers/net/hinic3/hinic3_ethdev.c +5 -0
 
+RTE_INIT(hinic3_init_log)
 
+{
 
+	hinic3_logtype = rte_log_register("pmd.net.hinic3");
 
+	if (hinic3_logtype >= 0)
 
+		rte_log_set_level(hinic3_logtype, RTE_LOG_INFO);

please use RTE_LOG_REGISTER_SUFFIX

Quoted 253 lines
drivers/net/hinic3/hinic3_ethdev.c +1 -0
+}
drivers/net/hinic3/hinic3_ethdev.h +0 -0

new file mode 100644

index 0000000000..e0d5b4602c

--- /dev/null

+++ b/drivers/net/hinic3/hinic3_ethdev.h

drivers/net/hinic3/hinic3_ethdev.h +119 -0
@@ -0,0 +1,119 @@
 
+/* SPDX-License-Identifier: BSD-3-Clause
 
+ * Copyright(c) 2025 Huawei Technologies Co., Ltd
 
+ */
 
+
 
+#ifndef _HINIC3_ETHDEV_H_
 
+#define _HINIC3_ETHDEV_H_
 
+
 
+#include <rte_ethdev.h>
 
+#include <rte_ethdev_core.h>
 
+
 
+#define HINIC3_PMD_DRV_VERSION "B106"
 
+
 
+#define PCI_DEV_TO_INTR_HANDLE(pci_dev) ((pci_dev)->intr_handle)
 
+
 
+#define HINIC3_PKT_RX_L4_CKSUM_BAD     RTE_MBUF_F_RX_L4_CKSUM_BAD
 
+#define HINIC3_PKT_RX_IP_CKSUM_BAD     RTE_MBUF_F_RX_IP_CKSUM_BAD
 
+#define HINIC3_PKT_RX_IP_CKSUM_UNKNOWN RTE_MBUF_F_RX_IP_CKSUM_UNKNOWN
 
+#define HINIC3_PKT_RX_L4_CKSUM_GOOD    RTE_MBUF_F_RX_L4_CKSUM_GOOD
 
+#define HINIC3_PKT_RX_IP_CKSUM_GOOD    RTE_MBUF_F_RX_IP_CKSUM_GOOD
 
+#define HINIC3_PKT_TX_TCP_SEG	       RTE_MBUF_F_TX_TCP_SEG
 
+#define HINIC3_PKT_TX_UDP_CKSUM	       RTE_MBUF_F_TX_UDP_CKSUM
 
+#define HINIC3_PKT_TX_TCP_CKSUM	       RTE_MBUF_F_TX_TCP_CKSUM
 
+#define HINIC3_PKT_TX_IP_CKSUM	       RTE_MBUF_F_TX_IP_CKSUM
 
+#define HINIC3_PKT_TX_VLAN_PKT	       RTE_MBUF_F_TX_VLAN
 
+#define HINIC3_PKT_TX_L4_MASK	       RTE_MBUF_F_TX_L4_MASK
 
+#define HINIC3_PKT_TX_SCTP_CKSUM       RTE_MBUF_F_TX_SCTP_CKSUM
 
+#define HINIC3_PKT_TX_IPV6	       RTE_MBUF_F_TX_IPV6
 
+#define HINIC3_PKT_TX_IPV4	       RTE_MBUF_F_TX_IPV4
 
+#define HINIC3_PKT_RX_VLAN	       RTE_MBUF_F_RX_VLAN
 
+#define HINIC3_PKT_RX_VLAN_STRIPPED    RTE_MBUF_F_RX_VLAN_STRIPPED
 
+#define HINIC3_PKT_RX_RSS_HASH	       RTE_MBUF_F_RX_RSS_HASH
 
+#define HINIC3_PKT_TX_TUNNEL_MASK      RTE_MBUF_F_TX_TUNNEL_MASK
 
+#define HINIC3_PKT_TX_TUNNEL_VXLAN     RTE_MBUF_F_TX_TUNNEL_VXLAN
 
+#define HINIC3_PKT_TX_OUTER_IP_CKSUM   RTE_MBUF_F_TX_OUTER_IP_CKSUM
 
+#define HINIC3_PKT_TX_OUTER_IPV6       RTE_MBUF_F_TX_OUTER_IPV6
 
+#define HINIC3_PKT_RX_LRO	       RTE_MBUF_F_RX_LRO
 
+#define HINIC3_PKT_TX_L4_NO_CKSUM      RTE_MBUF_F_TX_L4_NO_CKSUM
 
+
 
+#define HINCI3_CPY_MEMPOOL_NAME "cpy_mempool"
 
+/* Mbuf pool for copy invalid mbuf segs. */
 
+#define HINIC3_COPY_MEMPOOL_DEPTH 1024
 
+#define HINIC3_COPY_MEMPOOL_CACHE 128
 
+#define HINIC3_COPY_MBUF_SIZE	  4096
 
+
 
+#define HINIC3_DEV_NAME_LEN 32
 
+#define DEV_STOP_DELAY_MS   100
 
+#define DEV_START_DELAY_MS  100
 
+
 
+#define HINIC3_UINT32_BIT_SIZE (CHAR_BIT * sizeof(uint32_t))
 
+#define HINIC3_VFTA_SIZE       (4096 / HINIC3_UINT32_BIT_SIZE)
 
+#define HINIC3_MAX_QUEUE_NUM   64
 
+
 
+#define HINIC3_ETH_DEV_TO_PRIVATE_NIC_DEV(dev) \
 
+	((struct hinic3_nic_dev *)(dev)->data->dev_private)
 
+
 
+enum hinic3_dev_status {
 
+	HINIC3_DEV_INIT,
 
+	HINIC3_DEV_CLOSE,
 
+	HINIC3_DEV_START,
 
+	HINIC3_DEV_INTR_EN
 
+};
 
+
 
+enum hinic3_tx_cvlan_type {
 
+	HINIC3_TX_TPID0,
 
+};
 
+
 
+enum nic_feature_cap {
 
+	NIC_F_CSUM = BIT(0),
 
+	NIC_F_SCTP_CRC = BIT(1),
 
+	NIC_F_TSO = BIT(2),
 
+	NIC_F_LRO = BIT(3),
 
+	NIC_F_UFO = BIT(4),
 
+	NIC_F_RSS = BIT(5),
 
+	NIC_F_RX_VLAN_FILTER = BIT(6),
 
+	NIC_F_RX_VLAN_STRIP = BIT(7),
 
+	NIC_F_TX_VLAN_INSERT = BIT(8),
 
+	NIC_F_VXLAN_OFFLOAD = BIT(9),
 
+	NIC_F_IPSEC_OFFLOAD = BIT(10),
 
+	NIC_F_FDIR = BIT(11),
 
+	NIC_F_PROMISC = BIT(12),
 
+	NIC_F_ALLMULTI = BIT(13),
 
+};
 
+
 
+#define DEFAULT_DRV_FEATURE 0x3FFF
 
+
 
+struct hinic3_nic_dev {
 
+	struct hinic3_hwdev *hwdev; /**< Hardware device. */
 
+	struct hinic3_txq **txqs;
 
+	struct hinic3_rxq **rxqs;
 
+	struct rte_mempool *cpy_mpool;
 
+
 
+	u16 num_sqs;
 
+	u16 num_rqs;
 
+	u16 max_sqs;
 
+	u16 max_rqs;
 
+
 
+	u16 rx_buff_len;
 
+	u16 mtu_size;
 
+
 
+	u32 rx_mode;
 
+	u8 rx_queue_list[HINIC3_MAX_QUEUE_NUM];
 
+	rte_spinlock_t queue_list_lock;
 
+
 
+	pthread_mutex_t rx_mode_mutex;
 
+
 
+	u32 default_cos;
 
+	u32 rx_csum_en;
 
+
 
+	RTE_ATOMIC(u64) dev_status;
 
+
 
+	struct rte_ether_addr default_addr;
 
+	struct rte_ether_addr *mc_list;
 
+
 
+	char dev_name[HINIC3_DEV_NAME_LEN];
 
+	u64 feature_cap;
 
+	u32 vfta[HINIC3_VFTA_SIZE]; /**< VLAN bitmap. */
 
+};
 
+
 
+#endif /* _HINIC3_ETHDEV_H_ */