xhci.c 82 KB

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