Skip to content

Commit 93ceaa8

Browse files
matnymangregkh
authored andcommitted
xhci: Fix handling halted endpoint even if endpoint ring appears empty
If a class driver cancels its only URB then the endpoint ring buffer will appear empty to the xhci driver. xHC hardware may still process cached TRBs, and complete with a STALL, halting the endpoint. This halted endpoint was not handled correctly by xhci driver as events on empty rings were all assumed to be spurious events. xhci driver refused to restart the ring with EP_HALTED flag set, so class driver was never informed the endpoint halted even if it queued new URBs. The host side of the endpoint needs to be reset, and dequeue pointer should be moved in order to clear the cached TRBs and resetart the endpoint. Small adjustments in finding the new dequeue pointer are needed to support the case of stall on an empty ring and unknown current TD. Cc: <[email protected]> cc: Jeremy Compostella <[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]>
1 parent 4dfb9ce commit 93ceaa8

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

drivers/usb/host/xhci-ring.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,23 @@ void xhci_find_new_dequeue_state(struct xhci_hcd *xhci,
547547
stream_id);
548548
return;
549549
}
550+
/*
551+
* A cancelled TD can complete with a stall if HW cached the trb.
552+
* In this case driver can't find cur_td, but if the ring is empty we
553+
* can move the dequeue pointer to the current enqueue position.
554+
*/
555+
if (!cur_td) {
556+
if (list_empty(&ep_ring->td_list)) {
557+
state->new_deq_seg = ep_ring->enq_seg;
558+
state->new_deq_ptr = ep_ring->enqueue;
559+
state->new_cycle_state = ep_ring->cycle_state;
560+
goto done;
561+
} else {
562+
xhci_warn(xhci, "Can't find new dequeue state, missing cur_td\n");
563+
return;
564+
}
565+
}
566+
550567
/* Dig out the cycle state saved by the xHC during the stop ep cmd */
551568
xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
552569
"Finding endpoint context");
@@ -592,6 +609,7 @@ void xhci_find_new_dequeue_state(struct xhci_hcd *xhci,
592609
state->new_deq_seg = new_seg;
593610
state->new_deq_ptr = new_deq;
594611

612+
done:
595613
/* Don't update the ring cycle state for the producer (us). */
596614
xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
597615
"Cycle state = 0x%x", state->new_cycle_state);
@@ -1856,7 +1874,8 @@ static void xhci_cleanup_halted_endpoint(struct xhci_hcd *xhci,
18561874

18571875
if (reset_type == EP_HARD_RESET) {
18581876
ep->ep_state |= EP_HARD_CLEAR_TOGGLE;
1859-
xhci_cleanup_stalled_ring(xhci, ep_index, stream_id, td);
1877+
xhci_cleanup_stalled_ring(xhci, slot_id, ep_index, stream_id,
1878+
td);
18601879
xhci_clear_hub_tt_buffer(xhci, td, ep);
18611880
}
18621881
xhci_ring_cmd_db(xhci);
@@ -2539,6 +2558,15 @@ static int handle_tx_event(struct xhci_hcd *xhci,
25392558
xhci_dbg(xhci, "td_list is empty while skip flag set. Clear skip flag for slot %u ep %u.\n",
25402559
slot_id, ep_index);
25412560
}
2561+
if (trb_comp_code == COMP_STALL_ERROR ||
2562+
xhci_requires_manual_halt_cleanup(xhci, ep_ctx,
2563+
trb_comp_code)) {
2564+
xhci_cleanup_halted_endpoint(xhci, slot_id,
2565+
ep_index,
2566+
ep_ring->stream_id,
2567+
NULL,
2568+
EP_HARD_RESET);
2569+
}
25422570
goto cleanup;
25432571
}
25442572

drivers/usb/host/xhci.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3031,19 +3031,19 @@ static void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci,
30313031
added_ctxs, added_ctxs);
30323032
}
30333033

3034-
void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci, unsigned int ep_index,
3035-
unsigned int stream_id, struct xhci_td *td)
3034+
void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci, unsigned int slot_id,
3035+
unsigned int ep_index, unsigned int stream_id,
3036+
struct xhci_td *td)
30363037
{
30373038
struct xhci_dequeue_state deq_state;
3038-
struct usb_device *udev = td->urb->dev;
30393039

30403040
xhci_dbg_trace(xhci, trace_xhci_dbg_reset_ep,
30413041
"Cleaning up stalled endpoint ring");
30423042
/* We need to move the HW's dequeue pointer past this TD,
30433043
* or it will attempt to resend it on the next doorbell ring.
30443044
*/
3045-
xhci_find_new_dequeue_state(xhci, udev->slot_id,
3046-
ep_index, stream_id, td, &deq_state);
3045+
xhci_find_new_dequeue_state(xhci, slot_id, ep_index, stream_id, td,
3046+
&deq_state);
30473047

30483048
if (!deq_state.new_deq_ptr || !deq_state.new_deq_seg)
30493049
return;
@@ -3054,7 +3054,7 @@ void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci, unsigned int ep_index,
30543054
if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) {
30553055
xhci_dbg_trace(xhci, trace_xhci_dbg_reset_ep,
30563056
"Queueing new dequeue state");
3057-
xhci_queue_new_dequeue_state(xhci, udev->slot_id,
3057+
xhci_queue_new_dequeue_state(xhci, slot_id,
30583058
ep_index, &deq_state);
30593059
} else {
30603060
/* Better hope no one uses the input context between now and the
@@ -3065,7 +3065,7 @@ void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci, unsigned int ep_index,
30653065
xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
30663066
"Setting up input context for "
30673067
"configure endpoint command");
3068-
xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id,
3068+
xhci_setup_input_ctx_for_quirk(xhci, slot_id,
30693069
ep_index, &deq_state);
30703070
}
30713071
}

drivers/usb/host/xhci.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2116,8 +2116,9 @@ void xhci_find_new_dequeue_state(struct xhci_hcd *xhci,
21162116
void xhci_queue_new_dequeue_state(struct xhci_hcd *xhci,
21172117
unsigned int slot_id, unsigned int ep_index,
21182118
struct xhci_dequeue_state *deq_state);
2119-
void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci, unsigned int ep_index,
2120-
unsigned int stream_id, struct xhci_td *td);
2119+
void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci, unsigned int slot_id,
2120+
unsigned int ep_index, unsigned int stream_id,
2121+
struct xhci_td *td);
21212122
void xhci_stop_endpoint_command_watchdog(struct timer_list *t);
21222123
void xhci_handle_command_timeout(struct work_struct *work);
21232124

0 commit comments

Comments
 (0)