xhci.c 141 KB

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