xhci.c 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630
  1. /*
  2. * xHCI host controller driver
  3. *
  4. * Copyright (C) 2008 Intel Corp.
  5. *
  6. * Author: Sarah Sharp
  7. * Some code borrowed from the Linux EHCI driver.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  16. * for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software Foundation,
  20. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/pci.h>
  23. #include <linux/irq.h>
  24. #include <linux/log2.h>
  25. #include <linux/module.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/slab.h>
  28. #include "xhci.h"
  29. #define DRIVER_AUTHOR "Sarah Sharp"
  30. #define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
  31. /* Some 0.95 hardware can't handle the chain bit on a Link TRB being cleared */
  32. static int link_quirk;
  33. module_param(link_quirk, int, S_IRUGO | S_IWUSR);
  34. MODULE_PARM_DESC(link_quirk, "Don't clear the chain bit on a link TRB");
  35. /* TODO: copied from ehci-hcd.c - can this be refactored? */
  36. /*
  37. * handshake - spin reading hc until handshake completes or fails
  38. * @ptr: address of hc register to be read
  39. * @mask: bits to look at in result of read
  40. * @done: value of those bits when handshake succeeds
  41. * @usec: timeout in microseconds
  42. *
  43. * Returns negative errno, or zero on success
  44. *
  45. * Success happens when the "mask" bits have the specified value (hardware
  46. * handshake done). There are two failure modes: "usec" have passed (major
  47. * hardware flakeout), or the register reads as all-ones (hardware removed).
  48. */
  49. static int handshake(struct xhci_hcd *xhci, void __iomem *ptr,
  50. u32 mask, u32 done, int usec)
  51. {
  52. u32 result;
  53. do {
  54. result = xhci_readl(xhci, ptr);
  55. if (result == ~(u32)0) /* card removed */
  56. return -ENODEV;
  57. result &= mask;
  58. if (result == done)
  59. return 0;
  60. udelay(1);
  61. usec--;
  62. } while (usec > 0);
  63. return -ETIMEDOUT;
  64. }
  65. /*
  66. * Disable interrupts and begin the xHCI halting process.
  67. */
  68. void xhci_quiesce(struct xhci_hcd *xhci)
  69. {
  70. u32 halted;
  71. u32 cmd;
  72. u32 mask;
  73. mask = ~(XHCI_IRQS);
  74. halted = xhci_readl(xhci, &xhci->op_regs->status) & STS_HALT;
  75. if (!halted)
  76. mask &= ~CMD_RUN;
  77. cmd = xhci_readl(xhci, &xhci->op_regs->command);
  78. cmd &= mask;
  79. xhci_writel(xhci, cmd, &xhci->op_regs->command);
  80. }
  81. /*
  82. * Force HC into halt state.
  83. *
  84. * Disable any IRQs and clear the run/stop bit.
  85. * HC will complete any current and actively pipelined transactions, and
  86. * should halt within 16 ms of the run/stop bit being cleared.
  87. * Read HC Halted bit in the status register to see when the HC is finished.
  88. */
  89. int xhci_halt(struct xhci_hcd *xhci)
  90. {
  91. int ret;
  92. xhci_dbg(xhci, "// Halt the HC\n");
  93. xhci_quiesce(xhci);
  94. ret = handshake(xhci, &xhci->op_regs->status,
  95. STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
  96. if (!ret)
  97. xhci->xhc_state |= XHCI_STATE_HALTED;
  98. return ret;
  99. }
  100. /*
  101. * Set the run bit and wait for the host to be running.
  102. */
  103. static int xhci_start(struct xhci_hcd *xhci)
  104. {
  105. u32 temp;
  106. int ret;
  107. temp = xhci_readl(xhci, &xhci->op_regs->command);
  108. temp |= (CMD_RUN);
  109. xhci_dbg(xhci, "// Turn on HC, cmd = 0x%x.\n",
  110. temp);
  111. xhci_writel(xhci, temp, &xhci->op_regs->command);
  112. /*
  113. * Wait for the HCHalted Status bit to be 0 to indicate the host is
  114. * running.
  115. */
  116. ret = handshake(xhci, &xhci->op_regs->status,
  117. STS_HALT, 0, XHCI_MAX_HALT_USEC);
  118. if (ret == -ETIMEDOUT)
  119. xhci_err(xhci, "Host took too long to start, "
  120. "waited %u microseconds.\n",
  121. XHCI_MAX_HALT_USEC);
  122. if (!ret)
  123. xhci->xhc_state &= ~XHCI_STATE_HALTED;
  124. return ret;
  125. }
  126. /*
  127. * Reset a halted HC.
  128. *
  129. * This resets pipelines, timers, counters, state machines, etc.
  130. * Transactions will be terminated immediately, and operational registers
  131. * will be set to their defaults.
  132. */
  133. int xhci_reset(struct xhci_hcd *xhci)
  134. {
  135. u32 command;
  136. u32 state;
  137. int ret;
  138. state = xhci_readl(xhci, &xhci->op_regs->status);
  139. if ((state & STS_HALT) == 0) {
  140. xhci_warn(xhci, "Host controller not halted, aborting reset.\n");
  141. return 0;
  142. }
  143. xhci_dbg(xhci, "// Reset the HC\n");
  144. command = xhci_readl(xhci, &xhci->op_regs->command);
  145. command |= CMD_RESET;
  146. xhci_writel(xhci, command, &xhci->op_regs->command);
  147. ret = handshake(xhci, &xhci->op_regs->command,
  148. CMD_RESET, 0, 250 * 1000);
  149. if (ret)
  150. return ret;
  151. xhci_dbg(xhci, "Wait for controller to be ready for doorbell rings\n");
  152. /*
  153. * xHCI cannot write to any doorbells or operational registers other
  154. * than status until the "Controller Not Ready" flag is cleared.
  155. */
  156. return handshake(xhci, &xhci->op_regs->status, STS_CNR, 0, 250 * 1000);
  157. }
  158. /*
  159. * Free IRQs
  160. * free all IRQs request
  161. */
  162. static void xhci_free_irq(struct xhci_hcd *xhci)
  163. {
  164. int i;
  165. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  166. /* return if using legacy interrupt */
  167. if (xhci_to_hcd(xhci)->irq >= 0)
  168. return;
  169. if (xhci->msix_entries) {
  170. for (i = 0; i < xhci->msix_count; i++)
  171. if (xhci->msix_entries[i].vector)
  172. free_irq(xhci->msix_entries[i].vector,
  173. xhci_to_hcd(xhci));
  174. } else if (pdev->irq >= 0)
  175. free_irq(pdev->irq, xhci_to_hcd(xhci));
  176. return;
  177. }
  178. /*
  179. * Set up MSI
  180. */
  181. static int xhci_setup_msi(struct xhci_hcd *xhci)
  182. {
  183. int ret;
  184. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  185. ret = pci_enable_msi(pdev);
  186. if (ret) {
  187. xhci_err(xhci, "failed to allocate MSI entry\n");
  188. return ret;
  189. }
  190. ret = request_irq(pdev->irq, (irq_handler_t)xhci_msi_irq,
  191. 0, "xhci_hcd", xhci_to_hcd(xhci));
  192. if (ret) {
  193. xhci_err(xhci, "disable MSI interrupt\n");
  194. pci_disable_msi(pdev);
  195. }
  196. return ret;
  197. }
  198. /*
  199. * Set up MSI-X
  200. */
  201. static int xhci_setup_msix(struct xhci_hcd *xhci)
  202. {
  203. int i, ret = 0;
  204. struct usb_hcd *hcd = xhci_to_hcd(xhci);
  205. struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
  206. /*
  207. * calculate number of msi-x vectors supported.
  208. * - HCS_MAX_INTRS: the max number of interrupts the host can handle,
  209. * with max number of interrupters based on the xhci HCSPARAMS1.
  210. * - num_online_cpus: maximum msi-x vectors per CPUs core.
  211. * Add additional 1 vector to ensure always available interrupt.
  212. */
  213. xhci->msix_count = min(num_online_cpus() + 1,
  214. HCS_MAX_INTRS(xhci->hcs_params1));
  215. xhci->msix_entries =
  216. kmalloc((sizeof(struct msix_entry))*xhci->msix_count,
  217. GFP_KERNEL);
  218. if (!xhci->msix_entries) {
  219. xhci_err(xhci, "Failed to allocate MSI-X entries\n");
  220. return -ENOMEM;
  221. }
  222. for (i = 0; i < xhci->msix_count; i++) {
  223. xhci->msix_entries[i].entry = i;
  224. xhci->msix_entries[i].vector = 0;
  225. }
  226. ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count);
  227. if (ret) {
  228. xhci_err(xhci, "Failed to enable MSI-X\n");
  229. goto free_entries;
  230. }
  231. for (i = 0; i < xhci->msix_count; i++) {
  232. ret = request_irq(xhci->msix_entries[i].vector,
  233. (irq_handler_t)xhci_msi_irq,
  234. 0, "xhci_hcd", xhci_to_hcd(xhci));
  235. if (ret)
  236. goto disable_msix;
  237. }
  238. hcd->msix_enabled = 1;
  239. return ret;
  240. disable_msix:
  241. xhci_err(xhci, "disable MSI-X interrupt\n");
  242. xhci_free_irq(xhci);
  243. pci_disable_msix(pdev);
  244. free_entries:
  245. kfree(xhci->msix_entries);
  246. xhci->msix_entries = NULL;
  247. return ret;
  248. }
  249. /* Free any IRQs and disable MSI-X */
  250. static void xhci_cleanup_msix(struct xhci_hcd *xhci)
  251. {
  252. struct usb_hcd *hcd = xhci_to_hcd(xhci);
  253. struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
  254. xhci_free_irq(xhci);
  255. if (xhci->msix_entries) {
  256. pci_disable_msix(pdev);
  257. kfree(xhci->msix_entries);
  258. xhci->msix_entries = NULL;
  259. } else {
  260. pci_disable_msi(pdev);
  261. }
  262. hcd->msix_enabled = 0;
  263. return;
  264. }
  265. /*
  266. * Initialize memory for HCD and xHC (one-time init).
  267. *
  268. * Program the PAGESIZE register, initialize the device context array, create
  269. * device contexts (?), set up a command ring segment (or two?), create event
  270. * ring (one for now).
  271. */
  272. int xhci_init(struct usb_hcd *hcd)
  273. {
  274. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  275. int retval = 0;
  276. xhci_dbg(xhci, "xhci_init\n");
  277. spin_lock_init(&xhci->lock);
  278. if (link_quirk) {
  279. xhci_dbg(xhci, "QUIRK: Not clearing Link TRB chain bits.\n");
  280. xhci->quirks |= XHCI_LINK_TRB_QUIRK;
  281. } else {
  282. xhci_dbg(xhci, "xHCI doesn't need link TRB QUIRK\n");
  283. }
  284. retval = xhci_mem_init(xhci, GFP_KERNEL);
  285. xhci_dbg(xhci, "Finished xhci_init\n");
  286. return retval;
  287. }
  288. /*-------------------------------------------------------------------------*/
  289. #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
  290. static void xhci_event_ring_work(unsigned long arg)
  291. {
  292. unsigned long flags;
  293. int temp;
  294. u64 temp_64;
  295. struct xhci_hcd *xhci = (struct xhci_hcd *) arg;
  296. int i, j;
  297. xhci_dbg(xhci, "Poll event ring: %lu\n", jiffies);
  298. spin_lock_irqsave(&xhci->lock, flags);
  299. temp = xhci_readl(xhci, &xhci->op_regs->status);
  300. xhci_dbg(xhci, "op reg status = 0x%x\n", temp);
  301. if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) ||
  302. (xhci->xhc_state & XHCI_STATE_HALTED)) {
  303. xhci_dbg(xhci, "HW died, polling stopped.\n");
  304. spin_unlock_irqrestore(&xhci->lock, flags);
  305. return;
  306. }
  307. temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  308. xhci_dbg(xhci, "ir_set 0 pending = 0x%x\n", temp);
  309. xhci_dbg(xhci, "HC error bitmask = 0x%x\n", xhci->error_bitmask);
  310. xhci->error_bitmask = 0;
  311. xhci_dbg(xhci, "Event ring:\n");
  312. xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
  313. xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
  314. temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
  315. temp_64 &= ~ERST_PTR_MASK;
  316. xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
  317. xhci_dbg(xhci, "Command ring:\n");
  318. xhci_debug_segment(xhci, xhci->cmd_ring->deq_seg);
  319. xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
  320. xhci_dbg_cmd_ptrs(xhci);
  321. for (i = 0; i < MAX_HC_SLOTS; ++i) {
  322. if (!xhci->devs[i])
  323. continue;
  324. for (j = 0; j < 31; ++j) {
  325. xhci_dbg_ep_rings(xhci, i, j, &xhci->devs[i]->eps[j]);
  326. }
  327. }
  328. spin_unlock_irqrestore(&xhci->lock, flags);
  329. if (!xhci->zombie)
  330. mod_timer(&xhci->event_ring_timer, jiffies + POLL_TIMEOUT * HZ);
  331. else
  332. xhci_dbg(xhci, "Quit polling the event ring.\n");
  333. }
  334. #endif
  335. static int xhci_run_finished(struct xhci_hcd *xhci)
  336. {
  337. if (xhci_start(xhci)) {
  338. xhci_halt(xhci);
  339. return -ENODEV;
  340. }
  341. xhci->shared_hcd->state = HC_STATE_RUNNING;
  342. if (xhci->quirks & XHCI_NEC_HOST)
  343. xhci_ring_cmd_db(xhci);
  344. xhci_dbg(xhci, "Finished xhci_run for USB3 roothub\n");
  345. return 0;
  346. }
  347. /*
  348. * Start the HC after it was halted.
  349. *
  350. * This function is called by the USB core when the HC driver is added.
  351. * Its opposite is xhci_stop().
  352. *
  353. * xhci_init() must be called once before this function can be called.
  354. * Reset the HC, enable device slot contexts, program DCBAAP, and
  355. * set command ring pointer and event ring pointer.
  356. *
  357. * Setup MSI-X vectors and enable interrupts.
  358. */
  359. int xhci_run(struct usb_hcd *hcd)
  360. {
  361. u32 temp;
  362. u64 temp_64;
  363. u32 ret;
  364. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  365. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  366. /* Start the xHCI host controller running only after the USB 2.0 roothub
  367. * is setup.
  368. */
  369. hcd->uses_new_polling = 1;
  370. if (!usb_hcd_is_primary_hcd(hcd))
  371. return xhci_run_finished(xhci);
  372. xhci_dbg(xhci, "xhci_run\n");
  373. /* unregister the legacy interrupt */
  374. if (hcd->irq)
  375. free_irq(hcd->irq, hcd);
  376. hcd->irq = -1;
  377. /* Some Fresco Logic host controllers advertise MSI, but fail to
  378. * generate interrupts. Don't even try to enable MSI.
  379. */
  380. if (xhci->quirks & XHCI_BROKEN_MSI)
  381. goto legacy_irq;
  382. ret = xhci_setup_msix(xhci);
  383. if (ret)
  384. /* fall back to msi*/
  385. ret = xhci_setup_msi(xhci);
  386. if (ret) {
  387. legacy_irq:
  388. /* fall back to legacy interrupt*/
  389. ret = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED,
  390. hcd->irq_descr, hcd);
  391. if (ret) {
  392. xhci_err(xhci, "request interrupt %d failed\n",
  393. pdev->irq);
  394. return ret;
  395. }
  396. hcd->irq = pdev->irq;
  397. }
  398. #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
  399. init_timer(&xhci->event_ring_timer);
  400. xhci->event_ring_timer.data = (unsigned long) xhci;
  401. xhci->event_ring_timer.function = xhci_event_ring_work;
  402. /* Poll the event ring */
  403. xhci->event_ring_timer.expires = jiffies + POLL_TIMEOUT * HZ;
  404. xhci->zombie = 0;
  405. xhci_dbg(xhci, "Setting event ring polling timer\n");
  406. add_timer(&xhci->event_ring_timer);
  407. #endif
  408. xhci_dbg(xhci, "Command ring memory map follows:\n");
  409. xhci_debug_ring(xhci, xhci->cmd_ring);
  410. xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
  411. xhci_dbg_cmd_ptrs(xhci);
  412. xhci_dbg(xhci, "ERST memory map follows:\n");
  413. xhci_dbg_erst(xhci, &xhci->erst);
  414. xhci_dbg(xhci, "Event ring:\n");
  415. xhci_debug_ring(xhci, xhci->event_ring);
  416. xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
  417. temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
  418. temp_64 &= ~ERST_PTR_MASK;
  419. xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
  420. xhci_dbg(xhci, "// Set the interrupt modulation register\n");
  421. temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
  422. temp &= ~ER_IRQ_INTERVAL_MASK;
  423. temp |= (u32) 160;
  424. xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
  425. /* Set the HCD state before we enable the irqs */
  426. temp = xhci_readl(xhci, &xhci->op_regs->command);
  427. temp |= (CMD_EIE);
  428. xhci_dbg(xhci, "// Enable interrupts, cmd = 0x%x.\n",
  429. temp);
  430. xhci_writel(xhci, temp, &xhci->op_regs->command);
  431. temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  432. xhci_dbg(xhci, "// Enabling event ring interrupter %p by writing 0x%x to irq_pending\n",
  433. xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
  434. xhci_writel(xhci, ER_IRQ_ENABLE(temp),
  435. &xhci->ir_set->irq_pending);
  436. xhci_print_ir_set(xhci, 0);
  437. if (xhci->quirks & XHCI_NEC_HOST)
  438. xhci_queue_vendor_command(xhci, 0, 0, 0,
  439. TRB_TYPE(TRB_NEC_GET_FW));
  440. xhci_dbg(xhci, "Finished xhci_run for USB2 roothub\n");
  441. return 0;
  442. }
  443. static void xhci_only_stop_hcd(struct usb_hcd *hcd)
  444. {
  445. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  446. spin_lock_irq(&xhci->lock);
  447. xhci_halt(xhci);
  448. /* The shared_hcd is going to be deallocated shortly (the USB core only
  449. * calls this function when allocation fails in usb_add_hcd(), or
  450. * usb_remove_hcd() is called). So we need to unset xHCI's pointer.
  451. */
  452. xhci->shared_hcd = NULL;
  453. spin_unlock_irq(&xhci->lock);
  454. }
  455. /*
  456. * Stop xHCI driver.
  457. *
  458. * This function is called by the USB core when the HC driver is removed.
  459. * Its opposite is xhci_run().
  460. *
  461. * Disable device contexts, disable IRQs, and quiesce the HC.
  462. * Reset the HC, finish any completed transactions, and cleanup memory.
  463. */
  464. void xhci_stop(struct usb_hcd *hcd)
  465. {
  466. u32 temp;
  467. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  468. if (!usb_hcd_is_primary_hcd(hcd)) {
  469. xhci_only_stop_hcd(xhci->shared_hcd);
  470. return;
  471. }
  472. spin_lock_irq(&xhci->lock);
  473. /* Make sure the xHC is halted for a USB3 roothub
  474. * (xhci_stop() could be called as part of failed init).
  475. */
  476. xhci_halt(xhci);
  477. xhci_reset(xhci);
  478. spin_unlock_irq(&xhci->lock);
  479. xhci_cleanup_msix(xhci);
  480. #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
  481. /* Tell the event ring poll function not to reschedule */
  482. xhci->zombie = 1;
  483. del_timer_sync(&xhci->event_ring_timer);
  484. #endif
  485. if (xhci->quirks & XHCI_AMD_PLL_FIX)
  486. usb_amd_dev_put();
  487. xhci_dbg(xhci, "// Disabling event ring interrupts\n");
  488. temp = xhci_readl(xhci, &xhci->op_regs->status);
  489. xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
  490. temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  491. xhci_writel(xhci, ER_IRQ_DISABLE(temp),
  492. &xhci->ir_set->irq_pending);
  493. xhci_print_ir_set(xhci, 0);
  494. xhci_dbg(xhci, "cleaning up memory\n");
  495. xhci_mem_cleanup(xhci);
  496. xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
  497. xhci_readl(xhci, &xhci->op_regs->status));
  498. }
  499. /*
  500. * Shutdown HC (not bus-specific)
  501. *
  502. * This is called when the machine is rebooting or halting. We assume that the
  503. * machine will be powered off, and the HC's internal state will be reset.
  504. * Don't bother to free memory.
  505. *
  506. * This will only ever be called with the main usb_hcd (the USB3 roothub).
  507. */
  508. void xhci_shutdown(struct usb_hcd *hcd)
  509. {
  510. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  511. spin_lock_irq(&xhci->lock);
  512. xhci_halt(xhci);
  513. spin_unlock_irq(&xhci->lock);
  514. xhci_cleanup_msix(xhci);
  515. xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n",
  516. xhci_readl(xhci, &xhci->op_regs->status));
  517. }
  518. #ifdef CONFIG_PM
  519. static void xhci_save_registers(struct xhci_hcd *xhci)
  520. {
  521. xhci->s3.command = xhci_readl(xhci, &xhci->op_regs->command);
  522. xhci->s3.dev_nt = xhci_readl(xhci, &xhci->op_regs->dev_notification);
  523. xhci->s3.dcbaa_ptr = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
  524. xhci->s3.config_reg = xhci_readl(xhci, &xhci->op_regs->config_reg);
  525. xhci->s3.irq_pending = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  526. xhci->s3.irq_control = xhci_readl(xhci, &xhci->ir_set->irq_control);
  527. xhci->s3.erst_size = xhci_readl(xhci, &xhci->ir_set->erst_size);
  528. xhci->s3.erst_base = xhci_read_64(xhci, &xhci->ir_set->erst_base);
  529. xhci->s3.erst_dequeue = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
  530. }
  531. static void xhci_restore_registers(struct xhci_hcd *xhci)
  532. {
  533. xhci_writel(xhci, xhci->s3.command, &xhci->op_regs->command);
  534. xhci_writel(xhci, xhci->s3.dev_nt, &xhci->op_regs->dev_notification);
  535. xhci_write_64(xhci, xhci->s3.dcbaa_ptr, &xhci->op_regs->dcbaa_ptr);
  536. xhci_writel(xhci, xhci->s3.config_reg, &xhci->op_regs->config_reg);
  537. xhci_writel(xhci, xhci->s3.irq_pending, &xhci->ir_set->irq_pending);
  538. xhci_writel(xhci, xhci->s3.irq_control, &xhci->ir_set->irq_control);
  539. xhci_writel(xhci, xhci->s3.erst_size, &xhci->ir_set->erst_size);
  540. xhci_write_64(xhci, xhci->s3.erst_base, &xhci->ir_set->erst_base);
  541. }
  542. static void xhci_set_cmd_ring_deq(struct xhci_hcd *xhci)
  543. {
  544. u64 val_64;
  545. /* step 2: initialize command ring buffer */
  546. val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
  547. val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
  548. (xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
  549. xhci->cmd_ring->dequeue) &
  550. (u64) ~CMD_RING_RSVD_BITS) |
  551. xhci->cmd_ring->cycle_state;
  552. xhci_dbg(xhci, "// Setting command ring address to 0x%llx\n",
  553. (long unsigned long) val_64);
  554. xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
  555. }
  556. /*
  557. * The whole command ring must be cleared to zero when we suspend the host.
  558. *
  559. * The host doesn't save the command ring pointer in the suspend well, so we
  560. * need to re-program it on resume. Unfortunately, the pointer must be 64-byte
  561. * aligned, because of the reserved bits in the command ring dequeue pointer
  562. * register. Therefore, we can't just set the dequeue pointer back in the
  563. * middle of the ring (TRBs are 16-byte aligned).
  564. */
  565. static void xhci_clear_command_ring(struct xhci_hcd *xhci)
  566. {
  567. struct xhci_ring *ring;
  568. struct xhci_segment *seg;
  569. ring = xhci->cmd_ring;
  570. seg = ring->deq_seg;
  571. do {
  572. memset(seg->trbs, 0, SEGMENT_SIZE);
  573. seg = seg->next;
  574. } while (seg != ring->deq_seg);
  575. /* Reset the software enqueue and dequeue pointers */
  576. ring->deq_seg = ring->first_seg;
  577. ring->dequeue = ring->first_seg->trbs;
  578. ring->enq_seg = ring->deq_seg;
  579. ring->enqueue = ring->dequeue;
  580. /*
  581. * Ring is now zeroed, so the HW should look for change of ownership
  582. * when the cycle bit is set to 1.
  583. */
  584. ring->cycle_state = 1;
  585. /*
  586. * Reset the hardware dequeue pointer.
  587. * Yes, this will need to be re-written after resume, but we're paranoid
  588. * and want to make sure the hardware doesn't access bogus memory
  589. * because, say, the BIOS or an SMI started the host without changing
  590. * the command ring pointers.
  591. */
  592. xhci_set_cmd_ring_deq(xhci);
  593. }
  594. /*
  595. * Stop HC (not bus-specific)
  596. *
  597. * This is called when the machine transition into S3/S4 mode.
  598. *
  599. */
  600. int xhci_suspend(struct xhci_hcd *xhci)
  601. {
  602. int rc = 0;
  603. struct usb_hcd *hcd = xhci_to_hcd(xhci);
  604. u32 command;
  605. int i;
  606. spin_lock_irq(&xhci->lock);
  607. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  608. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
  609. /* step 1: stop endpoint */
  610. /* skipped assuming that port suspend has done */
  611. /* step 2: clear Run/Stop bit */
  612. command = xhci_readl(xhci, &xhci->op_regs->command);
  613. command &= ~CMD_RUN;
  614. xhci_writel(xhci, command, &xhci->op_regs->command);
  615. if (handshake(xhci, &xhci->op_regs->status,
  616. STS_HALT, STS_HALT, 100*100)) {
  617. xhci_warn(xhci, "WARN: xHC CMD_RUN timeout\n");
  618. spin_unlock_irq(&xhci->lock);
  619. return -ETIMEDOUT;
  620. }
  621. xhci_clear_command_ring(xhci);
  622. /* step 3: save registers */
  623. xhci_save_registers(xhci);
  624. /* step 4: set CSS flag */
  625. command = xhci_readl(xhci, &xhci->op_regs->command);
  626. command |= CMD_CSS;
  627. xhci_writel(xhci, command, &xhci->op_regs->command);
  628. if (handshake(xhci, &xhci->op_regs->status, STS_SAVE, 0, 10*100)) {
  629. xhci_warn(xhci, "WARN: xHC CMD_CSS timeout\n");
  630. spin_unlock_irq(&xhci->lock);
  631. return -ETIMEDOUT;
  632. }
  633. spin_unlock_irq(&xhci->lock);
  634. /* step 5: remove core well power */
  635. /* synchronize irq when using MSI-X */
  636. if (xhci->msix_entries) {
  637. for (i = 0; i < xhci->msix_count; i++)
  638. synchronize_irq(xhci->msix_entries[i].vector);
  639. }
  640. return rc;
  641. }
  642. /*
  643. * start xHC (not bus-specific)
  644. *
  645. * This is called when the machine transition from S3/S4 mode.
  646. *
  647. */
  648. int xhci_resume(struct xhci_hcd *xhci, bool hibernated)
  649. {
  650. u32 command, temp = 0;
  651. struct usb_hcd *hcd = xhci_to_hcd(xhci);
  652. struct usb_hcd *secondary_hcd;
  653. int retval;
  654. /* Wait a bit if either of the roothubs need to settle from the
  655. * transition into bus suspend.
  656. */
  657. if (time_before(jiffies, xhci->bus_state[0].next_statechange) ||
  658. time_before(jiffies,
  659. xhci->bus_state[1].next_statechange))
  660. msleep(100);
  661. spin_lock_irq(&xhci->lock);
  662. if (xhci->quirks & XHCI_RESET_ON_RESUME)
  663. hibernated = true;
  664. if (!hibernated) {
  665. /* step 1: restore register */
  666. xhci_restore_registers(xhci);
  667. /* step 2: initialize command ring buffer */
  668. xhci_set_cmd_ring_deq(xhci);
  669. /* step 3: restore state and start state*/
  670. /* step 3: set CRS flag */
  671. command = xhci_readl(xhci, &xhci->op_regs->command);
  672. command |= CMD_CRS;
  673. xhci_writel(xhci, command, &xhci->op_regs->command);
  674. if (handshake(xhci, &xhci->op_regs->status,
  675. STS_RESTORE, 0, 10*100)) {
  676. xhci_dbg(xhci, "WARN: xHC CMD_CSS timeout\n");
  677. spin_unlock_irq(&xhci->lock);
  678. return -ETIMEDOUT;
  679. }
  680. temp = xhci_readl(xhci, &xhci->op_regs->status);
  681. }
  682. /* If restore operation fails, re-initialize the HC during resume */
  683. if ((temp & STS_SRE) || hibernated) {
  684. /* Let the USB core know _both_ roothubs lost power. */
  685. usb_root_hub_lost_power(xhci->main_hcd->self.root_hub);
  686. usb_root_hub_lost_power(xhci->shared_hcd->self.root_hub);
  687. xhci_dbg(xhci, "Stop HCD\n");
  688. xhci_halt(xhci);
  689. xhci_reset(xhci);
  690. spin_unlock_irq(&xhci->lock);
  691. xhci_cleanup_msix(xhci);
  692. #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
  693. /* Tell the event ring poll function not to reschedule */
  694. xhci->zombie = 1;
  695. del_timer_sync(&xhci->event_ring_timer);
  696. #endif
  697. xhci_dbg(xhci, "// Disabling event ring interrupts\n");
  698. temp = xhci_readl(xhci, &xhci->op_regs->status);
  699. xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
  700. temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  701. xhci_writel(xhci, ER_IRQ_DISABLE(temp),
  702. &xhci->ir_set->irq_pending);
  703. xhci_print_ir_set(xhci, 0);
  704. xhci_dbg(xhci, "cleaning up memory\n");
  705. xhci_mem_cleanup(xhci);
  706. xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
  707. xhci_readl(xhci, &xhci->op_regs->status));
  708. /* USB core calls the PCI reinit and start functions twice:
  709. * first with the primary HCD, and then with the secondary HCD.
  710. * If we don't do the same, the host will never be started.
  711. */
  712. if (!usb_hcd_is_primary_hcd(hcd))
  713. secondary_hcd = hcd;
  714. else
  715. secondary_hcd = xhci->shared_hcd;
  716. xhci_dbg(xhci, "Initialize the xhci_hcd\n");
  717. retval = xhci_init(hcd->primary_hcd);
  718. if (retval)
  719. return retval;
  720. xhci_dbg(xhci, "Start the primary HCD\n");
  721. retval = xhci_run(hcd->primary_hcd);
  722. if (retval)
  723. goto failed_restart;
  724. xhci_dbg(xhci, "Start the secondary HCD\n");
  725. retval = xhci_run(secondary_hcd);
  726. if (!retval) {
  727. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  728. set_bit(HCD_FLAG_HW_ACCESSIBLE,
  729. &xhci->shared_hcd->flags);
  730. }
  731. failed_restart:
  732. hcd->state = HC_STATE_SUSPENDED;
  733. xhci->shared_hcd->state = HC_STATE_SUSPENDED;
  734. return retval;
  735. }
  736. /* step 4: set Run/Stop bit */
  737. command = xhci_readl(xhci, &xhci->op_regs->command);
  738. command |= CMD_RUN;
  739. xhci_writel(xhci, command, &xhci->op_regs->command);
  740. handshake(xhci, &xhci->op_regs->status, STS_HALT,
  741. 0, 250 * 1000);
  742. /* step 5: walk topology and initialize portsc,
  743. * portpmsc and portli
  744. */
  745. /* this is done in bus_resume */
  746. /* step 6: restart each of the previously
  747. * Running endpoints by ringing their doorbells
  748. */
  749. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  750. set_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
  751. spin_unlock_irq(&xhci->lock);
  752. return 0;
  753. }
  754. #endif /* CONFIG_PM */
  755. /*-------------------------------------------------------------------------*/
  756. /**
  757. * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
  758. * HCDs. Find the index for an endpoint given its descriptor. Use the return
  759. * value to right shift 1 for the bitmask.
  760. *
  761. * Index = (epnum * 2) + direction - 1,
  762. * where direction = 0 for OUT, 1 for IN.
  763. * For control endpoints, the IN index is used (OUT index is unused), so
  764. * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
  765. */
  766. unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
  767. {
  768. unsigned int index;
  769. if (usb_endpoint_xfer_control(desc))
  770. index = (unsigned int) (usb_endpoint_num(desc)*2);
  771. else
  772. index = (unsigned int) (usb_endpoint_num(desc)*2) +
  773. (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
  774. return index;
  775. }
  776. /* Find the flag for this endpoint (for use in the control context). Use the
  777. * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
  778. * bit 1, etc.
  779. */
  780. unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
  781. {
  782. return 1 << (xhci_get_endpoint_index(desc) + 1);
  783. }
  784. /* Find the flag for this endpoint (for use in the control context). Use the
  785. * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
  786. * bit 1, etc.
  787. */
  788. unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index)
  789. {
  790. return 1 << (ep_index + 1);
  791. }
  792. /* Compute the last valid endpoint context index. Basically, this is the
  793. * endpoint index plus one. For slot contexts with more than valid endpoint,
  794. * we find the most significant bit set in the added contexts flags.
  795. * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
  796. * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
  797. */
  798. unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
  799. {
  800. return fls(added_ctxs) - 1;
  801. }
  802. /* Returns 1 if the arguments are OK;
  803. * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
  804. */
  805. static int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
  806. struct usb_host_endpoint *ep, int check_ep, bool check_virt_dev,
  807. const char *func) {
  808. struct xhci_hcd *xhci;
  809. struct xhci_virt_device *virt_dev;
  810. if (!hcd || (check_ep && !ep) || !udev) {
  811. printk(KERN_DEBUG "xHCI %s called with invalid args\n",
  812. func);
  813. return -EINVAL;
  814. }
  815. if (!udev->parent) {
  816. printk(KERN_DEBUG "xHCI %s called for root hub\n",
  817. func);
  818. return 0;
  819. }
  820. xhci = hcd_to_xhci(hcd);
  821. if (xhci->xhc_state & XHCI_STATE_HALTED)
  822. return -ENODEV;
  823. if (check_virt_dev) {
  824. if (!udev->slot_id || !xhci->devs
  825. || !xhci->devs[udev->slot_id]) {
  826. printk(KERN_DEBUG "xHCI %s called with unaddressed "
  827. "device\n", func);
  828. return -EINVAL;
  829. }
  830. virt_dev = xhci->devs[udev->slot_id];
  831. if (virt_dev->udev != udev) {
  832. printk(KERN_DEBUG "xHCI %s called with udev and "
  833. "virt_dev does not match\n", func);
  834. return -EINVAL;
  835. }
  836. }
  837. return 1;
  838. }
  839. static int xhci_configure_endpoint(struct xhci_hcd *xhci,
  840. struct usb_device *udev, struct xhci_command *command,
  841. bool ctx_change, bool must_succeed);
  842. /*
  843. * Full speed devices may have a max packet size greater than 8 bytes, but the
  844. * USB core doesn't know that until it reads the first 8 bytes of the
  845. * descriptor. If the usb_device's max packet size changes after that point,
  846. * we need to issue an evaluate context command and wait on it.
  847. */
  848. static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
  849. unsigned int ep_index, struct urb *urb)
  850. {
  851. struct xhci_container_ctx *in_ctx;
  852. struct xhci_container_ctx *out_ctx;
  853. struct xhci_input_control_ctx *ctrl_ctx;
  854. struct xhci_ep_ctx *ep_ctx;
  855. int max_packet_size;
  856. int hw_max_packet_size;
  857. int ret = 0;
  858. out_ctx = xhci->devs[slot_id]->out_ctx;
  859. ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
  860. hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
  861. max_packet_size = usb_endpoint_maxp(&urb->dev->ep0.desc);
  862. if (hw_max_packet_size != max_packet_size) {
  863. xhci_dbg(xhci, "Max Packet Size for ep 0 changed.\n");
  864. xhci_dbg(xhci, "Max packet size in usb_device = %d\n",
  865. max_packet_size);
  866. xhci_dbg(xhci, "Max packet size in xHCI HW = %d\n",
  867. hw_max_packet_size);
  868. xhci_dbg(xhci, "Issuing evaluate context command.\n");
  869. /* Set up the modified control endpoint 0 */
  870. xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
  871. xhci->devs[slot_id]->out_ctx, ep_index);
  872. in_ctx = xhci->devs[slot_id]->in_ctx;
  873. ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
  874. ep_ctx->ep_info2 &= cpu_to_le32(~MAX_PACKET_MASK);
  875. ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size));
  876. /* Set up the input context flags for the command */
  877. /* FIXME: This won't work if a non-default control endpoint
  878. * changes max packet sizes.
  879. */
  880. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  881. ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
  882. ctrl_ctx->drop_flags = 0;
  883. xhci_dbg(xhci, "Slot %d input context\n", slot_id);
  884. xhci_dbg_ctx(xhci, in_ctx, ep_index);
  885. xhci_dbg(xhci, "Slot %d output context\n", slot_id);
  886. xhci_dbg_ctx(xhci, out_ctx, ep_index);
  887. ret = xhci_configure_endpoint(xhci, urb->dev, NULL,
  888. true, false);
  889. /* Clean up the input context for later use by bandwidth
  890. * functions.
  891. */
  892. ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
  893. }
  894. return ret;
  895. }
  896. /*
  897. * non-error returns are a promise to giveback() the urb later
  898. * we drop ownership so next owner (or urb unlink) can get it
  899. */
  900. int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
  901. {
  902. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  903. struct xhci_td *buffer;
  904. unsigned long flags;
  905. int ret = 0;
  906. unsigned int slot_id, ep_index;
  907. struct urb_priv *urb_priv;
  908. int size, i;
  909. if (!urb || xhci_check_args(hcd, urb->dev, urb->ep,
  910. true, true, __func__) <= 0)
  911. return -EINVAL;
  912. slot_id = urb->dev->slot_id;
  913. ep_index = xhci_get_endpoint_index(&urb->ep->desc);
  914. if (!HCD_HW_ACCESSIBLE(hcd)) {
  915. if (!in_interrupt())
  916. xhci_dbg(xhci, "urb submitted during PCI suspend\n");
  917. ret = -ESHUTDOWN;
  918. goto exit;
  919. }
  920. if (usb_endpoint_xfer_isoc(&urb->ep->desc))
  921. size = urb->number_of_packets;
  922. else
  923. size = 1;
  924. urb_priv = kzalloc(sizeof(struct urb_priv) +
  925. size * sizeof(struct xhci_td *), mem_flags);
  926. if (!urb_priv)
  927. return -ENOMEM;
  928. buffer = kzalloc(size * sizeof(struct xhci_td), mem_flags);
  929. if (!buffer) {
  930. kfree(urb_priv);
  931. return -ENOMEM;
  932. }
  933. for (i = 0; i < size; i++) {
  934. urb_priv->td[i] = buffer;
  935. buffer++;
  936. }
  937. urb_priv->length = size;
  938. urb_priv->td_cnt = 0;
  939. urb->hcpriv = urb_priv;
  940. if (usb_endpoint_xfer_control(&urb->ep->desc)) {
  941. /* Check to see if the max packet size for the default control
  942. * endpoint changed during FS device enumeration
  943. */
  944. if (urb->dev->speed == USB_SPEED_FULL) {
  945. ret = xhci_check_maxpacket(xhci, slot_id,
  946. ep_index, urb);
  947. if (ret < 0) {
  948. xhci_urb_free_priv(xhci, urb_priv);
  949. urb->hcpriv = NULL;
  950. return ret;
  951. }
  952. }
  953. /* We have a spinlock and interrupts disabled, so we must pass
  954. * atomic context to this function, which may allocate memory.
  955. */
  956. spin_lock_irqsave(&xhci->lock, flags);
  957. if (xhci->xhc_state & XHCI_STATE_DYING)
  958. goto dying;
  959. ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
  960. slot_id, ep_index);
  961. if (ret)
  962. goto free_priv;
  963. spin_unlock_irqrestore(&xhci->lock, flags);
  964. } else if (usb_endpoint_xfer_bulk(&urb->ep->desc)) {
  965. spin_lock_irqsave(&xhci->lock, flags);
  966. if (xhci->xhc_state & XHCI_STATE_DYING)
  967. goto dying;
  968. if (xhci->devs[slot_id]->eps[ep_index].ep_state &
  969. EP_GETTING_STREAMS) {
  970. xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
  971. "is transitioning to using streams.\n");
  972. ret = -EINVAL;
  973. } else if (xhci->devs[slot_id]->eps[ep_index].ep_state &
  974. EP_GETTING_NO_STREAMS) {
  975. xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
  976. "is transitioning to "
  977. "not having streams.\n");
  978. ret = -EINVAL;
  979. } else {
  980. ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
  981. slot_id, ep_index);
  982. }
  983. if (ret)
  984. goto free_priv;
  985. spin_unlock_irqrestore(&xhci->lock, flags);
  986. } else if (usb_endpoint_xfer_int(&urb->ep->desc)) {
  987. spin_lock_irqsave(&xhci->lock, flags);
  988. if (xhci->xhc_state & XHCI_STATE_DYING)
  989. goto dying;
  990. ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
  991. slot_id, ep_index);
  992. if (ret)
  993. goto free_priv;
  994. spin_unlock_irqrestore(&xhci->lock, flags);
  995. } else {
  996. spin_lock_irqsave(&xhci->lock, flags);
  997. if (xhci->xhc_state & XHCI_STATE_DYING)
  998. goto dying;
  999. ret = xhci_queue_isoc_tx_prepare(xhci, GFP_ATOMIC, urb,
  1000. slot_id, ep_index);
  1001. if (ret)
  1002. goto free_priv;
  1003. spin_unlock_irqrestore(&xhci->lock, flags);
  1004. }
  1005. exit:
  1006. return ret;
  1007. dying:
  1008. xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for "
  1009. "non-responsive xHCI host.\n",
  1010. urb->ep->desc.bEndpointAddress, urb);
  1011. ret = -ESHUTDOWN;
  1012. free_priv:
  1013. xhci_urb_free_priv(xhci, urb_priv);
  1014. urb->hcpriv = NULL;
  1015. spin_unlock_irqrestore(&xhci->lock, flags);
  1016. return ret;
  1017. }
  1018. /* Get the right ring for the given URB.
  1019. * If the endpoint supports streams, boundary check the URB's stream ID.
  1020. * If the endpoint doesn't support streams, return the singular endpoint ring.
  1021. */
  1022. static struct xhci_ring *xhci_urb_to_transfer_ring(struct xhci_hcd *xhci,
  1023. struct urb *urb)
  1024. {
  1025. unsigned int slot_id;
  1026. unsigned int ep_index;
  1027. unsigned int stream_id;
  1028. struct xhci_virt_ep *ep;
  1029. slot_id = urb->dev->slot_id;
  1030. ep_index = xhci_get_endpoint_index(&urb->ep->desc);
  1031. stream_id = urb->stream_id;
  1032. ep = &xhci->devs[slot_id]->eps[ep_index];
  1033. /* Common case: no streams */
  1034. if (!(ep->ep_state & EP_HAS_STREAMS))
  1035. return ep->ring;
  1036. if (stream_id == 0) {
  1037. xhci_warn(xhci,
  1038. "WARN: Slot ID %u, ep index %u has streams, "
  1039. "but URB has no stream ID.\n",
  1040. slot_id, ep_index);
  1041. return NULL;
  1042. }
  1043. if (stream_id < ep->stream_info->num_streams)
  1044. return ep->stream_info->stream_rings[stream_id];
  1045. xhci_warn(xhci,
  1046. "WARN: Slot ID %u, ep index %u has "
  1047. "stream IDs 1 to %u allocated, "
  1048. "but stream ID %u is requested.\n",
  1049. slot_id, ep_index,
  1050. ep->stream_info->num_streams - 1,
  1051. stream_id);
  1052. return NULL;
  1053. }
  1054. /*
  1055. * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
  1056. * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
  1057. * should pick up where it left off in the TD, unless a Set Transfer Ring
  1058. * Dequeue Pointer is issued.
  1059. *
  1060. * The TRBs that make up the buffers for the canceled URB will be "removed" from
  1061. * the ring. Since the ring is a contiguous structure, they can't be physically
  1062. * removed. Instead, there are two options:
  1063. *
  1064. * 1) If the HC is in the middle of processing the URB to be canceled, we
  1065. * simply move the ring's dequeue pointer past those TRBs using the Set
  1066. * Transfer Ring Dequeue Pointer command. This will be the common case,
  1067. * when drivers timeout on the last submitted URB and attempt to cancel.
  1068. *
  1069. * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
  1070. * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
  1071. * HC will need to invalidate the any TRBs it has cached after the stop
  1072. * endpoint command, as noted in the xHCI 0.95 errata.
  1073. *
  1074. * 3) The TD may have completed by the time the Stop Endpoint Command
  1075. * completes, so software needs to handle that case too.
  1076. *
  1077. * This function should protect against the TD enqueueing code ringing the
  1078. * doorbell while this code is waiting for a Stop Endpoint command to complete.
  1079. * It also needs to account for multiple cancellations on happening at the same
  1080. * time for the same endpoint.
  1081. *
  1082. * Note that this function can be called in any context, or so says
  1083. * usb_hcd_unlink_urb()
  1084. */
  1085. int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  1086. {
  1087. unsigned long flags;
  1088. int ret, i;
  1089. u32 temp;
  1090. struct xhci_hcd *xhci;
  1091. struct urb_priv *urb_priv;
  1092. struct xhci_td *td;
  1093. unsigned int ep_index;
  1094. struct xhci_ring *ep_ring;
  1095. struct xhci_virt_ep *ep;
  1096. xhci = hcd_to_xhci(hcd);
  1097. spin_lock_irqsave(&xhci->lock, flags);
  1098. /* Make sure the URB hasn't completed or been unlinked already */
  1099. ret = usb_hcd_check_unlink_urb(hcd, urb, status);
  1100. if (ret || !urb->hcpriv)
  1101. goto done;
  1102. temp = xhci_readl(xhci, &xhci->op_regs->status);
  1103. if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_HALTED)) {
  1104. xhci_dbg(xhci, "HW died, freeing TD.\n");
  1105. urb_priv = urb->hcpriv;
  1106. for (i = urb_priv->td_cnt; i < urb_priv->length; i++) {
  1107. td = urb_priv->td[i];
  1108. if (!list_empty(&td->td_list))
  1109. list_del_init(&td->td_list);
  1110. if (!list_empty(&td->cancelled_td_list))
  1111. list_del_init(&td->cancelled_td_list);
  1112. }
  1113. usb_hcd_unlink_urb_from_ep(hcd, urb);
  1114. spin_unlock_irqrestore(&xhci->lock, flags);
  1115. usb_hcd_giveback_urb(hcd, urb, -ESHUTDOWN);
  1116. xhci_urb_free_priv(xhci, urb_priv);
  1117. return ret;
  1118. }
  1119. if ((xhci->xhc_state & XHCI_STATE_DYING) ||
  1120. (xhci->xhc_state & XHCI_STATE_HALTED)) {
  1121. xhci_dbg(xhci, "Ep 0x%x: URB %p to be canceled on "
  1122. "non-responsive xHCI host.\n",
  1123. urb->ep->desc.bEndpointAddress, urb);
  1124. /* Let the stop endpoint command watchdog timer (which set this
  1125. * state) finish cleaning up the endpoint TD lists. We must
  1126. * have caught it in the middle of dropping a lock and giving
  1127. * back an URB.
  1128. */
  1129. goto done;
  1130. }
  1131. xhci_dbg(xhci, "Cancel URB %p\n", urb);
  1132. xhci_dbg(xhci, "Event ring:\n");
  1133. xhci_debug_ring(xhci, xhci->event_ring);
  1134. ep_index = xhci_get_endpoint_index(&urb->ep->desc);
  1135. ep = &xhci->devs[urb->dev->slot_id]->eps[ep_index];
  1136. ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
  1137. if (!ep_ring) {
  1138. ret = -EINVAL;
  1139. goto done;
  1140. }
  1141. xhci_dbg(xhci, "Endpoint ring:\n");
  1142. xhci_debug_ring(xhci, ep_ring);
  1143. urb_priv = urb->hcpriv;
  1144. for (i = urb_priv->td_cnt; i < urb_priv->length; i++) {
  1145. td = urb_priv->td[i];
  1146. list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
  1147. }
  1148. /* Queue a stop endpoint command, but only if this is
  1149. * the first cancellation to be handled.
  1150. */
  1151. if (!(ep->ep_state & EP_HALT_PENDING)) {
  1152. ep->ep_state |= EP_HALT_PENDING;
  1153. ep->stop_cmds_pending++;
  1154. ep->stop_cmd_timer.expires = jiffies +
  1155. XHCI_STOP_EP_CMD_TIMEOUT * HZ;
  1156. add_timer(&ep->stop_cmd_timer);
  1157. xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index, 0);
  1158. xhci_ring_cmd_db(xhci);
  1159. }
  1160. done:
  1161. spin_unlock_irqrestore(&xhci->lock, flags);
  1162. return ret;
  1163. }
  1164. /* Drop an endpoint from a new bandwidth configuration for this device.
  1165. * Only one call to this function is allowed per endpoint before
  1166. * check_bandwidth() or reset_bandwidth() must be called.
  1167. * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
  1168. * add the endpoint to the schedule with possibly new parameters denoted by a
  1169. * different endpoint descriptor in usb_host_endpoint.
  1170. * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
  1171. * not allowed.
  1172. *
  1173. * The USB core will not allow URBs to be queued to an endpoint that is being
  1174. * disabled, so there's no need for mutual exclusion to protect
  1175. * the xhci->devs[slot_id] structure.
  1176. */
  1177. int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
  1178. struct usb_host_endpoint *ep)
  1179. {
  1180. struct xhci_hcd *xhci;
  1181. struct xhci_container_ctx *in_ctx, *out_ctx;
  1182. struct xhci_input_control_ctx *ctrl_ctx;
  1183. struct xhci_slot_ctx *slot_ctx;
  1184. unsigned int last_ctx;
  1185. unsigned int ep_index;
  1186. struct xhci_ep_ctx *ep_ctx;
  1187. u32 drop_flag;
  1188. u32 new_add_flags, new_drop_flags, new_slot_info;
  1189. int ret;
  1190. ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
  1191. if (ret <= 0)
  1192. return ret;
  1193. xhci = hcd_to_xhci(hcd);
  1194. if (xhci->xhc_state & XHCI_STATE_DYING)
  1195. return -ENODEV;
  1196. xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
  1197. drop_flag = xhci_get_endpoint_flag(&ep->desc);
  1198. if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
  1199. xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
  1200. __func__, drop_flag);
  1201. return 0;
  1202. }
  1203. in_ctx = xhci->devs[udev->slot_id]->in_ctx;
  1204. out_ctx = xhci->devs[udev->slot_id]->out_ctx;
  1205. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  1206. ep_index = xhci_get_endpoint_index(&ep->desc);
  1207. ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
  1208. /* If the HC already knows the endpoint is disabled,
  1209. * or the HCD has noted it is disabled, ignore this request
  1210. */
  1211. if (((ep_ctx->ep_info & cpu_to_le32(EP_STATE_MASK)) ==
  1212. cpu_to_le32(EP_STATE_DISABLED)) ||
  1213. le32_to_cpu(ctrl_ctx->drop_flags) &
  1214. xhci_get_endpoint_flag(&ep->desc)) {
  1215. xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
  1216. __func__, ep);
  1217. return 0;
  1218. }
  1219. ctrl_ctx->drop_flags |= cpu_to_le32(drop_flag);
  1220. new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
  1221. ctrl_ctx->add_flags &= cpu_to_le32(~drop_flag);
  1222. new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
  1223. last_ctx = xhci_last_valid_endpoint(le32_to_cpu(ctrl_ctx->add_flags));
  1224. slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
  1225. /* Update the last valid endpoint context, if we deleted the last one */
  1226. if ((le32_to_cpu(slot_ctx->dev_info) & LAST_CTX_MASK) >
  1227. LAST_CTX(last_ctx)) {
  1228. slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
  1229. slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(last_ctx));
  1230. }
  1231. new_slot_info = le32_to_cpu(slot_ctx->dev_info);
  1232. xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
  1233. xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
  1234. (unsigned int) ep->desc.bEndpointAddress,
  1235. udev->slot_id,
  1236. (unsigned int) new_drop_flags,
  1237. (unsigned int) new_add_flags,
  1238. (unsigned int) new_slot_info);
  1239. return 0;
  1240. }
  1241. /* Add an endpoint to a new possible bandwidth configuration for this device.
  1242. * Only one call to this function is allowed per endpoint before
  1243. * check_bandwidth() or reset_bandwidth() must be called.
  1244. * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
  1245. * add the endpoint to the schedule with possibly new parameters denoted by a
  1246. * different endpoint descriptor in usb_host_endpoint.
  1247. * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
  1248. * not allowed.
  1249. *
  1250. * The USB core will not allow URBs to be queued to an endpoint until the
  1251. * configuration or alt setting is installed in the device, so there's no need
  1252. * for mutual exclusion to protect the xhci->devs[slot_id] structure.
  1253. */
  1254. int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
  1255. struct usb_host_endpoint *ep)
  1256. {
  1257. struct xhci_hcd *xhci;
  1258. struct xhci_container_ctx *in_ctx, *out_ctx;
  1259. unsigned int ep_index;
  1260. struct xhci_ep_ctx *ep_ctx;
  1261. struct xhci_slot_ctx *slot_ctx;
  1262. struct xhci_input_control_ctx *ctrl_ctx;
  1263. u32 added_ctxs;
  1264. unsigned int last_ctx;
  1265. u32 new_add_flags, new_drop_flags, new_slot_info;
  1266. struct xhci_virt_device *virt_dev;
  1267. int ret = 0;
  1268. ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
  1269. if (ret <= 0) {
  1270. /* So we won't queue a reset ep command for a root hub */
  1271. ep->hcpriv = NULL;
  1272. return ret;
  1273. }
  1274. xhci = hcd_to_xhci(hcd);
  1275. if (xhci->xhc_state & XHCI_STATE_DYING)
  1276. return -ENODEV;
  1277. added_ctxs = xhci_get_endpoint_flag(&ep->desc);
  1278. last_ctx = xhci_last_valid_endpoint(added_ctxs);
  1279. if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
  1280. /* FIXME when we have to issue an evaluate endpoint command to
  1281. * deal with ep0 max packet size changing once we get the
  1282. * descriptors
  1283. */
  1284. xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
  1285. __func__, added_ctxs);
  1286. return 0;
  1287. }
  1288. virt_dev = xhci->devs[udev->slot_id];
  1289. in_ctx = virt_dev->in_ctx;
  1290. out_ctx = virt_dev->out_ctx;
  1291. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  1292. ep_index = xhci_get_endpoint_index(&ep->desc);
  1293. ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
  1294. /* If this endpoint is already in use, and the upper layers are trying
  1295. * to add it again without dropping it, reject the addition.
  1296. */
  1297. if (virt_dev->eps[ep_index].ring &&
  1298. !(le32_to_cpu(ctrl_ctx->drop_flags) &
  1299. xhci_get_endpoint_flag(&ep->desc))) {
  1300. xhci_warn(xhci, "Trying to add endpoint 0x%x "
  1301. "without dropping it.\n",
  1302. (unsigned int) ep->desc.bEndpointAddress);
  1303. return -EINVAL;
  1304. }
  1305. /* If the HCD has already noted the endpoint is enabled,
  1306. * ignore this request.
  1307. */
  1308. if (le32_to_cpu(ctrl_ctx->add_flags) &
  1309. xhci_get_endpoint_flag(&ep->desc)) {
  1310. xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
  1311. __func__, ep);
  1312. return 0;
  1313. }
  1314. /*
  1315. * Configuration and alternate setting changes must be done in
  1316. * process context, not interrupt context (or so documenation
  1317. * for usb_set_interface() and usb_set_configuration() claim).
  1318. */
  1319. if (xhci_endpoint_init(xhci, virt_dev, udev, ep, GFP_NOIO) < 0) {
  1320. dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
  1321. __func__, ep->desc.bEndpointAddress);
  1322. return -ENOMEM;
  1323. }
  1324. ctrl_ctx->add_flags |= cpu_to_le32(added_ctxs);
  1325. new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
  1326. /* If xhci_endpoint_disable() was called for this endpoint, but the
  1327. * xHC hasn't been notified yet through the check_bandwidth() call,
  1328. * this re-adds a new state for the endpoint from the new endpoint
  1329. * descriptors. We must drop and re-add this endpoint, so we leave the
  1330. * drop flags alone.
  1331. */
  1332. new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
  1333. slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
  1334. /* Update the last valid endpoint context, if we just added one past */
  1335. if ((le32_to_cpu(slot_ctx->dev_info) & LAST_CTX_MASK) <
  1336. LAST_CTX(last_ctx)) {
  1337. slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
  1338. slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(last_ctx));
  1339. }
  1340. new_slot_info = le32_to_cpu(slot_ctx->dev_info);
  1341. /* Store the usb_device pointer for later use */
  1342. ep->hcpriv = udev;
  1343. xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
  1344. (unsigned int) ep->desc.bEndpointAddress,
  1345. udev->slot_id,
  1346. (unsigned int) new_drop_flags,
  1347. (unsigned int) new_add_flags,
  1348. (unsigned int) new_slot_info);
  1349. return 0;
  1350. }
  1351. static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev)
  1352. {
  1353. struct xhci_input_control_ctx *ctrl_ctx;
  1354. struct xhci_ep_ctx *ep_ctx;
  1355. struct xhci_slot_ctx *slot_ctx;
  1356. int i;
  1357. /* When a device's add flag and drop flag are zero, any subsequent
  1358. * configure endpoint command will leave that endpoint's state
  1359. * untouched. Make sure we don't leave any old state in the input
  1360. * endpoint contexts.
  1361. */
  1362. ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
  1363. ctrl_ctx->drop_flags = 0;
  1364. ctrl_ctx->add_flags = 0;
  1365. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
  1366. slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
  1367. /* Endpoint 0 is always valid */
  1368. slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
  1369. for (i = 1; i < 31; ++i) {
  1370. ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
  1371. ep_ctx->ep_info = 0;
  1372. ep_ctx->ep_info2 = 0;
  1373. ep_ctx->deq = 0;
  1374. ep_ctx->tx_info = 0;
  1375. }
  1376. }
  1377. static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
  1378. struct usb_device *udev, u32 *cmd_status)
  1379. {
  1380. int ret;
  1381. switch (*cmd_status) {
  1382. case COMP_ENOMEM:
  1383. dev_warn(&udev->dev, "Not enough host controller resources "
  1384. "for new device state.\n");
  1385. ret = -ENOMEM;
  1386. /* FIXME: can we allocate more resources for the HC? */
  1387. break;
  1388. case COMP_BW_ERR:
  1389. dev_warn(&udev->dev, "Not enough bandwidth "
  1390. "for new device state.\n");
  1391. ret = -ENOSPC;
  1392. /* FIXME: can we go back to the old state? */
  1393. break;
  1394. case COMP_TRB_ERR:
  1395. /* the HCD set up something wrong */
  1396. dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
  1397. "add flag = 1, "
  1398. "and endpoint is not disabled.\n");
  1399. ret = -EINVAL;
  1400. break;
  1401. case COMP_DEV_ERR:
  1402. dev_warn(&udev->dev, "ERROR: Incompatible device for endpoint "
  1403. "configure command.\n");
  1404. ret = -ENODEV;
  1405. break;
  1406. case COMP_SUCCESS:
  1407. dev_dbg(&udev->dev, "Successful Endpoint Configure command\n");
  1408. ret = 0;
  1409. break;
  1410. default:
  1411. xhci_err(xhci, "ERROR: unexpected command completion "
  1412. "code 0x%x.\n", *cmd_status);
  1413. ret = -EINVAL;
  1414. break;
  1415. }
  1416. return ret;
  1417. }
  1418. static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
  1419. struct usb_device *udev, u32 *cmd_status)
  1420. {
  1421. int ret;
  1422. struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
  1423. switch (*cmd_status) {
  1424. case COMP_EINVAL:
  1425. dev_warn(&udev->dev, "WARN: xHCI driver setup invalid evaluate "
  1426. "context command.\n");
  1427. ret = -EINVAL;
  1428. break;
  1429. case COMP_EBADSLT:
  1430. dev_warn(&udev->dev, "WARN: slot not enabled for"
  1431. "evaluate context command.\n");
  1432. case COMP_CTX_STATE:
  1433. dev_warn(&udev->dev, "WARN: invalid context state for "
  1434. "evaluate context command.\n");
  1435. xhci_dbg_ctx(xhci, virt_dev->out_ctx, 1);
  1436. ret = -EINVAL;
  1437. break;
  1438. case COMP_DEV_ERR:
  1439. dev_warn(&udev->dev, "ERROR: Incompatible device for evaluate "
  1440. "context command.\n");
  1441. ret = -ENODEV;
  1442. break;
  1443. case COMP_MEL_ERR:
  1444. /* Max Exit Latency too large error */
  1445. dev_warn(&udev->dev, "WARN: Max Exit Latency too large\n");
  1446. ret = -EINVAL;
  1447. break;
  1448. case COMP_SUCCESS:
  1449. dev_dbg(&udev->dev, "Successful evaluate context command\n");
  1450. ret = 0;
  1451. break;
  1452. default:
  1453. xhci_err(xhci, "ERROR: unexpected command completion "
  1454. "code 0x%x.\n", *cmd_status);
  1455. ret = -EINVAL;
  1456. break;
  1457. }
  1458. return ret;
  1459. }
  1460. static u32 xhci_count_num_new_endpoints(struct xhci_hcd *xhci,
  1461. struct xhci_container_ctx *in_ctx)
  1462. {
  1463. struct xhci_input_control_ctx *ctrl_ctx;
  1464. u32 valid_add_flags;
  1465. u32 valid_drop_flags;
  1466. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  1467. /* Ignore the slot flag (bit 0), and the default control endpoint flag
  1468. * (bit 1). The default control endpoint is added during the Address
  1469. * Device command and is never removed until the slot is disabled.
  1470. */
  1471. valid_add_flags = ctrl_ctx->add_flags >> 2;
  1472. valid_drop_flags = ctrl_ctx->drop_flags >> 2;
  1473. /* Use hweight32 to count the number of ones in the add flags, or
  1474. * number of endpoints added. Don't count endpoints that are changed
  1475. * (both added and dropped).
  1476. */
  1477. return hweight32(valid_add_flags) -
  1478. hweight32(valid_add_flags & valid_drop_flags);
  1479. }
  1480. static unsigned int xhci_count_num_dropped_endpoints(struct xhci_hcd *xhci,
  1481. struct xhci_container_ctx *in_ctx)
  1482. {
  1483. struct xhci_input_control_ctx *ctrl_ctx;
  1484. u32 valid_add_flags;
  1485. u32 valid_drop_flags;
  1486. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  1487. valid_add_flags = ctrl_ctx->add_flags >> 2;
  1488. valid_drop_flags = ctrl_ctx->drop_flags >> 2;
  1489. return hweight32(valid_drop_flags) -
  1490. hweight32(valid_add_flags & valid_drop_flags);
  1491. }
  1492. /*
  1493. * We need to reserve the new number of endpoints before the configure endpoint
  1494. * command completes. We can't subtract the dropped endpoints from the number
  1495. * of active endpoints until the command completes because we can oversubscribe
  1496. * the host in this case:
  1497. *
  1498. * - the first configure endpoint command drops more endpoints than it adds
  1499. * - a second configure endpoint command that adds more endpoints is queued
  1500. * - the first configure endpoint command fails, so the config is unchanged
  1501. * - the second command may succeed, even though there isn't enough resources
  1502. *
  1503. * Must be called with xhci->lock held.
  1504. */
  1505. static int xhci_reserve_host_resources(struct xhci_hcd *xhci,
  1506. struct xhci_container_ctx *in_ctx)
  1507. {
  1508. u32 added_eps;
  1509. added_eps = xhci_count_num_new_endpoints(xhci, in_ctx);
  1510. if (xhci->num_active_eps + added_eps > xhci->limit_active_eps) {
  1511. xhci_dbg(xhci, "Not enough ep ctxs: "
  1512. "%u active, need to add %u, limit is %u.\n",
  1513. xhci->num_active_eps, added_eps,
  1514. xhci->limit_active_eps);
  1515. return -ENOMEM;
  1516. }
  1517. xhci->num_active_eps += added_eps;
  1518. xhci_dbg(xhci, "Adding %u ep ctxs, %u now active.\n", added_eps,
  1519. xhci->num_active_eps);
  1520. return 0;
  1521. }
  1522. /*
  1523. * The configure endpoint was failed by the xHC for some other reason, so we
  1524. * need to revert the resources that failed configuration would have used.
  1525. *
  1526. * Must be called with xhci->lock held.
  1527. */
  1528. static void xhci_free_host_resources(struct xhci_hcd *xhci,
  1529. struct xhci_container_ctx *in_ctx)
  1530. {
  1531. u32 num_failed_eps;
  1532. num_failed_eps = xhci_count_num_new_endpoints(xhci, in_ctx);
  1533. xhci->num_active_eps -= num_failed_eps;
  1534. xhci_dbg(xhci, "Removing %u failed ep ctxs, %u now active.\n",
  1535. num_failed_eps,
  1536. xhci->num_active_eps);
  1537. }
  1538. /*
  1539. * Now that the command has completed, clean up the active endpoint count by
  1540. * subtracting out the endpoints that were dropped (but not changed).
  1541. *
  1542. * Must be called with xhci->lock held.
  1543. */
  1544. static void xhci_finish_resource_reservation(struct xhci_hcd *xhci,
  1545. struct xhci_container_ctx *in_ctx)
  1546. {
  1547. u32 num_dropped_eps;
  1548. num_dropped_eps = xhci_count_num_dropped_endpoints(xhci, in_ctx);
  1549. xhci->num_active_eps -= num_dropped_eps;
  1550. if (num_dropped_eps)
  1551. xhci_dbg(xhci, "Removing %u dropped ep ctxs, %u now active.\n",
  1552. num_dropped_eps,
  1553. xhci->num_active_eps);
  1554. }
  1555. unsigned int xhci_get_block_size(struct usb_device *udev)
  1556. {
  1557. switch (udev->speed) {
  1558. case USB_SPEED_LOW:
  1559. case USB_SPEED_FULL:
  1560. return FS_BLOCK;
  1561. case USB_SPEED_HIGH:
  1562. return HS_BLOCK;
  1563. case USB_SPEED_SUPER:
  1564. return SS_BLOCK;
  1565. case USB_SPEED_UNKNOWN:
  1566. case USB_SPEED_WIRELESS:
  1567. default:
  1568. /* Should never happen */
  1569. return 1;
  1570. }
  1571. }
  1572. unsigned int xhci_get_largest_overhead(struct xhci_interval_bw *interval_bw)
  1573. {
  1574. if (interval_bw->overhead[LS_OVERHEAD_TYPE])
  1575. return LS_OVERHEAD;
  1576. if (interval_bw->overhead[FS_OVERHEAD_TYPE])
  1577. return FS_OVERHEAD;
  1578. return HS_OVERHEAD;
  1579. }
  1580. /* If we are changing a LS/FS device under a HS hub,
  1581. * make sure (if we are activating a new TT) that the HS bus has enough
  1582. * bandwidth for this new TT.
  1583. */
  1584. static int xhci_check_tt_bw_table(struct xhci_hcd *xhci,
  1585. struct xhci_virt_device *virt_dev,
  1586. int old_active_eps)
  1587. {
  1588. struct xhci_interval_bw_table *bw_table;
  1589. struct xhci_tt_bw_info *tt_info;
  1590. /* Find the bandwidth table for the root port this TT is attached to. */
  1591. bw_table = &xhci->rh_bw[virt_dev->real_port - 1].bw_table;
  1592. tt_info = virt_dev->tt_info;
  1593. /* If this TT already had active endpoints, the bandwidth for this TT
  1594. * has already been added. Removing all periodic endpoints (and thus
  1595. * making the TT enactive) will only decrease the bandwidth used.
  1596. */
  1597. if (old_active_eps)
  1598. return 0;
  1599. if (old_active_eps == 0 && tt_info->active_eps != 0) {
  1600. if (bw_table->bw_used + TT_HS_OVERHEAD > HS_BW_LIMIT)
  1601. return -ENOMEM;
  1602. return 0;
  1603. }
  1604. /* Not sure why we would have no new active endpoints...
  1605. *
  1606. * Maybe because of an Evaluate Context change for a hub update or a
  1607. * control endpoint 0 max packet size change?
  1608. * FIXME: skip the bandwidth calculation in that case.
  1609. */
  1610. return 0;
  1611. }
  1612. /*
  1613. * This algorithm is a very conservative estimate of the worst-case scheduling
  1614. * scenario for any one interval. The hardware dynamically schedules the
  1615. * packets, so we can't tell which microframe could be the limiting factor in
  1616. * the bandwidth scheduling. This only takes into account periodic endpoints.
  1617. *
  1618. * Obviously, we can't solve an NP complete problem to find the minimum worst
  1619. * case scenario. Instead, we come up with an estimate that is no less than
  1620. * the worst case bandwidth used for any one microframe, but may be an
  1621. * over-estimate.
  1622. *
  1623. * We walk the requirements for each endpoint by interval, starting with the
  1624. * smallest interval, and place packets in the schedule where there is only one
  1625. * possible way to schedule packets for that interval. In order to simplify
  1626. * this algorithm, we record the largest max packet size for each interval, and
  1627. * assume all packets will be that size.
  1628. *
  1629. * For interval 0, we obviously must schedule all packets for each interval.
  1630. * The bandwidth for interval 0 is just the amount of data to be transmitted
  1631. * (the sum of all max ESIT payload sizes, plus any overhead per packet times
  1632. * the number of packets).
  1633. *
  1634. * For interval 1, we have two possible microframes to schedule those packets
  1635. * in. For this algorithm, if we can schedule the same number of packets for
  1636. * each possible scheduling opportunity (each microframe), we will do so. The
  1637. * remaining number of packets will be saved to be transmitted in the gaps in
  1638. * the next interval's scheduling sequence.
  1639. *
  1640. * As we move those remaining packets to be scheduled with interval 2 packets,
  1641. * we have to double the number of remaining packets to transmit. This is
  1642. * because the intervals are actually powers of 2, and we would be transmitting
  1643. * the previous interval's packets twice in this interval. We also have to be
  1644. * sure that when we look at the largest max packet size for this interval, we
  1645. * also look at the largest max packet size for the remaining packets and take
  1646. * the greater of the two.
  1647. *
  1648. * The algorithm continues to evenly distribute packets in each scheduling
  1649. * opportunity, and push the remaining packets out, until we get to the last
  1650. * interval. Then those packets and their associated overhead are just added
  1651. * to the bandwidth used.
  1652. */
  1653. static int xhci_check_bw_table(struct xhci_hcd *xhci,
  1654. struct xhci_virt_device *virt_dev,
  1655. int old_active_eps)
  1656. {
  1657. unsigned int bw_reserved;
  1658. unsigned int max_bandwidth;
  1659. unsigned int bw_used;
  1660. unsigned int block_size;
  1661. struct xhci_interval_bw_table *bw_table;
  1662. unsigned int packet_size = 0;
  1663. unsigned int overhead = 0;
  1664. unsigned int packets_transmitted = 0;
  1665. unsigned int packets_remaining = 0;
  1666. unsigned int i;
  1667. if (virt_dev->udev->speed == USB_SPEED_HIGH) {
  1668. max_bandwidth = HS_BW_LIMIT;
  1669. /* Convert percent of bus BW reserved to blocks reserved */
  1670. bw_reserved = DIV_ROUND_UP(HS_BW_RESERVED * max_bandwidth, 100);
  1671. } else {
  1672. max_bandwidth = FS_BW_LIMIT;
  1673. bw_reserved = DIV_ROUND_UP(FS_BW_RESERVED * max_bandwidth, 100);
  1674. }
  1675. bw_table = virt_dev->bw_table;
  1676. /* We need to translate the max packet size and max ESIT payloads into
  1677. * the units the hardware uses.
  1678. */
  1679. block_size = xhci_get_block_size(virt_dev->udev);
  1680. /* If we are manipulating a LS/FS device under a HS hub, double check
  1681. * that the HS bus has enough bandwidth if we are activing a new TT.
  1682. */
  1683. if (virt_dev->tt_info) {
  1684. xhci_dbg(xhci, "Recalculating BW for rootport %u\n",
  1685. virt_dev->real_port);
  1686. if (xhci_check_tt_bw_table(xhci, virt_dev, old_active_eps)) {
  1687. xhci_warn(xhci, "Not enough bandwidth on HS bus for "
  1688. "newly activated TT.\n");
  1689. return -ENOMEM;
  1690. }
  1691. xhci_dbg(xhci, "Recalculating BW for TT slot %u port %u\n",
  1692. virt_dev->tt_info->slot_id,
  1693. virt_dev->tt_info->ttport);
  1694. } else {
  1695. xhci_dbg(xhci, "Recalculating BW for rootport %u\n",
  1696. virt_dev->real_port);
  1697. }
  1698. /* Add in how much bandwidth will be used for interval zero, or the
  1699. * rounded max ESIT payload + number of packets * largest overhead.
  1700. */
  1701. bw_used = DIV_ROUND_UP(bw_table->interval0_esit_payload, block_size) +
  1702. bw_table->interval_bw[0].num_packets *
  1703. xhci_get_largest_overhead(&bw_table->interval_bw[0]);
  1704. for (i = 1; i < XHCI_MAX_INTERVAL; i++) {
  1705. unsigned int bw_added;
  1706. unsigned int largest_mps;
  1707. unsigned int interval_overhead;
  1708. /*
  1709. * How many packets could we transmit in this interval?
  1710. * If packets didn't fit in the previous interval, we will need
  1711. * to transmit that many packets twice within this interval.
  1712. */
  1713. packets_remaining = 2 * packets_remaining +
  1714. bw_table->interval_bw[i].num_packets;
  1715. /* Find the largest max packet size of this or the previous
  1716. * interval.
  1717. */
  1718. if (list_empty(&bw_table->interval_bw[i].endpoints))
  1719. largest_mps = 0;
  1720. else {
  1721. struct xhci_virt_ep *virt_ep;
  1722. struct list_head *ep_entry;
  1723. ep_entry = bw_table->interval_bw[i].endpoints.next;
  1724. virt_ep = list_entry(ep_entry,
  1725. struct xhci_virt_ep, bw_endpoint_list);
  1726. /* Convert to blocks, rounding up */
  1727. largest_mps = DIV_ROUND_UP(
  1728. virt_ep->bw_info.max_packet_size,
  1729. block_size);
  1730. }
  1731. if (largest_mps > packet_size)
  1732. packet_size = largest_mps;
  1733. /* Use the larger overhead of this or the previous interval. */
  1734. interval_overhead = xhci_get_largest_overhead(
  1735. &bw_table->interval_bw[i]);
  1736. if (interval_overhead > overhead)
  1737. overhead = interval_overhead;
  1738. /* How many packets can we evenly distribute across
  1739. * (1 << (i + 1)) possible scheduling opportunities?
  1740. */
  1741. packets_transmitted = packets_remaining >> (i + 1);
  1742. /* Add in the bandwidth used for those scheduled packets */
  1743. bw_added = packets_transmitted * (overhead + packet_size);
  1744. /* How many packets do we have remaining to transmit? */
  1745. packets_remaining = packets_remaining % (1 << (i + 1));
  1746. /* What largest max packet size should those packets have? */
  1747. /* If we've transmitted all packets, don't carry over the
  1748. * largest packet size.
  1749. */
  1750. if (packets_remaining == 0) {
  1751. packet_size = 0;
  1752. overhead = 0;
  1753. } else if (packets_transmitted > 0) {
  1754. /* Otherwise if we do have remaining packets, and we've
  1755. * scheduled some packets in this interval, take the
  1756. * largest max packet size from endpoints with this
  1757. * interval.
  1758. */
  1759. packet_size = largest_mps;
  1760. overhead = interval_overhead;
  1761. }
  1762. /* Otherwise carry over packet_size and overhead from the last
  1763. * time we had a remainder.
  1764. */
  1765. bw_used += bw_added;
  1766. if (bw_used > max_bandwidth) {
  1767. xhci_warn(xhci, "Not enough bandwidth. "
  1768. "Proposed: %u, Max: %u\n",
  1769. bw_used, max_bandwidth);
  1770. return -ENOMEM;
  1771. }
  1772. }
  1773. /*
  1774. * Ok, we know we have some packets left over after even-handedly
  1775. * scheduling interval 15. We don't know which microframes they will
  1776. * fit into, so we over-schedule and say they will be scheduled every
  1777. * microframe.
  1778. */
  1779. if (packets_remaining > 0)
  1780. bw_used += overhead + packet_size;
  1781. if (!virt_dev->tt_info && virt_dev->udev->speed == USB_SPEED_HIGH) {
  1782. unsigned int port_index = virt_dev->real_port - 1;
  1783. /* OK, we're manipulating a HS device attached to a
  1784. * root port bandwidth domain. Include the number of active TTs
  1785. * in the bandwidth used.
  1786. */
  1787. bw_used += TT_HS_OVERHEAD *
  1788. xhci->rh_bw[port_index].num_active_tts;
  1789. }
  1790. xhci_dbg(xhci, "Final bandwidth: %u, Limit: %u, Reserved: %u, "
  1791. "Available: %u " "percent\n",
  1792. bw_used, max_bandwidth, bw_reserved,
  1793. (max_bandwidth - bw_used - bw_reserved) * 100 /
  1794. max_bandwidth);
  1795. bw_used += bw_reserved;
  1796. if (bw_used > max_bandwidth) {
  1797. xhci_warn(xhci, "Not enough bandwidth. Proposed: %u, Max: %u\n",
  1798. bw_used, max_bandwidth);
  1799. return -ENOMEM;
  1800. }
  1801. bw_table->bw_used = bw_used;
  1802. return 0;
  1803. }
  1804. static bool xhci_is_async_ep(unsigned int ep_type)
  1805. {
  1806. return (ep_type != ISOC_OUT_EP && ep_type != INT_OUT_EP &&
  1807. ep_type != ISOC_IN_EP &&
  1808. ep_type != INT_IN_EP);
  1809. }
  1810. void xhci_drop_ep_from_interval_table(struct xhci_hcd *xhci,
  1811. struct xhci_bw_info *ep_bw,
  1812. struct xhci_interval_bw_table *bw_table,
  1813. struct usb_device *udev,
  1814. struct xhci_virt_ep *virt_ep,
  1815. struct xhci_tt_bw_info *tt_info)
  1816. {
  1817. struct xhci_interval_bw *interval_bw;
  1818. int normalized_interval;
  1819. if (xhci_is_async_ep(ep_bw->type) ||
  1820. list_empty(&virt_ep->bw_endpoint_list))
  1821. return;
  1822. /* For LS/FS devices, we need to translate the interval expressed in
  1823. * microframes to frames.
  1824. */
  1825. if (udev->speed == USB_SPEED_HIGH)
  1826. normalized_interval = ep_bw->ep_interval;
  1827. else
  1828. normalized_interval = ep_bw->ep_interval - 3;
  1829. if (normalized_interval == 0)
  1830. bw_table->interval0_esit_payload -= ep_bw->max_esit_payload;
  1831. interval_bw = &bw_table->interval_bw[normalized_interval];
  1832. interval_bw->num_packets -= ep_bw->num_packets;
  1833. switch (udev->speed) {
  1834. case USB_SPEED_LOW:
  1835. interval_bw->overhead[LS_OVERHEAD_TYPE] -= 1;
  1836. break;
  1837. case USB_SPEED_FULL:
  1838. interval_bw->overhead[FS_OVERHEAD_TYPE] -= 1;
  1839. break;
  1840. case USB_SPEED_HIGH:
  1841. interval_bw->overhead[HS_OVERHEAD_TYPE] -= 1;
  1842. break;
  1843. case USB_SPEED_SUPER:
  1844. case USB_SPEED_UNKNOWN:
  1845. case USB_SPEED_WIRELESS:
  1846. /* Should never happen because only LS/FS/HS endpoints will get
  1847. * added to the endpoint list.
  1848. */
  1849. return;
  1850. }
  1851. if (tt_info)
  1852. tt_info->active_eps -= 1;
  1853. list_del_init(&virt_ep->bw_endpoint_list);
  1854. }
  1855. static void xhci_add_ep_to_interval_table(struct xhci_hcd *xhci,
  1856. struct xhci_bw_info *ep_bw,
  1857. struct xhci_interval_bw_table *bw_table,
  1858. struct usb_device *udev,
  1859. struct xhci_virt_ep *virt_ep,
  1860. struct xhci_tt_bw_info *tt_info)
  1861. {
  1862. struct xhci_interval_bw *interval_bw;
  1863. struct xhci_virt_ep *smaller_ep;
  1864. int normalized_interval;
  1865. if (xhci_is_async_ep(ep_bw->type))
  1866. return;
  1867. /* For LS/FS devices, we need to translate the interval expressed in
  1868. * microframes to frames.
  1869. */
  1870. if (udev->speed == USB_SPEED_HIGH)
  1871. normalized_interval = ep_bw->ep_interval;
  1872. else
  1873. normalized_interval = ep_bw->ep_interval - 3;
  1874. if (normalized_interval == 0)
  1875. bw_table->interval0_esit_payload += ep_bw->max_esit_payload;
  1876. interval_bw = &bw_table->interval_bw[normalized_interval];
  1877. interval_bw->num_packets += ep_bw->num_packets;
  1878. switch (udev->speed) {
  1879. case USB_SPEED_LOW:
  1880. interval_bw->overhead[LS_OVERHEAD_TYPE] += 1;
  1881. break;
  1882. case USB_SPEED_FULL:
  1883. interval_bw->overhead[FS_OVERHEAD_TYPE] += 1;
  1884. break;
  1885. case USB_SPEED_HIGH:
  1886. interval_bw->overhead[HS_OVERHEAD_TYPE] += 1;
  1887. break;
  1888. case USB_SPEED_SUPER:
  1889. case USB_SPEED_UNKNOWN:
  1890. case USB_SPEED_WIRELESS:
  1891. /* Should never happen because only LS/FS/HS endpoints will get
  1892. * added to the endpoint list.
  1893. */
  1894. return;
  1895. }
  1896. if (tt_info)
  1897. tt_info->active_eps += 1;
  1898. /* Insert the endpoint into the list, largest max packet size first. */
  1899. list_for_each_entry(smaller_ep, &interval_bw->endpoints,
  1900. bw_endpoint_list) {
  1901. if (ep_bw->max_packet_size >=
  1902. smaller_ep->bw_info.max_packet_size) {
  1903. /* Add the new ep before the smaller endpoint */
  1904. list_add_tail(&virt_ep->bw_endpoint_list,
  1905. &smaller_ep->bw_endpoint_list);
  1906. return;
  1907. }
  1908. }
  1909. /* Add the new endpoint at the end of the list. */
  1910. list_add_tail(&virt_ep->bw_endpoint_list,
  1911. &interval_bw->endpoints);
  1912. }
  1913. void xhci_update_tt_active_eps(struct xhci_hcd *xhci,
  1914. struct xhci_virt_device *virt_dev,
  1915. int old_active_eps)
  1916. {
  1917. struct xhci_root_port_bw_info *rh_bw_info;
  1918. if (!virt_dev->tt_info)
  1919. return;
  1920. rh_bw_info = &xhci->rh_bw[virt_dev->real_port - 1];
  1921. if (old_active_eps == 0 &&
  1922. virt_dev->tt_info->active_eps != 0) {
  1923. rh_bw_info->num_active_tts += 1;
  1924. rh_bw_info->bw_table.bw_used += TT_HS_OVERHEAD;
  1925. } else if (old_active_eps != 0 &&
  1926. virt_dev->tt_info->active_eps == 0) {
  1927. rh_bw_info->num_active_tts -= 1;
  1928. rh_bw_info->bw_table.bw_used -= TT_HS_OVERHEAD;
  1929. }
  1930. }
  1931. static int xhci_reserve_bandwidth(struct xhci_hcd *xhci,
  1932. struct xhci_virt_device *virt_dev,
  1933. struct xhci_container_ctx *in_ctx)
  1934. {
  1935. struct xhci_bw_info ep_bw_info[31];
  1936. int i;
  1937. struct xhci_input_control_ctx *ctrl_ctx;
  1938. int old_active_eps = 0;
  1939. if (virt_dev->udev->speed == USB_SPEED_SUPER)
  1940. return 0;
  1941. if (virt_dev->tt_info)
  1942. old_active_eps = virt_dev->tt_info->active_eps;
  1943. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  1944. for (i = 0; i < 31; i++) {
  1945. if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
  1946. continue;
  1947. /* Make a copy of the BW info in case we need to revert this */
  1948. memcpy(&ep_bw_info[i], &virt_dev->eps[i].bw_info,
  1949. sizeof(ep_bw_info[i]));
  1950. /* Drop the endpoint from the interval table if the endpoint is
  1951. * being dropped or changed.
  1952. */
  1953. if (EP_IS_DROPPED(ctrl_ctx, i))
  1954. xhci_drop_ep_from_interval_table(xhci,
  1955. &virt_dev->eps[i].bw_info,
  1956. virt_dev->bw_table,
  1957. virt_dev->udev,
  1958. &virt_dev->eps[i],
  1959. virt_dev->tt_info);
  1960. }
  1961. /* Overwrite the information stored in the endpoints' bw_info */
  1962. xhci_update_bw_info(xhci, virt_dev->in_ctx, ctrl_ctx, virt_dev);
  1963. for (i = 0; i < 31; i++) {
  1964. /* Add any changed or added endpoints to the interval table */
  1965. if (EP_IS_ADDED(ctrl_ctx, i))
  1966. xhci_add_ep_to_interval_table(xhci,
  1967. &virt_dev->eps[i].bw_info,
  1968. virt_dev->bw_table,
  1969. virt_dev->udev,
  1970. &virt_dev->eps[i],
  1971. virt_dev->tt_info);
  1972. }
  1973. if (!xhci_check_bw_table(xhci, virt_dev, old_active_eps)) {
  1974. /* Ok, this fits in the bandwidth we have.
  1975. * Update the number of active TTs.
  1976. */
  1977. xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
  1978. return 0;
  1979. }
  1980. /* We don't have enough bandwidth for this, revert the stored info. */
  1981. for (i = 0; i < 31; i++) {
  1982. if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
  1983. continue;
  1984. /* Drop the new copies of any added or changed endpoints from
  1985. * the interval table.
  1986. */
  1987. if (EP_IS_ADDED(ctrl_ctx, i)) {
  1988. xhci_drop_ep_from_interval_table(xhci,
  1989. &virt_dev->eps[i].bw_info,
  1990. virt_dev->bw_table,
  1991. virt_dev->udev,
  1992. &virt_dev->eps[i],
  1993. virt_dev->tt_info);
  1994. }
  1995. /* Revert the endpoint back to its old information */
  1996. memcpy(&virt_dev->eps[i].bw_info, &ep_bw_info[i],
  1997. sizeof(ep_bw_info[i]));
  1998. /* Add any changed or dropped endpoints back into the table */
  1999. if (EP_IS_DROPPED(ctrl_ctx, i))
  2000. xhci_add_ep_to_interval_table(xhci,
  2001. &virt_dev->eps[i].bw_info,
  2002. virt_dev->bw_table,
  2003. virt_dev->udev,
  2004. &virt_dev->eps[i],
  2005. virt_dev->tt_info);
  2006. }
  2007. return -ENOMEM;
  2008. }
  2009. /* Issue a configure endpoint command or evaluate context command
  2010. * and wait for it to finish.
  2011. */
  2012. static int xhci_configure_endpoint(struct xhci_hcd *xhci,
  2013. struct usb_device *udev,
  2014. struct xhci_command *command,
  2015. bool ctx_change, bool must_succeed)
  2016. {
  2017. int ret;
  2018. int timeleft;
  2019. unsigned long flags;
  2020. struct xhci_container_ctx *in_ctx;
  2021. struct completion *cmd_completion;
  2022. u32 *cmd_status;
  2023. struct xhci_virt_device *virt_dev;
  2024. spin_lock_irqsave(&xhci->lock, flags);
  2025. virt_dev = xhci->devs[udev->slot_id];
  2026. if (command)
  2027. in_ctx = command->in_ctx;
  2028. else
  2029. in_ctx = virt_dev->in_ctx;
  2030. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK) &&
  2031. xhci_reserve_host_resources(xhci, in_ctx)) {
  2032. spin_unlock_irqrestore(&xhci->lock, flags);
  2033. xhci_warn(xhci, "Not enough host resources, "
  2034. "active endpoint contexts = %u\n",
  2035. xhci->num_active_eps);
  2036. return -ENOMEM;
  2037. }
  2038. if ((xhci->quirks & XHCI_SW_BW_CHECKING) &&
  2039. xhci_reserve_bandwidth(xhci, virt_dev, in_ctx)) {
  2040. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
  2041. xhci_free_host_resources(xhci, in_ctx);
  2042. spin_unlock_irqrestore(&xhci->lock, flags);
  2043. xhci_warn(xhci, "Not enough bandwidth\n");
  2044. return -ENOMEM;
  2045. }
  2046. if (command) {
  2047. cmd_completion = command->completion;
  2048. cmd_status = &command->status;
  2049. command->command_trb = xhci->cmd_ring->enqueue;
  2050. /* Enqueue pointer can be left pointing to the link TRB,
  2051. * we must handle that
  2052. */
  2053. if (TRB_TYPE_LINK_LE32(command->command_trb->link.control))
  2054. command->command_trb =
  2055. xhci->cmd_ring->enq_seg->next->trbs;
  2056. list_add_tail(&command->cmd_list, &virt_dev->cmd_list);
  2057. } else {
  2058. cmd_completion = &virt_dev->cmd_completion;
  2059. cmd_status = &virt_dev->cmd_status;
  2060. }
  2061. init_completion(cmd_completion);
  2062. if (!ctx_change)
  2063. ret = xhci_queue_configure_endpoint(xhci, in_ctx->dma,
  2064. udev->slot_id, must_succeed);
  2065. else
  2066. ret = xhci_queue_evaluate_context(xhci, in_ctx->dma,
  2067. udev->slot_id);
  2068. if (ret < 0) {
  2069. if (command)
  2070. list_del(&command->cmd_list);
  2071. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
  2072. xhci_free_host_resources(xhci, in_ctx);
  2073. spin_unlock_irqrestore(&xhci->lock, flags);
  2074. xhci_dbg(xhci, "FIXME allocate a new ring segment\n");
  2075. return -ENOMEM;
  2076. }
  2077. xhci_ring_cmd_db(xhci);
  2078. spin_unlock_irqrestore(&xhci->lock, flags);
  2079. /* Wait for the configure endpoint command to complete */
  2080. timeleft = wait_for_completion_interruptible_timeout(
  2081. cmd_completion,
  2082. USB_CTRL_SET_TIMEOUT);
  2083. if (timeleft <= 0) {
  2084. xhci_warn(xhci, "%s while waiting for %s command\n",
  2085. timeleft == 0 ? "Timeout" : "Signal",
  2086. ctx_change == 0 ?
  2087. "configure endpoint" :
  2088. "evaluate context");
  2089. /* FIXME cancel the configure endpoint command */
  2090. return -ETIME;
  2091. }
  2092. if (!ctx_change)
  2093. ret = xhci_configure_endpoint_result(xhci, udev, cmd_status);
  2094. else
  2095. ret = xhci_evaluate_context_result(xhci, udev, cmd_status);
  2096. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
  2097. spin_lock_irqsave(&xhci->lock, flags);
  2098. /* If the command failed, remove the reserved resources.
  2099. * Otherwise, clean up the estimate to include dropped eps.
  2100. */
  2101. if (ret)
  2102. xhci_free_host_resources(xhci, in_ctx);
  2103. else
  2104. xhci_finish_resource_reservation(xhci, in_ctx);
  2105. spin_unlock_irqrestore(&xhci->lock, flags);
  2106. }
  2107. return ret;
  2108. }
  2109. /* Called after one or more calls to xhci_add_endpoint() or
  2110. * xhci_drop_endpoint(). If this call fails, the USB core is expected
  2111. * to call xhci_reset_bandwidth().
  2112. *
  2113. * Since we are in the middle of changing either configuration or
  2114. * installing a new alt setting, the USB core won't allow URBs to be
  2115. * enqueued for any endpoint on the old config or interface. Nothing
  2116. * else should be touching the xhci->devs[slot_id] structure, so we
  2117. * don't need to take the xhci->lock for manipulating that.
  2118. */
  2119. int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
  2120. {
  2121. int i;
  2122. int ret = 0;
  2123. struct xhci_hcd *xhci;
  2124. struct xhci_virt_device *virt_dev;
  2125. struct xhci_input_control_ctx *ctrl_ctx;
  2126. struct xhci_slot_ctx *slot_ctx;
  2127. ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
  2128. if (ret <= 0)
  2129. return ret;
  2130. xhci = hcd_to_xhci(hcd);
  2131. if (xhci->xhc_state & XHCI_STATE_DYING)
  2132. return -ENODEV;
  2133. xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
  2134. virt_dev = xhci->devs[udev->slot_id];
  2135. /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
  2136. ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
  2137. ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
  2138. ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG);
  2139. ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG));
  2140. /* Don't issue the command if there's no endpoints to update. */
  2141. if (ctrl_ctx->add_flags == cpu_to_le32(SLOT_FLAG) &&
  2142. ctrl_ctx->drop_flags == 0)
  2143. return 0;
  2144. xhci_dbg(xhci, "New Input Control Context:\n");
  2145. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
  2146. xhci_dbg_ctx(xhci, virt_dev->in_ctx,
  2147. LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx->dev_info)));
  2148. ret = xhci_configure_endpoint(xhci, udev, NULL,
  2149. false, false);
  2150. if (ret) {
  2151. /* Callee should call reset_bandwidth() */
  2152. return ret;
  2153. }
  2154. xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
  2155. xhci_dbg_ctx(xhci, virt_dev->out_ctx,
  2156. LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx->dev_info)));
  2157. /* Free any rings that were dropped, but not changed. */
  2158. for (i = 1; i < 31; ++i) {
  2159. if ((le32_to_cpu(ctrl_ctx->drop_flags) & (1 << (i + 1))) &&
  2160. !(le32_to_cpu(ctrl_ctx->add_flags) & (1 << (i + 1))))
  2161. xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
  2162. }
  2163. xhci_zero_in_ctx(xhci, virt_dev);
  2164. /*
  2165. * Install any rings for completely new endpoints or changed endpoints,
  2166. * and free or cache any old rings from changed endpoints.
  2167. */
  2168. for (i = 1; i < 31; ++i) {
  2169. if (!virt_dev->eps[i].new_ring)
  2170. continue;
  2171. /* Only cache or free the old ring if it exists.
  2172. * It may not if this is the first add of an endpoint.
  2173. */
  2174. if (virt_dev->eps[i].ring) {
  2175. xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
  2176. }
  2177. virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
  2178. virt_dev->eps[i].new_ring = NULL;
  2179. }
  2180. return ret;
  2181. }
  2182. void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
  2183. {
  2184. struct xhci_hcd *xhci;
  2185. struct xhci_virt_device *virt_dev;
  2186. int i, ret;
  2187. ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
  2188. if (ret <= 0)
  2189. return;
  2190. xhci = hcd_to_xhci(hcd);
  2191. xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
  2192. virt_dev = xhci->devs[udev->slot_id];
  2193. /* Free any rings allocated for added endpoints */
  2194. for (i = 0; i < 31; ++i) {
  2195. if (virt_dev->eps[i].new_ring) {
  2196. xhci_ring_free(xhci, virt_dev->eps[i].new_ring);
  2197. virt_dev->eps[i].new_ring = NULL;
  2198. }
  2199. }
  2200. xhci_zero_in_ctx(xhci, virt_dev);
  2201. }
  2202. static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci,
  2203. struct xhci_container_ctx *in_ctx,
  2204. struct xhci_container_ctx *out_ctx,
  2205. u32 add_flags, u32 drop_flags)
  2206. {
  2207. struct xhci_input_control_ctx *ctrl_ctx;
  2208. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  2209. ctrl_ctx->add_flags = cpu_to_le32(add_flags);
  2210. ctrl_ctx->drop_flags = cpu_to_le32(drop_flags);
  2211. xhci_slot_copy(xhci, in_ctx, out_ctx);
  2212. ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
  2213. xhci_dbg(xhci, "Input Context:\n");
  2214. xhci_dbg_ctx(xhci, in_ctx, xhci_last_valid_endpoint(add_flags));
  2215. }
  2216. static void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci,
  2217. unsigned int slot_id, unsigned int ep_index,
  2218. struct xhci_dequeue_state *deq_state)
  2219. {
  2220. struct xhci_container_ctx *in_ctx;
  2221. struct xhci_ep_ctx *ep_ctx;
  2222. u32 added_ctxs;
  2223. dma_addr_t addr;
  2224. xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
  2225. xhci->devs[slot_id]->out_ctx, ep_index);
  2226. in_ctx = xhci->devs[slot_id]->in_ctx;
  2227. ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
  2228. addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
  2229. deq_state->new_deq_ptr);
  2230. if (addr == 0) {
  2231. xhci_warn(xhci, "WARN Cannot submit config ep after "
  2232. "reset ep command\n");
  2233. xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n",
  2234. deq_state->new_deq_seg,
  2235. deq_state->new_deq_ptr);
  2236. return;
  2237. }
  2238. ep_ctx->deq = cpu_to_le64(addr | deq_state->new_cycle_state);
  2239. added_ctxs = xhci_get_endpoint_flag_from_index(ep_index);
  2240. xhci_setup_input_ctx_for_config_ep(xhci, xhci->devs[slot_id]->in_ctx,
  2241. xhci->devs[slot_id]->out_ctx, added_ctxs, added_ctxs);
  2242. }
  2243. void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
  2244. struct usb_device *udev, unsigned int ep_index)
  2245. {
  2246. struct xhci_dequeue_state deq_state;
  2247. struct xhci_virt_ep *ep;
  2248. xhci_dbg(xhci, "Cleaning up stalled endpoint ring\n");
  2249. ep = &xhci->devs[udev->slot_id]->eps[ep_index];
  2250. /* We need to move the HW's dequeue pointer past this TD,
  2251. * or it will attempt to resend it on the next doorbell ring.
  2252. */
  2253. xhci_find_new_dequeue_state(xhci, udev->slot_id,
  2254. ep_index, ep->stopped_stream, ep->stopped_td,
  2255. &deq_state);
  2256. /* HW with the reset endpoint quirk will use the saved dequeue state to
  2257. * issue a configure endpoint command later.
  2258. */
  2259. if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) {
  2260. xhci_dbg(xhci, "Queueing new dequeue state\n");
  2261. xhci_queue_new_dequeue_state(xhci, udev->slot_id,
  2262. ep_index, ep->stopped_stream, &deq_state);
  2263. } else {
  2264. /* Better hope no one uses the input context between now and the
  2265. * reset endpoint completion!
  2266. * XXX: No idea how this hardware will react when stream rings
  2267. * are enabled.
  2268. */
  2269. xhci_dbg(xhci, "Setting up input context for "
  2270. "configure endpoint command\n");
  2271. xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id,
  2272. ep_index, &deq_state);
  2273. }
  2274. }
  2275. /* Deal with stalled endpoints. The core should have sent the control message
  2276. * to clear the halt condition. However, we need to make the xHCI hardware
  2277. * reset its sequence number, since a device will expect a sequence number of
  2278. * zero after the halt condition is cleared.
  2279. * Context: in_interrupt
  2280. */
  2281. void xhci_endpoint_reset(struct usb_hcd *hcd,
  2282. struct usb_host_endpoint *ep)
  2283. {
  2284. struct xhci_hcd *xhci;
  2285. struct usb_device *udev;
  2286. unsigned int ep_index;
  2287. unsigned long flags;
  2288. int ret;
  2289. struct xhci_virt_ep *virt_ep;
  2290. xhci = hcd_to_xhci(hcd);
  2291. udev = (struct usb_device *) ep->hcpriv;
  2292. /* Called with a root hub endpoint (or an endpoint that wasn't added
  2293. * with xhci_add_endpoint()
  2294. */
  2295. if (!ep->hcpriv)
  2296. return;
  2297. ep_index = xhci_get_endpoint_index(&ep->desc);
  2298. virt_ep = &xhci->devs[udev->slot_id]->eps[ep_index];
  2299. if (!virt_ep->stopped_td) {
  2300. xhci_dbg(xhci, "Endpoint 0x%x not halted, refusing to reset.\n",
  2301. ep->desc.bEndpointAddress);
  2302. return;
  2303. }
  2304. if (usb_endpoint_xfer_control(&ep->desc)) {
  2305. xhci_dbg(xhci, "Control endpoint stall already handled.\n");
  2306. return;
  2307. }
  2308. xhci_dbg(xhci, "Queueing reset endpoint command\n");
  2309. spin_lock_irqsave(&xhci->lock, flags);
  2310. ret = xhci_queue_reset_ep(xhci, udev->slot_id, ep_index);
  2311. /*
  2312. * Can't change the ring dequeue pointer until it's transitioned to the
  2313. * stopped state, which is only upon a successful reset endpoint
  2314. * command. Better hope that last command worked!
  2315. */
  2316. if (!ret) {
  2317. xhci_cleanup_stalled_ring(xhci, udev, ep_index);
  2318. kfree(virt_ep->stopped_td);
  2319. xhci_ring_cmd_db(xhci);
  2320. }
  2321. virt_ep->stopped_td = NULL;
  2322. virt_ep->stopped_trb = NULL;
  2323. virt_ep->stopped_stream = 0;
  2324. spin_unlock_irqrestore(&xhci->lock, flags);
  2325. if (ret)
  2326. xhci_warn(xhci, "FIXME allocate a new ring segment\n");
  2327. }
  2328. static int xhci_check_streams_endpoint(struct xhci_hcd *xhci,
  2329. struct usb_device *udev, struct usb_host_endpoint *ep,
  2330. unsigned int slot_id)
  2331. {
  2332. int ret;
  2333. unsigned int ep_index;
  2334. unsigned int ep_state;
  2335. if (!ep)
  2336. return -EINVAL;
  2337. ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, true, __func__);
  2338. if (ret <= 0)
  2339. return -EINVAL;
  2340. if (ep->ss_ep_comp.bmAttributes == 0) {
  2341. xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion"
  2342. " descriptor for ep 0x%x does not support streams\n",
  2343. ep->desc.bEndpointAddress);
  2344. return -EINVAL;
  2345. }
  2346. ep_index = xhci_get_endpoint_index(&ep->desc);
  2347. ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
  2348. if (ep_state & EP_HAS_STREAMS ||
  2349. ep_state & EP_GETTING_STREAMS) {
  2350. xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x "
  2351. "already has streams set up.\n",
  2352. ep->desc.bEndpointAddress);
  2353. xhci_warn(xhci, "Send email to xHCI maintainer and ask for "
  2354. "dynamic stream context array reallocation.\n");
  2355. return -EINVAL;
  2356. }
  2357. if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) {
  2358. xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk "
  2359. "endpoint 0x%x; URBs are pending.\n",
  2360. ep->desc.bEndpointAddress);
  2361. return -EINVAL;
  2362. }
  2363. return 0;
  2364. }
  2365. static void xhci_calculate_streams_entries(struct xhci_hcd *xhci,
  2366. unsigned int *num_streams, unsigned int *num_stream_ctxs)
  2367. {
  2368. unsigned int max_streams;
  2369. /* The stream context array size must be a power of two */
  2370. *num_stream_ctxs = roundup_pow_of_two(*num_streams);
  2371. /*
  2372. * Find out how many primary stream array entries the host controller
  2373. * supports. Later we may use secondary stream arrays (similar to 2nd
  2374. * level page entries), but that's an optional feature for xHCI host
  2375. * controllers. xHCs must support at least 4 stream IDs.
  2376. */
  2377. max_streams = HCC_MAX_PSA(xhci->hcc_params);
  2378. if (*num_stream_ctxs > max_streams) {
  2379. xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n",
  2380. max_streams);
  2381. *num_stream_ctxs = max_streams;
  2382. *num_streams = max_streams;
  2383. }
  2384. }
  2385. /* Returns an error code if one of the endpoint already has streams.
  2386. * This does not change any data structures, it only checks and gathers
  2387. * information.
  2388. */
  2389. static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci,
  2390. struct usb_device *udev,
  2391. struct usb_host_endpoint **eps, unsigned int num_eps,
  2392. unsigned int *num_streams, u32 *changed_ep_bitmask)
  2393. {
  2394. unsigned int max_streams;
  2395. unsigned int endpoint_flag;
  2396. int i;
  2397. int ret;
  2398. for (i = 0; i < num_eps; i++) {
  2399. ret = xhci_check_streams_endpoint(xhci, udev,
  2400. eps[i], udev->slot_id);
  2401. if (ret < 0)
  2402. return ret;
  2403. max_streams = USB_SS_MAX_STREAMS(
  2404. eps[i]->ss_ep_comp.bmAttributes);
  2405. if (max_streams < (*num_streams - 1)) {
  2406. xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n",
  2407. eps[i]->desc.bEndpointAddress,
  2408. max_streams);
  2409. *num_streams = max_streams+1;
  2410. }
  2411. endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc);
  2412. if (*changed_ep_bitmask & endpoint_flag)
  2413. return -EINVAL;
  2414. *changed_ep_bitmask |= endpoint_flag;
  2415. }
  2416. return 0;
  2417. }
  2418. static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci,
  2419. struct usb_device *udev,
  2420. struct usb_host_endpoint **eps, unsigned int num_eps)
  2421. {
  2422. u32 changed_ep_bitmask = 0;
  2423. unsigned int slot_id;
  2424. unsigned int ep_index;
  2425. unsigned int ep_state;
  2426. int i;
  2427. slot_id = udev->slot_id;
  2428. if (!xhci->devs[slot_id])
  2429. return 0;
  2430. for (i = 0; i < num_eps; i++) {
  2431. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2432. ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
  2433. /* Are streams already being freed for the endpoint? */
  2434. if (ep_state & EP_GETTING_NO_STREAMS) {
  2435. xhci_warn(xhci, "WARN Can't disable streams for "
  2436. "endpoint 0x%x\n, "
  2437. "streams are being disabled already.",
  2438. eps[i]->desc.bEndpointAddress);
  2439. return 0;
  2440. }
  2441. /* Are there actually any streams to free? */
  2442. if (!(ep_state & EP_HAS_STREAMS) &&
  2443. !(ep_state & EP_GETTING_STREAMS)) {
  2444. xhci_warn(xhci, "WARN Can't disable streams for "
  2445. "endpoint 0x%x\n, "
  2446. "streams are already disabled!",
  2447. eps[i]->desc.bEndpointAddress);
  2448. xhci_warn(xhci, "WARN xhci_free_streams() called "
  2449. "with non-streams endpoint\n");
  2450. return 0;
  2451. }
  2452. changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc);
  2453. }
  2454. return changed_ep_bitmask;
  2455. }
  2456. /*
  2457. * The USB device drivers use this function (though the HCD interface in USB
  2458. * core) to prepare a set of bulk endpoints to use streams. Streams are used to
  2459. * coordinate mass storage command queueing across multiple endpoints (basically
  2460. * a stream ID == a task ID).
  2461. *
  2462. * Setting up streams involves allocating the same size stream context array
  2463. * for each endpoint and issuing a configure endpoint command for all endpoints.
  2464. *
  2465. * Don't allow the call to succeed if one endpoint only supports one stream
  2466. * (which means it doesn't support streams at all).
  2467. *
  2468. * Drivers may get less stream IDs than they asked for, if the host controller
  2469. * hardware or endpoints claim they can't support the number of requested
  2470. * stream IDs.
  2471. */
  2472. int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
  2473. struct usb_host_endpoint **eps, unsigned int num_eps,
  2474. unsigned int num_streams, gfp_t mem_flags)
  2475. {
  2476. int i, ret;
  2477. struct xhci_hcd *xhci;
  2478. struct xhci_virt_device *vdev;
  2479. struct xhci_command *config_cmd;
  2480. unsigned int ep_index;
  2481. unsigned int num_stream_ctxs;
  2482. unsigned long flags;
  2483. u32 changed_ep_bitmask = 0;
  2484. if (!eps)
  2485. return -EINVAL;
  2486. /* Add one to the number of streams requested to account for
  2487. * stream 0 that is reserved for xHCI usage.
  2488. */
  2489. num_streams += 1;
  2490. xhci = hcd_to_xhci(hcd);
  2491. xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n",
  2492. num_streams);
  2493. config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
  2494. if (!config_cmd) {
  2495. xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
  2496. return -ENOMEM;
  2497. }
  2498. /* Check to make sure all endpoints are not already configured for
  2499. * streams. While we're at it, find the maximum number of streams that
  2500. * all the endpoints will support and check for duplicate endpoints.
  2501. */
  2502. spin_lock_irqsave(&xhci->lock, flags);
  2503. ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps,
  2504. num_eps, &num_streams, &changed_ep_bitmask);
  2505. if (ret < 0) {
  2506. xhci_free_command(xhci, config_cmd);
  2507. spin_unlock_irqrestore(&xhci->lock, flags);
  2508. return ret;
  2509. }
  2510. if (num_streams <= 1) {
  2511. xhci_warn(xhci, "WARN: endpoints can't handle "
  2512. "more than one stream.\n");
  2513. xhci_free_command(xhci, config_cmd);
  2514. spin_unlock_irqrestore(&xhci->lock, flags);
  2515. return -EINVAL;
  2516. }
  2517. vdev = xhci->devs[udev->slot_id];
  2518. /* Mark each endpoint as being in transition, so
  2519. * xhci_urb_enqueue() will reject all URBs.
  2520. */
  2521. for (i = 0; i < num_eps; i++) {
  2522. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2523. vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS;
  2524. }
  2525. spin_unlock_irqrestore(&xhci->lock, flags);
  2526. /* Setup internal data structures and allocate HW data structures for
  2527. * streams (but don't install the HW structures in the input context
  2528. * until we're sure all memory allocation succeeded).
  2529. */
  2530. xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs);
  2531. xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n",
  2532. num_stream_ctxs, num_streams);
  2533. for (i = 0; i < num_eps; i++) {
  2534. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2535. vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci,
  2536. num_stream_ctxs,
  2537. num_streams, mem_flags);
  2538. if (!vdev->eps[ep_index].stream_info)
  2539. goto cleanup;
  2540. /* Set maxPstreams in endpoint context and update deq ptr to
  2541. * point to stream context array. FIXME
  2542. */
  2543. }
  2544. /* Set up the input context for a configure endpoint command. */
  2545. for (i = 0; i < num_eps; i++) {
  2546. struct xhci_ep_ctx *ep_ctx;
  2547. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2548. ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index);
  2549. xhci_endpoint_copy(xhci, config_cmd->in_ctx,
  2550. vdev->out_ctx, ep_index);
  2551. xhci_setup_streams_ep_input_ctx(xhci, ep_ctx,
  2552. vdev->eps[ep_index].stream_info);
  2553. }
  2554. /* Tell the HW to drop its old copy of the endpoint context info
  2555. * and add the updated copy from the input context.
  2556. */
  2557. xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx,
  2558. vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
  2559. /* Issue and wait for the configure endpoint command */
  2560. ret = xhci_configure_endpoint(xhci, udev, config_cmd,
  2561. false, false);
  2562. /* xHC rejected the configure endpoint command for some reason, so we
  2563. * leave the old ring intact and free our internal streams data
  2564. * structure.
  2565. */
  2566. if (ret < 0)
  2567. goto cleanup;
  2568. spin_lock_irqsave(&xhci->lock, flags);
  2569. for (i = 0; i < num_eps; i++) {
  2570. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2571. vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
  2572. xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n",
  2573. udev->slot_id, ep_index);
  2574. vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS;
  2575. }
  2576. xhci_free_command(xhci, config_cmd);
  2577. spin_unlock_irqrestore(&xhci->lock, flags);
  2578. /* Subtract 1 for stream 0, which drivers can't use */
  2579. return num_streams - 1;
  2580. cleanup:
  2581. /* If it didn't work, free the streams! */
  2582. for (i = 0; i < num_eps; i++) {
  2583. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2584. xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
  2585. vdev->eps[ep_index].stream_info = NULL;
  2586. /* FIXME Unset maxPstreams in endpoint context and
  2587. * update deq ptr to point to normal string ring.
  2588. */
  2589. vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
  2590. vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
  2591. xhci_endpoint_zero(xhci, vdev, eps[i]);
  2592. }
  2593. xhci_free_command(xhci, config_cmd);
  2594. return -ENOMEM;
  2595. }
  2596. /* Transition the endpoint from using streams to being a "normal" endpoint
  2597. * without streams.
  2598. *
  2599. * Modify the endpoint context state, submit a configure endpoint command,
  2600. * and free all endpoint rings for streams if that completes successfully.
  2601. */
  2602. int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
  2603. struct usb_host_endpoint **eps, unsigned int num_eps,
  2604. gfp_t mem_flags)
  2605. {
  2606. int i, ret;
  2607. struct xhci_hcd *xhci;
  2608. struct xhci_virt_device *vdev;
  2609. struct xhci_command *command;
  2610. unsigned int ep_index;
  2611. unsigned long flags;
  2612. u32 changed_ep_bitmask;
  2613. xhci = hcd_to_xhci(hcd);
  2614. vdev = xhci->devs[udev->slot_id];
  2615. /* Set up a configure endpoint command to remove the streams rings */
  2616. spin_lock_irqsave(&xhci->lock, flags);
  2617. changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci,
  2618. udev, eps, num_eps);
  2619. if (changed_ep_bitmask == 0) {
  2620. spin_unlock_irqrestore(&xhci->lock, flags);
  2621. return -EINVAL;
  2622. }
  2623. /* Use the xhci_command structure from the first endpoint. We may have
  2624. * allocated too many, but the driver may call xhci_free_streams() for
  2625. * each endpoint it grouped into one call to xhci_alloc_streams().
  2626. */
  2627. ep_index = xhci_get_endpoint_index(&eps[0]->desc);
  2628. command = vdev->eps[ep_index].stream_info->free_streams_command;
  2629. for (i = 0; i < num_eps; i++) {
  2630. struct xhci_ep_ctx *ep_ctx;
  2631. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2632. ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
  2633. xhci->devs[udev->slot_id]->eps[ep_index].ep_state |=
  2634. EP_GETTING_NO_STREAMS;
  2635. xhci_endpoint_copy(xhci, command->in_ctx,
  2636. vdev->out_ctx, ep_index);
  2637. xhci_setup_no_streams_ep_input_ctx(xhci, ep_ctx,
  2638. &vdev->eps[ep_index]);
  2639. }
  2640. xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx,
  2641. vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
  2642. spin_unlock_irqrestore(&xhci->lock, flags);
  2643. /* Issue and wait for the configure endpoint command,
  2644. * which must succeed.
  2645. */
  2646. ret = xhci_configure_endpoint(xhci, udev, command,
  2647. false, true);
  2648. /* xHC rejected the configure endpoint command for some reason, so we
  2649. * leave the streams rings intact.
  2650. */
  2651. if (ret < 0)
  2652. return ret;
  2653. spin_lock_irqsave(&xhci->lock, flags);
  2654. for (i = 0; i < num_eps; i++) {
  2655. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2656. xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
  2657. vdev->eps[ep_index].stream_info = NULL;
  2658. /* FIXME Unset maxPstreams in endpoint context and
  2659. * update deq ptr to point to normal string ring.
  2660. */
  2661. vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS;
  2662. vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
  2663. }
  2664. spin_unlock_irqrestore(&xhci->lock, flags);
  2665. return 0;
  2666. }
  2667. /*
  2668. * Deletes endpoint resources for endpoints that were active before a Reset
  2669. * Device command, or a Disable Slot command. The Reset Device command leaves
  2670. * the control endpoint intact, whereas the Disable Slot command deletes it.
  2671. *
  2672. * Must be called with xhci->lock held.
  2673. */
  2674. void xhci_free_device_endpoint_resources(struct xhci_hcd *xhci,
  2675. struct xhci_virt_device *virt_dev, bool drop_control_ep)
  2676. {
  2677. int i;
  2678. unsigned int num_dropped_eps = 0;
  2679. unsigned int drop_flags = 0;
  2680. for (i = (drop_control_ep ? 0 : 1); i < 31; i++) {
  2681. if (virt_dev->eps[i].ring) {
  2682. drop_flags |= 1 << i;
  2683. num_dropped_eps++;
  2684. }
  2685. }
  2686. xhci->num_active_eps -= num_dropped_eps;
  2687. if (num_dropped_eps)
  2688. xhci_dbg(xhci, "Dropped %u ep ctxs, flags = 0x%x, "
  2689. "%u now active.\n",
  2690. num_dropped_eps, drop_flags,
  2691. xhci->num_active_eps);
  2692. }
  2693. /*
  2694. * This submits a Reset Device Command, which will set the device state to 0,
  2695. * set the device address to 0, and disable all the endpoints except the default
  2696. * control endpoint. The USB core should come back and call
  2697. * xhci_address_device(), and then re-set up the configuration. If this is
  2698. * called because of a usb_reset_and_verify_device(), then the old alternate
  2699. * settings will be re-installed through the normal bandwidth allocation
  2700. * functions.
  2701. *
  2702. * Wait for the Reset Device command to finish. Remove all structures
  2703. * associated with the endpoints that were disabled. Clear the input device
  2704. * structure? Cache the rings? Reset the control endpoint 0 max packet size?
  2705. *
  2706. * If the virt_dev to be reset does not exist or does not match the udev,
  2707. * it means the device is lost, possibly due to the xHC restore error and
  2708. * re-initialization during S3/S4. In this case, call xhci_alloc_dev() to
  2709. * re-allocate the device.
  2710. */
  2711. int xhci_discover_or_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
  2712. {
  2713. int ret, i;
  2714. unsigned long flags;
  2715. struct xhci_hcd *xhci;
  2716. unsigned int slot_id;
  2717. struct xhci_virt_device *virt_dev;
  2718. struct xhci_command *reset_device_cmd;
  2719. int timeleft;
  2720. int last_freed_endpoint;
  2721. struct xhci_slot_ctx *slot_ctx;
  2722. int old_active_eps = 0;
  2723. ret = xhci_check_args(hcd, udev, NULL, 0, false, __func__);
  2724. if (ret <= 0)
  2725. return ret;
  2726. xhci = hcd_to_xhci(hcd);
  2727. slot_id = udev->slot_id;
  2728. virt_dev = xhci->devs[slot_id];
  2729. if (!virt_dev) {
  2730. xhci_dbg(xhci, "The device to be reset with slot ID %u does "
  2731. "not exist. Re-allocate the device\n", slot_id);
  2732. ret = xhci_alloc_dev(hcd, udev);
  2733. if (ret == 1)
  2734. return 0;
  2735. else
  2736. return -EINVAL;
  2737. }
  2738. if (virt_dev->udev != udev) {
  2739. /* If the virt_dev and the udev does not match, this virt_dev
  2740. * may belong to another udev.
  2741. * Re-allocate the device.
  2742. */
  2743. xhci_dbg(xhci, "The device to be reset with slot ID %u does "
  2744. "not match the udev. Re-allocate the device\n",
  2745. slot_id);
  2746. ret = xhci_alloc_dev(hcd, udev);
  2747. if (ret == 1)
  2748. return 0;
  2749. else
  2750. return -EINVAL;
  2751. }
  2752. /* If device is not setup, there is no point in resetting it */
  2753. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
  2754. if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) ==
  2755. SLOT_STATE_DISABLED)
  2756. return 0;
  2757. xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id);
  2758. /* Allocate the command structure that holds the struct completion.
  2759. * Assume we're in process context, since the normal device reset
  2760. * process has to wait for the device anyway. Storage devices are
  2761. * reset as part of error handling, so use GFP_NOIO instead of
  2762. * GFP_KERNEL.
  2763. */
  2764. reset_device_cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
  2765. if (!reset_device_cmd) {
  2766. xhci_dbg(xhci, "Couldn't allocate command structure.\n");
  2767. return -ENOMEM;
  2768. }
  2769. /* Attempt to submit the Reset Device command to the command ring */
  2770. spin_lock_irqsave(&xhci->lock, flags);
  2771. reset_device_cmd->command_trb = xhci->cmd_ring->enqueue;
  2772. /* Enqueue pointer can be left pointing to the link TRB,
  2773. * we must handle that
  2774. */
  2775. if (TRB_TYPE_LINK_LE32(reset_device_cmd->command_trb->link.control))
  2776. reset_device_cmd->command_trb =
  2777. xhci->cmd_ring->enq_seg->next->trbs;
  2778. list_add_tail(&reset_device_cmd->cmd_list, &virt_dev->cmd_list);
  2779. ret = xhci_queue_reset_device(xhci, slot_id);
  2780. if (ret) {
  2781. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  2782. list_del(&reset_device_cmd->cmd_list);
  2783. spin_unlock_irqrestore(&xhci->lock, flags);
  2784. goto command_cleanup;
  2785. }
  2786. xhci_ring_cmd_db(xhci);
  2787. spin_unlock_irqrestore(&xhci->lock, flags);
  2788. /* Wait for the Reset Device command to finish */
  2789. timeleft = wait_for_completion_interruptible_timeout(
  2790. reset_device_cmd->completion,
  2791. USB_CTRL_SET_TIMEOUT);
  2792. if (timeleft <= 0) {
  2793. xhci_warn(xhci, "%s while waiting for reset device command\n",
  2794. timeleft == 0 ? "Timeout" : "Signal");
  2795. spin_lock_irqsave(&xhci->lock, flags);
  2796. /* The timeout might have raced with the event ring handler, so
  2797. * only delete from the list if the item isn't poisoned.
  2798. */
  2799. if (reset_device_cmd->cmd_list.next != LIST_POISON1)
  2800. list_del(&reset_device_cmd->cmd_list);
  2801. spin_unlock_irqrestore(&xhci->lock, flags);
  2802. ret = -ETIME;
  2803. goto command_cleanup;
  2804. }
  2805. /* The Reset Device command can't fail, according to the 0.95/0.96 spec,
  2806. * unless we tried to reset a slot ID that wasn't enabled,
  2807. * or the device wasn't in the addressed or configured state.
  2808. */
  2809. ret = reset_device_cmd->status;
  2810. switch (ret) {
  2811. case COMP_EBADSLT: /* 0.95 completion code for bad slot ID */
  2812. case COMP_CTX_STATE: /* 0.96 completion code for same thing */
  2813. xhci_info(xhci, "Can't reset device (slot ID %u) in %s state\n",
  2814. slot_id,
  2815. xhci_get_slot_state(xhci, virt_dev->out_ctx));
  2816. xhci_info(xhci, "Not freeing device rings.\n");
  2817. /* Don't treat this as an error. May change my mind later. */
  2818. ret = 0;
  2819. goto command_cleanup;
  2820. case COMP_SUCCESS:
  2821. xhci_dbg(xhci, "Successful reset device command.\n");
  2822. break;
  2823. default:
  2824. if (xhci_is_vendor_info_code(xhci, ret))
  2825. break;
  2826. xhci_warn(xhci, "Unknown completion code %u for "
  2827. "reset device command.\n", ret);
  2828. ret = -EINVAL;
  2829. goto command_cleanup;
  2830. }
  2831. /* Free up host controller endpoint resources */
  2832. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
  2833. spin_lock_irqsave(&xhci->lock, flags);
  2834. /* Don't delete the default control endpoint resources */
  2835. xhci_free_device_endpoint_resources(xhci, virt_dev, false);
  2836. spin_unlock_irqrestore(&xhci->lock, flags);
  2837. }
  2838. /* Everything but endpoint 0 is disabled, so free or cache the rings. */
  2839. last_freed_endpoint = 1;
  2840. for (i = 1; i < 31; ++i) {
  2841. struct xhci_virt_ep *ep = &virt_dev->eps[i];
  2842. if (ep->ep_state & EP_HAS_STREAMS) {
  2843. xhci_free_stream_info(xhci, ep->stream_info);
  2844. ep->stream_info = NULL;
  2845. ep->ep_state &= ~EP_HAS_STREAMS;
  2846. }
  2847. if (ep->ring) {
  2848. xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
  2849. last_freed_endpoint = i;
  2850. }
  2851. if (!list_empty(&virt_dev->eps[i].bw_endpoint_list))
  2852. xhci_drop_ep_from_interval_table(xhci,
  2853. &virt_dev->eps[i].bw_info,
  2854. virt_dev->bw_table,
  2855. udev,
  2856. &virt_dev->eps[i],
  2857. virt_dev->tt_info);
  2858. xhci_clear_endpoint_bw_info(&virt_dev->eps[i].bw_info);
  2859. }
  2860. /* If necessary, update the number of active TTs on this root port */
  2861. xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
  2862. xhci_dbg(xhci, "Output context after successful reset device cmd:\n");
  2863. xhci_dbg_ctx(xhci, virt_dev->out_ctx, last_freed_endpoint);
  2864. ret = 0;
  2865. command_cleanup:
  2866. xhci_free_command(xhci, reset_device_cmd);
  2867. return ret;
  2868. }
  2869. /*
  2870. * At this point, the struct usb_device is about to go away, the device has
  2871. * disconnected, and all traffic has been stopped and the endpoints have been
  2872. * disabled. Free any HC data structures associated with that device.
  2873. */
  2874. void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
  2875. {
  2876. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  2877. struct xhci_virt_device *virt_dev;
  2878. unsigned long flags;
  2879. u32 state;
  2880. int i, ret;
  2881. ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
  2882. /* If the host is halted due to driver unload, we still need to free the
  2883. * device.
  2884. */
  2885. if (ret <= 0 && ret != -ENODEV)
  2886. return;
  2887. virt_dev = xhci->devs[udev->slot_id];
  2888. /* Stop any wayward timer functions (which may grab the lock) */
  2889. for (i = 0; i < 31; ++i) {
  2890. virt_dev->eps[i].ep_state &= ~EP_HALT_PENDING;
  2891. del_timer_sync(&virt_dev->eps[i].stop_cmd_timer);
  2892. }
  2893. spin_lock_irqsave(&xhci->lock, flags);
  2894. /* Don't disable the slot if the host controller is dead. */
  2895. state = xhci_readl(xhci, &xhci->op_regs->status);
  2896. if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) ||
  2897. (xhci->xhc_state & XHCI_STATE_HALTED)) {
  2898. xhci_free_virt_device(xhci, udev->slot_id);
  2899. spin_unlock_irqrestore(&xhci->lock, flags);
  2900. return;
  2901. }
  2902. if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
  2903. spin_unlock_irqrestore(&xhci->lock, flags);
  2904. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  2905. return;
  2906. }
  2907. xhci_ring_cmd_db(xhci);
  2908. spin_unlock_irqrestore(&xhci->lock, flags);
  2909. /*
  2910. * Event command completion handler will free any data structures
  2911. * associated with the slot. XXX Can free sleep?
  2912. */
  2913. }
  2914. /*
  2915. * Checks if we have enough host controller resources for the default control
  2916. * endpoint.
  2917. *
  2918. * Must be called with xhci->lock held.
  2919. */
  2920. static int xhci_reserve_host_control_ep_resources(struct xhci_hcd *xhci)
  2921. {
  2922. if (xhci->num_active_eps + 1 > xhci->limit_active_eps) {
  2923. xhci_dbg(xhci, "Not enough ep ctxs: "
  2924. "%u active, need to add 1, limit is %u.\n",
  2925. xhci->num_active_eps, xhci->limit_active_eps);
  2926. return -ENOMEM;
  2927. }
  2928. xhci->num_active_eps += 1;
  2929. xhci_dbg(xhci, "Adding 1 ep ctx, %u now active.\n",
  2930. xhci->num_active_eps);
  2931. return 0;
  2932. }
  2933. /*
  2934. * Returns 0 if the xHC ran out of device slots, the Enable Slot command
  2935. * timed out, or allocating memory failed. Returns 1 on success.
  2936. */
  2937. int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
  2938. {
  2939. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  2940. unsigned long flags;
  2941. int timeleft;
  2942. int ret;
  2943. spin_lock_irqsave(&xhci->lock, flags);
  2944. ret = xhci_queue_slot_control(xhci, TRB_ENABLE_SLOT, 0);
  2945. if (ret) {
  2946. spin_unlock_irqrestore(&xhci->lock, flags);
  2947. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  2948. return 0;
  2949. }
  2950. xhci_ring_cmd_db(xhci);
  2951. spin_unlock_irqrestore(&xhci->lock, flags);
  2952. /* XXX: how much time for xHC slot assignment? */
  2953. timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
  2954. USB_CTRL_SET_TIMEOUT);
  2955. if (timeleft <= 0) {
  2956. xhci_warn(xhci, "%s while waiting for a slot\n",
  2957. timeleft == 0 ? "Timeout" : "Signal");
  2958. /* FIXME cancel the enable slot request */
  2959. return 0;
  2960. }
  2961. if (!xhci->slot_id) {
  2962. xhci_err(xhci, "Error while assigning device slot ID\n");
  2963. return 0;
  2964. }
  2965. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
  2966. spin_lock_irqsave(&xhci->lock, flags);
  2967. ret = xhci_reserve_host_control_ep_resources(xhci);
  2968. if (ret) {
  2969. spin_unlock_irqrestore(&xhci->lock, flags);
  2970. xhci_warn(xhci, "Not enough host resources, "
  2971. "active endpoint contexts = %u\n",
  2972. xhci->num_active_eps);
  2973. goto disable_slot;
  2974. }
  2975. spin_unlock_irqrestore(&xhci->lock, flags);
  2976. }
  2977. /* Use GFP_NOIO, since this function can be called from
  2978. * xhci_discover_or_reset_device(), which may be called as part of
  2979. * mass storage driver error handling.
  2980. */
  2981. if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_NOIO)) {
  2982. xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
  2983. goto disable_slot;
  2984. }
  2985. udev->slot_id = xhci->slot_id;
  2986. /* Is this a LS or FS device under a HS hub? */
  2987. /* Hub or peripherial? */
  2988. return 1;
  2989. disable_slot:
  2990. /* Disable slot, if we can do it without mem alloc */
  2991. spin_lock_irqsave(&xhci->lock, flags);
  2992. if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
  2993. xhci_ring_cmd_db(xhci);
  2994. spin_unlock_irqrestore(&xhci->lock, flags);
  2995. return 0;
  2996. }
  2997. /*
  2998. * Issue an Address Device command (which will issue a SetAddress request to
  2999. * the device).
  3000. * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so
  3001. * we should only issue and wait on one address command at the same time.
  3002. *
  3003. * We add one to the device address issued by the hardware because the USB core
  3004. * uses address 1 for the root hubs (even though they're not really devices).
  3005. */
  3006. int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
  3007. {
  3008. unsigned long flags;
  3009. int timeleft;
  3010. struct xhci_virt_device *virt_dev;
  3011. int ret = 0;
  3012. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  3013. struct xhci_slot_ctx *slot_ctx;
  3014. struct xhci_input_control_ctx *ctrl_ctx;
  3015. u64 temp_64;
  3016. if (!udev->slot_id) {
  3017. xhci_dbg(xhci, "Bad Slot ID %d\n", udev->slot_id);
  3018. return -EINVAL;
  3019. }
  3020. virt_dev = xhci->devs[udev->slot_id];
  3021. if (WARN_ON(!virt_dev)) {
  3022. /*
  3023. * In plug/unplug torture test with an NEC controller,
  3024. * a zero-dereference was observed once due to virt_dev = 0.
  3025. * Print useful debug rather than crash if it is observed again!
  3026. */
  3027. xhci_warn(xhci, "Virt dev invalid for slot_id 0x%x!\n",
  3028. udev->slot_id);
  3029. return -EINVAL;
  3030. }
  3031. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
  3032. /*
  3033. * If this is the first Set Address since device plug-in or
  3034. * virt_device realloaction after a resume with an xHCI power loss,
  3035. * then set up the slot context.
  3036. */
  3037. if (!slot_ctx->dev_info)
  3038. xhci_setup_addressable_virt_dev(xhci, udev);
  3039. /* Otherwise, update the control endpoint ring enqueue pointer. */
  3040. else
  3041. xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev);
  3042. xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
  3043. xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
  3044. spin_lock_irqsave(&xhci->lock, flags);
  3045. ret = xhci_queue_address_device(xhci, virt_dev->in_ctx->dma,
  3046. udev->slot_id);
  3047. if (ret) {
  3048. spin_unlock_irqrestore(&xhci->lock, flags);
  3049. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  3050. return ret;
  3051. }
  3052. xhci_ring_cmd_db(xhci);
  3053. spin_unlock_irqrestore(&xhci->lock, flags);
  3054. /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
  3055. timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
  3056. USB_CTRL_SET_TIMEOUT);
  3057. /* FIXME: From section 4.3.4: "Software shall be responsible for timing
  3058. * the SetAddress() "recovery interval" required by USB and aborting the
  3059. * command on a timeout.
  3060. */
  3061. if (timeleft <= 0) {
  3062. xhci_warn(xhci, "%s while waiting for a slot\n",
  3063. timeleft == 0 ? "Timeout" : "Signal");
  3064. /* FIXME cancel the address device command */
  3065. return -ETIME;
  3066. }
  3067. switch (virt_dev->cmd_status) {
  3068. case COMP_CTX_STATE:
  3069. case COMP_EBADSLT:
  3070. xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n",
  3071. udev->slot_id);
  3072. ret = -EINVAL;
  3073. break;
  3074. case COMP_TX_ERR:
  3075. dev_warn(&udev->dev, "Device not responding to set address.\n");
  3076. ret = -EPROTO;
  3077. break;
  3078. case COMP_DEV_ERR:
  3079. dev_warn(&udev->dev, "ERROR: Incompatible device for address "
  3080. "device command.\n");
  3081. ret = -ENODEV;
  3082. break;
  3083. case COMP_SUCCESS:
  3084. xhci_dbg(xhci, "Successful Address Device command\n");
  3085. break;
  3086. default:
  3087. xhci_err(xhci, "ERROR: unexpected command completion "
  3088. "code 0x%x.\n", virt_dev->cmd_status);
  3089. xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
  3090. xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
  3091. ret = -EINVAL;
  3092. break;
  3093. }
  3094. if (ret) {
  3095. return ret;
  3096. }
  3097. temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
  3098. xhci_dbg(xhci, "Op regs DCBAA ptr = %#016llx\n", temp_64);
  3099. xhci_dbg(xhci, "Slot ID %d dcbaa entry @%p = %#016llx\n",
  3100. udev->slot_id,
  3101. &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
  3102. (unsigned long long)
  3103. le64_to_cpu(xhci->dcbaa->dev_context_ptrs[udev->slot_id]));
  3104. xhci_dbg(xhci, "Output Context DMA address = %#08llx\n",
  3105. (unsigned long long)virt_dev->out_ctx->dma);
  3106. xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
  3107. xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
  3108. xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
  3109. xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
  3110. /*
  3111. * USB core uses address 1 for the roothubs, so we add one to the
  3112. * address given back to us by the HC.
  3113. */
  3114. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
  3115. /* Use kernel assigned address for devices; store xHC assigned
  3116. * address locally. */
  3117. virt_dev->address = (le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK)
  3118. + 1;
  3119. /* Zero the input context control for later use */
  3120. ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
  3121. ctrl_ctx->add_flags = 0;
  3122. ctrl_ctx->drop_flags = 0;
  3123. xhci_dbg(xhci, "Internal device address = %d\n", virt_dev->address);
  3124. return 0;
  3125. }
  3126. /* Once a hub descriptor is fetched for a device, we need to update the xHC's
  3127. * internal data structures for the device.
  3128. */
  3129. int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
  3130. struct usb_tt *tt, gfp_t mem_flags)
  3131. {
  3132. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  3133. struct xhci_virt_device *vdev;
  3134. struct xhci_command *config_cmd;
  3135. struct xhci_input_control_ctx *ctrl_ctx;
  3136. struct xhci_slot_ctx *slot_ctx;
  3137. unsigned long flags;
  3138. unsigned think_time;
  3139. int ret;
  3140. /* Ignore root hubs */
  3141. if (!hdev->parent)
  3142. return 0;
  3143. vdev = xhci->devs[hdev->slot_id];
  3144. if (!vdev) {
  3145. xhci_warn(xhci, "Cannot update hub desc for unknown device.\n");
  3146. return -EINVAL;
  3147. }
  3148. config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
  3149. if (!config_cmd) {
  3150. xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
  3151. return -ENOMEM;
  3152. }
  3153. spin_lock_irqsave(&xhci->lock, flags);
  3154. if (hdev->speed == USB_SPEED_HIGH &&
  3155. xhci_alloc_tt_info(xhci, vdev, hdev, tt, GFP_ATOMIC)) {
  3156. xhci_dbg(xhci, "Could not allocate xHCI TT structure.\n");
  3157. xhci_free_command(xhci, config_cmd);
  3158. spin_unlock_irqrestore(&xhci->lock, flags);
  3159. return -ENOMEM;
  3160. }
  3161. xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx);
  3162. ctrl_ctx = xhci_get_input_control_ctx(xhci, config_cmd->in_ctx);
  3163. ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
  3164. slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx);
  3165. slot_ctx->dev_info |= cpu_to_le32(DEV_HUB);
  3166. if (tt->multi)
  3167. slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
  3168. if (xhci->hci_version > 0x95) {
  3169. xhci_dbg(xhci, "xHCI version %x needs hub "
  3170. "TT think time and number of ports\n",
  3171. (unsigned int) xhci->hci_version);
  3172. slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(hdev->maxchild));
  3173. /* Set TT think time - convert from ns to FS bit times.
  3174. * 0 = 8 FS bit times, 1 = 16 FS bit times,
  3175. * 2 = 24 FS bit times, 3 = 32 FS bit times.
  3176. *
  3177. * xHCI 1.0: this field shall be 0 if the device is not a
  3178. * High-spped hub.
  3179. */
  3180. think_time = tt->think_time;
  3181. if (think_time != 0)
  3182. think_time = (think_time / 666) - 1;
  3183. if (xhci->hci_version < 0x100 || hdev->speed == USB_SPEED_HIGH)
  3184. slot_ctx->tt_info |=
  3185. cpu_to_le32(TT_THINK_TIME(think_time));
  3186. } else {
  3187. xhci_dbg(xhci, "xHCI version %x doesn't need hub "
  3188. "TT think time or number of ports\n",
  3189. (unsigned int) xhci->hci_version);
  3190. }
  3191. slot_ctx->dev_state = 0;
  3192. spin_unlock_irqrestore(&xhci->lock, flags);
  3193. xhci_dbg(xhci, "Set up %s for hub device.\n",
  3194. (xhci->hci_version > 0x95) ?
  3195. "configure endpoint" : "evaluate context");
  3196. xhci_dbg(xhci, "Slot %u Input Context:\n", hdev->slot_id);
  3197. xhci_dbg_ctx(xhci, config_cmd->in_ctx, 0);
  3198. /* Issue and wait for the configure endpoint or
  3199. * evaluate context command.
  3200. */
  3201. if (xhci->hci_version > 0x95)
  3202. ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
  3203. false, false);
  3204. else
  3205. ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
  3206. true, false);
  3207. xhci_dbg(xhci, "Slot %u Output Context:\n", hdev->slot_id);
  3208. xhci_dbg_ctx(xhci, vdev->out_ctx, 0);
  3209. xhci_free_command(xhci, config_cmd);
  3210. return ret;
  3211. }
  3212. int xhci_get_frame(struct usb_hcd *hcd)
  3213. {
  3214. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  3215. /* EHCI mods by the periodic size. Why? */
  3216. return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
  3217. }
  3218. MODULE_DESCRIPTION(DRIVER_DESC);
  3219. MODULE_AUTHOR(DRIVER_AUTHOR);
  3220. MODULE_LICENSE("GPL");
  3221. static int __init xhci_hcd_init(void)
  3222. {
  3223. #ifdef CONFIG_PCI
  3224. int retval = 0;
  3225. retval = xhci_register_pci();
  3226. if (retval < 0) {
  3227. printk(KERN_DEBUG "Problem registering PCI driver.");
  3228. return retval;
  3229. }
  3230. #endif
  3231. /*
  3232. * Check the compiler generated sizes of structures that must be laid
  3233. * out in specific ways for hardware access.
  3234. */
  3235. BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
  3236. BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
  3237. BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
  3238. /* xhci_device_control has eight fields, and also
  3239. * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
  3240. */
  3241. BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
  3242. BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
  3243. BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
  3244. BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 7*32/8);
  3245. BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
  3246. /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
  3247. BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
  3248. BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
  3249. return 0;
  3250. }
  3251. module_init(xhci_hcd_init);
  3252. static void __exit xhci_hcd_cleanup(void)
  3253. {
  3254. #ifdef CONFIG_PCI
  3255. xhci_unregister_pci();
  3256. #endif
  3257. }
  3258. module_exit(xhci_hcd_cleanup);