xhci.c 153 KB

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