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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069
  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. * Handle host controller event
  1413. *
  1414. * @v xhci xHCI device
  1415. * @v host Host controller event
  1416. */
  1417. static void xhci_host_controller ( struct xhci_device *xhci,
  1418. struct xhci_trb_host_controller *host ) {
  1419. int rc;
  1420. /* Construct error */
  1421. rc = -ECODE ( host->code );
  1422. DBGC ( xhci, "XHCI %p host controller event (code %d): %s\n",
  1423. xhci, host->code, strerror ( rc ) );
  1424. }
  1425. /**
  1426. * Poll event ring
  1427. *
  1428. * @v xhci xHCI device
  1429. */
  1430. static void xhci_event_poll ( struct xhci_device *xhci ) {
  1431. struct xhci_event_ring *event = &xhci->event;
  1432. union xhci_trb *trb;
  1433. unsigned int shift = XHCI_EVENT_TRBS_LOG2;
  1434. unsigned int count = ( 1 << shift );
  1435. unsigned int mask = ( count - 1 );
  1436. unsigned int consumed;
  1437. unsigned int type;
  1438. /* Poll for events */
  1439. profile_start ( &xhci_event_profiler );
  1440. for ( consumed = 0 ; ; consumed++ ) {
  1441. /* Stop if we reach an empty TRB */
  1442. rmb();
  1443. trb = &event->trb[ event->cons & mask ];
  1444. if ( ! ( ( trb->common.flags ^
  1445. ( event->cons >> shift ) ) & XHCI_TRB_C ) )
  1446. break;
  1447. /* Handle TRB */
  1448. type = ( trb->common.type & XHCI_TRB_TYPE_MASK );
  1449. switch ( type ) {
  1450. case XHCI_TRB_TRANSFER :
  1451. xhci_transfer ( xhci, &trb->transfer );
  1452. break;
  1453. case XHCI_TRB_COMPLETE :
  1454. xhci_complete ( xhci, &trb->complete );
  1455. break;
  1456. case XHCI_TRB_PORT_STATUS:
  1457. xhci_port_status ( xhci, &trb->port );
  1458. break;
  1459. case XHCI_TRB_HOST_CONTROLLER:
  1460. xhci_host_controller ( xhci, &trb->host );
  1461. break;
  1462. default:
  1463. DBGC ( xhci, "XHCI %p unrecognised event %#x\n:",
  1464. xhci, event->cons );
  1465. DBGC_HDA ( xhci, virt_to_phys ( trb ),
  1466. trb, sizeof ( *trb ) );
  1467. break;
  1468. }
  1469. /* Consume this TRB */
  1470. event->cons++;
  1471. }
  1472. /* Update dequeue pointer if applicable */
  1473. if ( consumed ) {
  1474. xhci_writeq ( xhci, virt_to_phys ( trb ),
  1475. xhci->run + XHCI_RUN_ERDP ( 0 ) );
  1476. profile_stop ( &xhci_event_profiler );
  1477. }
  1478. }
  1479. /**
  1480. * Issue command and wait for completion
  1481. *
  1482. * @v xhci xHCI device
  1483. * @v trb Transfer request block (with empty Cycle flag)
  1484. * @ret rc Return status code
  1485. *
  1486. * On a successful completion, the TRB will be overwritten with the
  1487. * completion.
  1488. */
  1489. static int xhci_command ( struct xhci_device *xhci, union xhci_trb *trb ) {
  1490. struct xhci_trb_complete *complete = &trb->complete;
  1491. unsigned int i;
  1492. int rc;
  1493. /* Record the completion buffer */
  1494. xhci->completion = trb;
  1495. /* Enqueue the command */
  1496. if ( ( rc = xhci_enqueue ( &xhci->command, NULL, trb ) ) != 0 )
  1497. goto err_enqueue;
  1498. /* Ring the command doorbell */
  1499. xhci_doorbell ( &xhci->command );
  1500. /* Wait for the command to complete */
  1501. for ( i = 0 ; i < XHCI_COMMAND_MAX_WAIT_MS ; i++ ) {
  1502. /* Poll event ring */
  1503. xhci_event_poll ( xhci );
  1504. /* Check for completion */
  1505. if ( ! xhci->completion ) {
  1506. if ( complete->code != XHCI_CMPLT_SUCCESS ) {
  1507. rc = -ECODE ( complete->code );
  1508. DBGC ( xhci, "XHCI %p command failed (code "
  1509. "%d): %s\n", xhci, complete->code,
  1510. strerror ( rc ) );
  1511. DBGC_HDA ( xhci, 0, trb, sizeof ( *trb ) );
  1512. return rc;
  1513. }
  1514. return 0;
  1515. }
  1516. /* Delay */
  1517. mdelay ( 1 );
  1518. }
  1519. /* Timeout */
  1520. DBGC ( xhci, "XHCI %p timed out waiting for completion\n", xhci );
  1521. rc = -ETIMEDOUT;
  1522. err_enqueue:
  1523. xhci->completion = NULL;
  1524. return rc;
  1525. }
  1526. /**
  1527. * Issue NOP and wait for completion
  1528. *
  1529. * @v xhci xHCI device
  1530. * @ret rc Return status code
  1531. */
  1532. static inline int xhci_nop ( struct xhci_device *xhci ) {
  1533. union xhci_trb trb;
  1534. struct xhci_trb_common *nop = &trb.common;
  1535. int rc;
  1536. /* Construct command */
  1537. memset ( nop, 0, sizeof ( *nop ) );
  1538. nop->flags = XHCI_TRB_IOC;
  1539. nop->type = XHCI_TRB_NOP_CMD;
  1540. /* Issue command and wait for completion */
  1541. if ( ( rc = xhci_command ( xhci, &trb ) ) != 0 )
  1542. return rc;
  1543. return 0;
  1544. }
  1545. /**
  1546. * Enable slot
  1547. *
  1548. * @v xhci xHCI device
  1549. * @v type Slot type
  1550. * @ret slot Device slot ID, or negative error
  1551. */
  1552. static inline int xhci_enable_slot ( struct xhci_device *xhci,
  1553. unsigned int type ) {
  1554. union xhci_trb trb;
  1555. struct xhci_trb_enable_slot *enable = &trb.enable;
  1556. struct xhci_trb_complete *enabled = &trb.complete;
  1557. unsigned int slot;
  1558. int rc;
  1559. /* Construct command */
  1560. memset ( enable, 0, sizeof ( *enable ) );
  1561. enable->slot = type;
  1562. enable->type = XHCI_TRB_ENABLE_SLOT;
  1563. /* Issue command and wait for completion */
  1564. if ( ( rc = xhci_command ( xhci, &trb ) ) != 0 ) {
  1565. DBGC ( xhci, "XHCI %p could not enable new slot: %s\n",
  1566. xhci, strerror ( rc ) );
  1567. return rc;
  1568. }
  1569. /* Extract slot number */
  1570. slot = enabled->slot;
  1571. DBGC2 ( xhci, "XHCI %p slot %d enabled\n", xhci, slot );
  1572. return slot;
  1573. }
  1574. /**
  1575. * Disable slot
  1576. *
  1577. * @v xhci xHCI device
  1578. * @v slot Device slot
  1579. * @ret rc Return status code
  1580. */
  1581. static inline int xhci_disable_slot ( struct xhci_device *xhci,
  1582. unsigned int slot ) {
  1583. union xhci_trb trb;
  1584. struct xhci_trb_disable_slot *disable = &trb.disable;
  1585. int rc;
  1586. /* Construct command */
  1587. memset ( disable, 0, sizeof ( *disable ) );
  1588. disable->type = XHCI_TRB_DISABLE_SLOT;
  1589. disable->slot = slot;
  1590. /* Issue command and wait for completion */
  1591. if ( ( rc = xhci_command ( xhci, &trb ) ) != 0 ) {
  1592. DBGC ( xhci, "XHCI %p could not disable slot %d: %s\n",
  1593. xhci, slot, strerror ( rc ) );
  1594. return rc;
  1595. }
  1596. DBGC2 ( xhci, "XHCI %p slot %d disabled\n", xhci, slot );
  1597. return 0;
  1598. }
  1599. /**
  1600. * Issue context-based command and wait for completion
  1601. *
  1602. * @v xhci xHCI device
  1603. * @v slot Device slot
  1604. * @v endpoint Endpoint
  1605. * @v type TRB type
  1606. * @v populate Input context populater
  1607. * @ret rc Return status code
  1608. */
  1609. static int xhci_context ( struct xhci_device *xhci, struct xhci_slot *slot,
  1610. struct xhci_endpoint *endpoint, unsigned int type,
  1611. void ( * populate ) ( struct xhci_device *xhci,
  1612. struct xhci_slot *slot,
  1613. struct xhci_endpoint *endpoint,
  1614. void *input ) ) {
  1615. union xhci_trb trb;
  1616. struct xhci_trb_context *context = &trb.context;
  1617. size_t len;
  1618. void *input;
  1619. int rc;
  1620. /* Allocate an input context */
  1621. len = xhci_input_context_offset ( xhci, XHCI_CTX_END );
  1622. input = malloc_dma ( len, xhci_align ( len ) );
  1623. if ( ! input ) {
  1624. rc = -ENOMEM;
  1625. goto err_alloc;
  1626. }
  1627. memset ( input, 0, len );
  1628. /* Populate input context */
  1629. populate ( xhci, slot, endpoint, input );
  1630. /* Construct command */
  1631. memset ( context, 0, sizeof ( *context ) );
  1632. context->type = type;
  1633. context->input = cpu_to_le64 ( virt_to_phys ( input ) );
  1634. context->slot = slot->id;
  1635. /* Issue command and wait for completion */
  1636. if ( ( rc = xhci_command ( xhci, &trb ) ) != 0 )
  1637. goto err_command;
  1638. err_command:
  1639. free_dma ( input, len );
  1640. err_alloc:
  1641. return rc;
  1642. }
  1643. /**
  1644. * Populate address device input context
  1645. *
  1646. * @v xhci xHCI device
  1647. * @v slot Device slot
  1648. * @v endpoint Endpoint
  1649. * @v input Input context
  1650. */
  1651. static void xhci_address_device_input ( struct xhci_device *xhci,
  1652. struct xhci_slot *slot,
  1653. struct xhci_endpoint *endpoint,
  1654. void *input ) {
  1655. struct xhci_control_context *control_ctx;
  1656. struct xhci_slot_context *slot_ctx;
  1657. struct xhci_endpoint_context *ep_ctx;
  1658. /* Sanity checks */
  1659. assert ( endpoint->ctx == XHCI_CTX_EP0 );
  1660. /* Populate control context */
  1661. control_ctx = input;
  1662. control_ctx->add = cpu_to_le32 ( ( 1 << XHCI_CTX_SLOT ) |
  1663. ( 1 << XHCI_CTX_EP0 ) );
  1664. /* Populate slot context */
  1665. slot_ctx = ( input + xhci_input_context_offset ( xhci, XHCI_CTX_SLOT ));
  1666. slot_ctx->info = cpu_to_le32 ( XHCI_SLOT_INFO ( 1, 0, slot->psiv,
  1667. slot->route ) );
  1668. slot_ctx->port = slot->port;
  1669. /* Populate control endpoint context */
  1670. ep_ctx = ( input + xhci_input_context_offset ( xhci, XHCI_CTX_EP0 ) );
  1671. ep_ctx->type = XHCI_EP_TYPE_CONTROL;
  1672. ep_ctx->burst = endpoint->ep->burst;
  1673. ep_ctx->mtu = cpu_to_le16 ( endpoint->ep->mtu );
  1674. ep_ctx->dequeue = cpu_to_le64 ( virt_to_phys ( endpoint->ring.trb ) |
  1675. XHCI_EP_DCS );
  1676. ep_ctx->trb_len = cpu_to_le16 ( XHCI_EP0_TRB_LEN );
  1677. }
  1678. /**
  1679. * Address device
  1680. *
  1681. * @v xhci xHCI device
  1682. * @v slot Device slot
  1683. * @ret rc Return status code
  1684. */
  1685. static inline int xhci_address_device ( struct xhci_device *xhci,
  1686. struct xhci_slot *slot ) {
  1687. struct usb_device *usb = slot->usb;
  1688. struct xhci_slot_context *slot_ctx;
  1689. int rc;
  1690. /* Assign device address */
  1691. if ( ( rc = xhci_context ( xhci, slot, slot->endpoint[XHCI_CTX_EP0],
  1692. XHCI_TRB_ADDRESS_DEVICE,
  1693. xhci_address_device_input ) ) != 0 )
  1694. return rc;
  1695. /* Get assigned address */
  1696. slot_ctx = ( slot->context +
  1697. xhci_device_context_offset ( xhci, XHCI_CTX_SLOT ) );
  1698. usb->address = slot_ctx->address;
  1699. DBGC2 ( xhci, "XHCI %p assigned address %d to %s\n",
  1700. xhci, usb->address, usb->name );
  1701. return 0;
  1702. }
  1703. /**
  1704. * Populate configure endpoint input context
  1705. *
  1706. * @v xhci xHCI device
  1707. * @v slot Device slot
  1708. * @v endpoint Endpoint
  1709. * @v input Input context
  1710. */
  1711. static void xhci_configure_endpoint_input ( struct xhci_device *xhci,
  1712. struct xhci_slot *slot __unused,
  1713. struct xhci_endpoint *endpoint,
  1714. void *input ) {
  1715. struct xhci_control_context *control_ctx;
  1716. struct xhci_slot_context *slot_ctx;
  1717. struct xhci_endpoint_context *ep_ctx;
  1718. /* Populate control context */
  1719. control_ctx = input;
  1720. control_ctx->add = cpu_to_le32 ( ( 1 << XHCI_CTX_SLOT ) |
  1721. ( 1 << endpoint->ctx ) );
  1722. /* Populate slot context */
  1723. slot_ctx = ( input + xhci_input_context_offset ( xhci, XHCI_CTX_SLOT ));
  1724. slot_ctx->info = cpu_to_le32 ( XHCI_SLOT_INFO ( ( XHCI_CTX_END - 1 ),
  1725. 0, 0, 0 ) );
  1726. /* Populate endpoint context */
  1727. ep_ctx = ( input + xhci_input_context_offset ( xhci, endpoint->ctx ) );
  1728. ep_ctx->interval = endpoint->interval;
  1729. ep_ctx->type = endpoint->type;
  1730. ep_ctx->burst = endpoint->ep->burst;
  1731. ep_ctx->mtu = cpu_to_le16 ( endpoint->ep->mtu );
  1732. ep_ctx->dequeue = cpu_to_le64 ( virt_to_phys ( endpoint->ring.trb ) |
  1733. XHCI_EP_DCS );
  1734. ep_ctx->trb_len = cpu_to_le16 ( endpoint->ep->mtu ); /* best guess */
  1735. }
  1736. /**
  1737. * Configure endpoint
  1738. *
  1739. * @v xhci xHCI device
  1740. * @v slot Device slot
  1741. * @v endpoint Endpoint
  1742. * @ret rc Return status code
  1743. */
  1744. static inline int xhci_configure_endpoint ( struct xhci_device *xhci,
  1745. struct xhci_slot *slot,
  1746. struct xhci_endpoint *endpoint ) {
  1747. int rc;
  1748. /* Configure endpoint */
  1749. if ( ( rc = xhci_context ( xhci, slot, endpoint,
  1750. XHCI_TRB_CONFIGURE_ENDPOINT,
  1751. xhci_configure_endpoint_input ) ) != 0 )
  1752. return rc;
  1753. DBGC2 ( xhci, "XHCI %p slot %d ctx %d configured\n",
  1754. xhci, slot->id, endpoint->ctx );
  1755. return 0;
  1756. }
  1757. /**
  1758. * Populate deconfigure endpoint input context
  1759. *
  1760. * @v xhci xHCI device
  1761. * @v slot Device slot
  1762. * @v endpoint Endpoint
  1763. * @v input Input context
  1764. */
  1765. static void
  1766. xhci_deconfigure_endpoint_input ( struct xhci_device *xhci __unused,
  1767. struct xhci_slot *slot __unused,
  1768. struct xhci_endpoint *endpoint,
  1769. void *input ) {
  1770. struct xhci_control_context *control_ctx;
  1771. struct xhci_slot_context *slot_ctx;
  1772. /* Populate control context */
  1773. control_ctx = input;
  1774. control_ctx->add = cpu_to_le32 ( 1 << XHCI_CTX_SLOT );
  1775. control_ctx->drop = cpu_to_le32 ( 1 << endpoint->ctx );
  1776. /* Populate slot context */
  1777. slot_ctx = ( input + xhci_input_context_offset ( xhci, XHCI_CTX_SLOT ));
  1778. slot_ctx->info = cpu_to_le32 ( XHCI_SLOT_INFO ( ( XHCI_CTX_END - 1 ),
  1779. 0, 0, 0 ) );
  1780. }
  1781. /**
  1782. * Deconfigure endpoint
  1783. *
  1784. * @v xhci xHCI device
  1785. * @v slot Device slot
  1786. * @v endpoint Endpoint
  1787. * @ret rc Return status code
  1788. */
  1789. static inline int xhci_deconfigure_endpoint ( struct xhci_device *xhci,
  1790. struct xhci_slot *slot,
  1791. struct xhci_endpoint *endpoint ) {
  1792. int rc;
  1793. /* Deconfigure endpoint */
  1794. if ( ( rc = xhci_context ( xhci, slot, endpoint,
  1795. XHCI_TRB_CONFIGURE_ENDPOINT,
  1796. xhci_deconfigure_endpoint_input ) ) != 0 )
  1797. return rc;
  1798. DBGC2 ( xhci, "XHCI %p slot %d ctx %d deconfigured\n",
  1799. xhci, slot->id, endpoint->ctx );
  1800. return 0;
  1801. }
  1802. /**
  1803. * Populate evaluate context input context
  1804. *
  1805. * @v xhci xHCI device
  1806. * @v slot Device slot
  1807. * @v endpoint Endpoint
  1808. * @v input Input context
  1809. */
  1810. static void xhci_evaluate_context_input ( struct xhci_device *xhci,
  1811. struct xhci_slot *slot __unused,
  1812. struct xhci_endpoint *endpoint,
  1813. void *input ) {
  1814. struct xhci_control_context *control_ctx;
  1815. struct xhci_slot_context *slot_ctx;
  1816. struct xhci_endpoint_context *ep_ctx;
  1817. /* Populate control context */
  1818. control_ctx = input;
  1819. control_ctx->add = cpu_to_le32 ( ( 1 << XHCI_CTX_SLOT ) |
  1820. ( 1 << endpoint->ctx ) );
  1821. /* Populate slot context */
  1822. slot_ctx = ( input + xhci_input_context_offset ( xhci, XHCI_CTX_SLOT ));
  1823. slot_ctx->info = cpu_to_le32 ( XHCI_SLOT_INFO ( ( XHCI_CTX_END - 1 ),
  1824. 0, 0, 0 ) );
  1825. /* Populate endpoint context */
  1826. ep_ctx = ( input + xhci_input_context_offset ( xhci, endpoint->ctx ) );
  1827. ep_ctx->mtu = cpu_to_le16 ( endpoint->ep->mtu );
  1828. }
  1829. /**
  1830. * Evaluate context
  1831. *
  1832. * @v xhci xHCI device
  1833. * @v slot Device slot
  1834. * @v endpoint Endpoint
  1835. * @ret rc Return status code
  1836. */
  1837. static inline int xhci_evaluate_context ( struct xhci_device *xhci,
  1838. struct xhci_slot *slot,
  1839. struct xhci_endpoint *endpoint ) {
  1840. int rc;
  1841. /* Configure endpoint */
  1842. if ( ( rc = xhci_context ( xhci, slot, endpoint,
  1843. XHCI_TRB_EVALUATE_CONTEXT,
  1844. xhci_evaluate_context_input ) ) != 0 )
  1845. return rc;
  1846. DBGC2 ( xhci, "XHCI %p slot %d ctx %d (re-)evaluated\n",
  1847. xhci, slot->id, endpoint->ctx );
  1848. return 0;
  1849. }
  1850. /**
  1851. * Reset endpoint
  1852. *
  1853. * @v xhci xHCI device
  1854. * @v slot Device slot
  1855. * @v endpoint Endpoint
  1856. * @ret rc Return status code
  1857. */
  1858. static inline int xhci_reset_endpoint ( struct xhci_device *xhci,
  1859. struct xhci_slot *slot,
  1860. struct xhci_endpoint *endpoint ) {
  1861. union xhci_trb trb;
  1862. struct xhci_trb_reset_endpoint *reset = &trb.reset;
  1863. int rc;
  1864. /* Construct command */
  1865. memset ( reset, 0, sizeof ( *reset ) );
  1866. reset->slot = slot->id;
  1867. reset->endpoint = endpoint->ctx;
  1868. reset->type = XHCI_TRB_RESET_ENDPOINT;
  1869. /* Issue command and wait for completion */
  1870. if ( ( rc = xhci_command ( xhci, &trb ) ) != 0 ) {
  1871. DBGC ( xhci, "XHCI %p slot %d ctx %d could not reset endpoint "
  1872. "in state %d: %s\n", xhci, slot->id, endpoint->ctx,
  1873. endpoint->context->state, strerror ( rc ) );
  1874. return rc;
  1875. }
  1876. return 0;
  1877. }
  1878. /**
  1879. * Stop endpoint
  1880. *
  1881. * @v xhci xHCI device
  1882. * @v slot Device slot
  1883. * @v endpoint Endpoint
  1884. * @ret rc Return status code
  1885. */
  1886. static inline int xhci_stop_endpoint ( struct xhci_device *xhci,
  1887. struct xhci_slot *slot,
  1888. struct xhci_endpoint *endpoint ) {
  1889. union xhci_trb trb;
  1890. struct xhci_trb_stop_endpoint *stop = &trb.stop;
  1891. int rc;
  1892. /* Construct command */
  1893. memset ( stop, 0, sizeof ( *stop ) );
  1894. stop->slot = slot->id;
  1895. stop->endpoint = endpoint->ctx;
  1896. stop->type = XHCI_TRB_STOP_ENDPOINT;
  1897. /* Issue command and wait for completion */
  1898. if ( ( rc = xhci_command ( xhci, &trb ) ) != 0 ) {
  1899. DBGC ( xhci, "XHCI %p slot %d ctx %d could not stop endpoint "
  1900. "in state %d: %s\n", xhci, slot->id, endpoint->ctx,
  1901. endpoint->context->state, strerror ( rc ) );
  1902. return rc;
  1903. }
  1904. return 0;
  1905. }
  1906. /**
  1907. * Set transfer ring dequeue pointer
  1908. *
  1909. * @v xhci xHCI device
  1910. * @v slot Device slot
  1911. * @v endpoint Endpoint
  1912. * @ret rc Return status code
  1913. */
  1914. static inline int
  1915. xhci_set_tr_dequeue_pointer ( struct xhci_device *xhci,
  1916. struct xhci_slot *slot,
  1917. struct xhci_endpoint *endpoint ) {
  1918. union xhci_trb trb;
  1919. struct xhci_trb_set_tr_dequeue_pointer *dequeue = &trb.dequeue;
  1920. struct xhci_trb_ring *ring = &endpoint->ring;
  1921. unsigned int cons;
  1922. unsigned int mask;
  1923. unsigned int index;
  1924. unsigned int dcs;
  1925. int rc;
  1926. /* Construct command */
  1927. memset ( dequeue, 0, sizeof ( *dequeue ) );
  1928. cons = ring->cons;
  1929. mask = ring->mask;
  1930. dcs = ( ( ~( cons >> ring->shift ) ) & XHCI_EP_DCS );
  1931. index = ( cons & mask );
  1932. dequeue->dequeue =
  1933. cpu_to_le64 ( virt_to_phys ( &ring->trb[index] ) | dcs );
  1934. dequeue->slot = slot->id;
  1935. dequeue->endpoint = endpoint->ctx;
  1936. dequeue->type = XHCI_TRB_SET_TR_DEQUEUE_POINTER;
  1937. /* Issue command and wait for completion */
  1938. if ( ( rc = xhci_command ( xhci, &trb ) ) != 0 ) {
  1939. DBGC ( xhci, "XHCI %p slot %d ctx %d could not set TR dequeue "
  1940. "pointer in state %d: %s\n", xhci, slot->id,
  1941. endpoint->ctx, endpoint->context->state, strerror ( rc));
  1942. return rc;
  1943. }
  1944. return 0;
  1945. }
  1946. /******************************************************************************
  1947. *
  1948. * Endpoint operations
  1949. *
  1950. ******************************************************************************
  1951. */
  1952. /**
  1953. * Open endpoint
  1954. *
  1955. * @v ep USB endpoint
  1956. * @ret rc Return status code
  1957. */
  1958. static int xhci_endpoint_open ( struct usb_endpoint *ep ) {
  1959. struct usb_device *usb = ep->usb;
  1960. struct xhci_slot *slot = usb_get_hostdata ( usb );
  1961. struct xhci_device *xhci = slot->xhci;
  1962. struct xhci_endpoint *endpoint;
  1963. unsigned int ctx;
  1964. unsigned int type;
  1965. unsigned int interval;
  1966. int rc;
  1967. /* Calculate context index */
  1968. ctx = XHCI_CTX ( ep->address );
  1969. assert ( slot->endpoint[ctx] == NULL );
  1970. /* Calculate endpoint type */
  1971. type = XHCI_EP_TYPE ( ep->attributes & USB_ENDPOINT_ATTR_TYPE_MASK );
  1972. if ( type == XHCI_EP_TYPE ( USB_ENDPOINT_ATTR_CONTROL ) )
  1973. type = XHCI_EP_TYPE_CONTROL;
  1974. if ( ep->address & USB_DIR_IN )
  1975. type |= XHCI_EP_TYPE_IN;
  1976. /* Calculate interval */
  1977. if ( type & XHCI_EP_TYPE_PERIODIC ) {
  1978. interval = ( fls ( ep->interval ) - 1 );
  1979. } else {
  1980. interval = ep->interval;
  1981. }
  1982. /* Allocate and initialise structure */
  1983. endpoint = zalloc ( sizeof ( *endpoint ) );
  1984. if ( ! endpoint ) {
  1985. rc = -ENOMEM;
  1986. goto err_alloc;
  1987. }
  1988. usb_endpoint_set_hostdata ( ep, endpoint );
  1989. slot->endpoint[ctx] = endpoint;
  1990. endpoint->xhci = xhci;
  1991. endpoint->slot = slot;
  1992. endpoint->ep = ep;
  1993. endpoint->ctx = ctx;
  1994. endpoint->type = type;
  1995. endpoint->interval = interval;
  1996. endpoint->context = ( ( ( void * ) slot->context ) +
  1997. xhci_device_context_offset ( xhci, ctx ) );
  1998. /* Allocate transfer ring */
  1999. if ( ( rc = xhci_ring_alloc ( xhci, &endpoint->ring,
  2000. XHCI_TRANSFER_TRBS_LOG2,
  2001. slot->id, ctx, 0 ) ) != 0 )
  2002. goto err_ring_alloc;
  2003. /* Configure endpoint, if applicable */
  2004. if ( ( ctx != XHCI_CTX_EP0 ) &&
  2005. ( ( rc = xhci_configure_endpoint ( xhci, slot, endpoint ) ) != 0 ))
  2006. goto err_configure_endpoint;
  2007. DBGC2 ( xhci, "XHCI %p slot %d ctx %d ring [%08lx,%08lx)\n",
  2008. xhci, slot->id, ctx, virt_to_phys ( endpoint->ring.trb ),
  2009. ( virt_to_phys ( endpoint->ring.trb ) + endpoint->ring.len ) );
  2010. return 0;
  2011. xhci_deconfigure_endpoint ( xhci, slot, endpoint );
  2012. err_configure_endpoint:
  2013. xhci_ring_free ( &endpoint->ring );
  2014. err_ring_alloc:
  2015. slot->endpoint[ctx] = NULL;
  2016. free ( endpoint );
  2017. err_alloc:
  2018. return rc;
  2019. }
  2020. /**
  2021. * Close endpoint
  2022. *
  2023. * @v ep USB endpoint
  2024. */
  2025. static void xhci_endpoint_close ( struct usb_endpoint *ep ) {
  2026. struct xhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
  2027. struct xhci_slot *slot = endpoint->slot;
  2028. struct xhci_device *xhci = slot->xhci;
  2029. struct io_buffer *iobuf;
  2030. unsigned int ctx = endpoint->ctx;
  2031. /* Deconfigure endpoint, if applicable */
  2032. if ( ctx != XHCI_CTX_EP0 )
  2033. xhci_deconfigure_endpoint ( xhci, slot, endpoint );
  2034. /* Cancel any incomplete transfers */
  2035. while ( xhci_ring_fill ( &endpoint->ring ) ) {
  2036. iobuf = xhci_dequeue_multi ( &endpoint->ring );
  2037. usb_complete_err ( ep, iobuf, -ECANCELED );
  2038. }
  2039. /* Free endpoint */
  2040. xhci_ring_free ( &endpoint->ring );
  2041. slot->endpoint[ctx] = NULL;
  2042. free ( endpoint );
  2043. }
  2044. /**
  2045. * Reset endpoint
  2046. *
  2047. * @v ep USB endpoint
  2048. * @ret rc Return status code
  2049. */
  2050. static int xhci_endpoint_reset ( struct usb_endpoint *ep ) {
  2051. struct xhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
  2052. struct xhci_slot *slot = endpoint->slot;
  2053. struct xhci_device *xhci = slot->xhci;
  2054. int rc;
  2055. /* Reset endpoint context */
  2056. if ( ( rc = xhci_reset_endpoint ( xhci, slot, endpoint ) ) != 0 )
  2057. return rc;
  2058. /* Set transfer ring dequeue pointer */
  2059. if ( ( rc = xhci_set_tr_dequeue_pointer ( xhci, slot, endpoint ) ) != 0)
  2060. return rc;
  2061. DBGC ( xhci, "XHCI %p slot %d ctx %d reset\n",
  2062. xhci, slot->id, endpoint->ctx );
  2063. return 0;
  2064. }
  2065. /**
  2066. * Update MTU
  2067. *
  2068. * @v ep USB endpoint
  2069. * @ret rc Return status code
  2070. */
  2071. static int xhci_endpoint_mtu ( struct usb_endpoint *ep ) {
  2072. struct xhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
  2073. struct xhci_slot *slot = endpoint->slot;
  2074. struct xhci_device *xhci = slot->xhci;
  2075. int rc;
  2076. /* Evalulate context */
  2077. if ( ( rc = xhci_evaluate_context ( xhci, slot, endpoint ) ) != 0 )
  2078. return rc;
  2079. return 0;
  2080. }
  2081. /**
  2082. * Enqueue message transfer
  2083. *
  2084. * @v ep USB endpoint
  2085. * @v packet Setup packet
  2086. * @v iobuf I/O buffer
  2087. * @ret rc Return status code
  2088. */
  2089. static int xhci_endpoint_message ( struct usb_endpoint *ep,
  2090. struct usb_setup_packet *packet,
  2091. struct io_buffer *iobuf ) {
  2092. struct xhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
  2093. unsigned int input = ( le16_to_cpu ( packet->request ) & USB_DIR_IN );
  2094. size_t len = iob_len ( iobuf );
  2095. union xhci_trb trbs[ 1 /* setup */ + 1 /* possible data */ +
  2096. 1 /* status */ ];
  2097. union xhci_trb *trb = trbs;
  2098. struct xhci_trb_setup *setup;
  2099. struct xhci_trb_data *data;
  2100. struct xhci_trb_status *status;
  2101. int rc;
  2102. /* Profile message transfers */
  2103. profile_start ( &xhci_message_profiler );
  2104. /* Construct setup stage TRB */
  2105. memset ( trbs, 0, sizeof ( trbs ) );
  2106. setup = &(trb++)->setup;
  2107. memcpy ( &setup->packet, packet, sizeof ( setup->packet ) );
  2108. setup->len = cpu_to_le32 ( sizeof ( *packet ) );
  2109. setup->flags = XHCI_TRB_IDT;
  2110. setup->type = XHCI_TRB_SETUP;
  2111. if ( len )
  2112. setup->direction = ( input ? XHCI_SETUP_IN : XHCI_SETUP_OUT );
  2113. /* Construct data stage TRB, if applicable */
  2114. if ( len ) {
  2115. data = &(trb++)->data;
  2116. data->data = cpu_to_le64 ( virt_to_phys ( iobuf->data ) );
  2117. data->len = cpu_to_le32 ( len );
  2118. data->type = XHCI_TRB_DATA;
  2119. data->direction = ( input ? XHCI_DATA_IN : XHCI_DATA_OUT );
  2120. }
  2121. /* Construct status stage TRB */
  2122. status = &(trb++)->status;
  2123. status->flags = XHCI_TRB_IOC;
  2124. status->type = XHCI_TRB_STATUS;
  2125. status->direction =
  2126. ( ( len && input ) ? XHCI_STATUS_OUT : XHCI_STATUS_IN );
  2127. /* Enqueue TRBs */
  2128. if ( ( rc = xhci_enqueue_multi ( &endpoint->ring, iobuf, trbs,
  2129. ( trb - trbs ) ) ) != 0 )
  2130. return rc;
  2131. /* Ring the doorbell */
  2132. xhci_doorbell ( &endpoint->ring );
  2133. profile_stop ( &xhci_message_profiler );
  2134. return 0;
  2135. }
  2136. /**
  2137. * Enqueue stream transfer
  2138. *
  2139. * @v ep USB endpoint
  2140. * @v iobuf I/O buffer
  2141. * @v terminate Terminate using a short packet
  2142. * @ret rc Return status code
  2143. */
  2144. static int xhci_endpoint_stream ( struct usb_endpoint *ep,
  2145. struct io_buffer *iobuf, int terminate ) {
  2146. struct xhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
  2147. union xhci_trb trbs[ 1 /* Normal */ + 1 /* Possible zero-length */ ];
  2148. union xhci_trb *trb = trbs;
  2149. struct xhci_trb_normal *normal;
  2150. size_t len = iob_len ( iobuf );
  2151. int rc;
  2152. /* Profile stream transfers */
  2153. profile_start ( &xhci_stream_profiler );
  2154. /* Construct normal TRBs */
  2155. memset ( &trbs, 0, sizeof ( trbs ) );
  2156. normal = &(trb++)->normal;
  2157. normal->data = cpu_to_le64 ( virt_to_phys ( iobuf->data ) );
  2158. normal->len = cpu_to_le32 ( len );
  2159. normal->type = XHCI_TRB_NORMAL;
  2160. if ( terminate && ( ( len & ( ep->mtu - 1 ) ) == 0 ) ) {
  2161. normal->flags = XHCI_TRB_CH;
  2162. normal = &(trb++)->normal;
  2163. normal->type = XHCI_TRB_NORMAL;
  2164. }
  2165. normal->flags = XHCI_TRB_IOC;
  2166. /* Enqueue TRBs */
  2167. if ( ( rc = xhci_enqueue_multi ( &endpoint->ring, iobuf, trbs,
  2168. ( trb - trbs ) ) ) != 0 )
  2169. return rc;
  2170. /* Ring the doorbell */
  2171. xhci_doorbell ( &endpoint->ring );
  2172. profile_stop ( &xhci_stream_profiler );
  2173. return 0;
  2174. }
  2175. /******************************************************************************
  2176. *
  2177. * Device operations
  2178. *
  2179. ******************************************************************************
  2180. */
  2181. /**
  2182. * Open device
  2183. *
  2184. * @v usb USB device
  2185. * @ret rc Return status code
  2186. */
  2187. static int xhci_device_open ( struct usb_device *usb ) {
  2188. struct xhci_device *xhci = usb_bus_get_hostdata ( usb->port->hub->bus );
  2189. struct xhci_slot *slot;
  2190. size_t len;
  2191. int type;
  2192. int id;
  2193. int rc;
  2194. /* Determine applicable slot type */
  2195. type = xhci_port_slot_type ( xhci, usb->port->address );
  2196. if ( type < 0 ) {
  2197. rc = type;
  2198. DBGC ( xhci, "XHCI %p port %d has no slot type\n",
  2199. xhci, usb->port->address );
  2200. goto err_type;
  2201. }
  2202. /* Allocate a device slot number */
  2203. id = xhci_enable_slot ( xhci, type );
  2204. if ( id < 0 ) {
  2205. rc = id;
  2206. goto err_enable_slot;
  2207. }
  2208. assert ( xhci->slot[id] == NULL );
  2209. /* Allocate and initialise structure */
  2210. slot = zalloc ( sizeof ( *slot ) );
  2211. if ( ! slot ) {
  2212. rc = -ENOMEM;
  2213. goto err_alloc;
  2214. }
  2215. usb_set_hostdata ( usb, slot );
  2216. xhci->slot[id] = slot;
  2217. slot->xhci = xhci;
  2218. slot->usb = usb;
  2219. slot->id = id;
  2220. /* Allocate a device context */
  2221. len = xhci_device_context_offset ( xhci, XHCI_CTX_END );
  2222. slot->context = malloc_dma ( len, xhci_align ( len ) );
  2223. if ( ! slot->context ) {
  2224. rc = -ENOMEM;
  2225. goto err_alloc_context;
  2226. }
  2227. memset ( slot->context, 0, len );
  2228. /* Set device context base address */
  2229. assert ( xhci->dcbaa[id] == 0 );
  2230. xhci->dcbaa[id] = cpu_to_le64 ( virt_to_phys ( slot->context ) );
  2231. DBGC2 ( xhci, "XHCI %p slot %d device context [%08lx,%08lx) for %s\n",
  2232. xhci, slot->id, virt_to_phys ( slot->context ),
  2233. ( virt_to_phys ( slot->context ) + len ), usb->name );
  2234. return 0;
  2235. xhci->dcbaa[id] = 0;
  2236. free_dma ( slot->context, len );
  2237. err_alloc_context:
  2238. xhci->slot[id] = NULL;
  2239. free ( slot );
  2240. err_alloc:
  2241. xhci_disable_slot ( xhci, id );
  2242. err_enable_slot:
  2243. err_type:
  2244. return rc;
  2245. }
  2246. /**
  2247. * Close device
  2248. *
  2249. * @v usb USB device
  2250. */
  2251. static void xhci_device_close ( struct usb_device *usb ) {
  2252. struct xhci_slot *slot = usb_get_hostdata ( usb );
  2253. struct xhci_device *xhci = slot->xhci;
  2254. size_t len = xhci_device_context_offset ( xhci, XHCI_CTX_END );
  2255. unsigned int id = slot->id;
  2256. int rc;
  2257. /* Disable slot */
  2258. if ( ( rc = xhci_disable_slot ( xhci, id ) ) != 0 ) {
  2259. /* Slot is still enabled. Leak the slot context,
  2260. * since the controller may still write to this
  2261. * memory, and leave the DCBAA entry intact.
  2262. *
  2263. * If the controller later reports that this same slot
  2264. * has been re-enabled, then some assertions will be
  2265. * triggered.
  2266. */
  2267. DBGC ( xhci, "XHCI %p slot %d leaking context memory\n",
  2268. xhci, slot->id );
  2269. slot->context = NULL;
  2270. }
  2271. /* Free slot */
  2272. if ( slot->context ) {
  2273. free_dma ( slot->context, len );
  2274. xhci->dcbaa[id] = 0;
  2275. }
  2276. xhci->slot[id] = NULL;
  2277. free ( slot );
  2278. }
  2279. /**
  2280. * Assign device address
  2281. *
  2282. * @v usb USB device
  2283. * @ret rc Return status code
  2284. */
  2285. static int xhci_device_address ( struct usb_device *usb ) {
  2286. struct xhci_slot *slot = usb_get_hostdata ( usb );
  2287. struct xhci_device *xhci = slot->xhci;
  2288. struct usb_port *port = usb->port;
  2289. struct usb_port *root_port;
  2290. int psiv;
  2291. int rc;
  2292. /* Calculate route string */
  2293. slot->route = usb_route_string ( usb );
  2294. /* Calculate root hub port number */
  2295. root_port = usb_root_hub_port ( usb );
  2296. slot->port = root_port->address;
  2297. /* Calculate protocol speed ID */
  2298. psiv = xhci_port_psiv ( xhci, slot->port, port->speed );
  2299. if ( psiv < 0 ) {
  2300. rc = psiv;
  2301. return rc;
  2302. }
  2303. slot->psiv = psiv;
  2304. /* Address device */
  2305. if ( ( rc = xhci_address_device ( xhci, slot ) ) != 0 )
  2306. return rc;
  2307. return 0;
  2308. }
  2309. /******************************************************************************
  2310. *
  2311. * Bus operations
  2312. *
  2313. ******************************************************************************
  2314. */
  2315. /**
  2316. * Open USB bus
  2317. *
  2318. * @v bus USB bus
  2319. * @ret rc Return status code
  2320. */
  2321. static int xhci_bus_open ( struct usb_bus *bus ) {
  2322. struct xhci_device *xhci = usb_bus_get_hostdata ( bus );
  2323. int rc;
  2324. /* Allocate device slot array */
  2325. xhci->slot = zalloc ( xhci->slots * sizeof ( xhci->slot[0] ) );
  2326. if ( ! xhci->slot ) {
  2327. rc = -ENOMEM;
  2328. goto err_slot_alloc;
  2329. }
  2330. /* Allocate device context base address array */
  2331. if ( ( rc = xhci_dcbaa_alloc ( xhci ) ) != 0 )
  2332. goto err_dcbaa_alloc;
  2333. /* Allocate scratchpad buffers */
  2334. if ( ( rc = xhci_scratchpad_alloc ( xhci ) ) != 0 )
  2335. goto err_scratchpad_alloc;
  2336. /* Allocate command ring */
  2337. if ( ( rc = xhci_command_alloc ( xhci ) ) != 0 )
  2338. goto err_command_alloc;
  2339. /* Allocate event ring */
  2340. if ( ( rc = xhci_event_alloc ( xhci ) ) != 0 )
  2341. goto err_event_alloc;
  2342. /* Start controller */
  2343. xhci_run ( xhci );
  2344. return 0;
  2345. xhci_stop ( xhci );
  2346. xhci_event_free ( xhci );
  2347. err_event_alloc:
  2348. xhci_command_free ( xhci );
  2349. err_command_alloc:
  2350. xhci_scratchpad_free ( xhci );
  2351. err_scratchpad_alloc:
  2352. xhci_dcbaa_free ( xhci );
  2353. err_dcbaa_alloc:
  2354. free ( xhci->slot );
  2355. err_slot_alloc:
  2356. return rc;
  2357. }
  2358. /**
  2359. * Close USB bus
  2360. *
  2361. * @v bus USB bus
  2362. */
  2363. static void xhci_bus_close ( struct usb_bus *bus ) {
  2364. struct xhci_device *xhci = usb_bus_get_hostdata ( bus );
  2365. unsigned int i;
  2366. /* Sanity checks */
  2367. assert ( xhci->slot != NULL );
  2368. for ( i = 0 ; i < xhci->slots ; i++ )
  2369. assert ( xhci->slot[i] == NULL );
  2370. xhci_stop ( xhci );
  2371. xhci_event_free ( xhci );
  2372. xhci_command_free ( xhci );
  2373. xhci_scratchpad_free ( xhci );
  2374. xhci_dcbaa_free ( xhci );
  2375. free ( xhci->slot );
  2376. }
  2377. /**
  2378. * Poll USB bus
  2379. *
  2380. * @v bus USB bus
  2381. */
  2382. static void xhci_bus_poll ( struct usb_bus *bus ) {
  2383. struct xhci_device *xhci = usb_bus_get_hostdata ( bus );
  2384. /* Poll event ring */
  2385. xhci_event_poll ( xhci );
  2386. }
  2387. /******************************************************************************
  2388. *
  2389. * Root hub operations
  2390. *
  2391. ******************************************************************************
  2392. */
  2393. /**
  2394. * Open root hub
  2395. *
  2396. * @v hub USB hub
  2397. * @ret rc Return status code
  2398. */
  2399. static int xhci_hub_open ( struct usb_hub *hub ) {
  2400. struct usb_bus *bus = hub->bus;
  2401. struct xhci_device *xhci = usb_bus_get_hostdata ( bus );
  2402. struct usb_port *port;
  2403. uint32_t portsc;
  2404. unsigned int i;
  2405. /* Enable power to all ports */
  2406. for ( i = 1 ; i <= xhci->ports ; i++ ) {
  2407. portsc = readl ( xhci->op + XHCI_OP_PORTSC ( i ) );
  2408. portsc &= XHCI_PORTSC_PRESERVE;
  2409. portsc |= XHCI_PORTSC_PP;
  2410. writel ( portsc, xhci->op + XHCI_OP_PORTSC ( i ) );
  2411. }
  2412. /* xHCI spec requires us to potentially wait 20ms after
  2413. * enabling power to a port.
  2414. */
  2415. mdelay ( XHCI_PORT_POWER_DELAY_MS );
  2416. /* USB3 ports may power up as Disabled */
  2417. for ( i = 1 ; i <= xhci->ports ; i++ ) {
  2418. portsc = readl ( xhci->op + XHCI_OP_PORTSC ( i ) );
  2419. port = usb_port ( hub, i );
  2420. if ( ( port->protocol >= USB_PROTO_3_0 ) &&
  2421. ( ( portsc & XHCI_PORTSC_PLS_MASK ) ==
  2422. XHCI_PORTSC_PLS_DISABLED ) ) {
  2423. /* Force link state to RxDetect */
  2424. portsc &= XHCI_PORTSC_PRESERVE;
  2425. portsc |= ( XHCI_PORTSC_PLS_RXDETECT | XHCI_PORTSC_LWS);
  2426. writel ( portsc, xhci->op + XHCI_OP_PORTSC ( i ) );
  2427. }
  2428. }
  2429. /* Some xHCI cards seem to require an additional delay after
  2430. * setting the link state to RxDetect.
  2431. */
  2432. mdelay ( XHCI_LINK_STATE_DELAY_MS );
  2433. /* Record hub driver private data */
  2434. usb_hub_set_drvdata ( hub, xhci );
  2435. return 0;
  2436. }
  2437. /**
  2438. * Close root hub
  2439. *
  2440. * @v hub USB hub
  2441. */
  2442. static void xhci_hub_close ( struct usb_hub *hub ) {
  2443. /* Clear hub driver private data */
  2444. usb_hub_set_drvdata ( hub, NULL );
  2445. }
  2446. /**
  2447. * Enable port
  2448. *
  2449. * @v hub USB hub
  2450. * @v port USB port
  2451. * @ret rc Return status code
  2452. */
  2453. static int xhci_hub_enable ( struct usb_hub *hub, struct usb_port *port ) {
  2454. struct xhci_device *xhci = usb_hub_get_drvdata ( hub );
  2455. uint32_t portsc;
  2456. unsigned int i;
  2457. /* Reset port if applicable */
  2458. if ( port->protocol < USB_PROTO_3_0 ) {
  2459. portsc = readl ( xhci->op + XHCI_OP_PORTSC ( port->address ) );
  2460. portsc &= XHCI_PORTSC_PRESERVE;
  2461. portsc |= XHCI_PORTSC_PR;
  2462. writel ( portsc, xhci->op + XHCI_OP_PORTSC ( port->address ) );
  2463. }
  2464. /* Wait for port to become enabled */
  2465. for ( i = 0 ; i < XHCI_PORT_RESET_MAX_WAIT_MS ; i++ ) {
  2466. /* Check port status */
  2467. portsc = readl ( xhci->op + XHCI_OP_PORTSC ( port->address ) );
  2468. if ( portsc & XHCI_PORTSC_PED )
  2469. return 0;
  2470. /* Delay */
  2471. mdelay ( 1 );
  2472. }
  2473. DBGC ( xhci, "XHCI %p timed out waiting for port %d to enable\n",
  2474. xhci, port->address );
  2475. return -ETIMEDOUT;
  2476. }
  2477. /**
  2478. * Disable port
  2479. *
  2480. * @v hub USB hub
  2481. * @v port USB port
  2482. * @ret rc Return status code
  2483. */
  2484. static int xhci_hub_disable ( struct usb_hub *hub, struct usb_port *port ) {
  2485. struct xhci_device *xhci = usb_hub_get_drvdata ( hub );
  2486. uint32_t portsc;
  2487. /* Disable port */
  2488. portsc = readl ( xhci->op + XHCI_OP_PORTSC ( port->address ) );
  2489. portsc &= XHCI_PORTSC_PRESERVE;
  2490. portsc |= XHCI_PORTSC_PED;
  2491. writel ( portsc, xhci->op + XHCI_OP_PORTSC ( port->address ) );
  2492. return 0;
  2493. }
  2494. /**
  2495. * Update root hub port speed
  2496. *
  2497. * @v hub USB hub
  2498. * @v port USB port
  2499. * @ret rc Return status code
  2500. */
  2501. static int xhci_hub_speed ( struct usb_hub *hub, struct usb_port *port ) {
  2502. struct xhci_device *xhci = usb_hub_get_drvdata ( hub );
  2503. uint32_t portsc;
  2504. unsigned int psiv;
  2505. int ccs;
  2506. int ped;
  2507. int speed;
  2508. int rc;
  2509. /* Read port status */
  2510. portsc = readl ( xhci->op + XHCI_OP_PORTSC ( port->address ) );
  2511. DBGC2 ( xhci, "XHCI %p port %d status is %08x\n",
  2512. xhci, port->address, portsc );
  2513. /* Check whether or not port is connected */
  2514. ccs = ( portsc & XHCI_PORTSC_CCS );
  2515. if ( ! ccs ) {
  2516. port->speed = USB_SPEED_NONE;
  2517. return 0;
  2518. }
  2519. /* For USB2 ports, the PSIV field is not valid until the port
  2520. * completes reset and becomes enabled.
  2521. */
  2522. ped = ( portsc & XHCI_PORTSC_PED );
  2523. if ( ( port->protocol < USB_PROTO_3_0 ) && ! ped ) {
  2524. port->speed = USB_SPEED_FULL;
  2525. return 0;
  2526. }
  2527. /* Get port speed and map to generic USB speed */
  2528. psiv = XHCI_PORTSC_PSIV ( portsc );
  2529. speed = xhci_port_speed ( xhci, port->address, psiv );
  2530. if ( speed < 0 ) {
  2531. rc = speed;
  2532. return rc;
  2533. }
  2534. port->speed = speed;
  2535. return 0;
  2536. }
  2537. /******************************************************************************
  2538. *
  2539. * PCI interface
  2540. *
  2541. ******************************************************************************
  2542. */
  2543. /** USB host controller operations */
  2544. static struct usb_host_operations xhci_operations = {
  2545. .endpoint = {
  2546. .open = xhci_endpoint_open,
  2547. .close = xhci_endpoint_close,
  2548. .reset = xhci_endpoint_reset,
  2549. .mtu = xhci_endpoint_mtu,
  2550. .message = xhci_endpoint_message,
  2551. .stream = xhci_endpoint_stream,
  2552. },
  2553. .device = {
  2554. .open = xhci_device_open,
  2555. .close = xhci_device_close,
  2556. .address = xhci_device_address,
  2557. },
  2558. .bus = {
  2559. .open = xhci_bus_open,
  2560. .close = xhci_bus_close,
  2561. .poll = xhci_bus_poll,
  2562. },
  2563. .hub = {
  2564. .open = xhci_hub_open,
  2565. .close = xhci_hub_close,
  2566. .enable = xhci_hub_enable,
  2567. .disable = xhci_hub_disable,
  2568. .speed = xhci_hub_speed,
  2569. },
  2570. };
  2571. /**
  2572. * Probe PCI device
  2573. *
  2574. * @v pci PCI device
  2575. * @ret rc Return status code
  2576. */
  2577. static int xhci_probe ( struct pci_device *pci ) {
  2578. struct xhci_device *xhci;
  2579. struct usb_port *port;
  2580. unsigned long bar_start;
  2581. size_t bar_size;
  2582. unsigned int i;
  2583. int rc;
  2584. /* Allocate and initialise structure */
  2585. xhci = zalloc ( sizeof ( *xhci ) );
  2586. if ( ! xhci ) {
  2587. rc = -ENOMEM;
  2588. goto err_alloc;
  2589. }
  2590. /* Fix up PCI device */
  2591. adjust_pci_device ( pci );
  2592. /* Map registers */
  2593. bar_start = pci_bar_start ( pci, XHCI_BAR );
  2594. bar_size = pci_bar_size ( pci, XHCI_BAR );
  2595. xhci->regs = ioremap ( bar_start, bar_size );
  2596. if ( ! xhci->regs ) {
  2597. rc = -ENODEV;
  2598. goto err_ioremap;
  2599. }
  2600. /* Initialise xHCI device */
  2601. xhci_init ( xhci, xhci->regs );
  2602. /* Initialise USB legacy support and claim ownership */
  2603. xhci_legacy_init ( xhci );
  2604. if ( ( rc = xhci_legacy_claim ( xhci ) ) != 0 )
  2605. goto err_legacy_claim;
  2606. /* Reset device */
  2607. if ( ( rc = xhci_reset ( xhci ) ) != 0 )
  2608. goto err_reset;
  2609. /* Allocate USB bus */
  2610. xhci->bus = alloc_usb_bus ( &pci->dev, xhci->ports,
  2611. &xhci_operations );
  2612. if ( ! xhci->bus ) {
  2613. rc = -ENOMEM;
  2614. goto err_alloc_bus;
  2615. }
  2616. usb_bus_set_hostdata ( xhci->bus, xhci );
  2617. usb_hub_set_drvdata ( xhci->bus->hub, xhci );
  2618. /* Set port protocols */
  2619. for ( i = 1 ; i <= xhci->ports ; i++ ) {
  2620. port = usb_port ( xhci->bus->hub, i );
  2621. port->protocol = xhci_port_protocol ( xhci, i );
  2622. }
  2623. /* Register USB bus */
  2624. if ( ( rc = register_usb_bus ( xhci->bus ) ) != 0 )
  2625. goto err_register;
  2626. pci_set_drvdata ( pci, xhci );
  2627. return 0;
  2628. unregister_usb_bus ( xhci->bus );
  2629. err_register:
  2630. free_usb_bus ( xhci->bus );
  2631. err_alloc_bus:
  2632. xhci_reset ( xhci );
  2633. err_reset:
  2634. xhci_legacy_release ( xhci );
  2635. err_legacy_claim:
  2636. iounmap ( xhci->regs );
  2637. err_ioremap:
  2638. free ( xhci );
  2639. err_alloc:
  2640. return rc;
  2641. }
  2642. /**
  2643. * Remove PCI device
  2644. *
  2645. * @v pci PCI device
  2646. */
  2647. static void xhci_remove ( struct pci_device *pci ) {
  2648. struct xhci_device *xhci = pci_get_drvdata ( pci );
  2649. struct usb_bus *bus = xhci->bus;
  2650. unregister_usb_bus ( bus );
  2651. free_usb_bus ( bus );
  2652. xhci_reset ( xhci );
  2653. xhci_legacy_release ( xhci );
  2654. iounmap ( xhci->regs );
  2655. free ( xhci );
  2656. }
  2657. /** XHCI PCI device IDs */
  2658. static struct pci_device_id xhci_ids[] = {
  2659. PCI_ROM ( 0xffff, 0xffff, "xhci", "xHCI", 0 ),
  2660. };
  2661. /** XHCI PCI driver */
  2662. struct pci_driver xhci_driver __pci_driver = {
  2663. .ids = xhci_ids,
  2664. .id_count = ( sizeof ( xhci_ids ) / sizeof ( xhci_ids[0] ) ),
  2665. .class = PCI_CLASS ( PCI_CLASS_SERIAL, PCI_CLASS_SERIAL_USB,
  2666. PCI_CLASS_SERIAL_USB_XHCI ),
  2667. .probe = xhci_probe,
  2668. .remove = xhci_remove,
  2669. };