xhci.c 148 KB

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