xhci.c 123 KB

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