Lines Matching +full:generic +full:- +full:xhci
1 // SPDX-License-Identifier: GPL-2.0
3 * xHCI host controller driver
26 * until you reach a non-link TRB.
57 #include <linux/dma-mapping.h>
58 #include "xhci.h"
59 #include "xhci-trace.h"
61 static int queue_command(struct xhci_hcd *xhci, struct xhci_command *cmd,
74 if (!seg || !trb || trb < seg->trbs) in xhci_trb_virt_to_dma()
77 segment_offset = trb - seg->trbs; in xhci_trb_virt_to_dma()
80 return seg->dma + (segment_offset * sizeof(*trb)); in xhci_trb_virt_to_dma()
85 return TRB_TYPE_NOOP_LE32(trb->generic.field[3]); in trb_is_noop()
90 return TRB_TYPE_LINK_LE32(trb->link.control); in trb_is_link()
95 return trb == &seg->trbs[TRBS_PER_SEGMENT - 1]; in last_trb_on_seg()
101 return last_trb_on_seg(seg, trb) && (seg->next == ring->first_seg); in last_trb_on_ring()
106 return le32_to_cpu(trb->link.control) & LINK_TOGGLE; in link_trb_toggles_cycle()
111 struct urb_priv *urb_priv = td->urb->hcpriv; in last_td_in_urb()
113 return urb_priv->num_tds_done == urb_priv->num_tds; in last_td_in_urb()
118 struct urb_priv *urb_priv = urb->hcpriv; in inc_td_cnt()
120 urb_priv->num_tds_done++; in inc_td_cnt()
127 trb->link.control &= cpu_to_le32(~TRB_CHAIN); in trb_to_noop()
129 trb->generic.field[0] = 0; in trb_to_noop()
130 trb->generic.field[1] = 0; in trb_to_noop()
131 trb->generic.field[2] = 0; in trb_to_noop()
133 trb->generic.field[3] &= cpu_to_le32(TRB_CYCLE); in trb_to_noop()
134 trb->generic.field[3] |= cpu_to_le32(TRB_TYPE(noop_type)); in trb_to_noop()
142 static void next_trb(struct xhci_hcd *xhci, in next_trb() argument
148 *seg = (*seg)->next; in next_trb()
149 *trb = ((*seg)->trbs); in next_trb()
158 void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring) in inc_deq() argument
163 if (ring->type == TYPE_EVENT) { in inc_deq()
164 if (!last_trb_on_seg(ring->deq_seg, ring->dequeue)) { in inc_deq()
165 ring->dequeue++; in inc_deq()
168 if (last_trb_on_ring(ring, ring->deq_seg, ring->dequeue)) in inc_deq()
169 ring->cycle_state ^= 1; in inc_deq()
170 ring->deq_seg = ring->deq_seg->next; in inc_deq()
171 ring->dequeue = ring->deq_seg->trbs; in inc_deq()
176 if (!trb_is_link(ring->dequeue)) { in inc_deq()
177 if (last_trb_on_seg(ring->deq_seg, ring->dequeue)) in inc_deq()
178 xhci_warn(xhci, "Missing link TRB at end of segment\n"); in inc_deq()
180 ring->dequeue++; in inc_deq()
183 while (trb_is_link(ring->dequeue)) { in inc_deq()
184 ring->deq_seg = ring->deq_seg->next; in inc_deq()
185 ring->dequeue = ring->deq_seg->trbs; in inc_deq()
187 if (link_trb_count++ > ring->num_segs) { in inc_deq()
188 xhci_warn(xhci, "Ring is an endless link TRB loop\n"); in inc_deq()
209 * xHCI hardware can't handle the chain bit being cleared on a link TRB.
214 static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring, in inc_enq() argument
221 chain = le32_to_cpu(ring->enqueue->generic.field[3]) & TRB_CHAIN; in inc_enq()
223 if (last_trb_on_seg(ring->enq_seg, ring->enqueue)) { in inc_enq()
224 xhci_err(xhci, "Tried to move enqueue past ring segment\n"); in inc_enq()
228 next = ++(ring->enqueue); in inc_enq()
247 if (!(ring->type == TYPE_ISOC && in inc_enq()
248 (xhci->quirks & XHCI_AMD_0x96_HOST)) && in inc_enq()
249 !xhci_link_trb_quirk(xhci)) { in inc_enq()
250 next->link.control &= cpu_to_le32(~TRB_CHAIN); in inc_enq()
251 next->link.control |= cpu_to_le32(chain); in inc_enq()
255 next->link.control ^= cpu_to_le32(TRB_CYCLE); in inc_enq()
259 ring->cycle_state ^= 1; in inc_enq()
261 ring->enq_seg = ring->enq_seg->next; in inc_enq()
262 ring->enqueue = ring->enq_seg->trbs; in inc_enq()
263 next = ring->enqueue; in inc_enq()
265 if (link_trb_count++ > ring->num_segs) { in inc_enq()
266 xhci_warn(xhci, "%s: Ring link TRB loop\n", __func__); in inc_enq()
280 static unsigned int xhci_num_trbs_free(struct xhci_hcd *xhci, struct xhci_ring *ring) in xhci_num_trbs_free() argument
282 struct xhci_segment *enq_seg = ring->enq_seg; in xhci_num_trbs_free()
283 union xhci_trb *enq = ring->enqueue; in xhci_num_trbs_free()
290 enq_seg = enq_seg->next; in xhci_num_trbs_free()
291 enq = enq_seg->trbs; in xhci_num_trbs_free()
295 if (enq == ring->dequeue) in xhci_num_trbs_free()
296 return ring->num_segs * (TRBS_PER_SEGMENT - 1); in xhci_num_trbs_free()
299 if (ring->deq_seg == enq_seg && ring->dequeue >= enq) in xhci_num_trbs_free()
300 return free + (ring->dequeue - enq); in xhci_num_trbs_free()
301 last_on_seg = &enq_seg->trbs[TRBS_PER_SEGMENT - 1]; in xhci_num_trbs_free()
302 free += last_on_seg - enq; in xhci_num_trbs_free()
303 enq_seg = enq_seg->next; in xhci_num_trbs_free()
304 enq = enq_seg->trbs; in xhci_num_trbs_free()
305 } while (i++ <= ring->num_segs); in xhci_num_trbs_free()
316 static unsigned int xhci_ring_expansion_needed(struct xhci_hcd *xhci, struct xhci_ring *ring, in xhci_ring_expansion_needed() argument
324 enq_used = ring->enqueue - ring->enq_seg->trbs; in xhci_ring_expansion_needed()
327 trbs_past_seg = enq_used + num_trbs - (TRBS_PER_SEGMENT - 1); in xhci_ring_expansion_needed()
339 if (trb_is_link(ring->enqueue) && ring->enq_seg->next->trbs == ring->dequeue) in xhci_ring_expansion_needed()
342 new_segs = 1 + (trbs_past_seg / (TRBS_PER_SEGMENT - 1)); in xhci_ring_expansion_needed()
343 seg = ring->enq_seg; in xhci_ring_expansion_needed()
346 seg = seg->next; in xhci_ring_expansion_needed()
347 if (seg == ring->deq_seg) { in xhci_ring_expansion_needed()
348 xhci_dbg(xhci, "Ring expansion by %d segments needed\n", in xhci_ring_expansion_needed()
350 xhci_dbg(xhci, "Adding %d trbs moves enq %d trbs into deq seg\n", in xhci_ring_expansion_needed()
354 new_segs--; in xhci_ring_expansion_needed()
361 void xhci_ring_cmd_db(struct xhci_hcd *xhci) in xhci_ring_cmd_db() argument
363 if (!(xhci->cmd_ring_state & CMD_RING_STATE_RUNNING)) in xhci_ring_cmd_db()
366 xhci_dbg(xhci, "// Ding dong!\n"); in xhci_ring_cmd_db()
370 writel(DB_VALUE_HOST, &xhci->dba->doorbell[0]); in xhci_ring_cmd_db()
372 readl(&xhci->dba->doorbell[0]); in xhci_ring_cmd_db()
375 static bool xhci_mod_cmd_timer(struct xhci_hcd *xhci) in xhci_mod_cmd_timer() argument
377 return mod_delayed_work(system_wq, &xhci->cmd_timer, in xhci_mod_cmd_timer()
378 msecs_to_jiffies(xhci->current_cmd->timeout_ms)); in xhci_mod_cmd_timer()
381 static struct xhci_command *xhci_next_queued_cmd(struct xhci_hcd *xhci) in xhci_next_queued_cmd() argument
383 return list_first_entry_or_null(&xhci->cmd_list, struct xhci_command, in xhci_next_queued_cmd()
388 * Turn all commands on command ring with status set to "aborted" to no-op trbs.
390 * This must be called with command ring stopped and xhci->lock held.
392 static void xhci_handle_stopped_cmd_ring(struct xhci_hcd *xhci, in xhci_handle_stopped_cmd_ring() argument
397 /* Turn all aborted commands in list to no-ops, then restart */ in xhci_handle_stopped_cmd_ring()
398 list_for_each_entry(i_cmd, &xhci->cmd_list, cmd_list) { in xhci_handle_stopped_cmd_ring()
400 if (i_cmd->status != COMP_COMMAND_ABORTED) in xhci_handle_stopped_cmd_ring()
403 i_cmd->status = COMP_COMMAND_RING_STOPPED; in xhci_handle_stopped_cmd_ring()
405 xhci_dbg(xhci, "Turn aborted command %p to no-op\n", in xhci_handle_stopped_cmd_ring()
406 i_cmd->command_trb); in xhci_handle_stopped_cmd_ring()
408 trb_to_noop(i_cmd->command_trb, TRB_CMD_NOOP); in xhci_handle_stopped_cmd_ring()
412 * completion event is received for these no-op commands in xhci_handle_stopped_cmd_ring()
416 xhci->cmd_ring_state = CMD_RING_STATE_RUNNING; in xhci_handle_stopped_cmd_ring()
419 if ((xhci->cmd_ring->dequeue != xhci->cmd_ring->enqueue) && in xhci_handle_stopped_cmd_ring()
420 !(xhci->xhc_state & XHCI_STATE_DYING)) { in xhci_handle_stopped_cmd_ring()
421 xhci->current_cmd = cur_cmd; in xhci_handle_stopped_cmd_ring()
422 xhci_mod_cmd_timer(xhci); in xhci_handle_stopped_cmd_ring()
423 xhci_ring_cmd_db(xhci); in xhci_handle_stopped_cmd_ring()
427 /* Must be called with xhci->lock held, releases and aquires lock back */
428 static int xhci_abort_cmd_ring(struct xhci_hcd *xhci, unsigned long flags) in xhci_abort_cmd_ring() argument
430 struct xhci_segment *new_seg = xhci->cmd_ring->deq_seg; in xhci_abort_cmd_ring()
431 union xhci_trb *new_deq = xhci->cmd_ring->dequeue; in xhci_abort_cmd_ring()
435 xhci_dbg(xhci, "Abort command ring\n"); in xhci_abort_cmd_ring()
437 reinit_completion(&xhci->cmd_ring_stop_completion); in xhci_abort_cmd_ring()
447 next_trb(xhci, NULL, &new_seg, &new_deq); in xhci_abort_cmd_ring()
449 next_trb(xhci, NULL, &new_seg, &new_deq); in xhci_abort_cmd_ring()
452 xhci_write_64(xhci, crcr | CMD_RING_ABORT, &xhci->op_regs->cmd_ring); in xhci_abort_cmd_ring()
454 /* Section 4.6.1.2 of xHCI 1.0 spec says software should also time the in xhci_abort_cmd_ring()
456 * seconds then driver handles it as if host died (-ENODEV). in xhci_abort_cmd_ring()
457 * In the future we should distinguish between -ENODEV and -ETIMEDOUT in xhci_abort_cmd_ring()
458 * and try to recover a -ETIMEDOUT with a host controller reset. in xhci_abort_cmd_ring()
460 ret = xhci_handshake(&xhci->op_regs->cmd_ring, in xhci_abort_cmd_ring()
463 xhci_err(xhci, "Abort failed to stop command ring: %d\n", ret); in xhci_abort_cmd_ring()
464 xhci_halt(xhci); in xhci_abort_cmd_ring()
465 xhci_hc_died(xhci); in xhci_abort_cmd_ring()
474 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_abort_cmd_ring()
475 ret = wait_for_completion_timeout(&xhci->cmd_ring_stop_completion, in xhci_abort_cmd_ring()
477 spin_lock_irqsave(&xhci->lock, flags); in xhci_abort_cmd_ring()
479 xhci_dbg(xhci, "No stop event for abort, ring start fail?\n"); in xhci_abort_cmd_ring()
480 xhci_cleanup_command_queue(xhci); in xhci_abort_cmd_ring()
482 xhci_handle_stopped_cmd_ring(xhci, xhci_next_queued_cmd(xhci)); in xhci_abort_cmd_ring()
487 void xhci_ring_ep_doorbell(struct xhci_hcd *xhci, in xhci_ring_ep_doorbell() argument
492 __le32 __iomem *db_addr = &xhci->dba->doorbell[slot_id]; in xhci_ring_ep_doorbell()
493 struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index]; in xhci_ring_ep_doorbell()
494 unsigned int ep_state = ep->ep_state; in xhci_ring_ep_doorbell()
514 static void ring_doorbell_for_active_rings(struct xhci_hcd *xhci, in ring_doorbell_for_active_rings() argument
521 ep = &xhci->devs[slot_id]->eps[ep_index]; in ring_doorbell_for_active_rings()
524 if (!(ep->ep_state & EP_HAS_STREAMS)) { in ring_doorbell_for_active_rings()
525 if (ep->ring && !(list_empty(&ep->ring->td_list))) in ring_doorbell_for_active_rings()
526 xhci_ring_ep_doorbell(xhci, slot_id, ep_index, 0); in ring_doorbell_for_active_rings()
530 for (stream_id = 1; stream_id < ep->stream_info->num_streams; in ring_doorbell_for_active_rings()
532 struct xhci_stream_info *stream_info = ep->stream_info; in ring_doorbell_for_active_rings()
533 if (!list_empty(&stream_info->stream_rings[stream_id]->td_list)) in ring_doorbell_for_active_rings()
534 xhci_ring_ep_doorbell(xhci, slot_id, ep_index, in ring_doorbell_for_active_rings()
539 void xhci_ring_doorbell_for_active_rings(struct xhci_hcd *xhci, in xhci_ring_doorbell_for_active_rings() argument
543 ring_doorbell_for_active_rings(xhci, slot_id, ep_index); in xhci_ring_doorbell_for_active_rings()
546 static struct xhci_virt_ep *xhci_get_virt_ep(struct xhci_hcd *xhci, in xhci_get_virt_ep() argument
551 xhci_warn(xhci, "Invalid slot_id %u\n", slot_id); in xhci_get_virt_ep()
555 xhci_warn(xhci, "Invalid endpoint index %u\n", ep_index); in xhci_get_virt_ep()
558 if (!xhci->devs[slot_id]) { in xhci_get_virt_ep()
559 xhci_warn(xhci, "No xhci virt device for slot_id %u\n", slot_id); in xhci_get_virt_ep()
563 return &xhci->devs[slot_id]->eps[ep_index]; in xhci_get_virt_ep()
566 static struct xhci_ring *xhci_virt_ep_to_ring(struct xhci_hcd *xhci, in xhci_virt_ep_to_ring() argument
571 if (!(ep->ep_state & EP_HAS_STREAMS)) in xhci_virt_ep_to_ring()
572 return ep->ring; in xhci_virt_ep_to_ring()
574 if (!ep->stream_info) in xhci_virt_ep_to_ring()
577 if (stream_id == 0 || stream_id >= ep->stream_info->num_streams) { in xhci_virt_ep_to_ring()
578 xhci_warn(xhci, "Invalid stream_id %u request for slot_id %u ep_index %u\n", in xhci_virt_ep_to_ring()
579 stream_id, ep->vdev->slot_id, ep->ep_index); in xhci_virt_ep_to_ring()
583 return ep->stream_info->stream_rings[stream_id]; in xhci_virt_ep_to_ring()
590 struct xhci_ring *xhci_triad_to_transfer_ring(struct xhci_hcd *xhci, in xhci_triad_to_transfer_ring() argument
596 ep = xhci_get_virt_ep(xhci, slot_id, ep_index); in xhci_triad_to_transfer_ring()
600 return xhci_virt_ep_to_ring(xhci, ep, stream_id); in xhci_triad_to_transfer_ring()
610 static u64 xhci_get_hw_deq(struct xhci_hcd *xhci, struct xhci_virt_device *vdev, in xhci_get_hw_deq() argument
617 ep = &vdev->eps[ep_index]; in xhci_get_hw_deq()
619 if (ep->ep_state & EP_HAS_STREAMS) { in xhci_get_hw_deq()
620 st_ctx = &ep->stream_info->stream_ctx_array[stream_id]; in xhci_get_hw_deq()
621 return le64_to_cpu(st_ctx->stream_ring); in xhci_get_hw_deq()
623 ep_ctx = xhci_get_ep_ctx(xhci, vdev->out_ctx, ep_index); in xhci_get_hw_deq()
624 return le64_to_cpu(ep_ctx->deq); in xhci_get_hw_deq()
627 static int xhci_move_dequeue_past_td(struct xhci_hcd *xhci, in xhci_move_dequeue_past_td() argument
631 struct xhci_virt_device *dev = xhci->devs[slot_id]; in xhci_move_dequeue_past_td()
632 struct xhci_virt_ep *ep = &dev->eps[ep_index]; in xhci_move_dequeue_past_td()
645 ep_ring = xhci_triad_to_transfer_ring(xhci, slot_id, in xhci_move_dequeue_past_td()
648 xhci_warn(xhci, "WARN can't find new dequeue, invalid stream ID %u\n", in xhci_move_dequeue_past_td()
650 return -ENODEV; in xhci_move_dequeue_past_td()
660 if (list_empty(&ep_ring->td_list)) { in xhci_move_dequeue_past_td()
661 new_seg = ep_ring->enq_seg; in xhci_move_dequeue_past_td()
662 new_deq = ep_ring->enqueue; in xhci_move_dequeue_past_td()
663 new_cycle = ep_ring->cycle_state; in xhci_move_dequeue_past_td()
664 xhci_dbg(xhci, "ep ring empty, Set new dequeue = enqueue"); in xhci_move_dequeue_past_td()
667 xhci_warn(xhci, "Can't find new dequeue state, missing td\n"); in xhci_move_dequeue_past_td()
668 return -EINVAL; in xhci_move_dequeue_past_td()
672 hw_dequeue = xhci_get_hw_deq(xhci, dev, ep_index, stream_id); in xhci_move_dequeue_past_td()
673 new_seg = ep_ring->deq_seg; in xhci_move_dequeue_past_td()
674 new_deq = ep_ring->dequeue; in xhci_move_dequeue_past_td()
690 if (new_deq == td->last_trb) in xhci_move_dequeue_past_td()
697 next_trb(xhci, ep_ring, &new_seg, &new_deq); in xhci_move_dequeue_past_td()
700 if (new_deq == ep->ring->dequeue) { in xhci_move_dequeue_past_td()
701 xhci_err(xhci, "Error: Failed finding new dequeue state\n"); in xhci_move_dequeue_past_td()
702 return -EINVAL; in xhci_move_dequeue_past_td()
712 xhci_warn(xhci, "Can't find dma of new dequeue ptr\n"); in xhci_move_dequeue_past_td()
713 xhci_warn(xhci, "deq seg = %p, deq ptr = %p\n", new_seg, new_deq); in xhci_move_dequeue_past_td()
714 return -EINVAL; in xhci_move_dequeue_past_td()
717 if ((ep->ep_state & SET_DEQ_PENDING)) { in xhci_move_dequeue_past_td()
718 xhci_warn(xhci, "Set TR Deq already pending, don't submit for 0x%pad\n", in xhci_move_dequeue_past_td()
720 return -EBUSY; in xhci_move_dequeue_past_td()
724 cmd = xhci_alloc_command(xhci, false, GFP_ATOMIC); in xhci_move_dequeue_past_td()
726 xhci_warn(xhci, "Can't alloc Set TR Deq cmd 0x%pad\n", &addr); in xhci_move_dequeue_past_td()
727 return -ENOMEM; in xhci_move_dequeue_past_td()
732 ret = queue_command(xhci, cmd, in xhci_move_dequeue_past_td()
738 xhci_free_command(xhci, cmd); in xhci_move_dequeue_past_td()
741 ep->queued_deq_seg = new_seg; in xhci_move_dequeue_past_td()
742 ep->queued_deq_ptr = new_deq; in xhci_move_dequeue_past_td()
744 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, in xhci_move_dequeue_past_td()
752 ep->ep_state |= SET_DEQ_PENDING; in xhci_move_dequeue_past_td()
753 xhci_ring_cmd_db(xhci); in xhci_move_dequeue_past_td()
761 static void td_to_noop(struct xhci_hcd *xhci, struct xhci_ring *ep_ring, in td_to_noop() argument
764 struct xhci_segment *seg = td->start_seg; in td_to_noop()
765 union xhci_trb *trb = td->first_trb; in td_to_noop()
771 if (flip_cycle && trb != td->first_trb && trb != td->last_trb) in td_to_noop()
772 trb->generic.field[3] ^= cpu_to_le32(TRB_CYCLE); in td_to_noop()
774 if (trb == td->last_trb) in td_to_noop()
777 next_trb(xhci, ep_ring, &seg, &trb); in td_to_noop()
782 * Must be called with xhci->lock held in interrupt context,
783 * releases and re-acquires xhci->lock
785 static void xhci_giveback_urb_in_irq(struct xhci_hcd *xhci, in xhci_giveback_urb_in_irq() argument
788 struct urb *urb = cur_td->urb; in xhci_giveback_urb_in_irq()
789 struct urb_priv *urb_priv = urb->hcpriv; in xhci_giveback_urb_in_irq()
790 struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus); in xhci_giveback_urb_in_irq()
792 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) { in xhci_giveback_urb_in_irq()
793 xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs--; in xhci_giveback_urb_in_irq()
794 if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs == 0) { in xhci_giveback_urb_in_irq()
795 if (xhci->quirks & XHCI_AMD_PLL_FIX) in xhci_giveback_urb_in_irq()
805 static void xhci_unmap_td_bounce_buffer(struct xhci_hcd *xhci, in xhci_unmap_td_bounce_buffer() argument
808 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_unmap_td_bounce_buffer()
809 struct xhci_segment *seg = td->bounce_seg; in xhci_unmap_td_bounce_buffer()
810 struct urb *urb = td->urb; in xhci_unmap_td_bounce_buffer()
817 dma_unmap_single(dev, seg->bounce_dma, ring->bounce_buf_len, in xhci_unmap_td_bounce_buffer()
822 dma_unmap_single(dev, seg->bounce_dma, ring->bounce_buf_len, in xhci_unmap_td_bounce_buffer()
825 if (urb->num_sgs) { in xhci_unmap_td_bounce_buffer()
826 len = sg_pcopy_from_buffer(urb->sg, urb->num_sgs, seg->bounce_buf, in xhci_unmap_td_bounce_buffer()
827 seg->bounce_len, seg->bounce_offs); in xhci_unmap_td_bounce_buffer()
828 if (len != seg->bounce_len) in xhci_unmap_td_bounce_buffer()
829 xhci_warn(xhci, "WARN Wrong bounce buffer read length: %zu != %d\n", in xhci_unmap_td_bounce_buffer()
830 len, seg->bounce_len); in xhci_unmap_td_bounce_buffer()
832 memcpy(urb->transfer_buffer + seg->bounce_offs, seg->bounce_buf, in xhci_unmap_td_bounce_buffer()
833 seg->bounce_len); in xhci_unmap_td_bounce_buffer()
835 seg->bounce_len = 0; in xhci_unmap_td_bounce_buffer()
836 seg->bounce_offs = 0; in xhci_unmap_td_bounce_buffer()
839 static int xhci_td_cleanup(struct xhci_hcd *xhci, struct xhci_td *td, in xhci_td_cleanup() argument
845 urb = td->urb; in xhci_td_cleanup()
848 xhci_unmap_td_bounce_buffer(xhci, ep_ring, td); in xhci_td_cleanup()
852 * length, urb->actual_length will be a very big number (since it's in xhci_td_cleanup()
855 if (urb->actual_length > urb->transfer_buffer_length) { in xhci_td_cleanup()
856 xhci_warn(xhci, "URB req %u and actual %u transfer length mismatch\n", in xhci_td_cleanup()
857 urb->transfer_buffer_length, urb->actual_length); in xhci_td_cleanup()
858 urb->actual_length = 0; in xhci_td_cleanup()
862 if (!list_empty(&td->td_list)) in xhci_td_cleanup()
863 list_del_init(&td->td_list); in xhci_td_cleanup()
865 if (!list_empty(&td->cancelled_td_list)) in xhci_td_cleanup()
866 list_del_init(&td->cancelled_td_list); in xhci_td_cleanup()
871 if ((urb->actual_length != urb->transfer_buffer_length && in xhci_td_cleanup()
872 (urb->transfer_flags & URB_SHORT_NOT_OK)) || in xhci_td_cleanup()
873 (status != 0 && !usb_endpoint_xfer_isoc(&urb->ep->desc))) in xhci_td_cleanup()
874 xhci_dbg(xhci, "Giveback URB %p, len = %d, expected = %d, status = %d\n", in xhci_td_cleanup()
875 urb, urb->actual_length, in xhci_td_cleanup()
876 urb->transfer_buffer_length, status); in xhci_td_cleanup()
879 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) in xhci_td_cleanup()
881 xhci_giveback_urb_in_irq(xhci, td, status); in xhci_td_cleanup()
894 list_for_each_entry_safe(td, tmp_td, &ep->cancelled_td_list, in xhci_giveback_invalidated_tds()
897 ring = xhci_urb_to_transfer_ring(ep->xhci, td->urb); in xhci_giveback_invalidated_tds()
899 if (td->cancel_status == TD_CLEARED) { in xhci_giveback_invalidated_tds()
900 xhci_dbg(ep->xhci, "%s: Giveback cancelled URB %p TD\n", in xhci_giveback_invalidated_tds()
901 __func__, td->urb); in xhci_giveback_invalidated_tds()
902 xhci_td_cleanup(ep->xhci, td, ring, td->status); in xhci_giveback_invalidated_tds()
904 xhci_dbg(ep->xhci, "%s: Keep cancelled URB %p TD as cancel_status is %d\n", in xhci_giveback_invalidated_tds()
905 __func__, td->urb, td->cancel_status); in xhci_giveback_invalidated_tds()
907 if (ep->xhci->xhc_state & XHCI_STATE_DYING) in xhci_giveback_invalidated_tds()
912 static int xhci_reset_halted_ep(struct xhci_hcd *xhci, unsigned int slot_id, in xhci_reset_halted_ep() argument
918 command = xhci_alloc_command(xhci, false, GFP_ATOMIC); in xhci_reset_halted_ep()
920 ret = -ENOMEM; in xhci_reset_halted_ep()
924 xhci_dbg(xhci, "%s-reset ep %u, slot %u\n", in xhci_reset_halted_ep()
928 ret = xhci_queue_reset_ep(xhci, command, slot_id, ep_index, reset_type); in xhci_reset_halted_ep()
931 xhci_err(xhci, "ERROR queuing reset endpoint for slot %d ep_index %d, %d\n", in xhci_reset_halted_ep()
936 static int xhci_handle_halted_endpoint(struct xhci_hcd *xhci, in xhci_handle_halted_endpoint() argument
941 unsigned int slot_id = ep->vdev->slot_id; in xhci_handle_halted_endpoint()
948 if (ep->vdev->flags & VDEV_PORT_ERROR) in xhci_handle_halted_endpoint()
949 return -ENODEV; in xhci_handle_halted_endpoint()
953 ep->ep_state |= EP_HARD_CLEAR_TOGGLE; in xhci_handle_halted_endpoint()
954 if (td && list_empty(&td->cancelled_td_list)) { in xhci_handle_halted_endpoint()
955 list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list); in xhci_handle_halted_endpoint()
956 td->cancel_status = TD_HALTED; in xhci_handle_halted_endpoint()
960 if (ep->ep_state & EP_HALTED) { in xhci_handle_halted_endpoint()
961 xhci_dbg(xhci, "Reset ep command for ep_index %d already pending\n", in xhci_handle_halted_endpoint()
962 ep->ep_index); in xhci_handle_halted_endpoint()
966 err = xhci_reset_halted_ep(xhci, slot_id, ep->ep_index, reset_type); in xhci_handle_halted_endpoint()
970 ep->ep_state |= EP_HALTED; in xhci_handle_halted_endpoint()
972 xhci_ring_cmd_db(xhci); in xhci_handle_halted_endpoint()
979 * We have the xHCI lock, so nothing can modify this list until we drop it.
980 * We're also in the event handler, so we can't get re-interrupted if another
988 struct xhci_hcd *xhci; in xhci_invalidate_cancelled_tds() local
994 unsigned int slot_id = ep->vdev->slot_id; in xhci_invalidate_cancelled_tds()
1001 if (ep->ep_state & SET_DEQ_PENDING) in xhci_invalidate_cancelled_tds()
1004 xhci = ep->xhci; in xhci_invalidate_cancelled_tds()
1006 list_for_each_entry_safe(td, tmp_td, &ep->cancelled_td_list, cancelled_td_list) { in xhci_invalidate_cancelled_tds()
1007 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, in xhci_invalidate_cancelled_tds()
1010 td->start_seg, td->first_trb), in xhci_invalidate_cancelled_tds()
1011 td->urb->stream_id, td->urb); in xhci_invalidate_cancelled_tds()
1012 list_del_init(&td->td_list); in xhci_invalidate_cancelled_tds()
1013 ring = xhci_urb_to_transfer_ring(xhci, td->urb); in xhci_invalidate_cancelled_tds()
1015 xhci_warn(xhci, "WARN Cancelled URB %p has invalid stream ID %u.\n", in xhci_invalidate_cancelled_tds()
1016 td->urb, td->urb->stream_id); in xhci_invalidate_cancelled_tds()
1025 hw_deq = xhci_get_hw_deq(xhci, ep->vdev, ep->ep_index, in xhci_invalidate_cancelled_tds()
1026 td->urb->stream_id); in xhci_invalidate_cancelled_tds()
1029 if (td->cancel_status == TD_HALTED || in xhci_invalidate_cancelled_tds()
1030 trb_in_td(xhci, td->start_seg, td->first_trb, td->last_trb, hw_deq, false)) { in xhci_invalidate_cancelled_tds()
1031 switch (td->cancel_status) { in xhci_invalidate_cancelled_tds()
1032 case TD_CLEARED: /* TD is already no-op */ in xhci_invalidate_cancelled_tds()
1039 if (cached_td->urb->stream_id != td->urb->stream_id) { in xhci_invalidate_cancelled_tds()
1041 xhci_dbg(xhci, in xhci_invalidate_cancelled_tds()
1043 td->urb->stream_id, td->urb); in xhci_invalidate_cancelled_tds()
1044 td->cancel_status = TD_CLEARING_CACHE_DEFERRED; in xhci_invalidate_cancelled_tds()
1049 xhci_warn(xhci, in xhci_invalidate_cancelled_tds()
1051 td->urb, cached_td->urb, in xhci_invalidate_cancelled_tds()
1052 td->urb->stream_id); in xhci_invalidate_cancelled_tds()
1053 td_to_noop(xhci, ring, cached_td, false); in xhci_invalidate_cancelled_tds()
1054 cached_td->cancel_status = TD_CLEARED; in xhci_invalidate_cancelled_tds()
1056 td_to_noop(xhci, ring, td, false); in xhci_invalidate_cancelled_tds()
1057 td->cancel_status = TD_CLEARING_CACHE; in xhci_invalidate_cancelled_tds()
1062 td_to_noop(xhci, ring, td, false); in xhci_invalidate_cancelled_tds()
1063 td->cancel_status = TD_CLEARED; in xhci_invalidate_cancelled_tds()
1071 err = xhci_move_dequeue_past_td(xhci, slot_id, ep->ep_index, in xhci_invalidate_cancelled_tds()
1072 cached_td->urb->stream_id, in xhci_invalidate_cancelled_tds()
1075 /* Failed to move past cached td, just set cached TDs to no-op */ in xhci_invalidate_cancelled_tds()
1076 list_for_each_entry_safe(td, tmp_td, &ep->cancelled_td_list, cancelled_td_list) { in xhci_invalidate_cancelled_tds()
1082 if (td->cancel_status != TD_CLEARING_CACHE && in xhci_invalidate_cancelled_tds()
1083 td->cancel_status != TD_CLEARING_CACHE_DEFERRED) in xhci_invalidate_cancelled_tds()
1085 xhci_warn(xhci, "Failed to clear cancelled cached URB %p, mark clear anyway\n", in xhci_invalidate_cancelled_tds()
1086 td->urb); in xhci_invalidate_cancelled_tds()
1087 td_to_noop(xhci, ring, td, false); in xhci_invalidate_cancelled_tds()
1088 td->cancel_status = TD_CLEARED; in xhci_invalidate_cancelled_tds()
1096 * Only call for non-running rings without streams.
1103 if (!list_empty(&ep->ring->td_list)) { /* Not streams compatible */ in find_halted_td()
1104 hw_deq = xhci_get_hw_deq(ep->xhci, ep->vdev, ep->ep_index, 0); in find_halted_td()
1106 td = list_first_entry(&ep->ring->td_list, struct xhci_td, td_list); in find_halted_td()
1107 if (trb_in_td(ep->xhci, td->start_seg, td->first_trb, in find_halted_td()
1108 td->last_trb, hw_deq, false)) in find_halted_td()
1121 * 2. Otherwise, we turn all the TRBs in the TD into No-op TRBs (with the chain
1124 static void xhci_handle_cmd_stop_ep(struct xhci_hcd *xhci, int slot_id, in xhci_handle_cmd_stop_ep() argument
1135 if (unlikely(TRB_TO_SUSPEND_PORT(le32_to_cpu(trb->generic.field[3])))) { in xhci_handle_cmd_stop_ep()
1136 if (!xhci->devs[slot_id]) in xhci_handle_cmd_stop_ep()
1137 xhci_warn(xhci, "Stop endpoint command completion for disabled slot %u\n", in xhci_handle_cmd_stop_ep()
1142 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3])); in xhci_handle_cmd_stop_ep()
1143 ep = xhci_get_virt_ep(xhci, slot_id, ep_index); in xhci_handle_cmd_stop_ep()
1147 ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep_index); in xhci_handle_cmd_stop_ep()
1160 * Proper error code is unknown here, it would be -EPIPE if device side in xhci_handle_cmd_stop_ep()
1161 * of enadpoit halted (aka STALL), and -EPROTO if not (transaction error) in xhci_handle_cmd_stop_ep()
1162 * We use -EPROTO, if device is stalled it should return a stall error on in xhci_handle_cmd_stop_ep()
1163 * next transfer, which then will return -EPIPE, and device side stall is in xhci_handle_cmd_stop_ep()
1168 xhci_dbg(xhci, "Stop ep completion raced with stall, reset ep\n"); in xhci_handle_cmd_stop_ep()
1169 if (ep->ep_state & EP_HAS_STREAMS) { in xhci_handle_cmd_stop_ep()
1175 td->status = -EPROTO; in xhci_handle_cmd_stop_ep()
1178 err = xhci_handle_halted_endpoint(xhci, ep, td, reset_type); in xhci_handle_cmd_stop_ep()
1181 ep->ep_state &= ~EP_STOP_CMD_PENDING; in xhci_handle_cmd_stop_ep()
1185 xhci_dbg(xhci, "Stop ep completion ctx error, ep is running\n"); in xhci_handle_cmd_stop_ep()
1187 command = xhci_alloc_command(xhci, false, GFP_ATOMIC); in xhci_handle_cmd_stop_ep()
1189 ep->ep_state &= ~EP_STOP_CMD_PENDING; in xhci_handle_cmd_stop_ep()
1192 xhci_queue_stop_endpoint(xhci, command, slot_id, ep_index, 0); in xhci_handle_cmd_stop_ep()
1193 xhci_ring_cmd_db(xhci); in xhci_handle_cmd_stop_ep()
1203 ep->ep_state &= ~EP_STOP_CMD_PENDING; in xhci_handle_cmd_stop_ep()
1207 ring_doorbell_for_active_rings(xhci, slot_id, ep_index); in xhci_handle_cmd_stop_ep()
1210 static void xhci_kill_ring_urbs(struct xhci_hcd *xhci, struct xhci_ring *ring) in xhci_kill_ring_urbs() argument
1215 list_for_each_entry_safe(cur_td, tmp, &ring->td_list, td_list) { in xhci_kill_ring_urbs()
1216 list_del_init(&cur_td->td_list); in xhci_kill_ring_urbs()
1218 if (!list_empty(&cur_td->cancelled_td_list)) in xhci_kill_ring_urbs()
1219 list_del_init(&cur_td->cancelled_td_list); in xhci_kill_ring_urbs()
1221 xhci_unmap_td_bounce_buffer(xhci, ring, cur_td); in xhci_kill_ring_urbs()
1223 inc_td_cnt(cur_td->urb); in xhci_kill_ring_urbs()
1225 xhci_giveback_urb_in_irq(xhci, cur_td, -ESHUTDOWN); in xhci_kill_ring_urbs()
1229 static void xhci_kill_endpoint_urbs(struct xhci_hcd *xhci, in xhci_kill_endpoint_urbs() argument
1237 ep = xhci_get_virt_ep(xhci, slot_id, ep_index); in xhci_kill_endpoint_urbs()
1241 if ((ep->ep_state & EP_HAS_STREAMS) || in xhci_kill_endpoint_urbs()
1242 (ep->ep_state & EP_GETTING_NO_STREAMS)) { in xhci_kill_endpoint_urbs()
1245 for (stream_id = 1; stream_id < ep->stream_info->num_streams; in xhci_kill_endpoint_urbs()
1247 ring = ep->stream_info->stream_rings[stream_id]; in xhci_kill_endpoint_urbs()
1251 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, in xhci_kill_endpoint_urbs()
1254 xhci_kill_ring_urbs(xhci, ring); in xhci_kill_endpoint_urbs()
1257 ring = ep->ring; in xhci_kill_endpoint_urbs()
1260 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, in xhci_kill_endpoint_urbs()
1263 xhci_kill_ring_urbs(xhci, ring); in xhci_kill_endpoint_urbs()
1266 list_for_each_entry_safe(cur_td, tmp, &ep->cancelled_td_list, in xhci_kill_endpoint_urbs()
1268 list_del_init(&cur_td->cancelled_td_list); in xhci_kill_endpoint_urbs()
1269 inc_td_cnt(cur_td->urb); in xhci_kill_endpoint_urbs()
1272 xhci_giveback_urb_in_irq(xhci, cur_td, -ESHUTDOWN); in xhci_kill_endpoint_urbs()
1282 * Call with xhci->lock held.
1283 * lock is relased and re-acquired while giving back urb.
1285 void xhci_hc_died(struct xhci_hcd *xhci) in xhci_hc_died() argument
1289 if (xhci->xhc_state & XHCI_STATE_DYING) in xhci_hc_died()
1292 xhci_err(xhci, "xHCI host controller not responding, assume dead\n"); in xhci_hc_died()
1293 xhci->xhc_state |= XHCI_STATE_DYING; in xhci_hc_died()
1295 xhci_cleanup_command_queue(xhci); in xhci_hc_died()
1298 for (i = 0; i <= HCS_MAX_SLOTS(xhci->hcs_params1); i++) { in xhci_hc_died()
1299 if (!xhci->devs[i]) in xhci_hc_died()
1302 xhci_kill_endpoint_urbs(xhci, i, j); in xhci_hc_died()
1306 if (!(xhci->xhc_state & XHCI_STATE_REMOVING)) in xhci_hc_died()
1307 usb_hc_died(xhci_to_hcd(xhci)); in xhci_hc_died()
1310 static void update_ring_for_set_deq_completion(struct xhci_hcd *xhci, in update_ring_for_set_deq_completion() argument
1317 dequeue_temp = ep_ring->dequeue; in update_ring_for_set_deq_completion()
1319 /* If we get two back-to-back stalls, and the first stalled transfer in update_ring_for_set_deq_completion()
1323 * the segment into la-la-land. in update_ring_for_set_deq_completion()
1325 if (trb_is_link(ep_ring->dequeue)) { in update_ring_for_set_deq_completion()
1326 ep_ring->deq_seg = ep_ring->deq_seg->next; in update_ring_for_set_deq_completion()
1327 ep_ring->dequeue = ep_ring->deq_seg->trbs; in update_ring_for_set_deq_completion()
1330 while (ep_ring->dequeue != dev->eps[ep_index].queued_deq_ptr) { in update_ring_for_set_deq_completion()
1332 ep_ring->dequeue++; in update_ring_for_set_deq_completion()
1333 if (trb_is_link(ep_ring->dequeue)) { in update_ring_for_set_deq_completion()
1334 if (ep_ring->dequeue == in update_ring_for_set_deq_completion()
1335 dev->eps[ep_index].queued_deq_ptr) in update_ring_for_set_deq_completion()
1337 ep_ring->deq_seg = ep_ring->deq_seg->next; in update_ring_for_set_deq_completion()
1338 ep_ring->dequeue = ep_ring->deq_seg->trbs; in update_ring_for_set_deq_completion()
1340 if (ep_ring->dequeue == dequeue_temp) { in update_ring_for_set_deq_completion()
1341 xhci_dbg(xhci, "Unable to find new dequeue pointer\n"); in update_ring_for_set_deq_completion()
1354 static void xhci_handle_cmd_set_deq(struct xhci_hcd *xhci, int slot_id, in xhci_handle_cmd_set_deq() argument
1365 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3])); in xhci_handle_cmd_set_deq()
1366 stream_id = TRB_TO_STREAM_ID(le32_to_cpu(trb->generic.field[2])); in xhci_handle_cmd_set_deq()
1367 ep = xhci_get_virt_ep(xhci, slot_id, ep_index); in xhci_handle_cmd_set_deq()
1371 ep_ring = xhci_virt_ep_to_ring(xhci, ep, stream_id); in xhci_handle_cmd_set_deq()
1373 xhci_warn(xhci, "WARN Set TR deq ptr command for freed stream ID %u\n", in xhci_handle_cmd_set_deq()
1379 ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep_index); in xhci_handle_cmd_set_deq()
1380 slot_ctx = xhci_get_slot_ctx(xhci, ep->vdev->out_ctx); in xhci_handle_cmd_set_deq()
1390 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd invalid because of stream ID configuration\n"); in xhci_handle_cmd_set_deq()
1393 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed due to incorrect slot or ep state.\n"); in xhci_handle_cmd_set_deq()
1395 slot_state = le32_to_cpu(slot_ctx->dev_state); in xhci_handle_cmd_set_deq()
1397 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, in xhci_handle_cmd_set_deq()
1402 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed because slot %u was not enabled.\n", in xhci_handle_cmd_set_deq()
1406 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd with unknown completion code of %u.\n", in xhci_handle_cmd_set_deq()
1419 if (ep->ep_state & EP_HAS_STREAMS) { in xhci_handle_cmd_set_deq()
1421 &ep->stream_info->stream_ctx_array[stream_id]; in xhci_handle_cmd_set_deq()
1422 deq = le64_to_cpu(ctx->stream_ring) & SCTX_DEQ_MASK; in xhci_handle_cmd_set_deq()
1425 * Cadence xHCI controllers store some endpoint state in xhci_handle_cmd_set_deq()
1433 if (xhci->quirks & XHCI_CDNS_SCTX_QUIRK) { in xhci_handle_cmd_set_deq()
1434 ctx->reserved[0] = 0; in xhci_handle_cmd_set_deq()
1435 ctx->reserved[1] = 0; in xhci_handle_cmd_set_deq()
1438 deq = le64_to_cpu(ep_ctx->deq) & ~EP_CTX_CYCLE_MASK; in xhci_handle_cmd_set_deq()
1440 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, in xhci_handle_cmd_set_deq()
1442 if (xhci_trb_virt_to_dma(ep->queued_deq_seg, in xhci_handle_cmd_set_deq()
1443 ep->queued_deq_ptr) == deq) { in xhci_handle_cmd_set_deq()
1447 update_ring_for_set_deq_completion(xhci, ep->vdev, in xhci_handle_cmd_set_deq()
1450 xhci_warn(xhci, "Mismatch between completed Set TR Deq Ptr command & xHCI internal state.\n"); in xhci_handle_cmd_set_deq()
1451 xhci_warn(xhci, "ep deq seg = %p, deq ptr = %p\n", in xhci_handle_cmd_set_deq()
1452 ep->queued_deq_seg, ep->queued_deq_ptr); in xhci_handle_cmd_set_deq()
1456 list_for_each_entry_safe(td, tmp_td, &ep->cancelled_td_list, in xhci_handle_cmd_set_deq()
1458 ep_ring = xhci_urb_to_transfer_ring(ep->xhci, td->urb); in xhci_handle_cmd_set_deq()
1459 if (td->cancel_status == TD_CLEARING_CACHE) { in xhci_handle_cmd_set_deq()
1460 td->cancel_status = TD_CLEARED; in xhci_handle_cmd_set_deq()
1461 xhci_dbg(ep->xhci, "%s: Giveback cancelled URB %p TD\n", in xhci_handle_cmd_set_deq()
1462 __func__, td->urb); in xhci_handle_cmd_set_deq()
1463 xhci_td_cleanup(ep->xhci, td, ep_ring, td->status); in xhci_handle_cmd_set_deq()
1465 xhci_dbg(ep->xhci, "%s: Keep cancelled URB %p TD as cancel_status is %d\n", in xhci_handle_cmd_set_deq()
1466 __func__, td->urb, td->cancel_status); in xhci_handle_cmd_set_deq()
1470 ep->ep_state &= ~SET_DEQ_PENDING; in xhci_handle_cmd_set_deq()
1471 ep->queued_deq_seg = NULL; in xhci_handle_cmd_set_deq()
1472 ep->queued_deq_ptr = NULL; in xhci_handle_cmd_set_deq()
1475 if (!list_empty(&ep->cancelled_td_list)) { in xhci_handle_cmd_set_deq()
1476 xhci_dbg(ep->xhci, "%s: Pending TDs to clear, continuing with invalidation\n", in xhci_handle_cmd_set_deq()
1480 ring_doorbell_for_active_rings(xhci, slot_id, ep_index); in xhci_handle_cmd_set_deq()
1485 xhci_dbg(ep->xhci, "%s: All TDs cleared, ring doorbell\n", __func__); in xhci_handle_cmd_set_deq()
1486 ring_doorbell_for_active_rings(xhci, slot_id, ep_index); in xhci_handle_cmd_set_deq()
1490 static void xhci_handle_cmd_reset_ep(struct xhci_hcd *xhci, int slot_id, in xhci_handle_cmd_reset_ep() argument
1497 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3])); in xhci_handle_cmd_reset_ep()
1498 ep = xhci_get_virt_ep(xhci, slot_id, ep_index); in xhci_handle_cmd_reset_ep()
1502 ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep_index); in xhci_handle_cmd_reset_ep()
1508 xhci_dbg_trace(xhci, trace_xhci_dbg_reset_ep, in xhci_handle_cmd_reset_ep()
1515 ep->ep_state &= ~EP_HALTED; in xhci_handle_cmd_reset_ep()
1520 if ((le32_to_cpu(trb->generic.field[3])) & TRB_TSP) in xhci_handle_cmd_reset_ep()
1521 ring_doorbell_for_active_rings(xhci, slot_id, ep_index); in xhci_handle_cmd_reset_ep()
1524 static void xhci_handle_cmd_enable_slot(struct xhci_hcd *xhci, int slot_id, in xhci_handle_cmd_enable_slot() argument
1528 command->slot_id = slot_id; in xhci_handle_cmd_enable_slot()
1530 command->slot_id = 0; in xhci_handle_cmd_enable_slot()
1533 static void xhci_handle_cmd_disable_slot(struct xhci_hcd *xhci, int slot_id) in xhci_handle_cmd_disable_slot() argument
1538 virt_dev = xhci->devs[slot_id]; in xhci_handle_cmd_disable_slot()
1542 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx); in xhci_handle_cmd_disable_slot()
1545 if (xhci->quirks & XHCI_EP_LIMIT_QUIRK) in xhci_handle_cmd_disable_slot()
1547 xhci_free_device_endpoint_resources(xhci, virt_dev, true); in xhci_handle_cmd_disable_slot()
1550 static void xhci_handle_cmd_config_ep(struct xhci_hcd *xhci, int slot_id, in xhci_handle_cmd_config_ep() argument
1564 virt_dev = xhci->devs[slot_id]; in xhci_handle_cmd_config_ep()
1567 ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx); in xhci_handle_cmd_config_ep()
1569 xhci_warn(xhci, "Could not get input context, bad type.\n"); in xhci_handle_cmd_config_ep()
1573 add_flags = le32_to_cpu(ctrl_ctx->add_flags); in xhci_handle_cmd_config_ep()
1576 ep_index = xhci_last_valid_endpoint(add_flags) - 1; in xhci_handle_cmd_config_ep()
1578 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->out_ctx, ep_index); in xhci_handle_cmd_config_ep()
1584 static void xhci_handle_cmd_addr_dev(struct xhci_hcd *xhci, int slot_id) in xhci_handle_cmd_addr_dev() argument
1589 vdev = xhci->devs[slot_id]; in xhci_handle_cmd_addr_dev()
1592 slot_ctx = xhci_get_slot_ctx(xhci, vdev->out_ctx); in xhci_handle_cmd_addr_dev()
1596 static void xhci_handle_cmd_reset_dev(struct xhci_hcd *xhci, int slot_id) in xhci_handle_cmd_reset_dev() argument
1601 vdev = xhci->devs[slot_id]; in xhci_handle_cmd_reset_dev()
1603 xhci_warn(xhci, "Reset device command completion for disabled slot %u\n", in xhci_handle_cmd_reset_dev()
1607 slot_ctx = xhci_get_slot_ctx(xhci, vdev->out_ctx); in xhci_handle_cmd_reset_dev()
1610 xhci_dbg(xhci, "Completed reset device command.\n"); in xhci_handle_cmd_reset_dev()
1613 static void xhci_handle_cmd_nec_get_fw(struct xhci_hcd *xhci, in xhci_handle_cmd_nec_get_fw() argument
1616 if (!(xhci->quirks & XHCI_NEC_HOST)) { in xhci_handle_cmd_nec_get_fw()
1617 xhci_warn(xhci, "WARN NEC_GET_FW command on non-NEC host\n"); in xhci_handle_cmd_nec_get_fw()
1620 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_handle_cmd_nec_get_fw()
1622 NEC_FW_MAJOR(le32_to_cpu(event->status)), in xhci_handle_cmd_nec_get_fw()
1623 NEC_FW_MINOR(le32_to_cpu(event->status))); in xhci_handle_cmd_nec_get_fw()
1628 list_del(&cmd->cmd_list); in xhci_complete_del_and_free_cmd()
1630 if (cmd->completion) { in xhci_complete_del_and_free_cmd()
1631 cmd->status = status; in xhci_complete_del_and_free_cmd()
1632 complete(cmd->completion); in xhci_complete_del_and_free_cmd()
1638 void xhci_cleanup_command_queue(struct xhci_hcd *xhci) in xhci_cleanup_command_queue() argument
1641 xhci->current_cmd = NULL; in xhci_cleanup_command_queue()
1642 list_for_each_entry_safe(cur_cmd, tmp_cmd, &xhci->cmd_list, cmd_list) in xhci_cleanup_command_queue()
1648 struct xhci_hcd *xhci; in xhci_handle_command_timeout() local
1655 xhci = container_of(to_delayed_work(work), struct xhci_hcd, cmd_timer); in xhci_handle_command_timeout()
1657 spin_lock_irqsave(&xhci->lock, flags); in xhci_handle_command_timeout()
1663 if (!xhci->current_cmd || delayed_work_pending(&xhci->cmd_timer)) { in xhci_handle_command_timeout()
1664 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_handle_command_timeout()
1668 cmd_field3 = le32_to_cpu(xhci->current_cmd->command_trb->generic.field[3]); in xhci_handle_command_timeout()
1669 usbsts = readl(&xhci->op_regs->status); in xhci_handle_command_timeout()
1670 xhci_dbg(xhci, "Command timeout, USBSTS:%s\n", xhci_decode_usbsts(str, usbsts)); in xhci_handle_command_timeout()
1672 /* Bail out and tear down xhci if a stop endpoint command failed */ in xhci_handle_command_timeout()
1676 xhci_warn(xhci, "xHCI host not responding to stop endpoint command\n"); in xhci_handle_command_timeout()
1678 ep = xhci_get_virt_ep(xhci, TRB_TO_SLOT_ID(cmd_field3), in xhci_handle_command_timeout()
1681 ep->ep_state &= ~EP_STOP_CMD_PENDING; in xhci_handle_command_timeout()
1683 xhci_halt(xhci); in xhci_handle_command_timeout()
1684 xhci_hc_died(xhci); in xhci_handle_command_timeout()
1689 xhci->current_cmd->status = COMP_COMMAND_ABORTED; in xhci_handle_command_timeout()
1692 hw_ring_state = xhci_read_64(xhci, &xhci->op_regs->cmd_ring); in xhci_handle_command_timeout()
1694 xhci_hc_died(xhci); in xhci_handle_command_timeout()
1698 if ((xhci->cmd_ring_state & CMD_RING_STATE_RUNNING) && in xhci_handle_command_timeout()
1701 xhci->cmd_ring_state = CMD_RING_STATE_ABORTED; in xhci_handle_command_timeout()
1702 xhci_dbg(xhci, "Command timeout\n"); in xhci_handle_command_timeout()
1703 xhci_abort_cmd_ring(xhci, flags); in xhci_handle_command_timeout()
1708 if (xhci->xhc_state & XHCI_STATE_REMOVING) { in xhci_handle_command_timeout()
1709 xhci_dbg(xhci, "host removed, ring start fail?\n"); in xhci_handle_command_timeout()
1710 xhci_cleanup_command_queue(xhci); in xhci_handle_command_timeout()
1716 xhci_dbg(xhci, "Command timeout on stopped ring\n"); in xhci_handle_command_timeout()
1717 xhci_handle_stopped_cmd_ring(xhci, xhci->current_cmd); in xhci_handle_command_timeout()
1720 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_handle_command_timeout()
1724 static void handle_cmd_completion(struct xhci_hcd *xhci, in handle_cmd_completion() argument
1727 unsigned int slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags)); in handle_cmd_completion()
1736 xhci_warn(xhci, "Invalid slot_id %u\n", slot_id); in handle_cmd_completion()
1740 cmd_dma = le64_to_cpu(event->cmd_trb); in handle_cmd_completion()
1741 cmd_trb = xhci->cmd_ring->dequeue; in handle_cmd_completion()
1743 trace_xhci_handle_command(xhci->cmd_ring, &cmd_trb->generic); in handle_cmd_completion()
1745 cmd_comp_code = GET_COMP_CODE(le32_to_cpu(event->status)); in handle_cmd_completion()
1749 complete_all(&xhci->cmd_ring_stop_completion); in handle_cmd_completion()
1753 cmd_dequeue_dma = xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg, in handle_cmd_completion()
1760 xhci_warn(xhci, in handle_cmd_completion()
1765 cmd = list_first_entry(&xhci->cmd_list, struct xhci_command, cmd_list); in handle_cmd_completion()
1767 cancel_delayed_work(&xhci->cmd_timer); in handle_cmd_completion()
1769 if (cmd->command_trb != xhci->cmd_ring->dequeue) { in handle_cmd_completion()
1770 xhci_err(xhci, in handle_cmd_completion()
1782 xhci->cmd_ring_state = CMD_RING_STATE_STOPPED; in handle_cmd_completion()
1783 if (cmd->status == COMP_COMMAND_ABORTED) { in handle_cmd_completion()
1784 if (xhci->current_cmd == cmd) in handle_cmd_completion()
1785 xhci->current_cmd = NULL; in handle_cmd_completion()
1790 cmd_type = TRB_FIELD_TO_TYPE(le32_to_cpu(cmd_trb->generic.field[3])); in handle_cmd_completion()
1793 xhci_handle_cmd_enable_slot(xhci, slot_id, cmd, cmd_comp_code); in handle_cmd_completion()
1796 xhci_handle_cmd_disable_slot(xhci, slot_id); in handle_cmd_completion()
1799 if (!cmd->completion) in handle_cmd_completion()
1800 xhci_handle_cmd_config_ep(xhci, slot_id, cmd_comp_code); in handle_cmd_completion()
1805 xhci_handle_cmd_addr_dev(xhci, slot_id); in handle_cmd_completion()
1809 le32_to_cpu(cmd_trb->generic.field[3]))); in handle_cmd_completion()
1810 if (!cmd->completion) in handle_cmd_completion()
1811 xhci_handle_cmd_stop_ep(xhci, slot_id, cmd_trb, in handle_cmd_completion()
1816 le32_to_cpu(cmd_trb->generic.field[3]))); in handle_cmd_completion()
1817 xhci_handle_cmd_set_deq(xhci, slot_id, cmd_trb, cmd_comp_code); in handle_cmd_completion()
1820 /* Is this an aborted command turned to NO-OP? */ in handle_cmd_completion()
1821 if (cmd->status == COMP_COMMAND_RING_STOPPED) in handle_cmd_completion()
1826 le32_to_cpu(cmd_trb->generic.field[3]))); in handle_cmd_completion()
1827 xhci_handle_cmd_reset_ep(xhci, slot_id, cmd_trb, cmd_comp_code); in handle_cmd_completion()
1831 * Use the SLOT_ID from the command TRB instead (xhci 4.6.11) in handle_cmd_completion()
1834 le32_to_cpu(cmd_trb->generic.field[3])); in handle_cmd_completion()
1835 xhci_handle_cmd_reset_dev(xhci, slot_id); in handle_cmd_completion()
1838 xhci_handle_cmd_nec_get_fw(xhci, event); in handle_cmd_completion()
1842 xhci_info(xhci, "INFO unknown command type %d\n", cmd_type); in handle_cmd_completion()
1847 if (!list_is_singular(&xhci->cmd_list)) { in handle_cmd_completion()
1848 xhci->current_cmd = list_first_entry(&cmd->cmd_list, in handle_cmd_completion()
1850 xhci_mod_cmd_timer(xhci); in handle_cmd_completion()
1851 } else if (xhci->current_cmd == cmd) { in handle_cmd_completion()
1852 xhci->current_cmd = NULL; in handle_cmd_completion()
1858 inc_deq(xhci, xhci->cmd_ring); in handle_cmd_completion()
1861 static void handle_vendor_event(struct xhci_hcd *xhci, in handle_vendor_event() argument
1864 xhci_dbg(xhci, "Vendor specific event TRB type = %u\n", trb_type); in handle_vendor_event()
1865 if (trb_type == TRB_NEC_CMD_COMP && (xhci->quirks & XHCI_NEC_HOST)) in handle_vendor_event()
1866 handle_cmd_completion(xhci, &event->event_cmd); in handle_vendor_event()
1869 static void handle_device_notification(struct xhci_hcd *xhci, in handle_device_notification() argument
1875 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->generic.field[3])); in handle_device_notification()
1876 if (!xhci->devs[slot_id]) { in handle_device_notification()
1877 xhci_warn(xhci, "Device Notification event for " in handle_device_notification()
1882 xhci_dbg(xhci, "Device Wake Notification event for slot ID %u\n", in handle_device_notification()
1884 udev = xhci->devs[slot_id]->udev; in handle_device_notification()
1885 if (udev && udev->parent) in handle_device_notification()
1886 usb_wakeup_notification(udev->parent, udev->portnum); in handle_device_notification()
1890 * Quirk hanlder for errata seen on Cavium ThunderX2 processor XHCI
1892 * As per ThunderX2errata-129 USB 2 device may come up as USB 1
1901 static void xhci_cavium_reset_phy_quirk(struct xhci_hcd *xhci) in xhci_cavium_reset_phy_quirk() argument
1903 struct usb_hcd *hcd = xhci_to_hcd(xhci); in xhci_cavium_reset_phy_quirk()
1909 writel(0x6F, hcd->regs + 0x1048); in xhci_cavium_reset_phy_quirk()
1911 /* De-assert the PHY reset */ in xhci_cavium_reset_phy_quirk()
1912 writel(0x7F, hcd->regs + 0x1048); in xhci_cavium_reset_phy_quirk()
1914 pll_lock_check = readl(hcd->regs + 0x1070); in xhci_cavium_reset_phy_quirk()
1915 } while (!(pll_lock_check & 0x1) && --retry_count); in xhci_cavium_reset_phy_quirk()
1918 static void handle_port_status(struct xhci_hcd *xhci, in handle_port_status() argument
1933 if (GET_COMP_CODE(le32_to_cpu(event->generic.field[2])) != COMP_SUCCESS) in handle_port_status()
1934 xhci_warn(xhci, in handle_port_status()
1937 port_id = GET_PORT_ID(le32_to_cpu(event->generic.field[0])); in handle_port_status()
1938 max_ports = HCS_MAX_PORTS(xhci->hcs_params1); in handle_port_status()
1941 xhci_warn(xhci, "Port change event with invalid port ID %d\n", in handle_port_status()
1943 inc_deq(xhci, ir->event_ring); in handle_port_status()
1947 port = &xhci->hw_ports[port_id - 1]; in handle_port_status()
1948 if (!port || !port->rhub || port->hcd_portnum == DUPLICATE_ENTRY) { in handle_port_status()
1949 xhci_warn(xhci, "Port change event, no port for port ID %u\n", in handle_port_status()
1956 if (port->rhub == &xhci->usb3_rhub && xhci->shared_hcd == NULL) { in handle_port_status()
1957 xhci_dbg(xhci, "ignore port event for removed USB3 hcd\n"); in handle_port_status()
1962 hcd = port->rhub->hcd; in handle_port_status()
1963 bus_state = &port->rhub->bus_state; in handle_port_status()
1964 hcd_portnum = port->hcd_portnum; in handle_port_status()
1965 portsc = readl(port->addr); in handle_port_status()
1967 xhci_dbg(xhci, "Port change event, %d-%d, id %d, portsc: 0x%x\n", in handle_port_status()
1968 hcd->self.busnum, hcd_portnum + 1, port_id, portsc); in handle_port_status()
1972 if (hcd->state == HC_STATE_SUSPENDED) { in handle_port_status()
1973 xhci_dbg(xhci, "resume root hub\n"); in handle_port_status()
1977 if (hcd->speed >= HCD_USB3 && in handle_port_status()
1979 slot_id = xhci_find_slot_id_by_port(hcd, xhci, hcd_portnum + 1); in handle_port_status()
1980 if (slot_id && xhci->devs[slot_id]) in handle_port_status()
1981 xhci->devs[slot_id]->flags |= VDEV_PORT_ERROR; in handle_port_status()
1985 xhci_dbg(xhci, "port resume event for port %d\n", port_id); in handle_port_status()
1987 cmd_reg = readl(&xhci->op_regs->command); in handle_port_status()
1989 xhci_warn(xhci, "xHC is not running.\n"); in handle_port_status()
1994 xhci_dbg(xhci, "remote wake SS port %d\n", port_id); in handle_port_status()
1999 bus_state->port_remote_wakeup |= 1 << hcd_portnum; in handle_port_status()
2000 xhci_test_and_clear_bit(xhci, port, PORT_PLC); in handle_port_status()
2001 usb_hcd_start_port_resume(&hcd->self, hcd_portnum); in handle_port_status()
2002 xhci_set_link_state(xhci, port, XDEV_U0); in handle_port_status()
2008 } else if (!test_bit(hcd_portnum, &bus_state->resuming_ports)) { in handle_port_status()
2009 xhci_dbg(xhci, "resume HS port %d\n", port_id); in handle_port_status()
2010 port->resume_timestamp = jiffies + in handle_port_status()
2012 set_bit(hcd_portnum, &bus_state->resuming_ports); in handle_port_status()
2015 * usb device auto-resume latency around ~40ms. in handle_port_status()
2017 set_bit(HCD_FLAG_POLL_RH, &hcd->flags); in handle_port_status()
2018 mod_timer(&hcd->rh_timer, in handle_port_status()
2019 port->resume_timestamp); in handle_port_status()
2020 usb_hcd_start_port_resume(&hcd->self, hcd_portnum); in handle_port_status()
2030 xhci_dbg(xhci, "resume SS port %d finished\n", port_id); in handle_port_status()
2031 complete(&port->u3exit_done); in handle_port_status()
2034 * U3Exit state after a host-initiated resume. If it's a device in handle_port_status()
2039 slot_id = xhci_find_slot_id_by_port(hcd, xhci, hcd_portnum + 1); in handle_port_status()
2040 if (slot_id && xhci->devs[slot_id]) in handle_port_status()
2041 xhci_ring_device(xhci, slot_id); in handle_port_status()
2042 if (bus_state->port_remote_wakeup & (1 << hcd_portnum)) { in handle_port_status()
2043 xhci_test_and_clear_bit(xhci, port, PORT_PLC); in handle_port_status()
2044 usb_wakeup_notification(hcd->self.root_hub, in handle_port_status()
2052 * Check to see if xhci-hub.c is waiting on RExit to U0 transition (or in handle_port_status()
2056 if (hcd->speed < HCD_USB3 && port->rexit_active) { in handle_port_status()
2057 complete(&port->rexit_done); in handle_port_status()
2058 port->rexit_active = false; in handle_port_status()
2063 if (hcd->speed < HCD_USB3) { in handle_port_status()
2064 xhci_test_and_clear_bit(xhci, port, PORT_PLC); in handle_port_status()
2065 if ((xhci->quirks & XHCI_RESET_PLL_ON_DISCONNECT) && in handle_port_status()
2067 xhci_cavium_reset_phy_quirk(xhci); in handle_port_status()
2072 inc_deq(xhci, ir->event_ring); in handle_port_status()
2082 * xHCI port-status-change events occur when the "or" of all the in handle_port_status()
2083 * status-change bits in the portsc register changes from 0 to 1. in handle_port_status()
2088 xhci_dbg(xhci, "%s: starting usb%d port polling.\n", in handle_port_status()
2089 __func__, hcd->self.busnum); in handle_port_status()
2090 set_bit(HCD_FLAG_POLL_RH, &hcd->flags); in handle_port_status()
2091 spin_unlock(&xhci->lock); in handle_port_status()
2094 spin_lock(&xhci->lock); in handle_port_status()
2103 struct xhci_segment *trb_in_td(struct xhci_hcd *xhci, in trb_in_td() argument
2123 &cur_seg->trbs[TRBS_PER_SEGMENT - 1]); in trb_in_td()
2128 xhci_warn(xhci, in trb_in_td()
2129 …"Looking for event-dma %016llx trb-start %016llx trb-end %016llx seg-start %016llx seg-end %016llx… in trb_in_td()
2133 (unsigned long long)cur_seg->dma, in trb_in_td()
2147 (suspect_dma >= cur_seg->dma && in trb_in_td()
2157 cur_seg = cur_seg->next; in trb_in_td()
2158 start_dma = xhci_trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]); in trb_in_td()
2164 static void xhci_clear_hub_tt_buffer(struct xhci_hcd *xhci, struct xhci_td *td, in xhci_clear_hub_tt_buffer() argument
2168 * As part of low/full-speed endpoint-halt processing in xhci_clear_hub_tt_buffer()
2171 if (td->urb->dev->tt && !usb_pipeint(td->urb->pipe) && in xhci_clear_hub_tt_buffer()
2172 (td->urb->dev->tt->hub != xhci_to_hcd(xhci)->self.root_hub) && in xhci_clear_hub_tt_buffer()
2173 !(ep->ep_state & EP_CLEARING_TT)) { in xhci_clear_hub_tt_buffer()
2174 ep->ep_state |= EP_CLEARING_TT; in xhci_clear_hub_tt_buffer()
2175 td->urb->ep->hcpriv = td->urb->dev; in xhci_clear_hub_tt_buffer()
2176 if (usb_hub_clear_tt_buffer(td->urb)) in xhci_clear_hub_tt_buffer()
2177 ep->ep_state &= ~EP_CLEARING_TT; in xhci_clear_hub_tt_buffer()
2182 * cleanup the halt for a non-default control endpoint if we indicate a stall.
2187 static int xhci_requires_manual_halt_cleanup(struct xhci_hcd *xhci, in xhci_requires_manual_halt_cleanup() argument
2207 int xhci_is_vendor_info_code(struct xhci_hcd *xhci, unsigned int trb_comp_code) in xhci_is_vendor_info_code() argument
2211 * treat as not-an-error. in xhci_is_vendor_info_code()
2213 xhci_dbg(xhci, "Vendor defined info completion code %u\n", in xhci_is_vendor_info_code()
2215 xhci_dbg(xhci, "Treating code as success.\n"); in xhci_is_vendor_info_code()
2221 static int finish_td(struct xhci_hcd *xhci, struct xhci_virt_ep *ep, in finish_td() argument
2227 ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep->ep_index); in finish_td()
2259 if ((ep->ep_state & EP_HALTED) && in finish_td()
2260 !list_empty(&td->cancelled_td_list)) { in finish_td()
2261 xhci_dbg(xhci, "Already resolving halted ep for 0x%llx\n", in finish_td()
2263 td->start_seg, td->first_trb)); in finish_td()
2270 xhci_clear_hub_tt_buffer(xhci, td, ep); in finish_td()
2271 xhci_handle_halted_endpoint(xhci, ep, td, EP_HARD_RESET); in finish_td()
2275 * xhci internal endpoint state will go to a "halt" state for in finish_td()
2284 if (ep->ep_index != 0) in finish_td()
2285 xhci_clear_hub_tt_buffer(xhci, td, ep); in finish_td()
2287 xhci_handle_halted_endpoint(xhci, ep, td, EP_HARD_RESET); in finish_td()
2295 ep_ring->dequeue = td->last_trb; in finish_td()
2296 ep_ring->deq_seg = td->last_trb_seg; in finish_td()
2297 inc_deq(xhci, ep_ring); in finish_td()
2299 return xhci_td_cleanup(xhci, td, ep_ring, td->status); in finish_td()
2303 static int sum_trb_lengths(struct xhci_hcd *xhci, struct xhci_ring *ring, in sum_trb_lengths() argument
2307 union xhci_trb *trb = ring->dequeue; in sum_trb_lengths()
2308 struct xhci_segment *seg = ring->deq_seg; in sum_trb_lengths()
2310 for (sum = 0; trb != stop_trb; next_trb(xhci, ring, &seg, &trb)) { in sum_trb_lengths()
2312 sum += TRB_LEN(le32_to_cpu(trb->generic.field[2])); in sum_trb_lengths()
2320 static int process_ctrl_td(struct xhci_hcd *xhci, struct xhci_virt_ep *ep, in process_ctrl_td() argument
2329 trb_type = TRB_FIELD_TO_TYPE(le32_to_cpu(ep_trb->generic.field[3])); in process_ctrl_td()
2330 ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep->ep_index); in process_ctrl_td()
2331 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len)); in process_ctrl_td()
2332 requested = td->urb->transfer_buffer_length; in process_ctrl_td()
2333 remaining = EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)); in process_ctrl_td()
2338 xhci_warn(xhci, "WARN: Success on ctrl %s TRB without IOC set?\n", in process_ctrl_td()
2340 td->status = -ESHUTDOWN; in process_ctrl_td()
2343 td->status = 0; in process_ctrl_td()
2346 td->status = 0; in process_ctrl_td()
2350 td->urb->actual_length = remaining; in process_ctrl_td()
2352 xhci_warn(xhci, "WARN: Stopped Short Packet on ctrl setup or status TRB\n"); in process_ctrl_td()
2357 td->urb->actual_length = 0; in process_ctrl_td()
2361 td->urb->actual_length = requested - remaining; in process_ctrl_td()
2364 td->urb->actual_length = requested; in process_ctrl_td()
2367 xhci_warn(xhci, "WARN: unexpected TRB Type %d\n", in process_ctrl_td()
2374 if (!xhci_requires_manual_halt_cleanup(xhci, in process_ctrl_td()
2377 xhci_dbg(xhci, "TRB error %u, halted endpoint index = %u\n", in process_ctrl_td()
2378 trb_comp_code, ep->ep_index); in process_ctrl_td()
2383 td->urb->actual_length = requested - remaining; in process_ctrl_td()
2384 else if (!td->urb_length_set) in process_ctrl_td()
2385 td->urb->actual_length = 0; in process_ctrl_td()
2399 td->urb_length_set = true; in process_ctrl_td()
2400 td->urb->actual_length = requested - remaining; in process_ctrl_td()
2401 xhci_dbg(xhci, "Waiting for status stage event\n"); in process_ctrl_td()
2406 if (!td->urb_length_set) in process_ctrl_td()
2407 td->urb->actual_length = requested; in process_ctrl_td()
2410 return finish_td(xhci, ep, ep_ring, td, trb_comp_code); in process_ctrl_td()
2416 static int process_isoc_td(struct xhci_hcd *xhci, struct xhci_virt_ep *ep, in process_isoc_td() argument
2428 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len)); in process_isoc_td()
2429 urb_priv = td->urb->hcpriv; in process_isoc_td()
2430 idx = urb_priv->num_tds_done; in process_isoc_td()
2431 frame = &td->urb->iso_frame_desc[idx]; in process_isoc_td()
2432 requested = frame->length; in process_isoc_td()
2433 remaining = EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)); in process_isoc_td()
2434 ep_trb_len = TRB_LEN(le32_to_cpu(ep_trb->generic.field[2])); in process_isoc_td()
2435 short_framestatus = td->urb->transfer_flags & URB_SHORT_NOT_OK ? in process_isoc_td()
2436 -EREMOTEIO : 0; in process_isoc_td()
2441 /* Don't overwrite status if TD had an error, see xHCI 4.9.1 */ in process_isoc_td()
2442 if (td->error_mid_td) in process_isoc_td()
2445 frame->status = short_framestatus; in process_isoc_td()
2449 frame->status = 0; in process_isoc_td()
2452 frame->status = short_framestatus; in process_isoc_td()
2456 frame->status = -ECOMM; in process_isoc_td()
2462 frame->status = -EOVERFLOW; in process_isoc_td()
2463 if (ep_trb != td->last_trb) in process_isoc_td()
2464 td->error_mid_td = true; in process_isoc_td()
2468 frame->status = -EPROTO; in process_isoc_td()
2471 frame->status = -EPROTO; in process_isoc_td()
2473 if (ep_trb != td->last_trb) in process_isoc_td()
2474 td->error_mid_td = true; in process_isoc_td()
2481 frame->status = short_framestatus; in process_isoc_td()
2490 frame->status = -1; in process_isoc_td()
2494 if (td->urb_length_set) in process_isoc_td()
2498 frame->actual_length = sum_trb_lengths(xhci, ep->ring, ep_trb) + in process_isoc_td()
2499 ep_trb_len - remaining; in process_isoc_td()
2501 frame->actual_length = requested; in process_isoc_td()
2503 td->urb->actual_length += frame->actual_length; in process_isoc_td()
2507 if (td->error_mid_td && ep_trb != td->last_trb) { in process_isoc_td()
2508 xhci_dbg(xhci, "Error mid isoc TD, wait for final completion event\n"); in process_isoc_td()
2509 td->urb_length_set = true; in process_isoc_td()
2513 return finish_td(xhci, ep, ep_ring, td, trb_comp_code); in process_isoc_td()
2516 static int skip_isoc_td(struct xhci_hcd *xhci, struct xhci_td *td, in skip_isoc_td() argument
2523 urb_priv = td->urb->hcpriv; in skip_isoc_td()
2524 idx = urb_priv->num_tds_done; in skip_isoc_td()
2525 frame = &td->urb->iso_frame_desc[idx]; in skip_isoc_td()
2528 frame->status = -EXDEV; in skip_isoc_td()
2531 frame->actual_length = 0; in skip_isoc_td()
2534 ep->ring->dequeue = td->last_trb; in skip_isoc_td()
2535 ep->ring->deq_seg = td->last_trb_seg; in skip_isoc_td()
2536 inc_deq(xhci, ep->ring); in skip_isoc_td()
2538 return xhci_td_cleanup(xhci, td, ep->ring, status); in skip_isoc_td()
2544 static int process_bulk_intr_td(struct xhci_hcd *xhci, struct xhci_virt_ep *ep, in process_bulk_intr_td() argument
2552 slot_ctx = xhci_get_slot_ctx(xhci, ep->vdev->out_ctx); in process_bulk_intr_td()
2553 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len)); in process_bulk_intr_td()
2554 remaining = EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)); in process_bulk_intr_td()
2555 ep_trb_len = TRB_LEN(le32_to_cpu(ep_trb->generic.field[2])); in process_bulk_intr_td()
2556 requested = td->urb->transfer_buffer_length; in process_bulk_intr_td()
2560 ep->err_count = 0; in process_bulk_intr_td()
2562 if (ep_trb != td->last_trb || remaining) { in process_bulk_intr_td()
2563 xhci_warn(xhci, "WARN Successful completion on short TX\n"); in process_bulk_intr_td()
2564 xhci_dbg(xhci, "ep %#x - asked for %d bytes, %d bytes untransferred\n", in process_bulk_intr_td()
2565 td->urb->ep->desc.bEndpointAddress, in process_bulk_intr_td()
2568 td->status = 0; in process_bulk_intr_td()
2571 xhci_dbg(xhci, "ep %#x - asked for %d bytes, %d bytes untransferred\n", in process_bulk_intr_td()
2572 td->urb->ep->desc.bEndpointAddress, in process_bulk_intr_td()
2574 td->status = 0; in process_bulk_intr_td()
2577 td->urb->actual_length = remaining; in process_bulk_intr_td()
2581 td->urb->actual_length = sum_trb_lengths(xhci, ep_ring, ep_trb); in process_bulk_intr_td()
2584 if (xhci->quirks & XHCI_NO_SOFT_RETRY || in process_bulk_intr_td()
2585 (ep->err_count++ > MAX_SOFT_RETRY) || in process_bulk_intr_td()
2586 le32_to_cpu(slot_ctx->tt_info) & TT_SLOT) in process_bulk_intr_td()
2589 td->status = 0; in process_bulk_intr_td()
2591 xhci_handle_halted_endpoint(xhci, ep, td, EP_SOFT_RESET); in process_bulk_intr_td()
2598 if (ep_trb == td->last_trb) in process_bulk_intr_td()
2599 td->urb->actual_length = requested - remaining; in process_bulk_intr_td()
2601 td->urb->actual_length = in process_bulk_intr_td()
2602 sum_trb_lengths(xhci, ep_ring, ep_trb) + in process_bulk_intr_td()
2603 ep_trb_len - remaining; in process_bulk_intr_td()
2606 xhci_warn(xhci, "bad transfer trb length %d in event trb\n", in process_bulk_intr_td()
2608 td->urb->actual_length = 0; in process_bulk_intr_td()
2611 return finish_td(xhci, ep, ep_ring, td, trb_comp_code); in process_bulk_intr_td()
2619 static int handle_tx_event(struct xhci_hcd *xhci, in handle_tx_event() argument
2631 int status = -EINPROGRESS; in handle_tx_event()
2637 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags)); in handle_tx_event()
2638 ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1; in handle_tx_event()
2639 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len)); in handle_tx_event()
2640 ep_trb_dma = le64_to_cpu(event->buffer); in handle_tx_event()
2642 ep = xhci_get_virt_ep(xhci, slot_id, ep_index); in handle_tx_event()
2644 xhci_err(xhci, "ERROR Invalid Transfer event\n"); in handle_tx_event()
2649 ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep_index); in handle_tx_event()
2652 xhci_err(xhci, in handle_tx_event()
2658 /* Some transfer events don't always point to a trb, see xhci 4.17.4 */ in handle_tx_event()
2665 xhci_dbg(xhci, "Stream transaction error ep %u no id\n", in handle_tx_event()
2667 if (ep->err_count++ > MAX_SOFT_RETRY) in handle_tx_event()
2668 xhci_handle_halted_endpoint(xhci, ep, NULL, in handle_tx_event()
2671 xhci_handle_halted_endpoint(xhci, ep, NULL, in handle_tx_event()
2679 xhci_err(xhci, "ERROR Transfer event for unknown stream ring slot %u ep %u\n", in handle_tx_event()
2685 /* Count current td numbers if ep->skip is set */ in handle_tx_event()
2686 if (ep->skip) in handle_tx_event()
2687 td_num += list_count_nodes(&ep_ring->td_list); in handle_tx_event()
2695 if (EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)) != 0) { in handle_tx_event()
2697 xhci_dbg(xhci, "Successful completion on short TX for slot %u ep %u with last td short %d\n", in handle_tx_event()
2698 slot_id, ep_index, ep_ring->last_td_was_short); in handle_tx_event()
2705 xhci_dbg(xhci, "Stopped on Transfer TRB for slot %u ep %u\n", in handle_tx_event()
2709 xhci_dbg(xhci, in handle_tx_event()
2710 "Stopped on No-op or Link TRB for slot %u ep %u\n", in handle_tx_event()
2714 xhci_dbg(xhci, in handle_tx_event()
2720 xhci_dbg(xhci, "Stalled endpoint for slot %u ep %u\n", slot_id, in handle_tx_event()
2722 status = -EPIPE; in handle_tx_event()
2725 xhci_dbg(xhci, "Split transaction error for slot %u ep %u\n", in handle_tx_event()
2727 status = -EPROTO; in handle_tx_event()
2730 xhci_dbg(xhci, "Transfer error for slot %u ep %u on endpoint\n", in handle_tx_event()
2732 status = -EPROTO; in handle_tx_event()
2735 xhci_dbg(xhci, "Babble error for slot %u ep %u on endpoint\n", in handle_tx_event()
2737 status = -EOVERFLOW; in handle_tx_event()
2741 xhci_warn(xhci, in handle_tx_event()
2744 status = -EILSEQ; in handle_tx_event()
2748 xhci_warn(xhci, in handle_tx_event()
2751 status = -ENOSR; in handle_tx_event()
2754 xhci_warn(xhci, in handle_tx_event()
2759 xhci_warn(xhci, in handle_tx_event()
2769 xhci_dbg(xhci, "underrun event on endpoint\n"); in handle_tx_event()
2770 if (!list_empty(&ep_ring->td_list)) in handle_tx_event()
2771 xhci_dbg(xhci, "Underrun Event for slot %d ep %d " in handle_tx_event()
2773 TRB_TO_SLOT_ID(le32_to_cpu(event->flags)), in handle_tx_event()
2777 xhci_dbg(xhci, "overrun event on endpoint\n"); in handle_tx_event()
2778 if (!list_empty(&ep_ring->td_list)) in handle_tx_event()
2779 xhci_dbg(xhci, "Overrun Event for slot %d ep %d " in handle_tx_event()
2781 TRB_TO_SLOT_ID(le32_to_cpu(event->flags)), in handle_tx_event()
2791 ep->skip = true; in handle_tx_event()
2792 xhci_dbg(xhci, in handle_tx_event()
2797 ep->skip = true; in handle_tx_event()
2798 xhci_dbg(xhci, in handle_tx_event()
2805 xhci_warn(xhci, in handle_tx_event()
2808 status = -EPROTO; in handle_tx_event()
2811 if (xhci_is_vendor_info_code(xhci, trb_comp_code)) { in handle_tx_event()
2815 xhci_warn(xhci, in handle_tx_event()
2825 if (list_empty(&ep_ring->td_list)) { in handle_tx_event()
2836 ep_ring->last_td_was_short)) { in handle_tx_event()
2837 xhci_warn(xhci, "WARN Event TRB for slot %d ep %d with no TDs queued?\n", in handle_tx_event()
2838 TRB_TO_SLOT_ID(le32_to_cpu(event->flags)), in handle_tx_event()
2841 if (ep->skip) { in handle_tx_event()
2842 ep->skip = false; in handle_tx_event()
2843 xhci_dbg(xhci, "td_list is empty while skip flag set. Clear skip flag for slot %u ep %u.\n", in handle_tx_event()
2847 xhci_requires_manual_halt_cleanup(xhci, ep_ctx, in handle_tx_event()
2849 xhci_handle_halted_endpoint(xhci, ep, NULL, in handle_tx_event()
2855 /* We've skipped all the TDs on the ep ring when ep->skip set */ in handle_tx_event()
2856 if (ep->skip && td_num == 0) { in handle_tx_event()
2857 ep->skip = false; in handle_tx_event()
2858 xhci_dbg(xhci, "All tds on the ep_ring skipped. Clear skip flag for slot %u ep %u.\n", in handle_tx_event()
2863 td = list_first_entry(&ep_ring->td_list, struct xhci_td, in handle_tx_event()
2865 if (ep->skip) in handle_tx_event()
2866 td_num--; in handle_tx_event()
2869 ep_seg = trb_in_td(xhci, ep_ring->deq_seg, ep_ring->dequeue, in handle_tx_event()
2870 td->last_trb, ep_trb_dma, false); in handle_tx_event()
2874 * is not in the current TD pointed by ep_ring->dequeue because in handle_tx_event()
2887 if (ep->skip && usb_endpoint_xfer_isoc(&td->urb->ep->desc)) { in handle_tx_event()
2888 skip_isoc_td(xhci, td, ep, status); in handle_tx_event()
2896 if ((xhci->quirks & XHCI_SPURIOUS_SUCCESS) && in handle_tx_event()
2897 ep_ring->last_td_was_short) { in handle_tx_event()
2898 ep_ring->last_td_was_short = false; in handle_tx_event()
2903 * xhci 4.10.2 states isoc endpoints should continue in handle_tx_event()
2907 * xhci 4.9.1 states that if there are errors in mult-TRB in handle_tx_event()
2913 if (td->error_mid_td && in handle_tx_event()
2914 !list_is_last(&td->td_list, &ep_ring->td_list)) { in handle_tx_event()
2917 ep_seg = trb_in_td(xhci, td_next->start_seg, td_next->first_trb, in handle_tx_event()
2918 td_next->last_trb, ep_trb_dma, false); in handle_tx_event()
2921 xhci_dbg(xhci, "Missing TD completion event after mid TD error\n"); in handle_tx_event()
2922 ep_ring->dequeue = td->last_trb; in handle_tx_event()
2923 ep_ring->deq_seg = td->last_trb_seg; in handle_tx_event()
2924 inc_deq(xhci, ep_ring); in handle_tx_event()
2925 xhci_td_cleanup(xhci, td, ep_ring, td->status); in handle_tx_event()
2932 xhci_err(xhci, in handle_tx_event()
2937 trb_in_td(xhci, ep_ring->deq_seg, in handle_tx_event()
2938 ep_ring->dequeue, td->last_trb, in handle_tx_event()
2940 return -ESHUTDOWN; in handle_tx_event()
2944 ep_ring->last_td_was_short = true; in handle_tx_event()
2946 ep_ring->last_td_was_short = false; in handle_tx_event()
2948 if (ep->skip) { in handle_tx_event()
2949 xhci_dbg(xhci, in handle_tx_event()
2952 ep->skip = false; in handle_tx_event()
2955 ep_trb = &ep_seg->trbs[(ep_trb_dma - ep_seg->dma) / in handle_tx_event()
2962 * No-op TRB could trigger interrupts in a case where in handle_tx_event()
2971 xhci_requires_manual_halt_cleanup(xhci, ep_ctx, in handle_tx_event()
2973 xhci_handle_halted_endpoint(xhci, ep, td, in handle_tx_event()
2978 td->status = status; in handle_tx_event()
2981 if (usb_endpoint_xfer_control(&td->urb->ep->desc)) in handle_tx_event()
2982 process_ctrl_td(xhci, ep, ep_ring, td, ep_trb, event); in handle_tx_event()
2983 else if (usb_endpoint_xfer_isoc(&td->urb->ep->desc)) in handle_tx_event()
2984 process_isoc_td(xhci, ep, ep_ring, td, ep_trb, event); in handle_tx_event()
2986 process_bulk_intr_td(xhci, ep, ep_ring, td, ep_trb, event); in handle_tx_event()
2988 handling_skipped_tds = ep->skip && in handle_tx_event()
2997 inc_deq(xhci, ir->event_ring); in handle_tx_event()
3000 * If ep->skip is set, it means there are missed tds on the in handle_tx_event()
3010 xhci_err(xhci, "@%016llx %08x %08x %08x %08x\n", in handle_tx_event()
3012 ir->event_ring->deq_seg, in handle_tx_event()
3013 ir->event_ring->dequeue), in handle_tx_event()
3014 lower_32_bits(le64_to_cpu(event->buffer)), in handle_tx_event()
3015 upper_32_bits(le64_to_cpu(event->buffer)), in handle_tx_event()
3016 le32_to_cpu(event->transfer_len), in handle_tx_event()
3017 le32_to_cpu(event->flags)); in handle_tx_event()
3018 return -ENODEV; in handle_tx_event()
3022 * This function handles all OS-owned events on the event ring. It may drop
3023 * xhci->lock between event processing (e.g. to pass up port status changes).
3027 static int xhci_handle_event(struct xhci_hcd *xhci, struct xhci_interrupter *ir) in xhci_handle_event() argument
3035 if (!ir || !ir->event_ring || !ir->event_ring->dequeue) { in xhci_handle_event()
3036 xhci_err(xhci, "ERROR interrupter not ready\n"); in xhci_handle_event()
3037 return -ENOMEM; in xhci_handle_event()
3040 event = ir->event_ring->dequeue; in xhci_handle_event()
3042 if ((le32_to_cpu(event->event_cmd.flags) & TRB_CYCLE) != in xhci_handle_event()
3043 ir->event_ring->cycle_state) in xhci_handle_event()
3046 trace_xhci_handle_event(ir->event_ring, &event->generic); in xhci_handle_event()
3053 trb_type = TRB_FIELD_TO_TYPE(le32_to_cpu(event->event_cmd.flags)); in xhci_handle_event()
3058 handle_cmd_completion(xhci, &event->event_cmd); in xhci_handle_event()
3061 handle_port_status(xhci, ir, event); in xhci_handle_event()
3065 ret = handle_tx_event(xhci, ir, &event->trans_event); in xhci_handle_event()
3070 handle_device_notification(xhci, event); in xhci_handle_event()
3074 handle_vendor_event(xhci, event, trb_type); in xhci_handle_event()
3076 xhci_warn(xhci, "ERROR unknown event type %d\n", trb_type); in xhci_handle_event()
3078 /* Any of the above functions may drop and re-acquire the lock, so check in xhci_handle_event()
3079 * to make sure a watchdog timer didn't mark the host as non-responsive. in xhci_handle_event()
3081 if (xhci->xhc_state & XHCI_STATE_DYING) { in xhci_handle_event()
3082 xhci_dbg(xhci, "xHCI host dying, returning from " in xhci_handle_event()
3089 inc_deq(xhci, ir->event_ring); in xhci_handle_event()
3099 * - When all events have finished
3100 * - To avoid "Event Ring Full Error" condition
3102 static void xhci_update_erst_dequeue(struct xhci_hcd *xhci, in xhci_update_erst_dequeue() argument
3110 temp_64 = xhci_read_64(xhci, &ir->ir_set->erst_dequeue); in xhci_update_erst_dequeue()
3112 if (event_ring_deq != ir->event_ring->dequeue) { in xhci_update_erst_dequeue()
3113 deq = xhci_trb_virt_to_dma(ir->event_ring->deq_seg, in xhci_update_erst_dequeue()
3114 ir->event_ring->dequeue); in xhci_update_erst_dequeue()
3116 xhci_warn(xhci, "WARN something wrong with SW event ring dequeue ptr\n"); in xhci_update_erst_dequeue()
3133 xhci_write_64(xhci, temp_64, &ir->ir_set->erst_dequeue); in xhci_update_erst_dequeue()
3137 * xHCI spec says we can get an interrupt, and if the HC has an error condition,
3143 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_irq() local
3151 spin_lock(&xhci->lock); in xhci_irq()
3153 status = readl(&xhci->op_regs->status); in xhci_irq()
3155 xhci_hc_died(xhci); in xhci_irq()
3164 xhci_warn(xhci, "WARNING: Host Controller Error\n"); in xhci_irq()
3169 xhci_warn(xhci, "WARNING: Host System Error\n"); in xhci_irq()
3170 xhci_halt(xhci); in xhci_irq()
3177 * so we can receive interrupts from other MSI-X interrupters. in xhci_irq()
3181 writel(status, &xhci->op_regs->status); in xhci_irq()
3184 ir = xhci->interrupter; in xhci_irq()
3185 if (!hcd->msi_enabled) { in xhci_irq()
3187 irq_pending = readl(&ir->ir_set->irq_pending); in xhci_irq()
3189 writel(irq_pending, &ir->ir_set->irq_pending); in xhci_irq()
3192 if (xhci->xhc_state & XHCI_STATE_DYING || in xhci_irq()
3193 xhci->xhc_state & XHCI_STATE_HALTED) { in xhci_irq()
3194 xhci_dbg(xhci, "xHCI dying, ignoring interrupt. " in xhci_irq()
3199 temp_64 = xhci_read_64(xhci, &ir->ir_set->erst_dequeue); in xhci_irq()
3200 xhci_write_64(xhci, temp_64 | ERST_EHB, in xhci_irq()
3201 &ir->ir_set->erst_dequeue); in xhci_irq()
3206 event_ring_deq = ir->event_ring->dequeue; in xhci_irq()
3210 while (xhci_handle_event(xhci, ir) > 0) { in xhci_irq()
3213 xhci_update_erst_dequeue(xhci, ir, event_ring_deq, false); in xhci_irq()
3214 event_ring_deq = ir->event_ring->dequeue; in xhci_irq()
3216 /* ring is half-full, force isoc trbs to interrupt more often */ in xhci_irq()
3217 if (xhci->isoc_bei_interval > AVOID_BEI_INTERVAL_MIN) in xhci_irq()
3218 xhci->isoc_bei_interval = xhci->isoc_bei_interval / 2; in xhci_irq()
3223 xhci_update_erst_dequeue(xhci, ir, event_ring_deq, true); in xhci_irq()
3227 spin_unlock(&xhci->lock); in xhci_irq()
3241 * Generic function for queueing a TRB on a ring.
3247 static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring, in queue_trb() argument
3253 trb = &ring->enqueue->generic; in queue_trb()
3254 trb->field[0] = cpu_to_le32(field1); in queue_trb()
3255 trb->field[1] = cpu_to_le32(field2); in queue_trb()
3256 trb->field[2] = cpu_to_le32(field3); in queue_trb()
3259 trb->field[3] = cpu_to_le32(field4); in queue_trb()
3263 inc_enq(xhci, ring, more_trbs_coming); in queue_trb()
3270 static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring, in prepare_ring() argument
3283 xhci_warn(xhci, "WARN urb submitted to disabled ep\n"); in prepare_ring()
3284 return -ENOENT; in prepare_ring()
3286 xhci_warn(xhci, "WARN waiting for error on ep to be cleared\n"); in prepare_ring()
3288 /* XXX not sure if this should be -ENOENT or not */ in prepare_ring()
3289 return -EINVAL; in prepare_ring()
3291 xhci_dbg(xhci, "WARN halted endpoint, queueing URB anyway.\n"); in prepare_ring()
3297 xhci_err(xhci, "ERROR unknown endpoint state for ep\n"); in prepare_ring()
3302 return -EINVAL; in prepare_ring()
3305 if (ep_ring != xhci->cmd_ring) { in prepare_ring()
3306 new_segs = xhci_ring_expansion_needed(xhci, ep_ring, num_trbs); in prepare_ring()
3307 } else if (xhci_num_trbs_free(xhci, ep_ring) <= num_trbs) { in prepare_ring()
3308 xhci_err(xhci, "Do not support expand command ring\n"); in prepare_ring()
3309 return -ENOMEM; in prepare_ring()
3313 xhci_dbg_trace(xhci, trace_xhci_dbg_ring_expansion, in prepare_ring()
3315 if (xhci_ring_expansion(xhci, ep_ring, new_segs, mem_flags)) { in prepare_ring()
3316 xhci_err(xhci, "Ring expansion failed\n"); in prepare_ring()
3317 return -ENOMEM; in prepare_ring()
3321 while (trb_is_link(ep_ring->enqueue)) { in prepare_ring()
3325 if (!xhci_link_trb_quirk(xhci) && in prepare_ring()
3326 !(ep_ring->type == TYPE_ISOC && in prepare_ring()
3327 (xhci->quirks & XHCI_AMD_0x96_HOST))) in prepare_ring()
3328 ep_ring->enqueue->link.control &= in prepare_ring()
3331 ep_ring->enqueue->link.control |= in prepare_ring()
3335 ep_ring->enqueue->link.control ^= cpu_to_le32(TRB_CYCLE); in prepare_ring()
3338 if (link_trb_toggles_cycle(ep_ring->enqueue)) in prepare_ring()
3339 ep_ring->cycle_state ^= 1; in prepare_ring()
3341 ep_ring->enq_seg = ep_ring->enq_seg->next; in prepare_ring()
3342 ep_ring->enqueue = ep_ring->enq_seg->trbs; in prepare_ring()
3345 if (link_trb_count++ > ep_ring->num_segs) { in prepare_ring()
3346 xhci_warn(xhci, "Ring is an endless link TRB loop\n"); in prepare_ring()
3347 return -EINVAL; in prepare_ring()
3351 if (last_trb_on_seg(ep_ring->enq_seg, ep_ring->enqueue)) { in prepare_ring()
3352 xhci_warn(xhci, "Missing link TRB at end of ring segment\n"); in prepare_ring()
3353 return -EINVAL; in prepare_ring()
3359 static int prepare_transfer(struct xhci_hcd *xhci, in prepare_transfer() argument
3372 struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index); in prepare_transfer()
3374 ep_ring = xhci_triad_to_transfer_ring(xhci, xdev->slot_id, ep_index, in prepare_transfer()
3377 xhci_dbg(xhci, "Can't prepare ring for bad stream ID %u\n", in prepare_transfer()
3379 return -EINVAL; in prepare_transfer()
3382 ret = prepare_ring(xhci, ep_ring, GET_EP_CTX_STATE(ep_ctx), in prepare_transfer()
3387 urb_priv = urb->hcpriv; in prepare_transfer()
3388 td = &urb_priv->td[td_index]; in prepare_transfer()
3390 INIT_LIST_HEAD(&td->td_list); in prepare_transfer()
3391 INIT_LIST_HEAD(&td->cancelled_td_list); in prepare_transfer()
3394 ret = usb_hcd_link_urb_to_ep(bus_to_hcd(urb->dev->bus), urb); in prepare_transfer()
3399 td->urb = urb; in prepare_transfer()
3401 list_add_tail(&td->td_list, &ep_ring->td_list); in prepare_transfer()
3402 td->start_seg = ep_ring->enq_seg; in prepare_transfer()
3403 td->first_trb = ep_ring->enqueue; in prepare_transfer()
3412 num_trbs = DIV_ROUND_UP(len + (addr & (TRB_MAX_BUFF_SIZE - 1)), in count_trbs()
3422 return count_trbs(urb->transfer_dma, urb->transfer_buffer_length); in count_trbs_needed()
3430 full_len = urb->transfer_buffer_length; in count_sg_trbs_needed()
3432 for_each_sg(urb->sg, sg, urb->num_mapped_sgs, i) { in count_sg_trbs_needed()
3436 full_len -= len; in count_sg_trbs_needed()
3448 addr = (u64) (urb->transfer_dma + urb->iso_frame_desc[i].offset); in count_isoc_trbs_needed()
3449 len = urb->iso_frame_desc[i].length; in count_isoc_trbs_needed()
3456 if (unlikely(running_total != urb->transfer_buffer_length)) in check_trb_math()
3457 dev_err(&urb->dev->dev, "%s - ep %#x - Miscalculated tx length, " in check_trb_math()
3460 urb->ep->desc.bEndpointAddress, in check_trb_math()
3462 urb->transfer_buffer_length, in check_trb_math()
3463 urb->transfer_buffer_length); in check_trb_math()
3466 static void giveback_first_trb(struct xhci_hcd *xhci, int slot_id, in giveback_first_trb() argument
3476 start_trb->field[3] |= cpu_to_le32(start_cycle); in giveback_first_trb()
3478 start_trb->field[3] &= cpu_to_le32(~TRB_CYCLE); in giveback_first_trb()
3479 xhci_ring_ep_doorbell(xhci, slot_id, ep_index, stream_id); in giveback_first_trb()
3482 static void check_interval(struct xhci_hcd *xhci, struct urb *urb, in check_interval() argument
3488 xhci_interval = EP_INTERVAL_TO_UFRAMES(le32_to_cpu(ep_ctx->ep_info)); in check_interval()
3489 ep_interval = urb->interval; in check_interval()
3492 if (urb->dev->speed == USB_SPEED_LOW || in check_interval()
3493 urb->dev->speed == USB_SPEED_FULL) in check_interval()
3500 dev_dbg_ratelimited(&urb->dev->dev, in check_interval()
3501 "Driver uses different interval (%d microframe%s) than xHCI (%d microframe%s)\n", in check_interval()
3504 urb->interval = xhci_interval; in check_interval()
3506 if (urb->dev->speed == USB_SPEED_LOW || in check_interval()
3507 urb->dev->speed == USB_SPEED_FULL) in check_interval()
3508 urb->interval /= 8; in check_interval()
3513 * xHCI uses normal TRBs for both bulk and interrupt. When the interrupt
3518 int xhci_queue_intr_tx(struct xhci_hcd *xhci, gfp_t mem_flags, in xhci_queue_intr_tx() argument
3523 ep_ctx = xhci_get_ep_ctx(xhci, xhci->devs[slot_id]->out_ctx, ep_index); in xhci_queue_intr_tx()
3524 check_interval(xhci, urb, ep_ctx); in xhci_queue_intr_tx()
3526 return xhci_queue_bulk_tx(xhci, mem_flags, urb, slot_id, ep_index); in xhci_queue_intr_tx()
3530 * For xHCI 1.0 host controllers, TD size is the number of max packet sized
3539 * TD size = total_packet_count - packets_transferred
3541 * For xHCI 0.96 and older, TD size field should be the remaining bytes
3549 static u32 xhci_td_remainder(struct xhci_hcd *xhci, int transferred, in xhci_td_remainder() argument
3555 /* MTK xHCI 0.96 contains some features from 1.0 */ in xhci_td_remainder()
3556 if (xhci->hci_version < 0x100 && !(xhci->quirks & XHCI_MTK_HOST)) in xhci_td_remainder()
3557 return ((td_total_len - transferred) >> 10); in xhci_td_remainder()
3559 /* One TRB with a zero-length data packet. */ in xhci_td_remainder()
3564 /* for MTK xHCI 0.96, TD size include this TRB, but not in 1.x */ in xhci_td_remainder()
3565 if ((xhci->quirks & XHCI_MTK_HOST) && (xhci->hci_version < 0x100)) in xhci_td_remainder()
3568 maxp = usb_endpoint_maxp(&urb->ep->desc); in xhci_td_remainder()
3572 return (total_packet_count - ((transferred + trb_buff_len) / maxp)); in xhci_td_remainder()
3576 static int xhci_align_td(struct xhci_hcd *xhci, struct urb *urb, u32 enqd_len, in xhci_align_td() argument
3579 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_align_td()
3585 max_pkt = usb_endpoint_maxp(&urb->ep->desc); in xhci_align_td()
3592 xhci_dbg(xhci, "Unaligned %d bytes, buff len %d\n", in xhci_align_td()
3597 *trb_buff_len -= unalign; in xhci_align_td()
3598 xhci_dbg(xhci, "split align, new buff len %d\n", *trb_buff_len); in xhci_align_td()
3607 new_buff_len = max_pkt - (enqd_len % max_pkt); in xhci_align_td()
3609 if (new_buff_len > (urb->transfer_buffer_length - enqd_len)) in xhci_align_td()
3610 new_buff_len = (urb->transfer_buffer_length - enqd_len); in xhci_align_td()
3614 if (urb->num_sgs) { in xhci_align_td()
3615 len = sg_pcopy_to_buffer(urb->sg, urb->num_sgs, in xhci_align_td()
3616 seg->bounce_buf, new_buff_len, enqd_len); in xhci_align_td()
3618 xhci_warn(xhci, "WARN Wrong bounce buffer write length: %zu != %d\n", in xhci_align_td()
3621 memcpy(seg->bounce_buf, urb->transfer_buffer + enqd_len, new_buff_len); in xhci_align_td()
3624 seg->bounce_dma = dma_map_single(dev, seg->bounce_buf, in xhci_align_td()
3627 seg->bounce_dma = dma_map_single(dev, seg->bounce_buf, in xhci_align_td()
3631 if (dma_mapping_error(dev, seg->bounce_dma)) { in xhci_align_td()
3633 xhci_warn(xhci, "Failed mapping bounce buffer, not aligning\n"); in xhci_align_td()
3637 seg->bounce_len = new_buff_len; in xhci_align_td()
3638 seg->bounce_offs = enqd_len; in xhci_align_td()
3640 xhci_dbg(xhci, "Bounce align, new buff len %d\n", *trb_buff_len); in xhci_align_td()
3645 /* This is very similar to what ehci-q.c qtd_fill() does */
3646 int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags, in xhci_queue_bulk_tx() argument
3664 ring = xhci_urb_to_transfer_ring(xhci, urb); in xhci_queue_bulk_tx()
3666 return -EINVAL; in xhci_queue_bulk_tx()
3668 full_len = urb->transfer_buffer_length; in xhci_queue_bulk_tx()
3670 if (urb->num_sgs && !(urb->transfer_flags & URB_DMA_MAP_SINGLE)) { in xhci_queue_bulk_tx()
3671 num_sgs = urb->num_mapped_sgs; in xhci_queue_bulk_tx()
3672 sg = urb->sg; in xhci_queue_bulk_tx()
3678 addr = (u64) urb->transfer_dma; in xhci_queue_bulk_tx()
3681 ret = prepare_transfer(xhci, xhci->devs[slot_id], in xhci_queue_bulk_tx()
3682 ep_index, urb->stream_id, in xhci_queue_bulk_tx()
3687 urb_priv = urb->hcpriv; in xhci_queue_bulk_tx()
3689 /* Deal with URB_ZERO_PACKET - need one more td/trb */ in xhci_queue_bulk_tx()
3690 if (urb->transfer_flags & URB_ZERO_PACKET && urb_priv->num_tds > 1) in xhci_queue_bulk_tx()
3693 td = &urb_priv->td[0]; in xhci_queue_bulk_tx()
3700 start_trb = &ring->enqueue->generic; in xhci_queue_bulk_tx()
3701 start_cycle = ring->cycle_state; in xhci_queue_bulk_tx()
3704 /* Queue the TRBs, even if they are zero-length */ in xhci_queue_bulk_tx()
3714 trb_buff_len = full_len - enqd_len; in xhci_queue_bulk_tx()
3722 field |= ring->cycle_state; in xhci_queue_bulk_tx()
3729 if (trb_is_link(ring->enqueue + 1)) { in xhci_queue_bulk_tx()
3730 if (xhci_align_td(xhci, urb, enqd_len, in xhci_queue_bulk_tx()
3732 ring->enq_seg)) { in xhci_queue_bulk_tx()
3733 send_addr = ring->enq_seg->bounce_dma; in xhci_queue_bulk_tx()
3735 td->bounce_seg = ring->enq_seg; in xhci_queue_bulk_tx()
3743 td->last_trb = ring->enqueue; in xhci_queue_bulk_tx()
3744 td->last_trb_seg = ring->enq_seg; in xhci_queue_bulk_tx()
3746 memcpy(&send_addr, urb->transfer_buffer, in xhci_queue_bulk_tx()
3758 remainder = xhci_td_remainder(xhci, enqd_len, trb_buff_len, in xhci_queue_bulk_tx()
3765 queue_trb(xhci, ring, more_trbs_coming | need_zero_pkt, in xhci_queue_bulk_tx()
3770 td->num_trbs++; in xhci_queue_bulk_tx()
3776 --num_sgs; in xhci_queue_bulk_tx()
3777 sent_len -= block_len; in xhci_queue_bulk_tx()
3785 block_len -= sent_len; in xhci_queue_bulk_tx()
3790 ret = prepare_transfer(xhci, xhci->devs[slot_id], in xhci_queue_bulk_tx()
3791 ep_index, urb->stream_id, in xhci_queue_bulk_tx()
3793 urb_priv->td[1].last_trb = ring->enqueue; in xhci_queue_bulk_tx()
3794 urb_priv->td[1].last_trb_seg = ring->enq_seg; in xhci_queue_bulk_tx()
3795 field = TRB_TYPE(TRB_NORMAL) | ring->cycle_state | TRB_IOC; in xhci_queue_bulk_tx()
3796 queue_trb(xhci, ring, 0, 0, 0, TRB_INTR_TARGET(0), field); in xhci_queue_bulk_tx()
3797 urb_priv->td[1].num_trbs++; in xhci_queue_bulk_tx()
3801 giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id, in xhci_queue_bulk_tx()
3806 /* Caller must have locked xhci->lock */
3807 int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags, in xhci_queue_ctrl_tx() argument
3820 ep_ring = xhci_urb_to_transfer_ring(xhci, urb); in xhci_queue_ctrl_tx()
3822 return -EINVAL; in xhci_queue_ctrl_tx()
3828 if (!urb->setup_packet) in xhci_queue_ctrl_tx()
3829 return -EINVAL; in xhci_queue_ctrl_tx()
3831 if ((xhci->quirks & XHCI_ETRON_HOST) && in xhci_queue_ctrl_tx()
3832 urb->dev->speed >= USB_SPEED_SUPER) { in xhci_queue_ctrl_tx()
3838 if (trb_is_link(ep_ring->enqueue + 1)) { in xhci_queue_ctrl_tx()
3839 field = TRB_TYPE(TRB_TR_NOOP) | ep_ring->cycle_state; in xhci_queue_ctrl_tx()
3840 queue_trb(xhci, ep_ring, false, 0, 0, in xhci_queue_ctrl_tx()
3852 if (urb->transfer_buffer_length > 0) in xhci_queue_ctrl_tx()
3854 ret = prepare_transfer(xhci, xhci->devs[slot_id], in xhci_queue_ctrl_tx()
3855 ep_index, urb->stream_id, in xhci_queue_ctrl_tx()
3860 urb_priv = urb->hcpriv; in xhci_queue_ctrl_tx()
3861 td = &urb_priv->td[0]; in xhci_queue_ctrl_tx()
3862 td->num_trbs = num_trbs; in xhci_queue_ctrl_tx()
3869 start_trb = &ep_ring->enqueue->generic; in xhci_queue_ctrl_tx()
3870 start_cycle = ep_ring->cycle_state; in xhci_queue_ctrl_tx()
3872 /* Queue setup TRB - see section 6.4.1.2.1 */ in xhci_queue_ctrl_tx()
3874 setup = (struct usb_ctrlrequest *) urb->setup_packet; in xhci_queue_ctrl_tx()
3880 /* xHCI 1.0/1.1 6.4.1.2.1: Transfer Type field */ in xhci_queue_ctrl_tx()
3881 if ((xhci->hci_version >= 0x100) || (xhci->quirks & XHCI_MTK_HOST)) { in xhci_queue_ctrl_tx()
3882 if (urb->transfer_buffer_length > 0) { in xhci_queue_ctrl_tx()
3883 if (setup->bRequestType & USB_DIR_IN) in xhci_queue_ctrl_tx()
3890 queue_trb(xhci, ep_ring, true, in xhci_queue_ctrl_tx()
3891 setup->bRequestType | setup->bRequest << 8 | le16_to_cpu(setup->wValue) << 16, in xhci_queue_ctrl_tx()
3892 le16_to_cpu(setup->wIndex) | le16_to_cpu(setup->wLength) << 16, in xhci_queue_ctrl_tx()
3904 if (urb->transfer_buffer_length > 0) { in xhci_queue_ctrl_tx()
3909 memcpy(&addr, urb->transfer_buffer, in xhci_queue_ctrl_tx()
3910 urb->transfer_buffer_length); in xhci_queue_ctrl_tx()
3914 addr = (u64) urb->transfer_dma; in xhci_queue_ctrl_tx()
3917 remainder = xhci_td_remainder(xhci, 0, in xhci_queue_ctrl_tx()
3918 urb->transfer_buffer_length, in xhci_queue_ctrl_tx()
3919 urb->transfer_buffer_length, in xhci_queue_ctrl_tx()
3921 length_field = TRB_LEN(urb->transfer_buffer_length) | in xhci_queue_ctrl_tx()
3924 if (setup->bRequestType & USB_DIR_IN) in xhci_queue_ctrl_tx()
3926 queue_trb(xhci, ep_ring, true, in xhci_queue_ctrl_tx()
3930 field | ep_ring->cycle_state); in xhci_queue_ctrl_tx()
3934 td->last_trb = ep_ring->enqueue; in xhci_queue_ctrl_tx()
3935 td->last_trb_seg = ep_ring->enq_seg; in xhci_queue_ctrl_tx()
3937 /* Queue status TRB - see Table 7 and sections 4.11.2.2 and 6.4.1.2.3 */ in xhci_queue_ctrl_tx()
3939 if (urb->transfer_buffer_length > 0 && setup->bRequestType & USB_DIR_IN) in xhci_queue_ctrl_tx()
3943 queue_trb(xhci, ep_ring, false, in xhci_queue_ctrl_tx()
3948 field | TRB_IOC | TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state); in xhci_queue_ctrl_tx()
3950 giveback_first_trb(xhci, slot_id, ep_index, 0, in xhci_queue_ctrl_tx()
3961 * zero. Only xHCI 1.0 host controllers support this field.
3963 static unsigned int xhci_get_burst_count(struct xhci_hcd *xhci, in xhci_get_burst_count() argument
3968 if (xhci->hci_version < 0x100 || urb->dev->speed < USB_SPEED_SUPER) in xhci_get_burst_count()
3971 max_burst = urb->ep->ss_ep_comp.bMaxBurst; in xhci_get_burst_count()
3972 return DIV_ROUND_UP(total_packet_count, max_burst + 1) - 1; in xhci_get_burst_count()
3983 static unsigned int xhci_get_last_burst_packet_count(struct xhci_hcd *xhci, in xhci_get_last_burst_packet_count() argument
3989 if (xhci->hci_version < 0x100) in xhci_get_last_burst_packet_count()
3992 if (urb->dev->speed >= USB_SPEED_SUPER) { in xhci_get_last_burst_packet_count()
3994 max_burst = urb->ep->ss_ep_comp.bMaxBurst; in xhci_get_last_burst_packet_count()
3997 * number of packets, but the TLBPC field is zero-based. in xhci_get_last_burst_packet_count()
4001 return residue - 1; in xhci_get_last_burst_packet_count()
4005 return total_packet_count - 1; in xhci_get_last_burst_packet_count()
4015 static int xhci_get_isoc_frame_id(struct xhci_hcd *xhci, in xhci_get_isoc_frame_id() argument
4021 if (urb->dev->speed == USB_SPEED_LOW || in xhci_get_isoc_frame_id()
4022 urb->dev->speed == USB_SPEED_FULL) in xhci_get_isoc_frame_id()
4023 start_frame = urb->start_frame + index * urb->interval; in xhci_get_isoc_frame_id()
4025 start_frame = (urb->start_frame + index * urb->interval) >> 3; in xhci_get_isoc_frame_id()
4035 ist = HCS_IST(xhci->hcs_params2) & 0x7; in xhci_get_isoc_frame_id()
4036 if (HCS_IST(xhci->hcs_params2) & (1 << 3)) in xhci_get_isoc_frame_id()
4052 current_frame_id = readl(&xhci->run_regs->microframe_index); in xhci_get_isoc_frame_id()
4060 xhci_dbg(xhci, "%s: index %d, reg 0x%x start_frame_id 0x%x, end_frame_id 0x%x, start_frame 0x%x\n", in xhci_get_isoc_frame_id()
4061 __func__, index, readl(&xhci->run_regs->microframe_index), in xhci_get_isoc_frame_id()
4067 ret = -EINVAL; in xhci_get_isoc_frame_id()
4071 ret = -EINVAL; in xhci_get_isoc_frame_id()
4073 ret = -EINVAL; in xhci_get_isoc_frame_id()
4077 if (ret == -EINVAL || start_frame == start_frame_id) { in xhci_get_isoc_frame_id()
4079 if (urb->dev->speed == USB_SPEED_LOW || in xhci_get_isoc_frame_id()
4080 urb->dev->speed == USB_SPEED_FULL) in xhci_get_isoc_frame_id()
4081 urb->start_frame = start_frame; in xhci_get_isoc_frame_id()
4083 urb->start_frame = start_frame << 3; in xhci_get_isoc_frame_id()
4089 xhci_warn(xhci, "Frame ID %d (reg %d, index %d) beyond range (%d, %d)\n", in xhci_get_isoc_frame_id()
4092 xhci_warn(xhci, "Ignore frame ID field, use SIA bit instead\n"); in xhci_get_isoc_frame_id()
4100 static bool trb_block_event_intr(struct xhci_hcd *xhci, int num_tds, int i) in trb_block_event_intr() argument
4102 if (xhci->hci_version < 0x100) in trb_block_event_intr()
4105 if (i == num_tds - 1) in trb_block_event_intr()
4111 if (i && xhci->quirks & XHCI_AVOID_BEI) in trb_block_event_intr()
4112 return !!(i % xhci->isoc_bei_interval); in trb_block_event_intr()
4118 static int xhci_queue_isoc_tx(struct xhci_hcd *xhci, gfp_t mem_flags, in xhci_queue_isoc_tx() argument
4136 xep = &xhci->devs[slot_id]->eps[ep_index]; in xhci_queue_isoc_tx()
4137 ep_ring = xhci->devs[slot_id]->eps[ep_index].ring; in xhci_queue_isoc_tx()
4139 num_tds = urb->number_of_packets; in xhci_queue_isoc_tx()
4141 xhci_dbg(xhci, "Isoc URB with zero packets?\n"); in xhci_queue_isoc_tx()
4142 return -EINVAL; in xhci_queue_isoc_tx()
4144 start_addr = (u64) urb->transfer_dma; in xhci_queue_isoc_tx()
4145 start_trb = &ep_ring->enqueue->generic; in xhci_queue_isoc_tx()
4146 start_cycle = ep_ring->cycle_state; in xhci_queue_isoc_tx()
4148 urb_priv = urb->hcpriv; in xhci_queue_isoc_tx()
4149 /* Queue the TRBs for each TD, even if they are zero-length */ in xhci_queue_isoc_tx()
4157 addr = start_addr + urb->iso_frame_desc[i].offset; in xhci_queue_isoc_tx()
4158 td_len = urb->iso_frame_desc[i].length; in xhci_queue_isoc_tx()
4160 max_pkt = usb_endpoint_maxp(&urb->ep->desc); in xhci_queue_isoc_tx()
4163 /* A zero-length transfer still involves at least one packet. */ in xhci_queue_isoc_tx()
4166 burst_count = xhci_get_burst_count(xhci, urb, total_pkt_count); in xhci_queue_isoc_tx()
4167 last_burst_pkt_count = xhci_get_last_burst_packet_count(xhci, in xhci_queue_isoc_tx()
4172 ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index, in xhci_queue_isoc_tx()
4173 urb->stream_id, trbs_per_td, urb, i, mem_flags); in xhci_queue_isoc_tx()
4179 td = &urb_priv->td[i]; in xhci_queue_isoc_tx()
4180 td->num_trbs = trbs_per_td; in xhci_queue_isoc_tx()
4183 if (!(urb->transfer_flags & URB_ISO_ASAP) && in xhci_queue_isoc_tx()
4184 HCC_CFC(xhci->hcc_params)) { in xhci_queue_isoc_tx()
4185 frame_id = xhci_get_isoc_frame_id(xhci, urb, i); in xhci_queue_isoc_tx()
4197 (i ? ep_ring->cycle_state : !start_cycle); in xhci_queue_isoc_tx()
4199 /* xhci 1.1 with ETE uses TD_Size field for TBC, old is Rsvdz */ in xhci_queue_isoc_tx()
4200 if (!xep->use_extended_tbc) in xhci_queue_isoc_tx()
4210 ep_ring->cycle_state; in xhci_queue_isoc_tx()
4217 if (j < trbs_per_td - 1) { in xhci_queue_isoc_tx()
4222 td->last_trb = ep_ring->enqueue; in xhci_queue_isoc_tx()
4223 td->last_trb_seg = ep_ring->enq_seg; in xhci_queue_isoc_tx()
4225 if (trb_block_event_intr(xhci, num_tds, i)) in xhci_queue_isoc_tx()
4234 remainder = xhci_td_remainder(xhci, running_total, in xhci_queue_isoc_tx()
4241 /* xhci 1.1 with ETE uses TD Size field for TBC */ in xhci_queue_isoc_tx()
4242 if (first_trb && xep->use_extended_tbc) in xhci_queue_isoc_tx()
4248 queue_trb(xhci, ep_ring, more_trbs_coming, in xhci_queue_isoc_tx()
4256 td_remain_len -= trb_buff_len; in xhci_queue_isoc_tx()
4261 xhci_err(xhci, "ISOC TD length unmatch\n"); in xhci_queue_isoc_tx()
4262 ret = -EINVAL; in xhci_queue_isoc_tx()
4268 if (HCC_CFC(xhci->hcc_params)) in xhci_queue_isoc_tx()
4269 xep->next_frame_id = urb->start_frame + num_tds * urb->interval; in xhci_queue_isoc_tx()
4271 if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs == 0) { in xhci_queue_isoc_tx()
4272 if (xhci->quirks & XHCI_AMD_PLL_FIX) in xhci_queue_isoc_tx()
4275 xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs++; in xhci_queue_isoc_tx()
4277 giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id, in xhci_queue_isoc_tx()
4283 for (i--; i >= 0; i--) in xhci_queue_isoc_tx()
4284 list_del_init(&urb_priv->td[i].td_list); in xhci_queue_isoc_tx()
4287 * into No-ops with a software-owned cycle bit. That way the hardware in xhci_queue_isoc_tx()
4289 * overwrite them. td->first_trb and td->start_seg are already set. in xhci_queue_isoc_tx()
4291 urb_priv->td[0].last_trb = ep_ring->enqueue; in xhci_queue_isoc_tx()
4293 td_to_noop(xhci, ep_ring, &urb_priv->td[0], true); in xhci_queue_isoc_tx()
4296 ep_ring->enqueue = urb_priv->td[0].first_trb; in xhci_queue_isoc_tx()
4297 ep_ring->enq_seg = urb_priv->td[0].start_seg; in xhci_queue_isoc_tx()
4298 ep_ring->cycle_state = start_cycle; in xhci_queue_isoc_tx()
4299 usb_hcd_unlink_urb_from_ep(bus_to_hcd(urb->dev->bus), urb); in xhci_queue_isoc_tx()
4306 * Update interval as xhci_queue_intr_tx does. Use xhci frame_index to
4307 * update urb->start_frame if URB_ISO_ASAP is set in transfer_flags or
4310 int xhci_queue_isoc_tx_prepare(struct xhci_hcd *xhci, gfp_t mem_flags, in xhci_queue_isoc_tx_prepare() argument
4322 xdev = xhci->devs[slot_id]; in xhci_queue_isoc_tx_prepare()
4323 xep = &xhci->devs[slot_id]->eps[ep_index]; in xhci_queue_isoc_tx_prepare()
4324 ep_ring = xdev->eps[ep_index].ring; in xhci_queue_isoc_tx_prepare()
4325 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index); in xhci_queue_isoc_tx_prepare()
4328 num_tds = urb->number_of_packets; in xhci_queue_isoc_tx_prepare()
4335 ret = prepare_ring(xhci, ep_ring, GET_EP_CTX_STATE(ep_ctx), in xhci_queue_isoc_tx_prepare()
4344 check_interval(xhci, urb, ep_ctx); in xhci_queue_isoc_tx_prepare()
4346 /* Calculate the start frame and put it in urb->start_frame. */ in xhci_queue_isoc_tx_prepare()
4347 if (HCC_CFC(xhci->hcc_params) && !list_empty(&ep_ring->td_list)) { in xhci_queue_isoc_tx_prepare()
4349 urb->start_frame = xep->next_frame_id; in xhci_queue_isoc_tx_prepare()
4354 start_frame = readl(&xhci->run_regs->microframe_index); in xhci_queue_isoc_tx_prepare()
4360 ist = HCS_IST(xhci->hcs_params2) & 0x7; in xhci_queue_isoc_tx_prepare()
4361 if (HCS_IST(xhci->hcs_params2) & (1 << 3)) in xhci_queue_isoc_tx_prepare()
4370 if (urb->dev->speed == USB_SPEED_LOW || in xhci_queue_isoc_tx_prepare()
4371 urb->dev->speed == USB_SPEED_FULL) { in xhci_queue_isoc_tx_prepare()
4372 start_frame = roundup(start_frame, urb->interval << 3); in xhci_queue_isoc_tx_prepare()
4373 urb->start_frame = start_frame >> 3; in xhci_queue_isoc_tx_prepare()
4375 start_frame = roundup(start_frame, urb->interval); in xhci_queue_isoc_tx_prepare()
4376 urb->start_frame = start_frame; in xhci_queue_isoc_tx_prepare()
4381 return xhci_queue_isoc_tx(xhci, mem_flags, urb, slot_id, ep_index); in xhci_queue_isoc_tx_prepare()
4386 /* Generic function for queueing a command TRB on the command ring.
4391 * Don't decrement xhci->cmd_ring_reserved_trbs after we've queued the TRB
4394 static int queue_command(struct xhci_hcd *xhci, struct xhci_command *cmd, in queue_command() argument
4398 int reserved_trbs = xhci->cmd_ring_reserved_trbs; in queue_command()
4401 if ((xhci->xhc_state & XHCI_STATE_DYING) || in queue_command()
4402 (xhci->xhc_state & XHCI_STATE_HALTED)) { in queue_command()
4403 xhci_dbg(xhci, "xHCI dying or halted, can't queue_command\n"); in queue_command()
4404 return -ESHUTDOWN; in queue_command()
4410 ret = prepare_ring(xhci, xhci->cmd_ring, EP_STATE_RUNNING, in queue_command()
4413 xhci_err(xhci, "ERR: No room for command on command ring\n"); in queue_command()
4415 xhci_err(xhci, "ERR: Reserved TRB counting for " in queue_command()
4420 cmd->command_trb = xhci->cmd_ring->enqueue; in queue_command()
4423 if (list_empty(&xhci->cmd_list)) { in queue_command()
4424 xhci->current_cmd = cmd; in queue_command()
4425 xhci_mod_cmd_timer(xhci); in queue_command()
4428 list_add_tail(&cmd->cmd_list, &xhci->cmd_list); in queue_command()
4430 queue_trb(xhci, xhci->cmd_ring, false, field1, field2, field3, in queue_command()
4431 field4 | xhci->cmd_ring->cycle_state); in queue_command()
4436 int xhci_queue_slot_control(struct xhci_hcd *xhci, struct xhci_command *cmd, in xhci_queue_slot_control() argument
4439 return queue_command(xhci, cmd, 0, 0, 0, in xhci_queue_slot_control()
4444 int xhci_queue_address_device(struct xhci_hcd *xhci, struct xhci_command *cmd, in xhci_queue_address_device() argument
4447 return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr), in xhci_queue_address_device()
4453 int xhci_queue_vendor_command(struct xhci_hcd *xhci, struct xhci_command *cmd, in xhci_queue_vendor_command() argument
4456 return queue_command(xhci, cmd, field1, field2, field3, field4, false); in xhci_queue_vendor_command()
4460 int xhci_queue_reset_device(struct xhci_hcd *xhci, struct xhci_command *cmd, in xhci_queue_reset_device() argument
4463 return queue_command(xhci, cmd, 0, 0, 0, in xhci_queue_reset_device()
4469 int xhci_queue_configure_endpoint(struct xhci_hcd *xhci, in xhci_queue_configure_endpoint() argument
4473 return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr), in xhci_queue_configure_endpoint()
4480 int xhci_queue_evaluate_context(struct xhci_hcd *xhci, struct xhci_command *cmd, in xhci_queue_evaluate_context() argument
4483 return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr), in xhci_queue_evaluate_context()
4493 int xhci_queue_stop_endpoint(struct xhci_hcd *xhci, struct xhci_command *cmd, in xhci_queue_stop_endpoint() argument
4501 return queue_command(xhci, cmd, 0, 0, 0, in xhci_queue_stop_endpoint()
4505 int xhci_queue_reset_ep(struct xhci_hcd *xhci, struct xhci_command *cmd, in xhci_queue_reset_ep() argument
4516 return queue_command(xhci, cmd, 0, 0, 0, in xhci_queue_reset_ep()