Skip to content

Commit ac9d8fe

Browse files
Sarah Sharpgregkh
authored andcommitted
USB: xhci: Add quirk for Fresco Logic xHCI hardware.
This Fresco Logic xHCI host controller chip revision puts bad data into the output endpoint context after a Reset Endpoint command. It needs a Configure Endpoint command (instead of a Set TR Dequeue Pointer command) after the reset endpoint command. Set up the input context before issuing the Reset Endpoint command so we don't copy bad data from the output endpoint context. The HW also can't handle two commands queued at once, so submit the TRB for the Configure Endpoint command in the event handler for the Reset Endpoint command. Devices that stall on control endpoints before a configuration is selected will not work under this Fresco Logic xHCI host controller revision. This patch is for prototype hardware that will be given to other companies for evaluation purposes only, and should not reach consumer hands. Fresco Logic's next chip rev should have this bug fixed. Signed-off-by: Sarah Sharp <[email protected]> Cc: stable <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 82d1009 commit ac9d8fe

File tree

4 files changed

+148
-23
lines changed

4 files changed

+148
-23
lines changed

drivers/usb/host/xhci-hcd.c

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ int xhci_init(struct usb_hcd *hcd)
224224
xhci_dbg(xhci, "QUIRK: Not clearing Link TRB chain bits.\n");
225225
xhci->quirks |= XHCI_LINK_TRB_QUIRK;
226226
} else {
227-
xhci_dbg(xhci, "xHCI has no QUIRKS\n");
227+
xhci_dbg(xhci, "xHCI doesn't need link TRB QUIRK\n");
228228
}
229229
retval = xhci_mem_init(xhci, GFP_KERNEL);
230230
xhci_dbg(xhci, "Finished xhci_init\n");
@@ -567,13 +567,22 @@ unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
567567
return 1 << (xhci_get_endpoint_index(desc) + 1);
568568
}
569569

570+
/* Find the flag for this endpoint (for use in the control context). Use the
571+
* endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
572+
* bit 1, etc.
573+
*/
574+
unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index)
575+
{
576+
return 1 << (ep_index + 1);
577+
}
578+
570579
/* Compute the last valid endpoint context index. Basically, this is the
571580
* endpoint index plus one. For slot contexts with more than valid endpoint,
572581
* we find the most significant bit set in the added contexts flags.
573582
* e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
574583
* fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
575584
*/
576-
static inline unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
585+
unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
577586
{
578587
return fls(added_ctxs) - 1;
579588
}
@@ -1230,8 +1239,44 @@ void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
12301239
xhci_zero_in_ctx(xhci, virt_dev);
12311240
}
12321241

1242+
void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci,
1243+
unsigned int slot_id, unsigned int ep_index,
1244+
struct xhci_dequeue_state *deq_state)
1245+
{
1246+
struct xhci_container_ctx *in_ctx;
1247+
struct xhci_input_control_ctx *ctrl_ctx;
1248+
struct xhci_ep_ctx *ep_ctx;
1249+
u32 added_ctxs;
1250+
dma_addr_t addr;
1251+
1252+
xhci_endpoint_copy(xhci, xhci->devs[slot_id], ep_index);
1253+
in_ctx = xhci->devs[slot_id]->in_ctx;
1254+
ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
1255+
addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
1256+
deq_state->new_deq_ptr);
1257+
if (addr == 0) {
1258+
xhci_warn(xhci, "WARN Cannot submit config ep after "
1259+
"reset ep command\n");
1260+
xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n",
1261+
deq_state->new_deq_seg,
1262+
deq_state->new_deq_ptr);
1263+
return;
1264+
}
1265+
ep_ctx->deq = addr | deq_state->new_cycle_state;
1266+
1267+
xhci_slot_copy(xhci, xhci->devs[slot_id]);
1268+
1269+
ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
1270+
added_ctxs = xhci_get_endpoint_flag_from_index(ep_index);
1271+
ctrl_ctx->add_flags = added_ctxs | SLOT_FLAG;
1272+
ctrl_ctx->drop_flags = added_ctxs;
1273+
1274+
xhci_dbg(xhci, "Slot ID %d Input Context:\n", slot_id);
1275+
xhci_dbg_ctx(xhci, in_ctx, ep_index);
1276+
}
1277+
12331278
void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
1234-
struct usb_device *udev, struct usb_host_endpoint *ep,
1279+
struct usb_device *udev,
12351280
unsigned int ep_index, struct xhci_ring *ep_ring)
12361281
{
12371282
struct xhci_dequeue_state deq_state;
@@ -1241,12 +1286,26 @@ void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
12411286
* or it will attempt to resend it on the next doorbell ring.
12421287
*/
12431288
xhci_find_new_dequeue_state(xhci, udev->slot_id,
1244-
ep_index, ep_ring->stopped_td, &deq_state);
1289+
ep_index, ep_ring->stopped_td,
1290+
&deq_state);
12451291

