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) || (ep_state & SET_DEQ_PENDING) || 565 (ep_state & EP_HALTED) || (ep_state & EP_CLEARING_TT)) 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 default: 2577 /* do nothing */ 2578 break; 2579 } 2580 2581 if (ep_trb == td->end_trb) 2582 td->urb->actual_length = requested - remaining; 2583 else 2584 td->urb->actual_length = 2585 sum_trb_lengths(td, ep_trb) + 2586 ep_trb_len - remaining; 2587 finish_td: 2588 if (remaining > requested) { 2589 xhci_warn(xhci, "bad transfer trb length %d in event trb\n", 2590 remaining); 2591 td->urb->actual_length = 0; 2592 } 2593 2594 finish_td(xhci, ep, ep_ring, td, trb_comp_code); 2595 } 2596 2597 /* Transfer events which don't point to a transfer TRB, see xhci 4.17.4 */ 2598 static int handle_transferless_tx_event(struct xhci_hcd *xhci, struct xhci_virt_ep *ep, 2599 u32 trb_comp_code) 2600 { 2601 switch (trb_comp_code) { 2602 case COMP_STALL_ERROR: 2603 case COMP_USB_TRANSACTION_ERROR: 2604 case COMP_INVALID_STREAM_TYPE_ERROR: 2605 case COMP_INVALID_STREAM_ID_ERROR: 2606 xhci_dbg(xhci, "Stream transaction error ep %u no id\n", ep->ep_index); 2607 if (ep->err_count++ > MAX_SOFT_RETRY) 2608 xhci_handle_halted_endpoint(xhci, ep, NULL, EP_HARD_RESET); 2609 else 2610 xhci_handle_halted_endpoint(xhci, ep, NULL, EP_SOFT_RESET); 2611 break; 2612 case COMP_RING_UNDERRUN: 2613 case COMP_RING_OVERRUN: 2614 case COMP_STOPPED_LENGTH_INVALID: 2615 break; 2616 default: 2617 xhci_err(xhci, "Transfer event %u for unknown stream ring slot %u ep %u\n", 2618 trb_comp_code, ep->vdev->slot_id, ep->ep_index); 2619 return -ENODEV; 2620 } 2621 return 0; 2622 } 2623 2624 static bool xhci_spurious_success_tx_event(struct xhci_hcd *xhci, 2625 struct xhci_ring *ring) 2626 { 2627 switch (ring->old_trb_comp_code) { 2628 case COMP_SHORT_PACKET: 2629 return xhci->quirks & XHCI_SPURIOUS_SUCCESS; 2630 case COMP_USB_TRANSACTION_ERROR: 2631 case COMP_BABBLE_DETECTED_ERROR: 2632 case COMP_ISOCH_BUFFER_OVERRUN: 2633 return xhci->quirks & XHCI_ETRON_HOST && 2634 ring->type == TYPE_ISOC; 2635 default: 2636 return false; 2637 } 2638 } 2639 2640 /* 2641 * If this function returns an error condition, it means it got a Transfer 2642 * event with a corrupted Slot ID, Endpoint ID, or TRB DMA address. 2643 * At this point, the host controller is probably hosed and should be reset. 2644 */ 2645 static int handle_tx_event(struct xhci_hcd *xhci, 2646 struct xhci_interrupter *ir, 2647 struct xhci_transfer_event *event) 2648 { 2649 struct xhci_virt_ep *ep; 2650 struct xhci_ring *ep_ring; 2651 unsigned int slot_id; 2652 int ep_index; 2653 struct xhci_td *td = NULL; 2654 dma_addr_t ep_trb_dma; 2655 struct xhci_segment *ep_seg; 2656 union xhci_trb *ep_trb; 2657 int status = -EINPROGRESS; 2658 struct xhci_ep_ctx *ep_ctx; 2659 u32 trb_comp_code; 2660 bool ring_xrun_event = false; 2661 2662 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags)); 2663 ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1; 2664 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len)); 2665 ep_trb_dma = le64_to_cpu(event->buffer); 2666 2667 ep = xhci_get_virt_ep(xhci, slot_id, ep_index); 2668 if (!ep) { 2669 xhci_err(xhci, "ERROR Invalid Transfer event\n"); 2670 goto err_out; 2671 } 2672 2673 ep_ring = xhci_dma_to_transfer_ring(ep, ep_trb_dma); 2674 ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep_index); 2675 2676 if (GET_EP_CTX_STATE(ep_ctx) == EP_STATE_DISABLED) { 2677 xhci_err(xhci, 2678 "ERROR Transfer event for disabled endpoint slot %u ep %u\n", 2679 slot_id, ep_index); 2680 goto err_out; 2681 } 2682 2683 if (!ep_ring) 2684 return handle_transferless_tx_event(xhci, ep, trb_comp_code); 2685 2686 /* Look for common error cases */ 2687 switch (trb_comp_code) { 2688 /* Skip codes that require special handling depending on 2689 * transfer type 2690 */ 2691 case COMP_SUCCESS: 2692 if (EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)) != 0) { 2693 trb_comp_code = COMP_SHORT_PACKET; 2694 xhci_dbg(xhci, "Successful completion on short TX for slot %u ep %u with last td comp code %d\n", 2695 slot_id, ep_index, ep_ring->old_trb_comp_code); 2696 } 2697 break; 2698 case COMP_SHORT_PACKET: 2699 break; 2700 /* Completion codes for endpoint stopped state */ 2701 case COMP_STOPPED: 2702 xhci_dbg(xhci, "Stopped on Transfer TRB for slot %u ep %u\n", 2703 slot_id, ep_index); 2704 break; 2705 case COMP_STOPPED_LENGTH_INVALID: 2706 xhci_dbg(xhci, 2707 "Stopped on No-op or Link TRB for slot %u ep %u\n", 2708 slot_id, ep_index); 2709 break; 2710 case COMP_STOPPED_SHORT_PACKET: 2711 xhci_dbg(xhci, 2712 "Stopped with short packet transfer detected for slot %u ep %u\n", 2713 slot_id, ep_index); 2714 break; 2715 /* Completion codes for endpoint halted state */ 2716 case COMP_STALL_ERROR: 2717 xhci_dbg(xhci, "Stalled endpoint for slot %u ep %u\n", slot_id, 2718 ep_index); 2719 status = -EPIPE; 2720 break; 2721 case COMP_SPLIT_TRANSACTION_ERROR: 2722 xhci_dbg(xhci, "Split transaction error for slot %u ep %u\n", 2723 slot_id, ep_index); 2724 status = -EPROTO; 2725 break; 2726 case COMP_USB_TRANSACTION_ERROR: 2727 xhci_dbg(xhci, "Transfer error for slot %u ep %u on endpoint\n", 2728 slot_id, ep_index); 2729 status = -EPROTO; 2730 break; 2731 case COMP_BABBLE_DETECTED_ERROR: 2732 xhci_dbg(xhci, "Babble error for slot %u ep %u on endpoint\n", 2733 slot_id, ep_index); 2734 status = -EOVERFLOW; 2735 break; 2736 /* Completion codes for endpoint error state */ 2737 case COMP_TRB_ERROR: 2738 xhci_warn(xhci, 2739 "WARN: TRB error for slot %u ep %u on endpoint\n", 2740 slot_id, ep_index); 2741 status = -EILSEQ; 2742 break; 2743 /* completion codes not indicating endpoint state change */ 2744 case COMP_DATA_BUFFER_ERROR: 2745 xhci_warn(xhci, 2746 "WARN: HC couldn't access mem fast enough for slot %u ep %u\n", 2747 slot_id, ep_index); 2748 status = -ENOSR; 2749 break; 2750 case COMP_BANDWIDTH_OVERRUN_ERROR: 2751 xhci_warn(xhci, 2752 "WARN: bandwidth overrun event for slot %u ep %u on endpoint\n", 2753 slot_id, ep_index); 2754 break; 2755 case COMP_ISOCH_BUFFER_OVERRUN: 2756 xhci_warn(xhci, 2757 "WARN: buffer overrun event for slot %u ep %u on endpoint", 2758 slot_id, ep_index); 2759 break; 2760 case COMP_RING_UNDERRUN: 2761 /* 2762 * When the Isoch ring is empty, the xHC will generate 2763 * a Ring Overrun Event for IN Isoch endpoint or Ring 2764 * Underrun Event for OUT Isoch endpoint. 2765 */ 2766 xhci_dbg(xhci, "Underrun event on slot %u ep %u\n", slot_id, ep_index); 2767 ring_xrun_event = true; 2768 break; 2769 case COMP_RING_OVERRUN: 2770 xhci_dbg(xhci, "Overrun event on slot %u ep %u\n", slot_id, ep_index); 2771 ring_xrun_event = true; 2772 break; 2773 case COMP_MISSED_SERVICE_ERROR: 2774 /* 2775 * When encounter missed service error, one or more isoc tds 2776 * may be missed by xHC. 2777 * Set skip flag of the ep_ring; Complete the missed tds as 2778 * short transfer when process the ep_ring next time. 2779 */ 2780 ep->skip = true; 2781 xhci_dbg(xhci, 2782 "Miss service interval error for slot %u ep %u, set skip flag%s\n", 2783 slot_id, ep_index, ep_trb_dma ? ", skip now" : ""); 2784 break; 2785 case COMP_NO_PING_RESPONSE_ERROR: 2786 ep->skip = true; 2787 xhci_dbg(xhci, 2788 "No Ping response error for slot %u ep %u, Skip one Isoc TD\n", 2789 slot_id, ep_index); 2790 return 0; 2791 2792 case COMP_INCOMPATIBLE_DEVICE_ERROR: 2793 /* needs disable slot command to recover */ 2794 xhci_warn(xhci, 2795 "WARN: detect an incompatible device for slot %u ep %u", 2796 slot_id, ep_index); 2797 status = -EPROTO; 2798 break; 2799 default: 2800 if (xhci_is_vendor_info_code(xhci, trb_comp_code)) { 2801 status = 0; 2802 break; 2803 } 2804 xhci_warn(xhci, 2805 "ERROR Unknown event condition %u for slot %u ep %u , HC probably busted\n", 2806 trb_comp_code, slot_id, ep_index); 2807 if (ep->skip) 2808 break; 2809 return 0; 2810 } 2811 2812 /* 2813 * xhci 4.10.2 states isoc endpoints should continue 2814 * processing the next TD if there was an error mid TD. 2815 * So host like NEC don't generate an event for the last 2816 * isoc TRB even if the IOC flag is set. 2817 * xhci 4.9.1 states that if there are errors in mult-TRB 2818 * TDs xHC should generate an error for that TRB, and if xHC 2819 * proceeds to the next TD it should genete an event for 2820 * any TRB with IOC flag on the way. Other host follow this. 2821 * 2822 * We wait for the final IOC event, but if we get an event 2823 * anywhere outside this TD, just give it back already. 2824 */ 2825 td = list_first_entry_or_null(&ep_ring->td_list, struct xhci_td, td_list); 2826 2827 if (td && td->error_mid_td && !trb_in_td(td, ep_trb_dma)) { 2828 xhci_dbg(xhci, "Missing TD completion event after mid TD error\n"); 2829 xhci_dequeue_td(xhci, td, ep_ring, td->status); 2830 } 2831 2832 /* If the TRB pointer is NULL, missed TDs will be skipped on the next event */ 2833 if (trb_comp_code == COMP_MISSED_SERVICE_ERROR && !ep_trb_dma) 2834 return 0; 2835 2836 if (list_empty(&ep_ring->td_list)) { 2837 /* 2838 * Don't print wanings if ring is empty due to a stopped endpoint generating an 2839 * extra completion event if the device was suspended. Or, a event for the last TRB 2840 * of a short TD we already got a short event for. The short TD is already removed 2841 * from the TD list. 2842 */ 2843 if (trb_comp_code != COMP_STOPPED && 2844 trb_comp_code != COMP_STOPPED_LENGTH_INVALID && 2845 !ring_xrun_event && 2846 !xhci_spurious_success_tx_event(xhci, ep_ring)) { 2847 xhci_warn(xhci, "Event TRB for slot %u ep %u with no TDs queued\n", 2848 slot_id, ep_index); 2849 } 2850 2851 ep->skip = false; 2852 goto check_endpoint_halted; 2853 } 2854 2855 do { 2856 td = list_first_entry(&ep_ring->td_list, struct xhci_td, 2857 td_list); 2858 2859 /* Is this a TRB in the currently executing TD? */ 2860 ep_seg = trb_in_td(td, ep_trb_dma); 2861 2862 if (!ep_seg) { 2863 2864 if (ep->skip && usb_endpoint_xfer_isoc(&td->urb->ep->desc)) { 2865 /* this event is unlikely to match any TD, don't skip them all */ 2866 if (trb_comp_code == COMP_STOPPED_LENGTH_INVALID) 2867 return 0; 2868 2869 skip_isoc_td(xhci, td, ep, status); 2870 2871 if (!list_empty(&ep_ring->td_list)) { 2872 if (ring_xrun_event) { 2873 /* 2874 * If we are here, we are on xHCI 1.0 host with no 2875 * idea how many TDs were missed or where the xrun 2876 * occurred. New TDs may have been added after the 2877 * xrun, so skip only one TD to be safe. 2878 */ 2879 xhci_dbg(xhci, "Skipped one TD for slot %u ep %u", 2880 slot_id, ep_index); 2881 return 0; 2882 } 2883 continue; 2884 } 2885 2886 xhci_dbg(xhci, "All TDs skipped for slot %u ep %u. Clear skip flag.\n", 2887 slot_id, ep_index); 2888 ep->skip = false; 2889 td = NULL; 2890 goto check_endpoint_halted; 2891 } 2892 2893 /* TD was queued after xrun, maybe xrun was on a link, don't panic yet */ 2894 if (ring_xrun_event) 2895 return 0; 2896 2897 /* 2898 * Skip the Force Stopped Event. The 'ep_trb' of FSE is not in the current 2899 * TD pointed by 'ep_ring->dequeue' because that the hardware dequeue 2900 * pointer still at the previous TRB of the current TD. The previous TRB 2901 * maybe a Link TD or the last TRB of the previous TD. The command 2902 * completion handle will take care the rest. 2903 */ 2904 if (trb_comp_code == COMP_STOPPED || 2905 trb_comp_code == COMP_STOPPED_LENGTH_INVALID) { 2906 return 0; 2907 } 2908 2909 /* 2910 * Some hosts give a spurious success event after a short 2911 * transfer or error on last TRB. Ignore it. 2912 */ 2913 if (xhci_spurious_success_tx_event(xhci, ep_ring)) { 2914 xhci_dbg(xhci, "Spurious event dma %pad, comp_code %u after %u\n", 2915 &ep_trb_dma, trb_comp_code, ep_ring->old_trb_comp_code); 2916 ep_ring->old_trb_comp_code = 0; 2917 return 0; 2918 } 2919 2920 /* HC is busted, give up! */ 2921 goto debug_finding_td; 2922 } 2923 2924 if (ep->skip) { 2925 xhci_dbg(xhci, 2926 "Found td. Clear skip flag for slot %u ep %u.\n", 2927 slot_id, ep_index); 2928 ep->skip = false; 2929 } 2930 2931 /* 2932 * If ep->skip is set, it means there are missed tds on the 2933 * endpoint ring need to take care of. 2934 * Process them as short transfer until reach the td pointed by 2935 * the event. 2936 */ 2937 } while (ep->skip); 2938 2939 ep_ring->old_trb_comp_code = trb_comp_code; 2940 2941 /* Get out if a TD was queued at enqueue after the xrun occurred */ 2942 if (ring_xrun_event) 2943 return 0; 2944 2945 ep_trb = &ep_seg->trbs[(ep_trb_dma - ep_seg->dma) / sizeof(*ep_trb)]; 2946 trace_xhci_handle_transfer(ep_ring, (struct xhci_generic_trb *) ep_trb, ep_trb_dma); 2947 2948 /* 2949 * No-op TRB could trigger interrupts in a case where a URB was killed 2950 * and a STALL_ERROR happens right after the endpoint ring stopped. 2951 * Reset the halted endpoint. Otherwise, the endpoint remains stalled 2952 * indefinitely. 2953 */ 2954 2955 if (trb_is_noop(ep_trb)) 2956 goto check_endpoint_halted; 2957 2958 td->status = status; 2959 2960 /* update the urb's actual_length and give back to the core */ 2961 if (usb_endpoint_xfer_control(&td->urb->ep->desc)) 2962 process_ctrl_td(xhci, ep, ep_ring, td, ep_trb, event); 2963 else if (usb_endpoint_xfer_isoc(&td->urb->ep->desc)) 2964 process_isoc_td(xhci, ep, ep_ring, td, ep_trb, event); 2965 else 2966 process_bulk_intr_td(xhci, ep, ep_ring, td, ep_trb, event); 2967 return 0; 2968 2969 check_endpoint_halted: 2970 if (xhci_halted_host_endpoint(ep_ctx, trb_comp_code)) 2971 xhci_handle_halted_endpoint(xhci, ep, td, EP_HARD_RESET); 2972 2973 return 0; 2974 2975 debug_finding_td: 2976 xhci_err(xhci, "Event dma %pad for ep %d status %d not part of TD at %016llx - %016llx\n", 2977 &ep_trb_dma, ep_index, trb_comp_code, 2978 (unsigned long long)xhci_trb_virt_to_dma(td->start_seg, td->start_trb), 2979 (unsigned long long)xhci_trb_virt_to_dma(td->end_seg, td->end_trb)); 2980 2981 xhci_for_each_ring_seg(ep_ring->first_seg, ep_seg) 2982 xhci_warn(xhci, "Ring seg %u dma %pad\n", ep_seg->num, &ep_seg->dma); 2983 2984 return -ESHUTDOWN; 2985 2986 err_out: 2987 xhci_err(xhci, "@%016llx %08x %08x %08x %08x\n", 2988 (unsigned long long) xhci_trb_virt_to_dma( 2989 ir->event_ring->deq_seg, 2990 ir->event_ring->dequeue), 2991 lower_32_bits(le64_to_cpu(event->buffer)), 2992 upper_32_bits(le64_to_cpu(event->buffer)), 2993 le32_to_cpu(event->transfer_len), 2994 le32_to_cpu(event->flags)); 2995 return -ENODEV; 2996 } 2997 2998 /* 2999 * This function handles one OS-owned event on the event ring. It may drop 3000 * xhci->lock between event processing (e.g. to pass up port status changes). 3001 */ 3002 static int xhci_handle_event_trb(struct xhci_hcd *xhci, struct xhci_interrupter *ir, 3003 union xhci_trb *event) 3004 { 3005 u32 trb_type; 3006 3007 trace_xhci_handle_event(ir->event_ring, &event->generic, 3008 xhci_trb_virt_to_dma(ir->event_ring->deq_seg, 3009 ir->event_ring->dequeue)); 3010 3011 /* 3012 * Barrier between reading the TRB_CYCLE (valid) flag before, and any 3013 * speculative reads of the event's flags/data below. 3014 */ 3015 rmb(); 3016 trb_type = TRB_FIELD_TO_TYPE(le32_to_cpu(event->event_cmd.flags)); 3017 /* FIXME: Handle more event types. */ 3018 3019 switch (trb_type) { 3020 case TRB_COMPLETION: 3021 handle_cmd_completion(xhci, &event->event_cmd); 3022 break; 3023 case TRB_PORT_STATUS: 3024 handle_port_status(xhci, event); 3025 break; 3026 case TRB_TRANSFER: 3027 handle_tx_event(xhci, ir, &event->trans_event); 3028 break; 3029 case TRB_DEV_NOTE: 3030 handle_device_notification(xhci, event); 3031 break; 3032 default: 3033 if (trb_type >= TRB_VENDOR_DEFINED_LOW) 3034 handle_vendor_event(xhci, event, trb_type); 3035 else 3036 xhci_warn(xhci, "ERROR unknown event type %d\n", trb_type); 3037 } 3038 /* Any of the above functions may drop and re-acquire the lock, so check 3039 * to make sure a watchdog timer didn't mark the host as non-responsive. 3040 */ 3041 if (xhci->xhc_state & XHCI_STATE_DYING) { 3042 xhci_dbg(xhci, "xHCI host dying, returning from event handler.\n"); 3043 return -ENODEV; 3044 } 3045 3046 return 0; 3047 } 3048 3049 /* 3050 * Update Event Ring Dequeue Pointer: 3051 * - When all events have finished 3052 * - To avoid "Event Ring Full Error" condition 3053 */ 3054 static void xhci_update_erst_dequeue(struct xhci_hcd *xhci, 3055 struct xhci_interrupter *ir, 3056 bool clear_ehb) 3057 { 3058 u64 temp_64; 3059 dma_addr_t deq; 3060 3061 temp_64 = xhci_read_64(xhci, &ir->ir_set->erst_dequeue); 3062 deq = xhci_trb_virt_to_dma(ir->event_ring->deq_seg, 3063 ir->event_ring->dequeue); 3064 if (deq == 0) 3065 xhci_warn(xhci, "WARN something wrong with SW event ring dequeue ptr\n"); 3066 /* 3067 * Per 4.9.4, Software writes to the ERDP register shall always advance 3068 * the Event Ring Dequeue Pointer value. 3069 */ 3070 if ((temp_64 & ERST_PTR_MASK) == (deq & ERST_PTR_MASK) && !clear_ehb) 3071 return; 3072 3073 /* Update HC event ring dequeue pointer */ 3074 temp_64 = ir->event_ring->deq_seg->num & ERST_DESI_MASK; 3075 temp_64 |= deq & ERST_PTR_MASK; 3076 3077 /* Clear the event handler busy flag (RW1C) */ 3078 if (clear_ehb) 3079 temp_64 |= ERST_EHB; 3080 xhci_write_64(xhci, temp_64, &ir->ir_set->erst_dequeue); 3081 } 3082 3083 /* Clear the interrupt pending bit for a specific interrupter. */ 3084 static void xhci_clear_interrupt_pending(struct xhci_interrupter *ir) 3085 { 3086 if (!ir->ip_autoclear) { 3087 u32 irq_pending; 3088 3089 irq_pending = readl(&ir->ir_set->irq_pending); 3090 irq_pending |= IMAN_IP; 3091 writel(irq_pending, &ir->ir_set->irq_pending); 3092 } 3093 } 3094 3095 /* 3096 * Handle all OS-owned events on an interrupter event ring. It may drop 3097 * and reaquire xhci->lock between event processing. 3098 */ 3099 static int xhci_handle_events(struct xhci_hcd *xhci, struct xhci_interrupter *ir) 3100 { 3101 int event_loop = 0; 3102 int err; 3103 u64 temp; 3104 3105 xhci_clear_interrupt_pending(ir); 3106 3107 /* Event ring hasn't been allocated yet. */ 3108 if (!ir->event_ring || !ir->event_ring->dequeue) { 3109 xhci_err(xhci, "ERROR interrupter event ring not ready\n"); 3110 return -ENOMEM; 3111 } 3112 3113 if (xhci->xhc_state & XHCI_STATE_DYING || 3114 xhci->xhc_state & XHCI_STATE_HALTED) { 3115 xhci_dbg(xhci, "xHCI dying, ignoring interrupt. Shouldn't IRQs be disabled?\n"); 3116 3117 /* Clear the event handler busy flag (RW1C) */ 3118 temp = xhci_read_64(xhci, &ir->ir_set->erst_dequeue); 3119 xhci_write_64(xhci, temp | ERST_EHB, &ir->ir_set->erst_dequeue); 3120 return -ENODEV; 3121 } 3122 3123 /* Process all OS owned event TRBs on this event ring */ 3124 while (unhandled_event_trb(ir->event_ring)) { 3125 err = xhci_handle_event_trb(xhci, ir, ir->event_ring->dequeue); 3126 3127 /* 3128 * If half a segment of events have been handled in one go then 3129 * update ERDP, and force isoc trbs to interrupt more often 3130 */ 3131 if (event_loop++ > TRBS_PER_SEGMENT / 2) { 3132 xhci_update_erst_dequeue(xhci, ir, false); 3133 3134 if (ir->isoc_bei_interval > AVOID_BEI_INTERVAL_MIN) 3135 ir->isoc_bei_interval = ir->isoc_bei_interval / 2; 3136 3137 event_loop = 0; 3138 } 3139 3140 /* Update SW event ring dequeue pointer */ 3141 inc_deq(xhci, ir->event_ring); 3142 3143 if (err) 3144 break; 3145 } 3146 3147 xhci_update_erst_dequeue(xhci, ir, true); 3148 3149 return 0; 3150 } 3151 3152 /* 3153 * xHCI spec says we can get an interrupt, and if the HC has an error condition, 3154 * we might get bad data out of the event ring. Section 4.10.2.7 has a list of 3155 * indicators of an event TRB error, but we check the status *first* to be safe. 3156 */ 3157 irqreturn_t xhci_irq(struct usb_hcd *hcd) 3158 { 3159 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 3160 irqreturn_t ret = IRQ_HANDLED; 3161 u32 status; 3162 3163 spin_lock(&xhci->lock); 3164 /* Check if the xHC generated the interrupt, or the irq is shared */ 3165 status = readl(&xhci->op_regs->status); 3166 if (status == ~(u32)0) { 3167 xhci_hc_died(xhci); 3168 goto out; 3169 } 3170 3171 if (!(status & STS_EINT)) { 3172 ret = IRQ_NONE; 3173 goto out; 3174 } 3175 3176 if (status & STS_HCE) { 3177 xhci_warn(xhci, "WARNING: Host Controller Error\n"); 3178 goto out; 3179 } 3180 3181 if (status & STS_FATAL) { 3182 xhci_warn(xhci, "WARNING: Host System Error\n"); 3183 xhci_halt(xhci); 3184 goto out; 3185 } 3186 3187 /* 3188 * Clear the op reg interrupt status first, 3189 * so we can receive interrupts from other MSI-X interrupters. 3190 * Write 1 to clear the interrupt status. 3191 */ 3192 status |= STS_EINT; 3193 writel(status, &xhci->op_regs->status); 3194 3195 /* This is the handler of the primary interrupter */ 3196 xhci_handle_events(xhci, xhci->interrupters[0]); 3197 out: 3198 spin_unlock(&xhci->lock); 3199 3200 return ret; 3201 } 3202 3203 irqreturn_t xhci_msi_irq(int irq, void *hcd) 3204 { 3205 return xhci_irq(hcd); 3206 } 3207 EXPORT_SYMBOL_GPL(xhci_msi_irq); 3208 3209 /**** Endpoint Ring Operations ****/ 3210 3211 /* 3212 * Generic function for queueing a TRB on a ring. 3213 * The caller must have checked to make sure there's room on the ring. 3214 * 3215 * @more_trbs_coming: Will you enqueue more TRBs before calling 3216 * prepare_transfer()? 3217 */ 3218 static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring, 3219 bool more_trbs_coming, 3220 u32 field1, u32 field2, u32 field3, u32 field4) 3221 { 3222 struct xhci_generic_trb *trb; 3223 3224 trb = &ring->enqueue->generic; 3225 trb->field[0] = cpu_to_le32(field1); 3226 trb->field[1] = cpu_to_le32(field2); 3227 trb->field[2] = cpu_to_le32(field3); 3228 /* make sure TRB is fully written before giving it to the controller */ 3229 wmb(); 3230 trb->field[3] = cpu_to_le32(field4); 3231 3232 trace_xhci_queue_trb(ring, trb, 3233 xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue)); 3234 3235 inc_enq(xhci, ring, more_trbs_coming); 3236 } 3237 3238 /* 3239 * Does various checks on the endpoint ring, and makes it ready to queue num_trbs. 3240 * expand ring if it start to be full. 3241 */ 3242 static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring, 3243 u32 ep_state, unsigned int num_trbs, gfp_t mem_flags) 3244 { 3245 unsigned int new_segs = 0; 3246 3247 /* Make sure the endpoint has been added to xHC schedule */ 3248 switch (ep_state) { 3249 case EP_STATE_DISABLED: 3250 /* 3251 * USB core changed config/interfaces without notifying us, 3252 * or hardware is reporting the wrong state. 3253 */ 3254 xhci_warn(xhci, "WARN urb submitted to disabled ep\n"); 3255 return -ENOENT; 3256 case EP_STATE_ERROR: 3257 xhci_warn(xhci, "WARN waiting for error on ep to be cleared\n"); 3258 /* FIXME event handling code for error needs to clear it */ 3259 /* XXX not sure if this should be -ENOENT or not */ 3260 return -EINVAL; 3261 case EP_STATE_HALTED: 3262 xhci_dbg(xhci, "WARN halted endpoint, queueing URB anyway.\n"); 3263 break; 3264 case EP_STATE_STOPPED: 3265 case EP_STATE_RUNNING: 3266 break; 3267 default: 3268 xhci_err(xhci, "ERROR unknown endpoint state for ep\n"); 3269 /* 3270 * FIXME issue Configure Endpoint command to try to get the HC 3271 * back into a known state. 3272 */ 3273 return -EINVAL; 3274 } 3275 3276 if (ep_ring != xhci->cmd_ring) { 3277 new_segs = xhci_ring_expansion_needed(xhci, ep_ring, num_trbs); 3278 } else if (xhci_num_trbs_free(ep_ring) <= num_trbs) { 3279 xhci_err(xhci, "Do not support expand command ring\n"); 3280 return -ENOMEM; 3281 } 3282 3283 if (new_segs) { 3284 xhci_dbg_trace(xhci, trace_xhci_dbg_ring_expansion, 3285 "ERROR no room on ep ring, try ring expansion"); 3286 if (xhci_ring_expansion(xhci, ep_ring, new_segs, mem_flags)) { 3287 xhci_err(xhci, "Ring expansion failed\n"); 3288 return -ENOMEM; 3289 } 3290 } 3291 3292 /* Ensure that new TRBs won't overwrite a link */ 3293 if (trb_is_link(ep_ring->enqueue)) 3294 inc_enq_past_link(xhci, ep_ring, 0); 3295 3296 if (last_trb_on_seg(ep_ring->enq_seg, ep_ring->enqueue)) { 3297 xhci_warn(xhci, "Missing link TRB at end of ring segment\n"); 3298 return -EINVAL; 3299 } 3300 3301 return 0; 3302 } 3303 3304 static int prepare_transfer(struct xhci_hcd *xhci, 3305 struct xhci_virt_device *xdev, 3306 unsigned int ep_index, 3307 unsigned int stream_id, 3308 unsigned int num_trbs, 3309 struct urb *urb, 3310 unsigned int td_index, 3311 gfp_t mem_flags) 3312 { 3313 int ret; 3314 struct urb_priv *urb_priv; 3315 struct xhci_td *td; 3316 struct xhci_ring *ep_ring; 3317 struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index); 3318 3319 ep_ring = xhci_triad_to_transfer_ring(xhci, xdev->slot_id, ep_index, 3320 stream_id); 3321 if (!ep_ring) { 3322 xhci_dbg(xhci, "Can't prepare ring for bad stream ID %u\n", 3323 stream_id); 3324 return -EINVAL; 3325 } 3326 3327 ret = prepare_ring(xhci, ep_ring, GET_EP_CTX_STATE(ep_ctx), 3328 num_trbs, mem_flags); 3329 if (ret) 3330 return ret; 3331 3332 urb_priv = urb->hcpriv; 3333 td = &urb_priv->td[td_index]; 3334 3335 INIT_LIST_HEAD(&td->td_list); 3336 INIT_LIST_HEAD(&td->cancelled_td_list); 3337 3338 if (td_index == 0) { 3339 ret = usb_hcd_link_urb_to_ep(bus_to_hcd(urb->dev->bus), urb); 3340 if (unlikely(ret)) 3341 return ret; 3342 } 3343 3344 td->urb = urb; 3345 /* Add this TD to the tail of the endpoint ring's TD list */ 3346 list_add_tail(&td->td_list, &ep_ring->td_list); 3347 td->start_seg = ep_ring->enq_seg; 3348 td->start_trb = ep_ring->enqueue; 3349 3350 return 0; 3351 } 3352 3353 unsigned int count_trbs(u64 addr, u64 len) 3354 { 3355 unsigned int num_trbs; 3356 3357 num_trbs = DIV_ROUND_UP(len + (addr & (TRB_MAX_BUFF_SIZE - 1)), 3358 TRB_MAX_BUFF_SIZE); 3359 if (num_trbs == 0) 3360 num_trbs++; 3361 3362 return num_trbs; 3363 } 3364 3365 static inline unsigned int count_trbs_needed(struct urb *urb) 3366 { 3367 return count_trbs(urb->transfer_dma, urb->transfer_buffer_length); 3368 } 3369 3370 static unsigned int count_sg_trbs_needed(struct urb *urb) 3371 { 3372 struct scatterlist *sg; 3373 unsigned int i, len, full_len, num_trbs = 0; 3374 3375 full_len = urb->transfer_buffer_length; 3376 3377 for_each_sg(urb->sg, sg, urb->num_mapped_sgs, i) { 3378 len = sg_dma_len(sg); 3379 num_trbs += count_trbs(sg_dma_address(sg), len); 3380 len = min_t(unsigned int, len, full_len); 3381 full_len -= len; 3382 if (full_len == 0) 3383 break; 3384 } 3385 3386 return num_trbs; 3387 } 3388 3389 static unsigned int count_isoc_trbs_needed(struct urb *urb, int i) 3390 { 3391 u64 addr, len; 3392 3393 addr = (u64) (urb->transfer_dma + urb->iso_frame_desc[i].offset); 3394 len = urb->iso_frame_desc[i].length; 3395 3396 return count_trbs(addr, len); 3397 } 3398 3399 static void check_trb_math(struct urb *urb, int running_total) 3400 { 3401 if (unlikely(running_total != urb->transfer_buffer_length)) 3402 dev_err(&urb->dev->dev, "%s - ep %#x - Miscalculated tx length, " 3403 "queued %#x (%d), asked for %#x (%d)\n", 3404 __func__, 3405 urb->ep->desc.bEndpointAddress, 3406 running_total, running_total, 3407 urb->transfer_buffer_length, 3408 urb->transfer_buffer_length); 3409 } 3410 3411 static void giveback_first_trb(struct xhci_hcd *xhci, int slot_id, 3412 unsigned int ep_index, unsigned int stream_id, int start_cycle, 3413 struct xhci_generic_trb *start_trb) 3414 { 3415 /* 3416 * Pass all the TRBs to the hardware at once and make sure this write 3417 * isn't reordered. 3418 */ 3419 wmb(); 3420 if (start_cycle) 3421 start_trb->field[3] |= cpu_to_le32(start_cycle); 3422 else 3423 start_trb->field[3] &= cpu_to_le32(~TRB_CYCLE); 3424 xhci_ring_ep_doorbell(xhci, slot_id, ep_index, stream_id); 3425 } 3426 3427 static void check_interval(struct urb *urb, struct xhci_ep_ctx *ep_ctx) 3428 { 3429 int xhci_interval; 3430 int ep_interval; 3431 3432 xhci_interval = EP_INTERVAL_TO_UFRAMES(le32_to_cpu(ep_ctx->ep_info)); 3433 ep_interval = urb->interval; 3434 3435 /* Convert to microframes */ 3436 if (urb->dev->speed == USB_SPEED_LOW || 3437 urb->dev->speed == USB_SPEED_FULL) 3438 ep_interval *= 8; 3439 3440 /* FIXME change this to a warning and a suggestion to use the new API 3441 * to set the polling interval (once the API is added). 3442 */ 3443 if (xhci_interval != ep_interval) { 3444 dev_dbg_ratelimited(&urb->dev->dev, 3445 "Driver uses different interval (%d microframe%s) than xHCI (%d microframe%s)\n", 3446 ep_interval, str_plural(ep_interval), 3447 xhci_interval, str_plural(xhci_interval)); 3448 urb->interval = xhci_interval; 3449 /* Convert back to frames for LS/FS devices */ 3450 if (urb->dev->speed == USB_SPEED_LOW || 3451 urb->dev->speed == USB_SPEED_FULL) 3452 urb->interval /= 8; 3453 } 3454 } 3455 3456 /* 3457 * xHCI uses normal TRBs for both bulk and interrupt. When the interrupt 3458 * endpoint is to be serviced, the xHC will consume (at most) one TD. A TD 3459 * (comprised of sg list entries) can take several service intervals to 3460 * transmit. 3461 */ 3462 int xhci_queue_intr_tx(struct xhci_hcd *xhci, gfp_t mem_flags, 3463 struct urb *urb, int slot_id, unsigned int ep_index) 3464 { 3465 struct xhci_ep_ctx *ep_ctx; 3466 3467 ep_ctx = xhci_get_ep_ctx(xhci, xhci->devs[slot_id]->out_ctx, ep_index); 3468 check_interval(urb, ep_ctx); 3469 3470 return xhci_queue_bulk_tx(xhci, mem_flags, urb, slot_id, ep_index); 3471 } 3472 3473 /* 3474 * For xHCI 1.0 host controllers, TD size is the number of max packet sized 3475 * packets remaining in the TD (*not* including this TRB). 3476 * 3477 * Total TD packet count = total_packet_count = 3478 * DIV_ROUND_UP(TD size in bytes / wMaxPacketSize) 3479 * 3480 * Packets transferred up to and including this TRB = packets_transferred = 3481 * rounddown(total bytes transferred including this TRB / wMaxPacketSize) 3482 * 3483 * TD size = total_packet_count - packets_transferred 3484 * 3485 * For xHCI 0.96 and older, TD size field should be the remaining bytes 3486 * including this TRB, right shifted by 10 3487 * 3488 * For all hosts it must fit in bits 21:17, so it can't be bigger than 31. 3489 * This is taken care of in the TRB_TD_SIZE() macro 3490 * 3491 * The last TRB in a TD must have the TD size set to zero. 3492 */ 3493 static u32 xhci_td_remainder(struct xhci_hcd *xhci, int transferred, 3494 int trb_buff_len, unsigned int td_total_len, 3495 struct urb *urb, bool more_trbs_coming) 3496 { 3497 u32 maxp, total_packet_count; 3498 3499 /* MTK xHCI 0.96 contains some features from 1.0 */ 3500 if (xhci->hci_version < 0x100 && !(xhci->quirks & XHCI_MTK_HOST)) 3501 return ((td_total_len - transferred) >> 10); 3502 3503 /* One TRB with a zero-length data packet. */ 3504 if (!more_trbs_coming || (transferred == 0 && trb_buff_len == 0) || 3505 trb_buff_len == td_total_len) 3506 return 0; 3507 3508 /* for MTK xHCI 0.96, TD size include this TRB, but not in 1.x */ 3509 if ((xhci->quirks & XHCI_MTK_HOST) && (xhci->hci_version < 0x100)) 3510 trb_buff_len = 0; 3511 3512 maxp = usb_endpoint_maxp(&urb->ep->desc); 3513 total_packet_count = DIV_ROUND_UP(td_total_len, maxp); 3514 3515 /* Queueing functions don't count the current TRB into transferred */ 3516 return (total_packet_count - ((transferred + trb_buff_len) / maxp)); 3517 } 3518 3519 3520 static int xhci_align_td(struct xhci_hcd *xhci, struct urb *urb, u32 enqd_len, 3521 u32 *trb_buff_len, struct xhci_segment *seg) 3522 { 3523 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; 3524 unsigned int unalign; 3525 unsigned int max_pkt; 3526 u32 new_buff_len; 3527 size_t len; 3528 3529 max_pkt = usb_endpoint_maxp(&urb->ep->desc); 3530 unalign = (enqd_len + *trb_buff_len) % max_pkt; 3531 3532 /* we got lucky, last normal TRB data on segment is packet aligned */ 3533 if (unalign == 0) 3534 return 0; 3535 3536 xhci_dbg(xhci, "Unaligned %d bytes, buff len %d\n", 3537 unalign, *trb_buff_len); 3538 3539 /* is the last nornal TRB alignable by splitting it */ 3540 if (*trb_buff_len > unalign) { 3541 *trb_buff_len -= unalign; 3542 xhci_dbg(xhci, "split align, new buff len %d\n", *trb_buff_len); 3543 return 0; 3544 } 3545 3546 /* 3547 * We want enqd_len + trb_buff_len to sum up to a number aligned to 3548 * number which is divisible by the endpoint's wMaxPacketSize. IOW: 3549 * (size of currently enqueued TRBs + remainder) % wMaxPacketSize == 0. 3550 */ 3551 new_buff_len = max_pkt - (enqd_len % max_pkt); 3552 3553 if (new_buff_len > (urb->transfer_buffer_length - enqd_len)) 3554 new_buff_len = (urb->transfer_buffer_length - enqd_len); 3555 3556 /* create a max max_pkt sized bounce buffer pointed to by last trb */ 3557 if (usb_urb_dir_out(urb)) { 3558 if (urb->num_sgs) { 3559 len = sg_pcopy_to_buffer(urb->sg, urb->num_sgs, 3560 seg->bounce_buf, new_buff_len, enqd_len); 3561 if (len != new_buff_len) 3562 xhci_warn(xhci, "WARN Wrong bounce buffer write length: %zu != %d\n", 3563 len, new_buff_len); 3564 } else { 3565 memcpy(seg->bounce_buf, urb->transfer_buffer + enqd_len, new_buff_len); 3566 } 3567 3568 seg->bounce_dma = dma_map_single(dev, seg->bounce_buf, 3569 max_pkt, DMA_TO_DEVICE); 3570 } else { 3571 seg->bounce_dma = dma_map_single(dev, seg->bounce_buf, 3572 max_pkt, DMA_FROM_DEVICE); 3573 } 3574 3575 if (dma_mapping_error(dev, seg->bounce_dma)) { 3576 /* try without aligning. Some host controllers survive */ 3577 xhci_warn(xhci, "Failed mapping bounce buffer, not aligning\n"); 3578 return 0; 3579 } 3580 *trb_buff_len = new_buff_len; 3581 seg->bounce_len = new_buff_len; 3582 seg->bounce_offs = enqd_len; 3583 3584 xhci_dbg(xhci, "Bounce align, new buff len %d\n", *trb_buff_len); 3585 3586 return 1; 3587 } 3588 3589 /* This is very similar to what ehci-q.c qtd_fill() does */ 3590 int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags, 3591 struct urb *urb, int slot_id, unsigned int ep_index) 3592 { 3593 struct xhci_ring *ring; 3594 struct urb_priv *urb_priv; 3595 struct xhci_td *td; 3596 struct xhci_generic_trb *start_trb; 3597 struct scatterlist *sg = NULL; 3598 bool more_trbs_coming = true; 3599 bool need_zero_pkt = false; 3600 bool first_trb = true; 3601 unsigned int num_trbs; 3602 unsigned int start_cycle, num_sgs = 0; 3603 unsigned int enqd_len, block_len, trb_buff_len, full_len; 3604 int sent_len, ret; 3605 u32 field, length_field, remainder; 3606 u64 addr, send_addr; 3607 3608 ring = xhci_urb_to_transfer_ring(xhci, urb); 3609 if (!ring) 3610 return -EINVAL; 3611 3612 full_len = urb->transfer_buffer_length; 3613 /* If we have scatter/gather list, we use it. */ 3614 if (urb->num_sgs && !(urb->transfer_flags & URB_DMA_MAP_SINGLE)) { 3615 num_sgs = urb->num_mapped_sgs; 3616 sg = urb->sg; 3617 addr = (u64) sg_dma_address(sg); 3618 block_len = sg_dma_len(sg); 3619 num_trbs = count_sg_trbs_needed(urb); 3620 } else { 3621 num_trbs = count_trbs_needed(urb); 3622 addr = (u64) urb->transfer_dma; 3623 block_len = full_len; 3624 } 3625 ret = prepare_transfer(xhci, xhci->devs[slot_id], 3626 ep_index, urb->stream_id, 3627 num_trbs, urb, 0, mem_flags); 3628 if (unlikely(ret < 0)) 3629 return ret; 3630 3631 urb_priv = urb->hcpriv; 3632 3633 /* Deal with URB_ZERO_PACKET - need one more td/trb */ 3634 if (urb->transfer_flags & URB_ZERO_PACKET && urb_priv->num_tds > 1) 3635 need_zero_pkt = true; 3636 3637 td = &urb_priv->td[0]; 3638 3639 /* 3640 * Don't give the first TRB to the hardware (by toggling the cycle bit) 3641 * until we've finished creating all the other TRBs. The ring's cycle 3642 * state may change as we enqueue the other TRBs, so save it too. 3643 */ 3644 start_trb = &ring->enqueue->generic; 3645 start_cycle = ring->cycle_state; 3646 send_addr = addr; 3647 3648 /* Queue the TRBs, even if they are zero-length */ 3649 for (enqd_len = 0; first_trb || enqd_len < full_len; 3650 enqd_len += trb_buff_len) { 3651 field = TRB_TYPE(TRB_NORMAL); 3652 3653 /* TRB buffer should not cross 64KB boundaries */ 3654 trb_buff_len = TRB_BUFF_LEN_UP_TO_BOUNDARY(addr); 3655 trb_buff_len = min_t(unsigned int, trb_buff_len, block_len); 3656 3657 if (enqd_len + trb_buff_len > full_len) 3658 trb_buff_len = full_len - enqd_len; 3659 3660 /* Don't change the cycle bit of the first TRB until later */ 3661 if (first_trb) { 3662 first_trb = false; 3663 if (start_cycle == 0) 3664 field |= TRB_CYCLE; 3665 } else 3666 field |= ring->cycle_state; 3667 3668 /* Chain all the TRBs together; clear the chain bit in the last 3669 * TRB to indicate it's the last TRB in the chain. 3670 */ 3671 if (enqd_len + trb_buff_len < full_len) { 3672 field |= TRB_CHAIN; 3673 if (trb_is_link(ring->enqueue + 1)) { 3674 if (xhci_align_td(xhci, urb, enqd_len, 3675 &trb_buff_len, 3676 ring->enq_seg)) { 3677 send_addr = ring->enq_seg->bounce_dma; 3678 /* assuming TD won't span 2 segs */ 3679 td->bounce_seg = ring->enq_seg; 3680 } 3681 } 3682 } 3683 if (enqd_len + trb_buff_len >= full_len) { 3684 field &= ~TRB_CHAIN; 3685 field |= TRB_IOC; 3686 more_trbs_coming = false; 3687 td->end_trb = ring->enqueue; 3688 td->end_seg = ring->enq_seg; 3689 if (xhci_urb_suitable_for_idt(urb)) { 3690 memcpy(&send_addr, urb->transfer_buffer, 3691 trb_buff_len); 3692 le64_to_cpus(&send_addr); 3693 field |= TRB_IDT; 3694 } 3695 } 3696 3697 /* Only set interrupt on short packet for IN endpoints */ 3698 if (usb_urb_dir_in(urb)) 3699 field |= TRB_ISP; 3700 3701 /* Set the TRB length, TD size, and interrupter fields. */ 3702 remainder = xhci_td_remainder(xhci, enqd_len, trb_buff_len, 3703 full_len, urb, more_trbs_coming); 3704 3705 length_field = TRB_LEN(trb_buff_len) | 3706 TRB_TD_SIZE(remainder) | 3707 TRB_INTR_TARGET(0); 3708 3709 queue_trb(xhci, ring, more_trbs_coming | need_zero_pkt, 3710 lower_32_bits(send_addr), 3711 upper_32_bits(send_addr), 3712 length_field, 3713 field); 3714 addr += trb_buff_len; 3715 sent_len = trb_buff_len; 3716 3717 while (sg && sent_len >= block_len) { 3718 /* New sg entry */ 3719 --num_sgs; 3720 sent_len -= block_len; 3721 sg = sg_next(sg); 3722 if (num_sgs != 0 && sg) { 3723 block_len = sg_dma_len(sg); 3724 addr = (u64) sg_dma_address(sg); 3725 addr += sent_len; 3726 } 3727 } 3728 block_len -= sent_len; 3729 send_addr = addr; 3730 } 3731 3732 if (need_zero_pkt) { 3733 ret = prepare_transfer(xhci, xhci->devs[slot_id], 3734 ep_index, urb->stream_id, 3735 1, urb, 1, mem_flags); 3736 urb_priv->td[1].end_trb = ring->enqueue; 3737 urb_priv->td[1].end_seg = ring->enq_seg; 3738 field = TRB_TYPE(TRB_NORMAL) | ring->cycle_state | TRB_IOC; 3739 queue_trb(xhci, ring, 0, 0, 0, TRB_INTR_TARGET(0), field); 3740 } 3741 3742 check_trb_math(urb, enqd_len); 3743 giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id, 3744 start_cycle, start_trb); 3745 return 0; 3746 } 3747 3748 /* Caller must have locked xhci->lock */ 3749 int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags, 3750 struct urb *urb, int slot_id, unsigned int ep_index) 3751 { 3752 struct xhci_ring *ep_ring; 3753 int num_trbs; 3754 int ret; 3755 struct usb_ctrlrequest *setup; 3756 struct xhci_generic_trb *start_trb; 3757 int start_cycle; 3758 u32 field; 3759 struct urb_priv *urb_priv; 3760 struct xhci_td *td; 3761 3762 ep_ring = xhci_urb_to_transfer_ring(xhci, urb); 3763 if (!ep_ring) 3764 return -EINVAL; 3765 3766 /* 3767 * Need to copy setup packet into setup TRB, so we can't use the setup 3768 * DMA address. 3769 */ 3770 if (!urb->setup_packet) 3771 return -EINVAL; 3772 3773 if ((xhci->quirks & XHCI_ETRON_HOST) && 3774 urb->dev->speed >= USB_SPEED_SUPER) { 3775 /* 3776 * If next available TRB is the Link TRB in the ring segment then 3777 * enqueue a No Op TRB, this can prevent the Setup and Data Stage 3778 * TRB to be breaked by the Link TRB. 3779 */ 3780 if (last_trb_on_seg(ep_ring->enq_seg, ep_ring->enqueue + 1)) { 3781 field = TRB_TYPE(TRB_TR_NOOP) | ep_ring->cycle_state; 3782 queue_trb(xhci, ep_ring, false, 0, 0, 3783 TRB_INTR_TARGET(0), field); 3784 } 3785 } 3786 3787 /* 1 TRB for setup, 1 for status */ 3788 num_trbs = 2; 3789 /* 3790 * Don't need to check if we need additional event data and normal TRBs, 3791 * since data in control transfers will never get bigger than 16MB 3792 * XXX: can we get a buffer that crosses 64KB boundaries? 3793 */ 3794 if (urb->transfer_buffer_length > 0) 3795 num_trbs++; 3796 ret = prepare_transfer(xhci, xhci->devs[slot_id], 3797 ep_index, urb->stream_id, 3798 num_trbs, urb, 0, mem_flags); 3799 if (ret < 0) 3800 return ret; 3801 3802 urb_priv = urb->hcpriv; 3803 td = &urb_priv->td[0]; 3804 3805 /* 3806 * Don't give the first TRB to the hardware (by toggling the cycle bit) 3807 * until we've finished creating all the other TRBs. The ring's cycle 3808 * state may change as we enqueue the other TRBs, so save it too. 3809 */ 3810 start_trb = &ep_ring->enqueue->generic; 3811 start_cycle = ep_ring->cycle_state; 3812 3813 /* Queue setup TRB - see section 6.4.1.2.1 */ 3814 /* FIXME better way to translate setup_packet into two u32 fields? */ 3815 setup = (struct usb_ctrlrequest *) urb->setup_packet; 3816 field = 0; 3817 field |= TRB_IDT | TRB_TYPE(TRB_SETUP); 3818 if (start_cycle == 0) 3819 field |= 0x1; 3820 3821 /* xHCI 1.0/1.1 6.4.1.2.1: Transfer Type field */ 3822 if ((xhci->hci_version >= 0x100) || (xhci->quirks & XHCI_MTK_HOST)) { 3823 if (urb->transfer_buffer_length > 0) { 3824 if (setup->bRequestType & USB_DIR_IN) 3825 field |= TRB_TX_TYPE(TRB_DATA_IN); 3826 else 3827 field |= TRB_TX_TYPE(TRB_DATA_OUT); 3828 } 3829 } 3830 3831 queue_trb(xhci, ep_ring, true, 3832 setup->bRequestType | setup->bRequest << 8 | le16_to_cpu(setup->wValue) << 16, 3833 le16_to_cpu(setup->wIndex) | le16_to_cpu(setup->wLength) << 16, 3834 TRB_LEN(8) | TRB_INTR_TARGET(0), 3835 /* Immediate data in pointer */ 3836 field); 3837 3838 /* If there's data, queue data TRBs */ 3839 /* Only set interrupt on short packet for IN endpoints */ 3840 if (usb_urb_dir_in(urb)) 3841 field = TRB_ISP | TRB_TYPE(TRB_DATA); 3842 else 3843 field = TRB_TYPE(TRB_DATA); 3844 3845 if (urb->transfer_buffer_length > 0) { 3846 u32 length_field, remainder; 3847 u64 addr; 3848 3849 if (xhci_urb_suitable_for_idt(urb)) { 3850 memcpy(&addr, urb->transfer_buffer, 3851 urb->transfer_buffer_length); 3852 le64_to_cpus(&addr); 3853 field |= TRB_IDT; 3854 } else { 3855 addr = (u64) urb->transfer_dma; 3856 } 3857 3858 remainder = xhci_td_remainder(xhci, 0, 3859 urb->transfer_buffer_length, 3860 urb->transfer_buffer_length, 3861 urb, 1); 3862 length_field = TRB_LEN(urb->transfer_buffer_length) | 3863 TRB_TD_SIZE(remainder) | 3864 TRB_INTR_TARGET(0); 3865 if (setup->bRequestType & USB_DIR_IN) 3866 field |= TRB_DIR_IN; 3867 queue_trb(xhci, ep_ring, true, 3868 lower_32_bits(addr), 3869 upper_32_bits(addr), 3870 length_field, 3871 field | ep_ring->cycle_state); 3872 } 3873 3874 /* Save the DMA address of the last TRB in the TD */ 3875 td->end_trb = ep_ring->enqueue; 3876 td->end_seg = ep_ring->enq_seg; 3877 3878 /* Queue status TRB - see Table 7 and sections 4.11.2.2 and 6.4.1.2.3 */ 3879 /* If the device sent data, the status stage is an OUT transfer */ 3880 if (urb->transfer_buffer_length > 0 && setup->bRequestType & USB_DIR_IN) 3881 field = 0; 3882 else 3883 field = TRB_DIR_IN; 3884 queue_trb(xhci, ep_ring, false, 3885 0, 3886 0, 3887 TRB_INTR_TARGET(0), 3888 /* Event on completion */ 3889 field | TRB_IOC | TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state); 3890 3891 giveback_first_trb(xhci, slot_id, ep_index, 0, 3892 start_cycle, start_trb); 3893 return 0; 3894 } 3895 3896 /* 3897 * The transfer burst count field of the isochronous TRB defines the number of 3898 * bursts that are required to move all packets in this TD. Only SuperSpeed 3899 * devices can burst up to bMaxBurst number of packets per service interval. 3900 * This field is zero based, meaning a value of zero in the field means one 3901 * burst. Basically, for everything but SuperSpeed devices, this field will be 3902 * zero. Only xHCI 1.0 host controllers support this field. 3903 */ 3904 static unsigned int xhci_get_burst_count(struct xhci_hcd *xhci, 3905 struct urb *urb, unsigned int total_packet_count) 3906 { 3907 unsigned int max_burst; 3908 3909 if (xhci->hci_version < 0x100 || urb->dev->speed < USB_SPEED_SUPER) 3910 return 0; 3911 3912 max_burst = urb->ep->ss_ep_comp.bMaxBurst; 3913 return DIV_ROUND_UP(total_packet_count, max_burst + 1) - 1; 3914 } 3915 3916 /* 3917 * Returns the number of packets in the last "burst" of packets. This field is 3918 * valid for all speeds of devices. USB 2.0 devices can only do one "burst", so 3919 * the last burst packet count is equal to the total number of packets in the 3920 * TD. SuperSpeed endpoints can have up to 3 bursts. All but the last burst 3921 * must contain (bMaxBurst + 1) number of packets, but the last burst can 3922 * contain 1 to (bMaxBurst + 1) packets. 3923 */ 3924 static unsigned int xhci_get_last_burst_packet_count(struct xhci_hcd *xhci, 3925 struct urb *urb, unsigned int total_packet_count) 3926 { 3927 unsigned int max_burst; 3928 unsigned int residue; 3929 3930 if (xhci->hci_version < 0x100) 3931 return 0; 3932 3933 if (urb->dev->speed >= USB_SPEED_SUPER) { 3934 /* bMaxBurst is zero based: 0 means 1 packet per burst */ 3935 max_burst = urb->ep->ss_ep_comp.bMaxBurst; 3936 residue = total_packet_count % (max_burst + 1); 3937 /* If residue is zero, the last burst contains (max_burst + 1) 3938 * number of packets, but the TLBPC field is zero-based. 3939 */ 3940 if (residue == 0) 3941 return max_burst; 3942 return residue - 1; 3943 } 3944 if (total_packet_count == 0) 3945 return 0; 3946 return total_packet_count - 1; 3947 } 3948 3949 /* 3950 * Calculates Frame ID field of the isochronous TRB identifies the 3951 * target frame that the Interval associated with this Isochronous 3952 * Transfer Descriptor will start on. Refer to 4.11.2.5 in 1.1 spec. 3953 * 3954 * Returns actual frame id on success, negative value on error. 3955 */ 3956 static int xhci_get_isoc_frame_id(struct xhci_hcd *xhci, 3957 struct urb *urb, int index) 3958 { 3959 int start_frame, ist, ret = 0; 3960 int start_frame_id, end_frame_id, current_frame_id; 3961 3962 if (urb->dev->speed == USB_SPEED_LOW || 3963 urb->dev->speed == USB_SPEED_FULL) 3964 start_frame = urb->start_frame + index * urb->interval; 3965 else 3966 start_frame = (urb->start_frame + index * urb->interval) >> 3; 3967 3968 /* Isochronous Scheduling Threshold (IST, bits 0~3 in HCSPARAMS2): 3969 * 3970 * If bit [3] of IST is cleared to '0', software can add a TRB no 3971 * later than IST[2:0] Microframes before that TRB is scheduled to 3972 * be executed. 3973 * If bit [3] of IST is set to '1', software can add a TRB no later 3974 * than IST[2:0] Frames before that TRB is scheduled to be executed. 3975 */ 3976 ist = HCS_IST(xhci->hcs_params2) & 0x7; 3977 if (HCS_IST(xhci->hcs_params2) & (1 << 3)) 3978 ist <<= 3; 3979 3980 /* Software shall not schedule an Isoch TD with a Frame ID value that 3981 * is less than the Start Frame ID or greater than the End Frame ID, 3982 * where: 3983 * 3984 * End Frame ID = (Current MFINDEX register value + 895 ms.) MOD 2048 3985 * Start Frame ID = (Current MFINDEX register value + IST + 1) MOD 2048 3986 * 3987 * Both the End Frame ID and Start Frame ID values are calculated 3988 * in microframes. When software determines the valid Frame ID value; 3989 * The End Frame ID value should be rounded down to the nearest Frame 3990 * boundary, and the Start Frame ID value should be rounded up to the 3991 * nearest Frame boundary. 3992 */ 3993 current_frame_id = readl(&xhci->run_regs->microframe_index); 3994 start_frame_id = roundup(current_frame_id + ist + 1, 8); 3995 end_frame_id = rounddown(current_frame_id + 895 * 8, 8); 3996 3997 start_frame &= 0x7ff; 3998 start_frame_id = (start_frame_id >> 3) & 0x7ff; 3999 end_frame_id = (end_frame_id >> 3) & 0x7ff; 4000 4001 if (start_frame_id < end_frame_id) { 4002 if (start_frame > end_frame_id || 4003 start_frame < start_frame_id) 4004 ret = -EINVAL; 4005 } else if (start_frame_id > end_frame_id) { 4006 if ((start_frame > end_frame_id && 4007 start_frame < start_frame_id)) 4008 ret = -EINVAL; 4009 } else { 4010 ret = -EINVAL; 4011 } 4012 4013 if (index == 0) { 4014 if (ret == -EINVAL || start_frame == start_frame_id) { 4015 start_frame = start_frame_id + 1; 4016 if (urb->dev->speed == USB_SPEED_LOW || 4017 urb->dev->speed == USB_SPEED_FULL) 4018 urb->start_frame = start_frame; 4019 else 4020 urb->start_frame = start_frame << 3; 4021 ret = 0; 4022 } 4023 } 4024 4025 if (ret) { 4026 xhci_warn(xhci, "Frame ID %d (reg %d, index %d) beyond range (%d, %d)\n", 4027 start_frame, current_frame_id, index, 4028 start_frame_id, end_frame_id); 4029 xhci_warn(xhci, "Ignore frame ID field, use SIA bit instead\n"); 4030 return ret; 4031 } 4032 4033 return start_frame; 4034 } 4035 4036 /* Check if we should generate event interrupt for a TD in an isoc URB */ 4037 static bool trb_block_event_intr(struct xhci_hcd *xhci, int num_tds, int i, 4038 struct xhci_interrupter *ir) 4039 { 4040 if (xhci->hci_version < 0x100) 4041 return false; 4042 /* always generate an event interrupt for the last TD */ 4043 if (i == num_tds - 1) 4044 return false; 4045 /* 4046 * If AVOID_BEI is set the host handles full event rings poorly, 4047 * generate an event at least every 8th TD to clear the event ring 4048 */ 4049 if (i && ir->isoc_bei_interval && xhci->quirks & XHCI_AVOID_BEI) 4050 return !!(i % ir->isoc_bei_interval); 4051 4052 return true; 4053 } 4054 4055 /* This is for isoc transfer */ 4056 static int xhci_queue_isoc_tx(struct xhci_hcd *xhci, gfp_t mem_flags, 4057 struct urb *urb, int slot_id, unsigned int ep_index) 4058 { 4059 struct xhci_interrupter *ir; 4060 struct xhci_ring *ep_ring; 4061 struct urb_priv *urb_priv; 4062 struct xhci_td *td; 4063 int num_tds, trbs_per_td; 4064 struct xhci_generic_trb *start_trb; 4065 bool first_trb; 4066 int start_cycle; 4067 u32 field, length_field; 4068 int running_total, trb_buff_len, td_len, td_remain_len, ret; 4069 u64 start_addr, addr; 4070 int i, j; 4071 bool more_trbs_coming; 4072 struct xhci_virt_ep *xep; 4073 int frame_id; 4074 4075 xep = &xhci->devs[slot_id]->eps[ep_index]; 4076 ep_ring = xhci->devs[slot_id]->eps[ep_index].ring; 4077 ir = xhci->interrupters[0]; 4078 4079 num_tds = urb->number_of_packets; 4080 if (num_tds < 1) { 4081 xhci_dbg(xhci, "Isoc URB with zero packets?\n"); 4082 return -EINVAL; 4083 } 4084 start_addr = (u64) urb->transfer_dma; 4085 start_trb = &ep_ring->enqueue->generic; 4086 start_cycle = ep_ring->cycle_state; 4087 4088 urb_priv = urb->hcpriv; 4089 /* Queue the TRBs for each TD, even if they are zero-length */ 4090 for (i = 0; i < num_tds; i++) { 4091 unsigned int total_pkt_count, max_pkt; 4092 unsigned int burst_count, last_burst_pkt_count; 4093 u32 sia_frame_id; 4094 4095 first_trb = true; 4096 running_total = 0; 4097 addr = start_addr + urb->iso_frame_desc[i].offset; 4098 td_len = urb->iso_frame_desc[i].length; 4099 td_remain_len = td_len; 4100 max_pkt = usb_endpoint_maxp(&urb->ep->desc); 4101 total_pkt_count = DIV_ROUND_UP(td_len, max_pkt); 4102 4103 /* A zero-length transfer still involves at least one packet. */ 4104 if (total_pkt_count == 0) 4105 total_pkt_count++; 4106 burst_count = xhci_get_burst_count(xhci, urb, total_pkt_count); 4107 last_burst_pkt_count = xhci_get_last_burst_packet_count(xhci, 4108 urb, total_pkt_count); 4109 4110 trbs_per_td = count_isoc_trbs_needed(urb, i); 4111 4112 ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index, 4113 urb->stream_id, trbs_per_td, urb, i, mem_flags); 4114 if (ret < 0) { 4115 if (i == 0) 4116 return ret; 4117 goto cleanup; 4118 } 4119 td = &urb_priv->td[i]; 4120 /* use SIA as default, if frame id is used overwrite it */ 4121 sia_frame_id = TRB_SIA; 4122 if (!(urb->transfer_flags & URB_ISO_ASAP) && 4123 HCC_CFC(xhci->hcc_params)) { 4124 frame_id = xhci_get_isoc_frame_id(xhci, urb, i); 4125 if (frame_id >= 0) 4126 sia_frame_id = TRB_FRAME_ID(frame_id); 4127 } 4128 /* 4129 * Set isoc specific data for the first TRB in a TD. 4130 * Prevent HW from getting the TRBs by keeping the cycle state 4131 * inverted in the first TDs isoc TRB. 4132 */ 4133 field = TRB_TYPE(TRB_ISOC) | 4134 TRB_TLBPC(last_burst_pkt_count) | 4135 sia_frame_id | 4136 (i ? ep_ring->cycle_state : !start_cycle); 4137 4138 /* xhci 1.1 with ETE uses TD_Size field for TBC, old is Rsvdz */ 4139 if (!xep->use_extended_tbc) 4140 field |= TRB_TBC(burst_count); 4141 4142 /* fill the rest of the TRB fields, and remaining normal TRBs */ 4143 for (j = 0; j < trbs_per_td; j++) { 4144 u32 remainder = 0; 4145 4146 /* only first TRB is isoc, overwrite otherwise */ 4147 if (!first_trb) 4148 field = TRB_TYPE(TRB_NORMAL) | 4149 ep_ring->cycle_state; 4150 4151 /* Only set interrupt on short packet for IN EPs */ 4152 if (usb_urb_dir_in(urb)) 4153 field |= TRB_ISP; 4154 4155 /* Set the chain bit for all except the last TRB */ 4156 if (j < trbs_per_td - 1) { 4157 more_trbs_coming = true; 4158 field |= TRB_CHAIN; 4159 } else { 4160 more_trbs_coming = false; 4161 td->end_trb = ep_ring->enqueue; 4162 td->end_seg = ep_ring->enq_seg; 4163 field |= TRB_IOC; 4164 if (trb_block_event_intr(xhci, num_tds, i, ir)) 4165 field |= TRB_BEI; 4166 } 4167 /* Calculate TRB length */ 4168 trb_buff_len = TRB_BUFF_LEN_UP_TO_BOUNDARY(addr); 4169 if (trb_buff_len > td_remain_len) 4170 trb_buff_len = td_remain_len; 4171 4172 /* Set the TRB length, TD size, & interrupter fields. */ 4173 remainder = xhci_td_remainder(xhci, running_total, 4174 trb_buff_len, td_len, 4175 urb, more_trbs_coming); 4176 4177 length_field = TRB_LEN(trb_buff_len) | 4178 TRB_INTR_TARGET(0); 4179 4180 /* xhci 1.1 with ETE uses TD Size field for TBC */ 4181 if (first_trb && xep->use_extended_tbc) 4182 length_field |= TRB_TD_SIZE_TBC(burst_count); 4183 else 4184 length_field |= TRB_TD_SIZE(remainder); 4185 first_trb = false; 4186 4187 queue_trb(xhci, ep_ring, more_trbs_coming, 4188 lower_32_bits(addr), 4189 upper_32_bits(addr), 4190 length_field, 4191 field); 4192 running_total += trb_buff_len; 4193 4194 addr += trb_buff_len; 4195 td_remain_len -= trb_buff_len; 4196 } 4197 4198 /* Check TD length */ 4199 if (running_total != td_len) { 4200 xhci_err(xhci, "ISOC TD length unmatch\n"); 4201 ret = -EINVAL; 4202 goto cleanup; 4203 } 4204 } 4205 4206 /* store the next frame id */ 4207 if (HCC_CFC(xhci->hcc_params)) 4208 xep->next_frame_id = urb->start_frame + num_tds * urb->interval; 4209 4210 if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs == 0) { 4211 if (xhci->quirks & XHCI_AMD_PLL_FIX) 4212 usb_amd_quirk_pll_disable(); 4213 } 4214 xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs++; 4215 4216 giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id, 4217 start_cycle, start_trb); 4218 return 0; 4219 cleanup: 4220 /* Clean up a partially enqueued isoc transfer. */ 4221 4222 for (i--; i >= 0; i--) 4223 list_del_init(&urb_priv->td[i].td_list); 4224 4225 /* Use the first TD as a temporary variable to turn the TDs we've queued 4226 * into No-ops with a software-owned cycle bit. That way the hardware 4227 * won't accidentally start executing bogus TDs when we partially 4228 * overwrite them. td->start_trb and td->start_seg are already set. 4229 */ 4230 urb_priv->td[0].end_trb = ep_ring->enqueue; 4231 /* Every TRB except the first & last will have its cycle bit flipped. */ 4232 td_to_noop(&urb_priv->td[0], true); 4233 4234 /* Reset the ring enqueue back to the first TRB and its cycle bit. */ 4235 ep_ring->enqueue = urb_priv->td[0].start_trb; 4236 ep_ring->enq_seg = urb_priv->td[0].start_seg; 4237 ep_ring->cycle_state = start_cycle; 4238 usb_hcd_unlink_urb_from_ep(bus_to_hcd(urb->dev->bus), urb); 4239 return ret; 4240 } 4241 4242 /* 4243 * Check transfer ring to guarantee there is enough room for the urb. 4244 * Update ISO URB start_frame and interval. 4245 * Update interval as xhci_queue_intr_tx does. Use xhci frame_index to 4246 * update urb->start_frame if URB_ISO_ASAP is set in transfer_flags or 4247 * Contiguous Frame ID is not supported by HC. 4248 */ 4249 int xhci_queue_isoc_tx_prepare(struct xhci_hcd *xhci, gfp_t mem_flags, 4250 struct urb *urb, int slot_id, unsigned int ep_index) 4251 { 4252 struct xhci_virt_device *xdev; 4253 struct xhci_ring *ep_ring; 4254 struct xhci_ep_ctx *ep_ctx; 4255 int start_frame; 4256 int num_tds, num_trbs, i; 4257 int ret; 4258 struct xhci_virt_ep *xep; 4259 int ist; 4260 4261 xdev = xhci->devs[slot_id]; 4262 xep = &xhci->devs[slot_id]->eps[ep_index]; 4263 ep_ring = xdev->eps[ep_index].ring; 4264 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index); 4265 4266 num_trbs = 0; 4267 num_tds = urb->number_of_packets; 4268 for (i = 0; i < num_tds; i++) 4269 num_trbs += count_isoc_trbs_needed(urb, i); 4270 4271 /* Check the ring to guarantee there is enough room for the whole urb. 4272 * Do not insert any td of the urb to the ring if the check failed. 4273 */ 4274 ret = prepare_ring(xhci, ep_ring, GET_EP_CTX_STATE(ep_ctx), 4275 num_trbs, mem_flags); 4276 if (ret) 4277 return ret; 4278 4279 /* 4280 * Check interval value. This should be done before we start to 4281 * calculate the start frame value. 4282 */ 4283 check_interval(urb, ep_ctx); 4284 4285 /* Calculate the start frame and put it in urb->start_frame. */ 4286 if (HCC_CFC(xhci->hcc_params) && !list_empty(&ep_ring->td_list)) { 4287 if (GET_EP_CTX_STATE(ep_ctx) == EP_STATE_RUNNING) { 4288 urb->start_frame = xep->next_frame_id; 4289 goto skip_start_over; 4290 } 4291 } 4292 4293 start_frame = readl(&xhci->run_regs->microframe_index); 4294 start_frame &= 0x3fff; 4295 /* 4296 * Round up to the next frame and consider the time before trb really 4297 * gets scheduled by hardare. 4298 */ 4299 ist = HCS_IST(xhci->hcs_params2) & 0x7; 4300 if (HCS_IST(xhci->hcs_params2) & (1 << 3)) 4301 ist <<= 3; 4302 start_frame += ist + XHCI_CFC_DELAY; 4303 start_frame = roundup(start_frame, 8); 4304 4305 /* 4306 * Round up to the next ESIT (Endpoint Service Interval Time) if ESIT 4307 * is greate than 8 microframes. 4308 */ 4309 if (urb->dev->speed == USB_SPEED_LOW || 4310 urb->dev->speed == USB_SPEED_FULL) { 4311 start_frame = roundup(start_frame, urb->interval << 3); 4312 urb->start_frame = start_frame >> 3; 4313 } else { 4314 start_frame = roundup(start_frame, urb->interval); 4315 urb->start_frame = start_frame; 4316 } 4317 4318 skip_start_over: 4319 4320 return xhci_queue_isoc_tx(xhci, mem_flags, urb, slot_id, ep_index); 4321 } 4322 4323 /**** Command Ring Operations ****/ 4324 4325 /* Generic function for queueing a command TRB on the command ring. 4326 * Check to make sure there's room on the command ring for one command TRB. 4327 * Also check that there's room reserved for commands that must not fail. 4328 * If this is a command that must not fail, meaning command_must_succeed = TRUE, 4329 * then only check for the number of reserved spots. 4330 * Don't decrement xhci->cmd_ring_reserved_trbs after we've queued the TRB 4331 * because the command event handler may want to resubmit a failed command. 4332 */ 4333 static int queue_command(struct xhci_hcd *xhci, struct xhci_command *cmd, 4334 u32 field1, u32 field2, 4335 u32 field3, u32 field4, bool command_must_succeed) 4336 { 4337 int reserved_trbs = xhci->cmd_ring_reserved_trbs; 4338 int ret; 4339 4340 if ((xhci->xhc_state & XHCI_STATE_DYING) || 4341 (xhci->xhc_state & XHCI_STATE_HALTED)) { 4342 xhci_dbg(xhci, "xHCI dying or halted, can't queue_command\n"); 4343 return -ESHUTDOWN; 4344 } 4345 4346 if (!command_must_succeed) 4347 reserved_trbs++; 4348 4349 ret = prepare_ring(xhci, xhci->cmd_ring, EP_STATE_RUNNING, 4350 reserved_trbs, GFP_ATOMIC); 4351 if (ret < 0) { 4352 xhci_err(xhci, "ERR: No room for command on command ring\n"); 4353 if (command_must_succeed) 4354 xhci_err(xhci, "ERR: Reserved TRB counting for " 4355 "unfailable commands failed.\n"); 4356 return ret; 4357 } 4358 4359 cmd->command_trb = xhci->cmd_ring->enqueue; 4360 4361 /* if there are no other commands queued we start the timeout timer */ 4362 if (list_empty(&xhci->cmd_list)) { 4363 xhci->current_cmd = cmd; 4364 xhci_mod_cmd_timer(xhci); 4365 } 4366 4367 list_add_tail(&cmd->cmd_list, &xhci->cmd_list); 4368 4369 queue_trb(xhci, xhci->cmd_ring, false, field1, field2, field3, 4370 field4 | xhci->cmd_ring->cycle_state); 4371 return 0; 4372 } 4373 4374 /* Queue a slot enable or disable request on the command ring */ 4375 int xhci_queue_slot_control(struct xhci_hcd *xhci, struct xhci_command *cmd, 4376 u32 trb_type, u32 slot_id) 4377 { 4378 return queue_command(xhci, cmd, 0, 0, 0, 4379 TRB_TYPE(trb_type) | SLOT_ID_FOR_TRB(slot_id), false); 4380 } 4381 4382 /* Queue an address device command TRB */ 4383 int xhci_queue_address_device(struct xhci_hcd *xhci, struct xhci_command *cmd, 4384 dma_addr_t in_ctx_ptr, u32 slot_id, enum xhci_setup_dev setup) 4385 { 4386 return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr), 4387 upper_32_bits(in_ctx_ptr), 0, 4388 TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id) 4389 | (setup == SETUP_CONTEXT_ONLY ? TRB_BSR : 0), false); 4390 } 4391 4392 int xhci_queue_vendor_command(struct xhci_hcd *xhci, struct xhci_command *cmd, 4393 u32 field1, u32 field2, u32 field3, u32 field4) 4394 { 4395 return queue_command(xhci, cmd, field1, field2, field3, field4, false); 4396 } 4397 4398 /* Queue a reset device command TRB */ 4399 int xhci_queue_reset_device(struct xhci_hcd *xhci, struct xhci_command *cmd, 4400 u32 slot_id) 4401 { 4402 return queue_command(xhci, cmd, 0, 0, 0, 4403 TRB_TYPE(TRB_RESET_DEV) | SLOT_ID_FOR_TRB(slot_id), 4404 false); 4405 } 4406 4407 /* Queue a configure endpoint command TRB */ 4408 int xhci_queue_configure_endpoint(struct xhci_hcd *xhci, 4409 struct xhci_command *cmd, dma_addr_t in_ctx_ptr, 4410 u32 slot_id, bool command_must_succeed) 4411 { 4412 return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr), 4413 upper_32_bits(in_ctx_ptr), 0, 4414 TRB_TYPE(TRB_CONFIG_EP) | SLOT_ID_FOR_TRB(slot_id), 4415 command_must_succeed); 4416 } 4417 4418 /* Queue an evaluate context command TRB */ 4419 int xhci_queue_evaluate_context(struct xhci_hcd *xhci, struct xhci_command *cmd, 4420 dma_addr_t in_ctx_ptr, u32 slot_id, bool command_must_succeed) 4421 { 4422 return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr), 4423 upper_32_bits(in_ctx_ptr), 0, 4424 TRB_TYPE(TRB_EVAL_CONTEXT) | SLOT_ID_FOR_TRB(slot_id), 4425 command_must_succeed); 4426 } 4427 4428 /* 4429 * Suspend is set to indicate "Stop Endpoint Command" is being issued to stop 4430 * activity on an endpoint that is about to be suspended. 4431 */ 4432 int xhci_queue_stop_endpoint(struct xhci_hcd *xhci, struct xhci_command *cmd, 4433 int slot_id, unsigned int ep_index, int suspend) 4434 { 4435 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id); 4436 u32 trb_ep_index = EP_INDEX_FOR_TRB(ep_index); 4437 u32 type = TRB_TYPE(TRB_STOP_RING); 4438 u32 trb_suspend = SUSPEND_PORT_FOR_TRB(suspend); 4439 4440 return queue_command(xhci, cmd, 0, 0, 0, 4441 trb_slot_id | trb_ep_index | type | trb_suspend, false); 4442 } 4443 4444 int xhci_queue_reset_ep(struct xhci_hcd *xhci, struct xhci_command *cmd, 4445 int slot_id, unsigned int ep_index, 4446 enum xhci_ep_reset_type reset_type) 4447 { 4448 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id); 4449 u32 trb_ep_index = EP_INDEX_FOR_TRB(ep_index); 4450 u32 type = TRB_TYPE(TRB_RESET_EP); 4451 4452 if (reset_type == EP_SOFT_RESET) 4453 type |= TRB_TSP; 4454 4455 return queue_command(xhci, cmd, 0, 0, 0, 4456 trb_slot_id | trb_ep_index | type, false); 4457 } 4458