xhci.c 73 KB

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