xhci.c 123 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106
  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. else
  99. xhci_warn(xhci, "Host not halted after %u microseconds.\n",
  100. XHCI_MAX_HALT_USEC);
  101. return ret;
  102. }
  103. /*
  104. * Set the run bit and wait for the host to be running.
  105. */
  106. static int xhci_start(struct xhci_hcd *xhci)
  107. {
  108. u32 temp;
  109. int ret;
  110. temp = xhci_readl(xhci, &xhci->op_regs->command);
  111. temp |= (CMD_RUN);
  112. xhci_dbg(xhci, "// Turn on HC, cmd = 0x%x.\n",
  113. temp);
  114. xhci_writel(xhci, temp, &xhci->op_regs->command);
  115. /*
  116. * Wait for the HCHalted Status bit to be 0 to indicate the host is
  117. * running.
  118. */
  119. ret = handshake(xhci, &xhci->op_regs->status,
  120. STS_HALT, 0, XHCI_MAX_HALT_USEC);
  121. if (ret == -ETIMEDOUT)
  122. xhci_err(xhci, "Host took too long to start, "
  123. "waited %u microseconds.\n",
  124. XHCI_MAX_HALT_USEC);
  125. if (!ret)
  126. xhci->xhc_state &= ~XHCI_STATE_HALTED;
  127. return ret;
  128. }
  129. /*
  130. * Reset a halted HC.
  131. *
  132. * This resets pipelines, timers, counters, state machines, etc.
  133. * Transactions will be terminated immediately, and operational registers
  134. * will be set to their defaults.
  135. */
  136. int xhci_reset(struct xhci_hcd *xhci)
  137. {
  138. u32 command;
  139. u32 state;
  140. int ret;
  141. state = xhci_readl(xhci, &xhci->op_regs->status);
  142. if ((state & STS_HALT) == 0) {
  143. xhci_warn(xhci, "Host controller not halted, aborting reset.\n");
  144. return 0;
  145. }
  146. xhci_dbg(xhci, "// Reset the HC\n");
  147. command = xhci_readl(xhci, &xhci->op_regs->command);
  148. command |= CMD_RESET;
  149. xhci_writel(xhci, command, &xhci->op_regs->command);
  150. ret = handshake(xhci, &xhci->op_regs->command,
  151. CMD_RESET, 0, 250 * 1000);
  152. if (ret)
  153. return ret;
  154. xhci_dbg(xhci, "Wait for controller to be ready for doorbell rings\n");
  155. /*
  156. * xHCI cannot write to any doorbells or operational registers other
  157. * than status until the "Controller Not Ready" flag is cleared.
  158. */
  159. return handshake(xhci, &xhci->op_regs->status, STS_CNR, 0, 250 * 1000);
  160. }
  161. #ifdef CONFIG_PCI
  162. static int xhci_free_msi(struct xhci_hcd *xhci)
  163. {
  164. int i;
  165. if (!xhci->msix_entries)
  166. return -EINVAL;
  167. for (i = 0; i < xhci->msix_count; i++)
  168. if (xhci->msix_entries[i].vector)
  169. free_irq(xhci->msix_entries[i].vector,
  170. xhci_to_hcd(xhci));
  171. return 0;
  172. }
  173. /*
  174. * Set up MSI
  175. */
  176. static int xhci_setup_msi(struct xhci_hcd *xhci)
  177. {
  178. int ret;
  179. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  180. ret = pci_enable_msi(pdev);
  181. if (ret) {
  182. xhci_dbg(xhci, "failed to allocate MSI entry\n");
  183. return ret;
  184. }
  185. ret = request_irq(pdev->irq, (irq_handler_t)xhci_msi_irq,
  186. 0, "xhci_hcd", xhci_to_hcd(xhci));
  187. if (ret) {
  188. xhci_dbg(xhci, "disable MSI interrupt\n");
  189. pci_disable_msi(pdev);
  190. }
  191. return ret;
  192. }
  193. /*
  194. * Free IRQs
  195. * free all IRQs request
  196. */
  197. static void xhci_free_irq(struct xhci_hcd *xhci)
  198. {
  199. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  200. int ret;
  201. /* return if using legacy interrupt */
  202. if (xhci_to_hcd(xhci)->irq > 0)
  203. return;
  204. ret = xhci_free_msi(xhci);
  205. if (!ret)
  206. return;
  207. if (pdev->irq > 0)
  208. free_irq(pdev->irq, xhci_to_hcd(xhci));
  209. return;
  210. }
  211. /*
  212. * Set up MSI-X
  213. */
  214. static int xhci_setup_msix(struct xhci_hcd *xhci)
  215. {
  216. int i, ret = 0;
  217. struct usb_hcd *hcd = xhci_to_hcd(xhci);
  218. struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
  219. /*
  220. * calculate number of msi-x vectors supported.
  221. * - HCS_MAX_INTRS: the max number of interrupts the host can handle,
  222. * with max number of interrupters based on the xhci HCSPARAMS1.
  223. * - num_online_cpus: maximum msi-x vectors per CPUs core.
  224. * Add additional 1 vector to ensure always available interrupt.
  225. */
  226. xhci->msix_count = min(num_online_cpus() + 1,
  227. HCS_MAX_INTRS(xhci->hcs_params1));
  228. xhci->msix_entries =
  229. kmalloc((sizeof(struct msix_entry))*xhci->msix_count,
  230. GFP_KERNEL);
  231. if (!xhci->msix_entries) {
  232. xhci_err(xhci, "Failed to allocate MSI-X entries\n");
  233. return -ENOMEM;
  234. }
  235. for (i = 0; i < xhci->msix_count; i++) {
  236. xhci->msix_entries[i].entry = i;
  237. xhci->msix_entries[i].vector = 0;
  238. }
  239. ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count);
  240. if (ret) {
  241. xhci_dbg(xhci, "Failed to enable MSI-X\n");
  242. goto free_entries;
  243. }
  244. for (i = 0; i < xhci->msix_count; i++) {
  245. ret = request_irq(xhci->msix_entries[i].vector,
  246. (irq_handler_t)xhci_msi_irq,
  247. 0, "xhci_hcd", xhci_to_hcd(xhci));
  248. if (ret)
  249. goto disable_msix;
  250. }
  251. hcd->msix_enabled = 1;
  252. return ret;
  253. disable_msix:
  254. xhci_dbg(xhci, "disable MSI-X interrupt\n");
  255. xhci_free_irq(xhci);
  256. pci_disable_msix(pdev);
  257. free_entries:
  258. kfree(xhci->msix_entries);
  259. xhci->msix_entries = NULL;
  260. return ret;
  261. }
  262. /* Free any IRQs and disable MSI-X */
  263. static void xhci_cleanup_msix(struct xhci_hcd *xhci)
  264. {
  265. struct usb_hcd *hcd = xhci_to_hcd(xhci);
  266. struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
  267. xhci_free_irq(xhci);
  268. if (xhci->msix_entries) {
  269. pci_disable_msix(pdev);
  270. kfree(xhci->msix_entries);
  271. xhci->msix_entries = NULL;
  272. } else {
  273. pci_disable_msi(pdev);
  274. }
  275. hcd->msix_enabled = 0;
  276. return;
  277. }
  278. static void xhci_msix_sync_irqs(struct xhci_hcd *xhci)
  279. {
  280. int i;
  281. if (xhci->msix_entries) {
  282. for (i = 0; i < xhci->msix_count; i++)
  283. synchronize_irq(xhci->msix_entries[i].vector);
  284. }
  285. }
  286. static int xhci_try_enable_msi(struct usb_hcd *hcd)
  287. {
  288. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  289. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  290. int ret;
  291. /*
  292. * Some Fresco Logic host controllers advertise MSI, but fail to
  293. * generate interrupts. Don't even try to enable MSI.
  294. */
  295. if (xhci->quirks & XHCI_BROKEN_MSI)
  296. return 0;
  297. /* unregister the legacy interrupt */
  298. if (hcd->irq)
  299. free_irq(hcd->irq, hcd);
  300. hcd->irq = 0;
  301. ret = xhci_setup_msix(xhci);
  302. if (ret)
  303. /* fall back to msi*/
  304. ret = xhci_setup_msi(xhci);
  305. if (!ret)
  306. /* hcd->irq is 0, we have MSI */
  307. return 0;
  308. if (!pdev->irq) {
  309. xhci_err(xhci, "No msi-x/msi found and no IRQ in BIOS\n");
  310. return -EINVAL;
  311. }
  312. /* fall back to legacy interrupt*/
  313. ret = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED,
  314. hcd->irq_descr, hcd);
  315. if (ret) {
  316. xhci_err(xhci, "request interrupt %d failed\n",
  317. pdev->irq);
  318. return ret;
  319. }
  320. hcd->irq = pdev->irq;
  321. return 0;
  322. }
  323. #else
  324. static int xhci_try_enable_msi(struct usb_hcd *hcd)
  325. {
  326. return 0;
  327. }
  328. static void xhci_cleanup_msix(struct xhci_hcd *xhci)
  329. {
  330. }
  331. static void xhci_msix_sync_irqs(struct xhci_hcd *xhci)
  332. {
  333. }
  334. #endif
  335. /*
  336. * Initialize memory for HCD and xHC (one-time init).
  337. *
  338. * Program the PAGESIZE register, initialize the device context array, create
  339. * device contexts (?), set up a command ring segment (or two?), create event
  340. * ring (one for now).
  341. */
  342. int xhci_init(struct usb_hcd *hcd)
  343. {
  344. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  345. int retval = 0;
  346. xhci_dbg(xhci, "xhci_init\n");
  347. spin_lock_init(&xhci->lock);
  348. if (xhci->hci_version == 0x95 && link_quirk) {
  349. xhci_dbg(xhci, "QUIRK: Not clearing Link TRB chain bits.\n");
  350. xhci->quirks |= XHCI_LINK_TRB_QUIRK;
  351. } else {
  352. xhci_dbg(xhci, "xHCI doesn't need link TRB QUIRK\n");
  353. }
  354. retval = xhci_mem_init(xhci, GFP_KERNEL);
  355. xhci_dbg(xhci, "Finished xhci_init\n");
  356. return retval;
  357. }
  358. /*-------------------------------------------------------------------------*/
  359. #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
  360. static void xhci_event_ring_work(unsigned long arg)
  361. {
  362. unsigned long flags;
  363. int temp;
  364. u64 temp_64;
  365. struct xhci_hcd *xhci = (struct xhci_hcd *) arg;
  366. int i, j;
  367. xhci_dbg(xhci, "Poll event ring: %lu\n", jiffies);
  368. spin_lock_irqsave(&xhci->lock, flags);
  369. temp = xhci_readl(xhci, &xhci->op_regs->status);
  370. xhci_dbg(xhci, "op reg status = 0x%x\n", temp);
  371. if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) ||
  372. (xhci->xhc_state & XHCI_STATE_HALTED)) {
  373. xhci_dbg(xhci, "HW died, polling stopped.\n");
  374. spin_unlock_irqrestore(&xhci->lock, flags);
  375. return;
  376. }
  377. temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  378. xhci_dbg(xhci, "ir_set 0 pending = 0x%x\n", temp);
  379. xhci_dbg(xhci, "HC error bitmask = 0x%x\n", xhci->error_bitmask);
  380. xhci->error_bitmask = 0;
  381. xhci_dbg(xhci, "Event ring:\n");
  382. xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
  383. xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
  384. temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
  385. temp_64 &= ~ERST_PTR_MASK;
  386. xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
  387. xhci_dbg(xhci, "Command ring:\n");
  388. xhci_debug_segment(xhci, xhci->cmd_ring->deq_seg);
  389. xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
  390. xhci_dbg_cmd_ptrs(xhci);
  391. for (i = 0; i < MAX_HC_SLOTS; ++i) {
  392. if (!xhci->devs[i])
  393. continue;
  394. for (j = 0; j < 31; ++j) {
  395. xhci_dbg_ep_rings(xhci, i, j, &xhci->devs[i]->eps[j]);
  396. }
  397. }
  398. spin_unlock_irqrestore(&xhci->lock, flags);
  399. if (!xhci->zombie)
  400. mod_timer(&xhci->event_ring_timer, jiffies + POLL_TIMEOUT * HZ);
  401. else
  402. xhci_dbg(xhci, "Quit polling the event ring.\n");
  403. }
  404. #endif
  405. static int xhci_run_finished(struct xhci_hcd *xhci)
  406. {
  407. if (xhci_start(xhci)) {
  408. xhci_halt(xhci);
  409. return -ENODEV;
  410. }
  411. xhci->shared_hcd->state = HC_STATE_RUNNING;
  412. if (xhci->quirks & XHCI_NEC_HOST)
  413. xhci_ring_cmd_db(xhci);
  414. xhci_dbg(xhci, "Finished xhci_run for USB3 roothub\n");
  415. return 0;
  416. }
  417. /*
  418. * Start the HC after it was halted.
  419. *
  420. * This function is called by the USB core when the HC driver is added.
  421. * Its opposite is xhci_stop().
  422. *
  423. * xhci_init() must be called once before this function can be called.
  424. * Reset the HC, enable device slot contexts, program DCBAAP, and
  425. * set command ring pointer and event ring pointer.
  426. *
  427. * Setup MSI-X vectors and enable interrupts.
  428. */
  429. int xhci_run(struct usb_hcd *hcd)
  430. {
  431. u32 temp;
  432. u64 temp_64;
  433. int ret;
  434. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  435. /* Start the xHCI host controller running only after the USB 2.0 roothub
  436. * is setup.
  437. */
  438. hcd->uses_new_polling = 1;
  439. if (!usb_hcd_is_primary_hcd(hcd))
  440. return xhci_run_finished(xhci);
  441. xhci_dbg(xhci, "xhci_run\n");
  442. ret = xhci_try_enable_msi(hcd);
  443. if (ret)
  444. return ret;
  445. #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
  446. init_timer(&xhci->event_ring_timer);
  447. xhci->event_ring_timer.data = (unsigned long) xhci;
  448. xhci->event_ring_timer.function = xhci_event_ring_work;
  449. /* Poll the event ring */
  450. xhci->event_ring_timer.expires = jiffies + POLL_TIMEOUT * HZ;
  451. xhci->zombie = 0;
  452. xhci_dbg(xhci, "Setting event ring polling timer\n");
  453. add_timer(&xhci->event_ring_timer);
  454. #endif
  455. xhci_dbg(xhci, "Command ring memory map follows:\n");
  456. xhci_debug_ring(xhci, xhci->cmd_ring);
  457. xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
  458. xhci_dbg_cmd_ptrs(xhci);
  459. xhci_dbg(xhci, "ERST memory map follows:\n");
  460. xhci_dbg_erst(xhci, &xhci->erst);
  461. xhci_dbg(xhci, "Event ring:\n");
  462. xhci_debug_ring(xhci, xhci->event_ring);
  463. xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
  464. temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
  465. temp_64 &= ~ERST_PTR_MASK;
  466. xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
  467. xhci_dbg(xhci, "// Set the interrupt modulation register\n");
  468. temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
  469. temp &= ~ER_IRQ_INTERVAL_MASK;
  470. temp |= (u32) 160;
  471. xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
  472. /* Set the HCD state before we enable the irqs */
  473. temp = xhci_readl(xhci, &xhci->op_regs->command);
  474. temp |= (CMD_EIE);
  475. xhci_dbg(xhci, "// Enable interrupts, cmd = 0x%x.\n",
  476. temp);
  477. xhci_writel(xhci, temp, &xhci->op_regs->command);
  478. temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  479. xhci_dbg(xhci, "// Enabling event ring interrupter %p by writing 0x%x to irq_pending\n",
  480. xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
  481. xhci_writel(xhci, ER_IRQ_ENABLE(temp),
  482. &xhci->ir_set->irq_pending);
  483. xhci_print_ir_set(xhci, 0);
  484. if (xhci->quirks & XHCI_NEC_HOST)
  485. xhci_queue_vendor_command(xhci, 0, 0, 0,
  486. TRB_TYPE(TRB_NEC_GET_FW));
  487. xhci_dbg(xhci, "Finished xhci_run for USB2 roothub\n");
  488. return 0;
  489. }
  490. static void xhci_only_stop_hcd(struct usb_hcd *hcd)
  491. {
  492. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  493. spin_lock_irq(&xhci->lock);
  494. xhci_halt(xhci);
  495. /* The shared_hcd is going to be deallocated shortly (the USB core only
  496. * calls this function when allocation fails in usb_add_hcd(), or
  497. * usb_remove_hcd() is called). So we need to unset xHCI's pointer.
  498. */
  499. xhci->shared_hcd = NULL;
  500. spin_unlock_irq(&xhci->lock);
  501. }
  502. /*
  503. * Stop xHCI driver.
  504. *
  505. * This function is called by the USB core when the HC driver is removed.
  506. * Its opposite is xhci_run().
  507. *
  508. * Disable device contexts, disable IRQs, and quiesce the HC.
  509. * Reset the HC, finish any completed transactions, and cleanup memory.
  510. */
  511. void xhci_stop(struct usb_hcd *hcd)
  512. {
  513. u32 temp;
  514. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  515. if (!usb_hcd_is_primary_hcd(hcd)) {
  516. xhci_only_stop_hcd(xhci->shared_hcd);
  517. return;
  518. }
  519. spin_lock_irq(&xhci->lock);
  520. /* Make sure the xHC is halted for a USB3 roothub
  521. * (xhci_stop() could be called as part of failed init).
  522. */
  523. xhci_halt(xhci);
  524. xhci_reset(xhci);
  525. spin_unlock_irq(&xhci->lock);
  526. xhci_cleanup_msix(xhci);
  527. #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
  528. /* Tell the event ring poll function not to reschedule */
  529. xhci->zombie = 1;
  530. del_timer_sync(&xhci->event_ring_timer);
  531. #endif
  532. if (xhci->quirks & XHCI_AMD_PLL_FIX)
  533. usb_amd_dev_put();
  534. xhci_dbg(xhci, "// Disabling event ring interrupts\n");
  535. temp = xhci_readl(xhci, &xhci->op_regs->status);
  536. xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
  537. temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  538. xhci_writel(xhci, ER_IRQ_DISABLE(temp),
  539. &xhci->ir_set->irq_pending);
  540. xhci_print_ir_set(xhci, 0);
  541. xhci_dbg(xhci, "cleaning up memory\n");
  542. xhci_mem_cleanup(xhci);
  543. xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
  544. xhci_readl(xhci, &xhci->op_regs->status));
  545. }
  546. /*
  547. * Shutdown HC (not bus-specific)
  548. *
  549. * This is called when the machine is rebooting or halting. We assume that the
  550. * machine will be powered off, and the HC's internal state will be reset.
  551. * Don't bother to free memory.
  552. *
  553. * This will only ever be called with the main usb_hcd (the USB3 roothub).
  554. */
  555. void xhci_shutdown(struct usb_hcd *hcd)
  556. {
  557. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  558. spin_lock_irq(&xhci->lock);
  559. xhci_halt(xhci);
  560. spin_unlock_irq(&xhci->lock);
  561. xhci_cleanup_msix(xhci);
  562. xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n",
  563. xhci_readl(xhci, &xhci->op_regs->status));
  564. }
  565. #ifdef CONFIG_PM
  566. static void xhci_save_registers(struct xhci_hcd *xhci)
  567. {
  568. xhci->s3.command = xhci_readl(xhci, &xhci->op_regs->command);
  569. xhci->s3.dev_nt = xhci_readl(xhci, &xhci->op_regs->dev_notification);
  570. xhci->s3.dcbaa_ptr = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
  571. xhci->s3.config_reg = xhci_readl(xhci, &xhci->op_regs->config_reg);
  572. xhci->s3.erst_size = xhci_readl(xhci, &xhci->ir_set->erst_size);
  573. xhci->s3.erst_base = xhci_read_64(xhci, &xhci->ir_set->erst_base);
  574. xhci->s3.erst_dequeue = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
  575. xhci->s3.irq_pending = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  576. xhci->s3.irq_control = xhci_readl(xhci, &xhci->ir_set->irq_control);
  577. }
  578. static void xhci_restore_registers(struct xhci_hcd *xhci)
  579. {
  580. xhci_writel(xhci, xhci->s3.command, &xhci->op_regs->command);
  581. xhci_writel(xhci, xhci->s3.dev_nt, &xhci->op_regs->dev_notification);
  582. xhci_write_64(xhci, xhci->s3.dcbaa_ptr, &xhci->op_regs->dcbaa_ptr);
  583. xhci_writel(xhci, xhci->s3.config_reg, &xhci->op_regs->config_reg);
  584. xhci_writel(xhci, xhci->s3.erst_size, &xhci->ir_set->erst_size);
  585. xhci_write_64(xhci, xhci->s3.erst_base, &xhci->ir_set->erst_base);
  586. xhci_write_64(xhci, xhci->s3.erst_dequeue, &xhci->ir_set->erst_dequeue);
  587. xhci_writel(xhci, xhci->s3.irq_pending, &xhci->ir_set->irq_pending);
  588. xhci_writel(xhci, xhci->s3.irq_control, &xhci->ir_set->irq_control);
  589. }
  590. static void xhci_set_cmd_ring_deq(struct xhci_hcd *xhci)
  591. {
  592. u64 val_64;
  593. /* step 2: initialize command ring buffer */
  594. val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
  595. val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
  596. (xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
  597. xhci->cmd_ring->dequeue) &
  598. (u64) ~CMD_RING_RSVD_BITS) |
  599. xhci->cmd_ring->cycle_state;
  600. xhci_dbg(xhci, "// Setting command ring address to 0x%llx\n",
  601. (long unsigned long) val_64);
  602. xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
  603. }
  604. /*
  605. * The whole command ring must be cleared to zero when we suspend the host.
  606. *
  607. * The host doesn't save the command ring pointer in the suspend well, so we
  608. * need to re-program it on resume. Unfortunately, the pointer must be 64-byte
  609. * aligned, because of the reserved bits in the command ring dequeue pointer
  610. * register. Therefore, we can't just set the dequeue pointer back in the
  611. * middle of the ring (TRBs are 16-byte aligned).
  612. */
  613. static void xhci_clear_command_ring(struct xhci_hcd *xhci)
  614. {
  615. struct xhci_ring *ring;
  616. struct xhci_segment *seg;
  617. ring = xhci->cmd_ring;
  618. seg = ring->deq_seg;
  619. do {
  620. memset(seg->trbs, 0,
  621. sizeof(union xhci_trb) * (TRBS_PER_SEGMENT - 1));
  622. seg->trbs[TRBS_PER_SEGMENT - 1].link.control &=
  623. cpu_to_le32(~TRB_CYCLE);
  624. seg = seg->next;
  625. } while (seg != ring->deq_seg);
  626. /* Reset the software enqueue and dequeue pointers */
  627. ring->deq_seg = ring->first_seg;
  628. ring->dequeue = ring->first_seg->trbs;
  629. ring->enq_seg = ring->deq_seg;
  630. ring->enqueue = ring->dequeue;
  631. ring->num_trbs_free = ring->num_segs * (TRBS_PER_SEGMENT - 1) - 1;
  632. /*
  633. * Ring is now zeroed, so the HW should look for change of ownership
  634. * when the cycle bit is set to 1.
  635. */
  636. ring->cycle_state = 1;
  637. /*
  638. * Reset the hardware dequeue pointer.
  639. * Yes, this will need to be re-written after resume, but we're paranoid
  640. * and want to make sure the hardware doesn't access bogus memory
  641. * because, say, the BIOS or an SMI started the host without changing
  642. * the command ring pointers.
  643. */
  644. xhci_set_cmd_ring_deq(xhci);
  645. }
  646. /*
  647. * Stop HC (not bus-specific)
  648. *
  649. * This is called when the machine transition into S3/S4 mode.
  650. *
  651. */
  652. int xhci_suspend(struct xhci_hcd *xhci)
  653. {
  654. int rc = 0;
  655. struct usb_hcd *hcd = xhci_to_hcd(xhci);
  656. u32 command;
  657. spin_lock_irq(&xhci->lock);
  658. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  659. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
  660. /* step 1: stop endpoint */
  661. /* skipped assuming that port suspend has done */
  662. /* step 2: clear Run/Stop bit */
  663. command = xhci_readl(xhci, &xhci->op_regs->command);
  664. command &= ~CMD_RUN;
  665. xhci_writel(xhci, command, &xhci->op_regs->command);
  666. if (handshake(xhci, &xhci->op_regs->status,
  667. STS_HALT, STS_HALT, 100*100)) {
  668. xhci_warn(xhci, "WARN: xHC CMD_RUN timeout\n");
  669. spin_unlock_irq(&xhci->lock);
  670. return -ETIMEDOUT;
  671. }
  672. xhci_clear_command_ring(xhci);
  673. /* step 3: save registers */
  674. xhci_save_registers(xhci);
  675. /* step 4: set CSS flag */
  676. command = xhci_readl(xhci, &xhci->op_regs->command);
  677. command |= CMD_CSS;
  678. xhci_writel(xhci, command, &xhci->op_regs->command);
  679. if (handshake(xhci, &xhci->op_regs->status, STS_SAVE, 0, 10*100)) {
  680. xhci_warn(xhci, "WARN: xHC CMD_CSS timeout\n");
  681. spin_unlock_irq(&xhci->lock);
  682. return -ETIMEDOUT;
  683. }
  684. spin_unlock_irq(&xhci->lock);
  685. /* step 5: remove core well power */
  686. /* synchronize irq when using MSI-X */
  687. xhci_msix_sync_irqs(xhci);
  688. return rc;
  689. }
  690. /*
  691. * start xHC (not bus-specific)
  692. *
  693. * This is called when the machine transition from S3/S4 mode.
  694. *
  695. */
  696. int xhci_resume(struct xhci_hcd *xhci, bool hibernated)
  697. {
  698. u32 command, temp = 0;
  699. struct usb_hcd *hcd = xhci_to_hcd(xhci);
  700. struct usb_hcd *secondary_hcd;
  701. int retval = 0;
  702. /* Wait a bit if either of the roothubs need to settle from the
  703. * transition into bus suspend.
  704. */
  705. if (time_before(jiffies, xhci->bus_state[0].next_statechange) ||
  706. time_before(jiffies,
  707. xhci->bus_state[1].next_statechange))
  708. msleep(100);
  709. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  710. set_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
  711. spin_lock_irq(&xhci->lock);
  712. if (xhci->quirks & XHCI_RESET_ON_RESUME)
  713. hibernated = true;
  714. if (!hibernated) {
  715. /* step 1: restore register */
  716. xhci_restore_registers(xhci);
  717. /* step 2: initialize command ring buffer */
  718. xhci_set_cmd_ring_deq(xhci);
  719. /* step 3: restore state and start state*/
  720. /* step 3: set CRS flag */
  721. command = xhci_readl(xhci, &xhci->op_regs->command);
  722. command |= CMD_CRS;
  723. xhci_writel(xhci, command, &xhci->op_regs->command);
  724. if (handshake(xhci, &xhci->op_regs->status,
  725. STS_RESTORE, 0, 10*100)) {
  726. xhci_dbg(xhci, "WARN: xHC CMD_CSS timeout\n");
  727. spin_unlock_irq(&xhci->lock);
  728. return -ETIMEDOUT;
  729. }
  730. temp = xhci_readl(xhci, &xhci->op_regs->status);
  731. }
  732. /* If restore operation fails, re-initialize the HC during resume */
  733. if ((temp & STS_SRE) || hibernated) {
  734. /* Let the USB core know _both_ roothubs lost power. */
  735. usb_root_hub_lost_power(xhci->main_hcd->self.root_hub);
  736. usb_root_hub_lost_power(xhci->shared_hcd->self.root_hub);
  737. xhci_dbg(xhci, "Stop HCD\n");
  738. xhci_halt(xhci);
  739. xhci_reset(xhci);
  740. spin_unlock_irq(&xhci->lock);
  741. xhci_cleanup_msix(xhci);
  742. #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
  743. /* Tell the event ring poll function not to reschedule */
  744. xhci->zombie = 1;
  745. del_timer_sync(&xhci->event_ring_timer);
  746. #endif
  747. xhci_dbg(xhci, "// Disabling event ring interrupts\n");
  748. temp = xhci_readl(xhci, &xhci->op_regs->status);
  749. xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
  750. temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  751. xhci_writel(xhci, ER_IRQ_DISABLE(temp),
  752. &xhci->ir_set->irq_pending);
  753. xhci_print_ir_set(xhci, 0);
  754. xhci_dbg(xhci, "cleaning up memory\n");
  755. xhci_mem_cleanup(xhci);
  756. xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
  757. xhci_readl(xhci, &xhci->op_regs->status));
  758. /* USB core calls the PCI reinit and start functions twice:
  759. * first with the primary HCD, and then with the secondary HCD.
  760. * If we don't do the same, the host will never be started.
  761. */
  762. if (!usb_hcd_is_primary_hcd(hcd))
  763. secondary_hcd = hcd;
  764. else
  765. secondary_hcd = xhci->shared_hcd;
  766. xhci_dbg(xhci, "Initialize the xhci_hcd\n");
  767. retval = xhci_init(hcd->primary_hcd);
  768. if (retval)
  769. return retval;
  770. xhci_dbg(xhci, "Start the primary HCD\n");
  771. retval = xhci_run(hcd->primary_hcd);
  772. if (!retval) {
  773. xhci_dbg(xhci, "Start the secondary HCD\n");
  774. retval = xhci_run(secondary_hcd);
  775. }
  776. hcd->state = HC_STATE_SUSPENDED;
  777. xhci->shared_hcd->state = HC_STATE_SUSPENDED;
  778. goto done;
  779. }
  780. /* step 4: set Run/Stop bit */
  781. command = xhci_readl(xhci, &xhci->op_regs->command);
  782. command |= CMD_RUN;
  783. xhci_writel(xhci, command, &xhci->op_regs->command);
  784. handshake(xhci, &xhci->op_regs->status, STS_HALT,
  785. 0, 250 * 1000);
  786. /* step 5: walk topology and initialize portsc,
  787. * portpmsc and portli
  788. */
  789. /* this is done in bus_resume */
  790. /* step 6: restart each of the previously
  791. * Running endpoints by ringing their doorbells
  792. */
  793. spin_unlock_irq(&xhci->lock);
  794. done:
  795. if (retval == 0) {
  796. usb_hcd_resume_root_hub(hcd);
  797. usb_hcd_resume_root_hub(xhci->shared_hcd);
  798. }
  799. return retval;
  800. }
  801. #endif /* CONFIG_PM */
  802. /*-------------------------------------------------------------------------*/
  803. /**
  804. * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
  805. * HCDs. Find the index for an endpoint given its descriptor. Use the return
  806. * value to right shift 1 for the bitmask.
  807. *
  808. * Index = (epnum * 2) + direction - 1,
  809. * where direction = 0 for OUT, 1 for IN.
  810. * For control endpoints, the IN index is used (OUT index is unused), so
  811. * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
  812. */
  813. unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
  814. {
  815. unsigned int index;
  816. if (usb_endpoint_xfer_control(desc))
  817. index = (unsigned int) (usb_endpoint_num(desc)*2);
  818. else
  819. index = (unsigned int) (usb_endpoint_num(desc)*2) +
  820. (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
  821. return index;
  822. }
  823. /* Find the flag for this endpoint (for use in the control context). Use the
  824. * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
  825. * bit 1, etc.
  826. */
  827. unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
  828. {
  829. return 1 << (xhci_get_endpoint_index(desc) + 1);
  830. }
  831. /* Find the flag for this endpoint (for use in the control context). Use the
  832. * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
  833. * bit 1, etc.
  834. */
  835. unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index)
  836. {
  837. return 1 << (ep_index + 1);
  838. }
  839. /* Compute the last valid endpoint context index. Basically, this is the
  840. * endpoint index plus one. For slot contexts with more than valid endpoint,
  841. * we find the most significant bit set in the added contexts flags.
  842. * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
  843. * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
  844. */
  845. unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
  846. {
  847. return fls(added_ctxs) - 1;
  848. }
  849. /* Returns 1 if the arguments are OK;
  850. * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
  851. */
  852. static int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
  853. struct usb_host_endpoint *ep, int check_ep, bool check_virt_dev,
  854. const char *func) {
  855. struct xhci_hcd *xhci;
  856. struct xhci_virt_device *virt_dev;
  857. if (!hcd || (check_ep && !ep) || !udev) {
  858. printk(KERN_DEBUG "xHCI %s called with invalid args\n",
  859. func);
  860. return -EINVAL;
  861. }
  862. if (!udev->parent) {
  863. printk(KERN_DEBUG "xHCI %s called for root hub\n",
  864. func);
  865. return 0;
  866. }
  867. xhci = hcd_to_xhci(hcd);
  868. if (xhci->xhc_state & XHCI_STATE_HALTED)
  869. return -ENODEV;
  870. if (check_virt_dev) {
  871. if (!udev->slot_id || !xhci->devs[udev->slot_id]) {
  872. printk(KERN_DEBUG "xHCI %s called with unaddressed "
  873. "device\n", func);
  874. return -EINVAL;
  875. }
  876. virt_dev = xhci->devs[udev->slot_id];
  877. if (virt_dev->udev != udev) {
  878. printk(KERN_DEBUG "xHCI %s called with udev and "
  879. "virt_dev does not match\n", func);
  880. return -EINVAL;
  881. }
  882. }
  883. return 1;
  884. }
  885. static int xhci_configure_endpoint(struct xhci_hcd *xhci,
  886. struct usb_device *udev, struct xhci_command *command,
  887. bool ctx_change, bool must_succeed);
  888. /*
  889. * Full speed devices may have a max packet size greater than 8 bytes, but the
  890. * USB core doesn't know that until it reads the first 8 bytes of the
  891. * descriptor. If the usb_device's max packet size changes after that point,
  892. * we need to issue an evaluate context command and wait on it.
  893. */
  894. static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
  895. unsigned int ep_index, struct urb *urb)
  896. {
  897. struct xhci_container_ctx *in_ctx;
  898. struct xhci_container_ctx *out_ctx;
  899. struct xhci_input_control_ctx *ctrl_ctx;
  900. struct xhci_ep_ctx *ep_ctx;
  901. int max_packet_size;
  902. int hw_max_packet_size;
  903. int ret = 0;
  904. out_ctx = xhci->devs[slot_id]->out_ctx;
  905. ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
  906. hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
  907. max_packet_size = usb_endpoint_maxp(&urb->dev->ep0.desc);
  908. if (hw_max_packet_size != max_packet_size) {
  909. xhci_dbg(xhci, "Max Packet Size for ep 0 changed.\n");
  910. xhci_dbg(xhci, "Max packet size in usb_device = %d\n",
  911. max_packet_size);
  912. xhci_dbg(xhci, "Max packet size in xHCI HW = %d\n",
  913. hw_max_packet_size);
  914. xhci_dbg(xhci, "Issuing evaluate context command.\n");
  915. /* Set up the modified control endpoint 0 */
  916. xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
  917. xhci->devs[slot_id]->out_ctx, ep_index);
  918. in_ctx = xhci->devs[slot_id]->in_ctx;
  919. ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
  920. ep_ctx->ep_info2 &= cpu_to_le32(~MAX_PACKET_MASK);
  921. ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size));
  922. /* Set up the input context flags for the command */
  923. /* FIXME: This won't work if a non-default control endpoint
  924. * changes max packet sizes.
  925. */
  926. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  927. ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
  928. ctrl_ctx->drop_flags = 0;
  929. xhci_dbg(xhci, "Slot %d input context\n", slot_id);
  930. xhci_dbg_ctx(xhci, in_ctx, ep_index);
  931. xhci_dbg(xhci, "Slot %d output context\n", slot_id);
  932. xhci_dbg_ctx(xhci, out_ctx, ep_index);
  933. ret = xhci_configure_endpoint(xhci, urb->dev, NULL,
  934. true, false);
  935. /* Clean up the input context for later use by bandwidth
  936. * functions.
  937. */
  938. ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
  939. }
  940. return ret;
  941. }
  942. /*
  943. * non-error returns are a promise to giveback() the urb later
  944. * we drop ownership so next owner (or urb unlink) can get it
  945. */
  946. int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
  947. {
  948. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  949. struct xhci_td *buffer;
  950. unsigned long flags;
  951. int ret = 0;
  952. unsigned int slot_id, ep_index;
  953. struct urb_priv *urb_priv;
  954. int size, i;
  955. if (!urb || xhci_check_args(hcd, urb->dev, urb->ep,
  956. true, true, __func__) <= 0)
  957. return -EINVAL;
  958. slot_id = urb->dev->slot_id;
  959. ep_index = xhci_get_endpoint_index(&urb->ep->desc);
  960. if (!HCD_HW_ACCESSIBLE(hcd)) {
  961. if (!in_interrupt())
  962. xhci_dbg(xhci, "urb submitted during PCI suspend\n");
  963. ret = -ESHUTDOWN;
  964. goto exit;
  965. }
  966. if (usb_endpoint_xfer_isoc(&urb->ep->desc))
  967. size = urb->number_of_packets;
  968. else
  969. size = 1;
  970. urb_priv = kzalloc(sizeof(struct urb_priv) +
  971. size * sizeof(struct xhci_td *), mem_flags);
  972. if (!urb_priv)
  973. return -ENOMEM;
  974. buffer = kzalloc(size * sizeof(struct xhci_td), mem_flags);
  975. if (!buffer) {
  976. kfree(urb_priv);
  977. return -ENOMEM;
  978. }
  979. for (i = 0; i < size; i++) {
  980. urb_priv->td[i] = buffer;
  981. buffer++;
  982. }
  983. urb_priv->length = size;
  984. urb_priv->td_cnt = 0;
  985. urb->hcpriv = urb_priv;
  986. if (usb_endpoint_xfer_control(&urb->ep->desc)) {
  987. /* Check to see if the max packet size for the default control
  988. * endpoint changed during FS device enumeration
  989. */
  990. if (urb->dev->speed == USB_SPEED_FULL) {
  991. ret = xhci_check_maxpacket(xhci, slot_id,
  992. ep_index, urb);
  993. if (ret < 0) {
  994. xhci_urb_free_priv(xhci, urb_priv);
  995. urb->hcpriv = NULL;
  996. return ret;
  997. }
  998. }
  999. /* We have a spinlock and interrupts disabled, so we must pass
  1000. * atomic context to this function, which may allocate memory.
  1001. */
  1002. spin_lock_irqsave(&xhci->lock, flags);
  1003. if (xhci->xhc_state & XHCI_STATE_DYING)
  1004. goto dying;
  1005. ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
  1006. slot_id, ep_index);
  1007. if (ret)
  1008. goto free_priv;
  1009. spin_unlock_irqrestore(&xhci->lock, flags);
  1010. } else if (usb_endpoint_xfer_bulk(&urb->ep->desc)) {
  1011. spin_lock_irqsave(&xhci->lock, flags);
  1012. if (xhci->xhc_state & XHCI_STATE_DYING)
  1013. goto dying;
  1014. if (xhci->devs[slot_id]->eps[ep_index].ep_state &
  1015. EP_GETTING_STREAMS) {
  1016. xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
  1017. "is transitioning to using streams.\n");
  1018. ret = -EINVAL;
  1019. } else if (xhci->devs[slot_id]->eps[ep_index].ep_state &
  1020. EP_GETTING_NO_STREAMS) {
  1021. xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
  1022. "is transitioning to "
  1023. "not having streams.\n");
  1024. ret = -EINVAL;
  1025. } else {
  1026. ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
  1027. slot_id, ep_index);
  1028. }
  1029. if (ret)
  1030. goto free_priv;
  1031. spin_unlock_irqrestore(&xhci->lock, flags);
  1032. } else if (usb_endpoint_xfer_int(&urb->ep->desc)) {
  1033. spin_lock_irqsave(&xhci->lock, flags);
  1034. if (xhci->xhc_state & XHCI_STATE_DYING)
  1035. goto dying;
  1036. ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
  1037. slot_id, ep_index);
  1038. if (ret)
  1039. goto free_priv;
  1040. spin_unlock_irqrestore(&xhci->lock, flags);
  1041. } else {
  1042. spin_lock_irqsave(&xhci->lock, flags);
  1043. if (xhci->xhc_state & XHCI_STATE_DYING)
  1044. goto dying;
  1045. ret = xhci_queue_isoc_tx_prepare(xhci, GFP_ATOMIC, urb,
  1046. slot_id, ep_index);
  1047. if (ret)
  1048. goto free_priv;
  1049. spin_unlock_irqrestore(&xhci->lock, flags);
  1050. }
  1051. exit:
  1052. return ret;
  1053. dying:
  1054. xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for "
  1055. "non-responsive xHCI host.\n",
  1056. urb->ep->desc.bEndpointAddress, urb);
  1057. ret = -ESHUTDOWN;
  1058. free_priv:
  1059. xhci_urb_free_priv(xhci, urb_priv);
  1060. urb->hcpriv = NULL;
  1061. spin_unlock_irqrestore(&xhci->lock, flags);
  1062. return ret;
  1063. }
  1064. /* Get the right ring for the given URB.
  1065. * If the endpoint supports streams, boundary check the URB's stream ID.
  1066. * If the endpoint doesn't support streams, return the singular endpoint ring.
  1067. */
  1068. static struct xhci_ring *xhci_urb_to_transfer_ring(struct xhci_hcd *xhci,
  1069. struct urb *urb)
  1070. {
  1071. unsigned int slot_id;
  1072. unsigned int ep_index;
  1073. unsigned int stream_id;
  1074. struct xhci_virt_ep *ep;
  1075. slot_id = urb->dev->slot_id;
  1076. ep_index = xhci_get_endpoint_index(&urb->ep->desc);
  1077. stream_id = urb->stream_id;
  1078. ep = &xhci->devs[slot_id]->eps[ep_index];
  1079. /* Common case: no streams */
  1080. if (!(ep->ep_state & EP_HAS_STREAMS))
  1081. return ep->ring;
  1082. if (stream_id == 0) {
  1083. xhci_warn(xhci,
  1084. "WARN: Slot ID %u, ep index %u has streams, "
  1085. "but URB has no stream ID.\n",
  1086. slot_id, ep_index);
  1087. return NULL;
  1088. }
  1089. if (stream_id < ep->stream_info->num_streams)
  1090. return ep->stream_info->stream_rings[stream_id];
  1091. xhci_warn(xhci,
  1092. "WARN: Slot ID %u, ep index %u has "
  1093. "stream IDs 1 to %u allocated, "
  1094. "but stream ID %u is requested.\n",
  1095. slot_id, ep_index,
  1096. ep->stream_info->num_streams - 1,
  1097. stream_id);
  1098. return NULL;
  1099. }
  1100. /*
  1101. * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
  1102. * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
  1103. * should pick up where it left off in the TD, unless a Set Transfer Ring
  1104. * Dequeue Pointer is issued.
  1105. *
  1106. * The TRBs that make up the buffers for the canceled URB will be "removed" from
  1107. * the ring. Since the ring is a contiguous structure, they can't be physically
  1108. * removed. Instead, there are two options:
  1109. *
  1110. * 1) If the HC is in the middle of processing the URB to be canceled, we
  1111. * simply move the ring's dequeue pointer past those TRBs using the Set
  1112. * Transfer Ring Dequeue Pointer command. This will be the common case,
  1113. * when drivers timeout on the last submitted URB and attempt to cancel.
  1114. *
  1115. * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
  1116. * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
  1117. * HC will need to invalidate the any TRBs it has cached after the stop
  1118. * endpoint command, as noted in the xHCI 0.95 errata.
  1119. *
  1120. * 3) The TD may have completed by the time the Stop Endpoint Command
  1121. * completes, so software needs to handle that case too.
  1122. *
  1123. * This function should protect against the TD enqueueing code ringing the
  1124. * doorbell while this code is waiting for a Stop Endpoint command to complete.
  1125. * It also needs to account for multiple cancellations on happening at the same
  1126. * time for the same endpoint.
  1127. *
  1128. * Note that this function can be called in any context, or so says
  1129. * usb_hcd_unlink_urb()
  1130. */
  1131. int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  1132. {
  1133. unsigned long flags;
  1134. int ret, i;
  1135. u32 temp;
  1136. struct xhci_hcd *xhci;
  1137. struct urb_priv *urb_priv;
  1138. struct xhci_td *td;
  1139. unsigned int ep_index;
  1140. struct xhci_ring *ep_ring;
  1141. struct xhci_virt_ep *ep;
  1142. xhci = hcd_to_xhci(hcd);
  1143. spin_lock_irqsave(&xhci->lock, flags);
  1144. /* Make sure the URB hasn't completed or been unlinked already */
  1145. ret = usb_hcd_check_unlink_urb(hcd, urb, status);
  1146. if (ret || !urb->hcpriv)
  1147. goto done;
  1148. temp = xhci_readl(xhci, &xhci->op_regs->status);
  1149. if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_HALTED)) {
  1150. xhci_dbg(xhci, "HW died, freeing TD.\n");
  1151. urb_priv = urb->hcpriv;
  1152. for (i = urb_priv->td_cnt; i < urb_priv->length; i++) {
  1153. td = urb_priv->td[i];
  1154. if (!list_empty(&td->td_list))
  1155. list_del_init(&td->td_list);
  1156. if (!list_empty(&td->cancelled_td_list))
  1157. list_del_init(&td->cancelled_td_list);
  1158. }
  1159. usb_hcd_unlink_urb_from_ep(hcd, urb);
  1160. spin_unlock_irqrestore(&xhci->lock, flags);
  1161. usb_hcd_giveback_urb(hcd, urb, -ESHUTDOWN);
  1162. xhci_urb_free_priv(xhci, urb_priv);
  1163. return ret;
  1164. }
  1165. if ((xhci->xhc_state & XHCI_STATE_DYING) ||
  1166. (xhci->xhc_state & XHCI_STATE_HALTED)) {
  1167. xhci_dbg(xhci, "Ep 0x%x: URB %p to be canceled on "
  1168. "non-responsive xHCI host.\n",
  1169. urb->ep->desc.bEndpointAddress, urb);
  1170. /* Let the stop endpoint command watchdog timer (which set this
  1171. * state) finish cleaning up the endpoint TD lists. We must
  1172. * have caught it in the middle of dropping a lock and giving
  1173. * back an URB.
  1174. */
  1175. goto done;
  1176. }
  1177. ep_index = xhci_get_endpoint_index(&urb->ep->desc);
  1178. ep = &xhci->devs[urb->dev->slot_id]->eps[ep_index];
  1179. ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
  1180. if (!ep_ring) {
  1181. ret = -EINVAL;
  1182. goto done;
  1183. }
  1184. urb_priv = urb->hcpriv;
  1185. i = urb_priv->td_cnt;
  1186. if (i < urb_priv->length)
  1187. xhci_dbg(xhci, "Cancel URB %p, dev %s, ep 0x%x, "
  1188. "starting at offset 0x%llx\n",
  1189. urb, urb->dev->devpath,
  1190. urb->ep->desc.bEndpointAddress,
  1191. (unsigned long long) xhci_trb_virt_to_dma(
  1192. urb_priv->td[i]->start_seg,
  1193. urb_priv->td[i]->first_trb));
  1194. for (; i < urb_priv->length; i++) {
  1195. td = urb_priv->td[i];
  1196. list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
  1197. }
  1198. /* Queue a stop endpoint command, but only if this is
  1199. * the first cancellation to be handled.
  1200. */
  1201. if (!(ep->ep_state & EP_HALT_PENDING)) {
  1202. ep->ep_state |= EP_HALT_PENDING;
  1203. ep->stop_cmds_pending++;
  1204. ep->stop_cmd_timer.expires = jiffies +
  1205. XHCI_STOP_EP_CMD_TIMEOUT * HZ;
  1206. add_timer(&ep->stop_cmd_timer);
  1207. xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index, 0);
  1208. xhci_ring_cmd_db(xhci);
  1209. }
  1210. done:
  1211. spin_unlock_irqrestore(&xhci->lock, flags);
  1212. return ret;
  1213. }
  1214. /* Drop an endpoint from a new bandwidth configuration for this device.
  1215. * Only one call to this function is allowed per endpoint before
  1216. * check_bandwidth() or reset_bandwidth() must be called.
  1217. * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
  1218. * add the endpoint to the schedule with possibly new parameters denoted by a
  1219. * different endpoint descriptor in usb_host_endpoint.
  1220. * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
  1221. * not allowed.
  1222. *
  1223. * The USB core will not allow URBs to be queued to an endpoint that is being
  1224. * disabled, so there's no need for mutual exclusion to protect
  1225. * the xhci->devs[slot_id] structure.
  1226. */
  1227. int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
  1228. struct usb_host_endpoint *ep)
  1229. {
  1230. struct xhci_hcd *xhci;
  1231. struct xhci_container_ctx *in_ctx, *out_ctx;
  1232. struct xhci_input_control_ctx *ctrl_ctx;
  1233. struct xhci_slot_ctx *slot_ctx;
  1234. unsigned int last_ctx;
  1235. unsigned int ep_index;
  1236. struct xhci_ep_ctx *ep_ctx;
  1237. u32 drop_flag;
  1238. u32 new_add_flags, new_drop_flags, new_slot_info;
  1239. int ret;
  1240. ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
  1241. if (ret <= 0)
  1242. return ret;
  1243. xhci = hcd_to_xhci(hcd);
  1244. if (xhci->xhc_state & XHCI_STATE_DYING)
  1245. return -ENODEV;
  1246. xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
  1247. drop_flag = xhci_get_endpoint_flag(&ep->desc);
  1248. if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
  1249. xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
  1250. __func__, drop_flag);
  1251. return 0;
  1252. }
  1253. in_ctx = xhci->devs[udev->slot_id]->in_ctx;
  1254. out_ctx = xhci->devs[udev->slot_id]->out_ctx;
  1255. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  1256. ep_index = xhci_get_endpoint_index(&ep->desc);
  1257. ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
  1258. /* If the HC already knows the endpoint is disabled,
  1259. * or the HCD has noted it is disabled, ignore this request
  1260. */
  1261. if (((ep_ctx->ep_info & cpu_to_le32(EP_STATE_MASK)) ==
  1262. cpu_to_le32(EP_STATE_DISABLED)) ||
  1263. le32_to_cpu(ctrl_ctx->drop_flags) &
  1264. xhci_get_endpoint_flag(&ep->desc)) {
  1265. xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
  1266. __func__, ep);
  1267. return 0;
  1268. }
  1269. ctrl_ctx->drop_flags |= cpu_to_le32(drop_flag);
  1270. new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
  1271. ctrl_ctx->add_flags &= cpu_to_le32(~drop_flag);
  1272. new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
  1273. last_ctx = xhci_last_valid_endpoint(le32_to_cpu(ctrl_ctx->add_flags));
  1274. slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
  1275. /* Update the last valid endpoint context, if we deleted the last one */
  1276. if ((le32_to_cpu(slot_ctx->dev_info) & LAST_CTX_MASK) >
  1277. LAST_CTX(last_ctx)) {
  1278. slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
  1279. slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(last_ctx));
  1280. }
  1281. new_slot_info = le32_to_cpu(slot_ctx->dev_info);
  1282. xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
  1283. xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
  1284. (unsigned int) ep->desc.bEndpointAddress,
  1285. udev->slot_id,
  1286. (unsigned int) new_drop_flags,
  1287. (unsigned int) new_add_flags,
  1288. (unsigned int) new_slot_info);
  1289. return 0;
  1290. }
  1291. /* Add an endpoint to a new possible bandwidth configuration for this device.
  1292. * Only one call to this function is allowed per endpoint before
  1293. * check_bandwidth() or reset_bandwidth() must be called.
  1294. * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
  1295. * add the endpoint to the schedule with possibly new parameters denoted by a
  1296. * different endpoint descriptor in usb_host_endpoint.
  1297. * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
  1298. * not allowed.
  1299. *
  1300. * The USB core will not allow URBs to be queued to an endpoint until the
  1301. * configuration or alt setting is installed in the device, so there's no need
  1302. * for mutual exclusion to protect the xhci->devs[slot_id] structure.
  1303. */
  1304. int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
  1305. struct usb_host_endpoint *ep)
  1306. {
  1307. struct xhci_hcd *xhci;
  1308. struct xhci_container_ctx *in_ctx, *out_ctx;
  1309. unsigned int ep_index;
  1310. struct xhci_ep_ctx *ep_ctx;
  1311. struct xhci_slot_ctx *slot_ctx;
  1312. struct xhci_input_control_ctx *ctrl_ctx;
  1313. u32 added_ctxs;
  1314. unsigned int last_ctx;
  1315. u32 new_add_flags, new_drop_flags, new_slot_info;
  1316. struct xhci_virt_device *virt_dev;
  1317. int ret = 0;
  1318. ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
  1319. if (ret <= 0) {
  1320. /* So we won't queue a reset ep command for a root hub */
  1321. ep->hcpriv = NULL;
  1322. return ret;
  1323. }
  1324. xhci = hcd_to_xhci(hcd);
  1325. if (xhci->xhc_state & XHCI_STATE_DYING)
  1326. return -ENODEV;
  1327. added_ctxs = xhci_get_endpoint_flag(&ep->desc);
  1328. last_ctx = xhci_last_valid_endpoint(added_ctxs);
  1329. if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
  1330. /* FIXME when we have to issue an evaluate endpoint command to
  1331. * deal with ep0 max packet size changing once we get the
  1332. * descriptors
  1333. */
  1334. xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
  1335. __func__, added_ctxs);
  1336. return 0;
  1337. }
  1338. virt_dev = xhci->devs[udev->slot_id];
  1339. in_ctx = virt_dev->in_ctx;
  1340. out_ctx = virt_dev->out_ctx;
  1341. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  1342. ep_index = xhci_get_endpoint_index(&ep->desc);
  1343. ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
  1344. /* If this endpoint is already in use, and the upper layers are trying
  1345. * to add it again without dropping it, reject the addition.
  1346. */
  1347. if (virt_dev->eps[ep_index].ring &&
  1348. !(le32_to_cpu(ctrl_ctx->drop_flags) &
  1349. xhci_get_endpoint_flag(&ep->desc))) {
  1350. xhci_warn(xhci, "Trying to add endpoint 0x%x "
  1351. "without dropping it.\n",
  1352. (unsigned int) ep->desc.bEndpointAddress);
  1353. return -EINVAL;
  1354. }
  1355. /* If the HCD has already noted the endpoint is enabled,
  1356. * ignore this request.
  1357. */
  1358. if (le32_to_cpu(ctrl_ctx->add_flags) &
  1359. xhci_get_endpoint_flag(&ep->desc)) {
  1360. xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
  1361. __func__, ep);
  1362. return 0;
  1363. }
  1364. /*
  1365. * Configuration and alternate setting changes must be done in
  1366. * process context, not interrupt context (or so documenation
  1367. * for usb_set_interface() and usb_set_configuration() claim).
  1368. */
  1369. if (xhci_endpoint_init(xhci, virt_dev, udev, ep, GFP_NOIO) < 0) {
  1370. dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
  1371. __func__, ep->desc.bEndpointAddress);
  1372. return -ENOMEM;
  1373. }
  1374. ctrl_ctx->add_flags |= cpu_to_le32(added_ctxs);
  1375. new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
  1376. /* If xhci_endpoint_disable() was called for this endpoint, but the
  1377. * xHC hasn't been notified yet through the check_bandwidth() call,
  1378. * this re-adds a new state for the endpoint from the new endpoint
  1379. * descriptors. We must drop and re-add this endpoint, so we leave the
  1380. * drop flags alone.
  1381. */
  1382. new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
  1383. slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
  1384. /* Update the last valid endpoint context, if we just added one past */
  1385. if ((le32_to_cpu(slot_ctx->dev_info) & LAST_CTX_MASK) <
  1386. LAST_CTX(last_ctx)) {
  1387. slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
  1388. slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(last_ctx));
  1389. }
  1390. new_slot_info = le32_to_cpu(slot_ctx->dev_info);
  1391. /* Store the usb_device pointer for later use */
  1392. ep->hcpriv = udev;
  1393. xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
  1394. (unsigned int) ep->desc.bEndpointAddress,
  1395. udev->slot_id,
  1396. (unsigned int) new_drop_flags,
  1397. (unsigned int) new_add_flags,
  1398. (unsigned int) new_slot_info);
  1399. return 0;
  1400. }
  1401. static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev)
  1402. {
  1403. struct xhci_input_control_ctx *ctrl_ctx;
  1404. struct xhci_ep_ctx *ep_ctx;
  1405. struct xhci_slot_ctx *slot_ctx;
  1406. int i;
  1407. /* When a device's add flag and drop flag are zero, any subsequent
  1408. * configure endpoint command will leave that endpoint's state
  1409. * untouched. Make sure we don't leave any old state in the input
  1410. * endpoint contexts.
  1411. */
  1412. ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
  1413. ctrl_ctx->drop_flags = 0;
  1414. ctrl_ctx->add_flags = 0;
  1415. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
  1416. slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
  1417. /* Endpoint 0 is always valid */
  1418. slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
  1419. for (i = 1; i < 31; ++i) {
  1420. ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
  1421. ep_ctx->ep_info = 0;
  1422. ep_ctx->ep_info2 = 0;
  1423. ep_ctx->deq = 0;
  1424. ep_ctx->tx_info = 0;
  1425. }
  1426. }
  1427. static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
  1428. struct usb_device *udev, u32 *cmd_status)
  1429. {
  1430. int ret;
  1431. switch (*cmd_status) {
  1432. case COMP_ENOMEM:
  1433. dev_warn(&udev->dev, "Not enough host controller resources "
  1434. "for new device state.\n");
  1435. ret = -ENOMEM;
  1436. /* FIXME: can we allocate more resources for the HC? */
  1437. break;
  1438. case COMP_BW_ERR:
  1439. case COMP_2ND_BW_ERR:
  1440. dev_warn(&udev->dev, "Not enough bandwidth "
  1441. "for new device state.\n");
  1442. ret = -ENOSPC;
  1443. /* FIXME: can we go back to the old state? */
  1444. break;
  1445. case COMP_TRB_ERR:
  1446. /* the HCD set up something wrong */
  1447. dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
  1448. "add flag = 1, "
  1449. "and endpoint is not disabled.\n");
  1450. ret = -EINVAL;
  1451. break;
  1452. case COMP_DEV_ERR:
  1453. dev_warn(&udev->dev, "ERROR: Incompatible device for endpoint "
  1454. "configure command.\n");
  1455. ret = -ENODEV;
  1456. break;
  1457. case COMP_SUCCESS:
  1458. dev_dbg(&udev->dev, "Successful Endpoint Configure command\n");
  1459. ret = 0;
  1460. break;
  1461. default:
  1462. xhci_err(xhci, "ERROR: unexpected command completion "
  1463. "code 0x%x.\n", *cmd_status);
  1464. ret = -EINVAL;
  1465. break;
  1466. }
  1467. return ret;
  1468. }
  1469. static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
  1470. struct usb_device *udev, u32 *cmd_status)
  1471. {
  1472. int ret;
  1473. struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
  1474. switch (*cmd_status) {
  1475. case COMP_EINVAL:
  1476. dev_warn(&udev->dev, "WARN: xHCI driver setup invalid evaluate "
  1477. "context command.\n");
  1478. ret = -EINVAL;
  1479. break;
  1480. case COMP_EBADSLT:
  1481. dev_warn(&udev->dev, "WARN: slot not enabled for"
  1482. "evaluate context command.\n");
  1483. case COMP_CTX_STATE:
  1484. dev_warn(&udev->dev, "WARN: invalid context state for "
  1485. "evaluate context command.\n");
  1486. xhci_dbg_ctx(xhci, virt_dev->out_ctx, 1);
  1487. ret = -EINVAL;
  1488. break;
  1489. case COMP_DEV_ERR:
  1490. dev_warn(&udev->dev, "ERROR: Incompatible device for evaluate "
  1491. "context command.\n");
  1492. ret = -ENODEV;
  1493. break;
  1494. case COMP_MEL_ERR:
  1495. /* Max Exit Latency too large error */
  1496. dev_warn(&udev->dev, "WARN: Max Exit Latency too large\n");
  1497. ret = -EINVAL;
  1498. break;
  1499. case COMP_SUCCESS:
  1500. dev_dbg(&udev->dev, "Successful evaluate context command\n");
  1501. ret = 0;
  1502. break;
  1503. default:
  1504. xhci_err(xhci, "ERROR: unexpected command completion "
  1505. "code 0x%x.\n", *cmd_status);
  1506. ret = -EINVAL;
  1507. break;
  1508. }
  1509. return ret;
  1510. }
  1511. static u32 xhci_count_num_new_endpoints(struct xhci_hcd *xhci,
  1512. struct xhci_container_ctx *in_ctx)
  1513. {
  1514. struct xhci_input_control_ctx *ctrl_ctx;
  1515. u32 valid_add_flags;
  1516. u32 valid_drop_flags;
  1517. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  1518. /* Ignore the slot flag (bit 0), and the default control endpoint flag
  1519. * (bit 1). The default control endpoint is added during the Address
  1520. * Device command and is never removed until the slot is disabled.
  1521. */
  1522. valid_add_flags = ctrl_ctx->add_flags >> 2;
  1523. valid_drop_flags = ctrl_ctx->drop_flags >> 2;
  1524. /* Use hweight32 to count the number of ones in the add flags, or
  1525. * number of endpoints added. Don't count endpoints that are changed
  1526. * (both added and dropped).
  1527. */
  1528. return hweight32(valid_add_flags) -
  1529. hweight32(valid_add_flags & valid_drop_flags);
  1530. }
  1531. static unsigned int xhci_count_num_dropped_endpoints(struct xhci_hcd *xhci,
  1532. struct xhci_container_ctx *in_ctx)
  1533. {
  1534. struct xhci_input_control_ctx *ctrl_ctx;
  1535. u32 valid_add_flags;
  1536. u32 valid_drop_flags;
  1537. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  1538. valid_add_flags = ctrl_ctx->add_flags >> 2;
  1539. valid_drop_flags = ctrl_ctx->drop_flags >> 2;
  1540. return hweight32(valid_drop_flags) -
  1541. hweight32(valid_add_flags & valid_drop_flags);
  1542. }
  1543. /*
  1544. * We need to reserve the new number of endpoints before the configure endpoint
  1545. * command completes. We can't subtract the dropped endpoints from the number
  1546. * of active endpoints until the command completes because we can oversubscribe
  1547. * the host in this case:
  1548. *
  1549. * - the first configure endpoint command drops more endpoints than it adds
  1550. * - a second configure endpoint command that adds more endpoints is queued
  1551. * - the first configure endpoint command fails, so the config is unchanged
  1552. * - the second command may succeed, even though there isn't enough resources
  1553. *
  1554. * Must be called with xhci->lock held.
  1555. */
  1556. static int xhci_reserve_host_resources(struct xhci_hcd *xhci,
  1557. struct xhci_container_ctx *in_ctx)
  1558. {
  1559. u32 added_eps;
  1560. added_eps = xhci_count_num_new_endpoints(xhci, in_ctx);
  1561. if (xhci->num_active_eps + added_eps > xhci->limit_active_eps) {
  1562. xhci_dbg(xhci, "Not enough ep ctxs: "
  1563. "%u active, need to add %u, limit is %u.\n",
  1564. xhci->num_active_eps, added_eps,
  1565. xhci->limit_active_eps);
  1566. return -ENOMEM;
  1567. }
  1568. xhci->num_active_eps += added_eps;
  1569. xhci_dbg(xhci, "Adding %u ep ctxs, %u now active.\n", added_eps,
  1570. xhci->num_active_eps);
  1571. return 0;
  1572. }
  1573. /*
  1574. * The configure endpoint was failed by the xHC for some other reason, so we
  1575. * need to revert the resources that failed configuration would have used.
  1576. *
  1577. * Must be called with xhci->lock held.
  1578. */
  1579. static void xhci_free_host_resources(struct xhci_hcd *xhci,
  1580. struct xhci_container_ctx *in_ctx)
  1581. {
  1582. u32 num_failed_eps;
  1583. num_failed_eps = xhci_count_num_new_endpoints(xhci, in_ctx);
  1584. xhci->num_active_eps -= num_failed_eps;
  1585. xhci_dbg(xhci, "Removing %u failed ep ctxs, %u now active.\n",
  1586. num_failed_eps,
  1587. xhci->num_active_eps);
  1588. }
  1589. /*
  1590. * Now that the command has completed, clean up the active endpoint count by
  1591. * subtracting out the endpoints that were dropped (but not changed).
  1592. *
  1593. * Must be called with xhci->lock held.
  1594. */
  1595. static void xhci_finish_resource_reservation(struct xhci_hcd *xhci,
  1596. struct xhci_container_ctx *in_ctx)
  1597. {
  1598. u32 num_dropped_eps;
  1599. num_dropped_eps = xhci_count_num_dropped_endpoints(xhci, in_ctx);
  1600. xhci->num_active_eps -= num_dropped_eps;
  1601. if (num_dropped_eps)
  1602. xhci_dbg(xhci, "Removing %u dropped ep ctxs, %u now active.\n",
  1603. num_dropped_eps,
  1604. xhci->num_active_eps);
  1605. }
  1606. unsigned int xhci_get_block_size(struct usb_device *udev)
  1607. {
  1608. switch (udev->speed) {
  1609. case USB_SPEED_LOW:
  1610. case USB_SPEED_FULL:
  1611. return FS_BLOCK;
  1612. case USB_SPEED_HIGH:
  1613. return HS_BLOCK;
  1614. case USB_SPEED_SUPER:
  1615. return SS_BLOCK;
  1616. case USB_SPEED_UNKNOWN:
  1617. case USB_SPEED_WIRELESS:
  1618. default:
  1619. /* Should never happen */
  1620. return 1;
  1621. }
  1622. }
  1623. unsigned int xhci_get_largest_overhead(struct xhci_interval_bw *interval_bw)
  1624. {
  1625. if (interval_bw->overhead[LS_OVERHEAD_TYPE])
  1626. return LS_OVERHEAD;
  1627. if (interval_bw->overhead[FS_OVERHEAD_TYPE])
  1628. return FS_OVERHEAD;
  1629. return HS_OVERHEAD;
  1630. }
  1631. /* If we are changing a LS/FS device under a HS hub,
  1632. * make sure (if we are activating a new TT) that the HS bus has enough
  1633. * bandwidth for this new TT.
  1634. */
  1635. static int xhci_check_tt_bw_table(struct xhci_hcd *xhci,
  1636. struct xhci_virt_device *virt_dev,
  1637. int old_active_eps)
  1638. {
  1639. struct xhci_interval_bw_table *bw_table;
  1640. struct xhci_tt_bw_info *tt_info;
  1641. /* Find the bandwidth table for the root port this TT is attached to. */
  1642. bw_table = &xhci->rh_bw[virt_dev->real_port - 1].bw_table;
  1643. tt_info = virt_dev->tt_info;
  1644. /* If this TT already had active endpoints, the bandwidth for this TT
  1645. * has already been added. Removing all periodic endpoints (and thus
  1646. * making the TT enactive) will only decrease the bandwidth used.
  1647. */
  1648. if (old_active_eps)
  1649. return 0;
  1650. if (old_active_eps == 0 && tt_info->active_eps != 0) {
  1651. if (bw_table->bw_used + TT_HS_OVERHEAD > HS_BW_LIMIT)
  1652. return -ENOMEM;
  1653. return 0;
  1654. }
  1655. /* Not sure why we would have no new active endpoints...
  1656. *
  1657. * Maybe because of an Evaluate Context change for a hub update or a
  1658. * control endpoint 0 max packet size change?
  1659. * FIXME: skip the bandwidth calculation in that case.
  1660. */
  1661. return 0;
  1662. }
  1663. static int xhci_check_ss_bw(struct xhci_hcd *xhci,
  1664. struct xhci_virt_device *virt_dev)
  1665. {
  1666. unsigned int bw_reserved;
  1667. bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_IN, 100);
  1668. if (virt_dev->bw_table->ss_bw_in > (SS_BW_LIMIT_IN - bw_reserved))
  1669. return -ENOMEM;
  1670. bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_OUT, 100);
  1671. if (virt_dev->bw_table->ss_bw_out > (SS_BW_LIMIT_OUT - bw_reserved))
  1672. return -ENOMEM;
  1673. return 0;
  1674. }
  1675. /*
  1676. * This algorithm is a very conservative estimate of the worst-case scheduling
  1677. * scenario for any one interval. The hardware dynamically schedules the
  1678. * packets, so we can't tell which microframe could be the limiting factor in
  1679. * the bandwidth scheduling. This only takes into account periodic endpoints.
  1680. *
  1681. * Obviously, we can't solve an NP complete problem to find the minimum worst
  1682. * case scenario. Instead, we come up with an estimate that is no less than
  1683. * the worst case bandwidth used for any one microframe, but may be an
  1684. * over-estimate.
  1685. *
  1686. * We walk the requirements for each endpoint by interval, starting with the
  1687. * smallest interval, and place packets in the schedule where there is only one
  1688. * possible way to schedule packets for that interval. In order to simplify
  1689. * this algorithm, we record the largest max packet size for each interval, and
  1690. * assume all packets will be that size.
  1691. *
  1692. * For interval 0, we obviously must schedule all packets for each interval.
  1693. * The bandwidth for interval 0 is just the amount of data to be transmitted
  1694. * (the sum of all max ESIT payload sizes, plus any overhead per packet times
  1695. * the number of packets).
  1696. *
  1697. * For interval 1, we have two possible microframes to schedule those packets
  1698. * in. For this algorithm, if we can schedule the same number of packets for
  1699. * each possible scheduling opportunity (each microframe), we will do so. The
  1700. * remaining number of packets will be saved to be transmitted in the gaps in
  1701. * the next interval's scheduling sequence.
  1702. *
  1703. * As we move those remaining packets to be scheduled with interval 2 packets,
  1704. * we have to double the number of remaining packets to transmit. This is
  1705. * because the intervals are actually powers of 2, and we would be transmitting
  1706. * the previous interval's packets twice in this interval. We also have to be
  1707. * sure that when we look at the largest max packet size for this interval, we
  1708. * also look at the largest max packet size for the remaining packets and take
  1709. * the greater of the two.
  1710. *
  1711. * The algorithm continues to evenly distribute packets in each scheduling
  1712. * opportunity, and push the remaining packets out, until we get to the last
  1713. * interval. Then those packets and their associated overhead are just added
  1714. * to the bandwidth used.
  1715. */
  1716. static int xhci_check_bw_table(struct xhci_hcd *xhci,
  1717. struct xhci_virt_device *virt_dev,
  1718. int old_active_eps)
  1719. {
  1720. unsigned int bw_reserved;
  1721. unsigned int max_bandwidth;
  1722. unsigned int bw_used;
  1723. unsigned int block_size;
  1724. struct xhci_interval_bw_table *bw_table;
  1725. unsigned int packet_size = 0;
  1726. unsigned int overhead = 0;
  1727. unsigned int packets_transmitted = 0;
  1728. unsigned int packets_remaining = 0;
  1729. unsigned int i;
  1730. if (virt_dev->udev->speed == USB_SPEED_SUPER)
  1731. return xhci_check_ss_bw(xhci, virt_dev);
  1732. if (virt_dev->udev->speed == USB_SPEED_HIGH) {
  1733. max_bandwidth = HS_BW_LIMIT;
  1734. /* Convert percent of bus BW reserved to blocks reserved */
  1735. bw_reserved = DIV_ROUND_UP(HS_BW_RESERVED * max_bandwidth, 100);
  1736. } else {
  1737. max_bandwidth = FS_BW_LIMIT;
  1738. bw_reserved = DIV_ROUND_UP(FS_BW_RESERVED * max_bandwidth, 100);
  1739. }
  1740. bw_table = virt_dev->bw_table;
  1741. /* We need to translate the max packet size and max ESIT payloads into
  1742. * the units the hardware uses.
  1743. */
  1744. block_size = xhci_get_block_size(virt_dev->udev);
  1745. /* If we are manipulating a LS/FS device under a HS hub, double check
  1746. * that the HS bus has enough bandwidth if we are activing a new TT.
  1747. */
  1748. if (virt_dev->tt_info) {
  1749. xhci_dbg(xhci, "Recalculating BW for rootport %u\n",
  1750. virt_dev->real_port);
  1751. if (xhci_check_tt_bw_table(xhci, virt_dev, old_active_eps)) {
  1752. xhci_warn(xhci, "Not enough bandwidth on HS bus for "
  1753. "newly activated TT.\n");
  1754. return -ENOMEM;
  1755. }
  1756. xhci_dbg(xhci, "Recalculating BW for TT slot %u port %u\n",
  1757. virt_dev->tt_info->slot_id,
  1758. virt_dev->tt_info->ttport);
  1759. } else {
  1760. xhci_dbg(xhci, "Recalculating BW for rootport %u\n",
  1761. virt_dev->real_port);
  1762. }
  1763. /* Add in how much bandwidth will be used for interval zero, or the
  1764. * rounded max ESIT payload + number of packets * largest overhead.
  1765. */
  1766. bw_used = DIV_ROUND_UP(bw_table->interval0_esit_payload, block_size) +
  1767. bw_table->interval_bw[0].num_packets *
  1768. xhci_get_largest_overhead(&bw_table->interval_bw[0]);
  1769. for (i = 1; i < XHCI_MAX_INTERVAL; i++) {
  1770. unsigned int bw_added;
  1771. unsigned int largest_mps;
  1772. unsigned int interval_overhead;
  1773. /*
  1774. * How many packets could we transmit in this interval?
  1775. * If packets didn't fit in the previous interval, we will need
  1776. * to transmit that many packets twice within this interval.
  1777. */
  1778. packets_remaining = 2 * packets_remaining +
  1779. bw_table->interval_bw[i].num_packets;
  1780. /* Find the largest max packet size of this or the previous
  1781. * interval.
  1782. */
  1783. if (list_empty(&bw_table->interval_bw[i].endpoints))
  1784. largest_mps = 0;
  1785. else {
  1786. struct xhci_virt_ep *virt_ep;
  1787. struct list_head *ep_entry;
  1788. ep_entry = bw_table->interval_bw[i].endpoints.next;
  1789. virt_ep = list_entry(ep_entry,
  1790. struct xhci_virt_ep, bw_endpoint_list);
  1791. /* Convert to blocks, rounding up */
  1792. largest_mps = DIV_ROUND_UP(
  1793. virt_ep->bw_info.max_packet_size,
  1794. block_size);
  1795. }
  1796. if (largest_mps > packet_size)
  1797. packet_size = largest_mps;
  1798. /* Use the larger overhead of this or the previous interval. */
  1799. interval_overhead = xhci_get_largest_overhead(
  1800. &bw_table->interval_bw[i]);
  1801. if (interval_overhead > overhead)
  1802. overhead = interval_overhead;
  1803. /* How many packets can we evenly distribute across
  1804. * (1 << (i + 1)) possible scheduling opportunities?
  1805. */
  1806. packets_transmitted = packets_remaining >> (i + 1);
  1807. /* Add in the bandwidth used for those scheduled packets */
  1808. bw_added = packets_transmitted * (overhead + packet_size);
  1809. /* How many packets do we have remaining to transmit? */
  1810. packets_remaining = packets_remaining % (1 << (i + 1));
  1811. /* What largest max packet size should those packets have? */
  1812. /* If we've transmitted all packets, don't carry over the
  1813. * largest packet size.
  1814. */
  1815. if (packets_remaining == 0) {
  1816. packet_size = 0;
  1817. overhead = 0;
  1818. } else if (packets_transmitted > 0) {
  1819. /* Otherwise if we do have remaining packets, and we've
  1820. * scheduled some packets in this interval, take the
  1821. * largest max packet size from endpoints with this
  1822. * interval.
  1823. */
  1824. packet_size = largest_mps;
  1825. overhead = interval_overhead;
  1826. }
  1827. /* Otherwise carry over packet_size and overhead from the last
  1828. * time we had a remainder.
  1829. */
  1830. bw_used += bw_added;
  1831. if (bw_used > max_bandwidth) {
  1832. xhci_warn(xhci, "Not enough bandwidth. "
  1833. "Proposed: %u, Max: %u\n",
  1834. bw_used, max_bandwidth);
  1835. return -ENOMEM;
  1836. }
  1837. }
  1838. /*
  1839. * Ok, we know we have some packets left over after even-handedly
  1840. * scheduling interval 15. We don't know which microframes they will
  1841. * fit into, so we over-schedule and say they will be scheduled every
  1842. * microframe.
  1843. */
  1844. if (packets_remaining > 0)
  1845. bw_used += overhead + packet_size;
  1846. if (!virt_dev->tt_info && virt_dev->udev->speed == USB_SPEED_HIGH) {
  1847. unsigned int port_index = virt_dev->real_port - 1;
  1848. /* OK, we're manipulating a HS device attached to a
  1849. * root port bandwidth domain. Include the number of active TTs
  1850. * in the bandwidth used.
  1851. */
  1852. bw_used += TT_HS_OVERHEAD *
  1853. xhci->rh_bw[port_index].num_active_tts;
  1854. }
  1855. xhci_dbg(xhci, "Final bandwidth: %u, Limit: %u, Reserved: %u, "
  1856. "Available: %u " "percent\n",
  1857. bw_used, max_bandwidth, bw_reserved,
  1858. (max_bandwidth - bw_used - bw_reserved) * 100 /
  1859. max_bandwidth);
  1860. bw_used += bw_reserved;
  1861. if (bw_used > max_bandwidth) {
  1862. xhci_warn(xhci, "Not enough bandwidth. Proposed: %u, Max: %u\n",
  1863. bw_used, max_bandwidth);
  1864. return -ENOMEM;
  1865. }
  1866. bw_table->bw_used = bw_used;
  1867. return 0;
  1868. }
  1869. static bool xhci_is_async_ep(unsigned int ep_type)
  1870. {
  1871. return (ep_type != ISOC_OUT_EP && ep_type != INT_OUT_EP &&
  1872. ep_type != ISOC_IN_EP &&
  1873. ep_type != INT_IN_EP);
  1874. }
  1875. static bool xhci_is_sync_in_ep(unsigned int ep_type)
  1876. {
  1877. return (ep_type == ISOC_IN_EP || ep_type != INT_IN_EP);
  1878. }
  1879. static unsigned int xhci_get_ss_bw_consumed(struct xhci_bw_info *ep_bw)
  1880. {
  1881. unsigned int mps = DIV_ROUND_UP(ep_bw->max_packet_size, SS_BLOCK);
  1882. if (ep_bw->ep_interval == 0)
  1883. return SS_OVERHEAD_BURST +
  1884. (ep_bw->mult * ep_bw->num_packets *
  1885. (SS_OVERHEAD + mps));
  1886. return DIV_ROUND_UP(ep_bw->mult * ep_bw->num_packets *
  1887. (SS_OVERHEAD + mps + SS_OVERHEAD_BURST),
  1888. 1 << ep_bw->ep_interval);
  1889. }
  1890. void xhci_drop_ep_from_interval_table(struct xhci_hcd *xhci,
  1891. struct xhci_bw_info *ep_bw,
  1892. struct xhci_interval_bw_table *bw_table,
  1893. struct usb_device *udev,
  1894. struct xhci_virt_ep *virt_ep,
  1895. struct xhci_tt_bw_info *tt_info)
  1896. {
  1897. struct xhci_interval_bw *interval_bw;
  1898. int normalized_interval;
  1899. if (xhci_is_async_ep(ep_bw->type))
  1900. return;
  1901. if (udev->speed == USB_SPEED_SUPER) {
  1902. if (xhci_is_sync_in_ep(ep_bw->type))
  1903. xhci->devs[udev->slot_id]->bw_table->ss_bw_in -=
  1904. xhci_get_ss_bw_consumed(ep_bw);
  1905. else
  1906. xhci->devs[udev->slot_id]->bw_table->ss_bw_out -=
  1907. xhci_get_ss_bw_consumed(ep_bw);
  1908. return;
  1909. }
  1910. /* SuperSpeed endpoints never get added to intervals in the table, so
  1911. * this check is only valid for HS/FS/LS devices.
  1912. */
  1913. if (list_empty(&virt_ep->bw_endpoint_list))
  1914. return;
  1915. /* For LS/FS devices, we need to translate the interval expressed in
  1916. * microframes to frames.
  1917. */
  1918. if (udev->speed == USB_SPEED_HIGH)
  1919. normalized_interval = ep_bw->ep_interval;
  1920. else
  1921. normalized_interval = ep_bw->ep_interval - 3;
  1922. if (normalized_interval == 0)
  1923. bw_table->interval0_esit_payload -= ep_bw->max_esit_payload;
  1924. interval_bw = &bw_table->interval_bw[normalized_interval];
  1925. interval_bw->num_packets -= ep_bw->num_packets;
  1926. switch (udev->speed) {
  1927. case USB_SPEED_LOW:
  1928. interval_bw->overhead[LS_OVERHEAD_TYPE] -= 1;
  1929. break;
  1930. case USB_SPEED_FULL:
  1931. interval_bw->overhead[FS_OVERHEAD_TYPE] -= 1;
  1932. break;
  1933. case USB_SPEED_HIGH:
  1934. interval_bw->overhead[HS_OVERHEAD_TYPE] -= 1;
  1935. break;
  1936. case USB_SPEED_SUPER:
  1937. case USB_SPEED_UNKNOWN:
  1938. case USB_SPEED_WIRELESS:
  1939. /* Should never happen because only LS/FS/HS endpoints will get
  1940. * added to the endpoint list.
  1941. */
  1942. return;
  1943. }
  1944. if (tt_info)
  1945. tt_info->active_eps -= 1;
  1946. list_del_init(&virt_ep->bw_endpoint_list);
  1947. }
  1948. static void xhci_add_ep_to_interval_table(struct xhci_hcd *xhci,
  1949. struct xhci_bw_info *ep_bw,
  1950. struct xhci_interval_bw_table *bw_table,
  1951. struct usb_device *udev,
  1952. struct xhci_virt_ep *virt_ep,
  1953. struct xhci_tt_bw_info *tt_info)
  1954. {
  1955. struct xhci_interval_bw *interval_bw;
  1956. struct xhci_virt_ep *smaller_ep;
  1957. int normalized_interval;
  1958. if (xhci_is_async_ep(ep_bw->type))
  1959. return;
  1960. if (udev->speed == USB_SPEED_SUPER) {
  1961. if (xhci_is_sync_in_ep(ep_bw->type))
  1962. xhci->devs[udev->slot_id]->bw_table->ss_bw_in +=
  1963. xhci_get_ss_bw_consumed(ep_bw);
  1964. else
  1965. xhci->devs[udev->slot_id]->bw_table->ss_bw_out +=
  1966. xhci_get_ss_bw_consumed(ep_bw);
  1967. return;
  1968. }
  1969. /* For LS/FS devices, we need to translate the interval expressed in
  1970. * microframes to frames.
  1971. */
  1972. if (udev->speed == USB_SPEED_HIGH)
  1973. normalized_interval = ep_bw->ep_interval;
  1974. else
  1975. normalized_interval = ep_bw->ep_interval - 3;
  1976. if (normalized_interval == 0)
  1977. bw_table->interval0_esit_payload += ep_bw->max_esit_payload;
  1978. interval_bw = &bw_table->interval_bw[normalized_interval];
  1979. interval_bw->num_packets += ep_bw->num_packets;
  1980. switch (udev->speed) {
  1981. case USB_SPEED_LOW:
  1982. interval_bw->overhead[LS_OVERHEAD_TYPE] += 1;
  1983. break;
  1984. case USB_SPEED_FULL:
  1985. interval_bw->overhead[FS_OVERHEAD_TYPE] += 1;
  1986. break;
  1987. case USB_SPEED_HIGH:
  1988. interval_bw->overhead[HS_OVERHEAD_TYPE] += 1;
  1989. break;
  1990. case USB_SPEED_SUPER:
  1991. case USB_SPEED_UNKNOWN:
  1992. case USB_SPEED_WIRELESS:
  1993. /* Should never happen because only LS/FS/HS endpoints will get
  1994. * added to the endpoint list.
  1995. */
  1996. return;
  1997. }
  1998. if (tt_info)
  1999. tt_info->active_eps += 1;
  2000. /* Insert the endpoint into the list, largest max packet size first. */
  2001. list_for_each_entry(smaller_ep, &interval_bw->endpoints,
  2002. bw_endpoint_list) {
  2003. if (ep_bw->max_packet_size >=
  2004. smaller_ep->bw_info.max_packet_size) {
  2005. /* Add the new ep before the smaller endpoint */
  2006. list_add_tail(&virt_ep->bw_endpoint_list,
  2007. &smaller_ep->bw_endpoint_list);
  2008. return;
  2009. }
  2010. }
  2011. /* Add the new endpoint at the end of the list. */
  2012. list_add_tail(&virt_ep->bw_endpoint_list,
  2013. &interval_bw->endpoints);
  2014. }
  2015. void xhci_update_tt_active_eps(struct xhci_hcd *xhci,
  2016. struct xhci_virt_device *virt_dev,
  2017. int old_active_eps)
  2018. {
  2019. struct xhci_root_port_bw_info *rh_bw_info;
  2020. if (!virt_dev->tt_info)
  2021. return;
  2022. rh_bw_info = &xhci->rh_bw[virt_dev->real_port - 1];
  2023. if (old_active_eps == 0 &&
  2024. virt_dev->tt_info->active_eps != 0) {
  2025. rh_bw_info->num_active_tts += 1;
  2026. rh_bw_info->bw_table.bw_used += TT_HS_OVERHEAD;
  2027. } else if (old_active_eps != 0 &&
  2028. virt_dev->tt_info->active_eps == 0) {
  2029. rh_bw_info->num_active_tts -= 1;
  2030. rh_bw_info->bw_table.bw_used -= TT_HS_OVERHEAD;
  2031. }
  2032. }
  2033. static int xhci_reserve_bandwidth(struct xhci_hcd *xhci,
  2034. struct xhci_virt_device *virt_dev,
  2035. struct xhci_container_ctx *in_ctx)
  2036. {
  2037. struct xhci_bw_info ep_bw_info[31];
  2038. int i;
  2039. struct xhci_input_control_ctx *ctrl_ctx;
  2040. int old_active_eps = 0;
  2041. if (virt_dev->tt_info)
  2042. old_active_eps = virt_dev->tt_info->active_eps;
  2043. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  2044. for (i = 0; i < 31; i++) {
  2045. if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
  2046. continue;
  2047. /* Make a copy of the BW info in case we need to revert this */
  2048. memcpy(&ep_bw_info[i], &virt_dev->eps[i].bw_info,
  2049. sizeof(ep_bw_info[i]));
  2050. /* Drop the endpoint from the interval table if the endpoint is
  2051. * being dropped or changed.
  2052. */
  2053. if (EP_IS_DROPPED(ctrl_ctx, i))
  2054. xhci_drop_ep_from_interval_table(xhci,
  2055. &virt_dev->eps[i].bw_info,
  2056. virt_dev->bw_table,
  2057. virt_dev->udev,
  2058. &virt_dev->eps[i],
  2059. virt_dev->tt_info);
  2060. }
  2061. /* Overwrite the information stored in the endpoints' bw_info */
  2062. xhci_update_bw_info(xhci, virt_dev->in_ctx, ctrl_ctx, virt_dev);
  2063. for (i = 0; i < 31; i++) {
  2064. /* Add any changed or added endpoints to the interval table */
  2065. if (EP_IS_ADDED(ctrl_ctx, i))
  2066. xhci_add_ep_to_interval_table(xhci,
  2067. &virt_dev->eps[i].bw_info,
  2068. virt_dev->bw_table,
  2069. virt_dev->udev,
  2070. &virt_dev->eps[i],
  2071. virt_dev->tt_info);
  2072. }
  2073. if (!xhci_check_bw_table(xhci, virt_dev, old_active_eps)) {
  2074. /* Ok, this fits in the bandwidth we have.
  2075. * Update the number of active TTs.
  2076. */
  2077. xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
  2078. return 0;
  2079. }
  2080. /* We don't have enough bandwidth for this, revert the stored info. */
  2081. for (i = 0; i < 31; i++) {
  2082. if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
  2083. continue;
  2084. /* Drop the new copies of any added or changed endpoints from
  2085. * the interval table.
  2086. */
  2087. if (EP_IS_ADDED(ctrl_ctx, i)) {
  2088. xhci_drop_ep_from_interval_table(xhci,
  2089. &virt_dev->eps[i].bw_info,
  2090. virt_dev->bw_table,
  2091. virt_dev->udev,
  2092. &virt_dev->eps[i],
  2093. virt_dev->tt_info);
  2094. }
  2095. /* Revert the endpoint back to its old information */
  2096. memcpy(&virt_dev->eps[i].bw_info, &ep_bw_info[i],
  2097. sizeof(ep_bw_info[i]));
  2098. /* Add any changed or dropped endpoints back into the table */
  2099. if (EP_IS_DROPPED(ctrl_ctx, i))
  2100. xhci_add_ep_to_interval_table(xhci,
  2101. &virt_dev->eps[i].bw_info,
  2102. virt_dev->bw_table,
  2103. virt_dev->udev,
  2104. &virt_dev->eps[i],
  2105. virt_dev->tt_info);
  2106. }
  2107. return -ENOMEM;
  2108. }
  2109. /* Issue a configure endpoint command or evaluate context command
  2110. * and wait for it to finish.
  2111. */
  2112. static int xhci_configure_endpoint(struct xhci_hcd *xhci,
  2113. struct usb_device *udev,
  2114. struct xhci_command *command,
  2115. bool ctx_change, bool must_succeed)
  2116. {
  2117. int ret;
  2118. int timeleft;
  2119. unsigned long flags;
  2120. struct xhci_container_ctx *in_ctx;
  2121. struct completion *cmd_completion;
  2122. u32 *cmd_status;
  2123. struct xhci_virt_device *virt_dev;
  2124. spin_lock_irqsave(&xhci->lock, flags);
  2125. virt_dev = xhci->devs[udev->slot_id];
  2126. if (command)
  2127. in_ctx = command->in_ctx;
  2128. else
  2129. in_ctx = virt_dev->in_ctx;
  2130. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK) &&
  2131. xhci_reserve_host_resources(xhci, in_ctx)) {
  2132. spin_unlock_irqrestore(&xhci->lock, flags);
  2133. xhci_warn(xhci, "Not enough host resources, "
  2134. "active endpoint contexts = %u\n",
  2135. xhci->num_active_eps);
  2136. return -ENOMEM;
  2137. }
  2138. if ((xhci->quirks & XHCI_SW_BW_CHECKING) &&
  2139. xhci_reserve_bandwidth(xhci, virt_dev, in_ctx)) {
  2140. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
  2141. xhci_free_host_resources(xhci, in_ctx);
  2142. spin_unlock_irqrestore(&xhci->lock, flags);
  2143. xhci_warn(xhci, "Not enough bandwidth\n");
  2144. return -ENOMEM;
  2145. }
  2146. if (command) {
  2147. cmd_completion = command->completion;
  2148. cmd_status = &command->status;
  2149. command->command_trb = xhci->cmd_ring->enqueue;
  2150. /* Enqueue pointer can be left pointing to the link TRB,
  2151. * we must handle that
  2152. */
  2153. if (TRB_TYPE_LINK_LE32(command->command_trb->link.control))
  2154. command->command_trb =
  2155. xhci->cmd_ring->enq_seg->next->trbs;
  2156. list_add_tail(&command->cmd_list, &virt_dev->cmd_list);
  2157. } else {
  2158. cmd_completion = &virt_dev->cmd_completion;
  2159. cmd_status = &virt_dev->cmd_status;
  2160. }
  2161. init_completion(cmd_completion);
  2162. if (!ctx_change)
  2163. ret = xhci_queue_configure_endpoint(xhci, in_ctx->dma,
  2164. udev->slot_id, must_succeed);
  2165. else
  2166. ret = xhci_queue_evaluate_context(xhci, in_ctx->dma,
  2167. udev->slot_id);
  2168. if (ret < 0) {
  2169. if (command)
  2170. list_del(&command->cmd_list);
  2171. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
  2172. xhci_free_host_resources(xhci, in_ctx);
  2173. spin_unlock_irqrestore(&xhci->lock, flags);
  2174. xhci_dbg(xhci, "FIXME allocate a new ring segment\n");
  2175. return -ENOMEM;
  2176. }
  2177. xhci_ring_cmd_db(xhci);
  2178. spin_unlock_irqrestore(&xhci->lock, flags);
  2179. /* Wait for the configure endpoint command to complete */
  2180. timeleft = wait_for_completion_interruptible_timeout(
  2181. cmd_completion,
  2182. USB_CTRL_SET_TIMEOUT);
  2183. if (timeleft <= 0) {
  2184. xhci_warn(xhci, "%s while waiting for %s command\n",
  2185. timeleft == 0 ? "Timeout" : "Signal",
  2186. ctx_change == 0 ?
  2187. "configure endpoint" :
  2188. "evaluate context");
  2189. /* FIXME cancel the configure endpoint command */
  2190. return -ETIME;
  2191. }
  2192. if (!ctx_change)
  2193. ret = xhci_configure_endpoint_result(xhci, udev, cmd_status);
  2194. else
  2195. ret = xhci_evaluate_context_result(xhci, udev, cmd_status);
  2196. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
  2197. spin_lock_irqsave(&xhci->lock, flags);
  2198. /* If the command failed, remove the reserved resources.
  2199. * Otherwise, clean up the estimate to include dropped eps.
  2200. */
  2201. if (ret)
  2202. xhci_free_host_resources(xhci, in_ctx);
  2203. else
  2204. xhci_finish_resource_reservation(xhci, in_ctx);
  2205. spin_unlock_irqrestore(&xhci->lock, flags);
  2206. }
  2207. return ret;
  2208. }
  2209. /* Called after one or more calls to xhci_add_endpoint() or
  2210. * xhci_drop_endpoint(). If this call fails, the USB core is expected
  2211. * to call xhci_reset_bandwidth().
  2212. *
  2213. * Since we are in the middle of changing either configuration or
  2214. * installing a new alt setting, the USB core won't allow URBs to be
  2215. * enqueued for any endpoint on the old config or interface. Nothing
  2216. * else should be touching the xhci->devs[slot_id] structure, so we
  2217. * don't need to take the xhci->lock for manipulating that.
  2218. */
  2219. int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
  2220. {
  2221. int i;
  2222. int ret = 0;
  2223. struct xhci_hcd *xhci;
  2224. struct xhci_virt_device *virt_dev;
  2225. struct xhci_input_control_ctx *ctrl_ctx;
  2226. struct xhci_slot_ctx *slot_ctx;
  2227. ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
  2228. if (ret <= 0)
  2229. return ret;
  2230. xhci = hcd_to_xhci(hcd);
  2231. if (xhci->xhc_state & XHCI_STATE_DYING)
  2232. return -ENODEV;
  2233. xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
  2234. virt_dev = xhci->devs[udev->slot_id];
  2235. /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
  2236. ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
  2237. ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
  2238. ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG);
  2239. ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG));
  2240. /* Don't issue the command if there's no endpoints to update. */
  2241. if (ctrl_ctx->add_flags == cpu_to_le32(SLOT_FLAG) &&
  2242. ctrl_ctx->drop_flags == 0)
  2243. return 0;
  2244. xhci_dbg(xhci, "New Input Control Context:\n");
  2245. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
  2246. xhci_dbg_ctx(xhci, virt_dev->in_ctx,
  2247. LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx->dev_info)));
  2248. ret = xhci_configure_endpoint(xhci, udev, NULL,
  2249. false, false);
  2250. if (ret) {
  2251. /* Callee should call reset_bandwidth() */
  2252. return ret;
  2253. }
  2254. xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
  2255. xhci_dbg_ctx(xhci, virt_dev->out_ctx,
  2256. LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx->dev_info)));
  2257. /* Free any rings that were dropped, but not changed. */
  2258. for (i = 1; i < 31; ++i) {
  2259. if ((le32_to_cpu(ctrl_ctx->drop_flags) & (1 << (i + 1))) &&
  2260. !(le32_to_cpu(ctrl_ctx->add_flags) & (1 << (i + 1))))
  2261. xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
  2262. }
  2263. xhci_zero_in_ctx(xhci, virt_dev);
  2264. /*
  2265. * Install any rings for completely new endpoints or changed endpoints,
  2266. * and free or cache any old rings from changed endpoints.
  2267. */
  2268. for (i = 1; i < 31; ++i) {
  2269. if (!virt_dev->eps[i].new_ring)
  2270. continue;
  2271. /* Only cache or free the old ring if it exists.
  2272. * It may not if this is the first add of an endpoint.
  2273. */
  2274. if (virt_dev->eps[i].ring) {
  2275. xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
  2276. }
  2277. virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
  2278. virt_dev->eps[i].new_ring = NULL;
  2279. }
  2280. return ret;
  2281. }
  2282. void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
  2283. {
  2284. struct xhci_hcd *xhci;
  2285. struct xhci_virt_device *virt_dev;
  2286. int i, ret;
  2287. ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
  2288. if (ret <= 0)
  2289. return;
  2290. xhci = hcd_to_xhci(hcd);
  2291. xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
  2292. virt_dev = xhci->devs[udev->slot_id];
  2293. /* Free any rings allocated for added endpoints */
  2294. for (i = 0; i < 31; ++i) {
  2295. if (virt_dev->eps[i].new_ring) {
  2296. xhci_ring_free(xhci, virt_dev->eps[i].new_ring);
  2297. virt_dev->eps[i].new_ring = NULL;
  2298. }
  2299. }
  2300. xhci_zero_in_ctx(xhci, virt_dev);
  2301. }
  2302. static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci,
  2303. struct xhci_container_ctx *in_ctx,
  2304. struct xhci_container_ctx *out_ctx,
  2305. u32 add_flags, u32 drop_flags)
  2306. {
  2307. struct xhci_input_control_ctx *ctrl_ctx;
  2308. ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
  2309. ctrl_ctx->add_flags = cpu_to_le32(add_flags);
  2310. ctrl_ctx->drop_flags = cpu_to_le32(drop_flags);
  2311. xhci_slot_copy(xhci, in_ctx, out_ctx);
  2312. ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
  2313. xhci_dbg(xhci, "Input Context:\n");
  2314. xhci_dbg_ctx(xhci, in_ctx, xhci_last_valid_endpoint(add_flags));
  2315. }
  2316. static void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci,
  2317. unsigned int slot_id, unsigned int ep_index,
  2318. struct xhci_dequeue_state *deq_state)
  2319. {
  2320. struct xhci_container_ctx *in_ctx;
  2321. struct xhci_ep_ctx *ep_ctx;
  2322. u32 added_ctxs;
  2323. dma_addr_t addr;
  2324. xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
  2325. xhci->devs[slot_id]->out_ctx, ep_index);
  2326. in_ctx = xhci->devs[slot_id]->in_ctx;
  2327. ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
  2328. addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
  2329. deq_state->new_deq_ptr);
  2330. if (addr == 0) {
  2331. xhci_warn(xhci, "WARN Cannot submit config ep after "
  2332. "reset ep command\n");
  2333. xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n",
  2334. deq_state->new_deq_seg,
  2335. deq_state->new_deq_ptr);
  2336. return;
  2337. }
  2338. ep_ctx->deq = cpu_to_le64(addr | deq_state->new_cycle_state);
  2339. added_ctxs = xhci_get_endpoint_flag_from_index(ep_index);
  2340. xhci_setup_input_ctx_for_config_ep(xhci, xhci->devs[slot_id]->in_ctx,
  2341. xhci->devs[slot_id]->out_ctx, added_ctxs, added_ctxs);
  2342. }
  2343. void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
  2344. struct usb_device *udev, unsigned int ep_index)
  2345. {
  2346. struct xhci_dequeue_state deq_state;
  2347. struct xhci_virt_ep *ep;
  2348. xhci_dbg(xhci, "Cleaning up stalled endpoint ring\n");
  2349. ep = &xhci->devs[udev->slot_id]->eps[ep_index];
  2350. /* We need to move the HW's dequeue pointer past this TD,
  2351. * or it will attempt to resend it on the next doorbell ring.
  2352. */
  2353. xhci_find_new_dequeue_state(xhci, udev->slot_id,
  2354. ep_index, ep->stopped_stream, ep->stopped_td,
  2355. &deq_state);
  2356. /* HW with the reset endpoint quirk will use the saved dequeue state to
  2357. * issue a configure endpoint command later.
  2358. */
  2359. if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) {
  2360. xhci_dbg(xhci, "Queueing new dequeue state\n");
  2361. xhci_queue_new_dequeue_state(xhci, udev->slot_id,
  2362. ep_index, ep->stopped_stream, &deq_state);
  2363. } else {
  2364. /* Better hope no one uses the input context between now and the
  2365. * reset endpoint completion!
  2366. * XXX: No idea how this hardware will react when stream rings
  2367. * are enabled.
  2368. */
  2369. xhci_dbg(xhci, "Setting up input context for "
  2370. "configure endpoint command\n");
  2371. xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id,
  2372. ep_index, &deq_state);
  2373. }
  2374. }
  2375. /* Deal with stalled endpoints. The core should have sent the control message
  2376. * to clear the halt condition. However, we need to make the xHCI hardware
  2377. * reset its sequence number, since a device will expect a sequence number of
  2378. * zero after the halt condition is cleared.
  2379. * Context: in_interrupt
  2380. */
  2381. void xhci_endpoint_reset(struct usb_hcd *hcd,
  2382. struct usb_host_endpoint *ep)
  2383. {
  2384. struct xhci_hcd *xhci;
  2385. struct usb_device *udev;
  2386. unsigned int ep_index;
  2387. unsigned long flags;
  2388. int ret;
  2389. struct xhci_virt_ep *virt_ep;
  2390. xhci = hcd_to_xhci(hcd);
  2391. udev = (struct usb_device *) ep->hcpriv;
  2392. /* Called with a root hub endpoint (or an endpoint that wasn't added
  2393. * with xhci_add_endpoint()
  2394. */
  2395. if (!ep->hcpriv)
  2396. return;
  2397. ep_index = xhci_get_endpoint_index(&ep->desc);
  2398. virt_ep = &xhci->devs[udev->slot_id]->eps[ep_index];
  2399. if (!virt_ep->stopped_td) {
  2400. xhci_dbg(xhci, "Endpoint 0x%x not halted, refusing to reset.\n",
  2401. ep->desc.bEndpointAddress);
  2402. return;
  2403. }
  2404. if (usb_endpoint_xfer_control(&ep->desc)) {
  2405. xhci_dbg(xhci, "Control endpoint stall already handled.\n");
  2406. return;
  2407. }
  2408. xhci_dbg(xhci, "Queueing reset endpoint command\n");
  2409. spin_lock_irqsave(&xhci->lock, flags);
  2410. ret = xhci_queue_reset_ep(xhci, udev->slot_id, ep_index);
  2411. /*
  2412. * Can't change the ring dequeue pointer until it's transitioned to the
  2413. * stopped state, which is only upon a successful reset endpoint
  2414. * command. Better hope that last command worked!
  2415. */
  2416. if (!ret) {
  2417. xhci_cleanup_stalled_ring(xhci, udev, ep_index);
  2418. kfree(virt_ep->stopped_td);
  2419. xhci_ring_cmd_db(xhci);
  2420. }
  2421. virt_ep->stopped_td = NULL;
  2422. virt_ep->stopped_trb = NULL;
  2423. virt_ep->stopped_stream = 0;
  2424. spin_unlock_irqrestore(&xhci->lock, flags);
  2425. if (ret)
  2426. xhci_warn(xhci, "FIXME allocate a new ring segment\n");
  2427. }
  2428. static int xhci_check_streams_endpoint(struct xhci_hcd *xhci,
  2429. struct usb_device *udev, struct usb_host_endpoint *ep,
  2430. unsigned int slot_id)
  2431. {
  2432. int ret;
  2433. unsigned int ep_index;
  2434. unsigned int ep_state;
  2435. if (!ep)
  2436. return -EINVAL;
  2437. ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, true, __func__);
  2438. if (ret <= 0)
  2439. return -EINVAL;
  2440. if (ep->ss_ep_comp.bmAttributes == 0) {
  2441. xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion"
  2442. " descriptor for ep 0x%x does not support streams\n",
  2443. ep->desc.bEndpointAddress);
  2444. return -EINVAL;
  2445. }
  2446. ep_index = xhci_get_endpoint_index(&ep->desc);
  2447. ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
  2448. if (ep_state & EP_HAS_STREAMS ||
  2449. ep_state & EP_GETTING_STREAMS) {
  2450. xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x "
  2451. "already has streams set up.\n",
  2452. ep->desc.bEndpointAddress);
  2453. xhci_warn(xhci, "Send email to xHCI maintainer and ask for "
  2454. "dynamic stream context array reallocation.\n");
  2455. return -EINVAL;
  2456. }
  2457. if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) {
  2458. xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk "
  2459. "endpoint 0x%x; URBs are pending.\n",
  2460. ep->desc.bEndpointAddress);
  2461. return -EINVAL;
  2462. }
  2463. return 0;
  2464. }
  2465. static void xhci_calculate_streams_entries(struct xhci_hcd *xhci,
  2466. unsigned int *num_streams, unsigned int *num_stream_ctxs)
  2467. {
  2468. unsigned int max_streams;
  2469. /* The stream context array size must be a power of two */
  2470. *num_stream_ctxs = roundup_pow_of_two(*num_streams);
  2471. /*
  2472. * Find out how many primary stream array entries the host controller
  2473. * supports. Later we may use secondary stream arrays (similar to 2nd
  2474. * level page entries), but that's an optional feature for xHCI host
  2475. * controllers. xHCs must support at least 4 stream IDs.
  2476. */
  2477. max_streams = HCC_MAX_PSA(xhci->hcc_params);
  2478. if (*num_stream_ctxs > max_streams) {
  2479. xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n",
  2480. max_streams);
  2481. *num_stream_ctxs = max_streams;
  2482. *num_streams = max_streams;
  2483. }
  2484. }
  2485. /* Returns an error code if one of the endpoint already has streams.
  2486. * This does not change any data structures, it only checks and gathers
  2487. * information.
  2488. */
  2489. static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci,
  2490. struct usb_device *udev,
  2491. struct usb_host_endpoint **eps, unsigned int num_eps,
  2492. unsigned int *num_streams, u32 *changed_ep_bitmask)
  2493. {
  2494. unsigned int max_streams;
  2495. unsigned int endpoint_flag;
  2496. int i;
  2497. int ret;
  2498. for (i = 0; i < num_eps; i++) {
  2499. ret = xhci_check_streams_endpoint(xhci, udev,
  2500. eps[i], udev->slot_id);
  2501. if (ret < 0)
  2502. return ret;
  2503. max_streams = usb_ss_max_streams(&eps[i]->ss_ep_comp);
  2504. if (max_streams < (*num_streams - 1)) {
  2505. xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n",
  2506. eps[i]->desc.bEndpointAddress,
  2507. max_streams);
  2508. *num_streams = max_streams+1;
  2509. }
  2510. endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc);
  2511. if (*changed_ep_bitmask & endpoint_flag)
  2512. return -EINVAL;
  2513. *changed_ep_bitmask |= endpoint_flag;
  2514. }
  2515. return 0;
  2516. }
  2517. static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci,
  2518. struct usb_device *udev,
  2519. struct usb_host_endpoint **eps, unsigned int num_eps)
  2520. {
  2521. u32 changed_ep_bitmask = 0;
  2522. unsigned int slot_id;
  2523. unsigned int ep_index;
  2524. unsigned int ep_state;
  2525. int i;
  2526. slot_id = udev->slot_id;
  2527. if (!xhci->devs[slot_id])
  2528. return 0;
  2529. for (i = 0; i < num_eps; i++) {
  2530. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2531. ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
  2532. /* Are streams already being freed for the endpoint? */
  2533. if (ep_state & EP_GETTING_NO_STREAMS) {
  2534. xhci_warn(xhci, "WARN Can't disable streams for "
  2535. "endpoint 0x%x\n, "
  2536. "streams are being disabled already.",
  2537. eps[i]->desc.bEndpointAddress);
  2538. return 0;
  2539. }
  2540. /* Are there actually any streams to free? */
  2541. if (!(ep_state & EP_HAS_STREAMS) &&
  2542. !(ep_state & EP_GETTING_STREAMS)) {
  2543. xhci_warn(xhci, "WARN Can't disable streams for "
  2544. "endpoint 0x%x\n, "
  2545. "streams are already disabled!",
  2546. eps[i]->desc.bEndpointAddress);
  2547. xhci_warn(xhci, "WARN xhci_free_streams() called "
  2548. "with non-streams endpoint\n");
  2549. return 0;
  2550. }
  2551. changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc);
  2552. }
  2553. return changed_ep_bitmask;
  2554. }
  2555. /*
  2556. * The USB device drivers use this function (though the HCD interface in USB
  2557. * core) to prepare a set of bulk endpoints to use streams. Streams are used to
  2558. * coordinate mass storage command queueing across multiple endpoints (basically
  2559. * a stream ID == a task ID).
  2560. *
  2561. * Setting up streams involves allocating the same size stream context array
  2562. * for each endpoint and issuing a configure endpoint command for all endpoints.
  2563. *
  2564. * Don't allow the call to succeed if one endpoint only supports one stream
  2565. * (which means it doesn't support streams at all).
  2566. *
  2567. * Drivers may get less stream IDs than they asked for, if the host controller
  2568. * hardware or endpoints claim they can't support the number of requested
  2569. * stream IDs.
  2570. */
  2571. int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
  2572. struct usb_host_endpoint **eps, unsigned int num_eps,
  2573. unsigned int num_streams, gfp_t mem_flags)
  2574. {
  2575. int i, ret;
  2576. struct xhci_hcd *xhci;
  2577. struct xhci_virt_device *vdev;
  2578. struct xhci_command *config_cmd;
  2579. unsigned int ep_index;
  2580. unsigned int num_stream_ctxs;
  2581. unsigned long flags;
  2582. u32 changed_ep_bitmask = 0;
  2583. if (!eps)
  2584. return -EINVAL;
  2585. /* Add one to the number of streams requested to account for
  2586. * stream 0 that is reserved for xHCI usage.
  2587. */
  2588. num_streams += 1;
  2589. xhci = hcd_to_xhci(hcd);
  2590. xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n",
  2591. num_streams);
  2592. config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
  2593. if (!config_cmd) {
  2594. xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
  2595. return -ENOMEM;
  2596. }
  2597. /* Check to make sure all endpoints are not already configured for
  2598. * streams. While we're at it, find the maximum number of streams that
  2599. * all the endpoints will support and check for duplicate endpoints.
  2600. */
  2601. spin_lock_irqsave(&xhci->lock, flags);
  2602. ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps,
  2603. num_eps, &num_streams, &changed_ep_bitmask);
  2604. if (ret < 0) {
  2605. xhci_free_command(xhci, config_cmd);
  2606. spin_unlock_irqrestore(&xhci->lock, flags);
  2607. return ret;
  2608. }
  2609. if (num_streams <= 1) {
  2610. xhci_warn(xhci, "WARN: endpoints can't handle "
  2611. "more than one stream.\n");
  2612. xhci_free_command(xhci, config_cmd);
  2613. spin_unlock_irqrestore(&xhci->lock, flags);
  2614. return -EINVAL;
  2615. }
  2616. vdev = xhci->devs[udev->slot_id];
  2617. /* Mark each endpoint as being in transition, so
  2618. * xhci_urb_enqueue() will reject all URBs.
  2619. */
  2620. for (i = 0; i < num_eps; i++) {
  2621. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2622. vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS;
  2623. }
  2624. spin_unlock_irqrestore(&xhci->lock, flags);
  2625. /* Setup internal data structures and allocate HW data structures for
  2626. * streams (but don't install the HW structures in the input context
  2627. * until we're sure all memory allocation succeeded).
  2628. */
  2629. xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs);
  2630. xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n",
  2631. num_stream_ctxs, num_streams);
  2632. for (i = 0; i < num_eps; i++) {
  2633. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2634. vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci,
  2635. num_stream_ctxs,
  2636. num_streams, mem_flags);
  2637. if (!vdev->eps[ep_index].stream_info)
  2638. goto cleanup;
  2639. /* Set maxPstreams in endpoint context and update deq ptr to
  2640. * point to stream context array. FIXME
  2641. */
  2642. }
  2643. /* Set up the input context for a configure endpoint command. */
  2644. for (i = 0; i < num_eps; i++) {
  2645. struct xhci_ep_ctx *ep_ctx;
  2646. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2647. ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index);
  2648. xhci_endpoint_copy(xhci, config_cmd->in_ctx,
  2649. vdev->out_ctx, ep_index);
  2650. xhci_setup_streams_ep_input_ctx(xhci, ep_ctx,
  2651. vdev->eps[ep_index].stream_info);
  2652. }
  2653. /* Tell the HW to drop its old copy of the endpoint context info
  2654. * and add the updated copy from the input context.
  2655. */
  2656. xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx,
  2657. vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
  2658. /* Issue and wait for the configure endpoint command */
  2659. ret = xhci_configure_endpoint(xhci, udev, config_cmd,
  2660. false, false);
  2661. /* xHC rejected the configure endpoint command for some reason, so we
  2662. * leave the old ring intact and free our internal streams data
  2663. * structure.
  2664. */
  2665. if (ret < 0)
  2666. goto cleanup;
  2667. spin_lock_irqsave(&xhci->lock, flags);
  2668. for (i = 0; i < num_eps; i++) {
  2669. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2670. vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
  2671. xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n",
  2672. udev->slot_id, ep_index);
  2673. vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS;
  2674. }
  2675. xhci_free_command(xhci, config_cmd);
  2676. spin_unlock_irqrestore(&xhci->lock, flags);
  2677. /* Subtract 1 for stream 0, which drivers can't use */
  2678. return num_streams - 1;
  2679. cleanup:
  2680. /* If it didn't work, free the streams! */
  2681. for (i = 0; i < num_eps; i++) {
  2682. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2683. xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
  2684. vdev->eps[ep_index].stream_info = NULL;
  2685. /* FIXME Unset maxPstreams in endpoint context and
  2686. * update deq ptr to point to normal string ring.
  2687. */
  2688. vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
  2689. vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
  2690. xhci_endpoint_zero(xhci, vdev, eps[i]);
  2691. }
  2692. xhci_free_command(xhci, config_cmd);
  2693. return -ENOMEM;
  2694. }
  2695. /* Transition the endpoint from using streams to being a "normal" endpoint
  2696. * without streams.
  2697. *
  2698. * Modify the endpoint context state, submit a configure endpoint command,
  2699. * and free all endpoint rings for streams if that completes successfully.
  2700. */
  2701. int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
  2702. struct usb_host_endpoint **eps, unsigned int num_eps,
  2703. gfp_t mem_flags)
  2704. {
  2705. int i, ret;
  2706. struct xhci_hcd *xhci;
  2707. struct xhci_virt_device *vdev;
  2708. struct xhci_command *command;
  2709. unsigned int ep_index;
  2710. unsigned long flags;
  2711. u32 changed_ep_bitmask;
  2712. xhci = hcd_to_xhci(hcd);
  2713. vdev = xhci->devs[udev->slot_id];
  2714. /* Set up a configure endpoint command to remove the streams rings */
  2715. spin_lock_irqsave(&xhci->lock, flags);
  2716. changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci,
  2717. udev, eps, num_eps);
  2718. if (changed_ep_bitmask == 0) {
  2719. spin_unlock_irqrestore(&xhci->lock, flags);
  2720. return -EINVAL;
  2721. }
  2722. /* Use the xhci_command structure from the first endpoint. We may have
  2723. * allocated too many, but the driver may call xhci_free_streams() for
  2724. * each endpoint it grouped into one call to xhci_alloc_streams().
  2725. */
  2726. ep_index = xhci_get_endpoint_index(&eps[0]->desc);
  2727. command = vdev->eps[ep_index].stream_info->free_streams_command;
  2728. for (i = 0; i < num_eps; i++) {
  2729. struct xhci_ep_ctx *ep_ctx;
  2730. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2731. ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
  2732. xhci->devs[udev->slot_id]->eps[ep_index].ep_state |=
  2733. EP_GETTING_NO_STREAMS;
  2734. xhci_endpoint_copy(xhci, command->in_ctx,
  2735. vdev->out_ctx, ep_index);
  2736. xhci_setup_no_streams_ep_input_ctx(xhci, ep_ctx,
  2737. &vdev->eps[ep_index]);
  2738. }
  2739. xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx,
  2740. vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
  2741. spin_unlock_irqrestore(&xhci->lock, flags);
  2742. /* Issue and wait for the configure endpoint command,
  2743. * which must succeed.
  2744. */
  2745. ret = xhci_configure_endpoint(xhci, udev, command,
  2746. false, true);
  2747. /* xHC rejected the configure endpoint command for some reason, so we
  2748. * leave the streams rings intact.
  2749. */
  2750. if (ret < 0)
  2751. return ret;
  2752. spin_lock_irqsave(&xhci->lock, flags);
  2753. for (i = 0; i < num_eps; i++) {
  2754. ep_index = xhci_get_endpoint_index(&eps[i]->desc);
  2755. xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
  2756. vdev->eps[ep_index].stream_info = NULL;
  2757. /* FIXME Unset maxPstreams in endpoint context and
  2758. * update deq ptr to point to normal string ring.
  2759. */
  2760. vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS;
  2761. vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
  2762. }
  2763. spin_unlock_irqrestore(&xhci->lock, flags);
  2764. return 0;
  2765. }
  2766. /*
  2767. * Deletes endpoint resources for endpoints that were active before a Reset
  2768. * Device command, or a Disable Slot command. The Reset Device command leaves
  2769. * the control endpoint intact, whereas the Disable Slot command deletes it.
  2770. *
  2771. * Must be called with xhci->lock held.
  2772. */
  2773. void xhci_free_device_endpoint_resources(struct xhci_hcd *xhci,
  2774. struct xhci_virt_device *virt_dev, bool drop_control_ep)
  2775. {
  2776. int i;
  2777. unsigned int num_dropped_eps = 0;
  2778. unsigned int drop_flags = 0;
  2779. for (i = (drop_control_ep ? 0 : 1); i < 31; i++) {
  2780. if (virt_dev->eps[i].ring) {
  2781. drop_flags |= 1 << i;
  2782. num_dropped_eps++;
  2783. }
  2784. }
  2785. xhci->num_active_eps -= num_dropped_eps;
  2786. if (num_dropped_eps)
  2787. xhci_dbg(xhci, "Dropped %u ep ctxs, flags = 0x%x, "
  2788. "%u now active.\n",
  2789. num_dropped_eps, drop_flags,
  2790. xhci->num_active_eps);
  2791. }
  2792. /*
  2793. * This submits a Reset Device Command, which will set the device state to 0,
  2794. * set the device address to 0, and disable all the endpoints except the default
  2795. * control endpoint. The USB core should come back and call
  2796. * xhci_address_device(), and then re-set up the configuration. If this is
  2797. * called because of a usb_reset_and_verify_device(), then the old alternate
  2798. * settings will be re-installed through the normal bandwidth allocation
  2799. * functions.
  2800. *
  2801. * Wait for the Reset Device command to finish. Remove all structures
  2802. * associated with the endpoints that were disabled. Clear the input device
  2803. * structure? Cache the rings? Reset the control endpoint 0 max packet size?
  2804. *
  2805. * If the virt_dev to be reset does not exist or does not match the udev,
  2806. * it means the device is lost, possibly due to the xHC restore error and
  2807. * re-initialization during S3/S4. In this case, call xhci_alloc_dev() to
  2808. * re-allocate the device.
  2809. */
  2810. int xhci_discover_or_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
  2811. {
  2812. int ret, i;
  2813. unsigned long flags;
  2814. struct xhci_hcd *xhci;
  2815. unsigned int slot_id;
  2816. struct xhci_virt_device *virt_dev;
  2817. struct xhci_command *reset_device_cmd;
  2818. int timeleft;
  2819. int last_freed_endpoint;
  2820. struct xhci_slot_ctx *slot_ctx;
  2821. int old_active_eps = 0;
  2822. ret = xhci_check_args(hcd, udev, NULL, 0, false, __func__);
  2823. if (ret <= 0)
  2824. return ret;
  2825. xhci = hcd_to_xhci(hcd);
  2826. slot_id = udev->slot_id;
  2827. virt_dev = xhci->devs[slot_id];
  2828. if (!virt_dev) {
  2829. xhci_dbg(xhci, "The device to be reset with slot ID %u does "
  2830. "not exist. Re-allocate the device\n", slot_id);
  2831. ret = xhci_alloc_dev(hcd, udev);
  2832. if (ret == 1)
  2833. return 0;
  2834. else
  2835. return -EINVAL;
  2836. }
  2837. if (virt_dev->udev != udev) {
  2838. /* If the virt_dev and the udev does not match, this virt_dev
  2839. * may belong to another udev.
  2840. * Re-allocate the device.
  2841. */
  2842. xhci_dbg(xhci, "The device to be reset with slot ID %u does "
  2843. "not match the udev. Re-allocate the device\n",
  2844. slot_id);
  2845. ret = xhci_alloc_dev(hcd, udev);
  2846. if (ret == 1)
  2847. return 0;
  2848. else
  2849. return -EINVAL;
  2850. }
  2851. /* If device is not setup, there is no point in resetting it */
  2852. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
  2853. if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) ==
  2854. SLOT_STATE_DISABLED)
  2855. return 0;
  2856. xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id);
  2857. /* Allocate the command structure that holds the struct completion.
  2858. * Assume we're in process context, since the normal device reset
  2859. * process has to wait for the device anyway. Storage devices are
  2860. * reset as part of error handling, so use GFP_NOIO instead of
  2861. * GFP_KERNEL.
  2862. */
  2863. reset_device_cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
  2864. if (!reset_device_cmd) {
  2865. xhci_dbg(xhci, "Couldn't allocate command structure.\n");
  2866. return -ENOMEM;
  2867. }
  2868. /* Attempt to submit the Reset Device command to the command ring */
  2869. spin_lock_irqsave(&xhci->lock, flags);
  2870. reset_device_cmd->command_trb = xhci->cmd_ring->enqueue;
  2871. /* Enqueue pointer can be left pointing to the link TRB,
  2872. * we must handle that
  2873. */
  2874. if (TRB_TYPE_LINK_LE32(reset_device_cmd->command_trb->link.control))
  2875. reset_device_cmd->command_trb =
  2876. xhci->cmd_ring->enq_seg->next->trbs;
  2877. list_add_tail(&reset_device_cmd->cmd_list, &virt_dev->cmd_list);
  2878. ret = xhci_queue_reset_device(xhci, slot_id);
  2879. if (ret) {
  2880. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  2881. list_del(&reset_device_cmd->cmd_list);
  2882. spin_unlock_irqrestore(&xhci->lock, flags);
  2883. goto command_cleanup;
  2884. }
  2885. xhci_ring_cmd_db(xhci);
  2886. spin_unlock_irqrestore(&xhci->lock, flags);
  2887. /* Wait for the Reset Device command to finish */
  2888. timeleft = wait_for_completion_interruptible_timeout(
  2889. reset_device_cmd->completion,
  2890. USB_CTRL_SET_TIMEOUT);
  2891. if (timeleft <= 0) {
  2892. xhci_warn(xhci, "%s while waiting for reset device command\n",
  2893. timeleft == 0 ? "Timeout" : "Signal");
  2894. spin_lock_irqsave(&xhci->lock, flags);
  2895. /* The timeout might have raced with the event ring handler, so
  2896. * only delete from the list if the item isn't poisoned.
  2897. */
  2898. if (reset_device_cmd->cmd_list.next != LIST_POISON1)
  2899. list_del(&reset_device_cmd->cmd_list);
  2900. spin_unlock_irqrestore(&xhci->lock, flags);
  2901. ret = -ETIME;
  2902. goto command_cleanup;
  2903. }
  2904. /* The Reset Device command can't fail, according to the 0.95/0.96 spec,
  2905. * unless we tried to reset a slot ID that wasn't enabled,
  2906. * or the device wasn't in the addressed or configured state.
  2907. */
  2908. ret = reset_device_cmd->status;
  2909. switch (ret) {
  2910. case COMP_EBADSLT: /* 0.95 completion code for bad slot ID */
  2911. case COMP_CTX_STATE: /* 0.96 completion code for same thing */
  2912. xhci_info(xhci, "Can't reset device (slot ID %u) in %s state\n",
  2913. slot_id,
  2914. xhci_get_slot_state(xhci, virt_dev->out_ctx));
  2915. xhci_info(xhci, "Not freeing device rings.\n");
  2916. /* Don't treat this as an error. May change my mind later. */
  2917. ret = 0;
  2918. goto command_cleanup;
  2919. case COMP_SUCCESS:
  2920. xhci_dbg(xhci, "Successful reset device command.\n");
  2921. break;
  2922. default:
  2923. if (xhci_is_vendor_info_code(xhci, ret))
  2924. break;
  2925. xhci_warn(xhci, "Unknown completion code %u for "
  2926. "reset device command.\n", ret);
  2927. ret = -EINVAL;
  2928. goto command_cleanup;
  2929. }
  2930. /* Free up host controller endpoint resources */
  2931. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
  2932. spin_lock_irqsave(&xhci->lock, flags);
  2933. /* Don't delete the default control endpoint resources */
  2934. xhci_free_device_endpoint_resources(xhci, virt_dev, false);
  2935. spin_unlock_irqrestore(&xhci->lock, flags);
  2936. }
  2937. /* Everything but endpoint 0 is disabled, so free or cache the rings. */
  2938. last_freed_endpoint = 1;
  2939. for (i = 1; i < 31; ++i) {
  2940. struct xhci_virt_ep *ep = &virt_dev->eps[i];
  2941. if (ep->ep_state & EP_HAS_STREAMS) {
  2942. xhci_free_stream_info(xhci, ep->stream_info);
  2943. ep->stream_info = NULL;
  2944. ep->ep_state &= ~EP_HAS_STREAMS;
  2945. }
  2946. if (ep->ring) {
  2947. xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
  2948. last_freed_endpoint = i;
  2949. }
  2950. if (!list_empty(&virt_dev->eps[i].bw_endpoint_list))
  2951. xhci_drop_ep_from_interval_table(xhci,
  2952. &virt_dev->eps[i].bw_info,
  2953. virt_dev->bw_table,
  2954. udev,
  2955. &virt_dev->eps[i],
  2956. virt_dev->tt_info);
  2957. xhci_clear_endpoint_bw_info(&virt_dev->eps[i].bw_info);
  2958. }
  2959. /* If necessary, update the number of active TTs on this root port */
  2960. xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
  2961. xhci_dbg(xhci, "Output context after successful reset device cmd:\n");
  2962. xhci_dbg_ctx(xhci, virt_dev->out_ctx, last_freed_endpoint);
  2963. ret = 0;
  2964. command_cleanup:
  2965. xhci_free_command(xhci, reset_device_cmd);
  2966. return ret;
  2967. }
  2968. /*
  2969. * At this point, the struct usb_device is about to go away, the device has
  2970. * disconnected, and all traffic has been stopped and the endpoints have been
  2971. * disabled. Free any HC data structures associated with that device.
  2972. */
  2973. void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
  2974. {
  2975. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  2976. struct xhci_virt_device *virt_dev;
  2977. unsigned long flags;
  2978. u32 state;
  2979. int i, ret;
  2980. ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
  2981. /* If the host is halted due to driver unload, we still need to free the
  2982. * device.
  2983. */
  2984. if (ret <= 0 && ret != -ENODEV)
  2985. return;
  2986. virt_dev = xhci->devs[udev->slot_id];
  2987. /* Stop any wayward timer functions (which may grab the lock) */
  2988. for (i = 0; i < 31; ++i) {
  2989. virt_dev->eps[i].ep_state &= ~EP_HALT_PENDING;
  2990. del_timer_sync(&virt_dev->eps[i].stop_cmd_timer);
  2991. }
  2992. if (udev->usb2_hw_lpm_enabled) {
  2993. xhci_set_usb2_hardware_lpm(hcd, udev, 0);
  2994. udev->usb2_hw_lpm_enabled = 0;
  2995. }
  2996. spin_lock_irqsave(&xhci->lock, flags);
  2997. /* Don't disable the slot if the host controller is dead. */
  2998. state = xhci_readl(xhci, &xhci->op_regs->status);
  2999. if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) ||
  3000. (xhci->xhc_state & XHCI_STATE_HALTED)) {
  3001. xhci_free_virt_device(xhci, udev->slot_id);
  3002. spin_unlock_irqrestore(&xhci->lock, flags);
  3003. return;
  3004. }
  3005. if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
  3006. spin_unlock_irqrestore(&xhci->lock, flags);
  3007. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  3008. return;
  3009. }
  3010. xhci_ring_cmd_db(xhci);
  3011. spin_unlock_irqrestore(&xhci->lock, flags);
  3012. /*
  3013. * Event command completion handler will free any data structures
  3014. * associated with the slot. XXX Can free sleep?
  3015. */
  3016. }
  3017. /*
  3018. * Checks if we have enough host controller resources for the default control
  3019. * endpoint.
  3020. *
  3021. * Must be called with xhci->lock held.
  3022. */
  3023. static int xhci_reserve_host_control_ep_resources(struct xhci_hcd *xhci)
  3024. {
  3025. if (xhci->num_active_eps + 1 > xhci->limit_active_eps) {
  3026. xhci_dbg(xhci, "Not enough ep ctxs: "
  3027. "%u active, need to add 1, limit is %u.\n",
  3028. xhci->num_active_eps, xhci->limit_active_eps);
  3029. return -ENOMEM;
  3030. }
  3031. xhci->num_active_eps += 1;
  3032. xhci_dbg(xhci, "Adding 1 ep ctx, %u now active.\n",
  3033. xhci->num_active_eps);
  3034. return 0;
  3035. }
  3036. /*
  3037. * Returns 0 if the xHC ran out of device slots, the Enable Slot command
  3038. * timed out, or allocating memory failed. Returns 1 on success.
  3039. */
  3040. int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
  3041. {
  3042. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  3043. unsigned long flags;
  3044. int timeleft;
  3045. int ret;
  3046. spin_lock_irqsave(&xhci->lock, flags);
  3047. ret = xhci_queue_slot_control(xhci, TRB_ENABLE_SLOT, 0);
  3048. if (ret) {
  3049. spin_unlock_irqrestore(&xhci->lock, flags);
  3050. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  3051. return 0;
  3052. }
  3053. xhci_ring_cmd_db(xhci);
  3054. spin_unlock_irqrestore(&xhci->lock, flags);
  3055. /* XXX: how much time for xHC slot assignment? */
  3056. timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
  3057. USB_CTRL_SET_TIMEOUT);
  3058. if (timeleft <= 0) {
  3059. xhci_warn(xhci, "%s while waiting for a slot\n",
  3060. timeleft == 0 ? "Timeout" : "Signal");
  3061. /* FIXME cancel the enable slot request */
  3062. return 0;
  3063. }
  3064. if (!xhci->slot_id) {
  3065. xhci_err(xhci, "Error while assigning device slot ID\n");
  3066. return 0;
  3067. }
  3068. if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
  3069. spin_lock_irqsave(&xhci->lock, flags);
  3070. ret = xhci_reserve_host_control_ep_resources(xhci);
  3071. if (ret) {
  3072. spin_unlock_irqrestore(&xhci->lock, flags);
  3073. xhci_warn(xhci, "Not enough host resources, "
  3074. "active endpoint contexts = %u\n",
  3075. xhci->num_active_eps);
  3076. goto disable_slot;
  3077. }
  3078. spin_unlock_irqrestore(&xhci->lock, flags);
  3079. }
  3080. /* Use GFP_NOIO, since this function can be called from
  3081. * xhci_discover_or_reset_device(), which may be called as part of
  3082. * mass storage driver error handling.
  3083. */
  3084. if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_NOIO)) {
  3085. xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
  3086. goto disable_slot;
  3087. }
  3088. udev->slot_id = xhci->slot_id;
  3089. /* Is this a LS or FS device under a HS hub? */
  3090. /* Hub or peripherial? */
  3091. return 1;
  3092. disable_slot:
  3093. /* Disable slot, if we can do it without mem alloc */
  3094. spin_lock_irqsave(&xhci->lock, flags);
  3095. if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
  3096. xhci_ring_cmd_db(xhci);
  3097. spin_unlock_irqrestore(&xhci->lock, flags);
  3098. return 0;
  3099. }
  3100. /*
  3101. * Issue an Address Device command (which will issue a SetAddress request to
  3102. * the device).
  3103. * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so
  3104. * we should only issue and wait on one address command at the same time.
  3105. *
  3106. * We add one to the device address issued by the hardware because the USB core
  3107. * uses address 1 for the root hubs (even though they're not really devices).
  3108. */
  3109. int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
  3110. {
  3111. unsigned long flags;
  3112. int timeleft;
  3113. struct xhci_virt_device *virt_dev;
  3114. int ret = 0;
  3115. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  3116. struct xhci_slot_ctx *slot_ctx;
  3117. struct xhci_input_control_ctx *ctrl_ctx;
  3118. u64 temp_64;
  3119. if (!udev->slot_id) {
  3120. xhci_dbg(xhci, "Bad Slot ID %d\n", udev->slot_id);
  3121. return -EINVAL;
  3122. }
  3123. virt_dev = xhci->devs[udev->slot_id];
  3124. if (WARN_ON(!virt_dev)) {
  3125. /*
  3126. * In plug/unplug torture test with an NEC controller,
  3127. * a zero-dereference was observed once due to virt_dev = 0.
  3128. * Print useful debug rather than crash if it is observed again!
  3129. */
  3130. xhci_warn(xhci, "Virt dev invalid for slot_id 0x%x!\n",
  3131. udev->slot_id);
  3132. return -EINVAL;
  3133. }
  3134. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
  3135. /*
  3136. * If this is the first Set Address since device plug-in or
  3137. * virt_device realloaction after a resume with an xHCI power loss,
  3138. * then set up the slot context.
  3139. */
  3140. if (!slot_ctx->dev_info)
  3141. xhci_setup_addressable_virt_dev(xhci, udev);
  3142. /* Otherwise, update the control endpoint ring enqueue pointer. */
  3143. else
  3144. xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev);
  3145. ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
  3146. ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
  3147. ctrl_ctx->drop_flags = 0;
  3148. xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
  3149. xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
  3150. spin_lock_irqsave(&xhci->lock, flags);
  3151. ret = xhci_queue_address_device(xhci, virt_dev->in_ctx->dma,
  3152. udev->slot_id);
  3153. if (ret) {
  3154. spin_unlock_irqrestore(&xhci->lock, flags);
  3155. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  3156. return ret;
  3157. }
  3158. xhci_ring_cmd_db(xhci);
  3159. spin_unlock_irqrestore(&xhci->lock, flags);
  3160. /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
  3161. timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
  3162. USB_CTRL_SET_TIMEOUT);
  3163. /* FIXME: From section 4.3.4: "Software shall be responsible for timing
  3164. * the SetAddress() "recovery interval" required by USB and aborting the
  3165. * command on a timeout.
  3166. */
  3167. if (timeleft <= 0) {
  3168. xhci_warn(xhci, "%s while waiting for address device command\n",
  3169. timeleft == 0 ? "Timeout" : "Signal");
  3170. /* FIXME cancel the address device command */
  3171. return -ETIME;
  3172. }
  3173. switch (virt_dev->cmd_status) {
  3174. case COMP_CTX_STATE:
  3175. case COMP_EBADSLT:
  3176. xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n",
  3177. udev->slot_id);
  3178. ret = -EINVAL;
  3179. break;
  3180. case COMP_TX_ERR:
  3181. dev_warn(&udev->dev, "Device not responding to set address.\n");
  3182. ret = -EPROTO;
  3183. break;
  3184. case COMP_DEV_ERR:
  3185. dev_warn(&udev->dev, "ERROR: Incompatible device for address "
  3186. "device command.\n");
  3187. ret = -ENODEV;
  3188. break;
  3189. case COMP_SUCCESS:
  3190. xhci_dbg(xhci, "Successful Address Device command\n");
  3191. break;
  3192. default:
  3193. xhci_err(xhci, "ERROR: unexpected command completion "
  3194. "code 0x%x.\n", virt_dev->cmd_status);
  3195. xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
  3196. xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
  3197. ret = -EINVAL;
  3198. break;
  3199. }
  3200. if (ret) {
  3201. return ret;
  3202. }
  3203. temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
  3204. xhci_dbg(xhci, "Op regs DCBAA ptr = %#016llx\n", temp_64);
  3205. xhci_dbg(xhci, "Slot ID %d dcbaa entry @%p = %#016llx\n",
  3206. udev->slot_id,
  3207. &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
  3208. (unsigned long long)
  3209. le64_to_cpu(xhci->dcbaa->dev_context_ptrs[udev->slot_id]));
  3210. xhci_dbg(xhci, "Output Context DMA address = %#08llx\n",
  3211. (unsigned long long)virt_dev->out_ctx->dma);
  3212. xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
  3213. xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
  3214. xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
  3215. xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
  3216. /*
  3217. * USB core uses address 1 for the roothubs, so we add one to the
  3218. * address given back to us by the HC.
  3219. */
  3220. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
  3221. /* Use kernel assigned address for devices; store xHC assigned
  3222. * address locally. */
  3223. virt_dev->address = (le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK)
  3224. + 1;
  3225. /* Zero the input context control for later use */
  3226. ctrl_ctx->add_flags = 0;
  3227. ctrl_ctx->drop_flags = 0;
  3228. xhci_dbg(xhci, "Internal device address = %d\n", virt_dev->address);
  3229. return 0;
  3230. }
  3231. #ifdef CONFIG_USB_SUSPEND
  3232. /* BESL to HIRD Encoding array for USB2 LPM */
  3233. static int xhci_besl_encoding[16] = {125, 150, 200, 300, 400, 500, 1000, 2000,
  3234. 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000};
  3235. /* Calculate HIRD/BESL for USB2 PORTPMSC*/
  3236. static int xhci_calculate_hird_besl(struct xhci_hcd *xhci,
  3237. struct usb_device *udev)
  3238. {
  3239. int u2del, besl, besl_host;
  3240. int besl_device = 0;
  3241. u32 field;
  3242. u2del = HCS_U2_LATENCY(xhci->hcs_params3);
  3243. field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
  3244. if (field & USB_BESL_SUPPORT) {
  3245. for (besl_host = 0; besl_host < 16; besl_host++) {
  3246. if (xhci_besl_encoding[besl_host] >= u2del)
  3247. break;
  3248. }
  3249. /* Use baseline BESL value as default */
  3250. if (field & USB_BESL_BASELINE_VALID)
  3251. besl_device = USB_GET_BESL_BASELINE(field);
  3252. else if (field & USB_BESL_DEEP_VALID)
  3253. besl_device = USB_GET_BESL_DEEP(field);
  3254. } else {
  3255. if (u2del <= 50)
  3256. besl_host = 0;
  3257. else
  3258. besl_host = (u2del - 51) / 75 + 1;
  3259. }
  3260. besl = besl_host + besl_device;
  3261. if (besl > 15)
  3262. besl = 15;
  3263. return besl;
  3264. }
  3265. static int xhci_usb2_software_lpm_test(struct usb_hcd *hcd,
  3266. struct usb_device *udev)
  3267. {
  3268. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  3269. struct dev_info *dev_info;
  3270. __le32 __iomem **port_array;
  3271. __le32 __iomem *addr, *pm_addr;
  3272. u32 temp, dev_id;
  3273. unsigned int port_num;
  3274. unsigned long flags;
  3275. int hird;
  3276. int ret;
  3277. if (hcd->speed == HCD_USB3 || !xhci->sw_lpm_support ||
  3278. !udev->lpm_capable)
  3279. return -EINVAL;
  3280. /* we only support lpm for non-hub device connected to root hub yet */
  3281. if (!udev->parent || udev->parent->parent ||
  3282. udev->descriptor.bDeviceClass == USB_CLASS_HUB)
  3283. return -EINVAL;
  3284. spin_lock_irqsave(&xhci->lock, flags);
  3285. /* Look for devices in lpm_failed_devs list */
  3286. dev_id = le16_to_cpu(udev->descriptor.idVendor) << 16 |
  3287. le16_to_cpu(udev->descriptor.idProduct);
  3288. list_for_each_entry(dev_info, &xhci->lpm_failed_devs, list) {
  3289. if (dev_info->dev_id == dev_id) {
  3290. ret = -EINVAL;
  3291. goto finish;
  3292. }
  3293. }
  3294. port_array = xhci->usb2_ports;
  3295. port_num = udev->portnum - 1;
  3296. if (port_num > HCS_MAX_PORTS(xhci->hcs_params1)) {
  3297. xhci_dbg(xhci, "invalid port number %d\n", udev->portnum);
  3298. ret = -EINVAL;
  3299. goto finish;
  3300. }
  3301. /*
  3302. * Test USB 2.0 software LPM.
  3303. * FIXME: some xHCI 1.0 hosts may implement a new register to set up
  3304. * hardware-controlled USB 2.0 LPM. See section 5.4.11 and 4.23.5.1.1.1
  3305. * in the June 2011 errata release.
  3306. */
  3307. xhci_dbg(xhci, "test port %d software LPM\n", port_num);
  3308. /*
  3309. * Set L1 Device Slot and HIRD/BESL.
  3310. * Check device's USB 2.0 extension descriptor to determine whether
  3311. * HIRD or BESL shoule be used. See USB2.0 LPM errata.
  3312. */
  3313. pm_addr = port_array[port_num] + 1;
  3314. hird = xhci_calculate_hird_besl(xhci, udev);
  3315. temp = PORT_L1DS(udev->slot_id) | PORT_HIRD(hird);
  3316. xhci_writel(xhci, temp, pm_addr);
  3317. /* Set port link state to U2(L1) */
  3318. addr = port_array[port_num];
  3319. xhci_set_link_state(xhci, port_array, port_num, XDEV_U2);
  3320. /* wait for ACK */
  3321. spin_unlock_irqrestore(&xhci->lock, flags);
  3322. msleep(10);
  3323. spin_lock_irqsave(&xhci->lock, flags);
  3324. /* Check L1 Status */
  3325. ret = handshake(xhci, pm_addr, PORT_L1S_MASK, PORT_L1S_SUCCESS, 125);
  3326. if (ret != -ETIMEDOUT) {
  3327. /* enter L1 successfully */
  3328. temp = xhci_readl(xhci, addr);
  3329. xhci_dbg(xhci, "port %d entered L1 state, port status 0x%x\n",
  3330. port_num, temp);
  3331. ret = 0;
  3332. } else {
  3333. temp = xhci_readl(xhci, pm_addr);
  3334. xhci_dbg(xhci, "port %d software lpm failed, L1 status %d\n",
  3335. port_num, temp & PORT_L1S_MASK);
  3336. ret = -EINVAL;
  3337. }
  3338. /* Resume the port */
  3339. xhci_set_link_state(xhci, port_array, port_num, XDEV_U0);
  3340. spin_unlock_irqrestore(&xhci->lock, flags);
  3341. msleep(10);
  3342. spin_lock_irqsave(&xhci->lock, flags);
  3343. /* Clear PLC */
  3344. xhci_test_and_clear_bit(xhci, port_array, port_num, PORT_PLC);
  3345. /* Check PORTSC to make sure the device is in the right state */
  3346. if (!ret) {
  3347. temp = xhci_readl(xhci, addr);
  3348. xhci_dbg(xhci, "resumed port %d status 0x%x\n", port_num, temp);
  3349. if (!(temp & PORT_CONNECT) || !(temp & PORT_PE) ||
  3350. (temp & PORT_PLS_MASK) != XDEV_U0) {
  3351. xhci_dbg(xhci, "port L1 resume fail\n");
  3352. ret = -EINVAL;
  3353. }
  3354. }
  3355. if (ret) {
  3356. /* Insert dev to lpm_failed_devs list */
  3357. xhci_warn(xhci, "device LPM test failed, may disconnect and "
  3358. "re-enumerate\n");
  3359. dev_info = kzalloc(sizeof(struct dev_info), GFP_ATOMIC);
  3360. if (!dev_info) {
  3361. ret = -ENOMEM;
  3362. goto finish;
  3363. }
  3364. dev_info->dev_id = dev_id;
  3365. INIT_LIST_HEAD(&dev_info->list);
  3366. list_add(&dev_info->list, &xhci->lpm_failed_devs);
  3367. } else {
  3368. xhci_ring_device(xhci, udev->slot_id);
  3369. }
  3370. finish:
  3371. spin_unlock_irqrestore(&xhci->lock, flags);
  3372. return ret;
  3373. }
  3374. int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
  3375. struct usb_device *udev, int enable)
  3376. {
  3377. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  3378. __le32 __iomem **port_array;
  3379. __le32 __iomem *pm_addr;
  3380. u32 temp;
  3381. unsigned int port_num;
  3382. unsigned long flags;
  3383. int hird;
  3384. if (hcd->speed == HCD_USB3 || !xhci->hw_lpm_support ||
  3385. !udev->lpm_capable)
  3386. return -EPERM;
  3387. if (!udev->parent || udev->parent->parent ||
  3388. udev->descriptor.bDeviceClass == USB_CLASS_HUB)
  3389. return -EPERM;
  3390. if (udev->usb2_hw_lpm_capable != 1)
  3391. return -EPERM;
  3392. spin_lock_irqsave(&xhci->lock, flags);
  3393. port_array = xhci->usb2_ports;
  3394. port_num = udev->portnum - 1;
  3395. pm_addr = port_array[port_num] + 1;
  3396. temp = xhci_readl(xhci, pm_addr);
  3397. xhci_dbg(xhci, "%s port %d USB2 hardware LPM\n",
  3398. enable ? "enable" : "disable", port_num);
  3399. hird = xhci_calculate_hird_besl(xhci, udev);
  3400. if (enable) {
  3401. temp &= ~PORT_HIRD_MASK;
  3402. temp |= PORT_HIRD(hird) | PORT_RWE;
  3403. xhci_writel(xhci, temp, pm_addr);
  3404. temp = xhci_readl(xhci, pm_addr);
  3405. temp |= PORT_HLE;
  3406. xhci_writel(xhci, temp, pm_addr);
  3407. } else {
  3408. temp &= ~(PORT_HLE | PORT_RWE | PORT_HIRD_MASK);
  3409. xhci_writel(xhci, temp, pm_addr);
  3410. }
  3411. spin_unlock_irqrestore(&xhci->lock, flags);
  3412. return 0;
  3413. }
  3414. int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
  3415. {
  3416. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  3417. int ret;
  3418. ret = xhci_usb2_software_lpm_test(hcd, udev);
  3419. if (!ret) {
  3420. xhci_dbg(xhci, "software LPM test succeed\n");
  3421. if (xhci->hw_lpm_support == 1) {
  3422. udev->usb2_hw_lpm_capable = 1;
  3423. ret = xhci_set_usb2_hardware_lpm(hcd, udev, 1);
  3424. if (!ret)
  3425. udev->usb2_hw_lpm_enabled = 1;
  3426. }
  3427. }
  3428. return 0;
  3429. }
  3430. #else
  3431. int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
  3432. struct usb_device *udev, int enable)
  3433. {
  3434. return 0;
  3435. }
  3436. int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
  3437. {
  3438. return 0;
  3439. }
  3440. #endif /* CONFIG_USB_SUSPEND */
  3441. /* Once a hub descriptor is fetched for a device, we need to update the xHC's
  3442. * internal data structures for the device.
  3443. */
  3444. int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
  3445. struct usb_tt *tt, gfp_t mem_flags)
  3446. {
  3447. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  3448. struct xhci_virt_device *vdev;
  3449. struct xhci_command *config_cmd;
  3450. struct xhci_input_control_ctx *ctrl_ctx;
  3451. struct xhci_slot_ctx *slot_ctx;
  3452. unsigned long flags;
  3453. unsigned think_time;
  3454. int ret;
  3455. /* Ignore root hubs */
  3456. if (!hdev->parent)
  3457. return 0;
  3458. vdev = xhci->devs[hdev->slot_id];
  3459. if (!vdev) {
  3460. xhci_warn(xhci, "Cannot update hub desc for unknown device.\n");
  3461. return -EINVAL;
  3462. }
  3463. config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
  3464. if (!config_cmd) {
  3465. xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
  3466. return -ENOMEM;
  3467. }
  3468. spin_lock_irqsave(&xhci->lock, flags);
  3469. if (hdev->speed == USB_SPEED_HIGH &&
  3470. xhci_alloc_tt_info(xhci, vdev, hdev, tt, GFP_ATOMIC)) {
  3471. xhci_dbg(xhci, "Could not allocate xHCI TT structure.\n");
  3472. xhci_free_command(xhci, config_cmd);
  3473. spin_unlock_irqrestore(&xhci->lock, flags);
  3474. return -ENOMEM;
  3475. }
  3476. xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx);
  3477. ctrl_ctx = xhci_get_input_control_ctx(xhci, config_cmd->in_ctx);
  3478. ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
  3479. slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx);
  3480. slot_ctx->dev_info |= cpu_to_le32(DEV_HUB);
  3481. if (tt->multi)
  3482. slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
  3483. if (xhci->hci_version > 0x95) {
  3484. xhci_dbg(xhci, "xHCI version %x needs hub "
  3485. "TT think time and number of ports\n",
  3486. (unsigned int) xhci->hci_version);
  3487. slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(hdev->maxchild));
  3488. /* Set TT think time - convert from ns to FS bit times.
  3489. * 0 = 8 FS bit times, 1 = 16 FS bit times,
  3490. * 2 = 24 FS bit times, 3 = 32 FS bit times.
  3491. *
  3492. * xHCI 1.0: this field shall be 0 if the device is not a
  3493. * High-spped hub.
  3494. */
  3495. think_time = tt->think_time;
  3496. if (think_time != 0)
  3497. think_time = (think_time / 666) - 1;
  3498. if (xhci->hci_version < 0x100 || hdev->speed == USB_SPEED_HIGH)
  3499. slot_ctx->tt_info |=
  3500. cpu_to_le32(TT_THINK_TIME(think_time));
  3501. } else {
  3502. xhci_dbg(xhci, "xHCI version %x doesn't need hub "
  3503. "TT think time or number of ports\n",
  3504. (unsigned int) xhci->hci_version);
  3505. }
  3506. slot_ctx->dev_state = 0;
  3507. spin_unlock_irqrestore(&xhci->lock, flags);
  3508. xhci_dbg(xhci, "Set up %s for hub device.\n",
  3509. (xhci->hci_version > 0x95) ?
  3510. "configure endpoint" : "evaluate context");
  3511. xhci_dbg(xhci, "Slot %u Input Context:\n", hdev->slot_id);
  3512. xhci_dbg_ctx(xhci, config_cmd->in_ctx, 0);
  3513. /* Issue and wait for the configure endpoint or
  3514. * evaluate context command.
  3515. */
  3516. if (xhci->hci_version > 0x95)
  3517. ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
  3518. false, false);
  3519. else
  3520. ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
  3521. true, false);
  3522. xhci_dbg(xhci, "Slot %u Output Context:\n", hdev->slot_id);
  3523. xhci_dbg_ctx(xhci, vdev->out_ctx, 0);
  3524. xhci_free_command(xhci, config_cmd);
  3525. return ret;
  3526. }
  3527. int xhci_get_frame(struct usb_hcd *hcd)
  3528. {
  3529. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  3530. /* EHCI mods by the periodic size. Why? */
  3531. return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
  3532. }
  3533. int xhci_gen_setup(struct usb_hcd *hcd, xhci_get_quirks_t get_quirks)
  3534. {
  3535. struct xhci_hcd *xhci;
  3536. struct device *dev = hcd->self.controller;
  3537. int retval;
  3538. u32 temp;
  3539. /* Accept arbitrarily long scatter-gather lists */
  3540. hcd->self.sg_tablesize = ~0;
  3541. if (usb_hcd_is_primary_hcd(hcd)) {
  3542. xhci = kzalloc(sizeof(struct xhci_hcd), GFP_KERNEL);
  3543. if (!xhci)
  3544. return -ENOMEM;
  3545. *((struct xhci_hcd **) hcd->hcd_priv) = xhci;
  3546. xhci->main_hcd = hcd;
  3547. /* Mark the first roothub as being USB 2.0.
  3548. * The xHCI driver will register the USB 3.0 roothub.
  3549. */
  3550. hcd->speed = HCD_USB2;
  3551. hcd->self.root_hub->speed = USB_SPEED_HIGH;
  3552. /*
  3553. * USB 2.0 roothub under xHCI has an integrated TT,
  3554. * (rate matching hub) as opposed to having an OHCI/UHCI
  3555. * companion controller.
  3556. */
  3557. hcd->has_tt = 1;
  3558. } else {
  3559. /* xHCI private pointer was set in xhci_pci_probe for the second
  3560. * registered roothub.
  3561. */
  3562. xhci = hcd_to_xhci(hcd);
  3563. temp = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
  3564. if (HCC_64BIT_ADDR(temp)) {
  3565. xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n");
  3566. dma_set_mask(hcd->self.controller, DMA_BIT_MASK(64));
  3567. } else {
  3568. dma_set_mask(hcd->self.controller, DMA_BIT_MASK(32));
  3569. }
  3570. return 0;
  3571. }
  3572. xhci->cap_regs = hcd->regs;
  3573. xhci->op_regs = hcd->regs +
  3574. HC_LENGTH(xhci_readl(xhci, &xhci->cap_regs->hc_capbase));
  3575. xhci->run_regs = hcd->regs +
  3576. (xhci_readl(xhci, &xhci->cap_regs->run_regs_off) & RTSOFF_MASK);
  3577. /* Cache read-only capability registers */
  3578. xhci->hcs_params1 = xhci_readl(xhci, &xhci->cap_regs->hcs_params1);
  3579. xhci->hcs_params2 = xhci_readl(xhci, &xhci->cap_regs->hcs_params2);
  3580. xhci->hcs_params3 = xhci_readl(xhci, &xhci->cap_regs->hcs_params3);
  3581. xhci->hcc_params = xhci_readl(xhci, &xhci->cap_regs->hc_capbase);
  3582. xhci->hci_version = HC_VERSION(xhci->hcc_params);
  3583. xhci->hcc_params = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
  3584. xhci_print_registers(xhci);
  3585. get_quirks(dev, xhci);
  3586. /* Make sure the HC is halted. */
  3587. retval = xhci_halt(xhci);
  3588. if (retval)
  3589. goto error;
  3590. xhci_dbg(xhci, "Resetting HCD\n");
  3591. /* Reset the internal HC memory state and registers. */
  3592. retval = xhci_reset(xhci);
  3593. if (retval)
  3594. goto error;
  3595. xhci_dbg(xhci, "Reset complete\n");
  3596. temp = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
  3597. if (HCC_64BIT_ADDR(temp)) {
  3598. xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n");
  3599. dma_set_mask(hcd->self.controller, DMA_BIT_MASK(64));
  3600. } else {
  3601. dma_set_mask(hcd->self.controller, DMA_BIT_MASK(32));
  3602. }
  3603. xhci_dbg(xhci, "Calling HCD init\n");
  3604. /* Initialize HCD and host controller data structures. */
  3605. retval = xhci_init(hcd);
  3606. if (retval)
  3607. goto error;
  3608. xhci_dbg(xhci, "Called HCD init\n");
  3609. return 0;
  3610. error:
  3611. kfree(xhci);
  3612. return retval;
  3613. }
  3614. MODULE_DESCRIPTION(DRIVER_DESC);
  3615. MODULE_AUTHOR(DRIVER_AUTHOR);
  3616. MODULE_LICENSE("GPL");
  3617. static int __init xhci_hcd_init(void)
  3618. {
  3619. int retval;
  3620. retval = xhci_register_pci();
  3621. if (retval < 0) {
  3622. printk(KERN_DEBUG "Problem registering PCI driver.");
  3623. return retval;
  3624. }
  3625. retval = xhci_register_plat();
  3626. if (retval < 0) {
  3627. printk(KERN_DEBUG "Problem registering platform driver.");
  3628. goto unreg_pci;
  3629. }
  3630. /*
  3631. * Check the compiler generated sizes of structures that must be laid
  3632. * out in specific ways for hardware access.
  3633. */
  3634. BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
  3635. BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
  3636. BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
  3637. /* xhci_device_control has eight fields, and also
  3638. * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
  3639. */
  3640. BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
  3641. BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
  3642. BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
  3643. BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 7*32/8);
  3644. BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
  3645. /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
  3646. BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
  3647. BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
  3648. return 0;
  3649. unreg_pci:
  3650. xhci_unregister_pci();
  3651. return retval;
  3652. }
  3653. module_init(xhci_hcd_init);
  3654. static void __exit xhci_hcd_cleanup(void)
  3655. {
  3656. xhci_unregister_pci();
  3657. xhci_unregister_plat();
  3658. }
  3659. module_exit(xhci_hcd_cleanup);