xhci.c 147 KB

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