xhci.c 142 KB

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