xhci.c 76 KB

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