xhci.c 149 KB

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