xhci.c 145 KB

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