xhci.c 150 KB

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