xhci.c 156 KB

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