public inbox for linux-usb@vger.kernel.org
 help / color / mirror / Atom feed
From: Mathias Nyman <mathias.nyman@linux.intel.com>
To: <gregkh@linuxfoundation.org>
Cc: <linux-usb@vger.kernel.org>,
	Mathias Nyman <mathias.nyman@linux.intel.com>
Subject: [PATCH 02/23] xhci: Add helper to find trb from its dma address
Date: Wed, 19 Nov 2025 16:23:56 +0200	[thread overview]
Message-ID: <20251119142417.2820519-3-mathias.nyman@linux.intel.com> (raw)
In-Reply-To: <20251119142417.2820519-1-mathias.nyman@linux.intel.com>

Add a xhci_dma_to_trb() helper, and use it to find the transfer TRB
early in handle_tx_event() based on the dma address found in the
event TRB.

With this helper we can avoid using 'ep_seg' transfer TRB segment
variable as both a a boolean to indicate if the transfer TRB is part
of the next queued TD, and to actually find the transfer TRB based
on ep_seg and ep_trb_dma.

This is a first step in reworking and cleaning up trb_in_td() and
handle_tx_event()

Signed-off-by: Mathias Nyman <mathias.nyman@linux.intel.com>
---
 drivers/usb/host/xhci-ring.c | 28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)

diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
index 8e209aa33ea7..7fccca7e1c62 100644
--- a/drivers/usb/host/xhci-ring.c
+++ b/drivers/usb/host/xhci-ring.c
@@ -82,6 +82,23 @@ dma_addr_t xhci_trb_virt_to_dma(struct xhci_segment *seg,
 	return seg->dma + (segment_offset * sizeof(*trb));
 }
 
