hcd_ddma.c 38 KB

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