xhci.c 123 KB

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