xhci.c 151 KB

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