xhci.c 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040
  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. int ret = 0;
  1239. ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
  1240. if (ret <= 0) {
  1241. /* So we won't queue a reset ep command for a root hub */
  1242. ep->hcpriv = NULL;
  1243. return ret;
  1244. }
  1245. xhci = hcd_to_xhci(hcd);
  1246. if (xhci->xhc_state & XHCI_STATE_DYING)
  1247. return -ENODEV;
  1248. added_ctxs = xhci_get_endpoint_flag(&ep->desc);
  1249. last_ctx = xhci_last_valid_endpoint(added_ctxs);
  1250. if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
  1251. /* FIXME when we have to issue an evaluate endpoint command to
  1252. * deal with ep0 max packet size changing once we get the
  1253. * descriptors
  1254. */
  1255. xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
  1256. __func__, added_ctxs);
  1257. return 0;
  1258. }
  1259. in_ctx = xhci->devs[udev->slot_id]->in_ctx;
  1260. out_ctx = xhci->devs[udev->slot_id]->out_ctx;
  1261. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  1262. ep_index = xhci_get_endpoint_index(&ep->desc);
  1263. ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
  1264. /* If the HCD has already noted the endpoint is enabled,
  1265. * ignore this request.
  1266. */
  1267. if (le32_to_cpu(ctrl_ctx->add_flags) &
  1268. xhci_get_endpoint_flag(&ep->desc)) {
  1269. xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
  1270. __func__, ep);
  1271. return 0;
  1272. }
  1273. /*
  1274. * Configuration and alternate setting changes must be done in
  1275. * process context, not interrupt context (or so documenation
  1276. * for usb_set_interface() and usb_set_configuration() claim).
  1277. */
  1278. if (xhci_endpoint_init(xhci, xhci->devs[udev->slot_id],
  1279. udev, ep, GFP_NOIO) < 0) {
  1280. dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
  1281. __func__, ep->desc.bEndpointAddress);
  1282. return -ENOMEM;
  1283. }
  1284. ctrl_ctx->add_flags |= cpu_to_le32(added_ctxs);
  1285. new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
  1286. /* If xhci_endpoint_disable() was called for this endpoint, but the
  1287. * xHC hasn't been notified yet through the check_bandwidth() call,
  1288. * this re-adds a new state for the endpoint from the new endpoint
  1289. * descriptors. We must drop and re-add this endpoint, so we leave the
  1290. * drop flags alone.
  1291. */
  1292. new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
  1293. slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
  1294. /* Update the last valid endpoint context, if we just added one past */
  1295. if ((le32_to_cpu(slot_ctx->dev_info) & LAST_CTX_MASK) <
  1296. LAST_CTX(last_ctx)) {
  1297. slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
  1298. slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(last_ctx));
  1299. }
  1300. new_slot_info = le32_to_cpu(slot_ctx->dev_info);
  1301. /* Store the usb_device pointer for later use */
  1302. ep->hcpriv = udev;
  1303. xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
  1304. (unsigned int) ep->desc.bEndpointAddress,
  1305. udev->slot_id,
  1306. (unsigned int) new_drop_flags,
  1307. (unsigned int) new_add_flags,
  1308. (unsigned int) new_slot_info);
  1309. return 0;
  1310. }
  1311. static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev)
  1312. {
  1313. struct xhci_input_control_ctx *ctrl_ctx;
  1314. struct xhci_ep_ctx *ep_ctx;
  1315. struct xhci_slot_ctx *slot_ctx;
  1316. int i;
  1317. /* When a device's add flag and drop flag are zero, any subsequent
  1318. * configure endpoint command will leave that endpoint's state
  1319. * untouched. Make sure we don't leave any old state in the input
  1320. * endpoint contexts.
  1321. */
  1322. ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
  1323. ctrl_ctx->drop_flags = 0;
  1324. ctrl_ctx->add_flags = 0;
  1325. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
  1326. slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
  1327. /* Endpoint 0 is always valid */
  1328. slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
  1329. for (i = 1; i < 31; ++i) {
  1330. ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
  1331. ep_ctx->ep_info = 0;
  1332. ep_ctx->ep_info2 = 0;
  1333. ep_ctx->deq = 0;
  1334. ep_ctx->tx_info = 0;
  1335. }
  1336. }
  1337. static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
  1338. struct usb_device *udev, u32 *cmd_status)
  1339. {
  1340. int ret;
  1341. switch (*cmd_status) {
  1342. case COMP_ENOMEM:
  1343. dev_warn(&udev->dev, "Not enough host controller resources "
  1344. "for new device state.\n");
  1345. ret = -ENOMEM;
  1346. /* FIXME: can we allocate more resources for the HC? */
  1347. break;
  1348. case COMP_BW_ERR:
  1349. dev_warn(&udev->dev, "Not enough bandwidth "
  1350. "for new device state.\n");
  1351. ret = -ENOSPC;
  1352. /* FIXME: can we go back to the old state? */
  1353. break;
  1354. case COMP_TRB_ERR:
  1355. /* the HCD set up something wrong */
  1356. dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
  1357. "add flag = 1, "
  1358. "and endpoint is not disabled.\n");
  1359. ret = -EINVAL;
  1360. break;
  1361. case COMP_SUCCESS:
  1362. dev_dbg(&udev->dev, "Successful Endpoint Configure command\n");
  1363. ret = 0;
  1364. break;
  1365. default:
  1366. xhci_err(xhci, "ERROR: unexpected command completion "
  1367. "code 0x%x.\n", *cmd_status);
  1368. ret = -EINVAL;
  1369. break;
  1370. }
  1371. return ret;
  1372. }
  1373. static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
  1374. struct usb_device *udev, u32 *cmd_status)
  1375. {
  1376. int ret;
  1377. struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
  1378. switch (*cmd_status) {
  1379. case COMP_EINVAL:
  1380. dev_warn(&udev->dev, "WARN: xHCI driver setup invalid evaluate "
  1381. "context command.\n");
  1382. ret = -EINVAL;
  1383. break;
  1384. case COMP_EBADSLT:
  1385. dev_warn(&udev->dev, "WARN: slot not enabled for"
  1386. "evaluate context command.\n");
  1387. case COMP_CTX_STATE:
  1388. dev_warn(&udev->dev, "WARN: invalid context state for "
  1389. "evaluate context command.\n");
  1390. xhci_dbg_ctx(xhci, virt_dev->out_ctx, 1);
  1391. ret = -EINVAL;
  1392. break;
  1393. case COMP_MEL_ERR:
  1394. /* Max Exit Latency too large error */
  1395. dev_warn(&udev->dev, "WARN: Max Exit Latency too large\n");
  1396. ret = -EINVAL;
  1397. break;
  1398. case COMP_SUCCESS:
  1399. dev_dbg(&udev->dev, "Successful evaluate context command\n");
  1400. ret = 0;
  1401. break;
  1402. default:
  1403. xhci_err(xhci, "ERROR: unexpected command completion "
  1404. "code 0x%x.\n", *cmd_status);
  1405. ret = -EINVAL;
  1406. break;
  1407. }
  1408. return ret;
  1409. }
  1410. static u32 xhci_count_num_new_endpoints(struct xhci_hcd *xhci,
  1411. struct xhci_container_ctx *in_ctx)
  1412. {
  1413. struct xhci_input_control_ctx *ctrl_ctx;
  1414. u32 valid_add_flags;
  1415. u32 valid_drop_flags;
  1416. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  1417. /* Ignore the slot flag (bit 0), and the default control endpoint flag
  1418. * (bit 1). The default control endpoint is added during the Address
  1419. * Device command and is never removed until the slot is disabled.
  1420. */
  1421. valid_add_flags = ctrl_ctx->add_flags >> 2;
  1422. valid_drop_flags = ctrl_ctx->drop_flags >> 2;
  1423. /* Use hweight32 to count the number of ones in the add flags, or
  1424. * number of endpoints added. Don't count endpoints that are changed
  1425. * (both added and dropped).
  1426. */
  1427. return hweight32(valid_add_flags) -
  1428. hweight32(valid_add_flags & valid_drop_flags);
  1429. }
  1430. static unsigned int xhci_count_num_dropped_endpoints(struct xhci_hcd *xhci,
  1431. struct xhci_container_ctx *in_ctx)
  1432. {
  1433. struct xhci_input_control_ctx *ctrl_ctx;
  1434. u32 valid_add_flags;
  1435. u32 valid_drop_flags;
  1436. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  1437. valid_add_flags = ctrl_ctx->add_flags >> 2;
  1438. valid_drop_flags = ctrl_ctx->drop_flags >> 2;
  1439. return hweight32(valid_drop_flags) -
  1440. hweight32(valid_add_flags & valid_drop_flags);
  1441. }
  1442. /*
  1443. * We need to reserve the new number of endpoints before the configure endpoint
  1444. * command completes. We can't subtract the dropped endpoints from the number
  1445. * of active endpoints until the command completes because we can oversubscribe
  1446. * the host in this case:
  1447. *
  1448. * - the first configure endpoint command drops more endpoints than it adds
  1449. * - a second configure endpoint command that adds more endpoints is queued
  1450. * - the first configure endpoint command fails, so the config is unchanged
  1451. * - the second command may succeed, even though there isn't enough resources
  1452. *
  1453. * Must be called with xhci->lock held.
  1454. */
  1455. static int xhci_reserve_host_resources(struct xhci_hcd *xhci,
  1456. struct xhci_container_ctx *in_ctx)
  1457. {
  1458. u32 added_eps;
  1459. added_eps = xhci_count_num_new_endpoints(xhci, in_ctx);
  1460. if (xhci->num_active_eps + added_eps > xhci->limit_active_eps) {
  1461. xhci_dbg(xhci, "Not enough ep ctxs: "
  1462. "%u active, need to add %u, limit is %u.\n",
  1463. xhci->num_active_eps, added_eps,
  1464. xhci->limit_active_eps);
  1465. return -ENOMEM;
  1466. }
  1467. xhci->num_active_eps += added_eps;
  1468. xhci_dbg(xhci, "Adding %u ep ctxs, %u now active.\n", added_eps,
  1469. xhci->num_active_eps);
  1470. return 0;
  1471. }
  1472. /*
  1473. * The configure endpoint was failed by the xHC for some other reason, so we
  1474. * need to revert the resources that failed configuration would have used.
  1475. *
  1476. * Must be called with xhci->lock held.
  1477. */
  1478. static void xhci_free_host_resources(struct xhci_hcd *xhci,
  1479. struct xhci_container_ctx *in_ctx)
  1480. {
  1481. u32 num_failed_eps;
  1482. num_failed_eps = xhci_count_num_new_endpoints(xhci, in_ctx);
  1483. xhci->num_active_eps -= num_failed_eps;
  1484. xhci_dbg(xhci, "Removing %u failed ep ctxs, %u now active.\n",
  1485. num_failed_eps,
  1486. xhci->num_active_eps);
  1487. }
  1488. /*
  1489. * Now that the command has completed, clean up the active endpoint count by
  1490. * subtracting out the endpoints that were dropped (but not changed).
  1491. *
  1492. * Must be called with xhci->lock held.
  1493. */
  1494. static void xhci_finish_resource_reservation(struct xhci_hcd *xhci,
  1495. struct xhci_container_ctx *in_ctx)
  1496. {
  1497. u32 num_dropped_eps;
  1498. num_dropped_eps = xhci_count_num_dropped_endpoints(xhci, in_ctx);
  1499. xhci->num_active_eps -= num_dropped_eps;
  1500. if (num_dropped_eps)
  1501. xhci_dbg(xhci, "Removing %u dropped ep ctxs, %u now active.\n",
  1502. num_dropped_eps,
  1503. xhci->num_active_eps);
  1504. }
  1505. /* Issue a configure endpoint command or evaluate context command
  1506. * and wait for it to finish.
  1507. */
  1508. static int xhci_configure_endpoint(struct xhci_hcd *xhci,
  1509. struct usb_device *udev,
  1510. struct xhci_command *command,
  1511. bool ctx_change, bool must_succeed)
  1512. {
  1513. int ret;
  1514. int timeleft;
  1515. unsigned long flags;
  1516. struct xhci_container_ctx *in_ctx;
  1517. struct completion *cmd_completion;
  1518. u32 *cmd_status;
  1519. struct xhci_virt_device *virt_dev;
  1520. spin_lock_irqsave(&xhci->lock, flags);
  1521. virt_dev = xhci->devs[udev->slot_id];
  1522. if (command) {
  1523. in_ctx = command->in_ctx;
  1524. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK) &&
  1525. xhci_reserve_host_resources(xhci, in_ctx)) {
  1526. spin_unlock_irqrestore(&xhci->lock, flags);
  1527. xhci_warn(xhci, "Not enough host resources, "
  1528. "active endpoint contexts = %u\n",
  1529. xhci->num_active_eps);
  1530. return -ENOMEM;
  1531. }
  1532. cmd_completion = command->completion;
  1533. cmd_status = &command->status;
  1534. command->command_trb = xhci->cmd_ring->enqueue;
  1535. /* Enqueue pointer can be left pointing to the link TRB,
  1536. * we must handle that
  1537. */
  1538. if ((le32_to_cpu(command->command_trb->link.control)
  1539. & TRB_TYPE_BITMASK) == TRB_TYPE(TRB_LINK))
  1540. command->command_trb =
  1541. xhci->cmd_ring->enq_seg->next->trbs;
  1542. list_add_tail(&command->cmd_list, &virt_dev->cmd_list);
  1543. } else {
  1544. in_ctx = virt_dev->in_ctx;
  1545. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK) &&
  1546. xhci_reserve_host_resources(xhci, in_ctx)) {
  1547. spin_unlock_irqrestore(&xhci->lock, flags);
  1548. xhci_warn(xhci, "Not enough host resources, "
  1549. "active endpoint contexts = %u\n",
  1550. xhci->num_active_eps);
  1551. return -ENOMEM;
  1552. }
  1553. cmd_completion = &virt_dev->cmd_completion;
  1554. cmd_status = &virt_dev->cmd_status;
  1555. }
  1556. init_completion(cmd_completion);
  1557. if (!ctx_change)
  1558. ret = xhci_queue_configure_endpoint(xhci, in_ctx->dma,
  1559. udev->slot_id, must_succeed);
  1560. else
  1561. ret = xhci_queue_evaluate_context(xhci, in_ctx->dma,
  1562. udev->slot_id);
  1563. if (ret < 0) {
  1564. if (command)
  1565. list_del(&command->cmd_list);
  1566. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
  1567. xhci_free_host_resources(xhci, in_ctx);
  1568. spin_unlock_irqrestore(&xhci->lock, flags);
  1569. xhci_dbg(xhci, "FIXME allocate a new ring segment\n");
  1570. return -ENOMEM;
  1571. }
  1572. xhci_ring_cmd_db(xhci);
  1573. spin_unlock_irqrestore(&xhci->lock, flags);
  1574. /* Wait for the configure endpoint command to complete */
  1575. timeleft = wait_for_completion_interruptible_timeout(
  1576. cmd_completion,
  1577. USB_CTRL_SET_TIMEOUT);
  1578. if (timeleft <= 0) {
  1579. xhci_warn(xhci, "%s while waiting for %s command\n",
  1580. timeleft == 0 ? "Timeout" : "Signal",
  1581. ctx_change == 0 ?
  1582. "configure endpoint" :
  1583. "evaluate context");
  1584. /* FIXME cancel the configure endpoint command */
  1585. return -ETIME;
  1586. }
  1587. if (!ctx_change)
  1588. ret = xhci_configure_endpoint_result(xhci, udev, cmd_status);
  1589. else
  1590. ret = xhci_evaluate_context_result(xhci, udev, cmd_status);
  1591. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
  1592. spin_lock_irqsave(&xhci->lock, flags);
  1593. /* If the command failed, remove the reserved resources.
  1594. * Otherwise, clean up the estimate to include dropped eps.
  1595. */
  1596. if (ret)
  1597. xhci_free_host_resources(xhci, in_ctx);
  1598. else
  1599. xhci_finish_resource_reservation(xhci, in_ctx);
  1600. spin_unlock_irqrestore(&xhci->lock, flags);
  1601. }
  1602. return ret;
  1603. }
  1604. /* Called after one or more calls to xhci_add_endpoint() or
  1605. * xhci_drop_endpoint(). If this call fails, the USB core is expected
  1606. * to call xhci_reset_bandwidth().
  1607. *
  1608. * Since we are in the middle of changing either configuration or
  1609. * installing a new alt setting, the USB core won't allow URBs to be
  1610. * enqueued for any endpoint on the old config or interface. Nothing
  1611. * else should be touching the xhci->devs[slot_id] structure, so we
  1612. * don't need to take the xhci->lock for manipulating that.
  1613. */
  1614. int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
  1615. {
  1616. int i;
  1617. int ret = 0;
  1618. struct xhci_hcd *xhci;
  1619. struct xhci_virt_device *virt_dev;
  1620. struct xhci_input_control_ctx *ctrl_ctx;
  1621. struct xhci_slot_ctx *slot_ctx;
  1622. ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
  1623. if (ret <= 0)
  1624. return ret;
  1625. xhci = hcd_to_xhci(hcd);
  1626. if (xhci->xhc_state & XHCI_STATE_DYING)
  1627. return -ENODEV;
  1628. xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
  1629. virt_dev = xhci->devs[udev->slot_id];
  1630. /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
  1631. ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
  1632. ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
  1633. ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG);
  1634. ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG));
  1635. xhci_dbg(xhci, "New Input Control Context:\n");
  1636. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
  1637. xhci_dbg_ctx(xhci, virt_dev->in_ctx,
  1638. LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx->dev_info)));
  1639. ret = xhci_configure_endpoint(xhci, udev, NULL,
  1640. false, false);
  1641. if (ret) {
  1642. /* Callee should call reset_bandwidth() */
  1643. return ret;
  1644. }
  1645. xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
  1646. xhci_dbg_ctx(xhci, virt_dev->out_ctx,
  1647. LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx->dev_info)));
  1648. /* Free any rings that were dropped, but not changed. */
  1649. for (i = 1; i < 31; ++i) {
  1650. if ((le32_to_cpu(ctrl_ctx->drop_flags) & (1 << (i + 1))) &&
  1651. !(le32_to_cpu(ctrl_ctx->add_flags) & (1 << (i + 1))))
  1652. xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
  1653. }
  1654. xhci_zero_in_ctx(xhci, virt_dev);
  1655. /*
  1656. * Install any rings for completely new endpoints or changed endpoints,
  1657. * and free or cache any old rings from changed endpoints.
  1658. */
  1659. for (i = 1; i < 31; ++i) {
  1660. if (!virt_dev->eps[i].new_ring)
  1661. continue;
  1662. /* Only cache or free the old ring if it exists.
  1663. * It may not if this is the first add of an endpoint.
  1664. */
  1665. if (virt_dev->eps[i].ring) {
  1666. xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
  1667. }
  1668. virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
  1669. virt_dev->eps[i].new_ring = NULL;
  1670. }
  1671. return ret;
  1672. }
  1673. void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
  1674. {
  1675. struct xhci_hcd *xhci;
  1676. struct xhci_virt_device *virt_dev;
  1677. int i, ret;
  1678. ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
  1679. if (ret <= 0)
  1680. return;
  1681. xhci = hcd_to_xhci(hcd);
  1682. xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
  1683. virt_dev = xhci->devs[udev->slot_id];
  1684. /* Free any rings allocated for added endpoints */
  1685. for (i = 0; i < 31; ++i) {
  1686. if (virt_dev->eps[i].new_ring) {
  1687. xhci_ring_free(xhci, virt_dev->eps[i].new_ring);
  1688. virt_dev->eps[i].new_ring = NULL;
  1689. }
  1690. }
  1691. xhci_zero_in_ctx(xhci, virt_dev);
  1692. }
  1693. static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci,
  1694. struct xhci_container_ctx *in_ctx,
  1695. struct xhci_container_ctx *out_ctx,
  1696. u32 add_flags, u32 drop_flags)
  1697. {
  1698. struct xhci_input_control_ctx *ctrl_ctx;
  1699. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  1700. ctrl_ctx->add_flags = cpu_to_le32(add_flags);
  1701. ctrl_ctx->drop_flags = cpu_to_le32(drop_flags);
  1702. xhci_slot_copy(xhci, in_ctx, out_ctx);
  1703. ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
  1704. xhci_dbg(xhci, "Input Context:\n");
  1705. xhci_dbg_ctx(xhci, in_ctx, xhci_last_valid_endpoint(add_flags));
  1706. }
  1707. static void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci,
  1708. unsigned int slot_id, unsigned int ep_index,
  1709. struct xhci_dequeue_state *deq_state)
  1710. {
  1711. struct xhci_container_ctx *in_ctx;
  1712. struct xhci_ep_ctx *ep_ctx;
  1713. u32 added_ctxs;
  1714. dma_addr_t addr;
  1715. xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
  1716. xhci->devs[slot_id]->out_ctx, ep_index);
  1717. in_ctx = xhci->devs[slot_id]->in_ctx;
  1718. ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
  1719. addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
  1720. deq_state->new_deq_ptr);
  1721. if (addr == 0) {
  1722. xhci_warn(xhci, "WARN Cannot submit config ep after "
  1723. "reset ep command\n");
  1724. xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n",
  1725. deq_state->new_deq_seg,
  1726. deq_state->new_deq_ptr);
  1727. return;
  1728. }
  1729. ep_ctx->deq = cpu_to_le64(addr | deq_state->new_cycle_state);
  1730. added_ctxs = xhci_get_endpoint_flag_from_index(ep_index);
  1731. xhci_setup_input_ctx_for_config_ep(xhci, xhci->devs[slot_id]->in_ctx,
  1732. xhci->devs[slot_id]->out_ctx, added_ctxs, added_ctxs);
  1733. }
  1734. void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
  1735. struct usb_device *udev, unsigned int ep_index)
  1736. {
  1737. struct xhci_dequeue_state deq_state;
  1738. struct xhci_virt_ep *ep;
  1739. xhci_dbg(xhci, "Cleaning up stalled endpoint ring\n");
  1740. ep = &xhci->devs[udev->slot_id]->eps[ep_index];
  1741. /* We need to move the HW's dequeue pointer past this TD,
  1742. * or it will attempt to resend it on the next doorbell ring.
  1743. */
  1744. xhci_find_new_dequeue_state(xhci, udev->slot_id,
  1745. ep_index, ep->stopped_stream, ep->stopped_td,
  1746. &deq_state);
  1747. /* HW with the reset endpoint quirk will use the saved dequeue state to
  1748. * issue a configure endpoint command later.
  1749. */
  1750. if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) {
  1751. xhci_dbg(xhci, "Queueing new dequeue state\n");
  1752. xhci_queue_new_dequeue_state(xhci, udev->slot_id,
  1753. ep_index, ep->stopped_stream, &deq_state);
  1754. } else {
  1755. /* Better hope no one uses the input context between now and the
  1756. * reset endpoint completion!
  1757. * XXX: No idea how this hardware will react when stream rings
  1758. * are enabled.
  1759. */
  1760. xhci_dbg(xhci, "Setting up input context for "
  1761. "configure endpoint command\n");
  1762. xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id,
  1763. ep_index, &deq_state);
  1764. }
  1765. }
  1766. /* Deal with stalled endpoints. The core should have sent the control message
  1767. * to clear the halt condition. However, we need to make the xHCI hardware
  1768. * reset its sequence number, since a device will expect a sequence number of
  1769. * zero after the halt condition is cleared.
  1770. * Context: in_interrupt
  1771. */
  1772. void xhci_endpoint_reset(struct usb_hcd *hcd,
  1773. struct usb_host_endpoint *ep)
  1774. {
  1775. struct xhci_hcd *xhci;
  1776. struct usb_device *udev;
  1777. unsigned int ep_index;
  1778. unsigned long flags;
  1779. int ret;
  1780. struct xhci_virt_ep *virt_ep;
  1781. xhci = hcd_to_xhci(hcd);
  1782. udev = (struct usb_device *) ep->hcpriv;
  1783. /* Called with a root hub endpoint (or an endpoint that wasn't added
  1784. * with xhci_add_endpoint()
  1785. */
  1786. if (!ep->hcpriv)
  1787. return;
  1788. ep_index = xhci_get_endpoint_index(&ep->desc);
  1789. virt_ep = &xhci->devs[udev->slot_id]->eps[ep_index];
  1790. if (!virt_ep->stopped_td) {
  1791. xhci_dbg(xhci, "Endpoint 0x%x not halted, refusing to reset.\n",
  1792. ep->desc.bEndpointAddress);
  1793. return;
  1794. }
  1795. if (usb_endpoint_xfer_control(&ep->desc)) {
  1796. xhci_dbg(xhci, "Control endpoint stall already handled.\n");
  1797. return;
  1798. }
  1799. xhci_dbg(xhci, "Queueing reset endpoint command\n");
  1800. spin_lock_irqsave(&xhci->lock, flags);
  1801. ret = xhci_queue_reset_ep(xhci, udev->slot_id, ep_index);
  1802. /*
  1803. * Can't change the ring dequeue pointer until it's transitioned to the
  1804. * stopped state, which is only upon a successful reset endpoint
  1805. * command. Better hope that last command worked!
  1806. */
  1807. if (!ret) {
  1808. xhci_cleanup_stalled_ring(xhci, udev, ep_index);
  1809. kfree(virt_ep->stopped_td);
  1810. xhci_ring_cmd_db(xhci);
  1811. }
  1812. virt_ep->stopped_td = NULL;
  1813. virt_ep->stopped_trb = NULL;
  1814. virt_ep->stopped_stream = 0;
  1815. spin_unlock_irqrestore(&xhci->lock, flags);
  1816. if (ret)
  1817. xhci_warn(xhci, "FIXME allocate a new ring segment\n");
  1818. }
  1819. static int xhci_check_streams_endpoint(struct xhci_hcd *xhci,
  1820. struct usb_device *udev, struct usb_host_endpoint *ep,
  1821. unsigned int slot_id)
  1822. {
  1823. int ret;
  1824. unsigned int ep_index;
  1825. unsigned int ep_state;
  1826. if (!ep)
  1827. return -EINVAL;
  1828. ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, true, __func__);
  1829. if (ret <= 0)
  1830. return -EINVAL;
  1831. if (ep->ss_ep_comp.bmAttributes == 0) {
  1832. xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion"
  1833. " descriptor for ep 0x%x does not support streams\n",
  1834. ep->desc.bEndpointAddress);
  1835. return -EINVAL;
  1836. }
  1837. ep_index = xhci_get_endpoint_index(&ep->desc);
  1838. ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
  1839. if (ep_state & EP_HAS_STREAMS ||
  1840. ep_state & EP_GETTING_STREAMS) {
  1841. xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x "
  1842. "already has streams set up.\n",
  1843. ep->desc.bEndpointAddress);
  1844. xhci_warn(xhci, "Send email to xHCI maintainer and ask for "
  1845. "dynamic stream context array reallocation.\n");
  1846. return -EINVAL;
  1847. }
  1848. if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) {
  1849. xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk "
  1850. "endpoint 0x%x; URBs are pending.\n",
  1851. ep->desc.bEndpointAddress);
  1852. return -EINVAL;
  1853. }
  1854. return 0;
  1855. }
  1856. static void xhci_calculate_streams_entries(struct xhci_hcd *xhci,
  1857. unsigned int *num_streams, unsigned int *num_stream_ctxs)
  1858. {
  1859. unsigned int max_streams;
  1860. /* The stream context array size must be a power of two */
  1861. *num_stream_ctxs = roundup_pow_of_two(*num_streams);
  1862. /*
  1863. * Find out how many primary stream array entries the host controller
  1864. * supports. Later we may use secondary stream arrays (similar to 2nd
  1865. * level page entries), but that's an optional feature for xHCI host
  1866. * controllers. xHCs must support at least 4 stream IDs.
  1867. */
  1868. max_streams = HCC_MAX_PSA(xhci->hcc_params);
  1869. if (*num_stream_ctxs > max_streams) {
  1870. xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n",
  1871. max_streams);
  1872. *num_stream_ctxs = max_streams;
  1873. *num_streams = max_streams;
  1874. }
  1875. }
  1876. /* Returns an error code if one of the endpoint already has streams.
  1877. * This does not change any data structures, it only checks and gathers
  1878. * information.
  1879. */
  1880. static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci,
  1881. struct usb_device *udev,
  1882. struct usb_host_endpoint **eps, unsigned int num_eps,
  1883. unsigned int *num_streams, u32 *changed_ep_bitmask)
  1884. {
  1885. unsigned int max_streams;
  1886. unsigned int endpoint_flag;
  1887. int i;
  1888. int ret;
  1889. for (i = 0; i < num_eps; i++) {
  1890. ret = xhci_check_streams_endpoint(xhci, udev,
  1891. eps[i], udev->slot_id);
  1892. if (ret < 0)
  1893. return ret;
  1894. max_streams = USB_SS_MAX_STREAMS(
  1895. eps[i]->ss_ep_comp.bmAttributes);
  1896. if (max_streams < (*num_streams - 1)) {
  1897. xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n",
  1898. eps[i]->desc.bEndpointAddress,
  1899. max_streams);
  1900. *num_streams = max_streams+1;
  1901. }
  1902. endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc);
  1903. if (*changed_ep_bitmask & endpoint_flag)
  1904. return -EINVAL;
  1905. *changed_ep_bitmask |= endpoint_flag;
  1906. }
  1907. return 0;
  1908. }
  1909. static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci,
  1910. struct usb_device *udev,
  1911. struct usb_host_endpoint **eps, unsigned int num_eps)
  1912. {
  1913. u32 changed_ep_bitmask = 0;
  1914. unsigned int slot_id;
  1915. unsigned int ep_index;
  1916. unsigned int ep_state;
  1917. int i;
  1918. slot_id = udev->slot_id;
  1919. if (!xhci->devs[slot_id])
  1920. return 0;
  1921. for (i = 0; i < num_eps; i++) {
  1922. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  1923. ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
  1924. /* Are streams already being freed for the endpoint? */
  1925. if (ep_state & EP_GETTING_NO_STREAMS) {
  1926. xhci_warn(xhci, "WARN Can't disable streams for "
  1927. "endpoint 0x%x\n, "
  1928. "streams are being disabled already.",
  1929. eps[i]->desc.bEndpointAddress);
  1930. return 0;
  1931. }
  1932. /* Are there actually any streams to free? */
  1933. if (!(ep_state & EP_HAS_STREAMS) &&
  1934. !(ep_state & EP_GETTING_STREAMS)) {
  1935. xhci_warn(xhci, "WARN Can't disable streams for "
  1936. "endpoint 0x%x\n, "
  1937. "streams are already disabled!",
  1938. eps[i]->desc.bEndpointAddress);
  1939. xhci_warn(xhci, "WARN xhci_free_streams() called "
  1940. "with non-streams endpoint\n");
  1941. return 0;
  1942. }
  1943. changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc);
  1944. }
  1945. return changed_ep_bitmask;
  1946. }
  1947. /*
  1948. * The USB device drivers use this function (though the HCD interface in USB
  1949. * core) to prepare a set of bulk endpoints to use streams. Streams are used to
  1950. * coordinate mass storage command queueing across multiple endpoints (basically
  1951. * a stream ID == a task ID).
  1952. *
  1953. * Setting up streams involves allocating the same size stream context array
  1954. * for each endpoint and issuing a configure endpoint command for all endpoints.
  1955. *
  1956. * Don't allow the call to succeed if one endpoint only supports one stream
  1957. * (which means it doesn't support streams at all).
  1958. *
  1959. * Drivers may get less stream IDs than they asked for, if the host controller
  1960. * hardware or endpoints claim they can't support the number of requested
  1961. * stream IDs.
  1962. */
  1963. int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
  1964. struct usb_host_endpoint **eps, unsigned int num_eps,
  1965. unsigned int num_streams, gfp_t mem_flags)
  1966. {
  1967. int i, ret;
  1968. struct xhci_hcd *xhci;
  1969. struct xhci_virt_device *vdev;
  1970. struct xhci_command *config_cmd;
  1971. unsigned int ep_index;
  1972. unsigned int num_stream_ctxs;
  1973. unsigned long flags;
  1974. u32 changed_ep_bitmask = 0;
  1975. if (!eps)
  1976. return -EINVAL;
  1977. /* Add one to the number of streams requested to account for
  1978. * stream 0 that is reserved for xHCI usage.
  1979. */
  1980. num_streams += 1;
  1981. xhci = hcd_to_xhci(hcd);
  1982. xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n",
  1983. num_streams);
  1984. config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
  1985. if (!config_cmd) {
  1986. xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
  1987. return -ENOMEM;
  1988. }
  1989. /* Check to make sure all endpoints are not already configured for
  1990. * streams. While we're at it, find the maximum number of streams that
  1991. * all the endpoints will support and check for duplicate endpoints.
  1992. */
  1993. spin_lock_irqsave(&xhci->lock, flags);
  1994. ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps,
  1995. num_eps, &num_streams, &changed_ep_bitmask);
  1996. if (ret < 0) {
  1997. xhci_free_command(xhci, config_cmd);
  1998. spin_unlock_irqrestore(&xhci->lock, flags);
  1999. return ret;
  2000. }
  2001. if (num_streams <= 1) {
  2002. xhci_warn(xhci, "WARN: endpoints can't handle "
  2003. "more than one stream.\n");
  2004. xhci_free_command(xhci, config_cmd);
  2005. spin_unlock_irqrestore(&xhci->lock, flags);
  2006. return -EINVAL;
  2007. }
  2008. vdev = xhci->devs[udev->slot_id];
  2009. /* Mark each endpoint as being in transition, so
  2010. * xhci_urb_enqueue() will reject all URBs.
  2011. */
  2012. for (i = 0; i < num_eps; i++) {
  2013. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2014. vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS;
  2015. }
  2016. spin_unlock_irqrestore(&xhci->lock, flags);
  2017. /* Setup internal data structures and allocate HW data structures for
  2018. * streams (but don't install the HW structures in the input context
  2019. * until we're sure all memory allocation succeeded).
  2020. */
  2021. xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs);
  2022. xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n",
  2023. num_stream_ctxs, num_streams);
  2024. for (i = 0; i < num_eps; i++) {
  2025. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2026. vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci,
  2027. num_stream_ctxs,
  2028. num_streams, mem_flags);
  2029. if (!vdev->eps[ep_index].stream_info)
  2030. goto cleanup;
  2031. /* Set maxPstreams in endpoint context and update deq ptr to
  2032. * point to stream context array. FIXME
  2033. */
  2034. }
  2035. /* Set up the input context for a configure endpoint command. */
  2036. for (i = 0; i < num_eps; i++) {
  2037. struct xhci_ep_ctx *ep_ctx;
  2038. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2039. ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index);
  2040. xhci_endpoint_copy(xhci, config_cmd->in_ctx,
  2041. vdev->out_ctx, ep_index);
  2042. xhci_setup_streams_ep_input_ctx(xhci, ep_ctx,
  2043. vdev->eps[ep_index].stream_info);
  2044. }
  2045. /* Tell the HW to drop its old copy of the endpoint context info
  2046. * and add the updated copy from the input context.
  2047. */
  2048. xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx,
  2049. vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
  2050. /* Issue and wait for the configure endpoint command */
  2051. ret = xhci_configure_endpoint(xhci, udev, config_cmd,
  2052. false, false);
  2053. /* xHC rejected the configure endpoint command for some reason, so we
  2054. * leave the old ring intact and free our internal streams data
  2055. * structure.
  2056. */
  2057. if (ret < 0)
  2058. goto cleanup;
  2059. spin_lock_irqsave(&xhci->lock, flags);
  2060. for (i = 0; i < num_eps; i++) {
  2061. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2062. vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
  2063. xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n",
  2064. udev->slot_id, ep_index);
  2065. vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS;
  2066. }
  2067. xhci_free_command(xhci, config_cmd);
  2068. spin_unlock_irqrestore(&xhci->lock, flags);
  2069. /* Subtract 1 for stream 0, which drivers can't use */
  2070. return num_streams - 1;
  2071. cleanup:
  2072. /* If it didn't work, free the streams! */
  2073. for (i = 0; i < num_eps; i++) {
  2074. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2075. xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
  2076. vdev->eps[ep_index].stream_info = NULL;
  2077. /* FIXME Unset maxPstreams in endpoint context and
  2078. * update deq ptr to point to normal string ring.
  2079. */
  2080. vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
  2081. vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
  2082. xhci_endpoint_zero(xhci, vdev, eps[i]);
  2083. }
  2084. xhci_free_command(xhci, config_cmd);
  2085. return -ENOMEM;
  2086. }
  2087. /* Transition the endpoint from using streams to being a "normal" endpoint
  2088. * without streams.
  2089. *
  2090. * Modify the endpoint context state, submit a configure endpoint command,
  2091. * and free all endpoint rings for streams if that completes successfully.
  2092. */
  2093. int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
  2094. struct usb_host_endpoint **eps, unsigned int num_eps,
  2095. gfp_t mem_flags)
  2096. {
  2097. int i, ret;
  2098. struct xhci_hcd *xhci;
  2099. struct xhci_virt_device *vdev;
  2100. struct xhci_command *command;
  2101. unsigned int ep_index;
  2102. unsigned long flags;
  2103. u32 changed_ep_bitmask;
  2104. xhci = hcd_to_xhci(hcd);
  2105. vdev = xhci->devs[udev->slot_id];
  2106. /* Set up a configure endpoint command to remove the streams rings */
  2107. spin_lock_irqsave(&xhci->lock, flags);
  2108. changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci,
  2109. udev, eps, num_eps);
  2110. if (changed_ep_bitmask == 0) {
  2111. spin_unlock_irqrestore(&xhci->lock, flags);
  2112. return -EINVAL;
  2113. }
  2114. /* Use the xhci_command structure from the first endpoint. We may have
  2115. * allocated too many, but the driver may call xhci_free_streams() for
  2116. * each endpoint it grouped into one call to xhci_alloc_streams().
  2117. */
  2118. ep_index = xhci_get_endpoint_index(&eps[0]->desc);
  2119. command = vdev->eps[ep_index].stream_info->free_streams_command;
  2120. for (i = 0; i < num_eps; i++) {
  2121. struct xhci_ep_ctx *ep_ctx;
  2122. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2123. ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
  2124. xhci->devs[udev->slot_id]->eps[ep_index].ep_state |=
  2125. EP_GETTING_NO_STREAMS;
  2126. xhci_endpoint_copy(xhci, command->in_ctx,
  2127. vdev->out_ctx, ep_index);
  2128. xhci_setup_no_streams_ep_input_ctx(xhci, ep_ctx,
  2129. &vdev->eps[ep_index]);
  2130. }
  2131. xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx,
  2132. vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
  2133. spin_unlock_irqrestore(&xhci->lock, flags);
  2134. /* Issue and wait for the configure endpoint command,
  2135. * which must succeed.
  2136. */
  2137. ret = xhci_configure_endpoint(xhci, udev, command,
  2138. false, true);
  2139. /* xHC rejected the configure endpoint command for some reason, so we
  2140. * leave the streams rings intact.
  2141. */
  2142. if (ret < 0)
  2143. return ret;
  2144. spin_lock_irqsave(&xhci->lock, flags);
  2145. for (i = 0; i < num_eps; i++) {
  2146. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2147. xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
  2148. vdev->eps[ep_index].stream_info = NULL;
  2149. /* FIXME Unset maxPstreams in endpoint context and
  2150. * update deq ptr to point to normal string ring.
  2151. */
  2152. vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS;
  2153. vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
  2154. }
  2155. spin_unlock_irqrestore(&xhci->lock, flags);
  2156. return 0;
  2157. }
  2158. /*
  2159. * Deletes endpoint resources for endpoints that were active before a Reset
  2160. * Device command, or a Disable Slot command. The Reset Device command leaves
  2161. * the control endpoint intact, whereas the Disable Slot command deletes it.
  2162. *
  2163. * Must be called with xhci->lock held.
  2164. */
  2165. void xhci_free_device_endpoint_resources(struct xhci_hcd *xhci,
  2166. struct xhci_virt_device *virt_dev, bool drop_control_ep)
  2167. {
  2168. int i;
  2169. unsigned int num_dropped_eps = 0;
  2170. unsigned int drop_flags = 0;
  2171. for (i = (drop_control_ep ? 0 : 1); i < 31; i++) {
  2172. if (virt_dev->eps[i].ring) {
  2173. drop_flags |= 1 << i;
  2174. num_dropped_eps++;
  2175. }
  2176. }
  2177. xhci->num_active_eps -= num_dropped_eps;
  2178. if (num_dropped_eps)
  2179. xhci_dbg(xhci, "Dropped %u ep ctxs, flags = 0x%x, "
  2180. "%u now active.\n",
  2181. num_dropped_eps, drop_flags,
  2182. xhci->num_active_eps);
  2183. }
  2184. /*
  2185. * This submits a Reset Device Command, which will set the device state to 0,
  2186. * set the device address to 0, and disable all the endpoints except the default
  2187. * control endpoint. The USB core should come back and call
  2188. * xhci_address_device(), and then re-set up the configuration. If this is
  2189. * called because of a usb_reset_and_verify_device(), then the old alternate
  2190. * settings will be re-installed through the normal bandwidth allocation
  2191. * functions.
  2192. *
  2193. * Wait for the Reset Device command to finish. Remove all structures
  2194. * associated with the endpoints that were disabled. Clear the input device
  2195. * structure? Cache the rings? Reset the control endpoint 0 max packet size?
  2196. *
  2197. * If the virt_dev to be reset does not exist or does not match the udev,
  2198. * it means the device is lost, possibly due to the xHC restore error and
  2199. * re-initialization during S3/S4. In this case, call xhci_alloc_dev() to
  2200. * re-allocate the device.
  2201. */
  2202. int xhci_discover_or_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
  2203. {
  2204. int ret, i;
  2205. unsigned long flags;
  2206. struct xhci_hcd *xhci;
  2207. unsigned int slot_id;
  2208. struct xhci_virt_device *virt_dev;
  2209. struct xhci_command *reset_device_cmd;
  2210. int timeleft;
  2211. int last_freed_endpoint;
  2212. struct xhci_slot_ctx *slot_ctx;
  2213. ret = xhci_check_args(hcd, udev, NULL, 0, false, __func__);
  2214. if (ret <= 0)
  2215. return ret;
  2216. xhci = hcd_to_xhci(hcd);
  2217. slot_id = udev->slot_id;
  2218. virt_dev = xhci->devs[slot_id];
  2219. if (!virt_dev) {
  2220. xhci_dbg(xhci, "The device to be reset with slot ID %u does "
  2221. "not exist. Re-allocate the device\n", slot_id);
  2222. ret = xhci_alloc_dev(hcd, udev);
  2223. if (ret == 1)
  2224. return 0;
  2225. else
  2226. return -EINVAL;
  2227. }
  2228. if (virt_dev->udev != udev) {
  2229. /* If the virt_dev and the udev does not match, this virt_dev
  2230. * may belong to another udev.
  2231. * Re-allocate the device.
  2232. */
  2233. xhci_dbg(xhci, "The device to be reset with slot ID %u does "
  2234. "not match the udev. Re-allocate the device\n",
  2235. slot_id);
  2236. ret = xhci_alloc_dev(hcd, udev);
  2237. if (ret == 1)
  2238. return 0;
  2239. else
  2240. return -EINVAL;
  2241. }
  2242. /* If device is not setup, there is no point in resetting it */
  2243. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
  2244. if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) ==
  2245. SLOT_STATE_DISABLED)
  2246. return 0;
  2247. xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id);
  2248. /* Allocate the command structure that holds the struct completion.
  2249. * Assume we're in process context, since the normal device reset
  2250. * process has to wait for the device anyway. Storage devices are
  2251. * reset as part of error handling, so use GFP_NOIO instead of
  2252. * GFP_KERNEL.
  2253. */
  2254. reset_device_cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
  2255. if (!reset_device_cmd) {
  2256. xhci_dbg(xhci, "Couldn't allocate command structure.\n");
  2257. return -ENOMEM;
  2258. }
  2259. /* Attempt to submit the Reset Device command to the command ring */
  2260. spin_lock_irqsave(&xhci->lock, flags);
  2261. reset_device_cmd->command_trb = xhci->cmd_ring->enqueue;
  2262. /* Enqueue pointer can be left pointing to the link TRB,
  2263. * we must handle that
  2264. */
  2265. if ((le32_to_cpu(reset_device_cmd->command_trb->link.control)
  2266. & TRB_TYPE_BITMASK) == TRB_TYPE(TRB_LINK))
  2267. reset_device_cmd->command_trb =
  2268. xhci->cmd_ring->enq_seg->next->trbs;
  2269. list_add_tail(&reset_device_cmd->cmd_list, &virt_dev->cmd_list);
  2270. ret = xhci_queue_reset_device(xhci, slot_id);
  2271. if (ret) {
  2272. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  2273. list_del(&reset_device_cmd->cmd_list);
  2274. spin_unlock_irqrestore(&xhci->lock, flags);
  2275. goto command_cleanup;
  2276. }
  2277. xhci_ring_cmd_db(xhci);
  2278. spin_unlock_irqrestore(&xhci->lock, flags);
  2279. /* Wait for the Reset Device command to finish */
  2280. timeleft = wait_for_completion_interruptible_timeout(
  2281. reset_device_cmd->completion,
  2282. USB_CTRL_SET_TIMEOUT);
  2283. if (timeleft <= 0) {
  2284. xhci_warn(xhci, "%s while waiting for reset device command\n",
  2285. timeleft == 0 ? "Timeout" : "Signal");
  2286. spin_lock_irqsave(&xhci->lock, flags);
  2287. /* The timeout might have raced with the event ring handler, so
  2288. * only delete from the list if the item isn't poisoned.
  2289. */
  2290. if (reset_device_cmd->cmd_list.next != LIST_POISON1)
  2291. list_del(&reset_device_cmd->cmd_list);
  2292. spin_unlock_irqrestore(&xhci->lock, flags);
  2293. ret = -ETIME;
  2294. goto command_cleanup;
  2295. }
  2296. /* The Reset Device command can't fail, according to the 0.95/0.96 spec,
  2297. * unless we tried to reset a slot ID that wasn't enabled,
  2298. * or the device wasn't in the addressed or configured state.
  2299. */
  2300. ret = reset_device_cmd->status;
  2301. switch (ret) {
  2302. case COMP_EBADSLT: /* 0.95 completion code for bad slot ID */
  2303. case COMP_CTX_STATE: /* 0.96 completion code for same thing */
  2304. xhci_info(xhci, "Can't reset device (slot ID %u) in %s state\n",
  2305. slot_id,
  2306. xhci_get_slot_state(xhci, virt_dev->out_ctx));
  2307. xhci_info(xhci, "Not freeing device rings.\n");
  2308. /* Don't treat this as an error. May change my mind later. */
  2309. ret = 0;
  2310. goto command_cleanup;
  2311. case COMP_SUCCESS:
  2312. xhci_dbg(xhci, "Successful reset device command.\n");
  2313. break;
  2314. default:
  2315. if (xhci_is_vendor_info_code(xhci, ret))
  2316. break;
  2317. xhci_warn(xhci, "Unknown completion code %u for "
  2318. "reset device command.\n", ret);
  2319. ret = -EINVAL;
  2320. goto command_cleanup;
  2321. }
  2322. /* Free up host controller endpoint resources */
  2323. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
  2324. spin_lock_irqsave(&xhci->lock, flags);
  2325. /* Don't delete the default control endpoint resources */
  2326. xhci_free_device_endpoint_resources(xhci, virt_dev, false);
  2327. spin_unlock_irqrestore(&xhci->lock, flags);
  2328. }
  2329. /* Everything but endpoint 0 is disabled, so free or cache the rings. */
  2330. last_freed_endpoint = 1;
  2331. for (i = 1; i < 31; ++i) {
  2332. struct xhci_virt_ep *ep = &virt_dev->eps[i];
  2333. if (ep->ep_state & EP_HAS_STREAMS) {
  2334. xhci_free_stream_info(xhci, ep->stream_info);
  2335. ep->stream_info = NULL;
  2336. ep->ep_state &= ~EP_HAS_STREAMS;
  2337. }
  2338. if (ep->ring) {
  2339. xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
  2340. last_freed_endpoint = i;
  2341. }
  2342. }
  2343. xhci_dbg(xhci, "Output context after successful reset device cmd:\n");
  2344. xhci_dbg_ctx(xhci, virt_dev->out_ctx, last_freed_endpoint);
  2345. ret = 0;
  2346. command_cleanup:
  2347. xhci_free_command(xhci, reset_device_cmd);
  2348. return ret;
  2349. }
  2350. /*
  2351. * At this point, the struct usb_device is about to go away, the device has
  2352. * disconnected, and all traffic has been stopped and the endpoints have been
  2353. * disabled. Free any HC data structures associated with that device.
  2354. */
  2355. void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
  2356. {
  2357. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  2358. struct xhci_virt_device *virt_dev;
  2359. unsigned long flags;
  2360. u32 state;
  2361. int i, ret;
  2362. ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
  2363. if (ret <= 0)
  2364. return;
  2365. virt_dev = xhci->devs[udev->slot_id];
  2366. /* Stop any wayward timer functions (which may grab the lock) */
  2367. for (i = 0; i < 31; ++i) {
  2368. virt_dev->eps[i].ep_state &= ~EP_HALT_PENDING;
  2369. del_timer_sync(&virt_dev->eps[i].stop_cmd_timer);
  2370. }
  2371. spin_lock_irqsave(&xhci->lock, flags);
  2372. /* Don't disable the slot if the host controller is dead. */
  2373. state = xhci_readl(xhci, &xhci->op_regs->status);
  2374. if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING)) {
  2375. xhci_free_virt_device(xhci, udev->slot_id);
  2376. spin_unlock_irqrestore(&xhci->lock, flags);
  2377. return;
  2378. }
  2379. if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
  2380. spin_unlock_irqrestore(&xhci->lock, flags);
  2381. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  2382. return;
  2383. }
  2384. xhci_ring_cmd_db(xhci);
  2385. spin_unlock_irqrestore(&xhci->lock, flags);
  2386. /*
  2387. * Event command completion handler will free any data structures
  2388. * associated with the slot. XXX Can free sleep?
  2389. */
  2390. }
  2391. /*
  2392. * Checks if we have enough host controller resources for the default control
  2393. * endpoint.
  2394. *
  2395. * Must be called with xhci->lock held.
  2396. */
  2397. static int xhci_reserve_host_control_ep_resources(struct xhci_hcd *xhci)
  2398. {
  2399. if (xhci->num_active_eps + 1 > xhci->limit_active_eps) {
  2400. xhci_dbg(xhci, "Not enough ep ctxs: "
  2401. "%u active, need to add 1, limit is %u.\n",
  2402. xhci->num_active_eps, xhci->limit_active_eps);
  2403. return -ENOMEM;
  2404. }
  2405. xhci->num_active_eps += 1;
  2406. xhci_dbg(xhci, "Adding 1 ep ctx, %u now active.\n",
  2407. xhci->num_active_eps);
  2408. return 0;
  2409. }
  2410. /*
  2411. * Returns 0 if the xHC ran out of device slots, the Enable Slot command
  2412. * timed out, or allocating memory failed. Returns 1 on success.
  2413. */
  2414. int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
  2415. {
  2416. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  2417. unsigned long flags;
  2418. int timeleft;
  2419. int ret;
  2420. spin_lock_irqsave(&xhci->lock, flags);
  2421. ret = xhci_queue_slot_control(xhci, TRB_ENABLE_SLOT, 0);
  2422. if (ret) {
  2423. spin_unlock_irqrestore(&xhci->lock, flags);
  2424. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  2425. return 0;
  2426. }
  2427. xhci_ring_cmd_db(xhci);
  2428. spin_unlock_irqrestore(&xhci->lock, flags);
  2429. /* XXX: how much time for xHC slot assignment? */
  2430. timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
  2431. USB_CTRL_SET_TIMEOUT);
  2432. if (timeleft <= 0) {
  2433. xhci_warn(xhci, "%s while waiting for a slot\n",
  2434. timeleft == 0 ? "Timeout" : "Signal");
  2435. /* FIXME cancel the enable slot request */
  2436. return 0;
  2437. }
  2438. if (!xhci->slot_id) {
  2439. xhci_err(xhci, "Error while assigning device slot ID\n");
  2440. return 0;
  2441. }
  2442. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
  2443. spin_lock_irqsave(&xhci->lock, flags);
  2444. ret = xhci_reserve_host_control_ep_resources(xhci);
  2445. if (ret) {
  2446. spin_unlock_irqrestore(&xhci->lock, flags);
  2447. xhci_warn(xhci, "Not enough host resources, "
  2448. "active endpoint contexts = %u\n",
  2449. xhci->num_active_eps);
  2450. goto disable_slot;
  2451. }
  2452. spin_unlock_irqrestore(&xhci->lock, flags);
  2453. }
  2454. /* Use GFP_NOIO, since this function can be called from
  2455. * xhci_discover_or_reset_device(), which may be called as part of
  2456. * mass storage driver error handling.
  2457. */
  2458. if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_NOIO)) {
  2459. xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
  2460. goto disable_slot;
  2461. }
  2462. udev->slot_id = xhci->slot_id;
  2463. /* Is this a LS or FS device under a HS hub? */
  2464. /* Hub or peripherial? */
  2465. return 1;
  2466. disable_slot:
  2467. /* Disable slot, if we can do it without mem alloc */
  2468. spin_lock_irqsave(&xhci->lock, flags);
  2469. if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
  2470. xhci_ring_cmd_db(xhci);
  2471. spin_unlock_irqrestore(&xhci->lock, flags);
  2472. return 0;
  2473. }
  2474. /*
  2475. * Issue an Address Device command (which will issue a SetAddress request to
  2476. * the device).
  2477. * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so
  2478. * we should only issue and wait on one address command at the same time.
  2479. *
  2480. * We add one to the device address issued by the hardware because the USB core
  2481. * uses address 1 for the root hubs (even though they're not really devices).
  2482. */
  2483. int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
  2484. {
  2485. unsigned long flags;
  2486. int timeleft;
  2487. struct xhci_virt_device *virt_dev;
  2488. int ret = 0;
  2489. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  2490. struct xhci_slot_ctx *slot_ctx;
  2491. struct xhci_input_control_ctx *ctrl_ctx;
  2492. u64 temp_64;
  2493. if (!udev->slot_id) {
  2494. xhci_dbg(xhci, "Bad Slot ID %d\n", udev->slot_id);
  2495. return -EINVAL;
  2496. }
  2497. virt_dev = xhci->devs[udev->slot_id];
  2498. if (WARN_ON(!virt_dev)) {
  2499. /*
  2500. * In plug/unplug torture test with an NEC controller,
  2501. * a zero-dereference was observed once due to virt_dev = 0.
  2502. * Print useful debug rather than crash if it is observed again!
  2503. */
  2504. xhci_warn(xhci, "Virt dev invalid for slot_id 0x%x!\n",
  2505. udev->slot_id);
  2506. return -EINVAL;
  2507. }
  2508. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
  2509. /*
  2510. * If this is the first Set Address since device plug-in or
  2511. * virt_device realloaction after a resume with an xHCI power loss,
  2512. * then set up the slot context.
  2513. */
  2514. if (!slot_ctx->dev_info)
  2515. xhci_setup_addressable_virt_dev(xhci, udev);
  2516. /* Otherwise, update the control endpoint ring enqueue pointer. */
  2517. else
  2518. xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev);
  2519. xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
  2520. xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
  2521. spin_lock_irqsave(&xhci->lock, flags);
  2522. ret = xhci_queue_address_device(xhci, virt_dev->in_ctx->dma,
  2523. udev->slot_id);
  2524. if (ret) {
  2525. spin_unlock_irqrestore(&xhci->lock, flags);
  2526. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  2527. return ret;
  2528. }
  2529. xhci_ring_cmd_db(xhci);
  2530. spin_unlock_irqrestore(&xhci->lock, flags);
  2531. /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
  2532. timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
  2533. USB_CTRL_SET_TIMEOUT);
  2534. /* FIXME: From section 4.3.4: "Software shall be responsible for timing
  2535. * the SetAddress() "recovery interval" required by USB and aborting the
  2536. * command on a timeout.
  2537. */
  2538. if (timeleft <= 0) {
  2539. xhci_warn(xhci, "%s while waiting for a slot\n",
  2540. timeleft == 0 ? "Timeout" : "Signal");
  2541. /* FIXME cancel the address device command */
  2542. return -ETIME;
  2543. }
  2544. switch (virt_dev->cmd_status) {
  2545. case COMP_CTX_STATE:
  2546. case COMP_EBADSLT:
  2547. xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n",
  2548. udev->slot_id);
  2549. ret = -EINVAL;
  2550. break;
  2551. case COMP_TX_ERR:
  2552. dev_warn(&udev->dev, "Device not responding to set address.\n");
  2553. ret = -EPROTO;
  2554. break;
  2555. case COMP_SUCCESS:
  2556. xhci_dbg(xhci, "Successful Address Device command\n");
  2557. break;
  2558. default:
  2559. xhci_err(xhci, "ERROR: unexpected command completion "
  2560. "code 0x%x.\n", virt_dev->cmd_status);
  2561. xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
  2562. xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
  2563. ret = -EINVAL;
  2564. break;
  2565. }
  2566. if (ret) {
  2567. return ret;
  2568. }
  2569. temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
  2570. xhci_dbg(xhci, "Op regs DCBAA ptr = %#016llx\n", temp_64);
  2571. xhci_dbg(xhci, "Slot ID %d dcbaa entry @%p = %#016llx\n",
  2572. udev->slot_id,
  2573. &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
  2574. (unsigned long long)
  2575. le64_to_cpu(xhci->dcbaa->dev_context_ptrs[udev->slot_id]));
  2576. xhci_dbg(xhci, "Output Context DMA address = %#08llx\n",
  2577. (unsigned long long)virt_dev->out_ctx->dma);
  2578. xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
  2579. xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
  2580. xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
  2581. xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
  2582. /*
  2583. * USB core uses address 1 for the roothubs, so we add one to the
  2584. * address given back to us by the HC.
  2585. */
  2586. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
  2587. /* Use kernel assigned address for devices; store xHC assigned
  2588. * address locally. */
  2589. virt_dev->address = (le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK)
  2590. + 1;
  2591. /* Zero the input context control for later use */
  2592. ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
  2593. ctrl_ctx->add_flags = 0;
  2594. ctrl_ctx->drop_flags = 0;
  2595. xhci_dbg(xhci, "Internal device address = %d\n", virt_dev->address);
  2596. return 0;
  2597. }
  2598. /* Once a hub descriptor is fetched for a device, we need to update the xHC's
  2599. * internal data structures for the device.
  2600. */
  2601. int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
  2602. struct usb_tt *tt, gfp_t mem_flags)
  2603. {
  2604. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  2605. struct xhci_virt_device *vdev;
  2606. struct xhci_command *config_cmd;
  2607. struct xhci_input_control_ctx *ctrl_ctx;
  2608. struct xhci_slot_ctx *slot_ctx;
  2609. unsigned long flags;
  2610. unsigned think_time;
  2611. int ret;
  2612. /* Ignore root hubs */
  2613. if (!hdev->parent)
  2614. return 0;
  2615. vdev = xhci->devs[hdev->slot_id];
  2616. if (!vdev) {
  2617. xhci_warn(xhci, "Cannot update hub desc for unknown device.\n");
  2618. return -EINVAL;
  2619. }
  2620. config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
  2621. if (!config_cmd) {
  2622. xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
  2623. return -ENOMEM;
  2624. }
  2625. spin_lock_irqsave(&xhci->lock, flags);
  2626. xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx);
  2627. ctrl_ctx = xhci_get_input_control_ctx(xhci, config_cmd->in_ctx);
  2628. ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
  2629. slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx);
  2630. slot_ctx->dev_info |= cpu_to_le32(DEV_HUB);
  2631. if (tt->multi)
  2632. slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
  2633. if (xhci->hci_version > 0x95) {
  2634. xhci_dbg(xhci, "xHCI version %x needs hub "
  2635. "TT think time and number of ports\n",
  2636. (unsigned int) xhci->hci_version);
  2637. slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(hdev->maxchild));
  2638. /* Set TT think time - convert from ns to FS bit times.
  2639. * 0 = 8 FS bit times, 1 = 16 FS bit times,
  2640. * 2 = 24 FS bit times, 3 = 32 FS bit times.
  2641. *
  2642. * xHCI 1.0: this field shall be 0 if the device is not a
  2643. * High-spped hub.
  2644. */
  2645. think_time = tt->think_time;
  2646. if (think_time != 0)
  2647. think_time = (think_time / 666) - 1;
  2648. if (xhci->hci_version < 0x100 || hdev->speed == USB_SPEED_HIGH)
  2649. slot_ctx->tt_info |=
  2650. cpu_to_le32(TT_THINK_TIME(think_time));
  2651. } else {
  2652. xhci_dbg(xhci, "xHCI version %x doesn't need hub "
  2653. "TT think time or number of ports\n",
  2654. (unsigned int) xhci->hci_version);
  2655. }
  2656. slot_ctx->dev_state = 0;
  2657. spin_unlock_irqrestore(&xhci->lock, flags);
  2658. xhci_dbg(xhci, "Set up %s for hub device.\n",
  2659. (xhci->hci_version > 0x95) ?
  2660. "configure endpoint" : "evaluate context");
  2661. xhci_dbg(xhci, "Slot %u Input Context:\n", hdev->slot_id);
  2662. xhci_dbg_ctx(xhci, config_cmd->in_ctx, 0);
  2663. /* Issue and wait for the configure endpoint or
  2664. * evaluate context command.
  2665. */
  2666. if (xhci->hci_version > 0x95)
  2667. ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
  2668. false, false);
  2669. else
  2670. ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
  2671. true, false);
  2672. xhci_dbg(xhci, "Slot %u Output Context:\n", hdev->slot_id);
  2673. xhci_dbg_ctx(xhci, vdev->out_ctx, 0);
  2674. xhci_free_command(xhci, config_cmd);
  2675. return ret;
  2676. }
  2677. int xhci_get_frame(struct usb_hcd *hcd)
  2678. {
  2679. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  2680. /* EHCI mods by the periodic size. Why? */
  2681. return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
  2682. }
  2683. MODULE_DESCRIPTION(DRIVER_DESC);
  2684. MODULE_AUTHOR(DRIVER_AUTHOR);
  2685. MODULE_LICENSE("GPL");
  2686. static int __init xhci_hcd_init(void)
  2687. {
  2688. #ifdef CONFIG_PCI
  2689. int retval = 0;
  2690. retval = xhci_register_pci();
  2691. if (retval < 0) {
  2692. printk(KERN_DEBUG "Problem registering PCI driver.");
  2693. return retval;
  2694. }
  2695. #endif
  2696. /*
  2697. * Check the compiler generated sizes of structures that must be laid
  2698. * out in specific ways for hardware access.
  2699. */
  2700. BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
  2701. BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
  2702. BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
  2703. /* xhci_device_control has eight fields, and also
  2704. * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
  2705. */
  2706. BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
  2707. BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
  2708. BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
  2709. BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 7*32/8);
  2710. BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
  2711. /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
  2712. BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
  2713. BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
  2714. return 0;
  2715. }
  2716. module_init(xhci_hcd_init);
  2717. static void __exit xhci_hcd_cleanup(void)
  2718. {
  2719. #ifdef CONFIG_PCI
  2720. xhci_unregister_pci();
  2721. #endif
  2722. }
  2723. module_exit(xhci_hcd_cleanup);