xhci-hcd.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  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/module.h>
  24. #include "xhci.h"
  25. #define DRIVER_AUTHOR "Sarah Sharp"
  26. #define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
  27. /* TODO: copied from ehci-hcd.c - can this be refactored? */
  28. /*
  29. * handshake - spin reading hc until handshake completes or fails
  30. * @ptr: address of hc register to be read
  31. * @mask: bits to look at in result of read
  32. * @done: value of those bits when handshake succeeds
  33. * @usec: timeout in microseconds
  34. *
  35. * Returns negative errno, or zero on success
  36. *
  37. * Success happens when the "mask" bits have the specified value (hardware
  38. * handshake done). There are two failure modes: "usec" have passed (major
  39. * hardware flakeout), or the register reads as all-ones (hardware removed).
  40. */
  41. static int handshake(struct xhci_hcd *xhci, void __iomem *ptr,
  42. u32 mask, u32 done, int usec)
  43. {
  44. u32 result;
  45. do {
  46. result = xhci_readl(xhci, ptr);
  47. if (result == ~(u32)0) /* card removed */
  48. return -ENODEV;
  49. result &= mask;
  50. if (result == done)
  51. return 0;
  52. udelay(1);
  53. usec--;
  54. } while (usec > 0);
  55. return -ETIMEDOUT;
  56. }
  57. /*
  58. * Force HC into halt state.
  59. *
  60. * Disable any IRQs and clear the run/stop bit.
  61. * HC will complete any current and actively pipelined transactions, and
  62. * should halt within 16 microframes of the run/stop bit being cleared.
  63. * Read HC Halted bit in the status register to see when the HC is finished.
  64. * XXX: shouldn't we set HC_STATE_HALT here somewhere?
  65. */
  66. int xhci_halt(struct xhci_hcd *xhci)
  67. {
  68. u32 halted;
  69. u32 cmd;
  70. u32 mask;
  71. xhci_dbg(xhci, "// Halt the HC\n");
  72. /* Disable all interrupts from the host controller */
  73. mask = ~(XHCI_IRQS);
  74. halted = xhci_readl(xhci, &xhci->op_regs->status) & STS_HALT;
  75. if (!halted)
  76. mask &= ~CMD_RUN;
  77. cmd = xhci_readl(xhci, &xhci->op_regs->command);
  78. cmd &= mask;
  79. xhci_writel(xhci, cmd, &xhci->op_regs->command);
  80. return handshake(xhci, &xhci->op_regs->status,
  81. STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
  82. }
  83. /*
  84. * Reset a halted HC, and set the internal HC state to HC_STATE_HALT.
  85. *
  86. * This resets pipelines, timers, counters, state machines, etc.
  87. * Transactions will be terminated immediately, and operational registers
  88. * will be set to their defaults.
  89. */
  90. int xhci_reset(struct xhci_hcd *xhci)
  91. {
  92. u32 command;
  93. u32 state;
  94. state = xhci_readl(xhci, &xhci->op_regs->status);
  95. BUG_ON((state & STS_HALT) == 0);
  96. xhci_dbg(xhci, "// Reset the HC\n");
  97. command = xhci_readl(xhci, &xhci->op_regs->command);
  98. command |= CMD_RESET;
  99. xhci_writel(xhci, command, &xhci->op_regs->command);
  100. /* XXX: Why does EHCI set this here? Shouldn't other code do this? */
  101. xhci_to_hcd(xhci)->state = HC_STATE_HALT;
  102. return handshake(xhci, &xhci->op_regs->command, CMD_RESET, 0, 250 * 1000);
  103. }
  104. /*
  105. * Stop the HC from processing the endpoint queues.
  106. */
  107. static void xhci_quiesce(struct xhci_hcd *xhci)
  108. {
  109. /*
  110. * Queues are per endpoint, so we need to disable an endpoint or slot.
  111. *
  112. * To disable a slot, we need to insert a disable slot command on the
  113. * command ring and ring the doorbell. This will also free any internal
  114. * resources associated with the slot (which might not be what we want).
  115. *
  116. * A Release Endpoint command sounds better - doesn't free internal HC
  117. * memory, but removes the endpoints from the schedule and releases the
  118. * bandwidth, disables the doorbells, and clears the endpoint enable
  119. * flag. Usually used prior to a set interface command.
  120. *
  121. * TODO: Implement after command ring code is done.
  122. */
  123. BUG_ON(!HC_IS_RUNNING(xhci_to_hcd(xhci)->state));
  124. xhci_dbg(xhci, "Finished quiescing -- code not written yet\n");
  125. }
  126. #if 0
  127. /* Set up MSI-X table for entry 0 (may claim other entries later) */
  128. static int xhci_setup_msix(struct xhci_hcd *xhci)
  129. {
  130. int ret;
  131. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  132. xhci->msix_count = 0;
  133. /* XXX: did I do this right? ixgbe does kcalloc for more than one */
  134. xhci->msix_entries = kmalloc(sizeof(struct msix_entry), GFP_KERNEL);
  135. if (!xhci->msix_entries) {
  136. xhci_err(xhci, "Failed to allocate MSI-X entries\n");
  137. return -ENOMEM;
  138. }
  139. xhci->msix_entries[0].entry = 0;
  140. ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count);
  141. if (ret) {
  142. xhci_err(xhci, "Failed to enable MSI-X\n");
  143. goto free_entries;
  144. }
  145. /*
  146. * Pass the xhci pointer value as the request_irq "cookie".
  147. * If more irqs are added, this will need to be unique for each one.
  148. */
  149. ret = request_irq(xhci->msix_entries[0].vector, &xhci_irq, 0,
  150. "xHCI", xhci_to_hcd(xhci));
  151. if (ret) {
  152. xhci_err(xhci, "Failed to allocate MSI-X interrupt\n");
  153. goto disable_msix;
  154. }
  155. xhci_dbg(xhci, "Finished setting up MSI-X\n");
  156. return 0;
  157. disable_msix:
  158. pci_disable_msix(pdev);
  159. free_entries:
  160. kfree(xhci->msix_entries);
  161. xhci->msix_entries = NULL;
  162. return ret;
  163. }
  164. /* XXX: code duplication; can xhci_setup_msix call this? */
  165. /* Free any IRQs and disable MSI-X */
  166. static void xhci_cleanup_msix(struct xhci_hcd *xhci)
  167. {
  168. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  169. if (!xhci->msix_entries)
  170. return;
  171. free_irq(xhci->msix_entries[0].vector, xhci);
  172. pci_disable_msix(pdev);
  173. kfree(xhci->msix_entries);
  174. xhci->msix_entries = NULL;
  175. xhci_dbg(xhci, "Finished cleaning up MSI-X\n");
  176. }
  177. #endif
  178. /*
  179. * Initialize memory for HCD and xHC (one-time init).
  180. *
  181. * Program the PAGESIZE register, initialize the device context array, create
  182. * device contexts (?), set up a command ring segment (or two?), create event
  183. * ring (one for now).
  184. */
  185. int xhci_init(struct usb_hcd *hcd)
  186. {
  187. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  188. int retval = 0;
  189. xhci_dbg(xhci, "xhci_init\n");
  190. spin_lock_init(&xhci->lock);
  191. retval = xhci_mem_init(xhci, GFP_KERNEL);
  192. xhci_dbg(xhci, "Finished xhci_init\n");
  193. return retval;
  194. }
  195. /*
  196. * Called in interrupt context when there might be work
  197. * queued on the event ring
  198. *
  199. * xhci->lock must be held by caller.
  200. */
  201. static void xhci_work(struct xhci_hcd *xhci)
  202. {
  203. u32 temp;
  204. /*
  205. * Clear the op reg interrupt status first,
  206. * so we can receive interrupts from other MSI-X interrupters.
  207. * Write 1 to clear the interrupt status.
  208. */
  209. temp = xhci_readl(xhci, &xhci->op_regs->status);
  210. temp |= STS_EINT;
  211. xhci_writel(xhci, temp, &xhci->op_regs->status);
  212. /* FIXME when MSI-X is supported and there are multiple vectors */
  213. /* Clear the MSI-X event interrupt status */
  214. /* Acknowledge the interrupt */
  215. temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  216. temp |= 0x3;
  217. xhci_writel(xhci, temp, &xhci->ir_set->irq_pending);
  218. /* Flush posted writes */
  219. xhci_readl(xhci, &xhci->ir_set->irq_pending);
  220. /* FIXME this should be a delayed service routine that clears the EHB */
  221. xhci_handle_event(xhci);
  222. /* Clear the event handler busy flag; the event ring should be empty. */
  223. temp = xhci_readl(xhci, &xhci->ir_set->erst_dequeue[0]);
  224. xhci_writel(xhci, temp & ~ERST_EHB, &xhci->ir_set->erst_dequeue[0]);
  225. /* Flush posted writes -- FIXME is this necessary? */
  226. xhci_readl(xhci, &xhci->ir_set->irq_pending);
  227. }
  228. /*-------------------------------------------------------------------------*/
  229. /*
  230. * xHCI spec says we can get an interrupt, and if the HC has an error condition,
  231. * we might get bad data out of the event ring. Section 4.10.2.7 has a list of
  232. * indicators of an event TRB error, but we check the status *first* to be safe.
  233. */
  234. irqreturn_t xhci_irq(struct usb_hcd *hcd)
  235. {
  236. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  237. u32 temp, temp2;
  238. spin_lock(&xhci->lock);
  239. /* Check if the xHC generated the interrupt, or the irq is shared */
  240. temp = xhci_readl(xhci, &xhci->op_regs->status);
  241. temp2 = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  242. if (!(temp & STS_EINT) && !ER_IRQ_PENDING(temp2)) {
  243. spin_unlock(&xhci->lock);
  244. return IRQ_NONE;
  245. }
  246. if (temp & STS_FATAL) {
  247. xhci_warn(xhci, "WARNING: Host System Error\n");
  248. xhci_halt(xhci);
  249. xhci_to_hcd(xhci)->state = HC_STATE_HALT;
  250. spin_unlock(&xhci->lock);
  251. return -ESHUTDOWN;
  252. }
  253. xhci_work(xhci);
  254. spin_unlock(&xhci->lock);
  255. return IRQ_HANDLED;
  256. }
  257. #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
  258. void xhci_event_ring_work(unsigned long arg)
  259. {
  260. unsigned long flags;
  261. int temp;
  262. struct xhci_hcd *xhci = (struct xhci_hcd *) arg;
  263. int i, j;
  264. xhci_dbg(xhci, "Poll event ring: %lu\n", jiffies);
  265. spin_lock_irqsave(&xhci->lock, flags);
  266. temp = xhci_readl(xhci, &xhci->op_regs->status);
  267. xhci_dbg(xhci, "op reg status = 0x%x\n", temp);
  268. temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  269. xhci_dbg(xhci, "ir_set 0 pending = 0x%x\n", temp);
  270. xhci_dbg(xhci, "No-op commands handled = %d\n", xhci->noops_handled);
  271. xhci_dbg(xhci, "HC error bitmask = 0x%x\n", xhci->error_bitmask);
  272. xhci->error_bitmask = 0;
  273. xhci_dbg(xhci, "Event ring:\n");
  274. xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
  275. xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
  276. temp = xhci_readl(xhci, &xhci->ir_set->erst_dequeue[0]);
  277. temp &= ERST_PTR_MASK;
  278. xhci_dbg(xhci, "ERST deq = 0x%x\n", temp);
  279. xhci_dbg(xhci, "Command ring:\n");
  280. xhci_debug_segment(xhci, xhci->cmd_ring->deq_seg);
  281. xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
  282. xhci_dbg_cmd_ptrs(xhci);
  283. for (i = 0; i < MAX_HC_SLOTS; ++i) {
  284. if (xhci->devs[i]) {
  285. for (j = 0; j < 31; ++j) {
  286. if (xhci->devs[i]->ep_rings[j]) {
  287. xhci_dbg(xhci, "Dev %d endpoint ring %d:\n", i, j);
  288. xhci_debug_segment(xhci, xhci->devs[i]->ep_rings[j]->deq_seg);
  289. }
  290. }
  291. }
  292. }
  293. if (xhci->noops_submitted != NUM_TEST_NOOPS)
  294. if (xhci_setup_one_noop(xhci))
  295. xhci_ring_cmd_db(xhci);
  296. spin_unlock_irqrestore(&xhci->lock, flags);
  297. if (!xhci->zombie)
  298. mod_timer(&xhci->event_ring_timer, jiffies + POLL_TIMEOUT * HZ);
  299. else
  300. xhci_dbg(xhci, "Quit polling the event ring.\n");
  301. }
  302. #endif
  303. /*
  304. * Start the HC after it was halted.
  305. *
  306. * This function is called by the USB core when the HC driver is added.
  307. * Its opposite is xhci_stop().
  308. *
  309. * xhci_init() must be called once before this function can be called.
  310. * Reset the HC, enable device slot contexts, program DCBAAP, and
  311. * set command ring pointer and event ring pointer.
  312. *
  313. * Setup MSI-X vectors and enable interrupts.
  314. */
  315. int xhci_run(struct usb_hcd *hcd)
  316. {
  317. u32 temp;
  318. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  319. void (*doorbell)(struct xhci_hcd *) = NULL;
  320. hcd->uses_new_polling = 1;
  321. hcd->poll_rh = 0;
  322. xhci_dbg(xhci, "xhci_run\n");
  323. #if 0 /* FIXME: MSI not setup yet */
  324. /* Do this at the very last minute */
  325. ret = xhci_setup_msix(xhci);
  326. if (!ret)
  327. return ret;
  328. return -ENOSYS;
  329. #endif
  330. #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
  331. init_timer(&xhci->event_ring_timer);
  332. xhci->event_ring_timer.data = (unsigned long) xhci;
  333. xhci->event_ring_timer.function = xhci_event_ring_work;
  334. /* Poll the event ring */
  335. xhci->event_ring_timer.expires = jiffies + POLL_TIMEOUT * HZ;
  336. xhci->zombie = 0;
  337. xhci_dbg(xhci, "Setting event ring polling timer\n");
  338. add_timer(&xhci->event_ring_timer);
  339. #endif
  340. xhci_dbg(xhci, "// Set the interrupt modulation register\n");
  341. temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
  342. temp &= ~ER_IRQ_INTERVAL_MASK;
  343. temp |= (u32) 160;
  344. xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
  345. /* Set the HCD state before we enable the irqs */
  346. hcd->state = HC_STATE_RUNNING;
  347. temp = xhci_readl(xhci, &xhci->op_regs->command);
  348. temp |= (CMD_EIE);
  349. xhci_dbg(xhci, "// Enable interrupts, cmd = 0x%x.\n",
  350. temp);
  351. xhci_writel(xhci, temp, &xhci->op_regs->command);
  352. temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  353. xhci_dbg(xhci, "// Enabling event ring interrupter %p by writing 0x%x to irq_pending\n",
  354. xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
  355. xhci_writel(xhci, ER_IRQ_ENABLE(temp),
  356. &xhci->ir_set->irq_pending);
  357. xhci_print_ir_set(xhci, xhci->ir_set, 0);
  358. if (NUM_TEST_NOOPS > 0)
  359. doorbell = xhci_setup_one_noop(xhci);
  360. xhci_dbg(xhci, "Command ring memory map follows:\n");
  361. xhci_debug_ring(xhci, xhci->cmd_ring);
  362. xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
  363. xhci_dbg_cmd_ptrs(xhci);
  364. xhci_dbg(xhci, "ERST memory map follows:\n");
  365. xhci_dbg_erst(xhci, &xhci->erst);
  366. xhci_dbg(xhci, "Event ring:\n");
  367. xhci_debug_ring(xhci, xhci->event_ring);
  368. xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
  369. temp = xhci_readl(xhci, &xhci->ir_set->erst_dequeue[0]);
  370. temp &= ERST_PTR_MASK;
  371. xhci_dbg(xhci, "ERST deq = 0x%x\n", temp);
  372. temp = xhci_readl(xhci, &xhci->ir_set->erst_dequeue[1]);
  373. xhci_dbg(xhci, "ERST deq upper = 0x%x\n", temp);
  374. temp = xhci_readl(xhci, &xhci->op_regs->command);
  375. temp |= (CMD_RUN);
  376. xhci_dbg(xhci, "// Turn on HC, cmd = 0x%x.\n",
  377. temp);
  378. xhci_writel(xhci, temp, &xhci->op_regs->command);
  379. /* Flush PCI posted writes */
  380. temp = xhci_readl(xhci, &xhci->op_regs->command);
  381. xhci_dbg(xhci, "// @%p = 0x%x\n", &xhci->op_regs->command, temp);
  382. if (doorbell)
  383. (*doorbell)(xhci);
  384. xhci_dbg(xhci, "Finished xhci_run\n");
  385. return 0;
  386. }
  387. /*
  388. * Stop xHCI driver.
  389. *
  390. * This function is called by the USB core when the HC driver is removed.
  391. * Its opposite is xhci_run().
  392. *
  393. * Disable device contexts, disable IRQs, and quiesce the HC.
  394. * Reset the HC, finish any completed transactions, and cleanup memory.
  395. */
  396. void xhci_stop(struct usb_hcd *hcd)
  397. {
  398. u32 temp;
  399. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  400. spin_lock_irq(&xhci->lock);
  401. if (HC_IS_RUNNING(hcd->state))
  402. xhci_quiesce(xhci);
  403. xhci_halt(xhci);
  404. xhci_reset(xhci);
  405. spin_unlock_irq(&xhci->lock);
  406. #if 0 /* No MSI yet */
  407. xhci_cleanup_msix(xhci);
  408. #endif
  409. #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
  410. /* Tell the event ring poll function not to reschedule */
  411. xhci->zombie = 1;
  412. del_timer_sync(&xhci->event_ring_timer);
  413. #endif
  414. xhci_dbg(xhci, "// Disabling event ring interrupts\n");
  415. temp = xhci_readl(xhci, &xhci->op_regs->status);
  416. xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
  417. temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  418. xhci_writel(xhci, ER_IRQ_DISABLE(temp),
  419. &xhci->ir_set->irq_pending);
  420. xhci_print_ir_set(xhci, xhci->ir_set, 0);
  421. xhci_dbg(xhci, "cleaning up memory\n");
  422. xhci_mem_cleanup(xhci);
  423. xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
  424. xhci_readl(xhci, &xhci->op_regs->status));
  425. }
  426. /*
  427. * Shutdown HC (not bus-specific)
  428. *
  429. * This is called when the machine is rebooting or halting. We assume that the
  430. * machine will be powered off, and the HC's internal state will be reset.
  431. * Don't bother to free memory.
  432. */
  433. void xhci_shutdown(struct usb_hcd *hcd)
  434. {
  435. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  436. spin_lock_irq(&xhci->lock);
  437. xhci_halt(xhci);
  438. spin_unlock_irq(&xhci->lock);
  439. #if 0
  440. xhci_cleanup_msix(xhci);
  441. #endif
  442. xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n",
  443. xhci_readl(xhci, &xhci->op_regs->status));
  444. }
  445. /*-------------------------------------------------------------------------*/
  446. /**
  447. * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
  448. * HCDs. Find the index for an endpoint given its descriptor. Use the return
  449. * value to right shift 1 for the bitmask.
  450. *
  451. * Index = (epnum * 2) + direction - 1,
  452. * where direction = 0 for OUT, 1 for IN.
  453. * For control endpoints, the IN index is used (OUT index is unused), so
  454. * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
  455. */
  456. unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
  457. {
  458. unsigned int index;
  459. if (usb_endpoint_xfer_control(desc))
  460. index = (unsigned int) (usb_endpoint_num(desc)*2);
  461. else
  462. index = (unsigned int) (usb_endpoint_num(desc)*2) +
  463. (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
  464. return index;
  465. }
  466. /* Find the flag for this endpoint (for use in the control context). Use the
  467. * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
  468. * bit 1, etc.
  469. */
  470. unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
  471. {
  472. return 1 << (xhci_get_endpoint_index(desc) + 1);
  473. }
  474. /* Compute the last valid endpoint context index. Basically, this is the
  475. * endpoint index plus one. For slot contexts with more than valid endpoint,
  476. * we find the most significant bit set in the added contexts flags.
  477. * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
  478. * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
  479. */
  480. static inline unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
  481. {
  482. return fls(added_ctxs) - 1;
  483. }
  484. /* Returns 1 if the arguments are OK;
  485. * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
  486. */
  487. int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
  488. struct usb_host_endpoint *ep, int check_ep, const char *func) {
  489. if (!hcd || (check_ep && !ep) || !udev) {
  490. printk(KERN_DEBUG "xHCI %s called with invalid args\n",
  491. func);
  492. return -EINVAL;
  493. }
  494. if (!udev->parent) {
  495. printk(KERN_DEBUG "xHCI %s called for root hub\n",
  496. func);
  497. return 0;
  498. }
  499. if (!udev->slot_id) {
  500. printk(KERN_DEBUG "xHCI %s called with unaddressed device\n",
  501. func);
  502. return -EINVAL;
  503. }
  504. return 1;
  505. }
  506. /*
  507. * non-error returns are a promise to giveback() the urb later
  508. * we drop ownership so next owner (or urb unlink) can get it
  509. */
  510. int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
  511. {
  512. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  513. unsigned long flags;
  514. int ret = 0;
  515. unsigned int slot_id, ep_index;
  516. if (!urb || xhci_check_args(hcd, urb->dev, urb->ep, true, __func__) <= 0)
  517. return -EINVAL;
  518. slot_id = urb->dev->slot_id;
  519. ep_index = xhci_get_endpoint_index(&urb->ep->desc);
  520. spin_lock_irqsave(&xhci->lock, flags);
  521. if (!xhci->devs || !xhci->devs[slot_id]) {
  522. if (!in_interrupt())
  523. dev_warn(&urb->dev->dev, "WARN: urb submitted for dev with no Slot ID\n");
  524. ret = -EINVAL;
  525. goto exit;
  526. }
  527. if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
  528. if (!in_interrupt())
  529. xhci_dbg(xhci, "urb submitted during PCI suspend\n");
  530. ret = -ESHUTDOWN;
  531. goto exit;
  532. }
  533. if (usb_endpoint_xfer_control(&urb->ep->desc))
  534. ret = xhci_queue_ctrl_tx(xhci, mem_flags, urb,
  535. slot_id, ep_index);
  536. else if (usb_endpoint_xfer_bulk(&urb->ep->desc))
  537. ret = xhci_queue_bulk_tx(xhci, mem_flags, urb,
  538. slot_id, ep_index);
  539. else
  540. ret = -EINVAL;
  541. exit:
  542. spin_unlock_irqrestore(&xhci->lock, flags);
  543. return ret;
  544. }
  545. /*
  546. * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
  547. * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
  548. * should pick up where it left off in the TD, unless a Set Transfer Ring
  549. * Dequeue Pointer is issued.
  550. *
  551. * The TRBs that make up the buffers for the canceled URB will be "removed" from
  552. * the ring. Since the ring is a contiguous structure, they can't be physically
  553. * removed. Instead, there are two options:
  554. *
  555. * 1) If the HC is in the middle of processing the URB to be canceled, we
  556. * simply move the ring's dequeue pointer past those TRBs using the Set
  557. * Transfer Ring Dequeue Pointer command. This will be the common case,
  558. * when drivers timeout on the last submitted URB and attempt to cancel.
  559. *
  560. * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
  561. * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
  562. * HC will need to invalidate the any TRBs it has cached after the stop
  563. * endpoint command, as noted in the xHCI 0.95 errata.
  564. *
  565. * 3) The TD may have completed by the time the Stop Endpoint Command
  566. * completes, so software needs to handle that case too.
  567. *
  568. * This function should protect against the TD enqueueing code ringing the
  569. * doorbell while this code is waiting for a Stop Endpoint command to complete.
  570. * It also needs to account for multiple cancellations on happening at the same
  571. * time for the same endpoint.
  572. *
  573. * Note that this function can be called in any context, or so says
  574. * usb_hcd_unlink_urb()
  575. */
  576. int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  577. {
  578. unsigned long flags;
  579. int ret;
  580. struct xhci_hcd *xhci;
  581. struct xhci_td *td;
  582. unsigned int ep_index;
  583. struct xhci_ring *ep_ring;
  584. xhci = hcd_to_xhci(hcd);
  585. spin_lock_irqsave(&xhci->lock, flags);
  586. /* Make sure the URB hasn't completed or been unlinked already */
  587. ret = usb_hcd_check_unlink_urb(hcd, urb, status);
  588. if (ret || !urb->hcpriv)
  589. goto done;
  590. xhci_dbg(xhci, "Cancel URB %p\n", urb);
  591. ep_index = xhci_get_endpoint_index(&urb->ep->desc);
  592. ep_ring = xhci->devs[urb->dev->slot_id]->ep_rings[ep_index];
  593. td = (struct xhci_td *) urb->hcpriv;
  594. ep_ring->cancels_pending++;
  595. list_add_tail(&td->cancelled_td_list, &ep_ring->cancelled_td_list);
  596. /* Queue a stop endpoint command, but only if this is
  597. * the first cancellation to be handled.
  598. */
  599. if (ep_ring->cancels_pending == 1) {
  600. xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index);
  601. xhci_ring_cmd_db(xhci);
  602. }
  603. done:
  604. spin_unlock_irqrestore(&xhci->lock, flags);
  605. return ret;
  606. }
  607. /* Drop an endpoint from a new bandwidth configuration for this device.
  608. * Only one call to this function is allowed per endpoint before
  609. * check_bandwidth() or reset_bandwidth() must be called.
  610. * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
  611. * add the endpoint to the schedule with possibly new parameters denoted by a
  612. * different endpoint descriptor in usb_host_endpoint.
  613. * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
  614. * not allowed.
  615. *
  616. * The USB core will not allow URBs to be queued to an endpoint that is being
  617. * disabled, so there's no need for mutual exclusion to protect
  618. * the xhci->devs[slot_id] structure.
  619. */
  620. int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
  621. struct usb_host_endpoint *ep)
  622. {
  623. struct xhci_hcd *xhci;
  624. struct xhci_device_control *in_ctx;
  625. unsigned int last_ctx;
  626. unsigned int ep_index;
  627. struct xhci_ep_ctx *ep_ctx;
  628. u32 drop_flag;
  629. u32 new_add_flags, new_drop_flags, new_slot_info;
  630. int ret;
  631. ret = xhci_check_args(hcd, udev, ep, 1, __func__);
  632. if (ret <= 0)
  633. return ret;
  634. xhci = hcd_to_xhci(hcd);
  635. xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
  636. drop_flag = xhci_get_endpoint_flag(&ep->desc);
  637. if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
  638. xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
  639. __func__, drop_flag);
  640. return 0;
  641. }
  642. if (!xhci->devs || !xhci->devs[udev->slot_id]) {
  643. xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
  644. __func__);
  645. return -EINVAL;
  646. }
  647. in_ctx = xhci->devs[udev->slot_id]->in_ctx;
  648. ep_index = xhci_get_endpoint_index(&ep->desc);
  649. ep_ctx = &xhci->devs[udev->slot_id]->out_ctx->ep[ep_index];
  650. /* If the HC already knows the endpoint is disabled,
  651. * or the HCD has noted it is disabled, ignore this request
  652. */
  653. if ((ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_DISABLED ||
  654. in_ctx->drop_flags & xhci_get_endpoint_flag(&ep->desc)) {
  655. xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
  656. __func__, ep);
  657. return 0;
  658. }
  659. in_ctx->drop_flags |= drop_flag;
  660. new_drop_flags = in_ctx->drop_flags;
  661. in_ctx->add_flags = ~drop_flag;
  662. new_add_flags = in_ctx->add_flags;
  663. last_ctx = xhci_last_valid_endpoint(in_ctx->add_flags);
  664. /* Update the last valid endpoint context, if we deleted the last one */
  665. if ((in_ctx->slot.dev_info & LAST_CTX_MASK) > LAST_CTX(last_ctx)) {
  666. in_ctx->slot.dev_info &= ~LAST_CTX_MASK;
  667. in_ctx->slot.dev_info |= LAST_CTX(last_ctx);
  668. }
  669. new_slot_info = in_ctx->slot.dev_info;
  670. xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
  671. xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
  672. (unsigned int) ep->desc.bEndpointAddress,
  673. udev->slot_id,
  674. (unsigned int) new_drop_flags,
  675. (unsigned int) new_add_flags,
  676. (unsigned int) new_slot_info);
  677. return 0;
  678. }
  679. /* Add an endpoint to a new possible bandwidth configuration for this device.
  680. * Only one call to this function is allowed per endpoint before
  681. * check_bandwidth() or reset_bandwidth() must be called.
  682. * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
  683. * add the endpoint to the schedule with possibly new parameters denoted by a
  684. * different endpoint descriptor in usb_host_endpoint.
  685. * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
  686. * not allowed.
  687. *
  688. * The USB core will not allow URBs to be queued to an endpoint until the
  689. * configuration or alt setting is installed in the device, so there's no need
  690. * for mutual exclusion to protect the xhci->devs[slot_id] structure.
  691. */
  692. int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
  693. struct usb_host_endpoint *ep)
  694. {
  695. struct xhci_hcd *xhci;
  696. struct xhci_device_control *in_ctx;
  697. unsigned int ep_index;
  698. struct xhci_ep_ctx *ep_ctx;
  699. u32 added_ctxs;
  700. unsigned int last_ctx;
  701. u32 new_add_flags, new_drop_flags, new_slot_info;
  702. int ret = 0;
  703. ret = xhci_check_args(hcd, udev, ep, 1, __func__);
  704. if (ret <= 0)
  705. return ret;
  706. xhci = hcd_to_xhci(hcd);
  707. added_ctxs = xhci_get_endpoint_flag(&ep->desc);
  708. last_ctx = xhci_last_valid_endpoint(added_ctxs);
  709. if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
  710. /* FIXME when we have to issue an evaluate endpoint command to
  711. * deal with ep0 max packet size changing once we get the
  712. * descriptors
  713. */
  714. xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
  715. __func__, added_ctxs);
  716. return 0;
  717. }
  718. if (!xhci->devs || !xhci->devs[udev->slot_id]) {
  719. xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
  720. __func__);
  721. return -EINVAL;
  722. }
  723. in_ctx = xhci->devs[udev->slot_id]->in_ctx;
  724. ep_index = xhci_get_endpoint_index(&ep->desc);
  725. ep_ctx = &xhci->devs[udev->slot_id]->out_ctx->ep[ep_index];
  726. /* If the HCD has already noted the endpoint is enabled,
  727. * ignore this request.
  728. */
  729. if (in_ctx->add_flags & xhci_get_endpoint_flag(&ep->desc)) {
  730. xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
  731. __func__, ep);
  732. return 0;
  733. }
  734. /*
  735. * Configuration and alternate setting changes must be done in
  736. * process context, not interrupt context (or so documenation
  737. * for usb_set_interface() and usb_set_configuration() claim).
  738. */
  739. if (xhci_endpoint_init(xhci, xhci->devs[udev->slot_id],
  740. udev, ep, GFP_KERNEL) < 0) {
  741. dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
  742. __func__, ep->desc.bEndpointAddress);
  743. return -ENOMEM;
  744. }
  745. in_ctx->add_flags |= added_ctxs;
  746. new_add_flags = in_ctx->add_flags;
  747. /* If xhci_endpoint_disable() was called for this endpoint, but the
  748. * xHC hasn't been notified yet through the check_bandwidth() call,
  749. * this re-adds a new state for the endpoint from the new endpoint
  750. * descriptors. We must drop and re-add this endpoint, so we leave the
  751. * drop flags alone.
  752. */
  753. new_drop_flags = in_ctx->drop_flags;
  754. /* Update the last valid endpoint context, if we just added one past */
  755. if ((in_ctx->slot.dev_info & LAST_CTX_MASK) < LAST_CTX(last_ctx)) {
  756. in_ctx->slot.dev_info &= ~LAST_CTX_MASK;
  757. in_ctx->slot.dev_info |= LAST_CTX(last_ctx);
  758. }
  759. new_slot_info = in_ctx->slot.dev_info;
  760. xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
  761. (unsigned int) ep->desc.bEndpointAddress,
  762. udev->slot_id,
  763. (unsigned int) new_drop_flags,
  764. (unsigned int) new_add_flags,
  765. (unsigned int) new_slot_info);
  766. return 0;
  767. }
  768. static void xhci_zero_in_ctx(struct xhci_virt_device *virt_dev)
  769. {
  770. struct xhci_ep_ctx *ep_ctx;
  771. int i;
  772. /* When a device's add flag and drop flag are zero, any subsequent
  773. * configure endpoint command will leave that endpoint's state
  774. * untouched. Make sure we don't leave any old state in the input
  775. * endpoint contexts.
  776. */
  777. virt_dev->in_ctx->drop_flags = 0;
  778. virt_dev->in_ctx->add_flags = 0;
  779. virt_dev->in_ctx->slot.dev_info &= ~LAST_CTX_MASK;
  780. /* Endpoint 0 is always valid */
  781. virt_dev->in_ctx->slot.dev_info |= LAST_CTX(1);
  782. for (i = 1; i < 31; ++i) {
  783. ep_ctx = &virt_dev->in_ctx->ep[i];
  784. ep_ctx->ep_info = 0;
  785. ep_ctx->ep_info2 = 0;
  786. ep_ctx->deq[0] = 0;
  787. ep_ctx->deq[1] = 0;
  788. ep_ctx->tx_info = 0;
  789. }
  790. }
  791. /* Called after one or more calls to xhci_add_endpoint() or
  792. * xhci_drop_endpoint(). If this call fails, the USB core is expected
  793. * to call xhci_reset_bandwidth().
  794. *
  795. * Since we are in the middle of changing either configuration or
  796. * installing a new alt setting, the USB core won't allow URBs to be
  797. * enqueued for any endpoint on the old config or interface. Nothing
  798. * else should be touching the xhci->devs[slot_id] structure, so we
  799. * don't need to take the xhci->lock for manipulating that.
  800. */
  801. int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
  802. {
  803. int i;
  804. int ret = 0;
  805. int timeleft;
  806. unsigned long flags;
  807. struct xhci_hcd *xhci;
  808. struct xhci_virt_device *virt_dev;
  809. ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
  810. if (ret <= 0)
  811. return ret;
  812. xhci = hcd_to_xhci(hcd);
  813. if (!udev->slot_id || !xhci->devs || !xhci->devs[udev->slot_id]) {
  814. xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
  815. __func__);
  816. return -EINVAL;
  817. }
  818. xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
  819. virt_dev = xhci->devs[udev->slot_id];
  820. /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
  821. virt_dev->in_ctx->add_flags |= SLOT_FLAG;
  822. virt_dev->in_ctx->add_flags &= ~EP0_FLAG;
  823. virt_dev->in_ctx->drop_flags &= ~SLOT_FLAG;
  824. virt_dev->in_ctx->drop_flags &= ~EP0_FLAG;
  825. xhci_dbg(xhci, "New Input Control Context:\n");
  826. xhci_dbg_ctx(xhci, virt_dev->in_ctx, virt_dev->in_ctx_dma,
  827. LAST_CTX_TO_EP_NUM(virt_dev->in_ctx->slot.dev_info));
  828. spin_lock_irqsave(&xhci->lock, flags);
  829. ret = xhci_queue_configure_endpoint(xhci, virt_dev->in_ctx_dma,
  830. udev->slot_id);
  831. if (ret < 0) {
  832. spin_unlock_irqrestore(&xhci->lock, flags);
  833. xhci_dbg(xhci, "FIXME allocate a new ring segment\n");
  834. return -ENOMEM;
  835. }
  836. xhci_ring_cmd_db(xhci);
  837. spin_unlock_irqrestore(&xhci->lock, flags);
  838. /* Wait for the configure endpoint command to complete */
  839. timeleft = wait_for_completion_interruptible_timeout(
  840. &virt_dev->cmd_completion,
  841. USB_CTRL_SET_TIMEOUT);
  842. if (timeleft <= 0) {
  843. xhci_warn(xhci, "%s while waiting for configure endpoint command\n",
  844. timeleft == 0 ? "Timeout" : "Signal");
  845. /* FIXME cancel the configure endpoint command */
  846. return -ETIME;
  847. }
  848. switch (virt_dev->cmd_status) {
  849. case COMP_ENOMEM:
  850. dev_warn(&udev->dev, "Not enough host controller resources "
  851. "for new device state.\n");
  852. ret = -ENOMEM;
  853. /* FIXME: can we allocate more resources for the HC? */
  854. break;
  855. case COMP_BW_ERR:
  856. dev_warn(&udev->dev, "Not enough bandwidth "
  857. "for new device state.\n");
  858. ret = -ENOSPC;
  859. /* FIXME: can we go back to the old state? */
  860. break;
  861. case COMP_TRB_ERR:
  862. /* the HCD set up something wrong */
  863. dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, add flag = 1, "
  864. "and endpoint is not disabled.\n");
  865. ret = -EINVAL;
  866. break;
  867. case COMP_SUCCESS:
  868. dev_dbg(&udev->dev, "Successful Endpoint Configure command\n");
  869. break;
  870. default:
  871. xhci_err(xhci, "ERROR: unexpected command completion "
  872. "code 0x%x.\n", virt_dev->cmd_status);
  873. ret = -EINVAL;
  874. break;
  875. }
  876. if (ret) {
  877. /* Callee should call reset_bandwidth() */
  878. return ret;
  879. }
  880. xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
  881. xhci_dbg_ctx(xhci, virt_dev->out_ctx, virt_dev->out_ctx_dma,
  882. LAST_CTX_TO_EP_NUM(virt_dev->in_ctx->slot.dev_info));
  883. xhci_zero_in_ctx(virt_dev);
  884. /* Free any old rings */
  885. for (i = 1; i < 31; ++i) {
  886. if (virt_dev->new_ep_rings[i]) {
  887. xhci_ring_free(xhci, virt_dev->ep_rings[i]);
  888. virt_dev->ep_rings[i] = virt_dev->new_ep_rings[i];
  889. virt_dev->new_ep_rings[i] = NULL;
  890. }
  891. }
  892. return ret;
  893. }
  894. void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
  895. {
  896. struct xhci_hcd *xhci;
  897. struct xhci_virt_device *virt_dev;
  898. int i, ret;
  899. ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
  900. if (ret <= 0)
  901. return;
  902. xhci = hcd_to_xhci(hcd);
  903. if (!xhci->devs || !xhci->devs[udev->slot_id]) {
  904. xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
  905. __func__);
  906. return;
  907. }
  908. xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
  909. virt_dev = xhci->devs[udev->slot_id];
  910. /* Free any rings allocated for added endpoints */
  911. for (i = 0; i < 31; ++i) {
  912. if (virt_dev->new_ep_rings[i]) {
  913. xhci_ring_free(xhci, virt_dev->new_ep_rings[i]);
  914. virt_dev->new_ep_rings[i] = NULL;
  915. }
  916. }
  917. xhci_zero_in_ctx(virt_dev);
  918. }
  919. /*
  920. * At this point, the struct usb_device is about to go away, the device has
  921. * disconnected, and all traffic has been stopped and the endpoints have been
  922. * disabled. Free any HC data structures associated with that device.
  923. */
  924. void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
  925. {
  926. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  927. unsigned long flags;
  928. if (udev->slot_id == 0)
  929. return;
  930. spin_lock_irqsave(&xhci->lock, flags);
  931. if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
  932. spin_unlock_irqrestore(&xhci->lock, flags);
  933. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  934. return;
  935. }
  936. xhci_ring_cmd_db(xhci);
  937. spin_unlock_irqrestore(&xhci->lock, flags);
  938. /*
  939. * Event command completion handler will free any data structures
  940. * associated with the slot. XXX Can free sleep?
  941. */
  942. }
  943. /*
  944. * Returns 0 if the xHC ran out of device slots, the Enable Slot command
  945. * timed out, or allocating memory failed. Returns 1 on success.
  946. */
  947. int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
  948. {
  949. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  950. unsigned long flags;
  951. int timeleft;
  952. int ret;
  953. spin_lock_irqsave(&xhci->lock, flags);
  954. ret = xhci_queue_slot_control(xhci, TRB_ENABLE_SLOT, 0);
  955. if (ret) {
  956. spin_unlock_irqrestore(&xhci->lock, flags);
  957. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  958. return 0;
  959. }
  960. xhci_ring_cmd_db(xhci);
  961. spin_unlock_irqrestore(&xhci->lock, flags);
  962. /* XXX: how much time for xHC slot assignment? */
  963. timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
  964. USB_CTRL_SET_TIMEOUT);
  965. if (timeleft <= 0) {
  966. xhci_warn(xhci, "%s while waiting for a slot\n",
  967. timeleft == 0 ? "Timeout" : "Signal");
  968. /* FIXME cancel the enable slot request */
  969. return 0;
  970. }
  971. if (!xhci->slot_id) {
  972. xhci_err(xhci, "Error while assigning device slot ID\n");
  973. return 0;
  974. }
  975. /* xhci_alloc_virt_device() does not touch rings; no need to lock */
  976. if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_KERNEL)) {
  977. /* Disable slot, if we can do it without mem alloc */
  978. xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
  979. spin_lock_irqsave(&xhci->lock, flags);
  980. if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
  981. xhci_ring_cmd_db(xhci);
  982. spin_unlock_irqrestore(&xhci->lock, flags);
  983. return 0;
  984. }
  985. udev->slot_id = xhci->slot_id;
  986. /* Is this a LS or FS device under a HS hub? */
  987. /* Hub or peripherial? */
  988. return 1;
  989. }
  990. /*
  991. * Issue an Address Device command (which will issue a SetAddress request to
  992. * the device).
  993. * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so
  994. * we should only issue and wait on one address command at the same time.
  995. *
  996. * We add one to the device address issued by the hardware because the USB core
  997. * uses address 1 for the root hubs (even though they're not really devices).
  998. */
  999. int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
  1000. {
  1001. unsigned long flags;
  1002. int timeleft;
  1003. struct xhci_virt_device *virt_dev;
  1004. int ret = 0;
  1005. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1006. u32 temp;
  1007. if (!udev->slot_id) {
  1008. xhci_dbg(xhci, "Bad Slot ID %d\n", udev->slot_id);
  1009. return -EINVAL;
  1010. }
  1011. virt_dev = xhci->devs[udev->slot_id];
  1012. /* If this is a Set Address to an unconfigured device, setup ep 0 */
  1013. if (!udev->config)
  1014. xhci_setup_addressable_virt_dev(xhci, udev);
  1015. /* Otherwise, assume the core has the device configured how it wants */
  1016. spin_lock_irqsave(&xhci->lock, flags);
  1017. ret = xhci_queue_address_device(xhci, virt_dev->in_ctx_dma,
  1018. udev->slot_id);
  1019. if (ret) {
  1020. spin_unlock_irqrestore(&xhci->lock, flags);
  1021. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  1022. return ret;
  1023. }
  1024. xhci_ring_cmd_db(xhci);
  1025. spin_unlock_irqrestore(&xhci->lock, flags);
  1026. /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
  1027. timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
  1028. USB_CTRL_SET_TIMEOUT);
  1029. /* FIXME: From section 4.3.4: "Software shall be responsible for timing
  1030. * the SetAddress() "recovery interval" required by USB and aborting the
  1031. * command on a timeout.
  1032. */
  1033. if (timeleft <= 0) {
  1034. xhci_warn(xhci, "%s while waiting for a slot\n",
  1035. timeleft == 0 ? "Timeout" : "Signal");
  1036. /* FIXME cancel the address device command */
  1037. return -ETIME;
  1038. }
  1039. switch (virt_dev->cmd_status) {
  1040. case COMP_CTX_STATE:
  1041. case COMP_EBADSLT:
  1042. xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n",
  1043. udev->slot_id);
  1044. ret = -EINVAL;
  1045. break;
  1046. case COMP_TX_ERR:
  1047. dev_warn(&udev->dev, "Device not responding to set address.\n");
  1048. ret = -EPROTO;
  1049. break;
  1050. case COMP_SUCCESS:
  1051. xhci_dbg(xhci, "Successful Address Device command\n");
  1052. break;
  1053. default:
  1054. xhci_err(xhci, "ERROR: unexpected command completion "
  1055. "code 0x%x.\n", virt_dev->cmd_status);
  1056. ret = -EINVAL;
  1057. break;
  1058. }
  1059. if (ret) {
  1060. return ret;
  1061. }
  1062. temp = xhci_readl(xhci, &xhci->op_regs->dcbaa_ptr[0]);
  1063. xhci_dbg(xhci, "Op regs DCBAA ptr[0] = %#08x\n", temp);
  1064. temp = xhci_readl(xhci, &xhci->op_regs->dcbaa_ptr[1]);
  1065. xhci_dbg(xhci, "Op regs DCBAA ptr[1] = %#08x\n", temp);
  1066. xhci_dbg(xhci, "Slot ID %d dcbaa entry[0] @%p = %#08x\n",
  1067. udev->slot_id,
  1068. &xhci->dcbaa->dev_context_ptrs[2*udev->slot_id],
  1069. xhci->dcbaa->dev_context_ptrs[2*udev->slot_id]);
  1070. xhci_dbg(xhci, "Slot ID %d dcbaa entry[1] @%p = %#08x\n",
  1071. udev->slot_id,
  1072. &xhci->dcbaa->dev_context_ptrs[2*udev->slot_id+1],
  1073. xhci->dcbaa->dev_context_ptrs[2*udev->slot_id+1]);
  1074. xhci_dbg(xhci, "Output Context DMA address = %#08llx\n",
  1075. (unsigned long long)virt_dev->out_ctx_dma);
  1076. xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
  1077. xhci_dbg_ctx(xhci, virt_dev->in_ctx, virt_dev->in_ctx_dma, 2);
  1078. xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
  1079. xhci_dbg_ctx(xhci, virt_dev->out_ctx, virt_dev->out_ctx_dma, 2);
  1080. /*
  1081. * USB core uses address 1 for the roothubs, so we add one to the
  1082. * address given back to us by the HC.
  1083. */
  1084. udev->devnum = (virt_dev->out_ctx->slot.dev_state & DEV_ADDR_MASK) + 1;
  1085. /* Zero the input context control for later use */
  1086. virt_dev->in_ctx->add_flags = 0;
  1087. virt_dev->in_ctx->drop_flags = 0;
  1088. /* Mirror flags in the output context for future ep enable/disable */
  1089. virt_dev->out_ctx->add_flags = SLOT_FLAG | EP0_FLAG;
  1090. virt_dev->out_ctx->drop_flags = 0;
  1091. xhci_dbg(xhci, "Device address = %d\n", udev->devnum);
  1092. /* XXX Meh, not sure if anyone else but choose_address uses this. */
  1093. set_bit(udev->devnum, udev->bus->devmap.devicemap);
  1094. return 0;
  1095. }
  1096. int xhci_get_frame(struct usb_hcd *hcd)
  1097. {
  1098. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1099. /* EHCI mods by the periodic size. Why? */
  1100. return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
  1101. }
  1102. MODULE_DESCRIPTION(DRIVER_DESC);
  1103. MODULE_AUTHOR(DRIVER_AUTHOR);
  1104. MODULE_LICENSE("GPL");
  1105. static int __init xhci_hcd_init(void)
  1106. {
  1107. #ifdef CONFIG_PCI
  1108. int retval = 0;
  1109. retval = xhci_register_pci();
  1110. if (retval < 0) {
  1111. printk(KERN_DEBUG "Problem registering PCI driver.");
  1112. return retval;
  1113. }
  1114. #endif
  1115. /*
  1116. * Check the compiler generated sizes of structures that must be laid
  1117. * out in specific ways for hardware access.
  1118. */
  1119. BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
  1120. BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
  1121. BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
  1122. /* xhci_device_control has eight fields, and also
  1123. * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
  1124. */
  1125. BUILD_BUG_ON(sizeof(struct xhci_device_control) != (8+8+8*31)*32/8);
  1126. BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
  1127. BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
  1128. BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
  1129. BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 7*32/8);
  1130. BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
  1131. /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
  1132. BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
  1133. BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
  1134. return 0;
  1135. }
  1136. module_init(xhci_hcd_init);
  1137. static void __exit xhci_hcd_cleanup(void)
  1138. {
  1139. #ifdef CONFIG_PCI
  1140. xhci_unregister_pci();
  1141. #endif
  1142. }
  1143. module_exit(xhci_hcd_cleanup);