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 4/4] usb: xhci: modify trb_in_td() to be more modular
Date: Thu, 6 Feb 2025 12:34:28 +0200 [thread overview]
Message-ID: <20250206103428.1034784-5-niklas.neronin@linux.intel.com> (raw)
In-Reply-To: <20250206103428.1034784-1-niklas.neronin@linux.intel.com>
Lay the ground work for future handle_tx_event() rework, which will
require a function which checks if a DMA address is in queueu.
To its core, trb_in_td() checks if a TRB falls within the specified start
and end TRB/segment range, a common requirement. For instance, a ring has
pointers to the queues first and last TRB/segment, which means that with
slight modifications and renaming trb_in_td() could work for other
structures not only TDs.
Modify trb_in_td() to accept pointer to start and end TRB/segment, and
introduce a new function that takes a 'xhci_td' struct pointer, forwarding
its elements to dma_in_range(), previously trb_in_td().
Signed-off-by: Niklas Neronin <niklas.neronin@linux.intel.com>
---
drivers/usb/host/xhci-ring.c | 41 ++++++++++++++++++++++--------------
1 file changed, 25 insertions(+), 16 deletions(-)
diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
index 23337c9d34c1..34699038b7f2 100644
--- a/drivers/usb/host/xhci-ring.c
+++ b/drivers/usb/host/xhci-ring.c
@@ -278,24 +278,28 @@ 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.
+ * Check if the DMA address of a TRB falls within the specified range.
+ * The range is defined by 'start_trb' in 'start_seg' and 'end_trb' in 'end_seg'.
+ * If the TRB's DMA address is within this range, return the segment containing the TRB.
+ * Otherwise, return 'NULL'.
*/
-static struct xhci_segment *trb_in_td(struct xhci_td *td, dma_addr_t dma)
+static struct xhci_segment *dma_in_range(struct xhci_segment *start_seg, union xhci_trb *start_trb,
+ struct xhci_segment *end_seg, union xhci_trb *end_trb,
+ dma_addr_t dma)
{
- struct xhci_segment *seg = td->start_seg;
+ struct xhci_segment *seg = start_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))
+ if (start_seg == end_seg) {
+ if (start_trb <= end_trb) {
+ if (xhci_trb_virt_to_dma(start_seg, start_trb) <= dma &&
+ dma <= xhci_trb_virt_to_dma(end_seg, end_trb))
return seg;
return NULL;
}
/* 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))
+ if (xhci_trb_virt_to_dma(end_seg, end_trb) < dma &&
+ dma < xhci_trb_virt_to_dma(start_seg, start_trb))
return NULL;
if (seg->dma <= dma && dma <= (seg->dma + TRB_SEGMENT_SIZE))
return seg;
@@ -304,24 +308,29 @@ static struct xhci_segment *trb_in_td(struct xhci_td *td, dma_addr_t dma)
/* 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)
+ if (seg == end_seg)
return NULL;
seg = seg->next;
- if (seg == td->start_seg)
+ if (seg == start_seg)
return NULL;
}
- if (seg == td->start_seg) {
- if (dma < xhci_trb_virt_to_dma(td->start_seg, td->start_trb))
+ if (seg == start_seg) {
+ if (dma < xhci_trb_virt_to_dma(start_seg, start_trb))
return NULL;
- } else if (seg == td->end_seg) {
- if (xhci_trb_virt_to_dma(td->end_seg, td->end_trb) < dma)
+ } else if (seg == end_seg) {
+ if (xhci_trb_virt_to_dma(end_seg, end_trb) < dma)
return NULL;
}
return seg;
}
+static struct xhci_segment *trb_in_td(struct xhci_td *td, dma_addr_t dma)
+{
+ return dma_in_range(td->start_seg, td->start_trb, td->end_seg, td->end_trb, dma);
+}
+
/*
* Return number of free normal TRBs from enqueue to dequeue pointer on ring.
* Not counting an assumed link TRB at end of each TRBS_PER_SEGMENT sized segment.
--
2.47.2
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 ` [PATCH 3/4] usb: xhci: rework and simplify trb_in_td() Niklas Neronin
2025-02-19 8:56 ` 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 ` Niklas Neronin [this message]
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-5-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