xhci.c 148 KB

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