hcd_queue.c 62 KB

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