From nobody Sat Nov 14 20:54:38 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4jRmb26fLmz70bsD for ; Sat, 14 Nov 2026 20:54:38 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 4jRmb25ddNz4FsC for ; Sat, 14 Nov 2026 20:54:38 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794689678; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=aKfadivp9hOqSxz1Rtg5ymXD/T3GQIMvrQYFRI1PQAg=; b=TQmfOrC66RFwKkGvkJNfVPY7SOheO1qxwKtHlhtHB5rVm7FfT0iNa5UDa8zDypGrENdnjY rRg6wIZPQniEavcJ/UOdOJ3LxrDn0ELJ8XbDjAPnZ+8Ee3IwXVmB5OiCP6NL8W6H5u+lCx SUPGQdhxZCSKTKLSTAarPXK6RtIDEn2N4Em1E18/ry7AvcQCG+0YoiJQ0D7Eky3rUXONlW fggCNsXnXLxGld4L72yEI8wQTXeJvtA6C76vs2RKaC7psbdSwTnvhrY9t/ZiUak3uIilTW JPlYlJylgcfzd/FzghVYzYgD0Sfy2blTgpU5C96e6pVO1lIr+NHgMEPEwWgb+Q== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1794689678; b=S3F2PQU/AHxHAX5lk3LLlQ/pH5faKbuDt6vwn6SvM71HAR8fZFKeKtrUt/WuBYxzyHhwar JrlonJRd+JyJcYLrgWA1pTBDZrhMJBz4dm88/nbh3YiqqhxGU+bzomPmoRRsJoU5NBcUBl oQbuZjjqjphJql67xNtBaNI7XZ5lk9gTj7oAA8WrV+oTYCJPdsUIQscM+zECtUhN+Vuebz rn6A3KenlPemGLilmofBFfS2qL+uuXlNpD/tzaOafiuhDpqxLuLWmthFrh1+SsT32Noqdu 43tAbqJbk3Hx/+SttCK+f6x8ylNZNf1jn87OhjN+wtQLe0gMahj2wQ+mNkRfSw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794689678; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=aKfadivp9hOqSxz1Rtg5ymXD/T3GQIMvrQYFRI1PQAg=; b=ZnoIDjrE+Wt19/V09hLQYXcqoJqdV6XQlL+CPUwJ2TZ3fminwlgqgb0SGUXFX5sB6G8ybu ESTddj8u0ghsHRIp+mG7HpxX9/AYUsAUqFRdEuV+yKADVT7+V02qWEo9so7LSfUsuT24Y7 ZM4ByuCeLeiIjefq3fUlrBr3WX4+KwJ2MXyRcuYzZry0uRxbe88IkwPnoHV4oUO5arfn3b EJzDlewmgfDzUUPelL4K6TtN/EGk4hdggJSU44UP1z2xNwmcxId9aOPxkqPLR21ZEPeziW N7DTxqAyo6uKLhJYvSDpR5sH8QOEI6HL4WJqFZrfQEd9le1LoUToBkzw190UMw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4jRmb25JCYzcll for ; Sat, 14 Nov 2026 20:54:38 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 11fb2 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Sat, 14 Nov 2026 20:54:38 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: ShengYi Hung Subject: git: f8a67d6ff5e7 - main - xhci: Map stream id to occupied slot List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all X-BeenThere: dev-commits-src-all@freebsd.org List-Id: Commit messages for all branches of the src repository List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: aokblast X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: f8a67d6ff5e7c89ee2ffd4b05a68d2f2b29db1a1 Auto-Submitted: auto-generated Date: Sat, 14 Nov 2026 20:54:38 +0000 Message-Id: <6af8ca8e.11fb2.55aa919b@gitrepo.freebsd.org> The branch main has been updated by aokblast: URL: https://cgit.FreeBSD.org/src/commit/?id=f8a67d6ff5e7c89ee2ffd4b05a68d2f2b29db1a1 commit f8a67d6ff5e7c89ee2ffd4b05a68d2f2b29db1a1 Author: ShengYi Hung AuthorDate: 2026-11-05 22:37:52 +0000 Commit: ShengYi Hung 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];