xhci.c 149 KB

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