xhci-hcd.c 54 KB

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