Netdev List
 help / color / mirror / Atom feed
From: Wei Hu <weh@linux.microsoft.com>
To: longli@kernel.org, kotaranov@microsoft.com, kuba@kernel.org,
	davem@davemloft.net, pabeni@redhat.com, edumazet@google.com,
	andrew+netdev@lunn.ch, jgg@ziepe.ca, leon@kernel.org,
	haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com,
	shradhagupta@linux.microsoft.com, horms@kernel.org,
	ernis@linux.microsoft.com, stephen@networkplumber.org
Cc: netdev@vger.kernel.org, linux-rdma@vger.kernel.org,
	linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org,
	gargaditya@linux.microsoft.com, kees+treewide@kernel.org,
	kees@kernel.org, mawasthi@linux.microsoft.com, weh@microsoft.com,
	edumazet@kernel.org
Subject: [PATCH net-next v13 4/5] net: mana: support concurrent HWC requests
Date: Tue,  1 Dec 2026 04:04:00 +0000	[thread overview]
Message-ID: <hwc-v13.ab4ca86c3bf24a51ad8a84cc8a0220d1.4@linux.microsoft.com> (raw)
In-Reply-To: <hwc-v13.ab4ca86c3bf24a51ad8a84cc8a0220d1.0@linux.microsoft.com>

From: Long Li <longli@microsoft.com>
Date: Mon, 7 Sep 2026 20:51:57 -0700

Serialize SQ posts, not response waits. Keep each message ID and TX
buffer until both its sender and its response are done. Close admission
and drain senders before dismantling the channel.

The PF retires old client DMA and interrupts when the VF does not receive
a posted command's reply within the normal PF response interval. Check
that deadline before accepting a reply, not just when the sender runs.
Leave responses already accepted by the VF alone.

If the timeout covers all submissions, stop posting and wake the senders
of still-unanswered commands. A later doorbell can cross the deadline
even under the SQ lock; its command keeps its own result and timeout.
Let established-object teardown finish locally on a covered timeout,
without manufacturing a PF reply. Neither admission delay nor a reset
notification provides that guarantee. Keep HWC mappings until the PF
acknowledges DESTROY_HWC.

Fixes: ca9c54d2d6a5 ("net: mana: Add a driver for Microsoft Azure Network Adapter (MANA)")
Signed-off-by: Long Li <longli@microsoft.com>
Signed-off-by: Wei Hu <weh@microsoft.com>
---
 drivers/infiniband/hw/mana/main.c             |   3 +-
 drivers/infiniband/hw/mana/mr.c               |   6 +-
 .../net/ethernet/microsoft/mana/gdma_cdx.c    |   2 +
 .../net/ethernet/microsoft/mana/gdma_main.c   |  99 ++-
 .../net/ethernet/microsoft/mana/gdma_pci.c    |  12 +-
 .../net/ethernet/microsoft/mana/hw_channel.c  | 587 ++++++++++++------
 include/net/mana/gdma.h                       |  14 +
 include/net/mana/hw_channel.h                 |  42 +-
 8 files changed, 517 insertions(+), 248 deletions(-)

diff --git a/drivers/infiniband/hw/mana/main.c b/drivers/infiniband/hw/mana/main.c
index 858bbc325eb29fbb677499e251941a5909ccf29d..60a9f15e5e8c1d0c807ded7177d8da909a0626da 100644
--- a/drivers/infiniband/hw/mana/main.c
+++ b/drivers/infiniband/hw/mana/main.c
@@ -100,7 +100,8 @@ static int mana_gd_destroy_pd(struct mana_ib_dev *mdev, u64 pd_handle)
 
 	req.pd_handle = pd_handle;
 
-	return mana_gd_send_request(mdev_to_gc(mdev), sizeof(req), &req, sizeof(resp), &resp);
+	return mana_gd_send_teardown_request(mdev_to_gc(mdev), sizeof(req),
+					     &req, sizeof(resp), &resp);
 }
 
 int mana_ib_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
diff --git a/drivers/infiniband/hw/mana/mr.c b/drivers/infiniband/hw/mana/mr.c
index 1233685b4b8906210a7648823265bccb6efaf578..9810aee3381e81fb63b96b73f8249d2cb629ad3b 100644
--- a/drivers/infiniband/hw/mana/mr.c
+++ b/drivers/infiniband/hw/mana/mr.c
@@ -94,7 +94,8 @@ static int mana_ib_gd_destroy_mr(struct mana_ib_dev *dev, u64 mr_handle)
 
 	req.mr_handle = mr_handle;
 
-	return mana_gd_send_request(gc, sizeof(req), &req, sizeof(resp), &resp);
+	return mana_gd_send_teardown_request(gc, sizeof(req), &req,
+					     sizeof(resp), &resp);
 }
 
 struct ib_mr *mana_ib_reg_user_mr(struct ib_pd *ibpd, u64 start, u64 length,
@@ -428,7 +429,8 @@ static int mana_ib_gd_destroy_dm(struct mana_ib_dev *mdev, struct mana_ib_dm *dm
 	mana_gd_init_req_hdr(&req.hdr, GDMA_DESTROY_DM, sizeof(req), sizeof(resp));
 	req.dm_handle = dm->dm_handle;
 
-	return mana_gd_send_request(gc, sizeof(req), &req, sizeof(resp), &resp);
+	return mana_gd_send_teardown_request(gc, sizeof(req), &req,
+					     sizeof(resp), &resp);
 }
 
 int mana_ib_dealloc_dm(struct ib_dm *ibdm, struct uverbs_attr_bundle *attrs)
diff --git a/drivers/net/ethernet/microsoft/mana/gdma_cdx.c b/drivers/net/ethernet/microsoft/mana/gdma_cdx.c
index 2e0dcb902d7e263fad7341a1756441ec10d8339d..1f124cfbdefdcde94f9ea3d0e78eb1e9170d77c2 100644
--- a/drivers/net/ethernet/microsoft/mana/gdma_cdx.c
+++ b/drivers/net/ethernet/microsoft/mana/gdma_cdx.c
@@ -221,6 +221,8 @@ static int mana_cdx_gd_probe(struct cdx_device *cdx_dev)
 
 	mutex_init(&gc->eq_test_event_mutex);
 	mutex_init(&gc->gic_mutex);
+	spin_lock_init(&gc->hwc_lock);
+	init_waitqueue_head(&gc->hwc_drain_waitq);
 	dev_set_drvdata(&cdx_dev->dev, gc);
 	gc->bar0_pa = cdx_resource_start(cdx_dev, 0);
 	gc->bar0_size = cdx_resource_len(cdx_dev, 0);
diff --git a/drivers/net/ethernet/microsoft/mana/gdma_main.c b/drivers/net/ethernet/microsoft/mana/gdma_main.c
index 6eea3f6df8801df9d15ab74a242de66b8d49e692..c921a4065daf98c11accdaa2daaee923144cba18 100644
--- a/drivers/net/ethernet/microsoft/mana/gdma_main.c
+++ b/drivers/net/ethernet/microsoft/mana/gdma_main.c
@@ -142,22 +142,21 @@ int mana_gd_init_registers(struct gdma_context *gc)
 		return mana_gd_init_vf_regs(gc);
 }
 
