xhci.c 149 KB

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