Thread View: linux.kernel.netdev
2 messages
2 total messages
Started by "Xin Tian"
Thu, 26 Nov 2026 14:57
[PATCH v2 09/11] net/xsc: report link state changes
Author: "Xin Tian"
Date: Thu, 26 Nov 2026 14:57
Date: Thu, 26 Nov 2026 14:57
206 lines
15306 bytes
15306 bytes
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
Re: [PATCH v2 09/11] net/xsc: report link state changes
Author: netdev-bot+sashiko
Date: Fri, 27 Nov 2026 15:58
Date: Fri, 27 Nov 2026 15:58
236 lines
16534 bytes
16534 bytes
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
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