-/* Suppress logging when we set timeout to zero */
+/* Once admission is closed, follow-on request failures are expected. */
 bool mana_need_log(struct gdma_context *gc, int err)
 {
 	struct hw_channel_context *hwc;
+	unsigned long flags;
+	bool log;
 
-	if (err != -ETIMEDOUT)
-		return true;
-
-	if (!gc)
+	if (!gc || (err != -ETIMEDOUT && err != -ENODEV))
 		return true;
 
+	spin_lock_irqsave(&gc->hwc_lock, flags);
 	hwc = gc->hwc.driver_data;
-	if (hwc && hwc->hwc_timeout == 0)
-		return false;
-
-	return true;
+	log = hwc && READ_ONCE(hwc->state) == HWC_READY;
+	spin_unlock_irqrestore(&gc->hwc_lock, flags);
+	return log;
 }
 
 int mana_gd_query_max_resources(struct gdma_context *gc)
@@ -310,20 +309,29 @@ int mana_gd_query_max_resources(struct gdma_context *gc)
 	return 0;
 }
 
-static int mana_gd_query_hwc_timeout(struct gdma_context *gc, u32 *timeout_val)
+static int mana_gd_query_hwc_timeout(struct gdma_context *gc)
 {
+	struct hw_channel_context *hwc = gc->hwc.driver_data;
 	struct gdma_query_hwc_timeout_resp resp = {};
 	struct gdma_query_hwc_timeout_req req = {};
 	int err;
 
 	mana_gd_init_req_hdr(&req.hdr, GDMA_QUERY_HWC_TIMEOUT,
 			     sizeof(req), sizeof(resp));
-	req.timeout_ms = *timeout_val;
+	scoped_guard(spinlock_irqsave, &hwc->inflight_msg_res.lock) {
+		hwc->timeout_query = true;
+		req.timeout_ms = hwc->hwc_timeout;
+	}
 	err = mana_gd_send_request(gc, sizeof(req), &req, sizeof(resp), &resp);
 	if (err || resp.hdr.status)
 		return err ? err : -EPROTO;
 
-	*timeout_val = resp.timeout_ms;
+	guard(spinlock_irqsave)(&hwc->inflight_msg_res.lock);
+	/* Don't overwrite a newer reconfiguration event. */
+	if (hwc->timeout_query && resp.timeout_ms)
+		hwc->hwc_timeout = resp.timeout_ms;
+	hwc->timeout_query = false;
+	dev_dbg(gc->dev, "set the hwc timeout to %u\n", hwc->hwc_timeout);
 
 	return 0;
 }
@@ -375,15 +383,47 @@ int mana_gd_detect_devices(struct gdma_context *gc)
 	return gc->mana.dev_id.type == 0 ? -ENODEV : 0;
 }
 
+static int mana_gd_send(struct gdma_context *gc, u32 req_len, const void *req,
+			u32 resp_len, void *resp, bool teardown)
+{
+	struct hw_channel_context *hwc;
+	unsigned long flags;
+	int err;
+
+	spin_lock_irqsave(&gc->hwc_lock, flags);
+	hwc = gc->hwc.driver_data;
+	if (!hwc) {
+		spin_unlock_irqrestore(&gc->hwc_lock, flags);
+		return -ENODEV;
+	}
+	hwc->active_senders++;
+	spin_unlock_irqrestore(&gc->hwc_lock, flags);
+
+	err = mana_hwc_send_request(hwc, req_len, req, resp_len, resp,
+				    teardown);
+
+	spin_lock_irqsave(&gc->hwc_lock, flags);
+	if (--hwc->active_senders == 0)
+		wake_up(&gc->hwc_drain_waitq);
+	spin_unlock_irqrestore(&gc->hwc_lock, flags);
+
+	return err;
+}
+
 int mana_gd_send_request(struct gdma_context *gc, u32 req_len, const void *req,
 			 u32 resp_len, void *resp)
 {
-	struct hw_channel_context *hwc = gc->hwc.driver_data;
-
-	return mana_hwc_send_request(hwc, req_len, req, resp_len, resp);
+	return mana_gd_send(gc, req_len, req, resp_len, resp, false);
 }
 EXPORT_SYMBOL_NS(mana_gd_send_request, "NET_MANA");
 
+int mana_gd_send_teardown_request(struct gdma_context *gc, u32 req_len,
+				  const void *req, u32 resp_len, void *resp)
+{
+	return mana_gd_send(gc, req_len, req, resp_len, resp, true);
+}
+EXPORT_SYMBOL_NS(mana_gd_send_teardown_request, "NET_MANA");
+
 int mana_gd_alloc_memory(struct gdma_context *gc, unsigned int length,
 			 struct gdma_mem_info *gmi, bool allow_scatter)
 {
@@ -1016,7 +1056,9 @@ static void mana_gd_deregister_irq(struct gdma_queue *queue)
 	synchronize_rcu();
 }
 
-int mana_gd_test_eq(struct gdma_context *gc, struct gdma_queue *eq)
+static int mana_gd_test_eq_request(struct gdma_context *gc,
+				   struct hw_channel_context *hwc,
+				   struct gdma_queue *eq)
 {
 	struct gdma_generate_test_event_req req = {};
 	struct gdma_general_resp resp = {};
@@ -1034,9 +1076,14 @@ int mana_gd_test_eq(struct gdma_context *gc, struct gdma_queue *eq)
 	req.hdr.dev_id = eq->gdma_dev->dev_id;
 	req.queue_index = eq->id;
 
-	err = mana_gd_send_request(gc, sizeof(req), &req, sizeof(resp), &resp);
+	if (hwc)
+		err = mana_hwc_send_request(hwc, sizeof(req), &req,
+					    sizeof(resp), &resp, false);
+	else
+		err = mana_gd_send_request(gc, sizeof(req), &req,
+					   sizeof(resp), &resp);
 	if (err) {
-		if (mana_need_log(gc, err))
+		if (hwc || mana_need_log(gc, err))
 			dev_err(dev, "test_eq failed: %d\n", err);
 		goto out;
 	}
@@ -1065,6 +1112,16 @@ out:
 	return err;
 }
 
