xhci.c 81 KB

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