Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * xHCI host controller driver |
| 3 | * |
| 4 | * Copyright (C) 2008 Intel Corp. |
| 5 | * |
| 6 | * Author: Sarah Sharp |
| 7 | * Some code borrowed from the Linux EHCI driver. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 15 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 16 | * for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software Foundation, |
| 20 | * Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 21 | */ |
| 22 | |
| 23 | /* |
| 24 | * Ring initialization rules: |
| 25 | * 1. Each segment is initialized to zero, except for link TRBs. |
| 26 | * 2. Ring cycle state = 0. This represents Producer Cycle State (PCS) or |
| 27 | * Consumer Cycle State (CCS), depending on ring function. |
| 28 | * 3. Enqueue pointer = dequeue pointer = address of first TRB in the segment. |
| 29 | * |
| 30 | * Ring behavior rules: |
| 31 | * 1. A ring is empty if enqueue == dequeue. This means there will always be at |
| 32 | * least one free TRB in the ring. This is useful if you want to turn that |
| 33 | * into a link TRB and expand the ring. |
| 34 | * 2. When incrementing an enqueue or dequeue pointer, if the next TRB is a |
| 35 | * link TRB, then load the pointer with the address in the link TRB. If the |
| 36 | * link TRB had its toggle bit set, you may need to update the ring cycle |
| 37 | * state (see cycle bit rules). You may have to do this multiple times |
| 38 | * until you reach a non-link TRB. |
| 39 | * 3. A ring is full if enqueue++ (for the definition of increment above) |
| 40 | * equals the dequeue pointer. |
| 41 | * |
| 42 | * Cycle bit rules: |
| 43 | * 1. When a consumer increments a dequeue pointer and encounters a toggle bit |
| 44 | * in a link TRB, it must toggle the ring cycle state. |
| 45 | * 2. When a producer increments an enqueue pointer and encounters a toggle bit |
| 46 | * in a link TRB, it must toggle the ring cycle state. |
| 47 | * |
| 48 | * Producer rules: |
| 49 | * 1. Check if ring is full before you enqueue. |
| 50 | * 2. Write the ring cycle state to the cycle bit in the TRB you're enqueuing. |
| 51 | * Update enqueue pointer between each write (which may update the ring |
| 52 | * cycle state). |
| 53 | * 3. Notify consumer. If SW is producer, it rings the doorbell for command |
| 54 | * and endpoint rings. If HC is the producer for the event ring, |
| 55 | * and it generates an interrupt according to interrupt modulation rules. |
| 56 | * |
| 57 | * Consumer rules: |
| 58 | * 1. Check if TRB belongs to you. If the cycle bit == your ring cycle state, |
| 59 | * the TRB is owned by the consumer. |
| 60 | * 2. Update dequeue pointer (which may update the ring cycle state) and |
| 61 | * continue processing TRBs until you reach a TRB which is not owned by you. |
| 62 | * 3. Notify the producer. SW is the consumer for the event ring, and it |
| 63 | * updates event ring dequeue pointer. HC is the consumer for the command and |
| 64 | * endpoint rings; it generates events on the event ring for these. |
| 65 | */ |
| 66 | |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 67 | #include <linux/scatterlist.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 68 | #include <linux/slab.h> |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 69 | #include "xhci.h" |
| 70 | |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 71 | static int handle_cmd_in_cmd_wait_list(struct xhci_hcd *xhci, |
| 72 | struct xhci_virt_device *virt_dev, |
| 73 | struct xhci_event_cmd *event); |
| 74 | |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 75 | /* |
| 76 | * Returns zero if the TRB isn't in this segment, otherwise it returns the DMA |
| 77 | * address of the TRB. |
| 78 | */ |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 79 | dma_addr_t xhci_trb_virt_to_dma(struct xhci_segment *seg, |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 80 | union xhci_trb *trb) |
| 81 | { |
Sarah Sharp | 6071d83 | 2009-05-14 11:44:14 -0700 | [diff] [blame] | 82 | unsigned long segment_offset; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 83 | |
Sarah Sharp | 6071d83 | 2009-05-14 11:44:14 -0700 | [diff] [blame] | 84 | if (!seg || !trb || trb < seg->trbs) |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 85 | return 0; |
Sarah Sharp | 6071d83 | 2009-05-14 11:44:14 -0700 | [diff] [blame] | 86 | /* offset in TRBs */ |
| 87 | segment_offset = trb - seg->trbs; |
| 88 | if (segment_offset > TRBS_PER_SEGMENT) |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 89 | return 0; |
Sarah Sharp | 6071d83 | 2009-05-14 11:44:14 -0700 | [diff] [blame] | 90 | return seg->dma + (segment_offset * sizeof(*trb)); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /* Does this link TRB point to the first segment in a ring, |
| 94 | * or was the previous TRB the last TRB on the last segment in the ERST? |
| 95 | */ |
Dmitry Torokhov | 575688e | 2011-03-20 02:15:16 -0700 | [diff] [blame] | 96 | static bool last_trb_on_last_seg(struct xhci_hcd *xhci, struct xhci_ring *ring, |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 97 | struct xhci_segment *seg, union xhci_trb *trb) |
| 98 | { |
| 99 | if (ring == xhci->event_ring) |
| 100 | return (trb == &seg->trbs[TRBS_PER_SEGMENT]) && |
| 101 | (seg->next == xhci->event_ring->first_seg); |
| 102 | else |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 103 | return le32_to_cpu(trb->link.control) & LINK_TOGGLE; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /* Is this TRB a link TRB or was the last TRB the last TRB in this event ring |
| 107 | * segment? I.e. would the updated event TRB pointer step off the end of the |
| 108 | * event seg? |
| 109 | */ |
Dmitry Torokhov | 575688e | 2011-03-20 02:15:16 -0700 | [diff] [blame] | 110 | static int last_trb(struct xhci_hcd *xhci, struct xhci_ring *ring, |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 111 | struct xhci_segment *seg, union xhci_trb *trb) |
| 112 | { |
| 113 | if (ring == xhci->event_ring) |
| 114 | return trb == &seg->trbs[TRBS_PER_SEGMENT]; |
| 115 | else |
Matt Evans | f5960b6 | 2011-06-01 10:22:55 +1000 | [diff] [blame] | 116 | return TRB_TYPE_LINK_LE32(trb->link.control); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Dmitry Torokhov | 575688e | 2011-03-20 02:15:16 -0700 | [diff] [blame] | 119 | static int enqueue_is_link_trb(struct xhci_ring *ring) |
John Youn | 6c12db9 | 2010-05-10 15:33:00 -0700 | [diff] [blame] | 120 | { |
| 121 | struct xhci_link_trb *link = &ring->enqueue->link; |
Matt Evans | f5960b6 | 2011-06-01 10:22:55 +1000 | [diff] [blame] | 122 | return TRB_TYPE_LINK_LE32(link->control); |
John Youn | 6c12db9 | 2010-05-10 15:33:00 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 125 | /* Updates trb to point to the next TRB in the ring, and updates seg if the next |
| 126 | * TRB is in a new segment. This does not skip over link TRBs, and it does not |
| 127 | * effect the ring dequeue or enqueue pointers. |
| 128 | */ |
| 129 | static void next_trb(struct xhci_hcd *xhci, |
| 130 | struct xhci_ring *ring, |
| 131 | struct xhci_segment **seg, |
| 132 | union xhci_trb **trb) |
| 133 | { |
| 134 | if (last_trb(xhci, ring, *seg, *trb)) { |
| 135 | *seg = (*seg)->next; |
| 136 | *trb = ((*seg)->trbs); |
| 137 | } else { |
John Youn | a1669b2 | 2010-08-09 13:56:11 -0700 | [diff] [blame] | 138 | (*trb)++; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 142 | /* |
| 143 | * See Cycle bit rules. SW is the consumer for the event ring only. |
| 144 | * Don't make a ring full of link TRBs. That would be dumb and this would loop. |
| 145 | */ |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame^] | 146 | static void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring) |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 147 | { |
| 148 | union xhci_trb *next = ++(ring->dequeue); |
Sarah Sharp | 66e49d8 | 2009-07-27 12:03:46 -0700 | [diff] [blame] | 149 | unsigned long long addr; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 150 | |
| 151 | ring->deq_updates++; |
| 152 | /* Update the dequeue pointer further if that was a link TRB or we're at |
| 153 | * the end of an event ring segment (which doesn't have link TRBS) |
| 154 | */ |
| 155 | while (last_trb(xhci, ring, ring->deq_seg, next)) { |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame^] | 156 | if (ring->type == TYPE_EVENT && last_trb_on_last_seg(xhci, |
| 157 | ring, ring->deq_seg, next)) { |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 158 | ring->cycle_state = (ring->cycle_state ? 0 : 1); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 159 | } |
| 160 | ring->deq_seg = ring->deq_seg->next; |
| 161 | ring->dequeue = ring->deq_seg->trbs; |
| 162 | next = ring->dequeue; |
| 163 | } |
Sarah Sharp | 66e49d8 | 2009-07-27 12:03:46 -0700 | [diff] [blame] | 164 | addr = (unsigned long long) xhci_trb_virt_to_dma(ring->deq_seg, ring->dequeue); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /* |
| 168 | * See Cycle bit rules. SW is the consumer for the event ring only. |
| 169 | * Don't make a ring full of link TRBs. That would be dumb and this would loop. |
| 170 | * |
| 171 | * If we've just enqueued a TRB that is in the middle of a TD (meaning the |
| 172 | * chain bit is set), then set the chain bit in all the following link TRBs. |
| 173 | * If we've enqueued the last TRB in a TD, make sure the following link TRBs |
| 174 | * have their chain bit cleared (so that each Link TRB is a separate TD). |
| 175 | * |
| 176 | * Section 6.4.4.1 of the 0.95 spec says link TRBs cannot have the chain bit |
Sarah Sharp | b0567b3 | 2009-08-07 14:04:36 -0700 | [diff] [blame] | 177 | * set, but other sections talk about dealing with the chain bit set. This was |
| 178 | * fixed in the 0.96 specification errata, but we have to assume that all 0.95 |
| 179 | * xHCI hardware can't handle the chain bit being cleared on a link TRB. |
Sarah Sharp | 6cc30d8 | 2010-06-10 12:25:28 -0700 | [diff] [blame] | 180 | * |
| 181 | * @more_trbs_coming: Will you enqueue more TRBs before calling |
| 182 | * prepare_transfer()? |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 183 | */ |
Sarah Sharp | 6cc30d8 | 2010-06-10 12:25:28 -0700 | [diff] [blame] | 184 | static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring, |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame^] | 185 | bool more_trbs_coming) |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 186 | { |
| 187 | u32 chain; |
| 188 | union xhci_trb *next; |
Sarah Sharp | 66e49d8 | 2009-07-27 12:03:46 -0700 | [diff] [blame] | 189 | unsigned long long addr; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 190 | |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 191 | chain = le32_to_cpu(ring->enqueue->generic.field[3]) & TRB_CHAIN; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 192 | next = ++(ring->enqueue); |
| 193 | |
| 194 | ring->enq_updates++; |
| 195 | /* Update the dequeue pointer further if that was a link TRB or we're at |
| 196 | * the end of an event ring segment (which doesn't have link TRBS) |
| 197 | */ |
| 198 | while (last_trb(xhci, ring, ring->enq_seg, next)) { |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame^] | 199 | if (ring->type != TYPE_EVENT) { |
| 200 | /* |
| 201 | * If the caller doesn't plan on enqueueing more |
| 202 | * TDs before ringing the doorbell, then we |
| 203 | * don't want to give the link TRB to the |
| 204 | * hardware just yet. We'll give the link TRB |
| 205 | * back in prepare_ring() just before we enqueue |
| 206 | * the TD at the top of the ring. |
| 207 | */ |
| 208 | if (!chain && !more_trbs_coming) |
| 209 | break; |
Sarah Sharp | 6cc30d8 | 2010-06-10 12:25:28 -0700 | [diff] [blame] | 210 | |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame^] | 211 | /* If we're not dealing with 0.95 hardware or |
| 212 | * isoc rings on AMD 0.96 host, |
| 213 | * carry over the chain bit of the previous TRB |
| 214 | * (which may mean the chain bit is cleared). |
| 215 | */ |
| 216 | if (!(ring->type == TYPE_ISOC && |
| 217 | (xhci->quirks & XHCI_AMD_0x96_HOST)) |
Andiry Xu | 7e393a8 | 2011-09-23 14:19:54 -0700 | [diff] [blame] | 218 | && !xhci_link_trb_quirk(xhci)) { |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame^] | 219 | next->link.control &= |
| 220 | cpu_to_le32(~TRB_CHAIN); |
| 221 | next->link.control |= |
| 222 | cpu_to_le32(chain); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 223 | } |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame^] | 224 | /* Give this link TRB to the hardware */ |
| 225 | wmb(); |
| 226 | next->link.control ^= cpu_to_le32(TRB_CYCLE); |
| 227 | |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 228 | /* Toggle the cycle bit after the last ring segment. */ |
| 229 | if (last_trb_on_last_seg(xhci, ring, ring->enq_seg, next)) { |
| 230 | ring->cycle_state = (ring->cycle_state ? 0 : 1); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | ring->enq_seg = ring->enq_seg->next; |
| 234 | ring->enqueue = ring->enq_seg->trbs; |
| 235 | next = ring->enqueue; |
| 236 | } |
Sarah Sharp | 66e49d8 | 2009-07-27 12:03:46 -0700 | [diff] [blame] | 237 | addr = (unsigned long long) xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /* |
| 241 | * Check to see if there's room to enqueue num_trbs on the ring. See rules |
| 242 | * above. |
| 243 | * FIXME: this would be simpler and faster if we just kept track of the number |
| 244 | * of free TRBs in a ring. |
| 245 | */ |
| 246 | static int room_on_ring(struct xhci_hcd *xhci, struct xhci_ring *ring, |
| 247 | unsigned int num_trbs) |
| 248 | { |
| 249 | int i; |
| 250 | union xhci_trb *enq = ring->enqueue; |
| 251 | struct xhci_segment *enq_seg = ring->enq_seg; |
Sarah Sharp | 44ebd03 | 2010-05-18 16:05:26 -0700 | [diff] [blame] | 252 | struct xhci_segment *cur_seg; |
| 253 | unsigned int left_on_ring; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 254 | |
John Youn | 6c12db9 | 2010-05-10 15:33:00 -0700 | [diff] [blame] | 255 | /* If we are currently pointing to a link TRB, advance the |
| 256 | * enqueue pointer before checking for space */ |
| 257 | while (last_trb(xhci, ring, enq_seg, enq)) { |
| 258 | enq_seg = enq_seg->next; |
| 259 | enq = enq_seg->trbs; |
| 260 | } |
| 261 | |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 262 | /* Check if ring is empty */ |
Sarah Sharp | 44ebd03 | 2010-05-18 16:05:26 -0700 | [diff] [blame] | 263 | if (enq == ring->dequeue) { |
| 264 | /* Can't use link trbs */ |
| 265 | left_on_ring = TRBS_PER_SEGMENT - 1; |
| 266 | for (cur_seg = enq_seg->next; cur_seg != enq_seg; |
| 267 | cur_seg = cur_seg->next) |
| 268 | left_on_ring += TRBS_PER_SEGMENT - 1; |
| 269 | |
| 270 | /* Always need one TRB free in the ring. */ |
| 271 | left_on_ring -= 1; |
| 272 | if (num_trbs > left_on_ring) { |
| 273 | xhci_warn(xhci, "Not enough room on ring; " |
| 274 | "need %u TRBs, %u TRBs left\n", |
| 275 | num_trbs, left_on_ring); |
| 276 | return 0; |
| 277 | } |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 278 | return 1; |
Sarah Sharp | 44ebd03 | 2010-05-18 16:05:26 -0700 | [diff] [blame] | 279 | } |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 280 | /* Make sure there's an extra empty TRB available */ |
| 281 | for (i = 0; i <= num_trbs; ++i) { |
| 282 | if (enq == ring->dequeue) |
| 283 | return 0; |
| 284 | enq++; |
| 285 | while (last_trb(xhci, ring, enq_seg, enq)) { |
| 286 | enq_seg = enq_seg->next; |
| 287 | enq = enq_seg->trbs; |
| 288 | } |
| 289 | } |
| 290 | return 1; |
| 291 | } |
| 292 | |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 293 | /* Ring the host controller doorbell after placing a command on the ring */ |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 294 | void xhci_ring_cmd_db(struct xhci_hcd *xhci) |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 295 | { |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 296 | xhci_dbg(xhci, "// Ding dong!\n"); |
Matthew Wilcox | 50d6467 | 2010-12-15 14:18:11 -0500 | [diff] [blame] | 297 | xhci_writel(xhci, DB_VALUE_HOST, &xhci->dba->doorbell[0]); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 298 | /* Flush PCI posted writes */ |
| 299 | xhci_readl(xhci, &xhci->dba->doorbell[0]); |
| 300 | } |
| 301 | |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 302 | void xhci_ring_ep_doorbell(struct xhci_hcd *xhci, |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 303 | unsigned int slot_id, |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 304 | unsigned int ep_index, |
| 305 | unsigned int stream_id) |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 306 | { |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 307 | __le32 __iomem *db_addr = &xhci->dba->doorbell[slot_id]; |
Matthew Wilcox | 50d6467 | 2010-12-15 14:18:11 -0500 | [diff] [blame] | 308 | struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index]; |
| 309 | unsigned int ep_state = ep->ep_state; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 310 | |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 311 | /* Don't ring the doorbell for this endpoint if there are pending |
Matthew Wilcox | 50d6467 | 2010-12-15 14:18:11 -0500 | [diff] [blame] | 312 | * cancellations because we don't want to interrupt processing. |
Sarah Sharp | 8df75f4 | 2010-04-02 15:34:16 -0700 | [diff] [blame] | 313 | * We don't want to restart any stream rings if there's a set dequeue |
| 314 | * pointer command pending because the device can choose to start any |
| 315 | * stream once the endpoint is on the HW schedule. |
| 316 | * FIXME - check all the stream rings for pending cancellations. |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 317 | */ |
Matthew Wilcox | 50d6467 | 2010-12-15 14:18:11 -0500 | [diff] [blame] | 318 | if ((ep_state & EP_HALT_PENDING) || (ep_state & SET_DEQ_PENDING) || |
| 319 | (ep_state & EP_HALTED)) |
| 320 | return; |
| 321 | xhci_writel(xhci, DB_VALUE(ep_index, stream_id), db_addr); |
| 322 | /* The CPU has better things to do at this point than wait for a |
| 323 | * write-posting flush. It'll get there soon enough. |
| 324 | */ |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 327 | /* Ring the doorbell for any rings with pending URBs */ |
| 328 | static void ring_doorbell_for_active_rings(struct xhci_hcd *xhci, |
| 329 | unsigned int slot_id, |
| 330 | unsigned int ep_index) |
| 331 | { |
| 332 | unsigned int stream_id; |
| 333 | struct xhci_virt_ep *ep; |
| 334 | |
| 335 | ep = &xhci->devs[slot_id]->eps[ep_index]; |
| 336 | |
| 337 | /* A ring has pending URBs if its TD list is not empty */ |
| 338 | if (!(ep->ep_state & EP_HAS_STREAMS)) { |
| 339 | if (!(list_empty(&ep->ring->td_list))) |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 340 | xhci_ring_ep_doorbell(xhci, slot_id, ep_index, 0); |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 341 | return; |
| 342 | } |
| 343 | |
| 344 | for (stream_id = 1; stream_id < ep->stream_info->num_streams; |
| 345 | stream_id++) { |
| 346 | struct xhci_stream_info *stream_info = ep->stream_info; |
| 347 | if (!list_empty(&stream_info->stream_rings[stream_id]->td_list)) |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 348 | xhci_ring_ep_doorbell(xhci, slot_id, ep_index, |
| 349 | stream_id); |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 353 | /* |
| 354 | * Find the segment that trb is in. Start searching in start_seg. |
| 355 | * If we must move past a segment that has a link TRB with a toggle cycle state |
| 356 | * bit set, then we will toggle the value pointed at by cycle_state. |
| 357 | */ |
| 358 | static struct xhci_segment *find_trb_seg( |
| 359 | struct xhci_segment *start_seg, |
| 360 | union xhci_trb *trb, int *cycle_state) |
| 361 | { |
| 362 | struct xhci_segment *cur_seg = start_seg; |
| 363 | struct xhci_generic_trb *generic_trb; |
| 364 | |
| 365 | while (cur_seg->trbs > trb || |
| 366 | &cur_seg->trbs[TRBS_PER_SEGMENT - 1] < trb) { |
| 367 | generic_trb = &cur_seg->trbs[TRBS_PER_SEGMENT - 1].generic; |
Matt Evans | f5960b6 | 2011-06-01 10:22:55 +1000 | [diff] [blame] | 368 | if (generic_trb->field[3] & cpu_to_le32(LINK_TOGGLE)) |
Sarah Sharp | ba0a4d9 | 2011-02-23 18:13:43 -0800 | [diff] [blame] | 369 | *cycle_state ^= 0x1; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 370 | cur_seg = cur_seg->next; |
| 371 | if (cur_seg == start_seg) |
| 372 | /* Looped over the entire list. Oops! */ |
Randy Dunlap | 326b481 | 2010-04-19 08:53:50 -0700 | [diff] [blame] | 373 | return NULL; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 374 | } |
| 375 | return cur_seg; |
| 376 | } |
| 377 | |
Sarah Sharp | 021bff9 | 2010-07-29 22:12:20 -0700 | [diff] [blame] | 378 | |
| 379 | static struct xhci_ring *xhci_triad_to_transfer_ring(struct xhci_hcd *xhci, |
| 380 | unsigned int slot_id, unsigned int ep_index, |
| 381 | unsigned int stream_id) |
| 382 | { |
| 383 | struct xhci_virt_ep *ep; |
| 384 | |
| 385 | ep = &xhci->devs[slot_id]->eps[ep_index]; |
| 386 | /* Common case: no streams */ |
| 387 | if (!(ep->ep_state & EP_HAS_STREAMS)) |
| 388 | return ep->ring; |
| 389 | |
| 390 | if (stream_id == 0) { |
| 391 | xhci_warn(xhci, |
| 392 | "WARN: Slot ID %u, ep index %u has streams, " |
| 393 | "but URB has no stream ID.\n", |
| 394 | slot_id, ep_index); |
| 395 | return NULL; |
| 396 | } |
| 397 | |
| 398 | if (stream_id < ep->stream_info->num_streams) |
| 399 | return ep->stream_info->stream_rings[stream_id]; |
| 400 | |
| 401 | xhci_warn(xhci, |
| 402 | "WARN: Slot ID %u, ep index %u has " |
| 403 | "stream IDs 1 to %u allocated, " |
| 404 | "but stream ID %u is requested.\n", |
| 405 | slot_id, ep_index, |
| 406 | ep->stream_info->num_streams - 1, |
| 407 | stream_id); |
| 408 | return NULL; |
| 409 | } |
| 410 | |
| 411 | /* Get the right ring for the given URB. |
| 412 | * If the endpoint supports streams, boundary check the URB's stream ID. |
| 413 | * If the endpoint doesn't support streams, return the singular endpoint ring. |
| 414 | */ |
| 415 | static struct xhci_ring *xhci_urb_to_transfer_ring(struct xhci_hcd *xhci, |
| 416 | struct urb *urb) |
| 417 | { |
| 418 | return xhci_triad_to_transfer_ring(xhci, urb->dev->slot_id, |
| 419 | xhci_get_endpoint_index(&urb->ep->desc), urb->stream_id); |
| 420 | } |
| 421 | |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 422 | /* |
| 423 | * Move the xHC's endpoint ring dequeue pointer past cur_td. |
| 424 | * Record the new state of the xHC's endpoint ring dequeue segment, |
| 425 | * dequeue pointer, and new consumer cycle state in state. |
| 426 | * Update our internal representation of the ring's dequeue pointer. |
| 427 | * |
| 428 | * We do this in three jumps: |
| 429 | * - First we update our new ring state to be the same as when the xHC stopped. |
| 430 | * - Then we traverse the ring to find the segment that contains |
| 431 | * the last TRB in the TD. We toggle the xHC's new cycle state when we pass |
| 432 | * any link TRBs with the toggle cycle bit set. |
| 433 | * - Finally we move the dequeue state one TRB further, toggling the cycle bit |
| 434 | * if we've moved it past a link TRB with the toggle cycle bit set. |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 435 | * |
| 436 | * Some of the uses of xhci_generic_trb are grotty, but if they're done |
| 437 | * with correct __le32 accesses they should work fine. Only users of this are |
| 438 | * in here. |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 439 | */ |
Sarah Sharp | c92bcfa | 2009-07-27 12:05:21 -0700 | [diff] [blame] | 440 | void xhci_find_new_dequeue_state(struct xhci_hcd *xhci, |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 441 | unsigned int slot_id, unsigned int ep_index, |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 442 | unsigned int stream_id, struct xhci_td *cur_td, |
| 443 | struct xhci_dequeue_state *state) |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 444 | { |
| 445 | struct xhci_virt_device *dev = xhci->devs[slot_id]; |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 446 | struct xhci_ring *ep_ring; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 447 | struct xhci_generic_trb *trb; |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 448 | struct xhci_ep_ctx *ep_ctx; |
Sarah Sharp | c92bcfa | 2009-07-27 12:05:21 -0700 | [diff] [blame] | 449 | dma_addr_t addr; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 450 | |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 451 | ep_ring = xhci_triad_to_transfer_ring(xhci, slot_id, |
| 452 | ep_index, stream_id); |
| 453 | if (!ep_ring) { |
| 454 | xhci_warn(xhci, "WARN can't find new dequeue state " |
| 455 | "for invalid stream ID %u.\n", |
| 456 | stream_id); |
| 457 | return; |
| 458 | } |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 459 | state->new_cycle_state = 0; |
Sarah Sharp | c92bcfa | 2009-07-27 12:05:21 -0700 | [diff] [blame] | 460 | xhci_dbg(xhci, "Finding segment containing stopped TRB.\n"); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 461 | state->new_deq_seg = find_trb_seg(cur_td->start_seg, |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame] | 462 | dev->eps[ep_index].stopped_trb, |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 463 | &state->new_cycle_state); |
Paul Zimmerman | 68e41c5 | 2011-02-12 14:06:06 -0800 | [diff] [blame] | 464 | if (!state->new_deq_seg) { |
| 465 | WARN_ON(1); |
| 466 | return; |
| 467 | } |
| 468 | |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 469 | /* Dig out the cycle state saved by the xHC during the stop ep cmd */ |
Sarah Sharp | c92bcfa | 2009-07-27 12:05:21 -0700 | [diff] [blame] | 470 | xhci_dbg(xhci, "Finding endpoint context\n"); |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 471 | ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index); |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 472 | state->new_cycle_state = 0x1 & le64_to_cpu(ep_ctx->deq); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 473 | |
| 474 | state->new_deq_ptr = cur_td->last_trb; |
Sarah Sharp | c92bcfa | 2009-07-27 12:05:21 -0700 | [diff] [blame] | 475 | xhci_dbg(xhci, "Finding segment containing last TRB in TD.\n"); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 476 | state->new_deq_seg = find_trb_seg(state->new_deq_seg, |
| 477 | state->new_deq_ptr, |
| 478 | &state->new_cycle_state); |
Paul Zimmerman | 68e41c5 | 2011-02-12 14:06:06 -0800 | [diff] [blame] | 479 | if (!state->new_deq_seg) { |
| 480 | WARN_ON(1); |
| 481 | return; |
| 482 | } |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 483 | |
| 484 | trb = &state->new_deq_ptr->generic; |
Matt Evans | f5960b6 | 2011-06-01 10:22:55 +1000 | [diff] [blame] | 485 | if (TRB_TYPE_LINK_LE32(trb->field[3]) && |
| 486 | (trb->field[3] & cpu_to_le32(LINK_TOGGLE))) |
Sarah Sharp | ba0a4d9 | 2011-02-23 18:13:43 -0800 | [diff] [blame] | 487 | state->new_cycle_state ^= 0x1; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 488 | next_trb(xhci, ep_ring, &state->new_deq_seg, &state->new_deq_ptr); |
| 489 | |
Sarah Sharp | 01a1fdb | 2011-02-23 18:12:29 -0800 | [diff] [blame] | 490 | /* |
| 491 | * If there is only one segment in a ring, find_trb_seg()'s while loop |
| 492 | * will not run, and it will return before it has a chance to see if it |
| 493 | * needs to toggle the cycle bit. It can't tell if the stalled transfer |
| 494 | * ended just before the link TRB on a one-segment ring, or if the TD |
| 495 | * wrapped around the top of the ring, because it doesn't have the TD in |
| 496 | * question. Look for the one-segment case where stalled TRB's address |
| 497 | * is greater than the new dequeue pointer address. |
| 498 | */ |
| 499 | if (ep_ring->first_seg == ep_ring->first_seg->next && |
| 500 | state->new_deq_ptr < dev->eps[ep_index].stopped_trb) |
| 501 | state->new_cycle_state ^= 0x1; |
| 502 | xhci_dbg(xhci, "Cycle state = 0x%x\n", state->new_cycle_state); |
| 503 | |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 504 | /* Don't update the ring cycle state for the producer (us). */ |
Sarah Sharp | c92bcfa | 2009-07-27 12:05:21 -0700 | [diff] [blame] | 505 | xhci_dbg(xhci, "New dequeue segment = %p (virtual)\n", |
| 506 | state->new_deq_seg); |
| 507 | addr = xhci_trb_virt_to_dma(state->new_deq_seg, state->new_deq_ptr); |
| 508 | xhci_dbg(xhci, "New dequeue pointer = 0x%llx (DMA)\n", |
| 509 | (unsigned long long) addr); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Sarah Sharp | 522989a | 2011-07-29 12:44:32 -0700 | [diff] [blame] | 512 | /* flip_cycle means flip the cycle bit of all but the first and last TRB. |
| 513 | * (The last TRB actually points to the ring enqueue pointer, which is not part |
| 514 | * of this TD.) This is used to remove partially enqueued isoc TDs from a ring. |
| 515 | */ |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 516 | static void td_to_noop(struct xhci_hcd *xhci, struct xhci_ring *ep_ring, |
Sarah Sharp | 522989a | 2011-07-29 12:44:32 -0700 | [diff] [blame] | 517 | struct xhci_td *cur_td, bool flip_cycle) |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 518 | { |
| 519 | struct xhci_segment *cur_seg; |
| 520 | union xhci_trb *cur_trb; |
| 521 | |
| 522 | for (cur_seg = cur_td->start_seg, cur_trb = cur_td->first_trb; |
| 523 | true; |
| 524 | next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) { |
Matt Evans | f5960b6 | 2011-06-01 10:22:55 +1000 | [diff] [blame] | 525 | if (TRB_TYPE_LINK_LE32(cur_trb->generic.field[3])) { |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 526 | /* Unchain any chained Link TRBs, but |
| 527 | * leave the pointers intact. |
| 528 | */ |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 529 | cur_trb->generic.field[3] &= cpu_to_le32(~TRB_CHAIN); |
Sarah Sharp | 522989a | 2011-07-29 12:44:32 -0700 | [diff] [blame] | 530 | /* Flip the cycle bit (link TRBs can't be the first |
| 531 | * or last TRB). |
| 532 | */ |
| 533 | if (flip_cycle) |
| 534 | cur_trb->generic.field[3] ^= |
| 535 | cpu_to_le32(TRB_CYCLE); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 536 | xhci_dbg(xhci, "Cancel (unchain) link TRB\n"); |
Greg Kroah-Hartman | 700e205 | 2009-04-29 19:14:08 -0700 | [diff] [blame] | 537 | xhci_dbg(xhci, "Address = %p (0x%llx dma); " |
| 538 | "in seg %p (0x%llx dma)\n", |
| 539 | cur_trb, |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 540 | (unsigned long long)xhci_trb_virt_to_dma(cur_seg, cur_trb), |
Greg Kroah-Hartman | 700e205 | 2009-04-29 19:14:08 -0700 | [diff] [blame] | 541 | cur_seg, |
| 542 | (unsigned long long)cur_seg->dma); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 543 | } else { |
| 544 | cur_trb->generic.field[0] = 0; |
| 545 | cur_trb->generic.field[1] = 0; |
| 546 | cur_trb->generic.field[2] = 0; |
| 547 | /* Preserve only the cycle bit of this TRB */ |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 548 | cur_trb->generic.field[3] &= cpu_to_le32(TRB_CYCLE); |
Sarah Sharp | 522989a | 2011-07-29 12:44:32 -0700 | [diff] [blame] | 549 | /* Flip the cycle bit except on the first or last TRB */ |
| 550 | if (flip_cycle && cur_trb != cur_td->first_trb && |
| 551 | cur_trb != cur_td->last_trb) |
| 552 | cur_trb->generic.field[3] ^= |
| 553 | cpu_to_le32(TRB_CYCLE); |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 554 | cur_trb->generic.field[3] |= cpu_to_le32( |
| 555 | TRB_TYPE(TRB_TR_NOOP)); |
Sarah Sharp | 79688ac | 2011-12-19 16:56:04 -0800 | [diff] [blame] | 556 | xhci_dbg(xhci, "TRB to noop at offset 0x%llx\n", |
| 557 | (unsigned long long) |
| 558 | xhci_trb_virt_to_dma(cur_seg, cur_trb)); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 559 | } |
| 560 | if (cur_trb == cur_td->last_trb) |
| 561 | break; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id, |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 566 | unsigned int ep_index, unsigned int stream_id, |
| 567 | struct xhci_segment *deq_seg, |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 568 | union xhci_trb *deq_ptr, u32 cycle_state); |
| 569 | |
Sarah Sharp | c92bcfa | 2009-07-27 12:05:21 -0700 | [diff] [blame] | 570 | void xhci_queue_new_dequeue_state(struct xhci_hcd *xhci, |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame] | 571 | unsigned int slot_id, unsigned int ep_index, |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 572 | unsigned int stream_id, |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame] | 573 | struct xhci_dequeue_state *deq_state) |
Sarah Sharp | c92bcfa | 2009-07-27 12:05:21 -0700 | [diff] [blame] | 574 | { |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame] | 575 | struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index]; |
| 576 | |
Sarah Sharp | c92bcfa | 2009-07-27 12:05:21 -0700 | [diff] [blame] | 577 | xhci_dbg(xhci, "Set TR Deq Ptr cmd, new deq seg = %p (0x%llx dma), " |
| 578 | "new deq ptr = %p (0x%llx dma), new cycle = %u\n", |
| 579 | deq_state->new_deq_seg, |
| 580 | (unsigned long long)deq_state->new_deq_seg->dma, |
| 581 | deq_state->new_deq_ptr, |
| 582 | (unsigned long long)xhci_trb_virt_to_dma(deq_state->new_deq_seg, deq_state->new_deq_ptr), |
| 583 | deq_state->new_cycle_state); |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 584 | queue_set_tr_deq(xhci, slot_id, ep_index, stream_id, |
Sarah Sharp | c92bcfa | 2009-07-27 12:05:21 -0700 | [diff] [blame] | 585 | deq_state->new_deq_seg, |
| 586 | deq_state->new_deq_ptr, |
| 587 | (u32) deq_state->new_cycle_state); |
| 588 | /* Stop the TD queueing code from ringing the doorbell until |
| 589 | * this command completes. The HC won't set the dequeue pointer |
| 590 | * if the ring is running, and ringing the doorbell starts the |
| 591 | * ring running. |
| 592 | */ |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame] | 593 | ep->ep_state |= SET_DEQ_PENDING; |
Sarah Sharp | c92bcfa | 2009-07-27 12:05:21 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Dmitry Torokhov | 575688e | 2011-03-20 02:15:16 -0700 | [diff] [blame] | 596 | static void xhci_stop_watchdog_timer_in_irq(struct xhci_hcd *xhci, |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 597 | struct xhci_virt_ep *ep) |
| 598 | { |
| 599 | ep->ep_state &= ~EP_HALT_PENDING; |
| 600 | /* Can't del_timer_sync in interrupt, so we attempt to cancel. If the |
| 601 | * timer is running on another CPU, we don't decrement stop_cmds_pending |
| 602 | * (since we didn't successfully stop the watchdog timer). |
| 603 | */ |
| 604 | if (del_timer(&ep->stop_cmd_timer)) |
| 605 | ep->stop_cmds_pending--; |
| 606 | } |
| 607 | |
| 608 | /* Must be called with xhci->lock held in interrupt context */ |
| 609 | static void xhci_giveback_urb_in_irq(struct xhci_hcd *xhci, |
| 610 | struct xhci_td *cur_td, int status, char *adjective) |
| 611 | { |
Sarah Sharp | 214f76f | 2010-10-26 11:22:02 -0700 | [diff] [blame] | 612 | struct usb_hcd *hcd; |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 613 | struct urb *urb; |
| 614 | struct urb_priv *urb_priv; |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 615 | |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 616 | urb = cur_td->urb; |
| 617 | urb_priv = urb->hcpriv; |
| 618 | urb_priv->td_cnt++; |
Sarah Sharp | 214f76f | 2010-10-26 11:22:02 -0700 | [diff] [blame] | 619 | hcd = bus_to_hcd(urb->dev->bus); |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 620 | |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 621 | /* Only giveback urb when this is the last td in urb */ |
| 622 | if (urb_priv->td_cnt == urb_priv->length) { |
Andiry Xu | c41136b | 2011-03-22 17:08:14 +0800 | [diff] [blame] | 623 | if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) { |
| 624 | xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs--; |
| 625 | if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs == 0) { |
| 626 | if (xhci->quirks & XHCI_AMD_PLL_FIX) |
| 627 | usb_amd_quirk_pll_enable(); |
| 628 | } |
| 629 | } |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 630 | usb_hcd_unlink_urb_from_ep(hcd, urb); |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 631 | |
| 632 | spin_unlock(&xhci->lock); |
| 633 | usb_hcd_giveback_urb(hcd, urb, status); |
| 634 | xhci_urb_free_priv(xhci, urb_priv); |
| 635 | spin_lock(&xhci->lock); |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 636 | } |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 637 | } |
| 638 | |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 639 | /* |
| 640 | * When we get a command completion for a Stop Endpoint Command, we need to |
| 641 | * unlink any cancelled TDs from the ring. There are two ways to do that: |
| 642 | * |
| 643 | * 1. If the HW was in the middle of processing the TD that needs to be |
| 644 | * cancelled, then we must move the ring's dequeue pointer past the last TRB |
| 645 | * in the TD with a Set Dequeue Pointer Command. |
| 646 | * 2. Otherwise, we turn all the TRBs in the TD into No-op TRBs (with the chain |
| 647 | * bit cleared) so that the HW will skip over them. |
| 648 | */ |
| 649 | static void handle_stopped_endpoint(struct xhci_hcd *xhci, |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 650 | union xhci_trb *trb, struct xhci_event_cmd *event) |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 651 | { |
| 652 | unsigned int slot_id; |
| 653 | unsigned int ep_index; |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 654 | struct xhci_virt_device *virt_dev; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 655 | struct xhci_ring *ep_ring; |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame] | 656 | struct xhci_virt_ep *ep; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 657 | struct list_head *entry; |
Randy Dunlap | 326b481 | 2010-04-19 08:53:50 -0700 | [diff] [blame] | 658 | struct xhci_td *cur_td = NULL; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 659 | struct xhci_td *last_unlinked_td; |
| 660 | |
Sarah Sharp | c92bcfa | 2009-07-27 12:05:21 -0700 | [diff] [blame] | 661 | struct xhci_dequeue_state deq_state; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 662 | |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 663 | if (unlikely(TRB_TO_SUSPEND_PORT( |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 664 | le32_to_cpu(xhci->cmd_ring->dequeue->generic.field[3])))) { |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 665 | slot_id = TRB_TO_SLOT_ID( |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 666 | le32_to_cpu(xhci->cmd_ring->dequeue->generic.field[3])); |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 667 | virt_dev = xhci->devs[slot_id]; |
| 668 | if (virt_dev) |
| 669 | handle_cmd_in_cmd_wait_list(xhci, virt_dev, |
| 670 | event); |
| 671 | else |
| 672 | xhci_warn(xhci, "Stop endpoint command " |
| 673 | "completion for disabled slot %u\n", |
| 674 | slot_id); |
| 675 | return; |
| 676 | } |
| 677 | |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 678 | memset(&deq_state, 0, sizeof(deq_state)); |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 679 | slot_id = TRB_TO_SLOT_ID(le32_to_cpu(trb->generic.field[3])); |
| 680 | ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3])); |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame] | 681 | ep = &xhci->devs[slot_id]->eps[ep_index]; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 682 | |
Sarah Sharp | 678539c | 2009-10-27 10:55:52 -0700 | [diff] [blame] | 683 | if (list_empty(&ep->cancelled_td_list)) { |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 684 | xhci_stop_watchdog_timer_in_irq(xhci, ep); |
Sarah Sharp | 0714a57 | 2011-05-24 11:53:29 -0700 | [diff] [blame] | 685 | ep->stopped_td = NULL; |
| 686 | ep->stopped_trb = NULL; |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 687 | ring_doorbell_for_active_rings(xhci, slot_id, ep_index); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 688 | return; |
Sarah Sharp | 678539c | 2009-10-27 10:55:52 -0700 | [diff] [blame] | 689 | } |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 690 | |
| 691 | /* Fix up the ep ring first, so HW stops executing cancelled TDs. |
| 692 | * We have the xHCI lock, so nothing can modify this list until we drop |
| 693 | * it. We're also in the event handler, so we can't get re-interrupted |
| 694 | * if another Stop Endpoint command completes |
| 695 | */ |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame] | 696 | list_for_each(entry, &ep->cancelled_td_list) { |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 697 | cur_td = list_entry(entry, struct xhci_td, cancelled_td_list); |
Sarah Sharp | 79688ac | 2011-12-19 16:56:04 -0800 | [diff] [blame] | 698 | xhci_dbg(xhci, "Removing canceled TD starting at 0x%llx (dma).\n", |
| 699 | (unsigned long long)xhci_trb_virt_to_dma( |
| 700 | cur_td->start_seg, cur_td->first_trb)); |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 701 | ep_ring = xhci_urb_to_transfer_ring(xhci, cur_td->urb); |
| 702 | if (!ep_ring) { |
| 703 | /* This shouldn't happen unless a driver is mucking |
| 704 | * with the stream ID after submission. This will |
| 705 | * leave the TD on the hardware ring, and the hardware |
| 706 | * will try to execute it, and may access a buffer |
| 707 | * that has already been freed. In the best case, the |
| 708 | * hardware will execute it, and the event handler will |
| 709 | * ignore the completion event for that TD, since it was |
| 710 | * removed from the td_list for that endpoint. In |
| 711 | * short, don't muck with the stream ID after |
| 712 | * submission. |
| 713 | */ |
| 714 | xhci_warn(xhci, "WARN Cancelled URB %p " |
| 715 | "has invalid stream ID %u.\n", |
| 716 | cur_td->urb, |
| 717 | cur_td->urb->stream_id); |
| 718 | goto remove_finished_td; |
| 719 | } |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 720 | /* |
| 721 | * If we stopped on the TD we need to cancel, then we have to |
| 722 | * move the xHC endpoint ring dequeue pointer past this TD. |
| 723 | */ |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame] | 724 | if (cur_td == ep->stopped_td) |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 725 | xhci_find_new_dequeue_state(xhci, slot_id, ep_index, |
| 726 | cur_td->urb->stream_id, |
| 727 | cur_td, &deq_state); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 728 | else |
Sarah Sharp | 522989a | 2011-07-29 12:44:32 -0700 | [diff] [blame] | 729 | td_to_noop(xhci, ep_ring, cur_td, false); |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 730 | remove_finished_td: |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 731 | /* |
| 732 | * The event handler won't see a completion for this TD anymore, |
| 733 | * so remove it from the endpoint ring's TD list. Keep it in |
| 734 | * the cancelled TD list for URB completion later. |
| 735 | */ |
Sarah Sharp | 585df1d | 2011-08-02 15:43:40 -0700 | [diff] [blame] | 736 | list_del_init(&cur_td->td_list); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 737 | } |
| 738 | last_unlinked_td = cur_td; |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 739 | xhci_stop_watchdog_timer_in_irq(xhci, ep); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 740 | |
| 741 | /* If necessary, queue a Set Transfer Ring Dequeue Pointer command */ |
| 742 | if (deq_state.new_deq_ptr && deq_state.new_deq_seg) { |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame] | 743 | xhci_queue_new_dequeue_state(xhci, |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 744 | slot_id, ep_index, |
| 745 | ep->stopped_td->urb->stream_id, |
| 746 | &deq_state); |
Sarah Sharp | ac9d8fe | 2009-08-07 14:04:55 -0700 | [diff] [blame] | 747 | xhci_ring_cmd_db(xhci); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 748 | } else { |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 749 | /* Otherwise ring the doorbell(s) to restart queued transfers */ |
| 750 | ring_doorbell_for_active_rings(xhci, slot_id, ep_index); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 751 | } |
Sarah Sharp | 1624ae1 | 2010-05-06 13:40:08 -0700 | [diff] [blame] | 752 | ep->stopped_td = NULL; |
| 753 | ep->stopped_trb = NULL; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 754 | |
| 755 | /* |
| 756 | * Drop the lock and complete the URBs in the cancelled TD list. |
| 757 | * New TDs to be cancelled might be added to the end of the list before |
| 758 | * we can complete all the URBs for the TDs we already unlinked. |
| 759 | * So stop when we've completed the URB for the last TD we unlinked. |
| 760 | */ |
| 761 | do { |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame] | 762 | cur_td = list_entry(ep->cancelled_td_list.next, |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 763 | struct xhci_td, cancelled_td_list); |
Sarah Sharp | 585df1d | 2011-08-02 15:43:40 -0700 | [diff] [blame] | 764 | list_del_init(&cur_td->cancelled_td_list); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 765 | |
| 766 | /* Clean up the cancelled URB */ |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 767 | /* Doesn't matter what we pass for status, since the core will |
| 768 | * just overwrite it (because the URB has been unlinked). |
| 769 | */ |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 770 | xhci_giveback_urb_in_irq(xhci, cur_td, 0, "cancelled"); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 771 | |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 772 | /* Stop processing the cancelled list if the watchdog timer is |
| 773 | * running. |
| 774 | */ |
| 775 | if (xhci->xhc_state & XHCI_STATE_DYING) |
| 776 | return; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 777 | } while (cur_td != last_unlinked_td); |
| 778 | |
| 779 | /* Return to the event handler with xhci->lock re-acquired */ |
| 780 | } |
| 781 | |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 782 | /* Watchdog timer function for when a stop endpoint command fails to complete. |
| 783 | * In this case, we assume the host controller is broken or dying or dead. The |
| 784 | * host may still be completing some other events, so we have to be careful to |
| 785 | * let the event ring handler and the URB dequeueing/enqueueing functions know |
| 786 | * through xhci->state. |
| 787 | * |
| 788 | * The timer may also fire if the host takes a very long time to respond to the |
| 789 | * command, and the stop endpoint command completion handler cannot delete the |
| 790 | * timer before the timer function is called. Another endpoint cancellation may |
| 791 | * sneak in before the timer function can grab the lock, and that may queue |
| 792 | * another stop endpoint command and add the timer back. So we cannot use a |
| 793 | * simple flag to say whether there is a pending stop endpoint command for a |
| 794 | * particular endpoint. |
| 795 | * |
| 796 | * Instead we use a combination of that flag and a counter for the number of |
| 797 | * pending stop endpoint commands. If the timer is the tail end of the last |
| 798 | * stop endpoint command, and the endpoint's command is still pending, we assume |
| 799 | * the host is dying. |
| 800 | */ |
| 801 | void xhci_stop_endpoint_command_watchdog(unsigned long arg) |
| 802 | { |
| 803 | struct xhci_hcd *xhci; |
| 804 | struct xhci_virt_ep *ep; |
| 805 | struct xhci_virt_ep *temp_ep; |
| 806 | struct xhci_ring *ring; |
| 807 | struct xhci_td *cur_td; |
| 808 | int ret, i, j; |
Don Zickus | f43d623 | 2011-10-20 23:52:14 -0400 | [diff] [blame] | 809 | unsigned long flags; |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 810 | |
| 811 | ep = (struct xhci_virt_ep *) arg; |
| 812 | xhci = ep->xhci; |
| 813 | |
Don Zickus | f43d623 | 2011-10-20 23:52:14 -0400 | [diff] [blame] | 814 | spin_lock_irqsave(&xhci->lock, flags); |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 815 | |
| 816 | ep->stop_cmds_pending--; |
| 817 | if (xhci->xhc_state & XHCI_STATE_DYING) { |
| 818 | xhci_dbg(xhci, "Stop EP timer ran, but another timer marked " |
| 819 | "xHCI as DYING, exiting.\n"); |
Don Zickus | f43d623 | 2011-10-20 23:52:14 -0400 | [diff] [blame] | 820 | spin_unlock_irqrestore(&xhci->lock, flags); |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 821 | return; |
| 822 | } |
| 823 | if (!(ep->stop_cmds_pending == 0 && (ep->ep_state & EP_HALT_PENDING))) { |
| 824 | xhci_dbg(xhci, "Stop EP timer ran, but no command pending, " |
| 825 | "exiting.\n"); |
Don Zickus | f43d623 | 2011-10-20 23:52:14 -0400 | [diff] [blame] | 826 | spin_unlock_irqrestore(&xhci->lock, flags); |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 827 | return; |
| 828 | } |
| 829 | |
| 830 | xhci_warn(xhci, "xHCI host not responding to stop endpoint command.\n"); |
| 831 | xhci_warn(xhci, "Assuming host is dying, halting host.\n"); |
| 832 | /* Oops, HC is dead or dying or at least not responding to the stop |
| 833 | * endpoint command. |
| 834 | */ |
| 835 | xhci->xhc_state |= XHCI_STATE_DYING; |
| 836 | /* Disable interrupts from the host controller and start halting it */ |
| 837 | xhci_quiesce(xhci); |
Don Zickus | f43d623 | 2011-10-20 23:52:14 -0400 | [diff] [blame] | 838 | spin_unlock_irqrestore(&xhci->lock, flags); |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 839 | |
| 840 | ret = xhci_halt(xhci); |
| 841 | |
Don Zickus | f43d623 | 2011-10-20 23:52:14 -0400 | [diff] [blame] | 842 | spin_lock_irqsave(&xhci->lock, flags); |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 843 | if (ret < 0) { |
| 844 | /* This is bad; the host is not responding to commands and it's |
| 845 | * not allowing itself to be halted. At least interrupts are |
Sarah Sharp | ac04e6f | 2011-03-11 08:47:33 -0800 | [diff] [blame] | 846 | * disabled. If we call usb_hc_died(), it will attempt to |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 847 | * disconnect all device drivers under this host. Those |
| 848 | * disconnect() methods will wait for all URBs to be unlinked, |
| 849 | * so we must complete them. |
| 850 | */ |
| 851 | xhci_warn(xhci, "Non-responsive xHCI host is not halting.\n"); |
| 852 | xhci_warn(xhci, "Completing active URBs anyway.\n"); |
| 853 | /* We could turn all TDs on the rings to no-ops. This won't |
| 854 | * help if the host has cached part of the ring, and is slow if |
| 855 | * we want to preserve the cycle bit. Skip it and hope the host |
| 856 | * doesn't touch the memory. |
| 857 | */ |
| 858 | } |
| 859 | for (i = 0; i < MAX_HC_SLOTS; i++) { |
| 860 | if (!xhci->devs[i]) |
| 861 | continue; |
| 862 | for (j = 0; j < 31; j++) { |
| 863 | temp_ep = &xhci->devs[i]->eps[j]; |
| 864 | ring = temp_ep->ring; |
| 865 | if (!ring) |
| 866 | continue; |
| 867 | xhci_dbg(xhci, "Killing URBs for slot ID %u, " |
| 868 | "ep index %u\n", i, j); |
| 869 | while (!list_empty(&ring->td_list)) { |
| 870 | cur_td = list_first_entry(&ring->td_list, |
| 871 | struct xhci_td, |
| 872 | td_list); |
Sarah Sharp | 585df1d | 2011-08-02 15:43:40 -0700 | [diff] [blame] | 873 | list_del_init(&cur_td->td_list); |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 874 | if (!list_empty(&cur_td->cancelled_td_list)) |
Sarah Sharp | 585df1d | 2011-08-02 15:43:40 -0700 | [diff] [blame] | 875 | list_del_init(&cur_td->cancelled_td_list); |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 876 | xhci_giveback_urb_in_irq(xhci, cur_td, |
| 877 | -ESHUTDOWN, "killed"); |
| 878 | } |
| 879 | while (!list_empty(&temp_ep->cancelled_td_list)) { |
| 880 | cur_td = list_first_entry( |
| 881 | &temp_ep->cancelled_td_list, |
| 882 | struct xhci_td, |
| 883 | cancelled_td_list); |
Sarah Sharp | 585df1d | 2011-08-02 15:43:40 -0700 | [diff] [blame] | 884 | list_del_init(&cur_td->cancelled_td_list); |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 885 | xhci_giveback_urb_in_irq(xhci, cur_td, |
| 886 | -ESHUTDOWN, "killed"); |
| 887 | } |
| 888 | } |
| 889 | } |
Don Zickus | f43d623 | 2011-10-20 23:52:14 -0400 | [diff] [blame] | 890 | spin_unlock_irqrestore(&xhci->lock, flags); |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 891 | xhci_dbg(xhci, "Calling usb_hc_died()\n"); |
Sarah Sharp | f6ff0ac | 2010-12-16 11:21:10 -0800 | [diff] [blame] | 892 | usb_hc_died(xhci_to_hcd(xhci)->primary_hcd); |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 893 | xhci_dbg(xhci, "xHCI host controller is dead.\n"); |
| 894 | } |
| 895 | |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 896 | /* |
| 897 | * When we get a completion for a Set Transfer Ring Dequeue Pointer command, |
| 898 | * we need to clear the set deq pending flag in the endpoint ring state, so that |
| 899 | * the TD queueing code can ring the doorbell again. We also need to ring the |
| 900 | * endpoint doorbell to restart the ring, but only if there aren't more |
| 901 | * cancellations pending. |
| 902 | */ |
| 903 | static void handle_set_deq_completion(struct xhci_hcd *xhci, |
| 904 | struct xhci_event_cmd *event, |
| 905 | union xhci_trb *trb) |
| 906 | { |
| 907 | unsigned int slot_id; |
| 908 | unsigned int ep_index; |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 909 | unsigned int stream_id; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 910 | struct xhci_ring *ep_ring; |
| 911 | struct xhci_virt_device *dev; |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 912 | struct xhci_ep_ctx *ep_ctx; |
| 913 | struct xhci_slot_ctx *slot_ctx; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 914 | |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 915 | slot_id = TRB_TO_SLOT_ID(le32_to_cpu(trb->generic.field[3])); |
| 916 | ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3])); |
| 917 | stream_id = TRB_TO_STREAM_ID(le32_to_cpu(trb->generic.field[2])); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 918 | dev = xhci->devs[slot_id]; |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 919 | |
| 920 | ep_ring = xhci_stream_id_to_ring(dev, ep_index, stream_id); |
| 921 | if (!ep_ring) { |
| 922 | xhci_warn(xhci, "WARN Set TR deq ptr command for " |
| 923 | "freed stream ID %u\n", |
| 924 | stream_id); |
| 925 | /* XXX: Harmless??? */ |
| 926 | dev->eps[ep_index].ep_state &= ~SET_DEQ_PENDING; |
| 927 | return; |
| 928 | } |
| 929 | |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 930 | ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index); |
| 931 | slot_ctx = xhci_get_slot_ctx(xhci, dev->out_ctx); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 932 | |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 933 | if (GET_COMP_CODE(le32_to_cpu(event->status)) != COMP_SUCCESS) { |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 934 | unsigned int ep_state; |
| 935 | unsigned int slot_state; |
| 936 | |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 937 | switch (GET_COMP_CODE(le32_to_cpu(event->status))) { |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 938 | case COMP_TRB_ERR: |
| 939 | xhci_warn(xhci, "WARN Set TR Deq Ptr cmd invalid because " |
| 940 | "of stream ID configuration\n"); |
| 941 | break; |
| 942 | case COMP_CTX_STATE: |
| 943 | xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed due " |
| 944 | "to incorrect slot or ep state.\n"); |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 945 | ep_state = le32_to_cpu(ep_ctx->ep_info); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 946 | ep_state &= EP_STATE_MASK; |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 947 | slot_state = le32_to_cpu(slot_ctx->dev_state); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 948 | slot_state = GET_SLOT_STATE(slot_state); |
| 949 | xhci_dbg(xhci, "Slot state = %u, EP state = %u\n", |
| 950 | slot_state, ep_state); |
| 951 | break; |
| 952 | case COMP_EBADSLT: |
| 953 | xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed because " |
| 954 | "slot %u was not enabled.\n", slot_id); |
| 955 | break; |
| 956 | default: |
| 957 | xhci_warn(xhci, "WARN Set TR Deq Ptr cmd with unknown " |
| 958 | "completion code of %u.\n", |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 959 | GET_COMP_CODE(le32_to_cpu(event->status))); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 960 | break; |
| 961 | } |
| 962 | /* OK what do we do now? The endpoint state is hosed, and we |
| 963 | * should never get to this point if the synchronization between |
| 964 | * queueing, and endpoint state are correct. This might happen |
| 965 | * if the device gets disconnected after we've finished |
| 966 | * cancelling URBs, which might not be an error... |
| 967 | */ |
| 968 | } else { |
Sarah Sharp | 8e595a5 | 2009-07-27 12:03:31 -0700 | [diff] [blame] | 969 | xhci_dbg(xhci, "Successful Set TR Deq Ptr cmd, deq = @%08llx\n", |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 970 | le64_to_cpu(ep_ctx->deq)); |
Sarah Sharp | bf161e8 | 2011-02-23 15:46:42 -0800 | [diff] [blame] | 971 | if (xhci_trb_virt_to_dma(dev->eps[ep_index].queued_deq_seg, |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 972 | dev->eps[ep_index].queued_deq_ptr) == |
| 973 | (le64_to_cpu(ep_ctx->deq) & ~(EP_CTX_CYCLE_MASK))) { |
Sarah Sharp | bf161e8 | 2011-02-23 15:46:42 -0800 | [diff] [blame] | 974 | /* Update the ring's dequeue segment and dequeue pointer |
| 975 | * to reflect the new position. |
| 976 | */ |
| 977 | ep_ring->deq_seg = dev->eps[ep_index].queued_deq_seg; |
| 978 | ep_ring->dequeue = dev->eps[ep_index].queued_deq_ptr; |
| 979 | } else { |
| 980 | xhci_warn(xhci, "Mismatch between completed Set TR Deq " |
| 981 | "Ptr command & xHCI internal state.\n"); |
| 982 | xhci_warn(xhci, "ep deq seg = %p, deq ptr = %p\n", |
| 983 | dev->eps[ep_index].queued_deq_seg, |
| 984 | dev->eps[ep_index].queued_deq_ptr); |
| 985 | } |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 986 | } |
| 987 | |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame] | 988 | dev->eps[ep_index].ep_state &= ~SET_DEQ_PENDING; |
Sarah Sharp | bf161e8 | 2011-02-23 15:46:42 -0800 | [diff] [blame] | 989 | dev->eps[ep_index].queued_deq_seg = NULL; |
| 990 | dev->eps[ep_index].queued_deq_ptr = NULL; |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 991 | /* Restart any rings with pending URBs */ |
| 992 | ring_doorbell_for_active_rings(xhci, slot_id, ep_index); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 993 | } |
| 994 | |
Sarah Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 995 | static void handle_reset_ep_completion(struct xhci_hcd *xhci, |
| 996 | struct xhci_event_cmd *event, |
| 997 | union xhci_trb *trb) |
| 998 | { |
| 999 | int slot_id; |
| 1000 | unsigned int ep_index; |
| 1001 | |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1002 | slot_id = TRB_TO_SLOT_ID(le32_to_cpu(trb->generic.field[3])); |
| 1003 | ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3])); |
Sarah Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 1004 | /* This command will only fail if the endpoint wasn't halted, |
| 1005 | * but we don't care. |
| 1006 | */ |
| 1007 | xhci_dbg(xhci, "Ignoring reset ep completion code of %u\n", |
Matt Evans | f5960b6 | 2011-06-01 10:22:55 +1000 | [diff] [blame] | 1008 | GET_COMP_CODE(le32_to_cpu(event->status))); |
Sarah Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 1009 | |
Sarah Sharp | ac9d8fe | 2009-08-07 14:04:55 -0700 | [diff] [blame] | 1010 | /* HW with the reset endpoint quirk needs to have a configure endpoint |
| 1011 | * command complete before the endpoint can be used. Queue that here |
| 1012 | * because the HW can't handle two commands being queued in a row. |
| 1013 | */ |
| 1014 | if (xhci->quirks & XHCI_RESET_EP_QUIRK) { |
| 1015 | xhci_dbg(xhci, "Queueing configure endpoint command\n"); |
| 1016 | xhci_queue_configure_endpoint(xhci, |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 1017 | xhci->devs[slot_id]->in_ctx->dma, slot_id, |
| 1018 | false); |
Sarah Sharp | ac9d8fe | 2009-08-07 14:04:55 -0700 | [diff] [blame] | 1019 | xhci_ring_cmd_db(xhci); |
| 1020 | } else { |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 1021 | /* Clear our internal halted state and restart the ring(s) */ |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame] | 1022 | xhci->devs[slot_id]->eps[ep_index].ep_state &= ~EP_HALTED; |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 1023 | ring_doorbell_for_active_rings(xhci, slot_id, ep_index); |
Sarah Sharp | ac9d8fe | 2009-08-07 14:04:55 -0700 | [diff] [blame] | 1024 | } |
Sarah Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 1025 | } |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 1026 | |
Sarah Sharp | a50c8aa | 2009-09-04 10:53:15 -0700 | [diff] [blame] | 1027 | /* Check to see if a command in the device's command queue matches this one. |
| 1028 | * Signal the completion or free the command, and return 1. Return 0 if the |
| 1029 | * completed command isn't at the head of the command list. |
| 1030 | */ |
| 1031 | static int handle_cmd_in_cmd_wait_list(struct xhci_hcd *xhci, |
| 1032 | struct xhci_virt_device *virt_dev, |
| 1033 | struct xhci_event_cmd *event) |
| 1034 | { |
| 1035 | struct xhci_command *command; |
| 1036 | |
| 1037 | if (list_empty(&virt_dev->cmd_list)) |
| 1038 | return 0; |
| 1039 | |
| 1040 | command = list_entry(virt_dev->cmd_list.next, |
| 1041 | struct xhci_command, cmd_list); |
| 1042 | if (xhci->cmd_ring->dequeue != command->command_trb) |
| 1043 | return 0; |
| 1044 | |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1045 | command->status = GET_COMP_CODE(le32_to_cpu(event->status)); |
Sarah Sharp | a50c8aa | 2009-09-04 10:53:15 -0700 | [diff] [blame] | 1046 | list_del(&command->cmd_list); |
| 1047 | if (command->completion) |
| 1048 | complete(command->completion); |
| 1049 | else |
| 1050 | xhci_free_command(xhci, command); |
| 1051 | return 1; |
| 1052 | } |
| 1053 | |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 1054 | static void handle_cmd_completion(struct xhci_hcd *xhci, |
| 1055 | struct xhci_event_cmd *event) |
| 1056 | { |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1057 | int slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags)); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 1058 | u64 cmd_dma; |
| 1059 | dma_addr_t cmd_dequeue_dma; |
Sarah Sharp | ac9d8fe | 2009-08-07 14:04:55 -0700 | [diff] [blame] | 1060 | struct xhci_input_control_ctx *ctrl_ctx; |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 1061 | struct xhci_virt_device *virt_dev; |
Sarah Sharp | ac9d8fe | 2009-08-07 14:04:55 -0700 | [diff] [blame] | 1062 | unsigned int ep_index; |
| 1063 | struct xhci_ring *ep_ring; |
| 1064 | unsigned int ep_state; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 1065 | |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1066 | cmd_dma = le64_to_cpu(event->cmd_trb); |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 1067 | cmd_dequeue_dma = xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg, |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 1068 | xhci->cmd_ring->dequeue); |
| 1069 | /* Is the command ring deq ptr out of sync with the deq seg ptr? */ |
| 1070 | if (cmd_dequeue_dma == 0) { |
| 1071 | xhci->error_bitmask |= 1 << 4; |
| 1072 | return; |
| 1073 | } |
| 1074 | /* Does the DMA address match our internal dequeue pointer address? */ |
| 1075 | if (cmd_dma != (u64) cmd_dequeue_dma) { |
| 1076 | xhci->error_bitmask |= 1 << 5; |
| 1077 | return; |
| 1078 | } |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1079 | switch (le32_to_cpu(xhci->cmd_ring->dequeue->generic.field[3]) |
| 1080 | & TRB_TYPE_BITMASK) { |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1081 | case TRB_TYPE(TRB_ENABLE_SLOT): |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1082 | if (GET_COMP_CODE(le32_to_cpu(event->status)) == COMP_SUCCESS) |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1083 | xhci->slot_id = slot_id; |
| 1084 | else |
| 1085 | xhci->slot_id = 0; |
| 1086 | complete(&xhci->addr_dev); |
| 1087 | break; |
| 1088 | case TRB_TYPE(TRB_DISABLE_SLOT): |
Sarah Sharp | 2cf95c1 | 2011-05-11 16:14:58 -0700 | [diff] [blame] | 1089 | if (xhci->devs[slot_id]) { |
| 1090 | if (xhci->quirks & XHCI_EP_LIMIT_QUIRK) |
| 1091 | /* Delete default control endpoint resources */ |
| 1092 | xhci_free_device_endpoint_resources(xhci, |
| 1093 | xhci->devs[slot_id], true); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1094 | xhci_free_virt_device(xhci, slot_id); |
Sarah Sharp | 2cf95c1 | 2011-05-11 16:14:58 -0700 | [diff] [blame] | 1095 | } |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1096 | break; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1097 | case TRB_TYPE(TRB_CONFIG_EP): |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 1098 | virt_dev = xhci->devs[slot_id]; |
Sarah Sharp | a50c8aa | 2009-09-04 10:53:15 -0700 | [diff] [blame] | 1099 | if (handle_cmd_in_cmd_wait_list(xhci, virt_dev, event)) |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 1100 | break; |
Sarah Sharp | ac9d8fe | 2009-08-07 14:04:55 -0700 | [diff] [blame] | 1101 | /* |
| 1102 | * Configure endpoint commands can come from the USB core |
| 1103 | * configuration or alt setting changes, or because the HW |
| 1104 | * needed an extra configure endpoint command after a reset |
Sarah Sharp | 8df75f4 | 2010-04-02 15:34:16 -0700 | [diff] [blame] | 1105 | * endpoint command or streams were being configured. |
| 1106 | * If the command was for a halted endpoint, the xHCI driver |
| 1107 | * is not waiting on the configure endpoint command. |
Sarah Sharp | ac9d8fe | 2009-08-07 14:04:55 -0700 | [diff] [blame] | 1108 | */ |
| 1109 | ctrl_ctx = xhci_get_input_control_ctx(xhci, |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 1110 | virt_dev->in_ctx); |
Sarah Sharp | ac9d8fe | 2009-08-07 14:04:55 -0700 | [diff] [blame] | 1111 | /* Input ctx add_flags are the endpoint index plus one */ |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1112 | ep_index = xhci_last_valid_endpoint(le32_to_cpu(ctrl_ctx->add_flags)) - 1; |
Sarah Sharp | 06df572 | 2009-12-03 09:44:31 -0800 | [diff] [blame] | 1113 | /* A usb_set_interface() call directly after clearing a halted |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 1114 | * condition may race on this quirky hardware. Not worth |
| 1115 | * worrying about, since this is prototype hardware. Not sure |
| 1116 | * if this will work for streams, but streams support was |
| 1117 | * untested on this prototype. |
Sarah Sharp | 06df572 | 2009-12-03 09:44:31 -0800 | [diff] [blame] | 1118 | */ |
Sarah Sharp | ac9d8fe | 2009-08-07 14:04:55 -0700 | [diff] [blame] | 1119 | if (xhci->quirks & XHCI_RESET_EP_QUIRK && |
Sarah Sharp | 06df572 | 2009-12-03 09:44:31 -0800 | [diff] [blame] | 1120 | ep_index != (unsigned int) -1 && |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1121 | le32_to_cpu(ctrl_ctx->add_flags) - SLOT_FLAG == |
| 1122 | le32_to_cpu(ctrl_ctx->drop_flags)) { |
Sarah Sharp | 06df572 | 2009-12-03 09:44:31 -0800 | [diff] [blame] | 1123 | ep_ring = xhci->devs[slot_id]->eps[ep_index].ring; |
| 1124 | ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state; |
| 1125 | if (!(ep_state & EP_HALTED)) |
| 1126 | goto bandwidth_change; |
| 1127 | xhci_dbg(xhci, "Completed config ep cmd - " |
| 1128 | "last ep index = %d, state = %d\n", |
| 1129 | ep_index, ep_state); |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 1130 | /* Clear internal halted state and restart ring(s) */ |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame] | 1131 | xhci->devs[slot_id]->eps[ep_index].ep_state &= |
Sarah Sharp | ac9d8fe | 2009-08-07 14:04:55 -0700 | [diff] [blame] | 1132 | ~EP_HALTED; |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 1133 | ring_doorbell_for_active_rings(xhci, slot_id, ep_index); |
Sarah Sharp | 06df572 | 2009-12-03 09:44:31 -0800 | [diff] [blame] | 1134 | break; |
Sarah Sharp | ac9d8fe | 2009-08-07 14:04:55 -0700 | [diff] [blame] | 1135 | } |
Sarah Sharp | 06df572 | 2009-12-03 09:44:31 -0800 | [diff] [blame] | 1136 | bandwidth_change: |
| 1137 | xhci_dbg(xhci, "Completed config ep cmd\n"); |
| 1138 | xhci->devs[slot_id]->cmd_status = |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1139 | GET_COMP_CODE(le32_to_cpu(event->status)); |
Sarah Sharp | 06df572 | 2009-12-03 09:44:31 -0800 | [diff] [blame] | 1140 | complete(&xhci->devs[slot_id]->cmd_completion); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1141 | break; |
Sarah Sharp | 2d3f1fa | 2009-08-07 14:04:49 -0700 | [diff] [blame] | 1142 | case TRB_TYPE(TRB_EVAL_CONTEXT): |
Sarah Sharp | ac1c1b7 | 2009-09-04 10:53:20 -0700 | [diff] [blame] | 1143 | virt_dev = xhci->devs[slot_id]; |
| 1144 | if (handle_cmd_in_cmd_wait_list(xhci, virt_dev, event)) |
| 1145 | break; |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1146 | xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(le32_to_cpu(event->status)); |
Sarah Sharp | 2d3f1fa | 2009-08-07 14:04:49 -0700 | [diff] [blame] | 1147 | complete(&xhci->devs[slot_id]->cmd_completion); |
| 1148 | break; |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1149 | case TRB_TYPE(TRB_ADDR_DEV): |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1150 | xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(le32_to_cpu(event->status)); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1151 | complete(&xhci->addr_dev); |
| 1152 | break; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 1153 | case TRB_TYPE(TRB_STOP_RING): |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 1154 | handle_stopped_endpoint(xhci, xhci->cmd_ring->dequeue, event); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 1155 | break; |
| 1156 | case TRB_TYPE(TRB_SET_DEQ): |
| 1157 | handle_set_deq_completion(xhci, event, xhci->cmd_ring->dequeue); |
| 1158 | break; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 1159 | case TRB_TYPE(TRB_CMD_NOOP): |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 1160 | break; |
Sarah Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 1161 | case TRB_TYPE(TRB_RESET_EP): |
| 1162 | handle_reset_ep_completion(xhci, event, xhci->cmd_ring->dequeue); |
| 1163 | break; |
Sarah Sharp | 2a8f82c | 2009-12-09 15:59:13 -0800 | [diff] [blame] | 1164 | case TRB_TYPE(TRB_RESET_DEV): |
| 1165 | xhci_dbg(xhci, "Completed reset device command.\n"); |
| 1166 | slot_id = TRB_TO_SLOT_ID( |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1167 | le32_to_cpu(xhci->cmd_ring->dequeue->generic.field[3])); |
Sarah Sharp | 2a8f82c | 2009-12-09 15:59:13 -0800 | [diff] [blame] | 1168 | virt_dev = xhci->devs[slot_id]; |
| 1169 | if (virt_dev) |
| 1170 | handle_cmd_in_cmd_wait_list(xhci, virt_dev, event); |
| 1171 | else |
| 1172 | xhci_warn(xhci, "Reset device command completion " |
| 1173 | "for disabled slot %u\n", slot_id); |
| 1174 | break; |
Sarah Sharp | 0238634 | 2010-05-24 13:25:28 -0700 | [diff] [blame] | 1175 | case TRB_TYPE(TRB_NEC_GET_FW): |
| 1176 | if (!(xhci->quirks & XHCI_NEC_HOST)) { |
| 1177 | xhci->error_bitmask |= 1 << 6; |
| 1178 | break; |
| 1179 | } |
| 1180 | xhci_dbg(xhci, "NEC firmware version %2x.%02x\n", |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1181 | NEC_FW_MAJOR(le32_to_cpu(event->status)), |
| 1182 | NEC_FW_MINOR(le32_to_cpu(event->status))); |
Sarah Sharp | 0238634 | 2010-05-24 13:25:28 -0700 | [diff] [blame] | 1183 | break; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 1184 | default: |
| 1185 | /* Skip over unknown commands on the event ring */ |
| 1186 | xhci->error_bitmask |= 1 << 6; |
| 1187 | break; |
| 1188 | } |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame^] | 1189 | inc_deq(xhci, xhci->cmd_ring); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 1190 | } |
| 1191 | |
Sarah Sharp | 0238634 | 2010-05-24 13:25:28 -0700 | [diff] [blame] | 1192 | static void handle_vendor_event(struct xhci_hcd *xhci, |
| 1193 | union xhci_trb *event) |
| 1194 | { |
| 1195 | u32 trb_type; |
| 1196 | |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1197 | trb_type = TRB_FIELD_TO_TYPE(le32_to_cpu(event->generic.field[3])); |
Sarah Sharp | 0238634 | 2010-05-24 13:25:28 -0700 | [diff] [blame] | 1198 | xhci_dbg(xhci, "Vendor specific event TRB type = %u\n", trb_type); |
| 1199 | if (trb_type == TRB_NEC_CMD_COMP && (xhci->quirks & XHCI_NEC_HOST)) |
| 1200 | handle_cmd_completion(xhci, &event->event_cmd); |
| 1201 | } |
| 1202 | |
Sarah Sharp | f6ff0ac | 2010-12-16 11:21:10 -0800 | [diff] [blame] | 1203 | /* @port_id: the one-based port ID from the hardware (indexed from array of all |
| 1204 | * port registers -- USB 3.0 and USB 2.0). |
| 1205 | * |
| 1206 | * Returns a zero-based port number, which is suitable for indexing into each of |
| 1207 | * the split roothubs' port arrays and bus state arrays. |
Sarah Sharp | d0cd5d4 | 2011-11-14 17:51:39 -0800 | [diff] [blame] | 1208 | * Add one to it in order to call xhci_find_slot_id_by_port. |
Sarah Sharp | f6ff0ac | 2010-12-16 11:21:10 -0800 | [diff] [blame] | 1209 | */ |
| 1210 | static unsigned int find_faked_portnum_from_hw_portnum(struct usb_hcd *hcd, |
| 1211 | struct xhci_hcd *xhci, u32 port_id) |
| 1212 | { |
| 1213 | unsigned int i; |
| 1214 | unsigned int num_similar_speed_ports = 0; |
| 1215 | |
| 1216 | /* port_id from the hardware is 1-based, but port_array[], usb3_ports[], |
| 1217 | * and usb2_ports are 0-based indexes. Count the number of similar |
| 1218 | * speed ports, up to 1 port before this port. |
| 1219 | */ |
| 1220 | for (i = 0; i < (port_id - 1); i++) { |
| 1221 | u8 port_speed = xhci->port_array[i]; |
| 1222 | |
| 1223 | /* |
| 1224 | * Skip ports that don't have known speeds, or have duplicate |
| 1225 | * Extended Capabilities port speed entries. |
| 1226 | */ |
Dan Carpenter | 22e0487 | 2011-03-17 22:39:49 +0300 | [diff] [blame] | 1227 | if (port_speed == 0 || port_speed == DUPLICATE_ENTRY) |
Sarah Sharp | f6ff0ac | 2010-12-16 11:21:10 -0800 | [diff] [blame] | 1228 | continue; |
| 1229 | |
| 1230 | /* |
| 1231 | * USB 3.0 ports are always under a USB 3.0 hub. USB 2.0 and |
| 1232 | * 1.1 ports are under the USB 2.0 hub. If the port speed |
| 1233 | * matches the device speed, it's a similar speed port. |
| 1234 | */ |
| 1235 | if ((port_speed == 0x03) == (hcd->speed == HCD_USB3)) |
| 1236 | num_similar_speed_ports++; |
| 1237 | } |
| 1238 | return num_similar_speed_ports; |
| 1239 | } |
| 1240 | |
Sarah Sharp | 623bef9 | 2011-11-11 14:57:33 -0800 | [diff] [blame] | 1241 | static void handle_device_notification(struct xhci_hcd *xhci, |
| 1242 | union xhci_trb *event) |
| 1243 | { |
| 1244 | u32 slot_id; |
Sarah Sharp | 4ee823b | 2011-11-14 18:00:01 -0800 | [diff] [blame] | 1245 | struct usb_device *udev; |
Sarah Sharp | 623bef9 | 2011-11-11 14:57:33 -0800 | [diff] [blame] | 1246 | |
| 1247 | slot_id = TRB_TO_SLOT_ID(event->generic.field[3]); |
Sarah Sharp | 4ee823b | 2011-11-14 18:00:01 -0800 | [diff] [blame] | 1248 | if (!xhci->devs[slot_id]) { |
Sarah Sharp | 623bef9 | 2011-11-11 14:57:33 -0800 | [diff] [blame] | 1249 | xhci_warn(xhci, "Device Notification event for " |
| 1250 | "unused slot %u\n", slot_id); |
Sarah Sharp | 4ee823b | 2011-11-14 18:00:01 -0800 | [diff] [blame] | 1251 | return; |
| 1252 | } |
| 1253 | |
| 1254 | xhci_dbg(xhci, "Device Wake Notification event for slot ID %u\n", |
| 1255 | slot_id); |
| 1256 | udev = xhci->devs[slot_id]->udev; |
| 1257 | if (udev && udev->parent) |
| 1258 | usb_wakeup_notification(udev->parent, udev->portnum); |
Sarah Sharp | 623bef9 | 2011-11-11 14:57:33 -0800 | [diff] [blame] | 1259 | } |
| 1260 | |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1261 | static void handle_port_status(struct xhci_hcd *xhci, |
| 1262 | union xhci_trb *event) |
| 1263 | { |
Sarah Sharp | f6ff0ac | 2010-12-16 11:21:10 -0800 | [diff] [blame] | 1264 | struct usb_hcd *hcd; |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1265 | u32 port_id; |
Andiry Xu | 5619253 | 2010-10-14 07:23:00 -0700 | [diff] [blame] | 1266 | u32 temp, temp1; |
Sarah Sharp | 518e848 | 2010-12-15 11:56:29 -0800 | [diff] [blame] | 1267 | int max_ports; |
Andiry Xu | 5619253 | 2010-10-14 07:23:00 -0700 | [diff] [blame] | 1268 | int slot_id; |
Sarah Sharp | 5308a91 | 2010-12-01 11:34:59 -0800 | [diff] [blame] | 1269 | unsigned int faked_port_index; |
Sarah Sharp | f6ff0ac | 2010-12-16 11:21:10 -0800 | [diff] [blame] | 1270 | u8 major_revision; |
Sarah Sharp | 20b67cf | 2010-12-15 12:47:14 -0800 | [diff] [blame] | 1271 | struct xhci_bus_state *bus_state; |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1272 | __le32 __iomem **port_array; |
Sarah Sharp | 386139d | 2011-03-24 08:02:58 -0700 | [diff] [blame] | 1273 | bool bogus_port_status = false; |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1274 | |
| 1275 | /* Port status change events always have a successful completion code */ |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1276 | if (GET_COMP_CODE(le32_to_cpu(event->generic.field[2])) != COMP_SUCCESS) { |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1277 | xhci_warn(xhci, "WARN: xHC returned failed port status event\n"); |
| 1278 | xhci->error_bitmask |= 1 << 8; |
| 1279 | } |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1280 | port_id = GET_PORT_ID(le32_to_cpu(event->generic.field[0])); |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1281 | xhci_dbg(xhci, "Port Status Change Event for port %d\n", port_id); |
| 1282 | |
Sarah Sharp | 518e848 | 2010-12-15 11:56:29 -0800 | [diff] [blame] | 1283 | max_ports = HCS_MAX_PORTS(xhci->hcs_params1); |
| 1284 | if ((port_id <= 0) || (port_id > max_ports)) { |
Andiry Xu | 5619253 | 2010-10-14 07:23:00 -0700 | [diff] [blame] | 1285 | xhci_warn(xhci, "Invalid port id %d\n", port_id); |
Sarah Sharp | 386139d | 2011-03-24 08:02:58 -0700 | [diff] [blame] | 1286 | bogus_port_status = true; |
Andiry Xu | 5619253 | 2010-10-14 07:23:00 -0700 | [diff] [blame] | 1287 | goto cleanup; |
| 1288 | } |
| 1289 | |
Sarah Sharp | f6ff0ac | 2010-12-16 11:21:10 -0800 | [diff] [blame] | 1290 | /* Figure out which usb_hcd this port is attached to: |
| 1291 | * is it a USB 3.0 port or a USB 2.0/1.1 port? |
| 1292 | */ |
| 1293 | major_revision = xhci->port_array[port_id - 1]; |
| 1294 | if (major_revision == 0) { |
| 1295 | xhci_warn(xhci, "Event for port %u not in " |
| 1296 | "Extended Capabilities, ignoring.\n", |
| 1297 | port_id); |
Sarah Sharp | 386139d | 2011-03-24 08:02:58 -0700 | [diff] [blame] | 1298 | bogus_port_status = true; |
Sarah Sharp | f6ff0ac | 2010-12-16 11:21:10 -0800 | [diff] [blame] | 1299 | goto cleanup; |
| 1300 | } |
Dan Carpenter | 22e0487 | 2011-03-17 22:39:49 +0300 | [diff] [blame] | 1301 | if (major_revision == DUPLICATE_ENTRY) { |
Sarah Sharp | f6ff0ac | 2010-12-16 11:21:10 -0800 | [diff] [blame] | 1302 | xhci_warn(xhci, "Event for port %u duplicated in" |
| 1303 | "Extended Capabilities, ignoring.\n", |
| 1304 | port_id); |
Sarah Sharp | 386139d | 2011-03-24 08:02:58 -0700 | [diff] [blame] | 1305 | bogus_port_status = true; |
Sarah Sharp | f6ff0ac | 2010-12-16 11:21:10 -0800 | [diff] [blame] | 1306 | goto cleanup; |
Sarah Sharp | 5308a91 | 2010-12-01 11:34:59 -0800 | [diff] [blame] | 1307 | } |
| 1308 | |
Sarah Sharp | f6ff0ac | 2010-12-16 11:21:10 -0800 | [diff] [blame] | 1309 | /* |
| 1310 | * Hardware port IDs reported by a Port Status Change Event include USB |
| 1311 | * 3.0 and USB 2.0 ports. We want to check if the port has reported a |
| 1312 | * resume event, but we first need to translate the hardware port ID |
| 1313 | * into the index into the ports on the correct split roothub, and the |
| 1314 | * correct bus_state structure. |
| 1315 | */ |
| 1316 | /* Find the right roothub. */ |
| 1317 | hcd = xhci_to_hcd(xhci); |
| 1318 | if ((major_revision == 0x03) != (hcd->speed == HCD_USB3)) |
| 1319 | hcd = xhci->shared_hcd; |
| 1320 | bus_state = &xhci->bus_state[hcd_index(hcd)]; |
| 1321 | if (hcd->speed == HCD_USB3) |
| 1322 | port_array = xhci->usb3_ports; |
| 1323 | else |
| 1324 | port_array = xhci->usb2_ports; |
| 1325 | /* Find the faked port hub number */ |
| 1326 | faked_port_index = find_faked_portnum_from_hw_portnum(hcd, xhci, |
| 1327 | port_id); |
| 1328 | |
Sarah Sharp | 5308a91 | 2010-12-01 11:34:59 -0800 | [diff] [blame] | 1329 | temp = xhci_readl(xhci, port_array[faked_port_index]); |
Sarah Sharp | 7111ebc | 2010-12-14 13:24:55 -0800 | [diff] [blame] | 1330 | if (hcd->state == HC_STATE_SUSPENDED) { |
Andiry Xu | 5619253 | 2010-10-14 07:23:00 -0700 | [diff] [blame] | 1331 | xhci_dbg(xhci, "resume root hub\n"); |
| 1332 | usb_hcd_resume_root_hub(hcd); |
| 1333 | } |
| 1334 | |
| 1335 | if ((temp & PORT_PLC) && (temp & PORT_PLS_MASK) == XDEV_RESUME) { |
| 1336 | xhci_dbg(xhci, "port resume event for port %d\n", port_id); |
| 1337 | |
| 1338 | temp1 = xhci_readl(xhci, &xhci->op_regs->command); |
| 1339 | if (!(temp1 & CMD_RUN)) { |
| 1340 | xhci_warn(xhci, "xHC is not running.\n"); |
| 1341 | goto cleanup; |
| 1342 | } |
| 1343 | |
| 1344 | if (DEV_SUPERSPEED(temp)) { |
Sarah Sharp | d93814c | 2012-01-24 16:39:02 -0800 | [diff] [blame] | 1345 | xhci_dbg(xhci, "remote wake SS port %d\n", port_id); |
Sarah Sharp | 4ee823b | 2011-11-14 18:00:01 -0800 | [diff] [blame] | 1346 | /* Set a flag to say the port signaled remote wakeup, |
| 1347 | * so we can tell the difference between the end of |
| 1348 | * device and host initiated resume. |
| 1349 | */ |
| 1350 | bus_state->port_remote_wakeup |= 1 << faked_port_index; |
Sarah Sharp | d93814c | 2012-01-24 16:39:02 -0800 | [diff] [blame] | 1351 | xhci_test_and_clear_bit(xhci, port_array, |
| 1352 | faked_port_index, PORT_PLC); |
Andiry Xu | c9682df | 2011-09-23 14:19:48 -0700 | [diff] [blame] | 1353 | xhci_set_link_state(xhci, port_array, faked_port_index, |
| 1354 | XDEV_U0); |
Sarah Sharp | d93814c | 2012-01-24 16:39:02 -0800 | [diff] [blame] | 1355 | /* Need to wait until the next link state change |
| 1356 | * indicates the device is actually in U0. |
| 1357 | */ |
| 1358 | bogus_port_status = true; |
| 1359 | goto cleanup; |
Andiry Xu | 5619253 | 2010-10-14 07:23:00 -0700 | [diff] [blame] | 1360 | } else { |
| 1361 | xhci_dbg(xhci, "resume HS port %d\n", port_id); |
Sarah Sharp | f6ff0ac | 2010-12-16 11:21:10 -0800 | [diff] [blame] | 1362 | bus_state->resume_done[faked_port_index] = jiffies + |
Andiry Xu | 5619253 | 2010-10-14 07:23:00 -0700 | [diff] [blame] | 1363 | msecs_to_jiffies(20); |
| 1364 | mod_timer(&hcd->rh_timer, |
Sarah Sharp | f6ff0ac | 2010-12-16 11:21:10 -0800 | [diff] [blame] | 1365 | bus_state->resume_done[faked_port_index]); |
Andiry Xu | 5619253 | 2010-10-14 07:23:00 -0700 | [diff] [blame] | 1366 | /* Do the rest in GetPortStatus */ |
| 1367 | } |
| 1368 | } |
| 1369 | |
Sarah Sharp | d93814c | 2012-01-24 16:39:02 -0800 | [diff] [blame] | 1370 | if ((temp & PORT_PLC) && (temp & PORT_PLS_MASK) == XDEV_U0 && |
| 1371 | DEV_SUPERSPEED(temp)) { |
| 1372 | xhci_dbg(xhci, "resume SS port %d finished\n", port_id); |
Sarah Sharp | 4ee823b | 2011-11-14 18:00:01 -0800 | [diff] [blame] | 1373 | /* We've just brought the device into U0 through either the |
| 1374 | * Resume state after a device remote wakeup, or through the |
| 1375 | * U3Exit state after a host-initiated resume. If it's a device |
| 1376 | * initiated remote wake, don't pass up the link state change, |
| 1377 | * so the roothub behavior is consistent with external |
| 1378 | * USB 3.0 hub behavior. |
| 1379 | */ |
Sarah Sharp | d93814c | 2012-01-24 16:39:02 -0800 | [diff] [blame] | 1380 | slot_id = xhci_find_slot_id_by_port(hcd, xhci, |
| 1381 | faked_port_index + 1); |
| 1382 | if (slot_id && xhci->devs[slot_id]) |
| 1383 | xhci_ring_device(xhci, slot_id); |
Sarah Sharp | 4ee823b | 2011-11-14 18:00:01 -0800 | [diff] [blame] | 1384 | if (bus_state->port_remote_wakeup && (1 << faked_port_index)) { |
| 1385 | bus_state->port_remote_wakeup &= |
| 1386 | ~(1 << faked_port_index); |
| 1387 | xhci_test_and_clear_bit(xhci, port_array, |
| 1388 | faked_port_index, PORT_PLC); |
| 1389 | usb_wakeup_notification(hcd->self.root_hub, |
| 1390 | faked_port_index + 1); |
| 1391 | bogus_port_status = true; |
| 1392 | goto cleanup; |
| 1393 | } |
Sarah Sharp | d93814c | 2012-01-24 16:39:02 -0800 | [diff] [blame] | 1394 | } |
| 1395 | |
Andiry Xu | 6fd4562 | 2011-09-23 14:19:50 -0700 | [diff] [blame] | 1396 | if (hcd->speed != HCD_USB3) |
| 1397 | xhci_test_and_clear_bit(xhci, port_array, faked_port_index, |
| 1398 | PORT_PLC); |
| 1399 | |
Andiry Xu | 5619253 | 2010-10-14 07:23:00 -0700 | [diff] [blame] | 1400 | cleanup: |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1401 | /* Update event ring dequeue pointer before dropping the lock */ |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame^] | 1402 | inc_deq(xhci, xhci->event_ring); |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1403 | |
Sarah Sharp | 386139d | 2011-03-24 08:02:58 -0700 | [diff] [blame] | 1404 | /* Don't make the USB core poll the roothub if we got a bad port status |
| 1405 | * change event. Besides, at that point we can't tell which roothub |
| 1406 | * (USB 2.0 or USB 3.0) to kick. |
| 1407 | */ |
| 1408 | if (bogus_port_status) |
| 1409 | return; |
| 1410 | |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1411 | spin_unlock(&xhci->lock); |
| 1412 | /* Pass this up to the core */ |
Sarah Sharp | f6ff0ac | 2010-12-16 11:21:10 -0800 | [diff] [blame] | 1413 | usb_hcd_poll_rh_status(hcd); |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 1414 | spin_lock(&xhci->lock); |
| 1415 | } |
| 1416 | |
| 1417 | /* |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 1418 | * This TD is defined by the TRBs starting at start_trb in start_seg and ending |
| 1419 | * at end_trb, which may be in another segment. If the suspect DMA address is a |
| 1420 | * TRB in this TD, this function returns that TRB's segment. Otherwise it |
| 1421 | * returns 0. |
| 1422 | */ |
Sarah Sharp | 6648f29 | 2009-11-09 13:35:23 -0800 | [diff] [blame] | 1423 | struct xhci_segment *trb_in_td(struct xhci_segment *start_seg, |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 1424 | union xhci_trb *start_trb, |
| 1425 | union xhci_trb *end_trb, |
| 1426 | dma_addr_t suspect_dma) |
| 1427 | { |
| 1428 | dma_addr_t start_dma; |
| 1429 | dma_addr_t end_seg_dma; |
| 1430 | dma_addr_t end_trb_dma; |
| 1431 | struct xhci_segment *cur_seg; |
| 1432 | |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 1433 | start_dma = xhci_trb_virt_to_dma(start_seg, start_trb); |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 1434 | cur_seg = start_seg; |
| 1435 | |
| 1436 | do { |
Sarah Sharp | 2fa88da | 2009-11-03 22:02:24 -0800 | [diff] [blame] | 1437 | if (start_dma == 0) |
Randy Dunlap | 326b481 | 2010-04-19 08:53:50 -0700 | [diff] [blame] | 1438 | return NULL; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 1439 | /* We may get an event for a Link TRB in the middle of a TD */ |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 1440 | end_seg_dma = xhci_trb_virt_to_dma(cur_seg, |
Sarah Sharp | 2fa88da | 2009-11-03 22:02:24 -0800 | [diff] [blame] | 1441 | &cur_seg->trbs[TRBS_PER_SEGMENT - 1]); |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 1442 | /* If the end TRB isn't in this segment, this is set to 0 */ |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 1443 | end_trb_dma = xhci_trb_virt_to_dma(cur_seg, end_trb); |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 1444 | |
| 1445 | if (end_trb_dma > 0) { |
| 1446 | /* The end TRB is in this segment, so suspect should be here */ |
| 1447 | if (start_dma <= end_trb_dma) { |
| 1448 | if (suspect_dma >= start_dma && suspect_dma <= end_trb_dma) |
| 1449 | return cur_seg; |
| 1450 | } else { |
| 1451 | /* Case for one segment with |
| 1452 | * a TD wrapped around to the top |
| 1453 | */ |
| 1454 | if ((suspect_dma >= start_dma && |
| 1455 | suspect_dma <= end_seg_dma) || |
| 1456 | (suspect_dma >= cur_seg->dma && |
| 1457 | suspect_dma <= end_trb_dma)) |
| 1458 | return cur_seg; |
| 1459 | } |
Randy Dunlap | 326b481 | 2010-04-19 08:53:50 -0700 | [diff] [blame] | 1460 | return NULL; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 1461 | } else { |
| 1462 | /* Might still be somewhere in this segment */ |
| 1463 | if (suspect_dma >= start_dma && suspect_dma <= end_seg_dma) |
| 1464 | return cur_seg; |
| 1465 | } |
| 1466 | cur_seg = cur_seg->next; |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 1467 | start_dma = xhci_trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]); |
Sarah Sharp | 2fa88da | 2009-11-03 22:02:24 -0800 | [diff] [blame] | 1468 | } while (cur_seg != start_seg); |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 1469 | |
Randy Dunlap | 326b481 | 2010-04-19 08:53:50 -0700 | [diff] [blame] | 1470 | return NULL; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 1471 | } |
| 1472 | |
Sarah Sharp | bcef3fd | 2009-11-11 10:28:44 -0800 | [diff] [blame] | 1473 | static void xhci_cleanup_halted_endpoint(struct xhci_hcd *xhci, |
| 1474 | unsigned int slot_id, unsigned int ep_index, |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 1475 | unsigned int stream_id, |
Sarah Sharp | bcef3fd | 2009-11-11 10:28:44 -0800 | [diff] [blame] | 1476 | struct xhci_td *td, union xhci_trb *event_trb) |
| 1477 | { |
| 1478 | struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index]; |
| 1479 | ep->ep_state |= EP_HALTED; |
| 1480 | ep->stopped_td = td; |
| 1481 | ep->stopped_trb = event_trb; |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 1482 | ep->stopped_stream = stream_id; |
Sarah Sharp | 1624ae1 | 2010-05-06 13:40:08 -0700 | [diff] [blame] | 1483 | |
Sarah Sharp | bcef3fd | 2009-11-11 10:28:44 -0800 | [diff] [blame] | 1484 | xhci_queue_reset_ep(xhci, slot_id, ep_index); |
| 1485 | xhci_cleanup_stalled_ring(xhci, td->urb->dev, ep_index); |
Sarah Sharp | 1624ae1 | 2010-05-06 13:40:08 -0700 | [diff] [blame] | 1486 | |
| 1487 | ep->stopped_td = NULL; |
| 1488 | ep->stopped_trb = NULL; |
Sarah Sharp | 5e5cf6f | 2010-05-06 13:40:18 -0700 | [diff] [blame] | 1489 | ep->stopped_stream = 0; |
Sarah Sharp | 1624ae1 | 2010-05-06 13:40:08 -0700 | [diff] [blame] | 1490 | |
Sarah Sharp | bcef3fd | 2009-11-11 10:28:44 -0800 | [diff] [blame] | 1491 | xhci_ring_cmd_db(xhci); |
| 1492 | } |
| 1493 | |
| 1494 | /* Check if an error has halted the endpoint ring. The class driver will |
| 1495 | * cleanup the halt for a non-default control endpoint if we indicate a stall. |
| 1496 | * However, a babble and other errors also halt the endpoint ring, and the class |
| 1497 | * driver won't clear the halt in that case, so we need to issue a Set Transfer |
| 1498 | * Ring Dequeue Pointer command manually. |
| 1499 | */ |
| 1500 | static int xhci_requires_manual_halt_cleanup(struct xhci_hcd *xhci, |
| 1501 | struct xhci_ep_ctx *ep_ctx, |
| 1502 | unsigned int trb_comp_code) |
| 1503 | { |
| 1504 | /* TRB completion codes that may require a manual halt cleanup */ |
| 1505 | if (trb_comp_code == COMP_TX_ERR || |
| 1506 | trb_comp_code == COMP_BABBLE || |
| 1507 | trb_comp_code == COMP_SPLIT_ERR) |
| 1508 | /* The 0.96 spec says a babbling control endpoint |
| 1509 | * is not halted. The 0.96 spec says it is. Some HW |
| 1510 | * claims to be 0.95 compliant, but it halts the control |
| 1511 | * endpoint anyway. Check if a babble halted the |
| 1512 | * endpoint. |
| 1513 | */ |
Matt Evans | f5960b6 | 2011-06-01 10:22:55 +1000 | [diff] [blame] | 1514 | if ((ep_ctx->ep_info & cpu_to_le32(EP_STATE_MASK)) == |
| 1515 | cpu_to_le32(EP_STATE_HALTED)) |
Sarah Sharp | bcef3fd | 2009-11-11 10:28:44 -0800 | [diff] [blame] | 1516 | return 1; |
| 1517 | |
| 1518 | return 0; |
| 1519 | } |
| 1520 | |
Sarah Sharp | b45b506 | 2009-12-09 15:59:06 -0800 | [diff] [blame] | 1521 | int xhci_is_vendor_info_code(struct xhci_hcd *xhci, unsigned int trb_comp_code) |
| 1522 | { |
| 1523 | if (trb_comp_code >= 224 && trb_comp_code <= 255) { |
| 1524 | /* Vendor defined "informational" completion code, |
| 1525 | * treat as not-an-error. |
| 1526 | */ |
| 1527 | xhci_dbg(xhci, "Vendor defined info completion code %u\n", |
| 1528 | trb_comp_code); |
| 1529 | xhci_dbg(xhci, "Treating code as success.\n"); |
| 1530 | return 1; |
| 1531 | } |
| 1532 | return 0; |
| 1533 | } |
| 1534 | |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 1535 | /* |
Andiry Xu | 4422da6 | 2010-07-22 15:22:55 -0700 | [diff] [blame] | 1536 | * Finish the td processing, remove the td from td list; |
| 1537 | * Return 1 if the urb can be given back. |
| 1538 | */ |
| 1539 | static int finish_td(struct xhci_hcd *xhci, struct xhci_td *td, |
| 1540 | union xhci_trb *event_trb, struct xhci_transfer_event *event, |
| 1541 | struct xhci_virt_ep *ep, int *status, bool skip) |
| 1542 | { |
| 1543 | struct xhci_virt_device *xdev; |
| 1544 | struct xhci_ring *ep_ring; |
| 1545 | unsigned int slot_id; |
| 1546 | int ep_index; |
| 1547 | struct urb *urb = NULL; |
| 1548 | struct xhci_ep_ctx *ep_ctx; |
| 1549 | int ret = 0; |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 1550 | struct urb_priv *urb_priv; |
Andiry Xu | 4422da6 | 2010-07-22 15:22:55 -0700 | [diff] [blame] | 1551 | u32 trb_comp_code; |
| 1552 | |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1553 | slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags)); |
Andiry Xu | 4422da6 | 2010-07-22 15:22:55 -0700 | [diff] [blame] | 1554 | xdev = xhci->devs[slot_id]; |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1555 | ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1; |
| 1556 | ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer)); |
Andiry Xu | 4422da6 | 2010-07-22 15:22:55 -0700 | [diff] [blame] | 1557 | ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index); |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1558 | trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len)); |
Andiry Xu | 4422da6 | 2010-07-22 15:22:55 -0700 | [diff] [blame] | 1559 | |
| 1560 | if (skip) |
| 1561 | goto td_cleanup; |
| 1562 | |
| 1563 | if (trb_comp_code == COMP_STOP_INVAL || |
| 1564 | trb_comp_code == COMP_STOP) { |
| 1565 | /* The Endpoint Stop Command completion will take care of any |
| 1566 | * stopped TDs. A stopped TD may be restarted, so don't update |
| 1567 | * the ring dequeue pointer or take this TD off any lists yet. |
| 1568 | */ |
| 1569 | ep->stopped_td = td; |
| 1570 | ep->stopped_trb = event_trb; |
| 1571 | return 0; |
| 1572 | } else { |
| 1573 | if (trb_comp_code == COMP_STALL) { |
| 1574 | /* The transfer is completed from the driver's |
| 1575 | * perspective, but we need to issue a set dequeue |
| 1576 | * command for this stalled endpoint to move the dequeue |
| 1577 | * pointer past the TD. We can't do that here because |
| 1578 | * the halt condition must be cleared first. Let the |
| 1579 | * USB class driver clear the stall later. |
| 1580 | */ |
| 1581 | ep->stopped_td = td; |
| 1582 | ep->stopped_trb = event_trb; |
| 1583 | ep->stopped_stream = ep_ring->stream_id; |
| 1584 | } else if (xhci_requires_manual_halt_cleanup(xhci, |
| 1585 | ep_ctx, trb_comp_code)) { |
| 1586 | /* Other types of errors halt the endpoint, but the |
| 1587 | * class driver doesn't call usb_reset_endpoint() unless |
| 1588 | * the error is -EPIPE. Clear the halted status in the |
| 1589 | * xHCI hardware manually. |
| 1590 | */ |
| 1591 | xhci_cleanup_halted_endpoint(xhci, |
| 1592 | slot_id, ep_index, ep_ring->stream_id, |
| 1593 | td, event_trb); |
| 1594 | } else { |
| 1595 | /* Update ring dequeue pointer */ |
| 1596 | while (ep_ring->dequeue != td->last_trb) |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame^] | 1597 | inc_deq(xhci, ep_ring); |
| 1598 | inc_deq(xhci, ep_ring); |
Andiry Xu | 4422da6 | 2010-07-22 15:22:55 -0700 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | td_cleanup: |
| 1602 | /* Clean up the endpoint's TD list */ |
| 1603 | urb = td->urb; |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 1604 | urb_priv = urb->hcpriv; |
Andiry Xu | 4422da6 | 2010-07-22 15:22:55 -0700 | [diff] [blame] | 1605 | |
| 1606 | /* Do one last check of the actual transfer length. |
| 1607 | * If the host controller said we transferred more data than |
| 1608 | * the buffer length, urb->actual_length will be a very big |
| 1609 | * number (since it's unsigned). Play it safe and say we didn't |
| 1610 | * transfer anything. |
| 1611 | */ |
| 1612 | if (urb->actual_length > urb->transfer_buffer_length) { |
| 1613 | xhci_warn(xhci, "URB transfer length is wrong, " |
| 1614 | "xHC issue? req. len = %u, " |
| 1615 | "act. len = %u\n", |
| 1616 | urb->transfer_buffer_length, |
| 1617 | urb->actual_length); |
| 1618 | urb->actual_length = 0; |
| 1619 | if (td->urb->transfer_flags & URB_SHORT_NOT_OK) |
| 1620 | *status = -EREMOTEIO; |
| 1621 | else |
| 1622 | *status = 0; |
| 1623 | } |
Sarah Sharp | 585df1d | 2011-08-02 15:43:40 -0700 | [diff] [blame] | 1624 | list_del_init(&td->td_list); |
Andiry Xu | 4422da6 | 2010-07-22 15:22:55 -0700 | [diff] [blame] | 1625 | /* Was this TD slated to be cancelled but completed anyway? */ |
| 1626 | if (!list_empty(&td->cancelled_td_list)) |
Sarah Sharp | 585df1d | 2011-08-02 15:43:40 -0700 | [diff] [blame] | 1627 | list_del_init(&td->cancelled_td_list); |
Andiry Xu | 4422da6 | 2010-07-22 15:22:55 -0700 | [diff] [blame] | 1628 | |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 1629 | urb_priv->td_cnt++; |
| 1630 | /* Giveback the urb when all the tds are completed */ |
Andiry Xu | c41136b | 2011-03-22 17:08:14 +0800 | [diff] [blame] | 1631 | if (urb_priv->td_cnt == urb_priv->length) { |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 1632 | ret = 1; |
Andiry Xu | c41136b | 2011-03-22 17:08:14 +0800 | [diff] [blame] | 1633 | if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) { |
| 1634 | xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs--; |
| 1635 | if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs |
| 1636 | == 0) { |
| 1637 | if (xhci->quirks & XHCI_AMD_PLL_FIX) |
| 1638 | usb_amd_quirk_pll_enable(); |
| 1639 | } |
| 1640 | } |
| 1641 | } |
Andiry Xu | 4422da6 | 2010-07-22 15:22:55 -0700 | [diff] [blame] | 1642 | } |
| 1643 | |
| 1644 | return ret; |
| 1645 | } |
| 1646 | |
| 1647 | /* |
Andiry Xu | 8af56be | 2010-07-22 15:23:03 -0700 | [diff] [blame] | 1648 | * Process control tds, update urb status and actual_length. |
| 1649 | */ |
| 1650 | static int process_ctrl_td(struct xhci_hcd *xhci, struct xhci_td *td, |
| 1651 | union xhci_trb *event_trb, struct xhci_transfer_event *event, |
| 1652 | struct xhci_virt_ep *ep, int *status) |
| 1653 | { |
| 1654 | struct xhci_virt_device *xdev; |
| 1655 | struct xhci_ring *ep_ring; |
| 1656 | unsigned int slot_id; |
| 1657 | int ep_index; |
| 1658 | struct xhci_ep_ctx *ep_ctx; |
| 1659 | u32 trb_comp_code; |
| 1660 | |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1661 | slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags)); |
Andiry Xu | 8af56be | 2010-07-22 15:23:03 -0700 | [diff] [blame] | 1662 | xdev = xhci->devs[slot_id]; |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1663 | ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1; |
| 1664 | ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer)); |
Andiry Xu | 8af56be | 2010-07-22 15:23:03 -0700 | [diff] [blame] | 1665 | ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index); |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1666 | trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len)); |
Andiry Xu | 8af56be | 2010-07-22 15:23:03 -0700 | [diff] [blame] | 1667 | |
Andiry Xu | 8af56be | 2010-07-22 15:23:03 -0700 | [diff] [blame] | 1668 | switch (trb_comp_code) { |
| 1669 | case COMP_SUCCESS: |
| 1670 | if (event_trb == ep_ring->dequeue) { |
| 1671 | xhci_warn(xhci, "WARN: Success on ctrl setup TRB " |
| 1672 | "without IOC set??\n"); |
| 1673 | *status = -ESHUTDOWN; |
| 1674 | } else if (event_trb != td->last_trb) { |
| 1675 | xhci_warn(xhci, "WARN: Success on ctrl data TRB " |
| 1676 | "without IOC set??\n"); |
| 1677 | *status = -ESHUTDOWN; |
| 1678 | } else { |
Andiry Xu | 8af56be | 2010-07-22 15:23:03 -0700 | [diff] [blame] | 1679 | *status = 0; |
| 1680 | } |
| 1681 | break; |
| 1682 | case COMP_SHORT_TX: |
Andiry Xu | 8af56be | 2010-07-22 15:23:03 -0700 | [diff] [blame] | 1683 | if (td->urb->transfer_flags & URB_SHORT_NOT_OK) |
| 1684 | *status = -EREMOTEIO; |
| 1685 | else |
| 1686 | *status = 0; |
| 1687 | break; |
Sarah Sharp | 3abeca9 | 2011-05-05 19:08:09 -0700 | [diff] [blame] | 1688 | case COMP_STOP_INVAL: |
| 1689 | case COMP_STOP: |
| 1690 | return finish_td(xhci, td, event_trb, event, ep, status, false); |
Andiry Xu | 8af56be | 2010-07-22 15:23:03 -0700 | [diff] [blame] | 1691 | default: |
| 1692 | if (!xhci_requires_manual_halt_cleanup(xhci, |
| 1693 | ep_ctx, trb_comp_code)) |
| 1694 | break; |
| 1695 | xhci_dbg(xhci, "TRB error code %u, " |
| 1696 | "halted endpoint index = %u\n", |
| 1697 | trb_comp_code, ep_index); |
| 1698 | /* else fall through */ |
| 1699 | case COMP_STALL: |
| 1700 | /* Did we transfer part of the data (middle) phase? */ |
| 1701 | if (event_trb != ep_ring->dequeue && |
| 1702 | event_trb != td->last_trb) |
| 1703 | td->urb->actual_length = |
| 1704 | td->urb->transfer_buffer_length |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1705 | - TRB_LEN(le32_to_cpu(event->transfer_len)); |
Andiry Xu | 8af56be | 2010-07-22 15:23:03 -0700 | [diff] [blame] | 1706 | else |
| 1707 | td->urb->actual_length = 0; |
| 1708 | |
| 1709 | xhci_cleanup_halted_endpoint(xhci, |
| 1710 | slot_id, ep_index, 0, td, event_trb); |
| 1711 | return finish_td(xhci, td, event_trb, event, ep, status, true); |
| 1712 | } |
| 1713 | /* |
| 1714 | * Did we transfer any data, despite the errors that might have |
| 1715 | * happened? I.e. did we get past the setup stage? |
| 1716 | */ |
| 1717 | if (event_trb != ep_ring->dequeue) { |
| 1718 | /* The event was for the status stage */ |
| 1719 | if (event_trb == td->last_trb) { |
| 1720 | if (td->urb->actual_length != 0) { |
| 1721 | /* Don't overwrite a previously set error code |
| 1722 | */ |
| 1723 | if ((*status == -EINPROGRESS || *status == 0) && |
| 1724 | (td->urb->transfer_flags |
| 1725 | & URB_SHORT_NOT_OK)) |
| 1726 | /* Did we already see a short data |
| 1727 | * stage? */ |
| 1728 | *status = -EREMOTEIO; |
| 1729 | } else { |
| 1730 | td->urb->actual_length = |
| 1731 | td->urb->transfer_buffer_length; |
| 1732 | } |
| 1733 | } else { |
| 1734 | /* Maybe the event was for the data stage? */ |
Sarah Sharp | 3abeca9 | 2011-05-05 19:08:09 -0700 | [diff] [blame] | 1735 | td->urb->actual_length = |
| 1736 | td->urb->transfer_buffer_length - |
| 1737 | TRB_LEN(le32_to_cpu(event->transfer_len)); |
| 1738 | xhci_dbg(xhci, "Waiting for status " |
| 1739 | "stage event\n"); |
| 1740 | return 0; |
Andiry Xu | 8af56be | 2010-07-22 15:23:03 -0700 | [diff] [blame] | 1741 | } |
| 1742 | } |
| 1743 | |
| 1744 | return finish_td(xhci, td, event_trb, event, ep, status, false); |
| 1745 | } |
| 1746 | |
| 1747 | /* |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 1748 | * Process isochronous tds, update urb packet status and actual_length. |
| 1749 | */ |
| 1750 | static int process_isoc_td(struct xhci_hcd *xhci, struct xhci_td *td, |
| 1751 | union xhci_trb *event_trb, struct xhci_transfer_event *event, |
| 1752 | struct xhci_virt_ep *ep, int *status) |
| 1753 | { |
| 1754 | struct xhci_ring *ep_ring; |
| 1755 | struct urb_priv *urb_priv; |
| 1756 | int idx; |
| 1757 | int len = 0; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 1758 | union xhci_trb *cur_trb; |
| 1759 | struct xhci_segment *cur_seg; |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 1760 | struct usb_iso_packet_descriptor *frame; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 1761 | u32 trb_comp_code; |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 1762 | bool skip_td = false; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 1763 | |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1764 | ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer)); |
| 1765 | trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len)); |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 1766 | urb_priv = td->urb->hcpriv; |
| 1767 | idx = urb_priv->td_cnt; |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 1768 | frame = &td->urb->iso_frame_desc[idx]; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 1769 | |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 1770 | /* handle completion code */ |
| 1771 | switch (trb_comp_code) { |
| 1772 | case COMP_SUCCESS: |
| 1773 | frame->status = 0; |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 1774 | break; |
| 1775 | case COMP_SHORT_TX: |
| 1776 | frame->status = td->urb->transfer_flags & URB_SHORT_NOT_OK ? |
| 1777 | -EREMOTEIO : 0; |
| 1778 | break; |
| 1779 | case COMP_BW_OVER: |
| 1780 | frame->status = -ECOMM; |
| 1781 | skip_td = true; |
| 1782 | break; |
| 1783 | case COMP_BUFF_OVER: |
| 1784 | case COMP_BABBLE: |
| 1785 | frame->status = -EOVERFLOW; |
| 1786 | skip_td = true; |
| 1787 | break; |
Alex He | f6ba6fe | 2011-06-08 18:34:06 +0800 | [diff] [blame] | 1788 | case COMP_DEV_ERR: |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 1789 | case COMP_STALL: |
| 1790 | frame->status = -EPROTO; |
| 1791 | skip_td = true; |
| 1792 | break; |
| 1793 | case COMP_STOP: |
| 1794 | case COMP_STOP_INVAL: |
| 1795 | break; |
| 1796 | default: |
| 1797 | frame->status = -1; |
| 1798 | break; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 1799 | } |
| 1800 | |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 1801 | if (trb_comp_code == COMP_SUCCESS || skip_td) { |
| 1802 | frame->actual_length = frame->length; |
| 1803 | td->urb->actual_length += frame->length; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 1804 | } else { |
| 1805 | for (cur_trb = ep_ring->dequeue, |
| 1806 | cur_seg = ep_ring->deq_seg; cur_trb != event_trb; |
| 1807 | next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) { |
Matt Evans | f5960b6 | 2011-06-01 10:22:55 +1000 | [diff] [blame] | 1808 | if (!TRB_TYPE_NOOP_LE32(cur_trb->generic.field[3]) && |
| 1809 | !TRB_TYPE_LINK_LE32(cur_trb->generic.field[3])) |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1810 | len += TRB_LEN(le32_to_cpu(cur_trb->generic.field[2])); |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 1811 | } |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1812 | len += TRB_LEN(le32_to_cpu(cur_trb->generic.field[2])) - |
| 1813 | TRB_LEN(le32_to_cpu(event->transfer_len)); |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 1814 | |
| 1815 | if (trb_comp_code != COMP_STOP_INVAL) { |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 1816 | frame->actual_length = len; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 1817 | td->urb->actual_length += len; |
| 1818 | } |
| 1819 | } |
| 1820 | |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 1821 | return finish_td(xhci, td, event_trb, event, ep, status, false); |
| 1822 | } |
| 1823 | |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 1824 | static int skip_isoc_td(struct xhci_hcd *xhci, struct xhci_td *td, |
| 1825 | struct xhci_transfer_event *event, |
| 1826 | struct xhci_virt_ep *ep, int *status) |
| 1827 | { |
| 1828 | struct xhci_ring *ep_ring; |
| 1829 | struct urb_priv *urb_priv; |
| 1830 | struct usb_iso_packet_descriptor *frame; |
| 1831 | int idx; |
| 1832 | |
Matt Evans | f697531 | 2011-06-01 13:01:01 +1000 | [diff] [blame] | 1833 | ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer)); |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 1834 | urb_priv = td->urb->hcpriv; |
| 1835 | idx = urb_priv->td_cnt; |
| 1836 | frame = &td->urb->iso_frame_desc[idx]; |
| 1837 | |
Sarah Sharp | b3df3f9 | 2011-06-15 19:57:46 -0700 | [diff] [blame] | 1838 | /* The transfer is partly done. */ |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 1839 | frame->status = -EXDEV; |
| 1840 | |
| 1841 | /* calc actual length */ |
| 1842 | frame->actual_length = 0; |
| 1843 | |
| 1844 | /* Update ring dequeue pointer */ |
| 1845 | while (ep_ring->dequeue != td->last_trb) |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame^] | 1846 | inc_deq(xhci, ep_ring); |
| 1847 | inc_deq(xhci, ep_ring); |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 1848 | |
| 1849 | return finish_td(xhci, td, NULL, event, ep, status, true); |
| 1850 | } |
| 1851 | |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 1852 | /* |
Andiry Xu | 22405ed | 2010-07-22 15:23:08 -0700 | [diff] [blame] | 1853 | * Process bulk and interrupt tds, update urb status and actual_length. |
| 1854 | */ |
| 1855 | static int process_bulk_intr_td(struct xhci_hcd *xhci, struct xhci_td *td, |
| 1856 | union xhci_trb *event_trb, struct xhci_transfer_event *event, |
| 1857 | struct xhci_virt_ep *ep, int *status) |
| 1858 | { |
| 1859 | struct xhci_ring *ep_ring; |
| 1860 | union xhci_trb *cur_trb; |
| 1861 | struct xhci_segment *cur_seg; |
| 1862 | u32 trb_comp_code; |
| 1863 | |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1864 | ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer)); |
| 1865 | trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len)); |
Andiry Xu | 22405ed | 2010-07-22 15:23:08 -0700 | [diff] [blame] | 1866 | |
| 1867 | switch (trb_comp_code) { |
| 1868 | case COMP_SUCCESS: |
| 1869 | /* Double check that the HW transferred everything. */ |
| 1870 | if (event_trb != td->last_trb) { |
| 1871 | xhci_warn(xhci, "WARN Successful completion " |
| 1872 | "on short TX\n"); |
| 1873 | if (td->urb->transfer_flags & URB_SHORT_NOT_OK) |
| 1874 | *status = -EREMOTEIO; |
| 1875 | else |
| 1876 | *status = 0; |
| 1877 | } else { |
Andiry Xu | 22405ed | 2010-07-22 15:23:08 -0700 | [diff] [blame] | 1878 | *status = 0; |
| 1879 | } |
| 1880 | break; |
| 1881 | case COMP_SHORT_TX: |
| 1882 | if (td->urb->transfer_flags & URB_SHORT_NOT_OK) |
| 1883 | *status = -EREMOTEIO; |
| 1884 | else |
| 1885 | *status = 0; |
| 1886 | break; |
| 1887 | default: |
| 1888 | /* Others already handled above */ |
| 1889 | break; |
| 1890 | } |
Sarah Sharp | f444ff2 | 2011-04-05 15:53:47 -0700 | [diff] [blame] | 1891 | if (trb_comp_code == COMP_SHORT_TX) |
| 1892 | xhci_dbg(xhci, "ep %#x - asked for %d bytes, " |
| 1893 | "%d bytes untransferred\n", |
| 1894 | td->urb->ep->desc.bEndpointAddress, |
| 1895 | td->urb->transfer_buffer_length, |
| 1896 | TRB_LEN(le32_to_cpu(event->transfer_len))); |
Andiry Xu | 22405ed | 2010-07-22 15:23:08 -0700 | [diff] [blame] | 1897 | /* Fast path - was this the last TRB in the TD for this URB? */ |
| 1898 | if (event_trb == td->last_trb) { |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1899 | if (TRB_LEN(le32_to_cpu(event->transfer_len)) != 0) { |
Andiry Xu | 22405ed | 2010-07-22 15:23:08 -0700 | [diff] [blame] | 1900 | td->urb->actual_length = |
| 1901 | td->urb->transfer_buffer_length - |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1902 | TRB_LEN(le32_to_cpu(event->transfer_len)); |
Andiry Xu | 22405ed | 2010-07-22 15:23:08 -0700 | [diff] [blame] | 1903 | if (td->urb->transfer_buffer_length < |
| 1904 | td->urb->actual_length) { |
| 1905 | xhci_warn(xhci, "HC gave bad length " |
| 1906 | "of %d bytes left\n", |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1907 | TRB_LEN(le32_to_cpu(event->transfer_len))); |
Andiry Xu | 22405ed | 2010-07-22 15:23:08 -0700 | [diff] [blame] | 1908 | td->urb->actual_length = 0; |
| 1909 | if (td->urb->transfer_flags & URB_SHORT_NOT_OK) |
| 1910 | *status = -EREMOTEIO; |
| 1911 | else |
| 1912 | *status = 0; |
| 1913 | } |
| 1914 | /* Don't overwrite a previously set error code */ |
| 1915 | if (*status == -EINPROGRESS) { |
| 1916 | if (td->urb->transfer_flags & URB_SHORT_NOT_OK) |
| 1917 | *status = -EREMOTEIO; |
| 1918 | else |
| 1919 | *status = 0; |
| 1920 | } |
| 1921 | } else { |
| 1922 | td->urb->actual_length = |
| 1923 | td->urb->transfer_buffer_length; |
| 1924 | /* Ignore a short packet completion if the |
| 1925 | * untransferred length was zero. |
| 1926 | */ |
| 1927 | if (*status == -EREMOTEIO) |
| 1928 | *status = 0; |
| 1929 | } |
| 1930 | } else { |
| 1931 | /* Slow path - walk the list, starting from the dequeue |
| 1932 | * pointer, to get the actual length transferred. |
| 1933 | */ |
| 1934 | td->urb->actual_length = 0; |
| 1935 | for (cur_trb = ep_ring->dequeue, cur_seg = ep_ring->deq_seg; |
| 1936 | cur_trb != event_trb; |
| 1937 | next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) { |
Matt Evans | f5960b6 | 2011-06-01 10:22:55 +1000 | [diff] [blame] | 1938 | if (!TRB_TYPE_NOOP_LE32(cur_trb->generic.field[3]) && |
| 1939 | !TRB_TYPE_LINK_LE32(cur_trb->generic.field[3])) |
Andiry Xu | 22405ed | 2010-07-22 15:23:08 -0700 | [diff] [blame] | 1940 | td->urb->actual_length += |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1941 | TRB_LEN(le32_to_cpu(cur_trb->generic.field[2])); |
Andiry Xu | 22405ed | 2010-07-22 15:23:08 -0700 | [diff] [blame] | 1942 | } |
| 1943 | /* If the ring didn't stop on a Link or No-op TRB, add |
| 1944 | * in the actual bytes transferred from the Normal TRB |
| 1945 | */ |
| 1946 | if (trb_comp_code != COMP_STOP_INVAL) |
| 1947 | td->urb->actual_length += |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1948 | TRB_LEN(le32_to_cpu(cur_trb->generic.field[2])) - |
| 1949 | TRB_LEN(le32_to_cpu(event->transfer_len)); |
Andiry Xu | 22405ed | 2010-07-22 15:23:08 -0700 | [diff] [blame] | 1950 | } |
| 1951 | |
| 1952 | return finish_td(xhci, td, event_trb, event, ep, status, false); |
| 1953 | } |
| 1954 | |
| 1955 | /* |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 1956 | * If this function returns an error condition, it means it got a Transfer |
| 1957 | * event with a corrupted Slot ID, Endpoint ID, or TRB DMA address. |
| 1958 | * At this point, the host controller is probably hosed and should be reset. |
| 1959 | */ |
| 1960 | static int handle_tx_event(struct xhci_hcd *xhci, |
| 1961 | struct xhci_transfer_event *event) |
| 1962 | { |
| 1963 | struct xhci_virt_device *xdev; |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame] | 1964 | struct xhci_virt_ep *ep; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 1965 | struct xhci_ring *ep_ring; |
Sarah Sharp | 82d1009 | 2009-08-07 14:04:52 -0700 | [diff] [blame] | 1966 | unsigned int slot_id; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 1967 | int ep_index; |
Randy Dunlap | 326b481 | 2010-04-19 08:53:50 -0700 | [diff] [blame] | 1968 | struct xhci_td *td = NULL; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 1969 | dma_addr_t event_dma; |
| 1970 | struct xhci_segment *event_seg; |
| 1971 | union xhci_trb *event_trb; |
Randy Dunlap | 326b481 | 2010-04-19 08:53:50 -0700 | [diff] [blame] | 1972 | struct urb *urb = NULL; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 1973 | int status = -EINPROGRESS; |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 1974 | struct urb_priv *urb_priv; |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1975 | struct xhci_ep_ctx *ep_ctx; |
Andiry Xu | c2d7b49 | 2011-09-19 16:05:12 -0700 | [diff] [blame] | 1976 | struct list_head *tmp; |
Sarah Sharp | 66d1eeb | 2009-08-27 14:35:53 -0700 | [diff] [blame] | 1977 | u32 trb_comp_code; |
Andiry Xu | 4422da6 | 2010-07-22 15:22:55 -0700 | [diff] [blame] | 1978 | int ret = 0; |
Andiry Xu | c2d7b49 | 2011-09-19 16:05:12 -0700 | [diff] [blame] | 1979 | int td_num = 0; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 1980 | |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1981 | slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags)); |
Sarah Sharp | 82d1009 | 2009-08-07 14:04:52 -0700 | [diff] [blame] | 1982 | xdev = xhci->devs[slot_id]; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 1983 | if (!xdev) { |
| 1984 | xhci_err(xhci, "ERROR Transfer event pointed to bad slot\n"); |
Sarah Sharp | 9258c0b | 2011-12-01 14:50:30 -0800 | [diff] [blame] | 1985 | xhci_err(xhci, "@%016llx %08x %08x %08x %08x\n", |
Sarah Sharp | e910b44 | 2012-01-04 16:54:12 -0800 | [diff] [blame] | 1986 | (unsigned long long) xhci_trb_virt_to_dma( |
| 1987 | xhci->event_ring->deq_seg, |
Sarah Sharp | 9258c0b | 2011-12-01 14:50:30 -0800 | [diff] [blame] | 1988 | xhci->event_ring->dequeue), |
| 1989 | lower_32_bits(le64_to_cpu(event->buffer)), |
| 1990 | upper_32_bits(le64_to_cpu(event->buffer)), |
| 1991 | le32_to_cpu(event->transfer_len), |
| 1992 | le32_to_cpu(event->flags)); |
| 1993 | xhci_dbg(xhci, "Event ring:\n"); |
| 1994 | xhci_debug_segment(xhci, xhci->event_ring->deq_seg); |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 1995 | return -ENODEV; |
| 1996 | } |
| 1997 | |
| 1998 | /* Endpoint ID is 1 based, our index is zero based */ |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 1999 | ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1; |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame] | 2000 | ep = &xdev->eps[ep_index]; |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 2001 | ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer)); |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 2002 | ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index); |
Andiry Xu | 986a92d | 2010-07-22 15:23:20 -0700 | [diff] [blame] | 2003 | if (!ep_ring || |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 2004 | (le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK) == |
| 2005 | EP_STATE_DISABLED) { |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 2006 | xhci_err(xhci, "ERROR Transfer event for disabled endpoint " |
| 2007 | "or incorrect stream ring\n"); |
Sarah Sharp | 9258c0b | 2011-12-01 14:50:30 -0800 | [diff] [blame] | 2008 | xhci_err(xhci, "@%016llx %08x %08x %08x %08x\n", |
Sarah Sharp | e910b44 | 2012-01-04 16:54:12 -0800 | [diff] [blame] | 2009 | (unsigned long long) xhci_trb_virt_to_dma( |
| 2010 | xhci->event_ring->deq_seg, |
Sarah Sharp | 9258c0b | 2011-12-01 14:50:30 -0800 | [diff] [blame] | 2011 | xhci->event_ring->dequeue), |
| 2012 | lower_32_bits(le64_to_cpu(event->buffer)), |
| 2013 | upper_32_bits(le64_to_cpu(event->buffer)), |
| 2014 | le32_to_cpu(event->transfer_len), |
| 2015 | le32_to_cpu(event->flags)); |
| 2016 | xhci_dbg(xhci, "Event ring:\n"); |
| 2017 | xhci_debug_segment(xhci, xhci->event_ring->deq_seg); |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2018 | return -ENODEV; |
| 2019 | } |
| 2020 | |
Andiry Xu | c2d7b49 | 2011-09-19 16:05:12 -0700 | [diff] [blame] | 2021 | /* Count current td numbers if ep->skip is set */ |
| 2022 | if (ep->skip) { |
| 2023 | list_for_each(tmp, &ep_ring->td_list) |
| 2024 | td_num++; |
| 2025 | } |
| 2026 | |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 2027 | event_dma = le64_to_cpu(event->buffer); |
| 2028 | trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len)); |
Andiry Xu | 986a92d | 2010-07-22 15:23:20 -0700 | [diff] [blame] | 2029 | /* Look for common error cases */ |
Sarah Sharp | 66d1eeb | 2009-08-27 14:35:53 -0700 | [diff] [blame] | 2030 | switch (trb_comp_code) { |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 2031 | /* Skip codes that require special handling depending on |
| 2032 | * transfer type |
| 2033 | */ |
| 2034 | case COMP_SUCCESS: |
| 2035 | case COMP_SHORT_TX: |
| 2036 | break; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 2037 | case COMP_STOP: |
| 2038 | xhci_dbg(xhci, "Stopped on Transfer TRB\n"); |
| 2039 | break; |
| 2040 | case COMP_STOP_INVAL: |
| 2041 | xhci_dbg(xhci, "Stopped on No-op or Link TRB\n"); |
| 2042 | break; |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 2043 | case COMP_STALL: |
Sarah Sharp | 2a9227a | 2011-10-25 13:55:30 +0200 | [diff] [blame] | 2044 | xhci_dbg(xhci, "Stalled endpoint\n"); |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame] | 2045 | ep->ep_state |= EP_HALTED; |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 2046 | status = -EPIPE; |
| 2047 | break; |
| 2048 | case COMP_TRB_ERR: |
| 2049 | xhci_warn(xhci, "WARN: TRB error on endpoint\n"); |
| 2050 | status = -EILSEQ; |
| 2051 | break; |
Sarah Sharp | ec74e40 | 2009-11-11 10:28:36 -0800 | [diff] [blame] | 2052 | case COMP_SPLIT_ERR: |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 2053 | case COMP_TX_ERR: |
Sarah Sharp | 2a9227a | 2011-10-25 13:55:30 +0200 | [diff] [blame] | 2054 | xhci_dbg(xhci, "Transfer error on endpoint\n"); |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 2055 | status = -EPROTO; |
| 2056 | break; |
Sarah Sharp | 4a73143 | 2009-07-27 12:04:32 -0700 | [diff] [blame] | 2057 | case COMP_BABBLE: |
Sarah Sharp | 2a9227a | 2011-10-25 13:55:30 +0200 | [diff] [blame] | 2058 | xhci_dbg(xhci, "Babble error on endpoint\n"); |
Sarah Sharp | 4a73143 | 2009-07-27 12:04:32 -0700 | [diff] [blame] | 2059 | status = -EOVERFLOW; |
| 2060 | break; |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 2061 | case COMP_DB_ERR: |
| 2062 | xhci_warn(xhci, "WARN: HC couldn't access mem fast enough\n"); |
| 2063 | status = -ENOSR; |
| 2064 | break; |
Andiry Xu | 986a92d | 2010-07-22 15:23:20 -0700 | [diff] [blame] | 2065 | case COMP_BW_OVER: |
| 2066 | xhci_warn(xhci, "WARN: bandwidth overrun event on endpoint\n"); |
| 2067 | break; |
| 2068 | case COMP_BUFF_OVER: |
| 2069 | xhci_warn(xhci, "WARN: buffer overrun event on endpoint\n"); |
| 2070 | break; |
| 2071 | case COMP_UNDERRUN: |
| 2072 | /* |
| 2073 | * When the Isoch ring is empty, the xHC will generate |
| 2074 | * a Ring Overrun Event for IN Isoch endpoint or Ring |
| 2075 | * Underrun Event for OUT Isoch endpoint. |
| 2076 | */ |
| 2077 | xhci_dbg(xhci, "underrun event on endpoint\n"); |
| 2078 | if (!list_empty(&ep_ring->td_list)) |
| 2079 | xhci_dbg(xhci, "Underrun Event for slot %d ep %d " |
| 2080 | "still with TDs queued?\n", |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 2081 | TRB_TO_SLOT_ID(le32_to_cpu(event->flags)), |
| 2082 | ep_index); |
Andiry Xu | 986a92d | 2010-07-22 15:23:20 -0700 | [diff] [blame] | 2083 | goto cleanup; |
| 2084 | case COMP_OVERRUN: |
| 2085 | xhci_dbg(xhci, "overrun event on endpoint\n"); |
| 2086 | if (!list_empty(&ep_ring->td_list)) |
| 2087 | xhci_dbg(xhci, "Overrun Event for slot %d ep %d " |
| 2088 | "still with TDs queued?\n", |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 2089 | TRB_TO_SLOT_ID(le32_to_cpu(event->flags)), |
| 2090 | ep_index); |
Andiry Xu | 986a92d | 2010-07-22 15:23:20 -0700 | [diff] [blame] | 2091 | goto cleanup; |
Alex He | f6ba6fe | 2011-06-08 18:34:06 +0800 | [diff] [blame] | 2092 | case COMP_DEV_ERR: |
| 2093 | xhci_warn(xhci, "WARN: detect an incompatible device"); |
| 2094 | status = -EPROTO; |
| 2095 | break; |
Andiry Xu | d18240d | 2010-07-22 15:23:25 -0700 | [diff] [blame] | 2096 | case COMP_MISSED_INT: |
| 2097 | /* |
| 2098 | * When encounter missed service error, one or more isoc tds |
| 2099 | * may be missed by xHC. |
| 2100 | * Set skip flag of the ep_ring; Complete the missed tds as |
| 2101 | * short transfer when process the ep_ring next time. |
| 2102 | */ |
| 2103 | ep->skip = true; |
| 2104 | xhci_dbg(xhci, "Miss service interval error, set skip flag\n"); |
| 2105 | goto cleanup; |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 2106 | default: |
Sarah Sharp | b45b506 | 2009-12-09 15:59:06 -0800 | [diff] [blame] | 2107 | if (xhci_is_vendor_info_code(xhci, trb_comp_code)) { |
Sarah Sharp | 5ad6a52 | 2009-11-11 10:28:40 -0800 | [diff] [blame] | 2108 | status = 0; |
| 2109 | break; |
| 2110 | } |
Andiry Xu | 986a92d | 2010-07-22 15:23:20 -0700 | [diff] [blame] | 2111 | xhci_warn(xhci, "ERROR Unknown event condition, HC probably " |
| 2112 | "busted\n"); |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 2113 | goto cleanup; |
| 2114 | } |
Andiry Xu | 986a92d | 2010-07-22 15:23:20 -0700 | [diff] [blame] | 2115 | |
Andiry Xu | d18240d | 2010-07-22 15:23:25 -0700 | [diff] [blame] | 2116 | do { |
| 2117 | /* This TRB should be in the TD at the head of this ring's |
| 2118 | * TD list. |
| 2119 | */ |
| 2120 | if (list_empty(&ep_ring->td_list)) { |
| 2121 | xhci_warn(xhci, "WARN Event TRB for slot %d ep %d " |
| 2122 | "with no TDs queued?\n", |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 2123 | TRB_TO_SLOT_ID(le32_to_cpu(event->flags)), |
| 2124 | ep_index); |
Andiry Xu | d18240d | 2010-07-22 15:23:25 -0700 | [diff] [blame] | 2125 | xhci_dbg(xhci, "Event TRB with TRB type ID %u\n", |
Matt Evans | f5960b6 | 2011-06-01 10:22:55 +1000 | [diff] [blame] | 2126 | (le32_to_cpu(event->flags) & |
| 2127 | TRB_TYPE_BITMASK)>>10); |
Andiry Xu | d18240d | 2010-07-22 15:23:25 -0700 | [diff] [blame] | 2128 | xhci_print_trb_offsets(xhci, (union xhci_trb *) event); |
| 2129 | if (ep->skip) { |
| 2130 | ep->skip = false; |
| 2131 | xhci_dbg(xhci, "td_list is empty while skip " |
| 2132 | "flag set. Clear skip flag.\n"); |
| 2133 | } |
| 2134 | ret = 0; |
| 2135 | goto cleanup; |
| 2136 | } |
Andiry Xu | 986a92d | 2010-07-22 15:23:20 -0700 | [diff] [blame] | 2137 | |
Andiry Xu | c2d7b49 | 2011-09-19 16:05:12 -0700 | [diff] [blame] | 2138 | /* We've skipped all the TDs on the ep ring when ep->skip set */ |
| 2139 | if (ep->skip && td_num == 0) { |
| 2140 | ep->skip = false; |
| 2141 | xhci_dbg(xhci, "All tds on the ep_ring skipped. " |
| 2142 | "Clear skip flag.\n"); |
| 2143 | ret = 0; |
| 2144 | goto cleanup; |
| 2145 | } |
| 2146 | |
Andiry Xu | d18240d | 2010-07-22 15:23:25 -0700 | [diff] [blame] | 2147 | td = list_entry(ep_ring->td_list.next, struct xhci_td, td_list); |
Andiry Xu | c2d7b49 | 2011-09-19 16:05:12 -0700 | [diff] [blame] | 2148 | if (ep->skip) |
| 2149 | td_num--; |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2150 | |
Andiry Xu | d18240d | 2010-07-22 15:23:25 -0700 | [diff] [blame] | 2151 | /* Is this a TRB in the currently executing TD? */ |
| 2152 | event_seg = trb_in_td(ep_ring->deq_seg, ep_ring->dequeue, |
| 2153 | td->last_trb, event_dma); |
Alex He | e1cf486 | 2011-06-03 15:58:25 +0800 | [diff] [blame] | 2154 | |
| 2155 | /* |
| 2156 | * Skip the Force Stopped Event. The event_trb(event_dma) of FSE |
| 2157 | * is not in the current TD pointed by ep_ring->dequeue because |
| 2158 | * that the hardware dequeue pointer still at the previous TRB |
| 2159 | * of the current TD. The previous TRB maybe a Link TD or the |
| 2160 | * last TRB of the previous TD. The command completion handle |
| 2161 | * will take care the rest. |
| 2162 | */ |
| 2163 | if (!event_seg && trb_comp_code == COMP_STOP_INVAL) { |
| 2164 | ret = 0; |
| 2165 | goto cleanup; |
| 2166 | } |
| 2167 | |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2168 | if (!event_seg) { |
| 2169 | if (!ep->skip || |
| 2170 | !usb_endpoint_xfer_isoc(&td->urb->ep->desc)) { |
Sarah Sharp | ad80833 | 2011-05-25 10:43:56 -0700 | [diff] [blame] | 2171 | /* Some host controllers give a spurious |
| 2172 | * successful event after a short transfer. |
| 2173 | * Ignore it. |
| 2174 | */ |
| 2175 | if ((xhci->quirks & XHCI_SPURIOUS_SUCCESS) && |
| 2176 | ep_ring->last_td_was_short) { |
| 2177 | ep_ring->last_td_was_short = false; |
| 2178 | ret = 0; |
| 2179 | goto cleanup; |
| 2180 | } |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2181 | /* HC is busted, give up! */ |
| 2182 | xhci_err(xhci, |
| 2183 | "ERROR Transfer event TRB DMA ptr not " |
| 2184 | "part of current TD\n"); |
| 2185 | return -ESHUTDOWN; |
| 2186 | } |
| 2187 | |
| 2188 | ret = skip_isoc_td(xhci, td, event, ep, &status); |
| 2189 | goto cleanup; |
| 2190 | } |
Sarah Sharp | ad80833 | 2011-05-25 10:43:56 -0700 | [diff] [blame] | 2191 | if (trb_comp_code == COMP_SHORT_TX) |
| 2192 | ep_ring->last_td_was_short = true; |
| 2193 | else |
| 2194 | ep_ring->last_td_was_short = false; |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2195 | |
| 2196 | if (ep->skip) { |
Andiry Xu | d18240d | 2010-07-22 15:23:25 -0700 | [diff] [blame] | 2197 | xhci_dbg(xhci, "Found td. Clear skip flag.\n"); |
| 2198 | ep->skip = false; |
| 2199 | } |
Andiry Xu | 986a92d | 2010-07-22 15:23:20 -0700 | [diff] [blame] | 2200 | |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2201 | event_trb = &event_seg->trbs[(event_dma - event_seg->dma) / |
| 2202 | sizeof(*event_trb)]; |
| 2203 | /* |
| 2204 | * No-op TRB should not trigger interrupts. |
| 2205 | * If event_trb is a no-op TRB, it means the |
| 2206 | * corresponding TD has been cancelled. Just ignore |
| 2207 | * the TD. |
| 2208 | */ |
Matt Evans | f5960b6 | 2011-06-01 10:22:55 +1000 | [diff] [blame] | 2209 | if (TRB_TYPE_NOOP_LE32(event_trb->generic.field[3])) { |
Dmitry Torokhov | 926008c | 2011-03-23 20:47:05 -0700 | [diff] [blame] | 2210 | xhci_dbg(xhci, |
| 2211 | "event_trb is a no-op TRB. Skip it\n"); |
| 2212 | goto cleanup; |
Andiry Xu | d18240d | 2010-07-22 15:23:25 -0700 | [diff] [blame] | 2213 | } |
| 2214 | |
| 2215 | /* Now update the urb's actual_length and give back to |
| 2216 | * the core |
| 2217 | */ |
| 2218 | if (usb_endpoint_xfer_control(&td->urb->ep->desc)) |
| 2219 | ret = process_ctrl_td(xhci, td, event_trb, event, ep, |
| 2220 | &status); |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 2221 | else if (usb_endpoint_xfer_isoc(&td->urb->ep->desc)) |
| 2222 | ret = process_isoc_td(xhci, td, event_trb, event, ep, |
| 2223 | &status); |
Andiry Xu | d18240d | 2010-07-22 15:23:25 -0700 | [diff] [blame] | 2224 | else |
| 2225 | ret = process_bulk_intr_td(xhci, td, event_trb, event, |
| 2226 | ep, &status); |
Andiry Xu | 4422da6 | 2010-07-22 15:22:55 -0700 | [diff] [blame] | 2227 | |
| 2228 | cleanup: |
Andiry Xu | d18240d | 2010-07-22 15:23:25 -0700 | [diff] [blame] | 2229 | /* |
| 2230 | * Do not update event ring dequeue pointer if ep->skip is set. |
| 2231 | * Will roll back to continue process missed tds. |
Sarah Sharp | 82d1009 | 2009-08-07 14:04:52 -0700 | [diff] [blame] | 2232 | */ |
Andiry Xu | d18240d | 2010-07-22 15:23:25 -0700 | [diff] [blame] | 2233 | if (trb_comp_code == COMP_MISSED_INT || !ep->skip) { |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame^] | 2234 | inc_deq(xhci, xhci->event_ring); |
Andiry Xu | d18240d | 2010-07-22 15:23:25 -0700 | [diff] [blame] | 2235 | } |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2236 | |
Andiry Xu | d18240d | 2010-07-22 15:23:25 -0700 | [diff] [blame] | 2237 | if (ret) { |
| 2238 | urb = td->urb; |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 2239 | urb_priv = urb->hcpriv; |
Andiry Xu | d18240d | 2010-07-22 15:23:25 -0700 | [diff] [blame] | 2240 | /* Leave the TD around for the reset endpoint function |
| 2241 | * to use(but only if it's not a control endpoint, |
| 2242 | * since we already queued the Set TR dequeue pointer |
| 2243 | * command for stalled control endpoints). |
| 2244 | */ |
| 2245 | if (usb_endpoint_xfer_control(&urb->ep->desc) || |
| 2246 | (trb_comp_code != COMP_STALL && |
| 2247 | trb_comp_code != COMP_BABBLE)) |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 2248 | xhci_urb_free_priv(xhci, urb_priv); |
Andiry Xu | d18240d | 2010-07-22 15:23:25 -0700 | [diff] [blame] | 2249 | |
Sarah Sharp | 214f76f | 2010-10-26 11:22:02 -0700 | [diff] [blame] | 2250 | usb_hcd_unlink_urb_from_ep(bus_to_hcd(urb->dev->bus), urb); |
Sarah Sharp | f444ff2 | 2011-04-05 15:53:47 -0700 | [diff] [blame] | 2251 | if ((urb->actual_length != urb->transfer_buffer_length && |
| 2252 | (urb->transfer_flags & |
| 2253 | URB_SHORT_NOT_OK)) || |
Sarah Sharp | fd984d2 | 2011-09-02 11:05:56 -0700 | [diff] [blame] | 2254 | (status != 0 && |
| 2255 | !usb_endpoint_xfer_isoc(&urb->ep->desc))) |
Sarah Sharp | f444ff2 | 2011-04-05 15:53:47 -0700 | [diff] [blame] | 2256 | xhci_dbg(xhci, "Giveback URB %p, len = %d, " |
| 2257 | "expected = %x, status = %d\n", |
| 2258 | urb, urb->actual_length, |
| 2259 | urb->transfer_buffer_length, |
| 2260 | status); |
Andiry Xu | d18240d | 2010-07-22 15:23:25 -0700 | [diff] [blame] | 2261 | spin_unlock(&xhci->lock); |
Sarah Sharp | b3df3f9 | 2011-06-15 19:57:46 -0700 | [diff] [blame] | 2262 | /* EHCI, UHCI, and OHCI always unconditionally set the |
| 2263 | * urb->status of an isochronous endpoint to 0. |
| 2264 | */ |
| 2265 | if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) |
| 2266 | status = 0; |
Sarah Sharp | 214f76f | 2010-10-26 11:22:02 -0700 | [diff] [blame] | 2267 | usb_hcd_giveback_urb(bus_to_hcd(urb->dev->bus), urb, status); |
Andiry Xu | d18240d | 2010-07-22 15:23:25 -0700 | [diff] [blame] | 2268 | spin_lock(&xhci->lock); |
| 2269 | } |
| 2270 | |
| 2271 | /* |
| 2272 | * If ep->skip is set, it means there are missed tds on the |
| 2273 | * endpoint ring need to take care of. |
| 2274 | * Process them as short transfer until reach the td pointed by |
| 2275 | * the event. |
| 2276 | */ |
| 2277 | } while (ep->skip && trb_comp_code != COMP_MISSED_INT); |
| 2278 | |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2279 | return 0; |
| 2280 | } |
| 2281 | |
| 2282 | /* |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 2283 | * This function handles all OS-owned events on the event ring. It may drop |
| 2284 | * xhci->lock between event processing (e.g. to pass up port status changes). |
Matt Evans | 9dee9a2 | 2011-03-29 13:41:02 +1100 | [diff] [blame] | 2285 | * Returns >0 for "possibly more events to process" (caller should call again), |
| 2286 | * otherwise 0 if done. In future, <0 returns should indicate error code. |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 2287 | */ |
Matt Evans | 9dee9a2 | 2011-03-29 13:41:02 +1100 | [diff] [blame] | 2288 | static int xhci_handle_event(struct xhci_hcd *xhci) |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 2289 | { |
| 2290 | union xhci_trb *event; |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 2291 | int update_ptrs = 1; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2292 | int ret; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 2293 | |
| 2294 | if (!xhci->event_ring || !xhci->event_ring->dequeue) { |
| 2295 | xhci->error_bitmask |= 1 << 1; |
Matt Evans | 9dee9a2 | 2011-03-29 13:41:02 +1100 | [diff] [blame] | 2296 | return 0; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 2297 | } |
| 2298 | |
| 2299 | event = xhci->event_ring->dequeue; |
| 2300 | /* Does the HC or OS own the TRB? */ |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 2301 | if ((le32_to_cpu(event->event_cmd.flags) & TRB_CYCLE) != |
| 2302 | xhci->event_ring->cycle_state) { |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 2303 | xhci->error_bitmask |= 1 << 2; |
Matt Evans | 9dee9a2 | 2011-03-29 13:41:02 +1100 | [diff] [blame] | 2304 | return 0; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 2305 | } |
| 2306 | |
Matt Evans | 92a3da4 | 2011-03-29 13:40:51 +1100 | [diff] [blame] | 2307 | /* |
| 2308 | * Barrier between reading the TRB_CYCLE (valid) flag above and any |
| 2309 | * speculative reads of the event's flags/data below. |
| 2310 | */ |
| 2311 | rmb(); |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 2312 | /* FIXME: Handle more event types. */ |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 2313 | switch ((le32_to_cpu(event->event_cmd.flags) & TRB_TYPE_BITMASK)) { |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 2314 | case TRB_TYPE(TRB_COMPLETION): |
| 2315 | handle_cmd_completion(xhci, &event->event_cmd); |
| 2316 | break; |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 2317 | case TRB_TYPE(TRB_PORT_STATUS): |
| 2318 | handle_port_status(xhci, event); |
| 2319 | update_ptrs = 0; |
| 2320 | break; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2321 | case TRB_TYPE(TRB_TRANSFER): |
| 2322 | ret = handle_tx_event(xhci, &event->trans_event); |
| 2323 | if (ret < 0) |
| 2324 | xhci->error_bitmask |= 1 << 9; |
| 2325 | else |
| 2326 | update_ptrs = 0; |
| 2327 | break; |
Sarah Sharp | 623bef9 | 2011-11-11 14:57:33 -0800 | [diff] [blame] | 2328 | case TRB_TYPE(TRB_DEV_NOTE): |
| 2329 | handle_device_notification(xhci, event); |
| 2330 | break; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 2331 | default: |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 2332 | if ((le32_to_cpu(event->event_cmd.flags) & TRB_TYPE_BITMASK) >= |
| 2333 | TRB_TYPE(48)) |
Sarah Sharp | 0238634 | 2010-05-24 13:25:28 -0700 | [diff] [blame] | 2334 | handle_vendor_event(xhci, event); |
| 2335 | else |
| 2336 | xhci->error_bitmask |= 1 << 3; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 2337 | } |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 2338 | /* Any of the above functions may drop and re-acquire the lock, so check |
| 2339 | * to make sure a watchdog timer didn't mark the host as non-responsive. |
| 2340 | */ |
| 2341 | if (xhci->xhc_state & XHCI_STATE_DYING) { |
| 2342 | xhci_dbg(xhci, "xHCI host dying, returning from " |
| 2343 | "event handler.\n"); |
Matt Evans | 9dee9a2 | 2011-03-29 13:41:02 +1100 | [diff] [blame] | 2344 | return 0; |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 2345 | } |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 2346 | |
Sarah Sharp | c06d68b | 2010-07-29 22:12:49 -0700 | [diff] [blame] | 2347 | if (update_ptrs) |
| 2348 | /* Update SW event ring dequeue pointer */ |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame^] | 2349 | inc_deq(xhci, xhci->event_ring); |
Sarah Sharp | c06d68b | 2010-07-29 22:12:49 -0700 | [diff] [blame] | 2350 | |
Matt Evans | 9dee9a2 | 2011-03-29 13:41:02 +1100 | [diff] [blame] | 2351 | /* Are there more items on the event ring? Caller will call us again to |
| 2352 | * check. |
| 2353 | */ |
| 2354 | return 1; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 2355 | } |
Sarah Sharp | 9032cd5 | 2010-07-29 22:12:29 -0700 | [diff] [blame] | 2356 | |
| 2357 | /* |
| 2358 | * xHCI spec says we can get an interrupt, and if the HC has an error condition, |
| 2359 | * we might get bad data out of the event ring. Section 4.10.2.7 has a list of |
| 2360 | * indicators of an event TRB error, but we check the status *first* to be safe. |
| 2361 | */ |
| 2362 | irqreturn_t xhci_irq(struct usb_hcd *hcd) |
| 2363 | { |
| 2364 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
Sarah Sharp | c21599a | 2010-07-29 22:13:00 -0700 | [diff] [blame] | 2365 | u32 status; |
Sarah Sharp | 9032cd5 | 2010-07-29 22:12:29 -0700 | [diff] [blame] | 2366 | union xhci_trb *trb; |
Sarah Sharp | bda5314 | 2010-07-29 22:12:38 -0700 | [diff] [blame] | 2367 | u64 temp_64; |
Sarah Sharp | c06d68b | 2010-07-29 22:12:49 -0700 | [diff] [blame] | 2368 | union xhci_trb *event_ring_deq; |
| 2369 | dma_addr_t deq; |
Sarah Sharp | 9032cd5 | 2010-07-29 22:12:29 -0700 | [diff] [blame] | 2370 | |
| 2371 | spin_lock(&xhci->lock); |
| 2372 | trb = xhci->event_ring->dequeue; |
| 2373 | /* Check if the xHC generated the interrupt, or the irq is shared */ |
Sarah Sharp | 27e0dd4 | 2010-07-29 22:12:43 -0700 | [diff] [blame] | 2374 | status = xhci_readl(xhci, &xhci->op_regs->status); |
Sarah Sharp | c21599a | 2010-07-29 22:13:00 -0700 | [diff] [blame] | 2375 | if (status == 0xffffffff) |
Sarah Sharp | 9032cd5 | 2010-07-29 22:12:29 -0700 | [diff] [blame] | 2376 | goto hw_died; |
| 2377 | |
Sarah Sharp | c21599a | 2010-07-29 22:13:00 -0700 | [diff] [blame] | 2378 | if (!(status & STS_EINT)) { |
Sarah Sharp | 9032cd5 | 2010-07-29 22:12:29 -0700 | [diff] [blame] | 2379 | spin_unlock(&xhci->lock); |
Sarah Sharp | 9032cd5 | 2010-07-29 22:12:29 -0700 | [diff] [blame] | 2380 | return IRQ_NONE; |
| 2381 | } |
Sarah Sharp | 27e0dd4 | 2010-07-29 22:12:43 -0700 | [diff] [blame] | 2382 | if (status & STS_FATAL) { |
Sarah Sharp | 9032cd5 | 2010-07-29 22:12:29 -0700 | [diff] [blame] | 2383 | xhci_warn(xhci, "WARNING: Host System Error\n"); |
| 2384 | xhci_halt(xhci); |
| 2385 | hw_died: |
Sarah Sharp | 9032cd5 | 2010-07-29 22:12:29 -0700 | [diff] [blame] | 2386 | spin_unlock(&xhci->lock); |
| 2387 | return -ESHUTDOWN; |
| 2388 | } |
| 2389 | |
Sarah Sharp | bda5314 | 2010-07-29 22:12:38 -0700 | [diff] [blame] | 2390 | /* |
| 2391 | * Clear the op reg interrupt status first, |
| 2392 | * so we can receive interrupts from other MSI-X interrupters. |
| 2393 | * Write 1 to clear the interrupt status. |
| 2394 | */ |
Sarah Sharp | 27e0dd4 | 2010-07-29 22:12:43 -0700 | [diff] [blame] | 2395 | status |= STS_EINT; |
| 2396 | xhci_writel(xhci, status, &xhci->op_regs->status); |
Sarah Sharp | bda5314 | 2010-07-29 22:12:38 -0700 | [diff] [blame] | 2397 | /* FIXME when MSI-X is supported and there are multiple vectors */ |
| 2398 | /* Clear the MSI-X event interrupt status */ |
| 2399 | |
Felipe Balbi | cd70469 | 2012-02-29 16:46:23 +0200 | [diff] [blame] | 2400 | if (hcd->irq) { |
Sarah Sharp | c21599a | 2010-07-29 22:13:00 -0700 | [diff] [blame] | 2401 | u32 irq_pending; |
| 2402 | /* Acknowledge the PCI interrupt */ |
| 2403 | irq_pending = xhci_readl(xhci, &xhci->ir_set->irq_pending); |
| 2404 | irq_pending |= 0x3; |
| 2405 | xhci_writel(xhci, irq_pending, &xhci->ir_set->irq_pending); |
| 2406 | } |
Sarah Sharp | bda5314 | 2010-07-29 22:12:38 -0700 | [diff] [blame] | 2407 | |
Sarah Sharp | c06d68b | 2010-07-29 22:12:49 -0700 | [diff] [blame] | 2408 | if (xhci->xhc_state & XHCI_STATE_DYING) { |
Sarah Sharp | bda5314 | 2010-07-29 22:12:38 -0700 | [diff] [blame] | 2409 | xhci_dbg(xhci, "xHCI dying, ignoring interrupt. " |
| 2410 | "Shouldn't IRQs be disabled?\n"); |
Sarah Sharp | c06d68b | 2010-07-29 22:12:49 -0700 | [diff] [blame] | 2411 | /* Clear the event handler busy flag (RW1C); |
| 2412 | * the event ring should be empty. |
Sarah Sharp | bda5314 | 2010-07-29 22:12:38 -0700 | [diff] [blame] | 2413 | */ |
Sarah Sharp | c06d68b | 2010-07-29 22:12:49 -0700 | [diff] [blame] | 2414 | temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue); |
| 2415 | xhci_write_64(xhci, temp_64 | ERST_EHB, |
| 2416 | &xhci->ir_set->erst_dequeue); |
| 2417 | spin_unlock(&xhci->lock); |
| 2418 | |
| 2419 | return IRQ_HANDLED; |
| 2420 | } |
| 2421 | |
| 2422 | event_ring_deq = xhci->event_ring->dequeue; |
| 2423 | /* FIXME this should be a delayed service routine |
| 2424 | * that clears the EHB. |
| 2425 | */ |
Matt Evans | 9dee9a2 | 2011-03-29 13:41:02 +1100 | [diff] [blame] | 2426 | while (xhci_handle_event(xhci) > 0) {} |
Sarah Sharp | c06d68b | 2010-07-29 22:12:49 -0700 | [diff] [blame] | 2427 | |
| 2428 | temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue); |
| 2429 | /* If necessary, update the HW's version of the event ring deq ptr. */ |
| 2430 | if (event_ring_deq != xhci->event_ring->dequeue) { |
| 2431 | deq = xhci_trb_virt_to_dma(xhci->event_ring->deq_seg, |
| 2432 | xhci->event_ring->dequeue); |
| 2433 | if (deq == 0) |
| 2434 | xhci_warn(xhci, "WARN something wrong with SW event " |
| 2435 | "ring dequeue ptr.\n"); |
| 2436 | /* Update HC event ring dequeue pointer */ |
| 2437 | temp_64 &= ERST_PTR_MASK; |
| 2438 | temp_64 |= ((u64) deq & (u64) ~ERST_PTR_MASK); |
| 2439 | } |
Sarah Sharp | bda5314 | 2010-07-29 22:12:38 -0700 | [diff] [blame] | 2440 | |
| 2441 | /* Clear the event handler busy flag (RW1C); event ring is empty. */ |
Sarah Sharp | c06d68b | 2010-07-29 22:12:49 -0700 | [diff] [blame] | 2442 | temp_64 |= ERST_EHB; |
| 2443 | xhci_write_64(xhci, temp_64, &xhci->ir_set->erst_dequeue); |
| 2444 | |
Sarah Sharp | 9032cd5 | 2010-07-29 22:12:29 -0700 | [diff] [blame] | 2445 | spin_unlock(&xhci->lock); |
| 2446 | |
| 2447 | return IRQ_HANDLED; |
| 2448 | } |
| 2449 | |
| 2450 | irqreturn_t xhci_msi_irq(int irq, struct usb_hcd *hcd) |
| 2451 | { |
Alan Stern | 968b822 | 2011-11-03 12:03:38 -0400 | [diff] [blame] | 2452 | return xhci_irq(hcd); |
Sarah Sharp | 9032cd5 | 2010-07-29 22:12:29 -0700 | [diff] [blame] | 2453 | } |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 2454 | |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2455 | /**** Endpoint Ring Operations ****/ |
| 2456 | |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 2457 | /* |
| 2458 | * Generic function for queueing a TRB on a ring. |
| 2459 | * The caller must have checked to make sure there's room on the ring. |
Sarah Sharp | 6cc30d8 | 2010-06-10 12:25:28 -0700 | [diff] [blame] | 2460 | * |
| 2461 | * @more_trbs_coming: Will you enqueue more TRBs before calling |
| 2462 | * prepare_transfer()? |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 2463 | */ |
| 2464 | static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring, |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame^] | 2465 | bool more_trbs_coming, |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 2466 | u32 field1, u32 field2, u32 field3, u32 field4) |
| 2467 | { |
| 2468 | struct xhci_generic_trb *trb; |
| 2469 | |
| 2470 | trb = &ring->enqueue->generic; |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 2471 | trb->field[0] = cpu_to_le32(field1); |
| 2472 | trb->field[1] = cpu_to_le32(field2); |
| 2473 | trb->field[2] = cpu_to_le32(field3); |
| 2474 | trb->field[3] = cpu_to_le32(field4); |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame^] | 2475 | inc_enq(xhci, ring, more_trbs_coming); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 2476 | } |
| 2477 | |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2478 | /* |
| 2479 | * Does various checks on the endpoint ring, and makes it ready to queue num_trbs. |
| 2480 | * FIXME allocate segments if the ring is full. |
| 2481 | */ |
| 2482 | static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring, |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame^] | 2483 | u32 ep_state, unsigned int num_trbs, gfp_t mem_flags) |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2484 | { |
| 2485 | /* Make sure the endpoint has been added to xHC schedule */ |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2486 | switch (ep_state) { |
| 2487 | case EP_STATE_DISABLED: |
| 2488 | /* |
| 2489 | * USB core changed config/interfaces without notifying us, |
| 2490 | * or hardware is reporting the wrong state. |
| 2491 | */ |
| 2492 | xhci_warn(xhci, "WARN urb submitted to disabled ep\n"); |
| 2493 | return -ENOENT; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2494 | case EP_STATE_ERROR: |
Sarah Sharp | c92bcfa | 2009-07-27 12:05:21 -0700 | [diff] [blame] | 2495 | xhci_warn(xhci, "WARN waiting for error on ep to be cleared\n"); |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2496 | /* FIXME event handling code for error needs to clear it */ |
| 2497 | /* XXX not sure if this should be -ENOENT or not */ |
| 2498 | return -EINVAL; |
Sarah Sharp | c92bcfa | 2009-07-27 12:05:21 -0700 | [diff] [blame] | 2499 | case EP_STATE_HALTED: |
| 2500 | xhci_dbg(xhci, "WARN halted endpoint, queueing URB anyway.\n"); |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2501 | case EP_STATE_STOPPED: |
| 2502 | case EP_STATE_RUNNING: |
| 2503 | break; |
| 2504 | default: |
| 2505 | xhci_err(xhci, "ERROR unknown endpoint state for ep\n"); |
| 2506 | /* |
| 2507 | * FIXME issue Configure Endpoint command to try to get the HC |
| 2508 | * back into a known state. |
| 2509 | */ |
| 2510 | return -EINVAL; |
| 2511 | } |
| 2512 | if (!room_on_ring(xhci, ep_ring, num_trbs)) { |
| 2513 | /* FIXME allocate more room */ |
| 2514 | xhci_err(xhci, "ERROR no room on ep ring\n"); |
| 2515 | return -ENOMEM; |
| 2516 | } |
John Youn | 6c12db9 | 2010-05-10 15:33:00 -0700 | [diff] [blame] | 2517 | |
| 2518 | if (enqueue_is_link_trb(ep_ring)) { |
| 2519 | struct xhci_ring *ring = ep_ring; |
| 2520 | union xhci_trb *next; |
John Youn | 6c12db9 | 2010-05-10 15:33:00 -0700 | [diff] [blame] | 2521 | |
John Youn | 6c12db9 | 2010-05-10 15:33:00 -0700 | [diff] [blame] | 2522 | next = ring->enqueue; |
| 2523 | |
| 2524 | while (last_trb(xhci, ring, ring->enq_seg, next)) { |
Andiry Xu | 7e393a8 | 2011-09-23 14:19:54 -0700 | [diff] [blame] | 2525 | /* If we're not dealing with 0.95 hardware or isoc rings |
| 2526 | * on AMD 0.96 host, clear the chain bit. |
John Youn | 6c12db9 | 2010-05-10 15:33:00 -0700 | [diff] [blame] | 2527 | */ |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame^] | 2528 | if (!xhci_link_trb_quirk(xhci) && |
| 2529 | !(ring->type == TYPE_ISOC && |
| 2530 | (xhci->quirks & XHCI_AMD_0x96_HOST))) |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 2531 | next->link.control &= cpu_to_le32(~TRB_CHAIN); |
John Youn | 6c12db9 | 2010-05-10 15:33:00 -0700 | [diff] [blame] | 2532 | else |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 2533 | next->link.control |= cpu_to_le32(TRB_CHAIN); |
John Youn | 6c12db9 | 2010-05-10 15:33:00 -0700 | [diff] [blame] | 2534 | |
| 2535 | wmb(); |
Matt Evans | f5960b6 | 2011-06-01 10:22:55 +1000 | [diff] [blame] | 2536 | next->link.control ^= cpu_to_le32(TRB_CYCLE); |
John Youn | 6c12db9 | 2010-05-10 15:33:00 -0700 | [diff] [blame] | 2537 | |
| 2538 | /* Toggle the cycle bit after the last ring segment. */ |
| 2539 | if (last_trb_on_last_seg(xhci, ring, ring->enq_seg, next)) { |
| 2540 | ring->cycle_state = (ring->cycle_state ? 0 : 1); |
John Youn | 6c12db9 | 2010-05-10 15:33:00 -0700 | [diff] [blame] | 2541 | } |
| 2542 | ring->enq_seg = ring->enq_seg->next; |
| 2543 | ring->enqueue = ring->enq_seg->trbs; |
| 2544 | next = ring->enqueue; |
| 2545 | } |
| 2546 | } |
| 2547 | |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2548 | return 0; |
| 2549 | } |
| 2550 | |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 2551 | static int prepare_transfer(struct xhci_hcd *xhci, |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2552 | struct xhci_virt_device *xdev, |
| 2553 | unsigned int ep_index, |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 2554 | unsigned int stream_id, |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2555 | unsigned int num_trbs, |
| 2556 | struct urb *urb, |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 2557 | unsigned int td_index, |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2558 | gfp_t mem_flags) |
| 2559 | { |
| 2560 | int ret; |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 2561 | struct urb_priv *urb_priv; |
| 2562 | struct xhci_td *td; |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 2563 | struct xhci_ring *ep_ring; |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 2564 | struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index); |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 2565 | |
| 2566 | ep_ring = xhci_stream_id_to_ring(xdev, ep_index, stream_id); |
| 2567 | if (!ep_ring) { |
| 2568 | xhci_dbg(xhci, "Can't prepare ring for bad stream ID %u\n", |
| 2569 | stream_id); |
| 2570 | return -EINVAL; |
| 2571 | } |
| 2572 | |
| 2573 | ret = prepare_ring(xhci, ep_ring, |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 2574 | le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK, |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame^] | 2575 | num_trbs, mem_flags); |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2576 | if (ret) |
| 2577 | return ret; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2578 | |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 2579 | urb_priv = urb->hcpriv; |
| 2580 | td = urb_priv->td[td_index]; |
| 2581 | |
| 2582 | INIT_LIST_HEAD(&td->td_list); |
| 2583 | INIT_LIST_HEAD(&td->cancelled_td_list); |
| 2584 | |
| 2585 | if (td_index == 0) { |
Sarah Sharp | 214f76f | 2010-10-26 11:22:02 -0700 | [diff] [blame] | 2586 | ret = usb_hcd_link_urb_to_ep(bus_to_hcd(urb->dev->bus), urb); |
Sarah Sharp | d13565c | 2011-07-22 14:34:34 -0700 | [diff] [blame] | 2587 | if (unlikely(ret)) |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 2588 | return ret; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2589 | } |
| 2590 | |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 2591 | td->urb = urb; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2592 | /* Add this TD to the tail of the endpoint ring's TD list */ |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 2593 | list_add_tail(&td->td_list, &ep_ring->td_list); |
| 2594 | td->start_seg = ep_ring->enq_seg; |
| 2595 | td->first_trb = ep_ring->enqueue; |
| 2596 | |
| 2597 | urb_priv->td[td_index] = td; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 2598 | |
| 2599 | return 0; |
| 2600 | } |
| 2601 | |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 2602 | static unsigned int count_sg_trbs_needed(struct xhci_hcd *xhci, struct urb *urb) |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2603 | { |
| 2604 | int num_sgs, num_trbs, running_total, temp, i; |
| 2605 | struct scatterlist *sg; |
| 2606 | |
| 2607 | sg = NULL; |
Clemens Ladisch | bc677d5 | 2011-12-03 23:41:31 +0100 | [diff] [blame] | 2608 | num_sgs = urb->num_mapped_sgs; |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2609 | temp = urb->transfer_buffer_length; |
| 2610 | |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2611 | num_trbs = 0; |
Matthew Wilcox | 910f8d0 | 2010-05-01 12:20:01 -0600 | [diff] [blame] | 2612 | for_each_sg(urb->sg, sg, num_sgs, i) { |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2613 | unsigned int len = sg_dma_len(sg); |
| 2614 | |
| 2615 | /* Scatter gather list entries may cross 64KB boundaries */ |
| 2616 | running_total = TRB_MAX_BUFF_SIZE - |
Paul Zimmerman | a249018 | 2011-02-12 14:06:44 -0800 | [diff] [blame] | 2617 | (sg_dma_address(sg) & (TRB_MAX_BUFF_SIZE - 1)); |
Paul Zimmerman | 5807795 | 2011-02-12 14:07:20 -0800 | [diff] [blame] | 2618 | running_total &= TRB_MAX_BUFF_SIZE - 1; |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2619 | if (running_total != 0) |
| 2620 | num_trbs++; |
| 2621 | |
| 2622 | /* How many more 64KB chunks to transfer, how many more TRBs? */ |
Paul Zimmerman | bcd2fde | 2011-02-12 14:07:57 -0800 | [diff] [blame] | 2623 | while (running_total < sg_dma_len(sg) && running_total < temp) { |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2624 | num_trbs++; |
| 2625 | running_total += TRB_MAX_BUFF_SIZE; |
| 2626 | } |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2627 | len = min_t(int, len, temp); |
| 2628 | temp -= len; |
| 2629 | if (temp == 0) |
| 2630 | break; |
| 2631 | } |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2632 | return num_trbs; |
| 2633 | } |
| 2634 | |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 2635 | static void check_trb_math(struct urb *urb, int num_trbs, int running_total) |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2636 | { |
| 2637 | if (num_trbs != 0) |
Paul Zimmerman | a249018 | 2011-02-12 14:06:44 -0800 | [diff] [blame] | 2638 | dev_err(&urb->dev->dev, "%s - ep %#x - Miscalculated number of " |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2639 | "TRBs, %d left\n", __func__, |
| 2640 | urb->ep->desc.bEndpointAddress, num_trbs); |
| 2641 | if (running_total != urb->transfer_buffer_length) |
Paul Zimmerman | a249018 | 2011-02-12 14:06:44 -0800 | [diff] [blame] | 2642 | dev_err(&urb->dev->dev, "%s - ep %#x - Miscalculated tx length, " |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2643 | "queued %#x (%d), asked for %#x (%d)\n", |
| 2644 | __func__, |
| 2645 | urb->ep->desc.bEndpointAddress, |
| 2646 | running_total, running_total, |
| 2647 | urb->transfer_buffer_length, |
| 2648 | urb->transfer_buffer_length); |
| 2649 | } |
| 2650 | |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 2651 | static void giveback_first_trb(struct xhci_hcd *xhci, int slot_id, |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 2652 | unsigned int ep_index, unsigned int stream_id, int start_cycle, |
Andiry Xu | e1eab2e | 2011-01-04 16:30:39 -0800 | [diff] [blame] | 2653 | struct xhci_generic_trb *start_trb) |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2654 | { |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2655 | /* |
| 2656 | * Pass all the TRBs to the hardware at once and make sure this write |
| 2657 | * isn't reordered. |
| 2658 | */ |
| 2659 | wmb(); |
Andiry Xu | 50f7b52 | 2010-12-20 15:09:34 +0800 | [diff] [blame] | 2660 | if (start_cycle) |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 2661 | start_trb->field[3] |= cpu_to_le32(start_cycle); |
Andiry Xu | 50f7b52 | 2010-12-20 15:09:34 +0800 | [diff] [blame] | 2662 | else |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 2663 | start_trb->field[3] &= cpu_to_le32(~TRB_CYCLE); |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 2664 | xhci_ring_ep_doorbell(xhci, slot_id, ep_index, stream_id); |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2665 | } |
| 2666 | |
Sarah Sharp | 624defa | 2009-09-02 12:14:28 -0700 | [diff] [blame] | 2667 | /* |
| 2668 | * xHCI uses normal TRBs for both bulk and interrupt. When the interrupt |
| 2669 | * endpoint is to be serviced, the xHC will consume (at most) one TD. A TD |
| 2670 | * (comprised of sg list entries) can take several service intervals to |
| 2671 | * transmit. |
| 2672 | */ |
| 2673 | int xhci_queue_intr_tx(struct xhci_hcd *xhci, gfp_t mem_flags, |
| 2674 | struct urb *urb, int slot_id, unsigned int ep_index) |
| 2675 | { |
| 2676 | struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci, |
| 2677 | xhci->devs[slot_id]->out_ctx, ep_index); |
| 2678 | int xhci_interval; |
| 2679 | int ep_interval; |
| 2680 | |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 2681 | xhci_interval = EP_INTERVAL_TO_UFRAMES(le32_to_cpu(ep_ctx->ep_info)); |
Sarah Sharp | 624defa | 2009-09-02 12:14:28 -0700 | [diff] [blame] | 2682 | ep_interval = urb->interval; |
| 2683 | /* Convert to microframes */ |
| 2684 | if (urb->dev->speed == USB_SPEED_LOW || |
| 2685 | urb->dev->speed == USB_SPEED_FULL) |
| 2686 | ep_interval *= 8; |
| 2687 | /* FIXME change this to a warning and a suggestion to use the new API |
| 2688 | * to set the polling interval (once the API is added). |
| 2689 | */ |
| 2690 | if (xhci_interval != ep_interval) { |
Andiry Xu | 7961acd | 2010-12-20 17:14:20 +0800 | [diff] [blame] | 2691 | if (printk_ratelimit()) |
Sarah Sharp | 624defa | 2009-09-02 12:14:28 -0700 | [diff] [blame] | 2692 | dev_dbg(&urb->dev->dev, "Driver uses different interval" |
| 2693 | " (%d microframe%s) than xHCI " |
| 2694 | "(%d microframe%s)\n", |
| 2695 | ep_interval, |
| 2696 | ep_interval == 1 ? "" : "s", |
| 2697 | xhci_interval, |
| 2698 | xhci_interval == 1 ? "" : "s"); |
| 2699 | urb->interval = xhci_interval; |
| 2700 | /* Convert back to frames for LS/FS devices */ |
| 2701 | if (urb->dev->speed == USB_SPEED_LOW || |
| 2702 | urb->dev->speed == USB_SPEED_FULL) |
| 2703 | urb->interval /= 8; |
| 2704 | } |
| 2705 | return xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb, slot_id, ep_index); |
| 2706 | } |
| 2707 | |
Sarah Sharp | 04dd950 | 2009-11-11 10:28:30 -0800 | [diff] [blame] | 2708 | /* |
| 2709 | * The TD size is the number of bytes remaining in the TD (including this TRB), |
| 2710 | * right shifted by 10. |
| 2711 | * It must fit in bits 21:17, so it can't be bigger than 31. |
| 2712 | */ |
| 2713 | static u32 xhci_td_remainder(unsigned int remainder) |
| 2714 | { |
| 2715 | u32 max = (1 << (21 - 17 + 1)) - 1; |
| 2716 | |
| 2717 | if ((remainder >> 10) >= max) |
| 2718 | return max << 17; |
| 2719 | else |
| 2720 | return (remainder >> 10) << 17; |
| 2721 | } |
| 2722 | |
Sarah Sharp | 4da6e6f | 2011-04-01 14:01:30 -0700 | [diff] [blame] | 2723 | /* |
| 2724 | * For xHCI 1.0 host controllers, TD size is the number of packets remaining in |
| 2725 | * the TD (*not* including this TRB). |
| 2726 | * |
| 2727 | * Total TD packet count = total_packet_count = |
| 2728 | * roundup(TD size in bytes / wMaxPacketSize) |
| 2729 | * |
| 2730 | * Packets transferred up to and including this TRB = packets_transferred = |
| 2731 | * rounddown(total bytes transferred including this TRB / wMaxPacketSize) |
| 2732 | * |
| 2733 | * TD size = total_packet_count - packets_transferred |
| 2734 | * |
| 2735 | * It must fit in bits 21:17, so it can't be bigger than 31. |
| 2736 | */ |
| 2737 | |
| 2738 | static u32 xhci_v1_0_td_remainder(int running_total, int trb_buff_len, |
| 2739 | unsigned int total_packet_count, struct urb *urb) |
| 2740 | { |
| 2741 | int packets_transferred; |
| 2742 | |
Sarah Sharp | 48df4a6 | 2011-08-12 10:23:01 -0700 | [diff] [blame] | 2743 | /* One TRB with a zero-length data packet. */ |
| 2744 | if (running_total == 0 && trb_buff_len == 0) |
| 2745 | return 0; |
| 2746 | |
Sarah Sharp | 4da6e6f | 2011-04-01 14:01:30 -0700 | [diff] [blame] | 2747 | /* All the TRB queueing functions don't count the current TRB in |
| 2748 | * running_total. |
| 2749 | */ |
| 2750 | packets_transferred = (running_total + trb_buff_len) / |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 2751 | usb_endpoint_maxp(&urb->ep->desc); |
Sarah Sharp | 4da6e6f | 2011-04-01 14:01:30 -0700 | [diff] [blame] | 2752 | |
| 2753 | return xhci_td_remainder(total_packet_count - packets_transferred); |
| 2754 | } |
| 2755 | |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 2756 | static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags, |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2757 | struct urb *urb, int slot_id, unsigned int ep_index) |
| 2758 | { |
| 2759 | struct xhci_ring *ep_ring; |
| 2760 | unsigned int num_trbs; |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 2761 | struct urb_priv *urb_priv; |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2762 | struct xhci_td *td; |
| 2763 | struct scatterlist *sg; |
| 2764 | int num_sgs; |
| 2765 | int trb_buff_len, this_sg_len, running_total; |
Sarah Sharp | 4da6e6f | 2011-04-01 14:01:30 -0700 | [diff] [blame] | 2766 | unsigned int total_packet_count; |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2767 | bool first_trb; |
| 2768 | u64 addr; |
Sarah Sharp | 6cc30d8 | 2010-06-10 12:25:28 -0700 | [diff] [blame] | 2769 | bool more_trbs_coming; |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2770 | |
| 2771 | struct xhci_generic_trb *start_trb; |
| 2772 | int start_cycle; |
| 2773 | |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 2774 | ep_ring = xhci_urb_to_transfer_ring(xhci, urb); |
| 2775 | if (!ep_ring) |
| 2776 | return -EINVAL; |
| 2777 | |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2778 | num_trbs = count_sg_trbs_needed(xhci, urb); |
Clemens Ladisch | bc677d5 | 2011-12-03 23:41:31 +0100 | [diff] [blame] | 2779 | num_sgs = urb->num_mapped_sgs; |
Sarah Sharp | 4da6e6f | 2011-04-01 14:01:30 -0700 | [diff] [blame] | 2780 | total_packet_count = roundup(urb->transfer_buffer_length, |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 2781 | usb_endpoint_maxp(&urb->ep->desc)); |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2782 | |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 2783 | trb_buff_len = prepare_transfer(xhci, xhci->devs[slot_id], |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 2784 | ep_index, urb->stream_id, |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame^] | 2785 | num_trbs, urb, 0, mem_flags); |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2786 | if (trb_buff_len < 0) |
| 2787 | return trb_buff_len; |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 2788 | |
| 2789 | urb_priv = urb->hcpriv; |
| 2790 | td = urb_priv->td[0]; |
| 2791 | |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2792 | /* |
| 2793 | * Don't give the first TRB to the hardware (by toggling the cycle bit) |
| 2794 | * until we've finished creating all the other TRBs. The ring's cycle |
| 2795 | * state may change as we enqueue the other TRBs, so save it too. |
| 2796 | */ |
| 2797 | start_trb = &ep_ring->enqueue->generic; |
| 2798 | start_cycle = ep_ring->cycle_state; |
| 2799 | |
| 2800 | running_total = 0; |
| 2801 | /* |
| 2802 | * How much data is in the first TRB? |
| 2803 | * |
| 2804 | * There are three forces at work for TRB buffer pointers and lengths: |
| 2805 | * 1. We don't want to walk off the end of this sg-list entry buffer. |
| 2806 | * 2. The transfer length that the driver requested may be smaller than |
| 2807 | * the amount of memory allocated for this scatter-gather list. |
| 2808 | * 3. TRBs buffers can't cross 64KB boundaries. |
| 2809 | */ |
Matthew Wilcox | 910f8d0 | 2010-05-01 12:20:01 -0600 | [diff] [blame] | 2810 | sg = urb->sg; |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2811 | addr = (u64) sg_dma_address(sg); |
| 2812 | this_sg_len = sg_dma_len(sg); |
Paul Zimmerman | a249018 | 2011-02-12 14:06:44 -0800 | [diff] [blame] | 2813 | trb_buff_len = TRB_MAX_BUFF_SIZE - (addr & (TRB_MAX_BUFF_SIZE - 1)); |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2814 | trb_buff_len = min_t(int, trb_buff_len, this_sg_len); |
| 2815 | if (trb_buff_len > urb->transfer_buffer_length) |
| 2816 | trb_buff_len = urb->transfer_buffer_length; |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2817 | |
| 2818 | first_trb = true; |
| 2819 | /* Queue the first TRB, even if it's zero-length */ |
| 2820 | do { |
| 2821 | u32 field = 0; |
Sarah Sharp | f9dc68f | 2009-07-27 12:03:07 -0700 | [diff] [blame] | 2822 | u32 length_field = 0; |
Sarah Sharp | 04dd950 | 2009-11-11 10:28:30 -0800 | [diff] [blame] | 2823 | u32 remainder = 0; |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2824 | |
| 2825 | /* Don't change the cycle bit of the first TRB until later */ |
Andiry Xu | 50f7b52 | 2010-12-20 15:09:34 +0800 | [diff] [blame] | 2826 | if (first_trb) { |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2827 | first_trb = false; |
Andiry Xu | 50f7b52 | 2010-12-20 15:09:34 +0800 | [diff] [blame] | 2828 | if (start_cycle == 0) |
| 2829 | field |= 0x1; |
| 2830 | } else |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2831 | field |= ep_ring->cycle_state; |
| 2832 | |
| 2833 | /* Chain all the TRBs together; clear the chain bit in the last |
| 2834 | * TRB to indicate it's the last TRB in the chain. |
| 2835 | */ |
| 2836 | if (num_trbs > 1) { |
| 2837 | field |= TRB_CHAIN; |
| 2838 | } else { |
| 2839 | /* FIXME - add check for ZERO_PACKET flag before this */ |
| 2840 | td->last_trb = ep_ring->enqueue; |
| 2841 | field |= TRB_IOC; |
| 2842 | } |
Sarah Sharp | af8b9e6 | 2011-03-23 16:26:26 -0700 | [diff] [blame] | 2843 | |
| 2844 | /* Only set interrupt on short packet for IN endpoints */ |
| 2845 | if (usb_urb_dir_in(urb)) |
| 2846 | field |= TRB_ISP; |
| 2847 | |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2848 | if (TRB_MAX_BUFF_SIZE - |
Paul Zimmerman | a249018 | 2011-02-12 14:06:44 -0800 | [diff] [blame] | 2849 | (addr & (TRB_MAX_BUFF_SIZE - 1)) < trb_buff_len) { |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2850 | xhci_warn(xhci, "WARN: sg dma xfer crosses 64KB boundaries!\n"); |
| 2851 | xhci_dbg(xhci, "Next boundary at %#x, end dma = %#x\n", |
| 2852 | (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1), |
| 2853 | (unsigned int) addr + trb_buff_len); |
| 2854 | } |
Sarah Sharp | 4da6e6f | 2011-04-01 14:01:30 -0700 | [diff] [blame] | 2855 | |
| 2856 | /* Set the TRB length, TD size, and interrupter fields. */ |
| 2857 | if (xhci->hci_version < 0x100) { |
| 2858 | remainder = xhci_td_remainder( |
| 2859 | urb->transfer_buffer_length - |
| 2860 | running_total); |
| 2861 | } else { |
| 2862 | remainder = xhci_v1_0_td_remainder(running_total, |
| 2863 | trb_buff_len, total_packet_count, urb); |
| 2864 | } |
Sarah Sharp | f9dc68f | 2009-07-27 12:03:07 -0700 | [diff] [blame] | 2865 | length_field = TRB_LEN(trb_buff_len) | |
Sarah Sharp | 04dd950 | 2009-11-11 10:28:30 -0800 | [diff] [blame] | 2866 | remainder | |
Sarah Sharp | f9dc68f | 2009-07-27 12:03:07 -0700 | [diff] [blame] | 2867 | TRB_INTR_TARGET(0); |
Sarah Sharp | 4da6e6f | 2011-04-01 14:01:30 -0700 | [diff] [blame] | 2868 | |
Sarah Sharp | 6cc30d8 | 2010-06-10 12:25:28 -0700 | [diff] [blame] | 2869 | if (num_trbs > 1) |
| 2870 | more_trbs_coming = true; |
| 2871 | else |
| 2872 | more_trbs_coming = false; |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame^] | 2873 | queue_trb(xhci, ep_ring, more_trbs_coming, |
Sarah Sharp | 8e595a5 | 2009-07-27 12:03:31 -0700 | [diff] [blame] | 2874 | lower_32_bits(addr), |
| 2875 | upper_32_bits(addr), |
Sarah Sharp | f9dc68f | 2009-07-27 12:03:07 -0700 | [diff] [blame] | 2876 | length_field, |
Sarah Sharp | af8b9e6 | 2011-03-23 16:26:26 -0700 | [diff] [blame] | 2877 | field | TRB_TYPE(TRB_NORMAL)); |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2878 | --num_trbs; |
| 2879 | running_total += trb_buff_len; |
| 2880 | |
| 2881 | /* Calculate length for next transfer -- |
| 2882 | * Are we done queueing all the TRBs for this sg entry? |
| 2883 | */ |
| 2884 | this_sg_len -= trb_buff_len; |
| 2885 | if (this_sg_len == 0) { |
| 2886 | --num_sgs; |
| 2887 | if (num_sgs == 0) |
| 2888 | break; |
| 2889 | sg = sg_next(sg); |
| 2890 | addr = (u64) sg_dma_address(sg); |
| 2891 | this_sg_len = sg_dma_len(sg); |
| 2892 | } else { |
| 2893 | addr += trb_buff_len; |
| 2894 | } |
| 2895 | |
| 2896 | trb_buff_len = TRB_MAX_BUFF_SIZE - |
Paul Zimmerman | a249018 | 2011-02-12 14:06:44 -0800 | [diff] [blame] | 2897 | (addr & (TRB_MAX_BUFF_SIZE - 1)); |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2898 | trb_buff_len = min_t(int, trb_buff_len, this_sg_len); |
| 2899 | if (running_total + trb_buff_len > urb->transfer_buffer_length) |
| 2900 | trb_buff_len = |
| 2901 | urb->transfer_buffer_length - running_total; |
| 2902 | } while (running_total < urb->transfer_buffer_length); |
| 2903 | |
| 2904 | check_trb_math(urb, num_trbs, running_total); |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 2905 | giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id, |
Andiry Xu | e1eab2e | 2011-01-04 16:30:39 -0800 | [diff] [blame] | 2906 | start_cycle, start_trb); |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2907 | return 0; |
| 2908 | } |
| 2909 | |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 2910 | /* This is very similar to what ehci-q.c qtd_fill() does */ |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 2911 | int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags, |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 2912 | struct urb *urb, int slot_id, unsigned int ep_index) |
| 2913 | { |
| 2914 | struct xhci_ring *ep_ring; |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 2915 | struct urb_priv *urb_priv; |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 2916 | struct xhci_td *td; |
| 2917 | int num_trbs; |
| 2918 | struct xhci_generic_trb *start_trb; |
| 2919 | bool first_trb; |
Sarah Sharp | 6cc30d8 | 2010-06-10 12:25:28 -0700 | [diff] [blame] | 2920 | bool more_trbs_coming; |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 2921 | int start_cycle; |
Sarah Sharp | f9dc68f | 2009-07-27 12:03:07 -0700 | [diff] [blame] | 2922 | u32 field, length_field; |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 2923 | |
| 2924 | int running_total, trb_buff_len, ret; |
Sarah Sharp | 4da6e6f | 2011-04-01 14:01:30 -0700 | [diff] [blame] | 2925 | unsigned int total_packet_count; |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 2926 | u64 addr; |
| 2927 | |
Alan Stern | ff9c895 | 2010-04-02 13:27:28 -0400 | [diff] [blame] | 2928 | if (urb->num_sgs) |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 2929 | return queue_bulk_sg_tx(xhci, mem_flags, urb, slot_id, ep_index); |
| 2930 | |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 2931 | ep_ring = xhci_urb_to_transfer_ring(xhci, urb); |
| 2932 | if (!ep_ring) |
| 2933 | return -EINVAL; |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 2934 | |
| 2935 | num_trbs = 0; |
| 2936 | /* How much data is (potentially) left before the 64KB boundary? */ |
| 2937 | running_total = TRB_MAX_BUFF_SIZE - |
Paul Zimmerman | a249018 | 2011-02-12 14:06:44 -0800 | [diff] [blame] | 2938 | (urb->transfer_dma & (TRB_MAX_BUFF_SIZE - 1)); |
Paul Zimmerman | 5807795 | 2011-02-12 14:07:20 -0800 | [diff] [blame] | 2939 | running_total &= TRB_MAX_BUFF_SIZE - 1; |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 2940 | |
| 2941 | /* If there's some data on this 64KB chunk, or we have to send a |
| 2942 | * zero-length transfer, we need at least one TRB |
| 2943 | */ |
| 2944 | if (running_total != 0 || urb->transfer_buffer_length == 0) |
| 2945 | num_trbs++; |
| 2946 | /* How many more 64KB chunks to transfer, how many more TRBs? */ |
| 2947 | while (running_total < urb->transfer_buffer_length) { |
| 2948 | num_trbs++; |
| 2949 | running_total += TRB_MAX_BUFF_SIZE; |
| 2950 | } |
| 2951 | /* FIXME: this doesn't deal with URB_ZERO_PACKET - need one more */ |
| 2952 | |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 2953 | ret = prepare_transfer(xhci, xhci->devs[slot_id], |
| 2954 | ep_index, urb->stream_id, |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame^] | 2955 | num_trbs, urb, 0, mem_flags); |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 2956 | if (ret < 0) |
| 2957 | return ret; |
| 2958 | |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 2959 | urb_priv = urb->hcpriv; |
| 2960 | td = urb_priv->td[0]; |
| 2961 | |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 2962 | /* |
| 2963 | * Don't give the first TRB to the hardware (by toggling the cycle bit) |
| 2964 | * until we've finished creating all the other TRBs. The ring's cycle |
| 2965 | * state may change as we enqueue the other TRBs, so save it too. |
| 2966 | */ |
| 2967 | start_trb = &ep_ring->enqueue->generic; |
| 2968 | start_cycle = ep_ring->cycle_state; |
| 2969 | |
| 2970 | running_total = 0; |
Sarah Sharp | 4da6e6f | 2011-04-01 14:01:30 -0700 | [diff] [blame] | 2971 | total_packet_count = roundup(urb->transfer_buffer_length, |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 2972 | usb_endpoint_maxp(&urb->ep->desc)); |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 2973 | /* How much data is in the first TRB? */ |
| 2974 | addr = (u64) urb->transfer_dma; |
| 2975 | trb_buff_len = TRB_MAX_BUFF_SIZE - |
Paul Zimmerman | a249018 | 2011-02-12 14:06:44 -0800 | [diff] [blame] | 2976 | (urb->transfer_dma & (TRB_MAX_BUFF_SIZE - 1)); |
| 2977 | if (trb_buff_len > urb->transfer_buffer_length) |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 2978 | trb_buff_len = urb->transfer_buffer_length; |
| 2979 | |
| 2980 | first_trb = true; |
| 2981 | |
| 2982 | /* Queue the first TRB, even if it's zero-length */ |
| 2983 | do { |
Sarah Sharp | 04dd950 | 2009-11-11 10:28:30 -0800 | [diff] [blame] | 2984 | u32 remainder = 0; |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 2985 | field = 0; |
| 2986 | |
| 2987 | /* Don't change the cycle bit of the first TRB until later */ |
Andiry Xu | 50f7b52 | 2010-12-20 15:09:34 +0800 | [diff] [blame] | 2988 | if (first_trb) { |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 2989 | first_trb = false; |
Andiry Xu | 50f7b52 | 2010-12-20 15:09:34 +0800 | [diff] [blame] | 2990 | if (start_cycle == 0) |
| 2991 | field |= 0x1; |
| 2992 | } else |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 2993 | field |= ep_ring->cycle_state; |
| 2994 | |
| 2995 | /* Chain all the TRBs together; clear the chain bit in the last |
| 2996 | * TRB to indicate it's the last TRB in the chain. |
| 2997 | */ |
| 2998 | if (num_trbs > 1) { |
| 2999 | field |= TRB_CHAIN; |
| 3000 | } else { |
| 3001 | /* FIXME - add check for ZERO_PACKET flag before this */ |
| 3002 | td->last_trb = ep_ring->enqueue; |
| 3003 | field |= TRB_IOC; |
| 3004 | } |
Sarah Sharp | af8b9e6 | 2011-03-23 16:26:26 -0700 | [diff] [blame] | 3005 | |
| 3006 | /* Only set interrupt on short packet for IN endpoints */ |
| 3007 | if (usb_urb_dir_in(urb)) |
| 3008 | field |= TRB_ISP; |
| 3009 | |
Sarah Sharp | 4da6e6f | 2011-04-01 14:01:30 -0700 | [diff] [blame] | 3010 | /* Set the TRB length, TD size, and interrupter fields. */ |
| 3011 | if (xhci->hci_version < 0x100) { |
| 3012 | remainder = xhci_td_remainder( |
| 3013 | urb->transfer_buffer_length - |
| 3014 | running_total); |
| 3015 | } else { |
| 3016 | remainder = xhci_v1_0_td_remainder(running_total, |
| 3017 | trb_buff_len, total_packet_count, urb); |
| 3018 | } |
Sarah Sharp | f9dc68f | 2009-07-27 12:03:07 -0700 | [diff] [blame] | 3019 | length_field = TRB_LEN(trb_buff_len) | |
Sarah Sharp | 04dd950 | 2009-11-11 10:28:30 -0800 | [diff] [blame] | 3020 | remainder | |
Sarah Sharp | f9dc68f | 2009-07-27 12:03:07 -0700 | [diff] [blame] | 3021 | TRB_INTR_TARGET(0); |
Sarah Sharp | 4da6e6f | 2011-04-01 14:01:30 -0700 | [diff] [blame] | 3022 | |
Sarah Sharp | 6cc30d8 | 2010-06-10 12:25:28 -0700 | [diff] [blame] | 3023 | if (num_trbs > 1) |
| 3024 | more_trbs_coming = true; |
| 3025 | else |
| 3026 | more_trbs_coming = false; |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame^] | 3027 | queue_trb(xhci, ep_ring, more_trbs_coming, |
Sarah Sharp | 8e595a5 | 2009-07-27 12:03:31 -0700 | [diff] [blame] | 3028 | lower_32_bits(addr), |
| 3029 | upper_32_bits(addr), |
Sarah Sharp | f9dc68f | 2009-07-27 12:03:07 -0700 | [diff] [blame] | 3030 | length_field, |
Sarah Sharp | af8b9e6 | 2011-03-23 16:26:26 -0700 | [diff] [blame] | 3031 | field | TRB_TYPE(TRB_NORMAL)); |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 3032 | --num_trbs; |
| 3033 | running_total += trb_buff_len; |
| 3034 | |
| 3035 | /* Calculate length for next transfer */ |
| 3036 | addr += trb_buff_len; |
| 3037 | trb_buff_len = urb->transfer_buffer_length - running_total; |
| 3038 | if (trb_buff_len > TRB_MAX_BUFF_SIZE) |
| 3039 | trb_buff_len = TRB_MAX_BUFF_SIZE; |
| 3040 | } while (running_total < urb->transfer_buffer_length); |
| 3041 | |
Sarah Sharp | 8a96c05 | 2009-04-27 19:59:19 -0700 | [diff] [blame] | 3042 | check_trb_math(urb, num_trbs, running_total); |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 3043 | giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id, |
Andiry Xu | e1eab2e | 2011-01-04 16:30:39 -0800 | [diff] [blame] | 3044 | start_cycle, start_trb); |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 3045 | return 0; |
| 3046 | } |
| 3047 | |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3048 | /* Caller must have locked xhci->lock */ |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 3049 | int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags, |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3050 | struct urb *urb, int slot_id, unsigned int ep_index) |
| 3051 | { |
| 3052 | struct xhci_ring *ep_ring; |
| 3053 | int num_trbs; |
| 3054 | int ret; |
| 3055 | struct usb_ctrlrequest *setup; |
| 3056 | struct xhci_generic_trb *start_trb; |
| 3057 | int start_cycle; |
Sarah Sharp | f9dc68f | 2009-07-27 12:03:07 -0700 | [diff] [blame] | 3058 | u32 field, length_field; |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 3059 | struct urb_priv *urb_priv; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3060 | struct xhci_td *td; |
| 3061 | |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 3062 | ep_ring = xhci_urb_to_transfer_ring(xhci, urb); |
| 3063 | if (!ep_ring) |
| 3064 | return -EINVAL; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3065 | |
| 3066 | /* |
| 3067 | * Need to copy setup packet into setup TRB, so we can't use the setup |
| 3068 | * DMA address. |
| 3069 | */ |
| 3070 | if (!urb->setup_packet) |
| 3071 | return -EINVAL; |
| 3072 | |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3073 | /* 1 TRB for setup, 1 for status */ |
| 3074 | num_trbs = 2; |
| 3075 | /* |
| 3076 | * Don't need to check if we need additional event data and normal TRBs, |
| 3077 | * since data in control transfers will never get bigger than 16MB |
| 3078 | * XXX: can we get a buffer that crosses 64KB boundaries? |
| 3079 | */ |
| 3080 | if (urb->transfer_buffer_length > 0) |
| 3081 | num_trbs++; |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 3082 | ret = prepare_transfer(xhci, xhci->devs[slot_id], |
| 3083 | ep_index, urb->stream_id, |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame^] | 3084 | num_trbs, urb, 0, mem_flags); |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3085 | if (ret < 0) |
| 3086 | return ret; |
| 3087 | |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 3088 | urb_priv = urb->hcpriv; |
| 3089 | td = urb_priv->td[0]; |
| 3090 | |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3091 | /* |
| 3092 | * Don't give the first TRB to the hardware (by toggling the cycle bit) |
| 3093 | * until we've finished creating all the other TRBs. The ring's cycle |
| 3094 | * state may change as we enqueue the other TRBs, so save it too. |
| 3095 | */ |
| 3096 | start_trb = &ep_ring->enqueue->generic; |
| 3097 | start_cycle = ep_ring->cycle_state; |
| 3098 | |
| 3099 | /* Queue setup TRB - see section 6.4.1.2.1 */ |
| 3100 | /* FIXME better way to translate setup_packet into two u32 fields? */ |
| 3101 | setup = (struct usb_ctrlrequest *) urb->setup_packet; |
Andiry Xu | 50f7b52 | 2010-12-20 15:09:34 +0800 | [diff] [blame] | 3102 | field = 0; |
| 3103 | field |= TRB_IDT | TRB_TYPE(TRB_SETUP); |
| 3104 | if (start_cycle == 0) |
| 3105 | field |= 0x1; |
Andiry Xu | b83cdc8 | 2011-05-05 18:13:56 +0800 | [diff] [blame] | 3106 | |
| 3107 | /* xHCI 1.0 6.4.1.2.1: Transfer Type field */ |
| 3108 | if (xhci->hci_version == 0x100) { |
| 3109 | if (urb->transfer_buffer_length > 0) { |
| 3110 | if (setup->bRequestType & USB_DIR_IN) |
| 3111 | field |= TRB_TX_TYPE(TRB_DATA_IN); |
| 3112 | else |
| 3113 | field |= TRB_TX_TYPE(TRB_DATA_OUT); |
| 3114 | } |
| 3115 | } |
| 3116 | |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame^] | 3117 | queue_trb(xhci, ep_ring, true, |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 3118 | setup->bRequestType | setup->bRequest << 8 | le16_to_cpu(setup->wValue) << 16, |
| 3119 | le16_to_cpu(setup->wIndex) | le16_to_cpu(setup->wLength) << 16, |
| 3120 | TRB_LEN(8) | TRB_INTR_TARGET(0), |
| 3121 | /* Immediate data in pointer */ |
| 3122 | field); |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3123 | |
| 3124 | /* If there's data, queue data TRBs */ |
Sarah Sharp | af8b9e6 | 2011-03-23 16:26:26 -0700 | [diff] [blame] | 3125 | /* Only set interrupt on short packet for IN endpoints */ |
| 3126 | if (usb_urb_dir_in(urb)) |
| 3127 | field = TRB_ISP | TRB_TYPE(TRB_DATA); |
| 3128 | else |
| 3129 | field = TRB_TYPE(TRB_DATA); |
| 3130 | |
Sarah Sharp | f9dc68f | 2009-07-27 12:03:07 -0700 | [diff] [blame] | 3131 | length_field = TRB_LEN(urb->transfer_buffer_length) | |
Sarah Sharp | 04dd950 | 2009-11-11 10:28:30 -0800 | [diff] [blame] | 3132 | xhci_td_remainder(urb->transfer_buffer_length) | |
Sarah Sharp | f9dc68f | 2009-07-27 12:03:07 -0700 | [diff] [blame] | 3133 | TRB_INTR_TARGET(0); |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3134 | if (urb->transfer_buffer_length > 0) { |
| 3135 | if (setup->bRequestType & USB_DIR_IN) |
| 3136 | field |= TRB_DIR_IN; |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame^] | 3137 | queue_trb(xhci, ep_ring, true, |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3138 | lower_32_bits(urb->transfer_dma), |
| 3139 | upper_32_bits(urb->transfer_dma), |
Sarah Sharp | f9dc68f | 2009-07-27 12:03:07 -0700 | [diff] [blame] | 3140 | length_field, |
Sarah Sharp | af8b9e6 | 2011-03-23 16:26:26 -0700 | [diff] [blame] | 3141 | field | ep_ring->cycle_state); |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3142 | } |
| 3143 | |
| 3144 | /* Save the DMA address of the last TRB in the TD */ |
| 3145 | td->last_trb = ep_ring->enqueue; |
| 3146 | |
| 3147 | /* Queue status TRB - see Table 7 and sections 4.11.2.2 and 6.4.1.2.3 */ |
| 3148 | /* If the device sent data, the status stage is an OUT transfer */ |
| 3149 | if (urb->transfer_buffer_length > 0 && setup->bRequestType & USB_DIR_IN) |
| 3150 | field = 0; |
| 3151 | else |
| 3152 | field = TRB_DIR_IN; |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame^] | 3153 | queue_trb(xhci, ep_ring, false, |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3154 | 0, |
| 3155 | 0, |
| 3156 | TRB_INTR_TARGET(0), |
| 3157 | /* Event on completion */ |
| 3158 | field | TRB_IOC | TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state); |
| 3159 | |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 3160 | giveback_first_trb(xhci, slot_id, ep_index, 0, |
Andiry Xu | e1eab2e | 2011-01-04 16:30:39 -0800 | [diff] [blame] | 3161 | start_cycle, start_trb); |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3162 | return 0; |
| 3163 | } |
| 3164 | |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3165 | static int count_isoc_trbs_needed(struct xhci_hcd *xhci, |
| 3166 | struct urb *urb, int i) |
| 3167 | { |
| 3168 | int num_trbs = 0; |
Sarah Sharp | 48df4a6 | 2011-08-12 10:23:01 -0700 | [diff] [blame] | 3169 | u64 addr, td_len; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3170 | |
| 3171 | addr = (u64) (urb->transfer_dma + urb->iso_frame_desc[i].offset); |
| 3172 | td_len = urb->iso_frame_desc[i].length; |
| 3173 | |
Sarah Sharp | 48df4a6 | 2011-08-12 10:23:01 -0700 | [diff] [blame] | 3174 | num_trbs = DIV_ROUND_UP(td_len + (addr & (TRB_MAX_BUFF_SIZE - 1)), |
| 3175 | TRB_MAX_BUFF_SIZE); |
| 3176 | if (num_trbs == 0) |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3177 | num_trbs++; |
| 3178 | |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3179 | return num_trbs; |
| 3180 | } |
| 3181 | |
Sarah Sharp | 5cd43e3 | 2011-04-08 09:37:29 -0700 | [diff] [blame] | 3182 | /* |
| 3183 | * The transfer burst count field of the isochronous TRB defines the number of |
| 3184 | * bursts that are required to move all packets in this TD. Only SuperSpeed |
| 3185 | * devices can burst up to bMaxBurst number of packets per service interval. |
| 3186 | * This field is zero based, meaning a value of zero in the field means one |
| 3187 | * burst. Basically, for everything but SuperSpeed devices, this field will be |
| 3188 | * zero. Only xHCI 1.0 host controllers support this field. |
| 3189 | */ |
| 3190 | static unsigned int xhci_get_burst_count(struct xhci_hcd *xhci, |
| 3191 | struct usb_device *udev, |
| 3192 | struct urb *urb, unsigned int total_packet_count) |
| 3193 | { |
| 3194 | unsigned int max_burst; |
| 3195 | |
| 3196 | if (xhci->hci_version < 0x100 || udev->speed != USB_SPEED_SUPER) |
| 3197 | return 0; |
| 3198 | |
| 3199 | max_burst = urb->ep->ss_ep_comp.bMaxBurst; |
| 3200 | return roundup(total_packet_count, max_burst + 1) - 1; |
| 3201 | } |
| 3202 | |
Sarah Sharp | b61d378 | 2011-04-19 17:43:33 -0700 | [diff] [blame] | 3203 | /* |
| 3204 | * Returns the number of packets in the last "burst" of packets. This field is |
| 3205 | * valid for all speeds of devices. USB 2.0 devices can only do one "burst", so |
| 3206 | * the last burst packet count is equal to the total number of packets in the |
| 3207 | * TD. SuperSpeed endpoints can have up to 3 bursts. All but the last burst |
| 3208 | * must contain (bMaxBurst + 1) number of packets, but the last burst can |
| 3209 | * contain 1 to (bMaxBurst + 1) packets. |
| 3210 | */ |
| 3211 | static unsigned int xhci_get_last_burst_packet_count(struct xhci_hcd *xhci, |
| 3212 | struct usb_device *udev, |
| 3213 | struct urb *urb, unsigned int total_packet_count) |
| 3214 | { |
| 3215 | unsigned int max_burst; |
| 3216 | unsigned int residue; |
| 3217 | |
| 3218 | if (xhci->hci_version < 0x100) |
| 3219 | return 0; |
| 3220 | |
| 3221 | switch (udev->speed) { |
| 3222 | case USB_SPEED_SUPER: |
| 3223 | /* bMaxBurst is zero based: 0 means 1 packet per burst */ |
| 3224 | max_burst = urb->ep->ss_ep_comp.bMaxBurst; |
| 3225 | residue = total_packet_count % (max_burst + 1); |
| 3226 | /* If residue is zero, the last burst contains (max_burst + 1) |
| 3227 | * number of packets, but the TLBPC field is zero-based. |
| 3228 | */ |
| 3229 | if (residue == 0) |
| 3230 | return max_burst; |
| 3231 | return residue - 1; |
| 3232 | default: |
| 3233 | if (total_packet_count == 0) |
| 3234 | return 0; |
| 3235 | return total_packet_count - 1; |
| 3236 | } |
| 3237 | } |
| 3238 | |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3239 | /* This is for isoc transfer */ |
| 3240 | static int xhci_queue_isoc_tx(struct xhci_hcd *xhci, gfp_t mem_flags, |
| 3241 | struct urb *urb, int slot_id, unsigned int ep_index) |
| 3242 | { |
| 3243 | struct xhci_ring *ep_ring; |
| 3244 | struct urb_priv *urb_priv; |
| 3245 | struct xhci_td *td; |
| 3246 | int num_tds, trbs_per_td; |
| 3247 | struct xhci_generic_trb *start_trb; |
| 3248 | bool first_trb; |
| 3249 | int start_cycle; |
| 3250 | u32 field, length_field; |
| 3251 | int running_total, trb_buff_len, td_len, td_remain_len, ret; |
| 3252 | u64 start_addr, addr; |
| 3253 | int i, j; |
Andiry Xu | 47cbf69 | 2010-12-20 14:49:48 +0800 | [diff] [blame] | 3254 | bool more_trbs_coming; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3255 | |
| 3256 | ep_ring = xhci->devs[slot_id]->eps[ep_index].ring; |
| 3257 | |
| 3258 | num_tds = urb->number_of_packets; |
| 3259 | if (num_tds < 1) { |
| 3260 | xhci_dbg(xhci, "Isoc URB with zero packets?\n"); |
| 3261 | return -EINVAL; |
| 3262 | } |
| 3263 | |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3264 | start_addr = (u64) urb->transfer_dma; |
| 3265 | start_trb = &ep_ring->enqueue->generic; |
| 3266 | start_cycle = ep_ring->cycle_state; |
| 3267 | |
Sarah Sharp | 522989a | 2011-07-29 12:44:32 -0700 | [diff] [blame] | 3268 | urb_priv = urb->hcpriv; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3269 | /* Queue the first TRB, even if it's zero-length */ |
| 3270 | for (i = 0; i < num_tds; i++) { |
Sarah Sharp | 4da6e6f | 2011-04-01 14:01:30 -0700 | [diff] [blame] | 3271 | unsigned int total_packet_count; |
Sarah Sharp | 5cd43e3 | 2011-04-08 09:37:29 -0700 | [diff] [blame] | 3272 | unsigned int burst_count; |
Sarah Sharp | b61d378 | 2011-04-19 17:43:33 -0700 | [diff] [blame] | 3273 | unsigned int residue; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3274 | |
Sarah Sharp | 4da6e6f | 2011-04-01 14:01:30 -0700 | [diff] [blame] | 3275 | first_trb = true; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3276 | running_total = 0; |
| 3277 | addr = start_addr + urb->iso_frame_desc[i].offset; |
| 3278 | td_len = urb->iso_frame_desc[i].length; |
| 3279 | td_remain_len = td_len; |
Sarah Sharp | 4da6e6f | 2011-04-01 14:01:30 -0700 | [diff] [blame] | 3280 | total_packet_count = roundup(td_len, |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 3281 | usb_endpoint_maxp(&urb->ep->desc)); |
Sarah Sharp | 48df4a6 | 2011-08-12 10:23:01 -0700 | [diff] [blame] | 3282 | /* A zero-length transfer still involves at least one packet. */ |
| 3283 | if (total_packet_count == 0) |
| 3284 | total_packet_count++; |
Sarah Sharp | 5cd43e3 | 2011-04-08 09:37:29 -0700 | [diff] [blame] | 3285 | burst_count = xhci_get_burst_count(xhci, urb->dev, urb, |
| 3286 | total_packet_count); |
Sarah Sharp | b61d378 | 2011-04-19 17:43:33 -0700 | [diff] [blame] | 3287 | residue = xhci_get_last_burst_packet_count(xhci, |
| 3288 | urb->dev, urb, total_packet_count); |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3289 | |
| 3290 | trbs_per_td = count_isoc_trbs_needed(xhci, urb, i); |
| 3291 | |
| 3292 | ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index, |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame^] | 3293 | urb->stream_id, trbs_per_td, urb, i, mem_flags); |
Sarah Sharp | 522989a | 2011-07-29 12:44:32 -0700 | [diff] [blame] | 3294 | if (ret < 0) { |
| 3295 | if (i == 0) |
| 3296 | return ret; |
| 3297 | goto cleanup; |
| 3298 | } |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3299 | |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3300 | td = urb_priv->td[i]; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3301 | for (j = 0; j < trbs_per_td; j++) { |
| 3302 | u32 remainder = 0; |
Sarah Sharp | b61d378 | 2011-04-19 17:43:33 -0700 | [diff] [blame] | 3303 | field = TRB_TBC(burst_count) | TRB_TLBPC(residue); |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3304 | |
| 3305 | if (first_trb) { |
| 3306 | /* Queue the isoc TRB */ |
| 3307 | field |= TRB_TYPE(TRB_ISOC); |
| 3308 | /* Assume URB_ISO_ASAP is set */ |
| 3309 | field |= TRB_SIA; |
Andiry Xu | 50f7b52 | 2010-12-20 15:09:34 +0800 | [diff] [blame] | 3310 | if (i == 0) { |
| 3311 | if (start_cycle == 0) |
| 3312 | field |= 0x1; |
| 3313 | } else |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3314 | field |= ep_ring->cycle_state; |
| 3315 | first_trb = false; |
| 3316 | } else { |
| 3317 | /* Queue other normal TRBs */ |
| 3318 | field |= TRB_TYPE(TRB_NORMAL); |
| 3319 | field |= ep_ring->cycle_state; |
| 3320 | } |
| 3321 | |
Sarah Sharp | af8b9e6 | 2011-03-23 16:26:26 -0700 | [diff] [blame] | 3322 | /* Only set interrupt on short packet for IN EPs */ |
| 3323 | if (usb_urb_dir_in(urb)) |
| 3324 | field |= TRB_ISP; |
| 3325 | |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3326 | /* Chain all the TRBs together; clear the chain bit in |
| 3327 | * the last TRB to indicate it's the last TRB in the |
| 3328 | * chain. |
| 3329 | */ |
| 3330 | if (j < trbs_per_td - 1) { |
| 3331 | field |= TRB_CHAIN; |
Andiry Xu | 47cbf69 | 2010-12-20 14:49:48 +0800 | [diff] [blame] | 3332 | more_trbs_coming = true; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3333 | } else { |
| 3334 | td->last_trb = ep_ring->enqueue; |
| 3335 | field |= TRB_IOC; |
Andiry Xu | ad106f2 | 2011-05-05 18:14:02 +0800 | [diff] [blame] | 3336 | if (xhci->hci_version == 0x100) { |
| 3337 | /* Set BEI bit except for the last td */ |
| 3338 | if (i < num_tds - 1) |
| 3339 | field |= TRB_BEI; |
| 3340 | } |
Andiry Xu | 47cbf69 | 2010-12-20 14:49:48 +0800 | [diff] [blame] | 3341 | more_trbs_coming = false; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3342 | } |
| 3343 | |
| 3344 | /* Calculate TRB length */ |
| 3345 | trb_buff_len = TRB_MAX_BUFF_SIZE - |
| 3346 | (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1)); |
| 3347 | if (trb_buff_len > td_remain_len) |
| 3348 | trb_buff_len = td_remain_len; |
| 3349 | |
Sarah Sharp | 4da6e6f | 2011-04-01 14:01:30 -0700 | [diff] [blame] | 3350 | /* Set the TRB length, TD size, & interrupter fields. */ |
| 3351 | if (xhci->hci_version < 0x100) { |
| 3352 | remainder = xhci_td_remainder( |
| 3353 | td_len - running_total); |
| 3354 | } else { |
| 3355 | remainder = xhci_v1_0_td_remainder( |
| 3356 | running_total, trb_buff_len, |
| 3357 | total_packet_count, urb); |
| 3358 | } |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3359 | length_field = TRB_LEN(trb_buff_len) | |
| 3360 | remainder | |
| 3361 | TRB_INTR_TARGET(0); |
Sarah Sharp | 4da6e6f | 2011-04-01 14:01:30 -0700 | [diff] [blame] | 3362 | |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame^] | 3363 | queue_trb(xhci, ep_ring, more_trbs_coming, |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3364 | lower_32_bits(addr), |
| 3365 | upper_32_bits(addr), |
| 3366 | length_field, |
Sarah Sharp | af8b9e6 | 2011-03-23 16:26:26 -0700 | [diff] [blame] | 3367 | field); |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3368 | running_total += trb_buff_len; |
| 3369 | |
| 3370 | addr += trb_buff_len; |
| 3371 | td_remain_len -= trb_buff_len; |
| 3372 | } |
| 3373 | |
| 3374 | /* Check TD length */ |
| 3375 | if (running_total != td_len) { |
| 3376 | xhci_err(xhci, "ISOC TD length unmatch\n"); |
Andiry Xu | cf84055 | 2012-01-18 17:47:12 +0800 | [diff] [blame] | 3377 | ret = -EINVAL; |
| 3378 | goto cleanup; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3379 | } |
| 3380 | } |
| 3381 | |
Andiry Xu | c41136b | 2011-03-22 17:08:14 +0800 | [diff] [blame] | 3382 | if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs == 0) { |
| 3383 | if (xhci->quirks & XHCI_AMD_PLL_FIX) |
| 3384 | usb_amd_quirk_pll_disable(); |
| 3385 | } |
| 3386 | xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs++; |
| 3387 | |
Andiry Xu | e1eab2e | 2011-01-04 16:30:39 -0800 | [diff] [blame] | 3388 | giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id, |
| 3389 | start_cycle, start_trb); |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3390 | return 0; |
Sarah Sharp | 522989a | 2011-07-29 12:44:32 -0700 | [diff] [blame] | 3391 | cleanup: |
| 3392 | /* Clean up a partially enqueued isoc transfer. */ |
| 3393 | |
| 3394 | for (i--; i >= 0; i--) |
Sarah Sharp | 585df1d | 2011-08-02 15:43:40 -0700 | [diff] [blame] | 3395 | list_del_init(&urb_priv->td[i]->td_list); |
Sarah Sharp | 522989a | 2011-07-29 12:44:32 -0700 | [diff] [blame] | 3396 | |
| 3397 | /* Use the first TD as a temporary variable to turn the TDs we've queued |
| 3398 | * into No-ops with a software-owned cycle bit. That way the hardware |
| 3399 | * won't accidentally start executing bogus TDs when we partially |
| 3400 | * overwrite them. td->first_trb and td->start_seg are already set. |
| 3401 | */ |
| 3402 | urb_priv->td[0]->last_trb = ep_ring->enqueue; |
| 3403 | /* Every TRB except the first & last will have its cycle bit flipped. */ |
| 3404 | td_to_noop(xhci, ep_ring, urb_priv->td[0], true); |
| 3405 | |
| 3406 | /* Reset the ring enqueue back to the first TRB and its cycle bit. */ |
| 3407 | ep_ring->enqueue = urb_priv->td[0]->first_trb; |
| 3408 | ep_ring->enq_seg = urb_priv->td[0]->start_seg; |
| 3409 | ep_ring->cycle_state = start_cycle; |
| 3410 | usb_hcd_unlink_urb_from_ep(bus_to_hcd(urb->dev->bus), urb); |
| 3411 | return ret; |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3412 | } |
| 3413 | |
| 3414 | /* |
| 3415 | * Check transfer ring to guarantee there is enough room for the urb. |
| 3416 | * Update ISO URB start_frame and interval. |
| 3417 | * Update interval as xhci_queue_intr_tx does. Just use xhci frame_index to |
| 3418 | * update the urb->start_frame by now. |
| 3419 | * Always assume URB_ISO_ASAP set, and NEVER use urb->start_frame as input. |
| 3420 | */ |
| 3421 | int xhci_queue_isoc_tx_prepare(struct xhci_hcd *xhci, gfp_t mem_flags, |
| 3422 | struct urb *urb, int slot_id, unsigned int ep_index) |
| 3423 | { |
| 3424 | struct xhci_virt_device *xdev; |
| 3425 | struct xhci_ring *ep_ring; |
| 3426 | struct xhci_ep_ctx *ep_ctx; |
| 3427 | int start_frame; |
| 3428 | int xhci_interval; |
| 3429 | int ep_interval; |
| 3430 | int num_tds, num_trbs, i; |
| 3431 | int ret; |
| 3432 | |
| 3433 | xdev = xhci->devs[slot_id]; |
| 3434 | ep_ring = xdev->eps[ep_index].ring; |
| 3435 | ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index); |
| 3436 | |
| 3437 | num_trbs = 0; |
| 3438 | num_tds = urb->number_of_packets; |
| 3439 | for (i = 0; i < num_tds; i++) |
| 3440 | num_trbs += count_isoc_trbs_needed(xhci, urb, i); |
| 3441 | |
| 3442 | /* Check the ring to guarantee there is enough room for the whole urb. |
| 3443 | * Do not insert any td of the urb to the ring if the check failed. |
| 3444 | */ |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 3445 | ret = prepare_ring(xhci, ep_ring, le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK, |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame^] | 3446 | num_trbs, mem_flags); |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3447 | if (ret) |
| 3448 | return ret; |
| 3449 | |
| 3450 | start_frame = xhci_readl(xhci, &xhci->run_regs->microframe_index); |
| 3451 | start_frame &= 0x3fff; |
| 3452 | |
| 3453 | urb->start_frame = start_frame; |
| 3454 | if (urb->dev->speed == USB_SPEED_LOW || |
| 3455 | urb->dev->speed == USB_SPEED_FULL) |
| 3456 | urb->start_frame >>= 3; |
| 3457 | |
Matt Evans | 28ccd29 | 2011-03-29 13:40:46 +1100 | [diff] [blame] | 3458 | xhci_interval = EP_INTERVAL_TO_UFRAMES(le32_to_cpu(ep_ctx->ep_info)); |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3459 | ep_interval = urb->interval; |
| 3460 | /* Convert to microframes */ |
| 3461 | if (urb->dev->speed == USB_SPEED_LOW || |
| 3462 | urb->dev->speed == USB_SPEED_FULL) |
| 3463 | ep_interval *= 8; |
| 3464 | /* FIXME change this to a warning and a suggestion to use the new API |
| 3465 | * to set the polling interval (once the API is added). |
| 3466 | */ |
| 3467 | if (xhci_interval != ep_interval) { |
Andiry Xu | 7961acd | 2010-12-20 17:14:20 +0800 | [diff] [blame] | 3468 | if (printk_ratelimit()) |
Andiry Xu | 04e5190 | 2010-07-22 15:23:39 -0700 | [diff] [blame] | 3469 | dev_dbg(&urb->dev->dev, "Driver uses different interval" |
| 3470 | " (%d microframe%s) than xHCI " |
| 3471 | "(%d microframe%s)\n", |
| 3472 | ep_interval, |
| 3473 | ep_interval == 1 ? "" : "s", |
| 3474 | xhci_interval, |
| 3475 | xhci_interval == 1 ? "" : "s"); |
| 3476 | urb->interval = xhci_interval; |
| 3477 | /* Convert back to frames for LS/FS devices */ |
| 3478 | if (urb->dev->speed == USB_SPEED_LOW || |
| 3479 | urb->dev->speed == USB_SPEED_FULL) |
| 3480 | urb->interval /= 8; |
| 3481 | } |
| 3482 | return xhci_queue_isoc_tx(xhci, GFP_ATOMIC, urb, slot_id, ep_index); |
| 3483 | } |
| 3484 | |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 3485 | /**** Command Ring Operations ****/ |
| 3486 | |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 3487 | /* Generic function for queueing a command TRB on the command ring. |
| 3488 | * Check to make sure there's room on the command ring for one command TRB. |
| 3489 | * Also check that there's room reserved for commands that must not fail. |
| 3490 | * If this is a command that must not fail, meaning command_must_succeed = TRUE, |
| 3491 | * then only check for the number of reserved spots. |
| 3492 | * Don't decrement xhci->cmd_ring_reserved_trbs after we've queued the TRB |
| 3493 | * because the command event handler may want to resubmit a failed command. |
| 3494 | */ |
| 3495 | static int queue_command(struct xhci_hcd *xhci, u32 field1, u32 field2, |
| 3496 | u32 field3, u32 field4, bool command_must_succeed) |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 3497 | { |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 3498 | int reserved_trbs = xhci->cmd_ring_reserved_trbs; |
Sarah Sharp | d1dc908 | 2010-07-09 17:08:38 +0200 | [diff] [blame] | 3499 | int ret; |
| 3500 | |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 3501 | if (!command_must_succeed) |
| 3502 | reserved_trbs++; |
| 3503 | |
Sarah Sharp | d1dc908 | 2010-07-09 17:08:38 +0200 | [diff] [blame] | 3504 | ret = prepare_ring(xhci, xhci->cmd_ring, EP_STATE_RUNNING, |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame^] | 3505 | reserved_trbs, GFP_ATOMIC); |
Sarah Sharp | d1dc908 | 2010-07-09 17:08:38 +0200 | [diff] [blame] | 3506 | if (ret < 0) { |
| 3507 | xhci_err(xhci, "ERR: No room for command on command ring\n"); |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 3508 | if (command_must_succeed) |
| 3509 | xhci_err(xhci, "ERR: Reserved TRB counting for " |
| 3510 | "unfailable commands failed.\n"); |
Sarah Sharp | d1dc908 | 2010-07-09 17:08:38 +0200 | [diff] [blame] | 3511 | return ret; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 3512 | } |
Andiry Xu | 3b72fca | 2012-03-05 17:49:32 +0800 | [diff] [blame^] | 3513 | queue_trb(xhci, xhci->cmd_ring, false, field1, field2, field3, |
| 3514 | field4 | xhci->cmd_ring->cycle_state); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 3515 | return 0; |
| 3516 | } |
| 3517 | |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 3518 | /* Queue a slot enable or disable request on the command ring */ |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 3519 | int xhci_queue_slot_control(struct xhci_hcd *xhci, u32 trb_type, u32 slot_id) |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 3520 | { |
| 3521 | return queue_command(xhci, 0, 0, 0, |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 3522 | TRB_TYPE(trb_type) | SLOT_ID_FOR_TRB(slot_id), false); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 3523 | } |
| 3524 | |
| 3525 | /* Queue an address device command TRB */ |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 3526 | int xhci_queue_address_device(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr, |
| 3527 | u32 slot_id) |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 3528 | { |
Sarah Sharp | 8e595a5 | 2009-07-27 12:03:31 -0700 | [diff] [blame] | 3529 | return queue_command(xhci, lower_32_bits(in_ctx_ptr), |
| 3530 | upper_32_bits(in_ctx_ptr), 0, |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 3531 | TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id), |
| 3532 | false); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 3533 | } |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 3534 | |
Sarah Sharp | 0238634 | 2010-05-24 13:25:28 -0700 | [diff] [blame] | 3535 | int xhci_queue_vendor_command(struct xhci_hcd *xhci, |
| 3536 | u32 field1, u32 field2, u32 field3, u32 field4) |
| 3537 | { |
| 3538 | return queue_command(xhci, field1, field2, field3, field4, false); |
| 3539 | } |
| 3540 | |
Sarah Sharp | 2a8f82c | 2009-12-09 15:59:13 -0800 | [diff] [blame] | 3541 | /* Queue a reset device command TRB */ |
| 3542 | int xhci_queue_reset_device(struct xhci_hcd *xhci, u32 slot_id) |
| 3543 | { |
| 3544 | return queue_command(xhci, 0, 0, 0, |
| 3545 | TRB_TYPE(TRB_RESET_DEV) | SLOT_ID_FOR_TRB(slot_id), |
| 3546 | false); |
| 3547 | } |
| 3548 | |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 3549 | /* Queue a configure endpoint command TRB */ |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 3550 | int xhci_queue_configure_endpoint(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr, |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 3551 | u32 slot_id, bool command_must_succeed) |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 3552 | { |
Sarah Sharp | 8e595a5 | 2009-07-27 12:03:31 -0700 | [diff] [blame] | 3553 | return queue_command(xhci, lower_32_bits(in_ctx_ptr), |
| 3554 | upper_32_bits(in_ctx_ptr), 0, |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 3555 | TRB_TYPE(TRB_CONFIG_EP) | SLOT_ID_FOR_TRB(slot_id), |
| 3556 | command_must_succeed); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 3557 | } |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 3558 | |
Sarah Sharp | f2217e8 | 2009-08-07 14:04:43 -0700 | [diff] [blame] | 3559 | /* Queue an evaluate context command TRB */ |
| 3560 | int xhci_queue_evaluate_context(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr, |
| 3561 | u32 slot_id) |
| 3562 | { |
| 3563 | return queue_command(xhci, lower_32_bits(in_ctx_ptr), |
| 3564 | upper_32_bits(in_ctx_ptr), 0, |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 3565 | TRB_TYPE(TRB_EVAL_CONTEXT) | SLOT_ID_FOR_TRB(slot_id), |
| 3566 | false); |
Sarah Sharp | f2217e8 | 2009-08-07 14:04:43 -0700 | [diff] [blame] | 3567 | } |
| 3568 | |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 3569 | /* |
| 3570 | * Suspend is set to indicate "Stop Endpoint Command" is being issued to stop |
| 3571 | * activity on an endpoint that is about to be suspended. |
| 3572 | */ |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 3573 | int xhci_queue_stop_endpoint(struct xhci_hcd *xhci, int slot_id, |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 3574 | unsigned int ep_index, int suspend) |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 3575 | { |
| 3576 | u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id); |
| 3577 | u32 trb_ep_index = EP_ID_FOR_TRB(ep_index); |
| 3578 | u32 type = TRB_TYPE(TRB_STOP_RING); |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 3579 | u32 trb_suspend = SUSPEND_PORT_FOR_TRB(suspend); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 3580 | |
| 3581 | return queue_command(xhci, 0, 0, 0, |
Andiry Xu | be88fe4 | 2010-10-14 07:22:57 -0700 | [diff] [blame] | 3582 | trb_slot_id | trb_ep_index | type | trb_suspend, false); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 3583 | } |
| 3584 | |
| 3585 | /* Set Transfer Ring Dequeue Pointer command. |
| 3586 | * This should not be used for endpoints that have streams enabled. |
| 3587 | */ |
| 3588 | static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id, |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 3589 | unsigned int ep_index, unsigned int stream_id, |
| 3590 | struct xhci_segment *deq_seg, |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 3591 | union xhci_trb *deq_ptr, u32 cycle_state) |
| 3592 | { |
| 3593 | dma_addr_t addr; |
| 3594 | u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id); |
| 3595 | u32 trb_ep_index = EP_ID_FOR_TRB(ep_index); |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 3596 | u32 trb_stream_id = STREAM_ID_FOR_TRB(stream_id); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 3597 | u32 type = TRB_TYPE(TRB_SET_DEQ); |
Sarah Sharp | bf161e8 | 2011-02-23 15:46:42 -0800 | [diff] [blame] | 3598 | struct xhci_virt_ep *ep; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 3599 | |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 3600 | addr = xhci_trb_virt_to_dma(deq_seg, deq_ptr); |
Sarah Sharp | c92bcfa | 2009-07-27 12:05:21 -0700 | [diff] [blame] | 3601 | if (addr == 0) { |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 3602 | xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr\n"); |
Greg Kroah-Hartman | 700e205 | 2009-04-29 19:14:08 -0700 | [diff] [blame] | 3603 | xhci_warn(xhci, "WARN deq seg = %p, deq pt = %p\n", |
| 3604 | deq_seg, deq_ptr); |
Sarah Sharp | c92bcfa | 2009-07-27 12:05:21 -0700 | [diff] [blame] | 3605 | return 0; |
| 3606 | } |
Sarah Sharp | bf161e8 | 2011-02-23 15:46:42 -0800 | [diff] [blame] | 3607 | ep = &xhci->devs[slot_id]->eps[ep_index]; |
| 3608 | if ((ep->ep_state & SET_DEQ_PENDING)) { |
| 3609 | xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr\n"); |
| 3610 | xhci_warn(xhci, "A Set TR Deq Ptr command is pending.\n"); |
| 3611 | return 0; |
| 3612 | } |
| 3613 | ep->queued_deq_seg = deq_seg; |
| 3614 | ep->queued_deq_ptr = deq_ptr; |
Sarah Sharp | 8e595a5 | 2009-07-27 12:03:31 -0700 | [diff] [blame] | 3615 | return queue_command(xhci, lower_32_bits(addr) | cycle_state, |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 3616 | upper_32_bits(addr), trb_stream_id, |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 3617 | trb_slot_id | trb_ep_index | type, false); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 3618 | } |
Sarah Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 3619 | |
| 3620 | int xhci_queue_reset_ep(struct xhci_hcd *xhci, int slot_id, |
| 3621 | unsigned int ep_index) |
| 3622 | { |
| 3623 | u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id); |
| 3624 | u32 trb_ep_index = EP_ID_FOR_TRB(ep_index); |
| 3625 | u32 type = TRB_TYPE(TRB_RESET_EP); |
| 3626 | |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 3627 | return queue_command(xhci, 0, 0, 0, trb_slot_id | trb_ep_index | type, |
| 3628 | false); |
Sarah Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 3629 | } |