162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * xHCI host controller driver 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (C) 2008 Intel Corp. 662306a36Sopenharmony_ci * 762306a36Sopenharmony_ci * Author: Sarah Sharp 862306a36Sopenharmony_ci * Some code borrowed from the Linux EHCI driver. 962306a36Sopenharmony_ci */ 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ci/* 1262306a36Sopenharmony_ci * Ring initialization rules: 1362306a36Sopenharmony_ci * 1. Each segment is initialized to zero, except for link TRBs. 1462306a36Sopenharmony_ci * 2. Ring cycle state = 0. This represents Producer Cycle State (PCS) or 1562306a36Sopenharmony_ci * Consumer Cycle State (CCS), depending on ring function. 1662306a36Sopenharmony_ci * 3. Enqueue pointer = dequeue pointer = address of first TRB in the segment. 1762306a36Sopenharmony_ci * 1862306a36Sopenharmony_ci * Ring behavior rules: 1962306a36Sopenharmony_ci * 1. A ring is empty if enqueue == dequeue. This means there will always be at 2062306a36Sopenharmony_ci * least one free TRB in the ring. This is useful if you want to turn that 2162306a36Sopenharmony_ci * into a link TRB and expand the ring. 2262306a36Sopenharmony_ci * 2. When incrementing an enqueue or dequeue pointer, if the next TRB is a 2362306a36Sopenharmony_ci * link TRB, then load the pointer with the address in the link TRB. If the 2462306a36Sopenharmony_ci * link TRB had its toggle bit set, you may need to update the ring cycle 2562306a36Sopenharmony_ci * state (see cycle bit rules). You may have to do this multiple times 2662306a36Sopenharmony_ci * until you reach a non-link TRB. 2762306a36Sopenharmony_ci * 3. A ring is full if enqueue++ (for the definition of increment above) 2862306a36Sopenharmony_ci * equals the dequeue pointer. 2962306a36Sopenharmony_ci * 3062306a36Sopenharmony_ci * Cycle bit rules: 3162306a36Sopenharmony_ci * 1. When a consumer increments a dequeue pointer and encounters a toggle bit 3262306a36Sopenharmony_ci * in a link TRB, it must toggle the ring cycle state. 3362306a36Sopenharmony_ci * 2. When a producer increments an enqueue pointer and encounters a toggle bit 3462306a36Sopenharmony_ci * in a link TRB, it must toggle the ring cycle state. 3562306a36Sopenharmony_ci * 3662306a36Sopenharmony_ci * Producer rules: 3762306a36Sopenharmony_ci * 1. Check if ring is full before you enqueue. 3862306a36Sopenharmony_ci * 2. Write the ring cycle state to the cycle bit in the TRB you're enqueuing. 3962306a36Sopenharmony_ci * Update enqueue pointer between each write (which may update the ring 4062306a36Sopenharmony_ci * cycle state). 4162306a36Sopenharmony_ci * 3. Notify consumer. If SW is producer, it rings the doorbell for command 4262306a36Sopenharmony_ci * and endpoint rings. If HC is the producer for the event ring, 4362306a36Sopenharmony_ci * and it generates an interrupt according to interrupt modulation rules. 4462306a36Sopenharmony_ci * 4562306a36Sopenharmony_ci * Consumer rules: 4662306a36Sopenharmony_ci * 1. Check if TRB belongs to you. If the cycle bit == your ring cycle state, 4762306a36Sopenharmony_ci * the TRB is owned by the consumer. 4862306a36Sopenharmony_ci * 2. Update dequeue pointer (which may update the ring cycle state) and 4962306a36Sopenharmony_ci * continue processing TRBs until you reach a TRB which is not owned by you. 5062306a36Sopenharmony_ci * 3. Notify the producer. SW is the consumer for the event ring, and it 5162306a36Sopenharmony_ci * updates event ring dequeue pointer. HC is the consumer for the command and 5262306a36Sopenharmony_ci * endpoint rings; it generates events on the event ring for these. 5362306a36Sopenharmony_ci */ 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ci#include <linux/scatterlist.h> 5662306a36Sopenharmony_ci#include <linux/slab.h> 5762306a36Sopenharmony_ci#include <linux/dma-mapping.h> 5862306a36Sopenharmony_ci#include "xhci.h" 5962306a36Sopenharmony_ci#include "xhci-trace.h" 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_cistatic int queue_command(struct xhci_hcd *xhci, struct xhci_command *cmd, 6262306a36Sopenharmony_ci u32 field1, u32 field2, 6362306a36Sopenharmony_ci u32 field3, u32 field4, bool command_must_succeed); 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ci/* 6662306a36Sopenharmony_ci * Returns zero if the TRB isn't in this segment, otherwise it returns the DMA 6762306a36Sopenharmony_ci * address of the TRB. 6862306a36Sopenharmony_ci */ 6962306a36Sopenharmony_cidma_addr_t xhci_trb_virt_to_dma(struct xhci_segment *seg, 7062306a36Sopenharmony_ci union xhci_trb *trb) 7162306a36Sopenharmony_ci{ 7262306a36Sopenharmony_ci unsigned long segment_offset; 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_ci if (!seg || !trb || trb < seg->trbs) 7562306a36Sopenharmony_ci return 0; 7662306a36Sopenharmony_ci /* offset in TRBs */ 7762306a36Sopenharmony_ci segment_offset = trb - seg->trbs; 7862306a36Sopenharmony_ci if (segment_offset >= TRBS_PER_SEGMENT) 7962306a36Sopenharmony_ci return 0; 8062306a36Sopenharmony_ci return seg->dma + (segment_offset * sizeof(*trb)); 8162306a36Sopenharmony_ci} 8262306a36Sopenharmony_ci 8362306a36Sopenharmony_cistatic bool trb_is_noop(union xhci_trb *trb) 8462306a36Sopenharmony_ci{ 8562306a36Sopenharmony_ci return TRB_TYPE_NOOP_LE32(trb->generic.field[3]); 8662306a36Sopenharmony_ci} 8762306a36Sopenharmony_ci 8862306a36Sopenharmony_cistatic bool trb_is_link(union xhci_trb *trb) 8962306a36Sopenharmony_ci{ 9062306a36Sopenharmony_ci return TRB_TYPE_LINK_LE32(trb->link.control); 9162306a36Sopenharmony_ci} 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_cistatic bool last_trb_on_seg(struct xhci_segment *seg, union xhci_trb *trb) 9462306a36Sopenharmony_ci{ 9562306a36Sopenharmony_ci return trb == &seg->trbs[TRBS_PER_SEGMENT - 1]; 9662306a36Sopenharmony_ci} 9762306a36Sopenharmony_ci 9862306a36Sopenharmony_cistatic bool last_trb_on_ring(struct xhci_ring *ring, 9962306a36Sopenharmony_ci struct xhci_segment *seg, union xhci_trb *trb) 10062306a36Sopenharmony_ci{ 10162306a36Sopenharmony_ci return last_trb_on_seg(seg, trb) && (seg->next == ring->first_seg); 10262306a36Sopenharmony_ci} 10362306a36Sopenharmony_ci 10462306a36Sopenharmony_cistatic bool link_trb_toggles_cycle(union xhci_trb *trb) 10562306a36Sopenharmony_ci{ 10662306a36Sopenharmony_ci return le32_to_cpu(trb->link.control) & LINK_TOGGLE; 10762306a36Sopenharmony_ci} 10862306a36Sopenharmony_ci 10962306a36Sopenharmony_cistatic bool last_td_in_urb(struct xhci_td *td) 11062306a36Sopenharmony_ci{ 11162306a36Sopenharmony_ci struct urb_priv *urb_priv = td->urb->hcpriv; 11262306a36Sopenharmony_ci 11362306a36Sopenharmony_ci return urb_priv->num_tds_done == urb_priv->num_tds; 11462306a36Sopenharmony_ci} 11562306a36Sopenharmony_ci 11662306a36Sopenharmony_cistatic void inc_td_cnt(struct urb *urb) 11762306a36Sopenharmony_ci{ 11862306a36Sopenharmony_ci struct urb_priv *urb_priv = urb->hcpriv; 11962306a36Sopenharmony_ci 12062306a36Sopenharmony_ci urb_priv->num_tds_done++; 12162306a36Sopenharmony_ci} 12262306a36Sopenharmony_ci 12362306a36Sopenharmony_cistatic void trb_to_noop(union xhci_trb *trb, u32 noop_type) 12462306a36Sopenharmony_ci{ 12562306a36Sopenharmony_ci if (trb_is_link(trb)) { 12662306a36Sopenharmony_ci /* unchain chained link TRBs */ 12762306a36Sopenharmony_ci trb->link.control &= cpu_to_le32(~TRB_CHAIN); 12862306a36Sopenharmony_ci } else { 12962306a36Sopenharmony_ci trb->generic.field[0] = 0; 13062306a36Sopenharmony_ci trb->generic.field[1] = 0; 13162306a36Sopenharmony_ci trb->generic.field[2] = 0; 13262306a36Sopenharmony_ci /* Preserve only the cycle bit of this TRB */ 13362306a36Sopenharmony_ci trb->generic.field[3] &= cpu_to_le32(TRB_CYCLE); 13462306a36Sopenharmony_ci trb->generic.field[3] |= cpu_to_le32(TRB_TYPE(noop_type)); 13562306a36Sopenharmony_ci } 13662306a36Sopenharmony_ci} 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_ci/* Updates trb to point to the next TRB in the ring, and updates seg if the next 13962306a36Sopenharmony_ci * TRB is in a new segment. This does not skip over link TRBs, and it does not 14062306a36Sopenharmony_ci * effect the ring dequeue or enqueue pointers. 14162306a36Sopenharmony_ci */ 14262306a36Sopenharmony_cistatic void next_trb(struct xhci_hcd *xhci, 14362306a36Sopenharmony_ci struct xhci_ring *ring, 14462306a36Sopenharmony_ci struct xhci_segment **seg, 14562306a36Sopenharmony_ci union xhci_trb **trb) 14662306a36Sopenharmony_ci{ 14762306a36Sopenharmony_ci if (trb_is_link(*trb)) { 14862306a36Sopenharmony_ci *seg = (*seg)->next; 14962306a36Sopenharmony_ci *trb = ((*seg)->trbs); 15062306a36Sopenharmony_ci } else { 15162306a36Sopenharmony_ci (*trb)++; 15262306a36Sopenharmony_ci } 15362306a36Sopenharmony_ci} 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_ci/* 15662306a36Sopenharmony_ci * See Cycle bit rules. SW is the consumer for the event ring only. 15762306a36Sopenharmony_ci */ 15862306a36Sopenharmony_civoid inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring) 15962306a36Sopenharmony_ci{ 16062306a36Sopenharmony_ci unsigned int link_trb_count = 0; 16162306a36Sopenharmony_ci 16262306a36Sopenharmony_ci /* event ring doesn't have link trbs, check for last trb */ 16362306a36Sopenharmony_ci if (ring->type == TYPE_EVENT) { 16462306a36Sopenharmony_ci if (!last_trb_on_seg(ring->deq_seg, ring->dequeue)) { 16562306a36Sopenharmony_ci ring->dequeue++; 16662306a36Sopenharmony_ci goto out; 16762306a36Sopenharmony_ci } 16862306a36Sopenharmony_ci if (last_trb_on_ring(ring, ring->deq_seg, ring->dequeue)) 16962306a36Sopenharmony_ci ring->cycle_state ^= 1; 17062306a36Sopenharmony_ci ring->deq_seg = ring->deq_seg->next; 17162306a36Sopenharmony_ci ring->dequeue = ring->deq_seg->trbs; 17262306a36Sopenharmony_ci goto out; 17362306a36Sopenharmony_ci } 17462306a36Sopenharmony_ci 17562306a36Sopenharmony_ci /* All other rings have link trbs */ 17662306a36Sopenharmony_ci if (!trb_is_link(ring->dequeue)) { 17762306a36Sopenharmony_ci if (last_trb_on_seg(ring->deq_seg, ring->dequeue)) 17862306a36Sopenharmony_ci xhci_warn(xhci, "Missing link TRB at end of segment\n"); 17962306a36Sopenharmony_ci else 18062306a36Sopenharmony_ci ring->dequeue++; 18162306a36Sopenharmony_ci } 18262306a36Sopenharmony_ci 18362306a36Sopenharmony_ci while (trb_is_link(ring->dequeue)) { 18462306a36Sopenharmony_ci ring->deq_seg = ring->deq_seg->next; 18562306a36Sopenharmony_ci ring->dequeue = ring->deq_seg->trbs; 18662306a36Sopenharmony_ci 18762306a36Sopenharmony_ci if (link_trb_count++ > ring->num_segs) { 18862306a36Sopenharmony_ci xhci_warn(xhci, "Ring is an endless link TRB loop\n"); 18962306a36Sopenharmony_ci break; 19062306a36Sopenharmony_ci } 19162306a36Sopenharmony_ci } 19262306a36Sopenharmony_ciout: 19362306a36Sopenharmony_ci trace_xhci_inc_deq(ring); 19462306a36Sopenharmony_ci 19562306a36Sopenharmony_ci return; 19662306a36Sopenharmony_ci} 19762306a36Sopenharmony_ci 19862306a36Sopenharmony_ci/* 19962306a36Sopenharmony_ci * See Cycle bit rules. SW is the consumer for the event ring only. 20062306a36Sopenharmony_ci * 20162306a36Sopenharmony_ci * If we've just enqueued a TRB that is in the middle of a TD (meaning the 20262306a36Sopenharmony_ci * chain bit is set), then set the chain bit in all the following link TRBs. 20362306a36Sopenharmony_ci * If we've enqueued the last TRB in a TD, make sure the following link TRBs 20462306a36Sopenharmony_ci * have their chain bit cleared (so that each Link TRB is a separate TD). 20562306a36Sopenharmony_ci * 20662306a36Sopenharmony_ci * Section 6.4.4.1 of the 0.95 spec says link TRBs cannot have the chain bit 20762306a36Sopenharmony_ci * set, but other sections talk about dealing with the chain bit set. This was 20862306a36Sopenharmony_ci * fixed in the 0.96 specification errata, but we have to assume that all 0.95 20962306a36Sopenharmony_ci * xHCI hardware can't handle the chain bit being cleared on a link TRB. 21062306a36Sopenharmony_ci * 21162306a36Sopenharmony_ci * @more_trbs_coming: Will you enqueue more TRBs before calling 21262306a36Sopenharmony_ci * prepare_transfer()? 21362306a36Sopenharmony_ci */ 21462306a36Sopenharmony_cistatic void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring, 21562306a36Sopenharmony_ci bool more_trbs_coming) 21662306a36Sopenharmony_ci{ 21762306a36Sopenharmony_ci u32 chain; 21862306a36Sopenharmony_ci union xhci_trb *next; 21962306a36Sopenharmony_ci unsigned int link_trb_count = 0; 22062306a36Sopenharmony_ci 22162306a36Sopenharmony_ci chain = le32_to_cpu(ring->enqueue->generic.field[3]) & TRB_CHAIN; 22262306a36Sopenharmony_ci 22362306a36Sopenharmony_ci if (last_trb_on_seg(ring->enq_seg, ring->enqueue)) { 22462306a36Sopenharmony_ci xhci_err(xhci, "Tried to move enqueue past ring segment\n"); 22562306a36Sopenharmony_ci return; 22662306a36Sopenharmony_ci } 22762306a36Sopenharmony_ci 22862306a36Sopenharmony_ci next = ++(ring->enqueue); 22962306a36Sopenharmony_ci 23062306a36Sopenharmony_ci /* Update the dequeue pointer further if that was a link TRB */ 23162306a36Sopenharmony_ci while (trb_is_link(next)) { 23262306a36Sopenharmony_ci 23362306a36Sopenharmony_ci /* 23462306a36Sopenharmony_ci * If the caller doesn't plan on enqueueing more TDs before 23562306a36Sopenharmony_ci * ringing the doorbell, then we don't want to give the link TRB 23662306a36Sopenharmony_ci * to the hardware just yet. We'll give the link TRB back in 23762306a36Sopenharmony_ci * prepare_ring() just before we enqueue the TD at the top of 23862306a36Sopenharmony_ci * the ring. 23962306a36Sopenharmony_ci */ 24062306a36Sopenharmony_ci if (!chain && !more_trbs_coming) 24162306a36Sopenharmony_ci break; 24262306a36Sopenharmony_ci 24362306a36Sopenharmony_ci /* If we're not dealing with 0.95 hardware or isoc rings on 24462306a36Sopenharmony_ci * AMD 0.96 host, carry over the chain bit of the previous TRB 24562306a36Sopenharmony_ci * (which may mean the chain bit is cleared). 24662306a36Sopenharmony_ci */ 24762306a36Sopenharmony_ci if (!(ring->type == TYPE_ISOC && 24862306a36Sopenharmony_ci (xhci->quirks & XHCI_AMD_0x96_HOST)) && 24962306a36Sopenharmony_ci !xhci_link_trb_quirk(xhci)) { 25062306a36Sopenharmony_ci next->link.control &= cpu_to_le32(~TRB_CHAIN); 25162306a36Sopenharmony_ci next->link.control |= cpu_to_le32(chain); 25262306a36Sopenharmony_ci } 25362306a36Sopenharmony_ci /* Give this link TRB to the hardware */ 25462306a36Sopenharmony_ci wmb(); 25562306a36Sopenharmony_ci next->link.control ^= cpu_to_le32(TRB_CYCLE); 25662306a36Sopenharmony_ci 25762306a36Sopenharmony_ci /* Toggle the cycle bit after the last ring segment. */ 25862306a36Sopenharmony_ci if (link_trb_toggles_cycle(next)) 25962306a36Sopenharmony_ci ring->cycle_state ^= 1; 26062306a36Sopenharmony_ci 26162306a36Sopenharmony_ci ring->enq_seg = ring->enq_seg->next; 26262306a36Sopenharmony_ci ring->enqueue = ring->enq_seg->trbs; 26362306a36Sopenharmony_ci next = ring->enqueue; 26462306a36Sopenharmony_ci 26562306a36Sopenharmony_ci if (link_trb_count++ > ring->num_segs) { 26662306a36Sopenharmony_ci xhci_warn(xhci, "%s: Ring link TRB loop\n", __func__); 26762306a36Sopenharmony_ci break; 26862306a36Sopenharmony_ci } 26962306a36Sopenharmony_ci } 27062306a36Sopenharmony_ci 27162306a36Sopenharmony_ci trace_xhci_inc_enq(ring); 27262306a36Sopenharmony_ci} 27362306a36Sopenharmony_ci 27462306a36Sopenharmony_ci/* 27562306a36Sopenharmony_ci * Return number of free normal TRBs from enqueue to dequeue pointer on ring. 27662306a36Sopenharmony_ci * Not counting an assumed link TRB at end of each TRBS_PER_SEGMENT sized segment. 27762306a36Sopenharmony_ci * Only for transfer and command rings where driver is the producer, not for 27862306a36Sopenharmony_ci * event rings. 27962306a36Sopenharmony_ci */ 28062306a36Sopenharmony_cistatic unsigned int xhci_num_trbs_free(struct xhci_hcd *xhci, struct xhci_ring *ring) 28162306a36Sopenharmony_ci{ 28262306a36Sopenharmony_ci struct xhci_segment *enq_seg = ring->enq_seg; 28362306a36Sopenharmony_ci union xhci_trb *enq = ring->enqueue; 28462306a36Sopenharmony_ci union xhci_trb *last_on_seg; 28562306a36Sopenharmony_ci unsigned int free = 0; 28662306a36Sopenharmony_ci int i = 0; 28762306a36Sopenharmony_ci 28862306a36Sopenharmony_ci /* Ring might be empty even if enq != deq if enq is left on a link trb */ 28962306a36Sopenharmony_ci if (trb_is_link(enq)) { 29062306a36Sopenharmony_ci enq_seg = enq_seg->next; 29162306a36Sopenharmony_ci enq = enq_seg->trbs; 29262306a36Sopenharmony_ci } 29362306a36Sopenharmony_ci 29462306a36Sopenharmony_ci /* Empty ring, common case, don't walk the segments */ 29562306a36Sopenharmony_ci if (enq == ring->dequeue) 29662306a36Sopenharmony_ci return ring->num_segs * (TRBS_PER_SEGMENT - 1); 29762306a36Sopenharmony_ci 29862306a36Sopenharmony_ci do { 29962306a36Sopenharmony_ci if (ring->deq_seg == enq_seg && ring->dequeue >= enq) 30062306a36Sopenharmony_ci return free + (ring->dequeue - enq); 30162306a36Sopenharmony_ci last_on_seg = &enq_seg->trbs[TRBS_PER_SEGMENT - 1]; 30262306a36Sopenharmony_ci free += last_on_seg - enq; 30362306a36Sopenharmony_ci enq_seg = enq_seg->next; 30462306a36Sopenharmony_ci enq = enq_seg->trbs; 30562306a36Sopenharmony_ci } while (i++ <= ring->num_segs); 30662306a36Sopenharmony_ci 30762306a36Sopenharmony_ci return free; 30862306a36Sopenharmony_ci} 30962306a36Sopenharmony_ci 31062306a36Sopenharmony_ci/* 31162306a36Sopenharmony_ci * Check to see if there's room to enqueue num_trbs on the ring and make sure 31262306a36Sopenharmony_ci * enqueue pointer will not advance into dequeue segment. See rules above. 31362306a36Sopenharmony_ci * return number of new segments needed to ensure this. 31462306a36Sopenharmony_ci */ 31562306a36Sopenharmony_ci 31662306a36Sopenharmony_cistatic unsigned int xhci_ring_expansion_needed(struct xhci_hcd *xhci, struct xhci_ring *ring, 31762306a36Sopenharmony_ci unsigned int num_trbs) 31862306a36Sopenharmony_ci{ 31962306a36Sopenharmony_ci struct xhci_segment *seg; 32062306a36Sopenharmony_ci int trbs_past_seg; 32162306a36Sopenharmony_ci int enq_used; 32262306a36Sopenharmony_ci int new_segs; 32362306a36Sopenharmony_ci 32462306a36Sopenharmony_ci enq_used = ring->enqueue - ring->enq_seg->trbs; 32562306a36Sopenharmony_ci 32662306a36Sopenharmony_ci /* how many trbs will be queued past the enqueue segment? */ 32762306a36Sopenharmony_ci trbs_past_seg = enq_used + num_trbs - (TRBS_PER_SEGMENT - 1); 32862306a36Sopenharmony_ci 32962306a36Sopenharmony_ci if (trbs_past_seg <= 0) 33062306a36Sopenharmony_ci return 0; 33162306a36Sopenharmony_ci 33262306a36Sopenharmony_ci /* Empty ring special case, enqueue stuck on link trb while dequeue advanced */ 33362306a36Sopenharmony_ci if (trb_is_link(ring->enqueue) && ring->enq_seg->next->trbs == ring->dequeue) 33462306a36Sopenharmony_ci return 0; 33562306a36Sopenharmony_ci 33662306a36Sopenharmony_ci new_segs = 1 + (trbs_past_seg / (TRBS_PER_SEGMENT - 1)); 33762306a36Sopenharmony_ci seg = ring->enq_seg; 33862306a36Sopenharmony_ci 33962306a36Sopenharmony_ci while (new_segs > 0) { 34062306a36Sopenharmony_ci seg = seg->next; 34162306a36Sopenharmony_ci if (seg == ring->deq_seg) { 34262306a36Sopenharmony_ci xhci_dbg(xhci, "Ring expansion by %d segments needed\n", 34362306a36Sopenharmony_ci new_segs); 34462306a36Sopenharmony_ci xhci_dbg(xhci, "Adding %d trbs moves enq %d trbs into deq seg\n", 34562306a36Sopenharmony_ci num_trbs, trbs_past_seg % TRBS_PER_SEGMENT); 34662306a36Sopenharmony_ci return new_segs; 34762306a36Sopenharmony_ci } 34862306a36Sopenharmony_ci new_segs--; 34962306a36Sopenharmony_ci } 35062306a36Sopenharmony_ci 35162306a36Sopenharmony_ci return 0; 35262306a36Sopenharmony_ci} 35362306a36Sopenharmony_ci 35462306a36Sopenharmony_ci/* Ring the host controller doorbell after placing a command on the ring */ 35562306a36Sopenharmony_civoid xhci_ring_cmd_db(struct xhci_hcd *xhci) 35662306a36Sopenharmony_ci{ 35762306a36Sopenharmony_ci if (!(xhci->cmd_ring_state & CMD_RING_STATE_RUNNING)) 35862306a36Sopenharmony_ci return; 35962306a36Sopenharmony_ci 36062306a36Sopenharmony_ci xhci_dbg(xhci, "// Ding dong!\n"); 36162306a36Sopenharmony_ci 36262306a36Sopenharmony_ci trace_xhci_ring_host_doorbell(0, DB_VALUE_HOST); 36362306a36Sopenharmony_ci 36462306a36Sopenharmony_ci writel(DB_VALUE_HOST, &xhci->dba->doorbell[0]); 36562306a36Sopenharmony_ci /* Flush PCI posted writes */ 36662306a36Sopenharmony_ci readl(&xhci->dba->doorbell[0]); 36762306a36Sopenharmony_ci} 36862306a36Sopenharmony_ci 36962306a36Sopenharmony_cistatic bool xhci_mod_cmd_timer(struct xhci_hcd *xhci, unsigned long delay) 37062306a36Sopenharmony_ci{ 37162306a36Sopenharmony_ci return mod_delayed_work(system_wq, &xhci->cmd_timer, delay); 37262306a36Sopenharmony_ci} 37362306a36Sopenharmony_ci 37462306a36Sopenharmony_cistatic struct xhci_command *xhci_next_queued_cmd(struct xhci_hcd *xhci) 37562306a36Sopenharmony_ci{ 37662306a36Sopenharmony_ci return list_first_entry_or_null(&xhci->cmd_list, struct xhci_command, 37762306a36Sopenharmony_ci cmd_list); 37862306a36Sopenharmony_ci} 37962306a36Sopenharmony_ci 38062306a36Sopenharmony_ci/* 38162306a36Sopenharmony_ci * Turn all commands on command ring with status set to "aborted" to no-op trbs. 38262306a36Sopenharmony_ci * If there are other commands waiting then restart the ring and kick the timer. 38362306a36Sopenharmony_ci * This must be called with command ring stopped and xhci->lock held. 38462306a36Sopenharmony_ci */ 38562306a36Sopenharmony_cistatic void xhci_handle_stopped_cmd_ring(struct xhci_hcd *xhci, 38662306a36Sopenharmony_ci struct xhci_command *cur_cmd) 38762306a36Sopenharmony_ci{ 38862306a36Sopenharmony_ci struct xhci_command *i_cmd; 38962306a36Sopenharmony_ci 39062306a36Sopenharmony_ci /* Turn all aborted commands in list to no-ops, then restart */ 39162306a36Sopenharmony_ci list_for_each_entry(i_cmd, &xhci->cmd_list, cmd_list) { 39262306a36Sopenharmony_ci 39362306a36Sopenharmony_ci if (i_cmd->status != COMP_COMMAND_ABORTED) 39462306a36Sopenharmony_ci continue; 39562306a36Sopenharmony_ci 39662306a36Sopenharmony_ci i_cmd->status = COMP_COMMAND_RING_STOPPED; 39762306a36Sopenharmony_ci 39862306a36Sopenharmony_ci xhci_dbg(xhci, "Turn aborted command %p to no-op\n", 39962306a36Sopenharmony_ci i_cmd->command_trb); 40062306a36Sopenharmony_ci 40162306a36Sopenharmony_ci trb_to_noop(i_cmd->command_trb, TRB_CMD_NOOP); 40262306a36Sopenharmony_ci 40362306a36Sopenharmony_ci /* 40462306a36Sopenharmony_ci * caller waiting for completion is called when command 40562306a36Sopenharmony_ci * completion event is received for these no-op commands 40662306a36Sopenharmony_ci */ 40762306a36Sopenharmony_ci } 40862306a36Sopenharmony_ci 40962306a36Sopenharmony_ci xhci->cmd_ring_state = CMD_RING_STATE_RUNNING; 41062306a36Sopenharmony_ci 41162306a36Sopenharmony_ci /* ring command ring doorbell to restart the command ring */ 41262306a36Sopenharmony_ci if ((xhci->cmd_ring->dequeue != xhci->cmd_ring->enqueue) && 41362306a36Sopenharmony_ci !(xhci->xhc_state & XHCI_STATE_DYING)) { 41462306a36Sopenharmony_ci xhci->current_cmd = cur_cmd; 41562306a36Sopenharmony_ci xhci_mod_cmd_timer(xhci, XHCI_CMD_DEFAULT_TIMEOUT); 41662306a36Sopenharmony_ci xhci_ring_cmd_db(xhci); 41762306a36Sopenharmony_ci } 41862306a36Sopenharmony_ci} 41962306a36Sopenharmony_ci 42062306a36Sopenharmony_ci/* Must be called with xhci->lock held, releases and aquires lock back */ 42162306a36Sopenharmony_cistatic int xhci_abort_cmd_ring(struct xhci_hcd *xhci, unsigned long flags) 42262306a36Sopenharmony_ci{ 42362306a36Sopenharmony_ci struct xhci_segment *new_seg = xhci->cmd_ring->deq_seg; 42462306a36Sopenharmony_ci union xhci_trb *new_deq = xhci->cmd_ring->dequeue; 42562306a36Sopenharmony_ci u64 crcr; 42662306a36Sopenharmony_ci int ret; 42762306a36Sopenharmony_ci 42862306a36Sopenharmony_ci xhci_dbg(xhci, "Abort command ring\n"); 42962306a36Sopenharmony_ci 43062306a36Sopenharmony_ci reinit_completion(&xhci->cmd_ring_stop_completion); 43162306a36Sopenharmony_ci 43262306a36Sopenharmony_ci /* 43362306a36Sopenharmony_ci * The control bits like command stop, abort are located in lower 43462306a36Sopenharmony_ci * dword of the command ring control register. 43562306a36Sopenharmony_ci * Some controllers require all 64 bits to be written to abort the ring. 43662306a36Sopenharmony_ci * Make sure the upper dword is valid, pointing to the next command, 43762306a36Sopenharmony_ci * avoiding corrupting the command ring pointer in case the command ring 43862306a36Sopenharmony_ci * is stopped by the time the upper dword is written. 43962306a36Sopenharmony_ci */ 44062306a36Sopenharmony_ci next_trb(xhci, NULL, &new_seg, &new_deq); 44162306a36Sopenharmony_ci if (trb_is_link(new_deq)) 44262306a36Sopenharmony_ci next_trb(xhci, NULL, &new_seg, &new_deq); 44362306a36Sopenharmony_ci 44462306a36Sopenharmony_ci crcr = xhci_trb_virt_to_dma(new_seg, new_deq); 44562306a36Sopenharmony_ci xhci_write_64(xhci, crcr | CMD_RING_ABORT, &xhci->op_regs->cmd_ring); 44662306a36Sopenharmony_ci 44762306a36Sopenharmony_ci /* Section 4.6.1.2 of xHCI 1.0 spec says software should also time the 44862306a36Sopenharmony_ci * completion of the Command Abort operation. If CRR is not negated in 5 44962306a36Sopenharmony_ci * seconds then driver handles it as if host died (-ENODEV). 45062306a36Sopenharmony_ci * In the future we should distinguish between -ENODEV and -ETIMEDOUT 45162306a36Sopenharmony_ci * and try to recover a -ETIMEDOUT with a host controller reset. 45262306a36Sopenharmony_ci */ 45362306a36Sopenharmony_ci ret = xhci_handshake(&xhci->op_regs->cmd_ring, 45462306a36Sopenharmony_ci CMD_RING_RUNNING, 0, 5 * 1000 * 1000); 45562306a36Sopenharmony_ci if (ret < 0) { 45662306a36Sopenharmony_ci xhci_err(xhci, "Abort failed to stop command ring: %d\n", ret); 45762306a36Sopenharmony_ci xhci_halt(xhci); 45862306a36Sopenharmony_ci xhci_hc_died(xhci); 45962306a36Sopenharmony_ci return ret; 46062306a36Sopenharmony_ci } 46162306a36Sopenharmony_ci /* 46262306a36Sopenharmony_ci * Writing the CMD_RING_ABORT bit should cause a cmd completion event, 46362306a36Sopenharmony_ci * however on some host hw the CMD_RING_RUNNING bit is correctly cleared 46462306a36Sopenharmony_ci * but the completion event in never sent. Wait 2 secs (arbitrary 46562306a36Sopenharmony_ci * number) to handle those cases after negation of CMD_RING_RUNNING. 46662306a36Sopenharmony_ci */ 46762306a36Sopenharmony_ci spin_unlock_irqrestore(&xhci->lock, flags); 46862306a36Sopenharmony_ci ret = wait_for_completion_timeout(&xhci->cmd_ring_stop_completion, 46962306a36Sopenharmony_ci msecs_to_jiffies(2000)); 47062306a36Sopenharmony_ci spin_lock_irqsave(&xhci->lock, flags); 47162306a36Sopenharmony_ci if (!ret) { 47262306a36Sopenharmony_ci xhci_dbg(xhci, "No stop event for abort, ring start fail?\n"); 47362306a36Sopenharmony_ci xhci_cleanup_command_queue(xhci); 47462306a36Sopenharmony_ci } else { 47562306a36Sopenharmony_ci xhci_handle_stopped_cmd_ring(xhci, xhci_next_queued_cmd(xhci)); 47662306a36Sopenharmony_ci } 47762306a36Sopenharmony_ci return 0; 47862306a36Sopenharmony_ci} 47962306a36Sopenharmony_ci 48062306a36Sopenharmony_civoid xhci_ring_ep_doorbell(struct xhci_hcd *xhci, 48162306a36Sopenharmony_ci unsigned int slot_id, 48262306a36Sopenharmony_ci unsigned int ep_index, 48362306a36Sopenharmony_ci unsigned int stream_id) 48462306a36Sopenharmony_ci{ 48562306a36Sopenharmony_ci __le32 __iomem *db_addr = &xhci->dba->doorbell[slot_id]; 48662306a36Sopenharmony_ci struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index]; 48762306a36Sopenharmony_ci unsigned int ep_state = ep->ep_state; 48862306a36Sopenharmony_ci 48962306a36Sopenharmony_ci /* Don't ring the doorbell for this endpoint if there are pending 49062306a36Sopenharmony_ci * cancellations because we don't want to interrupt processing. 49162306a36Sopenharmony_ci * We don't want to restart any stream rings if there's a set dequeue 49262306a36Sopenharmony_ci * pointer command pending because the device can choose to start any 49362306a36Sopenharmony_ci * stream once the endpoint is on the HW schedule. 49462306a36Sopenharmony_ci */ 49562306a36Sopenharmony_ci if ((ep_state & EP_STOP_CMD_PENDING) || (ep_state & SET_DEQ_PENDING) || 49662306a36Sopenharmony_ci (ep_state & EP_HALTED) || (ep_state & EP_CLEARING_TT)) 49762306a36Sopenharmony_ci return; 49862306a36Sopenharmony_ci 49962306a36Sopenharmony_ci trace_xhci_ring_ep_doorbell(slot_id, DB_VALUE(ep_index, stream_id)); 50062306a36Sopenharmony_ci 50162306a36Sopenharmony_ci writel(DB_VALUE(ep_index, stream_id), db_addr); 50262306a36Sopenharmony_ci /* flush the write */ 50362306a36Sopenharmony_ci readl(db_addr); 50462306a36Sopenharmony_ci} 50562306a36Sopenharmony_ci 50662306a36Sopenharmony_ci/* Ring the doorbell for any rings with pending URBs */ 50762306a36Sopenharmony_cistatic void ring_doorbell_for_active_rings(struct xhci_hcd *xhci, 50862306a36Sopenharmony_ci unsigned int slot_id, 50962306a36Sopenharmony_ci unsigned int ep_index) 51062306a36Sopenharmony_ci{ 51162306a36Sopenharmony_ci unsigned int stream_id; 51262306a36Sopenharmony_ci struct xhci_virt_ep *ep; 51362306a36Sopenharmony_ci 51462306a36Sopenharmony_ci ep = &xhci->devs[slot_id]->eps[ep_index]; 51562306a36Sopenharmony_ci 51662306a36Sopenharmony_ci /* A ring has pending URBs if its TD list is not empty */ 51762306a36Sopenharmony_ci if (!(ep->ep_state & EP_HAS_STREAMS)) { 51862306a36Sopenharmony_ci if (ep->ring && !(list_empty(&ep->ring->td_list))) 51962306a36Sopenharmony_ci xhci_ring_ep_doorbell(xhci, slot_id, ep_index, 0); 52062306a36Sopenharmony_ci return; 52162306a36Sopenharmony_ci } 52262306a36Sopenharmony_ci 52362306a36Sopenharmony_ci for (stream_id = 1; stream_id < ep->stream_info->num_streams; 52462306a36Sopenharmony_ci stream_id++) { 52562306a36Sopenharmony_ci struct xhci_stream_info *stream_info = ep->stream_info; 52662306a36Sopenharmony_ci if (!list_empty(&stream_info->stream_rings[stream_id]->td_list)) 52762306a36Sopenharmony_ci xhci_ring_ep_doorbell(xhci, slot_id, ep_index, 52862306a36Sopenharmony_ci stream_id); 52962306a36Sopenharmony_ci } 53062306a36Sopenharmony_ci} 53162306a36Sopenharmony_ci 53262306a36Sopenharmony_civoid xhci_ring_doorbell_for_active_rings(struct xhci_hcd *xhci, 53362306a36Sopenharmony_ci unsigned int slot_id, 53462306a36Sopenharmony_ci unsigned int ep_index) 53562306a36Sopenharmony_ci{ 53662306a36Sopenharmony_ci ring_doorbell_for_active_rings(xhci, slot_id, ep_index); 53762306a36Sopenharmony_ci} 53862306a36Sopenharmony_ci 53962306a36Sopenharmony_cistatic struct xhci_virt_ep *xhci_get_virt_ep(struct xhci_hcd *xhci, 54062306a36Sopenharmony_ci unsigned int slot_id, 54162306a36Sopenharmony_ci unsigned int ep_index) 54262306a36Sopenharmony_ci{ 54362306a36Sopenharmony_ci if (slot_id == 0 || slot_id >= MAX_HC_SLOTS) { 54462306a36Sopenharmony_ci xhci_warn(xhci, "Invalid slot_id %u\n", slot_id); 54562306a36Sopenharmony_ci return NULL; 54662306a36Sopenharmony_ci } 54762306a36Sopenharmony_ci if (ep_index >= EP_CTX_PER_DEV) { 54862306a36Sopenharmony_ci xhci_warn(xhci, "Invalid endpoint index %u\n", ep_index); 54962306a36Sopenharmony_ci return NULL; 55062306a36Sopenharmony_ci } 55162306a36Sopenharmony_ci if (!xhci->devs[slot_id]) { 55262306a36Sopenharmony_ci xhci_warn(xhci, "No xhci virt device for slot_id %u\n", slot_id); 55362306a36Sopenharmony_ci return NULL; 55462306a36Sopenharmony_ci } 55562306a36Sopenharmony_ci 55662306a36Sopenharmony_ci return &xhci->devs[slot_id]->eps[ep_index]; 55762306a36Sopenharmony_ci} 55862306a36Sopenharmony_ci 55962306a36Sopenharmony_cistatic struct xhci_ring *xhci_virt_ep_to_ring(struct xhci_hcd *xhci, 56062306a36Sopenharmony_ci struct xhci_virt_ep *ep, 56162306a36Sopenharmony_ci unsigned int stream_id) 56262306a36Sopenharmony_ci{ 56362306a36Sopenharmony_ci /* common case, no streams */ 56462306a36Sopenharmony_ci if (!(ep->ep_state & EP_HAS_STREAMS)) 56562306a36Sopenharmony_ci return ep->ring; 56662306a36Sopenharmony_ci 56762306a36Sopenharmony_ci if (!ep->stream_info) 56862306a36Sopenharmony_ci return NULL; 56962306a36Sopenharmony_ci 57062306a36Sopenharmony_ci if (stream_id == 0 || stream_id >= ep->stream_info->num_streams) { 57162306a36Sopenharmony_ci xhci_warn(xhci, "Invalid stream_id %u request for slot_id %u ep_index %u\n", 57262306a36Sopenharmony_ci stream_id, ep->vdev->slot_id, ep->ep_index); 57362306a36Sopenharmony_ci return NULL; 57462306a36Sopenharmony_ci } 57562306a36Sopenharmony_ci 57662306a36Sopenharmony_ci return ep->stream_info->stream_rings[stream_id]; 57762306a36Sopenharmony_ci} 57862306a36Sopenharmony_ci 57962306a36Sopenharmony_ci/* Get the right ring for the given slot_id, ep_index and stream_id. 58062306a36Sopenharmony_ci * If the endpoint supports streams, boundary check the URB's stream ID. 58162306a36Sopenharmony_ci * If the endpoint doesn't support streams, return the singular endpoint ring. 58262306a36Sopenharmony_ci */ 58362306a36Sopenharmony_cistruct xhci_ring *xhci_triad_to_transfer_ring(struct xhci_hcd *xhci, 58462306a36Sopenharmony_ci unsigned int slot_id, unsigned int ep_index, 58562306a36Sopenharmony_ci unsigned int stream_id) 58662306a36Sopenharmony_ci{ 58762306a36Sopenharmony_ci struct xhci_virt_ep *ep; 58862306a36Sopenharmony_ci 58962306a36Sopenharmony_ci ep = xhci_get_virt_ep(xhci, slot_id, ep_index); 59062306a36Sopenharmony_ci if (!ep) 59162306a36Sopenharmony_ci return NULL; 59262306a36Sopenharmony_ci 59362306a36Sopenharmony_ci return xhci_virt_ep_to_ring(xhci, ep, stream_id); 59462306a36Sopenharmony_ci} 59562306a36Sopenharmony_ci 59662306a36Sopenharmony_ci 59762306a36Sopenharmony_ci/* 59862306a36Sopenharmony_ci * Get the hw dequeue pointer xHC stopped on, either directly from the 59962306a36Sopenharmony_ci * endpoint context, or if streams are in use from the stream context. 60062306a36Sopenharmony_ci * The returned hw_dequeue contains the lowest four bits with cycle state 60162306a36Sopenharmony_ci * and possbile stream context type. 60262306a36Sopenharmony_ci */ 60362306a36Sopenharmony_cistatic u64 xhci_get_hw_deq(struct xhci_hcd *xhci, struct xhci_virt_device *vdev, 60462306a36Sopenharmony_ci unsigned int ep_index, unsigned int stream_id) 60562306a36Sopenharmony_ci{ 60662306a36Sopenharmony_ci struct xhci_ep_ctx *ep_ctx; 60762306a36Sopenharmony_ci struct xhci_stream_ctx *st_ctx; 60862306a36Sopenharmony_ci struct xhci_virt_ep *ep; 60962306a36Sopenharmony_ci 61062306a36Sopenharmony_ci ep = &vdev->eps[ep_index]; 61162306a36Sopenharmony_ci 61262306a36Sopenharmony_ci if (ep->ep_state & EP_HAS_STREAMS) { 61362306a36Sopenharmony_ci st_ctx = &ep->stream_info->stream_ctx_array[stream_id]; 61462306a36Sopenharmony_ci return le64_to_cpu(st_ctx->stream_ring); 61562306a36Sopenharmony_ci } 61662306a36Sopenharmony_ci ep_ctx = xhci_get_ep_ctx(xhci, vdev->out_ctx, ep_index); 61762306a36Sopenharmony_ci return le64_to_cpu(ep_ctx->deq); 61862306a36Sopenharmony_ci} 61962306a36Sopenharmony_ci 62062306a36Sopenharmony_cistatic int xhci_move_dequeue_past_td(struct xhci_hcd *xhci, 62162306a36Sopenharmony_ci unsigned int slot_id, unsigned int ep_index, 62262306a36Sopenharmony_ci unsigned int stream_id, struct xhci_td *td) 62362306a36Sopenharmony_ci{ 62462306a36Sopenharmony_ci struct xhci_virt_device *dev = xhci->devs[slot_id]; 62562306a36Sopenharmony_ci struct xhci_virt_ep *ep = &dev->eps[ep_index]; 62662306a36Sopenharmony_ci struct xhci_ring *ep_ring; 62762306a36Sopenharmony_ci struct xhci_command *cmd; 62862306a36Sopenharmony_ci struct xhci_segment *new_seg; 62962306a36Sopenharmony_ci union xhci_trb *new_deq; 63062306a36Sopenharmony_ci int new_cycle; 63162306a36Sopenharmony_ci dma_addr_t addr; 63262306a36Sopenharmony_ci u64 hw_dequeue; 63362306a36Sopenharmony_ci bool cycle_found = false; 63462306a36Sopenharmony_ci bool td_last_trb_found = false; 63562306a36Sopenharmony_ci u32 trb_sct = 0; 63662306a36Sopenharmony_ci int ret; 63762306a36Sopenharmony_ci 63862306a36Sopenharmony_ci ep_ring = xhci_triad_to_transfer_ring(xhci, slot_id, 63962306a36Sopenharmony_ci ep_index, stream_id); 64062306a36Sopenharmony_ci if (!ep_ring) { 64162306a36Sopenharmony_ci xhci_warn(xhci, "WARN can't find new dequeue, invalid stream ID %u\n", 64262306a36Sopenharmony_ci stream_id); 64362306a36Sopenharmony_ci return -ENODEV; 64462306a36Sopenharmony_ci } 64562306a36Sopenharmony_ci /* 64662306a36Sopenharmony_ci * A cancelled TD can complete with a stall if HW cached the trb. 64762306a36Sopenharmony_ci * In this case driver can't find td, but if the ring is empty we 64862306a36Sopenharmony_ci * can move the dequeue pointer to the current enqueue position. 64962306a36Sopenharmony_ci * We shouldn't hit this anymore as cached cancelled TRBs are given back 65062306a36Sopenharmony_ci * after clearing the cache, but be on the safe side and keep it anyway 65162306a36Sopenharmony_ci */ 65262306a36Sopenharmony_ci if (!td) { 65362306a36Sopenharmony_ci if (list_empty(&ep_ring->td_list)) { 65462306a36Sopenharmony_ci new_seg = ep_ring->enq_seg; 65562306a36Sopenharmony_ci new_deq = ep_ring->enqueue; 65662306a36Sopenharmony_ci new_cycle = ep_ring->cycle_state; 65762306a36Sopenharmony_ci xhci_dbg(xhci, "ep ring empty, Set new dequeue = enqueue"); 65862306a36Sopenharmony_ci goto deq_found; 65962306a36Sopenharmony_ci } else { 66062306a36Sopenharmony_ci xhci_warn(xhci, "Can't find new dequeue state, missing td\n"); 66162306a36Sopenharmony_ci return -EINVAL; 66262306a36Sopenharmony_ci } 66362306a36Sopenharmony_ci } 66462306a36Sopenharmony_ci 66562306a36Sopenharmony_ci hw_dequeue = xhci_get_hw_deq(xhci, dev, ep_index, stream_id); 66662306a36Sopenharmony_ci new_seg = ep_ring->deq_seg; 66762306a36Sopenharmony_ci new_deq = ep_ring->dequeue; 66862306a36Sopenharmony_ci new_cycle = hw_dequeue & 0x1; 66962306a36Sopenharmony_ci 67062306a36Sopenharmony_ci /* 67162306a36Sopenharmony_ci * We want to find the pointer, segment and cycle state of the new trb 67262306a36Sopenharmony_ci * (the one after current TD's last_trb). We know the cycle state at 67362306a36Sopenharmony_ci * hw_dequeue, so walk the ring until both hw_dequeue and last_trb are 67462306a36Sopenharmony_ci * found. 67562306a36Sopenharmony_ci */ 67662306a36Sopenharmony_ci do { 67762306a36Sopenharmony_ci if (!cycle_found && xhci_trb_virt_to_dma(new_seg, new_deq) 67862306a36Sopenharmony_ci == (dma_addr_t)(hw_dequeue & ~0xf)) { 67962306a36Sopenharmony_ci cycle_found = true; 68062306a36Sopenharmony_ci if (td_last_trb_found) 68162306a36Sopenharmony_ci break; 68262306a36Sopenharmony_ci } 68362306a36Sopenharmony_ci if (new_deq == td->last_trb) 68462306a36Sopenharmony_ci td_last_trb_found = true; 68562306a36Sopenharmony_ci 68662306a36Sopenharmony_ci if (cycle_found && trb_is_link(new_deq) && 68762306a36Sopenharmony_ci link_trb_toggles_cycle(new_deq)) 68862306a36Sopenharmony_ci new_cycle ^= 0x1; 68962306a36Sopenharmony_ci 69062306a36Sopenharmony_ci next_trb(xhci, ep_ring, &new_seg, &new_deq); 69162306a36Sopenharmony_ci 69262306a36Sopenharmony_ci /* Search wrapped around, bail out */ 69362306a36Sopenharmony_ci if (new_deq == ep->ring->dequeue) { 69462306a36Sopenharmony_ci xhci_err(xhci, "Error: Failed finding new dequeue state\n"); 69562306a36Sopenharmony_ci return -EINVAL; 69662306a36Sopenharmony_ci } 69762306a36Sopenharmony_ci 69862306a36Sopenharmony_ci } while (!cycle_found || !td_last_trb_found); 69962306a36Sopenharmony_ci 70062306a36Sopenharmony_cideq_found: 70162306a36Sopenharmony_ci 70262306a36Sopenharmony_ci /* Don't update the ring cycle state for the producer (us). */ 70362306a36Sopenharmony_ci addr = xhci_trb_virt_to_dma(new_seg, new_deq); 70462306a36Sopenharmony_ci if (addr == 0) { 70562306a36Sopenharmony_ci xhci_warn(xhci, "Can't find dma of new dequeue ptr\n"); 70662306a36Sopenharmony_ci xhci_warn(xhci, "deq seg = %p, deq ptr = %p\n", new_seg, new_deq); 70762306a36Sopenharmony_ci return -EINVAL; 70862306a36Sopenharmony_ci } 70962306a36Sopenharmony_ci 71062306a36Sopenharmony_ci if ((ep->ep_state & SET_DEQ_PENDING)) { 71162306a36Sopenharmony_ci xhci_warn(xhci, "Set TR Deq already pending, don't submit for 0x%pad\n", 71262306a36Sopenharmony_ci &addr); 71362306a36Sopenharmony_ci return -EBUSY; 71462306a36Sopenharmony_ci } 71562306a36Sopenharmony_ci 71662306a36Sopenharmony_ci /* This function gets called from contexts where it cannot sleep */ 71762306a36Sopenharmony_ci cmd = xhci_alloc_command(xhci, false, GFP_ATOMIC); 71862306a36Sopenharmony_ci if (!cmd) { 71962306a36Sopenharmony_ci xhci_warn(xhci, "Can't alloc Set TR Deq cmd 0x%pad\n", &addr); 72062306a36Sopenharmony_ci return -ENOMEM; 72162306a36Sopenharmony_ci } 72262306a36Sopenharmony_ci 72362306a36Sopenharmony_ci if (stream_id) 72462306a36Sopenharmony_ci trb_sct = SCT_FOR_TRB(SCT_PRI_TR); 72562306a36Sopenharmony_ci ret = queue_command(xhci, cmd, 72662306a36Sopenharmony_ci lower_32_bits(addr) | trb_sct | new_cycle, 72762306a36Sopenharmony_ci upper_32_bits(addr), 72862306a36Sopenharmony_ci STREAM_ID_FOR_TRB(stream_id), SLOT_ID_FOR_TRB(slot_id) | 72962306a36Sopenharmony_ci EP_ID_FOR_TRB(ep_index) | TRB_TYPE(TRB_SET_DEQ), false); 73062306a36Sopenharmony_ci if (ret < 0) { 73162306a36Sopenharmony_ci xhci_free_command(xhci, cmd); 73262306a36Sopenharmony_ci return ret; 73362306a36Sopenharmony_ci } 73462306a36Sopenharmony_ci ep->queued_deq_seg = new_seg; 73562306a36Sopenharmony_ci ep->queued_deq_ptr = new_deq; 73662306a36Sopenharmony_ci 73762306a36Sopenharmony_ci xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, 73862306a36Sopenharmony_ci "Set TR Deq ptr 0x%llx, cycle %u\n", addr, new_cycle); 73962306a36Sopenharmony_ci 74062306a36Sopenharmony_ci /* Stop the TD queueing code from ringing the doorbell until 74162306a36Sopenharmony_ci * this command completes. The HC won't set the dequeue pointer 74262306a36Sopenharmony_ci * if the ring is running, and ringing the doorbell starts the 74362306a36Sopenharmony_ci * ring running. 74462306a36Sopenharmony_ci */ 74562306a36Sopenharmony_ci ep->ep_state |= SET_DEQ_PENDING; 74662306a36Sopenharmony_ci xhci_ring_cmd_db(xhci); 74762306a36Sopenharmony_ci return 0; 74862306a36Sopenharmony_ci} 74962306a36Sopenharmony_ci 75062306a36Sopenharmony_ci/* flip_cycle means flip the cycle bit of all but the first and last TRB. 75162306a36Sopenharmony_ci * (The last TRB actually points to the ring enqueue pointer, which is not part 75262306a36Sopenharmony_ci * of this TD.) This is used to remove partially enqueued isoc TDs from a ring. 75362306a36Sopenharmony_ci */ 75462306a36Sopenharmony_cistatic void td_to_noop(struct xhci_hcd *xhci, struct xhci_ring *ep_ring, 75562306a36Sopenharmony_ci struct xhci_td *td, bool flip_cycle) 75662306a36Sopenharmony_ci{ 75762306a36Sopenharmony_ci struct xhci_segment *seg = td->start_seg; 75862306a36Sopenharmony_ci union xhci_trb *trb = td->first_trb; 75962306a36Sopenharmony_ci 76062306a36Sopenharmony_ci while (1) { 76162306a36Sopenharmony_ci trb_to_noop(trb, TRB_TR_NOOP); 76262306a36Sopenharmony_ci 76362306a36Sopenharmony_ci /* flip cycle if asked to */ 76462306a36Sopenharmony_ci if (flip_cycle && trb != td->first_trb && trb != td->last_trb) 76562306a36Sopenharmony_ci trb->generic.field[3] ^= cpu_to_le32(TRB_CYCLE); 76662306a36Sopenharmony_ci 76762306a36Sopenharmony_ci if (trb == td->last_trb) 76862306a36Sopenharmony_ci break; 76962306a36Sopenharmony_ci 77062306a36Sopenharmony_ci next_trb(xhci, ep_ring, &seg, &trb); 77162306a36Sopenharmony_ci } 77262306a36Sopenharmony_ci} 77362306a36Sopenharmony_ci 77462306a36Sopenharmony_ci/* 77562306a36Sopenharmony_ci * Must be called with xhci->lock held in interrupt context, 77662306a36Sopenharmony_ci * releases and re-acquires xhci->lock 77762306a36Sopenharmony_ci */ 77862306a36Sopenharmony_cistatic void xhci_giveback_urb_in_irq(struct xhci_hcd *xhci, 77962306a36Sopenharmony_ci struct xhci_td *cur_td, int status) 78062306a36Sopenharmony_ci{ 78162306a36Sopenharmony_ci struct urb *urb = cur_td->urb; 78262306a36Sopenharmony_ci struct urb_priv *urb_priv = urb->hcpriv; 78362306a36Sopenharmony_ci struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus); 78462306a36Sopenharmony_ci 78562306a36Sopenharmony_ci if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) { 78662306a36Sopenharmony_ci xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs--; 78762306a36Sopenharmony_ci if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs == 0) { 78862306a36Sopenharmony_ci if (xhci->quirks & XHCI_AMD_PLL_FIX) 78962306a36Sopenharmony_ci usb_amd_quirk_pll_enable(); 79062306a36Sopenharmony_ci } 79162306a36Sopenharmony_ci } 79262306a36Sopenharmony_ci xhci_urb_free_priv(urb_priv); 79362306a36Sopenharmony_ci usb_hcd_unlink_urb_from_ep(hcd, urb); 79462306a36Sopenharmony_ci trace_xhci_urb_giveback(urb); 79562306a36Sopenharmony_ci usb_hcd_giveback_urb(hcd, urb, status); 79662306a36Sopenharmony_ci} 79762306a36Sopenharmony_ci 79862306a36Sopenharmony_cistatic void xhci_unmap_td_bounce_buffer(struct xhci_hcd *xhci, 79962306a36Sopenharmony_ci struct xhci_ring *ring, struct xhci_td *td) 80062306a36Sopenharmony_ci{ 80162306a36Sopenharmony_ci struct device *dev = xhci_to_hcd(xhci)->self.sysdev; 80262306a36Sopenharmony_ci struct xhci_segment *seg = td->bounce_seg; 80362306a36Sopenharmony_ci struct urb *urb = td->urb; 80462306a36Sopenharmony_ci size_t len; 80562306a36Sopenharmony_ci 80662306a36Sopenharmony_ci if (!ring || !seg || !urb) 80762306a36Sopenharmony_ci return; 80862306a36Sopenharmony_ci 80962306a36Sopenharmony_ci if (usb_urb_dir_out(urb)) { 81062306a36Sopenharmony_ci dma_unmap_single(dev, seg->bounce_dma, ring->bounce_buf_len, 81162306a36Sopenharmony_ci DMA_TO_DEVICE); 81262306a36Sopenharmony_ci return; 81362306a36Sopenharmony_ci } 81462306a36Sopenharmony_ci 81562306a36Sopenharmony_ci dma_unmap_single(dev, seg->bounce_dma, ring->bounce_buf_len, 81662306a36Sopenharmony_ci DMA_FROM_DEVICE); 81762306a36Sopenharmony_ci /* for in tranfers we need to copy the data from bounce to sg */ 81862306a36Sopenharmony_ci if (urb->num_sgs) { 81962306a36Sopenharmony_ci len = sg_pcopy_from_buffer(urb->sg, urb->num_sgs, seg->bounce_buf, 82062306a36Sopenharmony_ci seg->bounce_len, seg->bounce_offs); 82162306a36Sopenharmony_ci if (len != seg->bounce_len) 82262306a36Sopenharmony_ci xhci_warn(xhci, "WARN Wrong bounce buffer read length: %zu != %d\n", 82362306a36Sopenharmony_ci len, seg->bounce_len); 82462306a36Sopenharmony_ci } else { 82562306a36Sopenharmony_ci memcpy(urb->transfer_buffer + seg->bounce_offs, seg->bounce_buf, 82662306a36Sopenharmony_ci seg->bounce_len); 82762306a36Sopenharmony_ci } 82862306a36Sopenharmony_ci seg->bounce_len = 0; 82962306a36Sopenharmony_ci seg->bounce_offs = 0; 83062306a36Sopenharmony_ci} 83162306a36Sopenharmony_ci 83262306a36Sopenharmony_cistatic int xhci_td_cleanup(struct xhci_hcd *xhci, struct xhci_td *td, 83362306a36Sopenharmony_ci struct xhci_ring *ep_ring, int status) 83462306a36Sopenharmony_ci{ 83562306a36Sopenharmony_ci struct urb *urb = NULL; 83662306a36Sopenharmony_ci 83762306a36Sopenharmony_ci /* Clean up the endpoint's TD list */ 83862306a36Sopenharmony_ci urb = td->urb; 83962306a36Sopenharmony_ci 84062306a36Sopenharmony_ci /* if a bounce buffer was used to align this td then unmap it */ 84162306a36Sopenharmony_ci xhci_unmap_td_bounce_buffer(xhci, ep_ring, td); 84262306a36Sopenharmony_ci 84362306a36Sopenharmony_ci /* Do one last check of the actual transfer length. 84462306a36Sopenharmony_ci * If the host controller said we transferred more data than the buffer 84562306a36Sopenharmony_ci * length, urb->actual_length will be a very big number (since it's 84662306a36Sopenharmony_ci * unsigned). Play it safe and say we didn't transfer anything. 84762306a36Sopenharmony_ci */ 84862306a36Sopenharmony_ci if (urb->actual_length > urb->transfer_buffer_length) { 84962306a36Sopenharmony_ci xhci_warn(xhci, "URB req %u and actual %u transfer length mismatch\n", 85062306a36Sopenharmony_ci urb->transfer_buffer_length, urb->actual_length); 85162306a36Sopenharmony_ci urb->actual_length = 0; 85262306a36Sopenharmony_ci status = 0; 85362306a36Sopenharmony_ci } 85462306a36Sopenharmony_ci /* TD might be removed from td_list if we are giving back a cancelled URB */ 85562306a36Sopenharmony_ci if (!list_empty(&td->td_list)) 85662306a36Sopenharmony_ci list_del_init(&td->td_list); 85762306a36Sopenharmony_ci /* Giving back a cancelled URB, or if a slated TD completed anyway */ 85862306a36Sopenharmony_ci if (!list_empty(&td->cancelled_td_list)) 85962306a36Sopenharmony_ci list_del_init(&td->cancelled_td_list); 86062306a36Sopenharmony_ci 86162306a36Sopenharmony_ci inc_td_cnt(urb); 86262306a36Sopenharmony_ci /* Giveback the urb when all the tds are completed */ 86362306a36Sopenharmony_ci if (last_td_in_urb(td)) { 86462306a36Sopenharmony_ci if ((urb->actual_length != urb->transfer_buffer_length && 86562306a36Sopenharmony_ci (urb->transfer_flags & URB_SHORT_NOT_OK)) || 86662306a36Sopenharmony_ci (status != 0 && !usb_endpoint_xfer_isoc(&urb->ep->desc))) 86762306a36Sopenharmony_ci xhci_dbg(xhci, "Giveback URB %p, len = %d, expected = %d, status = %d\n", 86862306a36Sopenharmony_ci urb, urb->actual_length, 86962306a36Sopenharmony_ci urb->transfer_buffer_length, status); 87062306a36Sopenharmony_ci 87162306a36Sopenharmony_ci /* set isoc urb status to 0 just as EHCI, UHCI, and OHCI */ 87262306a36Sopenharmony_ci if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) 87362306a36Sopenharmony_ci status = 0; 87462306a36Sopenharmony_ci xhci_giveback_urb_in_irq(xhci, td, status); 87562306a36Sopenharmony_ci } 87662306a36Sopenharmony_ci 87762306a36Sopenharmony_ci return 0; 87862306a36Sopenharmony_ci} 87962306a36Sopenharmony_ci 88062306a36Sopenharmony_ci 88162306a36Sopenharmony_ci/* Complete the cancelled URBs we unlinked from td_list. */ 88262306a36Sopenharmony_cistatic void xhci_giveback_invalidated_tds(struct xhci_virt_ep *ep) 88362306a36Sopenharmony_ci{ 88462306a36Sopenharmony_ci struct xhci_ring *ring; 88562306a36Sopenharmony_ci struct xhci_td *td, *tmp_td; 88662306a36Sopenharmony_ci 88762306a36Sopenharmony_ci list_for_each_entry_safe(td, tmp_td, &ep->cancelled_td_list, 88862306a36Sopenharmony_ci cancelled_td_list) { 88962306a36Sopenharmony_ci 89062306a36Sopenharmony_ci ring = xhci_urb_to_transfer_ring(ep->xhci, td->urb); 89162306a36Sopenharmony_ci 89262306a36Sopenharmony_ci if (td->cancel_status == TD_CLEARED) { 89362306a36Sopenharmony_ci xhci_dbg(ep->xhci, "%s: Giveback cancelled URB %p TD\n", 89462306a36Sopenharmony_ci __func__, td->urb); 89562306a36Sopenharmony_ci xhci_td_cleanup(ep->xhci, td, ring, td->status); 89662306a36Sopenharmony_ci } else { 89762306a36Sopenharmony_ci xhci_dbg(ep->xhci, "%s: Keep cancelled URB %p TD as cancel_status is %d\n", 89862306a36Sopenharmony_ci __func__, td->urb, td->cancel_status); 89962306a36Sopenharmony_ci } 90062306a36Sopenharmony_ci if (ep->xhci->xhc_state & XHCI_STATE_DYING) 90162306a36Sopenharmony_ci return; 90262306a36Sopenharmony_ci } 90362306a36Sopenharmony_ci} 90462306a36Sopenharmony_ci 90562306a36Sopenharmony_cistatic int xhci_reset_halted_ep(struct xhci_hcd *xhci, unsigned int slot_id, 90662306a36Sopenharmony_ci unsigned int ep_index, enum xhci_ep_reset_type reset_type) 90762306a36Sopenharmony_ci{ 90862306a36Sopenharmony_ci struct xhci_command *command; 90962306a36Sopenharmony_ci int ret = 0; 91062306a36Sopenharmony_ci 91162306a36Sopenharmony_ci command = xhci_alloc_command(xhci, false, GFP_ATOMIC); 91262306a36Sopenharmony_ci if (!command) { 91362306a36Sopenharmony_ci ret = -ENOMEM; 91462306a36Sopenharmony_ci goto done; 91562306a36Sopenharmony_ci } 91662306a36Sopenharmony_ci 91762306a36Sopenharmony_ci xhci_dbg(xhci, "%s-reset ep %u, slot %u\n", 91862306a36Sopenharmony_ci (reset_type == EP_HARD_RESET) ? "Hard" : "Soft", 91962306a36Sopenharmony_ci ep_index, slot_id); 92062306a36Sopenharmony_ci 92162306a36Sopenharmony_ci ret = xhci_queue_reset_ep(xhci, command, slot_id, ep_index, reset_type); 92262306a36Sopenharmony_cidone: 92362306a36Sopenharmony_ci if (ret) 92462306a36Sopenharmony_ci xhci_err(xhci, "ERROR queuing reset endpoint for slot %d ep_index %d, %d\n", 92562306a36Sopenharmony_ci slot_id, ep_index, ret); 92662306a36Sopenharmony_ci return ret; 92762306a36Sopenharmony_ci} 92862306a36Sopenharmony_ci 92962306a36Sopenharmony_cistatic int xhci_handle_halted_endpoint(struct xhci_hcd *xhci, 93062306a36Sopenharmony_ci struct xhci_virt_ep *ep, 93162306a36Sopenharmony_ci struct xhci_td *td, 93262306a36Sopenharmony_ci enum xhci_ep_reset_type reset_type) 93362306a36Sopenharmony_ci{ 93462306a36Sopenharmony_ci unsigned int slot_id = ep->vdev->slot_id; 93562306a36Sopenharmony_ci int err; 93662306a36Sopenharmony_ci 93762306a36Sopenharmony_ci /* 93862306a36Sopenharmony_ci * Avoid resetting endpoint if link is inactive. Can cause host hang. 93962306a36Sopenharmony_ci * Device will be reset soon to recover the link so don't do anything 94062306a36Sopenharmony_ci */ 94162306a36Sopenharmony_ci if (ep->vdev->flags & VDEV_PORT_ERROR) 94262306a36Sopenharmony_ci return -ENODEV; 94362306a36Sopenharmony_ci 94462306a36Sopenharmony_ci /* add td to cancelled list and let reset ep handler take care of it */ 94562306a36Sopenharmony_ci if (reset_type == EP_HARD_RESET) { 94662306a36Sopenharmony_ci ep->ep_state |= EP_HARD_CLEAR_TOGGLE; 94762306a36Sopenharmony_ci if (td && list_empty(&td->cancelled_td_list)) { 94862306a36Sopenharmony_ci list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list); 94962306a36Sopenharmony_ci td->cancel_status = TD_HALTED; 95062306a36Sopenharmony_ci } 95162306a36Sopenharmony_ci } 95262306a36Sopenharmony_ci 95362306a36Sopenharmony_ci if (ep->ep_state & EP_HALTED) { 95462306a36Sopenharmony_ci xhci_dbg(xhci, "Reset ep command for ep_index %d already pending\n", 95562306a36Sopenharmony_ci ep->ep_index); 95662306a36Sopenharmony_ci return 0; 95762306a36Sopenharmony_ci } 95862306a36Sopenharmony_ci 95962306a36Sopenharmony_ci err = xhci_reset_halted_ep(xhci, slot_id, ep->ep_index, reset_type); 96062306a36Sopenharmony_ci if (err) 96162306a36Sopenharmony_ci return err; 96262306a36Sopenharmony_ci 96362306a36Sopenharmony_ci ep->ep_state |= EP_HALTED; 96462306a36Sopenharmony_ci 96562306a36Sopenharmony_ci xhci_ring_cmd_db(xhci); 96662306a36Sopenharmony_ci 96762306a36Sopenharmony_ci return 0; 96862306a36Sopenharmony_ci} 96962306a36Sopenharmony_ci 97062306a36Sopenharmony_ci/* 97162306a36Sopenharmony_ci * Fix up the ep ring first, so HW stops executing cancelled TDs. 97262306a36Sopenharmony_ci * We have the xHCI lock, so nothing can modify this list until we drop it. 97362306a36Sopenharmony_ci * We're also in the event handler, so we can't get re-interrupted if another 97462306a36Sopenharmony_ci * Stop Endpoint command completes. 97562306a36Sopenharmony_ci * 97662306a36Sopenharmony_ci * only call this when ring is not in a running state 97762306a36Sopenharmony_ci */ 97862306a36Sopenharmony_ci 97962306a36Sopenharmony_cistatic int xhci_invalidate_cancelled_tds(struct xhci_virt_ep *ep) 98062306a36Sopenharmony_ci{ 98162306a36Sopenharmony_ci struct xhci_hcd *xhci; 98262306a36Sopenharmony_ci struct xhci_td *td = NULL; 98362306a36Sopenharmony_ci struct xhci_td *tmp_td = NULL; 98462306a36Sopenharmony_ci struct xhci_td *cached_td = NULL; 98562306a36Sopenharmony_ci struct xhci_ring *ring; 98662306a36Sopenharmony_ci u64 hw_deq; 98762306a36Sopenharmony_ci unsigned int slot_id = ep->vdev->slot_id; 98862306a36Sopenharmony_ci int err; 98962306a36Sopenharmony_ci 99062306a36Sopenharmony_ci xhci = ep->xhci; 99162306a36Sopenharmony_ci 99262306a36Sopenharmony_ci list_for_each_entry_safe(td, tmp_td, &ep->cancelled_td_list, cancelled_td_list) { 99362306a36Sopenharmony_ci xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, 99462306a36Sopenharmony_ci "Removing canceled TD starting at 0x%llx (dma) in stream %u URB %p", 99562306a36Sopenharmony_ci (unsigned long long)xhci_trb_virt_to_dma( 99662306a36Sopenharmony_ci td->start_seg, td->first_trb), 99762306a36Sopenharmony_ci td->urb->stream_id, td->urb); 99862306a36Sopenharmony_ci list_del_init(&td->td_list); 99962306a36Sopenharmony_ci ring = xhci_urb_to_transfer_ring(xhci, td->urb); 100062306a36Sopenharmony_ci if (!ring) { 100162306a36Sopenharmony_ci xhci_warn(xhci, "WARN Cancelled URB %p has invalid stream ID %u.\n", 100262306a36Sopenharmony_ci td->urb, td->urb->stream_id); 100362306a36Sopenharmony_ci continue; 100462306a36Sopenharmony_ci } 100562306a36Sopenharmony_ci /* 100662306a36Sopenharmony_ci * If a ring stopped on the TD we need to cancel then we have to 100762306a36Sopenharmony_ci * move the xHC endpoint ring dequeue pointer past this TD. 100862306a36Sopenharmony_ci * Rings halted due to STALL may show hw_deq is past the stalled 100962306a36Sopenharmony_ci * TD, but still require a set TR Deq command to flush xHC cache. 101062306a36Sopenharmony_ci */ 101162306a36Sopenharmony_ci hw_deq = xhci_get_hw_deq(xhci, ep->vdev, ep->ep_index, 101262306a36Sopenharmony_ci td->urb->stream_id); 101362306a36Sopenharmony_ci hw_deq &= ~0xf; 101462306a36Sopenharmony_ci 101562306a36Sopenharmony_ci if (td->cancel_status == TD_HALTED || 101662306a36Sopenharmony_ci trb_in_td(xhci, td->start_seg, td->first_trb, td->last_trb, hw_deq, false)) { 101762306a36Sopenharmony_ci switch (td->cancel_status) { 101862306a36Sopenharmony_ci case TD_CLEARED: /* TD is already no-op */ 101962306a36Sopenharmony_ci case TD_CLEARING_CACHE: /* set TR deq command already queued */ 102062306a36Sopenharmony_ci break; 102162306a36Sopenharmony_ci case TD_DIRTY: /* TD is cached, clear it */ 102262306a36Sopenharmony_ci case TD_HALTED: 102362306a36Sopenharmony_ci td->cancel_status = TD_CLEARING_CACHE; 102462306a36Sopenharmony_ci if (cached_td) 102562306a36Sopenharmony_ci /* FIXME stream case, several stopped rings */ 102662306a36Sopenharmony_ci xhci_dbg(xhci, 102762306a36Sopenharmony_ci "Move dq past stream %u URB %p instead of stream %u URB %p\n", 102862306a36Sopenharmony_ci td->urb->stream_id, td->urb, 102962306a36Sopenharmony_ci cached_td->urb->stream_id, cached_td->urb); 103062306a36Sopenharmony_ci cached_td = td; 103162306a36Sopenharmony_ci break; 103262306a36Sopenharmony_ci } 103362306a36Sopenharmony_ci } else { 103462306a36Sopenharmony_ci td_to_noop(xhci, ring, td, false); 103562306a36Sopenharmony_ci td->cancel_status = TD_CLEARED; 103662306a36Sopenharmony_ci } 103762306a36Sopenharmony_ci } 103862306a36Sopenharmony_ci 103962306a36Sopenharmony_ci /* If there's no need to move the dequeue pointer then we're done */ 104062306a36Sopenharmony_ci if (!cached_td) 104162306a36Sopenharmony_ci return 0; 104262306a36Sopenharmony_ci 104362306a36Sopenharmony_ci err = xhci_move_dequeue_past_td(xhci, slot_id, ep->ep_index, 104462306a36Sopenharmony_ci cached_td->urb->stream_id, 104562306a36Sopenharmony_ci cached_td); 104662306a36Sopenharmony_ci if (err) { 104762306a36Sopenharmony_ci /* Failed to move past cached td, just set cached TDs to no-op */ 104862306a36Sopenharmony_ci list_for_each_entry_safe(td, tmp_td, &ep->cancelled_td_list, cancelled_td_list) { 104962306a36Sopenharmony_ci if (td->cancel_status != TD_CLEARING_CACHE) 105062306a36Sopenharmony_ci continue; 105162306a36Sopenharmony_ci xhci_dbg(xhci, "Failed to clear cancelled cached URB %p, mark clear anyway\n", 105262306a36Sopenharmony_ci td->urb); 105362306a36Sopenharmony_ci td_to_noop(xhci, ring, td, false); 105462306a36Sopenharmony_ci td->cancel_status = TD_CLEARED; 105562306a36Sopenharmony_ci } 105662306a36Sopenharmony_ci } 105762306a36Sopenharmony_ci return 0; 105862306a36Sopenharmony_ci} 105962306a36Sopenharmony_ci 106062306a36Sopenharmony_ci/* 106162306a36Sopenharmony_ci * Returns the TD the endpoint ring halted on. 106262306a36Sopenharmony_ci * Only call for non-running rings without streams. 106362306a36Sopenharmony_ci */ 106462306a36Sopenharmony_cistatic struct xhci_td *find_halted_td(struct xhci_virt_ep *ep) 106562306a36Sopenharmony_ci{ 106662306a36Sopenharmony_ci struct xhci_td *td; 106762306a36Sopenharmony_ci u64 hw_deq; 106862306a36Sopenharmony_ci 106962306a36Sopenharmony_ci if (!list_empty(&ep->ring->td_list)) { /* Not streams compatible */ 107062306a36Sopenharmony_ci hw_deq = xhci_get_hw_deq(ep->xhci, ep->vdev, ep->ep_index, 0); 107162306a36Sopenharmony_ci hw_deq &= ~0xf; 107262306a36Sopenharmony_ci td = list_first_entry(&ep->ring->td_list, struct xhci_td, td_list); 107362306a36Sopenharmony_ci if (trb_in_td(ep->xhci, td->start_seg, td->first_trb, 107462306a36Sopenharmony_ci td->last_trb, hw_deq, false)) 107562306a36Sopenharmony_ci return td; 107662306a36Sopenharmony_ci } 107762306a36Sopenharmony_ci return NULL; 107862306a36Sopenharmony_ci} 107962306a36Sopenharmony_ci 108062306a36Sopenharmony_ci/* 108162306a36Sopenharmony_ci * When we get a command completion for a Stop Endpoint Command, we need to 108262306a36Sopenharmony_ci * unlink any cancelled TDs from the ring. There are two ways to do that: 108362306a36Sopenharmony_ci * 108462306a36Sopenharmony_ci * 1. If the HW was in the middle of processing the TD that needs to be 108562306a36Sopenharmony_ci * cancelled, then we must move the ring's dequeue pointer past the last TRB 108662306a36Sopenharmony_ci * in the TD with a Set Dequeue Pointer Command. 108762306a36Sopenharmony_ci * 2. Otherwise, we turn all the TRBs in the TD into No-op TRBs (with the chain 108862306a36Sopenharmony_ci * bit cleared) so that the HW will skip over them. 108962306a36Sopenharmony_ci */ 109062306a36Sopenharmony_cistatic void xhci_handle_cmd_stop_ep(struct xhci_hcd *xhci, int slot_id, 109162306a36Sopenharmony_ci union xhci_trb *trb, u32 comp_code) 109262306a36Sopenharmony_ci{ 109362306a36Sopenharmony_ci unsigned int ep_index; 109462306a36Sopenharmony_ci struct xhci_virt_ep *ep; 109562306a36Sopenharmony_ci struct xhci_ep_ctx *ep_ctx; 109662306a36Sopenharmony_ci struct xhci_td *td = NULL; 109762306a36Sopenharmony_ci enum xhci_ep_reset_type reset_type; 109862306a36Sopenharmony_ci struct xhci_command *command; 109962306a36Sopenharmony_ci int err; 110062306a36Sopenharmony_ci 110162306a36Sopenharmony_ci if (unlikely(TRB_TO_SUSPEND_PORT(le32_to_cpu(trb->generic.field[3])))) { 110262306a36Sopenharmony_ci if (!xhci->devs[slot_id]) 110362306a36Sopenharmony_ci xhci_warn(xhci, "Stop endpoint command completion for disabled slot %u\n", 110462306a36Sopenharmony_ci slot_id); 110562306a36Sopenharmony_ci return; 110662306a36Sopenharmony_ci } 110762306a36Sopenharmony_ci 110862306a36Sopenharmony_ci ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3])); 110962306a36Sopenharmony_ci ep = xhci_get_virt_ep(xhci, slot_id, ep_index); 111062306a36Sopenharmony_ci if (!ep) 111162306a36Sopenharmony_ci return; 111262306a36Sopenharmony_ci 111362306a36Sopenharmony_ci ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep_index); 111462306a36Sopenharmony_ci 111562306a36Sopenharmony_ci trace_xhci_handle_cmd_stop_ep(ep_ctx); 111662306a36Sopenharmony_ci 111762306a36Sopenharmony_ci if (comp_code == COMP_CONTEXT_STATE_ERROR) { 111862306a36Sopenharmony_ci /* 111962306a36Sopenharmony_ci * If stop endpoint command raced with a halting endpoint we need to 112062306a36Sopenharmony_ci * reset the host side endpoint first. 112162306a36Sopenharmony_ci * If the TD we halted on isn't cancelled the TD should be given back 112262306a36Sopenharmony_ci * with a proper error code, and the ring dequeue moved past the TD. 112362306a36Sopenharmony_ci * If streams case we can't find hw_deq, or the TD we halted on so do a 112462306a36Sopenharmony_ci * soft reset. 112562306a36Sopenharmony_ci * 112662306a36Sopenharmony_ci * Proper error code is unknown here, it would be -EPIPE if device side 112762306a36Sopenharmony_ci * of enadpoit halted (aka STALL), and -EPROTO if not (transaction error) 112862306a36Sopenharmony_ci * We use -EPROTO, if device is stalled it should return a stall error on 112962306a36Sopenharmony_ci * next transfer, which then will return -EPIPE, and device side stall is 113062306a36Sopenharmony_ci * noted and cleared by class driver. 113162306a36Sopenharmony_ci */ 113262306a36Sopenharmony_ci switch (GET_EP_CTX_STATE(ep_ctx)) { 113362306a36Sopenharmony_ci case EP_STATE_HALTED: 113462306a36Sopenharmony_ci xhci_dbg(xhci, "Stop ep completion raced with stall, reset ep\n"); 113562306a36Sopenharmony_ci if (ep->ep_state & EP_HAS_STREAMS) { 113662306a36Sopenharmony_ci reset_type = EP_SOFT_RESET; 113762306a36Sopenharmony_ci } else { 113862306a36Sopenharmony_ci reset_type = EP_HARD_RESET; 113962306a36Sopenharmony_ci td = find_halted_td(ep); 114062306a36Sopenharmony_ci if (td) 114162306a36Sopenharmony_ci td->status = -EPROTO; 114262306a36Sopenharmony_ci } 114362306a36Sopenharmony_ci /* reset ep, reset handler cleans up cancelled tds */ 114462306a36Sopenharmony_ci err = xhci_handle_halted_endpoint(xhci, ep, td, reset_type); 114562306a36Sopenharmony_ci if (err) 114662306a36Sopenharmony_ci break; 114762306a36Sopenharmony_ci ep->ep_state &= ~EP_STOP_CMD_PENDING; 114862306a36Sopenharmony_ci return; 114962306a36Sopenharmony_ci case EP_STATE_RUNNING: 115062306a36Sopenharmony_ci /* Race, HW handled stop ep cmd before ep was running */ 115162306a36Sopenharmony_ci xhci_dbg(xhci, "Stop ep completion ctx error, ep is running\n"); 115262306a36Sopenharmony_ci 115362306a36Sopenharmony_ci command = xhci_alloc_command(xhci, false, GFP_ATOMIC); 115462306a36Sopenharmony_ci if (!command) { 115562306a36Sopenharmony_ci ep->ep_state &= ~EP_STOP_CMD_PENDING; 115662306a36Sopenharmony_ci return; 115762306a36Sopenharmony_ci } 115862306a36Sopenharmony_ci xhci_queue_stop_endpoint(xhci, command, slot_id, ep_index, 0); 115962306a36Sopenharmony_ci xhci_ring_cmd_db(xhci); 116062306a36Sopenharmony_ci 116162306a36Sopenharmony_ci return; 116262306a36Sopenharmony_ci default: 116362306a36Sopenharmony_ci break; 116462306a36Sopenharmony_ci } 116562306a36Sopenharmony_ci } 116662306a36Sopenharmony_ci 116762306a36Sopenharmony_ci /* will queue a set TR deq if stopped on a cancelled, uncleared TD */ 116862306a36Sopenharmony_ci xhci_invalidate_cancelled_tds(ep); 116962306a36Sopenharmony_ci ep->ep_state &= ~EP_STOP_CMD_PENDING; 117062306a36Sopenharmony_ci 117162306a36Sopenharmony_ci /* Otherwise ring the doorbell(s) to restart queued transfers */ 117262306a36Sopenharmony_ci xhci_giveback_invalidated_tds(ep); 117362306a36Sopenharmony_ci ring_doorbell_for_active_rings(xhci, slot_id, ep_index); 117462306a36Sopenharmony_ci} 117562306a36Sopenharmony_ci 117662306a36Sopenharmony_cistatic void xhci_kill_ring_urbs(struct xhci_hcd *xhci, struct xhci_ring *ring) 117762306a36Sopenharmony_ci{ 117862306a36Sopenharmony_ci struct xhci_td *cur_td; 117962306a36Sopenharmony_ci struct xhci_td *tmp; 118062306a36Sopenharmony_ci 118162306a36Sopenharmony_ci list_for_each_entry_safe(cur_td, tmp, &ring->td_list, td_list) { 118262306a36Sopenharmony_ci list_del_init(&cur_td->td_list); 118362306a36Sopenharmony_ci 118462306a36Sopenharmony_ci if (!list_empty(&cur_td->cancelled_td_list)) 118562306a36Sopenharmony_ci list_del_init(&cur_td->cancelled_td_list); 118662306a36Sopenharmony_ci 118762306a36Sopenharmony_ci xhci_unmap_td_bounce_buffer(xhci, ring, cur_td); 118862306a36Sopenharmony_ci 118962306a36Sopenharmony_ci inc_td_cnt(cur_td->urb); 119062306a36Sopenharmony_ci if (last_td_in_urb(cur_td)) 119162306a36Sopenharmony_ci xhci_giveback_urb_in_irq(xhci, cur_td, -ESHUTDOWN); 119262306a36Sopenharmony_ci } 119362306a36Sopenharmony_ci} 119462306a36Sopenharmony_ci 119562306a36Sopenharmony_cistatic void xhci_kill_endpoint_urbs(struct xhci_hcd *xhci, 119662306a36Sopenharmony_ci int slot_id, int ep_index) 119762306a36Sopenharmony_ci{ 119862306a36Sopenharmony_ci struct xhci_td *cur_td; 119962306a36Sopenharmony_ci struct xhci_td *tmp; 120062306a36Sopenharmony_ci struct xhci_virt_ep *ep; 120162306a36Sopenharmony_ci struct xhci_ring *ring; 120262306a36Sopenharmony_ci 120362306a36Sopenharmony_ci ep = xhci_get_virt_ep(xhci, slot_id, ep_index); 120462306a36Sopenharmony_ci if (!ep) 120562306a36Sopenharmony_ci return; 120662306a36Sopenharmony_ci 120762306a36Sopenharmony_ci if ((ep->ep_state & EP_HAS_STREAMS) || 120862306a36Sopenharmony_ci (ep->ep_state & EP_GETTING_NO_STREAMS)) { 120962306a36Sopenharmony_ci int stream_id; 121062306a36Sopenharmony_ci 121162306a36Sopenharmony_ci for (stream_id = 1; stream_id < ep->stream_info->num_streams; 121262306a36Sopenharmony_ci stream_id++) { 121362306a36Sopenharmony_ci ring = ep->stream_info->stream_rings[stream_id]; 121462306a36Sopenharmony_ci if (!ring) 121562306a36Sopenharmony_ci continue; 121662306a36Sopenharmony_ci 121762306a36Sopenharmony_ci xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, 121862306a36Sopenharmony_ci "Killing URBs for slot ID %u, ep index %u, stream %u", 121962306a36Sopenharmony_ci slot_id, ep_index, stream_id); 122062306a36Sopenharmony_ci xhci_kill_ring_urbs(xhci, ring); 122162306a36Sopenharmony_ci } 122262306a36Sopenharmony_ci } else { 122362306a36Sopenharmony_ci ring = ep->ring; 122462306a36Sopenharmony_ci if (!ring) 122562306a36Sopenharmony_ci return; 122662306a36Sopenharmony_ci xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, 122762306a36Sopenharmony_ci "Killing URBs for slot ID %u, ep index %u", 122862306a36Sopenharmony_ci slot_id, ep_index); 122962306a36Sopenharmony_ci xhci_kill_ring_urbs(xhci, ring); 123062306a36Sopenharmony_ci } 123162306a36Sopenharmony_ci 123262306a36Sopenharmony_ci list_for_each_entry_safe(cur_td, tmp, &ep->cancelled_td_list, 123362306a36Sopenharmony_ci cancelled_td_list) { 123462306a36Sopenharmony_ci list_del_init(&cur_td->cancelled_td_list); 123562306a36Sopenharmony_ci inc_td_cnt(cur_td->urb); 123662306a36Sopenharmony_ci 123762306a36Sopenharmony_ci if (last_td_in_urb(cur_td)) 123862306a36Sopenharmony_ci xhci_giveback_urb_in_irq(xhci, cur_td, -ESHUTDOWN); 123962306a36Sopenharmony_ci } 124062306a36Sopenharmony_ci} 124162306a36Sopenharmony_ci 124262306a36Sopenharmony_ci/* 124362306a36Sopenharmony_ci * host controller died, register read returns 0xffffffff 124462306a36Sopenharmony_ci * Complete pending commands, mark them ABORTED. 124562306a36Sopenharmony_ci * URBs need to be given back as usb core might be waiting with device locks 124662306a36Sopenharmony_ci * held for the URBs to finish during device disconnect, blocking host remove. 124762306a36Sopenharmony_ci * 124862306a36Sopenharmony_ci * Call with xhci->lock held. 124962306a36Sopenharmony_ci * lock is relased and re-acquired while giving back urb. 125062306a36Sopenharmony_ci */ 125162306a36Sopenharmony_civoid xhci_hc_died(struct xhci_hcd *xhci) 125262306a36Sopenharmony_ci{ 125362306a36Sopenharmony_ci int i, j; 125462306a36Sopenharmony_ci 125562306a36Sopenharmony_ci if (xhci->xhc_state & XHCI_STATE_DYING) 125662306a36Sopenharmony_ci return; 125762306a36Sopenharmony_ci 125862306a36Sopenharmony_ci xhci_err(xhci, "xHCI host controller not responding, assume dead\n"); 125962306a36Sopenharmony_ci xhci->xhc_state |= XHCI_STATE_DYING; 126062306a36Sopenharmony_ci 126162306a36Sopenharmony_ci xhci_cleanup_command_queue(xhci); 126262306a36Sopenharmony_ci 126362306a36Sopenharmony_ci /* return any pending urbs, remove may be waiting for them */ 126462306a36Sopenharmony_ci for (i = 0; i <= HCS_MAX_SLOTS(xhci->hcs_params1); i++) { 126562306a36Sopenharmony_ci if (!xhci->devs[i]) 126662306a36Sopenharmony_ci continue; 126762306a36Sopenharmony_ci for (j = 0; j < 31; j++) 126862306a36Sopenharmony_ci xhci_kill_endpoint_urbs(xhci, i, j); 126962306a36Sopenharmony_ci } 127062306a36Sopenharmony_ci 127162306a36Sopenharmony_ci /* inform usb core hc died if PCI remove isn't already handling it */ 127262306a36Sopenharmony_ci if (!(xhci->xhc_state & XHCI_STATE_REMOVING)) 127362306a36Sopenharmony_ci usb_hc_died(xhci_to_hcd(xhci)); 127462306a36Sopenharmony_ci} 127562306a36Sopenharmony_ci 127662306a36Sopenharmony_cistatic void update_ring_for_set_deq_completion(struct xhci_hcd *xhci, 127762306a36Sopenharmony_ci struct xhci_virt_device *dev, 127862306a36Sopenharmony_ci struct xhci_ring *ep_ring, 127962306a36Sopenharmony_ci unsigned int ep_index) 128062306a36Sopenharmony_ci{ 128162306a36Sopenharmony_ci union xhci_trb *dequeue_temp; 128262306a36Sopenharmony_ci 128362306a36Sopenharmony_ci dequeue_temp = ep_ring->dequeue; 128462306a36Sopenharmony_ci 128562306a36Sopenharmony_ci /* If we get two back-to-back stalls, and the first stalled transfer 128662306a36Sopenharmony_ci * ends just before a link TRB, the dequeue pointer will be left on 128762306a36Sopenharmony_ci * the link TRB by the code in the while loop. So we have to update 128862306a36Sopenharmony_ci * the dequeue pointer one segment further, or we'll jump off 128962306a36Sopenharmony_ci * the segment into la-la-land. 129062306a36Sopenharmony_ci */ 129162306a36Sopenharmony_ci if (trb_is_link(ep_ring->dequeue)) { 129262306a36Sopenharmony_ci ep_ring->deq_seg = ep_ring->deq_seg->next; 129362306a36Sopenharmony_ci ep_ring->dequeue = ep_ring->deq_seg->trbs; 129462306a36Sopenharmony_ci } 129562306a36Sopenharmony_ci 129662306a36Sopenharmony_ci while (ep_ring->dequeue != dev->eps[ep_index].queued_deq_ptr) { 129762306a36Sopenharmony_ci /* We have more usable TRBs */ 129862306a36Sopenharmony_ci ep_ring->dequeue++; 129962306a36Sopenharmony_ci if (trb_is_link(ep_ring->dequeue)) { 130062306a36Sopenharmony_ci if (ep_ring->dequeue == 130162306a36Sopenharmony_ci dev->eps[ep_index].queued_deq_ptr) 130262306a36Sopenharmony_ci break; 130362306a36Sopenharmony_ci ep_ring->deq_seg = ep_ring->deq_seg->next; 130462306a36Sopenharmony_ci ep_ring->dequeue = ep_ring->deq_seg->trbs; 130562306a36Sopenharmony_ci } 130662306a36Sopenharmony_ci if (ep_ring->dequeue == dequeue_temp) { 130762306a36Sopenharmony_ci xhci_dbg(xhci, "Unable to find new dequeue pointer\n"); 130862306a36Sopenharmony_ci break; 130962306a36Sopenharmony_ci } 131062306a36Sopenharmony_ci } 131162306a36Sopenharmony_ci} 131262306a36Sopenharmony_ci 131362306a36Sopenharmony_ci/* 131462306a36Sopenharmony_ci * When we get a completion for a Set Transfer Ring Dequeue Pointer command, 131562306a36Sopenharmony_ci * we need to clear the set deq pending flag in the endpoint ring state, so that 131662306a36Sopenharmony_ci * the TD queueing code can ring the doorbell again. We also need to ring the 131762306a36Sopenharmony_ci * endpoint doorbell to restart the ring, but only if there aren't more 131862306a36Sopenharmony_ci * cancellations pending. 131962306a36Sopenharmony_ci */ 132062306a36Sopenharmony_cistatic void xhci_handle_cmd_set_deq(struct xhci_hcd *xhci, int slot_id, 132162306a36Sopenharmony_ci union xhci_trb *trb, u32 cmd_comp_code) 132262306a36Sopenharmony_ci{ 132362306a36Sopenharmony_ci unsigned int ep_index; 132462306a36Sopenharmony_ci unsigned int stream_id; 132562306a36Sopenharmony_ci struct xhci_ring *ep_ring; 132662306a36Sopenharmony_ci struct xhci_virt_ep *ep; 132762306a36Sopenharmony_ci struct xhci_ep_ctx *ep_ctx; 132862306a36Sopenharmony_ci struct xhci_slot_ctx *slot_ctx; 132962306a36Sopenharmony_ci struct xhci_td *td, *tmp_td; 133062306a36Sopenharmony_ci 133162306a36Sopenharmony_ci ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3])); 133262306a36Sopenharmony_ci stream_id = TRB_TO_STREAM_ID(le32_to_cpu(trb->generic.field[2])); 133362306a36Sopenharmony_ci ep = xhci_get_virt_ep(xhci, slot_id, ep_index); 133462306a36Sopenharmony_ci if (!ep) 133562306a36Sopenharmony_ci return; 133662306a36Sopenharmony_ci 133762306a36Sopenharmony_ci ep_ring = xhci_virt_ep_to_ring(xhci, ep, stream_id); 133862306a36Sopenharmony_ci if (!ep_ring) { 133962306a36Sopenharmony_ci xhci_warn(xhci, "WARN Set TR deq ptr command for freed stream ID %u\n", 134062306a36Sopenharmony_ci stream_id); 134162306a36Sopenharmony_ci /* XXX: Harmless??? */ 134262306a36Sopenharmony_ci goto cleanup; 134362306a36Sopenharmony_ci } 134462306a36Sopenharmony_ci 134562306a36Sopenharmony_ci ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep_index); 134662306a36Sopenharmony_ci slot_ctx = xhci_get_slot_ctx(xhci, ep->vdev->out_ctx); 134762306a36Sopenharmony_ci trace_xhci_handle_cmd_set_deq(slot_ctx); 134862306a36Sopenharmony_ci trace_xhci_handle_cmd_set_deq_ep(ep_ctx); 134962306a36Sopenharmony_ci 135062306a36Sopenharmony_ci if (cmd_comp_code != COMP_SUCCESS) { 135162306a36Sopenharmony_ci unsigned int ep_state; 135262306a36Sopenharmony_ci unsigned int slot_state; 135362306a36Sopenharmony_ci 135462306a36Sopenharmony_ci switch (cmd_comp_code) { 135562306a36Sopenharmony_ci case COMP_TRB_ERROR: 135662306a36Sopenharmony_ci xhci_warn(xhci, "WARN Set TR Deq Ptr cmd invalid because of stream ID configuration\n"); 135762306a36Sopenharmony_ci break; 135862306a36Sopenharmony_ci case COMP_CONTEXT_STATE_ERROR: 135962306a36Sopenharmony_ci xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed due to incorrect slot or ep state.\n"); 136062306a36Sopenharmony_ci ep_state = GET_EP_CTX_STATE(ep_ctx); 136162306a36Sopenharmony_ci slot_state = le32_to_cpu(slot_ctx->dev_state); 136262306a36Sopenharmony_ci slot_state = GET_SLOT_STATE(slot_state); 136362306a36Sopenharmony_ci xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, 136462306a36Sopenharmony_ci "Slot state = %u, EP state = %u", 136562306a36Sopenharmony_ci slot_state, ep_state); 136662306a36Sopenharmony_ci break; 136762306a36Sopenharmony_ci case COMP_SLOT_NOT_ENABLED_ERROR: 136862306a36Sopenharmony_ci xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed because slot %u was not enabled.\n", 136962306a36Sopenharmony_ci slot_id); 137062306a36Sopenharmony_ci break; 137162306a36Sopenharmony_ci default: 137262306a36Sopenharmony_ci xhci_warn(xhci, "WARN Set TR Deq Ptr cmd with unknown completion code of %u.\n", 137362306a36Sopenharmony_ci cmd_comp_code); 137462306a36Sopenharmony_ci break; 137562306a36Sopenharmony_ci } 137662306a36Sopenharmony_ci /* OK what do we do now? The endpoint state is hosed, and we 137762306a36Sopenharmony_ci * should never get to this point if the synchronization between 137862306a36Sopenharmony_ci * queueing, and endpoint state are correct. This might happen 137962306a36Sopenharmony_ci * if the device gets disconnected after we've finished 138062306a36Sopenharmony_ci * cancelling URBs, which might not be an error... 138162306a36Sopenharmony_ci */ 138262306a36Sopenharmony_ci } else { 138362306a36Sopenharmony_ci u64 deq; 138462306a36Sopenharmony_ci /* 4.6.10 deq ptr is written to the stream ctx for streams */ 138562306a36Sopenharmony_ci if (ep->ep_state & EP_HAS_STREAMS) { 138662306a36Sopenharmony_ci struct xhci_stream_ctx *ctx = 138762306a36Sopenharmony_ci &ep->stream_info->stream_ctx_array[stream_id]; 138862306a36Sopenharmony_ci deq = le64_to_cpu(ctx->stream_ring) & SCTX_DEQ_MASK; 138962306a36Sopenharmony_ci } else { 139062306a36Sopenharmony_ci deq = le64_to_cpu(ep_ctx->deq) & ~EP_CTX_CYCLE_MASK; 139162306a36Sopenharmony_ci } 139262306a36Sopenharmony_ci xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, 139362306a36Sopenharmony_ci "Successful Set TR Deq Ptr cmd, deq = @%08llx", deq); 139462306a36Sopenharmony_ci if (xhci_trb_virt_to_dma(ep->queued_deq_seg, 139562306a36Sopenharmony_ci ep->queued_deq_ptr) == deq) { 139662306a36Sopenharmony_ci /* Update the ring's dequeue segment and dequeue pointer 139762306a36Sopenharmony_ci * to reflect the new position. 139862306a36Sopenharmony_ci */ 139962306a36Sopenharmony_ci update_ring_for_set_deq_completion(xhci, ep->vdev, 140062306a36Sopenharmony_ci ep_ring, ep_index); 140162306a36Sopenharmony_ci } else { 140262306a36Sopenharmony_ci xhci_warn(xhci, "Mismatch between completed Set TR Deq Ptr command & xHCI internal state.\n"); 140362306a36Sopenharmony_ci xhci_warn(xhci, "ep deq seg = %p, deq ptr = %p\n", 140462306a36Sopenharmony_ci ep->queued_deq_seg, ep->queued_deq_ptr); 140562306a36Sopenharmony_ci } 140662306a36Sopenharmony_ci } 140762306a36Sopenharmony_ci /* HW cached TDs cleared from cache, give them back */ 140862306a36Sopenharmony_ci list_for_each_entry_safe(td, tmp_td, &ep->cancelled_td_list, 140962306a36Sopenharmony_ci cancelled_td_list) { 141062306a36Sopenharmony_ci ep_ring = xhci_urb_to_transfer_ring(ep->xhci, td->urb); 141162306a36Sopenharmony_ci if (td->cancel_status == TD_CLEARING_CACHE) { 141262306a36Sopenharmony_ci td->cancel_status = TD_CLEARED; 141362306a36Sopenharmony_ci xhci_dbg(ep->xhci, "%s: Giveback cancelled URB %p TD\n", 141462306a36Sopenharmony_ci __func__, td->urb); 141562306a36Sopenharmony_ci xhci_td_cleanup(ep->xhci, td, ep_ring, td->status); 141662306a36Sopenharmony_ci } else { 141762306a36Sopenharmony_ci xhci_dbg(ep->xhci, "%s: Keep cancelled URB %p TD as cancel_status is %d\n", 141862306a36Sopenharmony_ci __func__, td->urb, td->cancel_status); 141962306a36Sopenharmony_ci } 142062306a36Sopenharmony_ci } 142162306a36Sopenharmony_cicleanup: 142262306a36Sopenharmony_ci ep->ep_state &= ~SET_DEQ_PENDING; 142362306a36Sopenharmony_ci ep->queued_deq_seg = NULL; 142462306a36Sopenharmony_ci ep->queued_deq_ptr = NULL; 142562306a36Sopenharmony_ci /* Restart any rings with pending URBs */ 142662306a36Sopenharmony_ci ring_doorbell_for_active_rings(xhci, slot_id, ep_index); 142762306a36Sopenharmony_ci} 142862306a36Sopenharmony_ci 142962306a36Sopenharmony_cistatic void xhci_handle_cmd_reset_ep(struct xhci_hcd *xhci, int slot_id, 143062306a36Sopenharmony_ci union xhci_trb *trb, u32 cmd_comp_code) 143162306a36Sopenharmony_ci{ 143262306a36Sopenharmony_ci struct xhci_virt_ep *ep; 143362306a36Sopenharmony_ci struct xhci_ep_ctx *ep_ctx; 143462306a36Sopenharmony_ci unsigned int ep_index; 143562306a36Sopenharmony_ci 143662306a36Sopenharmony_ci ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3])); 143762306a36Sopenharmony_ci ep = xhci_get_virt_ep(xhci, slot_id, ep_index); 143862306a36Sopenharmony_ci if (!ep) 143962306a36Sopenharmony_ci return; 144062306a36Sopenharmony_ci 144162306a36Sopenharmony_ci ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep_index); 144262306a36Sopenharmony_ci trace_xhci_handle_cmd_reset_ep(ep_ctx); 144362306a36Sopenharmony_ci 144462306a36Sopenharmony_ci /* This command will only fail if the endpoint wasn't halted, 144562306a36Sopenharmony_ci * but we don't care. 144662306a36Sopenharmony_ci */ 144762306a36Sopenharmony_ci xhci_dbg_trace(xhci, trace_xhci_dbg_reset_ep, 144862306a36Sopenharmony_ci "Ignoring reset ep completion code of %u", cmd_comp_code); 144962306a36Sopenharmony_ci 145062306a36Sopenharmony_ci /* Cleanup cancelled TDs as ep is stopped. May queue a Set TR Deq cmd */ 145162306a36Sopenharmony_ci xhci_invalidate_cancelled_tds(ep); 145262306a36Sopenharmony_ci 145362306a36Sopenharmony_ci /* Clear our internal halted state */ 145462306a36Sopenharmony_ci ep->ep_state &= ~EP_HALTED; 145562306a36Sopenharmony_ci 145662306a36Sopenharmony_ci xhci_giveback_invalidated_tds(ep); 145762306a36Sopenharmony_ci 145862306a36Sopenharmony_ci /* if this was a soft reset, then restart */ 145962306a36Sopenharmony_ci if ((le32_to_cpu(trb->generic.field[3])) & TRB_TSP) 146062306a36Sopenharmony_ci ring_doorbell_for_active_rings(xhci, slot_id, ep_index); 146162306a36Sopenharmony_ci} 146262306a36Sopenharmony_ci 146362306a36Sopenharmony_cistatic void xhci_handle_cmd_enable_slot(struct xhci_hcd *xhci, int slot_id, 146462306a36Sopenharmony_ci struct xhci_command *command, u32 cmd_comp_code) 146562306a36Sopenharmony_ci{ 146662306a36Sopenharmony_ci if (cmd_comp_code == COMP_SUCCESS) 146762306a36Sopenharmony_ci command->slot_id = slot_id; 146862306a36Sopenharmony_ci else 146962306a36Sopenharmony_ci command->slot_id = 0; 147062306a36Sopenharmony_ci} 147162306a36Sopenharmony_ci 147262306a36Sopenharmony_cistatic void xhci_handle_cmd_disable_slot(struct xhci_hcd *xhci, int slot_id) 147362306a36Sopenharmony_ci{ 147462306a36Sopenharmony_ci struct xhci_virt_device *virt_dev; 147562306a36Sopenharmony_ci struct xhci_slot_ctx *slot_ctx; 147662306a36Sopenharmony_ci 147762306a36Sopenharmony_ci virt_dev = xhci->devs[slot_id]; 147862306a36Sopenharmony_ci if (!virt_dev) 147962306a36Sopenharmony_ci return; 148062306a36Sopenharmony_ci 148162306a36Sopenharmony_ci slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx); 148262306a36Sopenharmony_ci trace_xhci_handle_cmd_disable_slot(slot_ctx); 148362306a36Sopenharmony_ci 148462306a36Sopenharmony_ci if (xhci->quirks & XHCI_EP_LIMIT_QUIRK) 148562306a36Sopenharmony_ci /* Delete default control endpoint resources */ 148662306a36Sopenharmony_ci xhci_free_device_endpoint_resources(xhci, virt_dev, true); 148762306a36Sopenharmony_ci} 148862306a36Sopenharmony_ci 148962306a36Sopenharmony_cistatic void xhci_handle_cmd_config_ep(struct xhci_hcd *xhci, int slot_id, 149062306a36Sopenharmony_ci u32 cmd_comp_code) 149162306a36Sopenharmony_ci{ 149262306a36Sopenharmony_ci struct xhci_virt_device *virt_dev; 149362306a36Sopenharmony_ci struct xhci_input_control_ctx *ctrl_ctx; 149462306a36Sopenharmony_ci struct xhci_ep_ctx *ep_ctx; 149562306a36Sopenharmony_ci unsigned int ep_index; 149662306a36Sopenharmony_ci u32 add_flags; 149762306a36Sopenharmony_ci 149862306a36Sopenharmony_ci /* 149962306a36Sopenharmony_ci * Configure endpoint commands can come from the USB core configuration 150062306a36Sopenharmony_ci * or alt setting changes, or when streams were being configured. 150162306a36Sopenharmony_ci */ 150262306a36Sopenharmony_ci 150362306a36Sopenharmony_ci virt_dev = xhci->devs[slot_id]; 150462306a36Sopenharmony_ci if (!virt_dev) 150562306a36Sopenharmony_ci return; 150662306a36Sopenharmony_ci ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx); 150762306a36Sopenharmony_ci if (!ctrl_ctx) { 150862306a36Sopenharmony_ci xhci_warn(xhci, "Could not get input context, bad type.\n"); 150962306a36Sopenharmony_ci return; 151062306a36Sopenharmony_ci } 151162306a36Sopenharmony_ci 151262306a36Sopenharmony_ci add_flags = le32_to_cpu(ctrl_ctx->add_flags); 151362306a36Sopenharmony_ci 151462306a36Sopenharmony_ci /* Input ctx add_flags are the endpoint index plus one */ 151562306a36Sopenharmony_ci ep_index = xhci_last_valid_endpoint(add_flags) - 1; 151662306a36Sopenharmony_ci 151762306a36Sopenharmony_ci ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->out_ctx, ep_index); 151862306a36Sopenharmony_ci trace_xhci_handle_cmd_config_ep(ep_ctx); 151962306a36Sopenharmony_ci 152062306a36Sopenharmony_ci return; 152162306a36Sopenharmony_ci} 152262306a36Sopenharmony_ci 152362306a36Sopenharmony_cistatic void xhci_handle_cmd_addr_dev(struct xhci_hcd *xhci, int slot_id) 152462306a36Sopenharmony_ci{ 152562306a36Sopenharmony_ci struct xhci_virt_device *vdev; 152662306a36Sopenharmony_ci struct xhci_slot_ctx *slot_ctx; 152762306a36Sopenharmony_ci 152862306a36Sopenharmony_ci vdev = xhci->devs[slot_id]; 152962306a36Sopenharmony_ci if (!vdev) 153062306a36Sopenharmony_ci return; 153162306a36Sopenharmony_ci slot_ctx = xhci_get_slot_ctx(xhci, vdev->out_ctx); 153262306a36Sopenharmony_ci trace_xhci_handle_cmd_addr_dev(slot_ctx); 153362306a36Sopenharmony_ci} 153462306a36Sopenharmony_ci 153562306a36Sopenharmony_cistatic void xhci_handle_cmd_reset_dev(struct xhci_hcd *xhci, int slot_id) 153662306a36Sopenharmony_ci{ 153762306a36Sopenharmony_ci struct xhci_virt_device *vdev; 153862306a36Sopenharmony_ci struct xhci_slot_ctx *slot_ctx; 153962306a36Sopenharmony_ci 154062306a36Sopenharmony_ci vdev = xhci->devs[slot_id]; 154162306a36Sopenharmony_ci if (!vdev) { 154262306a36Sopenharmony_ci xhci_warn(xhci, "Reset device command completion for disabled slot %u\n", 154362306a36Sopenharmony_ci slot_id); 154462306a36Sopenharmony_ci return; 154562306a36Sopenharmony_ci } 154662306a36Sopenharmony_ci slot_ctx = xhci_get_slot_ctx(xhci, vdev->out_ctx); 154762306a36Sopenharmony_ci trace_xhci_handle_cmd_reset_dev(slot_ctx); 154862306a36Sopenharmony_ci 154962306a36Sopenharmony_ci xhci_dbg(xhci, "Completed reset device command.\n"); 155062306a36Sopenharmony_ci} 155162306a36Sopenharmony_ci 155262306a36Sopenharmony_cistatic void xhci_handle_cmd_nec_get_fw(struct xhci_hcd *xhci, 155362306a36Sopenharmony_ci struct xhci_event_cmd *event) 155462306a36Sopenharmony_ci{ 155562306a36Sopenharmony_ci if (!(xhci->quirks & XHCI_NEC_HOST)) { 155662306a36Sopenharmony_ci xhci_warn(xhci, "WARN NEC_GET_FW command on non-NEC host\n"); 155762306a36Sopenharmony_ci return; 155862306a36Sopenharmony_ci } 155962306a36Sopenharmony_ci xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, 156062306a36Sopenharmony_ci "NEC firmware version %2x.%02x", 156162306a36Sopenharmony_ci NEC_FW_MAJOR(le32_to_cpu(event->status)), 156262306a36Sopenharmony_ci NEC_FW_MINOR(le32_to_cpu(event->status))); 156362306a36Sopenharmony_ci} 156462306a36Sopenharmony_ci 156562306a36Sopenharmony_cistatic void xhci_complete_del_and_free_cmd(struct xhci_command *cmd, u32 status) 156662306a36Sopenharmony_ci{ 156762306a36Sopenharmony_ci list_del(&cmd->cmd_list); 156862306a36Sopenharmony_ci 156962306a36Sopenharmony_ci if (cmd->completion) { 157062306a36Sopenharmony_ci cmd->status = status; 157162306a36Sopenharmony_ci complete(cmd->completion); 157262306a36Sopenharmony_ci } else { 157362306a36Sopenharmony_ci kfree(cmd); 157462306a36Sopenharmony_ci } 157562306a36Sopenharmony_ci} 157662306a36Sopenharmony_ci 157762306a36Sopenharmony_civoid xhci_cleanup_command_queue(struct xhci_hcd *xhci) 157862306a36Sopenharmony_ci{ 157962306a36Sopenharmony_ci struct xhci_command *cur_cmd, *tmp_cmd; 158062306a36Sopenharmony_ci xhci->current_cmd = NULL; 158162306a36Sopenharmony_ci list_for_each_entry_safe(cur_cmd, tmp_cmd, &xhci->cmd_list, cmd_list) 158262306a36Sopenharmony_ci xhci_complete_del_and_free_cmd(cur_cmd, COMP_COMMAND_ABORTED); 158362306a36Sopenharmony_ci} 158462306a36Sopenharmony_ci 158562306a36Sopenharmony_civoid xhci_handle_command_timeout(struct work_struct *work) 158662306a36Sopenharmony_ci{ 158762306a36Sopenharmony_ci struct xhci_hcd *xhci; 158862306a36Sopenharmony_ci unsigned long flags; 158962306a36Sopenharmony_ci char str[XHCI_MSG_MAX]; 159062306a36Sopenharmony_ci u64 hw_ring_state; 159162306a36Sopenharmony_ci u32 cmd_field3; 159262306a36Sopenharmony_ci u32 usbsts; 159362306a36Sopenharmony_ci 159462306a36Sopenharmony_ci xhci = container_of(to_delayed_work(work), struct xhci_hcd, cmd_timer); 159562306a36Sopenharmony_ci 159662306a36Sopenharmony_ci spin_lock_irqsave(&xhci->lock, flags); 159762306a36Sopenharmony_ci 159862306a36Sopenharmony_ci /* 159962306a36Sopenharmony_ci * If timeout work is pending, or current_cmd is NULL, it means we 160062306a36Sopenharmony_ci * raced with command completion. Command is handled so just return. 160162306a36Sopenharmony_ci */ 160262306a36Sopenharmony_ci if (!xhci->current_cmd || delayed_work_pending(&xhci->cmd_timer)) { 160362306a36Sopenharmony_ci spin_unlock_irqrestore(&xhci->lock, flags); 160462306a36Sopenharmony_ci return; 160562306a36Sopenharmony_ci } 160662306a36Sopenharmony_ci 160762306a36Sopenharmony_ci cmd_field3 = le32_to_cpu(xhci->current_cmd->command_trb->generic.field[3]); 160862306a36Sopenharmony_ci usbsts = readl(&xhci->op_regs->status); 160962306a36Sopenharmony_ci xhci_dbg(xhci, "Command timeout, USBSTS:%s\n", xhci_decode_usbsts(str, usbsts)); 161062306a36Sopenharmony_ci 161162306a36Sopenharmony_ci /* Bail out and tear down xhci if a stop endpoint command failed */ 161262306a36Sopenharmony_ci if (TRB_FIELD_TO_TYPE(cmd_field3) == TRB_STOP_RING) { 161362306a36Sopenharmony_ci struct xhci_virt_ep *ep; 161462306a36Sopenharmony_ci 161562306a36Sopenharmony_ci xhci_warn(xhci, "xHCI host not responding to stop endpoint command\n"); 161662306a36Sopenharmony_ci 161762306a36Sopenharmony_ci ep = xhci_get_virt_ep(xhci, TRB_TO_SLOT_ID(cmd_field3), 161862306a36Sopenharmony_ci TRB_TO_EP_INDEX(cmd_field3)); 161962306a36Sopenharmony_ci if (ep) 162062306a36Sopenharmony_ci ep->ep_state &= ~EP_STOP_CMD_PENDING; 162162306a36Sopenharmony_ci 162262306a36Sopenharmony_ci xhci_halt(xhci); 162362306a36Sopenharmony_ci xhci_hc_died(xhci); 162462306a36Sopenharmony_ci goto time_out_completed; 162562306a36Sopenharmony_ci } 162662306a36Sopenharmony_ci 162762306a36Sopenharmony_ci /* mark this command to be cancelled */ 162862306a36Sopenharmony_ci xhci->current_cmd->status = COMP_COMMAND_ABORTED; 162962306a36Sopenharmony_ci 163062306a36Sopenharmony_ci /* Make sure command ring is running before aborting it */ 163162306a36Sopenharmony_ci hw_ring_state = xhci_read_64(xhci, &xhci->op_regs->cmd_ring); 163262306a36Sopenharmony_ci if (hw_ring_state == ~(u64)0) { 163362306a36Sopenharmony_ci xhci_hc_died(xhci); 163462306a36Sopenharmony_ci goto time_out_completed; 163562306a36Sopenharmony_ci } 163662306a36Sopenharmony_ci 163762306a36Sopenharmony_ci if ((xhci->cmd_ring_state & CMD_RING_STATE_RUNNING) && 163862306a36Sopenharmony_ci (hw_ring_state & CMD_RING_RUNNING)) { 163962306a36Sopenharmony_ci /* Prevent new doorbell, and start command abort */ 164062306a36Sopenharmony_ci xhci->cmd_ring_state = CMD_RING_STATE_ABORTED; 164162306a36Sopenharmony_ci xhci_dbg(xhci, "Command timeout\n"); 164262306a36Sopenharmony_ci xhci_abort_cmd_ring(xhci, flags); 164362306a36Sopenharmony_ci goto time_out_completed; 164462306a36Sopenharmony_ci } 164562306a36Sopenharmony_ci 164662306a36Sopenharmony_ci /* host removed. Bail out */ 164762306a36Sopenharmony_ci if (xhci->xhc_state & XHCI_STATE_REMOVING) { 164862306a36Sopenharmony_ci xhci_dbg(xhci, "host removed, ring start fail?\n"); 164962306a36Sopenharmony_ci xhci_cleanup_command_queue(xhci); 165062306a36Sopenharmony_ci 165162306a36Sopenharmony_ci goto time_out_completed; 165262306a36Sopenharmony_ci } 165362306a36Sopenharmony_ci 165462306a36Sopenharmony_ci /* command timeout on stopped ring, ring can't be aborted */ 165562306a36Sopenharmony_ci xhci_dbg(xhci, "Command timeout on stopped ring\n"); 165662306a36Sopenharmony_ci xhci_handle_stopped_cmd_ring(xhci, xhci->current_cmd); 165762306a36Sopenharmony_ci 165862306a36Sopenharmony_citime_out_completed: 165962306a36Sopenharmony_ci spin_unlock_irqrestore(&xhci->lock, flags); 166062306a36Sopenharmony_ci return; 166162306a36Sopenharmony_ci} 166262306a36Sopenharmony_ci 166362306a36Sopenharmony_cistatic void handle_cmd_completion(struct xhci_hcd *xhci, 166462306a36Sopenharmony_ci struct xhci_event_cmd *event) 166562306a36Sopenharmony_ci{ 166662306a36Sopenharmony_ci unsigned int slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags)); 166762306a36Sopenharmony_ci u64 cmd_dma; 166862306a36Sopenharmony_ci dma_addr_t cmd_dequeue_dma; 166962306a36Sopenharmony_ci u32 cmd_comp_code; 167062306a36Sopenharmony_ci union xhci_trb *cmd_trb; 167162306a36Sopenharmony_ci struct xhci_command *cmd; 167262306a36Sopenharmony_ci u32 cmd_type; 167362306a36Sopenharmony_ci 167462306a36Sopenharmony_ci if (slot_id >= MAX_HC_SLOTS) { 167562306a36Sopenharmony_ci xhci_warn(xhci, "Invalid slot_id %u\n", slot_id); 167662306a36Sopenharmony_ci return; 167762306a36Sopenharmony_ci } 167862306a36Sopenharmony_ci 167962306a36Sopenharmony_ci cmd_dma = le64_to_cpu(event->cmd_trb); 168062306a36Sopenharmony_ci cmd_trb = xhci->cmd_ring->dequeue; 168162306a36Sopenharmony_ci 168262306a36Sopenharmony_ci trace_xhci_handle_command(xhci->cmd_ring, &cmd_trb->generic); 168362306a36Sopenharmony_ci 168462306a36Sopenharmony_ci cmd_dequeue_dma = xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg, 168562306a36Sopenharmony_ci cmd_trb); 168662306a36Sopenharmony_ci /* 168762306a36Sopenharmony_ci * Check whether the completion event is for our internal kept 168862306a36Sopenharmony_ci * command. 168962306a36Sopenharmony_ci */ 169062306a36Sopenharmony_ci if (!cmd_dequeue_dma || cmd_dma != (u64)cmd_dequeue_dma) { 169162306a36Sopenharmony_ci xhci_warn(xhci, 169262306a36Sopenharmony_ci "ERROR mismatched command completion event\n"); 169362306a36Sopenharmony_ci return; 169462306a36Sopenharmony_ci } 169562306a36Sopenharmony_ci 169662306a36Sopenharmony_ci cmd = list_first_entry(&xhci->cmd_list, struct xhci_command, cmd_list); 169762306a36Sopenharmony_ci 169862306a36Sopenharmony_ci cancel_delayed_work(&xhci->cmd_timer); 169962306a36Sopenharmony_ci 170062306a36Sopenharmony_ci cmd_comp_code = GET_COMP_CODE(le32_to_cpu(event->status)); 170162306a36Sopenharmony_ci 170262306a36Sopenharmony_ci /* If CMD ring stopped we own the trbs between enqueue and dequeue */ 170362306a36Sopenharmony_ci if (cmd_comp_code == COMP_COMMAND_RING_STOPPED) { 170462306a36Sopenharmony_ci complete_all(&xhci->cmd_ring_stop_completion); 170562306a36Sopenharmony_ci return; 170662306a36Sopenharmony_ci } 170762306a36Sopenharmony_ci 170862306a36Sopenharmony_ci if (cmd->command_trb != xhci->cmd_ring->dequeue) { 170962306a36Sopenharmony_ci xhci_err(xhci, 171062306a36Sopenharmony_ci "Command completion event does not match command\n"); 171162306a36Sopenharmony_ci return; 171262306a36Sopenharmony_ci } 171362306a36Sopenharmony_ci 171462306a36Sopenharmony_ci /* 171562306a36Sopenharmony_ci * Host aborted the command ring, check if the current command was 171662306a36Sopenharmony_ci * supposed to be aborted, otherwise continue normally. 171762306a36Sopenharmony_ci * The command ring is stopped now, but the xHC will issue a Command 171862306a36Sopenharmony_ci * Ring Stopped event which will cause us to restart it. 171962306a36Sopenharmony_ci */ 172062306a36Sopenharmony_ci if (cmd_comp_code == COMP_COMMAND_ABORTED) { 172162306a36Sopenharmony_ci xhci->cmd_ring_state = CMD_RING_STATE_STOPPED; 172262306a36Sopenharmony_ci if (cmd->status == COMP_COMMAND_ABORTED) { 172362306a36Sopenharmony_ci if (xhci->current_cmd == cmd) 172462306a36Sopenharmony_ci xhci->current_cmd = NULL; 172562306a36Sopenharmony_ci goto event_handled; 172662306a36Sopenharmony_ci } 172762306a36Sopenharmony_ci } 172862306a36Sopenharmony_ci 172962306a36Sopenharmony_ci cmd_type = TRB_FIELD_TO_TYPE(le32_to_cpu(cmd_trb->generic.field[3])); 173062306a36Sopenharmony_ci switch (cmd_type) { 173162306a36Sopenharmony_ci case TRB_ENABLE_SLOT: 173262306a36Sopenharmony_ci xhci_handle_cmd_enable_slot(xhci, slot_id, cmd, cmd_comp_code); 173362306a36Sopenharmony_ci break; 173462306a36Sopenharmony_ci case TRB_DISABLE_SLOT: 173562306a36Sopenharmony_ci xhci_handle_cmd_disable_slot(xhci, slot_id); 173662306a36Sopenharmony_ci break; 173762306a36Sopenharmony_ci case TRB_CONFIG_EP: 173862306a36Sopenharmony_ci if (!cmd->completion) 173962306a36Sopenharmony_ci xhci_handle_cmd_config_ep(xhci, slot_id, cmd_comp_code); 174062306a36Sopenharmony_ci break; 174162306a36Sopenharmony_ci case TRB_EVAL_CONTEXT: 174262306a36Sopenharmony_ci break; 174362306a36Sopenharmony_ci case TRB_ADDR_DEV: 174462306a36Sopenharmony_ci xhci_handle_cmd_addr_dev(xhci, slot_id); 174562306a36Sopenharmony_ci break; 174662306a36Sopenharmony_ci case TRB_STOP_RING: 174762306a36Sopenharmony_ci WARN_ON(slot_id != TRB_TO_SLOT_ID( 174862306a36Sopenharmony_ci le32_to_cpu(cmd_trb->generic.field[3]))); 174962306a36Sopenharmony_ci if (!cmd->completion) 175062306a36Sopenharmony_ci xhci_handle_cmd_stop_ep(xhci, slot_id, cmd_trb, 175162306a36Sopenharmony_ci cmd_comp_code); 175262306a36Sopenharmony_ci break; 175362306a36Sopenharmony_ci case TRB_SET_DEQ: 175462306a36Sopenharmony_ci WARN_ON(slot_id != TRB_TO_SLOT_ID( 175562306a36Sopenharmony_ci le32_to_cpu(cmd_trb->generic.field[3]))); 175662306a36Sopenharmony_ci xhci_handle_cmd_set_deq(xhci, slot_id, cmd_trb, cmd_comp_code); 175762306a36Sopenharmony_ci break; 175862306a36Sopenharmony_ci case TRB_CMD_NOOP: 175962306a36Sopenharmony_ci /* Is this an aborted command turned to NO-OP? */ 176062306a36Sopenharmony_ci if (cmd->status == COMP_COMMAND_RING_STOPPED) 176162306a36Sopenharmony_ci cmd_comp_code = COMP_COMMAND_RING_STOPPED; 176262306a36Sopenharmony_ci break; 176362306a36Sopenharmony_ci case TRB_RESET_EP: 176462306a36Sopenharmony_ci WARN_ON(slot_id != TRB_TO_SLOT_ID( 176562306a36Sopenharmony_ci le32_to_cpu(cmd_trb->generic.field[3]))); 176662306a36Sopenharmony_ci xhci_handle_cmd_reset_ep(xhci, slot_id, cmd_trb, cmd_comp_code); 176762306a36Sopenharmony_ci break; 176862306a36Sopenharmony_ci case TRB_RESET_DEV: 176962306a36Sopenharmony_ci /* SLOT_ID field in reset device cmd completion event TRB is 0. 177062306a36Sopenharmony_ci * Use the SLOT_ID from the command TRB instead (xhci 4.6.11) 177162306a36Sopenharmony_ci */ 177262306a36Sopenharmony_ci slot_id = TRB_TO_SLOT_ID( 177362306a36Sopenharmony_ci le32_to_cpu(cmd_trb->generic.field[3])); 177462306a36Sopenharmony_ci xhci_handle_cmd_reset_dev(xhci, slot_id); 177562306a36Sopenharmony_ci break; 177662306a36Sopenharmony_ci case TRB_NEC_GET_FW: 177762306a36Sopenharmony_ci xhci_handle_cmd_nec_get_fw(xhci, event); 177862306a36Sopenharmony_ci break; 177962306a36Sopenharmony_ci default: 178062306a36Sopenharmony_ci /* Skip over unknown commands on the event ring */ 178162306a36Sopenharmony_ci xhci_info(xhci, "INFO unknown command type %d\n", cmd_type); 178262306a36Sopenharmony_ci break; 178362306a36Sopenharmony_ci } 178462306a36Sopenharmony_ci 178562306a36Sopenharmony_ci /* restart timer if this wasn't the last command */ 178662306a36Sopenharmony_ci if (!list_is_singular(&xhci->cmd_list)) { 178762306a36Sopenharmony_ci xhci->current_cmd = list_first_entry(&cmd->cmd_list, 178862306a36Sopenharmony_ci struct xhci_command, cmd_list); 178962306a36Sopenharmony_ci xhci_mod_cmd_timer(xhci, XHCI_CMD_DEFAULT_TIMEOUT); 179062306a36Sopenharmony_ci } else if (xhci->current_cmd == cmd) { 179162306a36Sopenharmony_ci xhci->current_cmd = NULL; 179262306a36Sopenharmony_ci } 179362306a36Sopenharmony_ci 179462306a36Sopenharmony_cievent_handled: 179562306a36Sopenharmony_ci xhci_complete_del_and_free_cmd(cmd, cmd_comp_code); 179662306a36Sopenharmony_ci 179762306a36Sopenharmony_ci inc_deq(xhci, xhci->cmd_ring); 179862306a36Sopenharmony_ci} 179962306a36Sopenharmony_ci 180062306a36Sopenharmony_cistatic void handle_vendor_event(struct xhci_hcd *xhci, 180162306a36Sopenharmony_ci union xhci_trb *event, u32 trb_type) 180262306a36Sopenharmony_ci{ 180362306a36Sopenharmony_ci xhci_dbg(xhci, "Vendor specific event TRB type = %u\n", trb_type); 180462306a36Sopenharmony_ci if (trb_type == TRB_NEC_CMD_COMP && (xhci->quirks & XHCI_NEC_HOST)) 180562306a36Sopenharmony_ci handle_cmd_completion(xhci, &event->event_cmd); 180662306a36Sopenharmony_ci} 180762306a36Sopenharmony_ci 180862306a36Sopenharmony_cistatic void handle_device_notification(struct xhci_hcd *xhci, 180962306a36Sopenharmony_ci union xhci_trb *event) 181062306a36Sopenharmony_ci{ 181162306a36Sopenharmony_ci u32 slot_id; 181262306a36Sopenharmony_ci struct usb_device *udev; 181362306a36Sopenharmony_ci 181462306a36Sopenharmony_ci slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->generic.field[3])); 181562306a36Sopenharmony_ci if (!xhci->devs[slot_id]) { 181662306a36Sopenharmony_ci xhci_warn(xhci, "Device Notification event for " 181762306a36Sopenharmony_ci "unused slot %u\n", slot_id); 181862306a36Sopenharmony_ci return; 181962306a36Sopenharmony_ci } 182062306a36Sopenharmony_ci 182162306a36Sopenharmony_ci xhci_dbg(xhci, "Device Wake Notification event for slot ID %u\n", 182262306a36Sopenharmony_ci slot_id); 182362306a36Sopenharmony_ci udev = xhci->devs[slot_id]->udev; 182462306a36Sopenharmony_ci if (udev && udev->parent) 182562306a36Sopenharmony_ci usb_wakeup_notification(udev->parent, udev->portnum); 182662306a36Sopenharmony_ci} 182762306a36Sopenharmony_ci 182862306a36Sopenharmony_ci/* 182962306a36Sopenharmony_ci * Quirk hanlder for errata seen on Cavium ThunderX2 processor XHCI 183062306a36Sopenharmony_ci * Controller. 183162306a36Sopenharmony_ci * As per ThunderX2errata-129 USB 2 device may come up as USB 1 183262306a36Sopenharmony_ci * If a connection to a USB 1 device is followed by another connection 183362306a36Sopenharmony_ci * to a USB 2 device. 183462306a36Sopenharmony_ci * 183562306a36Sopenharmony_ci * Reset the PHY after the USB device is disconnected if device speed 183662306a36Sopenharmony_ci * is less than HCD_USB3. 183762306a36Sopenharmony_ci * Retry the reset sequence max of 4 times checking the PLL lock status. 183862306a36Sopenharmony_ci * 183962306a36Sopenharmony_ci */ 184062306a36Sopenharmony_cistatic void xhci_cavium_reset_phy_quirk(struct xhci_hcd *xhci) 184162306a36Sopenharmony_ci{ 184262306a36Sopenharmony_ci struct usb_hcd *hcd = xhci_to_hcd(xhci); 184362306a36Sopenharmony_ci u32 pll_lock_check; 184462306a36Sopenharmony_ci u32 retry_count = 4; 184562306a36Sopenharmony_ci 184662306a36Sopenharmony_ci do { 184762306a36Sopenharmony_ci /* Assert PHY reset */ 184862306a36Sopenharmony_ci writel(0x6F, hcd->regs + 0x1048); 184962306a36Sopenharmony_ci udelay(10); 185062306a36Sopenharmony_ci /* De-assert the PHY reset */ 185162306a36Sopenharmony_ci writel(0x7F, hcd->regs + 0x1048); 185262306a36Sopenharmony_ci udelay(200); 185362306a36Sopenharmony_ci pll_lock_check = readl(hcd->regs + 0x1070); 185462306a36Sopenharmony_ci } while (!(pll_lock_check & 0x1) && --retry_count); 185562306a36Sopenharmony_ci} 185662306a36Sopenharmony_ci 185762306a36Sopenharmony_cistatic void handle_port_status(struct xhci_hcd *xhci, 185862306a36Sopenharmony_ci struct xhci_interrupter *ir, 185962306a36Sopenharmony_ci union xhci_trb *event) 186062306a36Sopenharmony_ci{ 186162306a36Sopenharmony_ci struct usb_hcd *hcd; 186262306a36Sopenharmony_ci u32 port_id; 186362306a36Sopenharmony_ci u32 portsc, cmd_reg; 186462306a36Sopenharmony_ci int max_ports; 186562306a36Sopenharmony_ci int slot_id; 186662306a36Sopenharmony_ci unsigned int hcd_portnum; 186762306a36Sopenharmony_ci struct xhci_bus_state *bus_state; 186862306a36Sopenharmony_ci bool bogus_port_status = false; 186962306a36Sopenharmony_ci struct xhci_port *port; 187062306a36Sopenharmony_ci 187162306a36Sopenharmony_ci /* Port status change events always have a successful completion code */ 187262306a36Sopenharmony_ci if (GET_COMP_CODE(le32_to_cpu(event->generic.field[2])) != COMP_SUCCESS) 187362306a36Sopenharmony_ci xhci_warn(xhci, 187462306a36Sopenharmony_ci "WARN: xHC returned failed port status event\n"); 187562306a36Sopenharmony_ci 187662306a36Sopenharmony_ci port_id = GET_PORT_ID(le32_to_cpu(event->generic.field[0])); 187762306a36Sopenharmony_ci max_ports = HCS_MAX_PORTS(xhci->hcs_params1); 187862306a36Sopenharmony_ci 187962306a36Sopenharmony_ci if ((port_id <= 0) || (port_id > max_ports)) { 188062306a36Sopenharmony_ci xhci_warn(xhci, "Port change event with invalid port ID %d\n", 188162306a36Sopenharmony_ci port_id); 188262306a36Sopenharmony_ci inc_deq(xhci, ir->event_ring); 188362306a36Sopenharmony_ci return; 188462306a36Sopenharmony_ci } 188562306a36Sopenharmony_ci 188662306a36Sopenharmony_ci port = &xhci->hw_ports[port_id - 1]; 188762306a36Sopenharmony_ci if (!port || !port->rhub || port->hcd_portnum == DUPLICATE_ENTRY) { 188862306a36Sopenharmony_ci xhci_warn(xhci, "Port change event, no port for port ID %u\n", 188962306a36Sopenharmony_ci port_id); 189062306a36Sopenharmony_ci bogus_port_status = true; 189162306a36Sopenharmony_ci goto cleanup; 189262306a36Sopenharmony_ci } 189362306a36Sopenharmony_ci 189462306a36Sopenharmony_ci /* We might get interrupts after shared_hcd is removed */ 189562306a36Sopenharmony_ci if (port->rhub == &xhci->usb3_rhub && xhci->shared_hcd == NULL) { 189662306a36Sopenharmony_ci xhci_dbg(xhci, "ignore port event for removed USB3 hcd\n"); 189762306a36Sopenharmony_ci bogus_port_status = true; 189862306a36Sopenharmony_ci goto cleanup; 189962306a36Sopenharmony_ci } 190062306a36Sopenharmony_ci 190162306a36Sopenharmony_ci hcd = port->rhub->hcd; 190262306a36Sopenharmony_ci bus_state = &port->rhub->bus_state; 190362306a36Sopenharmony_ci hcd_portnum = port->hcd_portnum; 190462306a36Sopenharmony_ci portsc = readl(port->addr); 190562306a36Sopenharmony_ci 190662306a36Sopenharmony_ci xhci_dbg(xhci, "Port change event, %d-%d, id %d, portsc: 0x%x\n", 190762306a36Sopenharmony_ci hcd->self.busnum, hcd_portnum + 1, port_id, portsc); 190862306a36Sopenharmony_ci 190962306a36Sopenharmony_ci trace_xhci_handle_port_status(hcd_portnum, portsc); 191062306a36Sopenharmony_ci 191162306a36Sopenharmony_ci if (hcd->state == HC_STATE_SUSPENDED) { 191262306a36Sopenharmony_ci xhci_dbg(xhci, "resume root hub\n"); 191362306a36Sopenharmony_ci usb_hcd_resume_root_hub(hcd); 191462306a36Sopenharmony_ci } 191562306a36Sopenharmony_ci 191662306a36Sopenharmony_ci if (hcd->speed >= HCD_USB3 && 191762306a36Sopenharmony_ci (portsc & PORT_PLS_MASK) == XDEV_INACTIVE) { 191862306a36Sopenharmony_ci slot_id = xhci_find_slot_id_by_port(hcd, xhci, hcd_portnum + 1); 191962306a36Sopenharmony_ci if (slot_id && xhci->devs[slot_id]) 192062306a36Sopenharmony_ci xhci->devs[slot_id]->flags |= VDEV_PORT_ERROR; 192162306a36Sopenharmony_ci } 192262306a36Sopenharmony_ci 192362306a36Sopenharmony_ci if ((portsc & PORT_PLC) && (portsc & PORT_PLS_MASK) == XDEV_RESUME) { 192462306a36Sopenharmony_ci xhci_dbg(xhci, "port resume event for port %d\n", port_id); 192562306a36Sopenharmony_ci 192662306a36Sopenharmony_ci cmd_reg = readl(&xhci->op_regs->command); 192762306a36Sopenharmony_ci if (!(cmd_reg & CMD_RUN)) { 192862306a36Sopenharmony_ci xhci_warn(xhci, "xHC is not running.\n"); 192962306a36Sopenharmony_ci goto cleanup; 193062306a36Sopenharmony_ci } 193162306a36Sopenharmony_ci 193262306a36Sopenharmony_ci if (DEV_SUPERSPEED_ANY(portsc)) { 193362306a36Sopenharmony_ci xhci_dbg(xhci, "remote wake SS port %d\n", port_id); 193462306a36Sopenharmony_ci /* Set a flag to say the port signaled remote wakeup, 193562306a36Sopenharmony_ci * so we can tell the difference between the end of 193662306a36Sopenharmony_ci * device and host initiated resume. 193762306a36Sopenharmony_ci */ 193862306a36Sopenharmony_ci bus_state->port_remote_wakeup |= 1 << hcd_portnum; 193962306a36Sopenharmony_ci xhci_test_and_clear_bit(xhci, port, PORT_PLC); 194062306a36Sopenharmony_ci usb_hcd_start_port_resume(&hcd->self, hcd_portnum); 194162306a36Sopenharmony_ci xhci_set_link_state(xhci, port, XDEV_U0); 194262306a36Sopenharmony_ci /* Need to wait until the next link state change 194362306a36Sopenharmony_ci * indicates the device is actually in U0. 194462306a36Sopenharmony_ci */ 194562306a36Sopenharmony_ci bogus_port_status = true; 194662306a36Sopenharmony_ci goto cleanup; 194762306a36Sopenharmony_ci } else if (!test_bit(hcd_portnum, &bus_state->resuming_ports)) { 194862306a36Sopenharmony_ci xhci_dbg(xhci, "resume HS port %d\n", port_id); 194962306a36Sopenharmony_ci port->resume_timestamp = jiffies + 195062306a36Sopenharmony_ci msecs_to_jiffies(USB_RESUME_TIMEOUT); 195162306a36Sopenharmony_ci set_bit(hcd_portnum, &bus_state->resuming_ports); 195262306a36Sopenharmony_ci /* Do the rest in GetPortStatus after resume time delay. 195362306a36Sopenharmony_ci * Avoid polling roothub status before that so that a 195462306a36Sopenharmony_ci * usb device auto-resume latency around ~40ms. 195562306a36Sopenharmony_ci */ 195662306a36Sopenharmony_ci set_bit(HCD_FLAG_POLL_RH, &hcd->flags); 195762306a36Sopenharmony_ci mod_timer(&hcd->rh_timer, 195862306a36Sopenharmony_ci port->resume_timestamp); 195962306a36Sopenharmony_ci usb_hcd_start_port_resume(&hcd->self, hcd_portnum); 196062306a36Sopenharmony_ci bogus_port_status = true; 196162306a36Sopenharmony_ci } 196262306a36Sopenharmony_ci } 196362306a36Sopenharmony_ci 196462306a36Sopenharmony_ci if ((portsc & PORT_PLC) && 196562306a36Sopenharmony_ci DEV_SUPERSPEED_ANY(portsc) && 196662306a36Sopenharmony_ci ((portsc & PORT_PLS_MASK) == XDEV_U0 || 196762306a36Sopenharmony_ci (portsc & PORT_PLS_MASK) == XDEV_U1 || 196862306a36Sopenharmony_ci (portsc & PORT_PLS_MASK) == XDEV_U2)) { 196962306a36Sopenharmony_ci xhci_dbg(xhci, "resume SS port %d finished\n", port_id); 197062306a36Sopenharmony_ci complete(&port->u3exit_done); 197162306a36Sopenharmony_ci /* We've just brought the device into U0/1/2 through either the 197262306a36Sopenharmony_ci * Resume state after a device remote wakeup, or through the 197362306a36Sopenharmony_ci * U3Exit state after a host-initiated resume. If it's a device 197462306a36Sopenharmony_ci * initiated remote wake, don't pass up the link state change, 197562306a36Sopenharmony_ci * so the roothub behavior is consistent with external 197662306a36Sopenharmony_ci * USB 3.0 hub behavior. 197762306a36Sopenharmony_ci */ 197862306a36Sopenharmony_ci slot_id = xhci_find_slot_id_by_port(hcd, xhci, hcd_portnum + 1); 197962306a36Sopenharmony_ci if (slot_id && xhci->devs[slot_id]) 198062306a36Sopenharmony_ci xhci_ring_device(xhci, slot_id); 198162306a36Sopenharmony_ci if (bus_state->port_remote_wakeup & (1 << hcd_portnum)) { 198262306a36Sopenharmony_ci xhci_test_and_clear_bit(xhci, port, PORT_PLC); 198362306a36Sopenharmony_ci usb_wakeup_notification(hcd->self.root_hub, 198462306a36Sopenharmony_ci hcd_portnum + 1); 198562306a36Sopenharmony_ci bogus_port_status = true; 198662306a36Sopenharmony_ci goto cleanup; 198762306a36Sopenharmony_ci } 198862306a36Sopenharmony_ci } 198962306a36Sopenharmony_ci 199062306a36Sopenharmony_ci /* 199162306a36Sopenharmony_ci * Check to see if xhci-hub.c is waiting on RExit to U0 transition (or 199262306a36Sopenharmony_ci * RExit to a disconnect state). If so, let the driver know it's 199362306a36Sopenharmony_ci * out of the RExit state. 199462306a36Sopenharmony_ci */ 199562306a36Sopenharmony_ci if (hcd->speed < HCD_USB3 && port->rexit_active) { 199662306a36Sopenharmony_ci complete(&port->rexit_done); 199762306a36Sopenharmony_ci port->rexit_active = false; 199862306a36Sopenharmony_ci bogus_port_status = true; 199962306a36Sopenharmony_ci goto cleanup; 200062306a36Sopenharmony_ci } 200162306a36Sopenharmony_ci 200262306a36Sopenharmony_ci if (hcd->speed < HCD_USB3) { 200362306a36Sopenharmony_ci xhci_test_and_clear_bit(xhci, port, PORT_PLC); 200462306a36Sopenharmony_ci if ((xhci->quirks & XHCI_RESET_PLL_ON_DISCONNECT) && 200562306a36Sopenharmony_ci (portsc & PORT_CSC) && !(portsc & PORT_CONNECT)) 200662306a36Sopenharmony_ci xhci_cavium_reset_phy_quirk(xhci); 200762306a36Sopenharmony_ci } 200862306a36Sopenharmony_ci 200962306a36Sopenharmony_cicleanup: 201062306a36Sopenharmony_ci /* Update event ring dequeue pointer before dropping the lock */ 201162306a36Sopenharmony_ci inc_deq(xhci, ir->event_ring); 201262306a36Sopenharmony_ci 201362306a36Sopenharmony_ci /* Don't make the USB core poll the roothub if we got a bad port status 201462306a36Sopenharmony_ci * change event. Besides, at that point we can't tell which roothub 201562306a36Sopenharmony_ci * (USB 2.0 or USB 3.0) to kick. 201662306a36Sopenharmony_ci */ 201762306a36Sopenharmony_ci if (bogus_port_status) 201862306a36Sopenharmony_ci return; 201962306a36Sopenharmony_ci 202062306a36Sopenharmony_ci /* 202162306a36Sopenharmony_ci * xHCI port-status-change events occur when the "or" of all the 202262306a36Sopenharmony_ci * status-change bits in the portsc register changes from 0 to 1. 202362306a36Sopenharmony_ci * New status changes won't cause an event if any other change 202462306a36Sopenharmony_ci * bits are still set. When an event occurs, switch over to 202562306a36Sopenharmony_ci * polling to avoid losing status changes. 202662306a36Sopenharmony_ci */ 202762306a36Sopenharmony_ci xhci_dbg(xhci, "%s: starting usb%d port polling.\n", 202862306a36Sopenharmony_ci __func__, hcd->self.busnum); 202962306a36Sopenharmony_ci set_bit(HCD_FLAG_POLL_RH, &hcd->flags); 203062306a36Sopenharmony_ci spin_unlock(&xhci->lock); 203162306a36Sopenharmony_ci /* Pass this up to the core */ 203262306a36Sopenharmony_ci usb_hcd_poll_rh_status(hcd); 203362306a36Sopenharmony_ci spin_lock(&xhci->lock); 203462306a36Sopenharmony_ci} 203562306a36Sopenharmony_ci 203662306a36Sopenharmony_ci/* 203762306a36Sopenharmony_ci * This TD is defined by the TRBs starting at start_trb in start_seg and ending 203862306a36Sopenharmony_ci * at end_trb, which may be in another segment. If the suspect DMA address is a 203962306a36Sopenharmony_ci * TRB in this TD, this function returns that TRB's segment. Otherwise it 204062306a36Sopenharmony_ci * returns 0. 204162306a36Sopenharmony_ci */ 204262306a36Sopenharmony_cistruct xhci_segment *trb_in_td(struct xhci_hcd *xhci, 204362306a36Sopenharmony_ci struct xhci_segment *start_seg, 204462306a36Sopenharmony_ci union xhci_trb *start_trb, 204562306a36Sopenharmony_ci union xhci_trb *end_trb, 204662306a36Sopenharmony_ci dma_addr_t suspect_dma, 204762306a36Sopenharmony_ci bool debug) 204862306a36Sopenharmony_ci{ 204962306a36Sopenharmony_ci dma_addr_t start_dma; 205062306a36Sopenharmony_ci dma_addr_t end_seg_dma; 205162306a36Sopenharmony_ci dma_addr_t end_trb_dma; 205262306a36Sopenharmony_ci struct xhci_segment *cur_seg; 205362306a36Sopenharmony_ci 205462306a36Sopenharmony_ci start_dma = xhci_trb_virt_to_dma(start_seg, start_trb); 205562306a36Sopenharmony_ci cur_seg = start_seg; 205662306a36Sopenharmony_ci 205762306a36Sopenharmony_ci do { 205862306a36Sopenharmony_ci if (start_dma == 0) 205962306a36Sopenharmony_ci return NULL; 206062306a36Sopenharmony_ci /* We may get an event for a Link TRB in the middle of a TD */ 206162306a36Sopenharmony_ci end_seg_dma = xhci_trb_virt_to_dma(cur_seg, 206262306a36Sopenharmony_ci &cur_seg->trbs[TRBS_PER_SEGMENT - 1]); 206362306a36Sopenharmony_ci /* If the end TRB isn't in this segment, this is set to 0 */ 206462306a36Sopenharmony_ci end_trb_dma = xhci_trb_virt_to_dma(cur_seg, end_trb); 206562306a36Sopenharmony_ci 206662306a36Sopenharmony_ci if (debug) 206762306a36Sopenharmony_ci xhci_warn(xhci, 206862306a36Sopenharmony_ci "Looking for event-dma %016llx trb-start %016llx trb-end %016llx seg-start %016llx seg-end %016llx\n", 206962306a36Sopenharmony_ci (unsigned long long)suspect_dma, 207062306a36Sopenharmony_ci (unsigned long long)start_dma, 207162306a36Sopenharmony_ci (unsigned long long)end_trb_dma, 207262306a36Sopenharmony_ci (unsigned long long)cur_seg->dma, 207362306a36Sopenharmony_ci (unsigned long long)end_seg_dma); 207462306a36Sopenharmony_ci 207562306a36Sopenharmony_ci if (end_trb_dma > 0) { 207662306a36Sopenharmony_ci /* The end TRB is in this segment, so suspect should be here */ 207762306a36Sopenharmony_ci if (start_dma <= end_trb_dma) { 207862306a36Sopenharmony_ci if (suspect_dma >= start_dma && suspect_dma <= end_trb_dma) 207962306a36Sopenharmony_ci return cur_seg; 208062306a36Sopenharmony_ci } else { 208162306a36Sopenharmony_ci /* Case for one segment with 208262306a36Sopenharmony_ci * a TD wrapped around to the top 208362306a36Sopenharmony_ci */ 208462306a36Sopenharmony_ci if ((suspect_dma >= start_dma && 208562306a36Sopenharmony_ci suspect_dma <= end_seg_dma) || 208662306a36Sopenharmony_ci (suspect_dma >= cur_seg->dma && 208762306a36Sopenharmony_ci suspect_dma <= end_trb_dma)) 208862306a36Sopenharmony_ci return cur_seg; 208962306a36Sopenharmony_ci } 209062306a36Sopenharmony_ci return NULL; 209162306a36Sopenharmony_ci } else { 209262306a36Sopenharmony_ci /* Might still be somewhere in this segment */ 209362306a36Sopenharmony_ci if (suspect_dma >= start_dma && suspect_dma <= end_seg_dma) 209462306a36Sopenharmony_ci return cur_seg; 209562306a36Sopenharmony_ci } 209662306a36Sopenharmony_ci cur_seg = cur_seg->next; 209762306a36Sopenharmony_ci start_dma = xhci_trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]); 209862306a36Sopenharmony_ci } while (cur_seg != start_seg); 209962306a36Sopenharmony_ci 210062306a36Sopenharmony_ci return NULL; 210162306a36Sopenharmony_ci} 210262306a36Sopenharmony_ci 210362306a36Sopenharmony_cistatic void xhci_clear_hub_tt_buffer(struct xhci_hcd *xhci, struct xhci_td *td, 210462306a36Sopenharmony_ci struct xhci_virt_ep *ep) 210562306a36Sopenharmony_ci{ 210662306a36Sopenharmony_ci /* 210762306a36Sopenharmony_ci * As part of low/full-speed endpoint-halt processing 210862306a36Sopenharmony_ci * we must clear the TT buffer (USB 2.0 specification 11.17.5). 210962306a36Sopenharmony_ci */ 211062306a36Sopenharmony_ci if (td->urb->dev->tt && !usb_pipeint(td->urb->pipe) && 211162306a36Sopenharmony_ci (td->urb->dev->tt->hub != xhci_to_hcd(xhci)->self.root_hub) && 211262306a36Sopenharmony_ci !(ep->ep_state & EP_CLEARING_TT)) { 211362306a36Sopenharmony_ci ep->ep_state |= EP_CLEARING_TT; 211462306a36Sopenharmony_ci td->urb->ep->hcpriv = td->urb->dev; 211562306a36Sopenharmony_ci if (usb_hub_clear_tt_buffer(td->urb)) 211662306a36Sopenharmony_ci ep->ep_state &= ~EP_CLEARING_TT; 211762306a36Sopenharmony_ci } 211862306a36Sopenharmony_ci} 211962306a36Sopenharmony_ci 212062306a36Sopenharmony_ci/* Check if an error has halted the endpoint ring. The class driver will 212162306a36Sopenharmony_ci * cleanup the halt for a non-default control endpoint if we indicate a stall. 212262306a36Sopenharmony_ci * However, a babble and other errors also halt the endpoint ring, and the class 212362306a36Sopenharmony_ci * driver won't clear the halt in that case, so we need to issue a Set Transfer 212462306a36Sopenharmony_ci * Ring Dequeue Pointer command manually. 212562306a36Sopenharmony_ci */ 212662306a36Sopenharmony_cistatic int xhci_requires_manual_halt_cleanup(struct xhci_hcd *xhci, 212762306a36Sopenharmony_ci struct xhci_ep_ctx *ep_ctx, 212862306a36Sopenharmony_ci unsigned int trb_comp_code) 212962306a36Sopenharmony_ci{ 213062306a36Sopenharmony_ci /* TRB completion codes that may require a manual halt cleanup */ 213162306a36Sopenharmony_ci if (trb_comp_code == COMP_USB_TRANSACTION_ERROR || 213262306a36Sopenharmony_ci trb_comp_code == COMP_BABBLE_DETECTED_ERROR || 213362306a36Sopenharmony_ci trb_comp_code == COMP_SPLIT_TRANSACTION_ERROR) 213462306a36Sopenharmony_ci /* The 0.95 spec says a babbling control endpoint 213562306a36Sopenharmony_ci * is not halted. The 0.96 spec says it is. Some HW 213662306a36Sopenharmony_ci * claims to be 0.95 compliant, but it halts the control 213762306a36Sopenharmony_ci * endpoint anyway. Check if a babble halted the 213862306a36Sopenharmony_ci * endpoint. 213962306a36Sopenharmony_ci */ 214062306a36Sopenharmony_ci if (GET_EP_CTX_STATE(ep_ctx) == EP_STATE_HALTED) 214162306a36Sopenharmony_ci return 1; 214262306a36Sopenharmony_ci 214362306a36Sopenharmony_ci return 0; 214462306a36Sopenharmony_ci} 214562306a36Sopenharmony_ci 214662306a36Sopenharmony_ciint xhci_is_vendor_info_code(struct xhci_hcd *xhci, unsigned int trb_comp_code) 214762306a36Sopenharmony_ci{ 214862306a36Sopenharmony_ci if (trb_comp_code >= 224 && trb_comp_code <= 255) { 214962306a36Sopenharmony_ci /* Vendor defined "informational" completion code, 215062306a36Sopenharmony_ci * treat as not-an-error. 215162306a36Sopenharmony_ci */ 215262306a36Sopenharmony_ci xhci_dbg(xhci, "Vendor defined info completion code %u\n", 215362306a36Sopenharmony_ci trb_comp_code); 215462306a36Sopenharmony_ci xhci_dbg(xhci, "Treating code as success.\n"); 215562306a36Sopenharmony_ci return 1; 215662306a36Sopenharmony_ci } 215762306a36Sopenharmony_ci return 0; 215862306a36Sopenharmony_ci} 215962306a36Sopenharmony_ci 216062306a36Sopenharmony_cistatic int finish_td(struct xhci_hcd *xhci, struct xhci_virt_ep *ep, 216162306a36Sopenharmony_ci struct xhci_ring *ep_ring, struct xhci_td *td, 216262306a36Sopenharmony_ci u32 trb_comp_code) 216362306a36Sopenharmony_ci{ 216462306a36Sopenharmony_ci struct xhci_ep_ctx *ep_ctx; 216562306a36Sopenharmony_ci 216662306a36Sopenharmony_ci ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep->ep_index); 216762306a36Sopenharmony_ci 216862306a36Sopenharmony_ci switch (trb_comp_code) { 216962306a36Sopenharmony_ci case COMP_STOPPED_LENGTH_INVALID: 217062306a36Sopenharmony_ci case COMP_STOPPED_SHORT_PACKET: 217162306a36Sopenharmony_ci case COMP_STOPPED: 217262306a36Sopenharmony_ci /* 217362306a36Sopenharmony_ci * The "Stop Endpoint" completion will take care of any 217462306a36Sopenharmony_ci * stopped TDs. A stopped TD may be restarted, so don't update 217562306a36Sopenharmony_ci * the ring dequeue pointer or take this TD off any lists yet. 217662306a36Sopenharmony_ci */ 217762306a36Sopenharmony_ci return 0; 217862306a36Sopenharmony_ci case COMP_USB_TRANSACTION_ERROR: 217962306a36Sopenharmony_ci case COMP_BABBLE_DETECTED_ERROR: 218062306a36Sopenharmony_ci case COMP_SPLIT_TRANSACTION_ERROR: 218162306a36Sopenharmony_ci /* 218262306a36Sopenharmony_ci * If endpoint context state is not halted we might be 218362306a36Sopenharmony_ci * racing with a reset endpoint command issued by a unsuccessful 218462306a36Sopenharmony_ci * stop endpoint completion (context error). In that case the 218562306a36Sopenharmony_ci * td should be on the cancelled list, and EP_HALTED flag set. 218662306a36Sopenharmony_ci * 218762306a36Sopenharmony_ci * Or then it's not halted due to the 0.95 spec stating that a 218862306a36Sopenharmony_ci * babbling control endpoint should not halt. The 0.96 spec 218962306a36Sopenharmony_ci * again says it should. Some HW claims to be 0.95 compliant, 219062306a36Sopenharmony_ci * but it halts the control endpoint anyway. 219162306a36Sopenharmony_ci */ 219262306a36Sopenharmony_ci if (GET_EP_CTX_STATE(ep_ctx) != EP_STATE_HALTED) { 219362306a36Sopenharmony_ci /* 219462306a36Sopenharmony_ci * If EP_HALTED is set and TD is on the cancelled list 219562306a36Sopenharmony_ci * the TD and dequeue pointer will be handled by reset 219662306a36Sopenharmony_ci * ep command completion 219762306a36Sopenharmony_ci */ 219862306a36Sopenharmony_ci if ((ep->ep_state & EP_HALTED) && 219962306a36Sopenharmony_ci !list_empty(&td->cancelled_td_list)) { 220062306a36Sopenharmony_ci xhci_dbg(xhci, "Already resolving halted ep for 0x%llx\n", 220162306a36Sopenharmony_ci (unsigned long long)xhci_trb_virt_to_dma( 220262306a36Sopenharmony_ci td->start_seg, td->first_trb)); 220362306a36Sopenharmony_ci return 0; 220462306a36Sopenharmony_ci } 220562306a36Sopenharmony_ci /* endpoint not halted, don't reset it */ 220662306a36Sopenharmony_ci break; 220762306a36Sopenharmony_ci } 220862306a36Sopenharmony_ci /* Almost same procedure as for STALL_ERROR below */ 220962306a36Sopenharmony_ci xhci_clear_hub_tt_buffer(xhci, td, ep); 221062306a36Sopenharmony_ci xhci_handle_halted_endpoint(xhci, ep, td, EP_HARD_RESET); 221162306a36Sopenharmony_ci return 0; 221262306a36Sopenharmony_ci case COMP_STALL_ERROR: 221362306a36Sopenharmony_ci /* 221462306a36Sopenharmony_ci * xhci internal endpoint state will go to a "halt" state for 221562306a36Sopenharmony_ci * any stall, including default control pipe protocol stall. 221662306a36Sopenharmony_ci * To clear the host side halt we need to issue a reset endpoint 221762306a36Sopenharmony_ci * command, followed by a set dequeue command to move past the 221862306a36Sopenharmony_ci * TD. 221962306a36Sopenharmony_ci * Class drivers clear the device side halt from a functional 222062306a36Sopenharmony_ci * stall later. Hub TT buffer should only be cleared for FS/LS 222162306a36Sopenharmony_ci * devices behind HS hubs for functional stalls. 222262306a36Sopenharmony_ci */ 222362306a36Sopenharmony_ci if (ep->ep_index != 0) 222462306a36Sopenharmony_ci xhci_clear_hub_tt_buffer(xhci, td, ep); 222562306a36Sopenharmony_ci 222662306a36Sopenharmony_ci xhci_handle_halted_endpoint(xhci, ep, td, EP_HARD_RESET); 222762306a36Sopenharmony_ci 222862306a36Sopenharmony_ci return 0; /* xhci_handle_halted_endpoint marked td cancelled */ 222962306a36Sopenharmony_ci default: 223062306a36Sopenharmony_ci break; 223162306a36Sopenharmony_ci } 223262306a36Sopenharmony_ci 223362306a36Sopenharmony_ci /* Update ring dequeue pointer */ 223462306a36Sopenharmony_ci ep_ring->dequeue = td->last_trb; 223562306a36Sopenharmony_ci ep_ring->deq_seg = td->last_trb_seg; 223662306a36Sopenharmony_ci inc_deq(xhci, ep_ring); 223762306a36Sopenharmony_ci 223862306a36Sopenharmony_ci return xhci_td_cleanup(xhci, td, ep_ring, td->status); 223962306a36Sopenharmony_ci} 224062306a36Sopenharmony_ci 224162306a36Sopenharmony_ci/* sum trb lengths from ring dequeue up to stop_trb, _excluding_ stop_trb */ 224262306a36Sopenharmony_cistatic int sum_trb_lengths(struct xhci_hcd *xhci, struct xhci_ring *ring, 224362306a36Sopenharmony_ci union xhci_trb *stop_trb) 224462306a36Sopenharmony_ci{ 224562306a36Sopenharmony_ci u32 sum; 224662306a36Sopenharmony_ci union xhci_trb *trb = ring->dequeue; 224762306a36Sopenharmony_ci struct xhci_segment *seg = ring->deq_seg; 224862306a36Sopenharmony_ci 224962306a36Sopenharmony_ci for (sum = 0; trb != stop_trb; next_trb(xhci, ring, &seg, &trb)) { 225062306a36Sopenharmony_ci if (!trb_is_noop(trb) && !trb_is_link(trb)) 225162306a36Sopenharmony_ci sum += TRB_LEN(le32_to_cpu(trb->generic.field[2])); 225262306a36Sopenharmony_ci } 225362306a36Sopenharmony_ci return sum; 225462306a36Sopenharmony_ci} 225562306a36Sopenharmony_ci 225662306a36Sopenharmony_ci/* 225762306a36Sopenharmony_ci * Process control tds, update urb status and actual_length. 225862306a36Sopenharmony_ci */ 225962306a36Sopenharmony_cistatic int process_ctrl_td(struct xhci_hcd *xhci, struct xhci_virt_ep *ep, 226062306a36Sopenharmony_ci struct xhci_ring *ep_ring, struct xhci_td *td, 226162306a36Sopenharmony_ci union xhci_trb *ep_trb, struct xhci_transfer_event *event) 226262306a36Sopenharmony_ci{ 226362306a36Sopenharmony_ci struct xhci_ep_ctx *ep_ctx; 226462306a36Sopenharmony_ci u32 trb_comp_code; 226562306a36Sopenharmony_ci u32 remaining, requested; 226662306a36Sopenharmony_ci u32 trb_type; 226762306a36Sopenharmony_ci 226862306a36Sopenharmony_ci trb_type = TRB_FIELD_TO_TYPE(le32_to_cpu(ep_trb->generic.field[3])); 226962306a36Sopenharmony_ci ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep->ep_index); 227062306a36Sopenharmony_ci trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len)); 227162306a36Sopenharmony_ci requested = td->urb->transfer_buffer_length; 227262306a36Sopenharmony_ci remaining = EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)); 227362306a36Sopenharmony_ci 227462306a36Sopenharmony_ci switch (trb_comp_code) { 227562306a36Sopenharmony_ci case COMP_SUCCESS: 227662306a36Sopenharmony_ci if (trb_type != TRB_STATUS) { 227762306a36Sopenharmony_ci xhci_warn(xhci, "WARN: Success on ctrl %s TRB without IOC set?\n", 227862306a36Sopenharmony_ci (trb_type == TRB_DATA) ? "data" : "setup"); 227962306a36Sopenharmony_ci td->status = -ESHUTDOWN; 228062306a36Sopenharmony_ci break; 228162306a36Sopenharmony_ci } 228262306a36Sopenharmony_ci td->status = 0; 228362306a36Sopenharmony_ci break; 228462306a36Sopenharmony_ci case COMP_SHORT_PACKET: 228562306a36Sopenharmony_ci td->status = 0; 228662306a36Sopenharmony_ci break; 228762306a36Sopenharmony_ci case COMP_STOPPED_SHORT_PACKET: 228862306a36Sopenharmony_ci if (trb_type == TRB_DATA || trb_type == TRB_NORMAL) 228962306a36Sopenharmony_ci td->urb->actual_length = remaining; 229062306a36Sopenharmony_ci else 229162306a36Sopenharmony_ci xhci_warn(xhci, "WARN: Stopped Short Packet on ctrl setup or status TRB\n"); 229262306a36Sopenharmony_ci goto finish_td; 229362306a36Sopenharmony_ci case COMP_STOPPED: 229462306a36Sopenharmony_ci switch (trb_type) { 229562306a36Sopenharmony_ci case TRB_SETUP: 229662306a36Sopenharmony_ci td->urb->actual_length = 0; 229762306a36Sopenharmony_ci goto finish_td; 229862306a36Sopenharmony_ci case TRB_DATA: 229962306a36Sopenharmony_ci case TRB_NORMAL: 230062306a36Sopenharmony_ci td->urb->actual_length = requested - remaining; 230162306a36Sopenharmony_ci goto finish_td; 230262306a36Sopenharmony_ci case TRB_STATUS: 230362306a36Sopenharmony_ci td->urb->actual_length = requested; 230462306a36Sopenharmony_ci goto finish_td; 230562306a36Sopenharmony_ci default: 230662306a36Sopenharmony_ci xhci_warn(xhci, "WARN: unexpected TRB Type %d\n", 230762306a36Sopenharmony_ci trb_type); 230862306a36Sopenharmony_ci goto finish_td; 230962306a36Sopenharmony_ci } 231062306a36Sopenharmony_ci case COMP_STOPPED_LENGTH_INVALID: 231162306a36Sopenharmony_ci goto finish_td; 231262306a36Sopenharmony_ci default: 231362306a36Sopenharmony_ci if (!xhci_requires_manual_halt_cleanup(xhci, 231462306a36Sopenharmony_ci ep_ctx, trb_comp_code)) 231562306a36Sopenharmony_ci break; 231662306a36Sopenharmony_ci xhci_dbg(xhci, "TRB error %u, halted endpoint index = %u\n", 231762306a36Sopenharmony_ci trb_comp_code, ep->ep_index); 231862306a36Sopenharmony_ci fallthrough; 231962306a36Sopenharmony_ci case COMP_STALL_ERROR: 232062306a36Sopenharmony_ci /* Did we transfer part of the data (middle) phase? */ 232162306a36Sopenharmony_ci if (trb_type == TRB_DATA || trb_type == TRB_NORMAL) 232262306a36Sopenharmony_ci td->urb->actual_length = requested - remaining; 232362306a36Sopenharmony_ci else if (!td->urb_length_set) 232462306a36Sopenharmony_ci td->urb->actual_length = 0; 232562306a36Sopenharmony_ci goto finish_td; 232662306a36Sopenharmony_ci } 232762306a36Sopenharmony_ci 232862306a36Sopenharmony_ci /* stopped at setup stage, no data transferred */ 232962306a36Sopenharmony_ci if (trb_type == TRB_SETUP) 233062306a36Sopenharmony_ci goto finish_td; 233162306a36Sopenharmony_ci 233262306a36Sopenharmony_ci /* 233362306a36Sopenharmony_ci * if on data stage then update the actual_length of the URB and flag it 233462306a36Sopenharmony_ci * as set, so it won't be overwritten in the event for the last TRB. 233562306a36Sopenharmony_ci */ 233662306a36Sopenharmony_ci if (trb_type == TRB_DATA || 233762306a36Sopenharmony_ci trb_type == TRB_NORMAL) { 233862306a36Sopenharmony_ci td->urb_length_set = true; 233962306a36Sopenharmony_ci td->urb->actual_length = requested - remaining; 234062306a36Sopenharmony_ci xhci_dbg(xhci, "Waiting for status stage event\n"); 234162306a36Sopenharmony_ci return 0; 234262306a36Sopenharmony_ci } 234362306a36Sopenharmony_ci 234462306a36Sopenharmony_ci /* at status stage */ 234562306a36Sopenharmony_ci if (!td->urb_length_set) 234662306a36Sopenharmony_ci td->urb->actual_length = requested; 234762306a36Sopenharmony_ci 234862306a36Sopenharmony_cifinish_td: 234962306a36Sopenharmony_ci return finish_td(xhci, ep, ep_ring, td, trb_comp_code); 235062306a36Sopenharmony_ci} 235162306a36Sopenharmony_ci 235262306a36Sopenharmony_ci/* 235362306a36Sopenharmony_ci * Process isochronous tds, update urb packet status and actual_length. 235462306a36Sopenharmony_ci */ 235562306a36Sopenharmony_cistatic int process_isoc_td(struct xhci_hcd *xhci, struct xhci_virt_ep *ep, 235662306a36Sopenharmony_ci struct xhci_ring *ep_ring, struct xhci_td *td, 235762306a36Sopenharmony_ci union xhci_trb *ep_trb, struct xhci_transfer_event *event) 235862306a36Sopenharmony_ci{ 235962306a36Sopenharmony_ci struct urb_priv *urb_priv; 236062306a36Sopenharmony_ci int idx; 236162306a36Sopenharmony_ci struct usb_iso_packet_descriptor *frame; 236262306a36Sopenharmony_ci u32 trb_comp_code; 236362306a36Sopenharmony_ci bool sum_trbs_for_length = false; 236462306a36Sopenharmony_ci u32 remaining, requested, ep_trb_len; 236562306a36Sopenharmony_ci int short_framestatus; 236662306a36Sopenharmony_ci 236762306a36Sopenharmony_ci trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len)); 236862306a36Sopenharmony_ci urb_priv = td->urb->hcpriv; 236962306a36Sopenharmony_ci idx = urb_priv->num_tds_done; 237062306a36Sopenharmony_ci frame = &td->urb->iso_frame_desc[idx]; 237162306a36Sopenharmony_ci requested = frame->length; 237262306a36Sopenharmony_ci remaining = EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)); 237362306a36Sopenharmony_ci ep_trb_len = TRB_LEN(le32_to_cpu(ep_trb->generic.field[2])); 237462306a36Sopenharmony_ci short_framestatus = td->urb->transfer_flags & URB_SHORT_NOT_OK ? 237562306a36Sopenharmony_ci -EREMOTEIO : 0; 237662306a36Sopenharmony_ci 237762306a36Sopenharmony_ci /* handle completion code */ 237862306a36Sopenharmony_ci switch (trb_comp_code) { 237962306a36Sopenharmony_ci case COMP_SUCCESS: 238062306a36Sopenharmony_ci /* Don't overwrite status if TD had an error, see xHCI 4.9.1 */ 238162306a36Sopenharmony_ci if (td->error_mid_td) 238262306a36Sopenharmony_ci break; 238362306a36Sopenharmony_ci if (remaining) { 238462306a36Sopenharmony_ci frame->status = short_framestatus; 238562306a36Sopenharmony_ci if (xhci->quirks & XHCI_TRUST_TX_LENGTH) 238662306a36Sopenharmony_ci sum_trbs_for_length = true; 238762306a36Sopenharmony_ci break; 238862306a36Sopenharmony_ci } 238962306a36Sopenharmony_ci frame->status = 0; 239062306a36Sopenharmony_ci break; 239162306a36Sopenharmony_ci case COMP_SHORT_PACKET: 239262306a36Sopenharmony_ci frame->status = short_framestatus; 239362306a36Sopenharmony_ci sum_trbs_for_length = true; 239462306a36Sopenharmony_ci break; 239562306a36Sopenharmony_ci case COMP_BANDWIDTH_OVERRUN_ERROR: 239662306a36Sopenharmony_ci frame->status = -ECOMM; 239762306a36Sopenharmony_ci break; 239862306a36Sopenharmony_ci case COMP_BABBLE_DETECTED_ERROR: 239962306a36Sopenharmony_ci sum_trbs_for_length = true; 240062306a36Sopenharmony_ci fallthrough; 240162306a36Sopenharmony_ci case COMP_ISOCH_BUFFER_OVERRUN: 240262306a36Sopenharmony_ci frame->status = -EOVERFLOW; 240362306a36Sopenharmony_ci if (ep_trb != td->last_trb) 240462306a36Sopenharmony_ci td->error_mid_td = true; 240562306a36Sopenharmony_ci break; 240662306a36Sopenharmony_ci case COMP_INCOMPATIBLE_DEVICE_ERROR: 240762306a36Sopenharmony_ci case COMP_STALL_ERROR: 240862306a36Sopenharmony_ci frame->status = -EPROTO; 240962306a36Sopenharmony_ci break; 241062306a36Sopenharmony_ci case COMP_USB_TRANSACTION_ERROR: 241162306a36Sopenharmony_ci frame->status = -EPROTO; 241262306a36Sopenharmony_ci sum_trbs_for_length = true; 241362306a36Sopenharmony_ci if (ep_trb != td->last_trb) 241462306a36Sopenharmony_ci td->error_mid_td = true; 241562306a36Sopenharmony_ci break; 241662306a36Sopenharmony_ci case COMP_STOPPED: 241762306a36Sopenharmony_ci sum_trbs_for_length = true; 241862306a36Sopenharmony_ci break; 241962306a36Sopenharmony_ci case COMP_STOPPED_SHORT_PACKET: 242062306a36Sopenharmony_ci /* field normally containing residue now contains tranferred */ 242162306a36Sopenharmony_ci frame->status = short_framestatus; 242262306a36Sopenharmony_ci requested = remaining; 242362306a36Sopenharmony_ci break; 242462306a36Sopenharmony_ci case COMP_STOPPED_LENGTH_INVALID: 242562306a36Sopenharmony_ci requested = 0; 242662306a36Sopenharmony_ci remaining = 0; 242762306a36Sopenharmony_ci break; 242862306a36Sopenharmony_ci default: 242962306a36Sopenharmony_ci sum_trbs_for_length = true; 243062306a36Sopenharmony_ci frame->status = -1; 243162306a36Sopenharmony_ci break; 243262306a36Sopenharmony_ci } 243362306a36Sopenharmony_ci 243462306a36Sopenharmony_ci if (td->urb_length_set) 243562306a36Sopenharmony_ci goto finish_td; 243662306a36Sopenharmony_ci 243762306a36Sopenharmony_ci if (sum_trbs_for_length) 243862306a36Sopenharmony_ci frame->actual_length = sum_trb_lengths(xhci, ep->ring, ep_trb) + 243962306a36Sopenharmony_ci ep_trb_len - remaining; 244062306a36Sopenharmony_ci else 244162306a36Sopenharmony_ci frame->actual_length = requested; 244262306a36Sopenharmony_ci 244362306a36Sopenharmony_ci td->urb->actual_length += frame->actual_length; 244462306a36Sopenharmony_ci 244562306a36Sopenharmony_cifinish_td: 244662306a36Sopenharmony_ci /* Don't give back TD yet if we encountered an error mid TD */ 244762306a36Sopenharmony_ci if (td->error_mid_td && ep_trb != td->last_trb) { 244862306a36Sopenharmony_ci xhci_dbg(xhci, "Error mid isoc TD, wait for final completion event\n"); 244962306a36Sopenharmony_ci td->urb_length_set = true; 245062306a36Sopenharmony_ci return 0; 245162306a36Sopenharmony_ci } 245262306a36Sopenharmony_ci 245362306a36Sopenharmony_ci return finish_td(xhci, ep, ep_ring, td, trb_comp_code); 245462306a36Sopenharmony_ci} 245562306a36Sopenharmony_ci 245662306a36Sopenharmony_cistatic int skip_isoc_td(struct xhci_hcd *xhci, struct xhci_td *td, 245762306a36Sopenharmony_ci struct xhci_virt_ep *ep, int status) 245862306a36Sopenharmony_ci{ 245962306a36Sopenharmony_ci struct urb_priv *urb_priv; 246062306a36Sopenharmony_ci struct usb_iso_packet_descriptor *frame; 246162306a36Sopenharmony_ci int idx; 246262306a36Sopenharmony_ci 246362306a36Sopenharmony_ci urb_priv = td->urb->hcpriv; 246462306a36Sopenharmony_ci idx = urb_priv->num_tds_done; 246562306a36Sopenharmony_ci frame = &td->urb->iso_frame_desc[idx]; 246662306a36Sopenharmony_ci 246762306a36Sopenharmony_ci /* The transfer is partly done. */ 246862306a36Sopenharmony_ci frame->status = -EXDEV; 246962306a36Sopenharmony_ci 247062306a36Sopenharmony_ci /* calc actual length */ 247162306a36Sopenharmony_ci frame->actual_length = 0; 247262306a36Sopenharmony_ci 247362306a36Sopenharmony_ci /* Update ring dequeue pointer */ 247462306a36Sopenharmony_ci ep->ring->dequeue = td->last_trb; 247562306a36Sopenharmony_ci ep->ring->deq_seg = td->last_trb_seg; 247662306a36Sopenharmony_ci inc_deq(xhci, ep->ring); 247762306a36Sopenharmony_ci 247862306a36Sopenharmony_ci return xhci_td_cleanup(xhci, td, ep->ring, status); 247962306a36Sopenharmony_ci} 248062306a36Sopenharmony_ci 248162306a36Sopenharmony_ci/* 248262306a36Sopenharmony_ci * Process bulk and interrupt tds, update urb status and actual_length. 248362306a36Sopenharmony_ci */ 248462306a36Sopenharmony_cistatic int process_bulk_intr_td(struct xhci_hcd *xhci, struct xhci_virt_ep *ep, 248562306a36Sopenharmony_ci struct xhci_ring *ep_ring, struct xhci_td *td, 248662306a36Sopenharmony_ci union xhci_trb *ep_trb, struct xhci_transfer_event *event) 248762306a36Sopenharmony_ci{ 248862306a36Sopenharmony_ci struct xhci_slot_ctx *slot_ctx; 248962306a36Sopenharmony_ci u32 trb_comp_code; 249062306a36Sopenharmony_ci u32 remaining, requested, ep_trb_len; 249162306a36Sopenharmony_ci 249262306a36Sopenharmony_ci slot_ctx = xhci_get_slot_ctx(xhci, ep->vdev->out_ctx); 249362306a36Sopenharmony_ci trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len)); 249462306a36Sopenharmony_ci remaining = EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)); 249562306a36Sopenharmony_ci ep_trb_len = TRB_LEN(le32_to_cpu(ep_trb->generic.field[2])); 249662306a36Sopenharmony_ci requested = td->urb->transfer_buffer_length; 249762306a36Sopenharmony_ci 249862306a36Sopenharmony_ci switch (trb_comp_code) { 249962306a36Sopenharmony_ci case COMP_SUCCESS: 250062306a36Sopenharmony_ci ep->err_count = 0; 250162306a36Sopenharmony_ci /* handle success with untransferred data as short packet */ 250262306a36Sopenharmony_ci if (ep_trb != td->last_trb || remaining) { 250362306a36Sopenharmony_ci xhci_warn(xhci, "WARN Successful completion on short TX\n"); 250462306a36Sopenharmony_ci xhci_dbg(xhci, "ep %#x - asked for %d bytes, %d bytes untransferred\n", 250562306a36Sopenharmony_ci td->urb->ep->desc.bEndpointAddress, 250662306a36Sopenharmony_ci requested, remaining); 250762306a36Sopenharmony_ci } 250862306a36Sopenharmony_ci td->status = 0; 250962306a36Sopenharmony_ci break; 251062306a36Sopenharmony_ci case COMP_SHORT_PACKET: 251162306a36Sopenharmony_ci xhci_dbg(xhci, "ep %#x - asked for %d bytes, %d bytes untransferred\n", 251262306a36Sopenharmony_ci td->urb->ep->desc.bEndpointAddress, 251362306a36Sopenharmony_ci requested, remaining); 251462306a36Sopenharmony_ci td->status = 0; 251562306a36Sopenharmony_ci break; 251662306a36Sopenharmony_ci case COMP_STOPPED_SHORT_PACKET: 251762306a36Sopenharmony_ci td->urb->actual_length = remaining; 251862306a36Sopenharmony_ci goto finish_td; 251962306a36Sopenharmony_ci case COMP_STOPPED_LENGTH_INVALID: 252062306a36Sopenharmony_ci /* stopped on ep trb with invalid length, exclude it */ 252162306a36Sopenharmony_ci ep_trb_len = 0; 252262306a36Sopenharmony_ci remaining = 0; 252362306a36Sopenharmony_ci break; 252462306a36Sopenharmony_ci case COMP_USB_TRANSACTION_ERROR: 252562306a36Sopenharmony_ci if (xhci->quirks & XHCI_NO_SOFT_RETRY || 252662306a36Sopenharmony_ci (ep->err_count++ > MAX_SOFT_RETRY) || 252762306a36Sopenharmony_ci le32_to_cpu(slot_ctx->tt_info) & TT_SLOT) 252862306a36Sopenharmony_ci break; 252962306a36Sopenharmony_ci 253062306a36Sopenharmony_ci td->status = 0; 253162306a36Sopenharmony_ci 253262306a36Sopenharmony_ci xhci_handle_halted_endpoint(xhci, ep, td, EP_SOFT_RESET); 253362306a36Sopenharmony_ci return 0; 253462306a36Sopenharmony_ci default: 253562306a36Sopenharmony_ci /* do nothing */ 253662306a36Sopenharmony_ci break; 253762306a36Sopenharmony_ci } 253862306a36Sopenharmony_ci 253962306a36Sopenharmony_ci if (ep_trb == td->last_trb) 254062306a36Sopenharmony_ci td->urb->actual_length = requested - remaining; 254162306a36Sopenharmony_ci else 254262306a36Sopenharmony_ci td->urb->actual_length = 254362306a36Sopenharmony_ci sum_trb_lengths(xhci, ep_ring, ep_trb) + 254462306a36Sopenharmony_ci ep_trb_len - remaining; 254562306a36Sopenharmony_cifinish_td: 254662306a36Sopenharmony_ci if (remaining > requested) { 254762306a36Sopenharmony_ci xhci_warn(xhci, "bad transfer trb length %d in event trb\n", 254862306a36Sopenharmony_ci remaining); 254962306a36Sopenharmony_ci td->urb->actual_length = 0; 255062306a36Sopenharmony_ci } 255162306a36Sopenharmony_ci 255262306a36Sopenharmony_ci return finish_td(xhci, ep, ep_ring, td, trb_comp_code); 255362306a36Sopenharmony_ci} 255462306a36Sopenharmony_ci 255562306a36Sopenharmony_ci/* 255662306a36Sopenharmony_ci * If this function returns an error condition, it means it got a Transfer 255762306a36Sopenharmony_ci * event with a corrupted Slot ID, Endpoint ID, or TRB DMA address. 255862306a36Sopenharmony_ci * At this point, the host controller is probably hosed and should be reset. 255962306a36Sopenharmony_ci */ 256062306a36Sopenharmony_cistatic int handle_tx_event(struct xhci_hcd *xhci, 256162306a36Sopenharmony_ci struct xhci_interrupter *ir, 256262306a36Sopenharmony_ci struct xhci_transfer_event *event) 256362306a36Sopenharmony_ci{ 256462306a36Sopenharmony_ci struct xhci_virt_ep *ep; 256562306a36Sopenharmony_ci struct xhci_ring *ep_ring; 256662306a36Sopenharmony_ci unsigned int slot_id; 256762306a36Sopenharmony_ci int ep_index; 256862306a36Sopenharmony_ci struct xhci_td *td = NULL; 256962306a36Sopenharmony_ci dma_addr_t ep_trb_dma; 257062306a36Sopenharmony_ci struct xhci_segment *ep_seg; 257162306a36Sopenharmony_ci union xhci_trb *ep_trb; 257262306a36Sopenharmony_ci int status = -EINPROGRESS; 257362306a36Sopenharmony_ci struct xhci_ep_ctx *ep_ctx; 257462306a36Sopenharmony_ci u32 trb_comp_code; 257562306a36Sopenharmony_ci int td_num = 0; 257662306a36Sopenharmony_ci bool handling_skipped_tds = false; 257762306a36Sopenharmony_ci 257862306a36Sopenharmony_ci slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags)); 257962306a36Sopenharmony_ci ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1; 258062306a36Sopenharmony_ci trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len)); 258162306a36Sopenharmony_ci ep_trb_dma = le64_to_cpu(event->buffer); 258262306a36Sopenharmony_ci 258362306a36Sopenharmony_ci ep = xhci_get_virt_ep(xhci, slot_id, ep_index); 258462306a36Sopenharmony_ci if (!ep) { 258562306a36Sopenharmony_ci xhci_err(xhci, "ERROR Invalid Transfer event\n"); 258662306a36Sopenharmony_ci goto err_out; 258762306a36Sopenharmony_ci } 258862306a36Sopenharmony_ci 258962306a36Sopenharmony_ci ep_ring = xhci_dma_to_transfer_ring(ep, ep_trb_dma); 259062306a36Sopenharmony_ci ep_ctx = xhci_get_ep_ctx(xhci, ep->vdev->out_ctx, ep_index); 259162306a36Sopenharmony_ci 259262306a36Sopenharmony_ci if (GET_EP_CTX_STATE(ep_ctx) == EP_STATE_DISABLED) { 259362306a36Sopenharmony_ci xhci_err(xhci, 259462306a36Sopenharmony_ci "ERROR Transfer event for disabled endpoint slot %u ep %u\n", 259562306a36Sopenharmony_ci slot_id, ep_index); 259662306a36Sopenharmony_ci goto err_out; 259762306a36Sopenharmony_ci } 259862306a36Sopenharmony_ci 259962306a36Sopenharmony_ci /* Some transfer events don't always point to a trb, see xhci 4.17.4 */ 260062306a36Sopenharmony_ci if (!ep_ring) { 260162306a36Sopenharmony_ci switch (trb_comp_code) { 260262306a36Sopenharmony_ci case COMP_STALL_ERROR: 260362306a36Sopenharmony_ci case COMP_USB_TRANSACTION_ERROR: 260462306a36Sopenharmony_ci case COMP_INVALID_STREAM_TYPE_ERROR: 260562306a36Sopenharmony_ci case COMP_INVALID_STREAM_ID_ERROR: 260662306a36Sopenharmony_ci xhci_dbg(xhci, "Stream transaction error ep %u no id\n", 260762306a36Sopenharmony_ci ep_index); 260862306a36Sopenharmony_ci if (ep->err_count++ > MAX_SOFT_RETRY) 260962306a36Sopenharmony_ci xhci_handle_halted_endpoint(xhci, ep, NULL, 261062306a36Sopenharmony_ci EP_HARD_RESET); 261162306a36Sopenharmony_ci else 261262306a36Sopenharmony_ci xhci_handle_halted_endpoint(xhci, ep, NULL, 261362306a36Sopenharmony_ci EP_SOFT_RESET); 261462306a36Sopenharmony_ci goto cleanup; 261562306a36Sopenharmony_ci case COMP_RING_UNDERRUN: 261662306a36Sopenharmony_ci case COMP_RING_OVERRUN: 261762306a36Sopenharmony_ci case COMP_STOPPED_LENGTH_INVALID: 261862306a36Sopenharmony_ci goto cleanup; 261962306a36Sopenharmony_ci default: 262062306a36Sopenharmony_ci xhci_err(xhci, "ERROR Transfer event for unknown stream ring slot %u ep %u\n", 262162306a36Sopenharmony_ci slot_id, ep_index); 262262306a36Sopenharmony_ci goto err_out; 262362306a36Sopenharmony_ci } 262462306a36Sopenharmony_ci } 262562306a36Sopenharmony_ci 262662306a36Sopenharmony_ci /* Count current td numbers if ep->skip is set */ 262762306a36Sopenharmony_ci if (ep->skip) 262862306a36Sopenharmony_ci td_num += list_count_nodes(&ep_ring->td_list); 262962306a36Sopenharmony_ci 263062306a36Sopenharmony_ci /* Look for common error cases */ 263162306a36Sopenharmony_ci switch (trb_comp_code) { 263262306a36Sopenharmony_ci /* Skip codes that require special handling depending on 263362306a36Sopenharmony_ci * transfer type 263462306a36Sopenharmony_ci */ 263562306a36Sopenharmony_ci case COMP_SUCCESS: 263662306a36Sopenharmony_ci if (EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)) == 0) 263762306a36Sopenharmony_ci break; 263862306a36Sopenharmony_ci if (xhci->quirks & XHCI_TRUST_TX_LENGTH || 263962306a36Sopenharmony_ci ep_ring->last_td_was_short) 264062306a36Sopenharmony_ci trb_comp_code = COMP_SHORT_PACKET; 264162306a36Sopenharmony_ci else 264262306a36Sopenharmony_ci xhci_warn_ratelimited(xhci, 264362306a36Sopenharmony_ci "WARN Successful completion on short TX for slot %u ep %u: needs XHCI_TRUST_TX_LENGTH quirk?\n", 264462306a36Sopenharmony_ci slot_id, ep_index); 264562306a36Sopenharmony_ci break; 264662306a36Sopenharmony_ci case COMP_SHORT_PACKET: 264762306a36Sopenharmony_ci break; 264862306a36Sopenharmony_ci /* Completion codes for endpoint stopped state */ 264962306a36Sopenharmony_ci case COMP_STOPPED: 265062306a36Sopenharmony_ci xhci_dbg(xhci, "Stopped on Transfer TRB for slot %u ep %u\n", 265162306a36Sopenharmony_ci slot_id, ep_index); 265262306a36Sopenharmony_ci break; 265362306a36Sopenharmony_ci case COMP_STOPPED_LENGTH_INVALID: 265462306a36Sopenharmony_ci xhci_dbg(xhci, 265562306a36Sopenharmony_ci "Stopped on No-op or Link TRB for slot %u ep %u\n", 265662306a36Sopenharmony_ci slot_id, ep_index); 265762306a36Sopenharmony_ci break; 265862306a36Sopenharmony_ci case COMP_STOPPED_SHORT_PACKET: 265962306a36Sopenharmony_ci xhci_dbg(xhci, 266062306a36Sopenharmony_ci "Stopped with short packet transfer detected for slot %u ep %u\n", 266162306a36Sopenharmony_ci slot_id, ep_index); 266262306a36Sopenharmony_ci break; 266362306a36Sopenharmony_ci /* Completion codes for endpoint halted state */ 266462306a36Sopenharmony_ci case COMP_STALL_ERROR: 266562306a36Sopenharmony_ci xhci_dbg(xhci, "Stalled endpoint for slot %u ep %u\n", slot_id, 266662306a36Sopenharmony_ci ep_index); 266762306a36Sopenharmony_ci status = -EPIPE; 266862306a36Sopenharmony_ci break; 266962306a36Sopenharmony_ci case COMP_SPLIT_TRANSACTION_ERROR: 267062306a36Sopenharmony_ci xhci_dbg(xhci, "Split transaction error for slot %u ep %u\n", 267162306a36Sopenharmony_ci slot_id, ep_index); 267262306a36Sopenharmony_ci status = -EPROTO; 267362306a36Sopenharmony_ci break; 267462306a36Sopenharmony_ci case COMP_USB_TRANSACTION_ERROR: 267562306a36Sopenharmony_ci xhci_dbg(xhci, "Transfer error for slot %u ep %u on endpoint\n", 267662306a36Sopenharmony_ci slot_id, ep_index); 267762306a36Sopenharmony_ci status = -EPROTO; 267862306a36Sopenharmony_ci break; 267962306a36Sopenharmony_ci case COMP_BABBLE_DETECTED_ERROR: 268062306a36Sopenharmony_ci xhci_dbg(xhci, "Babble error for slot %u ep %u on endpoint\n", 268162306a36Sopenharmony_ci slot_id, ep_index); 268262306a36Sopenharmony_ci status = -EOVERFLOW; 268362306a36Sopenharmony_ci break; 268462306a36Sopenharmony_ci /* Completion codes for endpoint error state */ 268562306a36Sopenharmony_ci case COMP_TRB_ERROR: 268662306a36Sopenharmony_ci xhci_warn(xhci, 268762306a36Sopenharmony_ci "WARN: TRB error for slot %u ep %u on endpoint\n", 268862306a36Sopenharmony_ci slot_id, ep_index); 268962306a36Sopenharmony_ci status = -EILSEQ; 269062306a36Sopenharmony_ci break; 269162306a36Sopenharmony_ci /* completion codes not indicating endpoint state change */ 269262306a36Sopenharmony_ci case COMP_DATA_BUFFER_ERROR: 269362306a36Sopenharmony_ci xhci_warn(xhci, 269462306a36Sopenharmony_ci "WARN: HC couldn't access mem fast enough for slot %u ep %u\n", 269562306a36Sopenharmony_ci slot_id, ep_index); 269662306a36Sopenharmony_ci status = -ENOSR; 269762306a36Sopenharmony_ci break; 269862306a36Sopenharmony_ci case COMP_BANDWIDTH_OVERRUN_ERROR: 269962306a36Sopenharmony_ci xhci_warn(xhci, 270062306a36Sopenharmony_ci "WARN: bandwidth overrun event for slot %u ep %u on endpoint\n", 270162306a36Sopenharmony_ci slot_id, ep_index); 270262306a36Sopenharmony_ci break; 270362306a36Sopenharmony_ci case COMP_ISOCH_BUFFER_OVERRUN: 270462306a36Sopenharmony_ci xhci_warn(xhci, 270562306a36Sopenharmony_ci "WARN: buffer overrun event for slot %u ep %u on endpoint", 270662306a36Sopenharmony_ci slot_id, ep_index); 270762306a36Sopenharmony_ci break; 270862306a36Sopenharmony_ci case COMP_RING_UNDERRUN: 270962306a36Sopenharmony_ci /* 271062306a36Sopenharmony_ci * When the Isoch ring is empty, the xHC will generate 271162306a36Sopenharmony_ci * a Ring Overrun Event for IN Isoch endpoint or Ring 271262306a36Sopenharmony_ci * Underrun Event for OUT Isoch endpoint. 271362306a36Sopenharmony_ci */ 271462306a36Sopenharmony_ci xhci_dbg(xhci, "underrun event on endpoint\n"); 271562306a36Sopenharmony_ci if (!list_empty(&ep_ring->td_list)) 271662306a36Sopenharmony_ci xhci_dbg(xhci, "Underrun Event for slot %d ep %d " 271762306a36Sopenharmony_ci "still with TDs queued?\n", 271862306a36Sopenharmony_ci TRB_TO_SLOT_ID(le32_to_cpu(event->flags)), 271962306a36Sopenharmony_ci ep_index); 272062306a36Sopenharmony_ci goto cleanup; 272162306a36Sopenharmony_ci case COMP_RING_OVERRUN: 272262306a36Sopenharmony_ci xhci_dbg(xhci, "overrun event on endpoint\n"); 272362306a36Sopenharmony_ci if (!list_empty(&ep_ring->td_list)) 272462306a36Sopenharmony_ci xhci_dbg(xhci, "Overrun Event for slot %d ep %d " 272562306a36Sopenharmony_ci "still with TDs queued?\n", 272662306a36Sopenharmony_ci TRB_TO_SLOT_ID(le32_to_cpu(event->flags)), 272762306a36Sopenharmony_ci ep_index); 272862306a36Sopenharmony_ci goto cleanup; 272962306a36Sopenharmony_ci case COMP_MISSED_SERVICE_ERROR: 273062306a36Sopenharmony_ci /* 273162306a36Sopenharmony_ci * When encounter missed service error, one or more isoc tds 273262306a36Sopenharmony_ci * may be missed by xHC. 273362306a36Sopenharmony_ci * Set skip flag of the ep_ring; Complete the missed tds as 273462306a36Sopenharmony_ci * short transfer when process the ep_ring next time. 273562306a36Sopenharmony_ci */ 273662306a36Sopenharmony_ci ep->skip = true; 273762306a36Sopenharmony_ci xhci_dbg(xhci, 273862306a36Sopenharmony_ci "Miss service interval error for slot %u ep %u, set skip flag\n", 273962306a36Sopenharmony_ci slot_id, ep_index); 274062306a36Sopenharmony_ci goto cleanup; 274162306a36Sopenharmony_ci case COMP_NO_PING_RESPONSE_ERROR: 274262306a36Sopenharmony_ci ep->skip = true; 274362306a36Sopenharmony_ci xhci_dbg(xhci, 274462306a36Sopenharmony_ci "No Ping response error for slot %u ep %u, Skip one Isoc TD\n", 274562306a36Sopenharmony_ci slot_id, ep_index); 274662306a36Sopenharmony_ci goto cleanup; 274762306a36Sopenharmony_ci 274862306a36Sopenharmony_ci case COMP_INCOMPATIBLE_DEVICE_ERROR: 274962306a36Sopenharmony_ci /* needs disable slot command to recover */ 275062306a36Sopenharmony_ci xhci_warn(xhci, 275162306a36Sopenharmony_ci "WARN: detect an incompatible device for slot %u ep %u", 275262306a36Sopenharmony_ci slot_id, ep_index); 275362306a36Sopenharmony_ci status = -EPROTO; 275462306a36Sopenharmony_ci break; 275562306a36Sopenharmony_ci default: 275662306a36Sopenharmony_ci if (xhci_is_vendor_info_code(xhci, trb_comp_code)) { 275762306a36Sopenharmony_ci status = 0; 275862306a36Sopenharmony_ci break; 275962306a36Sopenharmony_ci } 276062306a36Sopenharmony_ci xhci_warn(xhci, 276162306a36Sopenharmony_ci "ERROR Unknown event condition %u for slot %u ep %u , HC probably busted\n", 276262306a36Sopenharmony_ci trb_comp_code, slot_id, ep_index); 276362306a36Sopenharmony_ci goto cleanup; 276462306a36Sopenharmony_ci } 276562306a36Sopenharmony_ci 276662306a36Sopenharmony_ci do { 276762306a36Sopenharmony_ci /* This TRB should be in the TD at the head of this ring's 276862306a36Sopenharmony_ci * TD list. 276962306a36Sopenharmony_ci */ 277062306a36Sopenharmony_ci if (list_empty(&ep_ring->td_list)) { 277162306a36Sopenharmony_ci /* 277262306a36Sopenharmony_ci * Don't print wanings if it's due to a stopped endpoint 277362306a36Sopenharmony_ci * generating an extra completion event if the device 277462306a36Sopenharmony_ci * was suspended. Or, a event for the last TRB of a 277562306a36Sopenharmony_ci * short TD we already got a short event for. 277662306a36Sopenharmony_ci * The short TD is already removed from the TD list. 277762306a36Sopenharmony_ci */ 277862306a36Sopenharmony_ci 277962306a36Sopenharmony_ci if (!(trb_comp_code == COMP_STOPPED || 278062306a36Sopenharmony_ci trb_comp_code == COMP_STOPPED_LENGTH_INVALID || 278162306a36Sopenharmony_ci ep_ring->last_td_was_short)) { 278262306a36Sopenharmony_ci xhci_warn(xhci, "WARN Event TRB for slot %d ep %d with no TDs queued?\n", 278362306a36Sopenharmony_ci TRB_TO_SLOT_ID(le32_to_cpu(event->flags)), 278462306a36Sopenharmony_ci ep_index); 278562306a36Sopenharmony_ci } 278662306a36Sopenharmony_ci if (ep->skip) { 278762306a36Sopenharmony_ci ep->skip = false; 278862306a36Sopenharmony_ci xhci_dbg(xhci, "td_list is empty while skip flag set. Clear skip flag for slot %u ep %u.\n", 278962306a36Sopenharmony_ci slot_id, ep_index); 279062306a36Sopenharmony_ci } 279162306a36Sopenharmony_ci if (trb_comp_code == COMP_STALL_ERROR || 279262306a36Sopenharmony_ci xhci_requires_manual_halt_cleanup(xhci, ep_ctx, 279362306a36Sopenharmony_ci trb_comp_code)) { 279462306a36Sopenharmony_ci xhci_handle_halted_endpoint(xhci, ep, NULL, 279562306a36Sopenharmony_ci EP_HARD_RESET); 279662306a36Sopenharmony_ci } 279762306a36Sopenharmony_ci goto cleanup; 279862306a36Sopenharmony_ci } 279962306a36Sopenharmony_ci 280062306a36Sopenharmony_ci /* We've skipped all the TDs on the ep ring when ep->skip set */ 280162306a36Sopenharmony_ci if (ep->skip && td_num == 0) { 280262306a36Sopenharmony_ci ep->skip = false; 280362306a36Sopenharmony_ci xhci_dbg(xhci, "All tds on the ep_ring skipped. Clear skip flag for slot %u ep %u.\n", 280462306a36Sopenharmony_ci slot_id, ep_index); 280562306a36Sopenharmony_ci goto cleanup; 280662306a36Sopenharmony_ci } 280762306a36Sopenharmony_ci 280862306a36Sopenharmony_ci td = list_first_entry(&ep_ring->td_list, struct xhci_td, 280962306a36Sopenharmony_ci td_list); 281062306a36Sopenharmony_ci if (ep->skip) 281162306a36Sopenharmony_ci td_num--; 281262306a36Sopenharmony_ci 281362306a36Sopenharmony_ci /* Is this a TRB in the currently executing TD? */ 281462306a36Sopenharmony_ci ep_seg = trb_in_td(xhci, ep_ring->deq_seg, ep_ring->dequeue, 281562306a36Sopenharmony_ci td->last_trb, ep_trb_dma, false); 281662306a36Sopenharmony_ci 281762306a36Sopenharmony_ci /* 281862306a36Sopenharmony_ci * Skip the Force Stopped Event. The event_trb(event_dma) of FSE 281962306a36Sopenharmony_ci * is not in the current TD pointed by ep_ring->dequeue because 282062306a36Sopenharmony_ci * that the hardware dequeue pointer still at the previous TRB 282162306a36Sopenharmony_ci * of the current TD. The previous TRB maybe a Link TD or the 282262306a36Sopenharmony_ci * last TRB of the previous TD. The command completion handle 282362306a36Sopenharmony_ci * will take care the rest. 282462306a36Sopenharmony_ci */ 282562306a36Sopenharmony_ci if (!ep_seg && (trb_comp_code == COMP_STOPPED || 282662306a36Sopenharmony_ci trb_comp_code == COMP_STOPPED_LENGTH_INVALID)) { 282762306a36Sopenharmony_ci goto cleanup; 282862306a36Sopenharmony_ci } 282962306a36Sopenharmony_ci 283062306a36Sopenharmony_ci if (!ep_seg) { 283162306a36Sopenharmony_ci 283262306a36Sopenharmony_ci if (ep->skip && usb_endpoint_xfer_isoc(&td->urb->ep->desc)) { 283362306a36Sopenharmony_ci skip_isoc_td(xhci, td, ep, status); 283462306a36Sopenharmony_ci goto cleanup; 283562306a36Sopenharmony_ci } 283662306a36Sopenharmony_ci 283762306a36Sopenharmony_ci /* 283862306a36Sopenharmony_ci * Some hosts give a spurious success event after a short 283962306a36Sopenharmony_ci * transfer. Ignore it. 284062306a36Sopenharmony_ci */ 284162306a36Sopenharmony_ci if ((xhci->quirks & XHCI_SPURIOUS_SUCCESS) && 284262306a36Sopenharmony_ci ep_ring->last_td_was_short) { 284362306a36Sopenharmony_ci ep_ring->last_td_was_short = false; 284462306a36Sopenharmony_ci goto cleanup; 284562306a36Sopenharmony_ci } 284662306a36Sopenharmony_ci 284762306a36Sopenharmony_ci /* 284862306a36Sopenharmony_ci * xhci 4.10.2 states isoc endpoints should continue 284962306a36Sopenharmony_ci * processing the next TD if there was an error mid TD. 285062306a36Sopenharmony_ci * So host like NEC don't generate an event for the last 285162306a36Sopenharmony_ci * isoc TRB even if the IOC flag is set. 285262306a36Sopenharmony_ci * xhci 4.9.1 states that if there are errors in mult-TRB 285362306a36Sopenharmony_ci * TDs xHC should generate an error for that TRB, and if xHC 285462306a36Sopenharmony_ci * proceeds to the next TD it should genete an event for 285562306a36Sopenharmony_ci * any TRB with IOC flag on the way. Other host follow this. 285662306a36Sopenharmony_ci * So this event might be for the next TD. 285762306a36Sopenharmony_ci */ 285862306a36Sopenharmony_ci if (td->error_mid_td && 285962306a36Sopenharmony_ci !list_is_last(&td->td_list, &ep_ring->td_list)) { 286062306a36Sopenharmony_ci struct xhci_td *td_next = list_next_entry(td, td_list); 286162306a36Sopenharmony_ci 286262306a36Sopenharmony_ci ep_seg = trb_in_td(xhci, td_next->start_seg, td_next->first_trb, 286362306a36Sopenharmony_ci td_next->last_trb, ep_trb_dma, false); 286462306a36Sopenharmony_ci if (ep_seg) { 286562306a36Sopenharmony_ci /* give back previous TD, start handling new */ 286662306a36Sopenharmony_ci xhci_dbg(xhci, "Missing TD completion event after mid TD error\n"); 286762306a36Sopenharmony_ci ep_ring->dequeue = td->last_trb; 286862306a36Sopenharmony_ci ep_ring->deq_seg = td->last_trb_seg; 286962306a36Sopenharmony_ci inc_deq(xhci, ep_ring); 287062306a36Sopenharmony_ci xhci_td_cleanup(xhci, td, ep_ring, td->status); 287162306a36Sopenharmony_ci td = td_next; 287262306a36Sopenharmony_ci } 287362306a36Sopenharmony_ci } 287462306a36Sopenharmony_ci 287562306a36Sopenharmony_ci if (!ep_seg) { 287662306a36Sopenharmony_ci /* HC is busted, give up! */ 287762306a36Sopenharmony_ci xhci_err(xhci, 287862306a36Sopenharmony_ci "ERROR Transfer event TRB DMA ptr not " 287962306a36Sopenharmony_ci "part of current TD ep_index %d " 288062306a36Sopenharmony_ci "comp_code %u\n", ep_index, 288162306a36Sopenharmony_ci trb_comp_code); 288262306a36Sopenharmony_ci trb_in_td(xhci, ep_ring->deq_seg, 288362306a36Sopenharmony_ci ep_ring->dequeue, td->last_trb, 288462306a36Sopenharmony_ci ep_trb_dma, true); 288562306a36Sopenharmony_ci return -ESHUTDOWN; 288662306a36Sopenharmony_ci } 288762306a36Sopenharmony_ci } 288862306a36Sopenharmony_ci if (trb_comp_code == COMP_SHORT_PACKET) 288962306a36Sopenharmony_ci ep_ring->last_td_was_short = true; 289062306a36Sopenharmony_ci else 289162306a36Sopenharmony_ci ep_ring->last_td_was_short = false; 289262306a36Sopenharmony_ci 289362306a36Sopenharmony_ci if (ep->skip) { 289462306a36Sopenharmony_ci xhci_dbg(xhci, 289562306a36Sopenharmony_ci "Found td. Clear skip flag for slot %u ep %u.\n", 289662306a36Sopenharmony_ci slot_id, ep_index); 289762306a36Sopenharmony_ci ep->skip = false; 289862306a36Sopenharmony_ci } 289962306a36Sopenharmony_ci 290062306a36Sopenharmony_ci ep_trb = &ep_seg->trbs[(ep_trb_dma - ep_seg->dma) / 290162306a36Sopenharmony_ci sizeof(*ep_trb)]; 290262306a36Sopenharmony_ci 290362306a36Sopenharmony_ci trace_xhci_handle_transfer(ep_ring, 290462306a36Sopenharmony_ci (struct xhci_generic_trb *) ep_trb); 290562306a36Sopenharmony_ci 290662306a36Sopenharmony_ci /* 290762306a36Sopenharmony_ci * No-op TRB could trigger interrupts in a case where 290862306a36Sopenharmony_ci * a URB was killed and a STALL_ERROR happens right 290962306a36Sopenharmony_ci * after the endpoint ring stopped. Reset the halted 291062306a36Sopenharmony_ci * endpoint. Otherwise, the endpoint remains stalled 291162306a36Sopenharmony_ci * indefinitely. 291262306a36Sopenharmony_ci */ 291362306a36Sopenharmony_ci 291462306a36Sopenharmony_ci if (trb_is_noop(ep_trb)) { 291562306a36Sopenharmony_ci if (trb_comp_code == COMP_STALL_ERROR || 291662306a36Sopenharmony_ci xhci_requires_manual_halt_cleanup(xhci, ep_ctx, 291762306a36Sopenharmony_ci trb_comp_code)) 291862306a36Sopenharmony_ci xhci_handle_halted_endpoint(xhci, ep, td, 291962306a36Sopenharmony_ci EP_HARD_RESET); 292062306a36Sopenharmony_ci goto cleanup; 292162306a36Sopenharmony_ci } 292262306a36Sopenharmony_ci 292362306a36Sopenharmony_ci td->status = status; 292462306a36Sopenharmony_ci 292562306a36Sopenharmony_ci /* update the urb's actual_length and give back to the core */ 292662306a36Sopenharmony_ci if (usb_endpoint_xfer_control(&td->urb->ep->desc)) 292762306a36Sopenharmony_ci process_ctrl_td(xhci, ep, ep_ring, td, ep_trb, event); 292862306a36Sopenharmony_ci else if (usb_endpoint_xfer_isoc(&td->urb->ep->desc)) 292962306a36Sopenharmony_ci process_isoc_td(xhci, ep, ep_ring, td, ep_trb, event); 293062306a36Sopenharmony_ci else 293162306a36Sopenharmony_ci process_bulk_intr_td(xhci, ep, ep_ring, td, ep_trb, event); 293262306a36Sopenharmony_cicleanup: 293362306a36Sopenharmony_ci handling_skipped_tds = ep->skip && 293462306a36Sopenharmony_ci trb_comp_code != COMP_MISSED_SERVICE_ERROR && 293562306a36Sopenharmony_ci trb_comp_code != COMP_NO_PING_RESPONSE_ERROR; 293662306a36Sopenharmony_ci 293762306a36Sopenharmony_ci /* 293862306a36Sopenharmony_ci * Do not update event ring dequeue pointer if we're in a loop 293962306a36Sopenharmony_ci * processing missed tds. 294062306a36Sopenharmony_ci */ 294162306a36Sopenharmony_ci if (!handling_skipped_tds) 294262306a36Sopenharmony_ci inc_deq(xhci, ir->event_ring); 294362306a36Sopenharmony_ci 294462306a36Sopenharmony_ci /* 294562306a36Sopenharmony_ci * If ep->skip is set, it means there are missed tds on the 294662306a36Sopenharmony_ci * endpoint ring need to take care of. 294762306a36Sopenharmony_ci * Process them as short transfer until reach the td pointed by 294862306a36Sopenharmony_ci * the event. 294962306a36Sopenharmony_ci */ 295062306a36Sopenharmony_ci } while (handling_skipped_tds); 295162306a36Sopenharmony_ci 295262306a36Sopenharmony_ci return 0; 295362306a36Sopenharmony_ci 295462306a36Sopenharmony_cierr_out: 295562306a36Sopenharmony_ci xhci_err(xhci, "@%016llx %08x %08x %08x %08x\n", 295662306a36Sopenharmony_ci (unsigned long long) xhci_trb_virt_to_dma( 295762306a36Sopenharmony_ci ir->event_ring->deq_seg, 295862306a36Sopenharmony_ci ir->event_ring->dequeue), 295962306a36Sopenharmony_ci lower_32_bits(le64_to_cpu(event->buffer)), 296062306a36Sopenharmony_ci upper_32_bits(le64_to_cpu(event->buffer)), 296162306a36Sopenharmony_ci le32_to_cpu(event->transfer_len), 296262306a36Sopenharmony_ci le32_to_cpu(event->flags)); 296362306a36Sopenharmony_ci return -ENODEV; 296462306a36Sopenharmony_ci} 296562306a36Sopenharmony_ci 296662306a36Sopenharmony_ci/* 296762306a36Sopenharmony_ci * This function handles all OS-owned events on the event ring. It may drop 296862306a36Sopenharmony_ci * xhci->lock between event processing (e.g. to pass up port status changes). 296962306a36Sopenharmony_ci * Returns >0 for "possibly more events to process" (caller should call again), 297062306a36Sopenharmony_ci * otherwise 0 if done. In future, <0 returns should indicate error code. 297162306a36Sopenharmony_ci */ 297262306a36Sopenharmony_cistatic int xhci_handle_event(struct xhci_hcd *xhci, struct xhci_interrupter *ir) 297362306a36Sopenharmony_ci{ 297462306a36Sopenharmony_ci union xhci_trb *event; 297562306a36Sopenharmony_ci int update_ptrs = 1; 297662306a36Sopenharmony_ci u32 trb_type; 297762306a36Sopenharmony_ci int ret; 297862306a36Sopenharmony_ci 297962306a36Sopenharmony_ci /* Event ring hasn't been allocated yet. */ 298062306a36Sopenharmony_ci if (!ir || !ir->event_ring || !ir->event_ring->dequeue) { 298162306a36Sopenharmony_ci xhci_err(xhci, "ERROR interrupter not ready\n"); 298262306a36Sopenharmony_ci return -ENOMEM; 298362306a36Sopenharmony_ci } 298462306a36Sopenharmony_ci 298562306a36Sopenharmony_ci event = ir->event_ring->dequeue; 298662306a36Sopenharmony_ci /* Does the HC or OS own the TRB? */ 298762306a36Sopenharmony_ci if ((le32_to_cpu(event->event_cmd.flags) & TRB_CYCLE) != 298862306a36Sopenharmony_ci ir->event_ring->cycle_state) 298962306a36Sopenharmony_ci return 0; 299062306a36Sopenharmony_ci 299162306a36Sopenharmony_ci trace_xhci_handle_event(ir->event_ring, &event->generic); 299262306a36Sopenharmony_ci 299362306a36Sopenharmony_ci /* 299462306a36Sopenharmony_ci * Barrier between reading the TRB_CYCLE (valid) flag above and any 299562306a36Sopenharmony_ci * speculative reads of the event's flags/data below. 299662306a36Sopenharmony_ci */ 299762306a36Sopenharmony_ci rmb(); 299862306a36Sopenharmony_ci trb_type = TRB_FIELD_TO_TYPE(le32_to_cpu(event->event_cmd.flags)); 299962306a36Sopenharmony_ci /* FIXME: Handle more event types. */ 300062306a36Sopenharmony_ci 300162306a36Sopenharmony_ci switch (trb_type) { 300262306a36Sopenharmony_ci case TRB_COMPLETION: 300362306a36Sopenharmony_ci handle_cmd_completion(xhci, &event->event_cmd); 300462306a36Sopenharmony_ci break; 300562306a36Sopenharmony_ci case TRB_PORT_STATUS: 300662306a36Sopenharmony_ci handle_port_status(xhci, ir, event); 300762306a36Sopenharmony_ci update_ptrs = 0; 300862306a36Sopenharmony_ci break; 300962306a36Sopenharmony_ci case TRB_TRANSFER: 301062306a36Sopenharmony_ci ret = handle_tx_event(xhci, ir, &event->trans_event); 301162306a36Sopenharmony_ci if (ret >= 0) 301262306a36Sopenharmony_ci update_ptrs = 0; 301362306a36Sopenharmony_ci break; 301462306a36Sopenharmony_ci case TRB_DEV_NOTE: 301562306a36Sopenharmony_ci handle_device_notification(xhci, event); 301662306a36Sopenharmony_ci break; 301762306a36Sopenharmony_ci default: 301862306a36Sopenharmony_ci if (trb_type >= TRB_VENDOR_DEFINED_LOW) 301962306a36Sopenharmony_ci handle_vendor_event(xhci, event, trb_type); 302062306a36Sopenharmony_ci else 302162306a36Sopenharmony_ci xhci_warn(xhci, "ERROR unknown event type %d\n", trb_type); 302262306a36Sopenharmony_ci } 302362306a36Sopenharmony_ci /* Any of the above functions may drop and re-acquire the lock, so check 302462306a36Sopenharmony_ci * to make sure a watchdog timer didn't mark the host as non-responsive. 302562306a36Sopenharmony_ci */ 302662306a36Sopenharmony_ci if (xhci->xhc_state & XHCI_STATE_DYING) { 302762306a36Sopenharmony_ci xhci_dbg(xhci, "xHCI host dying, returning from " 302862306a36Sopenharmony_ci "event handler.\n"); 302962306a36Sopenharmony_ci return 0; 303062306a36Sopenharmony_ci } 303162306a36Sopenharmony_ci 303262306a36Sopenharmony_ci if (update_ptrs) 303362306a36Sopenharmony_ci /* Update SW event ring dequeue pointer */ 303462306a36Sopenharmony_ci inc_deq(xhci, ir->event_ring); 303562306a36Sopenharmony_ci 303662306a36Sopenharmony_ci /* Are there more items on the event ring? Caller will call us again to 303762306a36Sopenharmony_ci * check. 303862306a36Sopenharmony_ci */ 303962306a36Sopenharmony_ci return 1; 304062306a36Sopenharmony_ci} 304162306a36Sopenharmony_ci 304262306a36Sopenharmony_ci/* 304362306a36Sopenharmony_ci * Update Event Ring Dequeue Pointer: 304462306a36Sopenharmony_ci * - When all events have finished 304562306a36Sopenharmony_ci * - To avoid "Event Ring Full Error" condition 304662306a36Sopenharmony_ci */ 304762306a36Sopenharmony_cistatic void xhci_update_erst_dequeue(struct xhci_hcd *xhci, 304862306a36Sopenharmony_ci struct xhci_interrupter *ir, 304962306a36Sopenharmony_ci union xhci_trb *event_ring_deq, 305062306a36Sopenharmony_ci bool clear_ehb) 305162306a36Sopenharmony_ci{ 305262306a36Sopenharmony_ci u64 temp_64; 305362306a36Sopenharmony_ci dma_addr_t deq; 305462306a36Sopenharmony_ci 305562306a36Sopenharmony_ci temp_64 = xhci_read_64(xhci, &ir->ir_set->erst_dequeue); 305662306a36Sopenharmony_ci /* If necessary, update the HW's version of the event ring deq ptr. */ 305762306a36Sopenharmony_ci if (event_ring_deq != ir->event_ring->dequeue) { 305862306a36Sopenharmony_ci deq = xhci_trb_virt_to_dma(ir->event_ring->deq_seg, 305962306a36Sopenharmony_ci ir->event_ring->dequeue); 306062306a36Sopenharmony_ci if (deq == 0) 306162306a36Sopenharmony_ci xhci_warn(xhci, "WARN something wrong with SW event ring dequeue ptr\n"); 306262306a36Sopenharmony_ci /* 306362306a36Sopenharmony_ci * Per 4.9.4, Software writes to the ERDP register shall 306462306a36Sopenharmony_ci * always advance the Event Ring Dequeue Pointer value. 306562306a36Sopenharmony_ci */ 306662306a36Sopenharmony_ci if ((temp_64 & (u64) ~ERST_PTR_MASK) == 306762306a36Sopenharmony_ci ((u64) deq & (u64) ~ERST_PTR_MASK)) 306862306a36Sopenharmony_ci return; 306962306a36Sopenharmony_ci 307062306a36Sopenharmony_ci /* Update HC event ring dequeue pointer */ 307162306a36Sopenharmony_ci temp_64 &= ERST_DESI_MASK; 307262306a36Sopenharmony_ci temp_64 |= ((u64) deq & (u64) ~ERST_PTR_MASK); 307362306a36Sopenharmony_ci } 307462306a36Sopenharmony_ci 307562306a36Sopenharmony_ci /* Clear the event handler busy flag (RW1C) */ 307662306a36Sopenharmony_ci if (clear_ehb) 307762306a36Sopenharmony_ci temp_64 |= ERST_EHB; 307862306a36Sopenharmony_ci xhci_write_64(xhci, temp_64, &ir->ir_set->erst_dequeue); 307962306a36Sopenharmony_ci} 308062306a36Sopenharmony_ci 308162306a36Sopenharmony_ci/* 308262306a36Sopenharmony_ci * xHCI spec says we can get an interrupt, and if the HC has an error condition, 308362306a36Sopenharmony_ci * we might get bad data out of the event ring. Section 4.10.2.7 has a list of 308462306a36Sopenharmony_ci * indicators of an event TRB error, but we check the status *first* to be safe. 308562306a36Sopenharmony_ci */ 308662306a36Sopenharmony_ciirqreturn_t xhci_irq(struct usb_hcd *hcd) 308762306a36Sopenharmony_ci{ 308862306a36Sopenharmony_ci struct xhci_hcd *xhci = hcd_to_xhci(hcd); 308962306a36Sopenharmony_ci union xhci_trb *event_ring_deq; 309062306a36Sopenharmony_ci struct xhci_interrupter *ir; 309162306a36Sopenharmony_ci irqreturn_t ret = IRQ_NONE; 309262306a36Sopenharmony_ci u64 temp_64; 309362306a36Sopenharmony_ci u32 status; 309462306a36Sopenharmony_ci int event_loop = 0; 309562306a36Sopenharmony_ci 309662306a36Sopenharmony_ci spin_lock(&xhci->lock); 309762306a36Sopenharmony_ci /* Check if the xHC generated the interrupt, or the irq is shared */ 309862306a36Sopenharmony_ci status = readl(&xhci->op_regs->status); 309962306a36Sopenharmony_ci if (status == ~(u32)0) { 310062306a36Sopenharmony_ci xhci_hc_died(xhci); 310162306a36Sopenharmony_ci ret = IRQ_HANDLED; 310262306a36Sopenharmony_ci goto out; 310362306a36Sopenharmony_ci } 310462306a36Sopenharmony_ci 310562306a36Sopenharmony_ci if (!(status & STS_EINT)) 310662306a36Sopenharmony_ci goto out; 310762306a36Sopenharmony_ci 310862306a36Sopenharmony_ci if (status & STS_HCE) { 310962306a36Sopenharmony_ci xhci_warn(xhci, "WARNING: Host Controller Error\n"); 311062306a36Sopenharmony_ci goto out; 311162306a36Sopenharmony_ci } 311262306a36Sopenharmony_ci 311362306a36Sopenharmony_ci if (status & STS_FATAL) { 311462306a36Sopenharmony_ci xhci_warn(xhci, "WARNING: Host System Error\n"); 311562306a36Sopenharmony_ci xhci_halt(xhci); 311662306a36Sopenharmony_ci ret = IRQ_HANDLED; 311762306a36Sopenharmony_ci goto out; 311862306a36Sopenharmony_ci } 311962306a36Sopenharmony_ci 312062306a36Sopenharmony_ci /* 312162306a36Sopenharmony_ci * Clear the op reg interrupt status first, 312262306a36Sopenharmony_ci * so we can receive interrupts from other MSI-X interrupters. 312362306a36Sopenharmony_ci * Write 1 to clear the interrupt status. 312462306a36Sopenharmony_ci */ 312562306a36Sopenharmony_ci status |= STS_EINT; 312662306a36Sopenharmony_ci writel(status, &xhci->op_regs->status); 312762306a36Sopenharmony_ci 312862306a36Sopenharmony_ci /* This is the handler of the primary interrupter */ 312962306a36Sopenharmony_ci ir = xhci->interrupter; 313062306a36Sopenharmony_ci if (!hcd->msi_enabled) { 313162306a36Sopenharmony_ci u32 irq_pending; 313262306a36Sopenharmony_ci irq_pending = readl(&ir->ir_set->irq_pending); 313362306a36Sopenharmony_ci irq_pending |= IMAN_IP; 313462306a36Sopenharmony_ci writel(irq_pending, &ir->ir_set->irq_pending); 313562306a36Sopenharmony_ci } 313662306a36Sopenharmony_ci 313762306a36Sopenharmony_ci if (xhci->xhc_state & XHCI_STATE_DYING || 313862306a36Sopenharmony_ci xhci->xhc_state & XHCI_STATE_HALTED) { 313962306a36Sopenharmony_ci xhci_dbg(xhci, "xHCI dying, ignoring interrupt. " 314062306a36Sopenharmony_ci "Shouldn't IRQs be disabled?\n"); 314162306a36Sopenharmony_ci /* Clear the event handler busy flag (RW1C); 314262306a36Sopenharmony_ci * the event ring should be empty. 314362306a36Sopenharmony_ci */ 314462306a36Sopenharmony_ci temp_64 = xhci_read_64(xhci, &ir->ir_set->erst_dequeue); 314562306a36Sopenharmony_ci xhci_write_64(xhci, temp_64 | ERST_EHB, 314662306a36Sopenharmony_ci &ir->ir_set->erst_dequeue); 314762306a36Sopenharmony_ci ret = IRQ_HANDLED; 314862306a36Sopenharmony_ci goto out; 314962306a36Sopenharmony_ci } 315062306a36Sopenharmony_ci 315162306a36Sopenharmony_ci event_ring_deq = ir->event_ring->dequeue; 315262306a36Sopenharmony_ci /* FIXME this should be a delayed service routine 315362306a36Sopenharmony_ci * that clears the EHB. 315462306a36Sopenharmony_ci */ 315562306a36Sopenharmony_ci while (xhci_handle_event(xhci, ir) > 0) { 315662306a36Sopenharmony_ci if (event_loop++ < TRBS_PER_SEGMENT / 2) 315762306a36Sopenharmony_ci continue; 315862306a36Sopenharmony_ci xhci_update_erst_dequeue(xhci, ir, event_ring_deq, false); 315962306a36Sopenharmony_ci event_ring_deq = ir->event_ring->dequeue; 316062306a36Sopenharmony_ci 316162306a36Sopenharmony_ci /* ring is half-full, force isoc trbs to interrupt more often */ 316262306a36Sopenharmony_ci if (xhci->isoc_bei_interval > AVOID_BEI_INTERVAL_MIN) 316362306a36Sopenharmony_ci xhci->isoc_bei_interval = xhci->isoc_bei_interval / 2; 316462306a36Sopenharmony_ci 316562306a36Sopenharmony_ci event_loop = 0; 316662306a36Sopenharmony_ci } 316762306a36Sopenharmony_ci 316862306a36Sopenharmony_ci xhci_update_erst_dequeue(xhci, ir, event_ring_deq, true); 316962306a36Sopenharmony_ci ret = IRQ_HANDLED; 317062306a36Sopenharmony_ci 317162306a36Sopenharmony_ciout: 317262306a36Sopenharmony_ci spin_unlock(&xhci->lock); 317362306a36Sopenharmony_ci 317462306a36Sopenharmony_ci return ret; 317562306a36Sopenharmony_ci} 317662306a36Sopenharmony_ci 317762306a36Sopenharmony_ciirqreturn_t xhci_msi_irq(int irq, void *hcd) 317862306a36Sopenharmony_ci{ 317962306a36Sopenharmony_ci return xhci_irq(hcd); 318062306a36Sopenharmony_ci} 318162306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(xhci_msi_irq); 318262306a36Sopenharmony_ci 318362306a36Sopenharmony_ci/**** Endpoint Ring Operations ****/ 318462306a36Sopenharmony_ci 318562306a36Sopenharmony_ci/* 318662306a36Sopenharmony_ci * Generic function for queueing a TRB on a ring. 318762306a36Sopenharmony_ci * The caller must have checked to make sure there's room on the ring. 318862306a36Sopenharmony_ci * 318962306a36Sopenharmony_ci * @more_trbs_coming: Will you enqueue more TRBs before calling 319062306a36Sopenharmony_ci * prepare_transfer()? 319162306a36Sopenharmony_ci */ 319262306a36Sopenharmony_cistatic void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring, 319362306a36Sopenharmony_ci bool more_trbs_coming, 319462306a36Sopenharmony_ci u32 field1, u32 field2, u32 field3, u32 field4) 319562306a36Sopenharmony_ci{ 319662306a36Sopenharmony_ci struct xhci_generic_trb *trb; 319762306a36Sopenharmony_ci 319862306a36Sopenharmony_ci trb = &ring->enqueue->generic; 319962306a36Sopenharmony_ci trb->field[0] = cpu_to_le32(field1); 320062306a36Sopenharmony_ci trb->field[1] = cpu_to_le32(field2); 320162306a36Sopenharmony_ci trb->field[2] = cpu_to_le32(field3); 320262306a36Sopenharmony_ci /* make sure TRB is fully written before giving it to the controller */ 320362306a36Sopenharmony_ci wmb(); 320462306a36Sopenharmony_ci trb->field[3] = cpu_to_le32(field4); 320562306a36Sopenharmony_ci 320662306a36Sopenharmony_ci trace_xhci_queue_trb(ring, trb); 320762306a36Sopenharmony_ci 320862306a36Sopenharmony_ci inc_enq(xhci, ring, more_trbs_coming); 320962306a36Sopenharmony_ci} 321062306a36Sopenharmony_ci 321162306a36Sopenharmony_ci/* 321262306a36Sopenharmony_ci * Does various checks on the endpoint ring, and makes it ready to queue num_trbs. 321362306a36Sopenharmony_ci * expand ring if it start to be full. 321462306a36Sopenharmony_ci */ 321562306a36Sopenharmony_cistatic int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring, 321662306a36Sopenharmony_ci u32 ep_state, unsigned int num_trbs, gfp_t mem_flags) 321762306a36Sopenharmony_ci{ 321862306a36Sopenharmony_ci unsigned int link_trb_count = 0; 321962306a36Sopenharmony_ci unsigned int new_segs = 0; 322062306a36Sopenharmony_ci 322162306a36Sopenharmony_ci /* Make sure the endpoint has been added to xHC schedule */ 322262306a36Sopenharmony_ci switch (ep_state) { 322362306a36Sopenharmony_ci case EP_STATE_DISABLED: 322462306a36Sopenharmony_ci /* 322562306a36Sopenharmony_ci * USB core changed config/interfaces without notifying us, 322662306a36Sopenharmony_ci * or hardware is reporting the wrong state. 322762306a36Sopenharmony_ci */ 322862306a36Sopenharmony_ci xhci_warn(xhci, "WARN urb submitted to disabled ep\n"); 322962306a36Sopenharmony_ci return -ENOENT; 323062306a36Sopenharmony_ci case EP_STATE_ERROR: 323162306a36Sopenharmony_ci xhci_warn(xhci, "WARN waiting for error on ep to be cleared\n"); 323262306a36Sopenharmony_ci /* FIXME event handling code for error needs to clear it */ 323362306a36Sopenharmony_ci /* XXX not sure if this should be -ENOENT or not */ 323462306a36Sopenharmony_ci return -EINVAL; 323562306a36Sopenharmony_ci case EP_STATE_HALTED: 323662306a36Sopenharmony_ci xhci_dbg(xhci, "WARN halted endpoint, queueing URB anyway.\n"); 323762306a36Sopenharmony_ci break; 323862306a36Sopenharmony_ci case EP_STATE_STOPPED: 323962306a36Sopenharmony_ci case EP_STATE_RUNNING: 324062306a36Sopenharmony_ci break; 324162306a36Sopenharmony_ci default: 324262306a36Sopenharmony_ci xhci_err(xhci, "ERROR unknown endpoint state for ep\n"); 324362306a36Sopenharmony_ci /* 324462306a36Sopenharmony_ci * FIXME issue Configure Endpoint command to try to get the HC 324562306a36Sopenharmony_ci * back into a known state. 324662306a36Sopenharmony_ci */ 324762306a36Sopenharmony_ci return -EINVAL; 324862306a36Sopenharmony_ci } 324962306a36Sopenharmony_ci 325062306a36Sopenharmony_ci if (ep_ring != xhci->cmd_ring) { 325162306a36Sopenharmony_ci new_segs = xhci_ring_expansion_needed(xhci, ep_ring, num_trbs); 325262306a36Sopenharmony_ci } else if (xhci_num_trbs_free(xhci, ep_ring) <= num_trbs) { 325362306a36Sopenharmony_ci xhci_err(xhci, "Do not support expand command ring\n"); 325462306a36Sopenharmony_ci return -ENOMEM; 325562306a36Sopenharmony_ci } 325662306a36Sopenharmony_ci 325762306a36Sopenharmony_ci if (new_segs) { 325862306a36Sopenharmony_ci xhci_dbg_trace(xhci, trace_xhci_dbg_ring_expansion, 325962306a36Sopenharmony_ci "ERROR no room on ep ring, try ring expansion"); 326062306a36Sopenharmony_ci if (xhci_ring_expansion(xhci, ep_ring, new_segs, mem_flags)) { 326162306a36Sopenharmony_ci xhci_err(xhci, "Ring expansion failed\n"); 326262306a36Sopenharmony_ci return -ENOMEM; 326362306a36Sopenharmony_ci } 326462306a36Sopenharmony_ci } 326562306a36Sopenharmony_ci 326662306a36Sopenharmony_ci while (trb_is_link(ep_ring->enqueue)) { 326762306a36Sopenharmony_ci /* If we're not dealing with 0.95 hardware or isoc rings 326862306a36Sopenharmony_ci * on AMD 0.96 host, clear the chain bit. 326962306a36Sopenharmony_ci */ 327062306a36Sopenharmony_ci if (!xhci_link_trb_quirk(xhci) && 327162306a36Sopenharmony_ci !(ep_ring->type == TYPE_ISOC && 327262306a36Sopenharmony_ci (xhci->quirks & XHCI_AMD_0x96_HOST))) 327362306a36Sopenharmony_ci ep_ring->enqueue->link.control &= 327462306a36Sopenharmony_ci cpu_to_le32(~TRB_CHAIN); 327562306a36Sopenharmony_ci else 327662306a36Sopenharmony_ci ep_ring->enqueue->link.control |= 327762306a36Sopenharmony_ci cpu_to_le32(TRB_CHAIN); 327862306a36Sopenharmony_ci 327962306a36Sopenharmony_ci wmb(); 328062306a36Sopenharmony_ci ep_ring->enqueue->link.control ^= cpu_to_le32(TRB_CYCLE); 328162306a36Sopenharmony_ci 328262306a36Sopenharmony_ci /* Toggle the cycle bit after the last ring segment. */ 328362306a36Sopenharmony_ci if (link_trb_toggles_cycle(ep_ring->enqueue)) 328462306a36Sopenharmony_ci ep_ring->cycle_state ^= 1; 328562306a36Sopenharmony_ci 328662306a36Sopenharmony_ci ep_ring->enq_seg = ep_ring->enq_seg->next; 328762306a36Sopenharmony_ci ep_ring->enqueue = ep_ring->enq_seg->trbs; 328862306a36Sopenharmony_ci 328962306a36Sopenharmony_ci /* prevent infinite loop if all first trbs are link trbs */ 329062306a36Sopenharmony_ci if (link_trb_count++ > ep_ring->num_segs) { 329162306a36Sopenharmony_ci xhci_warn(xhci, "Ring is an endless link TRB loop\n"); 329262306a36Sopenharmony_ci return -EINVAL; 329362306a36Sopenharmony_ci } 329462306a36Sopenharmony_ci } 329562306a36Sopenharmony_ci 329662306a36Sopenharmony_ci if (last_trb_on_seg(ep_ring->enq_seg, ep_ring->enqueue)) { 329762306a36Sopenharmony_ci xhci_warn(xhci, "Missing link TRB at end of ring segment\n"); 329862306a36Sopenharmony_ci return -EINVAL; 329962306a36Sopenharmony_ci } 330062306a36Sopenharmony_ci 330162306a36Sopenharmony_ci return 0; 330262306a36Sopenharmony_ci} 330362306a36Sopenharmony_ci 330462306a36Sopenharmony_cistatic int prepare_transfer(struct xhci_hcd *xhci, 330562306a36Sopenharmony_ci struct xhci_virt_device *xdev, 330662306a36Sopenharmony_ci unsigned int ep_index, 330762306a36Sopenharmony_ci unsigned int stream_id, 330862306a36Sopenharmony_ci unsigned int num_trbs, 330962306a36Sopenharmony_ci struct urb *urb, 331062306a36Sopenharmony_ci unsigned int td_index, 331162306a36Sopenharmony_ci gfp_t mem_flags) 331262306a36Sopenharmony_ci{ 331362306a36Sopenharmony_ci int ret; 331462306a36Sopenharmony_ci struct urb_priv *urb_priv; 331562306a36Sopenharmony_ci struct xhci_td *td; 331662306a36Sopenharmony_ci struct xhci_ring *ep_ring; 331762306a36Sopenharmony_ci struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index); 331862306a36Sopenharmony_ci 331962306a36Sopenharmony_ci ep_ring = xhci_triad_to_transfer_ring(xhci, xdev->slot_id, ep_index, 332062306a36Sopenharmony_ci stream_id); 332162306a36Sopenharmony_ci if (!ep_ring) { 332262306a36Sopenharmony_ci xhci_dbg(xhci, "Can't prepare ring for bad stream ID %u\n", 332362306a36Sopenharmony_ci stream_id); 332462306a36Sopenharmony_ci return -EINVAL; 332562306a36Sopenharmony_ci } 332662306a36Sopenharmony_ci 332762306a36Sopenharmony_ci ret = prepare_ring(xhci, ep_ring, GET_EP_CTX_STATE(ep_ctx), 332862306a36Sopenharmony_ci num_trbs, mem_flags); 332962306a36Sopenharmony_ci if (ret) 333062306a36Sopenharmony_ci return ret; 333162306a36Sopenharmony_ci 333262306a36Sopenharmony_ci urb_priv = urb->hcpriv; 333362306a36Sopenharmony_ci td = &urb_priv->td[td_index]; 333462306a36Sopenharmony_ci 333562306a36Sopenharmony_ci INIT_LIST_HEAD(&td->td_list); 333662306a36Sopenharmony_ci INIT_LIST_HEAD(&td->cancelled_td_list); 333762306a36Sopenharmony_ci 333862306a36Sopenharmony_ci if (td_index == 0) { 333962306a36Sopenharmony_ci ret = usb_hcd_link_urb_to_ep(bus_to_hcd(urb->dev->bus), urb); 334062306a36Sopenharmony_ci if (unlikely(ret)) 334162306a36Sopenharmony_ci return ret; 334262306a36Sopenharmony_ci } 334362306a36Sopenharmony_ci 334462306a36Sopenharmony_ci td->urb = urb; 334562306a36Sopenharmony_ci /* Add this TD to the tail of the endpoint ring's TD list */ 334662306a36Sopenharmony_ci list_add_tail(&td->td_list, &ep_ring->td_list); 334762306a36Sopenharmony_ci td->start_seg = ep_ring->enq_seg; 334862306a36Sopenharmony_ci td->first_trb = ep_ring->enqueue; 334962306a36Sopenharmony_ci 335062306a36Sopenharmony_ci return 0; 335162306a36Sopenharmony_ci} 335262306a36Sopenharmony_ci 335362306a36Sopenharmony_ciunsigned int count_trbs(u64 addr, u64 len) 335462306a36Sopenharmony_ci{ 335562306a36Sopenharmony_ci unsigned int num_trbs; 335662306a36Sopenharmony_ci 335762306a36Sopenharmony_ci num_trbs = DIV_ROUND_UP(len + (addr & (TRB_MAX_BUFF_SIZE - 1)), 335862306a36Sopenharmony_ci TRB_MAX_BUFF_SIZE); 335962306a36Sopenharmony_ci if (num_trbs == 0) 336062306a36Sopenharmony_ci num_trbs++; 336162306a36Sopenharmony_ci 336262306a36Sopenharmony_ci return num_trbs; 336362306a36Sopenharmony_ci} 336462306a36Sopenharmony_ci 336562306a36Sopenharmony_cistatic inline unsigned int count_trbs_needed(struct urb *urb) 336662306a36Sopenharmony_ci{ 336762306a36Sopenharmony_ci return count_trbs(urb->transfer_dma, urb->transfer_buffer_length); 336862306a36Sopenharmony_ci} 336962306a36Sopenharmony_ci 337062306a36Sopenharmony_cistatic unsigned int count_sg_trbs_needed(struct urb *urb) 337162306a36Sopenharmony_ci{ 337262306a36Sopenharmony_ci struct scatterlist *sg; 337362306a36Sopenharmony_ci unsigned int i, len, full_len, num_trbs = 0; 337462306a36Sopenharmony_ci 337562306a36Sopenharmony_ci full_len = urb->transfer_buffer_length; 337662306a36Sopenharmony_ci 337762306a36Sopenharmony_ci for_each_sg(urb->sg, sg, urb->num_mapped_sgs, i) { 337862306a36Sopenharmony_ci len = sg_dma_len(sg); 337962306a36Sopenharmony_ci num_trbs += count_trbs(sg_dma_address(sg), len); 338062306a36Sopenharmony_ci len = min_t(unsigned int, len, full_len); 338162306a36Sopenharmony_ci full_len -= len; 338262306a36Sopenharmony_ci if (full_len == 0) 338362306a36Sopenharmony_ci break; 338462306a36Sopenharmony_ci } 338562306a36Sopenharmony_ci 338662306a36Sopenharmony_ci return num_trbs; 338762306a36Sopenharmony_ci} 338862306a36Sopenharmony_ci 338962306a36Sopenharmony_cistatic unsigned int count_isoc_trbs_needed(struct urb *urb, int i) 339062306a36Sopenharmony_ci{ 339162306a36Sopenharmony_ci u64 addr, len; 339262306a36Sopenharmony_ci 339362306a36Sopenharmony_ci addr = (u64) (urb->transfer_dma + urb->iso_frame_desc[i].offset); 339462306a36Sopenharmony_ci len = urb->iso_frame_desc[i].length; 339562306a36Sopenharmony_ci 339662306a36Sopenharmony_ci return count_trbs(addr, len); 339762306a36Sopenharmony_ci} 339862306a36Sopenharmony_ci 339962306a36Sopenharmony_cistatic void check_trb_math(struct urb *urb, int running_total) 340062306a36Sopenharmony_ci{ 340162306a36Sopenharmony_ci if (unlikely(running_total != urb->transfer_buffer_length)) 340262306a36Sopenharmony_ci dev_err(&urb->dev->dev, "%s - ep %#x - Miscalculated tx length, " 340362306a36Sopenharmony_ci "queued %#x (%d), asked for %#x (%d)\n", 340462306a36Sopenharmony_ci __func__, 340562306a36Sopenharmony_ci urb->ep->desc.bEndpointAddress, 340662306a36Sopenharmony_ci running_total, running_total, 340762306a36Sopenharmony_ci urb->transfer_buffer_length, 340862306a36Sopenharmony_ci urb->transfer_buffer_length); 340962306a36Sopenharmony_ci} 341062306a36Sopenharmony_ci 341162306a36Sopenharmony_cistatic void giveback_first_trb(struct xhci_hcd *xhci, int slot_id, 341262306a36Sopenharmony_ci unsigned int ep_index, unsigned int stream_id, int start_cycle, 341362306a36Sopenharmony_ci struct xhci_generic_trb *start_trb) 341462306a36Sopenharmony_ci{ 341562306a36Sopenharmony_ci /* 341662306a36Sopenharmony_ci * Pass all the TRBs to the hardware at once and make sure this write 341762306a36Sopenharmony_ci * isn't reordered. 341862306a36Sopenharmony_ci */ 341962306a36Sopenharmony_ci wmb(); 342062306a36Sopenharmony_ci if (start_cycle) 342162306a36Sopenharmony_ci start_trb->field[3] |= cpu_to_le32(start_cycle); 342262306a36Sopenharmony_ci else 342362306a36Sopenharmony_ci start_trb->field[3] &= cpu_to_le32(~TRB_CYCLE); 342462306a36Sopenharmony_ci xhci_ring_ep_doorbell(xhci, slot_id, ep_index, stream_id); 342562306a36Sopenharmony_ci} 342662306a36Sopenharmony_ci 342762306a36Sopenharmony_cistatic void check_interval(struct xhci_hcd *xhci, struct urb *urb, 342862306a36Sopenharmony_ci struct xhci_ep_ctx *ep_ctx) 342962306a36Sopenharmony_ci{ 343062306a36Sopenharmony_ci int xhci_interval; 343162306a36Sopenharmony_ci int ep_interval; 343262306a36Sopenharmony_ci 343362306a36Sopenharmony_ci xhci_interval = EP_INTERVAL_TO_UFRAMES(le32_to_cpu(ep_ctx->ep_info)); 343462306a36Sopenharmony_ci ep_interval = urb->interval; 343562306a36Sopenharmony_ci 343662306a36Sopenharmony_ci /* Convert to microframes */ 343762306a36Sopenharmony_ci if (urb->dev->speed == USB_SPEED_LOW || 343862306a36Sopenharmony_ci urb->dev->speed == USB_SPEED_FULL) 343962306a36Sopenharmony_ci ep_interval *= 8; 344062306a36Sopenharmony_ci 344162306a36Sopenharmony_ci /* FIXME change this to a warning and a suggestion to use the new API 344262306a36Sopenharmony_ci * to set the polling interval (once the API is added). 344362306a36Sopenharmony_ci */ 344462306a36Sopenharmony_ci if (xhci_interval != ep_interval) { 344562306a36Sopenharmony_ci dev_dbg_ratelimited(&urb->dev->dev, 344662306a36Sopenharmony_ci "Driver uses different interval (%d microframe%s) than xHCI (%d microframe%s)\n", 344762306a36Sopenharmony_ci ep_interval, ep_interval == 1 ? "" : "s", 344862306a36Sopenharmony_ci xhci_interval, xhci_interval == 1 ? "" : "s"); 344962306a36Sopenharmony_ci urb->interval = xhci_interval; 345062306a36Sopenharmony_ci /* Convert back to frames for LS/FS devices */ 345162306a36Sopenharmony_ci if (urb->dev->speed == USB_SPEED_LOW || 345262306a36Sopenharmony_ci urb->dev->speed == USB_SPEED_FULL) 345362306a36Sopenharmony_ci urb->interval /= 8; 345462306a36Sopenharmony_ci } 345562306a36Sopenharmony_ci} 345662306a36Sopenharmony_ci 345762306a36Sopenharmony_ci/* 345862306a36Sopenharmony_ci * xHCI uses normal TRBs for both bulk and interrupt. When the interrupt 345962306a36Sopenharmony_ci * endpoint is to be serviced, the xHC will consume (at most) one TD. A TD 346062306a36Sopenharmony_ci * (comprised of sg list entries) can take several service intervals to 346162306a36Sopenharmony_ci * transmit. 346262306a36Sopenharmony_ci */ 346362306a36Sopenharmony_ciint xhci_queue_intr_tx(struct xhci_hcd *xhci, gfp_t mem_flags, 346462306a36Sopenharmony_ci struct urb *urb, int slot_id, unsigned int ep_index) 346562306a36Sopenharmony_ci{ 346662306a36Sopenharmony_ci struct xhci_ep_ctx *ep_ctx; 346762306a36Sopenharmony_ci 346862306a36Sopenharmony_ci ep_ctx = xhci_get_ep_ctx(xhci, xhci->devs[slot_id]->out_ctx, ep_index); 346962306a36Sopenharmony_ci check_interval(xhci, urb, ep_ctx); 347062306a36Sopenharmony_ci 347162306a36Sopenharmony_ci return xhci_queue_bulk_tx(xhci, mem_flags, urb, slot_id, ep_index); 347262306a36Sopenharmony_ci} 347362306a36Sopenharmony_ci 347462306a36Sopenharmony_ci/* 347562306a36Sopenharmony_ci * For xHCI 1.0 host controllers, TD size is the number of max packet sized 347662306a36Sopenharmony_ci * packets remaining in the TD (*not* including this TRB). 347762306a36Sopenharmony_ci * 347862306a36Sopenharmony_ci * Total TD packet count = total_packet_count = 347962306a36Sopenharmony_ci * DIV_ROUND_UP(TD size in bytes / wMaxPacketSize) 348062306a36Sopenharmony_ci * 348162306a36Sopenharmony_ci * Packets transferred up to and including this TRB = packets_transferred = 348262306a36Sopenharmony_ci * rounddown(total bytes transferred including this TRB / wMaxPacketSize) 348362306a36Sopenharmony_ci * 348462306a36Sopenharmony_ci * TD size = total_packet_count - packets_transferred 348562306a36Sopenharmony_ci * 348662306a36Sopenharmony_ci * For xHCI 0.96 and older, TD size field should be the remaining bytes 348762306a36Sopenharmony_ci * including this TRB, right shifted by 10 348862306a36Sopenharmony_ci * 348962306a36Sopenharmony_ci * For all hosts it must fit in bits 21:17, so it can't be bigger than 31. 349062306a36Sopenharmony_ci * This is taken care of in the TRB_TD_SIZE() macro 349162306a36Sopenharmony_ci * 349262306a36Sopenharmony_ci * The last TRB in a TD must have the TD size set to zero. 349362306a36Sopenharmony_ci */ 349462306a36Sopenharmony_cistatic u32 xhci_td_remainder(struct xhci_hcd *xhci, int transferred, 349562306a36Sopenharmony_ci int trb_buff_len, unsigned int td_total_len, 349662306a36Sopenharmony_ci struct urb *urb, bool more_trbs_coming) 349762306a36Sopenharmony_ci{ 349862306a36Sopenharmony_ci u32 maxp, total_packet_count; 349962306a36Sopenharmony_ci 350062306a36Sopenharmony_ci /* MTK xHCI 0.96 contains some features from 1.0 */ 350162306a36Sopenharmony_ci if (xhci->hci_version < 0x100 && !(xhci->quirks & XHCI_MTK_HOST)) 350262306a36Sopenharmony_ci return ((td_total_len - transferred) >> 10); 350362306a36Sopenharmony_ci 350462306a36Sopenharmony_ci /* One TRB with a zero-length data packet. */ 350562306a36Sopenharmony_ci if (!more_trbs_coming || (transferred == 0 && trb_buff_len == 0) || 350662306a36Sopenharmony_ci trb_buff_len == td_total_len) 350762306a36Sopenharmony_ci return 0; 350862306a36Sopenharmony_ci 350962306a36Sopenharmony_ci /* for MTK xHCI 0.96, TD size include this TRB, but not in 1.x */ 351062306a36Sopenharmony_ci if ((xhci->quirks & XHCI_MTK_HOST) && (xhci->hci_version < 0x100)) 351162306a36Sopenharmony_ci trb_buff_len = 0; 351262306a36Sopenharmony_ci 351362306a36Sopenharmony_ci maxp = usb_endpoint_maxp(&urb->ep->desc); 351462306a36Sopenharmony_ci total_packet_count = DIV_ROUND_UP(td_total_len, maxp); 351562306a36Sopenharmony_ci 351662306a36Sopenharmony_ci /* Queueing functions don't count the current TRB into transferred */ 351762306a36Sopenharmony_ci return (total_packet_count - ((transferred + trb_buff_len) / maxp)); 351862306a36Sopenharmony_ci} 351962306a36Sopenharmony_ci 352062306a36Sopenharmony_ci 352162306a36Sopenharmony_cistatic int xhci_align_td(struct xhci_hcd *xhci, struct urb *urb, u32 enqd_len, 352262306a36Sopenharmony_ci u32 *trb_buff_len, struct xhci_segment *seg) 352362306a36Sopenharmony_ci{ 352462306a36Sopenharmony_ci struct device *dev = xhci_to_hcd(xhci)->self.sysdev; 352562306a36Sopenharmony_ci unsigned int unalign; 352662306a36Sopenharmony_ci unsigned int max_pkt; 352762306a36Sopenharmony_ci u32 new_buff_len; 352862306a36Sopenharmony_ci size_t len; 352962306a36Sopenharmony_ci 353062306a36Sopenharmony_ci max_pkt = usb_endpoint_maxp(&urb->ep->desc); 353162306a36Sopenharmony_ci unalign = (enqd_len + *trb_buff_len) % max_pkt; 353262306a36Sopenharmony_ci 353362306a36Sopenharmony_ci /* we got lucky, last normal TRB data on segment is packet aligned */ 353462306a36Sopenharmony_ci if (unalign == 0) 353562306a36Sopenharmony_ci return 0; 353662306a36Sopenharmony_ci 353762306a36Sopenharmony_ci xhci_dbg(xhci, "Unaligned %d bytes, buff len %d\n", 353862306a36Sopenharmony_ci unalign, *trb_buff_len); 353962306a36Sopenharmony_ci 354062306a36Sopenharmony_ci /* is the last nornal TRB alignable by splitting it */ 354162306a36Sopenharmony_ci if (*trb_buff_len > unalign) { 354262306a36Sopenharmony_ci *trb_buff_len -= unalign; 354362306a36Sopenharmony_ci xhci_dbg(xhci, "split align, new buff len %d\n", *trb_buff_len); 354462306a36Sopenharmony_ci return 0; 354562306a36Sopenharmony_ci } 354662306a36Sopenharmony_ci 354762306a36Sopenharmony_ci /* 354862306a36Sopenharmony_ci * We want enqd_len + trb_buff_len to sum up to a number aligned to 354962306a36Sopenharmony_ci * number which is divisible by the endpoint's wMaxPacketSize. IOW: 355062306a36Sopenharmony_ci * (size of currently enqueued TRBs + remainder) % wMaxPacketSize == 0. 355162306a36Sopenharmony_ci */ 355262306a36Sopenharmony_ci new_buff_len = max_pkt - (enqd_len % max_pkt); 355362306a36Sopenharmony_ci 355462306a36Sopenharmony_ci if (new_buff_len > (urb->transfer_buffer_length - enqd_len)) 355562306a36Sopenharmony_ci new_buff_len = (urb->transfer_buffer_length - enqd_len); 355662306a36Sopenharmony_ci 355762306a36Sopenharmony_ci /* create a max max_pkt sized bounce buffer pointed to by last trb */ 355862306a36Sopenharmony_ci if (usb_urb_dir_out(urb)) { 355962306a36Sopenharmony_ci if (urb->num_sgs) { 356062306a36Sopenharmony_ci len = sg_pcopy_to_buffer(urb->sg, urb->num_sgs, 356162306a36Sopenharmony_ci seg->bounce_buf, new_buff_len, enqd_len); 356262306a36Sopenharmony_ci if (len != new_buff_len) 356362306a36Sopenharmony_ci xhci_warn(xhci, "WARN Wrong bounce buffer write length: %zu != %d\n", 356462306a36Sopenharmony_ci len, new_buff_len); 356562306a36Sopenharmony_ci } else { 356662306a36Sopenharmony_ci memcpy(seg->bounce_buf, urb->transfer_buffer + enqd_len, new_buff_len); 356762306a36Sopenharmony_ci } 356862306a36Sopenharmony_ci 356962306a36Sopenharmony_ci seg->bounce_dma = dma_map_single(dev, seg->bounce_buf, 357062306a36Sopenharmony_ci max_pkt, DMA_TO_DEVICE); 357162306a36Sopenharmony_ci } else { 357262306a36Sopenharmony_ci seg->bounce_dma = dma_map_single(dev, seg->bounce_buf, 357362306a36Sopenharmony_ci max_pkt, DMA_FROM_DEVICE); 357462306a36Sopenharmony_ci } 357562306a36Sopenharmony_ci 357662306a36Sopenharmony_ci if (dma_mapping_error(dev, seg->bounce_dma)) { 357762306a36Sopenharmony_ci /* try without aligning. Some host controllers survive */ 357862306a36Sopenharmony_ci xhci_warn(xhci, "Failed mapping bounce buffer, not aligning\n"); 357962306a36Sopenharmony_ci return 0; 358062306a36Sopenharmony_ci } 358162306a36Sopenharmony_ci *trb_buff_len = new_buff_len; 358262306a36Sopenharmony_ci seg->bounce_len = new_buff_len; 358362306a36Sopenharmony_ci seg->bounce_offs = enqd_len; 358462306a36Sopenharmony_ci 358562306a36Sopenharmony_ci xhci_dbg(xhci, "Bounce align, new buff len %d\n", *trb_buff_len); 358662306a36Sopenharmony_ci 358762306a36Sopenharmony_ci return 1; 358862306a36Sopenharmony_ci} 358962306a36Sopenharmony_ci 359062306a36Sopenharmony_ci/* This is very similar to what ehci-q.c qtd_fill() does */ 359162306a36Sopenharmony_ciint xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags, 359262306a36Sopenharmony_ci struct urb *urb, int slot_id, unsigned int ep_index) 359362306a36Sopenharmony_ci{ 359462306a36Sopenharmony_ci struct xhci_ring *ring; 359562306a36Sopenharmony_ci struct urb_priv *urb_priv; 359662306a36Sopenharmony_ci struct xhci_td *td; 359762306a36Sopenharmony_ci struct xhci_generic_trb *start_trb; 359862306a36Sopenharmony_ci struct scatterlist *sg = NULL; 359962306a36Sopenharmony_ci bool more_trbs_coming = true; 360062306a36Sopenharmony_ci bool need_zero_pkt = false; 360162306a36Sopenharmony_ci bool first_trb = true; 360262306a36Sopenharmony_ci unsigned int num_trbs; 360362306a36Sopenharmony_ci unsigned int start_cycle, num_sgs = 0; 360462306a36Sopenharmony_ci unsigned int enqd_len, block_len, trb_buff_len, full_len; 360562306a36Sopenharmony_ci int sent_len, ret; 360662306a36Sopenharmony_ci u32 field, length_field, remainder; 360762306a36Sopenharmony_ci u64 addr, send_addr; 360862306a36Sopenharmony_ci 360962306a36Sopenharmony_ci ring = xhci_urb_to_transfer_ring(xhci, urb); 361062306a36Sopenharmony_ci if (!ring) 361162306a36Sopenharmony_ci return -EINVAL; 361262306a36Sopenharmony_ci 361362306a36Sopenharmony_ci full_len = urb->transfer_buffer_length; 361462306a36Sopenharmony_ci /* If we have scatter/gather list, we use it. */ 361562306a36Sopenharmony_ci if (urb->num_sgs && !(urb->transfer_flags & URB_DMA_MAP_SINGLE)) { 361662306a36Sopenharmony_ci num_sgs = urb->num_mapped_sgs; 361762306a36Sopenharmony_ci sg = urb->sg; 361862306a36Sopenharmony_ci addr = (u64) sg_dma_address(sg); 361962306a36Sopenharmony_ci block_len = sg_dma_len(sg); 362062306a36Sopenharmony_ci num_trbs = count_sg_trbs_needed(urb); 362162306a36Sopenharmony_ci } else { 362262306a36Sopenharmony_ci num_trbs = count_trbs_needed(urb); 362362306a36Sopenharmony_ci addr = (u64) urb->transfer_dma; 362462306a36Sopenharmony_ci block_len = full_len; 362562306a36Sopenharmony_ci } 362662306a36Sopenharmony_ci ret = prepare_transfer(xhci, xhci->devs[slot_id], 362762306a36Sopenharmony_ci ep_index, urb->stream_id, 362862306a36Sopenharmony_ci num_trbs, urb, 0, mem_flags); 362962306a36Sopenharmony_ci if (unlikely(ret < 0)) 363062306a36Sopenharmony_ci return ret; 363162306a36Sopenharmony_ci 363262306a36Sopenharmony_ci urb_priv = urb->hcpriv; 363362306a36Sopenharmony_ci 363462306a36Sopenharmony_ci /* Deal with URB_ZERO_PACKET - need one more td/trb */ 363562306a36Sopenharmony_ci if (urb->transfer_flags & URB_ZERO_PACKET && urb_priv->num_tds > 1) 363662306a36Sopenharmony_ci need_zero_pkt = true; 363762306a36Sopenharmony_ci 363862306a36Sopenharmony_ci td = &urb_priv->td[0]; 363962306a36Sopenharmony_ci 364062306a36Sopenharmony_ci /* 364162306a36Sopenharmony_ci * Don't give the first TRB to the hardware (by toggling the cycle bit) 364262306a36Sopenharmony_ci * until we've finished creating all the other TRBs. The ring's cycle 364362306a36Sopenharmony_ci * state may change as we enqueue the other TRBs, so save it too. 364462306a36Sopenharmony_ci */ 364562306a36Sopenharmony_ci start_trb = &ring->enqueue->generic; 364662306a36Sopenharmony_ci start_cycle = ring->cycle_state; 364762306a36Sopenharmony_ci send_addr = addr; 364862306a36Sopenharmony_ci 364962306a36Sopenharmony_ci /* Queue the TRBs, even if they are zero-length */ 365062306a36Sopenharmony_ci for (enqd_len = 0; first_trb || enqd_len < full_len; 365162306a36Sopenharmony_ci enqd_len += trb_buff_len) { 365262306a36Sopenharmony_ci field = TRB_TYPE(TRB_NORMAL); 365362306a36Sopenharmony_ci 365462306a36Sopenharmony_ci /* TRB buffer should not cross 64KB boundaries */ 365562306a36Sopenharmony_ci trb_buff_len = TRB_BUFF_LEN_UP_TO_BOUNDARY(addr); 365662306a36Sopenharmony_ci trb_buff_len = min_t(unsigned int, trb_buff_len, block_len); 365762306a36Sopenharmony_ci 365862306a36Sopenharmony_ci if (enqd_len + trb_buff_len > full_len) 365962306a36Sopenharmony_ci trb_buff_len = full_len - enqd_len; 366062306a36Sopenharmony_ci 366162306a36Sopenharmony_ci /* Don't change the cycle bit of the first TRB until later */ 366262306a36Sopenharmony_ci if (first_trb) { 366362306a36Sopenharmony_ci first_trb = false; 366462306a36Sopenharmony_ci if (start_cycle == 0) 366562306a36Sopenharmony_ci field |= TRB_CYCLE; 366662306a36Sopenharmony_ci } else 366762306a36Sopenharmony_ci field |= ring->cycle_state; 366862306a36Sopenharmony_ci 366962306a36Sopenharmony_ci /* Chain all the TRBs together; clear the chain bit in the last 367062306a36Sopenharmony_ci * TRB to indicate it's the last TRB in the chain. 367162306a36Sopenharmony_ci */ 367262306a36Sopenharmony_ci if (enqd_len + trb_buff_len < full_len) { 367362306a36Sopenharmony_ci field |= TRB_CHAIN; 367462306a36Sopenharmony_ci if (trb_is_link(ring->enqueue + 1)) { 367562306a36Sopenharmony_ci if (xhci_align_td(xhci, urb, enqd_len, 367662306a36Sopenharmony_ci &trb_buff_len, 367762306a36Sopenharmony_ci ring->enq_seg)) { 367862306a36Sopenharmony_ci send_addr = ring->enq_seg->bounce_dma; 367962306a36Sopenharmony_ci /* assuming TD won't span 2 segs */ 368062306a36Sopenharmony_ci td->bounce_seg = ring->enq_seg; 368162306a36Sopenharmony_ci } 368262306a36Sopenharmony_ci } 368362306a36Sopenharmony_ci } 368462306a36Sopenharmony_ci if (enqd_len + trb_buff_len >= full_len) { 368562306a36Sopenharmony_ci field &= ~TRB_CHAIN; 368662306a36Sopenharmony_ci field |= TRB_IOC; 368762306a36Sopenharmony_ci more_trbs_coming = false; 368862306a36Sopenharmony_ci td->last_trb = ring->enqueue; 368962306a36Sopenharmony_ci td->last_trb_seg = ring->enq_seg; 369062306a36Sopenharmony_ci if (xhci_urb_suitable_for_idt(urb)) { 369162306a36Sopenharmony_ci memcpy(&send_addr, urb->transfer_buffer, 369262306a36Sopenharmony_ci trb_buff_len); 369362306a36Sopenharmony_ci le64_to_cpus(&send_addr); 369462306a36Sopenharmony_ci field |= TRB_IDT; 369562306a36Sopenharmony_ci } 369662306a36Sopenharmony_ci } 369762306a36Sopenharmony_ci 369862306a36Sopenharmony_ci /* Only set interrupt on short packet for IN endpoints */ 369962306a36Sopenharmony_ci if (usb_urb_dir_in(urb)) 370062306a36Sopenharmony_ci field |= TRB_ISP; 370162306a36Sopenharmony_ci 370262306a36Sopenharmony_ci /* Set the TRB length, TD size, and interrupter fields. */ 370362306a36Sopenharmony_ci remainder = xhci_td_remainder(xhci, enqd_len, trb_buff_len, 370462306a36Sopenharmony_ci full_len, urb, more_trbs_coming); 370562306a36Sopenharmony_ci 370662306a36Sopenharmony_ci length_field = TRB_LEN(trb_buff_len) | 370762306a36Sopenharmony_ci TRB_TD_SIZE(remainder) | 370862306a36Sopenharmony_ci TRB_INTR_TARGET(0); 370962306a36Sopenharmony_ci 371062306a36Sopenharmony_ci queue_trb(xhci, ring, more_trbs_coming | need_zero_pkt, 371162306a36Sopenharmony_ci lower_32_bits(send_addr), 371262306a36Sopenharmony_ci upper_32_bits(send_addr), 371362306a36Sopenharmony_ci length_field, 371462306a36Sopenharmony_ci field); 371562306a36Sopenharmony_ci td->num_trbs++; 371662306a36Sopenharmony_ci addr += trb_buff_len; 371762306a36Sopenharmony_ci sent_len = trb_buff_len; 371862306a36Sopenharmony_ci 371962306a36Sopenharmony_ci while (sg && sent_len >= block_len) { 372062306a36Sopenharmony_ci /* New sg entry */ 372162306a36Sopenharmony_ci --num_sgs; 372262306a36Sopenharmony_ci sent_len -= block_len; 372362306a36Sopenharmony_ci sg = sg_next(sg); 372462306a36Sopenharmony_ci if (num_sgs != 0 && sg) { 372562306a36Sopenharmony_ci block_len = sg_dma_len(sg); 372662306a36Sopenharmony_ci addr = (u64) sg_dma_address(sg); 372762306a36Sopenharmony_ci addr += sent_len; 372862306a36Sopenharmony_ci } 372962306a36Sopenharmony_ci } 373062306a36Sopenharmony_ci block_len -= sent_len; 373162306a36Sopenharmony_ci send_addr = addr; 373262306a36Sopenharmony_ci } 373362306a36Sopenharmony_ci 373462306a36Sopenharmony_ci if (need_zero_pkt) { 373562306a36Sopenharmony_ci ret = prepare_transfer(xhci, xhci->devs[slot_id], 373662306a36Sopenharmony_ci ep_index, urb->stream_id, 373762306a36Sopenharmony_ci 1, urb, 1, mem_flags); 373862306a36Sopenharmony_ci urb_priv->td[1].last_trb = ring->enqueue; 373962306a36Sopenharmony_ci urb_priv->td[1].last_trb_seg = ring->enq_seg; 374062306a36Sopenharmony_ci field = TRB_TYPE(TRB_NORMAL) | ring->cycle_state | TRB_IOC; 374162306a36Sopenharmony_ci queue_trb(xhci, ring, 0, 0, 0, TRB_INTR_TARGET(0), field); 374262306a36Sopenharmony_ci urb_priv->td[1].num_trbs++; 374362306a36Sopenharmony_ci } 374462306a36Sopenharmony_ci 374562306a36Sopenharmony_ci check_trb_math(urb, enqd_len); 374662306a36Sopenharmony_ci giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id, 374762306a36Sopenharmony_ci start_cycle, start_trb); 374862306a36Sopenharmony_ci return 0; 374962306a36Sopenharmony_ci} 375062306a36Sopenharmony_ci 375162306a36Sopenharmony_ci/* Caller must have locked xhci->lock */ 375262306a36Sopenharmony_ciint xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags, 375362306a36Sopenharmony_ci struct urb *urb, int slot_id, unsigned int ep_index) 375462306a36Sopenharmony_ci{ 375562306a36Sopenharmony_ci struct xhci_ring *ep_ring; 375662306a36Sopenharmony_ci int num_trbs; 375762306a36Sopenharmony_ci int ret; 375862306a36Sopenharmony_ci struct usb_ctrlrequest *setup; 375962306a36Sopenharmony_ci struct xhci_generic_trb *start_trb; 376062306a36Sopenharmony_ci int start_cycle; 376162306a36Sopenharmony_ci u32 field; 376262306a36Sopenharmony_ci struct urb_priv *urb_priv; 376362306a36Sopenharmony_ci struct xhci_td *td; 376462306a36Sopenharmony_ci 376562306a36Sopenharmony_ci ep_ring = xhci_urb_to_transfer_ring(xhci, urb); 376662306a36Sopenharmony_ci if (!ep_ring) 376762306a36Sopenharmony_ci return -EINVAL; 376862306a36Sopenharmony_ci 376962306a36Sopenharmony_ci /* 377062306a36Sopenharmony_ci * Need to copy setup packet into setup TRB, so we can't use the setup 377162306a36Sopenharmony_ci * DMA address. 377262306a36Sopenharmony_ci */ 377362306a36Sopenharmony_ci if (!urb->setup_packet) 377462306a36Sopenharmony_ci return -EINVAL; 377562306a36Sopenharmony_ci 377662306a36Sopenharmony_ci /* 1 TRB for setup, 1 for status */ 377762306a36Sopenharmony_ci num_trbs = 2; 377862306a36Sopenharmony_ci /* 377962306a36Sopenharmony_ci * Don't need to check if we need additional event data and normal TRBs, 378062306a36Sopenharmony_ci * since data in control transfers will never get bigger than 16MB 378162306a36Sopenharmony_ci * XXX: can we get a buffer that crosses 64KB boundaries? 378262306a36Sopenharmony_ci */ 378362306a36Sopenharmony_ci if (urb->transfer_buffer_length > 0) 378462306a36Sopenharmony_ci num_trbs++; 378562306a36Sopenharmony_ci ret = prepare_transfer(xhci, xhci->devs[slot_id], 378662306a36Sopenharmony_ci ep_index, urb->stream_id, 378762306a36Sopenharmony_ci num_trbs, urb, 0, mem_flags); 378862306a36Sopenharmony_ci if (ret < 0) 378962306a36Sopenharmony_ci return ret; 379062306a36Sopenharmony_ci 379162306a36Sopenharmony_ci urb_priv = urb->hcpriv; 379262306a36Sopenharmony_ci td = &urb_priv->td[0]; 379362306a36Sopenharmony_ci td->num_trbs = num_trbs; 379462306a36Sopenharmony_ci 379562306a36Sopenharmony_ci /* 379662306a36Sopenharmony_ci * Don't give the first TRB to the hardware (by toggling the cycle bit) 379762306a36Sopenharmony_ci * until we've finished creating all the other TRBs. The ring's cycle 379862306a36Sopenharmony_ci * state may change as we enqueue the other TRBs, so save it too. 379962306a36Sopenharmony_ci */ 380062306a36Sopenharmony_ci start_trb = &ep_ring->enqueue->generic; 380162306a36Sopenharmony_ci start_cycle = ep_ring->cycle_state; 380262306a36Sopenharmony_ci 380362306a36Sopenharmony_ci /* Queue setup TRB - see section 6.4.1.2.1 */ 380462306a36Sopenharmony_ci /* FIXME better way to translate setup_packet into two u32 fields? */ 380562306a36Sopenharmony_ci setup = (struct usb_ctrlrequest *) urb->setup_packet; 380662306a36Sopenharmony_ci field = 0; 380762306a36Sopenharmony_ci field |= TRB_IDT | TRB_TYPE(TRB_SETUP); 380862306a36Sopenharmony_ci if (start_cycle == 0) 380962306a36Sopenharmony_ci field |= 0x1; 381062306a36Sopenharmony_ci 381162306a36Sopenharmony_ci /* xHCI 1.0/1.1 6.4.1.2.1: Transfer Type field */ 381262306a36Sopenharmony_ci if ((xhci->hci_version >= 0x100) || (xhci->quirks & XHCI_MTK_HOST)) { 381362306a36Sopenharmony_ci if (urb->transfer_buffer_length > 0) { 381462306a36Sopenharmony_ci if (setup->bRequestType & USB_DIR_IN) 381562306a36Sopenharmony_ci field |= TRB_TX_TYPE(TRB_DATA_IN); 381662306a36Sopenharmony_ci else 381762306a36Sopenharmony_ci field |= TRB_TX_TYPE(TRB_DATA_OUT); 381862306a36Sopenharmony_ci } 381962306a36Sopenharmony_ci } 382062306a36Sopenharmony_ci 382162306a36Sopenharmony_ci queue_trb(xhci, ep_ring, true, 382262306a36Sopenharmony_ci setup->bRequestType | setup->bRequest << 8 | le16_to_cpu(setup->wValue) << 16, 382362306a36Sopenharmony_ci le16_to_cpu(setup->wIndex) | le16_to_cpu(setup->wLength) << 16, 382462306a36Sopenharmony_ci TRB_LEN(8) | TRB_INTR_TARGET(0), 382562306a36Sopenharmony_ci /* Immediate data in pointer */ 382662306a36Sopenharmony_ci field); 382762306a36Sopenharmony_ci 382862306a36Sopenharmony_ci /* If there's data, queue data TRBs */ 382962306a36Sopenharmony_ci /* Only set interrupt on short packet for IN endpoints */ 383062306a36Sopenharmony_ci if (usb_urb_dir_in(urb)) 383162306a36Sopenharmony_ci field = TRB_ISP | TRB_TYPE(TRB_DATA); 383262306a36Sopenharmony_ci else 383362306a36Sopenharmony_ci field = TRB_TYPE(TRB_DATA); 383462306a36Sopenharmony_ci 383562306a36Sopenharmony_ci if (urb->transfer_buffer_length > 0) { 383662306a36Sopenharmony_ci u32 length_field, remainder; 383762306a36Sopenharmony_ci u64 addr; 383862306a36Sopenharmony_ci 383962306a36Sopenharmony_ci if (xhci_urb_suitable_for_idt(urb)) { 384062306a36Sopenharmony_ci memcpy(&addr, urb->transfer_buffer, 384162306a36Sopenharmony_ci urb->transfer_buffer_length); 384262306a36Sopenharmony_ci le64_to_cpus(&addr); 384362306a36Sopenharmony_ci field |= TRB_IDT; 384462306a36Sopenharmony_ci } else { 384562306a36Sopenharmony_ci addr = (u64) urb->transfer_dma; 384662306a36Sopenharmony_ci } 384762306a36Sopenharmony_ci 384862306a36Sopenharmony_ci remainder = xhci_td_remainder(xhci, 0, 384962306a36Sopenharmony_ci urb->transfer_buffer_length, 385062306a36Sopenharmony_ci urb->transfer_buffer_length, 385162306a36Sopenharmony_ci urb, 1); 385262306a36Sopenharmony_ci length_field = TRB_LEN(urb->transfer_buffer_length) | 385362306a36Sopenharmony_ci TRB_TD_SIZE(remainder) | 385462306a36Sopenharmony_ci TRB_INTR_TARGET(0); 385562306a36Sopenharmony_ci if (setup->bRequestType & USB_DIR_IN) 385662306a36Sopenharmony_ci field |= TRB_DIR_IN; 385762306a36Sopenharmony_ci queue_trb(xhci, ep_ring, true, 385862306a36Sopenharmony_ci lower_32_bits(addr), 385962306a36Sopenharmony_ci upper_32_bits(addr), 386062306a36Sopenharmony_ci length_field, 386162306a36Sopenharmony_ci field | ep_ring->cycle_state); 386262306a36Sopenharmony_ci } 386362306a36Sopenharmony_ci 386462306a36Sopenharmony_ci /* Save the DMA address of the last TRB in the TD */ 386562306a36Sopenharmony_ci td->last_trb = ep_ring->enqueue; 386662306a36Sopenharmony_ci td->last_trb_seg = ep_ring->enq_seg; 386762306a36Sopenharmony_ci 386862306a36Sopenharmony_ci /* Queue status TRB - see Table 7 and sections 4.11.2.2 and 6.4.1.2.3 */ 386962306a36Sopenharmony_ci /* If the device sent data, the status stage is an OUT transfer */ 387062306a36Sopenharmony_ci if (urb->transfer_buffer_length > 0 && setup->bRequestType & USB_DIR_IN) 387162306a36Sopenharmony_ci field = 0; 387262306a36Sopenharmony_ci else 387362306a36Sopenharmony_ci field = TRB_DIR_IN; 387462306a36Sopenharmony_ci queue_trb(xhci, ep_ring, false, 387562306a36Sopenharmony_ci 0, 387662306a36Sopenharmony_ci 0, 387762306a36Sopenharmony_ci TRB_INTR_TARGET(0), 387862306a36Sopenharmony_ci /* Event on completion */ 387962306a36Sopenharmony_ci field | TRB_IOC | TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state); 388062306a36Sopenharmony_ci 388162306a36Sopenharmony_ci giveback_first_trb(xhci, slot_id, ep_index, 0, 388262306a36Sopenharmony_ci start_cycle, start_trb); 388362306a36Sopenharmony_ci return 0; 388462306a36Sopenharmony_ci} 388562306a36Sopenharmony_ci 388662306a36Sopenharmony_ci/* 388762306a36Sopenharmony_ci * The transfer burst count field of the isochronous TRB defines the number of 388862306a36Sopenharmony_ci * bursts that are required to move all packets in this TD. Only SuperSpeed 388962306a36Sopenharmony_ci * devices can burst up to bMaxBurst number of packets per service interval. 389062306a36Sopenharmony_ci * This field is zero based, meaning a value of zero in the field means one 389162306a36Sopenharmony_ci * burst. Basically, for everything but SuperSpeed devices, this field will be 389262306a36Sopenharmony_ci * zero. Only xHCI 1.0 host controllers support this field. 389362306a36Sopenharmony_ci */ 389462306a36Sopenharmony_cistatic unsigned int xhci_get_burst_count(struct xhci_hcd *xhci, 389562306a36Sopenharmony_ci struct urb *urb, unsigned int total_packet_count) 389662306a36Sopenharmony_ci{ 389762306a36Sopenharmony_ci unsigned int max_burst; 389862306a36Sopenharmony_ci 389962306a36Sopenharmony_ci if (xhci->hci_version < 0x100 || urb->dev->speed < USB_SPEED_SUPER) 390062306a36Sopenharmony_ci return 0; 390162306a36Sopenharmony_ci 390262306a36Sopenharmony_ci max_burst = urb->ep->ss_ep_comp.bMaxBurst; 390362306a36Sopenharmony_ci return DIV_ROUND_UP(total_packet_count, max_burst + 1) - 1; 390462306a36Sopenharmony_ci} 390562306a36Sopenharmony_ci 390662306a36Sopenharmony_ci/* 390762306a36Sopenharmony_ci * Returns the number of packets in the last "burst" of packets. This field is 390862306a36Sopenharmony_ci * valid for all speeds of devices. USB 2.0 devices can only do one "burst", so 390962306a36Sopenharmony_ci * the last burst packet count is equal to the total number of packets in the 391062306a36Sopenharmony_ci * TD. SuperSpeed endpoints can have up to 3 bursts. All but the last burst 391162306a36Sopenharmony_ci * must contain (bMaxBurst + 1) number of packets, but the last burst can 391262306a36Sopenharmony_ci * contain 1 to (bMaxBurst + 1) packets. 391362306a36Sopenharmony_ci */ 391462306a36Sopenharmony_cistatic unsigned int xhci_get_last_burst_packet_count(struct xhci_hcd *xhci, 391562306a36Sopenharmony_ci struct urb *urb, unsigned int total_packet_count) 391662306a36Sopenharmony_ci{ 391762306a36Sopenharmony_ci unsigned int max_burst; 391862306a36Sopenharmony_ci unsigned int residue; 391962306a36Sopenharmony_ci 392062306a36Sopenharmony_ci if (xhci->hci_version < 0x100) 392162306a36Sopenharmony_ci return 0; 392262306a36Sopenharmony_ci 392362306a36Sopenharmony_ci if (urb->dev->speed >= USB_SPEED_SUPER) { 392462306a36Sopenharmony_ci /* bMaxBurst is zero based: 0 means 1 packet per burst */ 392562306a36Sopenharmony_ci max_burst = urb->ep->ss_ep_comp.bMaxBurst; 392662306a36Sopenharmony_ci residue = total_packet_count % (max_burst + 1); 392762306a36Sopenharmony_ci /* If residue is zero, the last burst contains (max_burst + 1) 392862306a36Sopenharmony_ci * number of packets, but the TLBPC field is zero-based. 392962306a36Sopenharmony_ci */ 393062306a36Sopenharmony_ci if (residue == 0) 393162306a36Sopenharmony_ci return max_burst; 393262306a36Sopenharmony_ci return residue - 1; 393362306a36Sopenharmony_ci } 393462306a36Sopenharmony_ci if (total_packet_count == 0) 393562306a36Sopenharmony_ci return 0; 393662306a36Sopenharmony_ci return total_packet_count - 1; 393762306a36Sopenharmony_ci} 393862306a36Sopenharmony_ci 393962306a36Sopenharmony_ci/* 394062306a36Sopenharmony_ci * Calculates Frame ID field of the isochronous TRB identifies the 394162306a36Sopenharmony_ci * target frame that the Interval associated with this Isochronous 394262306a36Sopenharmony_ci * Transfer Descriptor will start on. Refer to 4.11.2.5 in 1.1 spec. 394362306a36Sopenharmony_ci * 394462306a36Sopenharmony_ci * Returns actual frame id on success, negative value on error. 394562306a36Sopenharmony_ci */ 394662306a36Sopenharmony_cistatic int xhci_get_isoc_frame_id(struct xhci_hcd *xhci, 394762306a36Sopenharmony_ci struct urb *urb, int index) 394862306a36Sopenharmony_ci{ 394962306a36Sopenharmony_ci int start_frame, ist, ret = 0; 395062306a36Sopenharmony_ci int start_frame_id, end_frame_id, current_frame_id; 395162306a36Sopenharmony_ci 395262306a36Sopenharmony_ci if (urb->dev->speed == USB_SPEED_LOW || 395362306a36Sopenharmony_ci urb->dev->speed == USB_SPEED_FULL) 395462306a36Sopenharmony_ci start_frame = urb->start_frame + index * urb->interval; 395562306a36Sopenharmony_ci else 395662306a36Sopenharmony_ci start_frame = (urb->start_frame + index * urb->interval) >> 3; 395762306a36Sopenharmony_ci 395862306a36Sopenharmony_ci /* Isochronous Scheduling Threshold (IST, bits 0~3 in HCSPARAMS2): 395962306a36Sopenharmony_ci * 396062306a36Sopenharmony_ci * If bit [3] of IST is cleared to '0', software can add a TRB no 396162306a36Sopenharmony_ci * later than IST[2:0] Microframes before that TRB is scheduled to 396262306a36Sopenharmony_ci * be executed. 396362306a36Sopenharmony_ci * If bit [3] of IST is set to '1', software can add a TRB no later 396462306a36Sopenharmony_ci * than IST[2:0] Frames before that TRB is scheduled to be executed. 396562306a36Sopenharmony_ci */ 396662306a36Sopenharmony_ci ist = HCS_IST(xhci->hcs_params2) & 0x7; 396762306a36Sopenharmony_ci if (HCS_IST(xhci->hcs_params2) & (1 << 3)) 396862306a36Sopenharmony_ci ist <<= 3; 396962306a36Sopenharmony_ci 397062306a36Sopenharmony_ci /* Software shall not schedule an Isoch TD with a Frame ID value that 397162306a36Sopenharmony_ci * is less than the Start Frame ID or greater than the End Frame ID, 397262306a36Sopenharmony_ci * where: 397362306a36Sopenharmony_ci * 397462306a36Sopenharmony_ci * End Frame ID = (Current MFINDEX register value + 895 ms.) MOD 2048 397562306a36Sopenharmony_ci * Start Frame ID = (Current MFINDEX register value + IST + 1) MOD 2048 397662306a36Sopenharmony_ci * 397762306a36Sopenharmony_ci * Both the End Frame ID and Start Frame ID values are calculated 397862306a36Sopenharmony_ci * in microframes. When software determines the valid Frame ID value; 397962306a36Sopenharmony_ci * The End Frame ID value should be rounded down to the nearest Frame 398062306a36Sopenharmony_ci * boundary, and the Start Frame ID value should be rounded up to the 398162306a36Sopenharmony_ci * nearest Frame boundary. 398262306a36Sopenharmony_ci */ 398362306a36Sopenharmony_ci current_frame_id = readl(&xhci->run_regs->microframe_index); 398462306a36Sopenharmony_ci start_frame_id = roundup(current_frame_id + ist + 1, 8); 398562306a36Sopenharmony_ci end_frame_id = rounddown(current_frame_id + 895 * 8, 8); 398662306a36Sopenharmony_ci 398762306a36Sopenharmony_ci start_frame &= 0x7ff; 398862306a36Sopenharmony_ci start_frame_id = (start_frame_id >> 3) & 0x7ff; 398962306a36Sopenharmony_ci end_frame_id = (end_frame_id >> 3) & 0x7ff; 399062306a36Sopenharmony_ci 399162306a36Sopenharmony_ci xhci_dbg(xhci, "%s: index %d, reg 0x%x start_frame_id 0x%x, end_frame_id 0x%x, start_frame 0x%x\n", 399262306a36Sopenharmony_ci __func__, index, readl(&xhci->run_regs->microframe_index), 399362306a36Sopenharmony_ci start_frame_id, end_frame_id, start_frame); 399462306a36Sopenharmony_ci 399562306a36Sopenharmony_ci if (start_frame_id < end_frame_id) { 399662306a36Sopenharmony_ci if (start_frame > end_frame_id || 399762306a36Sopenharmony_ci start_frame < start_frame_id) 399862306a36Sopenharmony_ci ret = -EINVAL; 399962306a36Sopenharmony_ci } else if (start_frame_id > end_frame_id) { 400062306a36Sopenharmony_ci if ((start_frame > end_frame_id && 400162306a36Sopenharmony_ci start_frame < start_frame_id)) 400262306a36Sopenharmony_ci ret = -EINVAL; 400362306a36Sopenharmony_ci } else { 400462306a36Sopenharmony_ci ret = -EINVAL; 400562306a36Sopenharmony_ci } 400662306a36Sopenharmony_ci 400762306a36Sopenharmony_ci if (index == 0) { 400862306a36Sopenharmony_ci if (ret == -EINVAL || start_frame == start_frame_id) { 400962306a36Sopenharmony_ci start_frame = start_frame_id + 1; 401062306a36Sopenharmony_ci if (urb->dev->speed == USB_SPEED_LOW || 401162306a36Sopenharmony_ci urb->dev->speed == USB_SPEED_FULL) 401262306a36Sopenharmony_ci urb->start_frame = start_frame; 401362306a36Sopenharmony_ci else 401462306a36Sopenharmony_ci urb->start_frame = start_frame << 3; 401562306a36Sopenharmony_ci ret = 0; 401662306a36Sopenharmony_ci } 401762306a36Sopenharmony_ci } 401862306a36Sopenharmony_ci 401962306a36Sopenharmony_ci if (ret) { 402062306a36Sopenharmony_ci xhci_warn(xhci, "Frame ID %d (reg %d, index %d) beyond range (%d, %d)\n", 402162306a36Sopenharmony_ci start_frame, current_frame_id, index, 402262306a36Sopenharmony_ci start_frame_id, end_frame_id); 402362306a36Sopenharmony_ci xhci_warn(xhci, "Ignore frame ID field, use SIA bit instead\n"); 402462306a36Sopenharmony_ci return ret; 402562306a36Sopenharmony_ci } 402662306a36Sopenharmony_ci 402762306a36Sopenharmony_ci return start_frame; 402862306a36Sopenharmony_ci} 402962306a36Sopenharmony_ci 403062306a36Sopenharmony_ci/* Check if we should generate event interrupt for a TD in an isoc URB */ 403162306a36Sopenharmony_cistatic bool trb_block_event_intr(struct xhci_hcd *xhci, int num_tds, int i) 403262306a36Sopenharmony_ci{ 403362306a36Sopenharmony_ci if (xhci->hci_version < 0x100) 403462306a36Sopenharmony_ci return false; 403562306a36Sopenharmony_ci /* always generate an event interrupt for the last TD */ 403662306a36Sopenharmony_ci if (i == num_tds - 1) 403762306a36Sopenharmony_ci return false; 403862306a36Sopenharmony_ci /* 403962306a36Sopenharmony_ci * If AVOID_BEI is set the host handles full event rings poorly, 404062306a36Sopenharmony_ci * generate an event at least every 8th TD to clear the event ring 404162306a36Sopenharmony_ci */ 404262306a36Sopenharmony_ci if (i && xhci->quirks & XHCI_AVOID_BEI) 404362306a36Sopenharmony_ci return !!(i % xhci->isoc_bei_interval); 404462306a36Sopenharmony_ci 404562306a36Sopenharmony_ci return true; 404662306a36Sopenharmony_ci} 404762306a36Sopenharmony_ci 404862306a36Sopenharmony_ci/* This is for isoc transfer */ 404962306a36Sopenharmony_cistatic int xhci_queue_isoc_tx(struct xhci_hcd *xhci, gfp_t mem_flags, 405062306a36Sopenharmony_ci struct urb *urb, int slot_id, unsigned int ep_index) 405162306a36Sopenharmony_ci{ 405262306a36Sopenharmony_ci struct xhci_ring *ep_ring; 405362306a36Sopenharmony_ci struct urb_priv *urb_priv; 405462306a36Sopenharmony_ci struct xhci_td *td; 405562306a36Sopenharmony_ci int num_tds, trbs_per_td; 405662306a36Sopenharmony_ci struct xhci_generic_trb *start_trb; 405762306a36Sopenharmony_ci bool first_trb; 405862306a36Sopenharmony_ci int start_cycle; 405962306a36Sopenharmony_ci u32 field, length_field; 406062306a36Sopenharmony_ci int running_total, trb_buff_len, td_len, td_remain_len, ret; 406162306a36Sopenharmony_ci u64 start_addr, addr; 406262306a36Sopenharmony_ci int i, j; 406362306a36Sopenharmony_ci bool more_trbs_coming; 406462306a36Sopenharmony_ci struct xhci_virt_ep *xep; 406562306a36Sopenharmony_ci int frame_id; 406662306a36Sopenharmony_ci 406762306a36Sopenharmony_ci xep = &xhci->devs[slot_id]->eps[ep_index]; 406862306a36Sopenharmony_ci ep_ring = xhci->devs[slot_id]->eps[ep_index].ring; 406962306a36Sopenharmony_ci 407062306a36Sopenharmony_ci num_tds = urb->number_of_packets; 407162306a36Sopenharmony_ci if (num_tds < 1) { 407262306a36Sopenharmony_ci xhci_dbg(xhci, "Isoc URB with zero packets?\n"); 407362306a36Sopenharmony_ci return -EINVAL; 407462306a36Sopenharmony_ci } 407562306a36Sopenharmony_ci start_addr = (u64) urb->transfer_dma; 407662306a36Sopenharmony_ci start_trb = &ep_ring->enqueue->generic; 407762306a36Sopenharmony_ci start_cycle = ep_ring->cycle_state; 407862306a36Sopenharmony_ci 407962306a36Sopenharmony_ci urb_priv = urb->hcpriv; 408062306a36Sopenharmony_ci /* Queue the TRBs for each TD, even if they are zero-length */ 408162306a36Sopenharmony_ci for (i = 0; i < num_tds; i++) { 408262306a36Sopenharmony_ci unsigned int total_pkt_count, max_pkt; 408362306a36Sopenharmony_ci unsigned int burst_count, last_burst_pkt_count; 408462306a36Sopenharmony_ci u32 sia_frame_id; 408562306a36Sopenharmony_ci 408662306a36Sopenharmony_ci first_trb = true; 408762306a36Sopenharmony_ci running_total = 0; 408862306a36Sopenharmony_ci addr = start_addr + urb->iso_frame_desc[i].offset; 408962306a36Sopenharmony_ci td_len = urb->iso_frame_desc[i].length; 409062306a36Sopenharmony_ci td_remain_len = td_len; 409162306a36Sopenharmony_ci max_pkt = usb_endpoint_maxp(&urb->ep->desc); 409262306a36Sopenharmony_ci total_pkt_count = DIV_ROUND_UP(td_len, max_pkt); 409362306a36Sopenharmony_ci 409462306a36Sopenharmony_ci /* A zero-length transfer still involves at least one packet. */ 409562306a36Sopenharmony_ci if (total_pkt_count == 0) 409662306a36Sopenharmony_ci total_pkt_count++; 409762306a36Sopenharmony_ci burst_count = xhci_get_burst_count(xhci, urb, total_pkt_count); 409862306a36Sopenharmony_ci last_burst_pkt_count = xhci_get_last_burst_packet_count(xhci, 409962306a36Sopenharmony_ci urb, total_pkt_count); 410062306a36Sopenharmony_ci 410162306a36Sopenharmony_ci trbs_per_td = count_isoc_trbs_needed(urb, i); 410262306a36Sopenharmony_ci 410362306a36Sopenharmony_ci ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index, 410462306a36Sopenharmony_ci urb->stream_id, trbs_per_td, urb, i, mem_flags); 410562306a36Sopenharmony_ci if (ret < 0) { 410662306a36Sopenharmony_ci if (i == 0) 410762306a36Sopenharmony_ci return ret; 410862306a36Sopenharmony_ci goto cleanup; 410962306a36Sopenharmony_ci } 411062306a36Sopenharmony_ci td = &urb_priv->td[i]; 411162306a36Sopenharmony_ci td->num_trbs = trbs_per_td; 411262306a36Sopenharmony_ci /* use SIA as default, if frame id is used overwrite it */ 411362306a36Sopenharmony_ci sia_frame_id = TRB_SIA; 411462306a36Sopenharmony_ci if (!(urb->transfer_flags & URB_ISO_ASAP) && 411562306a36Sopenharmony_ci HCC_CFC(xhci->hcc_params)) { 411662306a36Sopenharmony_ci frame_id = xhci_get_isoc_frame_id(xhci, urb, i); 411762306a36Sopenharmony_ci if (frame_id >= 0) 411862306a36Sopenharmony_ci sia_frame_id = TRB_FRAME_ID(frame_id); 411962306a36Sopenharmony_ci } 412062306a36Sopenharmony_ci /* 412162306a36Sopenharmony_ci * Set isoc specific data for the first TRB in a TD. 412262306a36Sopenharmony_ci * Prevent HW from getting the TRBs by keeping the cycle state 412362306a36Sopenharmony_ci * inverted in the first TDs isoc TRB. 412462306a36Sopenharmony_ci */ 412562306a36Sopenharmony_ci field = TRB_TYPE(TRB_ISOC) | 412662306a36Sopenharmony_ci TRB_TLBPC(last_burst_pkt_count) | 412762306a36Sopenharmony_ci sia_frame_id | 412862306a36Sopenharmony_ci (i ? ep_ring->cycle_state : !start_cycle); 412962306a36Sopenharmony_ci 413062306a36Sopenharmony_ci /* xhci 1.1 with ETE uses TD_Size field for TBC, old is Rsvdz */ 413162306a36Sopenharmony_ci if (!xep->use_extended_tbc) 413262306a36Sopenharmony_ci field |= TRB_TBC(burst_count); 413362306a36Sopenharmony_ci 413462306a36Sopenharmony_ci /* fill the rest of the TRB fields, and remaining normal TRBs */ 413562306a36Sopenharmony_ci for (j = 0; j < trbs_per_td; j++) { 413662306a36Sopenharmony_ci u32 remainder = 0; 413762306a36Sopenharmony_ci 413862306a36Sopenharmony_ci /* only first TRB is isoc, overwrite otherwise */ 413962306a36Sopenharmony_ci if (!first_trb) 414062306a36Sopenharmony_ci field = TRB_TYPE(TRB_NORMAL) | 414162306a36Sopenharmony_ci ep_ring->cycle_state; 414262306a36Sopenharmony_ci 414362306a36Sopenharmony_ci /* Only set interrupt on short packet for IN EPs */ 414462306a36Sopenharmony_ci if (usb_urb_dir_in(urb)) 414562306a36Sopenharmony_ci field |= TRB_ISP; 414662306a36Sopenharmony_ci 414762306a36Sopenharmony_ci /* Set the chain bit for all except the last TRB */ 414862306a36Sopenharmony_ci if (j < trbs_per_td - 1) { 414962306a36Sopenharmony_ci more_trbs_coming = true; 415062306a36Sopenharmony_ci field |= TRB_CHAIN; 415162306a36Sopenharmony_ci } else { 415262306a36Sopenharmony_ci more_trbs_coming = false; 415362306a36Sopenharmony_ci td->last_trb = ep_ring->enqueue; 415462306a36Sopenharmony_ci td->last_trb_seg = ep_ring->enq_seg; 415562306a36Sopenharmony_ci field |= TRB_IOC; 415662306a36Sopenharmony_ci if (trb_block_event_intr(xhci, num_tds, i)) 415762306a36Sopenharmony_ci field |= TRB_BEI; 415862306a36Sopenharmony_ci } 415962306a36Sopenharmony_ci /* Calculate TRB length */ 416062306a36Sopenharmony_ci trb_buff_len = TRB_BUFF_LEN_UP_TO_BOUNDARY(addr); 416162306a36Sopenharmony_ci if (trb_buff_len > td_remain_len) 416262306a36Sopenharmony_ci trb_buff_len = td_remain_len; 416362306a36Sopenharmony_ci 416462306a36Sopenharmony_ci /* Set the TRB length, TD size, & interrupter fields. */ 416562306a36Sopenharmony_ci remainder = xhci_td_remainder(xhci, running_total, 416662306a36Sopenharmony_ci trb_buff_len, td_len, 416762306a36Sopenharmony_ci urb, more_trbs_coming); 416862306a36Sopenharmony_ci 416962306a36Sopenharmony_ci length_field = TRB_LEN(trb_buff_len) | 417062306a36Sopenharmony_ci TRB_INTR_TARGET(0); 417162306a36Sopenharmony_ci 417262306a36Sopenharmony_ci /* xhci 1.1 with ETE uses TD Size field for TBC */ 417362306a36Sopenharmony_ci if (first_trb && xep->use_extended_tbc) 417462306a36Sopenharmony_ci length_field |= TRB_TD_SIZE_TBC(burst_count); 417562306a36Sopenharmony_ci else 417662306a36Sopenharmony_ci length_field |= TRB_TD_SIZE(remainder); 417762306a36Sopenharmony_ci first_trb = false; 417862306a36Sopenharmony_ci 417962306a36Sopenharmony_ci queue_trb(xhci, ep_ring, more_trbs_coming, 418062306a36Sopenharmony_ci lower_32_bits(addr), 418162306a36Sopenharmony_ci upper_32_bits(addr), 418262306a36Sopenharmony_ci length_field, 418362306a36Sopenharmony_ci field); 418462306a36Sopenharmony_ci running_total += trb_buff_len; 418562306a36Sopenharmony_ci 418662306a36Sopenharmony_ci addr += trb_buff_len; 418762306a36Sopenharmony_ci td_remain_len -= trb_buff_len; 418862306a36Sopenharmony_ci } 418962306a36Sopenharmony_ci 419062306a36Sopenharmony_ci /* Check TD length */ 419162306a36Sopenharmony_ci if (running_total != td_len) { 419262306a36Sopenharmony_ci xhci_err(xhci, "ISOC TD length unmatch\n"); 419362306a36Sopenharmony_ci ret = -EINVAL; 419462306a36Sopenharmony_ci goto cleanup; 419562306a36Sopenharmony_ci } 419662306a36Sopenharmony_ci } 419762306a36Sopenharmony_ci 419862306a36Sopenharmony_ci /* store the next frame id */ 419962306a36Sopenharmony_ci if (HCC_CFC(xhci->hcc_params)) 420062306a36Sopenharmony_ci xep->next_frame_id = urb->start_frame + num_tds * urb->interval; 420162306a36Sopenharmony_ci 420262306a36Sopenharmony_ci if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs == 0) { 420362306a36Sopenharmony_ci if (xhci->quirks & XHCI_AMD_PLL_FIX) 420462306a36Sopenharmony_ci usb_amd_quirk_pll_disable(); 420562306a36Sopenharmony_ci } 420662306a36Sopenharmony_ci xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs++; 420762306a36Sopenharmony_ci 420862306a36Sopenharmony_ci giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id, 420962306a36Sopenharmony_ci start_cycle, start_trb); 421062306a36Sopenharmony_ci return 0; 421162306a36Sopenharmony_cicleanup: 421262306a36Sopenharmony_ci /* Clean up a partially enqueued isoc transfer. */ 421362306a36Sopenharmony_ci 421462306a36Sopenharmony_ci for (i--; i >= 0; i--) 421562306a36Sopenharmony_ci list_del_init(&urb_priv->td[i].td_list); 421662306a36Sopenharmony_ci 421762306a36Sopenharmony_ci /* Use the first TD as a temporary variable to turn the TDs we've queued 421862306a36Sopenharmony_ci * into No-ops with a software-owned cycle bit. That way the hardware 421962306a36Sopenharmony_ci * won't accidentally start executing bogus TDs when we partially 422062306a36Sopenharmony_ci * overwrite them. td->first_trb and td->start_seg are already set. 422162306a36Sopenharmony_ci */ 422262306a36Sopenharmony_ci urb_priv->td[0].last_trb = ep_ring->enqueue; 422362306a36Sopenharmony_ci /* Every TRB except the first & last will have its cycle bit flipped. */ 422462306a36Sopenharmony_ci td_to_noop(xhci, ep_ring, &urb_priv->td[0], true); 422562306a36Sopenharmony_ci 422662306a36Sopenharmony_ci /* Reset the ring enqueue back to the first TRB and its cycle bit. */ 422762306a36Sopenharmony_ci ep_ring->enqueue = urb_priv->td[0].first_trb; 422862306a36Sopenharmony_ci ep_ring->enq_seg = urb_priv->td[0].start_seg; 422962306a36Sopenharmony_ci ep_ring->cycle_state = start_cycle; 423062306a36Sopenharmony_ci usb_hcd_unlink_urb_from_ep(bus_to_hcd(urb->dev->bus), urb); 423162306a36Sopenharmony_ci return ret; 423262306a36Sopenharmony_ci} 423362306a36Sopenharmony_ci 423462306a36Sopenharmony_ci/* 423562306a36Sopenharmony_ci * Check transfer ring to guarantee there is enough room for the urb. 423662306a36Sopenharmony_ci * Update ISO URB start_frame and interval. 423762306a36Sopenharmony_ci * Update interval as xhci_queue_intr_tx does. Use xhci frame_index to 423862306a36Sopenharmony_ci * update urb->start_frame if URB_ISO_ASAP is set in transfer_flags or 423962306a36Sopenharmony_ci * Contiguous Frame ID is not supported by HC. 424062306a36Sopenharmony_ci */ 424162306a36Sopenharmony_ciint xhci_queue_isoc_tx_prepare(struct xhci_hcd *xhci, gfp_t mem_flags, 424262306a36Sopenharmony_ci struct urb *urb, int slot_id, unsigned int ep_index) 424362306a36Sopenharmony_ci{ 424462306a36Sopenharmony_ci struct xhci_virt_device *xdev; 424562306a36Sopenharmony_ci struct xhci_ring *ep_ring; 424662306a36Sopenharmony_ci struct xhci_ep_ctx *ep_ctx; 424762306a36Sopenharmony_ci int start_frame; 424862306a36Sopenharmony_ci int num_tds, num_trbs, i; 424962306a36Sopenharmony_ci int ret; 425062306a36Sopenharmony_ci struct xhci_virt_ep *xep; 425162306a36Sopenharmony_ci int ist; 425262306a36Sopenharmony_ci 425362306a36Sopenharmony_ci xdev = xhci->devs[slot_id]; 425462306a36Sopenharmony_ci xep = &xhci->devs[slot_id]->eps[ep_index]; 425562306a36Sopenharmony_ci ep_ring = xdev->eps[ep_index].ring; 425662306a36Sopenharmony_ci ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index); 425762306a36Sopenharmony_ci 425862306a36Sopenharmony_ci num_trbs = 0; 425962306a36Sopenharmony_ci num_tds = urb->number_of_packets; 426062306a36Sopenharmony_ci for (i = 0; i < num_tds; i++) 426162306a36Sopenharmony_ci num_trbs += count_isoc_trbs_needed(urb, i); 426262306a36Sopenharmony_ci 426362306a36Sopenharmony_ci /* Check the ring to guarantee there is enough room for the whole urb. 426462306a36Sopenharmony_ci * Do not insert any td of the urb to the ring if the check failed. 426562306a36Sopenharmony_ci */ 426662306a36Sopenharmony_ci ret = prepare_ring(xhci, ep_ring, GET_EP_CTX_STATE(ep_ctx), 426762306a36Sopenharmony_ci num_trbs, mem_flags); 426862306a36Sopenharmony_ci if (ret) 426962306a36Sopenharmony_ci return ret; 427062306a36Sopenharmony_ci 427162306a36Sopenharmony_ci /* 427262306a36Sopenharmony_ci * Check interval value. This should be done before we start to 427362306a36Sopenharmony_ci * calculate the start frame value. 427462306a36Sopenharmony_ci */ 427562306a36Sopenharmony_ci check_interval(xhci, urb, ep_ctx); 427662306a36Sopenharmony_ci 427762306a36Sopenharmony_ci /* Calculate the start frame and put it in urb->start_frame. */ 427862306a36Sopenharmony_ci if (HCC_CFC(xhci->hcc_params) && !list_empty(&ep_ring->td_list)) { 427962306a36Sopenharmony_ci if (GET_EP_CTX_STATE(ep_ctx) == EP_STATE_RUNNING) { 428062306a36Sopenharmony_ci urb->start_frame = xep->next_frame_id; 428162306a36Sopenharmony_ci goto skip_start_over; 428262306a36Sopenharmony_ci } 428362306a36Sopenharmony_ci } 428462306a36Sopenharmony_ci 428562306a36Sopenharmony_ci start_frame = readl(&xhci->run_regs->microframe_index); 428662306a36Sopenharmony_ci start_frame &= 0x3fff; 428762306a36Sopenharmony_ci /* 428862306a36Sopenharmony_ci * Round up to the next frame and consider the time before trb really 428962306a36Sopenharmony_ci * gets scheduled by hardare. 429062306a36Sopenharmony_ci */ 429162306a36Sopenharmony_ci ist = HCS_IST(xhci->hcs_params2) & 0x7; 429262306a36Sopenharmony_ci if (HCS_IST(xhci->hcs_params2) & (1 << 3)) 429362306a36Sopenharmony_ci ist <<= 3; 429462306a36Sopenharmony_ci start_frame += ist + XHCI_CFC_DELAY; 429562306a36Sopenharmony_ci start_frame = roundup(start_frame, 8); 429662306a36Sopenharmony_ci 429762306a36Sopenharmony_ci /* 429862306a36Sopenharmony_ci * Round up to the next ESIT (Endpoint Service Interval Time) if ESIT 429962306a36Sopenharmony_ci * is greate than 8 microframes. 430062306a36Sopenharmony_ci */ 430162306a36Sopenharmony_ci if (urb->dev->speed == USB_SPEED_LOW || 430262306a36Sopenharmony_ci urb->dev->speed == USB_SPEED_FULL) { 430362306a36Sopenharmony_ci start_frame = roundup(start_frame, urb->interval << 3); 430462306a36Sopenharmony_ci urb->start_frame = start_frame >> 3; 430562306a36Sopenharmony_ci } else { 430662306a36Sopenharmony_ci start_frame = roundup(start_frame, urb->interval); 430762306a36Sopenharmony_ci urb->start_frame = start_frame; 430862306a36Sopenharmony_ci } 430962306a36Sopenharmony_ci 431062306a36Sopenharmony_ciskip_start_over: 431162306a36Sopenharmony_ci 431262306a36Sopenharmony_ci return xhci_queue_isoc_tx(xhci, mem_flags, urb, slot_id, ep_index); 431362306a36Sopenharmony_ci} 431462306a36Sopenharmony_ci 431562306a36Sopenharmony_ci/**** Command Ring Operations ****/ 431662306a36Sopenharmony_ci 431762306a36Sopenharmony_ci/* Generic function for queueing a command TRB on the command ring. 431862306a36Sopenharmony_ci * Check to make sure there's room on the command ring for one command TRB. 431962306a36Sopenharmony_ci * Also check that there's room reserved for commands that must not fail. 432062306a36Sopenharmony_ci * If this is a command that must not fail, meaning command_must_succeed = TRUE, 432162306a36Sopenharmony_ci * then only check for the number of reserved spots. 432262306a36Sopenharmony_ci * Don't decrement xhci->cmd_ring_reserved_trbs after we've queued the TRB 432362306a36Sopenharmony_ci * because the command event handler may want to resubmit a failed command. 432462306a36Sopenharmony_ci */ 432562306a36Sopenharmony_cistatic int queue_command(struct xhci_hcd *xhci, struct xhci_command *cmd, 432662306a36Sopenharmony_ci u32 field1, u32 field2, 432762306a36Sopenharmony_ci u32 field3, u32 field4, bool command_must_succeed) 432862306a36Sopenharmony_ci{ 432962306a36Sopenharmony_ci int reserved_trbs = xhci->cmd_ring_reserved_trbs; 433062306a36Sopenharmony_ci int ret; 433162306a36Sopenharmony_ci 433262306a36Sopenharmony_ci if ((xhci->xhc_state & XHCI_STATE_DYING) || 433362306a36Sopenharmony_ci (xhci->xhc_state & XHCI_STATE_HALTED)) { 433462306a36Sopenharmony_ci xhci_dbg(xhci, "xHCI dying or halted, can't queue_command\n"); 433562306a36Sopenharmony_ci return -ESHUTDOWN; 433662306a36Sopenharmony_ci } 433762306a36Sopenharmony_ci 433862306a36Sopenharmony_ci if (!command_must_succeed) 433962306a36Sopenharmony_ci reserved_trbs++; 434062306a36Sopenharmony_ci 434162306a36Sopenharmony_ci ret = prepare_ring(xhci, xhci->cmd_ring, EP_STATE_RUNNING, 434262306a36Sopenharmony_ci reserved_trbs, GFP_ATOMIC); 434362306a36Sopenharmony_ci if (ret < 0) { 434462306a36Sopenharmony_ci xhci_err(xhci, "ERR: No room for command on command ring\n"); 434562306a36Sopenharmony_ci if (command_must_succeed) 434662306a36Sopenharmony_ci xhci_err(xhci, "ERR: Reserved TRB counting for " 434762306a36Sopenharmony_ci "unfailable commands failed.\n"); 434862306a36Sopenharmony_ci return ret; 434962306a36Sopenharmony_ci } 435062306a36Sopenharmony_ci 435162306a36Sopenharmony_ci cmd->command_trb = xhci->cmd_ring->enqueue; 435262306a36Sopenharmony_ci 435362306a36Sopenharmony_ci /* if there are no other commands queued we start the timeout timer */ 435462306a36Sopenharmony_ci if (list_empty(&xhci->cmd_list)) { 435562306a36Sopenharmony_ci xhci->current_cmd = cmd; 435662306a36Sopenharmony_ci xhci_mod_cmd_timer(xhci, XHCI_CMD_DEFAULT_TIMEOUT); 435762306a36Sopenharmony_ci } 435862306a36Sopenharmony_ci 435962306a36Sopenharmony_ci list_add_tail(&cmd->cmd_list, &xhci->cmd_list); 436062306a36Sopenharmony_ci 436162306a36Sopenharmony_ci queue_trb(xhci, xhci->cmd_ring, false, field1, field2, field3, 436262306a36Sopenharmony_ci field4 | xhci->cmd_ring->cycle_state); 436362306a36Sopenharmony_ci return 0; 436462306a36Sopenharmony_ci} 436562306a36Sopenharmony_ci 436662306a36Sopenharmony_ci/* Queue a slot enable or disable request on the command ring */ 436762306a36Sopenharmony_ciint xhci_queue_slot_control(struct xhci_hcd *xhci, struct xhci_command *cmd, 436862306a36Sopenharmony_ci u32 trb_type, u32 slot_id) 436962306a36Sopenharmony_ci{ 437062306a36Sopenharmony_ci return queue_command(xhci, cmd, 0, 0, 0, 437162306a36Sopenharmony_ci TRB_TYPE(trb_type) | SLOT_ID_FOR_TRB(slot_id), false); 437262306a36Sopenharmony_ci} 437362306a36Sopenharmony_ci 437462306a36Sopenharmony_ci/* Queue an address device command TRB */ 437562306a36Sopenharmony_ciint xhci_queue_address_device(struct xhci_hcd *xhci, struct xhci_command *cmd, 437662306a36Sopenharmony_ci dma_addr_t in_ctx_ptr, u32 slot_id, enum xhci_setup_dev setup) 437762306a36Sopenharmony_ci{ 437862306a36Sopenharmony_ci return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr), 437962306a36Sopenharmony_ci upper_32_bits(in_ctx_ptr), 0, 438062306a36Sopenharmony_ci TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id) 438162306a36Sopenharmony_ci | (setup == SETUP_CONTEXT_ONLY ? TRB_BSR : 0), false); 438262306a36Sopenharmony_ci} 438362306a36Sopenharmony_ci 438462306a36Sopenharmony_ciint xhci_queue_vendor_command(struct xhci_hcd *xhci, struct xhci_command *cmd, 438562306a36Sopenharmony_ci u32 field1, u32 field2, u32 field3, u32 field4) 438662306a36Sopenharmony_ci{ 438762306a36Sopenharmony_ci return queue_command(xhci, cmd, field1, field2, field3, field4, false); 438862306a36Sopenharmony_ci} 438962306a36Sopenharmony_ci 439062306a36Sopenharmony_ci/* Queue a reset device command TRB */ 439162306a36Sopenharmony_ciint xhci_queue_reset_device(struct xhci_hcd *xhci, struct xhci_command *cmd, 439262306a36Sopenharmony_ci u32 slot_id) 439362306a36Sopenharmony_ci{ 439462306a36Sopenharmony_ci return queue_command(xhci, cmd, 0, 0, 0, 439562306a36Sopenharmony_ci TRB_TYPE(TRB_RESET_DEV) | SLOT_ID_FOR_TRB(slot_id), 439662306a36Sopenharmony_ci false); 439762306a36Sopenharmony_ci} 439862306a36Sopenharmony_ci 439962306a36Sopenharmony_ci/* Queue a configure endpoint command TRB */ 440062306a36Sopenharmony_ciint xhci_queue_configure_endpoint(struct xhci_hcd *xhci, 440162306a36Sopenharmony_ci struct xhci_command *cmd, dma_addr_t in_ctx_ptr, 440262306a36Sopenharmony_ci u32 slot_id, bool command_must_succeed) 440362306a36Sopenharmony_ci{ 440462306a36Sopenharmony_ci return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr), 440562306a36Sopenharmony_ci upper_32_bits(in_ctx_ptr), 0, 440662306a36Sopenharmony_ci TRB_TYPE(TRB_CONFIG_EP) | SLOT_ID_FOR_TRB(slot_id), 440762306a36Sopenharmony_ci command_must_succeed); 440862306a36Sopenharmony_ci} 440962306a36Sopenharmony_ci 441062306a36Sopenharmony_ci/* Queue an evaluate context command TRB */ 441162306a36Sopenharmony_ciint xhci_queue_evaluate_context(struct xhci_hcd *xhci, struct xhci_command *cmd, 441262306a36Sopenharmony_ci dma_addr_t in_ctx_ptr, u32 slot_id, bool command_must_succeed) 441362306a36Sopenharmony_ci{ 441462306a36Sopenharmony_ci return queue_command(xhci, cmd, lower_32_bits(in_ctx_ptr), 441562306a36Sopenharmony_ci upper_32_bits(in_ctx_ptr), 0, 441662306a36Sopenharmony_ci TRB_TYPE(TRB_EVAL_CONTEXT) | SLOT_ID_FOR_TRB(slot_id), 441762306a36Sopenharmony_ci command_must_succeed); 441862306a36Sopenharmony_ci} 441962306a36Sopenharmony_ci 442062306a36Sopenharmony_ci/* 442162306a36Sopenharmony_ci * Suspend is set to indicate "Stop Endpoint Command" is being issued to stop 442262306a36Sopenharmony_ci * activity on an endpoint that is about to be suspended. 442362306a36Sopenharmony_ci */ 442462306a36Sopenharmony_ciint xhci_queue_stop_endpoint(struct xhci_hcd *xhci, struct xhci_command *cmd, 442562306a36Sopenharmony_ci int slot_id, unsigned int ep_index, int suspend) 442662306a36Sopenharmony_ci{ 442762306a36Sopenharmony_ci u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id); 442862306a36Sopenharmony_ci u32 trb_ep_index = EP_ID_FOR_TRB(ep_index); 442962306a36Sopenharmony_ci u32 type = TRB_TYPE(TRB_STOP_RING); 443062306a36Sopenharmony_ci u32 trb_suspend = SUSPEND_PORT_FOR_TRB(suspend); 443162306a36Sopenharmony_ci 443262306a36Sopenharmony_ci return queue_command(xhci, cmd, 0, 0, 0, 443362306a36Sopenharmony_ci trb_slot_id | trb_ep_index | type | trb_suspend, false); 443462306a36Sopenharmony_ci} 443562306a36Sopenharmony_ci 443662306a36Sopenharmony_ciint xhci_queue_reset_ep(struct xhci_hcd *xhci, struct xhci_command *cmd, 443762306a36Sopenharmony_ci int slot_id, unsigned int ep_index, 443862306a36Sopenharmony_ci enum xhci_ep_reset_type reset_type) 443962306a36Sopenharmony_ci{ 444062306a36Sopenharmony_ci u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id); 444162306a36Sopenharmony_ci u32 trb_ep_index = EP_ID_FOR_TRB(ep_index); 444262306a36Sopenharmony_ci u32 type = TRB_TYPE(TRB_RESET_EP); 444362306a36Sopenharmony_ci 444462306a36Sopenharmony_ci if (reset_type == EP_SOFT_RESET) 444562306a36Sopenharmony_ci type |= TRB_TSP; 444662306a36Sopenharmony_ci 444762306a36Sopenharmony_ci return queue_command(xhci, cmd, 0, 0, 0, 444862306a36Sopenharmony_ci trb_slot_id | trb_ep_index | type, false); 444962306a36Sopenharmony_ci} 4450