xhci.c 147 KB

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