You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xhci.c 77KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009
  1. /*
  2. * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <unistd.h>
  23. #include <string.h>
  24. #include <strings.h>
  25. #include <errno.h>
  26. #include <byteswap.h>
  27. #include <ipxe/malloc.h>
  28. #include <ipxe/umalloc.h>
  29. #include <ipxe/pci.h>
  30. #include <ipxe/usb.h>
  31. #include <ipxe/profile.h>
  32. #include "xhci.h"
  33. /** @file
  34. *
  35. * USB eXtensible Host Controller Interface (xHCI) driver
  36. *
  37. */
  38. /** Message transfer profiler */
  39. static struct profiler xhci_message_profiler __profiler =
  40. { .name = "xhci.message" };
  41. /** Stream transfer profiler */
  42. static struct profiler xhci_stream_profiler __profiler =
  43. { .name = "xhci.stream" };
  44. /** Event ring profiler */
  45. static struct profiler xhci_event_profiler __profiler =
  46. { .name = "xhci.event" };
  47. /** Transfer event profiler */
  48. static struct profiler xhci_transfer_profiler __profiler =
  49. { .name = "xhci.transfer" };
  50. /* Disambiguate the various error causes */
  51. #define EIO_DATA \
  52. __einfo_error ( EINFO_EIO_DATA )
  53. #define EINFO_EIO_DATA \
  54. __einfo_uniqify ( EINFO_EIO, ( 2 - 0 ), \
  55. "Data buffer error" )
  56. #define EIO_BABBLE \
  57. __einfo_error ( EINFO_EIO_BABBLE )
  58. #define EINFO_EIO_BABBLE \
  59. __einfo_uniqify ( EINFO_EIO, ( 3 - 0 ), \
  60. "Babble detected" )
  61. #define EIO_USB \
  62. __einfo_error ( EINFO_EIO_USB )
  63. #define EINFO_EIO_USB \
  64. __einfo_uniqify ( EINFO_EIO, ( 4 - 0 ), \
  65. "USB transaction error" )
  66. #define EIO_TRB \
  67. __einfo_error ( EINFO_EIO_TRB )
  68. #define EINFO_EIO_TRB \
  69. __einfo_uniqify ( EINFO_EIO, ( 5 - 0 ), \
  70. "TRB error" )
  71. #define EIO_STALL \
  72. __einfo_error ( EINFO_EIO_STALL )
  73. #define EINFO_EIO_STALL \
  74. __einfo_uniqify ( EINFO_EIO, ( 6 - 0 ), \
  75. "Stall error" )
  76. #define EIO_RESOURCE \
  77. __einfo_error ( EINFO_EIO_RESOURCE )
  78. #define EINFO_EIO_RESOURCE \
  79. __einfo_uniqify ( EINFO_EIO, ( 7 - 0 ), \
  80. "Resource error" )
  81. #define EIO_BANDWIDTH \
  82. __einfo_error ( EINFO_EIO_BANDWIDTH )
  83. #define EINFO_EIO_BANDWIDTH \
  84. __einfo_uniqify ( EINFO_EIO, ( 8 - 0 ), \
  85. "Bandwidth error" )
  86. #define EIO_NO_SLOTS \
  87. __einfo_error ( EINFO_EIO_NO_SLOTS )
  88. #define EINFO_EIO_NO_SLOTS \
  89. __einfo_uniqify ( EINFO_EIO, ( 9 - 0 ), \
  90. "No slots available" )
  91. #define EIO_STREAM_TYPE \
  92. __einfo_error ( EINFO_EIO_STREAM_TYPE )
  93. #define EINFO_EIO_STREAM_TYPE \
  94. __einfo_uniqify ( EINFO_EIO, ( 10 - 0 ), \
  95. "Invalid stream type" )
  96. #define EIO_SLOT \
  97. __einfo_error ( EINFO_EIO_SLOT )
  98. #define EINFO_EIO_SLOT \
  99. __einfo_uniqify ( EINFO_EIO, ( 11 - 0 ), \
  100. "Slot not enabled" )
  101. #define EIO_ENDPOINT \
  102. __einfo_error ( EINFO_EIO_ENDPOINT )
  103. #define EINFO_EIO_ENDPOINT \
  104. __einfo_uniqify ( EINFO_EIO, ( 12 - 0 ), \
  105. "Endpoint not enabled" )
  106. #define EIO_SHORT \
  107. __einfo_error ( EINFO_EIO_SHORT )
  108. #define EINFO_EIO_SHORT \
  109. __einfo_uniqify ( EINFO_EIO, ( 13 - 0 ), \
  110. "Short packet" )
  111. #define EIO_UNDERRUN \
  112. __einfo_error ( EINFO_EIO_UNDERRUN )
  113. #define EINFO_EIO_UNDERRUN \
  114. __einfo_uniqify ( EINFO_EIO, ( 14 - 0 ), \
  115. "Ring underrun" )
  116. #define EIO_OVERRUN \
  117. __einfo_error ( EINFO_EIO_OVERRUN )
  118. #define EINFO_EIO_OVERRUN \
  119. __einfo_uniqify ( EINFO_EIO, ( 15 - 0 ), \
  120. "Ring overrun" )
  121. #define EIO_VF_RING_FULL \
  122. __einfo_error ( EINFO_EIO_VF_RING_FULL )
  123. #define EINFO_EIO_VF_RING_FULL \
  124. __einfo_uniqify ( EINFO_EIO, ( 16 - 0 ), \
  125. "Virtual function event ring full" )
  126. #define EIO_PARAMETER \
  127. __einfo_error ( EINFO_EIO_PARAMETER )
  128. #define EINFO_EIO_PARAMETER \
  129. __einfo_uniqify ( EINFO_EIO, ( 17 - 0 ), \
  130. "Parameter error" )
  131. #define EIO_BANDWIDTH_OVERRUN \
  132. __einfo_error ( EINFO_EIO_BANDWIDTH_OVERRUN )
  133. #define EINFO_EIO_BANDWIDTH_OVERRUN \
  134. __einfo_uniqify ( EINFO_EIO, ( 18 - 0 ), \
  135. "Bandwidth overrun" )
  136. #define EIO_CONTEXT \
  137. __einfo_error ( EINFO_EIO_CONTEXT )
  138. #define EINFO_EIO_CONTEXT \
  139. __einfo_uniqify ( EINFO_EIO, ( 19 - 0 ), \
  140. "Context state error" )
  141. #define EIO_NO_PING \
  142. __einfo_error ( EINFO_EIO_NO_PING )
  143. #define EINFO_EIO_NO_PING \
  144. __einfo_uniqify ( EINFO_EIO, ( 20 - 0 ), \
  145. "No ping response" )
  146. #define EIO_RING_FULL \
  147. __einfo_error ( EINFO_EIO_RING_FULL )
  148. #define EINFO_EIO_RING_FULL \
  149. __einfo_uniqify ( EINFO_EIO, ( 21 - 0 ), \
  150. "Event ring full" )
  151. #define EIO_INCOMPATIBLE \
  152. __einfo_error ( EINFO_EIO_INCOMPATIBLE )
  153. #define EINFO_EIO_INCOMPATIBLE \
  154. __einfo_uniqify ( EINFO_EIO, ( 22 - 0 ), \
  155. "Incompatible device" )
  156. #define EIO_MISSED \
  157. __einfo_error ( EINFO_EIO_MISSED )
  158. #define EINFO_EIO_MISSED \
  159. __einfo_uniqify ( EINFO_EIO, ( 23 - 0 ), \
  160. "Missed service error" )
  161. #define EIO_CMD_STOPPED \
  162. __einfo_error ( EINFO_EIO_CMD_STOPPED )
  163. #define EINFO_EIO_CMD_STOPPED \
  164. __einfo_uniqify ( EINFO_EIO, ( 24 - 0 ), \
  165. "Command ring stopped" )
  166. #define EIO_CMD_ABORTED \
  167. __einfo_error ( EINFO_EIO_CMD_ABORTED )
  168. #define EINFO_EIO_CMD_ABORTED \
  169. __einfo_uniqify ( EINFO_EIO, ( 25 - 0 ), \
  170. "Command aborted" )
  171. #define EIO_STOP \
  172. __einfo_error ( EINFO_EIO_STOP )
  173. #define EINFO_EIO_STOP \
  174. __einfo_uniqify ( EINFO_EIO, ( 26 - 0 ), \
  175. "Stopped" )
  176. #define EIO_STOP_LEN \
  177. __einfo_error ( EINFO_EIO_STOP_LEN )
  178. #define EINFO_EIO_STOP_LEN \
  179. __einfo_uniqify ( EINFO_EIO, ( 27 - 0 ), \
  180. "Stopped - length invalid" )
  181. #define EIO_STOP_SHORT \
  182. __einfo_error ( EINFO_EIO_STOP_SHORT )
  183. #define EINFO_EIO_STOP_SHORT \
  184. __einfo_uniqify ( EINFO_EIO, ( 28 - 0 ), \
  185. "Stopped - short packet" )
  186. #define EIO_LATENCY \
  187. __einfo_error ( EINFO_EIO_LATENCY )
  188. #define EINFO_EIO_LATENCY \
  189. __einfo_uniqify ( EINFO_EIO, ( 29 - 0 ), \
  190. "Maximum exit latency too large" )
  191. #define EIO_ISOCH \
  192. __einfo_error ( EINFO_EIO_ISOCH )
  193. #define EINFO_EIO_ISOCH \
  194. __einfo_uniqify ( EINFO_EIO, ( 31 - 0 ), \
  195. "Isochronous buffer overrun" )
  196. #define EPROTO_LOST \
  197. __einfo_error ( EINFO_EPROTO_LOST )
  198. #define EINFO_EPROTO_LOST \
  199. __einfo_uniqify ( EINFO_EPROTO, ( 32 - 32 ), \
  200. "Event lost" )
  201. #define EPROTO_UNDEFINED \
  202. __einfo_error ( EINFO_EPROTO_UNDEFINED )
  203. #define EINFO_EPROTO_UNDEFINED \
  204. __einfo_uniqify ( EINFO_EPROTO, ( 33 - 32 ), \
  205. "Undefined error" )
  206. #define EPROTO_STREAM_ID \
  207. __einfo_error ( EINFO_EPROTO_STREAM_ID )
  208. #define EINFO_EPROTO_STREAM_ID \
  209. __einfo_uniqify ( EINFO_EPROTO, ( 34 - 32 ), \
  210. "Invalid stream ID" )
  211. #define EPROTO_SECONDARY \
  212. __einfo_error ( EINFO_EPROTO_SECONDARY )
  213. #define EINFO_EPROTO_SECONDARY \
  214. __einfo_uniqify ( EINFO_EPROTO, ( 35 - 32 ), \
  215. "Secondary bandwidth error" )
  216. #define EPROTO_SPLIT \
  217. __einfo_error ( EINFO_EPROTO_SPLIT )
  218. #define EINFO_EPROTO_SPLIT \
  219. __einfo_uniqify ( EINFO_EPROTO, ( 36 - 32 ), \
  220. "Split transaction error" )
  221. #define ECODE(code) \
  222. ( ( (code) < 32 ) ? \
  223. EUNIQ ( EINFO_EIO, ( (code) & 31 ), EIO_DATA, EIO_BABBLE, \
  224. EIO_USB, EIO_TRB, EIO_STALL, EIO_RESOURCE, \
  225. EIO_BANDWIDTH, EIO_NO_SLOTS, EIO_STREAM_TYPE, \
  226. EIO_SLOT, EIO_ENDPOINT, EIO_SHORT, EIO_UNDERRUN, \
  227. EIO_OVERRUN, EIO_VF_RING_FULL, EIO_PARAMETER, \
  228. EIO_BANDWIDTH_OVERRUN, EIO_CONTEXT, EIO_NO_PING, \
  229. EIO_RING_FULL, EIO_INCOMPATIBLE, EIO_MISSED, \
  230. EIO_CMD_STOPPED, EIO_CMD_ABORTED, EIO_STOP, \
  231. EIO_STOP_LEN, EIO_STOP_SHORT, EIO_LATENCY, \
  232. EIO_ISOCH ) : \
  233. ( (code) < 64 ) ? \
  234. EUNIQ ( EINFO_EPROTO, ( (code) & 31 ), EPROTO_LOST, \
  235. EPROTO_UNDEFINED, EPROTO_STREAM_ID, \
  236. EPROTO_SECONDARY, EPROTO_SPLIT ) : \
  237. EFAULT )
  238. /******************************************************************************
  239. *
  240. * Register access
  241. *
  242. ******************************************************************************
  243. */
  244. /**
  245. * Initialise device
  246. *
  247. * @v xhci xHCI device
  248. * @v regs MMIO registers
  249. */
  250. static void xhci_init ( struct xhci_device *xhci, void *regs ) {
  251. uint32_t hcsparams1;
  252. uint32_t hcsparams2;
  253. uint32_t hccparams1;
  254. uint32_t pagesize;
  255. size_t caplength;
  256. size_t rtsoff;
  257. size_t dboff;
  258. /* Locate capability, operational, runtime, and doorbell registers */
  259. xhci->cap = regs;
  260. caplength = readb ( xhci->cap + XHCI_CAP_CAPLENGTH );
  261. rtsoff = readl ( xhci->cap + XHCI_CAP_RTSOFF );
  262. dboff = readl ( xhci->cap + XHCI_CAP_DBOFF );
  263. xhci->op = ( xhci->cap + caplength );
  264. xhci->run = ( xhci->cap + rtsoff );
  265. xhci->db = ( xhci->cap + dboff );
  266. DBGC2 ( xhci, "XHCI %p cap %08lx op %08lx run %08lx db %08lx\n",
  267. xhci, virt_to_phys ( xhci->cap ), virt_to_phys ( xhci->op ),
  268. virt_to_phys ( xhci->run ), virt_to_phys ( xhci->db ) );
  269. /* Read structural parameters 1 */
  270. hcsparams1 = readl ( xhci->cap + XHCI_CAP_HCSPARAMS1 );
  271. xhci->slots = XHCI_HCSPARAMS1_SLOTS ( hcsparams1 );
  272. xhci->intrs = XHCI_HCSPARAMS1_INTRS ( hcsparams1 );
  273. xhci->ports = XHCI_HCSPARAMS1_PORTS ( hcsparams1 );
  274. DBGC ( xhci, "XHCI %p has %d slots %d intrs %d ports\n",
  275. xhci, xhci->slots, xhci->intrs, xhci->ports );
  276. /* Read structural parameters 2 */
  277. hcsparams2 = readl ( xhci->cap + XHCI_CAP_HCSPARAMS2 );
  278. xhci->scratchpads = XHCI_HCSPARAMS2_SCRATCHPADS ( hcsparams2 );
  279. DBGC2 ( xhci, "XHCI %p needs %d scratchpads\n",
  280. xhci, xhci->scratchpads );
  281. /* Read capability parameters 1 */
  282. hccparams1 = readl ( xhci->cap + XHCI_CAP_HCCPARAMS1 );
  283. xhci->addr64 = XHCI_HCCPARAMS1_ADDR64 ( hccparams1 );
  284. xhci->csz_shift = XHCI_HCCPARAMS1_CSZ_SHIFT ( hccparams1 );
  285. xhci->xecp = XHCI_HCCPARAMS1_XECP ( hccparams1 );
  286. /* Read page size */
  287. pagesize = readl ( xhci->op + XHCI_OP_PAGESIZE );
  288. xhci->pagesize = XHCI_PAGESIZE ( pagesize );
  289. assert ( xhci->pagesize != 0 );
  290. assert ( ( ( xhci->pagesize ) & ( xhci->pagesize - 1 ) ) == 0 );
  291. DBGC2 ( xhci, "XHCI %p page size %zd bytes\n",
  292. xhci, xhci->pagesize );
  293. }
  294. /**
  295. * Find extended capability
  296. *
  297. * @v xhci xHCI device
  298. * @v id Capability ID
  299. * @v offset Offset to previous extended capability instance, or zero
  300. * @ret offset Offset to extended capability, or zero if not found
  301. */
  302. static unsigned int xhci_extended_capability ( struct xhci_device *xhci,
  303. unsigned int id,
  304. unsigned int offset ) {
  305. uint32_t xecp;
  306. unsigned int next;
  307. /* Locate the extended capability */
  308. while ( 1 ) {
  309. /* Locate first or next capability as applicable */
  310. if ( offset ) {
  311. xecp = readl ( xhci->cap + offset );
  312. next = XHCI_XECP_NEXT ( xecp );
  313. } else {
  314. next = xhci->xecp;
  315. }
  316. if ( ! next )
  317. return 0;
  318. offset += next;
  319. /* Check if this is the requested capability */
  320. xecp = readl ( xhci->cap + offset );
  321. if ( XHCI_XECP_ID ( xecp ) == id )
  322. return offset;
  323. }
  324. }
  325. /**
  326. * Write potentially 64-bit register
  327. *
  328. * @v xhci xHCI device
  329. * @v value Value
  330. * @v reg Register address
  331. * @ret rc Return status code
  332. */
  333. static inline __attribute__ (( always_inline )) int
  334. xhci_writeq ( struct xhci_device *xhci, physaddr_t value, void *reg ) {
  335. /* If this is a 32-bit build, then this can never fail
  336. * (allowing the compiler to optimise out the error path).
  337. */
  338. if ( sizeof ( value ) <= sizeof ( uint32_t ) ) {
  339. writel ( value, reg );
  340. writel ( 0, ( reg + sizeof ( uint32_t ) ) );
  341. return 0;
  342. }
  343. /* If the device does not support 64-bit addresses and this
  344. * address is outside the 32-bit address space, then fail.
  345. */
  346. if ( ( value & ~0xffffffffULL ) && ! xhci->addr64 ) {
  347. DBGC ( xhci, "XHCI %p cannot access address %lx\n",
  348. xhci, value );
  349. return -ENOTSUP;
  350. }
  351. /* If this is a 64-bit build, then writeq() is available */
  352. writeq ( value, reg );
  353. return 0;
  354. }
  355. /**
  356. * Calculate buffer alignment
  357. *
  358. * @v len Length
  359. * @ret align Buffer alignment
  360. *
  361. * Determine alignment required for a buffer which must be aligned to
  362. * at least XHCI_MIN_ALIGN and which must not cross a page boundary.
  363. */
  364. static inline size_t xhci_align ( size_t len ) {
  365. size_t align;
  366. /* Align to own length (rounded up to a power of two) */
  367. align = ( 1 << fls ( len - 1 ) );
  368. /* Round up to XHCI_MIN_ALIGN if needed */
  369. if ( align < XHCI_MIN_ALIGN )
  370. align = XHCI_MIN_ALIGN;
  371. return align;
  372. }
  373. /**
  374. * Calculate device context offset
  375. *
  376. * @v xhci xHCI device
  377. * @v ctx Context index
  378. */
  379. static inline size_t xhci_device_context_offset ( struct xhci_device *xhci,
  380. unsigned int ctx ) {
  381. return ( XHCI_DCI ( ctx ) << xhci->csz_shift );
  382. }
  383. /**
  384. * Calculate input context offset
  385. *
  386. * @v xhci xHCI device
  387. * @v ctx Context index
  388. */
  389. static inline size_t xhci_input_context_offset ( struct xhci_device *xhci,
  390. unsigned int ctx ) {
  391. return ( XHCI_ICI ( ctx ) << xhci->csz_shift );
  392. }
  393. /******************************************************************************
  394. *
  395. * Diagnostics
  396. *
  397. ******************************************************************************
  398. */
  399. /**
  400. * Dump host controller registers
  401. *
  402. * @v xhci xHCI device
  403. */
  404. static inline void xhci_dump ( struct xhci_device *xhci ) {
  405. uint32_t usbcmd;
  406. uint32_t usbsts;
  407. uint32_t pagesize;
  408. uint32_t dnctrl;
  409. uint32_t config;
  410. /* Do nothing unless debugging is enabled */
  411. if ( ! DBG_LOG )
  412. return;
  413. /* Dump USBCMD */
  414. usbcmd = readl ( xhci->op + XHCI_OP_USBCMD );
  415. DBGC ( xhci, "XHCI %p USBCMD %08x%s%s\n", xhci, usbcmd,
  416. ( ( usbcmd & XHCI_USBCMD_RUN ) ? " run" : "" ),
  417. ( ( usbcmd & XHCI_USBCMD_HCRST ) ? " hcrst" : "" ) );
  418. /* Dump USBSTS */
  419. usbsts = readl ( xhci->op + XHCI_OP_USBSTS );
  420. DBGC ( xhci, "XHCI %p USBSTS %08x%s\n", xhci, usbsts,
  421. ( ( usbsts & XHCI_USBSTS_HCH ) ? " hch" : "" ) );
  422. /* Dump PAGESIZE */
  423. pagesize = readl ( xhci->op + XHCI_OP_PAGESIZE );
  424. DBGC ( xhci, "XHCI %p PAGESIZE %08x\n", xhci, pagesize );
  425. /* Dump DNCTRL */
  426. dnctrl = readl ( xhci->op + XHCI_OP_DNCTRL );
  427. DBGC ( xhci, "XHCI %p DNCTRL %08x\n", xhci, dnctrl );
  428. /* Dump CONFIG */
  429. config = readl ( xhci->op + XHCI_OP_CONFIG );
  430. DBGC ( xhci, "XHCI %p CONFIG %08x\n", xhci, config );
  431. }
  432. /**
  433. * Dump port registers
  434. *
  435. * @v xhci xHCI device
  436. * @v port Port number
  437. */
  438. static inline void xhci_dump_port ( struct xhci_device *xhci,
  439. unsigned int port ) {
  440. uint32_t portsc;
  441. uint32_t portpmsc;
  442. uint32_t portli;
  443. uint32_t porthlpmc;
  444. /* Do nothing unless debugging is enabled */
  445. if ( ! DBG_LOG )
  446. return;
  447. /* Dump PORTSC */
  448. portsc = readl ( xhci->op + XHCI_OP_PORTSC ( port ) );
  449. DBGC ( xhci, "XHCI %p port %d PORTSC %08x%s%s%s%s psiv=%d\n",
  450. xhci, port, portsc,
  451. ( ( portsc & XHCI_PORTSC_CCS ) ? " ccs" : "" ),
  452. ( ( portsc & XHCI_PORTSC_PED ) ? " ped" : "" ),
  453. ( ( portsc & XHCI_PORTSC_PR ) ? " pr" : "" ),
  454. ( ( portsc & XHCI_PORTSC_PP ) ? " pp" : "" ),
  455. XHCI_PORTSC_PSIV ( portsc ) );
  456. /* Dump PORTPMSC */
  457. portpmsc = readl ( xhci->op + XHCI_OP_PORTPMSC ( port ) );
  458. DBGC ( xhci, "XHCI %p port %d PORTPMSC %08x\n", xhci, port, portpmsc );
  459. /* Dump PORTLI */
  460. portli = readl ( xhci->op + XHCI_OP_PORTLI ( port ) );
  461. DBGC ( xhci, "XHCI %p port %d PORTLI %08x\n", xhci, port, portli );
  462. /* Dump PORTHLPMC */
  463. porthlpmc = readl ( xhci->op + XHCI_OP_PORTHLPMC ( port ) );
  464. DBGC ( xhci, "XHCI %p port %d PORTHLPMC %08x\n",
  465. xhci, port, porthlpmc );
  466. }
  467. /******************************************************************************
  468. *
  469. * USB legacy support
  470. *
  471. ******************************************************************************
  472. */
  473. /**
  474. * Initialise USB legacy support
  475. *
  476. * @v xhci xHCI device
  477. */
  478. static void xhci_legacy_init ( struct xhci_device *xhci ) {
  479. unsigned int legacy;
  480. uint8_t bios;
  481. /* Locate USB legacy support capability (if present) */
  482. legacy = xhci_extended_capability ( xhci, XHCI_XECP_ID_LEGACY, 0 );
  483. if ( ! legacy ) {
  484. /* Not an error; capability may not be present */
  485. DBGC ( xhci, "XHCI %p has no USB legacy support capability\n",
  486. xhci );
  487. return;
  488. }
  489. /* Check if legacy USB support is enabled */
  490. bios = readb ( xhci->cap + legacy + XHCI_USBLEGSUP_BIOS );
  491. if ( ! ( bios & XHCI_USBLEGSUP_BIOS_OWNED ) ) {
  492. /* Not an error; already owned by OS */
  493. DBGC ( xhci, "XHCI %p USB legacy support already disabled\n",
  494. xhci );
  495. return;
  496. }
  497. /* Record presence of USB legacy support capability */
  498. xhci->legacy = legacy;
  499. }
  500. /**
  501. * Claim ownership from BIOS
  502. *
  503. * @v xhci xHCI device
  504. * @ret rc Return status code
  505. */
  506. static int xhci_legacy_claim ( struct xhci_device *xhci ) {
  507. uint32_t ctlsts;
  508. uint8_t bios;
  509. unsigned int i;
  510. /* Do nothing unless legacy support capability is present */
  511. if ( ! xhci->legacy )
  512. return 0;
  513. /* Claim ownership */
  514. writeb ( XHCI_USBLEGSUP_OS_OWNED,
  515. xhci->cap + xhci->legacy + XHCI_USBLEGSUP_OS );
  516. /* Wait for BIOS to release ownership */
  517. for ( i = 0 ; i < XHCI_USBLEGSUP_MAX_WAIT_MS ; i++ ) {
  518. /* Check if BIOS has released ownership */
  519. bios = readb ( xhci->cap + xhci->legacy + XHCI_USBLEGSUP_BIOS );
  520. if ( ! ( bios & XHCI_USBLEGSUP_BIOS_OWNED ) ) {
  521. DBGC ( xhci, "XHCI %p claimed ownership from BIOS\n",
  522. xhci );
  523. ctlsts = readl ( xhci->cap + xhci->legacy +
  524. XHCI_USBLEGSUP_CTLSTS );
  525. if ( ctlsts ) {
  526. DBGC ( xhci, "XHCI %p warning: BIOS retained "
  527. "SMIs: %08x\n", xhci, ctlsts );
  528. }
  529. return 0;
  530. }
  531. /* Delay */
  532. mdelay ( 1 );
  533. }
  534. DBGC ( xhci, "XHCI %p timed out waiting for BIOS to release "
  535. "ownership\n", xhci );
  536. return -ETIMEDOUT;
  537. }
  538. /**
  539. * Release ownership back to BIOS
  540. *
  541. * @v xhci xHCI device
  542. */
  543. static void xhci_legacy_release ( struct xhci_device *xhci ) {
  544. /* Do nothing unless legacy support capability is present */
  545. if ( ! xhci->legacy )
  546. return;
  547. /* Release ownership */
  548. writeb ( 0, xhci->cap + xhci->legacy + XHCI_USBLEGSUP_OS );
  549. DBGC ( xhci, "XHCI %p released ownership to BIOS\n", xhci );
  550. }
  551. /******************************************************************************
  552. *
  553. * Supported protocols
  554. *
  555. ******************************************************************************
  556. */
  557. /**
  558. * Transcribe port speed (for debugging)
  559. *
  560. * @v psi Protocol speed ID
  561. * @ret speed Transcribed speed
  562. */
  563. static inline const char * xhci_speed_name ( uint32_t psi ) {
  564. static const char *exponents[4] = { "", "k", "M", "G" };
  565. static char buf[ 10 /* "xxxxxXbps" + NUL */ ];
  566. unsigned int mantissa;
  567. unsigned int exponent;
  568. /* Extract mantissa and exponent */
  569. mantissa = XHCI_SUPPORTED_PSI_MANTISSA ( psi );
  570. exponent = XHCI_SUPPORTED_PSI_EXPONENT ( psi );
  571. /* Transcribe speed */
  572. snprintf ( buf, sizeof ( buf ), "%d%sbps",
  573. mantissa, exponents[exponent] );
  574. return buf;
  575. }
  576. /**
  577. * Find supported protocol extended capability for a port
  578. *
  579. * @v xhci xHCI device
  580. * @v port Port number
  581. * @ret supported Offset to extended capability, or zero if not found
  582. */
  583. static unsigned int xhci_supported_protocol ( struct xhci_device *xhci,
  584. unsigned int port ) {
  585. unsigned int supported = 0;
  586. unsigned int offset;
  587. unsigned int count;
  588. uint32_t ports;
  589. /* Iterate over all supported protocol structures */
  590. while ( ( supported = xhci_extended_capability ( xhci,
  591. XHCI_XECP_ID_SUPPORTED,
  592. supported ) ) ) {
  593. /* Determine port range */
  594. ports = readl ( xhci->cap + supported + XHCI_SUPPORTED_PORTS );
  595. offset = XHCI_SUPPORTED_PORTS_OFFSET ( ports );
  596. count = XHCI_SUPPORTED_PORTS_COUNT ( ports );
  597. /* Check if port lies within this range */
  598. if ( ( port - offset ) < count )
  599. return supported;
  600. }
  601. DBGC ( xhci, "XHCI %p port %d has no supported protocol\n",
  602. xhci, port );
  603. return 0;
  604. }
  605. /**
  606. * Find port protocol
  607. *
  608. * @v xhci xHCI device
  609. * @v port Port number
  610. * @ret protocol USB protocol, or zero if not found
  611. */
  612. static unsigned int xhci_port_protocol ( struct xhci_device *xhci,
  613. unsigned int port ) {
  614. unsigned int supported = xhci_supported_protocol ( xhci, port );
  615. union {
  616. uint32_t raw;
  617. char text[5];
  618. } name;
  619. unsigned int protocol;
  620. unsigned int type;
  621. unsigned int psic;
  622. unsigned int psiv;
  623. unsigned int i;
  624. uint32_t revision;
  625. uint32_t ports;
  626. uint32_t slot;
  627. uint32_t psi;
  628. /* Fail if there is no supported protocol */
  629. if ( ! supported )
  630. return 0;
  631. /* Determine protocol version */
  632. revision = readl ( xhci->cap + supported + XHCI_SUPPORTED_REVISION );
  633. protocol = XHCI_SUPPORTED_REVISION_VER ( revision );
  634. /* Describe port protocol */
  635. if ( DBG_EXTRA ) {
  636. name.raw = cpu_to_le32 ( readl ( xhci->cap + supported +
  637. XHCI_SUPPORTED_NAME ) );
  638. name.text[4] = '\0';
  639. slot = readl ( xhci->cap + supported + XHCI_SUPPORTED_SLOT );
  640. type = XHCI_SUPPORTED_SLOT_TYPE ( slot );
  641. DBGC2 ( xhci, "XHCI %p port %d %sv%04x type %d",
  642. xhci, port, name.text, protocol, type );
  643. ports = readl ( xhci->cap + supported + XHCI_SUPPORTED_PORTS );
  644. psic = XHCI_SUPPORTED_PORTS_PSIC ( ports );
  645. if ( psic ) {
  646. DBGC2 ( xhci, " speeds" );
  647. for ( i = 0 ; i < psic ; i++ ) {
  648. psi = readl ( xhci->cap + supported +
  649. XHCI_SUPPORTED_PSI ( i ) );
  650. psiv = XHCI_SUPPORTED_PSI_VALUE ( psi );
  651. DBGC2 ( xhci, " %d:%s", psiv,
  652. xhci_speed_name ( psi ) );
  653. }
  654. }
  655. DBGC2 ( xhci, "\n" );
  656. }
  657. return protocol;
  658. }
  659. /**
  660. * Find port slot type
  661. *
  662. * @v xhci xHCI device
  663. * @v port Port number
  664. * @ret type Slot type, or negative error
  665. */
  666. static int xhci_port_slot_type ( struct xhci_device *xhci, unsigned int port ) {
  667. unsigned int supported = xhci_supported_protocol ( xhci, port );
  668. unsigned int type;
  669. uint32_t slot;
  670. /* Fail if there is no supported protocol */
  671. if ( ! supported )
  672. return -ENOTSUP;
  673. /* Get slot type */
  674. slot = readl ( xhci->cap + supported + XHCI_SUPPORTED_SLOT );
  675. type = XHCI_SUPPORTED_SLOT_TYPE ( slot );
  676. return type;
  677. }
  678. /**
  679. * Find port speed
  680. *
  681. * @v xhci xHCI device
  682. * @v port Port number
  683. * @v psiv Protocol speed ID value
  684. * @ret speed Port speed, or negative error
  685. */
  686. static int xhci_port_speed ( struct xhci_device *xhci, unsigned int port,
  687. unsigned int psiv ) {
  688. unsigned int supported = xhci_supported_protocol ( xhci, port );
  689. unsigned int psic;
  690. unsigned int mantissa;
  691. unsigned int exponent;
  692. unsigned int speed;
  693. unsigned int i;
  694. uint32_t ports;
  695. uint32_t psi;
  696. /* Fail if there is no supported protocol */
  697. if ( ! supported )
  698. return -ENOTSUP;
  699. /* Get protocol speed ID count */
  700. ports = readl ( xhci->cap + supported + XHCI_SUPPORTED_PORTS );
  701. psic = XHCI_SUPPORTED_PORTS_PSIC ( ports );
  702. /* Use the default mappings if applicable */
  703. if ( ! psic ) {
  704. switch ( psiv ) {
  705. case XHCI_SPEED_LOW : return USB_SPEED_LOW;
  706. case XHCI_SPEED_FULL : return USB_SPEED_FULL;
  707. case XHCI_SPEED_HIGH : return USB_SPEED_HIGH;
  708. case XHCI_SPEED_SUPER : return USB_SPEED_SUPER;
  709. default:
  710. DBGC ( xhci, "XHCI %p port %d non-standard PSI value "
  711. "%d\n", xhci, port, psiv );
  712. return -ENOTSUP;
  713. }
  714. }
  715. /* Iterate over PSI dwords looking for a match */
  716. for ( i = 0 ; i < psic ; i++ ) {
  717. psi = readl ( xhci->cap + supported + XHCI_SUPPORTED_PSI ( i ));
  718. if ( psiv == XHCI_SUPPORTED_PSI_VALUE ( psi ) ) {
  719. mantissa = XHCI_SUPPORTED_PSI_MANTISSA ( psi );
  720. exponent = XHCI_SUPPORTED_PSI_EXPONENT ( psi );
  721. speed = USB_SPEED ( mantissa, exponent );
  722. return speed;
  723. }
  724. }
  725. DBGC ( xhci, "XHCI %p port %d spurious PSI value %d\n",
  726. xhci, port, psiv );
  727. return -ENOENT;
  728. }
  729. /**
  730. * Find protocol speed ID value
  731. *
  732. * @v xhci xHCI device
  733. * @v port Port number
  734. * @v speed USB speed
  735. * @ret psiv Protocol speed ID value, or negative error
  736. */
  737. static int xhci_port_psiv ( struct xhci_device *xhci, unsigned int port,
  738. unsigned int speed ) {
  739. unsigned int supported = xhci_supported_protocol ( xhci, port );
  740. unsigned int psic;
  741. unsigned int mantissa;
  742. unsigned int exponent;
  743. unsigned int psiv;
  744. unsigned int i;
  745. uint32_t ports;
  746. uint32_t psi;
  747. /* Fail if there is no supported protocol */
  748. if ( ! supported )
  749. return -ENOTSUP;
  750. /* Get protocol speed ID count */
  751. ports = readl ( xhci->cap + supported + XHCI_SUPPORTED_PORTS );
  752. psic = XHCI_SUPPORTED_PORTS_PSIC ( ports );
  753. /* Use the default mappings if applicable */
  754. if ( ! psic ) {
  755. switch ( speed ) {
  756. case USB_SPEED_LOW : return XHCI_SPEED_LOW;
  757. case USB_SPEED_FULL : return XHCI_SPEED_FULL;
  758. case USB_SPEED_HIGH : return XHCI_SPEED_HIGH;
  759. case USB_SPEED_SUPER : return XHCI_SPEED_SUPER;
  760. default:
  761. DBGC ( xhci, "XHCI %p port %d non-standad speed %d\n",
  762. xhci, port, speed );
  763. return -ENOTSUP;
  764. }
  765. }
  766. /* Iterate over PSI dwords looking for a match */
  767. for ( i = 0 ; i < psic ; i++ ) {
  768. psi = readl ( xhci->cap + supported + XHCI_SUPPORTED_PSI ( i ));
  769. mantissa = XHCI_SUPPORTED_PSI_MANTISSA ( psi );
  770. exponent = XHCI_SUPPORTED_PSI_EXPONENT ( psi );
  771. if ( speed == USB_SPEED ( mantissa, exponent ) ) {
  772. psiv = XHCI_SUPPORTED_PSI_VALUE ( psi );
  773. return psiv;
  774. }
  775. }
  776. DBGC ( xhci, "XHCI %p port %d unrepresentable speed %#x\n",
  777. xhci, port, speed );
  778. return -ENOENT;
  779. }
  780. /******************************************************************************
  781. *
  782. * Device context base address array
  783. *
  784. ******************************************************************************
  785. */
  786. /**
  787. * Allocate device context base address array
  788. *
  789. * @v xhci xHCI device
  790. * @ret rc Return status code
  791. */
  792. static int xhci_dcbaa_alloc ( struct xhci_device *xhci ) {
  793. size_t len;
  794. physaddr_t dcbaap;
  795. int rc;
  796. /* Allocate and initialise structure. Must be at least
  797. * 64-byte aligned and must not cross a page boundary, so
  798. * align on its own size (rounded up to a power of two and
  799. * with a minimum of 64 bytes).
  800. */
  801. len = ( ( xhci->slots + 1 ) * sizeof ( xhci->dcbaa[0] ) );
  802. xhci->dcbaa = malloc_dma ( len, xhci_align ( len ) );
  803. if ( ! xhci->dcbaa ) {
  804. DBGC ( xhci, "XHCI %p could not allocate DCBAA\n", xhci );
  805. rc = -ENOMEM;
  806. goto err_alloc;
  807. }
  808. memset ( xhci->dcbaa, 0, len );
  809. /* Program DCBAA pointer */
  810. dcbaap = virt_to_phys ( xhci->dcbaa );
  811. if ( ( rc = xhci_writeq ( xhci, dcbaap,
  812. xhci->op + XHCI_OP_DCBAAP ) ) != 0 )
  813. goto err_writeq;
  814. DBGC2 ( xhci, "XHCI %p DCBAA at [%08lx,%08lx)\n",
  815. xhci, dcbaap, ( dcbaap + len ) );
  816. return 0;
  817. err_writeq:
  818. free_dma ( xhci->dcbaa, len );
  819. err_alloc:
  820. return rc;
  821. }
  822. /**
  823. * Free device context base address array
  824. *
  825. * @v xhci xHCI device
  826. */
  827. static void xhci_dcbaa_free ( struct xhci_device *xhci ) {
  828. size_t len;
  829. unsigned int i;
  830. /* Sanity check */
  831. for ( i = 0 ; i <= xhci->slots ; i++ )
  832. assert ( xhci->dcbaa[i] == 0 );
  833. /* Clear DCBAA pointer */
  834. xhci_writeq ( xhci, 0, xhci->op + XHCI_OP_DCBAAP );
  835. /* Free DCBAA */
  836. len = ( ( xhci->slots + 1 ) * sizeof ( xhci->dcbaa[0] ) );
  837. free_dma ( xhci->dcbaa, len );
  838. }
  839. /******************************************************************************
  840. *
  841. * Scratchpad buffers
  842. *
  843. ******************************************************************************
  844. */
  845. /**
  846. * Allocate scratchpad buffers
  847. *
  848. * @v xhci xHCI device
  849. * @ret rc Return status code
  850. */
  851. static int xhci_scratchpad_alloc ( struct xhci_device *xhci ) {
  852. size_t array_len;
  853. size_t len;
  854. physaddr_t phys;
  855. unsigned int i;
  856. int rc;
  857. /* Do nothing if no scratchpad buffers are used */
  858. if ( ! xhci->scratchpads )
  859. return 0;
  860. /* Allocate scratchpads */
  861. len = ( xhci->scratchpads * xhci->pagesize );
  862. xhci->scratchpad = umalloc ( len );
  863. if ( ! xhci->scratchpad ) {
  864. DBGC ( xhci, "XHCI %p could not allocate scratchpad buffers\n",
  865. xhci );
  866. rc = -ENOMEM;
  867. goto err_alloc;
  868. }
  869. memset_user ( xhci->scratchpad, 0, 0, len );
  870. /* Allocate scratchpad array */
  871. array_len = ( xhci->scratchpads * sizeof ( xhci->scratchpad_array[0] ));
  872. xhci->scratchpad_array =
  873. malloc_dma ( array_len, xhci_align ( array_len ) );
  874. if ( ! xhci->scratchpad_array ) {
  875. DBGC ( xhci, "XHCI %p could not allocate scratchpad buffer "
  876. "array\n", xhci );
  877. rc = -ENOMEM;
  878. goto err_alloc_array;
  879. }
  880. /* Populate scratchpad array */
  881. for ( i = 0 ; i < xhci->scratchpads ; i++ ) {
  882. phys = user_to_phys ( xhci->scratchpad, ( i * xhci->pagesize ));
  883. xhci->scratchpad_array[i] = phys;
  884. }
  885. /* Set scratchpad array pointer */
  886. assert ( xhci->dcbaa != NULL );
  887. xhci->dcbaa[0] = cpu_to_le64 ( virt_to_phys ( xhci->scratchpad_array ));
  888. DBGC2 ( xhci, "XHCI %p scratchpad [%08lx,%08lx) array [%08lx,%08lx)\n",
  889. xhci, user_to_phys ( xhci->scratchpad, 0 ),
  890. user_to_phys ( xhci->scratchpad, len ),
  891. virt_to_phys ( xhci->scratchpad_array ),
  892. ( virt_to_phys ( xhci->scratchpad_array ) + array_len ) );
  893. return 0;
  894. free_dma ( xhci->scratchpad_array, array_len );
  895. err_alloc_array:
  896. ufree ( xhci->scratchpad );
  897. err_alloc:
  898. return rc;
  899. }
  900. /**
  901. * Free scratchpad buffers
  902. *
  903. * @v xhci xHCI device
  904. */
  905. static void xhci_scratchpad_free ( struct xhci_device *xhci ) {
  906. size_t array_len;
  907. /* Do nothing if no scratchpad buffers are used */
  908. if ( ! xhci->scratchpads )
  909. return;
  910. /* Clear scratchpad array pointer */
  911. assert ( xhci->dcbaa != NULL );
  912. xhci->dcbaa[0] = 0;
  913. /* Free scratchpad array */
  914. array_len = ( xhci->scratchpads * sizeof ( xhci->scratchpad_array[0] ));
  915. free_dma ( xhci->scratchpad_array, array_len );
  916. /* Free scratchpads */
  917. ufree ( xhci->scratchpad );
  918. }
  919. /******************************************************************************
  920. *
  921. * Run / stop / reset
  922. *
  923. ******************************************************************************
  924. */
  925. /**
  926. * Start xHCI device
  927. *
  928. * @v xhci xHCI device
  929. */
  930. static void xhci_run ( struct xhci_device *xhci ) {
  931. uint32_t config;
  932. uint32_t usbcmd;
  933. /* Configure number of device slots */
  934. config = readl ( xhci->op + XHCI_OP_CONFIG );
  935. config &= ~XHCI_CONFIG_MAX_SLOTS_EN_MASK;
  936. config |= XHCI_CONFIG_MAX_SLOTS_EN ( xhci->slots );
  937. writel ( config, xhci->op + XHCI_OP_CONFIG );
  938. /* Set run/stop bit */
  939. usbcmd = readl ( xhci->op + XHCI_OP_USBCMD );
  940. usbcmd |= XHCI_USBCMD_RUN;
  941. writel ( usbcmd, xhci->op + XHCI_OP_USBCMD );
  942. }
  943. /**
  944. * Stop xHCI device
  945. *
  946. * @v xhci xHCI device
  947. * @ret rc Return status code
  948. */
  949. static int xhci_stop ( struct xhci_device *xhci ) {
  950. uint32_t usbcmd;
  951. uint32_t usbsts;
  952. unsigned int i;
  953. /* Clear run/stop bit */
  954. usbcmd = readl ( xhci->op + XHCI_OP_USBCMD );
  955. usbcmd &= ~XHCI_USBCMD_RUN;
  956. writel ( usbcmd, xhci->op + XHCI_OP_USBCMD );
  957. /* Wait for device to stop */
  958. for ( i = 0 ; i < XHCI_STOP_MAX_WAIT_MS ; i++ ) {
  959. /* Check if device is stopped */
  960. usbsts = readl ( xhci->op + XHCI_OP_USBSTS );
  961. if ( usbsts & XHCI_USBSTS_HCH )
  962. return 0;
  963. /* Delay */
  964. mdelay ( 1 );
  965. }
  966. DBGC ( xhci, "XHCI %p timed out waiting for stop\n", xhci );
  967. return -ETIMEDOUT;
  968. }
  969. /**
  970. * Reset xHCI device
  971. *
  972. * @v xhci xHCI device
  973. * @ret rc Return status code
  974. */
  975. static int xhci_reset ( struct xhci_device *xhci ) {
  976. uint32_t usbcmd;
  977. unsigned int i;
  978. int rc;
  979. /* The xHCI specification states that resetting a running
  980. * device may result in undefined behaviour, so try stopping
  981. * it first.
  982. */
  983. if ( ( rc = xhci_stop ( xhci ) ) != 0 ) {
  984. /* Ignore errors and attempt to reset the device anyway */
  985. }
  986. /* Reset device */
  987. writel ( XHCI_USBCMD_HCRST, xhci->op + XHCI_OP_USBCMD );
  988. /* Wait for reset to complete */
  989. for ( i = 0 ; i < XHCI_RESET_MAX_WAIT_MS ; i++ ) {
  990. /* Check if reset is complete */
  991. usbcmd = readl ( xhci->op + XHCI_OP_USBCMD );
  992. if ( ! ( usbcmd & XHCI_USBCMD_HCRST ) )
  993. return 0;
  994. /* Delay */
  995. mdelay ( 1 );
  996. }
  997. DBGC ( xhci, "XHCI %p timed out waiting for reset\n", xhci );
  998. return -ETIMEDOUT;
  999. }
  1000. /******************************************************************************
  1001. *
  1002. * Transfer request blocks
  1003. *
  1004. ******************************************************************************
  1005. */
  1006. /**
  1007. * Allocate transfer request block ring
  1008. *
  1009. * @v xhci xHCI device
  1010. * @v ring TRB ring
  1011. * @v shift Ring size (log2)
  1012. * @v slot Device slot
  1013. * @v target Doorbell target
  1014. * @v stream Doorbell stream ID
  1015. * @ret rc Return status code
  1016. */
  1017. static int xhci_ring_alloc ( struct xhci_device *xhci,
  1018. struct xhci_trb_ring *ring,
  1019. unsigned int shift, unsigned int slot,
  1020. unsigned int target, unsigned int stream ) {
  1021. struct xhci_trb_link *link;
  1022. unsigned int count;
  1023. int rc;
  1024. /* Sanity check */
  1025. assert ( shift > 0 );
  1026. /* Initialise structure */
  1027. memset ( ring, 0, sizeof ( *ring ) );
  1028. ring->shift = shift;
  1029. count = ( 1U << shift );
  1030. ring->mask = ( count - 1 );
  1031. ring->len = ( ( count + 1 /* Link TRB */ ) * sizeof ( ring->trb[0] ) );
  1032. ring->db = ( xhci->db + ( slot * sizeof ( ring->dbval ) ) );
  1033. ring->dbval = XHCI_DBVAL ( target, stream );
  1034. /* Allocate I/O buffers */
  1035. ring->iobuf = zalloc ( count * sizeof ( ring->iobuf[0] ) );
  1036. if ( ! ring->iobuf ) {
  1037. rc = -ENOMEM;
  1038. goto err_alloc_iobuf;
  1039. }
  1040. /* Allocate TRBs */
  1041. ring->trb = malloc_dma ( ring->len, xhci_align ( ring->len ) );
  1042. if ( ! ring->trb ) {
  1043. rc = -ENOMEM;
  1044. goto err_alloc_trb;
  1045. }
  1046. memset ( ring->trb, 0, ring->len );
  1047. /* Initialise Link TRB */
  1048. link = &ring->trb[count].link;
  1049. link->next = cpu_to_le64 ( virt_to_phys ( ring->trb ) );
  1050. link->flags = XHCI_TRB_TC;
  1051. link->type = XHCI_TRB_LINK;
  1052. ring->link = link;
  1053. return 0;
  1054. free_dma ( ring->trb, ring->len );
  1055. err_alloc_trb:
  1056. free ( ring->iobuf );
  1057. err_alloc_iobuf:
  1058. return rc;
  1059. }
  1060. /**
  1061. * Free transfer request block ring
  1062. *
  1063. * @v ring TRB ring
  1064. */
  1065. static void xhci_ring_free ( struct xhci_trb_ring *ring ) {
  1066. unsigned int count = ( 1U << ring->shift );
  1067. unsigned int i;
  1068. /* Sanity checks */
  1069. assert ( ring->cons == ring->prod );
  1070. for ( i = 0 ; i < count ; i++ )
  1071. assert ( ring->iobuf[i] == NULL );
  1072. /* Free TRBs */
  1073. free_dma ( ring->trb, ring->len );
  1074. /* Free I/O buffers */
  1075. free ( ring->iobuf );
  1076. }
  1077. /**
  1078. * Enqueue a transfer request block
  1079. *
  1080. * @v ring TRB ring
  1081. * @v iobuf I/O buffer (if any)
  1082. * @v trb Transfer request block (with empty Cycle flag)
  1083. * @ret rc Return status code
  1084. *
  1085. * This operation does not implicitly ring the doorbell register.
  1086. */
  1087. static int xhci_enqueue ( struct xhci_trb_ring *ring, struct io_buffer *iobuf,
  1088. const union xhci_trb *trb ) {
  1089. union xhci_trb *dest;
  1090. unsigned int prod;
  1091. unsigned int mask;
  1092. unsigned int index;
  1093. unsigned int cycle;
  1094. /* Sanity check */
  1095. assert ( ! ( trb->common.flags & XHCI_TRB_C ) );
  1096. /* Fail if ring is full */
  1097. if ( ! xhci_ring_remaining ( ring ) )
  1098. return -ENOBUFS;
  1099. /* Update producer counter (and link TRB, if applicable) */
  1100. prod = ring->prod++;
  1101. mask = ring->mask;
  1102. cycle = ( ( ~( prod >> ring->shift ) ) & XHCI_TRB_C );
  1103. index = ( prod & mask );
  1104. if ( index == 0 )
  1105. ring->link->flags = ( XHCI_TRB_TC | ( cycle ^ XHCI_TRB_C ) );
  1106. /* Record I/O buffer */
  1107. ring->iobuf[index] = iobuf;
  1108. /* Enqueue TRB */
  1109. dest = &ring->trb[index];
  1110. dest->template.parameter = trb->template.parameter;
  1111. dest->template.status = trb->template.status;
  1112. wmb();
  1113. dest->template.control = ( trb->template.control |
  1114. cpu_to_le32 ( cycle ) );
  1115. return 0;
  1116. }
  1117. /**
  1118. * Dequeue a transfer request block
  1119. *
  1120. * @v ring TRB ring
  1121. * @ret iobuf I/O buffer
  1122. */
  1123. static struct io_buffer * xhci_dequeue ( struct xhci_trb_ring *ring ) {
  1124. struct io_buffer *iobuf;
  1125. unsigned int cons;
  1126. unsigned int mask;
  1127. unsigned int index;
  1128. /* Sanity check */
  1129. assert ( xhci_ring_fill ( ring ) != 0 );
  1130. /* Update consumer counter */
  1131. cons = ring->cons++;
  1132. mask = ring->mask;
  1133. index = ( cons & mask );
  1134. /* Retrieve I/O buffer */
  1135. iobuf = ring->iobuf[index];
  1136. ring->iobuf[index] = NULL;
  1137. return iobuf;
  1138. }
  1139. /**
  1140. * Enqueue multiple transfer request blocks
  1141. *
  1142. * @v ring TRB ring
  1143. * @v iobuf I/O buffer
  1144. * @v trbs Transfer request blocks (with empty Cycle flag)
  1145. * @v count Number of transfer request blocks
  1146. * @ret rc Return status code
  1147. *
  1148. * This operation does not implicitly ring the doorbell register.
  1149. */
  1150. static int xhci_enqueue_multi ( struct xhci_trb_ring *ring,
  1151. struct io_buffer *iobuf,
  1152. const union xhci_trb *trbs,
  1153. unsigned int count ) {
  1154. const union xhci_trb *trb = trbs;
  1155. int rc;
  1156. /* Sanity check */
  1157. assert ( iobuf != NULL );
  1158. /* Fail if ring does not have sufficient space */
  1159. if ( xhci_ring_remaining ( ring ) < count )
  1160. return -ENOBUFS;
  1161. /* Enqueue each TRB, recording the I/O buffer with the final TRB */
  1162. while ( count-- ) {
  1163. rc = xhci_enqueue ( ring, ( count ? NULL : iobuf ), trb++ );
  1164. assert ( rc == 0 ); /* Should never be able to fail */
  1165. }
  1166. return 0;
  1167. }
  1168. /**
  1169. * Dequeue multiple transfer request blocks
  1170. *
  1171. * @v ring TRB ring
  1172. * @ret iobuf I/O buffer
  1173. */
  1174. static struct io_buffer * xhci_dequeue_multi ( struct xhci_trb_ring *ring ) {
  1175. struct io_buffer *iobuf;
  1176. /* Dequeue TRBs until we reach the final TRB for an I/O buffer */
  1177. do {
  1178. iobuf = xhci_dequeue ( ring );
  1179. } while ( iobuf == NULL );
  1180. return iobuf;
  1181. }
  1182. /**
  1183. * Ring doorbell register
  1184. *
  1185. * @v ring TRB ring
  1186. */
  1187. static inline __attribute__ (( always_inline )) void
  1188. xhci_doorbell ( struct xhci_trb_ring *ring ) {
  1189. wmb();
  1190. writel ( ring->dbval, ring->db );
  1191. }
  1192. /******************************************************************************
  1193. *
  1194. * Command and event rings
  1195. *
  1196. ******************************************************************************
  1197. */
  1198. /**
  1199. * Allocate command ring
  1200. *
  1201. * @v xhci xHCI device
  1202. * @ret rc Return status code
  1203. */
  1204. static int xhci_command_alloc ( struct xhci_device *xhci ) {
  1205. physaddr_t crp;
  1206. int rc;
  1207. /* Allocate TRB ring */
  1208. if ( ( rc = xhci_ring_alloc ( xhci, &xhci->command, XHCI_CMD_TRBS_LOG2,
  1209. 0, 0, 0 ) ) != 0 )
  1210. goto err_ring_alloc;
  1211. /* Program command ring control register */
  1212. crp = virt_to_phys ( xhci->command.trb );
  1213. if ( ( rc = xhci_writeq ( xhci, ( crp | XHCI_CRCR_RCS ),
  1214. xhci->op + XHCI_OP_CRCR ) ) != 0 )
  1215. goto err_writeq;
  1216. DBGC2 ( xhci, "XHCI %p CRCR at [%08lx,%08lx)\n",
  1217. xhci, crp, ( crp + xhci->command.len ) );
  1218. return 0;
  1219. err_writeq:
  1220. xhci_ring_free ( &xhci->command );
  1221. err_ring_alloc:
  1222. return rc;
  1223. }
  1224. /**
  1225. * Free command ring
  1226. *
  1227. * @v xhci xHCI device
  1228. */
  1229. static void xhci_command_free ( struct xhci_device *xhci ) {
  1230. /* Sanity check */
  1231. assert ( ( readl ( xhci->op + XHCI_OP_CRCR ) & XHCI_CRCR_CRR ) == 0 );
  1232. /* Clear command ring control register */
  1233. xhci_writeq ( xhci, 0, xhci->op + XHCI_OP_CRCR );
  1234. /* Free TRB ring */
  1235. xhci_ring_free ( &xhci->command );
  1236. }
  1237. /**
  1238. * Allocate event ring
  1239. *
  1240. * @v xhci xHCI device
  1241. * @ret rc Return status code
  1242. */
  1243. static int xhci_event_alloc ( struct xhci_device *xhci ) {
  1244. struct xhci_event_ring *event = &xhci->event;
  1245. unsigned int count;
  1246. size_t len;
  1247. int rc;
  1248. /* Allocate event ring */
  1249. count = ( 1 << XHCI_EVENT_TRBS_LOG2 );
  1250. len = ( count * sizeof ( event->trb[0] ) );
  1251. event->trb = malloc_dma ( len, xhci_align ( len ) );
  1252. if ( ! event->trb ) {
  1253. rc = -ENOMEM;
  1254. goto err_alloc_trb;
  1255. }
  1256. memset ( event->trb, 0, len );
  1257. /* Allocate event ring segment table */
  1258. event->segment = malloc_dma ( sizeof ( event->segment[0] ),
  1259. xhci_align ( sizeof (event->segment[0])));
  1260. if ( ! event->segment ) {
  1261. rc = -ENOMEM;
  1262. goto err_alloc_segment;
  1263. }
  1264. memset ( event->segment, 0, sizeof ( event->segment[0] ) );
  1265. event->segment[0].base = cpu_to_le64 ( virt_to_phys ( event->trb ) );
  1266. event->segment[0].count = cpu_to_le32 ( count );
  1267. /* Program event ring registers */
  1268. writel ( 1, xhci->run + XHCI_RUN_ERSTSZ ( 0 ) );
  1269. if ( ( rc = xhci_writeq ( xhci, virt_to_phys ( event->trb ),
  1270. xhci->run + XHCI_RUN_ERDP ( 0 ) ) ) != 0 )
  1271. goto err_writeq_erdp;
  1272. if ( ( rc = xhci_writeq ( xhci, virt_to_phys ( event->segment ),
  1273. xhci->run + XHCI_RUN_ERSTBA ( 0 ) ) ) != 0 )
  1274. goto err_writeq_erstba;
  1275. DBGC2 ( xhci, "XHCI %p event ring [%08lx,%08lx) table [%08lx,%08lx)\n",
  1276. xhci, virt_to_phys ( event->trb ),
  1277. ( virt_to_phys ( event->trb ) + len ),
  1278. virt_to_phys ( event->segment ),
  1279. ( virt_to_phys ( event->segment ) +
  1280. sizeof (event->segment[0] ) ) );
  1281. return 0;
  1282. xhci_writeq ( xhci, 0, xhci->run + XHCI_RUN_ERSTBA ( 0 ) );
  1283. err_writeq_erstba:
  1284. xhci_writeq ( xhci, 0, xhci->run + XHCI_RUN_ERDP ( 0 ) );
  1285. err_writeq_erdp:
  1286. free_dma ( event->trb, len );
  1287. err_alloc_segment:
  1288. free_dma ( event->segment, sizeof ( event->segment[0] ) );
  1289. err_alloc_trb:
  1290. return rc;
  1291. }
  1292. /**
  1293. * Free event ring
  1294. *
  1295. * @v xhci xHCI device
  1296. */
  1297. static void xhci_event_free ( struct xhci_device *xhci ) {
  1298. struct xhci_event_ring *event = &xhci->event;
  1299. unsigned int count;
  1300. size_t len;
  1301. /* Clear event ring registers */
  1302. writel ( 0, xhci->run + XHCI_RUN_ERSTSZ ( 0 ) );
  1303. xhci_writeq ( xhci, 0, xhci->run + XHCI_RUN_ERSTBA ( 0 ) );
  1304. xhci_writeq ( xhci, 0, xhci->run + XHCI_RUN_ERDP ( 0 ) );
  1305. /* Free event ring segment table */
  1306. free_dma ( event->segment, sizeof ( event->segment[0] ) );
  1307. /* Free event ring */
  1308. count = ( 1 << XHCI_EVENT_TRBS_LOG2 );
  1309. len = ( count * sizeof ( event->trb[0] ) );
  1310. free_dma ( event->trb, len );
  1311. }
  1312. /**
  1313. * Handle transfer event
  1314. *
  1315. * @v xhci xHCI device
  1316. * @v transfer Transfer event TRB
  1317. */
  1318. static void xhci_transfer ( struct xhci_device *xhci,
  1319. struct xhci_trb_transfer *transfer ) {
  1320. struct xhci_slot *slot;
  1321. struct xhci_endpoint *endpoint;
  1322. struct io_buffer *iobuf;
  1323. int rc;
  1324. /* Profile transfer events */
  1325. profile_start ( &xhci_transfer_profiler );
  1326. /* Identify slot */
  1327. if ( ( transfer->slot > xhci->slots ) ||
  1328. ( ( slot = xhci->slot[transfer->slot] ) == NULL ) ) {
  1329. DBGC ( xhci, "XHCI %p transfer event invalid slot %d:\n",
  1330. xhci, transfer->slot );
  1331. DBGC_HDA ( xhci, 0, transfer, sizeof ( *transfer ) );
  1332. return;
  1333. }
  1334. /* Identify endpoint */
  1335. if ( ( transfer->endpoint > XHCI_CTX_END ) ||
  1336. ( ( endpoint = slot->endpoint[transfer->endpoint] ) == NULL ) ) {
  1337. DBGC ( xhci, "XHCI %p slot %d transfer event invalid epid "
  1338. "%d:\n", xhci, slot->id, transfer->endpoint );
  1339. DBGC_HDA ( xhci, 0, transfer, sizeof ( *transfer ) );
  1340. return;
  1341. }
  1342. /* Dequeue TRB(s) */
  1343. iobuf = xhci_dequeue_multi ( &endpoint->ring );
  1344. assert ( iobuf != NULL );
  1345. /* Check for errors */
  1346. if ( ! ( ( transfer->code == XHCI_CMPLT_SUCCESS ) ||
  1347. ( transfer->code == XHCI_CMPLT_SHORT ) ) ) {
  1348. /* Construct error */
  1349. rc = -ECODE ( transfer->code );
  1350. DBGC ( xhci, "XHCI %p slot %d ctx %d failed (code %d): %s\n",
  1351. xhci, slot->id, endpoint->ctx, transfer->code,
  1352. strerror ( rc ) );
  1353. DBGC_HDA ( xhci, 0, transfer, sizeof ( *transfer ) );
  1354. /* Sanity check */
  1355. assert ( ( endpoint->context->state & XHCI_ENDPOINT_STATE_MASK )
  1356. != XHCI_ENDPOINT_RUNNING );
  1357. /* Report failure to USB core */
  1358. usb_complete_err ( endpoint->ep, iobuf, rc );
  1359. return;
  1360. }
  1361. /* Record actual transfer size */
  1362. iob_unput ( iobuf, le16_to_cpu ( transfer->residual ) );
  1363. /* Sanity check (for successful completions only) */
  1364. assert ( xhci_ring_consumed ( &endpoint->ring ) ==
  1365. le64_to_cpu ( transfer->transfer ) );
  1366. /* Report completion to USB core */
  1367. usb_complete ( endpoint->ep, iobuf );
  1368. profile_stop ( &xhci_transfer_profiler );
  1369. }
  1370. /**
  1371. * Handle command completion event
  1372. *
  1373. * @v xhci xHCI device
  1374. * @v complete Command completion event
  1375. */
  1376. static void xhci_complete ( struct xhci_device *xhci,
  1377. struct xhci_trb_complete *complete ) {
  1378. /* Dequeue command TRB */
  1379. xhci_dequeue ( &xhci->command );
  1380. /* Sanity check */
  1381. assert ( xhci_ring_consumed ( &xhci->command ) ==
  1382. le64_to_cpu ( complete->command ) );
  1383. /* Record completion if applicable */
  1384. if ( xhci->completion ) {
  1385. memcpy ( xhci->completion, complete,
  1386. sizeof ( *xhci->completion ) );
  1387. xhci->completion = NULL;
  1388. } else {
  1389. DBGC ( xhci, "XHCI %p unexpected completion:\n", xhci );
  1390. DBGC_HDA ( xhci, 0, complete, sizeof ( *complete ) );
  1391. }
  1392. }
  1393. /**
  1394. * Handle port status event
  1395. *
  1396. * @v xhci xHCI device
  1397. * @v port Port status event
  1398. */
  1399. static void xhci_port_status ( struct xhci_device *xhci,
  1400. struct xhci_trb_port_status *port ) {
  1401. uint32_t portsc;
  1402. /* Sanity check */
  1403. assert ( ( port->port > 0 ) && ( port->port <= xhci->ports ) );
  1404. /* Clear port status change bits */
  1405. portsc = readl ( xhci->op + XHCI_OP_PORTSC ( port->port ) );
  1406. portsc &= ( XHCI_PORTSC_PRESERVE | XHCI_PORTSC_CHANGE );
  1407. writel ( portsc, xhci->op + XHCI_OP_PORTSC ( port->port ) );
  1408. /* Report port status change */
  1409. usb_port_changed ( usb_port ( xhci->bus->hub, port->port ) );
  1410. }
  1411. /**
  1412. * Poll event ring
  1413. *
  1414. * @v xhci xHCI device
  1415. */
  1416. static void xhci_event_poll ( struct xhci_device *xhci ) {
  1417. struct xhci_event_ring *event = &xhci->event;
  1418. union xhci_trb *trb;
  1419. unsigned int shift = XHCI_EVENT_TRBS_LOG2;
  1420. unsigned int count = ( 1 << shift );
  1421. unsigned int mask = ( count - 1 );
  1422. unsigned int consumed;
  1423. unsigned int type;
  1424. /* Poll for events */
  1425. profile_start ( &xhci_event_profiler );
  1426. for ( consumed = 0 ; ; consumed++ ) {
  1427. /* Stop if we reach an empty TRB */
  1428. rmb();
  1429. trb = &event->trb[ event->cons & mask ];
  1430. if ( ! ( ( trb->common.flags ^
  1431. ( event->cons >> shift ) ) & XHCI_TRB_C ) )
  1432. break;
  1433. /* Handle TRB */
  1434. type = ( trb->common.type & XHCI_TRB_TYPE_MASK );
  1435. switch ( type ) {
  1436. case XHCI_TRB_TRANSFER :
  1437. xhci_transfer ( xhci, &trb->transfer );
  1438. break;
  1439. case XHCI_TRB_COMPLETE :
  1440. xhci_complete ( xhci, &trb->complete );
  1441. break;
  1442. case XHCI_TRB_PORT_STATUS:
  1443. xhci_port_status ( xhci, &trb->port );
  1444. break;
  1445. default:
  1446. DBGC ( xhci, "XHCI %p unrecognised event %#x\n:",
  1447. xhci, event->cons );
  1448. DBGC_HDA ( xhci, virt_to_phys ( trb ),
  1449. trb, sizeof ( *trb ) );
  1450. break;
  1451. }
  1452. /* Consume this TRB */
  1453. event->cons++;
  1454. }
  1455. /* Update dequeue pointer if applicable */
  1456. if ( consumed ) {
  1457. xhci_writeq ( xhci, virt_to_phys ( trb ),
  1458. xhci->run + XHCI_RUN_ERDP ( 0 ) );
  1459. profile_stop ( &xhci_event_profiler );
  1460. }
  1461. }
  1462. /**
  1463. * Issue command and wait for completion
  1464. *
  1465. * @v xhci xHCI device
  1466. * @v trb Transfer request block (with empty Cycle flag)
  1467. * @ret rc Return status code
  1468. *
  1469. * On a successful completion, the TRB will be overwritten with the
  1470. * completion.
  1471. */
  1472. static int xhci_command ( struct xhci_device *xhci, union xhci_trb *trb ) {
  1473. struct xhci_trb_complete *complete = &trb->complete;
  1474. unsigned int i;
  1475. int rc;
  1476. /* Record the completion buffer */
  1477. xhci->completion = trb;
  1478. /* Enqueue the command */
  1479. if ( ( rc = xhci_enqueue ( &xhci->command, NULL, trb ) ) != 0 )
  1480. goto err_enqueue;
  1481. /* Ring the command doorbell */
  1482. xhci_doorbell ( &xhci->command );
  1483. /* Wait for the command to complete */
  1484. for ( i = 0 ; i < XHCI_COMMAND_MAX_WAIT_MS ; i++ ) {
  1485. /* Poll event ring */
  1486. xhci_event_poll ( xhci );
  1487. /* Check for completion */
  1488. if ( ! xhci->completion ) {
  1489. if ( complete->code != XHCI_CMPLT_SUCCESS ) {
  1490. rc = -ECODE ( complete->code );
  1491. DBGC ( xhci, "XHCI %p command failed (code "
  1492. "%d): %s\n", xhci, complete->code,
  1493. strerror ( rc ) );
  1494. DBGC_HDA ( xhci, 0, trb, sizeof ( *trb ) );
  1495. return rc;
  1496. }
  1497. return 0;
  1498. }
  1499. /* Delay */
  1500. mdelay ( 1 );
  1501. }
  1502. /* Timeout */
  1503. DBGC ( xhci, "XHCI %p timed out waiting for completion\n", xhci );
  1504. rc = -ETIMEDOUT;
  1505. err_enqueue:
  1506. xhci->completion = NULL;
  1507. return rc;
  1508. }
  1509. /**
  1510. * Issue NOP and wait for completion
  1511. *
  1512. * @v xhci xHCI device
  1513. * @ret rc Return status code
  1514. */
  1515. static inline int xhci_nop ( struct xhci_device *xhci ) {
  1516. union xhci_trb trb;
  1517. struct xhci_trb_common *nop = &trb.common;
  1518. int rc;
  1519. /* Construct command */
  1520. memset ( nop, 0, sizeof ( *nop ) );
  1521. nop->flags = XHCI_TRB_IOC;
  1522. nop->type = XHCI_TRB_NOP_CMD;
  1523. /* Issue command and wait for completion */
  1524. if ( ( rc = xhci_command ( xhci, &trb ) ) != 0 )
  1525. return rc;
  1526. return 0;
  1527. }
  1528. /**
  1529. * Enable slot
  1530. *
  1531. * @v xhci xHCI device
  1532. * @v type Slot type
  1533. * @ret slot Device slot ID, or negative error
  1534. */
  1535. static inline int xhci_enable_slot ( struct xhci_device *xhci,
  1536. unsigned int type ) {
  1537. union xhci_trb trb;
  1538. struct xhci_trb_enable_slot *enable = &trb.enable;
  1539. struct xhci_trb_complete *enabled = &trb.complete;
  1540. unsigned int slot;
  1541. int rc;
  1542. /* Construct command */
  1543. memset ( enable, 0, sizeof ( *enable ) );
  1544. enable->slot = type;
  1545. enable->type = XHCI_TRB_ENABLE_SLOT;
  1546. /* Issue command and wait for completion */
  1547. if ( ( rc = xhci_command ( xhci, &trb ) ) != 0 ) {
  1548. DBGC ( xhci, "XHCI %p could not enable new slot: %s\n",
  1549. xhci, strerror ( rc ) );
  1550. return rc;
  1551. }
  1552. /* Extract slot number */
  1553. slot = enabled->slot;
  1554. DBGC2 ( xhci, "XHCI %p slot %d enabled\n", xhci, slot );
  1555. return slot;
  1556. }
  1557. /**
  1558. * Disable slot
  1559. *
  1560. * @v xhci xHCI device
  1561. * @v slot Device slot
  1562. * @ret rc Return status code
  1563. */
  1564. static inline int xhci_disable_slot ( struct xhci_device *xhci,
  1565. unsigned int slot ) {
  1566. union xhci_trb trb;
  1567. struct xhci_trb_disable_slot *disable = &trb.disable;
  1568. int rc;
  1569. /* Construct command */
  1570. memset ( disable, 0, sizeof ( *disable ) );
  1571. disable->type = XHCI_TRB_DISABLE_SLOT;
  1572. disable->slot = slot;
  1573. /* Issue command and wait for completion */
  1574. if ( ( rc = xhci_command ( xhci, &trb ) ) != 0 ) {
  1575. DBGC ( xhci, "XHCI %p could not disable slot %d: %s\n",
  1576. xhci, slot, strerror ( rc ) );
  1577. return rc;
  1578. }
  1579. DBGC2 ( xhci, "XHCI %p slot %d disabled\n", xhci, slot );
  1580. return 0;
  1581. }
  1582. /**
  1583. * Issue context-based command and wait for completion
  1584. *
  1585. * @v xhci xHCI device
  1586. * @v slot Device slot
  1587. * @v endpoint Endpoint
  1588. * @v type TRB type
  1589. * @v populate Input context populater
  1590. * @ret rc Return status code
  1591. */
  1592. static int xhci_context ( struct xhci_device *xhci, struct xhci_slot *slot,
  1593. struct xhci_endpoint *endpoint, unsigned int type,
  1594. void ( * populate ) ( struct xhci_device *xhci,
  1595. struct xhci_slot *slot,
  1596. struct xhci_endpoint *endpoint,
  1597. void *input ) ) {
  1598. union xhci_trb trb;
  1599. struct xhci_trb_context *context = &trb.context;
  1600. size_t len;
  1601. void *input;
  1602. int rc;
  1603. /* Allocate an input context */
  1604. len = xhci_input_context_offset ( xhci, XHCI_CTX_END );
  1605. input = malloc_dma ( len, xhci_align ( len ) );
  1606. if ( ! input ) {
  1607. rc = -ENOMEM;
  1608. goto err_alloc;
  1609. }
  1610. memset ( input, 0, len );
  1611. /* Populate input context */
  1612. populate ( xhci, slot, endpoint, input );
  1613. /* Construct command */
  1614. memset ( context, 0, sizeof ( *context ) );
  1615. context->type = type;
  1616. context->input = cpu_to_le64 ( virt_to_phys ( input ) );
  1617. context->slot = slot->id;
  1618. /* Issue command and wait for completion */
  1619. if ( ( rc = xhci_command ( xhci, &trb ) ) != 0 )
  1620. goto err_command;
  1621. err_command:
  1622. free_dma ( input, len );
  1623. err_alloc:
  1624. return rc;
  1625. }
  1626. /**
  1627. * Populate address device input context
  1628. *
  1629. * @v xhci xHCI device
  1630. * @v slot Device slot
  1631. * @v endpoint Endpoint
  1632. * @v input Input context
  1633. */
  1634. static void xhci_address_device_input ( struct xhci_device *xhci,
  1635. struct xhci_slot *slot,
  1636. struct xhci_endpoint *endpoint,
  1637. void *input ) {
  1638. struct xhci_control_context *control_ctx;
  1639. struct xhci_slot_context *slot_ctx;
  1640. struct xhci_endpoint_context *ep_ctx;
  1641. /* Sanity checks */
  1642. assert ( endpoint->ctx == XHCI_CTX_EP0 );
  1643. /* Populate control context */
  1644. control_ctx = input;
  1645. control_ctx->add = cpu_to_le32 ( ( 1 << XHCI_CTX_SLOT ) |
  1646. ( 1 << XHCI_CTX_EP0 ) );
  1647. /* Populate slot context */
  1648. slot_ctx = ( input + xhci_input_context_offset ( xhci, XHCI_CTX_SLOT ));
  1649. slot_ctx->info = cpu_to_le32 ( XHCI_SLOT_INFO ( 1, 0, slot->psiv,
  1650. slot->route ) );
  1651. slot_ctx->port = slot->port;
  1652. /* Populate control endpoint context */
  1653. ep_ctx = ( input + xhci_input_context_offset ( xhci, XHCI_CTX_EP0 ) );
  1654. ep_ctx->type = XHCI_EP_TYPE_CONTROL;
  1655. ep_ctx->burst = endpoint->ep->burst;
  1656. ep_ctx->mtu = cpu_to_le16 ( endpoint->ep->mtu );
  1657. ep_ctx->dequeue = cpu_to_le64 ( virt_to_phys ( endpoint->ring.trb ) |
  1658. XHCI_EP_DCS );
  1659. ep_ctx->trb_len = cpu_to_le16 ( XHCI_EP0_TRB_LEN );
  1660. }
  1661. /**
  1662. * Address device
  1663. *
  1664. * @v xhci xHCI device
  1665. * @v slot Device slot
  1666. * @ret rc Return status code
  1667. */
  1668. static inline int xhci_address_device ( struct xhci_device *xhci,
  1669. struct xhci_slot *slot ) {
  1670. struct usb_device *usb = slot->usb;
  1671. struct xhci_slot_context *slot_ctx;
  1672. int rc;
  1673. /* Assign device address */
  1674. if ( ( rc = xhci_context ( xhci, slot, slot->endpoint[XHCI_CTX_EP0],
  1675. XHCI_TRB_ADDRESS_DEVICE,
  1676. xhci_address_device_input ) ) != 0 )
  1677. return rc;
  1678. /* Get assigned address */
  1679. slot_ctx = ( slot->context +
  1680. xhci_device_context_offset ( xhci, XHCI_CTX_SLOT ) );
  1681. usb->address = slot_ctx->address;
  1682. DBGC2 ( xhci, "XHCI %p assigned address %d to %s\n",
  1683. xhci, usb->address, usb->name );
  1684. return 0;
  1685. }
  1686. /**
  1687. * Populate configure endpoint input context
  1688. *
  1689. * @v xhci xHCI device
  1690. * @v slot Device slot
  1691. * @v endpoint Endpoint
  1692. * @v input Input context
  1693. */
  1694. static void xhci_configure_endpoint_input ( struct xhci_device *xhci,
  1695. struct xhci_slot *slot __unused,
  1696. struct xhci_endpoint *endpoint,
  1697. void *input ) {
  1698. struct xhci_control_context *control_ctx;
  1699. struct xhci_slot_context *slot_ctx;
  1700. struct xhci_endpoint_context *ep_ctx;
  1701. /* Populate control context */
  1702. control_ctx = input;
  1703. control_ctx->add = cpu_to_le32 ( ( 1 << XHCI_CTX_SLOT ) |
  1704. ( 1 << endpoint->ctx ) );
  1705. /* Populate slot context */
  1706. slot_ctx = ( input + xhci_input_context_offset ( xhci, XHCI_CTX_SLOT ));
  1707. slot_ctx->info = cpu_to_le32 ( XHCI_SLOT_INFO ( ( XHCI_CTX_END - 1 ),
  1708. 0, 0, 0 ) );
  1709. /* Populate endpoint context */
  1710. ep_ctx = ( input + xhci_input_context_offset ( xhci, endpoint->ctx ) );
  1711. ep_ctx->type = endpoint->type;
  1712. ep_ctx->burst = endpoint->ep->burst;
  1713. ep_ctx->mtu = cpu_to_le16 ( endpoint->ep->mtu );
  1714. ep_ctx->dequeue = cpu_to_le64 ( virt_to_phys ( endpoint->ring.trb ) |
  1715. XHCI_EP_DCS );
  1716. ep_ctx->trb_len = cpu_to_le16 ( endpoint->ep->mtu ); /* best guess */
  1717. }
  1718. /**
  1719. * Configure endpoint
  1720. *
  1721. * @v xhci xHCI device
  1722. * @v slot Device slot
  1723. * @v endpoint Endpoint
  1724. * @ret rc Return status code
  1725. */
  1726. static inline int xhci_configure_endpoint ( struct xhci_device *xhci,
  1727. struct xhci_slot *slot,
  1728. struct xhci_endpoint *endpoint ) {
  1729. int rc;
  1730. /* Configure endpoint */
  1731. if ( ( rc = xhci_context ( xhci, slot, endpoint,
  1732. XHCI_TRB_CONFIGURE_ENDPOINT,
  1733. xhci_configure_endpoint_input ) ) != 0 )
  1734. return rc;
  1735. DBGC2 ( xhci, "XHCI %p slot %d ctx %d configured\n",
  1736. xhci, slot->id, endpoint->ctx );
  1737. return 0;
  1738. }
  1739. /**
  1740. * Populate deconfigure endpoint input context
  1741. *
  1742. * @v xhci xHCI device
  1743. * @v slot Device slot
  1744. * @v endpoint Endpoint
  1745. * @v input Input context
  1746. */
  1747. static void
  1748. xhci_deconfigure_endpoint_input ( struct xhci_device *xhci __unused,
  1749. struct xhci_slot *slot __unused,
  1750. struct xhci_endpoint *endpoint,
  1751. void *input ) {
  1752. struct xhci_control_context *control_ctx;
  1753. struct xhci_slot_context *slot_ctx;
  1754. /* Populate control context */
  1755. control_ctx = input;
  1756. control_ctx->add = cpu_to_le32 ( 1 << XHCI_CTX_SLOT );
  1757. control_ctx->drop = cpu_to_le32 ( 1 << endpoint->ctx );
  1758. /* Populate slot context */
  1759. slot_ctx = ( input + xhci_input_context_offset ( xhci, XHCI_CTX_SLOT ));
  1760. slot_ctx->info = cpu_to_le32 ( XHCI_SLOT_INFO ( ( XHCI_CTX_END - 1 ),
  1761. 0, 0, 0 ) );
  1762. }
  1763. /**
  1764. * Deconfigure endpoint
  1765. *
  1766. * @v xhci xHCI device
  1767. * @v slot Device slot
  1768. * @v endpoint Endpoint
  1769. * @ret rc Return status code
  1770. */
  1771. static inline int xhci_deconfigure_endpoint ( struct xhci_device *xhci,
  1772. struct xhci_slot *slot,
  1773. struct xhci_endpoint *endpoint ) {
  1774. int rc;
  1775. /* Deconfigure endpoint */
  1776. if ( ( rc = xhci_context ( xhci, slot, endpoint,
  1777. XHCI_TRB_CONFIGURE_ENDPOINT,
  1778. xhci_deconfigure_endpoint_input ) ) != 0 )
  1779. return rc;
  1780. DBGC2 ( xhci, "XHCI %p slot %d ctx %d deconfigured\n",
  1781. xhci, slot->id, endpoint->ctx );
  1782. return 0;
  1783. }
  1784. /**
  1785. * Populate evaluate context input context
  1786. *
  1787. * @v xhci xHCI device
  1788. * @v slot Device slot
  1789. * @v endpoint Endpoint
  1790. * @v input Input context
  1791. */
  1792. static void xhci_evaluate_context_input ( struct xhci_device *xhci,
  1793. struct xhci_slot *slot __unused,
  1794. struct xhci_endpoint *endpoint,
  1795. void *input ) {
  1796. struct xhci_control_context *control_ctx;
  1797. struct xhci_slot_context *slot_ctx;
  1798. struct xhci_endpoint_context *ep_ctx;
  1799. /* Populate control context */
  1800. control_ctx = input;
  1801. control_ctx->add = cpu_to_le32 ( ( 1 << XHCI_CTX_SLOT ) |
  1802. ( 1 << endpoint->ctx ) );
  1803. /* Populate slot context */
  1804. slot_ctx = ( input + xhci_input_context_offset ( xhci, XHCI_CTX_SLOT ));
  1805. slot_ctx->info = cpu_to_le32 ( XHCI_SLOT_INFO ( ( XHCI_CTX_END - 1 ),
  1806. 0, 0, 0 ) );
  1807. /* Populate endpoint context */
  1808. ep_ctx = ( input + xhci_input_context_offset ( xhci, endpoint->ctx ) );
  1809. ep_ctx->mtu = cpu_to_le16 ( endpoint->ep->mtu );
  1810. }
  1811. /**
  1812. * Evaluate context
  1813. *
  1814. * @v xhci xHCI device
  1815. * @v slot Device slot
  1816. * @v endpoint Endpoint
  1817. * @ret rc Return status code
  1818. */
  1819. static inline int xhci_evaluate_context ( struct xhci_device *xhci,
  1820. struct xhci_slot *slot,
  1821. struct xhci_endpoint *endpoint ) {
  1822. int rc;
  1823. /* Configure endpoint */
  1824. if ( ( rc = xhci_context ( xhci, slot, endpoint,
  1825. XHCI_TRB_EVALUATE_CONTEXT,
  1826. xhci_evaluate_context_input ) ) != 0 )
  1827. return rc;
  1828. DBGC2 ( xhci, "XHCI %p slot %d ctx %d (re-)evaluated\n",
  1829. xhci, slot->id, endpoint->ctx );
  1830. return 0;
  1831. }
  1832. /**
  1833. * Reset endpoint
  1834. *
  1835. * @v xhci xHCI device
  1836. * @v slot Device slot
  1837. * @v endpoint Endpoint
  1838. * @ret rc Return status code
  1839. */
  1840. static inline int xhci_reset_endpoint ( struct xhci_device *xhci,
  1841. struct xhci_slot *slot,
  1842. struct xhci_endpoint *endpoint ) {
  1843. union xhci_trb trb;
  1844. struct xhci_trb_reset_endpoint *reset = &trb.reset;
  1845. int rc;
  1846. /* Construct command */
  1847. memset ( reset, 0, sizeof ( *reset ) );
  1848. reset->slot = slot->id;
  1849. reset->endpoint = endpoint->ctx;
  1850. reset->type = XHCI_TRB_RESET_ENDPOINT;
  1851. /* Issue command and wait for completion */
  1852. if ( ( rc = xhci_command ( xhci, &trb ) ) != 0 ) {
  1853. DBGC ( xhci, "XHCI %p slot %d ctx %d could not reset endpoint "
  1854. "in state %d: %s\n", xhci, slot->id, endpoint->ctx,
  1855. endpoint->context->state, strerror ( rc ) );
  1856. return rc;
  1857. }
  1858. return 0;
  1859. }
  1860. /**
  1861. * Stop endpoint
  1862. *
  1863. * @v xhci xHCI device
  1864. * @v slot Device slot
  1865. * @v endpoint Endpoint
  1866. * @ret rc Return status code
  1867. */
  1868. static inline int xhci_stop_endpoint ( struct xhci_device *xhci,
  1869. struct xhci_slot *slot,
  1870. struct xhci_endpoint *endpoint ) {
  1871. union xhci_trb trb;
  1872. struct xhci_trb_stop_endpoint *stop = &trb.stop;
  1873. int rc;
  1874. /* Construct command */
  1875. memset ( stop, 0, sizeof ( *stop ) );
  1876. stop->slot = slot->id;
  1877. stop->endpoint = endpoint->ctx;
  1878. stop->type = XHCI_TRB_STOP_ENDPOINT;
  1879. /* Issue command and wait for completion */
  1880. if ( ( rc = xhci_command ( xhci, &trb ) ) != 0 ) {
  1881. DBGC ( xhci, "XHCI %p slot %d ctx %d could not stop endpoint "
  1882. "in state %d: %s\n", xhci, slot->id, endpoint->ctx,
  1883. endpoint->context->state, strerror ( rc ) );
  1884. return rc;
  1885. }
  1886. return 0;
  1887. }
  1888. /**
  1889. * Set transfer ring dequeue pointer
  1890. *
  1891. * @v xhci xHCI device
  1892. * @v slot Device slot
  1893. * @v endpoint Endpoint
  1894. * @ret rc Return status code
  1895. */
  1896. static inline int
  1897. xhci_set_tr_dequeue_pointer ( struct xhci_device *xhci,
  1898. struct xhci_slot *slot,
  1899. struct xhci_endpoint *endpoint ) {
  1900. union xhci_trb trb;
  1901. struct xhci_trb_set_tr_dequeue_pointer *dequeue = &trb.dequeue;
  1902. struct xhci_trb_ring *ring = &endpoint->ring;
  1903. unsigned int cons;
  1904. unsigned int mask;
  1905. unsigned int index;
  1906. unsigned int dcs;
  1907. int rc;
  1908. /* Construct command */
  1909. memset ( dequeue, 0, sizeof ( *dequeue ) );
  1910. cons = ring->cons;
  1911. mask = ring->mask;
  1912. dcs = ( ( ~( cons >> ring->shift ) ) & XHCI_EP_DCS );
  1913. index = ( cons & mask );
  1914. dequeue->dequeue =
  1915. cpu_to_le64 ( virt_to_phys ( &ring->trb[index] ) | dcs );
  1916. dequeue->slot = slot->id;
  1917. dequeue->endpoint = endpoint->ctx;
  1918. dequeue->type = XHCI_TRB_SET_TR_DEQUEUE_POINTER;
  1919. /* Issue command and wait for completion */
  1920. if ( ( rc = xhci_command ( xhci, &trb ) ) != 0 ) {
  1921. DBGC ( xhci, "XHCI %p slot %d ctx %d could not set TR dequeue "
  1922. "pointer in state %d: %s\n", xhci, slot->id,
  1923. endpoint->ctx, endpoint->context->state, strerror ( rc));
  1924. return rc;
  1925. }
  1926. return 0;
  1927. }
  1928. /******************************************************************************
  1929. *
  1930. * Endpoint operations
  1931. *
  1932. ******************************************************************************
  1933. */
  1934. /**
  1935. * Open endpoint
  1936. *
  1937. * @v ep USB endpoint
  1938. * @ret rc Return status code
  1939. */
  1940. static int xhci_endpoint_open ( struct usb_endpoint *ep ) {
  1941. struct usb_device *usb = ep->usb;
  1942. struct xhci_slot *slot = usb_get_hostdata ( usb );
  1943. struct xhci_device *xhci = slot->xhci;
  1944. struct xhci_endpoint *endpoint;
  1945. unsigned int ctx;
  1946. unsigned int type;
  1947. int rc;
  1948. /* Calculate context index */
  1949. ctx = XHCI_CTX ( ep->address );
  1950. assert ( slot->endpoint[ctx] == NULL );
  1951. /* Calculate endpoint type */
  1952. type = XHCI_EP_TYPE ( ep->attributes & USB_ENDPOINT_ATTR_TYPE_MASK );
  1953. if ( type == XHCI_EP_TYPE ( USB_ENDPOINT_ATTR_CONTROL ) )
  1954. type = XHCI_EP_TYPE_CONTROL;
  1955. if ( ep->address & USB_DIR_IN )
  1956. type |= XHCI_EP_TYPE_IN;
  1957. /* Allocate and initialise structure */
  1958. endpoint = zalloc ( sizeof ( *endpoint ) );
  1959. if ( ! endpoint ) {
  1960. rc = -ENOMEM;
  1961. goto err_alloc;
  1962. }
  1963. usb_endpoint_set_hostdata ( ep, endpoint );
  1964. slot->endpoint[ctx] = endpoint;
  1965. endpoint->xhci = xhci;
  1966. endpoint->slot = slot;
  1967. endpoint->ep = ep;
  1968. endpoint->ctx = ctx;
  1969. endpoint->type = type;
  1970. endpoint->context = ( ( ( void * ) slot->context ) +
  1971. xhci_device_context_offset ( xhci, ctx ) );
  1972. /* Allocate transfer ring */
  1973. if ( ( rc = xhci_ring_alloc ( xhci, &endpoint->ring,
  1974. XHCI_TRANSFER_TRBS_LOG2,
  1975. slot->id, ctx, 0 ) ) != 0 )
  1976. goto err_ring_alloc;
  1977. /* Configure endpoint, if applicable */
  1978. if ( ( ctx != XHCI_CTX_EP0 ) &&
  1979. ( ( rc = xhci_configure_endpoint ( xhci, slot, endpoint ) ) != 0 ))
  1980. goto err_configure_endpoint;
  1981. DBGC2 ( xhci, "XHCI %p slot %d ctx %d ring [%08lx,%08lx)\n",
  1982. xhci, slot->id, ctx, virt_to_phys ( endpoint->ring.trb ),
  1983. ( virt_to_phys ( endpoint->ring.trb ) + endpoint->ring.len ) );
  1984. return 0;
  1985. xhci_deconfigure_endpoint ( xhci, slot, endpoint );
  1986. err_configure_endpoint:
  1987. xhci_ring_free ( &endpoint->ring );
  1988. err_ring_alloc:
  1989. slot->endpoint[ctx] = NULL;
  1990. free ( endpoint );
  1991. err_alloc:
  1992. return rc;
  1993. }
  1994. /**
  1995. * Close endpoint
  1996. *
  1997. * @v ep USB endpoint
  1998. */
  1999. static void xhci_endpoint_close ( struct usb_endpoint *ep ) {
  2000. struct xhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
  2001. struct xhci_slot *slot = endpoint->slot;
  2002. struct xhci_device *xhci = slot->xhci;
  2003. struct io_buffer *iobuf;
  2004. unsigned int ctx = endpoint->ctx;
  2005. /* Deconfigure endpoint, if applicable */
  2006. if ( ctx != XHCI_CTX_EP0 )
  2007. xhci_deconfigure_endpoint ( xhci, slot, endpoint );
  2008. /* Cancel any incomplete transfers */
  2009. while ( xhci_ring_fill ( &endpoint->ring ) ) {
  2010. iobuf = xhci_dequeue_multi ( &endpoint->ring );
  2011. usb_complete_err ( ep, iobuf, -ECANCELED );
  2012. }
  2013. /* Free endpoint */
  2014. xhci_ring_free ( &endpoint->ring );
  2015. slot->endpoint[ctx] = NULL;
  2016. free ( endpoint );
  2017. }
  2018. /**
  2019. * Reset endpoint
  2020. *
  2021. * @v ep USB endpoint
  2022. * @ret rc Return status code
  2023. */
  2024. static int xhci_endpoint_reset ( struct usb_endpoint *ep ) {
  2025. struct xhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
  2026. struct xhci_slot *slot = endpoint->slot;
  2027. struct xhci_device *xhci = slot->xhci;
  2028. int rc;
  2029. /* Reset endpoint context */
  2030. if ( ( rc = xhci_reset_endpoint ( xhci, slot, endpoint ) ) != 0 )
  2031. return rc;
  2032. /* Set transfer ring dequeue pointer */
  2033. if ( ( rc = xhci_set_tr_dequeue_pointer ( xhci, slot, endpoint ) ) != 0)
  2034. return rc;
  2035. DBGC ( xhci, "XHCI %p slot %d ctx %d reset\n",
  2036. xhci, slot->id, endpoint->ctx );
  2037. return 0;
  2038. }
  2039. /**
  2040. * Update MTU
  2041. *
  2042. * @v ep USB endpoint
  2043. * @ret rc Return status code
  2044. */
  2045. static int xhci_endpoint_mtu ( struct usb_endpoint *ep ) {
  2046. struct xhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
  2047. struct xhci_slot *slot = endpoint->slot;
  2048. struct xhci_device *xhci = slot->xhci;
  2049. int rc;
  2050. /* Evalulate context */
  2051. if ( ( rc = xhci_evaluate_context ( xhci, slot, endpoint ) ) != 0 )
  2052. return rc;
  2053. return 0;
  2054. }
  2055. /**
  2056. * Enqueue message transfer
  2057. *
  2058. * @v ep USB endpoint
  2059. * @v packet Setup packet
  2060. * @v iobuf I/O buffer
  2061. * @ret rc Return status code
  2062. */
  2063. static int xhci_endpoint_message ( struct usb_endpoint *ep,
  2064. struct usb_setup_packet *packet,
  2065. struct io_buffer *iobuf ) {
  2066. struct xhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
  2067. unsigned int input = ( le16_to_cpu ( packet->request ) & USB_DIR_IN );
  2068. size_t len = iob_len ( iobuf );
  2069. union xhci_trb trbs[ 1 /* setup */ + 1 /* possible data */ +
  2070. 1 /* status */ ];
  2071. union xhci_trb *trb = trbs;
  2072. struct xhci_trb_setup *setup;
  2073. struct xhci_trb_data *data;
  2074. struct xhci_trb_status *status;
  2075. int rc;
  2076. /* Profile message transfers */
  2077. profile_start ( &xhci_message_profiler );
  2078. /* Construct setup stage TRB */
  2079. memset ( trbs, 0, sizeof ( trbs ) );
  2080. setup = &(trb++)->setup;
  2081. memcpy ( &setup->packet, packet, sizeof ( setup->packet ) );
  2082. setup->len = cpu_to_le32 ( sizeof ( *packet ) );
  2083. setup->flags = XHCI_TRB_IDT;
  2084. setup->type = XHCI_TRB_SETUP;
  2085. if ( len )
  2086. setup->direction = ( input ? XHCI_SETUP_IN : XHCI_SETUP_OUT );
  2087. /* Construct data stage TRB, if applicable */
  2088. if ( len ) {
  2089. data = &(trb++)->data;
  2090. data->data = cpu_to_le64 ( virt_to_phys ( iobuf->data ) );
  2091. data->len = cpu_to_le32 ( len );
  2092. data->type = XHCI_TRB_DATA;
  2093. data->direction = ( input ? XHCI_DATA_IN : XHCI_DATA_OUT );
  2094. }
  2095. /* Construct status stage TRB */
  2096. status = &(trb++)->status;
  2097. status->flags = XHCI_TRB_IOC;
  2098. status->type = XHCI_TRB_STATUS;
  2099. status->direction =
  2100. ( ( len && input ) ? XHCI_STATUS_OUT : XHCI_STATUS_IN );
  2101. /* Enqueue TRBs */
  2102. if ( ( rc = xhci_enqueue_multi ( &endpoint->ring, iobuf, trbs,
  2103. ( trb - trbs ) ) ) != 0 )
  2104. return rc;
  2105. /* Ring the doorbell */
  2106. xhci_doorbell ( &endpoint->ring );
  2107. profile_stop ( &xhci_message_profiler );
  2108. return 0;
  2109. }
  2110. /**
  2111. * Enqueue stream transfer
  2112. *
  2113. * @v ep USB endpoint
  2114. * @v iobuf I/O buffer
  2115. * @ret rc Return status code
  2116. */
  2117. static int xhci_endpoint_stream ( struct usb_endpoint *ep,
  2118. struct io_buffer *iobuf ) {
  2119. struct xhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
  2120. union xhci_trb trb;
  2121. struct xhci_trb_normal *normal;
  2122. int rc;
  2123. /* Profile stream transfers */
  2124. profile_start ( &xhci_stream_profiler );
  2125. /* Construct normal TRBs */
  2126. normal = &trb.normal;
  2127. normal->data = cpu_to_le64 ( virt_to_phys ( iobuf->data ) );
  2128. normal->len = cpu_to_le32 ( iob_len ( iobuf ) );
  2129. normal->flags = XHCI_TRB_IOC;
  2130. normal->type = XHCI_TRB_NORMAL;
  2131. /* Enqueue TRBs */
  2132. if ( ( rc = xhci_enqueue ( &endpoint->ring, iobuf, &trb ) ) != 0 )
  2133. return rc;
  2134. /* Ring the doorbell */
  2135. xhci_doorbell ( &endpoint->ring );
  2136. profile_stop ( &xhci_stream_profiler );
  2137. return 0;
  2138. }
  2139. /******************************************************************************
  2140. *
  2141. * Device operations
  2142. *
  2143. ******************************************************************************
  2144. */
  2145. /**
  2146. * Open device
  2147. *
  2148. * @v usb USB device
  2149. * @ret rc Return status code
  2150. */
  2151. static int xhci_device_open ( struct usb_device *usb ) {
  2152. struct xhci_device *xhci = usb_bus_get_hostdata ( usb->port->hub->bus );
  2153. struct xhci_slot *slot;
  2154. size_t len;
  2155. int type;
  2156. int id;
  2157. int rc;
  2158. /* Determine applicable slot type */
  2159. type = xhci_port_slot_type ( xhci, usb->port->address );
  2160. if ( type < 0 ) {
  2161. rc = type;
  2162. DBGC ( xhci, "XHCI %p port %d has no slot type\n",
  2163. xhci, usb->port->address );
  2164. goto err_type;
  2165. }
  2166. /* Allocate a device slot number */
  2167. id = xhci_enable_slot ( xhci, type );
  2168. if ( id < 0 ) {
  2169. rc = id;
  2170. goto err_enable_slot;
  2171. }
  2172. assert ( xhci->slot[id] == NULL );
  2173. /* Allocate and initialise structure */
  2174. slot = zalloc ( sizeof ( *slot ) );
  2175. if ( ! slot ) {
  2176. rc = -ENOMEM;
  2177. goto err_alloc;
  2178. }
  2179. usb_set_hostdata ( usb, slot );
  2180. xhci->slot[id] = slot;
  2181. slot->xhci = xhci;
  2182. slot->usb = usb;
  2183. slot->id = id;
  2184. /* Allocate a device context */
  2185. len = xhci_device_context_offset ( xhci, XHCI_CTX_END );
  2186. slot->context = malloc_dma ( len, xhci_align ( len ) );
  2187. if ( ! slot->context ) {
  2188. rc = -ENOMEM;
  2189. goto err_alloc_context;
  2190. }
  2191. memset ( slot->context, 0, len );
  2192. /* Set device context base address */
  2193. assert ( xhci->dcbaa[id] == 0 );
  2194. xhci->dcbaa[id] = cpu_to_le64 ( virt_to_phys ( slot->context ) );
  2195. DBGC2 ( xhci, "XHCI %p slot %d device context [%08lx,%08lx) for %s\n",
  2196. xhci, slot->id, virt_to_phys ( slot->context ),
  2197. ( virt_to_phys ( slot->context ) + len ), usb->name );
  2198. return 0;
  2199. xhci->dcbaa[id] = 0;
  2200. free_dma ( slot->context, len );
  2201. err_alloc_context:
  2202. xhci->slot[id] = NULL;
  2203. free ( slot );
  2204. err_alloc:
  2205. xhci_disable_slot ( xhci, id );
  2206. err_enable_slot:
  2207. err_type:
  2208. return rc;
  2209. }
  2210. /**
  2211. * Close device
  2212. *
  2213. * @v usb USB device
  2214. */
  2215. static void xhci_device_close ( struct usb_device *usb ) {
  2216. struct xhci_slot *slot = usb_get_hostdata ( usb );
  2217. struct xhci_device *xhci = slot->xhci;
  2218. size_t len = xhci_device_context_offset ( xhci, XHCI_CTX_END );
  2219. unsigned int id = slot->id;
  2220. /* Disable slot */
  2221. xhci_disable_slot ( xhci, id );
  2222. /* Free slot */
  2223. free_dma ( slot->context, len );
  2224. xhci->dcbaa[id] = 0;
  2225. xhci->slot[id] = NULL;
  2226. free ( slot );
  2227. }
  2228. /**
  2229. * Assign device address
  2230. *
  2231. * @v usb USB device
  2232. * @ret rc Return status code
  2233. */
  2234. static int xhci_device_address ( struct usb_device *usb ) {
  2235. struct xhci_slot *slot = usb_get_hostdata ( usb );
  2236. struct xhci_device *xhci = slot->xhci;
  2237. struct usb_port *port = usb->port;
  2238. struct usb_port *root_port;
  2239. int psiv;
  2240. int rc;
  2241. /* Calculate route string */
  2242. slot->route = usb_route_string ( usb );
  2243. /* Calculate root hub port number */
  2244. root_port = usb_root_hub_port ( usb );
  2245. slot->port = root_port->address;
  2246. /* Calculate protocol speed ID */
  2247. psiv = xhci_port_psiv ( xhci, slot->port, port->speed );
  2248. if ( psiv < 0 ) {
  2249. rc = psiv;
  2250. return rc;
  2251. }
  2252. slot->psiv = psiv;
  2253. /* Address device */
  2254. if ( ( rc = xhci_address_device ( xhci, slot ) ) != 0 )
  2255. return rc;
  2256. return 0;
  2257. }
  2258. /******************************************************************************
  2259. *
  2260. * Bus operations
  2261. *
  2262. ******************************************************************************
  2263. */
  2264. /**
  2265. * Open USB bus
  2266. *
  2267. * @v bus USB bus
  2268. * @ret rc Return status code
  2269. */
  2270. static int xhci_bus_open ( struct usb_bus *bus ) {
  2271. struct xhci_device *xhci = usb_bus_get_hostdata ( bus );
  2272. int rc;
  2273. /* Allocate device slot array */
  2274. xhci->slot = zalloc ( xhci->slots * sizeof ( xhci->slot[0] ) );
  2275. if ( ! xhci->slot ) {
  2276. rc = -ENOMEM;
  2277. goto err_slot_alloc;
  2278. }
  2279. /* Allocate device context base address array */
  2280. if ( ( rc = xhci_dcbaa_alloc ( xhci ) ) != 0 )
  2281. goto err_dcbaa_alloc;
  2282. /* Allocate scratchpad buffers */
  2283. if ( ( rc = xhci_scratchpad_alloc ( xhci ) ) != 0 )
  2284. goto err_scratchpad_alloc;
  2285. /* Allocate command ring */
  2286. if ( ( rc = xhci_command_alloc ( xhci ) ) != 0 )
  2287. goto err_command_alloc;
  2288. /* Allocate event ring */
  2289. if ( ( rc = xhci_event_alloc ( xhci ) ) != 0 )
  2290. goto err_event_alloc;
  2291. /* Start controller */
  2292. xhci_run ( xhci );
  2293. return 0;
  2294. xhci_stop ( xhci );
  2295. xhci_event_free ( xhci );
  2296. err_event_alloc:
  2297. xhci_command_free ( xhci );
  2298. err_command_alloc:
  2299. xhci_scratchpad_free ( xhci );
  2300. err_scratchpad_alloc:
  2301. xhci_dcbaa_free ( xhci );
  2302. err_dcbaa_alloc:
  2303. free ( xhci->slot );
  2304. err_slot_alloc:
  2305. return rc;
  2306. }
  2307. /**
  2308. * Close USB bus
  2309. *
  2310. * @v bus USB bus
  2311. */
  2312. static void xhci_bus_close ( struct usb_bus *bus ) {
  2313. struct xhci_device *xhci = usb_bus_get_hostdata ( bus );
  2314. unsigned int i;
  2315. /* Sanity checks */
  2316. assert ( xhci->slot != NULL );
  2317. for ( i = 0 ; i < xhci->slots ; i++ )
  2318. assert ( xhci->slot[i] == NULL );
  2319. xhci_stop ( xhci );
  2320. xhci_event_free ( xhci );
  2321. xhci_command_free ( xhci );
  2322. xhci_scratchpad_free ( xhci );
  2323. xhci_dcbaa_free ( xhci );
  2324. free ( xhci->slot );
  2325. }
  2326. /**
  2327. * Poll USB bus
  2328. *
  2329. * @v bus USB bus
  2330. */
  2331. static void xhci_bus_poll ( struct usb_bus *bus ) {
  2332. struct xhci_device *xhci = usb_bus_get_hostdata ( bus );
  2333. /* Poll event ring */
  2334. xhci_event_poll ( xhci );
  2335. }
  2336. /******************************************************************************
  2337. *
  2338. * Root hub operations
  2339. *
  2340. ******************************************************************************
  2341. */
  2342. /**
  2343. * Open root hub
  2344. *
  2345. * @v hub USB hub
  2346. * @ret rc Return status code
  2347. */
  2348. static int xhci_hub_open ( struct usb_hub *hub ) {
  2349. struct usb_bus *bus = hub->bus;
  2350. struct xhci_device *xhci = usb_bus_get_hostdata ( bus );
  2351. struct usb_port *port;
  2352. uint32_t portsc;
  2353. unsigned int i;
  2354. /* Enable power to all ports */
  2355. for ( i = 1 ; i <= xhci->ports ; i++ ) {
  2356. portsc = readl ( xhci->op + XHCI_OP_PORTSC ( i ) );
  2357. portsc &= XHCI_PORTSC_PRESERVE;
  2358. portsc |= XHCI_PORTSC_PP;
  2359. writel ( portsc, xhci->op + XHCI_OP_PORTSC ( i ) );
  2360. }
  2361. /* xHCI spec requires us to potentially wait 20ms after
  2362. * enabling power to a port.
  2363. */
  2364. mdelay ( XHCI_PORT_POWER_DELAY_MS );
  2365. /* USB3 ports may power up as Disabled */
  2366. for ( i = 1 ; i <= xhci->ports ; i++ ) {
  2367. portsc = readl ( xhci->op + XHCI_OP_PORTSC ( i ) );
  2368. port = usb_port ( hub, i );
  2369. if ( ( port->protocol >= USB_PROTO_3_0 ) &&
  2370. ( ( portsc & XHCI_PORTSC_PLS_MASK ) ==
  2371. XHCI_PORTSC_PLS_DISABLED ) ) {
  2372. /* Force link state to RxDetect */
  2373. portsc &= XHCI_PORTSC_PRESERVE;
  2374. portsc |= ( XHCI_PORTSC_PLS_RXDETECT | XHCI_PORTSC_LWS);
  2375. writel ( portsc, xhci->op + XHCI_OP_PORTSC ( i ) );
  2376. }
  2377. }
  2378. /* Record hub driver private data */
  2379. usb_hub_set_drvdata ( hub, xhci );
  2380. return 0;
  2381. }
  2382. /**
  2383. * Close root hub
  2384. *
  2385. * @v hub USB hub
  2386. */
  2387. static void xhci_hub_close ( struct usb_hub *hub ) {
  2388. /* Clear hub driver private data */
  2389. usb_hub_set_drvdata ( hub, NULL );
  2390. }
  2391. /**
  2392. * Enable port
  2393. *
  2394. * @v hub USB hub
  2395. * @v port USB port
  2396. * @ret rc Return status code
  2397. */
  2398. static int xhci_hub_enable ( struct usb_hub *hub, struct usb_port *port ) {
  2399. struct xhci_device *xhci = usb_hub_get_drvdata ( hub );
  2400. uint32_t portsc;
  2401. unsigned int i;
  2402. /* Reset port if applicable */
  2403. if ( port->protocol < USB_PROTO_3_0 ) {
  2404. portsc = readl ( xhci->op + XHCI_OP_PORTSC ( port->address ) );
  2405. portsc &= XHCI_PORTSC_PRESERVE;
  2406. portsc |= XHCI_PORTSC_PR;
  2407. writel ( portsc, xhci->op + XHCI_OP_PORTSC ( port->address ) );
  2408. }
  2409. /* Wait for port to become enabled */
  2410. for ( i = 0 ; i < XHCI_PORT_RESET_MAX_WAIT_MS ; i++ ) {
  2411. /* Check port status */
  2412. portsc = readl ( xhci->op + XHCI_OP_PORTSC ( port->address ) );
  2413. if ( portsc & XHCI_PORTSC_PED )
  2414. return 0;
  2415. /* Delay */
  2416. mdelay ( 1 );
  2417. }
  2418. DBGC ( xhci, "XHCI %p timed out waiting for port %d to enable\n",
  2419. xhci, port->address );
  2420. return -ETIMEDOUT;
  2421. }
  2422. /**
  2423. * Disable port
  2424. *
  2425. * @v hub USB hub
  2426. * @v port USB port
  2427. * @ret rc Return status code
  2428. */
  2429. static int xhci_hub_disable ( struct usb_hub *hub, struct usb_port *port ) {
  2430. struct xhci_device *xhci = usb_hub_get_drvdata ( hub );
  2431. uint32_t portsc;
  2432. /* Disable port */
  2433. portsc = readl ( xhci->op + XHCI_OP_PORTSC ( port->address ) );
  2434. portsc &= XHCI_PORTSC_PRESERVE;
  2435. portsc |= XHCI_PORTSC_PED;
  2436. writel ( portsc, xhci->op + XHCI_OP_PORTSC ( port->address ) );
  2437. return 0;
  2438. }
  2439. /**
  2440. * Update root hub port speed
  2441. *
  2442. * @v hub USB hub
  2443. * @v port USB port
  2444. * @ret rc Return status code
  2445. */
  2446. static int xhci_hub_speed ( struct usb_hub *hub, struct usb_port *port ) {
  2447. struct xhci_device *xhci = usb_hub_get_drvdata ( hub );
  2448. uint32_t portsc;
  2449. unsigned int psiv;
  2450. int ccs;
  2451. int ped;
  2452. int speed;
  2453. int rc;
  2454. /* Read port status */
  2455. portsc = readl ( xhci->op + XHCI_OP_PORTSC ( port->address ) );
  2456. DBGC2 ( xhci, "XHCI %p port %d status is %08x\n",
  2457. xhci, port->address, portsc );
  2458. /* Check whether or not port is connected */
  2459. ccs = ( portsc & XHCI_PORTSC_CCS );
  2460. if ( ! ccs ) {
  2461. port->speed = USB_SPEED_NONE;
  2462. return 0;
  2463. }
  2464. /* For USB2 ports, the PSIV field is not valid until the port
  2465. * completes reset and becomes enabled.
  2466. */
  2467. ped = ( portsc & XHCI_PORTSC_PED );
  2468. if ( ( port->protocol < USB_PROTO_3_0 ) && ! ped ) {
  2469. port->speed = USB_SPEED_FULL;
  2470. return 0;
  2471. }
  2472. /* Get port speed and map to generic USB speed */
  2473. psiv = XHCI_PORTSC_PSIV ( portsc );
  2474. speed = xhci_port_speed ( xhci, port->address, psiv );
  2475. if ( speed < 0 ) {
  2476. rc = speed;
  2477. return rc;
  2478. }
  2479. port->speed = speed;
  2480. return 0;
  2481. }
  2482. /******************************************************************************
  2483. *
  2484. * PCI interface
  2485. *
  2486. ******************************************************************************
  2487. */
  2488. /** USB host controller operations */
  2489. static struct usb_host_operations xhci_operations = {
  2490. .endpoint = {
  2491. .open = xhci_endpoint_open,
  2492. .close = xhci_endpoint_close,
  2493. .reset = xhci_endpoint_reset,
  2494. .mtu = xhci_endpoint_mtu,
  2495. .message = xhci_endpoint_message,
  2496. .stream = xhci_endpoint_stream,
  2497. },
  2498. .device = {
  2499. .open = xhci_device_open,
  2500. .close = xhci_device_close,
  2501. .address = xhci_device_address,
  2502. },
  2503. .bus = {
  2504. .open = xhci_bus_open,
  2505. .close = xhci_bus_close,
  2506. .poll = xhci_bus_poll,
  2507. },
  2508. .hub = {
  2509. .open = xhci_hub_open,
  2510. .close = xhci_hub_close,
  2511. .enable = xhci_hub_enable,
  2512. .disable = xhci_hub_disable,
  2513. .speed = xhci_hub_speed,
  2514. },
  2515. };
  2516. /**
  2517. * Probe PCI device
  2518. *
  2519. * @v pci PCI device
  2520. * @ret rc Return status code
  2521. */
  2522. static int xhci_probe ( struct pci_device *pci ) {
  2523. struct xhci_device *xhci;
  2524. struct usb_port *port;
  2525. unsigned long bar_start;
  2526. size_t bar_size;
  2527. unsigned int i;
  2528. int rc;
  2529. /* Allocate and initialise structure */
  2530. xhci = zalloc ( sizeof ( *xhci ) );
  2531. if ( ! xhci ) {
  2532. rc = -ENOMEM;
  2533. goto err_alloc;
  2534. }
  2535. /* Fix up PCI device */
  2536. adjust_pci_device ( pci );
  2537. /* Map registers */
  2538. bar_start = pci_bar_start ( pci, XHCI_BAR );
  2539. bar_size = pci_bar_size ( pci, XHCI_BAR );
  2540. xhci->regs = ioremap ( bar_start, bar_size );
  2541. if ( ! xhci->regs ) {
  2542. rc = -ENODEV;
  2543. goto err_ioremap;
  2544. }
  2545. /* Initialise xHCI device */
  2546. xhci_init ( xhci, xhci->regs );
  2547. /* Initialise USB legacy support and claim ownership */
  2548. xhci_legacy_init ( xhci );
  2549. if ( ( rc = xhci_legacy_claim ( xhci ) ) != 0 )
  2550. goto err_legacy_claim;
  2551. /* Reset device */
  2552. if ( ( rc = xhci_reset ( xhci ) ) != 0 )
  2553. goto err_reset;
  2554. /* Allocate USB bus */
  2555. xhci->bus = alloc_usb_bus ( &pci->dev, xhci->ports,
  2556. &xhci_operations );
  2557. if ( ! xhci->bus ) {
  2558. rc = -ENOMEM;
  2559. goto err_alloc_bus;
  2560. }
  2561. usb_bus_set_hostdata ( xhci->bus, xhci );
  2562. usb_hub_set_drvdata ( xhci->bus->hub, xhci );
  2563. /* Set port protocols */
  2564. for ( i = 1 ; i <= xhci->ports ; i++ ) {
  2565. port = usb_port ( xhci->bus->hub, i );
  2566. port->protocol = xhci_port_protocol ( xhci, i );
  2567. }
  2568. /* Register USB bus */
  2569. if ( ( rc = register_usb_bus ( xhci->bus ) ) != 0 )
  2570. goto err_register;
  2571. pci_set_drvdata ( pci, xhci );
  2572. return 0;
  2573. unregister_usb_bus ( xhci->bus );
  2574. err_register:
  2575. free_usb_bus ( xhci->bus );
  2576. err_alloc_bus:
  2577. xhci_reset ( xhci );
  2578. err_reset:
  2579. xhci_legacy_release ( xhci );
  2580. err_legacy_claim:
  2581. iounmap ( xhci->regs );
  2582. err_ioremap:
  2583. free ( xhci );
  2584. err_alloc:
  2585. return rc;
  2586. }
  2587. /**
  2588. * Remove PCI device
  2589. *
  2590. * @v pci PCI device
  2591. */
  2592. static void xhci_remove ( struct pci_device *pci ) {
  2593. struct xhci_device *xhci = pci_get_drvdata ( pci );
  2594. struct usb_bus *bus = xhci->bus;
  2595. unregister_usb_bus ( bus );
  2596. free_usb_bus ( bus );
  2597. xhci_reset ( xhci );
  2598. xhci_legacy_release ( xhci );
  2599. iounmap ( xhci->regs );
  2600. free ( xhci );
  2601. }
  2602. /** XHCI PCI device IDs */
  2603. static struct pci_device_id xhci_ids[] = {
  2604. PCI_ROM ( 0xffff, 0xffff, "xhci", "xHCI", 0 ),
  2605. };
  2606. /** XHCI PCI driver */
  2607. struct pci_driver xhci_driver __pci_driver = {
  2608. .ids = xhci_ids,
  2609. .id_count = ( sizeof ( xhci_ids ) / sizeof ( xhci_ids[0] ) ),
  2610. .class = PCI_CLASS ( PCI_CLASS_SERIAL, PCI_CLASS_SERIAL_USB,
  2611. PCI_CLASS_SERIAL_USB_XHCI ),
  2612. .probe = xhci_probe,
  2613. .remove = xhci_remove,
  2614. };