Thread View: linux.kernel.netdev
4 messages
4 total messages
Started by "Xin Tian"
Thu, 26 Nov 2026 14:57
[PATCH v2 03/11] net/xsc: add the command queue
Author: "Xin Tian"
Date: Thu, 26 Nov 2026 14:57
Date: Thu, 26 Nov 2026 14:57
1469 lines
44160 bytes
44160 bytes
Provide the mailbox-based firmware command interface: a single polling
thread, the command exec path with status-to-errno mapping, and the
firmware version check. Event-driven mode is deferred.
Signed-off-by: Xin Tian <tianx@yunsilicon.com>
---
.../ethernet/yunsilicon/xsc/common/xsc_cmd.h | 560 ++++++++++++++
.../ethernet/yunsilicon/xsc/common/xsc_core.h | 4 +
.../net/ethernet/yunsilicon/xsc/pci/Makefile | 2 +-
drivers/net/ethernet/yunsilicon/xsc/pci/cmd.c | 708 ++++++++++++++++++
drivers/net/ethernet/yunsilicon/xsc/pci/cmd.h | 87 +++
.../net/ethernet/yunsilicon/xsc/pci/main.c | 18 +
6 files changed, 1378 insertions(+), 1 deletion(-)
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/common/xsc_cmd.h
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/cmd.c
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/cmd.h
diff --git a/drivers/net/ethernet/yunsilicon/xsc/common/xsc_cmd.h b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_cmd.h
new file mode 100644
index 000000000..4dc650f83
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_cmd.h
@@ -0,0 +1,560 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
+ * All rights reserved.
+ */
+
+#ifndef __XSC_CMD_H
+#define __XSC_CMD_H
+
+#include <linux/if_ether.h>
+#include <linux/types.h>
+
+struct xsc_core_device;
+
+#define XSC_CMDQ_VERSION 0x32
+
+#define XSC_BOARD_SN_LEN 32
+#define XSC_MAX_NUM_PCIE_INTF 2
+#define XSC_MAX_PF_NUM_PER_PCIE 8
+
+/* hca_cap mbox version */
+#define XSC_CMD_QUERY_HCA_CAP_V1 1
+
+enum {
+ XSC_CMD_STAT_OK = 0x0,
+ XSC_CMD_STAT_BAD_OP_ERR = 0x2,
+ XSC_CMD_STAT_BAD_PARAM_ERR = 0x3,
+ XSC_CMD_STAT_RES_BUSY = 0x6,
+ XSC_CMD_STAT_IX_ERR = 0xa,
+ XSC_CMD_STAT_QP_FLUSH_BUSY = 0x4b,
+ XSC_CMD_STAT_QP_FLUSH_PENDING = 0x4c,
+ XSC_CMD_STAT_INVAL_FUNC = 0x41,
+};
+
+enum {
+ XSC_CMD_OP_QUERY_HCA_CAP = 0x100,
+ XSC_CMD_OP_QUERY_CMDQ_VERSION = 0x10a,
+ XSC_CMD_OP_FUNCTION_RESET = 0x10c,
+
+ XSC_CMD_OP_CREATE_EQ = 0x301,
+ XSC_CMD_OP_DESTROY_EQ = 0x302,
+
+ XSC_CMD_OP_CREATE_CQ = 0x400,
+ XSC_CMD_OP_DESTROY_CQ = 0x401,
+
+ XSC_CMD_OP_CREATE_QP = 0x500,
+ XSC_CMD_OP_DESTROY_QP = 0x501,
+ XSC_CMD_OP_2RST_QP = 0x50a,
+ XSC_CMD_OP_RTR2RTS_QP = 0x504,
+
+ XSC_CMD_OP_ENABLE_NIC_HCA = 0x810,
+ XSC_CMD_OP_DISABLE_NIC_HCA = 0x811,
+ XSC_CMD_OP_MODIFY_RAW_QP = 0x81f,
+
+ XSC_CMD_OP_QUERY_VPORT_STATE = 0x822,
+ XSC_CMD_OP_QUERY_EVENT_TYPE = 0x831,
+
+ XSC_CMD_OP_ENABLE_MSIX = 0x850,
+
+ XSC_CMD_OP_SET_MTU = 0x1100,
+ XSC_CMD_OP_QUERY_ETH_MAC = 0x1101,
+
+ XSC_CMD_OP_SET_PORT_ADMIN_STATUS = 0x1801,
+};
+
+enum xsc_dma_direct {
+ XSC_DMA_DIR_TO_MAC,
+};
+
+enum {
+ XSC_TBM_CAP_HASH_PPH = 0,
+ XSC_TBM_CAP_RSS,
+ XSC_TBM_CAP_PCT_DROP_CONFIG,
+};
+
+struct xsc_inbox_hdr {
+ __be16 opcode;
+ u8 rsvd[4];
+ __be16 ver;
+};
+
+struct xsc_outbox_hdr {
+ u8 status;
+ u8 rsvd[5];
+ __be16 ver;
+};
+
+/* CQ mbox */
+struct xsc_cq_context {
+ __be16 eqn;
+ __be16 pa_num;
+ __be16 glb_func_id;
+ u8 log_cq_sz;
+ u8 cq_type;
+};
+
+struct xsc_create_cq_mbox_in {
+ struct xsc_inbox_hdr hdr;
+ struct xsc_cq_context ctx;
+ __be64 pas[];
+};
+
+struct xsc_create_cq_mbox_out {
+ struct xsc_outbox_hdr hdr;
+ __be32 cqn;
+ u8 rsvd[4];
+};
+
+struct xsc_destroy_cq_mbox_in {
+ struct xsc_inbox_hdr hdr;
+ __be32 cqn;
+ u8 rsvd[4];
+};
+
+struct xsc_destroy_cq_mbox_out {
+ struct xsc_outbox_hdr hdr;
+ u8 rsvd[8];
+};
+
+/* QP mbox */
+struct __packed xsc_create_qp_request {
+ __be16 input_qpn;
+ __be16 pa_num;
+ u8 qp_type;
+ u8 log_sq_sz;
+ u8 log_rq_sz;
+ u8 dma_direct;
+ __be32 pdn;
+ __be16 cqn_send;
+ __be16 cqn_recv;
+ __be16 glb_funcid;
+ u8 page_shift;
+ u8 rsvd[5];
+ __be64 pas[];
+};
+
+struct xsc_create_qp_mbox_in {
+ struct xsc_inbox_hdr hdr;
+ struct xsc_create_qp_request req;
+};
+
+struct xsc_create_qp_mbox_out {
+ struct xsc_outbox_hdr hdr;
+ __be32 qpn;
+ u8 rsvd[4];
+};
+
+struct xsc_destroy_qp_mbox_in {
+ struct xsc_inbox_hdr hdr;
+ __be32 qpn;
+ u8 rsvd[4];
+};
+
+struct xsc_destroy_qp_mbox_out {
+ struct xsc_outbox_hdr hdr;
+ u8 rsvd[8];
+};
+
+struct xsc_qp_context {
+ __be32 remote_qpn;
+ __be32 cqn_send;
+ __be32 cqn_recv;
+ __be32 next_send_psn;
+ __be32 next_recv_psn;
+ __be32 pdn;
+ __be16 src_udp_port;
+ __be16 path_id;
+ u8 mtu_mode;
+ u8 lag_sel;
+ u8 lag_sel_en;
+ u8 retry_cnt;
+ u8 rnr_retry;
+ u8 dscp;
+ u8 state;
+ u8 hop_limit;
+ u8 dmac[6];
+ u8 smac[6];
+ __be32 dip[4];
+ __be32 sip[4];
+ __be16 ip_type;
+ __be16 grp_id;
+ u8 vlan_valid;
+ u8 dci_cfi_prio_sl;
+ __be16 vlan_id;
+ u8 qp_out_port;
+ u8 pcie_no;
+ __be16 lag_id;
+ __be16 func_id;
+ u8 max_sra;
+ u8 max_rra;
+ u8 no_need_wait;
+ u8 swith_qp_type;
+ u8 rsvd0[2];
+ __be32 qp_access_flags;
+};
+
+struct xsc_modify_qp_mbox_in {
+ struct xsc_inbox_hdr hdr;
+ __be32 qpn;
+ struct xsc_qp_context ctx;
+ u8 no_need_wait;
+};
+
+struct xsc_modify_qp_mbox_out {
+ struct xsc_outbox_hdr hdr;
+ u8 rsvd[8];
+};
+
+struct xsc_modify_raw_qp_request {
+ __be16 qpn;
+ __be16 lag_id;
+ __be16 func_id;
+ u8 dma_direct;
+ u8 prio;
+ u8 qp_out_port;
+ u8 rsvd[7];
+};
+
+struct xsc_modify_raw_qp_mbox_in {
+ struct xsc_inbox_hdr hdr;
+ u8 pcie_no;
+ u8 rsvd[7];
+ struct xsc_modify_raw_qp_request req;
+};
+
+struct xsc_modify_raw_qp_mbox_out {
+ struct xsc_outbox_hdr hdr;
+ u8 rsvd[8];
+};
+
+/* MSIX TABLE mbox */
+struct xsc_msix_table_info_mbox_in {
+ struct xsc_inbox_hdr hdr;
+ __be16 index;
+ u8 rsvd[6];
+};
+
+struct xsc_msix_table_info_mbox_out {
+ struct xsc_outbox_hdr hdr;
+ __be32 addr_lo;
+ __be32 addr_hi;
+ __be32 data;
+};
+
+/* EQ mbox */
+struct xsc_eq_context {
+ __be16 vecidx;
+ __be16 pa_num;
+ u8 log_eq_sz;
+ u8 rsvd1;
+ __be16 glb_func_id;
+ u8 is_async_eq;
+ u8 rsvd;
+};
+
+struct xsc_create_eq_mbox_in {
+ struct xsc_inbox_hdr hdr;
+ struct xsc_eq_context ctx;
+ u8 rsvd[6];
+ __be64 pas[];
+};
+
+struct xsc_create_eq_mbox_out {
+ struct xsc_outbox_hdr hdr;
+ __be32 eqn;
+ u8 rsvd[4];
+};
+
+struct xsc_destroy_eq_mbox_in {
+ struct xsc_inbox_hdr hdr;
+ __be32 eqn;
+ u8 rsvd[4];
+};
+
+struct xsc_destroy_eq_mbox_out {
+ struct xsc_outbox_hdr hdr;
+ u8 rsvd[8];
+};
+
+struct xsc_cmd_query_cmdq_ver_mbox_in {
+ struct xsc_inbox_hdr hdr;
+ u8 rsvd[8];
+};
+
+struct xsc_cmd_query_cmdq_ver_mbox_out {
+ struct xsc_outbox_hdr hdr;
+ __be16 cmdq_ver;
+ u8 rsvd[6];
+};
+
+/* fw mailbox layout, do not change */
+struct xsc_fw_version {
+ u8 rsvd[16];
+};
+
+struct __packed xsc_hca_cap {
+ u8 rsvd1[12];
+ u8 send_seg_num;
+ u8 send_wqe_shift;
+ u8 recv_seg_num;
+ u8 recv_wqe_shift;
+ u8 log_max_srq_sz;
+ u8 log_max_qp_sz;
+ u8 log_max_mtt;
+ u8 log_max_qp;
+ u8 log_max_strq_sz;
+ u8 log_max_srqs;
+ u8 rsvd4[2];
+ u8 log_max_tso;
+ u8 log_max_cq_sz;
+ u8 rsvd6;
+ u8 log_max_cq;
+ u8 log_max_eq_sz;
+ u8 log_max_mkey;
+ u8 log_max_msix;
+ u8 log_max_eq;
+ u8 max_indirection;
+ u8 log_max_mrw_sz;
+ u8 log_max_bsf_list_sz;
+ u8 log_max_klm_list_sz;
+ u8 rsvd_8_0;
+ u8 log_max_ra_req_dc;
+ u8 rsvd_8_1;
+ u8 log_max_ra_res_dc;
+ u8 rsvd9;
+ u8 log_max_ra_req_qp;
+ u8 log_max_qp_depth;
+ u8 log_max_ra_res_qp;
+ __be16 max_vfs;
+ __be16 raweth_qp_id_end;
+ __be16 raw_tpe_qp_num;
+ __be16 max_qp_count;
+ __be16 raweth_qp_id_base;
+ u8 rsvd13;
+ u8 local_ca_ack_delay;
+ u8 max_num_eqs;
+ u8 num_ports;
+ u8 log_max_msg;
+ u8 mac_port;
+ __be16 raweth_rss_qp_id_base;
+ __be16 stat_rate_support;
+ u8 rsvd16[8];
+ __be64 flags;
+ u8 rsvd17;
+ u8 uar_sz;
+ u8 rsvd18;
+ u8 log_pg_sz;
+ __be16 bf_log_bf_reg_size;
+ __be16 msix_base;
+ __be16 msix_num;
+ __be16 max_desc_sz_sq;
+ u8 rsvd20[2];
+ __be16 max_desc_sz_rq;
+ u8 rsvd21[2];
+ __be16 max_desc_sz_sq_dc;
+ u8 rsvd22[4];
+ __be16 max_qp_mcg;
+ u8 rsvd23;
+ u8 log_max_mcg;
+ u8 rsvd24;
+ u8 log_max_pd;
+ u8 rsvd25;
+ u8 log_max_xrcd;
+ u8 rsvd26[40];
+ __be32 uar_page_sz;
+ u8 rsvd27[8];
+ __be32 hw_feature_flag;
+ __be16 pf0_vf_funcid_base;
+ __be16 pf0_vf_funcid_top;
+ __be16 pf1_vf_funcid_base;
+ __be16 pf1_vf_funcid_top;
+ __be16 pcie0_pf_funcid_base;
+ __be16 pcie0_pf_funcid_top;
+ __be16 pcie1_pf_funcid_base;
+ __be16 pcie1_pf_funcid_top;
+ u8 log_msx_atomic_size_qp;
+ u8 pcie_host;
+ u8 rsvd28;
+ u8 log_msx_atomic_size_dc;
+ u8 board_sn[XSC_BOARD_SN_LEN];
+ u8 max_tc;
+ u8 mac_bit;
+ __be16 funcid_to_logic_port;
+ u8 rsvd29[6];
+ u8 nif_port_num;
+ u8 reg_mr_via_cmdq;
+ __be32 hca_core_clock;
+ __be32 max_rwq_indirection_tables;
+ __be32 max_rwq_indirection_table_size;
+ __be32 chip_ver_h;
+ __be32 chip_ver_m;
+ __be32 chip_ver_l;
+ __be32 hotfix_num;
+ __be32 feature_flag;
+ __be32 rx_pkt_len_max;
+ __be32 glb_func_id;
+ __be64 tx_db;
+ __be64 rx_db;
+ __be64 complete_db;
+ __be64 complete_reg;
+ __be64 event_db;
+ __be32 qp_rate_limit_min;
+ __be32 qp_rate_limit_max;
+ struct xsc_fw_version fw_ver;
+ u8 lag_logic_port_ofst;
+ u8 rsvd32[7];
+ /* V1 */
+ __be64 max_mr_size;
+ __be16 max_cmd_in_len;
+ __be16 max_cmd_out_len;
+ u8 rsvd_v2_v3[36];
+ /* V4 */
+ __be16 vf_funcid_base[XSC_MAX_NUM_PCIE_INTF][XSC_MAX_PF_NUM_PER_PCIE];
+ __be16 vf_funcid_top[XSC_MAX_NUM_PCIE_INTF][XSC_MAX_PF_NUM_PER_PCIE];
+ __be16 pf_funcid_base[XSC_MAX_NUM_PCIE_INTF];
+ __be16 pf_funcid_top[XSC_MAX_NUM_PCIE_INTF];
+ u8 pcie_no;
+ u8 pf_id;
+ __be16 vf_id;
+ u8 pcie_host_num;
+ u8 pf_num_per_pcie;
+ u8 rsvd_v6[46];
+};
+
+struct xsc_cmd_query_hca_cap_mbox_in {
+ struct xsc_inbox_hdr hdr;
+ __be16 cpu_num;
+ u8 rsvd[6];
+};
+
+struct xsc_cmd_query_hca_cap_mbox_out {
+ struct xsc_outbox_hdr hdr;
+ u8 rsvd0[8];
+ struct xsc_hca_cap hca_cap;
+};
+
+struct xsc_query_vport_state_out {
+ struct xsc_outbox_hdr hdr;
+ u8 state;
+#define XSC_QUERY_VPORT_ADMIN_STATE_MASK GENMASK(3, 0)
+#define XSC_QUERY_VPORT_STATE_MASK GENMASK(7, 4)
+};
+
+struct xsc_query_vport_state_in {
+ struct xsc_inbox_hdr hdr;
+ __be32 data0;
+#define XSC_QUERY_VPORT_OTHER_VPORT BIT(0)
+#define XSC_QUERY_VPORT_VPORT_NUM_MASK GENMASK(16, 1)
+};
+
+enum {
+ XSC_CMD_EVENT_RESP_CHANGE_LINK = BIT(0),
+};
+
+struct xsc_event_resp {
+ u8 resp_cmd_type;
+};
+
+struct xsc_event_query_type_mbox_in {
+ struct xsc_inbox_hdr hdr;
+ u8 rsvd[2];
+};
+
+struct xsc_event_query_type_mbox_out {
+ struct xsc_outbox_hdr hdr;
+ struct xsc_event_resp ctx;
+};
+
+struct xsc_set_mtu_mbox_in {
+ struct xsc_inbox_hdr hdr;
+ __be16 mtu;
+ __be16 rx_buf_sz_min;
+ u8 mac_port;
+ u8 rsvd;
+};
+
+struct xsc_set_mtu_mbox_out {
+ struct xsc_outbox_hdr hdr;
+};
+
+struct xsc_query_eth_mac_mbox_in {
+ struct xsc_inbox_hdr hdr;
+ u8 index;
+};
+
+struct xsc_query_eth_mac_mbox_out {
+ struct xsc_outbox_hdr hdr;
+ u8 mac[ETH_ALEN];
+};
+
+struct xsc_nic_attr {
+ __be16 caps;
+ __be16 caps_mask;
+ u8 mac_addr[ETH_ALEN];
+};
+
+struct xsc_rss_attr {
+ u8 rss_en;
+ u8 hfunc;
+ __be16 rqn_base;
+ __be16 rqn_num;
+ u8 rsvd[2];
+ __be32 hash_tmpl;
+};
+
+struct xsc_cmd_enable_nic_hca_mbox_in {
+ struct xsc_inbox_hdr hdr;
+ struct xsc_nic_attr nic;
+ struct xsc_rss_attr rss;
+};
+
+struct xsc_cmd_enable_nic_hca_mbox_out {
+ struct xsc_outbox_hdr hdr;
+ u8 rsvd0[2];
+};
+
+struct xsc_nic_dis_attr {
+ __be16 caps;
+};
+
+struct xsc_cmd_disable_nic_hca_mbox_in {
+ struct xsc_inbox_hdr hdr;
+ struct xsc_nic_dis_attr nic;
+};
+
+struct xsc_cmd_disable_nic_hca_mbox_out {
+ struct xsc_outbox_hdr hdr;
+ u8 rsvd0[4];
+};
+
+struct xsc_function_reset_mbox_in {
+ struct xsc_inbox_hdr hdr;
+ __be16 glb_func_id;
+ u8 rsvd[6];
+};
+
+struct xsc_function_reset_mbox_out {
+ struct xsc_outbox_hdr hdr;
+ u8 rsvd[8];
+};
+
+struct xsc_event_set_port_admin_status_mbox_in {
+ struct xsc_inbox_hdr hdr;
+ __be16 status;
+};
+
+struct xsc_event_set_port_admin_status_mbox_out {
+ struct xsc_outbox_hdr hdr;
+ __be32 status;
+};
+
+int xsc_cmd_init(struct xsc_core_device *xdev);
+void xsc_cmd_cleanup(struct xsc_core_device *xdev);
+int xsc_cmd_exec(struct xsc_core_device *xdev,
+ void *in, unsigned int in_size,
+ void *out, unsigned int out_size);
+int xsc_cmd_version_check(struct xsc_core_device *xdev);
+
+int xsc_cmd_status_to_err(struct xsc_outbox_hdr *hdr);
+
+#endif
diff --git a/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
index 9580d89a1..a6f0149ed 100644
--- a/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
+++ b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
@@ -24,12 +24,16 @@
#define XSC_PAGE_SHIFT_4K 12
#define XSC_PAGE_SIZE_4K (_AC(1, UL) << XSC_PAGE_SHIFT_4K)
+struct xsc_cmd;
+
struct xsc_core_device {
struct pci_dev *pdev;
struct device *device;
void __iomem *bar;
int bar_num;
+
+ struct xsc_cmd *cmd;
};
void xsc_update_tx_db(struct xsc_core_device *xdev, u32 sqn, u32 next_pid);
diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/Makefile b/drivers/net/ethernet/yunsilicon/xsc/pci/Makefile
index 4a08b92cd..e17a10cc1 100644
--- a/drivers/net/ethernet/yunsilicon/xsc/pci/Makefile
+++ b/drivers/net/ethernet/yunsilicon/xsc/pci/Makefile
@@ -6,4 +6,4 @@ ccflags-y += -I$(srctree)/drivers/net/ethernet/yunsilicon/xsc
obj-$(CONFIG_YUNSILICON_XSC_PCI) += xsc_pci.o
-xsc_pci-y := main.o doorbell.o
+xsc_pci-y := main.o cmd.o doorbell.o
diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/cmd.c b/drivers/net/ethernet/yunsilicon/xsc/pci/cmd.c
new file mode 100644
index 000000000..45dcdb2b4
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/pci/cmd.c
@@ -0,0 +1,708 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
+ * All rights reserved.
+ * Copyright (c) 2013-2016, Mellanox Technologies. All rights reserved.
+ */
+
+#include <linux/bitmap.h>
+#include <linux/delay.h>
+#include <linux/dma-mapping.h>
+#include <linux/kthread.h>
+#include <linux/pci.h>
+#include <linux/slab.h>
+
+#include "common/xsc_cmd.h"
+#include "cmd.h"
+#include "common/xsc_core.h"
+
+#define XSC_CMD_DATA_BLOCK_SIZE 512
+#define XSC_CMD_FIRST_SIZE 8
+
+struct xsc_cmd_prot_block {
+ u8 data[XSC_CMD_DATA_BLOCK_SIZE];
+ u8 rsvd0[48];
+ __be64 next;
+ __be32 block_num;
+ u8 owner_status;
+ u8 token;
+ u8 ctrl_sig;
+ u8 sig;
+};
+
+/* hw layout, do not change */
+struct xsc_cmd_layout {
+ u16 rsv0;
+ u8 rsv[2];
+ __be32 inlen;
+ __be64 in_ptr;
+ __be32 in[XSC_CMD_FIRST_SIZE];
+ __be64 out_ptr;
+ __be32 outlen;
+ u8 token;
+ u8 sig;
+ u8 idx;
+ u8 type_owner;
+};
+
+struct xsc_rsp_layout {
+ u16 rsv0;
+ u8 rsv[2];
+ __be32 out[XSC_RSP_FIRST_SIZE];
+ u8 token;
+ u8 sig;
+ u8 idx;
+ u8 type_owner;
+};
+
+#define XSC_CMD_TYPE_MASK GENMASK(6, 0)
+#define XSC_CMD_OWNER_BIT BIT(7)
+
+enum {
+ XSC_CMDQ_ERR_HW_READ_REQ_ERR = BIT(0),
+ XSC_CMDQ_ERR_HW_WRITE_REQ_ERR = BIT(1),
+ XSC_CMDQ_ERR_REQ_PID_ERR = BIT(2),
+ XSC_CMDQ_ERR_RSP_CID_ERR = BIT(3),
+};
+
+enum {
+ XSC_CMDQ_ELEMENT_SIZE_LOG = 6,
+ XSC_CMDQ_Q_DEPTH_LOG = 5,
+};
+
+static int xsc_alloc_ent(struct xsc_cmd *cmd)
+{
+ unsigned long flags;
+ int ret;
+
+ spin_lock_irqsave(&cmd->alloc_lock, flags);
+ ret = find_first_bit(&cmd->cmd_entry_mask, cmd->max_reg_cmds);
+ if (ret < cmd->max_reg_cmds)
+ clear_bit(ret, &cmd->cmd_entry_mask);
+ spin_unlock_irqrestore(&cmd->alloc_lock, flags);
+
+ return ret < cmd->max_reg_cmds ? ret : -ENOSPC;
+}
+
+static void xsc_free_ent(struct xsc_cmd *cmd, int idx)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&cmd->alloc_lock, flags);
+ set_bit(idx, &cmd->cmd_entry_mask);
+ spin_unlock_irqrestore(&cmd->alloc_lock, flags);
+}
+
+static struct xsc_cmd_layout *xsc_get_inst(struct xsc_cmd *cmd, int idx)
+{
+ return cmd->cmd_buf + (idx << cmd->log_stride);
+}
+
+static struct xsc_rsp_layout *xsc_get_cq_inst(struct xsc_cmd *cmd, int idx)
+{
+ return cmd->cq_buf + (idx << cmd->log_stride);
+}
+
+static u8 xsc_rsp_owner_bit(struct xsc_rsp_layout *rsp)
+{
+ return !!(rsp->type_owner & XSC_CMD_OWNER_BIT);
+}
+
+static u16 xsc_msg_to_opcode(const struct xsc_cmd_msg *in)
+{
+ const struct xsc_inbox_hdr *hdr = (const void *)in->first;
+
+ return be16_to_cpu(hdr->opcode);
+}
+
+static struct xsc_cmd_mailbox *xsc_alloc_cmd_box(struct xsc_core_device *xdev,
+ gfp_t flags)
+{
+ struct xsc_cmd_mailbox *mailbox;
+
+ mailbox = kmalloc_obj(*mailbox, flags);
+ if (!mailbox)
+ return ERR_PTR(-ENOMEM);
+
+ mailbox->buf = dma_pool_zalloc(xdev->cmd->pool, flags, &mailbox->dma);
+ if (!mailbox->buf) {
+ kfree(mailbox);
+ return ERR_PTR(-ENOMEM);
+ }
+ mailbox->next = NULL;
+
+ return mailbox;
+}
+
+static void xsc_free_cmd_box(struct xsc_core_device *xdev,
+ struct xsc_cmd_mailbox *mailbox)
+{
+ dma_pool_free(xdev->cmd->pool, mailbox->buf, mailbox->dma);
+ kfree(mailbox);
+}
+
+static struct xsc_cmd_msg *xsc_alloc_msg(struct xsc_core_device *xdev,
+ unsigned int size,
+ unsigned int first_size)
+{
+ struct xsc_cmd_mailbox *tmp, *head = NULL;
+ struct xsc_cmd_prot_block *block;
+ struct xsc_cmd_msg *msg;
+ unsigned int blen;
+ int err;
+ int n;
+ int i;
+
+ msg = kzalloc_obj(*msg, GFP_KERNEL);
+ if (!msg)
+ return ERR_PTR(-ENOMEM);
+
+ blen = size - min(size, first_size);
+ n = DIV_ROUND_UP(blen, XSC_CMD_DATA_BLOCK_SIZE);
+
+ for (i = 0; i < n; i++) {
+ tmp = xsc_alloc_cmd_box(xdev, GFP_KERNEL);
+ if (IS_ERR(tmp)) {
+ pci_err(xdev->pdev, "failed allocating block\n");
+ err = PTR_ERR(tmp);
+ goto err_free;
+ }
+
+ block = tmp->buf;
+ tmp->next = head;
+ block->next = cpu_to_be64(tmp->next ? tmp->next->dma : 0);
+ block->block_num = cpu_to_be32(n - i - 1);
+ head = tmp;
+ }
+ msg->next = head;
+ msg->len = size;
+ msg->first_size = first_size;
+
+ return msg;
+
+err_free:
+ while (head) {
+ tmp = head->next;
+ xsc_free_cmd_box(xdev, head);
+ head = tmp;
+ }
+ kfree(msg);
+
+ return ERR_PTR(err);
+}
+
+static void xsc_free_msg(struct xsc_core_device *xdev, struct xsc_cmd_msg *msg)
+{
+ struct xsc_cmd_mailbox *head = msg->next;
+ struct xsc_cmd_mailbox *next;
+
+ while (head) {
+ next = head->next;
+ xsc_free_cmd_box(xdev, head);
+ head = next;
+ }
+ kfree(msg);
+}
+
+static int xsc_copy_to_cmd_msg(struct xsc_cmd_msg *to, const void *from,
+ unsigned int size)
+{
+ struct xsc_cmd_prot_block *block;
+ struct xsc_cmd_mailbox *next;
+ const u8 *src = from;
+ unsigned int copy;
+
+ copy = min(size, to->first_size);
+ memcpy(to->first, src, copy);
+ size -= copy;
+ src += copy;
+
+ next = to->next;
+ while (size) {
+ if (!next) {
+ WARN_ONCE(1, "Mailbox too short\n");
+ return -ENOMEM;
+ }
+
+ copy = min(size, XSC_CMD_DATA_BLOCK_SIZE);
+ block = next->buf;
+ memcpy(block->data, src, copy);
+ block->owner_status = 0;
+ src += copy;
+ size -= copy;
+ next = next->next;
+ }
+
+ return 0;
+}
+
+static int xsc_copy_from_rsp_msg(void *to, const struct xsc_cmd_msg *from,
+ unsigned int size)
+{
+ struct xsc_cmd_prot_block *block;
+ struct xsc_cmd_mailbox *next;
+ u8 *dst = to;
+ unsigned int copy;
+
+ copy = min(size, from->first_size);
+ memcpy(dst, from->first, copy);
+ size -= copy;
+ dst += copy;
+
+ next = from->next;
+ while (size) {
+ if (!next) {
+ WARN_ONCE(1, "Mailbox too short\n");
+ return -ENOMEM;
+ }
+
+ copy = min(size, XSC_CMD_DATA_BLOCK_SIZE);
+ block = next->buf;
+ if (!block->owner_status)
+ return -EIO;
+
+ memcpy(dst, block->data, copy);
+ dst += copy;
+ size -= copy;
+ next = next->next;
+ }
+
+ return 0;
+}
+
+static int xsc_cmd_invoke(struct xsc_core_device *xdev, struct xsc_cmd_msg *in,
+ struct xsc_cmd_msg *out, u8 *status)
+{
+ struct xsc_cmd *cmd = xdev->cmd;
+ struct xsc_cmd_layout *lay;
+ struct xsc_cmd_work_ent *ent;
+ unsigned long timeout;
+ unsigned long flags;
+ int err;
+
+ ent = kzalloc_obj(*ent, GFP_KERNEL);
+ if (!ent)
+ return -ENOMEM;
+
+ ent->in = in;
+ ent->out = out;
+ init_completion(&ent->done);
+ down(&cmd->sem);
+ ent->idx = xsc_alloc_ent(cmd);
+ if (ent->idx < 0) {
+ pci_err(xdev->pdev, "failed to allocate command entry\n");
+ up(&cmd->sem);
+ kfree(ent);
+ return -ENOMEM;
+ }
+ ent->token = ent->idx + 1;
+ cmd->ent_arr[ent->idx] = ent;
+
+ spin_lock_irqsave(&cmd->doorbell_lock, flags);
+ lay = xsc_get_inst(cmd, cmd->cmd_pid);
+ memset(lay, 0, sizeof(*lay));
+ memcpy(lay->in, in->first, sizeof(lay->in));
+ if (in->next)
+ lay->in_ptr = cpu_to_be64(in->next->dma);
+ lay->inlen = cpu_to_be32(in->len);
+ if (out->next)
+ lay->out_ptr = cpu_to_be64(out->next->dma);
+ lay->outlen = cpu_to_be32(out->len);
+ lay->type_owner = FIELD_PREP(XSC_CMD_TYPE_MASK, XSC_PCI_CMD_XPORT);
+ lay->token = ent->token;
+ lay->idx = ent->idx;
+ lay->sig = 0xff;
+
+ /* ring doorbell after the descriptor is valid */
+ wmb();
+
+ cmd->cmd_pid = (cmd->cmd_pid + 1) % (1 << cmd->log_sz);
+ xsc_update_cmdq_req_pid(xdev, cmd->cmd_pid);
+ spin_unlock_irqrestore(&cmd->doorbell_lock, flags);
+
+ timeout = msecs_to_jiffies(XSC_CMD_TIMEOUT_MSEC);
+ if (!wait_for_completion_timeout(&ent->done, timeout)) {
+ cmd->cmd_status = XSC_CMD_STATUS_TIMEDOUT;
+ pci_err(xdev->pdev, "wait for opcode 0x%x response timeout\n",
+ xsc_msg_to_opcode(in));
+ /* the entry stays registered for the completion handler */
+ return -ETIMEDOUT;
+ }
+
+ err = ent->ret;
+ *status = ent->status;
+
+ spin_lock_irqsave(&cmd->alloc_lock, flags);
+ if (cmd->ent_arr[ent->idx] == ent)
+ cmd->ent_arr[ent->idx] = NULL;
+ spin_unlock_irqrestore(&cmd->alloc_lock, flags);
+ kfree(ent);
+
+ return err;
+}
+
+static void xsc_cmd_comp_handler(struct xsc_core_device *xdev, u8 idx,
+ struct xsc_rsp_layout *rsp)
+{
+ struct xsc_cmd *cmd = xdev->cmd;
+ struct xsc_cmd_work_ent *ent;
+
+ if (idx >= cmd->max_reg_cmds || (cmd->cmd_entry_mask & BIT(idx))) {
+ pci_err(xdev->pdev, "idx[%d] exceeds max cmds or has no request\n",
+ idx);
+ return;
+ }
+ ent = cmd->ent_arr[idx];
+
+ memcpy(ent->out->first, rsp->out, sizeof(rsp->out));
+ ent->ret = 0;
+ ent->status = ((struct xsc_outbox_hdr *)ent->out->first)->status;
+ cmd->retry_cnt = 0;
+
+ xsc_free_ent(cmd, ent->idx);
+ complete(&ent->done);
+ up(&cmd->sem);
+}
+
+static void xsc_cmd_handle_rsp_before_reload(struct xsc_cmd *cmd,
+ struct xsc_core_device *xdev)
+{
+ u32 rsp_pid, rsp_cid;
+
+ rsp_pid = xsc_get_cmdq_rsp_pid(xdev) & XSC_CMDQ_INDEX_MASK;
+ rsp_cid = xsc_get_cmdq_rsp_cid(xdev) & XSC_CMDQ_INDEX_MASK;
+ if (rsp_pid == rsp_cid)
+ return;
+
+ cmd->cq_cid = rsp_pid;
+ xsc_update_cmdq_rsp_cid(xdev, cmd->cq_cid);
+}
+
+static void xsc_cmd_err_handler(struct xsc_core_device *xdev, u32 raw)
+{
+ struct xsc_cmd *cmd = xdev->cmd;
+ unsigned long flags;
+ int err = 0;
+ int retry = 0;
+ u32 stat = raw;
+
+ while (stat) {
+ err++;
+ if (stat & XSC_CMDQ_ERR_HW_READ_REQ_ERR) {
+ retry = 1;
+ stat &= ~XSC_CMDQ_ERR_HW_READ_REQ_ERR;
+ pci_err(xdev->pdev, "hw read request from host failed\n");
+ } else if (stat & XSC_CMDQ_ERR_HW_WRITE_REQ_ERR) {
+ retry = 1;
+ stat &= ~XSC_CMDQ_ERR_HW_WRITE_REQ_ERR;
+ pci_err(xdev->pdev, "hw write request to fw failed\n");
+ } else if (stat & XSC_CMDQ_ERR_REQ_PID_ERR) {
+ stat &= ~XSC_CMDQ_ERR_REQ_PID_ERR;
+ pci_err(xdev->pdev, "hw reported unexpected req pid\n");
+ } else if (stat & XSC_CMDQ_ERR_RSP_CID_ERR) {
+ stat &= ~XSC_CMDQ_ERR_RSP_CID_ERR;
+ pci_err(xdev->pdev, "hw reported unexpected rsp cid\n");
+ } else {
+ stat = 0;
+ pci_err(xdev->pdev, "ignore unknown cmdq error\n");
+ }
+ }
+
+ if (retry) {
+ if (cmd->retry_cnt >= XSC_CMD_MAX_RETRY_CNT) {
+ pci_warn(xdev->pdev, "error handler retried %d times, stop retrying\n",
+ cmd->retry_cnt);
+ } else {
+ spin_lock_irqsave(&cmd->doorbell_lock, flags);
+ xsc_update_cmdq_req_pid(xdev, cmd->cmd_pid);
+ cmd->retry_cnt++;
+ spin_unlock_irqrestore(&cmd->doorbell_lock, flags);
+ }
+ }
+
+ if (err)
+ xsc_clear_cmdq_err_status(xdev, raw);
+}
+
+static int xsc_cmd_cq_polling(void *data)
+{
+ struct xsc_core_device *xdev = data;
+ struct xsc_cmd *cmd = xdev->cmd;
+ struct xsc_rsp_layout *rsp;
+ u32 err_status;
+ u32 cq_pid;
+
+ while (!kthread_should_stop()) {
+ if (need_resched())
+ schedule();
+
+ err_status = xsc_read_cmdq_err_status(xdev);
+ if (err_status)
+ xsc_cmd_err_handler(xdev, err_status);
+
+ cq_pid = xsc_get_cmdq_rsp_pid(xdev) & XSC_CMDQ_INDEX_MASK;
+ if (cmd->cq_cid == cq_pid) {
+ usleep_range(1000, 2000);
+ continue;
+ }
+
+ rsp = xsc_get_cq_inst(cmd, cmd->cq_cid);
+ if (!cmd->ownerbit_learned) {
+ cmd->ownerbit_learned = 1;
+ cmd->owner_bit = xsc_rsp_owner_bit(rsp);
+ }
+ if (cmd->owner_bit != xsc_rsp_owner_bit(rsp)) {
+ dev_err_ratelimited(&xdev->pdev->dev,
+ "rsp doorbell advanced but buffer not ready %u %u\n",
+ cmd->cq_cid, cq_pid);
+ msleep(20);
+ continue;
+ }
+
+ /* the hardware writes the payload before flipping the owner bit */
+ dma_rmb();
+
+ xsc_cmd_comp_handler(xdev, rsp->idx, rsp);
+
+ cmd->cq_cid = (cmd->cq_cid + 1) % (1 << cmd->log_sz);
+ xsc_update_cmdq_rsp_cid(xdev, cmd->cq_cid);
+ if (cmd->cq_cid == 0)
+ cmd->owner_bit = !cmd->owner_bit;
+ }
+
+ return 0;
+}
+
+int xsc_cmd_status_to_err(struct xsc_outbox_hdr *hdr)
+{
+ switch (hdr->status) {
+ case XSC_CMD_STAT_OK:
+ return 0;
+ case XSC_CMD_STAT_BAD_OP_ERR:
+ return -EOPNOTSUPP;
+ case XSC_CMD_STAT_BAD_PARAM_ERR:
+ return -EINVAL;
+ case XSC_CMD_STAT_RES_BUSY:
+ case XSC_CMD_STAT_QP_FLUSH_BUSY:
+ return -EBUSY;
+ case XSC_CMD_STAT_QP_FLUSH_PENDING:
+ return -EAGAIN;
+ case XSC_CMD_STAT_IX_ERR:
+ case XSC_CMD_STAT_INVAL_FUNC:
+ return -ENODEV;
+ default:
+ return -EIO;
+ }
+}
+
+static int _xsc_cmd_exec(struct xsc_core_device *xdev,
+ void *in, unsigned int in_size,
+ void *out, unsigned int out_size)
+{
+ struct xsc_cmd *cmd = xdev->cmd;
+ struct xsc_cmd_msg *outb;
+ struct xsc_cmd_msg *inb;
+ u8 status = 0;
+ int err;
+
+ if (cmd->cmd_status == XSC_CMD_STATUS_TIMEDOUT)
+ return -ETIMEDOUT;
+
+ inb = xsc_alloc_msg(xdev, in_size, sizeof(inb->first[0]) *
+ XSC_CMD_FIRST_SIZE);
+ if (IS_ERR(inb))
+ return PTR_ERR(inb);
+
+ err = xsc_copy_to_cmd_msg(inb, in, in_size);
+ if (err) {
+ pci_err(xdev->pdev, "copy to cmd msg err %d\n", err);
+ goto err_free_msg;
+ }
+
+ outb = xsc_alloc_msg(xdev, out_size, sizeof(outb->first[0]) *
+ XSC_RSP_FIRST_SIZE);
+ if (IS_ERR(outb)) {
+ err = PTR_ERR(outb);
+ goto err_free_msg;
+ }
+
+ err = xsc_cmd_invoke(xdev, inb, outb, &status);
+ if (err == -ETIMEDOUT)
+ /* the command owns both mailboxes now */
+ return err;
+
+ if (err)
+ goto err_free_rsp;
+
+ if (status) {
+ pci_err(xdev->pdev, "opcode 0x%x failed with status %u\n",
+ xsc_msg_to_opcode(inb), status);
+ xsc_copy_from_rsp_msg(out, outb, out_size);
+ err = -EREMOTEIO;
+ goto err_free_rsp;
+ }
+
+ err = xsc_copy_from_rsp_msg(out, outb, out_size);
+
+err_free_rsp:
+ xsc_free_msg(xdev, outb);
+
+err_free_msg:
+ xsc_free_msg(xdev, inb);
+ return err;
+}
+
+int xsc_cmd_exec(struct xsc_core_device *xdev,
+ void *in, unsigned int in_size,
+ void *out, unsigned int out_size)
+{
+ int err;
+
+ err = _xsc_cmd_exec(xdev, in, in_size, out, out_size);
+ if (err == -EREMOTEIO)
+ err = xsc_cmd_status_to_err((struct xsc_outbox_hdr *)out);
+
+ return err;
+}
+EXPORT_SYMBOL_GPL(xsc_cmd_exec);
+
+int xsc_cmd_init(struct xsc_core_device *xdev)
+{
+ unsigned int size = sizeof(struct xsc_cmd_prot_block);
+ int align = roundup_pow_of_two(size);
+ struct xsc_cmd *cmd;
+ u32 addr_h, addr_l;
+ int err = -ENOMEM;
+
+ cmd = kzalloc_obj(*cmd, GFP_KERNEL);
+ if (!cmd)
+ return -ENOMEM;
+
+ xdev->cmd = cmd;
+
+ cmd->pool = dma_pool_create("xsc_cmd", &xdev->pdev->dev, size, align, 0);
+ if (!cmd->pool)
+ goto err_free_pool;
+
+ cmd->cmd_buf = dma_alloc_coherent(&xdev->pdev->dev, PAGE_SIZE, &cmd->dma,
+ GFP_KERNEL);
+ if (!cmd->cmd_buf)
+ goto err_free_pool;
+
+ cmd->cq_buf = dma_alloc_coherent(&xdev->pdev->dev, PAGE_SIZE,
+ &cmd->cq_dma, GFP_KERNEL);
+ if (!cmd->cq_buf)
+ goto err_free_cmd;
+
+ memset(cmd->cmd_buf, 0, PAGE_SIZE);
+ memset(cmd->cq_buf, 0, PAGE_SIZE);
+
+ cmd->log_sz = XSC_CMDQ_Q_DEPTH_LOG;
+ cmd->log_stride = XSC_CMDQ_ELEMENT_SIZE_LOG;
+ cmd->max_reg_cmds = 1 << cmd->log_sz;
+ bitmap_fill(&cmd->cmd_entry_mask, cmd->max_reg_cmds);
+
+ spin_lock_init(&cmd->alloc_lock);
+ spin_lock_init(&cmd->doorbell_lock);
+ sema_init(&cmd->sem, cmd->max_reg_cmds);
+
+ cmd->cmd_pid = xsc_get_cmdq_req_pid(xdev) & XSC_CMDQ_INDEX_MASK;
+ cmd->cq_cid = xsc_get_cmdq_rsp_cid(xdev) & XSC_CMDQ_INDEX_MASK;
+ cmd->ownerbit_learned = 0;
+ cmd->cmd_status = XSC_CMD_STATUS_NORMAL;
+
+ xsc_cmd_handle_rsp_before_reload(cmd, xdev);
+
+ addr_h = upper_32_bits(cmd->dma);
+ addr_l = lower_32_bits(cmd->dma);
+ xsc_set_cmdq_req_buf_addr(xdev, addr_h, addr_l);
+
+ addr_h = upper_32_bits(cmd->cq_dma);
+ addr_l = lower_32_bits(cmd->cq_dma);
+ xsc_set_cmdq_rsp_buf_addr(xdev, addr_h, addr_l);
+
+ /* make sure firmware sees the complete address before we proceed */
+ wmb();
+
+ xsc_clear_cmdq_err_status(xdev, xsc_read_cmdq_err_status(xdev));
+
+ cmd->cq_task = kthread_create(xsc_cmd_cq_polling, (void *)xdev,
+ "xsc_cmd_cq_polling");
+ if (IS_ERR(cmd->cq_task)) {
+ err = PTR_ERR(cmd->cq_task);
+ pci_err(xdev->pdev, "failed to create cq polling task: %d\n",
+ err);
+ goto err_free_cq;
+ }
+ wake_up_process(cmd->cq_task);
+
+ return 0;
+
+err_free_cq:
+ dma_free_coherent(&xdev->pdev->dev, PAGE_SIZE, cmd->cq_buf, cmd->cq_dma);
+
+err_free_cmd:
+ dma_free_coherent(&xdev->pdev->dev, PAGE_SIZE, cmd->cmd_buf, cmd->dma);
+
+err_free_pool:
+ dma_pool_destroy(cmd->pool);
+
+ kfree(cmd);
+ xdev->cmd = NULL;
+
+ return err;
+}
+
+void xsc_cmd_cleanup(struct xsc_core_device *xdev)
+{
+ struct xsc_cmd *cmd = xdev->cmd;
+ int i;
+
+ if (!cmd)
+ return;
+
+ if (!IS_ERR_OR_NULL(cmd->cq_task)) {
+ kthread_stop(cmd->cq_task);
+ cmd->cq_task = NULL;
+ }
+
+ /* a timed-out command stays registered for a response that may never
+ * come; with the polling thread stopped nothing can touch it anymore
+ */
+ for (i = 0; i < cmd->max_reg_cmds; i++) {
+ if (!cmd->ent_arr[i])
+ continue;
+ xsc_free_msg(xdev, cmd->ent_arr[i]->in);
+ xsc_free_msg(xdev, cmd->ent_arr[i]->out);
+ kfree(cmd->ent_arr[i]);
+ cmd->ent_arr[i] = NULL;
+ }
+
+ dma_free_coherent(&xdev->pdev->dev, PAGE_SIZE, cmd->cq_buf, cmd->cq_dma);
+ dma_free_coherent(&xdev->pdev->dev, PAGE_SIZE, cmd->cmd_buf, cmd->dma);
+ dma_pool_destroy(cmd->pool);
+
+ kfree(cmd);
+ xdev->cmd = NULL;
+}
+
+int xsc_cmd_version_check(struct xsc_core_device *xdev)
+{
+ struct xsc_cmd_query_cmdq_ver_mbox_out out = {};
+ struct xsc_cmd_query_cmdq_ver_mbox_in in = {};
+ int err;
+
+ in.hdr.opcode = cpu_to_be16(XSC_CMD_OP_QUERY_CMDQ_VERSION);
+
+ err = xsc_cmd_exec(xdev, &in, sizeof(in), &out, sizeof(out));
+ if (err)
+ return err;
+
+ if (be16_to_cpu(out.cmdq_ver) != XSC_CMDQ_VERSION) {
+ pci_err(xdev->pdev, "cmdq version mismatch, expected %d got %d\n",
+ XSC_CMDQ_VERSION, be16_to_cpu(out.cmdq_ver));
+ return -EINVAL;
+ }
+
+ return 0;
+}
diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/cmd.h b/drivers/net/ethernet/yunsilicon/xsc/pci/cmd.h
new file mode 100644
index 000000000..c5fc62ae7
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/pci/cmd.h
@@ -0,0 +1,87 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
+ * All rights reserved.
+ */
+
+#ifndef __XSC_PCI_CMD_H
+#define __XSC_PCI_CMD_H
+
+#include <linux/completion.h>
+#include <linux/dma-mapping.h>
+#include <linux/sched.h>
+#include <linux/semaphore.h>
+#include <linux/spinlock.h>
+
+#include "common/xsc_cmd.h"
+
+#define XSC_CMD_MAX_RETRY_CNT 3
+
+enum {
+ XSC_CMD_TIMEOUT_MSEC = 10 * 1000,
+};
+
+enum {
+ XSC_MAX_COMMANDS = 32,
+ XSC_CMDQ_INDEX_MASK = XSC_MAX_COMMANDS - 1,
+ XSC_PCI_CMD_XPORT = 7,
+};
+
+#define XSC_RSP_FIRST_SIZE 14
+
+struct xsc_cmd_mailbox {
+ void *buf;
+ dma_addr_t dma;
+ struct xsc_cmd_mailbox *next;
+};
+
+struct xsc_cmd_msg {
+ u32 len;
+ unsigned int first_size;
+ struct xsc_cmd_mailbox *next;
+ __be32 first[XSC_RSP_FIRST_SIZE];
+};
+
+struct xsc_cmd_work_ent {
+ struct xsc_cmd_msg *in;
+ struct xsc_cmd_msg *out;
+ int idx;
+ struct completion done;
+ int ret;
+ u8 status;
+ u8 token;
+};
+
+enum xsc_cmd_status {
+ XSC_CMD_STATUS_NORMAL,
+ XSC_CMD_STATUS_TIMEDOUT,
+};
+
+struct xsc_cmd {
+ void *cmd_buf;
+ void *cq_buf;
+ dma_addr_t dma;
+ dma_addr_t cq_dma;
+ u16 cmd_pid;
+ u16 cq_cid;
+ u8 owner_bit;
+ u8 retry_cnt;
+ u8 log_sz;
+ u8 log_stride;
+ int max_reg_cmds;
+
+ /* the bitmap holds per-entry slots, the semaphore caps outstanding
+ * commands at max_reg_cmds so xsc_alloc_ent() cannot fail
+ */
+ spinlock_t alloc_lock;
+ /* serializes filling a request slot and ringing its doorbell */
+ spinlock_t doorbell_lock;
+ unsigned long cmd_entry_mask;
+ struct task_struct *cq_task;
+ struct semaphore sem;
+ struct xsc_cmd_work_ent *ent_arr[XSC_MAX_COMMANDS];
+ struct dma_pool *pool;
+ unsigned int ownerbit_learned;
+ u8 cmd_status;
+};
+
+#endif
diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/main.c b/drivers/net/ethernet/yunsilicon/xsc/pci/main.c
index c926507a7..9b08580f5 100644
--- a/drivers/net/ethernet/yunsilicon/xsc/pci/main.c
+++ b/drivers/net/ethernet/yunsilicon/xsc/pci/main.c
@@ -5,6 +5,7 @@
#include <linux/slab.h>
+#include "common/xsc_cmd.h"
#include "common/xsc_core.h"
static const struct pci_device_id xsc_pci_id_table[] = {
@@ -75,8 +76,24 @@ static int xsc_pci_init(struct xsc_core_device *xdev,
xdev->bar_num = bar_num;
xdev->bar = bar_base;
+ err = xsc_cmd_init(xdev);
+ if (err) {
+ pci_err(pdev, "failed to initialize command interface: err=%d\n",
+ err);
+ goto err_io_unmap;
+ }
+
+ err = xsc_cmd_version_check(xdev);
+ if (err) {
+ pci_err(pdev, "failed to check command queue version: err=%d\n",
+ err);
+ goto err_cmd_cleanup;
+ }
+
return 0;
+err_cmd_cleanup:
+ xsc_cmd_cleanup(xdev);
err_io_unmap:
pci_iounmap(pdev, bar_base);
err_clr_master:
@@ -92,6 +109,7 @@ static void xsc_pci_fini(struct xsc_core_device *xdev)
{
struct pci_dev *pdev = xdev->pdev;
+ xsc_cmd_cleanup(xdev);
pci_iounmap(pdev, xdev->bar);
pci_clear_master(pdev);
pci_release_region(pdev, xdev->bar_num);
--
2.43.0
Re: [PATCH v2 03/11] net/xsc: add the command queue
Author: netdev-bot+sashiko
Date: Fri, 27 Nov 2026 15:57
Date: Fri, 27 Nov 2026 15:57
369 lines
21124 bytes
21124 bytes
Thank you for your contribution! Sashiko AI review found 8 potential
issue(s) to consider.
Critical: 0 · High: 2 · Medium: 5 · Low: 1
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [High] Race on cmd->ent_arr[idx] can leave a live command with a NULL
slot, and the polling kthread then dereferences NULL.
- [High] xsc_cmd_cleanup() frees the request ring (cmd_buf), the response
ring (cq_buf), the dma_pool and the mailboxes of timed-out commands…
- [Medium] One command timeout permanently disables the command
interface.
- [Medium] A timed-out command's xsc_cmd_work_ent and its inb/outb
mailboxes can leak permanently.
- [Medium] xsc_cmd_comp_handler() matches a response to a waiter using
only the device-supplied rsp->idx.
- [Medium] The ring is sized so that a full ring cannot be told apart
from an empty one.
- [Medium] xsc_cmd_cq_polling() learns the expected owner/phase bit from
the slot it is about to validate (cmd.c:450-452: `if…
- [Low] The command-queue poller is one kthread per device, and it polls
even when no command is outstanding.
--
net/xsc: add the command queue
This commit adds the mailbox-based firmware command interface for the xsc
driver. A single polling kthread collects responses, command status is
mapped to errno values, and the cmdq version is checked at probe time.
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/cmd.c b/drivers/net/ethernet/yunsilicon/xsc/pci/cmd.c
> new file mode 100644
> index 0000000000000..45dcdb2b47eb0
> --- /dev/null
> +++ b/drivers/net/ethernet/yunsilicon/xsc/pci/cmd.c
> @@ -0,0 +1,708 @@
[ ... ]
> +static int xsc_cmd_invoke(struct xsc_core_device *xdev, struct xsc_cmd_msg *in,
> + struct xsc_cmd_msg *out, u8 *status)
> +{
[ ... ]
> + ent->token = ent->idx + 1;
> + cmd->ent_arr[ent->idx] = ent;
> +
[ ... ]
> + timeout = msecs_to_jiffies(XSC_CMD_TIMEOUT_MSEC);
> + if (!wait_for_completion_timeout(&ent->done, timeout)) {
> + cmd->cmd_status = XSC_CMD_STATUS_TIMEDOUT;
> + pci_err(xdev->pdev, "wait for opcode 0x%x response timeout\n",
> + xsc_msg_to_opcode(in));
> + /* the entry stays registered for the completion handler */
> + return -ETIMEDOUT;
> + }
> +
> + err = ent->ret;
> + *status = ent->status;
> +
> + spin_lock_irqsave(&cmd->alloc_lock, flags);
> + if (cmd->ent_arr[ent->idx] == ent)
> + cmd->ent_arr[ent->idx] = NULL;
> + spin_unlock_irqrestore(&cmd->alloc_lock, flags);
> + kfree(ent);
[Severity: High]
Can a live command end up with a NULL slot here?
xsc_cmd_comp_handler() releases the slot bit with xsc_free_ent() before it
calls complete(). So xsc_alloc_ent() can give the same idx to another caller
while this waiter still has its pointer in ent_arr[]. find_first_bit()
always picks the lowest free bit, so reusing the same idx is the normal
case.
The new owner publishes its entry with the plain
cmd->ent_arr[ent->idx] = ent store above, outside alloc_lock. That makes
this compare-and-clear atomic only against other locked accessors:
T1 (old waiter, idx 0) T2 (new owner of idx 0)
spin_lock(alloc_lock)
ent_arr[0] == ent1 -> true
cmd->ent_arr[0] = ent2;
ent_arr[0] = NULL
spin_unlock(alloc_lock)
When T2's response arrives, xsc_cmd_comp_handler() sees bit 0 allocated
and does this with ent == NULL:
ent = cmd->ent_arr[idx];
memcpy(ent->out->first, rsp->out, sizeof(rsp->out));
Wouldn't that oops the polling kthread? Every later command would then
time out.
If T2 times out instead, xsc_cmd_cleanup() can no longer reach its entry
and mailboxes.
The window is narrow. On PREEMPT_RT it is wider, because spinlock_t holders
can be preempted. In this patch only the serial probe-time version check
runs. However, the semaphore and bitmap allow 32 concurrent commands, and
later patches in the series add concurrent xsc_cmd_exec() callers.
> +
> + return err;
> +}
> +
> +static void xsc_cmd_comp_handler(struct xsc_core_device *xdev, u8 idx,
> + struct xsc_rsp_layout *rsp)
> +{
> + struct xsc_cmd *cmd = xdev->cmd;
> + struct xsc_cmd_work_ent *ent;
> +
> + if (idx >= cmd->max_reg_cmds || (cmd->cmd_entry_mask & BIT(idx))) {
> + pci_err(xdev->pdev, "idx[%d] exceeds max cmds or has no request\n",
> + idx);
> + return;
> + }
> + ent = cmd->ent_arr[idx];
> +
> + memcpy(ent->out->first, rsp->out, sizeof(rsp->out));
[Severity: Medium]
Can a stale response be given to the wrong command here?
The only matching is on the device-supplied rsp->idx, plus a lockless read
of cmd_entry_mask. ent_arr[idx] is not checked for NULL.
rsp->token is never compared with ent->token, even though xsc_cmd_invoke()
writes the token into the request:
lay->token = ent->token;
The token is idx + 1, so even if it were checked it could not tell two uses
of the same slot apart.
A late response can come from a command that timed out in this instance,
or from a request the firmware still holds from a previous driver instance.
Either one would complete whichever command now owns that idx.
xsc_cmd_handle_rsp_before_reload() only skips responses that were already
posted at load time. Allocation starts at bit 0, so the version check is the
likely victim.
There is also a duplicate-response case. Suppose the response arrives after
the idx has been re-allocated, but while ent_arr[idx] still points at the
previous entry that its waiter is freeing. Could the memcpy() into
ent->out->first and complete(&ent->done) then write into freed memory?
> + ent->ret = 0;
> + ent->status = ((struct xsc_outbox_hdr *)ent->out->first)->status;
> + cmd->retry_cnt = 0;
> +
> + xsc_free_ent(cmd, ent->idx);
> + complete(&ent->done);
> + up(&cmd->sem);
> +}
[Severity: Medium]
Does this leak the timed-out command's xsc_cmd_work_ent and its inb/outb
mailboxes?
After a timeout, entA stays in cmd->ent_arr[X] and owns both mailboxes
("the command owns both mailboxes now" in _xsc_cmd_exec()). When the late
response arrives, this path frees bit X and returns a semaphore permit. It
does not clear ent_arr[X] or free entA.
The timeout check at the top of _xsc_cmd_exec() is a plain lockless read. A
caller that already passed it can still get idx X from xsc_alloc_ent(), for
example one sleeping in xsc_alloc_msg() or blocked in down().
xsc_cmd_invoke() then overwrites the slot unconditionally:
cmd->ent_arr[ent->idx] = ent;
That command NULLs the slot when it finishes. From then on, entA, its two
xsc_cmd_msg chains and their dma_pool blocks are unreachable.
xsc_cmd_cleanup() cannot free them, and dma_pool_destroy() would report a
busy pool.
[ ... ]
> +static int xsc_cmd_cq_polling(void *data)
> +{
[ ... ]
> + cq_pid = xsc_get_cmdq_rsp_pid(xdev) & XSC_CMDQ_INDEX_MASK;
> + if (cmd->cq_cid == cq_pid) {
> + usleep_range(1000, 2000);
> + continue;
> + }
[Severity: Low]
This isn't a bug, but this loop keeps polling when no command is
outstanding. Each idle iteration does two non-posted MMIO reads (the error
status and RSP_PID), then usleep_range(1000, 2000).
That is roughly 500-1000 wakeups and 1000-2000 PCIe reads per second for
each bound device, for as long as it stays bound. In this patch, commands
are only issued at probe.
The commit message mentions "a single polling thread" but not this idle
cost. Could the thread poll only while commands are outstanding, or back
off when idle?
> +
> + rsp = xsc_get_cq_inst(cmd, cmd->cq_cid);
> + if (!cmd->ownerbit_learned) {
> + cmd->ownerbit_learned = 1;
> + cmd->owner_bit = xsc_rsp_owner_bit(rsp);
> + }
> + if (cmd->owner_bit != xsc_rsp_owner_bit(rsp)) {
[Severity: Medium]
The expected owner bit is learned from the same slot being validated, so the
readiness check always passes for the first response. Is that safe?
The "rsp doorbell advanced but buffer not ready" branch suggests RSP_PID can
become visible before the slot contents. Suppose that happens on the first
response after init:
- The zeroed cq_buf slot is consumed with rsp->idx == 0 and status 0.
- The version check completes with a zeroed payload (cmdq_ver == 0), so
probe fails.
- owner_bit is learned as 0.
The queue is never reset, so the hardware phase carries over from earlier
driver instances. If that phase is 1, every later slot fails the owner
check. The poller then loops on msleep(20) forever and every command times
out.
[ ... ]
> +static int _xsc_cmd_exec(struct xsc_core_device *xdev,
> + void *in, unsigned int in_size,
> + void *out, unsigned int out_size)
> +{
[ ... ]
> + if (cmd->cmd_status == XSC_CMD_STATUS_TIMEDOUT)
> + return -ETIMEDOUT;
[Severity: Medium]
Is it intended that one timeout disables the command interface for good?
xsc_cmd_invoke() sets cmd->cmd_status = XSC_CMD_STATUS_TIMEDOUT on timeout.
Nothing outside xsc_cmd_init() ever sets it back to XSC_CMD_STATUS_NORMAL.
That holds even when the late response arrives and xsc_cmd_comp_handler()
frees the slot and calls up(&cmd->sem).
After one firmware stall longer than XSC_CMD_TIMEOUT_MSEC, every later
xsc_cmd_exec() returns -ETIMEDOUT until the device is unbound and rebound.
Later in the series this covers CQ/QP/EQ create and destroy, MTU, port admin
status, vport state and event query.
The commit message only says "Event-driven mode is deferred". It does not
mention that timeouts are terminal.
[ ... ]
> +int xsc_cmd_init(struct xsc_core_device *xdev)
> +{
[ ... ]
> + cmd->log_sz = XSC_CMDQ_Q_DEPTH_LOG;
> + cmd->log_stride = XSC_CMDQ_ELEMENT_SIZE_LOG;
> + cmd->max_reg_cmds = 1 << cmd->log_sz;
> + bitmap_fill(&cmd->cmd_entry_mask, cmd->max_reg_cmds);
> +
> + spin_lock_init(&cmd->alloc_lock);
> + spin_lock_init(&cmd->doorbell_lock);
> + sema_init(&cmd->sem, cmd->max_reg_cmds);
[Severity: Medium]
With this sizing, can a full response ring be told apart from an empty one?
max_reg_cmds and the semaphore both allow 32 outstanding commands. The
request and response rings have exactly 32 slots, and indices are compared
modulo 32 (XSC_CMDQ_INDEX_MASK). A permit is only returned after the poller
consumes the response.
xsc_cmd_cq_polling() treats cq_cid == cq_pid as empty:
if (cmd->cq_cid == cq_pid) {
usleep_range(1000, 2000);
continue;
}
Suppose the device posts 32 responses while the poller is in
usleep_range(). The masked RSP_PID wraps back to cq_cid, so all 32 responses
are skipped. Every waiter then times out, the XSC_CMD_STATUS_TIMEDOUT latch
disables the interface, and the owner phase no longer matches the hardware.
The request side has the same issue. xsc_cmd_invoke() never compares cmd_pid
with the hardware consumer index, so a 32nd unconsumed request also makes
REQ_PID equal to the consumer index.
The code does not show whether the hardware posts into all 32 slots. Would
limiting outstanding commands to (1 << log_sz) - 1 avoid this?
[ ... ]
> +void xsc_cmd_cleanup(struct xsc_core_device *xdev)
> +{
[ ... ]
> + /* a timed-out command stays registered for a response that may never
> + * come; with the polling thread stopped nothing can touch it anymore
> + */
> + for (i = 0; i < cmd->max_reg_cmds; i++) {
> + if (!cmd->ent_arr[i])
> + continue;
> + xsc_free_msg(xdev, cmd->ent_arr[i]->in);
> + xsc_free_msg(xdev, cmd->ent_arr[i]->out);
> + kfree(cmd->ent_arr[i]);
> + cmd->ent_arr[i] = NULL;
> + }
> +
> + dma_free_coherent(&xdev->pdev->dev, PAGE_SIZE, cmd->cq_buf, cmd->cq_dma);
> + dma_free_coherent(&xdev->pdev->dev, PAGE_SIZE, cmd->cmd_buf, cmd->dma);
> + dma_pool_destroy(cmd->pool);
[Severity: High]
Can the device still DMA into these buffers after they are freed?
With the polling thread stopped, the host will not touch a timed-out entry.
The device, though, still holds the ring and mailbox addresses. Nothing here
does any of the following:
- reset the queue
- clear the CMDQM_HOST_REQ/RSP_BUF_BASE registers
- drain outstanding commands
Bus mastering is also still enabled, because xsc_pci_fini() only clears it
afterwards:
xsc_cmd_cleanup(xdev);
pci_iounmap(pdev, xdev->bar);
pci_clear_master(pdev);
The xsc_pci_init() unwind uses the same order. The probe path reaches this
directly when xsc_cmd_version_check() times out and jumps to
err_cmd_cleanup.
On rebind, pci_set_master() runs before xsc_cmd_init() reprograms the base
addresses. xsc_cmd_handle_rsp_before_reload() also writes RSP_CID before
that. A response the firmware still holds could then be written into the
previous instance's freed, and possibly reused, pages.
The kthread_create() failure path in xsc_cmd_init() has the same problem,
because the rings were already programmed into the device:
err_free_cq:
dma_free_coherent(&xdev->pdev->dev, PAGE_SIZE, cmd->cq_buf, cmd->cq_dma);
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126145711.70868-1-tianx%40yunsilicon.com
Re: [PATCH v2 03/11] net/xsc: add the command queue
Author: Jason Xing
Date: Sun, 29 Nov 2026 07:30
Date: Sun, 29 Nov 2026 07:30
1133 lines
50487 bytes
50487 bytes
On Thu, Nov 26, 2026 at 10:57 PM Xin Tian <tianx@yunsilicon.com> wrote:
>
> Provide the mailbox-based firmware command interface: a single polling
> thread, the command exec path with status-to-errno mapping, and the
> firmware version check. Event-driven mode is deferred.
Out of curiosity, what is the reason why the event-driven is deferred?
Polling mode is a bit less efficient.
>
> Signed-off-by: Xin Tian <tianx@yunsilicon.com>
> ---
> .../ethernet/yunsilicon/xsc/common/xsc_cmd.h | 560 ++++++++++++++
> .../ethernet/yunsilicon/xsc/common/xsc_core.h | 4 +
> .../net/ethernet/yunsilicon/xsc/pci/Makefile | 2 +-
> drivers/net/ethernet/yunsilicon/xsc/pci/cmd.c | 708 ++++++++++++++++++
> drivers/net/ethernet/yunsilicon/xsc/pci/cmd.h | 87 +++
> .../net/ethernet/yunsilicon/xsc/pci/main.c | 18 +
> 6 files changed, 1378 insertions(+), 1 deletion(-)
> create mode 100644 drivers/net/ethernet/yunsilicon/xsc/common/xsc_cmd.h
> create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/cmd.c
> create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/cmd.h
>
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/common/xsc_cmd.h b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_cmd.h
> new file mode 100644
> index 000000000..4dc650f83
> --- /dev/null
> +++ b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_cmd.h
> @@ -0,0 +1,560 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +/* Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
> + * All rights reserved.
> + */
> +
> +#ifndef __XSC_CMD_H
> +#define __XSC_CMD_H
> +
> +#include <linux/if_ether.h>
> +#include <linux/types.h>
> +
> +struct xsc_core_device;
> +
> +#define XSC_CMDQ_VERSION 0x32
> +
> +#define XSC_BOARD_SN_LEN 32
> +#define XSC_MAX_NUM_PCIE_INTF 2
> +#define XSC_MAX_PF_NUM_PER_PCIE 8
> +
> +/* hca_cap mbox version */
> +#define XSC_CMD_QUERY_HCA_CAP_V1 1
> +
> +enum {
> + XSC_CMD_STAT_OK = 0x0,
> + XSC_CMD_STAT_BAD_OP_ERR = 0x2,
> + XSC_CMD_STAT_BAD_PARAM_ERR = 0x3,
> + XSC_CMD_STAT_RES_BUSY = 0x6,
> + XSC_CMD_STAT_IX_ERR = 0xa,
> + XSC_CMD_STAT_QP_FLUSH_BUSY = 0x4b,
> + XSC_CMD_STAT_QP_FLUSH_PENDING = 0x4c,
> + XSC_CMD_STAT_INVAL_FUNC = 0x41,
> +};
> +
> +enum {
> + XSC_CMD_OP_QUERY_HCA_CAP = 0x100,
> + XSC_CMD_OP_QUERY_CMDQ_VERSION = 0x10a,
> + XSC_CMD_OP_FUNCTION_RESET = 0x10c,
> +
> + XSC_CMD_OP_CREATE_EQ = 0x301,
> + XSC_CMD_OP_DESTROY_EQ = 0x302,
> +
> + XSC_CMD_OP_CREATE_CQ = 0x400,
> + XSC_CMD_OP_DESTROY_CQ = 0x401,
> +
> + XSC_CMD_OP_CREATE_QP = 0x500,
> + XSC_CMD_OP_DESTROY_QP = 0x501,
> + XSC_CMD_OP_2RST_QP = 0x50a,
> + XSC_CMD_OP_RTR2RTS_QP = 0x504,
> +
> + XSC_CMD_OP_ENABLE_NIC_HCA = 0x810,
> + XSC_CMD_OP_DISABLE_NIC_HCA = 0x811,
> + XSC_CMD_OP_MODIFY_RAW_QP = 0x81f,
> +
> + XSC_CMD_OP_QUERY_VPORT_STATE = 0x822,
> + XSC_CMD_OP_QUERY_EVENT_TYPE = 0x831,
> +
> + XSC_CMD_OP_ENABLE_MSIX = 0x850,
> +
> + XSC_CMD_OP_SET_MTU = 0x1100,
> + XSC_CMD_OP_QUERY_ETH_MAC = 0x1101,
> +
> + XSC_CMD_OP_SET_PORT_ADMIN_STATUS = 0x1801,
> +};
> +
> +enum xsc_dma_direct {
> + XSC_DMA_DIR_TO_MAC,
> +};
> +
> +enum {
> + XSC_TBM_CAP_HASH_PPH = 0,
> + XSC_TBM_CAP_RSS,
> + XSC_TBM_CAP_PCT_DROP_CONFIG,
> +};
> +
> +struct xsc_inbox_hdr {
> + __be16 opcode;
> + u8 rsvd[4];
> + __be16 ver;
> +};
> +
> +struct xsc_outbox_hdr {
> + u8 status;
> + u8 rsvd[5];
> + __be16 ver;
> +};
> +
> +/* CQ mbox */
> +struct xsc_cq_context {
> + __be16 eqn;
> + __be16 pa_num;
> + __be16 glb_func_id;
> + u8 log_cq_sz;
> + u8 cq_type;
> +};
> +
> +struct xsc_create_cq_mbox_in {
> + struct xsc_inbox_hdr hdr;
> + struct xsc_cq_context ctx;
> + __be64 pas[];
> +};
> +
> +struct xsc_create_cq_mbox_out {
> + struct xsc_outbox_hdr hdr;
> + __be32 cqn;
> + u8 rsvd[4];
> +};
> +
> +struct xsc_destroy_cq_mbox_in {
> + struct xsc_inbox_hdr hdr;
> + __be32 cqn;
> + u8 rsvd[4];
> +};
> +
> +struct xsc_destroy_cq_mbox_out {
> + struct xsc_outbox_hdr hdr;
> + u8 rsvd[8];
> +};
> +
> +/* QP mbox */
> +struct __packed xsc_create_qp_request {
> + __be16 input_qpn;
> + __be16 pa_num;
> + u8 qp_type;
> + u8 log_sq_sz;
> + u8 log_rq_sz;
> + u8 dma_direct;
> + __be32 pdn;
> + __be16 cqn_send;
> + __be16 cqn_recv;
> + __be16 glb_funcid;
> + u8 page_shift;
> + u8 rsvd[5];
> + __be64 pas[];
> +};
> +
> +struct xsc_create_qp_mbox_in {
> + struct xsc_inbox_hdr hdr;
> + struct xsc_create_qp_request req;
> +};
> +
> +struct xsc_create_qp_mbox_out {
> + struct xsc_outbox_hdr hdr;
> + __be32 qpn;
> + u8 rsvd[4];
> +};
> +
> +struct xsc_destroy_qp_mbox_in {
> + struct xsc_inbox_hdr hdr;
> + __be32 qpn;
> + u8 rsvd[4];
> +};
> +
> +struct xsc_destroy_qp_mbox_out {
> + struct xsc_outbox_hdr hdr;
> + u8 rsvd[8];
> +};
> +
> +struct xsc_qp_context {
> + __be32 remote_qpn;
> + __be32 cqn_send;
> + __be32 cqn_recv;
> + __be32 next_send_psn;
> + __be32 next_recv_psn;
> + __be32 pdn;
> + __be16 src_udp_port;
> + __be16 path_id;
> + u8 mtu_mode;
> + u8 lag_sel;
> + u8 lag_sel_en;
> + u8 retry_cnt;
> + u8 rnr_retry;
> + u8 dscp;
> + u8 state;
> + u8 hop_limit;
> + u8 dmac[6];
> + u8 smac[6];
> + __be32 dip[4];
> + __be32 sip[4];
> + __be16 ip_type;
> + __be16 grp_id;
> + u8 vlan_valid;
> + u8 dci_cfi_prio_sl;
> + __be16 vlan_id;
> + u8 qp_out_port;
> + u8 pcie_no;
> + __be16 lag_id;
> + __be16 func_id;
> + u8 max_sra;
> + u8 max_rra;
> + u8 no_need_wait;
> + u8 swith_qp_type;
> + u8 rsvd0[2];
> + __be32 qp_access_flags;
> +};
> +
> +struct xsc_modify_qp_mbox_in {
> + struct xsc_inbox_hdr hdr;
> + __be32 qpn;
> + struct xsc_qp_context ctx;
> + u8 no_need_wait;
> +};
> +
> +struct xsc_modify_qp_mbox_out {
> + struct xsc_outbox_hdr hdr;
> + u8 rsvd[8];
> +};
> +
> +struct xsc_modify_raw_qp_request {
> + __be16 qpn;
> + __be16 lag_id;
> + __be16 func_id;
> + u8 dma_direct;
> + u8 prio;
> + u8 qp_out_port;
> + u8 rsvd[7];
> +};
> +
> +struct xsc_modify_raw_qp_mbox_in {
> + struct xsc_inbox_hdr hdr;
> + u8 pcie_no;
> + u8 rsvd[7];
> + struct xsc_modify_raw_qp_request req;
> +};
> +
> +struct xsc_modify_raw_qp_mbox_out {
> + struct xsc_outbox_hdr hdr;
> + u8 rsvd[8];
> +};
> +
> +/* MSIX TABLE mbox */
> +struct xsc_msix_table_info_mbox_in {
> + struct xsc_inbox_hdr hdr;
> + __be16 index;
> + u8 rsvd[6];
> +};
> +
> +struct xsc_msix_table_info_mbox_out {
> + struct xsc_outbox_hdr hdr;
> + __be32 addr_lo;
> + __be32 addr_hi;
> + __be32 data;
> +};
> +
> +/* EQ mbox */
> +struct xsc_eq_context {
> + __be16 vecidx;
> + __be16 pa_num;
> + u8 log_eq_sz;
> + u8 rsvd1;
> + __be16 glb_func_id;
> + u8 is_async_eq;
> + u8 rsvd;
> +};
> +
> +struct xsc_create_eq_mbox_in {
> + struct xsc_inbox_hdr hdr;
> + struct xsc_eq_context ctx;
> + u8 rsvd[6];
> + __be64 pas[];
> +};
> +
> +struct xsc_create_eq_mbox_out {
> + struct xsc_outbox_hdr hdr;
> + __be32 eqn;
> + u8 rsvd[4];
> +};
> +
> +struct xsc_destroy_eq_mbox_in {
> + struct xsc_inbox_hdr hdr;
> + __be32 eqn;
> + u8 rsvd[4];
> +};
> +
> +struct xsc_destroy_eq_mbox_out {
> + struct xsc_outbox_hdr hdr;
> + u8 rsvd[8];
> +};
> +
> +struct xsc_cmd_query_cmdq_ver_mbox_in {
> + struct xsc_inbox_hdr hdr;
> + u8 rsvd[8];
> +};
> +
> +struct xsc_cmd_query_cmdq_ver_mbox_out {
> + struct xsc_outbox_hdr hdr;
> + __be16 cmdq_ver;
> + u8 rsvd[6];
> +};
> +
> +/* fw mailbox layout, do not change */
> +struct xsc_fw_version {
> + u8 rsvd[16];
> +};
> +
> +struct __packed xsc_hca_cap {
> + u8 rsvd1[12];
> + u8 send_seg_num;
> + u8 send_wqe_shift;
> + u8 recv_seg_num;
> + u8 recv_wqe_shift;
> + u8 log_max_srq_sz;
> + u8 log_max_qp_sz;
> + u8 log_max_mtt;
> + u8 log_max_qp;
> + u8 log_max_strq_sz;
> + u8 log_max_srqs;
> + u8 rsvd4[2];
> + u8 log_max_tso;
> + u8 log_max_cq_sz;
> + u8 rsvd6;
> + u8 log_max_cq;
> + u8 log_max_eq_sz;
> + u8 log_max_mkey;
> + u8 log_max_msix;
> + u8 log_max_eq;
> + u8 max_indirection;
> + u8 log_max_mrw_sz;
> + u8 log_max_bsf_list_sz;
> + u8 log_max_klm_list_sz;
> + u8 rsvd_8_0;
> + u8 log_max_ra_req_dc;
> + u8 rsvd_8_1;
> + u8 log_max_ra_res_dc;
> + u8 rsvd9;
> + u8 log_max_ra_req_qp;
> + u8 log_max_qp_depth;
> + u8 log_max_ra_res_qp;
> + __be16 max_vfs;
> + __be16 raweth_qp_id_end;
> + __be16 raw_tpe_qp_num;
> + __be16 max_qp_count;
> + __be16 raweth_qp_id_base;
> + u8 rsvd13;
> + u8 local_ca_ack_delay;
> + u8 max_num_eqs;
> + u8 num_ports;
> + u8 log_max_msg;
> + u8 mac_port;
> + __be16 raweth_rss_qp_id_base;
> + __be16 stat_rate_support;
> + u8 rsvd16[8];
> + __be64 flags;
> + u8 rsvd17;
> + u8 uar_sz;
> + u8 rsvd18;
> + u8 log_pg_sz;
> + __be16 bf_log_bf_reg_size;
> + __be16 msix_base;
> + __be16 msix_num;
> + __be16 max_desc_sz_sq;
> + u8 rsvd20[2];
> + __be16 max_desc_sz_rq;
> + u8 rsvd21[2];
> + __be16 max_desc_sz_sq_dc;
> + u8 rsvd22[4];
> + __be16 max_qp_mcg;
> + u8 rsvd23;
> + u8 log_max_mcg;
> + u8 rsvd24;
> + u8 log_max_pd;
> + u8 rsvd25;
> + u8 log_max_xrcd;
> + u8 rsvd26[40];
> + __be32 uar_page_sz;
> + u8 rsvd27[8];
> + __be32 hw_feature_flag;
> + __be16 pf0_vf_funcid_base;
> + __be16 pf0_vf_funcid_top;
> + __be16 pf1_vf_funcid_base;
> + __be16 pf1_vf_funcid_top;
> + __be16 pcie0_pf_funcid_base;
> + __be16 pcie0_pf_funcid_top;
> + __be16 pcie1_pf_funcid_base;
> + __be16 pcie1_pf_funcid_top;
> + u8 log_msx_atomic_size_qp;
> + u8 pcie_host;
> + u8 rsvd28;
> + u8 log_msx_atomic_size_dc;
> + u8 board_sn[XSC_BOARD_SN_LEN];
> + u8 max_tc;
> + u8 mac_bit;
> + __be16 funcid_to_logic_port;
> + u8 rsvd29[6];
> + u8 nif_port_num;
> + u8 reg_mr_via_cmdq;
> + __be32 hca_core_clock;
> + __be32 max_rwq_indirection_tables;
> + __be32 max_rwq_indirection_table_size;
> + __be32 chip_ver_h;
> + __be32 chip_ver_m;
> + __be32 chip_ver_l;
> + __be32 hotfix_num;
> + __be32 feature_flag;
> + __be32 rx_pkt_len_max;
> + __be32 glb_func_id;
> + __be64 tx_db;
> + __be64 rx_db;
> + __be64 complete_db;
> + __be64 complete_reg;
> + __be64 event_db;
> + __be32 qp_rate_limit_min;
> + __be32 qp_rate_limit_max;
> + struct xsc_fw_version fw_ver;
> + u8 lag_logic_port_ofst;
> + u8 rsvd32[7];
> + /* V1 */
> + __be64 max_mr_size;
> + __be16 max_cmd_in_len;
> + __be16 max_cmd_out_len;
> + u8 rsvd_v2_v3[36];
> + /* V4 */
> + __be16 vf_funcid_base[XSC_MAX_NUM_PCIE_INTF][XSC_MAX_PF_NUM_PER_PCIE];
> + __be16 vf_funcid_top[XSC_MAX_NUM_PCIE_INTF][XSC_MAX_PF_NUM_PER_PCIE];
> + __be16 pf_funcid_base[XSC_MAX_NUM_PCIE_INTF];
> + __be16 pf_funcid_top[XSC_MAX_NUM_PCIE_INTF];
> + u8 pcie_no;
> + u8 pf_id;
> + __be16 vf_id;
> + u8 pcie_host_num;
> + u8 pf_num_per_pcie;
> + u8 rsvd_v6[46];
> +};
> +
> +struct xsc_cmd_query_hca_cap_mbox_in {
> + struct xsc_inbox_hdr hdr;
> + __be16 cpu_num;
> + u8 rsvd[6];
> +};
> +
> +struct xsc_cmd_query_hca_cap_mbox_out {
> + struct xsc_outbox_hdr hdr;
> + u8 rsvd0[8];
> + struct xsc_hca_cap hca_cap;
> +};
> +
> +struct xsc_query_vport_state_out {
> + struct xsc_outbox_hdr hdr;
> + u8 state;
> +#define XSC_QUERY_VPORT_ADMIN_STATE_MASK GENMASK(3, 0)
> +#define XSC_QUERY_VPORT_STATE_MASK GENMASK(7, 4)
> +};
> +
> +struct xsc_query_vport_state_in {
> + struct xsc_inbox_hdr hdr;
> + __be32 data0;
> +#define XSC_QUERY_VPORT_OTHER_VPORT BIT(0)
> +#define XSC_QUERY_VPORT_VPORT_NUM_MASK GENMASK(16, 1)
> +};
> +
> +enum {
> + XSC_CMD_EVENT_RESP_CHANGE_LINK = BIT(0),
> +};
> +
> +struct xsc_event_resp {
> + u8 resp_cmd_type;
> +};
> +
> +struct xsc_event_query_type_mbox_in {
> + struct xsc_inbox_hdr hdr;
> + u8 rsvd[2];
> +};
> +
> +struct xsc_event_query_type_mbox_out {
> + struct xsc_outbox_hdr hdr;
> + struct xsc_event_resp ctx;
> +};
> +
> +struct xsc_set_mtu_mbox_in {
> + struct xsc_inbox_hdr hdr;
> + __be16 mtu;
> + __be16 rx_buf_sz_min;
> + u8 mac_port;
> + u8 rsvd;
> +};
> +
> +struct xsc_set_mtu_mbox_out {
> + struct xsc_outbox_hdr hdr;
> +};
> +
> +struct xsc_query_eth_mac_mbox_in {
> + struct xsc_inbox_hdr hdr;
> + u8 index;
> +};
> +
> +struct xsc_query_eth_mac_mbox_out {
> + struct xsc_outbox_hdr hdr;
> + u8 mac[ETH_ALEN];
> +};
> +
> +struct xsc_nic_attr {
> + __be16 caps;
> + __be16 caps_mask;
> + u8 mac_addr[ETH_ALEN];
> +};
> +
> +struct xsc_rss_attr {
> + u8 rss_en;
> + u8 hfunc;
> + __be16 rqn_base;
> + __be16 rqn_num;
> + u8 rsvd[2];
> + __be32 hash_tmpl;
> +};
> +
> +struct xsc_cmd_enable_nic_hca_mbox_in {
> + struct xsc_inbox_hdr hdr;
> + struct xsc_nic_attr nic;
> + struct xsc_rss_attr rss;
> +};
> +
> +struct xsc_cmd_enable_nic_hca_mbox_out {
> + struct xsc_outbox_hdr hdr;
> + u8 rsvd0[2];
> +};
> +
> +struct xsc_nic_dis_attr {
> + __be16 caps;
> +};
> +
> +struct xsc_cmd_disable_nic_hca_mbox_in {
> + struct xsc_inbox_hdr hdr;
> + struct xsc_nic_dis_attr nic;
> +};
> +
> +struct xsc_cmd_disable_nic_hca_mbox_out {
> + struct xsc_outbox_hdr hdr;
> + u8 rsvd0[4];
> +};
> +
> +struct xsc_function_reset_mbox_in {
> + struct xsc_inbox_hdr hdr;
> + __be16 glb_func_id;
> + u8 rsvd[6];
> +};
> +
> +struct xsc_function_reset_mbox_out {
> + struct xsc_outbox_hdr hdr;
> + u8 rsvd[8];
> +};
> +
> +struct xsc_event_set_port_admin_status_mbox_in {
> + struct xsc_inbox_hdr hdr;
> + __be16 status;
> +};
> +
> +struct xsc_event_set_port_admin_status_mbox_out {
> + struct xsc_outbox_hdr hdr;
> + __be32 status;
> +};
> +
> +int xsc_cmd_init(struct xsc_core_device *xdev);
> +void xsc_cmd_cleanup(struct xsc_core_device *xdev);
> +int xsc_cmd_exec(struct xsc_core_device *xdev,
> + void *in, unsigned int in_size,
> + void *out, unsigned int out_size);
> +int xsc_cmd_version_check(struct xsc_core_device *xdev);
> +
> +int xsc_cmd_status_to_err(struct xsc_outbox_hdr *hdr);
> +
> +#endif
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
> index 9580d89a1..a6f0149ed 100644
> --- a/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
> +++ b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
> @@ -24,12 +24,16 @@
> #define XSC_PAGE_SHIFT_4K 12
> #define XSC_PAGE_SIZE_4K (_AC(1, UL) << XSC_PAGE_SHIFT_4K)
>
> +struct xsc_cmd;
> +
> struct xsc_core_device {
> struct pci_dev *pdev;
> struct device *device;
>
> void __iomem *bar;
> int bar_num;
> +
> + struct xsc_cmd *cmd;
> };
>
> void xsc_update_tx_db(struct xsc_core_device *xdev, u32 sqn, u32 next_pid);
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/Makefile b/drivers/net/ethernet/yunsilicon/xsc/pci/Makefile
> index 4a08b92cd..e17a10cc1 100644
> --- a/drivers/net/ethernet/yunsilicon/xsc/pci/Makefile
> +++ b/drivers/net/ethernet/yunsilicon/xsc/pci/Makefile
> @@ -6,4 +6,4 @@ ccflags-y += -I$(srctree)/drivers/net/ethernet/yunsilicon/xsc
>
> obj-$(CONFIG_YUNSILICON_XSC_PCI) += xsc_pci.o
>
> -xsc_pci-y := main.o doorbell.o
> +xsc_pci-y := main.o cmd.o doorbell.o
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/cmd.c b/drivers/net/ethernet/yunsilicon/xsc/pci/cmd.c
> new file mode 100644
> index 000000000..45dcdb2b4
> --- /dev/null
> +++ b/drivers/net/ethernet/yunsilicon/xsc/pci/cmd.c
> @@ -0,0 +1,708 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Copyright (c) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
> + * All rights reserved.
> + * Copyright (c) 2013-2016, Mellanox Technologies. All rights reserved.
> + */
> +
> +#include <linux/bitmap.h>
> +#include <linux/delay.h>
> +#include <linux/dma-mapping.h>
> +#include <linux/kthread.h>
> +#include <linux/pci.h>
> +#include <linux/slab.h>
> +
> +#include "common/xsc_cmd.h"
> +#include "cmd.h"
> +#include "common/xsc_core.h"
> +
> +#define XSC_CMD_DATA_BLOCK_SIZE 512
> +#define XSC_CMD_FIRST_SIZE 8
> +
> +struct xsc_cmd_prot_block {
> + u8 data[XSC_CMD_DATA_BLOCK_SIZE];
> + u8 rsvd0[48];
> + __be64 next;
> + __be32 block_num;
> + u8 owner_status;
> + u8 token;
> + u8 ctrl_sig;
> + u8 sig;
> +};
> +
> +/* hw layout, do not change */
> +struct xsc_cmd_layout {
> + u16 rsv0;
> + u8 rsv[2];
> + __be32 inlen;
> + __be64 in_ptr;
> + __be32 in[XSC_CMD_FIRST_SIZE];
> + __be64 out_ptr;
> + __be32 outlen;
> + u8 token;
> + u8 sig;
> + u8 idx;
> + u8 type_owner;
> +};
> +
> +struct xsc_rsp_layout {
> + u16 rsv0;
> + u8 rsv[2];
> + __be32 out[XSC_RSP_FIRST_SIZE];
> + u8 token;
> + u8 sig;
> + u8 idx;
> + u8 type_owner;
> +};
> +
> +#define XSC_CMD_TYPE_MASK GENMASK(6, 0)
> +#define XSC_CMD_OWNER_BIT BIT(7)
> +
> +enum {
> + XSC_CMDQ_ERR_HW_READ_REQ_ERR = BIT(0),
> + XSC_CMDQ_ERR_HW_WRITE_REQ_ERR = BIT(1),
> + XSC_CMDQ_ERR_REQ_PID_ERR = BIT(2),
> + XSC_CMDQ_ERR_RSP_CID_ERR = BIT(3),
> +};
> +
> +enum {
> + XSC_CMDQ_ELEMENT_SIZE_LOG = 6,
> + XSC_CMDQ_Q_DEPTH_LOG = 5,
> +};
> +
> +static int xsc_alloc_ent(struct xsc_cmd *cmd)
> +{
> + unsigned long flags;
> + int ret;
> +
> + spin_lock_irqsave(&cmd->alloc_lock, flags);
> + ret = find_first_bit(&cmd->cmd_entry_mask, cmd->max_reg_cmds);
> + if (ret < cmd->max_reg_cmds)
> + clear_bit(ret, &cmd->cmd_entry_mask);
> + spin_unlock_irqrestore(&cmd->alloc_lock, flags);
> +
> + return ret < cmd->max_reg_cmds ? ret : -ENOSPC;
> +}
> +
> +static void xsc_free_ent(struct xsc_cmd *cmd, int idx)
> +{
> + unsigned long flags;
> +
> + spin_lock_irqsave(&cmd->alloc_lock, flags);
> + set_bit(idx, &cmd->cmd_entry_mask);
> + spin_unlock_irqrestore(&cmd->alloc_lock, flags);
> +}
> +
> +static struct xsc_cmd_layout *xsc_get_inst(struct xsc_cmd *cmd, int idx)
> +{
> + return cmd->cmd_buf + (idx << cmd->log_stride);
> +}
> +
> +static struct xsc_rsp_layout *xsc_get_cq_inst(struct xsc_cmd *cmd, int idx)
> +{
> + return cmd->cq_buf + (idx << cmd->log_stride);
> +}
> +
> +static u8 xsc_rsp_owner_bit(struct xsc_rsp_layout *rsp)
> +{
> + return !!(rsp->type_owner & XSC_CMD_OWNER_BIT);
> +}
> +
> +static u16 xsc_msg_to_opcode(const struct xsc_cmd_msg *in)
> +{
> + const struct xsc_inbox_hdr *hdr = (const void *)in->first;
> +
> + return be16_to_cpu(hdr->opcode);
> +}
> +
> +static struct xsc_cmd_mailbox *xsc_alloc_cmd_box(struct xsc_core_device *xdev,
> + gfp_t flags)
> +{
> + struct xsc_cmd_mailbox *mailbox;
> +
> + mailbox = kmalloc_obj(*mailbox, flags);
> + if (!mailbox)
> + return ERR_PTR(-ENOMEM);
> +
> + mailbox->buf = dma_pool_zalloc(xdev->cmd->pool, flags, &mailbox->dma);
> + if (!mailbox->buf) {
> + kfree(mailbox);
> + return ERR_PTR(-ENOMEM);
> + }
> + mailbox->next = NULL;
> +
> + return mailbox;
> +}
> +
> +static void xsc_free_cmd_box(struct xsc_core_device *xdev,
> + struct xsc_cmd_mailbox *mailbox)
> +{
> + dma_pool_free(xdev->cmd->pool, mailbox->buf, mailbox->dma);
> + kfree(mailbox);
> +}
> +
> +static struct xsc_cmd_msg *xsc_alloc_msg(struct xsc_core_device *xdev,
> + unsigned int size,
> + unsigned int first_size)
> +{
> + struct xsc_cmd_mailbox *tmp, *head = NULL;
> + struct xsc_cmd_prot_block *block;
> + struct xsc_cmd_msg *msg;
> + unsigned int blen;
> + int err;
> + int n;
> + int i;
> +
> + msg = kzalloc_obj(*msg, GFP_KERNEL);
> + if (!msg)
> + return ERR_PTR(-ENOMEM);
> +
> + blen = size - min(size, first_size);
> + n = DIV_ROUND_UP(blen, XSC_CMD_DATA_BLOCK_SIZE);
> +
> + for (i = 0; i < n; i++) {
> + tmp = xsc_alloc_cmd_box(xdev, GFP_KERNEL);
> + if (IS_ERR(tmp)) {
> + pci_err(xdev->pdev, "failed allocating block\n");
> + err = PTR_ERR(tmp);
> + goto err_free;
> + }
> +
> + block = tmp->buf;
> + tmp->next = head;
> + block->next = cpu_to_be64(tmp->next ? tmp->next->dma : 0);
> + block->block_num = cpu_to_be32(n - i - 1);
> + head = tmp;
> + }
> + msg->next = head;
> + msg->len = size;
> + msg->first_size = first_size;
> +
> + return msg;
> +
> +err_free:
> + while (head) {
> + tmp = head->next;
> + xsc_free_cmd_box(xdev, head);
> + head = tmp;
> + }
> + kfree(msg);
> +
> + return ERR_PTR(err);
> +}
> +
> +static void xsc_free_msg(struct xsc_core_device *xdev, struct xsc_cmd_msg *msg)
> +{
> + struct xsc_cmd_mailbox *head = msg->next;
> + struct xsc_cmd_mailbox *next;
> +
> + while (head) {
> + next = head->next;
> + xsc_free_cmd_box(xdev, head);
> + head = next;
> + }
> + kfree(msg);
> +}
> +
> +static int xsc_copy_to_cmd_msg(struct xsc_cmd_msg *to, const void *from,
> + unsigned int size)
> +{
> + struct xsc_cmd_prot_block *block;
> + struct xsc_cmd_mailbox *next;
> + const u8 *src = from;
> + unsigned int copy;
> +
> + copy = min(size, to->first_size);
> + memcpy(to->first, src, copy);
> + size -= copy;
> + src += copy;
> +
> + next = to->next;
> + while (size) {
> + if (!next) {
> + WARN_ONCE(1, "Mailbox too short\n");
> + return -ENOMEM;
> + }
> +
> + copy = min(size, XSC_CMD_DATA_BLOCK_SIZE);
> + block = next->buf;
> + memcpy(block->data, src, copy);
> + block->owner_status = 0;
> + src += copy;
> + size -= copy;
> + next = next->next;
> + }
> +
> + return 0;
> +}
> +
> +static int xsc_copy_from_rsp_msg(void *to, const struct xsc_cmd_msg *from,
> + unsigned int size)
> +{
> + struct xsc_cmd_prot_block *block;
> + struct xsc_cmd_mailbox *next;
> + u8 *dst = to;
> + unsigned int copy;
> +
> + copy = min(size, from->first_size);
> + memcpy(dst, from->first, copy);
> + size -= copy;
> + dst += copy;
> +
> + next = from->next;
> + while (size) {
> + if (!next) {
> + WARN_ONCE(1, "Mailbox too short\n");
> + return -ENOMEM;
> + }
> +
> + copy = min(size, XSC_CMD_DATA_BLOCK_SIZE);
> + block = next->buf;
> + if (!block->owner_status)
> + return -EIO;
> +
> + memcpy(dst, block->data, copy);
> + dst += copy;
> + size -= copy;
> + next = next->next;
> + }
> +
> + return 0;
> +}
> +
> +static int xsc_cmd_invoke(struct xsc_core_device *xdev, struct xsc_cmd_msg *in,
> + struct xsc_cmd_msg *out, u8 *status)
> +{
> + struct xsc_cmd *cmd = xdev->cmd;
> + struct xsc_cmd_layout *lay;
> + struct xsc_cmd_work_ent *ent;
> + unsigned long timeout;
> + unsigned long flags;
> + int err;
> +
> + ent = kzalloc_obj(*ent, GFP_KERNEL);
> + if (!ent)
> + return -ENOMEM;
> +
> + ent->in = in;
> + ent->out = out;
> + init_completion(&ent->done);
> + down(&cmd->sem);
> + ent->idx = xsc_alloc_ent(cmd);
> + if (ent->idx < 0) {
> + pci_err(xdev->pdev, "failed to allocate command entry\n");
> + up(&cmd->sem);
> + kfree(ent);
> + return -ENOMEM;
> + }
> + ent->token = ent->idx + 1;
> + cmd->ent_arr[ent->idx] = ent;
> +
> + spin_lock_irqsave(&cmd->doorbell_lock, flags);
> + lay = xsc_get_inst(cmd, cmd->cmd_pid);
> + memset(lay, 0, sizeof(*lay));
> + memcpy(lay->in, in->first, sizeof(lay->in));
> + if (in->next)
> + lay->in_ptr = cpu_to_be64(in->next->dma);
> + lay->inlen = cpu_to_be32(in->len);
> + if (out->next)
> + lay->out_ptr = cpu_to_be64(out->next->dma);
> + lay->outlen = cpu_to_be32(out->len);
> + lay->type_owner = FIELD_PREP(XSC_CMD_TYPE_MASK, XSC_PCI_CMD_XPORT);
> + lay->token = ent->token;
> + lay->idx = ent->idx;
> + lay->sig = 0xff;
> +
> + /* ring doorbell after the descriptor is valid */
> + wmb();
> +
> + cmd->cmd_pid = (cmd->cmd_pid + 1) % (1 << cmd->log_sz);
> + xsc_update_cmdq_req_pid(xdev, cmd->cmd_pid);
> + spin_unlock_irqrestore(&cmd->doorbell_lock, flags);
> +
> + timeout = msecs_to_jiffies(XSC_CMD_TIMEOUT_MSEC);
> + if (!wait_for_completion_timeout(&ent->done, timeout)) {
> + cmd->cmd_status = XSC_CMD_STATUS_TIMEDOUT;
> + pci_err(xdev->pdev, "wait for opcode 0x%x response timeout\n",
> + xsc_msg_to_opcode(in));
> + /* the entry stays registered for the completion handler */
> + return -ETIMEDOUT;
> + }
> +
> + err = ent->ret;
> + *status = ent->status;
> +
> + spin_lock_irqsave(&cmd->alloc_lock, flags);
> + if (cmd->ent_arr[ent->idx] == ent)
> + cmd->ent_arr[ent->idx] = NULL;
> + spin_unlock_irqrestore(&cmd->alloc_lock, flags);
> + kfree(ent);
> +
> + return err;
> +}
> +
> +static void xsc_cmd_comp_handler(struct xsc_core_device *xdev, u8 idx,
> + struct xsc_rsp_layout *rsp)
> +{
> + struct xsc_cmd *cmd = xdev->cmd;
> + struct xsc_cmd_work_ent *ent;
> +
> + if (idx >= cmd->max_reg_cmds || (cmd->cmd_entry_mask & BIT(idx))) {
> + pci_err(xdev->pdev, "idx[%d] exceeds max cmds or has no request\n",
> + idx);
> + return;
> + }
> + ent = cmd->ent_arr[idx];
> +
> + memcpy(ent->out->first, rsp->out, sizeof(rsp->out));
> + ent->ret = 0;
> + ent->status = ((struct xsc_outbox_hdr *)ent->out->first)->status;
> + cmd->retry_cnt = 0;
> +
> + xsc_free_ent(cmd, ent->idx);
> + complete(&ent->done);
> + up(&cmd->sem);
> +}
> +
> +static void xsc_cmd_handle_rsp_before_reload(struct xsc_cmd *cmd,
> + struct xsc_core_device *xdev)
> +{
> + u32 rsp_pid, rsp_cid;
> +
> + rsp_pid = xsc_get_cmdq_rsp_pid(xdev) & XSC_CMDQ_INDEX_MASK;
> + rsp_cid = xsc_get_cmdq_rsp_cid(xdev) & XSC_CMDQ_INDEX_MASK;
> + if (rsp_pid == rsp_cid)
> + return;
> +
> + cmd->cq_cid = rsp_pid;
> + xsc_update_cmdq_rsp_cid(xdev, cmd->cq_cid);
> +}
> +
> +static void xsc_cmd_err_handler(struct xsc_core_device *xdev, u32 raw)
> +{
> + struct xsc_cmd *cmd = xdev->cmd;
> + unsigned long flags;
> + int err = 0;
> + int retry = 0;
> + u32 stat = raw;
> +
> + while (stat) {
> + err++;
> + if (stat & XSC_CMDQ_ERR_HW_READ_REQ_ERR) {
> + retry = 1;
> + stat &= ~XSC_CMDQ_ERR_HW_READ_REQ_ERR;
> + pci_err(xdev->pdev, "hw read request from host failed\n");
> + } else if (stat & XSC_CMDQ_ERR_HW_WRITE_REQ_ERR) {
> + retry = 1;
> + stat &= ~XSC_CMDQ_ERR_HW_WRITE_REQ_ERR;
> + pci_err(xdev->pdev, "hw write request to fw failed\n");
> + } else if (stat & XSC_CMDQ_ERR_REQ_PID_ERR) {
> + stat &= ~XSC_CMDQ_ERR_REQ_PID_ERR;
> + pci_err(xdev->pdev, "hw reported unexpected req pid\n");
> + } else if (stat & XSC_CMDQ_ERR_RSP_CID_ERR) {
> + stat &= ~XSC_CMDQ_ERR_RSP_CID_ERR;
> + pci_err(xdev->pdev, "hw reported unexpected rsp cid\n");
> + } else {
> + stat = 0;
> + pci_err(xdev->pdev, "ignore unknown cmdq error\n");
> + }
> + }
> +
> + if (retry) {
> + if (cmd->retry_cnt >= XSC_CMD_MAX_RETRY_CNT) {
> + pci_warn(xdev->pdev, "error handler retried %d times, stop retrying\n",
> + cmd->retry_cnt);
> + } else {
> + spin_lock_irqsave(&cmd->doorbell_lock, flags);
> + xsc_update_cmdq_req_pid(xdev, cmd->cmd_pid);
> + cmd->retry_cnt++;
> + spin_unlock_irqrestore(&cmd->doorbell_lock, flags);
> + }
> + }
> +
> + if (err)
> + xsc_clear_cmdq_err_status(xdev, raw);
> +}
> +
> +static int xsc_cmd_cq_polling(void *data)
> +{
> + struct xsc_core_device *xdev = data;
> + struct xsc_cmd *cmd = xdev->cmd;
> + struct xsc_rsp_layout *rsp;
> + u32 err_status;
> + u32 cq_pid;
> +
> + while (!kthread_should_stop()) {
> + if (need_resched())
> + schedule();
cond_resched()
> +
> + err_status = xsc_read_cmdq_err_status(xdev);
> + if (err_status)
> + xsc_cmd_err_handler(xdev, err_status);
> +
> + cq_pid = xsc_get_cmdq_rsp_pid(xdev) & XSC_CMDQ_INDEX_MASK;
> + if (cmd->cq_cid == cq_pid) {
> + usleep_range(1000, 2000);
xsc_cmd_init() creates a kthread that polls the response ring every
1-2ms, even with no command in flight. 1-2ms means around 1000
periodic wakeups/context switches on one cpu.
It reminds me of future RDMA support. I noticed there are a few
production applications where frequent create/modify/destroy of pairs
are unavoidable, meaning the latency there is sensitive. It's unlike
TCP 3-way handshake. 1-2ms will possibly add huge latency
comparatively.
With that said, what I had in my mind is there are three directions:
1. event-driven as you mentioned. Patch 5 already brings up the EQ and MSI-X.
2. demand-driven (dedicated) thread. Keep the kthread, but let the
submitter wake it and have it sleep on a waitqueue once no command is
outstanding.
3. Or drop the kthread and let the submitter poll in xsc_cmd_invoke()
with read_poll_timeout(), using a staged delay (udelay(1) a few times,
then usleep_range(20, 30), then 1ms) as bnxt does in __hwrm_send().
This also removes the fixed 1ms floor per command.
Any thoughts on this?
Thanks,
Jason
Re: [PATCH v2 03/11] net/xsc: add the command queue
Author: "Xin Tian"
Date: Tue, 01 Dec 2026 09:22
Date: Tue, 01 Dec 2026 09:22
1155 lines
49051 bytes
49051 bytes
On 11/29/2026 3:30 PM, Jason Xing wrote:
> On Thu, Nov 26, 2026 at 10:57 PM Xin Tian <tianx@yunsilicon.com> wrote:
>> Provide the mailbox-based firmware command interface: a single polling
>> thread, the command exec path with status-to-errno mapping, and the
>> firmware version check. Event-driven mode is deferred.
> Out of curiosity, what is the reason why the event-driven is deferred?
> Polling mode is a bit less efficient.
>> Signed-off-by: Xin Tian <tianx@yunsilicon.com>
>> ---
>> .../ethernet/yunsilicon/xsc/common/xsc_cmd.h | 560 ++++++++++++++
>> .../ethernet/yunsilicon/xsc/common/xsc_core.h | 4 +
>> .../net/ethernet/yunsilicon/xsc/pci/Makefile | 2 +-
>> drivers/net/ethernet/yunsilicon/xsc/pci/cmd.c | 708 ++++++++++++++++++
>> drivers/net/ethernet/yunsilicon/xsc/pci/cmd.h | 87 +++
>> .../net/ethernet/yunsilicon/xsc/pci/main.c | 18 +
>> 6 files changed, 1378 insertions(+), 1 deletion(-)
>> create mode 100644 drivers/net/ethernet/yunsilicon/xsc/common/xsc_cmd.h
>> create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/cmd.c
>> create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/cmd.h
>>
>> diff --git a/drivers/net/ethernet/yunsilicon/xsc/common/xsc_cmd.h b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_cmd.h
>> new file mode 100644
>> index 000000000..4dc650f83
>> --- /dev/null
>> +++ b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_cmd.h
>> @@ -0,0 +1,560 @@
>> +/* SPDX-License-Identifier: GPL-2.0 */
>> +/* Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
>> + * All rights reserved.
>> + */
>> +
>> +#ifndef __XSC_CMD_H
>> +#define __XSC_CMD_H
>> +
>> +#include <linux/if_ether.h>
>> +#include <linux/types.h>
>> +
>> +struct xsc_core_device;
>> +
>> +#define XSC_CMDQ_VERSION 0x32
>> +
>> +#define XSC_BOARD_SN_LEN 32
>> +#define XSC_MAX_NUM_PCIE_INTF 2
>> +#define XSC_MAX_PF_NUM_PER_PCIE 8
>> +
>> +/* hca_cap mbox version */
>> +#define XSC_CMD_QUERY_HCA_CAP_V1 1
>> +
>> +enum {
>> + XSC_CMD_STAT_OK = 0x0,
>> + XSC_CMD_STAT_BAD_OP_ERR = 0x2,
>> + XSC_CMD_STAT_BAD_PARAM_ERR = 0x3,
>> + XSC_CMD_STAT_RES_BUSY = 0x6,
>> + XSC_CMD_STAT_IX_ERR = 0xa,
>> + XSC_CMD_STAT_QP_FLUSH_BUSY = 0x4b,
>> + XSC_CMD_STAT_QP_FLUSH_PENDING = 0x4c,
>> + XSC_CMD_STAT_INVAL_FUNC = 0x41,
>> +};
>> +
>> +enum {
>> + XSC_CMD_OP_QUERY_HCA_CAP = 0x100,
>> + XSC_CMD_OP_QUERY_CMDQ_VERSION = 0x10a,
>> + XSC_CMD_OP_FUNCTION_RESET = 0x10c,
>> +
>> + XSC_CMD_OP_CREATE_EQ = 0x301,
>> + XSC_CMD_OP_DESTROY_EQ = 0x302,
>> +
>> + XSC_CMD_OP_CREATE_CQ = 0x400,
>> + XSC_CMD_OP_DESTROY_CQ = 0x401,
>> +
>> + XSC_CMD_OP_CREATE_QP = 0x500,
>> + XSC_CMD_OP_DESTROY_QP = 0x501,
>> + XSC_CMD_OP_2RST_QP = 0x50a,
>> + XSC_CMD_OP_RTR2RTS_QP = 0x504,
>> +
>> + XSC_CMD_OP_ENABLE_NIC_HCA = 0x810,
>> + XSC_CMD_OP_DISABLE_NIC_HCA = 0x811,
>> + XSC_CMD_OP_MODIFY_RAW_QP = 0x81f,
>> +
>> + XSC_CMD_OP_QUERY_VPORT_STATE = 0x822,
>> + XSC_CMD_OP_QUERY_EVENT_TYPE = 0x831,
>> +
>> + XSC_CMD_OP_ENABLE_MSIX = 0x850,
>> +
>> + XSC_CMD_OP_SET_MTU = 0x1100,
>> + XSC_CMD_OP_QUERY_ETH_MAC = 0x1101,
>> +
>> + XSC_CMD_OP_SET_PORT_ADMIN_STATUS = 0x1801,
>> +};
>> +
>> +enum xsc_dma_direct {
>> + XSC_DMA_DIR_TO_MAC,
>> +};
>> +
>> +enum {
>> + XSC_TBM_CAP_HASH_PPH = 0,
>> + XSC_TBM_CAP_RSS,
>> + XSC_TBM_CAP_PCT_DROP_CONFIG,
>> +};
>> +
>> +struct xsc_inbox_hdr {
>> + __be16 opcode;
>> + u8 rsvd[4];
>> + __be16 ver;
>> +};
>> +
>> +struct xsc_outbox_hdr {
>> + u8 status;
>> + u8 rsvd[5];
>> + __be16 ver;
>> +};
>> +
>> +/* CQ mbox */
>> +struct xsc_cq_context {
>> + __be16 eqn;
>> + __be16 pa_num;
>> + __be16 glb_func_id;
>> + u8 log_cq_sz;
>> + u8 cq_type;
>> +};
>> +
>> +struct xsc_create_cq_mbox_in {
>> + struct xsc_inbox_hdr hdr;
>> + struct xsc_cq_context ctx;
>> + __be64 pas[];
>> +};
>> +
>> +struct xsc_create_cq_mbox_out {
>> + struct xsc_outbox_hdr hdr;
>> + __be32 cqn;
>> + u8 rsvd[4];
>> +};
>> +
>> +struct xsc_destroy_cq_mbox_in {
>> + struct xsc_inbox_hdr hdr;
>> + __be32 cqn;
>> + u8 rsvd[4];
>> +};
>> +
>> +struct xsc_destroy_cq_mbox_out {
>> + struct xsc_outbox_hdr hdr;
>> + u8 rsvd[8];
>> +};
>> +
>> +/* QP mbox */
>> +struct __packed xsc_create_qp_request {
>> + __be16 input_qpn;
>> + __be16 pa_num;
>> + u8 qp_type;
>> + u8 log_sq_sz;
>> + u8 log_rq_sz;
>> + u8 dma_direct;
>> + __be32 pdn;
>> + __be16 cqn_send;
>> + __be16 cqn_recv;
>> + __be16 glb_funcid;
>> + u8 page_shift;
>> + u8 rsvd[5];
>> + __be64 pas[];
>> +};
>> +
>> +struct xsc_create_qp_mbox_in {
>> + struct xsc_inbox_hdr hdr;
>> + struct xsc_create_qp_request req;
>> +};
>> +
>> +struct xsc_create_qp_mbox_out {
>> + struct xsc_outbox_hdr hdr;
>> + __be32 qpn;
>> + u8 rsvd[4];
>> +};
>> +
>> +struct xsc_destroy_qp_mbox_in {
>> + struct xsc_inbox_hdr hdr;
>> + __be32 qpn;
>> + u8 rsvd[4];
>> +};
>> +
>> +struct xsc_destroy_qp_mbox_out {
>> + struct xsc_outbox_hdr hdr;
>> + u8 rsvd[8];
>> +};
>> +
>> +struct xsc_qp_context {
>> + __be32 remote_qpn;
>> + __be32 cqn_send;
>> + __be32 cqn_recv;
>> + __be32 next_send_psn;
>> + __be32 next_recv_psn;
>> + __be32 pdn;
>> + __be16 src_udp_port;
>> + __be16 path_id;
>> + u8 mtu_mode;
>> + u8 lag_sel;
>> + u8 lag_sel_en;
>> + u8 retry_cnt;
>> + u8 rnr_retry;
>> + u8 dscp;
>> + u8 state;
>> + u8 hop_limit;
>> + u8 dmac[6];
>> + u8 smac[6];
>> + __be32 dip[4];
>> + __be32 sip[4];
>> + __be16 ip_type;
>> + __be16 grp_id;
>> + u8 vlan_valid;
>> + u8 dci_cfi_prio_sl;
>> + __be16 vlan_id;
>> + u8 qp_out_port;
>> + u8 pcie_no;
>> + __be16 lag_id;
>> + __be16 func_id;
>> + u8 max_sra;
>> + u8 max_rra;
>> + u8 no_need_wait;
>> + u8 swith_qp_type;
>> + u8 rsvd0[2];
>> + __be32 qp_access_flags;
>> +};
>> +
>> +struct xsc_modify_qp_mbox_in {
>> + struct xsc_inbox_hdr hdr;
>> + __be32 qpn;
>> + struct xsc_qp_context ctx;
>> + u8 no_need_wait;
>> +};
>> +
>> +struct xsc_modify_qp_mbox_out {
>> + struct xsc_outbox_hdr hdr;
>> + u8 rsvd[8];
>> +};
>> +
>> +struct xsc_modify_raw_qp_request {
>> + __be16 qpn;
>> + __be16 lag_id;
>> + __be16 func_id;
>> + u8 dma_direct;
>> + u8 prio;
>> + u8 qp_out_port;
>> + u8 rsvd[7];
>> +};
>> +
>> +struct xsc_modify_raw_qp_mbox_in {
>> + struct xsc_inbox_hdr hdr;
>> + u8 pcie_no;
>> + u8 rsvd[7];
>> + struct xsc_modify_raw_qp_request req;
>> +};
>> +
>> +struct xsc_modify_raw_qp_mbox_out {
>> + struct xsc_outbox_hdr hdr;
>> + u8 rsvd[8];
>> +};
>> +
>> +/* MSIX TABLE mbox */
>> +struct xsc_msix_table_info_mbox_in {
>> + struct xsc_inbox_hdr hdr;
>> + __be16 index;
>> + u8 rsvd[6];
>> +};
>> +
>> +struct xsc_msix_table_info_mbox_out {
>> + struct xsc_outbox_hdr hdr;
>> + __be32 addr_lo;
>> + __be32 addr_hi;
>> + __be32 data;
>> +};
>> +
>> +/* EQ mbox */
>> +struct xsc_eq_context {
>> + __be16 vecidx;
>> + __be16 pa_num;
>> + u8 log_eq_sz;
>> + u8 rsvd1;
>> + __be16 glb_func_id;
>> + u8 is_async_eq;
>> + u8 rsvd;
>> +};
>> +
>> +struct xsc_create_eq_mbox_in {
>> + struct xsc_inbox_hdr hdr;
>> + struct xsc_eq_context ctx;
>> + u8 rsvd[6];
>> + __be64 pas[];
>> +};
>> +
>> +struct xsc_create_eq_mbox_out {
>> + struct xsc_outbox_hdr hdr;
>> + __be32 eqn;
>> + u8 rsvd[4];
>> +};
>> +
>> +struct xsc_destroy_eq_mbox_in {
>> + struct xsc_inbox_hdr hdr;
>> + __be32 eqn;
>> + u8 rsvd[4];
>> +};
>> +
>> +struct xsc_destroy_eq_mbox_out {
>> + struct xsc_outbox_hdr hdr;
>> + u8 rsvd[8];
>> +};
>> +
>> +struct xsc_cmd_query_cmdq_ver_mbox_in {
>> + struct xsc_inbox_hdr hdr;
>> + u8 rsvd[8];
>> +};
>> +
>> +struct xsc_cmd_query_cmdq_ver_mbox_out {
>> + struct xsc_outbox_hdr hdr;
>> + __be16 cmdq_ver;
>> + u8 rsvd[6];
>> +};
>> +
>> +/* fw mailbox layout, do not change */
>> +struct xsc_fw_version {
>> + u8 rsvd[16];
>> +};
>> +
>> +struct __packed xsc_hca_cap {
>> + u8 rsvd1[12];
>> + u8 send_seg_num;
>> + u8 send_wqe_shift;
>> + u8 recv_seg_num;
>> + u8 recv_wqe_shift;
>> + u8 log_max_srq_sz;
>> + u8 log_max_qp_sz;
>> + u8 log_max_mtt;
>> + u8 log_max_qp;
>> + u8 log_max_strq_sz;
>> + u8 log_max_srqs;
>> + u8 rsvd4[2];
>> + u8 log_max_tso;
>> + u8 log_max_cq_sz;
>> + u8 rsvd6;
>> + u8 log_max_cq;
>> + u8 log_max_eq_sz;
>> + u8 log_max_mkey;
>> + u8 log_max_msix;
>> + u8 log_max_eq;
>> + u8 max_indirection;
>> + u8 log_max_mrw_sz;
>> + u8 log_max_bsf_list_sz;
>> + u8 log_max_klm_list_sz;
>> + u8 rsvd_8_0;
>> + u8 log_max_ra_req_dc;
>> + u8 rsvd_8_1;
>> + u8 log_max_ra_res_dc;
>> + u8 rsvd9;
>> + u8 log_max_ra_req_qp;
>> + u8 log_max_qp_depth;
>> + u8 log_max_ra_res_qp;
>> + __be16 max_vfs;
>> + __be16 raweth_qp_id_end;
>> + __be16 raw_tpe_qp_num;
>> + __be16 max_qp_count;
>> + __be16 raweth_qp_id_base;
>> + u8 rsvd13;
>> + u8 local_ca_ack_delay;
>> + u8 max_num_eqs;
>> + u8 num_ports;
>> + u8 log_max_msg;
>> + u8 mac_port;
>> + __be16 raweth_rss_qp_id_base;
>> + __be16 stat_rate_support;
>> + u8 rsvd16[8];
>> + __be64 flags;
>> + u8 rsvd17;
>> + u8 uar_sz;
>> + u8 rsvd18;
>> + u8 log_pg_sz;
>> + __be16 bf_log_bf_reg_size;
>> + __be16 msix_base;
>> + __be16 msix_num;
>> + __be16 max_desc_sz_sq;
>> + u8 rsvd20[2];
>> + __be16 max_desc_sz_rq;
>> + u8 rsvd21[2];
>> + __be16 max_desc_sz_sq_dc;
>> + u8 rsvd22[4];
>> + __be16 max_qp_mcg;
>> + u8 rsvd23;
>> + u8 log_max_mcg;
>> + u8 rsvd24;
>> + u8 log_max_pd;
>> + u8 rsvd25;
>> + u8 log_max_xrcd;
>> + u8 rsvd26[40];
>> + __be32 uar_page_sz;
>> + u8 rsvd27[8];
>> + __be32 hw_feature_flag;
>> + __be16 pf0_vf_funcid_base;
>> + __be16 pf0_vf_funcid_top;
>> + __be16 pf1_vf_funcid_base;
>> + __be16 pf1_vf_funcid_top;
>> + __be16 pcie0_pf_funcid_base;
>> + __be16 pcie0_pf_funcid_top;
>> + __be16 pcie1_pf_funcid_base;
>> + __be16 pcie1_pf_funcid_top;
>> + u8 log_msx_atomic_size_qp;
>> + u8 pcie_host;
>> + u8 rsvd28;
>> + u8 log_msx_atomic_size_dc;
>> + u8 board_sn[XSC_BOARD_SN_LEN];
>> + u8 max_tc;
>> + u8 mac_bit;
>> + __be16 funcid_to_logic_port;
>> + u8 rsvd29[6];
>> + u8 nif_port_num;
>> + u8 reg_mr_via_cmdq;
>> + __be32 hca_core_clock;
>> + __be32 max_rwq_indirection_tables;
>> + __be32 max_rwq_indirection_table_size;
>> + __be32 chip_ver_h;
>> + __be32 chip_ver_m;
>> + __be32 chip_ver_l;
>> + __be32 hotfix_num;
>> + __be32 feature_flag;
>> + __be32 rx_pkt_len_max;
>> + __be32 glb_func_id;
>> + __be64 tx_db;
>> + __be64 rx_db;
>> + __be64 complete_db;
>> + __be64 complete_reg;
>> + __be64 event_db;
>> + __be32 qp_rate_limit_min;
>> + __be32 qp_rate_limit_max;
>> + struct xsc_fw_version fw_ver;
>> + u8 lag_logic_port_ofst;
>> + u8 rsvd32[7];
>> + /* V1 */
>> + __be64 max_mr_size;
>> + __be16 max_cmd_in_len;
>> + __be16 max_cmd_out_len;
>> + u8 rsvd_v2_v3[36];
>> + /* V4 */
>> + __be16 vf_funcid_base[XSC_MAX_NUM_PCIE_INTF][XSC_MAX_PF_NUM_PER_PCIE];
>> + __be16 vf_funcid_top[XSC_MAX_NUM_PCIE_INTF][XSC_MAX_PF_NUM_PER_PCIE];
>> + __be16 pf_funcid_base[XSC_MAX_NUM_PCIE_INTF];
>> + __be16 pf_funcid_top[XSC_MAX_NUM_PCIE_INTF];
>> + u8 pcie_no;
>> + u8 pf_id;
>> + __be16 vf_id;
>> + u8 pcie_host_num;
>> + u8 pf_num_per_pcie;
>> + u8 rsvd_v6[46];
>> +};
>> +
>> +struct xsc_cmd_query_hca_cap_mbox_in {
>> + struct xsc_inbox_hdr hdr;
>> + __be16 cpu_num;
>> + u8 rsvd[6];
>> +};
>> +
>> +struct xsc_cmd_query_hca_cap_mbox_out {
>> + struct xsc_outbox_hdr hdr;
>> + u8 rsvd0[8];
>> + struct xsc_hca_cap hca_cap;
>> +};
>> +
>> +struct xsc_query_vport_state_out {
>> + struct xsc_outbox_hdr hdr;
>> + u8 state;
>> +#define XSC_QUERY_VPORT_ADMIN_STATE_MASK GENMASK(3, 0)
>> +#define XSC_QUERY_VPORT_STATE_MASK GENMASK(7, 4)
>> +};
>> +
>> +struct xsc_query_vport_state_in {
>> + struct xsc_inbox_hdr hdr;
>> + __be32 data0;
>> +#define XSC_QUERY_VPORT_OTHER_VPORT BIT(0)
>> +#define XSC_QUERY_VPORT_VPORT_NUM_MASK GENMASK(16, 1)
>> +};
>> +
>> +enum {
>> + XSC_CMD_EVENT_RESP_CHANGE_LINK = BIT(0),
>> +};
>> +
>> +struct xsc_event_resp {
>> + u8 resp_cmd_type;
>> +};
>> +
>> +struct xsc_event_query_type_mbox_in {
>> + struct xsc_inbox_hdr hdr;
>> + u8 rsvd[2];
>> +};
>> +
>> +struct xsc_event_query_type_mbox_out {
>> + struct xsc_outbox_hdr hdr;
>> + struct xsc_event_resp ctx;
>> +};
>> +
>> +struct xsc_set_mtu_mbox_in {
>> + struct xsc_inbox_hdr hdr;
>> + __be16 mtu;
>> + __be16 rx_buf_sz_min;
>> + u8 mac_port;
>> + u8 rsvd;
>> +};
>> +
>> +struct xsc_set_mtu_mbox_out {
>> + struct xsc_outbox_hdr hdr;
>> +};
>> +
>> +struct xsc_query_eth_mac_mbox_in {
>> + struct xsc_inbox_hdr hdr;
>> + u8 index;
>> +};
>> +
>> +struct xsc_query_eth_mac_mbox_out {
>> + struct xsc_outbox_hdr hdr;
>> + u8 mac[ETH_ALEN];
>> +};
>> +
>> +struct xsc_nic_attr {
>> + __be16 caps;
>> + __be16 caps_mask;
>> + u8 mac_addr[ETH_ALEN];
>> +};
>> +
>> +struct xsc_rss_attr {
>> + u8 rss_en;
>> + u8 hfunc;
>> + __be16 rqn_base;
>> + __be16 rqn_num;
>> + u8 rsvd[2];
>> + __be32 hash_tmpl;
>> +};
>> +
>> +struct xsc_cmd_enable_nic_hca_mbox_in {
>> + struct xsc_inbox_hdr hdr;
>> + struct xsc_nic_attr nic;
>> + struct xsc_rss_attr rss;
>> +};
>> +
>> +struct xsc_cmd_enable_nic_hca_mbox_out {
>> + struct xsc_outbox_hdr hdr;
>> + u8 rsvd0[2];
>> +};
>> +
>> +struct xsc_nic_dis_attr {
>> + __be16 caps;
>> +};
>> +
>> +struct xsc_cmd_disable_nic_hca_mbox_in {
>> + struct xsc_inbox_hdr hdr;
>> + struct xsc_nic_dis_attr nic;
>> +};
>> +
>> +struct xsc_cmd_disable_nic_hca_mbox_out {
>> + struct xsc_outbox_hdr hdr;
>> + u8 rsvd0[4];
>> +};
>> +
>> +struct xsc_function_reset_mbox_in {
>> + struct xsc_inbox_hdr hdr;
>> + __be16 glb_func_id;
>> + u8 rsvd[6];
>> +};
>> +
>> +struct xsc_function_reset_mbox_out {
>> + struct xsc_outbox_hdr hdr;
>> + u8 rsvd[8];
>> +};
>> +
>> +struct xsc_event_set_port_admin_status_mbox_in {
>> + struct xsc_inbox_hdr hdr;
>> + __be16 status;
>> +};
>> +
>> +struct xsc_event_set_port_admin_status_mbox_out {
>> + struct xsc_outbox_hdr hdr;
>> + __be32 status;
>> +};
>> +
>> +int xsc_cmd_init(struct xsc_core_device *xdev);
>> +void xsc_cmd_cleanup(struct xsc_core_device *xdev);
>> +int xsc_cmd_exec(struct xsc_core_device *xdev,
>> + void *in, unsigned int in_size,
>> + void *out, unsigned int out_size);
>> +int xsc_cmd_version_check(struct xsc_core_device *xdev);
>> +
>> +int xsc_cmd_status_to_err(struct xsc_outbox_hdr *hdr);
>> +
>> +#endif
>> diff --git a/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
>> index 9580d89a1..a6f0149ed 100644
>> --- a/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
>> +++ b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
>> @@ -24,12 +24,16 @@
>> #define XSC_PAGE_SHIFT_4K 12
>> #define XSC_PAGE_SIZE_4K (_AC(1, UL) << XSC_PAGE_SHIFT_4K)
>>
>> +struct xsc_cmd;
>> +
>> struct xsc_core_device {
>> struct pci_dev *pdev;
>> struct device *device;
>>
>> void __iomem *bar;
>> int bar_num;
>> +
>> + struct xsc_cmd *cmd;
>> };
>>
>> void xsc_update_tx_db(struct xsc_core_device *xdev, u32 sqn, u32 next_pid);
>> diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/Makefile b/drivers/net/ethernet/yunsilicon/xsc/pci/Makefile
>> index 4a08b92cd..e17a10cc1 100644
>> --- a/drivers/net/ethernet/yunsilicon/xsc/pci/Makefile
>> +++ b/drivers/net/ethernet/yunsilicon/xsc/pci/Makefile
>> @@ -6,4 +6,4 @@ ccflags-y += -I$(srctree)/drivers/net/ethernet/yunsilicon/xsc
>>
>> obj-$(CONFIG_YUNSILICON_XSC_PCI) += xsc_pci.o
>>
>> -xsc_pci-y := main.o doorbell.o
>> +xsc_pci-y := main.o cmd.o doorbell.o
>> diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/cmd.c b/drivers/net/ethernet/yunsilicon/xsc/pci/cmd.c
>> new file mode 100644
>> index 000000000..45dcdb2b4
>> --- /dev/null
>> +++ b/drivers/net/ethernet/yunsilicon/xsc/pci/cmd.c
>> @@ -0,0 +1,708 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +/*
>> + * Copyright (c) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
>> + * All rights reserved.
>> + * Copyright (c) 2013-2016, Mellanox Technologies. All rights reserved.
>> + */
>> +
>> +#include <linux/bitmap.h>
>> +#include <linux/delay.h>
>> +#include <linux/dma-mapping.h>
>> +#include <linux/kthread.h>
>> +#include <linux/pci.h>
>> +#include <linux/slab.h>
>> +
>> +#include "common/xsc_cmd.h"
>> +#include "cmd.h"
>> +#include "common/xsc_core.h"
>> +
>> +#define XSC_CMD_DATA_BLOCK_SIZE 512
>> +#define XSC_CMD_FIRST_SIZE 8
>> +
>> +struct xsc_cmd_prot_block {
>> + u8 data[XSC_CMD_DATA_BLOCK_SIZE];
>> + u8 rsvd0[48];
>> + __be64 next;
>> + __be32 block_num;
>> + u8 owner_status;
>> + u8 token;
>> + u8 ctrl_sig;
>> + u8 sig;
>> +};
>> +
>> +/* hw layout, do not change */
>> +struct xsc_cmd_layout {
>> + u16 rsv0;
>> + u8 rsv[2];
>> + __be32 inlen;
>> + __be64 in_ptr;
>> + __be32 in[XSC_CMD_FIRST_SIZE];
>> + __be64 out_ptr;
>> + __be32 outlen;
>> + u8 token;
>> + u8 sig;
>> + u8 idx;
>> + u8 type_owner;
>> +};
>> +
>> +struct xsc_rsp_layout {
>> + u16 rsv0;
>> + u8 rsv[2];
>> + __be32 out[XSC_RSP_FIRST_SIZE];
>> + u8 token;
>> + u8 sig;
>> + u8 idx;
>> + u8 type_owner;
>> +};
>> +
>> +#define XSC_CMD_TYPE_MASK GENMASK(6, 0)
>> +#define XSC_CMD_OWNER_BIT BIT(7)
>> +
>> +enum {
>> + XSC_CMDQ_ERR_HW_READ_REQ_ERR = BIT(0),
>> + XSC_CMDQ_ERR_HW_WRITE_REQ_ERR = BIT(1),
>> + XSC_CMDQ_ERR_REQ_PID_ERR = BIT(2),
>> + XSC_CMDQ_ERR_RSP_CID_ERR = BIT(3),
>> +};
>> +
>> +enum {
>> + XSC_CMDQ_ELEMENT_SIZE_LOG = 6,
>> + XSC_CMDQ_Q_DEPTH_LOG = 5,
>> +};
>> +
>> +static int xsc_alloc_ent(struct xsc_cmd *cmd)
>> +{
>> + unsigned long flags;
>> + int ret;
>> +
>> + spin_lock_irqsave(&cmd->alloc_lock, flags);
>> + ret = find_first_bit(&cmd->cmd_entry_mask, cmd->max_reg_cmds);
>> + if (ret < cmd->max_reg_cmds)
>> + clear_bit(ret, &cmd->cmd_entry_mask);
>> + spin_unlock_irqrestore(&cmd->alloc_lock, flags);
>> +
>> + return ret < cmd->max_reg_cmds ? ret : -ENOSPC;
>> +}
>> +
>> +static void xsc_free_ent(struct xsc_cmd *cmd, int idx)
>> +{
>> + unsigned long flags;
>> +
>> + spin_lock_irqsave(&cmd->alloc_lock, flags);
>> + set_bit(idx, &cmd->cmd_entry_mask);
>> + spin_unlock_irqrestore(&cmd->alloc_lock, flags);
>> +}
>> +
>> +static struct xsc_cmd_layout *xsc_get_inst(struct xsc_cmd *cmd, int idx)
>> +{
>> + return cmd->cmd_buf + (idx << cmd->log_stride);
>> +}
>> +
>> +static struct xsc_rsp_layout *xsc_get_cq_inst(struct xsc_cmd *cmd, int idx)
>> +{
>> + return cmd->cq_buf + (idx << cmd->log_stride);
>> +}
>> +
>> +static u8 xsc_rsp_owner_bit(struct xsc_rsp_layout *rsp)
>> +{
>> + return !!(rsp->type_owner & XSC_CMD_OWNER_BIT);
>> +}
>> +
>> +static u16 xsc_msg_to_opcode(const struct xsc_cmd_msg *in)
>> +{
>> + const struct xsc_inbox_hdr *hdr = (const void *)in->first;
>> +
>> + return be16_to_cpu(hdr->opcode);
>> +}
>> +
>> +static struct xsc_cmd_mailbox *xsc_alloc_cmd_box(struct xsc_core_device *xdev,
>> + gfp_t flags)
>> +{
>> + struct xsc_cmd_mailbox *mailbox;
>> +
>> + mailbox = kmalloc_obj(*mailbox, flags);
>> + if (!mailbox)
>> + return ERR_PTR(-ENOMEM);
>> +
>> + mailbox->buf = dma_pool_zalloc(xdev->cmd->pool, flags, &mailbox->dma);
>> + if (!mailbox->buf) {
>> + kfree(mailbox);
>> + return ERR_PTR(-ENOMEM);
>> + }
>> + mailbox->next = NULL;
>> +
>> + return mailbox;
>> +}
>> +
>> +static void xsc_free_cmd_box(struct xsc_core_device *xdev,
>> + struct xsc_cmd_mailbox *mailbox)
>> +{
>> + dma_pool_free(xdev->cmd->pool, mailbox->buf, mailbox->dma);
>> + kfree(mailbox);
>> +}
>> +
>> +static struct xsc_cmd_msg *xsc_alloc_msg(struct xsc_core_device *xdev,
>> + unsigned int size,
>> + unsigned int first_size)
>> +{
>> + struct xsc_cmd_mailbox *tmp, *head = NULL;
>> + struct xsc_cmd_prot_block *block;
>> + struct xsc_cmd_msg *msg;
>> + unsigned int blen;
>> + int err;
>> + int n;
>> + int i;
>> +
>> + msg = kzalloc_obj(*msg, GFP_KERNEL);
>> + if (!msg)
>> + return ERR_PTR(-ENOMEM);
>> +
>> + blen = size - min(size, first_size);
>> + n = DIV_ROUND_UP(blen, XSC_CMD_DATA_BLOCK_SIZE);
>> +
>> + for (i = 0; i < n; i++) {
>> + tmp = xsc_alloc_cmd_box(xdev, GFP_KERNEL);
>> + if (IS_ERR(tmp)) {
>> + pci_err(xdev->pdev, "failed allocating block\n");
>> + err = PTR_ERR(tmp);
>> + goto err_free;
>> + }
>> +
>> + block = tmp->buf;
>> + tmp->next = head;
>> + block->next = cpu_to_be64(tmp->next ? tmp->next->dma : 0);
>> + block->block_num = cpu_to_be32(n - i - 1);
>> + head = tmp;
>> + }
>> + msg->next = head;
>> + msg->len = size;
>> + msg->first_size = first_size;
>> +
>> + return msg;
>> +
>> +err_free:
>> + while (head) {
>> + tmp = head->next;
>> + xsc_free_cmd_box(xdev, head);
>> + head = tmp;
>> + }
>> + kfree(msg);
>> +
>> + return ERR_PTR(err);
>> +}
>> +
>> +static void xsc_free_msg(struct xsc_core_device *xdev, struct xsc_cmd_msg *msg)
>> +{
>> + struct xsc_cmd_mailbox *head = msg->next;
>> + struct xsc_cmd_mailbox *next;
>> +
>> + while (head) {
>> + next = head->next;
>> + xsc_free_cmd_box(xdev, head);
>> + head = next;
>> + }
>> + kfree(msg);
>> +}
>> +
>> +static int xsc_copy_to_cmd_msg(struct xsc_cmd_msg *to, const void *from,
>> + unsigned int size)
>> +{
>> + struct xsc_cmd_prot_block *block;
>> + struct xsc_cmd_mailbox *next;
>> + const u8 *src = from;
>> + unsigned int copy;
>> +
>> + copy = min(size, to->first_size);
>> + memcpy(to->first, src, copy);
>> + size -= copy;
>> + src += copy;
>> +
>> + next = to->next;
>> + while (size) {
>> + if (!next) {
>> + WARN_ONCE(1, "Mailbox too short\n");
>> + return -ENOMEM;
>> + }
>> +
>> + copy = min(size, XSC_CMD_DATA_BLOCK_SIZE);
>> + block = next->buf;
>> + memcpy(block->data, src, copy);
>> + block->owner_status = 0;
>> + src += copy;
>> + size -= copy;
>> + next = next->next;
>> + }
>> +
>> + return 0;
>> +}
>> +
>> +static int xsc_copy_from_rsp_msg(void *to, const struct xsc_cmd_msg *from,
>> + unsigned int size)
>> +{
>> + struct xsc_cmd_prot_block *block;
>> + struct xsc_cmd_mailbox *next;
>> + u8 *dst = to;
>> + unsigned int copy;
>> +
>> + copy = min(size, from->first_size);
>> + memcpy(dst, from->first, copy);
>> + size -= copy;
>> + dst += copy;
>> +
>> + next = from->next;
>> + while (size) {
>> + if (!next) {
>> + WARN_ONCE(1, "Mailbox too short\n");
>> + return -ENOMEM;
>> + }
>> +
>> + copy = min(size, XSC_CMD_DATA_BLOCK_SIZE);
>> + block = next->buf;
>> + if (!block->owner_status)
>> + return -EIO;
>> +
>> + memcpy(dst, block->data, copy);
>> + dst += copy;
>> + size -= copy;
>> + next = next->next;
>> + }
>> +
>> + return 0;
>> +}
>> +
>> +static int xsc_cmd_invoke(struct xsc_core_device *xdev, struct xsc_cmd_msg *in,
>> + struct xsc_cmd_msg *out, u8 *status)
>> +{
>> + struct xsc_cmd *cmd = xdev->cmd;
>> + struct xsc_cmd_layout *lay;
>> + struct xsc_cmd_work_ent *ent;
>> + unsigned long timeout;
>> + unsigned long flags;
>> + int err;
>> +
>> + ent = kzalloc_obj(*ent, GFP_KERNEL);
>> + if (!ent)
>> + return -ENOMEM;
>> +
>> + ent->in = in;
>> + ent->out = out;
>> + init_completion(&ent->done);
>> + down(&cmd->sem);
>> + ent->idx = xsc_alloc_ent(cmd);
>> + if (ent->idx < 0) {
>> + pci_err(xdev->pdev, "failed to allocate command entry\n");
>> + up(&cmd->sem);
>> + kfree(ent);
>> + return -ENOMEM;
>> + }
>> + ent->token = ent->idx + 1;
>> + cmd->ent_arr[ent->idx] = ent;
>> +
>> + spin_lock_irqsave(&cmd->doorbell_lock, flags);
>> + lay = xsc_get_inst(cmd, cmd->cmd_pid);
>> + memset(lay, 0, sizeof(*lay));
>> + memcpy(lay->in, in->first, sizeof(lay->in));
>> + if (in->next)
>> + lay->in_ptr = cpu_to_be64(in->next->dma);
>> + lay->inlen = cpu_to_be32(in->len);
>> + if (out->next)
>> + lay->out_ptr = cpu_to_be64(out->next->dma);
>> + lay->outlen = cpu_to_be32(out->len);
>> + lay->type_owner = FIELD_PREP(XSC_CMD_TYPE_MASK, XSC_PCI_CMD_XPORT);
>> + lay->token = ent->token;
>> + lay->idx = ent->idx;
>> + lay->sig = 0xff;
>> +
>> + /* ring doorbell after the descriptor is valid */
>> + wmb();
>> +
>> + cmd->cmd_pid = (cmd->cmd_pid + 1) % (1 << cmd->log_sz);
>> + xsc_update_cmdq_req_pid(xdev, cmd->cmd_pid);
>> + spin_unlock_irqrestore(&cmd->doorbell_lock, flags);
>> +
>> + timeout = msecs_to_jiffies(XSC_CMD_TIMEOUT_MSEC);
>> + if (!wait_for_completion_timeout(&ent->done, timeout)) {
>> + cmd->cmd_status = XSC_CMD_STATUS_TIMEDOUT;
>> + pci_err(xdev->pdev, "wait for opcode 0x%x response timeout\n",
>> + xsc_msg_to_opcode(in));
>> + /* the entry stays registered for the completion handler */
>> + return -ETIMEDOUT;
>> + }
>> +
>> + err = ent->ret;
>> + *status = ent->status;
>> +
>> + spin_lock_irqsave(&cmd->alloc_lock, flags);
>> + if (cmd->ent_arr[ent->idx] == ent)
>> + cmd->ent_arr[ent->idx] = NULL;
>> + spin_unlock_irqrestore(&cmd->alloc_lock, flags);
>> + kfree(ent);
>> +
>> + return err;
>> +}
>> +
>> +static void xsc_cmd_comp_handler(struct xsc_core_device *xdev, u8 idx,
>> + struct xsc_rsp_layout *rsp)
>> +{
>> + struct xsc_cmd *cmd = xdev->cmd;
>> + struct xsc_cmd_work_ent *ent;
>> +
>> + if (idx >= cmd->max_reg_cmds || (cmd->cmd_entry_mask & BIT(idx))) {
>> + pci_err(xdev->pdev, "idx[%d] exceeds max cmds or has no request\n",
>> + idx);
>> + return;
>> + }
>> + ent = cmd->ent_arr[idx];
>> +
>> + memcpy(ent->out->first, rsp->out, sizeof(rsp->out));
>> + ent->ret = 0;
>> + ent->status = ((struct xsc_outbox_hdr *)ent->out->first)->status;
>> + cmd->retry_cnt = 0;
>> +
>> + xsc_free_ent(cmd, ent->idx);
>> + complete(&ent->done);
>> + up(&cmd->sem);
>> +}
>> +
>> +static void xsc_cmd_handle_rsp_before_reload(struct xsc_cmd *cmd,
>> + struct xsc_core_device *xdev)
>> +{
>> + u32 rsp_pid, rsp_cid;
>> +
>> + rsp_pid = xsc_get_cmdq_rsp_pid(xdev) & XSC_CMDQ_INDEX_MASK;
>> + rsp_cid = xsc_get_cmdq_rsp_cid(xdev) & XSC_CMDQ_INDEX_MASK;
>> + if (rsp_pid == rsp_cid)
>> + return;
>> +
>> + cmd->cq_cid = rsp_pid;
>> + xsc_update_cmdq_rsp_cid(xdev, cmd->cq_cid);
>> +}
>> +
>> +static void xsc_cmd_err_handler(struct xsc_core_device *xdev, u32 raw)
>> +{
>> + struct xsc_cmd *cmd = xdev->cmd;
>> + unsigned long flags;
>> + int err = 0;
>> + int retry = 0;
>> + u32 stat = raw;
>> +
>> + while (stat) {
>> + err++;
>> + if (stat & XSC_CMDQ_ERR_HW_READ_REQ_ERR) {
>> + retry = 1;
>> + stat &= ~XSC_CMDQ_ERR_HW_READ_REQ_ERR;
>> + pci_err(xdev->pdev, "hw read request from host failed\n");
>> + } else if (stat & XSC_CMDQ_ERR_HW_WRITE_REQ_ERR) {
>> + retry = 1;
>> + stat &= ~XSC_CMDQ_ERR_HW_WRITE_REQ_ERR;
>> + pci_err(xdev->pdev, "hw write request to fw failed\n");
>> + } else if (stat & XSC_CMDQ_ERR_REQ_PID_ERR) {
>> + stat &= ~XSC_CMDQ_ERR_REQ_PID_ERR;
>> + pci_err(xdev->pdev, "hw reported unexpected req pid\n");
>> + } else if (stat & XSC_CMDQ_ERR_RSP_CID_ERR) {
>> + stat &= ~XSC_CMDQ_ERR_RSP_CID_ERR;
>> + pci_err(xdev->pdev, "hw reported unexpected rsp cid\n");
>> + } else {
>> + stat = 0;
>> + pci_err(xdev->pdev, "ignore unknown cmdq error\n");
>> + }
>> + }
>> +
>> + if (retry) {
>> + if (cmd->retry_cnt >= XSC_CMD_MAX_RETRY_CNT) {
>> + pci_warn(xdev->pdev, "error handler retried %d times, stop retrying\n",
>> + cmd->retry_cnt);
>> + } else {
>> + spin_lock_irqsave(&cmd->doorbell_lock, flags);
>> + xsc_update_cmdq_req_pid(xdev, cmd->cmd_pid);
>> + cmd->retry_cnt++;
>> + spin_unlock_irqrestore(&cmd->doorbell_lock, flags);
>> + }
>> + }
>> +
>> + if (err)
>> + xsc_clear_cmdq_err_status(xdev, raw);
>> +}
>> +
>> +static int xsc_cmd_cq_polling(void *data)
>> +{
>> + struct xsc_core_device *xdev = data;
>> + struct xsc_cmd *cmd = xdev->cmd;
>> + struct xsc_rsp_layout *rsp;
>> + u32 err_status;
>> + u32 cq_pid;
>> +
>> + while (!kthread_should_stop()) {
>> + if (need_resched())
>> + schedule();
> cond_resched()
Will use cond_resched().
>
>> +
>> + err_status = xsc_read_cmdq_err_status(xdev);
>> + if (err_status)
>> + xsc_cmd_err_handler(xdev, err_status);
>> +
>> + cq_pid = xsc_get_cmdq_rsp_pid(xdev) & XSC_CMDQ_INDEX_MASK;
>> + if (cmd->cq_cid == cq_pid) {
>> + usleep_range(1000, 2000);
> xsc_cmd_init() creates a kthread that polls the response ring every
> 1-2ms, even with no command in flight. 1-2ms means around 1000
> periodic wakeups/context switches on one cpu.
>
> It reminds me of future RDMA support. I noticed there are a few
> production applications where frequent create/modify/destroy of pairs
> are unavoidable, meaning the latency there is sensitive. It's unlike
> TCP 3-way handshake. 1-2ms will possibly add huge latency
> comparatively.
>
> With that said, what I had in my mind is there are three directions:
> 1. event-driven as you mentioned. Patch 5 already brings up the EQ and MSI-X.
> 2. demand-driven (dedicated) thread. Keep the kthread, but let the
> submitter wake it and have it sleep on a waitqueue once no command is
> outstanding.
> 3. Or drop the kthread and let the submitter poll in xsc_cmd_invoke()
> with read_poll_timeout(), using a staged delay (udelay(1) a few times,
> then usleep_range(20, 30), then 1ms) as bnxt does in __hwrm_send().
> This also removes the fixed 1ms floor per command.
>
> Any thoughts on this?
>
> Thanks,
> Jason
This is an intentional trade-off for
this series. The command queue only serves the control plane (QP/CQ/EQ
create/destroy, queries, config), not the data path, so the completion
latency does not affect throughput. This version ships a single-channel
ethernet driver where polling is sufficient, and to keep the series lean
(the earlier ~6000-line concern) we keep only the poller for now. We do
plan to move the command queue to EQ-driven completion when multi-channel
and RDMA support land. So I'd like to keep the poller -- I hope the
rationale makes sense.
Thanks,
Xin
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads