1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * xHCI host controller driver 4 * 5 * Copyright (C) 2008 Intel Corp. 6 * 7 * Author: Sarah Sharp 8 * Some code borrowed from the Linux EHCI driver. 9 */ 10 11 /* 12 * Ring initialization rules: 13 * 1. Each segment is initialized to zero, except for link TRBs. 14 * 2. Ring cycle state = 0. This represents Producer Cycle State (PCS) or 15 * Consumer Cycle State (CCS), depending on ring function. 16 * 3. Enqueue pointer = dequeue pointer = address of first TRB in the segment. 17 * 18 * Ring behavior rules: 19 * 1. A ring is empty if enqueue == dequeue. This means there will always be at 20 * least one free TRB in the ring. This is useful if you want to turn that 21 * into a link TRB and expand the ring. 22 * 2. When incrementing an enqueue or dequeue pointer, if the next TRB is a 23 * link TRB, then load the pointer with the address in the link TRB. If the 24 * link TRB had its toggle bit set, you may need to update the ring cycle 25 * state (see cycle bit rules). You may have to do this multiple times 26 * until you reach a non-link TRB. 27 * 3. A ring is full if enqueue++ (for the definition of increment above) 28 * equals the dequeue pointer. 29 * 30 * Cycle bit rules: 31 * 1. When a consumer increments a dequeue pointer and encounters a toggle bit 32 * in a link TRB, it must toggle the ring cycle state. 33 * 2. When a producer increments an enqueue pointer and encounters a toggle bit 34 * in a link TRB, it must toggle the ring cycle state. 35 * 36 * Producer rules: 37 * 1. Check if ring is full before you enqueue. 38 * 2. Write the ring cycle state to the cycle bit in the TRB you're enqueuing. 39 * Update enqueue pointer between each write (which may update the ring 40 * cycle state). 41 * 3. Notify consumer. If SW is producer, it rings the doorbell for command 42 * and endpoint rings. If HC is the producer for the event ring, 43 * and it generates an interrupt according to interrupt modulation rules. 44 * 45 * Consumer rules: 46 * 1. Check if TRB belongs to you. If the cycle bit == your ring cycle state, 47 * the TRB is owned by the consumer. 48 * 2. Update dequeue pointer (which may update the ring cycle state) and 49 * continue processing TRBs until you reach a TRB which is not owned by you. 50 * 3. Notify the producer. SW is the consumer for the event ring, and it 51 * updates event ring dequeue pointer. HC is the consumer for the command and 52 * endpoint rings; it generates events on the event ring for these. 53 */ 54 55 #include <linux/jiffies.h> 56 #include <linux/scatterlist.h> 57 #include <linux/slab.h> 58 #include <linux/string_choices.h> 59 #include <linux/dma-mapping.h> 60 #include <linux/bitfield.h> 61 62 #include "xhci.h" 63 #include "xhci-trace.h" 64 65 static int queue_command(struct xhci_hcd *xhci, struct xhci_command *cmd, 66 u32 field1, u32 field2, 67 u32 field3, u32 field4, bool command_must_succeed); 68 69 /* 70 * Returns zero if the TRB isn't in this segment, otherwise it returns the DMA 71 * address of the TRB. 72 */ 73 dma_addr_t xhci_trb_virt_to_dma(struct xhci_segment *seg, 74 union xhci_trb *trb) 75 { 76 unsigned long segment_offset; 77 78 if (!seg || !trb || trb < seg->trbs) 79 return 0; 80 /* offset in TRBs */ 81 segment_offset = trb - seg->trbs; 82 if (segment_offset >= TRBS_PER_SEGMENT) 83 return 0; 84 return seg->dma + (segment_offset * sizeof(*trb)); 85 } 86 87 static union xhci_trb *xhci_dma_to_trb(struct xhci_segment *start_seg, 88 dma_addr_t dma, 89 struct xhci_segment **match_seg) 90 { 91 struct xhci_segment *seg; 92 93 xhci_for_each_ring_seg(start_seg, seg) { 94 if (in_range(dma, seg->dma, TRB_SEGMENT_SIZE)) { 95 if (match_seg) 96 *match_seg = seg; 97 return &seg->trbs[(dma - seg->dma) / sizeof(union xhci_trb)]; 98 } 99 } 100 101 return NULL; 102 } 103 104 static bool trb_is_noop(union xhci_trb *trb) 105 { 106 return TRB_TYPE_NOOP_LE32(trb->generic.field[3]); 107 } 108 109 static bool trb_is_link(union xhci_trb *trb) 110 { 111 return TRB_TYPE_LINK_LE32(trb->link.control); 112 } 113 114 static bool last_trb_on_seg(struct xhci_segment *seg, union xhci_trb *trb) 115 { 116 return trb == &seg->trbs[TRBS_PER_SEGMENT - 1]; 117 } 118 119 static bool last_trb_on_ring(struct xhci_ring *ring, 120 struct xhci_segment *seg, union xhci_trb *trb) 121 { 122 return last_trb_on_seg(seg, trb) && (seg->next == ring->first_seg); 123 } 124 125 static bool link_trb_toggles_cycle(union xhci_trb *trb) 126 { 127 return le32_to_cpu(trb->link.control) & LINK_TOGGLE; 128 } 129 130 static bool last_td_in_urb(struct xhci_td *td) 131 { 132 struct urb_priv *urb_priv = td->urb->hcpriv; 133 134 return urb_priv->num_tds_done == urb_priv->num_tds; 135 } 136 137 static bool unhandled_event_trb(struct xhci_ring *ring) 138 { 139 return ((le32_to_cpu(ring->dequeue->event_cmd.flags) & TRB_CYCLE) == 140 ring->cycle_state); 141 } 142 143 static void inc_td_cnt(struct urb *urb) 144 { 145 struct urb_priv *urb_priv = urb->hcpriv; 146 147 urb_priv->num_tds_done++; 148 } 149 150 static void trb_to_noop(union xhci_trb *trb, u32 noop_type, bool unchain_links) 151 { 152 if (trb_is_link(trb)) { 153 if (unchain_links) 154 trb->link.control &= cpu_to_le32(~TRB_CHAIN); 155 } else { 156 trb->generic.field[0] = 0; 157 trb->generic.field[1] = 0; 158 trb->generic.field[2] = 0; 159 /* Preserve only the cycle bit of this TRB */ 160 trb->generic.field[3] &= cpu_to_le32(TRB_CYCLE); 161 trb->generic.field[3] |= cpu_to_le32(TRB_TYPE(noop_type)); 162 } 163 } 164 165 static unsigned int trb_to_pos(struct xhci_segment *seg, union xhci_trb *trb) 166 { 167 return seg->num * TRBS_PER_SEGMENT + (trb - seg->trbs); 168 } 169 170 /* Updates trb to point to the next TRB in the ring, and updates seg if the next 171 * TRB is in a new segment. This does not skip over link TRBs, and it does not 172 * effect the ring dequeue or enqueue pointers. 173 */ 174 static void next_trb(struct xhci_segment **seg, 175 union xhci_trb **trb) 176 { 177 if (trb_is_link(*trb) || last_trb_on_seg(*seg, *trb)) { 178 *seg = (*seg)->next; 179 *trb = ((*seg)->trbs); 180 } else { 181 (*trb)++; 182 } 183 } 184 185 /* 186 * See Cycle bit rules. SW is the consumer for the event ring only. 187 */ 188 void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring) 189 { 190 unsigned int link_trb_count = 0; 191 192 /* event ring doesn't have link trbs, check for last trb */ 193 if (ring->type == TYPE_EVENT) { 194 if (!last_trb_on_seg(ring->deq_seg, ring->dequeue)) { 195 ring->dequeue++; 196 return; 197 } 198 if (last_trb_on_ring(ring, ring->deq_seg, ring->dequeue)) 199 ring->cycle_state ^= 1; 200 ring->deq_seg = ring->deq_seg->next; 201 ring->dequeue = ring->deq_seg->trbs; 202 203 trace_xhci_inc_deq(ring); 204 205 return; 206 } 207 208 /* All other rings have link trbs */ 209 if (!trb_is_link(ring->dequeue)) { 210 if (last_trb_on_seg(ring->deq_seg, ring->dequeue)) 211 xhci_warn(xhci, "Missing link TRB at end of segment\n"); 212 else 213 ring->dequeue++; 214 } 215 216 while (trb_is_link(ring->dequeue)) { 217 ring->deq_seg = ring->deq_seg->next; 218 ring->dequeue = ring->deq_seg->trbs; 219 220 trace_xhci_inc_deq(ring); 221 222 if (link_trb_count++ > ring->num_segs) { 223 xhci_warn(xhci, "Ring is an endless link TRB loop\n"); 224 break; 225 } 226 } 227 return; 228 } 229 230 /* 231 * If enqueue points at a link TRB, follow links until an ordinary TRB is reached. 232 * Toggle the cycle bit of passed link TRBs and optionally chain them. 233 */ 234 static void inc_enq_past_link(struct xhci_hcd *xhci, struct xhci_ring *ring, u32 chain) 235 { 236 unsigned int link_trb_count = 0; 237 238 while (trb_is_link(ring->enqueue)) { 239 240 /* 241 * Section 6.4.4.1 of the 0.95 spec says link TRBs cannot have the chain bit 242 * set, but other sections talk about dealing with the chain bit set. This was 243 * fixed in the 0.96 specification errata, but we have to assume that all 0.95 244 * xHCI hardware can't handle the chain bit being cleared on a link TRB. 245 * 246 * On 0.95 and some 0.96 HCs the chain bit is set once at segment initalization 247 * and never changed here. On all others, modify it as requested by the caller. 248 */ 249 if (!xhci_link_chain_quirk(xhci, ring->type)) { 250 ring->enqueue->link.control &= cpu_to_le32(~TRB_CHAIN); 251 ring->enqueue->link.control |= cpu_to_le32(chain); 252 } 253 254 /* Give this link TRB to the hardware */ 255 wmb(); 256 ring->enqueue->link.control ^= cpu_to_le32(TRB_CYCLE); 257 258 /* Toggle the cycle bit after the last ring segment. */ 259 if (link_trb_toggles_cycle(ring->enqueue)) 260 ring->cycle_state ^= 1; 261 262 ring->enq_seg = ring->enq_seg->next; 263 ring->enqueue = ring->enq_seg->trbs; 264 265 trace_xhci_inc_enq(ring); 266 267 if (link_trb_count++ > ring->num_segs) { 268 xhci_warn(xhci, "Link TRB loop at enqueue\n"); 269 break; 270 } 271 } 272 } 273 274 /* 275 * See Cycle bit rules. SW is the consumer for the event ring only. 276 * 277 * If we've just enqueued a TRB that is in the middle of a TD (meaning the 278 * chain bit is set), then set the chain bit in all the following link TRBs. 279 * If we've enqueued the last TRB in a TD, make sure the following link TRBs 280 * have their chain bit cleared (so that each Link TRB is a separate TD). 281 * 282 * @more_trbs_coming: Will you enqueue more TRBs before calling 283 * prepare_transfer()? 284 */ 285 static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring, 286 bool more_trbs_coming) 287 { 288 u32 chain; 289 290 chain = le32_to_cpu(ring->enqueue->generic.field[3]) & TRB_CHAIN; 291 292 if (last_trb_on_seg(ring->enq_seg, ring->enqueue)) { 293 xhci_err(xhci, "Tried to move enqueue past ring segment\n"); 294 return; 295 } 296 297 ring->enqueue++; 298 299 /* 300 * If we are in the middle of a TD or the caller plans to enqueue more 301 * TDs as one transfer (eg. control), traverse any link TRBs right now. 302 * Otherwise, enqueue can stay on a link until the next prepare_ring(). 303 * This avoids enqueue entering deq_seg and simplifies ring expansion. 304 */ 305 if (trb_is_link(ring->enqueue) && (chain || more_trbs_coming)) 306 inc_enq_past_link(xhci, ring, chain); 307 } 308 309 static bool dma_in_range(dma_addr_t dma, 310 struct xhci_segment *start_seg, union xhci_trb *start_trb, 311 struct xhci_segment *end_seg, union xhci_trb *end_trb) 312 { 313 unsigned int pos, start, end; 314 struct xhci_segment *pos_seg; 315 union xhci_trb *pos_trb = xhci_dma_to_trb(start_seg, dma, &pos_seg); 316 317 /* Is the trb dma address even part of the whole ring? */ 318 if (!pos_trb) 319 return false; 320 321 pos = trb_to_pos(pos_seg, pos_trb); 322 start = trb_to_pos(start_seg, start_trb); 323 end = trb_to_pos(end_seg, end_trb); 324 325 /* end position is smaller than start, search range wraps around */ 326 if (end < start) 327 return !(pos > end && pos < start); 328 329 return (pos >= start && pos <= end); 330 } 331 332 /* If the suspect DMA address is a TRB in this TD, this function returns true */ 333 static bool trb_in_td(struct xhci_td *td, dma_addr_t suspect_dma) 334 { 335 return dma_in_range(suspect_dma, td->start_seg, td->start_trb, 336 td->end_seg, td->end_trb); 337 } 338 339 /* 340 * Return number of free normal TRBs from enqueue to dequeue pointer on ring. 341 * Not counting an assumed link TRB at end of each TRBS_PER_SEGMENT sized segment. 342 * Only for transfer and command rings where driver is the producer, not for 343 * event rings. 344 */ 345 unsigned int xhci_num_trbs_free(struct xhci_ring *ring) 346 { 347 struct xhci_segment *enq_seg = ring->enq_seg; 348 union xhci_trb *enq = ring->enqueue; 349 union xhci_trb *last_on_seg; 350 unsigned int free = 0; 351 int i = 0; 352 353 /* Ring might be empty even if enq != deq if enq is left on a link trb */ 354 if (trb_is_link(enq)) { 355 enq_seg = enq_seg->next; 356 enq = enq_seg->trbs; 357 } 358 359 /* Empty ring, common case, don't walk the segments */ 360 if (enq == ring->dequeue) 361 return ring->num_segs * (TRBS_PER_SEGMENT - 1); 362 363 do { 364 if (ring->deq_seg == enq_seg && ring->dequeue >= enq) 365 return free + (ring->dequeue - enq); 366 last_on_seg = &enq_seg->trbs[TRBS_PER_SEGMENT - 1]; 367 free += last_on_seg - enq; 368 enq_seg = enq_seg->next; 369 enq = enq_seg->trbs; 370 } while (i++ < ring->num_segs); 371 372 return free; 373 } 374 375 /* 376 * Check to see if there's room to enqueue num_trbs on the ring and make sure 377 * enqueue pointer will not advance into dequeue segment. See rules above. 378 * return number of new segments needed to ensure this. 379 */ 380 381 static unsigned int xhci_ring_expansion_needed(struct xhci_hcd *xhci, struct xhci_ring *ring, 382 unsigned int num_trbs) 383 { 384 struct xhci_segment *seg; 385 int trbs_past_seg; 386 int enq_used; 387 int new_segs; 388 389 enq_used = ring->enqueue - ring->enq_seg->trbs; 390 391 /* how many trbs will be queued past the enqueue segment? */ 392 trbs_past_seg = enq_used + num_trbs - (TRBS_PER_SEGMENT - 1); 393 394 /* 395 * Consider expanding the ring already if num_trbs fills the current 396 * segment (i.e. trbs_past_seg == 0), not only when num_trbs goes into 397 * the next segment. Avoids confusing full ring with special empty ring 398 * case below 399 */ 400 if (trbs_past_seg < 0) 401 return 0; 402 403 /* Empty ring special case, enqueue stuck on link trb while dequeue advanced */ 404 if (trb_is_link(ring->enqueue) && ring->enq_seg->next->trbs == ring->dequeue) 405 return 0; 406 407 new_segs = 1 + (trbs_past_seg / (TRBS_PER_SEGMENT - 1)); 408 seg = ring->enq_seg; 409 410 while (new_segs > 0) { 411 seg = seg->next; 412 if (seg == ring->deq_seg) { 413 xhci_dbg(xhci, "Adding %d trbs requires expanding ring by %d segments\n", 414 num_trbs, new_segs); 415 return new_segs; 416 } 417 new_segs--; 418 } 419 420 return 0; 421 } 422 423 /* Ring the host controller doorbell after placing a command on the ring */ 424 void xhci_ring_cmd_db(struct xhci_hcd *xhci) 425 { 426 if (!(xhci->cmd_ring_state & CMD_RING_STATE_RUNNING)) 427 return; 428 429 xhci_dbg(xhci, "// Ding dong!\n"); 430 431 trace_xhci_ring_host_doorbell(0, DB_VALUE_HOST); 432 433 writel(DB_VALUE_HOST, &xhci->dba->doorbell[0]); 434 /* Flush PCI posted writes */ 435 readl(&xhci->dba->doorbell[0]); 436 } 437 438 static bool xhci_mod_cmd_timer(struct xhci_hcd *xhci) 439 { 440 return mod_delayed_work(system_percpu_wq, &xhci->cmd_timer, 441 msecs_to_jiffies(xhci->current_cmd->timeout_ms)); 442 } 443 444 static struct xhci_command *xhci_next_queued_cmd(struct xhci_hcd *xhci) 445 { 446 return list_first_entry_or_null(&xhci->cmd_list, struct xhci_command, 447 cmd_list); 448 } 449 450 /* 451 * Turn all commands on command ring with status set to "aborted" to no-op trbs. 452 * If there are other commands waiting then restart the ring and kick the timer. 453 * This must be called with command ring stopped and xhci->lock held. 454 */ 455 static void xhci_handle_stopped_cmd_ring(struct xhci_hcd *xhci, 456 struct xhci_command *cur_cmd) 457 { 458 struct xhci_command *i_cmd; 459 460 /* Turn all aborted commands in list to no-ops, then restart */ 461 list_for_each_entry(i_cmd, &xhci->cmd_list, cmd_list) { 462 463 if (i_cmd->status != COMP_COMMAND_ABORTED) 464 continue; 465 466 i_cmd->status = COMP_COMMAND_RING_STOPPED; 467 468 xhci_dbg(xhci, "Turn aborted command %p to no-op\n", 469 i_cmd->command_trb); 470 471 trb_to_noop(i_cmd->command_trb, TRB_CMD_NOOP, false); 472 473 /* 474 * caller waiting for completion is called when command 475 * completion event is received for these no-op commands 476 */ 477 } 478 479 xhci->cmd_ring_state = CMD_RING_STATE_RUNNING; 480 481 /* ring command ring doorbell to restart the command ring */ 482 if ((xhci->cmd_ring->dequeue != xhci->cmd_ring->enqueue) && 483 !(xhci->xhc_state & XHCI_STATE_DYING)) { 484 xhci->current_cmd = cur_cmd; 485 if (cur_cmd) 486 xhci_mod_cmd_timer(xhci); 487 xhci_ring_cmd_db(xhci); 488 } 489 } 490 491 /* Must be called with xhci->lock held, releases and acquires lock back */ 492 static int xhci_abort_cmd_ring(struct xhci_hcd *xhci, unsigned long flags) 493 { 494 struct xhci_segment *new_seg = xhci->cmd_ring->deq_seg; 495 union xhci_trb *new_deq = xhci->cmd_ring->dequeue; 496 u64 crcr; 497 int ret; 498 499 xhci_dbg(xhci, "Abort command ring\n"); 500 501 reinit_completion(&xhci->cmd_ring_stop_completion); 502 503 /* 504 * The control bits like command stop, abort are located in lower 505 * dword of the command ring control register. 506 * Some controllers require all 64 bits to be written to abort the ring. 507 * Make sure the upper dword is valid, pointing to the next command, 508 * avoiding corrupting the command ring pointer in case the command ring 509 * is stopped by the time the upper dword is written. 510 */ 511 next_trb(&new_seg, &new_deq); 512 if (trb_is_link(new_deq)) 513 next_trb(&new_seg, &new_deq); 514 515 crcr = xhci_trb_virt_to_dma(new_seg, new_deq); 516 xhci_write_64(xhci, crcr | CMD_RING_ABORT, &xhci->op_regs->cmd_ring); 517 518 /* Section 4.6.1.2 of xHCI 1.0 spec says software should also time the 519 * completion of the Command Abort operation. If CRR is not negated in 5 520 * seconds then driver handles it as if host died (-ENODEV). 521 * In the future we should distinguish between -ENODEV and -ETIMEDOUT 522 * and try to recover a -ETIMEDOUT with a host controller reset. 523 */ 524 ret = xhci_handshake(&xhci->op_regs->cmd_ring, 525 CMD_RING_RUNNING, 0, 5 * 1000 * 1000); 526 if (ret < 0) { 527 xhci_err(xhci, "Abort failed to stop command ring: %d\n", ret); 528 xhci_halt(xhci); 529 xhci_hc_died(xhci); 530 return ret; 531 } 532 /* 533 * Writing the CMD_RING_ABORT bit should cause a cmd completion event, 534 * however on some host hw the CMD_RING_RUNNING bit is correctly cleared 535 * but the completion event in never sent. Wait 2 secs (arbitrary 536 * number) to handle those cases after negation of CMD_RING_RUNNING. 537 */ 538 spin_unlock_irqrestore(&xhci->lock, flags); 539 ret = wait_for_completion_timeout(&xhci->cmd_ring_stop_completion, 540 msecs_to_jiffies(2000)); 541 spin_lock_irqsave(&xhci->lock, flags); 542 if (!ret) { 543 xhci_dbg(xhci, "No stop event for abort, ring start fail?\n"); 544 xhci_cleanup_command_queue(xhci); 545 } else { 546 xhci_handle_stopped_cmd_ring(xhci, xhci_next_queued_cmd(xhci)); 547 } 548 return 0; 549 } 550 551 void xhci_ring_ep_doorbell(struct xhci_hcd *xhci, 552 unsigned int slot_id, 553 unsigned int ep_index, 554 unsigned int stream_id) 555 { 556 __le32 __iomem *db_addr = &xhci->dba->doorbell[slot_id]; 557 struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index]; 558 unsigned int ep_state = ep->ep_state; 559 560 /* Don't ring the doorbell for this endpoint if there are pending 561 * cancellations because we don't want to interrupt processing. 562 * We don't want to restart any stream rings if there's a set dequeue 563 * pointer command pending because the device can choose to start any 564 * stream once the endpoint is on the HW schedule. 565 */ 566 if (ep_state & (EP_STOP_CMD_PENDING | SET_DEQ_PENDING | EP_HALTED | 567 EP_CLEARING_TT | EP_DROP_PENDING)) 568 return; 569 570 trace_xhci_ring_ep_doorbell(slot_id, DB_VALUE(ep_index, stream_id)); 571 572 writel(DB_VALUE(ep_index, stream_id), db_addr); 573 /* flush the write */ 574 readl(db_addr); 575 } 576 577 /* Ring the doorbell for any rings with pending URBs */ 578 void xhci_ring_doorbell_for_active_rings(struct xhci_hcd *xhci, unsigned int slot_id, 579 unsigned int ep_index) 580 { 581 unsigned int stream_id; 582 struct xhci_virt_ep *ep; 583 584 ep = &xhci->devs[slot_id]->eps[ep_index]; 585 586 /* A ring has pending URBs if its TD list is not empty */ 587 if (!(ep->ep_state & EP_HAS_STREAMS)) { 588 if (ep->ring && !(list_empty(&ep->ring->td_list))) 589 xhci_ring_ep_doorbell(xhci, slot_id, ep_index, 0); 590 return; 591 } 592 593 for (stream_id = 1; stream_id < ep->stream_info->num_streams; 594 stream_id++) { 595 struct xhci_stream_info *stream_info = ep->stream_info; 596 if (!list_empty(&stream_info->stream_rings[stream_id]->td_list)) 597 xhci_ring_ep_doorbell(xhci, slot_id, ep_index, 598 stream_id); 599 } 600 } 601 602 static struct xhci_virt_ep *xhci_get_virt_ep(struct xhci_hcd *xhci, 603 unsigned int slot_id, 604 unsigned int ep_index) 605 { 606 if (slot_id == 0 || slot_id > xhci->max_slots) { 607 xhci_warn(xhci, "Invalid slot_id %u\n", slot_id); 608 return NULL; 609 } 610 if (ep_index >= EP_CTX_PER_DEV) { 611 xhci_warn(xhci, "Invalid endpoint index %u\n", ep_index); 612 return NULL; 613 } 614 if (!xhci->devs[slot_id]) { 615 xhci_warn(xhci, "No xhci virt device for slot_id %u\n", slot_id); 616 return NULL; 617 } 618 619 return &xhci->devs[slot_id]->eps[ep_index]; 620 } 621 622 static struct xhci_ring *xhci_virt_ep_to_ring(struct xhci_hcd *xhci, 623 struct xhci_virt_ep *ep, 624 unsigned int stream_id) 625 { 626 /* common case, no streams */ 627 if (!(ep->ep_state & EP_HAS_STREAMS)) 628 return ep->ring; 629 630 if (!ep->stream_info) 631 return NULL; 632 633 if (stream_id == 0 || stream_id >= ep->stream_info->num_streams) { 634 xhci_warn(xhci, "Invalid stream_id %u request for slot_id %u ep_index %u\n", 635 stream_id, ep->vdev->slot_id, ep->ep_index); 636 return NULL; 637 } 638 639 return ep->stream_info->stream_rings[stream_id]; 640 } 641 642 /* Get the right ring for the given slot_id, ep_index and stream_id. 643 * If the endpoint supports streams, boundary check the URB's stream ID. 644 * If the endpoint doesn't support streams, return the singular endpoint ring. 645 */ 646 struct xhci_ring *xhci_triad_to_transfer_ring(struct xhci_hcd *xhci, 647 unsigned int slot_id, unsigned int ep_index, 648 unsigned int stream_id) 649 { 650 struct xhci_virt_ep *ep; 651 652 ep = xhci_get_virt_ep(xhci, slot_id, ep_index); 653 if (!ep) 654 return NULL; 655 656 return xhci_virt_ep_to_ring(xhci, ep, stream_id); 657 } 658 659 660 /* 661 * Get the hw dequeue pointer xHC stopped on, either directly from the 662 * endpoint context, or if streams are in use from the stream context. 663 * The returned hw_dequeue contains the lowest four bits with cycle state 664 * and possbile stream context type. 665 */ 666 static u64 xhci_get_hw_deq(struct xhci_hcd *xhci, struct xhci_virt_device *vdev, 667 unsigned int ep_index, unsigned int stream_id) 668 { 669 struct xhci_ep_ctx *ep_ctx; 670 struct xhci_stream_ctx *st_ctx; 671 struct xhci_virt_ep *ep; 672 673 ep = &vdev->eps[ep_index]; 674 675 if (ep->ep_state & EP_HAS_STREAMS) { 676 st_ctx = &ep->stream_info->stream_ctx_array[stream_id]; 677 return le64_to_cpu(st_ctx->stream_ring); 678 } 679 ep_ctx = xhci_get_ep_ctx(xhci, vdev->out_ctx, ep_index); 680 return le64_to_cpu(ep_ctx->deq); 681 } 682 683 /* 684 * Move the endpoint dequeue pointer to the next queued TD on ring->td_list or 685 * to enqueue if no TDs are queued (empty ring) 686 * All cancelled TDs on ring->td_list should be moved to ep->cancelled_td_list 687 * before calling this function 688 */ 689 static int xhci_move_deq_to_next_td(struct xhci_hcd *xhci, 690 struct xhci_virt_ep *ep, 691 unsigned int stream_id) 692 { 693 struct xhci_command *cmd; 694 struct xhci_ring *ring; 695 struct xhci_td *td; 696 dma_addr_t addr; 697 int new_cycle; 698 u32 trb_sct = 0; 699 int ret = 0; 700 701 ring = xhci_virt_ep_to_ring(xhci, ep, stream_id); 702 if (!ring) { 703 xhci_warn(xhci, "WARN can't find new dequeue, invalid stream ID %u\n", 704 stream_id); 705 return -ENODEV; 706 } 707 708 if ((ep->ep_state & SET_DEQ_PENDING)) { 709 xhci_warn(xhci, "Set TR Deq already pending\n"); 710 return -EBUSY; 711 } 712 713 /* This function gets called from contexts where it cannot sleep */ 714 cmd = xhci_alloc_command(xhci, false, GFP_ATOMIC); 715 if (!cmd) { 716 xhci_warn(xhci, "Can't alloc Set TR Deq cmd\n"); 717 return -ENOMEM; 718 } 719 720 /* 721 * Move dequeue to the beginning of next td, or to enqueue if ring is 722 * empty. Avoid moving dequeue to a link trb (empty ring) as it causes 723 * issues on some hosts. In that case advance the enqueue to next segment 724 * before moving dequeue to it 725 */ 726 727 if (list_empty(&ring->td_list)) { 728 if (trb_is_link(ring->enqueue)) 729 inc_enq_past_link(xhci, ring, 0); 730 ep->queued_deq_seg = ring->enq_seg; 731 ep->queued_deq_ptr = ring->enqueue; 732 new_cycle = ring->cycle_state; 733 } else { 734 td = list_first_entry(&ring->td_list, struct xhci_td, td_list); 735 ep->queued_deq_seg = td->start_seg; 736 ep->queued_deq_ptr = td->start_trb; 737 new_cycle = le32_to_cpu(td->start_trb->generic.field[3]) & TRB_CYCLE; 738 } 739 740 addr = xhci_trb_virt_to_dma(ep->queued_deq_seg, ep->queued_deq_ptr); 741 if (addr == 0) { 742 xhci_warn(xhci, "Can't find new dequeue dma of seg %p, ptr %p\n", 743 ep->queued_deq_seg, ep->queued_deq_ptr); 744 ret = -EINVAL; 745 goto err_out; 746 } 747 748 if (stream_id) 749 trb_sct = SCT_FOR_TRB(SCT_PRI_TR); 750 ret = queue_command(xhci, cmd, 751 lower_32_bits(addr) | trb_sct | new_cycle, 752 upper_32_bits(addr), 753 STREAM_ID_FOR_TRB(stream_id), SLOT_ID_FOR_TRB(ep->vdev->slot_id) | 754 EP_INDEX_FOR_TRB(ep->ep_index) | TRB_TYPE(TRB_SET_DEQ), false); 755 if (ret < 0) 756 goto err_out; 757 758 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, 759 "Set TR Deq ptr 0x%llx, cycle %u\n", addr, new_cycle); 760 761 /* 762 * Stop the TD queueing code from ringing the doorbell until this 763 * command completes. The HC won't set the dequeue pointer if the ring 764 * is running, and ringing the doorbell starts the ring. 765 */ 766 ep->ep_state |= SET_DEQ_PENDING; 767 xhci_ring_cmd_db(xhci); 768 769 return 0; 770 771 err_out: 772 xhci_free_command(xhci, cmd); 773 ep->queued_deq_seg = NULL; 774 ep->queued_deq_ptr = NULL; 775 776 return ret; 777 } 778 779 /* flip_cycle means flip the cycle bit of all but the first and last TRB. 780 * (The last TRB actually points to the ring enqueue pointer, which is not part 781 * of this TD.) This is used to remove partially enqueued isoc TDs from a ring. 782 */ 783 static void td_to_noop(struct xhci_hcd *xhci, struct xhci_virt_ep *ep, 784 struct xhci_td *td, bool flip_cycle) 785 { 786 bool unchain_links; 787 struct xhci_segment *seg = td->start_seg; 788 union xhci_trb *trb = td->start_trb; 789 790 /* link TRBs should now be unchained, but some old HCs expect otherwise */ 791 unchain_links = !xhci_link_chain_quirk(xhci, ep->ring ? ep->ring->type : TYPE_STREAM); 792 793 while (1) { 794 trb_to_noop(trb, TRB_TR_NOOP, unchain_links); 795 796 /* flip cycle if asked to */ 797 if (flip_cycle && trb != td->start_trb && trb != td->end_trb) 798 trb->generic.field[3] ^= cpu_to_le32(TRB_CYCLE); 799 800 if (trb == td->end_trb) 801 break; 802 803 next_trb(&seg, &trb); 804 } 805 } 806 807 static void xhci_giveback_urb_in_irq(struct xhci_hcd *xhci, 808 struct xhci_td *cur_td, int status) 809 { 810 struct urb *urb = cur_td->urb; 811 struct urb_priv *urb_priv = urb->hcpriv; 812 struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus); 813 814 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) { 815 xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs--; 816 if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs == 0) { 817 if (xhci->quirks & XHCI_AMD_PLL_FIX) 818 usb_amd_quirk_pll_enable(); 819 } 820 } 821 xhci_urb_free_priv(urb_priv); 822 usb_hcd_unlink_urb_from_ep(hcd, urb); 823 trace_xhci_urb_giveback(urb); 824 usb_hcd_giveback_urb(hcd, urb, status); 825 } 826 827 static void xhci_unmap_one_bounce_buffer(struct xhci_hcd *xhci, 828 struct xhci_ring *ring, struct xhci_td *td, 829 struct xhci_segment *seg) 830 { 831 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; 832 struct urb *urb = td->urb; 833 size_t len; 834 835 if (usb_urb_dir_out(urb)) { 836 dma_unmap_single(dev, seg->bounce_dma, ring->bounce_buf_len, 837 DMA_TO_DEVICE); 838 goto done; 839 } 840 841 dma_unmap_single(dev, seg->bounce_dma, ring->bounce_buf_len, 842 DMA_FROM_DEVICE); 843 /* for in transfers we need to copy the data from bounce to sg */ 844 if (urb->num_sgs) { 845 len = sg_pcopy_from_buffer(urb->sg, urb->num_sgs, seg->bounce_buf, 846 seg->bounce_len, seg->bounce_offs); 847 if (len != seg->bounce_len) 848 xhci_warn(xhci, "WARN Wrong bounce buffer read length: %zu != %d\n", 849 len, seg->bounce_len); 850 } else { 851 memcpy(urb->transfer_buffer + seg->bounce_offs, seg->bounce_buf, 852 seg->bounce_len); 853 } 854 done: 855 seg->bounce_len = 0; 856 seg->bounce_offs = 0; 857 } 858 859 static void xhci_unmap_td_bounce_buffer(struct xhci_hcd *xhci, 860 struct xhci_ring *ring, struct xhci_td *td) 861 { 862 struct xhci_segment *seg; 863 int i = 0; 864 865 if (!td->bounce_seg || !ring || !td->urb) 866 return; 867 868 /* td->bounce_seg is the last one bounced, unmap them all */ 869 for (seg = td->start_seg; i++ < ring->num_segs; seg = seg->next) { 870 if (seg->bounce_len) 871 xhci_unmap_one_bounce_buffer(xhci, ring, td, seg); 872 if (seg == td->bounce_seg) 873 break; 874 } 875 } 876 877 static void xhci_td_cleanup(struct xhci_hcd *xhci, struct xhci_td *td, 878 struct xhci_ring *ep_ring, int status) 879 { 880 struct urb *urb = NULL; 881 882 /* Clean up the endpoint's TD list */ 883 urb = td->urb; 884 885 /* if a bounce buffer was used to align this td then unmap it */ 886 xhci_unmap_td_bounce_buffer(xhci, ep_ring, td); 887 888 /* Do one last check of the actual transfer length. 889 * If the host controller said we transferred more data than the buffer 890 * length, urb->actual_length will be a very big number (since it's 891 * unsigned). Play it safe and say we didn't transfer anything. 892 */ 893 if (urb->actual_length > urb->transfer_buffer_length) { 894 xhci_warn(xhci, "URB req %u and actual %u transfer length mismatch\n", 895 urb->transfer_buffer_length, urb->actual_length); 896 urb->actual_length = 0; 897 status = 0; 898 } 899 /* TD might be removed from td_list if we are giving back a cancelled URB */ 900 if (!list_empty(&td->td_list)) 901 list_del_init(&td->td_list); 902 /* Giving back a cancelled URB, or if a slated TD completed anyway */ 903 if (!list_empty(&td->cancelled_td_list)) 904 list_del_init(&td->cancelled_td_list); 905 906 inc_td_cnt(urb); 907 /* Giveback the urb when all the tds are completed */ 908 if (last_td_in_urb(td)) { 909 if ((urb->actual_length != urb->transfer_buffer_length && 910 (urb->transfer_flags & URB_SHORT_NOT_OK)) || 911 (status != 0 && !usb_endpoint_xfer_isoc(&urb->ep->desc))) 912 xhci_dbg(xhci, "Giveback URB %p, len = %d, expected = %d, status = %d\n", 913 urb, urb->actual_length, 914 urb->transfer_buffer_length, status); 915 916 /* set isoc urb status to 0 just as EHCI, UHCI, and OHCI */ 917 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) 918 status = 0; 919 xhci_giveback_urb_in_irq(xhci, td, status); 920 } 921 } 922 923 /* Give back previous TD and move on to the next TD. */ 924 static void xhci_dequeue_td(struct xhci_hcd *xhci, struct xhci_td *td, struct xhci_ring *ring, 925 u32 status) 926 { 927 ring->dequeue = td->end_trb; 928 ring->deq_seg = td->end_seg; 929 inc_deq(xhci, ring); 930 931 xhci_td_cleanup(xhci, td, ring, status); 932 } 933 934 /* Complete the cancelled URBs we unlinked from td_list. */ 935 static void xhci_giveback_invalidated_tds(struct xhci_hcd *xhci, struct xhci_virt_ep *ep) 936 { 937 struct xhci_ring *ring; 938 struct xhci_td *td, *tmp_td; 939 940 list_for_each_entry_safe(td, tmp_td, &ep->cancelled_td_list, 941 cancelled_td_list) { 942 943 ring = xhci_urb_to_transfer_ring(xhci, td->urb); 944 945 if (td->cancel_status == TD_CLEARED) { 946 xhci_dbg(xhci, "%s: Giveback cancelled URB %p TD\n", 947 __func__, td->urb); 948 xhci_td_cleanup(xhci, td, ring, td->status); 949 } else { 950 xhci_dbg(xhci, "%s: Keep cancelled URB %p TD as cancel_status is %d\n", 951 __func__, td->urb, td->cancel_status); 952 } 953 if (xhci->xhc_state & XHCI_STATE_DYING) 954 return; 955 } 956 } 957 958 static int xhci_reset_halted_ep(struct xhci_hcd *xhci, unsigned int slot_id, 959 unsigned int ep_index, enum xhci_ep_reset_type reset_type) 960 { 961 struct xhci_command *command; 962 int ret = 0; 963 964 command = xhci_alloc_command(xhci, false, GFP_ATOMIC); 965 if (!command) { 966 ret = -ENOMEM; 967 goto done; 968 } 969 970 xhci_dbg(xhci, "%s-reset ep %u, slot %u\n", 971 (reset_type == EP_HARD_RESET) ? "Hard" : "Soft", 972 ep_index, slot_id); 973 974 ret = xhci_queue_reset_ep(xhci, command, slot_id, ep_index, reset_type); 975 done: 976 if (ret) 977 xhci_err(xhci, "ERROR queuing reset endpoint for slot %d ep_index %d, %d\n", 978 slot_id, ep_index, ret); 979 return ret; 980 } 981 982 static int xhci_handle_halted_endpoint(struct xhci_hcd *xhci, 983 struct xhci_virt_ep *ep, 984 struct xhci_td *td, 985 enum xhci_ep_reset_type reset_type) 986 { 987 struct xhci_port *rhub_port = ep->vdev->rhub_port; 988 unsigned int slot_id = ep->vdev->slot_id; 989 int err; 990 991 /* 992 * Avoid resetting endpoint if link is inactive or device disonnected. 993 * Can cause host hang. 994 * Device will be reset to recover an inactive link, so don't do anything 995 */ 996 if (rhub_port->link_inactive || !rhub_port->connected) { 997 ep->ep_state |= EP_DROP_PENDING; 998 return -ENODEV; 999 } 1000 1001 /* add td to cancelled list and let reset ep handler take care of it */ 1002 if (reset_type == EP_HARD_RESET) { 1003 ep->ep_state |= EP_HARD_CLEAR_TOGGLE; 1004 if (td && list_empty(&td->cancelled_td_list)) { 1005 list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list); 1006 td->cancel_status = TD_HALTED; 1007 } 1008 } 1009 1010 if (ep->ep_state & EP_HALTED) { 1011 xhci_dbg(xhci, "Reset ep command for ep_index %d already pending\n", 1012 ep->ep_index); 1013 return 0; 1014 } 1015 1016 err = xhci_reset_halted_ep(xhci, slot_id, ep->ep_index, reset_type); 1017 if (err) 1018 return err; 1019 1020 ep->ep_state |= EP_HALTED; 1021 1022 xhci_ring_cmd_db(xhci); 1023 1024 return 0; 1025 } 1026 1027 /* 1028 * Fix up the ep ring first, so HW stops executing cancelled TDs. 1029 * We have the xHCI lock, so nothing can modify this list until we drop it. 1030 * We're also in the event handler, so we can't get re-interrupted if another 1031 * Stop Endpoint command completes. 1032 * 1033 * only call this when ring is not in a running state 1034 */ 1035 1036 static int xhci_invalidate_cancelled_tds(struct xhci_hcd *xhci, struct xhci_virt_ep *ep) 1037 { 1038 struct xhci_td *td = NULL; 1039 struct xhci_td *tmp_td = NULL; 1040 struct xhci_td *cached_td = NULL; 1041 struct xhci_ring *ring; 1042 u64 hw_deq; 1043 int err; 1044 1045 /* 1046 * This is not going to work if the hardware is changing its dequeue 1047 * pointers as we look at them. Completion handler will call us later. 1048 */ 1049 if (ep->ep_state & SET_DEQ_PENDING) 1050 return 0; 1051 1052 list_for_each_entry_safe(td, tmp_td, &ep->cancelled_td_list, cancelled_td_list) { 1053 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, 1054 "Removing canceled TD starting at 0x%llx (dma) in stream %u URB %p", 1055 (unsigned long long)xhci_trb_virt_to_dma( 1056 td->start_seg, td->start_trb), 1057 td->urb->stream_id, td->urb); 1058 list_del_init(&td->td_list); 1059 ring = xhci_urb_to_transfer_ring(xhci, td->urb); 1060 if (!ring) { 1061 xhci_warn(xhci, "WARN Cancelled URB %p has invalid stream ID %u.\n", 1062 td->urb, td->urb->stream_id); 1063 continue; 1064 } 1065 1066 /* device disconnected or link error, ep will be dropped */ 1067 if (ep->ep_state & EP_DROP_PENDING) { 1068 td->cancel_status = TD_CLEARED; 1069 continue; 1070 } 1071 1072 /* 1073 * If a ring stopped on the TD we need to cancel then we have to 1074 * move the xHC endpoint ring dequeue pointer past this TD. 1075 * Rings halted due to STALL may show hw_deq is past the stalled 1076 * TD, but still require a set TR Deq command to flush xHC cache. 1077 */ 1078 hw_deq = xhci_get_hw_deq(xhci, ep->vdev, ep->ep_index, 1079 td->urb->stream_id); 1080 hw_deq &= TR_DEQ_PTR_MASK; 1081 1082 if (td->cancel_status == TD_HALTED || trb_in_td(td, hw_deq)) { 1083 switch (td->cancel_status) { 1084 case TD_CLEARED: /* TD is already no-op */ 1085 case TD_CLEARING_CACHE: /* set TR deq command already queued */ 1086 break; 1087 case TD_DIRTY: /* TD is cached, clear it */ 1088 case TD_HALTED: 1089 case TD_CLEARING_CACHE_DEFERRED: 1090 if (cached_td) { 1091 if (cached_td->urb->stream_id != td->urb->stream_id) { 1092 /* Multiple streams case, defer move dq */ 1093 xhci_dbg(xhci, 1094 "Move dq deferred: stream %u URB %p\n", 1095 td->urb->stream_id, td->urb); 1096 td->cancel_status = TD_CLEARING_CACHE_DEFERRED; 1097 break; 1098 } 1099 1100 /* Should never happen, but clear the TD if it does */ 1101 xhci_warn(xhci, 1102 "Found multiple active URBs %p and %p in stream %u?\n", 1103 td->urb, cached_td->urb, 1104 td->urb->stream_id); 1105 td_to_noop(xhci, ep, cached_td, false); 1106 cached_td->cancel_status = TD_CLEARED; 1107 } 1108 td_to_noop(xhci, ep, td, false); 1109 td->cancel_status = TD_CLEARING_CACHE; 1110 cached_td = td; 1111 break; 1112 } 1113 } else { 1114 td_to_noop(xhci, ep, td, false); 1115 td->cancel_status = TD_CLEARED; 1116 } 1117 } 1118 1119 /* If there's no need to move the dequeue pointer then we're done */ 1120 if (!cached_td) 1121 return 0; 1122 1123 err = xhci_move_deq_to_next_td(xhci, ep, cached_td->urb->stream_id); 1124 1125 if (err) { 1126 /* Failed to move past cached td, just set cached TDs to no-op */ 1127 list_for_each_entry_safe(td, tmp_td, &ep->cancelled_td_list, cancelled_td_list) { 1128 /* 1129 * Deferred TDs need to have the deq pointer set after the above command 1130 * completes, so if that failed we just give up on all of them (and 1131 * complain loudly since this could cause issues due to caching). 1132 */ 1133 if (td->cancel_status != TD_CLEARING_CACHE && 1134 td->cancel_status != TD_CLEARING_CACHE_DEFERRED) 1135 continue; 1136 xhci_warn(xhci, "Failed to clear cancelled cached URB %p, mark clear anyway\n", 1137 td->urb); 1138 td_to_noop(xhci, ep, td, false); 1139 td->cancel_status = TD_CLEARED; 1140 } 1141 } 1142 return 0; 1143 } 1144 1145 /* 1146 * Erase queued TDs from transfer ring(s) and give back those the xHC didn't 1147 * stop on. If necessary, queue commands to move the xHC off cancelled TDs it 1148 * stopped on. Those will be given back later when the commands complete. 1149 * 1150 * Call under xhci->lock on a stopped endpoint. 1151 */ 1152 void xhci_process_cancelled_tds(struct xhci_hcd *xhci, struct xhci_virt_ep *ep) 1153 { 1154 xhci_invalidate_cancelled_tds(xhci, ep); 1155 xhci_giveback_invalidated_tds(xhci, ep); 1156 } 1157 1158 /* 1159 * Returns the TD the endpoint ring halted on. 1160 * Only call for non-running rings without streams. 1161 */ 1162 static struct xhci_td *find_halted_td(struct xhci_hcd *xhci, struct xhci_virt_ep *ep) 1163 { 1164 struct xhci_td *td; 1165 u64 hw_deq; 1166 1167 if (!list_empty(&ep->ring->td_list)) { /* Not streams compatible */ 1168 hw_deq = xhci_get_hw_deq(xhci, ep->vdev, ep->ep_index, 0); 1169 hw_deq &= TR_DEQ_PTR_MASK; 1170 td = list_first_entry(&ep->ring->td_list, struct xhci_td, td_list); 1171 if (trb_in_td(td, hw_deq)) 1172 return td; 1173 } 1174 return NULL; 1175 } 1176 1177 /* 1178 * When we get a command completion for a Stop Endpoint Command, we need to 1179 * unlink any cancelled TDs from the ring. There are two ways to do that: 1180 * 1181 * 1. If the HW was in the middle of processing the TD that needs to be 1182 * cancelled, then we must move the ring's dequeue pointer past the last TRB 1183 * in the TD with a Set Dequeue Pointer Command. 1184 * 2. Otherwise, we turn all the TRBs in the TD into No-op TRBs (with the chain 1185 * bit cleared) so that the HW will skip over them. 1186 */ 1187 static void xhci_handle_cmd_stop_ep(struct xhci_hcd *xhci, int slot_id, 1188 union xhci_trb *trb, u32 comp_code) 1189 { 1190 unsigned int ep_index; 1191 struct xhci_virt_ep *ep; 1192 struct xhci_ep_ctx *ep_ctx; 1193 struct xhci_td *td = NULL; 1194 enum xhci_ep_reset_type reset_type; 1195 struct xhci_command *command; 1196 int err; 1197 1198 if (unlikely(TRB_TO_SUSPEND_PORT(le32_to_cpu(trb->generic.field[3])))) { 1199 if (!xhci->devs[slot_id]) 1200 xhci_warn(xhci, "Stop endpoint command completion for disabled slot %u\n", 1201 slot_id); 1202 return; 1203 } 1204 1205 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3])); 1206 ep = xhci_get_virt_ep(xhci, slot_id, ep_index); 1207 if (!ep) 1208 return; 1209 1210 ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep_index); 1211 1212 trace_xhci_handle_cmd_stop_ep(ep_ctx); 1213 1214 if (comp_code == COMP_CONTEXT_STATE_ERROR) { 1215 /* 1216 * If stop endpoint command raced with a halting endpoint we need to 1217 * reset the host side endpoint first. 1218 * If the TD we halted on isn't cancelled the TD should be given back 1219 * with a proper error code, and the ring dequeue moved past the TD. 1220 * If streams case we can't find hw_deq, or the TD we halted on so do a 1221 * soft reset. 1222 * 1223 * Proper error code is unknown here, it would be -EPIPE if device side 1224 * of enadpoit halted (aka STALL), and -EPROTO if not (transaction error) 1225 * We use -EPROTO, if device is stalled it should return a stall error on 1226 * next transfer, which then will return -EPIPE, and device side stall is 1227 * noted and cleared by class driver. 1228 */ 1229 switch (GET_EP_CTX_STATE(ep_ctx)) { 1230 case EP_STATE_HALTED: 1231 xhci_dbg(xhci, "Stop ep completion raced with stall\n"); 1232 /* 1233 * If the halt happened before Stop Endpoint failed, its transfer event 1234 * should have already been handled and Reset Endpoint should be pending. 1235 */ 1236 if (ep->ep_state & EP_HALTED) 1237 goto reset_done; 1238 1239 if (ep->ep_state & EP_HAS_STREAMS) { 1240 reset_type = EP_SOFT_RESET; 1241 } else { 1242 reset_type = EP_HARD_RESET; 1243 td = find_halted_td(xhci, ep); 1244 if (td) 1245 td->status = -EPROTO; 1246 } 1247 /* reset ep, reset handler cleans up cancelled tds */ 1248 err = xhci_handle_halted_endpoint(xhci, ep, td, reset_type); 1249 xhci_dbg(xhci, "Stop ep completion resetting ep, status %d\n", err); 1250 if (err) 1251 break; 1252 reset_done: 1253 /* Reset EP handler will clean up cancelled TDs */ 1254 ep->ep_state &= ~EP_STOP_CMD_PENDING; 1255 return; 1256 case EP_STATE_STOPPED: 1257 /* 1258 * Per xHCI 4.6.9, Stop Endpoint command on a Stopped 1259 * EP is a Context State Error, and EP stays Stopped. 1260 * 1261 * But maybe it failed on Halted, and somebody ran Reset 1262 * Endpoint later. EP state is now Stopped and EP_HALTED 1263 * still set because Reset EP handler will run after us. 1264 */ 1265 if (ep->ep_state & EP_HALTED) 1266 break; 1267 /* 1268 * On some HCs EP state remains Stopped for some tens of 1269 * us to a few ms or more after a doorbell ring, and any 1270 * new Stop Endpoint fails without aborting the restart. 1271 * This handler may run quickly enough to still see this 1272 * Stopped state, but it will soon change to Running. 1273 * 1274 * Assume this bug on unexpected Stop Endpoint failures. 1275 * Keep retrying until the EP starts and stops again or 1276 * up to a timeout (a defective HC may never start, or a 1277 * driver bug may cause stopping an already stopped EP). 1278 */ 1279 if (time_is_before_jiffies(ep->stop_time + msecs_to_jiffies(100))) 1280 break; 1281 fallthrough; 1282 case EP_STATE_RUNNING: 1283 /* Race, HW handled stop ep cmd before ep was running */ 1284 xhci_dbg(xhci, "Stop ep completion ctx error, ctx_state %d\n", 1285 GET_EP_CTX_STATE(ep_ctx)); 1286 1287 command = xhci_alloc_command(xhci, false, GFP_ATOMIC); 1288 if (!command) { 1289 ep->ep_state &= ~EP_STOP_CMD_PENDING; 1290 return; 1291 } 1292 xhci_queue_stop_endpoint(xhci, command, slot_id, ep_index, 0); 1293 xhci_ring_cmd_db(xhci); 1294 1295 return; 1296 default: 1297 break; 1298 } 1299 } 1300 1301 /* link is inactive or disconnected, ep is not running and shouldn't be restarted */ 1302 if (ep->vdev->rhub_port->link_inactive || !ep->vdev->rhub_port->connected) 1303 ep->ep_state |= EP_DROP_PENDING; 1304 1305 /* will queue a set TR deq if stopped on a cancelled, uncleared TD */ 1306 xhci_invalidate_cancelled_tds(xhci, ep); 1307 ep->ep_state &= ~EP_STOP_CMD_PENDING; 1308 1309 /* Otherwise ring the doorbell(s) to restart queued transfers */ 1310 xhci_giveback_invalidated_tds(xhci, ep); 1311 xhci_ring_doorbell_for_active_rings(xhci, slot_id, ep_index); 1312 } 1313 1314 static void xhci_kill_ring_urbs(struct xhci_hcd *xhci, struct xhci_ring *ring) 1315 { 1316 struct xhci_td *cur_td; 1317 struct xhci_td *tmp; 1318 1319 list_for_each_entry_safe(cur_td, tmp, &ring->td_list, td_list) { 1320 list_del_init(&cur_td->td_list); 1321 1322 if (!list_empty(&cur_td->cancelled_td_list)) 1323 list_del_init(&cur_td->cancelled_td_list); 1324 1325 xhci_unmap_td_bounce_buffer(xhci, ring, cur_td); 1326 1327 inc_td_cnt(cur_td->urb); 1328 if (last_td_in_urb(cur_td)) 1329 xhci_giveback_urb_in_irq(xhci, cur_td, -ESHUTDOWN); 1330 } 1331 } 1332 1333 static void xhci_kill_endpoint_urbs(struct xhci_hcd *xhci, 1334 int slot_id, int ep_index) 1335 { 1336 struct xhci_td *cur_td; 1337 struct xhci_td *tmp; 1338 struct xhci_virt_ep *ep; 1339 struct xhci_ring *ring; 1340 1341 ep = xhci_get_virt_ep(xhci, slot_id, ep_index); 1342 if (!ep) 1343 return; 1344 1345 if ((ep->ep_state & EP_HAS_STREAMS) || 1346 (ep->ep_state & EP_GETTING_NO_STREAMS)) { 1347 int stream_id; 1348 1349 for (stream_id = 1; stream_id < ep->stream_info->num_streams; 1350 stream_id++) { 1351 ring = ep->stream_info->stream_rings[stream_id]; 1352 if (!ring) 1353 continue; 1354 1355 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, 1356 "Killing URBs for slot ID %u, ep index %u, stream %u", 1357 slot_id, ep_index, stream_id); 1358 xhci_kill_ring_urbs(xhci, ring); 1359 } 1360 } else { 1361 ring = ep->ring; 1362 if (!ring) 1363 return; 1364 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, 1365 "Killing URBs for slot ID %u, ep index %u", 1366 slot_id, ep_index); 1367 xhci_kill_ring_urbs(xhci, ring); 1368 } 1369 1370 list_for_each_entry_safe(cur_td, tmp, &ep->cancelled_td_list, 1371 cancelled_td_list) { 1372 list_del_init(&cur_td->cancelled_td_list); 1373 inc_td_cnt(cur_td->urb); 1374 1375 if (last_td_in_urb(cur_td)) 1376 xhci_giveback_urb_in_irq(xhci, cur_td, -ESHUTDOWN); 1377 } 1378 } 1379 1380 /* 1381 * host controller died, register read returns 0xffffffff 1382 * Complete pending commands, mark them ABORTED. 1383 * URBs need to be given back as usb core might be waiting with device locks 1384 * held for the URBs to finish during device disconnect, blocking host remove. 1385 * 1386 * Call with xhci->lock held. 1387 * lock is relased and re-acquired while giving back urb. 1388 */ 1389 void xhci_hc_died(struct xhci_hcd *xhci) 1390 { 1391 bool notify; 1392 int i, j; 1393 1394 if (xhci->xhc_state & XHCI_STATE_DYING) 1395 return; 1396 1397 notify = !(xhci->xhc_state & XHCI_STATE_REMOVING); 1398 if (notify) 1399 xhci_err(xhci, "xHCI host controller not responding, assume dead\n"); 1400 xhci->xhc_state |= XHCI_STATE_DYING; 1401 1402 xhci_cleanup_command_queue(xhci); 1403 1404 /* return any pending urbs, remove may be waiting for them */ 1405 for (i = 0; i <= xhci->max_slots; i++) { 1406 if (!xhci->devs[i]) 1407 continue; 1408 for (j = 0; j < 31; j++) 1409 xhci_kill_endpoint_urbs(xhci, i, j); 1410 } 1411 1412 /* inform usb core hc died if PCI remove isn't already handling it */ 1413 if (notify) 1414 usb_hc_died(xhci_to_hcd(xhci)); 1415 } 1416 1417 /* 1418 * When we get a completion for a Set Transfer Ring Dequeue Pointer command, 1419 * we need to clear the set deq pending flag in the endpoint ring state, so that 1420 * the TD queueing code can ring the doorbell again. We also need to ring the 1421 * endpoint doorbell to restart the ring, but only if there aren't more 1422 * cancellations pending. 1423 */ 1424 static void xhci_handle_cmd_set_deq(struct xhci_hcd *xhci, int slot_id, 1425 union xhci_trb *trb, u32 cmd_comp_code) 1426 { 1427 unsigned int ep_index; 1428 unsigned int stream_id; 1429 struct xhci_ring *ep_ring; 1430 struct xhci_virt_ep *ep; 1431 struct xhci_ep_ctx *ep_ctx; 1432 struct xhci_slot_ctx *slot_ctx; 1433 struct xhci_stream_ctx *stream_ctx; 1434 struct xhci_td *td, *tmp_td; 1435 1436 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3])); 1437 stream_id = TRB_TO_STREAM_ID(le32_to_cpu(trb->generic.field[2])); 1438 ep = xhci_get_virt_ep(xhci, slot_id, ep_index); 1439 if (!ep) 1440 return; 1441 1442 ep_ring = xhci_virt_ep_to_ring(xhci, ep, stream_id); 1443 if (!ep_ring) { 1444 xhci_warn(xhci, "WARN Set TR deq ptr command for freed stream ID %u\n", 1445 stream_id); 1446 /* XXX: Harmless??? */ 1447 goto cleanup; 1448 } 1449 1450 ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep_index); 1451 slot_ctx = xhci_get_slot_ctx(xhci, ep->vdev->out_ctx); 1452 trace_xhci_handle_cmd_set_deq(slot_ctx); 1453 trace_xhci_handle_cmd_set_deq_ep(ep_ctx); 1454 1455 if (ep->ep_state & EP_HAS_STREAMS) { 1456 stream_ctx = &ep->stream_info->stream_ctx_array[stream_id]; 1457 trace_xhci_handle_cmd_set_deq_stream(ep->stream_info, stream_id); 1458 } 1459 1460 if (cmd_comp_code != COMP_SUCCESS) { 1461 unsigned int ep_state; 1462 unsigned int slot_state; 1463 1464 switch (cmd_comp_code) { 1465 case COMP_TRB_ERROR: 1466 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd invalid because of stream ID configuration\n"); 1467 break; 1468 case COMP_CONTEXT_STATE_ERROR: 1469 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed due to incorrect slot or ep state.\n"); 1470 ep_state = GET_EP_CTX_STATE(ep_ctx); 1471 slot_state = le32_to_cpu(slot_ctx->dev_state); 1472 slot_state = GET_SLOT_STATE(slot_state); 1473 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, 1474 "Slot state = %u, EP state = %u", 1475 slot_state, ep_state); 1476 break; 1477 case COMP_SLOT_NOT_ENABLED_ERROR: 1478 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed because slot %u was not enabled.\n", 1479 slot_id); 1480 break; 1481 default: 1482 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd with unknown completion code of %u.\n", 1483 cmd_comp_code); 1484 break; 1485 } 1486 /* OK what do we do now? The endpoint state is hosed, and we 1487 * should never get to this point if the synchronization between 1488 * queueing, and endpoint state are correct. This might happen 1489 * if the device gets disconnected after we've finished 1490 * cancelling URBs, which might not be an error... 1491 */ 1492 } else { 1493 u64 deq; 1494 /* 4.6.10 deq ptr is written to the stream ctx for streams */ 1495 if (ep->ep_state & EP_HAS_STREAMS) { 1496 deq = le64_to_cpu(stream_ctx->stream_ring) & TR_DEQ_PTR_MASK; 1497 1498 /* 1499 * Cadence xHCI controllers store some endpoint state 1500 * information within Rsvd0 fields of Stream Endpoint 1501 * context. This field is not cleared during Set TR 1502 * Dequeue Pointer command which causes XDMA to skip 1503 * over transfer ring and leads to data loss on stream 1504 * pipe. 1505 * To fix this issue driver must clear Rsvd0 field. 1506 */ 1507 if (xhci->quirks & XHCI_CDNS_SCTX_QUIRK) { 1508 stream_ctx->reserved[0] = 0; 1509 stream_ctx->reserved[1] = 0; 1510 } 1511 } else { 1512 deq = le64_to_cpu(ep_ctx->deq) & TR_DEQ_PTR_MASK; 1513 } 1514 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, 1515 "Successful Set TR Deq Ptr cmd, deq = @%08llx", deq); 1516 if (xhci_trb_virt_to_dma(ep->queued_deq_seg, 1517 ep->queued_deq_ptr) == deq) { 1518 /* Update the ring's dequeue segment and dequeue pointer 1519 * to reflect the new position. 1520 */ 1521 ep_ring->deq_seg = ep->queued_deq_seg; 1522 ep_ring->dequeue = ep->queued_deq_ptr; 1523 } else { 1524 xhci_warn(xhci, "Mismatch between completed Set TR Deq Ptr command & xHCI internal state.\n"); 1525 xhci_warn(xhci, "ep deq seg = %p, deq ptr = %p\n", 1526 ep->queued_deq_seg, ep->queued_deq_ptr); 1527 } 1528 } 1529 /* HW cached TDs cleared from cache, give them back */ 1530 list_for_each_entry_safe(td, tmp_td, &ep->cancelled_td_list, 1531 cancelled_td_list) { 1532 ep_ring = xhci_urb_to_transfer_ring(xhci, td->urb); 1533 if (td->cancel_status == TD_CLEARING_CACHE) { 1534 td->cancel_status = TD_CLEARED; 1535 xhci_dbg(xhci, "%s: Giveback cancelled URB %p TD\n", 1536 __func__, td->urb); 1537 xhci_td_cleanup(xhci, td, ep_ring, td->status); 1538 } else { 1539 xhci_dbg(xhci, "%s: Keep cancelled URB %p TD as cancel_status is %d\n", 1540 __func__, td->urb, td->cancel_status); 1541 } 1542 } 1543 cleanup: 1544 ep->ep_state &= ~SET_DEQ_PENDING; 1545 ep->queued_deq_seg = NULL; 1546 ep->queued_deq_ptr = NULL; 1547 1548 /* Check for deferred or newly cancelled TDs */ 1549 if (!list_empty(&ep->cancelled_td_list)) { 1550 xhci_dbg(xhci, "%s: Pending TDs to clear, continuing with invalidation\n", 1551 __func__); 1552 xhci_invalidate_cancelled_tds(xhci, ep); 1553 /* Try to restart the endpoint if all is done */ 1554 xhci_ring_doorbell_for_active_rings(xhci, slot_id, ep_index); 1555 /* Start giving back any TDs invalidated above */ 1556 xhci_giveback_invalidated_tds(xhci, ep); 1557 } else { 1558 /* Restart any rings with pending URBs */ 1559 xhci_dbg(xhci, "%s: All TDs cleared, ring doorbell\n", __func__); 1560 xhci_ring_doorbell_for_active_rings(xhci, slot_id, ep_index); 1561 } 1562 } 1563 1564 static void xhci_handle_cmd_reset_ep(struct xhci_hcd *xhci, int slot_id, 1565 union xhci_trb *trb, u32 cmd_comp_code) 1566 { 1567 struct xhci_virt_ep *ep; 1568 struct xhci_ep_ctx *ep_ctx; 1569 unsigned int ep_index; 1570 1571 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3])); 1572 ep = xhci_get_virt_ep(xhci, slot_id, ep_index); 1573 if (!ep) 1574 return; 1575 1576 ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep_index); 1577 trace_xhci_handle_cmd_reset_ep(ep_ctx); 1578 1579 /* This command will only fail if the endpoint wasn't halted, 1580 * but we don't care. 1581 */ 1582 xhci_dbg_trace(xhci, trace_xhci_dbg_reset_ep, 1583 "Ignoring reset ep completion code of %u", cmd_comp_code); 1584 1585 /* Cleanup cancelled TDs as ep is stopped. May queue a Set TR Deq cmd */ 1586 xhci_invalidate_cancelled_tds(xhci, ep); 1587 1588 /* Clear our internal halted state */ 1589 ep->ep_state &= ~EP_HALTED; 1590 1591 xhci_giveback_invalidated_tds(xhci, ep); 1592 1593 /* if this was a soft reset, then restart */ 1594 if ((le32_to_cpu(trb->generic.field[3])) & TRB_TSP) 1595 xhci_ring_doorbell_for_active_rings(xhci, slot_id, ep_index); 1596 } 1597 1598 static void xhci_handle_cmd_enable_slot(int slot_id, struct xhci_command *command, 1599 u32 cmd_comp_code) 1600 { 1601 if (cmd_comp_code == COMP_SUCCESS) 1602 command->slot_id = slot_id; 1603 else 1604 command->slot_id = 0; 1605 } 1606 1607 static void xhci_handle_cmd_disable_slot(struct xhci_hcd *xhci, int slot_id, 1608 u32 cmd_comp_code) 1609 { 1610 struct xhci_virt_device *virt_dev; 1611 struct xhci_slot_ctx *slot_ctx; 1612 1613 virt_dev = xhci->devs[slot_id]; 1614 if (!virt_dev) 1615 return; 1616 1617 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx); 1618 trace_xhci_handle_cmd_disable_slot(slot_ctx); 1619 1620 if (xhci->quirks & XHCI_EP_LIMIT_QUIRK) 1621 /* Delete default control endpoint resources */ 1622 xhci_free_device_endpoint_resources(xhci, virt_dev, true); 1623 if (cmd_comp_code == COMP_SUCCESS) { 1624 xhci->dcbaa.ctx_array[slot_id] = 0; 1625 xhci->devs[slot_id] = NULL; 1626 } 1627 } 1628 1629 static void xhci_handle_cmd_config_ep(struct xhci_hcd *xhci, int slot_id) 1630 { 1631 struct xhci_virt_device *virt_dev; 1632 struct xhci_input_control_ctx *ctrl_ctx; 1633 struct xhci_ep_ctx *ep_ctx; 1634 unsigned int ep_index; 1635 u32 add_flags; 1636 1637 /* 1638 * Configure endpoint commands can come from the USB core configuration 1639 * or alt setting changes, or when streams were being configured. 1640 */ 1641 1642 virt_dev = xhci->devs[slot_id]; 1643 if (!virt_dev) 1644 return; 1645 ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx); 1646 if (!ctrl_ctx) { 1647 xhci_warn(xhci, "Could not get input context, bad type.\n"); 1648 return; 1649 } 1650 1651 add_flags = le32_to_cpu(ctrl_ctx->add_flags); 1652 1653 /* Input ctx add_flags are the endpoint index plus one */ 1654 ep_index = xhci_last_valid_endpoint(add_flags) - 1; 1655 1656 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->out_ctx, ep_index); 1657 trace_xhci_handle_cmd_config_ep(ep_ctx); 1658 1659 return; 1660 } 1661 1662 static void xhci_handle_cmd_addr_dev(struct xhci_hcd *xhci, int slot_id) 1663 { 1664 struct xhci_virt_device *vdev; 1665 struct xhci_slot_ctx *slot_ctx; 1666 1667 vdev = xhci->devs[slot_id]; 1668 if (!vdev) 1669 return; 1670 slot_ctx = xhci_get_slot_ctx(xhci, vdev->out_ctx); 1671 trace_xhci_handle_cmd_addr_dev(slot_ctx); 1672 } 1673 1674 static void xhci_handle_cmd_reset_dev(struct xhci_hcd *xhci, int slot_id) 1675 { 1676 struct xhci_virt_device *vdev; 1677 struct xhci_slot_ctx *slot_ctx; 1678 1679 vdev = xhci->devs[slot_id]; 1680 if (!vdev) { 1681 xhci_warn(xhci, "Reset device command completion for disabled slot %u\n", 1682 slot_id); 1683 return; 1684 } 1685 slot_ctx = xhci_get_slot_ctx(xhci, vdev->out_ctx); 1686 trace_xhci_handle_cmd_reset_dev(slot_ctx); 1687 1688 xhci_dbg(xhci, "Completed reset device command.\n"); 1689 } 1690 1691 static void xhci_handle_cmd_nec_get_fw(struct xhci_hcd *xhci, 1692 struct xhci_event_cmd *event) 1693 { 1694 if (!(xhci->quirks & XHCI_NEC_HOST)) { 1695 xhci_warn(xhci, "WARN NEC_GET_FW command on non-NEC host\n"); 1696 return; 1697 } 1698 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, 1699 "NEC firmware version %2x.%02x", 1700 NEC_FW_MAJOR(le32_to_cpu(event->status)), 1701 NEC_FW_MINOR(le32_to_cpu(event->status))); 1702 } 1703 1704 static void xhci_complete_del_and_free_cmd(struct xhci_command *cmd, u32 comp_code, u32 comp_param) 1705 { 1706 list_del(&cmd->cmd_list); 1707 1708 if (cmd->completion) { 1709 cmd->status = comp_code; 1710 cmd->comp_param = comp_param; 1711 complete(cmd->completion); 1712 } else { 1713 kfree(cmd); 1714 } 1715 } 1716 1717 void xhci_cleanup_command_queue(struct xhci_hcd *xhci) 1718 { 1719 struct xhci_command *cur_cmd, *tmp_cmd; 1720 xhci->current_cmd = NULL; 1721 list_for_each_entry_safe(cur_cmd, tmp_cmd, &xhci->cmd_list, cmd_list) 1722 xhci_complete_del_and_free_cmd(cur_cmd, COMP_COMMAND_ABORTED, 0); 1723 } 1724 1725 void xhci_handle_command_timeout(struct work_struct *work) 1726 { 1727 struct xhci_hcd *xhci; 1728 unsigned long flags; 1729 char str[XHCI_MSG_MAX]; 1730 u64 hw_ring_state; 1731 u32 cmd_field3; 1732 u32 usbsts; 1733 1734 xhci = container_of(to_delayed_work(work), struct xhci_hcd, cmd_timer); 1735 1736 spin_lock_irqsave(&xhci->lock, flags); 1737 1738 /* 1739 * If timeout work is pending, or current_cmd is NULL, it means we 1740 * raced with command completion. Command is handled so just return. 1741 */ 1742 if (!xhci->current_cmd || delayed_work_pending(&xhci->cmd_timer)) { 1743 spin_unlock_irqrestore(&xhci->lock, flags); 1744 return; 1745 } 1746 1747 cmd_field3 = le32_to_cpu(xhci->current_cmd->command_trb->generic.field[3]); 1748 usbsts = readl(&xhci->op_regs->status); 1749 xhci_dbg(xhci, "Command timeout, USBSTS:%s\n", xhci_decode_usbsts(str, usbsts)); 1750 1751 /* Bail out and tear down xhci if a stop endpoint command failed */ 1752 if (TRB_FIELD_TO_TYPE(cmd_field3) == TRB_STOP_RING) { 1753 struct xhci_virt_ep *ep; 1754 1755 xhci_warn(xhci, "xHCI host not responding to stop endpoint command\n"); 1756 1757 ep = xhci_get_virt_ep(xhci, TRB_TO_SLOT_ID(cmd_field3), 1758 TRB_TO_EP_INDEX(cmd_field3)); 1759 if (ep) 1760 ep->ep_state &= ~EP_STOP_CMD_PENDING; 1761 1762 xhci_halt(xhci); 1763 xhci_hc_died(xhci); 1764 goto time_out_completed; 1765 } 1766 1767 /* mark this command to be cancelled */ 1768 xhci->current_cmd->status = COMP_COMMAND_ABORTED; 1769 1770 /* Make sure command ring is running before aborting it */ 1771 hw_ring_state = xhci_read_64(xhci, &xhci->op_regs->cmd_ring); 1772 if (hw_ring_state == ~(u64)0) { 1773 xhci_hc_died(xhci); 1774 goto time_out_completed; 1775 } 1776 1777 if ((xhci->cmd_ring_state & CMD_RING_STATE_RUNNING) && 1778 (hw_ring_state & CMD_RING_RUNNING)) { 1779 /* Prevent new doorbell, and start command abort */ 1780 xhci->cmd_ring_state = CMD_RING_STATE_ABORTED; 1781 xhci_dbg(xhci, "Command timeout\n"); 1782 xhci_abort_cmd_ring(xhci, flags); 1783 goto time_out_completed; 1784 } 1785 1786 /* host removed. Bail out */ 1787 if (xhci->xhc_state & XHCI_STATE_REMOVING) { 1788 xhci_dbg(xhci, "host removed, ring start fail?\n"); 1789 xhci_cleanup_command_queue(xhci); 1790 1791 goto time_out_completed; 1792 } 1793 1794 /* command timeout on stopped ring, ring can't be aborted */ 1795 xhci_dbg(xhci, "Command timeout on stopped ring\n"); 1796 xhci_handle_stopped_cmd_ring(xhci, xhci->current_cmd); 1797 1798 time_out_completed: 1799 spin_unlock_irqrestore(&xhci->lock, flags); 1800 return; 1801 } 1802 1803 static void handle_cmd_completion(struct xhci_hcd *xhci, 1804 struct xhci_event_cmd *event) 1805 { 1806 unsigned int slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags)); 1807 u32 status = le32_to_cpu(event->status); 1808 u64 cmd_dma; 1809 dma_addr_t cmd_dequeue_dma; 1810 u32 cmd_comp_code; 1811 union xhci_trb *cmd_trb; 1812 struct xhci_command *cmd; 1813 u32 cmd_type; 1814 1815 if (slot_id > xhci->max_slots) { 1816 xhci_warn(xhci, "Invalid slot_id %u\n", slot_id); 1817 return; 1818 } 1819 1820 cmd_dma = le64_to_cpu(event->cmd_trb); 1821 cmd_trb = xhci->cmd_ring->dequeue; 1822 1823 trace_xhci_handle_command(xhci->cmd_ring, &cmd_trb->generic, cmd_dma); 1824 1825 cmd_comp_code = GET_COMP_CODE(le32_to_cpu(event->status)); 1826 1827 /* If CMD ring stopped we own the trbs between enqueue and dequeue */ 1828 if (cmd_comp_code == COMP_COMMAND_RING_STOPPED) { 1829 complete_all(&xhci->cmd_ring_stop_completion); 1830 return; 1831 } 1832 1833 cmd_dequeue_dma = xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg, 1834 cmd_trb); 1835 /* 1836 * Check whether the completion event is for our internal kept 1837 * command. 1838 */ 1839 if (!cmd_dequeue_dma || cmd_dma != (u64)cmd_dequeue_dma) { 1840 xhci_warn(xhci, 1841 "ERROR mismatched command completion event\n"); 1842 return; 1843 } 1844 1845 cmd = list_first_entry(&xhci->cmd_list, struct xhci_command, cmd_list); 1846 1847 cancel_delayed_work(&xhci->cmd_timer); 1848 1849 if (cmd->command_trb != xhci->cmd_ring->dequeue) { 1850 xhci_err(xhci, 1851 "Command completion event does not match command\n"); 1852 return; 1853 } 1854 1855 /* 1856 * Host aborted the command ring, check if the current command was 1857 * supposed to be aborted, otherwise continue normally. 1858 * The command ring is stopped now, but the xHC will issue a Command 1859 * Ring Stopped event which will cause us to restart it. 1860 */ 1861 if (cmd_comp_code == COMP_COMMAND_ABORTED) { 1862 xhci->cmd_ring_state = CMD_RING_STATE_STOPPED; 1863 if (cmd->status == COMP_COMMAND_ABORTED) { 1864 if (xhci->current_cmd == cmd) 1865 xhci->current_cmd = NULL; 1866 goto event_handled; 1867 } 1868 } 1869 1870 cmd_type = TRB_FIELD_TO_TYPE(le32_to_cpu(cmd_trb->generic.field[3])); 1871 switch (cmd_type) { 1872 case TRB_ENABLE_SLOT: 1873 xhci_handle_cmd_enable_slot(slot_id, cmd, cmd_comp_code); 1874 break; 1875 case TRB_DISABLE_SLOT: 1876 xhci_handle_cmd_disable_slot(xhci, slot_id, cmd_comp_code); 1877 break; 1878 case TRB_CONFIG_EP: 1879 if (!cmd->completion) 1880 xhci_handle_cmd_config_ep(xhci, slot_id); 1881 break; 1882 case TRB_EVAL_CONTEXT: 1883 break; 1884 case TRB_ADDR_DEV: 1885 xhci_handle_cmd_addr_dev(xhci, slot_id); 1886 break; 1887 case TRB_STOP_RING: 1888 WARN_ON(slot_id != TRB_TO_SLOT_ID( 1889 le32_to_cpu(cmd_trb->generic.field[3]))); 1890 if (!cmd->completion) 1891 xhci_handle_cmd_stop_ep(xhci, slot_id, cmd_trb, 1892 cmd_comp_code); 1893 break; 1894 case TRB_SET_DEQ: 1895 WARN_ON(slot_id != TRB_TO_SLOT_ID( 1896 le32_to_cpu(cmd_trb->generic.field[3]))); 1897 xhci_handle_cmd_set_deq(xhci, slot_id, cmd_trb, cmd_comp_code); 1898 break; 1899 case TRB_CMD_NOOP: 1900 /* Is this an aborted command turned to NO-OP? */ 1901 if (cmd->status == COMP_COMMAND_RING_STOPPED) 1902 cmd_comp_code = COMP_COMMAND_RING_STOPPED; 1903 break; 1904 case TRB_RESET_EP: 1905 WARN_ON(slot_id != TRB_TO_SLOT_ID( 1906 le32_to_cpu(cmd_trb->generic.field[3]))); 1907 xhci_handle_cmd_reset_ep(xhci, slot_id, cmd_trb, cmd_comp_code); 1908 break; 1909 case TRB_RESET_DEV: 1910 /* SLOT_ID field in reset device cmd completion event TRB is 0. 1911 * Use the SLOT_ID from the command TRB instead (xhci 4.6.11) 1912 */ 1913 slot_id = TRB_TO_SLOT_ID( 1914 le32_to_cpu(cmd_trb->generic.field[3])); 1915 xhci_handle_cmd_reset_dev(xhci, slot_id); 1916 break; 1917 case TRB_NEC_GET_FW: 1918 xhci_handle_cmd_nec_get_fw(xhci, event); 1919 break; 1920 case TRB_GET_BW: 1921 break; 1922 default: 1923 /* Skip over unknown commands on the event ring */ 1924 xhci_info(xhci, "INFO unknown command type %d\n", cmd_type); 1925 break; 1926 } 1927 1928 /* restart timer if this wasn't the last command */ 1929 if (!list_is_singular(&xhci->cmd_list)) { 1930 xhci->current_cmd = list_first_entry(&cmd->cmd_list, 1931 struct xhci_command, cmd_list); 1932 xhci_mod_cmd_timer(xhci); 1933 } else if (xhci->current_cmd == cmd) { 1934 xhci->current_cmd = NULL; 1935 } 1936 1937 event_handled: 1938 xhci_complete_del_and_free_cmd(cmd, cmd_comp_code, COMP_PARAM(status)); 1939 1940 inc_deq(xhci, xhci->cmd_ring); 1941 } 1942 1943 static void handle_vendor_event(struct xhci_hcd *xhci, 1944 union xhci_trb *event, u32 trb_type) 1945 { 1946 xhci_dbg(xhci, "Vendor specific event TRB type = %u\n", trb_type); 1947 if (trb_type == TRB_NEC_CMD_COMP && (xhci->quirks & XHCI_NEC_HOST)) 1948 handle_cmd_completion(xhci, &event->event_cmd); 1949 } 1950 1951 static void handle_device_notification(struct xhci_hcd *xhci, 1952 union xhci_trb *event) 1953 { 1954 u32 slot_id; 1955 struct usb_device *udev; 1956 1957 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->generic.field[3])); 1958 if (!xhci->devs[slot_id]) { 1959 xhci_warn(xhci, "Device Notification event for " 1960 "unused slot %u\n", slot_id); 1961 return; 1962 } 1963 1964 xhci_dbg(xhci, "Device Wake Notification event for slot ID %u\n", 1965 slot_id); 1966 udev = xhci->devs[slot_id]->udev; 1967 if (udev && udev->parent) 1968 usb_wakeup_notification(udev->parent, udev->portnum); 1969 } 1970 1971 /* 1972 * Quirk hanlder for errata seen on Cavium ThunderX2 processor XHCI 1973 * Controller. 1974 * As per ThunderX2errata-129 USB 2 device may come up as USB 1 1975 * If a connection to a USB 1 device is followed by another connection 1976 * to a USB 2 device. 1977 * 1978 * Reset the PHY after the USB device is disconnected if device speed 1979 * is less than HCD_USB3. 1980 * Retry the reset sequence max of 4 times checking the PLL lock status. 1981 * 1982 */ 1983 static void xhci_cavium_reset_phy_quirk(struct xhci_hcd *xhci) 1984 { 1985 struct usb_hcd *hcd = xhci_to_hcd(xhci); 1986 u32 pll_lock_check; 1987 u32 retry_count = 4; 1988 1989 do { 1990 /* Assert PHY reset */ 1991 writel(0x6F, hcd->regs + 0x1048); 1992 udelay(10); 1993 /* De-assert the PHY reset */ 1994 writel(0x7F, hcd->regs + 0x1048); 1995 udelay(200); 1996 pll_lock_check = readl(hcd->regs + 0x1070); 1997 } while (!(pll_lock_check & 0x1) && --retry_count); 1998 } 1999 2000 static void handle_port_status(struct xhci_hcd *xhci, union xhci_trb *event) 2001 { 2002 struct xhci_virt_device *vdev = NULL; 2003 struct xhci_bus_state *bus_state; 2004 struct xhci_port *port; 2005 struct usb_hcd *hcd; 2006 bool bogus_port_status = false; 2007 unsigned int hcd_portnum; 2008 u32 cmd_reg; 2009 u32 port_id; 2010 u32 portsc; 2011 u32 pls; 2012 2013 /* Port status change events always have a successful completion code */ 2014 if (GET_COMP_CODE(le32_to_cpu(event->generic.field[2])) != COMP_SUCCESS) 2015 xhci_warn(xhci, 2016 "WARN: xHC returned failed port status event\n"); 2017 2018 port_id = GET_PORT_ID(le32_to_cpu(event->generic.field[0])); 2019 2020 if ((port_id <= 0) || (port_id > xhci->max_ports)) { 2021 xhci_warn(xhci, "Port change event with invalid port ID %d\n", 2022 port_id); 2023 return; 2024 } 2025 2026 port = &xhci->hw_ports[port_id - 1]; 2027 if (!port || !port->rhub || port->hcd_portnum == DUPLICATE_ENTRY) { 2028 xhci_warn(xhci, "Port change event, no port for port ID %u\n", 2029 port_id); 2030 bogus_port_status = true; 2031 goto cleanup; 2032 } 2033 2034 if (port->slot_id) 2035 vdev = xhci->devs[port->slot_id]; 2036 2037 /* We might get interrupts after shared_hcd is removed */ 2038 if (port->rhub == &xhci->usb3_rhub && xhci_get_usb3_hcd(xhci) == NULL) { 2039 xhci_dbg(xhci, "ignore port event for removed USB3 hcd\n"); 2040 bogus_port_status = true; 2041 goto cleanup; 2042 } 2043 2044 hcd = port->rhub->hcd; 2045 bus_state = &port->rhub->bus_state; 2046 hcd_portnum = port->hcd_portnum; 2047 portsc = xhci_portsc_readl(port); 2048 pls = portsc & PORT_PLS_MASK; 2049 2050 xhci_dbg(xhci, "Port change event, %d-%d, id %d, portsc: 0x%x\n", 2051 hcd->self.busnum, hcd_portnum + 1, port_id, portsc); 2052 2053 trace_xhci_handle_port_status(port, portsc); 2054 2055 if (hcd->state == HC_STATE_SUSPENDED) { 2056 xhci_dbg(xhci, "resume root hub\n"); 2057 usb_hcd_resume_root_hub(hcd); 2058 } 2059 2060 /* 2061 * Tag broken links to avoid retries while hub driver sorts it out. 2062 * Link status is not relible while port is in reset. 2063 */ 2064 if (!(portsc & PORT_RESET)) { 2065 port->link_inactive = (pls == XDEV_INACTIVE); 2066 port->connected = !!(portsc & PORT_CONNECT); 2067 } 2068 2069 if ((portsc & PORT_PLC) && (portsc & PORT_PLS_MASK) == XDEV_RESUME) { 2070 xhci_dbg(xhci, "port resume event for port %d\n", port_id); 2071 2072 cmd_reg = readl(&xhci->op_regs->command); 2073 if (!(cmd_reg & CMD_RUN)) { 2074 xhci_warn(xhci, "xHC is not running.\n"); 2075 goto cleanup; 2076 } 2077 2078 if (DEV_SUPERSPEED_ANY(portsc)) { 2079 xhci_dbg(xhci, "remote wake SS port %d\n", port_id); 2080 /* Set a flag to say the port signaled remote wakeup, 2081 * so we can tell the difference between the end of 2082 * device and host initiated resume. 2083 */ 2084 bus_state->port_remote_wakeup |= 1 << hcd_portnum; 2085 xhci_test_and_clear_bit(xhci, port, PORT_PLC); 2086 usb_hcd_start_port_resume(&hcd->self, hcd_portnum); 2087 xhci_set_link_state(xhci, port, XDEV_U0); 2088 /* Need to wait until the next link state change 2089 * indicates the device is actually in U0. 2090 */ 2091 bogus_port_status = true; 2092 goto cleanup; 2093 } else if (!test_bit(hcd_portnum, &bus_state->resuming_ports)) { 2094 xhci_dbg(xhci, "resume HS port %d\n", port_id); 2095 port->resume_timestamp = jiffies + 2096 msecs_to_jiffies(USB_RESUME_TIMEOUT); 2097 set_bit(hcd_portnum, &bus_state->resuming_ports); 2098 /* Do the rest in GetPortStatus after resume time delay. 2099 * Avoid polling roothub status before that so that a 2100 * usb device auto-resume latency around ~40ms. 2101 */ 2102 set_bit(HCD_FLAG_POLL_RH, &hcd->flags); 2103 mod_timer(&hcd->rh_timer, 2104 port->resume_timestamp); 2105 usb_hcd_start_port_resume(&hcd->self, hcd_portnum); 2106 bogus_port_status = true; 2107 } 2108 } 2109 2110 if ((portsc & PORT_PLC) && 2111 DEV_SUPERSPEED_ANY(portsc) && 2112 ((portsc & PORT_PLS_MASK) == XDEV_U0 || 2113 (portsc & PORT_PLS_MASK) == XDEV_U1 || 2114 (portsc & PORT_PLS_MASK) == XDEV_U2)) { 2115 xhci_dbg(xhci, "resume SS port %d finished\n", port_id); 2116 complete(&port->u3exit_done); 2117 /* We've just brought the device into U0/1/2 through either the 2118 * Resume state after a device remote wakeup, or through the 2119 * U3Exit state after a host-initiated resume. If it's a device 2120 * initiated remote wake, don't pass up the link state change, 2121 * so the roothub behavior is consistent with external 2122 * USB 3.0 hub behavior. 2123 */ 2124 if (vdev) 2125 xhci_ring_device(xhci, port->slot_id); 2126 if (bus_state->port_remote_wakeup & (1 << hcd_portnum)) { 2127 xhci_test_and_clear_bit(xhci, port, PORT_PLC); 2128 usb_wakeup_notification(hcd->self.root_hub, 2129 hcd_portnum + 1); 2130 bogus_port_status = true; 2131 goto cleanup; 2132 } 2133 } 2134 2135 /* 2136 * Check to see if xhci-hub.c is waiting on RExit to U0 transition (or 2137 * RExit to a disconnect state). If so, let the driver know it's 2138 * out of the RExit state. 2139 */ 2140 if (hcd->speed < HCD_USB3 && port->rexit_active) { 2141 complete(&port->rexit_done); 2142 port->rexit_active = false; 2143 bogus_port_status = true; 2144 goto cleanup; 2145 } 2146 2147 if (hcd->speed < HCD_USB3) { 2148 xhci_test_and_clear_bit(xhci, port, PORT_PLC); 2149 if ((xhci->quirks & XHCI_RESET_PLL_ON_DISCONNECT) && 2150 (portsc & PORT_CSC) && !(portsc & PORT_CONNECT)) 2151 xhci_cavium_reset_phy_quirk(xhci); 2152 } 2153 2154 cleanup: 2155 2156 /* Don't make the USB core poll the roothub if we got a bad port status 2157 * change event. Besides, at that point we can't tell which roothub 2158 * (USB 2.0 or USB 3.0) to kick. 2159 */ 2160 if (bogus_port_status) 2161 return; 2162 2163 /* 2164 * xHCI port-status-change events occur when the "or" of all the 2165 * status-change bits in the portsc register changes from 0 to 1. 2166 * New status changes won't cause an event if any other change 2167 * bits are still set. When an event occurs, switch over to 2168 * polling to avoid losing status changes. 2169 */ 2170 xhci_dbg(xhci, "%s: starting usb%d port polling.\n", 2171 __func__, hcd->self.busnum); 2172 set_bit(HCD_FLAG_POLL_RH, &hcd->flags); 2173 spin_unlock(&xhci->lock); 2174 /* Pass this up to the core */ 2175 usb_hcd_poll_rh_status(hcd); 2176 spin_lock(&xhci->lock); 2177 } 2178 2179 static void xhci_clear_hub_tt_buffer(struct xhci_hcd *xhci, struct xhci_td *td, 2180 struct xhci_virt_ep *ep) 2181 { 2182 /* 2183 * As part of low/full-speed endpoint-halt processing 2184 * we must clear the TT buffer (USB 2.0 specification 11.17.5). 2185 */ 2186 if (td->urb->dev->tt && !usb_pipeint(td->urb->pipe) && 2187 (td->urb->dev->tt->hub != xhci_to_hcd(xhci)->self.root_hub) && 2188 !(ep->ep_state & EP_CLEARING_TT)) { 2189 ep->ep_state |= EP_CLEARING_TT; 2190 td->urb->ep->hcpriv = td->urb->dev; 2191 if (usb_hub_clear_tt_buffer(td->urb)) 2192 ep->ep_state &= ~EP_CLEARING_TT; 2193 } 2194 } 2195 2196 /* 2197 * Check if xhci internal endpoint state has gone to a "halt" state due to an 2198 * error or stall, including default control pipe protocol stall. 2199 * The internal halt needs to be cleared with a reset endpoint command. 2200 * 2201 * External device side is also halted in functional stall cases. Class driver 2202 * will clear the device halt with a CLEAR_FEATURE(ENDPOINT_HALT) request later. 2203 */ 2204 static bool xhci_halted_host_endpoint(struct xhci_hcd *xhci, struct xhci_ep_ctx *ep_ctx, 2205 unsigned int comp_code) 2206 { 2207 int ep_type = CTX_TO_EP_TYPE(le32_to_cpu(ep_ctx->ep_info2)); 2208 2209 switch (comp_code) { 2210 case COMP_STALL_ERROR: 2211 /* on xHCI this always halts, including protocol stall */ 2212 return true; 2213 case COMP_BABBLE_DETECTED_ERROR: 2214 /* 2215 * The 0.95 spec says a babbling control endpoint is not halted. 2216 * The 0.96 spec says it is. Some HW claims to be 0.95 2217 * compliant, but it halts the control endpoint anyway. 2218 * Check endpoint context if endpoint is halted. 2219 */ 2220 if (xhci->hci_version <= 0x95 && ep_type == CTRL_EP) 2221 return GET_EP_CTX_STATE(ep_ctx) == EP_STATE_HALTED; 2222 2223 fallthrough; 2224 case COMP_USB_TRANSACTION_ERROR: 2225 case COMP_SPLIT_TRANSACTION_ERROR: 2226 /* these errors halt all non-isochronous endpoints */ 2227 return ep_type != ISOC_IN_EP && ep_type != ISOC_OUT_EP; 2228 } 2229 2230 return false; 2231 } 2232 2233 int xhci_is_vendor_info_code(struct xhci_hcd *xhci, unsigned int trb_comp_code) 2234 { 2235 if (trb_comp_code >= 224 && trb_comp_code <= 255) { 2236 /* Vendor defined "informational" completion code, 2237 * treat as not-an-error. 2238 */ 2239 xhci_dbg(xhci, "Vendor defined info completion code %u\n", 2240 trb_comp_code); 2241 xhci_dbg(xhci, "Treating code as success.\n"); 2242 return 1; 2243 } 2244 return 0; 2245 } 2246 2247 static void finish_td(struct xhci_hcd *xhci, struct xhci_virt_ep *ep, 2248 struct xhci_ring *ep_ring, struct xhci_td *td, 2249 u32 trb_comp_code) 2250 { 2251 struct xhci_ep_ctx *ep_ctx; 2252 2253 ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep->ep_index); 2254 2255 switch (trb_comp_code) { 2256 case COMP_STOPPED_LENGTH_INVALID: 2257 case COMP_STOPPED_SHORT_PACKET: 2258 case COMP_STOPPED: 2259 /* 2260 * The "Stop Endpoint" completion will take care of any 2261 * stopped TDs. A stopped TD may be restarted, so don't update 2262 * the ring dequeue pointer or take this TD off any lists yet. 2263 */ 2264 return; 2265 } 2266 2267 if (xhci_halted_host_endpoint(xhci, ep_ctx, trb_comp_code)) { 2268 /* 2269 * xhci internal endpoint state will go to a "halt" state for 2270 * any stall, including default control pipe protocol stall. 2271 * To clear the host side halt we need to issue a reset endpoint 2272 * command, followed by a set dequeue command to move past the 2273 * TD. 2274 * Class drivers clear the device side halt from a functional 2275 * stall later. Hub TT buffer should only be cleared for FS/LS 2276 * devices behind HS hubs for functional stalls. 2277 */ 2278 if (!(ep->ep_index == 0 && trb_comp_code == COMP_STALL_ERROR)) 2279 xhci_clear_hub_tt_buffer(xhci, td, ep); 2280 2281 xhci_handle_halted_endpoint(xhci, ep, td, EP_HARD_RESET); 2282 2283 return; /* xhci_handle_halted_endpoint marked td cancelled */ 2284 } 2285 2286 xhci_dequeue_td(xhci, td, ep_ring, td->status); 2287 } 2288 2289 /* sum trb lengths from the first trb up to stop_trb, _excluding_ stop_trb */ 2290 static u32 sum_trb_lengths(struct xhci_td *td, union xhci_trb *stop_trb) 2291 { 2292 u32 sum; 2293 union xhci_trb *trb = td->start_trb; 2294 struct xhci_segment *seg = td->start_seg; 2295 2296 for (sum = 0; trb != stop_trb; next_trb(&seg, &trb)) { 2297 if (!trb_is_noop(trb) && !trb_is_link(trb)) 2298 sum += TRB_LEN(le32_to_cpu(trb->generic.field[2])); 2299 } 2300 return sum; 2301 } 2302 2303 /* 2304 * Process control tds, update urb status and actual_length. 2305 */ 2306 static void process_ctrl_td(struct xhci_hcd *xhci, struct xhci_virt_ep *ep, 2307 struct xhci_ring *ep_ring, struct xhci_td *td, 2308 union xhci_trb *ep_trb, struct xhci_transfer_event *event) 2309 { 2310 struct xhci_ep_ctx *ep_ctx; 2311 u32 trb_comp_code; 2312 u32 remaining, requested; 2313 u32 trb_type; 2314 2315 trb_type = TRB_FIELD_TO_TYPE(le32_to_cpu(ep_trb->generic.field[3])); 2316 ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep->ep_index); 2317 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len)); 2318 requested = td->urb->transfer_buffer_length; 2319 remaining = EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)); 2320 2321 switch (trb_comp_code) { 2322 case COMP_SUCCESS: 2323 if (trb_type != TRB_STATUS) { 2324 xhci_warn(xhci, "WARN: Success on ctrl %s TRB without IOC set?\n", 2325 (trb_type == TRB_DATA) ? "data" : "setup"); 2326 td->status = -ESHUTDOWN; 2327 break; 2328 } 2329 td->status = 0; 2330 break; 2331 case COMP_SHORT_PACKET: 2332 td->status = 0; 2333 break; 2334 case COMP_STOPPED_SHORT_PACKET: 2335 if (trb_type == TRB_DATA || trb_type == TRB_NORMAL) 2336 td->urb->actual_length = remaining; 2337 else 2338 xhci_warn(xhci, "WARN: Stopped Short Packet on ctrl setup or status TRB\n"); 2339 goto finish_td; 2340 case COMP_STOPPED: 2341 switch (trb_type) { 2342 case TRB_SETUP: 2343 td->urb->actual_length = 0; 2344 goto finish_td; 2345 case TRB_DATA: 2346 case TRB_NORMAL: 2347 td->urb->actual_length = requested - remaining; 2348 goto finish_td; 2349 case TRB_STATUS: 2350 td->urb->actual_length = requested; 2351 goto finish_td; 2352 default: 2353 xhci_warn(xhci, "WARN: unexpected TRB Type %d\n", 2354 trb_type); 2355 goto finish_td; 2356 } 2357 case COMP_STOPPED_LENGTH_INVALID: 2358 goto finish_td; 2359 default: 2360 if (!xhci_halted_host_endpoint(xhci, ep_ctx, trb_comp_code)) 2361 break; 2362 xhci_dbg(xhci, "TRB error %u, halted endpoint index = %u\n", 2363 trb_comp_code, ep->ep_index); 2364 fallthrough; 2365 case COMP_STALL_ERROR: 2366 /* Did we transfer part of the data (middle) phase? */ 2367 if (trb_type == TRB_DATA || trb_type == TRB_NORMAL) 2368 td->urb->actual_length = requested - remaining; 2369 else if (!td->urb_length_set) 2370 td->urb->actual_length = 0; 2371 goto finish_td; 2372 } 2373 2374 /* stopped at setup stage, no data transferred */ 2375 if (trb_type == TRB_SETUP) 2376 goto finish_td; 2377 2378 /* 2379 * if on data stage then update the actual_length of the URB and flag it 2380 * as set, so it won't be overwritten in the event for the last TRB. 2381 */ 2382 if (trb_type == TRB_DATA || 2383 trb_type == TRB_NORMAL) { 2384 td->urb_length_set = true; 2385 td->urb->actual_length = requested - remaining; 2386 xhci_dbg(xhci, "Waiting for status stage event\n"); 2387 return; 2388 } 2389 2390 /* at status stage */ 2391 if (!td->urb_length_set) 2392 td->urb->actual_length = requested; 2393 2394 finish_td: 2395 finish_td(xhci, ep, ep_ring, td, trb_comp_code); 2396 } 2397 2398 /* 2399 * Process isochronous tds, update urb packet status and actual_length. 2400 */ 2401 static void process_isoc_td(struct xhci_hcd *xhci, struct xhci_virt_ep *ep, 2402 struct xhci_ring *ep_ring, struct xhci_td *td, 2403 union xhci_trb *ep_trb, struct xhci_transfer_event *event) 2404 { 2405 struct urb_priv *urb_priv; 2406 int idx; 2407 struct usb_iso_packet_descriptor *frame; 2408 u32 trb_comp_code; 2409 bool sum_trbs_for_length = false; 2410 u32 remaining, requested, ep_trb_len; 2411 2412 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len)); 2413 urb_priv = td->urb->hcpriv; 2414 idx = urb_priv->num_tds_done; 2415 frame = &td->urb->iso_frame_desc[idx]; 2416 requested = frame->length; 2417 remaining = EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)); 2418 ep_trb_len = TRB_LEN(le32_to_cpu(ep_trb->generic.field[2])); 2419 2420 /* handle completion code */ 2421 switch (trb_comp_code) { 2422 case COMP_SUCCESS: 2423 /* Don't overwrite status if TD had an error, see xHCI 4.9.1 */ 2424 if (td->error_mid_td) 2425 break; 2426 if (remaining) 2427 sum_trbs_for_length = true; 2428 frame->status = 0; 2429 break; 2430 case COMP_SHORT_PACKET: 2431 frame->status = 0; 2432 sum_trbs_for_length = true; 2433 break; 2434 case COMP_BANDWIDTH_OVERRUN_ERROR: 2435 frame->status = -ECOMM; 2436 break; 2437 case COMP_BABBLE_DETECTED_ERROR: 2438 sum_trbs_for_length = true; 2439 fallthrough; 2440 case COMP_ISOCH_BUFFER_OVERRUN: 2441 frame->status = -EOVERFLOW; 2442 if (ep_trb != td->end_trb) 2443 td->error_mid_td = true; 2444 break; 2445 case COMP_MISSED_SERVICE_ERROR: 2446 frame->status = -EXDEV; 2447 sum_trbs_for_length = true; 2448 if (ep_trb != td->end_trb) 2449 td->error_mid_td = true; 2450 break; 2451 case COMP_INCOMPATIBLE_DEVICE_ERROR: 2452 case COMP_STALL_ERROR: 2453 frame->status = -EPROTO; 2454 break; 2455 case COMP_USB_TRANSACTION_ERROR: 2456 frame->status = -EPROTO; 2457 sum_trbs_for_length = true; 2458 if (ep_trb != td->end_trb) 2459 td->error_mid_td = true; 2460 break; 2461 case COMP_STOPPED: 2462 sum_trbs_for_length = true; 2463 break; 2464 case COMP_STOPPED_SHORT_PACKET: 2465 /* field normally containing residue now contains transferred */ 2466 frame->status = 0; 2467 requested = remaining; 2468 break; 2469 case COMP_STOPPED_LENGTH_INVALID: 2470 /* exclude stopped trb with invalid length from length sum */ 2471 sum_trbs_for_length = true; 2472 ep_trb_len = 0; 2473 remaining = 0; 2474 break; 2475 default: 2476 sum_trbs_for_length = true; 2477 frame->status = -1; 2478 break; 2479 } 2480 2481 if (td->urb_length_set) 2482 goto finish_td; 2483 2484 if (sum_trbs_for_length) 2485 frame->actual_length = sum_trb_lengths(td, ep_trb) + 2486 ep_trb_len - remaining; 2487 else 2488 frame->actual_length = requested; 2489 2490 td->urb->actual_length += frame->actual_length; 2491 2492 finish_td: 2493 /* Don't give back TD yet if we encountered an error mid TD */ 2494 if (td->error_mid_td && ep_trb != td->end_trb) { 2495 xhci_dbg(xhci, "Error mid isoc TD, wait for final completion event\n"); 2496 td->urb_length_set = true; 2497 return; 2498 } 2499 finish_td(xhci, ep, ep_ring, td, trb_comp_code); 2500 } 2501 2502 /* 2503 * Process bulk and interrupt tds, update urb status and actual_length. 2504 */ 2505 static void process_bulk_intr_td(struct xhci_hcd *xhci, struct xhci_virt_ep *ep, 2506 struct xhci_ring *ep_ring, struct xhci_td *td, 2507 union xhci_trb *ep_trb, struct xhci_transfer_event *event) 2508 { 2509 struct xhci_slot_ctx *slot_ctx; 2510 u32 trb_comp_code; 2511 u32 remaining, requested, ep_trb_len; 2512 2513 slot_ctx = xhci_get_slot_ctx(xhci, ep->vdev->out_ctx); 2514 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len)); 2515 remaining = EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)); 2516 ep_trb_len = TRB_LEN(le32_to_cpu(ep_trb->generic.field[2])); 2517 requested = td->urb->transfer_buffer_length; 2518 2519 switch (trb_comp_code) { 2520 case COMP_SUCCESS: 2521 ep->err_count = 0; 2522 /* handle success with untransferred data as short packet */ 2523 if (ep_trb != td->end_trb || remaining) { 2524 xhci_warn(xhci, "WARN Successful completion on short TX\n"); 2525 xhci_dbg(xhci, "ep %#x - asked for %d bytes, %d bytes untransferred\n", 2526 td->urb->ep->desc.bEndpointAddress, 2527 requested, remaining); 2528 } 2529 td->status = 0; 2530 break; 2531 case COMP_SHORT_PACKET: 2532 ep->err_count = 0; 2533 td->status = 0; 2534 break; 2535 case COMP_STOPPED_SHORT_PACKET: 2536 td->urb->actual_length = remaining; 2537 goto finish_td; 2538 case COMP_STOPPED_LENGTH_INVALID: 2539 /* stopped on ep trb with invalid length, exclude it */ 2540 td->urb->actual_length = sum_trb_lengths(td, ep_trb); 2541 goto finish_td; 2542 case COMP_USB_TRANSACTION_ERROR: 2543 if (xhci->quirks & XHCI_NO_SOFT_RETRY || 2544 (ep->err_count++ > MAX_SOFT_RETRY) || 2545 le32_to_cpu(slot_ctx->tt_info) & TT_SLOT) 2546 break; 2547 2548 td->status = 0; 2549 2550 xhci_handle_halted_endpoint(xhci, ep, td, EP_SOFT_RESET); 2551 return; 2552 default: 2553 /* do nothing */ 2554 break; 2555 } 2556 2557 if (ep_trb == td->end_trb) 2558 td->urb->actual_length = requested - remaining; 2559 else 2560 td->urb->actual_length = 2561 sum_trb_lengths(td, ep_trb) + 2562 ep_trb_len - remaining; 2563 finish_td: 2564 if (remaining > requested) { 2565 xhci_warn(xhci, "bad transfer trb length %d in event trb\n", 2566 remaining); 2567 td->urb->actual_length = 0; 2568 } 2569 2570 finish_td(xhci, ep, ep_ring, td, trb_comp_code); 2571 } 2572 2573 /* Transfer events which don't point to a transfer TRB, see xhci 4.17.4 */ 2574 static int handle_transferless_tx_event(struct xhci_hcd *xhci, struct xhci_virt_ep *ep, 2575 u32 trb_comp_code) 2576 { 2577 switch (trb_comp_code) { 2578 case COMP_STALL_ERROR: 2579 case COMP_USB_TRANSACTION_ERROR: 2580 case COMP_INVALID_STREAM_TYPE_ERROR: 2581 case COMP_INVALID_STREAM_ID_ERROR: 2582 xhci_dbg(xhci, "Stream transaction error ep %u no id\n", ep->ep_index); 2583 if (ep->err_count++ > MAX_SOFT_RETRY) 2584 xhci_handle_halted_endpoint(xhci, ep, NULL, EP_HARD_RESET); 2585 else 2586 xhci_handle_halted_endpoint(xhci, ep, NULL, EP_SOFT_RESET); 2587 break; 2588 case COMP_RING_UNDERRUN: 2589 case COMP_RING_OVERRUN: 2590 case COMP_STOPPED_LENGTH_INVALID: 2591 break; 2592 default: 2593 xhci_err(xhci, "Transfer event %u for unknown stream ring slot %u ep %u\n", 2594 trb_comp_code, ep->vdev->slot_id, ep->ep_index); 2595 return -ENODEV; 2596 } 2597 return 0; 2598 } 2599 2600 static bool xhci_spurious_success_tx_event(struct xhci_hcd *xhci, 2601 struct xhci_ring *ring) 2602 { 2603 switch (ring->old_trb_comp_code) { 2604 case COMP_SHORT_PACKET: 2605 return xhci->quirks & XHCI_SPURIOUS_SUCCESS; 2606 case COMP_USB_TRANSACTION_ERROR: 2607 case COMP_BABBLE_DETECTED_ERROR: 2608 case COMP_ISOCH_BUFFER_OVERRUN: 2609 return xhci->quirks & XHCI_ETRON_HOST && 2610 ring->type == TYPE_ISOC; 2611 default: 2612 return false; 2613 } 2614 } 2615 2616 static struct xhci_td *find_td_by_dma(struct xhci_ring *ep_ring, dma_addr_t dma) 2617 { 2618 struct xhci_td *td; 2619 2620 if (dma) 2621 list_for_each_entry(td, &ep_ring->td_list, td_list) 2622 if (trb_in_td(td, dma)) 2623 return td; 2624 return NULL; 2625 } 2626 2627 /* 2628 * If this function returns an error condition, it means it got a Transfer 2629 * event with a corrupted Slot ID, Endpoint ID, or TRB DMA address. 2630 * At this point, the host controller is probably hosed and should be reset. 2631 */ 2632 static int handle_tx_event(struct xhci_hcd *xhci, 2633 struct xhci_interrupter *ir, 2634 struct xhci_transfer_event *event) 2635 { 2636 struct xhci_virt_ep *ep; 2637 struct xhci_ring *ep_ring; 2638 unsigned int slot_id; 2639 int ep_index; 2640 struct xhci_td *td = NULL; 2641 dma_addr_t ep_trb_dma; 2642 union xhci_trb *ep_trb; 2643 int status = -EINPROGRESS; 2644 struct xhci_ep_ctx *ep_ctx; 2645 u32 trb_comp_code; 2646 bool ring_xrun_event = false; 2647 2648 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags)); 2649 ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1; 2650 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len)); 2651 ep_trb_dma = le64_to_cpu(event->buffer); 2652 2653 ep = xhci_get_virt_ep(xhci, slot_id, ep_index); 2654 if (!ep) { 2655 xhci_err(xhci, "ERROR Invalid Transfer event\n"); 2656 goto err_out; 2657 } 2658 2659 ep_ring = xhci_dma_to_transfer_ring(ep, ep_trb_dma); 2660 ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep_index); 2661 2662 if (GET_EP_CTX_STATE(ep_ctx) == EP_STATE_DISABLED) { 2663 xhci_err(xhci, 2664 "ERROR Transfer event for disabled endpoint slot %u ep %u\n", 2665 slot_id, ep_index); 2666 goto err_out; 2667 } 2668 2669 if (!ep_ring) 2670 return handle_transferless_tx_event(xhci, ep, trb_comp_code); 2671 2672 /* find the transfer trb this events points to */ 2673 ep_trb = xhci_dma_to_trb(ep_ring->deq_seg, ep_trb_dma, NULL); 2674 2675 /* Look for common error cases */ 2676 switch (trb_comp_code) { 2677 /* Skip codes that require special handling depending on 2678 * transfer type 2679 */ 2680 case COMP_SUCCESS: 2681 if (EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)) != 0) { 2682 trb_comp_code = COMP_SHORT_PACKET; 2683 xhci_dbg(xhci, "Successful completion on short TX for slot %u ep %u with last td comp code %d\n", 2684 slot_id, ep_index, ep_ring->old_trb_comp_code); 2685 } 2686 break; 2687 case COMP_SHORT_PACKET: 2688 break; 2689 /* Completion codes for endpoint stopped state */ 2690 case COMP_STOPPED: 2691 xhci_dbg(xhci, "Stopped on Transfer TRB for slot %u ep %u\n", 2692 slot_id, ep_index); 2693 break; 2694 case COMP_STOPPED_LENGTH_INVALID: 2695 xhci_dbg(xhci, 2696 "Stopped on No-op or Link TRB for slot %u ep %u\n", 2697 slot_id, ep_index); 2698 break; 2699 case COMP_STOPPED_SHORT_PACKET: 2700 xhci_dbg(xhci, 2701 "Stopped with short packet transfer detected for slot %u ep %u\n", 2702 slot_id, ep_index); 2703 break; 2704 /* Completion codes for endpoint halted state */ 2705 case COMP_STALL_ERROR: 2706 xhci_dbg(xhci, "Stalled endpoint for slot %u ep %u\n", slot_id, 2707 ep_index); 2708 status = -EPIPE; 2709 break; 2710 case COMP_SPLIT_TRANSACTION_ERROR: 2711 xhci_dbg(xhci, "Split transaction error for slot %u ep %u\n", 2712 slot_id, ep_index); 2713 status = -EPROTO; 2714 break; 2715 case COMP_USB_TRANSACTION_ERROR: 2716 xhci_dbg(xhci, "Transfer error for slot %u ep %u on endpoint\n", 2717 slot_id, ep_index); 2718 status = -EPROTO; 2719 break; 2720 case COMP_BABBLE_DETECTED_ERROR: 2721 xhci_dbg(xhci, "Babble error for slot %u ep %u on endpoint\n", 2722 slot_id, ep_index); 2723 status = -EOVERFLOW; 2724 break; 2725 /* Completion codes for endpoint error state */ 2726 case COMP_TRB_ERROR: 2727 xhci_warn(xhci, 2728 "WARN: TRB error for slot %u ep %u on endpoint\n", 2729 slot_id, ep_index); 2730 status = -EILSEQ; 2731 break; 2732 /* completion codes not indicating endpoint state change */ 2733 case COMP_DATA_BUFFER_ERROR: 2734 xhci_warn(xhci, 2735 "WARN: HC couldn't access mem fast enough for slot %u ep %u\n", 2736 slot_id, ep_index); 2737 status = -ENOSR; 2738 break; 2739 case COMP_BANDWIDTH_OVERRUN_ERROR: 2740 xhci_warn(xhci, 2741 "WARN: bandwidth overrun event for slot %u ep %u on endpoint\n", 2742 slot_id, ep_index); 2743 break; 2744 case COMP_ISOCH_BUFFER_OVERRUN: 2745 xhci_warn(xhci, 2746 "WARN: buffer overrun event for slot %u ep %u on endpoint", 2747 slot_id, ep_index); 2748 break; 2749 case COMP_RING_UNDERRUN: 2750 /* 2751 * When the Isoch ring is empty, the xHC will generate 2752 * a Ring Overrun Event for IN Isoch endpoint or Ring 2753 * Underrun Event for OUT Isoch endpoint. 2754 */ 2755 xhci_dbg(xhci, "Underrun event on slot %u ep %u\n", slot_id, ep_index); 2756 ring_xrun_event = true; 2757 break; 2758 case COMP_RING_OVERRUN: 2759 xhci_dbg(xhci, "Overrun event on slot %u ep %u\n", slot_id, ep_index); 2760 ring_xrun_event = true; 2761 break; 2762 case COMP_MISSED_SERVICE_ERROR: 2763 /* 2764 * When encounter missed service error, one or more isoc tds 2765 * may be missed by xHC. 2766 * Set skip flag of the ep_ring; Complete the missed tds as 2767 * short transfer when process the ep_ring next time. 2768 */ 2769 ep->skip = true; 2770 xhci_dbg(xhci, 2771 "Miss service interval error for slot %u ep %u, set skip flag%s\n", 2772 slot_id, ep_index, ep_trb_dma ? ", skip now" : ""); 2773 break; 2774 case COMP_NO_PING_RESPONSE_ERROR: 2775 ep->skip = true; 2776 xhci_dbg(xhci, 2777 "No Ping response error for slot %u ep %u, Skip one Isoc TD\n", 2778 slot_id, ep_index); 2779 return 0; 2780 2781 case COMP_INCOMPATIBLE_DEVICE_ERROR: 2782 /* needs disable slot command to recover */ 2783 xhci_warn(xhci, 2784 "WARN: detect an incompatible device for slot %u ep %u", 2785 slot_id, ep_index); 2786 status = -EPROTO; 2787 break; 2788 default: 2789 if (xhci_is_vendor_info_code(xhci, trb_comp_code)) { 2790 status = 0; 2791 break; 2792 } 2793 xhci_warn(xhci, 2794 "ERROR Unknown event condition %u for slot %u ep %u , HC probably busted\n", 2795 trb_comp_code, slot_id, ep_index); 2796 if (ep->skip) 2797 break; 2798 return 0; 2799 } 2800 2801 /* 2802 * xhci 4.10.2 states isoc endpoints should continue 2803 * processing the next TD if there was an error mid TD. 2804 * So host like NEC don't generate an event for the last 2805 * isoc TRB even if the IOC flag is set. 2806 * xhci 4.9.1 states that if there are errors in mult-TRB 2807 * TDs xHC should generate an error for that TRB, and if xHC 2808 * proceeds to the next TD it should genete an event for 2809 * any TRB with IOC flag on the way. Other host follow this. 2810 * 2811 * We wait for the final IOC event, but if we get an event 2812 * anywhere outside this TD, just give it back already. 2813 */ 2814 td = list_first_entry_or_null(&ep_ring->td_list, struct xhci_td, td_list); 2815 2816 if (td && td->error_mid_td && !trb_in_td(td, ep_trb_dma)) { 2817 xhci_dbg(xhci, "Missing TD completion event after mid TD error\n"); 2818 xhci_dequeue_td(xhci, td, ep_ring, td->status); 2819 } 2820 2821 /* 2822 * We don't know how many TDs were missed when ep_trb_dma is zero (as permitted by 2823 * xHCI 1.0) or bogus. Bail out leaving ep->skip set, next event will sort it out. 2824 */ 2825 if (trb_comp_code == COMP_MISSED_SERVICE_ERROR && !find_td_by_dma(ep_ring, ep_trb_dma)) 2826 return 0; 2827 2828 if (list_empty(&ep_ring->td_list)) { 2829 /* 2830 * Don't print wanings if ring is empty due to a stopped endpoint generating an 2831 * extra completion event if the device was suspended. Or, a event for the last TRB 2832 * of a short TD we already got a short event for. The short TD is already removed 2833 * from the TD list. 2834 */ 2835 if (trb_comp_code != COMP_STOPPED && 2836 trb_comp_code != COMP_STOPPED_LENGTH_INVALID && 2837 !ring_xrun_event && 2838 !xhci_spurious_success_tx_event(xhci, ep_ring)) { 2839 xhci_warn(xhci, "Event TRB for slot %u ep %u with no TDs queued\n", 2840 slot_id, ep_index); 2841 } 2842 2843 ep->skip = false; 2844 goto check_endpoint_halted; 2845 } 2846 2847 do { 2848 td = list_first_entry(&ep_ring->td_list, struct xhci_td, 2849 td_list); 2850 2851 /* Is this TRB not part of the currently executing TD? */ 2852 if (!trb_in_td(td, ep_trb_dma)) { 2853 2854 if (ep->skip && usb_endpoint_xfer_isoc(&td->urb->ep->desc)) { 2855 /* this event is unlikely to match any TD, don't skip them all */ 2856 if (trb_comp_code == COMP_STOPPED_LENGTH_INVALID) 2857 return 0; 2858 2859 /* 2860 * TD was missed, skip it. Core already initialized frame->status 2861 * to -EXDEV and frame->actual_length to 0, nothing more to do. 2862 */ 2863 xhci_dequeue_td(xhci, td, ep_ring, 0); 2864 2865 if (!list_empty(&ep_ring->td_list)) { 2866 if (ring_xrun_event) { 2867 /* 2868 * If we are here, we are on xHCI 1.0 host with no 2869 * idea how many TDs were missed or where the xrun 2870 * occurred. New TDs may have been added after the 2871 * xrun, so skip only one TD to be safe. 2872 */ 2873 xhci_dbg(xhci, "Skipped one TD for slot %u ep %u", 2874 slot_id, ep_index); 2875 return 0; 2876 } 2877 continue; 2878 } 2879 2880 xhci_dbg(xhci, "All TDs skipped for slot %u ep %u. Clear skip flag.\n", 2881 slot_id, ep_index); 2882 ep->skip = false; 2883 td = NULL; 2884 goto check_endpoint_halted; 2885 } 2886 2887 /* TD was queued after xrun, maybe xrun was on a link, don't panic yet */ 2888 if (ring_xrun_event) 2889 return 0; 2890 2891 /* 2892 * Skip the Force Stopped Event. The 'ep_trb' of FSE is not in the current 2893 * TD pointed by 'ep_ring->dequeue' because that the hardware dequeue 2894 * pointer still at the previous TRB of the current TD. The previous TRB 2895 * maybe a Link TD or the last TRB of the previous TD. The command 2896 * completion handle will take care the rest. 2897 */ 2898 if (trb_comp_code == COMP_STOPPED || 2899 trb_comp_code == COMP_STOPPED_LENGTH_INVALID) { 2900 return 0; 2901 } 2902 2903 /* 2904 * Some hosts give a spurious success event after a short 2905 * transfer or error on last TRB. Ignore it. 2906 */ 2907 if (xhci_spurious_success_tx_event(xhci, ep_ring)) { 2908 xhci_dbg(xhci, "Spurious event dma %pad, comp_code %u after %u\n", 2909 &ep_trb_dma, trb_comp_code, ep_ring->old_trb_comp_code); 2910 ep_ring->old_trb_comp_code = 0; 2911 return 0; 2912 } 2913 2914 /* HC is busted, give up! */ 2915 goto debug_finding_td; 2916 } 2917 2918 if (ep->skip) { 2919 xhci_dbg(xhci, 2920 "Found td. Clear skip flag for slot %u ep %u.\n", 2921 slot_id, ep_index); 2922 ep->skip = false; 2923 } 2924 2925 /* 2926 * If ep->skip is set, it means there are missed tds on the 2927 * endpoint ring need to take care of. 2928 * Process them as short transfer until reach the td pointed by 2929 * the event. 2930 */ 2931 } while (ep->skip); 2932 2933 ep_ring->old_trb_comp_code = trb_comp_code; 2934 2935 /* Get out if a TD was queued at enqueue after the xrun occurred */ 2936 if (ring_xrun_event) 2937 return 0; 2938 2939 trace_xhci_handle_transfer(ep_ring, (struct xhci_generic_trb *) ep_trb, ep_trb_dma); 2940 2941 /* 2942 * No-op TRB could trigger interrupts in a case where a URB was killed 2943 * and a STALL_ERROR happens right after the endpoint ring stopped. 2944 * Reset the halted endpoint. Otherwise, the endpoint remains stalled 2945 * indefinitely. 2946 */ 2947 2948 if (trb_is_noop(ep_trb)) 2949 goto check_endpoint_halted; 2950 2951 td->status = status; 2952 2953 /* update the urb's actual_length and give back to the core */ 2954 if (usb_endpoint_xfer_control(&td->urb->ep->desc)) 2955 process_ctrl_td(xhci, ep, ep_ring, td, ep_trb, event); 2956 else if (usb_endpoint_xfer_isoc(&td->urb->ep->desc)) 2957 process_isoc_td(xhci, ep, ep_ring, td, ep_trb, event); 2958 else 2959 process_bulk_intr_td(xhci, ep, ep_ring, td, ep_trb, event); 2960 return 0; 2961 2962 check_endpoint_halted: 2963 if (xhci_halted_host_endpoint(xhci, ep_ctx, trb_comp_code)) 2964 xhci_handle_halted_endpoint(xhci, ep, td, EP_HARD_RESET); 2965 2966 return 0; 2967 2968 debug_finding_td: 2969 xhci_err(xhci, "Event dma %pad for ep %d status %d not part of TD at %016llx - %016llx\n", 2970 &ep_trb_dma, ep_index, trb_comp_code, 2971 (unsigned long long)xhci_trb_virt_to_dma(td->start_seg, td->start_trb), 2972 (unsigned long long)xhci_trb_virt_to_dma(td->end_seg, td->end_trb)); 2973 2974 return -ESHUTDOWN; 2975 2976 err_out: 2977 xhci_err(xhci, "@%016llx %08x %08x %08x %08x\n", 2978 (unsigned long long) xhci_trb_virt_to_dma( 2979 ir->event_ring->deq_seg, 2980 ir->event_ring->dequeue), 2981 lower_32_bits(le64_to_cpu(event->buffer)), 2982 upper_32_bits(le64_to_cpu(event->buffer)), 2983 le32_to_cpu(event->transfer_len), 2984 le32_to_cpu(event->flags)); 2985 return -ENODEV; 2986 } 2987 2988 /* 2989 * This function handles one OS-owned event on the event ring. It may drop 2990 * xhci->lock between event processing (e.g. to pass up port status changes). 2991 */ 2992 static int xhci_handle_event_trb(struct xhci_hcd *xhci, struct xhci_interrupter *ir, 2993 union xhci_trb *event) 2994 { 2995 u32 trb_type; 2996 2997 trace_xhci_handle_event(ir->event_ring, &event->generic, 2998 xhci_trb_virt_to_dma(ir->event_ring->deq_seg, 2999 ir->event_ring->dequeue)); 3000 3001 /* 3002 * Barrier between reading the TRB_CYCLE (valid) flag before, and any 3003 * speculative reads of the event's flags/data below. 3004 */ 3005 rmb(); 3006 trb_type = TRB_FIELD_TO_TYPE(le32_to_cpu(event->event_cmd.flags)); 3007 /* FIXME: Handle more event types. */ 3008 3009 switch (trb_type) { 3010 case TRB_COMPLETION: 3011 handle_cmd_completion(xhci, &event->event_cmd); 3012 break; 3013 case TRB_PORT_STATUS: 3014 handle_port_status(xhci, event); 3015 break; 3016 case TRB_TRANSFER: 3017 handle_tx_event(xhci, ir, &event->trans_event); 3018 break; 3019 case TRB_DEV_NOTE: 3020 handle_device_notification(xhci, event); 3021 break; 3022 default: 3023 if (trb_type >= TRB_VENDOR_DEFINED_LOW) 3024 handle_vendor_event(xhci, event, trb_type); 3025 else 3026 xhci_warn(xhci, "ERROR unknown event type %d\n", trb_type); 3027 } 3028 /* Any of the above functions may drop and re-acquire the lock, so check 3029 * to make sure a watchdog timer didn't mark the host as non-responsive. 3030 */ 3031 if (xhci->xhc_state & XHCI_STATE_DYING) { 3032 xhci_dbg(xhci, "xHCI host dying, returning from event handler.\n"); 3033 return -ENODEV; 3034 } 3035 3036 return 0; 3037 } 3038 3039 /* 3040 * Update Event Ring Dequeue Pointer: 3041 * - When all events have finished 3042 * - To avoid "Event Ring Full Error" condition 3043 */ 3044 void xhci_update_erst_dequeue(struct xhci_hcd *xhci, 3045 struct xhci_interrupter *ir, 3046 bool clear_ehb) 3047 { 3048 u64 temp_64; 3049 dma_addr_t deq; 3050 3051 temp_64 = xhci_read_64(xhci, &ir->ir_set->erst_dequeue); 3052 deq = xhci_trb_virt_to_dma(ir->event_ring->deq_seg, 3053 ir->event_ring->dequeue); 3054 if (deq == 0) 3055 xhci_warn(xhci, "WARN something wrong with SW event ring dequeue ptr\n"); 3056 /* 3057 * Per 4.9.4, Software writes to the ERDP register shall always advance 3058 * the Event Ring Dequeue Pointer value. 3059 */ 3060 if ((temp_64 & ERST_PTR_MASK) == (deq & ERST_PTR_MASK) && !clear_ehb) 3061 return; 3062 3063 /* Update HC event ring dequeue pointer */ 3064 temp_64 = ir->event_ring->deq_seg->num & ERST_DESI_MASK; 3065 temp_64 |= deq & ERST_PTR_MASK; 3066 3067 /* Clear the event handler busy flag (RW1C) */ 3068 if (clear_ehb) 3069 temp_64 |= ERST_EHB; 3070 xhci_write_64(xhci, temp_64, &ir->ir_set->erst_dequeue); 3071 } 3072 3073 /* Clear the interrupt pending bit for a specific interrupter. */ 3074 static void xhci_clear_interrupt_pending(struct xhci_interrupter *ir) 3075 { 3076 if (!ir->ip_autoclear) { 3077 u32 iman; 3078 3079 iman = readl(&ir->ir_set->iman); 3080 iman |= IMAN_IP; 3081 writel(iman, &ir->ir_set->iman); 3082 3083 /* Read operation to guarantee the write has been flushed from posted buffers */ 3084 readl(&ir->ir_set->iman); 3085 } 3086 } 3087 3088 /* 3089 * Handle all OS-owned events on an interrupter event ring. It may drop 3090 * and reaquire xhci->lock between event processing. 3091 */ 3092 static int xhci_handle_events(struct xhci_hcd *xhci, struct xhci_interrupter *ir, 3093 bool skip_events) 3094 { 3095 int event_loop = 0; 3096 int err = 0; 3097 u64 temp; 3098 3099 xhci_clear_interrupt_pending(ir); 3100 3101 /* Event ring hasn't been allocated yet. */ 3102 if (!ir->event_ring || !ir->event_ring->dequeue) { 3103 xhci_err(xhci, "ERROR interrupter event ring not ready\n"); 3104 return -ENOMEM; 3105 } 3106 3107 if (xhci->xhc_state & XHCI_STATE_DYING || 3108 xhci->xhc_state & XHCI_STATE_HALTED) { 3109 xhci_dbg(xhci, "xHCI dying, ignoring interrupt. Shouldn't IRQs be disabled?\n"); 3110 3111 /* Clear the event handler busy flag (RW1C) */ 3112 temp = xhci_read_64(xhci, &ir->ir_set->erst_dequeue); 3113 xhci_write_64(xhci, temp | ERST_EHB, &ir->ir_set->erst_dequeue); 3114 return -ENODEV; 3115 } 3116 3117 /* Process all OS owned event TRBs on this event ring */ 3118 while (unhandled_event_trb(ir->event_ring)) { 3119 if (!skip_events) 3120 err = xhci_handle_event_trb(xhci, ir, ir->event_ring->dequeue); 3121 3122 /* 3123 * If half a segment of events have been handled in one go then 3124 * update ERDP, and force isoc trbs to interrupt more often 3125 */ 3126 if (event_loop++ > TRBS_PER_SEGMENT / 2) { 3127 xhci_update_erst_dequeue(xhci, ir, false); 3128 3129 if (ir->isoc_bei_interval > AVOID_BEI_INTERVAL_MIN) 3130 ir->isoc_bei_interval = ir->isoc_bei_interval / 2; 3131 3132 event_loop = 0; 3133 } 3134 3135 /* Update SW event ring dequeue pointer */ 3136 inc_deq(xhci, ir->event_ring); 3137 3138 if (err) 3139 break; 3140 } 3141 3142 xhci_update_erst_dequeue(xhci, ir, true); 3143 3144 return 0; 3145 } 3146 3147 /* 3148 * Move the event ring dequeue pointer to skip events kept in the secondary 3149 * event ring. This is used to ensure that pending events in the ring are 3150 * acknowledged, so the xHCI HCD can properly enter suspend/resume. The 3151 * secondary ring is typically maintained by an external component. 3152 */ 3153 void xhci_skip_sec_intr_events(struct xhci_hcd *xhci, 3154 struct xhci_ring *ring, struct xhci_interrupter *ir) 3155 { 3156 union xhci_trb *current_trb; 3157 u64 erdp_reg; 3158 dma_addr_t deq; 3159 3160 /* disable irq, ack pending interrupt and ack all pending events */ 3161 xhci_disable_interrupter(xhci, ir); 3162 3163 /* last acked event trb is in erdp reg */ 3164 erdp_reg = xhci_read_64(xhci, &ir->ir_set->erst_dequeue); 3165 deq = (dma_addr_t)(erdp_reg & ERST_PTR_MASK); 3166 if (!deq) { 3167 xhci_err(xhci, "event ring handling not required\n"); 3168 return; 3169 } 3170 3171 current_trb = ir->event_ring->dequeue; 3172 /* read cycle state of the last acked trb to find out CCS */ 3173 ring->cycle_state = le32_to_cpu(current_trb->event_cmd.flags) & TRB_CYCLE; 3174 3175 xhci_handle_events(xhci, ir, true); 3176 } 3177 3178 /* 3179 * xHCI spec says we can get an interrupt, and if the HC has an error condition, 3180 * we might get bad data out of the event ring. Section 4.10.2.7 has a list of 3181 * indicators of an event TRB error, but we check the status *first* to be safe. 3182 */ 3183 irqreturn_t xhci_irq(struct usb_hcd *hcd) 3184 { 3185 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 3186 irqreturn_t ret = IRQ_HANDLED; 3187 u32 status; 3188 3189 spin_lock(&xhci->lock); 3190 /* Check if the xHC generated the interrupt, or the irq is shared */ 3191 status = readl(&xhci->op_regs->status); 3192 if (status == ~(u32)0) { 3193 xhci_hc_died(xhci); 3194 goto out; 3195 } 3196 3197 if (!(status & STS_EINT)) { 3198 ret = IRQ_NONE; 3199 goto out; 3200 } 3201 3202 if (status & STS_HCE) { 3203 xhci_warn(xhci, "WARNING: Host Controller Error\n"); 3204 xhci_halt(xhci); 3205 goto out; 3206 } 3207 3208 if (status & STS_FATAL) { 3209 xhci_warn(xhci, "WARNING: Host System Error\n"); 3210 xhci_halt(xhci); 3211 goto out; 3212 } 3213 3214 /* 3215 * Clear the op reg interrupt status first, 3216 * so we can receive interrupts from other MSI-X interrupters. 3217 * USBSTS bits are write 1 to clear. 3218 */ 3219 writel(STS_EINT, &xhci->op_regs->status); 3220 3221 /* This is the handler of the primary interrupter */ 3222 xhci_handle_events(xhci, xhci->interrupters[0], false); 3223 out: 3224 spin_unlock(&xhci->lock); 3225 3226 return ret; 3227 } 3228 3229 irqreturn_t xhci_msi_irq(int irq, void *hcd) 3230 { 3231 return xhci_irq(hcd); 3232 } 3233 EXPORT_SYMBOL_GPL(xhci_msi_irq); 3234 3235 /**** Endpoint Ring Operations ****/ 3236 3237 /* 3238 * Generic function for queueing a TRB on a ring. 3239 * The caller must have checked to make sure there's room on the ring. 3240 * 3241 * @more_trbs_coming: Will you enqueue more TRBs before calling 3242 * prepare_transfer()? 3243 */ 3244 static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring, 3245 bool more_trbs_coming, 3246 u32 field1, u32 field2, u32 field3, u32 field4) 3247 { 3248 struct xhci_generic_trb *trb; 3249 3250 trb = &ring->enqueue->generic; 3251 trb->field[0] = cpu_to_le32(field1); 3252 trb->field[1] = cpu_to_le32(field2); 3253 trb->field[2] = cpu_to_le32(field3); 3254 /* make sure TRB is fully written before giving it to the controller */ 3255 wmb(); 3256 trb->field[3] = cpu_to_le32(field4); 3257 3258 trace_xhci_queue_trb(ring, trb, 3259 xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue)); 3260 3261 inc_enq(xhci, ring, more_trbs_coming); 3262 } 3263 3264 /* 3265 * Does various checks on the endpoint ring, and makes it ready to queue num_trbs. 3266 * expand ring if it start to be full. 3267 */ 3268 static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring, 3269 u32 ep_state, unsigned int num_trbs, gfp_t mem_flags) 3270 { 3271 unsigned int new_segs = 0; 3272 3273 /* Make sure the endpoint has been added to xHC schedule */ 3274 switch (ep_state) { 3275 case EP_STATE_DISABLED: 3276 /* 3277 * USB core changed config/interfaces without notifying us, 3278 * or hardware is reporting the wrong state. 3279 */ 3280 xhci_warn(xhci, "WARN urb submitted to disabled ep\n"); 3281 return -ENOENT; 3282 case EP_STATE_ERROR: 3283 xhci_warn(xhci, "WARN waiting for error on ep to be cleared\n"); 3284 /* FIXME event handling code for error needs to clear it */ 3285 /* XXX not sure if this should be -ENOENT or not */ 3286 return -EINVAL; 3287 case EP_STATE_HALTED: 3288 xhci_dbg(xhci, "WARN halted endpoint, queueing URB anyway.\n"); 3289 break; 3290 case EP_STATE_STOPPED: 3291 case EP_STATE_RUNNING: 3292 break; 3293 default: 3294 xhci_err(xhci, "ERROR unknown endpoint state for ep\n"); 3295 /* 3296 * FIXME issue Configure Endpoint command to try to get the HC 3297 * back into a known state. 3298 */ 3299 return -EINVAL; 3300 } 3301 3302 if (ep_ring != xhci->cmd_ring) { 3303 new_segs = xhci_ring_expansion_needed(xhci, ep_ring, num_trbs); 3304 } else if (xhci_num_trbs_free(ep_ring) <= num_trbs) { 3305 xhci_err(xhci, "Do not support expand command ring\n"); 3306 return -ENOMEM; 3307 } 3308 3309 if (new_segs) { 3310 xhci_dbg_trace(xhci, trace_xhci_dbg_ring_expansion, 3311 "ERROR no room on ep ring, try ring expansion"); 3312 if (xhci_ring_expansion(xhci, ep_ring, new_segs, mem_flags)) { 3313 xhci_err(xhci, "Ring expansion failed\n"); 3314 return -ENOMEM; 3315 } 3316 } 3317 3318 /* Ensure that new TRBs won't overwrite a link */ 3319 if (trb_is_link(ep_ring->enqueue)) 3320 inc_enq_past_link(xhci, ep_ring, 0); 3321 3322 if (last_trb_on_seg(ep_ring->enq_seg, ep_ring->enqueue)) { 3323 xhci_warn(xhci, "Missing link TRB at end of ring segment\n"); 3324 return -EINVAL; 3325 } 3326 3327 return 0; 3328 } 3329 3330 static int prepare_transfer(struct xhci_hcd *xhci, 3331 struct xhci_virt_device *xdev, 3332 unsigned int ep_index, 3333 unsigned int stream_id, 3334 unsigned int num_trbs, 3335 struct urb *urb, 3336 unsigned int td_index, 3337 gfp_t mem_flags) 3338 { 3339 int ret; 3340 struct urb_priv *urb_priv; 3341 struct xhci_td *td; 3342 struct xhci_ring *ep_ring; 3343 struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index); 3344 3345 ep_ring = xhci_triad_to_transfer_ring(xhci, xdev->slot_id, ep_index, 3346 stream_id); 3347 if (!ep_ring) { 3348 xhci_dbg(xhci, "Can't prepare ring for bad stream ID %u\n", 3349 stream_id); 3350 return -EINVAL; 3351 } 3352 3353 ret = prepare_ring(xhci, ep_ring, GET_EP_CTX_STATE(ep_ctx), 3354 num_trbs, mem_flags); 3355 if (ret) 3356 return ret; 3357 3358 urb_priv = urb->hcpriv; 3359 td = &urb_priv->td[td_index]; 3360 3361 INIT_LIST_HEAD(&td->td_list); 3362 INIT_LIST_HEAD(&td->cancelled_td_list); 3363 3364 if (td_index == 0) { 3365 ret = usb_hcd_link_urb_to_ep(bus_to_hcd(urb->dev->bus), urb); 3366 if (unlikely(ret)) 3367 return ret; 3368 } 3369 3370 td->urb = urb; 3371 /* Add this TD to the tail of the endpoint ring's TD list */ 3372 list_add_tail(&td->td_list, &ep_ring->td_list); 3373 td->start_seg = ep_ring->enq_seg; 3374 td->start_trb = ep_ring->enqueue; 3375 3376 return 0; 3377 } 3378 3379 unsigned int count_trbs(u64 addr, u64 len) 3380 { 3381 unsigned int num_trbs; 3382 3383 num_trbs = DIV_ROUND_UP(len + (addr & (TRB_MAX_BUFF_SIZE - 1)), 3384 TRB_MAX_BUFF_SIZE); 3385 if (num_trbs == 0) 3386 num_trbs++; 3387 3388 return num_trbs; 3389 } 3390 3391 static inline unsigned int count_trbs_needed(struct urb *urb) 3392 { 3393 return count_trbs(urb->transfer_dma, urb->transfer_buffer_length); 3394 } 3395 3396 static unsigned int count_sg_trbs_needed(struct urb *urb) 3397 { 3398 struct scatterlist *sg; 3399 unsigned int i, len, full_len, num_trbs = 0; 3400 3401 full_len = urb->transfer_buffer_length; 3402 3403 for_each_sg(urb->sg, sg, urb->num_mapped_sgs, i) { 3404 len = sg_dma_len(sg); 3405 num_trbs += count_trbs(sg_dma_address(sg), len); 3406 len = min_t(unsigned int, len, full_len); 3407 full_len -= len; 3408 if (full_len == 0) 3409 break; 3410 } 3411 3412 return num_trbs; 3413 } 3414 3415 static unsigned int count_isoc_trbs_needed(struct urb *urb, int i) 3416 { 3417 u64 addr, len; 3418 3419 addr = (u64) (urb->transfer_dma + urb->iso_frame_desc[i].offset); 3420 len = urb->iso_frame_desc[i].length; 3421 3422 return count_trbs(addr, len); 3423 } 3424 3425 static void check_trb_math(struct urb *urb, int running_total) 3426 { 3427 if (unlikely(running_total != urb->transfer_buffer_length)) 3428 dev_err(&urb->dev->dev, "%s - ep %#x - Miscalculated tx length, " 3429 "queued %#x (%d), asked for %#x (%d)\n", 3430 __func__, 3431 urb->ep->desc.bEndpointAddress, 3432 running_total, running_total, 3433 urb->transfer_buffer_length, 3434 urb->transfer_buffer_length); 3435 } 3436 3437 static void giveback_first_trb(struct xhci_hcd *xhci, int slot_id, 3438 unsigned int ep_index, unsigned int stream_id, int start_cycle, 3439 struct xhci_generic_trb *start_trb) 3440 { 3441 /* 3442 * Pass all the TRBs to the hardware at once and make sure this write 3443 * isn't reordered. 3444 */ 3445 wmb(); 3446 if (start_cycle) 3447 start_trb->field[3] |= cpu_to_le32(start_cycle); 3448 else 3449 start_trb->field[3] &= cpu_to_le32(~TRB_CYCLE); 3450 xhci_ring_ep_doorbell(xhci, slot_id, ep_index, stream_id); 3451 } 3452 3453 static void check_interval(struct urb *urb, struct xhci_ep_ctx *ep_ctx) 3454 { 3455 int xhci_interval; 3456 int ep_interval; 3457 3458 xhci_interval = EP_INTERVAL_TO_UFRAMES(le32_to_cpu(ep_ctx->ep_info)); 3459 ep_interval = urb->interval; 3460 3461 /* Convert to microframes */ 3462 if (urb->dev->speed == USB_SPEED_LOW || 3463 urb->dev->speed == USB_SPEED_FULL) 3464 ep_interval *= 8; 3465 3466 /* FIXME change this to a warning and a suggestion to use the new API 3467 * to set the polling interval (once the API is added). 3468 */ 3469 if (xhci_interval != ep_interval) { 3470 dev_dbg_ratelimited(&urb->dev->dev, 3471 "Driver uses different interval (%d microframe%s) than xHCI (%d microframe%s)\n", 3472 ep_interval, str_plural(ep_interval), 3473 xhci_interval, str_plural(xhci_interval)); 3474 urb->interval = xhci_interval; 3475 /* Convert back to frames for LS/FS devices */ 3476 if (urb->dev->speed == USB_SPEED_LOW || 3477 urb->dev->speed == USB_SPEED_FULL) 3478 urb->interval /= 8; 3479 } 3480 } 3481 3482 /* 3483 * xHCI uses normal TRBs for both bulk and interrupt. When the interrupt 3484 * endpoint is to be serviced, the xHC will consume (at most) one TD. A TD 3485 * (comprised of sg list entries) can take several service intervals to 3486 * transmit. 3487 */ 3488 int xhci_queue_intr_tx(struct xhci_hcd *xhci, gfp_t mem_flags, 3489 struct urb *urb, int slot_id, unsigned int ep_index) 3490 { 3491 struct xhci_ep_ctx *ep_ctx; 3492 3493 ep_ctx = xhci_get_ep_ctx(xhci, xhci->devs[slot_id]->out_ctx, ep_index); 3494 check_interval(urb, ep_ctx); 3495 3496 return xhci_queue_bulk_tx(xhci, mem_flags, urb, slot_id, ep_index); 3497 } 3498 3499 /* 3500 * For xHCI 1.0 host controllers, TD size is the number of max packet sized 3501 * packets remaining in the TD (*not* including this TRB). 3502 * 3503 * Total TD packet count = total_packet_count = 3504 * DIV_ROUND_UP(TD size in bytes / wMaxPacketSize) 3505 * 3506 * Packets transferred up to and including this TRB = packets_transferred = 3507 * rounddown(total bytes transferred including this TRB / wMaxPacketSize) 3508 * 3509 * TD size = total_packet_count - packets_transferred 3510 * 3511 * For xHCI 0.96 and older, TD size field should be the remaining bytes 3512 * including this TRB, right shifted by 10 3513 * 3514 * For all hosts it must fit in bits 21:17, so it can't be bigger than 31. 3515 * This is taken care of in the TRB_TD_SIZE() macro 3516 * 3517 * The last TRB in a TD must have the TD size set to zero. 3518 */ 3519 static u32 xhci_td_remainder(struct xhci_hcd *xhci, int transferred, 3520 int trb_buff_len, unsigned int td_total_len, 3521 struct urb *urb, bool more_trbs_coming) 3522 { 3523 u32 maxp, total_packet_count; 3524 3525 /* MTK xHCI 0.96 contains some features from 1.0 */ 3526 if (xhci->hci_version < 0x100 && !(xhci->quirks & XHCI_MTK_HOST)) 3527 return ((td_total_len - transferred) >> 10); 3528 3529 /* One TRB with a zero-length data packet. */ 3530 if (!more_trbs_coming || (transferred == 0 && trb_buff_len == 0) || 3531 trb_buff_len == td_total_len) 3532 return 0; 3533 3534 /* for MTK xHCI 0.96, TD size include this TRB, but not in 1.x */ 3535 if ((xhci->quirks & XHCI_MTK_HOST) && (xhci->hci_version < 0x100)) 3536 trb_buff_len = 0; 3537 3538 maxp = xhci_usb_endpoint_maxp(urb->dev, urb->ep); 3539 total_packet_count = DIV_ROUND_UP(td_total_len, maxp); 3540 3541 /* Queueing functions don't count the current TRB into transferred */ 3542 return (total_packet_count - ((transferred + trb_buff_len) / maxp)); 3543 } 3544 3545 3546 static int xhci_align_td(struct xhci_hcd *xhci, struct urb *urb, u32 enqd_len, 3547 u32 *trb_buff_len, struct xhci_segment *seg) 3548 { 3549 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; 3550 unsigned int unalign; 3551 unsigned int max_pkt; 3552 u32 new_buff_len; 3553 size_t len; 3554 3555 max_pkt = xhci_usb_endpoint_maxp(urb->dev, urb->ep); 3556 unalign = (enqd_len + *trb_buff_len) % max_pkt; 3557 3558 /* we got lucky, last normal TRB data on segment is packet aligned */ 3559 if (unalign == 0) 3560 return 0; 3561 3562 xhci_dbg(xhci, "Unaligned %d bytes, buff len %d\n", 3563 unalign, *trb_buff_len); 3564 3565 /* is the last nornal TRB alignable by splitting it */ 3566 if (*trb_buff_len > unalign) { 3567 *trb_buff_len -= unalign; 3568 xhci_dbg(xhci, "split align, new buff len %d\n", *trb_buff_len); 3569 return 0; 3570 } 3571 3572 /* 3573 * We want enqd_len + trb_buff_len to sum up to a number aligned to 3574 * number which is divisible by the endpoint's wMaxPacketSize. IOW: 3575 * (size of currently enqueued TRBs + remainder) % wMaxPacketSize == 0. 3576 */ 3577 new_buff_len = max_pkt - (enqd_len % max_pkt); 3578 3579 if (new_buff_len > (urb->transfer_buffer_length - enqd_len)) 3580 new_buff_len = (urb->transfer_buffer_length - enqd_len); 3581 3582 /* create a max max_pkt sized bounce buffer pointed to by last trb */ 3583 if (usb_urb_dir_out(urb)) { 3584 if (urb->num_sgs) { 3585 len = sg_pcopy_to_buffer(urb->sg, urb->num_sgs, 3586 seg->bounce_buf, new_buff_len, enqd_len); 3587 if (len != new_buff_len) 3588 xhci_warn(xhci, "WARN Wrong bounce buffer write length: %zu != %d\n", 3589 len, new_buff_len); 3590 } else { 3591 memcpy(seg->bounce_buf, urb->transfer_buffer + enqd_len, new_buff_len); 3592 } 3593 3594 seg->bounce_dma = dma_map_single(dev, seg->bounce_buf, 3595 max_pkt, DMA_TO_DEVICE); 3596 } else { 3597 seg->bounce_dma = dma_map_single(dev, seg->bounce_buf, 3598 max_pkt, DMA_FROM_DEVICE); 3599 } 3600 3601 if (dma_mapping_error(dev, seg->bounce_dma)) { 3602 /* try without aligning. Some host controllers survive */ 3603 xhci_warn(xhci, "Failed mapping bounce buffer, not aligning\n"); 3604 return 0; 3605 } 3606 *trb_buff_len = new_buff_len; 3607 seg->bounce_len = new_buff_len; 3608 seg->bounce_offs = enqd_len; 3609 3610 xhci_dbg(xhci, "Bounce align, new buff len %d\n", *trb_buff_len); 3611 3612 return 1; 3613 } 3614 3615 /* This is very similar to what ehci-q.c qtd_fill() does */ 3616 int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags, 3617 struct urb *urb, int slot_id, unsigned int ep_index) 3618 { 3619 struct xhci_ring *ring; 3620 struct urb_priv *urb_priv; 3621 struct xhci_td *td; 3622 struct xhci_generic_trb *start_trb; 3623 struct scatterlist *sg = NULL; 3624 bool more_trbs_coming = true; 3625 bool need_zero_pkt = false; 3626 bool first_trb = true; 3627 unsigned int num_trbs; 3628 unsigned int start_cycle, num_sgs = 0; 3629 unsigned int enqd_len, block_len, trb_buff_len, full_len; 3630 int sent_len, ret; 3631 u32 field, length_field, remainder; 3632 u64 addr, send_addr; 3633 3634 ring = xhci_urb_to_transfer_ring(xhci, urb); 3635 if (!ring) 3636 return -EINVAL; 3637 3638 full_len = urb->transfer_buffer_length; 3639 /* If we have scatter/gather list, we use it. */ 3640 if (urb->num_sgs && !(urb->transfer_flags & URB_DMA_MAP_SINGLE)) { 3641 num_sgs = urb->num_mapped_sgs; 3642 sg = urb->sg; 3643 addr = (u64) sg_dma_address(sg); 3644 block_len = sg_dma_len(sg); 3645 num_trbs = count_sg_trbs_needed(urb); 3646 } else { 3647 num_trbs = count_trbs_needed(urb); 3648 addr = (u64) urb->transfer_dma; 3649 block_len = full_len; 3650 } 3651 ret = prepare_transfer(xhci, xhci->devs[slot_id], 3652 ep_index, urb->stream_id, 3653 num_trbs, urb, 0, mem_flags); 3654 if (unlikely(ret < 0)) 3655 return ret; 3656 3657 urb_priv = urb->hcpriv; 3658 3659 /* Deal with URB_ZERO_PACKET - need one more td/trb */ 3660 if (urb->transfer_flags & URB_ZERO_PACKET && urb_priv->num_tds > 1) 3661 need_zero_pkt = true; 3662 3663 td = &urb_priv->td[0]; 3664 3665 /* 3666 * Don't give the first TRB to the hardware (by toggling the cycle bit) 3667 * until we've finished creating all the other TRBs. The ring's cycle 3668 * state may change as we enqueue the other TRBs, so save it too. 3669 */ 3670 start_trb = &ring->enqueue->generic; 3671 start_cycle = ring->cycle_state; 3672 send_addr = addr; 3673 3674 /* Queue the TRBs, even if they are zero-length */ 3675 for (enqd_len = 0; first_trb || enqd_len < full_len; 3676 enqd_len += trb_buff_len) { 3677 field = TRB_TYPE(TRB_NORMAL); 3678 3679 /* TRB buffer should not cross 64KB boundaries */ 3680 trb_buff_len = TRB_BUFF_LEN_UP_TO_BOUNDARY(addr); 3681 trb_buff_len = min_t(unsigned int, trb_buff_len, block_len); 3682 3683 if (enqd_len + trb_buff_len > full_len) 3684 trb_buff_len = full_len - enqd_len; 3685 3686 /* Don't change the cycle bit of the first TRB until later */ 3687 if (first_trb) { 3688 first_trb = false; 3689 if (start_cycle == 0) 3690 field |= TRB_CYCLE; 3691 } else 3692 field |= ring->cycle_state; 3693 3694 /* Chain all the TRBs together; clear the chain bit in the last 3695 * TRB to indicate it's the last TRB in the chain. 3696 */ 3697 if (enqd_len + trb_buff_len < full_len) { 3698 field |= TRB_CHAIN; 3699 if (trb_is_link(ring->enqueue + 1)) { 3700 if (xhci_align_td(xhci, urb, enqd_len, 3701 &trb_buff_len, 3702 ring->enq_seg)) { 3703 send_addr = ring->enq_seg->bounce_dma; 3704 /* TD bounced at least, and last on this seg */ 3705 td->bounce_seg = ring->enq_seg; 3706 } 3707 } 3708 } 3709 if (enqd_len + trb_buff_len >= full_len) { 3710 field &= ~TRB_CHAIN; 3711 field |= TRB_IOC; 3712 more_trbs_coming = false; 3713 td->end_trb = ring->enqueue; 3714 td->end_seg = ring->enq_seg; 3715 if (xhci_urb_suitable_for_idt(urb)) { 3716 memcpy(&send_addr, urb->transfer_buffer, 3717 trb_buff_len); 3718 le64_to_cpus(&send_addr); 3719 field |= TRB_IDT; 3720 } 3721 } 3722 3723 /* Only set interrupt on short packet for IN endpoints */ 3724 if (usb_urb_dir_in(urb)) 3725 field |= TRB_ISP; 3726 3727 /* Set the TRB length, TD size, and interrupter fields. */ 3728 remainder = xhci_td_remainder(xhci, enqd_len, trb_buff_len, 3729 full_len, urb, more_trbs_coming); 3730 3731 length_field = TRB_LEN(trb_buff_len) | 3732 TRB_TD_SIZE(remainder) | 3733 TRB_INTR_TARGET(0); 3734 3735 queue_trb(xhci, ring, more_trbs_coming | need_zero_pkt, 3736 lower_32_bits(send_addr), 3737 upper_32_bits(send_addr), 3738 length_field, 3739 field); 3740 addr += trb_buff_len; 3741 sent_len = trb_buff_len; 3742 3743 while (sg && sent_len >= block_len) { 3744 /* New sg entry */ 3745 --num_sgs; 3746 sent_len -= block_len; 3747 sg = sg_next(sg); 3748 if (num_sgs != 0 && sg) { 3749 block_len = sg_dma_len(sg); 3750 addr = (u64) sg_dma_address(sg); 3751 addr += sent_len; 3752 } 3753 } 3754 block_len -= sent_len; 3755 send_addr = addr; 3756 } 3757 3758 if (need_zero_pkt) { 3759 ret = prepare_transfer(xhci, xhci->devs[slot_id], 3760 ep_index, urb->stream_id, 3761 1, urb, 1, mem_flags); 3762 urb_priv->td[1].end_trb = ring->enqueue; 3763 urb_priv->td[1].end_seg = ring->enq_seg; 3764 field = TRB_TYPE(TRB_NORMAL) | ring->cycle_state | TRB_IOC; 3765 queue_trb(xhci, ring, 0, 0, 0, TRB_INTR_TARGET(0), field); 3766 } 3767 3768 check_trb_math(urb, enqd_len); 3769 giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id, 3770 start_cycle, start_trb); 3771 return 0; 3772 } 3773 3774 /* Caller must have locked xhci->lock */ 3775 int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags, 3776 struct urb *urb, int slot_id, unsigned int ep_index) 3777 { 3778 struct xhci_ring *ep_ring; 3779 int num_trbs; 3780 int ret; 3781 struct usb_ctrlrequest *setup; 3782 struct xhci_generic_trb *start_trb; 3783 int start_cycle; 3784 u32 field; 3785 struct urb_priv *urb_priv; 3786 struct xhci_td *td; 3787 3788 ep_ring = xhci_urb_to_transfer_ring(xhci, urb); 3789 if (!ep_ring) 3790 return -EINVAL; 3791 3792 /* 3793 * Need to copy setup packet into setup TRB, so we can't use the setup 3794 * DMA address. 3795 */ 3796 if (!urb->setup_packet) 3797 return -EINVAL; 3798 3799 if ((xhci->quirks & XHCI_ETRON_HOST) && 3800 urb->dev->speed >= USB_SPEED_SUPER) { 3801 /* 3802 * If next available TRB is the Link TRB in the ring segment then 3803 * enqueue a No Op TRB, this can prevent the Setup and Data Stage 3804 * TRB to be breaked by the Link TRB. 3805 */ 3806 if (last_trb_on_seg(ep_ring->enq_seg, ep_ring->enqueue + 1)) { 3807 field = TRB_TYPE(TRB_TR_NOOP) | ep_ring->cycle_state; 3808 queue_trb(xhci, ep_ring, false, 0, 0, 3809 TRB_INTR_TARGET(0), field); 3810 } 3811 } 3812 3813 /* 1 TRB for setup, 1 for status */ 3814 num_trbs = 2; 3815 /* 3816 * Don't need to check if we need additional event data and normal TRBs, 3817 * since data in control transfers will never get bigger than 16MB 3818 * XXX: can we get a buffer that crosses 64KB boundaries? 3819 */ 3820 if (urb->transfer_buffer_length > 0) 3821 num_trbs++; 3822 ret = prepare_transfer(xhci, xhci->devs[slot_id], 3823 ep_index, urb->stream_id, 3824 num_trbs, urb, 0, mem_flags); 3825 if (ret < 0) 3826 return ret; 3827 3828 urb_priv = urb->hcpriv; 3829 td = &urb_priv->td[0]; 3830 3831 /* 3832 * Don't give the first TRB to the hardware (by toggling the cycle bit) 3833 * until we've finished creating all the other TRBs. The ring's cycle 3834 * state may change as we enqueue the other TRBs, so save it too. 3835 */ 3836 start_trb = &ep_ring->enqueue->generic; 3837 start_cycle = ep_ring->cycle_state; 3838 3839 /* Queue setup TRB - see section 6.4.1.2.1 */ 3840 /* FIXME better way to translate setup_packet into two u32 fields? */ 3841 setup = (struct usb_ctrlrequest *) urb->setup_packet; 3842 field = 0; 3843 field |= TRB_IDT | TRB_TYPE(TRB_SETUP); 3844 if (start_cycle == 0) 3845 field |= 0x1; 3846 3847 /* xHCI 1.0/1.1 6.4.1.2.1: Transfer Type field */ 3848 if ((xhci->hci_version >= 0x100) || (xhci->quirks & XHCI_MTK_HOST)) { 3849 if (urb->transfer_buffer_length > 0) { 3850 if (setup->bRequestType & USB_DIR_IN) 3851 field |= TRB_TX_TYPE(TRB_DATA_IN); 3852 else 3853 field |= TRB_TX_TYPE(TRB_DATA_OUT); 3854 } 3855 } 3856 3857 queue_trb(xhci, ep_ring, true, 3858 setup->bRequestType | setup->bRequest << 8 | le16_to_cpu(setup->wValue) << 16, 3859 le16_to_cpu(setup->wIndex) | le16_to_cpu(setup->wLength) << 16, 3860 TRB_LEN(8) | TRB_INTR_TARGET(0), 3861 /* Immediate data in pointer */ 3862 field); 3863 3864 /* If there's data, queue data TRBs */ 3865 /* Only set interrupt on short packet for IN endpoints */ 3866 if (usb_urb_dir_in(urb)) 3867 field = TRB_ISP | TRB_TYPE(TRB_DATA); 3868 else 3869 field = TRB_TYPE(TRB_DATA); 3870 3871 if (urb->transfer_buffer_length > 0) { 3872 u32 length_field, remainder; 3873 u64 addr; 3874 3875 if (xhci_urb_suitable_for_idt(urb)) { 3876 memcpy(&addr, urb->transfer_buffer, 3877 urb->transfer_buffer_length); 3878 le64_to_cpus(&addr); 3879 field |= TRB_IDT; 3880 } else { 3881 addr = (u64) urb->transfer_dma; 3882 } 3883 3884 remainder = xhci_td_remainder(xhci, 0, 3885 urb->transfer_buffer_length, 3886 urb->transfer_buffer_length, 3887 urb, 1); 3888 length_field = TRB_LEN(urb->transfer_buffer_length) | 3889 TRB_TD_SIZE(remainder) | 3890 TRB_INTR_TARGET(0); 3891 if (setup->bRequestType & USB_DIR_IN) 3892 field |= TRB_DIR_IN; 3893 queue_trb(xhci, ep_ring, true, 3894 lower_32_bits(addr), 3895 upper_32_bits(addr), 3896 length_field, 3897 field | ep_ring->cycle_state); 3898 } 3899 3900 /* Save the DMA address of the last TRB in the TD */ 3901 td->end_trb = ep_ring->enqueue; 3902 td->end_seg = ep_ring->enq_seg; 3903 3904 /* Queue status TRB - see Table 7 and sections 4.11.2.2 and 6.4.1.2.3 */ 3905 /* If the device sent data, the status stage is an OUT transfer */ 3906 if (urb->transfer_buffer_length > 0 && setup->bRequestType & USB_DIR_IN) 3907 field = 0; 3908 else 3909 field = TRB_DIR_IN; 3910 queue_trb(xhci, ep_ring, false, 3911 0, 3912 0, 3913 TRB_INTR_TARGET(0), 3914 /* Event on completion */ 3915 field | TRB_IOC | TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state); 3916 3917 giveback_first_trb(xhci, slot_id, ep_index, 0, 3918 start_cycle, start_trb); 3919 return 0; 3920 } 3921 3922 /* 3923 * The transfer burst count field of the isochronous TRB defines the number of 3924 * bursts that are required to move all packets in this TD. Only SuperSpeed 3925 * devices can burst up to bMaxBurst number of packets per service interval. 3926 * This field is zero based, meaning a value of zero in the field means one 3927 * burst. Basically, for everything but SuperSpeed devices, this field will be 3928 * zero. Only xHCI 1.0 host controllers support this field. 3929 */ 3930 static unsigned int xhci_get_burst_count(struct xhci_hcd *xhci, 3931 struct urb *urb, unsigned int total_packet_count) 3932 { 3933 unsigned int max_burst; 3934 3935 if (xhci->hci_version < 0x100 || urb->dev->speed < USB_SPEED_SUPER) 3936 return 0; 3937 3938 max_burst = urb->ep->ss_ep_comp.bMaxBurst; 3939 return DIV_ROUND_UP(total_packet_count, max_burst + 1) - 1; 3940 } 3941 3942 /* 3943 * Returns the number of packets in the last "burst" of packets. This field is 3944 * valid for all speeds of devices. USB 2.0 devices can only do one "burst", so 3945 * the last burst packet count is equal to the total number of packets in the 3946 * TD. SuperSpeed endpoints can have up to 3 bursts. All but the last burst 3947 * must contain (bMaxBurst + 1) number of packets, but the last burst can 3948 * contain 1 to (bMaxBurst + 1) packets. 3949 */ 3950 static unsigned int xhci_get_last_burst_packet_count(struct xhci_hcd *xhci, 3951 struct urb *urb, unsigned int total_packet_count) 3952 { 3953 unsigned int max_burst; 3954 unsigned int residue; 3955 3956 if (xhci->hci_version < 0x100) 3957 return 0; 3958 3959 if (urb->dev->speed >= USB_SPEED_SUPER) { 3960 /* bMaxBurst is zero based: 0 means 1 packet per burst */ 3961 max_burst = urb->ep->ss_ep_comp.bMaxBurst; 3962 residue = total_packet_count % (max_burst + 1); 3963 /* If residue is zero, the last burst contains (max_burst + 1) 3964 * number of packets, but the TLBPC field is zero-based. 3965 */ 3966 if (residue == 0) 3967 return max_burst; 3968 return residue - 1; 3969 } 3970 if (total_packet_count == 0) 3971 return 0; 3972 return total_packet_count - 1; 3973 } 3974 3975 /* Returns the Isochronous Scheduling Threshold in Microframes. 1 Frame is 8 Microframes. */ 3976 static int xhci_ist_microframes(struct xhci_hcd *xhci) 3977 { 3978 int ist = FIELD_GET(HCS_IST_VALUE, xhci->hcs_params2); 3979 3980 if (xhci->hcs_params2 & HCS_IST_UNIT) 3981 ist *= 8; 3982 return ist; 3983 } 3984 3985 3986 static bool xhci_isoc_td_uses_frame_id(struct xhci_hcd *xhci, struct urb *urb, 3987 struct xhci_virt_ep *ep, int i) 3988 { 3989 if (urb->transfer_flags & URB_ISO_ASAP) 3990 return false; 3991 3992 if (xhci->hcc_params & HCC_CFC) 3993 return true; 3994 3995 /* set frame id for first TD of first URB in stream */ 3996 if (ep->next_uframe == -1 && i == 0) 3997 return true; 3998 3999 return false; 4000 } 4001 4002 /* 4003 * Check if frame is in the valid frame window, including start and end. 4004 * If start > end then assume window wrapped around at a limit the frame 4005 * value won't exceed. 4006 */ 4007 static bool xhci_frame_in_range(u32 frame, u32 start, u32 end) 4008 { 4009 /* frame window end wrapped around */ 4010 if (start > end) 4011 return frame >= start || frame <= end; 4012 4013 return frame >= start && frame <= end; 4014 } 4015 4016 /* 4017 * Set the urb->start_frame of the URB. 4018 * 4019 * Returns microframe index of first TD 4020 */ 4021 static int xhci_get_isoc_start_frame(struct xhci_hcd *xhci, struct urb *urb, 4022 struct xhci_virt_ep *ep) 4023 { 4024 u32 curr_frame, start_uframe; 4025 u32 urb_start, urb_end; 4026 u32 win_start, win_end; 4027 bool frame_unit; 4028 int uinterval; 4029 u32 mfindex; 4030 int ist; 4031 4032 /* check if urb uses frame units instead of microframes */ 4033 frame_unit = (urb->dev->speed == USB_SPEED_FULL || 4034 urb->dev->speed == USB_SPEED_LOW); 4035 4036 uinterval = urb->interval; 4037 if (frame_unit) 4038 uinterval *= 8; 4039 4040 /* get current microframe index and isoc scheduling threshold */ 4041 mfindex = readl(&xhci->run_regs->microframe_index); 4042 ist = xhci_ist_microframes(xhci); 4043 4044 /* calculate valid frame window, in frame units, see xhci 4.11.2.5 */ 4045 curr_frame = MFINDEX_TO_FRAME(mfindex); 4046 win_start = (curr_frame + DIV_ROUND_UP_POW2(ist, 8) + 1) % MAX_FRAMES; 4047 win_end = (curr_frame + 895) % MAX_FRAMES; 4048 4049 /* Is this the first URB starting the whole isoc data flow? */ 4050 if (ep->next_uframe < 0) { 4051 /* align first URB to next interval boundary, or at last to full frame */ 4052 start_uframe = mfindex + ist + XHCI_CFC_DELAY; 4053 start_uframe = roundup(start_uframe, 8); 4054 start_uframe = roundup(start_uframe, uinterval) % MAX_UFRAMES; 4055 } else { 4056 /* URB is mid stream and expected to handle the next frame */ 4057 start_uframe = ep->next_uframe; 4058 urb_start = start_uframe / 8; 4059 urb_end = (start_uframe + urb->number_of_packets * uinterval) / 8; 4060 urb_end %= MAX_FRAMES; 4061 4062 if (!xhci_frame_in_range(urb_start, win_start, win_end)) 4063 xhci_dbg(xhci, "Ill-timed isoc URB %p for start frame %d, range %d-%d\n", 4064 urb, urb_start, win_start, win_end); 4065 4066 if (!xhci_frame_in_range(urb_end, win_start, win_end)) 4067 xhci_dbg(xhci, "Ill-timed isoc URB %p for end frame %d, range %d-%d\n", 4068 urb, urb_start, win_start, win_end); 4069 } 4070 /* set urb->start_frame */ 4071 urb->start_frame = frame_unit ? start_uframe / 8 : start_uframe; 4072 4073 return start_uframe; 4074 } 4075 4076 /* Check if we should generate event interrupt for a TD in an isoc URB */ 4077 static bool trb_block_event_intr(struct xhci_hcd *xhci, int num_tds, int i, 4078 struct xhci_interrupter *ir) 4079 { 4080 if (xhci->hci_version < 0x100) 4081 return false; 4082 /* always generate an event interrupt for the last TD */ 4083 if (i == num_tds - 1) 4084 return false; 4085 /* 4086 * If AVOID_BEI is set the host handles full event rings poorly, 4087 * generate an event at least every 8th TD to clear the event ring 4088 */ 4089 if (i && ir->isoc_bei_interval && xhci->quirks & XHCI_AVOID_BEI) 4090 return !!(i % ir->isoc_bei_interval); 4091 4092 return true; 4093 } 4094 4095 /* This is for isoc transfer */ 4096 static int xhci_queue_isoc_tx(struct xhci_hcd *xhci, gfp_t mem_flags, 4097 struct urb *urb, int slot_id, unsigned int ep_index) 4098 { 4099 struct xhci_interrupter *ir; 4100 struct xhci_ring *ep_ring; 4101 struct urb_priv *urb_priv; 4102 struct xhci_td *td; 4103 int num_tds, trbs_per_td; 4104 struct xhci_generic_trb *start_trb; 4105 bool first_trb; 4106 int start_cycle; 4107 u32 field, length_field; 4108 int running_total, trb_buff_len, td_len, td_remain_len, ret; 4109 u64 start_addr, addr; 4110 int i, j; 4111 bool more_trbs_coming; 4112 struct xhci_virt_ep *xep; 4113 int uinterval = urb->interval; 4114 int start_uframe; 4115 4116 xep = &xhci->devs[slot_id]->eps[ep_index]; 4117 ep_ring = xhci->devs[slot_id]->eps[ep_index].ring; 4118 ir = xhci->interrupters[0]; 4119 4120 num_tds = urb->number_of_packets; 4121 if (num_tds < 1) { 4122 xhci_dbg(xhci, "Isoc URB with zero packets?\n"); 4123 return -EINVAL; 4124 } 4125 start_addr = (u64) urb->transfer_dma; 4126 start_trb = &ep_ring->enqueue->generic; 4127 start_cycle = ep_ring->cycle_state; 4128 4129 urb_priv = urb->hcpriv; 4130 4131 if (urb->dev->speed == USB_SPEED_FULL || urb->dev->speed == USB_SPEED_LOW) 4132 uinterval = urb->interval * 8; 4133 4134 start_uframe = xhci_get_isoc_start_frame(xhci, urb, xep); 4135 4136 /* Queue the TRBs for each TD, even if they are zero-length */ 4137 for (i = 0; i < num_tds; i++) { 4138 unsigned int total_pkt_count, max_pkt; 4139 unsigned int burst_count, last_burst_pkt_count; 4140 u32 sia_frame_id; 4141 4142 first_trb = true; 4143 running_total = 0; 4144 addr = start_addr + urb->iso_frame_desc[i].offset; 4145 td_len = urb->iso_frame_desc[i].length; 4146 td_remain_len = td_len; 4147 max_pkt = xhci_usb_endpoint_maxp(urb->dev, urb->ep); 4148 total_pkt_count = DIV_ROUND_UP(td_len, max_pkt); 4149 4150 /* A zero-length transfer still involves at least one packet. */ 4151 if (total_pkt_count == 0) 4152 total_pkt_count++; 4153 burst_count = xhci_get_burst_count(xhci, urb, total_pkt_count); 4154 last_burst_pkt_count = xhci_get_last_burst_packet_count(xhci, 4155 urb, total_pkt_count); 4156 4157 trbs_per_td = count_isoc_trbs_needed(urb, i); 4158 4159 ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index, 4160 urb->stream_id, trbs_per_td, urb, i, mem_flags); 4161 if (ret < 0) { 4162 if (i == 0) 4163 return ret; 4164 goto cleanup; 4165 } 4166 td = &urb_priv->td[i]; 4167 4168 4169 /* Choose SIA or frame ID based scheduling for this TD */ 4170 if (xhci_isoc_td_uses_frame_id(xhci, urb, xep, i)) { 4171 sia_frame_id = (start_uframe + i * uinterval) / 8; 4172 sia_frame_id = TRB_FRAME_ID(sia_frame_id % MAX_FRAMES); 4173 } else { 4174 sia_frame_id = TRB_SIA; 4175 } 4176 4177 /* 4178 * Set isoc specific data for the first TRB in a TD. 4179 * Prevent HW from getting the TRBs by keeping the cycle state 4180 * inverted in the first TDs isoc TRB. 4181 */ 4182 field = TRB_TYPE(TRB_ISOC) | 4183 TRB_TLBPC(last_burst_pkt_count) | 4184 sia_frame_id | 4185 (i ? ep_ring->cycle_state : !start_cycle); 4186 4187 /* xhci 1.1 with ETE uses TD_Size field for TBC, old is Rsvdz */ 4188 if (!xep->use_extended_tbc) 4189 field |= TRB_TBC(burst_count); 4190 4191 /* fill the rest of the TRB fields, and remaining normal TRBs */ 4192 for (j = 0; j < trbs_per_td; j++) { 4193 u32 remainder = 0; 4194 4195 /* only first TRB is isoc, overwrite otherwise */ 4196 if (!first_trb) 4197 field = TRB_TYPE(TRB_NORMAL) | 4198 ep_ring->cycle_state; 4199 4200 /* Only set interrupt on short packet for IN EPs */ 4201 if (usb_urb_dir_in(urb)) 4202 field |= TRB_ISP; 4203 4204 /* Set the chain bit for all except the last TRB */ 4205 if (j < trbs_per_td - 1) { 4206 more_trbs_coming = true; 4207 field |= TRB_CHAIN; 4208 } else { 4209 more_trbs_coming = false; 4210 td->end_trb = ep_ring->enqueue; 4211 td->end_seg = ep_ring->enq_seg; 4212 field |= TRB_IOC; 4213 if (trb_block_event_intr(xhci, num_tds, i, ir)) 4214 field |= TRB_BEI; 4215 } 4216 /* Calculate TRB length */ 4217 trb_buff_len = TRB_BUFF_LEN_UP_TO_BOUNDARY(addr); 4218 if (trb_buff_len > td_remain_len) 4219 trb_buff_len = td_remain_len; 4220 4221 /* Set the TRB length, TD size, & interrupter fields. */ 4222 remainder = xhci_td_remainder(xhci, running_total, 4223 trb_buff_len, td_len, 4224 urb, more_trbs_coming); 4225 4226 length_field = TRB_LEN(trb_buff_len) | 4227 TRB_INTR_TARGET(0); 4228 4229 /* xhci 1.1 with ETE uses TD Size field for TBC */ 4230 if (first_trb && xep->use_extended_tbc) 4231 length_field |= TRB_TD_SIZE_TBC(burst_count); 4232 else 4233 length_field |= TRB_TD_SIZE(remainder); 4234 first_trb = false; 4235 4236 queue_trb(xhci, ep_ring, more_trbs_coming, 4237 lower_32_bits(addr), 4238 upper_32_bits(addr), 4239 length_field, 4240 field); 4241 running_total += trb_buff_len; 4242 4243 addr += trb_buff_len; 4244 td_remain_len -= trb_buff_len; 4245 } 4246 4247 /* Check TD length */ 4248 if (running_total != td_len) { 4249 xhci_err(xhci, "ISOC TD length unmatch\n"); 4250 ret = -EINVAL; 4251 goto cleanup; 4252 } 4253 } 4254 4255 xep->next_uframe = (start_uframe + num_tds * uinterval) % MAX_UFRAMES; 4256 4257 if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs == 0) { 4258 if (xhci->quirks & XHCI_AMD_PLL_FIX) 4259 usb_amd_quirk_pll_disable(); 4260 } 4261 xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs++; 4262 4263 giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id, 4264 start_cycle, start_trb); 4265 return 0; 4266 cleanup: 4267 /* Clean up a partially enqueued isoc transfer. */ 4268 4269 for (i--; i >= 0; i--) 4270 list_del_init(&urb_priv->td[i].td_list); 4271 4272 /* Use the first TD as a temporary variable to turn the TDs we've queued 4273 * into No-ops with a software-owned cycle bit. That way the hardware 4274 * won't accidentally start executing bogus TDs when we partially 4275 * overwrite them. td->start_trb and td->start_seg are already set. 4276 */ 4277 urb_priv->td[0].end_trb = ep_ring->enqueue; 4278 /* Every TRB except the first & last will have its cycle bit flipped. */ 4279 td_to_noop(xhci, xep, &urb_priv->td[0], true); 4280 4281 /* Reset the ring enqueue back to the first TRB and its cycle bit. */ 4282 ep_ring->enqueue = urb_priv->td[0].start_trb; 4283 ep_ring->enq_seg = urb_priv->td[0].start_seg; 4284 ep_ring->cycle_state = start_cycle; 4285 usb_hcd_unlink_urb_from_ep(bus_to_hcd(urb->dev->bus), urb); 4286 return ret; 4287 } 4288 4289 /* 4290 * Check transfer ring to guarantee there is enough room for the urb. 4291 * Update ISO URB start_frame and interval. 4292 * Update interval as xhci_queue_intr_tx does. Use xhci frame_index to 4293 * update urb->start_frame if URB_ISO_ASAP is set in transfer_flags or 4294 * Contiguous Frame ID is not supported by HC. 4295 */ 4296 int xhci_queue_isoc_tx_prepare(struct xhci_hcd *xhci, gfp_t mem_flags, 4297 struct urb *urb, int slot_id, unsigned int ep_index) 4298 { 4299 struct xhci_virt_device *xdev; 4300 struct xhci_ring *ep_ring; 4301 struct xhci_ep_ctx *ep_ctx; 4302 struct xhci_virt_ep *xep; 4303 int num_tds, num_trbs, i; 4304 int ret; 4305 4306 xdev = xhci->devs[slot_id]; 4307 xep = &xhci->devs[slot_id]->eps[ep_index]; 4308 ep_ring = xdev->eps[ep_index].ring; 4309 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index); 4310 4311 num_trbs = 0; 4312 num_tds = urb->number_of_packets; 4313 for (i = 0; i < num_tds; i++) 4314 num_trbs += count_isoc_trbs_needed(urb, i); 4315 4316 /* Check the ring to guarantee there is enough room for the whole urb. 4317 * Do not insert any td of the urb to the ring if the check failed. 4318 */ 4319 ret = prepare_ring(xhci, ep_ring, GET_EP_CTX_STATE(ep_ctx), 4320 num_trbs, mem_flags); 4321 if (ret) 4322 return ret; 4323 4324 /* 4325 * Check interval value. This should be done before we start to 4326 * calculate the start frame value. 4327 */ 4328 check_interval(urb, ep_ctx); 4329 4330 /* 4331 * Schedule the URB discontiguously if all previous URBs have completed. 4332 * XXX core can't tell if completions are pending but not running yet. 4333 */ 4334 if (list_empty(&ep_ring->td_list) && 4335 !hcd_periodic_completion_in_progress(xhci_to_hcd(xhci), urb->ep)) { 4336 if (GET_EP_CTX_STATE(ep_ctx) == EP_STATE_RUNNING) 4337 xhci_dbg(xhci, "Unexpected running ring at isoc stream start, uframe: %d\n", 4338 xep->next_uframe); 4339 xep->next_uframe = -1; 4340 } 4341 4342 return xhci_queue_isoc_tx(xhci, mem_flags, urb, slot_id, ep_index); 4343 } 4344 4345 /**** Command Ring Operations ****/ 4346 4347 /* Generic function for queueing a command TRB on the command ring. 4348 * Check to make sure there's room on the command ring for one command TRB. 4349 * Also check that there's room reserved for commands that must not fail. 4350 * If this is a command that must not fail, meaning command_must_succeed = TRUE, 4351 * then only check for the number of reserved spots. 4352 * Don't decrement xhci->cmd_ring_reserved_trbs after we've queued the TRB 4353 * because the command event handler may want to resubmit a failed command. 4354 */ 4355 static int queue_command(struct xhci_hcd *xhci, struct xhci_command *cmd, 4356 u32 field1, u32 field2, 4357 u32 field3, u32 field4, bool command_must_succeed) 4358 { 4359 int reserved_trbs = xhci->cmd_ring_reserved_trbs; 4360 struct usb_hcd *hcd = xhci_to_hcd(xhci); 4361 int ret; 4362 4363 if ((xhci->xhc_state & XHCI_STATE_DYING) || 4364 (xhci->xhc_state & XHCI_STATE_HALTED)) { 4365 xhci_dbg(xhci, "xHCI dying or halted, can't queue_command. state: 0x%x\n", 4366 xhci->xhc_state); 4367 return -ESHUTDOWN; 4368 } 4369 4370 if (!HCD_HW_ACCESSIBLE(hcd)) { 4371 xhci_warn(xhci, "Can't queue command, xHC not accessible\n"); 4372 return -ESHUTDOWN; 4373 } 4374 4375 if (!command_must_succeed) 4376 reserved_trbs++; 4377 4378 ret = prepare_ring(xhci, xhci->cmd_ring, EP_STATE_RUNNING, 4379 reserved_trbs, GFP_ATOMIC); 4380 if (ret < 0) { 4381 xhci_err(xhci, "ERR: No room for command on command ring\n"); 4382 if (command_must_succeed) 4383 xhci_err(xhci, "ERR: Reserved TRB counting for " 4384 "unfailable commands failed.\n"); 4385 return ret; 4386 } 4387 4388 cmd->command_trb = xhci->cmd_ring->enqueue; 4389 4390 /* if there are no other commands queued we start the timeout timer */ 4391 if (list_empty(&xhci->cmd_list)) { 4392 xhci->current_cmd = cmd; 4393 xhci_mod_cmd_timer(xhci); 4394 } 4395 4396 list_add_tail(&cmd->cmd_list, &xhci->cmd_list); 4397 4398 queue_trb(xhci, xhci->cmd_ring, false, field1, field2, field3, 4399 field4 | xhci->cmd_ring->cycle_state); 4400 return 0; 4401 } 4402 4403 /* Queue a slot enable or disable request on the command ring */ 4404 int xhci_queue_slot_control(struct xhci_hcd *xhci, struct xhci_command *cmd, 4405 u32 trb_type, u32 slot_id) 4406 { 4407 return queue_command(xhci, cmd, 0, 0, 0, 4408 TRB_TYPE(trb_type) | SLOT_ID_FOR_TRB(slot_id), false); 4409 } 4410 4411 /* Queue an address device command TRB */ 4412 int xhci_queue_address_device(struct xhci_hcd *xhci, struct xhci_command *cmd, 4413 dma_addr_t in_ctx_ptr, u32 slot_id, enum xhci_setup_dev setup) 4414 { 4415 return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr), 4416 upper_32_bits(in_ctx_ptr), 0, 4417 TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id) 4418 | (setup == SETUP_CONTEXT_ONLY ? TRB_BSR : 0), false); 4419 } 4420 4421 int xhci_queue_vendor_command(struct xhci_hcd *xhci, struct xhci_command *cmd, 4422 u32 field1, u32 field2, u32 field3, u32 field4) 4423 { 4424 return queue_command(xhci, cmd, field1, field2, field3, field4, false); 4425 } 4426 4427 /* Queue a reset device command TRB */ 4428 int xhci_queue_reset_device(struct xhci_hcd *xhci, struct xhci_command *cmd, 4429 u32 slot_id) 4430 { 4431 return queue_command(xhci, cmd, 0, 0, 0, 4432 TRB_TYPE(TRB_RESET_DEV) | SLOT_ID_FOR_TRB(slot_id), 4433 false); 4434 } 4435 4436 /* Queue a configure endpoint command TRB */ 4437 int xhci_queue_configure_endpoint(struct xhci_hcd *xhci, 4438 struct xhci_command *cmd, dma_addr_t in_ctx_ptr, 4439 u32 slot_id, bool command_must_succeed) 4440 { 4441 return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr), 4442 upper_32_bits(in_ctx_ptr), 0, 4443 TRB_TYPE(TRB_CONFIG_EP) | SLOT_ID_FOR_TRB(slot_id), 4444 command_must_succeed); 4445 } 4446 4447 /* Queue a get root hub port bandwidth command TRB */ 4448 int xhci_queue_get_port_bw(struct xhci_hcd *xhci, 4449 struct xhci_command *cmd, dma_addr_t in_ctx_ptr, 4450 u8 dev_speed, bool command_must_succeed) 4451 { 4452 return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr), 4453 upper_32_bits(in_ctx_ptr), 0, 4454 TRB_TYPE(TRB_GET_BW) | DEV_SPEED_FOR_TRB(dev_speed), 4455 command_must_succeed); 4456 } 4457 4458 /* Queue an evaluate context command TRB */ 4459 int xhci_queue_evaluate_context(struct xhci_hcd *xhci, struct xhci_command *cmd, 4460 dma_addr_t in_ctx_ptr, u32 slot_id, bool command_must_succeed) 4461 { 4462 return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr), 4463 upper_32_bits(in_ctx_ptr), 0, 4464 TRB_TYPE(TRB_EVAL_CONTEXT) | SLOT_ID_FOR_TRB(slot_id), 4465 command_must_succeed); 4466 } 4467 4468 /* 4469 * Suspend is set to indicate "Stop Endpoint Command" is being issued to stop 4470 * activity on an endpoint that is about to be suspended. 4471 */ 4472 int xhci_queue_stop_endpoint(struct xhci_hcd *xhci, struct xhci_command *cmd, 4473 int slot_id, unsigned int ep_index, int suspend) 4474 { 4475 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id); 4476 u32 trb_ep_index = EP_INDEX_FOR_TRB(ep_index); 4477 u32 type = TRB_TYPE(TRB_STOP_RING); 4478 u32 trb_suspend = SUSPEND_PORT_FOR_TRB(suspend); 4479 4480 return queue_command(xhci, cmd, 0, 0, 0, 4481 trb_slot_id | trb_ep_index | type | trb_suspend, false); 4482 } 4483 4484 int xhci_queue_reset_ep(struct xhci_hcd *xhci, struct xhci_command *cmd, 4485 int slot_id, unsigned int ep_index, 4486 enum xhci_ep_reset_type reset_type) 4487 { 4488 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id); 4489 u32 trb_ep_index = EP_INDEX_FOR_TRB(ep_index); 4490 u32 type = TRB_TYPE(TRB_RESET_EP); 4491 4492 if (reset_type == EP_SOFT_RESET) 4493 type |= TRB_TSP; 4494 4495 return queue_command(xhci, cmd, 0, 0, 0, 4496 trb_slot_id | trb_ep_index | type, false); 4497 } 4498