xhci.c 83 KB

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