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 03/23] xhci: simplify and rework trb_in_td()
Date: Wed, 19 Nov 2025 16:23:57 +0200	[thread overview]
Message-ID: <20251119142417.2820519-4-mathias.nyman@linux.intel.com> (raw)
In-Reply-To: <20251119142417.2820519-1-mathias.nyman@linux.intel.com>

The trb_in_td() checking is quite complex, especially when checking for
TRBs in ranges that can span several segments.

Simplify the search by creating a position index for each TRB on the
ring, and just compare the position indexes.
Add a more generic dma_in_range() helper that checks if a trb dma
address is in the range between a start and end trb and call it from
trb_in_td()

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

diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
index 7fccca7e1c62..aa7fc4d6f97c 100644
--- a/drivers/usb/host/xhci-ring.c
+++ b/drivers/usb/host/xhci-ring.c
@@ -160,6 +160,11 @@ static void trb_to_noop(union xhci_trb *trb, u32 noop_type)
 	}
 }
 
+static unsigned int trb_to_pos(struct xhci_segment *seg, union xhci_trb *trb)
+{
+	return seg->num * TRBS_PER_SEGMENT + (trb - seg->trbs);
+}
+
 /* Updates trb to point to the next TRB in the ring, and updates seg if the next
  * TRB is in a new segment.  This does not skip over link TRBs, and it does not
  * effect the ring dequeue or enqueue pointers.
@@ -299,55 +304,34 @@ static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring,
 		inc_enq_past_link(xhci, ring, chain);
 }
 
-/*
- * If the suspect DMA address is a TRB in this TD, this function returns that
- * TRB's segment. Otherwise it returns 0.
- */
-static struct xhci_segment *trb_in_td(struct xhci_td *td, dma_addr_t suspect_dma)
+static bool dma_in_range(dma_addr_t dma,
+			 struct xhci_segment *start_seg, union xhci_trb *start_trb,
+			 struct xhci_segment *end_seg, union xhci_trb *end_trb)
 {
-	dma_addr_t start_dma;
-	dma_addr_t end_seg_dma;
-	dma_addr_t end_trb_dma;
-	struct xhci_segment *cur_seg;
+	unsigned int pos, start, end;
+	struct xhci_segment *pos_seg;
+	union xhci_trb *pos_trb = xhci_dma_to_trb(start_seg, dma, &pos_seg);
 
-	start_dma = xhci_trb_virt_to_dma(td->start_seg, td->start_trb);
-	cur_seg = td->start_seg;
+	/* Is the trb dma address even part of the whole ring? */
+	if (!pos_trb)
+		return false;
 
-	do {
-		if (start_dma == 0)
-			return NULL;
-		/* We may get an event for a Link TRB in the middle of a TD */
-		end_seg_dma = xhci_trb_virt_to_dma(cur_seg,
-				&cur_seg->trbs[TRBS_PER_SEGMENT - 1]);
-		/* If the end TRB isn't in this segment, this is set to 0 */
-		end_trb_dma = xhci_trb_virt_to_dma(cur_seg, td->end_trb);
-
-		if (end_trb_dma > 0) {
-			/* The end TRB is in this segment, so suspect should be here */
-			if (start_dma <= end_trb_dma) {
-				if (suspect_dma >= start_dma && suspect_dma <= end_trb_dma)
-					return cur_seg;
-			} else {
-				/* Case for one segment with
-				 * a TD wrapped around to the top
-				 */
-				if ((suspect_dma >= start_dma &&
-							suspect_dma <= end_seg_dma) ||
-						(suspect_dma >= cur_seg->dma &&
-						 suspect_dma <= end_trb_dma))
-					return cur_seg;
-			}
-			return NULL;
-		}
-		/* Might still be somewhere in this segment */
-		if (suspect_dma >= start_dma && suspect_dma <= end_seg_dma)
-			return cur_seg;
+	pos = trb_to_pos(pos_seg, pos_trb);
+	start = trb_to_pos(start_seg, start_trb);
+	end = trb_to_pos(end_seg, end_trb);
 
-		cur_seg = cur_seg->next;
-		start_dma = xhci_trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]);
-	} while (cur_seg != td->start_seg);
+	/* end position is smaller than start, search range wraps around */
+	if (end < start)
+		return !(pos > end && pos < start);
 
-	return NULL;
+	return (pos >= start && pos <= end);
+}
+
+/* If the suspect DMA address is a TRB in this TD, this function returns true */
+static bool trb_in_td(struct xhci_td *td, dma_addr_t suspect_dma)
+{
+	return dma_in_range(suspect_dma, td->start_seg, td->start_trb,
+			    td->end_seg, td->end_trb);
 }
 
 /*
-- 
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 ` [PATCH 02/23] xhci: Add helper to find trb from its dma address Mathias Nyman
2025-11-19 14:23 ` Mathias Nyman [this message]
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-4-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