xhci features for usb-next
Subsystems (2)
cover xhci features for usb-next
Hi Greg
A small fixup series on top of previous series for usb-next.
Improves URB cancel during endpoint stall code in previous series PATCH 12/15 "xhci: Prevent early endpoint restart when handling STALL errors."
Also has a couple more patches just submitted by Michal that touches code relatad to URB cancel and endpoint stop.
Thanks
Mathias
Mathias Nyman (1):
xhci: Avoid queuing redundant Stop Endpoint command for stalled
endpoint
Michal Pecio (2):
usb: xhci: Don't change the status of stalled TDs on failed Stop EP
usb: xhci: Avoid Stop Endpoint retry loop if the endpoint seems
Running
drivers/usb/host/xhci-ring.c | 23 ++++++++++++++++++----- drivers/usb/host/xhci.c | 8 +++++--- 2 files changed, 23 insertions(+), 8 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/3 xhci: Avoid queuing redundant Stop Endpoint command for stalled endpoint
If EP_STALLED flag is set in xhci_urb_dequeue(), without EP_HALTED or SET_DEQ_PENDING flags, then the endpoint is in stopped state and the cancelled URB can be given back immediately withouth queueing a 'stop endpoint' command.
Without this change the cancelled URB would eventually be given back in the 'context state error' completion path of the 'stop endpoint' command. This is not optimal.
For this improvement to work the EP_STALLED flag must be cleared with xhci lock held.
--- drivers/usb/host/xhci.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/usb/host/xhci.c +5 -3
@@ -1773,8 +1773,8 @@ static int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) goto done; } -/* In this case no commands are pending but the endpoint is stopped */ -if (ep->ep_state &EP_CLEARING_TT ) { +/* In these cases no commands are pending but the endpoint is stopped */ +if (ep->ep_state & (EP_CLEARING_TT |EP_STALLED )) {/* and cancelled TDs can be given back right away */ xhci_dbg (xhci,"Invalidating TDs instantly on slot %d ep %d in state 0x%x\n" , urb->dev->slot_id, ep_index, ep->ep_state);
@@ -3211,10 +3211,12 @@ static void xhci_endpoint_reset(struct usb_hcd *hcd,return ; ep = &vdev->eps[ep_index]; + +spin_lock_irqsave (&xhci->lock, flags); + ep->ep_state &= ~EP_STALLED ;/* Bail out if toggle is already being cleared by a endpoint reset */ -spin_lock_irqsave (&xhci->lock, flags);if (ep->ep_state &EP_HARD_CLEAR_TOGGLE ) { ep->ep_state &= ~EP_HARD_CLEAR_TOGGLE ;spin_unlock_irqrestore (&xhci->lock, flags);
Signature · 1 line
2.43.0
PATCH 2/3 usb: xhci: Don't change the status of stalled TDs on failed Stop EP
In reply to Mathias NymanFrom: Michal Pecio <[email protected]>
When the device stalls an endpoint, current TD is assigned -EPIPE status and Reset Endpoint is queued. If a Stop Endpoint is pending at the time, it will run before Reset Endpoint and fail due to the stall. Its handler will change TD's status to -EPROTO before Reset Endpoint handler runs and initiates giveback.
Check if the stall has already been handled and don't try to do it again. Since xhci_handle_halted_endpoint() performs this check too, not overwriting td->status is the only difference.
I haven't seen this case yet, but I have seen a related one where the xHC has already executed Reset Endpoint, EP Context state is now Stopped and EP_HALTED is set. If the xHC took a bit longer to execute Reset Endpoint, said case would become this one.
--- drivers/usb/host/xhci-ring.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/usb/host/xhci-ring.c +11 -1
@@ -1220,7 +1220,14 @@ static void xhci_handle_cmd_stop_ep(struct xhci_hcd *xhci, int slot_id,*/ switch (GET_EP_CTX_STATE (ep_ctx)) {case EP_STATE_HALTED : -xhci_dbg (xhci,"Stop ep completion raced with stall, reset ep\n" ); +xhci_dbg (xhci,"Stop ep completion raced with stall\n" ); +/* +* If the halt happened before Stop Endpoint failed, its transfer event +* should have already been handled and Reset Endpoint should be pending. +*/ +if (ep->ep_state &EP_HALTED ) + goto reset_done; +if (ep->ep_state &EP_HAS_STREAMS ) { reset_type =EP_SOFT_RESET ; }else {
@@ -1231,8 +1238,11 @@ static void xhci_handle_cmd_stop_ep(struct xhci_hcd *xhci, int slot_id, }/* reset ep, reset handler cleans up cancelled tds */ err =xhci_handle_halted_endpoint (xhci, ep, td, reset_type); +xhci_dbg (xhci,"Stop ep completion resetting ep, status %d\n" , err);if (err)break ; +reset_done: +/* Reset EP handler will clean up cancelled TDs */ ep->ep_state &= ~EP_STOP_CMD_PENDING ;return ;case EP_STATE_STOPPED :
Signature · 1 line
2.43.0
PATCH 3/3 usb: xhci: Avoid Stop Endpoint retry loop if the endpoint seems Running
In reply to Mathias NymanFrom: Michal Pecio <[email protected]>
Nothing prevents a broken HC from claiming that an endpoint is Running and repeatedly rejecting Stop Endpoint with Context State Error.
Avoid infinite retries and give back cancelled TDs.
No such cases known so far, but HCs have bugs.
--- drivers/usb/host/xhci-ring.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/usb/host/xhci-ring.c +7 -4
@@ -1264,16 +1264,19 @@ static void xhci_handle_cmd_stop_ep(struct xhci_hcd *xhci, int slot_id,* Stopped state, but it will soon change to Running. * * Assume this bug on unexpected Stop Endpoint failures. -* Keep retrying until the EP starts and stops again, on -* chips where this is known to help. Wait for 100ms. +* Keep retrying until the EP starts and stops again. */ -if (time_is_before_jiffies (ep->stop_time +msecs_to_jiffies (100 ))) -break ; fallthrough;case EP_STATE_RUNNING :/* Race, HW handled stop ep cmd before ep was running */ xhci_dbg (xhci,"Stop ep completion ctx error, ctx_state %d\n" ,GET_EP_CTX_STATE (ep_ctx)); +/* +* Don't retry forever if we guessed wrong or a defective HC never starts +* the EP or says 'Running' but fails the command. We must give back TDs. +*/ +if (time_is_before_jiffies (ep->stop_time +msecs_to_jiffies (100 ))) +break ; command =xhci_alloc_command (xhci, false,GFP_ATOMIC );if (!command) {
Signature · 1 line
2.43.0