xhci.c 149 KB

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