xhci.c 149 KB

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