xhci.c 157 KB

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