hcd_ddma.c 38 KB

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