xhci.c 131 KB

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