xhci.c 151 KB

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