xhci.c 141 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728
  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 <linux/dmi.h>
  29. #include "xhci.h"
  30. #define DRIVER_AUTHOR "Sarah Sharp"
  31. #define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
  32. /* Some 0.95 hardware can't handle the chain bit on a Link TRB being cleared */
  33. static int link_quirk;
  34. module_param(link_quirk, int, S_IRUGO | S_IWUSR);
  35. MODULE_PARM_DESC(link_quirk, "Don't clear the chain bit on a link TRB");
  36. /* TODO: copied from ehci-hcd.c - can this be refactored? */
  37. /*
  38. * xhci_handshake - spin reading hc until handshake completes or fails
  39. * @ptr: address of hc register to be read
  40. * @mask: bits to look at in result of read
  41. * @done: value of those bits when handshake succeeds
  42. * @usec: timeout in microseconds
  43. *
  44. * Returns negative errno, or zero on success
  45. *
  46. * Success happens when the "mask" bits have the specified value (hardware
  47. * handshake done). There are two failure modes: "usec" have passed (major
  48. * hardware flakeout), or the register reads as all-ones (hardware removed).
  49. */
  50. int xhci_handshake(struct xhci_hcd *xhci, void __iomem *ptr,
  51. u32 mask, u32 done, int usec)
  52. {
  53. u32 result;
  54. do {
  55. result = xhci_readl(xhci, ptr);
  56. if (result == ~(u32)0) /* card removed */
  57. return -ENODEV;
  58. result &= mask;
  59. if (result == done)
  60. return 0;
  61. udelay(1);
  62. usec--;
  63. } while (usec > 0);
  64. return -ETIMEDOUT;
  65. }
  66. /*
  67. * Disable interrupts and begin the xHCI halting process.
  68. */
  69. void xhci_quiesce(struct xhci_hcd *xhci)
  70. {
  71. u32 halted;
  72. u32 cmd;
  73. u32 mask;
  74. mask = ~(XHCI_IRQS);
  75. halted = xhci_readl(xhci, &xhci->op_regs->status) & STS_HALT;
  76. if (!halted)
  77. mask &= ~CMD_RUN;
  78. cmd = xhci_readl(xhci, &xhci->op_regs->command);
  79. cmd &= mask;
  80. xhci_writel(xhci, cmd, &xhci->op_regs->command);
  81. }
  82. /*
  83. * Force HC into halt state.
  84. *
  85. * Disable any IRQs and clear the run/stop bit.
  86. * HC will complete any current and actively pipelined transactions, and
  87. * should halt within 16 ms of the run/stop bit being cleared.
  88. * Read HC Halted bit in the status register to see when the HC is finished.
  89. */
  90. int xhci_halt(struct xhci_hcd *xhci)
  91. {
  92. int ret;
  93. xhci_dbg(xhci, "// Halt the HC\n");
  94. xhci_quiesce(xhci);
  95. ret = xhci_handshake(xhci, &xhci->op_regs->status,
  96. STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
  97. if (!ret) {
  98. xhci->xhc_state |= XHCI_STATE_HALTED;
  99. xhci->cmd_ring_state = CMD_RING_STATE_STOPPED;
  100. } else
  101. xhci_warn(xhci, "Host not halted after %u microseconds.\n",
  102. XHCI_MAX_HALT_USEC);
  103. return ret;
  104. }
  105. /*
  106. * Set the run bit and wait for the host to be running.
  107. */
  108. static int xhci_start(struct xhci_hcd *xhci)
  109. {
  110. u32 temp;
  111. int ret;
  112. temp = xhci_readl(xhci, &xhci->op_regs->command);
  113. temp |= (CMD_RUN);
  114. xhci_dbg(xhci, "// Turn on HC, cmd = 0x%x.\n",
  115. temp);
  116. xhci_writel(xhci, temp, &xhci->op_regs->command);
  117. /*
  118. * Wait for the HCHalted Status bit to be 0 to indicate the host is
  119. * running.
  120. */
  121. ret = xhci_handshake(xhci, &xhci->op_regs->status,
  122. STS_HALT, 0, XHCI_MAX_HALT_USEC);
  123. if (ret == -ETIMEDOUT)
  124. xhci_err(xhci, "Host took too long to start, "
  125. "waited %u microseconds.\n",
  126. XHCI_MAX_HALT_USEC);
  127. if (!ret)
  128. xhci->xhc_state &= ~XHCI_STATE_HALTED;
  129. return ret;
  130. }
  131. /*
  132. * Reset a halted HC.
  133. *
  134. * This resets pipelines, timers, counters, state machines, etc.
  135. * Transactions will be terminated immediately, and operational registers
  136. * will be set to their defaults.
  137. */
  138. int xhci_reset(struct xhci_hcd *xhci)
  139. {
  140. u32 command;
  141. u32 state;
  142. int ret, i;
  143. state = xhci_readl(xhci, &xhci->op_regs->status);
  144. if ((state & STS_HALT) == 0) {
  145. xhci_warn(xhci, "Host controller not halted, aborting reset.\n");
  146. return 0;
  147. }
  148. xhci_dbg(xhci, "// Reset the HC\n");
  149. command = xhci_readl(xhci, &xhci->op_regs->command);
  150. command |= CMD_RESET;
  151. xhci_writel(xhci, command, &xhci->op_regs->command);
  152. ret = xhci_handshake(xhci, &xhci->op_regs->command,
  153. CMD_RESET, 0, 10 * 1000 * 1000);
  154. if (ret)
  155. return ret;
  156. xhci_dbg(xhci, "Wait for controller to be ready for doorbell rings\n");
  157. /*
  158. * xHCI cannot write to any doorbells or operational registers other
  159. * than status until the "Controller Not Ready" flag is cleared.
  160. */
  161. ret = xhci_handshake(xhci, &xhci->op_regs->status,
  162. STS_CNR, 0, 10 * 1000 * 1000);
  163. for (i = 0; i < 2; ++i) {
  164. xhci->bus_state[i].port_c_suspend = 0;
  165. xhci->bus_state[i].suspended_ports = 0;
  166. xhci->bus_state[i].resuming_ports = 0;
  167. }
  168. return ret;
  169. }
  170. #ifdef CONFIG_PCI
  171. static int xhci_free_msi(struct xhci_hcd *xhci)
  172. {
  173. int i;
  174. if (!xhci->msix_entries)
  175. return -EINVAL;
  176. for (i = 0; i < xhci->msix_count; i++)
  177. if (xhci->msix_entries[i].vector)
  178. free_irq(xhci->msix_entries[i].vector,
  179. xhci_to_hcd(xhci));
  180. return 0;
  181. }
  182. /*
  183. * Set up MSI
  184. */
  185. static int xhci_setup_msi(struct xhci_hcd *xhci)
  186. {
  187. int ret;
  188. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  189. ret = pci_enable_msi(pdev);
  190. if (ret) {
  191. xhci_dbg(xhci, "failed to allocate MSI entry\n");
  192. return ret;
  193. }
  194. ret = request_irq(pdev->irq, (irq_handler_t)xhci_msi_irq,
  195. 0, "xhci_hcd", xhci_to_hcd(xhci));
  196. if (ret) {
  197. xhci_dbg(xhci, "disable MSI interrupt\n");
  198. pci_disable_msi(pdev);
  199. }
  200. return ret;
  201. }
  202. /*
  203. * Free IRQs
  204. * free all IRQs request
  205. */
  206. static void xhci_free_irq(struct xhci_hcd *xhci)
  207. {
  208. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  209. int ret;
  210. /* return if using legacy interrupt */
  211. if (xhci_to_hcd(xhci)->irq > 0)
  212. return;
  213. ret = xhci_free_msi(xhci);
  214. if (!ret)
  215. return;
  216. if (pdev->irq > 0)
  217. free_irq(pdev->irq, xhci_to_hcd(xhci));
  218. return;
  219. }
  220. /*
  221. * Set up MSI-X
  222. */
  223. static int xhci_setup_msix(struct xhci_hcd *xhci)
  224. {
  225. int i, ret = 0;
  226. struct usb_hcd *hcd = xhci_to_hcd(xhci);
  227. struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
  228. /*
  229. * calculate number of msi-x vectors supported.
  230. * - HCS_MAX_INTRS: the max number of interrupts the host can handle,
  231. * with max number of interrupters based on the xhci HCSPARAMS1.
  232. * - num_online_cpus: maximum msi-x vectors per CPUs core.
  233. * Add additional 1 vector to ensure always available interrupt.
  234. */
  235. xhci->msix_count = min(num_online_cpus() + 1,
  236. HCS_MAX_INTRS(xhci->hcs_params1));
  237. xhci->msix_entries =
  238. kmalloc((sizeof(struct msix_entry))*xhci->msix_count,
  239. GFP_KERNEL);
  240. if (!xhci->msix_entries) {
  241. xhci_err(xhci, "Failed to allocate MSI-X entries\n");
  242. return -ENOMEM;
  243. }
  244. for (i = 0; i < xhci->msix_count; i++) {
  245. xhci->msix_entries[i].entry = i;
  246. xhci->msix_entries[i].vector = 0;
  247. }
  248. ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count);
  249. if (ret) {
  250. xhci_dbg(xhci, "Failed to enable MSI-X\n");
  251. goto free_entries;
  252. }
  253. for (i = 0; i < xhci->msix_count; i++) {
  254. ret = request_irq(xhci->msix_entries[i].vector,
  255. (irq_handler_t)xhci_msi_irq,
  256. 0, "xhci_hcd", xhci_to_hcd(xhci));
  257. if (ret)
  258. goto disable_msix;
  259. }
  260. hcd->msix_enabled = 1;
  261. return ret;
  262. disable_msix:
  263. xhci_dbg(xhci, "disable MSI-X interrupt\n");
  264. xhci_free_irq(xhci);
  265. pci_disable_msix(pdev);
  266. free_entries:
  267. kfree(xhci->msix_entries);
  268. xhci->msix_entries = NULL;
  269. return ret;
  270. }
  271. /* Free any IRQs and disable MSI-X */
  272. static void xhci_cleanup_msix(struct xhci_hcd *xhci)
  273. {
  274. struct usb_hcd *hcd = xhci_to_hcd(xhci);
  275. struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
  276. xhci_free_irq(xhci);
  277. if (xhci->msix_entries) {
  278. pci_disable_msix(pdev);
  279. kfree(xhci->msix_entries);
  280. xhci->msix_entries = NULL;
  281. } else {
  282. pci_disable_msi(pdev);
  283. }
  284. hcd->msix_enabled = 0;
  285. return;
  286. }
  287. static void xhci_msix_sync_irqs(struct xhci_hcd *xhci)
  288. {
  289. int i;
  290. if (xhci->msix_entries) {
  291. for (i = 0; i < xhci->msix_count; i++)
  292. synchronize_irq(xhci->msix_entries[i].vector);
  293. }
  294. }
  295. static int xhci_try_enable_msi(struct usb_hcd *hcd)
  296. {
  297. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  298. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  299. int ret;
  300. /*
  301. * Some Fresco Logic host controllers advertise MSI, but fail to
  302. * generate interrupts. Don't even try to enable MSI.
  303. */
  304. if (xhci->quirks & XHCI_BROKEN_MSI)
  305. return 0;
  306. /* unregister the legacy interrupt */
  307. if (hcd->irq)
  308. free_irq(hcd->irq, hcd);
  309. hcd->irq = 0;
  310. ret = xhci_setup_msix(xhci);
  311. if (ret)
  312. /* fall back to msi*/
  313. ret = xhci_setup_msi(xhci);
  314. if (!ret)
  315. /* hcd->irq is 0, we have MSI */
  316. return 0;
  317. if (!pdev->irq) {
  318. xhci_err(xhci, "No msi-x/msi found and no IRQ in BIOS\n");
  319. return -EINVAL;
  320. }
  321. /* fall back to legacy interrupt*/
  322. ret = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED,
  323. hcd->irq_descr, hcd);
  324. if (ret) {
  325. xhci_err(xhci, "request interrupt %d failed\n",
  326. pdev->irq);
  327. return ret;
  328. }
  329. hcd->irq = pdev->irq;
  330. return 0;
  331. }
  332. #else
  333. static int xhci_try_enable_msi(struct usb_hcd *hcd)
  334. {
  335. return 0;
  336. }
  337. static void xhci_cleanup_msix(struct xhci_hcd *xhci)
  338. {
  339. }
  340. static void xhci_msix_sync_irqs(struct xhci_hcd *xhci)
  341. {
  342. }
  343. #endif
  344. static void compliance_mode_recovery(unsigned long arg)
  345. {
  346. struct xhci_hcd *xhci;
  347. struct usb_hcd *hcd;
  348. u32 temp;
  349. int i;
  350. xhci = (struct xhci_hcd *)arg;
  351. for (i = 0; i < xhci->num_usb3_ports; i++) {
  352. temp = xhci_readl(xhci, xhci->usb3_ports[i]);
  353. if ((temp & PORT_PLS_MASK) == USB_SS_PORT_LS_COMP_MOD) {
  354. /*
  355. * Compliance Mode Detected. Letting USB Core
  356. * handle the Warm Reset
  357. */
  358. xhci_dbg(xhci, "Compliance Mode Detected->Port %d!\n",
  359. i + 1);
  360. xhci_dbg(xhci, "Attempting Recovery routine!\n");
  361. hcd = xhci->shared_hcd;
  362. if (hcd->state == HC_STATE_SUSPENDED)
  363. usb_hcd_resume_root_hub(hcd);
  364. usb_hcd_poll_rh_status(hcd);
  365. }
  366. }
  367. if (xhci->port_status_u0 != ((1 << xhci->num_usb3_ports)-1))
  368. mod_timer(&xhci->comp_mode_recovery_timer,
  369. jiffies + msecs_to_jiffies(COMP_MODE_RCVRY_MSECS));
  370. }
  371. /*
  372. * Quirk to work around issue generated by the SN65LVPE502CP USB3.0 re-driver
  373. * that causes ports behind that hardware to enter compliance mode sometimes.
  374. * The quirk creates a timer that polls every 2 seconds the link state of
  375. * each host controller's port and recovers it by issuing a Warm reset
  376. * if Compliance mode is detected, otherwise the port will become "dead" (no
  377. * device connections or disconnections will be detected anymore). Becasue no
  378. * status event is generated when entering compliance mode (per xhci spec),
  379. * this quirk is needed on systems that have the failing hardware installed.
  380. */
  381. static void compliance_mode_recovery_timer_init(struct xhci_hcd *xhci)
  382. {
  383. xhci->port_status_u0 = 0;
  384. init_timer(&xhci->comp_mode_recovery_timer);
  385. xhci->comp_mode_recovery_timer.data = (unsigned long) xhci;
  386. xhci->comp_mode_recovery_timer.function = compliance_mode_recovery;
  387. xhci->comp_mode_recovery_timer.expires = jiffies +
  388. msecs_to_jiffies(COMP_MODE_RCVRY_MSECS);
  389. set_timer_slack(&xhci->comp_mode_recovery_timer,
  390. msecs_to_jiffies(COMP_MODE_RCVRY_MSECS));
  391. add_timer(&xhci->comp_mode_recovery_timer);
  392. xhci_dbg(xhci, "Compliance Mode Recovery Timer Initialized.\n");
  393. }
  394. /*
  395. * This function identifies the systems that have installed the SN65LVPE502CP
  396. * USB3.0 re-driver and that need the Compliance Mode Quirk.
  397. * Systems:
  398. * Vendor: Hewlett-Packard -> System Models: Z420, Z620 and Z820
  399. */
  400. static bool compliance_mode_recovery_timer_quirk_check(void)
  401. {
  402. const char *dmi_product_name, *dmi_sys_vendor;
  403. dmi_product_name = dmi_get_system_info(DMI_PRODUCT_NAME);
  404. dmi_sys_vendor = dmi_get_system_info(DMI_SYS_VENDOR);
  405. if (!dmi_product_name || !dmi_sys_vendor)
  406. return false;
  407. if (!(strstr(dmi_sys_vendor, "Hewlett-Packard")))
  408. return false;
  409. if (strstr(dmi_product_name, "Z420") ||
  410. strstr(dmi_product_name, "Z620") ||
  411. strstr(dmi_product_name, "Z820") ||
  412. strstr(dmi_product_name, "Z1 Workstation"))
  413. return true;
  414. return false;
  415. }
  416. static int xhci_all_ports_seen_u0(struct xhci_hcd *xhci)
  417. {
  418. return (xhci->port_status_u0 == ((1 << xhci->num_usb3_ports)-1));
  419. }
  420. /*
  421. * Initialize memory for HCD and xHC (one-time init).
  422. *
  423. * Program the PAGESIZE register, initialize the device context array, create
  424. * device contexts (?), set up a command ring segment (or two?), create event
  425. * ring (one for now).
  426. */
  427. int xhci_init(struct usb_hcd *hcd)
  428. {
  429. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  430. int retval = 0;
  431. xhci_dbg(xhci, "xhci_init\n");
  432. spin_lock_init(&xhci->lock);
  433. if (xhci->hci_version == 0x95 && link_quirk) {
  434. xhci_dbg(xhci, "QUIRK: Not clearing Link TRB chain bits.\n");
  435. xhci->quirks |= XHCI_LINK_TRB_QUIRK;
  436. } else {
  437. xhci_dbg(xhci, "xHCI doesn't need link TRB QUIRK\n");
  438. }
  439. retval = xhci_mem_init(xhci, GFP_KERNEL);
  440. xhci_dbg(xhci, "Finished xhci_init\n");
  441. /* Initializing Compliance Mode Recovery Data If Needed */
  442. if (compliance_mode_recovery_timer_quirk_check()) {
  443. xhci->quirks |= XHCI_COMP_MODE_QUIRK;
  444. compliance_mode_recovery_timer_init(xhci);
  445. }
  446. return retval;
  447. }
  448. /*-------------------------------------------------------------------------*/
  449. #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
  450. static void xhci_event_ring_work(unsigned long arg)
  451. {
  452. unsigned long flags;
  453. int temp;
  454. u64 temp_64;
  455. struct xhci_hcd *xhci = (struct xhci_hcd *) arg;
  456. int i, j;
  457. xhci_dbg(xhci, "Poll event ring: %lu\n", jiffies);
  458. spin_lock_irqsave(&xhci->lock, flags);
  459. temp = xhci_readl(xhci, &xhci->op_regs->status);
  460. xhci_dbg(xhci, "op reg status = 0x%x\n", temp);
  461. if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) ||
  462. (xhci->xhc_state & XHCI_STATE_HALTED)) {
  463. xhci_dbg(xhci, "HW died, polling stopped.\n");
  464. spin_unlock_irqrestore(&xhci->lock, flags);
  465. return;
  466. }
  467. temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  468. xhci_dbg(xhci, "ir_set 0 pending = 0x%x\n", temp);
  469. xhci_dbg(xhci, "HC error bitmask = 0x%x\n", xhci->error_bitmask);
  470. xhci->error_bitmask = 0;
  471. xhci_dbg(xhci, "Event ring:\n");
  472. xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
  473. xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
  474. temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
  475. temp_64 &= ~ERST_PTR_MASK;
  476. xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
  477. xhci_dbg(xhci, "Command ring:\n");
  478. xhci_debug_segment(xhci, xhci->cmd_ring->deq_seg);
  479. xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
  480. xhci_dbg_cmd_ptrs(xhci);
  481. for (i = 0; i < MAX_HC_SLOTS; ++i) {
  482. if (!xhci->devs[i])
  483. continue;
  484. for (j = 0; j < 31; ++j) {
  485. xhci_dbg_ep_rings(xhci, i, j, &xhci->devs[i]->eps[j]);
  486. }
  487. }
  488. spin_unlock_irqrestore(&xhci->lock, flags);
  489. if (!xhci->zombie)
  490. mod_timer(&xhci->event_ring_timer, jiffies + POLL_TIMEOUT * HZ);
  491. else
  492. xhci_dbg(xhci, "Quit polling the event ring.\n");
  493. }
  494. #endif
  495. static int xhci_run_finished(struct xhci_hcd *xhci)
  496. {
  497. if (xhci_start(xhci)) {
  498. xhci_halt(xhci);
  499. return -ENODEV;
  500. }
  501. xhci->shared_hcd->state = HC_STATE_RUNNING;
  502. xhci->cmd_ring_state = CMD_RING_STATE_RUNNING;
  503. if (xhci->quirks & XHCI_NEC_HOST)
  504. xhci_ring_cmd_db(xhci);
  505. xhci_dbg(xhci, "Finished xhci_run for USB3 roothub\n");
  506. return 0;
  507. }
  508. /*
  509. * Start the HC after it was halted.
  510. *
  511. * This function is called by the USB core when the HC driver is added.
  512. * Its opposite is xhci_stop().
  513. *
  514. * xhci_init() must be called once before this function can be called.
  515. * Reset the HC, enable device slot contexts, program DCBAAP, and
  516. * set command ring pointer and event ring pointer.
  517. *
  518. * Setup MSI-X vectors and enable interrupts.
  519. */
  520. int xhci_run(struct usb_hcd *hcd)
  521. {
  522. u32 temp;
  523. u64 temp_64;
  524. int ret;
  525. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  526. /* Start the xHCI host controller running only after the USB 2.0 roothub
  527. * is setup.
  528. */
  529. hcd->uses_new_polling = 1;
  530. if (!usb_hcd_is_primary_hcd(hcd))
  531. return xhci_run_finished(xhci);
  532. xhci_dbg(xhci, "xhci_run\n");
  533. ret = xhci_try_enable_msi(hcd);
  534. if (ret)
  535. return ret;
  536. #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
  537. init_timer(&xhci->event_ring_timer);
  538. xhci->event_ring_timer.data = (unsigned long) xhci;
  539. xhci->event_ring_timer.function = xhci_event_ring_work;
  540. /* Poll the event ring */
  541. xhci->event_ring_timer.expires = jiffies + POLL_TIMEOUT * HZ;
  542. xhci->zombie = 0;
  543. xhci_dbg(xhci, "Setting event ring polling timer\n");
  544. add_timer(&xhci->event_ring_timer);
  545. #endif
  546. xhci_dbg(xhci, "Command ring memory map follows:\n");
  547. xhci_debug_ring(xhci, xhci->cmd_ring);
  548. xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
  549. xhci_dbg_cmd_ptrs(xhci);
  550. xhci_dbg(xhci, "ERST memory map follows:\n");
  551. xhci_dbg_erst(xhci, &xhci->erst);
  552. xhci_dbg(xhci, "Event ring:\n");
  553. xhci_debug_ring(xhci, xhci->event_ring);
  554. xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
  555. temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
  556. temp_64 &= ~ERST_PTR_MASK;
  557. xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
  558. xhci_dbg(xhci, "// Set the interrupt modulation register\n");
  559. temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
  560. temp &= ~ER_IRQ_INTERVAL_MASK;
  561. temp |= (u32) 160;
  562. xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
  563. /* Set the HCD state before we enable the irqs */
  564. temp = xhci_readl(xhci, &xhci->op_regs->command);
  565. temp |= (CMD_EIE);
  566. xhci_dbg(xhci, "// Enable interrupts, cmd = 0x%x.\n",
  567. temp);
  568. xhci_writel(xhci, temp, &xhci->op_regs->command);
  569. temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  570. xhci_dbg(xhci, "// Enabling event ring interrupter %p by writing 0x%x to irq_pending\n",
  571. xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
  572. xhci_writel(xhci, ER_IRQ_ENABLE(temp),
  573. &xhci->ir_set->irq_pending);
  574. xhci_print_ir_set(xhci, 0);
  575. if (xhci->quirks & XHCI_NEC_HOST)
  576. xhci_queue_vendor_command(xhci, 0, 0, 0,
  577. TRB_TYPE(TRB_NEC_GET_FW));
  578. xhci_dbg(xhci, "Finished xhci_run for USB2 roothub\n");
  579. return 0;
  580. }
  581. static void xhci_only_stop_hcd(struct usb_hcd *hcd)
  582. {
  583. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  584. spin_lock_irq(&xhci->lock);
  585. xhci_halt(xhci);
  586. /* The shared_hcd is going to be deallocated shortly (the USB core only
  587. * calls this function when allocation fails in usb_add_hcd(), or
  588. * usb_remove_hcd() is called). So we need to unset xHCI's pointer.
  589. */
  590. xhci->shared_hcd = NULL;
  591. spin_unlock_irq(&xhci->lock);
  592. }
  593. /*
  594. * Stop xHCI driver.
  595. *
  596. * This function is called by the USB core when the HC driver is removed.
  597. * Its opposite is xhci_run().
  598. *
  599. * Disable device contexts, disable IRQs, and quiesce the HC.
  600. * Reset the HC, finish any completed transactions, and cleanup memory.
  601. */
  602. void xhci_stop(struct usb_hcd *hcd)
  603. {
  604. u32 temp;
  605. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  606. if (!usb_hcd_is_primary_hcd(hcd)) {
  607. xhci_only_stop_hcd(xhci->shared_hcd);
  608. return;
  609. }
  610. spin_lock_irq(&xhci->lock);
  611. /* Make sure the xHC is halted for a USB3 roothub
  612. * (xhci_stop() could be called as part of failed init).
  613. */
  614. xhci_halt(xhci);
  615. xhci_reset(xhci);
  616. spin_unlock_irq(&xhci->lock);
  617. xhci_cleanup_msix(xhci);
  618. #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
  619. /* Tell the event ring poll function not to reschedule */
  620. xhci->zombie = 1;
  621. del_timer_sync(&xhci->event_ring_timer);
  622. #endif
  623. /* Deleting Compliance Mode Recovery Timer */
  624. if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
  625. (!(xhci_all_ports_seen_u0(xhci))))
  626. del_timer_sync(&xhci->comp_mode_recovery_timer);
  627. if (xhci->quirks & XHCI_AMD_PLL_FIX)
  628. usb_amd_dev_put();
  629. xhci_dbg(xhci, "// Disabling event ring interrupts\n");
  630. temp = xhci_readl(xhci, &xhci->op_regs->status);
  631. xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
  632. temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  633. xhci_writel(xhci, ER_IRQ_DISABLE(temp),
  634. &xhci->ir_set->irq_pending);
  635. xhci_print_ir_set(xhci, 0);
  636. xhci_dbg(xhci, "cleaning up memory\n");
  637. xhci_mem_cleanup(xhci);
  638. xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
  639. xhci_readl(xhci, &xhci->op_regs->status));
  640. }
  641. /*
  642. * Shutdown HC (not bus-specific)
  643. *
  644. * This is called when the machine is rebooting or halting. We assume that the
  645. * machine will be powered off, and the HC's internal state will be reset.
  646. * Don't bother to free memory.
  647. *
  648. * This will only ever be called with the main usb_hcd (the USB3 roothub).
  649. */
  650. void xhci_shutdown(struct usb_hcd *hcd)
  651. {
  652. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  653. if (xhci->quirks & XHCI_SPURIOUS_REBOOT)
  654. usb_disable_xhci_ports(to_pci_dev(hcd->self.controller));
  655. spin_lock_irq(&xhci->lock);
  656. xhci_halt(xhci);
  657. spin_unlock_irq(&xhci->lock);
  658. xhci_cleanup_msix(xhci);
  659. xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n",
  660. xhci_readl(xhci, &xhci->op_regs->status));
  661. }
  662. #ifdef CONFIG_PM
  663. static void xhci_save_registers(struct xhci_hcd *xhci)
  664. {
  665. xhci->s3.command = xhci_readl(xhci, &xhci->op_regs->command);
  666. xhci->s3.dev_nt = xhci_readl(xhci, &xhci->op_regs->dev_notification);
  667. xhci->s3.dcbaa_ptr = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
  668. xhci->s3.config_reg = xhci_readl(xhci, &xhci->op_regs->config_reg);
  669. xhci->s3.erst_size = xhci_readl(xhci, &xhci->ir_set->erst_size);
  670. xhci->s3.erst_base = xhci_read_64(xhci, &xhci->ir_set->erst_base);
  671. xhci->s3.erst_dequeue = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
  672. xhci->s3.irq_pending = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  673. xhci->s3.irq_control = xhci_readl(xhci, &xhci->ir_set->irq_control);
  674. }
  675. static void xhci_restore_registers(struct xhci_hcd *xhci)
  676. {
  677. xhci_writel(xhci, xhci->s3.command, &xhci->op_regs->command);
  678. xhci_writel(xhci, xhci->s3.dev_nt, &xhci->op_regs->dev_notification);
  679. xhci_write_64(xhci, xhci->s3.dcbaa_ptr, &xhci->op_regs->dcbaa_ptr);
  680. xhci_writel(xhci, xhci->s3.config_reg, &xhci->op_regs->config_reg);
  681. xhci_writel(xhci, xhci->s3.erst_size, &xhci->ir_set->erst_size);
  682. xhci_write_64(xhci, xhci->s3.erst_base, &xhci->ir_set->erst_base);
  683. xhci_write_64(xhci, xhci->s3.erst_dequeue, &xhci->ir_set->erst_dequeue);
  684. xhci_writel(xhci, xhci->s3.irq_pending, &xhci->ir_set->irq_pending);
  685. xhci_writel(xhci, xhci->s3.irq_control, &xhci->ir_set->irq_control);
  686. }
  687. static void xhci_set_cmd_ring_deq(struct xhci_hcd *xhci)
  688. {
  689. u64 val_64;
  690. /* step 2: initialize command ring buffer */
  691. val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
  692. val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
  693. (xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
  694. xhci->cmd_ring->dequeue) &
  695. (u64) ~CMD_RING_RSVD_BITS) |
  696. xhci->cmd_ring->cycle_state;
  697. xhci_dbg(xhci, "// Setting command ring address to 0x%llx\n",
  698. (long unsigned long) val_64);
  699. xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
  700. }
  701. /*
  702. * The whole command ring must be cleared to zero when we suspend the host.
  703. *
  704. * The host doesn't save the command ring pointer in the suspend well, so we
  705. * need to re-program it on resume. Unfortunately, the pointer must be 64-byte
  706. * aligned, because of the reserved bits in the command ring dequeue pointer
  707. * register. Therefore, we can't just set the dequeue pointer back in the
  708. * middle of the ring (TRBs are 16-byte aligned).
  709. */
  710. static void xhci_clear_command_ring(struct xhci_hcd *xhci)
  711. {
  712. struct xhci_ring *ring;
  713. struct xhci_segment *seg;
  714. ring = xhci->cmd_ring;
  715. seg = ring->deq_seg;
  716. do {
  717. memset(seg->trbs, 0,
  718. sizeof(union xhci_trb) * (TRBS_PER_SEGMENT - 1));
  719. seg->trbs[TRBS_PER_SEGMENT - 1].link.control &=
  720. cpu_to_le32(~TRB_CYCLE);
  721. seg = seg->next;
  722. } while (seg != ring->deq_seg);
  723. /* Reset the software enqueue and dequeue pointers */
  724. ring->deq_seg = ring->first_seg;
  725. ring->dequeue = ring->first_seg->trbs;
  726. ring->enq_seg = ring->deq_seg;
  727. ring->enqueue = ring->dequeue;
  728. ring->num_trbs_free = ring->num_segs * (TRBS_PER_SEGMENT - 1) - 1;
  729. /*
  730. * Ring is now zeroed, so the HW should look for change of ownership
  731. * when the cycle bit is set to 1.
  732. */
  733. ring->cycle_state = 1;
  734. /*
  735. * Reset the hardware dequeue pointer.
  736. * Yes, this will need to be re-written after resume, but we're paranoid
  737. * and want to make sure the hardware doesn't access bogus memory
  738. * because, say, the BIOS or an SMI started the host without changing
  739. * the command ring pointers.
  740. */
  741. xhci_set_cmd_ring_deq(xhci);
  742. }
  743. /*
  744. * Stop HC (not bus-specific)
  745. *
  746. * This is called when the machine transition into S3/S4 mode.
  747. *
  748. */
  749. int xhci_suspend(struct xhci_hcd *xhci)
  750. {
  751. int rc = 0;
  752. struct usb_hcd *hcd = xhci_to_hcd(xhci);
  753. u32 command;
  754. spin_lock_irq(&xhci->lock);
  755. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  756. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
  757. /* step 1: stop endpoint */
  758. /* skipped assuming that port suspend has done */
  759. /* step 2: clear Run/Stop bit */
  760. command = xhci_readl(xhci, &xhci->op_regs->command);
  761. command &= ~CMD_RUN;
  762. xhci_writel(xhci, command, &xhci->op_regs->command);
  763. if (xhci_handshake(xhci, &xhci->op_regs->status,
  764. STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC)) {
  765. xhci_warn(xhci, "WARN: xHC CMD_RUN timeout\n");
  766. spin_unlock_irq(&xhci->lock);
  767. return -ETIMEDOUT;
  768. }
  769. xhci_clear_command_ring(xhci);
  770. /* step 3: save registers */
  771. xhci_save_registers(xhci);
  772. /* step 4: set CSS flag */
  773. command = xhci_readl(xhci, &xhci->op_regs->command);
  774. command |= CMD_CSS;
  775. xhci_writel(xhci, command, &xhci->op_regs->command);
  776. if (xhci_handshake(xhci, &xhci->op_regs->status,
  777. STS_SAVE, 0, 10 * 1000)) {
  778. xhci_warn(xhci, "WARN: xHC save state timeout\n");
  779. spin_unlock_irq(&xhci->lock);
  780. return -ETIMEDOUT;
  781. }
  782. spin_unlock_irq(&xhci->lock);
  783. /*
  784. * Deleting Compliance Mode Recovery Timer because the xHCI Host
  785. * is about to be suspended.
  786. */
  787. if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
  788. (!(xhci_all_ports_seen_u0(xhci)))) {
  789. del_timer_sync(&xhci->comp_mode_recovery_timer);
  790. xhci_dbg(xhci, "Compliance Mode Recovery Timer Deleted!\n");
  791. }
  792. /* step 5: remove core well power */
  793. /* synchronize irq when using MSI-X */
  794. xhci_msix_sync_irqs(xhci);
  795. return rc;
  796. }
  797. /*
  798. * start xHC (not bus-specific)
  799. *
  800. * This is called when the machine transition from S3/S4 mode.
  801. *
  802. */
  803. int xhci_resume(struct xhci_hcd *xhci, bool hibernated)
  804. {
  805. u32 command, temp = 0;
  806. struct usb_hcd *hcd = xhci_to_hcd(xhci);
  807. struct usb_hcd *secondary_hcd;
  808. int retval = 0;
  809. /* Wait a bit if either of the roothubs need to settle from the
  810. * transition into bus suspend.
  811. */
  812. if (time_before(jiffies, xhci->bus_state[0].next_statechange) ||
  813. time_before(jiffies,
  814. xhci->bus_state[1].next_statechange))
  815. msleep(100);
  816. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  817. set_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
  818. spin_lock_irq(&xhci->lock);
  819. if (xhci->quirks & XHCI_RESET_ON_RESUME)
  820. hibernated = true;
  821. if (!hibernated) {
  822. /* step 1: restore register */
  823. xhci_restore_registers(xhci);
  824. /* step 2: initialize command ring buffer */
  825. xhci_set_cmd_ring_deq(xhci);
  826. /* step 3: restore state and start state*/
  827. /* step 3: set CRS flag */
  828. command = xhci_readl(xhci, &xhci->op_regs->command);
  829. command |= CMD_CRS;
  830. xhci_writel(xhci, command, &xhci->op_regs->command);
  831. if (xhci_handshake(xhci, &xhci->op_regs->status,
  832. STS_RESTORE, 0, 10 * 1000)) {
  833. xhci_warn(xhci, "WARN: xHC restore state timeout\n");
  834. spin_unlock_irq(&xhci->lock);
  835. return -ETIMEDOUT;
  836. }
  837. temp = xhci_readl(xhci, &xhci->op_regs->status);
  838. }
  839. /* If restore operation fails, re-initialize the HC during resume */
  840. if ((temp & STS_SRE) || hibernated) {
  841. /* Let the USB core know _both_ roothubs lost power. */
  842. usb_root_hub_lost_power(xhci->main_hcd->self.root_hub);
  843. usb_root_hub_lost_power(xhci->shared_hcd->self.root_hub);
  844. xhci_dbg(xhci, "Stop HCD\n");
  845. xhci_halt(xhci);
  846. xhci_reset(xhci);
  847. spin_unlock_irq(&xhci->lock);
  848. xhci_cleanup_msix(xhci);
  849. #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
  850. /* Tell the event ring poll function not to reschedule */
  851. xhci->zombie = 1;
  852. del_timer_sync(&xhci->event_ring_timer);
  853. #endif
  854. xhci_dbg(xhci, "// Disabling event ring interrupts\n");
  855. temp = xhci_readl(xhci, &xhci->op_regs->status);
  856. xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
  857. temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  858. xhci_writel(xhci, ER_IRQ_DISABLE(temp),
  859. &xhci->ir_set->irq_pending);
  860. xhci_print_ir_set(xhci, 0);
  861. xhci_dbg(xhci, "cleaning up memory\n");
  862. xhci_mem_cleanup(xhci);
  863. xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
  864. xhci_readl(xhci, &xhci->op_regs->status));
  865. /* USB core calls the PCI reinit and start functions twice:
  866. * first with the primary HCD, and then with the secondary HCD.
  867. * If we don't do the same, the host will never be started.
  868. */
  869. if (!usb_hcd_is_primary_hcd(hcd))
  870. secondary_hcd = hcd;
  871. else
  872. secondary_hcd = xhci->shared_hcd;
  873. xhci_dbg(xhci, "Initialize the xhci_hcd\n");
  874. retval = xhci_init(hcd->primary_hcd);
  875. if (retval)
  876. return retval;
  877. xhci_dbg(xhci, "Start the primary HCD\n");
  878. retval = xhci_run(hcd->primary_hcd);
  879. if (!retval) {
  880. xhci_dbg(xhci, "Start the secondary HCD\n");
  881. retval = xhci_run(secondary_hcd);
  882. }
  883. hcd->state = HC_STATE_SUSPENDED;
  884. xhci->shared_hcd->state = HC_STATE_SUSPENDED;
  885. goto done;
  886. }
  887. /* step 4: set Run/Stop bit */
  888. command = xhci_readl(xhci, &xhci->op_regs->command);
  889. command |= CMD_RUN;
  890. xhci_writel(xhci, command, &xhci->op_regs->command);
  891. xhci_handshake(xhci, &xhci->op_regs->status, STS_HALT,
  892. 0, 250 * 1000);
  893. /* step 5: walk topology and initialize portsc,
  894. * portpmsc and portli
  895. */
  896. /* this is done in bus_resume */
  897. /* step 6: restart each of the previously
  898. * Running endpoints by ringing their doorbells
  899. */
  900. spin_unlock_irq(&xhci->lock);
  901. done:
  902. if (retval == 0) {
  903. usb_hcd_resume_root_hub(hcd);
  904. usb_hcd_resume_root_hub(xhci->shared_hcd);
  905. }
  906. /*
  907. * If system is subject to the Quirk, Compliance Mode Timer needs to
  908. * be re-initialized Always after a system resume. Ports are subject
  909. * to suffer the Compliance Mode issue again. It doesn't matter if
  910. * ports have entered previously to U0 before system's suspension.
  911. */
  912. if (xhci->quirks & XHCI_COMP_MODE_QUIRK)
  913. compliance_mode_recovery_timer_init(xhci);
  914. return retval;
  915. }
  916. #endif /* CONFIG_PM */
  917. /*-------------------------------------------------------------------------*/
  918. /**
  919. * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
  920. * HCDs. Find the index for an endpoint given its descriptor. Use the return
  921. * value to right shift 1 for the bitmask.
  922. *
  923. * Index = (epnum * 2) + direction - 1,
  924. * where direction = 0 for OUT, 1 for IN.
  925. * For control endpoints, the IN index is used (OUT index is unused), so
  926. * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
  927. */
  928. unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
  929. {
  930. unsigned int index;
  931. if (usb_endpoint_xfer_control(desc))
  932. index = (unsigned int) (usb_endpoint_num(desc)*2);
  933. else
  934. index = (unsigned int) (usb_endpoint_num(desc)*2) +
  935. (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
  936. return index;
  937. }
  938. /* Find the flag for this endpoint (for use in the control context). Use the
  939. * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
  940. * bit 1, etc.
  941. */
  942. unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
  943. {
  944. return 1 << (xhci_get_endpoint_index(desc) + 1);
  945. }
  946. /* Find the flag for this endpoint (for use in the control context). Use the
  947. * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
  948. * bit 1, etc.
  949. */
  950. unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index)
  951. {
  952. return 1 << (ep_index + 1);
  953. }
  954. /* Compute the last valid endpoint context index. Basically, this is the
  955. * endpoint index plus one. For slot contexts with more than valid endpoint,
  956. * we find the most significant bit set in the added contexts flags.
  957. * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
  958. * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
  959. */
  960. unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
  961. {
  962. return fls(added_ctxs) - 1;
  963. }
  964. /* Returns 1 if the arguments are OK;
  965. * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
  966. */
  967. static int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
  968. struct usb_host_endpoint *ep, int check_ep, bool check_virt_dev,
  969. const char *func) {
  970. struct xhci_hcd *xhci;
  971. struct xhci_virt_device *virt_dev;
  972. if (!hcd || (check_ep && !ep) || !udev) {
  973. printk(KERN_DEBUG "xHCI %s called with invalid args\n",
  974. func);
  975. return -EINVAL;
  976. }
  977. if (!udev->parent) {
  978. printk(KERN_DEBUG "xHCI %s called for root hub\n",
  979. func);
  980. return 0;
  981. }
  982. xhci = hcd_to_xhci(hcd);
  983. if (xhci->xhc_state & XHCI_STATE_HALTED)
  984. return -ENODEV;
  985. if (check_virt_dev) {
  986. if (!udev->slot_id || !xhci->devs[udev->slot_id]) {
  987. printk(KERN_DEBUG "xHCI %s called with unaddressed "
  988. "device\n", func);
  989. return -EINVAL;
  990. }
  991. virt_dev = xhci->devs[udev->slot_id];
  992. if (virt_dev->udev != udev) {
  993. printk(KERN_DEBUG "xHCI %s called with udev and "
  994. "virt_dev does not match\n", func);
  995. return -EINVAL;
  996. }
  997. }
  998. return 1;
  999. }
  1000. static int xhci_configure_endpoint(struct xhci_hcd *xhci,
  1001. struct usb_device *udev, struct xhci_command *command,
  1002. bool ctx_change, bool must_succeed);
  1003. /*
  1004. * Full speed devices may have a max packet size greater than 8 bytes, but the
  1005. * USB core doesn't know that until it reads the first 8 bytes of the
  1006. * descriptor. If the usb_device's max packet size changes after that point,
  1007. * we need to issue an evaluate context command and wait on it.
  1008. */
  1009. static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
  1010. unsigned int ep_index, struct urb *urb)
  1011. {
  1012. struct xhci_container_ctx *in_ctx;
  1013. struct xhci_container_ctx *out_ctx;
  1014. struct xhci_input_control_ctx *ctrl_ctx;
  1015. struct xhci_ep_ctx *ep_ctx;
  1016. int max_packet_size;
  1017. int hw_max_packet_size;
  1018. int ret = 0;
  1019. out_ctx = xhci->devs[slot_id]->out_ctx;
  1020. ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
  1021. hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
  1022. max_packet_size = usb_endpoint_maxp(&urb->dev->ep0.desc);
  1023. if (hw_max_packet_size != max_packet_size) {
  1024. xhci_dbg(xhci, "Max Packet Size for ep 0 changed.\n");
  1025. xhci_dbg(xhci, "Max packet size in usb_device = %d\n",
  1026. max_packet_size);
  1027. xhci_dbg(xhci, "Max packet size in xHCI HW = %d\n",
  1028. hw_max_packet_size);
  1029. xhci_dbg(xhci, "Issuing evaluate context command.\n");
  1030. /* Set up the modified control endpoint 0 */
  1031. xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
  1032. xhci->devs[slot_id]->out_ctx, ep_index);
  1033. in_ctx = xhci->devs[slot_id]->in_ctx;
  1034. ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
  1035. ep_ctx->ep_info2 &= cpu_to_le32(~MAX_PACKET_MASK);
  1036. ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size));
  1037. /* Set up the input context flags for the command */
  1038. /* FIXME: This won't work if a non-default control endpoint
  1039. * changes max packet sizes.
  1040. */
  1041. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  1042. ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
  1043. ctrl_ctx->drop_flags = 0;
  1044. xhci_dbg(xhci, "Slot %d input context\n", slot_id);
  1045. xhci_dbg_ctx(xhci, in_ctx, ep_index);
  1046. xhci_dbg(xhci, "Slot %d output context\n", slot_id);
  1047. xhci_dbg_ctx(xhci, out_ctx, ep_index);
  1048. ret = xhci_configure_endpoint(xhci, urb->dev, NULL,
  1049. true, false);
  1050. /* Clean up the input context for later use by bandwidth
  1051. * functions.
  1052. */
  1053. ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
  1054. }
  1055. return ret;
  1056. }
  1057. /*
  1058. * non-error returns are a promise to giveback() the urb later
  1059. * we drop ownership so next owner (or urb unlink) can get it
  1060. */
  1061. int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
  1062. {
  1063. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1064. struct xhci_td *buffer;
  1065. unsigned long flags;
  1066. int ret = 0;
  1067. unsigned int slot_id, ep_index;
  1068. struct urb_priv *urb_priv;
  1069. int size, i;
  1070. if (!urb || xhci_check_args(hcd, urb->dev, urb->ep,
  1071. true, true, __func__) <= 0)
  1072. return -EINVAL;
  1073. slot_id = urb->dev->slot_id;
  1074. ep_index = xhci_get_endpoint_index(&urb->ep->desc);
  1075. if (!HCD_HW_ACCESSIBLE(hcd)) {
  1076. if (!in_interrupt())
  1077. xhci_dbg(xhci, "urb submitted during PCI suspend\n");
  1078. ret = -ESHUTDOWN;
  1079. goto exit;
  1080. }
  1081. if (usb_endpoint_xfer_isoc(&urb->ep->desc))
  1082. size = urb->number_of_packets;
  1083. else
  1084. size = 1;
  1085. urb_priv = kzalloc(sizeof(struct urb_priv) +
  1086. size * sizeof(struct xhci_td *), mem_flags);
  1087. if (!urb_priv)
  1088. return -ENOMEM;
  1089. buffer = kzalloc(size * sizeof(struct xhci_td), mem_flags);
  1090. if (!buffer) {
  1091. kfree(urb_priv);
  1092. return -ENOMEM;
  1093. }
  1094. for (i = 0; i < size; i++) {
  1095. urb_priv->td[i] = buffer;
  1096. buffer++;
  1097. }
  1098. urb_priv->length = size;
  1099. urb_priv->td_cnt = 0;
  1100. urb->hcpriv = urb_priv;
  1101. if (usb_endpoint_xfer_control(&urb->ep->desc)) {
  1102. /* Check to see if the max packet size for the default control
  1103. * endpoint changed during FS device enumeration
  1104. */
  1105. if (urb->dev->speed == USB_SPEED_FULL) {
  1106. ret = xhci_check_maxpacket(xhci, slot_id,
  1107. ep_index, urb);
  1108. if (ret < 0) {
  1109. xhci_urb_free_priv(xhci, urb_priv);
  1110. urb->hcpriv = NULL;
  1111. return ret;
  1112. }
  1113. }
  1114. /* We have a spinlock and interrupts disabled, so we must pass
  1115. * atomic context to this function, which may allocate memory.
  1116. */
  1117. spin_lock_irqsave(&xhci->lock, flags);
  1118. if (xhci->xhc_state & XHCI_STATE_DYING)
  1119. goto dying;
  1120. ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
  1121. slot_id, ep_index);
  1122. if (ret)
  1123. goto free_priv;
  1124. spin_unlock_irqrestore(&xhci->lock, flags);
  1125. } else if (usb_endpoint_xfer_bulk(&urb->ep->desc)) {
  1126. spin_lock_irqsave(&xhci->lock, flags);
  1127. if (xhci->xhc_state & XHCI_STATE_DYING)
  1128. goto dying;
  1129. if (xhci->devs[slot_id]->eps[ep_index].ep_state &
  1130. EP_GETTING_STREAMS) {
  1131. xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
  1132. "is transitioning to using streams.\n");
  1133. ret = -EINVAL;
  1134. } else if (xhci->devs[slot_id]->eps[ep_index].ep_state &
  1135. EP_GETTING_NO_STREAMS) {
  1136. xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
  1137. "is transitioning to "
  1138. "not having streams.\n");
  1139. ret = -EINVAL;
  1140. } else {
  1141. ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
  1142. slot_id, ep_index);
  1143. }
  1144. if (ret)
  1145. goto free_priv;
  1146. spin_unlock_irqrestore(&xhci->lock, flags);
  1147. } else if (usb_endpoint_xfer_int(&urb->ep->desc)) {
  1148. spin_lock_irqsave(&xhci->lock, flags);
  1149. if (xhci->xhc_state & XHCI_STATE_DYING)
  1150. goto dying;
  1151. ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
  1152. slot_id, ep_index);
  1153. if (ret)
  1154. goto free_priv;
  1155. spin_unlock_irqrestore(&xhci->lock, flags);
  1156. } else {
  1157. spin_lock_irqsave(&xhci->lock, flags);
  1158. if (xhci->xhc_state & XHCI_STATE_DYING)
  1159. goto dying;
  1160. ret = xhci_queue_isoc_tx_prepare(xhci, GFP_ATOMIC, urb,
  1161. slot_id, ep_index);
  1162. if (ret)
  1163. goto free_priv;
  1164. spin_unlock_irqrestore(&xhci->lock, flags);
  1165. }
  1166. exit:
  1167. return ret;
  1168. dying:
  1169. xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for "
  1170. "non-responsive xHCI host.\n",
  1171. urb->ep->desc.bEndpointAddress, urb);
  1172. ret = -ESHUTDOWN;
  1173. free_priv:
  1174. xhci_urb_free_priv(xhci, urb_priv);
  1175. urb->hcpriv = NULL;
  1176. spin_unlock_irqrestore(&xhci->lock, flags);
  1177. return ret;
  1178. }
  1179. /* Get the right ring for the given URB.
  1180. * If the endpoint supports streams, boundary check the URB's stream ID.
  1181. * If the endpoint doesn't support streams, return the singular endpoint ring.
  1182. */
  1183. static struct xhci_ring *xhci_urb_to_transfer_ring(struct xhci_hcd *xhci,
  1184. struct urb *urb)
  1185. {
  1186. unsigned int slot_id;
  1187. unsigned int ep_index;
  1188. unsigned int stream_id;
  1189. struct xhci_virt_ep *ep;
  1190. slot_id = urb->dev->slot_id;
  1191. ep_index = xhci_get_endpoint_index(&urb->ep->desc);
  1192. stream_id = urb->stream_id;
  1193. ep = &xhci->devs[slot_id]->eps[ep_index];
  1194. /* Common case: no streams */
  1195. if (!(ep->ep_state & EP_HAS_STREAMS))
  1196. return ep->ring;
  1197. if (stream_id == 0) {
  1198. xhci_warn(xhci,
  1199. "WARN: Slot ID %u, ep index %u has streams, "
  1200. "but URB has no stream ID.\n",
  1201. slot_id, ep_index);
  1202. return NULL;
  1203. }
  1204. if (stream_id < ep->stream_info->num_streams)
  1205. return ep->stream_info->stream_rings[stream_id];
  1206. xhci_warn(xhci,
  1207. "WARN: Slot ID %u, ep index %u has "
  1208. "stream IDs 1 to %u allocated, "
  1209. "but stream ID %u is requested.\n",
  1210. slot_id, ep_index,
  1211. ep->stream_info->num_streams - 1,
  1212. stream_id);
  1213. return NULL;
  1214. }
  1215. /*
  1216. * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
  1217. * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
  1218. * should pick up where it left off in the TD, unless a Set Transfer Ring
  1219. * Dequeue Pointer is issued.
  1220. *
  1221. * The TRBs that make up the buffers for the canceled URB will be "removed" from
  1222. * the ring. Since the ring is a contiguous structure, they can't be physically
  1223. * removed. Instead, there are two options:
  1224. *
  1225. * 1) If the HC is in the middle of processing the URB to be canceled, we
  1226. * simply move the ring's dequeue pointer past those TRBs using the Set
  1227. * Transfer Ring Dequeue Pointer command. This will be the common case,
  1228. * when drivers timeout on the last submitted URB and attempt to cancel.
  1229. *
  1230. * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
  1231. * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
  1232. * HC will need to invalidate the any TRBs it has cached after the stop
  1233. * endpoint command, as noted in the xHCI 0.95 errata.
  1234. *
  1235. * 3) The TD may have completed by the time the Stop Endpoint Command
  1236. * completes, so software needs to handle that case too.
  1237. *
  1238. * This function should protect against the TD enqueueing code ringing the
  1239. * doorbell while this code is waiting for a Stop Endpoint command to complete.
  1240. * It also needs to account for multiple cancellations on happening at the same
  1241. * time for the same endpoint.
  1242. *
  1243. * Note that this function can be called in any context, or so says
  1244. * usb_hcd_unlink_urb()
  1245. */
  1246. int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  1247. {
  1248. unsigned long flags;
  1249. int ret, i;
  1250. u32 temp;
  1251. struct xhci_hcd *xhci;
  1252. struct urb_priv *urb_priv;
  1253. struct xhci_td *td;
  1254. unsigned int ep_index;
  1255. struct xhci_ring *ep_ring;
  1256. struct xhci_virt_ep *ep;
  1257. xhci = hcd_to_xhci(hcd);
  1258. spin_lock_irqsave(&xhci->lock, flags);
  1259. /* Make sure the URB hasn't completed or been unlinked already */
  1260. ret = usb_hcd_check_unlink_urb(hcd, urb, status);
  1261. if (ret || !urb->hcpriv)
  1262. goto done;
  1263. temp = xhci_readl(xhci, &xhci->op_regs->status);
  1264. if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_HALTED)) {
  1265. xhci_dbg(xhci, "HW died, freeing TD.\n");
  1266. urb_priv = urb->hcpriv;
  1267. for (i = urb_priv->td_cnt; i < urb_priv->length; i++) {
  1268. td = urb_priv->td[i];
  1269. if (!list_empty(&td->td_list))
  1270. list_del_init(&td->td_list);
  1271. if (!list_empty(&td->cancelled_td_list))
  1272. list_del_init(&td->cancelled_td_list);
  1273. }
  1274. usb_hcd_unlink_urb_from_ep(hcd, urb);
  1275. spin_unlock_irqrestore(&xhci->lock, flags);
  1276. usb_hcd_giveback_urb(hcd, urb, -ESHUTDOWN);
  1277. xhci_urb_free_priv(xhci, urb_priv);
  1278. return ret;
  1279. }
  1280. if ((xhci->xhc_state & XHCI_STATE_DYING) ||
  1281. (xhci->xhc_state & XHCI_STATE_HALTED)) {
  1282. xhci_dbg(xhci, "Ep 0x%x: URB %p to be canceled on "
  1283. "non-responsive xHCI host.\n",
  1284. urb->ep->desc.bEndpointAddress, urb);
  1285. /* Let the stop endpoint command watchdog timer (which set this
  1286. * state) finish cleaning up the endpoint TD lists. We must
  1287. * have caught it in the middle of dropping a lock and giving
  1288. * back an URB.
  1289. */
  1290. goto done;
  1291. }
  1292. ep_index = xhci_get_endpoint_index(&urb->ep->desc);
  1293. ep = &xhci->devs[urb->dev->slot_id]->eps[ep_index];
  1294. ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
  1295. if (!ep_ring) {
  1296. ret = -EINVAL;
  1297. goto done;
  1298. }
  1299. urb_priv = urb->hcpriv;
  1300. i = urb_priv->td_cnt;
  1301. if (i < urb_priv->length)
  1302. xhci_dbg(xhci, "Cancel URB %p, dev %s, ep 0x%x, "
  1303. "starting at offset 0x%llx\n",
  1304. urb, urb->dev->devpath,
  1305. urb->ep->desc.bEndpointAddress,
  1306. (unsigned long long) xhci_trb_virt_to_dma(
  1307. urb_priv->td[i]->start_seg,
  1308. urb_priv->td[i]->first_trb));
  1309. for (; i < urb_priv->length; i++) {
  1310. td = urb_priv->td[i];
  1311. list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
  1312. }
  1313. /* Queue a stop endpoint command, but only if this is
  1314. * the first cancellation to be handled.
  1315. */
  1316. if (!(ep->ep_state & EP_HALT_PENDING)) {
  1317. ep->ep_state |= EP_HALT_PENDING;
  1318. ep->stop_cmds_pending++;
  1319. ep->stop_cmd_timer.expires = jiffies +
  1320. XHCI_STOP_EP_CMD_TIMEOUT * HZ;
  1321. add_timer(&ep->stop_cmd_timer);
  1322. xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index, 0);
  1323. xhci_ring_cmd_db(xhci);
  1324. }
  1325. done:
  1326. spin_unlock_irqrestore(&xhci->lock, flags);
  1327. return ret;
  1328. }
  1329. /* Drop an endpoint from a new bandwidth configuration for this device.
  1330. * Only one call to this function is allowed per endpoint before
  1331. * check_bandwidth() or reset_bandwidth() must be called.
  1332. * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
  1333. * add the endpoint to the schedule with possibly new parameters denoted by a
  1334. * different endpoint descriptor in usb_host_endpoint.
  1335. * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
  1336. * not allowed.
  1337. *
  1338. * The USB core will not allow URBs to be queued to an endpoint that is being
  1339. * disabled, so there's no need for mutual exclusion to protect
  1340. * the xhci->devs[slot_id] structure.
  1341. */
  1342. int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
  1343. struct usb_host_endpoint *ep)
  1344. {
  1345. struct xhci_hcd *xhci;
  1346. struct xhci_container_ctx *in_ctx, *out_ctx;
  1347. struct xhci_input_control_ctx *ctrl_ctx;
  1348. struct xhci_slot_ctx *slot_ctx;
  1349. unsigned int last_ctx;
  1350. unsigned int ep_index;
  1351. struct xhci_ep_ctx *ep_ctx;
  1352. u32 drop_flag;
  1353. u32 new_add_flags, new_drop_flags, new_slot_info;
  1354. int ret;
  1355. ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
  1356. if (ret <= 0)
  1357. return ret;
  1358. xhci = hcd_to_xhci(hcd);
  1359. if (xhci->xhc_state & XHCI_STATE_DYING)
  1360. return -ENODEV;
  1361. xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
  1362. drop_flag = xhci_get_endpoint_flag(&ep->desc);
  1363. if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
  1364. xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
  1365. __func__, drop_flag);
  1366. return 0;
  1367. }
  1368. in_ctx = xhci->devs[udev->slot_id]->in_ctx;
  1369. out_ctx = xhci->devs[udev->slot_id]->out_ctx;
  1370. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  1371. ep_index = xhci_get_endpoint_index(&ep->desc);
  1372. ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
  1373. /* If the HC already knows the endpoint is disabled,
  1374. * or the HCD has noted it is disabled, ignore this request
  1375. */
  1376. if (((ep_ctx->ep_info & cpu_to_le32(EP_STATE_MASK)) ==
  1377. cpu_to_le32(EP_STATE_DISABLED)) ||
  1378. le32_to_cpu(ctrl_ctx->drop_flags) &
  1379. xhci_get_endpoint_flag(&ep->desc)) {
  1380. xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
  1381. __func__, ep);
  1382. return 0;
  1383. }
  1384. ctrl_ctx->drop_flags |= cpu_to_le32(drop_flag);
  1385. new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
  1386. ctrl_ctx->add_flags &= cpu_to_le32(~drop_flag);
  1387. new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
  1388. last_ctx = xhci_last_valid_endpoint(le32_to_cpu(ctrl_ctx->add_flags));
  1389. slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
  1390. /* Update the last valid endpoint context, if we deleted the last one */
  1391. if ((le32_to_cpu(slot_ctx->dev_info) & LAST_CTX_MASK) >
  1392. LAST_CTX(last_ctx)) {
  1393. slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
  1394. slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(last_ctx));
  1395. }
  1396. new_slot_info = le32_to_cpu(slot_ctx->dev_info);
  1397. xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
  1398. xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
  1399. (unsigned int) ep->desc.bEndpointAddress,
  1400. udev->slot_id,
  1401. (unsigned int) new_drop_flags,
  1402. (unsigned int) new_add_flags,
  1403. (unsigned int) new_slot_info);
  1404. return 0;
  1405. }
  1406. /* Add an endpoint to a new possible bandwidth configuration for this device.
  1407. * Only one call to this function is allowed per endpoint before
  1408. * check_bandwidth() or reset_bandwidth() must be called.
  1409. * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
  1410. * add the endpoint to the schedule with possibly new parameters denoted by a
  1411. * different endpoint descriptor in usb_host_endpoint.
  1412. * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
  1413. * not allowed.
  1414. *
  1415. * The USB core will not allow URBs to be queued to an endpoint until the
  1416. * configuration or alt setting is installed in the device, so there's no need
  1417. * for mutual exclusion to protect the xhci->devs[slot_id] structure.
  1418. */
  1419. int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
  1420. struct usb_host_endpoint *ep)
  1421. {
  1422. struct xhci_hcd *xhci;
  1423. struct xhci_container_ctx *in_ctx, *out_ctx;
  1424. unsigned int ep_index;
  1425. struct xhci_slot_ctx *slot_ctx;
  1426. struct xhci_input_control_ctx *ctrl_ctx;
  1427. u32 added_ctxs;
  1428. unsigned int last_ctx;
  1429. u32 new_add_flags, new_drop_flags, new_slot_info;
  1430. struct xhci_virt_device *virt_dev;
  1431. int ret = 0;
  1432. ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
  1433. if (ret <= 0) {
  1434. /* So we won't queue a reset ep command for a root hub */
  1435. ep->hcpriv = NULL;
  1436. return ret;
  1437. }
  1438. xhci = hcd_to_xhci(hcd);
  1439. if (xhci->xhc_state & XHCI_STATE_DYING)
  1440. return -ENODEV;
  1441. added_ctxs = xhci_get_endpoint_flag(&ep->desc);
  1442. last_ctx = xhci_last_valid_endpoint(added_ctxs);
  1443. if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
  1444. /* FIXME when we have to issue an evaluate endpoint command to
  1445. * deal with ep0 max packet size changing once we get the
  1446. * descriptors
  1447. */
  1448. xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
  1449. __func__, added_ctxs);
  1450. return 0;
  1451. }
  1452. virt_dev = xhci->devs[udev->slot_id];
  1453. in_ctx = virt_dev->in_ctx;
  1454. out_ctx = virt_dev->out_ctx;
  1455. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  1456. ep_index = xhci_get_endpoint_index(&ep->desc);
  1457. /* If this endpoint is already in use, and the upper layers are trying
  1458. * to add it again without dropping it, reject the addition.
  1459. */
  1460. if (virt_dev->eps[ep_index].ring &&
  1461. !(le32_to_cpu(ctrl_ctx->drop_flags) &
  1462. xhci_get_endpoint_flag(&ep->desc))) {
  1463. xhci_warn(xhci, "Trying to add endpoint 0x%x "
  1464. "without dropping it.\n",
  1465. (unsigned int) ep->desc.bEndpointAddress);
  1466. return -EINVAL;
  1467. }
  1468. /* If the HCD has already noted the endpoint is enabled,
  1469. * ignore this request.
  1470. */
  1471. if (le32_to_cpu(ctrl_ctx->add_flags) &
  1472. xhci_get_endpoint_flag(&ep->desc)) {
  1473. xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
  1474. __func__, ep);
  1475. return 0;
  1476. }
  1477. /*
  1478. * Configuration and alternate setting changes must be done in
  1479. * process context, not interrupt context (or so documenation
  1480. * for usb_set_interface() and usb_set_configuration() claim).
  1481. */
  1482. if (xhci_endpoint_init(xhci, virt_dev, udev, ep, GFP_NOIO) < 0) {
  1483. dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
  1484. __func__, ep->desc.bEndpointAddress);
  1485. return -ENOMEM;
  1486. }
  1487. ctrl_ctx->add_flags |= cpu_to_le32(added_ctxs);
  1488. new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
  1489. /* If xhci_endpoint_disable() was called for this endpoint, but the
  1490. * xHC hasn't been notified yet through the check_bandwidth() call,
  1491. * this re-adds a new state for the endpoint from the new endpoint
  1492. * descriptors. We must drop and re-add this endpoint, so we leave the
  1493. * drop flags alone.
  1494. */
  1495. new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
  1496. slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
  1497. /* Update the last valid endpoint context, if we just added one past */
  1498. if ((le32_to_cpu(slot_ctx->dev_info) & LAST_CTX_MASK) <
  1499. LAST_CTX(last_ctx)) {
  1500. slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
  1501. slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(last_ctx));
  1502. }
  1503. new_slot_info = le32_to_cpu(slot_ctx->dev_info);
  1504. /* Store the usb_device pointer for later use */
  1505. ep->hcpriv = udev;
  1506. xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
  1507. (unsigned int) ep->desc.bEndpointAddress,
  1508. udev->slot_id,
  1509. (unsigned int) new_drop_flags,
  1510. (unsigned int) new_add_flags,
  1511. (unsigned int) new_slot_info);
  1512. return 0;
  1513. }
  1514. static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev)
  1515. {
  1516. struct xhci_input_control_ctx *ctrl_ctx;
  1517. struct xhci_ep_ctx *ep_ctx;
  1518. struct xhci_slot_ctx *slot_ctx;
  1519. int i;
  1520. /* When a device's add flag and drop flag are zero, any subsequent
  1521. * configure endpoint command will leave that endpoint's state
  1522. * untouched. Make sure we don't leave any old state in the input
  1523. * endpoint contexts.
  1524. */
  1525. ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
  1526. ctrl_ctx->drop_flags = 0;
  1527. ctrl_ctx->add_flags = 0;
  1528. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
  1529. slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
  1530. /* Endpoint 0 is always valid */
  1531. slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
  1532. for (i = 1; i < 31; ++i) {
  1533. ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
  1534. ep_ctx->ep_info = 0;
  1535. ep_ctx->ep_info2 = 0;
  1536. ep_ctx->deq = 0;
  1537. ep_ctx->tx_info = 0;
  1538. }
  1539. }
  1540. static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
  1541. struct usb_device *udev, u32 *cmd_status)
  1542. {
  1543. int ret;
  1544. switch (*cmd_status) {
  1545. case COMP_ENOMEM:
  1546. dev_warn(&udev->dev, "Not enough host controller resources "
  1547. "for new device state.\n");
  1548. ret = -ENOMEM;
  1549. /* FIXME: can we allocate more resources for the HC? */
  1550. break;
  1551. case COMP_BW_ERR:
  1552. case COMP_2ND_BW_ERR:
  1553. dev_warn(&udev->dev, "Not enough bandwidth "
  1554. "for new device state.\n");
  1555. ret = -ENOSPC;
  1556. /* FIXME: can we go back to the old state? */
  1557. break;
  1558. case COMP_TRB_ERR:
  1559. /* the HCD set up something wrong */
  1560. dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
  1561. "add flag = 1, "
  1562. "and endpoint is not disabled.\n");
  1563. ret = -EINVAL;
  1564. break;
  1565. case COMP_DEV_ERR:
  1566. dev_warn(&udev->dev, "ERROR: Incompatible device for endpoint "
  1567. "configure command.\n");
  1568. ret = -ENODEV;
  1569. break;
  1570. case COMP_SUCCESS:
  1571. dev_dbg(&udev->dev, "Successful Endpoint Configure command\n");
  1572. ret = 0;
  1573. break;
  1574. default:
  1575. xhci_err(xhci, "ERROR: unexpected command completion "
  1576. "code 0x%x.\n", *cmd_status);
  1577. ret = -EINVAL;
  1578. break;
  1579. }
  1580. return ret;
  1581. }
  1582. static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
  1583. struct usb_device *udev, u32 *cmd_status)
  1584. {
  1585. int ret;
  1586. struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
  1587. switch (*cmd_status) {
  1588. case COMP_EINVAL:
  1589. dev_warn(&udev->dev, "WARN: xHCI driver setup invalid evaluate "
  1590. "context command.\n");
  1591. ret = -EINVAL;
  1592. break;
  1593. case COMP_EBADSLT:
  1594. dev_warn(&udev->dev, "WARN: slot not enabled for"
  1595. "evaluate context command.\n");
  1596. ret = -EINVAL;
  1597. break;
  1598. case COMP_CTX_STATE:
  1599. dev_warn(&udev->dev, "WARN: invalid context state for "
  1600. "evaluate context command.\n");
  1601. xhci_dbg_ctx(xhci, virt_dev->out_ctx, 1);
  1602. ret = -EINVAL;
  1603. break;
  1604. case COMP_DEV_ERR:
  1605. dev_warn(&udev->dev, "ERROR: Incompatible device for evaluate "
  1606. "context command.\n");
  1607. ret = -ENODEV;
  1608. break;
  1609. case COMP_MEL_ERR:
  1610. /* Max Exit Latency too large error */
  1611. dev_warn(&udev->dev, "WARN: Max Exit Latency too large\n");
  1612. ret = -EINVAL;
  1613. break;
  1614. case COMP_SUCCESS:
  1615. dev_dbg(&udev->dev, "Successful evaluate context command\n");
  1616. ret = 0;
  1617. break;
  1618. default:
  1619. xhci_err(xhci, "ERROR: unexpected command completion "
  1620. "code 0x%x.\n", *cmd_status);
  1621. ret = -EINVAL;
  1622. break;
  1623. }
  1624. return ret;
  1625. }
  1626. static u32 xhci_count_num_new_endpoints(struct xhci_hcd *xhci,
  1627. struct xhci_container_ctx *in_ctx)
  1628. {
  1629. struct xhci_input_control_ctx *ctrl_ctx;
  1630. u32 valid_add_flags;
  1631. u32 valid_drop_flags;
  1632. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  1633. /* Ignore the slot flag (bit 0), and the default control endpoint flag
  1634. * (bit 1). The default control endpoint is added during the Address
  1635. * Device command and is never removed until the slot is disabled.
  1636. */
  1637. valid_add_flags = ctrl_ctx->add_flags >> 2;
  1638. valid_drop_flags = ctrl_ctx->drop_flags >> 2;
  1639. /* Use hweight32 to count the number of ones in the add flags, or
  1640. * number of endpoints added. Don't count endpoints that are changed
  1641. * (both added and dropped).
  1642. */
  1643. return hweight32(valid_add_flags) -
  1644. hweight32(valid_add_flags & valid_drop_flags);
  1645. }
  1646. static unsigned int xhci_count_num_dropped_endpoints(struct xhci_hcd *xhci,
  1647. struct xhci_container_ctx *in_ctx)
  1648. {
  1649. struct xhci_input_control_ctx *ctrl_ctx;
  1650. u32 valid_add_flags;
  1651. u32 valid_drop_flags;
  1652. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  1653. valid_add_flags = ctrl_ctx->add_flags >> 2;
  1654. valid_drop_flags = ctrl_ctx->drop_flags >> 2;
  1655. return hweight32(valid_drop_flags) -
  1656. hweight32(valid_add_flags & valid_drop_flags);
  1657. }
  1658. /*
  1659. * We need to reserve the new number of endpoints before the configure endpoint
  1660. * command completes. We can't subtract the dropped endpoints from the number
  1661. * of active endpoints until the command completes because we can oversubscribe
  1662. * the host in this case:
  1663. *
  1664. * - the first configure endpoint command drops more endpoints than it adds
  1665. * - a second configure endpoint command that adds more endpoints is queued
  1666. * - the first configure endpoint command fails, so the config is unchanged
  1667. * - the second command may succeed, even though there isn't enough resources
  1668. *
  1669. * Must be called with xhci->lock held.
  1670. */
  1671. static int xhci_reserve_host_resources(struct xhci_hcd *xhci,
  1672. struct xhci_container_ctx *in_ctx)
  1673. {
  1674. u32 added_eps;
  1675. added_eps = xhci_count_num_new_endpoints(xhci, in_ctx);
  1676. if (xhci->num_active_eps + added_eps > xhci->limit_active_eps) {
  1677. xhci_dbg(xhci, "Not enough ep ctxs: "
  1678. "%u active, need to add %u, limit is %u.\n",
  1679. xhci->num_active_eps, added_eps,
  1680. xhci->limit_active_eps);
  1681. return -ENOMEM;
  1682. }
  1683. xhci->num_active_eps += added_eps;
  1684. xhci_dbg(xhci, "Adding %u ep ctxs, %u now active.\n", added_eps,
  1685. xhci->num_active_eps);
  1686. return 0;
  1687. }
  1688. /*
  1689. * The configure endpoint was failed by the xHC for some other reason, so we
  1690. * need to revert the resources that failed configuration would have used.
  1691. *
  1692. * Must be called with xhci->lock held.
  1693. */
  1694. static void xhci_free_host_resources(struct xhci_hcd *xhci,
  1695. struct xhci_container_ctx *in_ctx)
  1696. {
  1697. u32 num_failed_eps;
  1698. num_failed_eps = xhci_count_num_new_endpoints(xhci, in_ctx);
  1699. xhci->num_active_eps -= num_failed_eps;
  1700. xhci_dbg(xhci, "Removing %u failed ep ctxs, %u now active.\n",
  1701. num_failed_eps,
  1702. xhci->num_active_eps);
  1703. }
  1704. /*
  1705. * Now that the command has completed, clean up the active endpoint count by
  1706. * subtracting out the endpoints that were dropped (but not changed).
  1707. *
  1708. * Must be called with xhci->lock held.
  1709. */
  1710. static void xhci_finish_resource_reservation(struct xhci_hcd *xhci,
  1711. struct xhci_container_ctx *in_ctx)
  1712. {
  1713. u32 num_dropped_eps;
  1714. num_dropped_eps = xhci_count_num_dropped_endpoints(xhci, in_ctx);
  1715. xhci->num_active_eps -= num_dropped_eps;
  1716. if (num_dropped_eps)
  1717. xhci_dbg(xhci, "Removing %u dropped ep ctxs, %u now active.\n",
  1718. num_dropped_eps,
  1719. xhci->num_active_eps);
  1720. }
  1721. static unsigned int xhci_get_block_size(struct usb_device *udev)
  1722. {
  1723. switch (udev->speed) {
  1724. case USB_SPEED_LOW:
  1725. case USB_SPEED_FULL:
  1726. return FS_BLOCK;
  1727. case USB_SPEED_HIGH:
  1728. return HS_BLOCK;
  1729. case USB_SPEED_SUPER:
  1730. return SS_BLOCK;
  1731. case USB_SPEED_UNKNOWN:
  1732. case USB_SPEED_WIRELESS:
  1733. default:
  1734. /* Should never happen */
  1735. return 1;
  1736. }
  1737. }
  1738. static unsigned int
  1739. xhci_get_largest_overhead(struct xhci_interval_bw *interval_bw)
  1740. {
  1741. if (interval_bw->overhead[LS_OVERHEAD_TYPE])
  1742. return LS_OVERHEAD;
  1743. if (interval_bw->overhead[FS_OVERHEAD_TYPE])
  1744. return FS_OVERHEAD;
  1745. return HS_OVERHEAD;
  1746. }
  1747. /* If we are changing a LS/FS device under a HS hub,
  1748. * make sure (if we are activating a new TT) that the HS bus has enough
  1749. * bandwidth for this new TT.
  1750. */
  1751. static int xhci_check_tt_bw_table(struct xhci_hcd *xhci,
  1752. struct xhci_virt_device *virt_dev,
  1753. int old_active_eps)
  1754. {
  1755. struct xhci_interval_bw_table *bw_table;
  1756. struct xhci_tt_bw_info *tt_info;
  1757. /* Find the bandwidth table for the root port this TT is attached to. */
  1758. bw_table = &xhci->rh_bw[virt_dev->real_port - 1].bw_table;
  1759. tt_info = virt_dev->tt_info;
  1760. /* If this TT already had active endpoints, the bandwidth for this TT
  1761. * has already been added. Removing all periodic endpoints (and thus
  1762. * making the TT enactive) will only decrease the bandwidth used.
  1763. */
  1764. if (old_active_eps)
  1765. return 0;
  1766. if (old_active_eps == 0 && tt_info->active_eps != 0) {
  1767. if (bw_table->bw_used + TT_HS_OVERHEAD > HS_BW_LIMIT)
  1768. return -ENOMEM;
  1769. return 0;
  1770. }
  1771. /* Not sure why we would have no new active endpoints...
  1772. *
  1773. * Maybe because of an Evaluate Context change for a hub update or a
  1774. * control endpoint 0 max packet size change?
  1775. * FIXME: skip the bandwidth calculation in that case.
  1776. */
  1777. return 0;
  1778. }
  1779. static int xhci_check_ss_bw(struct xhci_hcd *xhci,
  1780. struct xhci_virt_device *virt_dev)
  1781. {
  1782. unsigned int bw_reserved;
  1783. bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_IN, 100);
  1784. if (virt_dev->bw_table->ss_bw_in > (SS_BW_LIMIT_IN - bw_reserved))
  1785. return -ENOMEM;
  1786. bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_OUT, 100);
  1787. if (virt_dev->bw_table->ss_bw_out > (SS_BW_LIMIT_OUT - bw_reserved))
  1788. return -ENOMEM;
  1789. return 0;
  1790. }
  1791. /*
  1792. * This algorithm is a very conservative estimate of the worst-case scheduling
  1793. * scenario for any one interval. The hardware dynamically schedules the
  1794. * packets, so we can't tell which microframe could be the limiting factor in
  1795. * the bandwidth scheduling. This only takes into account periodic endpoints.
  1796. *
  1797. * Obviously, we can't solve an NP complete problem to find the minimum worst
  1798. * case scenario. Instead, we come up with an estimate that is no less than
  1799. * the worst case bandwidth used for any one microframe, but may be an
  1800. * over-estimate.
  1801. *
  1802. * We walk the requirements for each endpoint by interval, starting with the
  1803. * smallest interval, and place packets in the schedule where there is only one
  1804. * possible way to schedule packets for that interval. In order to simplify
  1805. * this algorithm, we record the largest max packet size for each interval, and
  1806. * assume all packets will be that size.
  1807. *
  1808. * For interval 0, we obviously must schedule all packets for each interval.
  1809. * The bandwidth for interval 0 is just the amount of data to be transmitted
  1810. * (the sum of all max ESIT payload sizes, plus any overhead per packet times
  1811. * the number of packets).
  1812. *
  1813. * For interval 1, we have two possible microframes to schedule those packets
  1814. * in. For this algorithm, if we can schedule the same number of packets for
  1815. * each possible scheduling opportunity (each microframe), we will do so. The
  1816. * remaining number of packets will be saved to be transmitted in the gaps in
  1817. * the next interval's scheduling sequence.
  1818. *
  1819. * As we move those remaining packets to be scheduled with interval 2 packets,
  1820. * we have to double the number of remaining packets to transmit. This is
  1821. * because the intervals are actually powers of 2, and we would be transmitting
  1822. * the previous interval's packets twice in this interval. We also have to be
  1823. * sure that when we look at the largest max packet size for this interval, we
  1824. * also look at the largest max packet size for the remaining packets and take
  1825. * the greater of the two.
  1826. *
  1827. * The algorithm continues to evenly distribute packets in each scheduling
  1828. * opportunity, and push the remaining packets out, until we get to the last
  1829. * interval. Then those packets and their associated overhead are just added
  1830. * to the bandwidth used.
  1831. */
  1832. static int xhci_check_bw_table(struct xhci_hcd *xhci,
  1833. struct xhci_virt_device *virt_dev,
  1834. int old_active_eps)
  1835. {
  1836. unsigned int bw_reserved;
  1837. unsigned int max_bandwidth;
  1838. unsigned int bw_used;
  1839. unsigned int block_size;
  1840. struct xhci_interval_bw_table *bw_table;
  1841. unsigned int packet_size = 0;
  1842. unsigned int overhead = 0;
  1843. unsigned int packets_transmitted = 0;
  1844. unsigned int packets_remaining = 0;
  1845. unsigned int i;
  1846. if (virt_dev->udev->speed == USB_SPEED_SUPER)
  1847. return xhci_check_ss_bw(xhci, virt_dev);
  1848. if (virt_dev->udev->speed == USB_SPEED_HIGH) {
  1849. max_bandwidth = HS_BW_LIMIT;
  1850. /* Convert percent of bus BW reserved to blocks reserved */
  1851. bw_reserved = DIV_ROUND_UP(HS_BW_RESERVED * max_bandwidth, 100);
  1852. } else {
  1853. max_bandwidth = FS_BW_LIMIT;
  1854. bw_reserved = DIV_ROUND_UP(FS_BW_RESERVED * max_bandwidth, 100);
  1855. }
  1856. bw_table = virt_dev->bw_table;
  1857. /* We need to translate the max packet size and max ESIT payloads into
  1858. * the units the hardware uses.
  1859. */
  1860. block_size = xhci_get_block_size(virt_dev->udev);
  1861. /* If we are manipulating a LS/FS device under a HS hub, double check
  1862. * that the HS bus has enough bandwidth if we are activing a new TT.
  1863. */
  1864. if (virt_dev->tt_info) {
  1865. xhci_dbg(xhci, "Recalculating BW for rootport %u\n",
  1866. virt_dev->real_port);
  1867. if (xhci_check_tt_bw_table(xhci, virt_dev, old_active_eps)) {
  1868. xhci_warn(xhci, "Not enough bandwidth on HS bus for "
  1869. "newly activated TT.\n");
  1870. return -ENOMEM;
  1871. }
  1872. xhci_dbg(xhci, "Recalculating BW for TT slot %u port %u\n",
  1873. virt_dev->tt_info->slot_id,
  1874. virt_dev->tt_info->ttport);
  1875. } else {
  1876. xhci_dbg(xhci, "Recalculating BW for rootport %u\n",
  1877. virt_dev->real_port);
  1878. }
  1879. /* Add in how much bandwidth will be used for interval zero, or the
  1880. * rounded max ESIT payload + number of packets * largest overhead.
  1881. */
  1882. bw_used = DIV_ROUND_UP(bw_table->interval0_esit_payload, block_size) +
  1883. bw_table->interval_bw[0].num_packets *
  1884. xhci_get_largest_overhead(&bw_table->interval_bw[0]);
  1885. for (i = 1; i < XHCI_MAX_INTERVAL; i++) {
  1886. unsigned int bw_added;
  1887. unsigned int largest_mps;
  1888. unsigned int interval_overhead;
  1889. /*
  1890. * How many packets could we transmit in this interval?
  1891. * If packets didn't fit in the previous interval, we will need
  1892. * to transmit that many packets twice within this interval.
  1893. */
  1894. packets_remaining = 2 * packets_remaining +
  1895. bw_table->interval_bw[i].num_packets;
  1896. /* Find the largest max packet size of this or the previous
  1897. * interval.
  1898. */
  1899. if (list_empty(&bw_table->interval_bw[i].endpoints))
  1900. largest_mps = 0;
  1901. else {
  1902. struct xhci_virt_ep *virt_ep;
  1903. struct list_head *ep_entry;
  1904. ep_entry = bw_table->interval_bw[i].endpoints.next;
  1905. virt_ep = list_entry(ep_entry,
  1906. struct xhci_virt_ep, bw_endpoint_list);
  1907. /* Convert to blocks, rounding up */
  1908. largest_mps = DIV_ROUND_UP(
  1909. virt_ep->bw_info.max_packet_size,
  1910. block_size);
  1911. }
  1912. if (largest_mps > packet_size)
  1913. packet_size = largest_mps;
  1914. /* Use the larger overhead of this or the previous interval. */
  1915. interval_overhead = xhci_get_largest_overhead(
  1916. &bw_table->interval_bw[i]);
  1917. if (interval_overhead > overhead)
  1918. overhead = interval_overhead;
  1919. /* How many packets can we evenly distribute across
  1920. * (1 << (i + 1)) possible scheduling opportunities?
  1921. */
  1922. packets_transmitted = packets_remaining >> (i + 1);
  1923. /* Add in the bandwidth used for those scheduled packets */
  1924. bw_added = packets_transmitted * (overhead + packet_size);
  1925. /* How many packets do we have remaining to transmit? */
  1926. packets_remaining = packets_remaining % (1 << (i + 1));
  1927. /* What largest max packet size should those packets have? */
  1928. /* If we've transmitted all packets, don't carry over the
  1929. * largest packet size.
  1930. */
  1931. if (packets_remaining == 0) {
  1932. packet_size = 0;
  1933. overhead = 0;
  1934. } else if (packets_transmitted > 0) {
  1935. /* Otherwise if we do have remaining packets, and we've
  1936. * scheduled some packets in this interval, take the
  1937. * largest max packet size from endpoints with this
  1938. * interval.
  1939. */
  1940. packet_size = largest_mps;
  1941. overhead = interval_overhead;
  1942. }
  1943. /* Otherwise carry over packet_size and overhead from the last
  1944. * time we had a remainder.
  1945. */
  1946. bw_used += bw_added;
  1947. if (bw_used > max_bandwidth) {
  1948. xhci_warn(xhci, "Not enough bandwidth. "
  1949. "Proposed: %u, Max: %u\n",
  1950. bw_used, max_bandwidth);
  1951. return -ENOMEM;
  1952. }
  1953. }
  1954. /*
  1955. * Ok, we know we have some packets left over after even-handedly
  1956. * scheduling interval 15. We don't know which microframes they will
  1957. * fit into, so we over-schedule and say they will be scheduled every
  1958. * microframe.
  1959. */
  1960. if (packets_remaining > 0)
  1961. bw_used += overhead + packet_size;
  1962. if (!virt_dev->tt_info && virt_dev->udev->speed == USB_SPEED_HIGH) {
  1963. unsigned int port_index = virt_dev->real_port - 1;
  1964. /* OK, we're manipulating a HS device attached to a
  1965. * root port bandwidth domain. Include the number of active TTs
  1966. * in the bandwidth used.
  1967. */
  1968. bw_used += TT_HS_OVERHEAD *
  1969. xhci->rh_bw[port_index].num_active_tts;
  1970. }
  1971. xhci_dbg(xhci, "Final bandwidth: %u, Limit: %u, Reserved: %u, "
  1972. "Available: %u " "percent\n",
  1973. bw_used, max_bandwidth, bw_reserved,
  1974. (max_bandwidth - bw_used - bw_reserved) * 100 /
  1975. max_bandwidth);
  1976. bw_used += bw_reserved;
  1977. if (bw_used > max_bandwidth) {
  1978. xhci_warn(xhci, "Not enough bandwidth. Proposed: %u, Max: %u\n",
  1979. bw_used, max_bandwidth);
  1980. return -ENOMEM;
  1981. }
  1982. bw_table->bw_used = bw_used;
  1983. return 0;
  1984. }
  1985. static bool xhci_is_async_ep(unsigned int ep_type)
  1986. {
  1987. return (ep_type != ISOC_OUT_EP && ep_type != INT_OUT_EP &&
  1988. ep_type != ISOC_IN_EP &&
  1989. ep_type != INT_IN_EP);
  1990. }
  1991. static bool xhci_is_sync_in_ep(unsigned int ep_type)
  1992. {
  1993. return (ep_type == ISOC_IN_EP || ep_type == INT_IN_EP);
  1994. }
  1995. static unsigned int xhci_get_ss_bw_consumed(struct xhci_bw_info *ep_bw)
  1996. {
  1997. unsigned int mps = DIV_ROUND_UP(ep_bw->max_packet_size, SS_BLOCK);
  1998. if (ep_bw->ep_interval == 0)
  1999. return SS_OVERHEAD_BURST +
  2000. (ep_bw->mult * ep_bw->num_packets *
  2001. (SS_OVERHEAD + mps));
  2002. return DIV_ROUND_UP(ep_bw->mult * ep_bw->num_packets *
  2003. (SS_OVERHEAD + mps + SS_OVERHEAD_BURST),
  2004. 1 << ep_bw->ep_interval);
  2005. }
  2006. void xhci_drop_ep_from_interval_table(struct xhci_hcd *xhci,
  2007. struct xhci_bw_info *ep_bw,
  2008. struct xhci_interval_bw_table *bw_table,
  2009. struct usb_device *udev,
  2010. struct xhci_virt_ep *virt_ep,
  2011. struct xhci_tt_bw_info *tt_info)
  2012. {
  2013. struct xhci_interval_bw *interval_bw;
  2014. int normalized_interval;
  2015. if (xhci_is_async_ep(ep_bw->type))
  2016. return;
  2017. if (udev->speed == USB_SPEED_SUPER) {
  2018. if (xhci_is_sync_in_ep(ep_bw->type))
  2019. xhci->devs[udev->slot_id]->bw_table->ss_bw_in -=
  2020. xhci_get_ss_bw_consumed(ep_bw);
  2021. else
  2022. xhci->devs[udev->slot_id]->bw_table->ss_bw_out -=
  2023. xhci_get_ss_bw_consumed(ep_bw);
  2024. return;
  2025. }
  2026. /* SuperSpeed endpoints never get added to intervals in the table, so
  2027. * this check is only valid for HS/FS/LS devices.
  2028. */
  2029. if (list_empty(&virt_ep->bw_endpoint_list))
  2030. return;
  2031. /* For LS/FS devices, we need to translate the interval expressed in
  2032. * microframes to frames.
  2033. */
  2034. if (udev->speed == USB_SPEED_HIGH)
  2035. normalized_interval = ep_bw->ep_interval;
  2036. else
  2037. normalized_interval = ep_bw->ep_interval - 3;
  2038. if (normalized_interval == 0)
  2039. bw_table->interval0_esit_payload -= ep_bw->max_esit_payload;
  2040. interval_bw = &bw_table->interval_bw[normalized_interval];
  2041. interval_bw->num_packets -= ep_bw->num_packets;
  2042. switch (udev->speed) {
  2043. case USB_SPEED_LOW:
  2044. interval_bw->overhead[LS_OVERHEAD_TYPE] -= 1;
  2045. break;
  2046. case USB_SPEED_FULL:
  2047. interval_bw->overhead[FS_OVERHEAD_TYPE] -= 1;
  2048. break;
  2049. case USB_SPEED_HIGH:
  2050. interval_bw->overhead[HS_OVERHEAD_TYPE] -= 1;
  2051. break;
  2052. case USB_SPEED_SUPER:
  2053. case USB_SPEED_UNKNOWN:
  2054. case USB_SPEED_WIRELESS:
  2055. /* Should never happen because only LS/FS/HS endpoints will get
  2056. * added to the endpoint list.
  2057. */
  2058. return;
  2059. }
  2060. if (tt_info)
  2061. tt_info->active_eps -= 1;
  2062. list_del_init(&virt_ep->bw_endpoint_list);
  2063. }
  2064. static void xhci_add_ep_to_interval_table(struct xhci_hcd *xhci,
  2065. struct xhci_bw_info *ep_bw,
  2066. struct xhci_interval_bw_table *bw_table,
  2067. struct usb_device *udev,
  2068. struct xhci_virt_ep *virt_ep,
  2069. struct xhci_tt_bw_info *tt_info)
  2070. {
  2071. struct xhci_interval_bw *interval_bw;
  2072. struct xhci_virt_ep *smaller_ep;
  2073. int normalized_interval;
  2074. if (xhci_is_async_ep(ep_bw->type))
  2075. return;
  2076. if (udev->speed == USB_SPEED_SUPER) {
  2077. if (xhci_is_sync_in_ep(ep_bw->type))
  2078. xhci->devs[udev->slot_id]->bw_table->ss_bw_in +=
  2079. xhci_get_ss_bw_consumed(ep_bw);
  2080. else
  2081. xhci->devs[udev->slot_id]->bw_table->ss_bw_out +=
  2082. xhci_get_ss_bw_consumed(ep_bw);
  2083. return;
  2084. }
  2085. /* For LS/FS devices, we need to translate the interval expressed in
  2086. * microframes to frames.
  2087. */
  2088. if (udev->speed == USB_SPEED_HIGH)
  2089. normalized_interval = ep_bw->ep_interval;
  2090. else
  2091. normalized_interval = ep_bw->ep_interval - 3;
  2092. if (normalized_interval == 0)
  2093. bw_table->interval0_esit_payload += ep_bw->max_esit_payload;
  2094. interval_bw = &bw_table->interval_bw[normalized_interval];
  2095. interval_bw->num_packets += ep_bw->num_packets;
  2096. switch (udev->speed) {
  2097. case USB_SPEED_LOW:
  2098. interval_bw->overhead[LS_OVERHEAD_TYPE] += 1;
  2099. break;
  2100. case USB_SPEED_FULL:
  2101. interval_bw->overhead[FS_OVERHEAD_TYPE] += 1;
  2102. break;
  2103. case USB_SPEED_HIGH:
  2104. interval_bw->overhead[HS_OVERHEAD_TYPE] += 1;
  2105. break;
  2106. case USB_SPEED_SUPER:
  2107. case USB_SPEED_UNKNOWN:
  2108. case USB_SPEED_WIRELESS:
  2109. /* Should never happen because only LS/FS/HS endpoints will get
  2110. * added to the endpoint list.
  2111. */
  2112. return;
  2113. }
  2114. if (tt_info)
  2115. tt_info->active_eps += 1;
  2116. /* Insert the endpoint into the list, largest max packet size first. */
  2117. list_for_each_entry(smaller_ep, &interval_bw->endpoints,
  2118. bw_endpoint_list) {
  2119. if (ep_bw->max_packet_size >=
  2120. smaller_ep->bw_info.max_packet_size) {
  2121. /* Add the new ep before the smaller endpoint */
  2122. list_add_tail(&virt_ep->bw_endpoint_list,
  2123. &smaller_ep->bw_endpoint_list);
  2124. return;
  2125. }
  2126. }
  2127. /* Add the new endpoint at the end of the list. */
  2128. list_add_tail(&virt_ep->bw_endpoint_list,
  2129. &interval_bw->endpoints);
  2130. }
  2131. void xhci_update_tt_active_eps(struct xhci_hcd *xhci,
  2132. struct xhci_virt_device *virt_dev,
  2133. int old_active_eps)
  2134. {
  2135. struct xhci_root_port_bw_info *rh_bw_info;
  2136. if (!virt_dev->tt_info)
  2137. return;
  2138. rh_bw_info = &xhci->rh_bw[virt_dev->real_port - 1];
  2139. if (old_active_eps == 0 &&
  2140. virt_dev->tt_info->active_eps != 0) {
  2141. rh_bw_info->num_active_tts += 1;
  2142. rh_bw_info->bw_table.bw_used += TT_HS_OVERHEAD;
  2143. } else if (old_active_eps != 0 &&
  2144. virt_dev->tt_info->active_eps == 0) {
  2145. rh_bw_info->num_active_tts -= 1;
  2146. rh_bw_info->bw_table.bw_used -= TT_HS_OVERHEAD;
  2147. }
  2148. }
  2149. static int xhci_reserve_bandwidth(struct xhci_hcd *xhci,
  2150. struct xhci_virt_device *virt_dev,
  2151. struct xhci_container_ctx *in_ctx)
  2152. {
  2153. struct xhci_bw_info ep_bw_info[31];
  2154. int i;
  2155. struct xhci_input_control_ctx *ctrl_ctx;
  2156. int old_active_eps = 0;
  2157. if (virt_dev->tt_info)
  2158. old_active_eps = virt_dev->tt_info->active_eps;
  2159. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  2160. for (i = 0; i < 31; i++) {
  2161. if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
  2162. continue;
  2163. /* Make a copy of the BW info in case we need to revert this */
  2164. memcpy(&ep_bw_info[i], &virt_dev->eps[i].bw_info,
  2165. sizeof(ep_bw_info[i]));
  2166. /* Drop the endpoint from the interval table if the endpoint is
  2167. * being dropped or changed.
  2168. */
  2169. if (EP_IS_DROPPED(ctrl_ctx, i))
  2170. xhci_drop_ep_from_interval_table(xhci,
  2171. &virt_dev->eps[i].bw_info,
  2172. virt_dev->bw_table,
  2173. virt_dev->udev,
  2174. &virt_dev->eps[i],
  2175. virt_dev->tt_info);
  2176. }
  2177. /* Overwrite the information stored in the endpoints' bw_info */
  2178. xhci_update_bw_info(xhci, virt_dev->in_ctx, ctrl_ctx, virt_dev);
  2179. for (i = 0; i < 31; i++) {
  2180. /* Add any changed or added endpoints to the interval table */
  2181. if (EP_IS_ADDED(ctrl_ctx, i))
  2182. xhci_add_ep_to_interval_table(xhci,
  2183. &virt_dev->eps[i].bw_info,
  2184. virt_dev->bw_table,
  2185. virt_dev->udev,
  2186. &virt_dev->eps[i],
  2187. virt_dev->tt_info);
  2188. }
  2189. if (!xhci_check_bw_table(xhci, virt_dev, old_active_eps)) {
  2190. /* Ok, this fits in the bandwidth we have.
  2191. * Update the number of active TTs.
  2192. */
  2193. xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
  2194. return 0;
  2195. }
  2196. /* We don't have enough bandwidth for this, revert the stored info. */
  2197. for (i = 0; i < 31; i++) {
  2198. if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
  2199. continue;
  2200. /* Drop the new copies of any added or changed endpoints from
  2201. * the interval table.
  2202. */
  2203. if (EP_IS_ADDED(ctrl_ctx, i)) {
  2204. xhci_drop_ep_from_interval_table(xhci,
  2205. &virt_dev->eps[i].bw_info,
  2206. virt_dev->bw_table,
  2207. virt_dev->udev,
  2208. &virt_dev->eps[i],
  2209. virt_dev->tt_info);
  2210. }
  2211. /* Revert the endpoint back to its old information */
  2212. memcpy(&virt_dev->eps[i].bw_info, &ep_bw_info[i],
  2213. sizeof(ep_bw_info[i]));
  2214. /* Add any changed or dropped endpoints back into the table */
  2215. if (EP_IS_DROPPED(ctrl_ctx, i))
  2216. xhci_add_ep_to_interval_table(xhci,
  2217. &virt_dev->eps[i].bw_info,
  2218. virt_dev->bw_table,
  2219. virt_dev->udev,
  2220. &virt_dev->eps[i],
  2221. virt_dev->tt_info);
  2222. }
  2223. return -ENOMEM;
  2224. }
  2225. /* Issue a configure endpoint command or evaluate context command
  2226. * and wait for it to finish.
  2227. */
  2228. static int xhci_configure_endpoint(struct xhci_hcd *xhci,
  2229. struct usb_device *udev,
  2230. struct xhci_command *command,
  2231. bool ctx_change, bool must_succeed)
  2232. {
  2233. int ret;
  2234. int timeleft;
  2235. unsigned long flags;
  2236. struct xhci_container_ctx *in_ctx;
  2237. struct completion *cmd_completion;
  2238. u32 *cmd_status;
  2239. struct xhci_virt_device *virt_dev;
  2240. union xhci_trb *cmd_trb;
  2241. spin_lock_irqsave(&xhci->lock, flags);
  2242. virt_dev = xhci->devs[udev->slot_id];
  2243. if (command)
  2244. in_ctx = command->in_ctx;
  2245. else
  2246. in_ctx = virt_dev->in_ctx;
  2247. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK) &&
  2248. xhci_reserve_host_resources(xhci, in_ctx)) {
  2249. spin_unlock_irqrestore(&xhci->lock, flags);
  2250. xhci_warn(xhci, "Not enough host resources, "
  2251. "active endpoint contexts = %u\n",
  2252. xhci->num_active_eps);
  2253. return -ENOMEM;
  2254. }
  2255. if ((xhci->quirks & XHCI_SW_BW_CHECKING) &&
  2256. xhci_reserve_bandwidth(xhci, virt_dev, in_ctx)) {
  2257. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
  2258. xhci_free_host_resources(xhci, in_ctx);
  2259. spin_unlock_irqrestore(&xhci->lock, flags);
  2260. xhci_warn(xhci, "Not enough bandwidth\n");
  2261. return -ENOMEM;
  2262. }
  2263. if (command) {
  2264. cmd_completion = command->completion;
  2265. cmd_status = &command->status;
  2266. command->command_trb = xhci->cmd_ring->enqueue;
  2267. /* Enqueue pointer can be left pointing to the link TRB,
  2268. * we must handle that
  2269. */
  2270. if (TRB_TYPE_LINK_LE32(command->command_trb->link.control))
  2271. command->command_trb =
  2272. xhci->cmd_ring->enq_seg->next->trbs;
  2273. list_add_tail(&command->cmd_list, &virt_dev->cmd_list);
  2274. } else {
  2275. cmd_completion = &virt_dev->cmd_completion;
  2276. cmd_status = &virt_dev->cmd_status;
  2277. }
  2278. init_completion(cmd_completion);
  2279. cmd_trb = xhci->cmd_ring->dequeue;
  2280. if (!ctx_change)
  2281. ret = xhci_queue_configure_endpoint(xhci, in_ctx->dma,
  2282. udev->slot_id, must_succeed);
  2283. else
  2284. ret = xhci_queue_evaluate_context(xhci, in_ctx->dma,
  2285. udev->slot_id, must_succeed);
  2286. if (ret < 0) {
  2287. if (command)
  2288. list_del(&command->cmd_list);
  2289. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
  2290. xhci_free_host_resources(xhci, in_ctx);
  2291. spin_unlock_irqrestore(&xhci->lock, flags);
  2292. xhci_dbg(xhci, "FIXME allocate a new ring segment\n");
  2293. return -ENOMEM;
  2294. }
  2295. xhci_ring_cmd_db(xhci);
  2296. spin_unlock_irqrestore(&xhci->lock, flags);
  2297. /* Wait for the configure endpoint command to complete */
  2298. timeleft = wait_for_completion_interruptible_timeout(
  2299. cmd_completion,
  2300. XHCI_CMD_DEFAULT_TIMEOUT);
  2301. if (timeleft <= 0) {
  2302. xhci_warn(xhci, "%s while waiting for %s command\n",
  2303. timeleft == 0 ? "Timeout" : "Signal",
  2304. ctx_change == 0 ?
  2305. "configure endpoint" :
  2306. "evaluate context");
  2307. /* cancel the configure endpoint command */
  2308. ret = xhci_cancel_cmd(xhci, command, cmd_trb);
  2309. if (ret < 0)
  2310. return ret;
  2311. return -ETIME;
  2312. }
  2313. if (!ctx_change)
  2314. ret = xhci_configure_endpoint_result(xhci, udev, cmd_status);
  2315. else
  2316. ret = xhci_evaluate_context_result(xhci, udev, cmd_status);
  2317. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
  2318. spin_lock_irqsave(&xhci->lock, flags);
  2319. /* If the command failed, remove the reserved resources.
  2320. * Otherwise, clean up the estimate to include dropped eps.
  2321. */
  2322. if (ret)
  2323. xhci_free_host_resources(xhci, in_ctx);
  2324. else
  2325. xhci_finish_resource_reservation(xhci, in_ctx);
  2326. spin_unlock_irqrestore(&xhci->lock, flags);
  2327. }
  2328. return ret;
  2329. }
  2330. /* Called after one or more calls to xhci_add_endpoint() or
  2331. * xhci_drop_endpoint(). If this call fails, the USB core is expected
  2332. * to call xhci_reset_bandwidth().
  2333. *
  2334. * Since we are in the middle of changing either configuration or
  2335. * installing a new alt setting, the USB core won't allow URBs to be
  2336. * enqueued for any endpoint on the old config or interface. Nothing
  2337. * else should be touching the xhci->devs[slot_id] structure, so we
  2338. * don't need to take the xhci->lock for manipulating that.
  2339. */
  2340. int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
  2341. {
  2342. int i;
  2343. int ret = 0;
  2344. struct xhci_hcd *xhci;
  2345. struct xhci_virt_device *virt_dev;
  2346. struct xhci_input_control_ctx *ctrl_ctx;
  2347. struct xhci_slot_ctx *slot_ctx;
  2348. ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
  2349. if (ret <= 0)
  2350. return ret;
  2351. xhci = hcd_to_xhci(hcd);
  2352. if (xhci->xhc_state & XHCI_STATE_DYING)
  2353. return -ENODEV;
  2354. xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
  2355. virt_dev = xhci->devs[udev->slot_id];
  2356. /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
  2357. ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
  2358. ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
  2359. ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG);
  2360. ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG));
  2361. /* Don't issue the command if there's no endpoints to update. */
  2362. if (ctrl_ctx->add_flags == cpu_to_le32(SLOT_FLAG) &&
  2363. ctrl_ctx->drop_flags == 0)
  2364. return 0;
  2365. xhci_dbg(xhci, "New Input Control Context:\n");
  2366. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
  2367. xhci_dbg_ctx(xhci, virt_dev->in_ctx,
  2368. LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx->dev_info)));
  2369. ret = xhci_configure_endpoint(xhci, udev, NULL,
  2370. false, false);
  2371. if (ret) {
  2372. /* Callee should call reset_bandwidth() */
  2373. return ret;
  2374. }
  2375. xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
  2376. xhci_dbg_ctx(xhci, virt_dev->out_ctx,
  2377. LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx->dev_info)));
  2378. /* Free any rings that were dropped, but not changed. */
  2379. for (i = 1; i < 31; ++i) {
  2380. if ((le32_to_cpu(ctrl_ctx->drop_flags) & (1 << (i + 1))) &&
  2381. !(le32_to_cpu(ctrl_ctx->add_flags) & (1 << (i + 1))))
  2382. xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
  2383. }
  2384. xhci_zero_in_ctx(xhci, virt_dev);
  2385. /*
  2386. * Install any rings for completely new endpoints or changed endpoints,
  2387. * and free or cache any old rings from changed endpoints.
  2388. */
  2389. for (i = 1; i < 31; ++i) {
  2390. if (!virt_dev->eps[i].new_ring)
  2391. continue;
  2392. /* Only cache or free the old ring if it exists.
  2393. * It may not if this is the first add of an endpoint.
  2394. */
  2395. if (virt_dev->eps[i].ring) {
  2396. xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
  2397. }
  2398. virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
  2399. virt_dev->eps[i].new_ring = NULL;
  2400. }
  2401. return ret;
  2402. }
  2403. void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
  2404. {
  2405. struct xhci_hcd *xhci;
  2406. struct xhci_virt_device *virt_dev;
  2407. int i, ret;
  2408. ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
  2409. if (ret <= 0)
  2410. return;
  2411. xhci = hcd_to_xhci(hcd);
  2412. xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
  2413. virt_dev = xhci->devs[udev->slot_id];
  2414. /* Free any rings allocated for added endpoints */
  2415. for (i = 0; i < 31; ++i) {
  2416. if (virt_dev->eps[i].new_ring) {
  2417. xhci_ring_free(xhci, virt_dev->eps[i].new_ring);
  2418. virt_dev->eps[i].new_ring = NULL;
  2419. }
  2420. }
  2421. xhci_zero_in_ctx(xhci, virt_dev);
  2422. }
  2423. static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci,
  2424. struct xhci_container_ctx *in_ctx,
  2425. struct xhci_container_ctx *out_ctx,
  2426. u32 add_flags, u32 drop_flags)
  2427. {
  2428. struct xhci_input_control_ctx *ctrl_ctx;
  2429. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  2430. ctrl_ctx->add_flags = cpu_to_le32(add_flags);
  2431. ctrl_ctx->drop_flags = cpu_to_le32(drop_flags);
  2432. xhci_slot_copy(xhci, in_ctx, out_ctx);
  2433. ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
  2434. xhci_dbg(xhci, "Input Context:\n");
  2435. xhci_dbg_ctx(xhci, in_ctx, xhci_last_valid_endpoint(add_flags));
  2436. }
  2437. static void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci,
  2438. unsigned int slot_id, unsigned int ep_index,
  2439. struct xhci_dequeue_state *deq_state)
  2440. {
  2441. struct xhci_container_ctx *in_ctx;
  2442. struct xhci_ep_ctx *ep_ctx;
  2443. u32 added_ctxs;
  2444. dma_addr_t addr;
  2445. xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
  2446. xhci->devs[slot_id]->out_ctx, ep_index);
  2447. in_ctx = xhci->devs[slot_id]->in_ctx;
  2448. ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
  2449. addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
  2450. deq_state->new_deq_ptr);
  2451. if (addr == 0) {
  2452. xhci_warn(xhci, "WARN Cannot submit config ep after "
  2453. "reset ep command\n");
  2454. xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n",
  2455. deq_state->new_deq_seg,
  2456. deq_state->new_deq_ptr);
  2457. return;
  2458. }
  2459. ep_ctx->deq = cpu_to_le64(addr | deq_state->new_cycle_state);
  2460. added_ctxs = xhci_get_endpoint_flag_from_index(ep_index);
  2461. xhci_setup_input_ctx_for_config_ep(xhci, xhci->devs[slot_id]->in_ctx,
  2462. xhci->devs[slot_id]->out_ctx, added_ctxs, added_ctxs);
  2463. }
  2464. void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
  2465. struct usb_device *udev, unsigned int ep_index)
  2466. {
  2467. struct xhci_dequeue_state deq_state;
  2468. struct xhci_virt_ep *ep;
  2469. xhci_dbg(xhci, "Cleaning up stalled endpoint ring\n");
  2470. ep = &xhci->devs[udev->slot_id]->eps[ep_index];
  2471. /* We need to move the HW's dequeue pointer past this TD,
  2472. * or it will attempt to resend it on the next doorbell ring.
  2473. */
  2474. xhci_find_new_dequeue_state(xhci, udev->slot_id,
  2475. ep_index, ep->stopped_stream, ep->stopped_td,
  2476. &deq_state);
  2477. /* HW with the reset endpoint quirk will use the saved dequeue state to
  2478. * issue a configure endpoint command later.
  2479. */
  2480. if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) {
  2481. xhci_dbg(xhci, "Queueing new dequeue state\n");
  2482. xhci_queue_new_dequeue_state(xhci, udev->slot_id,
  2483. ep_index, ep->stopped_stream, &deq_state);
  2484. } else {
  2485. /* Better hope no one uses the input context between now and the
  2486. * reset endpoint completion!
  2487. * XXX: No idea how this hardware will react when stream rings
  2488. * are enabled.
  2489. */
  2490. xhci_dbg(xhci, "Setting up input context for "
  2491. "configure endpoint command\n");
  2492. xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id,
  2493. ep_index, &deq_state);
  2494. }
  2495. }
  2496. /* Deal with stalled endpoints. The core should have sent the control message
  2497. * to clear the halt condition. However, we need to make the xHCI hardware
  2498. * reset its sequence number, since a device will expect a sequence number of
  2499. * zero after the halt condition is cleared.
  2500. * Context: in_interrupt
  2501. */
  2502. void xhci_endpoint_reset(struct usb_hcd *hcd,
  2503. struct usb_host_endpoint *ep)
  2504. {
  2505. struct xhci_hcd *xhci;
  2506. struct usb_device *udev;
  2507. unsigned int ep_index;
  2508. unsigned long flags;
  2509. int ret;
  2510. struct xhci_virt_ep *virt_ep;
  2511. xhci = hcd_to_xhci(hcd);
  2512. udev = (struct usb_device *) ep->hcpriv;
  2513. /* Called with a root hub endpoint (or an endpoint that wasn't added
  2514. * with xhci_add_endpoint()
  2515. */
  2516. if (!ep->hcpriv)
  2517. return;
  2518. ep_index = xhci_get_endpoint_index(&ep->desc);
  2519. virt_ep = &xhci->devs[udev->slot_id]->eps[ep_index];
  2520. if (!virt_ep->stopped_td) {
  2521. xhci_dbg(xhci, "Endpoint 0x%x not halted, refusing to reset.\n",
  2522. ep->desc.bEndpointAddress);
  2523. return;
  2524. }
  2525. if (usb_endpoint_xfer_control(&ep->desc)) {
  2526. xhci_dbg(xhci, "Control endpoint stall already handled.\n");
  2527. return;
  2528. }
  2529. xhci_dbg(xhci, "Queueing reset endpoint command\n");
  2530. spin_lock_irqsave(&xhci->lock, flags);
  2531. ret = xhci_queue_reset_ep(xhci, udev->slot_id, ep_index);
  2532. /*
  2533. * Can't change the ring dequeue pointer until it's transitioned to the
  2534. * stopped state, which is only upon a successful reset endpoint
  2535. * command. Better hope that last command worked!
  2536. */
  2537. if (!ret) {
  2538. xhci_cleanup_stalled_ring(xhci, udev, ep_index);
  2539. kfree(virt_ep->stopped_td);
  2540. xhci_ring_cmd_db(xhci);
  2541. }
  2542. virt_ep->stopped_td = NULL;
  2543. virt_ep->stopped_trb = NULL;
  2544. virt_ep->stopped_stream = 0;
  2545. spin_unlock_irqrestore(&xhci->lock, flags);
  2546. if (ret)
  2547. xhci_warn(xhci, "FIXME allocate a new ring segment\n");
  2548. }
  2549. static int xhci_check_streams_endpoint(struct xhci_hcd *xhci,
  2550. struct usb_device *udev, struct usb_host_endpoint *ep,
  2551. unsigned int slot_id)
  2552. {
  2553. int ret;
  2554. unsigned int ep_index;
  2555. unsigned int ep_state;
  2556. if (!ep)
  2557. return -EINVAL;
  2558. ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, true, __func__);
  2559. if (ret <= 0)
  2560. return -EINVAL;
  2561. if (ep->ss_ep_comp.bmAttributes == 0) {
  2562. xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion"
  2563. " descriptor for ep 0x%x does not support streams\n",
  2564. ep->desc.bEndpointAddress);
  2565. return -EINVAL;
  2566. }
  2567. ep_index = xhci_get_endpoint_index(&ep->desc);
  2568. ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
  2569. if (ep_state & EP_HAS_STREAMS ||
  2570. ep_state & EP_GETTING_STREAMS) {
  2571. xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x "
  2572. "already has streams set up.\n",
  2573. ep->desc.bEndpointAddress);
  2574. xhci_warn(xhci, "Send email to xHCI maintainer and ask for "
  2575. "dynamic stream context array reallocation.\n");
  2576. return -EINVAL;
  2577. }
  2578. if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) {
  2579. xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk "
  2580. "endpoint 0x%x; URBs are pending.\n",
  2581. ep->desc.bEndpointAddress);
  2582. return -EINVAL;
  2583. }
  2584. return 0;
  2585. }
  2586. static void xhci_calculate_streams_entries(struct xhci_hcd *xhci,
  2587. unsigned int *num_streams, unsigned int *num_stream_ctxs)
  2588. {
  2589. unsigned int max_streams;
  2590. /* The stream context array size must be a power of two */
  2591. *num_stream_ctxs = roundup_pow_of_two(*num_streams);
  2592. /*
  2593. * Find out how many primary stream array entries the host controller
  2594. * supports. Later we may use secondary stream arrays (similar to 2nd
  2595. * level page entries), but that's an optional feature for xHCI host
  2596. * controllers. xHCs must support at least 4 stream IDs.
  2597. */
  2598. max_streams = HCC_MAX_PSA(xhci->hcc_params);
  2599. if (*num_stream_ctxs > max_streams) {
  2600. xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n",
  2601. max_streams);
  2602. *num_stream_ctxs = max_streams;
  2603. *num_streams = max_streams;
  2604. }
  2605. }
  2606. /* Returns an error code if one of the endpoint already has streams.
  2607. * This does not change any data structures, it only checks and gathers
  2608. * information.
  2609. */
  2610. static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci,
  2611. struct usb_device *udev,
  2612. struct usb_host_endpoint **eps, unsigned int num_eps,
  2613. unsigned int *num_streams, u32 *changed_ep_bitmask)
  2614. {
  2615. unsigned int max_streams;
  2616. unsigned int endpoint_flag;
  2617. int i;
  2618. int ret;
  2619. for (i = 0; i < num_eps; i++) {
  2620. ret = xhci_check_streams_endpoint(xhci, udev,
  2621. eps[i], udev->slot_id);
  2622. if (ret < 0)
  2623. return ret;
  2624. max_streams = usb_ss_max_streams(&eps[i]->ss_ep_comp);
  2625. if (max_streams < (*num_streams - 1)) {
  2626. xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n",
  2627. eps[i]->desc.bEndpointAddress,
  2628. max_streams);
  2629. *num_streams = max_streams+1;
  2630. }
  2631. endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc);
  2632. if (*changed_ep_bitmask & endpoint_flag)
  2633. return -EINVAL;
  2634. *changed_ep_bitmask |= endpoint_flag;
  2635. }
  2636. return 0;
  2637. }
  2638. static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci,
  2639. struct usb_device *udev,
  2640. struct usb_host_endpoint **eps, unsigned int num_eps)
  2641. {
  2642. u32 changed_ep_bitmask = 0;
  2643. unsigned int slot_id;
  2644. unsigned int ep_index;
  2645. unsigned int ep_state;
  2646. int i;
  2647. slot_id = udev->slot_id;
  2648. if (!xhci->devs[slot_id])
  2649. return 0;
  2650. for (i = 0; i < num_eps; i++) {
  2651. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2652. ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
  2653. /* Are streams already being freed for the endpoint? */
  2654. if (ep_state & EP_GETTING_NO_STREAMS) {
  2655. xhci_warn(xhci, "WARN Can't disable streams for "
  2656. "endpoint 0x%x\n, "
  2657. "streams are being disabled already.",
  2658. eps[i]->desc.bEndpointAddress);
  2659. return 0;
  2660. }
  2661. /* Are there actually any streams to free? */
  2662. if (!(ep_state & EP_HAS_STREAMS) &&
  2663. !(ep_state & EP_GETTING_STREAMS)) {
  2664. xhci_warn(xhci, "WARN Can't disable streams for "
  2665. "endpoint 0x%x\n, "
  2666. "streams are already disabled!",
  2667. eps[i]->desc.bEndpointAddress);
  2668. xhci_warn(xhci, "WARN xhci_free_streams() called "
  2669. "with non-streams endpoint\n");
  2670. return 0;
  2671. }
  2672. changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc);
  2673. }
  2674. return changed_ep_bitmask;
  2675. }
  2676. /*
  2677. * The USB device drivers use this function (though the HCD interface in USB
  2678. * core) to prepare a set of bulk endpoints to use streams. Streams are used to
  2679. * coordinate mass storage command queueing across multiple endpoints (basically
  2680. * a stream ID == a task ID).
  2681. *
  2682. * Setting up streams involves allocating the same size stream context array
  2683. * for each endpoint and issuing a configure endpoint command for all endpoints.
  2684. *
  2685. * Don't allow the call to succeed if one endpoint only supports one stream
  2686. * (which means it doesn't support streams at all).
  2687. *
  2688. * Drivers may get less stream IDs than they asked for, if the host controller
  2689. * hardware or endpoints claim they can't support the number of requested
  2690. * stream IDs.
  2691. */
  2692. int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
  2693. struct usb_host_endpoint **eps, unsigned int num_eps,
  2694. unsigned int num_streams, gfp_t mem_flags)
  2695. {
  2696. int i, ret;
  2697. struct xhci_hcd *xhci;
  2698. struct xhci_virt_device *vdev;
  2699. struct xhci_command *config_cmd;
  2700. unsigned int ep_index;
  2701. unsigned int num_stream_ctxs;
  2702. unsigned long flags;
  2703. u32 changed_ep_bitmask = 0;
  2704. if (!eps)
  2705. return -EINVAL;
  2706. /* Add one to the number of streams requested to account for
  2707. * stream 0 that is reserved for xHCI usage.
  2708. */
  2709. num_streams += 1;
  2710. xhci = hcd_to_xhci(hcd);
  2711. xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n",
  2712. num_streams);
  2713. config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
  2714. if (!config_cmd) {
  2715. xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
  2716. return -ENOMEM;
  2717. }
  2718. /* Check to make sure all endpoints are not already configured for
  2719. * streams. While we're at it, find the maximum number of streams that
  2720. * all the endpoints will support and check for duplicate endpoints.
  2721. */
  2722. spin_lock_irqsave(&xhci->lock, flags);
  2723. ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps,
  2724. num_eps, &num_streams, &changed_ep_bitmask);
  2725. if (ret < 0) {
  2726. xhci_free_command(xhci, config_cmd);
  2727. spin_unlock_irqrestore(&xhci->lock, flags);
  2728. return ret;
  2729. }
  2730. if (num_streams <= 1) {
  2731. xhci_warn(xhci, "WARN: endpoints can't handle "
  2732. "more than one stream.\n");
  2733. xhci_free_command(xhci, config_cmd);
  2734. spin_unlock_irqrestore(&xhci->lock, flags);
  2735. return -EINVAL;
  2736. }
  2737. vdev = xhci->devs[udev->slot_id];
  2738. /* Mark each endpoint as being in transition, so
  2739. * xhci_urb_enqueue() will reject all URBs.
  2740. */
  2741. for (i = 0; i < num_eps; i++) {
  2742. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2743. vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS;
  2744. }
  2745. spin_unlock_irqrestore(&xhci->lock, flags);
  2746. /* Setup internal data structures and allocate HW data structures for
  2747. * streams (but don't install the HW structures in the input context
  2748. * until we're sure all memory allocation succeeded).
  2749. */
  2750. xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs);
  2751. xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n",
  2752. num_stream_ctxs, num_streams);
  2753. for (i = 0; i < num_eps; i++) {
  2754. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2755. vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci,
  2756. num_stream_ctxs,
  2757. num_streams, mem_flags);
  2758. if (!vdev->eps[ep_index].stream_info)
  2759. goto cleanup;
  2760. /* Set maxPstreams in endpoint context and update deq ptr to
  2761. * point to stream context array. FIXME
  2762. */
  2763. }
  2764. /* Set up the input context for a configure endpoint command. */
  2765. for (i = 0; i < num_eps; i++) {
  2766. struct xhci_ep_ctx *ep_ctx;
  2767. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2768. ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index);
  2769. xhci_endpoint_copy(xhci, config_cmd->in_ctx,
  2770. vdev->out_ctx, ep_index);
  2771. xhci_setup_streams_ep_input_ctx(xhci, ep_ctx,
  2772. vdev->eps[ep_index].stream_info);
  2773. }
  2774. /* Tell the HW to drop its old copy of the endpoint context info
  2775. * and add the updated copy from the input context.
  2776. */
  2777. xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx,
  2778. vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
  2779. /* Issue and wait for the configure endpoint command */
  2780. ret = xhci_configure_endpoint(xhci, udev, config_cmd,
  2781. false, false);
  2782. /* xHC rejected the configure endpoint command for some reason, so we
  2783. * leave the old ring intact and free our internal streams data
  2784. * structure.
  2785. */
  2786. if (ret < 0)
  2787. goto cleanup;
  2788. spin_lock_irqsave(&xhci->lock, flags);
  2789. for (i = 0; i < num_eps; i++) {
  2790. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2791. vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
  2792. xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n",
  2793. udev->slot_id, ep_index);
  2794. vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS;
  2795. }
  2796. xhci_free_command(xhci, config_cmd);
  2797. spin_unlock_irqrestore(&xhci->lock, flags);
  2798. /* Subtract 1 for stream 0, which drivers can't use */
  2799. return num_streams - 1;
  2800. cleanup:
  2801. /* If it didn't work, free the streams! */
  2802. for (i = 0; i < num_eps; i++) {
  2803. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2804. xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
  2805. vdev->eps[ep_index].stream_info = NULL;
  2806. /* FIXME Unset maxPstreams in endpoint context and
  2807. * update deq ptr to point to normal string ring.
  2808. */
  2809. vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
  2810. vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
  2811. xhci_endpoint_zero(xhci, vdev, eps[i]);
  2812. }
  2813. xhci_free_command(xhci, config_cmd);
  2814. return -ENOMEM;
  2815. }
  2816. /* Transition the endpoint from using streams to being a "normal" endpoint
  2817. * without streams.
  2818. *
  2819. * Modify the endpoint context state, submit a configure endpoint command,
  2820. * and free all endpoint rings for streams if that completes successfully.
  2821. */
  2822. int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
  2823. struct usb_host_endpoint **eps, unsigned int num_eps,
  2824. gfp_t mem_flags)
  2825. {
  2826. int i, ret;
  2827. struct xhci_hcd *xhci;
  2828. struct xhci_virt_device *vdev;
  2829. struct xhci_command *command;
  2830. unsigned int ep_index;
  2831. unsigned long flags;
  2832. u32 changed_ep_bitmask;
  2833. xhci = hcd_to_xhci(hcd);
  2834. vdev = xhci->devs[udev->slot_id];
  2835. /* Set up a configure endpoint command to remove the streams rings */
  2836. spin_lock_irqsave(&xhci->lock, flags);
  2837. changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci,
  2838. udev, eps, num_eps);
  2839. if (changed_ep_bitmask == 0) {
  2840. spin_unlock_irqrestore(&xhci->lock, flags);
  2841. return -EINVAL;
  2842. }
  2843. /* Use the xhci_command structure from the first endpoint. We may have
  2844. * allocated too many, but the driver may call xhci_free_streams() for
  2845. * each endpoint it grouped into one call to xhci_alloc_streams().
  2846. */
  2847. ep_index = xhci_get_endpoint_index(&eps[0]->desc);
  2848. command = vdev->eps[ep_index].stream_info->free_streams_command;
  2849. for (i = 0; i < num_eps; i++) {
  2850. struct xhci_ep_ctx *ep_ctx;
  2851. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2852. ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
  2853. xhci->devs[udev->slot_id]->eps[ep_index].ep_state |=
  2854. EP_GETTING_NO_STREAMS;
  2855. xhci_endpoint_copy(xhci, command->in_ctx,
  2856. vdev->out_ctx, ep_index);
  2857. xhci_setup_no_streams_ep_input_ctx(xhci, ep_ctx,
  2858. &vdev->eps[ep_index]);
  2859. }
  2860. xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx,
  2861. vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
  2862. spin_unlock_irqrestore(&xhci->lock, flags);
  2863. /* Issue and wait for the configure endpoint command,
  2864. * which must succeed.
  2865. */
  2866. ret = xhci_configure_endpoint(xhci, udev, command,
  2867. false, true);
  2868. /* xHC rejected the configure endpoint command for some reason, so we
  2869. * leave the streams rings intact.
  2870. */
  2871. if (ret < 0)
  2872. return ret;
  2873. spin_lock_irqsave(&xhci->lock, flags);
  2874. for (i = 0; i < num_eps; i++) {
  2875. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2876. xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
  2877. vdev->eps[ep_index].stream_info = NULL;
  2878. /* FIXME Unset maxPstreams in endpoint context and
  2879. * update deq ptr to point to normal string ring.
  2880. */
  2881. vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS;
  2882. vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
  2883. }
  2884. spin_unlock_irqrestore(&xhci->lock, flags);
  2885. return 0;
  2886. }
  2887. /*
  2888. * Deletes endpoint resources for endpoints that were active before a Reset
  2889. * Device command, or a Disable Slot command. The Reset Device command leaves
  2890. * the control endpoint intact, whereas the Disable Slot command deletes it.
  2891. *
  2892. * Must be called with xhci->lock held.
  2893. */
  2894. void xhci_free_device_endpoint_resources(struct xhci_hcd *xhci,
  2895. struct xhci_virt_device *virt_dev, bool drop_control_ep)
  2896. {
  2897. int i;
  2898. unsigned int num_dropped_eps = 0;
  2899. unsigned int drop_flags = 0;
  2900. for (i = (drop_control_ep ? 0 : 1); i < 31; i++) {
  2901. if (virt_dev->eps[i].ring) {
  2902. drop_flags |= 1 << i;
  2903. num_dropped_eps++;
  2904. }
  2905. }
  2906. xhci->num_active_eps -= num_dropped_eps;
  2907. if (num_dropped_eps)
  2908. xhci_dbg(xhci, "Dropped %u ep ctxs, flags = 0x%x, "
  2909. "%u now active.\n",
  2910. num_dropped_eps, drop_flags,
  2911. xhci->num_active_eps);
  2912. }
  2913. /*
  2914. * This submits a Reset Device Command, which will set the device state to 0,
  2915. * set the device address to 0, and disable all the endpoints except the default
  2916. * control endpoint. The USB core should come back and call
  2917. * xhci_address_device(), and then re-set up the configuration. If this is
  2918. * called because of a usb_reset_and_verify_device(), then the old alternate
  2919. * settings will be re-installed through the normal bandwidth allocation
  2920. * functions.
  2921. *
  2922. * Wait for the Reset Device command to finish. Remove all structures
  2923. * associated with the endpoints that were disabled. Clear the input device
  2924. * structure? Cache the rings? Reset the control endpoint 0 max packet size?
  2925. *
  2926. * If the virt_dev to be reset does not exist or does not match the udev,
  2927. * it means the device is lost, possibly due to the xHC restore error and
  2928. * re-initialization during S3/S4. In this case, call xhci_alloc_dev() to
  2929. * re-allocate the device.
  2930. */
  2931. int xhci_discover_or_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
  2932. {
  2933. int ret, i;
  2934. unsigned long flags;
  2935. struct xhci_hcd *xhci;
  2936. unsigned int slot_id;
  2937. struct xhci_virt_device *virt_dev;
  2938. struct xhci_command *reset_device_cmd;
  2939. int timeleft;
  2940. int last_freed_endpoint;
  2941. struct xhci_slot_ctx *slot_ctx;
  2942. int old_active_eps = 0;
  2943. ret = xhci_check_args(hcd, udev, NULL, 0, false, __func__);
  2944. if (ret <= 0)
  2945. return ret;
  2946. xhci = hcd_to_xhci(hcd);
  2947. slot_id = udev->slot_id;
  2948. virt_dev = xhci->devs[slot_id];
  2949. if (!virt_dev) {
  2950. xhci_dbg(xhci, "The device to be reset with slot ID %u does "
  2951. "not exist. Re-allocate the device\n", slot_id);
  2952. ret = xhci_alloc_dev(hcd, udev);
  2953. if (ret == 1)
  2954. return 0;
  2955. else
  2956. return -EINVAL;
  2957. }
  2958. if (virt_dev->udev != udev) {
  2959. /* If the virt_dev and the udev does not match, this virt_dev
  2960. * may belong to another udev.
  2961. * Re-allocate the device.
  2962. */
  2963. xhci_dbg(xhci, "The device to be reset with slot ID %u does "
  2964. "not match the udev. Re-allocate the device\n",
  2965. slot_id);
  2966. ret = xhci_alloc_dev(hcd, udev);
  2967. if (ret == 1)
  2968. return 0;
  2969. else
  2970. return -EINVAL;
  2971. }
  2972. /* If device is not setup, there is no point in resetting it */
  2973. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
  2974. if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) ==
  2975. SLOT_STATE_DISABLED)
  2976. return 0;
  2977. xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id);
  2978. /* Allocate the command structure that holds the struct completion.
  2979. * Assume we're in process context, since the normal device reset
  2980. * process has to wait for the device anyway. Storage devices are
  2981. * reset as part of error handling, so use GFP_NOIO instead of
  2982. * GFP_KERNEL.
  2983. */
  2984. reset_device_cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
  2985. if (!reset_device_cmd) {
  2986. xhci_dbg(xhci, "Couldn't allocate command structure.\n");
  2987. return -ENOMEM;
  2988. }
  2989. /* Attempt to submit the Reset Device command to the command ring */
  2990. spin_lock_irqsave(&xhci->lock, flags);
  2991. reset_device_cmd->command_trb = xhci->cmd_ring->enqueue;
  2992. /* Enqueue pointer can be left pointing to the link TRB,
  2993. * we must handle that
  2994. */
  2995. if (TRB_TYPE_LINK_LE32(reset_device_cmd->command_trb->link.control))
  2996. reset_device_cmd->command_trb =
  2997. xhci->cmd_ring->enq_seg->next->trbs;
  2998. list_add_tail(&reset_device_cmd->cmd_list, &virt_dev->cmd_list);
  2999. ret = xhci_queue_reset_device(xhci, slot_id);
  3000. if (ret) {
  3001. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  3002. list_del(&reset_device_cmd->cmd_list);
  3003. spin_unlock_irqrestore(&xhci->lock, flags);
  3004. goto command_cleanup;
  3005. }
  3006. xhci_ring_cmd_db(xhci);
  3007. spin_unlock_irqrestore(&xhci->lock, flags);
  3008. /* Wait for the Reset Device command to finish */
  3009. timeleft = wait_for_completion_interruptible_timeout(
  3010. reset_device_cmd->completion,
  3011. USB_CTRL_SET_TIMEOUT);
  3012. if (timeleft <= 0) {
  3013. xhci_warn(xhci, "%s while waiting for reset device command\n",
  3014. timeleft == 0 ? "Timeout" : "Signal");
  3015. spin_lock_irqsave(&xhci->lock, flags);
  3016. /* The timeout might have raced with the event ring handler, so
  3017. * only delete from the list if the item isn't poisoned.
  3018. */
  3019. if (reset_device_cmd->cmd_list.next != LIST_POISON1)
  3020. list_del(&reset_device_cmd->cmd_list);
  3021. spin_unlock_irqrestore(&xhci->lock, flags);
  3022. ret = -ETIME;
  3023. goto command_cleanup;
  3024. }
  3025. /* The Reset Device command can't fail, according to the 0.95/0.96 spec,
  3026. * unless we tried to reset a slot ID that wasn't enabled,
  3027. * or the device wasn't in the addressed or configured state.
  3028. */
  3029. ret = reset_device_cmd->status;
  3030. switch (ret) {
  3031. case COMP_EBADSLT: /* 0.95 completion code for bad slot ID */
  3032. case COMP_CTX_STATE: /* 0.96 completion code for same thing */
  3033. xhci_info(xhci, "Can't reset device (slot ID %u) in %s state\n",
  3034. slot_id,
  3035. xhci_get_slot_state(xhci, virt_dev->out_ctx));
  3036. xhci_info(xhci, "Not freeing device rings.\n");
  3037. /* Don't treat this as an error. May change my mind later. */
  3038. ret = 0;
  3039. goto command_cleanup;
  3040. case COMP_SUCCESS:
  3041. xhci_dbg(xhci, "Successful reset device command.\n");
  3042. break;
  3043. default:
  3044. if (xhci_is_vendor_info_code(xhci, ret))
  3045. break;
  3046. xhci_warn(xhci, "Unknown completion code %u for "
  3047. "reset device command.\n", ret);
  3048. ret = -EINVAL;
  3049. goto command_cleanup;
  3050. }
  3051. /* Free up host controller endpoint resources */
  3052. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
  3053. spin_lock_irqsave(&xhci->lock, flags);
  3054. /* Don't delete the default control endpoint resources */
  3055. xhci_free_device_endpoint_resources(xhci, virt_dev, false);
  3056. spin_unlock_irqrestore(&xhci->lock, flags);
  3057. }
  3058. /* Everything but endpoint 0 is disabled, so free or cache the rings. */
  3059. last_freed_endpoint = 1;
  3060. for (i = 1; i < 31; ++i) {
  3061. struct xhci_virt_ep *ep = &virt_dev->eps[i];
  3062. if (ep->ep_state & EP_HAS_STREAMS) {
  3063. xhci_free_stream_info(xhci, ep->stream_info);
  3064. ep->stream_info = NULL;
  3065. ep->ep_state &= ~EP_HAS_STREAMS;
  3066. }
  3067. if (ep->ring) {
  3068. xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
  3069. last_freed_endpoint = i;
  3070. }
  3071. if (!list_empty(&virt_dev->eps[i].bw_endpoint_list))
  3072. xhci_drop_ep_from_interval_table(xhci,
  3073. &virt_dev->eps[i].bw_info,
  3074. virt_dev->bw_table,
  3075. udev,
  3076. &virt_dev->eps[i],
  3077. virt_dev->tt_info);
  3078. xhci_clear_endpoint_bw_info(&virt_dev->eps[i].bw_info);
  3079. }
  3080. /* If necessary, update the number of active TTs on this root port */
  3081. xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
  3082. xhci_dbg(xhci, "Output context after successful reset device cmd:\n");
  3083. xhci_dbg_ctx(xhci, virt_dev->out_ctx, last_freed_endpoint);
  3084. ret = 0;
  3085. command_cleanup:
  3086. xhci_free_command(xhci, reset_device_cmd);
  3087. return ret;
  3088. }
  3089. /*
  3090. * At this point, the struct usb_device is about to go away, the device has
  3091. * disconnected, and all traffic has been stopped and the endpoints have been
  3092. * disabled. Free any HC data structures associated with that device.
  3093. */
  3094. void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
  3095. {
  3096. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  3097. struct xhci_virt_device *virt_dev;
  3098. unsigned long flags;
  3099. u32 state;
  3100. int i, ret;
  3101. ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
  3102. /* If the host is halted due to driver unload, we still need to free the
  3103. * device.
  3104. */
  3105. if (ret <= 0 && ret != -ENODEV)
  3106. return;
  3107. virt_dev = xhci->devs[udev->slot_id];
  3108. /* Stop any wayward timer functions (which may grab the lock) */
  3109. for (i = 0; i < 31; ++i) {
  3110. virt_dev->eps[i].ep_state &= ~EP_HALT_PENDING;
  3111. del_timer_sync(&virt_dev->eps[i].stop_cmd_timer);
  3112. }
  3113. if (udev->usb2_hw_lpm_enabled) {
  3114. xhci_set_usb2_hardware_lpm(hcd, udev, 0);
  3115. udev->usb2_hw_lpm_enabled = 0;
  3116. }
  3117. spin_lock_irqsave(&xhci->lock, flags);
  3118. /* Don't disable the slot if the host controller is dead. */
  3119. state = xhci_readl(xhci, &xhci->op_regs->status);
  3120. if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) ||
  3121. (xhci->xhc_state & XHCI_STATE_HALTED)) {
  3122. xhci_free_virt_device(xhci, udev->slot_id);
  3123. spin_unlock_irqrestore(&xhci->lock, flags);
  3124. return;
  3125. }
  3126. if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
  3127. spin_unlock_irqrestore(&xhci->lock, flags);
  3128. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  3129. return;
  3130. }
  3131. xhci_ring_cmd_db(xhci);
  3132. spin_unlock_irqrestore(&xhci->lock, flags);
  3133. /*
  3134. * Event command completion handler will free any data structures
  3135. * associated with the slot. XXX Can free sleep?
  3136. */
  3137. }
  3138. /*
  3139. * Checks if we have enough host controller resources for the default control
  3140. * endpoint.
  3141. *
  3142. * Must be called with xhci->lock held.
  3143. */
  3144. static int xhci_reserve_host_control_ep_resources(struct xhci_hcd *xhci)
  3145. {
  3146. if (xhci->num_active_eps + 1 > xhci->limit_active_eps) {
  3147. xhci_dbg(xhci, "Not enough ep ctxs: "
  3148. "%u active, need to add 1, limit is %u.\n",
  3149. xhci->num_active_eps, xhci->limit_active_eps);
  3150. return -ENOMEM;
  3151. }
  3152. xhci->num_active_eps += 1;
  3153. xhci_dbg(xhci, "Adding 1 ep ctx, %u now active.\n",
  3154. xhci->num_active_eps);
  3155. return 0;
  3156. }
  3157. /*
  3158. * Returns 0 if the xHC ran out of device slots, the Enable Slot command
  3159. * timed out, or allocating memory failed. Returns 1 on success.
  3160. */
  3161. int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
  3162. {
  3163. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  3164. unsigned long flags;
  3165. int timeleft;
  3166. int ret;
  3167. union xhci_trb *cmd_trb;
  3168. spin_lock_irqsave(&xhci->lock, flags);
  3169. cmd_trb = xhci->cmd_ring->dequeue;
  3170. ret = xhci_queue_slot_control(xhci, TRB_ENABLE_SLOT, 0);
  3171. if (ret) {
  3172. spin_unlock_irqrestore(&xhci->lock, flags);
  3173. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  3174. return 0;
  3175. }
  3176. xhci_ring_cmd_db(xhci);
  3177. spin_unlock_irqrestore(&xhci->lock, flags);
  3178. /* XXX: how much time for xHC slot assignment? */
  3179. timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
  3180. XHCI_CMD_DEFAULT_TIMEOUT);
  3181. if (timeleft <= 0) {
  3182. xhci_warn(xhci, "%s while waiting for a slot\n",
  3183. timeleft == 0 ? "Timeout" : "Signal");
  3184. /* cancel the enable slot request */
  3185. return xhci_cancel_cmd(xhci, NULL, cmd_trb);
  3186. }
  3187. if (!xhci->slot_id) {
  3188. xhci_err(xhci, "Error while assigning device slot ID\n");
  3189. return 0;
  3190. }
  3191. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
  3192. spin_lock_irqsave(&xhci->lock, flags);
  3193. ret = xhci_reserve_host_control_ep_resources(xhci);
  3194. if (ret) {
  3195. spin_unlock_irqrestore(&xhci->lock, flags);
  3196. xhci_warn(xhci, "Not enough host resources, "
  3197. "active endpoint contexts = %u\n",
  3198. xhci->num_active_eps);
  3199. goto disable_slot;
  3200. }
  3201. spin_unlock_irqrestore(&xhci->lock, flags);
  3202. }
  3203. /* Use GFP_NOIO, since this function can be called from
  3204. * xhci_discover_or_reset_device(), which may be called as part of
  3205. * mass storage driver error handling.
  3206. */
  3207. if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_NOIO)) {
  3208. xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
  3209. goto disable_slot;
  3210. }
  3211. udev->slot_id = xhci->slot_id;
  3212. /* Is this a LS or FS device under a HS hub? */
  3213. /* Hub or peripherial? */
  3214. return 1;
  3215. disable_slot:
  3216. /* Disable slot, if we can do it without mem alloc */
  3217. spin_lock_irqsave(&xhci->lock, flags);
  3218. if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
  3219. xhci_ring_cmd_db(xhci);
  3220. spin_unlock_irqrestore(&xhci->lock, flags);
  3221. return 0;
  3222. }
  3223. /*
  3224. * Issue an Address Device command (which will issue a SetAddress request to
  3225. * the device).
  3226. * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so
  3227. * we should only issue and wait on one address command at the same time.
  3228. *
  3229. * We add one to the device address issued by the hardware because the USB core
  3230. * uses address 1 for the root hubs (even though they're not really devices).
  3231. */
  3232. int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
  3233. {
  3234. unsigned long flags;
  3235. int timeleft;
  3236. struct xhci_virt_device *virt_dev;
  3237. int ret = 0;
  3238. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  3239. struct xhci_slot_ctx *slot_ctx;
  3240. struct xhci_input_control_ctx *ctrl_ctx;
  3241. u64 temp_64;
  3242. union xhci_trb *cmd_trb;
  3243. if (!udev->slot_id) {
  3244. xhci_dbg(xhci, "Bad Slot ID %d\n", udev->slot_id);
  3245. return -EINVAL;
  3246. }
  3247. virt_dev = xhci->devs[udev->slot_id];
  3248. if (WARN_ON(!virt_dev)) {
  3249. /*
  3250. * In plug/unplug torture test with an NEC controller,
  3251. * a zero-dereference was observed once due to virt_dev = 0.
  3252. * Print useful debug rather than crash if it is observed again!
  3253. */
  3254. xhci_warn(xhci, "Virt dev invalid for slot_id 0x%x!\n",
  3255. udev->slot_id);
  3256. return -EINVAL;
  3257. }
  3258. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
  3259. /*
  3260. * If this is the first Set Address since device plug-in or
  3261. * virt_device realloaction after a resume with an xHCI power loss,
  3262. * then set up the slot context.
  3263. */
  3264. if (!slot_ctx->dev_info)
  3265. xhci_setup_addressable_virt_dev(xhci, udev);
  3266. /* Otherwise, update the control endpoint ring enqueue pointer. */
  3267. else
  3268. xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev);
  3269. ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
  3270. ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
  3271. ctrl_ctx->drop_flags = 0;
  3272. xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
  3273. xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
  3274. spin_lock_irqsave(&xhci->lock, flags);
  3275. cmd_trb = xhci->cmd_ring->dequeue;
  3276. ret = xhci_queue_address_device(xhci, virt_dev->in_ctx->dma,
  3277. udev->slot_id);
  3278. if (ret) {
  3279. spin_unlock_irqrestore(&xhci->lock, flags);
  3280. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  3281. return ret;
  3282. }
  3283. xhci_ring_cmd_db(xhci);
  3284. spin_unlock_irqrestore(&xhci->lock, flags);
  3285. /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
  3286. timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
  3287. XHCI_CMD_DEFAULT_TIMEOUT);
  3288. /* FIXME: From section 4.3.4: "Software shall be responsible for timing
  3289. * the SetAddress() "recovery interval" required by USB and aborting the
  3290. * command on a timeout.
  3291. */
  3292. if (timeleft <= 0) {
  3293. xhci_warn(xhci, "%s while waiting for address device command\n",
  3294. timeleft == 0 ? "Timeout" : "Signal");
  3295. /* cancel the address device command */
  3296. ret = xhci_cancel_cmd(xhci, NULL, cmd_trb);
  3297. if (ret < 0)
  3298. return ret;
  3299. return -ETIME;
  3300. }
  3301. switch (virt_dev->cmd_status) {
  3302. case COMP_CTX_STATE:
  3303. case COMP_EBADSLT:
  3304. xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n",
  3305. udev->slot_id);
  3306. ret = -EINVAL;
  3307. break;
  3308. case COMP_TX_ERR:
  3309. dev_warn(&udev->dev, "Device not responding to set address.\n");
  3310. ret = -EPROTO;
  3311. break;
  3312. case COMP_DEV_ERR:
  3313. dev_warn(&udev->dev, "ERROR: Incompatible device for address "
  3314. "device command.\n");
  3315. ret = -ENODEV;
  3316. break;
  3317. case COMP_SUCCESS:
  3318. xhci_dbg(xhci, "Successful Address Device command\n");
  3319. break;
  3320. default:
  3321. xhci_err(xhci, "ERROR: unexpected command completion "
  3322. "code 0x%x.\n", virt_dev->cmd_status);
  3323. xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
  3324. xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
  3325. ret = -EINVAL;
  3326. break;
  3327. }
  3328. if (ret) {
  3329. return ret;
  3330. }
  3331. temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
  3332. xhci_dbg(xhci, "Op regs DCBAA ptr = %#016llx\n", temp_64);
  3333. xhci_dbg(xhci, "Slot ID %d dcbaa entry @%p = %#016llx\n",
  3334. udev->slot_id,
  3335. &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
  3336. (unsigned long long)
  3337. le64_to_cpu(xhci->dcbaa->dev_context_ptrs[udev->slot_id]));
  3338. xhci_dbg(xhci, "Output Context DMA address = %#08llx\n",
  3339. (unsigned long long)virt_dev->out_ctx->dma);
  3340. xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
  3341. xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
  3342. xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
  3343. xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
  3344. /*
  3345. * USB core uses address 1 for the roothubs, so we add one to the
  3346. * address given back to us by the HC.
  3347. */
  3348. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
  3349. /* Use kernel assigned address for devices; store xHC assigned
  3350. * address locally. */
  3351. virt_dev->address = (le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK)
  3352. + 1;
  3353. /* Zero the input context control for later use */
  3354. ctrl_ctx->add_flags = 0;
  3355. ctrl_ctx->drop_flags = 0;
  3356. xhci_dbg(xhci, "Internal device address = %d\n", virt_dev->address);
  3357. return 0;
  3358. }
  3359. #ifdef CONFIG_USB_SUSPEND
  3360. /* BESL to HIRD Encoding array for USB2 LPM */
  3361. static int xhci_besl_encoding[16] = {125, 150, 200, 300, 400, 500, 1000, 2000,
  3362. 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000};
  3363. /* Calculate HIRD/BESL for USB2 PORTPMSC*/
  3364. static int xhci_calculate_hird_besl(struct xhci_hcd *xhci,
  3365. struct usb_device *udev)
  3366. {
  3367. int u2del, besl, besl_host;
  3368. int besl_device = 0;
  3369. u32 field;
  3370. u2del = HCS_U2_LATENCY(xhci->hcs_params3);
  3371. field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
  3372. if (field & USB_BESL_SUPPORT) {
  3373. for (besl_host = 0; besl_host < 16; besl_host++) {
  3374. if (xhci_besl_encoding[besl_host] >= u2del)
  3375. break;
  3376. }
  3377. /* Use baseline BESL value as default */
  3378. if (field & USB_BESL_BASELINE_VALID)
  3379. besl_device = USB_GET_BESL_BASELINE(field);
  3380. else if (field & USB_BESL_DEEP_VALID)
  3381. besl_device = USB_GET_BESL_DEEP(field);
  3382. } else {
  3383. if (u2del <= 50)
  3384. besl_host = 0;
  3385. else
  3386. besl_host = (u2del - 51) / 75 + 1;
  3387. }
  3388. besl = besl_host + besl_device;
  3389. if (besl > 15)
  3390. besl = 15;
  3391. return besl;
  3392. }
  3393. static int xhci_usb2_software_lpm_test(struct usb_hcd *hcd,
  3394. struct usb_device *udev)
  3395. {
  3396. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  3397. struct dev_info *dev_info;
  3398. __le32 __iomem **port_array;
  3399. __le32 __iomem *addr, *pm_addr;
  3400. u32 temp, dev_id;
  3401. unsigned int port_num;
  3402. unsigned long flags;
  3403. int hird;
  3404. int ret;
  3405. if (hcd->speed == HCD_USB3 || !xhci->sw_lpm_support ||
  3406. !udev->lpm_capable)
  3407. return -EINVAL;
  3408. /* we only support lpm for non-hub device connected to root hub yet */
  3409. if (!udev->parent || udev->parent->parent ||
  3410. udev->descriptor.bDeviceClass == USB_CLASS_HUB)
  3411. return -EINVAL;
  3412. spin_lock_irqsave(&xhci->lock, flags);
  3413. /* Look for devices in lpm_failed_devs list */
  3414. dev_id = le16_to_cpu(udev->descriptor.idVendor) << 16 |
  3415. le16_to_cpu(udev->descriptor.idProduct);
  3416. list_for_each_entry(dev_info, &xhci->lpm_failed_devs, list) {
  3417. if (dev_info->dev_id == dev_id) {
  3418. ret = -EINVAL;
  3419. goto finish;
  3420. }
  3421. }
  3422. port_array = xhci->usb2_ports;
  3423. port_num = udev->portnum - 1;
  3424. if (port_num > HCS_MAX_PORTS(xhci->hcs_params1)) {
  3425. xhci_dbg(xhci, "invalid port number %d\n", udev->portnum);
  3426. ret = -EINVAL;
  3427. goto finish;
  3428. }
  3429. /*
  3430. * Test USB 2.0 software LPM.
  3431. * FIXME: some xHCI 1.0 hosts may implement a new register to set up
  3432. * hardware-controlled USB 2.0 LPM. See section 5.4.11 and 4.23.5.1.1.1
  3433. * in the June 2011 errata release.
  3434. */
  3435. xhci_dbg(xhci, "test port %d software LPM\n", port_num);
  3436. /*
  3437. * Set L1 Device Slot and HIRD/BESL.
  3438. * Check device's USB 2.0 extension descriptor to determine whether
  3439. * HIRD or BESL shoule be used. See USB2.0 LPM errata.
  3440. */
  3441. pm_addr = port_array[port_num] + 1;
  3442. hird = xhci_calculate_hird_besl(xhci, udev);
  3443. temp = PORT_L1DS(udev->slot_id) | PORT_HIRD(hird);
  3444. xhci_writel(xhci, temp, pm_addr);
  3445. /* Set port link state to U2(L1) */
  3446. addr = port_array[port_num];
  3447. xhci_set_link_state(xhci, port_array, port_num, XDEV_U2);
  3448. /* wait for ACK */
  3449. spin_unlock_irqrestore(&xhci->lock, flags);
  3450. msleep(10);
  3451. spin_lock_irqsave(&xhci->lock, flags);
  3452. /* Check L1 Status */
  3453. ret = xhci_handshake(xhci, pm_addr,
  3454. PORT_L1S_MASK, PORT_L1S_SUCCESS, 125);
  3455. if (ret != -ETIMEDOUT) {
  3456. /* enter L1 successfully */
  3457. temp = xhci_readl(xhci, addr);
  3458. xhci_dbg(xhci, "port %d entered L1 state, port status 0x%x\n",
  3459. port_num, temp);
  3460. ret = 0;
  3461. } else {
  3462. temp = xhci_readl(xhci, pm_addr);
  3463. xhci_dbg(xhci, "port %d software lpm failed, L1 status %d\n",
  3464. port_num, temp & PORT_L1S_MASK);
  3465. ret = -EINVAL;
  3466. }
  3467. /* Resume the port */
  3468. xhci_set_link_state(xhci, port_array, port_num, XDEV_U0);
  3469. spin_unlock_irqrestore(&xhci->lock, flags);
  3470. msleep(10);
  3471. spin_lock_irqsave(&xhci->lock, flags);
  3472. /* Clear PLC */
  3473. xhci_test_and_clear_bit(xhci, port_array, port_num, PORT_PLC);
  3474. /* Check PORTSC to make sure the device is in the right state */
  3475. if (!ret) {
  3476. temp = xhci_readl(xhci, addr);
  3477. xhci_dbg(xhci, "resumed port %d status 0x%x\n", port_num, temp);
  3478. if (!(temp & PORT_CONNECT) || !(temp & PORT_PE) ||
  3479. (temp & PORT_PLS_MASK) != XDEV_U0) {
  3480. xhci_dbg(xhci, "port L1 resume fail\n");
  3481. ret = -EINVAL;
  3482. }
  3483. }
  3484. if (ret) {
  3485. /* Insert dev to lpm_failed_devs list */
  3486. xhci_warn(xhci, "device LPM test failed, may disconnect and "
  3487. "re-enumerate\n");
  3488. dev_info = kzalloc(sizeof(struct dev_info), GFP_ATOMIC);
  3489. if (!dev_info) {
  3490. ret = -ENOMEM;
  3491. goto finish;
  3492. }
  3493. dev_info->dev_id = dev_id;
  3494. INIT_LIST_HEAD(&dev_info->list);
  3495. list_add(&dev_info->list, &xhci->lpm_failed_devs);
  3496. } else {
  3497. xhci_ring_device(xhci, udev->slot_id);
  3498. }
  3499. finish:
  3500. spin_unlock_irqrestore(&xhci->lock, flags);
  3501. return ret;
  3502. }
  3503. int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
  3504. struct usb_device *udev, int enable)
  3505. {
  3506. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  3507. __le32 __iomem **port_array;
  3508. __le32 __iomem *pm_addr;
  3509. u32 temp;
  3510. unsigned int port_num;
  3511. unsigned long flags;
  3512. int hird;
  3513. if (hcd->speed == HCD_USB3 || !xhci->hw_lpm_support ||
  3514. !udev->lpm_capable)
  3515. return -EPERM;
  3516. if (!udev->parent || udev->parent->parent ||
  3517. udev->descriptor.bDeviceClass == USB_CLASS_HUB)
  3518. return -EPERM;
  3519. if (udev->usb2_hw_lpm_capable != 1)
  3520. return -EPERM;
  3521. spin_lock_irqsave(&xhci->lock, flags);
  3522. port_array = xhci->usb2_ports;
  3523. port_num = udev->portnum - 1;
  3524. pm_addr = port_array[port_num] + 1;
  3525. temp = xhci_readl(xhci, pm_addr);
  3526. xhci_dbg(xhci, "%s port %d USB2 hardware LPM\n",
  3527. enable ? "enable" : "disable", port_num);
  3528. hird = xhci_calculate_hird_besl(xhci, udev);
  3529. if (enable) {
  3530. temp &= ~PORT_HIRD_MASK;
  3531. temp |= PORT_HIRD(hird) | PORT_RWE;
  3532. xhci_writel(xhci, temp, pm_addr);
  3533. temp = xhci_readl(xhci, pm_addr);
  3534. temp |= PORT_HLE;
  3535. xhci_writel(xhci, temp, pm_addr);
  3536. } else {
  3537. temp &= ~(PORT_HLE | PORT_RWE | PORT_HIRD_MASK);
  3538. xhci_writel(xhci, temp, pm_addr);
  3539. }
  3540. spin_unlock_irqrestore(&xhci->lock, flags);
  3541. return 0;
  3542. }
  3543. int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
  3544. {
  3545. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  3546. int ret;
  3547. ret = xhci_usb2_software_lpm_test(hcd, udev);
  3548. if (!ret) {
  3549. xhci_dbg(xhci, "software LPM test succeed\n");
  3550. if (xhci->hw_lpm_support == 1) {
  3551. udev->usb2_hw_lpm_capable = 1;
  3552. ret = xhci_set_usb2_hardware_lpm(hcd, udev, 1);
  3553. if (!ret)
  3554. udev->usb2_hw_lpm_enabled = 1;
  3555. }
  3556. }
  3557. return 0;
  3558. }
  3559. #else
  3560. int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
  3561. struct usb_device *udev, int enable)
  3562. {
  3563. return 0;
  3564. }
  3565. int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
  3566. {
  3567. return 0;
  3568. }
  3569. #endif /* CONFIG_USB_SUSPEND */
  3570. /*---------------------- USB 3.0 Link PM functions ------------------------*/
  3571. #ifdef CONFIG_PM
  3572. /* Service interval in nanoseconds = 2^(bInterval - 1) * 125us * 1000ns / 1us */
  3573. static unsigned long long xhci_service_interval_to_ns(
  3574. struct usb_endpoint_descriptor *desc)
  3575. {
  3576. return (1ULL << (desc->bInterval - 1)) * 125 * 1000;
  3577. }
  3578. static u16 xhci_get_timeout_no_hub_lpm(struct usb_device *udev,
  3579. enum usb3_link_state state)
  3580. {
  3581. unsigned long long sel;
  3582. unsigned long long pel;
  3583. unsigned int max_sel_pel;
  3584. char *state_name;
  3585. switch (state) {
  3586. case USB3_LPM_U1:
  3587. /* Convert SEL and PEL stored in nanoseconds to microseconds */
  3588. sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
  3589. pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
  3590. max_sel_pel = USB3_LPM_MAX_U1_SEL_PEL;
  3591. state_name = "U1";
  3592. break;
  3593. case USB3_LPM_U2:
  3594. sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
  3595. pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
  3596. max_sel_pel = USB3_LPM_MAX_U2_SEL_PEL;
  3597. state_name = "U2";
  3598. break;
  3599. default:
  3600. dev_warn(&udev->dev, "%s: Can't get timeout for non-U1 or U2 state.\n",
  3601. __func__);
  3602. return USB3_LPM_DISABLED;
  3603. }
  3604. if (sel <= max_sel_pel && pel <= max_sel_pel)
  3605. return USB3_LPM_DEVICE_INITIATED;
  3606. if (sel > max_sel_pel)
  3607. dev_dbg(&udev->dev, "Device-initiated %s disabled "
  3608. "due to long SEL %llu ms\n",
  3609. state_name, sel);
  3610. else
  3611. dev_dbg(&udev->dev, "Device-initiated %s disabled "
  3612. "due to long PEL %llu\n ms",
  3613. state_name, pel);
  3614. return USB3_LPM_DISABLED;
  3615. }
  3616. /* Returns the hub-encoded U1 timeout value.
  3617. * The U1 timeout should be the maximum of the following values:
  3618. * - For control endpoints, U1 system exit latency (SEL) * 3
  3619. * - For bulk endpoints, U1 SEL * 5
  3620. * - For interrupt endpoints:
  3621. * - Notification EPs, U1 SEL * 3
  3622. * - Periodic EPs, max(105% of bInterval, U1 SEL * 2)
  3623. * - For isochronous endpoints, max(105% of bInterval, U1 SEL * 2)
  3624. */
  3625. static u16 xhci_calculate_intel_u1_timeout(struct usb_device *udev,
  3626. struct usb_endpoint_descriptor *desc)
  3627. {
  3628. unsigned long long timeout_ns;
  3629. int ep_type;
  3630. int intr_type;
  3631. ep_type = usb_endpoint_type(desc);
  3632. switch (ep_type) {
  3633. case USB_ENDPOINT_XFER_CONTROL:
  3634. timeout_ns = udev->u1_params.sel * 3;
  3635. break;
  3636. case USB_ENDPOINT_XFER_BULK:
  3637. timeout_ns = udev->u1_params.sel * 5;
  3638. break;
  3639. case USB_ENDPOINT_XFER_INT:
  3640. intr_type = usb_endpoint_interrupt_type(desc);
  3641. if (intr_type == USB_ENDPOINT_INTR_NOTIFICATION) {
  3642. timeout_ns = udev->u1_params.sel * 3;
  3643. break;
  3644. }
  3645. /* Otherwise the calculation is the same as isoc eps */
  3646. case USB_ENDPOINT_XFER_ISOC:
  3647. timeout_ns = xhci_service_interval_to_ns(desc);
  3648. timeout_ns = DIV_ROUND_UP_ULL(timeout_ns * 105, 100);
  3649. if (timeout_ns < udev->u1_params.sel * 2)
  3650. timeout_ns = udev->u1_params.sel * 2;
  3651. break;
  3652. default:
  3653. return 0;
  3654. }
  3655. /* The U1 timeout is encoded in 1us intervals. */
  3656. timeout_ns = DIV_ROUND_UP_ULL(timeout_ns, 1000);
  3657. /* Don't return a timeout of zero, because that's USB3_LPM_DISABLED. */
  3658. if (timeout_ns == USB3_LPM_DISABLED)
  3659. timeout_ns++;
  3660. /* If the necessary timeout value is bigger than what we can set in the
  3661. * USB 3.0 hub, we have to disable hub-initiated U1.
  3662. */
  3663. if (timeout_ns <= USB3_LPM_U1_MAX_TIMEOUT)
  3664. return timeout_ns;
  3665. dev_dbg(&udev->dev, "Hub-initiated U1 disabled "
  3666. "due to long timeout %llu ms\n", timeout_ns);
  3667. return xhci_get_timeout_no_hub_lpm(udev, USB3_LPM_U1);
  3668. }
  3669. /* Returns the hub-encoded U2 timeout value.
  3670. * The U2 timeout should be the maximum of:
  3671. * - 10 ms (to avoid the bandwidth impact on the scheduler)
  3672. * - largest bInterval of any active periodic endpoint (to avoid going
  3673. * into lower power link states between intervals).
  3674. * - the U2 Exit Latency of the device
  3675. */
  3676. static u16 xhci_calculate_intel_u2_timeout(struct usb_device *udev,
  3677. struct usb_endpoint_descriptor *desc)
  3678. {
  3679. unsigned long long timeout_ns;
  3680. unsigned long long u2_del_ns;
  3681. timeout_ns = 10 * 1000 * 1000;
  3682. if ((usb_endpoint_xfer_int(desc) || usb_endpoint_xfer_isoc(desc)) &&
  3683. (xhci_service_interval_to_ns(desc) > timeout_ns))
  3684. timeout_ns = xhci_service_interval_to_ns(desc);
  3685. u2_del_ns = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat) * 1000ULL;
  3686. if (u2_del_ns > timeout_ns)
  3687. timeout_ns = u2_del_ns;
  3688. /* The U2 timeout is encoded in 256us intervals */
  3689. timeout_ns = DIV_ROUND_UP_ULL(timeout_ns, 256 * 1000);
  3690. /* If the necessary timeout value is bigger than what we can set in the
  3691. * USB 3.0 hub, we have to disable hub-initiated U2.
  3692. */
  3693. if (timeout_ns <= USB3_LPM_U2_MAX_TIMEOUT)
  3694. return timeout_ns;
  3695. dev_dbg(&udev->dev, "Hub-initiated U2 disabled "
  3696. "due to long timeout %llu ms\n", timeout_ns);
  3697. return xhci_get_timeout_no_hub_lpm(udev, USB3_LPM_U2);
  3698. }
  3699. static u16 xhci_call_host_update_timeout_for_endpoint(struct xhci_hcd *xhci,
  3700. struct usb_device *udev,
  3701. struct usb_endpoint_descriptor *desc,
  3702. enum usb3_link_state state,
  3703. u16 *timeout)
  3704. {
  3705. if (state == USB3_LPM_U1) {
  3706. if (xhci->quirks & XHCI_INTEL_HOST)
  3707. return xhci_calculate_intel_u1_timeout(udev, desc);
  3708. } else {
  3709. if (xhci->quirks & XHCI_INTEL_HOST)
  3710. return xhci_calculate_intel_u2_timeout(udev, desc);
  3711. }
  3712. return USB3_LPM_DISABLED;
  3713. }
  3714. static int xhci_update_timeout_for_endpoint(struct xhci_hcd *xhci,
  3715. struct usb_device *udev,
  3716. struct usb_endpoint_descriptor *desc,
  3717. enum usb3_link_state state,
  3718. u16 *timeout)
  3719. {
  3720. u16 alt_timeout;
  3721. alt_timeout = xhci_call_host_update_timeout_for_endpoint(xhci, udev,
  3722. desc, state, timeout);
  3723. /* If we found we can't enable hub-initiated LPM, or
  3724. * the U1 or U2 exit latency was too high to allow
  3725. * device-initiated LPM as well, just stop searching.
  3726. */
  3727. if (alt_timeout == USB3_LPM_DISABLED ||
  3728. alt_timeout == USB3_LPM_DEVICE_INITIATED) {
  3729. *timeout = alt_timeout;
  3730. return -E2BIG;
  3731. }
  3732. if (alt_timeout > *timeout)
  3733. *timeout = alt_timeout;
  3734. return 0;
  3735. }
  3736. static int xhci_update_timeout_for_interface(struct xhci_hcd *xhci,
  3737. struct usb_device *udev,
  3738. struct usb_host_interface *alt,
  3739. enum usb3_link_state state,
  3740. u16 *timeout)
  3741. {
  3742. int j;
  3743. for (j = 0; j < alt->desc.bNumEndpoints; j++) {
  3744. if (xhci_update_timeout_for_endpoint(xhci, udev,
  3745. &alt->endpoint[j].desc, state, timeout))
  3746. return -E2BIG;
  3747. continue;
  3748. }
  3749. return 0;
  3750. }
  3751. static int xhci_check_intel_tier_policy(struct usb_device *udev,
  3752. enum usb3_link_state state)
  3753. {
  3754. struct usb_device *parent;
  3755. unsigned int num_hubs;
  3756. if (state == USB3_LPM_U2)
  3757. return 0;
  3758. /* Don't enable U1 if the device is on a 2nd tier hub or lower. */
  3759. for (parent = udev->parent, num_hubs = 0; parent->parent;
  3760. parent = parent->parent)
  3761. num_hubs++;
  3762. if (num_hubs < 2)
  3763. return 0;
  3764. dev_dbg(&udev->dev, "Disabling U1 link state for device"
  3765. " below second-tier hub.\n");
  3766. dev_dbg(&udev->dev, "Plug device into first-tier hub "
  3767. "to decrease power consumption.\n");
  3768. return -E2BIG;
  3769. }
  3770. static int xhci_check_tier_policy(struct xhci_hcd *xhci,
  3771. struct usb_device *udev,
  3772. enum usb3_link_state state)
  3773. {
  3774. if (xhci->quirks & XHCI_INTEL_HOST)
  3775. return xhci_check_intel_tier_policy(udev, state);
  3776. return -EINVAL;
  3777. }
  3778. /* Returns the U1 or U2 timeout that should be enabled.
  3779. * If the tier check or timeout setting functions return with a non-zero exit
  3780. * code, that means the timeout value has been finalized and we shouldn't look
  3781. * at any more endpoints.
  3782. */
  3783. static u16 xhci_calculate_lpm_timeout(struct usb_hcd *hcd,
  3784. struct usb_device *udev, enum usb3_link_state state)
  3785. {
  3786. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  3787. struct usb_host_config *config;
  3788. char *state_name;
  3789. int i;
  3790. u16 timeout = USB3_LPM_DISABLED;
  3791. if (state == USB3_LPM_U1)
  3792. state_name = "U1";
  3793. else if (state == USB3_LPM_U2)
  3794. state_name = "U2";
  3795. else {
  3796. dev_warn(&udev->dev, "Can't enable unknown link state %i\n",
  3797. state);
  3798. return timeout;
  3799. }
  3800. if (xhci_check_tier_policy(xhci, udev, state) < 0)
  3801. return timeout;
  3802. /* Gather some information about the currently installed configuration
  3803. * and alternate interface settings.
  3804. */
  3805. if (xhci_update_timeout_for_endpoint(xhci, udev, &udev->ep0.desc,
  3806. state, &timeout))
  3807. return timeout;
  3808. config = udev->actconfig;
  3809. if (!config)
  3810. return timeout;
  3811. for (i = 0; i < USB_MAXINTERFACES; i++) {
  3812. struct usb_driver *driver;
  3813. struct usb_interface *intf = config->interface[i];
  3814. if (!intf)
  3815. continue;
  3816. /* Check if any currently bound drivers want hub-initiated LPM
  3817. * disabled.
  3818. */
  3819. if (intf->dev.driver) {
  3820. driver = to_usb_driver(intf->dev.driver);
  3821. if (driver && driver->disable_hub_initiated_lpm) {
  3822. dev_dbg(&udev->dev, "Hub-initiated %s disabled "
  3823. "at request of driver %s\n",
  3824. state_name, driver->name);
  3825. return xhci_get_timeout_no_hub_lpm(udev, state);
  3826. }
  3827. }
  3828. /* Not sure how this could happen... */
  3829. if (!intf->cur_altsetting)
  3830. continue;
  3831. if (xhci_update_timeout_for_interface(xhci, udev,
  3832. intf->cur_altsetting,
  3833. state, &timeout))
  3834. return timeout;
  3835. }
  3836. return timeout;
  3837. }
  3838. /*
  3839. * Issue an Evaluate Context command to change the Maximum Exit Latency in the
  3840. * slot context. If that succeeds, store the new MEL in the xhci_virt_device.
  3841. */
  3842. static int xhci_change_max_exit_latency(struct xhci_hcd *xhci,
  3843. struct usb_device *udev, u16 max_exit_latency)
  3844. {
  3845. struct xhci_virt_device *virt_dev;
  3846. struct xhci_command *command;
  3847. struct xhci_input_control_ctx *ctrl_ctx;
  3848. struct xhci_slot_ctx *slot_ctx;
  3849. unsigned long flags;
  3850. int ret;
  3851. spin_lock_irqsave(&xhci->lock, flags);
  3852. if (max_exit_latency == xhci->devs[udev->slot_id]->current_mel) {
  3853. spin_unlock_irqrestore(&xhci->lock, flags);
  3854. return 0;
  3855. }
  3856. /* Attempt to issue an Evaluate Context command to change the MEL. */
  3857. virt_dev = xhci->devs[udev->slot_id];
  3858. command = xhci->lpm_command;
  3859. xhci_slot_copy(xhci, command->in_ctx, virt_dev->out_ctx);
  3860. spin_unlock_irqrestore(&xhci->lock, flags);
  3861. ctrl_ctx = xhci_get_input_control_ctx(xhci, command->in_ctx);
  3862. ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
  3863. slot_ctx = xhci_get_slot_ctx(xhci, command->in_ctx);
  3864. slot_ctx->dev_info2 &= cpu_to_le32(~((u32) MAX_EXIT));
  3865. slot_ctx->dev_info2 |= cpu_to_le32(max_exit_latency);
  3866. xhci_dbg(xhci, "Set up evaluate context for LPM MEL change.\n");
  3867. xhci_dbg(xhci, "Slot %u Input Context:\n", udev->slot_id);
  3868. xhci_dbg_ctx(xhci, command->in_ctx, 0);
  3869. /* Issue and wait for the evaluate context command. */
  3870. ret = xhci_configure_endpoint(xhci, udev, command,
  3871. true, true);
  3872. xhci_dbg(xhci, "Slot %u Output Context:\n", udev->slot_id);
  3873. xhci_dbg_ctx(xhci, virt_dev->out_ctx, 0);
  3874. if (!ret) {
  3875. spin_lock_irqsave(&xhci->lock, flags);
  3876. virt_dev->current_mel = max_exit_latency;
  3877. spin_unlock_irqrestore(&xhci->lock, flags);
  3878. }
  3879. return ret;
  3880. }
  3881. static int calculate_max_exit_latency(struct usb_device *udev,
  3882. enum usb3_link_state state_changed,
  3883. u16 hub_encoded_timeout)
  3884. {
  3885. unsigned long long u1_mel_us = 0;
  3886. unsigned long long u2_mel_us = 0;
  3887. unsigned long long mel_us = 0;
  3888. bool disabling_u1;
  3889. bool disabling_u2;
  3890. bool enabling_u1;
  3891. bool enabling_u2;
  3892. disabling_u1 = (state_changed == USB3_LPM_U1 &&
  3893. hub_encoded_timeout == USB3_LPM_DISABLED);
  3894. disabling_u2 = (state_changed == USB3_LPM_U2 &&
  3895. hub_encoded_timeout == USB3_LPM_DISABLED);
  3896. enabling_u1 = (state_changed == USB3_LPM_U1 &&
  3897. hub_encoded_timeout != USB3_LPM_DISABLED);
  3898. enabling_u2 = (state_changed == USB3_LPM_U2 &&
  3899. hub_encoded_timeout != USB3_LPM_DISABLED);
  3900. /* If U1 was already enabled and we're not disabling it,
  3901. * or we're going to enable U1, account for the U1 max exit latency.
  3902. */
  3903. if ((udev->u1_params.timeout != USB3_LPM_DISABLED && !disabling_u1) ||
  3904. enabling_u1)
  3905. u1_mel_us = DIV_ROUND_UP(udev->u1_params.mel, 1000);
  3906. if ((udev->u2_params.timeout != USB3_LPM_DISABLED && !disabling_u2) ||
  3907. enabling_u2)
  3908. u2_mel_us = DIV_ROUND_UP(udev->u2_params.mel, 1000);
  3909. if (u1_mel_us > u2_mel_us)
  3910. mel_us = u1_mel_us;
  3911. else
  3912. mel_us = u2_mel_us;
  3913. /* xHCI host controller max exit latency field is only 16 bits wide. */
  3914. if (mel_us > MAX_EXIT) {
  3915. dev_warn(&udev->dev, "Link PM max exit latency of %lluus "
  3916. "is too big.\n", mel_us);
  3917. return -E2BIG;
  3918. }
  3919. return mel_us;
  3920. }
  3921. /* Returns the USB3 hub-encoded value for the U1/U2 timeout. */
  3922. int xhci_enable_usb3_lpm_timeout(struct usb_hcd *hcd,
  3923. struct usb_device *udev, enum usb3_link_state state)
  3924. {
  3925. struct xhci_hcd *xhci;
  3926. u16 hub_encoded_timeout;
  3927. int mel;
  3928. int ret;
  3929. xhci = hcd_to_xhci(hcd);
  3930. /* The LPM timeout values are pretty host-controller specific, so don't
  3931. * enable hub-initiated timeouts unless the vendor has provided
  3932. * information about their timeout algorithm.
  3933. */
  3934. if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) ||
  3935. !xhci->devs[udev->slot_id])
  3936. return USB3_LPM_DISABLED;
  3937. hub_encoded_timeout = xhci_calculate_lpm_timeout(hcd, udev, state);
  3938. mel = calculate_max_exit_latency(udev, state, hub_encoded_timeout);
  3939. if (mel < 0) {
  3940. /* Max Exit Latency is too big, disable LPM. */
  3941. hub_encoded_timeout = USB3_LPM_DISABLED;
  3942. mel = 0;
  3943. }
  3944. ret = xhci_change_max_exit_latency(xhci, udev, mel);
  3945. if (ret)
  3946. return ret;
  3947. return hub_encoded_timeout;
  3948. }
  3949. int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd,
  3950. struct usb_device *udev, enum usb3_link_state state)
  3951. {
  3952. struct xhci_hcd *xhci;
  3953. u16 mel;
  3954. int ret;
  3955. xhci = hcd_to_xhci(hcd);
  3956. if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) ||
  3957. !xhci->devs[udev->slot_id])
  3958. return 0;
  3959. mel = calculate_max_exit_latency(udev, state, USB3_LPM_DISABLED);
  3960. ret = xhci_change_max_exit_latency(xhci, udev, mel);
  3961. if (ret)
  3962. return ret;
  3963. return 0;
  3964. }
  3965. #else /* CONFIG_PM */
  3966. int xhci_enable_usb3_lpm_timeout(struct usb_hcd *hcd,
  3967. struct usb_device *udev, enum usb3_link_state state)
  3968. {
  3969. return USB3_LPM_DISABLED;
  3970. }
  3971. int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd,
  3972. struct usb_device *udev, enum usb3_link_state state)
  3973. {
  3974. return 0;
  3975. }
  3976. #endif /* CONFIG_PM */
  3977. /*-------------------------------------------------------------------------*/
  3978. /* Once a hub descriptor is fetched for a device, we need to update the xHC's
  3979. * internal data structures for the device.
  3980. */
  3981. int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
  3982. struct usb_tt *tt, gfp_t mem_flags)
  3983. {
  3984. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  3985. struct xhci_virt_device *vdev;
  3986. struct xhci_command *config_cmd;
  3987. struct xhci_input_control_ctx *ctrl_ctx;
  3988. struct xhci_slot_ctx *slot_ctx;
  3989. unsigned long flags;
  3990. unsigned think_time;
  3991. int ret;
  3992. /* Ignore root hubs */
  3993. if (!hdev->parent)
  3994. return 0;
  3995. vdev = xhci->devs[hdev->slot_id];
  3996. if (!vdev) {
  3997. xhci_warn(xhci, "Cannot update hub desc for unknown device.\n");
  3998. return -EINVAL;
  3999. }
  4000. config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
  4001. if (!config_cmd) {
  4002. xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
  4003. return -ENOMEM;
  4004. }
  4005. spin_lock_irqsave(&xhci->lock, flags);
  4006. if (hdev->speed == USB_SPEED_HIGH &&
  4007. xhci_alloc_tt_info(xhci, vdev, hdev, tt, GFP_ATOMIC)) {
  4008. xhci_dbg(xhci, "Could not allocate xHCI TT structure.\n");
  4009. xhci_free_command(xhci, config_cmd);
  4010. spin_unlock_irqrestore(&xhci->lock, flags);
  4011. return -ENOMEM;
  4012. }
  4013. xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx);
  4014. ctrl_ctx = xhci_get_input_control_ctx(xhci, config_cmd->in_ctx);
  4015. ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
  4016. slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx);
  4017. slot_ctx->dev_info |= cpu_to_le32(DEV_HUB);
  4018. if (tt->multi)
  4019. slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
  4020. if (xhci->hci_version > 0x95) {
  4021. xhci_dbg(xhci, "xHCI version %x needs hub "
  4022. "TT think time and number of ports\n",
  4023. (unsigned int) xhci->hci_version);
  4024. slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(hdev->maxchild));
  4025. /* Set TT think time - convert from ns to FS bit times.
  4026. * 0 = 8 FS bit times, 1 = 16 FS bit times,
  4027. * 2 = 24 FS bit times, 3 = 32 FS bit times.
  4028. *
  4029. * xHCI 1.0: this field shall be 0 if the device is not a
  4030. * High-spped hub.
  4031. */
  4032. think_time = tt->think_time;
  4033. if (think_time != 0)
  4034. think_time = (think_time / 666) - 1;
  4035. if (xhci->hci_version < 0x100 || hdev->speed == USB_SPEED_HIGH)
  4036. slot_ctx->tt_info |=
  4037. cpu_to_le32(TT_THINK_TIME(think_time));
  4038. } else {
  4039. xhci_dbg(xhci, "xHCI version %x doesn't need hub "
  4040. "TT think time or number of ports\n",
  4041. (unsigned int) xhci->hci_version);
  4042. }
  4043. slot_ctx->dev_state = 0;
  4044. spin_unlock_irqrestore(&xhci->lock, flags);
  4045. xhci_dbg(xhci, "Set up %s for hub device.\n",
  4046. (xhci->hci_version > 0x95) ?
  4047. "configure endpoint" : "evaluate context");
  4048. xhci_dbg(xhci, "Slot %u Input Context:\n", hdev->slot_id);
  4049. xhci_dbg_ctx(xhci, config_cmd->in_ctx, 0);
  4050. /* Issue and wait for the configure endpoint or
  4051. * evaluate context command.
  4052. */
  4053. if (xhci->hci_version > 0x95)
  4054. ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
  4055. false, false);
  4056. else
  4057. ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
  4058. true, false);
  4059. xhci_dbg(xhci, "Slot %u Output Context:\n", hdev->slot_id);
  4060. xhci_dbg_ctx(xhci, vdev->out_ctx, 0);
  4061. xhci_free_command(xhci, config_cmd);
  4062. return ret;
  4063. }
  4064. int xhci_get_frame(struct usb_hcd *hcd)
  4065. {
  4066. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  4067. /* EHCI mods by the periodic size. Why? */
  4068. return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
  4069. }
  4070. int xhci_gen_setup(struct usb_hcd *hcd, xhci_get_quirks_t get_quirks)
  4071. {
  4072. struct xhci_hcd *xhci;
  4073. struct device *dev = hcd->self.controller;
  4074. int retval;
  4075. u32 temp;
  4076. /* Accept arbitrarily long scatter-gather lists */
  4077. hcd->self.sg_tablesize = ~0;
  4078. /* XHCI controllers don't stop the ep queue on short packets :| */
  4079. hcd->self.no_stop_on_short = 1;
  4080. if (usb_hcd_is_primary_hcd(hcd)) {
  4081. xhci = kzalloc(sizeof(struct xhci_hcd), GFP_KERNEL);
  4082. if (!xhci)
  4083. return -ENOMEM;
  4084. *((struct xhci_hcd **) hcd->hcd_priv) = xhci;
  4085. xhci->main_hcd = hcd;
  4086. /* Mark the first roothub as being USB 2.0.
  4087. * The xHCI driver will register the USB 3.0 roothub.
  4088. */
  4089. hcd->speed = HCD_USB2;
  4090. hcd->self.root_hub->speed = USB_SPEED_HIGH;
  4091. /*
  4092. * USB 2.0 roothub under xHCI has an integrated TT,
  4093. * (rate matching hub) as opposed to having an OHCI/UHCI
  4094. * companion controller.
  4095. */
  4096. hcd->has_tt = 1;
  4097. } else {
  4098. /* xHCI private pointer was set in xhci_pci_probe for the second
  4099. * registered roothub.
  4100. */
  4101. xhci = hcd_to_xhci(hcd);
  4102. temp = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
  4103. if (HCC_64BIT_ADDR(temp)) {
  4104. xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n");
  4105. dma_set_mask(hcd->self.controller, DMA_BIT_MASK(64));
  4106. } else {
  4107. dma_set_mask(hcd->self.controller, DMA_BIT_MASK(32));
  4108. }
  4109. return 0;
  4110. }
  4111. xhci->cap_regs = hcd->regs;
  4112. xhci->op_regs = hcd->regs +
  4113. HC_LENGTH(xhci_readl(xhci, &xhci->cap_regs->hc_capbase));
  4114. xhci->run_regs = hcd->regs +
  4115. (xhci_readl(xhci, &xhci->cap_regs->run_regs_off) & RTSOFF_MASK);
  4116. /* Cache read-only capability registers */
  4117. xhci->hcs_params1 = xhci_readl(xhci, &xhci->cap_regs->hcs_params1);
  4118. xhci->hcs_params2 = xhci_readl(xhci, &xhci->cap_regs->hcs_params2);
  4119. xhci->hcs_params3 = xhci_readl(xhci, &xhci->cap_regs->hcs_params3);
  4120. xhci->hcc_params = xhci_readl(xhci, &xhci->cap_regs->hc_capbase);
  4121. xhci->hci_version = HC_VERSION(xhci->hcc_params);
  4122. xhci->hcc_params = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
  4123. xhci_print_registers(xhci);
  4124. get_quirks(dev, xhci);
  4125. /* Make sure the HC is halted. */
  4126. retval = xhci_halt(xhci);
  4127. if (retval)
  4128. goto error;
  4129. xhci_dbg(xhci, "Resetting HCD\n");
  4130. /* Reset the internal HC memory state and registers. */
  4131. retval = xhci_reset(xhci);
  4132. if (retval)
  4133. goto error;
  4134. xhci_dbg(xhci, "Reset complete\n");
  4135. temp = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
  4136. if (HCC_64BIT_ADDR(temp)) {
  4137. xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n");
  4138. dma_set_mask(hcd->self.controller, DMA_BIT_MASK(64));
  4139. } else {
  4140. dma_set_mask(hcd->self.controller, DMA_BIT_MASK(32));
  4141. }
  4142. xhci_dbg(xhci, "Calling HCD init\n");
  4143. /* Initialize HCD and host controller data structures. */
  4144. retval = xhci_init(hcd);
  4145. if (retval)
  4146. goto error;
  4147. xhci_dbg(xhci, "Called HCD init\n");
  4148. return 0;
  4149. error:
  4150. kfree(xhci);
  4151. return retval;
  4152. }
  4153. MODULE_DESCRIPTION(DRIVER_DESC);
  4154. MODULE_AUTHOR(DRIVER_AUTHOR);
  4155. MODULE_LICENSE("GPL");
  4156. static int __init xhci_hcd_init(void)
  4157. {
  4158. int retval;
  4159. retval = xhci_register_pci();
  4160. if (retval < 0) {
  4161. printk(KERN_DEBUG "Problem registering PCI driver.");
  4162. return retval;
  4163. }
  4164. retval = xhci_register_plat();
  4165. if (retval < 0) {
  4166. printk(KERN_DEBUG "Problem registering platform driver.");
  4167. goto unreg_pci;
  4168. }
  4169. /*
  4170. * Check the compiler generated sizes of structures that must be laid
  4171. * out in specific ways for hardware access.
  4172. */
  4173. BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
  4174. BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
  4175. BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
  4176. /* xhci_device_control has eight fields, and also
  4177. * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
  4178. */
  4179. BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
  4180. BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
  4181. BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
  4182. BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 7*32/8);
  4183. BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
  4184. /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
  4185. BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
  4186. return 0;
  4187. unreg_pci:
  4188. xhci_unregister_pci();
  4189. return retval;
  4190. }
  4191. module_init(xhci_hcd_init);
  4192. static void __exit xhci_hcd_cleanup(void)
  4193. {
  4194. xhci_unregister_pci();
  4195. xhci_unregister_plat();
  4196. }
  4197. module_exit(xhci_hcd_cleanup);