1246-
xhci_dbg(xhci, "Queueing new dequeue state\n");
1247-
xhci_queue_new_dequeue_state(xhci, ep_ring,
1248-
udev->slot_id,
1249-
ep_index, &deq_state);
1292+
/* HW with the reset endpoint quirk will use the saved dequeue state to
1293+
* issue a configure endpoint command later.
1294+
*/
1295+
if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) {
1296+
xhci_dbg(xhci, "Queueing new dequeue state\n");
1297+
xhci_queue_new_dequeue_state(xhci, ep_ring,
1298+
udev->slot_id,
1299+
ep_index, &deq_state);
1300+
} else {
1301+
/* Better hope no one uses the input context between now and the
1302+
* reset endpoint completion!
1303+
*/
1304+
xhci_dbg(xhci, "Setting up input context for "
1305+
"configure endpoint command\n");
1306+
xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id,
1307+
ep_index, &deq_state);
1308+
}
12501309
}
12511310

12521311
/* Deal with stalled endpoints. The core should have sent the control message
@@ -1293,7 +1352,7 @@ void xhci_endpoint_reset(struct usb_hcd *hcd,
12931352
* command. Better hope that last command worked!
12941353
*/
12951354
if (!ret) {
1296-
xhci_cleanup_stalled_ring(xhci, udev, ep, ep_index, ep_ring);
1355+
xhci_cleanup_stalled_ring(xhci, udev, ep_index, ep_ring);
12971356
kfree(ep_ring->stopped_td);
12981357
xhci_ring_cmd_db(xhci);
12991358
}

drivers/usb/host/xhci-pci.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424

2525
#include "xhci.h"
2626

27+
/* Device for a quirk */
28+
#define PCI_VENDOR_ID_FRESCO_LOGIC 0x1b73
29+
#define PCI_DEVICE_ID_FRESCO_LOGIC_PDK 0x1000
30+
2731
static const char hcd_name[] = "xhci_hcd";
2832

2933
/* called after powerup, by probe or system-pm "wakeup" */
@@ -62,6 +66,15 @@ static int xhci_pci_setup(struct usb_hcd *hcd)
6266
xhci->hcc_params = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
6367
xhci_print_registers(xhci);
6468

69+
/* Look for vendor-specific quirks */
70+
if (pdev->vendor == PCI_VENDOR_ID_FRESCO_LOGIC &&
71+
pdev->device == PCI_DEVICE_ID_FRESCO_LOGIC_PDK &&
72+
pdev->revision == 0x0) {
73+
xhci->quirks |= XHCI_RESET_EP_QUIRK;
74+
xhci_dbg(xhci, "QUIRK: Fresco Logic xHC needs configure"
75+
" endpoint cmd after reset endpoint\n");
76+
}
77+
6578
/* Make sure the HC is halted. */
6679
retval = xhci_halt(xhci);
6780
if (retval)

drivers/usb/host/xhci-ring.c

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,6 @@ void xhci_queue_new_dequeue_state(struct xhci_hcd *xhci,
469469
* ring running.
470470
*/
471471
ep_ring->state |= SET_DEQ_PENDING;
472-
xhci_ring_cmd_db(xhci);
473472
}
474473

