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

xhci.c 78KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029
  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->type = endpoint->type;
  1729. ep_ctx->burst = endpoint->ep->burst;
  1730. ep_ctx->mtu = cpu_to_le16 ( endpoint->ep->mtu );
  1731. ep_ctx->dequeue = cpu_to_le64 ( virt_to_phys ( endpoint->ring.trb ) |
  1732. XHCI_EP_DCS );
  1733. ep_ctx->trb_len = cpu_to_le16 ( endpoint->ep->mtu ); /* best guess */
  1734. }
  1735. /**
  1736. * Configure endpoint
  1737. *
  1738. * @v xhci xHCI device
  1739. * @v slot Device slot
  1740. * @v endpoint Endpoint
  1741. * @ret rc Return status code
  1742. */
  1743. static inline int xhci_configure_endpoint ( struct xhci_device *xhci,
  1744. struct xhci_slot *slot,
  1745. struct xhci_endpoint *endpoint ) {
  1746. int rc;
  1747. /* Configure endpoint */
  1748. if ( ( rc = xhci_context ( xhci, slot, endpoint,
  1749. XHCI_TRB_CONFIGURE_ENDPOINT,
  1750. xhci_configure_endpoint_input ) ) != 0 )
  1751. return rc;
  1752. DBGC2 ( xhci, "XHCI %p slot %d ctx %d configured\n",
  1753. xhci, slot->id, endpoint->ctx );
  1754. return 0;
  1755. }
  1756. /**
  1757. * Populate deconfigure endpoint input context
  1758. *
  1759. * @v xhci xHCI device
  1760. * @v slot Device slot
  1761. * @v endpoint Endpoint
  1762. * @v input Input context
  1763. */
  1764. static void
  1765. xhci_deconfigure_endpoint_input ( struct xhci_device *xhci __unused,
  1766. struct xhci_slot *slot __unused,
  1767. struct xhci_endpoint *endpoint,
  1768. void *input ) {
  1769. struct xhci_control_context *control_ctx;
  1770. struct xhci_slot_context *slot_ctx;
  1771. /* Populate control context */
  1772. control_ctx = input;
  1773. control_ctx->add = cpu_to_le32 ( 1 << XHCI_CTX_SLOT );
  1774. control_ctx->drop = cpu_to_le32 ( 1 << endpoint->ctx );
  1775. /* Populate slot context */
  1776. slot_ctx = ( input + xhci_input_context_offset ( xhci, XHCI_CTX_SLOT ));
  1777. slot_ctx->info = cpu_to_le32 ( XHCI_SLOT_INFO ( ( XHCI_CTX_END - 1 ),
  1778. 0, 0, 0 ) );
  1779. }
  1780. /**
  1781. * Deconfigure endpoint
  1782. *
  1783. * @v xhci xHCI device
  1784. * @v slot Device slot
  1785. * @v endpoint Endpoint
  1786. * @ret rc Return status code
  1787. */
  1788. static inline int xhci_deconfigure_endpoint ( struct xhci_device *xhci,
  1789. struct xhci_slot *slot,
  1790. struct xhci_endpoint *endpoint ) {
  1791. int rc;
  1792. /* Deconfigure endpoint */
  1793. if ( ( rc = xhci_context ( xhci, slot, endpoint,
  1794. XHCI_TRB_CONFIGURE_ENDPOINT,
  1795. xhci_deconfigure_endpoint_input ) ) != 0 )
  1796. return rc;
  1797. DBGC2 ( xhci, "XHCI %p slot %d ctx %d deconfigured\n",
  1798. xhci, slot->id, endpoint->ctx );
  1799. return 0;
  1800. }
  1801. /**
  1802. * Populate evaluate context input context
  1803. *
  1804. * @v xhci xHCI device
  1805. * @v slot Device slot
  1806. * @v endpoint Endpoint
  1807. * @v input Input context
  1808. */
  1809. static void xhci_evaluate_context_input ( struct xhci_device *xhci,
  1810. struct xhci_slot *slot __unused,
  1811. struct xhci_endpoint *endpoint,
  1812. void *input ) {
  1813. struct xhci_control_context *control_ctx;
  1814. struct xhci_slot_context *slot_ctx;
  1815. struct xhci_endpoint_context *ep_ctx;
  1816. /* Populate control context */
  1817. control_ctx = input;
  1818. control_ctx->add = cpu_to_le32 ( ( 1 << XHCI_CTX_SLOT ) |
  1819. ( 1 << endpoint->ctx ) );
  1820. /* Populate slot context */
  1821. slot_ctx = ( input + xhci_input_context_offset ( xhci, XHCI_CTX_SLOT ));
  1822. slot_ctx->info = cpu_to_le32 ( XHCI_SLOT_INFO ( ( XHCI_CTX_END - 1 ),
  1823. 0, 0, 0 ) );
  1824. /* Populate endpoint context */
  1825. ep_ctx = ( input + xhci_input_context_offset ( xhci, endpoint->ctx ) );
  1826. ep_ctx->mtu = cpu_to_le16 ( endpoint->ep->mtu );
  1827. }
  1828. /**
  1829. * Evaluate context
  1830. *
  1831. * @v xhci xHCI device
  1832. * @v slot Device slot
  1833. * @v endpoint Endpoint
  1834. * @ret rc Return status code
  1835. */
  1836. static inline int xhci_evaluate_context ( struct xhci_device *xhci,
  1837. struct xhci_slot *slot,
  1838. struct xhci_endpoint *endpoint ) {
  1839. int rc;
  1840. /* Configure endpoint */
  1841. if ( ( rc = xhci_context ( xhci, slot, endpoint,
  1842. XHCI_TRB_EVALUATE_CONTEXT,
  1843. xhci_evaluate_context_input ) ) != 0 )
  1844. return rc;
  1845. DBGC2 ( xhci, "XHCI %p slot %d ctx %d (re-)evaluated\n",
  1846. xhci, slot->id, endpoint->ctx );
  1847. return 0;
  1848. }
  1849. /**
  1850. * Reset endpoint
  1851. *
  1852. * @v xhci xHCI device
  1853. * @v slot Device slot
  1854. * @v endpoint Endpoint
  1855. * @ret rc Return status code
  1856. */
  1857. static inline int xhci_reset_endpoint ( struct xhci_device *xhci,
  1858. struct xhci_slot *slot,
  1859. struct xhci_endpoint *endpoint ) {
  1860. union xhci_trb trb;
  1861. struct xhci_trb_reset_endpoint *reset = &trb.reset;
  1862. int rc;
  1863. /* Construct command */
  1864. memset ( reset, 0, sizeof ( *reset ) );
  1865. reset->slot = slot->id;
  1866. reset->endpoint = endpoint->ctx;
  1867. reset->type = XHCI_TRB_RESET_ENDPOINT;
  1868. /* Issue command and wait for completion */
  1869. if ( ( rc = xhci_command ( xhci, &trb ) ) != 0 ) {
  1870. DBGC ( xhci, "XHCI %p slot %d ctx %d could not reset endpoint "
  1871. "in state %d: %s\n", xhci, slot->id, endpoint->ctx,
  1872. endpoint->context->state, strerror ( rc ) );
  1873. return rc;
  1874. }
  1875. return 0;
  1876. }
  1877. /**
  1878. * Stop endpoint
  1879. *
  1880. * @v xhci xHCI device
  1881. * @v slot Device slot
  1882. * @v endpoint Endpoint
  1883. * @ret rc Return status code
  1884. */
  1885. static inline int xhci_stop_endpoint ( struct xhci_device *xhci,
  1886. struct xhci_slot *slot,
  1887. struct xhci_endpoint *endpoint ) {
  1888. union xhci_trb trb;
  1889. struct xhci_trb_stop_endpoint *stop = &trb.stop;
  1890. int rc;
  1891. /* Construct command */
  1892. memset ( stop, 0, sizeof ( *stop ) );
  1893. stop->slot = slot->id;
  1894. stop->endpoint = endpoint->ctx;
  1895. stop->type = XHCI_TRB_STOP_ENDPOINT;
  1896. /* Issue command and wait for completion */
  1897. if ( ( rc = xhci_command ( xhci, &trb ) ) != 0 ) {
  1898. DBGC ( xhci, "XHCI %p slot %d ctx %d could not stop endpoint "
  1899. "in state %d: %s\n", xhci, slot->id, endpoint->ctx,
  1900. endpoint->context->state, strerror ( rc ) );
  1901. return rc;
  1902. }
  1903. return 0;
  1904. }
  1905. /**
  1906. * Set transfer ring dequeue pointer
  1907. *
  1908. * @v xhci xHCI device
  1909. * @v slot Device slot
  1910. * @v endpoint Endpoint
  1911. * @ret rc Return status code
  1912. */
  1913. static inline int
  1914. xhci_set_tr_dequeue_pointer ( struct xhci_device *xhci,
  1915. struct xhci_slot *slot,
  1916. struct xhci_endpoint *endpoint ) {
  1917. union xhci_trb trb;
  1918. struct xhci_trb_set_tr_dequeue_pointer *dequeue = &trb.dequeue;
  1919. struct xhci_trb_ring *ring = &endpoint->ring;
  1920. unsigned int cons;
  1921. unsigned int mask;
  1922. unsigned int index;
  1923. unsigned int dcs;
  1924. int rc;
  1925. /* Construct command */
  1926. memset ( dequeue, 0, sizeof ( *dequeue ) );
  1927. cons = ring->cons;
  1928. mask = ring->mask;
  1929. dcs = ( ( ~( cons >> ring->shift ) ) & XHCI_EP_DCS );
  1930. index = ( cons & mask );
  1931. dequeue->dequeue =
  1932. cpu_to_le64 ( virt_to_phys ( &ring->trb[index] ) | dcs );
  1933. dequeue->slot = slot->id;
  1934. dequeue->endpoint = endpoint->ctx;
  1935. dequeue->type = XHCI_TRB_SET_TR_DEQUEUE_POINTER;
  1936. /* Issue command and wait for completion */
  1937. if ( ( rc = xhci_command ( xhci, &trb ) ) != 0 ) {
  1938. DBGC ( xhci, "XHCI %p slot %d ctx %d could not set TR dequeue "
  1939. "pointer in state %d: %s\n", xhci, slot->id,
  1940. endpoint->ctx, endpoint->context->state, strerror ( rc));
  1941. return rc;
  1942. }
  1943. return 0;
  1944. }
  1945. /******************************************************************************
  1946. *
  1947. * Endpoint operations
  1948. *
  1949. ******************************************************************************
  1950. */
  1951. /**
  1952. * Open endpoint
  1953. *
  1954. * @v ep USB endpoint
  1955. * @ret rc Return status code
  1956. */
  1957. static int xhci_endpoint_open ( struct usb_endpoint *ep ) {
  1958. struct usb_device *usb = ep->usb;
  1959. struct xhci_slot *slot = usb_get_hostdata ( usb );
  1960. struct xhci_device *xhci = slot->xhci;
  1961. struct xhci_endpoint *endpoint;
  1962. unsigned int ctx;
  1963. unsigned int type;
  1964. int rc;
  1965. /* Calculate context index */
  1966. ctx = XHCI_CTX ( ep->address );
  1967. assert ( slot->endpoint[ctx] == NULL );
  1968. /* Calculate endpoint type */
  1969. type = XHCI_EP_TYPE ( ep->attributes & USB_ENDPOINT_ATTR_TYPE_MASK );
  1970. if ( type == XHCI_EP_TYPE ( USB_ENDPOINT_ATTR_CONTROL ) )
  1971. type = XHCI_EP_TYPE_CONTROL;
  1972. if ( ep->address & USB_DIR_IN )
  1973. type |= XHCI_EP_TYPE_IN;
  1974. /* Allocate and initialise structure */
  1975. endpoint = zalloc ( sizeof ( *endpoint ) );
  1976. if ( ! endpoint ) {
  1977. rc = -ENOMEM;
  1978. goto err_alloc;
  1979. }
  1980. usb_endpoint_set_hostdata ( ep, endpoint );
  1981. slot->endpoint[ctx] = endpoint;
  1982. endpoint->xhci = xhci;
  1983. endpoint->slot = slot;
  1984. endpoint->ep = ep;
  1985. endpoint->ctx = ctx;
  1986. endpoint->type = type;
  1987. endpoint->context = ( ( ( void * ) slot->context ) +
  1988. xhci_device_context_offset ( xhci, ctx ) );
  1989. /* Allocate transfer ring */
  1990. if ( ( rc = xhci_ring_alloc ( xhci, &endpoint->ring,
  1991. XHCI_TRANSFER_TRBS_LOG2,
  1992. slot->id, ctx, 0 ) ) != 0 )
  1993. goto err_ring_alloc;
  1994. /* Configure endpoint, if applicable */
  1995. if ( ( ctx != XHCI_CTX_EP0 ) &&
  1996. ( ( rc = xhci_configure_endpoint ( xhci, slot, endpoint ) ) != 0 ))
  1997. goto err_configure_endpoint;
  1998. DBGC2 ( xhci, "XHCI %p slot %d ctx %d ring [%08lx,%08lx)\n",
  1999. xhci, slot->id, ctx, virt_to_phys ( endpoint->ring.trb ),
  2000. ( virt_to_phys ( endpoint->ring.trb ) + endpoint->ring.len ) );
  2001. return 0;
  2002. xhci_deconfigure_endpoint ( xhci, slot, endpoint );
  2003. err_configure_endpoint:
  2004. xhci_ring_free ( &endpoint->ring );
  2005. err_ring_alloc:
  2006. slot->endpoint[ctx] = NULL;
  2007. free ( endpoint );
  2008. err_alloc:
  2009. return rc;
  2010. }
  2011. /**
  2012. * Close endpoint
  2013. *
  2014. * @v ep USB endpoint
  2015. */
  2016. static void xhci_endpoint_close ( struct usb_endpoint *ep ) {
  2017. struct xhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
  2018. struct xhci_slot *slot = endpoint->slot;
  2019. struct xhci_device *xhci = slot->xhci;
  2020. struct io_buffer *iobuf;
  2021. unsigned int ctx = endpoint->ctx;
  2022. /* Deconfigure endpoint, if applicable */
  2023. if ( ctx != XHCI_CTX_EP0 )
  2024. xhci_deconfigure_endpoint ( xhci, slot, endpoint );
  2025. /* Cancel any incomplete transfers */
  2026. while ( xhci_ring_fill ( &endpoint->ring ) ) {
  2027. iobuf = xhci_dequeue_multi ( &endpoint->ring );
  2028. usb_complete_err ( ep, iobuf, -ECANCELED );
  2029. }
  2030. /* Free endpoint */
  2031. xhci_ring_free ( &endpoint->ring );
  2032. slot->endpoint[ctx] = NULL;
  2033. free ( endpoint );
  2034. }
  2035. /**
  2036. * Reset endpoint
  2037. *
  2038. * @v ep USB endpoint
  2039. * @ret rc Return status code
  2040. */
  2041. static int xhci_endpoint_reset ( struct usb_endpoint *ep ) {
  2042. struct xhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
  2043. struct xhci_slot *slot = endpoint->slot;
  2044. struct xhci_device *xhci = slot->xhci;
  2045. int rc;
  2046. /* Reset endpoint context */
  2047. if ( ( rc = xhci_reset_endpoint ( xhci, slot, endpoint ) ) != 0 )
  2048. return rc;
  2049. /* Set transfer ring dequeue pointer */
  2050. if ( ( rc = xhci_set_tr_dequeue_pointer ( xhci, slot, endpoint ) ) != 0)
  2051. return rc;
  2052. DBGC ( xhci, "XHCI %p slot %d ctx %d reset\n",
  2053. xhci, slot->id, endpoint->ctx );
  2054. return 0;
  2055. }
  2056. /**
  2057. * Update MTU
  2058. *
  2059. * @v ep USB endpoint
  2060. * @ret rc Return status code
  2061. */
  2062. static int xhci_endpoint_mtu ( struct usb_endpoint *ep ) {
  2063. struct xhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
  2064. struct xhci_slot *slot = endpoint->slot;
  2065. struct xhci_device *xhci = slot->xhci;
  2066. int rc;
  2067. /* Evalulate context */
  2068. if ( ( rc = xhci_evaluate_context ( xhci, slot, endpoint ) ) != 0 )
  2069. return rc;
  2070. return 0;
  2071. }
  2072. /**
  2073. * Enqueue message transfer
  2074. *
  2075. * @v ep USB endpoint
  2076. * @v packet Setup packet
  2077. * @v iobuf I/O buffer
  2078. * @ret rc Return status code
  2079. */
  2080. static int xhci_endpoint_message ( struct usb_endpoint *ep,
  2081. struct usb_setup_packet *packet,
  2082. struct io_buffer *iobuf ) {
  2083. struct xhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
  2084. unsigned int input = ( le16_to_cpu ( packet->request ) & USB_DIR_IN );
  2085. size_t len = iob_len ( iobuf );
  2086. union xhci_trb trbs[ 1 /* setup */ + 1 /* possible data */ +
  2087. 1 /* status */ ];
  2088. union xhci_trb *trb = trbs;
  2089. struct xhci_trb_setup *setup;
  2090. struct xhci_trb_data *data;
  2091. struct xhci_trb_status *status;
  2092. int rc;
  2093. /* Profile message transfers */
  2094. profile_start ( &xhci_message_profiler );
  2095. /* Construct setup stage TRB */
  2096. memset ( trbs, 0, sizeof ( trbs ) );
  2097. setup = &(trb++)->setup;
  2098. memcpy ( &setup->packet, packet, sizeof ( setup->packet ) );
  2099. setup->len = cpu_to_le32 ( sizeof ( *packet ) );
  2100. setup->flags = XHCI_TRB_IDT;
  2101. setup->type = XHCI_TRB_SETUP;
  2102. if ( len )
  2103. setup->direction = ( input ? XHCI_SETUP_IN : XHCI_SETUP_OUT );
  2104. /* Construct data stage TRB, if applicable */
  2105. if ( len ) {
  2106. data = &(trb++)->data;
  2107. data->data = cpu_to_le64 ( virt_to_phys ( iobuf->data ) );
  2108. data->len = cpu_to_le32 ( len );
  2109. data->type = XHCI_TRB_DATA;
  2110. data->direction = ( input ? XHCI_DATA_IN : XHCI_DATA_OUT );
  2111. }
  2112. /* Construct status stage TRB */
  2113. status = &(trb++)->status;
  2114. status->flags = XHCI_TRB_IOC;
  2115. status->type = XHCI_TRB_STATUS;
  2116. status->direction =
  2117. ( ( len && input ) ? XHCI_STATUS_OUT : XHCI_STATUS_IN );
  2118. /* Enqueue TRBs */
  2119. if ( ( rc = xhci_enqueue_multi ( &endpoint->ring, iobuf, trbs,
  2120. ( trb - trbs ) ) ) != 0 )
  2121. return rc;
  2122. /* Ring the doorbell */
  2123. xhci_doorbell ( &endpoint->ring );
  2124. profile_stop ( &xhci_message_profiler );
  2125. return 0;
  2126. }
  2127. /**
  2128. * Enqueue stream transfer
  2129. *
  2130. * @v ep USB endpoint
  2131. * @v iobuf I/O buffer
  2132. * @ret rc Return status code
  2133. */
  2134. static int xhci_endpoint_stream ( struct usb_endpoint *ep,
  2135. struct io_buffer *iobuf ) {
  2136. struct xhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
  2137. union xhci_trb trb;
  2138. struct xhci_trb_normal *normal;
  2139. int rc;
  2140. /* Profile stream transfers */
  2141. profile_start ( &xhci_stream_profiler );
  2142. /* Construct normal TRBs */
  2143. normal = &trb.normal;
  2144. normal->data = cpu_to_le64 ( virt_to_phys ( iobuf->data ) );
  2145. normal->len = cpu_to_le32 ( iob_len ( iobuf ) );
  2146. normal->flags = XHCI_TRB_IOC;
  2147. normal->type = XHCI_TRB_NORMAL;
  2148. /* Enqueue TRBs */
  2149. if ( ( rc = xhci_enqueue ( &endpoint->ring, iobuf, &trb ) ) != 0 )
  2150. return rc;
  2151. /* Ring the doorbell */
  2152. xhci_doorbell ( &endpoint->ring );
  2153. profile_stop ( &xhci_stream_profiler );
  2154. return 0;
  2155. }
  2156. /******************************************************************************
  2157. *
  2158. * Device operations
  2159. *
  2160. ******************************************************************************
  2161. */
  2162. /**
  2163. * Open device
  2164. *
  2165. * @v usb USB device
  2166. * @ret rc Return status code
  2167. */
  2168. static int xhci_device_open ( struct usb_device *usb ) {
  2169. struct xhci_device *xhci = usb_bus_get_hostdata ( usb->port->hub->bus );
  2170. struct xhci_slot *slot;
  2171. size_t len;
  2172. int type;
  2173. int id;
  2174. int rc;
  2175. /* Determine applicable slot type */
  2176. type = xhci_port_slot_type ( xhci, usb->port->address );
  2177. if ( type < 0 ) {
  2178. rc = type;
  2179. DBGC ( xhci, "XHCI %p port %d has no slot type\n",
  2180. xhci, usb->port->address );
  2181. goto err_type;
  2182. }
  2183. /* Allocate a device slot number */
  2184. id = xhci_enable_slot ( xhci, type );
  2185. if ( id < 0 ) {
  2186. rc = id;
  2187. goto err_enable_slot;
  2188. }
  2189. assert ( xhci->slot[id] == NULL );
  2190. /* Allocate and initialise structure */
  2191. slot = zalloc ( sizeof ( *slot ) );
  2192. if ( ! slot ) {
  2193. rc = -ENOMEM;
  2194. goto err_alloc;
  2195. }
  2196. usb_set_hostdata ( usb, slot );
  2197. xhci->slot[id] = slot;
  2198. slot->xhci = xhci;
  2199. slot->usb = usb;
  2200. slot->id = id;
  2201. /* Allocate a device context */
  2202. len = xhci_device_context_offset ( xhci, XHCI_CTX_END );
  2203. slot->context = malloc_dma ( len, xhci_align ( len ) );
  2204. if ( ! slot->context ) {
  2205. rc = -ENOMEM;
  2206. goto err_alloc_context;
  2207. }
  2208. memset ( slot->context, 0, len );
  2209. /* Set device context base address */
  2210. assert ( xhci->dcbaa[id] == 0 );
  2211. xhci->dcbaa[id] = cpu_to_le64 ( virt_to_phys ( slot->context ) );
  2212. DBGC2 ( xhci, "XHCI %p slot %d device context [%08lx,%08lx) for %s\n",
  2213. xhci, slot->id, virt_to_phys ( slot->context ),
  2214. ( virt_to_phys ( slot->context ) + len ), usb->name );
  2215. return 0;
  2216. xhci->dcbaa[id] = 0;
  2217. free_dma ( slot->context, len );
  2218. err_alloc_context:
  2219. xhci->slot[id] = NULL;
  2220. free ( slot );
  2221. err_alloc:
  2222. xhci_disable_slot ( xhci, id );
  2223. err_enable_slot:
  2224. err_type:
  2225. return rc;
  2226. }
  2227. /**
  2228. * Close device
  2229. *
  2230. * @v usb USB device
  2231. */
  2232. static void xhci_device_close ( struct usb_device *usb ) {
  2233. struct xhci_slot *slot = usb_get_hostdata ( usb );
  2234. struct xhci_device *xhci = slot->xhci;
  2235. size_t len = xhci_device_context_offset ( xhci, XHCI_CTX_END );
  2236. unsigned int id = slot->id;
  2237. /* Disable slot */
  2238. xhci_disable_slot ( xhci, id );
  2239. /* Free slot */
  2240. free_dma ( slot->context, len );
  2241. xhci->dcbaa[id] = 0;
  2242. xhci->slot[id] = NULL;
  2243. free ( slot );
  2244. }
  2245. /**
  2246. * Assign device address
  2247. *
  2248. * @v usb USB device
  2249. * @ret rc Return status code
  2250. */
  2251. static int xhci_device_address ( struct usb_device *usb ) {
  2252. struct xhci_slot *slot = usb_get_hostdata ( usb );
  2253. struct xhci_device *xhci = slot->xhci;
  2254. struct usb_port *port = usb->port;
  2255. struct usb_port *root_port;
  2256. int psiv;
  2257. int rc;
  2258. /* Calculate route string */
  2259. slot->route = usb_route_string ( usb );
  2260. /* Calculate root hub port number */
  2261. root_port = usb_root_hub_port ( usb );
  2262. slot->port = root_port->address;
  2263. /* Calculate protocol speed ID */
  2264. psiv = xhci_port_psiv ( xhci, slot->port, port->speed );
  2265. if ( psiv < 0 ) {
  2266. rc = psiv;
  2267. return rc;
  2268. }
  2269. slot->psiv = psiv;
  2270. /* Address device */
  2271. if ( ( rc = xhci_address_device ( xhci, slot ) ) != 0 )
  2272. return rc;
  2273. return 0;
  2274. }
  2275. /******************************************************************************
  2276. *
  2277. * Bus operations
  2278. *
  2279. ******************************************************************************
  2280. */
  2281. /**
  2282. * Open USB bus
  2283. *
  2284. * @v bus USB bus
  2285. * @ret rc Return status code
  2286. */
  2287. static int xhci_bus_open ( struct usb_bus *bus ) {
  2288. struct xhci_device *xhci = usb_bus_get_hostdata ( bus );
  2289. int rc;
  2290. /* Allocate device slot array */
  2291. xhci->slot = zalloc ( xhci->slots * sizeof ( xhci->slot[0] ) );
  2292. if ( ! xhci->slot ) {
  2293. rc = -ENOMEM;
  2294. goto err_slot_alloc;
  2295. }
  2296. /* Allocate device context base address array */
  2297. if ( ( rc = xhci_dcbaa_alloc ( xhci ) ) != 0 )
  2298. goto err_dcbaa_alloc;
  2299. /* Allocate scratchpad buffers */
  2300. if ( ( rc = xhci_scratchpad_alloc ( xhci ) ) != 0 )
  2301. goto err_scratchpad_alloc;
  2302. /* Allocate command ring */
  2303. if ( ( rc = xhci_command_alloc ( xhci ) ) != 0 )
  2304. goto err_command_alloc;
  2305. /* Allocate event ring */
  2306. if ( ( rc = xhci_event_alloc ( xhci ) ) != 0 )
  2307. goto err_event_alloc;
  2308. /* Start controller */
  2309. xhci_run ( xhci );
  2310. return 0;
  2311. xhci_stop ( xhci );
  2312. xhci_event_free ( xhci );
  2313. err_event_alloc:
  2314. xhci_command_free ( xhci );
  2315. err_command_alloc:
  2316. xhci_scratchpad_free ( xhci );
  2317. err_scratchpad_alloc:
  2318. xhci_dcbaa_free ( xhci );
  2319. err_dcbaa_alloc:
  2320. free ( xhci->slot );
  2321. err_slot_alloc:
  2322. return rc;
  2323. }
  2324. /**
  2325. * Close USB bus
  2326. *
  2327. * @v bus USB bus
  2328. */
  2329. static void xhci_bus_close ( struct usb_bus *bus ) {
  2330. struct xhci_device *xhci = usb_bus_get_hostdata ( bus );
  2331. unsigned int i;
  2332. /* Sanity checks */
  2333. assert ( xhci->slot != NULL );
  2334. for ( i = 0 ; i < xhci->slots ; i++ )
  2335. assert ( xhci->slot[i] == NULL );
  2336. xhci_stop ( xhci );
  2337. xhci_event_free ( xhci );
  2338. xhci_command_free ( xhci );
  2339. xhci_scratchpad_free ( xhci );
  2340. xhci_dcbaa_free ( xhci );
  2341. free ( xhci->slot );
  2342. }
  2343. /**
  2344. * Poll USB bus
  2345. *
  2346. * @v bus USB bus
  2347. */
  2348. static void xhci_bus_poll ( struct usb_bus *bus ) {
  2349. struct xhci_device *xhci = usb_bus_get_hostdata ( bus );
  2350. /* Poll event ring */
  2351. xhci_event_poll ( xhci );
  2352. }
  2353. /******************************************************************************
  2354. *
  2355. * Root hub operations
  2356. *
  2357. ******************************************************************************
  2358. */
  2359. /**
  2360. * Open root hub
  2361. *
  2362. * @v hub USB hub
  2363. * @ret rc Return status code
  2364. */
  2365. static int xhci_hub_open ( struct usb_hub *hub ) {
  2366. struct usb_bus *bus = hub->bus;
  2367. struct xhci_device *xhci = usb_bus_get_hostdata ( bus );
  2368. struct usb_port *port;
  2369. uint32_t portsc;
  2370. unsigned int i;
  2371. /* Enable power to all ports */
  2372. for ( i = 1 ; i <= xhci->ports ; i++ ) {
  2373. portsc = readl ( xhci->op + XHCI_OP_PORTSC ( i ) );
  2374. portsc &= XHCI_PORTSC_PRESERVE;
  2375. portsc |= XHCI_PORTSC_PP;
  2376. writel ( portsc, xhci->op + XHCI_OP_PORTSC ( i ) );
  2377. }
  2378. /* xHCI spec requires us to potentially wait 20ms after
  2379. * enabling power to a port.
  2380. */
  2381. mdelay ( XHCI_PORT_POWER_DELAY_MS );
  2382. /* USB3 ports may power up as Disabled */
  2383. for ( i = 1 ; i <= xhci->ports ; i++ ) {
  2384. portsc = readl ( xhci->op + XHCI_OP_PORTSC ( i ) );
  2385. port = usb_port ( hub, i );
  2386. if ( ( port->protocol >= USB_PROTO_3_0 ) &&
  2387. ( ( portsc & XHCI_PORTSC_PLS_MASK ) ==
  2388. XHCI_PORTSC_PLS_DISABLED ) ) {
  2389. /* Force link state to RxDetect */
  2390. portsc &= XHCI_PORTSC_PRESERVE;
  2391. portsc |= ( XHCI_PORTSC_PLS_RXDETECT | XHCI_PORTSC_LWS);
  2392. writel ( portsc, xhci->op + XHCI_OP_PORTSC ( i ) );
  2393. }
  2394. }
  2395. /* Record hub driver private data */
  2396. usb_hub_set_drvdata ( hub, xhci );
  2397. return 0;
  2398. }
  2399. /**
  2400. * Close root hub
  2401. *
  2402. * @v hub USB hub
  2403. */
  2404. static void xhci_hub_close ( struct usb_hub *hub ) {
  2405. /* Clear hub driver private data */
  2406. usb_hub_set_drvdata ( hub, NULL );
  2407. }
  2408. /**
  2409. * Enable port
  2410. *
  2411. * @v hub USB hub
  2412. * @v port USB port
  2413. * @ret rc Return status code
  2414. */
  2415. static int xhci_hub_enable ( struct usb_hub *hub, struct usb_port *port ) {
  2416. struct xhci_device *xhci = usb_hub_get_drvdata ( hub );
  2417. uint32_t portsc;
  2418. unsigned int i;
  2419. /* Reset port if applicable */
  2420. if ( port->protocol < USB_PROTO_3_0 ) {
  2421. portsc = readl ( xhci->op + XHCI_OP_PORTSC ( port->address ) );
  2422. portsc &= XHCI_PORTSC_PRESERVE;
  2423. portsc |= XHCI_PORTSC_PR;
  2424. writel ( portsc, xhci->op + XHCI_OP_PORTSC ( port->address ) );
  2425. }
  2426. /* Wait for port to become enabled */
  2427. for ( i = 0 ; i < XHCI_PORT_RESET_MAX_WAIT_MS ; i++ ) {
  2428. /* Check port status */
  2429. portsc = readl ( xhci->op + XHCI_OP_PORTSC ( port->address ) );
  2430. if ( portsc & XHCI_PORTSC_PED )
  2431. return 0;
  2432. /* Delay */
  2433. mdelay ( 1 );
  2434. }
  2435. DBGC ( xhci, "XHCI %p timed out waiting for port %d to enable\n",
  2436. xhci, port->address );
  2437. return -ETIMEDOUT;
  2438. }
  2439. /**
  2440. * Disable port
  2441. *
  2442. * @v hub USB hub
  2443. * @v port USB port
  2444. * @ret rc Return status code
  2445. */
  2446. static int xhci_hub_disable ( struct usb_hub *hub, struct usb_port *port ) {
  2447. struct xhci_device *xhci = usb_hub_get_drvdata ( hub );
  2448. uint32_t portsc;
  2449. /* Disable port */
  2450. portsc = readl ( xhci->op + XHCI_OP_PORTSC ( port->address ) );
  2451. portsc &= XHCI_PORTSC_PRESERVE;
  2452. portsc |= XHCI_PORTSC_PED;
  2453. writel ( portsc, xhci->op + XHCI_OP_PORTSC ( port->address ) );
  2454. return 0;
  2455. }
  2456. /**
  2457. * Update root hub port speed
  2458. *
  2459. * @v hub USB hub
  2460. * @v port USB port
  2461. * @ret rc Return status code
  2462. */
  2463. static int xhci_hub_speed ( struct usb_hub *hub, struct usb_port *port ) {
  2464. struct xhci_device *xhci = usb_hub_get_drvdata ( hub );
  2465. uint32_t portsc;
  2466. unsigned int psiv;
  2467. int ccs;
  2468. int ped;
  2469. int speed;
  2470. int rc;
  2471. /* Read port status */
  2472. portsc = readl ( xhci->op + XHCI_OP_PORTSC ( port->address ) );
  2473. DBGC2 ( xhci, "XHCI %p port %d status is %08x\n",
  2474. xhci, port->address, portsc );
  2475. /* Check whether or not port is connected */
  2476. ccs = ( portsc & XHCI_PORTSC_CCS );
  2477. if ( ! ccs ) {
  2478. port->speed = USB_SPEED_NONE;
  2479. return 0;
  2480. }
  2481. /* For USB2 ports, the PSIV field is not valid until the port
  2482. * completes reset and becomes enabled.
  2483. */
  2484. ped = ( portsc & XHCI_PORTSC_PED );
  2485. if ( ( port->protocol < USB_PROTO_3_0 ) && ! ped ) {
  2486. port->speed = USB_SPEED_FULL;
  2487. return 0;
  2488. }
  2489. /* Get port speed and map to generic USB speed */
  2490. psiv = XHCI_PORTSC_PSIV ( portsc );
  2491. speed = xhci_port_speed ( xhci, port->address, psiv );
  2492. if ( speed < 0 ) {
  2493. rc = speed;
  2494. return rc;
  2495. }
  2496. port->speed = speed;
  2497. return 0;
  2498. }
  2499. /******************************************************************************
  2500. *
  2501. * PCI interface
  2502. *
  2503. ******************************************************************************
  2504. */
  2505. /** USB host controller operations */
  2506. static struct usb_host_operations xhci_operations = {
  2507. .endpoint = {
  2508. .open = xhci_endpoint_open,
  2509. .close = xhci_endpoint_close,
  2510. .reset = xhci_endpoint_reset,
  2511. .mtu = xhci_endpoint_mtu,
  2512. .message = xhci_endpoint_message,
  2513. .stream = xhci_endpoint_stream,
  2514. },
  2515. .device = {
  2516. .open = xhci_device_open,
  2517. .close = xhci_device_close,
  2518. .address = xhci_device_address,
  2519. },
  2520. .bus = {
  2521. .open = xhci_bus_open,
  2522. .close = xhci_bus_close,
  2523. .poll = xhci_bus_poll,
  2524. },
  2525. .hub = {
  2526. .open = xhci_hub_open,
  2527. .close = xhci_hub_close,
  2528. .enable = xhci_hub_enable,
  2529. .disable = xhci_hub_disable,
  2530. .speed = xhci_hub_speed,
  2531. },
  2532. };
  2533. /**
  2534. * Probe PCI device
  2535. *
  2536. * @v pci PCI device
  2537. * @ret rc Return status code
  2538. */
  2539. static int xhci_probe ( struct pci_device *pci ) {
  2540. struct xhci_device *xhci;
  2541. struct usb_port *port;
  2542. unsigned long bar_start;
  2543. size_t bar_size;
  2544. unsigned int i;
  2545. int rc;
  2546. /* Allocate and initialise structure */
  2547. xhci = zalloc ( sizeof ( *xhci ) );
  2548. if ( ! xhci ) {
  2549. rc = -ENOMEM;
  2550. goto err_alloc;
  2551. }
  2552. /* Fix up PCI device */
  2553. adjust_pci_device ( pci );
  2554. /* Map registers */
  2555. bar_start = pci_bar_start ( pci, XHCI_BAR );
  2556. bar_size = pci_bar_size ( pci, XHCI_BAR );
  2557. xhci->regs = ioremap ( bar_start, bar_size );
  2558. if ( ! xhci->regs ) {
  2559. rc = -ENODEV;
  2560. goto err_ioremap;
  2561. }
  2562. /* Initialise xHCI device */
  2563. xhci_init ( xhci, xhci->regs );
  2564. /* Initialise USB legacy support and claim ownership */
  2565. xhci_legacy_init ( xhci );
  2566. if ( ( rc = xhci_legacy_claim ( xhci ) ) != 0 )
  2567. goto err_legacy_claim;
  2568. /* Reset device */
  2569. if ( ( rc = xhci_reset ( xhci ) ) != 0 )
  2570. goto err_reset;
  2571. /* Allocate USB bus */
  2572. xhci->bus = alloc_usb_bus ( &pci->dev, xhci->ports,
  2573. &xhci_operations );
  2574. if ( ! xhci->bus ) {
  2575. rc = -ENOMEM;
  2576. goto err_alloc_bus;
  2577. }
  2578. usb_bus_set_hostdata ( xhci->bus, xhci );
  2579. usb_hub_set_drvdata ( xhci->bus->hub, xhci );
  2580. /* Set port protocols */
  2581. for ( i = 1 ; i <= xhci->ports ; i++ ) {
  2582. port = usb_port ( xhci->bus->hub, i );
  2583. port->protocol = xhci_port_protocol ( xhci, i );
  2584. }
  2585. /* Register USB bus */
  2586. if ( ( rc = register_usb_bus ( xhci->bus ) ) != 0 )
  2587. goto err_register;
  2588. pci_set_drvdata ( pci, xhci );
  2589. return 0;
  2590. unregister_usb_bus ( xhci->bus );
  2591. err_register:
  2592. free_usb_bus ( xhci->bus );
  2593. err_alloc_bus:
  2594. xhci_reset ( xhci );
  2595. err_reset:
  2596. xhci_legacy_release ( xhci );
  2597. err_legacy_claim:
  2598. iounmap ( xhci->regs );
  2599. err_ioremap:
  2600. free ( xhci );
  2601. err_alloc:
  2602. return rc;
  2603. }
  2604. /**
  2605. * Remove PCI device
  2606. *
  2607. * @v pci PCI device
  2608. */
  2609. static void xhci_remove ( struct pci_device *pci ) {
  2610. struct xhci_device *xhci = pci_get_drvdata ( pci );
  2611. struct usb_bus *bus = xhci->bus;
  2612. unregister_usb_bus ( bus );
  2613. free_usb_bus ( bus );
  2614. xhci_reset ( xhci );
  2615. xhci_legacy_release ( xhci );
  2616. iounmap ( xhci->regs );
  2617. free ( xhci );
  2618. }
  2619. /** XHCI PCI device IDs */
  2620. static struct pci_device_id xhci_ids[] = {
  2621. PCI_ROM ( 0xffff, 0xffff, "xhci", "xHCI", 0 ),
  2622. };
  2623. /** XHCI PCI driver */
  2624. struct pci_driver xhci_driver __pci_driver = {
  2625. .ids = xhci_ids,
  2626. .id_count = ( sizeof ( xhci_ids ) / sizeof ( xhci_ids[0] ) ),
  2627. .class = PCI_CLASS ( PCI_CLASS_SERIAL, PCI_CLASS_SERIAL_USB,
  2628. PCI_CLASS_SERIAL_USB_XHCI ),
  2629. .probe = xhci_probe,
  2630. .remove = xhci_remove,
  2631. };