xhci.c 94 KB

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