xhci.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479
  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 microframes of the run/stop bit being cleared.
  87. * Read HC Halted bit in the status register to see when the HC is finished.
  88. * XXX: shouldn't we set HC_STATE_HALT here somewhere?
  89. */
  90. int xhci_halt(struct xhci_hcd *xhci)
  91. {
  92. xhci_dbg(xhci, "// Halt the HC\n");
  93. xhci_quiesce(xhci);
  94. return handshake(xhci, &xhci->op_regs->status,
  95. STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
  96. }
  97. /*
  98. * Set the run bit and wait for the host to be running.
  99. */
  100. int xhci_start(struct xhci_hcd *xhci)
  101. {
  102. u32 temp;
  103. int ret;
  104. temp = xhci_readl(xhci, &xhci->op_regs->command);
  105. temp |= (CMD_RUN);
  106. xhci_dbg(xhci, "// Turn on HC, cmd = 0x%x.\n",
  107. temp);
  108. xhci_writel(xhci, temp, &xhci->op_regs->command);
  109. /*
  110. * Wait for the HCHalted Status bit to be 0 to indicate the host is
  111. * running.
  112. */
  113. ret = handshake(xhci, &xhci->op_regs->status,
  114. STS_HALT, 0, XHCI_MAX_HALT_USEC);
  115. if (ret == -ETIMEDOUT)
  116. xhci_err(xhci, "Host took too long to start, "
  117. "waited %u microseconds.\n",
  118. XHCI_MAX_HALT_USEC);
  119. return ret;
  120. }
  121. /*
  122. * Reset a halted HC, and set the internal HC state to HC_STATE_HALT.
  123. *
  124. * This resets pipelines, timers, counters, state machines, etc.
  125. * Transactions will be terminated immediately, and operational registers
  126. * will be set to their defaults.
  127. */
  128. int xhci_reset(struct xhci_hcd *xhci)
  129. {
  130. u32 command;
  131. u32 state;
  132. int ret;
  133. state = xhci_readl(xhci, &xhci->op_regs->status);
  134. if ((state & STS_HALT) == 0) {
  135. xhci_warn(xhci, "Host controller not halted, aborting reset.\n");
  136. return 0;
  137. }
  138. xhci_dbg(xhci, "// Reset the HC\n");
  139. command = xhci_readl(xhci, &xhci->op_regs->command);
  140. command |= CMD_RESET;
  141. xhci_writel(xhci, command, &xhci->op_regs->command);
  142. /* XXX: Why does EHCI set this here? Shouldn't other code do this? */
  143. xhci_to_hcd(xhci)->state = HC_STATE_HALT;
  144. ret = handshake(xhci, &xhci->op_regs->command,
  145. CMD_RESET, 0, 250 * 1000);
  146. if (ret)
  147. return ret;
  148. xhci_dbg(xhci, "Wait for controller to be ready for doorbell rings\n");
  149. /*
  150. * xHCI cannot write to any doorbells or operational registers other
  151. * than status until the "Controller Not Ready" flag is cleared.
  152. */
  153. return handshake(xhci, &xhci->op_regs->status, STS_CNR, 0, 250 * 1000);
  154. }
  155. static irqreturn_t xhci_msi_irq(int irq, struct usb_hcd *hcd)
  156. {
  157. irqreturn_t ret;
  158. set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
  159. ret = xhci_irq(hcd);
  160. return ret;
  161. }
  162. /*
  163. * Free IRQs
  164. * free all IRQs request
  165. */
  166. static void xhci_free_irq(struct xhci_hcd *xhci)
  167. {
  168. int i;
  169. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  170. /* return if using legacy interrupt */
  171. if (xhci_to_hcd(xhci)->irq >= 0)
  172. return;
  173. if (xhci->msix_entries) {
  174. for (i = 0; i < xhci->msix_count; i++)
  175. if (xhci->msix_entries[i].vector)
  176. free_irq(xhci->msix_entries[i].vector,
  177. xhci_to_hcd(xhci));
  178. } else if (pdev->irq >= 0)
  179. free_irq(pdev->irq, xhci_to_hcd(xhci));
  180. return;
  181. }
  182. /*
  183. * Set up MSI
  184. */
  185. static int xhci_setup_msi(struct xhci_hcd *xhci)
  186. {
  187. int ret;
  188. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  189. ret = pci_enable_msi(pdev);
  190. if (ret) {
  191. xhci_err(xhci, "failed to allocate MSI entry\n");
  192. return ret;
  193. }
  194. ret = request_irq(pdev->irq, (irq_handler_t)xhci_msi_irq,
  195. 0, "xhci_hcd", xhci_to_hcd(xhci));
  196. if (ret) {
  197. xhci_err(xhci, "disable MSI interrupt\n");
  198. pci_disable_msi(pdev);
  199. }
  200. return ret;
  201. }
  202. /*
  203. * Set up MSI-X
  204. */
  205. static int xhci_setup_msix(struct xhci_hcd *xhci)
  206. {
  207. int i, ret = 0;
  208. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  209. /*
  210. * calculate number of msi-x vectors supported.
  211. * - HCS_MAX_INTRS: the max number of interrupts the host can handle,
  212. * with max number of interrupters based on the xhci HCSPARAMS1.
  213. * - num_online_cpus: maximum msi-x vectors per CPUs core.
  214. * Add additional 1 vector to ensure always available interrupt.
  215. */
  216. xhci->msix_count = min(num_online_cpus() + 1,
  217. HCS_MAX_INTRS(xhci->hcs_params1));
  218. xhci->msix_entries =
  219. kmalloc((sizeof(struct msix_entry))*xhci->msix_count,
  220. GFP_KERNEL);
  221. if (!xhci->msix_entries) {
  222. xhci_err(xhci, "Failed to allocate MSI-X entries\n");
  223. return -ENOMEM;
  224. }
  225. for (i = 0; i < xhci->msix_count; i++) {
  226. xhci->msix_entries[i].entry = i;
  227. xhci->msix_entries[i].vector = 0;
  228. }
  229. ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count);
  230. if (ret) {
  231. xhci_err(xhci, "Failed to enable MSI-X\n");
  232. goto free_entries;
  233. }
  234. for (i = 0; i < xhci->msix_count; i++) {
  235. ret = request_irq(xhci->msix_entries[i].vector,
  236. (irq_handler_t)xhci_msi_irq,
  237. 0, "xhci_hcd", xhci_to_hcd(xhci));
  238. if (ret)
  239. goto disable_msix;
  240. }
  241. return ret;
  242. disable_msix:
  243. xhci_err(xhci, "disable MSI-X interrupt\n");
  244. xhci_free_irq(xhci);
  245. pci_disable_msix(pdev);
  246. free_entries:
  247. kfree(xhci->msix_entries);
  248. xhci->msix_entries = NULL;
  249. return ret;
  250. }
  251. /* Free any IRQs and disable MSI-X */
  252. static void xhci_cleanup_msix(struct xhci_hcd *xhci)
  253. {
  254. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  255. xhci_free_irq(xhci);
  256. if (xhci->msix_entries) {
  257. pci_disable_msix(pdev);
  258. kfree(xhci->msix_entries);
  259. xhci->msix_entries = NULL;
  260. } else {
  261. pci_disable_msi(pdev);
  262. }
  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. * Called in interrupt context when there might be work
  290. * queued on the event ring
  291. *
  292. * xhci->lock must be held by caller.
  293. */
  294. static void xhci_work(struct xhci_hcd *xhci)
  295. {
  296. u32 temp;
  297. u64 temp_64;
  298. /*
  299. * Clear the op reg interrupt status first,
  300. * so we can receive interrupts from other MSI-X interrupters.
  301. * Write 1 to clear the interrupt status.
  302. */
  303. temp = xhci_readl(xhci, &xhci->op_regs->status);
  304. temp |= STS_EINT;
  305. xhci_writel(xhci, temp, &xhci->op_regs->status);
  306. /* FIXME when MSI-X is supported and there are multiple vectors */
  307. /* Clear the MSI-X event interrupt status */
  308. /* Acknowledge the interrupt */
  309. temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  310. temp |= 0x3;
  311. xhci_writel(xhci, temp, &xhci->ir_set->irq_pending);
  312. /* Flush posted writes */
  313. xhci_readl(xhci, &xhci->ir_set->irq_pending);
  314. if (xhci->xhc_state & XHCI_STATE_DYING)
  315. xhci_dbg(xhci, "xHCI dying, ignoring interrupt. "
  316. "Shouldn't IRQs be disabled?\n");
  317. else
  318. /* FIXME this should be a delayed service routine
  319. * that clears the EHB.
  320. */
  321. xhci_handle_event(xhci);
  322. /* Clear the event handler busy flag (RW1C); the event ring should be empty. */
  323. temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
  324. xhci_write_64(xhci, temp_64 | ERST_EHB, &xhci->ir_set->erst_dequeue);
  325. /* Flush posted writes -- FIXME is this necessary? */
  326. xhci_readl(xhci, &xhci->ir_set->irq_pending);
  327. }
  328. /*-------------------------------------------------------------------------*/
  329. /*
  330. * xHCI spec says we can get an interrupt, and if the HC has an error condition,
  331. * we might get bad data out of the event ring. Section 4.10.2.7 has a list of
  332. * indicators of an event TRB error, but we check the status *first* to be safe.
  333. */
  334. irqreturn_t xhci_irq(struct usb_hcd *hcd)
  335. {
  336. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  337. u32 temp, temp2;
  338. union xhci_trb *trb;
  339. spin_lock(&xhci->lock);
  340. trb = xhci->event_ring->dequeue;
  341. /* Check if the xHC generated the interrupt, or the irq is shared */
  342. temp = xhci_readl(xhci, &xhci->op_regs->status);
  343. temp2 = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  344. if (temp == 0xffffffff && temp2 == 0xffffffff)
  345. goto hw_died;
  346. if (!(temp & STS_EINT) && !ER_IRQ_PENDING(temp2)) {
  347. spin_unlock(&xhci->lock);
  348. return IRQ_NONE;
  349. }
  350. xhci_dbg(xhci, "op reg status = %08x\n", temp);
  351. xhci_dbg(xhci, "ir set irq_pending = %08x\n", temp2);
  352. xhci_dbg(xhci, "Event ring dequeue ptr:\n");
  353. xhci_dbg(xhci, "@%llx %08x %08x %08x %08x\n",
  354. (unsigned long long)xhci_trb_virt_to_dma(xhci->event_ring->deq_seg, trb),
  355. lower_32_bits(trb->link.segment_ptr),
  356. upper_32_bits(trb->link.segment_ptr),
  357. (unsigned int) trb->link.intr_target,
  358. (unsigned int) trb->link.control);
  359. if (temp & STS_FATAL) {
  360. xhci_warn(xhci, "WARNING: Host System Error\n");
  361. xhci_halt(xhci);
  362. hw_died:
  363. xhci_to_hcd(xhci)->state = HC_STATE_HALT;
  364. spin_unlock(&xhci->lock);
  365. return -ESHUTDOWN;
  366. }
  367. xhci_work(xhci);
  368. spin_unlock(&xhci->lock);
  369. return IRQ_HANDLED;
  370. }
  371. #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
  372. void xhci_event_ring_work(unsigned long arg)
  373. {
  374. unsigned long flags;
  375. int temp;
  376. u64 temp_64;
  377. struct xhci_hcd *xhci = (struct xhci_hcd *) arg;
  378. int i, j;
  379. xhci_dbg(xhci, "Poll event ring: %lu\n", jiffies);
  380. spin_lock_irqsave(&xhci->lock, flags);
  381. temp = xhci_readl(xhci, &xhci->op_regs->status);
  382. xhci_dbg(xhci, "op reg status = 0x%x\n", temp);
  383. if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING)) {
  384. xhci_dbg(xhci, "HW died, polling stopped.\n");
  385. spin_unlock_irqrestore(&xhci->lock, flags);
  386. return;
  387. }
  388. temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  389. xhci_dbg(xhci, "ir_set 0 pending = 0x%x\n", temp);
  390. xhci_dbg(xhci, "No-op commands handled = %d\n", xhci->noops_handled);
  391. xhci_dbg(xhci, "HC error bitmask = 0x%x\n", xhci->error_bitmask);
  392. xhci->error_bitmask = 0;
  393. xhci_dbg(xhci, "Event ring:\n");
  394. xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
  395. xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
  396. temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
  397. temp_64 &= ~ERST_PTR_MASK;
  398. xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
  399. xhci_dbg(xhci, "Command ring:\n");
  400. xhci_debug_segment(xhci, xhci->cmd_ring->deq_seg);
  401. xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
  402. xhci_dbg_cmd_ptrs(xhci);
  403. for (i = 0; i < MAX_HC_SLOTS; ++i) {
  404. if (!xhci->devs[i])
  405. continue;
  406. for (j = 0; j < 31; ++j) {
  407. xhci_dbg_ep_rings(xhci, i, j, &xhci->devs[i]->eps[j]);
  408. }
  409. }
  410. if (xhci->noops_submitted != NUM_TEST_NOOPS)
  411. if (xhci_setup_one_noop(xhci))
  412. xhci_ring_cmd_db(xhci);
  413. spin_unlock_irqrestore(&xhci->lock, flags);
  414. if (!xhci->zombie)
  415. mod_timer(&xhci->event_ring_timer, jiffies + POLL_TIMEOUT * HZ);
  416. else
  417. xhci_dbg(xhci, "Quit polling the event ring.\n");
  418. }
  419. #endif
  420. /*
  421. * Start the HC after it was halted.
  422. *
  423. * This function is called by the USB core when the HC driver is added.
  424. * Its opposite is xhci_stop().
  425. *
  426. * xhci_init() must be called once before this function can be called.
  427. * Reset the HC, enable device slot contexts, program DCBAAP, and
  428. * set command ring pointer and event ring pointer.
  429. *
  430. * Setup MSI-X vectors and enable interrupts.
  431. */
  432. int xhci_run(struct usb_hcd *hcd)
  433. {
  434. u32 temp;
  435. u64 temp_64;
  436. u32 ret;
  437. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  438. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  439. void (*doorbell)(struct xhci_hcd *) = NULL;
  440. hcd->uses_new_polling = 1;
  441. xhci_dbg(xhci, "xhci_run\n");
  442. /* unregister the legacy interrupt */
  443. if (hcd->irq)
  444. free_irq(hcd->irq, hcd);
  445. hcd->irq = -1;
  446. ret = xhci_setup_msix(xhci);
  447. if (ret)
  448. /* fall back to msi*/
  449. ret = xhci_setup_msi(xhci);
  450. if (ret) {
  451. /* fall back to legacy interrupt*/
  452. ret = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED,
  453. hcd->irq_descr, hcd);
  454. if (ret) {
  455. xhci_err(xhci, "request interrupt %d failed\n",
  456. pdev->irq);
  457. return ret;
  458. }
  459. hcd->irq = pdev->irq;
  460. }
  461. #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
  462. init_timer(&xhci->event_ring_timer);
  463. xhci->event_ring_timer.data = (unsigned long) xhci;
  464. xhci->event_ring_timer.function = xhci_event_ring_work;
  465. /* Poll the event ring */
  466. xhci->event_ring_timer.expires = jiffies + POLL_TIMEOUT * HZ;
  467. xhci->zombie = 0;
  468. xhci_dbg(xhci, "Setting event ring polling timer\n");
  469. add_timer(&xhci->event_ring_timer);
  470. #endif
  471. xhci_dbg(xhci, "Command ring memory map follows:\n");
  472. xhci_debug_ring(xhci, xhci->cmd_ring);
  473. xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
  474. xhci_dbg_cmd_ptrs(xhci);
  475. xhci_dbg(xhci, "ERST memory map follows:\n");
  476. xhci_dbg_erst(xhci, &xhci->erst);
  477. xhci_dbg(xhci, "Event ring:\n");
  478. xhci_debug_ring(xhci, xhci->event_ring);
  479. xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
  480. temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
  481. temp_64 &= ~ERST_PTR_MASK;
  482. xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
  483. xhci_dbg(xhci, "// Set the interrupt modulation register\n");
  484. temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
  485. temp &= ~ER_IRQ_INTERVAL_MASK;
  486. temp |= (u32) 160;
  487. xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
  488. /* Set the HCD state before we enable the irqs */
  489. hcd->state = HC_STATE_RUNNING;
  490. temp = xhci_readl(xhci, &xhci->op_regs->command);
  491. temp |= (CMD_EIE);
  492. xhci_dbg(xhci, "// Enable interrupts, cmd = 0x%x.\n",
  493. temp);
  494. xhci_writel(xhci, temp, &xhci->op_regs->command);
  495. temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  496. xhci_dbg(xhci, "// Enabling event ring interrupter %p by writing 0x%x to irq_pending\n",
  497. xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
  498. xhci_writel(xhci, ER_IRQ_ENABLE(temp),
  499. &xhci->ir_set->irq_pending);
  500. xhci_print_ir_set(xhci, xhci->ir_set, 0);
  501. if (NUM_TEST_NOOPS > 0)
  502. doorbell = xhci_setup_one_noop(xhci);
  503. if (xhci->quirks & XHCI_NEC_HOST)
  504. xhci_queue_vendor_command(xhci, 0, 0, 0,
  505. TRB_TYPE(TRB_NEC_GET_FW));
  506. if (xhci_start(xhci)) {
  507. xhci_halt(xhci);
  508. return -ENODEV;
  509. }
  510. if (doorbell)
  511. (*doorbell)(xhci);
  512. if (xhci->quirks & XHCI_NEC_HOST)
  513. xhci_ring_cmd_db(xhci);
  514. xhci_dbg(xhci, "Finished xhci_run\n");
  515. return 0;
  516. }
  517. /*
  518. * Stop xHCI driver.
  519. *
  520. * This function is called by the USB core when the HC driver is removed.
  521. * Its opposite is xhci_run().
  522. *
  523. * Disable device contexts, disable IRQs, and quiesce the HC.
  524. * Reset the HC, finish any completed transactions, and cleanup memory.
  525. */
  526. void xhci_stop(struct usb_hcd *hcd)
  527. {
  528. u32 temp;
  529. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  530. spin_lock_irq(&xhci->lock);
  531. xhci_halt(xhci);
  532. xhci_reset(xhci);
  533. xhci_cleanup_msix(xhci);
  534. spin_unlock_irq(&xhci->lock);
  535. #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
  536. /* Tell the event ring poll function not to reschedule */
  537. xhci->zombie = 1;
  538. del_timer_sync(&xhci->event_ring_timer);
  539. #endif
  540. xhci_dbg(xhci, "// Disabling event ring interrupts\n");
  541. temp = xhci_readl(xhci, &xhci->op_regs->status);
  542. xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
  543. temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  544. xhci_writel(xhci, ER_IRQ_DISABLE(temp),
  545. &xhci->ir_set->irq_pending);
  546. xhci_print_ir_set(xhci, xhci->ir_set, 0);
  547. xhci_dbg(xhci, "cleaning up memory\n");
  548. xhci_mem_cleanup(xhci);
  549. xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
  550. xhci_readl(xhci, &xhci->op_regs->status));
  551. }
  552. /*
  553. * Shutdown HC (not bus-specific)
  554. *
  555. * This is called when the machine is rebooting or halting. We assume that the
  556. * machine will be powered off, and the HC's internal state will be reset.
  557. * Don't bother to free memory.
  558. */
  559. void xhci_shutdown(struct usb_hcd *hcd)
  560. {
  561. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  562. spin_lock_irq(&xhci->lock);
  563. xhci_halt(xhci);
  564. xhci_cleanup_msix(xhci);
  565. spin_unlock_irq(&xhci->lock);
  566. xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n",
  567. xhci_readl(xhci, &xhci->op_regs->status));
  568. }
  569. /*-------------------------------------------------------------------------*/
  570. /**
  571. * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
  572. * HCDs. Find the index for an endpoint given its descriptor. Use the return
  573. * value to right shift 1 for the bitmask.
  574. *
  575. * Index = (epnum * 2) + direction - 1,
  576. * where direction = 0 for OUT, 1 for IN.
  577. * For control endpoints, the IN index is used (OUT index is unused), so
  578. * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
  579. */
  580. unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
  581. {
  582. unsigned int index;
  583. if (usb_endpoint_xfer_control(desc))
  584. index = (unsigned int) (usb_endpoint_num(desc)*2);
  585. else
  586. index = (unsigned int) (usb_endpoint_num(desc)*2) +
  587. (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
  588. return index;
  589. }
  590. /* Find the flag for this endpoint (for use in the control context). Use the
  591. * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
  592. * bit 1, etc.
  593. */
  594. unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
  595. {
  596. return 1 << (xhci_get_endpoint_index(desc) + 1);
  597. }
  598. /* Find the flag for this endpoint (for use in the control context). Use the
  599. * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
  600. * bit 1, etc.
  601. */
  602. unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index)
  603. {
  604. return 1 << (ep_index + 1);
  605. }
  606. /* Compute the last valid endpoint context index. Basically, this is the
  607. * endpoint index plus one. For slot contexts with more than valid endpoint,
  608. * we find the most significant bit set in the added contexts flags.
  609. * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
  610. * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
  611. */
  612. unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
  613. {
  614. return fls(added_ctxs) - 1;
  615. }
  616. /* Returns 1 if the arguments are OK;
  617. * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
  618. */
  619. int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
  620. struct usb_host_endpoint *ep, int check_ep, const char *func) {
  621. if (!hcd || (check_ep && !ep) || !udev) {
  622. printk(KERN_DEBUG "xHCI %s called with invalid args\n",
  623. func);
  624. return -EINVAL;
  625. }
  626. if (!udev->parent) {
  627. printk(KERN_DEBUG "xHCI %s called for root hub\n",
  628. func);
  629. return 0;
  630. }
  631. if (!udev->slot_id) {
  632. printk(KERN_DEBUG "xHCI %s called with unaddressed device\n",
  633. func);
  634. return -EINVAL;
  635. }
  636. return 1;
  637. }
  638. static int xhci_configure_endpoint(struct xhci_hcd *xhci,
  639. struct usb_device *udev, struct xhci_command *command,
  640. bool ctx_change, bool must_succeed);
  641. /*
  642. * Full speed devices may have a max packet size greater than 8 bytes, but the
  643. * USB core doesn't know that until it reads the first 8 bytes of the
  644. * descriptor. If the usb_device's max packet size changes after that point,
  645. * we need to issue an evaluate context command and wait on it.
  646. */
  647. static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
  648. unsigned int ep_index, struct urb *urb)
  649. {
  650. struct xhci_container_ctx *in_ctx;
  651. struct xhci_container_ctx *out_ctx;
  652. struct xhci_input_control_ctx *ctrl_ctx;
  653. struct xhci_ep_ctx *ep_ctx;
  654. int max_packet_size;
  655. int hw_max_packet_size;
  656. int ret = 0;
  657. out_ctx = xhci->devs[slot_id]->out_ctx;
  658. ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
  659. hw_max_packet_size = MAX_PACKET_DECODED(ep_ctx->ep_info2);
  660. max_packet_size = urb->dev->ep0.desc.wMaxPacketSize;
  661. if (hw_max_packet_size != max_packet_size) {
  662. xhci_dbg(xhci, "Max Packet Size for ep 0 changed.\n");
  663. xhci_dbg(xhci, "Max packet size in usb_device = %d\n",
  664. max_packet_size);
  665. xhci_dbg(xhci, "Max packet size in xHCI HW = %d\n",
  666. hw_max_packet_size);
  667. xhci_dbg(xhci, "Issuing evaluate context command.\n");
  668. /* Set up the modified control endpoint 0 */
  669. xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
  670. xhci->devs[slot_id]->out_ctx, ep_index);
  671. in_ctx = xhci->devs[slot_id]->in_ctx;
  672. ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
  673. ep_ctx->ep_info2 &= ~MAX_PACKET_MASK;
  674. ep_ctx->ep_info2 |= MAX_PACKET(max_packet_size);
  675. /* Set up the input context flags for the command */
  676. /* FIXME: This won't work if a non-default control endpoint
  677. * changes max packet sizes.
  678. */
  679. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  680. ctrl_ctx->add_flags = EP0_FLAG;
  681. ctrl_ctx->drop_flags = 0;
  682. xhci_dbg(xhci, "Slot %d input context\n", slot_id);
  683. xhci_dbg_ctx(xhci, in_ctx, ep_index);
  684. xhci_dbg(xhci, "Slot %d output context\n", slot_id);
  685. xhci_dbg_ctx(xhci, out_ctx, ep_index);
  686. ret = xhci_configure_endpoint(xhci, urb->dev, NULL,
  687. true, false);
  688. /* Clean up the input context for later use by bandwidth
  689. * functions.
  690. */
  691. ctrl_ctx->add_flags = SLOT_FLAG;
  692. }
  693. return ret;
  694. }
  695. /*
  696. * non-error returns are a promise to giveback() the urb later
  697. * we drop ownership so next owner (or urb unlink) can get it
  698. */
  699. int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
  700. {
  701. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  702. unsigned long flags;
  703. int ret = 0;
  704. unsigned int slot_id, ep_index;
  705. struct urb_priv *urb_priv;
  706. int size, i;
  707. if (!urb || xhci_check_args(hcd, urb->dev, urb->ep, true, __func__) <= 0)
  708. return -EINVAL;
  709. slot_id = urb->dev->slot_id;
  710. ep_index = xhci_get_endpoint_index(&urb->ep->desc);
  711. if (!xhci->devs || !xhci->devs[slot_id]) {
  712. if (!in_interrupt())
  713. dev_warn(&urb->dev->dev, "WARN: urb submitted for dev with no Slot ID\n");
  714. ret = -EINVAL;
  715. goto exit;
  716. }
  717. if (!HCD_HW_ACCESSIBLE(hcd)) {
  718. if (!in_interrupt())
  719. xhci_dbg(xhci, "urb submitted during PCI suspend\n");
  720. ret = -ESHUTDOWN;
  721. goto exit;
  722. }
  723. if (usb_endpoint_xfer_isoc(&urb->ep->desc))
  724. size = urb->number_of_packets;
  725. else
  726. size = 1;
  727. urb_priv = kzalloc(sizeof(struct urb_priv) +
  728. size * sizeof(struct xhci_td *), mem_flags);
  729. if (!urb_priv)
  730. return -ENOMEM;
  731. for (i = 0; i < size; i++) {
  732. urb_priv->td[i] = kzalloc(sizeof(struct xhci_td), mem_flags);
  733. if (!urb_priv->td[i]) {
  734. urb_priv->length = i;
  735. xhci_urb_free_priv(xhci, urb_priv);
  736. return -ENOMEM;
  737. }
  738. }
  739. urb_priv->length = size;
  740. urb_priv->td_cnt = 0;
  741. urb->hcpriv = urb_priv;
  742. if (usb_endpoint_xfer_control(&urb->ep->desc)) {
  743. /* Check to see if the max packet size for the default control
  744. * endpoint changed during FS device enumeration
  745. */
  746. if (urb->dev->speed == USB_SPEED_FULL) {
  747. ret = xhci_check_maxpacket(xhci, slot_id,
  748. ep_index, urb);
  749. if (ret < 0)
  750. return ret;
  751. }
  752. /* We have a spinlock and interrupts disabled, so we must pass
  753. * atomic context to this function, which may allocate memory.
  754. */
  755. spin_lock_irqsave(&xhci->lock, flags);
  756. if (xhci->xhc_state & XHCI_STATE_DYING)
  757. goto dying;
  758. ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
  759. slot_id, ep_index);
  760. spin_unlock_irqrestore(&xhci->lock, flags);
  761. } else if (usb_endpoint_xfer_bulk(&urb->ep->desc)) {
  762. spin_lock_irqsave(&xhci->lock, flags);
  763. if (xhci->xhc_state & XHCI_STATE_DYING)
  764. goto dying;
  765. if (xhci->devs[slot_id]->eps[ep_index].ep_state &
  766. EP_GETTING_STREAMS) {
  767. xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
  768. "is transitioning to using streams.\n");
  769. ret = -EINVAL;
  770. } else if (xhci->devs[slot_id]->eps[ep_index].ep_state &
  771. EP_GETTING_NO_STREAMS) {
  772. xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
  773. "is transitioning to "
  774. "not having streams.\n");
  775. ret = -EINVAL;
  776. } else {
  777. ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
  778. slot_id, ep_index);
  779. }
  780. spin_unlock_irqrestore(&xhci->lock, flags);
  781. } else if (usb_endpoint_xfer_int(&urb->ep->desc)) {
  782. spin_lock_irqsave(&xhci->lock, flags);
  783. if (xhci->xhc_state & XHCI_STATE_DYING)
  784. goto dying;
  785. ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
  786. slot_id, ep_index);
  787. spin_unlock_irqrestore(&xhci->lock, flags);
  788. } else {
  789. spin_lock_irqsave(&xhci->lock, flags);
  790. if (xhci->xhc_state & XHCI_STATE_DYING)
  791. goto dying;
  792. ret = xhci_queue_isoc_tx_prepare(xhci, GFP_ATOMIC, urb,
  793. slot_id, ep_index);
  794. spin_unlock_irqrestore(&xhci->lock, flags);
  795. }
  796. exit:
  797. return ret;
  798. dying:
  799. xhci_urb_free_priv(xhci, urb_priv);
  800. urb->hcpriv = NULL;
  801. xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for "
  802. "non-responsive xHCI host.\n",
  803. urb->ep->desc.bEndpointAddress, urb);
  804. spin_unlock_irqrestore(&xhci->lock, flags);
  805. return -ESHUTDOWN;
  806. }
  807. /*
  808. * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
  809. * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
  810. * should pick up where it left off in the TD, unless a Set Transfer Ring
  811. * Dequeue Pointer is issued.
  812. *
  813. * The TRBs that make up the buffers for the canceled URB will be "removed" from
  814. * the ring. Since the ring is a contiguous structure, they can't be physically
  815. * removed. Instead, there are two options:
  816. *
  817. * 1) If the HC is in the middle of processing the URB to be canceled, we
  818. * simply move the ring's dequeue pointer past those TRBs using the Set
  819. * Transfer Ring Dequeue Pointer command. This will be the common case,
  820. * when drivers timeout on the last submitted URB and attempt to cancel.
  821. *
  822. * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
  823. * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
  824. * HC will need to invalidate the any TRBs it has cached after the stop
  825. * endpoint command, as noted in the xHCI 0.95 errata.
  826. *
  827. * 3) The TD may have completed by the time the Stop Endpoint Command
  828. * completes, so software needs to handle that case too.
  829. *
  830. * This function should protect against the TD enqueueing code ringing the
  831. * doorbell while this code is waiting for a Stop Endpoint command to complete.
  832. * It also needs to account for multiple cancellations on happening at the same
  833. * time for the same endpoint.
  834. *
  835. * Note that this function can be called in any context, or so says
  836. * usb_hcd_unlink_urb()
  837. */
  838. int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  839. {
  840. unsigned long flags;
  841. int ret, i;
  842. u32 temp;
  843. struct xhci_hcd *xhci;
  844. struct urb_priv *urb_priv;
  845. struct xhci_td *td;
  846. unsigned int ep_index;
  847. struct xhci_ring *ep_ring;
  848. struct xhci_virt_ep *ep;
  849. xhci = hcd_to_xhci(hcd);
  850. spin_lock_irqsave(&xhci->lock, flags);
  851. /* Make sure the URB hasn't completed or been unlinked already */
  852. ret = usb_hcd_check_unlink_urb(hcd, urb, status);
  853. if (ret || !urb->hcpriv)
  854. goto done;
  855. temp = xhci_readl(xhci, &xhci->op_regs->status);
  856. if (temp == 0xffffffff) {
  857. xhci_dbg(xhci, "HW died, freeing TD.\n");
  858. urb_priv = urb->hcpriv;
  859. usb_hcd_unlink_urb_from_ep(hcd, urb);
  860. spin_unlock_irqrestore(&xhci->lock, flags);
  861. usb_hcd_giveback_urb(xhci_to_hcd(xhci), urb, -ESHUTDOWN);
  862. xhci_urb_free_priv(xhci, urb_priv);
  863. return ret;
  864. }
  865. if (xhci->xhc_state & XHCI_STATE_DYING) {
  866. xhci_dbg(xhci, "Ep 0x%x: URB %p to be canceled on "
  867. "non-responsive xHCI host.\n",
  868. urb->ep->desc.bEndpointAddress, urb);
  869. /* Let the stop endpoint command watchdog timer (which set this
  870. * state) finish cleaning up the endpoint TD lists. We must
  871. * have caught it in the middle of dropping a lock and giving
  872. * back an URB.
  873. */
  874. goto done;
  875. }
  876. xhci_dbg(xhci, "Cancel URB %p\n", urb);
  877. xhci_dbg(xhci, "Event ring:\n");
  878. xhci_debug_ring(xhci, xhci->event_ring);
  879. ep_index = xhci_get_endpoint_index(&urb->ep->desc);
  880. ep = &xhci->devs[urb->dev->slot_id]->eps[ep_index];
  881. ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
  882. if (!ep_ring) {
  883. ret = -EINVAL;
  884. goto done;
  885. }
  886. xhci_dbg(xhci, "Endpoint ring:\n");
  887. xhci_debug_ring(xhci, ep_ring);
  888. urb_priv = urb->hcpriv;
  889. for (i = urb_priv->td_cnt; i < urb_priv->length; i++) {
  890. td = urb_priv->td[i];
  891. list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
  892. }
  893. /* Queue a stop endpoint command, but only if this is
  894. * the first cancellation to be handled.
  895. */
  896. if (!(ep->ep_state & EP_HALT_PENDING)) {
  897. ep->ep_state |= EP_HALT_PENDING;
  898. ep->stop_cmds_pending++;
  899. ep->stop_cmd_timer.expires = jiffies +
  900. XHCI_STOP_EP_CMD_TIMEOUT * HZ;
  901. add_timer(&ep->stop_cmd_timer);
  902. xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index);
  903. xhci_ring_cmd_db(xhci);
  904. }
  905. done:
  906. spin_unlock_irqrestore(&xhci->lock, flags);
  907. return ret;
  908. }
  909. /* Drop an endpoint from a new bandwidth configuration for this device.
  910. * Only one call to this function is allowed per endpoint before
  911. * check_bandwidth() or reset_bandwidth() must be called.
  912. * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
  913. * add the endpoint to the schedule with possibly new parameters denoted by a
  914. * different endpoint descriptor in usb_host_endpoint.
  915. * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
  916. * not allowed.
  917. *
  918. * The USB core will not allow URBs to be queued to an endpoint that is being
  919. * disabled, so there's no need for mutual exclusion to protect
  920. * the xhci->devs[slot_id] structure.
  921. */
  922. int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
  923. struct usb_host_endpoint *ep)
  924. {
  925. struct xhci_hcd *xhci;
  926. struct xhci_container_ctx *in_ctx, *out_ctx;
  927. struct xhci_input_control_ctx *ctrl_ctx;
  928. struct xhci_slot_ctx *slot_ctx;
  929. unsigned int last_ctx;
  930. unsigned int ep_index;
  931. struct xhci_ep_ctx *ep_ctx;
  932. u32 drop_flag;
  933. u32 new_add_flags, new_drop_flags, new_slot_info;
  934. int ret;
  935. ret = xhci_check_args(hcd, udev, ep, 1, __func__);
  936. if (ret <= 0)
  937. return ret;
  938. xhci = hcd_to_xhci(hcd);
  939. xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
  940. drop_flag = xhci_get_endpoint_flag(&ep->desc);
  941. if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
  942. xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
  943. __func__, drop_flag);
  944. return 0;
  945. }
  946. if (!xhci->devs || !xhci->devs[udev->slot_id]) {
  947. xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
  948. __func__);
  949. return -EINVAL;
  950. }
  951. in_ctx = xhci->devs[udev->slot_id]->in_ctx;
  952. out_ctx = xhci->devs[udev->slot_id]->out_ctx;
  953. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  954. ep_index = xhci_get_endpoint_index(&ep->desc);
  955. ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
  956. /* If the HC already knows the endpoint is disabled,
  957. * or the HCD has noted it is disabled, ignore this request
  958. */
  959. if ((ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_DISABLED ||
  960. ctrl_ctx->drop_flags & xhci_get_endpoint_flag(&ep->desc)) {
  961. xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
  962. __func__, ep);
  963. return 0;
  964. }
  965. ctrl_ctx->drop_flags |= drop_flag;
  966. new_drop_flags = ctrl_ctx->drop_flags;
  967. ctrl_ctx->add_flags &= ~drop_flag;
  968. new_add_flags = ctrl_ctx->add_flags;
  969. last_ctx = xhci_last_valid_endpoint(ctrl_ctx->add_flags);
  970. slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
  971. /* Update the last valid endpoint context, if we deleted the last one */
  972. if ((slot_ctx->dev_info & LAST_CTX_MASK) > LAST_CTX(last_ctx)) {
  973. slot_ctx->dev_info &= ~LAST_CTX_MASK;
  974. slot_ctx->dev_info |= LAST_CTX(last_ctx);
  975. }
  976. new_slot_info = slot_ctx->dev_info;
  977. xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
  978. xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
  979. (unsigned int) ep->desc.bEndpointAddress,
  980. udev->slot_id,
  981. (unsigned int) new_drop_flags,
  982. (unsigned int) new_add_flags,
  983. (unsigned int) new_slot_info);
  984. return 0;
  985. }
  986. /* Add an endpoint to a new possible bandwidth configuration for this device.
  987. * Only one call to this function is allowed per endpoint before
  988. * check_bandwidth() or reset_bandwidth() must be called.
  989. * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
  990. * add the endpoint to the schedule with possibly new parameters denoted by a
  991. * different endpoint descriptor in usb_host_endpoint.
  992. * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
  993. * not allowed.
  994. *
  995. * The USB core will not allow URBs to be queued to an endpoint until the
  996. * configuration or alt setting is installed in the device, so there's no need
  997. * for mutual exclusion to protect the xhci->devs[slot_id] structure.
  998. */
  999. int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
  1000. struct usb_host_endpoint *ep)
  1001. {
  1002. struct xhci_hcd *xhci;
  1003. struct xhci_container_ctx *in_ctx, *out_ctx;
  1004. unsigned int ep_index;
  1005. struct xhci_ep_ctx *ep_ctx;
  1006. struct xhci_slot_ctx *slot_ctx;
  1007. struct xhci_input_control_ctx *ctrl_ctx;
  1008. u32 added_ctxs;
  1009. unsigned int last_ctx;
  1010. u32 new_add_flags, new_drop_flags, new_slot_info;
  1011. int ret = 0;
  1012. ret = xhci_check_args(hcd, udev, ep, 1, __func__);
  1013. if (ret <= 0) {
  1014. /* So we won't queue a reset ep command for a root hub */
  1015. ep->hcpriv = NULL;
  1016. return ret;
  1017. }
  1018. xhci = hcd_to_xhci(hcd);
  1019. added_ctxs = xhci_get_endpoint_flag(&ep->desc);
  1020. last_ctx = xhci_last_valid_endpoint(added_ctxs);
  1021. if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
  1022. /* FIXME when we have to issue an evaluate endpoint command to
  1023. * deal with ep0 max packet size changing once we get the
  1024. * descriptors
  1025. */
  1026. xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
  1027. __func__, added_ctxs);
  1028. return 0;
  1029. }
  1030. if (!xhci->devs || !xhci->devs[udev->slot_id]) {
  1031. xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
  1032. __func__);
  1033. return -EINVAL;
  1034. }
  1035. in_ctx = xhci->devs[udev->slot_id]->in_ctx;
  1036. out_ctx = xhci->devs[udev->slot_id]->out_ctx;
  1037. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  1038. ep_index = xhci_get_endpoint_index(&ep->desc);
  1039. ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
  1040. /* If the HCD has already noted the endpoint is enabled,
  1041. * ignore this request.
  1042. */
  1043. if (ctrl_ctx->add_flags & xhci_get_endpoint_flag(&ep->desc)) {
  1044. xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
  1045. __func__, ep);
  1046. return 0;
  1047. }
  1048. /*
  1049. * Configuration and alternate setting changes must be done in
  1050. * process context, not interrupt context (or so documenation
  1051. * for usb_set_interface() and usb_set_configuration() claim).
  1052. */
  1053. if (xhci_endpoint_init(xhci, xhci->devs[udev->slot_id],
  1054. udev, ep, GFP_NOIO) < 0) {
  1055. dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
  1056. __func__, ep->desc.bEndpointAddress);
  1057. return -ENOMEM;
  1058. }
  1059. ctrl_ctx->add_flags |= added_ctxs;
  1060. new_add_flags = ctrl_ctx->add_flags;
  1061. /* If xhci_endpoint_disable() was called for this endpoint, but the
  1062. * xHC hasn't been notified yet through the check_bandwidth() call,
  1063. * this re-adds a new state for the endpoint from the new endpoint
  1064. * descriptors. We must drop and re-add this endpoint, so we leave the
  1065. * drop flags alone.
  1066. */
  1067. new_drop_flags = ctrl_ctx->drop_flags;
  1068. slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
  1069. /* Update the last valid endpoint context, if we just added one past */
  1070. if ((slot_ctx->dev_info & LAST_CTX_MASK) < LAST_CTX(last_ctx)) {
  1071. slot_ctx->dev_info &= ~LAST_CTX_MASK;
  1072. slot_ctx->dev_info |= LAST_CTX(last_ctx);
  1073. }
  1074. new_slot_info = slot_ctx->dev_info;
  1075. /* Store the usb_device pointer for later use */
  1076. ep->hcpriv = udev;
  1077. xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
  1078. (unsigned int) ep->desc.bEndpointAddress,
  1079. udev->slot_id,
  1080. (unsigned int) new_drop_flags,
  1081. (unsigned int) new_add_flags,
  1082. (unsigned int) new_slot_info);
  1083. return 0;
  1084. }
  1085. static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev)
  1086. {
  1087. struct xhci_input_control_ctx *ctrl_ctx;
  1088. struct xhci_ep_ctx *ep_ctx;
  1089. struct xhci_slot_ctx *slot_ctx;
  1090. int i;
  1091. /* When a device's add flag and drop flag are zero, any subsequent
  1092. * configure endpoint command will leave that endpoint's state
  1093. * untouched. Make sure we don't leave any old state in the input
  1094. * endpoint contexts.
  1095. */
  1096. ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
  1097. ctrl_ctx->drop_flags = 0;
  1098. ctrl_ctx->add_flags = 0;
  1099. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
  1100. slot_ctx->dev_info &= ~LAST_CTX_MASK;
  1101. /* Endpoint 0 is always valid */
  1102. slot_ctx->dev_info |= LAST_CTX(1);
  1103. for (i = 1; i < 31; ++i) {
  1104. ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
  1105. ep_ctx->ep_info = 0;
  1106. ep_ctx->ep_info2 = 0;
  1107. ep_ctx->deq = 0;
  1108. ep_ctx->tx_info = 0;
  1109. }
  1110. }
  1111. static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
  1112. struct usb_device *udev, int *cmd_status)
  1113. {
  1114. int ret;
  1115. switch (*cmd_status) {
  1116. case COMP_ENOMEM:
  1117. dev_warn(&udev->dev, "Not enough host controller resources "
  1118. "for new device state.\n");
  1119. ret = -ENOMEM;
  1120. /* FIXME: can we allocate more resources for the HC? */
  1121. break;
  1122. case COMP_BW_ERR:
  1123. dev_warn(&udev->dev, "Not enough bandwidth "
  1124. "for new device state.\n");
  1125. ret = -ENOSPC;
  1126. /* FIXME: can we go back to the old state? */
  1127. break;
  1128. case COMP_TRB_ERR:
  1129. /* the HCD set up something wrong */
  1130. dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
  1131. "add flag = 1, "
  1132. "and endpoint is not disabled.\n");
  1133. ret = -EINVAL;
  1134. break;
  1135. case COMP_SUCCESS:
  1136. dev_dbg(&udev->dev, "Successful Endpoint Configure command\n");
  1137. ret = 0;
  1138. break;
  1139. default:
  1140. xhci_err(xhci, "ERROR: unexpected command completion "
  1141. "code 0x%x.\n", *cmd_status);
  1142. ret = -EINVAL;
  1143. break;
  1144. }
  1145. return ret;
  1146. }
  1147. static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
  1148. struct usb_device *udev, int *cmd_status)
  1149. {
  1150. int ret;
  1151. struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
  1152. switch (*cmd_status) {
  1153. case COMP_EINVAL:
  1154. dev_warn(&udev->dev, "WARN: xHCI driver setup invalid evaluate "
  1155. "context command.\n");
  1156. ret = -EINVAL;
  1157. break;
  1158. case COMP_EBADSLT:
  1159. dev_warn(&udev->dev, "WARN: slot not enabled for"
  1160. "evaluate context command.\n");
  1161. case COMP_CTX_STATE:
  1162. dev_warn(&udev->dev, "WARN: invalid context state for "
  1163. "evaluate context command.\n");
  1164. xhci_dbg_ctx(xhci, virt_dev->out_ctx, 1);
  1165. ret = -EINVAL;
  1166. break;
  1167. case COMP_SUCCESS:
  1168. dev_dbg(&udev->dev, "Successful evaluate context command\n");
  1169. ret = 0;
  1170. break;
  1171. default:
  1172. xhci_err(xhci, "ERROR: unexpected command completion "
  1173. "code 0x%x.\n", *cmd_status);
  1174. ret = -EINVAL;
  1175. break;
  1176. }
  1177. return ret;
  1178. }
  1179. /* Issue a configure endpoint command or evaluate context command
  1180. * and wait for it to finish.
  1181. */
  1182. static int xhci_configure_endpoint(struct xhci_hcd *xhci,
  1183. struct usb_device *udev,
  1184. struct xhci_command *command,
  1185. bool ctx_change, bool must_succeed)
  1186. {
  1187. int ret;
  1188. int timeleft;
  1189. unsigned long flags;
  1190. struct xhci_container_ctx *in_ctx;
  1191. struct completion *cmd_completion;
  1192. int *cmd_status;
  1193. struct xhci_virt_device *virt_dev;
  1194. spin_lock_irqsave(&xhci->lock, flags);
  1195. virt_dev = xhci->devs[udev->slot_id];
  1196. if (command) {
  1197. in_ctx = command->in_ctx;
  1198. cmd_completion = command->completion;
  1199. cmd_status = &command->status;
  1200. command->command_trb = xhci->cmd_ring->enqueue;
  1201. list_add_tail(&command->cmd_list, &virt_dev->cmd_list);
  1202. } else {
  1203. in_ctx = virt_dev->in_ctx;
  1204. cmd_completion = &virt_dev->cmd_completion;
  1205. cmd_status = &virt_dev->cmd_status;
  1206. }
  1207. init_completion(cmd_completion);
  1208. if (!ctx_change)
  1209. ret = xhci_queue_configure_endpoint(xhci, in_ctx->dma,
  1210. udev->slot_id, must_succeed);
  1211. else
  1212. ret = xhci_queue_evaluate_context(xhci, in_ctx->dma,
  1213. udev->slot_id);
  1214. if (ret < 0) {
  1215. if (command)
  1216. list_del(&command->cmd_list);
  1217. spin_unlock_irqrestore(&xhci->lock, flags);
  1218. xhci_dbg(xhci, "FIXME allocate a new ring segment\n");
  1219. return -ENOMEM;
  1220. }
  1221. xhci_ring_cmd_db(xhci);
  1222. spin_unlock_irqrestore(&xhci->lock, flags);
  1223. /* Wait for the configure endpoint command to complete */
  1224. timeleft = wait_for_completion_interruptible_timeout(
  1225. cmd_completion,
  1226. USB_CTRL_SET_TIMEOUT);
  1227. if (timeleft <= 0) {
  1228. xhci_warn(xhci, "%s while waiting for %s command\n",
  1229. timeleft == 0 ? "Timeout" : "Signal",
  1230. ctx_change == 0 ?
  1231. "configure endpoint" :
  1232. "evaluate context");
  1233. /* FIXME cancel the configure endpoint command */
  1234. return -ETIME;
  1235. }
  1236. if (!ctx_change)
  1237. return xhci_configure_endpoint_result(xhci, udev, cmd_status);
  1238. return xhci_evaluate_context_result(xhci, udev, cmd_status);
  1239. }
  1240. /* Called after one or more calls to xhci_add_endpoint() or
  1241. * xhci_drop_endpoint(). If this call fails, the USB core is expected
  1242. * to call xhci_reset_bandwidth().
  1243. *
  1244. * Since we are in the middle of changing either configuration or
  1245. * installing a new alt setting, the USB core won't allow URBs to be
  1246. * enqueued for any endpoint on the old config or interface. Nothing
  1247. * else should be touching the xhci->devs[slot_id] structure, so we
  1248. * don't need to take the xhci->lock for manipulating that.
  1249. */
  1250. int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
  1251. {
  1252. int i;
  1253. int ret = 0;
  1254. struct xhci_hcd *xhci;
  1255. struct xhci_virt_device *virt_dev;
  1256. struct xhci_input_control_ctx *ctrl_ctx;
  1257. struct xhci_slot_ctx *slot_ctx;
  1258. ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
  1259. if (ret <= 0)
  1260. return ret;
  1261. xhci = hcd_to_xhci(hcd);
  1262. if (!udev->slot_id || !xhci->devs || !xhci->devs[udev->slot_id]) {
  1263. xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
  1264. __func__);
  1265. return -EINVAL;
  1266. }
  1267. xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
  1268. virt_dev = xhci->devs[udev->slot_id];
  1269. /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
  1270. ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
  1271. ctrl_ctx->add_flags |= SLOT_FLAG;
  1272. ctrl_ctx->add_flags &= ~EP0_FLAG;
  1273. ctrl_ctx->drop_flags &= ~SLOT_FLAG;
  1274. ctrl_ctx->drop_flags &= ~EP0_FLAG;
  1275. xhci_dbg(xhci, "New Input Control Context:\n");
  1276. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
  1277. xhci_dbg_ctx(xhci, virt_dev->in_ctx,
  1278. LAST_CTX_TO_EP_NUM(slot_ctx->dev_info));
  1279. ret = xhci_configure_endpoint(xhci, udev, NULL,
  1280. false, false);
  1281. if (ret) {
  1282. /* Callee should call reset_bandwidth() */
  1283. return ret;
  1284. }
  1285. xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
  1286. xhci_dbg_ctx(xhci, virt_dev->out_ctx,
  1287. LAST_CTX_TO_EP_NUM(slot_ctx->dev_info));
  1288. xhci_zero_in_ctx(xhci, virt_dev);
  1289. /* Install new rings and free or cache any old rings */
  1290. for (i = 1; i < 31; ++i) {
  1291. if (!virt_dev->eps[i].new_ring)
  1292. continue;
  1293. /* Only cache or free the old ring if it exists.
  1294. * It may not if this is the first add of an endpoint.
  1295. */
  1296. if (virt_dev->eps[i].ring) {
  1297. xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
  1298. }
  1299. virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
  1300. virt_dev->eps[i].new_ring = NULL;
  1301. }
  1302. return ret;
  1303. }
  1304. void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
  1305. {
  1306. struct xhci_hcd *xhci;
  1307. struct xhci_virt_device *virt_dev;
  1308. int i, ret;
  1309. ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
  1310. if (ret <= 0)
  1311. return;
  1312. xhci = hcd_to_xhci(hcd);
  1313. if (!xhci->devs || !xhci->devs[udev->slot_id]) {
  1314. xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
  1315. __func__);
  1316. return;
  1317. }
  1318. xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
  1319. virt_dev = xhci->devs[udev->slot_id];
  1320. /* Free any rings allocated for added endpoints */
  1321. for (i = 0; i < 31; ++i) {
  1322. if (virt_dev->eps[i].new_ring) {
  1323. xhci_ring_free(xhci, virt_dev->eps[i].new_ring);
  1324. virt_dev->eps[i].new_ring = NULL;
  1325. }
  1326. }
  1327. xhci_zero_in_ctx(xhci, virt_dev);
  1328. }
  1329. static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci,
  1330. struct xhci_container_ctx *in_ctx,
  1331. struct xhci_container_ctx *out_ctx,
  1332. u32 add_flags, u32 drop_flags)
  1333. {
  1334. struct xhci_input_control_ctx *ctrl_ctx;
  1335. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  1336. ctrl_ctx->add_flags = add_flags;
  1337. ctrl_ctx->drop_flags = drop_flags;
  1338. xhci_slot_copy(xhci, in_ctx, out_ctx);
  1339. ctrl_ctx->add_flags |= SLOT_FLAG;
  1340. xhci_dbg(xhci, "Input Context:\n");
  1341. xhci_dbg_ctx(xhci, in_ctx, xhci_last_valid_endpoint(add_flags));
  1342. }
  1343. void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci,
  1344. unsigned int slot_id, unsigned int ep_index,
  1345. struct xhci_dequeue_state *deq_state)
  1346. {
  1347. struct xhci_container_ctx *in_ctx;
  1348. struct xhci_ep_ctx *ep_ctx;
  1349. u32 added_ctxs;
  1350. dma_addr_t addr;
  1351. xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
  1352. xhci->devs[slot_id]->out_ctx, ep_index);
  1353. in_ctx = xhci->devs[slot_id]->in_ctx;
  1354. ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
  1355. addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
  1356. deq_state->new_deq_ptr);
  1357. if (addr == 0) {
  1358. xhci_warn(xhci, "WARN Cannot submit config ep after "
  1359. "reset ep command\n");
  1360. xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n",
  1361. deq_state->new_deq_seg,
  1362. deq_state->new_deq_ptr);
  1363. return;
  1364. }
  1365. ep_ctx->deq = addr | deq_state->new_cycle_state;
  1366. added_ctxs = xhci_get_endpoint_flag_from_index(ep_index);
  1367. xhci_setup_input_ctx_for_config_ep(xhci, xhci->devs[slot_id]->in_ctx,
  1368. xhci->devs[slot_id]->out_ctx, added_ctxs, added_ctxs);
  1369. }
  1370. void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
  1371. struct usb_device *udev, unsigned int ep_index)
  1372. {
  1373. struct xhci_dequeue_state deq_state;
  1374. struct xhci_virt_ep *ep;
  1375. xhci_dbg(xhci, "Cleaning up stalled endpoint ring\n");
  1376. ep = &xhci->devs[udev->slot_id]->eps[ep_index];
  1377. /* We need to move the HW's dequeue pointer past this TD,
  1378. * or it will attempt to resend it on the next doorbell ring.
  1379. */
  1380. xhci_find_new_dequeue_state(xhci, udev->slot_id,
  1381. ep_index, ep->stopped_stream, ep->stopped_td,
  1382. &deq_state);
  1383. /* HW with the reset endpoint quirk will use the saved dequeue state to
  1384. * issue a configure endpoint command later.
  1385. */
  1386. if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) {
  1387. xhci_dbg(xhci, "Queueing new dequeue state\n");
  1388. xhci_queue_new_dequeue_state(xhci, udev->slot_id,
  1389. ep_index, ep->stopped_stream, &deq_state);
  1390. } else {
  1391. /* Better hope no one uses the input context between now and the
  1392. * reset endpoint completion!
  1393. * XXX: No idea how this hardware will react when stream rings
  1394. * are enabled.
  1395. */
  1396. xhci_dbg(xhci, "Setting up input context for "
  1397. "configure endpoint command\n");
  1398. xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id,
  1399. ep_index, &deq_state);
  1400. }
  1401. }
  1402. /* Deal with stalled endpoints. The core should have sent the control message
  1403. * to clear the halt condition. However, we need to make the xHCI hardware
  1404. * reset its sequence number, since a device will expect a sequence number of
  1405. * zero after the halt condition is cleared.
  1406. * Context: in_interrupt
  1407. */
  1408. void xhci_endpoint_reset(struct usb_hcd *hcd,
  1409. struct usb_host_endpoint *ep)
  1410. {
  1411. struct xhci_hcd *xhci;
  1412. struct usb_device *udev;
  1413. unsigned int ep_index;
  1414. unsigned long flags;
  1415. int ret;
  1416. struct xhci_virt_ep *virt_ep;
  1417. xhci = hcd_to_xhci(hcd);
  1418. udev = (struct usb_device *) ep->hcpriv;
  1419. /* Called with a root hub endpoint (or an endpoint that wasn't added
  1420. * with xhci_add_endpoint()
  1421. */
  1422. if (!ep->hcpriv)
  1423. return;
  1424. ep_index = xhci_get_endpoint_index(&ep->desc);
  1425. virt_ep = &xhci->devs[udev->slot_id]->eps[ep_index];
  1426. if (!virt_ep->stopped_td) {
  1427. xhci_dbg(xhci, "Endpoint 0x%x not halted, refusing to reset.\n",
  1428. ep->desc.bEndpointAddress);
  1429. return;
  1430. }
  1431. if (usb_endpoint_xfer_control(&ep->desc)) {
  1432. xhci_dbg(xhci, "Control endpoint stall already handled.\n");
  1433. return;
  1434. }
  1435. xhci_dbg(xhci, "Queueing reset endpoint command\n");
  1436. spin_lock_irqsave(&xhci->lock, flags);
  1437. ret = xhci_queue_reset_ep(xhci, udev->slot_id, ep_index);
  1438. /*
  1439. * Can't change the ring dequeue pointer until it's transitioned to the
  1440. * stopped state, which is only upon a successful reset endpoint
  1441. * command. Better hope that last command worked!
  1442. */
  1443. if (!ret) {
  1444. xhci_cleanup_stalled_ring(xhci, udev, ep_index);
  1445. kfree(virt_ep->stopped_td);
  1446. xhci_ring_cmd_db(xhci);
  1447. }
  1448. virt_ep->stopped_td = NULL;
  1449. virt_ep->stopped_trb = NULL;
  1450. virt_ep->stopped_stream = 0;
  1451. spin_unlock_irqrestore(&xhci->lock, flags);
  1452. if (ret)
  1453. xhci_warn(xhci, "FIXME allocate a new ring segment\n");
  1454. }
  1455. static int xhci_check_streams_endpoint(struct xhci_hcd *xhci,
  1456. struct usb_device *udev, struct usb_host_endpoint *ep,
  1457. unsigned int slot_id)
  1458. {
  1459. int ret;
  1460. unsigned int ep_index;
  1461. unsigned int ep_state;
  1462. if (!ep)
  1463. return -EINVAL;
  1464. ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, __func__);
  1465. if (ret <= 0)
  1466. return -EINVAL;
  1467. if (ep->ss_ep_comp.bmAttributes == 0) {
  1468. xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion"
  1469. " descriptor for ep 0x%x does not support streams\n",
  1470. ep->desc.bEndpointAddress);
  1471. return -EINVAL;
  1472. }
  1473. ep_index = xhci_get_endpoint_index(&ep->desc);
  1474. ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
  1475. if (ep_state & EP_HAS_STREAMS ||
  1476. ep_state & EP_GETTING_STREAMS) {
  1477. xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x "
  1478. "already has streams set up.\n",
  1479. ep->desc.bEndpointAddress);
  1480. xhci_warn(xhci, "Send email to xHCI maintainer and ask for "
  1481. "dynamic stream context array reallocation.\n");
  1482. return -EINVAL;
  1483. }
  1484. if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) {
  1485. xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk "
  1486. "endpoint 0x%x; URBs are pending.\n",
  1487. ep->desc.bEndpointAddress);
  1488. return -EINVAL;
  1489. }
  1490. return 0;
  1491. }
  1492. static void xhci_calculate_streams_entries(struct xhci_hcd *xhci,
  1493. unsigned int *num_streams, unsigned int *num_stream_ctxs)
  1494. {
  1495. unsigned int max_streams;
  1496. /* The stream context array size must be a power of two */
  1497. *num_stream_ctxs = roundup_pow_of_two(*num_streams);
  1498. /*
  1499. * Find out how many primary stream array entries the host controller
  1500. * supports. Later we may use secondary stream arrays (similar to 2nd
  1501. * level page entries), but that's an optional feature for xHCI host
  1502. * controllers. xHCs must support at least 4 stream IDs.
  1503. */
  1504. max_streams = HCC_MAX_PSA(xhci->hcc_params);
  1505. if (*num_stream_ctxs > max_streams) {
  1506. xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n",
  1507. max_streams);
  1508. *num_stream_ctxs = max_streams;
  1509. *num_streams = max_streams;
  1510. }
  1511. }
  1512. /* Returns an error code if one of the endpoint already has streams.
  1513. * This does not change any data structures, it only checks and gathers
  1514. * information.
  1515. */
  1516. static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci,
  1517. struct usb_device *udev,
  1518. struct usb_host_endpoint **eps, unsigned int num_eps,
  1519. unsigned int *num_streams, u32 *changed_ep_bitmask)
  1520. {
  1521. unsigned int max_streams;
  1522. unsigned int endpoint_flag;
  1523. int i;
  1524. int ret;
  1525. for (i = 0; i < num_eps; i++) {
  1526. ret = xhci_check_streams_endpoint(xhci, udev,
  1527. eps[i], udev->slot_id);
  1528. if (ret < 0)
  1529. return ret;
  1530. max_streams = USB_SS_MAX_STREAMS(
  1531. eps[i]->ss_ep_comp.bmAttributes);
  1532. if (max_streams < (*num_streams - 1)) {
  1533. xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n",
  1534. eps[i]->desc.bEndpointAddress,
  1535. max_streams);
  1536. *num_streams = max_streams+1;
  1537. }
  1538. endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc);
  1539. if (*changed_ep_bitmask & endpoint_flag)
  1540. return -EINVAL;
  1541. *changed_ep_bitmask |= endpoint_flag;
  1542. }
  1543. return 0;
  1544. }
  1545. static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci,
  1546. struct usb_device *udev,
  1547. struct usb_host_endpoint **eps, unsigned int num_eps)
  1548. {
  1549. u32 changed_ep_bitmask = 0;
  1550. unsigned int slot_id;
  1551. unsigned int ep_index;
  1552. unsigned int ep_state;
  1553. int i;
  1554. slot_id = udev->slot_id;
  1555. if (!xhci->devs[slot_id])
  1556. return 0;
  1557. for (i = 0; i < num_eps; i++) {
  1558. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  1559. ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
  1560. /* Are streams already being freed for the endpoint? */
  1561. if (ep_state & EP_GETTING_NO_STREAMS) {
  1562. xhci_warn(xhci, "WARN Can't disable streams for "
  1563. "endpoint 0x%x\n, "
  1564. "streams are being disabled already.",
  1565. eps[i]->desc.bEndpointAddress);
  1566. return 0;
  1567. }
  1568. /* Are there actually any streams to free? */
  1569. if (!(ep_state & EP_HAS_STREAMS) &&
  1570. !(ep_state & EP_GETTING_STREAMS)) {
  1571. xhci_warn(xhci, "WARN Can't disable streams for "
  1572. "endpoint 0x%x\n, "
  1573. "streams are already disabled!",
  1574. eps[i]->desc.bEndpointAddress);
  1575. xhci_warn(xhci, "WARN xhci_free_streams() called "
  1576. "with non-streams endpoint\n");
  1577. return 0;
  1578. }
  1579. changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc);
  1580. }
  1581. return changed_ep_bitmask;
  1582. }
  1583. /*
  1584. * The USB device drivers use this function (though the HCD interface in USB
  1585. * core) to prepare a set of bulk endpoints to use streams. Streams are used to
  1586. * coordinate mass storage command queueing across multiple endpoints (basically
  1587. * a stream ID == a task ID).
  1588. *
  1589. * Setting up streams involves allocating the same size stream context array
  1590. * for each endpoint and issuing a configure endpoint command for all endpoints.
  1591. *
  1592. * Don't allow the call to succeed if one endpoint only supports one stream
  1593. * (which means it doesn't support streams at all).
  1594. *
  1595. * Drivers may get less stream IDs than they asked for, if the host controller
  1596. * hardware or endpoints claim they can't support the number of requested
  1597. * stream IDs.
  1598. */
  1599. int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
  1600. struct usb_host_endpoint **eps, unsigned int num_eps,
  1601. unsigned int num_streams, gfp_t mem_flags)
  1602. {
  1603. int i, ret;
  1604. struct xhci_hcd *xhci;
  1605. struct xhci_virt_device *vdev;
  1606. struct xhci_command *config_cmd;
  1607. unsigned int ep_index;
  1608. unsigned int num_stream_ctxs;
  1609. unsigned long flags;
  1610. u32 changed_ep_bitmask = 0;
  1611. if (!eps)
  1612. return -EINVAL;
  1613. /* Add one to the number of streams requested to account for
  1614. * stream 0 that is reserved for xHCI usage.
  1615. */
  1616. num_streams += 1;
  1617. xhci = hcd_to_xhci(hcd);
  1618. xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n",
  1619. num_streams);
  1620. config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
  1621. if (!config_cmd) {
  1622. xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
  1623. return -ENOMEM;
  1624. }
  1625. /* Check to make sure all endpoints are not already configured for
  1626. * streams. While we're at it, find the maximum number of streams that
  1627. * all the endpoints will support and check for duplicate endpoints.
  1628. */
  1629. spin_lock_irqsave(&xhci->lock, flags);
  1630. ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps,
  1631. num_eps, &num_streams, &changed_ep_bitmask);
  1632. if (ret < 0) {
  1633. xhci_free_command(xhci, config_cmd);
  1634. spin_unlock_irqrestore(&xhci->lock, flags);
  1635. return ret;
  1636. }
  1637. if (num_streams <= 1) {
  1638. xhci_warn(xhci, "WARN: endpoints can't handle "
  1639. "more than one stream.\n");
  1640. xhci_free_command(xhci, config_cmd);
  1641. spin_unlock_irqrestore(&xhci->lock, flags);
  1642. return -EINVAL;
  1643. }
  1644. vdev = xhci->devs[udev->slot_id];
  1645. /* Mark each endpoint as being in transistion, so
  1646. * xhci_urb_enqueue() will reject all URBs.
  1647. */
  1648. for (i = 0; i < num_eps; i++) {
  1649. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  1650. vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS;
  1651. }
  1652. spin_unlock_irqrestore(&xhci->lock, flags);
  1653. /* Setup internal data structures and allocate HW data structures for
  1654. * streams (but don't install the HW structures in the input context
  1655. * until we're sure all memory allocation succeeded).
  1656. */
  1657. xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs);
  1658. xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n",
  1659. num_stream_ctxs, num_streams);
  1660. for (i = 0; i < num_eps; i++) {
  1661. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  1662. vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci,
  1663. num_stream_ctxs,
  1664. num_streams, mem_flags);
  1665. if (!vdev->eps[ep_index].stream_info)
  1666. goto cleanup;
  1667. /* Set maxPstreams in endpoint context and update deq ptr to
  1668. * point to stream context array. FIXME
  1669. */
  1670. }
  1671. /* Set up the input context for a configure endpoint command. */
  1672. for (i = 0; i < num_eps; i++) {
  1673. struct xhci_ep_ctx *ep_ctx;
  1674. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  1675. ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index);
  1676. xhci_endpoint_copy(xhci, config_cmd->in_ctx,
  1677. vdev->out_ctx, ep_index);
  1678. xhci_setup_streams_ep_input_ctx(xhci, ep_ctx,
  1679. vdev->eps[ep_index].stream_info);
  1680. }
  1681. /* Tell the HW to drop its old copy of the endpoint context info
  1682. * and add the updated copy from the input context.
  1683. */
  1684. xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx,
  1685. vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
  1686. /* Issue and wait for the configure endpoint command */
  1687. ret = xhci_configure_endpoint(xhci, udev, config_cmd,
  1688. false, false);
  1689. /* xHC rejected the configure endpoint command for some reason, so we
  1690. * leave the old ring intact and free our internal streams data
  1691. * structure.
  1692. */
  1693. if (ret < 0)
  1694. goto cleanup;
  1695. spin_lock_irqsave(&xhci->lock, flags);
  1696. for (i = 0; i < num_eps; i++) {
  1697. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  1698. vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
  1699. xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n",
  1700. udev->slot_id, ep_index);
  1701. vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS;
  1702. }
  1703. xhci_free_command(xhci, config_cmd);
  1704. spin_unlock_irqrestore(&xhci->lock, flags);
  1705. /* Subtract 1 for stream 0, which drivers can't use */
  1706. return num_streams - 1;
  1707. cleanup:
  1708. /* If it didn't work, free the streams! */
  1709. for (i = 0; i < num_eps; i++) {
  1710. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  1711. xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
  1712. vdev->eps[ep_index].stream_info = NULL;
  1713. /* FIXME Unset maxPstreams in endpoint context and
  1714. * update deq ptr to point to normal string ring.
  1715. */
  1716. vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
  1717. vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
  1718. xhci_endpoint_zero(xhci, vdev, eps[i]);
  1719. }
  1720. xhci_free_command(xhci, config_cmd);
  1721. return -ENOMEM;
  1722. }
  1723. /* Transition the endpoint from using streams to being a "normal" endpoint
  1724. * without streams.
  1725. *
  1726. * Modify the endpoint context state, submit a configure endpoint command,
  1727. * and free all endpoint rings for streams if that completes successfully.
  1728. */
  1729. int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
  1730. struct usb_host_endpoint **eps, unsigned int num_eps,
  1731. gfp_t mem_flags)
  1732. {
  1733. int i, ret;
  1734. struct xhci_hcd *xhci;
  1735. struct xhci_virt_device *vdev;
  1736. struct xhci_command *command;
  1737. unsigned int ep_index;
  1738. unsigned long flags;
  1739. u32 changed_ep_bitmask;
  1740. xhci = hcd_to_xhci(hcd);
  1741. vdev = xhci->devs[udev->slot_id];
  1742. /* Set up a configure endpoint command to remove the streams rings */
  1743. spin_lock_irqsave(&xhci->lock, flags);
  1744. changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci,
  1745. udev, eps, num_eps);
  1746. if (changed_ep_bitmask == 0) {
  1747. spin_unlock_irqrestore(&xhci->lock, flags);
  1748. return -EINVAL;
  1749. }
  1750. /* Use the xhci_command structure from the first endpoint. We may have
  1751. * allocated too many, but the driver may call xhci_free_streams() for
  1752. * each endpoint it grouped into one call to xhci_alloc_streams().
  1753. */
  1754. ep_index = xhci_get_endpoint_index(&eps[0]->desc);
  1755. command = vdev->eps[ep_index].stream_info->free_streams_command;
  1756. for (i = 0; i < num_eps; i++) {
  1757. struct xhci_ep_ctx *ep_ctx;
  1758. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  1759. ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
  1760. xhci->devs[udev->slot_id]->eps[ep_index].ep_state |=
  1761. EP_GETTING_NO_STREAMS;
  1762. xhci_endpoint_copy(xhci, command->in_ctx,
  1763. vdev->out_ctx, ep_index);
  1764. xhci_setup_no_streams_ep_input_ctx(xhci, ep_ctx,
  1765. &vdev->eps[ep_index]);
  1766. }
  1767. xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx,
  1768. vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
  1769. spin_unlock_irqrestore(&xhci->lock, flags);
  1770. /* Issue and wait for the configure endpoint command,
  1771. * which must succeed.
  1772. */
  1773. ret = xhci_configure_endpoint(xhci, udev, command,
  1774. false, true);
  1775. /* xHC rejected the configure endpoint command for some reason, so we
  1776. * leave the streams rings intact.
  1777. */
  1778. if (ret < 0)
  1779. return ret;
  1780. spin_lock_irqsave(&xhci->lock, flags);
  1781. for (i = 0; i < num_eps; i++) {
  1782. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  1783. xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
  1784. vdev->eps[ep_index].stream_info = NULL;
  1785. /* FIXME Unset maxPstreams in endpoint context and
  1786. * update deq ptr to point to normal string ring.
  1787. */
  1788. vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS;
  1789. vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
  1790. }
  1791. spin_unlock_irqrestore(&xhci->lock, flags);
  1792. return 0;
  1793. }
  1794. /*
  1795. * This submits a Reset Device Command, which will set the device state to 0,
  1796. * set the device address to 0, and disable all the endpoints except the default
  1797. * control endpoint. The USB core should come back and call
  1798. * xhci_address_device(), and then re-set up the configuration. If this is
  1799. * called because of a usb_reset_and_verify_device(), then the old alternate
  1800. * settings will be re-installed through the normal bandwidth allocation
  1801. * functions.
  1802. *
  1803. * Wait for the Reset Device command to finish. Remove all structures
  1804. * associated with the endpoints that were disabled. Clear the input device
  1805. * structure? Cache the rings? Reset the control endpoint 0 max packet size?
  1806. */
  1807. int xhci_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
  1808. {
  1809. int ret, i;
  1810. unsigned long flags;
  1811. struct xhci_hcd *xhci;
  1812. unsigned int slot_id;
  1813. struct xhci_virt_device *virt_dev;
  1814. struct xhci_command *reset_device_cmd;
  1815. int timeleft;
  1816. int last_freed_endpoint;
  1817. ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
  1818. if (ret <= 0)
  1819. return ret;
  1820. xhci = hcd_to_xhci(hcd);
  1821. slot_id = udev->slot_id;
  1822. virt_dev = xhci->devs[slot_id];
  1823. if (!virt_dev) {
  1824. xhci_dbg(xhci, "%s called with invalid slot ID %u\n",
  1825. __func__, slot_id);
  1826. return -EINVAL;
  1827. }
  1828. xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id);
  1829. /* Allocate the command structure that holds the struct completion.
  1830. * Assume we're in process context, since the normal device reset
  1831. * process has to wait for the device anyway. Storage devices are
  1832. * reset as part of error handling, so use GFP_NOIO instead of
  1833. * GFP_KERNEL.
  1834. */
  1835. reset_device_cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
  1836. if (!reset_device_cmd) {
  1837. xhci_dbg(xhci, "Couldn't allocate command structure.\n");
  1838. return -ENOMEM;
  1839. }
  1840. /* Attempt to submit the Reset Device command to the command ring */
  1841. spin_lock_irqsave(&xhci->lock, flags);
  1842. reset_device_cmd->command_trb = xhci->cmd_ring->enqueue;
  1843. list_add_tail(&reset_device_cmd->cmd_list, &virt_dev->cmd_list);
  1844. ret = xhci_queue_reset_device(xhci, slot_id);
  1845. if (ret) {
  1846. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  1847. list_del(&reset_device_cmd->cmd_list);
  1848. spin_unlock_irqrestore(&xhci->lock, flags);
  1849. goto command_cleanup;
  1850. }
  1851. xhci_ring_cmd_db(xhci);
  1852. spin_unlock_irqrestore(&xhci->lock, flags);
  1853. /* Wait for the Reset Device command to finish */
  1854. timeleft = wait_for_completion_interruptible_timeout(
  1855. reset_device_cmd->completion,
  1856. USB_CTRL_SET_TIMEOUT);
  1857. if (timeleft <= 0) {
  1858. xhci_warn(xhci, "%s while waiting for reset device command\n",
  1859. timeleft == 0 ? "Timeout" : "Signal");
  1860. spin_lock_irqsave(&xhci->lock, flags);
  1861. /* The timeout might have raced with the event ring handler, so
  1862. * only delete from the list if the item isn't poisoned.
  1863. */
  1864. if (reset_device_cmd->cmd_list.next != LIST_POISON1)
  1865. list_del(&reset_device_cmd->cmd_list);
  1866. spin_unlock_irqrestore(&xhci->lock, flags);
  1867. ret = -ETIME;
  1868. goto command_cleanup;
  1869. }
  1870. /* The Reset Device command can't fail, according to the 0.95/0.96 spec,
  1871. * unless we tried to reset a slot ID that wasn't enabled,
  1872. * or the device wasn't in the addressed or configured state.
  1873. */
  1874. ret = reset_device_cmd->status;
  1875. switch (ret) {
  1876. case COMP_EBADSLT: /* 0.95 completion code for bad slot ID */
  1877. case COMP_CTX_STATE: /* 0.96 completion code for same thing */
  1878. xhci_info(xhci, "Can't reset device (slot ID %u) in %s state\n",
  1879. slot_id,
  1880. xhci_get_slot_state(xhci, virt_dev->out_ctx));
  1881. xhci_info(xhci, "Not freeing device rings.\n");
  1882. /* Don't treat this as an error. May change my mind later. */
  1883. ret = 0;
  1884. goto command_cleanup;
  1885. case COMP_SUCCESS:
  1886. xhci_dbg(xhci, "Successful reset device command.\n");
  1887. break;
  1888. default:
  1889. if (xhci_is_vendor_info_code(xhci, ret))
  1890. break;
  1891. xhci_warn(xhci, "Unknown completion code %u for "
  1892. "reset device command.\n", ret);
  1893. ret = -EINVAL;
  1894. goto command_cleanup;
  1895. }
  1896. /* Everything but endpoint 0 is disabled, so free or cache the rings. */
  1897. last_freed_endpoint = 1;
  1898. for (i = 1; i < 31; ++i) {
  1899. if (!virt_dev->eps[i].ring)
  1900. continue;
  1901. xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
  1902. last_freed_endpoint = i;
  1903. }
  1904. xhci_dbg(xhci, "Output context after successful reset device cmd:\n");
  1905. xhci_dbg_ctx(xhci, virt_dev->out_ctx, last_freed_endpoint);
  1906. ret = 0;
  1907. command_cleanup:
  1908. xhci_free_command(xhci, reset_device_cmd);
  1909. return ret;
  1910. }
  1911. /*
  1912. * At this point, the struct usb_device is about to go away, the device has
  1913. * disconnected, and all traffic has been stopped and the endpoints have been
  1914. * disabled. Free any HC data structures associated with that device.
  1915. */
  1916. void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
  1917. {
  1918. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1919. struct xhci_virt_device *virt_dev;
  1920. unsigned long flags;
  1921. u32 state;
  1922. int i;
  1923. if (udev->slot_id == 0)
  1924. return;
  1925. virt_dev = xhci->devs[udev->slot_id];
  1926. if (!virt_dev)
  1927. return;
  1928. /* Stop any wayward timer functions (which may grab the lock) */
  1929. for (i = 0; i < 31; ++i) {
  1930. virt_dev->eps[i].ep_state &= ~EP_HALT_PENDING;
  1931. del_timer_sync(&virt_dev->eps[i].stop_cmd_timer);
  1932. }
  1933. spin_lock_irqsave(&xhci->lock, flags);
  1934. /* Don't disable the slot if the host controller is dead. */
  1935. state = xhci_readl(xhci, &xhci->op_regs->status);
  1936. if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING)) {
  1937. xhci_free_virt_device(xhci, udev->slot_id);
  1938. spin_unlock_irqrestore(&xhci->lock, flags);
  1939. return;
  1940. }
  1941. if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
  1942. spin_unlock_irqrestore(&xhci->lock, flags);
  1943. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  1944. return;
  1945. }
  1946. xhci_ring_cmd_db(xhci);
  1947. spin_unlock_irqrestore(&xhci->lock, flags);
  1948. /*
  1949. * Event command completion handler will free any data structures
  1950. * associated with the slot. XXX Can free sleep?
  1951. */
  1952. }
  1953. /*
  1954. * Returns 0 if the xHC ran out of device slots, the Enable Slot command
  1955. * timed out, or allocating memory failed. Returns 1 on success.
  1956. */
  1957. int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
  1958. {
  1959. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1960. unsigned long flags;
  1961. int timeleft;
  1962. int ret;
  1963. spin_lock_irqsave(&xhci->lock, flags);
  1964. ret = xhci_queue_slot_control(xhci, TRB_ENABLE_SLOT, 0);
  1965. if (ret) {
  1966. spin_unlock_irqrestore(&xhci->lock, flags);
  1967. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  1968. return 0;
  1969. }
  1970. xhci_ring_cmd_db(xhci);
  1971. spin_unlock_irqrestore(&xhci->lock, flags);
  1972. /* XXX: how much time for xHC slot assignment? */
  1973. timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
  1974. USB_CTRL_SET_TIMEOUT);
  1975. if (timeleft <= 0) {
  1976. xhci_warn(xhci, "%s while waiting for a slot\n",
  1977. timeleft == 0 ? "Timeout" : "Signal");
  1978. /* FIXME cancel the enable slot request */
  1979. return 0;
  1980. }
  1981. if (!xhci->slot_id) {
  1982. xhci_err(xhci, "Error while assigning device slot ID\n");
  1983. return 0;
  1984. }
  1985. /* xhci_alloc_virt_device() does not touch rings; no need to lock */
  1986. if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_KERNEL)) {
  1987. /* Disable slot, if we can do it without mem alloc */
  1988. xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
  1989. spin_lock_irqsave(&xhci->lock, flags);
  1990. if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
  1991. xhci_ring_cmd_db(xhci);
  1992. spin_unlock_irqrestore(&xhci->lock, flags);
  1993. return 0;
  1994. }
  1995. udev->slot_id = xhci->slot_id;
  1996. /* Is this a LS or FS device under a HS hub? */
  1997. /* Hub or peripherial? */
  1998. return 1;
  1999. }
  2000. /*
  2001. * Issue an Address Device command (which will issue a SetAddress request to
  2002. * the device).
  2003. * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so
  2004. * we should only issue and wait on one address command at the same time.
  2005. *
  2006. * We add one to the device address issued by the hardware because the USB core
  2007. * uses address 1 for the root hubs (even though they're not really devices).
  2008. */
  2009. int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
  2010. {
  2011. unsigned long flags;
  2012. int timeleft;
  2013. struct xhci_virt_device *virt_dev;
  2014. int ret = 0;
  2015. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  2016. struct xhci_slot_ctx *slot_ctx;
  2017. struct xhci_input_control_ctx *ctrl_ctx;
  2018. u64 temp_64;
  2019. if (!udev->slot_id) {
  2020. xhci_dbg(xhci, "Bad Slot ID %d\n", udev->slot_id);
  2021. return -EINVAL;
  2022. }
  2023. virt_dev = xhci->devs[udev->slot_id];
  2024. /* If this is a Set Address to an unconfigured device, setup ep 0 */
  2025. if (!udev->config)
  2026. xhci_setup_addressable_virt_dev(xhci, udev);
  2027. else
  2028. xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev);
  2029. /* Otherwise, assume the core has the device configured how it wants */
  2030. xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
  2031. xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
  2032. spin_lock_irqsave(&xhci->lock, flags);
  2033. ret = xhci_queue_address_device(xhci, virt_dev->in_ctx->dma,
  2034. udev->slot_id);
  2035. if (ret) {
  2036. spin_unlock_irqrestore(&xhci->lock, flags);
  2037. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  2038. return ret;
  2039. }
  2040. xhci_ring_cmd_db(xhci);
  2041. spin_unlock_irqrestore(&xhci->lock, flags);
  2042. /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
  2043. timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
  2044. USB_CTRL_SET_TIMEOUT);
  2045. /* FIXME: From section 4.3.4: "Software shall be responsible for timing
  2046. * the SetAddress() "recovery interval" required by USB and aborting the
  2047. * command on a timeout.
  2048. */
  2049. if (timeleft <= 0) {
  2050. xhci_warn(xhci, "%s while waiting for a slot\n",
  2051. timeleft == 0 ? "Timeout" : "Signal");
  2052. /* FIXME cancel the address device command */
  2053. return -ETIME;
  2054. }
  2055. switch (virt_dev->cmd_status) {
  2056. case COMP_CTX_STATE:
  2057. case COMP_EBADSLT:
  2058. xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n",
  2059. udev->slot_id);
  2060. ret = -EINVAL;
  2061. break;
  2062. case COMP_TX_ERR:
  2063. dev_warn(&udev->dev, "Device not responding to set address.\n");
  2064. ret = -EPROTO;
  2065. break;
  2066. case COMP_SUCCESS:
  2067. xhci_dbg(xhci, "Successful Address Device command\n");
  2068. break;
  2069. default:
  2070. xhci_err(xhci, "ERROR: unexpected command completion "
  2071. "code 0x%x.\n", virt_dev->cmd_status);
  2072. xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
  2073. xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
  2074. ret = -EINVAL;
  2075. break;
  2076. }
  2077. if (ret) {
  2078. return ret;
  2079. }
  2080. temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
  2081. xhci_dbg(xhci, "Op regs DCBAA ptr = %#016llx\n", temp_64);
  2082. xhci_dbg(xhci, "Slot ID %d dcbaa entry @%p = %#016llx\n",
  2083. udev->slot_id,
  2084. &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
  2085. (unsigned long long)
  2086. xhci->dcbaa->dev_context_ptrs[udev->slot_id]);
  2087. xhci_dbg(xhci, "Output Context DMA address = %#08llx\n",
  2088. (unsigned long long)virt_dev->out_ctx->dma);
  2089. xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
  2090. xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
  2091. xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
  2092. xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
  2093. /*
  2094. * USB core uses address 1 for the roothubs, so we add one to the
  2095. * address given back to us by the HC.
  2096. */
  2097. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
  2098. udev->devnum = (slot_ctx->dev_state & DEV_ADDR_MASK) + 1;
  2099. /* Zero the input context control for later use */
  2100. ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
  2101. ctrl_ctx->add_flags = 0;
  2102. ctrl_ctx->drop_flags = 0;
  2103. xhci_dbg(xhci, "Device address = %d\n", udev->devnum);
  2104. /* XXX Meh, not sure if anyone else but choose_address uses this. */
  2105. set_bit(udev->devnum, udev->bus->devmap.devicemap);
  2106. return 0;
  2107. }
  2108. /* Once a hub descriptor is fetched for a device, we need to update the xHC's
  2109. * internal data structures for the device.
  2110. */
  2111. int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
  2112. struct usb_tt *tt, gfp_t mem_flags)
  2113. {
  2114. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  2115. struct xhci_virt_device *vdev;
  2116. struct xhci_command *config_cmd;
  2117. struct xhci_input_control_ctx *ctrl_ctx;
  2118. struct xhci_slot_ctx *slot_ctx;
  2119. unsigned long flags;
  2120. unsigned think_time;
  2121. int ret;
  2122. /* Ignore root hubs */
  2123. if (!hdev->parent)
  2124. return 0;
  2125. vdev = xhci->devs[hdev->slot_id];
  2126. if (!vdev) {
  2127. xhci_warn(xhci, "Cannot update hub desc for unknown device.\n");
  2128. return -EINVAL;
  2129. }
  2130. config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
  2131. if (!config_cmd) {
  2132. xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
  2133. return -ENOMEM;
  2134. }
  2135. spin_lock_irqsave(&xhci->lock, flags);
  2136. xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx);
  2137. ctrl_ctx = xhci_get_input_control_ctx(xhci, config_cmd->in_ctx);
  2138. ctrl_ctx->add_flags |= SLOT_FLAG;
  2139. slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx);
  2140. slot_ctx->dev_info |= DEV_HUB;
  2141. if (tt->multi)
  2142. slot_ctx->dev_info |= DEV_MTT;
  2143. if (xhci->hci_version > 0x95) {
  2144. xhci_dbg(xhci, "xHCI version %x needs hub "
  2145. "TT think time and number of ports\n",
  2146. (unsigned int) xhci->hci_version);
  2147. slot_ctx->dev_info2 |= XHCI_MAX_PORTS(hdev->maxchild);
  2148. /* Set TT think time - convert from ns to FS bit times.
  2149. * 0 = 8 FS bit times, 1 = 16 FS bit times,
  2150. * 2 = 24 FS bit times, 3 = 32 FS bit times.
  2151. */
  2152. think_time = tt->think_time;
  2153. if (think_time != 0)
  2154. think_time = (think_time / 666) - 1;
  2155. slot_ctx->tt_info |= TT_THINK_TIME(think_time);
  2156. } else {
  2157. xhci_dbg(xhci, "xHCI version %x doesn't need hub "
  2158. "TT think time or number of ports\n",
  2159. (unsigned int) xhci->hci_version);
  2160. }
  2161. slot_ctx->dev_state = 0;
  2162. spin_unlock_irqrestore(&xhci->lock, flags);
  2163. xhci_dbg(xhci, "Set up %s for hub device.\n",
  2164. (xhci->hci_version > 0x95) ?
  2165. "configure endpoint" : "evaluate context");
  2166. xhci_dbg(xhci, "Slot %u Input Context:\n", hdev->slot_id);
  2167. xhci_dbg_ctx(xhci, config_cmd->in_ctx, 0);
  2168. /* Issue and wait for the configure endpoint or
  2169. * evaluate context command.
  2170. */
  2171. if (xhci->hci_version > 0x95)
  2172. ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
  2173. false, false);
  2174. else
  2175. ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
  2176. true, false);
  2177. xhci_dbg(xhci, "Slot %u Output Context:\n", hdev->slot_id);
  2178. xhci_dbg_ctx(xhci, vdev->out_ctx, 0);
  2179. xhci_free_command(xhci, config_cmd);
  2180. return ret;
  2181. }
  2182. int xhci_get_frame(struct usb_hcd *hcd)
  2183. {
  2184. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  2185. /* EHCI mods by the periodic size. Why? */
  2186. return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
  2187. }
  2188. MODULE_DESCRIPTION(DRIVER_DESC);
  2189. MODULE_AUTHOR(DRIVER_AUTHOR);
  2190. MODULE_LICENSE("GPL");
  2191. static int __init xhci_hcd_init(void)
  2192. {
  2193. #ifdef CONFIG_PCI
  2194. int retval = 0;
  2195. retval = xhci_register_pci();
  2196. if (retval < 0) {
  2197. printk(KERN_DEBUG "Problem registering PCI driver.");
  2198. return retval;
  2199. }
  2200. #endif
  2201. /*
  2202. * Check the compiler generated sizes of structures that must be laid
  2203. * out in specific ways for hardware access.
  2204. */
  2205. BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
  2206. BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
  2207. BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
  2208. /* xhci_device_control has eight fields, and also
  2209. * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
  2210. */
  2211. BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
  2212. BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
  2213. BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
  2214. BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 7*32/8);
  2215. BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
  2216. /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
  2217. BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
  2218. BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
  2219. return 0;
  2220. }
  2221. module_init(xhci_hcd_init);
  2222. static void __exit xhci_hcd_cleanup(void)
  2223. {
  2224. #ifdef CONFIG_PCI
  2225. xhci_unregister_pci();
  2226. #endif
  2227. }
  2228. module_exit(xhci_hcd_cleanup);