xhci.c 150 KB

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