From: Niklas Neronin <niklas.neronin@linux.intel.com>
To: mathias.nyman@linux.intel.com
Cc: linux-usb@vger.kernel.org,
Niklas Neronin <niklas.neronin@linux.intel.com>
Subject: [PATCH 3/4] usb: xhci: rework and simplify trb_in_td()
Date: Thu, 6 Feb 2025 12:34:27 +0200 [thread overview]
Message-ID: <20250206103428.1034784-4-niklas.neronin@linux.intel.com> (raw)
In-Reply-To: <20250206103428.1034784-1-niklas.neronin@linux.intel.com>
Function trb_in_td() searches for a DMA address within a segment ring,
starting from 'start_seg'. If the DMA address is found within a segment
and is positioned between 'start_trb' and 'end_trb', the function returns
the segment. If not, it returns 'NULL'. See ring example at the end.
The original implementation is overly complex and suboptimal.
Key enhancements include:
- Utilize 'end_seg' pointer as counterpart to 'start_seg', narrowing the
search scope from start to end segment, improving efficiency.
- Prioritizing the most frequent scenario where the start and end
segments are identical, and 'start_trb' precedes end_trb', by
checking this case first for quicker resolution.
- Clarifying the handling of TD wrap-around cases, where a TD spans
back to the start segment, making start and end segments equal,
but with 'end_trb' preceding 'start_trb', for better readability
and maintainability.
============================= Example ====================================
Segment ring, consisting of 3 segments (A,B,C) each containing 3 TRBs
(1,2,3). Any segment can be start/end seg, and any TRB inside start seg
can be start TRB, vise versa.
+---+ +---+ +---+
C --> | A |-->| B |-->| C |--> A
+---+ +---+ +---+
| | |
+---+ +---+ +---+
| 1 | | 1 | | 1 |
| 2 | | 2 | | 2 |
| 3 | | 3 | | 3 |
+---+ +---+ +---+
Signed-off-by: Niklas Neronin <niklas.neronin@linux.intel.com>
---
drivers/usb/host/xhci-ring.c | 72 +++++++++++++++++-------------------
1 file changed, 33 insertions(+), 39 deletions(-)
diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
index a69972cc400c..23337c9d34c1 100644
--- a/drivers/usb/host/xhci-ring.c
+++ b/drivers/usb/host/xhci-ring.c
@@ -281,51 +281,45 @@ static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring,
* 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 struct xhci_segment *trb_in_td(struct xhci_td *td, dma_addr_t dma)
{
- dma_addr_t start_dma;
- dma_addr_t end_seg_dma;
- dma_addr_t end_trb_dma;
- struct xhci_segment *cur_seg;
+ struct xhci_segment *seg = td->start_seg;
- start_dma = xhci_trb_virt_to_dma(td->start_seg, td->start_trb);
- cur_seg = td->start_seg;
-
- 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;
- }
+ if (td->start_seg == td->end_seg) {
+ if (td->start_trb <= td->end_trb) {
+ if (xhci_trb_virt_to_dma(td->start_seg, td->start_trb) <= dma &&
+ dma <= xhci_trb_virt_to_dma(td->end_seg, td->end_trb))
+ return seg;
return NULL;
}
- /* Might still be somewhere in this segment */
- if (suspect_dma >= start_dma && suspect_dma <= end_seg_dma)
- return cur_seg;
- cur_seg = cur_seg->next;
- start_dma = xhci_trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]);
- } while (cur_seg != td->start_seg);
+ /* Edge case, the TD wrapped around to the start segment. */
+ if (xhci_trb_virt_to_dma(td->end_seg, td->end_trb) < dma &&
+ dma < xhci_trb_virt_to_dma(td->start_seg, td->start_trb))
+ return NULL;
+ if (seg->dma <= dma && dma <= (seg->dma + TRB_SEGMENT_SIZE))
+ return seg;
+ seg = seg->next;
+ }
- return NULL;
+ /* Loop through segment which don't contain the DMA address. */
+ while (dma < seg->dma || (seg->dma + TRB_SEGMENT_SIZE) <= dma) {
+ if (seg == td->end_seg)
+ return NULL;
+
+ seg = seg->next;
+ if (seg == td->start_seg)
+ return NULL;
+ }
+
+ if (seg == td->start_seg) {
+ if (dma < xhci_trb_virt_to_dma(td->start_seg, td->start_trb))
+ return NULL;
+ } else if (seg == td->end_seg) {
+ if (xhci_trb_virt_to_dma(td->end_seg, td->end_trb) < dma)
+ return NULL;
+ }
+ return seg;
}
/*
--
2.47.2
next prev parent reply other threads:[~2025-02-06 10:34 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-06 10:34 [PATCH 0/4] usb: xhci: improve trb_in_td() Niklas Neronin
2025-02-06 10:34 ` [PATCH 1/4] usb: xhci: refactor trb_in_td() to be static Niklas Neronin
2025-02-06 10:34 ` [PATCH 2/4] usb: xhci: move debug capabilities from trb_in_td() to handle_tx_event() Niklas Neronin
2025-03-05 8:46 ` Michał Pecio
2025-03-05 9:17 ` Neronin, Niklas
2025-02-06 10:34 ` Niklas Neronin [this message]
2025-02-19 8:56 ` [PATCH 3/4] usb: xhci: rework and simplify trb_in_td() Michał Pecio
2025-02-19 14:25 ` Mathias Nyman
2025-02-20 12:14 ` [PATCH 5/4 RFC] An alternative dma_in_range() implementation Michał Pecio
2025-02-20 13:18 ` Neronin, Niklas
2025-02-20 12:25 ` [PATCH 3/4] usb: xhci: rework and simplify trb_in_td() Neronin, Niklas
2025-02-06 10:34 ` [PATCH 4/4] usb: xhci: modify trb_in_td() to be more modular Niklas Neronin
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=20250206103428.1034784-4-niklas.neronin@linux.intel.com \
--to=niklas.neronin@linux.intel.com \
--cc=linux-usb@vger.kernel.org \
--cc=mathias.nyman@linux.intel.com \
/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