xhci.c 150 KB

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