hcd_ddma.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  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 = idx;
  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 frame_desc_idx;
  747. struct urb *usb_urb;
  748. u16 remain = 0;
  749. int rc = 0;
  750. if (!qtd->urb)
  751. return -EINVAL;
  752. usb_urb = qtd->urb->priv;
  753. dma_sync_single_for_cpu(hsotg->dev, qh->desc_list_dma + (idx *
  754. sizeof(struct dwc2_dma_desc)),
  755. sizeof(struct dwc2_dma_desc),
  756. DMA_FROM_DEVICE);
  757. dma_desc = &qh->desc_list[idx];
  758. frame_desc_idx = (idx - qtd->isoc_td_first) & (usb_urb->number_of_packets - 1);
  759. frame_desc = &qtd->urb->iso_descs[frame_desc_idx];
  760. if (idx == qtd->isoc_td_first)
  761. usb_urb->start_frame = dwc2_hcd_get_frame_number(hsotg);
  762. dma_desc->buf = (u32)(qtd->urb->dma + frame_desc->offset);
  763. if (chan->ep_is_in)
  764. remain = (dma_desc->status & HOST_DMA_ISOC_NBYTES_MASK) >>
  765. HOST_DMA_ISOC_NBYTES_SHIFT;
  766. if ((dma_desc->status & HOST_DMA_STS_MASK) == HOST_DMA_STS_PKTERR) {
  767. /*
  768. * XactError, or unable to complete all the transactions
  769. * in the scheduled micro-frame/frame, both indicated by
  770. * HOST_DMA_STS_PKTERR
  771. */
  772. qtd->urb->error_count++;
  773. frame_desc->actual_length = qh->n_bytes[idx] - remain;
  774. frame_desc->status = -EPROTO;
  775. } else {
  776. /* Success */
  777. frame_desc->actual_length = qh->n_bytes[idx] - remain;
  778. frame_desc->status = 0;
  779. }
  780. if (++qtd->isoc_frame_index == usb_urb->number_of_packets) {
  781. /*
  782. * urb->status is not used for isoc transfers here. The
  783. * individual frame_desc status are used instead.
  784. */
  785. dwc2_host_complete(hsotg, qtd, 0);
  786. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
  787. /*
  788. * This check is necessary because urb_dequeue can be called
  789. * from urb complete callback (sound driver for example). All
  790. * pending URBs are dequeued there, so no need for further
  791. * processing.
  792. */
  793. if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE)
  794. return -1;
  795. rc = DWC2_CMPL_DONE;
  796. }
  797. qh->ntd--;
  798. /* Stop if IOC requested descriptor reached */
  799. if (dma_desc->status & HOST_DMA_IOC)
  800. rc = DWC2_CMPL_STOP;
  801. return rc;
  802. }
  803. static void dwc2_complete_isoc_xfer_ddma(struct dwc2_hsotg *hsotg,
  804. struct dwc2_host_chan *chan,
  805. enum dwc2_halt_status halt_status)
  806. {
  807. struct dwc2_hcd_iso_packet_desc *frame_desc;
  808. struct dwc2_qtd *qtd, *qtd_tmp;
  809. struct dwc2_qh *qh;
  810. u16 idx;
  811. int rc;
  812. qh = chan->qh;
  813. idx = qh->td_first;
  814. if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE) {
  815. list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry)
  816. qtd->in_process = 0;
  817. return;
  818. }
  819. if (halt_status == DWC2_HC_XFER_AHB_ERR ||
  820. halt_status == DWC2_HC_XFER_BABBLE_ERR) {
  821. /*
  822. * Channel is halted in these error cases, considered as serious
  823. * issues.
  824. * Complete all URBs marking all frames as failed, irrespective
  825. * whether some of the descriptors (frames) succeeded or not.
  826. * Pass error code to completion routine as well, to update
  827. * urb->status, some of class drivers might use it to stop
  828. * queing transfer requests.
  829. */
  830. int err = halt_status == DWC2_HC_XFER_AHB_ERR ?
  831. -EIO : -EOVERFLOW;
  832. list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
  833. qtd_list_entry) {
  834. if (qtd->urb) {
  835. for (idx = 0; idx < qtd->urb->packet_count;
  836. idx++) {
  837. frame_desc = &qtd->urb->iso_descs[idx];
  838. frame_desc->status = err;
  839. }
  840. dwc2_host_complete(hsotg, qtd, err);
  841. }
  842. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
  843. }
  844. return;
  845. }
  846. list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
  847. if (!qtd->in_process)
  848. break;
  849. /*
  850. * Ensure idx corresponds to descriptor where first urb of this
  851. * qtd was added. In fact, during isoc desc init, dwc2 may skip
  852. * an index if current frame number is already over this index.
  853. */
  854. if (idx != qtd->isoc_td_first) {
  855. dev_vdbg(hsotg->dev,
  856. "try to complete %d instead of %d\n",
  857. idx, qtd->isoc_td_first);
  858. idx = qtd->isoc_td_first;
  859. }
  860. do {
  861. struct dwc2_qtd *qtd_next;
  862. u16 cur_idx;
  863. rc = dwc2_cmpl_host_isoc_dma_desc(hsotg, chan, qtd, qh,
  864. idx);
  865. if (rc < 0)
  866. return;
  867. idx = dwc2_desclist_idx_inc(idx, qh->host_interval,
  868. chan->speed);
  869. if (rc == 0)
  870. continue;
  871. if (rc == DWC2_CMPL_DONE || rc == DWC2_CMPL_STOP)
  872. goto stop_scan;
  873. /* rc == DWC2_CMPL_STOP */
  874. if (qh->host_interval >= 32)
  875. goto stop_scan;
  876. qh->td_first = idx;
  877. cur_idx = dwc2_frame_list_idx(hsotg->frame_number);
  878. qtd_next = list_first_entry(&qh->qtd_list,
  879. struct dwc2_qtd,
  880. qtd_list_entry);
  881. if (dwc2_frame_idx_num_gt(cur_idx,
  882. qtd_next->isoc_td_last))
  883. break;
  884. goto stop_scan;
  885. } while (idx != qh->td_first);
  886. }
  887. stop_scan:
  888. qh->td_first = idx;
  889. }
  890. static int dwc2_update_non_isoc_urb_state_ddma(struct dwc2_hsotg *hsotg,
  891. struct dwc2_host_chan *chan,
  892. struct dwc2_qtd *qtd,
  893. struct dwc2_dma_desc *dma_desc,
  894. enum dwc2_halt_status halt_status,
  895. u32 n_bytes, int *xfer_done)
  896. {
  897. struct dwc2_hcd_urb *urb = qtd->urb;
  898. u16 remain = 0;
  899. if (chan->ep_is_in)
  900. remain = (dma_desc->status & HOST_DMA_NBYTES_MASK) >>
  901. HOST_DMA_NBYTES_SHIFT;
  902. dev_vdbg(hsotg->dev, "remain=%d dwc2_urb=%p\n", remain, urb);
  903. if (halt_status == DWC2_HC_XFER_AHB_ERR) {
  904. dev_err(hsotg->dev, "EIO\n");
  905. urb->status = -EIO;
  906. return 1;
  907. }
  908. if ((dma_desc->status & HOST_DMA_STS_MASK) == HOST_DMA_STS_PKTERR) {
  909. switch (halt_status) {
  910. case DWC2_HC_XFER_STALL:
  911. dev_vdbg(hsotg->dev, "Stall\n");
  912. urb->status = -EPIPE;
  913. break;
  914. case DWC2_HC_XFER_BABBLE_ERR:
  915. dev_err(hsotg->dev, "Babble\n");
  916. urb->status = -EOVERFLOW;
  917. break;
  918. case DWC2_HC_XFER_XACT_ERR:
  919. dev_err(hsotg->dev, "XactErr\n");
  920. urb->status = -EPROTO;
  921. break;
  922. default:
  923. dev_err(hsotg->dev,
  924. "%s: Unhandled descriptor error status (%d)\n",
  925. __func__, halt_status);
  926. break;
  927. }
  928. return 1;
  929. }
  930. if (dma_desc->status & HOST_DMA_A) {
  931. dev_vdbg(hsotg->dev,
  932. "Active descriptor encountered on channel %d\n",
  933. chan->hc_num);
  934. return 0;
  935. }
  936. if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL) {
  937. if (qtd->control_phase == DWC2_CONTROL_DATA) {
  938. urb->actual_length += n_bytes - remain;
  939. if (remain || urb->actual_length >= urb->length) {
  940. /*
  941. * For Control Data stage do not set urb->status
  942. * to 0, to prevent URB callback. Set it when
  943. * Status phase is done. See below.
  944. */
  945. *xfer_done = 1;
  946. }
  947. } else if (qtd->control_phase == DWC2_CONTROL_STATUS) {
  948. urb->status = 0;
  949. *xfer_done = 1;
  950. }
  951. /* No handling for SETUP stage */
  952. } else {
  953. /* BULK and INTR */
  954. urb->actual_length += n_bytes - remain;
  955. dev_vdbg(hsotg->dev, "length=%d actual=%d\n", urb->length,
  956. urb->actual_length);
  957. if (remain || urb->actual_length >= urb->length) {
  958. urb->status = 0;
  959. *xfer_done = 1;
  960. }
  961. }
  962. return 0;
  963. }
  964. static int dwc2_process_non_isoc_desc(struct dwc2_hsotg *hsotg,
  965. struct dwc2_host_chan *chan,
  966. int chnum, struct dwc2_qtd *qtd,
  967. int desc_num,
  968. enum dwc2_halt_status halt_status,
  969. int *xfer_done)
  970. {
  971. struct dwc2_qh *qh = chan->qh;
  972. struct dwc2_hcd_urb *urb = qtd->urb;
  973. struct dwc2_dma_desc *dma_desc;
  974. u32 n_bytes;
  975. int failed;
  976. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  977. if (!urb)
  978. return -EINVAL;
  979. dma_sync_single_for_cpu(hsotg->dev,
  980. qh->desc_list_dma + (desc_num *
  981. sizeof(struct dwc2_dma_desc)),
  982. sizeof(struct dwc2_dma_desc),
  983. DMA_FROM_DEVICE);
  984. dma_desc = &qh->desc_list[desc_num];
  985. n_bytes = qh->n_bytes[desc_num];
  986. dev_vdbg(hsotg->dev,
  987. "qtd=%p dwc2_urb=%p desc_num=%d desc=%p n_bytes=%d\n",
  988. qtd, urb, desc_num, dma_desc, n_bytes);
  989. failed = dwc2_update_non_isoc_urb_state_ddma(hsotg, chan, qtd, dma_desc,
  990. halt_status, n_bytes,
  991. xfer_done);
  992. if (failed || (*xfer_done && urb->status != -EINPROGRESS)) {
  993. dwc2_host_complete(hsotg, qtd, urb->status);
  994. dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
  995. dev_vdbg(hsotg->dev, "failed=%1x xfer_done=%1x\n",
  996. failed, *xfer_done);
  997. return failed;
  998. }
  999. if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL) {
  1000. switch (qtd->control_phase) {
  1001. case DWC2_CONTROL_SETUP:
  1002. if (urb->length > 0)
  1003. qtd->control_phase = DWC2_CONTROL_DATA;
  1004. else
  1005. qtd->control_phase = DWC2_CONTROL_STATUS;
  1006. dev_vdbg(hsotg->dev,
  1007. " Control setup transaction done\n");
  1008. break;
  1009. case DWC2_CONTROL_DATA:
  1010. if (*xfer_done) {
  1011. qtd->control_phase = DWC2_CONTROL_STATUS;
  1012. dev_vdbg(hsotg->dev,
  1013. " Control data transfer done\n");
  1014. } else if (desc_num + 1 == qtd->n_desc) {
  1015. /*
  1016. * Last descriptor for Control data stage which
  1017. * is not completed yet
  1018. */
  1019. dwc2_hcd_save_data_toggle(hsotg, chan, chnum,
  1020. qtd);
  1021. }
  1022. break;
  1023. default:
  1024. break;
  1025. }
  1026. }
  1027. return 0;
  1028. }
  1029. static void dwc2_complete_non_isoc_xfer_ddma(struct dwc2_hsotg *hsotg,
  1030. struct dwc2_host_chan *chan,
  1031. int chnum,
  1032. enum dwc2_halt_status halt_status)
  1033. {
  1034. struct list_head *qtd_item, *qtd_tmp;
  1035. struct dwc2_qh *qh = chan->qh;
  1036. struct dwc2_qtd *qtd = NULL;
  1037. int xfer_done;
  1038. int desc_num = 0;
  1039. if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE) {
  1040. list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry)
  1041. qtd->in_process = 0;
  1042. return;
  1043. }
  1044. list_for_each_safe(qtd_item, qtd_tmp, &qh->qtd_list) {
  1045. int i;
  1046. int qtd_desc_count;
  1047. qtd = list_entry(qtd_item, struct dwc2_qtd, qtd_list_entry);
  1048. xfer_done = 0;
  1049. qtd_desc_count = qtd->n_desc;
  1050. for (i = 0; i < qtd_desc_count; i++) {
  1051. if (dwc2_process_non_isoc_desc(hsotg, chan, chnum, qtd,
  1052. desc_num, halt_status,
  1053. &xfer_done)) {
  1054. qtd = NULL;
  1055. goto stop_scan;
  1056. }
  1057. desc_num++;
  1058. }
  1059. }
  1060. stop_scan:
  1061. if (qh->ep_type != USB_ENDPOINT_XFER_CONTROL) {
  1062. /*
  1063. * Resetting the data toggle for bulk and interrupt endpoints
  1064. * in case of stall. See handle_hc_stall_intr().
  1065. */
  1066. if (halt_status == DWC2_HC_XFER_STALL)
  1067. qh->data_toggle = DWC2_HC_PID_DATA0;
  1068. else
  1069. dwc2_hcd_save_data_toggle(hsotg, chan, chnum, NULL);
  1070. }
  1071. if (halt_status == DWC2_HC_XFER_COMPLETE) {
  1072. if (chan->hcint & HCINTMSK_NYET) {
  1073. /*
  1074. * Got a NYET on the last transaction of the transfer.
  1075. * It means that the endpoint should be in the PING
  1076. * state at the beginning of the next transfer.
  1077. */
  1078. qh->ping_state = 1;
  1079. }
  1080. }
  1081. }
  1082. /**
  1083. * dwc2_hcd_complete_xfer_ddma() - Scans the descriptor list, updates URB's
  1084. * status and calls completion routine for the URB if it's done. Called from
  1085. * interrupt handlers.
  1086. *
  1087. * @hsotg: The HCD state structure for the DWC OTG controller
  1088. * @chan: Host channel the transfer is completed on
  1089. * @chnum: Index of Host channel registers
  1090. * @halt_status: Reason the channel is being halted or just XferComplete
  1091. * for isochronous transfers
  1092. *
  1093. * Releases the channel to be used by other transfers.
  1094. * In case of Isochronous endpoint the channel is not halted until the end of
  1095. * the session, i.e. QTD list is empty.
  1096. * If periodic channel released the FrameList is updated accordingly.
  1097. * Calls transaction selection routines to activate pending transfers.
  1098. */
  1099. void dwc2_hcd_complete_xfer_ddma(struct dwc2_hsotg *hsotg,
  1100. struct dwc2_host_chan *chan, int chnum,
  1101. enum dwc2_halt_status halt_status)
  1102. {
  1103. struct dwc2_qh *qh = chan->qh;
  1104. int continue_isoc_xfer = 0;
  1105. enum dwc2_transaction_type tr_type;
  1106. if (chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
  1107. dwc2_complete_isoc_xfer_ddma(hsotg, chan, halt_status);
  1108. /* Release the channel if halted or session completed */
  1109. if (halt_status != DWC2_HC_XFER_COMPLETE ||
  1110. list_empty(&qh->qtd_list)) {
  1111. struct dwc2_qtd *qtd, *qtd_tmp;
  1112. /*
  1113. * Kill all remainings QTDs since channel has been
  1114. * halted.
  1115. */
  1116. list_for_each_entry_safe(qtd, qtd_tmp,
  1117. &qh->qtd_list,
  1118. qtd_list_entry) {
  1119. dwc2_host_complete(hsotg, qtd,
  1120. -ECONNRESET);
  1121. dwc2_hcd_qtd_unlink_and_free(hsotg,
  1122. qtd, qh);
  1123. }
  1124. /* Halt the channel if session completed */
  1125. if (halt_status == DWC2_HC_XFER_COMPLETE)
  1126. dwc2_hc_halt(hsotg, chan, halt_status);
  1127. dwc2_release_channel_ddma(hsotg, qh);
  1128. dwc2_hcd_qh_unlink(hsotg, qh);
  1129. } else {
  1130. /* Keep in assigned schedule to continue transfer */
  1131. list_move_tail(&qh->qh_list_entry,
  1132. &hsotg->periodic_sched_assigned);
  1133. /*
  1134. * If channel has been halted during giveback of urb
  1135. * then prevent any new scheduling.
  1136. */
  1137. if (!chan->halt_status)
  1138. continue_isoc_xfer = 1;
  1139. }
  1140. /*
  1141. * Todo: Consider the case when period exceeds FrameList size.
  1142. * Frame Rollover interrupt should be used.
  1143. */
  1144. } else {
  1145. /*
  1146. * Scan descriptor list to complete the URB(s), then release
  1147. * the channel
  1148. */
  1149. dwc2_complete_non_isoc_xfer_ddma(hsotg, chan, chnum,
  1150. halt_status);
  1151. dwc2_release_channel_ddma(hsotg, qh);
  1152. dwc2_hcd_qh_unlink(hsotg, qh);
  1153. if (!list_empty(&qh->qtd_list)) {
  1154. /*
  1155. * Add back to inactive non-periodic schedule on normal
  1156. * completion
  1157. */
  1158. dwc2_hcd_qh_add(hsotg, qh);
  1159. }
  1160. }
  1161. tr_type = dwc2_hcd_select_transactions(hsotg);
  1162. if (tr_type != DWC2_TRANSACTION_NONE || continue_isoc_xfer) {
  1163. if (continue_isoc_xfer) {
  1164. if (tr_type == DWC2_TRANSACTION_NONE)
  1165. tr_type = DWC2_TRANSACTION_PERIODIC;
  1166. else if (tr_type == DWC2_TRANSACTION_NON_PERIODIC)
  1167. tr_type = DWC2_TRANSACTION_ALL;
  1168. }
  1169. dwc2_hcd_queue_transactions(hsotg, tr_type);
  1170. }
  1171. }