15853e133SVivek Gautam /* 25853e133SVivek Gautam * USB HOST XHCI Controller stack 35853e133SVivek Gautam * 45853e133SVivek Gautam * Based on xHCI host controller driver in linux-kernel 55853e133SVivek Gautam * by Sarah Sharp. 65853e133SVivek Gautam * 75853e133SVivek Gautam * Copyright (C) 2008 Intel Corp. 85853e133SVivek Gautam * Author: Sarah Sharp 95853e133SVivek Gautam * 105853e133SVivek Gautam * Copyright (C) 2013 Samsung Electronics Co.Ltd 115853e133SVivek Gautam * Authors: Vivek Gautam <gautam.vivek@samsung.com> 125853e133SVivek Gautam * Vikas Sajjan <vikas.sajjan@samsung.com> 135853e133SVivek Gautam * 145853e133SVivek Gautam * SPDX-License-Identifier: GPL-2.0+ 155853e133SVivek Gautam */ 165853e133SVivek Gautam 175853e133SVivek Gautam #include <common.h> 185853e133SVivek Gautam #include <asm/byteorder.h> 195853e133SVivek Gautam #include <usb.h> 205853e133SVivek Gautam #include <asm/unaligned.h> 215853e133SVivek Gautam #include <asm-generic/errno.h> 225853e133SVivek Gautam 235853e133SVivek Gautam #include "xhci.h" 245853e133SVivek Gautam 255853e133SVivek Gautam /** 265853e133SVivek Gautam * Is this TRB a link TRB or was the last TRB the last TRB in this event ring 275853e133SVivek Gautam * segment? I.e. would the updated event TRB pointer step off the end of the 285853e133SVivek Gautam * event seg ? 295853e133SVivek Gautam * 305853e133SVivek Gautam * @param ctrl Host controller data structure 315853e133SVivek Gautam * @param ring pointer to the ring 325853e133SVivek Gautam * @param seg poniter to the segment to which TRB belongs 335853e133SVivek Gautam * @param trb poniter to the ring trb 345853e133SVivek Gautam * @return 1 if this TRB a link TRB else 0 355853e133SVivek Gautam */ 365853e133SVivek Gautam static int last_trb(struct xhci_ctrl *ctrl, struct xhci_ring *ring, 375853e133SVivek Gautam struct xhci_segment *seg, union xhci_trb *trb) 385853e133SVivek Gautam { 395853e133SVivek Gautam if (ring == ctrl->event_ring) 405853e133SVivek Gautam return trb == &seg->trbs[TRBS_PER_SEGMENT]; 415853e133SVivek Gautam else 425853e133SVivek Gautam return TRB_TYPE_LINK_LE32(trb->link.control); 435853e133SVivek Gautam } 445853e133SVivek Gautam 455853e133SVivek Gautam /** 465853e133SVivek Gautam * Does this link TRB point to the first segment in a ring, 475853e133SVivek Gautam * or was the previous TRB the last TRB on the last segment in the ERST? 485853e133SVivek Gautam * 495853e133SVivek Gautam * @param ctrl Host controller data structure 505853e133SVivek Gautam * @param ring pointer to the ring 515853e133SVivek Gautam * @param seg poniter to the segment to which TRB belongs 525853e133SVivek Gautam * @param trb poniter to the ring trb 535853e133SVivek Gautam * @return 1 if this TRB is the last TRB on the last segment else 0 545853e133SVivek Gautam */ 555853e133SVivek Gautam static bool last_trb_on_last_seg(struct xhci_ctrl *ctrl, 565853e133SVivek Gautam struct xhci_ring *ring, 575853e133SVivek Gautam struct xhci_segment *seg, 585853e133SVivek Gautam union xhci_trb *trb) 595853e133SVivek Gautam { 605853e133SVivek Gautam if (ring == ctrl->event_ring) 615853e133SVivek Gautam return ((trb == &seg->trbs[TRBS_PER_SEGMENT]) && 625853e133SVivek Gautam (seg->next == ring->first_seg)); 635853e133SVivek Gautam else 645853e133SVivek Gautam return le32_to_cpu(trb->link.control) & LINK_TOGGLE; 655853e133SVivek Gautam } 665853e133SVivek Gautam 675853e133SVivek Gautam /** 685853e133SVivek Gautam * See Cycle bit rules. SW is the consumer for the event ring only. 695853e133SVivek Gautam * Don't make a ring full of link TRBs. That would be dumb and this would loop. 705853e133SVivek Gautam * 715853e133SVivek Gautam * If we've just enqueued a TRB that is in the middle of a TD (meaning the 725853e133SVivek Gautam * chain bit is set), then set the chain bit in all the following link TRBs. 735853e133SVivek Gautam * If we've enqueued the last TRB in a TD, make sure the following link TRBs 745853e133SVivek Gautam * have their chain bit cleared (so that each Link TRB is a separate TD). 755853e133SVivek Gautam * 765853e133SVivek Gautam * Section 6.4.4.1 of the 0.95 spec says link TRBs cannot have the chain bit 775853e133SVivek Gautam * set, but other sections talk about dealing with the chain bit set. This was 785853e133SVivek Gautam * fixed in the 0.96 specification errata, but we have to assume that all 0.95 795853e133SVivek Gautam * xHCI hardware can't handle the chain bit being cleared on a link TRB. 805853e133SVivek Gautam * 815853e133SVivek Gautam * @param ctrl Host controller data structure 825853e133SVivek Gautam * @param ring pointer to the ring 835853e133SVivek Gautam * @param more_trbs_coming flag to indicate whether more trbs 845853e133SVivek Gautam * are expected or NOT. 855853e133SVivek Gautam * Will you enqueue more TRBs before calling 865853e133SVivek Gautam * prepare_ring()? 875853e133SVivek Gautam * @return none 885853e133SVivek Gautam */ 895853e133SVivek Gautam static void inc_enq(struct xhci_ctrl *ctrl, struct xhci_ring *ring, 905853e133SVivek Gautam bool more_trbs_coming) 915853e133SVivek Gautam { 925853e133SVivek Gautam u32 chain; 935853e133SVivek Gautam union xhci_trb *next; 945853e133SVivek Gautam 955853e133SVivek Gautam chain = le32_to_cpu(ring->enqueue->generic.field[3]) & TRB_CHAIN; 965853e133SVivek Gautam next = ++(ring->enqueue); 975853e133SVivek Gautam 985853e133SVivek Gautam /* 995853e133SVivek Gautam * Update the dequeue pointer further if that was a link TRB or we're at 1005853e133SVivek Gautam * the end of an event ring segment (which doesn't have link TRBS) 1015853e133SVivek Gautam */ 1025853e133SVivek Gautam while (last_trb(ctrl, ring, ring->enq_seg, next)) { 1035853e133SVivek Gautam if (ring != ctrl->event_ring) { 1045853e133SVivek Gautam /* 1055853e133SVivek Gautam * If the caller doesn't plan on enqueueing more 1065853e133SVivek Gautam * TDs before ringing the doorbell, then we 1075853e133SVivek Gautam * don't want to give the link TRB to the 1085853e133SVivek Gautam * hardware just yet. We'll give the link TRB 1095853e133SVivek Gautam * back in prepare_ring() just before we enqueue 1105853e133SVivek Gautam * the TD at the top of the ring. 1115853e133SVivek Gautam */ 1125853e133SVivek Gautam if (!chain && !more_trbs_coming) 1135853e133SVivek Gautam break; 1145853e133SVivek Gautam 1155853e133SVivek Gautam /* 1165853e133SVivek Gautam * If we're not dealing with 0.95 hardware or 1175853e133SVivek Gautam * isoc rings on AMD 0.96 host, 1185853e133SVivek Gautam * carry over the chain bit of the previous TRB 1195853e133SVivek Gautam * (which may mean the chain bit is cleared). 1205853e133SVivek Gautam */ 1215853e133SVivek Gautam next->link.control &= cpu_to_le32(~TRB_CHAIN); 1225853e133SVivek Gautam next->link.control |= cpu_to_le32(chain); 1235853e133SVivek Gautam 1245853e133SVivek Gautam next->link.control ^= cpu_to_le32(TRB_CYCLE); 125421a5a0cSSergey Temerkhanov xhci_flush_cache((uintptr_t)next, 1265853e133SVivek Gautam sizeof(union xhci_trb)); 1275853e133SVivek Gautam } 1285853e133SVivek Gautam /* Toggle the cycle bit after the last ring segment. */ 1295853e133SVivek Gautam if (last_trb_on_last_seg(ctrl, ring, 1305853e133SVivek Gautam ring->enq_seg, next)) 1315853e133SVivek Gautam ring->cycle_state = (ring->cycle_state ? 0 : 1); 1325853e133SVivek Gautam 1335853e133SVivek Gautam ring->enq_seg = ring->enq_seg->next; 1345853e133SVivek Gautam ring->enqueue = ring->enq_seg->trbs; 1355853e133SVivek Gautam next = ring->enqueue; 1365853e133SVivek Gautam } 1375853e133SVivek Gautam } 1385853e133SVivek Gautam 1395853e133SVivek Gautam /** 1405853e133SVivek Gautam * See Cycle bit rules. SW is the consumer for the event ring only. 1415853e133SVivek Gautam * Don't make a ring full of link TRBs. That would be dumb and this would loop. 1425853e133SVivek Gautam * 1435853e133SVivek Gautam * @param ctrl Host controller data structure 1445853e133SVivek Gautam * @param ring Ring whose Dequeue TRB pointer needs to be incremented. 1455853e133SVivek Gautam * return none 1465853e133SVivek Gautam */ 1475853e133SVivek Gautam static void inc_deq(struct xhci_ctrl *ctrl, struct xhci_ring *ring) 1485853e133SVivek Gautam { 1495853e133SVivek Gautam do { 1505853e133SVivek Gautam /* 1515853e133SVivek Gautam * Update the dequeue pointer further if that was a link TRB or 1525853e133SVivek Gautam * we're at the end of an event ring segment (which doesn't have 1535853e133SVivek Gautam * link TRBS) 1545853e133SVivek Gautam */ 1555853e133SVivek Gautam if (last_trb(ctrl, ring, ring->deq_seg, ring->dequeue)) { 1565853e133SVivek Gautam if (ring == ctrl->event_ring && 1575853e133SVivek Gautam last_trb_on_last_seg(ctrl, ring, 1585853e133SVivek Gautam ring->deq_seg, ring->dequeue)) { 1595853e133SVivek Gautam ring->cycle_state = (ring->cycle_state ? 0 : 1); 1605853e133SVivek Gautam } 1615853e133SVivek Gautam ring->deq_seg = ring->deq_seg->next; 1625853e133SVivek Gautam ring->dequeue = ring->deq_seg->trbs; 1635853e133SVivek Gautam } else { 1645853e133SVivek Gautam ring->dequeue++; 1655853e133SVivek Gautam } 1665853e133SVivek Gautam } while (last_trb(ctrl, ring, ring->deq_seg, ring->dequeue)); 1675853e133SVivek Gautam } 1685853e133SVivek Gautam 1695853e133SVivek Gautam /** 1705853e133SVivek Gautam * Generic function for queueing a TRB on a ring. 1715853e133SVivek Gautam * The caller must have checked to make sure there's room on the ring. 1725853e133SVivek Gautam * 1735853e133SVivek Gautam * @param more_trbs_coming: Will you enqueue more TRBs before calling 1745853e133SVivek Gautam * prepare_ring()? 1755853e133SVivek Gautam * @param ctrl Host controller data structure 1765853e133SVivek Gautam * @param ring pointer to the ring 1775853e133SVivek Gautam * @param more_trbs_coming flag to indicate whether more trbs 1785853e133SVivek Gautam * @param trb_fields pointer to trb field array containing TRB contents 1795853e133SVivek Gautam * @return pointer to the enqueued trb 1805853e133SVivek Gautam */ 1815853e133SVivek Gautam static struct xhci_generic_trb *queue_trb(struct xhci_ctrl *ctrl, 1825853e133SVivek Gautam struct xhci_ring *ring, 1835853e133SVivek Gautam bool more_trbs_coming, 1845853e133SVivek Gautam unsigned int *trb_fields) 1855853e133SVivek Gautam { 1865853e133SVivek Gautam struct xhci_generic_trb *trb; 1875853e133SVivek Gautam int i; 1885853e133SVivek Gautam 1895853e133SVivek Gautam trb = &ring->enqueue->generic; 1905853e133SVivek Gautam 1915853e133SVivek Gautam for (i = 0; i < 4; i++) 1925853e133SVivek Gautam trb->field[i] = cpu_to_le32(trb_fields[i]); 1935853e133SVivek Gautam 194421a5a0cSSergey Temerkhanov xhci_flush_cache((uintptr_t)trb, sizeof(struct xhci_generic_trb)); 1955853e133SVivek Gautam 1965853e133SVivek Gautam inc_enq(ctrl, ring, more_trbs_coming); 1975853e133SVivek Gautam 1985853e133SVivek Gautam return trb; 1995853e133SVivek Gautam } 2005853e133SVivek Gautam 2015853e133SVivek Gautam /** 2025853e133SVivek Gautam * Does various checks on the endpoint ring, and makes it ready 2035853e133SVivek Gautam * to queue num_trbs. 2045853e133SVivek Gautam * 2055853e133SVivek Gautam * @param ctrl Host controller data structure 2065853e133SVivek Gautam * @param ep_ring pointer to the EP Transfer Ring 2075853e133SVivek Gautam * @param ep_state State of the End Point 2085853e133SVivek Gautam * @return error code in case of invalid ep_state, 0 on success 2095853e133SVivek Gautam */ 2105853e133SVivek Gautam static int prepare_ring(struct xhci_ctrl *ctrl, struct xhci_ring *ep_ring, 2115853e133SVivek Gautam u32 ep_state) 2125853e133SVivek Gautam { 2135853e133SVivek Gautam union xhci_trb *next = ep_ring->enqueue; 2145853e133SVivek Gautam 2155853e133SVivek Gautam /* Make sure the endpoint has been added to xHC schedule */ 2165853e133SVivek Gautam switch (ep_state) { 2175853e133SVivek Gautam case EP_STATE_DISABLED: 2185853e133SVivek Gautam /* 2195853e133SVivek Gautam * USB core changed config/interfaces without notifying us, 2205853e133SVivek Gautam * or hardware is reporting the wrong state. 2215853e133SVivek Gautam */ 2225853e133SVivek Gautam puts("WARN urb submitted to disabled ep\n"); 2235853e133SVivek Gautam return -ENOENT; 2245853e133SVivek Gautam case EP_STATE_ERROR: 2255853e133SVivek Gautam puts("WARN waiting for error on ep to be cleared\n"); 2265853e133SVivek Gautam return -EINVAL; 2275853e133SVivek Gautam case EP_STATE_HALTED: 2285853e133SVivek Gautam puts("WARN halted endpoint, queueing URB anyway.\n"); 2295853e133SVivek Gautam case EP_STATE_STOPPED: 2305853e133SVivek Gautam case EP_STATE_RUNNING: 2315853e133SVivek Gautam debug("EP STATE RUNNING.\n"); 2325853e133SVivek Gautam break; 2335853e133SVivek Gautam default: 2345853e133SVivek Gautam puts("ERROR unknown endpoint state for ep\n"); 2355853e133SVivek Gautam return -EINVAL; 2365853e133SVivek Gautam } 2375853e133SVivek Gautam 2385853e133SVivek Gautam while (last_trb(ctrl, ep_ring, ep_ring->enq_seg, next)) { 2395853e133SVivek Gautam /* 2405853e133SVivek Gautam * If we're not dealing with 0.95 hardware or isoc rings 2415853e133SVivek Gautam * on AMD 0.96 host, clear the chain bit. 2425853e133SVivek Gautam */ 2435853e133SVivek Gautam next->link.control &= cpu_to_le32(~TRB_CHAIN); 2445853e133SVivek Gautam 2455853e133SVivek Gautam next->link.control ^= cpu_to_le32(TRB_CYCLE); 2465853e133SVivek Gautam 247421a5a0cSSergey Temerkhanov xhci_flush_cache((uintptr_t)next, sizeof(union xhci_trb)); 2485853e133SVivek Gautam 2495853e133SVivek Gautam /* Toggle the cycle bit after the last ring segment. */ 2505853e133SVivek Gautam if (last_trb_on_last_seg(ctrl, ep_ring, 2515853e133SVivek Gautam ep_ring->enq_seg, next)) 2525853e133SVivek Gautam ep_ring->cycle_state = (ep_ring->cycle_state ? 0 : 1); 2535853e133SVivek Gautam ep_ring->enq_seg = ep_ring->enq_seg->next; 2545853e133SVivek Gautam ep_ring->enqueue = ep_ring->enq_seg->trbs; 2555853e133SVivek Gautam next = ep_ring->enqueue; 2565853e133SVivek Gautam } 2575853e133SVivek Gautam 2585853e133SVivek Gautam return 0; 2595853e133SVivek Gautam } 2605853e133SVivek Gautam 2615853e133SVivek Gautam /** 2625853e133SVivek Gautam * Generic function for queueing a command TRB on the command ring. 2635853e133SVivek Gautam * Check to make sure there's room on the command ring for one command TRB. 2645853e133SVivek Gautam * 2655853e133SVivek Gautam * @param ctrl Host controller data structure 2665853e133SVivek Gautam * @param ptr Pointer address to write in the first two fields (opt.) 2675853e133SVivek Gautam * @param slot_id Slot ID to encode in the flags field (opt.) 2685853e133SVivek Gautam * @param ep_index Endpoint index to encode in the flags field (opt.) 2695853e133SVivek Gautam * @param cmd Command type to enqueue 2705853e133SVivek Gautam * @return none 2715853e133SVivek Gautam */ 2725853e133SVivek Gautam void xhci_queue_command(struct xhci_ctrl *ctrl, u8 *ptr, u32 slot_id, 2735853e133SVivek Gautam u32 ep_index, trb_type cmd) 2745853e133SVivek Gautam { 2755853e133SVivek Gautam u32 fields[4]; 2765853e133SVivek Gautam u64 val_64 = (uintptr_t)ptr; 2775853e133SVivek Gautam 2785853e133SVivek Gautam BUG_ON(prepare_ring(ctrl, ctrl->cmd_ring, EP_STATE_RUNNING)); 2795853e133SVivek Gautam 2805853e133SVivek Gautam fields[0] = lower_32_bits(val_64); 2815853e133SVivek Gautam fields[1] = upper_32_bits(val_64); 2825853e133SVivek Gautam fields[2] = 0; 2835853e133SVivek Gautam fields[3] = TRB_TYPE(cmd) | EP_ID_FOR_TRB(ep_index) | 2845853e133SVivek Gautam SLOT_ID_FOR_TRB(slot_id) | ctrl->cmd_ring->cycle_state; 2855853e133SVivek Gautam 2865853e133SVivek Gautam queue_trb(ctrl, ctrl->cmd_ring, false, fields); 2875853e133SVivek Gautam 2885853e133SVivek Gautam /* Ring the command ring doorbell */ 2895853e133SVivek Gautam xhci_writel(&ctrl->dba->doorbell[0], DB_VALUE_HOST); 2905853e133SVivek Gautam } 2915853e133SVivek Gautam 2925853e133SVivek Gautam /** 2935853e133SVivek Gautam * The TD size is the number of bytes remaining in the TD (including this TRB), 2945853e133SVivek Gautam * right shifted by 10. 2955853e133SVivek Gautam * It must fit in bits 21:17, so it can't be bigger than 31. 2965853e133SVivek Gautam * 2975853e133SVivek Gautam * @param remainder remaining packets to be sent 2985853e133SVivek Gautam * @return remainder if remainder is less than max else max 2995853e133SVivek Gautam */ 3005853e133SVivek Gautam static u32 xhci_td_remainder(unsigned int remainder) 3015853e133SVivek Gautam { 3025853e133SVivek Gautam u32 max = (1 << (21 - 17 + 1)) - 1; 3035853e133SVivek Gautam 3045853e133SVivek Gautam if ((remainder >> 10) >= max) 3055853e133SVivek Gautam return max << 17; 3065853e133SVivek Gautam else 3075853e133SVivek Gautam return (remainder >> 10) << 17; 3085853e133SVivek Gautam } 3095853e133SVivek Gautam 3105853e133SVivek Gautam /** 3115853e133SVivek Gautam * Finds out the remanining packets to be sent 3125853e133SVivek Gautam * 3135853e133SVivek Gautam * @param running_total total size sent so far 3145853e133SVivek Gautam * @param trb_buff_len length of the TRB Buffer 3155853e133SVivek Gautam * @param total_packet_count total packet count 3165853e133SVivek Gautam * @param maxpacketsize max packet size of current pipe 3175853e133SVivek Gautam * @param num_trbs_left number of TRBs left to be processed 3185853e133SVivek Gautam * @return 0 if running_total or trb_buff_len is 0, else remainder 3195853e133SVivek Gautam */ 3205853e133SVivek Gautam static u32 xhci_v1_0_td_remainder(int running_total, 3215853e133SVivek Gautam int trb_buff_len, 3225853e133SVivek Gautam unsigned int total_packet_count, 3235853e133SVivek Gautam int maxpacketsize, 3245853e133SVivek Gautam unsigned int num_trbs_left) 3255853e133SVivek Gautam { 3265853e133SVivek Gautam int packets_transferred; 3275853e133SVivek Gautam 3285853e133SVivek Gautam /* One TRB with a zero-length data packet. */ 3295853e133SVivek Gautam if (num_trbs_left == 0 || (running_total == 0 && trb_buff_len == 0)) 3305853e133SVivek Gautam return 0; 3315853e133SVivek Gautam 3325853e133SVivek Gautam /* 3335853e133SVivek Gautam * All the TRB queueing functions don't count the current TRB in 3345853e133SVivek Gautam * running_total. 3355853e133SVivek Gautam */ 3365853e133SVivek Gautam packets_transferred = (running_total + trb_buff_len) / maxpacketsize; 3375853e133SVivek Gautam 3385853e133SVivek Gautam if ((total_packet_count - packets_transferred) > 31) 3395853e133SVivek Gautam return 31 << 17; 3405853e133SVivek Gautam return (total_packet_count - packets_transferred) << 17; 3415853e133SVivek Gautam } 3425853e133SVivek Gautam 3435853e133SVivek Gautam /** 3445853e133SVivek Gautam * Ring the doorbell of the End Point 3455853e133SVivek Gautam * 3465853e133SVivek Gautam * @param udev pointer to the USB device structure 3475853e133SVivek Gautam * @param ep_index index of the endpoint 3485853e133SVivek Gautam * @param start_cycle cycle flag of the first TRB 3495853e133SVivek Gautam * @param start_trb pionter to the first TRB 3505853e133SVivek Gautam * @return none 3515853e133SVivek Gautam */ 3525853e133SVivek Gautam static void giveback_first_trb(struct usb_device *udev, int ep_index, 3535853e133SVivek Gautam int start_cycle, 3545853e133SVivek Gautam struct xhci_generic_trb *start_trb) 3555853e133SVivek Gautam { 356*7c1deec0SSimon Glass struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); 3575853e133SVivek Gautam 3585853e133SVivek Gautam /* 3595853e133SVivek Gautam * Pass all the TRBs to the hardware at once and make sure this write 3605853e133SVivek Gautam * isn't reordered. 3615853e133SVivek Gautam */ 3625853e133SVivek Gautam if (start_cycle) 3635853e133SVivek Gautam start_trb->field[3] |= cpu_to_le32(start_cycle); 3645853e133SVivek Gautam else 3655853e133SVivek Gautam start_trb->field[3] &= cpu_to_le32(~TRB_CYCLE); 3665853e133SVivek Gautam 367421a5a0cSSergey Temerkhanov xhci_flush_cache((uintptr_t)start_trb, sizeof(struct xhci_generic_trb)); 3685853e133SVivek Gautam 3695853e133SVivek Gautam /* Ringing EP doorbell here */ 3705853e133SVivek Gautam xhci_writel(&ctrl->dba->doorbell[udev->slot_id], 3715853e133SVivek Gautam DB_VALUE(ep_index, 0)); 3725853e133SVivek Gautam 3735853e133SVivek Gautam return; 3745853e133SVivek Gautam } 3755853e133SVivek Gautam 3765853e133SVivek Gautam /**** POLLING mechanism for XHCI ****/ 3775853e133SVivek Gautam 3785853e133SVivek Gautam /** 3795853e133SVivek Gautam * Finalizes a handled event TRB by advancing our dequeue pointer and giving 3805853e133SVivek Gautam * the TRB back to the hardware for recycling. Must call this exactly once at 3815853e133SVivek Gautam * the end of each event handler, and not touch the TRB again afterwards. 3825853e133SVivek Gautam * 3835853e133SVivek Gautam * @param ctrl Host controller data structure 3845853e133SVivek Gautam * @return none 3855853e133SVivek Gautam */ 3865853e133SVivek Gautam void xhci_acknowledge_event(struct xhci_ctrl *ctrl) 3875853e133SVivek Gautam { 3885853e133SVivek Gautam /* Advance our dequeue pointer to the next event */ 3895853e133SVivek Gautam inc_deq(ctrl, ctrl->event_ring); 3905853e133SVivek Gautam 3915853e133SVivek Gautam /* Inform the hardware */ 3925853e133SVivek Gautam xhci_writeq(&ctrl->ir_set->erst_dequeue, 3935853e133SVivek Gautam (uintptr_t)ctrl->event_ring->dequeue | ERST_EHB); 3945853e133SVivek Gautam } 3955853e133SVivek Gautam 3965853e133SVivek Gautam /** 3975853e133SVivek Gautam * Checks if there is a new event to handle on the event ring. 3985853e133SVivek Gautam * 3995853e133SVivek Gautam * @param ctrl Host controller data structure 4005853e133SVivek Gautam * @return 0 if failure else 1 on success 4015853e133SVivek Gautam */ 4025853e133SVivek Gautam static int event_ready(struct xhci_ctrl *ctrl) 4035853e133SVivek Gautam { 4045853e133SVivek Gautam union xhci_trb *event; 4055853e133SVivek Gautam 406421a5a0cSSergey Temerkhanov xhci_inval_cache((uintptr_t)ctrl->event_ring->dequeue, 4075853e133SVivek Gautam sizeof(union xhci_trb)); 4085853e133SVivek Gautam 4095853e133SVivek Gautam event = ctrl->event_ring->dequeue; 4105853e133SVivek Gautam 4115853e133SVivek Gautam /* Does the HC or OS own the TRB? */ 4125853e133SVivek Gautam if ((le32_to_cpu(event->event_cmd.flags) & TRB_CYCLE) != 4135853e133SVivek Gautam ctrl->event_ring->cycle_state) 4145853e133SVivek Gautam return 0; 4155853e133SVivek Gautam 4165853e133SVivek Gautam return 1; 4175853e133SVivek Gautam } 4185853e133SVivek Gautam 4195853e133SVivek Gautam /** 4205853e133SVivek Gautam * Waits for a specific type of event and returns it. Discards unexpected 4215853e133SVivek Gautam * events. Caller *must* call xhci_acknowledge_event() after it is finished 4225853e133SVivek Gautam * processing the event, and must not access the returned pointer afterwards. 4235853e133SVivek Gautam * 4245853e133SVivek Gautam * @param ctrl Host controller data structure 4255853e133SVivek Gautam * @param expected TRB type expected from Event TRB 4265853e133SVivek Gautam * @return pointer to event trb 4275853e133SVivek Gautam */ 4285853e133SVivek Gautam union xhci_trb *xhci_wait_for_event(struct xhci_ctrl *ctrl, trb_type expected) 4295853e133SVivek Gautam { 4305853e133SVivek Gautam trb_type type; 4315853e133SVivek Gautam unsigned long ts = get_timer(0); 4325853e133SVivek Gautam 4335853e133SVivek Gautam do { 4345853e133SVivek Gautam union xhci_trb *event = ctrl->event_ring->dequeue; 4355853e133SVivek Gautam 4365853e133SVivek Gautam if (!event_ready(ctrl)) 4375853e133SVivek Gautam continue; 4385853e133SVivek Gautam 4395853e133SVivek Gautam type = TRB_FIELD_TO_TYPE(le32_to_cpu(event->event_cmd.flags)); 4405853e133SVivek Gautam if (type == expected) 4415853e133SVivek Gautam return event; 4425853e133SVivek Gautam 4435853e133SVivek Gautam if (type == TRB_PORT_STATUS) 4445853e133SVivek Gautam /* TODO: remove this once enumeration has been reworked */ 4455853e133SVivek Gautam /* 4465853e133SVivek Gautam * Port status change events always have a 4475853e133SVivek Gautam * successful completion code 4485853e133SVivek Gautam */ 4495853e133SVivek Gautam BUG_ON(GET_COMP_CODE( 4505853e133SVivek Gautam le32_to_cpu(event->generic.field[2])) != 4515853e133SVivek Gautam COMP_SUCCESS); 4525853e133SVivek Gautam else 4535853e133SVivek Gautam printf("Unexpected XHCI event TRB, skipping... " 4545853e133SVivek Gautam "(%08x %08x %08x %08x)\n", 4555853e133SVivek Gautam le32_to_cpu(event->generic.field[0]), 4565853e133SVivek Gautam le32_to_cpu(event->generic.field[1]), 4575853e133SVivek Gautam le32_to_cpu(event->generic.field[2]), 4585853e133SVivek Gautam le32_to_cpu(event->generic.field[3])); 4595853e133SVivek Gautam 4605853e133SVivek Gautam xhci_acknowledge_event(ctrl); 4615853e133SVivek Gautam } while (get_timer(ts) < XHCI_TIMEOUT); 4625853e133SVivek Gautam 4635853e133SVivek Gautam if (expected == TRB_TRANSFER) 4645853e133SVivek Gautam return NULL; 4655853e133SVivek Gautam 4665853e133SVivek Gautam printf("XHCI timeout on event type %d... cannot recover.\n", expected); 4675853e133SVivek Gautam BUG(); 4685853e133SVivek Gautam } 4695853e133SVivek Gautam 4705853e133SVivek Gautam /* 4715853e133SVivek Gautam * Stops transfer processing for an endpoint and throws away all unprocessed 4725853e133SVivek Gautam * TRBs by setting the xHC's dequeue pointer to our enqueue pointer. The next 4735853e133SVivek Gautam * xhci_bulk_tx/xhci_ctrl_tx on this enpoint will add new transfers there and 4745853e133SVivek Gautam * ring the doorbell, causing this endpoint to start working again. 4755853e133SVivek Gautam * (Careful: This will BUG() when there was no transfer in progress. Shouldn't 4765853e133SVivek Gautam * happen in practice for current uses and is too complicated to fix right now.) 4775853e133SVivek Gautam */ 4785853e133SVivek Gautam static void abort_td(struct usb_device *udev, int ep_index) 4795853e133SVivek Gautam { 480*7c1deec0SSimon Glass struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); 4815853e133SVivek Gautam struct xhci_ring *ring = ctrl->devs[udev->slot_id]->eps[ep_index].ring; 4825853e133SVivek Gautam union xhci_trb *event; 4835853e133SVivek Gautam u32 field; 4845853e133SVivek Gautam 4855853e133SVivek Gautam xhci_queue_command(ctrl, NULL, udev->slot_id, ep_index, TRB_STOP_RING); 4865853e133SVivek Gautam 4875853e133SVivek Gautam event = xhci_wait_for_event(ctrl, TRB_TRANSFER); 4885853e133SVivek Gautam field = le32_to_cpu(event->trans_event.flags); 4895853e133SVivek Gautam BUG_ON(TRB_TO_SLOT_ID(field) != udev->slot_id); 4905853e133SVivek Gautam BUG_ON(TRB_TO_EP_INDEX(field) != ep_index); 4915853e133SVivek Gautam BUG_ON(GET_COMP_CODE(le32_to_cpu(event->trans_event.transfer_len 4925853e133SVivek Gautam != COMP_STOP))); 4935853e133SVivek Gautam xhci_acknowledge_event(ctrl); 4945853e133SVivek Gautam 4955853e133SVivek Gautam event = xhci_wait_for_event(ctrl, TRB_COMPLETION); 4965853e133SVivek Gautam BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags)) 4975853e133SVivek Gautam != udev->slot_id || GET_COMP_CODE(le32_to_cpu( 4985853e133SVivek Gautam event->event_cmd.status)) != COMP_SUCCESS); 4995853e133SVivek Gautam xhci_acknowledge_event(ctrl); 5005853e133SVivek Gautam 5015853e133SVivek Gautam xhci_queue_command(ctrl, (void *)((uintptr_t)ring->enqueue | 5025853e133SVivek Gautam ring->cycle_state), udev->slot_id, ep_index, TRB_SET_DEQ); 5035853e133SVivek Gautam event = xhci_wait_for_event(ctrl, TRB_COMPLETION); 5045853e133SVivek Gautam BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags)) 5055853e133SVivek Gautam != udev->slot_id || GET_COMP_CODE(le32_to_cpu( 5065853e133SVivek Gautam event->event_cmd.status)) != COMP_SUCCESS); 5075853e133SVivek Gautam xhci_acknowledge_event(ctrl); 5085853e133SVivek Gautam } 5095853e133SVivek Gautam 5105853e133SVivek Gautam static void record_transfer_result(struct usb_device *udev, 5115853e133SVivek Gautam union xhci_trb *event, int length) 5125853e133SVivek Gautam { 5135853e133SVivek Gautam udev->act_len = min(length, length - 514b4141195SMasahiro Yamada (int)EVENT_TRB_LEN(le32_to_cpu(event->trans_event.transfer_len))); 5155853e133SVivek Gautam 5165853e133SVivek Gautam switch (GET_COMP_CODE(le32_to_cpu(event->trans_event.transfer_len))) { 5175853e133SVivek Gautam case COMP_SUCCESS: 5185853e133SVivek Gautam BUG_ON(udev->act_len != length); 5195853e133SVivek Gautam /* fallthrough */ 5205853e133SVivek Gautam case COMP_SHORT_TX: 5215853e133SVivek Gautam udev->status = 0; 5225853e133SVivek Gautam break; 5235853e133SVivek Gautam case COMP_STALL: 5245853e133SVivek Gautam udev->status = USB_ST_STALLED; 5255853e133SVivek Gautam break; 5265853e133SVivek Gautam case COMP_DB_ERR: 5275853e133SVivek Gautam case COMP_TRB_ERR: 5285853e133SVivek Gautam udev->status = USB_ST_BUF_ERR; 5295853e133SVivek Gautam break; 5305853e133SVivek Gautam case COMP_BABBLE: 5315853e133SVivek Gautam udev->status = USB_ST_BABBLE_DET; 5325853e133SVivek Gautam break; 5335853e133SVivek Gautam default: 5345853e133SVivek Gautam udev->status = 0x80; /* USB_ST_TOO_LAZY_TO_MAKE_A_NEW_MACRO */ 5355853e133SVivek Gautam } 5365853e133SVivek Gautam } 5375853e133SVivek Gautam 5385853e133SVivek Gautam /**** Bulk and Control transfer methods ****/ 5395853e133SVivek Gautam /** 5405853e133SVivek Gautam * Queues up the BULK Request 5415853e133SVivek Gautam * 5425853e133SVivek Gautam * @param udev pointer to the USB device structure 5435853e133SVivek Gautam * @param pipe contains the DIR_IN or OUT , devnum 5445853e133SVivek Gautam * @param length length of the buffer 5455853e133SVivek Gautam * @param buffer buffer to be read/written based on the request 5465853e133SVivek Gautam * @return returns 0 if successful else -1 on failure 5475853e133SVivek Gautam */ 5485853e133SVivek Gautam int xhci_bulk_tx(struct usb_device *udev, unsigned long pipe, 5495853e133SVivek Gautam int length, void *buffer) 5505853e133SVivek Gautam { 5515853e133SVivek Gautam int num_trbs = 0; 5525853e133SVivek Gautam struct xhci_generic_trb *start_trb; 5535853e133SVivek Gautam bool first_trb = 0; 5545853e133SVivek Gautam int start_cycle; 5555853e133SVivek Gautam u32 field = 0; 5565853e133SVivek Gautam u32 length_field = 0; 557*7c1deec0SSimon Glass struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); 5585853e133SVivek Gautam int slot_id = udev->slot_id; 5595853e133SVivek Gautam int ep_index; 5605853e133SVivek Gautam struct xhci_virt_device *virt_dev; 5615853e133SVivek Gautam struct xhci_ep_ctx *ep_ctx; 5625853e133SVivek Gautam struct xhci_ring *ring; /* EP transfer ring */ 5635853e133SVivek Gautam union xhci_trb *event; 5645853e133SVivek Gautam 5655853e133SVivek Gautam int running_total, trb_buff_len; 5665853e133SVivek Gautam unsigned int total_packet_count; 5675853e133SVivek Gautam int maxpacketsize; 5685853e133SVivek Gautam u64 addr; 5695853e133SVivek Gautam int ret; 5705853e133SVivek Gautam u32 trb_fields[4]; 5715853e133SVivek Gautam u64 val_64 = (uintptr_t)buffer; 5725853e133SVivek Gautam 5735853e133SVivek Gautam debug("dev=%p, pipe=%lx, buffer=%p, length=%d\n", 5745853e133SVivek Gautam udev, pipe, buffer, length); 5755853e133SVivek Gautam 5765853e133SVivek Gautam ep_index = usb_pipe_ep_index(pipe); 5775853e133SVivek Gautam virt_dev = ctrl->devs[slot_id]; 5785853e133SVivek Gautam 579421a5a0cSSergey Temerkhanov xhci_inval_cache((uintptr_t)virt_dev->out_ctx->bytes, 5805853e133SVivek Gautam virt_dev->out_ctx->size); 5815853e133SVivek Gautam 5825853e133SVivek Gautam ep_ctx = xhci_get_ep_ctx(ctrl, virt_dev->out_ctx, ep_index); 5835853e133SVivek Gautam 5845853e133SVivek Gautam ring = virt_dev->eps[ep_index].ring; 5855853e133SVivek Gautam /* 5865853e133SVivek Gautam * How much data is (potentially) left before the 64KB boundary? 5875853e133SVivek Gautam * XHCI Spec puts restriction( TABLE 49 and 6.4.1 section of XHCI Spec) 5885853e133SVivek Gautam * that the buffer should not span 64KB boundary. if so 5895853e133SVivek Gautam * we send request in more than 1 TRB by chaining them. 5905853e133SVivek Gautam */ 5915853e133SVivek Gautam running_total = TRB_MAX_BUFF_SIZE - 5925853e133SVivek Gautam (lower_32_bits(val_64) & (TRB_MAX_BUFF_SIZE - 1)); 5935853e133SVivek Gautam trb_buff_len = running_total; 5945853e133SVivek Gautam running_total &= TRB_MAX_BUFF_SIZE - 1; 5955853e133SVivek Gautam 5965853e133SVivek Gautam /* 5975853e133SVivek Gautam * If there's some data on this 64KB chunk, or we have to send a 5985853e133SVivek Gautam * zero-length transfer, we need at least one TRB 5995853e133SVivek Gautam */ 6005853e133SVivek Gautam if (running_total != 0 || length == 0) 6015853e133SVivek Gautam num_trbs++; 6025853e133SVivek Gautam 6035853e133SVivek Gautam /* How many more 64KB chunks to transfer, how many more TRBs? */ 6045853e133SVivek Gautam while (running_total < length) { 6055853e133SVivek Gautam num_trbs++; 6065853e133SVivek Gautam running_total += TRB_MAX_BUFF_SIZE; 6075853e133SVivek Gautam } 6085853e133SVivek Gautam 6095853e133SVivek Gautam /* 6105853e133SVivek Gautam * XXX: Calling routine prepare_ring() called in place of 6115853e133SVivek Gautam * prepare_trasfer() as there in 'Linux' since we are not 6125853e133SVivek Gautam * maintaining multiple TDs/transfer at the same time. 6135853e133SVivek Gautam */ 6145853e133SVivek Gautam ret = prepare_ring(ctrl, ring, 6155853e133SVivek Gautam le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK); 6165853e133SVivek Gautam if (ret < 0) 6175853e133SVivek Gautam return ret; 6185853e133SVivek Gautam 6195853e133SVivek Gautam /* 6205853e133SVivek Gautam * Don't give the first TRB to the hardware (by toggling the cycle bit) 6215853e133SVivek Gautam * until we've finished creating all the other TRBs. The ring's cycle 6225853e133SVivek Gautam * state may change as we enqueue the other TRBs, so save it too. 6235853e133SVivek Gautam */ 6245853e133SVivek Gautam start_trb = &ring->enqueue->generic; 6255853e133SVivek Gautam start_cycle = ring->cycle_state; 6265853e133SVivek Gautam 6275853e133SVivek Gautam running_total = 0; 6285853e133SVivek Gautam maxpacketsize = usb_maxpacket(udev, pipe); 6295853e133SVivek Gautam 6305853e133SVivek Gautam total_packet_count = DIV_ROUND_UP(length, maxpacketsize); 6315853e133SVivek Gautam 6325853e133SVivek Gautam /* How much data is in the first TRB? */ 6335853e133SVivek Gautam /* 6345853e133SVivek Gautam * How much data is (potentially) left before the 64KB boundary? 6355853e133SVivek Gautam * XHCI Spec puts restriction( TABLE 49 and 6.4.1 section of XHCI Spec) 6365853e133SVivek Gautam * that the buffer should not span 64KB boundary. if so 6375853e133SVivek Gautam * we send request in more than 1 TRB by chaining them. 6385853e133SVivek Gautam */ 6395853e133SVivek Gautam addr = val_64; 6405853e133SVivek Gautam 6415853e133SVivek Gautam if (trb_buff_len > length) 6425853e133SVivek Gautam trb_buff_len = length; 6435853e133SVivek Gautam 6445853e133SVivek Gautam first_trb = true; 6455853e133SVivek Gautam 6465853e133SVivek Gautam /* flush the buffer before use */ 647421a5a0cSSergey Temerkhanov xhci_flush_cache((uintptr_t)buffer, length); 6485853e133SVivek Gautam 6495853e133SVivek Gautam /* Queue the first TRB, even if it's zero-length */ 6505853e133SVivek Gautam do { 6515853e133SVivek Gautam u32 remainder = 0; 6525853e133SVivek Gautam field = 0; 6535853e133SVivek Gautam /* Don't change the cycle bit of the first TRB until later */ 6545853e133SVivek Gautam if (first_trb) { 6555853e133SVivek Gautam first_trb = false; 6565853e133SVivek Gautam if (start_cycle == 0) 6575853e133SVivek Gautam field |= TRB_CYCLE; 6585853e133SVivek Gautam } else { 6595853e133SVivek Gautam field |= ring->cycle_state; 6605853e133SVivek Gautam } 6615853e133SVivek Gautam 6625853e133SVivek Gautam /* 6635853e133SVivek Gautam * Chain all the TRBs together; clear the chain bit in the last 6645853e133SVivek Gautam * TRB to indicate it's the last TRB in the chain. 6655853e133SVivek Gautam */ 6665853e133SVivek Gautam if (num_trbs > 1) 6675853e133SVivek Gautam field |= TRB_CHAIN; 6685853e133SVivek Gautam else 6695853e133SVivek Gautam field |= TRB_IOC; 6705853e133SVivek Gautam 6715853e133SVivek Gautam /* Only set interrupt on short packet for IN endpoints */ 6725853e133SVivek Gautam if (usb_pipein(pipe)) 6735853e133SVivek Gautam field |= TRB_ISP; 6745853e133SVivek Gautam 6755853e133SVivek Gautam /* Set the TRB length, TD size, and interrupter fields. */ 6765853e133SVivek Gautam if (HC_VERSION(xhci_readl(&ctrl->hccr->cr_capbase)) < 0x100) 6775853e133SVivek Gautam remainder = xhci_td_remainder(length - running_total); 6785853e133SVivek Gautam else 6795853e133SVivek Gautam remainder = xhci_v1_0_td_remainder(running_total, 6805853e133SVivek Gautam trb_buff_len, 6815853e133SVivek Gautam total_packet_count, 6825853e133SVivek Gautam maxpacketsize, 6835853e133SVivek Gautam num_trbs - 1); 6845853e133SVivek Gautam 6855853e133SVivek Gautam length_field = ((trb_buff_len & TRB_LEN_MASK) | 6865853e133SVivek Gautam remainder | 6875853e133SVivek Gautam ((0 & TRB_INTR_TARGET_MASK) << 6885853e133SVivek Gautam TRB_INTR_TARGET_SHIFT)); 6895853e133SVivek Gautam 6905853e133SVivek Gautam trb_fields[0] = lower_32_bits(addr); 6915853e133SVivek Gautam trb_fields[1] = upper_32_bits(addr); 6925853e133SVivek Gautam trb_fields[2] = length_field; 6935853e133SVivek Gautam trb_fields[3] = field | (TRB_NORMAL << TRB_TYPE_SHIFT); 6945853e133SVivek Gautam 6955853e133SVivek Gautam queue_trb(ctrl, ring, (num_trbs > 1), trb_fields); 6965853e133SVivek Gautam 6975853e133SVivek Gautam --num_trbs; 6985853e133SVivek Gautam 6995853e133SVivek Gautam running_total += trb_buff_len; 7005853e133SVivek Gautam 7015853e133SVivek Gautam /* Calculate length for next transfer */ 7025853e133SVivek Gautam addr += trb_buff_len; 7035853e133SVivek Gautam trb_buff_len = min((length - running_total), TRB_MAX_BUFF_SIZE); 7045853e133SVivek Gautam } while (running_total < length); 7055853e133SVivek Gautam 7065853e133SVivek Gautam giveback_first_trb(udev, ep_index, start_cycle, start_trb); 7075853e133SVivek Gautam 7085853e133SVivek Gautam event = xhci_wait_for_event(ctrl, TRB_TRANSFER); 7095853e133SVivek Gautam if (!event) { 7105853e133SVivek Gautam debug("XHCI bulk transfer timed out, aborting...\n"); 7115853e133SVivek Gautam abort_td(udev, ep_index); 7125853e133SVivek Gautam udev->status = USB_ST_NAK_REC; /* closest thing to a timeout */ 7135853e133SVivek Gautam udev->act_len = 0; 7145853e133SVivek Gautam return -ETIMEDOUT; 7155853e133SVivek Gautam } 7165853e133SVivek Gautam field = le32_to_cpu(event->trans_event.flags); 7175853e133SVivek Gautam 7185853e133SVivek Gautam BUG_ON(TRB_TO_SLOT_ID(field) != slot_id); 7195853e133SVivek Gautam BUG_ON(TRB_TO_EP_INDEX(field) != ep_index); 7205853e133SVivek Gautam BUG_ON(*(void **)(uintptr_t)le64_to_cpu(event->trans_event.buffer) - 7215853e133SVivek Gautam buffer > (size_t)length); 7225853e133SVivek Gautam 7235853e133SVivek Gautam record_transfer_result(udev, event, length); 7245853e133SVivek Gautam xhci_acknowledge_event(ctrl); 725421a5a0cSSergey Temerkhanov xhci_inval_cache((uintptr_t)buffer, length); 7265853e133SVivek Gautam 7275853e133SVivek Gautam return (udev->status != USB_ST_NOT_PROC) ? 0 : -1; 7285853e133SVivek Gautam } 7295853e133SVivek Gautam 7305853e133SVivek Gautam /** 7315853e133SVivek Gautam * Queues up the Control Transfer Request 7325853e133SVivek Gautam * 7335853e133SVivek Gautam * @param udev pointer to the USB device structure 7345853e133SVivek Gautam * @param pipe contains the DIR_IN or OUT , devnum 7355853e133SVivek Gautam * @param req request type 7365853e133SVivek Gautam * @param length length of the buffer 7375853e133SVivek Gautam * @param buffer buffer to be read/written based on the request 7385853e133SVivek Gautam * @return returns 0 if successful else error code on failure 7395853e133SVivek Gautam */ 7405853e133SVivek Gautam int xhci_ctrl_tx(struct usb_device *udev, unsigned long pipe, 7415853e133SVivek Gautam struct devrequest *req, int length, 7425853e133SVivek Gautam void *buffer) 7435853e133SVivek Gautam { 7445853e133SVivek Gautam int ret; 7455853e133SVivek Gautam int start_cycle; 7465853e133SVivek Gautam int num_trbs; 7475853e133SVivek Gautam u32 field; 7485853e133SVivek Gautam u32 length_field; 7495853e133SVivek Gautam u64 buf_64 = 0; 7505853e133SVivek Gautam struct xhci_generic_trb *start_trb; 751*7c1deec0SSimon Glass struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); 7525853e133SVivek Gautam int slot_id = udev->slot_id; 7535853e133SVivek Gautam int ep_index; 7545853e133SVivek Gautam u32 trb_fields[4]; 7555853e133SVivek Gautam struct xhci_virt_device *virt_dev = ctrl->devs[slot_id]; 7565853e133SVivek Gautam struct xhci_ring *ep_ring; 7575853e133SVivek Gautam union xhci_trb *event; 7585853e133SVivek Gautam 7595853e133SVivek Gautam debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n", 7605853e133SVivek Gautam req->request, req->request, 7615853e133SVivek Gautam req->requesttype, req->requesttype, 7625853e133SVivek Gautam le16_to_cpu(req->value), le16_to_cpu(req->value), 7635853e133SVivek Gautam le16_to_cpu(req->index)); 7645853e133SVivek Gautam 7655853e133SVivek Gautam ep_index = usb_pipe_ep_index(pipe); 7665853e133SVivek Gautam 7675853e133SVivek Gautam ep_ring = virt_dev->eps[ep_index].ring; 7685853e133SVivek Gautam 7695853e133SVivek Gautam /* 7705853e133SVivek Gautam * Check to see if the max packet size for the default control 7715853e133SVivek Gautam * endpoint changed during FS device enumeration 7725853e133SVivek Gautam */ 7735853e133SVivek Gautam if (udev->speed == USB_SPEED_FULL) { 7745853e133SVivek Gautam ret = xhci_check_maxpacket(udev); 7755853e133SVivek Gautam if (ret < 0) 7765853e133SVivek Gautam return ret; 7775853e133SVivek Gautam } 7785853e133SVivek Gautam 779421a5a0cSSergey Temerkhanov xhci_inval_cache((uintptr_t)virt_dev->out_ctx->bytes, 7805853e133SVivek Gautam virt_dev->out_ctx->size); 7815853e133SVivek Gautam 7825853e133SVivek Gautam struct xhci_ep_ctx *ep_ctx = NULL; 7835853e133SVivek Gautam ep_ctx = xhci_get_ep_ctx(ctrl, virt_dev->out_ctx, ep_index); 7845853e133SVivek Gautam 7855853e133SVivek Gautam /* 1 TRB for setup, 1 for status */ 7865853e133SVivek Gautam num_trbs = 2; 7875853e133SVivek Gautam /* 7885853e133SVivek Gautam * Don't need to check if we need additional event data and normal TRBs, 7895853e133SVivek Gautam * since data in control transfers will never get bigger than 16MB 7905853e133SVivek Gautam * XXX: can we get a buffer that crosses 64KB boundaries? 7915853e133SVivek Gautam */ 7925853e133SVivek Gautam 7935853e133SVivek Gautam if (length > 0) 7945853e133SVivek Gautam num_trbs++; 7955853e133SVivek Gautam /* 7965853e133SVivek Gautam * XXX: Calling routine prepare_ring() called in place of 7975853e133SVivek Gautam * prepare_trasfer() as there in 'Linux' since we are not 7985853e133SVivek Gautam * maintaining multiple TDs/transfer at the same time. 7995853e133SVivek Gautam */ 8005853e133SVivek Gautam ret = prepare_ring(ctrl, ep_ring, 8015853e133SVivek Gautam le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK); 8025853e133SVivek Gautam 8035853e133SVivek Gautam if (ret < 0) 8045853e133SVivek Gautam return ret; 8055853e133SVivek Gautam 8065853e133SVivek Gautam /* 8075853e133SVivek Gautam * Don't give the first TRB to the hardware (by toggling the cycle bit) 8085853e133SVivek Gautam * until we've finished creating all the other TRBs. The ring's cycle 8095853e133SVivek Gautam * state may change as we enqueue the other TRBs, so save it too. 8105853e133SVivek Gautam */ 8115853e133SVivek Gautam start_trb = &ep_ring->enqueue->generic; 8125853e133SVivek Gautam start_cycle = ep_ring->cycle_state; 8135853e133SVivek Gautam 8145853e133SVivek Gautam debug("start_trb %p, start_cycle %d\n", start_trb, start_cycle); 8155853e133SVivek Gautam 8165853e133SVivek Gautam /* Queue setup TRB - see section 6.4.1.2.1 */ 8175853e133SVivek Gautam /* FIXME better way to translate setup_packet into two u32 fields? */ 8185853e133SVivek Gautam field = 0; 8195853e133SVivek Gautam field |= TRB_IDT | (TRB_SETUP << TRB_TYPE_SHIFT); 8205853e133SVivek Gautam if (start_cycle == 0) 8215853e133SVivek Gautam field |= 0x1; 8225853e133SVivek Gautam 8235853e133SVivek Gautam /* xHCI 1.0 6.4.1.2.1: Transfer Type field */ 8245853e133SVivek Gautam if (HC_VERSION(xhci_readl(&ctrl->hccr->cr_capbase)) == 0x100) { 8255853e133SVivek Gautam if (length > 0) { 8265853e133SVivek Gautam if (req->requesttype & USB_DIR_IN) 8275853e133SVivek Gautam field |= (TRB_DATA_IN << TRB_TX_TYPE_SHIFT); 8285853e133SVivek Gautam else 8295853e133SVivek Gautam field |= (TRB_DATA_OUT << TRB_TX_TYPE_SHIFT); 8305853e133SVivek Gautam } 8315853e133SVivek Gautam } 8325853e133SVivek Gautam 8335853e133SVivek Gautam debug("req->requesttype = %d, req->request = %d," 8345853e133SVivek Gautam "le16_to_cpu(req->value) = %d," 8355853e133SVivek Gautam "le16_to_cpu(req->index) = %d," 8365853e133SVivek Gautam "le16_to_cpu(req->length) = %d\n", 8375853e133SVivek Gautam req->requesttype, req->request, le16_to_cpu(req->value), 8385853e133SVivek Gautam le16_to_cpu(req->index), le16_to_cpu(req->length)); 8395853e133SVivek Gautam 8405853e133SVivek Gautam trb_fields[0] = req->requesttype | req->request << 8 | 8415853e133SVivek Gautam le16_to_cpu(req->value) << 16; 8425853e133SVivek Gautam trb_fields[1] = le16_to_cpu(req->index) | 8435853e133SVivek Gautam le16_to_cpu(req->length) << 16; 8445853e133SVivek Gautam /* TRB_LEN | (TRB_INTR_TARGET) */ 8455853e133SVivek Gautam trb_fields[2] = (8 | ((0 & TRB_INTR_TARGET_MASK) << 8465853e133SVivek Gautam TRB_INTR_TARGET_SHIFT)); 8475853e133SVivek Gautam /* Immediate data in pointer */ 8485853e133SVivek Gautam trb_fields[3] = field; 8495853e133SVivek Gautam queue_trb(ctrl, ep_ring, true, trb_fields); 8505853e133SVivek Gautam 8515853e133SVivek Gautam /* Re-initializing field to zero */ 8525853e133SVivek Gautam field = 0; 8535853e133SVivek Gautam /* If there's data, queue data TRBs */ 8545853e133SVivek Gautam /* Only set interrupt on short packet for IN endpoints */ 8555853e133SVivek Gautam if (usb_pipein(pipe)) 8565853e133SVivek Gautam field = TRB_ISP | (TRB_DATA << TRB_TYPE_SHIFT); 8575853e133SVivek Gautam else 8585853e133SVivek Gautam field = (TRB_DATA << TRB_TYPE_SHIFT); 8595853e133SVivek Gautam 8605853e133SVivek Gautam length_field = (length & TRB_LEN_MASK) | xhci_td_remainder(length) | 8615853e133SVivek Gautam ((0 & TRB_INTR_TARGET_MASK) << TRB_INTR_TARGET_SHIFT); 8625853e133SVivek Gautam debug("length_field = %d, length = %d," 8635853e133SVivek Gautam "xhci_td_remainder(length) = %d , TRB_INTR_TARGET(0) = %d\n", 8645853e133SVivek Gautam length_field, (length & TRB_LEN_MASK), 8655853e133SVivek Gautam xhci_td_remainder(length), 0); 8665853e133SVivek Gautam 8675853e133SVivek Gautam if (length > 0) { 8685853e133SVivek Gautam if (req->requesttype & USB_DIR_IN) 8695853e133SVivek Gautam field |= TRB_DIR_IN; 8705853e133SVivek Gautam buf_64 = (uintptr_t)buffer; 8715853e133SVivek Gautam 8725853e133SVivek Gautam trb_fields[0] = lower_32_bits(buf_64); 8735853e133SVivek Gautam trb_fields[1] = upper_32_bits(buf_64); 8745853e133SVivek Gautam trb_fields[2] = length_field; 8755853e133SVivek Gautam trb_fields[3] = field | ep_ring->cycle_state; 8765853e133SVivek Gautam 877421a5a0cSSergey Temerkhanov xhci_flush_cache((uintptr_t)buffer, length); 8785853e133SVivek Gautam queue_trb(ctrl, ep_ring, true, trb_fields); 8795853e133SVivek Gautam } 8805853e133SVivek Gautam 8815853e133SVivek Gautam /* 8825853e133SVivek Gautam * Queue status TRB - 8835853e133SVivek Gautam * see Table 7 and sections 4.11.2.2 and 6.4.1.2.3 8845853e133SVivek Gautam */ 8855853e133SVivek Gautam 8865853e133SVivek Gautam /* If the device sent data, the status stage is an OUT transfer */ 8875853e133SVivek Gautam field = 0; 8885853e133SVivek Gautam if (length > 0 && req->requesttype & USB_DIR_IN) 8895853e133SVivek Gautam field = 0; 8905853e133SVivek Gautam else 8915853e133SVivek Gautam field = TRB_DIR_IN; 8925853e133SVivek Gautam 8935853e133SVivek Gautam trb_fields[0] = 0; 8945853e133SVivek Gautam trb_fields[1] = 0; 8955853e133SVivek Gautam trb_fields[2] = ((0 & TRB_INTR_TARGET_MASK) << TRB_INTR_TARGET_SHIFT); 8965853e133SVivek Gautam /* Event on completion */ 8975853e133SVivek Gautam trb_fields[3] = field | TRB_IOC | 8985853e133SVivek Gautam (TRB_STATUS << TRB_TYPE_SHIFT) | 8995853e133SVivek Gautam ep_ring->cycle_state; 9005853e133SVivek Gautam 9015853e133SVivek Gautam queue_trb(ctrl, ep_ring, false, trb_fields); 9025853e133SVivek Gautam 9035853e133SVivek Gautam giveback_first_trb(udev, ep_index, start_cycle, start_trb); 9045853e133SVivek Gautam 9055853e133SVivek Gautam event = xhci_wait_for_event(ctrl, TRB_TRANSFER); 9065853e133SVivek Gautam if (!event) 9075853e133SVivek Gautam goto abort; 9085853e133SVivek Gautam field = le32_to_cpu(event->trans_event.flags); 9095853e133SVivek Gautam 9105853e133SVivek Gautam BUG_ON(TRB_TO_SLOT_ID(field) != slot_id); 9115853e133SVivek Gautam BUG_ON(TRB_TO_EP_INDEX(field) != ep_index); 9125853e133SVivek Gautam 9135853e133SVivek Gautam record_transfer_result(udev, event, length); 9145853e133SVivek Gautam xhci_acknowledge_event(ctrl); 9155853e133SVivek Gautam 9165853e133SVivek Gautam /* Invalidate buffer to make it available to usb-core */ 9175853e133SVivek Gautam if (length > 0) 918421a5a0cSSergey Temerkhanov xhci_inval_cache((uintptr_t)buffer, length); 9195853e133SVivek Gautam 9205853e133SVivek Gautam if (GET_COMP_CODE(le32_to_cpu(event->trans_event.transfer_len)) 9215853e133SVivek Gautam == COMP_SHORT_TX) { 9225853e133SVivek Gautam /* Short data stage, clear up additional status stage event */ 9235853e133SVivek Gautam event = xhci_wait_for_event(ctrl, TRB_TRANSFER); 9245853e133SVivek Gautam if (!event) 9255853e133SVivek Gautam goto abort; 9265853e133SVivek Gautam BUG_ON(TRB_TO_SLOT_ID(field) != slot_id); 9275853e133SVivek Gautam BUG_ON(TRB_TO_EP_INDEX(field) != ep_index); 9285853e133SVivek Gautam xhci_acknowledge_event(ctrl); 9295853e133SVivek Gautam } 9305853e133SVivek Gautam 9315853e133SVivek Gautam return (udev->status != USB_ST_NOT_PROC) ? 0 : -1; 9325853e133SVivek Gautam 9335853e133SVivek Gautam abort: 9345853e133SVivek Gautam debug("XHCI control transfer timed out, aborting...\n"); 9355853e133SVivek Gautam abort_td(udev, ep_index); 9365853e133SVivek Gautam udev->status = USB_ST_NAK_REC; 9375853e133SVivek Gautam udev->act_len = 0; 9385853e133SVivek Gautam return -ETIMEDOUT; 9395853e133SVivek Gautam } 940