+int mana_gd_test_eq(struct gdma_context *gc, struct gdma_queue *eq)
+{
+	return mana_gd_test_eq_request(gc, NULL, eq);
+}
+
+int mana_gd_test_hwc_eq(struct hw_channel_context *hwc, struct gdma_queue *eq)
+{
+	return mana_gd_test_eq_request(hwc->gdma_dev->gdma_context, hwc, eq);
+}
+
 static void mana_gd_destroy_eq(struct gdma_context *gc, bool flush_events,
 			       struct gdma_queue *queue)
 {
@@ -1300,6 +1357,7 @@ static int mana_gd_create_dma_region(struct gdma_dev *gd,
 	if (gmi->nr_pages == 0 && !MANA_PAGE_ALIGNED(gmi->virt_addr))
 		return -EINVAL;
 
+	/* The caller must keep the HWC alive throughout queue creation. */
 	hwc = gc->hwc.driver_data;
 	req_msg_size = struct_size(req, page_addr_list, num_page);
 	if (req_msg_size > hwc->max_req_msg_size)
@@ -1501,10 +1559,8 @@ int mana_gd_verify_vf_version(struct gdma_context *gc)
 {
 	struct gdma_verify_ver_resp resp = {};
 	struct gdma_verify_ver_req req = {};
-	struct hw_channel_context *hwc;
 	int err;
 
-	hwc = gc->hwc.driver_data;
 	mana_gd_init_req_hdr(&req.hdr, GDMA_VERIFY_VF_DRIVER_VERSION,
 			     sizeof(req), sizeof(resp));
 
@@ -1542,12 +1598,11 @@ int mana_gd_verify_vf_version(struct gdma_context *gc)
 			   &gc->pf_cap_flags1);
 
 	if (resp.pf_cap_flags1 & GDMA_DRV_CAP_FLAG_1_HWC_TIMEOUT_RECONFIG) {
-		err = mana_gd_query_hwc_timeout(gc, &hwc->hwc_timeout);
+		err = mana_gd_query_hwc_timeout(gc);
 		if (err) {
 			dev_err(gc->dev, "Failed to set the hwc timeout %d\n", err);
 			return err;
 		}
-		dev_dbg(gc->dev, "set the hwc timeout to %u\n", hwc->hwc_timeout);
 	}
 	return 0;
 }
diff --git a/drivers/net/ethernet/microsoft/mana/gdma_pci.c b/drivers/net/ethernet/microsoft/mana/gdma_pci.c
index 1cfeb6997372704428b7cc51250cf98cf74ab41e..ec435382b9c94d3e63c91036ae2cb13af158b9c3 100644
--- a/drivers/net/ethernet/microsoft/mana/gdma_pci.c
+++ b/drivers/net/ethernet/microsoft/mana/gdma_pci.c
@@ -14,7 +14,6 @@
 #include <linux/xarray.h>
 
 #include <net/mana/mana.h>
-#include <net/mana/hw_channel.h>
 
 struct mana_dev_recovery {
 	struct list_head list;
@@ -84,7 +83,6 @@ out:
 /* Return true if recovery needs to escalate to PCI re-enumeration. */
 static bool mana_serv_reset(struct pci_dev *pdev)
 {
-	struct hw_channel_context *hwc;
 	struct gdma_context *gc;
 	int ret;
 
@@ -93,14 +91,14 @@ static bool mana_serv_reset(struct pci_dev *pdev)
 		return false;
 
 	gc = pci_get_drvdata(pdev);
-	hwc = gc->hwc.driver_data;
-	if (!hwc) {
+	if (!gc->hwc.driver_data) {
 		dev_err(&pdev->dev, "MANA service: no HWC\n");
 		goto out;
 	}
 
-	/* HWC is not responding in this case, so don't wait */
-	hwc->hwc_timeout = 0;
+	/* A service notification is not a DMA fence. Keep the normal HWC
+	 * response window for cleanup, including when all slots are in use.
+	 */
 
 	dev_info(&pdev->dev, "MANA reset cycle start\n");
 
@@ -581,6 +579,8 @@ static int mana_gd_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 
 	mutex_init(&gc->eq_test_event_mutex);
 	mutex_init(&gc->gic_mutex);
+	spin_lock_init(&gc->hwc_lock);
+	init_waitqueue_head(&gc->hwc_drain_waitq);
 	pci_set_drvdata(pdev, gc);
 	gc->bar0_pa = pci_resource_start(pdev, 0);
 	gc->bar0_size = pci_resource_len(pdev, 0);
diff --git a/drivers/net/ethernet/microsoft/mana/hw_channel.c b/drivers/net/ethernet/microsoft/mana/hw_channel.c
index a98e58b83f9c75bf8b40183c73a9286670d4aef4..5c2385c4e162078fcbc9c12b14eac079d189cb25 100644
--- a/drivers/net/ethernet/microsoft/mana/hw_channel.c
+++ b/drivers/net/ethernet/microsoft/mana/hw_channel.c
@@ -6,48 +6,107 @@
 #include <net/mana/hw_channel.h>
 #include <linux/vmalloc.h>
 
-static int mana_hwc_get_msg_index(struct hw_channel_context *hwc, u16 *msg_id)
+/* Only this private result permits local teardown without a PF response. */
+enum { HWC_DMA_RETIRED = 1 };
+
+static void mana_hwc_finish_pending(struct hw_channel_context *hwc, int err)
+{
+	struct gdma_resource *r = &hwc->inflight_msg_res;
+	unsigned int i;
+
+	lockdep_assert_held(&r->lock);
+	for_each_set_bit(i, r->map, r->size) {
+		struct hwc_caller_ctx *ctx = &hwc->caller_ctx[i];
+
+		if (!ctx->pending || ctx->error != -EINPROGRESS)
+			continue;
+		/* Keep the ID and TX buffer until the response or teardown. */
+		ctx->error = err;
+		complete(&ctx->comp_event);
+	}
+}
+
+static void mana_hwc_expire(struct hw_channel_context *hwc,
+			    struct hwc_caller_ctx *ctx, ktime_t now)
+{
+	lockdep_assert_held(&hwc->inflight_msg_res.lock);
+
+	if (!ctx->pending || ctx->error != -EINPROGRESS ||
+	    ktime_before(now, ktime_add_ms(ctx->posted_at, ctx->timeout_ms)))
+		return;
+
+	ctx->error = -ETIMEDOUT;
+	complete(&ctx->comp_event);
+
+	/* If the VF hasn't received a response for the full PF interval,
+	 * old client DMA and interrupts are drained and old MRs revoked,
+	 * even if the PF wrote a reply that the VF didn't receive in time.
+	 * Unanswered old creates cannot grant access later. HWC memory
+	 * still needs DESTROY_HWC.
+	 *
+	 * Start the wait after the doorbell. To cover an independent post,
+	 * it must have finished before even the earliest possible expiry.
+	 * The lock alone doesn't prevent a post from crossing the deadline.
+	 */
+	if (hwc->state == HWC_READY &&
+	    (hwc->last_msg_id == ctx->msg_id ||
+	     ktime_before(hwc->last_post, ctx->earliest_expiry))) {
+		WRITE_ONCE(hwc->state, HWC_TIMED_OUT);
+		mana_hwc_finish_pending(hwc, HWC_DMA_RETIRED);
+		wake_up_all(&hwc->admission_waitq);
+	}
+}
+
+static void mana_hwc_check_timeouts(struct hw_channel_context *hwc, ktime_t now)
+{
+	struct gdma_resource *r = &hwc->inflight_msg_res;
+	unsigned int i;
+
+	for_each_set_bit(i, r->map, r->size)
+		mana_hwc_expire(hwc, &hwc->caller_ctx[i], now);
+}
+
+static int mana_hwc_try_get_msg_index(struct hw_channel_context *hwc,
+				      void *resp, u32 resp_len,
+				      struct hwc_caller_ctx **caller_ctx)
 {
 	struct gdma_resource *r = &hwc->inflight_msg_res;
 	struct hwc_caller_ctx *ctx;
-	unsigned long flags;
 	u32 index;
 
-	down(&hwc->sema);
-
-	spin_lock_irqsave(&r->lock, flags);
+	guard(spinlock_irqsave)(&r->lock);
+	if (hwc->state == HWC_TIMED_OUT)
+		return HWC_DMA_RETIRED;
+	if (hwc->state != HWC_READY)
+		return -ENODEV;
 
-	index = find_first_zero_bit(hwc->inflight_msg_res.map,
-				    hwc->inflight_msg_res.size);
+	index = find_first_zero_bit(r->map, r->size);
+	if (index >= r->size)
+		return -EBUSY;
 
 	ctx = &hwc->caller_ctx[index];
-	spin_lock(&ctx->lock);
 	reinit_completion(&ctx->comp_event);
-	ctx->output_buf = NULL;
-	ctx->output_buflen = 0;
+	ctx->output_buf = resp;
+	ctx->output_buflen = resp_len;
 	ctx->error = -EINPROGRESS;
 	ctx->status_code = 0;
-
-	bitmap_set(hwc->inflight_msg_res.map, index, 1);
-
-	spin_unlock(&ctx->lock);
-	spin_unlock_irqrestore(&r->lock, flags);
-
-	*msg_id = index;
-
+	ctx->msg_id = index;
+	ctx->sender = true;
+	__set_bit(index, r->map);
+	*caller_ctx = ctx;
 	return 0;
 }
 
-static void mana_hwc_put_msg_index(struct hw_channel_context *hwc, u16 msg_id)
+static void mana_hwc_put_slot(struct hw_channel_context *hwc,
+			      struct hwc_caller_ctx *ctx)
 {
 	struct gdma_resource *r = &hwc->inflight_msg_res;
-	unsigned long flags;
 
-	spin_lock_irqsave(&r->lock, flags);
-	bitmap_clear(hwc->inflight_msg_res.map, msg_id, 1);
-	spin_unlock_irqrestore(&r->lock, flags);
-
-	up(&hwc->sema);
+	lockdep_assert_held(&r->lock);
+	if (!ctx->sender && !ctx->pending) {
+		__clear_bit(ctx->msg_id, r->map);
+		wake_up(&hwc->admission_waitq);
+	}
 }
 
 static int mana_hwc_verify_resp_msg(const struct hwc_caller_ctx *caller_ctx,
@@ -90,40 +149,36 @@ static void mana_hwc_handle_resp(struct hw_channel_context *hwc, u32 resp_len,
 				 struct hwc_work_request *rx_req, u16 msg_id)
 {
 	const struct gdma_resp_hdr *resp_msg = rx_req->buf_va;
+	struct gdma_resource *r = &hwc->inflight_msg_res;
 	struct hwc_caller_ctx *ctx;
-	int err;
 
-	if (!test_bit(msg_id, hwc->inflight_msg_res.map)) {
+	spin_lock(&r->lock);
+	if (!test_bit(msg_id, r->map)) {
 		dev_err(hwc->dev, "hwc_rx: invalid msg_id = %u\n", msg_id);
-		mana_hwc_post_rx_wqe(hwc->rxq, rx_req);
-		return;
+		goto repost;
 	}
 
-	ctx = hwc->caller_ctx + msg_id;
-
-	spin_lock(&ctx->lock);
-	if (!ctx->output_buf) {
-		spin_unlock(&ctx->lock);
-		mana_hwc_post_rx_wqe(hwc->rxq, rx_req);
-		return;
-	}
-
-	err = mana_hwc_verify_resp_msg(ctx, resp_msg, resp_len);
-	if (!err) {
-		ctx->status_code = resp_msg->status;
-		memcpy(ctx->output_buf, resp_msg, resp_len);
+	ctx = &hwc->caller_ctx[msg_id];
+	if (!ctx->pending)
+		goto repost;
+
+	/* A peer's timeout may cover this reply before its waiter runs. */
+	mana_hwc_check_timeouts(hwc, ktime_get());
+	ctx->pending = false;
+	if (ctx->error == -EINPROGRESS) {
+		ctx->error = mana_hwc_verify_resp_msg(ctx, resp_msg, resp_len);
+		if (!ctx->error) {
+			ctx->status_code = resp_msg->status;
+			memcpy(ctx->output_buf, resp_msg, resp_len);
+		}
+		complete(&ctx->comp_event);
 	}
 
-	ctx->output_buf = NULL;
-	ctx->error = err;
-
-	/* Must post rx wqe before complete(), otherwise the next rx may
-	 * hit no_wqe error.
-	 */
+	mana_hwc_put_slot(hwc, ctx);
+repost:
+	/* Replenish the RQ before a sender can reuse this ID and TX buffer. */
 	mana_hwc_post_rx_wqe(hwc->rxq, rx_req);
-
-	complete(&ctx->comp_event);
-	spin_unlock(&ctx->lock);
+	spin_unlock(&r->lock);
 }
 
 static void mana_hwc_init_event_handler(void *ctx, struct gdma_queue *q_self,
@@ -227,7 +282,11 @@ static void mana_hwc_init_event_handler(void *ctx, struct gdma_queue *q_self,
 
 		switch (type) {
 		case HWC_DATA_CFG_HWC_TIMEOUT:
-			hwc->hwc_timeout = val;
+			if (val) {
+				guard(spinlock)(&hwc->inflight_msg_res.lock);
+				hwc->hwc_timeout = val;
+				hwc->timeout_query = false;
+			}
 			break;
 
 		case HWC_DATA_HW_LINK_CONNECT:
@@ -647,7 +706,7 @@ out:
 	return err;
 }
 
-static int mana_hwc_post_tx_wqe(const struct hwc_wq *hwc_txq,
+static int mana_hwc_post_tx_wqe(struct hwc_wq *hwc_txq,
 				struct hwc_work_request *req,
 				u32 dest_virt_rq_id, u32 dest_virt_rcq_id,
 				bool dest_pf)
@@ -687,6 +746,7 @@ static int mana_hwc_post_tx_wqe(const struct hwc_wq *hwc_txq,
 	req->wqe_req.client_data_unit = 0;
 
 	err = mana_gd_post_and_ring(hwc_txq->gdma_wq, &req->wqe_req, NULL);
+
 	if (err)
 		dev_err(dev, "Failed to post WQE on HWC SQ: %d\n", err);
 	return err;
@@ -697,51 +757,59 @@ static int mana_hwc_init_inflight_msg(struct hw_channel_context *hwc,
 {
 	int err;
 
-	sema_init(&hwc->sema, num_msg);
-
 	err = mana_gd_alloc_res_map(num_msg, &hwc->inflight_msg_res);
 	if (err)
 		dev_err(hwc->dev, "Failed to init inflight_msg_res: %d\n", err);
 	return err;
 }
 
-static int mana_hwc_test_channel(struct hw_channel_context *hwc, u16 q_depth,
-				 u32 max_req_msg_size, u32 max_resp_msg_size)
+static int mana_hwc_test_channel(struct hw_channel_context *hwc)
 {
-	struct gdma_context *gc = hwc->gdma_dev->gdma_context;
 	struct hwc_wq *hwc_rxq = hwc->rxq;
 	struct hwc_work_request *req;
 	struct hwc_caller_ctx *ctx;
+	unsigned long flags;
 	int err;
 	int i;
 
 	/* Post all WQEs on the RQ */
-	for (i = 0; i < q_depth; i++) {
+	for (i = 0; i < hwc->num_inflight_msg; i++) {
 		req = &hwc_rxq->msg_buf->reqs[i];
 		err = mana_hwc_post_rx_wqe(hwc_rxq, req);
 		if (err)
 			return err;
 	}
 
-	ctx = kzalloc_objs(*ctx, q_depth);
+	ctx = kzalloc_objs(*ctx, hwc->num_inflight_msg);
 	if (!ctx)
 		return -ENOMEM;
 
-	for (i = 0; i < q_depth; ++i) {
+	for (i = 0; i < hwc->num_inflight_msg; ++i)
 		init_completion(&ctx[i].comp_event);
-		spin_lock_init(&ctx[i].lock);
-	}
 
 	hwc->caller_ctx = ctx;
 
-	return mana_gd_test_eq(gc, hwc->cq->gdma_eq);
+	/* Setup owns hwc directly; runtime publication follows this test. */
+	spin_lock_irqsave(&hwc->inflight_msg_res.lock, flags);
+	WRITE_ONCE(hwc->state, HWC_READY);
+	spin_unlock_irqrestore(&hwc->inflight_msg_res.lock, flags);
+
+	err = mana_gd_test_hwc_eq(hwc, hwc->cq->gdma_eq);
+	if (err) {
+		spin_lock_irqsave(&hwc->inflight_msg_res.lock, flags);
+		WRITE_ONCE(hwc->state, HWC_STOPPED);
+		spin_unlock_irqrestore(&hwc->inflight_msg_res.lock, flags);
+	}
+
+	return err;
 }
 
-static int mana_hwc_establish_channel(struct gdma_context *gc, u16 *q_depth,
+static int mana_hwc_establish_channel(struct hw_channel_context *hwc,
+				      u16 *q_depth,
 				      u32 *max_req_msg_size,
 				      u32 *max_resp_msg_size)
 {
-	struct hw_channel_context *hwc = gc->hwc.driver_data;
+	struct gdma_context *gc = hwc->gdma_dev->gdma_context;
 	struct gdma_queue *rq = hwc->rxq->gdma_wq;
 	struct gdma_queue *sq = hwc->txq->gdma_wq;
 	struct gdma_queue *eq = hwc->cq->gdma_eq;
@@ -874,68 +942,77 @@ static void mana_hwc_clear_cq_table(struct gdma_context *gc)
 	vfree(cq_table);
 }
 
-int mana_hwc_create_channel(struct gdma_context *gc)
+static int mana_hwc_publish_channel(struct hw_channel_context *hwc)
+{
+	struct gdma_context *gc = hwc->gdma_dev->gdma_context;
+	unsigned long flags;
+	int err = 0;
+
+	spin_lock_irqsave(&gc->hwc_lock, flags);
+	if (WARN_ON_ONCE(gc->hwc.driver_data))
+		err = -EBUSY;
+	else
+		gc->hwc.driver_data = hwc;
+	spin_unlock_irqrestore(&gc->hwc_lock, flags);
+
+	return err;
+}
+
+static struct hw_channel_context *
+mana_hwc_unpublish_channel(struct gdma_context *gc)
 {
-	u32 max_req_msg_size, max_resp_msg_size;
-	struct gdma_dev *gd = &gc->hwc;
 	struct hw_channel_context *hwc;
-	u16 q_depth_max;
-	int err;
+	unsigned long flags;
 
-	/* Retry a retained context before assigning queues to the PF again. */
-	if (gd->driver_data) {
-		mana_hwc_destroy_channel(gc);
-		if (gd->driver_data)
-			return -ETIMEDOUT;
-	}
+	spin_lock_irqsave(&gc->hwc_lock, flags);
+	hwc = gc->hwc.driver_data;
+	gc->hwc.driver_data = NULL;
+	spin_unlock_irqrestore(&gc->hwc_lock, flags);
 
-	hwc = kzalloc_obj(*hwc);
-	if (!hwc)
-		return -ENOMEM;
+	return hwc;
+}
 
-	gd->gdma_context = gc;
-	gd->driver_data = hwc;
-	hwc->gdma_dev = gd;
-	hwc->dev = gc->dev;
-	hwc->hwc_timeout = HW_CHANNEL_WAIT_RESOURCE_TIMEOUT_MS;
+static void mana_hwc_retain_channel(struct hw_channel_context *hwc)
+{
+	struct gdma_context *gc = hwc->gdma_dev->gdma_context;
+	unsigned long flags;
 
-	/* HWC's instance number is always 0. */
-	gd->dev_id.as_uint32 = 0;
-	gd->dev_id.type = GDMA_DEVICE_HWC;
+	spin_lock_irqsave(&gc->hwc_lock, flags);
+	if (WARN_ON_ONCE(gc->hwc.driver_data))
+		dev_err(hwc->dev, "HWC retention slot is already occupied\n");
+	else
+		gc->hwc.driver_data = hwc;
+	spin_unlock_irqrestore(&gc->hwc_lock, flags);
+}
 
-	gd->pdid = INVALID_PDID;
-	gd->doorbell = INVALID_DOORBELL;
+static bool mana_hwc_senders_drained(struct gdma_context *gc,
+				     struct hw_channel_context *hwc)
+{
+	unsigned long flags;
+	bool drained;
 
-	/* mana_hwc_init_queues() only creates the required data structures,
-	 * and doesn't touch the HWC device.
-	 */
-	err = mana_hwc_init_queues(hwc, HW_CHANNEL_VF_BOOTSTRAP_QUEUE_DEPTH,
-				   HW_CHANNEL_MAX_REQUEST_SIZE,
-				   HW_CHANNEL_MAX_RESPONSE_SIZE);
-	if (err) {
-		dev_err(hwc->dev, "Failed to initialize HWC: %d\n", err);
-		goto out;
-	}
+	spin_lock_irqsave(&gc->hwc_lock, flags);
+	drained = hwc->active_senders == 0;
+	spin_unlock_irqrestore(&gc->hwc_lock, flags);
 
-	err = mana_hwc_establish_channel(gc, &q_depth_max, &max_req_msg_size,
-					 &max_resp_msg_size);
-	if (err) {
-		dev_err(hwc->dev, "Failed to establish HWC: %d\n", err);
-		goto out;
-	}
+	return drained;
+}
 
-	err = mana_hwc_test_channel(gc->hwc.driver_data,
-				    HW_CHANNEL_VF_BOOTSTRAP_QUEUE_DEPTH,
-				    max_req_msg_size, max_resp_msg_size);
-	if (err) {
-		dev_err(hwc->dev, "Failed to test HWC: %d\n", err);
-		goto out;
-	}
+static void mana_hwc_stop_channel(struct hw_channel_context *hwc)
+{
+	struct gdma_resource *r = &hwc->inflight_msg_res;
+	struct gdma_context *gc = hwc->gdma_dev->gdma_context;
 
-	return 0;
-out:
-	mana_hwc_destroy_channel(gc);
-	return err;
+	if (hwc->num_inflight_msg) {
+		guard(spinlock_irqsave)(&r->lock);
+		mana_hwc_check_timeouts(hwc, ktime_get());
+		if (hwc->state != HWC_TIMED_OUT) {
+			WRITE_ONCE(hwc->state, HWC_STOPPED);
+			mana_hwc_finish_pending(hwc, -ENODEV);
+		}
+	}
+	wake_up_all(&hwc->admission_waitq);
+	wait_event(gc->hwc_drain_waitq, mana_hwc_senders_drained(gc, hwc));
 }
 
 static void mana_hwc_fence_channel(struct gdma_context *gc,
@@ -951,13 +1028,12 @@ static void mana_hwc_fence_channel(struct gdma_context *gc,
 		mana_hwc_unpublish_cq(gc, hwc->cq->gdma_cq);
 }
 
-void mana_hwc_destroy_channel(struct gdma_context *gc)
+static bool mana_hwc_release_channel(struct hw_channel_context *hwc)
 {
-	struct hw_channel_context *hwc = gc->hwc.driver_data;
+	struct gdma_context *gc = hwc->gdma_dev->gdma_context;
 	int err;
 
-	if (!hwc)
-		return;
+	mana_hwc_stop_channel(hwc);
 
 	/* An unacknowledged destroy leaves the PF's mappings live.  Fence
 	 * software dispatch, but retain every PF-visible allocation.
@@ -969,7 +1045,8 @@ void mana_hwc_destroy_channel(struct gdma_context *gc)
 			"HWC teardown failed: %d, retaining PF-visible resources\n",
 			err);
 		mana_hwc_fence_channel(gc, hwc);
-		return;
+		mana_hwc_retain_channel(hwc);
+		return false;
 	}
 
 	mana_hwc_clear_cq_table(gc);
@@ -994,11 +1071,10 @@ void mana_hwc_destroy_channel(struct gdma_context *gc)
 	hwc->gdma_dev->doorbell = INVALID_DOORBELL;
 	hwc->gdma_dev->pdid = INVALID_PDID;
 
-	hwc->hwc_timeout = 0;
-
 	kfree(hwc);
-	gc->hwc.driver_data = NULL;
 	gc->hwc.gdma_context = NULL;
+
+	return true;
 }
 
 static void mana_hwc_free_retained_wq(struct hwc_wq *wq)
@@ -1016,12 +1092,12 @@ static void mana_hwc_free_retained_wq(struct hwc_wq *wq)
  */
 void mana_hwc_free_retained_channel(struct gdma_context *gc)
 {
-	struct hw_channel_context *hwc = gc->hwc.driver_data;
+	struct hw_channel_context *hwc = mana_hwc_unpublish_channel(gc);
 
 	if (!hwc)
 		return;
 
-	gc->hwc.driver_data = NULL;
+	mana_hwc_stop_channel(hwc);
 	mana_hwc_clear_cq_table(gc);
 
 	if (hwc->cq) {
@@ -1039,11 +1115,129 @@ void mana_hwc_free_retained_channel(struct gdma_context *gc)
 	gc->hwc.gdma_context = NULL;
 }
 
-static int mana_hwc_response_status(struct hw_channel_context *hwc,
-				    u32 command, int error, u32 status)
+static int mana_hwc_create_bootstrap_channel(struct hw_channel_context *hwc)
+{
+	u32 max_req_msg_size, max_resp_msg_size;
+	u16 q_depth_max;
+	int err;
+
+	err = mana_hwc_init_queues(hwc, HW_CHANNEL_VF_BOOTSTRAP_QUEUE_DEPTH,
+				   HW_CHANNEL_MAX_REQUEST_SIZE,
+				   HW_CHANNEL_MAX_RESPONSE_SIZE);
+	if (err) {
+		dev_err(hwc->dev, "Failed to initialize HWC: %d\n", err);
+		return err;
+	}
+
+	err = mana_hwc_establish_channel(hwc, &q_depth_max, &max_req_msg_size,
+					 &max_resp_msg_size);
+	if (err) {
+		dev_err(hwc->dev, "Failed to establish HWC: %d\n", err);
+		return err;
+	}
+
+	err = mana_hwc_test_channel(hwc);
+	if (err)
+		dev_err(hwc->dev, "Failed to test HWC: %d\n", err);
+
+	return err;
+}
+
+int mana_hwc_create_channel(struct gdma_context *gc)
 {
-	if (error)
-		return error;
+	struct gdma_dev *gd = &gc->hwc;
+	struct hw_channel_context *hwc;
+	int err;
+
+	/* Retry a retained context before assigning queues to the PF again. */
+	if (gd->driver_data) {
+		mana_hwc_destroy_channel(gc);
+		if (gd->driver_data)
+			return -ETIMEDOUT;
+	}
+
+	hwc = kzalloc_obj(*hwc);
+	if (!hwc)
+		return -ENOMEM;
+
+	gd->gdma_context = gc;
+	hwc->gdma_dev = gd;
+	hwc->dev = gc->dev;
+	WRITE_ONCE(hwc->hwc_timeout, HW_CHANNEL_WAIT_RESOURCE_TIMEOUT_MS);
+	init_waitqueue_head(&hwc->admission_waitq);
+
+	/* HWC's instance number is always 0. */
+	gd->dev_id.as_uint32 = 0;
+	gd->dev_id.type = GDMA_DEVICE_HWC;
+
+	gd->pdid = INVALID_PDID;
+	gd->doorbell = INVALID_DOORBELL;
+
+	err = mana_hwc_create_bootstrap_channel(hwc);
+	if (err) {
+		mana_hwc_release_channel(hwc);
+		return err;
+	}
+
+	err = mana_hwc_publish_channel(hwc);
+	if (err) {
+		mana_hwc_release_channel(hwc);
+		return err;
+	}
+
+	return 0;
+}
+
+void mana_hwc_destroy_channel(struct gdma_context *gc)
+{
+	struct hw_channel_context *hwc;
+
+	hwc = mana_hwc_unpublish_channel(gc);
+	if (!hwc)
+		return;
+
+	mana_hwc_release_channel(hwc);
+}
+
+static int mana_hwc_submit_request(struct hw_channel_context *hwc,
+				   struct hwc_caller_ctx *ctx,
+				   struct hwc_work_request *tx_wr)
+{
+	struct gdma_resource *r = &hwc->inflight_msg_res;
+	ktime_t start;
+	int err;
+
+	guard(spinlock_irqsave)(&r->lock);
+	if (hwc->state == HWC_STOPPED || !hwc->hwc_timeout)
+		return -ENODEV;
+
+	/* Check pending timers before ringing another doorbell, even if
+	 * their senders haven't been scheduled since the deadlines passed.
+	 */
+	mana_hwc_check_timeouts(hwc, ktime_get());
+	if (hwc->state == HWC_TIMED_OUT)
+		return HWC_DMA_RETIRED;
+
+	start = ktime_get();
+	err = mana_hwc_post_tx_wqe(hwc->txq, tx_wr, hwc->dest_vrq_id,
+				   hwc->dest_vrcq_id, false);
+	if (err)
+		return err;
+
+	ctx->timeout_ms = hwc->hwc_timeout;
+	ctx->earliest_expiry = ktime_add_ms(start, ctx->timeout_ms);
+	ctx->posted_at = ktime_get();
+	hwc->last_post = ctx->posted_at;
+	hwc->last_msg_id = ctx->msg_id;
+	ctx->pending = true;
+	return 0;
+}
+
+static int mana_hwc_response_result(struct hw_channel_context *hwc,
+				    u32 command, int err, u32 status)
+{
+	if (err)
+		return err;
 
 	if (!status || status == GDMA_STATUS_MORE_ENTRIES)
 		return 0;
@@ -1058,108 +1252,87 @@ static int mana_hwc_response_status(struct hw_channel_context *hwc,
 	return -EPROTO;
 }
 
-static void mana_hwc_finish_request(struct hw_channel_context *hwc,
-				    struct hwc_caller_ctx *ctx, u16 msg_id)
+static int mana_hwc_wait_for_response(struct hw_channel_context *hwc,
+				      struct hwc_caller_ctx *ctx,
+				      u32 command)
 {
-	unsigned long flags;
+	struct gdma_resource *r = &hwc->inflight_msg_res;
+	unsigned long flags, remaining;
+	ktime_t deadline, now;
+	u32 status;
+	int err;
 
-	spin_lock_irqsave(&ctx->lock, flags);
-	ctx->output_buf = NULL;
-	spin_unlock_irqrestore(&ctx->lock, flags);
+	spin_lock_irqsave(&r->lock, flags);
+	deadline = ktime_add_ms(ctx->posted_at, ctx->timeout_ms);
+	while (ctx->error == -EINPROGRESS) {
+		now = ktime_get();
+		mana_hwc_expire(hwc, ctx, now);
+		if (ctx->error != -EINPROGRESS)
+			break;
+		remaining = msecs_to_jiffies(ktime_ms_delta(deadline, now));
+		spin_unlock_irqrestore(&r->lock, flags);
+		wait_for_completion_timeout(&ctx->comp_event, remaining ?: 1);
+		spin_lock_irqsave(&r->lock, flags);
+	}
+	err = ctx->error;
+	status = ctx->status_code;
+	spin_unlock_irqrestore(&r->lock, flags);
 
-	mana_hwc_put_msg_index(hwc, msg_id);
+	if (err == -ETIMEDOUT) {
+		dev_err(hwc->dev, "Command 0x%x timed out: %u ms\n",
+			command, ctx->timeout_ms);
+		/* A posted destroy's own target predates its timeout, even
+		 * when an overlapping post precludes retiring the whole HWC.
+		 */
+		return HWC_DMA_RETIRED;
+	}
+	return mana_hwc_response_result(hwc, command, err, status);
 }
 
 int mana_hwc_send_request(struct hw_channel_context *hwc, u32 req_len,
-			  const void *req, u32 resp_len, void *resp)
+			  const void *req, u32 resp_len, void *resp,
+			  bool teardown)
 {
+	struct gdma_resource *r = &hwc->inflight_msg_res;
 	struct hwc_work_request *tx_wr;
-	struct hwc_wq *txq = hwc->txq;
 	struct gdma_req_hdr *req_msg;
 	struct hwc_caller_ctx *ctx;
-	unsigned long flags;
-	u32 dest_vrcq;
-	u32 dest_vrq;
 	u32 command;
-	u32 status;
-	u16 msg_id;
 	int err;
 
-	err = mana_hwc_get_msg_index(hwc, &msg_id);
+	wait_event(hwc->admission_waitq,
+		   (err = mana_hwc_try_get_msg_index(hwc, resp, resp_len,
+						    &ctx)) != -EBUSY);
 	if (err)
-		return err;
-
-	tx_wr = &txq->msg_buf->reqs[msg_id];
-	ctx = hwc->caller_ctx + msg_id;
+		goto out;
 
+	tx_wr = &hwc->txq->msg_buf->reqs[ctx->msg_id];
 	if (req_len > tx_wr->buf_len) {
 		dev_err(hwc->dev, "HWC: req msg size: %d > %d\n", req_len,
 			tx_wr->buf_len);
-		mana_hwc_finish_request(hwc, ctx, msg_id);
-		return -EINVAL;
+		err = -EINVAL;
+		goto put;
 	}
 
-	spin_lock_irqsave(&ctx->lock, flags);
-	ctx->output_buf = resp;
-	ctx->output_buflen = resp_len;
-	spin_unlock_irqrestore(&ctx->lock, flags);
-
-	req_msg = (struct gdma_req_hdr *)tx_wr->buf_va;
+	req_msg = tx_wr->buf_va;
 	if (req)
 		memcpy(req_msg, req, req_len);
-
-	req_msg->req.hwc_msg_id = msg_id;
+	req_msg->req.hwc_msg_id = ctx->msg_id;
 
 	tx_wr->msg_size = req_len;
 	command = req_msg->req.msg_type;
 
-	/* The hardware reports the HWC destination queues through
-	 * HWC_INIT_DATA_DEST_RQ_ID and HWC_INIT_DATA_DEST_CQ_ID, and
-	 * always supplies values that are valid for this function, so no
-	 * PF-specific handling is needed here.
-	 */
-	dest_vrq = hwc->dest_vrq_id;
-	dest_vrcq = hwc->dest_vrcq_id;
-
-	err = mana_hwc_post_tx_wqe(txq, tx_wr, dest_vrq, dest_vrcq, false);
-	if (err) {
-		dev_err(hwc->dev, "HWC: Failed to post send WQE: %d\n", err);
-		mana_hwc_finish_request(hwc, ctx, msg_id);
-		return err;
-	}
-
-	if (!wait_for_completion_timeout(&ctx->comp_event,
-					 msecs_to_jiffies(hwc->hwc_timeout))) {
-		spin_lock_irqsave(&ctx->lock, flags);
+	err = mana_hwc_submit_request(hwc, ctx, tx_wr);
+	if (!err)
+		err = mana_hwc_wait_for_response(hwc, ctx, command);
+put:
+	scoped_guard(spinlock_irqsave, &r->lock) {
 		ctx->output_buf = NULL;
-		err = ctx->error;
-		status = ctx->status_code;
-		spin_unlock_irqrestore(&ctx->lock, flags);
-
-		if (err != -EINPROGRESS) {
-			mana_hwc_finish_request(hwc, ctx, msg_id);
-			return mana_hwc_response_status(hwc, command, err,
-							status);
-		}
-
-		if (hwc->hwc_timeout != 0)
-			dev_err(hwc->dev, "Command 0x%x timed out: %u ms\n",
-				command, hwc->hwc_timeout);
-
-		/* Reduce further waiting if HWC no response */
-		if (hwc->hwc_timeout > 1)
-			hwc->hwc_timeout = 1;
-
-		mana_hwc_finish_request(hwc, ctx, msg_id);
-		return -ETIMEDOUT;
+		ctx->sender = false;
+		mana_hwc_put_slot(hwc, ctx);
 	}
-
-	spin_lock_irqsave(&ctx->lock, flags);
-	ctx->output_buf = NULL;
-	err = ctx->error;
-	status = ctx->status_code;
-	spin_unlock_irqrestore(&ctx->lock, flags);
-
-	mana_hwc_finish_request(hwc, ctx, msg_id);
-	return mana_hwc_response_status(hwc, command, err, status);
+out:
+	if (err == HWC_DMA_RETIRED)
+		return teardown ? 0 : -ETIMEDOUT;
+	return err;
 }
diff --git a/include/net/mana/gdma.h b/include/net/mana/gdma.h
index 0f3f38f4c52acf7d68d79e3c551efb79cd5caff8..849c68a1bc9bdf5b535cbe11d59e6502531b2a36 100644
--- a/include/net/mana/gdma.h
+++ b/include/net/mana/gdma.h
@@ -548,6 +548,12 @@ struct gdma_context {
 	/* Hardware communication channel (HWC) */
 	struct gdma_dev		hwc;
 
+	/* Sender drain; the final wakeup runs under hwc_lock. */
+	wait_queue_head_t	hwc_drain_waitq;
+
+	/* Protects HWC publication and active sender references. */
+	spinlock_t		hwc_lock;
+
 	/* Azure network adapter */
 	struct gdma_dev		mana;
 
@@ -1201,6 +1207,14 @@ void mana_gd_free_memory(struct gdma_mem_info *gmi);
 int mana_gd_send_request(struct gdma_context *gc, u32 req_len, const void *req,
 			 u32 resp_len, void *resp);
 
+/* For software teardown of an object whose lifetime excludes HWC replacement.
+ * If the PF's response timeout has retired the object, return zero for
+ * local cleanup without filling resp or implying a PF response.
+ * Callers needing response fields must use mana_gd_send_request().
+ */
+int mana_gd_send_teardown_request(struct gdma_context *gc, u32 req_len,
+				  const void *req, u32 resp_len, void *resp);
+
 int mana_gd_destroy_dma_region(struct gdma_context *gc, u64 dma_region_handle);
 void mana_register_debugfs(void);
 void mana_unregister_debugfs(void);
diff --git a/include/net/mana/hw_channel.h b/include/net/mana/hw_channel.h
index bcaf276976a81dca825ee08804a3b2fa5ee24a9a..6ff445da89f443f06eec4d95fbab33eab0486f3e 100644
--- a/include/net/mana/hw_channel.h
+++ b/include/net/mana/hw_channel.h
@@ -4,6 +4,8 @@
 #ifndef _HW_CHANNEL_H
 #define _HW_CHANNEL_H
 
+#include <linux/ktime.h>
+
 #define DEFAULT_LOG2_THROTTLING_FOR_ERROR_EQ  4
 
 #define HW_CHANNEL_MAX_REQUEST_SIZE  0x1000
@@ -166,15 +168,25 @@ struct hwc_wq {
 	struct hwc_cq *hwc_cq;
 };
 
+enum hwc_state {
+	HWC_STOPPED,
+	HWC_READY,
+	HWC_TIMED_OUT,
+};
+
+/* inflight_msg_res.lock protects the slot and the SQ producer. */
 struct hwc_caller_ctx {
 	struct completion comp_event;
-	/* Protects initialization, output storage and response state. */
-	spinlock_t lock;
 	void *output_buf;
 	u32 output_buflen;
-
-	int error; /* Linux error code */
+	int error;
 	u32 status_code;
+	u16 msg_id;
+	bool sender;
+	bool pending;
+	ktime_t earliest_expiry;
+	ktime_t posted_at;
+	u32 timeout_ms;
 };
 
 struct hw_channel_context {
@@ -197,18 +209,25 @@ struct hw_channel_context {
 	struct hwc_wq *txq;
 	struct hwc_cq *cq;
 
-	struct semaphore sema;
+	/* Wait for a free slot or channel shutdown. */
+	wait_queue_head_t admission_waitq;
 	struct gdma_resource inflight_msg_res;
+	ktime_t last_post;
+	u16 last_msg_id;
+	enum hwc_state state;
 
 	u32 dest_vrq_id;
 	u32 dest_vrcq_id;
+	/* Normal response window, sampled at submission under the SQ lock. */
 	u32 hwc_timeout;
+	bool timeout_query;
 
-	/* Once ESTABLISH_HWC submits the queue addresses, a setup error
-	 * does not release them. The PF may still use the rings and buffers
-	 * until it acknowledges DESTROY_HWC.
-	 */
+	/* The PF may own the HWC queues while this is true. */
 	bool setup_active;
+
+	/* mana_gd_send_request() callers, including waiters; under hwc_lock. */
+	unsigned int active_senders;
+
 	struct hwc_caller_ctx *caller_ctx;
 };
 
@@ -217,6 +236,9 @@ void mana_hwc_destroy_channel(struct gdma_context *gc);
 void mana_hwc_free_retained_channel(struct gdma_context *gc);
 
 int mana_hwc_send_request(struct hw_channel_context *hwc, u32 req_len,
-			  const void *req, u32 resp_len, void *resp);
+			  const void *req, u32 resp_len, void *resp,
+			  bool teardown);
+int mana_gd_test_hwc_eq(struct hw_channel_context *hwc,
+			struct gdma_queue *eq);
 
 #endif /* _HW_CHANNEL_H */

  parent reply	other threads:[~2026-12-01  4:04 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01  4:03 [PATCH net-next v13 0/5] net: mana: concurrent HWC requests Wei Hu
2026-12-01  4:03 ` [PATCH net-next v13 1/5] net: mana: validate the HWC doorbell before ringing it Wei Hu
2026-12-01  4:03 ` [PATCH net-next v13 2/5] net: mana: keep HWC queues until the PF acknowledges teardown Wei Hu
2026-12-01  4:03 ` [PATCH net-next v13 3/5] net: mana: protect the HWC response buffer until the sender returns Wei Hu
2026-12-01  4:04 ` Wei Hu [this message]
2026-12-01  4:04 ` [PATCH net-next v13 5/5] net: mana: use the reported HWC queue depth Wei Hu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=hwc-v13.ab4ca86c3bf24a51ad8a84cc8a0220d1.4@linux.microsoft.com \
    --to=weh@linux.microsoft.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=decui@microsoft.com \
    --cc=edumazet@google.com \
    --cc=edumazet@kernel.org \
    --cc=ernis@linux.microsoft.com \
    --cc=gargaditya@linux.microsoft.com \
    --cc=haiyangz@microsoft.com \
    --cc=horms@kernel.org \
    --cc=jgg@ziepe.ca \
    --cc=kees+treewide@kernel.org \
    --cc=kees@kernel.org \
    --cc=kotaranov@microsoft.com \
    --cc=kuba@kernel.org \
    --cc=leon@kernel.org \
    --cc=linux-hyperv@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=longli@kernel.org \
    --cc=mawasthi@linux.microsoft.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=shradhagupta@linux.microsoft.com \
    --cc=stephen@networkplumber.org \
    --cc=weh@microsoft.com \
    --cc=wei.liu@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox