PageRenderTime 66ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/usb/host/xhci.c

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