1*a0d2b8daSmglocker /* $OpenBSD: dwc2_hcdqueue.c,v 1.15 2022/09/09 21:16:54 mglocker Exp $ */
225e434cbSuebayasi /* $NetBSD: dwc2_hcdqueue.c,v 1.11 2014/09/03 10:00:08 skrll Exp $ */
3578f812dSuebayasi
4578f812dSuebayasi /*
5578f812dSuebayasi * hcd_queue.c - DesignWare HS OTG Controller host queuing routines
6578f812dSuebayasi *
7578f812dSuebayasi * Copyright (C) 2004-2013 Synopsys, Inc.
8578f812dSuebayasi *
9578f812dSuebayasi * Redistribution and use in source and binary forms, with or without
10578f812dSuebayasi * modification, are permitted provided that the following conditions
11578f812dSuebayasi * are met:
12578f812dSuebayasi * 1. Redistributions of source code must retain the above copyright
13578f812dSuebayasi * notice, this list of conditions, and the following disclaimer,
14578f812dSuebayasi * without modification.
15578f812dSuebayasi * 2. Redistributions in binary form must reproduce the above copyright
16578f812dSuebayasi * notice, this list of conditions and the following disclaimer in the
17578f812dSuebayasi * documentation and/or other materials provided with the distribution.
18578f812dSuebayasi * 3. The names of the above-listed copyright holders may not be used
19578f812dSuebayasi * to endorse or promote products derived from this software without
20578f812dSuebayasi * specific prior written permission.
21578f812dSuebayasi *
22578f812dSuebayasi * ALTERNATIVELY, this software may be distributed under the terms of the
23578f812dSuebayasi * GNU General Public License ("GPL") as published by the Free Software
24578f812dSuebayasi * Foundation; either version 2 of the License, or (at your option) any
25578f812dSuebayasi * later version.
26578f812dSuebayasi *
27578f812dSuebayasi * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
28578f812dSuebayasi * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
29578f812dSuebayasi * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30578f812dSuebayasi * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
31578f812dSuebayasi * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32578f812dSuebayasi * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33578f812dSuebayasi * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34578f812dSuebayasi * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35578f812dSuebayasi * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36578f812dSuebayasi * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37578f812dSuebayasi * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38578f812dSuebayasi */
39578f812dSuebayasi
40578f812dSuebayasi /*
41578f812dSuebayasi * This file contains the functions to manage Queue Heads and Queue
42578f812dSuebayasi * Transfer Descriptors for Host mode
43578f812dSuebayasi */
44309465dbSuebayasi #include <sys/param.h>
45c95c98b7Sjmatthew #include <sys/systm.h>
46309465dbSuebayasi #include <sys/malloc.h>
47578f812dSuebayasi #include <sys/pool.h>
48578f812dSuebayasi
49309465dbSuebayasi #include <machine/bus.h>
50309465dbSuebayasi
51578f812dSuebayasi #include <dev/usb/usb.h>
52578f812dSuebayasi #include <dev/usb/usbdi.h>
53578f812dSuebayasi #include <dev/usb/usbdivar.h>
54578f812dSuebayasi #include <dev/usb/usb_mem.h>
55578f812dSuebayasi
56309465dbSuebayasi #include <dev/usb/dwc2/dwc2.h>
57309465dbSuebayasi #include <dev/usb/dwc2/dwc2var.h>
58578f812dSuebayasi
59309465dbSuebayasi #include <dev/usb/dwc2/dwc2_core.h>
60309465dbSuebayasi #include <dev/usb/dwc2/dwc2_hcd.h>
61578f812dSuebayasi
62a9beb1edSmglocker #include <dev/usb/dwc2/gcd.h>
63c2d5a92fSmglocker #include <dev/usb/dwc2/bitmap.h>
64a9beb1edSmglocker
65d05ae140Smglocker STATIC void dwc2_wait_timer_fn(void *);
66d05ae140Smglocker
67a9beb1edSmglocker /* Wait this long before releasing periodic reservation */
68a9beb1edSmglocker #define DWC2_UNRESERVE_DELAY (msecs_to_jiffies(5))
69a9beb1edSmglocker
70d05ae140Smglocker /* If we get a NAK, wait this long before retrying */
71d05ae140Smglocker #define DWC2_RETRY_WAIT_DELAY 1 /* msec */
72578f812dSuebayasi
73578f812dSuebayasi /**
74578f812dSuebayasi * dwc2_periodic_channel_available() - Checks that a channel is available for a
75578f812dSuebayasi * periodic transfer
76578f812dSuebayasi *
77578f812dSuebayasi * @hsotg: The HCD state structure for the DWC OTG controller
78578f812dSuebayasi *
79578f812dSuebayasi * Return: 0 if successful, negative error code otherwise
80578f812dSuebayasi */
dwc2_periodic_channel_available(struct dwc2_hsotg * hsotg)8105c50565Suebayasi STATIC int dwc2_periodic_channel_available(struct dwc2_hsotg *hsotg)
82578f812dSuebayasi {
83578f812dSuebayasi /*
84578f812dSuebayasi * Currently assuming that there is a dedicated host channel for
85578f812dSuebayasi * each periodic transaction plus at least one host channel for
86578f812dSuebayasi * non-periodic transactions
87578f812dSuebayasi */
88578f812dSuebayasi int status;
89578f812dSuebayasi int num_channels;
90578f812dSuebayasi
91a9beb1edSmglocker num_channels = hsotg->params.host_channels;
92a9beb1edSmglocker if ((hsotg->periodic_channels + hsotg->non_periodic_channels <
93a9beb1edSmglocker num_channels) && (hsotg->periodic_channels < num_channels - 1)) {
94578f812dSuebayasi status = 0;
95578f812dSuebayasi } else {
96578f812dSuebayasi dev_dbg(hsotg->dev,
97a9beb1edSmglocker "%s: Total channels: %d, Periodic: %d, Non-periodic: %d\n",
98a9beb1edSmglocker __func__, num_channels,
99578f812dSuebayasi hsotg->periodic_channels, hsotg->non_periodic_channels);
100578f812dSuebayasi status = -ENOSPC;
101578f812dSuebayasi }
102578f812dSuebayasi
103578f812dSuebayasi return status;
104578f812dSuebayasi }
105578f812dSuebayasi
106578f812dSuebayasi /**
107578f812dSuebayasi * dwc2_check_periodic_bandwidth() - Checks that there is sufficient bandwidth
108578f812dSuebayasi * for the specified QH in the periodic schedule
109578f812dSuebayasi *
110578f812dSuebayasi * @hsotg: The HCD state structure for the DWC OTG controller
111578f812dSuebayasi * @qh: QH containing periodic bandwidth required
112578f812dSuebayasi *
113578f812dSuebayasi * Return: 0 if successful, negative error code otherwise
114578f812dSuebayasi *
115578f812dSuebayasi * For simplicity, this calculation assumes that all the transfers in the
116578f812dSuebayasi * periodic schedule may occur in the same (micro)frame
117578f812dSuebayasi */
dwc2_check_periodic_bandwidth(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)11805c50565Suebayasi STATIC int dwc2_check_periodic_bandwidth(struct dwc2_hsotg *hsotg,
119578f812dSuebayasi struct dwc2_qh *qh)
120578f812dSuebayasi {
121578f812dSuebayasi int status;
122578f812dSuebayasi s16 max_claimed_usecs;
123578f812dSuebayasi
124578f812dSuebayasi status = 0;
125578f812dSuebayasi
126578f812dSuebayasi if (qh->dev_speed == USB_SPEED_HIGH || qh->do_split) {
127578f812dSuebayasi /*
128578f812dSuebayasi * High speed mode
129578f812dSuebayasi * Max periodic usecs is 80% x 125 usec = 100 usec
130578f812dSuebayasi */
131a9beb1edSmglocker max_claimed_usecs = 100 - qh->host_us;
132578f812dSuebayasi } else {
133578f812dSuebayasi /*
134578f812dSuebayasi * Full speed mode
135578f812dSuebayasi * Max periodic usecs is 90% x 1000 usec = 900 usec
136578f812dSuebayasi */
137a9beb1edSmglocker max_claimed_usecs = 900 - qh->host_us;
138578f812dSuebayasi }
139578f812dSuebayasi
140578f812dSuebayasi if (hsotg->periodic_usecs > max_claimed_usecs) {
141578f812dSuebayasi dev_err(hsotg->dev,
142578f812dSuebayasi "%s: already claimed usecs %d, required usecs %d\n",
143a9beb1edSmglocker __func__, hsotg->periodic_usecs, qh->host_us);
144578f812dSuebayasi status = -ENOSPC;
145578f812dSuebayasi }
146578f812dSuebayasi
147578f812dSuebayasi return status;
148578f812dSuebayasi }
149578f812dSuebayasi
150578f812dSuebayasi /**
151a9beb1edSmglocker * pmap_schedule() - Schedule time in a periodic bitmap (pmap).
152a9beb1edSmglocker *
153a9beb1edSmglocker * @map: The bitmap representing the schedule; will be updated
154a9beb1edSmglocker * upon success.
155a9beb1edSmglocker * @bits_per_period: The schedule represents several periods. This is how many
156a9beb1edSmglocker * bits are in each period. It's assumed that the beginning
157a9beb1edSmglocker * of the schedule will repeat after its end.
158a9beb1edSmglocker * @periods_in_map: The number of periods in the schedule.
159a9beb1edSmglocker * @num_bits: The number of bits we need per period we want to reserve
160a9beb1edSmglocker * in this function call.
161a9beb1edSmglocker * @interval: How often we need to be scheduled for the reservation this
162a9beb1edSmglocker * time. 1 means every period. 2 means every other period.
163a9beb1edSmglocker * ...you get the picture?
164a9beb1edSmglocker * @start: The bit number to start at. Normally 0. Must be within
165a9beb1edSmglocker * the interval or we return failure right away.
166a9beb1edSmglocker * @only_one_period: Normally we'll allow picking a start anywhere within the
167a9beb1edSmglocker * first interval, since we can still make all repetition
168a9beb1edSmglocker * requirements by doing that. However, if you pass true
169a9beb1edSmglocker * here then we'll return failure if we can't fit within
170a9beb1edSmglocker * the period that "start" is in.
171a9beb1edSmglocker *
172a9beb1edSmglocker * The idea here is that we want to schedule time for repeating events that all
173a9beb1edSmglocker * want the same resource. The resource is divided into fixed-sized periods
174a9beb1edSmglocker * and the events want to repeat every "interval" periods. The schedule
175a9beb1edSmglocker * granularity is one bit.
176a9beb1edSmglocker *
177a9beb1edSmglocker * To keep things "simple", we'll represent our schedule with a bitmap that
178a9beb1edSmglocker * contains a fixed number of periods. This gets rid of a lot of complexity
179a9beb1edSmglocker * but does mean that we need to handle things specially (and non-ideally) if
180a9beb1edSmglocker * the number of the periods in the schedule doesn't match well with the
181a9beb1edSmglocker * intervals that we're trying to schedule.
182a9beb1edSmglocker *
183a9beb1edSmglocker * Here's an explanation of the scheme we'll implement, assuming 8 periods.
184a9beb1edSmglocker * - If interval is 1, we need to take up space in each of the 8
185a9beb1edSmglocker * periods we're scheduling. Easy.
186a9beb1edSmglocker * - If interval is 2, we need to take up space in half of the
187a9beb1edSmglocker * periods. Again, easy.
188a9beb1edSmglocker * - If interval is 3, we actually need to fall back to interval 1.
189a9beb1edSmglocker * Why? Because we might need time in any period. AKA for the
190a9beb1edSmglocker * first 8 periods, we'll be in slot 0, 3, 6. Then we'll be
191a9beb1edSmglocker * in slot 1, 4, 7. Then we'll be in 2, 5. Then we'll be back to
192a9beb1edSmglocker * 0, 3, and 6. Since we could be in any frame we need to reserve
193a9beb1edSmglocker * for all of them. Sucks, but that's what you gotta do. Note that
194a9beb1edSmglocker * if we were instead scheduling 8 * 3 = 24 we'd do much better, but
195a9beb1edSmglocker * then we need more memory and time to do scheduling.
196a9beb1edSmglocker * - If interval is 4, easy.
197a9beb1edSmglocker * - If interval is 5, we again need interval 1. The schedule will be
198a9beb1edSmglocker * 0, 5, 2, 7, 4, 1, 6, 3, 0
199a9beb1edSmglocker * - If interval is 6, we need interval 2. 0, 6, 4, 2.
200a9beb1edSmglocker * - If interval is 7, we need interval 1.
201a9beb1edSmglocker * - If interval is 8, we need interval 8.
202a9beb1edSmglocker *
203a9beb1edSmglocker * If you do the math, you'll see that we need to pretend that interval is
204a9beb1edSmglocker * equal to the greatest_common_divisor(interval, periods_in_map).
205a9beb1edSmglocker *
206a9beb1edSmglocker * Note that at the moment this function tends to front-pack the schedule.
207a9beb1edSmglocker * In some cases that's really non-ideal (it's hard to schedule things that
208a9beb1edSmglocker * need to repeat every period). In other cases it's perfect (you can easily
209a9beb1edSmglocker * schedule bigger, less often repeating things).
210a9beb1edSmglocker *
211a9beb1edSmglocker * Here's the algorithm in action (8 periods, 5 bits per period):
212a9beb1edSmglocker * |** | |** | |** | |** | | OK 2 bits, intv 2 at 0
213a9beb1edSmglocker * |*****| ***|*****| ***|*****| ***|*****| ***| OK 3 bits, intv 3 at 2
214a9beb1edSmglocker * |*****|* ***|*****| ***|*****|* ***|*****| ***| OK 1 bits, intv 4 at 5
215a9beb1edSmglocker * |** |* |** | |** |* |** | | Remv 3 bits, intv 3 at 2
216a9beb1edSmglocker * |*** |* |*** | |*** |* |*** | | OK 1 bits, intv 6 at 2
217a9beb1edSmglocker * |**** |* * |**** | * |**** |* * |**** | * | OK 1 bits, intv 1 at 3
218a9beb1edSmglocker * |**** |**** |**** | *** |**** |**** |**** | *** | OK 2 bits, intv 2 at 6
219a9beb1edSmglocker * |*****|*****|*****| ****|*****|*****|*****| ****| OK 1 bits, intv 1 at 4
220a9beb1edSmglocker * |*****|*****|*****| ****|*****|*****|*****| ****| FAIL 1 bits, intv 1
221a9beb1edSmglocker * | ***|*****| ***| ****| ***|*****| ***| ****| Remv 2 bits, intv 2 at 0
222a9beb1edSmglocker * | ***| ****| ***| ****| ***| ****| ***| ****| Remv 1 bits, intv 4 at 5
223a9beb1edSmglocker * | **| ****| **| ****| **| ****| **| ****| Remv 1 bits, intv 6 at 2
224a9beb1edSmglocker * | *| ** *| *| ** *| *| ** *| *| ** *| Remv 1 bits, intv 1 at 3
225a9beb1edSmglocker * | *| *| *| *| *| *| *| *| Remv 2 bits, intv 2 at 6
226a9beb1edSmglocker * | | | | | | | | | Remv 1 bits, intv 1 at 4
227a9beb1edSmglocker * |** | |** | |** | |** | | OK 2 bits, intv 2 at 0
228a9beb1edSmglocker * |*** | |** | |*** | |** | | OK 1 bits, intv 4 at 2
229a9beb1edSmglocker * |*****| |** **| |*****| |** **| | OK 2 bits, intv 2 at 3
230a9beb1edSmglocker * |*****|* |** **| |*****|* |** **| | OK 1 bits, intv 4 at 5
231a9beb1edSmglocker * |*****|*** |** **| ** |*****|*** |** **| ** | OK 2 bits, intv 2 at 6
232a9beb1edSmglocker * |*****|*****|** **| ****|*****|*****|** **| ****| OK 2 bits, intv 2 at 8
233a9beb1edSmglocker * |*****|*****|*****| ****|*****|*****|*****| ****| OK 1 bits, intv 4 at 12
234a9beb1edSmglocker *
235a9beb1edSmglocker * This function is pretty generic and could be easily abstracted if anything
236a9beb1edSmglocker * needed similar scheduling.
237a9beb1edSmglocker *
238a9beb1edSmglocker * Returns either -ENOSPC or a >= 0 start bit which should be passed to the
239a9beb1edSmglocker * unschedule routine. The map bitmap will be updated on a non-error result.
240578f812dSuebayasi */
pmap_schedule(unsigned long * map,int bits_per_period,int periods_in_map,int num_bits,int interval,int start,bool only_one_period)241a9beb1edSmglocker static int pmap_schedule(unsigned long *map, int bits_per_period,
242a9beb1edSmglocker int periods_in_map, int num_bits,
243a9beb1edSmglocker int interval, int start, bool only_one_period)
244a9beb1edSmglocker {
245a9beb1edSmglocker int interval_bits;
246a9beb1edSmglocker int to_reserve;
247a9beb1edSmglocker int first_end;
248a9beb1edSmglocker int i;
249a9beb1edSmglocker
250a9beb1edSmglocker if (num_bits > bits_per_period)
251a9beb1edSmglocker return -ENOSPC;
252a9beb1edSmglocker
253a9beb1edSmglocker /* Adjust interval as per description */
254a9beb1edSmglocker interval = gcd(interval, periods_in_map);
255a9beb1edSmglocker
256a9beb1edSmglocker interval_bits = bits_per_period * interval;
257a9beb1edSmglocker to_reserve = periods_in_map / interval;
258a9beb1edSmglocker
259a9beb1edSmglocker /* If start has gotten us past interval then we can't schedule */
260a9beb1edSmglocker if (start >= interval_bits)
261a9beb1edSmglocker return -ENOSPC;
262a9beb1edSmglocker
263a9beb1edSmglocker if (only_one_period)
264a9beb1edSmglocker /* Must fit within same period as start; end at begin of next */
265a9beb1edSmglocker first_end = (start / bits_per_period + 1) * bits_per_period;
266a9beb1edSmglocker else
267a9beb1edSmglocker /* Can fit anywhere in the first interval */
268a9beb1edSmglocker first_end = interval_bits;
269a9beb1edSmglocker
270a9beb1edSmglocker /*
271a9beb1edSmglocker * We'll try to pick the first repetition, then see if that time
272a9beb1edSmglocker * is free for each of the subsequent repetitions. If it's not
273a9beb1edSmglocker * we'll adjust the start time for the next search of the first
274a9beb1edSmglocker * repetition.
275a9beb1edSmglocker */
276a9beb1edSmglocker while (start + num_bits <= first_end) {
277a9beb1edSmglocker int end;
278a9beb1edSmglocker
279a9beb1edSmglocker /* Need to stay within this period */
280a9beb1edSmglocker end = (start / bits_per_period + 1) * bits_per_period;
281a9beb1edSmglocker
282a9beb1edSmglocker /* Look for num_bits us in this microframe starting at start */
283a9beb1edSmglocker start = bitmap_find_next_zero_area(map, end, start, num_bits,
284a9beb1edSmglocker 0);
285a9beb1edSmglocker
286a9beb1edSmglocker /*
287a9beb1edSmglocker * We should get start >= end if we fail. We might be
288a9beb1edSmglocker * able to check the next microframe depending on the
289a9beb1edSmglocker * interval, so continue on (start already updated).
290a9beb1edSmglocker */
291a9beb1edSmglocker if (start >= end) {
292a9beb1edSmglocker start = end;
293a9beb1edSmglocker continue;
294a9beb1edSmglocker }
295a9beb1edSmglocker
296a9beb1edSmglocker /* At this point we have a valid point for first one */
297a9beb1edSmglocker for (i = 1; i < to_reserve; i++) {
298a9beb1edSmglocker int ith_start = start + interval_bits * i;
299a9beb1edSmglocker int ith_end = end + interval_bits * i;
300a9beb1edSmglocker int ret;
301a9beb1edSmglocker
302a9beb1edSmglocker /* Use this as a dumb "check if bits are 0" */
303a9beb1edSmglocker ret = bitmap_find_next_zero_area(
304a9beb1edSmglocker map, ith_start + num_bits, ith_start, num_bits,
305a9beb1edSmglocker 0);
306a9beb1edSmglocker
307a9beb1edSmglocker /* We got the right place, continue checking */
308a9beb1edSmglocker if (ret == ith_start)
309a9beb1edSmglocker continue;
310a9beb1edSmglocker
311a9beb1edSmglocker /* Move start up for next time and exit for loop */
312a9beb1edSmglocker ith_start = bitmap_find_next_zero_area(
313a9beb1edSmglocker map, ith_end, ith_start, num_bits, 0);
314a9beb1edSmglocker if (ith_start >= ith_end)
315a9beb1edSmglocker /* Need a while new period next time */
316a9beb1edSmglocker start = end;
317a9beb1edSmglocker else
318a9beb1edSmglocker start = ith_start - interval_bits * i;
319a9beb1edSmglocker break;
320a9beb1edSmglocker }
321a9beb1edSmglocker
322a9beb1edSmglocker /* If didn't exit the for loop with a break, we have success */
323a9beb1edSmglocker if (i == to_reserve)
324a9beb1edSmglocker break;
325a9beb1edSmglocker }
326a9beb1edSmglocker
327a9beb1edSmglocker if (start + num_bits > first_end)
328a9beb1edSmglocker return -ENOSPC;
329a9beb1edSmglocker
330a9beb1edSmglocker for (i = 0; i < to_reserve; i++) {
331a9beb1edSmglocker int ith_start = start + interval_bits * i;
332a9beb1edSmglocker
333a9beb1edSmglocker bitmap_set(map, ith_start, num_bits);
334a9beb1edSmglocker }
335a9beb1edSmglocker
336a9beb1edSmglocker return start;
337a9beb1edSmglocker }
338a9beb1edSmglocker
339a9beb1edSmglocker /**
340a9beb1edSmglocker * pmap_unschedule() - Undo work done by pmap_schedule()
341a9beb1edSmglocker *
342a9beb1edSmglocker * @map: See pmap_schedule().
343a9beb1edSmglocker * @bits_per_period: See pmap_schedule().
344a9beb1edSmglocker * @periods_in_map: See pmap_schedule().
345a9beb1edSmglocker * @num_bits: The number of bits that was passed to schedule.
346a9beb1edSmglocker * @interval: The interval that was passed to schedule.
347a9beb1edSmglocker * @start: The return value from pmap_schedule().
348a9beb1edSmglocker */
pmap_unschedule(unsigned long * map,int bits_per_period,int periods_in_map,int num_bits,int interval,int start)349a9beb1edSmglocker static void pmap_unschedule(unsigned long *map, int bits_per_period,
350a9beb1edSmglocker int periods_in_map, int num_bits,
351a9beb1edSmglocker int interval, int start)
352a9beb1edSmglocker {
353a9beb1edSmglocker int interval_bits;
354a9beb1edSmglocker int to_release;
355a9beb1edSmglocker int i;
356a9beb1edSmglocker
357a9beb1edSmglocker /* Adjust interval as per description in pmap_schedule() */
358a9beb1edSmglocker interval = gcd(interval, periods_in_map);
359a9beb1edSmglocker
360a9beb1edSmglocker interval_bits = bits_per_period * interval;
361a9beb1edSmglocker to_release = periods_in_map / interval;
362a9beb1edSmglocker
363a9beb1edSmglocker for (i = 0; i < to_release; i++) {
364a9beb1edSmglocker int ith_start = start + interval_bits * i;
365a9beb1edSmglocker
366a9beb1edSmglocker bitmap_clear(map, ith_start, num_bits);
367a9beb1edSmglocker }
368a9beb1edSmglocker }
369a9beb1edSmglocker
370a9beb1edSmglocker /**
371a9beb1edSmglocker * dwc2_get_ls_map() - Get the map used for the given qh
372a9beb1edSmglocker *
373a9beb1edSmglocker * @hsotg: The HCD state structure for the DWC OTG controller.
374a9beb1edSmglocker * @qh: QH for the periodic transfer.
375a9beb1edSmglocker *
376a9beb1edSmglocker * We'll always get the periodic map out of our TT. Note that even if we're
377a9beb1edSmglocker * running the host straight in low speed / full speed mode it appears as if
378a9beb1edSmglocker * a TT is allocated for us, so we'll use it. If that ever changes we can
379a9beb1edSmglocker * add logic here to get a map out of "hsotg" if !qh->do_split.
380a9beb1edSmglocker *
381a9beb1edSmglocker * Returns: the map or NULL if a map couldn't be found.
382a9beb1edSmglocker */
dwc2_get_ls_map(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)383a9beb1edSmglocker static unsigned long *dwc2_get_ls_map(struct dwc2_hsotg *hsotg,
384a9beb1edSmglocker struct dwc2_qh *qh)
385a9beb1edSmglocker {
386a9beb1edSmglocker unsigned long *map;
387a9beb1edSmglocker
388a9beb1edSmglocker /* Don't expect to be missing a TT and be doing low speed scheduling */
389a9beb1edSmglocker if (WARN_ON(!qh->dwc_tt))
390a9beb1edSmglocker return NULL;
391a9beb1edSmglocker
392a9beb1edSmglocker /* Get the map and adjust if this is a multi_tt hub */
393a9beb1edSmglocker map = qh->dwc_tt->periodic_bitmaps;
394a9beb1edSmglocker if (qh->dwc_tt->usb_tt->hub->multi)
395a9beb1edSmglocker map += DWC2_ELEMENTS_PER_LS_BITMAP * (qh->ttport - 1);
396a9beb1edSmglocker
397a9beb1edSmglocker return map;
398a9beb1edSmglocker }
399a9beb1edSmglocker
400a9beb1edSmglocker #ifdef DWC2_PRINT_SCHEDULE
401a9beb1edSmglocker /*
402a9beb1edSmglocker * cat_printf() - A printf() + strcat() helper
403a9beb1edSmglocker *
404a9beb1edSmglocker * This is useful for concatenating a bunch of strings where each string is
405a9beb1edSmglocker * constructed using printf.
406a9beb1edSmglocker *
407a9beb1edSmglocker * @buf: The destination buffer; will be updated to point after the printed
408a9beb1edSmglocker * data.
409a9beb1edSmglocker * @size: The number of bytes in the buffer (includes space for '\0').
410a9beb1edSmglocker * @fmt: The format for printf.
411a9beb1edSmglocker * @...: The args for printf.
412a9beb1edSmglocker */
413a9beb1edSmglocker static __printf(3, 4)
cat_printf(char ** buf,size_t * size,const char * fmt,...)414a9beb1edSmglocker void cat_printf(char **buf, size_t *size, const char *fmt, ...)
415a9beb1edSmglocker {
416a9beb1edSmglocker va_list args;
417a9beb1edSmglocker int i;
418a9beb1edSmglocker
419a9beb1edSmglocker if (*size == 0)
420a9beb1edSmglocker return;
421a9beb1edSmglocker
422a9beb1edSmglocker va_start(args, fmt);
423a9beb1edSmglocker i = vsnprintf(*buf, *size, fmt, args);
424a9beb1edSmglocker va_end(args);
425a9beb1edSmglocker
426a9beb1edSmglocker if (i >= *size) {
427a9beb1edSmglocker (*buf)[*size - 1] = '\0';
428a9beb1edSmglocker *buf += *size;
429a9beb1edSmglocker *size = 0;
430a9beb1edSmglocker } else {
431a9beb1edSmglocker *buf += i;
432a9beb1edSmglocker *size -= i;
433a9beb1edSmglocker }
434a9beb1edSmglocker }
435a9beb1edSmglocker
436a9beb1edSmglocker /*
437a9beb1edSmglocker * pmap_print() - Print the given periodic map
438a9beb1edSmglocker *
439a9beb1edSmglocker * Will attempt to print out the periodic schedule.
440a9beb1edSmglocker *
441a9beb1edSmglocker * @map: See pmap_schedule().
442a9beb1edSmglocker * @bits_per_period: See pmap_schedule().
443a9beb1edSmglocker * @periods_in_map: See pmap_schedule().
444a9beb1edSmglocker * @period_name: The name of 1 period, like "uFrame"
445a9beb1edSmglocker * @units: The name of the units, like "us".
446a9beb1edSmglocker * @print_fn: The function to call for printing.
447a9beb1edSmglocker * @print_data: Opaque data to pass to the print function.
448a9beb1edSmglocker */
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)449a9beb1edSmglocker static void pmap_print(unsigned long *map, int bits_per_period,
450a9beb1edSmglocker int periods_in_map, const char *period_name,
451a9beb1edSmglocker const char *units,
452a9beb1edSmglocker void (*print_fn)(const char *str, void *data),
453a9beb1edSmglocker void *print_data)
454a9beb1edSmglocker {
455a9beb1edSmglocker int period;
456a9beb1edSmglocker
457a9beb1edSmglocker for (period = 0; period < periods_in_map; period++) {
458a9beb1edSmglocker char tmp[64];
459a9beb1edSmglocker char *buf = tmp;
460a9beb1edSmglocker size_t buf_size = sizeof(tmp);
461a9beb1edSmglocker int period_start = period * bits_per_period;
462a9beb1edSmglocker int period_end = period_start + bits_per_period;
463a9beb1edSmglocker int start = 0;
464a9beb1edSmglocker int count = 0;
465a9beb1edSmglocker bool printed = false;
466a9beb1edSmglocker int i;
467a9beb1edSmglocker
468a9beb1edSmglocker for (i = period_start; i < period_end + 1; i++) {
469a9beb1edSmglocker /* Handle case when ith bit is set */
470a9beb1edSmglocker if (i < period_end &&
471a9beb1edSmglocker bitmap_find_next_zero_area(map, i + 1,
472a9beb1edSmglocker i, 1, 0) != i) {
473a9beb1edSmglocker if (count == 0)
474a9beb1edSmglocker start = i - period_start;
475a9beb1edSmglocker count++;
476a9beb1edSmglocker continue;
477a9beb1edSmglocker }
478a9beb1edSmglocker
479a9beb1edSmglocker /* ith bit isn't set; don't care if count == 0 */
480a9beb1edSmglocker if (count == 0)
481a9beb1edSmglocker continue;
482a9beb1edSmglocker
483a9beb1edSmglocker if (!printed)
484a9beb1edSmglocker cat_printf(&buf, &buf_size, "%s %d: ",
485a9beb1edSmglocker period_name, period);
486a9beb1edSmglocker else
487a9beb1edSmglocker cat_printf(&buf, &buf_size, ", ");
488a9beb1edSmglocker printed = true;
489a9beb1edSmglocker
490a9beb1edSmglocker cat_printf(&buf, &buf_size, "%d %s -%3d %s", start,
491a9beb1edSmglocker units, start + count - 1, units);
492a9beb1edSmglocker count = 0;
493a9beb1edSmglocker }
494a9beb1edSmglocker
495a9beb1edSmglocker if (printed)
496a9beb1edSmglocker print_fn(tmp, print_data);
497a9beb1edSmglocker }
498a9beb1edSmglocker }
499a9beb1edSmglocker
500a9beb1edSmglocker struct dwc2_qh_print_data {
501a9beb1edSmglocker struct dwc2_hsotg *hsotg;
502a9beb1edSmglocker struct dwc2_qh *qh;
503578f812dSuebayasi };
504578f812dSuebayasi
505a9beb1edSmglocker /**
506a9beb1edSmglocker * dwc2_qh_print() - Helper function for dwc2_qh_schedule_print()
507a9beb1edSmglocker *
508a9beb1edSmglocker * @str: The string to print
509a9beb1edSmglocker * @data: A pointer to a struct dwc2_qh_print_data
510a9beb1edSmglocker */
dwc2_qh_print(const char * str,void * data)511a9beb1edSmglocker static void dwc2_qh_print(const char *str, void *data)
512578f812dSuebayasi {
513a9beb1edSmglocker struct dwc2_qh_print_data *print_data = data;
514a9beb1edSmglocker
515a9beb1edSmglocker dwc2_sch_dbg(print_data->hsotg, "QH=%p ...%s\n", print_data->qh, str);
516a9beb1edSmglocker }
517a9beb1edSmglocker
518a9beb1edSmglocker /**
519a9beb1edSmglocker * dwc2_qh_schedule_print() - Print the periodic schedule
520a9beb1edSmglocker *
521a9beb1edSmglocker * @hsotg: The HCD state structure for the DWC OTG controller.
522a9beb1edSmglocker * @qh: QH to print.
523a9beb1edSmglocker */
dwc2_qh_schedule_print(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)524a9beb1edSmglocker static void dwc2_qh_schedule_print(struct dwc2_hsotg *hsotg,
525a9beb1edSmglocker struct dwc2_qh *qh)
526a9beb1edSmglocker {
527a9beb1edSmglocker struct dwc2_qh_print_data print_data = { hsotg, qh };
528578f812dSuebayasi int i;
529578f812dSuebayasi
530a9beb1edSmglocker /*
531a9beb1edSmglocker * The printing functions are quite slow and inefficient.
532a9beb1edSmglocker * If we don't have tracing turned on, don't run unless the special
533a9beb1edSmglocker * define is turned on.
534a9beb1edSmglocker */
535a9beb1edSmglocker
536a9beb1edSmglocker if (qh->schedule_low_speed) {
537a9beb1edSmglocker unsigned long *map = dwc2_get_ls_map(hsotg, qh);
538a9beb1edSmglocker
539a9beb1edSmglocker dwc2_sch_dbg(hsotg, "QH=%p LS/FS trans: %d=>%d us @ %d us",
540a9beb1edSmglocker qh, qh->device_us,
541a9beb1edSmglocker DWC2_ROUND_US_TO_SLICE(qh->device_us),
542a9beb1edSmglocker DWC2_US_PER_SLICE * qh->ls_start_schedule_slice);
543a9beb1edSmglocker
544a9beb1edSmglocker if (map) {
545a9beb1edSmglocker dwc2_sch_dbg(hsotg,
546a9beb1edSmglocker "QH=%p Whole low/full speed map %p now:\n",
547a9beb1edSmglocker qh, map);
548a9beb1edSmglocker pmap_print(map, DWC2_LS_PERIODIC_SLICES_PER_FRAME,
549a9beb1edSmglocker DWC2_LS_SCHEDULE_FRAMES, "Frame ", "slices",
550a9beb1edSmglocker dwc2_qh_print, &print_data);
551a9beb1edSmglocker }
552578f812dSuebayasi }
553578f812dSuebayasi
554a9beb1edSmglocker for (i = 0; i < qh->num_hs_transfers; i++) {
555a9beb1edSmglocker struct dwc2_hs_transfer_time *trans_time = qh->hs_transfers + i;
556a9beb1edSmglocker int uframe = trans_time->start_schedule_us /
557a9beb1edSmglocker DWC2_HS_PERIODIC_US_PER_UFRAME;
558a9beb1edSmglocker int rel_us = trans_time->start_schedule_us %
559a9beb1edSmglocker DWC2_HS_PERIODIC_US_PER_UFRAME;
560a9beb1edSmglocker
561a9beb1edSmglocker dwc2_sch_dbg(hsotg,
562a9beb1edSmglocker "QH=%p HS trans #%d: %d us @ uFrame %d + %d us\n",
563a9beb1edSmglocker qh, i, trans_time->duration_us, uframe, rel_us);
564a9beb1edSmglocker }
565a9beb1edSmglocker if (qh->num_hs_transfers) {
566a9beb1edSmglocker dwc2_sch_dbg(hsotg, "QH=%p Whole high speed map now:\n", qh);
567a9beb1edSmglocker pmap_print(hsotg->hs_periodic_bitmap,
568a9beb1edSmglocker DWC2_HS_PERIODIC_US_PER_UFRAME,
569a9beb1edSmglocker DWC2_HS_SCHEDULE_UFRAMES, "uFrame", "us",
570a9beb1edSmglocker dwc2_qh_print, &print_data);
571a9beb1edSmglocker }
572a9beb1edSmglocker }
573a9beb1edSmglocker #else
dwc2_qh_schedule_print(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)574a9beb1edSmglocker static inline void dwc2_qh_schedule_print(struct dwc2_hsotg *hsotg,
575a9beb1edSmglocker struct dwc2_qh *qh) {};
576a9beb1edSmglocker #endif
577a9beb1edSmglocker
578a9beb1edSmglocker /**
579a9beb1edSmglocker * dwc2_ls_pmap_schedule() - Schedule a low speed QH
580a9beb1edSmglocker *
581a9beb1edSmglocker * @hsotg: The HCD state structure for the DWC OTG controller.
582a9beb1edSmglocker * @qh: QH for the periodic transfer.
583a9beb1edSmglocker * @search_slice: We'll start trying to schedule at the passed slice.
584a9beb1edSmglocker * Remember that slices are the units of the low speed
585a9beb1edSmglocker * schedule (think 25us or so).
586a9beb1edSmglocker *
587a9beb1edSmglocker * Wraps pmap_schedule() with the right parameters for low speed scheduling.
588a9beb1edSmglocker *
589a9beb1edSmglocker * Normally we schedule low speed devices on the map associated with the TT.
590a9beb1edSmglocker *
591a9beb1edSmglocker * Returns: 0 for success or an error code.
592a9beb1edSmglocker */
dwc2_ls_pmap_schedule(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh,int search_slice)593a9beb1edSmglocker static int dwc2_ls_pmap_schedule(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
594a9beb1edSmglocker int search_slice)
595578f812dSuebayasi {
596a9beb1edSmglocker int slices = DIV_ROUND_UP(qh->device_us, DWC2_US_PER_SLICE);
597a9beb1edSmglocker unsigned long *map = dwc2_get_ls_map(hsotg, qh);
598a9beb1edSmglocker int slice;
599a9beb1edSmglocker
600a9beb1edSmglocker if (!map)
601a9beb1edSmglocker return -EINVAL;
602a9beb1edSmglocker
603a9beb1edSmglocker /*
604a9beb1edSmglocker * Schedule on the proper low speed map with our low speed scheduling
605a9beb1edSmglocker * parameters. Note that we use the "device_interval" here since
606a9beb1edSmglocker * we want the low speed interval and the only way we'd be in this
607a9beb1edSmglocker * function is if the device is low speed.
608a9beb1edSmglocker *
609a9beb1edSmglocker * If we happen to be doing low speed and high speed scheduling for the
610a9beb1edSmglocker * same transaction (AKA we have a split) we always do low speed first.
611a9beb1edSmglocker * That means we can always pass "false" for only_one_period (that
612a9beb1edSmglocker * parameters is only useful when we're trying to get one schedule to
613a9beb1edSmglocker * match what we already planned in the other schedule).
614a9beb1edSmglocker */
615a9beb1edSmglocker slice = pmap_schedule(map, DWC2_LS_PERIODIC_SLICES_PER_FRAME,
616a9beb1edSmglocker DWC2_LS_SCHEDULE_FRAMES, slices,
617a9beb1edSmglocker qh->device_interval, search_slice, false);
618a9beb1edSmglocker
619a9beb1edSmglocker if (slice < 0)
620a9beb1edSmglocker return slice;
621a9beb1edSmglocker
622a9beb1edSmglocker qh->ls_start_schedule_slice = slice;
623a9beb1edSmglocker return 0;
624a9beb1edSmglocker }
625a9beb1edSmglocker
626a9beb1edSmglocker /**
627a9beb1edSmglocker * dwc2_ls_pmap_unschedule() - Undo work done by dwc2_ls_pmap_schedule()
628a9beb1edSmglocker *
629a9beb1edSmglocker * @hsotg: The HCD state structure for the DWC OTG controller.
630a9beb1edSmglocker * @qh: QH for the periodic transfer.
631a9beb1edSmglocker */
dwc2_ls_pmap_unschedule(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)632a9beb1edSmglocker static void dwc2_ls_pmap_unschedule(struct dwc2_hsotg *hsotg,
633a9beb1edSmglocker struct dwc2_qh *qh)
634a9beb1edSmglocker {
635a9beb1edSmglocker int slices = DIV_ROUND_UP(qh->device_us, DWC2_US_PER_SLICE);
636a9beb1edSmglocker unsigned long *map = dwc2_get_ls_map(hsotg, qh);
637a9beb1edSmglocker
638a9beb1edSmglocker /* Schedule should have failed, so no worries about no error code */
639a9beb1edSmglocker if (!map)
640a9beb1edSmglocker return;
641a9beb1edSmglocker
642a9beb1edSmglocker pmap_unschedule(map, DWC2_LS_PERIODIC_SLICES_PER_FRAME,
643a9beb1edSmglocker DWC2_LS_SCHEDULE_FRAMES, slices, qh->device_interval,
644a9beb1edSmglocker qh->ls_start_schedule_slice);
645a9beb1edSmglocker }
646a9beb1edSmglocker
647a9beb1edSmglocker /**
648a9beb1edSmglocker * dwc2_hs_pmap_schedule - Schedule in the main high speed schedule
649a9beb1edSmglocker *
650a9beb1edSmglocker * This will schedule something on the main dwc2 schedule.
651a9beb1edSmglocker *
652a9beb1edSmglocker * We'll start looking in qh->hs_transfers[index].start_schedule_us. We'll
653a9beb1edSmglocker * update this with the result upon success. We also use the duration from
654a9beb1edSmglocker * the same structure.
655a9beb1edSmglocker *
656a9beb1edSmglocker * @hsotg: The HCD state structure for the DWC OTG controller.
657a9beb1edSmglocker * @qh: QH for the periodic transfer.
658a9beb1edSmglocker * @only_one_period: If true we will limit ourselves to just looking at
659a9beb1edSmglocker * one period (aka one 100us chunk). This is used if we have
660a9beb1edSmglocker * already scheduled something on the low speed schedule and
661a9beb1edSmglocker * need to find something that matches on the high speed one.
662a9beb1edSmglocker * @index: The index into qh->hs_transfers that we're working with.
663a9beb1edSmglocker *
664a9beb1edSmglocker * Returns: 0 for success or an error code. Upon success the
665a9beb1edSmglocker * dwc2_hs_transfer_time specified by "index" will be updated.
666a9beb1edSmglocker */
dwc2_hs_pmap_schedule(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh,bool only_one_period,int index)667a9beb1edSmglocker static int dwc2_hs_pmap_schedule(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
668a9beb1edSmglocker bool only_one_period, int index)
669a9beb1edSmglocker {
670a9beb1edSmglocker struct dwc2_hs_transfer_time *trans_time = qh->hs_transfers + index;
671a9beb1edSmglocker int us;
672a9beb1edSmglocker
673a9beb1edSmglocker us = pmap_schedule(hsotg->hs_periodic_bitmap,
674a9beb1edSmglocker DWC2_HS_PERIODIC_US_PER_UFRAME,
675a9beb1edSmglocker DWC2_HS_SCHEDULE_UFRAMES, trans_time->duration_us,
676a9beb1edSmglocker qh->host_interval, trans_time->start_schedule_us,
677a9beb1edSmglocker only_one_period);
678a9beb1edSmglocker
679a9beb1edSmglocker if (us < 0)
680a9beb1edSmglocker return us;
681a9beb1edSmglocker
682a9beb1edSmglocker trans_time->start_schedule_us = us;
683a9beb1edSmglocker return 0;
684a9beb1edSmglocker }
685a9beb1edSmglocker
686a9beb1edSmglocker /**
687a9beb1edSmglocker * dwc2_hs_pmap_unschedule() - Undo work done by dwc2_hs_pmap_schedule()
688a9beb1edSmglocker *
689a9beb1edSmglocker * @hsotg: The HCD state structure for the DWC OTG controller.
690a9beb1edSmglocker * @qh: QH for the periodic transfer.
691a9beb1edSmglocker * @index: Transfer index
692a9beb1edSmglocker */
dwc2_hs_pmap_unschedule(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh,int index)693a9beb1edSmglocker static void dwc2_hs_pmap_unschedule(struct dwc2_hsotg *hsotg,
694a9beb1edSmglocker struct dwc2_qh *qh, int index)
695a9beb1edSmglocker {
696a9beb1edSmglocker struct dwc2_hs_transfer_time *trans_time = qh->hs_transfers + index;
697a9beb1edSmglocker
698a9beb1edSmglocker pmap_unschedule(hsotg->hs_periodic_bitmap,
699a9beb1edSmglocker DWC2_HS_PERIODIC_US_PER_UFRAME,
700a9beb1edSmglocker DWC2_HS_SCHEDULE_UFRAMES, trans_time->duration_us,
701a9beb1edSmglocker qh->host_interval, trans_time->start_schedule_us);
702a9beb1edSmglocker }
703a9beb1edSmglocker
704a9beb1edSmglocker /**
705a9beb1edSmglocker * dwc2_uframe_schedule_split - Schedule a QH for a periodic split xfer.
706a9beb1edSmglocker *
707a9beb1edSmglocker * This is the most complicated thing in USB. We have to find matching time
708a9beb1edSmglocker * in both the global high speed schedule for the port and the low speed
709a9beb1edSmglocker * schedule for the TT associated with the given device.
710a9beb1edSmglocker *
711a9beb1edSmglocker * Being here means that the host must be running in high speed mode and the
712a9beb1edSmglocker * device is in low or full speed mode (and behind a hub).
713a9beb1edSmglocker *
714a9beb1edSmglocker * @hsotg: The HCD state structure for the DWC OTG controller.
715a9beb1edSmglocker * @qh: QH for the periodic transfer.
716a9beb1edSmglocker */
dwc2_uframe_schedule_split(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)717a9beb1edSmglocker static int dwc2_uframe_schedule_split(struct dwc2_hsotg *hsotg,
718a9beb1edSmglocker struct dwc2_qh *qh)
719a9beb1edSmglocker {
720a9beb1edSmglocker int bytecount = qh->maxp_mult * qh->maxp;
721a9beb1edSmglocker int ls_search_slice;
722a9beb1edSmglocker int err = 0;
723a9beb1edSmglocker int host_interval_in_sched;
724a9beb1edSmglocker
725a9beb1edSmglocker /*
726a9beb1edSmglocker * The interval (how often to repeat) in the actual host schedule.
727a9beb1edSmglocker * See pmap_schedule() for gcd() explanation.
728a9beb1edSmglocker */
729a9beb1edSmglocker host_interval_in_sched = gcd(qh->host_interval,
730a9beb1edSmglocker DWC2_HS_SCHEDULE_UFRAMES);
731a9beb1edSmglocker
732a9beb1edSmglocker /*
733a9beb1edSmglocker * We always try to find space in the low speed schedule first, then
734a9beb1edSmglocker * try to find high speed time that matches. If we don't, we'll bump
735a9beb1edSmglocker * up the place we start searching in the low speed schedule and try
736a9beb1edSmglocker * again. To start we'll look right at the beginning of the low speed
737a9beb1edSmglocker * schedule.
738a9beb1edSmglocker *
739a9beb1edSmglocker * Note that this will tend to front-load the high speed schedule.
740a9beb1edSmglocker * We may eventually want to try to avoid this by either considering
741a9beb1edSmglocker * both schedules together or doing some sort of round robin.
742a9beb1edSmglocker */
743a9beb1edSmglocker ls_search_slice = 0;
744a9beb1edSmglocker
745a9beb1edSmglocker while (ls_search_slice < DWC2_LS_SCHEDULE_SLICES) {
746a9beb1edSmglocker int start_s_uframe;
747a9beb1edSmglocker int ssplit_s_uframe;
748a9beb1edSmglocker int second_s_uframe;
749a9beb1edSmglocker int rel_uframe;
750a9beb1edSmglocker int first_count;
751a9beb1edSmglocker int middle_count;
752a9beb1edSmglocker int end_count;
753a9beb1edSmglocker int first_data_bytes;
754a9beb1edSmglocker int other_data_bytes;
755578f812dSuebayasi int i;
756578f812dSuebayasi
757a9beb1edSmglocker if (qh->schedule_low_speed) {
758a9beb1edSmglocker err = dwc2_ls_pmap_schedule(hsotg, qh, ls_search_slice);
759578f812dSuebayasi
760578f812dSuebayasi /*
761a9beb1edSmglocker * If we got an error here there's no other magic we
762a9beb1edSmglocker * can do, so bail. All the looping above is only
763a9beb1edSmglocker * helpful to redo things if we got a low speed slot
764a9beb1edSmglocker * and then couldn't find a matching high speed slot.
765578f812dSuebayasi */
766a9beb1edSmglocker if (err)
767a9beb1edSmglocker return err;
768578f812dSuebayasi } else {
769a9beb1edSmglocker /* Must be missing the tt structure? Why? */
770a9beb1edSmglocker WARN_ON_ONCE(1);
771578f812dSuebayasi }
772578f812dSuebayasi
773a9beb1edSmglocker /*
774a9beb1edSmglocker * This will give us a number 0 - 7 if
775a9beb1edSmglocker * DWC2_LS_SCHEDULE_FRAMES == 1, or 0 - 15 if == 2, or ...
776a9beb1edSmglocker */
777a9beb1edSmglocker start_s_uframe = qh->ls_start_schedule_slice /
778a9beb1edSmglocker DWC2_SLICES_PER_UFRAME;
779a9beb1edSmglocker
780a9beb1edSmglocker /* Get a number that's always 0 - 7 */
781a9beb1edSmglocker rel_uframe = (start_s_uframe % 8);
782a9beb1edSmglocker
783a9beb1edSmglocker /*
784a9beb1edSmglocker * If we were going to start in uframe 7 then we would need to
785a9beb1edSmglocker * issue a start split in uframe 6, which spec says is not OK.
786a9beb1edSmglocker * Move on to the next full frame (assuming there is one).
787a9beb1edSmglocker *
788a9beb1edSmglocker * See 11.18.4 Host Split Transaction Scheduling Requirements
789a9beb1edSmglocker * bullet 1.
790a9beb1edSmglocker */
791a9beb1edSmglocker if (rel_uframe == 7) {
792a9beb1edSmglocker if (qh->schedule_low_speed)
793a9beb1edSmglocker dwc2_ls_pmap_unschedule(hsotg, qh);
794a9beb1edSmglocker ls_search_slice =
795a9beb1edSmglocker (qh->ls_start_schedule_slice /
796a9beb1edSmglocker DWC2_LS_PERIODIC_SLICES_PER_FRAME + 1) *
797a9beb1edSmglocker DWC2_LS_PERIODIC_SLICES_PER_FRAME;
798a9beb1edSmglocker continue;
799a9beb1edSmglocker }
800a9beb1edSmglocker
801a9beb1edSmglocker /*
802a9beb1edSmglocker * For ISOC in:
803a9beb1edSmglocker * - start split (frame -1)
804a9beb1edSmglocker * - complete split w/ data (frame +1)
805a9beb1edSmglocker * - complete split w/ data (frame +2)
806a9beb1edSmglocker * - ...
807a9beb1edSmglocker * - complete split w/ data (frame +num_data_packets)
808a9beb1edSmglocker * - complete split w/ data (frame +num_data_packets+1)
809a9beb1edSmglocker * - complete split w/ data (frame +num_data_packets+2, max 8)
810a9beb1edSmglocker * ...though if frame was "0" then max is 7...
811a9beb1edSmglocker *
812a9beb1edSmglocker * For ISOC out we might need to do:
813a9beb1edSmglocker * - start split w/ data (frame -1)
814a9beb1edSmglocker * - start split w/ data (frame +0)
815a9beb1edSmglocker * - ...
816a9beb1edSmglocker * - start split w/ data (frame +num_data_packets-2)
817a9beb1edSmglocker *
818a9beb1edSmglocker * For INTERRUPT in we might need to do:
819a9beb1edSmglocker * - start split (frame -1)
820a9beb1edSmglocker * - complete split w/ data (frame +1)
821a9beb1edSmglocker * - complete split w/ data (frame +2)
822a9beb1edSmglocker * - complete split w/ data (frame +3, max 8)
823a9beb1edSmglocker *
824a9beb1edSmglocker * For INTERRUPT out we might need to do:
825a9beb1edSmglocker * - start split w/ data (frame -1)
826a9beb1edSmglocker * - complete split (frame +1)
827a9beb1edSmglocker * - complete split (frame +2)
828a9beb1edSmglocker * - complete split (frame +3, max 8)
829a9beb1edSmglocker *
830a9beb1edSmglocker * Start adjusting!
831a9beb1edSmglocker */
832a9beb1edSmglocker ssplit_s_uframe = (start_s_uframe +
833a9beb1edSmglocker host_interval_in_sched - 1) %
834a9beb1edSmglocker host_interval_in_sched;
835a9beb1edSmglocker if (qh->ep_type == USB_ENDPOINT_XFER_ISOC && !qh->ep_is_in)
836a9beb1edSmglocker second_s_uframe = start_s_uframe;
837a9beb1edSmglocker else
838a9beb1edSmglocker second_s_uframe = start_s_uframe + 1;
839a9beb1edSmglocker
840a9beb1edSmglocker /* First data transfer might not be all 188 bytes. */
841a9beb1edSmglocker first_data_bytes = 188 -
842a9beb1edSmglocker DIV_ROUND_UP(188 * (qh->ls_start_schedule_slice %
843a9beb1edSmglocker DWC2_SLICES_PER_UFRAME),
844a9beb1edSmglocker DWC2_SLICES_PER_UFRAME);
845a9beb1edSmglocker if (first_data_bytes > bytecount)
846a9beb1edSmglocker first_data_bytes = bytecount;
847a9beb1edSmglocker other_data_bytes = bytecount - first_data_bytes;
848a9beb1edSmglocker
849a9beb1edSmglocker /*
850a9beb1edSmglocker * For now, skip OUT xfers where first xfer is partial
851a9beb1edSmglocker *
852a9beb1edSmglocker * Main dwc2 code assumes:
853a9beb1edSmglocker * - INT transfers never get split in two.
854a9beb1edSmglocker * - ISOC transfers can always transfer 188 bytes the first
855a9beb1edSmglocker * time.
856a9beb1edSmglocker *
857a9beb1edSmglocker * Until that code is fixed, try again if the first transfer
858a9beb1edSmglocker * couldn't transfer everything.
859a9beb1edSmglocker *
860a9beb1edSmglocker * This code can be removed if/when the rest of dwc2 handles
861a9beb1edSmglocker * the above cases. Until it's fixed we just won't be able
862a9beb1edSmglocker * to schedule quite as tightly.
863a9beb1edSmglocker */
864a9beb1edSmglocker if (!qh->ep_is_in &&
865a9beb1edSmglocker (first_data_bytes != min_t(int, 188, bytecount))) {
866a9beb1edSmglocker dwc2_sch_dbg(hsotg,
867a9beb1edSmglocker "QH=%p avoiding broken 1st xfer (%d, %d)\n",
868a9beb1edSmglocker qh, first_data_bytes, bytecount);
869a9beb1edSmglocker if (qh->schedule_low_speed)
870a9beb1edSmglocker dwc2_ls_pmap_unschedule(hsotg, qh);
871a9beb1edSmglocker ls_search_slice = (start_s_uframe + 1) *
872a9beb1edSmglocker DWC2_SLICES_PER_UFRAME;
873a9beb1edSmglocker continue;
874a9beb1edSmglocker }
875a9beb1edSmglocker
876a9beb1edSmglocker /* Start by assuming transfers for the bytes */
877a9beb1edSmglocker qh->num_hs_transfers = 1 + DIV_ROUND_UP(other_data_bytes, 188);
878a9beb1edSmglocker
879a9beb1edSmglocker /*
880a9beb1edSmglocker * Everything except ISOC OUT has extra transfers. Rules are
881a9beb1edSmglocker * complicated. See 11.18.4 Host Split Transaction Scheduling
882a9beb1edSmglocker * Requirements bullet 3.
883a9beb1edSmglocker */
884a9beb1edSmglocker if (qh->ep_type == USB_ENDPOINT_XFER_INT) {
885a9beb1edSmglocker if (rel_uframe == 6)
886a9beb1edSmglocker qh->num_hs_transfers += 2;
887a9beb1edSmglocker else
888a9beb1edSmglocker qh->num_hs_transfers += 3;
889a9beb1edSmglocker
890a9beb1edSmglocker if (qh->ep_is_in) {
891a9beb1edSmglocker /*
892a9beb1edSmglocker * First is start split, middle/end is data.
893a9beb1edSmglocker * Allocate full data bytes for all data.
894a9beb1edSmglocker */
895a9beb1edSmglocker first_count = 4;
896a9beb1edSmglocker middle_count = bytecount;
897a9beb1edSmglocker end_count = bytecount;
898a9beb1edSmglocker } else {
899a9beb1edSmglocker /*
900a9beb1edSmglocker * First is data, middle/end is complete.
901a9beb1edSmglocker * First transfer and second can have data.
902a9beb1edSmglocker * Rest should just have complete split.
903a9beb1edSmglocker */
904a9beb1edSmglocker first_count = first_data_bytes;
905a9beb1edSmglocker middle_count = max_t(int, 4, other_data_bytes);
906a9beb1edSmglocker end_count = 4;
907a9beb1edSmglocker }
908a9beb1edSmglocker } else {
909a9beb1edSmglocker if (qh->ep_is_in) {
910a9beb1edSmglocker int last;
911a9beb1edSmglocker
912a9beb1edSmglocker /* Account for the start split */
913a9beb1edSmglocker qh->num_hs_transfers++;
914a9beb1edSmglocker
915a9beb1edSmglocker /* Calculate "L" value from spec */
916a9beb1edSmglocker last = rel_uframe + qh->num_hs_transfers + 1;
917a9beb1edSmglocker
918a9beb1edSmglocker /* Start with basic case */
919a9beb1edSmglocker if (last <= 6)
920a9beb1edSmglocker qh->num_hs_transfers += 2;
921a9beb1edSmglocker else
922a9beb1edSmglocker qh->num_hs_transfers += 1;
923a9beb1edSmglocker
924a9beb1edSmglocker /* Adjust downwards */
925a9beb1edSmglocker if (last >= 6 && rel_uframe == 0)
926a9beb1edSmglocker qh->num_hs_transfers--;
927a9beb1edSmglocker
928a9beb1edSmglocker /* 1st = start; rest can contain data */
929a9beb1edSmglocker first_count = 4;
930a9beb1edSmglocker middle_count = min_t(int, 188, bytecount);
931a9beb1edSmglocker end_count = middle_count;
932a9beb1edSmglocker } else {
933a9beb1edSmglocker /* All contain data, last might be smaller */
934a9beb1edSmglocker first_count = first_data_bytes;
935a9beb1edSmglocker middle_count = min_t(int, 188,
936a9beb1edSmglocker other_data_bytes);
937a9beb1edSmglocker end_count = other_data_bytes % 188;
938a9beb1edSmglocker }
939a9beb1edSmglocker }
940a9beb1edSmglocker
941a9beb1edSmglocker /* Assign durations per uFrame */
942a9beb1edSmglocker qh->hs_transfers[0].duration_us = HS_USECS_ISO(first_count);
943a9beb1edSmglocker for (i = 1; i < qh->num_hs_transfers - 1; i++)
944a9beb1edSmglocker qh->hs_transfers[i].duration_us =
945a9beb1edSmglocker HS_USECS_ISO(middle_count);
946a9beb1edSmglocker if (qh->num_hs_transfers > 1)
947a9beb1edSmglocker qh->hs_transfers[qh->num_hs_transfers - 1].duration_us =
948a9beb1edSmglocker HS_USECS_ISO(end_count);
949a9beb1edSmglocker
950a9beb1edSmglocker /*
951a9beb1edSmglocker * Assign start us. The call below to dwc2_hs_pmap_schedule()
952a9beb1edSmglocker * will start with these numbers but may adjust within the same
953a9beb1edSmglocker * microframe.
954a9beb1edSmglocker */
955a9beb1edSmglocker qh->hs_transfers[0].start_schedule_us =
956a9beb1edSmglocker ssplit_s_uframe * DWC2_HS_PERIODIC_US_PER_UFRAME;
957a9beb1edSmglocker for (i = 1; i < qh->num_hs_transfers; i++)
958a9beb1edSmglocker qh->hs_transfers[i].start_schedule_us =
959a9beb1edSmglocker ((second_s_uframe + i - 1) %
960a9beb1edSmglocker DWC2_HS_SCHEDULE_UFRAMES) *
961a9beb1edSmglocker DWC2_HS_PERIODIC_US_PER_UFRAME;
962a9beb1edSmglocker
963a9beb1edSmglocker /* Try to schedule with filled in hs_transfers above */
964a9beb1edSmglocker for (i = 0; i < qh->num_hs_transfers; i++) {
965a9beb1edSmglocker err = dwc2_hs_pmap_schedule(hsotg, qh, true, i);
966a9beb1edSmglocker if (err)
967a9beb1edSmglocker break;
968a9beb1edSmglocker }
969a9beb1edSmglocker
970a9beb1edSmglocker /* If we scheduled all w/out breaking out then we're all good */
971a9beb1edSmglocker if (i == qh->num_hs_transfers)
972a9beb1edSmglocker break;
973a9beb1edSmglocker
974a9beb1edSmglocker for (; i >= 0; i--)
975a9beb1edSmglocker dwc2_hs_pmap_unschedule(hsotg, qh, i);
976a9beb1edSmglocker
977a9beb1edSmglocker if (qh->schedule_low_speed)
978a9beb1edSmglocker dwc2_ls_pmap_unschedule(hsotg, qh);
979a9beb1edSmglocker
980a9beb1edSmglocker /* Try again starting in the next microframe */
981a9beb1edSmglocker ls_search_slice = (start_s_uframe + 1) * DWC2_SLICES_PER_UFRAME;
982a9beb1edSmglocker }
983a9beb1edSmglocker
984a9beb1edSmglocker if (ls_search_slice >= DWC2_LS_SCHEDULE_SLICES)
985a9beb1edSmglocker return -ENOSPC;
986a9beb1edSmglocker
987a9beb1edSmglocker return 0;
988a9beb1edSmglocker }
989a9beb1edSmglocker
990a9beb1edSmglocker /**
991a9beb1edSmglocker * dwc2_uframe_schedule_hs - Schedule a QH for a periodic high speed xfer.
992a9beb1edSmglocker *
993a9beb1edSmglocker * Basically this just wraps dwc2_hs_pmap_schedule() to provide a clean
994a9beb1edSmglocker * interface.
995a9beb1edSmglocker *
996a9beb1edSmglocker * @hsotg: The HCD state structure for the DWC OTG controller.
997a9beb1edSmglocker * @qh: QH for the periodic transfer.
998a9beb1edSmglocker */
dwc2_uframe_schedule_hs(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)999a9beb1edSmglocker static int dwc2_uframe_schedule_hs(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1000a9beb1edSmglocker {
1001a9beb1edSmglocker /* In non-split host and device time are the same */
1002a9beb1edSmglocker WARN_ON(qh->host_us != qh->device_us);
1003a9beb1edSmglocker WARN_ON(qh->host_interval != qh->device_interval);
1004a9beb1edSmglocker WARN_ON(qh->num_hs_transfers != 1);
1005a9beb1edSmglocker
1006a9beb1edSmglocker /* We'll have one transfer; init start to 0 before calling scheduler */
1007a9beb1edSmglocker qh->hs_transfers[0].start_schedule_us = 0;
1008a9beb1edSmglocker qh->hs_transfers[0].duration_us = qh->host_us;
1009a9beb1edSmglocker
1010a9beb1edSmglocker return dwc2_hs_pmap_schedule(hsotg, qh, false, 0);
1011a9beb1edSmglocker }
1012a9beb1edSmglocker
1013a9beb1edSmglocker /**
1014a9beb1edSmglocker * dwc2_uframe_schedule_ls - Schedule a QH for a periodic low/full speed xfer.
1015a9beb1edSmglocker *
1016a9beb1edSmglocker * Basically this just wraps dwc2_ls_pmap_schedule() to provide a clean
1017a9beb1edSmglocker * interface.
1018a9beb1edSmglocker *
1019a9beb1edSmglocker * @hsotg: The HCD state structure for the DWC OTG controller.
1020a9beb1edSmglocker * @qh: QH for the periodic transfer.
1021a9beb1edSmglocker */
dwc2_uframe_schedule_ls(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)1022a9beb1edSmglocker static int dwc2_uframe_schedule_ls(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1023a9beb1edSmglocker {
1024a9beb1edSmglocker /* In non-split host and device time are the same */
1025a9beb1edSmglocker WARN_ON(qh->host_us != qh->device_us);
1026a9beb1edSmglocker WARN_ON(qh->host_interval != qh->device_interval);
1027a9beb1edSmglocker WARN_ON(!qh->schedule_low_speed);
1028a9beb1edSmglocker
1029a9beb1edSmglocker /* Run on the main low speed schedule (no split = no hub = no TT) */
1030a9beb1edSmglocker return dwc2_ls_pmap_schedule(hsotg, qh, 0);
1031a9beb1edSmglocker }
1032a9beb1edSmglocker
1033a9beb1edSmglocker /**
1034a9beb1edSmglocker * dwc2_uframe_schedule - Schedule a QH for a periodic xfer.
1035a9beb1edSmglocker *
1036a9beb1edSmglocker * Calls one of the 3 sub-function depending on what type of transfer this QH
1037a9beb1edSmglocker * is for. Also adds some printing.
1038a9beb1edSmglocker *
1039a9beb1edSmglocker * @hsotg: The HCD state structure for the DWC OTG controller.
1040a9beb1edSmglocker * @qh: QH for the periodic transfer.
1041a9beb1edSmglocker */
dwc2_uframe_schedule(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)1042a9beb1edSmglocker static int dwc2_uframe_schedule(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1043578f812dSuebayasi {
1044578f812dSuebayasi int ret;
1045578f812dSuebayasi
1046a9beb1edSmglocker if (qh->dev_speed == USB_SPEED_HIGH)
1047a9beb1edSmglocker ret = dwc2_uframe_schedule_hs(hsotg, qh);
1048a9beb1edSmglocker else if (!qh->do_split)
1049a9beb1edSmglocker ret = dwc2_uframe_schedule_ls(hsotg, qh);
1050a9beb1edSmglocker else
1051a9beb1edSmglocker ret = dwc2_uframe_schedule_split(hsotg, qh);
1052a9beb1edSmglocker
1053a9beb1edSmglocker if (ret)
1054a9beb1edSmglocker dwc2_sch_dbg(hsotg, "QH=%p Failed to schedule %d\n", qh, ret);
1055a9beb1edSmglocker else
1056a9beb1edSmglocker dwc2_qh_schedule_print(hsotg, qh);
1057a9beb1edSmglocker
1058a9beb1edSmglocker return ret;
1059a9beb1edSmglocker }
1060a9beb1edSmglocker
1061a9beb1edSmglocker /**
1062a9beb1edSmglocker * dwc2_uframe_unschedule - Undoes dwc2_uframe_schedule().
1063a9beb1edSmglocker *
1064a9beb1edSmglocker * @hsotg: The HCD state structure for the DWC OTG controller.
1065a9beb1edSmglocker * @qh: QH for the periodic transfer.
1066a9beb1edSmglocker */
dwc2_uframe_unschedule(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)1067a9beb1edSmglocker static void dwc2_uframe_unschedule(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1068a9beb1edSmglocker {
1069a9beb1edSmglocker int i;
1070a9beb1edSmglocker
1071a9beb1edSmglocker for (i = 0; i < qh->num_hs_transfers; i++)
1072a9beb1edSmglocker dwc2_hs_pmap_unschedule(hsotg, qh, i);
1073a9beb1edSmglocker
1074a9beb1edSmglocker if (qh->schedule_low_speed)
1075a9beb1edSmglocker dwc2_ls_pmap_unschedule(hsotg, qh);
1076a9beb1edSmglocker
1077a9beb1edSmglocker dwc2_sch_dbg(hsotg, "QH=%p Unscheduled\n", qh);
1078a9beb1edSmglocker }
1079a9beb1edSmglocker
1080a9beb1edSmglocker /**
1081a9beb1edSmglocker * dwc2_pick_first_frame() - Choose 1st frame for qh that's already scheduled
1082a9beb1edSmglocker *
1083a9beb1edSmglocker * Takes a qh that has already been scheduled (which means we know we have the
1084a9beb1edSmglocker * bandwdith reserved for us) and set the next_active_frame and the
1085a9beb1edSmglocker * start_active_frame.
1086a9beb1edSmglocker *
1087a9beb1edSmglocker * This is expected to be called on qh's that weren't previously actively
1088a9beb1edSmglocker * running. It just picks the next frame that we can fit into without any
1089a9beb1edSmglocker * thought about the past.
1090a9beb1edSmglocker *
1091a9beb1edSmglocker * @hsotg: The HCD state structure for the DWC OTG controller
1092a9beb1edSmglocker * @qh: QH for a periodic endpoint
1093a9beb1edSmglocker *
1094a9beb1edSmglocker */
dwc2_pick_first_frame(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)1095a9beb1edSmglocker static void dwc2_pick_first_frame(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1096a9beb1edSmglocker {
1097a9beb1edSmglocker u16 frame_number;
1098a9beb1edSmglocker u16 earliest_frame;
1099a9beb1edSmglocker u16 next_active_frame;
1100a9beb1edSmglocker u16 relative_frame;
1101a9beb1edSmglocker u16 interval;
1102a9beb1edSmglocker
1103a9beb1edSmglocker /*
1104a9beb1edSmglocker * Use the real frame number rather than the cached value as of the
1105a9beb1edSmglocker * last SOF to give us a little extra slop.
1106a9beb1edSmglocker */
1107a9beb1edSmglocker frame_number = dwc2_hcd_get_frame_number(hsotg);
1108a9beb1edSmglocker
1109a9beb1edSmglocker /*
1110a9beb1edSmglocker * We wouldn't want to start any earlier than the next frame just in
1111a9beb1edSmglocker * case the frame number ticks as we're doing this calculation.
1112a9beb1edSmglocker *
1113a9beb1edSmglocker * NOTE: if we could quantify how long till we actually get scheduled
1114a9beb1edSmglocker * we might be able to avoid the "+ 1" by looking at the upper part of
1115a9beb1edSmglocker * HFNUM (the FRREM field). For now we'll just use the + 1 though.
1116a9beb1edSmglocker */
1117a9beb1edSmglocker earliest_frame = dwc2_frame_num_inc(frame_number, 1);
1118a9beb1edSmglocker next_active_frame = earliest_frame;
1119a9beb1edSmglocker
1120a9beb1edSmglocker /* Get the "no microframe schduler" out of the way... */
1121a9beb1edSmglocker if (!hsotg->params.uframe_sched) {
1122a9beb1edSmglocker if (qh->do_split)
1123a9beb1edSmglocker /* Splits are active at microframe 0 minus 1 */
1124a9beb1edSmglocker next_active_frame |= 0x7;
1125a9beb1edSmglocker goto exit;
1126a9beb1edSmglocker }
1127a9beb1edSmglocker
1128a9beb1edSmglocker if (qh->dev_speed == USB_SPEED_HIGH || qh->do_split) {
1129a9beb1edSmglocker /*
1130a9beb1edSmglocker * We're either at high speed or we're doing a split (which
1131a9beb1edSmglocker * means we're talking high speed to a hub). In any case
1132a9beb1edSmglocker * the first frame should be based on when the first scheduled
1133a9beb1edSmglocker * event is.
1134a9beb1edSmglocker */
1135a9beb1edSmglocker WARN_ON(qh->num_hs_transfers < 1);
1136a9beb1edSmglocker
1137a9beb1edSmglocker relative_frame = qh->hs_transfers[0].start_schedule_us /
1138a9beb1edSmglocker DWC2_HS_PERIODIC_US_PER_UFRAME;
1139a9beb1edSmglocker
1140a9beb1edSmglocker /* Adjust interval as per high speed schedule */
1141a9beb1edSmglocker interval = gcd(qh->host_interval, DWC2_HS_SCHEDULE_UFRAMES);
1142a9beb1edSmglocker
1143578f812dSuebayasi } else {
1144578f812dSuebayasi /*
1145a9beb1edSmglocker * Low or full speed directly on dwc2. Just about the same
1146a9beb1edSmglocker * as high speed but on a different schedule and with slightly
1147a9beb1edSmglocker * different adjustments. Note that this works because when
1148a9beb1edSmglocker * the host and device are both low speed then frames in the
1149a9beb1edSmglocker * controller tick at low speed.
1150578f812dSuebayasi */
1151a9beb1edSmglocker relative_frame = qh->ls_start_schedule_slice /
1152a9beb1edSmglocker DWC2_LS_PERIODIC_SLICES_PER_FRAME;
1153a9beb1edSmglocker interval = gcd(qh->host_interval, DWC2_LS_SCHEDULE_FRAMES);
1154578f812dSuebayasi }
1155a9beb1edSmglocker
1156a9beb1edSmglocker /* Scheduler messed up if frame is past interval */
1157a9beb1edSmglocker WARN_ON(relative_frame >= interval);
1158a9beb1edSmglocker
1159a9beb1edSmglocker /*
1160a9beb1edSmglocker * We know interval must divide (HFNUM_MAX_FRNUM + 1) now that we've
1161a9beb1edSmglocker * done the gcd(), so it's safe to move to the beginning of the current
1162a9beb1edSmglocker * interval like this.
1163a9beb1edSmglocker *
1164a9beb1edSmglocker * After this we might be before earliest_frame, but don't worry,
1165a9beb1edSmglocker * we'll fix it...
1166a9beb1edSmglocker */
1167a9beb1edSmglocker next_active_frame = (next_active_frame / interval) * interval;
1168a9beb1edSmglocker
1169a9beb1edSmglocker /*
1170a9beb1edSmglocker * Actually choose to start at the frame number we've been
1171a9beb1edSmglocker * scheduled for.
1172a9beb1edSmglocker */
1173a9beb1edSmglocker next_active_frame = dwc2_frame_num_inc(next_active_frame,
1174a9beb1edSmglocker relative_frame);
1175a9beb1edSmglocker
1176a9beb1edSmglocker /*
1177a9beb1edSmglocker * We actually need 1 frame before since the next_active_frame is
1178a9beb1edSmglocker * the frame number we'll be put on the ready list and we won't be on
1179a9beb1edSmglocker * the bus until 1 frame later.
1180a9beb1edSmglocker */
1181a9beb1edSmglocker next_active_frame = dwc2_frame_num_dec(next_active_frame, 1);
1182a9beb1edSmglocker
1183a9beb1edSmglocker /*
1184a9beb1edSmglocker * By now we might actually be before the earliest_frame. Let's move
1185a9beb1edSmglocker * up intervals until we're not.
1186a9beb1edSmglocker */
1187a9beb1edSmglocker while (dwc2_frame_num_gt(earliest_frame, next_active_frame))
1188a9beb1edSmglocker next_active_frame = dwc2_frame_num_inc(next_active_frame,
1189a9beb1edSmglocker interval);
1190a9beb1edSmglocker
1191a9beb1edSmglocker exit:
1192a9beb1edSmglocker qh->next_active_frame = next_active_frame;
1193a9beb1edSmglocker qh->start_active_frame = next_active_frame;
1194a9beb1edSmglocker
1195a9beb1edSmglocker dwc2_sch_vdbg(hsotg, "QH=%p First fn=%04x nxt=%04x\n",
1196a9beb1edSmglocker qh, frame_number, qh->next_active_frame);
1197a9beb1edSmglocker }
1198a9beb1edSmglocker
1199a9beb1edSmglocker /**
1200a9beb1edSmglocker * dwc2_do_reserve() - Make a periodic reservation
1201a9beb1edSmglocker *
1202a9beb1edSmglocker * Try to allocate space in the periodic schedule. Depending on parameters
1203a9beb1edSmglocker * this might use the microframe scheduler or the dumb scheduler.
1204a9beb1edSmglocker *
1205a9beb1edSmglocker * @hsotg: The HCD state structure for the DWC OTG controller
1206a9beb1edSmglocker * @qh: QH for the periodic transfer.
1207a9beb1edSmglocker *
1208a9beb1edSmglocker * Returns: 0 upon success; error upon failure.
1209a9beb1edSmglocker */
dwc2_do_reserve(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)1210a9beb1edSmglocker static int dwc2_do_reserve(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1211a9beb1edSmglocker {
1212a9beb1edSmglocker int status;
1213a9beb1edSmglocker
1214a9beb1edSmglocker if (hsotg->params.uframe_sched) {
1215a9beb1edSmglocker status = dwc2_uframe_schedule(hsotg, qh);
1216a9beb1edSmglocker } else {
1217a9beb1edSmglocker status = dwc2_periodic_channel_available(hsotg);
1218a9beb1edSmglocker if (status) {
1219a9beb1edSmglocker dev_info(hsotg->dev,
1220a9beb1edSmglocker "%s: No host channel available for periodic transfer\n",
1221a9beb1edSmglocker __func__);
1222a9beb1edSmglocker return status;
1223a9beb1edSmglocker }
1224a9beb1edSmglocker
1225a9beb1edSmglocker status = dwc2_check_periodic_bandwidth(hsotg, qh);
1226a9beb1edSmglocker }
1227a9beb1edSmglocker
1228a9beb1edSmglocker if (status) {
1229a9beb1edSmglocker dev_dbg(hsotg->dev,
1230a9beb1edSmglocker "%s: Insufficient periodic bandwidth for periodic transfer\n",
1231a9beb1edSmglocker __func__);
1232a9beb1edSmglocker return status;
1233a9beb1edSmglocker }
1234a9beb1edSmglocker
1235a9beb1edSmglocker if (!hsotg->params.uframe_sched)
1236a9beb1edSmglocker /* Reserve periodic channel */
1237a9beb1edSmglocker hsotg->periodic_channels++;
1238a9beb1edSmglocker
1239a9beb1edSmglocker /* Update claimed usecs per (micro)frame */
1240a9beb1edSmglocker hsotg->periodic_usecs += qh->host_us;
1241a9beb1edSmglocker
1242a9beb1edSmglocker dwc2_pick_first_frame(hsotg, qh);
1243a9beb1edSmglocker
1244a9beb1edSmglocker return 0;
1245a9beb1edSmglocker }
1246a9beb1edSmglocker
1247a9beb1edSmglocker /**
1248a9beb1edSmglocker * dwc2_do_unreserve() - Actually release the periodic reservation
1249a9beb1edSmglocker *
1250a9beb1edSmglocker * This function actually releases the periodic bandwidth that was reserved
1251a9beb1edSmglocker * by the given qh.
1252a9beb1edSmglocker *
1253a9beb1edSmglocker * @hsotg: The HCD state structure for the DWC OTG controller
1254a9beb1edSmglocker * @qh: QH for the periodic transfer.
1255a9beb1edSmglocker */
dwc2_do_unreserve(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)1256a9beb1edSmglocker static void dwc2_do_unreserve(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1257a9beb1edSmglocker {
1258a9beb1edSmglocker MUTEX_ASSERT_LOCKED(&hsotg->lock);
1259a9beb1edSmglocker
1260a9beb1edSmglocker WARN_ON(!qh->unreserve_pending);
1261a9beb1edSmglocker
1262a9beb1edSmglocker /* No more unreserve pending--we're doing it */
1263a9beb1edSmglocker qh->unreserve_pending = false;
1264a9beb1edSmglocker
1265a9beb1edSmglocker if (WARN_ON(!list_empty(&qh->qh_list_entry)))
1266a9beb1edSmglocker list_del_init(&qh->qh_list_entry);
1267a9beb1edSmglocker
1268a9beb1edSmglocker /* Update claimed usecs per (micro)frame */
1269a9beb1edSmglocker hsotg->periodic_usecs -= qh->host_us;
1270a9beb1edSmglocker
1271a9beb1edSmglocker if (hsotg->params.uframe_sched) {
1272a9beb1edSmglocker dwc2_uframe_unschedule(hsotg, qh);
1273a9beb1edSmglocker } else {
1274a9beb1edSmglocker /* Release periodic channel reservation */
1275a9beb1edSmglocker hsotg->periodic_channels--;
1276a9beb1edSmglocker }
1277a9beb1edSmglocker }
1278a9beb1edSmglocker
1279a9beb1edSmglocker /**
1280a9beb1edSmglocker * dwc2_unreserve_timer_fn() - Timer function to release periodic reservation
1281a9beb1edSmglocker *
1282a9beb1edSmglocker * According to the kernel doc for usb_submit_urb() (specifically the part about
1283a9beb1edSmglocker * "Reserved Bandwidth Transfers"), we need to keep a reservation active as
1284a9beb1edSmglocker * long as a device driver keeps submitting. Since we're using HCD_BH to give
1285a9beb1edSmglocker * back the URB we need to give the driver a little bit of time before we
1286a9beb1edSmglocker * release the reservation. This worker is called after the appropriate
1287a9beb1edSmglocker * delay.
1288a9beb1edSmglocker *
1289a9beb1edSmglocker * @t: Address to a qh unreserve_work.
1290a9beb1edSmglocker */
dwc2_unreserve_timer_fn(void * arg)1291a9beb1edSmglocker static void dwc2_unreserve_timer_fn(void *arg)
1292a9beb1edSmglocker {
1293a9beb1edSmglocker struct dwc2_qh *qh = arg;
1294a9beb1edSmglocker struct dwc2_hsotg *hsotg = qh->hsotg;
1295a9beb1edSmglocker unsigned long flags;
1296a9beb1edSmglocker
1297a9beb1edSmglocker /*
1298a9beb1edSmglocker * Wait for the lock, or for us to be scheduled again. We
1299a9beb1edSmglocker * could be scheduled again if:
1300a9beb1edSmglocker * - We started executing but didn't get the lock yet.
1301a9beb1edSmglocker * - A new reservation came in, but cancel didn't take effect
1302a9beb1edSmglocker * because we already started executing.
1303a9beb1edSmglocker * - The timer has been kicked again.
1304a9beb1edSmglocker * In that case cancel and wait for the next call.
1305a9beb1edSmglocker */
1306a9beb1edSmglocker while (!spin_trylock_irqsave(&hsotg->lock, flags)) {
1307a9beb1edSmglocker if (timeout_pending(&qh->unreserve_timer))
1308a9beb1edSmglocker return;
1309a9beb1edSmglocker }
1310a9beb1edSmglocker
1311a9beb1edSmglocker /*
1312a9beb1edSmglocker * Might be no more unreserve pending if:
1313a9beb1edSmglocker * - We started executing but didn't get the lock yet.
1314a9beb1edSmglocker * - A new reservation came in, but cancel didn't take effect
1315a9beb1edSmglocker * because we already started executing.
1316a9beb1edSmglocker *
1317a9beb1edSmglocker * We can't put this in the loop above because unreserve_pending needs
1318a9beb1edSmglocker * to be accessed under lock, so we can only check it once we got the
1319a9beb1edSmglocker * lock.
1320a9beb1edSmglocker */
1321a9beb1edSmglocker if (qh->unreserve_pending)
1322a9beb1edSmglocker dwc2_do_unreserve(hsotg, qh);
1323a9beb1edSmglocker
1324a9beb1edSmglocker spin_unlock_irqrestore(&hsotg->lock, flags);
1325578f812dSuebayasi }
1326578f812dSuebayasi
1327578f812dSuebayasi /**
1328578f812dSuebayasi * dwc2_check_max_xfer_size() - Checks that the max transfer size allowed in a
1329578f812dSuebayasi * host channel is large enough to handle the maximum data transfer in a single
1330578f812dSuebayasi * (micro)frame for a periodic transfer
1331578f812dSuebayasi *
1332578f812dSuebayasi * @hsotg: The HCD state structure for the DWC OTG controller
1333578f812dSuebayasi * @qh: QH for a periodic endpoint
1334578f812dSuebayasi *
1335578f812dSuebayasi * Return: 0 if successful, negative error code otherwise
1336578f812dSuebayasi */
dwc2_check_max_xfer_size(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)133705c50565Suebayasi STATIC int dwc2_check_max_xfer_size(struct dwc2_hsotg *hsotg,
1338578f812dSuebayasi struct dwc2_qh *qh)
1339578f812dSuebayasi {
1340578f812dSuebayasi u32 max_xfer_size;
1341578f812dSuebayasi u32 max_channel_xfer_size;
1342578f812dSuebayasi int status = 0;
1343578f812dSuebayasi
1344a9beb1edSmglocker max_xfer_size = qh->maxp * qh->maxp_mult;
1345a9beb1edSmglocker max_channel_xfer_size = hsotg->params.max_transfer_size;
1346578f812dSuebayasi
1347578f812dSuebayasi if (max_xfer_size > max_channel_xfer_size) {
1348578f812dSuebayasi dev_err(hsotg->dev,
1349578f812dSuebayasi "%s: Periodic xfer length %d > max xfer length for channel %d\n",
1350578f812dSuebayasi __func__, max_xfer_size, max_channel_xfer_size);
1351578f812dSuebayasi status = -ENOSPC;
1352578f812dSuebayasi }
1353578f812dSuebayasi
1354578f812dSuebayasi return status;
1355578f812dSuebayasi }
1356578f812dSuebayasi
1357578f812dSuebayasi /**
1358578f812dSuebayasi * dwc2_schedule_periodic() - Schedules an interrupt or isochronous transfer in
1359578f812dSuebayasi * the periodic schedule
1360578f812dSuebayasi *
1361578f812dSuebayasi * @hsotg: The HCD state structure for the DWC OTG controller
1362578f812dSuebayasi * @qh: QH for the periodic transfer. The QH should already contain the
1363578f812dSuebayasi * scheduling information.
1364578f812dSuebayasi *
1365578f812dSuebayasi * Return: 0 if successful, negative error code otherwise
1366578f812dSuebayasi */
dwc2_schedule_periodic(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)136705c50565Suebayasi STATIC int dwc2_schedule_periodic(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1368578f812dSuebayasi {
1369578f812dSuebayasi int status;
1370578f812dSuebayasi
1371578f812dSuebayasi status = dwc2_check_max_xfer_size(hsotg, qh);
1372578f812dSuebayasi if (status) {
1373578f812dSuebayasi dev_dbg(hsotg->dev,
1374578f812dSuebayasi "%s: Channel max transfer size too small for periodic transfer\n",
1375578f812dSuebayasi __func__);
1376578f812dSuebayasi return status;
1377578f812dSuebayasi }
1378578f812dSuebayasi
1379a9beb1edSmglocker /* Cancel pending unreserve; if canceled OK, unreserve was pending */
1380a9beb1edSmglocker if (timeout_del(&qh->unreserve_timer))
1381a9beb1edSmglocker WARN_ON(!qh->unreserve_pending);
1382a9beb1edSmglocker
1383a9beb1edSmglocker /*
1384a9beb1edSmglocker * Only need to reserve if there's not an unreserve pending, since if an
1385a9beb1edSmglocker * unreserve is pending then by definition our old reservation is still
1386a9beb1edSmglocker * valid. Unreserve might still be pending even if we didn't cancel if
1387a9beb1edSmglocker * dwc2_unreserve_timer_fn() already started. Code in the timer handles
1388a9beb1edSmglocker * that case.
1389a9beb1edSmglocker */
1390a9beb1edSmglocker if (!qh->unreserve_pending) {
1391a9beb1edSmglocker status = dwc2_do_reserve(hsotg, qh);
1392a9beb1edSmglocker if (status)
1393a9beb1edSmglocker return status;
1394a9beb1edSmglocker } else {
1395a9beb1edSmglocker /*
1396a9beb1edSmglocker * It might have been a while, so make sure that frame_number
1397a9beb1edSmglocker * is still good. Note: we could also try to use the similar
1398a9beb1edSmglocker * dwc2_next_periodic_start() but that schedules much more
1399a9beb1edSmglocker * tightly and we might need to hurry and queue things up.
1400a9beb1edSmglocker */
1401a9beb1edSmglocker if (dwc2_frame_num_le(qh->next_active_frame,
1402a9beb1edSmglocker hsotg->frame_number))
1403a9beb1edSmglocker dwc2_pick_first_frame(hsotg, qh);
1404a9beb1edSmglocker }
1405a9beb1edSmglocker
1406a9beb1edSmglocker qh->unreserve_pending = 0;
1407a9beb1edSmglocker
1408a9beb1edSmglocker if (hsotg->params.dma_desc_enable)
1409578f812dSuebayasi /* Don't rely on SOF and start in ready schedule */
1410d05ae140Smglocker list_add_tail(&qh->qh_list_entry, &hsotg->periodic_sched_ready);
1411578f812dSuebayasi else
1412578f812dSuebayasi /* Always start in inactive schedule */
1413d05ae140Smglocker list_add_tail(&qh->qh_list_entry,
1414d05ae140Smglocker &hsotg->periodic_sched_inactive);
1415578f812dSuebayasi
1416a9beb1edSmglocker return 0;
1417578f812dSuebayasi }
1418578f812dSuebayasi
1419578f812dSuebayasi /**
1420578f812dSuebayasi * dwc2_deschedule_periodic() - Removes an interrupt or isochronous transfer
1421578f812dSuebayasi * from the periodic schedule
1422578f812dSuebayasi *
1423578f812dSuebayasi * @hsotg: The HCD state structure for the DWC OTG controller
1424578f812dSuebayasi * @qh: QH for the periodic transfer
1425578f812dSuebayasi */
dwc2_deschedule_periodic(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)142605c50565Suebayasi STATIC void dwc2_deschedule_periodic(struct dwc2_hsotg *hsotg,
1427578f812dSuebayasi struct dwc2_qh *qh)
1428578f812dSuebayasi {
1429a9beb1edSmglocker MUTEX_ASSERT_LOCKED(&hsotg->lock);
1430a9beb1edSmglocker
1431a9beb1edSmglocker /*
1432a9beb1edSmglocker * Schedule the unreserve to happen in a little bit. Cases here:
1433a9beb1edSmglocker * - Unreserve worker might be sitting there waiting to grab the lock.
1434a9beb1edSmglocker * In this case it will notice it's been schedule again and will
1435a9beb1edSmglocker * quit.
1436a9beb1edSmglocker * - Unreserve worker might not be scheduled.
1437a9beb1edSmglocker *
1438a9beb1edSmglocker * We should never already be scheduled since dwc2_schedule_periodic()
1439a9beb1edSmglocker * should have canceled the scheduled unreserve timer (hence the
1440a9beb1edSmglocker * warning on did_modify).
1441a9beb1edSmglocker *
1442a9beb1edSmglocker * We add + 1 to the timer to guarantee that at least 1 jiffy has
1443a9beb1edSmglocker * passed (otherwise if the jiffy counter might tick right after we
1444a9beb1edSmglocker * read it and we'll get no delay).
1445a9beb1edSmglocker */
1446a9beb1edSmglocker timeout_add(&qh->unreserve_timer, DWC2_UNRESERVE_DELAY + 1);
1447a9beb1edSmglocker qh->unreserve_pending = 1;
1448578f812dSuebayasi
1449d05ae140Smglocker list_del_init(&qh->qh_list_entry);
1450578f812dSuebayasi }
1451578f812dSuebayasi
1452578f812dSuebayasi /**
1453d05ae140Smglocker * dwc2_wait_timer_fn() - Timer function to re-queue after waiting
1454d05ae140Smglocker *
1455d05ae140Smglocker * As per the spec, a NAK indicates that "a function is temporarily unable to
1456d05ae140Smglocker * transmit or receive data, but will eventually be able to do so without need
1457d05ae140Smglocker * of host intervention".
1458d05ae140Smglocker *
1459d05ae140Smglocker * That means that when we encounter a NAK we're supposed to retry.
1460d05ae140Smglocker *
1461d05ae140Smglocker * ...but if we retry right away (from the interrupt handler that saw the NAK)
1462d05ae140Smglocker * then we can end up with an interrupt storm (if the other side keeps NAKing
1463d05ae140Smglocker * us) because on slow enough CPUs it could take us longer to get out of the
1464d05ae140Smglocker * interrupt routine than it takes for the device to send another NAK. That
1465d05ae140Smglocker * leads to a constant stream of NAK interrupts and the CPU locks.
1466d05ae140Smglocker *
1467d05ae140Smglocker * ...so instead of retrying right away in the case of a NAK we'll set a timer
1468d05ae140Smglocker * to retry some time later. This function handles that timer and moves the
1469d05ae140Smglocker * qh back to the "inactive" list, then queues transactions.
1470d05ae140Smglocker *
1471d05ae140Smglocker * @t: Pointer to wait_timer in a qh.
1472a9beb1edSmglocker *
1473a9beb1edSmglocker * Return: HRTIMER_NORESTART to not automatically restart this timer.
1474d05ae140Smglocker */
dwc2_wait_timer_fn(void * arg)1475d05ae140Smglocker STATIC void dwc2_wait_timer_fn(void *arg)
1476d05ae140Smglocker {
1477d05ae140Smglocker struct dwc2_qh *qh = arg;
1478d05ae140Smglocker struct dwc2_hsotg *hsotg = qh->hsotg;
1479d05ae140Smglocker unsigned long flags;
1480d05ae140Smglocker
1481d05ae140Smglocker spin_lock_irqsave(&hsotg->lock, flags);
1482d05ae140Smglocker
1483d05ae140Smglocker /*
1484d05ae140Smglocker * We'll set wait_timer_cancel to true if we want to cancel this
1485d05ae140Smglocker * operation in dwc2_hcd_qh_unlink().
1486d05ae140Smglocker */
1487d05ae140Smglocker if (!qh->wait_timer_cancel) {
1488d05ae140Smglocker enum dwc2_transaction_type tr_type;
1489d05ae140Smglocker
1490d05ae140Smglocker qh->want_wait = false;
1491d05ae140Smglocker
1492d05ae140Smglocker list_move(&qh->qh_list_entry,
1493d05ae140Smglocker &hsotg->non_periodic_sched_inactive);
1494d05ae140Smglocker
1495d05ae140Smglocker tr_type = dwc2_hcd_select_transactions(hsotg);
1496d05ae140Smglocker if (tr_type != DWC2_TRANSACTION_NONE)
1497d05ae140Smglocker dwc2_hcd_queue_transactions(hsotg, tr_type);
1498d05ae140Smglocker }
1499d05ae140Smglocker
1500d05ae140Smglocker spin_unlock_irqrestore(&hsotg->lock, flags);
1501d05ae140Smglocker }
1502d05ae140Smglocker
1503d05ae140Smglocker /**
1504a9beb1edSmglocker * dwc2_qh_init() - Initializes a QH structure
1505a9beb1edSmglocker *
1506a9beb1edSmglocker * @hsotg: The HCD state structure for the DWC OTG controller
1507a9beb1edSmglocker * @qh: The QH to init
1508a9beb1edSmglocker * @urb: Holds the information about the device/endpoint needed to initialize
1509a9beb1edSmglocker * the QH
1510a9beb1edSmglocker * @mem_flags: Flags for allocating memory.
1511a9beb1edSmglocker */
dwc2_qh_init(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh,struct dwc2_hcd_urb * urb,gfp_t mem_flags)1512a9beb1edSmglocker STATIC void dwc2_qh_init(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
1513a9beb1edSmglocker struct dwc2_hcd_urb *urb, gfp_t mem_flags)
1514a9beb1edSmglocker {
1515a9beb1edSmglocker int dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
1516a9beb1edSmglocker u8 ep_type = dwc2_hcd_get_pipe_type(&urb->pipe_info);
1517a9beb1edSmglocker bool ep_is_in = !!dwc2_hcd_is_pipe_in(&urb->pipe_info);
1518a9beb1edSmglocker bool ep_is_isoc = (ep_type == USB_ENDPOINT_XFER_ISOC);
1519a9beb1edSmglocker bool ep_is_int = (ep_type == USB_ENDPOINT_XFER_INT);
1520a9beb1edSmglocker u32 hprt = dwc2_readl(hsotg, HPRT0);
1521a9beb1edSmglocker u32 prtspd = (hprt & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
1522a9beb1edSmglocker bool do_split = (prtspd == HPRT0_SPD_HIGH_SPEED &&
1523a9beb1edSmglocker dev_speed != USB_SPEED_HIGH);
1524a9beb1edSmglocker int maxp = dwc2_hcd_get_maxp(&urb->pipe_info);
1525a9beb1edSmglocker int maxp_mult = dwc2_hcd_get_maxp_mult(&urb->pipe_info);
1526a9beb1edSmglocker int bytecount = maxp_mult * maxp;
1527a9beb1edSmglocker char *speed, *type;
1528a9beb1edSmglocker
1529a9beb1edSmglocker /* Initialize QH */
1530a9beb1edSmglocker qh->hsotg = hsotg;
1531a9beb1edSmglocker timeout_set(&qh->unreserve_timer, dwc2_unreserve_timer_fn, qh);
1532a9beb1edSmglocker timeout_set(&qh->wait_timer, dwc2_wait_timer_fn, qh);
1533a9beb1edSmglocker qh->ep_type = ep_type;
1534a9beb1edSmglocker qh->ep_is_in = ep_is_in;
1535a9beb1edSmglocker
1536a9beb1edSmglocker qh->data_toggle = DWC2_HC_PID_DATA0;
1537a9beb1edSmglocker qh->maxp = maxp;
1538a9beb1edSmglocker qh->maxp_mult = maxp_mult;
1539a9beb1edSmglocker INIT_LIST_HEAD(&qh->qtd_list);
1540a9beb1edSmglocker INIT_LIST_HEAD(&qh->qh_list_entry);
1541a9beb1edSmglocker
1542a9beb1edSmglocker qh->do_split = do_split;
1543a9beb1edSmglocker qh->dev_speed = dev_speed;
1544a9beb1edSmglocker
1545a9beb1edSmglocker if (ep_is_int || ep_is_isoc) {
1546a9beb1edSmglocker /* Compute scheduling parameters once and save them */
1547a9beb1edSmglocker int host_speed = do_split ? USB_SPEED_HIGH : dev_speed;
1548a9beb1edSmglocker struct dwc2_tt *dwc_tt = dwc2_host_get_tt_info(hsotg, urb->priv,
1549a9beb1edSmglocker mem_flags,
1550a9beb1edSmglocker &qh->ttport);
1551a9beb1edSmglocker int device_ns;
1552a9beb1edSmglocker
1553a9beb1edSmglocker qh->dwc_tt = dwc_tt;
1554a9beb1edSmglocker
1555a9beb1edSmglocker qh->host_us = NS_TO_US(dwc2_usb_calc_bus_time(host_speed,
1556a9beb1edSmglocker ep_is_in, ep_is_isoc, bytecount));
1557a9beb1edSmglocker device_ns = dwc2_usb_calc_bus_time(dev_speed, ep_is_in,
1558a9beb1edSmglocker ep_is_isoc, bytecount);
1559a9beb1edSmglocker
1560a9beb1edSmglocker if (do_split && dwc_tt)
1561a9beb1edSmglocker device_ns += dwc2_ttthink_to_ns(hsotg, urb->priv,
1562a9beb1edSmglocker dwc_tt->usb_tt->hub->ttthink);
1563a9beb1edSmglocker qh->device_us = NS_TO_US(device_ns);
1564a9beb1edSmglocker
1565a9beb1edSmglocker qh->device_interval = urb->interval;
1566a9beb1edSmglocker qh->host_interval = urb->interval * (do_split ? 8 : 1);
1567a9beb1edSmglocker
1568a9beb1edSmglocker /*
1569a9beb1edSmglocker * Schedule low speed if we're running the host in low or
1570a9beb1edSmglocker * full speed OR if we've got a "TT" to deal with to access this
1571a9beb1edSmglocker * device.
1572a9beb1edSmglocker */
1573a9beb1edSmglocker qh->schedule_low_speed = prtspd != HPRT0_SPD_HIGH_SPEED ||
1574a9beb1edSmglocker dwc_tt;
1575a9beb1edSmglocker
1576a9beb1edSmglocker if (do_split) {
1577a9beb1edSmglocker /* We won't know num transfers until we schedule */
1578a9beb1edSmglocker qh->num_hs_transfers = -1;
1579a9beb1edSmglocker } else if (dev_speed == USB_SPEED_HIGH) {
1580a9beb1edSmglocker qh->num_hs_transfers = 1;
1581a9beb1edSmglocker } else {
1582a9beb1edSmglocker qh->num_hs_transfers = 0;
1583a9beb1edSmglocker }
1584a9beb1edSmglocker
1585a9beb1edSmglocker /* We'll schedule later when we have something to do */
1586a9beb1edSmglocker }
1587a9beb1edSmglocker
1588a9beb1edSmglocker switch (dev_speed) {
1589a9beb1edSmglocker case USB_SPEED_LOW:
1590a9beb1edSmglocker speed = "low";
1591a9beb1edSmglocker break;
1592a9beb1edSmglocker case USB_SPEED_FULL:
1593a9beb1edSmglocker speed = "full";
1594a9beb1edSmglocker break;
1595a9beb1edSmglocker case USB_SPEED_HIGH:
1596a9beb1edSmglocker speed = "high";
1597a9beb1edSmglocker break;
1598a9beb1edSmglocker default:
1599a9beb1edSmglocker speed = "?";
1600a9beb1edSmglocker break;
1601a9beb1edSmglocker }
1602a9beb1edSmglocker
1603a9beb1edSmglocker switch (qh->ep_type) {
1604a9beb1edSmglocker case USB_ENDPOINT_XFER_ISOC:
1605a9beb1edSmglocker type = "isochronous";
1606a9beb1edSmglocker break;
1607a9beb1edSmglocker case USB_ENDPOINT_XFER_INT:
1608a9beb1edSmglocker type = "interrupt";
1609a9beb1edSmglocker break;
1610a9beb1edSmglocker case USB_ENDPOINT_XFER_CONTROL:
1611a9beb1edSmglocker type = "control";
1612a9beb1edSmglocker break;
1613a9beb1edSmglocker case USB_ENDPOINT_XFER_BULK:
1614a9beb1edSmglocker type = "bulk";
1615a9beb1edSmglocker break;
1616a9beb1edSmglocker default:
1617a9beb1edSmglocker type = "?";
1618a9beb1edSmglocker break;
1619a9beb1edSmglocker }
1620a9beb1edSmglocker
1621a9beb1edSmglocker dwc2_sch_dbg(hsotg, "QH=%p Init %s, %s speed, %d bytes:\n", qh, type,
1622a9beb1edSmglocker speed, bytecount);
1623a9beb1edSmglocker dwc2_sch_dbg(hsotg, "QH=%p ...addr=%d, ep=%d, %s\n", qh,
1624a9beb1edSmglocker dwc2_hcd_get_dev_addr(&urb->pipe_info),
1625a9beb1edSmglocker dwc2_hcd_get_ep_num(&urb->pipe_info),
1626a9beb1edSmglocker ep_is_in ? "IN" : "OUT");
1627a9beb1edSmglocker if (ep_is_int || ep_is_isoc) {
1628a9beb1edSmglocker dwc2_sch_dbg(hsotg,
1629a9beb1edSmglocker "QH=%p ...duration: host=%d us, device=%d us\n",
1630a9beb1edSmglocker qh, qh->host_us, qh->device_us);
1631a9beb1edSmglocker dwc2_sch_dbg(hsotg, "QH=%p ...interval: host=%d, device=%d\n",
1632a9beb1edSmglocker qh, qh->host_interval, qh->device_interval);
1633a9beb1edSmglocker if (qh->schedule_low_speed)
1634a9beb1edSmglocker dwc2_sch_dbg(hsotg, "QH=%p ...low speed schedule=%p\n",
1635a9beb1edSmglocker qh, dwc2_get_ls_map(hsotg, qh));
1636a9beb1edSmglocker }
1637a9beb1edSmglocker }
1638a9beb1edSmglocker
1639a9beb1edSmglocker /**
1640a9beb1edSmglocker * dwc2_hcd_qh_create() - Allocates and initializes a QH
1641a9beb1edSmglocker *
1642a9beb1edSmglocker * @hsotg: The HCD state structure for the DWC OTG controller
1643a9beb1edSmglocker * @urb: Holds the information about the device/endpoint needed
1644a9beb1edSmglocker * to initialize the QH
1645a9beb1edSmglocker * @mem_flags: Flags for allocating memory.
1646a9beb1edSmglocker *
1647a9beb1edSmglocker * Return: Pointer to the newly allocated QH, or NULL on error
1648a9beb1edSmglocker */
dwc2_hcd_qh_create(struct dwc2_hsotg * hsotg,struct dwc2_hcd_urb * urb,gfp_t mem_flags)1649a9beb1edSmglocker struct dwc2_qh *dwc2_hcd_qh_create(struct dwc2_hsotg *hsotg,
1650a9beb1edSmglocker struct dwc2_hcd_urb *urb,
1651a9beb1edSmglocker gfp_t mem_flags)
1652a9beb1edSmglocker {
1653a9beb1edSmglocker struct dwc2_softc *sc = hsotg->hsotg_sc;
1654a9beb1edSmglocker struct dwc2_qh *qh;
1655a9beb1edSmglocker
1656a9beb1edSmglocker if (!urb->priv)
1657a9beb1edSmglocker return NULL;
1658a9beb1edSmglocker
1659a9beb1edSmglocker /* Allocate memory */
1660*a0d2b8daSmglocker qh = pool_get(&sc->sc_qhpool, PR_NOWAIT | PR_ZERO);
1661a9beb1edSmglocker if (!qh)
1662a9beb1edSmglocker return NULL;
1663a9beb1edSmglocker
1664a9beb1edSmglocker dwc2_qh_init(hsotg, qh, urb, mem_flags);
1665a9beb1edSmglocker
1666a9beb1edSmglocker if (hsotg->params.dma_desc_enable &&
1667a9beb1edSmglocker dwc2_hcd_qh_init_ddma(hsotg, qh, mem_flags) < 0) {
1668a9beb1edSmglocker dwc2_hcd_qh_free(hsotg, qh);
1669a9beb1edSmglocker return NULL;
1670a9beb1edSmglocker }
1671a9beb1edSmglocker
1672a9beb1edSmglocker return qh;
1673a9beb1edSmglocker }
1674a9beb1edSmglocker
1675a9beb1edSmglocker /**
1676a9beb1edSmglocker * dwc2_hcd_qh_free() - Frees the QH
1677a9beb1edSmglocker *
1678a9beb1edSmglocker * @hsotg: HCD instance
1679a9beb1edSmglocker * @qh: The QH to free
1680a9beb1edSmglocker *
1681a9beb1edSmglocker * QH should already be removed from the list. QTD list should already be empty
1682a9beb1edSmglocker * if called from URB Dequeue.
1683a9beb1edSmglocker *
1684a9beb1edSmglocker * Must NOT be called with interrupt disabled or spinlock held
1685a9beb1edSmglocker */
dwc2_hcd_qh_free(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)1686a9beb1edSmglocker void dwc2_hcd_qh_free(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1687a9beb1edSmglocker {
1688a9beb1edSmglocker struct dwc2_softc *sc = hsotg->hsotg_sc;
1689a9beb1edSmglocker
1690a9beb1edSmglocker /* Make sure any unreserve work is finished. */
1691a9beb1edSmglocker if (timeout_del(&qh->unreserve_timer)) {
1692a9beb1edSmglocker unsigned long flags;
1693a9beb1edSmglocker
1694a9beb1edSmglocker spin_lock_irqsave(&hsotg->lock, flags);
1695a9beb1edSmglocker dwc2_do_unreserve(hsotg, qh);
1696a9beb1edSmglocker spin_unlock_irqrestore(&hsotg->lock, flags);
1697a9beb1edSmglocker }
1698a9beb1edSmglocker
1699a9beb1edSmglocker /*
1700a9beb1edSmglocker * We don't have the lock so we can safely wait until the wait timer
1701a9beb1edSmglocker * finishes. Of course, at this point in time we'd better have set
1702a9beb1edSmglocker * wait_timer_active to false so if this timer was still pending it
1703a9beb1edSmglocker * won't do anything anyway, but we want it to finish before we free
1704a9beb1edSmglocker * memory.
1705a9beb1edSmglocker */
1706a9beb1edSmglocker timeout_del(&qh->wait_timer);
1707a9beb1edSmglocker
1708a9beb1edSmglocker dwc2_host_put_tt_info(hsotg, qh->dwc_tt);
1709a9beb1edSmglocker
1710a9beb1edSmglocker if (qh->desc_list)
1711a9beb1edSmglocker dwc2_hcd_qh_free_ddma(hsotg, qh);
1712a9beb1edSmglocker else if (hsotg->unaligned_cache && qh->dw_align_buf) {
1713a9beb1edSmglocker usb_freemem(&sc->sc_bus, &qh->dw_align_buf_usbdma);
1714a9beb1edSmglocker qh->dw_align_buf_dma = (dma_addr_t)0;
1715a9beb1edSmglocker }
1716a9beb1edSmglocker
1717a9beb1edSmglocker pool_put(&sc->sc_qhpool, qh);
1718a9beb1edSmglocker }
1719a9beb1edSmglocker
1720a9beb1edSmglocker /**
1721578f812dSuebayasi * dwc2_hcd_qh_add() - Adds a QH to either the non periodic or periodic
1722578f812dSuebayasi * schedule if it is not already in the schedule. If the QH is already in
1723578f812dSuebayasi * the schedule, no action is taken.
1724578f812dSuebayasi *
1725578f812dSuebayasi * @hsotg: The HCD state structure for the DWC OTG controller
1726578f812dSuebayasi * @qh: The QH to add
1727578f812dSuebayasi *
1728578f812dSuebayasi * Return: 0 if successful, negative error code otherwise
1729578f812dSuebayasi */
dwc2_hcd_qh_add(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)1730578f812dSuebayasi int dwc2_hcd_qh_add(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1731578f812dSuebayasi {
1732578f812dSuebayasi int status;
1733578f812dSuebayasi u32 intr_mask;
1734578f812dSuebayasi
1735578f812dSuebayasi if (dbg_qh(qh))
1736578f812dSuebayasi dev_vdbg(hsotg->dev, "%s()\n", __func__);
1737578f812dSuebayasi
1738d05ae140Smglocker if (!list_empty(&qh->qh_list_entry))
1739578f812dSuebayasi /* QH already in a schedule */
1740578f812dSuebayasi return 0;
1741d05ae140Smglocker
1742578f812dSuebayasi /* Add the new QH to the appropriate schedule */
1743578f812dSuebayasi if (dwc2_qh_is_non_per(qh)) {
1744a9beb1edSmglocker /* Schedule right away */
1745a9beb1edSmglocker qh->start_active_frame = hsotg->frame_number;
1746a9beb1edSmglocker qh->next_active_frame = qh->start_active_frame;
1747a9beb1edSmglocker
1748d05ae140Smglocker if (qh->want_wait) {
1749d05ae140Smglocker list_add_tail(&qh->qh_list_entry,
1750d05ae140Smglocker &hsotg->non_periodic_sched_waiting);
1751d05ae140Smglocker qh->wait_timer_cancel = false;
1752d05ae140Smglocker timeout_add_msec(&qh->wait_timer,
1753d05ae140Smglocker DWC2_RETRY_WAIT_DELAY);
1754d05ae140Smglocker } else {
1755d05ae140Smglocker list_add_tail(&qh->qh_list_entry,
1756d05ae140Smglocker &hsotg->non_periodic_sched_inactive);
1757d05ae140Smglocker }
1758578f812dSuebayasi return 0;
1759578f812dSuebayasi }
1760d05ae140Smglocker
1761578f812dSuebayasi status = dwc2_schedule_periodic(hsotg, qh);
1762578f812dSuebayasi if (status)
1763578f812dSuebayasi return status;
1764578f812dSuebayasi if (!hsotg->periodic_qh_count) {
1765a9beb1edSmglocker intr_mask = dwc2_readl(hsotg, GINTMSK);
1766578f812dSuebayasi intr_mask |= GINTSTS_SOF;
1767a9beb1edSmglocker dwc2_writel(hsotg, intr_mask, GINTMSK);
1768578f812dSuebayasi }
1769578f812dSuebayasi hsotg->periodic_qh_count++;
1770578f812dSuebayasi
1771578f812dSuebayasi return 0;
1772578f812dSuebayasi }
1773578f812dSuebayasi
1774578f812dSuebayasi /**
1775578f812dSuebayasi * dwc2_hcd_qh_unlink() - Removes a QH from either the non-periodic or periodic
1776578f812dSuebayasi * schedule. Memory is not freed.
1777578f812dSuebayasi *
1778578f812dSuebayasi * @hsotg: The HCD state structure
1779578f812dSuebayasi * @qh: QH to remove from schedule
1780578f812dSuebayasi */
dwc2_hcd_qh_unlink(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh)1781578f812dSuebayasi void dwc2_hcd_qh_unlink(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1782578f812dSuebayasi {
1783578f812dSuebayasi u32 intr_mask;
1784578f812dSuebayasi
1785578f812dSuebayasi dev_vdbg(hsotg->dev, "%s()\n", __func__);
1786578f812dSuebayasi
1787d05ae140Smglocker /* If the wait_timer is pending, this will stop it from acting */
1788d05ae140Smglocker qh->wait_timer_cancel = true;
1789d05ae140Smglocker
1790d05ae140Smglocker if (list_empty(&qh->qh_list_entry))
1791578f812dSuebayasi /* QH is not in a schedule */
1792578f812dSuebayasi return;
1793578f812dSuebayasi
1794578f812dSuebayasi if (dwc2_qh_is_non_per(qh)) {
1795d05ae140Smglocker if (hsotg->non_periodic_qh_ptr == &qh->qh_list_entry)
1796578f812dSuebayasi hsotg->non_periodic_qh_ptr =
1797d05ae140Smglocker hsotg->non_periodic_qh_ptr->next;
1798d05ae140Smglocker list_del_init(&qh->qh_list_entry);
1799578f812dSuebayasi return;
1800578f812dSuebayasi }
1801d05ae140Smglocker
1802578f812dSuebayasi dwc2_deschedule_periodic(hsotg, qh);
1803578f812dSuebayasi hsotg->periodic_qh_count--;
1804a9beb1edSmglocker if (!hsotg->periodic_qh_count &&
1805a9beb1edSmglocker !hsotg->params.dma_desc_enable) {
1806a9beb1edSmglocker intr_mask = dwc2_readl(hsotg, GINTMSK);
1807578f812dSuebayasi intr_mask &= ~GINTSTS_SOF;
1808a9beb1edSmglocker dwc2_writel(hsotg, intr_mask, GINTMSK);
1809578f812dSuebayasi }
1810578f812dSuebayasi }
1811578f812dSuebayasi
1812a9beb1edSmglocker /**
1813a9beb1edSmglocker * dwc2_next_for_periodic_split() - Set next_active_frame midway thru a split.
1814a9beb1edSmglocker *
1815a9beb1edSmglocker * This is called for setting next_active_frame for periodic splits for all but
1816a9beb1edSmglocker * the first packet of the split. Confusing? I thought so...
1817a9beb1edSmglocker *
1818a9beb1edSmglocker * Periodic splits are single low/full speed transfers that we end up splitting
1819a9beb1edSmglocker * up into several high speed transfers. They always fit into one full (1 ms)
1820a9beb1edSmglocker * frame but might be split over several microframes (125 us each). We to put
1821a9beb1edSmglocker * each of the parts on a very specific high speed frame.
1822a9beb1edSmglocker *
1823a9beb1edSmglocker * This function figures out where the next active uFrame needs to be.
1824a9beb1edSmglocker *
1825a9beb1edSmglocker * @hsotg: The HCD state structure
1826a9beb1edSmglocker * @qh: QH for the periodic transfer.
1827a9beb1edSmglocker * @frame_number: The current frame number.
1828a9beb1edSmglocker *
1829a9beb1edSmglocker * Return: number missed by (or 0 if we didn't miss).
1830578f812dSuebayasi */
dwc2_next_for_periodic_split(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh,u16 frame_number)1831a9beb1edSmglocker static int dwc2_next_for_periodic_split(struct dwc2_hsotg *hsotg,
1832a9beb1edSmglocker struct dwc2_qh *qh, u16 frame_number)
1833578f812dSuebayasi {
1834a9beb1edSmglocker u16 old_frame = qh->next_active_frame;
1835a9beb1edSmglocker u16 prev_frame_number = dwc2_frame_num_dec(frame_number, 1);
1836a9beb1edSmglocker int missed = 0;
1837578f812dSuebayasi u16 incr;
1838578f812dSuebayasi
1839578f812dSuebayasi /*
1840a9beb1edSmglocker * See dwc2_uframe_schedule_split() for split scheduling.
1841a9beb1edSmglocker *
1842a9beb1edSmglocker * Basically: increment 1 normally, but 2 right after the start split
1843a9beb1edSmglocker * (except for ISOC out).
1844578f812dSuebayasi */
1845a9beb1edSmglocker if (old_frame == qh->start_active_frame &&
1846a9beb1edSmglocker !(qh->ep_type == USB_ENDPOINT_XFER_ISOC && !qh->ep_is_in))
1847a9beb1edSmglocker incr = 2;
1848a9beb1edSmglocker else
1849a9beb1edSmglocker incr = 1;
1850a9beb1edSmglocker
1851a9beb1edSmglocker qh->next_active_frame = dwc2_frame_num_inc(old_frame, incr);
1852a9beb1edSmglocker
1853a9beb1edSmglocker /*
1854a9beb1edSmglocker * Note that it's OK for frame_number to be 1 frame past
1855a9beb1edSmglocker * next_active_frame. Remember that next_active_frame is supposed to
1856a9beb1edSmglocker * be 1 frame _before_ when we want to be scheduled. If we're 1 frame
1857a9beb1edSmglocker * past it just means schedule ASAP.
1858a9beb1edSmglocker *
1859a9beb1edSmglocker * It's _not_ OK, however, if we're more than one frame past.
1860a9beb1edSmglocker */
1861a9beb1edSmglocker if (dwc2_frame_num_gt(prev_frame_number, qh->next_active_frame)) {
1862a9beb1edSmglocker /*
1863a9beb1edSmglocker * OOPS, we missed. That's actually pretty bad since
1864a9beb1edSmglocker * the hub will be unhappy; try ASAP I guess.
1865a9beb1edSmglocker */
1866a9beb1edSmglocker missed = dwc2_frame_num_dec(prev_frame_number,
1867a9beb1edSmglocker qh->next_active_frame);
1868a9beb1edSmglocker qh->next_active_frame = frame_number;
1869578f812dSuebayasi }
1870a9beb1edSmglocker
1871a9beb1edSmglocker return missed;
1872578f812dSuebayasi }
1873a9beb1edSmglocker
1874a9beb1edSmglocker /**
1875a9beb1edSmglocker * dwc2_next_periodic_start() - Set next_active_frame for next transfer start
1876a9beb1edSmglocker *
1877a9beb1edSmglocker * This is called for setting next_active_frame for a periodic transfer for
1878a9beb1edSmglocker * all cases other than midway through a periodic split. This will also update
1879a9beb1edSmglocker * start_active_frame.
1880a9beb1edSmglocker *
1881a9beb1edSmglocker * Since we _always_ keep start_active_frame as the start of the previous
1882a9beb1edSmglocker * transfer this is normally pretty easy: we just add our interval to
1883a9beb1edSmglocker * start_active_frame and we've got our answer.
1884a9beb1edSmglocker *
1885a9beb1edSmglocker * The tricks come into play if we miss. In that case we'll look for the next
1886a9beb1edSmglocker * slot we can fit into.
1887a9beb1edSmglocker *
1888a9beb1edSmglocker * @hsotg: The HCD state structure
1889a9beb1edSmglocker * @qh: QH for the periodic transfer.
1890a9beb1edSmglocker * @frame_number: The current frame number.
1891a9beb1edSmglocker *
1892a9beb1edSmglocker * Return: number missed by (or 0 if we didn't miss).
1893a9beb1edSmglocker */
dwc2_next_periodic_start(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh,u16 frame_number)1894a9beb1edSmglocker static int dwc2_next_periodic_start(struct dwc2_hsotg *hsotg,
1895a9beb1edSmglocker struct dwc2_qh *qh, u16 frame_number)
1896a9beb1edSmglocker {
1897a9beb1edSmglocker int missed = 0;
1898a9beb1edSmglocker u16 interval = qh->host_interval;
1899a9beb1edSmglocker u16 prev_frame_number = dwc2_frame_num_dec(frame_number, 1);
1900a9beb1edSmglocker
1901a9beb1edSmglocker qh->start_active_frame = dwc2_frame_num_inc(qh->start_active_frame,
1902a9beb1edSmglocker interval);
1903a9beb1edSmglocker
1904a9beb1edSmglocker /*
1905a9beb1edSmglocker * The dwc2_frame_num_gt() function used below won't work terribly well
1906a9beb1edSmglocker * with if we just incremented by a really large intervals since the
1907a9beb1edSmglocker * frame counter only goes to 0x3fff. It's terribly unlikely that we
1908a9beb1edSmglocker * will have missed in this case anyway. Just go to exit. If we want
1909a9beb1edSmglocker * to try to do better we'll need to keep track of a bigger counter
1910a9beb1edSmglocker * somewhere in the driver and handle overflows.
1911a9beb1edSmglocker */
1912a9beb1edSmglocker if (interval >= 0x1000)
1913a9beb1edSmglocker goto exit;
1914a9beb1edSmglocker
1915a9beb1edSmglocker /*
1916a9beb1edSmglocker * Test for misses, which is when it's too late to schedule.
1917a9beb1edSmglocker *
1918a9beb1edSmglocker * A few things to note:
1919a9beb1edSmglocker * - We compare against prev_frame_number since start_active_frame
1920a9beb1edSmglocker * and next_active_frame are always 1 frame before we want things
1921a9beb1edSmglocker * to be active and we assume we can still get scheduled in the
1922a9beb1edSmglocker * current frame number.
1923a9beb1edSmglocker * - It's possible for start_active_frame (now incremented) to be
1924a9beb1edSmglocker * next_active_frame if we got an EO MISS (even_odd miss) which
1925a9beb1edSmglocker * basically means that we detected there wasn't enough time for
1926a9beb1edSmglocker * the last packet and dwc2_hc_set_even_odd_frame() rescheduled us
1927a9beb1edSmglocker * at the last second. We want to make sure we don't schedule
1928a9beb1edSmglocker * another transfer for the same frame. My test webcam doesn't seem
1929a9beb1edSmglocker * terribly upset by missing a transfer but really doesn't like when
1930a9beb1edSmglocker * we do two transfers in the same frame.
1931a9beb1edSmglocker * - Some misses are expected. Specifically, in order to work
1932a9beb1edSmglocker * perfectly dwc2 really needs quite spectacular interrupt latency
1933a9beb1edSmglocker * requirements. It needs to be able to handle its interrupts
1934a9beb1edSmglocker * completely within 125 us of them being asserted. That not only
1935a9beb1edSmglocker * means that the dwc2 interrupt handler needs to be fast but it
1936a9beb1edSmglocker * means that nothing else in the system has to block dwc2 for a long
1937a9beb1edSmglocker * time. We can help with the dwc2 parts of this, but it's hard to
1938a9beb1edSmglocker * guarantee that a system will have interrupt latency < 125 us, so
1939a9beb1edSmglocker * we have to be robust to some misses.
1940a9beb1edSmglocker */
1941a9beb1edSmglocker if (qh->start_active_frame == qh->next_active_frame ||
1942a9beb1edSmglocker dwc2_frame_num_gt(prev_frame_number, qh->start_active_frame)) {
1943a9beb1edSmglocker u16 ideal_start = qh->start_active_frame;
1944a9beb1edSmglocker int periods_in_map;
1945a9beb1edSmglocker
1946a9beb1edSmglocker /*
1947a9beb1edSmglocker * Adjust interval as per gcd with map size.
1948a9beb1edSmglocker * See pmap_schedule() for more details here.
1949a9beb1edSmglocker */
1950a9beb1edSmglocker if (qh->do_split || qh->dev_speed == USB_SPEED_HIGH)
1951a9beb1edSmglocker periods_in_map = DWC2_HS_SCHEDULE_UFRAMES;
1952a9beb1edSmglocker else
1953a9beb1edSmglocker periods_in_map = DWC2_LS_SCHEDULE_FRAMES;
1954a9beb1edSmglocker interval = gcd(interval, periods_in_map);
1955a9beb1edSmglocker
1956a9beb1edSmglocker do {
1957a9beb1edSmglocker qh->start_active_frame = dwc2_frame_num_inc(
1958a9beb1edSmglocker qh->start_active_frame, interval);
1959a9beb1edSmglocker } while (dwc2_frame_num_gt(prev_frame_number,
1960a9beb1edSmglocker qh->start_active_frame));
1961a9beb1edSmglocker
1962a9beb1edSmglocker missed = dwc2_frame_num_dec(qh->start_active_frame,
1963a9beb1edSmglocker ideal_start);
1964578f812dSuebayasi }
1965a9beb1edSmglocker
1966a9beb1edSmglocker exit:
1967a9beb1edSmglocker qh->next_active_frame = qh->start_active_frame;
1968a9beb1edSmglocker
1969a9beb1edSmglocker return missed;
1970578f812dSuebayasi }
1971578f812dSuebayasi
1972578f812dSuebayasi /*
1973578f812dSuebayasi * Deactivates a QH. For non-periodic QHs, removes the QH from the active
1974578f812dSuebayasi * non-periodic schedule. The QH is added to the inactive non-periodic
1975578f812dSuebayasi * schedule if any QTDs are still attached to the QH.
1976578f812dSuebayasi *
1977578f812dSuebayasi * For periodic QHs, the QH is removed from the periodic queued schedule. If
1978578f812dSuebayasi * there are any QTDs still attached to the QH, the QH is added to either the
1979578f812dSuebayasi * periodic inactive schedule or the periodic ready schedule and its next
1980578f812dSuebayasi * scheduled frame is calculated. The QH is placed in the ready schedule if
1981578f812dSuebayasi * the scheduled frame has been reached already. Otherwise it's placed in the
1982578f812dSuebayasi * inactive schedule. If there are no QTDs attached to the QH, the QH is
1983578f812dSuebayasi * completely removed from the periodic schedule.
1984578f812dSuebayasi */
dwc2_hcd_qh_deactivate(struct dwc2_hsotg * hsotg,struct dwc2_qh * qh,int sched_next_periodic_split)1985578f812dSuebayasi void dwc2_hcd_qh_deactivate(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
1986578f812dSuebayasi int sched_next_periodic_split)
1987578f812dSuebayasi {
1988a9beb1edSmglocker #ifdef DWC2_DEBUG
1989a9beb1edSmglocker u16 old_frame = qh->next_active_frame;
1990a9beb1edSmglocker #endif
1991578f812dSuebayasi u16 frame_number;
1992a9beb1edSmglocker int missed;
1993578f812dSuebayasi
1994578f812dSuebayasi if (dbg_qh(qh))
1995578f812dSuebayasi dev_vdbg(hsotg->dev, "%s()\n", __func__);
1996578f812dSuebayasi
1997578f812dSuebayasi if (dwc2_qh_is_non_per(qh)) {
1998578f812dSuebayasi dwc2_hcd_qh_unlink(hsotg, qh);
1999d05ae140Smglocker if (!list_empty(&qh->qtd_list))
2000d05ae140Smglocker /* Add back to inactive/waiting non-periodic schedule */
2001578f812dSuebayasi dwc2_hcd_qh_add(hsotg, qh);
2002578f812dSuebayasi return;
2003578f812dSuebayasi }
2004578f812dSuebayasi
2005a9beb1edSmglocker /*
2006a9beb1edSmglocker * Use the real frame number rather than the cached value as of the
2007a9beb1edSmglocker * last SOF just to get us a little closer to reality. Note that
2008a9beb1edSmglocker * means we don't actually know if we've already handled the SOF
2009a9beb1edSmglocker * interrupt for this frame.
2010a9beb1edSmglocker */
2011578f812dSuebayasi frame_number = dwc2_hcd_get_frame_number(hsotg);
2012578f812dSuebayasi
2013a9beb1edSmglocker if (sched_next_periodic_split)
2014a9beb1edSmglocker missed = dwc2_next_for_periodic_split(hsotg, qh, frame_number);
2015a9beb1edSmglocker else
2016a9beb1edSmglocker missed = dwc2_next_periodic_start(hsotg, qh, frame_number);
2017a9beb1edSmglocker
2018a9beb1edSmglocker dwc2_sch_vdbg(hsotg,
2019a9beb1edSmglocker "QH=%p next(%d) fn=%04x, sch=%04x=>%04x (%+d) miss=%d %s\n",
2020a9beb1edSmglocker qh, sched_next_periodic_split, frame_number, old_frame,
2021a9beb1edSmglocker qh->next_active_frame,
2022a9beb1edSmglocker dwc2_frame_num_dec(qh->next_active_frame, old_frame),
2023a9beb1edSmglocker missed, missed ? "MISS" : "");
2024578f812dSuebayasi
2025d05ae140Smglocker if (list_empty(&qh->qtd_list)) {
2026578f812dSuebayasi dwc2_hcd_qh_unlink(hsotg, qh);
2027578f812dSuebayasi return;
2028578f812dSuebayasi }
2029a9beb1edSmglocker
2030578f812dSuebayasi /*
2031578f812dSuebayasi * Remove from periodic_sched_queued and move to
2032578f812dSuebayasi * appropriate queue
2033a9beb1edSmglocker *
2034a9beb1edSmglocker * Note: we purposely use the frame_number from the "hsotg" structure
2035a9beb1edSmglocker * since we know SOF interrupt will handle future frames.
2036578f812dSuebayasi */
2037a9beb1edSmglocker if (dwc2_frame_num_le(qh->next_active_frame, hsotg->frame_number))
2038a9beb1edSmglocker list_move_tail(&qh->qh_list_entry,
2039a9beb1edSmglocker &hsotg->periodic_sched_ready);
2040d05ae140Smglocker else
2041a9beb1edSmglocker list_move_tail(&qh->qh_list_entry,
2042a9beb1edSmglocker &hsotg->periodic_sched_inactive);
2043578f812dSuebayasi }
2044578f812dSuebayasi
2045578f812dSuebayasi /**
2046578f812dSuebayasi * dwc2_hcd_qtd_init() - Initializes a QTD structure
2047578f812dSuebayasi *
2048578f812dSuebayasi * @qtd: The QTD to initialize
2049578f812dSuebayasi * @urb: The associated URB
2050578f812dSuebayasi */
dwc2_hcd_qtd_init(struct dwc2_qtd * qtd,struct dwc2_hcd_urb * urb)2051578f812dSuebayasi void dwc2_hcd_qtd_init(struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
2052578f812dSuebayasi {
2053578f812dSuebayasi qtd->urb = urb;
2054578f812dSuebayasi if (dwc2_hcd_get_pipe_type(&urb->pipe_info) ==
2055578f812dSuebayasi USB_ENDPOINT_XFER_CONTROL) {
2056578f812dSuebayasi /*
2057578f812dSuebayasi * The only time the QTD data toggle is used is on the data
2058578f812dSuebayasi * phase of control transfers. This phase always starts with
2059578f812dSuebayasi * DATA1.
2060578f812dSuebayasi */
2061578f812dSuebayasi qtd->data_toggle = DWC2_HC_PID_DATA1;
2062578f812dSuebayasi qtd->control_phase = DWC2_CONTROL_SETUP;
2063578f812dSuebayasi }
2064578f812dSuebayasi
2065578f812dSuebayasi /* Start split */
2066578f812dSuebayasi qtd->complete_split = 0;
2067578f812dSuebayasi qtd->isoc_split_pos = DWC2_HCSPLT_XACTPOS_ALL;
2068578f812dSuebayasi qtd->isoc_split_offset = 0;
2069578f812dSuebayasi qtd->in_process = 0;
2070578f812dSuebayasi
2071578f812dSuebayasi /* Store the qtd ptr in the urb to reference the QTD */
2072578f812dSuebayasi urb->qtd = qtd;
2073578f812dSuebayasi }
2074578f812dSuebayasi
2075578f812dSuebayasi /**
2076578f812dSuebayasi * dwc2_hcd_qtd_add() - Adds a QTD to the QTD-list of a QH
2077d05ae140Smglocker * Caller must hold driver lock.
2078578f812dSuebayasi *
2079578f812dSuebayasi * @hsotg: The DWC HCD structure
2080578f812dSuebayasi * @qtd: The QTD to add
2081d05ae140Smglocker * @qh: Queue head to add qtd to
2082578f812dSuebayasi *
2083578f812dSuebayasi * Return: 0 if successful, negative error code otherwise
2084578f812dSuebayasi *
2085d05ae140Smglocker * If the QH to which the QTD is added is not currently scheduled, it is placed
2086d05ae140Smglocker * into the proper schedule based on its EP type.
2087578f812dSuebayasi */
dwc2_hcd_qtd_add(struct dwc2_hsotg * hsotg,struct dwc2_qtd * qtd,struct dwc2_qh * qh)2088578f812dSuebayasi int dwc2_hcd_qtd_add(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
2089d05ae140Smglocker struct dwc2_qh *qh)
2090578f812dSuebayasi {
2091578f812dSuebayasi int retval;
2092578f812dSuebayasi
2093a9beb1edSmglocker if (unlikely(!qh)) {
2094d05ae140Smglocker dev_err(hsotg->dev, "%s: Invalid QH\n", __func__);
2095d05ae140Smglocker retval = -EINVAL;
2096d05ae140Smglocker goto fail;
2097578f812dSuebayasi }
2098578f812dSuebayasi
2099d05ae140Smglocker retval = dwc2_hcd_qh_add(hsotg, qh);
2100578f812dSuebayasi if (retval)
2101578f812dSuebayasi goto fail;
2102578f812dSuebayasi
2103d05ae140Smglocker qtd->qh = qh;
2104d05ae140Smglocker list_add_tail(&qtd->qtd_list_entry, &qh->qtd_list);
2105578f812dSuebayasi
2106578f812dSuebayasi return 0;
2107578f812dSuebayasi fail:
2108578f812dSuebayasi return retval;
2109578f812dSuebayasi }
2110578f812dSuebayasi
2111a9beb1edSmglocker /*** XXX: Include following functions in to our USB stack? *******************/
2112a9beb1edSmglocker
2113a9beb1edSmglocker #define BW_HUB_LS_SETUP 333L /* nanoseconds */
2114a9beb1edSmglocker #define BW_HOST_DELAY 1000L /* nanoseconds */
2115a9beb1edSmglocker
2116a9beb1edSmglocker long
dwc2_usb_calc_bus_time(int speed,int is_input,int isoc,int bytecount)2117a9beb1edSmglocker dwc2_usb_calc_bus_time(int speed, int is_input, int isoc, int bytecount)
2118578f812dSuebayasi {
2119a9beb1edSmglocker unsigned long tmp;
2120578f812dSuebayasi
2121578f812dSuebayasi switch (speed) {
2122a9beb1edSmglocker case USB_SPEED_LOW: /* INTR only */
2123a9beb1edSmglocker if (is_input) {
2124a9beb1edSmglocker tmp = (67667L * (31L + 10L * BitTime (bytecount))) /
2125a9beb1edSmglocker 1000L;
2126a9beb1edSmglocker return 64060L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY +
2127a9beb1edSmglocker tmp;
2128578f812dSuebayasi } else {
2129a9beb1edSmglocker tmp = (66700L * (31L + 10L * BitTime (bytecount))) /
2130a9beb1edSmglocker 1000L;
2131a9beb1edSmglocker return 64107L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY +
2132a9beb1edSmglocker tmp;
2133578f812dSuebayasi }
2134a9beb1edSmglocker case USB_SPEED_FULL: /* ISOC or INTR */
2135a9beb1edSmglocker if (isoc) {
2136a9beb1edSmglocker tmp = (8354L * (31L + 10L * BitTime (bytecount))) /
2137a9beb1edSmglocker 1000L;
2138a9beb1edSmglocker return ((is_input) ? 7268L : 6265L) + BW_HOST_DELAY +
2139a9beb1edSmglocker tmp;
2140578f812dSuebayasi } else {
2141a9beb1edSmglocker tmp = (8354L * (31L + 10L * BitTime (bytecount))) /
2142a9beb1edSmglocker 1000L;
2143a9beb1edSmglocker return 9107L + BW_HOST_DELAY + tmp;
2144578f812dSuebayasi }
2145a9beb1edSmglocker case USB_SPEED_HIGH: /* ISOC or INTR */
2146a9beb1edSmglocker /* FIXME adjust for input vs output */
2147a9beb1edSmglocker if (isoc)
2148a9beb1edSmglocker tmp = HS_NSECS_ISO (bytecount);
2149a9beb1edSmglocker else
2150a9beb1edSmglocker tmp = HS_NSECS (bytecount);
2151a9beb1edSmglocker return tmp;
2152578f812dSuebayasi default:
2153a9beb1edSmglocker printf ("%s: bogus device speed!\n", __func__);
2154a9beb1edSmglocker return -1;
2155a9beb1edSmglocker }
2156578f812dSuebayasi }
2157578f812dSuebayasi
2158a9beb1edSmglocker int
dwc2_ttthink_to_ns(struct dwc2_hsotg * hsotg,void * context,int ttthink)2159a9beb1edSmglocker dwc2_ttthink_to_ns(struct dwc2_hsotg *hsotg, void *context, int ttthink)
2160a9beb1edSmglocker {
2161a9beb1edSmglocker struct usbd_xfer *xfer = context;
2162a9beb1edSmglocker struct dwc2_pipe *dpipe = DWC2_XFER2DPIPE(xfer);
2163a9beb1edSmglocker struct usbd_device *dev = dpipe->pipe.device;
2164a9beb1edSmglocker
2165a9beb1edSmglocker /* 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
2166a9beb1edSmglocker switch (ttthink) {
2167a9beb1edSmglocker case UHD_TT_THINK_8:
2168a9beb1edSmglocker if (dev->ddesc.bDeviceProtocol != 0)
2169a9beb1edSmglocker return 666;
2170a9beb1edSmglocker else
2171a9beb1edSmglocker return 0;
2172a9beb1edSmglocker case UHD_TT_THINK_16:
2173a9beb1edSmglocker return 666 * 2;
2174a9beb1edSmglocker case UHD_TT_THINK_24:
2175a9beb1edSmglocker return 666 * 3;
2176a9beb1edSmglocker case UHD_TT_THINK_32:
2177a9beb1edSmglocker return 666 * 4;
2178a9beb1edSmglocker default:
2179a9beb1edSmglocker dev_dbg(hsotg->dev, "%s: Invalid TT Think Time (0x%04x)!\n",
2180a9beb1edSmglocker __func__, ttthink);
2181a9beb1edSmglocker break;
2182a9beb1edSmglocker }
2183a9beb1edSmglocker
2184a9beb1edSmglocker return 0;
2185578f812dSuebayasi }
2186