xhci.c 93 KB

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