Skip to content

Commit b7350b1

Browse files
Michal Peciogregkh
authored andcommitted
usb: xhci: Avoid queuing redundant Stop Endpoint commands
[ Upstream commit 474538b8dd1cd9c666e56cfe8ef60fbb0fb513f4 ] Stop Endpoint command on an already stopped endpoint fails and may be misinterpreted as a known hardware bug by the completion handler. This results in an unnecessary delay with repeated retries of the command. Avoid queuing this command when endpoint state flags indicate that it's stopped or halted and the command will fail. If commands are pending on the endpoint, their completion handlers will process cancelled TDs so it's done. In case of waiting for external operations like clearing TT buffer, the endpoint is stopped and cancelled TDs can be processed now. This eliminates practically all unnecessary retries because an endpoint with pending URBs is maintained in Running state by the driver, unless aforementioned commands or other operations are pending on it. This is guaranteed by xhci_ring_ep_doorbell() and by the fact that it is called every time any of those operations completes. The only known exceptions are hardware bugs (the endpoint never starts at all) and Stream Protocol errors not associated with any TRB, which cause an endpoint reset not followed by restart. Sounds like a bug. Generally, these retries are only expected to happen when the endpoint fails to start for unknown/no reason, which is a worse problem itself, and fixing the bug eliminates the retries too. All cases were tested and found to work as expected. SET_DEQ_PENDING was produced by patching uvcvideo to unlink URBs in 100us intervals, which then runs into this case very often. EP_HALTED was produced by restarting 'cat /dev/ttyUSB0' on a serial dongle with broken cable. EP_CLEARING_TT by the same, with the dongle on an external hub. Fixes: fd9d55d190c0 ("xhci: retry Stop Endpoint on buggy NEC controllers") CC: [email protected] Signed-off-by: Michal Pecio <[email protected]> Signed-off-by: Mathias Nyman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 1164026 commit b7350b1

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

drivers/usb/host/xhci-ring.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,19 @@ static int xhci_invalidate_cancelled_tds(struct xhci_virt_ep *ep)
10521052
return 0;
10531053
}
10541054

1055+
/*
1056+
* Erase queued TDs from transfer ring(s) and give back those the xHC didn't
1057+
* stop on. If necessary, queue commands to move the xHC off cancelled TDs it
1058+
* stopped on. Those will be given back later when the commands complete.
1059+
*
1060+
* Call under xhci->lock on a stopped endpoint.
1061+
*/
1062+
void xhci_process_cancelled_tds(struct xhci_virt_ep *ep)
1063+
{
1064+
xhci_invalidate_cancelled_tds(ep);
1065+
xhci_giveback_invalidated_tds(ep);
1066+
}
1067+
10551068
/*
10561069
* Returns the TD the endpoint ring halted on.
10571070
* Only call for non-running rings without streams.

drivers/usb/host/xhci.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,10 +1903,21 @@ static int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
19031903
}
19041904
}
19051905

1906-
/* Queue a stop endpoint command, but only if this is
1907-
* the first cancellation to be handled.
1908-
*/
1909-
if (!(ep->ep_state & EP_STOP_CMD_PENDING)) {
1906+
/* These completion handlers will sort out cancelled TDs for us */
1907+
if (ep->ep_state & (EP_STOP_CMD_PENDING | EP_HALTED | SET_DEQ_PENDING)) {
1908+
xhci_dbg(xhci, "Not queuing Stop Endpoint on slot %d ep %d in state 0x%x\n",
1909+
urb->dev->slot_id, ep_index, ep->ep_state);
1910+
goto done;
1911+
}
1912+
1913+
/* In this case no commands are pending but the endpoint is stopped */
1914+
if (ep->ep_state & EP_CLEARING_TT) {
1915+
/* and cancelled TDs can be given back right away */
1916+
xhci_dbg(xhci, "Invalidating TDs instantly on slot %d ep %d in state 0x%x\n",
1917+
urb->dev->slot_id, ep_index, ep->ep_state);
1918+
xhci_process_cancelled_tds(ep);
1919+
} else {
1920+
/* Otherwise, queue a new Stop Endpoint command */
19101921
command = xhci_alloc_command(xhci, false, GFP_ATOMIC);
19111922
if (!command) {
19121923
ret = -ENOMEM;

drivers/usb/host/xhci.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1952,6 +1952,7 @@ void xhci_ring_doorbell_for_active_rings(struct xhci_hcd *xhci,
19521952
void xhci_cleanup_command_queue(struct xhci_hcd *xhci);
19531953
void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring);
19541954
unsigned int count_trbs(u64 addr, u64 len);
1955+
void xhci_process_cancelled_tds(struct xhci_virt_ep *ep);
19551956

19561957
/* xHCI roothub code */
19571958
void xhci_set_link_state(struct xhci_hcd *xhci, struct xhci_port *port,

0 commit comments

Comments
 (0)