net/ncsi: fix channel loop termination when Channel Count is 0
Subsystems (3)
PATCH 1/1 net/ncsi: fix channel loop termination when Channel Count is 0
ncsi_probe_channel() and ncsi_suspend_channel() (state ncsi_dev_state_suspend_gls) walk the channels of a package by incrementing ndp->channel_probe_id and stop only when it becomes equal to ndp->channel_count. channel_count is copied unchecked from the Channel Count field of the Get Capabilities response in ncsi_rsp_handler_gc().
If a Network Controller reports Channel Count 0, channel_probe_id is already 1 when it is first compared in the suspend_gls state, so the equality never holds. The driver then keeps sending Get Link Status to channel IDs 0, 1, 2, ... and never reaches Disable Channel Network Tx / Disable Channel, so the reconfiguration started by a single link-down AEN never completes and NC-SI does not recover when the link comes back. If the link flaps (down/up/down) inside that window, the driver additionally logs
Invalid state 0x3 on 0:0
NCSI: Handler for AEN type 0x0 returned -22
Wrong NCSI state 0x100 in workqueue
and channel monitoring stays stopped.
DSP0222 defines Channel Count as the number of channels supported by the controller, so a conforming controller reports at least 1; this is a robustness fix for non-conforming controllers, firmware bugs and emulators. One such emulator is libslirp's NC-SI responder (used by QEMU user networking for the Aspeed BMC machines), whose Get Capabilities response leaves Channel Count at 0.
The probe loop uses the same comparison. In the tests below it did not hang, because the Get Capabilities response was processed before the check that follows the GC command, so 0 == 0 held and probing moved on to Deselect Package. Whether it can hang depends on that ordering; no hang was observed in the probe path.
Use ">=" instead of "==" in both termination checks so that neither loop can run past the reported channel count. The suspend path change fixes the observed hang; the probe path change keeps the two loops consistent and removes the ordering dependency.
Channel Count is not validated in ncsi_rsp_handler_gc() instead, because rejecting or clamping a count of 0 would mean deciding what that value should mean for a non-conforming controller. Fixing the loop bounds handles any reported value where it is used and does not change behaviour for conforming controllers.
Tested on QEMU 11.1.3 romulus-bmc (aspeed_g5_defconfig, net tree at e203d8a4751d) with a minimal initramfs and a loopback NC-SI controller emulator (1 package, 1 channel) reporting Channel Count 0 or 1. Events:
(A) link-down AEN, link-up 15 s later; (B) down, up, down 0.5 s apart, link-up 15 s later. Result measured 180 s after the final link-up:
kernel count event after the event ch0 GLS/60 s unpatched 0 A GLS to ch 0,1,2,..., no DCNT/DC 1 unpatched 0 B GLS stops, monitor stopped 0 unpatched 1 A, B DCNT, DC, DP, ..., EC, AE 58 patched 0 A, B DCNT, DC, DP, ..., EC, AE 58 patched 1 A, B DCNT, DC, DP, ..., EC, AE 58
(GLS: Get Link Status, DCNT: Disable Channel Network Tx, DC: Disable Channel, DP: Deselect Package, EC: Enable Channel, AE: AEN Enable)
With the patch, Channel Count 0 behaves exactly like Channel Count 1 (same command counts); Channel Count 1 is unchanged.
An AI coding assistant (Claude Code) was used to analyze the problem, reproduce it in QEMU and draft this patch; I reviewed the change and the test results.
--- net/ncsi/ncsi-manage.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/ncsi/ncsi-manage.c +2 -2
@@ -520,7 +520,7 @@ static void ncsi_suspend_channel(struct ncsi_dev_priv *ndp) goto error; ndp->channel_probe_id++; -if (ndp->channel_probe_id == ndp->channel_count) { +if (ndp->channel_probe_id >= ndp->channel_count) { ndp->channel_probe_id =0 ; nd->state = ncsi_dev_state_suspend_dcnt; }
@@ -1491,7 +1491,7 @@ static void ncsi_probe_channel(struct ncsi_dev_priv *ndp) ndp->channel_probe_id++; } -if (ndp->channel_probe_id == ndp->channel_count) { +if (ndp->channel_probe_id >= ndp->channel_count) { ndp->channel_probe_id =0 ; nd->state = ncsi_dev_state_probe_dp; }
Signature · 1 line
2.56.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Critical: 0 · High: 0 (1) · Medium: 1 · Low: 0
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] The commit message says that "With the patch, Channel Count 0 behaves exactly like Channel Count 1" and that the probe-path change…
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the patch's own reasoning depends on it: the commit message says "Fixing the loop…
--
net/ncsi: fix channel loop termination when Channel Count is 0
This patch changes the termination checks of the channel walks in ncsi_suspend_channel() and ncsi_probe_channel() from "==" to ">=". The goal is that a controller reporting Channel Count 0 in its Get Capabilities response can no longer make the walks run past the reported count. The suspend_gls change fixes a hang seen after a link-down AEN.
Quoted 4 lines
net/ncsi/ncsi-manage.c +0
-0
[ ... ]
net/ncsi/ncsi-manage.c static void ncsi_probe_channel(struct ncsi_dev_priv *ndp) · line 1497
Quoted 9 lines
net/ncsi/ncsi-manage.c +1
-1@@ -1491,7 +1491,7 @@ static void ncsi_probe_channel(struct ncsi_dev_priv *ndp) ndp->channel_probe_id++; } -if (ndp->channel_probe_id == ndp->channel_count) { +if (ndp->channel_probe_id >= ndp->channel_count) { ndp->channel_probe_id =0 ; nd->state = ncsi_dev_state_probe_dp; }
[Severity: High]
This is a pre-existing issue, not one introduced by this patch. The commit message says:
Fixing the loop bounds handles any reported value where it is used
Does that hold for large counts? ncsi_rsp_handler_gc() still stores the 8-bit Channel Count with no upper bound:
net/ncsi/ncsi-rsp.c:ncsi_rsp_handler_gc() {
...
np->ndp->channel_count = rsp->channel_cnt;
...
}A controller can report a count from 33 to 255. Both this probe loop and the suspend_gls state in ncsi_suspend_channel() then send commands with nca.channel = channel_probe_id, up to channel 254.
ncsi_cmd_build_header() does not mask the channel:
#define NCSI_TO_CHANNEL(p, c) (((p) << NCSI_PACKAGE_SHIFT) | (c))
So channel 31 becomes the reserved package-level channel. On package 0, channel 32 goes out as 0x20, which is package 1, channel 0.
A channel that does not answer does not stop the walk. ncsi_request_timeout()->ncsi_free_request() schedules the work again and probing continues.
Suppose a second package, or an emulator that echoes the channel byte, answers the CIS sent to 0x20. Package 1 has not been added yet, because that only happens in ncsi_rsp_handler_sp(). So ncsi_find_package_and_channel() returns np == NULL and nc == NULL:
net/ncsi/ncsi-rsp.c:ncsi_rsp_handler_cis() {
...
ncsi_find_package_and_channel(ndp, rsp->rsp.common.channel, &np, &nc);
if (!nc) {
if (ndp->flags & NCSI_DEV_PROBED)
return -ENXIO;
id = NCSI_CHANNEL_INDEX(rsp->rsp.common.channel);
nc = ncsi_add_channel(np, id);
}
...
}NCSI_DEV_PROBED is still clear during probe. Can ncsi_add_channel() then oops on spin_lock_irqsave(&np->lock, flags) in the NET_RX softirq?
The old "==" check produced the same aliasing for these counts. Would it help to clamp channel_cnt in ncsi_rsp_handler_gc() to the range 1 to NCSI_MAX_CHANNEL - 1, and to add an np NULL check in ncsi_rsp_handler_cis()?
[Severity: Medium]
The commit message says:
With the patch, Channel Count 0 behaves exactly like Channel Count 1 (same command counts); Channel Count 1 is unchanged.
It also says the probe path change "removes the ordering dependency". Is that true for ncsi_probe_channel()?
This check runs after every CIS/GVI/GC/GLS step. It compares against ndp->channel_count, which is a single device-wide value. ncsi_register_dev() sets it to NCSI_RESERVED_CHANNEL once. After that, only ncsi_rsp_handler_gc() writes it, and it is never reset between packages.
Take a single package that reports 0. If the GC response is processed before the check that follows the GC command, as in the tests described above, the check sees 0 >= 0. The state then moves to probe_dp and GLS is never sent to channel 0.
If the response arrives later, GLS is sent. With a count of 1, GLS is always sent. So with a count of 0, the result still depends on response timing.
Now take multiple packages, where package N reports 0. The probe of package N+1 sends CIS to channel 0 and hits the check right away:
ncsi_dev_state_probe_cis:
send CIS to channel 0
nd->state = ncsi_dev_state_probe_gvi
channel_probe_id (0) >= channel_count (0)
nd->state = ncsi_dev_state_probe_dpGVI, GC and GLS are never sent to package N+1 channel 0. Its caps, version and link state stay at zero, and its other channels are not probed. The zero caps could also make ncsi_check_hwa() treat the device as not supporting HWA.
"==" behaves the same way in both of these cases, so this is no worse than before. For a count of 0 to really behave like a count of 1, would it need a clamp such as max(channel_cnt, 1), or a per-package bound?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261119100625.72505-1-me%407xz.dev