cdnsp-ring.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Cadence CDNSP DRD Driver.
  4. *
  5. * Copyright (C) 2020 Cadence.
  6. *
  7. * Author: Pawel Laszczak <pawell@cadence.com>
  8. *
  9. * Code based on Linux XHCI driver.
  10. * Origin: Copyright (C) 2008 Intel Corp
  11. */
  12. /*
  13. * Ring initialization rules:
  14. * 1. Each segment is initialized to zero, except for link TRBs.
  15. * 2. Ring cycle state = 0. This represents Producer Cycle State (PCS) or
  16. * Consumer Cycle State (CCS), depending on ring function.
  17. * 3. Enqueue pointer = dequeue pointer = address of first TRB in the segment.
  18. *
  19. * Ring behavior rules:
  20. * 1. A ring is empty if enqueue == dequeue. This means there will always be at
  21. * least one free TRB in the ring. This is useful if you want to turn that
  22. * into a link TRB and expand the ring.
  23. * 2. When incrementing an enqueue or dequeue pointer, if the next TRB is a
  24. * link TRB, then load the pointer with the address in the link TRB. If the
  25. * link TRB had its toggle bit set, you may need to update the ring cycle
  26. * state (see cycle bit rules). You may have to do this multiple times
  27. * until you reach a non-link TRB.
  28. * 3. A ring is full if enqueue++ (for the definition of increment above)
  29. * equals the dequeue pointer.
  30. *
  31. * Cycle bit rules:
  32. * 1. When a consumer increments a dequeue pointer and encounters a toggle bit
  33. * in a link TRB, it must toggle the ring cycle state.
  34. * 2. When a producer increments an enqueue pointer and encounters a toggle bit
  35. * in a link TRB, it must toggle the ring cycle state.
  36. *
  37. * Producer rules:
  38. * 1. Check if ring is full before you enqueue.
  39. * 2. Write the ring cycle state to the cycle bit in the TRB you're enqueuing.
  40. * Update enqueue pointer between each write (which may update the ring
  41. * cycle state).
  42. * 3. Notify consumer. If SW is producer, it rings the doorbell for command
  43. * and endpoint rings. If controller is the producer for the event ring,
  44. * and it generates an interrupt according to interrupt modulation rules.
  45. *
  46. * Consumer rules:
  47. * 1. Check if TRB belongs to you. If the cycle bit == your ring cycle state,
  48. * the TRB is owned by the consumer.
  49. * 2. Update dequeue pointer (which may update the ring cycle state) and
  50. * continue processing TRBs until you reach a TRB which is not owned by you.
  51. * 3. Notify the producer. SW is the consumer for the event ring, and it
  52. * updates event ring dequeue pointer. Controller is the consumer for the
  53. * command and endpoint rings; it generates events on the event ring
  54. * for these.
  55. */
  56. #include <linux/scatterlist.h>
  57. #include <linux/dma-mapping.h>
  58. #include <linux/delay.h>
  59. #include <linux/slab.h>
  60. #include <linux/irq.h>
  61. #include "cdnsp-trace.h"
  62. #include "cdnsp-gadget.h"
  63. /*
  64. * Returns zero if the TRB isn't in this segment, otherwise it returns the DMA
  65. * address of the TRB.
  66. */
  67. dma_addr_t cdnsp_trb_virt_to_dma(struct cdnsp_segment *seg,
  68. union cdnsp_trb *trb)
  69. {
  70. unsigned long segment_offset = trb - seg->trbs;
  71. if (trb < seg->trbs || segment_offset >= TRBS_PER_SEGMENT)
  72. return 0;
  73. return seg->dma + (segment_offset * sizeof(*trb));
  74. }
  75. static bool cdnsp_trb_is_noop(union cdnsp_trb *trb)
  76. {
  77. return TRB_TYPE_NOOP_LE32(trb->generic.field[3]);
  78. }
  79. static bool cdnsp_trb_is_link(union cdnsp_trb *trb)
  80. {
  81. return TRB_TYPE_LINK_LE32(trb->link.control);
  82. }
  83. bool cdnsp_last_trb_on_seg(struct cdnsp_segment *seg, union cdnsp_trb *trb)
  84. {
  85. return trb == &seg->trbs[TRBS_PER_SEGMENT - 1];
  86. }
  87. bool cdnsp_last_trb_on_ring(struct cdnsp_ring *ring,
  88. struct cdnsp_segment *seg,
  89. union cdnsp_trb *trb)
  90. {
  91. return cdnsp_last_trb_on_seg(seg, trb) && (seg->next == ring->first_seg);
  92. }
  93. static bool cdnsp_link_trb_toggles_cycle(union cdnsp_trb *trb)
  94. {
  95. return le32_to_cpu(trb->link.control) & LINK_TOGGLE;
  96. }
  97. static void cdnsp_trb_to_noop(union cdnsp_trb *trb, u32 noop_type)
  98. {
  99. if (cdnsp_trb_is_link(trb)) {
  100. /* Unchain chained link TRBs. */
  101. trb->link.control &= cpu_to_le32(~TRB_CHAIN);
  102. } else {
  103. trb->generic.field[0] = 0;
  104. trb->generic.field[1] = 0;
  105. trb->generic.field[2] = 0;
  106. /* Preserve only the cycle bit of this TRB. */
  107. trb->generic.field[3] &= cpu_to_le32(TRB_CYCLE);
  108. trb->generic.field[3] |= cpu_to_le32(TRB_TYPE(noop_type));
  109. }
  110. }
  111. /*
  112. * Updates trb to point to the next TRB in the ring, and updates seg if the next
  113. * TRB is in a new segment. This does not skip over link TRBs, and it does not
  114. * effect the ring dequeue or enqueue pointers.
  115. */
  116. static void cdnsp_next_trb(struct cdnsp_device *pdev,
  117. struct cdnsp_ring *ring,
  118. struct cdnsp_segment **seg,
  119. union cdnsp_trb **trb)
  120. {
  121. if (cdnsp_trb_is_link(*trb)) {
  122. *seg = (*seg)->next;
  123. *trb = ((*seg)->trbs);
  124. } else {
  125. (*trb)++;
  126. }
  127. }
  128. /*
  129. * See Cycle bit rules. SW is the consumer for the event ring only.
  130. * Don't make a ring full of link TRBs. That would be dumb and this would loop.
  131. */
  132. void cdnsp_inc_deq(struct cdnsp_device *pdev, struct cdnsp_ring *ring)
  133. {
  134. /* event ring doesn't have link trbs, check for last trb. */
  135. if (ring->type == TYPE_EVENT) {
  136. if (!cdnsp_last_trb_on_seg(ring->deq_seg, ring->dequeue)) {
  137. ring->dequeue++;
  138. goto out;
  139. }
  140. if (cdnsp_last_trb_on_ring(ring, ring->deq_seg, ring->dequeue))
  141. ring->cycle_state ^= 1;
  142. ring->deq_seg = ring->deq_seg->next;
  143. ring->dequeue = ring->deq_seg->trbs;
  144. goto out;
  145. }
  146. /* All other rings have link trbs. */
  147. if (!cdnsp_trb_is_link(ring->dequeue)) {
  148. ring->dequeue++;
  149. ring->num_trbs_free++;
  150. }
  151. while (cdnsp_trb_is_link(ring->dequeue)) {
  152. ring->deq_seg = ring->deq_seg->next;
  153. ring->dequeue = ring->deq_seg->trbs;
  154. }
  155. out:
  156. trace_cdnsp_inc_deq(ring);
  157. }
  158. /*
  159. * See Cycle bit rules. SW is the consumer for the event ring only.
  160. * Don't make a ring full of link TRBs. That would be dumb and this would loop.
  161. *
  162. * If we've just enqueued a TRB that is in the middle of a TD (meaning the
  163. * chain bit is set), then set the chain bit in all the following link TRBs.
  164. * If we've enqueued the last TRB in a TD, make sure the following link TRBs
  165. * have their chain bit cleared (so that each Link TRB is a separate TD).
  166. *
  167. * @more_trbs_coming: Will you enqueue more TRBs before ringing the doorbell.
  168. */
  169. static void cdnsp_inc_enq(struct cdnsp_device *pdev,
  170. struct cdnsp_ring *ring,
  171. bool more_trbs_coming)
  172. {
  173. union cdnsp_trb *next;
  174. u32 chain;
  175. chain = le32_to_cpu(ring->enqueue->generic.field[3]) & TRB_CHAIN;
  176. /* If this is not event ring, there is one less usable TRB. */
  177. if (!cdnsp_trb_is_link(ring->enqueue))
  178. ring->num_trbs_free--;
  179. next = ++(ring->enqueue);
  180. /* Update the dequeue pointer further if that was a link TRB */
  181. while (cdnsp_trb_is_link(next)) {
  182. /*
  183. * If the caller doesn't plan on enqueuing more TDs before
  184. * ringing the doorbell, then we don't want to give the link TRB
  185. * to the hardware just yet. We'll give the link TRB back in
  186. * cdnsp_prepare_ring() just before we enqueue the TD at the
  187. * top of the ring.
  188. */
  189. if (!chain && !more_trbs_coming)
  190. break;
  191. next->link.control &= cpu_to_le32(~TRB_CHAIN);
  192. next->link.control |= cpu_to_le32(chain);
  193. /* Give this link TRB to the hardware */
  194. wmb();
  195. next->link.control ^= cpu_to_le32(TRB_CYCLE);
  196. /* Toggle the cycle bit after the last ring segment. */
  197. if (cdnsp_link_trb_toggles_cycle(next))
  198. ring->cycle_state ^= 1;
  199. ring->enq_seg = ring->enq_seg->next;
  200. ring->enqueue = ring->enq_seg->trbs;
  201. next = ring->enqueue;
  202. }
  203. trace_cdnsp_inc_enq(ring);
  204. }
  205. /*
  206. * Check to see if there's room to enqueue num_trbs on the ring and make sure
  207. * enqueue pointer will not advance into dequeue segment.
  208. */
  209. static bool cdnsp_room_on_ring(struct cdnsp_device *pdev,
  210. struct cdnsp_ring *ring,
  211. unsigned int num_trbs)
  212. {
  213. int num_trbs_in_deq_seg;
  214. if (ring->num_trbs_free < num_trbs)
  215. return false;
  216. if (ring->type != TYPE_COMMAND && ring->type != TYPE_EVENT) {
  217. num_trbs_in_deq_seg = ring->dequeue - ring->deq_seg->trbs;
  218. if (ring->num_trbs_free < num_trbs + num_trbs_in_deq_seg)
  219. return false;
  220. }
  221. return true;
  222. }
  223. /*
  224. * Workaround for L1: controller has issue with resuming from L1 after
  225. * setting doorbell for endpoint during L1 state. This function forces
  226. * resume signal in such case.
  227. */
  228. static void cdnsp_force_l0_go(struct cdnsp_device *pdev)
  229. {
  230. if (pdev->active_port == &pdev->usb2_port && pdev->gadget.lpm_capable)
  231. cdnsp_set_link_state(pdev, &pdev->active_port->regs->portsc, XDEV_U0);
  232. }
  233. /* Ring the doorbell after placing a command on the ring. */
  234. void cdnsp_ring_cmd_db(struct cdnsp_device *pdev)
  235. {
  236. writel(DB_VALUE_CMD, &pdev->dba->cmd_db);
  237. }
  238. /*
  239. * Ring the doorbell after placing a transfer on the ring.
  240. * Returns true if doorbell was set, otherwise false.
  241. */
  242. static bool cdnsp_ring_ep_doorbell(struct cdnsp_device *pdev,
  243. struct cdnsp_ep *pep,
  244. unsigned int stream_id)
  245. {
  246. __le32 __iomem *reg_addr = &pdev->dba->ep_db;
  247. unsigned int ep_state = pep->ep_state;
  248. unsigned int db_value;
  249. /*
  250. * Don't ring the doorbell for this endpoint if endpoint is halted or
  251. * disabled.
  252. */
  253. if (ep_state & EP_HALTED || !(ep_state & EP_ENABLED))
  254. return false;
  255. /* For stream capable endpoints driver can ring doorbell only twice. */
  256. if (pep->ep_state & EP_HAS_STREAMS) {
  257. if (pep->stream_info.drbls_count >= 2)
  258. return false;
  259. pep->stream_info.drbls_count++;
  260. }
  261. pep->ep_state &= ~EP_STOPPED;
  262. if (pep->idx == 0 && pdev->ep0_stage == CDNSP_DATA_STAGE &&
  263. !pdev->ep0_expect_in)
  264. db_value = DB_VALUE_EP0_OUT(pep->idx, stream_id);
  265. else
  266. db_value = DB_VALUE(pep->idx, stream_id);
  267. trace_cdnsp_tr_drbl(pep, stream_id);
  268. writel(db_value, reg_addr);
  269. if (pdev->rtl_revision < RTL_REVISION_NEW_LPM)
  270. cdnsp_force_l0_go(pdev);
  271. /* Doorbell was set. */
  272. return true;
  273. }
  274. /*
  275. * Get the right ring for the given pep and stream_id.
  276. * If the endpoint supports streams, boundary check the USB request's stream ID.
  277. * If the endpoint doesn't support streams, return the singular endpoint ring.
  278. */
  279. static struct cdnsp_ring *cdnsp_get_transfer_ring(struct cdnsp_device *pdev,
  280. struct cdnsp_ep *pep,
  281. unsigned int stream_id)
  282. {
  283. if (!(pep->ep_state & EP_HAS_STREAMS))
  284. return pep->ring;
  285. if (stream_id == 0 || stream_id >= pep->stream_info.num_streams) {
  286. dev_err(pdev->dev, "ERR: %s ring doesn't exist for SID: %d.\n",
  287. pep->name, stream_id);
  288. return NULL;
  289. }
  290. return pep->stream_info.stream_rings[stream_id];
  291. }
  292. static struct cdnsp_ring *
  293. cdnsp_request_to_transfer_ring(struct cdnsp_device *pdev,
  294. struct cdnsp_request *preq)
  295. {
  296. return cdnsp_get_transfer_ring(pdev, preq->pep,
  297. preq->request.stream_id);
  298. }
  299. /* Ring the doorbell for any rings with pending requests. */
  300. void cdnsp_ring_doorbell_for_active_rings(struct cdnsp_device *pdev,
  301. struct cdnsp_ep *pep)
  302. {
  303. struct cdnsp_stream_info *stream_info;
  304. unsigned int stream_id;
  305. int ret;
  306. if (pep->ep_state & EP_DIS_IN_RROGRESS)
  307. return;
  308. /* A ring has pending Request if its TD list is not empty. */
  309. if (!(pep->ep_state & EP_HAS_STREAMS) && pep->number) {
  310. if (pep->ring && !list_empty(&pep->ring->td_list))
  311. cdnsp_ring_ep_doorbell(pdev, pep, 0);
  312. return;
  313. }
  314. stream_info = &pep->stream_info;
  315. for (stream_id = 1; stream_id < stream_info->num_streams; stream_id++) {
  316. struct cdnsp_td *td, *td_temp;
  317. struct cdnsp_ring *ep_ring;
  318. if (stream_info->drbls_count >= 2)
  319. return;
  320. ep_ring = cdnsp_get_transfer_ring(pdev, pep, stream_id);
  321. if (!ep_ring)
  322. continue;
  323. if (!ep_ring->stream_active || ep_ring->stream_rejected)
  324. continue;
  325. list_for_each_entry_safe(td, td_temp, &ep_ring->td_list,
  326. td_list) {
  327. if (td->drbl)
  328. continue;
  329. ret = cdnsp_ring_ep_doorbell(pdev, pep, stream_id);
  330. if (ret)
  331. td->drbl = 1;
  332. }
  333. }
  334. }
  335. /*
  336. * Get the hw dequeue pointer controller stopped on, either directly from the
  337. * endpoint context, or if streams are in use from the stream context.
  338. * The returned hw_dequeue contains the lowest four bits with cycle state
  339. * and possible stream context type.
  340. */
  341. static u64 cdnsp_get_hw_deq(struct cdnsp_device *pdev,
  342. unsigned int ep_index,
  343. unsigned int stream_id)
  344. {
  345. struct cdnsp_stream_ctx *st_ctx;
  346. struct cdnsp_ep *pep;
  347. pep = &pdev->eps[ep_index];
  348. if (pep->ep_state & EP_HAS_STREAMS) {
  349. st_ctx = &pep->stream_info.stream_ctx_array[stream_id];
  350. return le64_to_cpu(st_ctx->stream_ring);
  351. }
  352. return le64_to_cpu(pep->out_ctx->deq);
  353. }
  354. /*
  355. * Move the controller endpoint ring dequeue pointer past cur_td.
  356. * Record the new state of the controller endpoint ring dequeue segment,
  357. * dequeue pointer, and new consumer cycle state in state.
  358. * Update internal representation of the ring's dequeue pointer.
  359. *
  360. * We do this in three jumps:
  361. * - First we update our new ring state to be the same as when the
  362. * controller stopped.
  363. * - Then we traverse the ring to find the segment that contains
  364. * the last TRB in the TD. We toggle the controller new cycle state
  365. * when we pass any link TRBs with the toggle cycle bit set.
  366. * - Finally we move the dequeue state one TRB further, toggling the cycle bit
  367. * if we've moved it past a link TRB with the toggle cycle bit set.
  368. */
  369. static void cdnsp_find_new_dequeue_state(struct cdnsp_device *pdev,
  370. struct cdnsp_ep *pep,
  371. unsigned int stream_id,
  372. struct cdnsp_td *cur_td,
  373. struct cdnsp_dequeue_state *state)
  374. {
  375. bool td_last_trb_found = false;
  376. struct cdnsp_segment *new_seg;
  377. struct cdnsp_ring *ep_ring;
  378. union cdnsp_trb *new_deq;
  379. bool cycle_found = false;
  380. u64 hw_dequeue;
  381. ep_ring = cdnsp_get_transfer_ring(pdev, pep, stream_id);
  382. if (!ep_ring)
  383. return;
  384. /*
  385. * Dig out the cycle state saved by the controller during the
  386. * stop endpoint command.
  387. */
  388. hw_dequeue = cdnsp_get_hw_deq(pdev, pep->idx, stream_id);
  389. new_seg = ep_ring->deq_seg;
  390. new_deq = ep_ring->dequeue;
  391. state->new_cycle_state = hw_dequeue & 0x1;
  392. state->stream_id = stream_id;
  393. /*
  394. * We want to find the pointer, segment and cycle state of the new trb
  395. * (the one after current TD's last_trb). We know the cycle state at
  396. * hw_dequeue, so walk the ring until both hw_dequeue and last_trb are
  397. * found.
  398. */
  399. do {
  400. if (!cycle_found && cdnsp_trb_virt_to_dma(new_seg, new_deq)
  401. == (dma_addr_t)(hw_dequeue & ~0xf)) {
  402. cycle_found = true;
  403. if (td_last_trb_found)
  404. break;
  405. }
  406. if (new_deq == cur_td->last_trb)
  407. td_last_trb_found = true;
  408. if (cycle_found && cdnsp_trb_is_link(new_deq) &&
  409. cdnsp_link_trb_toggles_cycle(new_deq))
  410. state->new_cycle_state ^= 0x1;
  411. cdnsp_next_trb(pdev, ep_ring, &new_seg, &new_deq);
  412. /* Search wrapped around, bail out. */
  413. if (new_deq == pep->ring->dequeue) {
  414. dev_err(pdev->dev,
  415. "Error: Failed finding new dequeue state\n");
  416. state->new_deq_seg = NULL;
  417. state->new_deq_ptr = NULL;
  418. return;
  419. }
  420. } while (!cycle_found || !td_last_trb_found);
  421. state->new_deq_seg = new_seg;
  422. state->new_deq_ptr = new_deq;
  423. trace_cdnsp_new_deq_state(state);
  424. }
  425. /*
  426. * flip_cycle means flip the cycle bit of all but the first and last TRB.
  427. * (The last TRB actually points to the ring enqueue pointer, which is not part
  428. * of this TD.) This is used to remove partially enqueued isoc TDs from a ring.
  429. */
  430. static void cdnsp_td_to_noop(struct cdnsp_device *pdev,
  431. struct cdnsp_ring *ep_ring,
  432. struct cdnsp_td *td,
  433. bool flip_cycle)
  434. {
  435. struct cdnsp_segment *seg = td->start_seg;
  436. union cdnsp_trb *trb = td->first_trb;
  437. while (1) {
  438. cdnsp_trb_to_noop(trb, TRB_TR_NOOP);
  439. /* flip cycle if asked to */
  440. if (flip_cycle && trb != td->first_trb && trb != td->last_trb)
  441. trb->generic.field[3] ^= cpu_to_le32(TRB_CYCLE);
  442. if (trb == td->last_trb)
  443. break;
  444. cdnsp_next_trb(pdev, ep_ring, &seg, &trb);
  445. }
  446. }
  447. /*
  448. * This TD is defined by the TRBs starting at start_trb in start_seg and ending
  449. * at end_trb, which may be in another segment. If the suspect DMA address is a
  450. * TRB in this TD, this function returns that TRB's segment. Otherwise it
  451. * returns 0.
  452. */
  453. static struct cdnsp_segment *cdnsp_trb_in_td(struct cdnsp_device *pdev,
  454. struct cdnsp_segment *start_seg,
  455. union cdnsp_trb *start_trb,
  456. union cdnsp_trb *end_trb,
  457. dma_addr_t suspect_dma)
  458. {
  459. struct cdnsp_segment *cur_seg;
  460. union cdnsp_trb *temp_trb;
  461. dma_addr_t end_seg_dma;
  462. dma_addr_t end_trb_dma;
  463. dma_addr_t start_dma;
  464. start_dma = cdnsp_trb_virt_to_dma(start_seg, start_trb);
  465. cur_seg = start_seg;
  466. do {
  467. if (start_dma == 0)
  468. return NULL;
  469. temp_trb = &cur_seg->trbs[TRBS_PER_SEGMENT - 1];
  470. /* We may get an event for a Link TRB in the middle of a TD */
  471. end_seg_dma = cdnsp_trb_virt_to_dma(cur_seg, temp_trb);
  472. /* If the end TRB isn't in this segment, this is set to 0 */
  473. end_trb_dma = cdnsp_trb_virt_to_dma(cur_seg, end_trb);
  474. trace_cdnsp_looking_trb_in_td(suspect_dma, start_dma,
  475. end_trb_dma, cur_seg->dma,
  476. end_seg_dma);
  477. if (end_trb_dma > 0) {
  478. /*
  479. * The end TRB is in this segment, so suspect should
  480. * be here
  481. */
  482. if (start_dma <= end_trb_dma) {
  483. if (suspect_dma >= start_dma &&
  484. suspect_dma <= end_trb_dma) {
  485. return cur_seg;
  486. }
  487. } else {
  488. /*
  489. * Case for one segment with a
  490. * TD wrapped around to the top
  491. */
  492. if ((suspect_dma >= start_dma &&
  493. suspect_dma <= end_seg_dma) ||
  494. (suspect_dma >= cur_seg->dma &&
  495. suspect_dma <= end_trb_dma)) {
  496. return cur_seg;
  497. }
  498. }
  499. return NULL;
  500. }
  501. /* Might still be somewhere in this segment */
  502. if (suspect_dma >= start_dma && suspect_dma <= end_seg_dma)
  503. return cur_seg;
  504. cur_seg = cur_seg->next;
  505. start_dma = cdnsp_trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]);
  506. } while (cur_seg != start_seg);
  507. return NULL;
  508. }
  509. static void cdnsp_unmap_td_bounce_buffer(struct cdnsp_device *pdev,
  510. struct cdnsp_ring *ring,
  511. struct cdnsp_td *td)
  512. {
  513. struct cdnsp_segment *seg = td->bounce_seg;
  514. struct cdnsp_request *preq;
  515. size_t len;
  516. if (!seg)
  517. return;
  518. preq = td->preq;
  519. trace_cdnsp_bounce_unmap(td->preq, seg->bounce_len, seg->bounce_offs,
  520. seg->bounce_dma, 0);
  521. if (!preq->direction) {
  522. dma_unmap_single(pdev->dev, seg->bounce_dma,
  523. ring->bounce_buf_len, DMA_TO_DEVICE);
  524. return;
  525. }
  526. dma_unmap_single(pdev->dev, seg->bounce_dma, ring->bounce_buf_len,
  527. DMA_FROM_DEVICE);
  528. /* For in transfers we need to copy the data from bounce to sg */
  529. len = sg_pcopy_from_buffer(preq->request.sg, preq->request.num_sgs,
  530. seg->bounce_buf, seg->bounce_len,
  531. seg->bounce_offs);
  532. if (len != seg->bounce_len)
  533. dev_warn(pdev->dev, "WARN Wrong bounce buffer read length: %zu != %d\n",
  534. len, seg->bounce_len);
  535. seg->bounce_len = 0;
  536. seg->bounce_offs = 0;
  537. }
  538. static int cdnsp_cmd_set_deq(struct cdnsp_device *pdev,
  539. struct cdnsp_ep *pep,
  540. struct cdnsp_dequeue_state *deq_state)
  541. {
  542. struct cdnsp_ring *ep_ring;
  543. int ret;
  544. if (!deq_state->new_deq_ptr || !deq_state->new_deq_seg) {
  545. cdnsp_ring_doorbell_for_active_rings(pdev, pep);
  546. return 0;
  547. }
  548. cdnsp_queue_new_dequeue_state(pdev, pep, deq_state);
  549. cdnsp_ring_cmd_db(pdev);
  550. ret = cdnsp_wait_for_cmd_compl(pdev);
  551. trace_cdnsp_handle_cmd_set_deq(cdnsp_get_slot_ctx(&pdev->out_ctx));
  552. trace_cdnsp_handle_cmd_set_deq_ep(pep->out_ctx);
  553. /*
  554. * Update the ring's dequeue segment and dequeue pointer
  555. * to reflect the new position.
  556. */
  557. ep_ring = cdnsp_get_transfer_ring(pdev, pep, deq_state->stream_id);
  558. if (cdnsp_trb_is_link(ep_ring->dequeue)) {
  559. ep_ring->deq_seg = ep_ring->deq_seg->next;
  560. ep_ring->dequeue = ep_ring->deq_seg->trbs;
  561. }
  562. while (ep_ring->dequeue != deq_state->new_deq_ptr) {
  563. ep_ring->num_trbs_free++;
  564. ep_ring->dequeue++;
  565. if (cdnsp_trb_is_link(ep_ring->dequeue)) {
  566. if (ep_ring->dequeue == deq_state->new_deq_ptr)
  567. break;
  568. ep_ring->deq_seg = ep_ring->deq_seg->next;
  569. ep_ring->dequeue = ep_ring->deq_seg->trbs;
  570. }
  571. }
  572. /*
  573. * Probably there was TIMEOUT during handling Set Dequeue Pointer
  574. * command. It's critical error and controller will be stopped.
  575. */
  576. if (ret)
  577. return -ESHUTDOWN;
  578. /* Restart any rings with pending requests */
  579. cdnsp_ring_doorbell_for_active_rings(pdev, pep);
  580. return 0;
  581. }
  582. int cdnsp_remove_request(struct cdnsp_device *pdev,
  583. struct cdnsp_request *preq,
  584. struct cdnsp_ep *pep)
  585. {
  586. struct cdnsp_dequeue_state deq_state;
  587. struct cdnsp_td *cur_td = NULL;
  588. struct cdnsp_ring *ep_ring;
  589. struct cdnsp_segment *seg;
  590. int status = -ECONNRESET;
  591. int ret = 0;
  592. u64 hw_deq;
  593. memset(&deq_state, 0, sizeof(deq_state));
  594. trace_cdnsp_remove_request(pep->out_ctx);
  595. trace_cdnsp_remove_request_td(preq);
  596. cur_td = &preq->td;
  597. ep_ring = cdnsp_request_to_transfer_ring(pdev, preq);
  598. /*
  599. * If we stopped on the TD we need to cancel, then we have to
  600. * move the controller endpoint ring dequeue pointer past
  601. * this TD.
  602. */
  603. hw_deq = cdnsp_get_hw_deq(pdev, pep->idx, preq->request.stream_id);
  604. hw_deq &= ~0xf;
  605. seg = cdnsp_trb_in_td(pdev, cur_td->start_seg, cur_td->first_trb,
  606. cur_td->last_trb, hw_deq);
  607. if (seg && (pep->ep_state & EP_ENABLED) &&
  608. !(pep->ep_state & EP_DIS_IN_RROGRESS))
  609. cdnsp_find_new_dequeue_state(pdev, pep, preq->request.stream_id,
  610. cur_td, &deq_state);
  611. else
  612. cdnsp_td_to_noop(pdev, ep_ring, cur_td, false);
  613. /*
  614. * The event handler won't see a completion for this TD anymore,
  615. * so remove it from the endpoint ring's TD list.
  616. */
  617. list_del_init(&cur_td->td_list);
  618. ep_ring->num_tds--;
  619. pep->stream_info.td_count--;
  620. /*
  621. * During disconnecting all endpoint will be disabled so we don't
  622. * have to worry about updating dequeue pointer.
  623. */
  624. if (pdev->cdnsp_state & CDNSP_STATE_DISCONNECT_PENDING ||
  625. pep->ep_state & EP_DIS_IN_RROGRESS) {
  626. status = -ESHUTDOWN;
  627. ret = cdnsp_cmd_set_deq(pdev, pep, &deq_state);
  628. }
  629. cdnsp_unmap_td_bounce_buffer(pdev, ep_ring, cur_td);
  630. cdnsp_gadget_giveback(pep, cur_td->preq, status);
  631. return ret;
  632. }
  633. static int cdnsp_update_port_id(struct cdnsp_device *pdev, u32 port_id)
  634. {
  635. struct cdnsp_port *port = pdev->active_port;
  636. u8 old_port = 0;
  637. if (port && port->port_num == port_id)
  638. return 0;
  639. if (port)
  640. old_port = port->port_num;
  641. if (port_id == pdev->usb2_port.port_num) {
  642. port = &pdev->usb2_port;
  643. } else if (port_id == pdev->usb3_port.port_num) {
  644. port = &pdev->usb3_port;
  645. } else {
  646. dev_err(pdev->dev, "Port event with invalid port ID %d\n",
  647. port_id);
  648. return -EINVAL;
  649. }
  650. if (port_id != old_port) {
  651. if (pdev->slot_id)
  652. cdnsp_disable_slot(pdev);
  653. pdev->active_port = port;
  654. cdnsp_enable_slot(pdev);
  655. }
  656. if (port_id == pdev->usb2_port.port_num)
  657. cdnsp_set_usb2_hardware_lpm(pdev, NULL, 1);
  658. else
  659. writel(PORT_U1_TIMEOUT(1) | PORT_U2_TIMEOUT(1),
  660. &pdev->usb3_port.regs->portpmsc);
  661. return 0;
  662. }
  663. static void cdnsp_handle_port_status(struct cdnsp_device *pdev,
  664. union cdnsp_trb *event)
  665. {
  666. struct cdnsp_port_regs __iomem *port_regs;
  667. u32 portsc, cmd_regs;
  668. bool port2 = false;
  669. u32 link_state;
  670. u32 port_id;
  671. /* Port status change events always have a successful completion code */
  672. if (GET_COMP_CODE(le32_to_cpu(event->generic.field[2])) != COMP_SUCCESS)
  673. dev_err(pdev->dev, "ERR: incorrect PSC event\n");
  674. port_id = GET_PORT_ID(le32_to_cpu(event->generic.field[0]));
  675. if (cdnsp_update_port_id(pdev, port_id))
  676. goto cleanup;
  677. port_regs = pdev->active_port->regs;
  678. if (port_id == pdev->usb2_port.port_num)
  679. port2 = true;
  680. new_event:
  681. portsc = readl(&port_regs->portsc);
  682. writel(cdnsp_port_state_to_neutral(portsc) |
  683. (portsc & PORT_CHANGE_BITS), &port_regs->portsc);
  684. trace_cdnsp_handle_port_status(pdev->active_port->port_num, portsc);
  685. pdev->gadget.speed = cdnsp_port_speed(portsc);
  686. link_state = portsc & PORT_PLS_MASK;
  687. /* Port Link State change detected. */
  688. if ((portsc & PORT_PLC)) {
  689. if (!(pdev->cdnsp_state & CDNSP_WAKEUP_PENDING) &&
  690. link_state == XDEV_RESUME) {
  691. cmd_regs = readl(&pdev->op_regs->command);
  692. if (!(cmd_regs & CMD_R_S))
  693. goto cleanup;
  694. if (DEV_SUPERSPEED_ANY(portsc)) {
  695. cdnsp_set_link_state(pdev, &port_regs->portsc,
  696. XDEV_U0);
  697. cdnsp_resume_gadget(pdev);
  698. }
  699. }
  700. if ((pdev->cdnsp_state & CDNSP_WAKEUP_PENDING) &&
  701. link_state == XDEV_U0) {
  702. pdev->cdnsp_state &= ~CDNSP_WAKEUP_PENDING;
  703. cdnsp_force_header_wakeup(pdev, 1);
  704. cdnsp_ring_cmd_db(pdev);
  705. cdnsp_wait_for_cmd_compl(pdev);
  706. }
  707. if (link_state == XDEV_U0 && pdev->link_state == XDEV_U3 &&
  708. !DEV_SUPERSPEED_ANY(portsc))
  709. cdnsp_resume_gadget(pdev);
  710. if (link_state == XDEV_U3 && pdev->link_state != XDEV_U3)
  711. cdnsp_suspend_gadget(pdev);
  712. pdev->link_state = link_state;
  713. }
  714. if (portsc & PORT_CSC) {
  715. /* Detach device. */
  716. if (pdev->gadget.connected && !(portsc & PORT_CONNECT))
  717. cdnsp_disconnect_gadget(pdev);
  718. /* Attach device. */
  719. if (portsc & PORT_CONNECT) {
  720. if (!port2)
  721. cdnsp_irq_reset(pdev);
  722. usb_gadget_set_state(&pdev->gadget, USB_STATE_ATTACHED);
  723. }
  724. }
  725. /* Port reset. */
  726. if ((portsc & (PORT_RC | PORT_WRC)) && (portsc & PORT_CONNECT)) {
  727. cdnsp_irq_reset(pdev);
  728. pdev->u1_allowed = 0;
  729. pdev->u2_allowed = 0;
  730. pdev->may_wakeup = 0;
  731. }
  732. if (portsc & PORT_CEC)
  733. dev_err(pdev->dev, "Port Over Current detected\n");
  734. if (portsc & PORT_CEC)
  735. dev_err(pdev->dev, "Port Configure Error detected\n");
  736. if (readl(&port_regs->portsc) & PORT_CHANGE_BITS)
  737. goto new_event;
  738. cleanup:
  739. cdnsp_inc_deq(pdev, pdev->event_ring);
  740. }
  741. static void cdnsp_td_cleanup(struct cdnsp_device *pdev,
  742. struct cdnsp_td *td,
  743. struct cdnsp_ring *ep_ring,
  744. int *status)
  745. {
  746. struct cdnsp_request *preq = td->preq;
  747. /* if a bounce buffer was used to align this td then unmap it */
  748. cdnsp_unmap_td_bounce_buffer(pdev, ep_ring, td);
  749. /*
  750. * If the controller said we transferred more data than the buffer
  751. * length, Play it safe and say we didn't transfer anything.
  752. */
  753. if (preq->request.actual > preq->request.length) {
  754. preq->request.actual = 0;
  755. *status = 0;
  756. }
  757. list_del_init(&td->td_list);
  758. ep_ring->num_tds--;
  759. preq->pep->stream_info.td_count--;
  760. cdnsp_gadget_giveback(preq->pep, preq, *status);
  761. }
  762. static void cdnsp_finish_td(struct cdnsp_device *pdev,
  763. struct cdnsp_td *td,
  764. struct cdnsp_transfer_event *event,
  765. struct cdnsp_ep *ep,
  766. int *status)
  767. {
  768. struct cdnsp_ring *ep_ring;
  769. u32 trb_comp_code;
  770. ep_ring = cdnsp_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
  771. trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
  772. if (trb_comp_code == COMP_STOPPED_LENGTH_INVALID ||
  773. trb_comp_code == COMP_STOPPED ||
  774. trb_comp_code == COMP_STOPPED_SHORT_PACKET) {
  775. /*
  776. * The Endpoint Stop Command completion will take care of any
  777. * stopped TDs. A stopped TD may be restarted, so don't update
  778. * the ring dequeue pointer or take this TD off any lists yet.
  779. */
  780. return;
  781. }
  782. /* Update ring dequeue pointer */
  783. while (ep_ring->dequeue != td->last_trb)
  784. cdnsp_inc_deq(pdev, ep_ring);
  785. cdnsp_inc_deq(pdev, ep_ring);
  786. cdnsp_td_cleanup(pdev, td, ep_ring, status);
  787. }
  788. /* sum trb lengths from ring dequeue up to stop_trb, _excluding_ stop_trb */
  789. static int cdnsp_sum_trb_lengths(struct cdnsp_device *pdev,
  790. struct cdnsp_ring *ring,
  791. union cdnsp_trb *stop_trb)
  792. {
  793. struct cdnsp_segment *seg = ring->deq_seg;
  794. union cdnsp_trb *trb = ring->dequeue;
  795. u32 sum;
  796. for (sum = 0; trb != stop_trb; cdnsp_next_trb(pdev, ring, &seg, &trb)) {
  797. if (!cdnsp_trb_is_noop(trb) && !cdnsp_trb_is_link(trb))
  798. sum += TRB_LEN(le32_to_cpu(trb->generic.field[2]));
  799. }
  800. return sum;
  801. }
  802. static int cdnsp_giveback_first_trb(struct cdnsp_device *pdev,
  803. struct cdnsp_ep *pep,
  804. unsigned int stream_id,
  805. int start_cycle,
  806. struct cdnsp_generic_trb *start_trb)
  807. {
  808. /*
  809. * Pass all the TRBs to the hardware at once and make sure this write
  810. * isn't reordered.
  811. */
  812. wmb();
  813. if (start_cycle)
  814. start_trb->field[3] |= cpu_to_le32(start_cycle);
  815. else
  816. start_trb->field[3] &= cpu_to_le32(~TRB_CYCLE);
  817. if ((pep->ep_state & EP_HAS_STREAMS) &&
  818. !pep->stream_info.first_prime_det) {
  819. trace_cdnsp_wait_for_prime(pep, stream_id);
  820. return 0;
  821. }
  822. return cdnsp_ring_ep_doorbell(pdev, pep, stream_id);
  823. }
  824. /*
  825. * Process control tds, update USB request status and actual_length.
  826. */
  827. static void cdnsp_process_ctrl_td(struct cdnsp_device *pdev,
  828. struct cdnsp_td *td,
  829. union cdnsp_trb *event_trb,
  830. struct cdnsp_transfer_event *event,
  831. struct cdnsp_ep *pep,
  832. int *status)
  833. {
  834. struct cdnsp_ring *ep_ring;
  835. u32 remaining;
  836. u32 trb_type;
  837. trb_type = TRB_FIELD_TO_TYPE(le32_to_cpu(event_trb->generic.field[3]));
  838. ep_ring = cdnsp_dma_to_transfer_ring(pep, le64_to_cpu(event->buffer));
  839. remaining = EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
  840. /*
  841. * if on data stage then update the actual_length of the USB
  842. * request and flag it as set, so it won't be overwritten in the event
  843. * for the last TRB.
  844. */
  845. if (trb_type == TRB_DATA) {
  846. td->request_length_set = true;
  847. td->preq->request.actual = td->preq->request.length - remaining;
  848. }
  849. /* at status stage */
  850. if (!td->request_length_set)
  851. td->preq->request.actual = td->preq->request.length;
  852. if (pdev->ep0_stage == CDNSP_DATA_STAGE && pep->number == 0 &&
  853. pdev->three_stage_setup) {
  854. td = list_entry(ep_ring->td_list.next, struct cdnsp_td,
  855. td_list);
  856. pdev->ep0_stage = CDNSP_STATUS_STAGE;
  857. cdnsp_giveback_first_trb(pdev, pep, 0, ep_ring->cycle_state,
  858. &td->last_trb->generic);
  859. return;
  860. }
  861. *status = 0;
  862. cdnsp_finish_td(pdev, td, event, pep, status);
  863. }
  864. /*
  865. * Process isochronous tds, update usb request status and actual_length.
  866. */
  867. static void cdnsp_process_isoc_td(struct cdnsp_device *pdev,
  868. struct cdnsp_td *td,
  869. union cdnsp_trb *ep_trb,
  870. struct cdnsp_transfer_event *event,
  871. struct cdnsp_ep *pep,
  872. int status)
  873. {
  874. struct cdnsp_request *preq = td->preq;
  875. u32 remaining, requested, ep_trb_len;
  876. bool sum_trbs_for_length = false;
  877. struct cdnsp_ring *ep_ring;
  878. u32 trb_comp_code;
  879. u32 td_length;
  880. ep_ring = cdnsp_dma_to_transfer_ring(pep, le64_to_cpu(event->buffer));
  881. trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
  882. remaining = EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
  883. ep_trb_len = TRB_LEN(le32_to_cpu(ep_trb->generic.field[2]));
  884. requested = preq->request.length;
  885. /* handle completion code */
  886. switch (trb_comp_code) {
  887. case COMP_SUCCESS:
  888. preq->request.status = 0;
  889. break;
  890. case COMP_SHORT_PACKET:
  891. preq->request.status = 0;
  892. sum_trbs_for_length = true;
  893. break;
  894. case COMP_ISOCH_BUFFER_OVERRUN:
  895. case COMP_BABBLE_DETECTED_ERROR:
  896. preq->request.status = -EOVERFLOW;
  897. break;
  898. case COMP_STOPPED:
  899. sum_trbs_for_length = true;
  900. break;
  901. case COMP_STOPPED_SHORT_PACKET:
  902. /* field normally containing residue now contains transferred */
  903. preq->request.status = 0;
  904. requested = remaining;
  905. break;
  906. case COMP_STOPPED_LENGTH_INVALID:
  907. requested = 0;
  908. remaining = 0;
  909. break;
  910. default:
  911. sum_trbs_for_length = true;
  912. preq->request.status = -1;
  913. break;
  914. }
  915. if (sum_trbs_for_length) {
  916. td_length = cdnsp_sum_trb_lengths(pdev, ep_ring, ep_trb);
  917. td_length += ep_trb_len - remaining;
  918. } else {
  919. td_length = requested;
  920. }
  921. td->preq->request.actual += td_length;
  922. cdnsp_finish_td(pdev, td, event, pep, &status);
  923. }
  924. static void cdnsp_skip_isoc_td(struct cdnsp_device *pdev,
  925. struct cdnsp_td *td,
  926. struct cdnsp_transfer_event *event,
  927. struct cdnsp_ep *pep,
  928. int status)
  929. {
  930. struct cdnsp_ring *ep_ring;
  931. ep_ring = cdnsp_dma_to_transfer_ring(pep, le64_to_cpu(event->buffer));
  932. td->preq->request.status = -EXDEV;
  933. td->preq->request.actual = 0;
  934. /* Update ring dequeue pointer */
  935. while (ep_ring->dequeue != td->last_trb)
  936. cdnsp_inc_deq(pdev, ep_ring);
  937. cdnsp_inc_deq(pdev, ep_ring);
  938. cdnsp_td_cleanup(pdev, td, ep_ring, &status);
  939. }
  940. /*
  941. * Process bulk and interrupt tds, update usb request status and actual_length.
  942. */
  943. static void cdnsp_process_bulk_intr_td(struct cdnsp_device *pdev,
  944. struct cdnsp_td *td,
  945. union cdnsp_trb *ep_trb,
  946. struct cdnsp_transfer_event *event,
  947. struct cdnsp_ep *ep,
  948. int *status)
  949. {
  950. u32 remaining, requested, ep_trb_len;
  951. struct cdnsp_ring *ep_ring;
  952. u32 trb_comp_code;
  953. ep_ring = cdnsp_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
  954. trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
  955. remaining = EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
  956. ep_trb_len = TRB_LEN(le32_to_cpu(ep_trb->generic.field[2]));
  957. requested = td->preq->request.length;
  958. switch (trb_comp_code) {
  959. case COMP_SUCCESS:
  960. case COMP_SHORT_PACKET:
  961. *status = 0;
  962. break;
  963. case COMP_STOPPED_SHORT_PACKET:
  964. td->preq->request.actual = remaining;
  965. goto finish_td;
  966. case COMP_STOPPED_LENGTH_INVALID:
  967. /* Stopped on ep trb with invalid length, exclude it. */
  968. ep_trb_len = 0;
  969. remaining = 0;
  970. break;
  971. }
  972. if (ep_trb == td->last_trb)
  973. ep_trb_len = requested - remaining;
  974. else
  975. ep_trb_len = cdnsp_sum_trb_lengths(pdev, ep_ring, ep_trb) +
  976. ep_trb_len - remaining;
  977. td->preq->request.actual = ep_trb_len;
  978. finish_td:
  979. ep->stream_info.drbls_count--;
  980. cdnsp_finish_td(pdev, td, event, ep, status);
  981. }
  982. static void cdnsp_handle_tx_nrdy(struct cdnsp_device *pdev,
  983. struct cdnsp_transfer_event *event)
  984. {
  985. struct cdnsp_generic_trb *generic;
  986. struct cdnsp_ring *ep_ring;
  987. struct cdnsp_ep *pep;
  988. int cur_stream;
  989. int ep_index;
  990. int host_sid;
  991. int dev_sid;
  992. generic = (struct cdnsp_generic_trb *)event;
  993. ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1;
  994. dev_sid = TRB_TO_DEV_STREAM(le32_to_cpu(generic->field[0]));
  995. host_sid = TRB_TO_HOST_STREAM(le32_to_cpu(generic->field[2]));
  996. pep = &pdev->eps[ep_index];
  997. if (!(pep->ep_state & EP_HAS_STREAMS))
  998. return;
  999. if (host_sid == STREAM_PRIME_ACK) {
  1000. pep->stream_info.first_prime_det = 1;
  1001. for (cur_stream = 1; cur_stream < pep->stream_info.num_streams;
  1002. cur_stream++) {
  1003. ep_ring = pep->stream_info.stream_rings[cur_stream];
  1004. ep_ring->stream_active = 1;
  1005. ep_ring->stream_rejected = 0;
  1006. }
  1007. }
  1008. if (host_sid == STREAM_REJECTED) {
  1009. struct cdnsp_td *td, *td_temp;
  1010. pep->stream_info.drbls_count--;
  1011. ep_ring = pep->stream_info.stream_rings[dev_sid];
  1012. ep_ring->stream_active = 0;
  1013. ep_ring->stream_rejected = 1;
  1014. list_for_each_entry_safe(td, td_temp, &ep_ring->td_list,
  1015. td_list) {
  1016. td->drbl = 0;
  1017. }
  1018. }
  1019. cdnsp_ring_doorbell_for_active_rings(pdev, pep);
  1020. }
  1021. /*
  1022. * If this function returns an error condition, it means it got a Transfer
  1023. * event with a corrupted TRB DMA address or endpoint is disabled.
  1024. */
  1025. static int cdnsp_handle_tx_event(struct cdnsp_device *pdev,
  1026. struct cdnsp_transfer_event *event)
  1027. {
  1028. const struct usb_endpoint_descriptor *desc;
  1029. bool handling_skipped_tds = false;
  1030. struct cdnsp_segment *ep_seg;
  1031. struct cdnsp_ring *ep_ring;
  1032. int status = -EINPROGRESS;
  1033. union cdnsp_trb *ep_trb;
  1034. dma_addr_t ep_trb_dma;
  1035. struct cdnsp_ep *pep;
  1036. struct cdnsp_td *td;
  1037. u32 trb_comp_code;
  1038. int invalidate;
  1039. int ep_index;
  1040. invalidate = le32_to_cpu(event->flags) & TRB_EVENT_INVALIDATE;
  1041. ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1;
  1042. trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
  1043. ep_trb_dma = le64_to_cpu(event->buffer);
  1044. pep = &pdev->eps[ep_index];
  1045. ep_ring = cdnsp_dma_to_transfer_ring(pep, le64_to_cpu(event->buffer));
  1046. /*
  1047. * If device is disconnect then all requests will be dequeued
  1048. * by upper layers as part of disconnect sequence.
  1049. * We don't want handle such event to avoid racing.
  1050. */
  1051. if (invalidate || !pdev->gadget.connected)
  1052. goto cleanup;
  1053. if (GET_EP_CTX_STATE(pep->out_ctx) == EP_STATE_DISABLED) {
  1054. trace_cdnsp_ep_disabled(pep->out_ctx);
  1055. goto err_out;
  1056. }
  1057. /* Some transfer events don't always point to a trb*/
  1058. if (!ep_ring) {
  1059. switch (trb_comp_code) {
  1060. case COMP_INVALID_STREAM_TYPE_ERROR:
  1061. case COMP_INVALID_STREAM_ID_ERROR:
  1062. case COMP_RING_UNDERRUN:
  1063. case COMP_RING_OVERRUN:
  1064. goto cleanup;
  1065. default:
  1066. dev_err(pdev->dev, "ERROR: %s event for unknown ring\n",
  1067. pep->name);
  1068. goto err_out;
  1069. }
  1070. }
  1071. /* Look for some error cases that need special treatment. */
  1072. switch (trb_comp_code) {
  1073. case COMP_BABBLE_DETECTED_ERROR:
  1074. status = -EOVERFLOW;
  1075. break;
  1076. case COMP_RING_UNDERRUN:
  1077. case COMP_RING_OVERRUN:
  1078. /*
  1079. * When the Isoch ring is empty, the controller will generate
  1080. * a Ring Overrun Event for IN Isoch endpoint or Ring
  1081. * Underrun Event for OUT Isoch endpoint.
  1082. */
  1083. goto cleanup;
  1084. case COMP_MISSED_SERVICE_ERROR:
  1085. /*
  1086. * When encounter missed service error, one or more isoc tds
  1087. * may be missed by controller.
  1088. * Set skip flag of the ep_ring; Complete the missed tds as
  1089. * short transfer when process the ep_ring next time.
  1090. */
  1091. pep->skip = true;
  1092. break;
  1093. }
  1094. do {
  1095. /*
  1096. * This TRB should be in the TD at the head of this ring's TD
  1097. * list.
  1098. */
  1099. if (list_empty(&ep_ring->td_list)) {
  1100. /*
  1101. * Don't print warnings if it's due to a stopped
  1102. * endpoint generating an extra completion event, or
  1103. * a event for the last TRB of a short TD we already
  1104. * got a short event for.
  1105. * The short TD is already removed from the TD list.
  1106. */
  1107. if (!(trb_comp_code == COMP_STOPPED ||
  1108. trb_comp_code == COMP_STOPPED_LENGTH_INVALID ||
  1109. ep_ring->last_td_was_short))
  1110. trace_cdnsp_trb_without_td(ep_ring,
  1111. (struct cdnsp_generic_trb *)event);
  1112. if (pep->skip) {
  1113. pep->skip = false;
  1114. trace_cdnsp_ep_list_empty_with_skip(pep, 0);
  1115. }
  1116. goto cleanup;
  1117. }
  1118. td = list_entry(ep_ring->td_list.next, struct cdnsp_td,
  1119. td_list);
  1120. /* Is this a TRB in the currently executing TD? */
  1121. ep_seg = cdnsp_trb_in_td(pdev, ep_ring->deq_seg,
  1122. ep_ring->dequeue, td->last_trb,
  1123. ep_trb_dma);
  1124. desc = td->preq->pep->endpoint.desc;
  1125. if (ep_seg) {
  1126. ep_trb = &ep_seg->trbs[(ep_trb_dma - ep_seg->dma)
  1127. / sizeof(*ep_trb)];
  1128. trace_cdnsp_handle_transfer(ep_ring,
  1129. (struct cdnsp_generic_trb *)ep_trb);
  1130. if (pep->skip && usb_endpoint_xfer_isoc(desc) &&
  1131. td->last_trb != ep_trb)
  1132. return -EAGAIN;
  1133. }
  1134. /*
  1135. * Skip the Force Stopped Event. The event_trb(ep_trb_dma)
  1136. * of FSE is not in the current TD pointed by ep_ring->dequeue
  1137. * because that the hardware dequeue pointer still at the
  1138. * previous TRB of the current TD. The previous TRB maybe a
  1139. * Link TD or the last TRB of the previous TD. The command
  1140. * completion handle will take care the rest.
  1141. */
  1142. if (!ep_seg && (trb_comp_code == COMP_STOPPED ||
  1143. trb_comp_code == COMP_STOPPED_LENGTH_INVALID)) {
  1144. pep->skip = false;
  1145. goto cleanup;
  1146. }
  1147. if (!ep_seg) {
  1148. if (!pep->skip || !usb_endpoint_xfer_isoc(desc)) {
  1149. /* Something is busted, give up! */
  1150. dev_err(pdev->dev,
  1151. "ERROR Transfer event TRB DMA ptr not "
  1152. "part of current TD ep_index %d "
  1153. "comp_code %u\n", ep_index,
  1154. trb_comp_code);
  1155. return -EINVAL;
  1156. }
  1157. cdnsp_skip_isoc_td(pdev, td, event, pep, status);
  1158. goto cleanup;
  1159. }
  1160. if (trb_comp_code == COMP_SHORT_PACKET)
  1161. ep_ring->last_td_was_short = true;
  1162. else
  1163. ep_ring->last_td_was_short = false;
  1164. if (pep->skip) {
  1165. pep->skip = false;
  1166. cdnsp_skip_isoc_td(pdev, td, event, pep, status);
  1167. goto cleanup;
  1168. }
  1169. if (cdnsp_trb_is_noop(ep_trb))
  1170. goto cleanup;
  1171. if (usb_endpoint_xfer_control(desc))
  1172. cdnsp_process_ctrl_td(pdev, td, ep_trb, event, pep,
  1173. &status);
  1174. else if (usb_endpoint_xfer_isoc(desc))
  1175. cdnsp_process_isoc_td(pdev, td, ep_trb, event, pep,
  1176. status);
  1177. else
  1178. cdnsp_process_bulk_intr_td(pdev, td, ep_trb, event, pep,
  1179. &status);
  1180. cleanup:
  1181. handling_skipped_tds = pep->skip;
  1182. /*
  1183. * Do not update event ring dequeue pointer if we're in a loop
  1184. * processing missed tds.
  1185. */
  1186. if (!handling_skipped_tds)
  1187. cdnsp_inc_deq(pdev, pdev->event_ring);
  1188. /*
  1189. * If ep->skip is set, it means there are missed tds on the
  1190. * endpoint ring need to take care of.
  1191. * Process them as short transfer until reach the td pointed by
  1192. * the event.
  1193. */
  1194. } while (handling_skipped_tds);
  1195. return 0;
  1196. err_out:
  1197. dev_err(pdev->dev, "@%016llx %08x %08x %08x %08x\n",
  1198. (unsigned long long)
  1199. cdnsp_trb_virt_to_dma(pdev->event_ring->deq_seg,
  1200. pdev->event_ring->dequeue),
  1201. lower_32_bits(le64_to_cpu(event->buffer)),
  1202. upper_32_bits(le64_to_cpu(event->buffer)),
  1203. le32_to_cpu(event->transfer_len),
  1204. le32_to_cpu(event->flags));
  1205. return -EINVAL;
  1206. }
  1207. /*
  1208. * This function handles all events on the event ring.
  1209. * Returns true for "possibly more events to process" (caller should call
  1210. * again), otherwise false if done.
  1211. */
  1212. static bool cdnsp_handle_event(struct cdnsp_device *pdev)
  1213. {
  1214. unsigned int comp_code;
  1215. union cdnsp_trb *event;
  1216. bool update_ptrs = true;
  1217. u32 cycle_bit;
  1218. int ret = 0;
  1219. u32 flags;
  1220. event = pdev->event_ring->dequeue;
  1221. flags = le32_to_cpu(event->event_cmd.flags);
  1222. cycle_bit = (flags & TRB_CYCLE);
  1223. /* Does the controller or driver own the TRB? */
  1224. if (cycle_bit != pdev->event_ring->cycle_state)
  1225. return false;
  1226. trace_cdnsp_handle_event(pdev->event_ring, &event->generic);
  1227. /*
  1228. * Barrier between reading the TRB_CYCLE (valid) flag above and any
  1229. * reads of the event's flags/data below.
  1230. */
  1231. rmb();
  1232. switch (flags & TRB_TYPE_BITMASK) {
  1233. case TRB_TYPE(TRB_COMPLETION):
  1234. /*
  1235. * Command can't be handled in interrupt context so just
  1236. * increment command ring dequeue pointer.
  1237. */
  1238. cdnsp_inc_deq(pdev, pdev->cmd_ring);
  1239. break;
  1240. case TRB_TYPE(TRB_PORT_STATUS):
  1241. cdnsp_handle_port_status(pdev, event);
  1242. update_ptrs = false;
  1243. break;
  1244. case TRB_TYPE(TRB_TRANSFER):
  1245. ret = cdnsp_handle_tx_event(pdev, &event->trans_event);
  1246. if (ret >= 0)
  1247. update_ptrs = false;
  1248. break;
  1249. case TRB_TYPE(TRB_SETUP):
  1250. pdev->ep0_stage = CDNSP_SETUP_STAGE;
  1251. pdev->setup_id = TRB_SETUPID_TO_TYPE(flags);
  1252. pdev->setup_speed = TRB_SETUP_SPEEDID(flags);
  1253. pdev->setup = *((struct usb_ctrlrequest *)
  1254. &event->trans_event.buffer);
  1255. cdnsp_setup_analyze(pdev);
  1256. break;
  1257. case TRB_TYPE(TRB_ENDPOINT_NRDY):
  1258. cdnsp_handle_tx_nrdy(pdev, &event->trans_event);
  1259. break;
  1260. case TRB_TYPE(TRB_HC_EVENT): {
  1261. comp_code = GET_COMP_CODE(le32_to_cpu(event->generic.field[2]));
  1262. switch (comp_code) {
  1263. case COMP_EVENT_RING_FULL_ERROR:
  1264. dev_err(pdev->dev, "Event Ring Full\n");
  1265. break;
  1266. default:
  1267. dev_err(pdev->dev, "Controller error code 0x%02x\n",
  1268. comp_code);
  1269. }
  1270. break;
  1271. }
  1272. case TRB_TYPE(TRB_MFINDEX_WRAP):
  1273. case TRB_TYPE(TRB_DRB_OVERFLOW):
  1274. break;
  1275. default:
  1276. dev_warn(pdev->dev, "ERROR unknown event type %ld\n",
  1277. TRB_FIELD_TO_TYPE(flags));
  1278. }
  1279. if (update_ptrs)
  1280. /* Update SW event ring dequeue pointer. */
  1281. cdnsp_inc_deq(pdev, pdev->event_ring);
  1282. /*
  1283. * Caller will call us again to check if there are more items
  1284. * on the event ring.
  1285. */
  1286. return true;
  1287. }
  1288. irqreturn_t cdnsp_thread_irq_handler(int irq, void *data)
  1289. {
  1290. struct cdnsp_device *pdev = (struct cdnsp_device *)data;
  1291. union cdnsp_trb *event_ring_deq;
  1292. unsigned long flags;
  1293. int counter = 0;
  1294. local_bh_disable();
  1295. spin_lock_irqsave(&pdev->lock, flags);
  1296. if (pdev->cdnsp_state & (CDNSP_STATE_HALTED | CDNSP_STATE_DYING)) {
  1297. /*
  1298. * While removing or stopping driver there may still be deferred
  1299. * not handled interrupt which should not be treated as error.
  1300. * Driver should simply ignore it.
  1301. */
  1302. if (pdev->gadget_driver)
  1303. cdnsp_died(pdev);
  1304. spin_unlock_irqrestore(&pdev->lock, flags);
  1305. local_bh_enable();
  1306. return IRQ_HANDLED;
  1307. }
  1308. event_ring_deq = pdev->event_ring->dequeue;
  1309. while (cdnsp_handle_event(pdev)) {
  1310. if (++counter >= TRBS_PER_EV_DEQ_UPDATE) {
  1311. cdnsp_update_erst_dequeue(pdev, event_ring_deq, 0);
  1312. event_ring_deq = pdev->event_ring->dequeue;
  1313. counter = 0;
  1314. }
  1315. }
  1316. cdnsp_update_erst_dequeue(pdev, event_ring_deq, 1);
  1317. spin_unlock_irqrestore(&pdev->lock, flags);
  1318. local_bh_enable();
  1319. return IRQ_HANDLED;
  1320. }
  1321. irqreturn_t cdnsp_irq_handler(int irq, void *priv)
  1322. {
  1323. struct cdnsp_device *pdev = (struct cdnsp_device *)priv;
  1324. u32 irq_pending;
  1325. u32 status;
  1326. status = readl(&pdev->op_regs->status);
  1327. if (status == ~(u32)0) {
  1328. cdnsp_died(pdev);
  1329. return IRQ_HANDLED;
  1330. }
  1331. if (!(status & STS_EINT))
  1332. return IRQ_NONE;
  1333. writel(status | STS_EINT, &pdev->op_regs->status);
  1334. irq_pending = readl(&pdev->ir_set->irq_pending);
  1335. irq_pending |= IMAN_IP;
  1336. writel(irq_pending, &pdev->ir_set->irq_pending);
  1337. if (status & STS_FATAL) {
  1338. cdnsp_died(pdev);
  1339. return IRQ_HANDLED;
  1340. }
  1341. return IRQ_WAKE_THREAD;
  1342. }
  1343. /*
  1344. * Generic function for queuing a TRB on a ring.
  1345. * The caller must have checked to make sure there's room on the ring.
  1346. *
  1347. * @more_trbs_coming: Will you enqueue more TRBs before setting doorbell?
  1348. */
  1349. static void cdnsp_queue_trb(struct cdnsp_device *pdev, struct cdnsp_ring *ring,
  1350. bool more_trbs_coming, u32 field1, u32 field2,
  1351. u32 field3, u32 field4)
  1352. {
  1353. struct cdnsp_generic_trb *trb;
  1354. trb = &ring->enqueue->generic;
  1355. trb->field[0] = cpu_to_le32(field1);
  1356. trb->field[1] = cpu_to_le32(field2);
  1357. trb->field[2] = cpu_to_le32(field3);
  1358. trb->field[3] = cpu_to_le32(field4);
  1359. trace_cdnsp_queue_trb(ring, trb);
  1360. cdnsp_inc_enq(pdev, ring, more_trbs_coming);
  1361. }
  1362. /*
  1363. * Does various checks on the endpoint ring, and makes it ready to
  1364. * queue num_trbs.
  1365. */
  1366. static int cdnsp_prepare_ring(struct cdnsp_device *pdev,
  1367. struct cdnsp_ring *ep_ring,
  1368. u32 ep_state, unsigned
  1369. int num_trbs,
  1370. gfp_t mem_flags)
  1371. {
  1372. unsigned int num_trbs_needed;
  1373. /* Make sure the endpoint has been added to controller schedule. */
  1374. switch (ep_state) {
  1375. case EP_STATE_STOPPED:
  1376. case EP_STATE_RUNNING:
  1377. case EP_STATE_HALTED:
  1378. break;
  1379. default:
  1380. dev_err(pdev->dev, "ERROR: incorrect endpoint state\n");
  1381. return -EINVAL;
  1382. }
  1383. while (1) {
  1384. if (cdnsp_room_on_ring(pdev, ep_ring, num_trbs))
  1385. break;
  1386. trace_cdnsp_no_room_on_ring("try ring expansion");
  1387. num_trbs_needed = num_trbs - ep_ring->num_trbs_free;
  1388. if (cdnsp_ring_expansion(pdev, ep_ring, num_trbs_needed,
  1389. mem_flags)) {
  1390. dev_err(pdev->dev, "Ring expansion failed\n");
  1391. return -ENOMEM;
  1392. }
  1393. }
  1394. while (cdnsp_trb_is_link(ep_ring->enqueue)) {
  1395. ep_ring->enqueue->link.control |= cpu_to_le32(TRB_CHAIN);
  1396. /* The cycle bit must be set as the last operation. */
  1397. wmb();
  1398. ep_ring->enqueue->link.control ^= cpu_to_le32(TRB_CYCLE);
  1399. /* Toggle the cycle bit after the last ring segment. */
  1400. if (cdnsp_link_trb_toggles_cycle(ep_ring->enqueue))
  1401. ep_ring->cycle_state ^= 1;
  1402. ep_ring->enq_seg = ep_ring->enq_seg->next;
  1403. ep_ring->enqueue = ep_ring->enq_seg->trbs;
  1404. }
  1405. return 0;
  1406. }
  1407. static int cdnsp_prepare_transfer(struct cdnsp_device *pdev,
  1408. struct cdnsp_request *preq,
  1409. unsigned int num_trbs)
  1410. {
  1411. struct cdnsp_ring *ep_ring;
  1412. int ret;
  1413. ep_ring = cdnsp_get_transfer_ring(pdev, preq->pep,
  1414. preq->request.stream_id);
  1415. if (!ep_ring)
  1416. return -EINVAL;
  1417. ret = cdnsp_prepare_ring(pdev, ep_ring,
  1418. GET_EP_CTX_STATE(preq->pep->out_ctx),
  1419. num_trbs, GFP_ATOMIC);
  1420. if (ret)
  1421. return ret;
  1422. INIT_LIST_HEAD(&preq->td.td_list);
  1423. preq->td.preq = preq;
  1424. /* Add this TD to the tail of the endpoint ring's TD list. */
  1425. list_add_tail(&preq->td.td_list, &ep_ring->td_list);
  1426. ep_ring->num_tds++;
  1427. preq->pep->stream_info.td_count++;
  1428. preq->td.start_seg = ep_ring->enq_seg;
  1429. preq->td.first_trb = ep_ring->enqueue;
  1430. return 0;
  1431. }
  1432. static unsigned int cdnsp_count_trbs(u64 addr, u64 len)
  1433. {
  1434. unsigned int num_trbs;
  1435. num_trbs = DIV_ROUND_UP(len + (addr & (TRB_MAX_BUFF_SIZE - 1)),
  1436. TRB_MAX_BUFF_SIZE);
  1437. if (num_trbs == 0)
  1438. num_trbs++;
  1439. return num_trbs;
  1440. }
  1441. static unsigned int count_trbs_needed(struct cdnsp_request *preq)
  1442. {
  1443. return cdnsp_count_trbs(preq->request.dma, preq->request.length);
  1444. }
  1445. static unsigned int count_sg_trbs_needed(struct cdnsp_request *preq)
  1446. {
  1447. unsigned int i, len, full_len, num_trbs = 0;
  1448. struct scatterlist *sg;
  1449. full_len = preq->request.length;
  1450. for_each_sg(preq->request.sg, sg, preq->request.num_sgs, i) {
  1451. len = sg_dma_len(sg);
  1452. num_trbs += cdnsp_count_trbs(sg_dma_address(sg), len);
  1453. len = min(len, full_len);
  1454. full_len -= len;
  1455. if (full_len == 0)
  1456. break;
  1457. }
  1458. return num_trbs;
  1459. }
  1460. static void cdnsp_check_trb_math(struct cdnsp_request *preq, int running_total)
  1461. {
  1462. if (running_total != preq->request.length)
  1463. dev_err(preq->pep->pdev->dev,
  1464. "%s - Miscalculated tx length, "
  1465. "queued %#x, asked for %#x (%d)\n",
  1466. preq->pep->name, running_total,
  1467. preq->request.length, preq->request.actual);
  1468. }
  1469. /*
  1470. * TD size is the number of max packet sized packets remaining in the TD
  1471. * (*not* including this TRB).
  1472. *
  1473. * Total TD packet count = total_packet_count =
  1474. * DIV_ROUND_UP(TD size in bytes / wMaxPacketSize)
  1475. *
  1476. * Packets transferred up to and including this TRB = packets_transferred =
  1477. * rounddown(total bytes transferred including this TRB / wMaxPacketSize)
  1478. *
  1479. * TD size = total_packet_count - packets_transferred
  1480. *
  1481. * It must fit in bits 21:17, so it can't be bigger than 31.
  1482. * This is taken care of in the TRB_TD_SIZE() macro
  1483. *
  1484. * The last TRB in a TD must have the TD size set to zero.
  1485. */
  1486. static u32 cdnsp_td_remainder(struct cdnsp_device *pdev,
  1487. int transferred,
  1488. int trb_buff_len,
  1489. unsigned int td_total_len,
  1490. struct cdnsp_request *preq,
  1491. bool more_trbs_coming,
  1492. bool zlp)
  1493. {
  1494. u32 maxp, total_packet_count;
  1495. /* Before ZLP driver needs set TD_SIZE = 1. */
  1496. if (zlp)
  1497. return 1;
  1498. /* One TRB with a zero-length data packet. */
  1499. if (!more_trbs_coming || (transferred == 0 && trb_buff_len == 0) ||
  1500. trb_buff_len == td_total_len)
  1501. return 0;
  1502. maxp = usb_endpoint_maxp(preq->pep->endpoint.desc);
  1503. total_packet_count = DIV_ROUND_UP(td_total_len, maxp);
  1504. /* Queuing functions don't count the current TRB into transferred. */
  1505. return (total_packet_count - ((transferred + trb_buff_len) / maxp));
  1506. }
  1507. static int cdnsp_align_td(struct cdnsp_device *pdev,
  1508. struct cdnsp_request *preq, u32 enqd_len,
  1509. u32 *trb_buff_len, struct cdnsp_segment *seg)
  1510. {
  1511. struct device *dev = pdev->dev;
  1512. unsigned int unalign;
  1513. unsigned int max_pkt;
  1514. u32 new_buff_len;
  1515. max_pkt = usb_endpoint_maxp(preq->pep->endpoint.desc);
  1516. unalign = (enqd_len + *trb_buff_len) % max_pkt;
  1517. /* We got lucky, last normal TRB data on segment is packet aligned. */
  1518. if (unalign == 0)
  1519. return 0;
  1520. /* Is the last nornal TRB alignable by splitting it. */
  1521. if (*trb_buff_len > unalign) {
  1522. *trb_buff_len -= unalign;
  1523. trace_cdnsp_bounce_align_td_split(preq, *trb_buff_len,
  1524. enqd_len, 0, unalign);
  1525. return 0;
  1526. }
  1527. /*
  1528. * We want enqd_len + trb_buff_len to sum up to a number aligned to
  1529. * number which is divisible by the endpoint's wMaxPacketSize. IOW:
  1530. * (size of currently enqueued TRBs + remainder) % wMaxPacketSize == 0.
  1531. */
  1532. new_buff_len = max_pkt - (enqd_len % max_pkt);
  1533. if (new_buff_len > (preq->request.length - enqd_len))
  1534. new_buff_len = (preq->request.length - enqd_len);
  1535. /* Create a max max_pkt sized bounce buffer pointed to by last trb. */
  1536. if (preq->direction) {
  1537. sg_pcopy_to_buffer(preq->request.sg,
  1538. preq->request.num_mapped_sgs,
  1539. seg->bounce_buf, new_buff_len, enqd_len);
  1540. seg->bounce_dma = dma_map_single(dev, seg->bounce_buf,
  1541. max_pkt, DMA_TO_DEVICE);
  1542. } else {
  1543. seg->bounce_dma = dma_map_single(dev, seg->bounce_buf,
  1544. max_pkt, DMA_FROM_DEVICE);
  1545. }
  1546. if (dma_mapping_error(dev, seg->bounce_dma)) {
  1547. /* Try without aligning.*/
  1548. dev_warn(pdev->dev,
  1549. "Failed mapping bounce buffer, not aligning\n");
  1550. return 0;
  1551. }
  1552. *trb_buff_len = new_buff_len;
  1553. seg->bounce_len = new_buff_len;
  1554. seg->bounce_offs = enqd_len;
  1555. trace_cdnsp_bounce_map(preq, new_buff_len, enqd_len, seg->bounce_dma,
  1556. unalign);
  1557. /*
  1558. * Bounce buffer successful aligned and seg->bounce_dma will be used
  1559. * in transfer TRB as new transfer buffer address.
  1560. */
  1561. return 1;
  1562. }
  1563. int cdnsp_queue_bulk_tx(struct cdnsp_device *pdev, struct cdnsp_request *preq)
  1564. {
  1565. unsigned int enqd_len, block_len, trb_buff_len, full_len;
  1566. unsigned int start_cycle, num_sgs = 0;
  1567. struct cdnsp_generic_trb *start_trb;
  1568. u32 field, length_field, remainder;
  1569. struct scatterlist *sg = NULL;
  1570. bool more_trbs_coming = true;
  1571. bool need_zero_pkt = false;
  1572. bool zero_len_trb = false;
  1573. struct cdnsp_ring *ring;
  1574. bool first_trb = true;
  1575. unsigned int num_trbs;
  1576. struct cdnsp_ep *pep;
  1577. u64 addr, send_addr;
  1578. int sent_len, ret;
  1579. ring = cdnsp_request_to_transfer_ring(pdev, preq);
  1580. if (!ring)
  1581. return -EINVAL;
  1582. full_len = preq->request.length;
  1583. if (preq->request.num_sgs) {
  1584. num_sgs = preq->request.num_sgs;
  1585. sg = preq->request.sg;
  1586. addr = (u64)sg_dma_address(sg);
  1587. block_len = sg_dma_len(sg);
  1588. num_trbs = count_sg_trbs_needed(preq);
  1589. } else {
  1590. num_trbs = count_trbs_needed(preq);
  1591. addr = (u64)preq->request.dma;
  1592. block_len = full_len;
  1593. }
  1594. pep = preq->pep;
  1595. /* Deal with request.zero - need one more td/trb. */
  1596. if (preq->request.zero && preq->request.length &&
  1597. IS_ALIGNED(full_len, usb_endpoint_maxp(pep->endpoint.desc))) {
  1598. need_zero_pkt = true;
  1599. num_trbs++;
  1600. }
  1601. ret = cdnsp_prepare_transfer(pdev, preq, num_trbs);
  1602. if (ret)
  1603. return ret;
  1604. /*
  1605. * workaround 1: STOP EP command on LINK TRB with TC bit set to 1
  1606. * causes that internal cycle bit can have incorrect state after
  1607. * command complete. In consequence empty transfer ring can be
  1608. * incorrectly detected when EP is resumed.
  1609. * NOP TRB before LINK TRB avoid such scenario. STOP EP command is
  1610. * then on NOP TRB and internal cycle bit is not changed and have
  1611. * correct value.
  1612. */
  1613. if (pep->wa1_nop_trb) {
  1614. field = le32_to_cpu(pep->wa1_nop_trb->trans_event.flags);
  1615. field ^= TRB_CYCLE;
  1616. pep->wa1_nop_trb->trans_event.flags = cpu_to_le32(field);
  1617. pep->wa1_nop_trb = NULL;
  1618. }
  1619. /*
  1620. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  1621. * until we've finished creating all the other TRBs. The ring's cycle
  1622. * state may change as we enqueue the other TRBs, so save it too.
  1623. */
  1624. start_trb = &ring->enqueue->generic;
  1625. start_cycle = ring->cycle_state;
  1626. send_addr = addr;
  1627. /* Queue the TRBs, even if they are zero-length */
  1628. for (enqd_len = 0; zero_len_trb || first_trb || enqd_len < full_len;
  1629. enqd_len += trb_buff_len) {
  1630. field = TRB_TYPE(TRB_NORMAL);
  1631. /* TRB buffer should not cross 64KB boundaries */
  1632. trb_buff_len = TRB_BUFF_LEN_UP_TO_BOUNDARY(addr);
  1633. trb_buff_len = min(trb_buff_len, block_len);
  1634. if (enqd_len + trb_buff_len > full_len)
  1635. trb_buff_len = full_len - enqd_len;
  1636. /* Don't change the cycle bit of the first TRB until later */
  1637. if (first_trb) {
  1638. first_trb = false;
  1639. if (start_cycle == 0)
  1640. field |= TRB_CYCLE;
  1641. } else {
  1642. field |= ring->cycle_state;
  1643. }
  1644. /*
  1645. * Chain all the TRBs together; clear the chain bit in the last
  1646. * TRB to indicate it's the last TRB in the chain.
  1647. */
  1648. if (enqd_len + trb_buff_len < full_len || need_zero_pkt) {
  1649. field |= TRB_CHAIN;
  1650. if (cdnsp_trb_is_link(ring->enqueue + 1)) {
  1651. if (cdnsp_align_td(pdev, preq, enqd_len,
  1652. &trb_buff_len,
  1653. ring->enq_seg)) {
  1654. send_addr = ring->enq_seg->bounce_dma;
  1655. /* Assuming TD won't span 2 segs */
  1656. preq->td.bounce_seg = ring->enq_seg;
  1657. }
  1658. }
  1659. }
  1660. if (enqd_len + trb_buff_len >= full_len) {
  1661. if (need_zero_pkt && !zero_len_trb) {
  1662. zero_len_trb = true;
  1663. } else {
  1664. zero_len_trb = false;
  1665. field &= ~TRB_CHAIN;
  1666. field |= TRB_IOC;
  1667. more_trbs_coming = false;
  1668. need_zero_pkt = false;
  1669. preq->td.last_trb = ring->enqueue;
  1670. }
  1671. }
  1672. /* Only set interrupt on short packet for OUT endpoints. */
  1673. if (!preq->direction)
  1674. field |= TRB_ISP;
  1675. /* Set the TRB length, TD size, and interrupter fields. */
  1676. remainder = cdnsp_td_remainder(pdev, enqd_len, trb_buff_len,
  1677. full_len, preq,
  1678. more_trbs_coming,
  1679. zero_len_trb);
  1680. length_field = TRB_LEN(trb_buff_len) | TRB_TD_SIZE(remainder) |
  1681. TRB_INTR_TARGET(0);
  1682. cdnsp_queue_trb(pdev, ring, more_trbs_coming,
  1683. lower_32_bits(send_addr),
  1684. upper_32_bits(send_addr),
  1685. length_field,
  1686. field);
  1687. addr += trb_buff_len;
  1688. sent_len = trb_buff_len;
  1689. while (sg && sent_len >= block_len) {
  1690. /* New sg entry */
  1691. --num_sgs;
  1692. sent_len -= block_len;
  1693. if (num_sgs != 0) {
  1694. sg = sg_next(sg);
  1695. block_len = sg_dma_len(sg);
  1696. addr = (u64)sg_dma_address(sg);
  1697. addr += sent_len;
  1698. }
  1699. }
  1700. block_len -= sent_len;
  1701. send_addr = addr;
  1702. }
  1703. if (cdnsp_trb_is_link(ring->enqueue + 1)) {
  1704. field = TRB_TYPE(TRB_TR_NOOP) | TRB_IOC;
  1705. if (!ring->cycle_state)
  1706. field |= TRB_CYCLE;
  1707. pep->wa1_nop_trb = ring->enqueue;
  1708. cdnsp_queue_trb(pdev, ring, 0, 0x0, 0x0,
  1709. TRB_INTR_TARGET(0), field);
  1710. }
  1711. cdnsp_check_trb_math(preq, enqd_len);
  1712. ret = cdnsp_giveback_first_trb(pdev, pep, preq->request.stream_id,
  1713. start_cycle, start_trb);
  1714. if (ret)
  1715. preq->td.drbl = 1;
  1716. return 0;
  1717. }
  1718. int cdnsp_queue_ctrl_tx(struct cdnsp_device *pdev, struct cdnsp_request *preq)
  1719. {
  1720. u32 field, length_field, zlp = 0;
  1721. struct cdnsp_ep *pep = preq->pep;
  1722. struct cdnsp_ring *ep_ring;
  1723. int num_trbs;
  1724. u32 maxp;
  1725. int ret;
  1726. ep_ring = cdnsp_request_to_transfer_ring(pdev, preq);
  1727. if (!ep_ring)
  1728. return -EINVAL;
  1729. /* 1 TRB for data, 1 for status */
  1730. num_trbs = (pdev->three_stage_setup) ? 2 : 1;
  1731. maxp = usb_endpoint_maxp(pep->endpoint.desc);
  1732. if (preq->request.zero && preq->request.length &&
  1733. (preq->request.length % maxp == 0)) {
  1734. num_trbs++;
  1735. zlp = 1;
  1736. }
  1737. ret = cdnsp_prepare_transfer(pdev, preq, num_trbs);
  1738. if (ret)
  1739. return ret;
  1740. /* If there's data, queue data TRBs */
  1741. if (preq->request.length > 0) {
  1742. field = TRB_TYPE(TRB_DATA);
  1743. if (zlp)
  1744. field |= TRB_CHAIN;
  1745. else
  1746. field |= TRB_IOC | (pdev->ep0_expect_in ? 0 : TRB_ISP);
  1747. if (pdev->ep0_expect_in)
  1748. field |= TRB_DIR_IN;
  1749. length_field = TRB_LEN(preq->request.length) |
  1750. TRB_TD_SIZE(zlp) | TRB_INTR_TARGET(0);
  1751. cdnsp_queue_trb(pdev, ep_ring, true,
  1752. lower_32_bits(preq->request.dma),
  1753. upper_32_bits(preq->request.dma), length_field,
  1754. field | ep_ring->cycle_state |
  1755. TRB_SETUPID(pdev->setup_id) |
  1756. pdev->setup_speed);
  1757. if (zlp) {
  1758. field = TRB_TYPE(TRB_NORMAL) | TRB_IOC;
  1759. if (!pdev->ep0_expect_in)
  1760. field = TRB_ISP;
  1761. cdnsp_queue_trb(pdev, ep_ring, true,
  1762. lower_32_bits(preq->request.dma),
  1763. upper_32_bits(preq->request.dma), 0,
  1764. field | ep_ring->cycle_state |
  1765. TRB_SETUPID(pdev->setup_id) |
  1766. pdev->setup_speed);
  1767. }
  1768. pdev->ep0_stage = CDNSP_DATA_STAGE;
  1769. }
  1770. /* Save the DMA address of the last TRB in the TD. */
  1771. preq->td.last_trb = ep_ring->enqueue;
  1772. /* Queue status TRB. */
  1773. if (preq->request.length == 0)
  1774. field = ep_ring->cycle_state;
  1775. else
  1776. field = (ep_ring->cycle_state ^ 1);
  1777. if (preq->request.length > 0 && pdev->ep0_expect_in)
  1778. field |= TRB_DIR_IN;
  1779. if (pep->ep_state & EP0_HALTED_STATUS) {
  1780. pep->ep_state &= ~EP0_HALTED_STATUS;
  1781. field |= TRB_SETUPSTAT(TRB_SETUPSTAT_STALL);
  1782. } else {
  1783. field |= TRB_SETUPSTAT(TRB_SETUPSTAT_ACK);
  1784. }
  1785. cdnsp_queue_trb(pdev, ep_ring, false, 0, 0, TRB_INTR_TARGET(0),
  1786. field | TRB_IOC | TRB_SETUPID(pdev->setup_id) |
  1787. TRB_TYPE(TRB_STATUS) | pdev->setup_speed);
  1788. cdnsp_ring_ep_doorbell(pdev, pep, preq->request.stream_id);
  1789. return 0;
  1790. }
  1791. int cdnsp_cmd_stop_ep(struct cdnsp_device *pdev, struct cdnsp_ep *pep)
  1792. {
  1793. u32 ep_state = GET_EP_CTX_STATE(pep->out_ctx);
  1794. int ret = 0;
  1795. if (ep_state == EP_STATE_STOPPED || ep_state == EP_STATE_DISABLED ||
  1796. ep_state == EP_STATE_HALTED) {
  1797. trace_cdnsp_ep_stopped_or_disabled(pep->out_ctx);
  1798. goto ep_stopped;
  1799. }
  1800. cdnsp_queue_stop_endpoint(pdev, pep->idx);
  1801. cdnsp_ring_cmd_db(pdev);
  1802. ret = cdnsp_wait_for_cmd_compl(pdev);
  1803. trace_cdnsp_handle_cmd_stop_ep(pep->out_ctx);
  1804. ep_stopped:
  1805. pep->ep_state |= EP_STOPPED;
  1806. return ret;
  1807. }
  1808. /*
  1809. * The transfer burst count field of the isochronous TRB defines the number of
  1810. * bursts that are required to move all packets in this TD. Only SuperSpeed
  1811. * devices can burst up to bMaxBurst number of packets per service interval.
  1812. * This field is zero based, meaning a value of zero in the field means one
  1813. * burst. Basically, for everything but SuperSpeed devices, this field will be
  1814. * zero.
  1815. */
  1816. static unsigned int cdnsp_get_burst_count(struct cdnsp_device *pdev,
  1817. struct cdnsp_request *preq,
  1818. unsigned int total_packet_count)
  1819. {
  1820. unsigned int max_burst;
  1821. if (pdev->gadget.speed < USB_SPEED_SUPER)
  1822. return 0;
  1823. max_burst = preq->pep->endpoint.comp_desc->bMaxBurst;
  1824. return DIV_ROUND_UP(total_packet_count, max_burst + 1) - 1;
  1825. }
  1826. /*
  1827. * Returns the number of packets in the last "burst" of packets. This field is
  1828. * valid for all speeds of devices. USB 2.0 devices can only do one "burst", so
  1829. * the last burst packet count is equal to the total number of packets in the
  1830. * TD. SuperSpeed endpoints can have up to 3 bursts. All but the last burst
  1831. * must contain (bMaxBurst + 1) number of packets, but the last burst can
  1832. * contain 1 to (bMaxBurst + 1) packets.
  1833. */
  1834. static unsigned int
  1835. cdnsp_get_last_burst_packet_count(struct cdnsp_device *pdev,
  1836. struct cdnsp_request *preq,
  1837. unsigned int total_packet_count)
  1838. {
  1839. unsigned int max_burst;
  1840. unsigned int residue;
  1841. if (pdev->gadget.speed >= USB_SPEED_SUPER) {
  1842. /* bMaxBurst is zero based: 0 means 1 packet per burst. */
  1843. max_burst = preq->pep->endpoint.comp_desc->bMaxBurst;
  1844. residue = total_packet_count % (max_burst + 1);
  1845. /*
  1846. * If residue is zero, the last burst contains (max_burst + 1)
  1847. * number of packets, but the TLBPC field is zero-based.
  1848. */
  1849. if (residue == 0)
  1850. return max_burst;
  1851. return residue - 1;
  1852. }
  1853. if (total_packet_count == 0)
  1854. return 0;
  1855. return total_packet_count - 1;
  1856. }
  1857. /* Queue function isoc transfer */
  1858. int cdnsp_queue_isoc_tx(struct cdnsp_device *pdev,
  1859. struct cdnsp_request *preq)
  1860. {
  1861. unsigned int trb_buff_len, td_len, td_remain_len, block_len;
  1862. unsigned int burst_count, last_burst_pkt;
  1863. unsigned int total_pkt_count, max_pkt;
  1864. struct cdnsp_generic_trb *start_trb;
  1865. struct scatterlist *sg = NULL;
  1866. bool more_trbs_coming = true;
  1867. struct cdnsp_ring *ep_ring;
  1868. unsigned int num_sgs = 0;
  1869. int running_total = 0;
  1870. u32 field, length_field;
  1871. u64 addr, send_addr;
  1872. int start_cycle;
  1873. int trbs_per_td;
  1874. int i, sent_len, ret;
  1875. ep_ring = preq->pep->ring;
  1876. td_len = preq->request.length;
  1877. if (preq->request.num_sgs) {
  1878. num_sgs = preq->request.num_sgs;
  1879. sg = preq->request.sg;
  1880. addr = (u64)sg_dma_address(sg);
  1881. block_len = sg_dma_len(sg);
  1882. trbs_per_td = count_sg_trbs_needed(preq);
  1883. } else {
  1884. addr = (u64)preq->request.dma;
  1885. block_len = td_len;
  1886. trbs_per_td = count_trbs_needed(preq);
  1887. }
  1888. ret = cdnsp_prepare_transfer(pdev, preq, trbs_per_td);
  1889. if (ret)
  1890. return ret;
  1891. start_trb = &ep_ring->enqueue->generic;
  1892. start_cycle = ep_ring->cycle_state;
  1893. td_remain_len = td_len;
  1894. send_addr = addr;
  1895. max_pkt = usb_endpoint_maxp(preq->pep->endpoint.desc);
  1896. total_pkt_count = DIV_ROUND_UP(td_len, max_pkt);
  1897. /* A zero-length transfer still involves at least one packet. */
  1898. if (total_pkt_count == 0)
  1899. total_pkt_count++;
  1900. burst_count = cdnsp_get_burst_count(pdev, preq, total_pkt_count);
  1901. last_burst_pkt = cdnsp_get_last_burst_packet_count(pdev, preq,
  1902. total_pkt_count);
  1903. /*
  1904. * Set isoc specific data for the first TRB in a TD.
  1905. * Prevent HW from getting the TRBs by keeping the cycle state
  1906. * inverted in the first TDs isoc TRB.
  1907. */
  1908. field = TRB_TYPE(TRB_ISOC) | TRB_TLBPC(last_burst_pkt) |
  1909. TRB_SIA | TRB_TBC(burst_count);
  1910. if (!start_cycle)
  1911. field |= TRB_CYCLE;
  1912. /* Fill the rest of the TRB fields, and remaining normal TRBs. */
  1913. for (i = 0; i < trbs_per_td; i++) {
  1914. u32 remainder;
  1915. /* Calculate TRB length. */
  1916. trb_buff_len = TRB_BUFF_LEN_UP_TO_BOUNDARY(addr);
  1917. trb_buff_len = min(trb_buff_len, block_len);
  1918. if (trb_buff_len > td_remain_len)
  1919. trb_buff_len = td_remain_len;
  1920. /* Set the TRB length, TD size, & interrupter fields. */
  1921. remainder = cdnsp_td_remainder(pdev, running_total,
  1922. trb_buff_len, td_len, preq,
  1923. more_trbs_coming, 0);
  1924. length_field = TRB_LEN(trb_buff_len) | TRB_TD_SIZE(remainder) |
  1925. TRB_INTR_TARGET(0);
  1926. /* Only first TRB is isoc, overwrite otherwise. */
  1927. if (i) {
  1928. field = TRB_TYPE(TRB_NORMAL) | ep_ring->cycle_state;
  1929. length_field |= TRB_TD_SIZE(remainder);
  1930. } else {
  1931. length_field |= TRB_TD_SIZE_TBC(burst_count);
  1932. }
  1933. /* Only set interrupt on short packet for OUT EPs. */
  1934. if (usb_endpoint_dir_out(preq->pep->endpoint.desc))
  1935. field |= TRB_ISP;
  1936. /* Set the chain bit for all except the last TRB. */
  1937. if (i < trbs_per_td - 1) {
  1938. more_trbs_coming = true;
  1939. field |= TRB_CHAIN;
  1940. } else {
  1941. more_trbs_coming = false;
  1942. preq->td.last_trb = ep_ring->enqueue;
  1943. field |= TRB_IOC;
  1944. }
  1945. cdnsp_queue_trb(pdev, ep_ring, more_trbs_coming,
  1946. lower_32_bits(send_addr), upper_32_bits(send_addr),
  1947. length_field, field);
  1948. running_total += trb_buff_len;
  1949. addr += trb_buff_len;
  1950. td_remain_len -= trb_buff_len;
  1951. sent_len = trb_buff_len;
  1952. while (sg && sent_len >= block_len) {
  1953. /* New sg entry */
  1954. --num_sgs;
  1955. sent_len -= block_len;
  1956. if (num_sgs != 0) {
  1957. sg = sg_next(sg);
  1958. block_len = sg_dma_len(sg);
  1959. addr = (u64)sg_dma_address(sg);
  1960. addr += sent_len;
  1961. }
  1962. }
  1963. block_len -= sent_len;
  1964. send_addr = addr;
  1965. }
  1966. /* Check TD length */
  1967. if (running_total != td_len) {
  1968. dev_err(pdev->dev, "ISOC TD length unmatch\n");
  1969. ret = -EINVAL;
  1970. goto cleanup;
  1971. }
  1972. cdnsp_giveback_first_trb(pdev, preq->pep, preq->request.stream_id,
  1973. start_cycle, start_trb);
  1974. return 0;
  1975. cleanup:
  1976. /* Clean up a partially enqueued isoc transfer. */
  1977. list_del_init(&preq->td.td_list);
  1978. ep_ring->num_tds--;
  1979. /*
  1980. * Use the first TD as a temporary variable to turn the TDs we've
  1981. * queued into No-ops with a software-owned cycle bit.
  1982. * That way the hardware won't accidentally start executing bogus TDs
  1983. * when we partially overwrite them.
  1984. * td->first_trb and td->start_seg are already set.
  1985. */
  1986. preq->td.last_trb = ep_ring->enqueue;
  1987. /* Every TRB except the first & last will have its cycle bit flipped. */
  1988. cdnsp_td_to_noop(pdev, ep_ring, &preq->td, true);
  1989. /* Reset the ring enqueue back to the first TRB and its cycle bit. */
  1990. ep_ring->enqueue = preq->td.first_trb;
  1991. ep_ring->enq_seg = preq->td.start_seg;
  1992. ep_ring->cycle_state = start_cycle;
  1993. return ret;
  1994. }
  1995. /**** Command Ring Operations ****/
  1996. /*
  1997. * Generic function for queuing a command TRB on the command ring.
  1998. * Driver queue only one command to ring in the moment.
  1999. */
  2000. static void cdnsp_queue_command(struct cdnsp_device *pdev,
  2001. u32 field1,
  2002. u32 field2,
  2003. u32 field3,
  2004. u32 field4)
  2005. {
  2006. cdnsp_prepare_ring(pdev, pdev->cmd_ring, EP_STATE_RUNNING, 1,
  2007. GFP_ATOMIC);
  2008. pdev->cmd.command_trb = pdev->cmd_ring->enqueue;
  2009. cdnsp_queue_trb(pdev, pdev->cmd_ring, false, field1, field2,
  2010. field3, field4 | pdev->cmd_ring->cycle_state);
  2011. }
  2012. /* Queue a slot enable or disable request on the command ring */
  2013. void cdnsp_queue_slot_control(struct cdnsp_device *pdev, u32 trb_type)
  2014. {
  2015. cdnsp_queue_command(pdev, 0, 0, 0, TRB_TYPE(trb_type) |
  2016. SLOT_ID_FOR_TRB(pdev->slot_id));
  2017. }
  2018. /* Queue an address device command TRB */
  2019. void cdnsp_queue_address_device(struct cdnsp_device *pdev,
  2020. dma_addr_t in_ctx_ptr,
  2021. enum cdnsp_setup_dev setup)
  2022. {
  2023. cdnsp_queue_command(pdev, lower_32_bits(in_ctx_ptr),
  2024. upper_32_bits(in_ctx_ptr), 0,
  2025. TRB_TYPE(TRB_ADDR_DEV) |
  2026. SLOT_ID_FOR_TRB(pdev->slot_id) |
  2027. (setup == SETUP_CONTEXT_ONLY ? TRB_BSR : 0));
  2028. }
  2029. /* Queue a reset device command TRB */
  2030. void cdnsp_queue_reset_device(struct cdnsp_device *pdev)
  2031. {
  2032. cdnsp_queue_command(pdev, 0, 0, 0, TRB_TYPE(TRB_RESET_DEV) |
  2033. SLOT_ID_FOR_TRB(pdev->slot_id));
  2034. }
  2035. /* Queue a configure endpoint command TRB */
  2036. void cdnsp_queue_configure_endpoint(struct cdnsp_device *pdev,
  2037. dma_addr_t in_ctx_ptr)
  2038. {
  2039. cdnsp_queue_command(pdev, lower_32_bits(in_ctx_ptr),
  2040. upper_32_bits(in_ctx_ptr), 0,
  2041. TRB_TYPE(TRB_CONFIG_EP) |
  2042. SLOT_ID_FOR_TRB(pdev->slot_id));
  2043. }
  2044. /*
  2045. * Suspend is set to indicate "Stop Endpoint Command" is being issued to stop
  2046. * activity on an endpoint that is about to be suspended.
  2047. */
  2048. void cdnsp_queue_stop_endpoint(struct cdnsp_device *pdev, unsigned int ep_index)
  2049. {
  2050. cdnsp_queue_command(pdev, 0, 0, 0, SLOT_ID_FOR_TRB(pdev->slot_id) |
  2051. EP_ID_FOR_TRB(ep_index) | TRB_TYPE(TRB_STOP_RING));
  2052. }
  2053. /* Set Transfer Ring Dequeue Pointer command. */
  2054. void cdnsp_queue_new_dequeue_state(struct cdnsp_device *pdev,
  2055. struct cdnsp_ep *pep,
  2056. struct cdnsp_dequeue_state *deq_state)
  2057. {
  2058. u32 trb_stream_id = STREAM_ID_FOR_TRB(deq_state->stream_id);
  2059. u32 trb_slot_id = SLOT_ID_FOR_TRB(pdev->slot_id);
  2060. u32 type = TRB_TYPE(TRB_SET_DEQ);
  2061. u32 trb_sct = 0;
  2062. dma_addr_t addr;
  2063. addr = cdnsp_trb_virt_to_dma(deq_state->new_deq_seg,
  2064. deq_state->new_deq_ptr);
  2065. if (deq_state->stream_id)
  2066. trb_sct = SCT_FOR_TRB(SCT_PRI_TR);
  2067. cdnsp_queue_command(pdev, lower_32_bits(addr) | trb_sct |
  2068. deq_state->new_cycle_state, upper_32_bits(addr),
  2069. trb_stream_id, trb_slot_id |
  2070. EP_ID_FOR_TRB(pep->idx) | type);
  2071. }
  2072. void cdnsp_queue_reset_ep(struct cdnsp_device *pdev, unsigned int ep_index)
  2073. {
  2074. return cdnsp_queue_command(pdev, 0, 0, 0,
  2075. SLOT_ID_FOR_TRB(pdev->slot_id) |
  2076. EP_ID_FOR_TRB(ep_index) |
  2077. TRB_TYPE(TRB_RESET_EP));
  2078. }
  2079. /*
  2080. * Queue a halt endpoint request on the command ring.
  2081. */
  2082. void cdnsp_queue_halt_endpoint(struct cdnsp_device *pdev, unsigned int ep_index)
  2083. {
  2084. cdnsp_queue_command(pdev, 0, 0, 0, TRB_TYPE(TRB_HALT_ENDPOINT) |
  2085. SLOT_ID_FOR_TRB(pdev->slot_id) |
  2086. EP_ID_FOR_TRB(ep_index) |
  2087. (!ep_index ? TRB_ESP : 0));
  2088. }
  2089. void cdnsp_force_header_wakeup(struct cdnsp_device *pdev, int intf_num)
  2090. {
  2091. u32 lo, mid;
  2092. lo = TRB_FH_TO_PACKET_TYPE(TRB_FH_TR_PACKET) |
  2093. TRB_FH_TO_DEVICE_ADDRESS(pdev->device_address);
  2094. mid = TRB_FH_TR_PACKET_DEV_NOT |
  2095. TRB_FH_TO_NOT_TYPE(TRB_FH_TR_PACKET_FUNCTION_WAKE) |
  2096. TRB_FH_TO_INTERFACE(intf_num);
  2097. cdnsp_queue_command(pdev, lo, mid, 0,
  2098. TRB_TYPE(TRB_FORCE_HEADER) | SET_PORT_ID(2));
  2099. }