git: f8a67d6ff5e7 - main - xhci: Map stream id to occupied slot

From: ShengYi Hung <aokblast_at_FreeBSD.org>
Date: Sat, 14 Nov 2026 20:54:38 UTC
The branch main has been updated by aokblast:

URL: https://cgit.FreeBSD.org/src/commit/?id=f8a67d6ff5e7c89ee2ffd4b05a68d2f2b29db1a1

commit f8a67d6ff5e7c89ee2ffd4b05a68d2f2b29db1a1
Author:     ShengYi Hung <aokblast@FreeBSD.org>
AuthorDate: 2026-11-05 22:37:52 +0000
Commit:     ShengYi Hung <aokblast@FreeBSD.org>
CommitDate: 2026-11-14 20:54:11 +0000

    xhci: Map stream id to occupied slot
    
    In xhci_check_transfer, we tried to iterate through STREAM * NUM_SLOTS
    and find the correct stream. However, stream is rear nowadaya both on
    FreeBSD and Linux. In which means that such STREAM * NUM_SLOTS array is
    sparse.
    
    As a result, we use a bit map to map stream id to slot. For a
    xhci_mask[stream_id], it contains the slot_id bitmap over which slot has
    been occupied.
    
    Differential Revision: https://reviews.freebsd.org/D61247
---
 sys/dev/usb/controller/xhci.c | 234 +++++++++++++++++++++++-------------------
 sys/dev/usb/controller/xhci.h |   2 +
 2 files changed, 129 insertions(+), 107 deletions(-)

diff --git a/sys/dev/usb/controller/xhci.c b/sys/dev/usb/controller/xhci.c
index 894cd159c281..5cc541b9f634 100644
--- a/sys/dev/usb/controller/xhci.c
+++ b/sys/dev/usb/controller/xhci.c
@@ -977,6 +977,60 @@ xhci_skip_transfer(struct usb_xfer *xfer)
 	}
 }
 
+/*------------------------------------------------------------------------*
+ *	xhci_find_transfer
+ *
+ * Find the queued USB transfer whose current TD contains the TRB
+ * address reported by a transfer event.
+ *------------------------------------------------------------------------*/
+static struct usb_xfer *
+xhci_find_transfer(struct xhci_endpoint_ext *pepext, uint64_t td_event,
+    int64_t *poffset)
+{
+	struct usb_xfer *xfer;
+	struct xhci_td *td;
+	int64_t offset;
+	uint32_t mask;
+	uint16_t stream_id;
+	int i;
+
+	for (stream_id = 0; stream_id != XHCI_MAX_STREAMS; stream_id++) {
+		for (mask = pepext->xfer_mask[stream_id]; mask != 0;
+		    mask &= ~(1U << i)) {
+			i = ffs(mask) - 1;
+
+			xfer = pepext->xfer[i +
+			    (XHCI_MAX_TRANSFERS * stream_id)];
+			td = xfer->td_transfer_cache;
+			if (td == NULL)
+				continue;
+
+			DPRINTFN(5, "Checking if 0x%016llx == "
+			    "(0x%016llx .. 0x%016llx)\n",
+			    (long long)td_event,
+			    (long long)td->td_self,
+			    (long long)td->td_self + sizeof(td->td_trb));
+
+			/*
+			 * NOTE: Some XHCI implementations might not trigger
+			 * an event on the last LINK TRB so we need to
+			 * consider both the last and second last event
+			 * address as conditions for a successful transfer.
+			 *
+			 * NOTE: We assume that the XHCI will only trigger one
+			 * event per chain of TRBs.
+			 */
+			offset = td_event - td->td_self;
+			if (offset >= 0 &&
+			    offset < (int64_t)sizeof(td->td_trb)) {
+				*poffset = offset;
+				return (xfer);
+			}
+		}
+	}
+	return (NULL);
+}
+
 /*------------------------------------------------------------------------*
  *	xhci_check_transfer
  *------------------------------------------------------------------------*/
