nbl driver for Nebulamatrix NICs
illusion.wang
maintainerv34 cover nbl driver for Nebulamatrix NICs
This series continues and refines the previous v28 NBL NIC driver patch set for Nebula-matrix m18110/m18000 (SNIC s1000) Ethernet adapters.
The first two foundational patches of v28 have been merged into net-next main branch:
- net/nebula-matrix: add minimum nbl build framework (cffde9e49e86)
- net/nebula-matrix: add core driver architecture and HW layer initialization (825e6d230163)
Following maintainer feedback to shorten the patch series, this iteration drops the two merged commits, reorganizes and polishes the remaining functionality into a standalone 8-patch sequence. No new features are added; this series only completes the pending core infrastructure.
The overall driver development plan remains consistent with the v28 two-phase roadmap:
1) Complete hardware initialization, mailbox/channel communication and control-plane infrastructure (this series)
2) Implement netdev registration and TX/RX data-path functionality (follow-up series)
This patch set supplements the merged foundational code, covering channel layer abstraction, resource management, interrupt handling, chip-level initialization, control dispatch routing, channel RPC framework, and device lifecycle init/remove/start/stop logic.
changes v33->v34
Link to v33:https://lore.kernel.org/netdev/[email protected]/ AI review issues
changes v32->v33
Link to v32:https://lore.kernel.org/netdev/[email protected]/ AI review issues
changes v31->v32
Link to v31:https://lore.kernel.org/netdev/[email protected]/ AI review issues
changes v30->v31
Link to v30:https://lore.kernel.org/netdev/[email protected]/ 1.AI review issues
changes v29->v30
Link to v29:https://lore.kernel.org/netdev/[email protected]/ 1.AI review issues
changes v28->v29
Link to v28:https://lore.kernel.org/netdev/[email protected]/ 1.AI review issues
2.remove the hash table implementation
illusion wang (8):
net/nebula-matrix: add channel layer
net/nebula-matrix: add common resource implementation net/nebula-matrix: add intr resource implementation net/nebula-matrix: add chip-wide hardware init/deinit implementation net/nebula-matrix: dispatch: add control-level routing core
infrastructure
net/nebula-matrix: dispatch: add mailbox RPCs for msix map, mailbox
irq and vsi/eth id
net/nebula-matrix: add common/ctrl dev init/remove operation net/nebula-matrix: add common dev start/stop operation
.../net/ethernet/nebula-matrix/nbl/Makefile | 10 +- .../nbl/nbl_channel/nbl_channel.c | 1546 +++++++++++++++++ .../nbl/nbl_channel/nbl_channel.h | 196 +++ .../nebula-matrix/nbl/nbl_common/nbl_common.c | 51 + .../nebula-matrix/nbl/nbl_common/nbl_common.h | 14 + .../net/ethernet/nebula-matrix/nbl/nbl_core.h | 23 + .../nebula-matrix/nbl/nbl_core/nbl_dev.c | 743 ++++++++ .../nebula-matrix/nbl/nbl_core/nbl_dev.h | 55 + .../nebula-matrix/nbl/nbl_core/nbl_dispatch.c | 671 +++++++ .../nebula-matrix/nbl/nbl_core/nbl_dispatch.h | 23 + .../nebula-matrix/nbl/nbl_hw/nbl_chip.c | 32 + .../nebula-matrix/nbl/nbl_hw/nbl_chip.h | 12 + .../nbl/nbl_hw/nbl_hw_leonis/nbl_hw_leonis.c | 1309 +++++++++++++- .../nbl/nbl_hw/nbl_hw_leonis/nbl_hw_leonis.h | 342 ++++ .../nbl_hw_leonis/nbl_resource_leonis.c | 373 ++++ .../nbl_hw_leonis/nbl_resource_leonis.h | 12 + .../nebula-matrix/nbl/nbl_hw/nbl_hw_reg.h | 33 + .../nebula-matrix/nbl/nbl_hw/nbl_interrupt.c | 853 +++++++++ .../nebula-matrix/nbl/nbl_hw/nbl_interrupt.h | 21 + .../nebula-matrix/nbl/nbl_hw/nbl_resource.c | 157 ++ .../nebula-matrix/nbl/nbl_hw/nbl_resource.h | 159 ++ .../nbl/nbl_include/nbl_def_channel.h | 175 ++ .../nbl/nbl_include/nbl_def_common.h | 23 + .../nbl/nbl_include/nbl_def_dev.h | 16 + .../nbl/nbl_include/nbl_def_dispatch.h | 93 + .../nbl/nbl_include/nbl_def_hw.h | 81 + .../nbl/nbl_include/nbl_def_resource.h | 38 + .../nbl/nbl_include/nbl_include.h | 28 + .../net/ethernet/nebula-matrix/nbl/nbl_main.c | 262 ++- 29 files changed, 7328 insertions(+), 23 deletions(-) create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_channel/nbl_channel.c create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_channel/nbl_channel.h create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_common/nbl_common.c create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_common/nbl_common.h create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dev.c create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dev.h create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dispatch.c create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dispatch.h create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_chip.c create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_chip.h create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_resource_leonis.c create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_resource_leonis.h create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_interrupt.c create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_interrupt.h create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_resource.c create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_resource.h create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_include/nbl_def_channel.h create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_include/nbl_def_dev.h create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_include/nbl_def_dispatch.h create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_include/nbl_def_resource.h
Signature · 1 line
2.51.0.windows.1
illusion.wang
maintainerPATCH v34 1/8 net/nebula-matrix: add channel layer
From: illusion wang <[email protected]>
Add a mailbox-based channel layer that carries control messages between PF0 and the other PFs, providing the inter-PF control transport the rest of the driver builds on.
The channel ops table is defined here; queue setup, RX interrupt/polling processing and the upper-layer callers are wired up in later patches of this series.
Implementation notes:
- Message handlers live in an xarray, giving lockless lookup and duplicate-registration rejection (xa_insert returns -EBUSY). Both fire-and-forget and synchronous ACK-based sends are supported.
- ACK wait-slot exhaustion returns -EAGAIN; a full TX descriptor ring returns -EBUSY. Payloads are inline (up to 16 B) or DMA-based (up to 4032 B).
- The per-slot data buffers of each ring are carved out of one contiguous coherent DMA block, so those buffers cost one devres node and one IOMMU mapping per ring instead of one per slot. That is the only part of the ring which is collapsed: the descriptor ring is a second coherent allocation of its own, and the buf[]/wait[] bookkeeping arrays are devm_kcalloc() nodes.
- Mailbox queue resources are devm-managed and the DMA is allocated once during setup. There is no free path, so a channel is set up exactly once per device lifetime and teardown is terminal for it; a re-setup is rejected with -EBUSY.
- RX is driven either from the interrupt or by polling, with cleanup offloaded to a workqueue. Teardown quiesces both queues before the rings can be freed.
- Register access goes through hw_ops. The reg_lock spinlock covers BAR0 register access only; BAR2 mailbox registers and the doorbell write do not take it.
This patch also adds nbl_probe_chip_deps() and calls it from nbl_probe() before nbl_core_init(). For every PF that is not the physical function 0 (has_ctrl == 0) it resolves function 0 in the same slot and:
- fails the probe with -ENODEV if function 0 is absent, and also when the lookup resolves to this device itself: a VF is never the management PF, yet under ARI its devfn decodes to function 0 of a slot;
- returns -EPROBE_DEFER unless function 0 is bound to *this* driver with its probe completed. Testing links.status == DL_DEV_DRIVER_BOUND alone would also accept function 0 on vfio-pci or pci-stub through driver_override, where the chip-global setup this PF depends on never ran, so the bound driver is compared as well;
- creates a persistent DL_FLAG_AUTOPROBE_CONSUMER device link, so the driver core unbinds every sibling PF before function 0 goes away and re-probes them when function 0 binds again.
Function 0's device lock is never taken: this probe already runs under device_lock(funcN) while __pci_bus_lock() (AER recovery, slot reset) walks the bus in devfn order, 0 -> N, and dev->mutex is lockdep_set_novalidate_class(), so lockdep could not report the resulting N -> 0 inversion. Neither state read needs the lock: a missed transition only costs a deferral, and that deferral is self-healing. driver_bound() marks function 0 bound - klist_add_tail(), which is what device_is_bound() reads - before it calls driver_deferred_probe_trigger(), so the retried probe already sees it bound.
function 0 can still unbind between that check and device_link_add(), which would leave the link DORMANT, so the probe re-checks both conditions after creating the link, deletes it and defers if they no longer hold.
Nothing here consumes the ordering yet - setup_queue() and cfg_chan_qinfo_map_table() still have no callers - but the later init/remove and start/stop patches do. Note the cost: with function 0 bound to another driver (e.g. vfio-pci) a sibling PF stays deferred for as long as that holds, with only a dev_dbg() record, and a function 0 that is simply absent fails the probe outright.
--- .../net/ethernet/nebula-matrix/nbl/Makefile | 4 +- .../nbl/nbl_channel/nbl_channel.c | 1546 +++++++++++++++++ .../nbl/nbl_channel/nbl_channel.h | 196 +++ .../nebula-matrix/nbl/nbl_common/nbl_common.c | 29 + .../nebula-matrix/nbl/nbl_common/nbl_common.h | 14 + .../net/ethernet/nebula-matrix/nbl/nbl_core.h | 7 + .../nbl/nbl_hw/nbl_hw_leonis/nbl_hw_leonis.c | 299 +++- .../nbl/nbl_hw/nbl_hw_leonis/nbl_hw_leonis.h | 41 + .../nebula-matrix/nbl/nbl_hw/nbl_hw_reg.h | 32 + .../nbl/nbl_include/nbl_def_channel.h | 125 ++ .../nbl/nbl_include/nbl_def_common.h | 3 + .../nbl/nbl_include/nbl_def_hw.h | 43 + .../nbl/nbl_include/nbl_include.h | 3 + .../net/ethernet/nebula-matrix/nbl/nbl_main.c | 155 +- 14 files changed, 2479 insertions(+), 18 deletions(-) create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_channel/nbl_channel.c create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_channel/nbl_channel.h create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_common/nbl_common.c create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_common/nbl_common.h create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_include/nbl_def_channel.h
drivers/net/ethernet/nebula-matrix/nbl/Makefile +3 -1
@@ -3,5 +3,7 @@ obj-$(CONFIG_NBL) := nbl.o -nbl-objs += nbl_hw/nbl_hw_leonis/nbl_hw_leonis.o \ +nbl-objs += nbl_common/nbl_common.o \ + nbl_channel/nbl_channel.o \ + nbl_hw/nbl_hw_leonis/nbl_hw_leonis.o \ nbl_main.o
drivers/net/ethernet/nebula-matrix/nbl/nbl_channel/nbl_channel.c new file +1546 -0
1,546 lines not on this page · raw message
drivers/net/ethernet/nebula-matrix/nbl/nbl_channel/nbl_channel.h new file +196 -0
196 lines not on this page · raw message
drivers/net/ethernet/nebula-matrix/nbl/nbl_common/nbl_common.c new file +29 -0
@@ -0,0 +1,29 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (c) 2026 Nebula Matrix Limited. +*/ + +#include <linux/device.h> +#include "nbl_common.h" + +void nbl_common_destroy_wq (struct nbl_common_info *common) +{ +if (!common || !common->wq) +return ; + +destroy_workqueue (common->wq); + common->wq =NULL ; +} + +int nbl_common_create_wq (struct nbl_common_info *common) +{ + common->wq =alloc_workqueue ("nbl_wq_%s" ,WQ_UNBOUND ,0 , +pci_name (common->pdev)); +if (!common->wq) { +dev_err (common->dev,"Failed to alloc workqueue for %s\n" , +pci_name (common->pdev)); +return -ENOMEM ; + } + +return 0 ; +}
drivers/net/ethernet/nebula-matrix/nbl/nbl_common/nbl_common.h new file +14 -0
@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (c) 2026 Nebula Matrix Limited. +*/ + +#ifndef _NBL_COMMON_H_ +#define _NBL_COMMON_H_ + +#include <linux/types.h> + +#include "../nbl_include/nbl_include.h" +#include "../nbl_include/nbl_def_common.h" + +#endif
drivers/net/ethernet/nebula-matrix/nbl/nbl_core.h +7 -0
@@ -14,13 +14,20 @@ enum { NBL_CAP_HAS_NET_BIT, }; +struct nbl_interface { +struct nbl_hw_ops_tbl *hw_ops_tbl; +struct nbl_channel_ops_tbl *channel_ops_tbl; +}; +struct nbl_core {struct nbl_hw_mgt *hw_mgt; +struct nbl_channel_mgt *chan_mgt; };struct nbl_adapter {struct pci_dev *pdev;struct nbl_core core; +struct nbl_interface intf;struct nbl_common_info common; };
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_hw_leonis.c +287 -12
379 lines not on this page · raw message
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_hw_leonis.h +41 -0
46 lines not on this page · raw message
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_reg.h +32 -0
@@ -8,6 +8,7 @@#include <linux/types.h> +#include "../nbl_include/nbl_def_channel.h" #include "../nbl_include/nbl_def_hw.h" #include "../nbl_include/nbl_def_common.h" #include "../nbl_core.h"
@@ -26,6 +27,37 @@ struct nbl_hw_mgt {u8 __iomem *hw_addr;u8 __iomem *mailbox_bar_hw_addr;resource_size_t mailbox_bar_size; +spinlock_t reg_lock;/* Protect reg access */ }; +static inline u32 rd32 (u8 __iomem *addr,u64 reg) +{ +return readl (addr + reg); +} + +static inline void wr32 (u8 __iomem *addr,u64 reg,u32 value) +{ +writel (value, addr + reg); +} + +static inline void nbl_hw_wr32 (struct nbl_hw_mgt *hw_mgt,u64 reg,u32 value) +{ +wr32 (hw_mgt->hw_addr, reg, value); +} + +static inline u32 nbl_hw_rd32 (struct nbl_hw_mgt *hw_mgt,u64 reg) +{ +return rd32 (hw_mgt->hw_addr, reg); +} + +static inline void nbl_mbx_wr32 (struct nbl_hw_mgt *hw_mgt,u64 reg,u32 value) +{ +writel (value, hw_mgt->mailbox_bar_hw_addr + reg); +} + +static inline u32 nbl_mbx_rd32 (struct nbl_hw_mgt *hw_mgt,u64 reg) +{ +return readl (hw_mgt->mailbox_bar_hw_addr + reg); +} +#endif
drivers/net/ethernet/nebula-matrix/nbl/nbl_include/nbl_def_channel.h new file +125 -0
125 lines not on this page · raw message
drivers/net/ethernet/nebula-matrix/nbl/nbl_include/nbl_def_common.h +3 -0
@@ -12,6 +12,7 @@#include "nbl_include.h" struct nbl_common_info { +struct workqueue_struct *wq;struct pci_dev *pdev;struct device *dev;u16 vsi_id;
@@ -28,4 +29,6 @@ struct nbl_common_info { u8 has_net; }; +void nbl_common_destroy_wq (struct nbl_common_info *common); +int nbl_common_create_wq (struct nbl_common_info *common);#endif
drivers/net/ethernet/nebula-matrix/nbl/nbl_include/nbl_def_hw.h +43 -0
49 lines not on this page · raw message
drivers/net/ethernet/nebula-matrix/nbl/nbl_include/nbl_include.h +3 -0
@@ -10,6 +10,9 @@/* ------ Basic definitions ------- */ #define NBL_DRIVER_NAME "nbl" +#define NBL_MAX_PF 8 +#define NBL_NEXT_ID (id, max) (((id) + 1) % ((max) + 1)) +struct nbl_func_caps {u32 has_ctrl:1 ;u32 has_net:1 ;
drivers/net/ethernet/nebula-matrix/nbl/nbl_main.c +150 -5
204 lines not on this page · raw message
Signature · 1 line
2.51.0.windows.1
Re: [PATCH v34 net-next 1/8] net/nebula-matrix: add channel layer
Quoted 18 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_main.c +2
-1@@ -75,27 +215,32 @@ static int nbl_probe(struct pci_dev *pdev,struct nbl_adapter *adapter;int err; +nbl_get_func_param (pdev, id->driver_data, ¶m); err =pcim_enable_device (pdev);if (err) {dev_err (&pdev->dev,"Failed to enable PCI dev, err=%d\n" , err);return err; } -nbl_get_func_param (pdev, id->driver_data, ¶m);/* never return fail when DMA_BIT_MASK(64) */ dma_set_mask_and_coherent (dev,DMA_BIT_MASK (64 ));pci_set_master (pdev); + err =nbl_probe_chip_deps (pdev, param.caps.has_ctrl);
drivers/net/ethernet/nebula-matrix/nbl/nbl_main.c static int nbl_probe(struct pci_dev *pdev, · line 239
Quoted 10 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_main.c +4
-1+if (err) + goto out_clear_master; + adapter =nbl_core_init (pdev, ¶m);if (IS_ERR (adapter)) {dev_err (dev,"Nbl adapter init fail: %pe\n" , adapter); err =PTR_ERR (adapter); - goto adapter_init_err; + goto out_clear_master; }
nbl_get_func_param() now runs before pcim_enable_device(), but nbl_probe_chip_deps() still runs after pcim_enable_device() and pci_set_master().
Shouldn't nbl_probe_chip_deps() be on called earlier?
[email protected]
AI reviewRe: [PATCH v34 net-next 1/8] net/nebula-matrix: add channel layer
In reply to illusion.wangThank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider.
Critical: 0 · High: 1 · Medium: 4 · 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] The re-check in nbl_probe_chip_deps() is meant to catch func0 unbinding concurrently, and it misses the whole period when func0's…
- [Medium] The rollback in nbl_probe_chip_deps() cannot work.
- [Medium] The managed DL_FLAG_AUTOPROBE_CONSUMER link from funcN to func0 is never removed when the nbl binding that created it ends: not…
- [Medium] On a TX completion timeout, nbl_chan_quiesce_and_reclaim_tx() calls hw_ops->stop_mailbox_txq().
- [Medium] net/nebula-matrix: RX queue stopped before cancel_work_sync() in nbl_chan_teardown_queue()
- [Low] The comment in nbl_chan_advance_rx_ring() says recycling the initially unused slot lets "the ring to become fully populated".
--
net/nebula-matrix: add channel layer
This adds a mailbox-based channel layer for control messages between PFs. It has TX/RX descriptor rings, ACK wait slots and an xarray of message handlers. It also adds nbl_probe_chip_deps(), which defers a non-function-0 PF until function 0 is bound to nbl, and then creates a DL_FLAG_AUTOPROBE_CONSUMER device link to function 0.
Quoted 5 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_channel/nbl_channel.c new file +0
-0
[ ... ]
Quoted 14 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_channel/nbl_channel.c new file +14
-0+static void nbl_chan_quiesce_and_reclaim_tx (struct nbl_channel_mgt *chan_mgt, +struct nbl_chan_info *chan_info) +{ +struct nbl_hw_ops *hw_ops = chan_mgt->hw_ops_tbl->ops; +struct nbl_hw_mgt *hw_priv = chan_mgt->hw_ops_tbl->priv; +struct nbl_chan_ring *txq = &chan_info->txq; +struct nbl_chan_tx_desc *tx_desc; + +/* +* Assert QUEUE_RST to stop hardware fetching new descriptors. +* stop_mailbox_txq() flushes through the mailbox BAR itself, so +* the reset has reached the device when it returns. +*/ + hw_ops->stop_mailbox_txq (hw_priv);
[ ... ]
Quoted 3 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_channel/nbl_channel.c new file +3
-0+ txq->tail_ptr =0 ; + txq->next_to_use =0 ; + txq->next_to_clean =0 ;
[ ... ]
drivers/net/ethernet/nebula-matrix/nbl/nbl_channel/nbl_channel.c line 655
Quoted 6 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_channel/nbl_channel.c new file +6
-0+if (READ_ONCE (chan_info->shutdn)) +return ; + +/* Re-enable queue with current ring base and size */ +nbl_chan_config_queue (chan_mgt, chan_info, true); +}
[Severity: Medium]
Does anything make sure the device's TX engine is idle before the queue is re-enabled here?
nbl_hw_stop_mailbox_txq() writes QUEUE_RST, clears data[0..2], and reads back one QINFO dword through nbl_hw_flush_mbx_write(). That read-back shows the posted MMIO writes reached the device. It does not cover device DMA already in flight, such as a late USED write-back for the descriptor that timed out. The DIF_ERR/PTR_ERR bits defined for the QINFO entry are never read either.
After the reset, the next send always uses slot 0. If the descriptor that timed out was also slot 0, could a late USED write-back from the old run land after the new descriptor's AVAIL write? If so, nbl_chan_kick_tx_ring() would report success for a message the device never sent, or the peer could get the old message late.
This depends on how the hardware handles QUEUE_RST, so it may not happen in practice, but there is no idle or reset-done wait before nbl_chan_config_queue() re-arms the queue. The code is the same at the end of the series.
[ ... ]
Quoted 4 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_channel/nbl_channel.c new file +4
-0+static void nbl_chan_advance_rx_ring (struct nbl_channel_mgt *chan_mgt, +struct nbl_chan_info *chan_info, +struct nbl_chan_ring *rxq) +{
[ ... ]
drivers/net/ethernet/nebula-matrix/nbl/nbl_channel/nbl_channel.c line 914
Quoted 6 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_channel/nbl_channel.c new file +6
-0+/* +* Recycle the RX descriptor at next_to_use. The initial +* unused slot is intentionally recycled after the first +* RX descriptor is consumed, allowing the ring to become +* fully populated while next_to_clean tracks the consumer. +*/
[Severity: Low]
This isn't a bug, but is this comment accurate?
nbl_chan_init_rx_queue() posts descriptors 0..N-2 and sets next_to_use = N-1 with next_to_clean = 0. Each consumed descriptor reposts exactly one at next_to_use, so next_to_use stays one slot behind next_to_clean. At most N-1 descriptors are ever AVAIL.
That matches the comment in nbl_chan_init_rx_queue() about keeping one slot unused, but not "allowing the ring to become fully populated" here.
[ ... ]
Quoted 4 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_main.c +0
-0
[ ... ]
Quoted 2 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_main.c +2
-0+static int nbl_probe_chip_deps (struct pci_dev *pdev,bool has_ctrl) +{
[ ... ]
drivers/net/ethernet/nebula-matrix/nbl/nbl_main.c static void nbl_get_func_param(struct pci_dev *pdev, kernel_ulong_t driver_data, · line 188
Quoted 8 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_main.c +8
-0+ link =device_link_add (&pdev->dev, &mgt->dev, +DL_FLAG_AUTOPROBE_CONSUMER ); +if (!link) { +dev_err (&pdev->dev, +"failed to create device link to management PF\n" ); +pci_dev_put (mgt); +return -ENOMEM ; + }
[Severity: Medium]
Is this managed link meant to outlive the nbl binding that created it?
Nothing removes it in nbl_remove(), on module unload, or when nbl_core_init() fails after nbl_probe_chip_deps() has succeeded. Since DL_FLAG_AUTOREMOVE_CONSUMER is not set, __device_links_no_driver() keeps the link, so it applies to whatever driver binds funcN next.
For example, after rmmod nbl, func0's cleanup leaves the link DORMANT. Binding funcN to vfio-pci or pci-stub is then deferred by device_links_check_suppliers() (status != DL_STATE_AVAILABLE) until func0 is bound to some driver.
Unbinding func0 from any driver would also force-unbind whatever driver funcN has, through device_links_unbind_consumers().
The commit message calls the link persistent. Is this effect on other drivers bound to the sibling functions intended?
drivers/net/ethernet/nebula-matrix/nbl/nbl_main.c static void nbl_get_func_param(struct pci_dev *pdev, kernel_ulong_t driver_data, · line 197
Quoted 9 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_main.c +9
-0+ +/* +* func 0 can start unbinding between the check above and +* device_link_add(), which leaves the link DORMANT and not what this +* probe should succeed with. Drop it and defer instead: the next +* bind of func 0 re-arms deferred probing. +*/ +if (READ_ONCE (mgt->dev.driver) != pdev->dev.driver || + !device_is_bound (&mgt->dev)) {
[Severity: High]
Can this re-check miss func0 unbinding while its remove callback is running?
In __device_release_driver(), device_links_busy() first sets func0's links.status to DL_DEV_UNBINDING. Then device_remove() runs func0's nbl_remove(). Only after that does device_unbind_cleanup() clear dev->driver and klist_remove(&dev->p->knode_driver) clear what device_is_bound() reads. So both conditions here stay true for all of func0's remove.
Consider a sibling probing for the first time in that window. It has no link yet, so device_links_busy() did not wait for it. It passes the first check, and device_link_init_status() starts the link in DL_STATE_SUPPLIER_UNBIND:
case DL_DEV_UNBINDING:
link->status = DL_STATE_SUPPLIER_UNBIND;This re-check also passes, so the sibling continues into nbl_core_init() while func0's chip-global state is being torn down.
Later, device_links_driver_bound() would hit:
WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
func0's cleanup then moves the link to DORMANT. When func0 rebinds, the link becomes AVAILABLE, not ACTIVE. A later func0 unbind would then find no busy consumer and run chip-global deinit while the sibling is still bound.
The commit message says:
function 0 can still unbind between that check and device_link_add(), which would leave the link DORMANT, so the probe re-checks both conditions after creating the link, deletes it and defers if they no longer hold.
Would checking the state of the returned link, for example READ_ONCE(link->status) == DL_STATE_CONSUMER_PROBE, be more reliable than the driver pointer and bound state?
The code is the same at the end of the series. Also, a comment added to nbl_dev.c later in the series says this function holds func0's device lock across the check and device_link_add(), and it does not.
drivers/net/ethernet/nebula-matrix/nbl/nbl_main.c static void nbl_get_func_param(struct pci_dev *pdev, kernel_ulong_t driver_data, · line 198
Quoted 1 line
drivers/net/ethernet/nebula-matrix/nbl/nbl_main.c +1
-0+device_link_del (link);
[Severity: Medium]
Can device_link_del() delete this link?
DL_FLAG_STATELESS is not set, so this is a managed link. device_link_del() ends up in device_link_put_kref():
if (link->flags & DL_FLAG_STATELESS)
kref_put(&link->kref, __device_link_del);
else if (!device_is_registered(link->consumer))
__device_link_del(&link->kref);
else
WARN(1, "Unable to drop a managed device link reference\n");The consumer is the PF that is currently in ->probe(), so it is registered. On this path the WARN always fires (with panic_on_warn the machine panics) and the link is left in place.
That makes "deletes it and defers" in the commit message and "Drop it and defer instead" in the comment above inaccurate.
Could the device_link_del() call just be dropped? On -EPROBE_DEFER the driver core already handles the managed link through device_links_no_driver(), and DL_FLAG_AUTOPROBE_CONSUMER re-queues the probe. The code is the same at the end of the series.
Quoted 5 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_main.c +5
-0+dev_dbg (&pdev->dev, +"management PF went away, deferring probe\n" ); +pci_dev_put (mgt); +return -EPROBE_DEFER ; + }
[ ... ]
Quoted 1,537 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_main.c +1536
-0@@ -0,0 +1,1546 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (c) 2026 Nebula Matrix Limited. +*/ +#include <linux/delay.h> +#include <linux/device.h> +#include <linux/mutex.h> +#include <linux/bitfield.h> +#include <linux/pci.h> +#include <linux/bits.h> +#include <linux/dma-mapping.h> +#include <linux/atomic.h> +#include <linux/wait.h> +#include "nbl_channel.h" + +static int nbl_chan_add_msg_handler (struct nbl_channel_mgt *chan_mgt, +u16 msg_type,nbl_chan_resp func, +void *priv) +{ +struct nbl_chan_msg_node_data *handler; +int ret; + + handler =kzalloc_obj (*handler,GFP_KERNEL ); +if (!handler) +return -ENOMEM ; + + handler->func = func; + handler->priv = priv; + +/* Each msg_type is registered at most once; reject duplicates. */ +mutex_lock (&chan_mgt->handler_lock); + ret =xa_insert (&chan_mgt->handler_xa, msg_type, handler,GFP_KERNEL ); +mutex_unlock (&chan_mgt->handler_lock); +if (ret) +kfree (handler); + +return ret; +} + +static int nbl_chan_init_msg_handler (struct nbl_channel_mgt *chan_mgt) +{ +int ret; + + ret =devm_mutex_init (chan_mgt->common->dev, &chan_mgt->handler_lock); +if (ret) +return ret; + +xa_init (&chan_mgt->handler_xa); + +return 0 ; +} + +static void nbl_chan_remove_msg_handler (struct nbl_channel_mgt *chan_mgt) +{ +struct nbl_chan_msg_node_data *handler; +unsigned long msg_type; + +mutex_lock (&chan_mgt->handler_lock); +xa_for_each (&chan_mgt->handler_xa, msg_type, handler) { +xa_erase (&chan_mgt->handler_xa, msg_type); +kfree (handler); + } +mutex_unlock (&chan_mgt->handler_lock); +xa_destroy (&chan_mgt->handler_xa); +} + +static void nbl_chan_init_queue_param (struct nbl_chan_info *chan_info, +u16 num_txq_entries,u16 num_rxq_entries, +u16 txq_buf_size,u16 rxq_buf_size) +{ + chan_info->num_txq_entries = num_txq_entries; + chan_info->num_rxq_entries = num_rxq_entries; + chan_info->txq_buf_size = txq_buf_size; + chan_info->rxq_buf_size = rxq_buf_size; +atomic_set (&chan_info->inflight_tx_cnt,0 ); +WRITE_ONCE (chan_info->shutdn, false); +WRITE_ONCE (chan_info->active, false); +WRITE_ONCE (chan_info->wait_head_index,0 ); +memset (chan_info->state,0 ,sizeof (chan_info->state)); +init_waitqueue_head (&chan_info->inflight_wait); +} + +static int nbl_chan_init_tx_queue (struct nbl_common_info *common, +struct nbl_chan_info *chan_info) +{ +struct nbl_chan_ring *txq = &chan_info->txq; +struct device *dev = common->dev; +dma_addr_t base_dma; +size_t size = + chan_info->num_txq_entries *sizeof (struct nbl_chan_tx_desc ); +void *base; +u16 i; + + txq->desc.tx_desc =dmam_alloc_coherent (dev, size, &txq->dma, +GFP_KERNEL ); +if (!txq->desc.tx_desc) +return -ENOMEM ; + +/* +* The per-entry *data buffers* all come from one contiguous coherent +* block, so those buffers cost exactly one coherent allocation, one +* devres node and one IOMMU mapping for the whole ring whatever the +* page size is. Per-entry dmam_alloc_coherent() instead rounds every +* NBL_CHAN_BUF_LEN buffer up to a whole page (8 MiB per PF on 16 KiB +* pages, 32 MiB on 64 KiB pages), and a dma_pool only fixes the +* large-page case: with NBL_CHAN_BUF_LEN equal to PAGE_SIZE it still +* degenerates to one allocation per buffer on 4 KiB pages. +* NBL_CHAN_BUF_LEN is a power of two, so the block always divides +* evenly into per-entry slots. +* +* That is the only part of the ring that is collapsed: the descriptor +* ring above is its own dmam_alloc_coherent(), and txq->buf[] and +* chan_info->wait[] below are separate devm_kcalloc() nodes, so a +* ring is two coherent mappings plus those arrays, not one. +*/ + base =dmam_alloc_coherent (dev, + chan_info->num_txq_entries * + chan_info->txq_buf_size, + &base_dma,GFP_KERNEL ); +if (!base) +return -ENOMEM ; + + chan_info->wait =devm_kcalloc (dev, chan_info->num_txq_entries, +sizeof (*chan_info->wait),GFP_KERNEL ); +if (!chan_info->wait) +return -ENOMEM ; +for (i =0 ; i < chan_info->num_txq_entries; i++) { +init_waitqueue_head (&chan_info->wait[i].wait_queue); +WRITE_ONCE (chan_info->wait[i].status,NBL_MBX_STATUS_IDLE ); +WRITE_ONCE (chan_info->wait[i].acked,0 ); +WRITE_ONCE (chan_info->wait[i].ack_data,NULL ); +WRITE_ONCE (chan_info->wait[i].ack_data_len,0 ); +WRITE_ONCE (chan_info->wait[i].ack_err,0 ); +WRITE_ONCE (chan_info->wait[i].msg_type,0 ); +WRITE_ONCE (chan_info->wait[i].msg_index,0 ); +WRITE_ONCE (chan_info->wait[i].dstid,0 ); + } + + txq->buf =devm_kcalloc (dev, chan_info->num_txq_entries, +sizeof (*txq->buf),GFP_KERNEL ); +if (!txq->buf) +return -ENOMEM ; + +/* Carve the contiguous block into fixed-size per-entry slots */ +for (i =0 ; i < chan_info->num_txq_entries; i++) { + txq->buf[i].va = (u8 *)base + + (size_t )i * chan_info->txq_buf_size; + txq->buf[i].pa = base_dma + + (dma_addr_t )i * chan_info->txq_buf_size; + txq->buf[i].size = chan_info->txq_buf_size; + } + + txq->next_to_clean =0 ; + txq->next_to_use =0 ; + txq->tail_ptr =0 ; + +return 0 ; +} + +static int nbl_chan_init_rx_queue (struct nbl_common_info *common, +struct nbl_chan_info *chan_info) +{ +struct nbl_chan_ring *rxq = &chan_info->rxq; +struct device *dev = common->dev; +struct nbl_chan_rx_desc *desc; +dma_addr_t base_dma; +size_t size = + chan_info->num_rxq_entries *sizeof (struct nbl_chan_rx_desc ); +void *base; +u16 i; + + rxq->desc.rx_desc =dmam_alloc_coherent (dev, size, &rxq->dma, +GFP_KERNEL ); +if (!rxq->desc.rx_desc) { +dev_err_ratelimited (dev, +"Allocate DMA for chan rx descriptor ring failed\n" ); +return -ENOMEM ; + } + +/* See nbl_chan_init_tx_queue() for why one contiguous block is used */ + base =dmam_alloc_coherent (dev, + chan_info->num_rxq_entries * + chan_info->rxq_buf_size, + &base_dma,GFP_KERNEL ); +if (!base) +return -ENOMEM ; + + rxq->buf =devm_kcalloc (dev, chan_info->num_rxq_entries, +sizeof (*rxq->buf),GFP_KERNEL ); +if (!rxq->buf) +return -ENOMEM ; + +/* Carve the contiguous block into fixed-size per-entry slots */ +for (i =0 ; i < chan_info->num_rxq_entries; i++) { + rxq->buf[i].va = (u8 *)base + + (size_t )i * chan_info->rxq_buf_size; + rxq->buf[i].pa = base_dma + + (dma_addr_t )i * chan_info->rxq_buf_size; + rxq->buf[i].size = chan_info->rxq_buf_size; + } + + desc = rxq->desc.rx_desc; +/* +* Initially leave one RX descriptor unused so that +* next_to_clean and next_to_use can distinguish an empty +* ring from a full ring. +* +* The unused slot is replenished as RX descriptors are +* consumed and recycled. +*/ +for (i =0 ; i < chan_info->num_rxq_entries -1 ; i++) { + desc[i].buf_addr =cpu_to_le64 (rxq->buf[i].pa); + desc[i].buf_len =cpu_to_le32 (chan_info->rxq_buf_size); + desc[i].flags =cpu_to_le16 (BIT (NBL_CHAN_RX_DESC_AVAIL )); + } + + rxq->next_to_clean =0 ; + rxq->next_to_use = chan_info->num_rxq_entries -1 ; + rxq->tail_ptr = chan_info->num_rxq_entries -1 ; + +return 0 ; +} + +static int nbl_chan_init_queue (struct nbl_common_info *common, +struct nbl_chan_info *chan_info) +{ +int err; + + err =nbl_chan_init_tx_queue (common, chan_info); +if (err) +return err; + + err =nbl_chan_init_rx_queue (common, chan_info); + +return err; +} + +static void nbl_chan_config_queue (struct nbl_channel_mgt *chan_mgt, +struct nbl_chan_info *chan_info,bool tx) +{ +struct nbl_hw_ops *hw_ops = chan_mgt->hw_ops_tbl->ops; +struct nbl_hw_mgt *p = chan_mgt->hw_ops_tbl->priv; +struct nbl_chan_ring *ring; +dma_addr_t addr; +int size_bwid; + +if (tx) + ring = &chan_info->txq; +else + ring = &chan_info->rxq; + addr = ring->dma; +if (tx) { + size_bwid =ilog2 (chan_info->num_txq_entries); + hw_ops->config_mailbox_txq (p, addr, size_bwid); + }else { + size_bwid =ilog2 (chan_info->num_rxq_entries); + hw_ops->config_mailbox_rxq (p, addr, size_bwid); + } +} + +static void nbl_chan_cfg_qinfo_map_table (struct nbl_channel_mgt *chan_mgt, +u8 bus,u8 devid) +{ +struct nbl_hw_ops *hw_ops = chan_mgt->hw_ops_tbl->ops; +struct nbl_hw_mgt *p = chan_mgt->hw_ops_tbl->priv; +u32 pf_mask =0 ; +u8 func_id; + +/* +* k_pf_mask rule: bit N == 0 means PF#N enabled, bit N == 1 masked out. +* Program mailbox QINFO entry for each hardware-active PF func_id. +* +* Note: This loop iterates over the raw hardware PF func_id, so it +* assumes the mask describes a contiguous set of PFs starting at PF0 +* (0xfe/0xfc/0xf0). nbl_res_init_pf_num() enforces that before +* nbl_dev_setup_chan_qinfo() reaches this op, so no second check is +* done here. +*/ + hw_ops->get_host_pf_mask (p, &pf_mask); +for (func_id =0 ; func_id <NBL_MAX_PF ; func_id++) { +if (!(pf_mask & (1 << func_id))) + hw_ops->cfg_mailbox_qinfo (p, func_id, bus, + devid, func_id); + } +} + +/* +* The RX QINFO region is written only by config/stop paths, never by +* senders (which only touch the TX QINFO region and the doorbell), so +* stopping RX does not require txq_lock. +*/ +static void nbl_chan_stop_rx_queue (struct nbl_channel_mgt *chan_mgt) +{ +struct nbl_hw_ops *hw_ops = chan_mgt->hw_ops_tbl->ops; + + hw_ops->stop_mailbox_rxq (chan_mgt->hw_ops_tbl->priv); +} + +/* +* The TX QINFO region is also written by nbl_chan_quiesce_and_reclaim_tx() +* under txq_lock, so TX stop must serialize against it via txq_lock. +*/ +static void nbl_chan_stop_tx_queue (struct nbl_channel_mgt *chan_mgt) +{ +struct nbl_hw_ops *hw_ops = chan_mgt->hw_ops_tbl->ops; + + hw_ops->stop_mailbox_txq (chan_mgt->hw_ops_tbl->priv); +} + +static void nbl_chan_reset_wait_head (struct nbl_chan_info *chan_info, +struct nbl_chan_waitqueue_head *wait_head) +{ +lockdep_assert_held (&chan_info->pending_lock); + +WRITE_ONCE (wait_head->acked,0 ); +WRITE_ONCE (wait_head->status,NBL_MBX_STATUS_IDLE ); +WRITE_ONCE (wait_head->ack_data,NULL ); +WRITE_ONCE (wait_head->ack_data_len,0 ); +WRITE_ONCE (wait_head->ack_err,0 ); +WRITE_ONCE (wait_head->msg_type,0 ); +WRITE_ONCE (wait_head->dstid,0 ); +} + +static int nbl_chan_teardown_queue (struct nbl_channel_mgt *chan_mgt, +u8 chan_type) +{ +struct nbl_chan_info *chan_info = chan_mgt->chan_info[chan_type]; +struct nbl_chan_waitqueue_head *wait_head; +struct work_struct *task; +u16 i; + +if (!READ_ONCE (chan_info->active)) { +dev_warn (chan_mgt->common->dev,"channel not active, skip duplicate teardown\n" ); +return 0 ; + } +/* +* Step1: +* block new sender +*/ +mutex_lock (&chan_info->state_lock); +WRITE_ONCE (chan_info->shutdn, true); + task =READ_ONCE (chan_info->clean_task); +WRITE_ONCE (chan_info->clean_task,NULL ); +mutex_unlock (&chan_info->state_lock); +/* +* Step2: +* abort pending ACK waiters +* +* Poke only slots with a live waiter (status == WAITING): publish a +* synthetic (-EIO, len 0) completion and wake the owner. The slot is +* deliberately NOT reset to IDLE here - ownership stays with the +* sender, which consumes the completion and resets its own slot in +* nbl_chan_send_msg():out_clear_wait_slot under pending_lock. +* +* Resetting an owned slot here would corrupt two things: +* - an ACKD slot whose sender is consuming the completion +* locklessly (smp_rmb()-ordered reads) could observe torn +* ack_data_len/ack_err pairs; +* - nbl_chan_get_msg_id() hands out IDLE/TIMEOUT slots, so a slot +* reset to IDLE under its live owner could be reallocated to a +* sender admitted before shutdn was set. +* +* IDLE/TIMEOUT slots have no waiter and need no action. A late ACK +* racing this poke is serialized by pending_lock: either the real +* completion lands first (status becomes ACKD and is skipped) or +* this synthetic failure does, both are valid outcomes. +*/ +mutex_lock (&chan_info->pending_lock); +for (i =0 ; i < chan_info->num_txq_entries; i++) { + wait_head = &chan_info->wait[i]; +if (READ_ONCE (wait_head->status) !=NBL_MBX_STATUS_WAITING ) +continue ; +WRITE_ONCE (wait_head->status,NBL_MBX_STATUS_ACKD ); +WRITE_ONCE (wait_head->ack_data_len,0 ); +WRITE_ONCE (wait_head->ack_err, (s32)-EIO ); +/* Publish completion fields before the acked flag */ +smp_wmb (); +WRITE_ONCE (wait_head->acked,1 ); +wake_up (&wait_head->wait_queue); + } +mutex_unlock (&chan_info->pending_lock); + +/* +* Step3: +* stop the RX queue unconditionally. The RX QINFO region is written +* only by config/stop paths, never by senders (which only touch the +* TX QINFO region and the doorbell), so no txq_lock is needed. The +* RX engine must be quiesced before the rings are freed, regardless +* of TX-side state - skipping it would leave the device DMAing peer +* messages and descriptor writebacks into released coherent memory. +*/ +nbl_chan_stop_rx_queue (chan_mgt); + +/* +* Drain strategy mirrors mlx5 command interface teardown: +* set shutdown flag first, abort all pending waiters, then +* block until inflight_tx_cnt reaches zero. +* +* After shutdn is set every sender exits promptly at its next +* checkpoint: +* - interrupt-driven senders wake on shutdn immediately +* (it is part of the wait_event condition); +* - ACK polling senders check shutdn each iteration before +* sleeping 1000-1200us. This per-iteration check avoids +* waiting the full 5.0-6.0s worst-case ACK timeout. +* +* The wait is deliberately unbounded: a surviving sender still +* holds txq_lock or touches chan_info (state_lock, wait[]) on its +* exit path, so returning early would let devres free the rings +* and chan_info underneath it. In practice the TX polling loop is +* bounded (NBL_CHAN_TX_WAIT_TIMES) and wedged-link MMIO reads +* eventually complete with all-1s data, so this loop terminates; +* warn periodically to keep a genuinely stuck sender diagnosable. +* +* A sleepable rwsem held for read across each sender (released by +* teardown with down_write()) would express the same exclusion with +* less code and with lockdep coverage. It is not used because the +* periodic warning above is the only diagnostic for a sender wedged +* on a dead link: with a rwsem, teardown would block silently in +* down_write() and the driver would look hung instead of reporting +* which side is stuck. The counter and the join below are correct +* without it - see the comment on the join for the exact invariant. +*/ +while (wait_event_timeout (chan_info->inflight_wait, +atomic_read (&chan_info->inflight_tx_cnt) ==0 , +msecs_to_jiffies (5000 )) ==0 ) +dev_warn (chan_mgt->common->dev, +"teardown: still waiting for %d inflight sender(s)\n" , +atomic_read (&chan_info->inflight_tx_cnt)); + +/* +* Join the last exiting sender through state_lock. A sender drops +* inflight_tx_cnt and wakes this drain while still holding +* state_lock, so observing the counter reach zero only proves the +* decrement happened; the acquire below is what proves that no +* sender still holds the lock. Once it returns, no sender can be +* using chan_info either: the decrement is the last field access a +* sender makes, and the only thing that follows it is the +* mutex_unlock() of state_lock itself. +* +* That trailing unlock is why this is one acquire/release pair and +* not a handshake that ends the sender: mutex_unlock() may still be +* touching the mutex structure after it has released it, so this does +* not prove the sender has returned from it (see +* Documentation/locking/mutex-design.rst). It is safe only because +* chan_info - mutexes included - is devm-allocated and released at +* detach, long after this teardown, and because nothing frees it +* here. Freeing chan_info or calling mutex_destroy() at this point +* would need a sender-exit completion the channel does not have. +* +* shutdn blocks new senders, so one acquire/release pair is +* sufficient. +*/ +mutex_lock (&chan_info->state_lock); +mutex_unlock (&chan_info->state_lock); + +/* +* All senders have exited, so txq_lock is uncontended: plain lock. +* Take it so this is the sole writer of the TX QINFO block +* (sender-side stop/config/doorbell sequences all run under +* txq_lock). +*/ +mutex_lock (&chan_info->txq_lock); +nbl_chan_stop_tx_queue (chan_mgt); +mutex_unlock (&chan_info->txq_lock); + +/* +* Join the RX clean work before active=false and before devres +* releases the rings. IRQ is already freed and clean_task is +* cleared under state_lock, so no new instance can be queued. +*/ +if (task) +cancel_work_sync (task); + +WRITE_ONCE (chan_info->active, false); +return 0 ; +} + +static int nbl_chan_setup_queue (struct nbl_channel_mgt *chan_mgt,u8 chan_type) +{ +struct nbl_chan_info *chan_info = chan_mgt->chan_info[chan_type]; +struct nbl_hw_ops *hw_ops = chan_mgt->hw_ops_tbl->ops; +struct nbl_common_info *common = chan_mgt->common; +struct nbl_chan_ring *rxq = &chan_info->rxq; +int err; + +if (READ_ONCE (chan_info->active)) { +dev_warn (common->dev,"channel already active, reject duplicate setup\n" ); +return -EBUSY ; + } +/* +* The descriptor rings and the per-ring contiguous buffer blocks are +* allocated once and released only by devres at device detach: there +* is no free path, so a channel is set up exactly once per device +* lifetime and teardown is terminal for it. Re-setup would allocate +* a second set - about 2 MiB of coherent DMA plus the descriptor +* rings and wait[] - and orphan the first one, and it would revive a +* queue whose state teardown has already retired, so it is rejected. +*/ +if (chan_info->dma_allocated) { +dev_warn (common->dev, +"channel DMA already allocated, re-setup not supported\n" ); +return -EBUSY ; + } + +nbl_chan_init_queue_param (chan_info,NBL_CHAN_QUEUE_LEN , +NBL_CHAN_QUEUE_LEN ,NBL_CHAN_BUF_LEN , +NBL_CHAN_BUF_LEN ); +/* +* Set the flag before the first allocation and leave it set even +* when setup fails: whatever nbl_chan_init_queue() managed to +* allocate is devres-owned and stays live until detach, so the retry +* that clearing the flag would allow would allocate a second set on +* top of the orphans. +*/ + chan_info->dma_allocated = true; + err =nbl_chan_init_queue (common, chan_info); +if (err) +return err; + +nbl_chan_config_queue (chan_mgt, chan_info, true);/* tx */ +nbl_chan_config_queue (chan_mgt, chan_info, false);/* rx */ +nbl_chan_update_tail_ptr (hw_ops, chan_mgt->hw_ops_tbl->priv, + rxq->tail_ptr,NBL_MB_RX_QID ); +WRITE_ONCE (chan_info->active, true); +return 0 ; +} + +static bool nbl_chan_txq_full (struct nbl_chan_ring *txq, +u16 num_entries) +{ +return NBL_NEXT_ID (txq->next_to_use, num_entries -1 ) == + txq->next_to_clean; +} + +static int nbl_chan_update_txqueue (struct nbl_channel_mgt *chan_mgt, +struct nbl_chan_info *chan_info, +struct nbl_chan_tx_param *param) +{ +struct nbl_chan_ring *txq = &chan_info->txq; +struct nbl_chan_tx_desc *tx_desc; +struct nbl_chan_buf *tx_buf; + +/* +* param->arg is non-NULL whenever param->arg_len is non-zero: +* nbl_chan_send_msg() validates that before calling here, because a +* length with no buffer would publish buf_addr/buf_len describing +* memory that was never filled. +*/ +if (nbl_chan_txq_full (txq, chan_info->num_txq_entries)) +return -EBUSY ; +if (param->arg_len >NBL_CHAN_BUF_LEN -sizeof (*tx_desc)) +return -EINVAL ; + tx_desc = +NBL_CHAN_TX_RING_TO_DESC (txq, txq->next_to_use); + tx_buf = +NBL_CHAN_TX_RING_TO_BUF (txq, txq->next_to_use); + tx_desc->dstid =cpu_to_le16 (param->dstid); + tx_desc->msg_type =cpu_to_le16 (param->msg_type); + tx_desc->msgid =cpu_to_le16 (param->msgid); + +/* +* srcid field is filled by mailbox hardware after peer receives this +* packet, driver producer never writes srcid; reused descriptor slots +* will contain stale srcid value temporarily until hardware overwrites +* it. +*/ +if (param->arg_len >NBL_CHAN_TX_DESC_EMBEDDED_DATA_LEN ) { +memcpy (tx_buf->va, param->arg, param->arg_len); + tx_desc->buf_addr =cpu_to_le64 (tx_buf->pa); + tx_desc->buf_len =cpu_to_le16 (param->arg_len); + tx_desc->data_len =0 ; +memset (tx_desc->data,0 ,sizeof (tx_desc->data)); + }else { +memset (tx_desc->data,0 ,sizeof (tx_desc->data)); +memset (&tx_desc->buf_addr,0 ,sizeof (tx_desc->buf_addr)); +if (param->arg_len) +memcpy (tx_desc->data, param->arg, param->arg_len); + tx_desc->buf_len =0 ; + tx_desc->data_len =cpu_to_le16 (param->arg_len); + } +/* Ensure descriptor data visible to device before AVAIL flag */ +dma_wmb (); + tx_desc->flags =cpu_to_le16 (BIT (NBL_CHAN_TX_DESC_AVAIL )); + + txq->next_to_use = +NBL_NEXT_ID (txq->next_to_use, chan_info->num_txq_entries -1 ); + txq->tail_ptr++; + +return 0 ; +} + +/* +* Quiesce the TX mailbox queue and reclaim all outstanding +* descriptors. Called from the timeout path of nbl_chan_kick_tx_ring() +* with txq_lock held. +* +* The device failed to fetch/complete the current descriptor within the +* polling window. We assert QUEUE_RST to stop further DMA fetches, +* reclaim every descriptor between next_to_clean and next_to_use, +* reset the software tail_ptr counter to match the hardware reset state, +* and re-enable the queue so subsequent sends can proceed. +*/ +static void nbl_chan_quiesce_and_reclaim_tx (struct nbl_channel_mgt *chan_mgt, +struct nbl_chan_info *chan_info) +{ +struct nbl_hw_ops *hw_ops = chan_mgt->hw_ops_tbl->ops; +struct nbl_hw_mgt *hw_priv = chan_mgt->hw_ops_tbl->priv; +struct nbl_chan_ring *txq = &chan_info->txq; +struct nbl_chan_tx_desc *tx_desc; + +/* +* Assert QUEUE_RST to stop hardware fetching new descriptors. +* stop_mailbox_txq() flushes through the mailbox BAR itself, so +* the reset has reached the device when it returns. +*/ + hw_ops->stop_mailbox_txq (hw_priv); + +/* +* Reclaim all outstanding descriptors between next_to_clean and +* next_to_use. Under txq_lock there is at most one in-flight +* descriptor, but iterate the full range for robustness. +*/ +while (txq->next_to_clean != txq->next_to_use) { + tx_desc =NBL_CHAN_TX_RING_TO_DESC (txq, + txq->next_to_clean); +WRITE_ONCE (tx_desc->flags,0 ); + txq->next_to_clean = +NBL_NEXT_ID (txq->next_to_clean, + chan_info->num_txq_entries -1 ); + } + +/* +* Hardware tail_ptr counter is cleared by QUEUE_RST. Reset +* software counter to match so the next doorbell update does +* not produce a false 16-bit wrap delta. +*/ + txq->tail_ptr =0 ; + txq->next_to_use =0 ; + txq->next_to_clean =0 ; + +/* +* Authoritative re-arm gate, evaluated while txq_lock is held: +* teardown sets shutdn before draining senders and then owns +* hardware state. If teardown started while we were stopping/ +* reclaiming, leave the queue in QUEUE_RST and never program +* QUEUE_EN, otherwise the queue could be armed again pointing +* at rings that are about to be freed. +*/ +if (READ_ONCE (chan_info->shutdn)) +return ; + +/* Re-enable queue with current ring base and size */ +nbl_chan_config_queue (chan_mgt, chan_info, true); +} + +static int nbl_chan_kick_tx_ring (struct nbl_channel_mgt *chan_mgt, +struct nbl_chan_info *chan_info) +{ +struct nbl_hw_ops *hw_ops = chan_mgt->hw_ops_tbl->ops; +struct nbl_chan_ring *txq = &chan_info->txq; +struct device *dev = chan_mgt->common->dev; +int max_retries =NBL_CHAN_TX_WAIT_TIMES ; +struct nbl_chan_tx_desc *tx_desc; +int retry_count =0 ; +u16 msg_type; + +nbl_chan_update_tail_ptr (hw_ops, chan_mgt->hw_ops_tbl->priv, + txq->tail_ptr,NBL_MB_TX_QID ); + + tx_desc =NBL_CHAN_TX_RING_TO_DESC (txq, txq->next_to_clean); +/* +* Poll for HW to mark descriptor as USED. +* Mailbox is a low-speed control channel for management commands. +* We avoid enabling dedicated per-TX interrupt for single control +* message to reduce interrupt overhead, so use bounded polling +* with small delay instead. +*/ +while (retry_count < max_retries) { +if (READ_ONCE (chan_info->shutdn)) +return -ESHUTDOWN ; + +if (le16_to_cpu (READ_ONCE (tx_desc->flags)) & +BIT (NBL_CHAN_TX_DESC_USED )) { +/* +* Order reads of other device-written descriptor +* fields after observing USED. Matches the RX side +* pattern in nbl_chan_clean_queue(). +*/ +dma_rmb (); +break ; + } + + retry_count++; +if (retry_count == max_retries) { + msg_type =le16_to_cpu (READ_ONCE (tx_desc->msg_type)); +dev_err_ratelimited (dev,"chan send msg type: %d timeout\n" , + msg_type); +/* +* Teardown may have started after the loop-top +* check on this final iteration. Do not run lock +* recovery then: it would stop/reclaim the queue +* and, without the in-quiesce gate, re-arm it. +* nbl_chan_teardown_queue() owns hardware state. +*/ +if (READ_ONCE (chan_info->shutdn)) +return -ESHUTDOWN ; +/* +* Device failed to complete this descriptor. +* Quiesce the queue, reclaim the timed-out +* descriptor, and re-enable so future sends can +* proceed instead of stalling the ring full. +*/ +nbl_chan_quiesce_and_reclaim_tx (chan_mgt, + chan_info); +return -ETIMEDOUT ; + } +usleep_range (NBL_CHAN_TX_WAIT_US ,NBL_CHAN_TX_WAIT_US_MAX ); + } + + txq->next_to_clean = txq->next_to_use; + +return 0 ; +} + +static void nbl_chan_recv_ack_msg (void *priv,u16 srcid,u16 msgid,void *data, +u32 data_len) +{ +struct nbl_channel_mgt *chan_mgt = (struct nbl_channel_mgt *)priv; +struct nbl_chan_waitqueue_head *wait_head =NULL ; +struct device *dev = chan_mgt->common->dev; +struct nbl_chan_info *chan_info = + chan_mgt->chan_info[NBL_CHAN_TYPE_MAILBOX ]; +u16 w_dstid, w_msgtype, w_msgidx; +u32 *payload = data; +u16 ack_msgtype =0 ; +u16 ack_msgid =0 ; +u32 ack_datalen; +void *ack_data; +u32 copy_len; +int w_status; +s32 raw_err; + +if (READ_ONCE (chan_info->shutdn)) +return ; +if (data_len >NBL_CHAN_BUF_LEN || + data_len <NBL_CHAN_ACK_HEAD_LEN *sizeof (u32)) { +dev_err_ratelimited (dev,"Invalid ACK data_len: %u\n" , + data_len); +return ; + } + ack_datalen = data_len -NBL_CHAN_ACK_HEAD_LEN *sizeof (u32); + ack_msgtype =le16_to_cpu (*(__le16 *)(payload +NBL_CHAN_MSG_TYPE_POS )); + ack_msgid =le16_to_cpu (*(__le16 *)(payload +NBL_CHAN_MSG_ID_POS )); +if (FIELD_GET (NBL_CHAN_MSGID_LOC_MASK , ack_msgid) >= + chan_info->num_txq_entries) { +dev_err_ratelimited (dev,"chan recv msg id: %u err\n" , + ack_msgid); +return ; + } + wait_head = + &chan_info->wait[FIELD_GET (NBL_CHAN_MSGID_LOC_MASK , ack_msgid)]; + +mutex_lock (&chan_info->pending_lock); + +/* Cache repeated READ_ONCE values */ + w_dstid =READ_ONCE (wait_head->dstid); + w_status =READ_ONCE (wait_head->status); + w_msgtype =READ_ONCE (wait_head->msg_type); + w_msgidx =READ_ONCE (wait_head->msg_index); + +if (srcid != w_dstid) { +mutex_unlock (&chan_info->pending_lock); +dev_err_ratelimited (dev,"ACK srcid=%u != dstid=%u, rejecting\n" , + srcid, w_dstid); +return ; + } +if (w_status !=NBL_MBX_STATUS_WAITING ) { +mutex_unlock (&chan_info->pending_lock); +dev_err_ratelimited (dev, +"Skip ack invalid status, wait msgtype:%u idx:%u status:%d ack msgtype:%u msgid:%u datalen:%u\n" , + w_msgtype, w_msgidx, w_status, + ack_msgtype, ack_msgid, ack_datalen); +return ; + } + +if (w_msgtype != ack_msgtype) { +mutex_unlock (&chan_info->pending_lock); +dev_err_ratelimited (dev, +"Skip ack msgtype mismatch, wait msgtype:%u idx:%u ack msgtype:%u msgid:%u\n" , + w_msgtype, w_msgidx, ack_msgtype, + ack_msgid); +return ; + } +if (FIELD_GET (NBL_CHAN_MSGID_INDEX_MASK , ack_msgid) != w_msgidx) { +mutex_unlock (&chan_info->pending_lock); +dev_err_ratelimited (dev, +"Stale ACK: expected index=%u, got msgid=%u\n" , + w_msgidx, ack_msgid); +return ; + } + + raw_err = (s32 )le32_to_cpu (*(__le32 *)&payload[NBL_CHAN_ACK_RET_POS ]); +if (raw_err >0 || raw_err < -MAX_ERRNO ) + raw_err = -EREMOTEIO ; + +WRITE_ONCE (wait_head->ack_err, raw_err); + + copy_len =min_t (u32,READ_ONCE (wait_head->ack_data_len), ack_datalen); +if (READ_ONCE (wait_head->ack_err) >=0 && copy_len >0 ) { + ack_data =READ_ONCE (wait_head->ack_data); +if (!ack_data) { +dev_err_ratelimited (dev,"ACK payload dropped: ack_data is NULL\n" ); +WRITE_ONCE (wait_head->ack_data_len,0 ); + goto ack_done; + } +memcpy ((char *)ack_data, + payload +NBL_CHAN_ACK_HEAD_LEN , copy_len); +WRITE_ONCE (wait_head->ack_data_len, (u16 )copy_len); + }else { +WRITE_ONCE (wait_head->ack_data_len,0 ); + } +ack_done: +/* Guarantee payload data finished before acked flag visible */ +smp_wmb (); +WRITE_ONCE (wait_head->acked,1 ); +WRITE_ONCE (wait_head->status,NBL_MBX_STATUS_ACKD ); +mutex_unlock (&chan_info->pending_lock); +wake_up (&wait_head->wait_queue); +} + +static void nbl_chan_recv_msg (struct nbl_channel_mgt *chan_mgt,void *data) +{ +struct device *dev = chan_mgt->common->dev; +struct nbl_chan_msg_node_data *msg_handler; +u16 msg_type, payload_len, srcid, msgid; +struct nbl_chan_info *chan_info = + chan_mgt->chan_info[NBL_CHAN_TYPE_MAILBOX ]; +struct nbl_chan_tx_desc *tx_desc; +void *payload; +size_t avail_space; +u16 data_len_fw; + +if (READ_ONCE (chan_info->shutdn)) +return ; + + tx_desc = data; + msg_type =le16_to_cpu (READ_ONCE (tx_desc->msg_type)); +dev_dbg (dev,"recv msg_type: %d\n" , msg_type); + + srcid =le16_to_cpu (READ_ONCE (tx_desc->srcid)); + msgid =le16_to_cpu (READ_ONCE (tx_desc->msgid)); + +if (msg_type >=NBL_CHAN_MSG_MAILBOX_MAX ) +return ; + + data_len_fw =le16_to_cpu (READ_ONCE (tx_desc->data_len)); +if (data_len_fw) { + payload_len = data_len_fw; + +if (payload_len >NBL_CHAN_TX_DESC_EMBEDDED_DATA_LEN ) { +dev_err_ratelimited (dev, +"data_len=%u exceeds embedded buffer size=%u\n" , + payload_len, +NBL_CHAN_TX_DESC_EMBEDDED_DATA_LEN ); +return ; + } +/* Small pkt: payload stored inside descriptor data[] array */ + payload = tx_desc->data; + }else { + payload_len =le16_to_cpu (READ_ONCE (tx_desc->buf_len)); + + avail_space =NBL_CHAN_BUF_LEN -sizeof (*tx_desc); +if (payload_len > avail_space) { +dev_err_ratelimited (dev, +"buf_len=%u exceeds external buffer size=%zu\n" , + payload_len, avail_space); +return ; + } +/* Large pkt: payload follows immediately after tx_desc */ + payload = tx_desc +1 ; + } + + msg_handler =xa_load (&chan_mgt->handler_xa, msg_type); +if (!msg_handler || !msg_handler->func) { +dev_err_ratelimited (dev, +"No handler for msg_type: %u (srcid=%u, msgid=%u)\n" , + msg_type, srcid, msgid); +return ; + } + + msg_handler->func (msg_handler->priv, srcid, msgid, payload, + payload_len); +} + +static void nbl_chan_advance_rx_ring (struct nbl_channel_mgt *chan_mgt, +struct nbl_chan_info *chan_info, +struct nbl_chan_ring *rxq) +{ +struct nbl_hw_ops *hw_ops = chan_mgt->hw_ops_tbl->ops; +struct nbl_chan_rx_desc *rx_desc; +struct nbl_chan_buf *rx_buf; +u16 next_to_use; + + next_to_use = rxq->next_to_use; + rx_desc =NBL_CHAN_RX_RING_TO_DESC (rxq, next_to_use); + rx_buf =NBL_CHAN_RX_RING_TO_BUF (rxq, next_to_use); + +/* +* Recycle the RX descriptor at next_to_use. The initial +* unused slot is intentionally recycled after the first +* RX descriptor is consumed, allowing the ring to become +* fully populated while next_to_clean tracks the consumer. +*/ + rx_desc->buf_addr =cpu_to_le64 (rx_buf->pa); + rx_desc->buf_len =cpu_to_le32 (chan_info->rxq_buf_size); + +/* +* DMA Write Memory Barrier: +* Ensures all previous DMA-mapped writes (buffer address/length) +* are completed before the descriptor flags are updated. +* This prevents hardware from seeing a partially updated descriptor +* where flags are set but buffer info isn't ready yet. +*/ +dma_wmb (); + + rx_desc->flags =cpu_to_le16 (BIT (NBL_CHAN_RX_DESC_AVAIL )); + + rxq->next_to_use++; +if (rxq->next_to_use == chan_info->num_rxq_entries) + rxq->next_to_use =0 ; + rxq->tail_ptr++; + +nbl_chan_update_tail_ptr (hw_ops, chan_mgt->hw_ops_tbl->priv, + rxq->tail_ptr,NBL_MB_RX_QID ); +} + +static void nbl_chan_clean_queue (struct nbl_channel_mgt *chan_mgt, +struct nbl_chan_info *chan_info) +{ +struct nbl_common_info *common = chan_mgt->common; +struct nbl_chan_ring *rxq = &chan_info->rxq; +struct device *dev = chan_mgt->common->dev; +u32 budget =NBL_CHAN_RX_CLEAN_BUDGET ; +struct nbl_chan_rx_desc *rx_desc; +struct nbl_chan_buf *rx_buf; +struct work_struct *task; +bool more_work = false; +u16 next_to_clean; +u16 flags; + +/* Nothing to clean before setup: the RX ring is still NULL */ +if (!READ_ONCE (chan_info->active)) +return ; + + next_to_clean = rxq->next_to_clean; + rx_desc =NBL_CHAN_RX_RING_TO_DESC (rxq, next_to_clean); + rx_buf =NBL_CHAN_RX_RING_TO_BUF (rxq, next_to_clean); +while (le16_to_cpu (READ_ONCE (rx_desc->flags)) & +BIT (NBL_CHAN_RX_DESC_USED )) { + flags =le16_to_cpu (READ_ONCE (rx_desc->flags)); + +if (READ_ONCE (chan_info->shutdn)) +break ; + +/* Make sure hardware written descriptor visible to CPU */ +dma_rmb (); + +/* +* Only a descriptor the device marked WRITE carries a message. +* The RX buffers are fixed slots of one coherent block and +* nbl_chan_advance_rx_ring() does not clear the payload, so +* parsing an unwritten slot would replay the message of the +* previous ring lap to its handler. Drop such a completion and +* recycle it instead. +*/ +if (flags &BIT (NBL_CHAN_RX_DESC_WRITE )) +nbl_chan_recv_msg (chan_mgt, rx_buf->va); +else +dev_warn_ratelimited (dev, +"mailbox rx desc %u completed without WRITE, flags 0x%x\n" , + next_to_clean, flags); +nbl_chan_advance_rx_ring (chan_mgt, chan_info, rxq); + next_to_clean++; +if (next_to_clean == chan_info->num_rxq_entries) + next_to_clean =0 ; + rx_desc =NBL_CHAN_RX_RING_TO_DESC (rxq, next_to_clean); + rx_buf =NBL_CHAN_RX_RING_TO_BUF (rxq, next_to_clean); +if (--budget ==0 ) { + more_work = true; +break ; + } +cond_resched (); + } + rxq->next_to_clean = next_to_clean; + +mutex_lock (&chan_info->state_lock); +/* Prevent queue_work after teardown clears clean_task */ +if (READ_ONCE (chan_info->shutdn)) { +mutex_unlock (&chan_info->state_lock); +return ; + } +if (common->wq && more_work) { + task =READ_ONCE (chan_info->clean_task); +if (task) +queue_work (common->wq, task); + } +mutex_unlock (&chan_info->state_lock); +} + +static void nbl_chan_clean_queue_subtask (struct nbl_channel_mgt *chan_mgt, +u8 chan_type) +{ +struct nbl_chan_info *chan_info = chan_mgt->chan_info[chan_type]; + +nbl_chan_clean_queue (chan_mgt, chan_info); +} + +static int nbl_chan_get_msg_id (struct nbl_chan_info *chan_info, +u16 *msgid) +{ +int search_loc =READ_ONCE (chan_info->wait_head_index), i; +struct nbl_chan_waitqueue_head *wait =NULL ; +int status; +int next; + +lockdep_assert_held (&chan_info->pending_lock); +for (i =0 ; i < chan_info->num_txq_entries; i++) { + wait = &chan_info->wait[search_loc]; + status =READ_ONCE (wait->status); +if (status ==NBL_MBX_STATUS_IDLE || + status ==NBL_MBX_STATUS_TIMEOUT ) { +WRITE_ONCE (wait->msg_index, +NBL_NEXT_ID (wait->msg_index, +NBL_CHAN_MSG_INDEX_MAX )); + + *msgid =FIELD_PREP (NBL_CHAN_MSGID_INDEX_MASK , + wait->msg_index) | +FIELD_PREP (NBL_CHAN_MSGID_LOC_MASK , + search_loc); + +/* Advance starting search position for next caller */ + next =NBL_NEXT_ID (search_loc, + chan_info->num_txq_entries -1 ); +WRITE_ONCE (chan_info->wait_head_index, next); +return 0 ; + } + + search_loc =NBL_NEXT_ID (search_loc, + chan_info->num_txq_entries -1 ); + } + +/* +* All tx slots are occupied. May happen under high transmit load +* or delayed remote ACK responses. Caller should retry later. +*/ +return -EAGAIN ; +} + +static int nbl_chan_send_msg (struct nbl_channel_mgt *chan_mgt, +struct nbl_chan_send_info *chan_send) +{ +struct nbl_common_info *common = chan_mgt->common; +struct nbl_chan_waitqueue_head *wait_head =NULL ; +struct nbl_chan_tx_param tx_param = {0 }; +int i =NBL_CHAN_TX_WAIT_ACK_TIMES ; +struct nbl_chan_info *chan_info = + chan_mgt->chan_info[NBL_CHAN_TYPE_MAILBOX ]; +struct device *dev = common->dev; +struct work_struct *task; +u16 msgid =0 ; +int ret; + +if (chan_send->resp_len >NBL_CHAN_BUF_LEN ) { +dev_err_ratelimited (dev,"resp_len %zu exceeds max %d\n" , + chan_send->resp_len,NBL_CHAN_BUF_LEN ); +return -EINVAL ; + } + +/* +* A length without a payload would leave the descriptor pointing at +* whatever the reused slot still holds - nbl_chan_update_txqueue() +* skips the copy - and the peer would receive those stale bytes. +* Reject the combination instead of transmitting them. +*/ +if (chan_send->arg_len && !chan_send->arg) { +dev_err_ratelimited (dev,"arg_len %zu without arg\n" , + chan_send->arg_len); +return -EINVAL ; + } + +/* +* Entry points are only reached after a successful setup_queue() +* (probe path), but the channel is a dynamic object: fail cleanly +* instead of dereferencing rings/wait[] that were never allocated +* (num_txq_entries == 0). active is published last in setup and +* cleared last in teardown, so a false value here means the channel +* never came up - or has already been torn down, and shutdn tells +* the two apart: it is set before active is cleared, so a +* post-teardown sender gets the same -ESHUTDOWN as the check below +* rather than the -ENODEV reserved for a channel setup_queue() +* never completed. +*/ +if (!READ_ONCE (chan_info->active)) { +if (READ_ONCE (chan_info->shutdn)) +return -ESHUTDOWN ; +return -ENODEV ; + } + +mutex_lock (&chan_info->state_lock); +if (READ_ONCE (chan_info->shutdn)) { +mutex_unlock (&chan_info->state_lock); +return -ESHUTDOWN ; + } +atomic_inc (&chan_info->inflight_tx_cnt); +mutex_unlock (&chan_info->state_lock); + + tx_param.msg_type = chan_send->msg_type; + tx_param.arg = chan_send->arg; + tx_param.arg_len = chan_send->arg_len; + tx_param.dstid = chan_send->dstid; + tx_param.msgid = msgid; +if (chan_send->ack) { +mutex_lock (&chan_info->pending_lock); + + ret =nbl_chan_get_msg_id (chan_info, &msgid); +if (ret) { +mutex_unlock (&chan_info->pending_lock); +dev_err_ratelimited (dev, +"Channel tx wait head full, send msgtype:%u to dstid:%u failed\n" , + chan_send->msg_type, + chan_send->dstid); + goto out_clean_inflight; + } + wait_head = + &chan_info->wait[FIELD_GET (NBL_CHAN_MSGID_LOC_MASK , + msgid)]; +WRITE_ONCE (wait_head->acked,0 ); +WRITE_ONCE (wait_head->ack_data, chan_send->resp); +WRITE_ONCE (wait_head->ack_data_len, chan_send->resp_len); +WRITE_ONCE (wait_head->msg_type, chan_send->msg_type); +WRITE_ONCE (wait_head->msg_index, +FIELD_GET (NBL_CHAN_MSGID_INDEX_MASK , msgid)); +WRITE_ONCE (wait_head->dstid, chan_send->dstid); + +WRITE_ONCE (wait_head->status,NBL_MBX_STATUS_WAITING ); +mutex_unlock (&chan_info->pending_lock); + + tx_param.msgid = msgid; + } + +mutex_lock (&chan_info->txq_lock); + ret =nbl_chan_update_txqueue (chan_mgt, chan_info, &tx_param); +if (ret) { +mutex_unlock (&chan_info->txq_lock); +dev_err_ratelimited (dev, +"Channel tx queue full, send msgtype:%u to dstid:%u failed\n" , + chan_send->msg_type, chan_send->dstid); +if (wait_head) + goto out_clear_wait_slot; + goto out_clean_inflight; + } + + ret =nbl_chan_kick_tx_ring (chan_mgt, chan_info); +mutex_unlock (&chan_info->txq_lock); +if (ret) { +if (wait_head) + goto out_clear_wait_slot; + goto out_clean_inflight; + } + +if (!chan_send->ack) { + ret =0 ; + goto out_clean_inflight; + } + +if (test_bit (NBL_CHAN_IRQ_RDY , chan_info->state)) { +while (!READ_ONCE (wait_head->acked)) { +/* +* avoids long task blocking when interrupt mode is +* disabled mid-wait. Cannot guarantee subsequent ACK +* delivery after interrupt mask off, only prevents +* infinite blocking. Spurious timeout is possible. +*/ + ret =wait_event_timeout (wait_head->wait_queue, +READ_ONCE (wait_head->acked) || +READ_ONCE (chan_info->shutdn) || + !test_bit (NBL_CHAN_IRQ_RDY , + chan_info->state), +NBL_CHAN_ACK_WAIT_TIME ); + +if (READ_ONCE (chan_info->shutdn)) { + ret = -ESHUTDOWN ; + goto out_clear_wait_slot; + } +if (!test_bit (NBL_CHAN_IRQ_RDY , chan_info->state)) { + ret = -EIO ; + goto out_clear_wait_slot; + } +if (ret ==0 ) { +mutex_lock (&chan_info->pending_lock); +if (READ_ONCE (wait_head->status) == +NBL_MBX_STATUS_WAITING ) { +WRITE_ONCE (wait_head->status, +NBL_MBX_STATUS_TIMEOUT ); +WRITE_ONCE (wait_head->acked,0 ); +WRITE_ONCE (wait_head->ack_data,NULL ); +WRITE_ONCE (wait_head->ack_data_len,0 ); +/* +* Ensure all status/ack slot +* updates are visible before subsequent +* readers observe acked == 0 +*/ +smp_wmb (); +mutex_unlock (&chan_info->pending_lock); +dev_err_ratelimited (dev, +"Channel waiting ack failed, message type: %d, msg id: %u\n" , + chan_send->msg_type, + msgid); + ret = -ETIMEDOUT ; +/* +* TIMEOUT slots can be reused by +* another sender. A late ACK is +* rejected by the receive path, +* which only accepts WAITING slots +* (and by the msg_index generation +* check after reallocation). The +* current sender no longer owns the +* slot, so skip the IDLE reset. +*/ + goto out_clean_inflight; + } +/* +* ACK won the timeout race: between the +* final condition check and this lock the +* receiver published acked=1/ACKD and +* copied the payload into our response +* buffer. The slot is still exclusively +* ours (only IDLE/TIMEOUT slots are ever +* reallocated), so consume the completion: +* unlock and continue to the loop-bottom +* acked check, which enters the normal +* success readout and IDLE reset. Leaving +* here via the timeout path would strand +* the slot in ACKD forever, permanently +* shrinking the 256-entry pool, and would +* report a false -ETIMEDOUT for a request +* whose response already arrived. +*/ +mutex_unlock (&chan_info->pending_lock); + } + +if (READ_ONCE (wait_head->acked)) +break ; + } +if (READ_ONCE (wait_head->acked)) { +/* +* Load ordering: observe acked flag before +* reading ACK payload metadata. +*/ +smp_rmb (); + chan_send->ack_len =READ_ONCE (wait_head->ack_data_len); + ret =READ_ONCE (wait_head->ack_err); + } + }else { +/* Polling path for synchronous ACK */ +while (i--) { +if (READ_ONCE (chan_info->shutdn)) { + ret = -ESHUTDOWN ; + goto out_clear_wait_slot; + } + +mutex_lock (&chan_info->state_lock); + task =READ_ONCE (chan_info->clean_task); +if (common->wq && task && + !READ_ONCE (chan_info->shutdn) && + !work_pending (task)) +queue_work (common->wq, task); +mutex_unlock (&chan_info->state_lock); +if (READ_ONCE (wait_head->acked)) { +/* +* Guarantee load order: observe acked +* flag before reading ack payload metadata. +*/ +smp_rmb (); + chan_send->ack_len = +READ_ONCE (wait_head->ack_data_len); + ret =READ_ONCE (wait_head->ack_err); + goto out_clear_wait_slot; + } + +usleep_range (NBL_CHAN_TX_WAIT_ACK_US_MIN , +NBL_CHAN_TX_WAIT_ACK_US_MAX ); +cond_resched (); + } +mutex_lock (&chan_info->pending_lock); +if (READ_ONCE (wait_head->status) ==NBL_MBX_STATUS_ACKD ) { + chan_send->ack_len =READ_ONCE (wait_head->ack_data_len); + ret =READ_ONCE (wait_head->ack_err); +mutex_unlock (&chan_info->pending_lock); + goto out_clear_wait_slot; + } +/* +* Polling timed out without receiving an ACK. Transition +* the slot to TIMEOUT so nbl_chan_get_msg_id() can reuse +* it. Must not proceed to out_clean_inflight with +* the slot still in WAITING state — that would leak the +* slot permanently and eventually exhaust the 256-entry +* channel queue. +*/ +if (READ_ONCE (wait_head->status) ==NBL_MBX_STATUS_WAITING ) { +WRITE_ONCE (wait_head->status,NBL_MBX_STATUS_TIMEOUT ); +WRITE_ONCE (wait_head->acked,0 ); +WRITE_ONCE (wait_head->ack_data,NULL ); +WRITE_ONCE (wait_head->ack_data_len,0 ); + } +mutex_unlock (&chan_info->pending_lock); +dev_err_ratelimited (dev, +"Channel polling ack failed, message type: %d msg id: %u\n" , + chan_send->msg_type, msgid); + ret = -ETIMEDOUT ; + goto out_clean_inflight; + } + +out_clear_wait_slot: +mutex_lock (&chan_info->pending_lock); +nbl_chan_reset_wait_head (chan_info, wait_head); +mutex_unlock (&chan_info->pending_lock); + +out_clean_inflight: +mutex_lock (&chan_info->state_lock); +if (atomic_dec_and_test (&chan_info->inflight_tx_cnt)) +wake_up (&chan_info->inflight_wait); +mutex_unlock (&chan_info->state_lock); +return ret; +} + +static int nbl_chan_send_ack (struct nbl_channel_mgt *chan_mgt, +struct nbl_chan_ack_info *chan_ack) +{ +size_t head_len =NBL_CHAN_ACK_HEAD_LEN *sizeof (u32); +size_t data_len = chan_ack->data_len; +struct nbl_chan_send_info chan_send; +__le32 *tmp; +size_t len; +int ret; + +if (data_len > +NBL_CHAN_BUF_LEN -sizeof (struct nbl_chan_tx_desc ) - head_len) +return -EINVAL ; + + len = head_len + data_len; + tmp =kzalloc (len,GFP_KERNEL ); +if (!tmp) +return -ENOMEM ; + + *(__le16 *)&tmp[NBL_CHAN_MSG_TYPE_POS ] = +cpu_to_le16 (chan_ack->msg_type); + *(__le16 *)&tmp[NBL_CHAN_MSG_ID_POS ] =cpu_to_le16 (chan_ack->msgid); + tmp[NBL_CHAN_ACK_RET_POS ] =cpu_to_le32 (chan_ack->err); +if (chan_ack->data && chan_ack->data_len) +memcpy (&tmp[NBL_CHAN_ACK_HEAD_LEN ], chan_ack->data, + chan_ack->data_len); + +nbl_chan_fill_send_info (&chan_send, chan_ack->dstid,NBL_CHAN_MSG_ACK , + tmp, len,NULL ,0 ,0 ); + ret =nbl_chan_send_msg (chan_mgt, &chan_send); +kfree (tmp); + +return ret; +} + +static int nbl_chan_register_msg (struct nbl_channel_mgt *chan_mgt,u16 msg_type, +nbl_chan_resp func,void *callback) +{ +return nbl_chan_add_msg_handler (chan_mgt, msg_type, func, callback); +} + +static bool nbl_chan_check_queue_exist (struct nbl_channel_mgt *chan_mgt, +u8 chan_type) +{ +struct nbl_chan_info *chan_info = chan_mgt->chan_info[chan_type]; + +return chan_info ? true : false; +} + +static void nbl_chan_register_chan_task (struct nbl_channel_mgt *chan_mgt, +u8 chan_type,struct work_struct *task) +{ +struct nbl_chan_info *chan_info = chan_mgt->chan_info[chan_type]; + +mutex_lock (&chan_info->state_lock); +if (!READ_ONCE (chan_info->shutdn)) +WRITE_ONCE (chan_info->clean_task, task); +mutex_unlock (&chan_info->state_lock); +} + +static void nbl_chan_set_queue_state (struct nbl_channel_mgt *chan_mgt, +enum nbl_chan_state state,u8 chan_type, +u8 set) +{ +struct nbl_chan_info *chan_info = chan_mgt->chan_info[chan_type]; +int i; + +if (set) +set_bit (state, chan_info->state); +else +clear_bit (state, chan_info->state); +/* +* When clearing IRQ_RDY, wake all per-slot wait queues so +* sleeping senders observe the condition immediately and +* return -EIO instead of waiting out the 3s timeout and +* reporting -ETIMEDOUT. +* +* wait[] is only allocated by a successful nbl_chan_init_tx_queue(), +* while num_txq_entries is already set by nbl_chan_init_queue_param(); +* skip the walk if that allocation never happened. +*/ +if (!set && state ==NBL_CHAN_IRQ_RDY && chan_info->wait) { +for (i =0 ; i < chan_info->num_txq_entries; i++) +wake_up_all (&chan_info->wait[i].wait_queue); + } +} + +static struct nbl_channel_ops chan_ops = { + .send_msg = nbl_chan_send_msg, + .send_ack = nbl_chan_send_ack, + .register_msg = nbl_chan_register_msg, + .cfg_chan_qinfo_map_table = nbl_chan_cfg_qinfo_map_table, + .check_queue_exist = nbl_chan_check_queue_exist, + .setup_queue = nbl_chan_setup_queue, + .teardown_queue = nbl_chan_teardown_queue, + .clean_queue_subtask = nbl_chan_clean_queue_subtask, + .register_chan_task = nbl_chan_register_chan_task, + .set_queue_state = nbl_chan_set_queue_state, +}; + +static struct nbl_channel_mgt * +nbl_chan_setup_chan_mgt (struct nbl_adapter *adapter) +{ +struct nbl_hw_ops_tbl *hw_ops_tbl = adapter->intf.hw_ops_tbl; +struct nbl_common_info *common = &adapter->common; +struct device *dev = &adapter->pdev->dev; +struct nbl_channel_mgt *chan_mgt; +struct nbl_chan_info *mailbox; +int ret; + + chan_mgt =devm_kzalloc (dev,sizeof (*chan_mgt),GFP_KERNEL ); +if (!chan_mgt) +return ERR_PTR (-ENOMEM ); + + chan_mgt->common = common; + chan_mgt->hw_ops_tbl = hw_ops_tbl; + + mailbox =devm_kzalloc (dev,sizeof (*mailbox),GFP_KERNEL ); +if (!mailbox) +return ERR_PTR (-ENOMEM ); + mailbox->chan_type =NBL_CHAN_TYPE_MAILBOX ; + chan_mgt->chan_info[NBL_CHAN_TYPE_MAILBOX ] = mailbox; + + ret =nbl_chan_init_msg_handler (chan_mgt); +if (ret) +return ERR_PTR (ret); + ret =devm_mutex_init (common->dev, &mailbox->txq_lock); +if (ret) +return ERR_PTR (ret); + ret =devm_mutex_init (common->dev, &mailbox->state_lock); +if (ret) +return ERR_PTR (ret); + ret =devm_mutex_init (common->dev, &mailbox->pending_lock); +if (ret) +return ERR_PTR (ret); +return chan_mgt; +} + +static struct nbl_channel_ops_tbl * +nbl_chan_setup_ops (struct device *dev,struct nbl_channel_mgt *chan_mgt) +{ +struct nbl_channel_ops_tbl *chan_ops_tbl; +int ret; + + chan_ops_tbl =devm_kzalloc (dev,sizeof (*chan_ops_tbl),GFP_KERNEL ); +if (!chan_ops_tbl) +return ERR_PTR (-ENOMEM ); +if (!chan_ops.send_msg || !chan_ops.send_ack || + !chan_ops.register_msg || !chan_ops.cfg_chan_qinfo_map_table || + !chan_ops.check_queue_exist || !chan_ops.setup_queue || + !chan_ops.teardown_queue || !chan_ops.clean_queue_subtask || + !chan_ops.register_chan_task || !chan_ops.set_queue_state) +return ERR_PTR (-EINVAL ); + + chan_ops_tbl->ops = &chan_ops; + chan_ops_tbl->priv = chan_mgt; + + ret =nbl_chan_register_msg (chan_mgt,NBL_CHAN_MSG_ACK , + nbl_chan_recv_ack_msg, chan_mgt); +if (ret) +return ERR_PTR (ret); + +return chan_ops_tbl; +} + +int nbl_chan_init_common (struct nbl_adapter *adap) +{ +struct nbl_channel_ops_tbl *chan_ops_tbl; +struct device *dev = &adap->pdev->dev; +struct nbl_channel_mgt *chan_mgt; +int ret; + + chan_mgt =nbl_chan_setup_chan_mgt (adap); +if (IS_ERR (chan_mgt)) +return PTR_ERR (chan_mgt); + + chan_ops_tbl =nbl_chan_setup_ops (dev, chan_mgt); +if (IS_ERR (chan_ops_tbl)) { + ret =PTR_ERR (chan_ops_tbl); + goto cleanup_mgt; + } + + adap->intf.channel_ops_tbl = chan_ops_tbl; + adap->core.chan_mgt = chan_mgt; + ret =nbl_common_create_wq (&adap->common); +if (ret) + goto cleanup_mgt; +return 0 ; + +cleanup_mgt: +nbl_chan_remove_msg_handler (chan_mgt); +return ret; +} + +void nbl_chan_remove_common (struct nbl_adapter *adap) +{ +struct nbl_channel_mgt *chan_mgt = adap->core.chan_mgt; + +if (!chan_mgt)
drivers/net/ethernet/nebula-matrix/nbl/nbl_main.c line 1546
Quoted 10 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_main.c +10
-0+return ; +nbl_common_destroy_wq (&adap->common); +/* +* All channel queues shall be torn down earlier in remove path +* to drain inflight tx workers and stop hardware before destroying +* message handler xarray. +*/ +nbl_chan_remove_msg_handler (chan_mgt); + adap->core.chan_mgt =NULL ; +}
[Severity: Medium]
Can the RX clean work still ring the RX doorbell after the queue has been put into reset here?
Step 1 sets shutdn and clears clean_task. That stops new work from being queued, but it does not wait for an nbl_chan_clean_queue() instance that is already running. cancel_work_sync(task) is only called at the very end of nbl_chan_teardown_queue(), after the RX stop, the inflight sender drain and the TX stop.
Suppose a worker read shutdn as false at the top of its loop. It then goes into nbl_chan_recv_msg() and nbl_chan_advance_rx_ring(), which reposts the descriptor and calls nbl_chan_update_tail_ptr() with NBL_MB_RX_QID. If that happens after nbl_hw_stop_mailbox_rxq() has asserted QUEUE_RST and cleared the ring base and size, the device gets an RX doorbell with a non-zero tail pointer. That doorbell is for a queue held in reset with a base address of 0.
The window is not tiny. nbl_chan_recv_msg() runs the registered handler before the doorbell write, and that handler can take a while.
The Step 3 comment only reasons about senders. It does not account for the clean worker, which is still a live writer of the RX doorbell at this point. The comment on the cancel_work_sync() call covers the rings being released, but not this ordering against the hardware stop.
Whether the device ignores a doorbell on a queue in QUEUE_RST depends on the hardware, so this may turn out to be harmless. Would it be simpler to join the work before stopping the queue? After Step 1, shutdn is already set and clean_task is already NULL. The polling sender only queues the work under state_lock with shutdn clear. So moving cancel_work_sync(task) ahead of nbl_chan_stop_rx_queue() looks safe, and it would make the RX stop the last write to the RX side.
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261112094925.6098-1-illusion.wang%40nebula-matrix.com
illusion.wang
maintainerPATCH v34 2/8 net/nebula-matrix: add common resource implementation
In reply to illusion.wangFrom: illusion wang <[email protected]>
Add chip-agnostic resource layer to manage PF topology, SR-IOV BDF, Ethernet port and VSI identity mappings, providing reusable conversion helpers for the Nebula Matrix driver control plane.
This patch implements core resource initialization and lookup logic, running exclusively on the control PF and building read-only resource tables once during probe:
- nbl_res_init_pf_num(): parse firmware PF mask and validate supported topologies. Only contiguous 1/2/4 PFs starting from PF0 are permitted, rejecting invalid/sparse configurations with early probe failure.
- nbl_common_func_id_to_rel_pf_id(): convert absolute PF function ID to relative PF index for consistent resource table indexing.
- nbl_res_ctrl_dev_sriov_info_init(): calculate and store per-PF BDF entries based on hardware PCI bus number for MSI-X programming.
- nbl_res_ctrl_dev_setup_eth_info(): verify firmware port count and Ethernet bitmap consistency, then construct per-PF eth_id lookup table. logic_eth_id is computed on-the-fly from relative PF ID instead of table lookup.
- nbl_res_ctrl_dev_vsi_info_init(): assign staggered per-PF VSI base IDs with gaps of 1024, 512 and 256 for 1, 2 and 4 port modes.
- Implement VSI/PF/Eth ID conversion helpers with strict control PF validity and range guards.
The hardware and resource layers are reached through ops tables (nbl_hw_ops_tbl, nbl_resource_ops_tbl) rather than called directly. Each has a single implementation today - leonis - and the NULL checks in nbl_hw_setup_ops() and nbl_res_setup_ops() therefore can never trigger. The tables are kept because draco is planned as the second chip
family: it has its own datapath register map and its own set of hardware helpers, while the resource logic in this commit (PF mask validation, BDF/Eth/VSI table construction, ID conversion) is what the two families share. Collapsing the indirection now would mean re-introducing it when that support lands, together with a rewrite of resource code that has already been reviewed, and the NULL checks are what keeps a partially populated table from being used in the meantime.
All resource tables are initialized once at control PF probe time and remain read-only afterwards, so lookup helpers require no internal locking. Upper-layer serialization via mutex and non-control PF mailbox RPC routing will be introduced in later series patches.
All resource memory allocations use devm managed semantics, requiring no explicit cleanup in the remove path.
--- .../net/ethernet/nebula-matrix/nbl/Makefile | 2 + .../nebula-matrix/nbl/nbl_common/nbl_common.c | 22 ++ .../net/ethernet/nebula-matrix/nbl/nbl_core.h | 2 + .../nbl/nbl_hw/nbl_hw_leonis/nbl_hw_leonis.c | 80 ++++- .../nbl/nbl_hw/nbl_hw_leonis/nbl_hw_leonis.h | 15 + .../nbl_hw_leonis/nbl_resource_leonis.c | 338 ++++++++++++++++++ .../nbl_hw_leonis/nbl_resource_leonis.h | 10 + .../nebula-matrix/nbl/nbl_hw/nbl_hw_reg.h | 1 + .../nebula-matrix/nbl/nbl_hw/nbl_resource.c | 125 +++++++ .../nebula-matrix/nbl/nbl_hw/nbl_resource.h | 69 ++++ .../nbl/nbl_include/nbl_def_channel.h | 10 + .../nbl/nbl_include/nbl_def_common.h | 20 ++ .../nbl/nbl_include/nbl_def_hw.h | 26 ++ .../nbl/nbl_include/nbl_def_resource.h | 29 ++ .../nbl/nbl_include/nbl_include.h | 6 + .../net/ethernet/nebula-matrix/nbl/nbl_main.c | 9 + 16 files changed, 762 insertions(+), 2 deletions(-) create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_resource_leonis.c create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_resource_leonis.h create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_resource.c create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_resource.h create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_include/nbl_def_resource.h
drivers/net/ethernet/nebula-matrix/nbl/Makefile +2 -0
@@ -6,4 +6,6 @@ obj-$(CONFIG_NBL) := nbl.o nbl-objs += nbl_common/nbl_common.o \ nbl_channel/nbl_channel.o \ nbl_hw/nbl_hw_leonis/nbl_hw_leonis.o \ + nbl_hw/nbl_hw_leonis/nbl_resource_leonis.o \ + nbl_hw/nbl_resource.o \ nbl_main.o
drivers/net/ethernet/nebula-matrix/nbl/nbl_common/nbl_common.c +22 -0
@@ -27,3 +27,25 @@ int nbl_common_create_wq(struct nbl_common_info *common)return 0 ; } + +/** +* nbl_common_func_id_to_rel_pf_id - convert absolute PF id to relative PF id +* @common: common device info +* @pf_id: absolute PF identifier +* @rel_pf_id: output relative pf id +* +* Leonis uses fixed mgt_pf = 0. Support future non-zero management PF. +* +* Return: 0 on success, -EINVAL on invalid arguments. +*/ +int nbl_common_func_id_to_rel_pf_id (struct nbl_common_info *common,u32 pf_id, +u32 *rel_pf_id) +{ +if (!rel_pf_id) +return -EINVAL ; + +if (pf_id < common->mgt_pf) +return -EINVAL ; + *rel_pf_id = pf_id - common->mgt_pf; +return 0 ; +}
drivers/net/ethernet/nebula-matrix/nbl/nbl_core.h +2 -0
@@ -16,11 +16,13 @@ enum {struct nbl_interface {struct nbl_hw_ops_tbl *hw_ops_tbl; +struct nbl_resource_ops_tbl *resource_ops_tbl;struct nbl_channel_ops_tbl *channel_ops_tbl; };struct nbl_core {struct nbl_hw_mgt *hw_mgt; +struct nbl_resource_mgt *res_mgt;struct nbl_channel_mgt *chan_mgt; };
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_hw_leonis.c +78 -2
115 lines not on this page · raw message
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_hw_leonis.h +15 -0
@@ -51,6 +51,21 @@ struct nbl_mailbox_qinfo_cfg_table {/* -------- HOST_PCIE -------- */ #define NBL_PCIE_HOST_K_PF_MASK_REG (NBL_INTF_HOST_PCIE_BASE + 0x00001004) +#define NBL_PCIE_HOST_TL_CFG_BUSDEV (NBL_INTF_HOST_PCIE_BASE + 0x11040) + +#define NBL_PCIE_BUS_MASK GENMASK(12, 5) +#define NBL_FW_BOARD_CONFIG 0x200 +#define NBL_FW_BOARD_DW3_OFFSET (NBL_FW_BOARD_CONFIG + 12) +#define NBL_FW_BOARD_DW6_OFFSET (NBL_FW_BOARD_CONFIG + 24) + +#define NBL_FW_BOARD_DW3_PORT_TYPE_MASK BIT(0) +#define NBL_FW_BOARD_DW3_PORT_NUM_MASK GENMASK(7, 1) +#define NBL_FW_BOARD_DW3_PORT_SPEED_MASK GENMASK(9, 8) +#define NBL_FW_BOARD_DW3_GPIO_TYPE_MASK GENMASK(12, 10) +#define NBL_FW_BOARD_DW3_P4_VERSION_MASK GENMASK(13, 13) + +#define NBL_FW_BOARD_DW6_LANE_BITMAP_MASK GENMASK(7, 0) +#define NBL_FW_BOARD_DW6_ETH_BITMAP_MASK GENMASK(15, 8)#define NBL_BAR2_MAX_LEN 0x300#endif
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_resource_leonis.c new file +338 -0
338 lines not on this page · raw message
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_resource_leonis.h new file +10 -0
@@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (c) 2026 Nebula Matrix Limited. +*/ + +#ifndef _NBL_RESOURCE_LEONIS_H_ +#define _NBL_RESOURCE_LEONIS_H_ + +#include "../nbl_resource.h" +#endif
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_reg.h +1 -0
@@ -17,6 +17,7 @@#define NBL_MAILBOX_BAR 2#define NBL_RDMA_NOTIFY_LEN (8ULL << 10)#define NBL_REG_NET_ONLY_LEN (8ULL << 10) +#define NBL_HW_DUMMY_REG 0x1300904/* * PCI MEMORY BAR total size: 64MiB. */
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_resource.c new file +125 -0
125 lines not on this page · raw message
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_resource.h new file +69 -0
69 lines not on this page · raw message
drivers/net/ethernet/nebula-matrix/nbl/nbl_include/nbl_def_channel.h +10 -0
@@ -39,6 +39,16 @@ enum nbl_chan_state {NBL_CHAN_STATE_NBITS }; +struct nbl_board_port_info { +u8 eth_num; +u8 eth_speed; +u8 p4_version; +u8 rsv[5 ]; +}; + +static_assert (sizeof (struct nbl_board_port_info ) ==8 , +"nbl_board_port_info size must be 8 bytes" ); +struct nbl_chan_send_info {void *arg;size_t arg_len;
drivers/net/ethernet/nebula-matrix/nbl/nbl_include/nbl_def_common.h +20 -0
@@ -11,6 +11,22 @@#include <linux/device.h> #include "nbl_include.h" +#define NBL_TWO_ETHERNET_PORT 2 +#define NBL_FOUR_ETHERNET_PORT 4 +#define NBL_DEFAULT_VSI_ID_GAP 1024 +#define NBL_TWO_ETHERNET_VSI_ID_GAP 512 +#define NBL_FOUR_ETHERNET_VSI_ID_GAP 256 + +static inline u32 nbl_vsi_id_gap (u32 m) +{ +if (m ==NBL_FOUR_ETHERNET_PORT ) +return NBL_FOUR_ETHERNET_VSI_ID_GAP ; +else if (m ==NBL_TWO_ETHERNET_PORT ) +return NBL_TWO_ETHERNET_VSI_ID_GAP ; + +return NBL_DEFAULT_VSI_ID_GAP ; +} +struct nbl_common_info {struct workqueue_struct *wq;struct pci_dev *pdev;
@@ -24,11 +40,15 @@ struct nbl_common_info {u8 devid;u8 bus;u8 hw_bus; +u16 mgt_pf;u8 has_ctrl;u8 has_net; + u8 max_pf; };void nbl_common_destroy_wq (struct nbl_common_info *common);int nbl_common_create_wq (struct nbl_common_info *common); +int nbl_common_func_id_to_rel_pf_id (struct nbl_common_info *common,u32 pf_id, +u32 *rel_pf_id);#endif
drivers/net/ethernet/nebula-matrix/nbl/nbl_include/nbl_def_hw.h +26 -0
@@ -8,9 +8,22 @@#include <linux/types.h> +struct nbl_board_port_info ;struct nbl_hw_mgt ;struct nbl_adapter ;struct nbl_hw_ops { +/** +* flush_write - Flush posted MEMORY-BAR writes +* @hw_mgt: hardware management context +* +* Only valid on the control PF (common->has_ctrl != 0). The +* register read back to order the writes, NBL_HW_DUMMY_REG +* (0x1300904), is covered by the ~64 MiB MEMORY-BAR mapping the +* control PF gets, but sits outside the NBL_REG_NET_ONLY_LEN +* (8 KiB) mapping a net-only PF gets. The leonis implementation +* enforces this with a WARN_ON_ONCE(). +*/ +void (*flush_write)(struct nbl_hw_mgt *hw_mgt);void (*update_mailbox_queue_tail_ptr)(struct nbl_hw_mgt *hw_mgt,u16 tail_ptr,u8 txrx); void (*config_mailbox_rxq)(struct nbl_hw_mgt *hw_mgt,
@@ -44,9 +57,22 @@ struct nbl_hw_ops {* PFs starting at PF0. */ void (*get_host_pf_mask)(struct nbl_hw_mgt *hw_mgt,u32 *pf_mask); +void (*get_real_bus)(struct nbl_hw_mgt *hw_mgt,u8 *bus);void (*cfg_mailbox_qinfo)(struct nbl_hw_mgt *hw_mgt,u16 func_id,u8 bus,u8 devid,u8 function); +void (*get_fw_eth_map)(struct nbl_hw_mgt *hw_mgt,u32 *eth_map); +/** +* get_board_info - Fetch board info from firmware +* @hw_mgt: hardware management context +* @board_info: output pointer for board info structure +* +* Firmware contract: board_info.eth_num MUST equal the number of +* unmasked PFs from get_host_pf_mask(). See get_host_pf_mask for +* details. +*/ +void (*get_board_info)(struct nbl_hw_mgt *hw_mgt, +struct nbl_board_port_info *board_info); };struct nbl_hw_ops_tbl {
drivers/net/ethernet/nebula-matrix/nbl/nbl_include/nbl_def_resource.h new file +29 -0
@@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (c) 2026 Nebula Matrix Limited. +*/ + +#ifndef _NBL_DEF_RESOURCE_H_ +#define _NBL_DEF_RESOURCE_H_ + +#include <linux/types.h> + +struct nbl_resource_mgt ; +struct nbl_adapter ; + +struct nbl_resource_ops { +int (*get_vsi_id)(struct nbl_resource_mgt *res_mgt,u16 func_id, +u16 type,u16 *vsi_id); +int (*get_eth_id)(struct nbl_resource_mgt *res_mgt,u16 func_id, +u16 vsi_id,u8 *eth_num,u8 *eth_id, +u8 *logic_eth_id); +}; + +struct nbl_resource_ops_tbl { +struct nbl_resource_ops *ops; +struct nbl_resource_mgt *priv; +}; + +int nbl_res_init_leonis (struct nbl_adapter *adapter); +void nbl_res_remove_leonis (struct nbl_adapter *adapter); +#endif
drivers/net/ethernet/nebula-matrix/nbl/nbl_include/nbl_include.h +6 -0
@@ -13,6 +13,12 @@#define NBL_MAX_PF 8#define NBL_NEXT_ID (id, max) (((id) + 1) % ((max) + 1)) +#define NBL_MAX_ETHERNET 4 + +enum { +NBL_VSI_DATA =0 , +}; +struct nbl_func_caps {u32 has_ctrl:1 ;u32 has_net:1 ;
drivers/net/ethernet/nebula-matrix/nbl/nbl_main.c +9 -0
@@ -10,6 +10,7 @@#include "nbl_include/nbl_include.h" #include "nbl_include/nbl_def_channel.h" #include "nbl_include/nbl_def_hw.h" +#include "nbl_include/nbl_def_resource.h" #include "nbl_include/nbl_def_common.h" #include "nbl_core.h"
@@ -27,6 +28,7 @@ struct nbl_adapter *nbl_core_init(struct pci_dev *pdev, adapter->pdev = pdev; common = &adapter->common; + common->mgt_pf =0 ; common->pdev = pdev; common->dev = &pdev->dev; common->has_ctrl = param->caps.has_ctrl;
@@ -42,7 +44,13 @@ struct nbl_adapter *nbl_core_init(struct pci_dev *pdev, ret =nbl_chan_init_common (adapter);if (ret) goto out_hw_remove; + + ret =nbl_res_init_leonis (adapter); +if (ret) + goto out_chan_remove;return adapter; +out_chan_remove: +nbl_chan_remove_common (adapter); out_hw_remove:nbl_hw_remove_leonis (adapter); out:
@@ -51,6 +59,7 @@ out:void nbl_core_remove (struct nbl_adapter *adapter) { +nbl_res_remove_leonis (adapter);nbl_chan_remove_common (adapter);nbl_hw_remove_leonis (adapter); }
Signature · 1 line
2.51.0.windows.1
[email protected]
AI reviewRe: [PATCH v34 net-next 2/8] net/nebula-matrix: add common resource implementation
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
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] nbl_res_ctrl_dev_sriov_info_init() builds the per-PF BDF table from two sources.
- [Low] The commit message says this patch manages 'SR-IOV BDF', and the new table is named struct nbl_sriov_info and filled by…
- [Low] net/nebula-matrix: single-implementation ops tables and dead NULL checks
--
net/nebula-matrix: add common resource implementation
This commit adds a chip-agnostic resource layer for the Nebula Matrix nbl driver. At probe time on the control PF, it validates the firmware PF mask and builds the per-PF BDF, Ethernet port and VSI base id tables. It also adds VSI/PF/Eth ID conversion helpers.
Quoted 6 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_resource_leonis.c new file +0
-0@@ -0,0 +1,338 @@
[ ... ]
Quoted 12 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_resource_leonis.c new file +12
-0+static int nbl_res_ctrl_dev_sriov_info_init (struct nbl_resource_mgt *res_mgt) +{ +struct nbl_hw_ops *hw_ops = res_mgt->hw_ops_tbl->ops; +struct nbl_hw_mgt *p = res_mgt->hw_ops_tbl->priv; +struct nbl_common_info *common = res_mgt->common; +struct nbl_sriov_info *sriov_info; +struct device *dev = common->dev; +u8 hw_bus =0 ; +u16 function; +u16 func_id; + + hw_ops->get_real_bus (p, &hw_bus);
[ ... ]
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_resource_leonis.c line 154
Quoted 7 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_resource_leonis.c new file +7
-0+ common->hw_bus = hw_bus; + +for (func_id =0 ; func_id < common->max_pf; func_id++) { + sriov_info = res_mgt->resource_info->sriov_info + func_id; + function = common->function + func_id; + sriov_info->bdf =PCI_DEVID (common->hw_bus, +PCI_DEVFN (common->devid, function));
[Severity: Medium]
Can this BDF fail to match any real requester ID when the OS view of the topology is different from the physical one?
The bus number is the device's own captured value, but the slot number comes from the OS view:
nbl_hw_get_real_bus() {
nbl_hw_rd_regs_lock(hw_mgt, NBL_PCIE_HOST_TL_CFG_BUSDEV, &data,
sizeof(data));
*bus = FIELD_GET(NBL_PCIE_BUS_MASK, data);
}nbl_core_init() {
...
common->devid = PCI_SLOT(pdev->devfn);
...
}Reading the bus from hardware instead of using pdev->bus->number only matters when the two can differ, for example when PF0 is passed through to a VM. In that case pdev->devfn is also the guest's view. The guest slot is often non-zero, while a physical endpoint under a root port is device 0.
NBL_PCIE_BUS_MASK is GENMASK(12, 5), so bits [4:0] of TL_CFG_BUSDEV are not used. Do those bits hold the captured device number? If so, should that value be used here instead of common->devid?
Later patches in the series program this combination into hardware:nbl_res_func_id_to_bdf() passes sriov_info->bdf to cfg_msix_info(). nbl_res_intr_cfg_msix_map() passes common->hw_bus, common->devid and common->function to cfg_msix_map(). nbl_dev_setup_chan_qinfo() passes common->hw_bus and common->devid to cfg_chan_qinfo_map_table().
With a mismatched device number, would MSI-X delivery and mailbox routing be misrouted on such a setup?
The function number looks fine. has_ctrl is only set when PCI_FUNC(pdev->devfn) == 0, so common->function + func_id equals the absolute PF id.
Quoted 4 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_resource_leonis.c new file +4
-0+ } + +return 0 ; +}
[ ... ]
Quoted 6 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_resource.h new file +0
-0@@ -0,0 +1,69 @@
[ ... ]
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_resource.h line 23
Quoted 4 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_resource.h new file +4
-0+/* --------- INFO ---------- */ +struct nbl_sriov_info { +unsigned int bdf; +};
[Severity: Low]
This isn't a bug, but the naming doesn't match the contents. The commit message says the layer manages "SR-IOV BDF". The table is named struct nbl_sriov_info and is filled by nbl_res_ctrl_dev_sriov_info_init(). It only holds one PCI_DEVID per physical PF, since the loop runs func_id from 0 to common->max_pf and VFs are never included.
Would a PF-specific name, and matching commit message wording, describe this table better?
Also, nothing in this commit reads sriov_info or common->hw_bus. The later patch "net/nebula-matrix: add intr resource implementation" adds a reader, nbl_res_func_id_to_bdf(). The SR-IOV naming is still there at the end of the series.
[ ... ]
Quoted 329 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_resource.h new file +328
-0@@ -0,0 +1,338 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (c) 2026 Nebula Matrix Limited. +*/ +#include <linux/device.h> +#include <linux/pci.h> +#include <linux/bits.h> +#include "nbl_resource_leonis.h" + +static struct nbl_resource_ops res_ops = { + .get_vsi_id = nbl_res_func_id_to_vsi_id, + .get_eth_id = nbl_res_get_eth_id, +}; + +static struct nbl_resource_mgt * +nbl_res_setup_res_mgt (struct nbl_common_info *common) +{ +struct nbl_resource_info *resource_info; +struct nbl_resource_mgt *res_mgt; +struct device *dev = common->dev; + + res_mgt =devm_kzalloc (dev,sizeof (*res_mgt),GFP_KERNEL ); +if (!res_mgt) +return ERR_PTR (-ENOMEM ); + res_mgt->common = common; + + resource_info = +devm_kzalloc (dev,sizeof (*resource_info),GFP_KERNEL ); +if (!resource_info) +return ERR_PTR (-ENOMEM ); + res_mgt->resource_info = resource_info; + +return res_mgt; +} + +static struct nbl_resource_ops_tbl * +nbl_res_setup_ops (struct device *dev,struct nbl_resource_mgt *res_mgt) +{ +struct nbl_resource_ops_tbl *res_ops_tbl; + + res_ops_tbl =devm_kzalloc (dev,sizeof (*res_ops_tbl),GFP_KERNEL ); +if (!res_ops_tbl) +return ERR_PTR (-ENOMEM ); +if (!res_ops.get_vsi_id || !res_ops.get_eth_id) +return ERR_PTR (-EINVAL ); + res_ops_tbl->ops = &res_ops; + res_ops_tbl->priv = res_mgt; + +return res_ops_tbl; +} + +static int nbl_res_ctrl_dev_setup_eth_info (struct nbl_resource_mgt *res_mgt) +{ +struct nbl_hw_ops *hw_ops = res_mgt->hw_ops_tbl->ops; +struct device *dev = res_mgt->common->dev; +struct nbl_eth_info *eth_info; +u32 eth_bitmap =0 ; +u32 eth_num =0 ; +u32 fw_port_num; +int i; + + eth_info =devm_kzalloc (dev,sizeof (*eth_info),GFP_KERNEL ); +if (!eth_info) +return -ENOMEM ; + + res_mgt->resource_info->eth_info = eth_info; + + fw_port_num = res_mgt->resource_info->board_info.eth_num; + + hw_ops->get_fw_eth_map (res_mgt->hw_ops_tbl->priv, ð_bitmap); +if (eth_bitmap & ~((1 <<NBL_MAX_ETHERNET ) -1 )) { +dev_err (dev,"FW reported invalid eth_bitmap 0x%x\n" , + eth_bitmap); +return -EINVAL ; + } +if (fw_port_num !=hweight32 (eth_bitmap)) { +dev_err (dev,"FW inconsistency: port_num=%u, bitmap=0x%x\n" , + fw_port_num, eth_bitmap); +return -EINVAL ; + } +/* +* Firmware is ready before probe. Valid port counts are 1/2/4; +* 0 (invalid config), 3 (unsupported topology), and >4 (exceeds +* hardware max) are all rejected with -EINVAL. +*/ +if (fw_port_num ==0 || fw_port_num ==3 || + fw_port_num >NBL_MAX_ETHERNET ) { +dev_err (dev,"FW reports %u Ethernet ports, unsupported (valid: 1/2/4)\n" , + fw_port_num); +return -EINVAL ; + } + eth_info->eth_num = fw_port_num; +/* Intentional design constraint: each PF maps to exactly one +* Ethernet port. This couples PF identity to port identity +* and is required by nbl_res_get_eth_id() which indexes +* eth_info->eth_id[] by relative PF id. +*/ +if (res_mgt->common->max_pf != eth_info->eth_num) { +dev_err (dev,"Invalid PF-to-port topology: max_pf=%u, eth_num=%u\n" , + res_mgt->common->max_pf, eth_info->eth_num); +return -EINVAL ; + } + +/* +* Any subset of valid bitmap bits is accepted (e.g. 0/1, 0/2, +* 1/3, etc.). Firmware only needs to report the correct count +* of active ports; no hard-coded fixed bit positions required. +* eth_id[] is filled in ascending bitmap-bit order, so the Nth +* relative PF owns the Nth logical port (max_pf == eth_num is +* enforced above). +*/ +for (i =0 ; i <NBL_MAX_ETHERNET ; i++) { +if ((1 << i) & eth_bitmap) { + eth_info->eth_id[eth_num] = i; + eth_num++; + } + } + +return 0 ; +} + +static int nbl_res_ctrl_dev_sriov_info_init (struct nbl_resource_mgt *res_mgt) +{ +struct nbl_hw_ops *hw_ops = res_mgt->hw_ops_tbl->ops; +struct nbl_hw_mgt *p = res_mgt->hw_ops_tbl->priv; +struct nbl_common_info *common = res_mgt->common; +struct nbl_sriov_info *sriov_info; +struct device *dev = common->dev; +u8 hw_bus =0 ; +u16 function; +u16 func_id; + + hw_ops->get_real_bus (p, &hw_bus); +if (common->function + common->max_pf >NBL_MAX_PF ) { +dev_err (dev,"PF count exceeds available function space\n" ); +return -EINVAL ; + } + sriov_info =devm_kcalloc (dev, common->max_pf, +sizeof (*sriov_info),GFP_KERNEL ); +if (!sriov_info) +return -ENOMEM ; + + res_mgt->resource_info->sriov_info = sriov_info; +/* +* Real bus number of the control PF; +* the BDF table below is built from it. +*/ + common->hw_bus = hw_bus; + +for (func_id =0 ; func_id < common->max_pf; func_id++) { + sriov_info = res_mgt->resource_info->sriov_info + func_id; + function = common->function + func_id; + sriov_info->bdf =PCI_DEVID (common->hw_bus, +PCI_DEVFN (common->devid, function)); + } + +return 0 ; +} + +static int nbl_res_ctrl_dev_vsi_info_init (struct nbl_resource_mgt *res_mgt) +{ +struct nbl_eth_info *eth_info = res_mgt->resource_info->eth_info; +struct nbl_common_info *common = res_mgt->common; +struct device *dev = common->dev; +struct nbl_vsi_info *vsi_info; +int i; + + vsi_info =devm_kzalloc (dev,sizeof (*vsi_info),GFP_KERNEL ); +if (!vsi_info) +return -ENOMEM ; + + res_mgt->resource_info->vsi_info = vsi_info; +/* +* case 1 one port(1pf) +* pf0 (NBL_VSI_SERV_PF_DATA_TYPE) vsi is 0 +* case 2 two port(2pf) +* pf0,pf1(NBL_VSI_SERV_PF_DATA_TYPE) vsi is 0,512 +* case 3 four port(4pf) +* pf0,pf1,pf2,pf3(NBL_VSI_SERV_PF_DATA_TYPE) vsi is 0,256,512,768 +*/ + + vsi_info->num = eth_info->eth_num; +/* +* eth_num can be 1/2/4: +* - 2/4 ports use dedicated gap constants; +* - 1 port falls back to NBL_DEFAULT_VSI_ID_GAP (1024). +* All three values produce valid base_id offsets. +*/ +for (i =0 ; i < vsi_info->num; i++) { + vsi_info->serv_info[i][NBL_VSI_SERV_PF_DATA_TYPE ].base_id = + i *nbl_vsi_id_gap (vsi_info->num); + vsi_info->serv_info[i][NBL_VSI_SERV_PF_DATA_TYPE ].num =1 ; + } + +return 0 ; +} + +static int nbl_res_init_pf_num (struct nbl_resource_mgt *res_mgt) +{ +struct nbl_hw_ops *hw_ops = res_mgt->hw_ops_tbl->ops; +u32 exp_contiguous_mask =0 ; +u32 pf_mask =0 ; +u32 pf_num =0 ; +int i; + + hw_ops->get_host_pf_mask (res_mgt->hw_ops_tbl->priv, &pf_mask); + +/* +* k_pf_mask register rule: +* bit N == 0 -> PF#N enabled; bit N == 1 -> PF#N masked out. +* Hardware constraint: bit0 is PF0's mask bit; driver requires +* PF0 enabled as management PF, so bit0 must be clear. +* All-zero pf_mask means all PF0~PF7 are enabled, which is unsupported +* by the driver +* +* Product firmware constraint: only 3 valid configurations supported: +* 1 PF (PF0 only): pf_num = 1, mask = 0xfe +* 2 PFs (PF0,PF1): pf_num = 2, mask = 0xfc +* 4 PFs (PF0~PF3): pf_num = 4, mask = 0xf0 +* No other PF count or sparse/non-contiguous PF layout is allowed. +* +* This is the only validation of the mask in the driver. The channel +* layer reads the same raw register value to program the mailbox +* QINFO table and assumes the layout checked here, so it depends on +* this running first. What actually orders the two is not layer +* setup - nbl_chan_init_common() runs before nbl_res_init_leonis() - +* but the QINFO programming point: it is reached from nbl_dev_init() +* via nbl_dev_setup_chan_qinfo(), long after resource init has +* finished, so the mask has already been validated by then. +*/ +for (i =0 ; i <NBL_MAX_PF ; i++) { +if (!(pf_mask & (1 << i))) + pf_num++; + } + +/* +* Sanity check: enabled PFs must be contiguous starting from PF0. +* Current resource framework uses relative PF id, sparse PF layout +* will cause mismatch between resource layer and hardware func_id. +*/ +for (i =0 ; i < pf_num; i++) + exp_contiguous_mask |=BIT (i); +if ((pf_mask & exp_contiguous_mask) !=0 ) { +dev_err (res_mgt->common->dev, +"pf_mask 0x%08x: non-contiguous enabled PF, unsupported\n" , + pf_mask); +return -EINVAL ; + } + +/* Only allow product-specified PF count: 1 / 2 / 4 */ +if (pf_num !=1 && pf_num !=2 && pf_num !=4 ) { +dev_err (res_mgt->common->dev, +"Invalid pf_num=%u (mask=0x%08x), only 1/2/4 PFs supported\n" , + pf_num, pf_mask); +return -EINVAL ; + } + + res_mgt->common->max_pf = pf_num; + +return 0 ; +} + +static void nbl_res_init_board_info (struct nbl_resource_mgt *res_mgt) +{ +struct nbl_hw_ops *hw_ops = res_mgt->hw_ops_tbl->ops; + + hw_ops->get_board_info (res_mgt->hw_ops_tbl->priv, + &res_mgt->resource_info->board_info); +} + +static int nbl_res_start (struct nbl_resource_mgt *res_mgt) +{ +struct nbl_common_info *common = res_mgt->common; +int ret =0 ; + +if (common->has_ctrl) { +nbl_res_init_board_info (res_mgt); + + ret =nbl_res_init_pf_num (res_mgt); +if (ret) +return ret; + + ret =nbl_res_ctrl_dev_sriov_info_init (res_mgt); +if (ret) +return ret; + + ret =nbl_res_ctrl_dev_setup_eth_info (res_mgt); +if (ret) +return ret; + + ret =nbl_res_ctrl_dev_vsi_info_init (res_mgt); +if (ret) +return ret; + } + +return 0 ; +} + +int nbl_res_init_leonis (struct nbl_adapter *adap) +{ +struct nbl_channel_ops_tbl *chan_ops_tbl = adap->intf.channel_ops_tbl; +struct nbl_hw_ops_tbl *hw_ops_tbl = adap->intf.hw_ops_tbl; +struct nbl_common_info *common = &adap->common; +struct nbl_resource_ops_tbl *res_ops_tbl; +struct device *dev = &adap->pdev->dev; +struct nbl_resource_mgt *res_mgt; +int ret; + + res_mgt =nbl_res_setup_res_mgt (common); +if (IS_ERR (res_mgt)) { + ret =PTR_ERR (res_mgt); +return ret; + } + res_mgt->chan_ops_tbl = chan_ops_tbl; + res_mgt->hw_ops_tbl = hw_ops_tbl; + + ret =nbl_res_start (res_mgt); +if (ret) +return ret; + + res_ops_tbl =nbl_res_setup_ops (dev, res_mgt); +if (IS_ERR (res_ops_tbl)) { + ret =PTR_ERR (res_ops_tbl); +return ret; + } + adap->intf.resource_ops_tbl = res_ops_tbl; + adap->core.res_mgt = res_mgt; +
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_resource.h line 338
Quoted 10 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_resource.h new file +10
-0+return 0 ; +} + +void nbl_res_remove_leonis (struct nbl_adapter *adap) +{ +/* +* No resource release here because all memory uses devm managed +* allocation +*/ +}
[Severity: Low]
This isn't a bug, but this looks like the indirection that was asked to be removed on v22:
https://lore.kernel.org/r/[email protected]
struct nbl_resource_ops has exactly one instance, res_ops in nbl_resource_leonis.c, and no later patch in the series adds a second one. Both of its members are chip-agnostic helpers in nbl_resource.c:nbl_res_func_id_to_vsi_id() and nbl_res_get_eth_id(). The commit message says leonis and draco will share this resource logic. If that is the plan, why does it need a per-chip ops table at all? Could the callers call the shared helpers directly?
The NULL checks in nbl_res_setup_ops() and nbl_hw_setup_ops() test members of static tables that are initialized a few lines above in the same file. The commit message already says they can never trigger. A future draco table would be a separate object with its own setup function, so these checks would not protect it either. Is there a reason to keep them, and to keep the devm_kzalloc() of the ops_tbl wrapper that comes with them?
Also, res_mgt->chan_ops_tbl is assigned in nbl_res_init_leonis(), but nothing reads it in this patch or in the rest of the series. Could it be added in the patch that first uses it?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261112094925.6098-1-illusion.wang%40nebula-matrix.com
illusion.wang
maintainerPATCH v34 3/8 net/nebula-matrix: add intr resource implementation
In reply to illusion.wangFrom: illusion wang <[email protected]>
Add dedicated nbl_interrupt module to manage chip-internal MSI-X interrupt resource and hardware mapping for the Nebula Matrix Ethernet driver.
This module manages the driver-wide global hardware MSI-X vector index space, split into independent network and control interrupt bitmaps (intr_net_bmap/intr_other_bmap), and handles programming of the chip-internal MSI-X mapping registers. It explicitly does not manage physical PCI MSI-X entries; physical vector allocation via pci_alloc_irq_vectors() will be implemented in a follow-up patch via nbl_dev_init_interrupt_scheme().
The published manager is control-PF only: the resource ops reject calls when common->has_ctrl is not set.
Core functional interfaces:
1. cfg_msix_map: Allocate global hardware MSI-X vectors from the separate net/other interrupt bitmaps. The per-function coherent DMA map table is allocated once and reused by every later reconfiguration, so a reconfiguration does not free and reallocate it. It does still take the destroy-path quiesce sequence before the old vectors are recycled
- disable mailbox routing, invalidate the per-vector INFO entries, clear map VALID, flush, wait for in-flight table fetches - because the old table is what the pcompleter may still be reading; only the DMA buffer itself survives. Only tear down the old hardware state after the new allocation succeeds, to avoid interrupt loss, and program the MSI-X table DMA address plus the control-PF BDF into NBL_PCOMPLETER_FUNCTION_MSIX_MAP. No extra invalidate or quiesce window is needed before the first configuration either: every map entry is invalid at that point, since nbl_intr_mgt_start() scrubbed them all and nothing arms one before cfg_msix_map does.
2. destroy_msix_map: Recycle global vector indices, clear hardware MSI-X mappings, and release the descriptor resources. Implements two-stage
teardown: disable mailbox IRQ routing first, mask vectors, clear hardware VALID bits while retaining the live DMA addresses, take a bounded best-effort ~1ms hardware DMA quiescence window, then zero the table entries and release the vector bitmap and interrupt buffer. The coherent map table itself is deliberately not freed: a sibling PF can reach this teardown at runtime while the control PF is still bus master, the table fetches carry the control PF's BDF, and the quiesce window is best-effort, so a late fetch could read reused memory. The table is kept for the function's lifetime, reused by a later configuration, and released only by nbl_intr_mgt_stop().
3. set_mailbox_irq: Toggle PF-specific mailbox MSI-X routing by updating NBL_MAILBOX_QINFO_MAP_REG_ARR. The disable path works without a configured MSI-X map, enabling safe routing cleanup before vector release.
4. cfg_msix_info: Program PADPT_HOST_MSIX_INFO and PCOMPLETER_HOST_MSIX_FID_TABLE with the strict hardware-defined programming order (forward for enable, reverse for teardown) to avoid inconsistent hardware state.
Key design & safety features:
- Self-contained intr_mgt->lock protects the global bitmaps and the per-function state; all public APIs hold the lock internally, so callers need no upper-layer locking.
- Global cleanup via nbl_intr_mgt_stop(): under the lock it latches the manager stopping and invalidates the hardware map entry of every function still left CONFIGURED, then takes a single global quiesce window before the coherent tables and vector indices are released. Remote PF maps are created via mailbox RPC in later patches; the cleanup itself uses local MMIO writes.
- Only PFs are supported: every op rejects a func_id outside the PF range (a VF id) with -EINVAL, and cfg_msix_map additionally rejects a func_id that maps to a PF which is not present on this board with -EOPNOTSUPP, via nbl_res_func_id_to_bdf().
- FUNCTION_MSIX_MAP survives a kexec or a forced unload without FLR and there is no .shutdown callback to scrub it, so nbl_intr_mgt_start() invalidates all NBL_MAX_PF map entries once, followed by one flush and one quiesce window, before any function is configured. nbl_probe() now leaves bus mastering disabled until nbl_core_init() has returned, so this scrub runs before the device can issue a single fetch against a stale VALID entry - those fetches are bus-master reads, and a stale entry points at memory this kernel may already have reallocated. Everything up to the scrub is host-side setup plus MMIO and does not wait for the device, so keeping bus mastering off until nbl_core_init() has returned costs nothing on the probe path.
Known limitation - MSI-X map table DMA quiescence:
The chip exposes no idle or completion status for a pcompleter fetch of a map table, so the only ordering available anywhere in this module is "clear VALID -> flush_write() -> bounded delay". The delay is sized well above the tables' worst-case fetch/response latency, but it is a best-effort approximation and not a guarantee, which is why nothing in this module relies on it to make coherent memory freeable: a per-function destroy keeps its table. What the window does protect is the vector set that a reconfiguration hands to another function.
The tables are released only by nbl_intr_mgt_stop(), after one global window, and nbl_remove() clears Bus Master Enable before nbl_core_remove() reaches it, so the device cannot start a new fetch and the residual window covers in-flight requests only. Clearing bus mastering cannot help a sibling PF's map destroyed at runtime - those fetches are tagged with the control PF's BDF - which is exactly why the table is retained there.
Instantiate the interrupt manager via nbl_intr_mgt_start() during device resource initialization, and attach it to the resource management context.
Add corresponding hardware register definitions, helper functions, resource ops callbacks, and Makefile entries to wire up the new module. The new resource ops (cfg_msix_map, destroy_msix_map, set_mailbox_irq) are hooked into resource_ops but have no in-tree callers within this patch; invocation will be added in subsequent patches in this series.
--- .../net/ethernet/nebula-matrix/nbl/Makefile | 1 + .../nbl/nbl_hw/nbl_hw_leonis/nbl_hw_leonis.c | 164 +++- .../nbl/nbl_hw/nbl_hw_leonis/nbl_hw_leonis.h | 44 + .../nbl_hw_leonis/nbl_resource_leonis.c | 38 +- .../nbl_hw_leonis/nbl_resource_leonis.h | 1 + .../nebula-matrix/nbl/nbl_hw/nbl_interrupt.c | 853 ++++++++++++++++++ .../nebula-matrix/nbl/nbl_hw/nbl_interrupt.h | 21 + .../nebula-matrix/nbl/nbl_hw/nbl_resource.c | 32 + .../nebula-matrix/nbl/nbl_hw/nbl_resource.h | 90 ++ .../nbl/nbl_include/nbl_def_hw.h | 9 + .../nbl/nbl_include/nbl_def_resource.h | 6 + .../net/ethernet/nebula-matrix/nbl/nbl_main.c | 44 +- 12 files changed, 1289 insertions(+), 14 deletions(-) create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_interrupt.c create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_interrupt.h
drivers/net/ethernet/nebula-matrix/nbl/Makefile +1 -0
@@ -8,4 +8,5 @@ nbl-objs += nbl_common/nbl_common.o \ nbl_hw/nbl_hw_leonis/nbl_hw_leonis.o \ nbl_hw/nbl_hw_leonis/nbl_resource_leonis.o \ nbl_hw/nbl_resource.o \ + nbl_hw/nbl_interrupt.o \ nbl_main.o
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_hw_leonis.c +162 -2
197 lines not on this page · raw message
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_hw_leonis.h +44 -0
50 lines not on this page · raw message
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_resource_leonis.c +35 -3
@@ -10,6 +10,9 @@static struct nbl_resource_ops res_ops = { .get_vsi_id = nbl_res_func_id_to_vsi_id, .get_eth_id = nbl_res_get_eth_id, + .cfg_msix_map = nbl_res_intr_cfg_msix_map, + .destroy_msix_map = nbl_res_intr_destroy_msix_map, + .set_mailbox_irq = nbl_res_intr_set_mailbox_irq, }; static struct nbl_resource_mgt *
@@ -41,7 +44,9 @@ nbl_res_setup_ops(struct device *dev, struct nbl_resource_mgt *res_mgt) res_ops_tbl =devm_kzalloc (dev,sizeof (*res_ops_tbl),GFP_KERNEL );if (!res_ops_tbl)return ERR_PTR (-ENOMEM ); -if (!res_ops.get_vsi_id || !res_ops.get_eth_id) +if (!res_ops.get_vsi_id || !res_ops.get_eth_id || + !res_ops.cfg_msix_map || !res_ops.destroy_msix_map || + !res_ops.set_mailbox_irq)return ERR_PTR (-EINVAL ); res_ops_tbl->ops = &res_ops; res_ops_tbl->priv = res_mgt;
@@ -291,6 +296,10 @@ static int nbl_res_start(struct nbl_resource_mgt *res_mgt) ret =nbl_res_ctrl_dev_vsi_info_init (res_mgt);if (ret)return ret; + + ret =nbl_intr_mgt_start (res_mgt); +if (ret) +return ret; }return 0 ;
@@ -331,8 +340,31 @@ int nbl_res_init_leonis(struct nbl_adapter *adap)void nbl_res_remove_leonis (struct nbl_adapter *adap) { +struct nbl_resource_mgt *res_mgt = adap->core.res_mgt; +struct nbl_common_info *common = &adap->common; + +if (!res_mgt) +return ; +/* -* No resource release here because all memory uses devm managed -* allocation +* Tear down all MSI-X maps before destroying coherent tables. +* This is critical on the control PF, whose table carries the map +* entries installed for every configured function: they must be +* invalidated in hardware before the tables backing them are +* freed, otherwise the device keeps DMA-reading released pages. +* Sibling PFs are already unbound by the device-link ordering +* so no remote PF still has a live map entry or a running +* mailbox RPC at this point. +*/ +if (common->has_ctrl && res_mgt->intr_mgt) +nbl_intr_mgt_stop (res_mgt); + +/* Note: +* per-function interrupts arrays (kcalloc) are freed by +* nbl_intr_mgt_stop(). +* MSIX coherent tables are explicitly freed by dma_free_coherent() +* inside the intr destroy path, before nbl_intr_mgt_stop() returns. +* intr_mgt itself (devm_kzalloc) is released by devres after this +* function returns */ }
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_resource_leonis.h +1 -0
@@ -7,4 +7,5 @@#define _NBL_RESOURCE_LEONIS_H_#include "../nbl_resource.h" +#include "../nbl_interrupt.h" #endif
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_interrupt.c new file +853 -0
853 lines not on this page · raw message
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_interrupt.h new file +21 -0
@@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (c) 2026 Nebula Matrix Limited. +*/ + +#ifndef _NBL_INTERRUPT_H_ +#define _NBL_INTERRUPT_H_ + +#include "nbl_resource.h" + +#define NBL_MSIX_MAP_TABLE_MAX_ENTRIES 1024 +int nbl_res_intr_destroy_msix_map (struct nbl_resource_mgt *res_mgt, +u16 func_id); +int nbl_res_intr_cfg_msix_map (struct nbl_resource_mgt *res_mgt, +u16 func_id,u16 num_net_msix, +u16 num_others_msix, +bool net_msix_mask_en); +int nbl_res_intr_set_mailbox_irq (struct nbl_resource_mgt *res_mgt, +u16 func_id,u16 vector_id, +bool en_msix); +#endif
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_resource.c +32 -0
@@ -68,6 +68,38 @@ int nbl_res_vsi_id_to_pf_id(struct nbl_resource_mgt *res_mgt, u16 vsi_id)return -ENOENT ; } +int nbl_res_func_id_to_bdf (struct nbl_resource_mgt *res_mgt,u16 func_id, +u8 *bus,u8 *dev,u8 *function) +{ +struct nbl_common_info *common = res_mgt->common; +struct nbl_sriov_info *sriov_info; +int pfid = func_id; +u8 pf_bus, devfn; +u32 rel_pf_id; +int ret; + +if (!common->has_ctrl || !bus || !dev || !function) +return -EINVAL ; + ret =nbl_common_func_id_to_rel_pf_id (common, pfid, &rel_pf_id); +if (ret) +return ret; +if (rel_pf_id >= common->max_pf) { +dev_err (common->dev, +"func_id=%u rel_pf_id=%u exceeds max_pf=%u, no such PF on this board\n" , + pfid, rel_pf_id, + common->max_pf); +return -EOPNOTSUPP ; + } + sriov_info = res_mgt->resource_info->sriov_info + rel_pf_id; + pf_bus =PCI_BUS_NUM (sriov_info->bdf); + devfn = sriov_info->bdf &0xff ; + *bus = pf_bus; + *dev =PCI_SLOT (devfn); + *function =PCI_FUNC (devfn); + +return 0 ; +} +int nbl_res_get_eth_id (struct nbl_resource_mgt *res_mgt,u16 func_id,u16 vsi_id,u8 *eth_num,u8 *eth_id,u8 *logic_eth_id) {
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_resource.h +90 -0
110 lines not on this page · raw message
drivers/net/ethernet/nebula-matrix/nbl/nbl_include/nbl_def_hw.h +9 -0
@@ -12,6 +12,13 @@ struct nbl_board_port_info;struct nbl_hw_mgt ;struct nbl_adapter ;struct nbl_hw_ops { +void (*cfg_msix_map)(struct nbl_hw_mgt *hw_mgt,u16 func_id, +bool valid,dma_addr_t dma_addr,u8 bus, +u8 devid,u8 function); +void (*cfg_msix_info)(struct nbl_hw_mgt *hw_mgt,u16 func_id, +bool valid,u16 interrupt_id,u8 bus, +u8 devid,u8 function, +bool net_msix_mask_en);/** * flush_write - Flush posted MEMORY-BAR writes * @hw_mgt: hardware management context
@@ -61,6 +68,8 @@ struct nbl_hw_ops {void (*cfg_mailbox_qinfo)(struct nbl_hw_mgt *hw_mgt,u16 func_id,u8 bus,u8 devid,u8 function); +void (*set_mailbox_irq)(struct nbl_hw_mgt *hw_mgt,u16 func_id, +bool en_msix,u16 gvec);void (*get_fw_eth_map)(struct nbl_hw_mgt *hw_mgt,u32 *eth_map);/** * get_board_info - Fetch board info from firmware
drivers/net/ethernet/nebula-matrix/nbl/nbl_include/nbl_def_resource.h +6 -0
@@ -12,6 +12,12 @@ struct nbl_resource_mgt;struct nbl_adapter ;struct nbl_resource_ops { +int (*cfg_msix_map)(struct nbl_resource_mgt *res_mgt,u16 func_id, +u16 num_net_msix,u16 num_others_msix, +bool net_msix_mask_en); +int (*destroy_msix_map)(struct nbl_resource_mgt *res_mgt,u16 func_id); +int (*set_mailbox_irq)(struct nbl_resource_mgt *res_mgt,u16 func_id, +u16 vector_id,bool en_msix);int (*get_vsi_id)(struct nbl_resource_mgt *res_mgt,u16 func_id,u16 type,u16 *vsi_id); int (*get_eth_id)(struct nbl_resource_mgt *res_mgt, u16 func_id,
drivers/net/ethernet/nebula-matrix/nbl/nbl_main.c +35 -9
68 lines not on this page · raw message
Signature · 1 line
2.51.0.windows.1
[email protected]
AI reviewRe: [PATCH v34 net-next 3/8] net/nebula-matrix: add intr resource implementation
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider.
Critical: 0 · High: 0 · Medium: 2 · 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] The error path of nbl_res_intr_cfg_msix_map() (drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_interrupt.c) frees…
- [Medium] nbl_intr_setup_mgt() (drivers/net/ethernet/nebula- matrix/nbl/nbl_hw/nbl_interrupt.c) invalidates FUNCTION_MSIX_MAP only…
- [Low] The note added to nbl_res_remove_leonis() (drivers/net/ethernet/nebula-…
- [Low] nbl_probe() (drivers/net/ethernet/nebula-matrix/nbl/nbl_main.c) and the commit message argue that the stale-FUNCTION_MSIX_MAP scrub in…
--
net/nebula-matrix: add intr resource implementation
This commit adds an nbl_interrupt module for the control PF. The module manages the chip-internal MSI-X vector bitmaps and the per-function coherent MSI-X map tables. It also adds hardware ops that program FUNCTION_MSIX_MAP, HOST_MSIX_INFO and mailbox IRQ routing. nbl_probe() now calls pci_set_master() only after nbl_core_init(), and nbl_remove() now calls pci_clear_master() before nbl_core_remove().
Quoted 4 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_resource_leonis.c +0
-0
[ ... ]
Quoted 4 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_resource_leonis.c +0
-0@@ -331,8 +340,31 @@ int nbl_res_init_leonis(struct nbl_adapter *adap)void nbl_res_remove_leonis (struct nbl_adapter *adap) {
[ ... ]
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_resource_leonis.c int nbl_res_init_leonis(struct nbl_adapter *adap) · line 370
Quoted 12 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_resource_leonis.c +10
-0+if (common->has_ctrl && res_mgt->intr_mgt) +nbl_intr_mgt_stop (res_mgt); + +/* Note: +* per-function interrupts arrays (kcalloc) are freed by +* nbl_intr_mgt_stop(). +* MSIX coherent tables are explicitly freed by dma_free_coherent() +* inside the intr destroy path, before nbl_intr_mgt_stop() returns. +* intr_mgt itself (devm_kzalloc) is released by devres after this +* function returns */ }
[Severity: Low]
Is this note accurate? Elsewhere in the patch, the "destroy path" means nbl_res_intr_destroy_msix_map()->__nbl_res_intr_complete_destroy_msix_map(). That path does not free the table on purpose:
/* * The coherent table is kept, not freed here.
The dma_free_coherent() calls are actually in a separate loop at the end of nbl_intr_mgt_stop(). The commit message also says the table is "released only by nbl_intr_mgt_stop()".
Could the comment name nbl_intr_mgt_stop() directly, so it isn't read as saying destroy_msix_map frees the DMA memory?
Quoted 6 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_interrupt.c new file +0
-0@@ -0,0 +1,853 @@
[ ... ]
Quoted 5 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_interrupt.c new file +5
-0+int nbl_res_intr_cfg_msix_map (struct nbl_resource_mgt *res_mgt, +u16 func_id,u16 num_net_msix, +u16 num_others_msix, +bool net_msix_mask_en) +{
[ ... ]
Quoted 2 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_interrupt.c new file +2
-0+ had_config = intr_mgt->func_intr_res[func_id].state == +NBL_INTR_FUNC_CONFIGURED ;
[ ... ]
Quoted 25 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_interrupt.c new file +25
-0+/* Allocate MSIX DMA table once per function */ +if (!official_tbl->base_addr) { + official_tbl->size = +sizeof (struct nbl_msix_map ) * +NBL_MSIX_MAP_TABLE_MAX_ENTRIES ; + official_tbl->base_addr =dma_alloc_coherent (dev, + official_tbl->size, + &official_tbl->dma, +GFP_KERNEL ); +if (!official_tbl->base_addr) { +dev_err (dev,"Failed to allocate DMA memory for MSIX table\n" ); + ret = -ENOMEM ; + goto release_vecs_unlock; + } + } + +/* Allocate net interrupt vectors */ +for (i =0 ; i < num_net_msix; i++) { + intr_index =find_first_zero_bit (intr_mgt->intr_net_bmap, +NBL_MAX_NET_INTERRUPT ); +if (intr_index ==NBL_MAX_NET_INTERRUPT ) { +dev_err (dev,"No free net interrupt vectors left\n" ); + ret = -EAGAIN ; + goto release_vecs_unlock; + }
[ ... ]
Quoted 8 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_interrupt.c new file +8
-0+release_vecs_unlock: +nbl_intr_release_bitmap (intr_mgt, res_mgt, tmp_interrupts, + allocated_cnt); +kfree (tmp_interrupts); +/* +* On a failed fresh configuration, release the DMA table +* allocated during this call. On a failed reconfiguration the +* old configuration is still intact (it is only torn down
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_interrupt.c line 622
Quoted 10 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_interrupt.c new file +10
-0+* after all vector allocations succeed) and owns the table. +*/ +if (!had_config && official_tbl->base_addr) { +dma_free_coherent (dev, official_tbl->size, + official_tbl->base_addr, + official_tbl->dma); + official_tbl->base_addr =NULL ; + official_tbl->dma =0 ; + official_tbl->size =0 ; + }
[Severity: Medium]
Can this free a table that this call did not allocate?
Suppose a function is configured and then destroyed. In that case __nbl_res_intr_complete_destroy_msix_map() sets the state to NBL_INTR_FUNC_IDLE and keeps official_tbl->base_addr allocated.
On the next nbl_res_intr_cfg_msix_map() for that function, had_config is false. The "if (!official_tbl->base_addr)" allocation is skipped, so the kept table is reused.
If the net or other vector allocation then fails with -EAGAIN, the code jumps to release_vecs_unlock. The !had_config check then frees the kept table at runtime, while the control PF is still bus master.
This conflicts with the comment in __nbl_res_intr_complete_destroy_msix_map() ("freeing here would let a late fetch read reused memory"). It also conflicts with the comment in nbl_intr_mgt_stop() ("so this is the only place they are released"). In addition, the next configuration allocates a fresh table, so the table is not "allocated once per function".
Later in the series, the sibling PF path reaches this code. nbl_dev_stop() sends the destroy RPC, and a rebind goes through nbl_dev_start() -> cfg RPC -> nbl_res_intr_cfg_msix_map(). With today's single mailbox vector per PF the pool is hard to exhaust. However, the RPC accepts up to 1024 vectors per function.
Would a local flag that records whether this call allocated the table be a better condition for the free than !had_config?
[ ... ]
Quoted 9 lines
+/*
+ * Reset the software state and invalidate every function's hardware MSI-X
+ * map entry once. The chip-internal tables (FUNCTION_MSIX_MAP,
+ *PADPT_HOST_MSIX_INFO,HOST_MSIX_FID_TABLE) can survive a kexec or a
+ * forced unload without FLR and there is no .shutdown callback to scrub
+ * them. OnlyFUNCTION_MSIX_MAPis scrubbed here: it is indexed by
+ *func_id, so covering allNBL_MAX_PFentries costs one register write
+ * each, after which the pcompleter can no longer fetch a stale table that
+ * points into memory this kernel may have reallocated. The INFO/FID
[ ... ]
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_interrupt.c line 716
Quoted 5 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_interrupt.c new file +5
-0+for (func_id =0 ; func_id <NBL_MAX_PF ; func_id++) + hw_ops->cfg_msix_map (res_mgt->hw_ops_tbl->priv, func_id, + false,0 ,0 ,0 ,0 ); + hw_ops->flush_write (res_mgt->hw_ops_tbl->priv); +nbl_intr_quiesce_wait ();
[Severity: Medium]
Does this loop cover every FUNCTION_MSIX_MAP entry that the hardware can fetch? It only invalidates func_id 0 through NBL_MAX_PF - 1.
The patch itself describes a function-id space wider than the PFs. __nbl_res_intr_set_mailbox_irq() says func_intr_res[] "is indexed by the raw hardware function id; this bound check ... is what keeps VF ids out". Also, NBL_PCOMPLETER_HOST_MSIX_FID_TABLE_FID_MASK is GENMASK(9, 0).
Suppose a previous kernel, for example one running an out-of-tree driver with VF support, left VF map entries VALID and they survived kexec. This loop would not scrub those entries. Their fetches carry the control PF's BDF, so pci_set_master() in nbl_probe() would re-enable them.
The patch doesn't say whether the table has entries beyond PF 7, or whether the chip still fetches them when VFs are disabled. If it does, should the scrub cover the full function-id range?
[ ... ]
Quoted 27 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_main.c +16
-2@@ -234,24 +234,36 @@ static int nbl_probe(struct pci_dev *pdev,/* never return fail when DMA_BIT_MASK(64) */ dma_set_mask_and_coherent (dev,DMA_BIT_MASK (64 )); -pci_set_master (pdev); - +/* +* Bus mastering is enabled only after nbl_core_init() returns. +* +* FUNCTION_MSIX_MAP survives a kexec or a forced unload without +* FLR and there is no .shutdown callback to scrub it, so at this +* point every map entry left VALID by a previous kernel still +* holds a DMA address that this kernel has already reallocated. +* Those entries tag their table fetches as bus-master reads, and +* the only thing standing between such a fetch and a read of +* reused memory is Bus Master Enable, which the PCI core cleared +* on the way into this kernel. nbl_intr_setup_mgt() invalidates +* the entries from inside nbl_core_init(); enabling bus mastering +* before that would re-open the window. Everything up to that +* point is host-side setup plus MMIO and nothing in it waits for +* the device, so it does not need bus mastering. +*/ err =nbl_probe_chip_deps (pdev, param.caps.has_ctrl);
[ ... ]
drivers/net/ethernet/nebula-matrix/nbl/nbl_main.c static int nbl_probe(struct pci_dev *pdev, · line 264
Quoted 3 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_main.c +2
-0+ +pci_set_master (pdev);pci_set_drvdata (pdev, adapter);
[Severity: Low]
Is Bus Master Enable guaranteed to be clear at this point? The patch only moves pci_set_master() later. Nothing clears the bit before the scrub in nbl_intr_setup_mgt().
On the way out, the PCI core clears it only in pci_device_shutdown() when kexec_in_progress is set, so the kexec case in the comment does hold.
On a kdump boot, the crashed kernel runs no shutdown callbacks. Also, pci_device_remove() does not clear the bit when a previously bound driver unbinds without calling pci_clear_master() or pci_disable_device().
In those cases, could the device fetch stale VALID map entries between pcim_enable_device() and the scrub? Would an explicit pci_clear_master() before nbl_core_init() make the stated guarantee hold no matter how the device was left?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261112094925.6098-1-illusion.wang%40nebula-matrix.com
illusion.wang
maintainerPATCH v34 4/8 net/nebula-matrix: add chip-wide hardware init/deinit implementation
In reply to illusion.wangFrom: illusion wang <[email protected]>
Add the Leonis chip-wide init_module() and deinit_module() hooks in the hw and resource layers. The resource layer gates both on has_ctrl, so they only run on the control PF.
init_module() programs the chip-wide datapath registers, none of which firmware sets up on the driver's behalf:
- DPED: VLAN/DSCP offsets and the SCTP L4 checksum enable.
- UPED: V4/V6 TCP L3-length edit profiles.
- SHAPING: per-port CIR/PIR from the firmware-reported speed (the DVN dport entry carries PIR at the full rate and CIR at half rate), a bucket depth derived from the rate, PSHA enable for the ports present, and a clear of the PF-indexed shaping_net table.
- DSCH: host QID max (0x7ff) and the host/ECPU quanta.
- USTORE: minimum packet length 42 and the per-port drop threshold, scaled by the PF count, plus a clear of the port drop/truncate counters by reading them back.
- DSTORE: clear the per-port drop enable, program the XON/XOFF flow-control thresholds (a larger pair at 100G), and enable the discard backpressure threshold.
- DVN: ECPU queue count, descriptor-write merge timeout, descriptor request count (by speed), and the relaxed-ordering bits.
- UVN: ECPU queue count, descriptor read-wait/write-timeout, the error mask, and the relaxed-ordering bits. The relaxed-ordering bits in both DVN and UVN are derived from the PCIe DEVCTL state through pcie_relaxed_ordering_enabled().
- UQM: clear the drop counters and force que_type to backpressure.
- PADPT: the host flow-control credits.
This also adds a firmware ABI dependency: nbl_hw_get_quirks() reads a quirk word at mailbox offset NBL_LEONIS_QUIRKS_OFFSET (0x140), where ~0U means "no quirks" and BIT(1) selects the UVN prefetch mode (sel=0, use the configured descriptor count) instead of the internal calculation.
deinit_module() clears the driver_status bit, flushes writes and sleeps 2-3 ms to give the asynchronous firmware cleanup pass time to run. It deliberately reverts none of the above: that pass covers per-PF state only (qinfo registers and mailbox routing), so the chip-wide datapath registers stay programmed until chip reset or until a later init_module() overwrites them, even if the control PF is later bound to another driver. The hardware exposes no cleanup-complete status bit, so the sleep is best-effort and cannot prove the pass has finished.
A stale driver_status bit (kexec, or an unload that never reached deinit_module()) is cleared in nbl_hw_init_leonis(), before this probe programs any per-PF state. The 1 -> 0 transition makes firmware reclaim the mailbox QINFO routing and qinfo registers, so clearing it from init_module() instead - after nbl_dev_setup_chan_qinfo() has programmed them - would leave the inter-PF mailbox dead and make sibling probes time out.
Two known limitations of that handshake, neither of which the driver can close on this hardware:
- NBL_DRIVER_STATUS_REG is shared with firmware. Bit 16 is the only bit the driver owns, and the register has no documented W1S/W1C form, so changing bit 16 has to be a read-modify-write. reg_lock makes the read-modify-write atomic against other CPU writers but cannot serialise against firmware, so a firmware update to a different bit landing between the read and the write would be written back stale. All three callers (the probe-time stale clear, init_module() and deinit_module()) could in principle hit that; no such firmware update is known to race them, but that is an observation about this hardware revision, not a guarantee.
- The 2-3 ms wait is an empirical bound, not a documented firmware latency. There is nothing to poll instead: the hardware exposes no cleanup-complete bit, and the reclaimed entries read back the same before and after the pass, so there is no observable side effect to wait on and nothing to validate the delay against. A pass that takes longer than the window - an ECPU still busy right after kexec, say - can therefore still race a later probe and reclaim routing that probe has already programmed. What actually protects the per-PF state is the ordering around the clear, not the delay, and on teardown that ordering comes from the device link between the sibling PFs and the control PF.
The two hooks are only checked for non-NULL by nbl_hw_setup_ops() and nbl_res_setup_ops() at this point; nothing calls them yet. They gain callers later in the series, from the dispatch init_module()/ deinit_module() and from nbl_dev_setup_ctrl_dev()/ nbl_dev_remove_ctrl_dev().
--- .../net/ethernet/nebula-matrix/nbl/Makefile | 1 + .../nebula-matrix/nbl/nbl_hw/nbl_chip.c | 32 + .../nebula-matrix/nbl/nbl_hw/nbl_chip.h | 12 + .../nbl/nbl_hw/nbl_hw_leonis/nbl_hw_leonis.c | 776 +++++++++++++++++- .../nbl/nbl_hw/nbl_hw_leonis/nbl_hw_leonis.h | 242 ++++++ .../nbl_hw_leonis/nbl_resource_leonis.c | 5 +- .../nbl_hw_leonis/nbl_resource_leonis.h | 1 + .../nbl/nbl_include/nbl_def_hw.h | 3 + .../nbl/nbl_include/nbl_def_resource.h | 3 + .../nbl/nbl_include/nbl_include.h | 19 + 10 files changed, 1092 insertions(+), 2 deletions(-) create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_chip.c create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_chip.h
drivers/net/ethernet/nebula-matrix/nbl/Makefile +1 -0
@@ -9,4 +9,5 @@ nbl-objs += nbl_common/nbl_common.o \ nbl_hw/nbl_hw_leonis/nbl_resource_leonis.o \ nbl_hw/nbl_resource.o \ nbl_hw/nbl_interrupt.o \ + nbl_hw/nbl_chip.o \ nbl_main.o
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_chip.c new file +32 -0
@@ -0,0 +1,32 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (c) 2026 Nebula Matrix Limited. +*/ +#include <linux/device.h> +#include "nbl_chip.h" + +void nbl_res_chip_deinit_module (struct nbl_resource_mgt *res_mgt) +{ +struct nbl_hw_ops *hw_ops = res_mgt->hw_ops_tbl->ops; +struct nbl_common_info *common = res_mgt->common; + +if (!common->has_ctrl) +return ; + hw_ops->deinit_module (res_mgt->hw_ops_tbl->priv); +} + +int nbl_res_chip_init_module (struct nbl_resource_mgt *res_mgt) +{ +struct nbl_common_info *common = res_mgt->common; +struct nbl_hw_ops *hw_ops; +u8 eth_speed, eth_num; +struct nbl_hw_mgt *p; + +if (!common->has_ctrl) +return -EINVAL ; + eth_speed = res_mgt->resource_info->board_info.eth_speed; + eth_num = res_mgt->resource_info->board_info.eth_num; + hw_ops = res_mgt->hw_ops_tbl->ops; + p = res_mgt->hw_ops_tbl->priv; +return hw_ops->init_module (p, eth_speed, eth_num); +}
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_chip.h new file +12 -0
@@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (c) 2026 Nebula Matrix Limited. +*/ + +#ifndef _NBL_CHIP_H_ +#define _NBL_CHIP_H_ + +#include "nbl_resource.h" +int nbl_res_chip_init_module (struct nbl_resource_mgt *res_mgt); +void nbl_res_chip_deinit_module (struct nbl_resource_mgt *res_mgt); +#endif
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_hw_leonis.c +775 -1
811 lines not on this page · raw message
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_hw_leonis.h +242 -0
271 lines not on this page · raw message
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_resource_leonis.c +4 -1
@@ -13,6 +13,8 @@ static struct nbl_resource_ops res_ops = { .cfg_msix_map = nbl_res_intr_cfg_msix_map, .destroy_msix_map = nbl_res_intr_destroy_msix_map, .set_mailbox_irq = nbl_res_intr_set_mailbox_irq, + .init_module = nbl_res_chip_init_module, + .deinit_module = nbl_res_chip_deinit_module, }; static struct nbl_resource_mgt *
@@ -46,7 +48,8 @@ nbl_res_setup_ops(struct device *dev, struct nbl_resource_mgt *res_mgt)return ERR_PTR (-ENOMEM );if (!res_ops.get_vsi_id || !res_ops.get_eth_id || !res_ops.cfg_msix_map || !res_ops.destroy_msix_map || - !res_ops.set_mailbox_irq) + !res_ops.set_mailbox_irq || !res_ops.init_module || + !res_ops.deinit_module)return ERR_PTR (-EINVAL ); res_ops_tbl->ops = &res_ops; res_ops_tbl->priv = res_mgt;
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_resource_leonis.h +1 -0
@@ -8,4 +8,5 @@#include "../nbl_resource.h" #include "../nbl_interrupt.h" +#include "../nbl_chip.h" #endif
drivers/net/ethernet/nebula-matrix/nbl/nbl_include/nbl_def_hw.h +3 -0
@@ -12,6 +12,9 @@ struct nbl_board_port_info;struct nbl_hw_mgt ;struct nbl_adapter ;struct nbl_hw_ops { +int (*init_module)(struct nbl_hw_mgt *hw_mgt,u8 eth_speed,u8 eth_num); +void (*deinit_module)(struct nbl_hw_mgt *hw_mgt); +void (*cfg_msix_map)(struct nbl_hw_mgt *hw_mgt,u16 func_id,bool valid,dma_addr_t dma_addr,u8 bus,u8 devid,u8 function);
drivers/net/ethernet/nebula-matrix/nbl/nbl_include/nbl_def_resource.h +3 -0
@@ -12,6 +12,9 @@ struct nbl_resource_mgt;struct nbl_adapter ;struct nbl_resource_ops { +int (*init_module)(struct nbl_resource_mgt *res_mgt); +void (*deinit_module)(struct nbl_resource_mgt *res_mgt); +int (*cfg_msix_map)(struct nbl_resource_mgt *res_mgt,u16 func_id,u16 num_net_msix,u16 num_others_msix,bool net_msix_mask_en);
drivers/net/ethernet/nebula-matrix/nbl/nbl_include/nbl_include.h +19 -0
@@ -29,4 +29,23 @@ struct nbl_init_param { struct nbl_func_caps caps; }; +/* +* Firmware ABI defines port speed enum fixed, value 0 represents 10G, cannot +* reassign 0 to INVALID for compatibility +*/ +enum nbl_fw_port_speed { +NBL_FW_PORT_SPEED_10G , +NBL_FW_PORT_SPEED_25G , +NBL_FW_PORT_SPEED_50G , +NBL_FW_PORT_SPEED_100G , +}; + +/* +* Firmware quirk word @ NBL_LEONIS_QUIRKS_OFFSET (0x140) +* Sentinel value: ~0U (0xFFFFFFFF) = firmware reports no active quirks +* BIT(1): NBL_QUIRK_UVN_PREFETCH_ALIGN – control UVN descriptor prefetch +* selection +*/ +#define NBL_QUIRK_UVN_PREFETCH_ALIGN BIT(1) +#endif
Signature · 1 line
2.51.0.windows.1
[email protected]
AI reviewRe: [PATCH v34 net-next 4/8] net/nebula-matrix: add chip-wide hardware init/deinit implementation
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
Critical: 0 · High: 0 · Medium: 2 · 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] The control-PF probe has no real handshake with the asynchronous firmware cleanup pass; it relies only on a fixed 2-3 ms…
- [Medium] nbl_hw_set_driver_status() changes bit16 of NBL_DRIVER_STATUS_REG (0x1300444) with a read-modify-write:…
- [Low] The patch adds a firmware ABI dependency with no version or capability gate.
--
net/nebula-matrix: add chip-wide hardware init/deinit implementation
This adds the Leonis chip-wide init_module() and deinit_module() hooks in the hw and resource layers. They run only on the control PF. init_module() programs the chip-wide datapath registers and sets the driver_status bit. deinit_module() clears the bit and sleeps 2-3 ms for the asynchronous firmware cleanup. A stale driver_status bit is also cleared at probe time in nbl_hw_init_leonis().
Quoted 4 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_hw_leonis.c +0
-0
[ ... ]
Quoted 12 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_hw_leonis.c +8
-0@@ -150,6 +175,742 @@ static void nbl_hw_get_fw_eth_map(struct nbl_hw_mgt *hw_mgt, u32 *eth_map) *eth_map =FIELD_GET (NBL_FW_BOARD_DW6_ETH_BITMAP_MASK , data); } +static u32 nbl_hw_get_quirks (struct nbl_hw_mgt *hw_mgt) +{ +u32 quirks =0 ; + +/* +* Read quirk bits from mailbox register. +* All supported firmware implement the quirk ABI, +* firmware always populates NBL_LEONIS_QUIRKS_OFFSET.
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_hw_leonis.c static void nbl_hw_get_fw_eth_map(struct nbl_hw_mgt *hw_mgt, u32 *eth_map) · line 195
Quoted 10 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_hw_leonis.c +10
-0+* Value ~0U indicates no active quirks. +*/ +nbl_hw_read_mbx_regs (hw_mgt,NBL_LEONIS_QUIRKS_OFFSET , &quirks, +sizeof (u32)); + +if (quirks == ~0u ) +return 0 ; + +return quirks; +}
[Severity: Low]
The comment says all supported firmware fills in NBL_LEONIS_QUIRKS_OFFSET. However, neither the code nor the commit message names a minimum firmware version. Nothing gates this read on a firmware version or capability either.
nbl_uvn_init() then uses BIT(1) of this word to choose the UVN descriptor prefetch mode:
FIELD_PREP(NBL_UVN_DESC_PREFETCH_INIT_SEL_MASK,
(quirks & NBL_QUIRK_UVN_PREFETCH_ALIGN) ? 0 : 1);On firmware that does not fill in offset 0x140, any value with BIT(1) set other than ~0U would switch the RX prefetch mode to sel=0. No message is printed when that happens.
Could the minimum firmware version be documented? Alternatively, could this read be gated on a firmware version or capability check?
[ ... ]
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_hw_leonis.c static void nbl_hw_get_fw_eth_map(struct nbl_hw_mgt *hw_mgt, u32 *eth_map) · line 767
Quoted 13 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_hw_leonis.c +13
-0+static void nbl_hw_set_driver_status (struct nbl_hw_mgt *hw_mgt,bool active) +{ +u32 status; + +spin_lock (&hw_mgt->reg_lock); + status =nbl_hw_rd32 (hw_mgt,NBL_DRIVER_STATUS_REG ); + + status &= ~BIT (NBL_DRIVER_STATUS_BIT ); + status |=FIELD_PREP (BIT (NBL_DRIVER_STATUS_BIT ), active); + +nbl_hw_wr32 (hw_mgt,NBL_DRIVER_STATUS_REG , status); +spin_unlock (&hw_mgt->reg_lock); +}
[Severity: Medium]NBL_DRIVER_STATUS_REG is documented as shared with firmware, and every bit except bit 16 belongs to firmware. reg_lock only serializes host CPUs.
If firmware updates one of its bits between the nbl_hw_rd32() and the nbl_hw_wr32() here, won't that update be overwritten with the stale value?
All three callers go through this read-modify-write:nbl_hw_clear_stale_status() at probe, nbl_hw_init_module() and nbl_hw_deinit_module().
The header comment says no such firmware write is known. It also says that this is an observation about this hardware revision, not something the driver enforces. What happens to firmware if one of its bits is written back stale is not documented.
Would a driver-owned register or a firmware-acknowledged mailbox command be a safer way to signal that the driver is present?
[ ... ]
Quoted 11 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_hw_leonis.c +11
-0+static void nbl_hw_deinit_module (struct nbl_hw_mgt *hw_mgt) +{ +nbl_hw_set_driver_status (hw_mgt, false); +/* ensure driver_status reaches the chip */ +nbl_flush_writes (hw_mgt); +/* +* Give the asynchronous firmware cleanup pass time to settle +* before returning; best-effort, see NBL_FW_CLEANUP_SYNC_MIN_US. +*/ +usleep_range (NBL_FW_CLEANUP_SYNC_MIN_US ,NBL_FW_CLEANUP_SYNC_MAX_US ); +}
[ ... ]
Quoted 7 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_hw_leonis.c +7
-0+static void nbl_hw_clear_stale_status (struct nbl_hw_mgt *hw_mgt) +{ +u32 status; + +if (!hw_mgt->common->has_ctrl) +return ; +
[ ... ]
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_hw_leonis.c static void nbl_hw_get_fw_eth_map(struct nbl_hw_mgt *hw_mgt, u32 *eth_map) · line 872
Quoted 10 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_hw_leonis/nbl_hw_leonis.c +10
-0+ status =nbl_hw_rd32 (hw_mgt,NBL_DRIVER_STATUS_REG ); +if (!(status &BIT (NBL_DRIVER_STATUS_BIT ))) +return ; + +dev_warn (hw_mgt->common->dev, +"driver_status already set, stale cleanup forced\n" ); +nbl_hw_set_driver_status (hw_mgt, false); +nbl_flush_writes (hw_mgt); +usleep_range (NBL_FW_CLEANUP_SYNC_MIN_US ,NBL_FW_CLEANUP_SYNC_MAX_US ); +}
[Severity: Medium]
Is the 2-3 ms sleep enough to order this probe after the firmware cleanup pass?
Later in the series ("net/nebula-matrix: add common/ctrl dev init/remove operation"), nbl_dev_init() runs after this function. It programs exactly the state that the firmware pass reclaims:
if (common->has_ctrl)
nbl_dev_setup_chan_qinfo(dev_mgt, NBL_CHAN_TYPE_MAILBOX);
...
ret = nbl_dev_setup_common_dev(adapter);The pass can run longer than the window. The commit message gives an ECPU still busy right after kexec as one example. Firmware would then wipe the mailbox QINFO routing and qinfo registers that probe has just programmed.
No error is reported. Sibling PFs' RPCs in nbl_dev_start() then time out, and their probes fail.
The unbind/rebind path looks similar. nbl_hw_deinit_module() sleeps 2-3 ms and returns. The next probe of the control PF reads bit 16 as 0. It then returns early from nbl_hw_clear_stale_status() without waiting and starts programming the routing right away.
The device link orders the siblings against the control PF. Does anything order the control PF's next probe against its own previous cleanup pass?
The nbl_hw_deinit_module() comment says "Both steps are far slower than the firmware pass". There doesn't seem to be a documented firmware latency behind that.
Is there a firmware acknowledgement that could be waited on instead, such as a mailbox command or another register that firmware writes? If not, could a much larger margin be used on the stale-clear and teardown paths?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261112094925.6098-1-illusion.wang%40nebula-matrix.com
illusion.wang
maintainerPATCH v34 5/8 net/nebula-matrix: dispatch: add control-level routing core infrastructure
In reply to illusion.wangFrom: illusion wang <[email protected]>
Allocate the dispatch management state and ops table and wire the init_module and deinit_module wrappers to the resource ops. Add a ctrl_lvl bitmap to track control privileges; only the MGT level is defined, and it is enabled only on the control PF.
--- .../net/ethernet/nebula-matrix/nbl/Makefile | 1 + .../net/ethernet/nebula-matrix/nbl/nbl_core.h | 2 + .../nebula-matrix/nbl/nbl_core/nbl_dispatch.c | 117 ++++++++++++++++++ .../nebula-matrix/nbl/nbl_core/nbl_dispatch.h | 23 ++++ .../nbl/nbl_include/nbl_def_dispatch.h | 54 ++++++++ .../net/ethernet/nebula-matrix/nbl/nbl_main.c | 8 ++ 6 files changed, 205 insertions(+) create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dispatch.c create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dispatch.h create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_include/nbl_def_dispatch.h
drivers/net/ethernet/nebula-matrix/nbl/Makefile +1 -0
@@ -10,4 +10,5 @@ nbl-objs += nbl_common/nbl_common.o \ nbl_hw/nbl_resource.o \ nbl_hw/nbl_interrupt.o \ nbl_hw/nbl_chip.o \ + nbl_core/nbl_dispatch.o \ nbl_main.o
drivers/net/ethernet/nebula-matrix/nbl/nbl_core.h +2 -0
@@ -17,12 +17,14 @@ enum {struct nbl_interface {struct nbl_hw_ops_tbl *hw_ops_tbl;struct nbl_resource_ops_tbl *resource_ops_tbl; +struct nbl_dispatch_ops_tbl *dispatch_ops_tbl;struct nbl_channel_ops_tbl *channel_ops_tbl; };struct nbl_core {struct nbl_hw_mgt *hw_mgt;struct nbl_resource_mgt *res_mgt; +struct nbl_dispatch_mgt *disp_mgt;struct nbl_channel_mgt *chan_mgt; };
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dispatch.c new file +117 -0
117 lines not on this page · raw message
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dispatch.h new file +23 -0
@@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (c) 2026 Nebula Matrix Limited. +*/ + +#ifndef _NBL_DISPATCH_H_ +#define _NBL_DISPATCH_H_ +#include "../nbl_include/nbl_include.h" +#include "../nbl_include/nbl_def_channel.h" +#include "../nbl_include/nbl_def_resource.h" +#include "../nbl_include/nbl_def_dispatch.h" +#include "../nbl_include/nbl_def_common.h" +#include "../nbl_core.h" + +struct nbl_dispatch_mgt { +struct nbl_common_info *common; +struct nbl_resource_ops_tbl *res_ops_tbl; +struct nbl_channel_ops_tbl *chan_ops_tbl; +struct nbl_dispatch_ops_tbl *disp_ops_tbl; + DECLARE_BITMAP(ctrl_lvl, NBL_DISP_CTRL_LVL_MAX); +}; + +#endif
drivers/net/ethernet/nebula-matrix/nbl/nbl_include/nbl_def_dispatch.h new file +54 -0
54 lines not on this page · raw message
drivers/net/ethernet/nebula-matrix/nbl/nbl_main.c +8 -0
@@ -11,6 +11,7 @@#include "nbl_include/nbl_def_channel.h" #include "nbl_include/nbl_def_hw.h" #include "nbl_include/nbl_def_resource.h" +#include "nbl_include/nbl_def_dispatch.h" #include "nbl_include/nbl_def_common.h" #include "nbl_core.h"
@@ -48,7 +49,13 @@ struct nbl_adapter *nbl_core_init(struct pci_dev *pdev, ret =nbl_res_init_leonis (adapter);if (ret) goto out_chan_remove; + + ret =nbl_disp_init (adapter); +if (ret) + goto out_res_remove;return adapter; +out_res_remove: +nbl_res_remove_leonis (adapter); out_chan_remove:nbl_chan_remove_common (adapter); out_hw_remove:
@@ -59,6 +66,7 @@ out:void nbl_core_remove (struct nbl_adapter *adapter) { +nbl_disp_remove (adapter);nbl_res_remove_leonis (adapter);nbl_chan_remove_common (adapter);nbl_hw_remove_leonis (adapter);
Signature · 1 line
2.51.0.windows.1
[email protected]
AI reviewRe: [PATCH v34 net-next 5/8] net/nebula-matrix: dispatch: add control-level routing core infrastructure
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The -EOPNOTSUPP fallback in nbl_disp_init_module() can never run, and the kernel-doc describes it as if it could.
--
net/nebula-matrix: dispatch: add control-level routing core infrastructure
This patch adds the dispatch layer. It allocates the dispatch management state and the ops table, and it wires the init_module and deinit_module wrappers to the resource ops. It also adds a ctrl_lvl bitmap, with only the MGT level defined, which is set only on the control PF.
Quoted 6 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dispatch.c new file +0
-0@@ -0,0 +1,117 @@
[ ... ]
Quoted 8 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dispatch.c new file +8
-0+static void nbl_disp_deinit_module (struct nbl_dispatch_mgt *disp_mgt) +{ +struct nbl_resource_ops *res_ops = disp_mgt->res_ops_tbl->ops; +struct nbl_resource_mgt *p = disp_mgt->res_ops_tbl->priv; + +if (res_ops->deinit_module) + res_ops->deinit_module (p); +}
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dispatch.c line 26
Quoted 10 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dispatch.c new file +10
-0+ +static int nbl_disp_init_module (struct nbl_dispatch_mgt *disp_mgt) +{ +struct nbl_resource_ops *res_ops = disp_mgt->res_ops_tbl->ops; +struct nbl_resource_mgt *p = disp_mgt->res_ops_tbl->priv; + +if (res_ops->init_module) +return res_ops->init_module (p); +return -EOPNOTSUPP ; +}
[Severity: Low]
This isn't a bug, but can the -EOPNOTSUPP fallback in nbl_disp_init_module() ever run? The same question applies to the NULL check in nbl_disp_deinit_module().
nbl_res_setup_ops() in nbl_resource_leonis.c will not publish the resource ops table if either op is missing:
nbl_res_setup_ops() {
...
if (!res_ops.get_vsi_id || !res_ops.get_eth_id ||
!res_ops.cfg_msix_map || !res_ops.destroy_msix_map ||
!res_ops.set_mailbox_irq || !res_ops.init_module ||
!res_ops.deinit_module)
return ERR_PTR(-EINVAL);
...
}nbl_core_init() calls nbl_res_init_leonis() before nbl_disp_init(), so a missing op fails probe before the dispatch wrappers are installed. The static res_ops table also always sets nbl_res_chip_init_module and nbl_res_chip_deinit_module.
Could these NULL branches be dropped? The kernel-doc below would then need updating too.
[ ... ]
Quoted 6 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_include/nbl_def_dispatch.h new file +0
-0@@ -0,0 +1,54 @@
[ ... ]
Quoted 7 lines
+/**
+ * structnbl_dispatch_ops- dispatch control plane operation callbacks
+ * @init_module: chip-wide datapath init, forwarded to the resource layer+ * (res_ops->init_module() and then hw_ops->init_module()); + * on leonis it programs the chip-wide datapath registers and + * sets driver_status=true. Returns -EOPNOTSUPP if the + * resource layer installs no init_module op. Control-PF
This sentence documents an error return that no caller can get. It also suggests the resource op is optional, while nbl_res_setup_ops() requires it. This text is still unchanged at the end of the series.
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261112094925.6098-1-illusion.wang%40nebula-matrix.com
illusion.wang
maintainerPATCH v34 6/8 net/nebula-matrix: dispatch: add mailbox RPCs for msix map, mailbox irq and vsi/eth id
In reply to illusion.wangFrom: illusion wang <[email protected]>
Add request and response handlers for five dispatch operations:cfg_msix_map, destroy_msix_map, set_mailbox_irq, get_vsi_id and get_eth_id. Each op is resolved by capability level: a function with has_ctrl calls the resource op locally, a function with has_net forwards it to the management PF over the channel, and a function with neither leaves the op pointer NULL, so the caller must not invoke it. The channel framework itself - send_msg, the ACK plumbing and handler registration - is already in place from the channel layer patch; this one adds the five message handlers on top of it.
A request builds the payload, sends it synchronously and checks the ACK length before parsing; a short ACK returns -EBADMSG. A responder rejects VFs and out-of-range source PF ids with -EPERM, checks the payload length before parsing, and forwards the resource layer's errno verbatim so the requester can tell a protocol error from a failed operation. If the resource op is not installed the responder answers -EOPNOTSUPP. A peer PF is treated as trusted: hardware and firmware forbid PF passthrough to guests, so every peer runs this host driver, and the resource layer bounds the requested vector counts by the global pool.
The cfg_msix_map, destroy_msix_map and set_mailbox_irq requests carry a caller precondition: clear the mailbox IRQ_RDY flag and use the polling send path, because the responder changes mailbox MSI-X routing and an interrupt-driven ACK wakeup cannot be relied on while routing is in flux. The precondition is documented on struct nbl_dispatch_ops, which is what callers program against, not only next to the static helpers.
No serialization is added here. The resource layer serializes concurrent callers itself through intr_mgt->lock, and the dispatch layer has no mutable state of its own to protect; the read-only get_vsi_id/get_eth_id paths only touch static init metadata.
This is also the first path that lets a sibling PF tear its MSI-X map down on the control PF at runtime. That teardown runs while the control PF is still bus master, which is why the interrupt layer keeps each coherent map table for the function's lifetime and releases it only in nbl_intr_mgt_stop(), instead of relying on the per-destroy quiesce window.
The five responders are registered with the channel layer and owned by
it: they live in the channel's handler xarray and are freed with it, so nbl_disp_remove() has nothing to undo. Nothing can enter them in this patch because no mailbox queue is running yet. Once a queue is brought up, the invariant they rely on is that it must be torn down before nbl_res_remove_leonis() runs, so no responder can outlive the resource layer it forwards into.
--- .../nebula-matrix/nbl/nbl_core/nbl_dispatch.c | 534 ++++++++++++++++++ .../nbl/nbl_include/nbl_def_channel.h | 40 ++ .../nbl/nbl_include/nbl_def_dispatch.h | 45 +- 3 files changed, 616 insertions(+), 3 deletions(-)
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dispatch.c +534 -0
@@ -6,6 +6,174 @@#include <linux/pci.h> #include "nbl_dispatch.h" +static int nbl_disp_chan_get_vsi_id_req (struct nbl_dispatch_mgt *disp_mgt, +u16 type,u16 *vsi_id) +{ +struct nbl_channel_ops *chan_ops = disp_mgt->chan_ops_tbl->ops; +struct nbl_common_info *common = disp_mgt->common; +struct nbl_chan_param_get_vsi_id result = {0 }; +struct nbl_chan_param_get_vsi_id param = {0 }; +struct nbl_chan_send_info chan_send = {0 }; +int ret; + + param.type =cpu_to_le16 (type); + +nbl_chan_fill_send_info (&chan_send, common->mgt_pf, +NBL_CHAN_MSG_GET_VSI_ID , + ¶m,sizeof (param), &result, +sizeof (result),1 ); + ret = chan_ops->send_msg (disp_mgt->chan_ops_tbl->priv, &chan_send); +if (ret) +return ret; +if (chan_send.ack_len !=sizeof (result)) { +dev_err (disp_mgt->common->dev, +"get_vsi_id: short ACK, ack_len=%u expected %zu\n" , + chan_send.ack_len,sizeof (result)); +return -EBADMSG ; + } + *vsi_id =le16_to_cpu (result.vsi_id); +return 0 ; +} + +static void nbl_disp_chan_get_vsi_id_resp (void *priv,u16 src_id,u16 msg_id, +void *data,u32 data_len) +{ +struct nbl_dispatch_mgt *disp_mgt = (struct nbl_dispatch_mgt *)priv; +struct nbl_channel_ops *chan_ops = disp_mgt->chan_ops_tbl->ops; +struct nbl_resource_ops *res_ops = disp_mgt->res_ops_tbl->ops; +struct nbl_resource_mgt *p = disp_mgt->res_ops_tbl->priv; +struct device *dev = disp_mgt->common->dev; +struct nbl_chan_param_get_vsi_id result = {0 }; +struct nbl_chan_param_get_vsi_id param = {0 }; +struct nbl_chan_ack_info chan_ack; +int err =0 ; +u16 vsi_id =0 ; +u32 rel_pf_id; +int ret; + + ret =nbl_common_func_id_to_rel_pf_id (disp_mgt->common, src_id, + &rel_pf_id); +if (ret) { + err = -EPERM ; + goto ack_out; + } +if (rel_pf_id >= disp_mgt->common->max_pf) { + err = -EPERM ; + goto ack_out; + } +if (data_len <sizeof (param)) { + err = -EBADMSG ; + goto ack_out; + } +memcpy (¶m, data,sizeof (param)); + +if (res_ops->get_vsi_id) { + ret = res_ops->get_vsi_id (p, src_id,le16_to_cpu (param.type), + &vsi_id); +/* Forward the resource errno verbatim to the requester */ +if (ret) + err = ret; + }else { + err = -EOPNOTSUPP ; + } + + result.vsi_id =cpu_to_le16 (vsi_id); +ack_out: +nbl_chan_fill_ack_info (&chan_ack, src_id, +NBL_CHAN_MSG_GET_VSI_ID , msg_id, err, + &result,sizeof (result)); + ret = chan_ops->send_ack (disp_mgt->chan_ops_tbl->priv, &chan_ack); +if (ret) +dev_err (dev, +"channel send ack failed with ret: %d, msg_type: %d\n" , + ret,NBL_CHAN_MSG_GET_VSI_ID ); +} + +static int nbl_disp_chan_get_eth_id_req (struct nbl_dispatch_mgt *disp_mgt, +u16 vsi_id,u8 *eth_num,u8 *eth_id, +u8 *logic_eth_id) +{ +struct nbl_channel_ops *chan_ops = disp_mgt->chan_ops_tbl->ops; +struct nbl_common_info *common = disp_mgt->common; +struct nbl_chan_param_get_eth_id result = {0 }; +struct nbl_chan_param_get_eth_id param = {0 }; +struct nbl_chan_send_info chan_send = {0 }; +int ret; + + param.vsi_id =cpu_to_le16 (vsi_id); + +nbl_chan_fill_send_info (&chan_send, common->mgt_pf, +NBL_CHAN_MSG_GET_ETH_ID , + ¶m,sizeof (param), &result, +sizeof (result),1 ); + ret = chan_ops->send_msg (disp_mgt->chan_ops_tbl->priv, &chan_send); +if (ret) +return ret; +if (chan_send.ack_len !=sizeof (result)) { +dev_err (disp_mgt->common->dev, +"get_eth_id: short ACK, ack_len=%u expected %zu\n" , + chan_send.ack_len,sizeof (result)); +return -EBADMSG ; + } + *eth_num = result.eth_num; + *eth_id = result.eth_id; + *logic_eth_id = result.logic_eth_id; + +return 0 ; +} + +static void nbl_disp_chan_get_eth_id_resp (void *priv,u16 src_id,u16 msg_id, +void *data,u32 data_len) +{ +struct nbl_dispatch_mgt *disp_mgt = (struct nbl_dispatch_mgt *)priv; +struct nbl_channel_ops *chan_ops = disp_mgt->chan_ops_tbl->ops; +struct nbl_resource_ops *res_ops = disp_mgt->res_ops_tbl->ops; +struct nbl_resource_mgt *p = disp_mgt->res_ops_tbl->priv; +struct nbl_chan_param_get_eth_id result = {0 }; +struct nbl_chan_param_get_eth_id param = {0 }; +struct device *dev = disp_mgt->common->dev; +struct nbl_chan_ack_info chan_ack; +int err =0 ; +u32 rel_pf_id; +int ret; + + ret =nbl_common_func_id_to_rel_pf_id (disp_mgt->common, src_id, + &rel_pf_id); +if (ret) { + err = -EPERM ; + goto ack_out; + } +if (rel_pf_id >= disp_mgt->common->max_pf) { + err = -EPERM ; + goto ack_out; + } +if (data_len <sizeof (param)) { + err = -EBADMSG ; + goto ack_out; + } +memcpy (¶m, data,sizeof (param)); + +if (res_ops->get_eth_id) { + ret = res_ops->get_eth_id (p, src_id,le16_to_cpu (param.vsi_id), + &result.eth_num, &result.eth_id, + &result.logic_eth_id); +/* Forward the resource errno verbatim to the requester */ +if (ret) + err = ret; + }else { + err = -EOPNOTSUPP ; + } +ack_out: +nbl_chan_fill_ack_info (&chan_ack, src_id, +NBL_CHAN_MSG_GET_ETH_ID , msg_id, err, + &result,sizeof (result)); + ret = chan_ops->send_ack (disp_mgt->chan_ops_tbl->priv, &chan_ack); +if (ret) +dev_err (dev, +"channel send ack failed with ret: %d, msg_type: %d\n" , + ret,NBL_CHAN_MSG_GET_ETH_ID ); +} +static void nbl_disp_deinit_module (struct nbl_dispatch_mgt *disp_mgt) {struct nbl_resource_ops *res_ops = disp_mgt->res_ops_tbl->ops;
@@ -25,6 +193,353 @@ static int nbl_disp_init_module(struct nbl_dispatch_mgt *disp_mgt)return -EOPNOTSUPP ; } +static int nbl_disp_cfg_msix_map (struct nbl_dispatch_mgt *disp_mgt, +u16 num_net_msix,u16 num_others_msix, +bool net_msix_mask_en) +{ +struct nbl_resource_ops *res_ops = disp_mgt->res_ops_tbl->ops; +struct nbl_resource_mgt *p = disp_mgt->res_ops_tbl->priv; +struct nbl_common_info *common = disp_mgt->common; + +if (!res_ops->cfg_msix_map) +return -EOPNOTSUPP ; +/* +* No core-layer lock is taken here: the resource layer serializes +* concurrent callers itself (intr_mgt->lock), and the dispatch +* layer has no mutable state of its own for this op to protect. +*/ +return res_ops->cfg_msix_map (p, common->mgt_pf, num_net_msix, + num_others_msix, net_msix_mask_en); +} + +/* +* Precondition: caller must disable mailbox IRQ_RDY and switch send_msg +* to the polling path before issuing this RPC. The responder rewrites +* the requester's mailbox MSI-X routing during the resource op, so the +* ACK cannot rely on interrupt wakeup while routing is in flux. +*/ +static int +nbl_disp_chan_cfg_msix_map_req (struct nbl_dispatch_mgt *disp_mgt, +u16 num_net_msix,u16 num_others_msix, +bool net_msix_mask_en) +{ +struct nbl_channel_ops *chan_ops = disp_mgt->chan_ops_tbl->ops; +struct nbl_common_info *common = disp_mgt->common; +struct nbl_chan_param_cfg_msix_map param = {0 }; +struct nbl_chan_send_info chan_send = {0 }; +int ret; + + param.num_net_msix =cpu_to_le16 (num_net_msix); + param.num_others_msix =cpu_to_le16 (num_others_msix); + param.msix_mask_en =cpu_to_le16 (!!net_msix_mask_en); + +nbl_chan_fill_send_info (&chan_send, common->mgt_pf, +NBL_CHAN_MSG_CONFIGURE_MSIX_MAP , + ¶m,sizeof (param), +NULL ,0 ,1 ); + ret = chan_ops->send_msg (disp_mgt->chan_ops_tbl->priv, &chan_send); +if (ret) +return ret; +return 0 ; +} + +static void nbl_disp_chan_cfg_msix_map_resp (void *priv,u16 src_id,u16 msg_id, +void *data,u32 data_len) +{ +struct nbl_dispatch_mgt *disp_mgt = (struct nbl_dispatch_mgt *)priv; +struct nbl_channel_ops *chan_ops = disp_mgt->chan_ops_tbl->ops; +struct nbl_resource_ops *res_ops = disp_mgt->res_ops_tbl->ops; +struct nbl_resource_mgt *p = disp_mgt->res_ops_tbl->priv; +struct device *dev = disp_mgt->common->dev; +struct nbl_chan_param_cfg_msix_map param = {0 }; +struct nbl_chan_ack_info chan_ack; +int err =0 ; +u32 rel_pf_id; +int ret; + + ret =nbl_common_func_id_to_rel_pf_id (disp_mgt->common, src_id, + &rel_pf_id); +if (ret) { + err = -EPERM ; + goto ack_out; + } +if (rel_pf_id >= disp_mgt->common->max_pf) { + err = -EPERM ; + goto ack_out; + } +if (data_len <sizeof (param)) { + err = -EBADMSG ; + goto ack_out; + } +memcpy (¶m, data,sizeof (param)); + +if (res_ops->cfg_msix_map) { +/* +* The vector counts come from the peer PF. A peer is +* trusted: hardware and firmware forbid PF passthrough to +* guests (see nbl_get_func_param()), so every peer runs this +* same host driver, and the resource layer bounds the counts +* by the global vector pool. A peer asking for the whole +* pool can starve the others with -EAGAIN, but it cannot +* corrupt state. +*/ + ret = res_ops->cfg_msix_map (p, src_id, +le16_to_cpu (param.num_net_msix), +le16_to_cpu (param.num_others_msix), + !!le16_to_cpu (param.msix_mask_en)); +/* Forward the resource errno verbatim to the requester */ +if (ret) + err = ret; + }else { + err = -EOPNOTSUPP ; + } +ack_out: +nbl_chan_fill_ack_info (&chan_ack, src_id, +NBL_CHAN_MSG_CONFIGURE_MSIX_MAP , msg_id, + err,NULL ,0 ); + ret = chan_ops->send_ack (disp_mgt->chan_ops_tbl->priv, &chan_ack); +if (ret) +dev_err (dev, +"channel send ack failed with ret: %d, msg_type: %d\n" , + ret,NBL_CHAN_MSG_CONFIGURE_MSIX_MAP ); +} + +/* +* Precondition: caller must disable mailbox IRQ_RDY and switch send_msg +* to the polling path before issuing this RPC. The responder retargets +* the requester's mailbox MSI-X routing during the resource op, so the +* ACK cannot rely on interrupt wakeup while routing is in flux. +*/ +static int nbl_disp_chan_destroy_msix_map_req (struct nbl_dispatch_mgt *disp_mgt) +{ +struct nbl_channel_ops *chan_ops = disp_mgt->chan_ops_tbl->ops; +struct nbl_common_info *common = disp_mgt->common; +struct nbl_chan_send_info chan_send = {0 }; +int ret; + +nbl_chan_fill_send_info (&chan_send, common->mgt_pf, +NBL_CHAN_MSG_DESTROY_MSIX_MAP , +NULL ,0 ,NULL ,0 ,1 ); + ret = chan_ops->send_msg (disp_mgt->chan_ops_tbl->priv, &chan_send); +if (ret) +return ret; +return 0 ; +} + +static void nbl_disp_chan_destroy_msix_map_resp (void *priv,u16 src_id, +u16 msg_id,void *data, +u32 data_len) +{ +struct nbl_dispatch_mgt *disp_mgt = (struct nbl_dispatch_mgt *)priv; +struct nbl_channel_ops *chan_ops = disp_mgt->chan_ops_tbl->ops; +struct nbl_resource_ops *res_ops = disp_mgt->res_ops_tbl->ops; +struct nbl_resource_mgt *p = disp_mgt->res_ops_tbl->priv; +struct device *dev = disp_mgt->common->dev; +struct nbl_chan_ack_info chan_ack; +int err =0 ; +u32 rel_pf_id; +int ret; + + ret =nbl_common_func_id_to_rel_pf_id (disp_mgt->common, src_id, + &rel_pf_id); +if (ret) { + err = -EPERM ; + goto ack_out; + } +if (rel_pf_id >= disp_mgt->common->max_pf) { + err = -EPERM ; + goto ack_out; + } +if (res_ops->destroy_msix_map) { + ret = res_ops->destroy_msix_map (p, src_id); +/* Forward the resource errno verbatim to the requester */ +if (ret) + err = ret; + }else { + err = -EOPNOTSUPP ; + } +ack_out: +nbl_chan_fill_ack_info (&chan_ack, src_id, +NBL_CHAN_MSG_DESTROY_MSIX_MAP , msg_id, + err,NULL ,0 ); + ret = chan_ops->send_ack (disp_mgt->chan_ops_tbl->priv, &chan_ack); +if (ret) +dev_err (dev, +"channel send ack failed with ret: %d, msg_type: %d\n" , + ret,NBL_CHAN_MSG_DESTROY_MSIX_MAP ); +} + +/* +* Precondition: caller must disable mailbox IRQ_RDY and switch send_msg +* to the polling path before issuing this RPC. The responder rewrites +* the requester's own mailbox MSI-X routing (MSIX_IDX / MSIX_IDX_VALID) +* before the ACK is sent, so the ACK cannot rely on interrupt wakeup +* while routing is in flux. +*/ +static int nbl_disp_chan_set_mailbox_irq_req (struct nbl_dispatch_mgt *disp_mgt, +u16 vector_id,bool en_msix) +{ +struct nbl_channel_ops *chan_ops = disp_mgt->chan_ops_tbl->ops; +struct nbl_chan_param_set_mailbox_irq param = {0 }; +struct nbl_common_info *common = disp_mgt->common; +struct nbl_chan_send_info chan_send = {0 }; +int ret; + + param.vector_id =cpu_to_le16 (vector_id); + param.en_msix = !!en_msix; + +nbl_chan_fill_send_info (&chan_send, common->mgt_pf, +NBL_CHAN_MSG_MAILBOX_SET_IRQ , + ¶m,sizeof (param),NULL ,0 ,1 ); + ret = chan_ops->send_msg (disp_mgt->chan_ops_tbl->priv, &chan_send); +if (ret) +return ret; +return 0 ; +} + +static void nbl_disp_chan_set_mailbox_irq_resp (void *priv,u16 src_id, +u16 msg_id,void *data, +u32 data_len) +{ +struct nbl_dispatch_mgt *disp_mgt = (struct nbl_dispatch_mgt *)priv; +struct nbl_channel_ops *chan_ops = disp_mgt->chan_ops_tbl->ops; +struct nbl_resource_ops *res_ops = disp_mgt->res_ops_tbl->ops; +struct nbl_resource_mgt *p = disp_mgt->res_ops_tbl->priv; +struct nbl_chan_param_set_mailbox_irq param = {0 }; +struct device *dev = disp_mgt->common->dev; +struct nbl_chan_ack_info chan_ack; +int err =0 ; +u16 vector_id; +u32 rel_pf_id; +bool en_msix; +int ret; + + ret =nbl_common_func_id_to_rel_pf_id (disp_mgt->common, src_id, + &rel_pf_id); +if (ret) { + err = -EPERM ; + goto ack_out; + } +if (rel_pf_id >= disp_mgt->common->max_pf) { + err = -EPERM ; + goto ack_out; + } +if (data_len <sizeof (param)) { + err = -EBADMSG ; + goto ack_out; + } +memcpy (¶m, data,sizeof (param)); + vector_id =le16_to_cpu (param.vector_id); + en_msix = !!param.en_msix; + +if (res_ops->set_mailbox_irq) { + ret = res_ops->set_mailbox_irq (p, src_id, vector_id, en_msix); +/* Forward the resource errno verbatim to the requester */ +if (ret) + err = ret; + }else { + err = -EOPNOTSUPP ; + } + +ack_out: +nbl_chan_fill_ack_info (&chan_ack, src_id, +NBL_CHAN_MSG_MAILBOX_SET_IRQ , msg_id, + err,NULL ,0 ); + ret = chan_ops->send_ack (disp_mgt->chan_ops_tbl->priv, &chan_ack); +if (ret) +dev_err (dev, +"channel send ack failed with ret: %d, msg_type: %d\n" , + ret,NBL_CHAN_MSG_MAILBOX_SET_IRQ ); +} + +static int nbl_disp_destroy_msix_map (struct nbl_dispatch_mgt *disp_mgt) +{ +struct nbl_resource_ops *res_ops = disp_mgt->res_ops_tbl->ops; +struct nbl_resource_mgt *p = disp_mgt->res_ops_tbl->priv; +struct nbl_common_info *common = disp_mgt->common; + +if (!res_ops->destroy_msix_map) +return -EOPNOTSUPP ; +return res_ops->destroy_msix_map (p, common->mgt_pf); +} + +static int nbl_disp_set_mailbox_irq (struct nbl_dispatch_mgt *disp_mgt, +u16 vector_id,bool en_msix) +{ +struct nbl_resource_ops *res_ops = disp_mgt->res_ops_tbl->ops; +struct nbl_resource_mgt *p = disp_mgt->res_ops_tbl->priv; +struct nbl_common_info *common = disp_mgt->common; + +if (!res_ops->set_mailbox_irq) +return -EOPNOTSUPP ; +return res_ops->set_mailbox_irq (p, common->mgt_pf, vector_id, en_msix); +} + +static int nbl_disp_get_vsi_id (struct nbl_dispatch_mgt *disp_mgt,u16 type, +u16 *vsi_id) +{ +struct nbl_resource_ops *res_ops = disp_mgt->res_ops_tbl->ops; +struct nbl_resource_mgt *p = disp_mgt->res_ops_tbl->priv; +struct nbl_common_info *common = disp_mgt->common; + +if (res_ops->get_vsi_id) +return res_ops->get_vsi_id (p, common->mgt_pf, type, vsi_id); +return -EOPNOTSUPP ; +} + +static int nbl_disp_get_eth_id (struct nbl_dispatch_mgt *disp_mgt,u16 vsi_id, +u8 *eth_num,u8 *eth_id,u8 *logic_eth_id) +{ +struct nbl_resource_ops *res_ops = disp_mgt->res_ops_tbl->ops; +struct nbl_resource_mgt *p = disp_mgt->res_ops_tbl->priv; +struct nbl_common_info *common = disp_mgt->common; + +if (res_ops->get_eth_id) +return res_ops->get_eth_id (p, common->mgt_pf, vsi_id, + eth_num, eth_id, logic_eth_id); +return -EOPNOTSUPP ; +} + +static int nbl_disp_setup_msg (struct nbl_dispatch_mgt *disp_mgt) +{ +struct nbl_channel_ops *chan_ops = disp_mgt->chan_ops_tbl->ops; +struct nbl_channel_mgt *p = disp_mgt->chan_ops_tbl->priv; +int ret =0 ; +int _ret; + + _ret = chan_ops->register_msg (p,NBL_CHAN_MSG_CONFIGURE_MSIX_MAP , + nbl_disp_chan_cfg_msix_map_resp, + disp_mgt); +if (_ret <0 && !ret) + ret = _ret; + + _ret = chan_ops->register_msg (p,NBL_CHAN_MSG_DESTROY_MSIX_MAP , + nbl_disp_chan_destroy_msix_map_resp, + disp_mgt); +if (_ret <0 && !ret) + ret = _ret; + + _ret = chan_ops->register_msg (p,NBL_CHAN_MSG_MAILBOX_SET_IRQ , + nbl_disp_chan_set_mailbox_irq_resp, + disp_mgt); +if (_ret <0 && !ret) + ret = _ret; + + _ret = chan_ops->register_msg (p,NBL_CHAN_MSG_GET_VSI_ID , + nbl_disp_chan_get_vsi_id_resp, + disp_mgt); +if (_ret <0 && !ret) + ret = _ret; + + _ret = chan_ops->register_msg (p,NBL_CHAN_MSG_GET_ETH_ID , + nbl_disp_chan_get_eth_id_resp, + disp_mgt); +if (_ret <0 && !ret) + ret = _ret; + +return ret; +} +static void nbl_disp_set_ctrl_bit (struct nbl_dispatch_mgt *disp_mgt,u32 lvl) {set_bit (lvl, disp_mgt->ctrl_lvl);
@@ -34,9 +549,22 @@ static void nbl_disp_refresh_ctrl_ops(struct nbl_dispatch_mgt *disp_mgt) {struct nbl_dispatch_ops *disp_ops = disp_mgt->disp_ops_tbl->ops; +memset (disp_ops,0 ,sizeof (*disp_ops));if (test_bit (NBL_DISP_CTRL_LVL_MGT , disp_mgt->ctrl_lvl)) { disp_ops->init_module = nbl_disp_init_module; disp_ops->deinit_module = nbl_disp_deinit_module; + disp_ops->cfg_msix_map = nbl_disp_cfg_msix_map; + disp_ops->destroy_msix_map = nbl_disp_destroy_msix_map; + disp_ops->set_mailbox_irq = nbl_disp_set_mailbox_irq; + disp_ops->get_vsi_id = nbl_disp_get_vsi_id; + disp_ops->get_eth_id = nbl_disp_get_eth_id; + }else if (test_bit (NBL_DISP_CTRL_LVL_NET , disp_mgt->ctrl_lvl)) { + disp_ops->cfg_msix_map = + nbl_disp_chan_cfg_msix_map_req; + disp_ops->destroy_msix_map = nbl_disp_chan_destroy_msix_map_req; + disp_ops->set_mailbox_irq = nbl_disp_chan_set_mailbox_irq_req; + disp_ops->get_vsi_id = nbl_disp_chan_get_vsi_id_req; + disp_ops->get_eth_id = nbl_disp_chan_get_eth_id_req; } }
@@ -104,9 +632,15 @@ int nbl_disp_init(struct nbl_adapter *adapter) adapter->core.disp_mgt = disp_mgt; adapter->intf.dispatch_ops_tbl = disp_ops_tbl; + ret =nbl_disp_setup_msg (disp_mgt); +if (ret) +return ret; +if (common->has_ctrl)nbl_disp_set_ctrl_bit (disp_mgt,NBL_DISP_CTRL_LVL_MGT ); +if (common->has_net) +nbl_disp_set_ctrl_bit (disp_mgt,NBL_DISP_CTRL_LVL_NET );nbl_disp_refresh_ctrl_ops (disp_mgt);return 0 ; }
drivers/net/ethernet/nebula-matrix/nbl/nbl_include/nbl_def_channel.h +40 -0
@@ -7,6 +7,7 @@#define _NBL_DEF_CHANNEL_H_#include <linux/types.h> +#include <linux/build_bug.h> struct nbl_channel_mgt ;struct nbl_adapter ;
@@ -30,6 +31,11 @@ typedef void (*nbl_chan_resp)(void *, u16, u16, void *, u32);*/ enum nbl_chan_msg_type {NBL_CHAN_MSG_ACK =0 , +NBL_CHAN_MSG_CONFIGURE_MSIX_MAP =17 , +NBL_CHAN_MSG_DESTROY_MSIX_MAP =18 , +NBL_CHAN_MSG_MAILBOX_SET_IRQ =19 , +NBL_CHAN_MSG_GET_VSI_ID =21 , +NBL_CHAN_MSG_GET_ETH_ID =67 ,/* mailbox msg end */ NBL_CHAN_MSG_MAILBOX_MAX , };
@@ -39,6 +45,32 @@ enum nbl_chan_state {NBL_CHAN_STATE_NBITS }; +struct nbl_chan_param_cfg_msix_map { +__le16 num_net_msix; +__le16 num_others_msix; +__le16 msix_mask_en; +__le16 rsvd; +}; + +struct nbl_chan_param_set_mailbox_irq { +__le16 vector_id; +u8 en_msix; +u8 rsvd; +}; + +struct nbl_chan_param_get_vsi_id { +__le16 vsi_id; +__le16 type; +}; + +struct nbl_chan_param_get_eth_id { +__le16 vsi_id; +u8 eth_num; +u8 eth_id; +u8 logic_eth_id; +u8 rsvd[3 ]; +}; +struct nbl_board_port_info {u8 eth_num;u8 eth_speed;
@@ -46,6 +78,14 @@ struct nbl_board_port_info {u8 rsv[5 ]; }; +static_assert (sizeof (struct nbl_chan_param_cfg_msix_map ) ==8 , +"nbl_chan_param_cfg_msix_map size must be 8 bytes" ); +static_assert (sizeof (struct nbl_chan_param_set_mailbox_irq ) ==4 , +"nbl_chan_param_set_mailbox_irq size must be 4 bytes" ); +static_assert (sizeof (struct nbl_chan_param_get_vsi_id ) ==4 , +"nbl_chan_param_get_vsi_id size must be 4 bytes" ); +static_assert (sizeof (struct nbl_chan_param_get_eth_id ) ==8 , +"nbl_chan_param_get_eth_id size must be 8 bytes" );static_assert (sizeof (struct nbl_board_port_info ) ==8 ,"nbl_board_port_info size must be 8 bytes" );
drivers/net/ethernet/nebula-matrix/nbl/nbl_include/nbl_def_dispatch.h +42 -3
64 lines not on this page · raw message
Signature · 1 line
2.51.0.windows.1
[email protected]
AI reviewRe: [PATCH v34 net-next 6/8] net/nebula-matrix: dispatch: add mailbox RPCs for msix map, mailbox irq and vsi/eth id
Thank 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 · 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 new NBL_CHAN_MSG_CONFIGURE_MSIX_MAP responder in nbl_disp_chan_cfg_msix_map_resp() (nbl_dispatch.c:286) passes a peer…
--
net/nebula-matrix: dispatch: add mailbox RPCs for msix map, mailbox irq and vsi/eth id
This patch adds request and response handlers for five dispatch
operations: cfg_msix_map, destroy_msix_map, set_mailbox_irq, get_vsi_id and get_eth_id. A control PF calls the resource ops locally. A has_net PF forwards them to the management PF over the mailbox channel, where the new responders run them on the requester's behalf.
Quoted 4 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dispatch.c +0
-0
[ ... ]
Quoted 1 line
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dispatch.c +0
-0@@ -25,6 +193,353 @@ static int nbl_disp_init_module(struct nbl_dispatch_mgt *disp_mgt)
[ ... ]
Quoted 3 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dispatch.c +3
-0+static void nbl_disp_chan_cfg_msix_map_resp (void *priv,u16 src_id,u16 msg_id, +void *data,u32 data_len) +{
[ ... ]
Quoted 14 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dispatch.c +14
-0+if (rel_pf_id >= disp_mgt->common->max_pf) { + err = -EPERM ; + goto ack_out; + } +if (data_len <sizeof (param)) { + err = -EBADMSG ; + goto ack_out; + } +memcpy (¶m, data,sizeof (param)); + +if (res_ops->cfg_msix_map) { +/* +* The vector counts come from the peer PF. A peer is +* trusted: hardware and firmware forbid PF passthrough to
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dispatch.c static int nbl_disp_init_module(struct nbl_dispatch_mgt *disp_mgt) · line 289
Quoted 10 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dispatch.c +10
-0+* guests (see nbl_get_func_param()), so every peer runs this +* same host driver, and the resource layer bounds the counts +* by the global vector pool. A peer asking for the whole +* pool can starve the others with -EAGAIN, but it cannot +* corrupt state. +*/ + ret = res_ops->cfg_msix_map (p, src_id, +le16_to_cpu (param.num_net_msix), +le16_to_cpu (param.num_others_msix), + !!le16_to_cpu (param.msix_mask_en));
[Severity: Medium]
Can one sibling PF use this responder to drain the shared MSI-X vector pools?
Before res_ops->cfg_msix_map() runs, the only checks are the rel_pf_id range and data_len. So num_net_msix and num_others_msix reach nbl_res_intr_cfg_msix_map() exactly as the peer sent them.
There is no per-function quota in nbl_res_intr_cfg_msix_map() either. Each count is only capped at NBL_MSIX_MAP_TABLE_MAX_ENTRIES (1024), which is the same size as the whole NBL_MAX_OTHER_INTERRUPT pool:
drivers/net/ethernet/nebula-matrix/nbl/nbl_hw/nbl_interrupt.c:nbl_res_intr_cfg_msix_map() {
...
for (; i < requested; i++) {
intr_index =
find_first_zero_bit(intr_mgt->intr_other_bmap,
NBL_MAX_OTHER_INTERRUPT);
if (intr_index == NBL_MAX_OTHER_INTERRUPT) {
dev_err(dev, "No free control interrupt vectors left\n");
ret = -EAGAIN;
goto release_vecs_unlock;
}
...
}The control PF already holds one vector, so a peer request for 1023 other vectors succeeds. After that, every sibling PF that configures its map gets -EAGAIN from cfg_msix_map().
With the later "add common dev start/stop operation" patch, that error makes nbl_dev_start() fail, so the sibling loses its mailbox interrupt and its probe fails.
The net pool (NBL_MAX_NET_INTERRUPT, 4096) can be drained the same way by four peers asking for 1024 net vectors each.
A peer can also send reconfiguration requests over and over. Each one does a kcalloc() and a usleep_range()-based nbl_intr_quiesce_wait(), and it still holds intr_mgt->lock during the wait.
The comment above trusts peers because "hardware and firmware forbid PF passthrough to guests (see nbl_get_func_param())". However, nbl_get_func_param() only selects the control PF:
drivers/net/ethernet/nebula-matrix/nbl/nbl_main.c:nbl_get_func_param() {
...
if ((PCI_FUNC(pdev->devfn) == 0) && !pdev->is_virtfn)
param->caps.has_ctrl = 1;
}No software check stops a sibling PF from being bound to another driver or passed through. The in-tree driver only requests one non-net vector, so this needs a misbehaving or out-of-tree peer driver. Whether such a peer can exist depends on the hardware and firmware claim, which the code does not show either way.
This is the first runtime path from a sibling PF into the control PF's shared allocator. Would a fair-share cap here help, such as pool size divided by max_pf, or one reserved other vector for each present PF?
Quoted 6 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dispatch.c +6
-0+/* Forward the resource errno verbatim to the requester */ +if (ret) + err = ret; + }else { + err = -EOPNOTSUPP ; + }
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261112094925.6098-1-illusion.wang%40nebula-matrix.com
illusion.wang
maintainerPATCH v34 7/8 net/nebula-matrix: add common/ctrl dev init/remove operation
In reply to illusion.wangFrom: illusion wang <[email protected]>
Add nbl_dev.c, nbl_dev.h and nbl_def_dev.h to host device-level initialization and teardown logic, and wire the new nbl_dev_init() / nbl_dev_remove() entry points into core init/remove.
Common device setup allocates per-device state, initializes the mailbox cleanup work and creates the mailbox channel queue. VSI/ETH identity lookup and MSI-X vector allocation are deferred to the subsequent start routine, so that the control PF mailbox responder is fully ready before any cross-PF RPC is issued and each commit in the series can be probed on its own.
Control-PF-only setup invokes chip-level module initialization and programs the mailbox QINFO routing table entries that map PF bus/devid to the mailbox queue.
Init/teardown ordering is dictated by a hardware constraint rather than by strict symmetry. Init: program mailbox QINFO (control PF only), create the mailbox queue, then run control device init_module. Teardown:
stop the mailbox channel and drain all in-flight DMA first, then call deinit_module(). This ordering prevents the firmware from reclaiming the routing under a live transfer, which would leave the device DMAing into released memory.
The firmware reclaims per-PF state (mailbox QINFO routing, qinfo registers and the like) asynchronously once driver status is marked inactive. It does not revert the chip-wide datapath registers configured by init_module; those stay valid until chip reset (see nbl_hw_deinit_module()). The device link created during non-control PF probe makes the driver core unbind all sibling PFs before the control PF is removed.
--- .../net/ethernet/nebula-matrix/nbl/Makefile | 1 + .../net/ethernet/nebula-matrix/nbl/nbl_core.h | 1 + .../nebula-matrix/nbl/nbl_core/nbl_dev.c | 305 ++++++++++++++++++ .../nebula-matrix/nbl/nbl_core/nbl_dev.h | 55 ++++ .../nebula-matrix/nbl/nbl_core/nbl_dispatch.c | 22 +- .../nbl/nbl_include/nbl_def_dev.h | 14 + .../net/ethernet/nebula-matrix/nbl/nbl_main.c | 9 + 7 files changed, 406 insertions(+), 1 deletion(-) create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dev.c create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dev.h create mode 100644 drivers/net/ethernet/nebula-matrix/nbl/nbl_include/nbl_def_dev.h
drivers/net/ethernet/nebula-matrix/nbl/Makefile +1 -0
@@ -11,4 +11,5 @@ nbl-objs += nbl_common/nbl_common.o \ nbl_hw/nbl_interrupt.o \ nbl_hw/nbl_chip.o \ nbl_core/nbl_dispatch.o \ + nbl_core/nbl_dev.o \ nbl_main.o
drivers/net/ethernet/nebula-matrix/nbl/nbl_core.h +1 -0
@@ -25,6 +25,7 @@ struct nbl_core {struct nbl_hw_mgt *hw_mgt;struct nbl_resource_mgt *res_mgt;struct nbl_dispatch_mgt *disp_mgt; +struct nbl_dev_mgt *dev_mgt;struct nbl_channel_mgt *chan_mgt; };
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dev.c new file +305 -0
305 lines not on this page · raw message
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dev.h new file +55 -0
55 lines not on this page · raw message
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dispatch.c +21 -1
@@ -647,5 +647,25 @@ int nbl_disp_init(struct nbl_adapter *adapter)void nbl_disp_remove (struct nbl_adapter *adapter) { -/* Dispatch structures are allocated via devm */ +/* +* Dispatch structures are devm-allocated and freed at detach. +* +* The five responders registered by nbl_disp_setup_msg() are +* owned by the channel layer (xarray of handlers) and are never +* unregistered here. This is safe because the teardown order +* guarantees no responder can run after this point: +* +* mailbox teardown +* -> cancel_work_sync(clean_mbx_task) // drain RX work +* -> nbl_chan_teardown_queue() // stop HW queue, +* // join clean task, +* // active=false +* -> nbl_chan_remove_common() +* -> nbl_common_destroy_wq() // no new work +* -> nbl_chan_remove_msg_handler() // free handler nodes +* +* By the time devres frees disp_mgt, the mailbox queue is stopped +* and the handler xarray is empty, so no responder can touch +* res_mgt->intr_mgt after nbl_intr_mgt_stop() has cleared it. +*/ }
drivers/net/ethernet/nebula-matrix/nbl/nbl_include/nbl_def_dev.h new file +14 -0
@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (c) 2026 Nebula Matrix Limited. +*/ + +#ifndef _NBL_DEF_DEV_H_ +#define _NBL_DEF_DEV_H_ + +struct nbl_adapter ; + +int nbl_dev_init (struct nbl_adapter *adapter); +void nbl_dev_remove (struct nbl_adapter *adapter); + +#endif
drivers/net/ethernet/nebula-matrix/nbl/nbl_main.c +9 -0
@@ -12,6 +12,7 @@#include "nbl_include/nbl_def_hw.h" #include "nbl_include/nbl_def_resource.h" #include "nbl_include/nbl_def_dispatch.h" +#include "nbl_include/nbl_def_dev.h" #include "nbl_include/nbl_def_common.h" #include "nbl_core.h"
@@ -53,7 +54,14 @@ struct nbl_adapter *nbl_core_init(struct pci_dev *pdev, ret =nbl_disp_init (adapter);if (ret) goto out_res_remove; + + ret =nbl_dev_init (adapter); +if (ret) + goto out_disp_remove;return adapter; + +out_disp_remove: +nbl_disp_remove (adapter); out_res_remove:nbl_res_remove_leonis (adapter); out_chan_remove:
@@ -66,6 +74,7 @@ out:void nbl_core_remove (struct nbl_adapter *adapter) { +nbl_dev_remove (adapter);nbl_disp_remove (adapter);nbl_res_remove_leonis (adapter);nbl_chan_remove_common (adapter);
Signature · 1 line
2.51.0.windows.1
[email protected]
AI reviewRe: [PATCH v34 net-next 7/8] net/nebula-matrix: add common/ctrl dev init/remove operation
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider.
Critical: 0 · High: 0 · Medium: 2 · 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] This patch's teardown ordering assumes that no sibling PF can probe while the control PF runs nbl_dev_remove().
- [Medium] This patch adds nbl_dev_init() to nbl_core_init(), and nbl_probe() calls pci_set_master() only after nbl_core_init() returns.
- [Low] The comment block added above nbl_dev_remove_ctrl_dev() (drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dev.c, lines…
- [Low] The commit message and the code comment added by the same patch give opposite reasons for deferring the VSI/ETH identity lookup.
--
net/nebula-matrix: add common/ctrl dev init/remove operation
This commit adds nbl_dev.c and related headers that provide nbl_dev_init() and nbl_dev_remove(), and wires them into nbl_core_init() and nbl_core_remove(). Common setup creates the mailbox clean work and the mailbox channel queue. The control PF also programs the mailbox QINFO routing and runs init_module(), and calls deinit_module() after the mailbox has been torn down.
Quoted 5 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dev.c new file +0
-0
[ ... ]
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dev.c line 135
Quoted 10 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dev.c new file +10
-0+nbl_dev_register_chan_task (dev_mgt,NBL_CHAN_TYPE_MAILBOX , + &common_dev->clean_mbx_task); +/* +* VSI/ETH identity is deliberately not fetched here but in the +* start path, which is where it is used: the IDs only feed MSI-X +* and queue setup. Responder availability is not what defers it - +* nbl_probe_chip_deps() keeps a sibling in -EPROBE_DEFER until +* func 0 is bound, and driver_bound() only runs after func 0's +* whole probe, this function included, has returned. +*/
[Severity: Low]
The commit message gives the opposite reason for deferring the VSI/ETH lookup:
"VSI/ETH identity lookup and MSI-X vector allocation are deferred to the subsequent start routine, so that the control PF mailbox responder is fully ready before any cross-PF RPC is issued"
This comment, on the other hand, says responder availability is not why the lookup is deferred.
Later in the series this comment is unchanged. The nbl_dev_start() comment added there argues that responder readiness (func 0's set_mailbox_irq(true) at the end of its start path) does matter.
Which of these is the real reason? Could the commit message and this comment be made to agree?
[ ... ]
Quoted 8 lines
+/*
+ * Tear down control device:deinit_modulesets driver_status=false
+ * to notify firmware to clean all per-PF hardware state (including
+ * qinfo registers). The qinfo map programmed innbl_dev_init()via
+ *nbl_dev_setup_chan_qinfo()is not explicitly cleared; firmware
+ * handles it ondriver_statuschange.
+ *
+ * Teardown ordering guarantee: every non-management PF creates a
Quoted 8 lines
+ *
consumer->controlPF device link in its probe path, so the driver
+ * core always unbinds all siblings before allowing the control PF to
+ * be detached (sysfs unbind, driver unregister and hot-unplug alike).
+ *nbl_probe_chip_deps()defers a sibling's probe until the control PF
+ * is fully bound (DL_DEV_DRIVER_BOUND), and holds the management PF's
+ * device lock across both that check anddevice_link_add(), so a
+ * supplier cannot slip toDL_DEV_NO_DRIVERorDL_DEV_PROBINGin
+ * between. A sibling therefore cannot
[Severity: Low]
Is this an accurate description of nbl_probe_chip_deps()? The function itself says it does not take the management PF's device lock:
drivers/net/ethernet/nebula-matrix/nbl/nbl_main.c:nbl_probe_chip_deps() {
...
/*
* Deliberately lock-free. This probe already runs under
* device_lock(funcN), while __pci_bus_lock() (AER recovery, slot
* reset) walks the bus in devfn order, i.e. 0 -> N: taking
* device_lock(func0) here would establish the opposite order, and
...
if (READ_ONCE(mgt->dev.driver) != pdev->dev.driver ||
!device_is_bound(&mgt->dev)) {
...
link = device_link_add(&pdev->dev, &mgt->dev,
DL_FLAG_AUTOPROBE_CONSUMER);
...
if (READ_ONCE(mgt->dev.driver) != pdev->dev.driver ||
!device_is_bound(&mgt->dev)) {
...
}There is no device_lock(&mgt->dev) call. The check reads dev.driver and device_is_bound(), not links.status.
driver_bound() sets knode_driver with klist_add_tail() before device_links_driver_bound() moves links.status to DL_DEV_DRIVER_BOUND. So the check can accept a supplier that is still DL_DEV_PROBING.
This comment is still the same at the end of the series. Could it be updated to match the lock-free implementation in nbl_main.c?
Quoted 3 lines
+ * reach its mailbox setup before the control PF is fully probed, nor
+ * race this deinit afterwards - the link forces it to be unbound
+ * first. The link is a persistent managed link with
[Severity: Medium]
Does the link really keep a sibling from racing this deinit? The commit message makes the same claim:
"The device link created during non-control PF probe makes the driver core unbind all sibling PFs before the control PF is removed."
The checks in nbl_probe_chip_deps() don't seem to catch a control PF that is already unbinding. dev->driver and knode_driver both stay set for the whole of ->remove():
drivers/base/dd.c:__device_release_driver() {
...
device_remove(dev);
...
device_unbind_cleanup(dev);
device_links_driver_cleanup(dev);
klist_remove(&dev->p->knode_driver);
...
}Suppose func 0 is unbound through sysfs while a sibling with no link yet probes (a first bind, a driver_override switch, or a deferred-probe retry):
func 0 (sysfs unbind)__device_release_driver()
device_links_busy()
links.status = DL_DEV_UNBINDING /* no busy consumer link */
device_remove()
nbl_remove()->nbl_core_remove()->nbl_dev_remove()
nbl_dev_remove_common_dev()
nbl_dev_remove_ctrl_dev()->deinit_module()sibling funcN (probing concurrently)device_links_check_suppliers() /* no link yet, passes */nbl_probe_chip_deps()
dev.driver / device_is_bound() check passes
device_link_add()
device_link_init_status()
link->status = DL_STATE_SUPPLIER_UNBIND re-check with the same predicates passes
nbl_core_init()->nbl_dev_init()->nbl_dev_setup_common_dev()
nbl_dev_setup_chan_queue() /* sibling mailbox enabled */
The sibling's mailbox setup then overlaps func 0's mailbox teardown and deinit_module(). deinit_module() sets driver_status=false, which lets firmware reclaim the QINFO routing.
When the sibling's probe finishes, device_links_driver_bound() hits WARN_ON(link->status != DL_STATE_CONSUMER_PROBE) and forces the link to ACTIVE. func 0's device_links_driver_cleanup() then hits WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND) and sets the link DORMANT.
On the next bind of func 0 the link becomes AVAILABLE while the sibling is still bound. A later unbind of func 0 then sees device_links_busy() return false and removes the control PF with the sibling still bound.
A sibling that already has a link from an earlier probe is deferred by device_links_check_suppliers() while the link is SUPPLIER_UNBIND, so this seems limited to a sibling's first probe. nbl_probe_chip_deps() comes from earlier in this series and is unchanged at the end of it.
Is there anything that orders a sibling's first probe against func 0's unbind here?
[ ... ]
Quoted 4 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_main.c +0
-0
[ ... ]
drivers/net/ethernet/nebula-matrix/nbl/nbl_main.c struct nbl_adapter *nbl_core_init(struct pci_dev *pdev, · line 61
Quoted 9 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_main.c +4
-0@@ -53,7 +54,14 @@ struct nbl_adapter *nbl_core_init(struct pci_dev *pdev, ret =nbl_disp_init (adapter);if (ret) goto out_res_remove; + + ret =nbl_dev_init (adapter); +if (ret) + goto out_disp_remove;return adapter;
[Severity: Medium]
With nbl_dev_init() now called from nbl_core_init(), is the mailbox DMA engine armed before bus mastering is enabled? nbl_probe() calls pci_set_master() only after nbl_core_init() has returned:
drivers/net/ethernet/nebula-matrix/nbl/nbl_main.c:nbl_probe() {
...
* point is host-side setup plus MMIO and nothing in it waits for
* the device, so it does not need bus mastering.
*/
err = nbl_probe_chip_deps(pdev, param.caps.has_ctrl);
...
adapter = nbl_core_init(pdev, ¶m);
...
pci_set_master(pdev);
...
}The chain nbl_dev_init()->nbl_dev_setup_common_dev()-> nbl_dev_setup_chan_queue()->nbl_chan_setup_queue() programs the ring addresses with QUEUE_EN and gives the RX descriptors to the device:
drivers/net/ethernet/nebula-matrix/nbl/nbl_channel/nbl_channel.c:nbl_chan_setup_queue() {
...
nbl_chan_config_queue(chan_mgt, chan_info, true); /* tx */
nbl_chan_config_queue(chan_mgt, chan_info, false); /* rx */
nbl_chan_update_tail_ptr(hw_ops, chan_mgt->hw_ops_tbl->priv,
rxq->tail_ptr, NBL_MB_RX_QID);
WRITE_ONCE(chan_info->active, true);
...
}This runs on both the control PF and the other PFs. The nbl_probe() comment saying nothing before pci_set_master() needs bus mastering is no longer true after this patch. The ordering is still the same at the end of the series.
With Bus Master Enable clear, the function cannot issue memory requests. Depending on the mailbox engine, an RX descriptor prefetch after the tail doorbell could stall, be dropped, or latch an error. Nothing re-kicks or re-validates the queue after pci_set_master().
Does this hardware hold such requests until bus mastering is enabled? If it doesn't, can the queue state drift away from the driver's rings while chan_info->active is already true?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261112094925.6098-1-illusion.wang%40nebula-matrix.com
illusion.wang
maintainerPATCH v34 8/8 net/nebula-matrix: add common dev start/stop operation
In reply to illusion.wangFrom: illusion wang <[email protected]>
Add nbl_dev_start() and nbl_dev_stop(), called from nbl_probe() and nbl_remove() via nbl_core_start() / nbl_core_stop().
Start configures the device MSI-X map, fetches the VSI/ETH ids, allocates the MSI-X vectors and arms the mailbox interrupt; stop performs the matching teardown.
nbl_probe() now rejects a function with neither the ctrl nor the net capability with -EOPNOTSUPP, before any hardware is touched. Such a function would reach nbl_dev_start() with every dispatch op left NULL by nbl_disp_refresh_ctrl_ops(). The state is only reachable throughdriver_override: binding this driver to a function that is absent from nbl_id_table makes pci_match_device() pass the dummy ID whose driver_data 0 leaves both capabilities clear. That means a VF, or a foreign device at a function other than 0 - a PF of this device always matches nbl_id_table, and new_id rejects a driver_data the table does not contain. A foreign non-VF device at function 0 is unaffected:nbl_get_func_param() gives it has_ctrl, so it is not rejected here and still runs nbl_core_init() and nbl_core_start() with the local control dispatch ops. nbl_dev_init()'s unwind labels change with it:setup_ctrl_dev_fail becomes out_remove_common_dev, and the setup_err path drops its label and returns directly, since a failed nbl_dev_setup_common_dev() has nothing to unwind.
Startup RPC errors are propagated unchanged. A -ETIMEDOUT here is a real failure, not a "supplier not ready yet" condition: nbl_probe() already deferred this PF in nbl_probe_chip_deps() until the control PF reached DL_DEV_DRIVER_BOUND, and the driver core only marks a device bound once its entire probe - including the control PF's own nbl_core_start() - has returned. That state is what bounds the wait, not nbl_dev_init(): on the control PF inbound mailbox work only runs from clean_mbx_task, which is queued by the mailbox hardirq, by the task's own self-requeue, or by this function's polling sends, and the control PF uses the local dispatch ops so it sends none. The responder is armed by set_mailbox_irq(true) at the end of the peer's nbl_dev_start(). Relaxing the dependency to "func 0 finished nbl_dev_init()" would therefore let every sibling RPC sit out the full 5-6s polling wait. Converting the timeout to -EPROBE_DEFER would only hide it behind dev_dbg() and make every later bind pay that wait again.
Teardown releases the kernel MSI-X vectors before destroying the hardware pcompleter map. pci_free_irq_vectors() ends in pci_msix_shutdown(), which masks every descriptor by writing the MSI-X table and reading it back to flush the write; those accesses should reach the function while FUNCTION_MSIX_MAP is still valid. This mirrors start, where cfg_msix_map runs before pci_alloc_irq_vectors_affinity(). It is not a double free of the pcim_msi_release devres registered alongside pci_alloc_irq_vectors(): that action only calls pci_free_irq_vectors() again, and pci_disable_msix() returns early once msix_enabled is clear. Start and stop still each run once, tied to probe and remove.
The rest of teardown is best-effort. The MSI-X map is torn down even when the configure RPC failed: a timeout only means the ACK never arrived, while the responder runs to completion before ACKing, so the entry, the intr_net/other_bmap bits and the kcalloc()ed vector array stay owned by the control PF. Those are host resources of that driver, not firmware state, and neither recovery path needs a chip reset: a later successful configuration of this function releases the old bits, frees the old array, invalidates and rewrites the entry on its had_config path, and nbl_intr_mgt_stop() clears the FUNCTION_MSIX_MAP valid bit for every still-configured function when the control PF is removed. The coherent map table is different: neither per-function path frees it, it is kept for the function's lifetime and reused in place, and nbl_intr_mgt_stop() is the only place it is released.
That is also what keeps the stop ordering safe. nbl_remove() calls nbl_core_stop() before pci_clear_master(), so the control PF destroys its own map entry while its Bus Master Enable is still set. That is
deliberate: nbl_core_stop() cannot run after pci_clear_master(), because on a non-control PF it is a mailbox RPC, which needs bus mastering for the ring DMA. The destroy only clears the VALID bit and keeps the coherent table, so the actual free still happens later, in nbl_intr_mgt_stop(), after pci_clear_master(). nbl_probe()'s start-failure unwind uses the same order. pci_clear_master() remains no safety net for a non-control PF: it cannot stop DMA issued with the management PF's BDF.
--- .../net/ethernet/nebula-matrix/nbl/nbl_core.h | 11 + .../nebula-matrix/nbl/nbl_core/nbl_dev.c | 446 +++++++++++++++++- .../nbl/nbl_include/nbl_def_dev.h | 2 + .../net/ethernet/nebula-matrix/nbl/nbl_main.c | 49 +- 4 files changed, 502 insertions(+), 6 deletions(-)
drivers/net/ethernet/nebula-matrix/nbl/nbl_core.h +11 -0
@@ -40,4 +40,15 @@ struct nbl_adapter *nbl_core_init(struct pci_dev *pdev, struct nbl_init_param *param);void nbl_core_remove (struct nbl_adapter *adapter); +/* +* Single-shot start/stop pair, called once each from PCI probe/remove. +* Not repeatable, but not because the MSI-X core would refuse a second +* start: nbl_dev_stop() releases the vectors with pci_free_irq_vectors(). +* What the pair brackets is the device's active lifetime, and +* nbl_core_stop() is only ever followed by nbl_core_remove(), which +* destroys the mailbox channel and the resource state that start needs. +*/ +int nbl_core_start (struct nbl_adapter *adapter); +void nbl_core_stop (struct nbl_adapter *adapter); +#endif
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dev.c +442 -4
487 lines not on this page · raw message
drivers/net/ethernet/nebula-matrix/nbl/nbl_include/nbl_def_dev.h +2 -0
@@ -10,5 +10,7 @@ struct nbl_adapter;int nbl_dev_init (struct nbl_adapter *adapter);void nbl_dev_remove (struct nbl_adapter *adapter); +int nbl_dev_start (struct nbl_adapter *adapter); +void nbl_dev_stop (struct nbl_adapter *adapter);#endif
drivers/net/ethernet/nebula-matrix/nbl/nbl_main.c +47 -2
79 lines not on this page · raw message
Signature · 1 line
2.51.0.windows.1
[email protected]
AI reviewRe: [PATCH v34 net-next 8/8] net/nebula-matrix: add common dev start/stop operation
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider.
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] net/nebula-matrix: unbind race and managed link del in nbl_probe_chip_deps()
- [Low] This patch's commit message, and the new comment in nbl_dev_stop() (around nbl_dev.c:729), say the coherent MSI-X map…
- [Low] The comment on nbl_dev_clear_interrupt_scheme() (nbl_dev.c:260) says "The handler is expected to be gone by now: both callers run…
- [Low] A comment in nbl_res_remove_leonis() (nbl_resource_leonis.c:358-360) says "Sibling PFs are already unbound…
- [Low] The kernel-doc of nbl_res_intr_cfg_msix_map() (nbl_interrupt.c:361-363) says "The in-tree caller…
--
net/nebula-matrix: add common dev start/stop operation
This adds nbl_dev_start() and nbl_dev_stop(), which nbl_probe() and nbl_remove() call through nbl_core_start() and nbl_core_stop(). Start configures the MSI-X map, fetches the VSI/ETH ids, allocates the MSI-X vectors and arms the mailbox interrupt, and stop undoes those steps. nbl_probe() also now rejects a function that has neither the ctrl nor the net capability.
Quoted 4 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dev.c +0
-0
[ ... ]
Quoted 2 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dev.c +2
-0+static int nbl_dev_cfg_msix_map (struct nbl_dev_mgt *dev_mgt) +{
[ ... ]
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dev.c static void nbl_dev_init_msix_cnt(struct nbl_dev_mgt *dev_mgt) · line 194
Quoted 6 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dev.c +6
-0+ err = disp_ops->cfg_msix_map (dev_mgt->disp_ops_tbl->priv, + msix_net_num, msix_not_net_num, + mask_en); + +return err; +}
[Severity: Low]
This isn't a bug, but nbl_dev_cfg_msix_map() is now the in-tree caller of cfg_msix_map. The kernel-doc of nbl_res_intr_cfg_msix_map() in nbl_interrupt.c names a different function:
* in-tree caller (nbl_dev_init_msix_cnt()) asks for one non-net vector
* per PF, far below NBL_MAX_OTHER_INTERRUPT.
nbl_dev_init_msix_cnt() only sets serv_info[NBL_MSIX_MAILBOX_TYPE].num = 1 and never calls cfg_msix_map.
Should the kernel-doc point to nbl_dev_cfg_msix_map(), reached from nbl_dev_start(), instead?
[ ... ]
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dev.c static void nbl_dev_init_msix_cnt(struct nbl_dev_mgt *dev_mgt) · line 268
Quoted 9 lines
+ * The handler is expected to be gone by now: both callers run
+ *nbl_dev_free_mailbox_irq()first.
+ */drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dev.c +6
-0+static void nbl_dev_clear_interrupt_scheme (struct nbl_dev_mgt *dev_mgt) +{ +struct nbl_common_info *common = dev_mgt->common; + +pci_free_irq_vectors (common->pdev); +}
[Severity: Low]
Is this comment accurate for the nbl_dev_start() rollback? Five of the error exits in nbl_dev_start() jump straight to err_destroy_map. That label calls nbl_dev_clear_interrupt_scheme() without nbl_dev_free_mailbox_irq():
ret = nbl_dev_cfg_msix_map(dev_mgt); if (ret) goto err_destroy_map; ... ret = nbl_dev_request_mailbox_irq(dev_mgt); if (ret) goto err_destroy_map;
The same applies to the get_vsi_id, get_eth_id and nbl_dev_init_interrupt_scheme() failures in between. Only the nbl_dev_enable_mailbox_irq() failure goes through err_free_irq.
On those direct paths request_irq() was either never reached or failed, so no handler is registered and nothing goes wrong today.
Could the comment describe that case instead of saying that both callers free the IRQ first?
[ ... ]
Quoted 1 line
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dev.c +0
-0@@ -303,3 +559,185 @@ void nbl_dev_remove(struct nbl_adapter *adapter)
[ ... ]
Quoted 2 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dev.c +2
-0+void nbl_dev_stop (struct nbl_adapter *adapter) +{
[ ... ]
Quoted 17 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dev.c +17
-0+/* +* Destroy the hardware MSI-X map: masks all device vectors and +* clears the pcompleter map entry. +* +* Best-effort: if the destroy RPC fails, the control PF still owns +* this function's map entry, its intr_net/other_bmap bits and its +* kcalloc()ed vector array. Those are host resources of that +* driver, released either by a later successful configuration of +* this function - nbl_res_intr_cfg_msix_map() invalidates and +* rewrites the entry, releases the old bits and frees the old array +* on its had_config path - or by nbl_intr_mgt_stop() when the +* control PF is removed, which walks every +* NBL_INTR_FUNC_CONFIGURED function through +* __nbl_res_intr_prepare_destroy_msix_map() and clears the +* FUNCTION_MSIX_MAP valid bit. The coherent map table is neither +* freed nor reallocated by reconfiguration: it is kept for the +* function's lifetime and released only by nbl_intr_mgt_stop().
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dev.c void nbl_dev_remove(struct nbl_adapter *adapter) · line 731
Quoted 10 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dev.c +10
-0+* Both rewrite the hardware entry without a chip reset; nothing else +* collects it, so a stranded one just stays as it is, and nothing +* requests one. +* +* pci_clear_master() on a non-control PF cannot stop DMA using the +* management PF's BDF. It has not run yet on the control PF either, +* since nbl_remove() clears it after this function returns; what +* makes the only free - the one in nbl_intr_mgt_stop() - safe is +* that it runs after that point, with bus mastering off. +*/
[Severity: Low]
Is "the only free" accurate here? The commit message makes the same claim:
The coherent map table is different: neither per-function path frees it, it is kept for the function's lifetime and reused in place, and nbl_intr_mgt_stop() is the only place it is released.
nbl_res_intr_cfg_msix_map() is a per-function path. Its release_vecs_unlock error label frees the table while the control PF is still bus master:
if (!had_config && official_tbl->base_addr) {
dma_free_coherent(dev, official_tbl->size,
official_tbl->base_addr,
official_tbl->dma);had_config is false on a first configuration. It is also false after a destroy, because __nbl_res_intr_complete_destroy_msix_map() keeps base_addr but resets the state:
func_res->state = NBL_INTR_FUNC_IDLE;
Suppose a sibling PF is configured again after an earlier destroy. cfg_msix_map reuses the kept table, which the pcompleter has already used. If a later vector allocation then fails with -EAGAIN, the error path frees that table.
That contradicts the label's own comment ("release the DMA table allocated during this call"). It also contradicts the keep-for-lifetime rule documented in complete_destroy.
With the in-tree vector counts (one non-net vector per PF) the bitmap pools can't run out, so the stop ordering added here is still safe today.
Should the error path free the table only when this call allocated it? Alternatively, should the commit message and this comment be adjusted?
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dev.c void nbl_dev_remove(struct nbl_adapter *adapter) · line 735
Quoted 4 lines
drivers/net/ethernet/nebula-matrix/nbl/nbl_core/nbl_dev.c +4
-0+ ret =nbl_dev_destroy_msix_map (dev_mgt); +if (ret) +dev_err (dev_mgt->common->dev, +"Failed to destroy MSI-X map: %d\n" , ret);
[Severity: Medium]
This patch doesn't change nbl_probe_chip_deps(), but with nbl_core_start() a sibling PF now sends cfg_msix_map, get_vsi_id and get_eth_id to func 0 from inside its own probe. The ordering that nbl_probe_chip_deps() is meant to guarantee therefore matters more now. Does it hold if func 0 is unbound while a sibling PF is probing?
In __device_release_driver(), device_links_busy() moves func 0's links.status to DL_DEV_UNBINDING before device_remove() calls nbl_remove(). Two things are only cleared after device_remove()
returns: mgt->dev.driver, by device_unbind_cleanup(), and knode_driver, by klist_remove(). So for the whole of func 0's nbl_core_stop() and nbl_core_remove(), the post-check in nbl_probe_chip_deps() still passes:
if (READ_ONCE(mgt->dev.driver) != pdev->dev.driver ||
!device_is_bound(&mgt->dev)) {In that window, device_link_add() sees the supplier in DL_DEV_UNBINDING and creates the link in DL_STATE_SUPPLIER_UNBIND. nbl_probe_chip_deps() returns 0 anyway. The sibling then runs nbl_core_init() and nbl_dev_start() against a control PF that is disabling its mailbox and heading into nbl_intr_mgt_stop().
device_links_unbind_consumers() has already run on func 0, so nothing unbinds the sibling afterwards. If the sibling's probe does complete, device_links_driver_bound() hits itsWARN_ON(link->status != DL_STATE_CONSUMER_PROBE).
The other outcome of the race is also wrong. If func 0 has finished nbl_remove() by the time of the post-check, the check fires and calls device_link_del() on a link created with DL_FLAG_AUTOPROBE_CONSUMER. That is a managed link, and the consumer is registered, so device_link_put_kref() takes its "Unable to drop a managed device link reference" WARN branch and leaves the link in place.
Triggering this needs root to unbind func 0 while a sibling probes. The window is all of nbl_remove(), though, which sleeps during the mailbox and interrupt teardown.
Could nbl_probe_chip_deps() check link->status on the link it just added, and defer unless it is DL_STATE_CONSUMER_PROBE? A link in that state makes device_links_busy() wait for the sibling's probe before func 0 is unbound.
And should the defer path keep the managed link instead of calling device_link_del() on it? That would let the driver core reprobe the sibling when func 0 binds again.
[Severity: Low]
This destroy RPC can fail, for example with a polling-mode -ETIMEDOUT. The comment above says the sibling's entry then stays NBL_INTR_FUNC_CONFIGURED until nbl_intr_mgt_stop() collects it.
Does that make the existing comment in nbl_res_remove_leonis() inaccurate?
* Sibling PFs are already unbound by the device-link ordering
* so no remote PF still has a live map entry or a running
* mailbox RPC at this point.
After this patch, a sibling that unbound after a failed destroy still has a valid map entry when nbl_res_remove_leonis() runs.
The code handles this, because nbl_intr_mgt_stop() walks every CONFIGURED function. Could the leonis comment be updated so that a later change doesn't drop that walk based on it?
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261112094925.6098-1-illusion.wang%40nebula-matrix.com
Jakub Kicinski
maintainerQuoted 2 lines · On Thu, 12 Nov 2026 17:49:12 +0800 illusion.wang wrote:
This series continues and refines the previous v28 NBL NIC driver patch set for Nebula-matrix m18110/m18000 (SNIC s1000) Ethernet adapters.
Please wait for human review on v35.
If you reach v40 we will ask you to go away with this driver..