+static union xhci_trb *xhci_dma_to_trb(struct xhci_segment *start_seg,
+				       dma_addr_t dma,
+				       struct xhci_segment **match_seg)
+{
+	struct xhci_segment *seg;
+
+	xhci_for_each_ring_seg(start_seg, seg) {
+		if (in_range(dma, seg->dma, TRB_SEGMENT_SIZE)) {
+			if (match_seg)
+				*match_seg = seg;
+			return &seg->trbs[(dma - seg->dma) / sizeof(union xhci_trb)];
+		}
+	}
+
+	return NULL;
+}
+
 static bool trb_is_noop(union xhci_trb *trb)
 {
 	return TRB_TYPE_NOOP_LE32(trb->generic.field[3]);
@@ -2658,7 +2675,6 @@ static int handle_tx_event(struct xhci_hcd *xhci,
 	int ep_index;
 	struct xhci_td *td = NULL;
 	dma_addr_t ep_trb_dma;
-	struct xhci_segment *ep_seg;
 	union xhci_trb *ep_trb;
 	int status = -EINPROGRESS;
 	struct xhci_ep_ctx *ep_ctx;
@@ -2689,6 +2705,9 @@ static int handle_tx_event(struct xhci_hcd *xhci,
 	if (!ep_ring)
 		return handle_transferless_tx_event(xhci, ep, trb_comp_code);
 
+	/* find the transfer trb this events points to */
+	ep_trb = xhci_dma_to_trb(ep_ring->deq_seg, ep_trb_dma, NULL);
+
 	/* Look for common error cases */
 	switch (trb_comp_code) {
 	/* Skip codes that require special handling depending on
@@ -2862,10 +2881,8 @@ static int handle_tx_event(struct xhci_hcd *xhci,
 		td = list_first_entry(&ep_ring->td_list, struct xhci_td,
 				      td_list);
 
-		/* Is this a TRB in the currently executing TD? */
-		ep_seg = trb_in_td(td, ep_trb_dma);
-
-		if (!ep_seg) {
+		/* Is this TRB not part of the currently executing TD? */
+		if (!trb_in_td(td, ep_trb_dma)) {
 
 			if (ep->skip && usb_endpoint_xfer_isoc(&td->urb->ep->desc)) {
 				/* this event is unlikely to match any TD, don't skip them all */
@@ -2948,7 +2965,6 @@ static int handle_tx_event(struct xhci_hcd *xhci,
 	if (ring_xrun_event)
 		return 0;
 
-	ep_trb = &ep_seg->trbs[(ep_trb_dma - ep_seg->dma) / sizeof(*ep_trb)];
 	trace_xhci_handle_transfer(ep_ring, (struct xhci_generic_trb *) ep_trb, ep_trb_dma);
 
 	/*
-- 
2.43.0


  parent reply	other threads:[~2025-11-19 14:24 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-19 14:23 [PATCH 00/23] xhci features for usb-next Mathias Nyman
2025-11-19 14:23 ` [PATCH 01/23] usb: xhci: limit run_graceperiod for only usb 3.0 devices Mathias Nyman
2025-11-19 14:23 ` Mathias Nyman [this message]
2025-11-19 14:23 ` [PATCH 03/23] xhci: simplify and rework trb_in_td() Mathias Nyman
2025-11-19 14:23 ` [PATCH 04/23] usb: xhci: rework xhci_decode_portsc() Mathias Nyman
2025-11-19 14:23 ` [PATCH 05/23] usb: xhci: add tracing for PORTSC register writes Mathias Nyman
2025-11-19 14:24 ` [PATCH 06/23] usb: xhci: add helper to read PORTSC register Mathias Nyman
2025-11-19 14:24 ` [PATCH 07/23] usb: xhci: add USB Port Register Set struct Mathias Nyman
2025-11-19 14:24 ` [PATCH 08/23] usb: xhci: implement " Mathias Nyman
2025-11-19 14:24 ` [PATCH 09/23] usb: xhci: Assume that endpoints halt as specified Mathias Nyman
2025-11-19 14:24 ` [PATCH 10/23] usb: xhci: Don't unchain link TRBs on quirky HCs Mathias Nyman
2025-11-19 14:24 ` [PATCH 11/23] usb: xhci: replace use of system_wq with system_percpu_wq Mathias Nyman
2025-11-19 14:24 ` [PATCH 12/23] usb: xhci: remove deprecated TODO comment Mathias Nyman
2025-11-19 14:24 ` [PATCH 13/23] usb: xhci: remove unused trace operation and argument Mathias Nyman
2025-11-19 14:24 ` [PATCH 14/23] usb: xhci: use cached HCSPARAMS1 value Mathias Nyman
2025-11-19 14:24 ` [PATCH 15/23] usb: xhci: simplify handling of Structural Parameters 1 values Mathias Nyman
2025-11-19 14:24 ` [PATCH 16/23] usb: xhci: limit number of ports to 127 Mathias Nyman
2025-11-19 14:24 ` [PATCH 17/23] usb: xhci: limit number of interrupts to 128 Mathias Nyman
2025-11-19 14:24 ` [PATCH 18/23] usb: xhci: improve xhci-caps.h comments Mathias Nyman
2025-11-19 14:24 ` [PATCH 19/23] usb: xhci: simplify Isochronous Scheduling Threshold handling Mathias Nyman
2025-11-19 14:24 ` [PATCH 20/23] usb: xhci: simplify Max Scratchpad buffer macros Mathias Nyman
2025-11-19 14:24 ` [PATCH 21/23] usb: xhci: drop xhci-caps.h dependence on xhci-ext-caps.h Mathias Nyman
2025-11-19 14:24 ` [PATCH 22/23] usb: xhci: standardize single bit-field macros Mathias Nyman
2025-11-19 14:24 ` [PATCH 23/23] usb: xhci: Add debugfs support for xHCI Port Link Info (PORTLI) register Mathias Nyman
2026-03-04  9:42   ` Michal Pecio
2026-03-04 14:25     ` Mathias Nyman
2026-03-04 16:40       ` Michal Pecio

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=20251119142417.2820519-3-mathias.nyman@linux.intel.com \
    --to=mathias.nyman@linux.intel.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-usb@vger.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