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