xhci.c 73 KB

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