xhci.c 137 KB

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