Lines Matching +full:xdma +full:- +full:host +full:- +full:3
1 // SPDX-License-Identifier: GPL-2.0
3 * xHCI host controller driver
16 * 3. Enqueue pointer = dequeue pointer = address of first TRB in the segment.
26 * until you reach a non-link TRB.
27 * 3. A ring is full if enqueue++ (for the definition of increment above)
41 * 3. Notify consumer. If SW is producer, it rings the doorbell for command
50 * 3. Notify the producer. SW is the consumer for the event ring, and it
59 #include <linux/dma-mapping.h>
61 #include "xhci-trace.h"
76 if (!seg || !trb || trb < seg->trbs) in xhci_trb_virt_to_dma()
79 segment_offset = trb - seg->trbs; in xhci_trb_virt_to_dma()
82 return seg->dma + (segment_offset * sizeof(*trb)); in xhci_trb_virt_to_dma()
87 return TRB_TYPE_NOOP_LE32(trb->generic.field[3]); in trb_is_noop()
92 return TRB_TYPE_LINK_LE32(trb->link.control); in trb_is_link()
97 return trb == &seg->trbs[TRBS_PER_SEGMENT - 1]; in last_trb_on_seg()
103 return last_trb_on_seg(seg, trb) && (seg->next == ring->first_seg); in last_trb_on_ring()
108 return le32_to_cpu(trb->link.control) & LINK_TOGGLE; in link_trb_toggles_cycle()
113 struct urb_priv *urb_priv = td->urb->hcpriv; in last_td_in_urb()
115 return urb_priv->num_tds_done == urb_priv->num_tds; in last_td_in_urb()
120 return ((le32_to_cpu(ring->dequeue->event_cmd.flags) & TRB_CYCLE) == in unhandled_event_trb()
121 ring->cycle_state); in unhandled_event_trb()
126 struct urb_priv *urb_priv = urb->hcpriv; in inc_td_cnt()
128 urb_priv->num_tds_done++; in inc_td_cnt()
135 trb->link.control &= cpu_to_le32(~TRB_CHAIN); in trb_to_noop()
137 trb->generic.field[0] = 0; in trb_to_noop()
138 trb->generic.field[1] = 0; in trb_to_noop()
139 trb->generic.field[2] = 0; in trb_to_noop()
141 trb->generic.field[3] &= cpu_to_le32(TRB_CYCLE); in trb_to_noop()
142 trb->generic.field[3] |= cpu_to_le32(TRB_TYPE(noop_type)); in trb_to_noop()
154 *seg = (*seg)->next; in next_trb()
155 *trb = ((*seg)->trbs); in next_trb()
169 if (ring->type == TYPE_EVENT) { in inc_deq()
170 if (!last_trb_on_seg(ring->deq_seg, ring->dequeue)) { in inc_deq()
171 ring->dequeue++; in inc_deq()
174 if (last_trb_on_ring(ring, ring->deq_seg, ring->dequeue)) in inc_deq()
175 ring->cycle_state ^= 1; in inc_deq()
176 ring->deq_seg = ring->deq_seg->next; in inc_deq()
177 ring->dequeue = ring->deq_seg->trbs; in inc_deq()
185 if (!trb_is_link(ring->dequeue)) { in inc_deq()
186 if (last_trb_on_seg(ring->deq_seg, ring->dequeue)) in inc_deq()
189 ring->dequeue++; in inc_deq()
192 while (trb_is_link(ring->dequeue)) { in inc_deq()
193 ring->deq_seg = ring->deq_seg->next; in inc_deq()
194 ring->dequeue = ring->deq_seg->trbs; in inc_deq()
198 if (link_trb_count++ > ring->num_segs) { in inc_deq()
229 chain = le32_to_cpu(ring->enqueue->generic.field[3]) & TRB_CHAIN; in inc_enq()
231 if (last_trb_on_seg(ring->enq_seg, ring->enqueue)) { in inc_enq()
236 next = ++(ring->enqueue); in inc_enq()
252 * AMD 0.96 host, carry over the chain bit of the previous TRB in inc_enq()
255 if (!xhci_link_chain_quirk(xhci, ring->type)) { in inc_enq()
256 next->link.control &= cpu_to_le32(~TRB_CHAIN); in inc_enq()
257 next->link.control |= cpu_to_le32(chain); in inc_enq()
261 next->link.control ^= cpu_to_le32(TRB_CYCLE); in inc_enq()
265 ring->cycle_state ^= 1; in inc_enq()
267 ring->enq_seg = ring->enq_seg->next; in inc_enq()
268 ring->enqueue = ring->enq_seg->trbs; in inc_enq()
269 next = ring->enqueue; in inc_enq()
273 if (link_trb_count++ > ring->num_segs) { in inc_enq()
288 struct xhci_segment *enq_seg = ring->enq_seg; in xhci_num_trbs_free()
289 union xhci_trb *enq = ring->enqueue; in xhci_num_trbs_free()
296 enq_seg = enq_seg->next; in xhci_num_trbs_free()
297 enq = enq_seg->trbs; in xhci_num_trbs_free()
301 if (enq == ring->dequeue) in xhci_num_trbs_free()
302 return ring->num_segs * (TRBS_PER_SEGMENT - 1); in xhci_num_trbs_free()
305 if (ring->deq_seg == enq_seg && ring->dequeue >= enq) in xhci_num_trbs_free()
306 return free + (ring->dequeue - enq); in xhci_num_trbs_free()
307 last_on_seg = &enq_seg->trbs[TRBS_PER_SEGMENT - 1]; in xhci_num_trbs_free()
308 free += last_on_seg - enq; in xhci_num_trbs_free()
309 enq_seg = enq_seg->next; in xhci_num_trbs_free()
310 enq = enq_seg->trbs; in xhci_num_trbs_free()
311 } while (i++ < ring->num_segs); in xhci_num_trbs_free()
330 enq_used = ring->enqueue - ring->enq_seg->trbs; in xhci_ring_expansion_needed()
333 trbs_past_seg = enq_used + num_trbs - (TRBS_PER_SEGMENT - 1); in xhci_ring_expansion_needed()
345 if (trb_is_link(ring->enqueue) && ring->enq_seg->next->trbs == ring->dequeue) in xhci_ring_expansion_needed()
348 new_segs = 1 + (trbs_past_seg / (TRBS_PER_SEGMENT - 1)); in xhci_ring_expansion_needed()
349 seg = ring->enq_seg; in xhci_ring_expansion_needed()
352 seg = seg->next; in xhci_ring_expansion_needed()
353 if (seg == ring->deq_seg) { in xhci_ring_expansion_needed()
358 new_segs--; in xhci_ring_expansion_needed()
364 /* Ring the host controller doorbell after placing a command on the ring */
367 if (!(xhci->cmd_ring_state & CMD_RING_STATE_RUNNING)) in xhci_ring_cmd_db()
374 writel(DB_VALUE_HOST, &xhci->dba->doorbell[0]); in xhci_ring_cmd_db()
376 readl(&xhci->dba->doorbell[0]); in xhci_ring_cmd_db()
381 return mod_delayed_work(system_wq, &xhci->cmd_timer, in xhci_mod_cmd_timer()
382 msecs_to_jiffies(xhci->current_cmd->timeout_ms)); in xhci_mod_cmd_timer()
387 return list_first_entry_or_null(&xhci->cmd_list, struct xhci_command, in xhci_next_queued_cmd()
392 * Turn all commands on command ring with status set to "aborted" to no-op trbs.
394 * This must be called with command ring stopped and xhci->lock held.
401 /* Turn all aborted commands in list to no-ops, then restart */ in xhci_handle_stopped_cmd_ring()
402 list_for_each_entry(i_cmd, &xhci->cmd_list, cmd_list) { in xhci_handle_stopped_cmd_ring()
404 if (i_cmd->status != COMP_COMMAND_ABORTED) in xhci_handle_stopped_cmd_ring()
407 i_cmd->status = COMP_COMMAND_RING_STOPPED; in xhci_handle_stopped_cmd_ring()
409 xhci_dbg(xhci, "Turn aborted command %p to no-op\n", in xhci_handle_stopped_cmd_ring()
410 i_cmd->command_trb); in xhci_handle_stopped_cmd_ring()
412 trb_to_noop(i_cmd->command_trb, TRB_CMD_NOOP); in xhci_handle_stopped_cmd_ring()
416 * completion event is received for these no-op commands in xhci_handle_stopped_cmd_ring()
420 xhci->cmd_ring_state = CMD_RING_STATE_RUNNING; in xhci_handle_stopped_cmd_ring()
423 if ((xhci->cmd_ring->dequeue != xhci->cmd_ring->enqueue) && in xhci_handle_stopped_cmd_ring()
424 !(xhci->xhc_state & XHCI_STATE_DYING)) { in xhci_handle_stopped_cmd_ring()
425 xhci->current_cmd = cur_cmd; in xhci_handle_stopped_cmd_ring()
432 /* Must be called with xhci->lock held, releases and acquires lock back */
435 struct xhci_segment *new_seg = xhci->cmd_ring->deq_seg; in xhci_abort_cmd_ring()
436 union xhci_trb *new_deq = xhci->cmd_ring->dequeue; in xhci_abort_cmd_ring()
442 reinit_completion(&xhci->cmd_ring_stop_completion); in xhci_abort_cmd_ring()
457 xhci_write_64(xhci, crcr | CMD_RING_ABORT, &xhci->op_regs->cmd_ring); in xhci_abort_cmd_ring()
461 * seconds then driver handles it as if host died (-ENODEV). in xhci_abort_cmd_ring()
462 * In the future we should distinguish between -ENODEV and -ETIMEDOUT in xhci_abort_cmd_ring()
463 * and try to recover a -ETIMEDOUT with a host controller reset. in xhci_abort_cmd_ring()
465 ret = xhci_handshake_check_state(xhci, &xhci->op_regs->cmd_ring, in xhci_abort_cmd_ring()
476 * however on some host hw the CMD_RING_RUNNING bit is correctly cleared in xhci_abort_cmd_ring()
480 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_abort_cmd_ring()
481 ret = wait_for_completion_timeout(&xhci->cmd_ring_stop_completion, in xhci_abort_cmd_ring()
483 spin_lock_irqsave(&xhci->lock, flags); in xhci_abort_cmd_ring()
498 __le32 __iomem *db_addr = &xhci->dba->doorbell[slot_id]; in xhci_ring_ep_doorbell()
499 struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index]; in xhci_ring_ep_doorbell()
500 unsigned int ep_state = ep->ep_state; in xhci_ring_ep_doorbell()
527 ep = &xhci->devs[slot_id]->eps[ep_index]; in ring_doorbell_for_active_rings()
530 if (!(ep->ep_state & EP_HAS_STREAMS)) { in ring_doorbell_for_active_rings()
531 if (ep->ring && !(list_empty(&ep->ring->td_list))) in ring_doorbell_for_active_rings()
536 for (stream_id = 1; stream_id < ep->stream_info->num_streams; in ring_doorbell_for_active_rings()
538 struct xhci_stream_info *stream_info = ep->stream_info; in ring_doorbell_for_active_rings()
539 if (!list_empty(&stream_info->stream_rings[stream_id]->td_list)) in ring_doorbell_for_active_rings()
564 if (!xhci->devs[slot_id]) { in xhci_get_virt_ep()
569 return &xhci->devs[slot_id]->eps[ep_index]; in xhci_get_virt_ep()
577 if (!(ep->ep_state & EP_HAS_STREAMS)) in xhci_virt_ep_to_ring()
578 return ep->ring; in xhci_virt_ep_to_ring()
580 if (!ep->stream_info) in xhci_virt_ep_to_ring()
583 if (stream_id == 0 || stream_id >= ep->stream_info->num_streams) { in xhci_virt_ep_to_ring()
585 stream_id, ep->vdev->slot_id, ep->ep_index); in xhci_virt_ep_to_ring()
589 return ep->stream_info->stream_rings[stream_id]; in xhci_virt_ep_to_ring()
623 ep = &vdev->eps[ep_index]; in xhci_get_hw_deq()
625 if (ep->ep_state & EP_HAS_STREAMS) { in xhci_get_hw_deq()
626 st_ctx = &ep->stream_info->stream_ctx_array[stream_id]; in xhci_get_hw_deq()
627 return le64_to_cpu(st_ctx->stream_ring); in xhci_get_hw_deq()
629 ep_ctx = xhci_get_ep_ctx(xhci, vdev->out_ctx, ep_index); in xhci_get_hw_deq()
630 return le64_to_cpu(ep_ctx->deq); in xhci_get_hw_deq()
637 struct xhci_virt_device *dev = xhci->devs[slot_id]; in xhci_move_dequeue_past_td()
638 struct xhci_virt_ep *ep = &dev->eps[ep_index]; in xhci_move_dequeue_past_td()
656 return -ENODEV; in xhci_move_dequeue_past_td()
660 new_seg = ep_ring->deq_seg; in xhci_move_dequeue_past_td()
661 new_deq = ep_ring->dequeue; in xhci_move_dequeue_past_td()
677 if (new_deq == td->end_trb) in xhci_move_dequeue_past_td()
687 if (new_deq == ep->ring->dequeue) { in xhci_move_dequeue_past_td()
689 return -EINVAL; in xhci_move_dequeue_past_td()
699 return -EINVAL; in xhci_move_dequeue_past_td()
702 if ((ep->ep_state & SET_DEQ_PENDING)) { in xhci_move_dequeue_past_td()
705 return -EBUSY; in xhci_move_dequeue_past_td()
712 return -ENOMEM; in xhci_move_dequeue_past_td()
726 ep->queued_deq_seg = new_seg; in xhci_move_dequeue_past_td()
727 ep->queued_deq_ptr = new_deq; in xhci_move_dequeue_past_td()
737 ep->ep_state |= SET_DEQ_PENDING; in xhci_move_dequeue_past_td()
748 struct xhci_segment *seg = td->start_seg; in td_to_noop()
749 union xhci_trb *trb = td->start_trb; in td_to_noop()
755 if (flip_cycle && trb != td->start_trb && trb != td->end_trb) in td_to_noop()
756 trb->generic.field[3] ^= cpu_to_le32(TRB_CYCLE); in td_to_noop()
758 if (trb == td->end_trb) in td_to_noop()
768 struct urb *urb = cur_td->urb; in xhci_giveback_urb_in_irq()
769 struct urb_priv *urb_priv = urb->hcpriv; in xhci_giveback_urb_in_irq()
770 struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus); in xhci_giveback_urb_in_irq()
772 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) { in xhci_giveback_urb_in_irq()
773 xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs--; in xhci_giveback_urb_in_irq()
774 if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs == 0) { in xhci_giveback_urb_in_irq()
775 if (xhci->quirks & XHCI_AMD_PLL_FIX) in xhci_giveback_urb_in_irq()
788 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_unmap_td_bounce_buffer()
789 struct xhci_segment *seg = td->bounce_seg; in xhci_unmap_td_bounce_buffer()
790 struct urb *urb = td->urb; in xhci_unmap_td_bounce_buffer()
797 dma_unmap_single(dev, seg->bounce_dma, ring->bounce_buf_len, in xhci_unmap_td_bounce_buffer()
802 dma_unmap_single(dev, seg->bounce_dma, ring->bounce_buf_len, in xhci_unmap_td_bounce_buffer()
805 if (urb->num_sgs) { in xhci_unmap_td_bounce_buffer()
806 len = sg_pcopy_from_buffer(urb->sg, urb->num_sgs, seg->bounce_buf, in xhci_unmap_td_bounce_buffer()
807 seg->bounce_len, seg->bounce_offs); in xhci_unmap_td_bounce_buffer()
808 if (len != seg->bounce_len) in xhci_unmap_td_bounce_buffer()
810 len, seg->bounce_len); in xhci_unmap_td_bounce_buffer()
812 memcpy(urb->transfer_buffer + seg->bounce_offs, seg->bounce_buf, in xhci_unmap_td_bounce_buffer()
813 seg->bounce_len); in xhci_unmap_td_bounce_buffer()
815 seg->bounce_len = 0; in xhci_unmap_td_bounce_buffer()
816 seg->bounce_offs = 0; in xhci_unmap_td_bounce_buffer()
825 urb = td->urb; in xhci_td_cleanup()
831 * If the host controller said we transferred more data than the buffer in xhci_td_cleanup()
832 * length, urb->actual_length will be a very big number (since it's in xhci_td_cleanup()
835 if (urb->actual_length > urb->transfer_buffer_length) { in xhci_td_cleanup()
837 urb->transfer_buffer_length, urb->actual_length); in xhci_td_cleanup()
838 urb->actual_length = 0; in xhci_td_cleanup()
842 if (!list_empty(&td->td_list)) in xhci_td_cleanup()
843 list_del_init(&td->td_list); in xhci_td_cleanup()
845 if (!list_empty(&td->cancelled_td_list)) in xhci_td_cleanup()
846 list_del_init(&td->cancelled_td_list); in xhci_td_cleanup()
851 if ((urb->actual_length != urb->transfer_buffer_length && in xhci_td_cleanup()
852 (urb->transfer_flags & URB_SHORT_NOT_OK)) || in xhci_td_cleanup()
853 (status != 0 && !usb_endpoint_xfer_isoc(&urb->ep->desc))) in xhci_td_cleanup()
855 urb, urb->actual_length, in xhci_td_cleanup()
856 urb->transfer_buffer_length, status); in xhci_td_cleanup()
859 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) in xhci_td_cleanup()
869 ring->dequeue = td->end_trb; in xhci_dequeue_td()
870 ring->deq_seg = td->end_seg; in xhci_dequeue_td()
882 list_for_each_entry_safe(td, tmp_td, &ep->cancelled_td_list, in xhci_giveback_invalidated_tds()
885 ring = xhci_urb_to_transfer_ring(ep->xhci, td->urb); in xhci_giveback_invalidated_tds()
887 if (td->cancel_status == TD_CLEARED) { in xhci_giveback_invalidated_tds()
888 xhci_dbg(ep->xhci, "%s: Giveback cancelled URB %p TD\n", in xhci_giveback_invalidated_tds()
889 __func__, td->urb); in xhci_giveback_invalidated_tds()
890 xhci_td_cleanup(ep->xhci, td, ring, td->status); in xhci_giveback_invalidated_tds()
892 xhci_dbg(ep->xhci, "%s: Keep cancelled URB %p TD as cancel_status is %d\n", in xhci_giveback_invalidated_tds()
893 __func__, td->urb, td->cancel_status); in xhci_giveback_invalidated_tds()
895 if (ep->xhci->xhc_state & XHCI_STATE_DYING) in xhci_giveback_invalidated_tds()
908 ret = -ENOMEM; in xhci_reset_halted_ep()
912 xhci_dbg(xhci, "%s-reset ep %u, slot %u\n", in xhci_reset_halted_ep()
929 unsigned int slot_id = ep->vdev->slot_id; in xhci_handle_halted_endpoint()
933 * Avoid resetting endpoint if link is inactive. Can cause host hang. in xhci_handle_halted_endpoint()
936 if (ep->vdev->flags & VDEV_PORT_ERROR) in xhci_handle_halted_endpoint()
937 return -ENODEV; in xhci_handle_halted_endpoint()
941 ep->ep_state |= EP_HARD_CLEAR_TOGGLE; in xhci_handle_halted_endpoint()
942 if (td && list_empty(&td->cancelled_td_list)) { in xhci_handle_halted_endpoint()
943 list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list); in xhci_handle_halted_endpoint()
944 td->cancel_status = TD_HALTED; in xhci_handle_halted_endpoint()
948 if (ep->ep_state & EP_HALTED) { in xhci_handle_halted_endpoint()
950 ep->ep_index); in xhci_handle_halted_endpoint()
954 err = xhci_reset_halted_ep(xhci, slot_id, ep->ep_index, reset_type); in xhci_handle_halted_endpoint()
958 ep->ep_state |= EP_HALTED; in xhci_handle_halted_endpoint()
968 * We're also in the event handler, so we can't get re-interrupted if another
982 unsigned int slot_id = ep->vdev->slot_id; in xhci_invalidate_cancelled_tds()
989 if (ep->ep_state & SET_DEQ_PENDING) in xhci_invalidate_cancelled_tds()
992 xhci = ep->xhci; in xhci_invalidate_cancelled_tds()
994 list_for_each_entry_safe(td, tmp_td, &ep->cancelled_td_list, cancelled_td_list) { in xhci_invalidate_cancelled_tds()
998 td->start_seg, td->start_trb), in xhci_invalidate_cancelled_tds()
999 td->urb->stream_id, td->urb); in xhci_invalidate_cancelled_tds()
1000 list_del_init(&td->td_list); in xhci_invalidate_cancelled_tds()
1001 ring = xhci_urb_to_transfer_ring(xhci, td->urb); in xhci_invalidate_cancelled_tds()
1004 td->urb, td->urb->stream_id); in xhci_invalidate_cancelled_tds()
1013 hw_deq = xhci_get_hw_deq(xhci, ep->vdev, ep->ep_index, in xhci_invalidate_cancelled_tds()
1014 td->urb->stream_id); in xhci_invalidate_cancelled_tds()
1017 if (td->cancel_status == TD_HALTED || trb_in_td(xhci, td, hw_deq, false)) { in xhci_invalidate_cancelled_tds()
1018 switch (td->cancel_status) { in xhci_invalidate_cancelled_tds()
1019 case TD_CLEARED: /* TD is already no-op */ in xhci_invalidate_cancelled_tds()
1026 if (cached_td->urb->stream_id != td->urb->stream_id) { in xhci_invalidate_cancelled_tds()
1030 td->urb->stream_id, td->urb); in xhci_invalidate_cancelled_tds()
1031 td->cancel_status = TD_CLEARING_CACHE_DEFERRED; in xhci_invalidate_cancelled_tds()
1038 td->urb, cached_td->urb, in xhci_invalidate_cancelled_tds()
1039 td->urb->stream_id); in xhci_invalidate_cancelled_tds()
1041 cached_td->cancel_status = TD_CLEARED; in xhci_invalidate_cancelled_tds()
1044 td->cancel_status = TD_CLEARING_CACHE; in xhci_invalidate_cancelled_tds()
1050 td->cancel_status = TD_CLEARED; in xhci_invalidate_cancelled_tds()
1058 err = xhci_move_dequeue_past_td(xhci, slot_id, ep->ep_index, in xhci_invalidate_cancelled_tds()
1059 cached_td->urb->stream_id, in xhci_invalidate_cancelled_tds()
1062 /* Failed to move past cached td, just set cached TDs to no-op */ in xhci_invalidate_cancelled_tds()
1063 list_for_each_entry_safe(td, tmp_td, &ep->cancelled_td_list, cancelled_td_list) { in xhci_invalidate_cancelled_tds()
1069 if (td->cancel_status != TD_CLEARING_CACHE && in xhci_invalidate_cancelled_tds()
1070 td->cancel_status != TD_CLEARING_CACHE_DEFERRED) in xhci_invalidate_cancelled_tds()
1073 td->urb); in xhci_invalidate_cancelled_tds()
1075 td->cancel_status = TD_CLEARED; in xhci_invalidate_cancelled_tds()
1086 * Call under xhci->lock on a stopped endpoint.
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, hw_deq, false)) in find_halted_td()
1120 * 2. Otherwise, we turn all the TRBs in the TD into No-op TRBs (with the chain
1134 if (unlikely(TRB_TO_SUSPEND_PORT(le32_to_cpu(trb->generic.field[3])))) { in xhci_handle_cmd_stop_ep()
1135 if (!xhci->devs[slot_id]) in xhci_handle_cmd_stop_ep()
1141 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3])); in xhci_handle_cmd_stop_ep()
1146 ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep_index); in xhci_handle_cmd_stop_ep()
1153 * reset the host side endpoint first. in xhci_handle_cmd_stop_ep()
1159 * Proper error code is unknown here, it would be -EPIPE if device side in xhci_handle_cmd_stop_ep()
1160 * of enadpoit halted (aka STALL), and -EPROTO if not (transaction error) in xhci_handle_cmd_stop_ep()
1161 * We use -EPROTO, if device is stalled it should return a stall error on in xhci_handle_cmd_stop_ep()
1162 * next transfer, which then will return -EPIPE, and device side stall is in xhci_handle_cmd_stop_ep()
1168 if (ep->ep_state & EP_HAS_STREAMS) { in xhci_handle_cmd_stop_ep()
1174 td->status = -EPROTO; in xhci_handle_cmd_stop_ep()
1180 ep->ep_state &= ~EP_STOP_CMD_PENDING; in xhci_handle_cmd_stop_ep()
1191 if (ep->ep_state & EP_HALTED) in xhci_handle_cmd_stop_ep()
1204 if (time_is_before_jiffies(ep->stop_time + msecs_to_jiffies(100))) in xhci_handle_cmd_stop_ep()
1214 ep->ep_state &= ~EP_STOP_CMD_PENDING; in xhci_handle_cmd_stop_ep()
1228 ep->ep_state &= ~EP_STOP_CMD_PENDING; in xhci_handle_cmd_stop_ep()
1240 list_for_each_entry_safe(cur_td, tmp, &ring->td_list, td_list) { in xhci_kill_ring_urbs()
1241 list_del_init(&cur_td->td_list); in xhci_kill_ring_urbs()
1243 if (!list_empty(&cur_td->cancelled_td_list)) in xhci_kill_ring_urbs()
1244 list_del_init(&cur_td->cancelled_td_list); in xhci_kill_ring_urbs()
1248 inc_td_cnt(cur_td->urb); in xhci_kill_ring_urbs()
1250 xhci_giveback_urb_in_irq(xhci, cur_td, -ESHUTDOWN); in xhci_kill_ring_urbs()
1266 if ((ep->ep_state & EP_HAS_STREAMS) || in xhci_kill_endpoint_urbs()
1267 (ep->ep_state & EP_GETTING_NO_STREAMS)) { in xhci_kill_endpoint_urbs()
1270 for (stream_id = 1; stream_id < ep->stream_info->num_streams; in xhci_kill_endpoint_urbs()
1272 ring = ep->stream_info->stream_rings[stream_id]; in xhci_kill_endpoint_urbs()
1282 ring = ep->ring; in xhci_kill_endpoint_urbs()
1291 list_for_each_entry_safe(cur_td, tmp, &ep->cancelled_td_list, in xhci_kill_endpoint_urbs()
1293 list_del_init(&cur_td->cancelled_td_list); in xhci_kill_endpoint_urbs()
1294 inc_td_cnt(cur_td->urb); in xhci_kill_endpoint_urbs()
1297 xhci_giveback_urb_in_irq(xhci, cur_td, -ESHUTDOWN); in xhci_kill_endpoint_urbs()
1302 * host controller died, register read returns 0xffffffff
1305 * held for the URBs to finish during device disconnect, blocking host remove.
1307 * Call with xhci->lock held.
1308 * lock is relased and re-acquired while giving back urb.
1314 if (xhci->xhc_state & XHCI_STATE_DYING) in xhci_hc_died()
1317 xhci_err(xhci, "xHCI host controller not responding, assume dead\n"); in xhci_hc_died()
1318 xhci->xhc_state |= XHCI_STATE_DYING; in xhci_hc_died()
1323 for (i = 0; i <= HCS_MAX_SLOTS(xhci->hcs_params1); i++) { in xhci_hc_died()
1324 if (!xhci->devs[i]) in xhci_hc_died()
1331 if (!(xhci->xhc_state & XHCI_STATE_REMOVING)) in xhci_hc_died()
1342 dequeue_temp = ep_ring->dequeue; in update_ring_for_set_deq_completion()
1344 /* If we get two back-to-back stalls, and the first stalled transfer in update_ring_for_set_deq_completion()
1348 * the segment into la-la-land. in update_ring_for_set_deq_completion()
1350 if (trb_is_link(ep_ring->dequeue)) { in update_ring_for_set_deq_completion()
1351 ep_ring->deq_seg = ep_ring->deq_seg->next; in update_ring_for_set_deq_completion()
1352 ep_ring->dequeue = ep_ring->deq_seg->trbs; in update_ring_for_set_deq_completion()
1355 while (ep_ring->dequeue != dev->eps[ep_index].queued_deq_ptr) { in update_ring_for_set_deq_completion()
1357 ep_ring->dequeue++; in update_ring_for_set_deq_completion()
1358 if (trb_is_link(ep_ring->dequeue)) { in update_ring_for_set_deq_completion()
1359 if (ep_ring->dequeue == in update_ring_for_set_deq_completion()
1360 dev->eps[ep_index].queued_deq_ptr) in update_ring_for_set_deq_completion()
1362 ep_ring->deq_seg = ep_ring->deq_seg->next; in update_ring_for_set_deq_completion()
1363 ep_ring->dequeue = ep_ring->deq_seg->trbs; in update_ring_for_set_deq_completion()
1365 if (ep_ring->dequeue == dequeue_temp) { in update_ring_for_set_deq_completion()
1391 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3])); in xhci_handle_cmd_set_deq()
1392 stream_id = TRB_TO_STREAM_ID(le32_to_cpu(trb->generic.field[2])); in xhci_handle_cmd_set_deq()
1405 ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep_index); in xhci_handle_cmd_set_deq()
1406 slot_ctx = xhci_get_slot_ctx(xhci, ep->vdev->out_ctx); in xhci_handle_cmd_set_deq()
1410 if (ep->ep_state & EP_HAS_STREAMS) { in xhci_handle_cmd_set_deq()
1411 stream_ctx = &ep->stream_info->stream_ctx_array[stream_id]; in xhci_handle_cmd_set_deq()
1412 trace_xhci_handle_cmd_set_deq_stream(ep->stream_info, stream_id); in xhci_handle_cmd_set_deq()
1426 slot_state = le32_to_cpu(slot_ctx->dev_state); in xhci_handle_cmd_set_deq()
1450 if (ep->ep_state & EP_HAS_STREAMS) { in xhci_handle_cmd_set_deq()
1451 deq = le64_to_cpu(stream_ctx->stream_ring) & SCTX_DEQ_MASK; in xhci_handle_cmd_set_deq()
1457 * Dequeue Pointer command which causes XDMA to skip in xhci_handle_cmd_set_deq()
1462 if (xhci->quirks & XHCI_CDNS_SCTX_QUIRK) { in xhci_handle_cmd_set_deq()
1463 stream_ctx->reserved[0] = 0; in xhci_handle_cmd_set_deq()
1464 stream_ctx->reserved[1] = 0; in xhci_handle_cmd_set_deq()
1467 deq = le64_to_cpu(ep_ctx->deq) & ~EP_CTX_CYCLE_MASK; in xhci_handle_cmd_set_deq()
1471 if (xhci_trb_virt_to_dma(ep->queued_deq_seg, in xhci_handle_cmd_set_deq()
1472 ep->queued_deq_ptr) == deq) { in xhci_handle_cmd_set_deq()
1476 update_ring_for_set_deq_completion(xhci, ep->vdev, in xhci_handle_cmd_set_deq()
1481 ep->queued_deq_seg, ep->queued_deq_ptr); in xhci_handle_cmd_set_deq()
1485 list_for_each_entry_safe(td, tmp_td, &ep->cancelled_td_list, in xhci_handle_cmd_set_deq()
1487 ep_ring = xhci_urb_to_transfer_ring(ep->xhci, td->urb); in xhci_handle_cmd_set_deq()
1488 if (td->cancel_status == TD_CLEARING_CACHE) { in xhci_handle_cmd_set_deq()
1489 td->cancel_status = TD_CLEARED; in xhci_handle_cmd_set_deq()
1490 xhci_dbg(ep->xhci, "%s: Giveback cancelled URB %p TD\n", in xhci_handle_cmd_set_deq()
1491 __func__, td->urb); in xhci_handle_cmd_set_deq()
1492 xhci_td_cleanup(ep->xhci, td, ep_ring, td->status); in xhci_handle_cmd_set_deq()
1494 xhci_dbg(ep->xhci, "%s: Keep cancelled URB %p TD as cancel_status is %d\n", in xhci_handle_cmd_set_deq()
1495 __func__, td->urb, td->cancel_status); in xhci_handle_cmd_set_deq()
1499 ep->ep_state &= ~SET_DEQ_PENDING; in xhci_handle_cmd_set_deq()
1500 ep->queued_deq_seg = NULL; in xhci_handle_cmd_set_deq()
1501 ep->queued_deq_ptr = NULL; in xhci_handle_cmd_set_deq()
1504 if (!list_empty(&ep->cancelled_td_list)) { in xhci_handle_cmd_set_deq()
1505 xhci_dbg(ep->xhci, "%s: Pending TDs to clear, continuing with invalidation\n", in xhci_handle_cmd_set_deq()
1514 xhci_dbg(ep->xhci, "%s: All TDs cleared, ring doorbell\n", __func__); in xhci_handle_cmd_set_deq()
1526 ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3])); in xhci_handle_cmd_reset_ep()
1531 ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep_index); in xhci_handle_cmd_reset_ep()
1544 ep->ep_state &= ~EP_HALTED; in xhci_handle_cmd_reset_ep()
1549 if ((le32_to_cpu(trb->generic.field[3])) & TRB_TSP) in xhci_handle_cmd_reset_ep()
1557 command->slot_id = slot_id; in xhci_handle_cmd_enable_slot()
1559 command->slot_id = 0; in xhci_handle_cmd_enable_slot()
1567 virt_dev = xhci->devs[slot_id]; in xhci_handle_cmd_disable_slot()
1571 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx); in xhci_handle_cmd_disable_slot()
1574 if (xhci->quirks & XHCI_EP_LIMIT_QUIRK) in xhci_handle_cmd_disable_slot()
1592 virt_dev = xhci->devs[slot_id]; in xhci_handle_cmd_config_ep()
1595 ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx); in xhci_handle_cmd_config_ep()
1601 add_flags = le32_to_cpu(ctrl_ctx->add_flags); in xhci_handle_cmd_config_ep()
1604 ep_index = xhci_last_valid_endpoint(add_flags) - 1; in xhci_handle_cmd_config_ep()
1606 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->out_ctx, ep_index); in xhci_handle_cmd_config_ep()
1617 vdev = xhci->devs[slot_id]; in xhci_handle_cmd_addr_dev()
1620 slot_ctx = xhci_get_slot_ctx(xhci, vdev->out_ctx); in xhci_handle_cmd_addr_dev()
1629 vdev = xhci->devs[slot_id]; in xhci_handle_cmd_reset_dev()
1635 slot_ctx = xhci_get_slot_ctx(xhci, vdev->out_ctx); in xhci_handle_cmd_reset_dev()
1644 if (!(xhci->quirks & XHCI_NEC_HOST)) { in xhci_handle_cmd_nec_get_fw()
1645 xhci_warn(xhci, "WARN NEC_GET_FW command on non-NEC host\n"); in xhci_handle_cmd_nec_get_fw()
1650 NEC_FW_MAJOR(le32_to_cpu(event->status)), in xhci_handle_cmd_nec_get_fw()
1651 NEC_FW_MINOR(le32_to_cpu(event->status))); in xhci_handle_cmd_nec_get_fw()
1656 list_del(&cmd->cmd_list); in xhci_complete_del_and_free_cmd()
1658 if (cmd->completion) { in xhci_complete_del_and_free_cmd()
1659 cmd->status = comp_code; in xhci_complete_del_and_free_cmd()
1660 cmd->comp_param = comp_param; in xhci_complete_del_and_free_cmd()
1661 complete(cmd->completion); in xhci_complete_del_and_free_cmd()
1670 xhci->current_cmd = NULL; in xhci_cleanup_command_queue()
1671 list_for_each_entry_safe(cur_cmd, tmp_cmd, &xhci->cmd_list, cmd_list) in xhci_cleanup_command_queue()
1686 spin_lock_irqsave(&xhci->lock, flags); in xhci_handle_command_timeout()
1692 if (!xhci->current_cmd || delayed_work_pending(&xhci->cmd_timer)) { in xhci_handle_command_timeout()
1693 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_handle_command_timeout()
1697 cmd_field3 = le32_to_cpu(xhci->current_cmd->command_trb->generic.field[3]); in xhci_handle_command_timeout()
1698 usbsts = readl(&xhci->op_regs->status); in xhci_handle_command_timeout()
1705 xhci_warn(xhci, "xHCI host not responding to stop endpoint command\n"); in xhci_handle_command_timeout()
1710 ep->ep_state &= ~EP_STOP_CMD_PENDING; in xhci_handle_command_timeout()
1718 xhci->current_cmd->status = COMP_COMMAND_ABORTED; in xhci_handle_command_timeout()
1721 hw_ring_state = xhci_read_64(xhci, &xhci->op_regs->cmd_ring); in xhci_handle_command_timeout()
1727 if ((xhci->cmd_ring_state & CMD_RING_STATE_RUNNING) && in xhci_handle_command_timeout()
1730 xhci->cmd_ring_state = CMD_RING_STATE_ABORTED; in xhci_handle_command_timeout()
1736 /* host removed. Bail out */ in xhci_handle_command_timeout()
1737 if (xhci->xhc_state & XHCI_STATE_REMOVING) { in xhci_handle_command_timeout()
1738 xhci_dbg(xhci, "host removed, ring start fail?\n"); in xhci_handle_command_timeout()
1746 xhci_handle_stopped_cmd_ring(xhci, xhci->current_cmd); in xhci_handle_command_timeout()
1749 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_handle_command_timeout()
1756 unsigned int slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags)); in handle_cmd_completion()
1757 u32 status = le32_to_cpu(event->status); in handle_cmd_completion()
1770 cmd_dma = le64_to_cpu(event->cmd_trb); in handle_cmd_completion()
1771 cmd_trb = xhci->cmd_ring->dequeue; in handle_cmd_completion()
1773 trace_xhci_handle_command(xhci->cmd_ring, &cmd_trb->generic, cmd_dma); in handle_cmd_completion()
1775 cmd_comp_code = GET_COMP_CODE(le32_to_cpu(event->status)); in handle_cmd_completion()
1779 complete_all(&xhci->cmd_ring_stop_completion); in handle_cmd_completion()
1783 cmd_dequeue_dma = xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg, in handle_cmd_completion()
1795 cmd = list_first_entry(&xhci->cmd_list, struct xhci_command, cmd_list); in handle_cmd_completion()
1797 cancel_delayed_work(&xhci->cmd_timer); in handle_cmd_completion()
1799 if (cmd->command_trb != xhci->cmd_ring->dequeue) { in handle_cmd_completion()
1806 * Host aborted the command ring, check if the current command was in handle_cmd_completion()
1812 xhci->cmd_ring_state = CMD_RING_STATE_STOPPED; in handle_cmd_completion()
1813 if (cmd->status == COMP_COMMAND_ABORTED) { in handle_cmd_completion()
1814 if (xhci->current_cmd == cmd) in handle_cmd_completion()
1815 xhci->current_cmd = NULL; in handle_cmd_completion()
1820 cmd_type = TRB_FIELD_TO_TYPE(le32_to_cpu(cmd_trb->generic.field[3])); in handle_cmd_completion()
1829 if (!cmd->completion) in handle_cmd_completion()
1839 le32_to_cpu(cmd_trb->generic.field[3]))); in handle_cmd_completion()
1840 if (!cmd->completion) in handle_cmd_completion()
1846 le32_to_cpu(cmd_trb->generic.field[3]))); in handle_cmd_completion()
1850 /* Is this an aborted command turned to NO-OP? */ in handle_cmd_completion()
1851 if (cmd->status == COMP_COMMAND_RING_STOPPED) in handle_cmd_completion()
1856 le32_to_cpu(cmd_trb->generic.field[3]))); in handle_cmd_completion()
1864 le32_to_cpu(cmd_trb->generic.field[3])); in handle_cmd_completion()
1877 if (!list_is_singular(&xhci->cmd_list)) { in handle_cmd_completion()
1878 xhci->current_cmd = list_first_entry(&cmd->cmd_list, in handle_cmd_completion()
1881 } else if (xhci->current_cmd == cmd) { in handle_cmd_completion()
1882 xhci->current_cmd = NULL; in handle_cmd_completion()
1888 inc_deq(xhci, xhci->cmd_ring); in handle_cmd_completion()
1895 if (trb_type == TRB_NEC_CMD_COMP && (xhci->quirks & XHCI_NEC_HOST)) in handle_vendor_event()
1896 handle_cmd_completion(xhci, &event->event_cmd); in handle_vendor_event()
1905 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->generic.field[3])); in handle_device_notification()
1906 if (!xhci->devs[slot_id]) { in handle_device_notification()
1914 udev = xhci->devs[slot_id]->udev; in handle_device_notification()
1915 if (udev && udev->parent) in handle_device_notification()
1916 usb_wakeup_notification(udev->parent, udev->portnum); in handle_device_notification()
1922 * As per ThunderX2errata-129 USB 2 device may come up as USB 1
1939 writel(0x6F, hcd->regs + 0x1048); in xhci_cavium_reset_phy_quirk()
1941 /* De-assert the PHY reset */ in xhci_cavium_reset_phy_quirk()
1942 writel(0x7F, hcd->regs + 0x1048); in xhci_cavium_reset_phy_quirk()
1944 pll_lock_check = readl(hcd->regs + 0x1070); in xhci_cavium_reset_phy_quirk()
1945 } while (!(pll_lock_check & 0x1) && --retry_count); in xhci_cavium_reset_phy_quirk()
1960 if (GET_COMP_CODE(le32_to_cpu(event->generic.field[2])) != COMP_SUCCESS) in handle_port_status()
1964 port_id = GET_PORT_ID(le32_to_cpu(event->generic.field[0])); in handle_port_status()
1965 max_ports = HCS_MAX_PORTS(xhci->hcs_params1); in handle_port_status()
1973 port = &xhci->hw_ports[port_id - 1]; in handle_port_status()
1974 if (!port || !port->rhub || port->hcd_portnum == DUPLICATE_ENTRY) { in handle_port_status()
1982 if (port->rhub == &xhci->usb3_rhub && xhci->shared_hcd == NULL) { in handle_port_status()
1988 hcd = port->rhub->hcd; in handle_port_status()
1989 bus_state = &port->rhub->bus_state; in handle_port_status()
1990 hcd_portnum = port->hcd_portnum; in handle_port_status()
1991 portsc = readl(port->addr); in handle_port_status()
1993 xhci_dbg(xhci, "Port change event, %d-%d, id %d, portsc: 0x%x\n", in handle_port_status()
1994 hcd->self.busnum, hcd_portnum + 1, port_id, portsc); in handle_port_status()
1998 if (hcd->state == HC_STATE_SUSPENDED) { in handle_port_status()
2003 if (hcd->speed >= HCD_USB3 && in handle_port_status()
2005 if (port->slot_id && xhci->devs[port->slot_id]) in handle_port_status()
2006 xhci->devs[port->slot_id]->flags |= VDEV_PORT_ERROR; in handle_port_status()
2012 cmd_reg = readl(&xhci->op_regs->command); in handle_port_status()
2022 * device and host initiated resume. in handle_port_status()
2024 bus_state->port_remote_wakeup |= 1 << hcd_portnum; in handle_port_status()
2026 usb_hcd_start_port_resume(&hcd->self, hcd_portnum); in handle_port_status()
2033 } else if (!test_bit(hcd_portnum, &bus_state->resuming_ports)) { in handle_port_status()
2035 port->resume_timestamp = jiffies + in handle_port_status()
2037 set_bit(hcd_portnum, &bus_state->resuming_ports); in handle_port_status()
2040 * usb device auto-resume latency around ~40ms. in handle_port_status()
2042 set_bit(HCD_FLAG_POLL_RH, &hcd->flags); in handle_port_status()
2043 mod_timer(&hcd->rh_timer, in handle_port_status()
2044 port->resume_timestamp); in handle_port_status()
2045 usb_hcd_start_port_resume(&hcd->self, hcd_portnum); in handle_port_status()
2056 complete(&port->u3exit_done); in handle_port_status()
2059 * U3Exit state after a host-initiated resume. If it's a device in handle_port_status()
2064 if (port->slot_id && xhci->devs[port->slot_id]) in handle_port_status()
2065 xhci_ring_device(xhci, port->slot_id); in handle_port_status()
2066 if (bus_state->port_remote_wakeup & (1 << hcd_portnum)) { in handle_port_status()
2068 usb_wakeup_notification(hcd->self.root_hub, in handle_port_status()
2076 * Check to see if xhci-hub.c is waiting on RExit to U0 transition (or in handle_port_status()
2080 if (hcd->speed < HCD_USB3 && port->rexit_active) { in handle_port_status()
2081 complete(&port->rexit_done); in handle_port_status()
2082 port->rexit_active = false; in handle_port_status()
2087 if (hcd->speed < HCD_USB3) { in handle_port_status()
2089 if ((xhci->quirks & XHCI_RESET_PLL_ON_DISCONNECT) && in handle_port_status()
2104 * xHCI port-status-change events occur when the "or" of all the in handle_port_status()
2105 * status-change bits in the portsc register changes from 0 to 1. in handle_port_status()
2111 __func__, hcd->self.busnum); in handle_port_status()
2112 set_bit(HCD_FLAG_POLL_RH, &hcd->flags); in handle_port_status()
2113 spin_unlock(&xhci->lock); in handle_port_status()
2116 spin_lock(&xhci->lock); in handle_port_status()
2131 start_dma = xhci_trb_virt_to_dma(td->start_seg, td->start_trb); in trb_in_td()
2132 cur_seg = td->start_seg; in trb_in_td()
2139 &cur_seg->trbs[TRBS_PER_SEGMENT - 1]); in trb_in_td()
2141 end_trb_dma = xhci_trb_virt_to_dma(cur_seg, td->end_trb); in trb_in_td()
2145 …"Looking for event-dma %016llx trb-start %016llx trb-end %016llx seg-start %016llx seg-end %016llx… in trb_in_td()
2149 (unsigned long long)cur_seg->dma, in trb_in_td()
2163 (suspect_dma >= cur_seg->dma && in trb_in_td()
2173 cur_seg = cur_seg->next; in trb_in_td()
2174 start_dma = xhci_trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]); in trb_in_td()
2175 } while (cur_seg != td->start_seg); in trb_in_td()
2184 * As part of low/full-speed endpoint-halt processing in xhci_clear_hub_tt_buffer()
2187 if (td->urb->dev->tt && !usb_pipeint(td->urb->pipe) && in xhci_clear_hub_tt_buffer()
2188 (td->urb->dev->tt->hub != xhci_to_hcd(xhci)->self.root_hub) && in xhci_clear_hub_tt_buffer()
2189 !(ep->ep_state & EP_CLEARING_TT)) { in xhci_clear_hub_tt_buffer()
2190 ep->ep_state |= EP_CLEARING_TT; in xhci_clear_hub_tt_buffer()
2191 td->urb->ep->hcpriv = td->urb->dev; in xhci_clear_hub_tt_buffer()
2192 if (usb_hub_clear_tt_buffer(td->urb)) in xhci_clear_hub_tt_buffer()
2193 ep->ep_state &= ~EP_CLEARING_TT; in xhci_clear_hub_tt_buffer()
2231 * treat as not-an-error. in xhci_is_vendor_info_code()
2247 ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep->ep_index); in finish_td()
2279 if ((ep->ep_state & EP_HALTED) && in finish_td()
2280 !list_empty(&td->cancelled_td_list)) { in finish_td()
2283 td->start_seg, td->start_trb)); in finish_td()
2297 * To clear the host side halt we need to issue a reset endpoint in finish_td()
2304 if (ep->ep_index != 0) in finish_td()
2314 xhci_dequeue_td(xhci, td, ep_ring, td->status); in finish_td()
2321 union xhci_trb *trb = td->start_trb; in sum_trb_lengths()
2322 struct xhci_segment *seg = td->start_seg; in sum_trb_lengths()
2326 sum += TRB_LEN(le32_to_cpu(trb->generic.field[2])); in sum_trb_lengths()
2343 trb_type = TRB_FIELD_TO_TYPE(le32_to_cpu(ep_trb->generic.field[3])); in process_ctrl_td()
2344 ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep->ep_index); in process_ctrl_td()
2345 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len)); in process_ctrl_td()
2346 requested = td->urb->transfer_buffer_length; in process_ctrl_td()
2347 remaining = EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)); in process_ctrl_td()
2354 td->status = -ESHUTDOWN; in process_ctrl_td()
2357 td->status = 0; in process_ctrl_td()
2360 td->status = 0; in process_ctrl_td()
2364 td->urb->actual_length = remaining; in process_ctrl_td()
2371 td->urb->actual_length = 0; in process_ctrl_td()
2375 td->urb->actual_length = requested - remaining; in process_ctrl_td()
2378 td->urb->actual_length = requested; in process_ctrl_td()
2391 trb_comp_code, ep->ep_index); in process_ctrl_td()
2396 td->urb->actual_length = requested - remaining; in process_ctrl_td()
2397 else if (!td->urb_length_set) in process_ctrl_td()
2398 td->urb->actual_length = 0; in process_ctrl_td()
2412 td->urb_length_set = true; in process_ctrl_td()
2413 td->urb->actual_length = requested - remaining; in process_ctrl_td()
2419 if (!td->urb_length_set) in process_ctrl_td()
2420 td->urb->actual_length = requested; in process_ctrl_td()
2441 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len)); in process_isoc_td()
2442 urb_priv = td->urb->hcpriv; in process_isoc_td()
2443 idx = urb_priv->num_tds_done; in process_isoc_td()
2444 frame = &td->urb->iso_frame_desc[idx]; in process_isoc_td()
2445 requested = frame->length; in process_isoc_td()
2446 remaining = EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)); in process_isoc_td()
2447 ep_trb_len = TRB_LEN(le32_to_cpu(ep_trb->generic.field[2])); in process_isoc_td()
2448 short_framestatus = td->urb->transfer_flags & URB_SHORT_NOT_OK ? in process_isoc_td()
2449 -EREMOTEIO : 0; in process_isoc_td()
2455 if (td->error_mid_td) in process_isoc_td()
2458 frame->status = short_framestatus; in process_isoc_td()
2462 frame->status = 0; in process_isoc_td()
2465 frame->status = short_framestatus; in process_isoc_td()
2469 frame->status = -ECOMM; in process_isoc_td()
2475 frame->status = -EOVERFLOW; in process_isoc_td()
2476 if (ep_trb != td->end_trb) in process_isoc_td()
2477 td->error_mid_td = true; in process_isoc_td()
2481 frame->status = -EPROTO; in process_isoc_td()
2484 frame->status = -EPROTO; in process_isoc_td()
2486 if (ep_trb != td->end_trb) in process_isoc_td()
2487 td->error_mid_td = true; in process_isoc_td()
2494 frame->status = short_framestatus; in process_isoc_td()
2505 frame->status = -1; in process_isoc_td()
2509 if (td->urb_length_set) in process_isoc_td()
2513 frame->actual_length = sum_trb_lengths(td, ep_trb) + in process_isoc_td()
2514 ep_trb_len - remaining; in process_isoc_td()
2516 frame->actual_length = requested; in process_isoc_td()
2518 td->urb->actual_length += frame->actual_length; in process_isoc_td()
2522 if (td->error_mid_td && ep_trb != td->end_trb) { in process_isoc_td()
2524 td->urb_length_set = true; in process_isoc_td()
2537 urb_priv = td->urb->hcpriv; in skip_isoc_td()
2538 idx = urb_priv->num_tds_done; in skip_isoc_td()
2539 frame = &td->urb->iso_frame_desc[idx]; in skip_isoc_td()
2542 frame->status = -EXDEV; in skip_isoc_td()
2545 frame->actual_length = 0; in skip_isoc_td()
2547 xhci_dequeue_td(xhci, td, ep->ring, status); in skip_isoc_td()
2561 slot_ctx = xhci_get_slot_ctx(xhci, ep->vdev->out_ctx); in process_bulk_intr_td()
2562 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len)); in process_bulk_intr_td()
2563 remaining = EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)); in process_bulk_intr_td()
2564 ep_trb_len = TRB_LEN(le32_to_cpu(ep_trb->generic.field[2])); in process_bulk_intr_td()
2565 requested = td->urb->transfer_buffer_length; in process_bulk_intr_td()
2569 ep->err_count = 0; in process_bulk_intr_td()
2571 if (ep_trb != td->end_trb || remaining) { in process_bulk_intr_td()
2573 xhci_dbg(xhci, "ep %#x - asked for %d bytes, %d bytes untransferred\n", in process_bulk_intr_td()
2574 td->urb->ep->desc.bEndpointAddress, in process_bulk_intr_td()
2577 td->status = 0; in process_bulk_intr_td()
2580 td->status = 0; in process_bulk_intr_td()
2583 td->urb->actual_length = remaining; in process_bulk_intr_td()
2587 td->urb->actual_length = sum_trb_lengths(td, ep_trb); in process_bulk_intr_td()
2590 if (xhci->quirks & XHCI_NO_SOFT_RETRY || in process_bulk_intr_td()
2591 (ep->err_count++ > MAX_SOFT_RETRY) || in process_bulk_intr_td()
2592 le32_to_cpu(slot_ctx->tt_info) & TT_SLOT) in process_bulk_intr_td()
2595 td->status = 0; in process_bulk_intr_td()
2604 if (ep_trb == td->end_trb) in process_bulk_intr_td()
2605 td->urb->actual_length = requested - remaining; in process_bulk_intr_td()
2607 td->urb->actual_length = in process_bulk_intr_td()
2609 ep_trb_len - remaining; in process_bulk_intr_td()
2614 td->urb->actual_length = 0; in process_bulk_intr_td()
2629 xhci_dbg(xhci, "Stream transaction error ep %u no id\n", ep->ep_index); in handle_transferless_tx_event()
2630 if (ep->err_count++ > MAX_SOFT_RETRY) in handle_transferless_tx_event()
2641 trb_comp_code, ep->vdev->slot_id, ep->ep_index); in handle_transferless_tx_event()
2642 return -ENODEV; in handle_transferless_tx_event()
2650 * At this point, the host controller is probably hosed and should be reset.
2664 int status = -EINPROGRESS; in handle_tx_event()
2668 slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags)); in handle_tx_event()
2669 ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1; in handle_tx_event()
2670 trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len)); in handle_tx_event()
2671 ep_trb_dma = le64_to_cpu(event->buffer); in handle_tx_event()
2680 ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep_index); in handle_tx_event()
2698 if (EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)) != 0) { in handle_tx_event()
2701 slot_id, ep_index, ep_ring->last_td_was_short); in handle_tx_event()
2713 "Stopped on No-op or Link TRB for slot %u ep %u\n", in handle_tx_event()
2725 status = -EPIPE; in handle_tx_event()
2730 status = -EPROTO; in handle_tx_event()
2735 status = -EPROTO; in handle_tx_event()
2740 status = -EOVERFLOW; in handle_tx_event()
2747 status = -EILSEQ; in handle_tx_event()
2754 status = -ENOSR; in handle_tx_event()
2773 if (ep->skip) in handle_tx_event()
2778 if (ep->skip) in handle_tx_event()
2788 ep->skip = true; in handle_tx_event()
2794 ep->skip = true; in handle_tx_event()
2805 status = -EPROTO; in handle_tx_event()
2815 if (ep->skip) in handle_tx_event()
2823 * So host like NEC don't generate an event for the last in handle_tx_event()
2825 * xhci 4.9.1 states that if there are errors in mult-TRB in handle_tx_event()
2828 * any TRB with IOC flag on the way. Other host follow this. in handle_tx_event()
2833 td = list_first_entry_or_null(&ep_ring->td_list, struct xhci_td, td_list); in handle_tx_event()
2835 if (td && td->error_mid_td && !trb_in_td(xhci, td, ep_trb_dma, false)) { in handle_tx_event()
2837 xhci_dequeue_td(xhci, td, ep_ring, td->status); in handle_tx_event()
2840 if (list_empty(&ep_ring->td_list)) { in handle_tx_event()
2849 !ep_ring->last_td_was_short) { in handle_tx_event()
2854 ep->skip = false; in handle_tx_event()
2859 td = list_first_entry(&ep_ring->td_list, struct xhci_td, in handle_tx_event()
2867 if (ep->skip && usb_endpoint_xfer_isoc(&td->urb->ep->desc)) { in handle_tx_event()
2869 if (!list_empty(&ep_ring->td_list)) in handle_tx_event()
2874 ep->skip = false; in handle_tx_event()
2881 * TD pointed by 'ep_ring->dequeue' because that the hardware dequeue in handle_tx_event()
2895 if ((xhci->quirks & XHCI_SPURIOUS_SUCCESS) && in handle_tx_event()
2896 ep_ring->last_td_was_short) { in handle_tx_event()
2897 ep_ring->last_td_was_short = false; in handle_tx_event()
2907 return -ESHUTDOWN; in handle_tx_event()
2910 if (ep->skip) { in handle_tx_event()
2914 ep->skip = false; in handle_tx_event()
2918 * If ep->skip is set, it means there are missed tds on the in handle_tx_event()
2923 } while (ep->skip); in handle_tx_event()
2926 ep_ring->last_td_was_short = true; in handle_tx_event()
2928 ep_ring->last_td_was_short = false; in handle_tx_event()
2930 ep_trb = &ep_seg->trbs[(ep_trb_dma - ep_seg->dma) / sizeof(*ep_trb)]; in handle_tx_event()
2934 * No-op TRB could trigger interrupts in a case where a URB was killed in handle_tx_event()
2943 td->status = status; in handle_tx_event()
2946 if (usb_endpoint_xfer_control(&td->urb->ep->desc)) in handle_tx_event()
2948 else if (usb_endpoint_xfer_isoc(&td->urb->ep->desc)) in handle_tx_event()
2963 ir->event_ring->deq_seg, in handle_tx_event()
2964 ir->event_ring->dequeue), in handle_tx_event()
2965 lower_32_bits(le64_to_cpu(event->buffer)), in handle_tx_event()
2966 upper_32_bits(le64_to_cpu(event->buffer)), in handle_tx_event()
2967 le32_to_cpu(event->transfer_len), in handle_tx_event()
2968 le32_to_cpu(event->flags)); in handle_tx_event()
2969 return -ENODEV; in handle_tx_event()
2973 * This function handles one OS-owned event on the event ring. It may drop
2974 * xhci->lock between event processing (e.g. to pass up port status changes).
2981 trace_xhci_handle_event(ir->event_ring, &event->generic, in xhci_handle_event_trb()
2982 xhci_trb_virt_to_dma(ir->event_ring->deq_seg, in xhci_handle_event_trb()
2983 ir->event_ring->dequeue)); in xhci_handle_event_trb()
2990 trb_type = TRB_FIELD_TO_TYPE(le32_to_cpu(event->event_cmd.flags)); in xhci_handle_event_trb()
2995 handle_cmd_completion(xhci, &event->event_cmd); in xhci_handle_event_trb()
3001 handle_tx_event(xhci, ir, &event->trans_event); in xhci_handle_event_trb()
3012 /* Any of the above functions may drop and re-acquire the lock, so check in xhci_handle_event_trb()
3013 * to make sure a watchdog timer didn't mark the host as non-responsive. in xhci_handle_event_trb()
3015 if (xhci->xhc_state & XHCI_STATE_DYING) { in xhci_handle_event_trb()
3016 xhci_dbg(xhci, "xHCI host dying, returning from event handler.\n"); in xhci_handle_event_trb()
3017 return -ENODEV; in xhci_handle_event_trb()
3025 * - When all events have finished
3026 * - To avoid "Event Ring Full Error" condition
3035 temp_64 = xhci_read_64(xhci, &ir->ir_set->erst_dequeue); in xhci_update_erst_dequeue()
3036 deq = xhci_trb_virt_to_dma(ir->event_ring->deq_seg, in xhci_update_erst_dequeue()
3037 ir->event_ring->dequeue); in xhci_update_erst_dequeue()
3048 temp_64 = ir->event_ring->deq_seg->num & ERST_DESI_MASK; in xhci_update_erst_dequeue()
3054 xhci_write_64(xhci, temp_64, &ir->ir_set->erst_dequeue); in xhci_update_erst_dequeue()
3060 if (!ir->ip_autoclear) { in xhci_clear_interrupt_pending()
3063 irq_pending = readl(&ir->ir_set->irq_pending); in xhci_clear_interrupt_pending()
3065 writel(irq_pending, &ir->ir_set->irq_pending); in xhci_clear_interrupt_pending()
3070 * Handle all OS-owned events on an interrupter event ring. It may drop
3071 * and reaquire xhci->lock between event processing.
3082 if (!ir->event_ring || !ir->event_ring->dequeue) { in xhci_handle_events()
3084 return -ENOMEM; in xhci_handle_events()
3087 if (xhci->xhc_state & XHCI_STATE_DYING || in xhci_handle_events()
3088 xhci->xhc_state & XHCI_STATE_HALTED) { in xhci_handle_events()
3092 temp = xhci_read_64(xhci, &ir->ir_set->erst_dequeue); in xhci_handle_events()
3093 xhci_write_64(xhci, temp | ERST_EHB, &ir->ir_set->erst_dequeue); in xhci_handle_events()
3094 return -ENODEV; in xhci_handle_events()
3098 while (unhandled_event_trb(ir->event_ring)) { in xhci_handle_events()
3099 err = xhci_handle_event_trb(xhci, ir, ir->event_ring->dequeue); in xhci_handle_events()
3108 if (ir->isoc_bei_interval > AVOID_BEI_INTERVAL_MIN) in xhci_handle_events()
3109 ir->isoc_bei_interval = ir->isoc_bei_interval / 2; in xhci_handle_events()
3115 inc_deq(xhci, ir->event_ring); in xhci_handle_events()
3137 spin_lock(&xhci->lock); in xhci_irq()
3139 status = readl(&xhci->op_regs->status); in xhci_irq()
3151 xhci_warn(xhci, "WARNING: Host Controller Error\n"); in xhci_irq()
3156 xhci_warn(xhci, "WARNING: Host System Error\n"); in xhci_irq()
3163 * so we can receive interrupts from other MSI-X interrupters. in xhci_irq()
3167 writel(status, &xhci->op_regs->status); in xhci_irq()
3170 xhci_handle_events(xhci, xhci->interrupters[0]); in xhci_irq()
3172 spin_unlock(&xhci->lock); in xhci_irq()
3198 trb = &ring->enqueue->generic; in queue_trb()
3199 trb->field[0] = cpu_to_le32(field1); in queue_trb()
3200 trb->field[1] = cpu_to_le32(field2); in queue_trb()
3201 trb->field[2] = cpu_to_le32(field3); in queue_trb()
3204 trb->field[3] = cpu_to_le32(field4); in queue_trb()
3207 xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue)); in queue_trb()
3230 return -ENOENT; in prepare_ring()
3234 /* XXX not sure if this should be -ENOENT or not */ in prepare_ring()
3235 return -EINVAL; in prepare_ring()
3248 return -EINVAL; in prepare_ring()
3251 if (ep_ring != xhci->cmd_ring) { in prepare_ring()
3255 return -ENOMEM; in prepare_ring()
3263 return -ENOMEM; in prepare_ring()
3267 while (trb_is_link(ep_ring->enqueue)) { in prepare_ring()
3269 * on AMD 0.96 host, clear the chain bit. in prepare_ring()
3271 if (!xhci_link_chain_quirk(xhci, ep_ring->type)) in prepare_ring()
3272 ep_ring->enqueue->link.control &= in prepare_ring()
3275 ep_ring->enqueue->link.control |= in prepare_ring()
3279 ep_ring->enqueue->link.control ^= cpu_to_le32(TRB_CYCLE); in prepare_ring()
3282 if (link_trb_toggles_cycle(ep_ring->enqueue)) in prepare_ring()
3283 ep_ring->cycle_state ^= 1; in prepare_ring()
3285 ep_ring->enq_seg = ep_ring->enq_seg->next; in prepare_ring()
3286 ep_ring->enqueue = ep_ring->enq_seg->trbs; in prepare_ring()
3289 if (link_trb_count++ > ep_ring->num_segs) { in prepare_ring()
3291 return -EINVAL; in prepare_ring()
3295 if (last_trb_on_seg(ep_ring->enq_seg, ep_ring->enqueue)) { in prepare_ring()
3297 return -EINVAL; in prepare_ring()
3316 struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index); in prepare_transfer()
3318 ep_ring = xhci_triad_to_transfer_ring(xhci, xdev->slot_id, ep_index, in prepare_transfer()
3323 return -EINVAL; in prepare_transfer()
3331 urb_priv = urb->hcpriv; in prepare_transfer()
3332 td = &urb_priv->td[td_index]; in prepare_transfer()
3334 INIT_LIST_HEAD(&td->td_list); in prepare_transfer()
3335 INIT_LIST_HEAD(&td->cancelled_td_list); in prepare_transfer()
3338 ret = usb_hcd_link_urb_to_ep(bus_to_hcd(urb->dev->bus), urb); in prepare_transfer()
3343 td->urb = urb; in prepare_transfer()
3345 list_add_tail(&td->td_list, &ep_ring->td_list); in prepare_transfer()
3346 td->start_seg = ep_ring->enq_seg; in prepare_transfer()
3347 td->start_trb = ep_ring->enqueue; in prepare_transfer()
3356 num_trbs = DIV_ROUND_UP(len + (addr & (TRB_MAX_BUFF_SIZE - 1)), in count_trbs()
3366 return count_trbs(urb->transfer_dma, urb->transfer_buffer_length); in count_trbs_needed()
3374 full_len = urb->transfer_buffer_length; in count_sg_trbs_needed()
3376 for_each_sg(urb->sg, sg, urb->num_mapped_sgs, i) { in count_sg_trbs_needed()
3380 full_len -= len; in count_sg_trbs_needed()
3392 addr = (u64) (urb->transfer_dma + urb->iso_frame_desc[i].offset); in count_isoc_trbs_needed()
3393 len = urb->iso_frame_desc[i].length; in count_isoc_trbs_needed()
3400 if (unlikely(running_total != urb->transfer_buffer_length)) in check_trb_math()
3401 dev_err(&urb->dev->dev, "%s - ep %#x - Miscalculated tx length, " in check_trb_math()
3404 urb->ep->desc.bEndpointAddress, in check_trb_math()
3406 urb->transfer_buffer_length, in check_trb_math()
3407 urb->transfer_buffer_length); in check_trb_math()
3420 start_trb->field[3] |= cpu_to_le32(start_cycle); in giveback_first_trb()
3422 start_trb->field[3] &= cpu_to_le32(~TRB_CYCLE); in giveback_first_trb()
3431 xhci_interval = EP_INTERVAL_TO_UFRAMES(le32_to_cpu(ep_ctx->ep_info)); in check_interval()
3432 ep_interval = urb->interval; in check_interval()
3435 if (urb->dev->speed == USB_SPEED_LOW || in check_interval()
3436 urb->dev->speed == USB_SPEED_FULL) in check_interval()
3443 dev_dbg_ratelimited(&urb->dev->dev, in check_interval()
3447 urb->interval = xhci_interval; in check_interval()
3449 if (urb->dev->speed == USB_SPEED_LOW || in check_interval()
3450 urb->dev->speed == USB_SPEED_FULL) in check_interval()
3451 urb->interval /= 8; in check_interval()
3466 ep_ctx = xhci_get_ep_ctx(xhci, xhci->devs[slot_id]->out_ctx, ep_index); in xhci_queue_intr_tx()
3473 * For xHCI 1.0 host controllers, TD size is the number of max packet sized
3482 * TD size = total_packet_count - packets_transferred
3499 if (xhci->hci_version < 0x100 && !(xhci->quirks & XHCI_MTK_HOST)) in xhci_td_remainder()
3500 return ((td_total_len - transferred) >> 10); in xhci_td_remainder()
3502 /* One TRB with a zero-length data packet. */ in xhci_td_remainder()
3508 if ((xhci->quirks & XHCI_MTK_HOST) && (xhci->hci_version < 0x100)) in xhci_td_remainder()
3511 maxp = usb_endpoint_maxp(&urb->ep->desc); in xhci_td_remainder()
3515 return (total_packet_count - ((transferred + trb_buff_len) / maxp)); in xhci_td_remainder()
3522 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_align_td()
3528 max_pkt = usb_endpoint_maxp(&urb->ep->desc); in xhci_align_td()
3540 *trb_buff_len -= unalign; in xhci_align_td()
3550 new_buff_len = max_pkt - (enqd_len % max_pkt); in xhci_align_td()
3552 if (new_buff_len > (urb->transfer_buffer_length - enqd_len)) in xhci_align_td()
3553 new_buff_len = (urb->transfer_buffer_length - enqd_len); in xhci_align_td()
3557 if (urb->num_sgs) { in xhci_align_td()
3558 len = sg_pcopy_to_buffer(urb->sg, urb->num_sgs, in xhci_align_td()
3559 seg->bounce_buf, new_buff_len, enqd_len); in xhci_align_td()
3564 memcpy(seg->bounce_buf, urb->transfer_buffer + enqd_len, new_buff_len); in xhci_align_td()
3567 seg->bounce_dma = dma_map_single(dev, seg->bounce_buf, in xhci_align_td()
3570 seg->bounce_dma = dma_map_single(dev, seg->bounce_buf, in xhci_align_td()
3574 if (dma_mapping_error(dev, seg->bounce_dma)) { in xhci_align_td()
3575 /* try without aligning. Some host controllers survive */ in xhci_align_td()
3580 seg->bounce_len = new_buff_len; in xhci_align_td()
3581 seg->bounce_offs = enqd_len; in xhci_align_td()
3588 /* This is very similar to what ehci-q.c qtd_fill() does */
3609 return -EINVAL; in xhci_queue_bulk_tx()
3611 full_len = urb->transfer_buffer_length; in xhci_queue_bulk_tx()
3613 if (urb->num_sgs && !(urb->transfer_flags & URB_DMA_MAP_SINGLE)) { in xhci_queue_bulk_tx()
3614 num_sgs = urb->num_mapped_sgs; in xhci_queue_bulk_tx()
3615 sg = urb->sg; in xhci_queue_bulk_tx()
3621 addr = (u64) urb->transfer_dma; in xhci_queue_bulk_tx()
3624 ret = prepare_transfer(xhci, xhci->devs[slot_id], in xhci_queue_bulk_tx()
3625 ep_index, urb->stream_id, in xhci_queue_bulk_tx()
3630 urb_priv = urb->hcpriv; in xhci_queue_bulk_tx()
3632 /* Deal with URB_ZERO_PACKET - need one more td/trb */ in xhci_queue_bulk_tx()
3633 if (urb->transfer_flags & URB_ZERO_PACKET && urb_priv->num_tds > 1) in xhci_queue_bulk_tx()
3636 td = &urb_priv->td[0]; in xhci_queue_bulk_tx()
3643 start_trb = &ring->enqueue->generic; in xhci_queue_bulk_tx()
3644 start_cycle = ring->cycle_state; in xhci_queue_bulk_tx()
3647 /* Queue the TRBs, even if they are zero-length */ in xhci_queue_bulk_tx()
3657 trb_buff_len = full_len - enqd_len; in xhci_queue_bulk_tx()
3665 field |= ring->cycle_state; in xhci_queue_bulk_tx()
3672 if (trb_is_link(ring->enqueue + 1)) { in xhci_queue_bulk_tx()
3675 ring->enq_seg)) { in xhci_queue_bulk_tx()
3676 send_addr = ring->enq_seg->bounce_dma; in xhci_queue_bulk_tx()
3678 td->bounce_seg = ring->enq_seg; in xhci_queue_bulk_tx()
3686 td->end_trb = ring->enqueue; in xhci_queue_bulk_tx()
3687 td->end_seg = ring->enq_seg; in xhci_queue_bulk_tx()
3689 memcpy(&send_addr, urb->transfer_buffer, in xhci_queue_bulk_tx()
3718 --num_sgs; in xhci_queue_bulk_tx()
3719 sent_len -= block_len; in xhci_queue_bulk_tx()
3727 block_len -= sent_len; in xhci_queue_bulk_tx()
3732 ret = prepare_transfer(xhci, xhci->devs[slot_id], in xhci_queue_bulk_tx()
3733 ep_index, urb->stream_id, in xhci_queue_bulk_tx()
3735 urb_priv->td[1].end_trb = ring->enqueue; in xhci_queue_bulk_tx()
3736 urb_priv->td[1].end_seg = ring->enq_seg; in xhci_queue_bulk_tx()
3737 field = TRB_TYPE(TRB_NORMAL) | ring->cycle_state | TRB_IOC; in xhci_queue_bulk_tx()
3742 giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id, in xhci_queue_bulk_tx()
3747 /* Caller must have locked xhci->lock */
3763 return -EINVAL; in xhci_queue_ctrl_tx()
3769 if (!urb->setup_packet) in xhci_queue_ctrl_tx()
3770 return -EINVAL; in xhci_queue_ctrl_tx()
3772 if ((xhci->quirks & XHCI_ETRON_HOST) && in xhci_queue_ctrl_tx()
3773 urb->dev->speed >= USB_SPEED_SUPER) { in xhci_queue_ctrl_tx()
3779 if (trb_is_link(ep_ring->enqueue + 1)) { in xhci_queue_ctrl_tx()
3780 field = TRB_TYPE(TRB_TR_NOOP) | ep_ring->cycle_state; in xhci_queue_ctrl_tx()
3793 if (urb->transfer_buffer_length > 0) in xhci_queue_ctrl_tx()
3795 ret = prepare_transfer(xhci, xhci->devs[slot_id], in xhci_queue_ctrl_tx()
3796 ep_index, urb->stream_id, in xhci_queue_ctrl_tx()
3801 urb_priv = urb->hcpriv; in xhci_queue_ctrl_tx()
3802 td = &urb_priv->td[0]; in xhci_queue_ctrl_tx()
3809 start_trb = &ep_ring->enqueue->generic; in xhci_queue_ctrl_tx()
3810 start_cycle = ep_ring->cycle_state; in xhci_queue_ctrl_tx()
3812 /* Queue setup TRB - see section 6.4.1.2.1 */ in xhci_queue_ctrl_tx()
3814 setup = (struct usb_ctrlrequest *) urb->setup_packet; in xhci_queue_ctrl_tx()
3821 if ((xhci->hci_version >= 0x100) || (xhci->quirks & XHCI_MTK_HOST)) { in xhci_queue_ctrl_tx()
3822 if (urb->transfer_buffer_length > 0) { in xhci_queue_ctrl_tx()
3823 if (setup->bRequestType & USB_DIR_IN) in xhci_queue_ctrl_tx()
3831 setup->bRequestType | setup->bRequest << 8 | le16_to_cpu(setup->wValue) << 16, in xhci_queue_ctrl_tx()
3832 le16_to_cpu(setup->wIndex) | le16_to_cpu(setup->wLength) << 16, in xhci_queue_ctrl_tx()
3844 if (urb->transfer_buffer_length > 0) { in xhci_queue_ctrl_tx()
3849 memcpy(&addr, urb->transfer_buffer, in xhci_queue_ctrl_tx()
3850 urb->transfer_buffer_length); in xhci_queue_ctrl_tx()
3854 addr = (u64) urb->transfer_dma; in xhci_queue_ctrl_tx()
3858 urb->transfer_buffer_length, in xhci_queue_ctrl_tx()
3859 urb->transfer_buffer_length, in xhci_queue_ctrl_tx()
3861 length_field = TRB_LEN(urb->transfer_buffer_length) | in xhci_queue_ctrl_tx()
3864 if (setup->bRequestType & USB_DIR_IN) in xhci_queue_ctrl_tx()
3870 field | ep_ring->cycle_state); in xhci_queue_ctrl_tx()
3874 td->end_trb = ep_ring->enqueue; in xhci_queue_ctrl_tx()
3875 td->end_seg = ep_ring->enq_seg; in xhci_queue_ctrl_tx()
3877 /* Queue status TRB - see Table 7 and sections 4.11.2.2 and 6.4.1.2.3 */ in xhci_queue_ctrl_tx()
3879 if (urb->transfer_buffer_length > 0 && setup->bRequestType & USB_DIR_IN) in xhci_queue_ctrl_tx()
3888 field | TRB_IOC | TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state); in xhci_queue_ctrl_tx()
3901 * zero. Only xHCI 1.0 host controllers support this field.
3908 if (xhci->hci_version < 0x100 || urb->dev->speed < USB_SPEED_SUPER) in xhci_get_burst_count()
3911 max_burst = urb->ep->ss_ep_comp.bMaxBurst; in xhci_get_burst_count()
3912 return DIV_ROUND_UP(total_packet_count, max_burst + 1) - 1; in xhci_get_burst_count()
3919 * TD. SuperSpeed endpoints can have up to 3 bursts. All but the last burst
3929 if (xhci->hci_version < 0x100) in xhci_get_last_burst_packet_count()
3932 if (urb->dev->speed >= USB_SPEED_SUPER) { in xhci_get_last_burst_packet_count()
3934 max_burst = urb->ep->ss_ep_comp.bMaxBurst; in xhci_get_last_burst_packet_count()
3937 * number of packets, but the TLBPC field is zero-based. in xhci_get_last_burst_packet_count()
3941 return residue - 1; in xhci_get_last_burst_packet_count()
3945 return total_packet_count - 1; in xhci_get_last_burst_packet_count()
3961 if (urb->dev->speed == USB_SPEED_LOW || in xhci_get_isoc_frame_id()
3962 urb->dev->speed == USB_SPEED_FULL) in xhci_get_isoc_frame_id()
3963 start_frame = urb->start_frame + index * urb->interval; in xhci_get_isoc_frame_id()
3965 start_frame = (urb->start_frame + index * urb->interval) >> 3; in xhci_get_isoc_frame_id()
3967 /* Isochronous Scheduling Threshold (IST, bits 0~3 in HCSPARAMS2): in xhci_get_isoc_frame_id()
3969 * If bit [3] of IST is cleared to '0', software can add a TRB no in xhci_get_isoc_frame_id()
3972 * If bit [3] of IST is set to '1', software can add a TRB no later in xhci_get_isoc_frame_id()
3975 ist = HCS_IST(xhci->hcs_params2) & 0x7; in xhci_get_isoc_frame_id()
3976 if (HCS_IST(xhci->hcs_params2) & (1 << 3)) in xhci_get_isoc_frame_id()
3977 ist <<= 3; in xhci_get_isoc_frame_id()
3992 current_frame_id = readl(&xhci->run_regs->microframe_index); in xhci_get_isoc_frame_id()
3997 start_frame_id = (start_frame_id >> 3) & 0x7ff; in xhci_get_isoc_frame_id()
3998 end_frame_id = (end_frame_id >> 3) & 0x7ff; in xhci_get_isoc_frame_id()
4003 ret = -EINVAL; in xhci_get_isoc_frame_id()
4007 ret = -EINVAL; in xhci_get_isoc_frame_id()
4009 ret = -EINVAL; in xhci_get_isoc_frame_id()
4013 if (ret == -EINVAL || start_frame == start_frame_id) { in xhci_get_isoc_frame_id()
4015 if (urb->dev->speed == USB_SPEED_LOW || in xhci_get_isoc_frame_id()
4016 urb->dev->speed == USB_SPEED_FULL) in xhci_get_isoc_frame_id()
4017 urb->start_frame = start_frame; in xhci_get_isoc_frame_id()
4019 urb->start_frame = start_frame << 3; in xhci_get_isoc_frame_id()
4039 if (xhci->hci_version < 0x100) in trb_block_event_intr()
4042 if (i == num_tds - 1) in trb_block_event_intr()
4045 * If AVOID_BEI is set the host handles full event rings poorly, in trb_block_event_intr()
4048 if (i && ir->isoc_bei_interval && xhci->quirks & XHCI_AVOID_BEI) in trb_block_event_intr()
4049 return !!(i % ir->isoc_bei_interval); in trb_block_event_intr()
4074 xep = &xhci->devs[slot_id]->eps[ep_index]; in xhci_queue_isoc_tx()
4075 ep_ring = xhci->devs[slot_id]->eps[ep_index].ring; in xhci_queue_isoc_tx()
4076 ir = xhci->interrupters[0]; in xhci_queue_isoc_tx()
4078 num_tds = urb->number_of_packets; in xhci_queue_isoc_tx()
4081 return -EINVAL; in xhci_queue_isoc_tx()
4083 start_addr = (u64) urb->transfer_dma; in xhci_queue_isoc_tx()
4084 start_trb = &ep_ring->enqueue->generic; in xhci_queue_isoc_tx()
4085 start_cycle = ep_ring->cycle_state; in xhci_queue_isoc_tx()
4087 urb_priv = urb->hcpriv; in xhci_queue_isoc_tx()
4088 /* Queue the TRBs for each TD, even if they are zero-length */ in xhci_queue_isoc_tx()
4096 addr = start_addr + urb->iso_frame_desc[i].offset; in xhci_queue_isoc_tx()
4097 td_len = urb->iso_frame_desc[i].length; in xhci_queue_isoc_tx()
4099 max_pkt = usb_endpoint_maxp(&urb->ep->desc); in xhci_queue_isoc_tx()
4102 /* A zero-length transfer still involves at least one packet. */ in xhci_queue_isoc_tx()
4111 ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index, in xhci_queue_isoc_tx()
4112 urb->stream_id, trbs_per_td, urb, i, mem_flags); in xhci_queue_isoc_tx()
4118 td = &urb_priv->td[i]; in xhci_queue_isoc_tx()
4121 if (!(urb->transfer_flags & URB_ISO_ASAP) && in xhci_queue_isoc_tx()
4122 HCC_CFC(xhci->hcc_params)) { in xhci_queue_isoc_tx()
4135 (i ? ep_ring->cycle_state : !start_cycle); in xhci_queue_isoc_tx()
4138 if (!xep->use_extended_tbc) in xhci_queue_isoc_tx()
4148 ep_ring->cycle_state; in xhci_queue_isoc_tx()
4155 if (j < trbs_per_td - 1) { in xhci_queue_isoc_tx()
4160 td->end_trb = ep_ring->enqueue; in xhci_queue_isoc_tx()
4161 td->end_seg = ep_ring->enq_seg; in xhci_queue_isoc_tx()
4180 if (first_trb && xep->use_extended_tbc) in xhci_queue_isoc_tx()
4194 td_remain_len -= trb_buff_len; in xhci_queue_isoc_tx()
4200 ret = -EINVAL; in xhci_queue_isoc_tx()
4206 if (HCC_CFC(xhci->hcc_params)) in xhci_queue_isoc_tx()
4207 xep->next_frame_id = urb->start_frame + num_tds * urb->interval; in xhci_queue_isoc_tx()
4209 if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs == 0) { in xhci_queue_isoc_tx()
4210 if (xhci->quirks & XHCI_AMD_PLL_FIX) in xhci_queue_isoc_tx()
4213 xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs++; in xhci_queue_isoc_tx()
4215 giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id, in xhci_queue_isoc_tx()
4221 for (i--; i >= 0; i--) in xhci_queue_isoc_tx()
4222 list_del_init(&urb_priv->td[i].td_list); in xhci_queue_isoc_tx()
4225 * into No-ops with a software-owned cycle bit. That way the hardware in xhci_queue_isoc_tx()
4227 * overwrite them. td->start_trb and td->start_seg are already set. in xhci_queue_isoc_tx()
4229 urb_priv->td[0].end_trb = ep_ring->enqueue; in xhci_queue_isoc_tx()
4231 td_to_noop(&urb_priv->td[0], true); in xhci_queue_isoc_tx()
4234 ep_ring->enqueue = urb_priv->td[0].start_trb; in xhci_queue_isoc_tx()
4235 ep_ring->enq_seg = urb_priv->td[0].start_seg; in xhci_queue_isoc_tx()
4236 ep_ring->cycle_state = start_cycle; in xhci_queue_isoc_tx()
4237 usb_hcd_unlink_urb_from_ep(bus_to_hcd(urb->dev->bus), urb); in xhci_queue_isoc_tx()
4245 * update urb->start_frame if URB_ISO_ASAP is set in transfer_flags or
4260 xdev = xhci->devs[slot_id]; in xhci_queue_isoc_tx_prepare()
4261 xep = &xhci->devs[slot_id]->eps[ep_index]; in xhci_queue_isoc_tx_prepare()
4262 ep_ring = xdev->eps[ep_index].ring; in xhci_queue_isoc_tx_prepare()
4263 ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index); in xhci_queue_isoc_tx_prepare()
4266 num_tds = urb->number_of_packets; in xhci_queue_isoc_tx_prepare()
4284 /* Calculate the start frame and put it in urb->start_frame. */ in xhci_queue_isoc_tx_prepare()
4285 if (HCC_CFC(xhci->hcc_params) && !list_empty(&ep_ring->td_list)) { in xhci_queue_isoc_tx_prepare()
4287 urb->start_frame = xep->next_frame_id; in xhci_queue_isoc_tx_prepare()
4292 start_frame = readl(&xhci->run_regs->microframe_index); in xhci_queue_isoc_tx_prepare()
4298 ist = HCS_IST(xhci->hcs_params2) & 0x7; in xhci_queue_isoc_tx_prepare()
4299 if (HCS_IST(xhci->hcs_params2) & (1 << 3)) in xhci_queue_isoc_tx_prepare()
4300 ist <<= 3; in xhci_queue_isoc_tx_prepare()
4308 if (urb->dev->speed == USB_SPEED_LOW || in xhci_queue_isoc_tx_prepare()
4309 urb->dev->speed == USB_SPEED_FULL) { in xhci_queue_isoc_tx_prepare()
4310 start_frame = roundup(start_frame, urb->interval << 3); in xhci_queue_isoc_tx_prepare()
4311 urb->start_frame = start_frame >> 3; in xhci_queue_isoc_tx_prepare()
4313 start_frame = roundup(start_frame, urb->interval); in xhci_queue_isoc_tx_prepare()
4314 urb->start_frame = start_frame; in xhci_queue_isoc_tx_prepare()
4329 * Don't decrement xhci->cmd_ring_reserved_trbs after we've queued the TRB
4336 int reserved_trbs = xhci->cmd_ring_reserved_trbs; in queue_command()
4339 if ((xhci->xhc_state & XHCI_STATE_DYING) || in queue_command()
4340 (xhci->xhc_state & XHCI_STATE_HALTED)) { in queue_command()
4342 return -ESHUTDOWN; in queue_command()
4348 ret = prepare_ring(xhci, xhci->cmd_ring, EP_STATE_RUNNING, in queue_command()
4358 cmd->command_trb = xhci->cmd_ring->enqueue; in queue_command()
4361 if (list_empty(&xhci->cmd_list)) { in queue_command()
4362 xhci->current_cmd = cmd; in queue_command()
4366 list_add_tail(&cmd->cmd_list, &xhci->cmd_list); in queue_command()
4368 queue_trb(xhci, xhci->cmd_ring, false, field1, field2, field3, in queue_command()
4369 field4 | xhci->cmd_ring->cycle_state); in queue_command()