xhci.c 122 KB

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