cdnsp-ring.c 67 KB

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