xhci.c 149 KB

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