xhci.c 123 KB

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