xhci.c 122 KB

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