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