xhci.c 148 KB

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