xhci.c 148 KB

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