xhci.c 122 KB

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