xhci.c 82 KB

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