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