xhci.c 73 KB

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