Received: by 2002:ab2:710b:0:b0:1ef:a325:1205 with SMTP id z11csp1878020lql; Wed, 13 Mar 2024 10:32:35 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUBEn0CqYs+/Ixu2pTC224lfNftHjyxwGVcL4TRxxhdbpKlq9UCfMs8N+YvtepgcXXy+pXyUlIaB3e+bmxJDyzhjFk/i46O7f8weY5YnQ== X-Google-Smtp-Source: AGHT+IF7u1Utfq+t5pBhn0/TydyCXELOm9gg91EZbYJsCPSYrI0CYH8ocatr/71DMxyg2UJ/J5U5 X-Received: by 2002:a05:622a:345:b0:42e:bcc4:2959 with SMTP id r5-20020a05622a034500b0042ebcc42959mr18627888qtw.4.1710351155196; Wed, 13 Mar 2024 10:32:35 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710351155; cv=pass; d=google.com; s=arc-20160816; b=w7H47oEGjDtj/dfq91/Z/UIvOvsCQjodBazwzK98Wt83rGgQBN4XEVk7+/354A+xHq Na/dVpu3rTMRsiPLhxB3F0hVfimfHqYOxnv0G1fjIHoq7cWXEUOzgpBrCRwqSgoGEi+Y HEc5Hz6rJob1uhGvATAlooEJBf23EugMJPePdI4htcdkCzzizlslN6ByGJF/vHMRqfvz Njdl6IS0lBXLxpv1cKellghomhQa2eQ1E2H5xrSoCzsiOJtQYfTQE22WtE7AwRjxzQCO nl7vXzFM+YujavjSve5PrLBZ7cuheQxxJ4daiPenBwIny9vsunm2tIPpHKS+w+a/08X+ RDmA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=R3t6PJnYgksDzts8Checnq5iJh6b82xv3xTIXs62CSY=; fh=emoPbv5YBXn6b7rRdWPRBN3EcQv1XOxUiVyou9FdZi0=; b=D63hw1N/yxmhzJ/3why+oemMwkoMUNq9ohZAD4ebUKNJ9S6WcKpxc4M7R7JrzVPaPH l/8ebk3AsrAc2dcbvyQz9df3PWdOtFVhgK9Mc8yNdq4XQy4p/dEkEPMGqLm8RSMGsawG /5JGGNg4gUudX8aSH5P1P/QZXGM7sFH/AY2V75MMVZpvdNeUh+Z5yxwx0/b7CiHikNiT wY+hEljcxZQIDOR1i+hjpPsmkOX3RCRHAJP2yf+QXTB9TASrAhJahpY9yIwi0iaZb77u 5RUQzmN8QEglf9QESMdbpbIqcx8WzfjQKTJNvdzn5zJ/GpEunTO4+SUbkzol65043Bzg x86A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=LArpGSzY; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-101986-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-101986-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id v9-20020ac85789000000b0042ef5bdda3asi10320875qta.374.2024.03.13.10.32.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Mar 2024 10:32:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-101986-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=LArpGSzY; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-101986-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-101986-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id D9ED01C2164B for ; Wed, 13 Mar 2024 17:32:34 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E8AF2148316; Wed, 13 Mar 2024 16:43:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="LArpGSzY" 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 D97F96351D; Wed, 13 Mar 2024 16:43:17 +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=1710348197; cv=none; b=jThpK7Wzhb+R1JEzKTarI1d4Cc+LydsG5HSbyRfaz3/DOwoZOZm2dNfTR0MyuWm8tA0M800AXiLeJvOn6gE3WXJB5JXq3uke2W55wLFyLqh8rT454qRgKcq2dZraXR5pJY8svWFNupqgv3WkBUuKj8dz1rz6PwX5xTE7bt8IvzY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710348197; c=relaxed/simple; bh=5rpHXRhcPl6lbXJLB6ylw3YrCJhJxnN/XB+6g1t3MRo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=b14mfp1Oz+hcc9DUt4VdCjgKmpb7fixaqnv+fNzzwYuI57YUJogDjUB8N1ku7rKuffAU3wnyWKDoFrdHdtu3Lpb/R406TbM0nxfpO1iupX3QmBWmiudgg21/kXZkOKPciGKVVDw3LRiT4ycILn2bS5FD9HazRWJjELIQMMrXvTY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=LArpGSzY; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 82989C43601; Wed, 13 Mar 2024 16:43:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1710348197; bh=5rpHXRhcPl6lbXJLB6ylw3YrCJhJxnN/XB+6g1t3MRo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LArpGSzYfLVg/2vSW4S1lZLDjCE/a2EXRbI5boHWqqAW5THZv8AGLftN8OzlSXk/X qhzBljndpZtEjUAWzyjyYPiMhYvfdJmZ1JVhHxCxfaLko2qcT8rSvcpjFgcyRXRtIt FsXSrsAzTM80qxYw3Wuyz1PxlnzYK0srJgBjs25ZtZO25u/MfxuPPrkg+E2XInycqy j/AHz+Tje6QyLOh0DUxya2IfS7cMTuiRAEOidmRCNCXMeBuM8Z4E611Q/pwD2mi2gE AoBNTZ8DrI/JcTnDaO19IoAvj3UfySv4IrMciha60vOrJuZi7HIzgJsb/q5NSSkLs9 XL72IwgLLne9Q== 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.15 46/76] xhci: process isoc TD properly when there was a transaction error mid TD. Date: Wed, 13 Mar 2024 12:41:53 -0400 Message-ID: <20240313164223.615640-47-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240313164223.615640-1-sashal@kernel.org> References: <20240313164223.615640-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.15.152-rc1.gz X-KernelTest-Tree: git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git X-KernelTest-Branch: linux-5.15.y X-KernelTest-Patches: git://git.kernel.org/pub/scm/linux/kernel/git/stable/stable-queue.git X-KernelTest-Version: 5.15.152-rc1 X-KernelTest-Deadline: 2024-03-15T16:42+00:00 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit 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ł Pecio Closes: https://lore.kernel.org/linux-usb/20240112235205.1259f60c@foxbook/ Tested-by: Michał Pecio Cc: stable@vger.kernel.org Signed-off-by: Mathias Nyman Link: https://lore.kernel.org/r/20240125152737.2983959-4-mathias.nyman@linux.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 aa12da0796d2d..7e88b65b694ad 100644 --- a/drivers/usb/host/xhci-ring.c +++ b/drivers/usb/host/xhci-ring.c @@ -2428,6 +2428,9 @@ static int process_isoc_td(struct xhci_hcd *xhci, struct 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 = short_framestatus; if (xhci->quirks & XHCI_TRUST_TX_LENGTH) @@ -2453,8 +2456,9 @@ static int process_isoc_td(struct xhci_hcd *xhci, struct xhci_virt_ep *ep, break; case COMP_USB_TRANSACTION_ERROR: frame->status = -EPROTO; + sum_trbs_for_length = true; if (ep_trb != td->last_trb) - return 0; + td->error_mid_td = true; break; case COMP_STOPPED: sum_trbs_for_length = true; @@ -2474,6 +2478,9 @@ static int process_isoc_td(struct xhci_hcd *xhci, struct xhci_virt_ep *ep, break; } + if (td->urb_length_set) + goto finish_td; + if (sum_trbs_for_length) frame->actual_length = sum_trb_lengths(xhci, ep->ring, ep_trb) + ep_trb_len - remaining; @@ -2482,6 +2489,14 @@ static int process_isoc_td(struct xhci_hcd *xhci, struct xhci_virt_ep *ep, td->urb->actual_length += frame->actual_length; +finish_td: + /* Don't give back TD yet if we encountered an error mid TD */ + if (td->error_mid_td && ep_trb != td->last_trb) { + xhci_dbg(xhci, "Error mid isoc TD, wait for final completion event\n"); + td->urb_length_set = true; + return 0; + } + return finish_td(xhci, ep, ep_ring, td, trb_comp_code); } @@ -2866,17 +2881,51 @@ static int handle_tx_event(struct xhci_hcd *xhci, } 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 = 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 = 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 = list_next_entry(td, td_list); + + ep_seg = 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 = td->last_trb; + ep_ring->deq_seg = td->last_trb_seg; + inc_deq(xhci, ep_ring); + xhci_td_cleanup(xhci, td, ep_ring, td->status); + td = td_next; } + } + + if (!ep_seg) { /* HC is busted, give up! */ xhci_err(xhci, "ERROR Transfer event TRB DMA ptr not " @@ -2888,9 +2937,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 == COMP_SHORT_PACKET) ep_ring->last_td_was_short = true; diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h index 8ae33db1e4bcc..0e46b9e45c20c 100644 --- a/drivers/usb/host/xhci.h +++ b/drivers/usb/host/xhci.h @@ -1572,6 +1572,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; }; -- 2.43.0