xhci.c 95 KB

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