xref: /OK3568_Linux_fs/kernel/drivers/usb/dwc2/hcd_queue.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * hcd_queue.c - DesignWare HS OTG Controller host queuing routines
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2004-2013 Synopsys, Inc.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
8*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions
9*4882a593Smuzhiyun  * are met:
10*4882a593Smuzhiyun  * 1. Redistributions of source code must retain the above copyright
11*4882a593Smuzhiyun  *    notice, this list of conditions, and the following disclaimer,
12*4882a593Smuzhiyun  *    without modification.
13*4882a593Smuzhiyun  * 2. Redistributions in binary form must reproduce the above copyright
14*4882a593Smuzhiyun  *    notice, this list of conditions and the following disclaimer in the
15*4882a593Smuzhiyun  *    documentation and/or other materials provided with the distribution.
16*4882a593Smuzhiyun  * 3. The names of the above-listed copyright holders may not be used
17*4882a593Smuzhiyun  *    to endorse or promote products derived from this software without
18*4882a593Smuzhiyun  *    specific prior written permission.
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  * ALTERNATIVELY, this software may be distributed under the terms of the
21*4882a593Smuzhiyun  * GNU General Public License ("GPL") as published by the Free Software
22*4882a593Smuzhiyun  * Foundation; either version 2 of the License, or (at your option) any
23*4882a593Smuzhiyun  * later version.
24*4882a593Smuzhiyun  *
25*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
26*4882a593Smuzhiyun  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27*4882a593Smuzhiyun  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28*4882a593Smuzhiyun  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29*4882a593Smuzhiyun  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30*4882a593Smuzhiyun  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31*4882a593Smuzhiyun  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32*4882a593Smuzhiyun  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33*4882a593Smuzhiyun  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34*4882a593Smuzhiyun  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35*4882a593Smuzhiyun  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36*4882a593Smuzhiyun  */
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun /*
39*4882a593Smuzhiyun  * This file contains the functions to manage Queue Heads and Queue
40*4882a593Smuzhiyun  * Transfer Descriptors for Host mode
41*4882a593Smuzhiyun  */
42*4882a593Smuzhiyun #include <linux/gcd.h>
43*4882a593Smuzhiyun #include <linux/kernel.h>
44*4882a593Smuzhiyun #include <linux/module.h>
45*4882a593Smuzhiyun #include <linux/spinlock.h>
46*4882a593Smuzhiyun #include <linux/interrupt.h>
47*4882a593Smuzhiyun #include <linux/dma-mapping.h>
48*4882a593Smuzhiyun #include <linux/io.h>
49*4882a593Smuzhiyun #include <linux/slab.h>
50*4882a593Smuzhiyun #include <linux/usb.h>
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun #include <linux/usb/hcd.h>
53*4882a593Smuzhiyun #include <linux/usb/ch11.h>
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun #include "core.h"
56*4882a593Smuzhiyun #include "hcd.h"
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun /* Wait this long before releasing periodic reservation */
59*4882a593Smuzhiyun #define DWC2_UNRESERVE_DELAY (msecs_to_jiffies(5))
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun /* If we get a NAK, wait this long before retrying */
62*4882a593Smuzhiyun #define DWC2_RETRY_WAIT_DELAY (1 * NSEC_PER_MSEC)
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun /**
65*4882a593Smuzhiyun  * dwc2_periodic_channel_available() - Checks that a channel is available for a
66*4882a593Smuzhiyun  * periodic transfer
67*4882a593Smuzhiyun  *
68*4882a593Smuzhiyun  * @hsotg: The HCD state structure for the DWC OTG controller
69*4882a593Smuzhiyun  *
70*4882a593Smuzhiyun  * Return: 0 if successful, negative error code otherwise
71*4882a593Smuzhiyun  */
dwc2_periodic_channel_available(struct dwc2_hsotg * hsotg)72*4882a593Smuzhiyun static int dwc2_periodic_channel_available(struct dwc2_hsotg *hsotg)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun 	/*
75*4882a593Smuzhiyun 	 * Currently assuming that there is a dedicated host channel for
76*4882a593Smuzhiyun 	 * each periodic transaction plus at least one host channel for
77*4882a593Smuzhiyun 	 * non-periodic transactions
78*4882a593Smuzhiyun 	 */
79*4882a593Smuzhiyun 	int status;
80*4882a593Smuzhiyun 	int num_channels;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	num_channels = hsotg->params.host_channels;
83*4882a593Smuzhiyun 	if ((hsotg->periodic_channels + hsotg->non_periodic_channels <
84*4882a593Smuzhiyun 	     num_channels) && (hsotg->periodic_channels < num_channels - 1)) {
85*4882a593Smuzhiyun 		status = 0;
86*4882a593Smuzhiyun 	} else {
87*4882a593Smuzhiyun 		dev_dbg(hsotg->dev,
88*4882a593Smuzhiyun 			"%s: Total channels: %d, Periodic: %d, Non-periodic: %d\n",
89*4882a593Smuzhiyun 			__func__, num_channels,
90*4882a593Smuzhiyun 			hsotg->periodic_channels, hsotg->non_periodic_channels);
91*4882a593Smuzhiyun 		status = -ENOSPC;
92*4882a593Smuzhiyun 	}
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	return status;
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun /**
98*4882a593Smuzhiyun  * dwc2_check_periodic_bandwidth() - Checks that there is sufficient bandwidth
99*4882a593Smuzhiyun  * for the specified QH in the periodic schedule
100*4882a593Smuzhiyun  *
101*4882a593Smuzhiyun  * @hsotg: The HCD state structure for the DWC OTG controller
102*4882a593Smuzhiyun  * @qh:    QH containing periodic bandwidth required
103*4882a593Smuzhiyun  *
104*4882a593Smuzhiyun  * Return: 0 if successful, negative error code otherwise
105*4882a593Smuzhiyun  *
106*4882a593Smuzhiyun  * For simplicity, this calculation assumes that all the transfers in the
107*4882a593Smuzhiyun  * periodic schedule may occur in the same (micro)frame
108*4882a593Smuzhiyun  */
dwc2_check_periodic_bandwidth(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)109*4882a593Smuzhiyun static int dwc2_check_periodic_bandwidth(struct dwc2_hsotg *hsotg,
110*4882a593Smuzhiyun 					 struct dwc2_qh *qh)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun 	int status;
113*4882a593Smuzhiyun 	s16 max_claimed_usecs;
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	status = 0;
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	if (qh->dev_speed == USB_SPEED_HIGH || qh->do_split) {
118*4882a593Smuzhiyun 		/*
119*4882a593Smuzhiyun 		 * High speed mode
120*4882a593Smuzhiyun 		 * Max periodic usecs is 80% x 125 usec = 100 usec
121*4882a593Smuzhiyun 		 */
122*4882a593Smuzhiyun 		max_claimed_usecs = 100 - qh->host_us;
123*4882a593Smuzhiyun 	} else {
124*4882a593Smuzhiyun 		/*
125*4882a593Smuzhiyun 		 * Full speed mode
126*4882a593Smuzhiyun 		 * Max periodic usecs is 90% x 1000 usec = 900 usec
127*4882a593Smuzhiyun 		 */
128*4882a593Smuzhiyun 		max_claimed_usecs = 900 - qh->host_us;
129*4882a593Smuzhiyun 	}
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	if (hsotg->periodic_usecs > max_claimed_usecs) {
132*4882a593Smuzhiyun 		dev_err(hsotg->dev,
133*4882a593Smuzhiyun 			"%s: already claimed usecs %d, required usecs %d\n",
134*4882a593Smuzhiyun 			__func__, hsotg->periodic_usecs, qh->host_us);
135*4882a593Smuzhiyun 		status = -ENOSPC;
136*4882a593Smuzhiyun 	}
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	return status;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun /**
142*4882a593Smuzhiyun  * pmap_schedule() - Schedule time in a periodic bitmap (pmap).
143*4882a593Smuzhiyun  *
144*4882a593Smuzhiyun  * @map:             The bitmap representing the schedule; will be updated
145*4882a593Smuzhiyun  *                   upon success.
146*4882a593Smuzhiyun  * @bits_per_period: The schedule represents several periods.  This is how many
147*4882a593Smuzhiyun  *                   bits are in each period.  It's assumed that the beginning
148*4882a593Smuzhiyun  *                   of the schedule will repeat after its end.
149*4882a593Smuzhiyun  * @periods_in_map:  The number of periods in the schedule.
150*4882a593Smuzhiyun  * @num_bits:        The number of bits we need per period we want to reserve
151*4882a593Smuzhiyun  *                   in this function call.
152*4882a593Smuzhiyun  * @interval:        How often we need to be scheduled for the reservation this
153*4882a593Smuzhiyun  *                   time.  1 means every period.  2 means every other period.
154*4882a593Smuzhiyun  *                   ...you get the picture?
155*4882a593Smuzhiyun  * @start:           The bit number to start at.  Normally 0.  Must be within
156*4882a593Smuzhiyun  *                   the interval or we return failure right away.
157*4882a593Smuzhiyun  * @only_one_period: Normally we'll allow picking a start anywhere within the
158*4882a593Smuzhiyun  *                   first interval, since we can still make all repetition
159*4882a593Smuzhiyun  *                   requirements by doing that.  However, if you pass true
160*4882a593Smuzhiyun  *                   here then we'll return failure if we can't fit within
161*4882a593Smuzhiyun  *                   the period that "start" is in.
162*4882a593Smuzhiyun  *
163*4882a593Smuzhiyun  * The idea here is that we want to schedule time for repeating events that all
164*4882a593Smuzhiyun  * want the same resource.  The resource is divided into fixed-sized periods
165*4882a593Smuzhiyun  * and the events want to repeat every "interval" periods.  The schedule
166*4882a593Smuzhiyun  * granularity is one bit.
167*4882a593Smuzhiyun  *
168*4882a593Smuzhiyun  * To keep things "simple", we'll represent our schedule with a bitmap that
169*4882a593Smuzhiyun  * contains a fixed number of periods.  This gets rid of a lot of complexity
170*4882a593Smuzhiyun  * but does mean that we need to handle things specially (and non-ideally) if
171*4882a593Smuzhiyun  * the number of the periods in the schedule doesn't match well with the
172*4882a593Smuzhiyun  * intervals that we're trying to schedule.
173*4882a593Smuzhiyun  *
174*4882a593Smuzhiyun  * Here's an explanation of the scheme we'll implement, assuming 8 periods.
175*4882a593Smuzhiyun  * - If interval is 1, we need to take up space in each of the 8
176*4882a593Smuzhiyun  *   periods we're scheduling.  Easy.
177*4882a593Smuzhiyun  * - If interval is 2, we need to take up space in half of the
178*4882a593Smuzhiyun  *   periods.  Again, easy.
179*4882a593Smuzhiyun  * - If interval is 3, we actually need to fall back to interval 1.
180*4882a593Smuzhiyun  *   Why?  Because we might need time in any period.  AKA for the
181*4882a593Smuzhiyun  *   first 8 periods, we'll be in slot 0, 3, 6.  Then we'll be
182*4882a593Smuzhiyun  *   in slot 1, 4, 7.  Then we'll be in 2, 5.  Then we'll be back to
183*4882a593Smuzhiyun  *   0, 3, and 6.  Since we could be in any frame we need to reserve
184*4882a593Smuzhiyun  *   for all of them.  Sucks, but that's what you gotta do.  Note that
185*4882a593Smuzhiyun  *   if we were instead scheduling 8 * 3 = 24 we'd do much better, but
186*4882a593Smuzhiyun  *   then we need more memory and time to do scheduling.
187*4882a593Smuzhiyun  * - If interval is 4, easy.
188*4882a593Smuzhiyun  * - If interval is 5, we again need interval 1.  The schedule will be
189*4882a593Smuzhiyun  *   0, 5, 2, 7, 4, 1, 6, 3, 0
190*4882a593Smuzhiyun  * - If interval is 6, we need interval 2.  0, 6, 4, 2.
191*4882a593Smuzhiyun  * - If interval is 7, we need interval 1.
192*4882a593Smuzhiyun  * - If interval is 8, we need interval 8.
193*4882a593Smuzhiyun  *
194*4882a593Smuzhiyun  * If you do the math, you'll see that we need to pretend that interval is
195*4882a593Smuzhiyun  * equal to the greatest_common_divisor(interval, periods_in_map).
196*4882a593Smuzhiyun  *
197*4882a593Smuzhiyun  * Note that at the moment this function tends to front-pack the schedule.
198*4882a593Smuzhiyun  * In some cases that's really non-ideal (it's hard to schedule things that
199*4882a593Smuzhiyun  * need to repeat every period).  In other cases it's perfect (you can easily
200*4882a593Smuzhiyun  * schedule bigger, less often repeating things).
201*4882a593Smuzhiyun  *
202*4882a593Smuzhiyun  * Here's the algorithm in action (8 periods, 5 bits per period):
203*4882a593Smuzhiyun  *  |**   |     |**   |     |**   |     |**   |     |   OK 2 bits, intv 2 at 0
204*4882a593Smuzhiyun  *  |*****|  ***|*****|  ***|*****|  ***|*****|  ***|   OK 3 bits, intv 3 at 2
205*4882a593Smuzhiyun  *  |*****|* ***|*****|  ***|*****|* ***|*****|  ***|   OK 1 bits, intv 4 at 5
206*4882a593Smuzhiyun  *  |**   |*    |**   |     |**   |*    |**   |     | Remv 3 bits, intv 3 at 2
207*4882a593Smuzhiyun  *  |***  |*    |***  |     |***  |*    |***  |     |   OK 1 bits, intv 6 at 2
208*4882a593Smuzhiyun  *  |**** |*  * |**** |   * |**** |*  * |**** |   * |   OK 1 bits, intv 1 at 3
209*4882a593Smuzhiyun  *  |**** |**** |**** | *** |**** |**** |**** | *** |   OK 2 bits, intv 2 at 6
210*4882a593Smuzhiyun  *  |*****|*****|*****| ****|*****|*****|*****| ****|   OK 1 bits, intv 1 at 4
211*4882a593Smuzhiyun  *  |*****|*****|*****| ****|*****|*****|*****| ****| FAIL 1 bits, intv 1
212*4882a593Smuzhiyun  *  |  ***|*****|  ***| ****|  ***|*****|  ***| ****| Remv 2 bits, intv 2 at 0
213*4882a593Smuzhiyun  *  |  ***| ****|  ***| ****|  ***| ****|  ***| ****| Remv 1 bits, intv 4 at 5
214*4882a593Smuzhiyun  *  |   **| ****|   **| ****|   **| ****|   **| ****| Remv 1 bits, intv 6 at 2
215*4882a593Smuzhiyun  *  |    *| ** *|    *| ** *|    *| ** *|    *| ** *| Remv 1 bits, intv 1 at 3
216*4882a593Smuzhiyun  *  |    *|    *|    *|    *|    *|    *|    *|    *| Remv 2 bits, intv 2 at 6
217*4882a593Smuzhiyun  *  |     |     |     |     |     |     |     |     | Remv 1 bits, intv 1 at 4
218*4882a593Smuzhiyun  *  |**   |     |**   |     |**   |     |**   |     |   OK 2 bits, intv 2 at 0
219*4882a593Smuzhiyun  *  |***  |     |**   |     |***  |     |**   |     |   OK 1 bits, intv 4 at 2
220*4882a593Smuzhiyun  *  |*****|     |** **|     |*****|     |** **|     |   OK 2 bits, intv 2 at 3
221*4882a593Smuzhiyun  *  |*****|*    |** **|     |*****|*    |** **|     |   OK 1 bits, intv 4 at 5
222*4882a593Smuzhiyun  *  |*****|***  |** **| **  |*****|***  |** **| **  |   OK 2 bits, intv 2 at 6
223*4882a593Smuzhiyun  *  |*****|*****|** **| ****|*****|*****|** **| ****|   OK 2 bits, intv 2 at 8
224*4882a593Smuzhiyun  *  |*****|*****|*****| ****|*****|*****|*****| ****|   OK 1 bits, intv 4 at 12
225*4882a593Smuzhiyun  *
226*4882a593Smuzhiyun  * This function is pretty generic and could be easily abstracted if anything
227*4882a593Smuzhiyun  * needed similar scheduling.
228*4882a593Smuzhiyun  *
229*4882a593Smuzhiyun  * Returns either -ENOSPC or a >= 0 start bit which should be passed to the
230*4882a593Smuzhiyun  * unschedule routine.  The map bitmap will be updated on a non-error result.
231*4882a593Smuzhiyun  */
pmap_schedule(unsigned long * map,int bits_per_period,int periods_in_map,int num_bits,int interval,int start,bool only_one_period)232*4882a593Smuzhiyun static int pmap_schedule(unsigned long *map, int bits_per_period,
233*4882a593Smuzhiyun 			 int periods_in_map, int num_bits,
234*4882a593Smuzhiyun 			 int interval, int start, bool only_one_period)
235*4882a593Smuzhiyun {
236*4882a593Smuzhiyun 	int interval_bits;
237*4882a593Smuzhiyun 	int to_reserve;
238*4882a593Smuzhiyun 	int first_end;
239*4882a593Smuzhiyun 	int i;
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 	if (num_bits > bits_per_period)
242*4882a593Smuzhiyun 		return -ENOSPC;
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	/* Adjust interval as per description */
245*4882a593Smuzhiyun 	interval = gcd(interval, periods_in_map);
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	interval_bits = bits_per_period * interval;
248*4882a593Smuzhiyun 	to_reserve = periods_in_map / interval;
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	/* If start has gotten us past interval then we can't schedule */
251*4882a593Smuzhiyun 	if (start >= interval_bits)
252*4882a593Smuzhiyun 		return -ENOSPC;
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	if (only_one_period)
255*4882a593Smuzhiyun 		/* Must fit within same period as start; end at begin of next */
256*4882a593Smuzhiyun 		first_end = (start / bits_per_period + 1) * bits_per_period;
257*4882a593Smuzhiyun 	else
258*4882a593Smuzhiyun 		/* Can fit anywhere in the first interval */
259*4882a593Smuzhiyun 		first_end = interval_bits;
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	/*
262*4882a593Smuzhiyun 	 * We'll try to pick the first repetition, then see if that time
263*4882a593Smuzhiyun 	 * is free for each of the subsequent repetitions.  If it's not
264*4882a593Smuzhiyun 	 * we'll adjust the start time for the next search of the first
265*4882a593Smuzhiyun 	 * repetition.
266*4882a593Smuzhiyun 	 */
267*4882a593Smuzhiyun 	while (start + num_bits <= first_end) {
268*4882a593Smuzhiyun 		int end;
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 		/* Need to stay within this period */
271*4882a593Smuzhiyun 		end = (start / bits_per_period + 1) * bits_per_period;
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 		/* Look for num_bits us in this microframe starting at start */
274*4882a593Smuzhiyun 		start = bitmap_find_next_zero_area(map, end, start, num_bits,
275*4882a593Smuzhiyun 						   0);
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 		/*
278*4882a593Smuzhiyun 		 * We should get start >= end if we fail.  We might be
279*4882a593Smuzhiyun 		 * able to check the next microframe depending on the
280*4882a593Smuzhiyun 		 * interval, so continue on (start already updated).
281*4882a593Smuzhiyun 		 */
282*4882a593Smuzhiyun 		if (start >= end) {
283*4882a593Smuzhiyun 			start = end;
284*4882a593Smuzhiyun 			continue;
285*4882a593Smuzhiyun 		}
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 		/* At this point we have a valid point for first one */
288*4882a593Smuzhiyun 		for (i = 1; i < to_reserve; i++) {
289*4882a593Smuzhiyun 			int ith_start = start + interval_bits * i;
290*4882a593Smuzhiyun 			int ith_end = end + interval_bits * i;
291*4882a593Smuzhiyun 			int ret;
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 			/* Use this as a dumb "check if bits are 0" */
294*4882a593Smuzhiyun 			ret = bitmap_find_next_zero_area(
295*4882a593Smuzhiyun 				map, ith_start + num_bits, ith_start, num_bits,
296*4882a593Smuzhiyun 				0);
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 			/* We got the right place, continue checking */
299*4882a593Smuzhiyun 			if (ret == ith_start)
300*4882a593Smuzhiyun 				continue;
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 			/* Move start up for next time and exit for loop */
303*4882a593Smuzhiyun 			ith_start = bitmap_find_next_zero_area(
304*4882a593Smuzhiyun 				map, ith_end, ith_start, num_bits, 0);
305*4882a593Smuzhiyun 			if (ith_start >= ith_end)
306*4882a593Smuzhiyun 				/* Need a while new period next time */
307*4882a593Smuzhiyun 				start = end;
308*4882a593Smuzhiyun 			else
309*4882a593Smuzhiyun 				start = ith_start - interval_bits * i;
310*4882a593Smuzhiyun 			break;
311*4882a593Smuzhiyun 		}
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 		/* If didn't exit the for loop with a break, we have success */
314*4882a593Smuzhiyun 		if (i == to_reserve)
315*4882a593Smuzhiyun 			break;
316*4882a593Smuzhiyun 	}
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	if (start + num_bits > first_end)
319*4882a593Smuzhiyun 		return -ENOSPC;
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	for (i = 0; i < to_reserve; i++) {
322*4882a593Smuzhiyun 		int ith_start = start + interval_bits * i;
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 		bitmap_set(map, ith_start, num_bits);
325*4882a593Smuzhiyun 	}
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun 	return start;
328*4882a593Smuzhiyun }
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun /**
331*4882a593Smuzhiyun  * pmap_unschedule() - Undo work done by pmap_schedule()
332*4882a593Smuzhiyun  *
333*4882a593Smuzhiyun  * @map:             See pmap_schedule().
334*4882a593Smuzhiyun  * @bits_per_period: See pmap_schedule().
335*4882a593Smuzhiyun  * @periods_in_map:  See pmap_schedule().
336*4882a593Smuzhiyun  * @num_bits:        The number of bits that was passed to schedule.
337*4882a593Smuzhiyun  * @interval:        The interval that was passed to schedule.
338*4882a593Smuzhiyun  * @start:           The return value from pmap_schedule().
339*4882a593Smuzhiyun  */
pmap_unschedule(unsigned long * map,int bits_per_period,int periods_in_map,int num_bits,int interval,int start)340*4882a593Smuzhiyun static void pmap_unschedule(unsigned long *map, int bits_per_period,
341*4882a593Smuzhiyun 			    int periods_in_map, int num_bits,
342*4882a593Smuzhiyun 			    int interval, int start)
343*4882a593Smuzhiyun {
344*4882a593Smuzhiyun 	int interval_bits;
345*4882a593Smuzhiyun 	int to_release;
346*4882a593Smuzhiyun 	int i;
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 	/* Adjust interval as per description in pmap_schedule() */
349*4882a593Smuzhiyun 	interval = gcd(interval, periods_in_map);
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	interval_bits = bits_per_period * interval;
352*4882a593Smuzhiyun 	to_release = periods_in_map / interval;
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 	for (i = 0; i < to_release; i++) {
355*4882a593Smuzhiyun 		int ith_start = start + interval_bits * i;
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 		bitmap_clear(map, ith_start, num_bits);
358*4882a593Smuzhiyun 	}
359*4882a593Smuzhiyun }
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun /**
362*4882a593Smuzhiyun  * dwc2_get_ls_map() - Get the map used for the given qh
363*4882a593Smuzhiyun  *
364*4882a593Smuzhiyun  * @hsotg: The HCD state structure for the DWC OTG controller.
365*4882a593Smuzhiyun  * @qh:    QH for the periodic transfer.
366*4882a593Smuzhiyun  *
367*4882a593Smuzhiyun  * We'll always get the periodic map out of our TT.  Note that even if we're
368*4882a593Smuzhiyun  * running the host straight in low speed / full speed mode it appears as if
369*4882a593Smuzhiyun  * a TT is allocated for us, so we'll use it.  If that ever changes we can
370*4882a593Smuzhiyun  * add logic here to get a map out of "hsotg" if !qh->do_split.
371*4882a593Smuzhiyun  *
372*4882a593Smuzhiyun  * Returns: the map or NULL if a map couldn't be found.
373*4882a593Smuzhiyun  */
dwc2_get_ls_map(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)374*4882a593Smuzhiyun static unsigned long *dwc2_get_ls_map(struct dwc2_hsotg *hsotg,
375*4882a593Smuzhiyun 				      struct dwc2_qh *qh)
376*4882a593Smuzhiyun {
377*4882a593Smuzhiyun 	unsigned long *map;
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 	/* Don't expect to be missing a TT and be doing low speed scheduling */
380*4882a593Smuzhiyun 	if (WARN_ON(!qh->dwc_tt))
381*4882a593Smuzhiyun 		return NULL;
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 	/* Get the map and adjust if this is a multi_tt hub */
384*4882a593Smuzhiyun 	map = qh->dwc_tt->periodic_bitmaps;
385*4882a593Smuzhiyun 	if (qh->dwc_tt->usb_tt->multi)
386*4882a593Smuzhiyun 		map += DWC2_ELEMENTS_PER_LS_BITMAP * (qh->ttport - 1);
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun 	return map;
389*4882a593Smuzhiyun }
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun #ifdef DWC2_PRINT_SCHEDULE
392*4882a593Smuzhiyun /*
393*4882a593Smuzhiyun  * cat_printf() - A printf() + strcat() helper
394*4882a593Smuzhiyun  *
395*4882a593Smuzhiyun  * This is useful for concatenating a bunch of strings where each string is
396*4882a593Smuzhiyun  * constructed using printf.
397*4882a593Smuzhiyun  *
398*4882a593Smuzhiyun  * @buf:   The destination buffer; will be updated to point after the printed
399*4882a593Smuzhiyun  *         data.
400*4882a593Smuzhiyun  * @size:  The number of bytes in the buffer (includes space for '\0').
401*4882a593Smuzhiyun  * @fmt:   The format for printf.
402*4882a593Smuzhiyun  * @...:   The args for printf.
403*4882a593Smuzhiyun  */
404*4882a593Smuzhiyun static __printf(3, 4)
cat_printf(char ** buf,size_t * size,const char * fmt,...)405*4882a593Smuzhiyun void cat_printf(char **buf, size_t *size, const char *fmt, ...)
406*4882a593Smuzhiyun {
407*4882a593Smuzhiyun 	va_list args;
408*4882a593Smuzhiyun 	int i;
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun 	if (*size == 0)
411*4882a593Smuzhiyun 		return;
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun 	va_start(args, fmt);
414*4882a593Smuzhiyun 	i = vsnprintf(*buf, *size, fmt, args);
415*4882a593Smuzhiyun 	va_end(args);
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	if (i >= *size) {
418*4882a593Smuzhiyun 		(*buf)[*size - 1] = '\0';
419*4882a593Smuzhiyun 		*buf += *size;
420*4882a593Smuzhiyun 		*size = 0;
421*4882a593Smuzhiyun 	} else {
422*4882a593Smuzhiyun 		*buf += i;
423*4882a593Smuzhiyun 		*size -= i;
424*4882a593Smuzhiyun 	}
425*4882a593Smuzhiyun }
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun /*
428*4882a593Smuzhiyun  * pmap_print() - Print the given periodic map
429*4882a593Smuzhiyun  *
430*4882a593Smuzhiyun  * Will attempt to print out the periodic schedule.
431*4882a593Smuzhiyun  *
432*4882a593Smuzhiyun  * @map:             See pmap_schedule().
433*4882a593Smuzhiyun  * @bits_per_period: See pmap_schedule().
434*4882a593Smuzhiyun  * @periods_in_map:  See pmap_schedule().
435*4882a593Smuzhiyun  * @period_name:     The name of 1 period, like "uFrame"
436*4882a593Smuzhiyun  * @units:           The name of the units, like "us".
437*4882a593Smuzhiyun  * @print_fn:        The function to call for printing.
438*4882a593Smuzhiyun  * @print_data:      Opaque data to pass to the print function.
439*4882a593Smuzhiyun  */
pmap_print(unsigned long * map,int bits_per_period,int periods_in_map,const char * period_name,const char * units,void (* print_fn)(const char * str,void * data),void * print_data)440*4882a593Smuzhiyun static void pmap_print(unsigned long *map, int bits_per_period,
441*4882a593Smuzhiyun 		       int periods_in_map, const char *period_name,
442*4882a593Smuzhiyun 		       const char *units,
443*4882a593Smuzhiyun 		       void (*print_fn)(const char *str, void *data),
444*4882a593Smuzhiyun 		       void *print_data)
445*4882a593Smuzhiyun {
446*4882a593Smuzhiyun 	int period;
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun 	for (period = 0; period < periods_in_map; period++) {
449*4882a593Smuzhiyun 		char tmp[64];
450*4882a593Smuzhiyun 		char *buf = tmp;
451*4882a593Smuzhiyun 		size_t buf_size = sizeof(tmp);
452*4882a593Smuzhiyun 		int period_start = period * bits_per_period;
453*4882a593Smuzhiyun 		int period_end = period_start + bits_per_period;
454*4882a593Smuzhiyun 		int start = 0;
455*4882a593Smuzhiyun 		int count = 0;
456*4882a593Smuzhiyun 		bool printed = false;
457*4882a593Smuzhiyun 		int i;
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun 		for (i = period_start; i < period_end + 1; i++) {
460*4882a593Smuzhiyun 			/* Handle case when ith bit is set */
461*4882a593Smuzhiyun 			if (i < period_end &&
462*4882a593Smuzhiyun 			    bitmap_find_next_zero_area(map, i + 1,
463*4882a593Smuzhiyun 						       i, 1, 0) != i) {
464*4882a593Smuzhiyun 				if (count == 0)
465*4882a593Smuzhiyun 					start = i - period_start;
466*4882a593Smuzhiyun 				count++;
467*4882a593Smuzhiyun 				continue;
468*4882a593Smuzhiyun 			}
469*4882a593Smuzhiyun 
470*4882a593Smuzhiyun 			/* ith bit isn't set; don't care if count == 0 */
471*4882a593Smuzhiyun 			if (count == 0)
472*4882a593Smuzhiyun 				continue;
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun 			if (!printed)
475*4882a593Smuzhiyun 				cat_printf(&buf, &buf_size, "%s %d: ",
476*4882a593Smuzhiyun 					   period_name, period);
477*4882a593Smuzhiyun 			else
478*4882a593Smuzhiyun 				cat_printf(&buf, &buf_size, ", ");
479*4882a593Smuzhiyun 			printed = true;
480*4882a593Smuzhiyun 
481*4882a593Smuzhiyun 			cat_printf(&buf, &buf_size, "%d %s -%3d %s", start,
482*4882a593Smuzhiyun 				   units, start + count - 1, units);
483*4882a593Smuzhiyun 			count = 0;
484*4882a593Smuzhiyun 		}
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 		if (printed)
487*4882a593Smuzhiyun 			print_fn(tmp, print_data);
488*4882a593Smuzhiyun 	}
489*4882a593Smuzhiyun }
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun struct dwc2_qh_print_data {
492*4882a593Smuzhiyun 	struct dwc2_hsotg *hsotg;
493*4882a593Smuzhiyun 	struct dwc2_qh *qh;
494*4882a593Smuzhiyun };
495*4882a593Smuzhiyun 
496*4882a593Smuzhiyun /**
497*4882a593Smuzhiyun  * dwc2_qh_print() - Helper function for dwc2_qh_schedule_print()
498*4882a593Smuzhiyun  *
499*4882a593Smuzhiyun  * @str:  The string to print
500*4882a593Smuzhiyun  * @data: A pointer to a struct dwc2_qh_print_data
501*4882a593Smuzhiyun  */
dwc2_qh_print(const char * str,void * data)502*4882a593Smuzhiyun static void dwc2_qh_print(const char *str, void *data)
503*4882a593Smuzhiyun {
504*4882a593Smuzhiyun 	struct dwc2_qh_print_data *print_data = data;
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun 	dwc2_sch_dbg(print_data->hsotg, "QH=%p ...%s\n", print_data->qh, str);
507*4882a593Smuzhiyun }
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun /**
510*4882a593Smuzhiyun  * dwc2_qh_schedule_print() - Print the periodic schedule
511*4882a593Smuzhiyun  *
512*4882a593Smuzhiyun  * @hsotg: The HCD state structure for the DWC OTG controller.
513*4882a593Smuzhiyun  * @qh:    QH to print.
514*4882a593Smuzhiyun  */
dwc2_qh_schedule_print(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)515*4882a593Smuzhiyun static void dwc2_qh_schedule_print(struct dwc2_hsotg *hsotg,
516*4882a593Smuzhiyun 				   struct dwc2_qh *qh)
517*4882a593Smuzhiyun {
518*4882a593Smuzhiyun 	struct dwc2_qh_print_data print_data = { hsotg, qh };
519*4882a593Smuzhiyun 	int i;
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun 	/*
522*4882a593Smuzhiyun 	 * The printing functions are quite slow and inefficient.
523*4882a593Smuzhiyun 	 * If we don't have tracing turned on, don't run unless the special
524*4882a593Smuzhiyun 	 * define is turned on.
525*4882a593Smuzhiyun 	 */
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun 	if (qh->schedule_low_speed) {
528*4882a593Smuzhiyun 		unsigned long *map = dwc2_get_ls_map(hsotg, qh);
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 		dwc2_sch_dbg(hsotg, "QH=%p LS/FS trans: %d=>%d us @ %d us",
531*4882a593Smuzhiyun 			     qh, qh->device_us,
532*4882a593Smuzhiyun 			     DWC2_ROUND_US_TO_SLICE(qh->device_us),
533*4882a593Smuzhiyun 			     DWC2_US_PER_SLICE * qh->ls_start_schedule_slice);
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun 		if (map) {
536*4882a593Smuzhiyun 			dwc2_sch_dbg(hsotg,
537*4882a593Smuzhiyun 				     "QH=%p Whole low/full speed map %p now:\n",
538*4882a593Smuzhiyun 				     qh, map);
539*4882a593Smuzhiyun 			pmap_print(map, DWC2_LS_PERIODIC_SLICES_PER_FRAME,
540*4882a593Smuzhiyun 				   DWC2_LS_SCHEDULE_FRAMES, "Frame ", "slices",
541*4882a593Smuzhiyun 				   dwc2_qh_print, &print_data);
542*4882a593Smuzhiyun 		}
543*4882a593Smuzhiyun 	}
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 	for (i = 0; i < qh->num_hs_transfers; i++) {
546*4882a593Smuzhiyun 		struct dwc2_hs_transfer_time *trans_time = qh->hs_transfers + i;
547*4882a593Smuzhiyun 		int uframe = trans_time->start_schedule_us /
548*4882a593Smuzhiyun 			     DWC2_HS_PERIODIC_US_PER_UFRAME;
549*4882a593Smuzhiyun 		int rel_us = trans_time->start_schedule_us %
550*4882a593Smuzhiyun 			     DWC2_HS_PERIODIC_US_PER_UFRAME;
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun 		dwc2_sch_dbg(hsotg,
553*4882a593Smuzhiyun 			     "QH=%p HS trans #%d: %d us @ uFrame %d + %d us\n",
554*4882a593Smuzhiyun 			     qh, i, trans_time->duration_us, uframe, rel_us);
555*4882a593Smuzhiyun 	}
556*4882a593Smuzhiyun 	if (qh->num_hs_transfers) {
557*4882a593Smuzhiyun 		dwc2_sch_dbg(hsotg, "QH=%p Whole high speed map now:\n", qh);
558*4882a593Smuzhiyun 		pmap_print(hsotg->hs_periodic_bitmap,
559*4882a593Smuzhiyun 			   DWC2_HS_PERIODIC_US_PER_UFRAME,
560*4882a593Smuzhiyun 			   DWC2_HS_SCHEDULE_UFRAMES, "uFrame", "us",
561*4882a593Smuzhiyun 			   dwc2_qh_print, &print_data);
562*4882a593Smuzhiyun 	}
563*4882a593Smuzhiyun }
564*4882a593Smuzhiyun #else
dwc2_qh_schedule_print(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)565*4882a593Smuzhiyun static inline void dwc2_qh_schedule_print(struct dwc2_hsotg *hsotg,
566*4882a593Smuzhiyun 					  struct dwc2_qh *qh) {};
567*4882a593Smuzhiyun #endif
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun /**
570*4882a593Smuzhiyun  * dwc2_ls_pmap_schedule() - Schedule a low speed QH
571*4882a593Smuzhiyun  *
572*4882a593Smuzhiyun  * @hsotg:        The HCD state structure for the DWC OTG controller.
573*4882a593Smuzhiyun  * @qh:           QH for the periodic transfer.
574*4882a593Smuzhiyun  * @search_slice: We'll start trying to schedule at the passed slice.
575*4882a593Smuzhiyun  *                Remember that slices are the units of the low speed
576*4882a593Smuzhiyun  *                schedule (think 25us or so).
577*4882a593Smuzhiyun  *
578*4882a593Smuzhiyun  * Wraps pmap_schedule() with the right parameters for low speed scheduling.
579*4882a593Smuzhiyun  *
580*4882a593Smuzhiyun  * Normally we schedule low speed devices on the map associated with the TT.
581*4882a593Smuzhiyun  *
582*4882a593Smuzhiyun  * Returns: 0 for success or an error code.
583*4882a593Smuzhiyun  */
dwc2_ls_pmap_schedule(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh,int search_slice)584*4882a593Smuzhiyun static int dwc2_ls_pmap_schedule(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
585*4882a593Smuzhiyun 				 int search_slice)
586*4882a593Smuzhiyun {
587*4882a593Smuzhiyun 	int slices = DIV_ROUND_UP(qh->device_us, DWC2_US_PER_SLICE);
588*4882a593Smuzhiyun 	unsigned long *map = dwc2_get_ls_map(hsotg, qh);
589*4882a593Smuzhiyun 	int slice;
590*4882a593Smuzhiyun 
591*4882a593Smuzhiyun 	if (!map)
592*4882a593Smuzhiyun 		return -EINVAL;
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun 	/*
595*4882a593Smuzhiyun 	 * Schedule on the proper low speed map with our low speed scheduling
596*4882a593Smuzhiyun 	 * parameters.  Note that we use the "device_interval" here since
597*4882a593Smuzhiyun 	 * we want the low speed interval and the only way we'd be in this
598*4882a593Smuzhiyun 	 * function is if the device is low speed.
599*4882a593Smuzhiyun 	 *
600*4882a593Smuzhiyun 	 * If we happen to be doing low speed and high speed scheduling for the
601*4882a593Smuzhiyun 	 * same transaction (AKA we have a split) we always do low speed first.
602*4882a593Smuzhiyun 	 * That means we can always pass "false" for only_one_period (that
603*4882a593Smuzhiyun 	 * parameters is only useful when we're trying to get one schedule to
604*4882a593Smuzhiyun 	 * match what we already planned in the other schedule).
605*4882a593Smuzhiyun 	 */
606*4882a593Smuzhiyun 	slice = pmap_schedule(map, DWC2_LS_PERIODIC_SLICES_PER_FRAME,
607*4882a593Smuzhiyun 			      DWC2_LS_SCHEDULE_FRAMES, slices,
608*4882a593Smuzhiyun 			      qh->device_interval, search_slice, false);
609*4882a593Smuzhiyun 
610*4882a593Smuzhiyun 	if (slice < 0)
611*4882a593Smuzhiyun 		return slice;
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun 	qh->ls_start_schedule_slice = slice;
614*4882a593Smuzhiyun 	return 0;
615*4882a593Smuzhiyun }
616*4882a593Smuzhiyun 
617*4882a593Smuzhiyun /**
618*4882a593Smuzhiyun  * dwc2_ls_pmap_unschedule() - Undo work done by dwc2_ls_pmap_schedule()
619*4882a593Smuzhiyun  *
620*4882a593Smuzhiyun  * @hsotg:       The HCD state structure for the DWC OTG controller.
621*4882a593Smuzhiyun  * @qh:          QH for the periodic transfer.
622*4882a593Smuzhiyun  */
dwc2_ls_pmap_unschedule(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)623*4882a593Smuzhiyun static void dwc2_ls_pmap_unschedule(struct dwc2_hsotg *hsotg,
624*4882a593Smuzhiyun 				    struct dwc2_qh *qh)
625*4882a593Smuzhiyun {
626*4882a593Smuzhiyun 	int slices = DIV_ROUND_UP(qh->device_us, DWC2_US_PER_SLICE);
627*4882a593Smuzhiyun 	unsigned long *map = dwc2_get_ls_map(hsotg, qh);
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun 	/* Schedule should have failed, so no worries about no error code */
630*4882a593Smuzhiyun 	if (!map)
631*4882a593Smuzhiyun 		return;
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 	pmap_unschedule(map, DWC2_LS_PERIODIC_SLICES_PER_FRAME,
634*4882a593Smuzhiyun 			DWC2_LS_SCHEDULE_FRAMES, slices, qh->device_interval,
635*4882a593Smuzhiyun 			qh->ls_start_schedule_slice);
636*4882a593Smuzhiyun }
637*4882a593Smuzhiyun 
638*4882a593Smuzhiyun /**
639*4882a593Smuzhiyun  * dwc2_hs_pmap_schedule - Schedule in the main high speed schedule
640*4882a593Smuzhiyun  *
641*4882a593Smuzhiyun  * This will schedule something on the main dwc2 schedule.
642*4882a593Smuzhiyun  *
643*4882a593Smuzhiyun  * We'll start looking in qh->hs_transfers[index].start_schedule_us.  We'll
644*4882a593Smuzhiyun  * update this with the result upon success.  We also use the duration from
645*4882a593Smuzhiyun  * the same structure.
646*4882a593Smuzhiyun  *
647*4882a593Smuzhiyun  * @hsotg:           The HCD state structure for the DWC OTG controller.
648*4882a593Smuzhiyun  * @qh:              QH for the periodic transfer.
649*4882a593Smuzhiyun  * @only_one_period: If true we will limit ourselves to just looking at
650*4882a593Smuzhiyun  *                   one period (aka one 100us chunk).  This is used if we have
651*4882a593Smuzhiyun  *                   already scheduled something on the low speed schedule and
652*4882a593Smuzhiyun  *                   need to find something that matches on the high speed one.
653*4882a593Smuzhiyun  * @index:           The index into qh->hs_transfers that we're working with.
654*4882a593Smuzhiyun  *
655*4882a593Smuzhiyun  * Returns: 0 for success or an error code.  Upon success the
656*4882a593Smuzhiyun  *          dwc2_hs_transfer_time specified by "index" will be updated.
657*4882a593Smuzhiyun  */
dwc2_hs_pmap_schedule(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh,bool only_one_period,int index)658*4882a593Smuzhiyun static int dwc2_hs_pmap_schedule(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
659*4882a593Smuzhiyun 				 bool only_one_period, int index)
660*4882a593Smuzhiyun {
661*4882a593Smuzhiyun 	struct dwc2_hs_transfer_time *trans_time = qh->hs_transfers + index;
662*4882a593Smuzhiyun 	int us;
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun 	us = pmap_schedule(hsotg->hs_periodic_bitmap,
665*4882a593Smuzhiyun 			   DWC2_HS_PERIODIC_US_PER_UFRAME,
666*4882a593Smuzhiyun 			   DWC2_HS_SCHEDULE_UFRAMES, trans_time->duration_us,
667*4882a593Smuzhiyun 			   qh->host_interval, trans_time->start_schedule_us,
668*4882a593Smuzhiyun 			   only_one_period);
669*4882a593Smuzhiyun 
670*4882a593Smuzhiyun 	if (us < 0)
671*4882a593Smuzhiyun 		return us;
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun 	trans_time->start_schedule_us = us;
674*4882a593Smuzhiyun 	return 0;
675*4882a593Smuzhiyun }
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun /**
678*4882a593Smuzhiyun  * dwc2_ls_pmap_unschedule() - Undo work done by dwc2_hs_pmap_schedule()
679*4882a593Smuzhiyun  *
680*4882a593Smuzhiyun  * @hsotg:       The HCD state structure for the DWC OTG controller.
681*4882a593Smuzhiyun  * @qh:          QH for the periodic transfer.
682*4882a593Smuzhiyun  * @index:       Transfer index
683*4882a593Smuzhiyun  */
dwc2_hs_pmap_unschedule(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh,int index)684*4882a593Smuzhiyun static void dwc2_hs_pmap_unschedule(struct dwc2_hsotg *hsotg,
685*4882a593Smuzhiyun 				    struct dwc2_qh *qh, int index)
686*4882a593Smuzhiyun {
687*4882a593Smuzhiyun 	struct dwc2_hs_transfer_time *trans_time = qh->hs_transfers + index;
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun 	pmap_unschedule(hsotg->hs_periodic_bitmap,
690*4882a593Smuzhiyun 			DWC2_HS_PERIODIC_US_PER_UFRAME,
691*4882a593Smuzhiyun 			DWC2_HS_SCHEDULE_UFRAMES, trans_time->duration_us,
692*4882a593Smuzhiyun 			qh->host_interval, trans_time->start_schedule_us);
693*4882a593Smuzhiyun }
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun /**
696*4882a593Smuzhiyun  * dwc2_uframe_schedule_split - Schedule a QH for a periodic split xfer.
697*4882a593Smuzhiyun  *
698*4882a593Smuzhiyun  * This is the most complicated thing in USB.  We have to find matching time
699*4882a593Smuzhiyun  * in both the global high speed schedule for the port and the low speed
700*4882a593Smuzhiyun  * schedule for the TT associated with the given device.
701*4882a593Smuzhiyun  *
702*4882a593Smuzhiyun  * Being here means that the host must be running in high speed mode and the
703*4882a593Smuzhiyun  * device is in low or full speed mode (and behind a hub).
704*4882a593Smuzhiyun  *
705*4882a593Smuzhiyun  * @hsotg:       The HCD state structure for the DWC OTG controller.
706*4882a593Smuzhiyun  * @qh:          QH for the periodic transfer.
707*4882a593Smuzhiyun  */
dwc2_uframe_schedule_split(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)708*4882a593Smuzhiyun static int dwc2_uframe_schedule_split(struct dwc2_hsotg *hsotg,
709*4882a593Smuzhiyun 				      struct dwc2_qh *qh)
710*4882a593Smuzhiyun {
711*4882a593Smuzhiyun 	int bytecount = qh->maxp_mult * qh->maxp;
712*4882a593Smuzhiyun 	int ls_search_slice;
713*4882a593Smuzhiyun 	int err = 0;
714*4882a593Smuzhiyun 	int host_interval_in_sched;
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun 	/*
717*4882a593Smuzhiyun 	 * The interval (how often to repeat) in the actual host schedule.
718*4882a593Smuzhiyun 	 * See pmap_schedule() for gcd() explanation.
719*4882a593Smuzhiyun 	 */
720*4882a593Smuzhiyun 	host_interval_in_sched = gcd(qh->host_interval,
721*4882a593Smuzhiyun 				     DWC2_HS_SCHEDULE_UFRAMES);
722*4882a593Smuzhiyun 
723*4882a593Smuzhiyun 	/*
724*4882a593Smuzhiyun 	 * We always try to find space in the low speed schedule first, then
725*4882a593Smuzhiyun 	 * try to find high speed time that matches.  If we don't, we'll bump
726*4882a593Smuzhiyun 	 * up the place we start searching in the low speed schedule and try
727*4882a593Smuzhiyun 	 * again.  To start we'll look right at the beginning of the low speed
728*4882a593Smuzhiyun 	 * schedule.
729*4882a593Smuzhiyun 	 *
730*4882a593Smuzhiyun 	 * Note that this will tend to front-load the high speed schedule.
731*4882a593Smuzhiyun 	 * We may eventually want to try to avoid this by either considering
732*4882a593Smuzhiyun 	 * both schedules together or doing some sort of round robin.
733*4882a593Smuzhiyun 	 *
734*4882a593Smuzhiyun 	 * For isoc split out, start schedule at the 2 * DWC2_SLICES_PER_UFRAME
735*4882a593Smuzhiyun 	 * to transfer SSPLIT-begin OUT transaction like EHCI controller.
736*4882a593Smuzhiyun 	 */
737*4882a593Smuzhiyun 	if (qh->ep_type == USB_ENDPOINT_XFER_ISOC && !qh->ep_is_in)
738*4882a593Smuzhiyun 		ls_search_slice = 2 * DWC2_SLICES_PER_UFRAME;
739*4882a593Smuzhiyun 	else
740*4882a593Smuzhiyun 		ls_search_slice = 0;
741*4882a593Smuzhiyun 
742*4882a593Smuzhiyun 	while (ls_search_slice < DWC2_LS_SCHEDULE_SLICES) {
743*4882a593Smuzhiyun 		int start_s_uframe;
744*4882a593Smuzhiyun 		int ssplit_s_uframe;
745*4882a593Smuzhiyun 		int second_s_uframe;
746*4882a593Smuzhiyun 		int rel_uframe;
747*4882a593Smuzhiyun 		int first_count;
748*4882a593Smuzhiyun 		int middle_count;
749*4882a593Smuzhiyun 		int end_count;
750*4882a593Smuzhiyun 		int first_data_bytes;
751*4882a593Smuzhiyun 		int other_data_bytes;
752*4882a593Smuzhiyun 		int i;
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun 		if (qh->schedule_low_speed) {
755*4882a593Smuzhiyun 			err = dwc2_ls_pmap_schedule(hsotg, qh, ls_search_slice);
756*4882a593Smuzhiyun 
757*4882a593Smuzhiyun 			/*
758*4882a593Smuzhiyun 			 * If we got an error here there's no other magic we
759*4882a593Smuzhiyun 			 * can do, so bail.  All the looping above is only
760*4882a593Smuzhiyun 			 * helpful to redo things if we got a low speed slot
761*4882a593Smuzhiyun 			 * and then couldn't find a matching high speed slot.
762*4882a593Smuzhiyun 			 */
763*4882a593Smuzhiyun 			if (err)
764*4882a593Smuzhiyun 				return err;
765*4882a593Smuzhiyun 		} else {
766*4882a593Smuzhiyun 			/* Must be missing the tt structure?  Why? */
767*4882a593Smuzhiyun 			WARN_ON_ONCE(1);
768*4882a593Smuzhiyun 		}
769*4882a593Smuzhiyun 
770*4882a593Smuzhiyun 		/*
771*4882a593Smuzhiyun 		 * This will give us a number 0 - 7 if
772*4882a593Smuzhiyun 		 * DWC2_LS_SCHEDULE_FRAMES == 1, or 0 - 15 if == 2, or ...
773*4882a593Smuzhiyun 		 */
774*4882a593Smuzhiyun 		start_s_uframe = qh->ls_start_schedule_slice /
775*4882a593Smuzhiyun 				 DWC2_SLICES_PER_UFRAME;
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun 		/* Get a number that's always 0 - 7 */
778*4882a593Smuzhiyun 		rel_uframe = (start_s_uframe % 8);
779*4882a593Smuzhiyun 
780*4882a593Smuzhiyun 		/*
781*4882a593Smuzhiyun 		 * If we were going to start in uframe 7 then we would need to
782*4882a593Smuzhiyun 		 * issue a start split in uframe 6, which spec says is not OK.
783*4882a593Smuzhiyun 		 * Move on to the next full frame (assuming there is one).
784*4882a593Smuzhiyun 		 *
785*4882a593Smuzhiyun 		 * See 11.18.4 Host Split Transaction Scheduling Requirements
786*4882a593Smuzhiyun 		 * bullet 1.
787*4882a593Smuzhiyun 		 */
788*4882a593Smuzhiyun 		if (rel_uframe == 7) {
789*4882a593Smuzhiyun 			if (qh->schedule_low_speed)
790*4882a593Smuzhiyun 				dwc2_ls_pmap_unschedule(hsotg, qh);
791*4882a593Smuzhiyun 			ls_search_slice =
792*4882a593Smuzhiyun 				(qh->ls_start_schedule_slice /
793*4882a593Smuzhiyun 				 DWC2_LS_PERIODIC_SLICES_PER_FRAME + 1) *
794*4882a593Smuzhiyun 				DWC2_LS_PERIODIC_SLICES_PER_FRAME;
795*4882a593Smuzhiyun 			continue;
796*4882a593Smuzhiyun 		}
797*4882a593Smuzhiyun 
798*4882a593Smuzhiyun 		/*
799*4882a593Smuzhiyun 		 * For ISOC in:
800*4882a593Smuzhiyun 		 * - start split            (frame -1)
801*4882a593Smuzhiyun 		 * - complete split w/ data (frame +1)
802*4882a593Smuzhiyun 		 * - complete split w/ data (frame +2)
803*4882a593Smuzhiyun 		 * - ...
804*4882a593Smuzhiyun 		 * - complete split w/ data (frame +num_data_packets)
805*4882a593Smuzhiyun 		 * - complete split w/ data (frame +num_data_packets+1)
806*4882a593Smuzhiyun 		 * - complete split w/ data (frame +num_data_packets+2, max 8)
807*4882a593Smuzhiyun 		 *   ...though if frame was "0" then max is 7...
808*4882a593Smuzhiyun 		 *
809*4882a593Smuzhiyun 		 * For ISOC out we might need to do:
810*4882a593Smuzhiyun 		 * - start split w/ data    (frame -1)
811*4882a593Smuzhiyun 		 * - start split w/ data    (frame +0)
812*4882a593Smuzhiyun 		 * - ...
813*4882a593Smuzhiyun 		 * - start split w/ data    (frame +num_data_packets-2)
814*4882a593Smuzhiyun 		 *
815*4882a593Smuzhiyun 		 * For INTERRUPT in we might need to do:
816*4882a593Smuzhiyun 		 * - start split            (frame -1)
817*4882a593Smuzhiyun 		 * - complete split w/ data (frame +1)
818*4882a593Smuzhiyun 		 * - complete split w/ data (frame +2)
819*4882a593Smuzhiyun 		 * - complete split w/ data (frame +3, max 8)
820*4882a593Smuzhiyun 		 *
821*4882a593Smuzhiyun 		 * For INTERRUPT out we might need to do:
822*4882a593Smuzhiyun 		 * - start split w/ data    (frame -1)
823*4882a593Smuzhiyun 		 * - complete split         (frame +1)
824*4882a593Smuzhiyun 		 * - complete split         (frame +2)
825*4882a593Smuzhiyun 		 * - complete split         (frame +3, max 8)
826*4882a593Smuzhiyun 		 *
827*4882a593Smuzhiyun 		 * Start adjusting!
828*4882a593Smuzhiyun 		 */
829*4882a593Smuzhiyun 		ssplit_s_uframe = (start_s_uframe +
830*4882a593Smuzhiyun 				   host_interval_in_sched - 1) %
831*4882a593Smuzhiyun 				  host_interval_in_sched;
832*4882a593Smuzhiyun 		if (qh->ep_type == USB_ENDPOINT_XFER_ISOC && !qh->ep_is_in)
833*4882a593Smuzhiyun 			second_s_uframe = start_s_uframe;
834*4882a593Smuzhiyun 		else
835*4882a593Smuzhiyun 			second_s_uframe = start_s_uframe + 1;
836*4882a593Smuzhiyun 
837*4882a593Smuzhiyun 		/* First data transfer might not be all 188 bytes. */
838*4882a593Smuzhiyun 		first_data_bytes = 188 -
839*4882a593Smuzhiyun 			DIV_ROUND_UP(188 * (qh->ls_start_schedule_slice %
840*4882a593Smuzhiyun 					    DWC2_SLICES_PER_UFRAME),
841*4882a593Smuzhiyun 				     DWC2_SLICES_PER_UFRAME);
842*4882a593Smuzhiyun 		if (first_data_bytes > bytecount)
843*4882a593Smuzhiyun 			first_data_bytes = bytecount;
844*4882a593Smuzhiyun 		other_data_bytes = bytecount - first_data_bytes;
845*4882a593Smuzhiyun 
846*4882a593Smuzhiyun 		/*
847*4882a593Smuzhiyun 		 * For now, skip OUT xfers where first xfer is partial
848*4882a593Smuzhiyun 		 *
849*4882a593Smuzhiyun 		 * Main dwc2 code assumes:
850*4882a593Smuzhiyun 		 * - INT transfers never get split in two.
851*4882a593Smuzhiyun 		 * - ISOC transfers can always transfer 188 bytes the first
852*4882a593Smuzhiyun 		 *   time.
853*4882a593Smuzhiyun 		 *
854*4882a593Smuzhiyun 		 * Until that code is fixed, try again if the first transfer
855*4882a593Smuzhiyun 		 * couldn't transfer everything.
856*4882a593Smuzhiyun 		 *
857*4882a593Smuzhiyun 		 * This code can be removed if/when the rest of dwc2 handles
858*4882a593Smuzhiyun 		 * the above cases.  Until it's fixed we just won't be able
859*4882a593Smuzhiyun 		 * to schedule quite as tightly.
860*4882a593Smuzhiyun 		 */
861*4882a593Smuzhiyun 		if (!qh->ep_is_in &&
862*4882a593Smuzhiyun 		    (first_data_bytes != min_t(int, 188, bytecount))) {
863*4882a593Smuzhiyun 			dwc2_sch_dbg(hsotg,
864*4882a593Smuzhiyun 				     "QH=%p avoiding broken 1st xfer (%d, %d)\n",
865*4882a593Smuzhiyun 				     qh, first_data_bytes, bytecount);
866*4882a593Smuzhiyun 			if (qh->schedule_low_speed)
867*4882a593Smuzhiyun 				dwc2_ls_pmap_unschedule(hsotg, qh);
868*4882a593Smuzhiyun 			ls_search_slice = (start_s_uframe + 1) *
869*4882a593Smuzhiyun 				DWC2_SLICES_PER_UFRAME;
870*4882a593Smuzhiyun 			continue;
871*4882a593Smuzhiyun 		}
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun 		/* Start by assuming transfers for the bytes */
874*4882a593Smuzhiyun 		qh->num_hs_transfers = 1 + DIV_ROUND_UP(other_data_bytes, 188);
875*4882a593Smuzhiyun 
876*4882a593Smuzhiyun 		/*
877*4882a593Smuzhiyun 		 * Everything except ISOC OUT has extra transfers.  Rules are
878*4882a593Smuzhiyun 		 * complicated.  See 11.18.4 Host Split Transaction Scheduling
879*4882a593Smuzhiyun 		 * Requirements bullet 3.
880*4882a593Smuzhiyun 		 */
881*4882a593Smuzhiyun 		if (qh->ep_type == USB_ENDPOINT_XFER_INT) {
882*4882a593Smuzhiyun 			if (rel_uframe == 6)
883*4882a593Smuzhiyun 				qh->num_hs_transfers += 2;
884*4882a593Smuzhiyun 			else
885*4882a593Smuzhiyun 				qh->num_hs_transfers += 3;
886*4882a593Smuzhiyun 
887*4882a593Smuzhiyun 			if (qh->ep_is_in) {
888*4882a593Smuzhiyun 				/*
889*4882a593Smuzhiyun 				 * First is start split, middle/end is data.
890*4882a593Smuzhiyun 				 * Allocate full data bytes for all data.
891*4882a593Smuzhiyun 				 */
892*4882a593Smuzhiyun 				first_count = 4;
893*4882a593Smuzhiyun 				middle_count = bytecount;
894*4882a593Smuzhiyun 				end_count = bytecount;
895*4882a593Smuzhiyun 			} else {
896*4882a593Smuzhiyun 				/*
897*4882a593Smuzhiyun 				 * First is data, middle/end is complete.
898*4882a593Smuzhiyun 				 * First transfer and second can have data.
899*4882a593Smuzhiyun 				 * Rest should just have complete split.
900*4882a593Smuzhiyun 				 */
901*4882a593Smuzhiyun 				first_count = first_data_bytes;
902*4882a593Smuzhiyun 				middle_count = max_t(int, 4, other_data_bytes);
903*4882a593Smuzhiyun 				end_count = 4;
904*4882a593Smuzhiyun 			}
905*4882a593Smuzhiyun 		} else {
906*4882a593Smuzhiyun 			if (qh->ep_is_in) {
907*4882a593Smuzhiyun 				int last;
908*4882a593Smuzhiyun 
909*4882a593Smuzhiyun 				/* Account for the start split */
910*4882a593Smuzhiyun 				qh->num_hs_transfers++;
911*4882a593Smuzhiyun 
912*4882a593Smuzhiyun 				/* Calculate "L" value from spec */
913*4882a593Smuzhiyun 				last = rel_uframe + qh->num_hs_transfers + 1;
914*4882a593Smuzhiyun 
915*4882a593Smuzhiyun 				/* Start with basic case */
916*4882a593Smuzhiyun 				if (last <= 6)
917*4882a593Smuzhiyun 					qh->num_hs_transfers += 2;
918*4882a593Smuzhiyun 				else
919*4882a593Smuzhiyun 					qh->num_hs_transfers += 1;
920*4882a593Smuzhiyun 
921*4882a593Smuzhiyun 				/* Adjust downwards */
922*4882a593Smuzhiyun 				if (last >= 6 && rel_uframe == 0)
923*4882a593Smuzhiyun 					qh->num_hs_transfers--;
924*4882a593Smuzhiyun 
925*4882a593Smuzhiyun 				/* 1st = start; rest can contain data */
926*4882a593Smuzhiyun 				first_count = 4;
927*4882a593Smuzhiyun 				middle_count = min_t(int, 188, bytecount);
928*4882a593Smuzhiyun 				end_count = middle_count;
929*4882a593Smuzhiyun 			} else {
930*4882a593Smuzhiyun 				/* All contain data, last might be smaller */
931*4882a593Smuzhiyun 				first_count = first_data_bytes;
932*4882a593Smuzhiyun 				middle_count = min_t(int, 188,
933*4882a593Smuzhiyun 						     other_data_bytes);
934*4882a593Smuzhiyun 				end_count = other_data_bytes % 188;
935*4882a593Smuzhiyun 			}
936*4882a593Smuzhiyun 		}
937*4882a593Smuzhiyun 
938*4882a593Smuzhiyun 		/* Assign durations per uFrame */
939*4882a593Smuzhiyun 		qh->hs_transfers[0].duration_us = HS_USECS_ISO(first_count);
940*4882a593Smuzhiyun 		for (i = 1; i < qh->num_hs_transfers - 1; i++)
941*4882a593Smuzhiyun 			qh->hs_transfers[i].duration_us =
942*4882a593Smuzhiyun 				HS_USECS_ISO(middle_count);
943*4882a593Smuzhiyun 		if (qh->num_hs_transfers > 1)
944*4882a593Smuzhiyun 			qh->hs_transfers[qh->num_hs_transfers - 1].duration_us =
945*4882a593Smuzhiyun 				HS_USECS_ISO(end_count);
946*4882a593Smuzhiyun 
947*4882a593Smuzhiyun 		/*
948*4882a593Smuzhiyun 		 * Assign start us.  The call below to dwc2_hs_pmap_schedule()
949*4882a593Smuzhiyun 		 * will start with these numbers but may adjust within the same
950*4882a593Smuzhiyun 		 * microframe.
951*4882a593Smuzhiyun 		 */
952*4882a593Smuzhiyun 		qh->hs_transfers[0].start_schedule_us =
953*4882a593Smuzhiyun 			ssplit_s_uframe * DWC2_HS_PERIODIC_US_PER_UFRAME;
954*4882a593Smuzhiyun 		for (i = 1; i < qh->num_hs_transfers; i++)
955*4882a593Smuzhiyun 			qh->hs_transfers[i].start_schedule_us =
956*4882a593Smuzhiyun 				((second_s_uframe + i - 1) %
957*4882a593Smuzhiyun 				 DWC2_HS_SCHEDULE_UFRAMES) *
958*4882a593Smuzhiyun 				DWC2_HS_PERIODIC_US_PER_UFRAME;
959*4882a593Smuzhiyun 
960*4882a593Smuzhiyun 		/* Try to schedule with filled in hs_transfers above */
961*4882a593Smuzhiyun 		for (i = 0; i < qh->num_hs_transfers; i++) {
962*4882a593Smuzhiyun 			err = dwc2_hs_pmap_schedule(hsotg, qh, true, i);
963*4882a593Smuzhiyun 			if (err)
964*4882a593Smuzhiyun 				break;
965*4882a593Smuzhiyun 		}
966*4882a593Smuzhiyun 
967*4882a593Smuzhiyun 		/* If we scheduled all w/out breaking out then we're all good */
968*4882a593Smuzhiyun 		if (i == qh->num_hs_transfers)
969*4882a593Smuzhiyun 			break;
970*4882a593Smuzhiyun 
971*4882a593Smuzhiyun 		for (; i >= 0; i--)
972*4882a593Smuzhiyun 			dwc2_hs_pmap_unschedule(hsotg, qh, i);
973*4882a593Smuzhiyun 
974*4882a593Smuzhiyun 		if (qh->schedule_low_speed)
975*4882a593Smuzhiyun 			dwc2_ls_pmap_unschedule(hsotg, qh);
976*4882a593Smuzhiyun 
977*4882a593Smuzhiyun 		/* Try again starting in the next microframe */
978*4882a593Smuzhiyun 		ls_search_slice = (start_s_uframe + 1) * DWC2_SLICES_PER_UFRAME;
979*4882a593Smuzhiyun 	}
980*4882a593Smuzhiyun 
981*4882a593Smuzhiyun 	if (ls_search_slice >= DWC2_LS_SCHEDULE_SLICES)
982*4882a593Smuzhiyun 		return -ENOSPC;
983*4882a593Smuzhiyun 
984*4882a593Smuzhiyun 	return 0;
985*4882a593Smuzhiyun }
986*4882a593Smuzhiyun 
987*4882a593Smuzhiyun /**
988*4882a593Smuzhiyun  * dwc2_uframe_schedule_hs - Schedule a QH for a periodic high speed xfer.
989*4882a593Smuzhiyun  *
990*4882a593Smuzhiyun  * Basically this just wraps dwc2_hs_pmap_schedule() to provide a clean
991*4882a593Smuzhiyun  * interface.
992*4882a593Smuzhiyun  *
993*4882a593Smuzhiyun  * @hsotg:       The HCD state structure for the DWC OTG controller.
994*4882a593Smuzhiyun  * @qh:          QH for the periodic transfer.
995*4882a593Smuzhiyun  */
dwc2_uframe_schedule_hs(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)996*4882a593Smuzhiyun static int dwc2_uframe_schedule_hs(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
997*4882a593Smuzhiyun {
998*4882a593Smuzhiyun 	/* In non-split host and device time are the same */
999*4882a593Smuzhiyun 	WARN_ON(qh->host_us != qh->device_us);
1000*4882a593Smuzhiyun 	WARN_ON(qh->host_interval != qh->device_interval);
1001*4882a593Smuzhiyun 	WARN_ON(qh->num_hs_transfers != 1);
1002*4882a593Smuzhiyun 
1003*4882a593Smuzhiyun 	/* We'll have one transfer; init start to 0 before calling scheduler */
1004*4882a593Smuzhiyun 	qh->hs_transfers[0].start_schedule_us = 0;
1005*4882a593Smuzhiyun 	qh->hs_transfers[0].duration_us = qh->host_us;
1006*4882a593Smuzhiyun 
1007*4882a593Smuzhiyun 	return dwc2_hs_pmap_schedule(hsotg, qh, false, 0);
1008*4882a593Smuzhiyun }
1009*4882a593Smuzhiyun 
1010*4882a593Smuzhiyun /**
1011*4882a593Smuzhiyun  * dwc2_uframe_schedule_ls - Schedule a QH for a periodic low/full speed xfer.
1012*4882a593Smuzhiyun  *
1013*4882a593Smuzhiyun  * Basically this just wraps dwc2_ls_pmap_schedule() to provide a clean
1014*4882a593Smuzhiyun  * interface.
1015*4882a593Smuzhiyun  *
1016*4882a593Smuzhiyun  * @hsotg:       The HCD state structure for the DWC OTG controller.
1017*4882a593Smuzhiyun  * @qh:          QH for the periodic transfer.
1018*4882a593Smuzhiyun  */
dwc2_uframe_schedule_ls(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)1019*4882a593Smuzhiyun static int dwc2_uframe_schedule_ls(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1020*4882a593Smuzhiyun {
1021*4882a593Smuzhiyun 	/* In non-split host and device time are the same */
1022*4882a593Smuzhiyun 	WARN_ON(qh->host_us != qh->device_us);
1023*4882a593Smuzhiyun 	WARN_ON(qh->host_interval != qh->device_interval);
1024*4882a593Smuzhiyun 	WARN_ON(!qh->schedule_low_speed);
1025*4882a593Smuzhiyun 
1026*4882a593Smuzhiyun 	/* Run on the main low speed schedule (no split = no hub = no TT) */
1027*4882a593Smuzhiyun 	return dwc2_ls_pmap_schedule(hsotg, qh, 0);
1028*4882a593Smuzhiyun }
1029*4882a593Smuzhiyun 
1030*4882a593Smuzhiyun /**
1031*4882a593Smuzhiyun  * dwc2_uframe_schedule - Schedule a QH for a periodic xfer.
1032*4882a593Smuzhiyun  *
1033*4882a593Smuzhiyun  * Calls one of the 3 sub-function depending on what type of transfer this QH
1034*4882a593Smuzhiyun  * is for.  Also adds some printing.
1035*4882a593Smuzhiyun  *
1036*4882a593Smuzhiyun  * @hsotg:       The HCD state structure for the DWC OTG controller.
1037*4882a593Smuzhiyun  * @qh:          QH for the periodic transfer.
1038*4882a593Smuzhiyun  */
dwc2_uframe_schedule(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)1039*4882a593Smuzhiyun static int dwc2_uframe_schedule(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1040*4882a593Smuzhiyun {
1041*4882a593Smuzhiyun 	int ret;
1042*4882a593Smuzhiyun 
1043*4882a593Smuzhiyun 	if (qh->dev_speed == USB_SPEED_HIGH)
1044*4882a593Smuzhiyun 		ret = dwc2_uframe_schedule_hs(hsotg, qh);
1045*4882a593Smuzhiyun 	else if (!qh->do_split)
1046*4882a593Smuzhiyun 		ret = dwc2_uframe_schedule_ls(hsotg, qh);
1047*4882a593Smuzhiyun 	else
1048*4882a593Smuzhiyun 		ret = dwc2_uframe_schedule_split(hsotg, qh);
1049*4882a593Smuzhiyun 
1050*4882a593Smuzhiyun 	if (ret)
1051*4882a593Smuzhiyun 		dwc2_sch_dbg(hsotg, "QH=%p Failed to schedule %d\n", qh, ret);
1052*4882a593Smuzhiyun 	else
1053*4882a593Smuzhiyun 		dwc2_qh_schedule_print(hsotg, qh);
1054*4882a593Smuzhiyun 
1055*4882a593Smuzhiyun 	return ret;
1056*4882a593Smuzhiyun }
1057*4882a593Smuzhiyun 
1058*4882a593Smuzhiyun /**
1059*4882a593Smuzhiyun  * dwc2_uframe_unschedule - Undoes dwc2_uframe_schedule().
1060*4882a593Smuzhiyun  *
1061*4882a593Smuzhiyun  * @hsotg:       The HCD state structure for the DWC OTG controller.
1062*4882a593Smuzhiyun  * @qh:          QH for the periodic transfer.
1063*4882a593Smuzhiyun  */
dwc2_uframe_unschedule(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)1064*4882a593Smuzhiyun static void dwc2_uframe_unschedule(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1065*4882a593Smuzhiyun {
1066*4882a593Smuzhiyun 	int i;
1067*4882a593Smuzhiyun 
1068*4882a593Smuzhiyun 	for (i = 0; i < qh->num_hs_transfers; i++)
1069*4882a593Smuzhiyun 		dwc2_hs_pmap_unschedule(hsotg, qh, i);
1070*4882a593Smuzhiyun 
1071*4882a593Smuzhiyun 	if (qh->schedule_low_speed)
1072*4882a593Smuzhiyun 		dwc2_ls_pmap_unschedule(hsotg, qh);
1073*4882a593Smuzhiyun 
1074*4882a593Smuzhiyun 	dwc2_sch_dbg(hsotg, "QH=%p Unscheduled\n", qh);
1075*4882a593Smuzhiyun }
1076*4882a593Smuzhiyun 
1077*4882a593Smuzhiyun /**
1078*4882a593Smuzhiyun  * dwc2_pick_first_frame() - Choose 1st frame for qh that's already scheduled
1079*4882a593Smuzhiyun  *
1080*4882a593Smuzhiyun  * Takes a qh that has already been scheduled (which means we know we have the
1081*4882a593Smuzhiyun  * bandwdith reserved for us) and set the next_active_frame and the
1082*4882a593Smuzhiyun  * start_active_frame.
1083*4882a593Smuzhiyun  *
1084*4882a593Smuzhiyun  * This is expected to be called on qh's that weren't previously actively
1085*4882a593Smuzhiyun  * running.  It just picks the next frame that we can fit into without any
1086*4882a593Smuzhiyun  * thought about the past.
1087*4882a593Smuzhiyun  *
1088*4882a593Smuzhiyun  * @hsotg: The HCD state structure for the DWC OTG controller
1089*4882a593Smuzhiyun  * @qh:    QH for a periodic endpoint
1090*4882a593Smuzhiyun  *
1091*4882a593Smuzhiyun  */
dwc2_pick_first_frame(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)1092*4882a593Smuzhiyun static void dwc2_pick_first_frame(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1093*4882a593Smuzhiyun {
1094*4882a593Smuzhiyun 	u16 frame_number;
1095*4882a593Smuzhiyun 	u16 earliest_frame;
1096*4882a593Smuzhiyun 	u16 next_active_frame;
1097*4882a593Smuzhiyun 	u16 relative_frame;
1098*4882a593Smuzhiyun 	u16 interval;
1099*4882a593Smuzhiyun 
1100*4882a593Smuzhiyun 	/*
1101*4882a593Smuzhiyun 	 * Use the real frame number rather than the cached value as of the
1102*4882a593Smuzhiyun 	 * last SOF to give us a little extra slop.
1103*4882a593Smuzhiyun 	 */
1104*4882a593Smuzhiyun 	frame_number = dwc2_hcd_get_frame_number(hsotg);
1105*4882a593Smuzhiyun 
1106*4882a593Smuzhiyun 	/*
1107*4882a593Smuzhiyun 	 * We wouldn't want to start any earlier than the next frame just in
1108*4882a593Smuzhiyun 	 * case the frame number ticks as we're doing this calculation.
1109*4882a593Smuzhiyun 	 *
1110*4882a593Smuzhiyun 	 * NOTE: if we could quantify how long till we actually get scheduled
1111*4882a593Smuzhiyun 	 * we might be able to avoid the "+ 1" by looking at the upper part of
1112*4882a593Smuzhiyun 	 * HFNUM (the FRREM field).  For now we'll just use the + 1 though.
1113*4882a593Smuzhiyun 	 */
1114*4882a593Smuzhiyun 	earliest_frame = dwc2_frame_num_inc(frame_number, 1);
1115*4882a593Smuzhiyun 	next_active_frame = earliest_frame;
1116*4882a593Smuzhiyun 
1117*4882a593Smuzhiyun 	/* Get the "no microframe schduler" out of the way... */
1118*4882a593Smuzhiyun 	if (!hsotg->params.uframe_sched) {
1119*4882a593Smuzhiyun 		if (qh->do_split)
1120*4882a593Smuzhiyun 			/* Splits are active at microframe 0 minus 1 */
1121*4882a593Smuzhiyun 			next_active_frame |= 0x7;
1122*4882a593Smuzhiyun 		goto exit;
1123*4882a593Smuzhiyun 	}
1124*4882a593Smuzhiyun 
1125*4882a593Smuzhiyun 	if (qh->dev_speed == USB_SPEED_HIGH || qh->do_split) {
1126*4882a593Smuzhiyun 		/*
1127*4882a593Smuzhiyun 		 * We're either at high speed or we're doing a split (which
1128*4882a593Smuzhiyun 		 * means we're talking high speed to a hub).  In any case
1129*4882a593Smuzhiyun 		 * the first frame should be based on when the first scheduled
1130*4882a593Smuzhiyun 		 * event is.
1131*4882a593Smuzhiyun 		 */
1132*4882a593Smuzhiyun 		WARN_ON(qh->num_hs_transfers < 1);
1133*4882a593Smuzhiyun 
1134*4882a593Smuzhiyun 		relative_frame = qh->hs_transfers[0].start_schedule_us /
1135*4882a593Smuzhiyun 				 DWC2_HS_PERIODIC_US_PER_UFRAME;
1136*4882a593Smuzhiyun 
1137*4882a593Smuzhiyun 		/* Adjust interval as per high speed schedule */
1138*4882a593Smuzhiyun 		interval = gcd(qh->host_interval, DWC2_HS_SCHEDULE_UFRAMES);
1139*4882a593Smuzhiyun 
1140*4882a593Smuzhiyun 	} else {
1141*4882a593Smuzhiyun 		/*
1142*4882a593Smuzhiyun 		 * Low or full speed directly on dwc2.  Just about the same
1143*4882a593Smuzhiyun 		 * as high speed but on a different schedule and with slightly
1144*4882a593Smuzhiyun 		 * different adjustments.  Note that this works because when
1145*4882a593Smuzhiyun 		 * the host and device are both low speed then frames in the
1146*4882a593Smuzhiyun 		 * controller tick at low speed.
1147*4882a593Smuzhiyun 		 */
1148*4882a593Smuzhiyun 		relative_frame = qh->ls_start_schedule_slice /
1149*4882a593Smuzhiyun 				 DWC2_LS_PERIODIC_SLICES_PER_FRAME;
1150*4882a593Smuzhiyun 		interval = gcd(qh->host_interval, DWC2_LS_SCHEDULE_FRAMES);
1151*4882a593Smuzhiyun 	}
1152*4882a593Smuzhiyun 
1153*4882a593Smuzhiyun 	/* Scheduler messed up if frame is past interval */
1154*4882a593Smuzhiyun 	WARN_ON(relative_frame >= interval);
1155*4882a593Smuzhiyun 
1156*4882a593Smuzhiyun 	/*
1157*4882a593Smuzhiyun 	 * We know interval must divide (HFNUM_MAX_FRNUM + 1) now that we've
1158*4882a593Smuzhiyun 	 * done the gcd(), so it's safe to move to the beginning of the current
1159*4882a593Smuzhiyun 	 * interval like this.
1160*4882a593Smuzhiyun 	 *
1161*4882a593Smuzhiyun 	 * After this we might be before earliest_frame, but don't worry,
1162*4882a593Smuzhiyun 	 * we'll fix it...
1163*4882a593Smuzhiyun 	 */
1164*4882a593Smuzhiyun 	next_active_frame = (next_active_frame / interval) * interval;
1165*4882a593Smuzhiyun 
1166*4882a593Smuzhiyun 	/*
1167*4882a593Smuzhiyun 	 * Actually choose to start at the frame number we've been
1168*4882a593Smuzhiyun 	 * scheduled for.
1169*4882a593Smuzhiyun 	 */
1170*4882a593Smuzhiyun 	next_active_frame = dwc2_frame_num_inc(next_active_frame,
1171*4882a593Smuzhiyun 					       relative_frame);
1172*4882a593Smuzhiyun 
1173*4882a593Smuzhiyun 	/*
1174*4882a593Smuzhiyun 	 * We actually need 1 frame before since the next_active_frame is
1175*4882a593Smuzhiyun 	 * the frame number we'll be put on the ready list and we won't be on
1176*4882a593Smuzhiyun 	 * the bus until 1 frame later.
1177*4882a593Smuzhiyun 	 */
1178*4882a593Smuzhiyun 	next_active_frame = dwc2_frame_num_dec(next_active_frame, 1);
1179*4882a593Smuzhiyun 
1180*4882a593Smuzhiyun 	/*
1181*4882a593Smuzhiyun 	 * By now we might actually be before the earliest_frame.  Let's move
1182*4882a593Smuzhiyun 	 * up intervals until we're not.
1183*4882a593Smuzhiyun 	 */
1184*4882a593Smuzhiyun 	while (dwc2_frame_num_gt(earliest_frame, next_active_frame))
1185*4882a593Smuzhiyun 		next_active_frame = dwc2_frame_num_inc(next_active_frame,
1186*4882a593Smuzhiyun 						       interval);
1187*4882a593Smuzhiyun 
1188*4882a593Smuzhiyun exit:
1189*4882a593Smuzhiyun 	qh->next_active_frame = next_active_frame;
1190*4882a593Smuzhiyun 	qh->start_active_frame = next_active_frame;
1191*4882a593Smuzhiyun 
1192*4882a593Smuzhiyun 	dwc2_sch_vdbg(hsotg, "QH=%p First fn=%04x nxt=%04x\n",
1193*4882a593Smuzhiyun 		      qh, frame_number, qh->next_active_frame);
1194*4882a593Smuzhiyun }
1195*4882a593Smuzhiyun 
1196*4882a593Smuzhiyun /**
1197*4882a593Smuzhiyun  * dwc2_do_reserve() - Make a periodic reservation
1198*4882a593Smuzhiyun  *
1199*4882a593Smuzhiyun  * Try to allocate space in the periodic schedule.  Depending on parameters
1200*4882a593Smuzhiyun  * this might use the microframe scheduler or the dumb scheduler.
1201*4882a593Smuzhiyun  *
1202*4882a593Smuzhiyun  * @hsotg: The HCD state structure for the DWC OTG controller
1203*4882a593Smuzhiyun  * @qh:    QH for the periodic transfer.
1204*4882a593Smuzhiyun  *
1205*4882a593Smuzhiyun  * Returns: 0 upon success; error upon failure.
1206*4882a593Smuzhiyun  */
dwc2_do_reserve(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)1207*4882a593Smuzhiyun static int dwc2_do_reserve(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1208*4882a593Smuzhiyun {
1209*4882a593Smuzhiyun 	int status;
1210*4882a593Smuzhiyun 
1211*4882a593Smuzhiyun 	if (hsotg->params.uframe_sched) {
1212*4882a593Smuzhiyun 		status = dwc2_uframe_schedule(hsotg, qh);
1213*4882a593Smuzhiyun 	} else {
1214*4882a593Smuzhiyun 		status = dwc2_periodic_channel_available(hsotg);
1215*4882a593Smuzhiyun 		if (status) {
1216*4882a593Smuzhiyun 			dev_info(hsotg->dev,
1217*4882a593Smuzhiyun 				 "%s: No host channel available for periodic transfer\n",
1218*4882a593Smuzhiyun 				 __func__);
1219*4882a593Smuzhiyun 			return status;
1220*4882a593Smuzhiyun 		}
1221*4882a593Smuzhiyun 
1222*4882a593Smuzhiyun 		status = dwc2_check_periodic_bandwidth(hsotg, qh);
1223*4882a593Smuzhiyun 	}
1224*4882a593Smuzhiyun 
1225*4882a593Smuzhiyun 	if (status) {
1226*4882a593Smuzhiyun 		dev_dbg(hsotg->dev,
1227*4882a593Smuzhiyun 			"%s: Insufficient periodic bandwidth for periodic transfer\n",
1228*4882a593Smuzhiyun 			__func__);
1229*4882a593Smuzhiyun 		return status;
1230*4882a593Smuzhiyun 	}
1231*4882a593Smuzhiyun 
1232*4882a593Smuzhiyun 	if (!hsotg->params.uframe_sched)
1233*4882a593Smuzhiyun 		/* Reserve periodic channel */
1234*4882a593Smuzhiyun 		hsotg->periodic_channels++;
1235*4882a593Smuzhiyun 
1236*4882a593Smuzhiyun 	/* Update claimed usecs per (micro)frame */
1237*4882a593Smuzhiyun 	hsotg->periodic_usecs += qh->host_us;
1238*4882a593Smuzhiyun 
1239*4882a593Smuzhiyun 	dwc2_pick_first_frame(hsotg, qh);
1240*4882a593Smuzhiyun 
1241*4882a593Smuzhiyun 	return 0;
1242*4882a593Smuzhiyun }
1243*4882a593Smuzhiyun 
1244*4882a593Smuzhiyun /**
1245*4882a593Smuzhiyun  * dwc2_do_unreserve() - Actually release the periodic reservation
1246*4882a593Smuzhiyun  *
1247*4882a593Smuzhiyun  * This function actually releases the periodic bandwidth that was reserved
1248*4882a593Smuzhiyun  * by the given qh.
1249*4882a593Smuzhiyun  *
1250*4882a593Smuzhiyun  * @hsotg: The HCD state structure for the DWC OTG controller
1251*4882a593Smuzhiyun  * @qh:    QH for the periodic transfer.
1252*4882a593Smuzhiyun  */
dwc2_do_unreserve(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)1253*4882a593Smuzhiyun static void dwc2_do_unreserve(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1254*4882a593Smuzhiyun {
1255*4882a593Smuzhiyun 	assert_spin_locked(&hsotg->lock);
1256*4882a593Smuzhiyun 
1257*4882a593Smuzhiyun 	WARN_ON(!qh->unreserve_pending);
1258*4882a593Smuzhiyun 
1259*4882a593Smuzhiyun 	/* No more unreserve pending--we're doing it */
1260*4882a593Smuzhiyun 	qh->unreserve_pending = false;
1261*4882a593Smuzhiyun 
1262*4882a593Smuzhiyun 	if (WARN_ON(!list_empty(&qh->qh_list_entry)))
1263*4882a593Smuzhiyun 		list_del_init(&qh->qh_list_entry);
1264*4882a593Smuzhiyun 
1265*4882a593Smuzhiyun 	/* Update claimed usecs per (micro)frame */
1266*4882a593Smuzhiyun 	hsotg->periodic_usecs -= qh->host_us;
1267*4882a593Smuzhiyun 
1268*4882a593Smuzhiyun 	if (hsotg->params.uframe_sched) {
1269*4882a593Smuzhiyun 		dwc2_uframe_unschedule(hsotg, qh);
1270*4882a593Smuzhiyun 	} else {
1271*4882a593Smuzhiyun 		/* Release periodic channel reservation */
1272*4882a593Smuzhiyun 		hsotg->periodic_channels--;
1273*4882a593Smuzhiyun 	}
1274*4882a593Smuzhiyun }
1275*4882a593Smuzhiyun 
1276*4882a593Smuzhiyun /**
1277*4882a593Smuzhiyun  * dwc2_unreserve_timer_fn() - Timer function to release periodic reservation
1278*4882a593Smuzhiyun  *
1279*4882a593Smuzhiyun  * According to the kernel doc for usb_submit_urb() (specifically the part about
1280*4882a593Smuzhiyun  * "Reserved Bandwidth Transfers"), we need to keep a reservation active as
1281*4882a593Smuzhiyun  * long as a device driver keeps submitting.  Since we're using HCD_BH to give
1282*4882a593Smuzhiyun  * back the URB we need to give the driver a little bit of time before we
1283*4882a593Smuzhiyun  * release the reservation.  This worker is called after the appropriate
1284*4882a593Smuzhiyun  * delay.
1285*4882a593Smuzhiyun  *
1286*4882a593Smuzhiyun  * @t: Address to a qh unreserve_work.
1287*4882a593Smuzhiyun  */
dwc2_unreserve_timer_fn(struct timer_list * t)1288*4882a593Smuzhiyun static void dwc2_unreserve_timer_fn(struct timer_list *t)
1289*4882a593Smuzhiyun {
1290*4882a593Smuzhiyun 	struct dwc2_qh *qh = from_timer(qh, t, unreserve_timer);
1291*4882a593Smuzhiyun 	struct dwc2_hsotg *hsotg = qh->hsotg;
1292*4882a593Smuzhiyun 	unsigned long flags;
1293*4882a593Smuzhiyun 
1294*4882a593Smuzhiyun 	/*
1295*4882a593Smuzhiyun 	 * Wait for the lock, or for us to be scheduled again.  We
1296*4882a593Smuzhiyun 	 * could be scheduled again if:
1297*4882a593Smuzhiyun 	 * - We started executing but didn't get the lock yet.
1298*4882a593Smuzhiyun 	 * - A new reservation came in, but cancel didn't take effect
1299*4882a593Smuzhiyun 	 *   because we already started executing.
1300*4882a593Smuzhiyun 	 * - The timer has been kicked again.
1301*4882a593Smuzhiyun 	 * In that case cancel and wait for the next call.
1302*4882a593Smuzhiyun 	 */
1303*4882a593Smuzhiyun 	while (!spin_trylock_irqsave(&hsotg->lock, flags)) {
1304*4882a593Smuzhiyun 		if (timer_pending(&qh->unreserve_timer))
1305*4882a593Smuzhiyun 			return;
1306*4882a593Smuzhiyun 	}
1307*4882a593Smuzhiyun 
1308*4882a593Smuzhiyun 	/*
1309*4882a593Smuzhiyun 	 * Might be no more unreserve pending if:
1310*4882a593Smuzhiyun 	 * - We started executing but didn't get the lock yet.
1311*4882a593Smuzhiyun 	 * - A new reservation came in, but cancel didn't take effect
1312*4882a593Smuzhiyun 	 *   because we already started executing.
1313*4882a593Smuzhiyun 	 *
1314*4882a593Smuzhiyun 	 * We can't put this in the loop above because unreserve_pending needs
1315*4882a593Smuzhiyun 	 * to be accessed under lock, so we can only check it once we got the
1316*4882a593Smuzhiyun 	 * lock.
1317*4882a593Smuzhiyun 	 */
1318*4882a593Smuzhiyun 	if (qh->unreserve_pending)
1319*4882a593Smuzhiyun 		dwc2_do_unreserve(hsotg, qh);
1320*4882a593Smuzhiyun 
1321*4882a593Smuzhiyun 	spin_unlock_irqrestore(&hsotg->lock, flags);
1322*4882a593Smuzhiyun }
1323*4882a593Smuzhiyun 
1324*4882a593Smuzhiyun /**
1325*4882a593Smuzhiyun  * dwc2_check_max_xfer_size() - Checks that the max transfer size allowed in a
1326*4882a593Smuzhiyun  * host channel is large enough to handle the maximum data transfer in a single
1327*4882a593Smuzhiyun  * (micro)frame for a periodic transfer
1328*4882a593Smuzhiyun  *
1329*4882a593Smuzhiyun  * @hsotg: The HCD state structure for the DWC OTG controller
1330*4882a593Smuzhiyun  * @qh:    QH for a periodic endpoint
1331*4882a593Smuzhiyun  *
1332*4882a593Smuzhiyun  * Return: 0 if successful, negative error code otherwise
1333*4882a593Smuzhiyun  */
dwc2_check_max_xfer_size(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)1334*4882a593Smuzhiyun static int dwc2_check_max_xfer_size(struct dwc2_hsotg *hsotg,
1335*4882a593Smuzhiyun 				    struct dwc2_qh *qh)
1336*4882a593Smuzhiyun {
1337*4882a593Smuzhiyun 	u32 max_xfer_size;
1338*4882a593Smuzhiyun 	u32 max_channel_xfer_size;
1339*4882a593Smuzhiyun 	int status = 0;
1340*4882a593Smuzhiyun 
1341*4882a593Smuzhiyun 	max_xfer_size = qh->maxp * qh->maxp_mult;
1342*4882a593Smuzhiyun 	max_channel_xfer_size = hsotg->params.max_transfer_size;
1343*4882a593Smuzhiyun 
1344*4882a593Smuzhiyun 	if (max_xfer_size > max_channel_xfer_size) {
1345*4882a593Smuzhiyun 		dev_err(hsotg->dev,
1346*4882a593Smuzhiyun 			"%s: Periodic xfer length %d > max xfer length for channel %d\n",
1347*4882a593Smuzhiyun 			__func__, max_xfer_size, max_channel_xfer_size);
1348*4882a593Smuzhiyun 		status = -ENOSPC;
1349*4882a593Smuzhiyun 	}
1350*4882a593Smuzhiyun 
1351*4882a593Smuzhiyun 	return status;
1352*4882a593Smuzhiyun }
1353*4882a593Smuzhiyun 
1354*4882a593Smuzhiyun /**
1355*4882a593Smuzhiyun  * dwc2_schedule_periodic() - Schedules an interrupt or isochronous transfer in
1356*4882a593Smuzhiyun  * the periodic schedule
1357*4882a593Smuzhiyun  *
1358*4882a593Smuzhiyun  * @hsotg: The HCD state structure for the DWC OTG controller
1359*4882a593Smuzhiyun  * @qh:    QH for the periodic transfer. The QH should already contain the
1360*4882a593Smuzhiyun  *         scheduling information.
1361*4882a593Smuzhiyun  *
1362*4882a593Smuzhiyun  * Return: 0 if successful, negative error code otherwise
1363*4882a593Smuzhiyun  */
dwc2_schedule_periodic(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)1364*4882a593Smuzhiyun static int dwc2_schedule_periodic(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1365*4882a593Smuzhiyun {
1366*4882a593Smuzhiyun 	int status;
1367*4882a593Smuzhiyun 
1368*4882a593Smuzhiyun 	status = dwc2_check_max_xfer_size(hsotg, qh);
1369*4882a593Smuzhiyun 	if (status) {
1370*4882a593Smuzhiyun 		dev_dbg(hsotg->dev,
1371*4882a593Smuzhiyun 			"%s: Channel max transfer size too small for periodic transfer\n",
1372*4882a593Smuzhiyun 			__func__);
1373*4882a593Smuzhiyun 		return status;
1374*4882a593Smuzhiyun 	}
1375*4882a593Smuzhiyun 
1376*4882a593Smuzhiyun 	/* Cancel pending unreserve; if canceled OK, unreserve was pending */
1377*4882a593Smuzhiyun 	if (del_timer(&qh->unreserve_timer))
1378*4882a593Smuzhiyun 		WARN_ON(!qh->unreserve_pending);
1379*4882a593Smuzhiyun 
1380*4882a593Smuzhiyun 	/*
1381*4882a593Smuzhiyun 	 * Only need to reserve if there's not an unreserve pending, since if an
1382*4882a593Smuzhiyun 	 * unreserve is pending then by definition our old reservation is still
1383*4882a593Smuzhiyun 	 * valid.  Unreserve might still be pending even if we didn't cancel if
1384*4882a593Smuzhiyun 	 * dwc2_unreserve_timer_fn() already started.  Code in the timer handles
1385*4882a593Smuzhiyun 	 * that case.
1386*4882a593Smuzhiyun 	 */
1387*4882a593Smuzhiyun 	if (!qh->unreserve_pending) {
1388*4882a593Smuzhiyun 		status = dwc2_do_reserve(hsotg, qh);
1389*4882a593Smuzhiyun 		if (status)
1390*4882a593Smuzhiyun 			return status;
1391*4882a593Smuzhiyun 	} else {
1392*4882a593Smuzhiyun 		/*
1393*4882a593Smuzhiyun 		 * It might have been a while, so make sure that frame_number
1394*4882a593Smuzhiyun 		 * is still good.  Note: we could also try to use the similar
1395*4882a593Smuzhiyun 		 * dwc2_next_periodic_start() but that schedules much more
1396*4882a593Smuzhiyun 		 * tightly and we might need to hurry and queue things up.
1397*4882a593Smuzhiyun 		 */
1398*4882a593Smuzhiyun 		if (dwc2_frame_num_le(qh->next_active_frame,
1399*4882a593Smuzhiyun 				      hsotg->frame_number))
1400*4882a593Smuzhiyun 			dwc2_pick_first_frame(hsotg, qh);
1401*4882a593Smuzhiyun 	}
1402*4882a593Smuzhiyun 
1403*4882a593Smuzhiyun 	qh->unreserve_pending = 0;
1404*4882a593Smuzhiyun 
1405*4882a593Smuzhiyun 	if (hsotg->params.dma_desc_enable)
1406*4882a593Smuzhiyun 		/* Don't rely on SOF and start in ready schedule */
1407*4882a593Smuzhiyun 		list_add_tail(&qh->qh_list_entry, &hsotg->periodic_sched_ready);
1408*4882a593Smuzhiyun 	else
1409*4882a593Smuzhiyun 		/* Always start in inactive schedule */
1410*4882a593Smuzhiyun 		list_add_tail(&qh->qh_list_entry,
1411*4882a593Smuzhiyun 			      &hsotg->periodic_sched_inactive);
1412*4882a593Smuzhiyun 
1413*4882a593Smuzhiyun 	return 0;
1414*4882a593Smuzhiyun }
1415*4882a593Smuzhiyun 
1416*4882a593Smuzhiyun /**
1417*4882a593Smuzhiyun  * dwc2_deschedule_periodic() - Removes an interrupt or isochronous transfer
1418*4882a593Smuzhiyun  * from the periodic schedule
1419*4882a593Smuzhiyun  *
1420*4882a593Smuzhiyun  * @hsotg: The HCD state structure for the DWC OTG controller
1421*4882a593Smuzhiyun  * @qh:	   QH for the periodic transfer
1422*4882a593Smuzhiyun  */
dwc2_deschedule_periodic(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)1423*4882a593Smuzhiyun static void dwc2_deschedule_periodic(struct dwc2_hsotg *hsotg,
1424*4882a593Smuzhiyun 				     struct dwc2_qh *qh)
1425*4882a593Smuzhiyun {
1426*4882a593Smuzhiyun 	bool did_modify;
1427*4882a593Smuzhiyun 
1428*4882a593Smuzhiyun 	assert_spin_locked(&hsotg->lock);
1429*4882a593Smuzhiyun 
1430*4882a593Smuzhiyun 	/*
1431*4882a593Smuzhiyun 	 * Schedule the unreserve to happen in a little bit.  Cases here:
1432*4882a593Smuzhiyun 	 * - Unreserve worker might be sitting there waiting to grab the lock.
1433*4882a593Smuzhiyun 	 *   In this case it will notice it's been schedule again and will
1434*4882a593Smuzhiyun 	 *   quit.
1435*4882a593Smuzhiyun 	 * - Unreserve worker might not be scheduled.
1436*4882a593Smuzhiyun 	 *
1437*4882a593Smuzhiyun 	 * We should never already be scheduled since dwc2_schedule_periodic()
1438*4882a593Smuzhiyun 	 * should have canceled the scheduled unreserve timer (hence the
1439*4882a593Smuzhiyun 	 * warning on did_modify).
1440*4882a593Smuzhiyun 	 *
1441*4882a593Smuzhiyun 	 * We add + 1 to the timer to guarantee that at least 1 jiffy has
1442*4882a593Smuzhiyun 	 * passed (otherwise if the jiffy counter might tick right after we
1443*4882a593Smuzhiyun 	 * read it and we'll get no delay).
1444*4882a593Smuzhiyun 	 */
1445*4882a593Smuzhiyun 	did_modify = mod_timer(&qh->unreserve_timer,
1446*4882a593Smuzhiyun 			       jiffies + DWC2_UNRESERVE_DELAY + 1);
1447*4882a593Smuzhiyun 	WARN_ON(did_modify);
1448*4882a593Smuzhiyun 	qh->unreserve_pending = 1;
1449*4882a593Smuzhiyun 
1450*4882a593Smuzhiyun 	list_del_init(&qh->qh_list_entry);
1451*4882a593Smuzhiyun }
1452*4882a593Smuzhiyun 
1453*4882a593Smuzhiyun /**
1454*4882a593Smuzhiyun  * dwc2_wait_timer_fn() - Timer function to re-queue after waiting
1455*4882a593Smuzhiyun  *
1456*4882a593Smuzhiyun  * As per the spec, a NAK indicates that "a function is temporarily unable to
1457*4882a593Smuzhiyun  * transmit or receive data, but will eventually be able to do so without need
1458*4882a593Smuzhiyun  * of host intervention".
1459*4882a593Smuzhiyun  *
1460*4882a593Smuzhiyun  * That means that when we encounter a NAK we're supposed to retry.
1461*4882a593Smuzhiyun  *
1462*4882a593Smuzhiyun  * ...but if we retry right away (from the interrupt handler that saw the NAK)
1463*4882a593Smuzhiyun  * then we can end up with an interrupt storm (if the other side keeps NAKing
1464*4882a593Smuzhiyun  * us) because on slow enough CPUs it could take us longer to get out of the
1465*4882a593Smuzhiyun  * interrupt routine than it takes for the device to send another NAK.  That
1466*4882a593Smuzhiyun  * leads to a constant stream of NAK interrupts and the CPU locks.
1467*4882a593Smuzhiyun  *
1468*4882a593Smuzhiyun  * ...so instead of retrying right away in the case of a NAK we'll set a timer
1469*4882a593Smuzhiyun  * to retry some time later.  This function handles that timer and moves the
1470*4882a593Smuzhiyun  * qh back to the "inactive" list, then queues transactions.
1471*4882a593Smuzhiyun  *
1472*4882a593Smuzhiyun  * @t: Pointer to wait_timer in a qh.
1473*4882a593Smuzhiyun  *
1474*4882a593Smuzhiyun  * Return: HRTIMER_NORESTART to not automatically restart this timer.
1475*4882a593Smuzhiyun  */
dwc2_wait_timer_fn(struct hrtimer * t)1476*4882a593Smuzhiyun static enum hrtimer_restart dwc2_wait_timer_fn(struct hrtimer *t)
1477*4882a593Smuzhiyun {
1478*4882a593Smuzhiyun 	struct dwc2_qh *qh = container_of(t, struct dwc2_qh, wait_timer);
1479*4882a593Smuzhiyun 	struct dwc2_hsotg *hsotg = qh->hsotg;
1480*4882a593Smuzhiyun 	unsigned long flags;
1481*4882a593Smuzhiyun 
1482*4882a593Smuzhiyun 	spin_lock_irqsave(&hsotg->lock, flags);
1483*4882a593Smuzhiyun 
1484*4882a593Smuzhiyun 	/*
1485*4882a593Smuzhiyun 	 * We'll set wait_timer_cancel to true if we want to cancel this
1486*4882a593Smuzhiyun 	 * operation in dwc2_hcd_qh_unlink().
1487*4882a593Smuzhiyun 	 */
1488*4882a593Smuzhiyun 	if (!qh->wait_timer_cancel) {
1489*4882a593Smuzhiyun 		enum dwc2_transaction_type tr_type;
1490*4882a593Smuzhiyun 
1491*4882a593Smuzhiyun 		qh->want_wait = false;
1492*4882a593Smuzhiyun 
1493*4882a593Smuzhiyun 		list_move(&qh->qh_list_entry,
1494*4882a593Smuzhiyun 			  &hsotg->non_periodic_sched_inactive);
1495*4882a593Smuzhiyun 
1496*4882a593Smuzhiyun 		tr_type = dwc2_hcd_select_transactions(hsotg);
1497*4882a593Smuzhiyun 		if (tr_type != DWC2_TRANSACTION_NONE)
1498*4882a593Smuzhiyun 			dwc2_hcd_queue_transactions(hsotg, tr_type);
1499*4882a593Smuzhiyun 	}
1500*4882a593Smuzhiyun 
1501*4882a593Smuzhiyun 	spin_unlock_irqrestore(&hsotg->lock, flags);
1502*4882a593Smuzhiyun 	return HRTIMER_NORESTART;
1503*4882a593Smuzhiyun }
1504*4882a593Smuzhiyun 
1505*4882a593Smuzhiyun /**
1506*4882a593Smuzhiyun  * dwc2_qh_init() - Initializes a QH structure
1507*4882a593Smuzhiyun  *
1508*4882a593Smuzhiyun  * @hsotg: The HCD state structure for the DWC OTG controller
1509*4882a593Smuzhiyun  * @qh:    The QH to init
1510*4882a593Smuzhiyun  * @urb:   Holds the information about the device/endpoint needed to initialize
1511*4882a593Smuzhiyun  *         the QH
1512*4882a593Smuzhiyun  * @mem_flags: Flags for allocating memory.
1513*4882a593Smuzhiyun  */
dwc2_qh_init(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh,struct dwc2_hcd_urb * urb,gfp_t mem_flags)1514*4882a593Smuzhiyun static void dwc2_qh_init(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
1515*4882a593Smuzhiyun 			 struct dwc2_hcd_urb *urb, gfp_t mem_flags)
1516*4882a593Smuzhiyun {
1517*4882a593Smuzhiyun 	int dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
1518*4882a593Smuzhiyun 	u8 ep_type = dwc2_hcd_get_pipe_type(&urb->pipe_info);
1519*4882a593Smuzhiyun 	bool ep_is_in = !!dwc2_hcd_is_pipe_in(&urb->pipe_info);
1520*4882a593Smuzhiyun 	bool ep_is_isoc = (ep_type == USB_ENDPOINT_XFER_ISOC);
1521*4882a593Smuzhiyun 	bool ep_is_int = (ep_type == USB_ENDPOINT_XFER_INT);
1522*4882a593Smuzhiyun 	u32 hprt = dwc2_readl(hsotg, HPRT0);
1523*4882a593Smuzhiyun 	u32 prtspd = (hprt & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
1524*4882a593Smuzhiyun 	bool do_split = (prtspd == HPRT0_SPD_HIGH_SPEED &&
1525*4882a593Smuzhiyun 			 dev_speed != USB_SPEED_HIGH);
1526*4882a593Smuzhiyun 	int maxp = dwc2_hcd_get_maxp(&urb->pipe_info);
1527*4882a593Smuzhiyun 	int maxp_mult = dwc2_hcd_get_maxp_mult(&urb->pipe_info);
1528*4882a593Smuzhiyun 	int bytecount = maxp_mult * maxp;
1529*4882a593Smuzhiyun 	char *speed, *type;
1530*4882a593Smuzhiyun 
1531*4882a593Smuzhiyun 	/* Initialize QH */
1532*4882a593Smuzhiyun 	qh->hsotg = hsotg;
1533*4882a593Smuzhiyun 	timer_setup(&qh->unreserve_timer, dwc2_unreserve_timer_fn, 0);
1534*4882a593Smuzhiyun 	hrtimer_init(&qh->wait_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1535*4882a593Smuzhiyun 	qh->wait_timer.function = &dwc2_wait_timer_fn;
1536*4882a593Smuzhiyun 	qh->ep_type = ep_type;
1537*4882a593Smuzhiyun 	qh->ep_is_in = ep_is_in;
1538*4882a593Smuzhiyun 
1539*4882a593Smuzhiyun 	qh->data_toggle = DWC2_HC_PID_DATA0;
1540*4882a593Smuzhiyun 	qh->maxp = maxp;
1541*4882a593Smuzhiyun 	qh->maxp_mult = maxp_mult;
1542*4882a593Smuzhiyun 	INIT_LIST_HEAD(&qh->qtd_list);
1543*4882a593Smuzhiyun 	INIT_LIST_HEAD(&qh->qh_list_entry);
1544*4882a593Smuzhiyun 
1545*4882a593Smuzhiyun 	qh->do_split = do_split;
1546*4882a593Smuzhiyun 	qh->dev_speed = dev_speed;
1547*4882a593Smuzhiyun 
1548*4882a593Smuzhiyun 	if (ep_is_int || ep_is_isoc) {
1549*4882a593Smuzhiyun 		/* Compute scheduling parameters once and save them */
1550*4882a593Smuzhiyun 		int host_speed = do_split ? USB_SPEED_HIGH : dev_speed;
1551*4882a593Smuzhiyun 		struct dwc2_tt *dwc_tt = dwc2_host_get_tt_info(hsotg, urb->priv,
1552*4882a593Smuzhiyun 							       mem_flags,
1553*4882a593Smuzhiyun 							       &qh->ttport);
1554*4882a593Smuzhiyun 		int device_ns;
1555*4882a593Smuzhiyun 
1556*4882a593Smuzhiyun 		qh->dwc_tt = dwc_tt;
1557*4882a593Smuzhiyun 
1558*4882a593Smuzhiyun 		qh->host_us = NS_TO_US(usb_calc_bus_time(host_speed, ep_is_in,
1559*4882a593Smuzhiyun 				       ep_is_isoc, bytecount));
1560*4882a593Smuzhiyun 		device_ns = usb_calc_bus_time(dev_speed, ep_is_in,
1561*4882a593Smuzhiyun 					      ep_is_isoc, bytecount);
1562*4882a593Smuzhiyun 
1563*4882a593Smuzhiyun 		if (do_split && dwc_tt)
1564*4882a593Smuzhiyun 			device_ns += dwc_tt->usb_tt->think_time;
1565*4882a593Smuzhiyun 		qh->device_us = NS_TO_US(device_ns);
1566*4882a593Smuzhiyun 
1567*4882a593Smuzhiyun 		qh->device_interval = urb->interval;
1568*4882a593Smuzhiyun 		qh->host_interval = urb->interval * (do_split ? 8 : 1);
1569*4882a593Smuzhiyun 
1570*4882a593Smuzhiyun 		/*
1571*4882a593Smuzhiyun 		 * Schedule low speed if we're running the host in low or
1572*4882a593Smuzhiyun 		 * full speed OR if we've got a "TT" to deal with to access this
1573*4882a593Smuzhiyun 		 * device.
1574*4882a593Smuzhiyun 		 */
1575*4882a593Smuzhiyun 		qh->schedule_low_speed = prtspd != HPRT0_SPD_HIGH_SPEED ||
1576*4882a593Smuzhiyun 					 dwc_tt;
1577*4882a593Smuzhiyun 
1578*4882a593Smuzhiyun 		if (do_split) {
1579*4882a593Smuzhiyun 			/* We won't know num transfers until we schedule */
1580*4882a593Smuzhiyun 			qh->num_hs_transfers = -1;
1581*4882a593Smuzhiyun 		} else if (dev_speed == USB_SPEED_HIGH) {
1582*4882a593Smuzhiyun 			qh->num_hs_transfers = 1;
1583*4882a593Smuzhiyun 		} else {
1584*4882a593Smuzhiyun 			qh->num_hs_transfers = 0;
1585*4882a593Smuzhiyun 		}
1586*4882a593Smuzhiyun 
1587*4882a593Smuzhiyun 		/* We'll schedule later when we have something to do */
1588*4882a593Smuzhiyun 	}
1589*4882a593Smuzhiyun 
1590*4882a593Smuzhiyun 	switch (dev_speed) {
1591*4882a593Smuzhiyun 	case USB_SPEED_LOW:
1592*4882a593Smuzhiyun 		speed = "low";
1593*4882a593Smuzhiyun 		break;
1594*4882a593Smuzhiyun 	case USB_SPEED_FULL:
1595*4882a593Smuzhiyun 		speed = "full";
1596*4882a593Smuzhiyun 		break;
1597*4882a593Smuzhiyun 	case USB_SPEED_HIGH:
1598*4882a593Smuzhiyun 		speed = "high";
1599*4882a593Smuzhiyun 		break;
1600*4882a593Smuzhiyun 	default:
1601*4882a593Smuzhiyun 		speed = "?";
1602*4882a593Smuzhiyun 		break;
1603*4882a593Smuzhiyun 	}
1604*4882a593Smuzhiyun 
1605*4882a593Smuzhiyun 	switch (qh->ep_type) {
1606*4882a593Smuzhiyun 	case USB_ENDPOINT_XFER_ISOC:
1607*4882a593Smuzhiyun 		type = "isochronous";
1608*4882a593Smuzhiyun 		break;
1609*4882a593Smuzhiyun 	case USB_ENDPOINT_XFER_INT:
1610*4882a593Smuzhiyun 		type = "interrupt";
1611*4882a593Smuzhiyun 		break;
1612*4882a593Smuzhiyun 	case USB_ENDPOINT_XFER_CONTROL:
1613*4882a593Smuzhiyun 		type = "control";
1614*4882a593Smuzhiyun 		break;
1615*4882a593Smuzhiyun 	case USB_ENDPOINT_XFER_BULK:
1616*4882a593Smuzhiyun 		type = "bulk";
1617*4882a593Smuzhiyun 		break;
1618*4882a593Smuzhiyun 	default:
1619*4882a593Smuzhiyun 		type = "?";
1620*4882a593Smuzhiyun 		break;
1621*4882a593Smuzhiyun 	}
1622*4882a593Smuzhiyun 
1623*4882a593Smuzhiyun 	dwc2_sch_dbg(hsotg, "QH=%p Init %s, %s speed, %d bytes:\n", qh, type,
1624*4882a593Smuzhiyun 		     speed, bytecount);
1625*4882a593Smuzhiyun 	dwc2_sch_dbg(hsotg, "QH=%p ...addr=%d, ep=%d, %s\n", qh,
1626*4882a593Smuzhiyun 		     dwc2_hcd_get_dev_addr(&urb->pipe_info),
1627*4882a593Smuzhiyun 		     dwc2_hcd_get_ep_num(&urb->pipe_info),
1628*4882a593Smuzhiyun 		     ep_is_in ? "IN" : "OUT");
1629*4882a593Smuzhiyun 	if (ep_is_int || ep_is_isoc) {
1630*4882a593Smuzhiyun 		dwc2_sch_dbg(hsotg,
1631*4882a593Smuzhiyun 			     "QH=%p ...duration: host=%d us, device=%d us\n",
1632*4882a593Smuzhiyun 			     qh, qh->host_us, qh->device_us);
1633*4882a593Smuzhiyun 		dwc2_sch_dbg(hsotg, "QH=%p ...interval: host=%d, device=%d\n",
1634*4882a593Smuzhiyun 			     qh, qh->host_interval, qh->device_interval);
1635*4882a593Smuzhiyun 		if (qh->schedule_low_speed)
1636*4882a593Smuzhiyun 			dwc2_sch_dbg(hsotg, "QH=%p ...low speed schedule=%p\n",
1637*4882a593Smuzhiyun 				     qh, dwc2_get_ls_map(hsotg, qh));
1638*4882a593Smuzhiyun 	}
1639*4882a593Smuzhiyun }
1640*4882a593Smuzhiyun 
1641*4882a593Smuzhiyun /**
1642*4882a593Smuzhiyun  * dwc2_hcd_qh_create() - Allocates and initializes a QH
1643*4882a593Smuzhiyun  *
1644*4882a593Smuzhiyun  * @hsotg:        The HCD state structure for the DWC OTG controller
1645*4882a593Smuzhiyun  * @urb:          Holds the information about the device/endpoint needed
1646*4882a593Smuzhiyun  *                to initialize the QH
1647*4882a593Smuzhiyun  * @mem_flags:   Flags for allocating memory.
1648*4882a593Smuzhiyun  *
1649*4882a593Smuzhiyun  * Return: Pointer to the newly allocated QH, or NULL on error
1650*4882a593Smuzhiyun  */
dwc2_hcd_qh_create(struct dwc2_hsotg * hsotg,struct dwc2_hcd_urb * urb,gfp_t mem_flags)1651*4882a593Smuzhiyun struct dwc2_qh *dwc2_hcd_qh_create(struct dwc2_hsotg *hsotg,
1652*4882a593Smuzhiyun 				   struct dwc2_hcd_urb *urb,
1653*4882a593Smuzhiyun 					  gfp_t mem_flags)
1654*4882a593Smuzhiyun {
1655*4882a593Smuzhiyun 	struct dwc2_qh *qh;
1656*4882a593Smuzhiyun 
1657*4882a593Smuzhiyun 	if (!urb->priv)
1658*4882a593Smuzhiyun 		return NULL;
1659*4882a593Smuzhiyun 
1660*4882a593Smuzhiyun 	/* Allocate memory */
1661*4882a593Smuzhiyun 	qh = kzalloc(sizeof(*qh), mem_flags);
1662*4882a593Smuzhiyun 	if (!qh)
1663*4882a593Smuzhiyun 		return NULL;
1664*4882a593Smuzhiyun 
1665*4882a593Smuzhiyun 	dwc2_qh_init(hsotg, qh, urb, mem_flags);
1666*4882a593Smuzhiyun 
1667*4882a593Smuzhiyun 	if (hsotg->params.dma_desc_enable &&
1668*4882a593Smuzhiyun 	    dwc2_hcd_qh_init_ddma(hsotg, qh, mem_flags) < 0) {
1669*4882a593Smuzhiyun 		dwc2_hcd_qh_free(hsotg, qh);
1670*4882a593Smuzhiyun 		return NULL;
1671*4882a593Smuzhiyun 	}
1672*4882a593Smuzhiyun 
1673*4882a593Smuzhiyun 	return qh;
1674*4882a593Smuzhiyun }
1675*4882a593Smuzhiyun 
1676*4882a593Smuzhiyun /**
1677*4882a593Smuzhiyun  * dwc2_hcd_qh_free() - Frees the QH
1678*4882a593Smuzhiyun  *
1679*4882a593Smuzhiyun  * @hsotg: HCD instance
1680*4882a593Smuzhiyun  * @qh:    The QH to free
1681*4882a593Smuzhiyun  *
1682*4882a593Smuzhiyun  * QH should already be removed from the list. QTD list should already be empty
1683*4882a593Smuzhiyun  * if called from URB Dequeue.
1684*4882a593Smuzhiyun  *
1685*4882a593Smuzhiyun  * Must NOT be called with interrupt disabled or spinlock held
1686*4882a593Smuzhiyun  */
dwc2_hcd_qh_free(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)1687*4882a593Smuzhiyun void dwc2_hcd_qh_free(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1688*4882a593Smuzhiyun {
1689*4882a593Smuzhiyun 	/* Make sure any unreserve work is finished. */
1690*4882a593Smuzhiyun 	if (del_timer_sync(&qh->unreserve_timer)) {
1691*4882a593Smuzhiyun 		unsigned long flags;
1692*4882a593Smuzhiyun 
1693*4882a593Smuzhiyun 		spin_lock_irqsave(&hsotg->lock, flags);
1694*4882a593Smuzhiyun 		dwc2_do_unreserve(hsotg, qh);
1695*4882a593Smuzhiyun 		spin_unlock_irqrestore(&hsotg->lock, flags);
1696*4882a593Smuzhiyun 	}
1697*4882a593Smuzhiyun 
1698*4882a593Smuzhiyun 	/*
1699*4882a593Smuzhiyun 	 * We don't have the lock so we can safely wait until the wait timer
1700*4882a593Smuzhiyun 	 * finishes.  Of course, at this point in time we'd better have set
1701*4882a593Smuzhiyun 	 * wait_timer_active to false so if this timer was still pending it
1702*4882a593Smuzhiyun 	 * won't do anything anyway, but we want it to finish before we free
1703*4882a593Smuzhiyun 	 * memory.
1704*4882a593Smuzhiyun 	 */
1705*4882a593Smuzhiyun 	hrtimer_cancel(&qh->wait_timer);
1706*4882a593Smuzhiyun 
1707*4882a593Smuzhiyun 	dwc2_host_put_tt_info(hsotg, qh->dwc_tt);
1708*4882a593Smuzhiyun 
1709*4882a593Smuzhiyun 	if (qh->desc_list)
1710*4882a593Smuzhiyun 		dwc2_hcd_qh_free_ddma(hsotg, qh);
1711*4882a593Smuzhiyun 	else if (hsotg->unaligned_cache && qh->dw_align_buf)
1712*4882a593Smuzhiyun 		kmem_cache_free(hsotg->unaligned_cache, qh->dw_align_buf);
1713*4882a593Smuzhiyun 
1714*4882a593Smuzhiyun 	kfree(qh);
1715*4882a593Smuzhiyun }
1716*4882a593Smuzhiyun 
1717*4882a593Smuzhiyun /**
1718*4882a593Smuzhiyun  * dwc2_hcd_qh_add() - Adds a QH to either the non periodic or periodic
1719*4882a593Smuzhiyun  * schedule if it is not already in the schedule. If the QH is already in
1720*4882a593Smuzhiyun  * the schedule, no action is taken.
1721*4882a593Smuzhiyun  *
1722*4882a593Smuzhiyun  * @hsotg: The HCD state structure for the DWC OTG controller
1723*4882a593Smuzhiyun  * @qh:    The QH to add
1724*4882a593Smuzhiyun  *
1725*4882a593Smuzhiyun  * Return: 0 if successful, negative error code otherwise
1726*4882a593Smuzhiyun  */
dwc2_hcd_qh_add(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)1727*4882a593Smuzhiyun int dwc2_hcd_qh_add(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1728*4882a593Smuzhiyun {
1729*4882a593Smuzhiyun 	int status;
1730*4882a593Smuzhiyun 	u32 intr_mask;
1731*4882a593Smuzhiyun 	ktime_t delay;
1732*4882a593Smuzhiyun 
1733*4882a593Smuzhiyun 	if (dbg_qh(qh))
1734*4882a593Smuzhiyun 		dev_vdbg(hsotg->dev, "%s()\n", __func__);
1735*4882a593Smuzhiyun 
1736*4882a593Smuzhiyun 	if (!list_empty(&qh->qh_list_entry))
1737*4882a593Smuzhiyun 		/* QH already in a schedule */
1738*4882a593Smuzhiyun 		return 0;
1739*4882a593Smuzhiyun 
1740*4882a593Smuzhiyun 	/* Add the new QH to the appropriate schedule */
1741*4882a593Smuzhiyun 	if (dwc2_qh_is_non_per(qh)) {
1742*4882a593Smuzhiyun 		/* Schedule right away */
1743*4882a593Smuzhiyun 		qh->start_active_frame = hsotg->frame_number;
1744*4882a593Smuzhiyun 		qh->next_active_frame = qh->start_active_frame;
1745*4882a593Smuzhiyun 
1746*4882a593Smuzhiyun 		if (qh->want_wait) {
1747*4882a593Smuzhiyun 			list_add_tail(&qh->qh_list_entry,
1748*4882a593Smuzhiyun 				      &hsotg->non_periodic_sched_waiting);
1749*4882a593Smuzhiyun 			qh->wait_timer_cancel = false;
1750*4882a593Smuzhiyun 			delay = ktime_set(0, DWC2_RETRY_WAIT_DELAY);
1751*4882a593Smuzhiyun 			hrtimer_start(&qh->wait_timer, delay, HRTIMER_MODE_REL);
1752*4882a593Smuzhiyun 		} else {
1753*4882a593Smuzhiyun 			list_add_tail(&qh->qh_list_entry,
1754*4882a593Smuzhiyun 				      &hsotg->non_periodic_sched_inactive);
1755*4882a593Smuzhiyun 		}
1756*4882a593Smuzhiyun 		return 0;
1757*4882a593Smuzhiyun 	}
1758*4882a593Smuzhiyun 
1759*4882a593Smuzhiyun 	status = dwc2_schedule_periodic(hsotg, qh);
1760*4882a593Smuzhiyun 	if (status)
1761*4882a593Smuzhiyun 		return status;
1762*4882a593Smuzhiyun 	if (!hsotg->periodic_qh_count) {
1763*4882a593Smuzhiyun 		intr_mask = dwc2_readl(hsotg, GINTMSK);
1764*4882a593Smuzhiyun 		intr_mask |= GINTSTS_SOF;
1765*4882a593Smuzhiyun 		dwc2_writel(hsotg, intr_mask, GINTMSK);
1766*4882a593Smuzhiyun 	}
1767*4882a593Smuzhiyun 	hsotg->periodic_qh_count++;
1768*4882a593Smuzhiyun 
1769*4882a593Smuzhiyun 	return 0;
1770*4882a593Smuzhiyun }
1771*4882a593Smuzhiyun 
1772*4882a593Smuzhiyun /**
1773*4882a593Smuzhiyun  * dwc2_hcd_qh_unlink() - Removes a QH from either the non-periodic or periodic
1774*4882a593Smuzhiyun  * schedule. Memory is not freed.
1775*4882a593Smuzhiyun  *
1776*4882a593Smuzhiyun  * @hsotg: The HCD state structure
1777*4882a593Smuzhiyun  * @qh:    QH to remove from schedule
1778*4882a593Smuzhiyun  */
dwc2_hcd_qh_unlink(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)1779*4882a593Smuzhiyun void dwc2_hcd_qh_unlink(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1780*4882a593Smuzhiyun {
1781*4882a593Smuzhiyun 	u32 intr_mask;
1782*4882a593Smuzhiyun 
1783*4882a593Smuzhiyun 	dev_vdbg(hsotg->dev, "%s()\n", __func__);
1784*4882a593Smuzhiyun 
1785*4882a593Smuzhiyun 	/* If the wait_timer is pending, this will stop it from acting */
1786*4882a593Smuzhiyun 	qh->wait_timer_cancel = true;
1787*4882a593Smuzhiyun 
1788*4882a593Smuzhiyun 	if (list_empty(&qh->qh_list_entry))
1789*4882a593Smuzhiyun 		/* QH is not in a schedule */
1790*4882a593Smuzhiyun 		return;
1791*4882a593Smuzhiyun 
1792*4882a593Smuzhiyun 	if (dwc2_qh_is_non_per(qh)) {
1793*4882a593Smuzhiyun 		if (hsotg->non_periodic_qh_ptr == &qh->qh_list_entry)
1794*4882a593Smuzhiyun 			hsotg->non_periodic_qh_ptr =
1795*4882a593Smuzhiyun 					hsotg->non_periodic_qh_ptr->next;
1796*4882a593Smuzhiyun 		list_del_init(&qh->qh_list_entry);
1797*4882a593Smuzhiyun 		return;
1798*4882a593Smuzhiyun 	}
1799*4882a593Smuzhiyun 
1800*4882a593Smuzhiyun 	dwc2_deschedule_periodic(hsotg, qh);
1801*4882a593Smuzhiyun 	hsotg->periodic_qh_count--;
1802*4882a593Smuzhiyun 	if (!hsotg->periodic_qh_count &&
1803*4882a593Smuzhiyun 	    !hsotg->params.dma_desc_enable) {
1804*4882a593Smuzhiyun 		intr_mask = dwc2_readl(hsotg, GINTMSK);
1805*4882a593Smuzhiyun 		intr_mask &= ~GINTSTS_SOF;
1806*4882a593Smuzhiyun 		dwc2_writel(hsotg, intr_mask, GINTMSK);
1807*4882a593Smuzhiyun 	}
1808*4882a593Smuzhiyun }
1809*4882a593Smuzhiyun 
1810*4882a593Smuzhiyun /**
1811*4882a593Smuzhiyun  * dwc2_next_for_periodic_split() - Set next_active_frame midway thru a split.
1812*4882a593Smuzhiyun  *
1813*4882a593Smuzhiyun  * This is called for setting next_active_frame for periodic splits for all but
1814*4882a593Smuzhiyun  * the first packet of the split.  Confusing?  I thought so...
1815*4882a593Smuzhiyun  *
1816*4882a593Smuzhiyun  * Periodic splits are single low/full speed transfers that we end up splitting
1817*4882a593Smuzhiyun  * up into several high speed transfers.  They always fit into one full (1 ms)
1818*4882a593Smuzhiyun  * frame but might be split over several microframes (125 us each).  We to put
1819*4882a593Smuzhiyun  * each of the parts on a very specific high speed frame.
1820*4882a593Smuzhiyun  *
1821*4882a593Smuzhiyun  * This function figures out where the next active uFrame needs to be.
1822*4882a593Smuzhiyun  *
1823*4882a593Smuzhiyun  * @hsotg:        The HCD state structure
1824*4882a593Smuzhiyun  * @qh:           QH for the periodic transfer.
1825*4882a593Smuzhiyun  * @frame_number: The current frame number.
1826*4882a593Smuzhiyun  *
1827*4882a593Smuzhiyun  * Return: number missed by (or 0 if we didn't miss).
1828*4882a593Smuzhiyun  */
dwc2_next_for_periodic_split(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh,u16 frame_number)1829*4882a593Smuzhiyun static int dwc2_next_for_periodic_split(struct dwc2_hsotg *hsotg,
1830*4882a593Smuzhiyun 					struct dwc2_qh *qh, u16 frame_number)
1831*4882a593Smuzhiyun {
1832*4882a593Smuzhiyun 	u16 old_frame = qh->next_active_frame;
1833*4882a593Smuzhiyun 	u16 prev_frame_number = dwc2_frame_num_dec(frame_number, 1);
1834*4882a593Smuzhiyun 	int missed = 0;
1835*4882a593Smuzhiyun 	u16 incr;
1836*4882a593Smuzhiyun 
1837*4882a593Smuzhiyun 	/*
1838*4882a593Smuzhiyun 	 * See dwc2_uframe_schedule_split() for split scheduling.
1839*4882a593Smuzhiyun 	 *
1840*4882a593Smuzhiyun 	 * Basically: increment 1 normally, but 2 right after the start split
1841*4882a593Smuzhiyun 	 * (except for ISOC out).
1842*4882a593Smuzhiyun 	 */
1843*4882a593Smuzhiyun 	if (old_frame == qh->start_active_frame &&
1844*4882a593Smuzhiyun 	    !(qh->ep_type == USB_ENDPOINT_XFER_ISOC && !qh->ep_is_in))
1845*4882a593Smuzhiyun 		incr = 2;
1846*4882a593Smuzhiyun 	else
1847*4882a593Smuzhiyun 		incr = 1;
1848*4882a593Smuzhiyun 
1849*4882a593Smuzhiyun 	qh->next_active_frame = dwc2_frame_num_inc(old_frame, incr);
1850*4882a593Smuzhiyun 
1851*4882a593Smuzhiyun 	/*
1852*4882a593Smuzhiyun 	 * Note that it's OK for frame_number to be 1 frame past
1853*4882a593Smuzhiyun 	 * next_active_frame.  Remember that next_active_frame is supposed to
1854*4882a593Smuzhiyun 	 * be 1 frame _before_ when we want to be scheduled.  If we're 1 frame
1855*4882a593Smuzhiyun 	 * past it just means schedule ASAP.
1856*4882a593Smuzhiyun 	 *
1857*4882a593Smuzhiyun 	 * It's _not_ OK, however, if we're more than one frame past.
1858*4882a593Smuzhiyun 	 */
1859*4882a593Smuzhiyun 	if (dwc2_frame_num_gt(prev_frame_number, qh->next_active_frame)) {
1860*4882a593Smuzhiyun 		/*
1861*4882a593Smuzhiyun 		 * OOPS, we missed.  That's actually pretty bad since
1862*4882a593Smuzhiyun 		 * the hub will be unhappy; try ASAP I guess.
1863*4882a593Smuzhiyun 		 */
1864*4882a593Smuzhiyun 		missed = dwc2_frame_num_dec(prev_frame_number,
1865*4882a593Smuzhiyun 					    qh->next_active_frame);
1866*4882a593Smuzhiyun 		qh->next_active_frame = frame_number;
1867*4882a593Smuzhiyun 	}
1868*4882a593Smuzhiyun 
1869*4882a593Smuzhiyun 	return missed;
1870*4882a593Smuzhiyun }
1871*4882a593Smuzhiyun 
1872*4882a593Smuzhiyun /**
1873*4882a593Smuzhiyun  * dwc2_next_periodic_start() - Set next_active_frame for next transfer start
1874*4882a593Smuzhiyun  *
1875*4882a593Smuzhiyun  * This is called for setting next_active_frame for a periodic transfer for
1876*4882a593Smuzhiyun  * all cases other than midway through a periodic split.  This will also update
1877*4882a593Smuzhiyun  * start_active_frame.
1878*4882a593Smuzhiyun  *
1879*4882a593Smuzhiyun  * Since we _always_ keep start_active_frame as the start of the previous
1880*4882a593Smuzhiyun  * transfer this is normally pretty easy: we just add our interval to
1881*4882a593Smuzhiyun  * start_active_frame and we've got our answer.
1882*4882a593Smuzhiyun  *
1883*4882a593Smuzhiyun  * The tricks come into play if we miss.  In that case we'll look for the next
1884*4882a593Smuzhiyun  * slot we can fit into.
1885*4882a593Smuzhiyun  *
1886*4882a593Smuzhiyun  * @hsotg:        The HCD state structure
1887*4882a593Smuzhiyun  * @qh:           QH for the periodic transfer.
1888*4882a593Smuzhiyun  * @frame_number: The current frame number.
1889*4882a593Smuzhiyun  *
1890*4882a593Smuzhiyun  * Return: number missed by (or 0 if we didn't miss).
1891*4882a593Smuzhiyun  */
dwc2_next_periodic_start(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh,u16 frame_number)1892*4882a593Smuzhiyun static int dwc2_next_periodic_start(struct dwc2_hsotg *hsotg,
1893*4882a593Smuzhiyun 				    struct dwc2_qh *qh, u16 frame_number)
1894*4882a593Smuzhiyun {
1895*4882a593Smuzhiyun 	int missed = 0;
1896*4882a593Smuzhiyun 	u16 interval = qh->host_interval;
1897*4882a593Smuzhiyun 	u16 prev_frame_number = dwc2_frame_num_dec(frame_number, 1);
1898*4882a593Smuzhiyun 
1899*4882a593Smuzhiyun 	qh->start_active_frame = dwc2_frame_num_inc(qh->start_active_frame,
1900*4882a593Smuzhiyun 						    interval);
1901*4882a593Smuzhiyun 
1902*4882a593Smuzhiyun 	/*
1903*4882a593Smuzhiyun 	 * The dwc2_frame_num_gt() function used below won't work terribly well
1904*4882a593Smuzhiyun 	 * with if we just incremented by a really large intervals since the
1905*4882a593Smuzhiyun 	 * frame counter only goes to 0x3fff.  It's terribly unlikely that we
1906*4882a593Smuzhiyun 	 * will have missed in this case anyway.  Just go to exit.  If we want
1907*4882a593Smuzhiyun 	 * to try to do better we'll need to keep track of a bigger counter
1908*4882a593Smuzhiyun 	 * somewhere in the driver and handle overflows.
1909*4882a593Smuzhiyun 	 */
1910*4882a593Smuzhiyun 	if (interval >= 0x1000)
1911*4882a593Smuzhiyun 		goto exit;
1912*4882a593Smuzhiyun 
1913*4882a593Smuzhiyun 	/*
1914*4882a593Smuzhiyun 	 * Test for misses, which is when it's too late to schedule.
1915*4882a593Smuzhiyun 	 *
1916*4882a593Smuzhiyun 	 * A few things to note:
1917*4882a593Smuzhiyun 	 * - We compare against prev_frame_number since start_active_frame
1918*4882a593Smuzhiyun 	 *   and next_active_frame are always 1 frame before we want things
1919*4882a593Smuzhiyun 	 *   to be active and we assume we can still get scheduled in the
1920*4882a593Smuzhiyun 	 *   current frame number.
1921*4882a593Smuzhiyun 	 * - It's possible for start_active_frame (now incremented) to be
1922*4882a593Smuzhiyun 	 *   next_active_frame if we got an EO MISS (even_odd miss) which
1923*4882a593Smuzhiyun 	 *   basically means that we detected there wasn't enough time for
1924*4882a593Smuzhiyun 	 *   the last packet and dwc2_hc_set_even_odd_frame() rescheduled us
1925*4882a593Smuzhiyun 	 *   at the last second.  We want to make sure we don't schedule
1926*4882a593Smuzhiyun 	 *   another transfer for the same frame.  My test webcam doesn't seem
1927*4882a593Smuzhiyun 	 *   terribly upset by missing a transfer but really doesn't like when
1928*4882a593Smuzhiyun 	 *   we do two transfers in the same frame.
1929*4882a593Smuzhiyun 	 * - Some misses are expected.  Specifically, in order to work
1930*4882a593Smuzhiyun 	 *   perfectly dwc2 really needs quite spectacular interrupt latency
1931*4882a593Smuzhiyun 	 *   requirements.  It needs to be able to handle its interrupts
1932*4882a593Smuzhiyun 	 *   completely within 125 us of them being asserted. That not only
1933*4882a593Smuzhiyun 	 *   means that the dwc2 interrupt handler needs to be fast but it
1934*4882a593Smuzhiyun 	 *   means that nothing else in the system has to block dwc2 for a long
1935*4882a593Smuzhiyun 	 *   time.  We can help with the dwc2 parts of this, but it's hard to
1936*4882a593Smuzhiyun 	 *   guarantee that a system will have interrupt latency < 125 us, so
1937*4882a593Smuzhiyun 	 *   we have to be robust to some misses.
1938*4882a593Smuzhiyun 	 */
1939*4882a593Smuzhiyun 	if (qh->start_active_frame == qh->next_active_frame ||
1940*4882a593Smuzhiyun 	    dwc2_frame_num_gt(prev_frame_number, qh->start_active_frame)) {
1941*4882a593Smuzhiyun 		u16 ideal_start = qh->start_active_frame;
1942*4882a593Smuzhiyun 		int periods_in_map;
1943*4882a593Smuzhiyun 
1944*4882a593Smuzhiyun 		/*
1945*4882a593Smuzhiyun 		 * Adjust interval as per gcd with map size.
1946*4882a593Smuzhiyun 		 * See pmap_schedule() for more details here.
1947*4882a593Smuzhiyun 		 */
1948*4882a593Smuzhiyun 		if (qh->do_split || qh->dev_speed == USB_SPEED_HIGH)
1949*4882a593Smuzhiyun 			periods_in_map = DWC2_HS_SCHEDULE_UFRAMES;
1950*4882a593Smuzhiyun 		else
1951*4882a593Smuzhiyun 			periods_in_map = DWC2_LS_SCHEDULE_FRAMES;
1952*4882a593Smuzhiyun 		interval = gcd(interval, periods_in_map);
1953*4882a593Smuzhiyun 
1954*4882a593Smuzhiyun 		do {
1955*4882a593Smuzhiyun 			qh->start_active_frame = dwc2_frame_num_inc(
1956*4882a593Smuzhiyun 				qh->start_active_frame, interval);
1957*4882a593Smuzhiyun 		} while (dwc2_frame_num_gt(prev_frame_number,
1958*4882a593Smuzhiyun 					   qh->start_active_frame));
1959*4882a593Smuzhiyun 
1960*4882a593Smuzhiyun 		missed = dwc2_frame_num_dec(qh->start_active_frame,
1961*4882a593Smuzhiyun 					    ideal_start);
1962*4882a593Smuzhiyun 	}
1963*4882a593Smuzhiyun 
1964*4882a593Smuzhiyun exit:
1965*4882a593Smuzhiyun 	qh->next_active_frame = qh->start_active_frame;
1966*4882a593Smuzhiyun 
1967*4882a593Smuzhiyun 	return missed;
1968*4882a593Smuzhiyun }
1969*4882a593Smuzhiyun 
1970*4882a593Smuzhiyun /*
1971*4882a593Smuzhiyun  * Deactivates a QH. For non-periodic QHs, removes the QH from the active
1972*4882a593Smuzhiyun  * non-periodic schedule. The QH is added to the inactive non-periodic
1973*4882a593Smuzhiyun  * schedule if any QTDs are still attached to the QH.
1974*4882a593Smuzhiyun  *
1975*4882a593Smuzhiyun  * For periodic QHs, the QH is removed from the periodic queued schedule. If
1976*4882a593Smuzhiyun  * there are any QTDs still attached to the QH, the QH is added to either the
1977*4882a593Smuzhiyun  * periodic inactive schedule or the periodic ready schedule and its next
1978*4882a593Smuzhiyun  * scheduled frame is calculated. The QH is placed in the ready schedule if
1979*4882a593Smuzhiyun  * the scheduled frame has been reached already. Otherwise it's placed in the
1980*4882a593Smuzhiyun  * inactive schedule. If there are no QTDs attached to the QH, the QH is
1981*4882a593Smuzhiyun  * completely removed from the periodic schedule.
1982*4882a593Smuzhiyun  */
dwc2_hcd_qh_deactivate(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh,int sched_next_periodic_split)1983*4882a593Smuzhiyun void dwc2_hcd_qh_deactivate(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
1984*4882a593Smuzhiyun 			    int sched_next_periodic_split)
1985*4882a593Smuzhiyun {
1986*4882a593Smuzhiyun 	u16 old_frame = qh->next_active_frame;
1987*4882a593Smuzhiyun 	u16 frame_number;
1988*4882a593Smuzhiyun 	int missed;
1989*4882a593Smuzhiyun 
1990*4882a593Smuzhiyun 	if (dbg_qh(qh))
1991*4882a593Smuzhiyun 		dev_vdbg(hsotg->dev, "%s()\n", __func__);
1992*4882a593Smuzhiyun 
1993*4882a593Smuzhiyun 	if (dwc2_qh_is_non_per(qh)) {
1994*4882a593Smuzhiyun 		dwc2_hcd_qh_unlink(hsotg, qh);
1995*4882a593Smuzhiyun 		if (!list_empty(&qh->qtd_list))
1996*4882a593Smuzhiyun 			/* Add back to inactive/waiting non-periodic schedule */
1997*4882a593Smuzhiyun 			dwc2_hcd_qh_add(hsotg, qh);
1998*4882a593Smuzhiyun 		return;
1999*4882a593Smuzhiyun 	}
2000*4882a593Smuzhiyun 
2001*4882a593Smuzhiyun 	/*
2002*4882a593Smuzhiyun 	 * Use the real frame number rather than the cached value as of the
2003*4882a593Smuzhiyun 	 * last SOF just to get us a little closer to reality.  Note that
2004*4882a593Smuzhiyun 	 * means we don't actually know if we've already handled the SOF
2005*4882a593Smuzhiyun 	 * interrupt for this frame.
2006*4882a593Smuzhiyun 	 */
2007*4882a593Smuzhiyun 	frame_number = dwc2_hcd_get_frame_number(hsotg);
2008*4882a593Smuzhiyun 
2009*4882a593Smuzhiyun 	if (sched_next_periodic_split)
2010*4882a593Smuzhiyun 		missed = dwc2_next_for_periodic_split(hsotg, qh, frame_number);
2011*4882a593Smuzhiyun 	else
2012*4882a593Smuzhiyun 		missed = dwc2_next_periodic_start(hsotg, qh, frame_number);
2013*4882a593Smuzhiyun 
2014*4882a593Smuzhiyun 	dwc2_sch_vdbg(hsotg,
2015*4882a593Smuzhiyun 		      "QH=%p next(%d) fn=%04x, sch=%04x=>%04x (%+d) miss=%d %s\n",
2016*4882a593Smuzhiyun 		     qh, sched_next_periodic_split, frame_number, old_frame,
2017*4882a593Smuzhiyun 		     qh->next_active_frame,
2018*4882a593Smuzhiyun 		     dwc2_frame_num_dec(qh->next_active_frame, old_frame),
2019*4882a593Smuzhiyun 		missed, missed ? "MISS" : "");
2020*4882a593Smuzhiyun 
2021*4882a593Smuzhiyun 	if (list_empty(&qh->qtd_list)) {
2022*4882a593Smuzhiyun 		dwc2_hcd_qh_unlink(hsotg, qh);
2023*4882a593Smuzhiyun 		return;
2024*4882a593Smuzhiyun 	}
2025*4882a593Smuzhiyun 
2026*4882a593Smuzhiyun 	/*
2027*4882a593Smuzhiyun 	 * Remove from periodic_sched_queued and move to
2028*4882a593Smuzhiyun 	 * appropriate queue
2029*4882a593Smuzhiyun 	 *
2030*4882a593Smuzhiyun 	 * Note: we purposely use the frame_number from the "hsotg" structure
2031*4882a593Smuzhiyun 	 * since we know SOF interrupt will handle future frames.
2032*4882a593Smuzhiyun 	 */
2033*4882a593Smuzhiyun 	if (dwc2_frame_num_le(qh->next_active_frame, hsotg->frame_number))
2034*4882a593Smuzhiyun 		list_move_tail(&qh->qh_list_entry,
2035*4882a593Smuzhiyun 			       &hsotg->periodic_sched_ready);
2036*4882a593Smuzhiyun 	else
2037*4882a593Smuzhiyun 		list_move_tail(&qh->qh_list_entry,
2038*4882a593Smuzhiyun 			       &hsotg->periodic_sched_inactive);
2039*4882a593Smuzhiyun }
2040*4882a593Smuzhiyun 
2041*4882a593Smuzhiyun /**
2042*4882a593Smuzhiyun  * dwc2_hcd_qtd_init() - Initializes a QTD structure
2043*4882a593Smuzhiyun  *
2044*4882a593Smuzhiyun  * @qtd: The QTD to initialize
2045*4882a593Smuzhiyun  * @urb: The associated URB
2046*4882a593Smuzhiyun  */
dwc2_hcd_qtd_init(struct dwc2_qtd * qtd,struct dwc2_hcd_urb * urb)2047*4882a593Smuzhiyun void dwc2_hcd_qtd_init(struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
2048*4882a593Smuzhiyun {
2049*4882a593Smuzhiyun 	qtd->urb = urb;
2050*4882a593Smuzhiyun 	if (dwc2_hcd_get_pipe_type(&urb->pipe_info) ==
2051*4882a593Smuzhiyun 			USB_ENDPOINT_XFER_CONTROL) {
2052*4882a593Smuzhiyun 		/*
2053*4882a593Smuzhiyun 		 * The only time the QTD data toggle is used is on the data
2054*4882a593Smuzhiyun 		 * phase of control transfers. This phase always starts with
2055*4882a593Smuzhiyun 		 * DATA1.
2056*4882a593Smuzhiyun 		 */
2057*4882a593Smuzhiyun 		qtd->data_toggle = DWC2_HC_PID_DATA1;
2058*4882a593Smuzhiyun 		qtd->control_phase = DWC2_CONTROL_SETUP;
2059*4882a593Smuzhiyun 	}
2060*4882a593Smuzhiyun 
2061*4882a593Smuzhiyun 	/* Start split */
2062*4882a593Smuzhiyun 	qtd->complete_split = 0;
2063*4882a593Smuzhiyun 	qtd->isoc_split_pos = DWC2_HCSPLT_XACTPOS_ALL;
2064*4882a593Smuzhiyun 	qtd->isoc_split_offset = 0;
2065*4882a593Smuzhiyun 	qtd->in_process = 0;
2066*4882a593Smuzhiyun 
2067*4882a593Smuzhiyun 	/* Store the qtd ptr in the urb to reference the QTD */
2068*4882a593Smuzhiyun 	urb->qtd = qtd;
2069*4882a593Smuzhiyun }
2070*4882a593Smuzhiyun 
2071*4882a593Smuzhiyun /**
2072*4882a593Smuzhiyun  * dwc2_hcd_qtd_add() - Adds a QTD to the QTD-list of a QH
2073*4882a593Smuzhiyun  *			Caller must hold driver lock.
2074*4882a593Smuzhiyun  *
2075*4882a593Smuzhiyun  * @hsotg:        The DWC HCD structure
2076*4882a593Smuzhiyun  * @qtd:          The QTD to add
2077*4882a593Smuzhiyun  * @qh:           Queue head to add qtd to
2078*4882a593Smuzhiyun  *
2079*4882a593Smuzhiyun  * Return: 0 if successful, negative error code otherwise
2080*4882a593Smuzhiyun  *
2081*4882a593Smuzhiyun  * If the QH to which the QTD is added is not currently scheduled, it is placed
2082*4882a593Smuzhiyun  * into the proper schedule based on its EP type.
2083*4882a593Smuzhiyun  */
dwc2_hcd_qtd_add(struct dwc2_hsotg * hsotg,struct dwc2_qtd * qtd,struct dwc2_qh * qh)2084*4882a593Smuzhiyun int dwc2_hcd_qtd_add(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
2085*4882a593Smuzhiyun 		     struct dwc2_qh *qh)
2086*4882a593Smuzhiyun {
2087*4882a593Smuzhiyun 	int retval;
2088*4882a593Smuzhiyun 
2089*4882a593Smuzhiyun 	if (unlikely(!qh)) {
2090*4882a593Smuzhiyun 		dev_err(hsotg->dev, "%s: Invalid QH\n", __func__);
2091*4882a593Smuzhiyun 		retval = -EINVAL;
2092*4882a593Smuzhiyun 		goto fail;
2093*4882a593Smuzhiyun 	}
2094*4882a593Smuzhiyun 
2095*4882a593Smuzhiyun 	retval = dwc2_hcd_qh_add(hsotg, qh);
2096*4882a593Smuzhiyun 	if (retval)
2097*4882a593Smuzhiyun 		goto fail;
2098*4882a593Smuzhiyun 
2099*4882a593Smuzhiyun 	qtd->qh = qh;
2100*4882a593Smuzhiyun 	list_add_tail(&qtd->qtd_list_entry, &qh->qtd_list);
2101*4882a593Smuzhiyun 
2102*4882a593Smuzhiyun 	return 0;
2103*4882a593Smuzhiyun fail:
2104*4882a593Smuzhiyun 	return retval;
2105*4882a593Smuzhiyun }
2106