cdnsp-ring.c 67 KB

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