hcd_queue.c 64 KB

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