xhci-hcd.c 60 KB

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