Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 2 | /* |
| 3 | * USB HOST XHCI Controller stack |
| 4 | * |
| 5 | * Based on xHCI host controller driver in linux-kernel |
| 6 | * by Sarah Sharp. |
| 7 | * |
| 8 | * Copyright (C) 2008 Intel Corp. |
| 9 | * Author: Sarah Sharp |
| 10 | * |
| 11 | * Copyright (C) 2013 Samsung Electronics Co.Ltd |
| 12 | * Authors: Vivek Gautam <gautam.vivek@samsung.com> |
| 13 | * Vikas Sajjan <vikas.sajjan@samsung.com> |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <common.h> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 17 | #include <cpu_func.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 18 | #include <log.h> |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 19 | #include <asm/byteorder.h> |
| 20 | #include <usb.h> |
| 21 | #include <asm/unaligned.h> |
Simon Glass | c06c1be | 2020-05-10 11:40:08 -0600 | [diff] [blame] | 22 | #include <linux/bug.h> |
Masahiro Yamada | 64e4f7f | 2016-09-21 11:28:57 +0900 | [diff] [blame] | 23 | #include <linux/errno.h> |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 24 | |
Jean-Jacques Hiblot | ad4142b | 2019-09-11 11:33:46 +0200 | [diff] [blame] | 25 | #include <usb/xhci.h> |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Is this TRB a link TRB or was the last TRB the last TRB in this event ring |
| 29 | * segment? I.e. would the updated event TRB pointer step off the end of the |
| 30 | * event seg ? |
| 31 | * |
| 32 | * @param ctrl Host controller data structure |
| 33 | * @param ring pointer to the ring |
| 34 | * @param seg poniter to the segment to which TRB belongs |
| 35 | * @param trb poniter to the ring trb |
| 36 | * @return 1 if this TRB a link TRB else 0 |
| 37 | */ |
| 38 | static int last_trb(struct xhci_ctrl *ctrl, struct xhci_ring *ring, |
| 39 | struct xhci_segment *seg, union xhci_trb *trb) |
| 40 | { |
| 41 | if (ring == ctrl->event_ring) |
| 42 | return trb == &seg->trbs[TRBS_PER_SEGMENT]; |
| 43 | else |
| 44 | return TRB_TYPE_LINK_LE32(trb->link.control); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Does this link TRB point to the first segment in a ring, |
| 49 | * or was the previous TRB the last TRB on the last segment in the ERST? |
| 50 | * |
| 51 | * @param ctrl Host controller data structure |
| 52 | * @param ring pointer to the ring |
| 53 | * @param seg poniter to the segment to which TRB belongs |
| 54 | * @param trb poniter to the ring trb |
| 55 | * @return 1 if this TRB is the last TRB on the last segment else 0 |
| 56 | */ |
| 57 | static bool last_trb_on_last_seg(struct xhci_ctrl *ctrl, |
| 58 | struct xhci_ring *ring, |
| 59 | struct xhci_segment *seg, |
| 60 | union xhci_trb *trb) |
| 61 | { |
| 62 | if (ring == ctrl->event_ring) |
| 63 | return ((trb == &seg->trbs[TRBS_PER_SEGMENT]) && |
| 64 | (seg->next == ring->first_seg)); |
| 65 | else |
| 66 | return le32_to_cpu(trb->link.control) & LINK_TOGGLE; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * See Cycle bit rules. SW is the consumer for the event ring only. |
| 71 | * Don't make a ring full of link TRBs. That would be dumb and this would loop. |
| 72 | * |
| 73 | * If we've just enqueued a TRB that is in the middle of a TD (meaning the |
| 74 | * chain bit is set), then set the chain bit in all the following link TRBs. |
| 75 | * If we've enqueued the last TRB in a TD, make sure the following link TRBs |
| 76 | * have their chain bit cleared (so that each Link TRB is a separate TD). |
| 77 | * |
| 78 | * Section 6.4.4.1 of the 0.95 spec says link TRBs cannot have the chain bit |
| 79 | * set, but other sections talk about dealing with the chain bit set. This was |
| 80 | * fixed in the 0.96 specification errata, but we have to assume that all 0.95 |
| 81 | * xHCI hardware can't handle the chain bit being cleared on a link TRB. |
| 82 | * |
| 83 | * @param ctrl Host controller data structure |
| 84 | * @param ring pointer to the ring |
| 85 | * @param more_trbs_coming flag to indicate whether more trbs |
| 86 | * are expected or NOT. |
| 87 | * Will you enqueue more TRBs before calling |
| 88 | * prepare_ring()? |
| 89 | * @return none |
| 90 | */ |
| 91 | static void inc_enq(struct xhci_ctrl *ctrl, struct xhci_ring *ring, |
| 92 | bool more_trbs_coming) |
| 93 | { |
| 94 | u32 chain; |
| 95 | union xhci_trb *next; |
| 96 | |
| 97 | chain = le32_to_cpu(ring->enqueue->generic.field[3]) & TRB_CHAIN; |
| 98 | next = ++(ring->enqueue); |
| 99 | |
| 100 | /* |
| 101 | * Update the dequeue pointer further if that was a link TRB or we're at |
| 102 | * the end of an event ring segment (which doesn't have link TRBS) |
| 103 | */ |
| 104 | while (last_trb(ctrl, ring, ring->enq_seg, next)) { |
| 105 | if (ring != ctrl->event_ring) { |
| 106 | /* |
| 107 | * If the caller doesn't plan on enqueueing more |
| 108 | * TDs before ringing the doorbell, then we |
| 109 | * don't want to give the link TRB to the |
| 110 | * hardware just yet. We'll give the link TRB |
| 111 | * back in prepare_ring() just before we enqueue |
| 112 | * the TD at the top of the ring. |
| 113 | */ |
| 114 | if (!chain && !more_trbs_coming) |
| 115 | break; |
| 116 | |
| 117 | /* |
| 118 | * If we're not dealing with 0.95 hardware or |
| 119 | * isoc rings on AMD 0.96 host, |
| 120 | * carry over the chain bit of the previous TRB |
| 121 | * (which may mean the chain bit is cleared). |
| 122 | */ |
| 123 | next->link.control &= cpu_to_le32(~TRB_CHAIN); |
| 124 | next->link.control |= cpu_to_le32(chain); |
| 125 | |
| 126 | next->link.control ^= cpu_to_le32(TRB_CYCLE); |
Sergey Temerkhanov | 3859346 | 2015-04-01 17:18:45 +0300 | [diff] [blame] | 127 | xhci_flush_cache((uintptr_t)next, |
| 128 | sizeof(union xhci_trb)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 129 | } |
| 130 | /* Toggle the cycle bit after the last ring segment. */ |
| 131 | if (last_trb_on_last_seg(ctrl, ring, |
| 132 | ring->enq_seg, next)) |
| 133 | ring->cycle_state = (ring->cycle_state ? 0 : 1); |
| 134 | |
| 135 | ring->enq_seg = ring->enq_seg->next; |
| 136 | ring->enqueue = ring->enq_seg->trbs; |
| 137 | next = ring->enqueue; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * See Cycle bit rules. SW is the consumer for the event ring only. |
| 143 | * Don't make a ring full of link TRBs. That would be dumb and this would loop. |
| 144 | * |
| 145 | * @param ctrl Host controller data structure |
| 146 | * @param ring Ring whose Dequeue TRB pointer needs to be incremented. |
| 147 | * return none |
| 148 | */ |
| 149 | static void inc_deq(struct xhci_ctrl *ctrl, struct xhci_ring *ring) |
| 150 | { |
| 151 | do { |
| 152 | /* |
| 153 | * Update the dequeue pointer further if that was a link TRB or |
| 154 | * we're at the end of an event ring segment (which doesn't have |
| 155 | * link TRBS) |
| 156 | */ |
| 157 | if (last_trb(ctrl, ring, ring->deq_seg, ring->dequeue)) { |
| 158 | if (ring == ctrl->event_ring && |
| 159 | last_trb_on_last_seg(ctrl, ring, |
| 160 | ring->deq_seg, ring->dequeue)) { |
| 161 | ring->cycle_state = (ring->cycle_state ? 0 : 1); |
| 162 | } |
| 163 | ring->deq_seg = ring->deq_seg->next; |
| 164 | ring->dequeue = ring->deq_seg->trbs; |
| 165 | } else { |
| 166 | ring->dequeue++; |
| 167 | } |
| 168 | } while (last_trb(ctrl, ring, ring->deq_seg, ring->dequeue)); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Generic function for queueing a TRB on a ring. |
| 173 | * The caller must have checked to make sure there's room on the ring. |
| 174 | * |
| 175 | * @param more_trbs_coming: Will you enqueue more TRBs before calling |
| 176 | * prepare_ring()? |
| 177 | * @param ctrl Host controller data structure |
| 178 | * @param ring pointer to the ring |
| 179 | * @param more_trbs_coming flag to indicate whether more trbs |
| 180 | * @param trb_fields pointer to trb field array containing TRB contents |
| 181 | * @return pointer to the enqueued trb |
| 182 | */ |
| 183 | static struct xhci_generic_trb *queue_trb(struct xhci_ctrl *ctrl, |
| 184 | struct xhci_ring *ring, |
| 185 | bool more_trbs_coming, |
| 186 | unsigned int *trb_fields) |
| 187 | { |
| 188 | struct xhci_generic_trb *trb; |
| 189 | int i; |
| 190 | |
| 191 | trb = &ring->enqueue->generic; |
| 192 | |
| 193 | for (i = 0; i < 4; i++) |
| 194 | trb->field[i] = cpu_to_le32(trb_fields[i]); |
| 195 | |
Sergey Temerkhanov | 3859346 | 2015-04-01 17:18:45 +0300 | [diff] [blame] | 196 | xhci_flush_cache((uintptr_t)trb, sizeof(struct xhci_generic_trb)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 197 | |
| 198 | inc_enq(ctrl, ring, more_trbs_coming); |
| 199 | |
| 200 | return trb; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Does various checks on the endpoint ring, and makes it ready |
| 205 | * to queue num_trbs. |
| 206 | * |
| 207 | * @param ctrl Host controller data structure |
| 208 | * @param ep_ring pointer to the EP Transfer Ring |
| 209 | * @param ep_state State of the End Point |
| 210 | * @return error code in case of invalid ep_state, 0 on success |
| 211 | */ |
| 212 | static int prepare_ring(struct xhci_ctrl *ctrl, struct xhci_ring *ep_ring, |
| 213 | u32 ep_state) |
| 214 | { |
| 215 | union xhci_trb *next = ep_ring->enqueue; |
| 216 | |
| 217 | /* Make sure the endpoint has been added to xHC schedule */ |
| 218 | switch (ep_state) { |
| 219 | case EP_STATE_DISABLED: |
| 220 | /* |
| 221 | * USB core changed config/interfaces without notifying us, |
| 222 | * or hardware is reporting the wrong state. |
| 223 | */ |
| 224 | puts("WARN urb submitted to disabled ep\n"); |
| 225 | return -ENOENT; |
| 226 | case EP_STATE_ERROR: |
| 227 | puts("WARN waiting for error on ep to be cleared\n"); |
| 228 | return -EINVAL; |
| 229 | case EP_STATE_HALTED: |
| 230 | puts("WARN halted endpoint, queueing URB anyway.\n"); |
| 231 | case EP_STATE_STOPPED: |
| 232 | case EP_STATE_RUNNING: |
| 233 | debug("EP STATE RUNNING.\n"); |
| 234 | break; |
| 235 | default: |
| 236 | puts("ERROR unknown endpoint state for ep\n"); |
| 237 | return -EINVAL; |
| 238 | } |
| 239 | |
| 240 | while (last_trb(ctrl, ep_ring, ep_ring->enq_seg, next)) { |
| 241 | /* |
| 242 | * If we're not dealing with 0.95 hardware or isoc rings |
| 243 | * on AMD 0.96 host, clear the chain bit. |
| 244 | */ |
| 245 | next->link.control &= cpu_to_le32(~TRB_CHAIN); |
| 246 | |
| 247 | next->link.control ^= cpu_to_le32(TRB_CYCLE); |
| 248 | |
Sergey Temerkhanov | 3859346 | 2015-04-01 17:18:45 +0300 | [diff] [blame] | 249 | xhci_flush_cache((uintptr_t)next, sizeof(union xhci_trb)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 250 | |
| 251 | /* Toggle the cycle bit after the last ring segment. */ |
| 252 | if (last_trb_on_last_seg(ctrl, ep_ring, |
| 253 | ep_ring->enq_seg, next)) |
| 254 | ep_ring->cycle_state = (ep_ring->cycle_state ? 0 : 1); |
| 255 | ep_ring->enq_seg = ep_ring->enq_seg->next; |
| 256 | ep_ring->enqueue = ep_ring->enq_seg->trbs; |
| 257 | next = ep_ring->enqueue; |
| 258 | } |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Generic function for queueing a command TRB on the command ring. |
| 265 | * Check to make sure there's room on the command ring for one command TRB. |
| 266 | * |
| 267 | * @param ctrl Host controller data structure |
| 268 | * @param ptr Pointer address to write in the first two fields (opt.) |
| 269 | * @param slot_id Slot ID to encode in the flags field (opt.) |
| 270 | * @param ep_index Endpoint index to encode in the flags field (opt.) |
| 271 | * @param cmd Command type to enqueue |
| 272 | * @return none |
| 273 | */ |
| 274 | void xhci_queue_command(struct xhci_ctrl *ctrl, u8 *ptr, u32 slot_id, |
| 275 | u32 ep_index, trb_type cmd) |
| 276 | { |
| 277 | u32 fields[4]; |
Stefan Roese | caf8cae | 2020-07-21 10:46:05 +0200 | [diff] [blame] | 278 | u64 val_64 = virt_to_phys(ptr); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 279 | |
| 280 | BUG_ON(prepare_ring(ctrl, ctrl->cmd_ring, EP_STATE_RUNNING)); |
| 281 | |
| 282 | fields[0] = lower_32_bits(val_64); |
| 283 | fields[1] = upper_32_bits(val_64); |
| 284 | fields[2] = 0; |
Bin Meng | 474b250 | 2017-07-19 21:49:54 +0800 | [diff] [blame] | 285 | fields[3] = TRB_TYPE(cmd) | SLOT_ID_FOR_TRB(slot_id) | |
| 286 | ctrl->cmd_ring->cycle_state; |
| 287 | |
| 288 | /* |
| 289 | * Only 'reset endpoint', 'stop endpoint' and 'set TR dequeue pointer' |
| 290 | * commands need endpoint id encoded. |
| 291 | */ |
| 292 | if (cmd >= TRB_RESET_EP && cmd <= TRB_SET_DEQ) |
| 293 | fields[3] |= EP_ID_FOR_TRB(ep_index); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 294 | |
| 295 | queue_trb(ctrl, ctrl->cmd_ring, false, fields); |
| 296 | |
| 297 | /* Ring the command ring doorbell */ |
| 298 | xhci_writel(&ctrl->dba->doorbell[0], DB_VALUE_HOST); |
| 299 | } |
| 300 | |
developer | 570c2a9 | 2020-09-08 18:59:56 +0200 | [diff] [blame] | 301 | /* |
| 302 | * For xHCI 1.0 host controllers, TD size is the number of max packet sized |
| 303 | * packets remaining in the TD (*not* including this TRB). |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 304 | * |
developer | 570c2a9 | 2020-09-08 18:59:56 +0200 | [diff] [blame] | 305 | * Total TD packet count = total_packet_count = |
| 306 | * DIV_ROUND_UP(TD size in bytes / wMaxPacketSize) |
| 307 | * |
| 308 | * Packets transferred up to and including this TRB = packets_transferred = |
| 309 | * rounddown(total bytes transferred including this TRB / wMaxPacketSize) |
| 310 | * |
| 311 | * TD size = total_packet_count - packets_transferred |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 312 | * |
developer | 570c2a9 | 2020-09-08 18:59:56 +0200 | [diff] [blame] | 313 | * For xHCI 0.96 and older, TD size field should be the remaining bytes |
| 314 | * including this TRB, right shifted by 10 |
| 315 | * |
| 316 | * For all hosts it must fit in bits 21:17, so it can't be bigger than 31. |
| 317 | * This is taken care of in the TRB_TD_SIZE() macro |
| 318 | * |
| 319 | * The last TRB in a TD must have the TD size set to zero. |
| 320 | * |
| 321 | * @param ctrl host controller data structure |
| 322 | * @param transferred total size sent so far |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 323 | * @param trb_buff_len length of the TRB Buffer |
developer | 570c2a9 | 2020-09-08 18:59:56 +0200 | [diff] [blame] | 324 | * @param td_total_len total packet count |
| 325 | * @param maxp max packet size of current pipe |
| 326 | * @param more_trbs_coming indicate last trb in TD |
| 327 | * @return remainder |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 328 | */ |
developer | 570c2a9 | 2020-09-08 18:59:56 +0200 | [diff] [blame] | 329 | static u32 xhci_td_remainder(struct xhci_ctrl *ctrl, int transferred, |
| 330 | int trb_buff_len, unsigned int td_total_len, |
| 331 | int maxp, bool more_trbs_coming) |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 332 | { |
developer | 570c2a9 | 2020-09-08 18:59:56 +0200 | [diff] [blame] | 333 | u32 total_packet_count; |
| 334 | |
developer | 8039053 | 2020-09-08 18:59:57 +0200 | [diff] [blame] | 335 | /* MTK xHCI 0.96 contains some features from 1.0 */ |
| 336 | if (ctrl->hci_version < 0x100 && !(ctrl->quirks & XHCI_MTK_HOST)) |
developer | 570c2a9 | 2020-09-08 18:59:56 +0200 | [diff] [blame] | 337 | return ((td_total_len - transferred) >> 10); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 338 | |
| 339 | /* One TRB with a zero-length data packet. */ |
developer | 570c2a9 | 2020-09-08 18:59:56 +0200 | [diff] [blame] | 340 | if (!more_trbs_coming || (transferred == 0 && trb_buff_len == 0) || |
| 341 | trb_buff_len == td_total_len) |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 342 | return 0; |
| 343 | |
developer | 8039053 | 2020-09-08 18:59:57 +0200 | [diff] [blame] | 344 | /* for MTK xHCI 0.96, TD size include this TRB, but not in 1.x */ |
| 345 | if ((ctrl->quirks & XHCI_MTK_HOST) && (ctrl->hci_version < 0x100)) |
| 346 | trb_buff_len = 0; |
| 347 | |
developer | 570c2a9 | 2020-09-08 18:59:56 +0200 | [diff] [blame] | 348 | total_packet_count = DIV_ROUND_UP(td_total_len, maxp); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 349 | |
developer | 570c2a9 | 2020-09-08 18:59:56 +0200 | [diff] [blame] | 350 | /* Queueing functions don't count the current TRB into transferred */ |
| 351 | return (total_packet_count - ((transferred + trb_buff_len) / maxp)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Ring the doorbell of the End Point |
| 356 | * |
| 357 | * @param udev pointer to the USB device structure |
| 358 | * @param ep_index index of the endpoint |
| 359 | * @param start_cycle cycle flag of the first TRB |
| 360 | * @param start_trb pionter to the first TRB |
| 361 | * @return none |
| 362 | */ |
| 363 | static void giveback_first_trb(struct usb_device *udev, int ep_index, |
| 364 | int start_cycle, |
| 365 | struct xhci_generic_trb *start_trb) |
| 366 | { |
Simon Glass | a49e27b | 2015-03-25 12:22:49 -0600 | [diff] [blame] | 367 | struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 368 | |
| 369 | /* |
| 370 | * Pass all the TRBs to the hardware at once and make sure this write |
| 371 | * isn't reordered. |
| 372 | */ |
| 373 | if (start_cycle) |
| 374 | start_trb->field[3] |= cpu_to_le32(start_cycle); |
| 375 | else |
| 376 | start_trb->field[3] &= cpu_to_le32(~TRB_CYCLE); |
| 377 | |
Sergey Temerkhanov | 3859346 | 2015-04-01 17:18:45 +0300 | [diff] [blame] | 378 | xhci_flush_cache((uintptr_t)start_trb, sizeof(struct xhci_generic_trb)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 379 | |
| 380 | /* Ringing EP doorbell here */ |
| 381 | xhci_writel(&ctrl->dba->doorbell[udev->slot_id], |
| 382 | DB_VALUE(ep_index, 0)); |
| 383 | |
| 384 | return; |
| 385 | } |
| 386 | |
| 387 | /**** POLLING mechanism for XHCI ****/ |
| 388 | |
| 389 | /** |
| 390 | * Finalizes a handled event TRB by advancing our dequeue pointer and giving |
| 391 | * the TRB back to the hardware for recycling. Must call this exactly once at |
| 392 | * the end of each event handler, and not touch the TRB again afterwards. |
| 393 | * |
| 394 | * @param ctrl Host controller data structure |
| 395 | * @return none |
| 396 | */ |
| 397 | void xhci_acknowledge_event(struct xhci_ctrl *ctrl) |
| 398 | { |
| 399 | /* Advance our dequeue pointer to the next event */ |
| 400 | inc_deq(ctrl, ctrl->event_ring); |
| 401 | |
| 402 | /* Inform the hardware */ |
| 403 | xhci_writeq(&ctrl->ir_set->erst_dequeue, |
Stefan Roese | caf8cae | 2020-07-21 10:46:05 +0200 | [diff] [blame] | 404 | virt_to_phys(ctrl->event_ring->dequeue) | ERST_EHB); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Checks if there is a new event to handle on the event ring. |
| 409 | * |
| 410 | * @param ctrl Host controller data structure |
| 411 | * @return 0 if failure else 1 on success |
| 412 | */ |
| 413 | static int event_ready(struct xhci_ctrl *ctrl) |
| 414 | { |
| 415 | union xhci_trb *event; |
| 416 | |
Sergey Temerkhanov | 3859346 | 2015-04-01 17:18:45 +0300 | [diff] [blame] | 417 | xhci_inval_cache((uintptr_t)ctrl->event_ring->dequeue, |
| 418 | sizeof(union xhci_trb)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 419 | |
| 420 | event = ctrl->event_ring->dequeue; |
| 421 | |
| 422 | /* Does the HC or OS own the TRB? */ |
| 423 | if ((le32_to_cpu(event->event_cmd.flags) & TRB_CYCLE) != |
| 424 | ctrl->event_ring->cycle_state) |
| 425 | return 0; |
| 426 | |
| 427 | return 1; |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Waits for a specific type of event and returns it. Discards unexpected |
| 432 | * events. Caller *must* call xhci_acknowledge_event() after it is finished |
| 433 | * processing the event, and must not access the returned pointer afterwards. |
| 434 | * |
| 435 | * @param ctrl Host controller data structure |
| 436 | * @param expected TRB type expected from Event TRB |
| 437 | * @return pointer to event trb |
| 438 | */ |
| 439 | union xhci_trb *xhci_wait_for_event(struct xhci_ctrl *ctrl, trb_type expected) |
| 440 | { |
| 441 | trb_type type; |
| 442 | unsigned long ts = get_timer(0); |
| 443 | |
| 444 | do { |
| 445 | union xhci_trb *event = ctrl->event_ring->dequeue; |
| 446 | |
| 447 | if (!event_ready(ctrl)) |
| 448 | continue; |
| 449 | |
| 450 | type = TRB_FIELD_TO_TYPE(le32_to_cpu(event->event_cmd.flags)); |
| 451 | if (type == expected) |
| 452 | return event; |
| 453 | |
| 454 | if (type == TRB_PORT_STATUS) |
| 455 | /* TODO: remove this once enumeration has been reworked */ |
| 456 | /* |
| 457 | * Port status change events always have a |
| 458 | * successful completion code |
| 459 | */ |
| 460 | BUG_ON(GET_COMP_CODE( |
| 461 | le32_to_cpu(event->generic.field[2])) != |
| 462 | COMP_SUCCESS); |
| 463 | else |
| 464 | printf("Unexpected XHCI event TRB, skipping... " |
| 465 | "(%08x %08x %08x %08x)\n", |
| 466 | le32_to_cpu(event->generic.field[0]), |
| 467 | le32_to_cpu(event->generic.field[1]), |
| 468 | le32_to_cpu(event->generic.field[2]), |
| 469 | le32_to_cpu(event->generic.field[3])); |
| 470 | |
| 471 | xhci_acknowledge_event(ctrl); |
| 472 | } while (get_timer(ts) < XHCI_TIMEOUT); |
| 473 | |
| 474 | if (expected == TRB_TRANSFER) |
| 475 | return NULL; |
| 476 | |
| 477 | printf("XHCI timeout on event type %d... cannot recover.\n", expected); |
| 478 | BUG(); |
| 479 | } |
| 480 | |
| 481 | /* |
| 482 | * Stops transfer processing for an endpoint and throws away all unprocessed |
| 483 | * TRBs by setting the xHC's dequeue pointer to our enqueue pointer. The next |
| 484 | * xhci_bulk_tx/xhci_ctrl_tx on this enpoint will add new transfers there and |
| 485 | * ring the doorbell, causing this endpoint to start working again. |
| 486 | * (Careful: This will BUG() when there was no transfer in progress. Shouldn't |
| 487 | * happen in practice for current uses and is too complicated to fix right now.) |
| 488 | */ |
| 489 | static void abort_td(struct usb_device *udev, int ep_index) |
| 490 | { |
Simon Glass | a49e27b | 2015-03-25 12:22:49 -0600 | [diff] [blame] | 491 | struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 492 | struct xhci_ring *ring = ctrl->devs[udev->slot_id]->eps[ep_index].ring; |
| 493 | union xhci_trb *event; |
| 494 | u32 field; |
| 495 | |
| 496 | xhci_queue_command(ctrl, NULL, udev->slot_id, ep_index, TRB_STOP_RING); |
| 497 | |
| 498 | event = xhci_wait_for_event(ctrl, TRB_TRANSFER); |
| 499 | field = le32_to_cpu(event->trans_event.flags); |
| 500 | BUG_ON(TRB_TO_SLOT_ID(field) != udev->slot_id); |
| 501 | BUG_ON(TRB_TO_EP_INDEX(field) != ep_index); |
| 502 | BUG_ON(GET_COMP_CODE(le32_to_cpu(event->trans_event.transfer_len |
| 503 | != COMP_STOP))); |
| 504 | xhci_acknowledge_event(ctrl); |
| 505 | |
| 506 | event = xhci_wait_for_event(ctrl, TRB_COMPLETION); |
| 507 | BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags)) |
| 508 | != udev->slot_id || GET_COMP_CODE(le32_to_cpu( |
| 509 | event->event_cmd.status)) != COMP_SUCCESS); |
| 510 | xhci_acknowledge_event(ctrl); |
| 511 | |
| 512 | xhci_queue_command(ctrl, (void *)((uintptr_t)ring->enqueue | |
| 513 | ring->cycle_state), udev->slot_id, ep_index, TRB_SET_DEQ); |
| 514 | event = xhci_wait_for_event(ctrl, TRB_COMPLETION); |
| 515 | BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags)) |
| 516 | != udev->slot_id || GET_COMP_CODE(le32_to_cpu( |
| 517 | event->event_cmd.status)) != COMP_SUCCESS); |
| 518 | xhci_acknowledge_event(ctrl); |
| 519 | } |
| 520 | |
| 521 | static void record_transfer_result(struct usb_device *udev, |
| 522 | union xhci_trb *event, int length) |
| 523 | { |
| 524 | udev->act_len = min(length, length - |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 525 | (int)EVENT_TRB_LEN(le32_to_cpu(event->trans_event.transfer_len))); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 526 | |
| 527 | switch (GET_COMP_CODE(le32_to_cpu(event->trans_event.transfer_len))) { |
| 528 | case COMP_SUCCESS: |
| 529 | BUG_ON(udev->act_len != length); |
| 530 | /* fallthrough */ |
| 531 | case COMP_SHORT_TX: |
| 532 | udev->status = 0; |
| 533 | break; |
| 534 | case COMP_STALL: |
| 535 | udev->status = USB_ST_STALLED; |
| 536 | break; |
| 537 | case COMP_DB_ERR: |
| 538 | case COMP_TRB_ERR: |
| 539 | udev->status = USB_ST_BUF_ERR; |
| 540 | break; |
| 541 | case COMP_BABBLE: |
| 542 | udev->status = USB_ST_BABBLE_DET; |
| 543 | break; |
| 544 | default: |
| 545 | udev->status = 0x80; /* USB_ST_TOO_LAZY_TO_MAKE_A_NEW_MACRO */ |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | /**** Bulk and Control transfer methods ****/ |
| 550 | /** |
| 551 | * Queues up the BULK Request |
| 552 | * |
| 553 | * @param udev pointer to the USB device structure |
| 554 | * @param pipe contains the DIR_IN or OUT , devnum |
| 555 | * @param length length of the buffer |
| 556 | * @param buffer buffer to be read/written based on the request |
| 557 | * @return returns 0 if successful else -1 on failure |
| 558 | */ |
| 559 | int xhci_bulk_tx(struct usb_device *udev, unsigned long pipe, |
| 560 | int length, void *buffer) |
| 561 | { |
| 562 | int num_trbs = 0; |
| 563 | struct xhci_generic_trb *start_trb; |
Gustavo A. R. Silva | 0a1ef7c | 2018-01-20 02:37:31 -0600 | [diff] [blame] | 564 | bool first_trb = false; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 565 | int start_cycle; |
| 566 | u32 field = 0; |
| 567 | u32 length_field = 0; |
Simon Glass | a49e27b | 2015-03-25 12:22:49 -0600 | [diff] [blame] | 568 | struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 569 | int slot_id = udev->slot_id; |
| 570 | int ep_index; |
| 571 | struct xhci_virt_device *virt_dev; |
| 572 | struct xhci_ep_ctx *ep_ctx; |
| 573 | struct xhci_ring *ring; /* EP transfer ring */ |
| 574 | union xhci_trb *event; |
| 575 | |
| 576 | int running_total, trb_buff_len; |
developer | 570c2a9 | 2020-09-08 18:59:56 +0200 | [diff] [blame] | 577 | bool more_trbs_coming = true; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 578 | int maxpacketsize; |
| 579 | u64 addr; |
| 580 | int ret; |
| 581 | u32 trb_fields[4]; |
Stefan Roese | caf8cae | 2020-07-21 10:46:05 +0200 | [diff] [blame] | 582 | u64 val_64 = virt_to_phys(buffer); |
Ran Wang | a050583 | 2020-11-18 15:49:02 +0800 | [diff] [blame] | 583 | void *last_transfer_trb_addr; |
| 584 | int available_length; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 585 | |
| 586 | debug("dev=%p, pipe=%lx, buffer=%p, length=%d\n", |
| 587 | udev, pipe, buffer, length); |
| 588 | |
Ran Wang | a050583 | 2020-11-18 15:49:02 +0800 | [diff] [blame] | 589 | available_length = length; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 590 | ep_index = usb_pipe_ep_index(pipe); |
| 591 | virt_dev = ctrl->devs[slot_id]; |
| 592 | |
Sergey Temerkhanov | 3859346 | 2015-04-01 17:18:45 +0300 | [diff] [blame] | 593 | xhci_inval_cache((uintptr_t)virt_dev->out_ctx->bytes, |
| 594 | virt_dev->out_ctx->size); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 595 | |
| 596 | ep_ctx = xhci_get_ep_ctx(ctrl, virt_dev->out_ctx, ep_index); |
| 597 | |
| 598 | ring = virt_dev->eps[ep_index].ring; |
| 599 | /* |
| 600 | * How much data is (potentially) left before the 64KB boundary? |
| 601 | * XHCI Spec puts restriction( TABLE 49 and 6.4.1 section of XHCI Spec) |
| 602 | * that the buffer should not span 64KB boundary. if so |
| 603 | * we send request in more than 1 TRB by chaining them. |
| 604 | */ |
| 605 | running_total = TRB_MAX_BUFF_SIZE - |
| 606 | (lower_32_bits(val_64) & (TRB_MAX_BUFF_SIZE - 1)); |
| 607 | trb_buff_len = running_total; |
| 608 | running_total &= TRB_MAX_BUFF_SIZE - 1; |
| 609 | |
| 610 | /* |
| 611 | * If there's some data on this 64KB chunk, or we have to send a |
| 612 | * zero-length transfer, we need at least one TRB |
| 613 | */ |
| 614 | if (running_total != 0 || length == 0) |
| 615 | num_trbs++; |
| 616 | |
| 617 | /* How many more 64KB chunks to transfer, how many more TRBs? */ |
| 618 | while (running_total < length) { |
| 619 | num_trbs++; |
| 620 | running_total += TRB_MAX_BUFF_SIZE; |
| 621 | } |
| 622 | |
| 623 | /* |
| 624 | * XXX: Calling routine prepare_ring() called in place of |
| 625 | * prepare_trasfer() as there in 'Linux' since we are not |
| 626 | * maintaining multiple TDs/transfer at the same time. |
| 627 | */ |
| 628 | ret = prepare_ring(ctrl, ring, |
| 629 | le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK); |
| 630 | if (ret < 0) |
| 631 | return ret; |
| 632 | |
| 633 | /* |
| 634 | * Don't give the first TRB to the hardware (by toggling the cycle bit) |
| 635 | * until we've finished creating all the other TRBs. The ring's cycle |
| 636 | * state may change as we enqueue the other TRBs, so save it too. |
| 637 | */ |
| 638 | start_trb = &ring->enqueue->generic; |
| 639 | start_cycle = ring->cycle_state; |
| 640 | |
| 641 | running_total = 0; |
| 642 | maxpacketsize = usb_maxpacket(udev, pipe); |
| 643 | |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 644 | /* How much data is in the first TRB? */ |
| 645 | /* |
| 646 | * How much data is (potentially) left before the 64KB boundary? |
| 647 | * XHCI Spec puts restriction( TABLE 49 and 6.4.1 section of XHCI Spec) |
| 648 | * that the buffer should not span 64KB boundary. if so |
| 649 | * we send request in more than 1 TRB by chaining them. |
| 650 | */ |
| 651 | addr = val_64; |
| 652 | |
| 653 | if (trb_buff_len > length) |
| 654 | trb_buff_len = length; |
| 655 | |
| 656 | first_trb = true; |
| 657 | |
| 658 | /* flush the buffer before use */ |
Sergey Temerkhanov | 3859346 | 2015-04-01 17:18:45 +0300 | [diff] [blame] | 659 | xhci_flush_cache((uintptr_t)buffer, length); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 660 | |
| 661 | /* Queue the first TRB, even if it's zero-length */ |
| 662 | do { |
| 663 | u32 remainder = 0; |
| 664 | field = 0; |
| 665 | /* Don't change the cycle bit of the first TRB until later */ |
| 666 | if (first_trb) { |
| 667 | first_trb = false; |
| 668 | if (start_cycle == 0) |
| 669 | field |= TRB_CYCLE; |
| 670 | } else { |
| 671 | field |= ring->cycle_state; |
| 672 | } |
| 673 | |
| 674 | /* |
| 675 | * Chain all the TRBs together; clear the chain bit in the last |
| 676 | * TRB to indicate it's the last TRB in the chain. |
| 677 | */ |
developer | 570c2a9 | 2020-09-08 18:59:56 +0200 | [diff] [blame] | 678 | if (num_trbs > 1) { |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 679 | field |= TRB_CHAIN; |
developer | 570c2a9 | 2020-09-08 18:59:56 +0200 | [diff] [blame] | 680 | } else { |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 681 | field |= TRB_IOC; |
developer | 570c2a9 | 2020-09-08 18:59:56 +0200 | [diff] [blame] | 682 | more_trbs_coming = false; |
| 683 | } |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 684 | |
| 685 | /* Only set interrupt on short packet for IN endpoints */ |
| 686 | if (usb_pipein(pipe)) |
| 687 | field |= TRB_ISP; |
| 688 | |
| 689 | /* Set the TRB length, TD size, and interrupter fields. */ |
developer | 570c2a9 | 2020-09-08 18:59:56 +0200 | [diff] [blame] | 690 | remainder = xhci_td_remainder(ctrl, running_total, trb_buff_len, |
| 691 | length, maxpacketsize, |
| 692 | more_trbs_coming); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 693 | |
developer | 6cabb14 | 2020-09-08 19:00:00 +0200 | [diff] [blame] | 694 | length_field = (TRB_LEN(trb_buff_len) | |
developer | 570c2a9 | 2020-09-08 18:59:56 +0200 | [diff] [blame] | 695 | TRB_TD_SIZE(remainder) | |
developer | 6cabb14 | 2020-09-08 19:00:00 +0200 | [diff] [blame] | 696 | TRB_INTR_TARGET(0)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 697 | |
| 698 | trb_fields[0] = lower_32_bits(addr); |
| 699 | trb_fields[1] = upper_32_bits(addr); |
| 700 | trb_fields[2] = length_field; |
developer | 497dcfa | 2020-09-08 18:59:59 +0200 | [diff] [blame] | 701 | trb_fields[3] = field | TRB_TYPE(TRB_NORMAL); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 702 | |
Ran Wang | a050583 | 2020-11-18 15:49:02 +0800 | [diff] [blame] | 703 | last_transfer_trb_addr = queue_trb(ctrl, ring, (num_trbs > 1), trb_fields); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 704 | |
| 705 | --num_trbs; |
| 706 | |
| 707 | running_total += trb_buff_len; |
| 708 | |
| 709 | /* Calculate length for next transfer */ |
| 710 | addr += trb_buff_len; |
| 711 | trb_buff_len = min((length - running_total), TRB_MAX_BUFF_SIZE); |
| 712 | } while (running_total < length); |
| 713 | |
| 714 | giveback_first_trb(udev, ep_index, start_cycle, start_trb); |
| 715 | |
Ran Wang | a050583 | 2020-11-18 15:49:02 +0800 | [diff] [blame] | 716 | again: |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 717 | event = xhci_wait_for_event(ctrl, TRB_TRANSFER); |
| 718 | if (!event) { |
| 719 | debug("XHCI bulk transfer timed out, aborting...\n"); |
| 720 | abort_td(udev, ep_index); |
| 721 | udev->status = USB_ST_NAK_REC; /* closest thing to a timeout */ |
| 722 | udev->act_len = 0; |
| 723 | return -ETIMEDOUT; |
| 724 | } |
Ran Wang | a050583 | 2020-11-18 15:49:02 +0800 | [diff] [blame] | 725 | |
| 726 | if ((uintptr_t)(le64_to_cpu(event->trans_event.buffer)) |
| 727 | != (uintptr_t)last_transfer_trb_addr) { |
| 728 | available_length -= |
| 729 | (int)EVENT_TRB_LEN(le32_to_cpu(event->trans_event.transfer_len)); |
| 730 | xhci_acknowledge_event(ctrl); |
| 731 | goto again; |
| 732 | } |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 733 | |
Ran Wang | a050583 | 2020-11-18 15:49:02 +0800 | [diff] [blame] | 734 | field = le32_to_cpu(event->trans_event.flags); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 735 | BUG_ON(TRB_TO_SLOT_ID(field) != slot_id); |
| 736 | BUG_ON(TRB_TO_EP_INDEX(field) != ep_index); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 737 | |
Ran Wang | a050583 | 2020-11-18 15:49:02 +0800 | [diff] [blame] | 738 | record_transfer_result(udev, event, available_length); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 739 | xhci_acknowledge_event(ctrl); |
Sergey Temerkhanov | 3859346 | 2015-04-01 17:18:45 +0300 | [diff] [blame] | 740 | xhci_inval_cache((uintptr_t)buffer, length); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 741 | |
| 742 | return (udev->status != USB_ST_NOT_PROC) ? 0 : -1; |
| 743 | } |
| 744 | |
| 745 | /** |
| 746 | * Queues up the Control Transfer Request |
| 747 | * |
| 748 | * @param udev pointer to the USB device structure |
| 749 | * @param pipe contains the DIR_IN or OUT , devnum |
| 750 | * @param req request type |
| 751 | * @param length length of the buffer |
| 752 | * @param buffer buffer to be read/written based on the request |
| 753 | * @return returns 0 if successful else error code on failure |
| 754 | */ |
| 755 | int xhci_ctrl_tx(struct usb_device *udev, unsigned long pipe, |
| 756 | struct devrequest *req, int length, |
| 757 | void *buffer) |
| 758 | { |
| 759 | int ret; |
| 760 | int start_cycle; |
| 761 | int num_trbs; |
| 762 | u32 field; |
| 763 | u32 length_field; |
| 764 | u64 buf_64 = 0; |
| 765 | struct xhci_generic_trb *start_trb; |
Simon Glass | a49e27b | 2015-03-25 12:22:49 -0600 | [diff] [blame] | 766 | struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 767 | int slot_id = udev->slot_id; |
| 768 | int ep_index; |
| 769 | u32 trb_fields[4]; |
| 770 | struct xhci_virt_device *virt_dev = ctrl->devs[slot_id]; |
| 771 | struct xhci_ring *ep_ring; |
| 772 | union xhci_trb *event; |
developer | 570c2a9 | 2020-09-08 18:59:56 +0200 | [diff] [blame] | 773 | u32 remainder; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 774 | |
| 775 | debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n", |
| 776 | req->request, req->request, |
| 777 | req->requesttype, req->requesttype, |
| 778 | le16_to_cpu(req->value), le16_to_cpu(req->value), |
| 779 | le16_to_cpu(req->index)); |
| 780 | |
| 781 | ep_index = usb_pipe_ep_index(pipe); |
| 782 | |
| 783 | ep_ring = virt_dev->eps[ep_index].ring; |
| 784 | |
| 785 | /* |
| 786 | * Check to see if the max packet size for the default control |
| 787 | * endpoint changed during FS device enumeration |
| 788 | */ |
| 789 | if (udev->speed == USB_SPEED_FULL) { |
| 790 | ret = xhci_check_maxpacket(udev); |
| 791 | if (ret < 0) |
| 792 | return ret; |
| 793 | } |
| 794 | |
Sergey Temerkhanov | 3859346 | 2015-04-01 17:18:45 +0300 | [diff] [blame] | 795 | xhci_inval_cache((uintptr_t)virt_dev->out_ctx->bytes, |
| 796 | virt_dev->out_ctx->size); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 797 | |
| 798 | struct xhci_ep_ctx *ep_ctx = NULL; |
| 799 | ep_ctx = xhci_get_ep_ctx(ctrl, virt_dev->out_ctx, ep_index); |
| 800 | |
| 801 | /* 1 TRB for setup, 1 for status */ |
| 802 | num_trbs = 2; |
| 803 | /* |
| 804 | * Don't need to check if we need additional event data and normal TRBs, |
| 805 | * since data in control transfers will never get bigger than 16MB |
| 806 | * XXX: can we get a buffer that crosses 64KB boundaries? |
| 807 | */ |
| 808 | |
| 809 | if (length > 0) |
| 810 | num_trbs++; |
| 811 | /* |
| 812 | * XXX: Calling routine prepare_ring() called in place of |
| 813 | * prepare_trasfer() as there in 'Linux' since we are not |
| 814 | * maintaining multiple TDs/transfer at the same time. |
| 815 | */ |
| 816 | ret = prepare_ring(ctrl, ep_ring, |
| 817 | le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK); |
| 818 | |
| 819 | if (ret < 0) |
| 820 | return ret; |
| 821 | |
| 822 | /* |
| 823 | * Don't give the first TRB to the hardware (by toggling the cycle bit) |
| 824 | * until we've finished creating all the other TRBs. The ring's cycle |
| 825 | * state may change as we enqueue the other TRBs, so save it too. |
| 826 | */ |
| 827 | start_trb = &ep_ring->enqueue->generic; |
| 828 | start_cycle = ep_ring->cycle_state; |
| 829 | |
| 830 | debug("start_trb %p, start_cycle %d\n", start_trb, start_cycle); |
| 831 | |
| 832 | /* Queue setup TRB - see section 6.4.1.2.1 */ |
| 833 | /* FIXME better way to translate setup_packet into two u32 fields? */ |
| 834 | field = 0; |
developer | 497dcfa | 2020-09-08 18:59:59 +0200 | [diff] [blame] | 835 | field |= TRB_IDT | TRB_TYPE(TRB_SETUP); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 836 | if (start_cycle == 0) |
| 837 | field |= 0x1; |
| 838 | |
| 839 | /* xHCI 1.0 6.4.1.2.1: Transfer Type field */ |
developer | 8039053 | 2020-09-08 18:59:57 +0200 | [diff] [blame] | 840 | if (ctrl->hci_version >= 0x100 || ctrl->quirks & XHCI_MTK_HOST) { |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 841 | if (length > 0) { |
| 842 | if (req->requesttype & USB_DIR_IN) |
developer | 57c052b | 2020-09-08 19:00:01 +0200 | [diff] [blame] | 843 | field |= TRB_TX_TYPE(TRB_DATA_IN); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 844 | else |
developer | 57c052b | 2020-09-08 19:00:01 +0200 | [diff] [blame] | 845 | field |= TRB_TX_TYPE(TRB_DATA_OUT); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 846 | } |
| 847 | } |
| 848 | |
| 849 | debug("req->requesttype = %d, req->request = %d," |
| 850 | "le16_to_cpu(req->value) = %d," |
| 851 | "le16_to_cpu(req->index) = %d," |
| 852 | "le16_to_cpu(req->length) = %d\n", |
| 853 | req->requesttype, req->request, le16_to_cpu(req->value), |
| 854 | le16_to_cpu(req->index), le16_to_cpu(req->length)); |
| 855 | |
| 856 | trb_fields[0] = req->requesttype | req->request << 8 | |
| 857 | le16_to_cpu(req->value) << 16; |
| 858 | trb_fields[1] = le16_to_cpu(req->index) | |
| 859 | le16_to_cpu(req->length) << 16; |
| 860 | /* TRB_LEN | (TRB_INTR_TARGET) */ |
developer | 6cabb14 | 2020-09-08 19:00:00 +0200 | [diff] [blame] | 861 | trb_fields[2] = (TRB_LEN(8) | TRB_INTR_TARGET(0)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 862 | /* Immediate data in pointer */ |
| 863 | trb_fields[3] = field; |
| 864 | queue_trb(ctrl, ep_ring, true, trb_fields); |
| 865 | |
| 866 | /* Re-initializing field to zero */ |
| 867 | field = 0; |
| 868 | /* If there's data, queue data TRBs */ |
| 869 | /* Only set interrupt on short packet for IN endpoints */ |
| 870 | if (usb_pipein(pipe)) |
developer | 497dcfa | 2020-09-08 18:59:59 +0200 | [diff] [blame] | 871 | field = TRB_ISP | TRB_TYPE(TRB_DATA); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 872 | else |
developer | 497dcfa | 2020-09-08 18:59:59 +0200 | [diff] [blame] | 873 | field = TRB_TYPE(TRB_DATA); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 874 | |
developer | 570c2a9 | 2020-09-08 18:59:56 +0200 | [diff] [blame] | 875 | remainder = xhci_td_remainder(ctrl, 0, length, length, |
| 876 | usb_maxpacket(udev, pipe), true); |
developer | 6cabb14 | 2020-09-08 19:00:00 +0200 | [diff] [blame] | 877 | length_field = TRB_LEN(length) | TRB_TD_SIZE(remainder) | |
| 878 | TRB_INTR_TARGET(0); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 879 | debug("length_field = %d, length = %d," |
| 880 | "xhci_td_remainder(length) = %d , TRB_INTR_TARGET(0) = %d\n", |
developer | 6cabb14 | 2020-09-08 19:00:00 +0200 | [diff] [blame] | 881 | length_field, TRB_LEN(length), |
developer | 570c2a9 | 2020-09-08 18:59:56 +0200 | [diff] [blame] | 882 | TRB_TD_SIZE(remainder), 0); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 883 | |
| 884 | if (length > 0) { |
| 885 | if (req->requesttype & USB_DIR_IN) |
| 886 | field |= TRB_DIR_IN; |
Stefan Roese | caf8cae | 2020-07-21 10:46:05 +0200 | [diff] [blame] | 887 | buf_64 = virt_to_phys(buffer); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 888 | |
| 889 | trb_fields[0] = lower_32_bits(buf_64); |
| 890 | trb_fields[1] = upper_32_bits(buf_64); |
| 891 | trb_fields[2] = length_field; |
| 892 | trb_fields[3] = field | ep_ring->cycle_state; |
| 893 | |
Sergey Temerkhanov | 3859346 | 2015-04-01 17:18:45 +0300 | [diff] [blame] | 894 | xhci_flush_cache((uintptr_t)buffer, length); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 895 | queue_trb(ctrl, ep_ring, true, trb_fields); |
| 896 | } |
| 897 | |
| 898 | /* |
| 899 | * Queue status TRB - |
| 900 | * see Table 7 and sections 4.11.2.2 and 6.4.1.2.3 |
| 901 | */ |
| 902 | |
| 903 | /* If the device sent data, the status stage is an OUT transfer */ |
| 904 | field = 0; |
| 905 | if (length > 0 && req->requesttype & USB_DIR_IN) |
| 906 | field = 0; |
| 907 | else |
| 908 | field = TRB_DIR_IN; |
| 909 | |
| 910 | trb_fields[0] = 0; |
| 911 | trb_fields[1] = 0; |
developer | 6cabb14 | 2020-09-08 19:00:00 +0200 | [diff] [blame] | 912 | trb_fields[2] = TRB_INTR_TARGET(0); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 913 | /* Event on completion */ |
| 914 | trb_fields[3] = field | TRB_IOC | |
developer | 497dcfa | 2020-09-08 18:59:59 +0200 | [diff] [blame] | 915 | TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 916 | |
| 917 | queue_trb(ctrl, ep_ring, false, trb_fields); |
| 918 | |
| 919 | giveback_first_trb(udev, ep_index, start_cycle, start_trb); |
| 920 | |
| 921 | event = xhci_wait_for_event(ctrl, TRB_TRANSFER); |
| 922 | if (!event) |
| 923 | goto abort; |
| 924 | field = le32_to_cpu(event->trans_event.flags); |
| 925 | |
| 926 | BUG_ON(TRB_TO_SLOT_ID(field) != slot_id); |
| 927 | BUG_ON(TRB_TO_EP_INDEX(field) != ep_index); |
| 928 | |
| 929 | record_transfer_result(udev, event, length); |
| 930 | xhci_acknowledge_event(ctrl); |
| 931 | |
| 932 | /* Invalidate buffer to make it available to usb-core */ |
| 933 | if (length > 0) |
Sergey Temerkhanov | 3859346 | 2015-04-01 17:18:45 +0300 | [diff] [blame] | 934 | xhci_inval_cache((uintptr_t)buffer, length); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 935 | |
| 936 | if (GET_COMP_CODE(le32_to_cpu(event->trans_event.transfer_len)) |
| 937 | == COMP_SHORT_TX) { |
| 938 | /* Short data stage, clear up additional status stage event */ |
| 939 | event = xhci_wait_for_event(ctrl, TRB_TRANSFER); |
| 940 | if (!event) |
| 941 | goto abort; |
| 942 | BUG_ON(TRB_TO_SLOT_ID(field) != slot_id); |
| 943 | BUG_ON(TRB_TO_EP_INDEX(field) != ep_index); |
| 944 | xhci_acknowledge_event(ctrl); |
| 945 | } |
| 946 | |
| 947 | return (udev->status != USB_ST_NOT_PROC) ? 0 : -1; |
| 948 | |
| 949 | abort: |
| 950 | debug("XHCI control transfer timed out, aborting...\n"); |
| 951 | abort_td(udev, ep_index); |
| 952 | udev->status = USB_ST_NAK_REC; |
| 953 | udev->act_len = 0; |
| 954 | return -ETIMEDOUT; |
| 955 | } |