xhci.c 81 KB

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