Thread View: linux.kernel.netdev
2 messages
2 total messages
Started by "Xin Tian"
Thu, 26 Nov 2026 14:57
[PATCH v2 06/11] net/xsc: add CQ and QP helpers
Author: "Xin Tian"
Date: Thu, 26 Nov 2026 14:57
Date: Thu, 26 Nov 2026 14:57
634 lines
26865 bytes
26865 bytes
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
Re: [PATCH v2 06/11] net/xsc: add CQ and QP helpers
Author: netdev-bot+sashiko
Date: Fri, 27 Nov 2026 15:57
Date: Fri, 27 Nov 2026 15:57
338 lines
19756 bytes
19756 bytes
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
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads