xhci.c 147 KB

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