xhci.c 150 KB

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