hcd_ddma.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347
  1. // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
  2. /*
  3. * hcd_ddma.c - DesignWare HS OTG Controller descriptor DMA routines
  4. *
  5. * Copyright (C) 2004-2013 Synopsys, Inc.
  6. */
  7. /*
  8. * This file contains the Descriptor DMA implementation for Host mode
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/io.h>
  16. #include <linux/slab.h>
  17. #include <linux/usb.h>
  18. #include <linux/usb/hcd.h>
  19. #include <linux/usb/ch11.h>
  20. #include "core.h"
  21. #include "hcd.h"
  22. static u16 dwc2_frame_list_idx(u16 frame)
  23. {
  24. return frame & (FRLISTEN_64_SIZE - 1);
  25. }
  26. static u16 dwc2_desclist_idx_inc(u16 idx, u16 inc, u8 speed)
  27. {
  28. return (idx + inc) &
  29. ((speed == USB_SPEED_HIGH ? MAX_DMA_DESC_NUM_HS_ISOC :
  30. MAX_DMA_DESC_NUM_GENERIC) - 1);
  31. }
  32. static u16 dwc2_desclist_idx_dec(u16 idx, u16 inc, u8 speed)
  33. {
  34. return (idx - inc) &
  35. ((speed == USB_SPEED_HIGH ? MAX_DMA_DESC_NUM_HS_ISOC :
  36. MAX_DMA_DESC_NUM_GENERIC) - 1);
  37. }
  38. static u16 dwc2_max_desc_num(struct dwc2_qh *qh)
  39. {
  40. return (qh->ep_type == USB_ENDPOINT_XFER_ISOC &&
  41. qh->dev_speed == USB_SPEED_HIGH) ?
  42. MAX_DMA_DESC_NUM_HS_ISOC : MAX_DMA_DESC_NUM_GENERIC;
  43. }
  44. static u16 dwc2_frame_incr_val(struct dwc2_qh *qh)
  45. {
  46. return qh->dev_speed == USB_SPEED_HIGH ?
  47. (qh->host_interval + 8 - 1) / 8 : qh->host_interval;
  48. }
  49. static int dwc2_desc_list_alloc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
  50. gfp_t flags)
  51. {
  52. struct kmem_cache *desc_cache;
  53. if (qh->ep_type == USB_ENDPOINT_XFER_ISOC &&
  54. qh->dev_speed == USB_SPEED_HIGH)
  55. desc_cache = hsotg->desc_hsisoc_cache;
  56. else
  57. desc_cache = hsotg->desc_gen_cache;
  58. qh->desc_list_sz = sizeof(struct dwc2_dma_desc) *
  59. dwc2_max_desc_num(qh);
  60. qh->desc_list = kmem_cache_zalloc(desc_cache, flags | GFP_DMA);
  61. if (!qh->desc_list)
  62. return -ENOMEM;
  63. qh->desc_list_dma = dma_map_single(hsotg->dev, qh->desc_list,
  64. qh->desc_list_sz,
  65. DMA_TO_DEVICE);
  66. qh->n_bytes = kcalloc(dwc2_max_desc_num(qh), sizeof(u32), flags);
  67. if (!qh->n_bytes) {
  68. dma_unmap_single(hsotg->dev, qh->desc_list_dma,
  69. qh->desc_list_sz,
  70. DMA_FROM_DEVICE);
  71. kmem_cache_free(desc_cache, qh->desc_list);
  72. qh->desc_list = NULL;
  73. return -ENOMEM;
  74. }
  75. return 0;
  76. }
  77. static void dwc2_desc_list_free(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  78. {
  79. struct kmem_cache *desc_cache;
  80. if (qh->ep_type == USB_ENDPOINT_XFER_ISOC &&
  81. qh->dev_speed == USB_SPEED_HIGH)
  82. desc_cache = hsotg->desc_hsisoc_cache;
  83. else
  84. desc_cache = hsotg->desc_gen_cache;
  85. if (qh->desc_list) {
  86. dma_unmap_single(hsotg->dev, qh->desc_list_dma,
  87. qh->desc_list_sz, DMA_FROM_DEVICE);
  88. kmem_cache_free(desc_cache, qh->desc_list);
  89. qh->desc_list = NULL;
  90. }
  91. kfree(qh->n_bytes);
  92. qh->n_bytes = NULL;
  93. }
  94. static int dwc2_frame_list_alloc(struct dwc2_hsotg *hsotg, gfp_t mem_flags)
  95. {
  96. if (hsotg->frame_list)
  97. return 0;
  98. hsotg->frame_list_sz = 4 * FRLISTEN_64_SIZE;
  99. hsotg->frame_list = kzalloc(hsotg->frame_list_sz, GFP_ATOMIC | GFP_DMA);
  100. if (!hsotg->frame_list)
  101. return -ENOMEM;
  102. hsotg->frame_list_dma = dma_map_single(hsotg->dev, hsotg->frame_list,
  103. hsotg->frame_list_sz,
  104. DMA_TO_DEVICE);
  105. return 0;
  106. }
  107. static void dwc2_frame_list_free(struct dwc2_hsotg *hsotg)
  108. {
  109. unsigned long flags;
  110. spin_lock_irqsave(&hsotg->lock, flags);
  111. if (!hsotg->frame_list) {
  112. spin_unlock_irqrestore(&hsotg->lock, flags);
  113. return;
  114. }
  115. dma_unmap_single(hsotg->dev, hsotg->frame_list_dma,
  116. hsotg->frame_list_sz, DMA_FROM_DEVICE);
  117. kfree(hsotg->frame_list);
  118. hsotg->frame_list = NULL;
  119. spin_unlock_irqrestore(&hsotg->lock, flags);
  120. }
  121. static void dwc2_per_sched_enable(struct dwc2_hsotg *hsotg, u32 fr_list_en)
  122. {
  123. u32 hcfg;
  124. unsigned long flags;
  125. spin_lock_irqsave(&hsotg->lock, flags);
  126. hcfg = dwc2_readl(hsotg, HCFG);
  127. if (hcfg & HCFG_PERSCHEDENA) {
  128. /* already enabled */
  129. spin_unlock_irqrestore(&hsotg->lock, flags);
  130. return;
  131. }
  132. dwc2_writel(hsotg, hsotg->frame_list_dma, HFLBADDR);
  133. hcfg &= ~HCFG_FRLISTEN_MASK;
  134. hcfg |= fr_list_en | HCFG_PERSCHEDENA;
  135. dev_vdbg(hsotg->dev, "Enabling Periodic schedule\n");
  136. dwc2_writel(hsotg, hcfg, HCFG);
  137. spin_unlock_irqrestore(&hsotg->lock, flags);
  138. }
  139. static void dwc2_per_sched_disable(struct dwc2_hsotg *hsotg)
  140. {
  141. u32 hcfg;
  142. unsigned long flags;
  143. spin_lock_irqsave(&hsotg->lock, flags);
  144. hcfg = dwc2_readl(hsotg, HCFG);
  145. if (!(hcfg & HCFG_PERSCHEDENA)) {
  146. /* already disabled */
  147. spin_unlock_irqrestore(&hsotg->lock, flags);
  148. return;
  149. }
  150. hcfg &= ~HCFG_PERSCHEDENA;
  151. dev_vdbg(hsotg->dev, "Disabling Periodic schedule\n");
  152. dwc2_writel(hsotg, hcfg, HCFG);
  153. spin_unlock_irqrestore(&hsotg->lock, flags);
  154. }
  155. /*
  156. * Activates/Deactivates FrameList entries for the channel based on endpoint
  157. * servicing period
  158. */
  159. static void dwc2_update_frame_list(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
  160. int enable)
  161. {
  162. struct dwc2_host_chan *chan;
  163. u16 i, j, inc;
  164. if (!hsotg) {
  165. pr_err("hsotg = %p\n", hsotg);
  166. return;
  167. }
  168. if (!qh->channel) {
  169. dev_err(hsotg->dev, "qh->channel = %p\n", qh->channel);
  170. return;
  171. }
  172. if (!hsotg->frame_list) {
  173. dev_err(hsotg->dev, "hsotg->frame_list = %p\n",
  174. hsotg->frame_list);
  175. return;
  176. }
  177. chan = qh->channel;
  178. inc = dwc2_frame_incr_val(qh);
  179. if (qh->ep_type == USB_ENDPOINT_XFER_ISOC)
  180. i = dwc2_frame_list_idx(qh->next_active_frame);
  181. else
  182. i = 0;
  183. j = i;
  184. do {
  185. if (enable)
  186. hsotg->frame_list[j] |= 1 << chan->hc_num;
  187. else
  188. hsotg->frame_list[j] &= ~(1 << chan->hc_num);
  189. j = (j + inc) & (FRLISTEN_64_SIZE - 1);
  190. } while (j != i);
  191. /*
  192. * Sync frame list since controller will access it if periodic
  193. * channel is currently enabled.
  194. */
  195. dma_sync_single_for_device(hsotg->dev,
  196. hsotg->frame_list_dma,
  197. hsotg->frame_list_sz,
  198. DMA_TO_DEVICE);
  199. if (!enable)
  200. return;
  201. chan->schinfo = 0;
  202. if (chan->speed == USB_SPEED_HIGH && qh->host_interval) {
  203. j = 1;
  204. /* TODO - check this */
  205. inc = (8 + qh->host_interval - 1) / qh->host_interval;
  206. for (i = 0; i < inc; i++) {
  207. chan->schinfo |= j;
  208. j = j << qh->host_interval;
  209. }
  210. } else {
  211. chan->schinfo = 0xff;
  212. }
  213. }
  214. static void dwc2_release_channel_ddma(struct dwc2_hsotg *hsotg,
  215. struct dwc2_qh *qh)
  216. {
  217. struct dwc2_host_chan *chan = qh->channel;
  218. if (dwc2_qh_is_non_per(qh)) {
  219. if (hsotg->params.uframe_sched)
  220. hsotg->available_host_channels++;
  221. else
  222. hsotg->non_periodic_channels--;
  223. } else {
  224. dwc2_update_frame_list(hsotg, qh, 0);
  225. hsotg->available_host_channels++;
  226. }
  227. /*
  228. * The condition is added to prevent double cleanup try in case of
  229. * device disconnect. See channel cleanup in dwc2_hcd_disconnect().
  230. */
  231. if (chan->qh) {
  232. if (!list_empty(&chan->hc_list_entry))
  233. list_del(&chan->hc_list_entry);
  234. dwc2_hc_cleanup(hsotg, chan);
  235. list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
  236. chan->qh = NULL;
  237. }
  238. qh->channel = NULL;
  239. qh->ntd = 0;
  240. if (qh->desc_list)
  241. memset(qh->desc_list, 0, sizeof(struct dwc2_dma_desc) *
  242. dwc2_max_desc_num(qh));
  243. }
  244. /**
  245. * dwc2_hcd_qh_init_ddma() - Initializes a QH structure's Descriptor DMA
  246. * related members
  247. *
  248. * @hsotg: The HCD state structure for the DWC OTG controller
  249. * @qh: The QH to init
  250. * @mem_flags: Indicates the type of memory allocation
  251. *
  252. * Return: 0 if successful, negative error code otherwise
  253. *
  254. * Allocates memory for the descriptor list. For the first periodic QH,
  255. * allocates memory for the FrameList and enables periodic scheduling.
  256. */
  257. int dwc2_hcd_qh_init_ddma(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
  258. gfp_t mem_flags)
  259. {
  260. int retval;
  261. if (qh->do_split) {
  262. dev_err(hsotg->dev,
  263. "SPLIT Transfers are not supported in Descriptor DMA mode.\n");
  264. retval = -EINVAL;
  265. goto err0;
  266. }
  267. retval = dwc2_desc_list_alloc(hsotg, qh, mem_flags);
  268. if (retval)
  269. goto err0;
  270. if (qh->ep_type == USB_ENDPOINT_XFER_ISOC ||
  271. qh->ep_type == USB_ENDPOINT_XFER_INT) {
  272. if (!hsotg->frame_list) {
  273. retval = dwc2_frame_list_alloc(hsotg, mem_flags);
  274. if (retval)
  275. goto err1;
  276. /* Enable periodic schedule on first periodic QH */
  277. dwc2_per_sched_enable(hsotg, HCFG_FRLISTEN_64);
  278. }
  279. }
  280. qh->ntd = 0;
  281. return 0;
  282. err1:
  283. dwc2_desc_list_free(hsotg, qh);
  284. err0:
  285. return retval;
  286. }
  287. /**
  288. * dwc2_hcd_qh_free_ddma() - Frees a QH structure's Descriptor DMA related
  289. * members
  290. *
  291. * @hsotg: The HCD state structure for the DWC OTG controller
  292. * @qh: The QH to free
  293. *
  294. * Frees descriptor list memory associated with the QH. If QH is periodic and
  295. * the last, frees FrameList memory and disables periodic scheduling.
  296. */
  297. void dwc2_hcd_qh_free_ddma(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  298. {
  299. unsigned long flags;
  300. dwc2_desc_list_free(hsotg, qh);
  301. /*
  302. * Channel still assigned due to some reasons.
  303. * Seen on Isoc URB dequeue. Channel halted but no subsequent
  304. * ChHalted interrupt to release the channel. Afterwards
  305. * when it comes here from endpoint disable routine
  306. * channel remains assigned.
  307. */
  308. spin_lock_irqsave(&hsotg->lock, flags);
  309. if (qh->channel)
  310. dwc2_release_channel_ddma(hsotg, qh);
  311. spin_unlock_irqrestore(&hsotg->lock, flags);
  312. if ((qh->ep_type == USB_ENDPOINT_XFER_ISOC ||
  313. qh->ep_type == USB_ENDPOINT_XFER_INT) &&
  314. (hsotg->params.uframe_sched ||
  315. !hsotg->periodic_channels) && hsotg->frame_list) {
  316. dwc2_per_sched_disable(hsotg);
  317. dwc2_frame_list_free(hsotg);
  318. }
  319. }
  320. static u8 dwc2_frame_to_desc_idx(struct dwc2_qh *qh, u16 frame_idx)
  321. {
  322. if (qh->dev_speed == USB_SPEED_HIGH)
  323. /* Descriptor set (8 descriptors) index which is 8-aligned */
  324. return (frame_idx & ((MAX_DMA_DESC_NUM_HS_ISOC / 8) - 1)) * 8;
  325. else
  326. return frame_idx & (MAX_DMA_DESC_NUM_GENERIC - 1);
  327. }
  328. /*
  329. * Determine starting frame for Isochronous transfer.
  330. * Few frames skipped to prevent race condition with HC.
  331. */
  332. static u16 dwc2_calc_starting_frame(struct dwc2_hsotg *hsotg,
  333. struct dwc2_qh *qh, u16 *skip_frames)
  334. {
  335. u16 frame;
  336. hsotg->frame_number = dwc2_hcd_get_frame_number(hsotg);
  337. /*
  338. * next_active_frame is always frame number (not uFrame) both in FS
  339. * and HS!
  340. */
  341. /*
  342. * skip_frames is used to limit activated descriptors number
  343. * to avoid the situation when HC services the last activated
  344. * descriptor firstly.
  345. * Example for FS:
  346. * Current frame is 1, scheduled frame is 3. Since HC always fetches
  347. * the descriptor corresponding to curr_frame+1, the descriptor
  348. * corresponding to frame 2 will be fetched. If the number of
  349. * descriptors is max=64 (or greather) the list will be fully programmed
  350. * with Active descriptors and it is possible case (rare) that the
  351. * latest descriptor(considering rollback) corresponding to frame 2 will
  352. * be serviced first. HS case is more probable because, in fact, up to
  353. * 11 uframes (16 in the code) may be skipped.
  354. */
  355. if (qh->dev_speed == USB_SPEED_HIGH) {
  356. /*
  357. * Consider uframe counter also, to start xfer asap. If half of
  358. * the frame elapsed skip 2 frames otherwise just 1 frame.
  359. * Starting descriptor index must be 8-aligned, so if the
  360. * current frame is near to complete the next one is skipped as
  361. * well.
  362. */
  363. if (dwc2_micro_frame_num(hsotg->frame_number) >= 5) {
  364. *skip_frames = 2 * 8;
  365. frame = dwc2_frame_num_inc(hsotg->frame_number,
  366. *skip_frames);
  367. } else {
  368. *skip_frames = 1 * 8;
  369. frame = dwc2_frame_num_inc(hsotg->frame_number,
  370. *skip_frames);
  371. }
  372. frame = dwc2_full_frame_num(frame);
  373. } else {
  374. /*
  375. * Two frames are skipped for FS - the current and the next.
  376. * But for descriptor programming, 1 frame (descriptor) is
  377. * enough, see example above.
  378. */
  379. *skip_frames = 1;
  380. frame = dwc2_frame_num_inc(hsotg->frame_number, 2);
  381. }
  382. return frame;
  383. }
  384. /*
  385. * Calculate initial descriptor index for isochronous transfer based on
  386. * scheduled frame
  387. */
  388. static u16 dwc2_recalc_initial_desc_idx(struct dwc2_hsotg *hsotg,
  389. struct dwc2_qh *qh)
  390. {
  391. u16 frame, fr_idx, fr_idx_tmp, skip_frames;
  392. /*
  393. * With current ISOC processing algorithm the channel is being released
  394. * when no more QTDs in the list (qh->ntd == 0). Thus this function is
  395. * called only when qh->ntd == 0 and qh->channel == 0.
  396. *
  397. * So qh->channel != NULL branch is not used and just not removed from
  398. * the source file. It is required for another possible approach which
  399. * is, do not disable and release the channel when ISOC session
  400. * completed, just move QH to inactive schedule until new QTD arrives.
  401. * On new QTD, the QH moved back to 'ready' schedule, starting frame and
  402. * therefore starting desc_index are recalculated. In this case channel
  403. * is released only on ep_disable.
  404. */
  405. /*
  406. * Calculate starting descriptor index. For INTERRUPT endpoint it is
  407. * always 0.
  408. */
  409. if (qh->channel) {
  410. frame = dwc2_calc_starting_frame(hsotg, qh, &skip_frames);
  411. /*
  412. * Calculate initial descriptor index based on FrameList current
  413. * bitmap and servicing period
  414. */
  415. fr_idx_tmp = dwc2_frame_list_idx(frame);
  416. fr_idx = (FRLISTEN_64_SIZE +
  417. dwc2_frame_list_idx(qh->next_active_frame) -
  418. fr_idx_tmp) % dwc2_frame_incr_val(qh);
  419. fr_idx = (fr_idx + fr_idx_tmp) % FRLISTEN_64_SIZE;
  420. } else {
  421. qh->next_active_frame = dwc2_calc_starting_frame(hsotg, qh,
  422. &skip_frames);
  423. fr_idx = dwc2_frame_list_idx(qh->next_active_frame);
  424. }
  425. qh->td_first = qh->td_last = dwc2_frame_to_desc_idx(qh, fr_idx);
  426. return skip_frames;
  427. }
  428. #define ISOC_URB_GIVEBACK_ASAP
  429. #define MAX_ISOC_XFER_SIZE_FS 1023
  430. #define MAX_ISOC_XFER_SIZE_HS 3072
  431. #define DESCNUM_THRESHOLD 4
  432. static void dwc2_fill_host_isoc_dma_desc(struct dwc2_hsotg *hsotg,
  433. struct dwc2_qtd *qtd,
  434. struct dwc2_qh *qh, u32 max_xfer_size,
  435. u16 idx)
  436. {
  437. struct dwc2_dma_desc *dma_desc = &qh->desc_list[idx];
  438. struct dwc2_hcd_iso_packet_desc *frame_desc;
  439. memset(dma_desc, 0, sizeof(*dma_desc));
  440. frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
  441. if (frame_desc->length > max_xfer_size)
  442. qh->n_bytes[idx] = max_xfer_size;
  443. else
  444. qh->n_bytes[idx] = frame_desc->length;
  445. dma_desc->buf = (u32)(qtd->urb->dma + frame_desc->offset);
  446. dma_desc->status = qh->n_bytes[idx] << HOST_DMA_ISOC_NBYTES_SHIFT &
  447. HOST_DMA_ISOC_NBYTES_MASK;
  448. /* Set active bit */
  449. dma_desc->status |= HOST_DMA_A;
  450. qh->ntd++;
  451. qtd->isoc_frame_index_last++;
  452. #ifdef ISOC_URB_GIVEBACK_ASAP
  453. /* Set IOC for each descriptor corresponding to last frame of URB */
  454. if (qtd->isoc_frame_index_last == qtd->urb->packet_count)
  455. dma_desc->status |= HOST_DMA_IOC;
  456. #endif
  457. dma_sync_single_for_device(hsotg->dev,
  458. qh->desc_list_dma +
  459. (idx * sizeof(struct dwc2_dma_desc)),
  460. sizeof(struct dwc2_dma_desc),
  461. DMA_TO_DEVICE);
  462. }
  463. static void dwc2_init_isoc_dma_desc(struct dwc2_hsotg *hsotg,
  464. struct dwc2_qh *qh, u16 skip_frames)
  465. {
  466. struct dwc2_qtd *qtd;
  467. u32 max_xfer_size;
  468. u16 idx, inc, n_desc = 0, ntd_max = 0;
  469. u16 cur_idx;
  470. u16 next_idx;
  471. idx = qh->td_last;
  472. inc = qh->host_interval;
  473. hsotg->frame_number = dwc2_hcd_get_frame_number(hsotg);
  474. cur_idx = dwc2_frame_list_idx(hsotg->frame_number);
  475. next_idx = dwc2_desclist_idx_inc(qh->td_last, inc, qh->dev_speed);
  476. /*
  477. * Ensure current frame number didn't overstep last scheduled
  478. * descriptor. If it happens, the only way to recover is to move
  479. * qh->td_last to current frame number + 1.
  480. * So that next isoc descriptor will be scheduled on frame number + 1
  481. * and not on a past frame.
  482. */
  483. if (dwc2_frame_idx_num_gt(cur_idx, next_idx) || (cur_idx == next_idx)) {
  484. if (inc < 32) {
  485. dev_vdbg(hsotg->dev,
  486. "current frame number overstep last descriptor\n");
  487. qh->td_last = dwc2_desclist_idx_inc(cur_idx, inc,
  488. qh->dev_speed);
  489. idx = qh->td_last;
  490. }
  491. }
  492. if (qh->host_interval) {
  493. ntd_max = (dwc2_max_desc_num(qh) + qh->host_interval - 1) /
  494. qh->host_interval;
  495. if (skip_frames && !qh->channel)
  496. ntd_max -= skip_frames / qh->host_interval;
  497. }
  498. max_xfer_size = qh->dev_speed == USB_SPEED_HIGH ?
  499. MAX_ISOC_XFER_SIZE_HS : MAX_ISOC_XFER_SIZE_FS;
  500. list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry) {
  501. if (qtd->in_process &&
  502. qtd->isoc_frame_index_last ==
  503. qtd->urb->packet_count)
  504. continue;
  505. qtd->isoc_td_first = idx;
  506. while (qh->ntd < ntd_max && qtd->isoc_frame_index_last <
  507. qtd->urb->packet_count) {
  508. dwc2_fill_host_isoc_dma_desc(hsotg, qtd, qh,
  509. max_xfer_size, idx);
  510. idx = dwc2_desclist_idx_inc(idx, inc, qh->dev_speed);
  511. n_desc++;
  512. }
  513. qtd->isoc_td_last = idx;
  514. qtd->in_process = 1;
  515. }
  516. qh->td_last = idx;
  517. #ifdef ISOC_URB_GIVEBACK_ASAP
  518. /* Set IOC for last descriptor if descriptor list is full */
  519. if (qh->ntd == ntd_max) {
  520. idx = dwc2_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
  521. qh->desc_list[idx].status |= HOST_DMA_IOC;
  522. dma_sync_single_for_device(hsotg->dev,
  523. qh->desc_list_dma + (idx *
  524. sizeof(struct dwc2_dma_desc)),
  525. sizeof(struct dwc2_dma_desc),
  526. DMA_TO_DEVICE);
  527. }
  528. #else
  529. /*
  530. * Set IOC bit only for one descriptor. Always try to be ahead of HW
  531. * processing, i.e. on IOC generation driver activates next descriptor
  532. * but core continues to process descriptors following the one with IOC
  533. * set.
  534. */
  535. if (n_desc > DESCNUM_THRESHOLD)
  536. /*
  537. * Move IOC "up". Required even if there is only one QTD
  538. * in the list, because QTDs might continue to be queued,
  539. * but during the activation it was only one queued.
  540. * Actually more than one QTD might be in the list if this
  541. * function called from XferCompletion - QTDs was queued during
  542. * HW processing of the previous descriptor chunk.
  543. */
  544. idx = dwc2_desclist_idx_dec(idx, inc * ((qh->ntd + 1) / 2),
  545. qh->dev_speed);
  546. else
  547. /*
  548. * Set the IOC for the latest descriptor if either number of
  549. * descriptors is not greater than threshold or no more new
  550. * descriptors activated
  551. */
  552. idx = dwc2_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
  553. qh->desc_list[idx].status |= HOST_DMA_IOC;
  554. dma_sync_single_for_device(hsotg->dev,
  555. qh->desc_list_dma +
  556. (idx * sizeof(struct dwc2_dma_desc)),
  557. sizeof(struct dwc2_dma_desc),
  558. DMA_TO_DEVICE);
  559. #endif
  560. }
  561. static void dwc2_fill_host_dma_desc(struct dwc2_hsotg *hsotg,
  562. struct dwc2_host_chan *chan,
  563. struct dwc2_qtd *qtd, struct dwc2_qh *qh,
  564. int n_desc)
  565. {
  566. struct dwc2_dma_desc *dma_desc = &qh->desc_list[n_desc];
  567. int len = chan->xfer_len;
  568. if (len > HOST_DMA_NBYTES_LIMIT - (chan->max_packet - 1))
  569. len = HOST_DMA_NBYTES_LIMIT - (chan->max_packet - 1);
  570. if (chan->ep_is_in) {
  571. int num_packets;
  572. if (len > 0 && chan->max_packet)
  573. num_packets = (len + chan->max_packet - 1)
  574. / chan->max_packet;
  575. else
  576. /* Need 1 packet for transfer length of 0 */
  577. num_packets = 1;
  578. /* Always program an integral # of packets for IN transfers */
  579. len = num_packets * chan->max_packet;
  580. }
  581. dma_desc->status = len << HOST_DMA_NBYTES_SHIFT & HOST_DMA_NBYTES_MASK;
  582. qh->n_bytes[n_desc] = len;
  583. if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL &&
  584. qtd->control_phase == DWC2_CONTROL_SETUP)
  585. dma_desc->status |= HOST_DMA_SUP;
  586. dma_desc->buf = (u32)chan->xfer_dma;
  587. dma_sync_single_for_device(hsotg->dev,
  588. qh->desc_list_dma +
  589. (n_desc * sizeof(struct dwc2_dma_desc)),
  590. sizeof(struct dwc2_dma_desc),
  591. DMA_TO_DEVICE);
  592. /*
  593. * Last (or only) descriptor of IN transfer with actual size less
  594. * than MaxPacket
  595. */
  596. if (len > chan->xfer_len) {
  597. chan->xfer_len = 0;
  598. } else {
  599. chan->xfer_dma += len;
  600. chan->xfer_len -= len;
  601. }
  602. }
  603. static void dwc2_init_non_isoc_dma_desc(struct dwc2_hsotg *hsotg,
  604. struct dwc2_qh *qh)
  605. {
  606. struct dwc2_qtd *qtd;
  607. struct dwc2_host_chan *chan = qh->channel;
  608. int n_desc = 0;
  609. dev_vdbg(hsotg->dev, "%s(): qh=%p dma=%08lx len=%d\n", __func__, qh,
  610. (unsigned long)chan->xfer_dma, chan->xfer_len);
  611. /*
  612. * Start with chan->xfer_dma initialized in assign_and_init_hc(), then
  613. * if SG transfer consists of multiple URBs, this pointer is re-assigned
  614. * to the buffer of the currently processed QTD. For non-SG request
  615. * there is always one QTD active.
  616. */
  617. list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry) {
  618. dev_vdbg(hsotg->dev, "qtd=%p\n", qtd);
  619. if (n_desc) {
  620. /* SG request - more than 1 QTD */
  621. chan->xfer_dma = qtd->urb->dma +
  622. qtd->urb->actual_length;
  623. chan->xfer_len = qtd->urb->length -
  624. qtd->urb->actual_length;
  625. dev_vdbg(hsotg->dev, "buf=%08lx len=%d\n",
  626. (unsigned long)chan->xfer_dma, chan->xfer_len);
  627. }
  628. qtd->n_desc = 0;
  629. do {
  630. if (n_desc > 1) {
  631. qh->desc_list[n_desc - 1].status |= HOST_DMA_A;
  632. dev_vdbg(hsotg->dev,
  633. "set A bit in desc %d (%p)\n",
  634. n_desc - 1,
  635. &qh->desc_list[n_desc - 1]);
  636. dma_sync_single_for_device(hsotg->dev,
  637. qh->desc_list_dma +
  638. ((n_desc - 1) *
  639. sizeof(struct dwc2_dma_desc)),
  640. sizeof(struct dwc2_dma_desc),
  641. DMA_TO_DEVICE);
  642. }
  643. dwc2_fill_host_dma_desc(hsotg, chan, qtd, qh, n_desc);
  644. dev_vdbg(hsotg->dev,
  645. "desc %d (%p) buf=%08x status=%08x\n",
  646. n_desc, &qh->desc_list[n_desc],
  647. qh->desc_list[n_desc].buf,
  648. qh->desc_list[n_desc].status);
  649. qtd->n_desc++;
  650. n_desc++;
  651. } while (chan->xfer_len > 0 &&
  652. n_desc != MAX_DMA_DESC_NUM_GENERIC);
  653. dev_vdbg(hsotg->dev, "n_desc=%d\n", n_desc);
  654. qtd->in_process = 1;
  655. if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL)
  656. break;
  657. if (n_desc == MAX_DMA_DESC_NUM_GENERIC)
  658. break;
  659. }
  660. if (n_desc) {
  661. qh->desc_list[n_desc - 1].status |=
  662. HOST_DMA_IOC | HOST_DMA_EOL | HOST_DMA_A;
  663. dev_vdbg(hsotg->dev, "set IOC/EOL/A bits in desc %d (%p)\n",
  664. n_desc - 1, &qh->desc_list[n_desc - 1]);
  665. dma_sync_single_for_device(hsotg->dev,
  666. qh->desc_list_dma + (n_desc - 1) *
  667. sizeof(struct dwc2_dma_desc),
  668. sizeof(struct dwc2_dma_desc),
  669. DMA_TO_DEVICE);
  670. if (n_desc > 1) {
  671. qh->desc_list[0].status |= HOST_DMA_A;
  672. dev_vdbg(hsotg->dev, "set A bit in desc 0 (%p)\n",
  673. &qh->desc_list[0]);
  674. dma_sync_single_for_device(hsotg->dev,
  675. qh->desc_list_dma,
  676. sizeof(struct dwc2_dma_desc),
  677. DMA_TO_DEVICE);
  678. }
  679. chan->ntd = n_desc;
  680. }
  681. }
  682. /**
  683. * dwc2_hcd_start_xfer_ddma() - Starts a transfer in Descriptor DMA mode
  684. *
  685. * @hsotg: The HCD state structure for the DWC OTG controller
  686. * @qh: The QH to init
  687. *
  688. * Return: 0 if successful, negative error code otherwise
  689. *
  690. * For Control and Bulk endpoints, initializes descriptor list and starts the
  691. * transfer. For Interrupt and Isochronous endpoints, initializes descriptor
  692. * list then updates FrameList, marking appropriate entries as active.
  693. *
  694. * For Isochronous endpoints the starting descriptor index is calculated based
  695. * on the scheduled frame, but only on the first transfer descriptor within a
  696. * session. Then the transfer is started via enabling the channel.
  697. *
  698. * For Isochronous endpoints the channel is not halted on XferComplete
  699. * interrupt so remains assigned to the endpoint(QH) until session is done.
  700. */
  701. void dwc2_hcd_start_xfer_ddma(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  702. {
  703. /* Channel is already assigned */
  704. struct dwc2_host_chan *chan = qh->channel;
  705. u16 skip_frames = 0;
  706. switch (chan->ep_type) {
  707. case USB_ENDPOINT_XFER_CONTROL:
  708. case USB_ENDPOINT_XFER_BULK:
  709. dwc2_init_non_isoc_dma_desc(hsotg, qh);
  710. dwc2_hc_start_transfer_ddma(hsotg, chan);
  711. break;
  712. case USB_ENDPOINT_XFER_INT:
  713. dwc2_init_non_isoc_dma_desc(hsotg, qh);
  714. dwc2_update_frame_list(hsotg, qh, 1);
  715. dwc2_hc_start_transfer_ddma(hsotg, chan);
  716. break;
  717. case USB_ENDPOINT_XFER_ISOC:
  718. if (!qh->ntd)
  719. skip_frames = dwc2_recalc_initial_desc_idx(hsotg, qh);
  720. dwc2_init_isoc_dma_desc(hsotg, qh, skip_frames);
  721. if (!chan->xfer_started) {
  722. dwc2_update_frame_list(hsotg, qh, 1);
  723. /*
  724. * Always set to max, instead of actual size. Otherwise
  725. * ntd will be changed with channel being enabled. Not
  726. * recommended.
  727. */
  728. chan->ntd = dwc2_max_desc_num(qh);
  729. /* Enable channel only once for ISOC */
  730. dwc2_hc_start_transfer_ddma(hsotg, chan);
  731. }
  732. break;
  733. default:
  734. break;
  735. }
  736. }
  737. #define DWC2_CMPL_DONE 1
  738. #define DWC2_CMPL_STOP 2
  739. static int dwc2_cmpl_host_isoc_dma_desc(struct dwc2_hsotg *hsotg,
  740. struct dwc2_host_chan *chan,
  741. struct dwc2_qtd *qtd,
  742. struct dwc2_qh *qh, u16 idx)
  743. {
  744. struct dwc2_dma_desc *dma_desc;
  745. struct dwc2_hcd_iso_packet_desc *frame_desc;
  746. u16 remain = 0;
  747. int rc = 0;
  748. if (!qtd->urb)
  749. return -EINVAL;
  750. dma_sync_single_for_cpu(hsotg->dev, qh->desc_list_dma + (idx *
  751. sizeof(struct dwc2_dma_desc)),
  752. sizeof(struct dwc2_dma_desc),
  753. DMA_FROM_DEVICE);
  754. dma_desc = &qh->desc_list[idx];
  755. frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
  756. dma_desc->buf = (u32)(qtd->urb->dma + frame_desc->offset);
  757. if (chan->ep_is_in)
  758. remain = (dma_desc->status & HOST_DMA_ISOC_NBYTES_MASK) >>
  759. HOST_DMA_ISOC_NBYTES_SHIFT;
  760. if ((dma_desc->status & HOST_DMA_STS_MASK) == HOST_DMA_STS_PKTERR) {
  761. /*
  762. * XactError, or unable to complete all the transactions
  763. * in the scheduled micro-frame/frame, both indicated by
  764. * HOST_DMA_STS_PKTERR
  765. */
  766. qtd->urb->error_count++;
  767. frame_desc->actual_length = qh->n_bytes[idx] - remain;
  768. frame_desc->status = -EPROTO;
  769. } else {
  770. /* Success */
  771. frame_desc->actual_length = qh->n_bytes[idx] - remain;
  772. frame_desc->status = 0;
  773. }
  774. if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
  775. /*
  776. * urb->status is not used for isoc transfers here. The
  777. * individual frame_desc status are used instead.
  778. */
  779. dwc2_host_complete(hsotg, qtd, 0);
  780. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
  781. /*
  782. * This check is necessary because urb_dequeue can be called
  783. * from urb complete callback (sound driver for example). All
  784. * pending URBs are dequeued there, so no need for further
  785. * processing.
  786. */
  787. if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE)
  788. return -1;
  789. rc = DWC2_CMPL_DONE;
  790. }
  791. qh->ntd--;
  792. /* Stop if IOC requested descriptor reached */
  793. if (dma_desc->status & HOST_DMA_IOC)
  794. rc = DWC2_CMPL_STOP;
  795. return rc;
  796. }
  797. static void dwc2_complete_isoc_xfer_ddma(struct dwc2_hsotg *hsotg,
  798. struct dwc2_host_chan *chan,
  799. enum dwc2_halt_status halt_status)
  800. {
  801. struct dwc2_hcd_iso_packet_desc *frame_desc;
  802. struct dwc2_qtd *qtd, *qtd_tmp;
  803. struct dwc2_qh *qh;
  804. u16 idx;
  805. int rc;
  806. qh = chan->qh;
  807. idx = qh->td_first;
  808. if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE) {
  809. list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry)
  810. qtd->in_process = 0;
  811. return;
  812. }
  813. if (halt_status == DWC2_HC_XFER_AHB_ERR ||
  814. halt_status == DWC2_HC_XFER_BABBLE_ERR) {
  815. /*
  816. * Channel is halted in these error cases, considered as serious
  817. * issues.
  818. * Complete all URBs marking all frames as failed, irrespective
  819. * whether some of the descriptors (frames) succeeded or not.
  820. * Pass error code to completion routine as well, to update
  821. * urb->status, some of class drivers might use it to stop
  822. * queing transfer requests.
  823. */
  824. int err = halt_status == DWC2_HC_XFER_AHB_ERR ?
  825. -EIO : -EOVERFLOW;
  826. list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
  827. qtd_list_entry) {
  828. if (qtd->urb) {
  829. for (idx = 0; idx < qtd->urb->packet_count;
  830. idx++) {
  831. frame_desc = &qtd->urb->iso_descs[idx];
  832. frame_desc->status = err;
  833. }
  834. dwc2_host_complete(hsotg, qtd, err);
  835. }
  836. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
  837. }
  838. return;
  839. }
  840. list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
  841. if (!qtd->in_process)
  842. break;
  843. /*
  844. * Ensure idx corresponds to descriptor where first urb of this
  845. * qtd was added. In fact, during isoc desc init, dwc2 may skip
  846. * an index if current frame number is already over this index.
  847. */
  848. if (idx != qtd->isoc_td_first) {
  849. dev_vdbg(hsotg->dev,
  850. "try to complete %d instead of %d\n",
  851. idx, qtd->isoc_td_first);
  852. idx = qtd->isoc_td_first;
  853. }
  854. do {
  855. struct dwc2_qtd *qtd_next;
  856. u16 cur_idx;
  857. rc = dwc2_cmpl_host_isoc_dma_desc(hsotg, chan, qtd, qh,
  858. idx);
  859. if (rc < 0)
  860. return;
  861. idx = dwc2_desclist_idx_inc(idx, qh->host_interval,
  862. chan->speed);
  863. if (!rc)
  864. continue;
  865. if (rc == DWC2_CMPL_DONE)
  866. break;
  867. /* rc == DWC2_CMPL_STOP */
  868. if (qh->host_interval >= 32)
  869. goto stop_scan;
  870. qh->td_first = idx;
  871. cur_idx = dwc2_frame_list_idx(hsotg->frame_number);
  872. qtd_next = list_first_entry(&qh->qtd_list,
  873. struct dwc2_qtd,
  874. qtd_list_entry);
  875. if (dwc2_frame_idx_num_gt(cur_idx,
  876. qtd_next->isoc_td_last))
  877. break;
  878. goto stop_scan;
  879. } while (idx != qh->td_first);
  880. }
  881. stop_scan:
  882. qh->td_first = idx;
  883. }
  884. static int dwc2_update_non_isoc_urb_state_ddma(struct dwc2_hsotg *hsotg,
  885. struct dwc2_host_chan *chan,
  886. struct dwc2_qtd *qtd,
  887. struct dwc2_dma_desc *dma_desc,
  888. enum dwc2_halt_status halt_status,
  889. u32 n_bytes, int *xfer_done)
  890. {
  891. struct dwc2_hcd_urb *urb = qtd->urb;
  892. u16 remain = 0;
  893. if (chan->ep_is_in)
  894. remain = (dma_desc->status & HOST_DMA_NBYTES_MASK) >>
  895. HOST_DMA_NBYTES_SHIFT;
  896. dev_vdbg(hsotg->dev, "remain=%d dwc2_urb=%p\n", remain, urb);
  897. if (halt_status == DWC2_HC_XFER_AHB_ERR) {
  898. dev_err(hsotg->dev, "EIO\n");
  899. urb->status = -EIO;
  900. return 1;
  901. }
  902. if ((dma_desc->status & HOST_DMA_STS_MASK) == HOST_DMA_STS_PKTERR) {
  903. switch (halt_status) {
  904. case DWC2_HC_XFER_STALL:
  905. dev_vdbg(hsotg->dev, "Stall\n");
  906. urb->status = -EPIPE;
  907. break;
  908. case DWC2_HC_XFER_BABBLE_ERR:
  909. dev_err(hsotg->dev, "Babble\n");
  910. urb->status = -EOVERFLOW;
  911. break;
  912. case DWC2_HC_XFER_XACT_ERR:
  913. dev_err(hsotg->dev, "XactErr\n");
  914. urb->status = -EPROTO;
  915. break;
  916. default:
  917. dev_err(hsotg->dev,
  918. "%s: Unhandled descriptor error status (%d)\n",
  919. __func__, halt_status);
  920. break;
  921. }
  922. return 1;
  923. }
  924. if (dma_desc->status & HOST_DMA_A) {
  925. dev_vdbg(hsotg->dev,
  926. "Active descriptor encountered on channel %d\n",
  927. chan->hc_num);
  928. return 0;
  929. }
  930. if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL) {
  931. if (qtd->control_phase == DWC2_CONTROL_DATA) {
  932. urb->actual_length += n_bytes - remain;
  933. if (remain || urb->actual_length >= urb->length) {
  934. /*
  935. * For Control Data stage do not set urb->status
  936. * to 0, to prevent URB callback. Set it when
  937. * Status phase is done. See below.
  938. */
  939. *xfer_done = 1;
  940. }
  941. } else if (qtd->control_phase == DWC2_CONTROL_STATUS) {
  942. urb->status = 0;
  943. *xfer_done = 1;
  944. }
  945. /* No handling for SETUP stage */
  946. } else {
  947. /* BULK and INTR */
  948. urb->actual_length += n_bytes - remain;
  949. dev_vdbg(hsotg->dev, "length=%d actual=%d\n", urb->length,
  950. urb->actual_length);
  951. if (remain || urb->actual_length >= urb->length) {
  952. urb->status = 0;
  953. *xfer_done = 1;
  954. }
  955. }
  956. return 0;
  957. }
  958. static int dwc2_process_non_isoc_desc(struct dwc2_hsotg *hsotg,
  959. struct dwc2_host_chan *chan,
  960. int chnum, struct dwc2_qtd *qtd,
  961. int desc_num,
  962. enum dwc2_halt_status halt_status,
  963. int *xfer_done)
  964. {
  965. struct dwc2_qh *qh = chan->qh;
  966. struct dwc2_hcd_urb *urb = qtd->urb;
  967. struct dwc2_dma_desc *dma_desc;
  968. u32 n_bytes;
  969. int failed;
  970. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  971. if (!urb)
  972. return -EINVAL;
  973. dma_sync_single_for_cpu(hsotg->dev,
  974. qh->desc_list_dma + (desc_num *
  975. sizeof(struct dwc2_dma_desc)),
  976. sizeof(struct dwc2_dma_desc),
  977. DMA_FROM_DEVICE);
  978. dma_desc = &qh->desc_list[desc_num];
  979. n_bytes = qh->n_bytes[desc_num];
  980. dev_vdbg(hsotg->dev,
  981. "qtd=%p dwc2_urb=%p desc_num=%d desc=%p n_bytes=%d\n",
  982. qtd, urb, desc_num, dma_desc, n_bytes);
  983. failed = dwc2_update_non_isoc_urb_state_ddma(hsotg, chan, qtd, dma_desc,
  984. halt_status, n_bytes,
  985. xfer_done);
  986. if (failed || (*xfer_done && urb->status != -EINPROGRESS)) {
  987. dwc2_host_complete(hsotg, qtd, urb->status);
  988. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
  989. dev_vdbg(hsotg->dev, "failed=%1x xfer_done=%1x\n",
  990. failed, *xfer_done);
  991. return failed;
  992. }
  993. if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL) {
  994. switch (qtd->control_phase) {
  995. case DWC2_CONTROL_SETUP:
  996. if (urb->length > 0)
  997. qtd->control_phase = DWC2_CONTROL_DATA;
  998. else
  999. qtd->control_phase = DWC2_CONTROL_STATUS;
  1000. dev_vdbg(hsotg->dev,
  1001. " Control setup transaction done\n");
  1002. break;
  1003. case DWC2_CONTROL_DATA:
  1004. if (*xfer_done) {
  1005. qtd->control_phase = DWC2_CONTROL_STATUS;
  1006. dev_vdbg(hsotg->dev,
  1007. " Control data transfer done\n");
  1008. } else if (desc_num + 1 == qtd->n_desc) {
  1009. /*
  1010. * Last descriptor for Control data stage which
  1011. * is not completed yet
  1012. */
  1013. dwc2_hcd_save_data_toggle(hsotg, chan, chnum,
  1014. qtd);
  1015. }
  1016. break;
  1017. default:
  1018. break;
  1019. }
  1020. }
  1021. return 0;
  1022. }
  1023. static void dwc2_complete_non_isoc_xfer_ddma(struct dwc2_hsotg *hsotg,
  1024. struct dwc2_host_chan *chan,
  1025. int chnum,
  1026. enum dwc2_halt_status halt_status)
  1027. {
  1028. struct list_head *qtd_item, *qtd_tmp;
  1029. struct dwc2_qh *qh = chan->qh;
  1030. struct dwc2_qtd *qtd = NULL;
  1031. int xfer_done;
  1032. int desc_num = 0;
  1033. if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE) {
  1034. list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry)
  1035. qtd->in_process = 0;
  1036. return;
  1037. }
  1038. list_for_each_safe(qtd_item, qtd_tmp, &qh->qtd_list) {
  1039. int i;
  1040. int qtd_desc_count;
  1041. qtd = list_entry(qtd_item, struct dwc2_qtd, qtd_list_entry);
  1042. xfer_done = 0;
  1043. qtd_desc_count = qtd->n_desc;
  1044. for (i = 0; i < qtd_desc_count; i++) {
  1045. if (dwc2_process_non_isoc_desc(hsotg, chan, chnum, qtd,
  1046. desc_num, halt_status,
  1047. &xfer_done)) {
  1048. qtd = NULL;
  1049. goto stop_scan;
  1050. }
  1051. desc_num++;
  1052. }
  1053. }
  1054. stop_scan:
  1055. if (qh->ep_type != USB_ENDPOINT_XFER_CONTROL) {
  1056. /*
  1057. * Resetting the data toggle for bulk and interrupt endpoints
  1058. * in case of stall. See handle_hc_stall_intr().
  1059. */
  1060. if (halt_status == DWC2_HC_XFER_STALL)
  1061. qh->data_toggle = DWC2_HC_PID_DATA0;
  1062. else
  1063. dwc2_hcd_save_data_toggle(hsotg, chan, chnum, NULL);
  1064. }
  1065. if (halt_status == DWC2_HC_XFER_COMPLETE) {
  1066. if (chan->hcint & HCINTMSK_NYET) {
  1067. /*
  1068. * Got a NYET on the last transaction of the transfer.
  1069. * It means that the endpoint should be in the PING
  1070. * state at the beginning of the next transfer.
  1071. */
  1072. qh->ping_state = 1;
  1073. }
  1074. }
  1075. }
  1076. /**
  1077. * dwc2_hcd_complete_xfer_ddma() - Scans the descriptor list, updates URB's
  1078. * status and calls completion routine for the URB if it's done. Called from
  1079. * interrupt handlers.
  1080. *
  1081. * @hsotg: The HCD state structure for the DWC OTG controller
  1082. * @chan: Host channel the transfer is completed on
  1083. * @chnum: Index of Host channel registers
  1084. * @halt_status: Reason the channel is being halted or just XferComplete
  1085. * for isochronous transfers
  1086. *
  1087. * Releases the channel to be used by other transfers.
  1088. * In case of Isochronous endpoint the channel is not halted until the end of
  1089. * the session, i.e. QTD list is empty.
  1090. * If periodic channel released the FrameList is updated accordingly.
  1091. * Calls transaction selection routines to activate pending transfers.
  1092. */
  1093. void dwc2_hcd_complete_xfer_ddma(struct dwc2_hsotg *hsotg,
  1094. struct dwc2_host_chan *chan, int chnum,
  1095. enum dwc2_halt_status halt_status)
  1096. {
  1097. struct dwc2_qh *qh = chan->qh;
  1098. int continue_isoc_xfer = 0;
  1099. enum dwc2_transaction_type tr_type;
  1100. if (chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
  1101. dwc2_complete_isoc_xfer_ddma(hsotg, chan, halt_status);
  1102. /* Release the channel if halted or session completed */
  1103. if (halt_status != DWC2_HC_XFER_COMPLETE ||
  1104. list_empty(&qh->qtd_list)) {
  1105. struct dwc2_qtd *qtd, *qtd_tmp;
  1106. /*
  1107. * Kill all remainings QTDs since channel has been
  1108. * halted.
  1109. */
  1110. list_for_each_entry_safe(qtd, qtd_tmp,
  1111. &qh->qtd_list,
  1112. qtd_list_entry) {
  1113. dwc2_host_complete(hsotg, qtd,
  1114. -ECONNRESET);
  1115. dwc2_hcd_qtd_unlink_and_free(hsotg,
  1116. qtd, qh);
  1117. }
  1118. /* Halt the channel if session completed */
  1119. if (halt_status == DWC2_HC_XFER_COMPLETE)
  1120. dwc2_hc_halt(hsotg, chan, halt_status);
  1121. dwc2_release_channel_ddma(hsotg, qh);
  1122. dwc2_hcd_qh_unlink(hsotg, qh);
  1123. } else {
  1124. /* Keep in assigned schedule to continue transfer */
  1125. list_move_tail(&qh->qh_list_entry,
  1126. &hsotg->periodic_sched_assigned);
  1127. /*
  1128. * If channel has been halted during giveback of urb
  1129. * then prevent any new scheduling.
  1130. */
  1131. if (!chan->halt_status)
  1132. continue_isoc_xfer = 1;
  1133. }
  1134. /*
  1135. * Todo: Consider the case when period exceeds FrameList size.
  1136. * Frame Rollover interrupt should be used.
  1137. */
  1138. } else {
  1139. /*
  1140. * Scan descriptor list to complete the URB(s), then release
  1141. * the channel
  1142. */
  1143. dwc2_complete_non_isoc_xfer_ddma(hsotg, chan, chnum,
  1144. halt_status);
  1145. dwc2_release_channel_ddma(hsotg, qh);
  1146. dwc2_hcd_qh_unlink(hsotg, qh);
  1147. if (!list_empty(&qh->qtd_list)) {
  1148. /*
  1149. * Add back to inactive non-periodic schedule on normal
  1150. * completion
  1151. */
  1152. dwc2_hcd_qh_add(hsotg, qh);
  1153. }
  1154. }
  1155. tr_type = dwc2_hcd_select_transactions(hsotg);
  1156. if (tr_type != DWC2_TRANSACTION_NONE || continue_isoc_xfer) {
  1157. if (continue_isoc_xfer) {
  1158. if (tr_type == DWC2_TRANSACTION_NONE)
  1159. tr_type = DWC2_TRANSACTION_PERIODIC;
  1160. else if (tr_type == DWC2_TRANSACTION_NON_PERIODIC)
  1161. tr_type = DWC2_TRANSACTION_ALL;
  1162. }
  1163. dwc2_hcd_queue_transactions(hsotg, tr_type);
  1164. }
  1165. }