xhci.c 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118
  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 (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. if (!pdev->irq) {
  389. xhci_err(xhci, "No msi-x/msi found and "
  390. "no IRQ in BIOS\n");
  391. return -EINVAL;
  392. }
  393. /* fall back to legacy interrupt*/
  394. ret = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED,
  395. hcd->irq_descr, hcd);
  396. if (ret) {
  397. xhci_err(xhci, "request interrupt %d failed\n",
  398. pdev->irq);
  399. return ret;
  400. }
  401. hcd->irq = pdev->irq;
  402. }
  403. #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
  404. init_timer(&xhci->event_ring_timer);
  405. xhci->event_ring_timer.data = (unsigned long) xhci;
  406. xhci->event_ring_timer.function = xhci_event_ring_work;
  407. /* Poll the event ring */
  408. xhci->event_ring_timer.expires = jiffies + POLL_TIMEOUT * HZ;
  409. xhci->zombie = 0;
  410. xhci_dbg(xhci, "Setting event ring polling timer\n");
  411. add_timer(&xhci->event_ring_timer);
  412. #endif
  413. xhci_dbg(xhci, "Command ring memory map follows:\n");
  414. xhci_debug_ring(xhci, xhci->cmd_ring);
  415. xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
  416. xhci_dbg_cmd_ptrs(xhci);
  417. xhci_dbg(xhci, "ERST memory map follows:\n");
  418. xhci_dbg_erst(xhci, &xhci->erst);
  419. xhci_dbg(xhci, "Event ring:\n");
  420. xhci_debug_ring(xhci, xhci->event_ring);
  421. xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
  422. temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
  423. temp_64 &= ~ERST_PTR_MASK;
  424. xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
  425. xhci_dbg(xhci, "// Set the interrupt modulation register\n");
  426. temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
  427. temp &= ~ER_IRQ_INTERVAL_MASK;
  428. temp |= (u32) 160;
  429. xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
  430. /* Set the HCD state before we enable the irqs */
  431. temp = xhci_readl(xhci, &xhci->op_regs->command);
  432. temp |= (CMD_EIE);
  433. xhci_dbg(xhci, "// Enable interrupts, cmd = 0x%x.\n",
  434. temp);
  435. xhci_writel(xhci, temp, &xhci->op_regs->command);
  436. temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  437. xhci_dbg(xhci, "// Enabling event ring interrupter %p by writing 0x%x to irq_pending\n",
  438. xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
  439. xhci_writel(xhci, ER_IRQ_ENABLE(temp),
  440. &xhci->ir_set->irq_pending);
  441. xhci_print_ir_set(xhci, 0);
  442. if (xhci->quirks & XHCI_NEC_HOST)
  443. xhci_queue_vendor_command(xhci, 0, 0, 0,
  444. TRB_TYPE(TRB_NEC_GET_FW));
  445. xhci_dbg(xhci, "Finished xhci_run for USB2 roothub\n");
  446. return 0;
  447. }
  448. static void xhci_only_stop_hcd(struct usb_hcd *hcd)
  449. {
  450. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  451. spin_lock_irq(&xhci->lock);
  452. xhci_halt(xhci);
  453. /* The shared_hcd is going to be deallocated shortly (the USB core only
  454. * calls this function when allocation fails in usb_add_hcd(), or
  455. * usb_remove_hcd() is called). So we need to unset xHCI's pointer.
  456. */
  457. xhci->shared_hcd = NULL;
  458. spin_unlock_irq(&xhci->lock);
  459. }
  460. /*
  461. * Stop xHCI driver.
  462. *
  463. * This function is called by the USB core when the HC driver is removed.
  464. * Its opposite is xhci_run().
  465. *
  466. * Disable device contexts, disable IRQs, and quiesce the HC.
  467. * Reset the HC, finish any completed transactions, and cleanup memory.
  468. */
  469. void xhci_stop(struct usb_hcd *hcd)
  470. {
  471. u32 temp;
  472. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  473. if (!usb_hcd_is_primary_hcd(hcd)) {
  474. xhci_only_stop_hcd(xhci->shared_hcd);
  475. return;
  476. }
  477. spin_lock_irq(&xhci->lock);
  478. /* Make sure the xHC is halted for a USB3 roothub
  479. * (xhci_stop() could be called as part of failed init).
  480. */
  481. xhci_halt(xhci);
  482. xhci_reset(xhci);
  483. spin_unlock_irq(&xhci->lock);
  484. xhci_cleanup_msix(xhci);
  485. #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
  486. /* Tell the event ring poll function not to reschedule */
  487. xhci->zombie = 1;
  488. del_timer_sync(&xhci->event_ring_timer);
  489. #endif
  490. if (xhci->quirks & XHCI_AMD_PLL_FIX)
  491. usb_amd_dev_put();
  492. xhci_dbg(xhci, "// Disabling event ring interrupts\n");
  493. temp = xhci_readl(xhci, &xhci->op_regs->status);
  494. xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
  495. temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  496. xhci_writel(xhci, ER_IRQ_DISABLE(temp),
  497. &xhci->ir_set->irq_pending);
  498. xhci_print_ir_set(xhci, 0);
  499. xhci_dbg(xhci, "cleaning up memory\n");
  500. xhci_mem_cleanup(xhci);
  501. xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
  502. xhci_readl(xhci, &xhci->op_regs->status));
  503. }
  504. /*
  505. * Shutdown HC (not bus-specific)
  506. *
  507. * This is called when the machine is rebooting or halting. We assume that the
  508. * machine will be powered off, and the HC's internal state will be reset.
  509. * Don't bother to free memory.
  510. *
  511. * This will only ever be called with the main usb_hcd (the USB3 roothub).
  512. */
  513. void xhci_shutdown(struct usb_hcd *hcd)
  514. {
  515. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  516. spin_lock_irq(&xhci->lock);
  517. xhci_halt(xhci);
  518. spin_unlock_irq(&xhci->lock);
  519. xhci_cleanup_msix(xhci);
  520. xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n",
  521. xhci_readl(xhci, &xhci->op_regs->status));
  522. }
  523. #ifdef CONFIG_PM
  524. static void xhci_save_registers(struct xhci_hcd *xhci)
  525. {
  526. xhci->s3.command = xhci_readl(xhci, &xhci->op_regs->command);
  527. xhci->s3.dev_nt = xhci_readl(xhci, &xhci->op_regs->dev_notification);
  528. xhci->s3.dcbaa_ptr = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
  529. xhci->s3.config_reg = xhci_readl(xhci, &xhci->op_regs->config_reg);
  530. xhci->s3.erst_size = xhci_readl(xhci, &xhci->ir_set->erst_size);
  531. xhci->s3.erst_base = xhci_read_64(xhci, &xhci->ir_set->erst_base);
  532. xhci->s3.erst_dequeue = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
  533. xhci->s3.irq_pending = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  534. xhci->s3.irq_control = xhci_readl(xhci, &xhci->ir_set->irq_control);
  535. }
  536. static void xhci_restore_registers(struct xhci_hcd *xhci)
  537. {
  538. xhci_writel(xhci, xhci->s3.command, &xhci->op_regs->command);
  539. xhci_writel(xhci, xhci->s3.dev_nt, &xhci->op_regs->dev_notification);
  540. xhci_write_64(xhci, xhci->s3.dcbaa_ptr, &xhci->op_regs->dcbaa_ptr);
  541. xhci_writel(xhci, xhci->s3.config_reg, &xhci->op_regs->config_reg);
  542. xhci_writel(xhci, xhci->s3.erst_size, &xhci->ir_set->erst_size);
  543. xhci_write_64(xhci, xhci->s3.erst_base, &xhci->ir_set->erst_base);
  544. xhci_write_64(xhci, xhci->s3.erst_dequeue, &xhci->ir_set->erst_dequeue);
  545. xhci_writel(xhci, xhci->s3.irq_pending, &xhci->ir_set->irq_pending);
  546. xhci_writel(xhci, xhci->s3.irq_control, &xhci->ir_set->irq_control);
  547. }
  548. static void xhci_set_cmd_ring_deq(struct xhci_hcd *xhci)
  549. {
  550. u64 val_64;
  551. /* step 2: initialize command ring buffer */
  552. val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
  553. val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
  554. (xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
  555. xhci->cmd_ring->dequeue) &
  556. (u64) ~CMD_RING_RSVD_BITS) |
  557. xhci->cmd_ring->cycle_state;
  558. xhci_dbg(xhci, "// Setting command ring address to 0x%llx\n",
  559. (long unsigned long) val_64);
  560. xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
  561. }
  562. /*
  563. * The whole command ring must be cleared to zero when we suspend the host.
  564. *
  565. * The host doesn't save the command ring pointer in the suspend well, so we
  566. * need to re-program it on resume. Unfortunately, the pointer must be 64-byte
  567. * aligned, because of the reserved bits in the command ring dequeue pointer
  568. * register. Therefore, we can't just set the dequeue pointer back in the
  569. * middle of the ring (TRBs are 16-byte aligned).
  570. */
  571. static void xhci_clear_command_ring(struct xhci_hcd *xhci)
  572. {
  573. struct xhci_ring *ring;
  574. struct xhci_segment *seg;
  575. ring = xhci->cmd_ring;
  576. seg = ring->deq_seg;
  577. do {
  578. memset(seg->trbs, 0,
  579. sizeof(union xhci_trb) * (TRBS_PER_SEGMENT - 1));
  580. seg->trbs[TRBS_PER_SEGMENT - 1].link.control &=
  581. cpu_to_le32(~TRB_CYCLE);
  582. seg = seg->next;
  583. } while (seg != ring->deq_seg);
  584. /* Reset the software enqueue and dequeue pointers */
  585. ring->deq_seg = ring->first_seg;
  586. ring->dequeue = ring->first_seg->trbs;
  587. ring->enq_seg = ring->deq_seg;
  588. ring->enqueue = ring->dequeue;
  589. /*
  590. * Ring is now zeroed, so the HW should look for change of ownership
  591. * when the cycle bit is set to 1.
  592. */
  593. ring->cycle_state = 1;
  594. /*
  595. * Reset the hardware dequeue pointer.
  596. * Yes, this will need to be re-written after resume, but we're paranoid
  597. * and want to make sure the hardware doesn't access bogus memory
  598. * because, say, the BIOS or an SMI started the host without changing
  599. * the command ring pointers.
  600. */
  601. xhci_set_cmd_ring_deq(xhci);
  602. }
  603. /*
  604. * Stop HC (not bus-specific)
  605. *
  606. * This is called when the machine transition into S3/S4 mode.
  607. *
  608. */
  609. int xhci_suspend(struct xhci_hcd *xhci)
  610. {
  611. int rc = 0;
  612. struct usb_hcd *hcd = xhci_to_hcd(xhci);
  613. u32 command;
  614. int i;
  615. spin_lock_irq(&xhci->lock);
  616. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  617. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
  618. /* step 1: stop endpoint */
  619. /* skipped assuming that port suspend has done */
  620. /* step 2: clear Run/Stop bit */
  621. command = xhci_readl(xhci, &xhci->op_regs->command);
  622. command &= ~CMD_RUN;
  623. xhci_writel(xhci, command, &xhci->op_regs->command);
  624. if (handshake(xhci, &xhci->op_regs->status,
  625. STS_HALT, STS_HALT, 100*100)) {
  626. xhci_warn(xhci, "WARN: xHC CMD_RUN timeout\n");
  627. spin_unlock_irq(&xhci->lock);
  628. return -ETIMEDOUT;
  629. }
  630. xhci_clear_command_ring(xhci);
  631. /* step 3: save registers */
  632. xhci_save_registers(xhci);
  633. /* step 4: set CSS flag */
  634. command = xhci_readl(xhci, &xhci->op_regs->command);
  635. command |= CMD_CSS;
  636. xhci_writel(xhci, command, &xhci->op_regs->command);
  637. if (handshake(xhci, &xhci->op_regs->status, STS_SAVE, 0, 10*100)) {
  638. xhci_warn(xhci, "WARN: xHC CMD_CSS timeout\n");
  639. spin_unlock_irq(&xhci->lock);
  640. return -ETIMEDOUT;
  641. }
  642. spin_unlock_irq(&xhci->lock);
  643. /* step 5: remove core well power */
  644. /* synchronize irq when using MSI-X */
  645. if (xhci->msix_entries) {
  646. for (i = 0; i < xhci->msix_count; i++)
  647. synchronize_irq(xhci->msix_entries[i].vector);
  648. }
  649. return rc;
  650. }
  651. /*
  652. * start xHC (not bus-specific)
  653. *
  654. * This is called when the machine transition from S3/S4 mode.
  655. *
  656. */
  657. int xhci_resume(struct xhci_hcd *xhci, bool hibernated)
  658. {
  659. u32 command, temp = 0;
  660. struct usb_hcd *hcd = xhci_to_hcd(xhci);
  661. struct usb_hcd *secondary_hcd;
  662. int retval = 0;
  663. /* Wait a bit if either of the roothubs need to settle from the
  664. * transition into bus suspend.
  665. */
  666. if (time_before(jiffies, xhci->bus_state[0].next_statechange) ||
  667. time_before(jiffies,
  668. xhci->bus_state[1].next_statechange))
  669. msleep(100);
  670. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  671. set_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
  672. spin_lock_irq(&xhci->lock);
  673. if (xhci->quirks & XHCI_RESET_ON_RESUME)
  674. hibernated = true;
  675. if (!hibernated) {
  676. /* step 1: restore register */
  677. xhci_restore_registers(xhci);
  678. /* step 2: initialize command ring buffer */
  679. xhci_set_cmd_ring_deq(xhci);
  680. /* step 3: restore state and start state*/
  681. /* step 3: set CRS flag */
  682. command = xhci_readl(xhci, &xhci->op_regs->command);
  683. command |= CMD_CRS;
  684. xhci_writel(xhci, command, &xhci->op_regs->command);
  685. if (handshake(xhci, &xhci->op_regs->status,
  686. STS_RESTORE, 0, 10*100)) {
  687. xhci_dbg(xhci, "WARN: xHC CMD_CSS timeout\n");
  688. spin_unlock_irq(&xhci->lock);
  689. return -ETIMEDOUT;
  690. }
  691. temp = xhci_readl(xhci, &xhci->op_regs->status);
  692. }
  693. /* If restore operation fails, re-initialize the HC during resume */
  694. if ((temp & STS_SRE) || hibernated) {
  695. /* Let the USB core know _both_ roothubs lost power. */
  696. usb_root_hub_lost_power(xhci->main_hcd->self.root_hub);
  697. usb_root_hub_lost_power(xhci->shared_hcd->self.root_hub);
  698. xhci_dbg(xhci, "Stop HCD\n");
  699. xhci_halt(xhci);
  700. xhci_reset(xhci);
  701. spin_unlock_irq(&xhci->lock);
  702. xhci_cleanup_msix(xhci);
  703. #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
  704. /* Tell the event ring poll function not to reschedule */
  705. xhci->zombie = 1;
  706. del_timer_sync(&xhci->event_ring_timer);
  707. #endif
  708. xhci_dbg(xhci, "// Disabling event ring interrupts\n");
  709. temp = xhci_readl(xhci, &xhci->op_regs->status);
  710. xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
  711. temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  712. xhci_writel(xhci, ER_IRQ_DISABLE(temp),
  713. &xhci->ir_set->irq_pending);
  714. xhci_print_ir_set(xhci, 0);
  715. xhci_dbg(xhci, "cleaning up memory\n");
  716. xhci_mem_cleanup(xhci);
  717. xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
  718. xhci_readl(xhci, &xhci->op_regs->status));
  719. /* USB core calls the PCI reinit and start functions twice:
  720. * first with the primary HCD, and then with the secondary HCD.
  721. * If we don't do the same, the host will never be started.
  722. */
  723. if (!usb_hcd_is_primary_hcd(hcd))
  724. secondary_hcd = hcd;
  725. else
  726. secondary_hcd = xhci->shared_hcd;
  727. xhci_dbg(xhci, "Initialize the xhci_hcd\n");
  728. retval = xhci_init(hcd->primary_hcd);
  729. if (retval)
  730. return retval;
  731. xhci_dbg(xhci, "Start the primary HCD\n");
  732. retval = xhci_run(hcd->primary_hcd);
  733. if (!retval) {
  734. xhci_dbg(xhci, "Start the secondary HCD\n");
  735. retval = xhci_run(secondary_hcd);
  736. }
  737. hcd->state = HC_STATE_SUSPENDED;
  738. xhci->shared_hcd->state = HC_STATE_SUSPENDED;
  739. goto done;
  740. }
  741. /* step 4: set Run/Stop bit */
  742. command = xhci_readl(xhci, &xhci->op_regs->command);
  743. command |= CMD_RUN;
  744. xhci_writel(xhci, command, &xhci->op_regs->command);
  745. handshake(xhci, &xhci->op_regs->status, STS_HALT,
  746. 0, 250 * 1000);
  747. /* step 5: walk topology and initialize portsc,
  748. * portpmsc and portli
  749. */
  750. /* this is done in bus_resume */
  751. /* step 6: restart each of the previously
  752. * Running endpoints by ringing their doorbells
  753. */
  754. spin_unlock_irq(&xhci->lock);
  755. done:
  756. if (retval == 0) {
  757. usb_hcd_resume_root_hub(hcd);
  758. usb_hcd_resume_root_hub(xhci->shared_hcd);
  759. }
  760. return retval;
  761. }
  762. #endif /* CONFIG_PM */
  763. /*-------------------------------------------------------------------------*/
  764. /**
  765. * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
  766. * HCDs. Find the index for an endpoint given its descriptor. Use the return
  767. * value to right shift 1 for the bitmask.
  768. *
  769. * Index = (epnum * 2) + direction - 1,
  770. * where direction = 0 for OUT, 1 for IN.
  771. * For control endpoints, the IN index is used (OUT index is unused), so
  772. * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
  773. */
  774. unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
  775. {
  776. unsigned int index;
  777. if (usb_endpoint_xfer_control(desc))
  778. index = (unsigned int) (usb_endpoint_num(desc)*2);
  779. else
  780. index = (unsigned int) (usb_endpoint_num(desc)*2) +
  781. (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
  782. return index;
  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(struct usb_endpoint_descriptor *desc)
  789. {
  790. return 1 << (xhci_get_endpoint_index(desc) + 1);
  791. }
  792. /* Find the flag for this endpoint (for use in the control context). Use the
  793. * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
  794. * bit 1, etc.
  795. */
  796. unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index)
  797. {
  798. return 1 << (ep_index + 1);
  799. }
  800. /* Compute the last valid endpoint context index. Basically, this is the
  801. * endpoint index plus one. For slot contexts with more than valid endpoint,
  802. * we find the most significant bit set in the added contexts flags.
  803. * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
  804. * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
  805. */
  806. unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
  807. {
  808. return fls(added_ctxs) - 1;
  809. }
  810. /* Returns 1 if the arguments are OK;
  811. * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
  812. */
  813. static int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
  814. struct usb_host_endpoint *ep, int check_ep, bool check_virt_dev,
  815. const char *func) {
  816. struct xhci_hcd *xhci;
  817. struct xhci_virt_device *virt_dev;
  818. if (!hcd || (check_ep && !ep) || !udev) {
  819. printk(KERN_DEBUG "xHCI %s called with invalid args\n",
  820. func);
  821. return -EINVAL;
  822. }
  823. if (!udev->parent) {
  824. printk(KERN_DEBUG "xHCI %s called for root hub\n",
  825. func);
  826. return 0;
  827. }
  828. xhci = hcd_to_xhci(hcd);
  829. if (xhci->xhc_state & XHCI_STATE_HALTED)
  830. return -ENODEV;
  831. if (check_virt_dev) {
  832. if (!udev->slot_id || !xhci->devs
  833. || !xhci->devs[udev->slot_id]) {
  834. printk(KERN_DEBUG "xHCI %s called with unaddressed "
  835. "device\n", func);
  836. return -EINVAL;
  837. }
  838. virt_dev = xhci->devs[udev->slot_id];
  839. if (virt_dev->udev != udev) {
  840. printk(KERN_DEBUG "xHCI %s called with udev and "
  841. "virt_dev does not match\n", func);
  842. return -EINVAL;
  843. }
  844. }
  845. return 1;
  846. }
  847. static int xhci_configure_endpoint(struct xhci_hcd *xhci,
  848. struct usb_device *udev, struct xhci_command *command,
  849. bool ctx_change, bool must_succeed);
  850. /*
  851. * Full speed devices may have a max packet size greater than 8 bytes, but the
  852. * USB core doesn't know that until it reads the first 8 bytes of the
  853. * descriptor. If the usb_device's max packet size changes after that point,
  854. * we need to issue an evaluate context command and wait on it.
  855. */
  856. static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
  857. unsigned int ep_index, struct urb *urb)
  858. {
  859. struct xhci_container_ctx *in_ctx;
  860. struct xhci_container_ctx *out_ctx;
  861. struct xhci_input_control_ctx *ctrl_ctx;
  862. struct xhci_ep_ctx *ep_ctx;
  863. int max_packet_size;
  864. int hw_max_packet_size;
  865. int ret = 0;
  866. out_ctx = xhci->devs[slot_id]->out_ctx;
  867. ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
  868. hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
  869. max_packet_size = le16_to_cpu(urb->dev->ep0.desc.wMaxPacketSize);
  870. if (hw_max_packet_size != max_packet_size) {
  871. xhci_dbg(xhci, "Max Packet Size for ep 0 changed.\n");
  872. xhci_dbg(xhci, "Max packet size in usb_device = %d\n",
  873. max_packet_size);
  874. xhci_dbg(xhci, "Max packet size in xHCI HW = %d\n",
  875. hw_max_packet_size);
  876. xhci_dbg(xhci, "Issuing evaluate context command.\n");
  877. /* Set up the modified control endpoint 0 */
  878. xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
  879. xhci->devs[slot_id]->out_ctx, ep_index);
  880. in_ctx = xhci->devs[slot_id]->in_ctx;
  881. ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
  882. ep_ctx->ep_info2 &= cpu_to_le32(~MAX_PACKET_MASK);
  883. ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size));
  884. /* Set up the input context flags for the command */
  885. /* FIXME: This won't work if a non-default control endpoint
  886. * changes max packet sizes.
  887. */
  888. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  889. ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
  890. ctrl_ctx->drop_flags = 0;
  891. xhci_dbg(xhci, "Slot %d input context\n", slot_id);
  892. xhci_dbg_ctx(xhci, in_ctx, ep_index);
  893. xhci_dbg(xhci, "Slot %d output context\n", slot_id);
  894. xhci_dbg_ctx(xhci, out_ctx, ep_index);
  895. ret = xhci_configure_endpoint(xhci, urb->dev, NULL,
  896. true, false);
  897. /* Clean up the input context for later use by bandwidth
  898. * functions.
  899. */
  900. ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
  901. }
  902. return ret;
  903. }
  904. /*
  905. * non-error returns are a promise to giveback() the urb later
  906. * we drop ownership so next owner (or urb unlink) can get it
  907. */
  908. int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
  909. {
  910. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  911. unsigned long flags;
  912. int ret = 0;
  913. unsigned int slot_id, ep_index;
  914. struct urb_priv *urb_priv;
  915. int size, i;
  916. if (!urb || xhci_check_args(hcd, urb->dev, urb->ep,
  917. true, true, __func__) <= 0)
  918. return -EINVAL;
  919. slot_id = urb->dev->slot_id;
  920. ep_index = xhci_get_endpoint_index(&urb->ep->desc);
  921. if (!HCD_HW_ACCESSIBLE(hcd)) {
  922. if (!in_interrupt())
  923. xhci_dbg(xhci, "urb submitted during PCI suspend\n");
  924. ret = -ESHUTDOWN;
  925. goto exit;
  926. }
  927. if (usb_endpoint_xfer_isoc(&urb->ep->desc))
  928. size = urb->number_of_packets;
  929. else
  930. size = 1;
  931. urb_priv = kzalloc(sizeof(struct urb_priv) +
  932. size * sizeof(struct xhci_td *), mem_flags);
  933. if (!urb_priv)
  934. return -ENOMEM;
  935. for (i = 0; i < size; i++) {
  936. urb_priv->td[i] = kzalloc(sizeof(struct xhci_td), mem_flags);
  937. if (!urb_priv->td[i]) {
  938. urb_priv->length = i;
  939. xhci_urb_free_priv(xhci, urb_priv);
  940. return -ENOMEM;
  941. }
  942. }
  943. urb_priv->length = size;
  944. urb_priv->td_cnt = 0;
  945. urb->hcpriv = urb_priv;
  946. if (usb_endpoint_xfer_control(&urb->ep->desc)) {
  947. /* Check to see if the max packet size for the default control
  948. * endpoint changed during FS device enumeration
  949. */
  950. if (urb->dev->speed == USB_SPEED_FULL) {
  951. ret = xhci_check_maxpacket(xhci, slot_id,
  952. ep_index, urb);
  953. if (ret < 0) {
  954. xhci_urb_free_priv(xhci, urb_priv);
  955. urb->hcpriv = NULL;
  956. return ret;
  957. }
  958. }
  959. /* We have a spinlock and interrupts disabled, so we must pass
  960. * atomic context to this function, which may allocate memory.
  961. */
  962. spin_lock_irqsave(&xhci->lock, flags);
  963. if (xhci->xhc_state & XHCI_STATE_DYING)
  964. goto dying;
  965. ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
  966. slot_id, ep_index);
  967. if (ret)
  968. goto free_priv;
  969. spin_unlock_irqrestore(&xhci->lock, flags);
  970. } else if (usb_endpoint_xfer_bulk(&urb->ep->desc)) {
  971. spin_lock_irqsave(&xhci->lock, flags);
  972. if (xhci->xhc_state & XHCI_STATE_DYING)
  973. goto dying;
  974. if (xhci->devs[slot_id]->eps[ep_index].ep_state &
  975. EP_GETTING_STREAMS) {
  976. xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
  977. "is transitioning to using streams.\n");
  978. ret = -EINVAL;
  979. } else if (xhci->devs[slot_id]->eps[ep_index].ep_state &
  980. EP_GETTING_NO_STREAMS) {
  981. xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
  982. "is transitioning to "
  983. "not having streams.\n");
  984. ret = -EINVAL;
  985. } else {
  986. ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
  987. slot_id, ep_index);
  988. }
  989. if (ret)
  990. goto free_priv;
  991. spin_unlock_irqrestore(&xhci->lock, flags);
  992. } else if (usb_endpoint_xfer_int(&urb->ep->desc)) {
  993. spin_lock_irqsave(&xhci->lock, flags);
  994. if (xhci->xhc_state & XHCI_STATE_DYING)
  995. goto dying;
  996. ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
  997. slot_id, ep_index);
  998. if (ret)
  999. goto free_priv;
  1000. spin_unlock_irqrestore(&xhci->lock, flags);
  1001. } else {
  1002. spin_lock_irqsave(&xhci->lock, flags);
  1003. if (xhci->xhc_state & XHCI_STATE_DYING)
  1004. goto dying;
  1005. ret = xhci_queue_isoc_tx_prepare(xhci, GFP_ATOMIC, urb,
  1006. slot_id, ep_index);
  1007. if (ret)
  1008. goto free_priv;
  1009. spin_unlock_irqrestore(&xhci->lock, flags);
  1010. }
  1011. exit:
  1012. return ret;
  1013. dying:
  1014. xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for "
  1015. "non-responsive xHCI host.\n",
  1016. urb->ep->desc.bEndpointAddress, urb);
  1017. ret = -ESHUTDOWN;
  1018. free_priv:
  1019. xhci_urb_free_priv(xhci, urb_priv);
  1020. urb->hcpriv = NULL;
  1021. spin_unlock_irqrestore(&xhci->lock, flags);
  1022. return ret;
  1023. }
  1024. /* Get the right ring for the given URB.
  1025. * If the endpoint supports streams, boundary check the URB's stream ID.
  1026. * If the endpoint doesn't support streams, return the singular endpoint ring.
  1027. */
  1028. static struct xhci_ring *xhci_urb_to_transfer_ring(struct xhci_hcd *xhci,
  1029. struct urb *urb)
  1030. {
  1031. unsigned int slot_id;
  1032. unsigned int ep_index;
  1033. unsigned int stream_id;
  1034. struct xhci_virt_ep *ep;
  1035. slot_id = urb->dev->slot_id;
  1036. ep_index = xhci_get_endpoint_index(&urb->ep->desc);
  1037. stream_id = urb->stream_id;
  1038. ep = &xhci->devs[slot_id]->eps[ep_index];
  1039. /* Common case: no streams */
  1040. if (!(ep->ep_state & EP_HAS_STREAMS))
  1041. return ep->ring;
  1042. if (stream_id == 0) {
  1043. xhci_warn(xhci,
  1044. "WARN: Slot ID %u, ep index %u has streams, "
  1045. "but URB has no stream ID.\n",
  1046. slot_id, ep_index);
  1047. return NULL;
  1048. }
  1049. if (stream_id < ep->stream_info->num_streams)
  1050. return ep->stream_info->stream_rings[stream_id];
  1051. xhci_warn(xhci,
  1052. "WARN: Slot ID %u, ep index %u has "
  1053. "stream IDs 1 to %u allocated, "
  1054. "but stream ID %u is requested.\n",
  1055. slot_id, ep_index,
  1056. ep->stream_info->num_streams - 1,
  1057. stream_id);
  1058. return NULL;
  1059. }
  1060. /*
  1061. * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
  1062. * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
  1063. * should pick up where it left off in the TD, unless a Set Transfer Ring
  1064. * Dequeue Pointer is issued.
  1065. *
  1066. * The TRBs that make up the buffers for the canceled URB will be "removed" from
  1067. * the ring. Since the ring is a contiguous structure, they can't be physically
  1068. * removed. Instead, there are two options:
  1069. *
  1070. * 1) If the HC is in the middle of processing the URB to be canceled, we
  1071. * simply move the ring's dequeue pointer past those TRBs using the Set
  1072. * Transfer Ring Dequeue Pointer command. This will be the common case,
  1073. * when drivers timeout on the last submitted URB and attempt to cancel.
  1074. *
  1075. * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
  1076. * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
  1077. * HC will need to invalidate the any TRBs it has cached after the stop
  1078. * endpoint command, as noted in the xHCI 0.95 errata.
  1079. *
  1080. * 3) The TD may have completed by the time the Stop Endpoint Command
  1081. * completes, so software needs to handle that case too.
  1082. *
  1083. * This function should protect against the TD enqueueing code ringing the
  1084. * doorbell while this code is waiting for a Stop Endpoint command to complete.
  1085. * It also needs to account for multiple cancellations on happening at the same
  1086. * time for the same endpoint.
  1087. *
  1088. * Note that this function can be called in any context, or so says
  1089. * usb_hcd_unlink_urb()
  1090. */
  1091. int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  1092. {
  1093. unsigned long flags;
  1094. int ret, i;
  1095. u32 temp;
  1096. struct xhci_hcd *xhci;
  1097. struct urb_priv *urb_priv;
  1098. struct xhci_td *td;
  1099. unsigned int ep_index;
  1100. struct xhci_ring *ep_ring;
  1101. struct xhci_virt_ep *ep;
  1102. xhci = hcd_to_xhci(hcd);
  1103. spin_lock_irqsave(&xhci->lock, flags);
  1104. /* Make sure the URB hasn't completed or been unlinked already */
  1105. ret = usb_hcd_check_unlink_urb(hcd, urb, status);
  1106. if (ret || !urb->hcpriv)
  1107. goto done;
  1108. temp = xhci_readl(xhci, &xhci->op_regs->status);
  1109. if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_HALTED)) {
  1110. xhci_dbg(xhci, "HW died, freeing TD.\n");
  1111. urb_priv = urb->hcpriv;
  1112. for (i = urb_priv->td_cnt; i < urb_priv->length; i++) {
  1113. td = urb_priv->td[i];
  1114. if (!list_empty(&td->td_list))
  1115. list_del_init(&td->td_list);
  1116. if (!list_empty(&td->cancelled_td_list))
  1117. list_del_init(&td->cancelled_td_list);
  1118. }
  1119. usb_hcd_unlink_urb_from_ep(hcd, urb);
  1120. spin_unlock_irqrestore(&xhci->lock, flags);
  1121. usb_hcd_giveback_urb(hcd, urb, -ESHUTDOWN);
  1122. xhci_urb_free_priv(xhci, urb_priv);
  1123. return ret;
  1124. }
  1125. if ((xhci->xhc_state & XHCI_STATE_DYING) ||
  1126. (xhci->xhc_state & XHCI_STATE_HALTED)) {
  1127. xhci_dbg(xhci, "Ep 0x%x: URB %p to be canceled on "
  1128. "non-responsive xHCI host.\n",
  1129. urb->ep->desc.bEndpointAddress, urb);
  1130. /* Let the stop endpoint command watchdog timer (which set this
  1131. * state) finish cleaning up the endpoint TD lists. We must
  1132. * have caught it in the middle of dropping a lock and giving
  1133. * back an URB.
  1134. */
  1135. goto done;
  1136. }
  1137. xhci_dbg(xhci, "Cancel URB %p\n", urb);
  1138. xhci_dbg(xhci, "Event ring:\n");
  1139. xhci_debug_ring(xhci, xhci->event_ring);
  1140. ep_index = xhci_get_endpoint_index(&urb->ep->desc);
  1141. ep = &xhci->devs[urb->dev->slot_id]->eps[ep_index];
  1142. ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
  1143. if (!ep_ring) {
  1144. ret = -EINVAL;
  1145. goto done;
  1146. }
  1147. xhci_dbg(xhci, "Endpoint ring:\n");
  1148. xhci_debug_ring(xhci, ep_ring);
  1149. urb_priv = urb->hcpriv;
  1150. for (i = urb_priv->td_cnt; i < urb_priv->length; i++) {
  1151. td = urb_priv->td[i];
  1152. list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
  1153. }
  1154. /* Queue a stop endpoint command, but only if this is
  1155. * the first cancellation to be handled.
  1156. */
  1157. if (!(ep->ep_state & EP_HALT_PENDING)) {
  1158. ep->ep_state |= EP_HALT_PENDING;
  1159. ep->stop_cmds_pending++;
  1160. ep->stop_cmd_timer.expires = jiffies +
  1161. XHCI_STOP_EP_CMD_TIMEOUT * HZ;
  1162. add_timer(&ep->stop_cmd_timer);
  1163. xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index, 0);
  1164. xhci_ring_cmd_db(xhci);
  1165. }
  1166. done:
  1167. spin_unlock_irqrestore(&xhci->lock, flags);
  1168. return ret;
  1169. }
  1170. /* Drop an endpoint from a new bandwidth configuration for this device.
  1171. * Only one call to this function is allowed per endpoint before
  1172. * check_bandwidth() or reset_bandwidth() must be called.
  1173. * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
  1174. * add the endpoint to the schedule with possibly new parameters denoted by a
  1175. * different endpoint descriptor in usb_host_endpoint.
  1176. * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
  1177. * not allowed.
  1178. *
  1179. * The USB core will not allow URBs to be queued to an endpoint that is being
  1180. * disabled, so there's no need for mutual exclusion to protect
  1181. * the xhci->devs[slot_id] structure.
  1182. */
  1183. int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
  1184. struct usb_host_endpoint *ep)
  1185. {
  1186. struct xhci_hcd *xhci;
  1187. struct xhci_container_ctx *in_ctx, *out_ctx;
  1188. struct xhci_input_control_ctx *ctrl_ctx;
  1189. struct xhci_slot_ctx *slot_ctx;
  1190. unsigned int last_ctx;
  1191. unsigned int ep_index;
  1192. struct xhci_ep_ctx *ep_ctx;
  1193. u32 drop_flag;
  1194. u32 new_add_flags, new_drop_flags, new_slot_info;
  1195. int ret;
  1196. ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
  1197. if (ret <= 0)
  1198. return ret;
  1199. xhci = hcd_to_xhci(hcd);
  1200. if (xhci->xhc_state & XHCI_STATE_DYING)
  1201. return -ENODEV;
  1202. xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
  1203. drop_flag = xhci_get_endpoint_flag(&ep->desc);
  1204. if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
  1205. xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
  1206. __func__, drop_flag);
  1207. return 0;
  1208. }
  1209. in_ctx = xhci->devs[udev->slot_id]->in_ctx;
  1210. out_ctx = xhci->devs[udev->slot_id]->out_ctx;
  1211. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  1212. ep_index = xhci_get_endpoint_index(&ep->desc);
  1213. ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
  1214. /* If the HC already knows the endpoint is disabled,
  1215. * or the HCD has noted it is disabled, ignore this request
  1216. */
  1217. if ((le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK) ==
  1218. EP_STATE_DISABLED ||
  1219. le32_to_cpu(ctrl_ctx->drop_flags) &
  1220. xhci_get_endpoint_flag(&ep->desc)) {
  1221. xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
  1222. __func__, ep);
  1223. return 0;
  1224. }
  1225. ctrl_ctx->drop_flags |= cpu_to_le32(drop_flag);
  1226. new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
  1227. ctrl_ctx->add_flags &= cpu_to_le32(~drop_flag);
  1228. new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
  1229. last_ctx = xhci_last_valid_endpoint(le32_to_cpu(ctrl_ctx->add_flags));
  1230. slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
  1231. /* Update the last valid endpoint context, if we deleted the last one */
  1232. if ((le32_to_cpu(slot_ctx->dev_info) & LAST_CTX_MASK) >
  1233. LAST_CTX(last_ctx)) {
  1234. slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
  1235. slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(last_ctx));
  1236. }
  1237. new_slot_info = le32_to_cpu(slot_ctx->dev_info);
  1238. xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
  1239. xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
  1240. (unsigned int) ep->desc.bEndpointAddress,
  1241. udev->slot_id,
  1242. (unsigned int) new_drop_flags,
  1243. (unsigned int) new_add_flags,
  1244. (unsigned int) new_slot_info);
  1245. return 0;
  1246. }
  1247. /* Add an endpoint to a new possible bandwidth configuration for this device.
  1248. * Only one call to this function is allowed per endpoint before
  1249. * check_bandwidth() or reset_bandwidth() must be called.
  1250. * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
  1251. * add the endpoint to the schedule with possibly new parameters denoted by a
  1252. * different endpoint descriptor in usb_host_endpoint.
  1253. * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
  1254. * not allowed.
  1255. *
  1256. * The USB core will not allow URBs to be queued to an endpoint until the
  1257. * configuration or alt setting is installed in the device, so there's no need
  1258. * for mutual exclusion to protect the xhci->devs[slot_id] structure.
  1259. */
  1260. int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
  1261. struct usb_host_endpoint *ep)
  1262. {
  1263. struct xhci_hcd *xhci;
  1264. struct xhci_container_ctx *in_ctx, *out_ctx;
  1265. unsigned int ep_index;
  1266. struct xhci_ep_ctx *ep_ctx;
  1267. struct xhci_slot_ctx *slot_ctx;
  1268. struct xhci_input_control_ctx *ctrl_ctx;
  1269. u32 added_ctxs;
  1270. unsigned int last_ctx;
  1271. u32 new_add_flags, new_drop_flags, new_slot_info;
  1272. struct xhci_virt_device *virt_dev;
  1273. int ret = 0;
  1274. ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
  1275. if (ret <= 0) {
  1276. /* So we won't queue a reset ep command for a root hub */
  1277. ep->hcpriv = NULL;
  1278. return ret;
  1279. }
  1280. xhci = hcd_to_xhci(hcd);
  1281. if (xhci->xhc_state & XHCI_STATE_DYING)
  1282. return -ENODEV;
  1283. added_ctxs = xhci_get_endpoint_flag(&ep->desc);
  1284. last_ctx = xhci_last_valid_endpoint(added_ctxs);
  1285. if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
  1286. /* FIXME when we have to issue an evaluate endpoint command to
  1287. * deal with ep0 max packet size changing once we get the
  1288. * descriptors
  1289. */
  1290. xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
  1291. __func__, added_ctxs);
  1292. return 0;
  1293. }
  1294. virt_dev = xhci->devs[udev->slot_id];
  1295. in_ctx = virt_dev->in_ctx;
  1296. out_ctx = virt_dev->out_ctx;
  1297. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  1298. ep_index = xhci_get_endpoint_index(&ep->desc);
  1299. ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
  1300. /* If this endpoint is already in use, and the upper layers are trying
  1301. * to add it again without dropping it, reject the addition.
  1302. */
  1303. if (virt_dev->eps[ep_index].ring &&
  1304. !(le32_to_cpu(ctrl_ctx->drop_flags) &
  1305. xhci_get_endpoint_flag(&ep->desc))) {
  1306. xhci_warn(xhci, "Trying to add endpoint 0x%x "
  1307. "without dropping it.\n",
  1308. (unsigned int) ep->desc.bEndpointAddress);
  1309. return -EINVAL;
  1310. }
  1311. /* If the HCD has already noted the endpoint is enabled,
  1312. * ignore this request.
  1313. */
  1314. if (le32_to_cpu(ctrl_ctx->add_flags) &
  1315. xhci_get_endpoint_flag(&ep->desc)) {
  1316. xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
  1317. __func__, ep);
  1318. return 0;
  1319. }
  1320. /*
  1321. * Configuration and alternate setting changes must be done in
  1322. * process context, not interrupt context (or so documenation
  1323. * for usb_set_interface() and usb_set_configuration() claim).
  1324. */
  1325. if (xhci_endpoint_init(xhci, virt_dev, udev, ep, GFP_NOIO) < 0) {
  1326. dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
  1327. __func__, ep->desc.bEndpointAddress);
  1328. return -ENOMEM;
  1329. }
  1330. ctrl_ctx->add_flags |= cpu_to_le32(added_ctxs);
  1331. new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
  1332. /* If xhci_endpoint_disable() was called for this endpoint, but the
  1333. * xHC hasn't been notified yet through the check_bandwidth() call,
  1334. * this re-adds a new state for the endpoint from the new endpoint
  1335. * descriptors. We must drop and re-add this endpoint, so we leave the
  1336. * drop flags alone.
  1337. */
  1338. new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
  1339. slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
  1340. /* Update the last valid endpoint context, if we just added one past */
  1341. if ((le32_to_cpu(slot_ctx->dev_info) & LAST_CTX_MASK) <
  1342. LAST_CTX(last_ctx)) {
  1343. slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
  1344. slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(last_ctx));
  1345. }
  1346. new_slot_info = le32_to_cpu(slot_ctx->dev_info);
  1347. /* Store the usb_device pointer for later use */
  1348. ep->hcpriv = udev;
  1349. xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
  1350. (unsigned int) ep->desc.bEndpointAddress,
  1351. udev->slot_id,
  1352. (unsigned int) new_drop_flags,
  1353. (unsigned int) new_add_flags,
  1354. (unsigned int) new_slot_info);
  1355. return 0;
  1356. }
  1357. static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev)
  1358. {
  1359. struct xhci_input_control_ctx *ctrl_ctx;
  1360. struct xhci_ep_ctx *ep_ctx;
  1361. struct xhci_slot_ctx *slot_ctx;
  1362. int i;
  1363. /* When a device's add flag and drop flag are zero, any subsequent
  1364. * configure endpoint command will leave that endpoint's state
  1365. * untouched. Make sure we don't leave any old state in the input
  1366. * endpoint contexts.
  1367. */
  1368. ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
  1369. ctrl_ctx->drop_flags = 0;
  1370. ctrl_ctx->add_flags = 0;
  1371. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
  1372. slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
  1373. /* Endpoint 0 is always valid */
  1374. slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
  1375. for (i = 1; i < 31; ++i) {
  1376. ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
  1377. ep_ctx->ep_info = 0;
  1378. ep_ctx->ep_info2 = 0;
  1379. ep_ctx->deq = 0;
  1380. ep_ctx->tx_info = 0;
  1381. }
  1382. }
  1383. static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
  1384. struct usb_device *udev, u32 *cmd_status)
  1385. {
  1386. int ret;
  1387. switch (*cmd_status) {
  1388. case COMP_ENOMEM:
  1389. dev_warn(&udev->dev, "Not enough host controller resources "
  1390. "for new device state.\n");
  1391. ret = -ENOMEM;
  1392. /* FIXME: can we allocate more resources for the HC? */
  1393. break;
  1394. case COMP_BW_ERR:
  1395. case COMP_2ND_BW_ERR:
  1396. dev_warn(&udev->dev, "Not enough bandwidth "
  1397. "for new device state.\n");
  1398. ret = -ENOSPC;
  1399. /* FIXME: can we go back to the old state? */
  1400. break;
  1401. case COMP_TRB_ERR:
  1402. /* the HCD set up something wrong */
  1403. dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
  1404. "add flag = 1, "
  1405. "and endpoint is not disabled.\n");
  1406. ret = -EINVAL;
  1407. break;
  1408. case COMP_DEV_ERR:
  1409. dev_warn(&udev->dev, "ERROR: Incompatible device for endpoint "
  1410. "configure command.\n");
  1411. ret = -ENODEV;
  1412. break;
  1413. case COMP_SUCCESS:
  1414. dev_dbg(&udev->dev, "Successful Endpoint Configure command\n");
  1415. ret = 0;
  1416. break;
  1417. default:
  1418. xhci_err(xhci, "ERROR: unexpected command completion "
  1419. "code 0x%x.\n", *cmd_status);
  1420. ret = -EINVAL;
  1421. break;
  1422. }
  1423. return ret;
  1424. }
  1425. static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
  1426. struct usb_device *udev, u32 *cmd_status)
  1427. {
  1428. int ret;
  1429. struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
  1430. switch (*cmd_status) {
  1431. case COMP_EINVAL:
  1432. dev_warn(&udev->dev, "WARN: xHCI driver setup invalid evaluate "
  1433. "context command.\n");
  1434. ret = -EINVAL;
  1435. break;
  1436. case COMP_EBADSLT:
  1437. dev_warn(&udev->dev, "WARN: slot not enabled for"
  1438. "evaluate context command.\n");
  1439. case COMP_CTX_STATE:
  1440. dev_warn(&udev->dev, "WARN: invalid context state for "
  1441. "evaluate context command.\n");
  1442. xhci_dbg_ctx(xhci, virt_dev->out_ctx, 1);
  1443. ret = -EINVAL;
  1444. break;
  1445. case COMP_DEV_ERR:
  1446. dev_warn(&udev->dev, "ERROR: Incompatible device for evaluate "
  1447. "context command.\n");
  1448. ret = -ENODEV;
  1449. break;
  1450. case COMP_MEL_ERR:
  1451. /* Max Exit Latency too large error */
  1452. dev_warn(&udev->dev, "WARN: Max Exit Latency too large\n");
  1453. ret = -EINVAL;
  1454. break;
  1455. case COMP_SUCCESS:
  1456. dev_dbg(&udev->dev, "Successful evaluate context command\n");
  1457. ret = 0;
  1458. break;
  1459. default:
  1460. xhci_err(xhci, "ERROR: unexpected command completion "
  1461. "code 0x%x.\n", *cmd_status);
  1462. ret = -EINVAL;
  1463. break;
  1464. }
  1465. return ret;
  1466. }
  1467. static u32 xhci_count_num_new_endpoints(struct xhci_hcd *xhci,
  1468. struct xhci_container_ctx *in_ctx)
  1469. {
  1470. struct xhci_input_control_ctx *ctrl_ctx;
  1471. u32 valid_add_flags;
  1472. u32 valid_drop_flags;
  1473. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  1474. /* Ignore the slot flag (bit 0), and the default control endpoint flag
  1475. * (bit 1). The default control endpoint is added during the Address
  1476. * Device command and is never removed until the slot is disabled.
  1477. */
  1478. valid_add_flags = ctrl_ctx->add_flags >> 2;
  1479. valid_drop_flags = ctrl_ctx->drop_flags >> 2;
  1480. /* Use hweight32 to count the number of ones in the add flags, or
  1481. * number of endpoints added. Don't count endpoints that are changed
  1482. * (both added and dropped).
  1483. */
  1484. return hweight32(valid_add_flags) -
  1485. hweight32(valid_add_flags & valid_drop_flags);
  1486. }
  1487. static unsigned int xhci_count_num_dropped_endpoints(struct xhci_hcd *xhci,
  1488. struct xhci_container_ctx *in_ctx)
  1489. {
  1490. struct xhci_input_control_ctx *ctrl_ctx;
  1491. u32 valid_add_flags;
  1492. u32 valid_drop_flags;
  1493. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  1494. valid_add_flags = ctrl_ctx->add_flags >> 2;
  1495. valid_drop_flags = ctrl_ctx->drop_flags >> 2;
  1496. return hweight32(valid_drop_flags) -
  1497. hweight32(valid_add_flags & valid_drop_flags);
  1498. }
  1499. /*
  1500. * We need to reserve the new number of endpoints before the configure endpoint
  1501. * command completes. We can't subtract the dropped endpoints from the number
  1502. * of active endpoints until the command completes because we can oversubscribe
  1503. * the host in this case:
  1504. *
  1505. * - the first configure endpoint command drops more endpoints than it adds
  1506. * - a second configure endpoint command that adds more endpoints is queued
  1507. * - the first configure endpoint command fails, so the config is unchanged
  1508. * - the second command may succeed, even though there isn't enough resources
  1509. *
  1510. * Must be called with xhci->lock held.
  1511. */
  1512. static int xhci_reserve_host_resources(struct xhci_hcd *xhci,
  1513. struct xhci_container_ctx *in_ctx)
  1514. {
  1515. u32 added_eps;
  1516. added_eps = xhci_count_num_new_endpoints(xhci, in_ctx);
  1517. if (xhci->num_active_eps + added_eps > xhci->limit_active_eps) {
  1518. xhci_dbg(xhci, "Not enough ep ctxs: "
  1519. "%u active, need to add %u, limit is %u.\n",
  1520. xhci->num_active_eps, added_eps,
  1521. xhci->limit_active_eps);
  1522. return -ENOMEM;
  1523. }
  1524. xhci->num_active_eps += added_eps;
  1525. xhci_dbg(xhci, "Adding %u ep ctxs, %u now active.\n", added_eps,
  1526. xhci->num_active_eps);
  1527. return 0;
  1528. }
  1529. /*
  1530. * The configure endpoint was failed by the xHC for some other reason, so we
  1531. * need to revert the resources that failed configuration would have used.
  1532. *
  1533. * Must be called with xhci->lock held.
  1534. */
  1535. static void xhci_free_host_resources(struct xhci_hcd *xhci,
  1536. struct xhci_container_ctx *in_ctx)
  1537. {
  1538. u32 num_failed_eps;
  1539. num_failed_eps = xhci_count_num_new_endpoints(xhci, in_ctx);
  1540. xhci->num_active_eps -= num_failed_eps;
  1541. xhci_dbg(xhci, "Removing %u failed ep ctxs, %u now active.\n",
  1542. num_failed_eps,
  1543. xhci->num_active_eps);
  1544. }
  1545. /*
  1546. * Now that the command has completed, clean up the active endpoint count by
  1547. * subtracting out the endpoints that were dropped (but not changed).
  1548. *
  1549. * Must be called with xhci->lock held.
  1550. */
  1551. static void xhci_finish_resource_reservation(struct xhci_hcd *xhci,
  1552. struct xhci_container_ctx *in_ctx)
  1553. {
  1554. u32 num_dropped_eps;
  1555. num_dropped_eps = xhci_count_num_dropped_endpoints(xhci, in_ctx);
  1556. xhci->num_active_eps -= num_dropped_eps;
  1557. if (num_dropped_eps)
  1558. xhci_dbg(xhci, "Removing %u dropped ep ctxs, %u now active.\n",
  1559. num_dropped_eps,
  1560. xhci->num_active_eps);
  1561. }
  1562. /* Issue a configure endpoint command or evaluate context command
  1563. * and wait for it to finish.
  1564. */
  1565. static int xhci_configure_endpoint(struct xhci_hcd *xhci,
  1566. struct usb_device *udev,
  1567. struct xhci_command *command,
  1568. bool ctx_change, bool must_succeed)
  1569. {
  1570. int ret;
  1571. int timeleft;
  1572. unsigned long flags;
  1573. struct xhci_container_ctx *in_ctx;
  1574. struct completion *cmd_completion;
  1575. u32 *cmd_status;
  1576. struct xhci_virt_device *virt_dev;
  1577. spin_lock_irqsave(&xhci->lock, flags);
  1578. virt_dev = xhci->devs[udev->slot_id];
  1579. if (command) {
  1580. in_ctx = command->in_ctx;
  1581. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK) &&
  1582. xhci_reserve_host_resources(xhci, in_ctx)) {
  1583. spin_unlock_irqrestore(&xhci->lock, flags);
  1584. xhci_warn(xhci, "Not enough host resources, "
  1585. "active endpoint contexts = %u\n",
  1586. xhci->num_active_eps);
  1587. return -ENOMEM;
  1588. }
  1589. cmd_completion = command->completion;
  1590. cmd_status = &command->status;
  1591. command->command_trb = xhci->cmd_ring->enqueue;
  1592. /* Enqueue pointer can be left pointing to the link TRB,
  1593. * we must handle that
  1594. */
  1595. if ((le32_to_cpu(command->command_trb->link.control)
  1596. & TRB_TYPE_BITMASK) == TRB_TYPE(TRB_LINK))
  1597. command->command_trb =
  1598. xhci->cmd_ring->enq_seg->next->trbs;
  1599. list_add_tail(&command->cmd_list, &virt_dev->cmd_list);
  1600. } else {
  1601. in_ctx = virt_dev->in_ctx;
  1602. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK) &&
  1603. xhci_reserve_host_resources(xhci, in_ctx)) {
  1604. spin_unlock_irqrestore(&xhci->lock, flags);
  1605. xhci_warn(xhci, "Not enough host resources, "
  1606. "active endpoint contexts = %u\n",
  1607. xhci->num_active_eps);
  1608. return -ENOMEM;
  1609. }
  1610. cmd_completion = &virt_dev->cmd_completion;
  1611. cmd_status = &virt_dev->cmd_status;
  1612. }
  1613. init_completion(cmd_completion);
  1614. if (!ctx_change)
  1615. ret = xhci_queue_configure_endpoint(xhci, in_ctx->dma,
  1616. udev->slot_id, must_succeed);
  1617. else
  1618. ret = xhci_queue_evaluate_context(xhci, in_ctx->dma,
  1619. udev->slot_id);
  1620. if (ret < 0) {
  1621. if (command)
  1622. list_del(&command->cmd_list);
  1623. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
  1624. xhci_free_host_resources(xhci, in_ctx);
  1625. spin_unlock_irqrestore(&xhci->lock, flags);
  1626. xhci_dbg(xhci, "FIXME allocate a new ring segment\n");
  1627. return -ENOMEM;
  1628. }
  1629. xhci_ring_cmd_db(xhci);
  1630. spin_unlock_irqrestore(&xhci->lock, flags);
  1631. /* Wait for the configure endpoint command to complete */
  1632. timeleft = wait_for_completion_interruptible_timeout(
  1633. cmd_completion,
  1634. USB_CTRL_SET_TIMEOUT);
  1635. if (timeleft <= 0) {
  1636. xhci_warn(xhci, "%s while waiting for %s command\n",
  1637. timeleft == 0 ? "Timeout" : "Signal",
  1638. ctx_change == 0 ?
  1639. "configure endpoint" :
  1640. "evaluate context");
  1641. /* FIXME cancel the configure endpoint command */
  1642. return -ETIME;
  1643. }
  1644. if (!ctx_change)
  1645. ret = xhci_configure_endpoint_result(xhci, udev, cmd_status);
  1646. else
  1647. ret = xhci_evaluate_context_result(xhci, udev, cmd_status);
  1648. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
  1649. spin_lock_irqsave(&xhci->lock, flags);
  1650. /* If the command failed, remove the reserved resources.
  1651. * Otherwise, clean up the estimate to include dropped eps.
  1652. */
  1653. if (ret)
  1654. xhci_free_host_resources(xhci, in_ctx);
  1655. else
  1656. xhci_finish_resource_reservation(xhci, in_ctx);
  1657. spin_unlock_irqrestore(&xhci->lock, flags);
  1658. }
  1659. return ret;
  1660. }
  1661. /* Called after one or more calls to xhci_add_endpoint() or
  1662. * xhci_drop_endpoint(). If this call fails, the USB core is expected
  1663. * to call xhci_reset_bandwidth().
  1664. *
  1665. * Since we are in the middle of changing either configuration or
  1666. * installing a new alt setting, the USB core won't allow URBs to be
  1667. * enqueued for any endpoint on the old config or interface. Nothing
  1668. * else should be touching the xhci->devs[slot_id] structure, so we
  1669. * don't need to take the xhci->lock for manipulating that.
  1670. */
  1671. int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
  1672. {
  1673. int i;
  1674. int ret = 0;
  1675. struct xhci_hcd *xhci;
  1676. struct xhci_virt_device *virt_dev;
  1677. struct xhci_input_control_ctx *ctrl_ctx;
  1678. struct xhci_slot_ctx *slot_ctx;
  1679. ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
  1680. if (ret <= 0)
  1681. return ret;
  1682. xhci = hcd_to_xhci(hcd);
  1683. if (xhci->xhc_state & XHCI_STATE_DYING)
  1684. return -ENODEV;
  1685. xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
  1686. virt_dev = xhci->devs[udev->slot_id];
  1687. /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
  1688. ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
  1689. ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
  1690. ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG);
  1691. ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG));
  1692. /* Don't issue the command if there's no endpoints to update. */
  1693. if (ctrl_ctx->add_flags == cpu_to_le32(SLOT_FLAG) &&
  1694. ctrl_ctx->drop_flags == 0)
  1695. return 0;
  1696. xhci_dbg(xhci, "New Input Control Context:\n");
  1697. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
  1698. xhci_dbg_ctx(xhci, virt_dev->in_ctx,
  1699. LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx->dev_info)));
  1700. ret = xhci_configure_endpoint(xhci, udev, NULL,
  1701. false, false);
  1702. if (ret) {
  1703. /* Callee should call reset_bandwidth() */
  1704. return ret;
  1705. }
  1706. xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
  1707. xhci_dbg_ctx(xhci, virt_dev->out_ctx,
  1708. LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx->dev_info)));
  1709. /* Free any rings that were dropped, but not changed. */
  1710. for (i = 1; i < 31; ++i) {
  1711. if ((le32_to_cpu(ctrl_ctx->drop_flags) & (1 << (i + 1))) &&
  1712. !(le32_to_cpu(ctrl_ctx->add_flags) & (1 << (i + 1))))
  1713. xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
  1714. }
  1715. xhci_zero_in_ctx(xhci, virt_dev);
  1716. /*
  1717. * Install any rings for completely new endpoints or changed endpoints,
  1718. * and free or cache any old rings from changed endpoints.
  1719. */
  1720. for (i = 1; i < 31; ++i) {
  1721. if (!virt_dev->eps[i].new_ring)
  1722. continue;
  1723. /* Only cache or free the old ring if it exists.
  1724. * It may not if this is the first add of an endpoint.
  1725. */
  1726. if (virt_dev->eps[i].ring) {
  1727. xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
  1728. }
  1729. virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
  1730. virt_dev->eps[i].new_ring = NULL;
  1731. }
  1732. return ret;
  1733. }
  1734. void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
  1735. {
  1736. struct xhci_hcd *xhci;
  1737. struct xhci_virt_device *virt_dev;
  1738. int i, ret;
  1739. ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
  1740. if (ret <= 0)
  1741. return;
  1742. xhci = hcd_to_xhci(hcd);
  1743. xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
  1744. virt_dev = xhci->devs[udev->slot_id];
  1745. /* Free any rings allocated for added endpoints */
  1746. for (i = 0; i < 31; ++i) {
  1747. if (virt_dev->eps[i].new_ring) {
  1748. xhci_ring_free(xhci, virt_dev->eps[i].new_ring);
  1749. virt_dev->eps[i].new_ring = NULL;
  1750. }
  1751. }
  1752. xhci_zero_in_ctx(xhci, virt_dev);
  1753. }
  1754. static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci,
  1755. struct xhci_container_ctx *in_ctx,
  1756. struct xhci_container_ctx *out_ctx,
  1757. u32 add_flags, u32 drop_flags)
  1758. {
  1759. struct xhci_input_control_ctx *ctrl_ctx;
  1760. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  1761. ctrl_ctx->add_flags = cpu_to_le32(add_flags);
  1762. ctrl_ctx->drop_flags = cpu_to_le32(drop_flags);
  1763. xhci_slot_copy(xhci, in_ctx, out_ctx);
  1764. ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
  1765. xhci_dbg(xhci, "Input Context:\n");
  1766. xhci_dbg_ctx(xhci, in_ctx, xhci_last_valid_endpoint(add_flags));
  1767. }
  1768. static void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci,
  1769. unsigned int slot_id, unsigned int ep_index,
  1770. struct xhci_dequeue_state *deq_state)
  1771. {
  1772. struct xhci_container_ctx *in_ctx;
  1773. struct xhci_ep_ctx *ep_ctx;
  1774. u32 added_ctxs;
  1775. dma_addr_t addr;
  1776. xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
  1777. xhci->devs[slot_id]->out_ctx, ep_index);
  1778. in_ctx = xhci->devs[slot_id]->in_ctx;
  1779. ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
  1780. addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
  1781. deq_state->new_deq_ptr);
  1782. if (addr == 0) {
  1783. xhci_warn(xhci, "WARN Cannot submit config ep after "
  1784. "reset ep command\n");
  1785. xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n",
  1786. deq_state->new_deq_seg,
  1787. deq_state->new_deq_ptr);
  1788. return;
  1789. }
  1790. ep_ctx->deq = cpu_to_le64(addr | deq_state->new_cycle_state);
  1791. added_ctxs = xhci_get_endpoint_flag_from_index(ep_index);
  1792. xhci_setup_input_ctx_for_config_ep(xhci, xhci->devs[slot_id]->in_ctx,
  1793. xhci->devs[slot_id]->out_ctx, added_ctxs, added_ctxs);
  1794. }
  1795. void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
  1796. struct usb_device *udev, unsigned int ep_index)
  1797. {
  1798. struct xhci_dequeue_state deq_state;
  1799. struct xhci_virt_ep *ep;
  1800. xhci_dbg(xhci, "Cleaning up stalled endpoint ring\n");
  1801. ep = &xhci->devs[udev->slot_id]->eps[ep_index];
  1802. /* We need to move the HW's dequeue pointer past this TD,
  1803. * or it will attempt to resend it on the next doorbell ring.
  1804. */
  1805. xhci_find_new_dequeue_state(xhci, udev->slot_id,
  1806. ep_index, ep->stopped_stream, ep->stopped_td,
  1807. &deq_state);
  1808. /* HW with the reset endpoint quirk will use the saved dequeue state to
  1809. * issue a configure endpoint command later.
  1810. */
  1811. if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) {
  1812. xhci_dbg(xhci, "Queueing new dequeue state\n");
  1813. xhci_queue_new_dequeue_state(xhci, udev->slot_id,
  1814. ep_index, ep->stopped_stream, &deq_state);
  1815. } else {
  1816. /* Better hope no one uses the input context between now and the
  1817. * reset endpoint completion!
  1818. * XXX: No idea how this hardware will react when stream rings
  1819. * are enabled.
  1820. */
  1821. xhci_dbg(xhci, "Setting up input context for "
  1822. "configure endpoint command\n");
  1823. xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id,
  1824. ep_index, &deq_state);
  1825. }
  1826. }
  1827. /* Deal with stalled endpoints. The core should have sent the control message
  1828. * to clear the halt condition. However, we need to make the xHCI hardware
  1829. * reset its sequence number, since a device will expect a sequence number of
  1830. * zero after the halt condition is cleared.
  1831. * Context: in_interrupt
  1832. */
  1833. void xhci_endpoint_reset(struct usb_hcd *hcd,
  1834. struct usb_host_endpoint *ep)
  1835. {
  1836. struct xhci_hcd *xhci;
  1837. struct usb_device *udev;
  1838. unsigned int ep_index;
  1839. unsigned long flags;
  1840. int ret;
  1841. struct xhci_virt_ep *virt_ep;
  1842. xhci = hcd_to_xhci(hcd);
  1843. udev = (struct usb_device *) ep->hcpriv;
  1844. /* Called with a root hub endpoint (or an endpoint that wasn't added
  1845. * with xhci_add_endpoint()
  1846. */
  1847. if (!ep->hcpriv)
  1848. return;
  1849. ep_index = xhci_get_endpoint_index(&ep->desc);
  1850. virt_ep = &xhci->devs[udev->slot_id]->eps[ep_index];
  1851. if (!virt_ep->stopped_td) {
  1852. xhci_dbg(xhci, "Endpoint 0x%x not halted, refusing to reset.\n",
  1853. ep->desc.bEndpointAddress);
  1854. return;
  1855. }
  1856. if (usb_endpoint_xfer_control(&ep->desc)) {
  1857. xhci_dbg(xhci, "Control endpoint stall already handled.\n");
  1858. return;
  1859. }
  1860. xhci_dbg(xhci, "Queueing reset endpoint command\n");
  1861. spin_lock_irqsave(&xhci->lock, flags);
  1862. ret = xhci_queue_reset_ep(xhci, udev->slot_id, ep_index);
  1863. /*
  1864. * Can't change the ring dequeue pointer until it's transitioned to the
  1865. * stopped state, which is only upon a successful reset endpoint
  1866. * command. Better hope that last command worked!
  1867. */
  1868. if (!ret) {
  1869. xhci_cleanup_stalled_ring(xhci, udev, ep_index);
  1870. kfree(virt_ep->stopped_td);
  1871. xhci_ring_cmd_db(xhci);
  1872. }
  1873. virt_ep->stopped_td = NULL;
  1874. virt_ep->stopped_trb = NULL;
  1875. virt_ep->stopped_stream = 0;
  1876. spin_unlock_irqrestore(&xhci->lock, flags);
  1877. if (ret)
  1878. xhci_warn(xhci, "FIXME allocate a new ring segment\n");
  1879. }
  1880. static int xhci_check_streams_endpoint(struct xhci_hcd *xhci,
  1881. struct usb_device *udev, struct usb_host_endpoint *ep,
  1882. unsigned int slot_id)
  1883. {
  1884. int ret;
  1885. unsigned int ep_index;
  1886. unsigned int ep_state;
  1887. if (!ep)
  1888. return -EINVAL;
  1889. ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, true, __func__);
  1890. if (ret <= 0)
  1891. return -EINVAL;
  1892. if (ep->ss_ep_comp.bmAttributes == 0) {
  1893. xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion"
  1894. " descriptor for ep 0x%x does not support streams\n",
  1895. ep->desc.bEndpointAddress);
  1896. return -EINVAL;
  1897. }
  1898. ep_index = xhci_get_endpoint_index(&ep->desc);
  1899. ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
  1900. if (ep_state & EP_HAS_STREAMS ||
  1901. ep_state & EP_GETTING_STREAMS) {
  1902. xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x "
  1903. "already has streams set up.\n",
  1904. ep->desc.bEndpointAddress);
  1905. xhci_warn(xhci, "Send email to xHCI maintainer and ask for "
  1906. "dynamic stream context array reallocation.\n");
  1907. return -EINVAL;
  1908. }
  1909. if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) {
  1910. xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk "
  1911. "endpoint 0x%x; URBs are pending.\n",
  1912. ep->desc.bEndpointAddress);
  1913. return -EINVAL;
  1914. }
  1915. return 0;
  1916. }
  1917. static void xhci_calculate_streams_entries(struct xhci_hcd *xhci,
  1918. unsigned int *num_streams, unsigned int *num_stream_ctxs)
  1919. {
  1920. unsigned int max_streams;
  1921. /* The stream context array size must be a power of two */
  1922. *num_stream_ctxs = roundup_pow_of_two(*num_streams);
  1923. /*
  1924. * Find out how many primary stream array entries the host controller
  1925. * supports. Later we may use secondary stream arrays (similar to 2nd
  1926. * level page entries), but that's an optional feature for xHCI host
  1927. * controllers. xHCs must support at least 4 stream IDs.
  1928. */
  1929. max_streams = HCC_MAX_PSA(xhci->hcc_params);
  1930. if (*num_stream_ctxs > max_streams) {
  1931. xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n",
  1932. max_streams);
  1933. *num_stream_ctxs = max_streams;
  1934. *num_streams = max_streams;
  1935. }
  1936. }
  1937. /* Returns an error code if one of the endpoint already has streams.
  1938. * This does not change any data structures, it only checks and gathers
  1939. * information.
  1940. */
  1941. static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci,
  1942. struct usb_device *udev,
  1943. struct usb_host_endpoint **eps, unsigned int num_eps,
  1944. unsigned int *num_streams, u32 *changed_ep_bitmask)
  1945. {
  1946. unsigned int max_streams;
  1947. unsigned int endpoint_flag;
  1948. int i;
  1949. int ret;
  1950. for (i = 0; i < num_eps; i++) {
  1951. ret = xhci_check_streams_endpoint(xhci, udev,
  1952. eps[i], udev->slot_id);
  1953. if (ret < 0)
  1954. return ret;
  1955. max_streams = usb_ss_max_streams(&eps[i]->ss_ep_comp);
  1956. if (max_streams < (*num_streams - 1)) {
  1957. xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n",
  1958. eps[i]->desc.bEndpointAddress,
  1959. max_streams);
  1960. *num_streams = max_streams+1;
  1961. }
  1962. endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc);
  1963. if (*changed_ep_bitmask & endpoint_flag)
  1964. return -EINVAL;
  1965. *changed_ep_bitmask |= endpoint_flag;
  1966. }
  1967. return 0;
  1968. }
  1969. static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci,
  1970. struct usb_device *udev,
  1971. struct usb_host_endpoint **eps, unsigned int num_eps)
  1972. {
  1973. u32 changed_ep_bitmask = 0;
  1974. unsigned int slot_id;
  1975. unsigned int ep_index;
  1976. unsigned int ep_state;
  1977. int i;
  1978. slot_id = udev->slot_id;
  1979. if (!xhci->devs[slot_id])
  1980. return 0;
  1981. for (i = 0; i < num_eps; i++) {
  1982. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  1983. ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
  1984. /* Are streams already being freed for the endpoint? */
  1985. if (ep_state & EP_GETTING_NO_STREAMS) {
  1986. xhci_warn(xhci, "WARN Can't disable streams for "
  1987. "endpoint 0x%x\n, "
  1988. "streams are being disabled already.",
  1989. eps[i]->desc.bEndpointAddress);
  1990. return 0;
  1991. }
  1992. /* Are there actually any streams to free? */
  1993. if (!(ep_state & EP_HAS_STREAMS) &&
  1994. !(ep_state & EP_GETTING_STREAMS)) {
  1995. xhci_warn(xhci, "WARN Can't disable streams for "
  1996. "endpoint 0x%x\n, "
  1997. "streams are already disabled!",
  1998. eps[i]->desc.bEndpointAddress);
  1999. xhci_warn(xhci, "WARN xhci_free_streams() called "
  2000. "with non-streams endpoint\n");
  2001. return 0;
  2002. }
  2003. changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc);
  2004. }
  2005. return changed_ep_bitmask;
  2006. }
  2007. /*
  2008. * The USB device drivers use this function (though the HCD interface in USB
  2009. * core) to prepare a set of bulk endpoints to use streams. Streams are used to
  2010. * coordinate mass storage command queueing across multiple endpoints (basically
  2011. * a stream ID == a task ID).
  2012. *
  2013. * Setting up streams involves allocating the same size stream context array
  2014. * for each endpoint and issuing a configure endpoint command for all endpoints.
  2015. *
  2016. * Don't allow the call to succeed if one endpoint only supports one stream
  2017. * (which means it doesn't support streams at all).
  2018. *
  2019. * Drivers may get less stream IDs than they asked for, if the host controller
  2020. * hardware or endpoints claim they can't support the number of requested
  2021. * stream IDs.
  2022. */
  2023. int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
  2024. struct usb_host_endpoint **eps, unsigned int num_eps,
  2025. unsigned int num_streams, gfp_t mem_flags)
  2026. {
  2027. int i, ret;
  2028. struct xhci_hcd *xhci;
  2029. struct xhci_virt_device *vdev;
  2030. struct xhci_command *config_cmd;
  2031. unsigned int ep_index;
  2032. unsigned int num_stream_ctxs;
  2033. unsigned long flags;
  2034. u32 changed_ep_bitmask = 0;
  2035. if (!eps)
  2036. return -EINVAL;
  2037. /* Add one to the number of streams requested to account for
  2038. * stream 0 that is reserved for xHCI usage.
  2039. */
  2040. num_streams += 1;
  2041. xhci = hcd_to_xhci(hcd);
  2042. xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n",
  2043. num_streams);
  2044. config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
  2045. if (!config_cmd) {
  2046. xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
  2047. return -ENOMEM;
  2048. }
  2049. /* Check to make sure all endpoints are not already configured for
  2050. * streams. While we're at it, find the maximum number of streams that
  2051. * all the endpoints will support and check for duplicate endpoints.
  2052. */
  2053. spin_lock_irqsave(&xhci->lock, flags);
  2054. ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps,
  2055. num_eps, &num_streams, &changed_ep_bitmask);
  2056. if (ret < 0) {
  2057. xhci_free_command(xhci, config_cmd);
  2058. spin_unlock_irqrestore(&xhci->lock, flags);
  2059. return ret;
  2060. }
  2061. if (num_streams <= 1) {
  2062. xhci_warn(xhci, "WARN: endpoints can't handle "
  2063. "more than one stream.\n");
  2064. xhci_free_command(xhci, config_cmd);
  2065. spin_unlock_irqrestore(&xhci->lock, flags);
  2066. return -EINVAL;
  2067. }
  2068. vdev = xhci->devs[udev->slot_id];
  2069. /* Mark each endpoint as being in transition, so
  2070. * xhci_urb_enqueue() will reject all URBs.
  2071. */
  2072. for (i = 0; i < num_eps; i++) {
  2073. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2074. vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS;
  2075. }
  2076. spin_unlock_irqrestore(&xhci->lock, flags);
  2077. /* Setup internal data structures and allocate HW data structures for
  2078. * streams (but don't install the HW structures in the input context
  2079. * until we're sure all memory allocation succeeded).
  2080. */
  2081. xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs);
  2082. xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n",
  2083. num_stream_ctxs, num_streams);
  2084. for (i = 0; i < num_eps; i++) {
  2085. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2086. vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci,
  2087. num_stream_ctxs,
  2088. num_streams, mem_flags);
  2089. if (!vdev->eps[ep_index].stream_info)
  2090. goto cleanup;
  2091. /* Set maxPstreams in endpoint context and update deq ptr to
  2092. * point to stream context array. FIXME
  2093. */
  2094. }
  2095. /* Set up the input context for a configure endpoint command. */
  2096. for (i = 0; i < num_eps; i++) {
  2097. struct xhci_ep_ctx *ep_ctx;
  2098. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2099. ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index);
  2100. xhci_endpoint_copy(xhci, config_cmd->in_ctx,
  2101. vdev->out_ctx, ep_index);
  2102. xhci_setup_streams_ep_input_ctx(xhci, ep_ctx,
  2103. vdev->eps[ep_index].stream_info);
  2104. }
  2105. /* Tell the HW to drop its old copy of the endpoint context info
  2106. * and add the updated copy from the input context.
  2107. */
  2108. xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx,
  2109. vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
  2110. /* Issue and wait for the configure endpoint command */
  2111. ret = xhci_configure_endpoint(xhci, udev, config_cmd,
  2112. false, false);
  2113. /* xHC rejected the configure endpoint command for some reason, so we
  2114. * leave the old ring intact and free our internal streams data
  2115. * structure.
  2116. */
  2117. if (ret < 0)
  2118. goto cleanup;
  2119. spin_lock_irqsave(&xhci->lock, flags);
  2120. for (i = 0; i < num_eps; i++) {
  2121. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2122. vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
  2123. xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n",
  2124. udev->slot_id, ep_index);
  2125. vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS;
  2126. }
  2127. xhci_free_command(xhci, config_cmd);
  2128. spin_unlock_irqrestore(&xhci->lock, flags);
  2129. /* Subtract 1 for stream 0, which drivers can't use */
  2130. return num_streams - 1;
  2131. cleanup:
  2132. /* If it didn't work, free the streams! */
  2133. for (i = 0; i < num_eps; i++) {
  2134. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2135. xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
  2136. vdev->eps[ep_index].stream_info = NULL;
  2137. /* FIXME Unset maxPstreams in endpoint context and
  2138. * update deq ptr to point to normal string ring.
  2139. */
  2140. vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
  2141. vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
  2142. xhci_endpoint_zero(xhci, vdev, eps[i]);
  2143. }
  2144. xhci_free_command(xhci, config_cmd);
  2145. return -ENOMEM;
  2146. }
  2147. /* Transition the endpoint from using streams to being a "normal" endpoint
  2148. * without streams.
  2149. *
  2150. * Modify the endpoint context state, submit a configure endpoint command,
  2151. * and free all endpoint rings for streams if that completes successfully.
  2152. */
  2153. int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
  2154. struct usb_host_endpoint **eps, unsigned int num_eps,
  2155. gfp_t mem_flags)
  2156. {
  2157. int i, ret;
  2158. struct xhci_hcd *xhci;
  2159. struct xhci_virt_device *vdev;
  2160. struct xhci_command *command;
  2161. unsigned int ep_index;
  2162. unsigned long flags;
  2163. u32 changed_ep_bitmask;
  2164. xhci = hcd_to_xhci(hcd);
  2165. vdev = xhci->devs[udev->slot_id];
  2166. /* Set up a configure endpoint command to remove the streams rings */
  2167. spin_lock_irqsave(&xhci->lock, flags);
  2168. changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci,
  2169. udev, eps, num_eps);
  2170. if (changed_ep_bitmask == 0) {
  2171. spin_unlock_irqrestore(&xhci->lock, flags);
  2172. return -EINVAL;
  2173. }
  2174. /* Use the xhci_command structure from the first endpoint. We may have
  2175. * allocated too many, but the driver may call xhci_free_streams() for
  2176. * each endpoint it grouped into one call to xhci_alloc_streams().
  2177. */
  2178. ep_index = xhci_get_endpoint_index(&eps[0]->desc);
  2179. command = vdev->eps[ep_index].stream_info->free_streams_command;
  2180. for (i = 0; i < num_eps; i++) {
  2181. struct xhci_ep_ctx *ep_ctx;
  2182. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2183. ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
  2184. xhci->devs[udev->slot_id]->eps[ep_index].ep_state |=
  2185. EP_GETTING_NO_STREAMS;
  2186. xhci_endpoint_copy(xhci, command->in_ctx,
  2187. vdev->out_ctx, ep_index);
  2188. xhci_setup_no_streams_ep_input_ctx(xhci, ep_ctx,
  2189. &vdev->eps[ep_index]);
  2190. }
  2191. xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx,
  2192. vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
  2193. spin_unlock_irqrestore(&xhci->lock, flags);
  2194. /* Issue and wait for the configure endpoint command,
  2195. * which must succeed.
  2196. */
  2197. ret = xhci_configure_endpoint(xhci, udev, command,
  2198. false, true);
  2199. /* xHC rejected the configure endpoint command for some reason, so we
  2200. * leave the streams rings intact.
  2201. */
  2202. if (ret < 0)
  2203. return ret;
  2204. spin_lock_irqsave(&xhci->lock, flags);
  2205. for (i = 0; i < num_eps; i++) {
  2206. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2207. xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
  2208. vdev->eps[ep_index].stream_info = NULL;
  2209. /* FIXME Unset maxPstreams in endpoint context and
  2210. * update deq ptr to point to normal string ring.
  2211. */
  2212. vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS;
  2213. vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
  2214. }
  2215. spin_unlock_irqrestore(&xhci->lock, flags);
  2216. return 0;
  2217. }
  2218. /*
  2219. * Deletes endpoint resources for endpoints that were active before a Reset
  2220. * Device command, or a Disable Slot command. The Reset Device command leaves
  2221. * the control endpoint intact, whereas the Disable Slot command deletes it.
  2222. *
  2223. * Must be called with xhci->lock held.
  2224. */
  2225. void xhci_free_device_endpoint_resources(struct xhci_hcd *xhci,
  2226. struct xhci_virt_device *virt_dev, bool drop_control_ep)
  2227. {
  2228. int i;
  2229. unsigned int num_dropped_eps = 0;
  2230. unsigned int drop_flags = 0;
  2231. for (i = (drop_control_ep ? 0 : 1); i < 31; i++) {
  2232. if (virt_dev->eps[i].ring) {
  2233. drop_flags |= 1 << i;
  2234. num_dropped_eps++;
  2235. }
  2236. }
  2237. xhci->num_active_eps -= num_dropped_eps;
  2238. if (num_dropped_eps)
  2239. xhci_dbg(xhci, "Dropped %u ep ctxs, flags = 0x%x, "
  2240. "%u now active.\n",
  2241. num_dropped_eps, drop_flags,
  2242. xhci->num_active_eps);
  2243. }
  2244. /*
  2245. * This submits a Reset Device Command, which will set the device state to 0,
  2246. * set the device address to 0, and disable all the endpoints except the default
  2247. * control endpoint. The USB core should come back and call
  2248. * xhci_address_device(), and then re-set up the configuration. If this is
  2249. * called because of a usb_reset_and_verify_device(), then the old alternate
  2250. * settings will be re-installed through the normal bandwidth allocation
  2251. * functions.
  2252. *
  2253. * Wait for the Reset Device command to finish. Remove all structures
  2254. * associated with the endpoints that were disabled. Clear the input device
  2255. * structure? Cache the rings? Reset the control endpoint 0 max packet size?
  2256. *
  2257. * If the virt_dev to be reset does not exist or does not match the udev,
  2258. * it means the device is lost, possibly due to the xHC restore error and
  2259. * re-initialization during S3/S4. In this case, call xhci_alloc_dev() to
  2260. * re-allocate the device.
  2261. */
  2262. int xhci_discover_or_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
  2263. {
  2264. int ret, i;
  2265. unsigned long flags;
  2266. struct xhci_hcd *xhci;
  2267. unsigned int slot_id;
  2268. struct xhci_virt_device *virt_dev;
  2269. struct xhci_command *reset_device_cmd;
  2270. int timeleft;
  2271. int last_freed_endpoint;
  2272. struct xhci_slot_ctx *slot_ctx;
  2273. ret = xhci_check_args(hcd, udev, NULL, 0, false, __func__);
  2274. if (ret <= 0)
  2275. return ret;
  2276. xhci = hcd_to_xhci(hcd);
  2277. slot_id = udev->slot_id;
  2278. virt_dev = xhci->devs[slot_id];
  2279. if (!virt_dev) {
  2280. xhci_dbg(xhci, "The device to be reset with slot ID %u does "
  2281. "not exist. Re-allocate the device\n", slot_id);
  2282. ret = xhci_alloc_dev(hcd, udev);
  2283. if (ret == 1)
  2284. return 0;
  2285. else
  2286. return -EINVAL;
  2287. }
  2288. if (virt_dev->udev != udev) {
  2289. /* If the virt_dev and the udev does not match, this virt_dev
  2290. * may belong to another udev.
  2291. * Re-allocate the device.
  2292. */
  2293. xhci_dbg(xhci, "The device to be reset with slot ID %u does "
  2294. "not match the udev. Re-allocate the device\n",
  2295. slot_id);
  2296. ret = xhci_alloc_dev(hcd, udev);
  2297. if (ret == 1)
  2298. return 0;
  2299. else
  2300. return -EINVAL;
  2301. }
  2302. /* If device is not setup, there is no point in resetting it */
  2303. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
  2304. if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) ==
  2305. SLOT_STATE_DISABLED)
  2306. return 0;
  2307. xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id);
  2308. /* Allocate the command structure that holds the struct completion.
  2309. * Assume we're in process context, since the normal device reset
  2310. * process has to wait for the device anyway. Storage devices are
  2311. * reset as part of error handling, so use GFP_NOIO instead of
  2312. * GFP_KERNEL.
  2313. */
  2314. reset_device_cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
  2315. if (!reset_device_cmd) {
  2316. xhci_dbg(xhci, "Couldn't allocate command structure.\n");
  2317. return -ENOMEM;
  2318. }
  2319. /* Attempt to submit the Reset Device command to the command ring */
  2320. spin_lock_irqsave(&xhci->lock, flags);
  2321. reset_device_cmd->command_trb = xhci->cmd_ring->enqueue;
  2322. /* Enqueue pointer can be left pointing to the link TRB,
  2323. * we must handle that
  2324. */
  2325. if ((le32_to_cpu(reset_device_cmd->command_trb->link.control)
  2326. & TRB_TYPE_BITMASK) == TRB_TYPE(TRB_LINK))
  2327. reset_device_cmd->command_trb =
  2328. xhci->cmd_ring->enq_seg->next->trbs;
  2329. list_add_tail(&reset_device_cmd->cmd_list, &virt_dev->cmd_list);
  2330. ret = xhci_queue_reset_device(xhci, slot_id);
  2331. if (ret) {
  2332. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  2333. list_del(&reset_device_cmd->cmd_list);
  2334. spin_unlock_irqrestore(&xhci->lock, flags);
  2335. goto command_cleanup;
  2336. }
  2337. xhci_ring_cmd_db(xhci);
  2338. spin_unlock_irqrestore(&xhci->lock, flags);
  2339. /* Wait for the Reset Device command to finish */
  2340. timeleft = wait_for_completion_interruptible_timeout(
  2341. reset_device_cmd->completion,
  2342. USB_CTRL_SET_TIMEOUT);
  2343. if (timeleft <= 0) {
  2344. xhci_warn(xhci, "%s while waiting for reset device command\n",
  2345. timeleft == 0 ? "Timeout" : "Signal");
  2346. spin_lock_irqsave(&xhci->lock, flags);
  2347. /* The timeout might have raced with the event ring handler, so
  2348. * only delete from the list if the item isn't poisoned.
  2349. */
  2350. if (reset_device_cmd->cmd_list.next != LIST_POISON1)
  2351. list_del(&reset_device_cmd->cmd_list);
  2352. spin_unlock_irqrestore(&xhci->lock, flags);
  2353. ret = -ETIME;
  2354. goto command_cleanup;
  2355. }
  2356. /* The Reset Device command can't fail, according to the 0.95/0.96 spec,
  2357. * unless we tried to reset a slot ID that wasn't enabled,
  2358. * or the device wasn't in the addressed or configured state.
  2359. */
  2360. ret = reset_device_cmd->status;
  2361. switch (ret) {
  2362. case COMP_EBADSLT: /* 0.95 completion code for bad slot ID */
  2363. case COMP_CTX_STATE: /* 0.96 completion code for same thing */
  2364. xhci_info(xhci, "Can't reset device (slot ID %u) in %s state\n",
  2365. slot_id,
  2366. xhci_get_slot_state(xhci, virt_dev->out_ctx));
  2367. xhci_info(xhci, "Not freeing device rings.\n");
  2368. /* Don't treat this as an error. May change my mind later. */
  2369. ret = 0;
  2370. goto command_cleanup;
  2371. case COMP_SUCCESS:
  2372. xhci_dbg(xhci, "Successful reset device command.\n");
  2373. break;
  2374. default:
  2375. if (xhci_is_vendor_info_code(xhci, ret))
  2376. break;
  2377. xhci_warn(xhci, "Unknown completion code %u for "
  2378. "reset device command.\n", ret);
  2379. ret = -EINVAL;
  2380. goto command_cleanup;
  2381. }
  2382. /* Free up host controller endpoint resources */
  2383. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
  2384. spin_lock_irqsave(&xhci->lock, flags);
  2385. /* Don't delete the default control endpoint resources */
  2386. xhci_free_device_endpoint_resources(xhci, virt_dev, false);
  2387. spin_unlock_irqrestore(&xhci->lock, flags);
  2388. }
  2389. /* Everything but endpoint 0 is disabled, so free or cache the rings. */
  2390. last_freed_endpoint = 1;
  2391. for (i = 1; i < 31; ++i) {
  2392. struct xhci_virt_ep *ep = &virt_dev->eps[i];
  2393. if (ep->ep_state & EP_HAS_STREAMS) {
  2394. xhci_free_stream_info(xhci, ep->stream_info);
  2395. ep->stream_info = NULL;
  2396. ep->ep_state &= ~EP_HAS_STREAMS;
  2397. }
  2398. if (ep->ring) {
  2399. xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
  2400. last_freed_endpoint = i;
  2401. }
  2402. }
  2403. xhci_dbg(xhci, "Output context after successful reset device cmd:\n");
  2404. xhci_dbg_ctx(xhci, virt_dev->out_ctx, last_freed_endpoint);
  2405. ret = 0;
  2406. command_cleanup:
  2407. xhci_free_command(xhci, reset_device_cmd);
  2408. return ret;
  2409. }
  2410. /*
  2411. * At this point, the struct usb_device is about to go away, the device has
  2412. * disconnected, and all traffic has been stopped and the endpoints have been
  2413. * disabled. Free any HC data structures associated with that device.
  2414. */
  2415. void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
  2416. {
  2417. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  2418. struct xhci_virt_device *virt_dev;
  2419. unsigned long flags;
  2420. u32 state;
  2421. int i, ret;
  2422. ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
  2423. /* If the host is halted due to driver unload, we still need to free the
  2424. * device.
  2425. */
  2426. if (ret <= 0 && ret != -ENODEV)
  2427. return;
  2428. virt_dev = xhci->devs[udev->slot_id];
  2429. /* Stop any wayward timer functions (which may grab the lock) */
  2430. for (i = 0; i < 31; ++i) {
  2431. virt_dev->eps[i].ep_state &= ~EP_HALT_PENDING;
  2432. del_timer_sync(&virt_dev->eps[i].stop_cmd_timer);
  2433. }
  2434. spin_lock_irqsave(&xhci->lock, flags);
  2435. /* Don't disable the slot if the host controller is dead. */
  2436. state = xhci_readl(xhci, &xhci->op_regs->status);
  2437. if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) ||
  2438. (xhci->xhc_state & XHCI_STATE_HALTED)) {
  2439. xhci_free_virt_device(xhci, udev->slot_id);
  2440. spin_unlock_irqrestore(&xhci->lock, flags);
  2441. return;
  2442. }
  2443. if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
  2444. spin_unlock_irqrestore(&xhci->lock, flags);
  2445. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  2446. return;
  2447. }
  2448. xhci_ring_cmd_db(xhci);
  2449. spin_unlock_irqrestore(&xhci->lock, flags);
  2450. /*
  2451. * Event command completion handler will free any data structures
  2452. * associated with the slot. XXX Can free sleep?
  2453. */
  2454. }
  2455. /*
  2456. * Checks if we have enough host controller resources for the default control
  2457. * endpoint.
  2458. *
  2459. * Must be called with xhci->lock held.
  2460. */
  2461. static int xhci_reserve_host_control_ep_resources(struct xhci_hcd *xhci)
  2462. {
  2463. if (xhci->num_active_eps + 1 > xhci->limit_active_eps) {
  2464. xhci_dbg(xhci, "Not enough ep ctxs: "
  2465. "%u active, need to add 1, limit is %u.\n",
  2466. xhci->num_active_eps, xhci->limit_active_eps);
  2467. return -ENOMEM;
  2468. }
  2469. xhci->num_active_eps += 1;
  2470. xhci_dbg(xhci, "Adding 1 ep ctx, %u now active.\n",
  2471. xhci->num_active_eps);
  2472. return 0;
  2473. }
  2474. /*
  2475. * Returns 0 if the xHC ran out of device slots, the Enable Slot command
  2476. * timed out, or allocating memory failed. Returns 1 on success.
  2477. */
  2478. int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
  2479. {
  2480. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  2481. unsigned long flags;
  2482. int timeleft;
  2483. int ret;
  2484. spin_lock_irqsave(&xhci->lock, flags);
  2485. ret = xhci_queue_slot_control(xhci, TRB_ENABLE_SLOT, 0);
  2486. if (ret) {
  2487. spin_unlock_irqrestore(&xhci->lock, flags);
  2488. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  2489. return 0;
  2490. }
  2491. xhci_ring_cmd_db(xhci);
  2492. spin_unlock_irqrestore(&xhci->lock, flags);
  2493. /* XXX: how much time for xHC slot assignment? */
  2494. timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
  2495. USB_CTRL_SET_TIMEOUT);
  2496. if (timeleft <= 0) {
  2497. xhci_warn(xhci, "%s while waiting for a slot\n",
  2498. timeleft == 0 ? "Timeout" : "Signal");
  2499. /* FIXME cancel the enable slot request */
  2500. return 0;
  2501. }
  2502. if (!xhci->slot_id) {
  2503. xhci_err(xhci, "Error while assigning device slot ID\n");
  2504. return 0;
  2505. }
  2506. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
  2507. spin_lock_irqsave(&xhci->lock, flags);
  2508. ret = xhci_reserve_host_control_ep_resources(xhci);
  2509. if (ret) {
  2510. spin_unlock_irqrestore(&xhci->lock, flags);
  2511. xhci_warn(xhci, "Not enough host resources, "
  2512. "active endpoint contexts = %u\n",
  2513. xhci->num_active_eps);
  2514. goto disable_slot;
  2515. }
  2516. spin_unlock_irqrestore(&xhci->lock, flags);
  2517. }
  2518. /* Use GFP_NOIO, since this function can be called from
  2519. * xhci_discover_or_reset_device(), which may be called as part of
  2520. * mass storage driver error handling.
  2521. */
  2522. if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_NOIO)) {
  2523. xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
  2524. goto disable_slot;
  2525. }
  2526. udev->slot_id = xhci->slot_id;
  2527. /* Is this a LS or FS device under a HS hub? */
  2528. /* Hub or peripherial? */
  2529. return 1;
  2530. disable_slot:
  2531. /* Disable slot, if we can do it without mem alloc */
  2532. spin_lock_irqsave(&xhci->lock, flags);
  2533. if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
  2534. xhci_ring_cmd_db(xhci);
  2535. spin_unlock_irqrestore(&xhci->lock, flags);
  2536. return 0;
  2537. }
  2538. /*
  2539. * Issue an Address Device command (which will issue a SetAddress request to
  2540. * the device).
  2541. * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so
  2542. * we should only issue and wait on one address command at the same time.
  2543. *
  2544. * We add one to the device address issued by the hardware because the USB core
  2545. * uses address 1 for the root hubs (even though they're not really devices).
  2546. */
  2547. int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
  2548. {
  2549. unsigned long flags;
  2550. int timeleft;
  2551. struct xhci_virt_device *virt_dev;
  2552. int ret = 0;
  2553. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  2554. struct xhci_slot_ctx *slot_ctx;
  2555. struct xhci_input_control_ctx *ctrl_ctx;
  2556. u64 temp_64;
  2557. if (!udev->slot_id) {
  2558. xhci_dbg(xhci, "Bad Slot ID %d\n", udev->slot_id);
  2559. return -EINVAL;
  2560. }
  2561. virt_dev = xhci->devs[udev->slot_id];
  2562. if (WARN_ON(!virt_dev)) {
  2563. /*
  2564. * In plug/unplug torture test with an NEC controller,
  2565. * a zero-dereference was observed once due to virt_dev = 0.
  2566. * Print useful debug rather than crash if it is observed again!
  2567. */
  2568. xhci_warn(xhci, "Virt dev invalid for slot_id 0x%x!\n",
  2569. udev->slot_id);
  2570. return -EINVAL;
  2571. }
  2572. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
  2573. /*
  2574. * If this is the first Set Address since device plug-in or
  2575. * virt_device realloaction after a resume with an xHCI power loss,
  2576. * then set up the slot context.
  2577. */
  2578. if (!slot_ctx->dev_info)
  2579. xhci_setup_addressable_virt_dev(xhci, udev);
  2580. /* Otherwise, update the control endpoint ring enqueue pointer. */
  2581. else
  2582. xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev);
  2583. ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
  2584. ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
  2585. ctrl_ctx->drop_flags = 0;
  2586. xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
  2587. xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
  2588. spin_lock_irqsave(&xhci->lock, flags);
  2589. ret = xhci_queue_address_device(xhci, virt_dev->in_ctx->dma,
  2590. udev->slot_id);
  2591. if (ret) {
  2592. spin_unlock_irqrestore(&xhci->lock, flags);
  2593. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  2594. return ret;
  2595. }
  2596. xhci_ring_cmd_db(xhci);
  2597. spin_unlock_irqrestore(&xhci->lock, flags);
  2598. /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
  2599. timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
  2600. USB_CTRL_SET_TIMEOUT);
  2601. /* FIXME: From section 4.3.4: "Software shall be responsible for timing
  2602. * the SetAddress() "recovery interval" required by USB and aborting the
  2603. * command on a timeout.
  2604. */
  2605. if (timeleft <= 0) {
  2606. xhci_warn(xhci, "%s while waiting for a slot\n",
  2607. timeleft == 0 ? "Timeout" : "Signal");
  2608. /* FIXME cancel the address device command */
  2609. return -ETIME;
  2610. }
  2611. switch (virt_dev->cmd_status) {
  2612. case COMP_CTX_STATE:
  2613. case COMP_EBADSLT:
  2614. xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n",
  2615. udev->slot_id);
  2616. ret = -EINVAL;
  2617. break;
  2618. case COMP_TX_ERR:
  2619. dev_warn(&udev->dev, "Device not responding to set address.\n");
  2620. ret = -EPROTO;
  2621. break;
  2622. case COMP_DEV_ERR:
  2623. dev_warn(&udev->dev, "ERROR: Incompatible device for address "
  2624. "device command.\n");
  2625. ret = -ENODEV;
  2626. break;
  2627. case COMP_SUCCESS:
  2628. xhci_dbg(xhci, "Successful Address Device command\n");
  2629. break;
  2630. default:
  2631. xhci_err(xhci, "ERROR: unexpected command completion "
  2632. "code 0x%x.\n", virt_dev->cmd_status);
  2633. xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
  2634. xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
  2635. ret = -EINVAL;
  2636. break;
  2637. }
  2638. if (ret) {
  2639. return ret;
  2640. }
  2641. temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
  2642. xhci_dbg(xhci, "Op regs DCBAA ptr = %#016llx\n", temp_64);
  2643. xhci_dbg(xhci, "Slot ID %d dcbaa entry @%p = %#016llx\n",
  2644. udev->slot_id,
  2645. &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
  2646. (unsigned long long)
  2647. le64_to_cpu(xhci->dcbaa->dev_context_ptrs[udev->slot_id]));
  2648. xhci_dbg(xhci, "Output Context DMA address = %#08llx\n",
  2649. (unsigned long long)virt_dev->out_ctx->dma);
  2650. xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
  2651. xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
  2652. xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
  2653. xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
  2654. /*
  2655. * USB core uses address 1 for the roothubs, so we add one to the
  2656. * address given back to us by the HC.
  2657. */
  2658. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
  2659. /* Use kernel assigned address for devices; store xHC assigned
  2660. * address locally. */
  2661. virt_dev->address = (le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK)
  2662. + 1;
  2663. /* Zero the input context control for later use */
  2664. ctrl_ctx->add_flags = 0;
  2665. ctrl_ctx->drop_flags = 0;
  2666. xhci_dbg(xhci, "Internal device address = %d\n", virt_dev->address);
  2667. return 0;
  2668. }
  2669. /* Once a hub descriptor is fetched for a device, we need to update the xHC's
  2670. * internal data structures for the device.
  2671. */
  2672. int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
  2673. struct usb_tt *tt, gfp_t mem_flags)
  2674. {
  2675. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  2676. struct xhci_virt_device *vdev;
  2677. struct xhci_command *config_cmd;
  2678. struct xhci_input_control_ctx *ctrl_ctx;
  2679. struct xhci_slot_ctx *slot_ctx;
  2680. unsigned long flags;
  2681. unsigned think_time;
  2682. int ret;
  2683. /* Ignore root hubs */
  2684. if (!hdev->parent)
  2685. return 0;
  2686. vdev = xhci->devs[hdev->slot_id];
  2687. if (!vdev) {
  2688. xhci_warn(xhci, "Cannot update hub desc for unknown device.\n");
  2689. return -EINVAL;
  2690. }
  2691. config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
  2692. if (!config_cmd) {
  2693. xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
  2694. return -ENOMEM;
  2695. }
  2696. spin_lock_irqsave(&xhci->lock, flags);
  2697. xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx);
  2698. ctrl_ctx = xhci_get_input_control_ctx(xhci, config_cmd->in_ctx);
  2699. ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
  2700. slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx);
  2701. slot_ctx->dev_info |= cpu_to_le32(DEV_HUB);
  2702. if (tt->multi)
  2703. slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
  2704. if (xhci->hci_version > 0x95) {
  2705. xhci_dbg(xhci, "xHCI version %x needs hub "
  2706. "TT think time and number of ports\n",
  2707. (unsigned int) xhci->hci_version);
  2708. slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(hdev->maxchild));
  2709. /* Set TT think time - convert from ns to FS bit times.
  2710. * 0 = 8 FS bit times, 1 = 16 FS bit times,
  2711. * 2 = 24 FS bit times, 3 = 32 FS bit times.
  2712. *
  2713. * xHCI 1.0: this field shall be 0 if the device is not a
  2714. * High-spped hub.
  2715. */
  2716. think_time = tt->think_time;
  2717. if (think_time != 0)
  2718. think_time = (think_time / 666) - 1;
  2719. if (xhci->hci_version < 0x100 || hdev->speed == USB_SPEED_HIGH)
  2720. slot_ctx->tt_info |=
  2721. cpu_to_le32(TT_THINK_TIME(think_time));
  2722. } else {
  2723. xhci_dbg(xhci, "xHCI version %x doesn't need hub "
  2724. "TT think time or number of ports\n",
  2725. (unsigned int) xhci->hci_version);
  2726. }
  2727. slot_ctx->dev_state = 0;
  2728. spin_unlock_irqrestore(&xhci->lock, flags);
  2729. xhci_dbg(xhci, "Set up %s for hub device.\n",
  2730. (xhci->hci_version > 0x95) ?
  2731. "configure endpoint" : "evaluate context");
  2732. xhci_dbg(xhci, "Slot %u Input Context:\n", hdev->slot_id);
  2733. xhci_dbg_ctx(xhci, config_cmd->in_ctx, 0);
  2734. /* Issue and wait for the configure endpoint or
  2735. * evaluate context command.
  2736. */
  2737. if (xhci->hci_version > 0x95)
  2738. ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
  2739. false, false);
  2740. else
  2741. ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
  2742. true, false);
  2743. xhci_dbg(xhci, "Slot %u Output Context:\n", hdev->slot_id);
  2744. xhci_dbg_ctx(xhci, vdev->out_ctx, 0);
  2745. xhci_free_command(xhci, config_cmd);
  2746. return ret;
  2747. }
  2748. int xhci_get_frame(struct usb_hcd *hcd)
  2749. {
  2750. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  2751. /* EHCI mods by the periodic size. Why? */
  2752. return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
  2753. }
  2754. MODULE_DESCRIPTION(DRIVER_DESC);
  2755. MODULE_AUTHOR(DRIVER_AUTHOR);
  2756. MODULE_LICENSE("GPL");
  2757. static int __init xhci_hcd_init(void)
  2758. {
  2759. #ifdef CONFIG_PCI
  2760. int retval = 0;
  2761. retval = xhci_register_pci();
  2762. if (retval < 0) {
  2763. printk(KERN_DEBUG "Problem registering PCI driver.");
  2764. return retval;
  2765. }
  2766. #endif
  2767. /*
  2768. * Check the compiler generated sizes of structures that must be laid
  2769. * out in specific ways for hardware access.
  2770. */
  2771. BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
  2772. BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
  2773. BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
  2774. /* xhci_device_control has eight fields, and also
  2775. * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
  2776. */
  2777. BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
  2778. BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
  2779. BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
  2780. BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 7*32/8);
  2781. BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
  2782. /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
  2783. BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
  2784. BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
  2785. return 0;
  2786. }
  2787. module_init(xhci_hcd_init);
  2788. static void __exit xhci_hcd_cleanup(void)
  2789. {
  2790. #ifdef CONFIG_PCI
  2791. xhci_unregister_pci();
  2792. #endif
  2793. }
  2794. module_exit(xhci_hcd_cleanup);