xhci.c 152 KB

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