xhci.c 155 KB

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