xhci.c 111 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629
  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[udev->slot_id]) {
  825. printk(KERN_DEBUG "xHCI %s called with unaddressed "
  826. "device\n", func);
  827. return -EINVAL;
  828. }
  829. virt_dev = xhci->devs[udev->slot_id];
  830. if (virt_dev->udev != udev) {
  831. printk(KERN_DEBUG "xHCI %s called with udev and "
  832. "virt_dev does not match\n", func);
  833. return -EINVAL;
  834. }
  835. }
  836. return 1;
  837. }
  838. static int xhci_configure_endpoint(struct xhci_hcd *xhci,
  839. struct usb_device *udev, struct xhci_command *command,
  840. bool ctx_change, bool must_succeed);
  841. /*
  842. * Full speed devices may have a max packet size greater than 8 bytes, but the
  843. * USB core doesn't know that until it reads the first 8 bytes of the
  844. * descriptor. If the usb_device's max packet size changes after that point,
  845. * we need to issue an evaluate context command and wait on it.
  846. */
  847. static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
  848. unsigned int ep_index, struct urb *urb)
  849. {
  850. struct xhci_container_ctx *in_ctx;
  851. struct xhci_container_ctx *out_ctx;
  852. struct xhci_input_control_ctx *ctrl_ctx;
  853. struct xhci_ep_ctx *ep_ctx;
  854. int max_packet_size;
  855. int hw_max_packet_size;
  856. int ret = 0;
  857. out_ctx = xhci->devs[slot_id]->out_ctx;
  858. ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
  859. hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
  860. max_packet_size = usb_endpoint_maxp(&urb->dev->ep0.desc);
  861. if (hw_max_packet_size != max_packet_size) {
  862. xhci_dbg(xhci, "Max Packet Size for ep 0 changed.\n");
  863. xhci_dbg(xhci, "Max packet size in usb_device = %d\n",
  864. max_packet_size);
  865. xhci_dbg(xhci, "Max packet size in xHCI HW = %d\n",
  866. hw_max_packet_size);
  867. xhci_dbg(xhci, "Issuing evaluate context command.\n");
  868. /* Set up the modified control endpoint 0 */
  869. xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
  870. xhci->devs[slot_id]->out_ctx, ep_index);
  871. in_ctx = xhci->devs[slot_id]->in_ctx;
  872. ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
  873. ep_ctx->ep_info2 &= cpu_to_le32(~MAX_PACKET_MASK);
  874. ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size));
  875. /* Set up the input context flags for the command */
  876. /* FIXME: This won't work if a non-default control endpoint
  877. * changes max packet sizes.
  878. */
  879. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  880. ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
  881. ctrl_ctx->drop_flags = 0;
  882. xhci_dbg(xhci, "Slot %d input context\n", slot_id);
  883. xhci_dbg_ctx(xhci, in_ctx, ep_index);
  884. xhci_dbg(xhci, "Slot %d output context\n", slot_id);
  885. xhci_dbg_ctx(xhci, out_ctx, ep_index);
  886. ret = xhci_configure_endpoint(xhci, urb->dev, NULL,
  887. true, false);
  888. /* Clean up the input context for later use by bandwidth
  889. * functions.
  890. */
  891. ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
  892. }
  893. return ret;
  894. }
  895. /*
  896. * non-error returns are a promise to giveback() the urb later
  897. * we drop ownership so next owner (or urb unlink) can get it
  898. */
  899. int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
  900. {
  901. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  902. struct xhci_td *buffer;
  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. buffer = kzalloc(size * sizeof(struct xhci_td), mem_flags);
  928. if (!buffer) {
  929. kfree(urb_priv);
  930. return -ENOMEM;
  931. }
  932. for (i = 0; i < size; i++) {
  933. urb_priv->td[i] = buffer;
  934. buffer++;
  935. }
  936. urb_priv->length = size;
  937. urb_priv->td_cnt = 0;
  938. urb->hcpriv = urb_priv;
  939. if (usb_endpoint_xfer_control(&urb->ep->desc)) {
  940. /* Check to see if the max packet size for the default control
  941. * endpoint changed during FS device enumeration
  942. */
  943. if (urb->dev->speed == USB_SPEED_FULL) {
  944. ret = xhci_check_maxpacket(xhci, slot_id,
  945. ep_index, urb);
  946. if (ret < 0) {
  947. xhci_urb_free_priv(xhci, urb_priv);
  948. urb->hcpriv = NULL;
  949. return ret;
  950. }
  951. }
  952. /* We have a spinlock and interrupts disabled, so we must pass
  953. * atomic context to this function, which may allocate memory.
  954. */
  955. spin_lock_irqsave(&xhci->lock, flags);
  956. if (xhci->xhc_state & XHCI_STATE_DYING)
  957. goto dying;
  958. ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
  959. slot_id, ep_index);
  960. if (ret)
  961. goto free_priv;
  962. spin_unlock_irqrestore(&xhci->lock, flags);
  963. } else if (usb_endpoint_xfer_bulk(&urb->ep->desc)) {
  964. spin_lock_irqsave(&xhci->lock, flags);
  965. if (xhci->xhc_state & XHCI_STATE_DYING)
  966. goto dying;
  967. if (xhci->devs[slot_id]->eps[ep_index].ep_state &
  968. EP_GETTING_STREAMS) {
  969. xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
  970. "is transitioning to using streams.\n");
  971. ret = -EINVAL;
  972. } else if (xhci->devs[slot_id]->eps[ep_index].ep_state &
  973. EP_GETTING_NO_STREAMS) {
  974. xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
  975. "is transitioning to "
  976. "not having streams.\n");
  977. ret = -EINVAL;
  978. } else {
  979. ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
  980. slot_id, ep_index);
  981. }
  982. if (ret)
  983. goto free_priv;
  984. spin_unlock_irqrestore(&xhci->lock, flags);
  985. } else if (usb_endpoint_xfer_int(&urb->ep->desc)) {
  986. spin_lock_irqsave(&xhci->lock, flags);
  987. if (xhci->xhc_state & XHCI_STATE_DYING)
  988. goto dying;
  989. ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
  990. slot_id, ep_index);
  991. if (ret)
  992. goto free_priv;
  993. spin_unlock_irqrestore(&xhci->lock, flags);
  994. } else {
  995. spin_lock_irqsave(&xhci->lock, flags);
  996. if (xhci->xhc_state & XHCI_STATE_DYING)
  997. goto dying;
  998. ret = xhci_queue_isoc_tx_prepare(xhci, GFP_ATOMIC, urb,
  999. slot_id, ep_index);
  1000. if (ret)
  1001. goto free_priv;
  1002. spin_unlock_irqrestore(&xhci->lock, flags);
  1003. }
  1004. exit:
  1005. return ret;
  1006. dying:
  1007. xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for "
  1008. "non-responsive xHCI host.\n",
  1009. urb->ep->desc.bEndpointAddress, urb);
  1010. ret = -ESHUTDOWN;
  1011. free_priv:
  1012. xhci_urb_free_priv(xhci, urb_priv);
  1013. urb->hcpriv = NULL;
  1014. spin_unlock_irqrestore(&xhci->lock, flags);
  1015. return ret;
  1016. }
  1017. /* Get the right ring for the given URB.
  1018. * If the endpoint supports streams, boundary check the URB's stream ID.
  1019. * If the endpoint doesn't support streams, return the singular endpoint ring.
  1020. */
  1021. static struct xhci_ring *xhci_urb_to_transfer_ring(struct xhci_hcd *xhci,
  1022. struct urb *urb)
  1023. {
  1024. unsigned int slot_id;
  1025. unsigned int ep_index;
  1026. unsigned int stream_id;
  1027. struct xhci_virt_ep *ep;
  1028. slot_id = urb->dev->slot_id;
  1029. ep_index = xhci_get_endpoint_index(&urb->ep->desc);
  1030. stream_id = urb->stream_id;
  1031. ep = &xhci->devs[slot_id]->eps[ep_index];
  1032. /* Common case: no streams */
  1033. if (!(ep->ep_state & EP_HAS_STREAMS))
  1034. return ep->ring;
  1035. if (stream_id == 0) {
  1036. xhci_warn(xhci,
  1037. "WARN: Slot ID %u, ep index %u has streams, "
  1038. "but URB has no stream ID.\n",
  1039. slot_id, ep_index);
  1040. return NULL;
  1041. }
  1042. if (stream_id < ep->stream_info->num_streams)
  1043. return ep->stream_info->stream_rings[stream_id];
  1044. xhci_warn(xhci,
  1045. "WARN: Slot ID %u, ep index %u has "
  1046. "stream IDs 1 to %u allocated, "
  1047. "but stream ID %u is requested.\n",
  1048. slot_id, ep_index,
  1049. ep->stream_info->num_streams - 1,
  1050. stream_id);
  1051. return NULL;
  1052. }
  1053. /*
  1054. * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
  1055. * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
  1056. * should pick up where it left off in the TD, unless a Set Transfer Ring
  1057. * Dequeue Pointer is issued.
  1058. *
  1059. * The TRBs that make up the buffers for the canceled URB will be "removed" from
  1060. * the ring. Since the ring is a contiguous structure, they can't be physically
  1061. * removed. Instead, there are two options:
  1062. *
  1063. * 1) If the HC is in the middle of processing the URB to be canceled, we
  1064. * simply move the ring's dequeue pointer past those TRBs using the Set
  1065. * Transfer Ring Dequeue Pointer command. This will be the common case,
  1066. * when drivers timeout on the last submitted URB and attempt to cancel.
  1067. *
  1068. * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
  1069. * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
  1070. * HC will need to invalidate the any TRBs it has cached after the stop
  1071. * endpoint command, as noted in the xHCI 0.95 errata.
  1072. *
  1073. * 3) The TD may have completed by the time the Stop Endpoint Command
  1074. * completes, so software needs to handle that case too.
  1075. *
  1076. * This function should protect against the TD enqueueing code ringing the
  1077. * doorbell while this code is waiting for a Stop Endpoint command to complete.
  1078. * It also needs to account for multiple cancellations on happening at the same
  1079. * time for the same endpoint.
  1080. *
  1081. * Note that this function can be called in any context, or so says
  1082. * usb_hcd_unlink_urb()
  1083. */
  1084. int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  1085. {
  1086. unsigned long flags;
  1087. int ret, i;
  1088. u32 temp;
  1089. struct xhci_hcd *xhci;
  1090. struct urb_priv *urb_priv;
  1091. struct xhci_td *td;
  1092. unsigned int ep_index;
  1093. struct xhci_ring *ep_ring;
  1094. struct xhci_virt_ep *ep;
  1095. xhci = hcd_to_xhci(hcd);
  1096. spin_lock_irqsave(&xhci->lock, flags);
  1097. /* Make sure the URB hasn't completed or been unlinked already */
  1098. ret = usb_hcd_check_unlink_urb(hcd, urb, status);
  1099. if (ret || !urb->hcpriv)
  1100. goto done;
  1101. temp = xhci_readl(xhci, &xhci->op_regs->status);
  1102. if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_HALTED)) {
  1103. xhci_dbg(xhci, "HW died, freeing TD.\n");
  1104. urb_priv = urb->hcpriv;
  1105. for (i = urb_priv->td_cnt; i < urb_priv->length; i++) {
  1106. td = urb_priv->td[i];
  1107. if (!list_empty(&td->td_list))
  1108. list_del_init(&td->td_list);
  1109. if (!list_empty(&td->cancelled_td_list))
  1110. list_del_init(&td->cancelled_td_list);
  1111. }
  1112. usb_hcd_unlink_urb_from_ep(hcd, urb);
  1113. spin_unlock_irqrestore(&xhci->lock, flags);
  1114. usb_hcd_giveback_urb(hcd, urb, -ESHUTDOWN);
  1115. xhci_urb_free_priv(xhci, urb_priv);
  1116. return ret;
  1117. }
  1118. if ((xhci->xhc_state & XHCI_STATE_DYING) ||
  1119. (xhci->xhc_state & XHCI_STATE_HALTED)) {
  1120. xhci_dbg(xhci, "Ep 0x%x: URB %p to be canceled on "
  1121. "non-responsive xHCI host.\n",
  1122. urb->ep->desc.bEndpointAddress, urb);
  1123. /* Let the stop endpoint command watchdog timer (which set this
  1124. * state) finish cleaning up the endpoint TD lists. We must
  1125. * have caught it in the middle of dropping a lock and giving
  1126. * back an URB.
  1127. */
  1128. goto done;
  1129. }
  1130. xhci_dbg(xhci, "Cancel URB %p\n", urb);
  1131. xhci_dbg(xhci, "Event ring:\n");
  1132. xhci_debug_ring(xhci, xhci->event_ring);
  1133. ep_index = xhci_get_endpoint_index(&urb->ep->desc);
  1134. ep = &xhci->devs[urb->dev->slot_id]->eps[ep_index];
  1135. ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
  1136. if (!ep_ring) {
  1137. ret = -EINVAL;
  1138. goto done;
  1139. }
  1140. xhci_dbg(xhci, "Endpoint ring:\n");
  1141. xhci_debug_ring(xhci, ep_ring);
  1142. urb_priv = urb->hcpriv;
  1143. for (i = urb_priv->td_cnt; i < urb_priv->length; i++) {
  1144. td = urb_priv->td[i];
  1145. list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
  1146. }
  1147. /* Queue a stop endpoint command, but only if this is
  1148. * the first cancellation to be handled.
  1149. */
  1150. if (!(ep->ep_state & EP_HALT_PENDING)) {
  1151. ep->ep_state |= EP_HALT_PENDING;
  1152. ep->stop_cmds_pending++;
  1153. ep->stop_cmd_timer.expires = jiffies +
  1154. XHCI_STOP_EP_CMD_TIMEOUT * HZ;
  1155. add_timer(&ep->stop_cmd_timer);
  1156. xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index, 0);
  1157. xhci_ring_cmd_db(xhci);
  1158. }
  1159. done:
  1160. spin_unlock_irqrestore(&xhci->lock, flags);
  1161. return ret;
  1162. }
  1163. /* Drop an endpoint from a new bandwidth configuration for this device.
  1164. * Only one call to this function is allowed per endpoint before
  1165. * check_bandwidth() or reset_bandwidth() must be called.
  1166. * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
  1167. * add the endpoint to the schedule with possibly new parameters denoted by a
  1168. * different endpoint descriptor in usb_host_endpoint.
  1169. * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
  1170. * not allowed.
  1171. *
  1172. * The USB core will not allow URBs to be queued to an endpoint that is being
  1173. * disabled, so there's no need for mutual exclusion to protect
  1174. * the xhci->devs[slot_id] structure.
  1175. */
  1176. int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
  1177. struct usb_host_endpoint *ep)
  1178. {
  1179. struct xhci_hcd *xhci;
  1180. struct xhci_container_ctx *in_ctx, *out_ctx;
  1181. struct xhci_input_control_ctx *ctrl_ctx;
  1182. struct xhci_slot_ctx *slot_ctx;
  1183. unsigned int last_ctx;
  1184. unsigned int ep_index;
  1185. struct xhci_ep_ctx *ep_ctx;
  1186. u32 drop_flag;
  1187. u32 new_add_flags, new_drop_flags, new_slot_info;
  1188. int ret;
  1189. ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
  1190. if (ret <= 0)
  1191. return ret;
  1192. xhci = hcd_to_xhci(hcd);
  1193. if (xhci->xhc_state & XHCI_STATE_DYING)
  1194. return -ENODEV;
  1195. xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
  1196. drop_flag = xhci_get_endpoint_flag(&ep->desc);
  1197. if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
  1198. xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
  1199. __func__, drop_flag);
  1200. return 0;
  1201. }
  1202. in_ctx = xhci->devs[udev->slot_id]->in_ctx;
  1203. out_ctx = xhci->devs[udev->slot_id]->out_ctx;
  1204. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  1205. ep_index = xhci_get_endpoint_index(&ep->desc);
  1206. ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
  1207. /* If the HC already knows the endpoint is disabled,
  1208. * or the HCD has noted it is disabled, ignore this request
  1209. */
  1210. if (((ep_ctx->ep_info & cpu_to_le32(EP_STATE_MASK)) ==
  1211. cpu_to_le32(EP_STATE_DISABLED)) ||
  1212. le32_to_cpu(ctrl_ctx->drop_flags) &
  1213. xhci_get_endpoint_flag(&ep->desc)) {
  1214. xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
  1215. __func__, ep);
  1216. return 0;
  1217. }
  1218. ctrl_ctx->drop_flags |= cpu_to_le32(drop_flag);
  1219. new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
  1220. ctrl_ctx->add_flags &= cpu_to_le32(~drop_flag);
  1221. new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
  1222. last_ctx = xhci_last_valid_endpoint(le32_to_cpu(ctrl_ctx->add_flags));
  1223. slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
  1224. /* Update the last valid endpoint context, if we deleted the last one */
  1225. if ((le32_to_cpu(slot_ctx->dev_info) & LAST_CTX_MASK) >
  1226. LAST_CTX(last_ctx)) {
  1227. slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
  1228. slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(last_ctx));
  1229. }
  1230. new_slot_info = le32_to_cpu(slot_ctx->dev_info);
  1231. xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
  1232. xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
  1233. (unsigned int) ep->desc.bEndpointAddress,
  1234. udev->slot_id,
  1235. (unsigned int) new_drop_flags,
  1236. (unsigned int) new_add_flags,
  1237. (unsigned int) new_slot_info);
  1238. return 0;
  1239. }
  1240. /* Add an endpoint to a new possible bandwidth configuration for this device.
  1241. * Only one call to this function is allowed per endpoint before
  1242. * check_bandwidth() or reset_bandwidth() must be called.
  1243. * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
  1244. * add the endpoint to the schedule with possibly new parameters denoted by a
  1245. * different endpoint descriptor in usb_host_endpoint.
  1246. * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
  1247. * not allowed.
  1248. *
  1249. * The USB core will not allow URBs to be queued to an endpoint until the
  1250. * configuration or alt setting is installed in the device, so there's no need
  1251. * for mutual exclusion to protect the xhci->devs[slot_id] structure.
  1252. */
  1253. int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
  1254. struct usb_host_endpoint *ep)
  1255. {
  1256. struct xhci_hcd *xhci;
  1257. struct xhci_container_ctx *in_ctx, *out_ctx;
  1258. unsigned int ep_index;
  1259. struct xhci_ep_ctx *ep_ctx;
  1260. struct xhci_slot_ctx *slot_ctx;
  1261. struct xhci_input_control_ctx *ctrl_ctx;
  1262. u32 added_ctxs;
  1263. unsigned int last_ctx;
  1264. u32 new_add_flags, new_drop_flags, new_slot_info;
  1265. struct xhci_virt_device *virt_dev;
  1266. int ret = 0;
  1267. ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
  1268. if (ret <= 0) {
  1269. /* So we won't queue a reset ep command for a root hub */
  1270. ep->hcpriv = NULL;
  1271. return ret;
  1272. }
  1273. xhci = hcd_to_xhci(hcd);
  1274. if (xhci->xhc_state & XHCI_STATE_DYING)
  1275. return -ENODEV;
  1276. added_ctxs = xhci_get_endpoint_flag(&ep->desc);
  1277. last_ctx = xhci_last_valid_endpoint(added_ctxs);
  1278. if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
  1279. /* FIXME when we have to issue an evaluate endpoint command to
  1280. * deal with ep0 max packet size changing once we get the
  1281. * descriptors
  1282. */
  1283. xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
  1284. __func__, added_ctxs);
  1285. return 0;
  1286. }
  1287. virt_dev = xhci->devs[udev->slot_id];
  1288. in_ctx = virt_dev->in_ctx;
  1289. out_ctx = virt_dev->out_ctx;
  1290. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  1291. ep_index = xhci_get_endpoint_index(&ep->desc);
  1292. ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
  1293. /* If this endpoint is already in use, and the upper layers are trying
  1294. * to add it again without dropping it, reject the addition.
  1295. */
  1296. if (virt_dev->eps[ep_index].ring &&
  1297. !(le32_to_cpu(ctrl_ctx->drop_flags) &
  1298. xhci_get_endpoint_flag(&ep->desc))) {
  1299. xhci_warn(xhci, "Trying to add endpoint 0x%x "
  1300. "without dropping it.\n",
  1301. (unsigned int) ep->desc.bEndpointAddress);
  1302. return -EINVAL;
  1303. }
  1304. /* If the HCD has already noted the endpoint is enabled,
  1305. * ignore this request.
  1306. */
  1307. if (le32_to_cpu(ctrl_ctx->add_flags) &
  1308. xhci_get_endpoint_flag(&ep->desc)) {
  1309. xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
  1310. __func__, ep);
  1311. return 0;
  1312. }
  1313. /*
  1314. * Configuration and alternate setting changes must be done in
  1315. * process context, not interrupt context (or so documenation
  1316. * for usb_set_interface() and usb_set_configuration() claim).
  1317. */
  1318. if (xhci_endpoint_init(xhci, virt_dev, udev, ep, GFP_NOIO) < 0) {
  1319. dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
  1320. __func__, ep->desc.bEndpointAddress);
  1321. return -ENOMEM;
  1322. }
  1323. ctrl_ctx->add_flags |= cpu_to_le32(added_ctxs);
  1324. new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
  1325. /* If xhci_endpoint_disable() was called for this endpoint, but the
  1326. * xHC hasn't been notified yet through the check_bandwidth() call,
  1327. * this re-adds a new state for the endpoint from the new endpoint
  1328. * descriptors. We must drop and re-add this endpoint, so we leave the
  1329. * drop flags alone.
  1330. */
  1331. new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
  1332. slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
  1333. /* Update the last valid endpoint context, if we just added one past */
  1334. if ((le32_to_cpu(slot_ctx->dev_info) & LAST_CTX_MASK) <
  1335. LAST_CTX(last_ctx)) {
  1336. slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
  1337. slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(last_ctx));
  1338. }
  1339. new_slot_info = le32_to_cpu(slot_ctx->dev_info);
  1340. /* Store the usb_device pointer for later use */
  1341. ep->hcpriv = udev;
  1342. xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
  1343. (unsigned int) ep->desc.bEndpointAddress,
  1344. udev->slot_id,
  1345. (unsigned int) new_drop_flags,
  1346. (unsigned int) new_add_flags,
  1347. (unsigned int) new_slot_info);
  1348. return 0;
  1349. }
  1350. static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev)
  1351. {
  1352. struct xhci_input_control_ctx *ctrl_ctx;
  1353. struct xhci_ep_ctx *ep_ctx;
  1354. struct xhci_slot_ctx *slot_ctx;
  1355. int i;
  1356. /* When a device's add flag and drop flag are zero, any subsequent
  1357. * configure endpoint command will leave that endpoint's state
  1358. * untouched. Make sure we don't leave any old state in the input
  1359. * endpoint contexts.
  1360. */
  1361. ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
  1362. ctrl_ctx->drop_flags = 0;
  1363. ctrl_ctx->add_flags = 0;
  1364. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
  1365. slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
  1366. /* Endpoint 0 is always valid */
  1367. slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
  1368. for (i = 1; i < 31; ++i) {
  1369. ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
  1370. ep_ctx->ep_info = 0;
  1371. ep_ctx->ep_info2 = 0;
  1372. ep_ctx->deq = 0;
  1373. ep_ctx->tx_info = 0;
  1374. }
  1375. }
  1376. static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
  1377. struct usb_device *udev, u32 *cmd_status)
  1378. {
  1379. int ret;
  1380. switch (*cmd_status) {
  1381. case COMP_ENOMEM:
  1382. dev_warn(&udev->dev, "Not enough host controller resources "
  1383. "for new device state.\n");
  1384. ret = -ENOMEM;
  1385. /* FIXME: can we allocate more resources for the HC? */
  1386. break;
  1387. case COMP_BW_ERR:
  1388. dev_warn(&udev->dev, "Not enough bandwidth "
  1389. "for new device state.\n");
  1390. ret = -ENOSPC;
  1391. /* FIXME: can we go back to the old state? */
  1392. break;
  1393. case COMP_TRB_ERR:
  1394. /* the HCD set up something wrong */
  1395. dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
  1396. "add flag = 1, "
  1397. "and endpoint is not disabled.\n");
  1398. ret = -EINVAL;
  1399. break;
  1400. case COMP_DEV_ERR:
  1401. dev_warn(&udev->dev, "ERROR: Incompatible device for endpoint "
  1402. "configure command.\n");
  1403. ret = -ENODEV;
  1404. break;
  1405. case COMP_SUCCESS:
  1406. dev_dbg(&udev->dev, "Successful Endpoint Configure command\n");
  1407. ret = 0;
  1408. break;
  1409. default:
  1410. xhci_err(xhci, "ERROR: unexpected command completion "
  1411. "code 0x%x.\n", *cmd_status);
  1412. ret = -EINVAL;
  1413. break;
  1414. }
  1415. return ret;
  1416. }
  1417. static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
  1418. struct usb_device *udev, u32 *cmd_status)
  1419. {
  1420. int ret;
  1421. struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
  1422. switch (*cmd_status) {
  1423. case COMP_EINVAL:
  1424. dev_warn(&udev->dev, "WARN: xHCI driver setup invalid evaluate "
  1425. "context command.\n");
  1426. ret = -EINVAL;
  1427. break;
  1428. case COMP_EBADSLT:
  1429. dev_warn(&udev->dev, "WARN: slot not enabled for"
  1430. "evaluate context command.\n");
  1431. case COMP_CTX_STATE:
  1432. dev_warn(&udev->dev, "WARN: invalid context state for "
  1433. "evaluate context command.\n");
  1434. xhci_dbg_ctx(xhci, virt_dev->out_ctx, 1);
  1435. ret = -EINVAL;
  1436. break;
  1437. case COMP_DEV_ERR:
  1438. dev_warn(&udev->dev, "ERROR: Incompatible device for evaluate "
  1439. "context command.\n");
  1440. ret = -ENODEV;
  1441. break;
  1442. case COMP_MEL_ERR:
  1443. /* Max Exit Latency too large error */
  1444. dev_warn(&udev->dev, "WARN: Max Exit Latency too large\n");
  1445. ret = -EINVAL;
  1446. break;
  1447. case COMP_SUCCESS:
  1448. dev_dbg(&udev->dev, "Successful evaluate context command\n");
  1449. ret = 0;
  1450. break;
  1451. default:
  1452. xhci_err(xhci, "ERROR: unexpected command completion "
  1453. "code 0x%x.\n", *cmd_status);
  1454. ret = -EINVAL;
  1455. break;
  1456. }
  1457. return ret;
  1458. }
  1459. static u32 xhci_count_num_new_endpoints(struct xhci_hcd *xhci,
  1460. struct xhci_container_ctx *in_ctx)
  1461. {
  1462. struct xhci_input_control_ctx *ctrl_ctx;
  1463. u32 valid_add_flags;
  1464. u32 valid_drop_flags;
  1465. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  1466. /* Ignore the slot flag (bit 0), and the default control endpoint flag
  1467. * (bit 1). The default control endpoint is added during the Address
  1468. * Device command and is never removed until the slot is disabled.
  1469. */
  1470. valid_add_flags = ctrl_ctx->add_flags >> 2;
  1471. valid_drop_flags = ctrl_ctx->drop_flags >> 2;
  1472. /* Use hweight32 to count the number of ones in the add flags, or
  1473. * number of endpoints added. Don't count endpoints that are changed
  1474. * (both added and dropped).
  1475. */
  1476. return hweight32(valid_add_flags) -
  1477. hweight32(valid_add_flags & valid_drop_flags);
  1478. }
  1479. static unsigned int xhci_count_num_dropped_endpoints(struct xhci_hcd *xhci,
  1480. struct xhci_container_ctx *in_ctx)
  1481. {
  1482. struct xhci_input_control_ctx *ctrl_ctx;
  1483. u32 valid_add_flags;
  1484. u32 valid_drop_flags;
  1485. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  1486. valid_add_flags = ctrl_ctx->add_flags >> 2;
  1487. valid_drop_flags = ctrl_ctx->drop_flags >> 2;
  1488. return hweight32(valid_drop_flags) -
  1489. hweight32(valid_add_flags & valid_drop_flags);
  1490. }
  1491. /*
  1492. * We need to reserve the new number of endpoints before the configure endpoint
  1493. * command completes. We can't subtract the dropped endpoints from the number
  1494. * of active endpoints until the command completes because we can oversubscribe
  1495. * the host in this case:
  1496. *
  1497. * - the first configure endpoint command drops more endpoints than it adds
  1498. * - a second configure endpoint command that adds more endpoints is queued
  1499. * - the first configure endpoint command fails, so the config is unchanged
  1500. * - the second command may succeed, even though there isn't enough resources
  1501. *
  1502. * Must be called with xhci->lock held.
  1503. */
  1504. static int xhci_reserve_host_resources(struct xhci_hcd *xhci,
  1505. struct xhci_container_ctx *in_ctx)
  1506. {
  1507. u32 added_eps;
  1508. added_eps = xhci_count_num_new_endpoints(xhci, in_ctx);
  1509. if (xhci->num_active_eps + added_eps > xhci->limit_active_eps) {
  1510. xhci_dbg(xhci, "Not enough ep ctxs: "
  1511. "%u active, need to add %u, limit is %u.\n",
  1512. xhci->num_active_eps, added_eps,
  1513. xhci->limit_active_eps);
  1514. return -ENOMEM;
  1515. }
  1516. xhci->num_active_eps += added_eps;
  1517. xhci_dbg(xhci, "Adding %u ep ctxs, %u now active.\n", added_eps,
  1518. xhci->num_active_eps);
  1519. return 0;
  1520. }
  1521. /*
  1522. * The configure endpoint was failed by the xHC for some other reason, so we
  1523. * need to revert the resources that failed configuration would have used.
  1524. *
  1525. * Must be called with xhci->lock held.
  1526. */
  1527. static void xhci_free_host_resources(struct xhci_hcd *xhci,
  1528. struct xhci_container_ctx *in_ctx)
  1529. {
  1530. u32 num_failed_eps;
  1531. num_failed_eps = xhci_count_num_new_endpoints(xhci, in_ctx);
  1532. xhci->num_active_eps -= num_failed_eps;
  1533. xhci_dbg(xhci, "Removing %u failed ep ctxs, %u now active.\n",
  1534. num_failed_eps,
  1535. xhci->num_active_eps);
  1536. }
  1537. /*
  1538. * Now that the command has completed, clean up the active endpoint count by
  1539. * subtracting out the endpoints that were dropped (but not changed).
  1540. *
  1541. * Must be called with xhci->lock held.
  1542. */
  1543. static void xhci_finish_resource_reservation(struct xhci_hcd *xhci,
  1544. struct xhci_container_ctx *in_ctx)
  1545. {
  1546. u32 num_dropped_eps;
  1547. num_dropped_eps = xhci_count_num_dropped_endpoints(xhci, in_ctx);
  1548. xhci->num_active_eps -= num_dropped_eps;
  1549. if (num_dropped_eps)
  1550. xhci_dbg(xhci, "Removing %u dropped ep ctxs, %u now active.\n",
  1551. num_dropped_eps,
  1552. xhci->num_active_eps);
  1553. }
  1554. unsigned int xhci_get_block_size(struct usb_device *udev)
  1555. {
  1556. switch (udev->speed) {
  1557. case USB_SPEED_LOW:
  1558. case USB_SPEED_FULL:
  1559. return FS_BLOCK;
  1560. case USB_SPEED_HIGH:
  1561. return HS_BLOCK;
  1562. case USB_SPEED_SUPER:
  1563. return SS_BLOCK;
  1564. case USB_SPEED_UNKNOWN:
  1565. case USB_SPEED_WIRELESS:
  1566. default:
  1567. /* Should never happen */
  1568. return 1;
  1569. }
  1570. }
  1571. unsigned int xhci_get_largest_overhead(struct xhci_interval_bw *interval_bw)
  1572. {
  1573. if (interval_bw->overhead[LS_OVERHEAD_TYPE])
  1574. return LS_OVERHEAD;
  1575. if (interval_bw->overhead[FS_OVERHEAD_TYPE])
  1576. return FS_OVERHEAD;
  1577. return HS_OVERHEAD;
  1578. }
  1579. /* If we are changing a LS/FS device under a HS hub,
  1580. * make sure (if we are activating a new TT) that the HS bus has enough
  1581. * bandwidth for this new TT.
  1582. */
  1583. static int xhci_check_tt_bw_table(struct xhci_hcd *xhci,
  1584. struct xhci_virt_device *virt_dev,
  1585. int old_active_eps)
  1586. {
  1587. struct xhci_interval_bw_table *bw_table;
  1588. struct xhci_tt_bw_info *tt_info;
  1589. /* Find the bandwidth table for the root port this TT is attached to. */
  1590. bw_table = &xhci->rh_bw[virt_dev->real_port - 1].bw_table;
  1591. tt_info = virt_dev->tt_info;
  1592. /* If this TT already had active endpoints, the bandwidth for this TT
  1593. * has already been added. Removing all periodic endpoints (and thus
  1594. * making the TT enactive) will only decrease the bandwidth used.
  1595. */
  1596. if (old_active_eps)
  1597. return 0;
  1598. if (old_active_eps == 0 && tt_info->active_eps != 0) {
  1599. if (bw_table->bw_used + TT_HS_OVERHEAD > HS_BW_LIMIT)
  1600. return -ENOMEM;
  1601. return 0;
  1602. }
  1603. /* Not sure why we would have no new active endpoints...
  1604. *
  1605. * Maybe because of an Evaluate Context change for a hub update or a
  1606. * control endpoint 0 max packet size change?
  1607. * FIXME: skip the bandwidth calculation in that case.
  1608. */
  1609. return 0;
  1610. }
  1611. /*
  1612. * This algorithm is a very conservative estimate of the worst-case scheduling
  1613. * scenario for any one interval. The hardware dynamically schedules the
  1614. * packets, so we can't tell which microframe could be the limiting factor in
  1615. * the bandwidth scheduling. This only takes into account periodic endpoints.
  1616. *
  1617. * Obviously, we can't solve an NP complete problem to find the minimum worst
  1618. * case scenario. Instead, we come up with an estimate that is no less than
  1619. * the worst case bandwidth used for any one microframe, but may be an
  1620. * over-estimate.
  1621. *
  1622. * We walk the requirements for each endpoint by interval, starting with the
  1623. * smallest interval, and place packets in the schedule where there is only one
  1624. * possible way to schedule packets for that interval. In order to simplify
  1625. * this algorithm, we record the largest max packet size for each interval, and
  1626. * assume all packets will be that size.
  1627. *
  1628. * For interval 0, we obviously must schedule all packets for each interval.
  1629. * The bandwidth for interval 0 is just the amount of data to be transmitted
  1630. * (the sum of all max ESIT payload sizes, plus any overhead per packet times
  1631. * the number of packets).
  1632. *
  1633. * For interval 1, we have two possible microframes to schedule those packets
  1634. * in. For this algorithm, if we can schedule the same number of packets for
  1635. * each possible scheduling opportunity (each microframe), we will do so. The
  1636. * remaining number of packets will be saved to be transmitted in the gaps in
  1637. * the next interval's scheduling sequence.
  1638. *
  1639. * As we move those remaining packets to be scheduled with interval 2 packets,
  1640. * we have to double the number of remaining packets to transmit. This is
  1641. * because the intervals are actually powers of 2, and we would be transmitting
  1642. * the previous interval's packets twice in this interval. We also have to be
  1643. * sure that when we look at the largest max packet size for this interval, we
  1644. * also look at the largest max packet size for the remaining packets and take
  1645. * the greater of the two.
  1646. *
  1647. * The algorithm continues to evenly distribute packets in each scheduling
  1648. * opportunity, and push the remaining packets out, until we get to the last
  1649. * interval. Then those packets and their associated overhead are just added
  1650. * to the bandwidth used.
  1651. */
  1652. static int xhci_check_bw_table(struct xhci_hcd *xhci,
  1653. struct xhci_virt_device *virt_dev,
  1654. int old_active_eps)
  1655. {
  1656. unsigned int bw_reserved;
  1657. unsigned int max_bandwidth;
  1658. unsigned int bw_used;
  1659. unsigned int block_size;
  1660. struct xhci_interval_bw_table *bw_table;
  1661. unsigned int packet_size = 0;
  1662. unsigned int overhead = 0;
  1663. unsigned int packets_transmitted = 0;
  1664. unsigned int packets_remaining = 0;
  1665. unsigned int i;
  1666. if (virt_dev->udev->speed == USB_SPEED_HIGH) {
  1667. max_bandwidth = HS_BW_LIMIT;
  1668. /* Convert percent of bus BW reserved to blocks reserved */
  1669. bw_reserved = DIV_ROUND_UP(HS_BW_RESERVED * max_bandwidth, 100);
  1670. } else {
  1671. max_bandwidth = FS_BW_LIMIT;
  1672. bw_reserved = DIV_ROUND_UP(FS_BW_RESERVED * max_bandwidth, 100);
  1673. }
  1674. bw_table = virt_dev->bw_table;
  1675. /* We need to translate the max packet size and max ESIT payloads into
  1676. * the units the hardware uses.
  1677. */
  1678. block_size = xhci_get_block_size(virt_dev->udev);
  1679. /* If we are manipulating a LS/FS device under a HS hub, double check
  1680. * that the HS bus has enough bandwidth if we are activing a new TT.
  1681. */
  1682. if (virt_dev->tt_info) {
  1683. xhci_dbg(xhci, "Recalculating BW for rootport %u\n",
  1684. virt_dev->real_port);
  1685. if (xhci_check_tt_bw_table(xhci, virt_dev, old_active_eps)) {
  1686. xhci_warn(xhci, "Not enough bandwidth on HS bus for "
  1687. "newly activated TT.\n");
  1688. return -ENOMEM;
  1689. }
  1690. xhci_dbg(xhci, "Recalculating BW for TT slot %u port %u\n",
  1691. virt_dev->tt_info->slot_id,
  1692. virt_dev->tt_info->ttport);
  1693. } else {
  1694. xhci_dbg(xhci, "Recalculating BW for rootport %u\n",
  1695. virt_dev->real_port);
  1696. }
  1697. /* Add in how much bandwidth will be used for interval zero, or the
  1698. * rounded max ESIT payload + number of packets * largest overhead.
  1699. */
  1700. bw_used = DIV_ROUND_UP(bw_table->interval0_esit_payload, block_size) +
  1701. bw_table->interval_bw[0].num_packets *
  1702. xhci_get_largest_overhead(&bw_table->interval_bw[0]);
  1703. for (i = 1; i < XHCI_MAX_INTERVAL; i++) {
  1704. unsigned int bw_added;
  1705. unsigned int largest_mps;
  1706. unsigned int interval_overhead;
  1707. /*
  1708. * How many packets could we transmit in this interval?
  1709. * If packets didn't fit in the previous interval, we will need
  1710. * to transmit that many packets twice within this interval.
  1711. */
  1712. packets_remaining = 2 * packets_remaining +
  1713. bw_table->interval_bw[i].num_packets;
  1714. /* Find the largest max packet size of this or the previous
  1715. * interval.
  1716. */
  1717. if (list_empty(&bw_table->interval_bw[i].endpoints))
  1718. largest_mps = 0;
  1719. else {
  1720. struct xhci_virt_ep *virt_ep;
  1721. struct list_head *ep_entry;
  1722. ep_entry = bw_table->interval_bw[i].endpoints.next;
  1723. virt_ep = list_entry(ep_entry,
  1724. struct xhci_virt_ep, bw_endpoint_list);
  1725. /* Convert to blocks, rounding up */
  1726. largest_mps = DIV_ROUND_UP(
  1727. virt_ep->bw_info.max_packet_size,
  1728. block_size);
  1729. }
  1730. if (largest_mps > packet_size)
  1731. packet_size = largest_mps;
  1732. /* Use the larger overhead of this or the previous interval. */
  1733. interval_overhead = xhci_get_largest_overhead(
  1734. &bw_table->interval_bw[i]);
  1735. if (interval_overhead > overhead)
  1736. overhead = interval_overhead;
  1737. /* How many packets can we evenly distribute across
  1738. * (1 << (i + 1)) possible scheduling opportunities?
  1739. */
  1740. packets_transmitted = packets_remaining >> (i + 1);
  1741. /* Add in the bandwidth used for those scheduled packets */
  1742. bw_added = packets_transmitted * (overhead + packet_size);
  1743. /* How many packets do we have remaining to transmit? */
  1744. packets_remaining = packets_remaining % (1 << (i + 1));
  1745. /* What largest max packet size should those packets have? */
  1746. /* If we've transmitted all packets, don't carry over the
  1747. * largest packet size.
  1748. */
  1749. if (packets_remaining == 0) {
  1750. packet_size = 0;
  1751. overhead = 0;
  1752. } else if (packets_transmitted > 0) {
  1753. /* Otherwise if we do have remaining packets, and we've
  1754. * scheduled some packets in this interval, take the
  1755. * largest max packet size from endpoints with this
  1756. * interval.
  1757. */
  1758. packet_size = largest_mps;
  1759. overhead = interval_overhead;
  1760. }
  1761. /* Otherwise carry over packet_size and overhead from the last
  1762. * time we had a remainder.
  1763. */
  1764. bw_used += bw_added;
  1765. if (bw_used > max_bandwidth) {
  1766. xhci_warn(xhci, "Not enough bandwidth. "
  1767. "Proposed: %u, Max: %u\n",
  1768. bw_used, max_bandwidth);
  1769. return -ENOMEM;
  1770. }
  1771. }
  1772. /*
  1773. * Ok, we know we have some packets left over after even-handedly
  1774. * scheduling interval 15. We don't know which microframes they will
  1775. * fit into, so we over-schedule and say they will be scheduled every
  1776. * microframe.
  1777. */
  1778. if (packets_remaining > 0)
  1779. bw_used += overhead + packet_size;
  1780. if (!virt_dev->tt_info && virt_dev->udev->speed == USB_SPEED_HIGH) {
  1781. unsigned int port_index = virt_dev->real_port - 1;
  1782. /* OK, we're manipulating a HS device attached to a
  1783. * root port bandwidth domain. Include the number of active TTs
  1784. * in the bandwidth used.
  1785. */
  1786. bw_used += TT_HS_OVERHEAD *
  1787. xhci->rh_bw[port_index].num_active_tts;
  1788. }
  1789. xhci_dbg(xhci, "Final bandwidth: %u, Limit: %u, Reserved: %u, "
  1790. "Available: %u " "percent\n",
  1791. bw_used, max_bandwidth, bw_reserved,
  1792. (max_bandwidth - bw_used - bw_reserved) * 100 /
  1793. max_bandwidth);
  1794. bw_used += bw_reserved;
  1795. if (bw_used > max_bandwidth) {
  1796. xhci_warn(xhci, "Not enough bandwidth. Proposed: %u, Max: %u\n",
  1797. bw_used, max_bandwidth);
  1798. return -ENOMEM;
  1799. }
  1800. bw_table->bw_used = bw_used;
  1801. return 0;
  1802. }
  1803. static bool xhci_is_async_ep(unsigned int ep_type)
  1804. {
  1805. return (ep_type != ISOC_OUT_EP && ep_type != INT_OUT_EP &&
  1806. ep_type != ISOC_IN_EP &&
  1807. ep_type != INT_IN_EP);
  1808. }
  1809. void xhci_drop_ep_from_interval_table(struct xhci_hcd *xhci,
  1810. struct xhci_bw_info *ep_bw,
  1811. struct xhci_interval_bw_table *bw_table,
  1812. struct usb_device *udev,
  1813. struct xhci_virt_ep *virt_ep,
  1814. struct xhci_tt_bw_info *tt_info)
  1815. {
  1816. struct xhci_interval_bw *interval_bw;
  1817. int normalized_interval;
  1818. if (xhci_is_async_ep(ep_bw->type) ||
  1819. list_empty(&virt_ep->bw_endpoint_list))
  1820. return;
  1821. /* For LS/FS devices, we need to translate the interval expressed in
  1822. * microframes to frames.
  1823. */
  1824. if (udev->speed == USB_SPEED_HIGH)
  1825. normalized_interval = ep_bw->ep_interval;
  1826. else
  1827. normalized_interval = ep_bw->ep_interval - 3;
  1828. if (normalized_interval == 0)
  1829. bw_table->interval0_esit_payload -= ep_bw->max_esit_payload;
  1830. interval_bw = &bw_table->interval_bw[normalized_interval];
  1831. interval_bw->num_packets -= ep_bw->num_packets;
  1832. switch (udev->speed) {
  1833. case USB_SPEED_LOW:
  1834. interval_bw->overhead[LS_OVERHEAD_TYPE] -= 1;
  1835. break;
  1836. case USB_SPEED_FULL:
  1837. interval_bw->overhead[FS_OVERHEAD_TYPE] -= 1;
  1838. break;
  1839. case USB_SPEED_HIGH:
  1840. interval_bw->overhead[HS_OVERHEAD_TYPE] -= 1;
  1841. break;
  1842. case USB_SPEED_SUPER:
  1843. case USB_SPEED_UNKNOWN:
  1844. case USB_SPEED_WIRELESS:
  1845. /* Should never happen because only LS/FS/HS endpoints will get
  1846. * added to the endpoint list.
  1847. */
  1848. return;
  1849. }
  1850. if (tt_info)
  1851. tt_info->active_eps -= 1;
  1852. list_del_init(&virt_ep->bw_endpoint_list);
  1853. }
  1854. static void xhci_add_ep_to_interval_table(struct xhci_hcd *xhci,
  1855. struct xhci_bw_info *ep_bw,
  1856. struct xhci_interval_bw_table *bw_table,
  1857. struct usb_device *udev,
  1858. struct xhci_virt_ep *virt_ep,
  1859. struct xhci_tt_bw_info *tt_info)
  1860. {
  1861. struct xhci_interval_bw *interval_bw;
  1862. struct xhci_virt_ep *smaller_ep;
  1863. int normalized_interval;
  1864. if (xhci_is_async_ep(ep_bw->type))
  1865. return;
  1866. /* For LS/FS devices, we need to translate the interval expressed in
  1867. * microframes to frames.
  1868. */
  1869. if (udev->speed == USB_SPEED_HIGH)
  1870. normalized_interval = ep_bw->ep_interval;
  1871. else
  1872. normalized_interval = ep_bw->ep_interval - 3;
  1873. if (normalized_interval == 0)
  1874. bw_table->interval0_esit_payload += ep_bw->max_esit_payload;
  1875. interval_bw = &bw_table->interval_bw[normalized_interval];
  1876. interval_bw->num_packets += ep_bw->num_packets;
  1877. switch (udev->speed) {
  1878. case USB_SPEED_LOW:
  1879. interval_bw->overhead[LS_OVERHEAD_TYPE] += 1;
  1880. break;
  1881. case USB_SPEED_FULL:
  1882. interval_bw->overhead[FS_OVERHEAD_TYPE] += 1;
  1883. break;
  1884. case USB_SPEED_HIGH:
  1885. interval_bw->overhead[HS_OVERHEAD_TYPE] += 1;
  1886. break;
  1887. case USB_SPEED_SUPER:
  1888. case USB_SPEED_UNKNOWN:
  1889. case USB_SPEED_WIRELESS:
  1890. /* Should never happen because only LS/FS/HS endpoints will get
  1891. * added to the endpoint list.
  1892. */
  1893. return;
  1894. }
  1895. if (tt_info)
  1896. tt_info->active_eps += 1;
  1897. /* Insert the endpoint into the list, largest max packet size first. */
  1898. list_for_each_entry(smaller_ep, &interval_bw->endpoints,
  1899. bw_endpoint_list) {
  1900. if (ep_bw->max_packet_size >=
  1901. smaller_ep->bw_info.max_packet_size) {
  1902. /* Add the new ep before the smaller endpoint */
  1903. list_add_tail(&virt_ep->bw_endpoint_list,
  1904. &smaller_ep->bw_endpoint_list);
  1905. return;
  1906. }
  1907. }
  1908. /* Add the new endpoint at the end of the list. */
  1909. list_add_tail(&virt_ep->bw_endpoint_list,
  1910. &interval_bw->endpoints);
  1911. }
  1912. void xhci_update_tt_active_eps(struct xhci_hcd *xhci,
  1913. struct xhci_virt_device *virt_dev,
  1914. int old_active_eps)
  1915. {
  1916. struct xhci_root_port_bw_info *rh_bw_info;
  1917. if (!virt_dev->tt_info)
  1918. return;
  1919. rh_bw_info = &xhci->rh_bw[virt_dev->real_port - 1];
  1920. if (old_active_eps == 0 &&
  1921. virt_dev->tt_info->active_eps != 0) {
  1922. rh_bw_info->num_active_tts += 1;
  1923. rh_bw_info->bw_table.bw_used += TT_HS_OVERHEAD;
  1924. } else if (old_active_eps != 0 &&
  1925. virt_dev->tt_info->active_eps == 0) {
  1926. rh_bw_info->num_active_tts -= 1;
  1927. rh_bw_info->bw_table.bw_used -= TT_HS_OVERHEAD;
  1928. }
  1929. }
  1930. static int xhci_reserve_bandwidth(struct xhci_hcd *xhci,
  1931. struct xhci_virt_device *virt_dev,
  1932. struct xhci_container_ctx *in_ctx)
  1933. {
  1934. struct xhci_bw_info ep_bw_info[31];
  1935. int i;
  1936. struct xhci_input_control_ctx *ctrl_ctx;
  1937. int old_active_eps = 0;
  1938. if (virt_dev->udev->speed == USB_SPEED_SUPER)
  1939. return 0;
  1940. if (virt_dev->tt_info)
  1941. old_active_eps = virt_dev->tt_info->active_eps;
  1942. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  1943. for (i = 0; i < 31; i++) {
  1944. if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
  1945. continue;
  1946. /* Make a copy of the BW info in case we need to revert this */
  1947. memcpy(&ep_bw_info[i], &virt_dev->eps[i].bw_info,
  1948. sizeof(ep_bw_info[i]));
  1949. /* Drop the endpoint from the interval table if the endpoint is
  1950. * being dropped or changed.
  1951. */
  1952. if (EP_IS_DROPPED(ctrl_ctx, i))
  1953. xhci_drop_ep_from_interval_table(xhci,
  1954. &virt_dev->eps[i].bw_info,
  1955. virt_dev->bw_table,
  1956. virt_dev->udev,
  1957. &virt_dev->eps[i],
  1958. virt_dev->tt_info);
  1959. }
  1960. /* Overwrite the information stored in the endpoints' bw_info */
  1961. xhci_update_bw_info(xhci, virt_dev->in_ctx, ctrl_ctx, virt_dev);
  1962. for (i = 0; i < 31; i++) {
  1963. /* Add any changed or added endpoints to the interval table */
  1964. if (EP_IS_ADDED(ctrl_ctx, i))
  1965. xhci_add_ep_to_interval_table(xhci,
  1966. &virt_dev->eps[i].bw_info,
  1967. virt_dev->bw_table,
  1968. virt_dev->udev,
  1969. &virt_dev->eps[i],
  1970. virt_dev->tt_info);
  1971. }
  1972. if (!xhci_check_bw_table(xhci, virt_dev, old_active_eps)) {
  1973. /* Ok, this fits in the bandwidth we have.
  1974. * Update the number of active TTs.
  1975. */
  1976. xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
  1977. return 0;
  1978. }
  1979. /* We don't have enough bandwidth for this, revert the stored info. */
  1980. for (i = 0; i < 31; i++) {
  1981. if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
  1982. continue;
  1983. /* Drop the new copies of any added or changed endpoints from
  1984. * the interval table.
  1985. */
  1986. if (EP_IS_ADDED(ctrl_ctx, i)) {
  1987. xhci_drop_ep_from_interval_table(xhci,
  1988. &virt_dev->eps[i].bw_info,
  1989. virt_dev->bw_table,
  1990. virt_dev->udev,
  1991. &virt_dev->eps[i],
  1992. virt_dev->tt_info);
  1993. }
  1994. /* Revert the endpoint back to its old information */
  1995. memcpy(&virt_dev->eps[i].bw_info, &ep_bw_info[i],
  1996. sizeof(ep_bw_info[i]));
  1997. /* Add any changed or dropped endpoints back into the table */
  1998. if (EP_IS_DROPPED(ctrl_ctx, i))
  1999. xhci_add_ep_to_interval_table(xhci,
  2000. &virt_dev->eps[i].bw_info,
  2001. virt_dev->bw_table,
  2002. virt_dev->udev,
  2003. &virt_dev->eps[i],
  2004. virt_dev->tt_info);
  2005. }
  2006. return -ENOMEM;
  2007. }
  2008. /* Issue a configure endpoint command or evaluate context command
  2009. * and wait for it to finish.
  2010. */
  2011. static int xhci_configure_endpoint(struct xhci_hcd *xhci,
  2012. struct usb_device *udev,
  2013. struct xhci_command *command,
  2014. bool ctx_change, bool must_succeed)
  2015. {
  2016. int ret;
  2017. int timeleft;
  2018. unsigned long flags;
  2019. struct xhci_container_ctx *in_ctx;
  2020. struct completion *cmd_completion;
  2021. u32 *cmd_status;
  2022. struct xhci_virt_device *virt_dev;
  2023. spin_lock_irqsave(&xhci->lock, flags);
  2024. virt_dev = xhci->devs[udev->slot_id];
  2025. if (command)
  2026. in_ctx = command->in_ctx;
  2027. else
  2028. in_ctx = virt_dev->in_ctx;
  2029. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK) &&
  2030. xhci_reserve_host_resources(xhci, in_ctx)) {
  2031. spin_unlock_irqrestore(&xhci->lock, flags);
  2032. xhci_warn(xhci, "Not enough host resources, "
  2033. "active endpoint contexts = %u\n",
  2034. xhci->num_active_eps);
  2035. return -ENOMEM;
  2036. }
  2037. if ((xhci->quirks & XHCI_SW_BW_CHECKING) &&
  2038. xhci_reserve_bandwidth(xhci, virt_dev, in_ctx)) {
  2039. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
  2040. xhci_free_host_resources(xhci, in_ctx);
  2041. spin_unlock_irqrestore(&xhci->lock, flags);
  2042. xhci_warn(xhci, "Not enough bandwidth\n");
  2043. return -ENOMEM;
  2044. }
  2045. if (command) {
  2046. cmd_completion = command->completion;
  2047. cmd_status = &command->status;
  2048. command->command_trb = xhci->cmd_ring->enqueue;
  2049. /* Enqueue pointer can be left pointing to the link TRB,
  2050. * we must handle that
  2051. */
  2052. if (TRB_TYPE_LINK_LE32(command->command_trb->link.control))
  2053. command->command_trb =
  2054. xhci->cmd_ring->enq_seg->next->trbs;
  2055. list_add_tail(&command->cmd_list, &virt_dev->cmd_list);
  2056. } else {
  2057. cmd_completion = &virt_dev->cmd_completion;
  2058. cmd_status = &virt_dev->cmd_status;
  2059. }
  2060. init_completion(cmd_completion);
  2061. if (!ctx_change)
  2062. ret = xhci_queue_configure_endpoint(xhci, in_ctx->dma,
  2063. udev->slot_id, must_succeed);
  2064. else
  2065. ret = xhci_queue_evaluate_context(xhci, in_ctx->dma,
  2066. udev->slot_id);
  2067. if (ret < 0) {
  2068. if (command)
  2069. list_del(&command->cmd_list);
  2070. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
  2071. xhci_free_host_resources(xhci, in_ctx);
  2072. spin_unlock_irqrestore(&xhci->lock, flags);
  2073. xhci_dbg(xhci, "FIXME allocate a new ring segment\n");
  2074. return -ENOMEM;
  2075. }
  2076. xhci_ring_cmd_db(xhci);
  2077. spin_unlock_irqrestore(&xhci->lock, flags);
  2078. /* Wait for the configure endpoint command to complete */
  2079. timeleft = wait_for_completion_interruptible_timeout(
  2080. cmd_completion,
  2081. USB_CTRL_SET_TIMEOUT);
  2082. if (timeleft <= 0) {
  2083. xhci_warn(xhci, "%s while waiting for %s command\n",
  2084. timeleft == 0 ? "Timeout" : "Signal",
  2085. ctx_change == 0 ?
  2086. "configure endpoint" :
  2087. "evaluate context");
  2088. /* FIXME cancel the configure endpoint command */
  2089. return -ETIME;
  2090. }
  2091. if (!ctx_change)
  2092. ret = xhci_configure_endpoint_result(xhci, udev, cmd_status);
  2093. else
  2094. ret = xhci_evaluate_context_result(xhci, udev, cmd_status);
  2095. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
  2096. spin_lock_irqsave(&xhci->lock, flags);
  2097. /* If the command failed, remove the reserved resources.
  2098. * Otherwise, clean up the estimate to include dropped eps.
  2099. */
  2100. if (ret)
  2101. xhci_free_host_resources(xhci, in_ctx);
  2102. else
  2103. xhci_finish_resource_reservation(xhci, in_ctx);
  2104. spin_unlock_irqrestore(&xhci->lock, flags);
  2105. }
  2106. return ret;
  2107. }
  2108. /* Called after one or more calls to xhci_add_endpoint() or
  2109. * xhci_drop_endpoint(). If this call fails, the USB core is expected
  2110. * to call xhci_reset_bandwidth().
  2111. *
  2112. * Since we are in the middle of changing either configuration or
  2113. * installing a new alt setting, the USB core won't allow URBs to be
  2114. * enqueued for any endpoint on the old config or interface. Nothing
  2115. * else should be touching the xhci->devs[slot_id] structure, so we
  2116. * don't need to take the xhci->lock for manipulating that.
  2117. */
  2118. int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
  2119. {
  2120. int i;
  2121. int ret = 0;
  2122. struct xhci_hcd *xhci;
  2123. struct xhci_virt_device *virt_dev;
  2124. struct xhci_input_control_ctx *ctrl_ctx;
  2125. struct xhci_slot_ctx *slot_ctx;
  2126. ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
  2127. if (ret <= 0)
  2128. return ret;
  2129. xhci = hcd_to_xhci(hcd);
  2130. if (xhci->xhc_state & XHCI_STATE_DYING)
  2131. return -ENODEV;
  2132. xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
  2133. virt_dev = xhci->devs[udev->slot_id];
  2134. /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
  2135. ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
  2136. ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
  2137. ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG);
  2138. ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG));
  2139. /* Don't issue the command if there's no endpoints to update. */
  2140. if (ctrl_ctx->add_flags == cpu_to_le32(SLOT_FLAG) &&
  2141. ctrl_ctx->drop_flags == 0)
  2142. return 0;
  2143. xhci_dbg(xhci, "New Input Control Context:\n");
  2144. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
  2145. xhci_dbg_ctx(xhci, virt_dev->in_ctx,
  2146. LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx->dev_info)));
  2147. ret = xhci_configure_endpoint(xhci, udev, NULL,
  2148. false, false);
  2149. if (ret) {
  2150. /* Callee should call reset_bandwidth() */
  2151. return ret;
  2152. }
  2153. xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
  2154. xhci_dbg_ctx(xhci, virt_dev->out_ctx,
  2155. LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx->dev_info)));
  2156. /* Free any rings that were dropped, but not changed. */
  2157. for (i = 1; i < 31; ++i) {
  2158. if ((le32_to_cpu(ctrl_ctx->drop_flags) & (1 << (i + 1))) &&
  2159. !(le32_to_cpu(ctrl_ctx->add_flags) & (1 << (i + 1))))
  2160. xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
  2161. }
  2162. xhci_zero_in_ctx(xhci, virt_dev);
  2163. /*
  2164. * Install any rings for completely new endpoints or changed endpoints,
  2165. * and free or cache any old rings from changed endpoints.
  2166. */
  2167. for (i = 1; i < 31; ++i) {
  2168. if (!virt_dev->eps[i].new_ring)
  2169. continue;
  2170. /* Only cache or free the old ring if it exists.
  2171. * It may not if this is the first add of an endpoint.
  2172. */
  2173. if (virt_dev->eps[i].ring) {
  2174. xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
  2175. }
  2176. virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
  2177. virt_dev->eps[i].new_ring = NULL;
  2178. }
  2179. return ret;
  2180. }
  2181. void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
  2182. {
  2183. struct xhci_hcd *xhci;
  2184. struct xhci_virt_device *virt_dev;
  2185. int i, ret;
  2186. ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
  2187. if (ret <= 0)
  2188. return;
  2189. xhci = hcd_to_xhci(hcd);
  2190. xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
  2191. virt_dev = xhci->devs[udev->slot_id];
  2192. /* Free any rings allocated for added endpoints */
  2193. for (i = 0; i < 31; ++i) {
  2194. if (virt_dev->eps[i].new_ring) {
  2195. xhci_ring_free(xhci, virt_dev->eps[i].new_ring);
  2196. virt_dev->eps[i].new_ring = NULL;
  2197. }
  2198. }
  2199. xhci_zero_in_ctx(xhci, virt_dev);
  2200. }
  2201. static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci,
  2202. struct xhci_container_ctx *in_ctx,
  2203. struct xhci_container_ctx *out_ctx,
  2204. u32 add_flags, u32 drop_flags)
  2205. {
  2206. struct xhci_input_control_ctx *ctrl_ctx;
  2207. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  2208. ctrl_ctx->add_flags = cpu_to_le32(add_flags);
  2209. ctrl_ctx->drop_flags = cpu_to_le32(drop_flags);
  2210. xhci_slot_copy(xhci, in_ctx, out_ctx);
  2211. ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
  2212. xhci_dbg(xhci, "Input Context:\n");
  2213. xhci_dbg_ctx(xhci, in_ctx, xhci_last_valid_endpoint(add_flags));
  2214. }
  2215. static void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci,
  2216. unsigned int slot_id, unsigned int ep_index,
  2217. struct xhci_dequeue_state *deq_state)
  2218. {
  2219. struct xhci_container_ctx *in_ctx;
  2220. struct xhci_ep_ctx *ep_ctx;
  2221. u32 added_ctxs;
  2222. dma_addr_t addr;
  2223. xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
  2224. xhci->devs[slot_id]->out_ctx, ep_index);
  2225. in_ctx = xhci->devs[slot_id]->in_ctx;
  2226. ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
  2227. addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
  2228. deq_state->new_deq_ptr);
  2229. if (addr == 0) {
  2230. xhci_warn(xhci, "WARN Cannot submit config ep after "
  2231. "reset ep command\n");
  2232. xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n",
  2233. deq_state->new_deq_seg,
  2234. deq_state->new_deq_ptr);
  2235. return;
  2236. }
  2237. ep_ctx->deq = cpu_to_le64(addr | deq_state->new_cycle_state);
  2238. added_ctxs = xhci_get_endpoint_flag_from_index(ep_index);
  2239. xhci_setup_input_ctx_for_config_ep(xhci, xhci->devs[slot_id]->in_ctx,
  2240. xhci->devs[slot_id]->out_ctx, added_ctxs, added_ctxs);
  2241. }
  2242. void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
  2243. struct usb_device *udev, unsigned int ep_index)
  2244. {
  2245. struct xhci_dequeue_state deq_state;
  2246. struct xhci_virt_ep *ep;
  2247. xhci_dbg(xhci, "Cleaning up stalled endpoint ring\n");
  2248. ep = &xhci->devs[udev->slot_id]->eps[ep_index];
  2249. /* We need to move the HW's dequeue pointer past this TD,
  2250. * or it will attempt to resend it on the next doorbell ring.
  2251. */
  2252. xhci_find_new_dequeue_state(xhci, udev->slot_id,
  2253. ep_index, ep->stopped_stream, ep->stopped_td,
  2254. &deq_state);
  2255. /* HW with the reset endpoint quirk will use the saved dequeue state to
  2256. * issue a configure endpoint command later.
  2257. */
  2258. if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) {
  2259. xhci_dbg(xhci, "Queueing new dequeue state\n");
  2260. xhci_queue_new_dequeue_state(xhci, udev->slot_id,
  2261. ep_index, ep->stopped_stream, &deq_state);
  2262. } else {
  2263. /* Better hope no one uses the input context between now and the
  2264. * reset endpoint completion!
  2265. * XXX: No idea how this hardware will react when stream rings
  2266. * are enabled.
  2267. */
  2268. xhci_dbg(xhci, "Setting up input context for "
  2269. "configure endpoint command\n");
  2270. xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id,
  2271. ep_index, &deq_state);
  2272. }
  2273. }
  2274. /* Deal with stalled endpoints. The core should have sent the control message
  2275. * to clear the halt condition. However, we need to make the xHCI hardware
  2276. * reset its sequence number, since a device will expect a sequence number of
  2277. * zero after the halt condition is cleared.
  2278. * Context: in_interrupt
  2279. */
  2280. void xhci_endpoint_reset(struct usb_hcd *hcd,
  2281. struct usb_host_endpoint *ep)
  2282. {
  2283. struct xhci_hcd *xhci;
  2284. struct usb_device *udev;
  2285. unsigned int ep_index;
  2286. unsigned long flags;
  2287. int ret;
  2288. struct xhci_virt_ep *virt_ep;
  2289. xhci = hcd_to_xhci(hcd);
  2290. udev = (struct usb_device *) ep->hcpriv;
  2291. /* Called with a root hub endpoint (or an endpoint that wasn't added
  2292. * with xhci_add_endpoint()
  2293. */
  2294. if (!ep->hcpriv)
  2295. return;
  2296. ep_index = xhci_get_endpoint_index(&ep->desc);
  2297. virt_ep = &xhci->devs[udev->slot_id]->eps[ep_index];
  2298. if (!virt_ep->stopped_td) {
  2299. xhci_dbg(xhci, "Endpoint 0x%x not halted, refusing to reset.\n",
  2300. ep->desc.bEndpointAddress);
  2301. return;
  2302. }
  2303. if (usb_endpoint_xfer_control(&ep->desc)) {
  2304. xhci_dbg(xhci, "Control endpoint stall already handled.\n");
  2305. return;
  2306. }
  2307. xhci_dbg(xhci, "Queueing reset endpoint command\n");
  2308. spin_lock_irqsave(&xhci->lock, flags);
  2309. ret = xhci_queue_reset_ep(xhci, udev->slot_id, ep_index);
  2310. /*
  2311. * Can't change the ring dequeue pointer until it's transitioned to the
  2312. * stopped state, which is only upon a successful reset endpoint
  2313. * command. Better hope that last command worked!
  2314. */
  2315. if (!ret) {
  2316. xhci_cleanup_stalled_ring(xhci, udev, ep_index);
  2317. kfree(virt_ep->stopped_td);
  2318. xhci_ring_cmd_db(xhci);
  2319. }
  2320. virt_ep->stopped_td = NULL;
  2321. virt_ep->stopped_trb = NULL;
  2322. virt_ep->stopped_stream = 0;
  2323. spin_unlock_irqrestore(&xhci->lock, flags);
  2324. if (ret)
  2325. xhci_warn(xhci, "FIXME allocate a new ring segment\n");
  2326. }
  2327. static int xhci_check_streams_endpoint(struct xhci_hcd *xhci,
  2328. struct usb_device *udev, struct usb_host_endpoint *ep,
  2329. unsigned int slot_id)
  2330. {
  2331. int ret;
  2332. unsigned int ep_index;
  2333. unsigned int ep_state;
  2334. if (!ep)
  2335. return -EINVAL;
  2336. ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, true, __func__);
  2337. if (ret <= 0)
  2338. return -EINVAL;
  2339. if (ep->ss_ep_comp.bmAttributes == 0) {
  2340. xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion"
  2341. " descriptor for ep 0x%x does not support streams\n",
  2342. ep->desc.bEndpointAddress);
  2343. return -EINVAL;
  2344. }
  2345. ep_index = xhci_get_endpoint_index(&ep->desc);
  2346. ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
  2347. if (ep_state & EP_HAS_STREAMS ||
  2348. ep_state & EP_GETTING_STREAMS) {
  2349. xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x "
  2350. "already has streams set up.\n",
  2351. ep->desc.bEndpointAddress);
  2352. xhci_warn(xhci, "Send email to xHCI maintainer and ask for "
  2353. "dynamic stream context array reallocation.\n");
  2354. return -EINVAL;
  2355. }
  2356. if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) {
  2357. xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk "
  2358. "endpoint 0x%x; URBs are pending.\n",
  2359. ep->desc.bEndpointAddress);
  2360. return -EINVAL;
  2361. }
  2362. return 0;
  2363. }
  2364. static void xhci_calculate_streams_entries(struct xhci_hcd *xhci,
  2365. unsigned int *num_streams, unsigned int *num_stream_ctxs)
  2366. {
  2367. unsigned int max_streams;
  2368. /* The stream context array size must be a power of two */
  2369. *num_stream_ctxs = roundup_pow_of_two(*num_streams);
  2370. /*
  2371. * Find out how many primary stream array entries the host controller
  2372. * supports. Later we may use secondary stream arrays (similar to 2nd
  2373. * level page entries), but that's an optional feature for xHCI host
  2374. * controllers. xHCs must support at least 4 stream IDs.
  2375. */
  2376. max_streams = HCC_MAX_PSA(xhci->hcc_params);
  2377. if (*num_stream_ctxs > max_streams) {
  2378. xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n",
  2379. max_streams);
  2380. *num_stream_ctxs = max_streams;
  2381. *num_streams = max_streams;
  2382. }
  2383. }
  2384. /* Returns an error code if one of the endpoint already has streams.
  2385. * This does not change any data structures, it only checks and gathers
  2386. * information.
  2387. */
  2388. static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci,
  2389. struct usb_device *udev,
  2390. struct usb_host_endpoint **eps, unsigned int num_eps,
  2391. unsigned int *num_streams, u32 *changed_ep_bitmask)
  2392. {
  2393. unsigned int max_streams;
  2394. unsigned int endpoint_flag;
  2395. int i;
  2396. int ret;
  2397. for (i = 0; i < num_eps; i++) {
  2398. ret = xhci_check_streams_endpoint(xhci, udev,
  2399. eps[i], udev->slot_id);
  2400. if (ret < 0)
  2401. return ret;
  2402. max_streams = USB_SS_MAX_STREAMS(
  2403. eps[i]->ss_ep_comp.bmAttributes);
  2404. if (max_streams < (*num_streams - 1)) {
  2405. xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n",
  2406. eps[i]->desc.bEndpointAddress,
  2407. max_streams);
  2408. *num_streams = max_streams+1;
  2409. }
  2410. endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc);
  2411. if (*changed_ep_bitmask & endpoint_flag)
  2412. return -EINVAL;
  2413. *changed_ep_bitmask |= endpoint_flag;
  2414. }
  2415. return 0;
  2416. }
  2417. static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci,
  2418. struct usb_device *udev,
  2419. struct usb_host_endpoint **eps, unsigned int num_eps)
  2420. {
  2421. u32 changed_ep_bitmask = 0;
  2422. unsigned int slot_id;
  2423. unsigned int ep_index;
  2424. unsigned int ep_state;
  2425. int i;
  2426. slot_id = udev->slot_id;
  2427. if (!xhci->devs[slot_id])
  2428. return 0;
  2429. for (i = 0; i < num_eps; i++) {
  2430. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2431. ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
  2432. /* Are streams already being freed for the endpoint? */
  2433. if (ep_state & EP_GETTING_NO_STREAMS) {
  2434. xhci_warn(xhci, "WARN Can't disable streams for "
  2435. "endpoint 0x%x\n, "
  2436. "streams are being disabled already.",
  2437. eps[i]->desc.bEndpointAddress);
  2438. return 0;
  2439. }
  2440. /* Are there actually any streams to free? */
  2441. if (!(ep_state & EP_HAS_STREAMS) &&
  2442. !(ep_state & EP_GETTING_STREAMS)) {
  2443. xhci_warn(xhci, "WARN Can't disable streams for "
  2444. "endpoint 0x%x\n, "
  2445. "streams are already disabled!",
  2446. eps[i]->desc.bEndpointAddress);
  2447. xhci_warn(xhci, "WARN xhci_free_streams() called "
  2448. "with non-streams endpoint\n");
  2449. return 0;
  2450. }
  2451. changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc);
  2452. }
  2453. return changed_ep_bitmask;
  2454. }
  2455. /*
  2456. * The USB device drivers use this function (though the HCD interface in USB
  2457. * core) to prepare a set of bulk endpoints to use streams. Streams are used to
  2458. * coordinate mass storage command queueing across multiple endpoints (basically
  2459. * a stream ID == a task ID).
  2460. *
  2461. * Setting up streams involves allocating the same size stream context array
  2462. * for each endpoint and issuing a configure endpoint command for all endpoints.
  2463. *
  2464. * Don't allow the call to succeed if one endpoint only supports one stream
  2465. * (which means it doesn't support streams at all).
  2466. *
  2467. * Drivers may get less stream IDs than they asked for, if the host controller
  2468. * hardware or endpoints claim they can't support the number of requested
  2469. * stream IDs.
  2470. */
  2471. int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
  2472. struct usb_host_endpoint **eps, unsigned int num_eps,
  2473. unsigned int num_streams, gfp_t mem_flags)
  2474. {
  2475. int i, ret;
  2476. struct xhci_hcd *xhci;
  2477. struct xhci_virt_device *vdev;
  2478. struct xhci_command *config_cmd;
  2479. unsigned int ep_index;
  2480. unsigned int num_stream_ctxs;
  2481. unsigned long flags;
  2482. u32 changed_ep_bitmask = 0;
  2483. if (!eps)
  2484. return -EINVAL;
  2485. /* Add one to the number of streams requested to account for
  2486. * stream 0 that is reserved for xHCI usage.
  2487. */
  2488. num_streams += 1;
  2489. xhci = hcd_to_xhci(hcd);
  2490. xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n",
  2491. num_streams);
  2492. config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
  2493. if (!config_cmd) {
  2494. xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
  2495. return -ENOMEM;
  2496. }
  2497. /* Check to make sure all endpoints are not already configured for
  2498. * streams. While we're at it, find the maximum number of streams that
  2499. * all the endpoints will support and check for duplicate endpoints.
  2500. */
  2501. spin_lock_irqsave(&xhci->lock, flags);
  2502. ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps,
  2503. num_eps, &num_streams, &changed_ep_bitmask);
  2504. if (ret < 0) {
  2505. xhci_free_command(xhci, config_cmd);
  2506. spin_unlock_irqrestore(&xhci->lock, flags);
  2507. return ret;
  2508. }
  2509. if (num_streams <= 1) {
  2510. xhci_warn(xhci, "WARN: endpoints can't handle "
  2511. "more than one stream.\n");
  2512. xhci_free_command(xhci, config_cmd);
  2513. spin_unlock_irqrestore(&xhci->lock, flags);
  2514. return -EINVAL;
  2515. }
  2516. vdev = xhci->devs[udev->slot_id];
  2517. /* Mark each endpoint as being in transition, so
  2518. * xhci_urb_enqueue() will reject all URBs.
  2519. */
  2520. for (i = 0; i < num_eps; i++) {
  2521. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2522. vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS;
  2523. }
  2524. spin_unlock_irqrestore(&xhci->lock, flags);
  2525. /* Setup internal data structures and allocate HW data structures for
  2526. * streams (but don't install the HW structures in the input context
  2527. * until we're sure all memory allocation succeeded).
  2528. */
  2529. xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs);
  2530. xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n",
  2531. num_stream_ctxs, num_streams);
  2532. for (i = 0; i < num_eps; i++) {
  2533. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2534. vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci,
  2535. num_stream_ctxs,
  2536. num_streams, mem_flags);
  2537. if (!vdev->eps[ep_index].stream_info)
  2538. goto cleanup;
  2539. /* Set maxPstreams in endpoint context and update deq ptr to
  2540. * point to stream context array. FIXME
  2541. */
  2542. }
  2543. /* Set up the input context for a configure endpoint command. */
  2544. for (i = 0; i < num_eps; i++) {
  2545. struct xhci_ep_ctx *ep_ctx;
  2546. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2547. ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index);
  2548. xhci_endpoint_copy(xhci, config_cmd->in_ctx,
  2549. vdev->out_ctx, ep_index);
  2550. xhci_setup_streams_ep_input_ctx(xhci, ep_ctx,
  2551. vdev->eps[ep_index].stream_info);
  2552. }
  2553. /* Tell the HW to drop its old copy of the endpoint context info
  2554. * and add the updated copy from the input context.
  2555. */
  2556. xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx,
  2557. vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
  2558. /* Issue and wait for the configure endpoint command */
  2559. ret = xhci_configure_endpoint(xhci, udev, config_cmd,
  2560. false, false);
  2561. /* xHC rejected the configure endpoint command for some reason, so we
  2562. * leave the old ring intact and free our internal streams data
  2563. * structure.
  2564. */
  2565. if (ret < 0)
  2566. goto cleanup;
  2567. spin_lock_irqsave(&xhci->lock, flags);
  2568. for (i = 0; i < num_eps; i++) {
  2569. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2570. vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
  2571. xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n",
  2572. udev->slot_id, ep_index);
  2573. vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS;
  2574. }
  2575. xhci_free_command(xhci, config_cmd);
  2576. spin_unlock_irqrestore(&xhci->lock, flags);
  2577. /* Subtract 1 for stream 0, which drivers can't use */
  2578. return num_streams - 1;
  2579. cleanup:
  2580. /* If it didn't work, free the streams! */
  2581. for (i = 0; i < num_eps; i++) {
  2582. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2583. xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
  2584. vdev->eps[ep_index].stream_info = NULL;
  2585. /* FIXME Unset maxPstreams in endpoint context and
  2586. * update deq ptr to point to normal string ring.
  2587. */
  2588. vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
  2589. vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
  2590. xhci_endpoint_zero(xhci, vdev, eps[i]);
  2591. }
  2592. xhci_free_command(xhci, config_cmd);
  2593. return -ENOMEM;
  2594. }
  2595. /* Transition the endpoint from using streams to being a "normal" endpoint
  2596. * without streams.
  2597. *
  2598. * Modify the endpoint context state, submit a configure endpoint command,
  2599. * and free all endpoint rings for streams if that completes successfully.
  2600. */
  2601. int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
  2602. struct usb_host_endpoint **eps, unsigned int num_eps,
  2603. gfp_t mem_flags)
  2604. {
  2605. int i, ret;
  2606. struct xhci_hcd *xhci;
  2607. struct xhci_virt_device *vdev;
  2608. struct xhci_command *command;
  2609. unsigned int ep_index;
  2610. unsigned long flags;
  2611. u32 changed_ep_bitmask;
  2612. xhci = hcd_to_xhci(hcd);
  2613. vdev = xhci->devs[udev->slot_id];
  2614. /* Set up a configure endpoint command to remove the streams rings */
  2615. spin_lock_irqsave(&xhci->lock, flags);
  2616. changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci,
  2617. udev, eps, num_eps);
  2618. if (changed_ep_bitmask == 0) {
  2619. spin_unlock_irqrestore(&xhci->lock, flags);
  2620. return -EINVAL;
  2621. }
  2622. /* Use the xhci_command structure from the first endpoint. We may have
  2623. * allocated too many, but the driver may call xhci_free_streams() for
  2624. * each endpoint it grouped into one call to xhci_alloc_streams().
  2625. */
  2626. ep_index = xhci_get_endpoint_index(&eps[0]->desc);
  2627. command = vdev->eps[ep_index].stream_info->free_streams_command;
  2628. for (i = 0; i < num_eps; i++) {
  2629. struct xhci_ep_ctx *ep_ctx;
  2630. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2631. ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
  2632. xhci->devs[udev->slot_id]->eps[ep_index].ep_state |=
  2633. EP_GETTING_NO_STREAMS;
  2634. xhci_endpoint_copy(xhci, command->in_ctx,
  2635. vdev->out_ctx, ep_index);
  2636. xhci_setup_no_streams_ep_input_ctx(xhci, ep_ctx,
  2637. &vdev->eps[ep_index]);
  2638. }
  2639. xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx,
  2640. vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
  2641. spin_unlock_irqrestore(&xhci->lock, flags);
  2642. /* Issue and wait for the configure endpoint command,
  2643. * which must succeed.
  2644. */
  2645. ret = xhci_configure_endpoint(xhci, udev, command,
  2646. false, true);
  2647. /* xHC rejected the configure endpoint command for some reason, so we
  2648. * leave the streams rings intact.
  2649. */
  2650. if (ret < 0)
  2651. return ret;
  2652. spin_lock_irqsave(&xhci->lock, flags);
  2653. for (i = 0; i < num_eps; i++) {
  2654. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2655. xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
  2656. vdev->eps[ep_index].stream_info = NULL;
  2657. /* FIXME Unset maxPstreams in endpoint context and
  2658. * update deq ptr to point to normal string ring.
  2659. */
  2660. vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS;
  2661. vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
  2662. }
  2663. spin_unlock_irqrestore(&xhci->lock, flags);
  2664. return 0;
  2665. }
  2666. /*
  2667. * Deletes endpoint resources for endpoints that were active before a Reset
  2668. * Device command, or a Disable Slot command. The Reset Device command leaves
  2669. * the control endpoint intact, whereas the Disable Slot command deletes it.
  2670. *
  2671. * Must be called with xhci->lock held.
  2672. */
  2673. void xhci_free_device_endpoint_resources(struct xhci_hcd *xhci,
  2674. struct xhci_virt_device *virt_dev, bool drop_control_ep)
  2675. {
  2676. int i;
  2677. unsigned int num_dropped_eps = 0;
  2678. unsigned int drop_flags = 0;
  2679. for (i = (drop_control_ep ? 0 : 1); i < 31; i++) {
  2680. if (virt_dev->eps[i].ring) {
  2681. drop_flags |= 1 << i;
  2682. num_dropped_eps++;
  2683. }
  2684. }
  2685. xhci->num_active_eps -= num_dropped_eps;
  2686. if (num_dropped_eps)
  2687. xhci_dbg(xhci, "Dropped %u ep ctxs, flags = 0x%x, "
  2688. "%u now active.\n",
  2689. num_dropped_eps, drop_flags,
  2690. xhci->num_active_eps);
  2691. }
  2692. /*
  2693. * This submits a Reset Device Command, which will set the device state to 0,
  2694. * set the device address to 0, and disable all the endpoints except the default
  2695. * control endpoint. The USB core should come back and call
  2696. * xhci_address_device(), and then re-set up the configuration. If this is
  2697. * called because of a usb_reset_and_verify_device(), then the old alternate
  2698. * settings will be re-installed through the normal bandwidth allocation
  2699. * functions.
  2700. *
  2701. * Wait for the Reset Device command to finish. Remove all structures
  2702. * associated with the endpoints that were disabled. Clear the input device
  2703. * structure? Cache the rings? Reset the control endpoint 0 max packet size?
  2704. *
  2705. * If the virt_dev to be reset does not exist or does not match the udev,
  2706. * it means the device is lost, possibly due to the xHC restore error and
  2707. * re-initialization during S3/S4. In this case, call xhci_alloc_dev() to
  2708. * re-allocate the device.
  2709. */
  2710. int xhci_discover_or_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
  2711. {
  2712. int ret, i;
  2713. unsigned long flags;
  2714. struct xhci_hcd *xhci;
  2715. unsigned int slot_id;
  2716. struct xhci_virt_device *virt_dev;
  2717. struct xhci_command *reset_device_cmd;
  2718. int timeleft;
  2719. int last_freed_endpoint;
  2720. struct xhci_slot_ctx *slot_ctx;
  2721. int old_active_eps = 0;
  2722. ret = xhci_check_args(hcd, udev, NULL, 0, false, __func__);
  2723. if (ret <= 0)
  2724. return ret;
  2725. xhci = hcd_to_xhci(hcd);
  2726. slot_id = udev->slot_id;
  2727. virt_dev = xhci->devs[slot_id];
  2728. if (!virt_dev) {
  2729. xhci_dbg(xhci, "The device to be reset with slot ID %u does "
  2730. "not exist. Re-allocate the device\n", slot_id);
  2731. ret = xhci_alloc_dev(hcd, udev);
  2732. if (ret == 1)
  2733. return 0;
  2734. else
  2735. return -EINVAL;
  2736. }
  2737. if (virt_dev->udev != udev) {
  2738. /* If the virt_dev and the udev does not match, this virt_dev
  2739. * may belong to another udev.
  2740. * Re-allocate the device.
  2741. */
  2742. xhci_dbg(xhci, "The device to be reset with slot ID %u does "
  2743. "not match the udev. Re-allocate the device\n",
  2744. slot_id);
  2745. ret = xhci_alloc_dev(hcd, udev);
  2746. if (ret == 1)
  2747. return 0;
  2748. else
  2749. return -EINVAL;
  2750. }
  2751. /* If device is not setup, there is no point in resetting it */
  2752. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
  2753. if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) ==
  2754. SLOT_STATE_DISABLED)
  2755. return 0;
  2756. xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id);
  2757. /* Allocate the command structure that holds the struct completion.
  2758. * Assume we're in process context, since the normal device reset
  2759. * process has to wait for the device anyway. Storage devices are
  2760. * reset as part of error handling, so use GFP_NOIO instead of
  2761. * GFP_KERNEL.
  2762. */
  2763. reset_device_cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
  2764. if (!reset_device_cmd) {
  2765. xhci_dbg(xhci, "Couldn't allocate command structure.\n");
  2766. return -ENOMEM;
  2767. }
  2768. /* Attempt to submit the Reset Device command to the command ring */
  2769. spin_lock_irqsave(&xhci->lock, flags);
  2770. reset_device_cmd->command_trb = xhci->cmd_ring->enqueue;
  2771. /* Enqueue pointer can be left pointing to the link TRB,
  2772. * we must handle that
  2773. */
  2774. if (TRB_TYPE_LINK_LE32(reset_device_cmd->command_trb->link.control))
  2775. reset_device_cmd->command_trb =
  2776. xhci->cmd_ring->enq_seg->next->trbs;
  2777. list_add_tail(&reset_device_cmd->cmd_list, &virt_dev->cmd_list);
  2778. ret = xhci_queue_reset_device(xhci, slot_id);
  2779. if (ret) {
  2780. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  2781. list_del(&reset_device_cmd->cmd_list);
  2782. spin_unlock_irqrestore(&xhci->lock, flags);
  2783. goto command_cleanup;
  2784. }
  2785. xhci_ring_cmd_db(xhci);
  2786. spin_unlock_irqrestore(&xhci->lock, flags);
  2787. /* Wait for the Reset Device command to finish */
  2788. timeleft = wait_for_completion_interruptible_timeout(
  2789. reset_device_cmd->completion,
  2790. USB_CTRL_SET_TIMEOUT);
  2791. if (timeleft <= 0) {
  2792. xhci_warn(xhci, "%s while waiting for reset device command\n",
  2793. timeleft == 0 ? "Timeout" : "Signal");
  2794. spin_lock_irqsave(&xhci->lock, flags);
  2795. /* The timeout might have raced with the event ring handler, so
  2796. * only delete from the list if the item isn't poisoned.
  2797. */
  2798. if (reset_device_cmd->cmd_list.next != LIST_POISON1)
  2799. list_del(&reset_device_cmd->cmd_list);
  2800. spin_unlock_irqrestore(&xhci->lock, flags);
  2801. ret = -ETIME;
  2802. goto command_cleanup;
  2803. }
  2804. /* The Reset Device command can't fail, according to the 0.95/0.96 spec,
  2805. * unless we tried to reset a slot ID that wasn't enabled,
  2806. * or the device wasn't in the addressed or configured state.
  2807. */
  2808. ret = reset_device_cmd->status;
  2809. switch (ret) {
  2810. case COMP_EBADSLT: /* 0.95 completion code for bad slot ID */
  2811. case COMP_CTX_STATE: /* 0.96 completion code for same thing */
  2812. xhci_info(xhci, "Can't reset device (slot ID %u) in %s state\n",
  2813. slot_id,
  2814. xhci_get_slot_state(xhci, virt_dev->out_ctx));
  2815. xhci_info(xhci, "Not freeing device rings.\n");
  2816. /* Don't treat this as an error. May change my mind later. */
  2817. ret = 0;
  2818. goto command_cleanup;
  2819. case COMP_SUCCESS:
  2820. xhci_dbg(xhci, "Successful reset device command.\n");
  2821. break;
  2822. default:
  2823. if (xhci_is_vendor_info_code(xhci, ret))
  2824. break;
  2825. xhci_warn(xhci, "Unknown completion code %u for "
  2826. "reset device command.\n", ret);
  2827. ret = -EINVAL;
  2828. goto command_cleanup;
  2829. }
  2830. /* Free up host controller endpoint resources */
  2831. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
  2832. spin_lock_irqsave(&xhci->lock, flags);
  2833. /* Don't delete the default control endpoint resources */
  2834. xhci_free_device_endpoint_resources(xhci, virt_dev, false);
  2835. spin_unlock_irqrestore(&xhci->lock, flags);
  2836. }
  2837. /* Everything but endpoint 0 is disabled, so free or cache the rings. */
  2838. last_freed_endpoint = 1;
  2839. for (i = 1; i < 31; ++i) {
  2840. struct xhci_virt_ep *ep = &virt_dev->eps[i];
  2841. if (ep->ep_state & EP_HAS_STREAMS) {
  2842. xhci_free_stream_info(xhci, ep->stream_info);
  2843. ep->stream_info = NULL;
  2844. ep->ep_state &= ~EP_HAS_STREAMS;
  2845. }
  2846. if (ep->ring) {
  2847. xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
  2848. last_freed_endpoint = i;
  2849. }
  2850. if (!list_empty(&virt_dev->eps[i].bw_endpoint_list))
  2851. xhci_drop_ep_from_interval_table(xhci,
  2852. &virt_dev->eps[i].bw_info,
  2853. virt_dev->bw_table,
  2854. udev,
  2855. &virt_dev->eps[i],
  2856. virt_dev->tt_info);
  2857. xhci_clear_endpoint_bw_info(&virt_dev->eps[i].bw_info);
  2858. }
  2859. /* If necessary, update the number of active TTs on this root port */
  2860. xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
  2861. xhci_dbg(xhci, "Output context after successful reset device cmd:\n");
  2862. xhci_dbg_ctx(xhci, virt_dev->out_ctx, last_freed_endpoint);
  2863. ret = 0;
  2864. command_cleanup:
  2865. xhci_free_command(xhci, reset_device_cmd);
  2866. return ret;
  2867. }
  2868. /*
  2869. * At this point, the struct usb_device is about to go away, the device has
  2870. * disconnected, and all traffic has been stopped and the endpoints have been
  2871. * disabled. Free any HC data structures associated with that device.
  2872. */
  2873. void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
  2874. {
  2875. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  2876. struct xhci_virt_device *virt_dev;
  2877. unsigned long flags;
  2878. u32 state;
  2879. int i, ret;
  2880. ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
  2881. /* If the host is halted due to driver unload, we still need to free the
  2882. * device.
  2883. */
  2884. if (ret <= 0 && ret != -ENODEV)
  2885. return;
  2886. virt_dev = xhci->devs[udev->slot_id];
  2887. /* Stop any wayward timer functions (which may grab the lock) */
  2888. for (i = 0; i < 31; ++i) {
  2889. virt_dev->eps[i].ep_state &= ~EP_HALT_PENDING;
  2890. del_timer_sync(&virt_dev->eps[i].stop_cmd_timer);
  2891. }
  2892. spin_lock_irqsave(&xhci->lock, flags);
  2893. /* Don't disable the slot if the host controller is dead. */
  2894. state = xhci_readl(xhci, &xhci->op_regs->status);
  2895. if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) ||
  2896. (xhci->xhc_state & XHCI_STATE_HALTED)) {
  2897. xhci_free_virt_device(xhci, udev->slot_id);
  2898. spin_unlock_irqrestore(&xhci->lock, flags);
  2899. return;
  2900. }
  2901. if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
  2902. spin_unlock_irqrestore(&xhci->lock, flags);
  2903. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  2904. return;
  2905. }
  2906. xhci_ring_cmd_db(xhci);
  2907. spin_unlock_irqrestore(&xhci->lock, flags);
  2908. /*
  2909. * Event command completion handler will free any data structures
  2910. * associated with the slot. XXX Can free sleep?
  2911. */
  2912. }
  2913. /*
  2914. * Checks if we have enough host controller resources for the default control
  2915. * endpoint.
  2916. *
  2917. * Must be called with xhci->lock held.
  2918. */
  2919. static int xhci_reserve_host_control_ep_resources(struct xhci_hcd *xhci)
  2920. {
  2921. if (xhci->num_active_eps + 1 > xhci->limit_active_eps) {
  2922. xhci_dbg(xhci, "Not enough ep ctxs: "
  2923. "%u active, need to add 1, limit is %u.\n",
  2924. xhci->num_active_eps, xhci->limit_active_eps);
  2925. return -ENOMEM;
  2926. }
  2927. xhci->num_active_eps += 1;
  2928. xhci_dbg(xhci, "Adding 1 ep ctx, %u now active.\n",
  2929. xhci->num_active_eps);
  2930. return 0;
  2931. }
  2932. /*
  2933. * Returns 0 if the xHC ran out of device slots, the Enable Slot command
  2934. * timed out, or allocating memory failed. Returns 1 on success.
  2935. */
  2936. int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
  2937. {
  2938. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  2939. unsigned long flags;
  2940. int timeleft;
  2941. int ret;
  2942. spin_lock_irqsave(&xhci->lock, flags);
  2943. ret = xhci_queue_slot_control(xhci, TRB_ENABLE_SLOT, 0);
  2944. if (ret) {
  2945. spin_unlock_irqrestore(&xhci->lock, flags);
  2946. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  2947. return 0;
  2948. }
  2949. xhci_ring_cmd_db(xhci);
  2950. spin_unlock_irqrestore(&xhci->lock, flags);
  2951. /* XXX: how much time for xHC slot assignment? */
  2952. timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
  2953. USB_CTRL_SET_TIMEOUT);
  2954. if (timeleft <= 0) {
  2955. xhci_warn(xhci, "%s while waiting for a slot\n",
  2956. timeleft == 0 ? "Timeout" : "Signal");
  2957. /* FIXME cancel the enable slot request */
  2958. return 0;
  2959. }
  2960. if (!xhci->slot_id) {
  2961. xhci_err(xhci, "Error while assigning device slot ID\n");
  2962. return 0;
  2963. }
  2964. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
  2965. spin_lock_irqsave(&xhci->lock, flags);
  2966. ret = xhci_reserve_host_control_ep_resources(xhci);
  2967. if (ret) {
  2968. spin_unlock_irqrestore(&xhci->lock, flags);
  2969. xhci_warn(xhci, "Not enough host resources, "
  2970. "active endpoint contexts = %u\n",
  2971. xhci->num_active_eps);
  2972. goto disable_slot;
  2973. }
  2974. spin_unlock_irqrestore(&xhci->lock, flags);
  2975. }
  2976. /* Use GFP_NOIO, since this function can be called from
  2977. * xhci_discover_or_reset_device(), which may be called as part of
  2978. * mass storage driver error handling.
  2979. */
  2980. if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_NOIO)) {
  2981. xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
  2982. goto disable_slot;
  2983. }
  2984. udev->slot_id = xhci->slot_id;
  2985. /* Is this a LS or FS device under a HS hub? */
  2986. /* Hub or peripherial? */
  2987. return 1;
  2988. disable_slot:
  2989. /* Disable slot, if we can do it without mem alloc */
  2990. spin_lock_irqsave(&xhci->lock, flags);
  2991. if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
  2992. xhci_ring_cmd_db(xhci);
  2993. spin_unlock_irqrestore(&xhci->lock, flags);
  2994. return 0;
  2995. }
  2996. /*
  2997. * Issue an Address Device command (which will issue a SetAddress request to
  2998. * the device).
  2999. * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so
  3000. * we should only issue and wait on one address command at the same time.
  3001. *
  3002. * We add one to the device address issued by the hardware because the USB core
  3003. * uses address 1 for the root hubs (even though they're not really devices).
  3004. */
  3005. int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
  3006. {
  3007. unsigned long flags;
  3008. int timeleft;
  3009. struct xhci_virt_device *virt_dev;
  3010. int ret = 0;
  3011. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  3012. struct xhci_slot_ctx *slot_ctx;
  3013. struct xhci_input_control_ctx *ctrl_ctx;
  3014. u64 temp_64;
  3015. if (!udev->slot_id) {
  3016. xhci_dbg(xhci, "Bad Slot ID %d\n", udev->slot_id);
  3017. return -EINVAL;
  3018. }
  3019. virt_dev = xhci->devs[udev->slot_id];
  3020. if (WARN_ON(!virt_dev)) {
  3021. /*
  3022. * In plug/unplug torture test with an NEC controller,
  3023. * a zero-dereference was observed once due to virt_dev = 0.
  3024. * Print useful debug rather than crash if it is observed again!
  3025. */
  3026. xhci_warn(xhci, "Virt dev invalid for slot_id 0x%x!\n",
  3027. udev->slot_id);
  3028. return -EINVAL;
  3029. }
  3030. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
  3031. /*
  3032. * If this is the first Set Address since device plug-in or
  3033. * virt_device realloaction after a resume with an xHCI power loss,
  3034. * then set up the slot context.
  3035. */
  3036. if (!slot_ctx->dev_info)
  3037. xhci_setup_addressable_virt_dev(xhci, udev);
  3038. /* Otherwise, update the control endpoint ring enqueue pointer. */
  3039. else
  3040. xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev);
  3041. xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
  3042. xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
  3043. spin_lock_irqsave(&xhci->lock, flags);
  3044. ret = xhci_queue_address_device(xhci, virt_dev->in_ctx->dma,
  3045. udev->slot_id);
  3046. if (ret) {
  3047. spin_unlock_irqrestore(&xhci->lock, flags);
  3048. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  3049. return ret;
  3050. }
  3051. xhci_ring_cmd_db(xhci);
  3052. spin_unlock_irqrestore(&xhci->lock, flags);
  3053. /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
  3054. timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
  3055. USB_CTRL_SET_TIMEOUT);
  3056. /* FIXME: From section 4.3.4: "Software shall be responsible for timing
  3057. * the SetAddress() "recovery interval" required by USB and aborting the
  3058. * command on a timeout.
  3059. */
  3060. if (timeleft <= 0) {
  3061. xhci_warn(xhci, "%s while waiting for a slot\n",
  3062. timeleft == 0 ? "Timeout" : "Signal");
  3063. /* FIXME cancel the address device command */
  3064. return -ETIME;
  3065. }
  3066. switch (virt_dev->cmd_status) {
  3067. case COMP_CTX_STATE:
  3068. case COMP_EBADSLT:
  3069. xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n",
  3070. udev->slot_id);
  3071. ret = -EINVAL;
  3072. break;
  3073. case COMP_TX_ERR:
  3074. dev_warn(&udev->dev, "Device not responding to set address.\n");
  3075. ret = -EPROTO;
  3076. break;
  3077. case COMP_DEV_ERR:
  3078. dev_warn(&udev->dev, "ERROR: Incompatible device for address "
  3079. "device command.\n");
  3080. ret = -ENODEV;
  3081. break;
  3082. case COMP_SUCCESS:
  3083. xhci_dbg(xhci, "Successful Address Device command\n");
  3084. break;
  3085. default:
  3086. xhci_err(xhci, "ERROR: unexpected command completion "
  3087. "code 0x%x.\n", virt_dev->cmd_status);
  3088. xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
  3089. xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
  3090. ret = -EINVAL;
  3091. break;
  3092. }
  3093. if (ret) {
  3094. return ret;
  3095. }
  3096. temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
  3097. xhci_dbg(xhci, "Op regs DCBAA ptr = %#016llx\n", temp_64);
  3098. xhci_dbg(xhci, "Slot ID %d dcbaa entry @%p = %#016llx\n",
  3099. udev->slot_id,
  3100. &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
  3101. (unsigned long long)
  3102. le64_to_cpu(xhci->dcbaa->dev_context_ptrs[udev->slot_id]));
  3103. xhci_dbg(xhci, "Output Context DMA address = %#08llx\n",
  3104. (unsigned long long)virt_dev->out_ctx->dma);
  3105. xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
  3106. xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
  3107. xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
  3108. xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
  3109. /*
  3110. * USB core uses address 1 for the roothubs, so we add one to the
  3111. * address given back to us by the HC.
  3112. */
  3113. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
  3114. /* Use kernel assigned address for devices; store xHC assigned
  3115. * address locally. */
  3116. virt_dev->address = (le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK)
  3117. + 1;
  3118. /* Zero the input context control for later use */
  3119. ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
  3120. ctrl_ctx->add_flags = 0;
  3121. ctrl_ctx->drop_flags = 0;
  3122. xhci_dbg(xhci, "Internal device address = %d\n", virt_dev->address);
  3123. return 0;
  3124. }
  3125. /* Once a hub descriptor is fetched for a device, we need to update the xHC's
  3126. * internal data structures for the device.
  3127. */
  3128. int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
  3129. struct usb_tt *tt, gfp_t mem_flags)
  3130. {
  3131. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  3132. struct xhci_virt_device *vdev;
  3133. struct xhci_command *config_cmd;
  3134. struct xhci_input_control_ctx *ctrl_ctx;
  3135. struct xhci_slot_ctx *slot_ctx;
  3136. unsigned long flags;
  3137. unsigned think_time;
  3138. int ret;
  3139. /* Ignore root hubs */
  3140. if (!hdev->parent)
  3141. return 0;
  3142. vdev = xhci->devs[hdev->slot_id];
  3143. if (!vdev) {
  3144. xhci_warn(xhci, "Cannot update hub desc for unknown device.\n");
  3145. return -EINVAL;
  3146. }
  3147. config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
  3148. if (!config_cmd) {
  3149. xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
  3150. return -ENOMEM;
  3151. }
  3152. spin_lock_irqsave(&xhci->lock, flags);
  3153. if (hdev->speed == USB_SPEED_HIGH &&
  3154. xhci_alloc_tt_info(xhci, vdev, hdev, tt, GFP_ATOMIC)) {
  3155. xhci_dbg(xhci, "Could not allocate xHCI TT structure.\n");
  3156. xhci_free_command(xhci, config_cmd);
  3157. spin_unlock_irqrestore(&xhci->lock, flags);
  3158. return -ENOMEM;
  3159. }
  3160. xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx);
  3161. ctrl_ctx = xhci_get_input_control_ctx(xhci, config_cmd->in_ctx);
  3162. ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
  3163. slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx);
  3164. slot_ctx->dev_info |= cpu_to_le32(DEV_HUB);
  3165. if (tt->multi)
  3166. slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
  3167. if (xhci->hci_version > 0x95) {
  3168. xhci_dbg(xhci, "xHCI version %x needs hub "
  3169. "TT think time and number of ports\n",
  3170. (unsigned int) xhci->hci_version);
  3171. slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(hdev->maxchild));
  3172. /* Set TT think time - convert from ns to FS bit times.
  3173. * 0 = 8 FS bit times, 1 = 16 FS bit times,
  3174. * 2 = 24 FS bit times, 3 = 32 FS bit times.
  3175. *
  3176. * xHCI 1.0: this field shall be 0 if the device is not a
  3177. * High-spped hub.
  3178. */
  3179. think_time = tt->think_time;
  3180. if (think_time != 0)
  3181. think_time = (think_time / 666) - 1;
  3182. if (xhci->hci_version < 0x100 || hdev->speed == USB_SPEED_HIGH)
  3183. slot_ctx->tt_info |=
  3184. cpu_to_le32(TT_THINK_TIME(think_time));
  3185. } else {
  3186. xhci_dbg(xhci, "xHCI version %x doesn't need hub "
  3187. "TT think time or number of ports\n",
  3188. (unsigned int) xhci->hci_version);
  3189. }
  3190. slot_ctx->dev_state = 0;
  3191. spin_unlock_irqrestore(&xhci->lock, flags);
  3192. xhci_dbg(xhci, "Set up %s for hub device.\n",
  3193. (xhci->hci_version > 0x95) ?
  3194. "configure endpoint" : "evaluate context");
  3195. xhci_dbg(xhci, "Slot %u Input Context:\n", hdev->slot_id);
  3196. xhci_dbg_ctx(xhci, config_cmd->in_ctx, 0);
  3197. /* Issue and wait for the configure endpoint or
  3198. * evaluate context command.
  3199. */
  3200. if (xhci->hci_version > 0x95)
  3201. ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
  3202. false, false);
  3203. else
  3204. ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
  3205. true, false);
  3206. xhci_dbg(xhci, "Slot %u Output Context:\n", hdev->slot_id);
  3207. xhci_dbg_ctx(xhci, vdev->out_ctx, 0);
  3208. xhci_free_command(xhci, config_cmd);
  3209. return ret;
  3210. }
  3211. int xhci_get_frame(struct usb_hcd *hcd)
  3212. {
  3213. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  3214. /* EHCI mods by the periodic size. Why? */
  3215. return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
  3216. }
  3217. MODULE_DESCRIPTION(DRIVER_DESC);
  3218. MODULE_AUTHOR(DRIVER_AUTHOR);
  3219. MODULE_LICENSE("GPL");
  3220. static int __init xhci_hcd_init(void)
  3221. {
  3222. #ifdef CONFIG_PCI
  3223. int retval = 0;
  3224. retval = xhci_register_pci();
  3225. if (retval < 0) {
  3226. printk(KERN_DEBUG "Problem registering PCI driver.");
  3227. return retval;
  3228. }
  3229. #endif
  3230. /*
  3231. * Check the compiler generated sizes of structures that must be laid
  3232. * out in specific ways for hardware access.
  3233. */
  3234. BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
  3235. BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
  3236. BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
  3237. /* xhci_device_control has eight fields, and also
  3238. * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
  3239. */
  3240. BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
  3241. BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
  3242. BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
  3243. BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 7*32/8);
  3244. BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
  3245. /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
  3246. BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
  3247. BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
  3248. return 0;
  3249. }
  3250. module_init(xhci_hcd_init);
  3251. static void __exit xhci_hcd_cleanup(void)
  3252. {
  3253. #ifdef CONFIG_PCI
  3254. xhci_unregister_pci();
  3255. #endif
  3256. }
  3257. module_exit(xhci_hcd_cleanup);