From nobody Fri Dec 19 20:14:02 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 860861586D5; Wed, 13 Mar 2024 16:47:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710348441; cv=none; b=LJ/YKBd+Au/zFJcnQPCKsOqNWJrzXKgubKux0DrVF5WZEmaGhQPipZmIbu6dB780lZVfi0M8JJSnRfkeKh15n+mKzQ4Ayv3CxIqdqhlkH3dro0GMxKoQSZzN/+bY7/uOtY2wKFKVO3NsgqAiX/+XYQ3yb93S5CMCR2HKmYDHKds= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710348441; c=relaxed/simple; bh=YA9Ft6bl28bLy63KaXUuZC8DvGOBxcJ5mO6uDn/RKqw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=gv1l3qN+hzL0xfnGEslxadykC66kJIhFscJqScFK6Xoay+/UWo9cj98YYvx5vXQBhYM/BO9GYD4axIOOukYhnYVVyXnAlRPFTZ1JBslRjyXiuZYFCTu4wvv4/BwV0rJCNE5aeFyWC/hqojObXk5sOKALnf6JP7nVBzsEbDnnUEk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=oqSsMawi; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="oqSsMawi" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 317BFC43390; Wed, 13 Mar 2024 16:47:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1710348441; bh=YA9Ft6bl28bLy63KaXUuZC8DvGOBxcJ5mO6uDn/RKqw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oqSsMawipQUZERsQnRP8AMvgLSmOnR9HuARb+E6/c6sNUsg+xAKTGma2qgcEhknDv r9ZvMRIIA7/COxMlWff9PnSn3NfpgXdY4UbooBv9lKTuJP/1tMbmMpC3HPa06mApP8 VZ1ko0DIl48Bt4suMkORqqnAvVcNaq9bIhbm0bogyefeycyHzVmS8RMcAbkuGCez+I +XZw40i/SgS3xGaeWCBjaHeuquQP5mNd4wKylKYlyTxef74KNvLB0L0LBWiLQkBM6k bXeEZaXOWSDa4k99+2LFO5Y6VdcyEsQrE6/kY3A9hpdk4jimHhBVfuLpPcmZftq3PV Pc7Ql/5l1zn9g== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Mathias Nyman , =?UTF-8?q?Micha=C5=82=20Pecio?= , Greg Kroah-Hartman , Sasha Levin Subject: [PATCH 5.10 35/73] xhci: process isoc TD properly when there was a transaction error mid TD. Date: Wed, 13 Mar 2024 12:46:02 -0400 Message-ID: <20240313164640.616049-36-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240313164640.616049-1-sashal@kernel.org> References: <20240313164640.616049-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" X-KernelTest-Patch: http://kernel.org/pub/linux/kernel/v5.x/stable-review/patch-5.10.213-rc1.gz X-KernelTest-Tree: git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git X-KernelTest-Branch: linux-5.10.y X-KernelTest-Patches: git://git.kernel.org/pub/scm/linux/kernel/git/stable/stable-queue.git X-KernelTest-Version: 5.10.213-rc1 X-KernelTest-Deadline: 2024-03-15T16:46+00:00 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: quoted-printable From: Mathias Nyman [ Upstream commit 5372c65e1311a16351ef03dd096ff576e6477674 ] The last TRB of a isoc TD might not trigger an event if there was an error event for a TRB mid TD. This is seen on a NEC Corporation uPD720200 USB 3.0 Host After an error mid a multi-TRB TD the xHC should according to xhci 4.9.1 generate events for passed TRBs with IOC flag set if it proceeds to the next TD. This event is either a copy of the original error, or a "success" transfer event. If that event is missing then the driver and xHC host get out of sync as the driver is still expecting a transfer event for that first TD, while xHC host is already sending events for the next TD in the list. This leads to "Transfer event TRB DMA ptr not part of current TD" messages. As a solution we tag the isoc TDs that get error events mid TD. If an event doesn't match the first TD, then check if the tag is set, and event points to the next TD. In that case give back the fist TD and process the next TD normally Make sure TD status and transferred length stay valid in both cases with and without final TD completion event. Reported-by: Micha=C5=82 Pecio Closes: https://lore.kernel.org/linux-usb/20240112235205.1259f60c@foxbook/ Tested-by: Micha=C5=82 Pecio Cc: stable@vger.kernel.org Signed-off-by: Mathias Nyman Link: https://lore.kernel.org/r/20240125152737.2983959-4-mathias.nyman@linu= x.intel.com Signed-off-by: Greg Kroah-Hartman Signed-off-by: Sasha Levin --- drivers/usb/host/xhci-ring.c | 74 +++++++++++++++++++++++++++++------- drivers/usb/host/xhci.h | 1 + 2 files changed, 61 insertions(+), 14 deletions(-) diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c index 62d92da7016e7..883cf477a70b9 100644 --- a/drivers/usb/host/xhci-ring.c +++ b/drivers/usb/host/xhci-ring.c @@ -2244,6 +2244,9 @@ static int process_isoc_td(struct xhci_hcd *xhci, str= uct xhci_virt_ep *ep, /* handle completion code */ switch (trb_comp_code) { case COMP_SUCCESS: + /* Don't overwrite status if TD had an error, see xHCI 4.9.1 */ + if (td->error_mid_td) + break; if (remaining) { frame->status =3D short_framestatus; if (xhci->quirks & XHCI_TRUST_TX_LENGTH) @@ -2269,8 +2272,9 @@ static int process_isoc_td(struct xhci_hcd *xhci, str= uct xhci_virt_ep *ep, break; case COMP_USB_TRANSACTION_ERROR: frame->status =3D -EPROTO; + sum_trbs_for_length =3D true; if (ep_trb !=3D td->last_trb) - return 0; + td->error_mid_td =3D true; break; case COMP_STOPPED: sum_trbs_for_length =3D true; @@ -2290,6 +2294,9 @@ static int process_isoc_td(struct xhci_hcd *xhci, str= uct xhci_virt_ep *ep, break; } =20 + if (td->urb_length_set) + goto finish_td; + if (sum_trbs_for_length) frame->actual_length =3D sum_trb_lengths(xhci, ep->ring, ep_trb) + ep_trb_len - remaining; @@ -2298,6 +2305,14 @@ static int process_isoc_td(struct xhci_hcd *xhci, st= ruct xhci_virt_ep *ep, =20 td->urb->actual_length +=3D frame->actual_length; =20 +finish_td: + /* Don't give back TD yet if we encountered an error mid TD */ + if (td->error_mid_td && ep_trb !=3D td->last_trb) { + xhci_dbg(xhci, "Error mid isoc TD, wait for final completion event\n"); + td->urb_length_set =3D true; + return 0; + } + return finish_td(xhci, ep, ep_ring, td, trb_comp_code); } =20 @@ -2684,17 +2699,51 @@ static int handle_tx_event(struct xhci_hcd *xhci, } =20 if (!ep_seg) { - if (!ep->skip || - !usb_endpoint_xfer_isoc(&td->urb->ep->desc)) { - /* Some host controllers give a spurious - * successful event after a short transfer. - * Ignore it. - */ - if ((xhci->quirks & XHCI_SPURIOUS_SUCCESS) && - ep_ring->last_td_was_short) { - ep_ring->last_td_was_short =3D false; - goto cleanup; + + if (ep->skip && usb_endpoint_xfer_isoc(&td->urb->ep->desc)) { + skip_isoc_td(xhci, td, ep, status); + goto cleanup; + } + + /* + * Some hosts give a spurious success event after a short + * transfer. Ignore it. + */ + if ((xhci->quirks & XHCI_SPURIOUS_SUCCESS) && + ep_ring->last_td_was_short) { + ep_ring->last_td_was_short =3D false; + goto cleanup; + } + + /* + * xhci 4.10.2 states isoc endpoints should continue + * processing the next TD if there was an error mid TD. + * So host like NEC don't generate an event for the last + * isoc TRB even if the IOC flag is set. + * xhci 4.9.1 states that if there are errors in mult-TRB + * TDs xHC should generate an error for that TRB, and if xHC + * proceeds to the next TD it should genete an event for + * any TRB with IOC flag on the way. Other host follow this. + * So this event might be for the next TD. + */ + if (td->error_mid_td && + !list_is_last(&td->td_list, &ep_ring->td_list)) { + struct xhci_td *td_next =3D list_next_entry(td, td_list); + + ep_seg =3D trb_in_td(xhci, td_next->start_seg, td_next->first_trb, + td_next->last_trb, ep_trb_dma, false); + if (ep_seg) { + /* give back previous TD, start handling new */ + xhci_dbg(xhci, "Missing TD completion event after mid TD error\n"); + ep_ring->dequeue =3D td->last_trb; + ep_ring->deq_seg =3D td->last_trb_seg; + inc_deq(xhci, ep_ring); + xhci_td_cleanup(xhci, td, ep_ring, td->status); + td =3D td_next; } + } + + if (!ep_seg) { /* HC is busted, give up! */ xhci_err(xhci, "ERROR Transfer event TRB DMA ptr not " @@ -2706,9 +2755,6 @@ static int handle_tx_event(struct xhci_hcd *xhci, ep_trb_dma, true); return -ESHUTDOWN; } - - skip_isoc_td(xhci, td, ep, status); - goto cleanup; } if (trb_comp_code =3D=3D COMP_SHORT_PACKET) ep_ring->last_td_was_short =3D true; diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h index 85ab213c7940a..5a8443f6ed703 100644 --- a/drivers/usb/host/xhci.h +++ b/drivers/usb/host/xhci.h @@ -1554,6 +1554,7 @@ struct xhci_td { struct xhci_segment *bounce_seg; /* actual_length of the URB has already been set */ bool urb_length_set; + bool error_mid_td; unsigned int num_trbs; }; =20 --=20 2.43.0