Skip to content

Commit f12ea4a

Browse files
Andiry Xugregkh
authored andcommitted
xHCI: AMD isoc link TRB chain bit quirk
commit 7e393a8 upstream. Setting the chain (CH) bit in the link TRB of isochronous transfer rings is required by AMD 0.96 xHCI host controller to successfully transverse multi-TRB TD that span through different memory segments. When a Missed Service Error event occurs, if the chain bit is not set in the link TRB and the host skips TDs which just across a link TRB, the host may falsely recognize the link TRB as a normal TRB. You can see this may cause big trouble - the host does not jump to the right address which is pointed by the link TRB, but continue fetching the memory which is after the link TRB address, which may not even belong to the host, and the result cannot be predicted. This causes some big problems. Without the former patch I sent: "xHCI: prevent infinite loop when processing MSE event", the system may hang. With that patch applied, system does not hang, but the host still access wrong memory address and isoc transfer will fail. With this patch, isochronous transfer works as expected. This patch should be applied to kernels as old as 2.6.36, which was when the first isochronous support was added for the xHCI host controller. Signed-off-by: Andiry Xu <andiry.xu@amd.com> Signed-off-by: Sarah Sharp <sarah.a.sharp@linux.intel.com> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
1 parent 697ec8f commit f12ea4a

4 files changed

Lines changed: 51 additions & 38 deletions

File tree

