xhci.c 146 KB

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