xhci.c 142 KB

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