xhci.c 137 KB

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