xhci.c 148 KB

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