475474
/*
@@ -538,6 +537,7 @@ static void handle_stopped_endpoint(struct xhci_hcd *xhci,
538537
if (deq_state.new_deq_ptr && deq_state.new_deq_seg) {
539538
xhci_queue_new_dequeue_state(xhci, ep_ring,
540539
slot_id, ep_index, &deq_state);
540+
xhci_ring_cmd_db(xhci);
541541
} else {
542542
/* Otherwise just ring the doorbell to restart the ring */
543543
ring_ep_doorbell(xhci, slot_id, ep_index);
@@ -651,18 +651,31 @@ static void handle_reset_ep_completion(struct xhci_hcd *xhci,
651651
{
652652
int slot_id;
653653
unsigned int ep_index;
654+
struct xhci_ring *ep_ring;
654655

655656
slot_id = TRB_TO_SLOT_ID(trb->generic.field[3]);
656657
ep_index = TRB_TO_EP_INDEX(trb->generic.field[3]);
658+
ep_ring = xhci->devs[slot_id]->ep_rings[ep_index];
657659
/* This command will only fail if the endpoint wasn't halted,
658660
* but we don't care.
659661
*/
660662
xhci_dbg(xhci, "Ignoring reset ep completion code of %u\n",
661663
(unsigned int) GET_COMP_CODE(event->status));
662664

663-
/* Clear our internal halted state and restart the ring */
664-
xhci->devs[slot_id]->ep_rings[ep_index]->state &= ~EP_HALTED;
665-
ring_ep_doorbell(xhci, slot_id, ep_index);
665+
/* HW with the reset endpoint quirk needs to have a configure endpoint
666+
* command complete before the endpoint can be used. Queue that here
667+
* because the HW can't handle two commands being queued in a row.
668+
*/
669+
if (xhci->quirks & XHCI_RESET_EP_QUIRK) {
670+
xhci_dbg(xhci, "Queueing configure endpoint command\n");
671+
xhci_queue_configure_endpoint(xhci,
672+
xhci->devs[slot_id]->in_ctx->dma, slot_id);
673+
xhci_ring_cmd_db(xhci);
674+
} else {
675+
/* Clear our internal halted state and restart the ring */
676+
ep_ring->state &= ~EP_HALTED;
677+
ring_ep_doorbell(xhci, slot_id, ep_index);
678+
}
666679
}
667680

668681
static void handle_cmd_completion(struct xhci_hcd *xhci,
@@ -671,6 +684,10 @@ static void handle_cmd_completion(struct xhci_hcd *xhci,
671684
int slot_id = TRB_TO_SLOT_ID(event->flags);
672685
u64 cmd_dma;
673686
dma_addr_t cmd_dequeue_dma;
687+
struct xhci_input_control_ctx *ctrl_ctx;
688+
unsigned int ep_index;
689+
struct xhci_ring *ep_ring;
690+
unsigned int ep_state;
674691

675692
cmd_dma = event->cmd_trb;
676693
cmd_dequeue_dma = xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
@@ -698,8 +715,39 @@ static void handle_cmd_completion(struct xhci_hcd *xhci,
698715
xhci_free_virt_device(xhci, slot_id);
699716
break;
700717
case TRB_TYPE(TRB_CONFIG_EP):
701-
xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(event->status);
702-
complete(&xhci->devs[slot_id]->cmd_completion);
718+
/*
719+
* Configure endpoint commands can come from the USB core
720+
* configuration or alt setting changes, or because the HW
721+
* needed an extra configure endpoint command after a reset
722+
* endpoint command. In the latter case, the xHCI driver is
723+
* not waiting on the configure endpoint command.
724+
*/
725+
ctrl_ctx = xhci_get_input_control_ctx(xhci,
726+
xhci->devs[slot_id]->in_ctx);
727+
/* Input ctx add_flags are the endpoint index plus one */
728+
ep_index = xhci_last_valid_endpoint(ctrl_ctx->add_flags) - 1;
729+
ep_ring = xhci->devs[slot_id]->ep_rings[ep_index];
730+
if (!ep_ring) {
731+
/* This must have been an initial configure endpoint */
732+
xhci->devs[slot_id]->cmd_status =
733+
GET_COMP_CODE(event->status);
734+
complete(&xhci->devs[slot_id]->cmd_completion);
735+
break;
736+
}
737+
ep_state = ep_ring->state;
738+
xhci_dbg(xhci, "Completed config ep cmd - last ep index = %d, "
739+
"state = %d\n", ep_index, ep_state);
740+
if (xhci->quirks & XHCI_RESET_EP_QUIRK &&
741+
ep_state & EP_HALTED) {
742+
/* Clear our internal halted state and restart ring */
743+
xhci->devs[slot_id]->ep_rings[ep_index]->state &=
744+
~EP_HALTED;
745+
ring_ep_doorbell(xhci, slot_id, ep_index);
746+
} else {
747+
xhci->devs[slot_id]->cmd_status =
748+
GET_COMP_CODE(event->status);
749+
complete(&xhci->devs[slot_id]->cmd_completion);
750+
}
703751
break;
704752
case TRB_TYPE(TRB_EVAL_CONTEXT):
705753
xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(event->status);
@@ -958,7 +1006,6 @@ static int handle_tx_event(struct xhci_hcd *xhci,
9581006
xhci_queue_reset_ep(xhci, slot_id, ep_index);
9591007
xhci_cleanup_stalled_ring(xhci,
9601008
td->urb->dev,
961-
td->urb->ep,
9621009
ep_index, ep_ring);
9631010
xhci_ring_cmd_db(xhci);
9641011
goto td_cleanup;

drivers/usb/host/xhci.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,12 @@ struct xhci_td {
929929
union xhci_trb *last_trb;
930930
};
931931

932+
struct xhci_dequeue_state {
933+
struct xhci_segment *new_deq_seg;
934+
union xhci_trb *new_deq_ptr;
935+
int new_cycle_state;
936+
};
937+
932938
struct xhci_ring {
933939
struct xhci_segment *first_seg;
934940
union xhci_trb *enqueue;
@@ -955,12 +961,6 @@ struct xhci_ring {
955961
u32 cycle_state;
956962
};
957963

958-
struct xhci_dequeue_state {
959-
struct xhci_segment *new_deq_seg;
960-
union xhci_trb *new_deq_ptr;
961-
int new_cycle_state;
962-
};
963-
964964
struct xhci_erst_entry {
965965
/* 64-bit event ring segment address */
966966
u64 seg_addr;
@@ -1063,6 +1063,7 @@ struct xhci_hcd {
10631063
int error_bitmask;
10641064
unsigned int quirks;
10651065
#define XHCI_LINK_TRB_QUIRK (1 << 0)
1066+
#define XHCI_RESET_EP_QUIRK (1 << 1)
10661067
};
10671068

10681069
/* For testing purposes */
@@ -1170,6 +1171,8 @@ int xhci_alloc_virt_device(struct xhci_hcd *xhci, int slot_id, struct usb_device
11701171
int xhci_setup_addressable_virt_dev(struct xhci_hcd *xhci, struct usb_device *udev);
11711172
unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc);
11721173
unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc);
1174+
unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index);
1175+
unsigned int xhci_last_valid_endpoint(u32 added_ctxs);
11731176
void xhci_endpoint_zero(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev, struct usb_host_endpoint *ep);
11741177
void xhci_endpoint_copy(struct xhci_hcd *xhci,
11751178
struct xhci_virt_device *vdev, unsigned int ep_index);
@@ -1233,8 +1236,11 @@ void xhci_queue_new_dequeue_state(struct xhci_hcd *xhci,
12331236
struct xhci_ring *ep_ring, unsigned int slot_id,
12341237
unsigned int ep_index, struct xhci_dequeue_state *deq_state);
12351238
void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
1236-
struct usb_device *udev, struct usb_host_endpoint *ep,
1239+
struct usb_device *udev,
12371240
unsigned int ep_index, struct xhci_ring *ep_ring);
1241+
void xhci_queue_config_ep_quirk(struct xhci_hcd *xhci,
1242+
unsigned int slot_id, unsigned int ep_index,
1243+
struct xhci_dequeue_state *deq_state);
12381244

12391245
/* xHCI roothub code */
12401246
int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, u16 wIndex,

0 commit comments

Comments
 (0)