/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

Large files are truncated click here to view the full file

  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_d