‎drivers/usb/host/xhci-mem.c‎

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static void xhci_segment_free(struct xhci_hcd *xhci, struct xhci_segment *seg)
8181
* related flags, such as End TRB, Toggle Cycle, and no snoop.
8282
*/
8383
static void xhci_link_segments(struct xhci_hcd *xhci, struct xhci_segment *prev,
84-
struct xhci_segment *next, bool link_trbs)
84+
struct xhci_segment *next, bool link_trbs, bool isoc)
8585
{
8686
u32 val;
8787

@@ -97,7 +97,9 @@ static void xhci_link_segments(struct xhci_hcd *xhci, struct xhci_segment *prev,
9797
val &= ~TRB_TYPE_BITMASK;
9898
val |= TRB_TYPE(TRB_LINK);
9999
/* Always set the chain bit with 0.95 hardware */
100-
if (xhci_link_trb_quirk(xhci))
100+
/* Set chain bit for isoc rings on AMD 0.96 host */
101+
if (xhci_link_trb_quirk(xhci) ||
102+
(isoc && (xhci->quirks & XHCI_AMD_0x96_HOST)))
101103
val |= TRB_CHAIN;
102104
prev->trbs[TRBS_PER_SEGMENT-1].link.control = cpu_to_le32(val);
103105
}
@@ -154,7 +156,7 @@ static void xhci_initialize_ring_info(struct xhci_ring *ring)
154156
* See section 4.9.1 and figures 15 and 16.
155157
*/
156158
static struct xhci_ring *xhci_ring_alloc(struct xhci_hcd *xhci,
157-
unsigned int num_segs, bool link_trbs, gfp_t flags)
159+
unsigned int num_segs, bool link_trbs, bool isoc, gfp_t flags)
158160
{
159161
struct xhci_ring *ring;
160162
struct xhci_segment *prev;
@@ -180,12 +182,12 @@ static struct xhci_ring *xhci_ring_alloc(struct xhci_hcd *xhci,
180182
next = xhci_segment_alloc(xhci, flags);
181183
if (!next)
182184
goto fail;
183-
xhci_link_segments(xhci, prev, next, link_trbs);
185+
xhci_link_segments(xhci, prev, next, link_trbs, isoc);
184186

185187
prev = next;
186188
num_segs--;
187189
}
188-
xhci_link_segments(xhci, prev, ring->first_seg, link_trbs);
190+
xhci_link_segments(xhci, prev, ring->first_seg, link_trbs, isoc);
189191

190192
if (link_trbs) {
191193
/* See section 4.9.2.1 and 6.4.4.1 */
@@ -231,14 +233,14 @@ void xhci_free_or_cache_endpoint_ring(struct xhci_hcd *xhci,
231233
* pointers to the beginning of the ring.
232234
*/
233235
static void xhci_reinit_cached_ring(struct xhci_hcd *xhci,
234-
struct xhci_ring *ring)
236+
struct xhci_ring *ring, bool isoc)
235237
{
236238
struct xhci_segment *seg = ring->first_seg;
237239
do {
238240
memset(seg->trbs, 0,
239241
sizeof(union xhci_trb)*TRBS_PER_SEGMENT);
240242
/* All endpoint rings have link TRBs */
241-
xhci_link_segments(xhci, seg, seg->next, 1);
243+
xhci_link_segments(xhci, seg, seg->next, 1, isoc);
242244
seg = seg->next;
243245
} while (seg != ring->first_seg);
244246
xhci_initialize_ring_info(ring);
@@ -542,7 +544,7 @@ struct xhci_stream_info *xhci_alloc_stream_info(struct xhci_hcd *xhci,
542544
*/
543545
for (cur_stream = 1; cur_stream < num_streams; cur_stream++) {
544546
stream_info->stream_rings[cur_stream] =
545-
xhci_ring_alloc(xhci, 1, true, mem_flags);
547+
xhci_ring_alloc(xhci, 1, true, false, mem_flags);
546548
cur_ring = stream_info->stream_rings[cur_stream];
547549
if (!cur_ring)
548550
goto cleanup_rings;
@@ -767,7 +769,7 @@ int xhci_alloc_virt_device(struct xhci_hcd *xhci, int slot_id,
767769
}
768770

769771
/* Allocate endpoint 0 ring */
770-
dev->eps[0].ring = xhci_ring_alloc(xhci, 1, true, flags);
772+
dev->eps[0].ring = xhci_ring_alloc(xhci, 1, true, false, flags);
771773
if (!dev->eps[0].ring)
772774
goto fail;
773775

@@ -1177,10 +1179,10 @@ int xhci_endpoint_init(struct xhci_hcd *xhci,
11771179
*/
11781180
if (usb_endpoint_xfer_isoc(&ep->desc))
11791181
virt_dev->eps[ep_index].new_ring =
1180-
xhci_ring_alloc(xhci, 8, true, mem_flags);
1182+
xhci_ring_alloc(xhci, 8, true, true, mem_flags);
11811183
else
11821184
virt_dev->eps[ep_index].new_ring =
1183-
xhci_ring_alloc(xhci, 1, true, mem_flags);
1185+
xhci_ring_alloc(xhci, 1, true, false, mem_flags);
11841186
if (!virt_dev->eps[ep_index].new_ring) {
11851187
/* Attempt to use the ring cache */
11861188
if (virt_dev->num_rings_cached == 0)
@@ -1189,7 +1191,8 @@ int xhci_endpoint_init(struct xhci_hcd *xhci,
11891191
virt_dev->ring_cache[virt_dev->num_rings_cached];
11901192
virt_dev->ring_cache[virt_dev->num_rings_cached] = NULL;
11911193
virt_dev->num_rings_cached--;
1192-
xhci_reinit_cached_ring(xhci, virt_dev->eps[ep_index].new_ring);
1194+
xhci_reinit_cached_ring(xhci, virt_dev->eps[ep_index].new_ring,
1195+
usb_endpoint_xfer_isoc(&ep->desc) ? true : false);
11931196
}
11941197
virt_dev->eps[ep_index].skip = false;
11951198
ep_ring = virt_dev->eps[ep_index].new_ring;
@@ -2003,7 +2006,7 @@ int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
20032006
goto fail;
20042007

20052008
/* Set up the command ring to have one segments for now. */
2006-
xhci->cmd_ring = xhci_ring_alloc(xhci, 1, true, flags);
2009+
xhci->cmd_ring = xhci_ring_alloc(xhci, 1, true, false, flags);
20072010
if (!xhci->cmd_ring)
20082011
goto fail;
20092012
xhci_dbg(xhci, "Allocated command ring at %p\n", xhci->cmd_ring);
@@ -2034,7 +2037,8 @@ int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
20342037
* the event ring segment table (ERST). Section 4.9.3.
20352038
*/
20362039
xhci_dbg(xhci, "// Allocating event ring\n");
2037-
xhci->event_ring = xhci_ring_alloc(xhci, ERST_NUM_SEGS, false, flags);
2040+
xhci->event_ring = xhci_ring_alloc(xhci, ERST_NUM_SEGS, false, false,
2041+
flags);
20382042
if (!xhci->event_ring)
20392043
goto fail;
20402044
if (xhci_check_trb_in_td_math(xhci, flags) < 0)

‎drivers/usb/host/xhci-pci.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ static int xhci_pci_setup(struct usb_hcd *hcd)
128128
if (pdev->vendor == PCI_VENDOR_ID_NEC)
129129
xhci->quirks |= XHCI_NEC_HOST;
130130

131+
if (pdev->vendor == PCI_VENDOR_ID_AMD && xhci->hci_version == 0x96)
132+
xhci->quirks |= XHCI_AMD_0x96_HOST;
133+
131134
/* AMD PLL quirk */
132135
if (pdev->vendor == PCI_VENDOR_ID_AMD && usb_amd_find_chipset_info())
133136
xhci->quirks |= XHCI_AMD_PLL_FIX;

‎drivers/usb/host/xhci-ring.c‎

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ static void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring, bool consumer
185185
* prepare_transfer()?
186186
*/
187187
static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring,
188-
bool consumer, bool more_trbs_coming)
188+
bool consumer, bool more_trbs_coming, bool isoc)
189189
{
190190
u32 chain;
191191
union xhci_trb *next;
@@ -212,11 +212,13 @@ static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring,
212212
if (!chain && !more_trbs_coming)
213213
break;
214214

215-
/* If we're not dealing with 0.95 hardware,
215+
/* If we're not dealing with 0.95 hardware or
216+
* isoc rings on AMD 0.96 host,
216217
* carry over the chain bit of the previous TRB
217218
* (which may mean the chain bit is cleared).
218219
*/
219-
if (!xhci_link_trb_quirk(xhci)) {
220+
if (!(isoc && (xhci->quirks & XHCI_AMD_0x96_HOST))
221+
&& !xhci_link_trb_quirk(xhci)) {
220222
next->link.control &=
221223
cpu_to_le32(~TRB_CHAIN);
222224
next->link.control |=
@@ -2409,7 +2411,7 @@ irqreturn_t xhci_msi_irq(int irq, struct usb_hcd *hcd)
24092411
* prepare_transfer()?
24102412
*/
24112413
static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
2412-
bool consumer, bool more_trbs_coming,
2414+
bool consumer, bool more_trbs_coming, bool isoc,
24132415
u32 field1, u32 field2, u32 field3, u32 field4)
24142416
{
24152417
struct xhci_generic_trb *trb;
@@ -2419,15 +2421,15 @@ static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
24192421
trb->field[1] = cpu_to_le32(field2);
24202422
trb->field[2] = cpu_to_le32(field3);
24212423
trb->field[3] = cpu_to_le32(field4);
2422-
inc_enq(xhci, ring, consumer, more_trbs_coming);
2424+
inc_enq(xhci, ring, consumer, more_trbs_coming, isoc);
24232425
}
24242426

24252427
/*
24262428
* Does various checks on the endpoint ring, and makes it ready to queue num_trbs.
24272429
* FIXME allocate segments if the ring is full.
24282430
*/
24292431
static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
2430-
u32 ep_state, unsigned int num_trbs, gfp_t mem_flags)
2432+
u32 ep_state, unsigned int num_trbs, bool isoc, gfp_t mem_flags)
24312433
{
24322434
/* Make sure the endpoint has been added to xHC schedule */
24332435
switch (ep_state) {
@@ -2469,10 +2471,11 @@ static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
24692471
next = ring->enqueue;
24702472

24712473
while (last_trb(xhci, ring, ring->enq_seg, next)) {
2472-
/* If we're not dealing with 0.95 hardware,
2473-
* clear the chain bit.
2474+
/* If we're not dealing with 0.95 hardware or isoc rings
2475+
* on AMD 0.96 host, clear the chain bit.
24742476
*/
2475-
if (!xhci_link_trb_quirk(xhci))
2477+
if (!xhci_link_trb_quirk(xhci) && !(isoc &&
2478+
(xhci->quirks & XHCI_AMD_0x96_HOST)))
24762479
next->link.control &= cpu_to_le32(~TRB_CHAIN);
24772480
else
24782481
next->link.control |= cpu_to_le32(TRB_CHAIN);
@@ -2505,6 +2508,7 @@ static int prepare_transfer(struct xhci_hcd *xhci,
25052508
unsigned int num_trbs,
25062509
struct urb *urb,
25072510
unsigned int td_index,
2511+
bool isoc,
25082512
gfp_t mem_flags)
25092513
{
25102514
int ret;
@@ -2522,7 +2526,7 @@ static int prepare_transfer(struct xhci_hcd *xhci,
25222526

25232527
ret = prepare_ring(xhci, ep_ring,
25242528
le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK,
2525-
num_trbs, mem_flags);
2529+
num_trbs, isoc, mem_flags);
25262530
if (ret)
25272531
return ret;
25282532

@@ -2745,7 +2749,7 @@ static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
27452749

27462750
trb_buff_len = prepare_transfer(xhci, xhci->devs[slot_id],
27472751
ep_index, urb->stream_id,
2748-
num_trbs, urb, 0, mem_flags);
2752+
num_trbs, urb, 0, false, mem_flags);
27492753
if (trb_buff_len < 0)
27502754
return trb_buff_len;
27512755

@@ -2840,7 +2844,7 @@ static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
28402844
more_trbs_coming = true;
28412845
else
28422846
more_trbs_coming = false;
2843-
queue_trb(xhci, ep_ring, false, more_trbs_coming,
2847+
queue_trb(xhci, ep_ring, false, more_trbs_coming, false,
28442848
lower_32_bits(addr),
28452849
upper_32_bits(addr),
28462850
length_field,
@@ -2931,7 +2935,7 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
29312935

29322936
ret = prepare_transfer(xhci, xhci->devs[slot_id],
29332937
ep_index, urb->stream_id,
2934-
num_trbs, urb, 0, mem_flags);
2938+
num_trbs, urb, 0, false, mem_flags);
29352939
if (ret < 0)
29362940
return ret;
29372941

@@ -3003,7 +3007,7 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
30033007
more_trbs_coming = true;
30043008
else
30053009
more_trbs_coming = false;
3006-
queue_trb(xhci, ep_ring, false, more_trbs_coming,
3010+
queue_trb(xhci, ep_ring, false, more_trbs_coming, false,
30073011
lower_32_bits(addr),
30083012
upper_32_bits(addr),
30093013
length_field,
@@ -3063,7 +3067,7 @@ int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
30633067
num_trbs++;
30643068
ret = prepare_transfer(xhci, xhci->devs[slot_id],
30653069
ep_index, urb->stream_id,
3066-
num_trbs, urb, 0, mem_flags);
3070+
num_trbs, urb, 0, false, mem_flags);
30673071
if (ret < 0)
30683072
return ret;
30693073

@@ -3096,7 +3100,7 @@ int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
30963100
}
30973101
}
30983102

3099-
queue_trb(xhci, ep_ring, false, true,
3103+
queue_trb(xhci, ep_ring, false, true, false,
31003104
setup->bRequestType | setup->bRequest << 8 | le16_to_cpu(setup->wValue) << 16,
31013105
le16_to_cpu(setup->wIndex) | le16_to_cpu(setup->wLength) << 16,
31023106
TRB_LEN(8) | TRB_INTR_TARGET(0),
@@ -3116,7 +3120,7 @@ int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
31163120
if (urb->transfer_buffer_length > 0) {
31173121
if (setup->bRequestType & USB_DIR_IN)
31183122
field |= TRB_DIR_IN;
3119-
queue_trb(xhci, ep_ring, false, true,
3123+
queue_trb(xhci, ep_ring, false, true, false,
31203124
lower_32_bits(urb->transfer_dma),
31213125
upper_32_bits(urb->transfer_dma),
31223126
length_field,
@@ -3132,7 +3136,7 @@ int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
31323136
field = 0;
31333137
else
31343138
field = TRB_DIR_IN;
3135-
queue_trb(xhci, ep_ring, false, false,
3139+
queue_trb(xhci, ep_ring, false, false, false,
31363140
0,
31373141
0,
31383142
TRB_INTR_TARGET(0),
@@ -3281,7 +3285,8 @@ static int xhci_queue_isoc_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
32813285
trbs_per_td = count_isoc_trbs_needed(xhci, urb, i);
32823286

32833287
ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index,
3284-
urb->stream_id, trbs_per_td, urb, i, mem_flags);
3288+
urb->stream_id, trbs_per_td, urb, i, true,
3289+
mem_flags);
32853290
if (ret < 0) {
32863291
if (i == 0)
32873292
return ret;
@@ -3351,7 +3356,7 @@ static int xhci_queue_isoc_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
33513356
remainder |
33523357
TRB_INTR_TARGET(0);
33533358

3354-
queue_trb(xhci, ep_ring, false, more_trbs_coming,
3359+
queue_trb(xhci, ep_ring, false, more_trbs_coming, true,
33553360
lower_32_bits(addr),
33563361
upper_32_bits(addr),
33573362
length_field,
@@ -3433,7 +3438,7 @@ int xhci_queue_isoc_tx_prepare(struct xhci_hcd *xhci, gfp_t mem_flags,
34333438
* Do not insert any td of the urb to the ring if the check failed.
34343439
*/
34353440
ret = prepare_ring(xhci, ep_ring, le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK,
3436-
num_trbs, mem_flags);
3441+
num_trbs, true, mem_flags);
34373442
if (ret)
34383443
return ret;
34393444

@@ -3492,16 +3497,16 @@ static int queue_command(struct xhci_hcd *xhci, u32 field1, u32 field2,
34923497
reserved_trbs++;
34933498

34943499
ret = prepare_ring(xhci, xhci->cmd_ring, EP_STATE_RUNNING,
3495-
reserved_trbs, GFP_ATOMIC);
3500+
reserved_trbs, false, GFP_ATOMIC);
34963501
if (ret < 0) {
34973502
xhci_err(xhci, "ERR: No room for command on command ring\n");
34983503
if (command_must_succeed)
34993504
xhci_err(xhci, "ERR: Reserved TRB counting for "
35003505
"unfailable commands failed.\n");
35013506
return ret;
35023507
}
3503-
queue_trb(xhci, xhci->cmd_ring, false, false, field1, field2, field3,
3504-
field4 | xhci->cmd_ring->cycle_state);
3508+
queue_trb(xhci, xhci->cmd_ring, false, false, false, field1, field2,
3509+
field3, field4 | xhci->cmd_ring->cycle_state);
35053510
return 0;
35063511
}
35073512

‎drivers/usb/host/xhci.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,7 @@ struct xhci_hcd {
13181318
#define XHCI_EP_LIMIT_QUIRK (1 << 5)
13191319
#define XHCI_BROKEN_MSI (1 << 6)
13201320
#define XHCI_RESET_ON_RESUME (1 << 7)
1321+
#define XHCI_AMD_0x96_HOST (1 << 9)
13211322
unsigned int num_active_eps;
13221323
unsigned int limit_active_eps;
13231324
/* There are two roothubs to keep track of bus suspend info for */

0 commit comments

Comments
 (0)