@@ -984,11 +1038,12 @@ static void
 xhci_check_transfer(struct xhci_softc *sc, struct xhci_trb *trb)
 {
 	struct xhci_endpoint_ext *pepext;
+	struct usb_xfer *xfer;
+	struct xhci_td *td;
 	int64_t offset;
 	uint64_t td_event;
 	uint32_t temp;
 	uint32_t remainder;
-	uint16_t stream_id = 0;
 	uint16_t i;
 	uint8_t status;
 	uint8_t halted;
@@ -1026,125 +1081,87 @@ xhci_check_transfer(struct xhci_softc *sc, struct xhci_trb *trb)
 	pepext = &sc->sc_hw.devs[index].endp[epno];
 
 	/* try to find the USB transfer that generated the event */
-	for (i = 0;; i++) {
-		struct usb_xfer *xfer;
-		struct xhci_td *td;
-
-		if (i == (XHCI_MAX_TRANSFERS - 1)) {
-			if (pepext->trb_ep_mode != USB_EP_MODE_STREAMS ||
-			    stream_id == (XHCI_MAX_STREAMS - 1))
-				break;
-			stream_id++;
-			i = 0;
-			DPRINTFN(5, "stream_id=%u\n", stream_id);
-		}
-
-		xfer = pepext->xfer[i + (XHCI_MAX_TRANSFERS * stream_id)];
-		if (xfer == NULL)
-			continue;
-
-		td = xfer->td_transfer_cache;
-		if (td == NULL)
-			continue;
-
-		DPRINTFN(5, "Checking if 0x%016llx == (0x%016llx .. 0x%016llx)\n",
-			(long long)td_event,
-			(long long)td->td_self,
-			(long long)td->td_self + sizeof(td->td_trb));
-
-		/*
-		 * NOTE: Some XHCI implementations might not trigger
-		 * an event on the last LINK TRB so we need to
-		 * consider both the last and second last event
-		 * address as conditions for a successful transfer.
-		 *
-		 * NOTE: We assume that the XHCI will only trigger one
-		 * event per chain of TRBs.
-		 */
-
-		offset = td_event - td->td_self;
+	xfer = xhci_find_transfer(pepext, td_event, &offset);
+	if (xfer == NULL)
+		return;
 
-		if (offset >= 0 &&
-		    offset < (int64_t)sizeof(td->td_trb)) {
-			usb_pc_cpu_invalidate(td->page_cache);
+	td = xfer->td_transfer_cache;
 
-			/* compute rest of remainder, if any */
-			for (i = (offset / 16) + 1; i < td->ntrb; i++) {
-				temp = le32toh(td->td_trb[i].dwTrb2);
-				remainder += XHCI_TRB_2_BYTES_GET(temp);
-			}
+	usb_pc_cpu_invalidate(td->page_cache);
 
-			DPRINTFN(5, "New remainder: %u\n", remainder);
+	/* compute rest of remainder, if any */
+	for (i = (offset / 16) + 1; i < td->ntrb; i++) {
+		temp = le32toh(td->td_trb[i].dwTrb2);
+		remainder += XHCI_TRB_2_BYTES_GET(temp);
+	}
 
-			/* clear isochronous transfer errors */
-			if (xfer->flags_int.isochronous_xfr) {
-				if (halted) {
-					halted = 0;
-					status = XHCI_TRB_ERROR_SUCCESS;
-					remainder = td->len;
-				}
-			}
+	DPRINTFN(5, "New remainder: %u\n", remainder);
 
-			/* "td->remainder" is verified later */
-			td->remainder = remainder;
-			td->status = status;
+	/* clear isochronous transfer errors */
+	if (xfer->flags_int.isochronous_xfr) {
+		if (halted) {
+			halted = 0;
+			status = XHCI_TRB_ERROR_SUCCESS;
+			remainder = td->len;
+		}
+	}
 
-			usb_pc_cpu_flush(td->page_cache);
+	/* "td->remainder" is verified later */
+	td->remainder = remainder;
+	td->status = status;
 
-			/*
-			 * 1) Last transfer descriptor makes the
-			 * transfer done
-			 */
-			if (((void *)td) == xfer->td_transfer_last) {
-				DPRINTF("TD is last\n");
-				xhci_generic_done(xfer);
-				break;
-			}
+	usb_pc_cpu_flush(td->page_cache);
 
-			/*
-			 * 2) Any kind of error makes the transfer
-			 * done
-			 */
-			if (halted) {
-				DPRINTF("TD has I/O error\n");
-				xhci_generic_done(xfer);
-				break;
-			}
+	/*
+	 * 1) Last transfer descriptor makes the
+	 * transfer done
+	 */
+	if (((void *)td) == xfer->td_transfer_last) {
+		DPRINTF("TD is last\n");
+		xhci_generic_done(xfer);
+		return;
+	}
 
-			/*
-			 * 3) If there is no alternate next transfer,
-			 * a short packet also makes the transfer done
-			 */
-			if (td->remainder > 0) {
-				if (td->alt_next == NULL) {
-					DPRINTF(
-					    "short TD has no alternate next\n");
-					xhci_generic_done(xfer);
-					break;
-				}
-				DPRINTF("TD has short pkt\n");
-				if (xfer->flags_int.short_frames_ok ||
-				    xfer->flags_int.isochronous_xfr ||
-				    xfer->flags_int.control_xfr) {
-					/* follow the alt next */
-					xfer->td_transfer_cache = td->alt_next;
-					xhci_activate_transfer(xfer);
-					break;
-				}
-				xhci_skip_transfer(xfer);
-				xhci_generic_done(xfer);
-				break;
-			}
+	/*
+	 * 2) Any kind of error makes the transfer
+	 * done
+	 */
+	if (halted) {
+		DPRINTF("TD has I/O error\n");
+		xhci_generic_done(xfer);
+		return;
+	}
 
-			/*
-			 * 4) Transfer complete - go to next TD
-			 */
-			DPRINTF("Following next TD\n");
-			xfer->td_transfer_cache = td->obj_next;
+	/*
+	 * 3) If there is no alternate next transfer,
+	 * a short packet also makes the transfer done
+	 */
+	if (td->remainder > 0) {
+		if (td->alt_next == NULL) {
+			DPRINTF("short TD has no alternate next\n");
+			xhci_generic_done(xfer);
+			return;
+		}
+		DPRINTF("TD has short pkt\n");
+		if (xfer->flags_int.short_frames_ok ||
+		    xfer->flags_int.isochronous_xfr ||
+		    xfer->flags_int.control_xfr) {
+			/* follow the alt next */
+			xfer->td_transfer_cache = td->alt_next;
 			xhci_activate_transfer(xfer);
-			break;		/* there should only be one match */
+			return;
 		}
+		xhci_skip_transfer(xfer);
+		xhci_generic_done(xfer);
+		return;
 	}
+
+	/*
+	 * 4) Transfer complete - go to next TD
+	 */
+	DPRINTF("Following next TD\n");
+	xfer->td_transfer_cache = td->obj_next;
+	xhci_activate_transfer(xfer);
 }
 
 static int
@@ -3119,6 +3136,8 @@ xhci_transfer_remove(struct usb_xfer *xfer, usb_error_t error)
 		pepext->trb_used[xfer->stream_id]--;
 
 		pepext->xfer[xfer->qh_pos] = NULL;
+		pepext->xfer_mask[xfer->stream_id] &=
+		    ~(1U << (xfer->qh_pos % XHCI_MAX_TRANSFERS));
 
 		if (error && pepext->trb_running != 0) {
 			pepext->trb_halted = 1;
@@ -3259,6 +3278,7 @@ xhci_transfer_insert(struct usb_xfer *xfer)
 	DPRINTF("qh_pos = %u\n", i);
 
 	pepext->xfer[i] = xfer;
+	pepext->xfer_mask[id] |= 1U << (i % XHCI_MAX_TRANSFERS);
 
 	xfer->qh_pos = i;
 
diff --git a/sys/dev/usb/controller/xhci.h b/sys/dev/usb/controller/xhci.h
index e54ac6c66bbd..6818f0e177e8 100644
--- a/sys/dev/usb/controller/xhci.h
+++ b/sys/dev/usb/controller/xhci.h
@@ -409,10 +409,12 @@ struct xhci_hw_root {
 };
 
 CTASSERT(sizeof(struct xhci_hw_root) == XHCI_PAGE_SIZE);
+CTASSERT(XHCI_MAX_TRANSFERS <= 32);
 
 struct xhci_endpoint_ext {
 	struct xhci_trb		*trb;
 	struct usb_xfer		*xfer[XHCI_MAX_TRANSFERS * XHCI_MAX_STREAMS];
+	uint32_t		xfer_mask[XHCI_MAX_STREAMS];
 	struct usb_page_cache	*page_cache;
 	uint64_t		physaddr;
 	uint8_t			trb_used[XHCI_MAX_STREAMS];