Linux Audio

Check our new training course

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