xhci.c 113 KB

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