* [PATCH v2 01/11] net/xsc: add Yunsilicon XSC ethernet PCI driver
2026-11-26 14:57 [PATCH v2 00/11] net/xsc: add the Yunsilicon XSC ethernet driver Xin Tian
@ 2026-11-26 14:57 ` Xin Tian
2026-11-27 15:57 ` netdev-bot+sashiko
2026-11-26 14:57 ` [PATCH v2 02/11] net/xsc: add doorbell and cmdq MMIO accessors Xin Tian
` (10 subsequent siblings)
11 siblings, 1 reply; 33+ messages in thread
From: Xin Tian @ 2026-11-26 14:57 UTC (permalink / raw)
To: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Jason Gunthorpe, Leon Romanovsky, Junxian Huang
Cc: weihg, linux-kernel, netdev, linux-rdma, kerneljasonxing
skeleton
Add the Yunsilicon vendor Kbuild glue and the PCI layer of the XSC
ethernet driver. For now the PCI core only enables the device, maps
BAR0 and records DMA capabilities; the doorbell, command queue and
netdev layer are added by subsequent patches.
Signed-off-by: Xin Tian <tianx@yunsilicon.com>
---
MAINTAINERS | 7 +
drivers/net/ethernet/Kconfig | 1 +
drivers/net/ethernet/Makefile | 1 +
drivers/net/ethernet/yunsilicon/Kconfig | 26 +++
drivers/net/ethernet/yunsilicon/Makefile | 8 +
.../ethernet/yunsilicon/xsc/common/xsc_core.h | 35 ++++
.../ethernet/yunsilicon/xsc/common/xsc_reg.h | 23 +++
.../net/ethernet/yunsilicon/xsc/pci/Kconfig | 16 ++
.../net/ethernet/yunsilicon/xsc/pci/Makefile | 9 +
.../net/ethernet/yunsilicon/xsc/pci/main.c | 170 ++++++++++++++++++
10 files changed, 296 insertions(+)
create mode 100644 drivers/net/ethernet/yunsilicon/Kconfig
create mode 100644 drivers/net/ethernet/yunsilicon/Makefile
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/common/xsc_reg.h
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/Kconfig
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/Makefile
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/main.c
diff --git a/MAINTAINERS b/MAINTAINERS
index c24144478..bc9e7d267 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -30069,6 +30069,13 @@ S: Maintained
F: Documentation/input/devices/yealink.rst
F: drivers/input/misc/yealink.*
+YUNSILICON XSC DRIVERS
+M: Honggang Wei <weihg@yunsilicon.com>
+M: Xin Tian <tianx@yunsilicon.com>
+L: netdev@vger.kernel.org
+S: Maintained
+F: drivers/net/ethernet/yunsilicon/xsc
+
ZD1211RW WIRELESS DRIVER
L: linux-wireless@vger.kernel.org
S: Orphan
diff --git a/drivers/net/ethernet/Kconfig b/drivers/net/ethernet/Kconfig
index 8581ccba1..88cdf87b0 100644
--- a/drivers/net/ethernet/Kconfig
+++ b/drivers/net/ethernet/Kconfig
@@ -189,6 +189,7 @@ source "drivers/net/ethernet/wangxun/Kconfig"
source "drivers/net/ethernet/wiznet/Kconfig"
source "drivers/net/ethernet/xilinx/Kconfig"
source "drivers/net/ethernet/xircom/Kconfig"
+source "drivers/net/ethernet/yunsilicon/Kconfig"
source "drivers/net/ethernet/zte/Kconfig"
endif # ETHERNET
diff --git a/drivers/net/ethernet/Makefile b/drivers/net/ethernet/Makefile
index 2b1153d35..5b571d761 100644
--- a/drivers/net/ethernet/Makefile
+++ b/drivers/net/ethernet/Makefile
@@ -101,6 +101,7 @@ obj-$(CONFIG_NET_VENDOR_VIA) += via/
obj-$(CONFIG_NET_VENDOR_WANGXUN) += wangxun/
obj-$(CONFIG_NET_VENDOR_WIZNET) += wiznet/
obj-$(CONFIG_NET_VENDOR_XILINX) += xilinx/
+obj-$(CONFIG_NET_VENDOR_YUNSILICON) += yunsilicon/
obj-$(CONFIG_NET_VENDOR_XIRCOM) += xircom/
obj-$(CONFIG_NET_VENDOR_SYNOPSYS) += synopsys/
obj-$(CONFIG_NET_VENDOR_PENSANDO) += pensando/
diff --git a/drivers/net/ethernet/yunsilicon/Kconfig b/drivers/net/ethernet/yunsilicon/Kconfig
new file mode 100644
index 000000000..ffdfdf4bf
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/Kconfig
@@ -0,0 +1,26 @@
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
+# All rights reserved.
+#
+# Yunsilicon driver configuration
+#
+
+config NET_VENDOR_YUNSILICON
+ bool "Yunsilicon devices"
+ default y
+ depends on PCI
+ depends on 64BIT || COMPILE_TEST
+ help
+ If you have a network (Ethernet) device belonging to this class,
+ say Y.
+
+ Note that the answer to this question doesn't directly affect the
+ kernel: saying N will just cause the configurator to skip all
+ the questions about Yunsilicon cards. If you say Y, you will be asked
+ for your specific card in the following questions.
+
+if NET_VENDOR_YUNSILICON
+
+source "drivers/net/ethernet/yunsilicon/xsc/pci/Kconfig"
+
+endif # NET_VENDOR_YUNSILICON
diff --git a/drivers/net/ethernet/yunsilicon/Makefile b/drivers/net/ethernet/yunsilicon/Makefile
new file mode 100644
index 000000000..b2924ccaf
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/Makefile
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
+# All rights reserved.
+#
+# Makefile for the Yunsilicon device drivers.
+#
+
+obj-$(CONFIG_YUNSILICON_XSC_PCI) += xsc/pci/
diff --git a/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
new file mode 100644
index 000000000..2d0eda8a2
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
+ * All rights reserved.
+ */
+
+#ifndef __XSC_CORE_H
+#define __XSC_CORE_H
+
+#include <linux/pci.h>
+#include <linux/bitfield.h>
+
+#define XSC_PCI_VENDOR_ID 0x1f67
+
+#define XSC_MC_PF_DEV_ID 0x1011
+
+#define XSC_MF_HOST_PF_DEV_ID 0x1051
+#define XSC_MF_SOC_PF_DEV_ID 0x1053
+
+#define XSC_MS_PF_DEV_ID 0x1111
+
+#define XSC_MV_HOST_PF_DEV_ID 0x1151
+#define XSC_MV_SOC_PF_DEV_ID 0x1153
+
+#define XSC_PAGE_SHIFT_4K 12
+#define XSC_PAGE_SIZE_4K (_AC(1, UL) << XSC_PAGE_SHIFT_4K)
+
+struct xsc_core_device {
+ struct pci_dev *pdev;
+ struct device *device;
+
+ void __iomem *bar;
+ int bar_num;
+};
+
+#endif
diff --git a/drivers/net/ethernet/yunsilicon/xsc/common/xsc_reg.h b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_reg.h
new file mode 100644
index 000000000..26773383f
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_reg.h
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
+ * All rights reserved.
+ */
+
+#ifndef __XSC_REG_H
+#define __XSC_REG_H
+#define CMDQM_HOST_REQ_BUF_BASE_H_ADDR_MEM_ADDR 0x0
+#define CMDQM_HOST_REQ_BUF_BASE_L_ADDR_MEM_ADDR 0x4
+#define CMDQM_HOST_REQ_PID_MEM_ADDR 0x8
+#define CMDQM_HOST_RSP_BUF_BASE_H_ADDR_MEM_ADDR 0x10
+#define CMDQM_HOST_RSP_BUF_BASE_L_ADDR_MEM_ADDR 0x14
+#define CMDQM_HOST_RSP_PID_MEM_ADDR 0x18
+#define CMDQM_HOST_RSP_CID_MEM_ADDR 0x1c
+#define CMDQM_HOST_VF_ERR_STS_MEM_ADDR 0x20
+
+#define DB_CQ_FUNC_MEM_ADDR 0x8c4
+#define DB_EQ_FUNC_MEM_ADDR 0x8c8
+#define DB_CQ_CID_DIRECT_MEM_ADDR 0x8cc
+#define TX_DB_FUNC_MEM_ADDR 0x8d0
+#define RX_DB_FUNC_MEM_ADDR 0x8d4
+
+#endif
diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/Kconfig b/drivers/net/ethernet/yunsilicon/xsc/pci/Kconfig
new file mode 100644
index 000000000..a27a37b3a
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/pci/Kconfig
@@ -0,0 +1,16 @@
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
+# All rights reserved.
+# Yunsilicon PCI configuration
+#
+
+config YUNSILICON_XSC_PCI
+ tristate "Yunsilicon XSC PCI driver"
+ depends on PCI
+ depends on NET
+ help
+ This driver provides the PCI core for Yunsilicon XSC
+ ethernet devices.
+
+ To compile this driver as a module, choose M here. The module
+ will be called xsc_pci.
diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/Makefile b/drivers/net/ethernet/yunsilicon/xsc/pci/Makefile
new file mode 100644
index 000000000..c163aa950
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/pci/Makefile
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
+# All rights reserved.
+
+ccflags-y += -I$(srctree)/drivers/net/ethernet/yunsilicon/xsc
+
+obj-$(CONFIG_YUNSILICON_XSC_PCI) += xsc_pci.o
+
+xsc_pci-y := main.o
diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/main.c b/drivers/net/ethernet/yunsilicon/xsc/pci/main.c
new file mode 100644
index 000000000..c926507a7
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/pci/main.c
@@ -0,0 +1,170 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
+ * All rights reserved.
+ */
+
+#include <linux/slab.h>
+
+#include "common/xsc_core.h"
+
+static const struct pci_device_id xsc_pci_id_table[] = {
+ { PCI_DEVICE(XSC_PCI_VENDOR_ID, XSC_MC_PF_DEV_ID) },
+ { PCI_DEVICE(XSC_PCI_VENDOR_ID, XSC_MF_HOST_PF_DEV_ID) },
+ { PCI_DEVICE(XSC_PCI_VENDOR_ID, XSC_MF_SOC_PF_DEV_ID) },
+ { PCI_DEVICE(XSC_PCI_VENDOR_ID, XSC_MS_PF_DEV_ID) },
+ { PCI_DEVICE(XSC_PCI_VENDOR_ID, XSC_MV_HOST_PF_DEV_ID) },
+ { PCI_DEVICE(XSC_PCI_VENDOR_ID, XSC_MV_SOC_PF_DEV_ID) },
+ { 0 }
+};
+
+static int xsc_set_dma_caps(struct pci_dev *pdev)
+{
+ int err;
+
+ err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
+ if (!err)
+ dma_set_max_seg_size(&pdev->dev, SZ_2G);
+
+ return err;
+}
+
+static int xsc_pci_init(struct xsc_core_device *xdev,
+ const struct pci_device_id *id)
+{
+ struct pci_dev *pdev = xdev->pdev;
+ void __iomem *bar_base;
+ int bar_num = 0;
+ int err;
+
+ err = pci_enable_device(pdev);
+ if (err) {
+ pci_err(pdev, "failed to enable PCI device: err=%d\n", err);
+ goto err_out;
+ }
+
+ err = pci_request_region(pdev, bar_num, KBUILD_MODNAME);
+ if (err) {
+ pci_err(pdev, "failed to request %s pci_region=%d: err=%d\n",
+ KBUILD_MODNAME, bar_num, err);
+ goto err_disable;
+ }
+
+ pci_set_master(pdev);
+
+ err = xsc_set_dma_caps(pdev);
+ if (err) {
+ pci_err(pdev, "failed to set DMA capabilities mask: err=%d\n",
+ err);
+ goto err_clr_master;
+ }
+
+ bar_base = pci_ioremap_bar(pdev, bar_num);
+ if (!bar_base) {
+ pci_err(pdev, "failed to ioremap %s bar%d\n", KBUILD_MODNAME,
+ bar_num);
+ err = -ENOMEM;
+ goto err_clr_master;
+ }
+
+ err = pci_save_state(pdev);
+ if (err) {
+ pci_err(pdev, "pci_save_state failed: err=%d\n", err);
+ goto err_io_unmap;
+ }
+
+ xdev->bar_num = bar_num;
+ xdev->bar = bar_base;
+
+ return 0;
+
+err_io_unmap:
+ pci_iounmap(pdev, bar_base);
+err_clr_master:
+ pci_clear_master(pdev);
+ pci_release_region(pdev, bar_num);
+err_disable:
+ pci_disable_device(pdev);
+err_out:
+ return err;
+}
+
+static void xsc_pci_fini(struct xsc_core_device *xdev)
+{
+ struct pci_dev *pdev = xdev->pdev;
+
+ pci_iounmap(pdev, xdev->bar);
+ pci_clear_master(pdev);
+ pci_release_region(pdev, xdev->bar_num);
+ pci_disable_device(pdev);
+}
+
+static int xsc_pci_probe(struct pci_dev *pci_dev,
+ const struct pci_device_id *id)
+{
+ struct xsc_core_device *xdev;
+ int err;
+
+ xdev = kzalloc_obj(*xdev, GFP_KERNEL);
+ if (!xdev)
+ return -ENOMEM;
+
+ xdev->pdev = pci_dev;
+ xdev->device = &pci_dev->dev;
+
+ pci_set_drvdata(pci_dev, xdev);
+ err = xsc_pci_init(xdev, id);
+ if (err) {
+ pci_err(pci_dev, "xsc_pci_init failed %d\n", err);
+ goto err_unset_pci_drvdata;
+ }
+
+ return 0;
+
+err_unset_pci_drvdata:
+ pci_set_drvdata(pci_dev, NULL);
+ kfree(xdev);
+
+ return err;
+}
+
+static void xsc_pci_remove(struct pci_dev *pci_dev)
+{
+ struct xsc_core_device *xdev = pci_get_drvdata(pci_dev);
+
+ xsc_pci_fini(xdev);
+ pci_set_drvdata(pci_dev, NULL);
+ kfree(xdev);
+}
+
+static struct pci_driver xsc_pci_driver = {
+ .name = "xsc-pci",
+ .id_table = xsc_pci_id_table,
+ .probe = xsc_pci_probe,
+ .remove = xsc_pci_remove,
+};
+
+static int __init xsc_init(void)
+{
+ int err;
+
+ err = pci_register_driver(&xsc_pci_driver);
+ if (err) {
+ pr_err("failed to register pci driver\n");
+ return err;
+ }
+
+ return 0;
+}
+
+static void __exit xsc_fini(void)
+{
+ pci_unregister_driver(&xsc_pci_driver);
+}
+
+module_init(xsc_init);
+module_exit(xsc_fini);
+
+MODULE_DEVICE_TABLE(pci, xsc_pci_id_table);
+
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("Yunsilicon XSC PCI driver");
--
2.43.0
^ permalink raw reply related [flat|nested] 33+ messages in thread
* [PATCH v2 02/11] net/xsc: add doorbell and cmdq MMIO accessors
2026-11-26 14:57 [PATCH v2 00/11] net/xsc: add the Yunsilicon XSC ethernet driver Xin Tian
2026-11-26 14:57 ` [PATCH v2 01/11] net/xsc: add Yunsilicon XSC ethernet PCI driver Xin Tian
@ 2026-11-26 14:57 ` Xin Tian
2026-11-27 15:57 ` netdev-bot+sashiko
2026-11-26 14:57 ` [PATCH v2 03/11] net/xsc: add the command queue Xin Tian
` (9 subsequent siblings)
11 siblings, 1 reply; 33+ messages in thread
From: Xin Tian @ 2026-11-26 14:57 UTC (permalink / raw)
To: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Jason Gunthorpe, Leon Romanovsky, Junxian Huang
Cc: weihg, linux-kernel, netdev, linux-rdma, kerneljasonxing
Provide doorbell writers and cmdq register accessors operating at
fixed offsets in the compressed-BAR register window.
Signed-off-by: Xin Tian <tianx@yunsilicon.com>
---
.../ethernet/yunsilicon/xsc/common/xsc_core.h | 15 ++
.../net/ethernet/yunsilicon/xsc/pci/Makefile | 2 +-
.../ethernet/yunsilicon/xsc/pci/doorbell.c | 148 ++++++++++++++++++
3 files changed, 164 insertions(+), 1 deletion(-)
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/doorbell.c
diff --git a/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
index 2d0eda8a2..9580d89a1 100644
--- a/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
+++ b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
@@ -32,4 +32,19 @@ struct xsc_core_device {
int bar_num;
};
+void xsc_update_tx_db(struct xsc_core_device *xdev, u32 sqn, u32 next_pid);
+void xsc_update_rx_db(struct xsc_core_device *xdev, u32 rqn, u32 next_pid);
+void xsc_arm_cq(struct xsc_core_device *xdev, u32 cqn, u32 next_cid, u8 solicited);
+void xsc_update_cq_ci(struct xsc_core_device *xdev, u32 cqn, u32 next_cid);
+void xsc_update_eq_ci(struct xsc_core_device *xdev, u32 eqn, u32 next_cid, u8 arm);
+void xsc_update_cmdq_req_pid(struct xsc_core_device *xdev, u32 req_pid);
+void xsc_update_cmdq_rsp_cid(struct xsc_core_device *xdev, u32 rsp_cid);
+u32 xsc_get_cmdq_req_pid(struct xsc_core_device *xdev);
+u32 xsc_get_cmdq_rsp_pid(struct xsc_core_device *xdev);
+u32 xsc_get_cmdq_rsp_cid(struct xsc_core_device *xdev);
+void xsc_set_cmdq_req_buf_addr(struct xsc_core_device *xdev, u32 haddr, u32 laddr);
+void xsc_set_cmdq_rsp_buf_addr(struct xsc_core_device *xdev, u32 haddr, u32 laddr);
+u32 xsc_read_cmdq_err_status(struct xsc_core_device *xdev);
+void xsc_clear_cmdq_err_status(struct xsc_core_device *xdev, u32 status);
+
#endif
diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/Makefile b/drivers/net/ethernet/yunsilicon/xsc/pci/Makefile
index c163aa950..4a08b92cd 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
+xsc_pci-y := main.o doorbell.o
diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/doorbell.c b/drivers/net/ethernet/yunsilicon/xsc/pci/doorbell.c
new file mode 100644
index 000000000..5bd64be67
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/pci/doorbell.c
@@ -0,0 +1,148 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2024 - 2026, Shanghai Yunsilicon Technology Co., Ltd.
+ * All rights reserved.
+ */
+
+#include <linux/kernel.h>
+#include <linux/pci.h>
+
+#include "common/xsc_core.h"
+#include "common/xsc_reg.h"
+
+/* doorbell words */
+#define XSC_TX_DB_DATA0_NEXT_PID_MASK GENMASK(15, 0)
+#define XSC_TX_DB_DATA0_QP_ID_MASK GENMASK(30, 16)
+#define XSC_RX_DB_DATA0_NEXT_PID_MASK GENMASK(12, 0)
+#define XSC_RX_DB_DATA0_QP_ID_MASK GENMASK(27, 13)
+#define XSC_CQ_DB_DATA0_NEXT_CID_MASK GENMASK(15, 0)
+#define XSC_CQ_DB_DATA0_CQ_ID_MASK GENMASK(30, 16)
+#define XSC_CQ_DB_DATA0_ARM BIT(31)
+#define XSC_EQ_DB_DATA0_NEXT_CID_MASK GENMASK(10, 0)
+#define XSC_EQ_DB_DATA0_EQ_ID_MASK GENMASK(21, 11)
+#define XSC_EQ_DB_DATA0_ARM BIT(22)
+
+static u32 xsc_read32(void __iomem *bar, u32 off)
+{
+ return readl(bar + off);
+}
+
+static void xsc_write32(void __iomem *bar, u32 off, u32 val)
+{
+ writel(val, bar + off);
+}
+
+void xsc_update_tx_db(struct xsc_core_device *xdev, u32 sqn, u32 next_pid)
+{
+ u32 db = FIELD_PREP(XSC_TX_DB_DATA0_NEXT_PID_MASK, next_pid) |
+ FIELD_PREP(XSC_TX_DB_DATA0_QP_ID_MASK, sqn);
+
+ /* Make sure that descriptors are written before
+ * updating doorbell record and ringing the doorbell
+ */
+ wmb();
+
+ writel(db, xdev->bar + TX_DB_FUNC_MEM_ADDR);
+}
+EXPORT_SYMBOL_GPL(xsc_update_tx_db);
+
+void xsc_update_rx_db(struct xsc_core_device *xdev, u32 rqn, u32 next_pid)
+{
+ u32 db = FIELD_PREP(XSC_RX_DB_DATA0_NEXT_PID_MASK, next_pid) |
+ FIELD_PREP(XSC_RX_DB_DATA0_QP_ID_MASK, rqn);
+
+ /* Make sure that descriptors are written before
+ * updating doorbell record and ringing the doorbell
+ */
+ wmb();
+
+ writel(db, xdev->bar + RX_DB_FUNC_MEM_ADDR);
+}
+EXPORT_SYMBOL_GPL(xsc_update_rx_db);
+
+void xsc_arm_cq(struct xsc_core_device *xdev, u32 cqn, u32 next_cid,
+ u8 solicited)
+{
+ u32 db = FIELD_PREP(XSC_CQ_DB_DATA0_NEXT_CID_MASK, next_cid) |
+ FIELD_PREP(XSC_CQ_DB_DATA0_CQ_ID_MASK, cqn) |
+ FIELD_PREP(XSC_CQ_DB_DATA0_ARM, !!solicited);
+
+ /* Make sure that the doorbell record in host memory is
+ * written before ringing the doorbell via PCI MMIO.
+ */
+ wmb();
+ writel(db, xdev->bar + DB_CQ_FUNC_MEM_ADDR);
+}
+EXPORT_SYMBOL_GPL(xsc_arm_cq);
+
+void xsc_update_cq_ci(struct xsc_core_device *xdev, u32 cqn, u32 next_cid)
+{
+ u32 db = FIELD_PREP(XSC_CQ_DB_DATA0_NEXT_CID_MASK, next_cid) |
+ FIELD_PREP(XSC_CQ_DB_DATA0_CQ_ID_MASK, cqn);
+
+ /* make sure val write to memory done */
+ wmb();
+ writel(db, xdev->bar + DB_CQ_CID_DIRECT_MEM_ADDR);
+}
+EXPORT_SYMBOL_GPL(xsc_update_cq_ci);
+
+void xsc_update_eq_ci(struct xsc_core_device *xdev, u32 eqn, u32 next_cid,
+ u8 arm)
+{
+ u32 db = FIELD_PREP(XSC_EQ_DB_DATA0_NEXT_CID_MASK, next_cid) |
+ FIELD_PREP(XSC_EQ_DB_DATA0_EQ_ID_MASK, eqn) |
+ FIELD_PREP(XSC_EQ_DB_DATA0_ARM, !!arm);
+
+ /* make sure val write to memory done */
+ wmb();
+ writel(db, xdev->bar + DB_EQ_FUNC_MEM_ADDR);
+}
+
+void xsc_update_cmdq_req_pid(struct xsc_core_device *xdev, u32 req_pid)
+{
+ xsc_write32(xdev->bar, CMDQM_HOST_REQ_PID_MEM_ADDR, req_pid);
+}
+
+void xsc_update_cmdq_rsp_cid(struct xsc_core_device *xdev, u32 rsp_cid)
+{
+ xsc_write32(xdev->bar, CMDQM_HOST_RSP_CID_MEM_ADDR, rsp_cid);
+}
+
+u32 xsc_get_cmdq_req_pid(struct xsc_core_device *xdev)
+{
+ return xsc_read32(xdev->bar, CMDQM_HOST_REQ_PID_MEM_ADDR);
+}
+
+u32 xsc_get_cmdq_rsp_pid(struct xsc_core_device *xdev)
+{
+ return xsc_read32(xdev->bar, CMDQM_HOST_RSP_PID_MEM_ADDR);
+}
+
+u32 xsc_get_cmdq_rsp_cid(struct xsc_core_device *xdev)
+{
+ return xsc_read32(xdev->bar, CMDQM_HOST_RSP_CID_MEM_ADDR);
+}
+
+void xsc_set_cmdq_req_buf_addr(struct xsc_core_device *xdev, u32 haddr,
+ u32 laddr)
+{
+ xsc_write32(xdev->bar, CMDQM_HOST_REQ_BUF_BASE_H_ADDR_MEM_ADDR, haddr);
+ xsc_write32(xdev->bar, CMDQM_HOST_REQ_BUF_BASE_L_ADDR_MEM_ADDR, laddr);
+}
+
+void xsc_set_cmdq_rsp_buf_addr(struct xsc_core_device *xdev, u32 haddr,
+ u32 laddr)
+{
+ xsc_write32(xdev->bar, CMDQM_HOST_RSP_BUF_BASE_H_ADDR_MEM_ADDR, haddr);
+ xsc_write32(xdev->bar, CMDQM_HOST_RSP_BUF_BASE_L_ADDR_MEM_ADDR, laddr);
+}
+
+u32 xsc_read_cmdq_err_status(struct xsc_core_device *xdev)
+{
+ return xsc_read32(xdev->bar, CMDQM_HOST_VF_ERR_STS_MEM_ADDR);
+}
+
+void xsc_clear_cmdq_err_status(struct xsc_core_device *xdev, u32 status)
+{
+ if (status)
+ xsc_write32(xdev->bar, CMDQM_HOST_VF_ERR_STS_MEM_ADDR, status);
+}
--
2.43.0
^ permalink raw reply related [flat|nested] 33+ messages in thread
* [PATCH v2 03/11] net/xsc: add the command queue
2026-11-26 14:57 [PATCH v2 00/11] net/xsc: add the Yunsilicon XSC ethernet driver Xin Tian
2026-11-26 14:57 ` [PATCH v2 01/11] net/xsc: add Yunsilicon XSC ethernet PCI driver Xin Tian
2026-11-26 14:57 ` [PATCH v2 02/11] net/xsc: add doorbell and cmdq MMIO accessors Xin Tian
@ 2026-11-26 14:57 ` Xin Tian
2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:57 ` netdev-bot+sashiko
2026-11-26 14:57 ` [PATCH v2 04/11] net/xsc: query HCA capabilities and reset function Xin Tian
` (8 subsequent siblings)
11 siblings, 2 replies; 33+ messages in thread
From: Xin Tian @ 2026-11-26 14:57 UTC (permalink / raw)
To: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Jason Gunthorpe, Leon Romanovsky, Junxian Huang
Cc: weihg, linux-kernel, netdev, linux-rdma, kerneljasonxing
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
^ permalink raw reply related [flat|nested] 33+ messages in thread
* [PATCH v2 04/11] net/xsc: query HCA capabilities and reset function
2026-11-26 14:57 [PATCH v2 00/11] net/xsc: add the Yunsilicon XSC ethernet driver Xin Tian
` (2 preceding siblings ...)
2026-11-26 14:57 ` [PATCH v2 03/11] net/xsc: add the command queue Xin Tian
@ 2026-11-26 14:57 ` Xin Tian
2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:57 ` netdev-bot+sashiko
2026-11-26 14:57 ` [PATCH v2 05/11] net/xsc: add event queues and MSI-X Xin Tian
` (7 subsequent siblings)
11 siblings, 2 replies; 33+ messages in thread
From: Xin Tian @ 2026-11-26 14:57 UTC (permalink / raw)
To: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Jason Gunthorpe, Leon Romanovsky, Junxian Huang
Cc: weihg, linux-kernel, netdev, linux-rdma, kerneljasonxing
resources
Query the firmware HCA capabilities needed by the ethernet driver and
reset the function resources during probe.
Signed-off-by: Xin Tian <tianx@yunsilicon.com>
---
.../ethernet/yunsilicon/xsc/common/xsc_core.h | 18 +++++
.../net/ethernet/yunsilicon/xsc/pci/main.c | 70 +++++++++++++++++++
2 files changed, 88 insertions(+)
diff --git a/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
index a6f0149ed..1ffbc2daa 100644
--- a/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
+++ b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
@@ -24,6 +24,18 @@
#define XSC_PAGE_SHIFT_4K 12
#define XSC_PAGE_SIZE_4K (_AC(1, UL) << XSC_PAGE_SHIFT_4K)
+struct xsc_caps {
+ u32 max_cqes;
+ u32 send_ds_num;
+ u32 recv_ds_num;
+ u8 log_max_qp_depth;
+ u16 msix_base;
+ u16 msix_num;
+ u32 nic_caps;
+};
+
+#define XSC_CAP_MAC_PORT_MASK GENMASK(15, 8)
+
struct xsc_cmd;
struct xsc_core_device {
@@ -33,6 +45,12 @@ struct xsc_core_device {
void __iomem *bar;
int bar_num;
+ u8 mac_port;
+ u8 pcie_no;
+ u8 pf_id;
+ u16 glb_func_id;
+
+ struct xsc_caps caps;
struct xsc_cmd *cmd;
};
diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/main.c b/drivers/net/ethernet/yunsilicon/xsc/pci/main.c
index 9b08580f5..42c5a1bae 100644
--- a/drivers/net/ethernet/yunsilicon/xsc/pci/main.c
+++ b/drivers/net/ethernet/yunsilicon/xsc/pci/main.c
@@ -29,6 +29,63 @@ static int xsc_set_dma_caps(struct pci_dev *pdev)
return err;
}
+static int xsc_cmd_query_hca_cap(struct xsc_core_device *xdev,
+ struct xsc_caps *caps)
+{
+ struct xsc_cmd_query_hca_cap_mbox_out *out;
+ struct xsc_cmd_query_hca_cap_mbox_in in;
+ int err;
+
+ out = kzalloc_obj(*out, GFP_KERNEL);
+ if (!out)
+ return -ENOMEM;
+
+ memset(&in, 0, sizeof(in));
+ in.hdr.opcode = cpu_to_be16(XSC_CMD_OP_QUERY_HCA_CAP);
+ in.cpu_num = cpu_to_be16(num_online_cpus());
+
+ err = xsc_cmd_exec(xdev, &in, sizeof(in), out, sizeof(*out));
+ if (err)
+ goto err_free;
+
+ xdev->glb_func_id = be32_to_cpu(out->hca_cap.glb_func_id);
+
+ caps->max_cqes = 1 << out->hca_cap.log_max_cq_sz;
+ caps->send_ds_num = out->hca_cap.send_seg_num;
+ caps->recv_ds_num = out->hca_cap.recv_seg_num;
+
+ caps->nic_caps = (caps->nic_caps & ~XSC_CAP_MAC_PORT_MASK) |
+ FIELD_PREP(XSC_CAP_MAC_PORT_MASK,
+ out->hca_cap.mac_port & 0xff);
+ xdev->mac_port = FIELD_GET(XSC_CAP_MAC_PORT_MASK, caps->nic_caps);
+
+ caps->msix_base = be16_to_cpu(out->hca_cap.msix_base);
+ caps->msix_num = be16_to_cpu(out->hca_cap.msix_num);
+
+ caps->log_max_qp_depth = out->hca_cap.log_max_qp_depth & 0xff;
+
+ xdev->pcie_no = out->hca_cap.pcie_no;
+ xdev->pf_id = out->hca_cap.pf_id;
+
+ kfree(out);
+ return 0;
+err_free:
+ kfree(out);
+ return err;
+}
+
+static int xsc_reset_function_resource(struct xsc_core_device *xdev)
+{
+ struct xsc_function_reset_mbox_out out;
+ struct xsc_function_reset_mbox_in in;
+
+ memset(&in, 0, sizeof(in));
+ in.hdr.opcode = cpu_to_be16(XSC_CMD_OP_FUNCTION_RESET);
+ in.glb_func_id = cpu_to_be16(xdev->glb_func_id);
+
+ return xsc_cmd_exec(xdev, &in, sizeof(in), &out, sizeof(out));
+}
+
static int xsc_pci_init(struct xsc_core_device *xdev,
const struct pci_device_id *id)
{
@@ -90,6 +147,19 @@ static int xsc_pci_init(struct xsc_core_device *xdev,
goto err_cmd_cleanup;
}
+ err = xsc_cmd_query_hca_cap(xdev, &xdev->caps);
+ if (err) {
+ pci_err(pdev, "failed to query hca caps: err=%d\n", err);
+ goto err_cmd_cleanup;
+ }
+
+ err = xsc_reset_function_resource(xdev);
+ if (err) {
+ pci_err(pdev, "failed to reset function resource: err=%d\n",
+ err);
+ goto err_cmd_cleanup;
+ }
+
return 0;
err_cmd_cleanup:
--
2.43.0
^ permalink raw reply related [flat|nested] 33+ messages in thread
* [PATCH v2 05/11] net/xsc: add event queues and MSI-X
2026-11-26 14:57 [PATCH v2 00/11] net/xsc: add the Yunsilicon XSC ethernet driver Xin Tian
` (3 preceding siblings ...)
2026-11-26 14:57 ` [PATCH v2 04/11] net/xsc: query HCA capabilities and reset function Xin Tian
@ 2026-11-26 14:57 ` Xin Tian
2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:57 ` netdev-bot+sashiko
2026-11-26 14:57 ` [PATCH v2 06/11] net/xsc: add CQ and QP helpers Xin Tian
` (6 subsequent siblings)
11 siblings, 2 replies; 33+ messages in thread
From: Xin Tian @ 2026-11-26 14:57 UTC (permalink / raw)
To: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Jason Gunthorpe, Leon Romanovsky, Junxian Huang
Cc: weihg, linux-kernel, netdev, linux-rdma, kerneljasonxing
Create the asynchronous and completion event queues, allocate MSI-X
vectors, and dispatch link events from a workqueue. The command queue
remains polling-only in this series.
Signed-off-by: Xin Tian <tianx@yunsilicon.com>
---
.../ethernet/yunsilicon/xsc/common/xsc_core.h | 77 ++++
.../net/ethernet/yunsilicon/xsc/pci/Makefile | 2 +-
.../net/ethernet/yunsilicon/xsc/pci/alloc.c | 123 +++++++
.../net/ethernet/yunsilicon/xsc/pci/alloc.h | 18 +
drivers/net/ethernet/yunsilicon/xsc/pci/eq.c | 217 +++++++++++
drivers/net/ethernet/yunsilicon/xsc/pci/eq.h | 38 ++
.../net/ethernet/yunsilicon/xsc/pci/main.c | 9 +
.../net/ethernet/yunsilicon/xsc/pci/pci_irq.c | 345 ++++++++++++++++++
.../net/ethernet/yunsilicon/xsc/pci/pci_irq.h | 16 +
9 files changed, 844 insertions(+), 1 deletion(-)
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/alloc.c
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/alloc.h
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/eq.c
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/eq.h
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/pci_irq.c
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/pci_irq.h
diff --git a/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
index 1ffbc2daa..212cb2f9c 100644
--- a/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
+++ b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
@@ -8,6 +8,9 @@
#include <linux/pci.h>
#include <linux/bitfield.h>
+#include <linux/interrupt.h>
+#include <linux/list.h>
+#include <linux/workqueue.h>
#define XSC_PCI_VENDOR_ID 0x1f67
@@ -36,12 +39,62 @@ struct xsc_caps {
#define XSC_CAP_MAC_PORT_MASK GENMASK(15, 8)
+enum {
+ XSC_MAX_IRQ_NAME = 32
+};
+
+/* alloc */
+struct xsc_buf_list {
+ void *buf;
+ dma_addr_t map;
+};
+
+struct xsc_buf {
+ struct xsc_buf_list direct;
+ struct xsc_buf_list *page_list;
+ unsigned long nbufs;
+ unsigned long npages;
+ unsigned long page_shift;
+ unsigned long size;
+};
+
+/* eq */
+struct xsc_eq {
+ struct xsc_core_device *dev;
+ u32 cons_index;
+ struct xsc_buf buf;
+ unsigned int irqn;
+ u32 eqn;
+ u32 nent;
+ struct list_head list;
+ u16 index;
+};
+
+struct xsc_eq_table {
+ struct list_head comp_eqs_list;
+ struct xsc_eq async_eq;
+ int num_comp_vectors;
+ int eq_vec_comp_base;
+ /* protect EQs list */
+ spinlock_t lock;
+};
+
+/* irq */
+struct xsc_irq_info {
+ char name[XSC_MAX_IRQ_NAME];
+};
+
struct xsc_cmd;
struct xsc_core_device {
struct pci_dev *pdev;
struct device *device;
+ /* async event plumbing, owned by the PCI layer */
+ struct work_struct event_work;
+ struct workqueue_struct *event_wq;
+ void (*link_event_handler)(void *xdev);
+
void __iomem *bar;
int bar_num;
@@ -49,9 +102,12 @@ struct xsc_core_device {
u8 pcie_no;
u8 pf_id;
u16 glb_func_id;
+ u16 msix_vec_base;
struct xsc_caps caps;
struct xsc_cmd *cmd;
+ struct xsc_eq_table eq_table;
+ struct xsc_irq_info *irq_info;
};
void xsc_update_tx_db(struct xsc_core_device *xdev, u32 sqn, u32 next_pid);
@@ -69,4 +125,25 @@ void xsc_set_cmdq_rsp_buf_addr(struct xsc_core_device *xdev, u32 haddr, u32 ladd
u32 xsc_read_cmdq_err_status(struct xsc_core_device *xdev);
void xsc_clear_cmdq_err_status(struct xsc_core_device *xdev, u32 status);
+int xsc_core_vector2eqn(struct xsc_core_device *xdev, int vector, u32 *eqn,
+ unsigned int *irqn);
+
+/*
+ * Link event callback registration. The events are dispatched from the PCI
+ * layer's own workqueue; unregister detaches the callback and waits for one
+ * already in flight, so it must precede freeing what the handler touches.
+ */
+void xsc_irq_event_register(struct xsc_core_device *xdev,
+ void (*handler)(void *xdev));
+void xsc_irq_event_unregister(struct xsc_core_device *xdev);
+
+static inline void *xsc_buf_offset(struct xsc_buf *buf, unsigned long offset)
+{
+ if (likely(buf->nbufs == 1))
+ return buf->direct.buf + offset;
+ else
+ return buf->page_list[offset >> PAGE_SHIFT].buf +
+ (offset & (PAGE_SIZE - 1));
+}
+
#endif
diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/Makefile b/drivers/net/ethernet/yunsilicon/xsc/pci/Makefile
index e17a10cc1..7a32e0320 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 cmd.o doorbell.o
+xsc_pci-y := main.o cmd.o eq.o pci_irq.o alloc.o doorbell.o
diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/alloc.c b/drivers/net/ethernet/yunsilicon/xsc/pci/alloc.c
new file mode 100644
index 000000000..46eb0b031
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/pci/alloc.c
@@ -0,0 +1,123 @@
+// 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/dma-mapping.h>
+#include <linux/mm.h>
+#include <linux/slab.h>
+#include <linux/vmalloc.h>
+
+#include "alloc.h"
+
+int xsc_buf_alloc(struct xsc_core_device *xdev,
+ unsigned long size,
+ unsigned long max_direct,
+ struct xsc_buf *buf)
+{
+ dma_addr_t t;
+
+ buf->size = size;
+ if (size <= max_direct) {
+ buf->nbufs = 1;
+ buf->npages = 1;
+ buf->page_shift = get_order(size) + PAGE_SHIFT;
+ buf->direct.buf = dma_alloc_coherent(&xdev->pdev->dev, size, &t,
+ GFP_KERNEL);
+ if (!buf->direct.buf)
+ return -ENOMEM;
+
+ buf->direct.map = t;
+
+ while (t & GENMASK(buf->page_shift - 1, 0)) {
+ --buf->page_shift;
+ buf->npages *= 2;
+ }
+ } else {
+ struct page **pages;
+ unsigned int i;
+
+ buf->direct.buf = NULL;
+ buf->nbufs = DIV_ROUND_UP(size, PAGE_SIZE);
+ buf->npages = buf->nbufs;
+ buf->page_shift = PAGE_SHIFT;
+ buf->page_list = kcalloc(buf->nbufs, sizeof(*buf->page_list),
+ GFP_KERNEL);
+ if (!buf->page_list)
+ return -ENOMEM;
+
+ for (i = 0; i < buf->nbufs; i++) {
+ buf->page_list[i].buf =
+ dma_alloc_coherent(&xdev->pdev->dev, PAGE_SIZE,
+ &t, GFP_KERNEL);
+ if (!buf->page_list[i].buf)
+ goto err_free;
+
+ buf->page_list[i].map = t;
+ }
+
+ pages = kmalloc_array(buf->nbufs, sizeof(*pages), GFP_KERNEL);
+ if (!pages)
+ goto err_free;
+
+ for (i = 0; i < buf->nbufs; i++) {
+ void *addr = buf->page_list[i].buf;
+
+ if (is_vmalloc_addr(addr))
+ pages[i] = vmalloc_to_page(addr);
+ else
+ pages[i] = virt_to_page(addr);
+ }
+ buf->direct.buf = vmap(pages, buf->nbufs, VM_MAP, PAGE_KERNEL);
+ kfree(pages);
+ if (!buf->direct.buf)
+ goto err_free;
+ }
+
+ return 0;
+
+err_free:
+ xsc_buf_free(xdev, buf);
+
+ return -ENOMEM;
+}
+
+void xsc_buf_free(struct xsc_core_device *xdev, struct xsc_buf *buf)
+{
+ unsigned int i;
+
+ if (buf->nbufs == 1) {
+ dma_free_coherent(&xdev->pdev->dev, buf->size, buf->direct.buf,
+ buf->direct.map);
+ } else {
+ if (buf->direct.buf)
+ vunmap(buf->direct.buf);
+
+ for (i = 0; i < buf->nbufs; i++)
+ if (buf->page_list[i].buf)
+ dma_free_coherent(&xdev->pdev->dev, PAGE_SIZE,
+ buf->page_list[i].buf,
+ buf->page_list[i].map);
+ kfree(buf->page_list);
+ }
+}
+
+void xsc_fill_page_array(struct xsc_buf *buf, __be64 *pas, unsigned int npages)
+{
+ unsigned int shift = PAGE_SHIFT - XSC_PAGE_SHIFT_4K;
+ unsigned int mask = (1 << shift) - 1;
+ unsigned int i;
+ u64 addr;
+
+ for (i = 0; i < npages; i++) {
+ if (buf->nbufs == 1)
+ addr = buf->direct.map + (i << XSC_PAGE_SHIFT_4K);
+ else
+ addr = buf->page_list[i >> shift].map +
+ ((i & mask) << XSC_PAGE_SHIFT_4K);
+
+ pas[i] = cpu_to_be64(addr);
+ }
+}
diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/alloc.h b/drivers/net/ethernet/yunsilicon/xsc/pci/alloc.h
new file mode 100644
index 000000000..c8d65e2fa
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/pci/alloc.h
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
+ * All rights reserved.
+ */
+
+#ifndef __XSC_ALLOC_H
+#define __XSC_ALLOC_H
+
+#include "common/xsc_core.h"
+
+int xsc_buf_alloc(struct xsc_core_device *xdev,
+ unsigned long size,
+ unsigned long max_direct,
+ struct xsc_buf *buf);
+void xsc_buf_free(struct xsc_core_device *xdev, struct xsc_buf *buf);
+void xsc_fill_page_array(struct xsc_buf *buf, __be64 *pas, unsigned int npages);
+
+#endif
diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/eq.c b/drivers/net/ethernet/yunsilicon/xsc/pci/eq.c
new file mode 100644
index 000000000..875a277a8
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/pci/eq.c
@@ -0,0 +1,217 @@
+// 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/interrupt.h>
+#include <linux/mm.h>
+#include <linux/slab.h>
+
+#include "common/xsc_cmd.h"
+#include "common/xsc_core.h"
+#include "alloc.h"
+#include "eq.h"
+
+enum {
+ XSC_EQE_SIZE = sizeof(struct xsc_eqe),
+};
+
+enum {
+ XSC_NUM_SPARE_EQE = 0x80,
+ XSC_NUM_ASYNC_EQE = 0x100,
+};
+
+static int xsc_cmd_destroy_eq(struct xsc_core_device *xdev, u32 eqn)
+{
+ struct xsc_destroy_eq_mbox_out out = {};
+ struct xsc_destroy_eq_mbox_in in = {};
+
+ in.hdr.opcode = cpu_to_be16(XSC_CMD_OP_DESTROY_EQ);
+ in.eqn = cpu_to_be32(eqn);
+
+ return xsc_cmd_exec(xdev, &in, sizeof(in), &out, sizeof(out));
+}
+
+static struct xsc_eqe *get_eqe(struct xsc_eq *eq, u32 entry)
+{
+ return xsc_buf_offset(&eq->buf, entry * XSC_EQE_SIZE);
+}
+
+static struct xsc_eqe *next_eqe_sw(struct xsc_eq *eq)
+{
+ struct xsc_eqe *eqe = get_eqe(eq, eq->cons_index & (eq->nent - 1));
+
+ return (!!(eqe->owner_data & XSC_EQE_OWNER) ^
+ !!(eq->cons_index & eq->nent)) ? NULL : eqe;
+}
+
+static void eq_update_ci(struct xsc_eq *eq, int arm)
+{
+ xsc_update_eq_ci(eq->dev, eq->eqn, eq->cons_index, arm);
+}
+
+static int xsc_eq_int(struct xsc_eq *eq)
+{
+ struct xsc_eqe *eqe;
+ int eqes_found = 0;
+ int set_ci = 0;
+
+ while ((eqe = next_eqe_sw(eq))) {
+ /* Make sure we read EQ entry contents after we've
+ * checked the ownership bit.
+ */
+ rmb();
+
+ ++eq->cons_index;
+ eqes_found = 1;
+ ++set_ci;
+
+ if (unlikely(set_ci >= XSC_NUM_SPARE_EQE)) {
+ eq_update_ci(eq, 0);
+ set_ci = 0;
+ }
+ }
+
+ eq_update_ci(eq, 1);
+
+ return eqes_found;
+}
+
+static irqreturn_t xsc_msix_handler(int irq, void *eq_ptr)
+{
+ struct xsc_eq *eq = eq_ptr;
+
+ xsc_eq_int(eq);
+
+ /* MSI-X vectors always belong to us */
+ return IRQ_HANDLED;
+}
+
+static void init_eq_buf(struct xsc_eq *eq)
+{
+ struct xsc_eqe *eqe;
+ int i;
+
+ for (i = 0; i < eq->nent; i++) {
+ eqe = get_eqe(eq, i);
+ eqe->owner_data |= XSC_EQE_OWNER;
+ }
+}
+
+int xsc_create_map_eq(struct xsc_core_device *xdev,
+ struct xsc_eq *eq, u16 vecidx,
+ u32 nent, const char *name)
+{
+ u16 msix_vec_offset = xdev->msix_vec_base + vecidx;
+ struct xsc_create_eq_mbox_out out;
+ struct xsc_create_eq_mbox_in *in;
+ unsigned int hw_npages;
+ u32 inlen;
+ int err;
+
+ eq->nent = roundup_pow_of_two(nent + XSC_NUM_SPARE_EQE);
+ err = xsc_buf_alloc(xdev, eq->nent * XSC_EQE_SIZE, PAGE_SIZE, &eq->buf);
+ if (err)
+ return err;
+
+ init_eq_buf(eq);
+
+ hw_npages = DIV_ROUND_UP(eq->nent * XSC_EQE_SIZE, XSC_PAGE_SIZE_4K);
+ inlen = sizeof(*in) + sizeof(in->pas[0]) * hw_npages;
+ in = kvzalloc(inlen, GFP_KERNEL);
+ if (!in) {
+ err = -ENOMEM;
+ goto err_free_eq_buf;
+ }
+
+ xsc_fill_page_array(&eq->buf, in->pas, hw_npages);
+
+ in->hdr.opcode = cpu_to_be16(XSC_CMD_OP_CREATE_EQ);
+ in->ctx.log_eq_sz = ilog2(eq->nent);
+ in->ctx.vecidx = cpu_to_be16(msix_vec_offset);
+ in->ctx.pa_num = cpu_to_be16(hw_npages);
+ in->ctx.glb_func_id = cpu_to_be16(xdev->glb_func_id);
+ in->ctx.is_async_eq = (vecidx == XSC_EQ_VEC_ASYNC ? 1 : 0);
+
+ err = xsc_cmd_exec(xdev, in, inlen, &out, sizeof(out));
+ if (err)
+ goto err_free_in;
+
+ snprintf(xdev->irq_info[vecidx].name, XSC_MAX_IRQ_NAME, "%s@pci:%s",
+ name, pci_name(xdev->pdev));
+
+ eq->eqn = be32_to_cpu(out.eqn);
+ eq->irqn = pci_irq_vector(xdev->pdev, vecidx);
+ eq->dev = xdev;
+ eq->index = vecidx;
+
+ err = request_irq(eq->irqn, xsc_msix_handler, 0,
+ xdev->irq_info[vecidx].name, eq);
+ if (err)
+ goto err_destroy_eq;
+
+ /* EQs are created in ARMED state
+ */
+ eq_update_ci(eq, 1);
+ kvfree(in);
+ return 0;
+
+err_destroy_eq:
+ /* CREATE_EQ already reached the firmware; if the rollback DESTROY_EQ
+ * fails the EQ is still armed and DMAing, so leak its buffer rather
+ * than free pages the device can still write to.
+ */
+ if (xsc_cmd_destroy_eq(xdev, eq->eqn)) {
+ kvfree(in);
+ return err;
+ }
+
+err_free_in:
+ kvfree(in);
+
+err_free_eq_buf:
+ xsc_buf_free(xdev, &eq->buf);
+ return err;
+}
+
+int xsc_destroy_unmap_eq(struct xsc_core_device *xdev, struct xsc_eq *eq)
+{
+ int err;
+
+ free_irq(eq->irqn, eq);
+ err = xsc_cmd_destroy_eq(xdev, eq->eqn);
+ if (err) {
+ pci_err(xdev->pdev, "failed to destroy a previously created eq: eqn %d\n",
+ eq->eqn);
+ /* DESTROY_EQ failed: the EQ is still armed in the firmware and
+ * keeps DMAing events into this buffer, so leak it instead of
+ * handing the pages back while the device can still write them.
+ */
+ return err;
+ }
+ xsc_buf_free(xdev, &eq->buf);
+
+ return 0;
+}
+
+int xsc_start_eqs(struct xsc_core_device *xdev)
+{
+ struct xsc_eq_table *table = &xdev->eq_table;
+ int err;
+
+ err = xsc_create_map_eq(xdev, &table->async_eq, XSC_EQ_VEC_ASYNC,
+ XSC_NUM_ASYNC_EQE, "xsc_async_eq");
+ if (err)
+ pci_err(xdev->pdev, "failed to create async EQ %d\n", err);
+
+ return err;
+}
+
+void xsc_stop_eqs(struct xsc_core_device *xdev)
+{
+ struct xsc_eq_table *table = &xdev->eq_table;
+
+ xsc_destroy_unmap_eq(xdev, &table->async_eq);
+}
diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/eq.h b/drivers/net/ethernet/yunsilicon/xsc/pci/eq.h
new file mode 100644
index 000000000..e8c4741cf
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/pci/eq.h
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
+ * All rights reserved.
+ */
+
+#ifndef __EQ_H
+#define __EQ_H
+
+#include "common/xsc_core.h"
+
+enum {
+ XSC_EQ_VEC_ASYNC = 0,
+ XSC_VEC_CMD = 1,
+ XSC_VEC_CMD_EVENT = 2,
+ XSC_DMA_READ_DONE_VEC = 3,
+ XSC_EQ_VEC_COMP_BASE,
+};
+
+struct xsc_eqe {
+ u8 type;
+ u8 sub_type;
+ __le16 queue_id_data;
+#define XSC_EQE_QUEUE_ID_MASK GENMASK(14, 0)
+
+ u8 err_code;
+ u8 rsvd[2];
+ u8 owner_data;
+#define XSC_EQE_OWNER BIT(7)
+};
+
+int xsc_create_map_eq(struct xsc_core_device *xdev,
+ struct xsc_eq *eq, u16 vecidx,
+ u32 nent, const char *name);
+int xsc_destroy_unmap_eq(struct xsc_core_device *xdev, struct xsc_eq *eq);
+int xsc_start_eqs(struct xsc_core_device *xdev);
+void xsc_stop_eqs(struct xsc_core_device *xdev);
+
+#endif
diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/main.c b/drivers/net/ethernet/yunsilicon/xsc/pci/main.c
index 42c5a1bae..315f212c1 100644
--- a/drivers/net/ethernet/yunsilicon/xsc/pci/main.c
+++ b/drivers/net/ethernet/yunsilicon/xsc/pci/main.c
@@ -7,6 +7,8 @@
#include "common/xsc_cmd.h"
#include "common/xsc_core.h"
+#include "eq.h"
+#include "pci_irq.h"
static const struct pci_device_id xsc_pci_id_table[] = {
{ PCI_DEVICE(XSC_PCI_VENDOR_ID, XSC_MC_PF_DEV_ID) },
@@ -160,6 +162,12 @@ static int xsc_pci_init(struct xsc_core_device *xdev,
goto err_cmd_cleanup;
}
+ err = xsc_irq_eq_create(xdev);
+ if (err) {
+ pci_err(pdev, "failed to create irq/eq: err=%d\n", err);
+ goto err_cmd_cleanup;
+ }
+
return 0;
err_cmd_cleanup:
@@ -179,6 +187,7 @@ static void xsc_pci_fini(struct xsc_core_device *xdev)
{
struct pci_dev *pdev = xdev->pdev;
+ xsc_irq_eq_destroy(xdev);
xsc_cmd_cleanup(xdev);
pci_iounmap(pdev, xdev->bar);
pci_clear_master(pdev);
diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/pci_irq.c b/drivers/net/ethernet/yunsilicon/xsc/pci/pci_irq.c
new file mode 100644
index 000000000..accc3e56b
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/pci/pci_irq.c
@@ -0,0 +1,345 @@
+// 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/interrupt.h>
+#include <linux/irqdomain.h>
+#include <linux/module.h>
+#include <linux/msi.h>
+#include <linux/slab.h>
+
+#include "common/xsc_cmd.h"
+#include "common/xsc_core.h"
+#include "eq.h"
+#include "pci_irq.h"
+
+enum {
+ XSC_COMP_EQ_SIZE = 1024,
+};
+
+static void xsc_free_irq(struct xsc_core_device *xdev, unsigned int vector)
+{
+ unsigned int irqn = pci_irq_vector(xdev->pdev, vector);
+
+ disable_irq(irqn);
+ free_irq(irqn, xdev);
+}
+
+static int xsc_alloc_irq_vectors(struct xsc_core_device *xdev)
+{
+ struct xsc_eq_table *table = &xdev->eq_table;
+ int nvec = xdev->caps.msix_num;
+ int nvec_base;
+ int err;
+
+ nvec_base = XSC_EQ_VEC_COMP_BASE;
+ if (nvec <= nvec_base) {
+ pci_err(xdev->pdev, "failed to alloc irq vector(%d)\n", nvec);
+ return -ENOMEM;
+ }
+
+ xdev->irq_info = kcalloc(nvec, sizeof(*xdev->irq_info), GFP_KERNEL);
+ if (!xdev->irq_info)
+ return -ENOMEM;
+
+ nvec = pci_alloc_irq_vectors(xdev->pdev, nvec_base + 1, nvec,
+ PCI_IRQ_MSIX);
+ if (nvec < 0) {
+ err = nvec;
+ goto err_free_irq_info;
+ }
+
+ table->eq_vec_comp_base = nvec_base;
+ table->num_comp_vectors = nvec - nvec_base;
+ /*
+ * caps.msix_base is the start of this function's slice in the
+ * firmware's own vector numbering space. EQ setup reports
+ * msix_vec_base + vecidx to the firmware, while the host IRQ is
+ * requested on MSI-X table entry vecidx; the firmware translates
+ * back to the host table entry.
+ */
+ xdev->msix_vec_base = xdev->caps.msix_base;
+
+ return 0;
+
+err_free_irq_info:
+ kfree(xdev->irq_info);
+ xdev->irq_info = NULL;
+ return err;
+}
+
+static void xsc_free_irq_vectors(struct xsc_core_device *xdev)
+{
+ pci_free_irq_vectors(xdev->pdev);
+ kfree(xdev->irq_info);
+ xdev->irq_info = NULL;
+}
+
+int xsc_core_vector2eqn(struct xsc_core_device *xdev, int vector, u32 *eqn,
+ unsigned int *irqn)
+{
+ struct xsc_eq_table *table = &xdev->eq_table;
+ struct xsc_eq *eq, *n;
+ int err = -ENOENT;
+
+ spin_lock(&table->lock);
+ list_for_each_entry_safe(eq, n, &table->comp_eqs_list, list) {
+ if (eq->index == vector) {
+ *eqn = eq->eqn;
+ *irqn = eq->irqn;
+ err = 0;
+ break;
+ }
+ }
+ spin_unlock(&table->lock);
+
+ return err;
+}
+EXPORT_SYMBOL_GPL(xsc_core_vector2eqn);
+
+static void free_comp_eqs(struct xsc_core_device *xdev)
+{
+ struct xsc_eq_table *table = &xdev->eq_table;
+ struct xsc_eq *eq, *n;
+
+ spin_lock(&table->lock);
+ list_for_each_entry_safe(eq, n, &table->comp_eqs_list, list) {
+ list_del(&eq->list);
+ spin_unlock(&table->lock);
+ if (xsc_destroy_unmap_eq(xdev, eq))
+ pci_err(xdev->pdev, "failed to destroy EQ 0x%x\n",
+ eq->eqn);
+ kfree(eq);
+ spin_lock(&table->lock);
+ }
+ spin_unlock(&table->lock);
+}
+
+static int alloc_comp_eqs(struct xsc_core_device *xdev)
+{
+ struct xsc_eq_table *table = &xdev->eq_table;
+ char name[XSC_MAX_IRQ_NAME];
+ struct xsc_eq *eq;
+ int ncomp_vec;
+ u32 nent;
+ int err;
+ int i;
+
+ INIT_LIST_HEAD(&table->comp_eqs_list);
+ ncomp_vec = table->num_comp_vectors;
+ nent = XSC_COMP_EQ_SIZE;
+
+ for (i = 0; i < ncomp_vec; i++) {
+ eq = kzalloc_obj(*eq, GFP_KERNEL);
+ if (!eq) {
+ err = -ENOMEM;
+ goto err_free;
+ }
+
+ snprintf(name, XSC_MAX_IRQ_NAME, "xsc_comp%d", i);
+ err = xsc_create_map_eq(xdev, eq,
+ i + table->eq_vec_comp_base,
+ nent, name);
+ if (err) {
+ kfree(eq);
+ goto err_free;
+ }
+
+ eq->index = i;
+ spin_lock(&table->lock);
+ list_add_tail(&eq->list, &table->comp_eqs_list);
+ spin_unlock(&table->lock);
+ }
+
+ return 0;
+
+err_free:
+ free_comp_eqs(xdev);
+ return err;
+}
+
+/*
+ * Handle an asynchronous event. This runs on the PCI layer's own workqueue,
+ * so it outlives any auxiliary driver: the driver only ever registers a
+ * callback here, and the memory this touches belongs to xsc_core_device
+ * rather than to the eth private area.
+ */
+static void xsc_event_work(struct work_struct *work)
+{
+ struct xsc_core_device *xdev = container_of(work, struct xsc_core_device,
+ event_work);
+ struct xsc_event_query_type_mbox_out out = {};
+ struct xsc_event_query_type_mbox_in in = {};
+ u8 event;
+ int err;
+
+ in.hdr.opcode = cpu_to_be16(XSC_CMD_OP_QUERY_EVENT_TYPE);
+
+ err = xsc_cmd_exec(xdev, &in, sizeof(in), &out, sizeof(out));
+ if (err) {
+ pci_err(xdev->pdev, "failed to query event type, err=%d\n", err);
+ return;
+ }
+
+ event = out.ctx.resp_cmd_type;
+ while (event) {
+ if (event & XSC_CMD_EVENT_RESP_CHANGE_LINK) {
+ void (*handler)(void *xdev);
+
+ handler = READ_ONCE(xdev->link_event_handler);
+ if (handler)
+ handler(xdev);
+ event &= ~XSC_CMD_EVENT_RESP_CHANGE_LINK;
+ } else {
+ pci_dbg(xdev->pdev, "unknown event cmdtype=0x%x\n",
+ out.ctx.resp_cmd_type);
+ event = 0;
+ }
+ }
+}
+
+static irqreturn_t xsc_event_handler(int irq, void *arg)
+{
+ struct xsc_core_device *xdev = arg;
+
+ queue_work(xdev->event_wq, &xdev->event_work);
+
+ return IRQ_HANDLED;
+}
+
+/*
+ * Register the link event callback.
+ *
+ * The events are dispatched from the PCI layer's own workqueue, so the
+ * client must not touch xdev->event_wq itself.
+ */
+void xsc_irq_event_register(struct xsc_core_device *xdev,
+ void (*handler)(void *xdev))
+{
+ WRITE_ONCE(xdev->link_event_handler, handler);
+}
+EXPORT_SYMBOL_GPL(xsc_irq_event_register);
+
+/*
+ * Detach the callback and wait for an event that is already in flight to
+ * finish with it, in the same way free_irq() both unregisters and waits.
+ * Must be called before the caller frees whatever its handler dereferences.
+ * Safe to call when no handler is registered.
+ */
+void xsc_irq_event_unregister(struct xsc_core_device *xdev)
+{
+ WRITE_ONCE(xdev->link_event_handler, NULL);
+
+ flush_workqueue(xdev->event_wq);
+}
+EXPORT_SYMBOL_GPL(xsc_irq_event_unregister);
+
+static int xsc_request_irq_for_event(struct xsc_core_device *xdev)
+{
+ snprintf(xdev->irq_info[XSC_VEC_CMD_EVENT].name, XSC_MAX_IRQ_NAME,
+ "%s@pci:%s", "xsc_eth_event", pci_name(xdev->pdev));
+ return request_irq(pci_irq_vector(xdev->pdev, XSC_VEC_CMD_EVENT),
+ xsc_event_handler, 0,
+ xdev->irq_info[XSC_VEC_CMD_EVENT].name, xdev);
+}
+
+static void xsc_free_irq_for_event(struct xsc_core_device *xdev)
+{
+ xsc_free_irq(xdev, XSC_VEC_CMD_EVENT);
+}
+
+static int xsc_cmd_enable_msix(struct xsc_core_device *xdev)
+{
+ struct xsc_msix_table_info_mbox_out out = {};
+ struct xsc_msix_table_info_mbox_in in = {};
+ int err;
+
+ in.hdr.opcode = cpu_to_be16(XSC_CMD_OP_ENABLE_MSIX);
+
+ err = xsc_cmd_exec(xdev, &in, sizeof(in), &out, sizeof(out));
+ if (err) {
+ pci_err(xdev->pdev, "xsc_cmd_exec enable msix failed %d\n",
+ err);
+ return err;
+ }
+
+ return 0;
+}
+
+int xsc_irq_eq_create(struct xsc_core_device *xdev)
+{
+ int err;
+
+ spin_lock_init(&xdev->eq_table.lock);
+
+ err = xsc_alloc_irq_vectors(xdev);
+ if (err) {
+ pci_err(xdev->pdev, "enable msix failed, err=%d\n", err);
+ goto err_out;
+ }
+
+ err = xsc_start_eqs(xdev);
+ if (err) {
+ pci_err(xdev->pdev, "failed to start EQs, err=%d\n", err);
+ goto err_free_irq_vectors;
+ }
+
+ err = alloc_comp_eqs(xdev);
+ if (err) {
+ pci_err(xdev->pdev, "failed to alloc comp EQs, err=%d\n", err);
+ goto err_stop_eqs;
+ }
+
+ xdev->event_wq = create_singlethread_workqueue("xsc_event");
+ if (!xdev->event_wq) {
+ pci_err(xdev->pdev, "failed to create event workqueue\n");
+ err = -ENOMEM;
+ goto err_free_comp_eqs;
+ }
+ INIT_WORK(&xdev->event_work, xsc_event_work);
+
+ err = xsc_request_irq_for_event(xdev);
+ if (err) {
+ pci_err(xdev->pdev, "failed to request irq for event, err=%d\n",
+ err);
+ goto err_destroy_event_wq;
+ }
+
+ err = xsc_cmd_enable_msix(xdev);
+ if (err) {
+ pci_err(xdev->pdev, "xsc_cmd_enable_msix failed %d.\n", err);
+ goto err_free_irq_event;
+ }
+ return 0;
+
+err_free_irq_event:
+ xsc_free_irq_for_event(xdev);
+err_destroy_event_wq:
+ destroy_workqueue(xdev->event_wq);
+ xdev->event_wq = NULL;
+err_free_comp_eqs:
+ free_comp_eqs(xdev);
+err_stop_eqs:
+ xsc_stop_eqs(xdev);
+err_free_irq_vectors:
+ xsc_free_irq_vectors(xdev);
+err_out:
+ return err;
+}
+
+void xsc_irq_eq_destroy(struct xsc_core_device *xdev)
+{
+ xsc_stop_eqs(xdev);
+ free_comp_eqs(xdev);
+
+ /* xsc_free_irq() disables and synchronises the event IRQ, so no new
+ * work can be queued once this returns.
+ */
+ xsc_free_irq_for_event(xdev);
+ destroy_workqueue(xdev->event_wq);
+ xdev->event_wq = NULL;
+ xsc_free_irq_vectors(xdev);
+}
diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/pci_irq.h b/drivers/net/ethernet/yunsilicon/xsc/pci/pci_irq.h
new file mode 100644
index 000000000..293269b6c
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/pci/pci_irq.h
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
+ * All rights reserved.
+ */
+
+#ifndef __XSC_PCI_IRQ_H
+#define __XSC_PCI_IRQ_H
+
+#include "common/xsc_core.h"
+
+int xsc_irq_eq_create(struct xsc_core_device *xdev);
+void xsc_irq_eq_destroy(struct xsc_core_device *xdev);
+int xsc_core_vector2eqn(struct xsc_core_device *xdev, int vector, u32 *eqn,
+ unsigned int *irqn);
+
+#endif
--
2.43.0
^ permalink raw reply related [flat|nested] 33+ messages in thread
* [PATCH v2 06/11] net/xsc: add CQ and QP helpers
2026-11-26 14:57 [PATCH v2 00/11] net/xsc: add the Yunsilicon XSC ethernet driver Xin Tian
` (4 preceding siblings ...)
2026-11-26 14:57 ` [PATCH v2 05/11] net/xsc: add event queues and MSI-X Xin Tian
@ 2026-11-26 14:57 ` Xin Tian
2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:57 ` netdev-bot+sashiko
2026-11-26 14:57 ` [PATCH v2 07/11] net/xsc: add the ethernet netdev Xin Tian
` (5 subsequent siblings)
11 siblings, 2 replies; 33+ messages in thread
From: Xin Tian @ 2026-11-26 14:57 UTC (permalink / raw)
To: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Jason Gunthorpe, Leon Romanovsky, Junxian Huang
Cc: weihg, linux-kernel, netdev, linux-rdma, kerneljasonxing
Provide the CQ/QP tables and the ethernet create/destroy/modify
helpers. Dispatch completion and error events from the event queue.
Signed-off-by: Xin Tian <tianx@yunsilicon.com>
---
.../ethernet/yunsilicon/xsc/common/xsc_core.h | 69 +++++++
.../net/ethernet/yunsilicon/xsc/pci/Makefile | 2 +-
drivers/net/ethernet/yunsilicon/xsc/pci/cq.c | 130 +++++++++++++
drivers/net/ethernet/yunsilicon/xsc/pci/cq.h | 13 ++
drivers/net/ethernet/yunsilicon/xsc/pci/eq.c | 83 ++++++++
.../net/ethernet/yunsilicon/xsc/pci/main.c | 5 +
drivers/net/ethernet/yunsilicon/xsc/pci/qp.c | 180 ++++++++++++++++++
drivers/net/ethernet/yunsilicon/xsc/pci/qp.h | 14 ++
8 files changed, 495 insertions(+), 1 deletion(-)
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/cq.c
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/cq.h
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/qp.c
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/qp.h
diff --git a/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
index 212cb2f9c..7a74f860c 100644
--- a/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
+++ b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
@@ -8,10 +8,14 @@
#include <linux/pci.h>
#include <linux/bitfield.h>
+#include <linux/completion.h>
#include <linux/interrupt.h>
#include <linux/list.h>
+#include <linux/radix-tree.h>
#include <linux/workqueue.h>
+#include "common/xsc_cmd.h"
+
#define XSC_PCI_VENDOR_ID 0x1f67
#define XSC_MC_PF_DEV_ID 0x1011
@@ -84,6 +88,49 @@ struct xsc_irq_info {
char name[XSC_MAX_IRQ_NAME];
};
+enum xsc_event {
+ XSC_EVENT_TYPE_COMP = 0x0,
+ XSC_EVENT_TYPE_CQ_ERROR = 0x04,
+ XSC_EVENT_TYPE_WQ_CATAS_ERROR = 0x05,
+ XSC_EVENT_TYPE_INTERNAL_ERROR = 0x08,
+ XSC_EVENT_TYPE_WQ_INVAL_REQ_ERROR = 0x10,
+ XSC_EVENT_TYPE_WQ_ACCESS_ERROR = 0x11,
+};
+
+/* qp */
+struct xsc_core_qp {
+ void (*event)(struct xsc_core_qp *qp, int type);
+ u32 qpn;
+ atomic_t refcount;
+ struct completion free;
+ u16 eth_queue_type;
+};
+
+struct xsc_qp_table {
+ /* protect radix tree */
+ spinlock_t lock;
+ struct radix_tree_root tree;
+};
+
+/* cq */
+struct xsc_core_cq {
+ u32 cqn;
+ struct xsc_core_device *xdev;
+ atomic_t refcount;
+ struct completion free;
+ unsigned int vector;
+ unsigned int irqn;
+ void (*comp)(struct xsc_core_cq *cq);
+ void (*event)(struct xsc_core_cq *cq,
+ enum xsc_event);
+};
+
+struct xsc_cq_table {
+ /* protect radix tree */
+ spinlock_t lock;
+ struct radix_tree_root tree;
+};
+
struct xsc_cmd;
struct xsc_core_device {
@@ -107,6 +154,8 @@ struct xsc_core_device {
struct xsc_caps caps;
struct xsc_cmd *cmd;
struct xsc_eq_table eq_table;
+ struct xsc_qp_table qp_table;
+ struct xsc_cq_table cq_table;
struct xsc_irq_info *irq_info;
};
@@ -128,6 +177,26 @@ void xsc_clear_cmdq_err_status(struct xsc_core_device *xdev, u32 status);
int xsc_core_vector2eqn(struct xsc_core_device *xdev, int vector, u32 *eqn,
unsigned int *irqn);
+/* cq/qp resource */
+int xsc_core_create_resource_common(struct xsc_core_device *xdev,
+ struct xsc_core_qp *qp);
+void xsc_core_destroy_resource_common(struct xsc_core_device *xdev,
+ struct xsc_core_qp *qp);
+int xsc_core_eth_create_qp(struct xsc_core_device *xdev,
+ struct xsc_create_qp_mbox_in *in,
+ int insize, u32 *p_qpn);
+int xsc_core_eth_modify_qp_status(struct xsc_core_device *xdev,
+ u32 qpn, u16 status);
+int xsc_core_eth_destroy_qp(struct xsc_core_device *xdev, u32 qpn);
+int xsc_core_eth_modify_raw_qp(struct xsc_core_device *xdev,
+ struct xsc_modify_raw_qp_mbox_in *in);
+int xsc_core_eth_create_cq(struct xsc_core_device *xdev,
+ struct xsc_core_cq *xcq,
+ struct xsc_create_cq_mbox_in *in,
+ int insize);
+int xsc_core_eth_destroy_cq(struct xsc_core_device *xdev,
+ struct xsc_core_cq *xcq);
+
/*
* Link event callback registration. The events are dispatched from the PCI
* layer's own workqueue; unregister detaches the callback and waits for one
diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/Makefile b/drivers/net/ethernet/yunsilicon/xsc/pci/Makefile
index 7a32e0320..552cd5f46 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 cmd.o eq.o pci_irq.o alloc.o doorbell.o
+xsc_pci-y := main.o cmd.o eq.o cq.o qp.o pci_irq.o alloc.o doorbell.o
diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/cq.c b/drivers/net/ethernet/yunsilicon/xsc/pci/cq.c
new file mode 100644
index 000000000..a4f9b488d
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/pci/cq.c
@@ -0,0 +1,130 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
+ * All rights reserved.
+ */
+
+#include "common/xsc_core.h"
+#include "common/xsc_cmd.h"
+#include "cq.h"
+
+void xsc_init_cq_table(struct xsc_core_device *xdev)
+{
+ struct xsc_cq_table *table = &xdev->cq_table;
+
+ spin_lock_init(&table->lock);
+ INIT_RADIX_TREE(&table->tree, GFP_ATOMIC);
+}
+
+static int xsc_create_cq(struct xsc_core_device *xdev, u32 *p_cqn,
+ struct xsc_create_cq_mbox_in *in, int insize)
+{
+ struct xsc_create_cq_mbox_out out;
+ int ret;
+
+ memset(&out, 0, sizeof(out));
+ in->hdr.opcode = cpu_to_be16(XSC_CMD_OP_CREATE_CQ);
+ ret = xsc_cmd_exec(xdev, in, insize, &out, sizeof(out));
+ if (ret) {
+ pci_err(xdev->pdev, "failed to create cq, err=%d out.status=%u\n",
+ ret, out.hdr.status);
+ return ret;
+ }
+
+ *p_cqn = be32_to_cpu(out.cqn) & 0xffffff;
+ return 0;
+}
+
+static int xsc_destroy_cq(struct xsc_core_device *xdev, u32 cqn)
+{
+ struct xsc_destroy_cq_mbox_out out;
+ struct xsc_destroy_cq_mbox_in in;
+
+ memset(&in, 0, sizeof(in));
+ memset(&out, 0, sizeof(out));
+ in.hdr.opcode = cpu_to_be16(XSC_CMD_OP_DESTROY_CQ);
+ in.cqn = cpu_to_be32(cqn);
+
+ return xsc_cmd_exec(xdev, &in, sizeof(in), &out, sizeof(out));
+}
+
+int xsc_core_eth_create_cq(struct xsc_core_device *xdev,
+ struct xsc_core_cq *xcq,
+ struct xsc_create_cq_mbox_in *in,
+ int insize)
+{
+ struct xsc_cq_table *table = &xdev->cq_table;
+ u32 cqn;
+ int ret;
+ int err;
+
+ ret = xsc_create_cq(xdev, &cqn, in, insize);
+ if (ret) {
+ pci_err(xdev->pdev, "xsc_create_cq failed, err=%d\n", ret);
+ return ret;
+ }
+ xcq->cqn = cqn;
+ xcq->xdev = xdev;
+ atomic_set(&xcq->refcount, 1);
+ init_completion(&xcq->free);
+
+ spin_lock_irq(&table->lock);
+ ret = radix_tree_insert(&table->tree, xcq->cqn, xcq);
+ spin_unlock_irq(&table->lock);
+ if (ret)
+ goto err_insert_cq;
+ return 0;
+
+err_insert_cq:
+ err = xsc_destroy_cq(xdev, cqn);
+ if (err)
+ pci_err(xdev->pdev, "failed to destroy cqn=%d, err=%d\n",
+ xcq->cqn, err);
+ return ret;
+}
+EXPORT_SYMBOL_GPL(xsc_core_eth_create_cq);
+
+int xsc_core_eth_destroy_cq(struct xsc_core_device *xdev,
+ struct xsc_core_cq *xcq)
+{
+ struct xsc_cq_table *table = &xdev->cq_table;
+ struct xsc_core_cq *tmp;
+ int err;
+
+ spin_lock_irq(&table->lock);
+ tmp = radix_tree_delete(&table->tree, xcq->cqn);
+ spin_unlock_irq(&table->lock);
+ if (!tmp) {
+ err = -ENOENT;
+ goto err_delete_cq;
+ }
+
+ if (tmp != xcq) {
+ err = -EINVAL;
+ goto err_delete_cq;
+ }
+
+ /* the completion reader looks the cq up under rcu_read_lock and
+ * takes its reference afterwards; wait for it before dropping
+ * the tree reference so it never touches a freed cq
+ */
+ synchronize_rcu();
+
+ err = xsc_destroy_cq(xdev, xcq->cqn);
+ if (err)
+ goto err_destroy_cq;
+
+ if (atomic_dec_and_test(&xcq->refcount))
+ complete(&xcq->free);
+ wait_for_completion(&xcq->free);
+ return 0;
+
+err_destroy_cq:
+ pci_err(xdev->pdev, "failed to destroy cqn=%d, err=%d\n",
+ xcq->cqn, err);
+ return err;
+err_delete_cq:
+ pci_err(xdev->pdev, "cqn=%d not found in tree, err=%d\n",
+ xcq->cqn, err);
+ return err;
+}
+EXPORT_SYMBOL_GPL(xsc_core_eth_destroy_cq);
diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/cq.h b/drivers/net/ethernet/yunsilicon/xsc/pci/cq.h
new file mode 100644
index 000000000..46ad9cb52
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/pci/cq.h
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
+ * All rights reserved.
+ */
+
+#ifndef __XSC_CQ_H
+#define __XSC_CQ_H
+
+#include "common/xsc_core.h"
+
+void xsc_init_cq_table(struct xsc_core_device *xdev);
+
+#endif /* __XSC_CQ_H */
diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/eq.c b/drivers/net/ethernet/yunsilicon/xsc/pci/eq.c
index 875a277a8..1ebc89410 100644
--- a/drivers/net/ethernet/yunsilicon/xsc/pci/eq.c
+++ b/drivers/net/ethernet/yunsilicon/xsc/pci/eq.c
@@ -12,7 +12,9 @@
#include "common/xsc_cmd.h"
#include "common/xsc_core.h"
#include "alloc.h"
+#include "cq.h"
#include "eq.h"
+#include "qp.h"
enum {
XSC_EQE_SIZE = sizeof(struct xsc_eqe),
@@ -52,8 +54,63 @@ static void eq_update_ci(struct xsc_eq *eq, int arm)
xsc_update_eq_ci(eq->dev, eq->eqn, eq->cons_index, arm);
}
+static void xsc_cq_completion(struct xsc_core_device *xdev, u32 cqn)
+{
+ struct xsc_cq_table *table = &xdev->cq_table;
+ struct xsc_core_cq *cq;
+
+ rcu_read_lock();
+ cq = radix_tree_lookup(&table->tree, cqn);
+ if (likely(cq))
+ atomic_inc(&cq->refcount);
+ rcu_read_unlock();
+
+ if (!cq) {
+ pci_err(xdev->pdev, "Completion event for bogus CQ, cqn=%d\n",
+ cqn);
+ return;
+ }
+
+ if (!cq->comp)
+ pci_err(xdev->pdev, "cq->comp is NULL\n");
+ else
+ cq->comp(cq);
+
+ if (atomic_dec_and_test(&cq->refcount))
+ complete(&cq->free);
+}
+
+static void xsc_eq_cq_event(struct xsc_core_device *xdev,
+ u32 cqn, int event_type)
+{
+ struct xsc_cq_table *table = &xdev->cq_table;
+ struct xsc_core_cq *cq;
+
+ spin_lock(&table->lock);
+ cq = radix_tree_lookup(&table->tree, cqn);
+ if (likely(cq))
+ atomic_inc(&cq->refcount);
+ spin_unlock(&table->lock);
+
+ if (unlikely(!cq)) {
+ pci_err(xdev->pdev, "Async event for bogus CQ, cqn=%d\n",
+ cqn);
+ return;
+ }
+
+ if (cq->event)
+ cq->event(cq, event_type);
+ else
+ pci_err(xdev->pdev, "cq->event is NULL, cqn=%d\n", cq->cqn);
+
+ if (atomic_dec_and_test(&cq->refcount))
+ complete(&cq->free);
+}
+
static int xsc_eq_int(struct xsc_eq *eq)
{
+ struct xsc_core_device *xdev = eq->dev;
+ u32 cqn, qpn, queue_id;
struct xsc_eqe *eqe;
int eqes_found = 0;
int set_ci = 0;
@@ -63,6 +120,32 @@ static int xsc_eq_int(struct xsc_eq *eq)
* checked the ownership bit.
*/
rmb();
+ switch (eqe->type) {
+ case XSC_EVENT_TYPE_COMP:
+ case XSC_EVENT_TYPE_INTERNAL_ERROR:
+ queue_id = FIELD_GET(XSC_EQE_QUEUE_ID_MASK,
+ le16_to_cpu(eqe->queue_id_data));
+ cqn = queue_id;
+ xsc_cq_completion(xdev, cqn);
+ break;
+
+ case XSC_EVENT_TYPE_CQ_ERROR:
+ queue_id = FIELD_GET(XSC_EQE_QUEUE_ID_MASK,
+ le16_to_cpu(eqe->queue_id_data));
+ cqn = queue_id;
+ xsc_eq_cq_event(xdev, cqn, eqe->type);
+ break;
+ case XSC_EVENT_TYPE_WQ_CATAS_ERROR:
+ case XSC_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
+ case XSC_EVENT_TYPE_WQ_ACCESS_ERROR:
+ queue_id = FIELD_GET(XSC_EQE_QUEUE_ID_MASK,
+ le16_to_cpu(eqe->queue_id_data));
+ qpn = queue_id;
+ xsc_qp_event(xdev, qpn, eqe->type);
+ break;
+ default:
+ break;
+ }
++eq->cons_index;
eqes_found = 1;
diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/main.c b/drivers/net/ethernet/yunsilicon/xsc/pci/main.c
index 315f212c1..e158dd2e9 100644
--- a/drivers/net/ethernet/yunsilicon/xsc/pci/main.c
+++ b/drivers/net/ethernet/yunsilicon/xsc/pci/main.c
@@ -7,8 +7,10 @@
#include "common/xsc_cmd.h"
#include "common/xsc_core.h"
+#include "cq.h"
#include "eq.h"
#include "pci_irq.h"
+#include "qp.h"
static const struct pci_device_id xsc_pci_id_table[] = {
{ PCI_DEVICE(XSC_PCI_VENDOR_ID, XSC_MC_PF_DEV_ID) },
@@ -162,6 +164,9 @@ static int xsc_pci_init(struct xsc_core_device *xdev,
goto err_cmd_cleanup;
}
+ xsc_init_cq_table(xdev);
+ xsc_init_qp_table(xdev);
+
err = xsc_irq_eq_create(xdev);
if (err) {
pci_err(pdev, "failed to create irq/eq: err=%d\n", err);
diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/qp.c b/drivers/net/ethernet/yunsilicon/xsc/pci/qp.c
new file mode 100644
index 000000000..ca2ad4ab0
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/pci/qp.c
@@ -0,0 +1,180 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
+ * All rights reserved.
+ */
+
+#include <linux/gfp.h>
+#include <linux/export.h>
+
+#include "common/xsc_core.h"
+#include "common/xsc_cmd.h"
+#include "qp.h"
+
+int xsc_core_create_resource_common(struct xsc_core_device *xdev,
+ struct xsc_core_qp *qp)
+{
+ struct xsc_qp_table *table = &xdev->qp_table;
+ int err;
+
+ /* initialize before the insert: once in the tree, the qp can be
+ * found and referenced by the async event handler
+ */
+ atomic_set(&qp->refcount, 1);
+ init_completion(&qp->free);
+
+ spin_lock_irq(&table->lock);
+ err = radix_tree_insert(&table->tree, qp->qpn, qp);
+ spin_unlock_irq(&table->lock);
+ if (err)
+ return err;
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(xsc_core_create_resource_common);
+
+void xsc_core_destroy_resource_common(struct xsc_core_device *xdev,
+ struct xsc_core_qp *qp)
+{
+ struct xsc_qp_table *table = &xdev->qp_table;
+ struct xsc_core_qp *tmp;
+ unsigned long flags;
+
+ spin_lock_irqsave(&table->lock, flags);
+ tmp = radix_tree_delete(&table->tree, qp->qpn);
+ spin_unlock_irqrestore(&table->lock, flags);
+ if (!tmp) {
+ pci_warn(xdev->pdev, "qp 0x%x not found in tree\n", qp->qpn);
+ return;
+ }
+
+ if (atomic_dec_and_test(&qp->refcount))
+ complete(&qp->free);
+ wait_for_completion(&qp->free);
+}
+EXPORT_SYMBOL_GPL(xsc_core_destroy_resource_common);
+
+void xsc_qp_event(struct xsc_core_device *xdev, u32 qpn, int event_type)
+{
+ struct xsc_qp_table *table = &xdev->qp_table;
+ struct xsc_core_qp *qp;
+
+ spin_lock(&table->lock);
+
+ qp = radix_tree_lookup(&table->tree, qpn);
+ if (qp)
+ atomic_inc(&qp->refcount);
+
+ spin_unlock(&table->lock);
+
+ if (!qp) {
+ pci_err(xdev->pdev, "Async event for bogus QP 0x%x\n", qpn);
+ return;
+ }
+
+ if (qp->event)
+ qp->event(qp, event_type);
+ else
+ pci_err(xdev->pdev, "qp->event is NULL, qpn=0x%x\n", qp->qpn);
+
+ if (atomic_dec_and_test(&qp->refcount))
+ complete(&qp->free);
+}
+
+void xsc_init_qp_table(struct xsc_core_device *xdev)
+{
+ struct xsc_qp_table *table = &xdev->qp_table;
+
+ spin_lock_init(&table->lock);
+ INIT_RADIX_TREE(&table->tree, GFP_ATOMIC);
+}
+
+int xsc_core_eth_create_qp(struct xsc_core_device *xdev,
+ struct xsc_create_qp_mbox_in *in,
+ int insize, u32 *p_qpn)
+{
+ struct xsc_create_qp_mbox_out out = {};
+ int ret;
+
+ in->hdr.opcode = cpu_to_be16(XSC_CMD_OP_CREATE_QP);
+ ret = xsc_cmd_exec(xdev, in, insize, &out, sizeof(out));
+ if (ret) {
+ pci_err(xdev->pdev, "failed to create sq, err=%d out.status=%u\n",
+ ret, out.hdr.status);
+ return ret;
+ }
+
+ *p_qpn = be32_to_cpu(out.qpn) & 0xffffff;
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(xsc_core_eth_create_qp);
+
+int xsc_core_eth_modify_qp_status(struct xsc_core_device *xdev,
+ u32 qpn, u16 status)
+{
+ struct xsc_modify_qp_mbox_out out = {};
+ struct xsc_modify_qp_mbox_in in = {};
+ int ret;
+
+ in.hdr.opcode = cpu_to_be16(status);
+ in.qpn = cpu_to_be32(qpn);
+ in.no_need_wait = 1;
+
+ ret = xsc_cmd_exec(xdev, &in, sizeof(in), &out, sizeof(out));
+ if (ret) {
+ pci_err(xdev->pdev, "failed to modify qp %u status=%u, err=%d out.status %u\n",
+ qpn, status, ret, out.hdr.status);
+ return ret;
+ }
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(xsc_core_eth_modify_qp_status);
+
+int xsc_core_eth_destroy_qp(struct xsc_core_device *xdev, u32 qpn)
+{
+ struct xsc_destroy_qp_mbox_out out;
+ struct xsc_destroy_qp_mbox_in in;
+ int err;
+
+ err = xsc_core_eth_modify_qp_status(xdev, qpn, XSC_CMD_OP_2RST_QP);
+ if (err) {
+ pci_err(xdev->pdev, "failed to set sq%d status=rst, err=%d\n",
+ qpn, err);
+ return err;
+ }
+
+ memset(&in, 0, sizeof(in));
+ memset(&out, 0, sizeof(out));
+ in.hdr.opcode = cpu_to_be16(XSC_CMD_OP_DESTROY_QP);
+ in.qpn = cpu_to_be32(qpn);
+ err = xsc_cmd_exec(xdev, &in, sizeof(in), &out, sizeof(out));
+ if (err) {
+ pci_err(xdev->pdev, "failed to destroy sq%d, err=%d out.status=%u\n",
+ qpn, err, out.hdr.status);
+ return err;
+ }
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(xsc_core_eth_destroy_qp);
+
+int xsc_core_eth_modify_raw_qp(struct xsc_core_device *xdev,
+ struct xsc_modify_raw_qp_mbox_in *in)
+{
+ struct xsc_modify_raw_qp_mbox_out out = {};
+ int err;
+
+ in->hdr.opcode = cpu_to_be16(XSC_CMD_OP_MODIFY_RAW_QP);
+
+ err = xsc_cmd_exec(xdev, in, sizeof(struct xsc_modify_raw_qp_mbox_in),
+ &out, sizeof(struct xsc_modify_raw_qp_mbox_out));
+ if (err) {
+ pci_err(xdev->pdev, "failed to modify sq, err=%d out.status=%u\n",
+ err, out.hdr.status);
+ return err;
+ }
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(xsc_core_eth_modify_raw_qp);
diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/qp.h b/drivers/net/ethernet/yunsilicon/xsc/pci/qp.h
new file mode 100644
index 000000000..068bfe801
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/pci/qp.h
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
+ * All rights reserved.
+ */
+
+#ifndef __XSC_QP_H
+#define __XSC_QP_H
+
+#include "common/xsc_core.h"
+
+void xsc_init_qp_table(struct xsc_core_device *xdev);
+void xsc_qp_event(struct xsc_core_device *xdev, u32 qpn, int event_type);
+
+#endif /* __XSC_QP_H */
--
2.43.0
^ permalink raw reply related [flat|nested] 33+ messages in thread
* [PATCH v2 07/11] net/xsc: add the ethernet netdev
2026-11-26 14:57 [PATCH v2 00/11] net/xsc: add the Yunsilicon XSC ethernet driver Xin Tian
` (5 preceding siblings ...)
2026-11-26 14:57 ` [PATCH v2 06/11] net/xsc: add CQ and QP helpers Xin Tian
@ 2026-11-26 14:57 ` Xin Tian
2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:57 ` netdev-bot+sashiko
2026-11-26 14:57 ` [PATCH v2 08/11] net/xsc: add the single TX/RX channel Xin Tian
` (4 subsequent siblings)
11 siblings, 2 replies; 33+ messages in thread
From: Xin Tian @ 2026-11-26 14:57 UTC (permalink / raw)
To: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Jason Gunthorpe, Leon Romanovsky, Junxian Huang
Cc: weihg, linux-kernel, netdev, linux-rdma, kerneljasonxing
Register the ethernet auxiliary driver and set up the netdev: create the
device, program the MAC address and MTU, and wire up open/close. The
queue and data path are added in follow-up patches, so the interface is
created without a channel to move packets yet.
The netdev has no ndo_start_xmit until the transmit patch lands, so it
must not be brought up in this state.
Signed-off-by: Xin Tian <tianx@yunsilicon.com>
---
drivers/net/ethernet/yunsilicon/Kconfig | 1 +
drivers/net/ethernet/yunsilicon/Makefile | 1 +
.../ethernet/yunsilicon/xsc/common/xsc_core.h | 73 +++++
.../net/ethernet/yunsilicon/xsc/net/Kconfig | 17 ++
.../net/ethernet/yunsilicon/xsc/net/Makefile | 9 +
.../net/ethernet/yunsilicon/xsc/net/main.c | 263 ++++++++++++++++++
.../net/ethernet/yunsilicon/xsc/net/xsc_eth.h | 33 +++
.../yunsilicon/xsc/net/xsc_eth_common.h | 116 ++++++++
.../ethernet/yunsilicon/xsc/net/xsc_eth_wq.h | 150 ++++++++++
.../ethernet/yunsilicon/xsc/net/xsc_queue.h | 103 +++++++
.../net/ethernet/yunsilicon/xsc/pci/Makefile | 2 +-
.../net/ethernet/yunsilicon/xsc/pci/adev.c | 133 +++++++++
.../net/ethernet/yunsilicon/xsc/pci/adev.h | 14 +
.../net/ethernet/yunsilicon/xsc/pci/main.c | 10 +
14 files changed, 924 insertions(+), 1 deletion(-)
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/net/Kconfig
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/net/Makefile
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/net/main.c
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth.h
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_common.h
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_wq.h
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/net/xsc_queue.h
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/adev.c
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/adev.h
diff --git a/drivers/net/ethernet/yunsilicon/Kconfig b/drivers/net/ethernet/yunsilicon/Kconfig
index ffdfdf4bf..231661a4a 100644
--- a/drivers/net/ethernet/yunsilicon/Kconfig
+++ b/drivers/net/ethernet/yunsilicon/Kconfig
@@ -22,5 +22,6 @@ config NET_VENDOR_YUNSILICON
if NET_VENDOR_YUNSILICON
source "drivers/net/ethernet/yunsilicon/xsc/pci/Kconfig"
+source "drivers/net/ethernet/yunsilicon/xsc/net/Kconfig"
endif # NET_VENDOR_YUNSILICON
diff --git a/drivers/net/ethernet/yunsilicon/Makefile b/drivers/net/ethernet/yunsilicon/Makefile
index b2924ccaf..54a3122fe 100644
--- a/drivers/net/ethernet/yunsilicon/Makefile
+++ b/drivers/net/ethernet/yunsilicon/Makefile
@@ -6,3 +6,4 @@
#
obj-$(CONFIG_YUNSILICON_XSC_PCI) += xsc/pci/
+obj-$(CONFIG_YUNSILICON_XSC_ETH) += xsc/net/
diff --git a/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
index 7a74f860c..74cd5b66f 100644
--- a/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
+++ b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
@@ -7,6 +7,7 @@
#define __XSC_CORE_H
#include <linux/pci.h>
+#include <linux/auxiliary_bus.h>
#include <linux/bitfield.h>
#include <linux/completion.h>
#include <linux/interrupt.h>
@@ -31,6 +32,8 @@
#define XSC_PAGE_SHIFT_4K 12
#define XSC_PAGE_SIZE_4K (_AC(1, UL) << XSC_PAGE_SHIFT_4K)
+#define XSC_INVALID_LKEY 0x100
+
struct xsc_caps {
u32 max_cqes;
u32 send_ds_num;
@@ -47,6 +50,25 @@ enum {
XSC_MAX_IRQ_NAME = 32
};
+/* adev */
+#define XSC_PCI_DRV_NAME "xsc_pci"
+#define XSC_ETH_ADEV_NAME "eth"
+
+enum {
+ XSC_QUEUE_TYPE_RAW = 2,
+ XSC_QUEUE_TYPE_RAW_TSO = 6,
+};
+
+enum xsc_adev_idx {
+ XSC_ADEV_IDX_ETH,
+ XSC_ADEV_IDX_MAX,
+};
+
+struct xsc_adev {
+ struct auxiliary_device adev;
+ struct xsc_core_device *xdev;
+};
+
/* alloc */
struct xsc_buf_list {
void *buf;
@@ -62,6 +84,54 @@ struct xsc_buf {
unsigned long size;
};
+struct xsc_frag_buf {
+ struct xsc_buf_list *frags;
+ unsigned long npages;
+ unsigned long size;
+ u8 page_shift;
+};
+
+struct xsc_frag_buf_ctrl {
+ struct xsc_buf_list *frags;
+ u32 sz_m1;
+ u16 frag_sz_m1;
+ u16 strides_offset;
+ u8 log_sz;
+ u8 log_stride;
+ u8 log_frag_strides;
+};
+
+/* wqe */
+struct xsc_send_wqe_ctrl_seg {
+ __le32 data0;
+ __le32 msg_len;
+ __le32 data2;
+ __le32 data3;
+};
+
+struct xsc_wqe_data_seg {
+ __le32 data0;
+ __le32 mkey;
+ __le64 va;
+};
+
+#define XSC_WQE_DATA_SEG_DATA0_SEG_LEN_MASK GENMASK(31, 1)
+
+/* cqe */
+struct xsc_cqe {
+ __le32 data0;
+ __le32 imm_data;
+ __le32 msg_len;
+ __le32 vni;
+ __le64 data2;
+ u8 placeholder2;
+ u8 rsv3;
+ __le16 rsv[1];
+ __le32 data1;
+};
+
+#define XSC_CQE_DATA1_OWNER BIT(31)
+
/* eq */
struct xsc_eq {
struct xsc_core_device *dev;
@@ -136,6 +206,9 @@ struct xsc_cmd;
struct xsc_core_device {
struct pci_dev *pdev;
struct device *device;
+ int adev_id;
+ struct xsc_adev **xsc_adev_list;
+ void *eth_priv;
/* async event plumbing, owned by the PCI layer */
struct work_struct event_work;
diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/Kconfig b/drivers/net/ethernet/yunsilicon/xsc/net/Kconfig
new file mode 100644
index 000000000..7428b2629
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/net/Kconfig
@@ -0,0 +1,17 @@
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
+# All rights reserved.
+# Yunsilicon driver configuration
+#
+
+config YUNSILICON_XSC_ETH
+ tristate "Yunsilicon XSC ethernet driver"
+ depends on YUNSILICON_XSC_PCI
+ depends on NET
+ select PAGE_POOL
+ help
+ This driver provides ethernet support for
+ Yunsilicon XSC devices.
+
+ To compile this driver as a module, choose M here. The module
+ will be called xsc_eth.
diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/Makefile b/drivers/net/ethernet/yunsilicon/xsc/net/Makefile
new file mode 100644
index 000000000..db289d974
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/net/Makefile
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
+# All rights reserved.
+
+ccflags-y += -I$(srctree)/drivers/net/ethernet/yunsilicon/xsc
+
+obj-$(CONFIG_YUNSILICON_XSC_ETH) += xsc_eth.o
+
+xsc_eth-y := main.o
diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/main.c b/drivers/net/ethernet/yunsilicon/xsc/net/main.c
new file mode 100644
index 000000000..b9e75d7e1
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/net/main.c
@@ -0,0 +1,263 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
+ * All rights reserved.
+ */
+
+#include <linux/netdevice.h>
+#include <linux/etherdevice.h>
+#include <linux/auxiliary_bus.h>
+#include <linux/irq.h>
+
+#include "common/xsc_core.h"
+#include "common/xsc_cmd.h"
+#include "xsc_eth_common.h"
+#include "xsc_eth.h"
+
+static void xsc_eth_build_nic_params(struct xsc_adapter *adapter)
+{
+ struct xsc_eth_params *params = &adapter->nic_param;
+
+ params->mtu = SW_DEFAULT_MTU;
+}
+
+static void xsc_eth_netdev_init(struct xsc_adapter *adapter)
+{
+ netif_carrier_off(adapter->netdev);
+}
+
+static void xsc_set_port_admin_status(struct xsc_adapter *adapter,
+ enum xsc_port_status status)
+{
+ struct xsc_event_set_port_admin_status_mbox_out out;
+ struct xsc_event_set_port_admin_status_mbox_in in = {};
+ int err;
+
+ in.hdr.opcode = cpu_to_be16(XSC_CMD_OP_SET_PORT_ADMIN_STATUS);
+ in.status = cpu_to_be16(status);
+
+ err = xsc_cmd_exec(adapter->xdev, &in, sizeof(in), &out, sizeof(out));
+ if (err)
+ netdev_err(adapter->netdev,
+ "failed to set port admin status %s, err=%d\n",
+ status == XSC_PORT_UP ? "up" : "down", err);
+}
+
+static int xsc_eth_open(struct net_device *netdev)
+{
+ struct xsc_adapter *adapter = netdev_priv(netdev);
+
+ adapter->opened = true;
+
+ xsc_set_port_admin_status(adapter, XSC_PORT_UP);
+
+ return 0;
+}
+
+static int xsc_eth_close(struct net_device *netdev)
+{
+ struct xsc_adapter *adapter = netdev_priv(netdev);
+
+ adapter->opened = false;
+
+ netif_carrier_off(adapter->netdev);
+
+ xsc_set_port_admin_status(adapter, XSC_PORT_DOWN);
+
+ return 0;
+}
+
+static int xsc_eth_set_hw_mtu(struct xsc_core_device *xdev,
+ u16 mtu, u16 rx_buf_sz)
+{
+ struct xsc_set_mtu_mbox_out out;
+ struct xsc_set_mtu_mbox_in in;
+ int err;
+
+ memset(&in, 0, sizeof(struct xsc_set_mtu_mbox_in));
+ memset(&out, 0, sizeof(struct xsc_set_mtu_mbox_out));
+
+ in.hdr.opcode = cpu_to_be16(XSC_CMD_OP_SET_MTU);
+ in.mtu = cpu_to_be16(mtu);
+ in.rx_buf_sz_min = cpu_to_be16(rx_buf_sz);
+ in.mac_port = xdev->mac_port;
+
+ err = xsc_cmd_exec(xdev, &in, sizeof(struct xsc_set_mtu_mbox_in), &out,
+ sizeof(struct xsc_set_mtu_mbox_out));
+ if (err) {
+ netdev_err(xsc_dev_to_netdev(xdev),
+ "failed to set hw_mtu=%u rx_buf_sz=%u, err=%d, status=%d\n",
+ mtu, rx_buf_sz, err, out.hdr.status);
+ return err;
+ }
+
+ return 0;
+}
+
+static const struct net_device_ops xsc_netdev_ops = {
+ .ndo_open = xsc_eth_open,
+ .ndo_stop = xsc_eth_close,
+};
+
+static void xsc_eth_build_nic_netdev(struct xsc_adapter *adapter)
+{
+ struct net_device *netdev = adapter->netdev;
+
+ netdev->netdev_ops = &xsc_netdev_ops;
+
+ netdev->min_mtu = SW_DEFAULT_MTU;
+ netdev->max_mtu = SW_DEFAULT_MTU;
+ netdev->mtu = SW_DEFAULT_MTU;
+}
+
+static int xsc_eth_nic_init(struct xsc_adapter *adapter)
+{
+ xsc_eth_build_nic_params(adapter);
+ xsc_eth_netdev_init(adapter);
+ xsc_eth_build_nic_netdev(adapter);
+
+ return 0;
+}
+
+static int xsc_eth_get_mac(struct xsc_core_device *xdev, char *mac)
+{
+ struct xsc_query_eth_mac_mbox_out *out;
+ struct xsc_query_eth_mac_mbox_in in;
+ int err;
+
+ out = kzalloc_obj(*out, GFP_KERNEL);
+ if (!out)
+ return -ENOMEM;
+
+ memset(&in, 0, sizeof(in));
+ in.hdr.opcode = cpu_to_be16(XSC_CMD_OP_QUERY_ETH_MAC);
+
+ err = xsc_cmd_exec(xdev, &in, sizeof(in), out, sizeof(*out));
+ if (err || out->hdr.status) {
+ netdev_warn(xsc_dev_to_netdev(xdev),
+ "get mac failed! err=%d, out.status=%u\n",
+ err, out->hdr.status);
+ err = -ENOEXEC;
+ goto exit;
+ }
+
+ ether_addr_copy(mac, out->mac);
+ netdev_dbg(xsc_dev_to_netdev(xdev),
+ "get mac %02x:%02x:%02x:%02x:%02x:%02x\n",
+ mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
+
+exit:
+ kfree(out);
+ return err;
+}
+
+static void xsc_eth_l2_addr_init(struct xsc_adapter *adapter)
+{
+ struct net_device *netdev = adapter->netdev;
+ char mac[6] = {0};
+ int ret = 0;
+
+ ret = xsc_eth_get_mac(adapter->xdev, mac);
+ if (ret) {
+ netdev_warn(netdev, "get mac failed %d, generate random mac...",
+ ret);
+ eth_random_addr(mac);
+ netdev->addr_assign_type = NET_ADDR_RANDOM;
+ } else if (!is_valid_ether_addr(mac)) {
+ netdev_warn(netdev, "invalid mac %pM from firmware, generate random mac...\n",
+ mac);
+ eth_random_addr(mac);
+ netdev->addr_assign_type = NET_ADDR_RANDOM;
+ } else {
+ netdev->addr_assign_type = NET_ADDR_PERM;
+ }
+ eth_hw_addr_set(netdev, mac);
+}
+
+static int xsc_eth_nic_enable(struct xsc_adapter *adapter)
+{
+ struct xsc_core_device *xdev = adapter->xdev;
+
+ xsc_eth_l2_addr_init(adapter);
+
+ return xsc_eth_set_hw_mtu(xdev, XSC_SW2HW_MTU(adapter->nic_param.mtu),
+ XSC_SW2HW_RX_PKT_LEN(adapter->nic_param.mtu));
+}
+
+static int xsc_eth_probe(struct auxiliary_device *adev,
+ const struct auxiliary_device_id *adev_id)
+{
+ struct xsc_adev *xsc_adev = container_of(adev, struct xsc_adev, adev);
+ struct xsc_core_device *xdev = xsc_adev->xdev;
+ struct xsc_adapter *adapter;
+ struct net_device *netdev;
+ int err;
+
+ netdev = alloc_etherdev(sizeof(struct xsc_adapter));
+ if (!netdev)
+ return -ENOMEM;
+
+ netdev->dev.parent = &xdev->pdev->dev;
+ adapter = netdev_priv(netdev);
+ adapter->netdev = netdev;
+ adapter->pdev = xdev->pdev;
+ adapter->dev = &adapter->pdev->dev;
+ adapter->xdev = xdev;
+ xdev->eth_priv = adapter;
+
+ err = xsc_eth_nic_init(adapter);
+ if (err) {
+ netdev_err(netdev, "xsc_eth_nic_init failed, err=%d\n", err);
+ goto err_free_netdev;
+ }
+
+ err = xsc_eth_nic_enable(adapter);
+ if (err) {
+ netdev_err(netdev, "xsc_eth_nic_enable failed, err=%d\n", err);
+ goto err_free_netdev;
+ }
+
+ err = register_netdev(netdev);
+ if (err) {
+ netdev_err(netdev, "register_netdev failed, err=%d\n", err);
+ goto err_free_netdev;
+ }
+
+ return 0;
+
+err_free_netdev:
+ xdev->eth_priv = NULL;
+ free_netdev(netdev);
+
+ return err;
+}
+
+static void xsc_eth_remove(struct auxiliary_device *adev)
+{
+ struct xsc_adev *xsc_adev = container_of(adev, struct xsc_adev, adev);
+ struct xsc_core_device *xdev = xsc_adev->xdev;
+ struct xsc_adapter *adapter = xdev->eth_priv;
+
+ /* Runs ndo_stop(), so the queues and the HCA are shut down first. */
+ unregister_netdev(adapter->netdev);
+
+ free_netdev(adapter->netdev);
+
+ xdev->eth_priv = NULL;
+}
+
+static const struct auxiliary_device_id xsc_eth_id_table[] = {
+ { .name = XSC_PCI_DRV_NAME "." XSC_ETH_ADEV_NAME },
+ {},
+};
+MODULE_DEVICE_TABLE(auxiliary, xsc_eth_id_table);
+
+static struct auxiliary_driver xsc_eth_driver = {
+ .name = "eth",
+ .probe = xsc_eth_probe,
+ .remove = xsc_eth_remove,
+ .id_table = xsc_eth_id_table,
+};
+module_auxiliary_driver(xsc_eth_driver);
+
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("Yunsilicon XSC ethernet driver");
diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth.h b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth.h
new file mode 100644
index 000000000..38c620f00
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth.h
@@ -0,0 +1,33 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
+ * All rights reserved.
+ */
+
+#ifndef __XSC_ETH_H
+#define __XSC_ETH_H
+
+#include <linux/udp.h>
+
+#include "common/xsc_core.h"
+#include "xsc_eth_common.h"
+
+struct xsc_adapter {
+ struct net_device *netdev;
+ struct pci_dev *pdev;
+ struct device *dev;
+ struct xsc_core_device *xdev;
+
+ struct xsc_eth_params nic_param;
+
+ struct xsc_channel channel;
+
+ /* true between ndo_open and ndo_stop */
+ bool opened;
+};
+
+static inline struct net_device *xsc_dev_to_netdev(struct xsc_core_device *xdev)
+{
+ return ((struct xsc_adapter *)xdev->eth_priv)->netdev;
+}
+
+#endif /* __XSC_ETH_H */
diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_common.h b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_common.h
new file mode 100644
index 000000000..a20ba7e62
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_common.h
@@ -0,0 +1,116 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
+ * All rights reserved.
+ */
+
+#ifndef __XSC_ETH_COMMON_H
+#define __XSC_ETH_COMMON_H
+
+#include <linux/netdevice.h>
+#include <linux/if_vlan.h>
+#include <net/page_pool/types.h>
+#include <net/page_pool/helpers.h>
+
+#include "xsc_queue.h"
+
+#define SW_DEFAULT_MTU ETH_DATA_LEN
+
+#define XSC_HASH_IP_PORTS (BIT(0) | BIT(2) | BIT(3) | BIT(4) | BIT(1))
+#define XSC_HASH_IP6_PORTS (BIT(5) | BIT(6) | BIT(7) | BIT(8) | BIT(1))
+
+enum {
+ XSC_HASH_FUNC_TOP = 1,
+};
+
+#define XSC_ETH_HW_MTU_SEND 9800
+#define XSC_ETH_HARD_MTU (ETH_HLEN + VLAN_HLEN * 2 + ETH_FCS_LEN)
+#define XSC_SW2HW_MTU(mtu) ((mtu) + XSC_ETH_HARD_MTU)
+#define XSC_SW2HW_HLEN (ETH_HLEN + VLAN_HLEN * 2 + ETH_FCS_LEN)
+#define XSC_SW2HW_FRAG_SIZE(mtu) ((mtu) + XSC_SW2HW_HLEN)
+#define XSC_ETH_RX_MAX_HEAD_ROOM 256
+#define XSC_SW2HW_RX_PKT_LEN(mtu) \
+ ((mtu) + ETH_HLEN + XSC_ETH_RX_MAX_HEAD_ROOM)
+
+#define XSC_QPN_SQN_STUB 1025
+
+#define XSC_SKB_FRAG_SZ(len) (SKB_DATA_ALIGN(len) + \
+ SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
+
+#define XSC_RQCQ_ELE_SZ 32
+#define XSC_SQCQ_ELE_SZ 32
+
+#define XSC_LOG_RQCQ_SZ 0xb
+#define XSC_LOG_SQCQ_SZ 0xa
+
+#define XSC_RQCQ_ELE_NUM BIT(XSC_LOG_RQCQ_SZ)
+#define XSC_SQCQ_ELE_NUM BIT(XSC_LOG_SQCQ_SZ)
+
+enum xsc_port_status {
+ XSC_PORT_DOWN = 0,
+ XSC_PORT_UP = 1,
+};
+
+enum xsc_queue_type {
+ XSC_QUEUE_TYPE_RQCQ,
+ XSC_QUEUE_TYPE_SQCQ,
+ XSC_QUEUE_TYPE_RQ,
+ XSC_QUEUE_TYPE_SQ,
+};
+
+struct xsc_queue_attr {
+ u32 ele_num;
+ u32 ele_size;
+ u8 ele_log_size;
+ u8 q_log_size;
+};
+
+struct xsc_eth_rx_wqe_cyc {
+ DECLARE_FLEX_ARRAY(struct xsc_wqe_data_seg, data);
+};
+
+struct xsc_cq_param {
+ struct xsc_wq_param wq;
+ struct xsc_queue_attr cq_attr;
+};
+
+struct xsc_rq_param {
+ struct xsc_wq_param wq;
+ struct xsc_queue_attr rq_attr;
+ u32 frag_size;
+};
+
+struct xsc_sq_param {
+ struct xsc_wq_param wq;
+ struct xsc_queue_attr sq_attr;
+};
+
+struct xsc_channel_param {
+ struct xsc_cq_param rqcq_param;
+ struct xsc_cq_param sqcq_param;
+ struct xsc_rq_param rq_param;
+ struct xsc_sq_param sq_param;
+};
+
+struct xsc_eth_qp {
+ struct xsc_rq rq;
+ struct xsc_sq sq;
+};
+
+struct xsc_eth_params {
+ u32 mtu;
+ u32 sq_size;
+ u32 rq_size;
+};
+
+struct xsc_channel {
+ /* data path */
+ struct xsc_eth_qp qp;
+
+ /* relationship */
+ struct xsc_adapter *adapter;
+ struct net_device *netdev;
+ int cpu;
+} ____cacheline_aligned_in_smp;
+
+#endif
diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_wq.h b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_wq.h
new file mode 100644
index 000000000..35206ddf4
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_wq.h
@@ -0,0 +1,150 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (c) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd. All
+ * rights reserved.
+ * Copyright (c) 2013-2015, Mellanox Technologies, Ltd. All rights reserved.
+ */
+
+#ifndef __XSC_WQ_H
+#define __XSC_WQ_H
+
+#include "common/xsc_core.h"
+
+struct xsc_wq_param {
+ int buf_numa_node;
+ int db_numa_node;
+};
+
+struct xsc_wq_ctrl {
+ struct xsc_core_device *xdev;
+ struct xsc_frag_buf buf;
+};
+
+struct xsc_wq_cyc {
+ struct xsc_frag_buf_ctrl fbc;
+ u16 sz;
+ u16 wqe_ctr;
+ u16 cur_sz;
+};
+
+struct xsc_cqwq {
+ struct xsc_frag_buf_ctrl fbc;
+ u32 cc;
+};
+
+enum xsc_res_type {
+ XSC_RES_RQ,
+ XSC_RES_SQ,
+};
+
+u32 xsc_wq_cyc_get_size(struct xsc_wq_cyc *wq);
+
+int xsc_eth_cqwq_create(struct xsc_core_device *xdev,
+ struct xsc_wq_param *param,
+ u8 q_log_size,
+ u8 ele_log_size,
+ struct xsc_cqwq *wq,
+ struct xsc_wq_ctrl *wq_ctrl);
+
+int xsc_eth_wq_cyc_create(struct xsc_core_device *xdev,
+ struct xsc_wq_param *param,
+ u8 q_log_size,
+ u8 ele_log_size,
+ struct xsc_wq_cyc *wq,
+ struct xsc_wq_ctrl *wq_ctrl);
+void xsc_eth_wq_destroy(struct xsc_wq_ctrl *wq_ctrl);
+
+static inline void xsc_init_fbc(struct xsc_buf_list *frags,
+ u8 log_stride, u8 log_sz,
+ struct xsc_frag_buf_ctrl *fbc)
+{
+ fbc->frags = frags;
+ fbc->log_stride = log_stride;
+ fbc->log_sz = log_sz;
+ fbc->sz_m1 = (1 << fbc->log_sz) - 1;
+ fbc->log_frag_strides = PAGE_SHIFT - fbc->log_stride;
+ fbc->frag_sz_m1 = (1 << fbc->log_frag_strides) - 1;
+ fbc->strides_offset = 0;
+}
+
+static inline void *xsc_frag_buf_get_wqe(struct xsc_frag_buf_ctrl *fbc,
+ u32 ix)
+{
+ unsigned int frag;
+
+ ix += fbc->strides_offset;
+ frag = ix >> fbc->log_frag_strides;
+
+ return fbc->frags[frag].buf +
+ ((fbc->frag_sz_m1 & ix) << fbc->log_stride);
+}
+
+static inline int xsc_wq_cyc_missing(struct xsc_wq_cyc *wq)
+{
+ return wq->sz - wq->cur_sz;
+}
+
+static inline void xsc_wq_cyc_push_n(struct xsc_wq_cyc *wq, u8 n)
+{
+ wq->wqe_ctr += n;
+ wq->cur_sz += n;
+}
+
+static inline void xsc_wq_cyc_pop(struct xsc_wq_cyc *wq)
+{
+ wq->cur_sz--;
+}
+
+static inline u16 xsc_wq_cyc_ctr2ix(struct xsc_wq_cyc *wq, u16 ctr)
+{
+ return ctr & wq->fbc.sz_m1;
+}
+
+static inline u16 xsc_wq_cyc_get_head(struct xsc_wq_cyc *wq)
+{
+ return xsc_wq_cyc_ctr2ix(wq, wq->wqe_ctr);
+}
+
+static inline void *xsc_wq_cyc_get_wqe(struct xsc_wq_cyc *wq, u16 ix)
+{
+ return xsc_frag_buf_get_wqe(&wq->fbc, ix);
+}
+
+static inline u32 xsc_cqwq_ctr2ix(struct xsc_cqwq *wq, u32 ctr)
+{
+ return ctr & wq->fbc.sz_m1;
+}
+
+static inline u32 xsc_cqwq_get_ci(struct xsc_cqwq *wq)
+{
+ return xsc_cqwq_ctr2ix(wq, wq->cc);
+}
+
+static inline u32 xsc_cqwq_get_ctr_wrap_cnt(struct xsc_cqwq *wq, u32 ctr)
+{
+ return ctr >> wq->fbc.log_sz;
+}
+
+static inline u32 xsc_cqwq_get_wrap_cnt(struct xsc_cqwq *wq)
+{
+ return xsc_cqwq_get_ctr_wrap_cnt(wq, wq->cc);
+}
+
+static inline void xsc_cqwq_pop(struct xsc_cqwq *wq)
+{
+ wq->cc++;
+}
+
+static inline u32 xsc_cqwq_get_size(struct xsc_cqwq *wq)
+{
+ return wq->fbc.sz_m1 + 1;
+}
+
+static inline struct xsc_cqe *xsc_cqwq_get_wqe(struct xsc_cqwq *wq, u32 ix)
+{
+ struct xsc_cqe *cqe = xsc_frag_buf_get_wqe(&wq->fbc, ix);
+
+ return cqe;
+}
+
+#endif /* __XSC_WQ_H */
diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/xsc_queue.h b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_queue.h
new file mode 100644
index 000000000..d89e62d93
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_queue.h
@@ -0,0 +1,103 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
+ * All rights reserved.
+ * Copyright (c) 2015-2016, Mellanox Technologies. All rights reserved.
+ */
+
+#ifndef __XSC_QUEUE_H
+#define __XSC_QUEUE_H
+
+#include <net/page_pool/types.h>
+#include <net/page_pool/helpers.h>
+
+#include "common/xsc_core.h"
+#include "xsc_eth_wq.h"
+
+struct xsc_channel;
+
+enum {
+ XSC_SEND_WQE_DS = 16,
+};
+
+enum {
+ XSC_RECV_WQE_DS = 16,
+};
+
+enum {
+ XSC_ETH_RQ_STATE_ENABLED,
+};
+
+enum {
+ XSC_ETH_SQ_STATE_ENABLED,
+};
+
+#define XSC_RX_FRAG_SZ_ORDER 0
+
+struct xsc_cq {
+ /* data path - accessed per cqe */
+ struct xsc_cqwq wq;
+
+ struct xsc_core_cq xcq;
+ struct xsc_channel *channel;
+
+ /* control */
+ struct xsc_core_device *xdev;
+ struct xsc_wq_ctrl wq_ctrl;
+} ____cacheline_aligned_in_smp;
+
+struct xsc_dma_info {
+ struct page *page;
+ dma_addr_t addr;
+};
+
+struct xsc_rq {
+ struct xsc_core_qp cqp;
+ struct {
+ struct xsc_wq_cyc wq;
+ struct xsc_dma_info *di;
+ u8 wqe_bulk;
+ } wqe;
+
+ struct page_pool *page_pool;
+ struct xsc_wq_ctrl wq_ctrl;
+ struct xsc_cq cq;
+ u32 rqn;
+ u32 frag_size;
+
+ unsigned long state;
+} ____cacheline_aligned_in_smp;
+
+struct xsc_tx_wqe_info {
+ struct sk_buff *skb;
+ dma_addr_t dma_addr;
+};
+
+struct xsc_tx_wqe {
+ struct xsc_send_wqe_ctrl_seg ctrl;
+ struct xsc_wqe_data_seg data[];
+};
+
+struct xsc_sq {
+ struct xsc_core_qp cqp;
+ u16 cc;
+ u16 pc ____cacheline_aligned_in_smp;
+
+ struct xsc_cq cq;
+
+ /* read only */
+ struct xsc_wq_cyc wq;
+ struct xsc_tx_wqe_info *wqe_info;
+ struct netdev_queue *txq;
+ u32 sqn;
+ u16 stop_room;
+
+ unsigned long state;
+ unsigned int hw_mtu;
+
+ /* control path */
+ struct xsc_wq_ctrl wq_ctrl;
+ struct xsc_channel *channel;
+} ____cacheline_aligned_in_smp;
+
+#endif /* __XSC_QUEUE_H */
diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/Makefile b/drivers/net/ethernet/yunsilicon/xsc/pci/Makefile
index 552cd5f46..9cdc31f9d 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 cmd.o eq.o cq.o qp.o pci_irq.o alloc.o doorbell.o
+xsc_pci-y := main.o adev.o cmd.o eq.o cq.o qp.o pci_irq.o alloc.o doorbell.o
diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/adev.c b/drivers/net/ethernet/yunsilicon/xsc/pci/adev.c
new file mode 100644
index 000000000..3376ed10b
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/pci/adev.c
@@ -0,0 +1,133 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
+ * All rights reserved.
+ */
+
+#include <linux/auxiliary_bus.h>
+#include <linux/idr.h>
+
+#include "adev.h"
+
+static DEFINE_IDA(xsc_adev_ida);
+
+static const char * const xsc_adev_name[] = {
+ [XSC_ADEV_IDX_ETH] = XSC_ETH_ADEV_NAME,
+};
+
+static void xsc_release_adev(struct device *dev)
+{
+ struct xsc_adev *xsc_adev =
+ container_of(dev, struct xsc_adev, adev.dev);
+
+ kfree(xsc_adev);
+}
+
+static int xsc_adev_add(struct xsc_core_device *xdev, int idx)
+{
+ struct auxiliary_device *adev;
+ struct xsc_adev *xsc_adev;
+ int ret;
+
+ if (!xdev->xsc_adev_list)
+ return -ENODEV;
+
+ if (xdev->xsc_adev_list[idx])
+ return 0;
+
+ xsc_adev = kzalloc_obj(*xsc_adev, GFP_KERNEL);
+ if (!xsc_adev)
+ return -ENOMEM;
+
+ adev = &xsc_adev->adev;
+ adev->name = xsc_adev_name[idx];
+ adev->id = xdev->adev_id;
+ adev->dev.parent = &xdev->pdev->dev;
+ adev->dev.release = xsc_release_adev;
+ xsc_adev->xdev = xdev;
+
+ ret = auxiliary_device_init(adev);
+ if (ret) {
+ kfree(xsc_adev);
+ return ret;
+ }
+
+ ret = auxiliary_device_add(adev);
+ if (ret) {
+ auxiliary_device_uninit(adev);
+ return ret;
+ }
+
+ xdev->xsc_adev_list[idx] = xsc_adev;
+
+ return 0;
+}
+
+static void xsc_adev_del(struct xsc_core_device *xdev, int idx)
+{
+ struct xsc_adev *xsc_adev;
+ struct auxiliary_device *adev;
+
+ if (!xdev->xsc_adev_list)
+ return;
+
+ xsc_adev = xdev->xsc_adev_list[idx];
+ if (!xsc_adev)
+ return;
+
+ /* Clear the slot before teardown: the driver's remove callback runs
+ * synchronously inside auxiliary_device_delete() and may re-enter
+ * xsc_adev_add() for this index.
+ */
+ xdev->xsc_adev_list[idx] = NULL;
+
+ adev = &xsc_adev->adev;
+ auxiliary_device_delete(adev);
+ auxiliary_device_uninit(adev);
+}
+
+int xsc_adev_init(struct xsc_core_device *xdev)
+{
+ struct xsc_adev **xsc_adev_list;
+ int adev_id;
+ int ret;
+
+ xsc_adev_list = kcalloc(XSC_ADEV_IDX_MAX,
+ sizeof(struct xsc_adev *),
+ GFP_KERNEL);
+ if (!xsc_adev_list)
+ return -ENOMEM;
+ xdev->xsc_adev_list = xsc_adev_list;
+
+ adev_id = ida_alloc(&xsc_adev_ida, GFP_KERNEL);
+ if (adev_id < 0) {
+ ret = adev_id;
+ goto err_free_adev_list;
+ }
+ xdev->adev_id = adev_id;
+
+ ret = xsc_adev_add(xdev, XSC_ADEV_IDX_ETH);
+ if (ret)
+ goto err_dalloc_adev_id;
+
+ return 0;
+
+err_dalloc_adev_id:
+ ida_free(&xsc_adev_ida, xdev->adev_id);
+err_free_adev_list:
+ kfree(xsc_adev_list);
+ xdev->xsc_adev_list = NULL;
+
+ return ret;
+}
+
+void xsc_adev_uninit(struct xsc_core_device *xdev)
+{
+ if (!xdev->xsc_adev_list)
+ return;
+
+ xsc_adev_del(xdev, XSC_ADEV_IDX_ETH);
+ ida_free(&xsc_adev_ida, xdev->adev_id);
+ kfree(xdev->xsc_adev_list);
+ xdev->xsc_adev_list = NULL;
+}
diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/adev.h b/drivers/net/ethernet/yunsilicon/xsc/pci/adev.h
new file mode 100644
index 000000000..d5c2c940e
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/pci/adev.h
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
+ * All rights reserved.
+ */
+
+#ifndef __XSC_ADEV_H
+#define __XSC_ADEV_H
+
+#include "common/xsc_core.h"
+
+int xsc_adev_init(struct xsc_core_device *xdev);
+void xsc_adev_uninit(struct xsc_core_device *xdev);
+
+#endif
diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/main.c b/drivers/net/ethernet/yunsilicon/xsc/pci/main.c
index e158dd2e9..c800cef3d 100644
--- a/drivers/net/ethernet/yunsilicon/xsc/pci/main.c
+++ b/drivers/net/ethernet/yunsilicon/xsc/pci/main.c
@@ -7,6 +7,7 @@
#include "common/xsc_cmd.h"
#include "common/xsc_core.h"
+#include "adev.h"
#include "cq.h"
#include "eq.h"
#include "pci_irq.h"
@@ -173,8 +174,16 @@ static int xsc_pci_init(struct xsc_core_device *xdev,
goto err_cmd_cleanup;
}
+ err = xsc_adev_init(xdev);
+ if (err) {
+ pci_err(pdev, "xsc_adev_init failed: err=%d\n", err);
+ goto err_irq_eq_destroy;
+ }
+
return 0;
+err_irq_eq_destroy:
+ xsc_irq_eq_destroy(xdev);
err_cmd_cleanup:
xsc_cmd_cleanup(xdev);
err_io_unmap:
@@ -192,6 +201,7 @@ static void xsc_pci_fini(struct xsc_core_device *xdev)
{
struct pci_dev *pdev = xdev->pdev;
+ xsc_adev_uninit(xdev);
xsc_irq_eq_destroy(xdev);
xsc_cmd_cleanup(xdev);
pci_iounmap(pdev, xdev->bar);
--
2.43.0
^ permalink raw reply related [flat|nested] 33+ messages in thread
* [PATCH v2 08/11] net/xsc: add the single TX/RX channel
2026-11-26 14:57 [PATCH v2 00/11] net/xsc: add the Yunsilicon XSC ethernet driver Xin Tian
` (6 preceding siblings ...)
2026-11-26 14:57 ` [PATCH v2 07/11] net/xsc: add the ethernet netdev Xin Tian
@ 2026-11-26 14:57 ` Xin Tian
2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:58 ` netdev-bot+sashiko
2026-11-26 14:57 ` [PATCH v2 09/11] net/xsc: report link state changes Xin Tian
` (3 subsequent siblings)
11 siblings, 2 replies; 33+ messages in thread
From: Xin Tian @ 2026-11-26 14:57 UTC (permalink / raw)
To: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Jason Gunthorpe, Leon Romanovsky, Junxian Huang
Cc: weihg, linux-kernel, netdev, linux-rdma, kerneljasonxing
Bring up one channel on top of the netdev: build the queue parameters,
create the CQ, SQ and RQ through the core, and enable the NIC HCA so the
channel can move packets. The queue ring helpers and the fragmented
buffer allocation they rely on are added here as well.
The CQ completion callback and NAPI poll are set up by the receive patch
that follows, so the channel cannot move packets yet and the interface
must not be opened in this state.
Signed-off-by: Xin Tian <tianx@yunsilicon.com>
---
.../ethernet/yunsilicon/xsc/common/xsc_core.h | 9 +
.../net/ethernet/yunsilicon/xsc/net/Makefile | 2 +-
.../net/ethernet/yunsilicon/xsc/net/main.c | 791 +++++++++++++++++-
.../ethernet/yunsilicon/xsc/net/xsc_eth_wq.c | 80 ++
.../net/ethernet/yunsilicon/xsc/pci/alloc.c | 102 +++
5 files changed, 982 insertions(+), 2 deletions(-)
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_wq.c
diff --git a/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
index 74cd5b66f..a72143ae6 100644
--- a/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
+++ b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
@@ -279,6 +279,15 @@ void xsc_irq_event_register(struct xsc_core_device *xdev,
void (*handler)(void *xdev));
void xsc_irq_event_unregister(struct xsc_core_device *xdev);
+void xsc_core_fill_page_frag_array(struct xsc_frag_buf *buf,
+ __be64 *pas, unsigned int npages);
+int xsc_core_frag_buf_alloc_node(struct xsc_core_device *xdev,
+ unsigned long size,
+ struct xsc_frag_buf *buf,
+ int node);
+void xsc_core_frag_buf_free(struct xsc_core_device *xdev,
+ struct xsc_frag_buf *buf);
+
static inline void *xsc_buf_offset(struct xsc_buf *buf, unsigned long offset)
{
if (likely(buf->nbufs == 1))
diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/Makefile b/drivers/net/ethernet/yunsilicon/xsc/net/Makefile
index db289d974..0e6249cec 100644
--- a/drivers/net/ethernet/yunsilicon/xsc/net/Makefile
+++ b/drivers/net/ethernet/yunsilicon/xsc/net/Makefile
@@ -6,4 +6,4 @@ ccflags-y += -I$(srctree)/drivers/net/ethernet/yunsilicon/xsc
obj-$(CONFIG_YUNSILICON_XSC_ETH) += xsc_eth.o
-xsc_eth-y := main.o
+xsc_eth-y := main.o xsc_eth_wq.o
diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/main.c b/drivers/net/ethernet/yunsilicon/xsc/net/main.c
index b9e75d7e1..1c7bbc27d 100644
--- a/drivers/net/ethernet/yunsilicon/xsc/net/main.c
+++ b/drivers/net/ethernet/yunsilicon/xsc/net/main.c
@@ -25,6 +25,774 @@ static void xsc_eth_netdev_init(struct xsc_adapter *adapter)
netif_carrier_off(adapter->netdev);
}
+static void xsc_eth_build_queue_param(struct xsc_adapter *adapter,
+ struct xsc_queue_attr *attr, u8 type)
+{
+ struct xsc_core_device *xdev = adapter->xdev;
+
+ if (adapter->nic_param.sq_size == 0)
+ adapter->nic_param.sq_size = min_t(u32,
+ BIT(xdev->caps.log_max_qp_depth),
+ XSC_SQCQ_ELE_NUM);
+ if (adapter->nic_param.rq_size == 0)
+ adapter->nic_param.rq_size = BIT(xdev->caps.log_max_qp_depth);
+
+ if (type == XSC_QUEUE_TYPE_RQCQ) {
+ attr->ele_num = min_t(int, XSC_RQCQ_ELE_NUM,
+ xdev->caps.max_cqes);
+ attr->ele_size = XSC_RQCQ_ELE_SZ;
+ attr->ele_log_size = order_base_2(XSC_RQCQ_ELE_SZ);
+ attr->q_log_size = order_base_2(attr->ele_num);
+ } else if (type == XSC_QUEUE_TYPE_SQCQ) {
+ attr->ele_num = min_t(int, XSC_SQCQ_ELE_NUM,
+ xdev->caps.max_cqes);
+ attr->ele_size = XSC_SQCQ_ELE_SZ;
+ attr->ele_log_size = order_base_2(XSC_SQCQ_ELE_SZ);
+ attr->q_log_size = order_base_2(attr->ele_num);
+ } else if (type == XSC_QUEUE_TYPE_RQ) {
+ attr->ele_num = adapter->nic_param.rq_size;
+ attr->ele_size = xdev->caps.recv_ds_num * XSC_RECV_WQE_DS;
+ attr->ele_log_size = order_base_2(attr->ele_size);
+ attr->q_log_size = order_base_2(attr->ele_num);
+ } else if (type == XSC_QUEUE_TYPE_SQ) {
+ attr->ele_num = adapter->nic_param.sq_size;
+ attr->ele_size = xdev->caps.send_ds_num * XSC_SEND_WQE_DS;
+ attr->ele_log_size = order_base_2(attr->ele_size);
+ attr->q_log_size = order_base_2(attr->ele_num);
+ }
+}
+
+static u32 xsc_rx_get_linear_frag_sz(u32 mtu)
+{
+ return XSC_SKB_FRAG_SZ(XSC_SW2HW_FRAG_SIZE(mtu));
+}
+
+static void xsc_eth_build_channel_param(struct xsc_adapter *adapter,
+ struct xsc_channel_param *chl_param)
+{
+ xsc_eth_build_queue_param(adapter, &chl_param->rqcq_param.cq_attr,
+ XSC_QUEUE_TYPE_RQCQ);
+ chl_param->rqcq_param.wq.buf_numa_node = dev_to_node(adapter->dev);
+
+ xsc_eth_build_queue_param(adapter, &chl_param->sqcq_param.cq_attr,
+ XSC_QUEUE_TYPE_SQCQ);
+ chl_param->sqcq_param.wq.buf_numa_node = dev_to_node(adapter->dev);
+
+ xsc_eth_build_queue_param(adapter, &chl_param->sq_param.sq_attr,
+ XSC_QUEUE_TYPE_SQ);
+ chl_param->sq_param.wq.buf_numa_node = dev_to_node(adapter->dev);
+
+ xsc_eth_build_queue_param(adapter, &chl_param->rq_param.rq_attr,
+ XSC_QUEUE_TYPE_RQ);
+ chl_param->rq_param.wq.buf_numa_node = dev_to_node(adapter->dev);
+ chl_param->rq_param.frag_size = XSC_SW2HW_FRAG_SIZE(adapter->nic_param.mtu);
+}
+
+static void xsc_eth_cq_error_event(struct xsc_core_cq *xcq,
+ enum xsc_event event)
+{
+ struct xsc_cq *xsc_cq = container_of(xcq, struct xsc_cq, xcq);
+ struct xsc_core_device *xdev = xsc_cq->xdev;
+
+ if (event != XSC_EVENT_TYPE_CQ_ERROR) {
+ netdev_err(xsc_dev_to_netdev(xdev),
+ "Unexpected event type %d on CQ %06x\n",
+ event, xcq->cqn);
+ return;
+ }
+
+ netdev_err(xsc_dev_to_netdev(xdev),
+ "Eth catch CQ ERROR: %x, cqn: %d\n", event, xcq->cqn);
+}
+
+static int xsc_eth_alloc_cq(struct xsc_channel *c, struct xsc_cq *pcq,
+ struct xsc_cq_param *pcq_param)
+{
+ u8 ele_log_size = pcq_param->cq_attr.ele_log_size;
+ struct xsc_core_device *xdev = c->adapter->xdev;
+ u8 q_log_size = pcq_param->cq_attr.q_log_size;
+ struct xsc_core_cq *core_cq = &pcq->xcq;
+ int ret;
+ u32 i;
+
+ pcq_param->wq.db_numa_node = cpu_to_node(c->cpu);
+ pcq_param->wq.buf_numa_node = cpu_to_node(c->cpu);
+
+ ret = xsc_eth_cqwq_create(xdev, &pcq_param->wq,
+ q_log_size, ele_log_size, &pcq->wq,
+ &pcq->wq_ctrl);
+ if (ret)
+ return ret;
+
+ core_cq->event = xsc_eth_cq_error_event;
+ core_cq->vector = 0;
+
+ for (i = 0; i < xsc_cqwq_get_size(&pcq->wq); i++) {
+ struct xsc_cqe *cqe = xsc_cqwq_get_wqe(&pcq->wq, i);
+
+ cqe->data1 |= cpu_to_le32(XSC_CQE_DATA1_OWNER);
+ }
+ pcq->xdev = xdev;
+ return 0;
+}
+
+static int xsc_eth_set_cq(struct xsc_channel *c,
+ struct xsc_cq *pcq,
+ struct xsc_cq_param *pcq_param)
+{
+ struct xsc_core_device *xdev = c->adapter->xdev;
+ struct xsc_create_cq_mbox_in *in;
+ unsigned int hw_npages;
+ unsigned int irqn;
+ int inlen;
+ u32 eqn;
+ int ret;
+
+ hw_npages = DIV_ROUND_UP(pcq->wq_ctrl.buf.size, XSC_PAGE_SIZE_4K);
+ /*mbox size + pas size*/
+ inlen = sizeof(struct xsc_create_cq_mbox_in) +
+ sizeof(__be64) * hw_npages;
+
+ in = kvzalloc(inlen, GFP_KERNEL);
+ if (!in)
+ return -ENOMEM;
+
+ ret = xsc_core_vector2eqn(xdev, pcq->xcq.vector, &eqn, &irqn);
+ if (ret)
+ goto err_free;
+
+ in->ctx.eqn = cpu_to_be16(eqn);
+ in->ctx.log_cq_sz = pcq_param->cq_attr.q_log_size;
+ in->ctx.pa_num = cpu_to_be16(hw_npages);
+ in->ctx.glb_func_id = cpu_to_be16(xdev->glb_func_id);
+
+ xsc_core_fill_page_frag_array(&pcq->wq_ctrl.buf,
+ &in->pas[0],
+ hw_npages);
+
+ ret = xsc_core_eth_create_cq(xdev, &pcq->xcq, in, inlen);
+ if (ret)
+ goto err_free;
+ pcq->xcq.irqn = irqn;
+ kvfree(in);
+ return 0;
+
+err_free:
+ kvfree(in);
+ return ret;
+}
+
+static void xsc_eth_free_cq(struct xsc_cq *cq)
+{
+ xsc_eth_wq_destroy(&cq->wq_ctrl);
+}
+
+static int xsc_eth_open_cq(struct xsc_channel *c,
+ struct xsc_cq *pcq,
+ struct xsc_cq_param *pcq_param)
+{
+ int ret;
+
+ ret = xsc_eth_alloc_cq(c, pcq, pcq_param);
+ if (ret)
+ return ret;
+
+ ret = xsc_eth_set_cq(c, pcq, pcq_param);
+ if (ret)
+ goto err_free_cq;
+
+ xsc_arm_cq(pcq->xdev, pcq->xcq.cqn, pcq->wq.cc, 0);
+
+ pcq->channel = c;
+
+ return 0;
+
+err_free_cq:
+ xsc_eth_free_cq(pcq);
+ return ret;
+}
+
+static void xsc_eth_close_cq(struct xsc_channel *c, struct xsc_cq *pcq)
+{
+ struct xsc_core_device *xdev = c->adapter->xdev;
+
+ /* If DESTROY_CQ failed the firmware may still own this CQ and keep
+ * writing CQEs into its buffer; leak the buffer rather than hand it
+ * back to the allocator while the device can still DMA into it.
+ */
+ if (xsc_core_eth_destroy_cq(xdev, &pcq->xcq)) {
+ netdev_err(xsc_dev_to_netdev(xdev), "failed to close cq%d\n",
+ pcq->xcq.cqn);
+ return;
+ }
+
+ xsc_eth_free_cq(pcq);
+}
+
+static void xsc_free_qp_sq_db(struct xsc_sq *sq)
+{
+ kvfree(sq->wqe_info);
+ sq->wqe_info = NULL;
+}
+
+static void xsc_free_qp_sq(struct xsc_sq *sq)
+{
+ xsc_free_qp_sq_db(sq);
+ xsc_eth_wq_destroy(&sq->wq_ctrl);
+}
+
+static int xsc_eth_alloc_qp_sq_db(struct xsc_sq *sq, int numa)
+{
+ sq->wqe_info = kvzalloc_node(array_size(xsc_wq_cyc_get_size(&sq->wq),
+ sizeof(*sq->wqe_info)),
+ GFP_KERNEL, numa);
+ if (!sq->wqe_info)
+ return -ENOMEM;
+
+ return 0;
+}
+
+static void xsc_eth_qp_event(struct xsc_core_qp *qp, int type)
+{
+ struct xsc_core_device *xdev;
+ struct xsc_rq *rq;
+ struct xsc_sq *sq;
+
+ if (qp->eth_queue_type == XSC_RES_RQ) {
+ rq = container_of(qp, struct xsc_rq, cqp);
+ xdev = rq->cq.xdev;
+ } else if (qp->eth_queue_type == XSC_RES_SQ) {
+ sq = container_of(qp, struct xsc_sq, cqp);
+ xdev = sq->cq.xdev;
+ } else {
+ pr_err("%s:Unknown eth qp type %d\n", __func__, type);
+ return;
+ }
+
+ switch (type) {
+ case XSC_EVENT_TYPE_WQ_CATAS_ERROR:
+ case XSC_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
+ case XSC_EVENT_TYPE_WQ_ACCESS_ERROR:
+ netdev_err(xsc_dev_to_netdev(xdev),
+ "%s:Async event %x on QP %d\n",
+ __func__, type, qp->qpn);
+ break;
+ default:
+ netdev_err(xsc_dev_to_netdev(xdev),
+ "%s: Unexpected event type %d on QP %d\n",
+ __func__, type, qp->qpn);
+ return;
+ }
+}
+
+static int xsc_eth_open_qp_sq(struct xsc_channel *c,
+ struct xsc_sq *psq,
+ struct xsc_sq_param *psq_param)
+{
+ u8 ele_log_size = psq_param->sq_attr.ele_log_size;
+ u8 q_log_size = psq_param->sq_attr.q_log_size;
+ struct xsc_modify_raw_qp_mbox_in *modify_in;
+ struct xsc_create_qp_mbox_in *in;
+ struct xsc_core_device *xdev;
+ struct xsc_adapter *adapter;
+ unsigned int hw_npages;
+ int inlen;
+ int ret;
+
+ adapter = c->adapter;
+ xdev = adapter->xdev;
+ psq_param->wq.db_numa_node = cpu_to_node(c->cpu);
+
+ ret = xsc_eth_wq_cyc_create(xdev, &psq_param->wq,
+ q_log_size, ele_log_size, &psq->wq,
+ &psq->wq_ctrl);
+ if (ret)
+ return ret;
+
+ hw_npages = DIV_ROUND_UP(psq->wq_ctrl.buf.size, XSC_PAGE_SIZE_4K);
+ inlen = sizeof(struct xsc_create_qp_mbox_in) +
+ sizeof(__be64) * hw_npages;
+
+ in = kvzalloc(inlen, GFP_KERNEL);
+ if (!in) {
+ ret = -ENOMEM;
+ goto err_sq_wq_destroy;
+ }
+ in->req.input_qpn = cpu_to_be16(XSC_QPN_SQN_STUB);
+ in->req.qp_type = XSC_QUEUE_TYPE_RAW_TSO;
+ in->req.log_sq_sz = ilog2(xdev->caps.send_ds_num) + q_log_size;
+ in->req.pa_num = cpu_to_be16(hw_npages);
+ in->req.cqn_send = cpu_to_be16(psq->cq.xcq.cqn);
+ in->req.cqn_recv = in->req.cqn_send;
+ in->req.glb_funcid = cpu_to_be16(xdev->glb_func_id);
+
+ xsc_core_fill_page_frag_array(&psq->wq_ctrl.buf,
+ &in->req.pas[0], hw_npages);
+
+ ret = xsc_core_eth_create_qp(xdev, in, inlen, &psq->sqn);
+ if (ret)
+ goto err_sq_in_destroy;
+
+ psq->cqp.qpn = psq->sqn;
+ psq->cqp.event = xsc_eth_qp_event;
+ psq->cqp.eth_queue_type = XSC_RES_SQ;
+
+ ret = xsc_core_create_resource_common(xdev, &psq->cqp);
+ if (ret) {
+ netdev_err(adapter->netdev, "%s:error qp:%d errno:%d\n",
+ __func__, psq->sqn, ret);
+ goto err_sq_destroy;
+ }
+
+ psq->channel = c;
+
+ psq->hw_mtu = XSC_ETH_HW_MTU_SEND;
+ psq->stop_room = 1;
+
+ ret = xsc_eth_alloc_qp_sq_db(psq, psq_param->wq.db_numa_node);
+ if (ret)
+ goto err_sq_common_destroy;
+
+ inlen = sizeof(struct xsc_modify_raw_qp_mbox_in);
+ modify_in = kvzalloc(inlen, GFP_KERNEL);
+ if (!modify_in) {
+ ret = -ENOMEM;
+ goto err_sq_common_destroy;
+ }
+
+ modify_in->req.qp_out_port = xdev->pf_id;
+ modify_in->pcie_no = xdev->pcie_no;
+ modify_in->req.qpn = cpu_to_be16((u16)(psq->sqn));
+ modify_in->req.func_id = cpu_to_be16(xdev->glb_func_id);
+ modify_in->req.dma_direct = XSC_DMA_DIR_TO_MAC;
+ modify_in->req.prio = 0;
+ ret = xsc_core_eth_modify_raw_qp(xdev, modify_in);
+ if (ret)
+ goto err_sq_modify_in_destroy;
+
+ kvfree(modify_in);
+ kvfree(in);
+
+ return 0;
+
+err_sq_modify_in_destroy:
+ kvfree(modify_in);
+
+err_sq_common_destroy:
+ xsc_free_qp_sq_db(psq);
+
+ xsc_core_destroy_resource_common(xdev, &psq->cqp);
+
+err_sq_destroy:
+ xsc_core_eth_destroy_qp(xdev, psq->cqp.qpn);
+
+err_sq_in_destroy:
+ kvfree(in);
+
+err_sq_wq_destroy:
+ xsc_eth_wq_destroy(&psq->wq_ctrl);
+ return ret;
+}
+
+static void xsc_eth_close_qp_sq(struct xsc_channel *c, struct xsc_sq *psq)
+{
+ struct xsc_core_device *xdev = c->adapter->xdev;
+
+ xsc_core_destroy_resource_common(xdev, &psq->cqp);
+
+ /* If DESTROY_QP failed the QP is still live in the firmware with its
+ * WQ pages programmed; leak the WQ buffer (and in-flight skbs) rather
+ * than free pages the device can still DMA into.
+ */
+ if (xsc_core_eth_destroy_qp(xdev, psq->cqp.qpn)) {
+ netdev_err(c->adapter->netdev, "failed to close sq%d\n",
+ psq->cqp.qpn);
+ return;
+ }
+
+ xsc_free_qp_sq(psq);
+}
+
+static int xsc_eth_open_channel(struct xsc_adapter *adapter,
+ struct xsc_channel *c,
+ struct xsc_channel_param *chl_param)
+{
+ struct xsc_core_device *xdev = adapter->xdev;
+ const struct cpumask *aff;
+ unsigned int irqn;
+ u32 eqn;
+ int ret;
+
+ c->adapter = adapter;
+ c->netdev = adapter->netdev;
+ c->cpu = 0;
+
+ ret = xsc_core_vector2eqn(xdev, 0, &eqn, &irqn);
+ if (ret)
+ goto err_out;
+ aff = irq_get_affinity_mask(irqn);
+ if (aff)
+ c->cpu = cpumask_first(aff);
+
+ ret = xsc_eth_open_cq(c, &c->qp.rq.cq, &chl_param->rqcq_param);
+ if (ret)
+ goto err_out;
+
+ ret = xsc_eth_open_cq(c, &c->qp.sq.cq, &chl_param->sqcq_param);
+ if (ret)
+ goto err_close_rq_cq;
+
+ ret = xsc_eth_open_qp_sq(c, &c->qp.sq, &chl_param->sq_param);
+ if (ret)
+ goto err_close_sq_cq;
+
+ netdev_dbg(adapter->netdev, "open channel ok\n");
+ return 0;
+
+err_close_sq_cq:
+ xsc_eth_close_cq(c, &c->qp.sq.cq);
+err_close_rq_cq:
+ xsc_eth_close_cq(c, &c->qp.rq.cq);
+err_out:
+ netdev_err(adapter->netdev, "failed to open channel, err=%d\n", ret);
+ return ret;
+}
+
+static int xsc_eth_modify_qps_channel(struct xsc_adapter *adapter,
+ struct xsc_channel *c)
+{
+ int ret;
+
+ ret = xsc_core_eth_modify_qp_status(adapter->xdev, c->qp.rq.rqn,
+ XSC_CMD_OP_RTR2RTS_QP);
+ if (ret)
+ return ret;
+
+ return xsc_core_eth_modify_qp_status(adapter->xdev, c->qp.sq.sqn,
+ XSC_CMD_OP_RTR2RTS_QP);
+}
+
+static void xsc_eth_free_qp_rq(struct xsc_rq *rq)
+{
+ kvfree(rq->wqe.di);
+
+ if (rq->page_pool)
+ page_pool_destroy(rq->page_pool);
+
+ xsc_eth_wq_destroy(&rq->wq_ctrl);
+}
+
+static int xsc_eth_alloc_rq(struct xsc_channel *c,
+ struct xsc_rq *prq,
+ struct xsc_rq_param *prq_param)
+{
+ u8 ele_log_size = prq_param->rq_attr.ele_log_size;
+ struct page_pool_params pagepool_params = {0};
+ u8 q_log_size = prq_param->rq_attr.q_log_size;
+ u32 wq_sz;
+ int ret = 0;
+ int i;
+
+ prq_param->wq.db_numa_node = cpu_to_node(c->cpu);
+
+ ret = xsc_eth_wq_cyc_create(c->adapter->xdev, &prq_param->wq,
+ q_log_size, ele_log_size, &prq->wqe.wq,
+ &prq->wq_ctrl);
+ if (ret)
+ return ret;
+
+ wq_sz = xsc_wq_cyc_get_size(&prq->wqe.wq);
+ prq->wqe.di = kvzalloc_node(array_size(wq_sz, sizeof(*prq->wqe.di)),
+ GFP_KERNEL, cpu_to_node(c->cpu));
+ if (!prq->wqe.di)
+ goto err_destroy_wq;
+
+ prq->wqe.wqe_bulk = PAGE_SIZE /
+ roundup_pow_of_two(xsc_rx_get_linear_frag_sz(c->adapter->nic_param.mtu));
+
+ pagepool_params.order = XSC_RX_FRAG_SZ_ORDER;
+ pagepool_params.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
+ pagepool_params.pool_size = wq_sz;
+ pagepool_params.nid = cpu_to_node(c->cpu);
+ pagepool_params.dev = c->adapter->dev;
+ pagepool_params.dma_dir = DMA_FROM_DEVICE;
+ pagepool_params.max_len = PAGE_SIZE;
+ prq->page_pool = page_pool_create(&pagepool_params);
+ if (IS_ERR(prq->page_pool)) {
+ ret = PTR_ERR(prq->page_pool);
+ prq->page_pool = NULL;
+ goto err_free_di;
+ }
+
+ for (i = 0; i < wq_sz; i++) {
+ struct xsc_eth_rx_wqe_cyc *wqe =
+ xsc_wq_cyc_get_wqe(&prq->wqe.wq, i);
+
+ wqe->data[0].data0 =
+ cpu_to_le32(FIELD_PREP(XSC_WQE_DATA_SEG_DATA0_SEG_LEN_MASK,
+ prq_param->frag_size));
+ wqe->data[0].mkey = cpu_to_le32(XSC_INVALID_LKEY);
+ }
+
+ prq->frag_size = prq_param->frag_size;
+
+ return 0;
+
+err_free_di:
+ kvfree(prq->wqe.di);
+err_destroy_wq:
+ xsc_eth_wq_destroy(&prq->wq_ctrl);
+ return ret;
+}
+
+static int xsc_eth_open_qp_rq(struct xsc_channel *c,
+ struct xsc_rq *prq,
+ struct xsc_rq_param *prq_param)
+{
+ struct xsc_core_device *xdev = c->adapter->xdev;
+ struct xsc_create_qp_mbox_in *in;
+ unsigned int hw_npages;
+ u8 q_log_size;
+ int inlen;
+ int ret;
+
+ ret = xsc_eth_alloc_rq(c, prq, prq_param);
+ if (ret)
+ return ret;
+
+ q_log_size = prq_param->rq_attr.q_log_size;
+ hw_npages = DIV_ROUND_UP(prq->wq_ctrl.buf.size, XSC_PAGE_SIZE_4K);
+ inlen = sizeof(struct xsc_create_qp_mbox_in) +
+ sizeof(__be64) * hw_npages;
+ in = kvzalloc(inlen, GFP_KERNEL);
+ if (!in) {
+ ret = -ENOMEM;
+ goto err_free_rq;
+ }
+
+ in->req.input_qpn = cpu_to_be16(0);
+ in->req.qp_type = XSC_QUEUE_TYPE_RAW;
+ in->req.log_rq_sz = ilog2(xdev->caps.recv_ds_num) + q_log_size;
+ in->req.pa_num = cpu_to_be16(hw_npages);
+ in->req.cqn_recv = cpu_to_be16(prq->cq.xcq.cqn);
+ in->req.cqn_send = in->req.cqn_recv;
+ in->req.glb_funcid = cpu_to_be16(xdev->glb_func_id);
+
+ xsc_core_fill_page_frag_array(&prq->wq_ctrl.buf,
+ &in->req.pas[0], hw_npages);
+
+ ret = xsc_core_eth_create_qp(xdev, in, inlen, &prq->rqn);
+ kvfree(in);
+ if (ret)
+ goto err_free_rq;
+
+ prq->cqp.qpn = prq->rqn;
+ prq->cqp.event = xsc_eth_qp_event;
+ prq->cqp.eth_queue_type = XSC_RES_RQ;
+
+ ret = xsc_core_create_resource_common(xdev, &prq->cqp);
+ if (ret) {
+ netdev_err(c->adapter->netdev,
+ "create resource common error qp:%d errno:%d\n",
+ prq->rqn, ret);
+ goto err_destroy_qp;
+ }
+
+ return 0;
+
+err_destroy_qp:
+ xsc_core_eth_destroy_qp(xdev, prq->rqn);
+err_free_rq:
+ xsc_eth_free_qp_rq(prq);
+ return ret;
+}
+
+static void xsc_eth_close_qp_rq(struct xsc_channel *c, struct xsc_rq *prq)
+{
+ struct xsc_core_device *xdev = c->adapter->xdev;
+
+ xsc_core_destroy_resource_common(xdev, &prq->cqp);
+
+ /* If DESTROY_QP failed the RQ is still live in the firmware with its
+ * WQ pages posted; leak the WQ buffer and page_pool rather than return
+ * buffer pages the device can still DMA into.
+ */
+ if (xsc_core_eth_destroy_qp(xdev, prq->cqp.qpn)) {
+ netdev_err(c->adapter->netdev, "failed to close rq%d\n",
+ prq->cqp.qpn);
+ return;
+ }
+
+ xsc_eth_free_qp_rq(prq);
+}
+
+static void xsc_eth_close_channel(struct xsc_channel *c, bool free_rq)
+{
+ if (free_rq)
+ xsc_eth_close_qp_rq(c, &c->qp.rq);
+ xsc_eth_close_cq(c, &c->qp.rq.cq);
+ memset(&c->qp.rq, 0, sizeof(struct xsc_rq));
+
+ xsc_eth_close_qp_sq(c, &c->qp.sq);
+ xsc_eth_close_cq(c, &c->qp.sq.cq);
+ memset(&c->qp.sq, 0, sizeof(struct xsc_sq));
+}
+
+static int xsc_eth_open_channels(struct xsc_adapter *adapter)
+{
+ struct xsc_channel_param *chl_param;
+ bool free_rq = false;
+ int ret = 0;
+
+ chl_param = kvzalloc_obj(*chl_param, GFP_KERNEL);
+ if (!chl_param)
+ return -ENOMEM;
+
+ xsc_eth_build_channel_param(adapter, chl_param);
+
+ ret = xsc_eth_open_channel(adapter, &adapter->channel, chl_param);
+ if (ret)
+ goto err_free_param;
+
+ ret = xsc_eth_open_qp_rq(&adapter->channel, &adapter->channel.qp.rq,
+ &chl_param->rq_param);
+ if (ret)
+ goto err_close_channel;
+ free_rq = true;
+
+ ret = xsc_eth_modify_qps_channel(adapter, &adapter->channel);
+ if (ret)
+ goto err_close_channel;
+
+ kvfree(chl_param);
+ return 0;
+
+err_close_channel:
+ xsc_eth_close_channel(&adapter->channel, free_rq);
+err_free_param:
+ kvfree(chl_param);
+ netdev_err(adapter->netdev, "failed to open channel, err=%d\n", ret);
+ return ret;
+}
+
+static void xsc_eth_close_channels(struct xsc_adapter *adapter)
+{
+ xsc_eth_close_channel(&adapter->channel, true);
+}
+
+static void xsc_eth_activate_channel(struct xsc_channel *c)
+{
+ struct xsc_sq *psq = &c->qp.sq;
+
+ psq->txq = netdev_get_tx_queue(c->netdev, 0);
+ set_bit(XSC_ETH_SQ_STATE_ENABLED, &psq->state);
+ netdev_tx_reset_queue(psq->txq);
+ netif_tx_start_queue(psq->txq);
+
+ set_bit(XSC_ETH_RQ_STATE_ENABLED, &c->qp.rq.state);
+}
+
+static void xsc_eth_deactivate_channel(struct xsc_channel *c)
+{
+ netif_tx_disable(c->netdev);
+
+ clear_bit(XSC_ETH_RQ_STATE_ENABLED, &c->qp.rq.state);
+ clear_bit(XSC_ETH_SQ_STATE_ENABLED, &c->qp.sq.state);
+
+ synchronize_net();
+}
+
+static int xsc_eth_sw_init(struct xsc_adapter *adapter)
+{
+ int ret;
+
+ ret = xsc_eth_open_channels(adapter);
+ if (ret)
+ return ret;
+
+ xsc_eth_activate_channel(&adapter->channel);
+
+ return 0;
+}
+
+static void xsc_eth_sw_deinit(struct xsc_adapter *adapter)
+{
+ xsc_eth_deactivate_channel(&adapter->channel);
+ xsc_eth_close_channels(adapter);
+}
+
+static bool xsc_get_pct_drop_config(struct xsc_core_device *xdev)
+{
+ return (xdev->pdev->device == XSC_MC_PF_DEV_ID) ||
+ (xdev->pdev->device == XSC_MF_SOC_PF_DEV_ID) ||
+ (xdev->pdev->device == XSC_MS_PF_DEV_ID) ||
+ (xdev->pdev->device == XSC_MV_SOC_PF_DEV_ID);
+}
+
+static int xsc_eth_enable_nic_hca(struct xsc_adapter *adapter)
+{
+ struct xsc_cmd_enable_nic_hca_mbox_out out = {};
+ struct xsc_cmd_enable_nic_hca_mbox_in in = {};
+ struct xsc_core_device *xdev = adapter->xdev;
+ struct net_device *netdev = adapter->netdev;
+ u16 caps_mask = 0;
+ u16 caps = 0;
+ int err;
+
+ in.hdr.opcode = cpu_to_be16(XSC_CMD_OP_ENABLE_NIC_HCA);
+
+ in.rss.rss_en = 1;
+ in.rss.rqn_base = cpu_to_be16(adapter->channel.qp.rq.rqn);
+ in.rss.rqn_num = cpu_to_be16(1);
+ in.rss.hfunc = XSC_HASH_FUNC_TOP;
+ in.rss.hash_tmpl = cpu_to_be32(XSC_HASH_IP_PORTS | XSC_HASH_IP6_PORTS);
+ caps_mask |= BIT(XSC_TBM_CAP_RSS);
+ caps_mask |= BIT(XSC_TBM_CAP_HASH_PPH);
+
+ if (xsc_get_pct_drop_config(xdev))
+ caps |= BIT(XSC_TBM_CAP_PCT_DROP_CONFIG);
+ caps_mask |= BIT(XSC_TBM_CAP_PCT_DROP_CONFIG);
+
+ memcpy(in.nic.mac_addr, netdev->dev_addr, ETH_ALEN);
+
+ in.nic.caps = cpu_to_be16(caps);
+ in.nic.caps_mask = cpu_to_be16(caps_mask);
+
+ err = xsc_cmd_exec(xdev, &in, sizeof(in), &out, sizeof(out));
+ if (err) {
+ netdev_err(netdev, "failed!! err=%d, status=%d\n",
+ err, out.hdr.status);
+ return -ENOEXEC;
+ }
+
+ return 0;
+}
+
+static int xsc_eth_disable_nic_hca(struct xsc_adapter *adapter)
+{
+ struct xsc_cmd_disable_nic_hca_mbox_out out = {};
+ struct xsc_cmd_disable_nic_hca_mbox_in in = {};
+ struct xsc_core_device *xdev = adapter->xdev;
+ struct net_device *netdev = adapter->netdev;
+ u16 caps = 0;
+ int err;
+
+ in.hdr.opcode = cpu_to_be16(XSC_CMD_OP_DISABLE_NIC_HCA);
+
+ if (xsc_get_pct_drop_config(xdev))
+ caps |= BIT(XSC_TBM_CAP_PCT_DROP_CONFIG);
+
+ in.nic.caps = cpu_to_be16(caps);
+ err = xsc_cmd_exec(xdev, &in, sizeof(in), &out, sizeof(out));
+ if (err) {
+ netdev_err(netdev, "failed!! err=%d, status=%d\n",
+ err, out.hdr.status);
+ return -ENOEXEC;
+ }
+
+ return 0;
+}
+
static void xsc_set_port_admin_status(struct xsc_adapter *adapter,
enum xsc_port_status status)
{
@@ -45,25 +813,46 @@ static void xsc_set_port_admin_status(struct xsc_adapter *adapter,
static int xsc_eth_open(struct net_device *netdev)
{
struct xsc_adapter *adapter = netdev_priv(netdev);
+ int ret;
+
+ ret = xsc_eth_sw_init(adapter);
+ if (ret)
+ return ret;
+
+ ret = xsc_eth_enable_nic_hca(adapter);
+ if (ret)
+ goto err_sw_deinit;
adapter->opened = true;
xsc_set_port_admin_status(adapter, XSC_PORT_UP);
return 0;
+
+err_sw_deinit:
+ xsc_eth_sw_deinit(adapter);
+ return ret;
}
static int xsc_eth_close(struct net_device *netdev)
{
struct xsc_adapter *adapter = netdev_priv(netdev);
+ int ret;
adapter->opened = false;
netif_carrier_off(adapter->netdev);
+ xsc_eth_sw_deinit(adapter);
+
+ ret = xsc_eth_disable_nic_hca(adapter);
+ if (ret)
+ netdev_err(adapter->netdev, "failed to disable nic hca, err=%d\n",
+ ret);
+
xsc_set_port_admin_status(adapter, XSC_PORT_DOWN);
- return 0;
+ return ret;
}
static int xsc_eth_set_hw_mtu(struct xsc_core_device *xdev,
diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_wq.c b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_wq.c
new file mode 100644
index 000000000..b69a4e373
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_wq.c
@@ -0,0 +1,80 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd. All
+ * rights reserved.
+ * Copyright (c) 2013-2015, Mellanox Technologies, Ltd. All rights reserved.
+ */
+
+#include "xsc_eth_wq.h"
+#include "xsc_eth.h"
+
+u32 xsc_wq_cyc_get_size(struct xsc_wq_cyc *wq)
+{
+ return (u32)wq->fbc.sz_m1 + 1;
+}
+
+static u32 wq_get_byte_sz(u8 log_sz, u8 log_stride)
+{
+ return ((u32)1 << log_sz) << log_stride;
+}
+
+int xsc_eth_cqwq_create(struct xsc_core_device *xdev,
+ struct xsc_wq_param *param,
+ u8 q_log_size,
+ u8 ele_log_size,
+ struct xsc_cqwq *wq,
+ struct xsc_wq_ctrl *wq_ctrl)
+{
+ u8 log_wq_stride = ele_log_size;
+ u8 log_wq_sz = q_log_size;
+ int err;
+
+ err = xsc_core_frag_buf_alloc_node(xdev,
+ wq_get_byte_sz(log_wq_sz,
+ log_wq_stride),
+ &wq_ctrl->buf,
+ param->buf_numa_node);
+ if (err) {
+ netdev_err(xsc_dev_to_netdev(xdev),
+ "xsc_core_frag_buf_alloc_node failed, %d\n", err);
+ return err;
+ }
+
+ xsc_init_fbc(wq_ctrl->buf.frags, log_wq_stride, log_wq_sz, &wq->fbc);
+ wq_ctrl->xdev = xdev;
+ return 0;
+}
+
+int xsc_eth_wq_cyc_create(struct xsc_core_device *xdev,
+ struct xsc_wq_param *param,
+ u8 q_log_size,
+ u8 ele_log_size,
+ struct xsc_wq_cyc *wq,
+ struct xsc_wq_ctrl *wq_ctrl)
+{
+ struct xsc_frag_buf_ctrl *fbc = &wq->fbc;
+ u8 log_wq_stride = ele_log_size;
+ u8 log_wq_sz = q_log_size;
+ int err;
+
+ err = xsc_core_frag_buf_alloc_node(xdev,
+ wq_get_byte_sz(log_wq_sz,
+ log_wq_stride),
+ &wq_ctrl->buf,
+ param->buf_numa_node);
+ if (err) {
+ netdev_err(xsc_dev_to_netdev(xdev),
+ "xsc_core_frag_buf_alloc_node failed, %d\n", err);
+ return err;
+ }
+
+ xsc_init_fbc(wq_ctrl->buf.frags, log_wq_stride, log_wq_sz, fbc);
+ wq->sz = xsc_wq_cyc_get_size(wq);
+ wq_ctrl->xdev = xdev;
+ return 0;
+}
+
+void xsc_eth_wq_destroy(struct xsc_wq_ctrl *wq_ctrl)
+{
+ xsc_core_frag_buf_free(wq_ctrl->xdev, &wq_ctrl->buf);
+}
diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/alloc.c b/drivers/net/ethernet/yunsilicon/xsc/pci/alloc.c
index 46eb0b031..0b0478c64 100644
--- a/drivers/net/ethernet/yunsilicon/xsc/pci/alloc.c
+++ b/drivers/net/ethernet/yunsilicon/xsc/pci/alloc.c
@@ -121,3 +121,105 @@ void xsc_fill_page_array(struct xsc_buf *buf, __be64 *pas, unsigned int npages)
pas[i] = cpu_to_be64(addr);
}
}
+EXPORT_SYMBOL_GPL(xsc_fill_page_array);
+
+void xsc_core_fill_page_frag_array(struct xsc_frag_buf *buf,
+ __be64 *pas,
+ unsigned int npages)
+{
+ int i;
+ dma_addr_t addr;
+ unsigned int shift = PAGE_SHIFT - XSC_PAGE_SHIFT_4K;
+ unsigned int mask = (1 << shift) - 1;
+
+ for (i = 0; i < npages; i++) {
+ addr = buf->frags[i >> shift].map +
+ ((i & mask) << XSC_PAGE_SHIFT_4K);
+ pas[i] = cpu_to_be64(addr);
+ }
+}
+EXPORT_SYMBOL_GPL(xsc_core_fill_page_frag_array);
+
+static void *xsc_dma_zalloc_coherent_node(struct xsc_core_device *xdev,
+ size_t size, dma_addr_t *dma_handle,
+ int node)
+{
+ struct device *device = &xdev->pdev->dev;
+ int original_node;
+ void *cpu_handle;
+
+ /* WA for kernels that don't use numa_mem_id in alloc_pages_node */
+ if (node == NUMA_NO_NODE)
+ node = numa_mem_id();
+
+ original_node = dev_to_node(device);
+ set_dev_node(device, node);
+ cpu_handle = dma_alloc_coherent(device, size, dma_handle, GFP_KERNEL);
+ set_dev_node(device, original_node);
+
+ return cpu_handle;
+}
+
+int xsc_core_frag_buf_alloc_node(struct xsc_core_device *xdev,
+ unsigned long size,
+ struct xsc_frag_buf *buf,
+ int node)
+{
+ int i;
+
+ buf->size = size;
+ buf->npages = DIV_ROUND_UP(size, PAGE_SIZE);
+ buf->page_shift = PAGE_SHIFT;
+ buf->frags = kcalloc(buf->npages, sizeof(struct xsc_buf_list),
+ GFP_KERNEL);
+ if (!buf->frags)
+ goto err_out;
+
+ for (i = 0; i < buf->npages; i++) {
+ unsigned long frag_sz = min_t(unsigned long, size, PAGE_SIZE);
+ struct xsc_buf_list *frag = &buf->frags[i];
+
+ frag->buf = xsc_dma_zalloc_coherent_node(xdev, frag_sz,
+ &frag->map, node);
+ if (!frag->buf)
+ goto err_free_buf;
+ if (frag->map & ((1 << buf->page_shift) - 1)) {
+ dma_free_coherent(&xdev->pdev->dev, frag_sz,
+ buf->frags[i].buf, buf->frags[i].map);
+ pci_err(xdev->pdev, "unexpected map alignment: %pad, page_shift=%d\n",
+ &frag->map, buf->page_shift);
+ goto err_free_buf;
+ }
+ size -= frag_sz;
+ }
+
+ return 0;
+
+err_free_buf:
+ while (i--)
+ dma_free_coherent(&xdev->pdev->dev,
+ PAGE_SIZE,
+ buf->frags[i].buf,
+ buf->frags[i].map);
+ kfree(buf->frags);
+err_out:
+ return -ENOMEM;
+}
+EXPORT_SYMBOL_GPL(xsc_core_frag_buf_alloc_node);
+
+void xsc_core_frag_buf_free(struct xsc_core_device *xdev,
+ struct xsc_frag_buf *buf)
+{
+ unsigned long size = buf->size;
+ int i;
+
+ for (i = 0; i < buf->npages; i++) {
+ unsigned long frag_sz = min_t(unsigned long, size, PAGE_SIZE);
+
+ dma_free_coherent(&xdev->pdev->dev, frag_sz, buf->frags[i].buf,
+ buf->frags[i].map);
+ size -= frag_sz;
+ }
+ kfree(buf->frags);
+}
+EXPORT_SYMBOL_GPL(xsc_core_frag_buf_free);
--
2.43.0
^ permalink raw reply related [flat|nested] 33+ messages in thread
* [PATCH v2 09/11] net/xsc: report link state changes
2026-11-26 14:57 [PATCH v2 00/11] net/xsc: add the Yunsilicon XSC ethernet driver Xin Tian
` (7 preceding siblings ...)
2026-11-26 14:57 ` [PATCH v2 08/11] net/xsc: add the single TX/RX channel Xin Tian
@ 2026-11-26 14:57 ` Xin Tian
2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:58 ` netdev-bot+sashiko
2026-11-26 14:57 ` [PATCH v2 10/11] net/xsc: add the transmit path Xin Tian
` (2 subsequent siblings)
11 siblings, 2 replies; 33+ messages in thread
From: Xin Tian @ 2026-11-26 14:57 UTC (permalink / raw)
To: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Jason Gunthorpe, Leon Romanovsky, Junxian Huang
Cc: weihg, linux-kernel, netdev, linux-rdma, kerneljasonxing
Query the virtual port state and reflect it in the netdev carrier.
Register for the PCI layer's link change event and re-query the port on
each one so carrier tracks the physical link.
Signed-off-by: Xin Tian <tianx@yunsilicon.com>
---
.../ethernet/yunsilicon/xsc/common/xsc_core.h | 3 +
.../net/ethernet/yunsilicon/xsc/net/main.c | 78 ++++++++++++++++++-
.../net/ethernet/yunsilicon/xsc/pci/Makefile | 2 +-
.../net/ethernet/yunsilicon/xsc/pci/vport.c | 36 +++++++++
4 files changed, 117 insertions(+), 2 deletions(-)
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/vport.c
diff --git a/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
index a72143ae6..cca1fafc8 100644
--- a/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
+++ b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
@@ -270,6 +270,9 @@ int xsc_core_eth_create_cq(struct xsc_core_device *xdev,
int xsc_core_eth_destroy_cq(struct xsc_core_device *xdev,
struct xsc_core_cq *xcq);
+int xsc_core_query_vport_state(struct xsc_core_device *xdev, u16 vport,
+ u8 *state);
+
/*
* Link event callback registration. The events are dispatched from the PCI
* layer's own workqueue; unregister detaches the callback and waits for one
diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/main.c b/drivers/net/ethernet/yunsilicon/xsc/net/main.c
index 1c7bbc27d..75a6abd6e 100644
--- a/drivers/net/ethernet/yunsilicon/xsc/net/main.c
+++ b/drivers/net/ethernet/yunsilicon/xsc/net/main.c
@@ -721,6 +721,62 @@ static void xsc_eth_sw_deinit(struct xsc_adapter *adapter)
xsc_eth_close_channels(adapter);
}
+static int xsc_eth_get_link_status(struct xsc_adapter *adapter, bool *link_up)
+{
+ struct xsc_core_device *xdev = adapter->xdev;
+ u8 state;
+ int err;
+
+ err = xsc_core_query_vport_state(xdev, 0, &state);
+ if (err)
+ return err;
+
+ *link_up = state;
+ return 0;
+}
+
+static int xsc_eth_change_link_status(struct xsc_adapter *adapter)
+{
+ bool link_up;
+ int err;
+
+ err = xsc_eth_get_link_status(adapter, &link_up);
+ if (err) {
+ netdev_err(adapter->netdev, "failed to query link status, err=%d\n",
+ err);
+ return err;
+ }
+
+ if (link_up && !netif_carrier_ok(adapter->netdev)) {
+ netdev_info(adapter->netdev, "Link up\n");
+ netif_carrier_on(adapter->netdev);
+ } else if (!link_up && netif_carrier_ok(adapter->netdev)) {
+ netdev_info(adapter->netdev, "Link down\n");
+ netif_carrier_off(adapter->netdev);
+ }
+
+ return 0;
+}
+
+/*
+ * Registered with the PCI layer, which runs it on its own workqueue for
+ * every XSC_CMD_EVENT_RESP_CHANGE_LINK event.
+ */
+static void xsc_eth_link_event_handler(void *arg)
+{
+ struct xsc_core_device *xdev = arg;
+ struct xsc_adapter *adapter = xdev->eth_priv;
+ int err;
+
+ if (!adapter || !adapter->opened)
+ return;
+
+ err = xsc_eth_change_link_status(adapter);
+ if (err)
+ netdev_err(adapter->netdev, "failed to change linkstatus, err=%d\n",
+ err);
+}
+
static bool xsc_get_pct_drop_config(struct xsc_core_device *xdev)
{
return (xdev->pdev->device == XSC_MC_PF_DEV_ID) ||
@@ -813,6 +869,7 @@ static void xsc_set_port_admin_status(struct xsc_adapter *adapter,
static int xsc_eth_open(struct net_device *netdev)
{
struct xsc_adapter *adapter = netdev_priv(netdev);
+ bool link_up;
int ret;
ret = xsc_eth_sw_init(adapter);
@@ -823,6 +880,16 @@ static int xsc_eth_open(struct net_device *netdev)
if (ret)
goto err_sw_deinit;
+ ret = xsc_eth_get_link_status(adapter, &link_up);
+ if (ret) {
+ netdev_warn(netdev, "failed to query link status, err=%d\n", ret);
+ } else if (link_up) {
+ netdev_info(netdev, "Link up\n");
+ netif_carrier_on(adapter->netdev);
+ } else {
+ netdev_info(netdev, "Link down\n");
+ }
+
adapter->opened = true;
xsc_set_port_admin_status(adapter, XSC_PORT_UP);
@@ -1005,14 +1072,18 @@ static int xsc_eth_probe(struct auxiliary_device *adev,
goto err_free_netdev;
}
+ xsc_irq_event_register(xdev, xsc_eth_link_event_handler);
+
err = register_netdev(netdev);
if (err) {
netdev_err(netdev, "register_netdev failed, err=%d\n", err);
- goto err_free_netdev;
+ goto err_unregister_event;
}
return 0;
+err_unregister_event:
+ xsc_irq_event_unregister(xdev);
err_free_netdev:
xdev->eth_priv = NULL;
free_netdev(netdev);
@@ -1026,6 +1097,11 @@ static void xsc_eth_remove(struct auxiliary_device *adev)
struct xsc_core_device *xdev = xsc_adev->xdev;
struct xsc_adapter *adapter = xdev->eth_priv;
+ /* Stop link events reaching this adapter and wait out one already in
+ * flight, which would otherwise touch the memory freed below.
+ */
+ xsc_irq_event_unregister(xdev);
+
/* Runs ndo_stop(), so the queues and the HCA are shut down first. */
unregister_netdev(adapter->netdev);
diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/Makefile b/drivers/net/ethernet/yunsilicon/xsc/pci/Makefile
index 9cdc31f9d..8b22decb4 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 adev.o cmd.o eq.o cq.o qp.o pci_irq.o alloc.o doorbell.o
+xsc_pci-y := main.o adev.o cmd.o eq.o cq.o qp.o vport.o pci_irq.o alloc.o doorbell.o
diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/vport.c b/drivers/net/ethernet/yunsilicon/xsc/pci/vport.c
new file mode 100644
index 000000000..8272383b3
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/pci/vport.c
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2021 - 2026, Shanghai Yunsilicon Technology Co., Ltd.
+ * All rights reserved.
+ */
+
+#include "common/xsc_core.h"
+#include "common/xsc_cmd.h"
+
+int xsc_core_query_vport_state(struct xsc_core_device *xdev, u16 vport,
+ u8 *state)
+{
+ struct xsc_query_vport_state_out out;
+ struct xsc_query_vport_state_in in;
+ u32 val = 0;
+ int err;
+
+ memset(&in, 0, sizeof(in));
+ memset(&out, 0, sizeof(out));
+
+ in.hdr.opcode = cpu_to_be16(XSC_CMD_OP_QUERY_VPORT_STATE);
+ val |= FIELD_PREP(XSC_QUERY_VPORT_VPORT_NUM_MASK, vport);
+ if (vport)
+ val |= XSC_QUERY_VPORT_OTHER_VPORT;
+ in.data0 = cpu_to_be32(val);
+
+ err = xsc_cmd_exec(xdev, &in, sizeof(in), &out, sizeof(out));
+ if (err) {
+ pci_err(xdev->pdev, "failed to query vport state, err=%d, status=%d\n",
+ err, out.hdr.status);
+ return err;
+ }
+
+ *state = FIELD_GET(XSC_QUERY_VPORT_STATE_MASK, out.state);
+ return 0;
+}
+EXPORT_SYMBOL_GPL(xsc_core_query_vport_state);
--
2.43.0
^ permalink raw reply related [flat|nested] 33+ messages in thread
* [PATCH v2 10/11] net/xsc: add the transmit path
2026-11-26 14:57 [PATCH v2 00/11] net/xsc: add the Yunsilicon XSC ethernet driver Xin Tian
` (8 preceding siblings ...)
2026-11-26 14:57 ` [PATCH v2 09/11] net/xsc: report link state changes Xin Tian
@ 2026-11-26 14:57 ` Xin Tian
2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:58 ` netdev-bot+sashiko
2026-11-26 14:57 ` [PATCH v2 11/11] net/xsc: add the receive path Xin Tian
2026-11-27 14:58 ` [PATCH v2 00/11] net/xsc: add the Yunsilicon XSC ethernet driver sashiko-bot
11 siblings, 2 replies; 33+ messages in thread
From: Xin Tian @ 2026-11-26 14:57 UTC (permalink / raw)
To: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Jason Gunthorpe, Leon Romanovsky, Junxian Huang
Cc: weihg, linux-kernel, netdev, linux-rdma, kerneljasonxing
Post linear skbs to the send queue, ring the doorbell, and reap
completions. Without offloads every frame takes the same simple path.
Signed-off-by: Xin Tian <tianx@yunsilicon.com>
---
.../ethernet/yunsilicon/xsc/common/xsc_core.h | 4 +
.../net/ethernet/yunsilicon/xsc/net/Makefile | 2 +-
.../net/ethernet/yunsilicon/xsc/net/main.c | 3 +
.../ethernet/yunsilicon/xsc/net/xsc_eth_tx.c | 87 +++++++++++++++++++
.../yunsilicon/xsc/net/xsc_eth_txrx.c | 70 +++++++++++++++
.../yunsilicon/xsc/net/xsc_eth_txrx.h | 27 ++++++
6 files changed, 192 insertions(+), 1 deletion(-)
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_tx.c
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_txrx.c
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_txrx.h
diff --git a/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
index cca1fafc8..872080d09 100644
--- a/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
+++ b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
@@ -115,6 +115,10 @@ struct xsc_wqe_data_seg {
__le64 va;
};
+#define XSC_WQE_CTRL_SEG_DATA0_MSG_OPCODE_MASK GENMASK(7, 0)
+#define XSC_WQE_CTRL_SEG_DATA0_DS_DATA_NUM_MASK GENMASK(15, 11)
+#define XSC_WQE_CTRL_SEG_DATA0_WQE_ID_MASK GENMASK(31, 16)
+#define XSC_WQE_CTRL_SEG_DATA3_CE BIT(1)
#define XSC_WQE_DATA_SEG_DATA0_SEG_LEN_MASK GENMASK(31, 1)
/* cqe */
diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/Makefile b/drivers/net/ethernet/yunsilicon/xsc/net/Makefile
index 0e6249cec..ffb819884 100644
--- a/drivers/net/ethernet/yunsilicon/xsc/net/Makefile
+++ b/drivers/net/ethernet/yunsilicon/xsc/net/Makefile
@@ -6,4 +6,4 @@ ccflags-y += -I$(srctree)/drivers/net/ethernet/yunsilicon/xsc
obj-$(CONFIG_YUNSILICON_XSC_ETH) += xsc_eth.o
-xsc_eth-y := main.o xsc_eth_wq.o
+xsc_eth-y := main.o xsc_eth_wq.o xsc_eth_txrx.o xsc_eth_tx.o
diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/main.c b/drivers/net/ethernet/yunsilicon/xsc/net/main.c
index 75a6abd6e..8eda3c8b1 100644
--- a/drivers/net/ethernet/yunsilicon/xsc/net/main.c
+++ b/drivers/net/ethernet/yunsilicon/xsc/net/main.c
@@ -12,6 +12,7 @@
#include "common/xsc_cmd.h"
#include "xsc_eth_common.h"
#include "xsc_eth.h"
+#include "xsc_eth_txrx.h"
static void xsc_eth_build_nic_params(struct xsc_adapter *adapter)
{
@@ -410,6 +411,7 @@ static void xsc_eth_close_qp_sq(struct xsc_channel *c, struct xsc_sq *psq)
return;
}
+ xsc_free_tx_wqe(c->adapter->dev, psq);
xsc_free_qp_sq(psq);
}
@@ -952,6 +954,7 @@ static int xsc_eth_set_hw_mtu(struct xsc_core_device *xdev,
static const struct net_device_ops xsc_netdev_ops = {
.ndo_open = xsc_eth_open,
.ndo_stop = xsc_eth_close,
+ .ndo_start_xmit = xsc_eth_xmit,
};
static void xsc_eth_build_nic_netdev(struct xsc_adapter *adapter)
diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_tx.c b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_tx.c
new file mode 100644
index 000000000..580af1f23
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_tx.c
@@ -0,0 +1,87 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
+ * All rights reserved.
+ */
+
+#include "xsc_eth.h"
+#include "xsc_eth_txrx.h"
+
+#define XSC_OPCODE_RAW 7
+
+netdev_tx_t xsc_eth_xmit(struct sk_buff *skb, struct net_device *netdev)
+{
+ struct xsc_adapter *adapter = netdev_priv(netdev);
+ struct xsc_core_device *xdev = adapter->xdev;
+ struct xsc_send_wqe_ctrl_seg *cseg;
+ struct xsc_tx_wqe_info *wi;
+ struct xsc_sq *sq = &adapter->channel.qp.sq;
+ struct xsc_tx_wqe *wqe;
+ u32 ds_num_log = ilog2(xdev->caps.send_ds_num);
+ dma_addr_t dma_addr;
+ u16 pi;
+
+ if (!adapter->opened)
+ return NETDEV_TX_BUSY;
+
+ if (unlikely(skb_is_nonlinear(skb) || skb->len == 0 ||
+ skb->len > sq->hw_mtu))
+ goto err_drop;
+
+ pi = xsc_wq_cyc_ctr2ix(&sq->wq, sq->pc);
+ wqe = xsc_wq_cyc_get_wqe(&sq->wq, pi);
+ memset(wqe, 0, xdev->caps.send_ds_num * XSC_SEND_WQE_DS);
+
+ dma_addr = dma_map_single(sq->channel->adapter->dev, skb->data,
+ skb->len, DMA_TO_DEVICE);
+ if (unlikely(dma_mapping_error(sq->channel->adapter->dev, dma_addr)))
+ goto err_drop;
+
+ wi = &sq->wqe_info[pi];
+ wi->skb = skb;
+ wi->dma_addr = dma_addr;
+
+ cseg = &wqe->ctrl;
+ cseg->data0 = cpu_to_le32(FIELD_PREP(XSC_WQE_CTRL_SEG_DATA0_MSG_OPCODE_MASK,
+ XSC_OPCODE_RAW) |
+ FIELD_PREP(XSC_WQE_CTRL_SEG_DATA0_DS_DATA_NUM_MASK,
+ 1) |
+ FIELD_PREP(XSC_WQE_CTRL_SEG_DATA0_WQE_ID_MASK,
+ sq->pc << ds_num_log));
+ cseg->msg_len = cpu_to_le32(skb->len);
+ cseg->data3 = cpu_to_le32(XSC_WQE_CTRL_SEG_DATA3_CE);
+ wqe->data[0].data0 =
+ cpu_to_le32(FIELD_PREP(XSC_WQE_DATA_SEG_DATA0_SEG_LEN_MASK,
+ skb->len));
+ wqe->data[0].va = cpu_to_le64(dma_addr);
+
+ sq->pc++;
+ if (unlikely(!xsc_wqc_has_room_for(&sq->wq, sq->cc, sq->pc,
+ sq->stop_room)))
+ netif_tx_stop_queue(sq->txq);
+
+ if (!netdev_xmit_more() || netif_xmit_stopped(sq->txq))
+ xsc_update_tx_db(xdev, sq->sqn, sq->pc << ds_num_log);
+
+ return NETDEV_TX_OK;
+
+err_drop:
+ dev_kfree_skb_any(skb);
+ if (!netdev_xmit_more() || netif_xmit_stopped(sq->txq))
+ xsc_update_tx_db(xdev, sq->sqn, sq->pc << ds_num_log);
+ return NETDEV_TX_OK;
+}
+
+void xsc_free_tx_wqe(struct device *dev, struct xsc_sq *sq)
+{
+ for (; sq->cc != sq->pc; sq->cc++) {
+ struct xsc_tx_wqe_info *wi =
+ &sq->wqe_info[xsc_wq_cyc_ctr2ix(&sq->wq, sq->cc)];
+
+ if (!wi->skb)
+ continue;
+
+ dma_unmap_single(dev, wi->dma_addr, wi->skb->len,
+ DMA_TO_DEVICE);
+ dev_kfree_skb_any(wi->skb);
+ }
+}
diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_txrx.c b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_txrx.c
new file mode 100644
index 000000000..ee6554ed8
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_txrx.c
@@ -0,0 +1,70 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
+ * All rights reserved.
+ */
+
+#include "xsc_eth.h"
+#include "xsc_eth_txrx.h"
+
+struct xsc_cqe *xsc_cqwq_get_cqe(struct xsc_cqwq *wq)
+{
+ struct xsc_cqe *cqe = xsc_frag_buf_get_wqe(&wq->fbc,
+ xsc_cqwq_get_ci(wq));
+
+ if (!!(le32_to_cpu(cqe->data1) & XSC_CQE_DATA1_OWNER) !=
+ (xsc_cqwq_get_wrap_cnt(wq) & 1))
+ return NULL;
+
+ return cqe;
+}
+
+int xsc_poll_tx_cq(struct xsc_cq *cq, int budget)
+{
+ struct xsc_cqe *cqe;
+ struct device *dev;
+ struct xsc_sq *sq;
+ u16 sqcc;
+ int i = 0;
+
+ sq = container_of(cq, struct xsc_sq, cq);
+ if (!test_bit(XSC_ETH_SQ_STATE_ENABLED, &sq->state))
+ return 0;
+
+ dev = sq->channel->adapter->dev;
+ sqcc = sq->cc;
+
+ while (i < budget) {
+ struct xsc_tx_wqe_info *wi;
+ struct sk_buff *skb;
+
+ cqe = xsc_cqwq_get_cqe(&cq->wq);
+ if (!cqe)
+ break;
+ /* ensure cqe content is read after cqe ownership bit */
+ dma_rmb();
+
+ xsc_cqwq_pop(&cq->wq);
+ wi = &sq->wqe_info[xsc_wq_cyc_ctr2ix(&sq->wq, sqcc)];
+
+ skb = wi->skb;
+ wi->skb = NULL;
+ if (skb) {
+ dma_unmap_single(dev, wi->dma_addr, skb->len,
+ DMA_TO_DEVICE);
+ dev_kfree_skb_any(skb);
+ }
+
+ sqcc++;
+ i++;
+ }
+
+ xsc_update_cq_ci(cq->xdev, cq->xcq.cqn, cq->wq.cc);
+
+ sq->cc = sqcc;
+
+ if (netif_tx_queue_stopped(sq->txq) &&
+ xsc_wqc_has_room_for(&sq->wq, sq->cc, sq->pc, sq->stop_room))
+ netif_tx_wake_queue(sq->txq);
+
+ return i == budget;
+}
diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_txrx.h b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_txrx.h
new file mode 100644
index 000000000..02aaa0980
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_txrx.h
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
+ * All rights reserved.
+ */
+
+#ifndef __XSC_ETH_TXRX_H
+#define __XSC_ETH_TXRX_H
+
+#include <linux/dma-mapping.h>
+#include <linux/netdevice.h>
+#include <linux/skbuff.h>
+
+#include "xsc_eth.h"
+
+int xsc_poll_tx_cq(struct xsc_cq *cq, int budget);
+void xsc_free_tx_wqe(struct device *dev, struct xsc_sq *sq);
+netdev_tx_t xsc_eth_xmit(struct sk_buff *skb, struct net_device *netdev);
+
+struct xsc_cqe *xsc_cqwq_get_cqe(struct xsc_cqwq *wq);
+
+static inline bool xsc_wqc_has_room_for(struct xsc_wq_cyc *wq,
+ u16 cc, u16 pc, u16 n)
+{
+ return (xsc_wq_cyc_ctr2ix(wq, cc - pc) >= n) || (cc == pc);
+}
+
+#endif /* __XSC_ETH_TXRX_H */
--
2.43.0
^ permalink raw reply related [flat|nested] 33+ messages in thread
* [PATCH v2 11/11] net/xsc: add the receive path
2026-11-26 14:57 [PATCH v2 00/11] net/xsc: add the Yunsilicon XSC ethernet driver Xin Tian
` (9 preceding siblings ...)
2026-11-26 14:57 ` [PATCH v2 10/11] net/xsc: add the transmit path Xin Tian
@ 2026-11-26 14:57 ` Xin Tian
2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:58 ` netdev-bot+sashiko
2026-11-27 14:58 ` [PATCH v2 00/11] net/xsc: add the Yunsilicon XSC ethernet driver sashiko-bot
11 siblings, 2 replies; 33+ messages in thread
From: Xin Tian @ 2026-11-26 14:57 UTC (permalink / raw)
To: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Jason Gunthorpe, Leon Romanovsky, Junxian Huang
Cc: weihg, linux-kernel, netdev, linux-rdma, kerneljasonxing
Receive linear skbs from the completion queue with a page pool and
handle both queue completions from a single NAPI poll.
Signed-off-by: Xin Tian <tianx@yunsilicon.com>
---
.../ethernet/yunsilicon/xsc/common/xsc_core.h | 4 +
.../net/ethernet/yunsilicon/xsc/net/Makefile | 2 +-
.../net/ethernet/yunsilicon/xsc/net/main.c | 30 +++
.../yunsilicon/xsc/net/xsc_eth_common.h | 1 +
.../ethernet/yunsilicon/xsc/net/xsc_eth_rx.c | 210 ++++++++++++++++++
.../yunsilicon/xsc/net/xsc_eth_txrx.c | 25 +++
.../yunsilicon/xsc/net/xsc_eth_txrx.h | 9 +
.../ethernet/yunsilicon/xsc/net/xsc_queue.h | 2 +-
8 files changed, 281 insertions(+), 2 deletions(-)
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_rx.c
diff --git a/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
index 872080d09..238bab4ec 100644
--- a/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
+++ b/drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
@@ -134,6 +134,10 @@ struct xsc_cqe {
__le32 data1;
};
+#define XSC_CQE_DATA0_MSG_OPCD_MASK GENMASK(7, 0)
+#define XSC_CQE_DATA0_ERR_CODE_MASK GENMASK(6, 0)
+#define XSC_CQE_DATA0_IS_ERR BIT(7)
+#define XSC_CQE_DATA0_QP_ID_MASK GENMASK(22, 8)
#define XSC_CQE_DATA1_OWNER BIT(31)
/* eq */
diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/Makefile b/drivers/net/ethernet/yunsilicon/xsc/net/Makefile
index ffb819884..ce2fffd48 100644
--- a/drivers/net/ethernet/yunsilicon/xsc/net/Makefile
+++ b/drivers/net/ethernet/yunsilicon/xsc/net/Makefile
@@ -6,4 +6,4 @@ ccflags-y += -I$(srctree)/drivers/net/ethernet/yunsilicon/xsc
obj-$(CONFIG_YUNSILICON_XSC_ETH) += xsc_eth.o
-xsc_eth-y := main.o xsc_eth_wq.o xsc_eth_txrx.o xsc_eth_tx.o
+xsc_eth-y := main.o xsc_eth_wq.o xsc_eth_txrx.o xsc_eth_tx.o xsc_eth_rx.o
diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/main.c b/drivers/net/ethernet/yunsilicon/xsc/net/main.c
index 8eda3c8b1..e7b0871c1 100644
--- a/drivers/net/ethernet/yunsilicon/xsc/net/main.c
+++ b/drivers/net/ethernet/yunsilicon/xsc/net/main.c
@@ -106,6 +106,13 @@ static void xsc_eth_cq_error_event(struct xsc_core_cq *xcq,
"Eth catch CQ ERROR: %x, cqn: %d\n", event, xcq->cqn);
}
+static void xsc_eth_comp(struct xsc_core_cq *xcq)
+{
+ struct xsc_cq *cq = container_of(xcq, struct xsc_cq, xcq);
+
+ napi_schedule(cq->napi);
+}
+
static int xsc_eth_alloc_cq(struct xsc_channel *c, struct xsc_cq *pcq,
struct xsc_cq_param *pcq_param)
{
@@ -125,6 +132,7 @@ static int xsc_eth_alloc_cq(struct xsc_channel *c, struct xsc_cq *pcq,
if (ret)
return ret;
+ core_cq->comp = xsc_eth_comp;
core_cq->event = xsc_eth_cq_error_event;
core_cq->vector = 0;
@@ -204,6 +212,7 @@ static int xsc_eth_open_cq(struct xsc_channel *c,
xsc_arm_cq(pcq->xdev, pcq->xcq.cqn, pcq->wq.cc, 0);
+ pcq->napi = &c->napi;
pcq->channel = c;
return 0;
@@ -448,6 +457,8 @@ static int xsc_eth_open_channel(struct xsc_adapter *adapter,
if (ret)
goto err_close_sq_cq;
+ netif_napi_add(adapter->netdev, &c->napi, xsc_eth_napi_poll);
+
netdev_dbg(adapter->netdev, "open channel ok\n");
return 0;
@@ -465,6 +476,8 @@ static int xsc_eth_modify_qps_channel(struct xsc_adapter *adapter,
{
int ret;
+ xsc_eth_post_rx_wqes(&c->qp.rq);
+
ret = xsc_core_eth_modify_qp_status(adapter->xdev, c->qp.rq.rqn,
XSC_CMD_OP_RTR2RTS_QP);
if (ret)
@@ -612,6 +625,8 @@ static int xsc_eth_open_qp_rq(struct xsc_channel *c,
static void xsc_eth_close_qp_rq(struct xsc_channel *c, struct xsc_rq *prq)
{
struct xsc_core_device *xdev = c->adapter->xdev;
+ u32 wq_sz = xsc_wq_cyc_get_size(&prq->wqe.wq);
+ u32 i;
xsc_core_destroy_resource_common(xdev, &prq->cqp);
@@ -625,11 +640,17 @@ static void xsc_eth_close_qp_rq(struct xsc_channel *c, struct xsc_rq *prq)
return;
}
+ /* return the pages still posted to hardware before the pool dies */
+ for (i = 0; i < wq_sz; i++)
+ xsc_eth_dealloc_rx_wqe(prq, i);
+
xsc_eth_free_qp_rq(prq);
}
static void xsc_eth_close_channel(struct xsc_channel *c, bool free_rq)
{
+ netif_napi_del(&c->napi);
+
if (free_rq)
xsc_eth_close_qp_rq(c, &c->qp.rq);
xsc_eth_close_cq(c, &c->qp.rq.cq);
@@ -644,6 +665,7 @@ static int xsc_eth_open_channels(struct xsc_adapter *adapter)
{
struct xsc_channel_param *chl_param;
bool free_rq = false;
+ bool napi_enabled = false;
int ret = 0;
chl_param = kvzalloc_obj(*chl_param, GFP_KERNEL);
@@ -662,6 +684,9 @@ static int xsc_eth_open_channels(struct xsc_adapter *adapter)
goto err_close_channel;
free_rq = true;
+ napi_enable(&adapter->channel.napi);
+ napi_enabled = true;
+
ret = xsc_eth_modify_qps_channel(adapter, &adapter->channel);
if (ret)
goto err_close_channel;
@@ -670,6 +695,8 @@ static int xsc_eth_open_channels(struct xsc_adapter *adapter)
return 0;
err_close_channel:
+ if (napi_enabled)
+ napi_disable(&adapter->channel.napi);
xsc_eth_close_channel(&adapter->channel, free_rq);
err_free_param:
kvfree(chl_param);
@@ -702,6 +729,9 @@ static void xsc_eth_deactivate_channel(struct xsc_channel *c)
clear_bit(XSC_ETH_SQ_STATE_ENABLED, &c->qp.sq.state);
synchronize_net();
+
+ /* last doorbell out */
+ napi_disable(&c->napi);
}
static int xsc_eth_sw_init(struct xsc_adapter *adapter)
diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_common.h b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_common.h
index a20ba7e62..1d03209cb 100644
--- a/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_common.h
+++ b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_common.h
@@ -106,6 +106,7 @@ struct xsc_eth_params {
struct xsc_channel {
/* data path */
struct xsc_eth_qp qp;
+ struct napi_struct napi;
/* relationship */
struct xsc_adapter *adapter;
diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_rx.c b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_rx.c
new file mode 100644
index 000000000..2c07f8092
--- /dev/null
+++ b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_rx.c
@@ -0,0 +1,210 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
+ * All rights reserved.
+ */
+
+#include "xsc_eth.h"
+#include "xsc_eth_txrx.h"
+
+static void xsc_rq_notify_hw(struct xsc_rq *rq)
+{
+ struct xsc_core_device *xdev = rq->cq.xdev;
+
+ /* reverse wqe index to ds index */
+ xsc_update_rx_db(xdev, rq->rqn,
+ rq->wqe.wq.wqe_ctr << ilog2(xdev->caps.recv_ds_num));
+}
+
+static struct sk_buff *xsc_skb_from_cqe(struct xsc_rq *rq,
+ struct xsc_dma_info *di,
+ u32 cqe_bcnt)
+{
+ struct sk_buff *skb;
+ void *va;
+ u32 frag_size;
+
+ va = page_address(di->page);
+ frag_size = XSC_SKB_FRAG_SZ(cqe_bcnt);
+
+ dma_sync_single_range_for_cpu(rq->cq.xdev->device, di->addr,
+ 0, frag_size, DMA_FROM_DEVICE);
+ net_prefetch(va + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
+
+ skb = napi_build_skb(va, frag_size);
+ if (unlikely(!skb))
+ return NULL;
+
+ skb_put(skb, cqe_bcnt);
+ skb_mark_for_recycle(skb);
+
+ return skb;
+}
+
+static void xsc_dump_error_rqcqe(struct xsc_rq *rq, struct xsc_cqe *cqe)
+{
+ struct xsc_channel *c = rq->cq.channel;
+ u32 data0 = le32_to_cpu(cqe->data0);
+
+ net_err_ratelimited("Error cqe on dev=%s, cqn=%d, ci=%d, rqn=%d, qpn=%lu, error_code=0x%lx\n",
+ c->netdev->name, rq->cq.xcq.cqn,
+ xsc_cqwq_get_ci(&rq->cq.wq), rq->rqn,
+ FIELD_GET(XSC_CQE_DATA0_QP_ID_MASK, data0),
+ FIELD_GET(XSC_CQE_DATA0_ERR_CODE_MASK, data0));
+}
+
+void xsc_eth_handle_rx_cqe(struct xsc_rq *rq, struct xsc_cqe *cqe)
+{
+ struct xsc_wq_cyc *wq = &rq->wqe.wq;
+ struct xsc_dma_info *di;
+ struct sk_buff *skb;
+ u32 cqe_bcnt;
+ u16 ci;
+
+ ci = xsc_wq_cyc_ctr2ix(wq, rq->cq.wq.cc);
+ di = &rq->wqe.di[ci];
+
+ if (unlikely(le32_to_cpu(cqe->data0) & XSC_CQE_DATA0_IS_ERR)) {
+ xsc_dump_error_rqcqe(rq, cqe);
+ page_pool_recycle_direct(rq->page_pool, di->page);
+ di->page = NULL;
+ xsc_wq_cyc_pop(wq);
+ return;
+ }
+
+ cqe_bcnt = le32_to_cpu(cqe->msg_len);
+ skb = (cqe_bcnt >= ETH_HLEN && cqe_bcnt <= rq->frag_size) ?
+ xsc_skb_from_cqe(rq, di, cqe_bcnt) : NULL;
+
+ if (skb) {
+ skb_record_rx_queue(skb, 0);
+ skb->protocol = eth_type_trans(skb, rq->cq.channel->netdev);
+
+ napi_gro_receive(rq->cq.napi, skb);
+ } else {
+ /* when the skb took the page, it recycles it via pp_recycle */
+ page_pool_recycle_direct(rq->page_pool, di->page);
+ }
+ di->page = NULL;
+
+ xsc_wq_cyc_pop(wq);
+}
+
+int xsc_poll_rx_cq(struct xsc_cq *cq, int budget)
+{
+ struct xsc_rq *rq = container_of(cq, struct xsc_rq, cq);
+ struct xsc_cqe *cqe;
+ int work_done = 0;
+
+ if (!test_bit(XSC_ETH_RQ_STATE_ENABLED, &rq->state))
+ return 0;
+
+ while (work_done < budget) {
+ cqe = xsc_cqwq_get_cqe(&cq->wq);
+ if (!cqe)
+ break;
+ /* ensure cqe content is read after cqe ownership bit */
+ dma_rmb();
+
+ xsc_eth_handle_rx_cqe(rq, cqe);
+ ++work_done;
+
+ xsc_cqwq_pop(&cq->wq);
+ }
+
+ if (work_done)
+ xsc_cq_notify_hw(cq);
+
+ return work_done;
+}
+
+void xsc_eth_dealloc_rx_wqe(struct xsc_rq *rq, u16 ix)
+{
+ struct xsc_dma_info *di = &rq->wqe.di[ix];
+
+ if (!di->page)
+ return;
+
+ page_pool_recycle_direct(rq->page_pool, di->page);
+ di->page = NULL;
+}
+
+static int xsc_alloc_rx_wqes(struct xsc_rq *rq, u16 head, int bulk)
+{
+ struct xsc_wq_cyc *wq = &rq->wqe.wq;
+ struct xsc_dma_info *di;
+ struct xsc_eth_rx_wqe_cyc *wqe;
+ int i;
+
+ for (i = 0; i < bulk; i++) {
+ u16 ix = xsc_wq_cyc_ctr2ix(wq, head + i);
+
+ di = &rq->wqe.di[ix];
+ di->page = page_pool_dev_alloc_pages(rq->page_pool);
+ if (unlikely(!di->page)) {
+ while (--i >= 0)
+ xsc_eth_dealloc_rx_wqe(rq,
+ xsc_wq_cyc_ctr2ix(wq, head + i));
+ return -ENOMEM;
+ }
+ di->addr = page_pool_get_dma_addr(di->page);
+
+ wqe = xsc_wq_cyc_get_wqe(wq, ix);
+ wqe->data[0].va = cpu_to_le64(di->addr);
+ }
+
+ return 0;
+}
+
+bool xsc_eth_post_rx_wqes(struct xsc_rq *rq)
+{
+ struct xsc_wq_cyc *wq = &rq->wqe.wq;
+ int alloc;
+ u16 head;
+
+ if (xsc_wq_cyc_missing(wq) < rq->wqe.wqe_bulk)
+ return false;
+
+ do {
+ head = xsc_wq_cyc_get_head(wq);
+ alloc = min_t(int, rq->wqe.wqe_bulk, xsc_wq_cyc_missing(wq));
+
+ if (xsc_alloc_rx_wqes(rq, head, alloc))
+ return true;
+
+ xsc_wq_cyc_push_n(wq, alloc);
+ } while (xsc_wq_cyc_missing(wq) >= rq->wqe.wqe_bulk);
+
+ dma_wmb();
+
+ /* ensure wqes are visible to device before updating doorbell record */
+ xsc_rq_notify_hw(rq);
+
+ return false;
+}
+
+int xsc_eth_napi_poll(struct napi_struct *napi, int budget)
+{
+ struct xsc_channel *c = container_of(napi, struct xsc_channel, napi);
+ struct xsc_rq *rq = &c->qp.rq;
+ bool busy;
+ int work_done;
+
+ busy = xsc_poll_tx_cq(&c->qp.sq.cq, xsc_cqwq_get_size(&c->qp.sq.cq.wq));
+
+ work_done = xsc_poll_rx_cq(&rq->cq, budget);
+ busy |= work_done == budget;
+
+ if (xsc_eth_post_rx_wqes(rq))
+ busy = true;
+
+ if (busy)
+ return budget;
+
+ if (unlikely(!napi_complete_done(napi, work_done)))
+ return work_done;
+
+ xsc_cq_notify_hw_rearm(&c->qp.sq.cq);
+ xsc_cq_notify_hw_rearm(&rq->cq);
+
+ return work_done;
+}
diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_txrx.c b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_txrx.c
index ee6554ed8..b3c926506 100644
--- a/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_txrx.c
+++ b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_txrx.c
@@ -18,6 +18,28 @@ struct xsc_cqe *xsc_cqwq_get_cqe(struct xsc_cqwq *wq)
return cqe;
}
+void xsc_cq_notify_hw_rearm(struct xsc_cq *cq)
+{
+ xsc_arm_cq(cq->xdev, cq->xcq.cqn, cq->wq.cc, 0);
+}
+
+void xsc_cq_notify_hw(struct xsc_cq *cq)
+{
+ xsc_update_cq_ci(cq->xdev, cq->xcq.cqn, cq->wq.cc);
+}
+
+static void xsc_dump_error_sqcqe(struct xsc_sq *sq, struct xsc_cqe *cqe)
+{
+ struct xsc_channel *c = sq->channel;
+ u32 data0 = le32_to_cpu(cqe->data0);
+
+ net_err_ratelimited("Err cqe on dev %s cqn=0x%x ci=0x%x sqn=0x%x err_code=0x%lx qpid=0x%lx\n",
+ c->netdev->name, sq->cq.xcq.cqn,
+ xsc_cqwq_get_ci(&sq->cq.wq), sq->sqn,
+ FIELD_GET(XSC_CQE_DATA0_ERR_CODE_MASK, data0),
+ FIELD_GET(XSC_CQE_DATA0_QP_ID_MASK, data0));
+}
+
int xsc_poll_tx_cq(struct xsc_cq *cq, int budget)
{
struct xsc_cqe *cqe;
@@ -43,6 +65,9 @@ int xsc_poll_tx_cq(struct xsc_cq *cq, int budget)
/* ensure cqe content is read after cqe ownership bit */
dma_rmb();
+ if (unlikely(le32_to_cpu(cqe->data0) & XSC_CQE_DATA0_IS_ERR))
+ xsc_dump_error_sqcqe(sq, cqe);
+
xsc_cqwq_pop(&cq->wq);
wi = &sq->wqe_info[xsc_wq_cyc_ctr2ix(&sq->wq, sqcc)];
diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_txrx.h b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_txrx.h
index 02aaa0980..a7e090b4b 100644
--- a/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_txrx.h
+++ b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_txrx.h
@@ -16,6 +16,12 @@ int xsc_poll_tx_cq(struct xsc_cq *cq, int budget);
void xsc_free_tx_wqe(struct device *dev, struct xsc_sq *sq);
netdev_tx_t xsc_eth_xmit(struct sk_buff *skb, struct net_device *netdev);
+int xsc_poll_rx_cq(struct xsc_cq *cq, int budget);
+int xsc_eth_napi_poll(struct napi_struct *napi, int budget);
+void xsc_eth_handle_rx_cqe(struct xsc_rq *rq, struct xsc_cqe *cqe);
+bool xsc_eth_post_rx_wqes(struct xsc_rq *rq);
+void xsc_eth_dealloc_rx_wqe(struct xsc_rq *rq, u16 ix);
+
struct xsc_cqe *xsc_cqwq_get_cqe(struct xsc_cqwq *wq);
static inline bool xsc_wqc_has_room_for(struct xsc_wq_cyc *wq,
@@ -24,4 +30,7 @@ static inline bool xsc_wqc_has_room_for(struct xsc_wq_cyc *wq,
return (xsc_wq_cyc_ctr2ix(wq, cc - pc) >= n) || (cc == pc);
}
+void xsc_cq_notify_hw(struct xsc_cq *cq);
+void xsc_cq_notify_hw_rearm(struct xsc_cq *cq);
+
#endif /* __XSC_ETH_TXRX_H */
diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/xsc_queue.h b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_queue.h
index d89e62d93..61a2de9ef 100644
--- a/drivers/net/ethernet/yunsilicon/xsc/net/xsc_queue.h
+++ b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_queue.h
@@ -37,7 +37,7 @@ enum {
struct xsc_cq {
/* data path - accessed per cqe */
struct xsc_cqwq wq;
-
+ struct napi_struct *napi;
struct xsc_core_cq xcq;
struct xsc_channel *channel;
--
2.43.0
^ permalink raw reply related [flat|nested] 33+ messages in thread
* [PATCH v2 00/11] net/xsc: add the Yunsilicon XSC ethernet driver
@ 2026-11-26 14:57 Xin Tian
2026-11-26 14:57 ` [PATCH v2 01/11] net/xsc: add Yunsilicon XSC ethernet PCI driver Xin Tian
` (11 more replies)
0 siblings, 12 replies; 33+ messages in thread
From: Xin Tian @ 2026-11-26 14:57 UTC (permalink / raw)
To: Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Jason Gunthorpe, Leon Romanovsky, Junxian Huang
Cc: weihg, linux-kernel, netdev, linux-rdma, kerneljasonxing
This is the first series of v2 for the Yunsilicon XSC converged network
adapter (PCI vendor 0x1f67). Responding to the review of v1, this is a
much smaller, self-contained ethernet-only driver: about 5.7k added
lines, down from the combined ethernet and RDMA driver in v1.
The driver is split into the same two modules as before, which share the
xsc prefix and talk over the auxiliary bus:
1. xsc_pci (CONFIG_YUNSILICON_XSC_PCI) - the PCI core driver. Owns the
PCI function, the command queue, the event queues, MSI-X and the
CQ/QP resource pools, and publishes one auxiliary device per PF.
Patches 01-06.
2. xsc_eth (CONFIG_YUNSILICON_XSC_ETH) - the ethernet driver. Binds to
the auxiliary device, registers the netdev, brings up a single TX/RX
queue pair with NAPI and page_pool, and tracks the link state from
the port events. Patches 07-11.
Patch 01 adds the MAINTAINERS entry together with the Kbuild plumbing.
To keep the series within the requested size, this first series
deliberately omits RDMA, statistics, offloads (TSO/checksum), RSS and the
configuration interfaces (ethtool, module parameters, ndo_change_mtu).
Only the compressed-BAR PF form of the hardware is supported. These will
be submitted as follow-up series once the base driver is accepted.
The series is split so that every patch builds cleanly with GCC 8.3.1 and
W=1. The core patches add the command queue, capabilities, event queues
and resource pools incrementally, and the ethernet patches register the
netdev first, then add the channel and link handling, and finally the TX
and RX data paths.
Changes since v1:
- Split the combined ethernet+RDMA driver into a minimal ethernet-only
first series, about 5.7k added lines instead of 10k, as requested.
Addressing the netdev review of the v1 netdev patch:
- Register the link event handler before register_netdevice(), keying the
interface off xdev->eth_priv which is set before registration.
- Use ether_addr_copy() to copy the firmware MAC address.zaizuo
- Drop the unreachable NULL checks in the remove path.
- Move xsc_cqwq_get_cqe() out of the header into xsc_eth_txrx.c.
- Drop the unreachable "already opened"/"not opened" branches in
ndo_open()/ndo_stop(); the netdev core serialises these via rtnl.
- Keep xsc_set_port_admin_status() void: it runs after open/close, a
rejected call is legitimate (VF, port_link_en, multi-function
arbitration, old firmware), and the real carrier comes from the link
events and QUERY_VPORT_STATE, so we log and carry on.
Xin Tian (11):
net/xsc: add Yunsilicon XSC ethernet PCI driver skeleton
net/xsc: add doorbell and cmdq MMIO accessors
net/xsc: add the command queue
net/xsc: query HCA capabilities and reset function resources
net/xsc: add event queues and MSI-X
net/xsc: add CQ and QP helpers
net/xsc: add the ethernet netdev
net/xsc: add the single TX/RX channel
net/xsc: report link state changes
net/xsc: add the transmit path
net/xsc: add the receive path
MAINTAINERS | 7 +
drivers/net/ethernet/Kconfig | 1 +
drivers/net/ethernet/Makefile | 1 +
drivers/net/ethernet/yunsilicon/Kconfig | 27 +
drivers/net/ethernet/yunsilicon/Makefile | 9 +
.../ethernet/yunsilicon/xsc/common/xsc_cmd.h | 560 ++++++++
.../ethernet/yunsilicon/xsc/common/xsc_core.h | 311 +++++
.../ethernet/yunsilicon/xsc/common/xsc_reg.h | 23 +
.../net/ethernet/yunsilicon/xsc/net/Kconfig | 17 +
.../net/ethernet/yunsilicon/xsc/net/Makefile | 9 +
.../net/ethernet/yunsilicon/xsc/net/main.c | 1161 +++++++++++++++++
.../net/ethernet/yunsilicon/xsc/net/xsc_eth.h | 33 +
.../yunsilicon/xsc/net/xsc_eth_common.h | 117 ++
.../ethernet/yunsilicon/xsc/net/xsc_eth_rx.c | 210 +++
.../ethernet/yunsilicon/xsc/net/xsc_eth_tx.c | 87 ++
.../yunsilicon/xsc/net/xsc_eth_txrx.c | 95 ++
.../yunsilicon/xsc/net/xsc_eth_txrx.h | 36 +
.../ethernet/yunsilicon/xsc/net/xsc_eth_wq.c | 80 ++
.../ethernet/yunsilicon/xsc/net/xsc_eth_wq.h | 150 +++
.../ethernet/yunsilicon/xsc/net/xsc_queue.h | 103 ++
.../net/ethernet/yunsilicon/xsc/pci/Kconfig | 16 +
.../net/ethernet/yunsilicon/xsc/pci/Makefile | 9 +
.../net/ethernet/yunsilicon/xsc/pci/adev.c | 133 ++
.../net/ethernet/yunsilicon/xsc/pci/adev.h | 14 +
.../net/ethernet/yunsilicon/xsc/pci/alloc.c | 225 ++++
.../net/ethernet/yunsilicon/xsc/pci/alloc.h | 18 +
drivers/net/ethernet/yunsilicon/xsc/pci/cmd.c | 708 ++++++++++
drivers/net/ethernet/yunsilicon/xsc/pci/cmd.h | 87 ++
drivers/net/ethernet/yunsilicon/xsc/pci/cq.c | 130 ++
drivers/net/ethernet/yunsilicon/xsc/pci/cq.h | 13 +
.../ethernet/yunsilicon/xsc/pci/doorbell.c | 148 +++
drivers/net/ethernet/yunsilicon/xsc/pci/eq.c | 300 +++++
drivers/net/ethernet/yunsilicon/xsc/pci/eq.h | 38 +
.../net/ethernet/yunsilicon/xsc/pci/main.c | 282 ++++
.../net/ethernet/yunsilicon/xsc/pci/pci_irq.c | 345 +++++
.../net/ethernet/yunsilicon/xsc/pci/pci_irq.h | 16 +
drivers/net/ethernet/yunsilicon/xsc/pci/qp.c | 180 +++
drivers/net/ethernet/yunsilicon/xsc/pci/qp.h | 14 +
.../net/ethernet/yunsilicon/xsc/pci/vport.c | 36 +
39 files changed, 5749 insertions(+)
create mode 100644 drivers/net/ethernet/yunsilicon/Kconfig
create mode 100644 drivers/net/ethernet/yunsilicon/Makefile
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/common/xsc_cmd.h
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/common/xsc_core.h
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/common/xsc_reg.h
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/net/Kconfig
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/net/Makefile
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/net/main.c
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth.h
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_common.h
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_rx.c
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_tx.c
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_txrx.c
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_txrx.h
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_wq.c
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_wq.h
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/net/xsc_queue.h
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/Kconfig
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/Makefile
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/adev.c
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/adev.h
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/alloc.c
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/alloc.h
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/cmd.c
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/cmd.h
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/cq.c
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/cq.h
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/doorbell.c
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/eq.c
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/eq.h
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/main.c
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/pci_irq.c
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/pci_irq.h
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/qp.c
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/qp.h
create mode 100644 drivers/net/ethernet/yunsilicon/xsc/pci/vport.c
--
2.43.0
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: [PATCH v2 03/11] net/xsc: add the command queue
2026-11-26 14:57 ` [PATCH v2 03/11] net/xsc: add the command queue Xin Tian
@ 2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:57 ` netdev-bot+sashiko
1 sibling, 0 replies; 33+ messages in thread
From: sashiko-bot @ 2026-11-27 14:58 UTC (permalink / raw)
To: Xin Tian; +Cc: linux-rdma
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] net/xsc: race on cmd->ent_arr in xsc_cmd_invoke() and
xsc_cmd_comp_handler()
- [Medium] net/xsc: broken CQE owner-bit readiness check in
xsc_cmd_cq_polling()
- [Medium] net/xsc: continuous idle MMIO polling in xsc_cmd_cq_polling()
- [Medium] net/xsc: clear cmdq error status before retrying in
xsc_cmd_err_handler()
--
commit 69606c4897d6d499bb3174901050bfe9f0c8a30d
Author: Xin Tian <tianx@yunsilicon.com>
net/xsc: add the command queue
This commit adds the mailbox-based firmware command interface for the
Yunsilicon XSC PCI driver, including command execution, completion
polling, error handling, and command queue version verification.
> 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
[ ... ]
> +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);
> +}
[Severity: High]
Can concurrent callers of xsc_cmd_invoke() race with xsc_cmd_comp_handler()
on cmd->ent_arr[idx]?
xsc_cmd_comp_handler() releases idx back to cmd->cmd_entry_mask via
xsc_free_ent() before cmd->ent_arr[idx] is cleared, and xsc_cmd_invoke()
assigns cmd->ent_arr[ent->idx] = ent without holding cmd->alloc_lock:
CPU0 (cq_task) CPU1 (cmd 1) CPU2 (cmd 2)
xsc_cmd_comp_handler()
xsc_free_ent(cmd, idx)
complete(&ent1->done)
xsc_cmd_invoke()
xsc_alloc_ent() -> idx
spin_lock(&alloc_lock)
if (ent_arr[idx] == ent1)
ent_arr[idx] = NULL
spin_unlock(&alloc_lock)
kfree(ent1)
/* before ent_arr[idx]
* is written, or if
* CPU1 overwrote ent2
* with NULL: */
xsc_cmd_comp_handler()
ent = cmd->ent_arr[idx]
memcpy(ent->out->first, ...) <-- NULL pointer dereference or use-after-free
Also, if a timed-out command completes late, xsc_cmd_comp_handler() frees
its slot in cmd->cmd_entry_mask without freeing ent, ent->in, or ent->out.
Any concurrent command that already passed the cmd_status check in
_xsc_cmd_exec() can then reuse idx and overwrite cmd->ent_arr[idx], leaking
the timed-out entry and its DMA pool buffers.
[ ... ]
> +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;
[ ... ]
> + 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);
> +}
[Severity: Medium]
Should xsc_cmd_err_handler() clear the error status register via
xsc_clear_cmdq_err_status() before ringing the retry doorbell with
xsc_update_cmdq_req_pid()?
Ringing the request doorbell before the write-1-to-clear on the error
status register means the hardware either sees the retry doorbell while
the error bit is still latched, or a fast error on the retried request has
its newly latched error bit immediately cleared by
xsc_clear_cmdq_err_status(xdev, raw), causing the command to time out.
> +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;
> + }
[Severity: Medium]
Does xsc_cmd_cq_polling() need to wake up every 1-2 ms and perform two
PCIe MMIO reads when no commands are in flight?
Even with event-driven interrupts deferred, this kthread runs
continuously for the lifetime of every probed PCI function, preventing
deep CPU idle states and adding a 1-2 ms polling delay to command
completions. Parking the thread on a waitqueue when no commands are
outstanding and waking it from xsc_cmd_invoke() would avoid idle MMIO
polling and poll immediately when a command is submitted.
> +
> + 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;
> + }
[Severity: Medium]
Does learning cmd->owner_bit from rsp right before comparing it against
xsc_rsp_owner_bit(rsp) defeat the buffer readiness check?
On the first response (!cmd->ownerbit_learned), cmd->owner_bit is set to
xsc_rsp_owner_bit(rsp) immediately before testing
cmd->owner_bit != xsc_rsp_owner_bit(rsp), so the check cannot fail. If
the MMIO response PID advances before the CQE DMA write lands in
cmd->cq_buf (which xsc_cmd_init() zeroes with memset()), the polling
thread learns cmd->owner_bit = 0 from the unwritten slot, consumes an
all-zero CQE for index 0, and latches an inverted owner_bit if the
hardware phase bit was 1. Likewise, whenever the expected owner_bit is 0,
zero-initialized slots already match cmd->owner_bit before DMA completes.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261126145711.70868-1-tianx@yunsilicon.com?part=3
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: [PATCH v2 04/11] net/xsc: query HCA capabilities and reset function
2026-11-26 14:57 ` [PATCH v2 04/11] net/xsc: query HCA capabilities and reset function Xin Tian
@ 2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:57 ` netdev-bot+sashiko
1 sibling, 0 replies; 33+ messages in thread
From: sashiko-bot @ 2026-11-27 14:58 UTC (permalink / raw)
To: Xin Tian; +Cc: linux-rdma
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] net/xsc: missing in.hdr.ver initialization in
xsc_cmd_query_hca_cap()
- [Low] net/xsc: wrapped commit subject line spilling into commit
message body
--
commit f750ba7cb78453344d90f6c33acb7a3b7bad17b8
Author: Xin Tian <tianx@yunsilicon.com>
> net/xsc: query HCA capabilities and reset function
>
> resources
>
> Query the firmware HCA capabilities needed by the ethernet driver and
> reset the function resources during probe.
>
> Signed-off-by: Xin Tian <tianx@yunsilicon.com>
[Severity: Low]
The commit subject line appears to have been wrapped mid-phrase, leaving
"resources" as a standalone line at the top of the commit message body.
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/main.c b/drivers/net/ethernet/yunsilicon/xsc/pci/main.c
> index 9b08580f52f38..42c5a1baed23b 100644
> --- a/drivers/net/ethernet/yunsilicon/xsc/pci/main.c
> +++ b/drivers/net/ethernet/yunsilicon/xsc/pci/main.c
> @@ -29,6 +29,63 @@ static int xsc_set_dma_caps(struct pci_dev *pdev)
> return err;
> }
>
> +static int xsc_cmd_query_hca_cap(struct xsc_core_device *xdev,
> + struct xsc_caps *caps)
> +{
> + struct xsc_cmd_query_hca_cap_mbox_out *out;
> + struct xsc_cmd_query_hca_cap_mbox_in in;
> + int err;
> +
> + out = kzalloc_obj(*out, GFP_KERNEL);
> + if (!out)
> + return -ENOMEM;
> +
> + memset(&in, 0, sizeof(in));
> + in.hdr.opcode = cpu_to_be16(XSC_CMD_OP_QUERY_HCA_CAP);
> + in.cpu_num = cpu_to_be16(num_online_cpus());
> +
> + err = xsc_cmd_exec(xdev, &in, sizeof(in), out, sizeof(*out));
> + if (err)
> + goto err_free;
[ ... ]
> + xdev->pcie_no = out->hca_cap.pcie_no;
> + xdev->pf_id = out->hca_cap.pf_id;
[Severity: Medium]
Should in.hdr.ver be initialized to cpu_to_be16(XSC_CMD_QUERY_HCA_CAP_V1)
before calling xsc_cmd_exec()?
XSC_CMD_QUERY_HCA_CAP_V1 is defined in
drivers/net/ethernet/yunsilicon/xsc/common/xsc_cmd.h as the HCA capability
mailbox version, and xsc_cmd_query_hca_cap() reads out->hca_cap.pcie_no and
out->hca_cap.pf_id from the versioned extension section of struct
xsc_hca_cap. Leaving in.hdr.ver at 0 could cause firmware that checks the
request version to reject the query or leave those fields unpopulated.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261126145711.70868-1-tianx@yunsilicon.com?part=4
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: [PATCH v2 05/11] net/xsc: add event queues and MSI-X
2026-11-26 14:57 ` [PATCH v2 05/11] net/xsc: add event queues and MSI-X Xin Tian
@ 2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:57 ` netdev-bot+sashiko
1 sibling, 0 replies; 33+ messages in thread
From: sashiko-bot @ 2026-11-27 14:58 UTC (permalink / raw)
To: Xin Tian; +Cc: linux-rdma
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] net/xsc: invalid vmap() of dma_alloc_coherent() pages in
xsc_buf_alloc()
--
commit 73274bfd54f18b8487a549dce31abd6f4b58e46d
Author: Xin Tian <tianx@yunsilicon.com>
net/xsc: add event queues and MSI-X
This commit adds asynchronous and completion event queues, allocates MSI-X
vectors, and dispatches link events from a workqueue.
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/alloc.c b/drivers/net/ethernet/yunsilicon/xsc/pci/alloc.c
> new file mode 100644
> index 0000000000000..46eb0b031511d
> --- /dev/null
> +++ b/drivers/net/ethernet/yunsilicon/xsc/pci/alloc.c
> @@ -0,0 +1,123 @@
[ ... ]
> +int xsc_buf_alloc(struct xsc_core_device *xdev,
> + unsigned long size,
> + unsigned long max_direct,
> + struct xsc_buf *buf)
> +{
> + dma_addr_t t;
> +
> + buf->size = size;
> + if (size <= max_direct) {
[ ... ]
> + } else {
> + struct page **pages;
> + unsigned int i;
> +
> + buf->direct.buf = NULL;
> + buf->nbufs = DIV_ROUND_UP(size, PAGE_SIZE);
> + buf->npages = buf->nbufs;
> + buf->page_shift = PAGE_SHIFT;
> + buf->page_list = kcalloc(buf->nbufs, sizeof(*buf->page_list),
> + GFP_KERNEL);
> + if (!buf->page_list)
> + return -ENOMEM;
> +
> + for (i = 0; i < buf->nbufs; i++) {
> + buf->page_list[i].buf =
> + dma_alloc_coherent(&xdev->pdev->dev, PAGE_SIZE,
> + &t, GFP_KERNEL);
> + if (!buf->page_list[i].buf)
> + goto err_free;
> +
> + buf->page_list[i].map = t;
> + }
> +
> + pages = kmalloc_array(buf->nbufs, sizeof(*pages), GFP_KERNEL);
> + if (!pages)
> + goto err_free;
> +
> + for (i = 0; i < buf->nbufs; i++) {
> + void *addr = buf->page_list[i].buf;
> +
> + if (is_vmalloc_addr(addr))
> + pages[i] = vmalloc_to_page(addr);
> + else
> + pages[i] = virt_to_page(addr);
> + }
> + buf->direct.buf = vmap(pages, buf->nbufs, VM_MAP, PAGE_KERNEL);
> + kfree(pages);
> + if (!buf->direct.buf)
> + goto err_free;
> + }
[Severity: High]
Is it valid to call virt_to_page() or vmalloc_to_page() and vmap(...,
PAGE_KERNEL) on memory returned by dma_alloc_coherent()?
Coherent DMA allocations are not guaranteed to be backed by normal linear
or vmalloc struct pages, and on non-coherent architectures or confidential
VMs remapping coherent DMA pages with PAGE_KERNEL creates a conflicting
cacheable or encrypted alias. This branch is hit for every completion EQ
on 4K-page kernels (alloc_comp_eqs() -> xsc_create_map_eq() ->
xsc_buf_alloc() with size = 16384 and max_direct = PAGE_SIZE).
Also, when buf->nbufs > 1, xsc_buf_offset() and xsc_fill_page_array() use
buf->page_list directly and never touch buf->direct.buf, so the vmap()
here and the paired vunmap() in xsc_buf_free() can be dropped altogether.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261126145711.70868-1-tianx@yunsilicon.com?part=5
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: [PATCH v2 06/11] net/xsc: add CQ and QP helpers
2026-11-26 14:57 ` [PATCH v2 06/11] net/xsc: add CQ and QP helpers Xin Tian
@ 2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:57 ` netdev-bot+sashiko
1 sibling, 0 replies; 33+ messages in thread
From: sashiko-bot @ 2026-11-27 14:58 UTC (permalink / raw)
To: Xin Tian; +Cc: linux-rdma
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] net/xsc: xsc_core_eth_destroy_cq() skips waiting on xcq->free
on error
--
commit e696a8de856ac33999eafa7adfe7de1ccd665f4e
Author: Xin Tian <tianx@yunsilicon.com>
net/xsc: add CQ and QP helpers
This commit adds CQ and QP radix-tree tables, helpers to create, modify,
and destroy ethernet CQs and QPs, and EQ dispatching for completion and
error events.
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/cq.c b/drivers/net/ethernet/yunsilicon/xsc/pci/cq.c
> new file mode 100644
> index 0000000000000..a4f9b488d659d
> --- /dev/null
> +++ b/drivers/net/ethernet/yunsilicon/xsc/pci/cq.c
[ ... ]
> +int xsc_core_eth_destroy_cq(struct xsc_core_device *xdev,
> + struct xsc_core_cq *xcq)
> +{
> + struct xsc_cq_table *table = &xdev->cq_table;
> + struct xsc_core_cq *tmp;
> + int err;
> +
> + spin_lock_irq(&table->lock);
> + tmp = radix_tree_delete(&table->tree, xcq->cqn);
> + spin_unlock_irq(&table->lock);
> + if (!tmp) {
> + err = -ENOENT;
> + goto err_delete_cq;
> + }
> +
> + if (tmp != xcq) {
> + err = -EINVAL;
> + goto err_delete_cq;
> + }
> +
> + /* the completion reader looks the cq up under rcu_read_lock and
> + * takes its reference afterwards; wait for it before dropping
> + * the tree reference so it never touches a freed cq
> + */
> + synchronize_rcu();
> +
> + err = xsc_destroy_cq(xdev, xcq->cqn);
> + if (err)
> + goto err_destroy_cq;
> +
> + if (atomic_dec_and_test(&xcq->refcount))
> + complete(&xcq->free);
> + wait_for_completion(&xcq->free);
> + return 0;
[Severity: Medium]
If xsc_destroy_cq() fails, can xsc_core_eth_destroy_cq() return while an
in-flight xsc_cq_completion() or xsc_eq_cq_event() callback is still using
xcq?
Both xsc_cq_completion() and xsc_eq_cq_event() drop rcu_read_unlock() or
table->lock after incrementing cq->refcount and before calling cq->comp()
or cq->event(), so synchronize_rcu() does not wait for those callbacks to
finish. Jumping to err_destroy_cq after xcq has already been deleted from
table->tree skips waiting on xcq->free, allowing the caller to zero or
free the enclosing structure while a callback is still running:
CPU0 CPU1
xsc_core_eth_destroy_cq()
radix_tree_delete(..., xcq->cqn)
xsc_cq_completion()
rcu_read_lock()
/* looked up before delete */
atomic_inc(&cq->refcount)
rcu_read_unlock()
synchronize_rcu()
xsc_destroy_cq() -> fails
goto err_destroy_cq
/* caller zeroes or frees xcq */
cq->comp(cq) <-- use-after-free
Dropping the initial refcount and waiting on xcq->free right after
synchronize_rcu(), before xsc_destroy_cq(), would ensure all in-flight
callbacks have finished even if the firmware command fails.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261126145711.70868-1-tianx@yunsilicon.com?part=6
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: [PATCH v2 07/11] net/xsc: add the ethernet netdev
2026-11-26 14:57 ` [PATCH v2 07/11] net/xsc: add the ethernet netdev Xin Tian
@ 2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:57 ` netdev-bot+sashiko
1 sibling, 0 replies; 33+ messages in thread
From: sashiko-bot @ 2026-11-27 14:58 UTC (permalink / raw)
To: Xin Tian; +Cc: linux-rdma
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] net/xsc: missing select AUXILIARY_BUS in Kconfig for XSC PCI
and ETH
--
commit a8648ed654545f6dce03a1a5471a356b495435e2
Author: Xin Tian <tianx@yunsilicon.com>
net/xsc: add the ethernet netdev
This commit registers the Yunsilicon XSC ethernet auxiliary device and
driver, sets up the netdev, and programs its MAC address, MTU, and
open/close callbacks.
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/Kconfig b/drivers/net/ethernet/yunsilicon/xsc/net/Kconfig
> new file mode 100644
> index 0000000000000..7428b26295b66
> --- /dev/null
> +++ b/drivers/net/ethernet/yunsilicon/xsc/net/Kconfig
> @@ -0,0 +1,17 @@
> +# SPDX-License-Identifier: GPL-2.0
> +# Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
> +# All rights reserved.
> +# Yunsilicon driver configuration
> +#
> +
> +config YUNSILICON_XSC_ETH
> + tristate "Yunsilicon XSC ethernet driver"
> + depends on YUNSILICON_XSC_PCI
> + depends on NET
> + select PAGE_POOL
[Severity: Medium]
Should YUNSILICON_XSC_PCI (and YUNSILICON_XSC_ETH) select AUXILIARY_BUS?
This commit adds auxiliary device registration in xsc_pci (xsc_adev_add()
calling auxiliary_device_init() and auxiliary_device_add()) and auxiliary
driver registration in xsc_eth (module_auxiliary_driver()). Because
AUXILIARY_BUS is a hidden Kconfig symbol without stubs when disabled,
configs enabling YUNSILICON_XSC_PCI or YUNSILICON_XSC_ETH without another
driver selecting AUXILIARY_BUS will fail to link with undefined references
to auxiliary_device_init(), __auxiliary_device_add(),
__auxiliary_driver_register(), and auxiliary_driver_unregister().
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261126145711.70868-1-tianx@yunsilicon.com?part=7
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: [PATCH v2 08/11] net/xsc: add the single TX/RX channel
2026-11-26 14:57 ` [PATCH v2 08/11] net/xsc: add the single TX/RX channel Xin Tian
@ 2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:58 ` netdev-bot+sashiko
1 sibling, 0 replies; 33+ messages in thread
From: sashiko-bot @ 2026-11-27 14:58 UTC (permalink / raw)
To: Xin Tian; +Cc: linux-rdma
Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] net/xsc: return -ENOMEM on di alloc failure in
xsc_eth_alloc_rq()
- [Medium] net/xsc: disable NIC HCA and port before queue teardown in
xsc_eth_close()
- [Medium] net/xsc: WQ DMA buffers freed on xsc_core_eth_destroy_qp()
rollback failure
- [Medium] net/xsc: unsafe CQ teardown and memset on failure in
xsc_eth_close_channel()
- [Medium] net/xsc: unsynchronized set_dev_node() in
xsc_dma_zalloc_coherent_node()
--
commit 72a1cf783f0fa5afa01bbb9581c2a8a19828e758
Author: Xin Tian <tianx@yunsilicon.com>
net/xsc: add the single TX/RX channel
This commit brings up a single TX/RX channel on the netdev by building queue
parameters, creating the CQs, SQ, and RQ, enabling the NIC HCA, and adding
the queue ring and fragmented DMA buffer helpers.
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/main.c b/drivers/net/ethernet/yunsilicon/xsc/net/main.c
> index b9e75d7e14e78..1c7bbc27d8c4f 100644
> --- a/drivers/net/ethernet/yunsilicon/xsc/net/main.c
> +++ b/drivers/net/ethernet/yunsilicon/xsc/net/main.c
> @@ -25,6 +25,774 @@ static void xsc_eth_netdev_init(struct xsc_adapter *adapter)
[ ... ]
> +static int xsc_eth_open_qp_sq(struct xsc_channel *c,
> + struct xsc_sq *psq,
> + struct xsc_sq_param *psq_param)
> +{
[ ... ]
> +err_sq_modify_in_destroy:
> + kvfree(modify_in);
> +
> +err_sq_common_destroy:
> + xsc_free_qp_sq_db(psq);
> +
> + xsc_core_destroy_resource_common(xdev, &psq->cqp);
> +
> +err_sq_destroy:
> + xsc_core_eth_destroy_qp(xdev, psq->cqp.qpn);
> +
> +err_sq_in_destroy:
> + kvfree(in);
> +
> +err_sq_wq_destroy:
> + xsc_eth_wq_destroy(&psq->wq_ctrl);
> + return ret;
> +}
[Severity: Medium]
Can this free psq->wq_ctrl while the SQ is still live in firmware if
xsc_core_eth_destroy_qp() fails during error rollback? Unlike
xsc_eth_close_qp_sq() and xsc_eth_close_qp_rq(), which leak the WQ buffer
when DESTROY_QP fails so the device cannot DMA into freed pages,
err_sq_destroy here and err_destroy_qp in xsc_eth_open_qp_rq() ignore the
return value of xsc_core_eth_destroy_qp() and unconditionally free the WQ
DMA buffers (and prq->page_pool in the RQ case).
[ ... ]
> +static int xsc_eth_alloc_rq(struct xsc_channel *c,
> + struct xsc_rq *prq,
> + struct xsc_rq_param *prq_param)
> +{
> + u8 ele_log_size = prq_param->rq_attr.ele_log_size;
> + struct page_pool_params pagepool_params = {0};
> + u8 q_log_size = prq_param->rq_attr.q_log_size;
> + u32 wq_sz;
> + int ret = 0;
> + int i;
> +
> + prq_param->wq.db_numa_node = cpu_to_node(c->cpu);
> +
> + ret = xsc_eth_wq_cyc_create(c->adapter->xdev, &prq_param->wq,
> + q_log_size, ele_log_size, &prq->wqe.wq,
> + &prq->wq_ctrl);
> + if (ret)
> + return ret;
> +
> + wq_sz = xsc_wq_cyc_get_size(&prq->wqe.wq);
> + prq->wqe.di = kvzalloc_node(array_size(wq_sz, sizeof(*prq->wqe.di)),
> + GFP_KERNEL, cpu_to_node(c->cpu));
> + if (!prq->wqe.di)
> + goto err_destroy_wq;
[ ... ]
> +err_free_di:
> + kvfree(prq->wqe.di);
> +err_destroy_wq:
> + xsc_eth_wq_destroy(&prq->wq_ctrl);
> + return ret;
> +}
[Severity: Medium]
Does xsc_eth_alloc_rq() return 0 when kvzalloc_node() fails to allocate
prq->wqe.di? Because ret is still 0 from xsc_eth_wq_cyc_create(), jumping
to err_destroy_wq frees prq->wq_ctrl.buf and returns success to
xsc_eth_open_qp_rq(), which then reads the freed prq->wq_ctrl.buf.frags in
xsc_core_fill_page_frag_array() and double frees prq->wq_ctrl on teardown.
[ ... ]
> +static void xsc_eth_close_channel(struct xsc_channel *c, bool free_rq)
> +{
> + if (free_rq)
> + xsc_eth_close_qp_rq(c, &c->qp.rq);
> + xsc_eth_close_cq(c, &c->qp.rq.cq);
> + memset(&c->qp.rq, 0, sizeof(struct xsc_rq));
> +
> + xsc_eth_close_qp_sq(c, &c->qp.sq);
> + xsc_eth_close_cq(c, &c->qp.sq.cq);
> + memset(&c->qp.sq, 0, sizeof(struct xsc_sq));
> +}
[Severity: Medium]
What happens in xsc_eth_close_channel() if xsc_core_eth_destroy_qp() or
xsc_core_eth_destroy_cq() fails? When QP destruction fails in
xsc_eth_close_qp_rq() or xsc_eth_close_qp_sq(), the QP remains live in
firmware, yet xsc_eth_close_channel() still destroys and frees its bound CQ.
In addition, when xsc_core_eth_destroy_cq() fails, xsc_destroy_cq() returns
before waiting on xcq->free for in-flight event or completion handlers, and
the unconditional memset() zeroes xcq->free, xcq->refcount, and cq->xdev
while a handler can still be using them.
[ ... ]
> @@ -45,25 +813,46 @@ static void xsc_set_port_admin_status(struct xsc_adapter *adapter,
> static int xsc_eth_open(struct net_device *netdev)
> {
> struct xsc_adapter *adapter = netdev_priv(netdev);
> + int ret;
> +
> + ret = xsc_eth_sw_init(adapter);
> + if (ret)
> + return ret;
> +
> + ret = xsc_eth_enable_nic_hca(adapter);
> + if (ret)
> + goto err_sw_deinit;
>
> adapter->opened = true;
>
> xsc_set_port_admin_status(adapter, XSC_PORT_UP);
>
> return 0;
> +
> +err_sw_deinit:
> + xsc_eth_sw_deinit(adapter);
> + return ret;
> }
>
> static int xsc_eth_close(struct net_device *netdev)
> {
> struct xsc_adapter *adapter = netdev_priv(netdev);
> + int ret;
>
> adapter->opened = false;
>
> netif_carrier_off(adapter->netdev);
>
> + xsc_eth_sw_deinit(adapter);
> +
> + ret = xsc_eth_disable_nic_hca(adapter);
> + if (ret)
> + netdev_err(adapter->netdev, "failed to disable nic hca, err=%d\n",
> + ret);
> +
> xsc_set_port_admin_status(adapter, XSC_PORT_DOWN);
>
> - return 0;
> + return ret;
> }
[Severity: Medium]
Should xsc_eth_close() bring the port down and disable the NIC HCA before
calling xsc_eth_sw_deinit()? Right now xsc_eth_sw_deinit() destroys the RQ,
SQ, and CQs and frees their DMA buffers and page_pool while the port is
still admin-up and the NIC HCA is still steering incoming packets to
adapter->channel.qp.rq.rqn.
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/alloc.c b/drivers/net/ethernet/yunsilicon/xsc/pci/alloc.c
> index 46eb0b031511d..0b0478c64be3b 100644
> --- a/drivers/net/ethernet/yunsilicon/xsc/pci/alloc.c
> +++ b/drivers/net/ethernet/yunsilicon/xsc/pci/alloc.c
> @@ -121,3 +121,105 @@ void xsc_fill_page_array(struct xsc_buf *buf, __be64 *pas, unsigned int npages)
[ ... ]
> +static void *xsc_dma_zalloc_coherent_node(struct xsc_core_device *xdev,
> + size_t size, dma_addr_t *dma_handle,
> + int node)
> +{
> + struct device *device = &xdev->pdev->dev;
> + int original_node;
> + void *cpu_handle;
> +
> + /* WA for kernels that don't use numa_mem_id in alloc_pages_node */
> + if (node == NUMA_NO_NODE)
> + node = numa_mem_id();
> +
> + original_node = dev_to_node(device);
> + set_dev_node(device, node);
> + cpu_handle = dma_alloc_coherent(device, size, dma_handle, GFP_KERNEL);
> + set_dev_node(device, original_node);
> +
> + return cpu_handle;
> +}
[Severity: Medium]
Can xsc_dma_zalloc_coherent_node() use dma_alloc_coherent_node() instead of
mutating xdev->pdev->dev.numa_node with set_dev_node()? Overwriting the
shared device's NUMA node across a sleeping dma_alloc_coherent() call
without locking exposes the temporary node to concurrent readers of
dev_to_node(&xdev->pdev->dev), and two concurrent callers of
xsc_core_frag_buf_alloc_node() can save the overwritten value as
original_node and permanently corrupt the device's NUMA node.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261126145711.70868-1-tianx@yunsilicon.com?part=8
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: [PATCH v2 09/11] net/xsc: report link state changes
2026-11-26 14:57 ` [PATCH v2 09/11] net/xsc: report link state changes Xin Tian
@ 2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:58 ` netdev-bot+sashiko
1 sibling, 0 replies; 33+ messages in thread
From: sashiko-bot @ 2026-11-27 14:58 UTC (permalink / raw)
To: Xin Tian; +Cc: linux-rdma
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] net/xsc: unsynchronized carrier updates in
xsc_eth_link_event_handler()
--
commit e468bd52287d3b4539c22501cb440d036af1c666
Author: Xin Tian <tianx@yunsilicon.com>
net/xsc: report link state changes
This commit queries virtual port state to update the netdev carrier and
registers an event handler for PCI link state change notifications.
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/main.c b/drivers/net/ethernet/yunsilicon/xsc/net/main.c
> index 1c7bbc27d8c4f..75a6abd6ed136 100644
> --- a/drivers/net/ethernet/yunsilicon/xsc/net/main.c
> +++ b/drivers/net/ethernet/yunsilicon/xsc/net/main.c
> @@ -721,6 +721,62 @@ static void xsc_eth_sw_deinit(struct xsc_adapter *adapter)
> xsc_eth_close_channels(adapter);
> }
>
> +static int xsc_eth_get_link_status(struct xsc_adapter *adapter, bool *link_up)
> +{
> + struct xsc_core_device *xdev = adapter->xdev;
> + u8 state;
> + int err;
> +
> + err = xsc_core_query_vport_state(xdev, 0, &state);
> + if (err)
> + return err;
> +
> + *link_up = state;
> + return 0;
> +}
> +
> +static int xsc_eth_change_link_status(struct xsc_adapter *adapter)
> +{
> + bool link_up;
> + int err;
> +
> + err = xsc_eth_get_link_status(adapter, &link_up);
> + if (err) {
> + netdev_err(adapter->netdev, "failed to query link status, err=%d\n",
> + err);
> + return err;
> + }
> +
> + if (link_up && !netif_carrier_ok(adapter->netdev)) {
> + netdev_info(adapter->netdev, "Link up\n");
> + netif_carrier_on(adapter->netdev);
> + } else if (!link_up && netif_carrier_ok(adapter->netdev)) {
> + netdev_info(adapter->netdev, "Link down\n");
> + netif_carrier_off(adapter->netdev);
> + }
> +
> + return 0;
> +}
> +
> +/*
> + * Registered with the PCI layer, which runs it on its own workqueue for
> + * every XSC_CMD_EVENT_RESP_CHANGE_LINK event.
> + */
> +static void xsc_eth_link_event_handler(void *arg)
> +{
> + struct xsc_core_device *xdev = arg;
> + struct xsc_adapter *adapter = xdev->eth_priv;
> + int err;
> +
> + if (!adapter || !adapter->opened)
> + return;
> +
> + err = xsc_eth_change_link_status(adapter);
> + if (err)
> + netdev_err(adapter->netdev, "failed to change linkstatus, err=%d\n",
> + err);
> +}
> +
> static bool xsc_get_pct_drop_config(struct xsc_core_device *xdev)
> {
> return (xdev->pdev->device == XSC_MC_PF_DEV_ID) ||
> @@ -813,6 +869,7 @@ static void xsc_set_port_admin_status(struct xsc_adapter *adapter,
> static int xsc_eth_open(struct net_device *netdev)
> {
> struct xsc_adapter *adapter = netdev_priv(netdev);
> + bool link_up;
> int ret;
>
> ret = xsc_eth_sw_init(adapter);
> @@ -823,6 +880,16 @@ static int xsc_eth_open(struct net_device *netdev)
> if (ret)
> goto err_sw_deinit;
>
> + ret = xsc_eth_get_link_status(adapter, &link_up);
> + if (ret) {
> + netdev_warn(netdev, "failed to query link status, err=%d\n", ret);
> + } else if (link_up) {
> + netdev_info(netdev, "Link up\n");
> + netif_carrier_on(adapter->netdev);
> + } else {
> + netdev_info(netdev, "Link down\n");
> + }
> +
> adapter->opened = true;
>
> xsc_set_port_admin_status(adapter, XSC_PORT_UP);
[Severity: Medium]
Can xsc_eth_link_event_handler() race with xsc_eth_close() and
xsc_eth_open() and leave the netdev carrier state out of sync?
xsc_eth_link_event_handler() checks adapter->opened without locking and
then sleeps in xsc_core_query_vport_state() -> xsc_cmd_exec(), while
xsc_eth_close() clears adapter->opened and calls netif_carrier_off()
without waiting for in-flight link event work:
drivers/net/ethernet/yunsilicon/xsc/net/main.c:xsc_eth_close() {
adapter->opened = false;
netif_carrier_off(adapter->netdev);
...
}
CPU0 (event_wq) CPU1
xsc_eth_link_event_handler()
if (!adapter->opened)
xsc_eth_change_link_status()
xsc_eth_get_link_status()
/* sleeps in cmd exec */
xsc_eth_close()
adapter->opened = false;
netif_carrier_off(netdev);
netif_carrier_on(netdev);
/* carrier enabled while closed */
Because xsc_eth_open() only calls netif_carrier_on() when link_up is true
and does not call netif_carrier_off() in the else branch, if the physical
link goes down while the interface is closed, a subsequent xsc_eth_open()
logs "Link down" but leaves netif_carrier_ok() true.
Also in xsc_eth_open(), xsc_eth_get_link_status() runs before
adapter->opened = true and before xsc_set_port_admin_status(adapter,
XSC_PORT_UP). That queries the vport while it is still administratively
down from a prior close, and any link change event arriving between
xsc_eth_get_link_status() and adapter->opened = true is dropped by
xsc_eth_link_event_handler().
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261126145711.70868-1-tianx@yunsilicon.com?part=9
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: [PATCH v2 10/11] net/xsc: add the transmit path
2026-11-26 14:57 ` [PATCH v2 10/11] net/xsc: add the transmit path Xin Tian
@ 2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:58 ` netdev-bot+sashiko
1 sibling, 0 replies; 33+ messages in thread
From: sashiko-bot @ 2026-11-27 14:58 UTC (permalink / raw)
To: Xin Tian; +Cc: linux-rdma
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] net/xsc: lockless TX queue stop/wake race in xsc_eth_xmit()
- [Medium] net/xsc: xsc_eth_xmit() returns NETDEV_TX_BUSY without
stopping TX queue
- [Medium] net/xsc: xsc_poll_tx_cq() frees completed TX skbs with
dev_kfree_skb_any()
- [Medium] net/xsc: unconditional CQ doorbell MMIO write in
xsc_poll_tx_cq()
--
commit a6c0dbbfbb297d8daa2d0367ab92f524118727e6
Author: Xin Tian <tianx@yunsilicon.com>
net/xsc: add the transmit path
This commit adds the transmit path for the xsc Ethernet driver, including
posting linear skbs to the send queue, ringing the TX doorbell, and
polling the TX completion queue.
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_tx.c b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_tx.c
> new file mode 100644
> index 0000000000000..580af1f231056
> --- /dev/null
> +++ b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_tx.c
> @@ -0,0 +1,87 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/* Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
> + * All rights reserved.
> + */
> +
> +#include "xsc_eth.h"
> +#include "xsc_eth_txrx.h"
> +
> +#define XSC_OPCODE_RAW 7
> +
> +netdev_tx_t xsc_eth_xmit(struct sk_buff *skb, struct net_device *netdev)
> +{
> + struct xsc_adapter *adapter = netdev_priv(netdev);
> + struct xsc_core_device *xdev = adapter->xdev;
> + struct xsc_send_wqe_ctrl_seg *cseg;
> + struct xsc_tx_wqe_info *wi;
> + struct xsc_sq *sq = &adapter->channel.qp.sq;
> + struct xsc_tx_wqe *wqe;
> + u32 ds_num_log = ilog2(xdev->caps.send_ds_num);
> + dma_addr_t dma_addr;
> + u16 pi;
> +
> + if (!adapter->opened)
> + return NETDEV_TX_BUSY;
[Severity: Medium]
Does returning NETDEV_TX_BUSY here without stopping sq->txq cause a
busy-requeue loop in sch_direct_xmit()?
In xsc_eth_open(), xsc_eth_sw_init() starts sq->txq and
netif_carrier_on() runs before adapter->opened is set to true, and in
xsc_eth_close(), adapter->opened is cleared before xsc_eth_sw_deinit()
disables the TX queue:
drivers/net/ethernet/yunsilicon/xsc/net/main.c:xsc_eth_close() {
adapter->opened = false;
netif_carrier_off(adapter->netdev);
xsc_eth_sw_deinit(adapter);
...
}
While sq->txq is running and adapter->opened is false, returning
NETDEV_TX_BUSY causes sch_direct_xmit() to requeue skb and immediately
reschedule the qdisc via __netif_schedule(). Also, if earlier packets in
a batch were posted with netdev_xmit_more() true and adapter->opened
becomes false before the last packet of the batch, returning early here
skips xsc_update_tx_db() for the already-posted WQEs.
> +
> + if (unlikely(skb_is_nonlinear(skb) || skb->len == 0 ||
> + skb->len > sq->hw_mtu))
> + goto err_drop;
> +
> + pi = xsc_wq_cyc_ctr2ix(&sq->wq, sq->pc);
> + wqe = xsc_wq_cyc_get_wqe(&sq->wq, pi);
> + memset(wqe, 0, xdev->caps.send_ds_num * XSC_SEND_WQE_DS);
> +
> + dma_addr = dma_map_single(sq->channel->adapter->dev, skb->data,
> + skb->len, DMA_TO_DEVICE);
> + if (unlikely(dma_mapping_error(sq->channel->adapter->dev, dma_addr)))
> + goto err_drop;
> +
> + wi = &sq->wqe_info[pi];
> + wi->skb = skb;
> + wi->dma_addr = dma_addr;
> +
> + cseg = &wqe->ctrl;
> + cseg->data0 = cpu_to_le32(FIELD_PREP(XSC_WQE_CTRL_SEG_DATA0_MSG_OPCODE_MASK,
> + XSC_OPCODE_RAW) |
> + FIELD_PREP(XSC_WQE_CTRL_SEG_DATA0_DS_DATA_NUM_MASK,
> + 1) |
> + FIELD_PREP(XSC_WQE_CTRL_SEG_DATA0_WQE_ID_MASK,
> + sq->pc << ds_num_log));
> + cseg->msg_len = cpu_to_le32(skb->len);
> + cseg->data3 = cpu_to_le32(XSC_WQE_CTRL_SEG_DATA3_CE);
> + wqe->data[0].data0 =
> + cpu_to_le32(FIELD_PREP(XSC_WQE_DATA_SEG_DATA0_SEG_LEN_MASK,
> + skb->len));
> + wqe->data[0].va = cpu_to_le64(dma_addr);
> +
> + sq->pc++;
> + if (unlikely(!xsc_wqc_has_room_for(&sq->wq, sq->cc, sq->pc,
> + sq->stop_room)))
> + netif_tx_stop_queue(sq->txq);
[Severity: High]
Can the lockless stop/wake protocol between xsc_eth_xmit() and
xsc_poll_tx_cq() race to either wake a full ring or leave sq->txq
permanently stopped?
xsc_eth_xmit() does not barrier and re-check xsc_wqc_has_room_for() after
netif_tx_stop_queue(), and xsc_poll_tx_cq() updates sq->cc and checks
netif_tx_queue_stopped(sq->txq) and sq->pc without memory barriers (even
when 0 CQEs were reaped):
CPU0 (xsc_eth_xmit) CPU1 (xsc_poll_tx_cq)
------------------- ---------------------
/* 1 slot left:
* pc - cc == sz - 1
*/
/* 0 CQEs reaped; loads old
* sq->pc before CPU0's store
* is visible
*/
sq->pc++;
/* pc - cc == sz (full) */
netif_tx_stop_queue(sq->txq);
if (netif_tx_queue_stopped(sq->txq) &&
xsc_wqc_has_room_for(..., old_pc, ...))
netif_tx_wake_queue(sq->txq);
<-- wakes full SQ
Because xsc_eth_xmit() does not check xsc_wqc_has_room_for() at entry,
the next xsc_eth_xmit() call indexes pi at sq->cc, overwriting the
in-flight WQE and sq->wqe_info[pi] (leaking wi->skb and wi->dma_addr) and
wrapping sq->pc past sq->cc + sz so the queue stays awake.
Conversely, when CPU1 reaps completions and writes sq->cc = sqcc while
CPU0 reads the old sq->cc and stops the queue, the lack of a store-load
barrier after sq->cc = sqcc on CPU1 and after netif_tx_stop_queue() on
CPU0 allows CPU1 to observe netif_tx_queue_stopped(sq->txq) == false and
skip waking the queue, leaving sq->txq stopped with free descriptors.
Using netif_txq_maybe_stop() in xsc_eth_xmit() and only checking for wake
when i > 0 after smp_mb() in xsc_poll_tx_cq() would close both races.
[ ... ]
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_txrx.c b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_txrx.c
> new file mode 100644
> index 0000000000000..ee6554ed80040
> --- /dev/null
> +++ b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_txrx.c
> @@ -0,0 +1,70 @@
[ ... ]
> +int xsc_poll_tx_cq(struct xsc_cq *cq, int budget)
> +{
> + struct xsc_cqe *cqe;
> + struct device *dev;
> + struct xsc_sq *sq;
> + u16 sqcc;
> + int i = 0;
> +
> + sq = container_of(cq, struct xsc_sq, cq);
> + if (!test_bit(XSC_ETH_SQ_STATE_ENABLED, &sq->state))
> + return 0;
> +
> + dev = sq->channel->adapter->dev;
> + sqcc = sq->cc;
> +
> + while (i < budget) {
> + struct xsc_tx_wqe_info *wi;
> + struct sk_buff *skb;
> +
> + cqe = xsc_cqwq_get_cqe(&cq->wq);
> + if (!cqe)
> + break;
> + /* ensure cqe content is read after cqe ownership bit */
> + dma_rmb();
> +
> + xsc_cqwq_pop(&cq->wq);
> + wi = &sq->wqe_info[xsc_wq_cyc_ctr2ix(&sq->wq, sqcc)];
> +
> + skb = wi->skb;
> + wi->skb = NULL;
> + if (skb) {
> + dma_unmap_single(dev, wi->dma_addr, skb->len,
> + DMA_TO_DEVICE);
> + dev_kfree_skb_any(skb);
> + }
[Severity: Medium]
Should xsc_poll_tx_cq() free completed TX packets with
napi_consume_skb(skb, budget) instead of dev_kfree_skb_any(skb)?
dev_kfree_skb_any() records SKB_DROP_REASON_NOT_SPECIFIED instead of
SKB_CONSUMED, which reports every transmitted packet as a drop in
skb:kfree_skb tracepoints and drop_monitor, and bypasses NAPI bulk skb
freeing.
> +
> + sqcc++;
> + i++;
> + }
> +
> + xsc_update_cq_ci(cq->xdev, cq->xcq.cqn, cq->wq.cc);
> +
> + sq->cc = sqcc;
[Severity: Medium]
When no TX completions are reaped (i == 0), does xsc_poll_tx_cq() still
issue an unconditional doorbell MMIO write?
xsc_update_cq_ci() executes a wmb() and a writel() to the PCIe BAR, and
sq->cc = sqcc dirties the sq->cc cacheline on every NAPI poll even when
the TX completion queue was empty. Guarding the doorbell update, sq->cc
write, and wake check on i > 0 avoids the barrier and MMIO write on
idle-TX polls.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261126145711.70868-1-tianx@yunsilicon.com?part=10
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: [PATCH v2 11/11] net/xsc: add the receive path
2026-11-26 14:57 ` [PATCH v2 11/11] net/xsc: add the receive path Xin Tian
@ 2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:58 ` netdev-bot+sashiko
1 sibling, 0 replies; 33+ messages in thread
From: sashiko-bot @ 2026-11-27 14:58 UTC (permalink / raw)
To: Xin Tian; +Cc: linux-rdma
Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [High] net/xsc: missing RX doorbell on partial alloc in
xsc_eth_post_rx_wqes()
- [High] net/xsc: missing RQ state check in xsc_eth_post_rx_wqes() and
early NAPI enable
- [Medium] net/xsc: CQs armed before NAPI init in xsc_eth_open_cq() and
open_channel()
- [Medium] net/xsc: netif_napi_del() called before closing CQs in
xsc_eth_close_channel()
- [Medium] net/xsc: missing pagepool_params.napi assignment in
xsc_eth_alloc_rq()
- [Medium] net/xsc: wrong prefetch offset and DMA sync length in
xsc_skb_from_cqe()
- [Medium] net/xsc: unconditional TX CQ doorbell MMIO write in
xsc_poll_tx_cq()
--
commit 5c867b6dd2012f64299cb03411c3483a056f2b36
Author: Xin Tian <tianx@yunsilicon.com>
net/xsc: add the receive path
This commit adds the receive path for the xsc ethernet driver, building
linear skbs from page_pool buffers and polling both TX and RX completion
queues from a single NAPI instance.
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/main.c b/drivers/net/ethernet/yunsilicon/xsc/net/main.c
> index 8eda3c8b12f25..e7b0871c1ccb7 100644
> --- a/drivers/net/ethernet/yunsilicon/xsc/net/main.c
> +++ b/drivers/net/ethernet/yunsilicon/xsc/net/main.c
> @@ -106,6 +106,13 @@ static void xsc_eth_cq_error_event(struct xsc_core_cq *xcq,
> "Eth catch CQ ERROR: %x, cqn: %d\n", event, xcq->cqn);
> }
>
> +static void xsc_eth_comp(struct xsc_core_cq *xcq)
> +{
> + struct xsc_cq *cq = container_of(xcq, struct xsc_cq, xcq);
> +
> + napi_schedule(cq->napi);
> +}
> +
> static int xsc_eth_alloc_cq(struct xsc_channel *c, struct xsc_cq *pcq,
> struct xsc_cq_param *pcq_param)
> {
> @@ -125,6 +132,7 @@ static int xsc_eth_alloc_cq(struct xsc_channel *c, struct xsc_cq *pcq,
> if (ret)
> return ret;
>
> + core_cq->comp = xsc_eth_comp;
> core_cq->event = xsc_eth_cq_error_event;
> core_cq->vector = 0;
>
> @@ -204,6 +212,7 @@ static int xsc_eth_open_cq(struct xsc_channel *c,
>
> xsc_arm_cq(pcq->xdev, pcq->xcq.cqn, pcq->wq.cc, 0);
>
> + pcq->napi = &c->napi;
> pcq->channel = c;
>
> return 0;
> @@ -448,6 +457,8 @@ static int xsc_eth_open_channel(struct xsc_adapter *adapter,
> if (ret)
> goto err_close_sq_cq;
>
> + netif_napi_add(adapter->netdev, &c->napi, xsc_eth_napi_poll);
> +
> netdev_dbg(adapter->netdev, "open channel ok\n");
> return 0;
[Severity: Medium]
Can an EQ completion interrupt reach xsc_eth_comp() before pcq->napi and
c->napi are initialized?
xsc_eth_open_cq() registers pcq->xcq in xdev->cq_table.tree via
xsc_eth_set_cq() and arms it with xsc_arm_cq() before assigning pcq->napi,
and xsc_eth_open_channel() opens both CQs before calling netif_napi_add():
CPU0 <hardirq>
xsc_eth_open_channel()
xsc_eth_open_cq(&c->qp.rq.cq)
xsc_eth_set_cq()
xsc_arm_cq()
xsc_eq_int()
xsc_cq_completion()
xsc_eth_comp()
napi_schedule(cq->napi)
<-- NULL or uninitialized napi
pcq->napi = &c->napi
...
netif_napi_add(&c->napi)
If an EQ event arrives in that window, napi_schedule() either dereferences
NULL or schedules a zeroed napi_struct whose NAPI_STATE_SCHED bit is clear
and poll callback is NULL. Calling netif_napi_add() before opening the
CQs and setting pcq->napi before xsc_eth_set_cq() would close this window.
[ ... ]
> @@ -625,11 +640,17 @@ static void xsc_eth_close_qp_rq(struct xsc_channel *c, struct xsc_rq *prq)
> return;
> }
>
> + /* return the pages still posted to hardware before the pool dies */
> + for (i = 0; i < wq_sz; i++)
> + xsc_eth_dealloc_rx_wqe(prq, i);
> +
> xsc_eth_free_qp_rq(prq);
> }
>
> static void xsc_eth_close_channel(struct xsc_channel *c, bool free_rq)
> {
> + netif_napi_del(&c->napi);
> +
> if (free_rq)
> xsc_eth_close_qp_rq(c, &c->qp.rq);
> xsc_eth_close_cq(c, &c->qp.rq.cq);
[Severity: Medium]
Should netif_napi_del(&c->napi) run after both CQs are closed in
xsc_eth_close_channel()?
While xsc_eth_close_qp_rq(), xsc_eth_close_qp_sq() and xsc_eth_close_cq()
are still running, the CQs remain in xdev->cq_table.tree and an arriving
EQ interrupt can invoke xsc_eth_comp() -> napi_schedule(&c->napi) on the
already deleted napi_struct.
> @@ -644,6 +665,7 @@ static int xsc_eth_open_channels(struct xsc_adapter *adapter)
> {
> struct xsc_channel_param *chl_param;
> bool free_rq = false;
> + bool napi_enabled = false;
> int ret = 0;
>
> chl_param = kvzalloc_obj(*chl_param, GFP_KERNEL);
> @@ -662,6 +684,9 @@ static int xsc_eth_open_channels(struct xsc_adapter *adapter)
> goto err_close_channel;
> free_rq = true;
>
> + napi_enable(&adapter->channel.napi);
> + napi_enabled = true;
> +
> ret = xsc_eth_modify_qps_channel(adapter, &adapter->channel);
> if (ret)
> goto err_close_channel;
> @@ -670,6 +695,8 @@ static int xsc_eth_open_channels(struct xsc_adapter *adapter)
> return 0;
>
> err_close_channel:
> + if (napi_enabled)
> + napi_disable(&adapter->channel.napi);
> xsc_eth_close_channel(&adapter->channel, free_rq);
> err_free_param:
> kvfree(chl_param);
> @@ -702,6 +729,9 @@ static void xsc_eth_deactivate_channel(struct xsc_channel *c)
> clear_bit(XSC_ETH_SQ_STATE_ENABLED, &c->qp.sq.state);
>
> synchronize_net();
> +
> + /* last doorbell out */
> + napi_disable(&c->napi);
> }
[Severity: High]
Can xsc_eth_napi_poll() race with xsc_eth_modify_qps_channel() or run
while XSC_ETH_RQ_STATE_ENABLED is clear?
xsc_eth_open_channels() calls napi_enable() while both CQs are already
armed, before xsc_eth_modify_qps_channel() posts the initial RX WQEs and
before xsc_eth_activate_channel() sets XSC_ETH_RQ_STATE_ENABLED and
XSC_ETH_SQ_STATE_ENABLED. Because xsc_eth_post_rx_wqes() does not check
XSC_ETH_RQ_STATE_ENABLED, an EQ interrupt during open can run
xsc_eth_post_rx_wqes() in NAPI concurrently with process context:
CPU0 (process) CPU1 (<softirq>)
xsc_eth_open_channels()
napi_enable(&c->napi)
xsc_eth_modify_qps_channel()
xsc_eth_post_rx_wqes(rq)
xsc_alloc_rx_wqes()
xsc_eth_napi_poll()
xsc_eth_post_rx_wqes(rq)
xsc_alloc_rx_wqes()
<-- concurrent page_pool alloc
and rq->wqe.wq corruption
This violates the single-consumer BH invariant of page_pool_dev_alloc_pages()
and corrupts rq->wqe.wq and rq->wqe.di[].
In addition, when xsc_eth_napi_poll() runs while the ENABLED bits are
clear (either before xsc_eth_activate_channel(), or in
xsc_eth_deactivate_channel() between clear_bit() and napi_disable()):
- xsc_eth_post_rx_wqes() still allocates pages and rings the RX doorbell,
and if xsc_alloc_rx_wqes() fails with -ENOMEM it returns true (busy),
preventing napi_complete_done() so napi_disable() hangs forever.
- xsc_poll_tx_cq() and xsc_poll_rx_cq() return 0 without draining pending
CQEs, yet xsc_eth_napi_poll() unconditionally rearms both CQs at the
unconsumed cq->wq.cc index, causing an immediate re-interrupt loop.
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_rx.c b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_rx.c
> new file mode 100644
> index 0000000000000..2c07f80928ea2
> --- /dev/null
> +++ b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_rx.c
> @@ -0,0 +1,210 @@
[ ... ]
> +static struct sk_buff *xsc_skb_from_cqe(struct xsc_rq *rq,
> + struct xsc_dma_info *di,
> + u32 cqe_bcnt)
> +{
> + struct sk_buff *skb;
> + void *va;
> + u32 frag_size;
> +
> + va = page_address(di->page);
> + frag_size = XSC_SKB_FRAG_SZ(cqe_bcnt);
> +
> + dma_sync_single_range_for_cpu(rq->cq.xdev->device, di->addr,
> + 0, frag_size, DMA_FROM_DEVICE);
> + net_prefetch(va + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
[Severity: Medium]
Does net_prefetch() prefetch past the packet headers here?
xsc_alloc_rx_wqes() programs wqe->data[0].va with di->addr (offset 0 of
di->page), and napi_build_skb(va, frag_size) places struct skb_shared_info
at the end of the buffer, not at the start. Prefetching
va + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) skips the first 320
bytes where the Ethernet, IP, and transport headers sit (and prefetches
past the end of frames <= 320 bytes), while dma_sync_single_range_for_cpu()
also syncs the trailing skb_shared_info region instead of cqe_bcnt bytes.
> +
> + skb = napi_build_skb(va, frag_size);
> + if (unlikely(!skb))
> + return NULL;
> +
> + skb_put(skb, cqe_bcnt);
> + skb_mark_for_recycle(skb);
> +
> + return skb;
> +}
[Severity: Medium]
Does skb_mark_for_recycle() ever use the lockless NAPI page_pool cache
without pagepool_params.napi set?
In xsc_eth_alloc_rq(), pagepool_params.napi is left NULL:
drivers/net/ethernet/yunsilicon/xsc/net/main.c:xsc_eth_alloc_rq() {
...
pagepool_params.dev = c->adapter->dev;
pagepool_params.dma_dir = DMA_FROM_DEVICE;
pagepool_params.max_len = PAGE_SIZE;
prq->page_pool = page_pool_create(&pagepool_params);
...
}
Because pool->p.napi is NULL, napi_pp_put_page() -> page_pool_napi_local()
always returns false when freeing recycled skbs, bypassing
pool->alloc.cache and forcing every recycled page through the locked
ptr_ring slow path.
[ ... ]
> +bool xsc_eth_post_rx_wqes(struct xsc_rq *rq)
> +{
> + struct xsc_wq_cyc *wq = &rq->wqe.wq;
> + int alloc;
> + u16 head;
> +
> + if (xsc_wq_cyc_missing(wq) < rq->wqe.wqe_bulk)
> + return false;
> +
> + do {
> + head = xsc_wq_cyc_get_head(wq);
> + alloc = min_t(int, rq->wqe.wqe_bulk, xsc_wq_cyc_missing(wq));
> +
> + if (xsc_alloc_rx_wqes(rq, head, alloc))
> + return true;
> +
> + xsc_wq_cyc_push_n(wq, alloc);
> + } while (xsc_wq_cyc_missing(wq) >= rq->wqe.wqe_bulk);
> +
> + dma_wmb();
> +
> + /* ensure wqes are visible to device before updating doorbell record */
> + xsc_rq_notify_hw(rq);
> +
> + return false;
> +}
[Severity: High]
If xsc_alloc_rx_wqes() fails after earlier loop iterations already pushed
WQEs, doesn't returning true immediately skip dma_wmb() and
xsc_rq_notify_hw(rq)?
Those earlier batches are already accounted for in wq->wqe_ctr and
wq->cur_sz via xsc_wq_cyc_push_n(), so skipping the doorbell leaves
hardware unaware of the posted descriptors. Furthermore,
xsc_eth_modify_qps_channel() ignores the return value of
xsc_eth_post_rx_wqes(&c->qp.rq) during initial fill; if any batch fails
during ndo_open, the RX doorbell is never rung at all and the RX queue
starts in RTS with 0 descriptors notified to hardware, stalling RX
permanently.
> +
> +int xsc_eth_napi_poll(struct napi_struct *napi, int budget)
> +{
> + struct xsc_channel *c = container_of(napi, struct xsc_channel, napi);
> + struct xsc_rq *rq = &c->qp.rq;
> + bool busy;
> + int work_done;
> +
> + busy = xsc_poll_tx_cq(&c->qp.sq.cq, xsc_cqwq_get_size(&c->qp.sq.cq.wq));
[Severity: Medium]
Does xsc_poll_tx_cq() ring the TX CQ consumer-index doorbell on every NAPI
poll even when no TX completions were processed?
Unlike xsc_poll_rx_cq(), which guards xsc_cq_notify_hw() with
if (work_done), xsc_poll_tx_cq() calls xsc_update_cq_ci() unconditionally
when i == 0, issuing an unnecessary wmb() and PCIe MMIO writel() on every
RX-only NAPI poll:
drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_txrx.c:xsc_poll_tx_cq() {
...
if (skb) {
dma_unmap_single(dev, wi->dma_addr, skb->len,
DMA_TO_DEVICE);
dev_kfree_skb_any(skb);
}
sqcc++;
i++;
}
xsc_update_cq_ci(cq->xdev, cq->xcq.cqn, cq->wq.cc);
...
}
Also, since xsc_poll_tx_cq() runs from NAPI poll context, using
napi_consume_skb(skb, budget) instead of dev_kfree_skb_any(skb) would
enable NAPI skb bulk freeing.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261126145711.70868-1-tianx@yunsilicon.com?part=11
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: [PATCH v2 00/11] net/xsc: add the Yunsilicon XSC ethernet driver
2026-11-26 14:57 [PATCH v2 00/11] net/xsc: add the Yunsilicon XSC ethernet driver Xin Tian
` (10 preceding siblings ...)
2026-11-26 14:57 ` [PATCH v2 11/11] net/xsc: add the receive path Xin Tian
@ 2026-11-27 14:58 ` sashiko-bot
11 siblings, 0 replies; 33+ messages in thread
From: sashiko-bot @ 2026-11-27 14:58 UTC (permalink / raw)
To: Xin Tian; +Cc: linux-rdma
Sashiko has finished reviewing this 11-patch series:
- Patches 1 and 2: no issues found.
- Patch 3 ("net/xsc: add the command queue"): 4 potential issues (1
High, 3 Medium)
- Patch 4 ("net/xsc: query HCA capabilities and reset function"): 2
potential issues (1 Medium, 1 Low)
- Patch 5 ("net/xsc: add event queues and MSI-X"): 1 potential issue (1
High)
- Patch 6 ("net/xsc: add CQ and QP helpers"): 1 potential issue (1
Medium)
- Patch 7 ("net/xsc: add the ethernet netdev"): 1 potential issue (1
Medium)
- Patch 8 ("net/xsc: add the single TX/RX channel"): 5 potential issues
(5 Medium)
- Patch 9 ("net/xsc: report link state changes"): 1 potential issue (1
Medium)
- Patch 10 ("net/xsc: add the transmit path"): 4 potential issues (1
High, 3 Medium)
- Patch 11 ("net/xsc: add the receive path"): 7 potential issues (2
High, 5 Medium)
Detailed reviews have been sent as separate replies to patches 3-11.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261126145711.70868-1-tianx@yunsilicon.com
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: [PATCH v2 01/11] net/xsc: add Yunsilicon XSC ethernet PCI driver
2026-11-26 14:57 ` [PATCH v2 01/11] net/xsc: add Yunsilicon XSC ethernet PCI driver Xin Tian
@ 2026-11-27 15:57 ` netdev-bot+sashiko
0 siblings, 0 replies; 33+ messages in thread
From: netdev-bot+sashiko @ 2026-11-27 15:57 UTC (permalink / raw)
To: tianx
Cc: andrew+netdev, davem, edumazet, kuba, pabeni, jgg, leon,
huangjunxian6, weihg, linux-kernel, netdev, linux-rdma,
kerneljasonxing
Thank you for your contribution! Sashiko AI review found 4 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 3
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).
- [Medium] In xsc_pci_init(), BAR0 is mapped with pci_ioremap_bar(),
which is a plain ioremap() of pdev->resource[0], but it is unmapped…
- [Low] The subject line is cut short.
- [Low] The new MAINTAINERS entry 'YUNSILICON XSC DRIVERS' has only 'F:
drivers/net/ethernet/yunsilicon/xsc'.
- [Low] xsc_pci_driver.name is "xsc-pci".
--
net/xsc: add Yunsilicon XSC ethernet PCI driver
This commit adds the Yunsilicon vendor Kconfig/Makefile glue and a
skeleton PCI driver for XSC ethernet devices. The PCI core enables the
device, requests and maps BAR0, and sets the DMA masks. The doorbell,
command queue and netdev layers come in later patches.
> net/xsc: add Yunsilicon XSC ethernet PCI driver
>
> skeleton
[Severity: Low]
This isn't a bug, but the subject line seems to have been split. The word
"skeleton" is now the first line of the body.
As a result the shortlog reads "net/xsc: add Yunsilicon XSC ethernet PCI
driver". The commit only adds a PCI skeleton with no netdev. Could
"skeleton" go back into the subject line?
> diff --git a/MAINTAINERS b/MAINTAINERS
> index bde216003f428..1cd2a0a6477c7 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -30909,6 +30909,13 @@ S: Maintained
> F: Documentation/input/devices/yealink.rst
> F: drivers/input/misc/yealink.*
>
> +YUNSILICON XSC DRIVERS
> +M: Honggang Wei <weihg@yunsilicon.com>
> +M: Xin Tian <tianx@yunsilicon.com>
> +L: netdev@vger.kernel.org
> +S: Maintained
> +F: drivers/net/ethernet/yunsilicon/xsc
[Severity: Low]
This isn't a bug, but should this F: entry be
drivers/net/ethernet/yunsilicon/ with a trailing slash?
As written it does not cover the vendor-level files this patch adds,
drivers/net/ethernet/yunsilicon/Kconfig and
drivers/net/ethernet/yunsilicon/Makefile. Changes to those files would not
be sent to the listed maintainers.
The new line in drivers/net/ethernet/Makefile is also out of order:
obj-$(CONFIG_NET_VENDOR_XILINX) += xilinx/
obj-$(CONFIG_NET_VENDOR_YUNSILICON) += yunsilicon/
obj-$(CONFIG_NET_VENDOR_XIRCOM) += xircom/
In drivers/net/ethernet/Kconfig the matching source line comes after
xircom. Both issues are still present at the end of the series.
> +
> ZD1211RW WIRELESS DRIVER
> L: linux-wireless@vger.kernel.org
> S: Orphan
[ ... ]
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/main.c b/drivers/net/ethernet/yunsilicon/xsc/pci/main.c
> new file mode 100644
> index 0000000000000..c926507a74b04
> --- /dev/null
> +++ b/drivers/net/ethernet/yunsilicon/xsc/pci/main.c
[ ... ]
> + bar_base = pci_ioremap_bar(pdev, bar_num);
> + if (!bar_base) {
> + pci_err(pdev, "failed to ioremap %s bar%d\n", KBUILD_MODNAME,
> + bar_num);
> + err = -ENOMEM;
> + goto err_clr_master;
> + }
> +
> + err = pci_save_state(pdev);
> + if (err) {
> + pci_err(pdev, "pci_save_state failed: err=%d\n", err);
> + goto err_io_unmap;
> + }
> +
> + xdev->bar_num = bar_num;
> + xdev->bar = bar_base;
> +
> + return 0;
> +
> +err_io_unmap:
> + pci_iounmap(pdev, bar_base);
[Severity: Medium]
Is pci_iounmap() the right call to undo pci_ioremap_bar()? It pairs with
pci_iomap(), while pci_ioremap_bar() is a plain ioremap(). xsc_pci_fini()
uses the same pairing on remove.
On s390 zPCI without MIO, the two calls are not symmetric.
zpci_map_resources() stores a cookie from pci_iomap_range_fh() in
resource[0].start, and that call takes the only reference.
pci_ioremap_bar() returns that cookie without taking a reference:
arch/s390/pci/pci.c:ioremap_prot() {
...
if (!static_branch_unlikely(&have_mio))
return (void __iomem *)phys_addr;
...
}
pci_iounmap() then calls pci_iounmap_fh(), which drops the reference that
zpci_map_resources() holds:
arch/s390/pci/pci.c:pci_iounmap_fh() {
...
WARN_ON(!zpci_iomap_start[idx].count);
if (!--zpci_iomap_start[idx].count) {
zpci_iomap_start[idx].fh = 0;
zpci_iomap_start[idx].bar = 0;
}
...
}
After one unbind, or one probe failure that reaches err_io_unmap, would
the next probe get a cookie whose fh is 0? Then every BAR0 readl()/writel()
would go to the wrong function handle.
When the device is finally removed, zpci_unmap_resources() calls
pci_iounmap_fh() again. That would trigger WARN_ON(!count) and underflow
the counter.
Later patches in the series add more ways to reach err_io_unmap, such as
xsc_cmd_init() failing.
Could this pair pci_ioremap_bar() with iounmap() instead? Another option is
to use pci_iomap()/pci_iounmap() or pcim_iomap_region().
> +err_clr_master:
> + pci_clear_master(pdev);
> + pci_release_region(pdev, bar_num);
> +err_disable:
> + pci_disable_device(pdev);
> +err_out:
> + return err;
> +}
> +
> +static void xsc_pci_fini(struct xsc_core_device *xdev)
> +{
> + struct pci_dev *pdev = xdev->pdev;
> +
> + pci_iounmap(pdev, xdev->bar);
> + pci_clear_master(pdev);
> + pci_release_region(pdev, xdev->bar_num);
> + pci_disable_device(pdev);
> +}
[ ... ]
> +static struct pci_driver xsc_pci_driver = {
> + .name = "xsc-pci",
[Severity: Low]
This isn't a bug, but the driver name here is "xsc-pci". The module is
called xsc_pci per the Kconfig help and the Makefile, and
pci_request_region() in xsc_pci_init() claims BAR0 under KBUILD_MODNAME
("xsc_pci").
So /sys/bus/pci/drivers/xsc-pci and the owner shown in /proc/iomem would
have different names. Later in the series xsc_core.h defines
XSC_PCI_DRV_NAME as "xsc_pci", but .name still uses the hyphenated string.
Would .name = KBUILD_MODNAME be more consistent?
> + .id_table = xsc_pci_id_table,
> + .probe = xsc_pci_probe,
> + .remove = xsc_pci_remove,
> +};
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126145711.70868-1-tianx%40yunsilicon.com
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: [PATCH v2 02/11] net/xsc: add doorbell and cmdq MMIO accessors
2026-11-26 14:57 ` [PATCH v2 02/11] net/xsc: add doorbell and cmdq MMIO accessors Xin Tian
@ 2026-11-27 15:57 ` netdev-bot+sashiko
0 siblings, 0 replies; 33+ messages in thread
From: netdev-bot+sashiko @ 2026-11-27 15:57 UTC (permalink / raw)
To: tianx
Cc: andrew+netdev, davem, edumazet, kuba, pabeni, jgg, leon,
huangjunxian6, weihg, linux-kernel, netdev, linux-rdma,
kerneljasonxing
Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 2
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).
- [Medium] xsc_arm_cq(xdev, cqn, next_cid, u8 solicited) in
drivers/net/ethernet/yunsilicon/xsc/pci/doorbell.c (lines 62-74) puts…
- [Low] All five doorbell writers in
drivers/net/ethernet/yunsilicon/xsc/pci/doorbell.c call a full wmb()…
- [Low] The new file drivers/net/ethernet/yunsilicon/xsc/pci/doorbell.c
calls EXPORT_SYMBOL_GPL() four times (xsc_update_tx_db,…
--
net/xsc: add doorbell and cmdq MMIO accessors
This commit adds doorbell writers for TX/RX queues, CQs and EQs, along
with cmdq register accessors. They all operate on fixed offsets in the
device BAR. The helpers are declared in xsc_core.h, and the TX, RX and
CQ helpers are exported for use by other modules.
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/doorbell.c b/drivers/net/ethernet/yunsilicon/xsc/pci/doorbell.c
> new file mode 100644
> index 0000000000000..5bd64be671646
> --- /dev/null
> +++ b/drivers/net/ethernet/yunsilicon/xsc/pci/doorbell.c
> @@ -0,0 +1,148 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/* Copyright (C) 2024 - 2026, Shanghai Yunsilicon Technology Co., Ltd.
> + * All rights reserved.
> + */
> +
> +#include <linux/kernel.h>
> +#include <linux/pci.h>
> +
> +#include "common/xsc_core.h"
> +#include "common/xsc_reg.h"
[Severity: Low]
This isn't a bug, but should this file include <linux/export.h> directly?
It uses EXPORT_SYMBOL_GPL() for xsc_update_tx_db(), xsc_update_rx_db(),
xsc_arm_cq() and xsc_update_cq_ci(). With W=2, the
check_missing_include_linux_export_h check in scripts/misc-check reports
this for the file:
EXPORT_SYMBOL() is used, but #include <linux/export.h> is missing
The file builds today only because export.h is pulled in indirectly.
[ ... ]
> +void xsc_update_tx_db(struct xsc_core_device *xdev, u32 sqn, u32 next_pid)
> +{
> + u32 db = FIELD_PREP(XSC_TX_DB_DATA0_NEXT_PID_MASK, next_pid) |
> + FIELD_PREP(XSC_TX_DB_DATA0_QP_ID_MASK, sqn);
> +
> + /* Make sure that descriptors are written before
> + * updating doorbell record and ringing the doorbell
> + */
> + wmb();
> +
> + writel(db, xdev->bar + TX_DB_FUNC_MEM_ADDR);
> +}
[Severity: Low]
Is the explicit wmb() needed before writel() here? writel() already
orders earlier stores to normal and coherent DMA memory before the MMIO
store:
include/asm-generic/io.h:writel() {
...
__io_bw();
__raw_writel((u32 __force)__cpu_to_le32(value), addr);
__io_aw();
...
}
On arm64, __io_bw() is dma_wmb(), so the extra wmb() adds a dsb st to
every TX doorbell.
xsc_update_rx_db(), xsc_arm_cq(), xsc_update_cq_ci() and
xsc_update_eq_ci() use the same wmb() plus writel() pattern. Once the
later "net/xsc: add the transmit path" and "net/xsc: add the receive
path" patches call these helpers, they are on the hot path.
xsc_update_cmdq_req_pid() uses a plain writel(), so the barrier usage in
this file is also inconsistent. The code is unchanged at the end of the
series.
The comments don't seem to match the code either:
- The TX/RX comments mention "updating doorbell record".
- xsc_arm_cq() mentions "the doorbell record in host memory".
- Only a single MMIO word is written, and there is no doorbell record
in host memory.
- xsc_update_cq_ci() and xsc_update_eq_ci() say "make sure val write to
memory done", but there is no val variable.
For the CQ/EQ consumer index updates, the intent may be that the
CQE/EQE loads finish before the device can reuse the entry. wmb() only
orders stores, so would a different barrier be needed there?
[ ... ]
> +void xsc_arm_cq(struct xsc_core_device *xdev, u32 cqn, u32 next_cid,
> + u8 solicited)
> +{
> + u32 db = FIELD_PREP(XSC_CQ_DB_DATA0_NEXT_CID_MASK, next_cid) |
> + FIELD_PREP(XSC_CQ_DB_DATA0_CQ_ID_MASK, cqn) |
> + FIELD_PREP(XSC_CQ_DB_DATA0_ARM, !!solicited);
[Severity: Medium]
Is the solicited argument meant to set the bit named
XSC_CQ_DB_DATA0_ARM?
The peer helper xsc_update_eq_ci() sets XSC_EQ_DB_DATA0_ARM from an arm
argument. Its callers in eq.c pass 1 to arm the EQ at the end of
xsc_eq_int() and at EQ creation, and 0 for intermediate CI updates.
Later in the series, every caller of xsc_arm_cq() passes 0. The re-arm
after NAPI completion does this:
xsc_cq_notify_hw_rearm()
xsc_arm_cq(cq->xdev, cq->xcq.cqn, cq->wq.cc, 0);
So does the initial arm at CQ creation in net/main.c:
xsc_arm_cq(pcq->xdev, pcq->xcq.cqn, pcq->wq.cc, 0);
If BIT(31) is the arm enable bit, as the macro name suggests, do the
CQs ever get armed? Completion interrupts would then stop after the
first napi_complete_done(), and the RX/TX datapath would stall.
Another reading is that any write to DB_CQ_FUNC_MEM_ADDR arms the CQ,
and BIT(31) only selects solicited-only notification. In that case,
should the macro be renamed so the exported prototype, the
implementation and the EQ helper agree on what this bit means?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126145711.70868-1-tianx%40yunsilicon.com
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: [PATCH v2 03/11] net/xsc: add the command queue
2026-11-26 14:57 ` [PATCH v2 03/11] net/xsc: add the command queue Xin Tian
2026-11-27 14:58 ` sashiko-bot
@ 2026-11-27 15:57 ` netdev-bot+sashiko
1 sibling, 0 replies; 33+ messages in thread
From: netdev-bot+sashiko @ 2026-11-27 15:57 UTC (permalink / raw)
To: tianx
Cc: andrew+netdev, davem, edumazet, kuba, pabeni, jgg, leon,
huangjunxian6, weihg, linux-kernel, netdev, linux-rdma,
kerneljasonxing
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
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: [PATCH v2 04/11] net/xsc: query HCA capabilities and reset function
2026-11-26 14:57 ` [PATCH v2 04/11] net/xsc: query HCA capabilities and reset function Xin Tian
2026-11-27 14:58 ` sashiko-bot
@ 2026-11-27 15:57 ` netdev-bot+sashiko
1 sibling, 0 replies; 33+ messages in thread
From: netdev-bot+sashiko @ 2026-11-27 15:57 UTC (permalink / raw)
To: tianx
Cc: andrew+netdev, davem, edumazet, kuba, pabeni, jgg, leon,
huangjunxian6, weihg, linux-kernel, netdev, linux-rdma,
kerneljasonxing
Thank you for your contribution! Sashiko AI review found 5 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 1 · Low: 3
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] In xsc_pci_init()
(drivers/net/ethernet/yunsilicon/xsc/pci/main.c), the new…
- [Medium] net/xsc: missing in.hdr.ver initialization in
xsc_cmd_query_hca_cap()
- [Low] xsc_cmd_query_hca_cap() in
drivers/net/ethernet/yunsilicon/xsc/pci/main.c computes…
- [Low] The subject line is split across two lines.
- [Low] xsc_cmd_query_hca_cap()
(drivers/net/ethernet/yunsilicon/xsc/pci/main.c) takes an unneeded…
--
net/xsc: query HCA capabilities and reset function
During probe, this commit queries the firmware HCA capabilities (CQ size,
segment counts, MSI-X range, port and PF identifiers). It stores them in
struct xsc_caps and struct xsc_core_device, then sends the firmware
FUNCTION_RESET command to reset the function's resources.
> net/xsc: query HCA capabilities and reset function
>
> resources
[Severity: Low]
This isn't a bug, but the subject line looks like it was wrapped onto two
lines. Git will use "net/xsc: query HCA capabilities and reset function"
as the subject, and "resources" will become the first paragraph of the
body.
The cut-off shortlog also reads like a PCI function-level reset. The patch
actually sends the firmware XSC_CMD_OP_FUNCTION_RESET command.
Could the subject be joined back into a single line?
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/main.c b/drivers/net/ethernet/yunsilicon/xsc/pci/main.c
> index 9b08580f52f38..42c5a1baed23b 100644
> --- a/drivers/net/ethernet/yunsilicon/xsc/pci/main.c
> +++ b/drivers/net/ethernet/yunsilicon/xsc/pci/main.c
> @@ -29,6 +29,63 @@ static int xsc_set_dma_caps(struct pci_dev *pdev)
> return err;
> }
>
> +static int xsc_cmd_query_hca_cap(struct xsc_core_device *xdev,
> + struct xsc_caps *caps)
> +{
[ ... ]
> + xdev->glb_func_id = be32_to_cpu(out->hca_cap.glb_func_id);
> +
> + caps->max_cqes = 1 << out->hca_cap.log_max_cq_sz;
[Severity: Low]
log_max_cq_sz is a u8 that comes straight from firmware, and nothing
checks its range. The 1 here is a signed int, so a value of 31 shifts into
the sign bit, and a value of 32 or more is undefined behaviour.
Later in the series, xsc_eth_build_queue_param() does:
min_t(int, XSC_RQCQ_ELE_NUM, xdev->caps.max_cqes)
A max_cqes of 0x80000000 would become INT_MIN there and give a negative
queue size. log_max_qp_depth is also stored unchecked, and the same
function uses it in BIT(xdev->caps.log_max_qp_depth) for rq_size with no
upper clamp.
Would it make sense to use 1U << n (or BIT()) here, and reject or clamp
out-of-range exponents?
> + caps->send_ds_num = out->hca_cap.send_seg_num;
> + caps->recv_ds_num = out->hca_cap.recv_seg_num;
> +
> + caps->nic_caps = (caps->nic_caps & ~XSC_CAP_MAC_PORT_MASK) |
> + FIELD_PREP(XSC_CAP_MAC_PORT_MASK,
> + out->hca_cap.mac_port & 0xff);
> + xdev->mac_port = FIELD_GET(XSC_CAP_MAC_PORT_MASK, caps->nic_caps);
[Severity: Low]
This isn't a bug, but is the round trip through caps->nic_caps needed?
Packing mac_port with FIELD_PREP() and then unpacking it with FIELD_GET()
gives the same result as:
xdev->mac_port = out->hca_cap.mac_port;
As far as I can tell, nothing else in the series reads caps->nic_caps. That
leaves the nic_caps field and XSC_CAP_MAC_PORT_MASK otherwise unused. The
other capability fields are read by later patches, so this applies only to
nic_caps.
The & 0xff masks on the u8 fields mac_port and log_max_qp_depth also have
no effect.
[ ... ]
> @@ -90,6 +147,19 @@ static int xsc_pci_init(struct xsc_core_device *xdev,
> goto err_cmd_cleanup;
> }
>
> + err = xsc_cmd_query_hca_cap(xdev, &xdev->caps);
> + if (err) {
> + pci_err(pdev, "failed to query hca caps: err=%d\n", err);
> + goto err_cmd_cleanup;
> + }
> +
> + err = xsc_reset_function_resource(xdev);
> + if (err) {
> + pci_err(pdev, "failed to reset function resource: err=%d\n",
> + err);
> + goto err_cmd_cleanup;
> + }
[Severity: High]
What happens to the device's DMA if one of these commands times out and
probe takes the err_cmd_cleanup path?
On a timeout, xsc_cmd_invoke() leaves the entry registered, and
_xsc_cmd_exec() leaves both mailboxes owned by the device:
drivers/net/ethernet/yunsilicon/xsc/pci/cmd.c:xsc_cmd_invoke() {
...
if (!wait_for_completion_timeout(&ent->done, timeout)) {
cmd->cmd_status = XSC_CMD_STATUS_TIMEDOUT;
...
/* the entry stays registered for the completion handler */
return -ETIMEDOUT;
}
...
}
drivers/net/ethernet/yunsilicon/xsc/pci/cmd.c:_xsc_cmd_exec() {
...
err = xsc_cmd_invoke(xdev, inb, outb, &status);
if (err == -ETIMEDOUT)
/* the command owns both mailboxes now */
return err;
...
}
The device still holds out->next->dma through lay->out_ptr. It also still
has cq_buf and cmd_buf programmed as DMA targets.
xsc_cmd_cleanup() then stops only the polling kthread. It frees all of
that memory while bus mastering is still enabled:
drivers/net/ethernet/yunsilicon/xsc/pci/cmd.c:xsc_cmd_cleanup() {
...
xsc_free_msg(xdev, cmd->ent_arr[i]->in);
xsc_free_msg(xdev, cmd->ent_arr[i]->out);
...
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);
...
}
pci_clear_master() only runs later, at err_clr_master, after
pci_iounmap(). If the firmware finishes the timed-out command late, can it
DMA the response and completion entry into memory that has already gone
back to the page allocator or the dma_pool?
The comment in xsc_cmd_cleanup() describes a response that "may never
come", which suggests the driver expects a late response is possible.
This ordering comes from the earlier "net/xsc: add the command queue"
patch in this series. The xsc_cmd_version_check() error path can already
reach it, and xsc_pci_fini() also calls xsc_cmd_cleanup() before
pci_clear_master(). This patch adds two more probe paths into it.
FUNCTION_RESET is a firmware resource teardown, so it seems likely to run
long and time out.
Should the device's cmdq be quiesced, or bus mastering cleared, before
the mailboxes and rings are freed?
[ ... ]
> @@ -29,6 +29,63 @@ static int xsc_set_dma_caps(struct pci_dev *pdev)
> return err;
> }
>
> +static int xsc_cmd_query_hca_cap(struct xsc_core_device *xdev,
> + struct xsc_caps *caps)
> +{
> + struct xsc_cmd_query_hca_cap_mbox_out *out;
> + struct xsc_cmd_query_hca_cap_mbox_in in;
> + int err;
> +
> + out = kzalloc_obj(*out, GFP_KERNEL);
> + if (!out)
> + return -ENOMEM;
> +
> + memset(&in, 0, sizeof(in));
> + in.hdr.opcode = cpu_to_be16(XSC_CMD_OP_QUERY_HCA_CAP);
> + in.cpu_num = cpu_to_be16(num_online_cpus());
> +
> + err = xsc_cmd_exec(xdev, &in, sizeof(in), out, sizeof(*out));
> + if (err)
> + goto err_free;
> +
> + xdev->glb_func_id = be32_to_cpu(out->hca_cap.glb_func_id);
> +
> + caps->max_cqes = 1 << out->hca_cap.log_max_cq_sz;
> + caps->send_ds_num = out->hca_cap.send_seg_num;
> + caps->recv_ds_num = out->hca_cap.recv_seg_num;
> +
> + caps->nic_caps = (caps->nic_caps & ~XSC_CAP_MAC_PORT_MASK) |
> + FIELD_PREP(XSC_CAP_MAC_PORT_MASK,
> + out->hca_cap.mac_port & 0xff);
> + xdev->mac_port = FIELD_GET(XSC_CAP_MAC_PORT_MASK, caps->nic_caps);
> +
> + caps->msix_base = be16_to_cpu(out->hca_cap.msix_base);
> + caps->msix_num = be16_to_cpu(out->hca_cap.msix_num);
> +
> + caps->log_max_qp_depth = out->hca_cap.log_max_qp_depth & 0xff;
> +
> + xdev->pcie_no = out->hca_cap.pcie_no;
> + xdev->pf_id = out->hca_cap.pf_id;
> +
> + kfree(out);
> + return 0;
> +err_free:
> + kfree(out);
> + return err;
> +}
> +
> +static int xsc_reset_function_resource(struct xsc_core_device *xdev)
> +{
> + struct xsc_function_reset_mbox_out out;
> + struct xsc_function_reset_mbox_in in;
> +
> + memset(&in, 0, sizeof(in));
> + in.hdr.opcode = cpu_to_be16(XSC_CMD_OP_FUNCTION_RESET);
> + in.glb_func_id = cpu_to_be16(xdev->glb_func_id);
> +
> + return xsc_cmd_exec(xdev, &in, sizeof(in), &out, sizeof(out));
> +}
> +
> static int xsc_pci_init(struct xsc_core_device *xdev,
> const struct pci_device_id *id)
> {
[Severity: Medium]
The input mailbox is zeroed by memset(), and only in.hdr.opcode and
in.cpu_num are filled in, so in.hdr.ver goes to the firmware as 0.
xsc_cmd.h defines XSC_CMD_QUERY_HCA_CAP_V1 as the hca_cap mbox
version, but nothing in the driver uses it.
This matters because the function then reads out->hca_cap.pcie_no and
out->hca_cap.pf_id without any check. Those fields are part of the
versioned extension of struct xsc_hca_cap, and later in the series
xsc_core_eth_modify_raw_qp() uses them to set up raw QP port routing.
The firmware might validate hdr.ver, or fill in the extended fields
only for a V1 request. In that case a version 0 request could fail
the query and abort probe. It could also leave pcie_no and pf_id at
zero from the kzalloc. In that second case probe succeeds and the raw
QPs are set up with the wrong port routing, and nothing is logged.
Should this set in.hdr.ver = cpu_to_be16(XSC_CMD_QUERY_HCA_CAP_V1)?
If the firmware is meant to return the full structure for version 0,
could the commit message say so, and the unused define be dropped?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126145711.70868-1-tianx%40yunsilicon.com
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: [PATCH v2 05/11] net/xsc: add event queues and MSI-X
2026-11-26 14:57 ` [PATCH v2 05/11] net/xsc: add event queues and MSI-X Xin Tian
2026-11-27 14:58 ` sashiko-bot
@ 2026-11-27 15:57 ` netdev-bot+sashiko
1 sibling, 0 replies; 33+ messages in thread
From: netdev-bot+sashiko @ 2026-11-27 15:57 UTC (permalink / raw)
To: tianx
Cc: andrew+netdev, davem, edumazet, kuba, pabeni, jgg, leon,
huangjunxian6, weihg, linux-kernel, netdev, linux-rdma,
kerneljasonxing
Thank you for your contribution! Sashiko AI review found 7 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 2 · Low: 5
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).
- [Medium] xsc_buf_alloc()
(drivers/net/ethernet/yunsilicon/xsc/pci/alloc.c) handles buffers…
- [Medium] xsc_create_map_eq()
(drivers/net/ethernet/yunsilicon/xsc/pci/eq.c) frees the EQ ring for…
- [Low] In xsc_destroy_unmap_eq() (eq.c), the u32 eq->eqn is printed with
"%d", a signed specifier.
- [Low] struct xsc_eq::index gets two different meanings.
- [Low] The exported link-event callback API publishes the handler with a
plain WRITE_ONCE() in xsc_irq_event_register().
- [Low] The EQ consumer-index doorbell field is only 11 bits wide
(XSC_EQ_DB_DATA0_NEXT_CID_MASK = GENMASK(10, 0), with eqn in…
- [Low] xsc_irq_eq_destroy() tears down the async EQ (xsc_stop_eqs)
before the completion EQs (free_comp_eqs).
--
net/xsc: add event queues and MSI-X
This commit creates the asynchronous and completion event queues,
allocates MSI-X vectors, and dispatches link events from a dedicated
workqueue. The command queue remains polling-only.
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/alloc.c b/drivers/net/ethernet/yunsilicon/xsc/pci/alloc.c
> new file mode 100644
> index 0000000000000..46eb0b031511d
> --- /dev/null
> +++ b/drivers/net/ethernet/yunsilicon/xsc/pci/alloc.c
> @@ -0,0 +1,123 @@
[ ... ]
> + for (i = 0; i < buf->nbufs; i++) {
> + buf->page_list[i].buf =
> + dma_alloc_coherent(&xdev->pdev->dev, PAGE_SIZE,
> + &t, GFP_KERNEL);
[ ... ]
> + for (i = 0; i < buf->nbufs; i++) {
> + void *addr = buf->page_list[i].buf;
> +
> + if (is_vmalloc_addr(addr))
> + pages[i] = vmalloc_to_page(addr);
> + else
> + pages[i] = virt_to_page(addr);
> + }
> + buf->direct.buf = vmap(pages, buf->nbufs, VM_MAP, PAGE_KERNEL);
[Severity: Medium]
Is it valid to call virt_to_page()/vmalloc_to_page() on memory returned by
dma_alloc_coherent() and then vmap() it with PAGE_KERNEL?
The DMA API doesn't guarantee that a coherent allocation sits behind a
linear-map address with a valid struct page. It can be an uncached remap,
memory from a memremap'd per-device or restricted pool, or a decrypted page
on SEV/TDX guests. In those cases the derived struct page is wrong, or the
new cacheable alias has attributes that don't match. vmap() can also fail,
and then EQ creation fails.
On 4K-page systems this path runs on every probe. Each completion EQ has
nent = roundup_pow_of_two(1024 + 0x80) = 2048 entries of 8 bytes. That is
16 KiB, which is more than the max_direct = PAGE_SIZE passed by
xsc_create_map_eq().
The vmap'd buffer also seems to be unused. xsc_buf_offset() in xsc_core.h
never reads direct.buf when nbufs != 1:
if (likely(buf->nbufs == 1))
return buf->direct.buf + offset;
else
return buf->page_list[offset >> PAGE_SHIFT].buf +
(offset & (PAGE_SIZE - 1));
So direct.buf is only ever passed to vunmap().
Upstream removed the same pattern from mlx4_buf_alloc(). The current mlx4
alloc.c uses per-page dma_alloc_coherent() fragments and no vmap.
Could the vmap() step be dropped here too?
[ ... ]
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/eq.c b/drivers/net/ethernet/yunsilicon/xsc/pci/eq.c
> new file mode 100644
> index 0000000000000..875a277a83cb8
> --- /dev/null
> +++ b/drivers/net/ethernet/yunsilicon/xsc/pci/eq.c
> @@ -0,0 +1,217 @@
[ ... ]
> +int xsc_create_map_eq(struct xsc_core_device *xdev,
> + struct xsc_eq *eq, u16 vecidx,
> + u32 nent, const char *name)
> +{
[ ... ]
> + eq->nent = roundup_pow_of_two(nent + XSC_NUM_SPARE_EQE);
> + err = xsc_buf_alloc(xdev, eq->nent * XSC_EQE_SIZE, PAGE_SIZE, &eq->buf);
[ ... ]
> + in->hdr.opcode = cpu_to_be16(XSC_CMD_OP_CREATE_EQ);
> + in->ctx.log_eq_sz = ilog2(eq->nent);
[Severity: Low]
Can the consumer index doorbell describe a 2048-entry EQ? The CID field in
doorbell.c is only 11 bits wide:
#define XSC_EQ_DB_DATA0_NEXT_CID_MASK GENMASK(10, 0)
#define XSC_EQ_DB_DATA0_EQ_ID_MASK GENMASK(21, 11)
xsc_update_eq_ci() fills that field from the free-running eq->cons_index
using FIELD_PREP(), which silently masks non-constant values.
The comp EQs have nent = 2048 (log_eq_sz = 11), so their doorbell carries
only the slot index. Bit 11, the wrap/phase bit that next_eqe_sw() uses
for ownership, is lost. The 512-entry async EQ keeps its index bits plus
two wrap bits.
Whether this matters depends on how the hardware reads CID, which the
source doesn't show, and EQ occupancy is small today.
Should the driver check the firmware-reported log_max_eq_sz before asking
for a 2^11 EQ? The field is in the hca_cap layout in xsc_cmd.h, but it is
not stored in xsc_caps.
> + in->ctx.vecidx = cpu_to_be16(msix_vec_offset);
> + in->ctx.pa_num = cpu_to_be16(hw_npages);
> + in->ctx.glb_func_id = cpu_to_be16(xdev->glb_func_id);
> + in->ctx.is_async_eq = (vecidx == XSC_EQ_VEC_ASYNC ? 1 : 0);
> +
> + err = xsc_cmd_exec(xdev, in, inlen, &out, sizeof(out));
> + if (err)
> + goto err_free_in;
[Severity: Medium]
What happens to the EQ ring when xsc_cmd_exec() returns -ETIMEDOUT here?
The command layer treats a timeout as a command that is still in flight.
In xsc_cmd_invoke():
if (!wait_for_completion_timeout(&ent->done, timeout)) {
cmd->cmd_status = XSC_CMD_STATUS_TIMEDOUT;
...
/* the entry stays registered for the completion handler */
return -ETIMEDOUT;
}
and _xsc_cmd_exec() keeps the mailboxes alive on a timeout:
if (err == -ETIMEDOUT)
/* the command owns both mailboxes now */
return err;
Here the error path goes to err_free_in and then err_free_eq_buf, so
xsc_buf_free() returns the ring pages with dma_free_coherent(). Their bus
addresses were already passed to firmware in in->pas[].
If the firmware completes the late CREATE_EQ, can it end up with an armed
EQ that DMAs into freed pages? Once cmd_status is TIMEDOUT, every later
xsc_cmd_exec() returns -ETIMEDOUT at once, so a DESTROY_EQ can never be
sent to recover.
The err_destroy_eq path and xsc_destroy_unmap_eq() already leak the buffer
for this reason when DESTROY_EQ fails. Should a CREATE_EQ timeout leak
eq->buf as well, instead of freeing it?
[ ... ]
> + eq->index = vecidx;
[ ... ]
> +int xsc_destroy_unmap_eq(struct xsc_core_device *xdev, struct xsc_eq *eq)
> +{
> + int err;
> +
> + free_irq(eq->irqn, eq);
> + err = xsc_cmd_destroy_eq(xdev, eq->eqn);
> + if (err) {
> + pci_err(xdev->pdev, "failed to destroy a previously created eq: eqn %d\n",
> + eq->eqn);
[Severity: Low]
This isn't a bug, but eq->eqn is a u32 and is printed with %d. The caller
free_comp_eqs() logs the same failure again as "failed to destroy EQ
0x%x". A single DESTROY_EQ failure therefore produces two error lines,
with the same EQ number shown once in decimal and once in hex.
Could one of the messages be dropped, or both use the same format?
[ ... ]
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/pci_irq.c b/drivers/net/ethernet/yunsilicon/xsc/pci/pci_irq.c
> new file mode 100644
> index 0000000000000..accc3e56ba46c
> --- /dev/null
> +++ b/drivers/net/ethernet/yunsilicon/xsc/pci/pci_irq.c
> @@ -0,0 +1,345 @@
[ ... ]
> +int xsc_core_vector2eqn(struct xsc_core_device *xdev, int vector, u32 *eqn,
> + unsigned int *irqn)
> +{
[ ... ]
> + list_for_each_entry_safe(eq, n, &table->comp_eqs_list, list) {
> + if (eq->index == vector) {
[ ... ]
> + snprintf(name, XSC_MAX_IRQ_NAME, "xsc_comp%d", i);
> + err = xsc_create_map_eq(xdev, eq,
> + i + table->eq_vec_comp_base,
> + nent, name);
> + if (err) {
> + kfree(eq);
> + goto err_free;
> + }
> +
> + eq->index = i;
[Severity: Low]
This isn't a bug, but struct xsc_eq::index ends up with two meanings.
xsc_create_map_eq() sets eq->index = vecidx, which is the absolute MSI-X
table entry: 0 for async, and comp EQs starting at
XSC_EQ_VEC_COMP_BASE = 4. This line then immediately overwrites it with
the comp-relative i.
The exported xsc_core_vector2eqn() compares vector against this field.
That makes the store in xsc_create_map_eq() dead for comp EQs, and the API
does not say that vector is comp-relative.
The in-tree callers added later in the series, xsc_eth_open_channel() and
xsc_eth_set_cq(), pass comp-relative indices and work today.
Could the field or the vector argument of xsc_core_vector2eqn() be
documented, or split into two fields, so that a caller expecting MSI-X
numbering is not misled?
[ ... ]
> +static void xsc_event_work(struct work_struct *work)
> +{
[ ... ]
> + handler = READ_ONCE(xdev->link_event_handler);
> + if (handler)
> + handler(xdev);
[ ... ]
> +void xsc_irq_event_register(struct xsc_core_device *xdev,
> + void (*handler)(void *xdev))
> +{
> + WRITE_ONCE(xdev->link_event_handler, handler);
> +}
[Severity: Low]
Is there enough ordering between xsc_irq_event_register() and
xsc_event_work()?
The handler is published with a plain WRITE_ONCE(), read with a plain
READ_ONCE(), and called right away. It reaches the registrant's state
through xdev (for example xdev->eth_priv), and that access has no address
dependency on the function pointer.
There is no smp_store_release()/smp_load_acquire() pair, lock, or RCU
here. A worker on another CPU could therefore see the new handler together
with state written before the register call that is still stale.
The in-tree user in xsc_eth_probe() happens to get a wmb() on the writer
side from the xsc_cmd_exec() calls in between. Also,
xsc_eth_link_event_handler() returns early when !adapter ||
!adapter->opened, so at worst a link event is ignored today.
The header comment describes a register/unregister contract, but nothing
in the code orders it. Would release/acquire semantics be worth using
here?
[ ... ]
> +void xsc_irq_eq_destroy(struct xsc_core_device *xdev)
> +{
> + xsc_stop_eqs(xdev);
> + free_comp_eqs(xdev);
[Severity: Low]
Should this teardown run in reverse creation order?
xsc_irq_eq_create() creates the async EQ first and the comp EQs after it.
Its own error unwind gets the order right:
err_free_comp_eqs:
free_comp_eqs(xdev);
err_stop_eqs:
xsc_stop_eqs(xdev);
Here, the async EQ (is_async_eq = 1, where firmware reports CQ/QP error
events) is destroyed while the comp EQs are still live in firmware.
Later in the series xsc_pci_fini() calls xsc_adev_uninit() before this, so
no concrete failure is known. The normal teardown and the error unwind
still disagree.
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126145711.70868-1-tianx%40yunsilicon.com
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: [PATCH v2 06/11] net/xsc: add CQ and QP helpers
2026-11-26 14:57 ` [PATCH v2 06/11] net/xsc: add CQ and QP helpers Xin Tian
2026-11-27 14:58 ` sashiko-bot
@ 2026-11-27 15:57 ` netdev-bot+sashiko
1 sibling, 0 replies; 33+ messages in thread
From: netdev-bot+sashiko @ 2026-11-27 15:57 UTC (permalink / raw)
To: tianx
Cc: andrew+netdev, davem, edumazet, kuba, pabeni, jgg, leon,
huangjunxian6, weihg, linux-kernel, netdev, linux-rdma,
kerneljasonxing
Thank you for your contribution! Sashiko AI review found 8 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 7
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).
- [Medium] In xsc_core_eth_destroy_cq()
(drivers/net/ethernet/yunsilicon/xsc/pci/cq.c), the CQ is taken out of…
- [Low] xsc_eq_int() in drivers/net/ethernet/yunsilicon/xsc/pci/eq.c
groups XSC_EVENT_TYPE_INTERNAL_ERROR (0x08, defined in enum xsc_event…
- [Low] xsc_cq_completion() ('Completion event for bogus CQ'),
xsc_eq_cq_event() ('Async event for bogus CQ') and xsc_qp_event()…
- [Low] The CQ/QP number widths do not agree across three places.
- [Low] The comment in xsc_core_eth_destroy_cq() (cq.c lines 106-109)
says the completion reader 'looks the cq up under rcu_read_lock and…
- [Low] The exported helper xsc_core_eth_modify_qp_status(xdev, qpn, u16
status) (prototype in xsc_core.h, implementation in qp.c) and its…
- [Low] Several new messages print unsigned u32 CQ/QP numbers with %d:
cq.c xsc_core_eth_create_cq() and xsc_core_eth_destroy_cq() ('failed…
- [Low] xsc_core_eth_destroy_qp() (qp.c lines 134-159) first moves the QP
to RST with xsc_core_eth_modify_qp_status(..., XSC_CMD_OP_2RST_QP).
--
net/xsc: add CQ and QP helpers
This patch adds CQ and QP radix tree tables to the xsc PCI core, along
with exported ethernet helpers to create, destroy and modify CQs and QPs.
It also dispatches completion, CQ error and WQ error events from
xsc_eq_int() to the registered CQ/QP callbacks.
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/cq.c b/drivers/net/ethernet/yunsilicon/xsc/pci/cq.c
> new file mode 100644
> index 0000000000000..a4f9b488d659d
> --- /dev/null
> +++ b/drivers/net/ethernet/yunsilicon/xsc/pci/cq.c
> @@ -0,0 +1,130 @@
[ ... ]
> +static int xsc_create_cq(struct xsc_core_device *xdev, u32 *p_cqn,
> + struct xsc_create_cq_mbox_in *in, int insize)
> +{
[ ... ]
> + *p_cqn = be32_to_cpu(out.cqn) & 0xffffff;
> + return 0;
> +}
[Severity: Low]
The CQN kept here is 24 bits wide. xsc_core_eth_create_qp() keeps a
24-bit QPN the same way. Both are used as radix tree keys, but the EQE
that xsc_eq_int() uses to look them up only carries 15 bits:
eq.h:
#define XSC_EQE_QUEUE_ID_MASK GENMASK(14, 0)
The mailbox fields that carry the same IDs (cqn_send, cqn_recv and
input_qpn in xsc_create_qp_request, qpn in xsc_modify_raw_qp_request) are
__be16, and the eth driver casts them with (u16).
If firmware ever returned an ID of 0x8000 or above, would EQE lookups for
that object miss and get reported as bogus? Could they instead alias
another registered CQ/QP and run its callback? Should the create path
reject IDs that don't fit in the EQE queue_id field?
[ ... ]
> +int xsc_core_eth_destroy_cq(struct xsc_core_device *xdev,
> + struct xsc_core_cq *xcq)
> +{
> + struct xsc_cq_table *table = &xdev->cq_table;
> + struct xsc_core_cq *tmp;
> + int err;
> +
> + spin_lock_irq(&table->lock);
> + tmp = radix_tree_delete(&table->tree, xcq->cqn);
> + spin_unlock_irq(&table->lock);
[ ... ]
> + /* the completion reader looks the cq up under rcu_read_lock and
> + * takes its reference afterwards; wait for it before dropping
> + * the tree reference so it never touches a freed cq
> + */
> + synchronize_rcu();
[Severity: Low]
This isn't a bug, but the comment doesn't match xsc_cq_completion(). That
function takes the reference inside the RCU read-side section, before
rcu_read_unlock():
rcu_read_lock();
cq = radix_tree_lookup(&table->tree, cqn);
if (likely(cq))
atomic_inc(&cq->refcount);
rcu_read_unlock();
That placement is what lets synchronize_rcu() order against the final
atomic_dec_and_test(). If atomic_inc() were moved after rcu_read_unlock()
to match the comment, the result would be a use-after-free.
The comment also leaves out xsc_eq_cq_event(), which uses table->lock
rather than RCU. It also doesn't say that any use after the RCU section is
protected only by the refcount and the completion.
Could the comment be updated to describe this accurately?
> +
> + err = xsc_destroy_cq(xdev, xcq->cqn);
> + if (err)
> + goto err_destroy_cq;
> +
> + if (atomic_dec_and_test(&xcq->refcount))
> + complete(&xcq->free);
> + wait_for_completion(&xcq->free);
> + return 0;
> +
> +err_destroy_cq:
> + pci_err(xdev->pdev, "failed to destroy cqn=%d, err=%d\n",
> + xcq->cqn, err);
> + return err;
[Severity: Medium]
What happens to the CQ if xsc_destroy_cq() fails here?
By then radix_tree_delete() has already removed the CQ from cq_table. The
err_destroy_cq path only logs and returns. It never drops the tree
reference and never calls wait_for_completion(&xcq->free).
synchronize_rcu() doesn't drain handlers that already hold a reference.
xsc_cq_completion() calls cq->comp() after rcu_read_unlock():
xsc_cq_completion()
rcu_read_lock();
cq = radix_tree_lookup(...);
atomic_inc(&cq->refcount);
rcu_read_unlock();
cq->comp(cq);
atomic_dec_and_test(&cq->refcount) -> complete(&cq->free)
xsc_eq_cq_event() takes its reference under table->lock, with no RCU at
all. Only the refcount wait covers these handlers, and this error path
skips it.
The hardware CQ also can't be destroyed afterwards. A retry of
xsc_core_eth_destroy_cq() finds nothing in the tree and returns -ENOENT
without sending DESTROY_CQ again.
Later in the series, xsc_eth_close_cq() leaks the CQ buffer on failure,
which avoids DMA into freed pages. xsc_eth_close_channel() then does:
xsc_eth_close_cq(c, &c->qp.rq.cq);
memset(&c->qp.rq, 0, sizeof(struct xsc_rq));
and the same for c->qp.sq. Suppose a handler that wasn't drained loaded
cq->comp before the memset. Could xsc_eth_comp() then read a zeroed
cq->napi and call napi_schedule(NULL)?
On non-RT kernels the hardirq handler usually finishes long before a
failed firmware round trip completes. With PREEMPT_RT, though, the handler
can be preempted after rcu_read_unlock().
mlx5_core_destroy_cq() has the same delete-before-command ordering and
the same early return, so the pattern isn't unique to xsc. Should this
error path still drop the tree reference and wait on xcq->free before
returning?
> +err_delete_cq:
> + pci_err(xdev->pdev, "cqn=%d not found in tree, err=%d\n",
> + xcq->cqn, err);
> + return err;
> +}
[Severity: Low]
This isn't a bug, but cqn is a u32 printed with %d. Several other new
messages do the same:
"failed to destroy cqn=%d" in xsc_core_eth_create_cq() and
xsc_core_eth_destroy_cq()
"bogus CQ, cqn=%d" and "cq->event is NULL, cqn=%d" in eq.c
"sq%d" in xsc_core_eth_destroy_qp()
Other messages in qp.c print the same kind of ID as 0x%x or %u. Could
these use one consistent unsigned format?
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/eq.c b/drivers/net/ethernet/yunsilicon/xsc/pci/eq.c
> index 875a277a83cb8..1ebc8941081a8 100644
> --- a/drivers/net/ethernet/yunsilicon/xsc/pci/eq.c
> +++ b/drivers/net/ethernet/yunsilicon/xsc/pci/eq.c
[ ... ]
> @@ -52,8 +54,63 @@ static void eq_update_ci(struct xsc_eq *eq, int arm)
> xsc_update_eq_ci(eq->dev, eq->eqn, eq->cons_index, arm);
> }
>
> +static void xsc_cq_completion(struct xsc_core_device *xdev, u32 cqn)
> +{
> + struct xsc_cq_table *table = &xdev->cq_table;
> + struct xsc_core_cq *cq;
> +
> + rcu_read_lock();
> + cq = radix_tree_lookup(&table->tree, cqn);
> + if (likely(cq))
> + atomic_inc(&cq->refcount);
> + rcu_read_unlock();
> +
> + if (!cq) {
> + pci_err(xdev->pdev, "Completion event for bogus CQ, cqn=%d\n",
> + cqn);
> + return;
> + }
[Severity: Low]
This runs from the MSI-X hard IRQ handler, via
xsc_msix_handler()->xsc_eq_int(), and the queue ID comes from the device.
Could a misbehaving device flood the console from hardirq context here?
EQEs that arrive after a CQ/QP has left its table could do the same,
since the destroy helpers delete from the tree before sending the
firmware destroy command.
xsc_eq_cq_event() ("Async event for bogus CQ") and xsc_qp_event() ("Async
event for bogus QP") have the same unratelimited pci_err(). The equivalent
message in mlx5_eq_comp_int() uses dev_dbg_ratelimited().
[ ... ]
> @@ -63,6 +120,32 @@ static int xsc_eq_int(struct xsc_eq *eq)
> * checked the ownership bit.
> */
> rmb();
> + switch (eqe->type) {
> + case XSC_EVENT_TYPE_COMP:
> + case XSC_EVENT_TYPE_INTERNAL_ERROR:
> + queue_id = FIELD_GET(XSC_EQE_QUEUE_ID_MASK,
> + le16_to_cpu(eqe->queue_id_data));
> + cqn = queue_id;
> + xsc_cq_completion(xdev, cqn);
> + break;
[Severity: Low]
Is XSC_EVENT_TYPE_INTERNAL_ERROR meant to be handled as a normal
completion?
enum xsc_event lists it with the other error events. xsc_cq_completion()
only calls cq->comp(cq) and drops the event type. So cq->event never sees
an internal error, and nothing is logged for it.
Should this go through xsc_eq_cq_event() like XSC_EVENT_TYPE_CQ_ERROR? Or
does the name not reflect what the hardware reports? The intended
semantics can't be confirmed from the tree.
[ ... ]
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/qp.c b/drivers/net/ethernet/yunsilicon/xsc/pci/qp.c
> new file mode 100644
> index 0000000000000..ca2ad4ab0b42b
> --- /dev/null
> +++ b/drivers/net/ethernet/yunsilicon/xsc/pci/qp.c
> @@ -0,0 +1,180 @@
[ ... ]
> +int xsc_core_eth_modify_qp_status(struct xsc_core_device *xdev,
> + u32 qpn, u16 status)
> +{
> + struct xsc_modify_qp_mbox_out out = {};
> + struct xsc_modify_qp_mbox_in in = {};
> + int ret;
> +
> + in.hdr.opcode = cpu_to_be16(status);
[Severity: Low]
This isn't a bug with the current callers, but the third argument is
named status and printed as "status=%u", yet it is written straight into
the command opcode.
The only valid values are XSC_CMD_OP_* opcodes, such as
XSC_CMD_OP_2RST_QP and XSC_CMD_OP_RTR2RTS_QP, which is what the in-tree
callers pass. struct xsc_modify_qp_mbox_in also has a real state field,
which stays zero here.
Since this helper is exported, could the argument be renamed (to opcode,
for example)? A future caller could otherwise pass a QP state and send
firmware an invalid opcode.
[ ... ]
> +int xsc_core_eth_destroy_qp(struct xsc_core_device *xdev, u32 qpn)
> +{
> + struct xsc_destroy_qp_mbox_out out;
> + struct xsc_destroy_qp_mbox_in in;
> + int err;
> +
> + err = xsc_core_eth_modify_qp_status(xdev, qpn, XSC_CMD_OP_2RST_QP);
> + if (err) {
> + pci_err(xdev->pdev, "failed to set sq%d status=rst, err=%d\n",
> + qpn, err);
> + return err;
> + }
[Severity: Low]
If the 2RST command fails, this returns without ever sending
XSC_CMD_OP_DESTROY_QP. Does that leak the firmware QP context and its
qpn?
The close paths later in the series, xsc_eth_close_qp_sq() and
xsc_eth_close_qp_rq(), leak the WQ buffers when this returns an error.
The open-path rollbacks in xsc_eth_open_qp_sq() and xsc_eth_open_qp_rq()
ignore the return value:
err_sq_destroy:
xsc_core_eth_destroy_qp(xdev, psq->cqp.qpn);
The QP has already been removed from qp_table at that point, so it can't
be retried.
Would a best-effort DESTROY_QP after a failed 2RST be worthwhile? The tree
doesn't show whether firmware requires the QP to be in RST before it can
be destroyed.
> +
> + memset(&in, 0, sizeof(in));
> + memset(&out, 0, sizeof(out));
> + in.hdr.opcode = cpu_to_be16(XSC_CMD_OP_DESTROY_QP);
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126145711.70868-1-tianx%40yunsilicon.com
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: [PATCH v2 07/11] net/xsc: add the ethernet netdev
2026-11-26 14:57 ` [PATCH v2 07/11] net/xsc: add the ethernet netdev Xin Tian
2026-11-27 14:58 ` sashiko-bot
@ 2026-11-27 15:57 ` netdev-bot+sashiko
1 sibling, 0 replies; 33+ messages in thread
From: netdev-bot+sashiko @ 2026-11-27 15:57 UTC (permalink / raw)
To: tianx
Cc: andrew+netdev, davem, edumazet, kuba, pabeni, jgg, leon,
huangjunxian6, weihg, linux-kernel, netdev, linux-rdma,
kerneljasonxing
Thank you for your contribution! Sashiko AI review found 5 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 4
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).
- [Medium] At this commit, xsc_eth_probe() registers a netdev whose ops
table (xsc_netdev_ops in…
- [Low] This patch adds calls to the auxiliary bus API.
- [Low] At this commit, xsc_eth_wq.h declares xsc_eth_cqwq_create(),
xsc_eth_wq_cyc_create(), xsc_eth_wq_destroy() and…
- [Low] Several comments do not match the code.
- [Low] xsc_eth_l2_addr_init()
(drivers/net/ethernet/yunsilicon/xsc/net/main.c:156) declares `char…
--
net/xsc: add the ethernet netdev
This patch registers an ethernet auxiliary device from the xsc PCI core and
adds an auxiliary driver that creates a netdev. The netdev queries the MAC
address, sets the hardware MTU, and implements ndo_open/ndo_stop. The queue
and data path are left to follow-up patches.
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/Kconfig b/drivers/net/ethernet/yunsilicon/xsc/net/Kconfig
> new file mode 100644
> index 0000000000000..7428b26295b66
> --- /dev/null
> +++ b/drivers/net/ethernet/yunsilicon/xsc/net/Kconfig
> @@ -0,0 +1,17 @@
[ ... ]
> +config YUNSILICON_XSC_ETH
> + tristate "Yunsilicon XSC ethernet driver"
> + depends on YUNSILICON_XSC_PCI
> + depends on NET
> + select PAGE_POOL
[Severity: Low]
Should this entry select AUXILIARY_BUS? xsc_eth uses
module_auxiliary_driver(). xsc_pci now builds adev.o, and xsc_adev_add()
in that file calls auxiliary_device_init() and auxiliary_device_add().
AUXILIARY_BUS is a bool with no prompt in drivers/base/Kconfig:
config AUXILIARY_BUS
bool
auxiliary.o is only built under obj-$(CONFIG_AUXILIARY_BUS), and
include/linux/auxiliary_bus.h has no stubs for the disabled case.
If nothing else in the config selects AUXILIARY_BUS, won't the link or
modpost fail with undefined references to __auxiliary_device_add() and
__auxiliary_driver_register()?
At the end of the series, neither YUNSILICON_XSC_PCI nor YUNSILICON_XSC_ETH
selects it.
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/main.c b/drivers/net/ethernet/yunsilicon/xsc/net/main.c
> new file mode 100644
> index 0000000000000..b9e75d7e14e78
> --- /dev/null
> +++ b/drivers/net/ethernet/yunsilicon/xsc/net/main.c
> @@ -0,0 +1,263 @@
[ ... ]
> +static const struct net_device_ops xsc_netdev_ops = {
> + .ndo_open = xsc_eth_open,
> + .ndo_stop = xsc_eth_close,
> +};
[Severity: Medium]
The commit message says the netdev "must not be brought up in this state".
Does anything enforce that?
xsc_netdev_ops has no ndo_start_xmit. xsc_eth_open() always returns 0, so
the interface can be brought up once xsc_eth_probe() has called
register_netdev().
Carrier stays off at this commit, so the qdisc path drops packets. netpoll
does not check carrier, though:
__netpoll_send_skb()
if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
...
netpoll_start_xmit()
netdev_start_xmit()
ops->ndo_start_xmit() <- NULL here
__netpoll_setup() only rejects IFF_DISABLE_NETPOLL devices. Can
configuring netconsole on this interface end in a NULL function pointer
call?
Later in the series, "net/xsc: report link state changes" adds
netif_carrier_on() while xsc_netdev_ops still has no ndo_start_xmit. At
that commit, a plain "ip link set up" with the link up seems to be enough
to hit this.
"net/xsc: add the transmit path" adds .ndo_start_xmit = xsc_eth_xmit, so
the end of the series is fine. The intermediate commits are still
bisectable, though.
Would a stub ndo_start_xmit that drops the skb work until the transmit
patch lands? Failing ndo_open with an error would be another option.
[ ... ]
> +static void xsc_eth_l2_addr_init(struct xsc_adapter *adapter)
> +{
> + struct net_device *netdev = adapter->netdev;
> + char mac[6] = {0};
> + int ret = 0;
> +
> + ret = xsc_eth_get_mac(adapter->xdev, mac);
[Severity: Low]
Is mac[] guaranteed to be u16 aligned here?
xsc_eth_get_mac() fills it with ether_addr_copy(mac, out->mac). It is then
passed to is_valid_ether_addr(), which calls is_zero_ether_addr().
include/linux/etherdevice.h documents that both need u16 alignment:
* Please note: dst & src must both be aligned to u16.
A char[6] on the stack is only 1-byte aligned. Without
CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, could these become unaligned u16
accesses?
Would u8 mac[ETH_ALEN] __aligned(2), with xsc_eth_get_mac() taking a u8 *,
avoid this? The code is unchanged at the end of the series.
[ ... ]
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_wq.h b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_wq.h
> new file mode 100644
> index 0000000000000..35206ddf42f46
> --- /dev/null
> +++ b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_wq.h
> @@ -0,0 +1,150 @@
[ ... ]
> +u32 xsc_wq_cyc_get_size(struct xsc_wq_cyc *wq);
> +
> +int xsc_eth_cqwq_create(struct xsc_core_device *xdev,
> + struct xsc_wq_param *param,
> + u8 q_log_size,
> + u8 ele_log_size,
> + struct xsc_cqwq *wq,
> + struct xsc_wq_ctrl *wq_ctrl);
> +
> +int xsc_eth_wq_cyc_create(struct xsc_core_device *xdev,
> + struct xsc_wq_param *param,
> + u8 q_log_size,
> + u8 ele_log_size,
> + struct xsc_wq_cyc *wq,
> + struct xsc_wq_ctrl *wq_ctrl);
> +void xsc_eth_wq_destroy(struct xsc_wq_ctrl *wq_ctrl);
[Severity: Low]
This isn't a bug, but these prototypes have no definitions at this commit.
The net Makefile only builds main.o:
xsc_eth-y := main.o
Several other items are also unused here:
xsc_queue.h
the queue parameter structs
the CQE/WQE layouts
XSC_INVALID_LKEY and XSC_QUEUE_TYPE_RAW*
adapter->channel
select PAGE_POOL
Nothing calls these functions, so this commit builds and links. Later in
the series, "net/xsc: add the single TX/RX channel" adds xsc_eth_wq.c, and
the transmit and receive patches use the rest.
Could these definitions move into the patches that first use them?
[ ... ]
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/adev.c b/drivers/net/ethernet/yunsilicon/xsc/pci/adev.c
> new file mode 100644
> index 0000000000000..3376ed10ba6ca
> --- /dev/null
> +++ b/drivers/net/ethernet/yunsilicon/xsc/pci/adev.c
> @@ -0,0 +1,133 @@
[ ... ]
> +static void xsc_adev_del(struct xsc_core_device *xdev, int idx)
> +{
[ ... ]
> + /* Clear the slot before teardown: the driver's remove callback runs
> + * synchronously inside auxiliary_device_delete() and may re-enter
> + * xsc_adev_add() for this index.
> + */
> + xdev->xsc_adev_list[idx] = NULL;
[Severity: Low]
Is this comment accurate? xsc_adev_add() is static, and its only caller is
xsc_adev_init(). xsc_adev_init() is only called from xsc_pci_init().
xsc_eth_remove() never calls back into the PCI layer. It doesn't look like
the remove callback can re-enter xsc_adev_add(). The comment is unchanged
at the end of the series.
A couple of other descriptions don't seem to match the code at this commit
either. xsc_eth_remove() has:
/* Runs ndo_stop(), so the queues and the HCA are shut down first. */
xsc_eth_close() only clears adapter->opened, turns the carrier off and
sends XSC_PORT_DOWN. Queue and HCA teardown only arrive in later patches.
The commit message says:
create the device, program the MAC address and MTU
xsc_eth_l2_addr_init() only queries the MAC with XSC_CMD_OP_QUERY_ETH_MAC
and calls eth_hw_addr_set(). Nothing is written to the device.
The commit message also doesn't mention that ndo_open sets the port admin
status to up.
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126145711.70868-1-tianx%40yunsilicon.com
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: [PATCH v2 08/11] net/xsc: add the single TX/RX channel
2026-11-26 14:57 ` [PATCH v2 08/11] net/xsc: add the single TX/RX channel Xin Tian
2026-11-27 14:58 ` sashiko-bot
@ 2026-11-27 15:58 ` netdev-bot+sashiko
1 sibling, 0 replies; 33+ messages in thread
From: netdev-bot+sashiko @ 2026-11-27 15:58 UTC (permalink / raw)
To: tianx
Cc: andrew+netdev, davem, edumazet, kuba, pabeni, jgg, leon,
huangjunxian6, weihg, linux-kernel, netdev, linux-rdma,
kerneljasonxing
Thank you for your contribution! Sashiko AI review found 11 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 4 · Low: 6
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] xsc_eth_alloc_rq()
(drivers/net/ethernet/yunsilicon/xsc/net/main.c) reports success when…
- [Medium] The teardown order in xsc_eth_close() is not the reverse of
xsc_eth_open().
- [Medium] xsc_eth_build_queue_param() sets the RQ depth to `rq_size =
BIT(xdev->caps.log_max_qp_depth)` (line 38) with no clamp.
- [Medium] ENABLE_NIC_HCA in xsc_eth_enable_nic_hca() writes the absolute
QPN returned by CREATE_QP into in.rss.rqn_base (line 745).
- [Medium] net/xsc: WQ DMA buffers freed on xsc_core_eth_destroy_qp()
rollback failure
- [Low] Software and the CREATE_QP mailbox round the WQE stride
differently.
- [Low] When DESTROY_QP fails, xsc_eth_close_qp_rq() and
xsc_eth_close_qp_sq() return early on purpose and leak the WQ buffer,…
- [Low] The patch adds EXPORT_SYMBOL_GPL(xsc_fill_page_array) in
drivers/net/ethernet/yunsilicon/xsc/pci/alloc.c (line 124), and the…
- [Low] In xsc_eth_qp_event(), the branch for an unknown queue type
prints `type` (the async event code) instead of qp->eth_queue_type,…
- [Low] xsc_dma_zalloc_coherent_node() (pci/alloc.c, lines 155-158) saves
the numa_node of the shared, live PCI device, overwrites it with…
- [Low] xsc_eth_open_channel() reads the live IRQ descriptor's affinity
mask without desc->lock: `aff = irq_get_affinity_mask(irqn); if (aff)…
--
net/xsc: add the single TX/RX channel
This patch brings up a single TX/RX channel on the xsc netdev. It builds
the queue parameters, creates the CQs, SQ and RQ through the core, and
enables the NIC HCA from ndo_open. It also adds the work queue ring helpers
and the fragmented DMA buffer allocation helpers in the pci core.
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/main.c b/drivers/net/ethernet/yunsilicon/xsc/net/main.c
> index b9e75d7e14e78..1c7bbc27d8c4f 100644
> --- a/drivers/net/ethernet/yunsilicon/xsc/net/main.c
> +++ b/drivers/net/ethernet/yunsilicon/xsc/net/main.c
> @@ -25,6 +25,774 @@ static void xsc_eth_netdev_init(struct xsc_adapter *adapter)
> netif_carrier_off(adapter->netdev);
> }
>
> +static void xsc_eth_build_queue_param(struct xsc_adapter *adapter,
> + struct xsc_queue_attr *attr, u8 type)
> +{
> + struct xsc_core_device *xdev = adapter->xdev;
> +
> + if (adapter->nic_param.sq_size == 0)
> + adapter->nic_param.sq_size = min_t(u32,
> + BIT(xdev->caps.log_max_qp_depth),
> + XSC_SQCQ_ELE_NUM);
> + if (adapter->nic_param.rq_size == 0)
> + adapter->nic_param.rq_size = BIT(xdev->caps.log_max_qp_depth);
[Severity: Medium]
Should rq_size be clamped here the same way sq_size is?
Further down, the RQ's CQ gets min(XSC_RQCQ_ELE_NUM, caps.max_cqes)
entries. If firmware reports log_max_qp_depth above 11, the RQ ring ends up
deeper than its 2048-entry CQ. Once the receive path fills the ring, could
more completions be outstanding than the CQ can hold?
The sq_size clamp only uses XSC_SQCQ_ELE_NUM and ignores caps.max_cqes, so
the SQ can also end up deeper than its CQ.
Separately, struct xsc_wq_cyc in xsc_eth_wq.h stores the ring size in u16
fields:
struct xsc_wq_cyc {
struct xsc_frag_buf_ctrl fbc;
u16 sz;
u16 wqe_ctr;
u16 cur_sz;
};
xsc_wq_cyc_get_size() returns u32, though. With log_max_qp_depth >= 16,
xsc_eth_wq_cyc_create() would truncate wq->sz to 0. xsc_wq_cyc_missing()
would then never be positive, so the RX ring would never be posted. Can
firmware report a value in that range?
[ ... ]
> + } else if (type == XSC_QUEUE_TYPE_RQ) {
> + attr->ele_num = adapter->nic_param.rq_size;
> + attr->ele_size = xdev->caps.recv_ds_num * XSC_RECV_WQE_DS;
> + attr->ele_log_size = order_base_2(attr->ele_size);
> + attr->q_log_size = order_base_2(attr->ele_num);
> + } else if (type == XSC_QUEUE_TYPE_SQ) {
> + attr->ele_num = adapter->nic_param.sq_size;
> + attr->ele_size = xdev->caps.send_ds_num * XSC_SEND_WQE_DS;
> + attr->ele_log_size = order_base_2(attr->ele_size);
> + attr->q_log_size = order_base_2(attr->ele_num);
> + }
> +}
[Severity: Low]
The software WQE stride here is order_base_2(ds_num * 16), which rounds
up. xsc_eth_open_qp_sq() and xsc_eth_open_qp_rq() pass the hardware a size
that rounds down:
in->req.log_sq_sz = ilog2(xdev->caps.send_ds_num) + q_log_size;
in->req.log_rq_sz = ilog2(xdev->caps.recv_ds_num) + q_log_size;
send_ds_num and recv_ds_num come from firmware, and nothing checks that
they are powers of two. If one is not, do the hardware and software then
disagree on WQE offsets and ring size?
Could one rounding be used consistently, or non-power-of-two values be
rejected?
[ ... ]
> +static void xsc_eth_qp_event(struct xsc_core_qp *qp, int type)
> +{
> + struct xsc_core_device *xdev;
> + struct xsc_rq *rq;
> + struct xsc_sq *sq;
> +
> + if (qp->eth_queue_type == XSC_RES_RQ) {
> + rq = container_of(qp, struct xsc_rq, cqp);
> + xdev = rq->cq.xdev;
> + } else if (qp->eth_queue_type == XSC_RES_SQ) {
> + sq = container_of(qp, struct xsc_sq, cqp);
> + xdev = sq->cq.xdev;
> + } else {
> + pr_err("%s:Unknown eth qp type %d\n", __func__, type);
[Severity: Low]
This isn't a bug, but should this print qp->eth_queue_type instead of type?
type is the async event code, not the queue type that failed the check.
[ ... ]
> +static int xsc_eth_open_channel(struct xsc_adapter *adapter,
> + struct xsc_channel *c,
> + struct xsc_channel_param *chl_param)
> +{
[ ... ]
> + ret = xsc_core_vector2eqn(xdev, 0, &eqn, &irqn);
> + if (ret)
> + goto err_out;
> + aff = irq_get_affinity_mask(irqn);
> + if (aff)
> + c->cpu = cpumask_first(aff);
[Severity: Low]
Can c->cpu end up >= nr_cpu_ids here?
The affinity mask is read without desc->lock. On a system with more than
BITS_PER_LONG CPUs, a concurrent write to /proc/irq/N/smp_affinity could
briefly expose an empty mask. xsc_eth_alloc_cq(), xsc_eth_open_qp_sq() and
xsc_eth_alloc_rq() then use c->cpu in cpu_to_node(c->cpu) without a range
check.
Would cpumask_first_and() with cpu_online_mask, plus a fallback when the
result is >= nr_cpu_ids, be safer?
[ ... ]
> +static int xsc_eth_alloc_rq(struct xsc_channel *c,
> + struct xsc_rq *prq,
> + struct xsc_rq_param *prq_param)
> +{
[ ... ]
> + ret = xsc_eth_wq_cyc_create(c->adapter->xdev, &prq_param->wq,
> + q_log_size, ele_log_size, &prq->wqe.wq,
> + &prq->wq_ctrl);
> + if (ret)
> + return ret;
> +
> + wq_sz = xsc_wq_cyc_get_size(&prq->wqe.wq);
> + prq->wqe.di = kvzalloc_node(array_size(wq_sz, sizeof(*prq->wqe.di)),
> + GFP_KERNEL, cpu_to_node(c->cpu));
> + if (!prq->wqe.di)
> + goto err_destroy_wq;
[Severity: High]
Is ret still 0 at this point? The last assignment was the successful
xsc_eth_wq_cyc_create(). If kvzalloc_node() fails, this path frees the WQ
buffer and returns 0.
xsc_eth_open_qp_rq() then treats the call as a success:
xsc_eth_open_qp_rq()
hw_npages = DIV_ROUND_UP(prq->wq_ctrl.buf.size, XSC_PAGE_SIZE_4K);
...
xsc_core_fill_page_frag_array(&prq->wq_ctrl.buf,
&in->req.pas[0], hw_npages);
ret = xsc_core_eth_create_qp(xdev, in, inlen, &prq->rqn);
xsc_core_frag_buf_free() kfree()s buf->frags but leaves frags, npages and
size unchanged. xsc_core_fill_page_frag_array() therefore reads the freed
frags array. CREATE_QP is then given the DMA addresses of pages that
dma_free_coherent() has already released. Could the device then DMA into
freed memory?
Later, xsc_eth_free_qp_rq() runs, from either the CREATE_QP failure path or
xsc_eth_close_qp_rq(). It calls xsc_eth_wq_destroy() again on the same
stale buf, so every coherent frag and buf->frags would be freed a second
time.
With the receive patch that follows, xsc_eth_modify_qps_channel() also ends
up posting RX WQEs while di == NULL, page_pool == NULL and wqe_bulk == 0.
Should ret be set to -ENOMEM before the goto, and should
xsc_core_frag_buf_free() clear buf->frags and buf->npages?
[ ... ]
> +static void xsc_eth_close_channel(struct xsc_channel *c, bool free_rq)
> +{
> + if (free_rq)
> + xsc_eth_close_qp_rq(c, &c->qp.rq);
> + xsc_eth_close_cq(c, &c->qp.rq.cq);
> + memset(&c->qp.rq, 0, sizeof(struct xsc_rq));
> +
> + xsc_eth_close_qp_sq(c, &c->qp.sq);
> + xsc_eth_close_cq(c, &c->qp.sq.cq);
> + memset(&c->qp.sq, 0, sizeof(struct xsc_sq));
> +}
[Severity: Low]
When DESTROY_QP fails, xsc_eth_close_qp_rq() and xsc_eth_close_qp_sq()
return early on purpose and leak the WQ buffer, because the QP is still
live in firmware. They return void, though.
Does this function then destroy the CQ that the live QP is still bound to,
and memset() away its QPN and wq_ctrl?
If so, there is no way to retry the destroy or reclaim the leaked DMA
pages. The CQN could also be reused by another queue while the old QP
still points at it.
Could the close helpers report the failure, so that the CQ teardown and
the memset() are skipped in that case?
[ ... ]
> +static int xsc_eth_enable_nic_hca(struct xsc_adapter *adapter)
> +{
[ ... ]
> + in.hdr.opcode = cpu_to_be16(XSC_CMD_OP_ENABLE_NIC_HCA);
> +
> + in.rss.rss_en = 1;
> + in.rss.rqn_base = cpu_to_be16(adapter->channel.qp.rq.rqn);
[Severity: Medium]
Does firmware expect an absolute QPN in rqn_base?
struct xsc_hca_cap in common/xsc_cmd.h has raweth_rss_qp_id_base and
raweth_qp_id_base fields. The caps query in pci/main.c never parses them,
and nothing subtracts a base from rqn.
If firmware treats rqn_base as relative to raweth_rss_qp_id_base, RSS could
steer traffic to the wrong RQ on any function where that base is non-zero.
Also, cpu_to_be16() here silently narrows the 24-bit qpn returned by
xsc_core_eth_create_qp() to 16 bits.
[ ... ]
> @@ -45,25 +813,46 @@ static void xsc_set_port_admin_status(struct xsc_adapter *adapter,
[ ... ]
> static int xsc_eth_close(struct net_device *netdev)
> {
> struct xsc_adapter *adapter = netdev_priv(netdev);
> + int ret;
>
> adapter->opened = false;
>
> netif_carrier_off(adapter->netdev);
>
> + xsc_eth_sw_deinit(adapter);
> +
> + ret = xsc_eth_disable_nic_hca(adapter);
> + if (ret)
> + netdev_err(adapter->netdev, "failed to disable nic hca, err=%d\n",
> + ret);
> +
> xsc_set_port_admin_status(adapter, XSC_PORT_DOWN);
>
> - return 0;
> + return ret;
> }
[Severity: Medium]
If xsc_core_eth_create_qp() has succeeded and a later step fails, this
rollback calls xsc_core_eth_destroy_qp() but ignores its return value,
then falls through to xsc_eth_wq_destroy() and frees the SQ WQ pages
anyway.
xsc_eth_close_qp_sq() and xsc_eth_close_qp_rq() in this same patch
check the DESTROY_QP result and deliberately leak the WQ buffer when it
fails, because the QP is still live in firmware with those pages
programmed. Shouldn't the open-time rollback follow the same rule?
As written, a failure in xsc_core_create_resource_common(),
xsc_eth_alloc_qp_sq_db() or xsc_core_eth_modify_raw_qp() followed by a
failed DESTROY_QP hands pages back to the allocator that the device can
still DMA into.
xsc_eth_open_qp_rq() has the same issue at err_destroy_qp. The result
of xsc_core_eth_destroy_qp() is dropped, and xsc_eth_free_qp_rq() then
frees the WQ buffer and destroys the page_pool anyway.
Could both paths check the destroy result and skip the buffer free (and
the page_pool destroy for the RQ) when it fails?
[Severity: Medium]
Should this teardown be the reverse of xsc_eth_open()?
Open creates the queues and then sends ENABLE_NIC_HCA with RSS pointed at
channel.qp.rq.rqn. Close does it the other way round.
xsc_eth_sw_deinit() destroys the RQ/SQ, the CQs, the buffers and the
page_pool first. Only after that is DISABLE_NIC_HCA sent, and the port is
still admin up at that point.
During that window, the HCA steering refers to a QPN that firmware has
already destroyed and may reuse. If DISABLE_NIC_HCA fails, close only logs
an error, and the HCA stays enabled with a stale RQN.
Would it be better to disable the HCA, or redirect RSS, before the queues
are destroyed?
[ ... ]
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/pci/alloc.c b/drivers/net/ethernet/yunsilicon/xsc/pci/alloc.c
> index 46eb0b031511d..0b0478c64be3b 100644
> --- a/drivers/net/ethernet/yunsilicon/xsc/pci/alloc.c
> +++ b/drivers/net/ethernet/yunsilicon/xsc/pci/alloc.c
> @@ -121,3 +121,105 @@ void xsc_fill_page_array(struct xsc_buf *buf, __be64 *pas, unsigned int npages)
> pas[i] = cpu_to_be64(addr);
> }
> }
> +EXPORT_SYMBOL_GPL(xsc_fill_page_array);
[Severity: Low]
This isn't a bug, but is this export needed?
xsc_fill_page_array() is declared only in the private pci/alloc.h. Its only
caller is xsc_create_eq() in pci/eq.c, which is built into the same module.
xsc_eth uses only the new frag helpers.
The commit message doesn't mention this export either.
[ ... ]
> +static void *xsc_dma_zalloc_coherent_node(struct xsc_core_device *xdev,
> + size_t size, dma_addr_t *dma_handle,
> + int node)
> +{
> + struct device *device = &xdev->pdev->dev;
> + int original_node;
> + void *cpu_handle;
> +
> + /* WA for kernels that don't use numa_mem_id in alloc_pages_node */
> + if (node == NUMA_NO_NODE)
> + node = numa_mem_id();
> +
> + original_node = dev_to_node(device);
> + set_dev_node(device, node);
> + cpu_handle = dma_alloc_coherent(device, size, dma_handle, GFP_KERNEL);
> + set_dev_node(device, original_node);
[Severity: Low]
What serializes this save, modify and restore of the PCI device's
numa_node? mlx5_dma_zalloc_coherent_node(), which this looks derived from,
holds priv->alloc_mutex around the same sequence.
Other users of &xdev->pdev->dev can see the temporary node. One example is
the xsc_cmd dma_pool created in cmd.c, which is used from the event
workqueue without rtnl.
Two concurrent callers of the exported xsc_core_frag_buf_alloc_node() could
also interleave their save and restore steps. That would leave
dev->numa_node permanently wrong.
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126145711.70868-1-tianx%40yunsilicon.com
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: [PATCH v2 09/11] net/xsc: report link state changes
2026-11-26 14:57 ` [PATCH v2 09/11] net/xsc: report link state changes Xin Tian
2026-11-27 14:58 ` sashiko-bot
@ 2026-11-27 15:58 ` netdev-bot+sashiko
1 sibling, 0 replies; 33+ messages in thread
From: netdev-bot+sashiko @ 2026-11-27 15:58 UTC (permalink / raw)
To: tianx
Cc: andrew+netdev, davem, edumazet, kuba, pabeni, jgg, leon,
huangjunxian6, weihg, linux-kernel, netdev, linux-rdma,
kerneljasonxing
Thank you for your contribution! Sashiko AI review found 4 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 3 · 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).
- [Medium] In xsc_eth_open()
(drivers/net/ethernet/yunsilicon/xsc/net/main.c), the initial carrier…
- [Medium] xsc_eth_link_event_handler() (main.c:765-778) runs from
xsc_event_work() on xdev->event_wq with no lock held.
- [Medium] xsc_eth_open() (main.c:883-893) samples the link state with
xsc_eth_get_link_status() and sets the carrier before it sets…
- [Low] The comment above xsc_event_work() in
drivers/net/ethernet/yunsilicon/xsc/pci/pci_irq.c (lines 164-169) says…
--
net/xsc: report link state changes
This commit adds xsc_core_query_vport_state() and uses it to set the
netdev carrier in xsc_eth_open(). It also registers a link event callback
with the PCI layer, so the port is re-queried and the carrier updated on
every XSC_CMD_EVENT_RESP_CHANGE_LINK event.
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/main.c b/drivers/net/ethernet/yunsilicon/xsc/net/main.c
> index 1c7bbc27d8c4f..75a6abd6ed136 100644
> --- a/drivers/net/ethernet/yunsilicon/xsc/net/main.c
> +++ b/drivers/net/ethernet/yunsilicon/xsc/net/main.c
> @@ -721,6 +721,62 @@ static void xsc_eth_sw_deinit(struct xsc_adapter *adapter)
[ ... ]
> +static int xsc_eth_change_link_status(struct xsc_adapter *adapter)
> +{
> + bool link_up;
> + int err;
> +
> + err = xsc_eth_get_link_status(adapter, &link_up);
> + if (err) {
> + netdev_err(adapter->netdev, "failed to query link status, err=%d\n",
> + err);
> + return err;
> + }
> +
> + if (link_up && !netif_carrier_ok(adapter->netdev)) {
> + netdev_info(adapter->netdev, "Link up\n");
> + netif_carrier_on(adapter->netdev);
> + } else if (!link_up && netif_carrier_ok(adapter->netdev)) {
> + netdev_info(adapter->netdev, "Link down\n");
> + netif_carrier_off(adapter->netdev);
> + }
> +
> + return 0;
> +}
> +
> +/*
> + * Registered with the PCI layer, which runs it on its own workqueue for
> + * every XSC_CMD_EVENT_RESP_CHANGE_LINK event.
> + */
> +static void xsc_eth_link_event_handler(void *arg)
> +{
> + struct xsc_core_device *xdev = arg;
> + struct xsc_adapter *adapter = xdev->eth_priv;
> + int err;
> +
> + if (!adapter || !adapter->opened)
> + return;
> +
> + err = xsc_eth_change_link_status(adapter);
[Severity: Medium]
Can this race with xsc_eth_close() and leave the carrier on for a closed
device?
The handler runs from xsc_event_work() on xdev->event_wq with no lock
held. Its only guard is a plain read of adapter->opened. After that it
sends a sleeping firmware command and then checks and sets the carrier
in two separate steps.
xsc_eth_close() writes opened and the carrier under RTNL. It does not
exclude or wait for a handler that has already passed the check:
event_wq ip link set down (RTNL)
xsc_eth_link_event_handler()
adapter->opened == true
xsc_eth_change_link_status()
xsc_core_query_vport_state()
xsc_eth_close()
adapter->opened = false;
netif_carrier_off()
xsc_eth_sw_deinit()
...
link_up && !netif_carrier_ok()
netif_carrier_on()
On the next xsc_eth_open(), the "Link down" branch never calls
netif_carrier_off(). If the link is down at reopen, the interface comes
up with carrier and operstate up and stays that way until the next link
transition.
The opened bool acts as a gate guard rather than a real lock. It is also
read and written without READ_ONCE()/WRITE_ONCE().
Would it work to take rtnl_lock() in the handler, or a driver state mutex
that open and close also hold (like state_lock in
mlx5e_update_carrier_work()), and re-check opened under it? Remove and
the probe error path call xsc_irq_event_unregister() without RTNL, so
its flush_workqueue() should not deadlock against that.
> + if (err)
> + netdev_err(adapter->netdev, "failed to change linkstatus, err=%d\n",
> + err);
> +}
[Severity: Low]
This isn't a bug, but once this handler is registered, the comment above
xsc_event_work() in drivers/net/ethernet/yunsilicon/xsc/pci/pci_irq.c no
longer matches what the work does:
/*
* Handle an asynchronous event. This runs on the PCI layer's own workqueue,
* so it outlives any auxiliary driver: the driver only ever registers a
* callback here, and the memory this touches belongs to xsc_core_device
* rather than to the eth private area.
*/
xsc_event_work() calls the callback inline:
handler = READ_ONCE(xdev->link_event_handler);
if (handler)
handler(xdev);
xsc_eth_link_event_handler() dereferences xdev->eth_priv,
adapter->opened and adapter->netdev. All of these live in the
alloc_etherdev() private area that free_netdev() frees.
What actually keeps this safe is that xsc_eth_remove() and the probe
error path call xsc_irq_event_unregister() before free_netdev().
Could the comment say that callbacks may touch client-owned memory, and
that clients must unregister before freeing it?
[ ... ]
> @@ -823,6 +880,16 @@ static int xsc_eth_open(struct net_device *netdev)
> if (ret)
> goto err_sw_deinit;
>
> + ret = xsc_eth_get_link_status(adapter, &link_up);
> + if (ret) {
> + netdev_warn(netdev, "failed to query link status, err=%d\n", ret);
> + } else if (link_up) {
> + netdev_info(netdev, "Link up\n");
> + netif_carrier_on(adapter->netdev);
> + } else {
> + netdev_info(netdev, "Link down\n");
> + }
> +
> adapter->opened = true;
[Severity: Medium]
Can a link change event get lost between the query and this assignment?
The link state is sampled before adapter->opened is set. While opened is
false, xsc_eth_link_event_handler() returns early and records nothing:
if (!adapter || !adapter->opened)
return;
Suppose the link changes after the firmware answers QUERY_VPORT_STATE,
and the CHANGE_LINK work reaches that check before adapter->opened is
set to true. The event is dropped. Nothing re-queries afterwards, so the
carrier stays wrong until the next link transition, which may never come
on a stable link. This looks reachable at least on the first open after
probe.
Also, if xsc_eth_get_link_status() fails here, for example because
xsc_cmd_exec() times out, open only warns and returns 0. The carrier
stays off and the query is never retried. A link that is up and stays
up would then be reported down indefinitely.
Would either of these close the window and let a later event correct a
failed query?
- set opened before the query, as mlx5e_open_locked() does with
MLX5E_STATE_OPENED before updating the carrier
- re-query once opened and admin-up are both set
>
> xsc_set_port_admin_status(adapter, XSC_PORT_UP);
[Severity: Medium]
Does the oper state read above mean anything while the port is still
admin-down?
xsc_eth_close() always ends with:
xsc_set_port_admin_status(adapter, XSC_PORT_DOWN);
So on every re-open, xsc_eth_get_link_status() queries a port the driver
has just set admin-down. XSC_PORT_UP is only sent after the carrier has
been decided.
The reply carries separate admin and oper nibbles:
#define XSC_QUERY_VPORT_ADMIN_STATE_MASK GENMASK(3, 0)
#define XSC_QUERY_VPORT_STATE_MASK GENMASK(7, 4)
xsc_core_query_vport_state() returns the oper nibble.
If the firmware reports oper-down while the port is admin-down, open
takes the "Link down" branch. After that, only a later
XSC_CMD_EVENT_RESP_CHANGE_LINK event can turn the carrier on.
Does the firmware raise that event when admin-up changes the oper state?
If it does not, the netdev stays carrier-off after ip link set down
followed by ip link set up, even though the physical link is up. It
would stay that way until the link next flaps.
mlx5e queries the state before admin-up in the same way and relies on
firmware port change events. So this may be fine if the xsc firmware
behaves the same way.
Would setting opened, then sending XSC_PORT_UP, and only then sampling
the link state make the initial sample match the real link?
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126145711.70868-1-tianx%40yunsilicon.com
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: [PATCH v2 10/11] net/xsc: add the transmit path
2026-11-26 14:57 ` [PATCH v2 10/11] net/xsc: add the transmit path Xin Tian
2026-11-27 14:58 ` sashiko-bot
@ 2026-11-27 15:58 ` netdev-bot+sashiko
1 sibling, 0 replies; 33+ messages in thread
From: netdev-bot+sashiko @ 2026-11-27 15:58 UTC (permalink / raw)
To: tianx
Cc: andrew+netdev, davem, edumazet, kuba, pabeni, jgg, leon,
huangjunxian6, weihg, linux-kernel, netdev, linux-rdma,
kerneljasonxing
Thank you for your contribution! Sashiko AI review found 7 potential
issue(s) to consider.
Critical: 0 · High: 1 · 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] net/xsc: lockless TX queue stop/wake race in xsc_eth_xmit()
- [Medium] The new transmit path in xsc_eth_xmit()
(drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_tx.c) drops frames…
- [Medium] The RX and TX paths fill the same hardware descriptor, struct
xsc_wqe_data_seg, with a raw DMA address in ->va, but they set ->mkey…
- [Medium] The SQ and its completion queue are sized from unrelated
capabilities, while the new TX path asks for one CQE per WQE and…
- [Medium] net/xsc: unconditional CQ doorbell MMIO write in
xsc_poll_tx_cq()
- [Medium] net/xsc: xsc_poll_tx_cq() frees completed TX skbs with
dev_kfree_skb_any()
- [Low] xsc_eth_xmit() starts with `if (!adapter->opened) return
NETDEV_TX_BUSY;`.
--
net/xsc: add the transmit path
This adds an ndo_start_xmit implementation to the xsc ethernet driver. It
posts linear skbs to the send queue and rings the doorbell. A TX completion
handler unmaps and frees the skbs once they have been sent.
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_tx.c b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_tx.c
> new file mode 100644
> index 0000000000000..580af1f231056
> --- /dev/null
> +++ b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_tx.c
> @@ -0,0 +1,87 @@
[ ... ]
> +netdev_tx_t xsc_eth_xmit(struct sk_buff *skb, struct net_device *netdev)
> +{
[ ... ]
> + if (!adapter->opened)
> + return NETDEV_TX_BUSY;
[Severity: Low]
Is this check needed? adapter->opened is a plain bool. xsc_eth_open() and
xsc_eth_close() write it without WRITE_ONCE(), and this code reads it
without a lock or READ_ONCE(). KCSAN would likely report the pair as a data
race.
On close, the core has already quiesced TX before ndo_stop runs.
On open, the queue is woken and the carrier is turned on before the flag is
set:
xsc_eth_activate_channel()
netif_tx_start_queue(psq->txq);
...
xsc_eth_open()
netif_carrier_on(adapter->netdev);
...
adapter->opened = true;
In that window, could a PACKET_QDISC_BYPASS sender going through
__dev_direct_xmit() reach xsc_eth_xmit()? It would get NETDEV_TX_BUSY on an
awake queue, and its skb would be dropped.
The driver API also discourages returning NETDEV_TX_BUSY without stopping
the queue. Could this check be removed so that queue start/stop ordering
handles it, or could the accesses at least be annotated?
> +
> + if (unlikely(skb_is_nonlinear(skb) || skb->len == 0 ||
> + skb->len > sq->hw_mtu))
> + goto err_drop;
> +
> + pi = xsc_wq_cyc_ctr2ix(&sq->wq, sq->pc);
> + wqe = xsc_wq_cyc_get_wqe(&sq->wq, pi);
> + memset(wqe, 0, xdev->caps.send_ds_num * XSC_SEND_WQE_DS);
[ ... ]
> + cseg->data3 = cpu_to_le32(XSC_WQE_CTRL_SEG_DATA3_CE);
> + wqe->data[0].data0 =
> + cpu_to_le32(FIELD_PREP(XSC_WQE_DATA_SEG_DATA0_SEG_LEN_MASK,
> + skb->len));
> + wqe->data[0].va = cpu_to_le64(dma_addr);
[Severity: Medium]
Should data[0].mkey be set here too? The memset() leaves it at 0.
The RX path fills the same struct xsc_wqe_data_seg with a raw DMA address
and marks it with XSC_INVALID_LKEY:
drivers/net/ethernet/yunsilicon/xsc/net/main.c:xsc_eth_alloc_rq() {
...
wqe->data[0].mkey = cpu_to_le32(XSC_INVALID_LKEY);
...
}
Suppose the device treats any mkey other than XSC_INVALID_LKEY (0x100) as a
real memory key. Would every TX descriptor then be checked against MR 0 and
complete with an error, or put the wrong data on the wire?
The tree doesn't show whether the hardware reads mkey on raw ethernet send
WQEs. Still, the two writers of this descriptor disagree. Could the TX path
set mkey to cpu_to_le32(XSC_INVALID_LKEY) the same way RX does?
> +
> + sq->pc++;
> + if (unlikely(!xsc_wqc_has_room_for(&sq->wq, sq->cc, sq->pc,
> + sq->stop_room)))
> + netif_tx_stop_queue(sq->txq);
[Severity: Medium]
Can the SQ completion queue overflow here? Every WQE sets
XSC_WQE_CTRL_SEG_DATA3_CE, so every posted WQE produces a CQE. The queue
is only stopped when the SQ ring itself is full.
xsc_eth_build_queue_param() sizes the SQ and its CQ from different
capabilities:
adapter->nic_param.sq_size = min_t(u32,
BIT(xdev->caps.log_max_qp_depth),
XSC_SQCQ_ELE_NUM);
...
} else if (type == XSC_QUEUE_TYPE_SQCQ) {
attr->ele_num = min_t(int, XSC_SQCQ_ELE_NUM,
xdev->caps.max_cqes);
caps.max_cqes is 1 << log_max_cq_sz as reported by firmware. If
log_max_cq_sz is below 10 while log_max_qp_depth is 10 or more, up to 1024
WQEs, and so up to 1024 CQEs, can be outstanding against a smaller CQ
before NAPI reaps them.
If that happens, the CQ error event would only be logged by
xsc_eth_cq_error_event(), and TX completions would stall.
Neither xsc_eth_xmit() nor xsc_wqc_has_room_for() takes the CQ capacity
into account. Should sq_size be clamped to the SQ CQ size?
> +
> + if (!netdev_xmit_more() || netif_xmit_stopped(sq->txq))
> + xsc_update_tx_db(xdev, sq->sqn, sq->pc << ds_num_log);
> +
> + return NETDEV_TX_OK;
> +
> +err_drop:
> + dev_kfree_skb_any(skb);
[Severity: Medium]
Are these drops counted anywhere? Four cases end up here: non-linear skbs,
zero-length skbs, skbs longer than sq->hw_mtu, and dma_map_single()
failures. Nothing calls dev_core_stats_tx_dropped_inc() or updates a driver
counter.
Successful sends aren't counted either. No tx_packets or tx_bytes are kept,
and xsc_netdev_ops has only ndo_open, ndo_stop and ndo_start_xmit, with no
ndo_get_stats64. This seems to still be the case after the later commit
"net/xsc: add the receive path".
Won't ip -s link then show no TX activity and no TX drops for this device,
even when DMA mapping fails or frames are rejected?
> + if (!netdev_xmit_more() || netif_xmit_stopped(sq->txq))
> + xsc_update_tx_db(xdev, sq->sqn, sq->pc << ds_num_log);
> + return NETDEV_TX_OK;
> +}
[ ... ]
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_txrx.c b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_txrx.c
> --- /dev/null
> +++ b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_txrx.c
> @@ -0,0 +1,70 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/* Copyright (C) 2021-2026, Shanghai Yunsilicon Technology Co., Ltd.
> + * All rights reserved.
> + */
> +
> +#include "xsc_eth.h"
> +#include "xsc_eth_txrx.h"
> +
> +struct xsc_cqe *xsc_cqwq_get_cqe(struct xsc_cqwq *wq)
> +{
> + struct xsc_cqe *cqe = xsc_frag_buf_get_wqe(&wq->fbc,
> + xsc_cqwq_get_ci(wq));
> +
> + if (!!(le32_to_cpu(cqe->data1) & XSC_CQE_DATA1_OWNER) !=
> + (xsc_cqwq_get_wrap_cnt(wq) & 1))
> + return NULL;
> +
> + return cqe;
> +}
> +
> +int xsc_poll_tx_cq(struct xsc_cq *cq, int budget)
> +{
> + struct xsc_cqe *cqe;
> + struct device *dev;
> + struct xsc_sq *sq;
> + u16 sqcc;
> + int i = 0;
> +
> + sq = container_of(cq, struct xsc_sq, cq);
> + if (!test_bit(XSC_ETH_SQ_STATE_ENABLED, &sq->state))
> + return 0;
> +
> + dev = sq->channel->adapter->dev;
> + sqcc = sq->cc;
> +
> + while (i < budget) {
> + struct xsc_tx_wqe_info *wi;
> + struct sk_buff *skb;
> +
> + cqe = xsc_cqwq_get_cqe(&cq->wq);
> + if (!cqe)
> + break;
> + /* ensure cqe content is read after cqe ownership bit */
> + dma_rmb();
> +
> + xsc_cqwq_pop(&cq->wq);
> + wi = &sq->wqe_info[xsc_wq_cyc_ctr2ix(&sq->wq, sqcc)];
> +
> + skb = wi->skb;
> + wi->skb = NULL;
> + if (skb) {
> + dma_unmap_single(dev, wi->dma_addr, skb->len,
> + DMA_TO_DEVICE);
> + dev_kfree_skb_any(skb);
> + }
> +
> + sqcc++;
> + i++;
> + }
> +
> + xsc_update_cq_ci(cq->xdev, cq->xcq.cqn, cq->wq.cc);
> +
> + sq->cc = sqcc;
> +
> + if (netif_tx_queue_stopped(sq->txq) &&
> + xsc_wqc_has_room_for(&sq->wq, sq->cc, sq->pc, sq->stop_room))
> + netif_tx_wake_queue(sq->txq);
> +
> + return i == budget;
> +}
[Severity: Medium]
Is dev_kfree_skb_any() the right call for a completed TX skb? It goes
through dev_kfree_skb_any_reason() with SKB_DROP_REASON_NOT_SPECIFIED.
Every frame that was transmitted successfully would then be reported
as a drop by the skb:kfree_skb tracepoint and by drop_monitor.
This runs from the channel NAPI poll, so napi_consume_skb() would
account the skb as consumed. It would also let the skb use the NAPI
bulk free path. That needs the NAPI budget to reach
xsc_poll_tx_cq(), with a budget of 0 passed through when called from
netpoll. Would napi_consume_skb(skb, budget) work here, or at least
dev_consume_skb_any()?
[Severity: High]
What orders the queue stop in xsc_eth_xmit() against the wake here?
xsc_poll_tx_cq() runs from NAPI without the TX queue lock. Neither
side has the barriers that netif_txq_maybe_stop() and
netif_txq_completed_wake() provide.
In xsc_eth_xmit(), netif_tx_stop_queue() is called based on a plain
read of sq->cc. There is no smp_mb__after_atomic() and no re-check
of xsc_wqc_has_room_for() afterwards. Here, sq->cc = sqcc is followed
directly by netif_tx_queue_stopped() with no smp_mb() in between.
Couldn't this interleave as:
CPU0 (xsc_eth_xmit) CPU1 (xsc_poll_tx_cq)
reads old sq->cc, ring full
sq->cc = sqcc
netif_tx_queue_stopped() == false
netif_tx_stop_queue()
That would leave the queue stopped with free descriptors and no
further completions to wake it.
The other direction looks worse. Suppose this function observes
__QUEUE_STATE_DRV_XOFF but loads a stale sq->pc from before the
final post. It can then call netif_tx_wake_queue() on a full ring,
even on a poll that reaped no CQEs. Nothing orders the state bit
test before the sq->pc load.
xsc_eth_xmit() never checks for room before deriving pi from sq->pc.
The next frame would then overwrite the WQE and sq->wqe_info[pi] of a
descriptor the device still owns, leaking that skb and its DMA
mapping. Once sq->pc is more than a ring size ahead of sq->cc,
xsc_wqc_has_room_for() wraps and reports room again. The queue would
stay awake and keep overwriting in-flight entries.
Would it make sense to use netif_txq_maybe_stop() and
netif_txq_completed_wake() from include/net/netdev_queues.h? That
would go with READ_ONCE()/WRITE_ONCE() on sq->cc and sq->pc. Could
xsc_eth_xmit() also stop the queue and return NETDEV_TX_BUSY if it is
ever entered without room?
[Severity: Medium]
Does this need to ring the CQ consumer doorbell when nothing was
reaped? If xsc_cqwq_get_cqe() returns NULL on the first iteration,
i is 0 and cq->wq.cc has not moved. xsc_update_cq_ci() still issues
a wmb() and an uncached writel() to DB_CQ_CID_DIRECT_MEM_ADDR.
The channel NAPI poll will call this on every pass, including passes
that only have RX work. With RX-heavy traffic or idle TX, most of
these MMIO writes would be redundant. The sq->cc = sqcc store is also
unconditional and dirties the cacheline that xsc_eth_xmit() reads on
the transmitting CPU.
Could the doorbell and the sq->cc update be skipped when i == 0?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126145711.70868-1-tianx%40yunsilicon.com
^ permalink raw reply [flat|nested] 33+ messages in thread
* Re: [PATCH v2 11/11] net/xsc: add the receive path
2026-11-26 14:57 ` [PATCH v2 11/11] net/xsc: add the receive path Xin Tian
2026-11-27 14:58 ` sashiko-bot
@ 2026-11-27 15:58 ` netdev-bot+sashiko
1 sibling, 0 replies; 33+ messages in thread
From: netdev-bot+sashiko @ 2026-11-27 15:58 UTC (permalink / raw)
To: tianx
Cc: andrew+netdev, davem, edumazet, kuba, pabeni, jgg, leon,
huangjunxian6, weihg, linux-kernel, netdev, linux-rdma,
kerneljasonxing
Thank you for your contribution! Sashiko AI review found 12 potential
issue(s) to consider.
Critical: 0 · High: 2 · Medium: 6 · Low: 4
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] xsc_skb_from_cqe() builds each RX skb with napi_build_skb(va,
XSC_SKB_FRAG_SZ(cqe_bcnt)), where cqe_bcnt is the packet length…
- [High] In xsc_eth_alloc_rq(), if kvzalloc_node() for prq->wqe.di fails,
the code does goto err_destroy_wq while ret still holds 0 from the…
- [Medium] xsc_eth_napi_poll() calls xsc_eth_post_rx_wqes(rq) on every
poll, including polls with budget == 0.
- [Medium] When xsc_alloc_rx_wqes() fails, xsc_eth_post_rx_wqes() returns
true from inside its do/while loop.
- [Medium] xsc_eth_build_queue_param() clamps the SQ depth to its CQ:
sq_size = min(BIT(log_max_qp_depth), XSC_SQCQ_ELE_NUM).
- [Medium] In xsc_eth_open_cq(), the CQ becomes reachable from the EQ IRQ
path before its NAPI context exists.
- [Medium] xsc_eth_open_channels() calls
napi_enable(&adapter->channel.napi) before…
- [Medium] net/xsc: unconditional TX CQ doorbell MMIO write in
xsc_poll_tx_cq()
- [Low] The commit message only mentions the RX path and a shared NAPI
poll.
- [Low] In xsc_skb_from_cqe(), net_prefetch(va +
SKB_DATA_ALIGN(sizeof(struct skb_shared_info))) does not match this…
- [Low] xsc_dump_error_rqcqe() prints u32 values (rq->cq.xcq.cqn,
xsc_cqwq_get_ci(), rq->rqn) with %d and the QP id with %lu.
- [Low] In xsc_eth_deactivate_channel(), XSC_ETH_RQ_STATE_ENABLED is
cleared, then synchronize_net() runs, then napi_disable().
--
net/xsc: add the receive path
This patch adds the RX path for the xsc driver. It builds linear skbs
from page pool pages on RQ completions. A single NAPI context per
channel polls both the TX and RX completion queues, and the CQ completion
callback is wired to schedule it.
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/main.c b/drivers/net/ethernet/yunsilicon/xsc/net/main.c
> index 8eda3c8b12f25..e7b0871c1ccb7 100644
> --- a/drivers/net/ethernet/yunsilicon/xsc/net/main.c
> +++ b/drivers/net/ethernet/yunsilicon/xsc/net/main.c
> @@ -106,6 +106,13 @@ static void xsc_eth_cq_error_event(struct xsc_core_cq *xcq,
> "Eth catch CQ ERROR: %x, cqn: %d\n", event, xcq->cqn);
> }
>
> +static void xsc_eth_comp(struct xsc_core_cq *xcq)
> +{
> + struct xsc_cq *cq = container_of(xcq, struct xsc_cq, xcq);
> +
> + napi_schedule(cq->napi);
> +}
> +
[ ... ]
> @@ -204,6 +212,7 @@ static int xsc_eth_open_cq(struct xsc_channel *c,
>
> xsc_arm_cq(pcq->xdev, pcq->xcq.cqn, pcq->wq.cc, 0);
>
> + pcq->napi = &c->napi;
> pcq->channel = c;
>
> return 0;
[Severity: Medium]
Can xsc_eth_comp() run before cq->napi is valid? This is the order of
operations:
xsc_eth_alloc_cq()
core_cq->comp = xsc_eth_comp;
xsc_eth_set_cq()
CQ inserted into cq_table
xsc_arm_cq()
CQ armed
pcq->napi = &c->napi; (no barrier)
...
xsc_eth_open_channel()
netif_napi_add(adapter->netdev, &c->napi, xsc_eth_napi_poll);
A COMP or INTERNAL_ERROR EQE for this CQN can arrive before pcq->napi is
assigned. In that case xsc_eth_comp() calls napi_schedule(NULL) from
hardirq context.
A second window exists after the pointer is set but before
netif_napi_add() runs. Then napi_schedule_prep() succeeds on the zeroed
state and queues a napi with a NULL ->poll. The later netif_napi_add()
then reinitializes a poll_list that is still linked.
The normal open sequence does not post work to these CQs this early, so
a stale or spurious EQE would be needed. Would it be safer to set
cq->napi and call netif_napi_add() before the CQs are created and armed?
[ ... ]
> @@ -465,6 +476,8 @@ static int xsc_eth_modify_qps_channel(struct xsc_adapter *adapter,
> {
> int ret;
>
> + xsc_eth_post_rx_wqes(&c->qp.rq);
> +
[Severity: High]
What happens here if xsc_eth_alloc_rq() failed to allocate the di array?
That error path was added earlier in the series by "net/xsc: add the
single TX/RX channel":
xsc_eth_alloc_rq() {
...
prq->wqe.di = kvzalloc_node(array_size(wq_sz, sizeof(*prq->wqe.di)),
GFP_KERNEL, cpu_to_node(c->cpu));
if (!prq->wqe.di)
goto err_destroy_wq;
...
}
On that path ret is still 0 from xsc_eth_wq_cyc_create(). The cleanup
calls xsc_eth_wq_destroy()->xsc_core_frag_buf_free(), which frees the WQ
pages and kfree()s buf->frags, and the function then returns success.
After that, xsc_eth_open_qp_rq()->xsc_core_fill_page_frag_array() reads
the freed buf->frags[] and passes the freed DMA addresses to firmware in
CREATE_QP.
With this patch, the RQ then reaches xsc_eth_post_rx_wqes() with
rq->wqe.wqe_bulk still 0:
do {
head = xsc_wq_cyc_get_head(wq);
alloc = min_t(int, rq->wqe.wqe_bulk, xsc_wq_cyc_missing(wq));
...
xsc_wq_cyc_push_n(wq, alloc);
} while (xsc_wq_cyc_missing(wq) >= rq->wqe.wqe_bulk);
alloc is 0, nothing gets pushed, and the condition is always true. Does
this loop forever in ndo_open with rtnl held? If it were ever escaped,
xsc_eth_close_qp_rq()->xsc_eth_dealloc_rx_wqe() would dereference the
NULL di.
Should ret be set to -ENOMEM before the goto in xsc_eth_alloc_rq()?
> ret = xsc_core_eth_modify_qp_status(adapter->xdev, c->qp.rq.rqn,
> XSC_CMD_OP_RTR2RTS_QP);
[ ... ]
> @@ -662,6 +684,9 @@ static int xsc_eth_open_channels(struct xsc_adapter *adapter)
> goto err_close_channel;
> free_rq = true;
>
> + napi_enable(&adapter->channel.napi);
> + napi_enabled = true;
> +
> ret = xsc_eth_modify_qps_channel(adapter, &adapter->channel);
> if (ret)
> goto err_close_channel;
[Severity: Medium]
NAPI is enabled here before xsc_eth_modify_qps_channel() fills the RX
ring from process context:
xsc_eth_modify_qps_channel()
xsc_eth_post_rx_wqes()
xsc_alloc_rx_wqes()
page_pool_dev_alloc_pages()
xsc_wq_cyc_push_n()
xsc_rq_notify_hw()
Once NAPI is enabled, any completion scheduled through xsc_eth_comp()
runs xsc_eth_napi_poll(). That poll calls xsc_eth_post_rx_wqes(rq)
unconditionally, because only xsc_poll_rx_cq() checks
XSC_ETH_RQ_STATE_ENABLED.
Nothing serializes the two callers. They could race on wq->wqe_ctr,
wq->cur_sz, the di[] slots, the RX doorbell value, and the page pool's
lockless alloc cache. That cache expects to be used only from the owning
NAPI.
In the normal open sequence this would need a spurious or stale EQE.
Could the ring be filled before napi_enable(), or filled only from NAPI
by calling napi_schedule() after enabling it?
[ ... ]
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_rx.c b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_rx.c
> new file mode 100644
> index 0000000000000..2c07f80928ea2
> --- /dev/null
> +++ b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_rx.c
> @@ -0,0 +1,210 @@
[ ... ]
> +static struct sk_buff *xsc_skb_from_cqe(struct xsc_rq *rq,
> + struct xsc_dma_info *di,
> + u32 cqe_bcnt)
> +{
> + struct sk_buff *skb;
> + void *va;
> + u32 frag_size;
> +
> + va = page_address(di->page);
> + frag_size = XSC_SKB_FRAG_SZ(cqe_bcnt);
> +
> + dma_sync_single_range_for_cpu(rq->cq.xdev->device, di->addr,
> + 0, frag_size, DMA_FROM_DEVICE);
> + net_prefetch(va + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
[Severity: Low]
Is this prefetch offset intended? The WQE is posted with the raw page DMA
address (wqe->data[0].va = cpu_to_le64(di->addr)), and napi_build_skb()
is called without skb_reserve(). So the Ethernet header is at va + 0 and
shinfo is at va + SKB_DATA_ALIGN(cqe_bcnt).
This prefetch lands about 320 bytes into the payload, or past the end of
small frames.
The skb also ends up with zero headroom, even though xsc_eth_common.h
defines XSC_ETH_RX_MAX_HEAD_ROOM and XSC_SW2HW_RX_PKT_LEN accounts for
it. Was headroom meant to be reserved here?
> +
> + skb = napi_build_skb(va, frag_size);
[Severity: High]
Does this undercount skb->truesize? frag_size is derived from the packet
length reported by the device, but each WQE owns a whole order-0 page:
xsc_eth_alloc_rq() {
...
prq->wqe.wqe_bulk = PAGE_SIZE /
roundup_pow_of_two(xsc_rx_get_linear_frag_sz(c->adapter->nic_param.mtu));
pagepool_params.order = XSC_RX_FRAG_SZ_ORDER;
...
}
xsc_alloc_rx_wqes() allocates one page_pool page per WQE and never
shares a page between WQEs. The frags-per-page formula above is only
used as a batch count.
__finalize_skb_around() sets truesize to
SKB_TRUESIZE(SKB_DATA_ALIGN(cqe_bcnt)). A 60-byte frame is charged
roughly 640 bytes while it pins 4 KiB. On 64 KiB page systems the gap is
close to 100x.
Socket rcvbuf, TCP rmem and OOO pruning, ipfrag_high_thresh and GRO
merged truesize all rely on this value. A remote sender of small frames
or fragments could therefore pin much more memory than the limits allow.
Should the skb be built with the real buffer size, so truesize reflects
the page being pinned?
> + if (unlikely(!skb))
> + return NULL;
[ ... ]
> +static void xsc_dump_error_rqcqe(struct xsc_rq *rq, struct xsc_cqe *cqe)
> +{
> + struct xsc_channel *c = rq->cq.channel;
> + u32 data0 = le32_to_cpu(cqe->data0);
> +
> + net_err_ratelimited("Error cqe on dev=%s, cqn=%d, ci=%d, rqn=%d, qpn=%lu, error_code=0x%lx\n",
> + c->netdev->name, rq->cq.xcq.cqn,
> + xsc_cqwq_get_ci(&rq->cq.wq), rq->rqn,
> + FIELD_GET(XSC_CQE_DATA0_QP_ID_MASK, data0),
> + FIELD_GET(XSC_CQE_DATA0_ERR_CODE_MASK, data0));
> +}
[Severity: Low]
This isn't a bug, but cqn, ci and rqn are u32 values printed with %d, and
the QP id is printed with %lu.
The TX counterpart added in this patch, xsc_dump_error_sqcqe(), prints
the same CQE fields in hex. It also uses a different order (err_code
before qpid). Could the two dumps use the same format?
[ ... ]
> +bool xsc_eth_post_rx_wqes(struct xsc_rq *rq)
> +{
> + struct xsc_wq_cyc *wq = &rq->wqe.wq;
> + int alloc;
> + u16 head;
> +
> + if (xsc_wq_cyc_missing(wq) < rq->wqe.wqe_bulk)
> + return false;
> +
> + do {
> + head = xsc_wq_cyc_get_head(wq);
> + alloc = min_t(int, rq->wqe.wqe_bulk, xsc_wq_cyc_missing(wq));
> +
> + if (xsc_alloc_rx_wqes(rq, head, alloc))
> + return true;
[Severity: Medium]
Are the WQEs from earlier iterations lost to the device here? Those bulks
have already advanced wq->wqe_ctr and wq->cur_sz through
xsc_wq_cyc_push_n(). This early return skips dma_wmb() and
xsc_rq_notify_hw(), so the hardware is never told about them.
At open time this matters more. xsc_eth_modify_qps_channel() ignores the
return value and moves the RQ to RTS anyway. xsc_eth_activate_channel()
only sets XSC_ETH_RQ_STATE_ENABLED and never schedules NAPI.
If the initial fill fails (page_pool_dev_alloc_pages() is GFP_ATOMIC for
up to wq_sz pages), the hardware RQ has no buffers. No RX CQE or RX
interrupt can then occur. The refill would only be retried if a TX
completion happens to schedule the shared NAPI.
Should the doorbell be rung for what was already pushed? Should open
either fail or schedule NAPI when the initial fill comes up short?
> +
> + xsc_wq_cyc_push_n(wq, alloc);
> + } while (xsc_wq_cyc_missing(wq) >= rq->wqe.wqe_bulk);
[Severity: Medium]
This loop keeps the RQ nearly full. Is the RQ depth guaranteed to fit in
the RQ CQ? The SQ depth is clamped to XSC_SQCQ_ELE_NUM in
xsc_eth_build_queue_param(), but the RQ depth is not:
xsc_eth_build_queue_param() {
...
if (adapter->nic_param.rq_size == 0)
adapter->nic_param.rq_size = BIT(xdev->caps.log_max_qp_depth);
...
}
The RQ CQ is min(XSC_RQCQ_ELE_NUM, caps.max_cqes). Firmware could report
log_max_qp_depth > 11, or max_cqes < rq_size. Then the device can have
more outstanding receives than the CQ has entries.
xsc_eth_handle_rx_cqe() derives the WQE index from rq->cq.wq.cc. A CQ
overrun would therefore pair CQEs with the wrong buffers.
Separately, the RX doorbell PID field is only 13 bits:
#define XSC_RX_DB_DATA0_NEXT_PID_MASK GENMASK(12, 0)
xsc_eth_open_qp_rq() creates the QP with
log_rq_sz = ilog2(recv_ds_num) + q_log_size, and nothing checks the two
against each other.
Should rq_size be clamped the same way the SQ is, and checked against
the doorbell width?
> +
> + dma_wmb();
> +
> + /* ensure wqes are visible to device before updating doorbell record */
> + xsc_rq_notify_hw(rq);
> +
> + return false;
> +}
> +
> +int xsc_eth_napi_poll(struct napi_struct *napi, int budget)
> +{
> + struct xsc_channel *c = container_of(napi, struct xsc_channel, napi);
> + struct xsc_rq *rq = &c->qp.rq;
> + bool busy;
> + int work_done;
> +
> + busy = xsc_poll_tx_cq(&c->qp.sq.cq, xsc_cqwq_get_size(&c->qp.sq.cq.wq));
> +
> + work_done = xsc_poll_rx_cq(&rq->cq, budget);
> + busy |= work_done == budget;
> +
> + if (xsc_eth_post_rx_wqes(rq))
> + busy = true;
[Severity: Medium]
This patch makes xsc_eth_napi_poll() the only caller of
xsc_poll_tx_cq(), so the TX CQ is now reaped on every NAPI poll,
including polls driven purely by RX. xsc_poll_tx_cq() still ends with
an unconditional doorbell write:
xsc_update_cq_ci(cq->xdev, cq->xcq.cqn, cq->wq.cc);
That write happens even when i == 0 and cq->wq.cc did not move. It
costs a wmb() and a writel() to DB_CQ_CID_DIRECT_MEM_ADDR and achieves
nothing.
When the poll then completes, xsc_cq_notify_hw_rearm() writes the SQ
CQ doorbell again through xsc_arm_cq(). An idle TX side therefore adds
two full barriers and two MMIO writes to every RX interrupt.
xsc_poll_rx_cq() already guards xsc_cq_notify_hw() with
if (work_done). Could xsc_poll_tx_cq() do the same, for example by
calling xsc_cq_notify_hw() only when i is non-zero?
There are two related points in the same path.
The TX budget passed in is xsc_cqwq_get_size(), the whole SQ CQ. One
poll can spend up to a full ring of unmaps and frees before RX is
looked at.
Completed skbs are released with dev_kfree_skb_any(). That skips the
NAPI bulk free cache, and it also reports successfully transmitted
skbs as drops.
Now that this only runs from NAPI, would napi_consume_skb(skb, budget)
be a better fit? It would need the real NAPI budget passed down
instead of the ring size. That way the netpoll case with budget == 0
still falls back to dev_consume_skb_any().
[Severity: Medium]
Is the refill safe when budget is 0? netpoll calls the poll function
with a zero budget, usually with IRQs disabled:
netpoll_send_skb()
netpoll_poll_dev()
poll_napi()
poll_one_napi()
napi->poll(napi, 0)
xsc_poll_rx_cq() does nothing in that case, but xsc_eth_post_rx_wqes()
still runs. It calls page_pool_dev_alloc_pages(), and on failure its
unwind calls page_pool_recycle_direct().
Documentation/networking/napi.rst says "Rx specific APIs like page pool
or XDP cannot be used at all when budget is 0" and "if the argument is 0
driver cannot call any XDP (or page pool) APIs".
With hardirqs disabled, the lockdep_assert_no_hardirq() in
__page_pool_put_page() would fire. The pool's lockless alloc cache could
also be touched while this CPU's NAPI was interrupted mid-recycle.
Should the RX refill be skipped when budget is 0?
> +
> + if (busy)
> + return budget;
> +
> + if (unlikely(!napi_complete_done(napi, work_done)))
> + return work_done;
> +
> + xsc_cq_notify_hw_rearm(&c->qp.sq.cq);
> + xsc_cq_notify_hw_rearm(&rq->cq);
[Severity: Low]
Can this re-arm loop without progress during ifdown?
xsc_eth_deactivate_channel() does this:
clear_bit(XSC_ETH_RQ_STATE_ENABLED, &c->qp.rq.state);
clear_bit(XSC_ETH_SQ_STATE_ENABLED, &c->qp.sq.state);
synchronize_net();
/* last doorbell out */
napi_disable(&c->napi);
The RQ stays in RTS with RSS active for that whole window, because
xsc_eth_disable_nic_hca() only runs after xsc_eth_sw_deinit() in
xsc_eth_close().
Packets arriving in this window generate RX CQEs. xsc_poll_rx_cq()
returns 0 because the bit is cleared, NAPI completes, and the RQ CQ is
re-armed with a consumer index behind the hardware producer.
If the hardware raises an event whenever a CQ with pending CQEs is
armed, this would cycle interrupt -> NAPI -> re-arm until napi_disable().
Should the RQ CQ re-arm be skipped once the RQ is disabled?
> +
> + return work_done;
> +}
> diff --git a/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_txrx.c b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_txrx.c
> index ee6554ed80040..b3c926506a5e8 100644
> --- a/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_txrx.c
> +++ b/drivers/net/ethernet/yunsilicon/xsc/net/xsc_eth_txrx.c
[ ... ]
> @@ -43,6 +65,9 @@ int xsc_poll_tx_cq(struct xsc_cq *cq, int budget)
> /* ensure cqe content is read after cqe ownership bit */
> dma_rmb();
>
> + if (unlikely(le32_to_cpu(cqe->data0) & XSC_CQE_DATA0_IS_ERR))
> + xsc_dump_error_sqcqe(sq, cqe);
> +
> xsc_cqwq_pop(&cq->wq);
[Severity: Low]
This isn't a bug, but the commit message only talks about the receive
path:
Receive linear skbs from the completion queue with a page pool and
handle both queue completions from a single NAPI poll.
The patch also changes TX behaviour. It adds xsc_dump_error_sqcqe() and
this error check. It is also the first patch to set
core_cq->comp = xsc_eth_comp, which is what makes xsc_poll_tx_cq()
actually run.
In the previous commit, "net/xsc: add the transmit path", xsc_poll_tx_cq()
had no caller, even though that commit's message says it reaps
completions. Could the TX changes be mentioned here, or moved into the TX
patch?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126145711.70868-1-tianx%40yunsilicon.com
^ permalink raw reply [flat|nested] 33+ messages in thread
end of thread, other threads:[~2026-11-27 15:58 UTC | newest]
Thread overview: 33+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 14:57 [PATCH v2 00/11] net/xsc: add the Yunsilicon XSC ethernet driver Xin Tian
2026-11-26 14:57 ` [PATCH v2 01/11] net/xsc: add Yunsilicon XSC ethernet PCI driver Xin Tian
2026-11-27 15:57 ` netdev-bot+sashiko
2026-11-26 14:57 ` [PATCH v2 02/11] net/xsc: add doorbell and cmdq MMIO accessors Xin Tian
2026-11-27 15:57 ` netdev-bot+sashiko
2026-11-26 14:57 ` [PATCH v2 03/11] net/xsc: add the command queue Xin Tian
2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:57 ` netdev-bot+sashiko
2026-11-26 14:57 ` [PATCH v2 04/11] net/xsc: query HCA capabilities and reset function Xin Tian
2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:57 ` netdev-bot+sashiko
2026-11-26 14:57 ` [PATCH v2 05/11] net/xsc: add event queues and MSI-X Xin Tian
2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:57 ` netdev-bot+sashiko
2026-11-26 14:57 ` [PATCH v2 06/11] net/xsc: add CQ and QP helpers Xin Tian
2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:57 ` netdev-bot+sashiko
2026-11-26 14:57 ` [PATCH v2 07/11] net/xsc: add the ethernet netdev Xin Tian
2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:57 ` netdev-bot+sashiko
2026-11-26 14:57 ` [PATCH v2 08/11] net/xsc: add the single TX/RX channel Xin Tian
2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:58 ` netdev-bot+sashiko
2026-11-26 14:57 ` [PATCH v2 09/11] net/xsc: report link state changes Xin Tian
2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:58 ` netdev-bot+sashiko
2026-11-26 14:57 ` [PATCH v2 10/11] net/xsc: add the transmit path Xin Tian
2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:58 ` netdev-bot+sashiko
2026-11-26 14:57 ` [PATCH v2 11/11] net/xsc: add the receive path Xin Tian
2026-11-27 14:58 ` sashiko-bot
2026-11-27 15:58 ` netdev-bot+sashiko
2026-11-27 14:58 ` [PATCH v2 00/11] net/xsc: add the Yunsilicon XSC ethernet driver sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox