xhci.c 149 KB

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