hcd-ehci.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540
  1. /*
  2. * QEMU USB EHCI Emulation
  3. *
  4. * Copyright(c) 2008 Emutex Ltd. (address@hidden)
  5. * Copyright(c) 2011-2012 Red Hat, Inc.
  6. *
  7. * Red Hat Authors:
  8. * Gerd Hoffmann <kraxel@redhat.com>
  9. * Hans de Goede <hdegoede@redhat.com>
  10. *
  11. * EHCI project was started by Mark Burkley, with contributions by
  12. * Niels de Vos. David S. Ahern continued working on it. Kevin Wolf,
  13. * Jan Kiszka and Vincent Palatin contributed bugfixes.
  14. *
  15. *
  16. * This library is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU Lesser General Public
  18. * License as published by the Free Software Foundation; either
  19. * version 2 of the License, or(at your option) any later version.
  20. *
  21. * This library is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  24. * Lesser General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  28. */
  29. #include "qemu/osdep.h"
  30. #include "qapi/error.h"
  31. #include "hw/usb/ehci-regs.h"
  32. #include "hw/usb/hcd-ehci.h"
  33. #include "trace.h"
  34. #define FRAME_TIMER_FREQ 1000
  35. #define FRAME_TIMER_NS (NANOSECONDS_PER_SECOND / FRAME_TIMER_FREQ)
  36. #define UFRAME_TIMER_NS (FRAME_TIMER_NS / 8)
  37. #define NB_MAXINTRATE 8 // Max rate at which controller issues ints
  38. #define BUFF_SIZE 5*4096 // Max bytes to transfer per transaction
  39. #define MAX_QH 100 // Max allowable queue heads in a chain
  40. #define MIN_UFR_PER_TICK 24 /* Min frames to process when catching up */
  41. #define PERIODIC_ACTIVE 512 /* Micro-frames */
  42. /* Internal periodic / asynchronous schedule state machine states
  43. */
  44. typedef enum {
  45. EST_INACTIVE = 1000,
  46. EST_ACTIVE,
  47. EST_EXECUTING,
  48. EST_SLEEPING,
  49. /* The following states are internal to the state machine function
  50. */
  51. EST_WAITLISTHEAD,
  52. EST_FETCHENTRY,
  53. EST_FETCHQH,
  54. EST_FETCHITD,
  55. EST_FETCHSITD,
  56. EST_ADVANCEQUEUE,
  57. EST_FETCHQTD,
  58. EST_EXECUTE,
  59. EST_WRITEBACK,
  60. EST_HORIZONTALQH
  61. } EHCI_STATES;
  62. /* macros for accessing fields within next link pointer entry */
  63. #define NLPTR_GET(x) ((x) & 0xffffffe0)
  64. #define NLPTR_TYPE_GET(x) (((x) >> 1) & 3)
  65. #define NLPTR_TBIT(x) ((x) & 1) // 1=invalid, 0=valid
  66. /* link pointer types */
  67. #define NLPTR_TYPE_ITD 0 // isoc xfer descriptor
  68. #define NLPTR_TYPE_QH 1 // queue head
  69. #define NLPTR_TYPE_STITD 2 // split xaction, isoc xfer descriptor
  70. #define NLPTR_TYPE_FSTN 3 // frame span traversal node
  71. #define SET_LAST_RUN_CLOCK(s) \
  72. (s)->last_run_ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  73. /* nifty macros from Arnon's EHCI version */
  74. #define get_field(data, field) \
  75. (((data) & field##_MASK) >> field##_SH)
  76. #define set_field(data, newval, field) do { \
  77. uint32_t val = *data; \
  78. val &= ~ field##_MASK; \
  79. val |= ((newval) << field##_SH) & field##_MASK; \
  80. *data = val; \
  81. } while(0)
  82. static const char *ehci_state_names[] = {
  83. [EST_INACTIVE] = "INACTIVE",
  84. [EST_ACTIVE] = "ACTIVE",
  85. [EST_EXECUTING] = "EXECUTING",
  86. [EST_SLEEPING] = "SLEEPING",
  87. [EST_WAITLISTHEAD] = "WAITLISTHEAD",
  88. [EST_FETCHENTRY] = "FETCH ENTRY",
  89. [EST_FETCHQH] = "FETCH QH",
  90. [EST_FETCHITD] = "FETCH ITD",
  91. [EST_ADVANCEQUEUE] = "ADVANCEQUEUE",
  92. [EST_FETCHQTD] = "FETCH QTD",
  93. [EST_EXECUTE] = "EXECUTE",
  94. [EST_WRITEBACK] = "WRITEBACK",
  95. [EST_HORIZONTALQH] = "HORIZONTALQH",
  96. };
  97. static const char *ehci_mmio_names[] = {
  98. [USBCMD] = "USBCMD",
  99. [USBSTS] = "USBSTS",
  100. [USBINTR] = "USBINTR",
  101. [FRINDEX] = "FRINDEX",
  102. [PERIODICLISTBASE] = "P-LIST BASE",
  103. [ASYNCLISTADDR] = "A-LIST ADDR",
  104. [CONFIGFLAG] = "CONFIGFLAG",
  105. };
  106. static int ehci_state_executing(EHCIQueue *q);
  107. static int ehci_state_writeback(EHCIQueue *q);
  108. static int ehci_state_advqueue(EHCIQueue *q);
  109. static int ehci_fill_queue(EHCIPacket *p);
  110. static void ehci_free_packet(EHCIPacket *p);
  111. static const char *nr2str(const char **n, size_t len, uint32_t nr)
  112. {
  113. if (nr < len && n[nr] != NULL) {
  114. return n[nr];
  115. } else {
  116. return "unknown";
  117. }
  118. }
  119. static const char *state2str(uint32_t state)
  120. {
  121. return nr2str(ehci_state_names, ARRAY_SIZE(ehci_state_names), state);
  122. }
  123. static const char *addr2str(hwaddr addr)
  124. {
  125. return nr2str(ehci_mmio_names, ARRAY_SIZE(ehci_mmio_names), addr);
  126. }
  127. static void ehci_trace_usbsts(uint32_t mask, int state)
  128. {
  129. /* interrupts */
  130. if (mask & USBSTS_INT) {
  131. trace_usb_ehci_usbsts("INT", state);
  132. }
  133. if (mask & USBSTS_ERRINT) {
  134. trace_usb_ehci_usbsts("ERRINT", state);
  135. }
  136. if (mask & USBSTS_PCD) {
  137. trace_usb_ehci_usbsts("PCD", state);
  138. }
  139. if (mask & USBSTS_FLR) {
  140. trace_usb_ehci_usbsts("FLR", state);
  141. }
  142. if (mask & USBSTS_HSE) {
  143. trace_usb_ehci_usbsts("HSE", state);
  144. }
  145. if (mask & USBSTS_IAA) {
  146. trace_usb_ehci_usbsts("IAA", state);
  147. }
  148. /* status */
  149. if (mask & USBSTS_HALT) {
  150. trace_usb_ehci_usbsts("HALT", state);
  151. }
  152. if (mask & USBSTS_REC) {
  153. trace_usb_ehci_usbsts("REC", state);
  154. }
  155. if (mask & USBSTS_PSS) {
  156. trace_usb_ehci_usbsts("PSS", state);
  157. }
  158. if (mask & USBSTS_ASS) {
  159. trace_usb_ehci_usbsts("ASS", state);
  160. }
  161. }
  162. static inline void ehci_set_usbsts(EHCIState *s, int mask)
  163. {
  164. if ((s->usbsts & mask) == mask) {
  165. return;
  166. }
  167. ehci_trace_usbsts(mask, 1);
  168. s->usbsts |= mask;
  169. }
  170. static inline void ehci_clear_usbsts(EHCIState *s, int mask)
  171. {
  172. if ((s->usbsts & mask) == 0) {
  173. return;
  174. }
  175. ehci_trace_usbsts(mask, 0);
  176. s->usbsts &= ~mask;
  177. }
  178. /* update irq line */
  179. static inline void ehci_update_irq(EHCIState *s)
  180. {
  181. int level = 0;
  182. if ((s->usbsts & USBINTR_MASK) & s->usbintr) {
  183. level = 1;
  184. }
  185. trace_usb_ehci_irq(level, s->frindex, s->usbsts, s->usbintr);
  186. qemu_set_irq(s->irq, level);
  187. }
  188. /* flag interrupt condition */
  189. static inline void ehci_raise_irq(EHCIState *s, int intr)
  190. {
  191. if (intr & (USBSTS_PCD | USBSTS_FLR | USBSTS_HSE)) {
  192. s->usbsts |= intr;
  193. ehci_update_irq(s);
  194. } else {
  195. s->usbsts_pending |= intr;
  196. }
  197. }
  198. /*
  199. * Commit pending interrupts (added via ehci_raise_irq),
  200. * at the rate allowed by "Interrupt Threshold Control".
  201. */
  202. static inline void ehci_commit_irq(EHCIState *s)
  203. {
  204. uint32_t itc;
  205. if (!s->usbsts_pending) {
  206. return;
  207. }
  208. if (s->usbsts_frindex > s->frindex) {
  209. return;
  210. }
  211. itc = (s->usbcmd >> 16) & 0xff;
  212. s->usbsts |= s->usbsts_pending;
  213. s->usbsts_pending = 0;
  214. s->usbsts_frindex = s->frindex + itc;
  215. ehci_update_irq(s);
  216. }
  217. static void ehci_update_halt(EHCIState *s)
  218. {
  219. if (s->usbcmd & USBCMD_RUNSTOP) {
  220. ehci_clear_usbsts(s, USBSTS_HALT);
  221. } else {
  222. if (s->astate == EST_INACTIVE && s->pstate == EST_INACTIVE) {
  223. ehci_set_usbsts(s, USBSTS_HALT);
  224. }
  225. }
  226. }
  227. static void ehci_set_state(EHCIState *s, int async, int state)
  228. {
  229. if (async) {
  230. trace_usb_ehci_state("async", state2str(state));
  231. s->astate = state;
  232. if (s->astate == EST_INACTIVE) {
  233. ehci_clear_usbsts(s, USBSTS_ASS);
  234. ehci_update_halt(s);
  235. } else {
  236. ehci_set_usbsts(s, USBSTS_ASS);
  237. }
  238. } else {
  239. trace_usb_ehci_state("periodic", state2str(state));
  240. s->pstate = state;
  241. if (s->pstate == EST_INACTIVE) {
  242. ehci_clear_usbsts(s, USBSTS_PSS);
  243. ehci_update_halt(s);
  244. } else {
  245. ehci_set_usbsts(s, USBSTS_PSS);
  246. }
  247. }
  248. }
  249. static int ehci_get_state(EHCIState *s, int async)
  250. {
  251. return async ? s->astate : s->pstate;
  252. }
  253. static void ehci_set_fetch_addr(EHCIState *s, int async, uint32_t addr)
  254. {
  255. if (async) {
  256. s->a_fetch_addr = addr;
  257. } else {
  258. s->p_fetch_addr = addr;
  259. }
  260. }
  261. static int ehci_get_fetch_addr(EHCIState *s, int async)
  262. {
  263. return async ? s->a_fetch_addr : s->p_fetch_addr;
  264. }
  265. static void ehci_trace_qh(EHCIQueue *q, hwaddr addr, EHCIqh *qh)
  266. {
  267. /* need three here due to argument count limits */
  268. trace_usb_ehci_qh_ptrs(q, addr, qh->next,
  269. qh->current_qtd, qh->next_qtd, qh->altnext_qtd);
  270. trace_usb_ehci_qh_fields(addr,
  271. get_field(qh->epchar, QH_EPCHAR_RL),
  272. get_field(qh->epchar, QH_EPCHAR_MPLEN),
  273. get_field(qh->epchar, QH_EPCHAR_EPS),
  274. get_field(qh->epchar, QH_EPCHAR_EP),
  275. get_field(qh->epchar, QH_EPCHAR_DEVADDR));
  276. trace_usb_ehci_qh_bits(addr,
  277. (bool)(qh->epchar & QH_EPCHAR_C),
  278. (bool)(qh->epchar & QH_EPCHAR_H),
  279. (bool)(qh->epchar & QH_EPCHAR_DTC),
  280. (bool)(qh->epchar & QH_EPCHAR_I));
  281. }
  282. static void ehci_trace_qtd(EHCIQueue *q, hwaddr addr, EHCIqtd *qtd)
  283. {
  284. /* need three here due to argument count limits */
  285. trace_usb_ehci_qtd_ptrs(q, addr, qtd->next, qtd->altnext);
  286. trace_usb_ehci_qtd_fields(addr,
  287. get_field(qtd->token, QTD_TOKEN_TBYTES),
  288. get_field(qtd->token, QTD_TOKEN_CPAGE),
  289. get_field(qtd->token, QTD_TOKEN_CERR),
  290. get_field(qtd->token, QTD_TOKEN_PID));
  291. trace_usb_ehci_qtd_bits(addr,
  292. (bool)(qtd->token & QTD_TOKEN_IOC),
  293. (bool)(qtd->token & QTD_TOKEN_ACTIVE),
  294. (bool)(qtd->token & QTD_TOKEN_HALT),
  295. (bool)(qtd->token & QTD_TOKEN_BABBLE),
  296. (bool)(qtd->token & QTD_TOKEN_XACTERR));
  297. }
  298. static void ehci_trace_itd(EHCIState *s, hwaddr addr, EHCIitd *itd)
  299. {
  300. trace_usb_ehci_itd(addr, itd->next,
  301. get_field(itd->bufptr[1], ITD_BUFPTR_MAXPKT),
  302. get_field(itd->bufptr[2], ITD_BUFPTR_MULT),
  303. get_field(itd->bufptr[0], ITD_BUFPTR_EP),
  304. get_field(itd->bufptr[0], ITD_BUFPTR_DEVADDR));
  305. }
  306. static void ehci_trace_sitd(EHCIState *s, hwaddr addr,
  307. EHCIsitd *sitd)
  308. {
  309. trace_usb_ehci_sitd(addr, sitd->next,
  310. (bool)(sitd->results & SITD_RESULTS_ACTIVE));
  311. }
  312. static void ehci_trace_guest_bug(EHCIState *s, const char *message)
  313. {
  314. trace_usb_ehci_guest_bug(message);
  315. fprintf(stderr, "ehci warning: %s\n", message);
  316. }
  317. static inline bool ehci_enabled(EHCIState *s)
  318. {
  319. return s->usbcmd & USBCMD_RUNSTOP;
  320. }
  321. static inline bool ehci_async_enabled(EHCIState *s)
  322. {
  323. return ehci_enabled(s) && (s->usbcmd & USBCMD_ASE);
  324. }
  325. static inline bool ehci_periodic_enabled(EHCIState *s)
  326. {
  327. return ehci_enabled(s) && (s->usbcmd & USBCMD_PSE);
  328. }
  329. /* Get an array of dwords from main memory */
  330. static inline int get_dwords(EHCIState *ehci, uint32_t addr,
  331. uint32_t *buf, int num)
  332. {
  333. int i;
  334. if (!ehci->as) {
  335. ehci_raise_irq(ehci, USBSTS_HSE);
  336. ehci->usbcmd &= ~USBCMD_RUNSTOP;
  337. trace_usb_ehci_dma_error();
  338. return -1;
  339. }
  340. for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
  341. dma_memory_read(ehci->as, addr, buf, sizeof(*buf));
  342. *buf = le32_to_cpu(*buf);
  343. }
  344. return num;
  345. }
  346. /* Put an array of dwords in to main memory */
  347. static inline int put_dwords(EHCIState *ehci, uint32_t addr,
  348. uint32_t *buf, int num)
  349. {
  350. int i;
  351. if (!ehci->as) {
  352. ehci_raise_irq(ehci, USBSTS_HSE);
  353. ehci->usbcmd &= ~USBCMD_RUNSTOP;
  354. trace_usb_ehci_dma_error();
  355. return -1;
  356. }
  357. for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
  358. uint32_t tmp = cpu_to_le32(*buf);
  359. dma_memory_write(ehci->as, addr, &tmp, sizeof(tmp));
  360. }
  361. return num;
  362. }
  363. static int ehci_get_pid(EHCIqtd *qtd)
  364. {
  365. switch (get_field(qtd->token, QTD_TOKEN_PID)) {
  366. case 0:
  367. return USB_TOKEN_OUT;
  368. case 1:
  369. return USB_TOKEN_IN;
  370. case 2:
  371. return USB_TOKEN_SETUP;
  372. default:
  373. fprintf(stderr, "bad token\n");
  374. return 0;
  375. }
  376. }
  377. static bool ehci_verify_qh(EHCIQueue *q, EHCIqh *qh)
  378. {
  379. uint32_t devaddr = get_field(qh->epchar, QH_EPCHAR_DEVADDR);
  380. uint32_t endp = get_field(qh->epchar, QH_EPCHAR_EP);
  381. if ((devaddr != get_field(q->qh.epchar, QH_EPCHAR_DEVADDR)) ||
  382. (endp != get_field(q->qh.epchar, QH_EPCHAR_EP)) ||
  383. (qh->current_qtd != q->qh.current_qtd) ||
  384. (q->async && qh->next_qtd != q->qh.next_qtd) ||
  385. (memcmp(&qh->altnext_qtd, &q->qh.altnext_qtd,
  386. 7 * sizeof(uint32_t)) != 0) ||
  387. (q->dev != NULL && q->dev->addr != devaddr)) {
  388. return false;
  389. } else {
  390. return true;
  391. }
  392. }
  393. static bool ehci_verify_qtd(EHCIPacket *p, EHCIqtd *qtd)
  394. {
  395. if (p->qtdaddr != p->queue->qtdaddr ||
  396. (p->queue->async && !NLPTR_TBIT(p->qtd.next) &&
  397. (p->qtd.next != qtd->next)) ||
  398. (!NLPTR_TBIT(p->qtd.altnext) && (p->qtd.altnext != qtd->altnext)) ||
  399. p->qtd.token != qtd->token ||
  400. p->qtd.bufptr[0] != qtd->bufptr[0]) {
  401. return false;
  402. } else {
  403. return true;
  404. }
  405. }
  406. static bool ehci_verify_pid(EHCIQueue *q, EHCIqtd *qtd)
  407. {
  408. int ep = get_field(q->qh.epchar, QH_EPCHAR_EP);
  409. int pid = ehci_get_pid(qtd);
  410. /* Note the pid changing is normal for ep 0 (the control ep) */
  411. if (q->last_pid && ep != 0 && pid != q->last_pid) {
  412. return false;
  413. } else {
  414. return true;
  415. }
  416. }
  417. /* Finish executing and writeback a packet outside of the regular
  418. fetchqh -> fetchqtd -> execute -> writeback cycle */
  419. static void ehci_writeback_async_complete_packet(EHCIPacket *p)
  420. {
  421. EHCIQueue *q = p->queue;
  422. EHCIqtd qtd;
  423. EHCIqh qh;
  424. int state;
  425. /* Verify the qh + qtd, like we do when going through fetchqh & fetchqtd */
  426. get_dwords(q->ehci, NLPTR_GET(q->qhaddr),
  427. (uint32_t *) &qh, sizeof(EHCIqh) >> 2);
  428. get_dwords(q->ehci, NLPTR_GET(q->qtdaddr),
  429. (uint32_t *) &qtd, sizeof(EHCIqtd) >> 2);
  430. if (!ehci_verify_qh(q, &qh) || !ehci_verify_qtd(p, &qtd)) {
  431. p->async = EHCI_ASYNC_INITIALIZED;
  432. ehci_free_packet(p);
  433. return;
  434. }
  435. state = ehci_get_state(q->ehci, q->async);
  436. ehci_state_executing(q);
  437. ehci_state_writeback(q); /* Frees the packet! */
  438. if (!(q->qh.token & QTD_TOKEN_HALT)) {
  439. ehci_state_advqueue(q);
  440. }
  441. ehci_set_state(q->ehci, q->async, state);
  442. }
  443. /* packet management */
  444. static EHCIPacket *ehci_alloc_packet(EHCIQueue *q)
  445. {
  446. EHCIPacket *p;
  447. p = g_new0(EHCIPacket, 1);
  448. p->queue = q;
  449. usb_packet_init(&p->packet);
  450. QTAILQ_INSERT_TAIL(&q->packets, p, next);
  451. trace_usb_ehci_packet_action(p->queue, p, "alloc");
  452. return p;
  453. }
  454. static void ehci_free_packet(EHCIPacket *p)
  455. {
  456. if (p->async == EHCI_ASYNC_FINISHED &&
  457. !(p->queue->qh.token & QTD_TOKEN_HALT)) {
  458. ehci_writeback_async_complete_packet(p);
  459. return;
  460. }
  461. trace_usb_ehci_packet_action(p->queue, p, "free");
  462. if (p->async == EHCI_ASYNC_INFLIGHT) {
  463. usb_cancel_packet(&p->packet);
  464. }
  465. if (p->async == EHCI_ASYNC_FINISHED &&
  466. p->packet.status == USB_RET_SUCCESS) {
  467. fprintf(stderr,
  468. "EHCI: Dropping completed packet from halted %s ep %02X\n",
  469. (p->pid == USB_TOKEN_IN) ? "in" : "out",
  470. get_field(p->queue->qh.epchar, QH_EPCHAR_EP));
  471. }
  472. if (p->async != EHCI_ASYNC_NONE) {
  473. usb_packet_unmap(&p->packet, &p->sgl);
  474. qemu_sglist_destroy(&p->sgl);
  475. }
  476. QTAILQ_REMOVE(&p->queue->packets, p, next);
  477. usb_packet_cleanup(&p->packet);
  478. g_free(p);
  479. }
  480. /* queue management */
  481. static EHCIQueue *ehci_alloc_queue(EHCIState *ehci, uint32_t addr, int async)
  482. {
  483. EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues;
  484. EHCIQueue *q;
  485. q = g_malloc0(sizeof(*q));
  486. q->ehci = ehci;
  487. q->qhaddr = addr;
  488. q->async = async;
  489. QTAILQ_INIT(&q->packets);
  490. QTAILQ_INSERT_HEAD(head, q, next);
  491. trace_usb_ehci_queue_action(q, "alloc");
  492. return q;
  493. }
  494. static void ehci_queue_stopped(EHCIQueue *q)
  495. {
  496. int endp = get_field(q->qh.epchar, QH_EPCHAR_EP);
  497. if (!q->last_pid || !q->dev) {
  498. return;
  499. }
  500. usb_device_ep_stopped(q->dev, usb_ep_get(q->dev, q->last_pid, endp));
  501. }
  502. static int ehci_cancel_queue(EHCIQueue *q)
  503. {
  504. EHCIPacket *p;
  505. int packets = 0;
  506. p = QTAILQ_FIRST(&q->packets);
  507. if (p == NULL) {
  508. goto leave;
  509. }
  510. trace_usb_ehci_queue_action(q, "cancel");
  511. do {
  512. ehci_free_packet(p);
  513. packets++;
  514. } while ((p = QTAILQ_FIRST(&q->packets)) != NULL);
  515. leave:
  516. ehci_queue_stopped(q);
  517. return packets;
  518. }
  519. static int ehci_reset_queue(EHCIQueue *q)
  520. {
  521. int packets;
  522. trace_usb_ehci_queue_action(q, "reset");
  523. packets = ehci_cancel_queue(q);
  524. q->dev = NULL;
  525. q->qtdaddr = 0;
  526. q->last_pid = 0;
  527. return packets;
  528. }
  529. static void ehci_free_queue(EHCIQueue *q, const char *warn)
  530. {
  531. EHCIQueueHead *head = q->async ? &q->ehci->aqueues : &q->ehci->pqueues;
  532. int cancelled;
  533. trace_usb_ehci_queue_action(q, "free");
  534. cancelled = ehci_cancel_queue(q);
  535. if (warn && cancelled > 0) {
  536. ehci_trace_guest_bug(q->ehci, warn);
  537. }
  538. QTAILQ_REMOVE(head, q, next);
  539. g_free(q);
  540. }
  541. static EHCIQueue *ehci_find_queue_by_qh(EHCIState *ehci, uint32_t addr,
  542. int async)
  543. {
  544. EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues;
  545. EHCIQueue *q;
  546. QTAILQ_FOREACH(q, head, next) {
  547. if (addr == q->qhaddr) {
  548. return q;
  549. }
  550. }
  551. return NULL;
  552. }
  553. static void ehci_queues_rip_unused(EHCIState *ehci, int async)
  554. {
  555. EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues;
  556. const char *warn = async ? "guest unlinked busy QH" : NULL;
  557. uint64_t maxage = FRAME_TIMER_NS * ehci->maxframes * 4;
  558. EHCIQueue *q, *tmp;
  559. QTAILQ_FOREACH_SAFE(q, head, next, tmp) {
  560. if (q->seen) {
  561. q->seen = 0;
  562. q->ts = ehci->last_run_ns;
  563. continue;
  564. }
  565. if (ehci->last_run_ns < q->ts + maxage) {
  566. continue;
  567. }
  568. ehci_free_queue(q, warn);
  569. }
  570. }
  571. static void ehci_queues_rip_unseen(EHCIState *ehci, int async)
  572. {
  573. EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues;
  574. EHCIQueue *q, *tmp;
  575. QTAILQ_FOREACH_SAFE(q, head, next, tmp) {
  576. if (!q->seen) {
  577. ehci_free_queue(q, NULL);
  578. }
  579. }
  580. }
  581. static void ehci_queues_rip_device(EHCIState *ehci, USBDevice *dev, int async)
  582. {
  583. EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues;
  584. EHCIQueue *q, *tmp;
  585. QTAILQ_FOREACH_SAFE(q, head, next, tmp) {
  586. if (q->dev != dev) {
  587. continue;
  588. }
  589. ehci_free_queue(q, NULL);
  590. }
  591. }
  592. static void ehci_queues_rip_all(EHCIState *ehci, int async)
  593. {
  594. EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues;
  595. const char *warn = async ? "guest stopped busy async schedule" : NULL;
  596. EHCIQueue *q, *tmp;
  597. QTAILQ_FOREACH_SAFE(q, head, next, tmp) {
  598. ehci_free_queue(q, warn);
  599. }
  600. }
  601. /* Attach or detach a device on root hub */
  602. static void ehci_attach(USBPort *port)
  603. {
  604. EHCIState *s = port->opaque;
  605. uint32_t *portsc = &s->portsc[port->index];
  606. const char *owner = (*portsc & PORTSC_POWNER) ? "comp" : "ehci";
  607. trace_usb_ehci_port_attach(port->index, owner, port->dev->product_desc);
  608. if (*portsc & PORTSC_POWNER) {
  609. USBPort *companion = s->companion_ports[port->index];
  610. companion->dev = port->dev;
  611. companion->ops->attach(companion);
  612. return;
  613. }
  614. *portsc |= PORTSC_CONNECT;
  615. *portsc |= PORTSC_CSC;
  616. ehci_raise_irq(s, USBSTS_PCD);
  617. }
  618. static void ehci_detach(USBPort *port)
  619. {
  620. EHCIState *s = port->opaque;
  621. uint32_t *portsc = &s->portsc[port->index];
  622. const char *owner = (*portsc & PORTSC_POWNER) ? "comp" : "ehci";
  623. trace_usb_ehci_port_detach(port->index, owner);
  624. if (*portsc & PORTSC_POWNER) {
  625. USBPort *companion = s->companion_ports[port->index];
  626. companion->ops->detach(companion);
  627. companion->dev = NULL;
  628. /*
  629. * EHCI spec 4.2.2: "When a disconnect occurs... On the event,
  630. * the port ownership is returned immediately to the EHCI controller."
  631. */
  632. *portsc &= ~PORTSC_POWNER;
  633. return;
  634. }
  635. ehci_queues_rip_device(s, port->dev, 0);
  636. ehci_queues_rip_device(s, port->dev, 1);
  637. *portsc &= ~(PORTSC_CONNECT|PORTSC_PED|PORTSC_SUSPEND);
  638. *portsc |= PORTSC_CSC;
  639. ehci_raise_irq(s, USBSTS_PCD);
  640. }
  641. static void ehci_child_detach(USBPort *port, USBDevice *child)
  642. {
  643. EHCIState *s = port->opaque;
  644. uint32_t portsc = s->portsc[port->index];
  645. if (portsc & PORTSC_POWNER) {
  646. USBPort *companion = s->companion_ports[port->index];
  647. companion->ops->child_detach(companion, child);
  648. return;
  649. }
  650. ehci_queues_rip_device(s, child, 0);
  651. ehci_queues_rip_device(s, child, 1);
  652. }
  653. static void ehci_wakeup(USBPort *port)
  654. {
  655. EHCIState *s = port->opaque;
  656. uint32_t *portsc = &s->portsc[port->index];
  657. if (*portsc & PORTSC_POWNER) {
  658. USBPort *companion = s->companion_ports[port->index];
  659. if (companion->ops->wakeup) {
  660. companion->ops->wakeup(companion);
  661. }
  662. return;
  663. }
  664. if (*portsc & PORTSC_SUSPEND) {
  665. trace_usb_ehci_port_wakeup(port->index);
  666. *portsc |= PORTSC_FPRES;
  667. ehci_raise_irq(s, USBSTS_PCD);
  668. }
  669. qemu_bh_schedule(s->async_bh);
  670. }
  671. static void ehci_register_companion(USBBus *bus, USBPort *ports[],
  672. uint32_t portcount, uint32_t firstport,
  673. Error **errp)
  674. {
  675. EHCIState *s = container_of(bus, EHCIState, bus);
  676. uint32_t i;
  677. if (firstport + portcount > NB_PORTS) {
  678. error_setg(errp, "firstport must be between 0 and %u",
  679. NB_PORTS - portcount);
  680. return;
  681. }
  682. for (i = 0; i < portcount; i++) {
  683. if (s->companion_ports[firstport + i]) {
  684. error_setg(errp, "firstport %u asks for ports %u-%u,"
  685. " but port %u has a companion assigned already",
  686. firstport, firstport, firstport + portcount - 1,
  687. firstport + i);
  688. return;
  689. }
  690. }
  691. for (i = 0; i < portcount; i++) {
  692. s->companion_ports[firstport + i] = ports[i];
  693. s->ports[firstport + i].speedmask |=
  694. USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL;
  695. /* Ensure devs attached before the initial reset go to the companion */
  696. s->portsc[firstport + i] = PORTSC_POWNER;
  697. }
  698. s->companion_count++;
  699. s->caps[0x05] = (s->companion_count << 4) | portcount;
  700. }
  701. static void ehci_wakeup_endpoint(USBBus *bus, USBEndpoint *ep,
  702. unsigned int stream)
  703. {
  704. EHCIState *s = container_of(bus, EHCIState, bus);
  705. uint32_t portsc = s->portsc[ep->dev->port->index];
  706. if (portsc & PORTSC_POWNER) {
  707. return;
  708. }
  709. s->periodic_sched_active = PERIODIC_ACTIVE;
  710. qemu_bh_schedule(s->async_bh);
  711. }
  712. static USBDevice *ehci_find_device(EHCIState *ehci, uint8_t addr)
  713. {
  714. USBDevice *dev;
  715. USBPort *port;
  716. int i;
  717. for (i = 0; i < NB_PORTS; i++) {
  718. port = &ehci->ports[i];
  719. if (!(ehci->portsc[i] & PORTSC_PED)) {
  720. DPRINTF("Port %d not enabled\n", i);
  721. continue;
  722. }
  723. dev = usb_find_device(port, addr);
  724. if (dev != NULL) {
  725. return dev;
  726. }
  727. }
  728. return NULL;
  729. }
  730. /* 4.1 host controller initialization */
  731. void ehci_reset(void *opaque)
  732. {
  733. EHCIState *s = opaque;
  734. int i;
  735. USBDevice *devs[NB_PORTS];
  736. trace_usb_ehci_reset();
  737. /*
  738. * Do the detach before touching portsc, so that it correctly gets send to
  739. * us or to our companion based on PORTSC_POWNER before the reset.
  740. */
  741. for(i = 0; i < NB_PORTS; i++) {
  742. devs[i] = s->ports[i].dev;
  743. if (devs[i] && devs[i]->attached) {
  744. usb_detach(&s->ports[i]);
  745. }
  746. }
  747. memset(&s->opreg, 0x00, sizeof(s->opreg));
  748. memset(&s->portsc, 0x00, sizeof(s->portsc));
  749. s->usbcmd = NB_MAXINTRATE << USBCMD_ITC_SH;
  750. s->usbsts = USBSTS_HALT;
  751. s->usbsts_pending = 0;
  752. s->usbsts_frindex = 0;
  753. ehci_update_irq(s);
  754. s->astate = EST_INACTIVE;
  755. s->pstate = EST_INACTIVE;
  756. for(i = 0; i < NB_PORTS; i++) {
  757. if (s->companion_ports[i]) {
  758. s->portsc[i] = PORTSC_POWNER | PORTSC_PPOWER;
  759. } else {
  760. s->portsc[i] = PORTSC_PPOWER;
  761. }
  762. if (devs[i] && devs[i]->attached) {
  763. usb_attach(&s->ports[i]);
  764. usb_device_reset(devs[i]);
  765. }
  766. }
  767. ehci_queues_rip_all(s, 0);
  768. ehci_queues_rip_all(s, 1);
  769. timer_del(s->frame_timer);
  770. qemu_bh_cancel(s->async_bh);
  771. }
  772. static uint64_t ehci_caps_read(void *ptr, hwaddr addr,
  773. unsigned size)
  774. {
  775. EHCIState *s = ptr;
  776. return s->caps[addr];
  777. }
  778. static uint64_t ehci_opreg_read(void *ptr, hwaddr addr,
  779. unsigned size)
  780. {
  781. EHCIState *s = ptr;
  782. uint32_t val;
  783. switch (addr) {
  784. case FRINDEX:
  785. /* Round down to mult of 8, else it can go backwards on migration */
  786. val = s->frindex & ~7;
  787. break;
  788. default:
  789. val = s->opreg[addr >> 2];
  790. }
  791. trace_usb_ehci_opreg_read(addr + s->opregbase, addr2str(addr), val);
  792. return val;
  793. }
  794. static uint64_t ehci_port_read(void *ptr, hwaddr addr,
  795. unsigned size)
  796. {
  797. EHCIState *s = ptr;
  798. uint32_t val;
  799. val = s->portsc[addr >> 2];
  800. trace_usb_ehci_portsc_read(addr + s->portscbase, addr >> 2, val);
  801. return val;
  802. }
  803. static void handle_port_owner_write(EHCIState *s, int port, uint32_t owner)
  804. {
  805. USBDevice *dev = s->ports[port].dev;
  806. uint32_t *portsc = &s->portsc[port];
  807. uint32_t orig;
  808. if (s->companion_ports[port] == NULL)
  809. return;
  810. owner = owner & PORTSC_POWNER;
  811. orig = *portsc & PORTSC_POWNER;
  812. if (!(owner ^ orig)) {
  813. return;
  814. }
  815. if (dev && dev->attached) {
  816. usb_detach(&s->ports[port]);
  817. }
  818. *portsc &= ~PORTSC_POWNER;
  819. *portsc |= owner;
  820. if (dev && dev->attached) {
  821. usb_attach(&s->ports[port]);
  822. }
  823. }
  824. static void ehci_port_write(void *ptr, hwaddr addr,
  825. uint64_t val, unsigned size)
  826. {
  827. EHCIState *s = ptr;
  828. int port = addr >> 2;
  829. uint32_t *portsc = &s->portsc[port];
  830. uint32_t old = *portsc;
  831. USBDevice *dev = s->ports[port].dev;
  832. trace_usb_ehci_portsc_write(addr + s->portscbase, addr >> 2, val);
  833. /* Clear rwc bits */
  834. *portsc &= ~(val & PORTSC_RWC_MASK);
  835. /* The guest may clear, but not set the PED bit */
  836. *portsc &= val | ~PORTSC_PED;
  837. /* POWNER is masked out by RO_MASK as it is RO when we've no companion */
  838. handle_port_owner_write(s, port, val);
  839. /* And finally apply RO_MASK */
  840. val &= PORTSC_RO_MASK;
  841. if ((val & PORTSC_PRESET) && !(*portsc & PORTSC_PRESET)) {
  842. trace_usb_ehci_port_reset(port, 1);
  843. }
  844. if (!(val & PORTSC_PRESET) &&(*portsc & PORTSC_PRESET)) {
  845. trace_usb_ehci_port_reset(port, 0);
  846. if (dev && dev->attached) {
  847. usb_port_reset(&s->ports[port]);
  848. *portsc &= ~PORTSC_CSC;
  849. }
  850. /*
  851. * Table 2.16 Set the enable bit(and enable bit change) to indicate
  852. * to SW that this port has a high speed device attached
  853. */
  854. if (dev && dev->attached && (dev->speedmask & USB_SPEED_MASK_HIGH)) {
  855. val |= PORTSC_PED;
  856. }
  857. }
  858. if ((val & PORTSC_SUSPEND) && !(*portsc & PORTSC_SUSPEND)) {
  859. trace_usb_ehci_port_suspend(port);
  860. }
  861. if (!(val & PORTSC_FPRES) && (*portsc & PORTSC_FPRES)) {
  862. trace_usb_ehci_port_resume(port);
  863. val &= ~PORTSC_SUSPEND;
  864. }
  865. *portsc &= ~PORTSC_RO_MASK;
  866. *portsc |= val;
  867. trace_usb_ehci_portsc_change(addr + s->portscbase, addr >> 2, *portsc, old);
  868. }
  869. static void ehci_opreg_write(void *ptr, hwaddr addr,
  870. uint64_t val, unsigned size)
  871. {
  872. EHCIState *s = ptr;
  873. uint32_t *mmio = s->opreg + (addr >> 2);
  874. uint32_t old = *mmio;
  875. int i;
  876. trace_usb_ehci_opreg_write(addr + s->opregbase, addr2str(addr), val);
  877. switch (addr) {
  878. case USBCMD:
  879. if (val & USBCMD_HCRESET) {
  880. ehci_reset(s);
  881. val = s->usbcmd;
  882. break;
  883. }
  884. /* not supporting dynamic frame list size at the moment */
  885. if ((val & USBCMD_FLS) && !(s->usbcmd & USBCMD_FLS)) {
  886. fprintf(stderr, "attempt to set frame list size -- value %d\n",
  887. (int)val & USBCMD_FLS);
  888. val &= ~USBCMD_FLS;
  889. }
  890. if (val & USBCMD_IAAD) {
  891. /*
  892. * Process IAAD immediately, otherwise the Linux IAAD watchdog may
  893. * trigger and re-use a qh without us seeing the unlink.
  894. */
  895. s->async_stepdown = 0;
  896. qemu_bh_schedule(s->async_bh);
  897. trace_usb_ehci_doorbell_ring();
  898. }
  899. if (((USBCMD_RUNSTOP | USBCMD_PSE | USBCMD_ASE) & val) !=
  900. ((USBCMD_RUNSTOP | USBCMD_PSE | USBCMD_ASE) & s->usbcmd)) {
  901. if (s->pstate == EST_INACTIVE) {
  902. SET_LAST_RUN_CLOCK(s);
  903. }
  904. s->usbcmd = val; /* Set usbcmd for ehci_update_halt() */
  905. ehci_update_halt(s);
  906. s->async_stepdown = 0;
  907. qemu_bh_schedule(s->async_bh);
  908. }
  909. break;
  910. case USBSTS:
  911. val &= USBSTS_RO_MASK; // bits 6 through 31 are RO
  912. ehci_clear_usbsts(s, val); // bits 0 through 5 are R/WC
  913. val = s->usbsts;
  914. ehci_update_irq(s);
  915. break;
  916. case USBINTR:
  917. val &= USBINTR_MASK;
  918. if (ehci_enabled(s) && (USBSTS_FLR & val)) {
  919. qemu_bh_schedule(s->async_bh);
  920. }
  921. break;
  922. case FRINDEX:
  923. val &= 0x00003fff; /* frindex is 14bits */
  924. s->usbsts_frindex = val;
  925. break;
  926. case CONFIGFLAG:
  927. val &= 0x1;
  928. if (val) {
  929. for(i = 0; i < NB_PORTS; i++)
  930. handle_port_owner_write(s, i, 0);
  931. }
  932. break;
  933. case PERIODICLISTBASE:
  934. if (ehci_periodic_enabled(s)) {
  935. fprintf(stderr,
  936. "ehci: PERIODIC list base register set while periodic schedule\n"
  937. " is enabled and HC is enabled\n");
  938. }
  939. break;
  940. case ASYNCLISTADDR:
  941. if (ehci_async_enabled(s)) {
  942. fprintf(stderr,
  943. "ehci: ASYNC list address register set while async schedule\n"
  944. " is enabled and HC is enabled\n");
  945. }
  946. break;
  947. }
  948. *mmio = val;
  949. trace_usb_ehci_opreg_change(addr + s->opregbase, addr2str(addr),
  950. *mmio, old);
  951. }
  952. /*
  953. * Write the qh back to guest physical memory. This step isn't
  954. * in the EHCI spec but we need to do it since we don't share
  955. * physical memory with our guest VM.
  956. *
  957. * The first three dwords are read-only for the EHCI, so skip them
  958. * when writing back the qh.
  959. */
  960. static void ehci_flush_qh(EHCIQueue *q)
  961. {
  962. uint32_t *qh = (uint32_t *) &q->qh;
  963. uint32_t dwords = sizeof(EHCIqh) >> 2;
  964. uint32_t addr = NLPTR_GET(q->qhaddr);
  965. put_dwords(q->ehci, addr + 3 * sizeof(uint32_t), qh + 3, dwords - 3);
  966. }
  967. // 4.10.2
  968. static int ehci_qh_do_overlay(EHCIQueue *q)
  969. {
  970. EHCIPacket *p = QTAILQ_FIRST(&q->packets);
  971. int i;
  972. int dtoggle;
  973. int ping;
  974. int eps;
  975. int reload;
  976. assert(p != NULL);
  977. assert(p->qtdaddr == q->qtdaddr);
  978. // remember values in fields to preserve in qh after overlay
  979. dtoggle = q->qh.token & QTD_TOKEN_DTOGGLE;
  980. ping = q->qh.token & QTD_TOKEN_PING;
  981. q->qh.current_qtd = p->qtdaddr;
  982. q->qh.next_qtd = p->qtd.next;
  983. q->qh.altnext_qtd = p->qtd.altnext;
  984. q->qh.token = p->qtd.token;
  985. eps = get_field(q->qh.epchar, QH_EPCHAR_EPS);
  986. if (eps == EHCI_QH_EPS_HIGH) {
  987. q->qh.token &= ~QTD_TOKEN_PING;
  988. q->qh.token |= ping;
  989. }
  990. reload = get_field(q->qh.epchar, QH_EPCHAR_RL);
  991. set_field(&q->qh.altnext_qtd, reload, QH_ALTNEXT_NAKCNT);
  992. for (i = 0; i < 5; i++) {
  993. q->qh.bufptr[i] = p->qtd.bufptr[i];
  994. }
  995. if (!(q->qh.epchar & QH_EPCHAR_DTC)) {
  996. // preserve QH DT bit
  997. q->qh.token &= ~QTD_TOKEN_DTOGGLE;
  998. q->qh.token |= dtoggle;
  999. }
  1000. q->qh.bufptr[1] &= ~BUFPTR_CPROGMASK_MASK;
  1001. q->qh.bufptr[2] &= ~BUFPTR_FRAMETAG_MASK;
  1002. ehci_flush_qh(q);
  1003. return 0;
  1004. }
  1005. static int ehci_init_transfer(EHCIPacket *p)
  1006. {
  1007. uint32_t cpage, offset, bytes, plen;
  1008. dma_addr_t page;
  1009. cpage = get_field(p->qtd.token, QTD_TOKEN_CPAGE);
  1010. bytes = get_field(p->qtd.token, QTD_TOKEN_TBYTES);
  1011. offset = p->qtd.bufptr[0] & ~QTD_BUFPTR_MASK;
  1012. qemu_sglist_init(&p->sgl, p->queue->ehci->device, 5, p->queue->ehci->as);
  1013. while (bytes > 0) {
  1014. if (cpage > 4) {
  1015. fprintf(stderr, "cpage out of range (%d)\n", cpage);
  1016. return -1;
  1017. }
  1018. page = p->qtd.bufptr[cpage] & QTD_BUFPTR_MASK;
  1019. page += offset;
  1020. plen = bytes;
  1021. if (plen > 4096 - offset) {
  1022. plen = 4096 - offset;
  1023. offset = 0;
  1024. cpage++;
  1025. }
  1026. qemu_sglist_add(&p->sgl, page, plen);
  1027. bytes -= plen;
  1028. }
  1029. return 0;
  1030. }
  1031. static void ehci_finish_transfer(EHCIQueue *q, int len)
  1032. {
  1033. uint32_t cpage, offset;
  1034. if (len > 0) {
  1035. /* update cpage & offset */
  1036. cpage = get_field(q->qh.token, QTD_TOKEN_CPAGE);
  1037. offset = q->qh.bufptr[0] & ~QTD_BUFPTR_MASK;
  1038. offset += len;
  1039. cpage += offset >> QTD_BUFPTR_SH;
  1040. offset &= ~QTD_BUFPTR_MASK;
  1041. set_field(&q->qh.token, cpage, QTD_TOKEN_CPAGE);
  1042. q->qh.bufptr[0] &= QTD_BUFPTR_MASK;
  1043. q->qh.bufptr[0] |= offset;
  1044. }
  1045. }
  1046. static void ehci_async_complete_packet(USBPort *port, USBPacket *packet)
  1047. {
  1048. EHCIPacket *p;
  1049. EHCIState *s = port->opaque;
  1050. uint32_t portsc = s->portsc[port->index];
  1051. if (portsc & PORTSC_POWNER) {
  1052. USBPort *companion = s->companion_ports[port->index];
  1053. companion->ops->complete(companion, packet);
  1054. return;
  1055. }
  1056. p = container_of(packet, EHCIPacket, packet);
  1057. assert(p->async == EHCI_ASYNC_INFLIGHT);
  1058. if (packet->status == USB_RET_REMOVE_FROM_QUEUE) {
  1059. trace_usb_ehci_packet_action(p->queue, p, "remove");
  1060. ehci_free_packet(p);
  1061. return;
  1062. }
  1063. trace_usb_ehci_packet_action(p->queue, p, "wakeup");
  1064. p->async = EHCI_ASYNC_FINISHED;
  1065. if (!p->queue->async) {
  1066. s->periodic_sched_active = PERIODIC_ACTIVE;
  1067. }
  1068. qemu_bh_schedule(s->async_bh);
  1069. }
  1070. static void ehci_execute_complete(EHCIQueue *q)
  1071. {
  1072. EHCIPacket *p = QTAILQ_FIRST(&q->packets);
  1073. uint32_t tbytes;
  1074. assert(p != NULL);
  1075. assert(p->qtdaddr == q->qtdaddr);
  1076. assert(p->async == EHCI_ASYNC_INITIALIZED ||
  1077. p->async == EHCI_ASYNC_FINISHED);
  1078. DPRINTF("execute_complete: qhaddr 0x%x, next 0x%x, qtdaddr 0x%x, "
  1079. "status %d, actual_length %d\n",
  1080. q->qhaddr, q->qh.next, q->qtdaddr,
  1081. p->packet.status, p->packet.actual_length);
  1082. switch (p->packet.status) {
  1083. case USB_RET_SUCCESS:
  1084. break;
  1085. case USB_RET_IOERROR:
  1086. case USB_RET_NODEV:
  1087. q->qh.token |= (QTD_TOKEN_HALT | QTD_TOKEN_XACTERR);
  1088. set_field(&q->qh.token, 0, QTD_TOKEN_CERR);
  1089. ehci_raise_irq(q->ehci, USBSTS_ERRINT);
  1090. break;
  1091. case USB_RET_STALL:
  1092. q->qh.token |= QTD_TOKEN_HALT;
  1093. ehci_raise_irq(q->ehci, USBSTS_ERRINT);
  1094. break;
  1095. case USB_RET_NAK:
  1096. set_field(&q->qh.altnext_qtd, 0, QH_ALTNEXT_NAKCNT);
  1097. return; /* We're not done yet with this transaction */
  1098. case USB_RET_BABBLE:
  1099. q->qh.token |= (QTD_TOKEN_HALT | QTD_TOKEN_BABBLE);
  1100. ehci_raise_irq(q->ehci, USBSTS_ERRINT);
  1101. break;
  1102. default:
  1103. /* should not be triggerable */
  1104. fprintf(stderr, "USB invalid response %d\n", p->packet.status);
  1105. g_assert_not_reached();
  1106. break;
  1107. }
  1108. /* TODO check 4.12 for splits */
  1109. tbytes = get_field(q->qh.token, QTD_TOKEN_TBYTES);
  1110. if (tbytes && p->pid == USB_TOKEN_IN) {
  1111. tbytes -= p->packet.actual_length;
  1112. if (tbytes) {
  1113. /* 4.15.1.2 must raise int on a short input packet */
  1114. ehci_raise_irq(q->ehci, USBSTS_INT);
  1115. if (q->async) {
  1116. q->ehci->int_req_by_async = true;
  1117. }
  1118. }
  1119. } else {
  1120. tbytes = 0;
  1121. }
  1122. DPRINTF("updating tbytes to %d\n", tbytes);
  1123. set_field(&q->qh.token, tbytes, QTD_TOKEN_TBYTES);
  1124. ehci_finish_transfer(q, p->packet.actual_length);
  1125. usb_packet_unmap(&p->packet, &p->sgl);
  1126. qemu_sglist_destroy(&p->sgl);
  1127. p->async = EHCI_ASYNC_NONE;
  1128. q->qh.token ^= QTD_TOKEN_DTOGGLE;
  1129. q->qh.token &= ~QTD_TOKEN_ACTIVE;
  1130. if (q->qh.token & QTD_TOKEN_IOC) {
  1131. ehci_raise_irq(q->ehci, USBSTS_INT);
  1132. if (q->async) {
  1133. q->ehci->int_req_by_async = true;
  1134. }
  1135. }
  1136. }
  1137. /* 4.10.3 returns "again" */
  1138. static int ehci_execute(EHCIPacket *p, const char *action)
  1139. {
  1140. USBEndpoint *ep;
  1141. int endp;
  1142. bool spd;
  1143. assert(p->async == EHCI_ASYNC_NONE ||
  1144. p->async == EHCI_ASYNC_INITIALIZED);
  1145. if (!(p->qtd.token & QTD_TOKEN_ACTIVE)) {
  1146. fprintf(stderr, "Attempting to execute inactive qtd\n");
  1147. return -1;
  1148. }
  1149. if (get_field(p->qtd.token, QTD_TOKEN_TBYTES) > BUFF_SIZE) {
  1150. ehci_trace_guest_bug(p->queue->ehci,
  1151. "guest requested more bytes than allowed");
  1152. return -1;
  1153. }
  1154. if (!ehci_verify_pid(p->queue, &p->qtd)) {
  1155. ehci_queue_stopped(p->queue); /* Mark the ep in the prev dir stopped */
  1156. }
  1157. p->pid = ehci_get_pid(&p->qtd);
  1158. p->queue->last_pid = p->pid;
  1159. endp = get_field(p->queue->qh.epchar, QH_EPCHAR_EP);
  1160. ep = usb_ep_get(p->queue->dev, p->pid, endp);
  1161. if (p->async == EHCI_ASYNC_NONE) {
  1162. if (ehci_init_transfer(p) != 0) {
  1163. return -1;
  1164. }
  1165. spd = (p->pid == USB_TOKEN_IN && NLPTR_TBIT(p->qtd.altnext) == 0);
  1166. usb_packet_setup(&p->packet, p->pid, ep, 0, p->qtdaddr, spd,
  1167. (p->qtd.token & QTD_TOKEN_IOC) != 0);
  1168. usb_packet_map(&p->packet, &p->sgl);
  1169. p->async = EHCI_ASYNC_INITIALIZED;
  1170. }
  1171. trace_usb_ehci_packet_action(p->queue, p, action);
  1172. usb_handle_packet(p->queue->dev, &p->packet);
  1173. DPRINTF("submit: qh 0x%x next 0x%x qtd 0x%x pid 0x%x len %zd endp 0x%x "
  1174. "status %d actual_length %d\n", p->queue->qhaddr, p->qtd.next,
  1175. p->qtdaddr, p->pid, p->packet.iov.size, endp, p->packet.status,
  1176. p->packet.actual_length);
  1177. if (p->packet.actual_length > BUFF_SIZE) {
  1178. fprintf(stderr, "ret from usb_handle_packet > BUFF_SIZE\n");
  1179. return -1;
  1180. }
  1181. return 1;
  1182. }
  1183. /* 4.7.2
  1184. */
  1185. static int ehci_process_itd(EHCIState *ehci,
  1186. EHCIitd *itd,
  1187. uint32_t addr)
  1188. {
  1189. USBDevice *dev;
  1190. USBEndpoint *ep;
  1191. uint32_t i, len, pid, dir, devaddr, endp, xfers = 0;
  1192. uint32_t pg, off, ptr1, ptr2, max, mult;
  1193. ehci->periodic_sched_active = PERIODIC_ACTIVE;
  1194. dir =(itd->bufptr[1] & ITD_BUFPTR_DIRECTION);
  1195. devaddr = get_field(itd->bufptr[0], ITD_BUFPTR_DEVADDR);
  1196. endp = get_field(itd->bufptr[0], ITD_BUFPTR_EP);
  1197. max = get_field(itd->bufptr[1], ITD_BUFPTR_MAXPKT);
  1198. mult = get_field(itd->bufptr[2], ITD_BUFPTR_MULT);
  1199. for(i = 0; i < 8; i++) {
  1200. if (itd->transact[i] & ITD_XACT_ACTIVE) {
  1201. pg = get_field(itd->transact[i], ITD_XACT_PGSEL);
  1202. off = itd->transact[i] & ITD_XACT_OFFSET_MASK;
  1203. len = get_field(itd->transact[i], ITD_XACT_LENGTH);
  1204. if (len > max * mult) {
  1205. len = max * mult;
  1206. }
  1207. if (len > BUFF_SIZE || pg > 6) {
  1208. return -1;
  1209. }
  1210. ptr1 = (itd->bufptr[pg] & ITD_BUFPTR_MASK);
  1211. qemu_sglist_init(&ehci->isgl, ehci->device, 2, ehci->as);
  1212. if (off + len > 4096) {
  1213. /* transfer crosses page border */
  1214. if (pg == 6) {
  1215. return -1; /* avoid page pg + 1 */
  1216. }
  1217. ptr2 = (itd->bufptr[pg + 1] & ITD_BUFPTR_MASK);
  1218. uint32_t len2 = off + len - 4096;
  1219. uint32_t len1 = len - len2;
  1220. qemu_sglist_add(&ehci->isgl, ptr1 + off, len1);
  1221. qemu_sglist_add(&ehci->isgl, ptr2, len2);
  1222. } else {
  1223. qemu_sglist_add(&ehci->isgl, ptr1 + off, len);
  1224. }
  1225. pid = dir ? USB_TOKEN_IN : USB_TOKEN_OUT;
  1226. dev = ehci_find_device(ehci, devaddr);
  1227. ep = usb_ep_get(dev, pid, endp);
  1228. if (ep && ep->type == USB_ENDPOINT_XFER_ISOC) {
  1229. usb_packet_setup(&ehci->ipacket, pid, ep, 0, addr, false,
  1230. (itd->transact[i] & ITD_XACT_IOC) != 0);
  1231. usb_packet_map(&ehci->ipacket, &ehci->isgl);
  1232. usb_handle_packet(dev, &ehci->ipacket);
  1233. usb_packet_unmap(&ehci->ipacket, &ehci->isgl);
  1234. } else {
  1235. DPRINTF("ISOCH: attempt to addess non-iso endpoint\n");
  1236. ehci->ipacket.status = USB_RET_NAK;
  1237. ehci->ipacket.actual_length = 0;
  1238. }
  1239. qemu_sglist_destroy(&ehci->isgl);
  1240. switch (ehci->ipacket.status) {
  1241. case USB_RET_SUCCESS:
  1242. break;
  1243. default:
  1244. fprintf(stderr, "Unexpected iso usb result: %d\n",
  1245. ehci->ipacket.status);
  1246. /* Fall through */
  1247. case USB_RET_IOERROR:
  1248. case USB_RET_NODEV:
  1249. /* 3.3.2: XACTERR is only allowed on IN transactions */
  1250. if (dir) {
  1251. itd->transact[i] |= ITD_XACT_XACTERR;
  1252. ehci_raise_irq(ehci, USBSTS_ERRINT);
  1253. }
  1254. break;
  1255. case USB_RET_BABBLE:
  1256. itd->transact[i] |= ITD_XACT_BABBLE;
  1257. ehci_raise_irq(ehci, USBSTS_ERRINT);
  1258. break;
  1259. case USB_RET_NAK:
  1260. /* no data for us, so do a zero-length transfer */
  1261. ehci->ipacket.actual_length = 0;
  1262. break;
  1263. }
  1264. if (!dir) {
  1265. set_field(&itd->transact[i], len - ehci->ipacket.actual_length,
  1266. ITD_XACT_LENGTH); /* OUT */
  1267. } else {
  1268. set_field(&itd->transact[i], ehci->ipacket.actual_length,
  1269. ITD_XACT_LENGTH); /* IN */
  1270. }
  1271. if (itd->transact[i] & ITD_XACT_IOC) {
  1272. ehci_raise_irq(ehci, USBSTS_INT);
  1273. }
  1274. itd->transact[i] &= ~ITD_XACT_ACTIVE;
  1275. xfers++;
  1276. }
  1277. }
  1278. return xfers ? 0 : -1;
  1279. }
  1280. /* This state is the entry point for asynchronous schedule
  1281. * processing. Entry here consitutes a EHCI start event state (4.8.5)
  1282. */
  1283. static int ehci_state_waitlisthead(EHCIState *ehci, int async)
  1284. {
  1285. EHCIqh qh;
  1286. int i = 0;
  1287. int again = 0;
  1288. uint32_t entry = ehci->asynclistaddr;
  1289. /* set reclamation flag at start event (4.8.6) */
  1290. if (async) {
  1291. ehci_set_usbsts(ehci, USBSTS_REC);
  1292. }
  1293. ehci_queues_rip_unused(ehci, async);
  1294. /* Find the head of the list (4.9.1.1) */
  1295. for(i = 0; i < MAX_QH; i++) {
  1296. if (get_dwords(ehci, NLPTR_GET(entry), (uint32_t *) &qh,
  1297. sizeof(EHCIqh) >> 2) < 0) {
  1298. return 0;
  1299. }
  1300. ehci_trace_qh(NULL, NLPTR_GET(entry), &qh);
  1301. if (qh.epchar & QH_EPCHAR_H) {
  1302. if (async) {
  1303. entry |= (NLPTR_TYPE_QH << 1);
  1304. }
  1305. ehci_set_fetch_addr(ehci, async, entry);
  1306. ehci_set_state(ehci, async, EST_FETCHENTRY);
  1307. again = 1;
  1308. goto out;
  1309. }
  1310. entry = qh.next;
  1311. if (entry == ehci->asynclistaddr) {
  1312. break;
  1313. }
  1314. }
  1315. /* no head found for list. */
  1316. ehci_set_state(ehci, async, EST_ACTIVE);
  1317. out:
  1318. return again;
  1319. }
  1320. /* This state is the entry point for periodic schedule processing as
  1321. * well as being a continuation state for async processing.
  1322. */
  1323. static int ehci_state_fetchentry(EHCIState *ehci, int async)
  1324. {
  1325. int again = 0;
  1326. uint32_t entry = ehci_get_fetch_addr(ehci, async);
  1327. if (NLPTR_TBIT(entry)) {
  1328. ehci_set_state(ehci, async, EST_ACTIVE);
  1329. goto out;
  1330. }
  1331. /* section 4.8, only QH in async schedule */
  1332. if (async && (NLPTR_TYPE_GET(entry) != NLPTR_TYPE_QH)) {
  1333. fprintf(stderr, "non queue head request in async schedule\n");
  1334. return -1;
  1335. }
  1336. switch (NLPTR_TYPE_GET(entry)) {
  1337. case NLPTR_TYPE_QH:
  1338. ehci_set_state(ehci, async, EST_FETCHQH);
  1339. again = 1;
  1340. break;
  1341. case NLPTR_TYPE_ITD:
  1342. ehci_set_state(ehci, async, EST_FETCHITD);
  1343. again = 1;
  1344. break;
  1345. case NLPTR_TYPE_STITD:
  1346. ehci_set_state(ehci, async, EST_FETCHSITD);
  1347. again = 1;
  1348. break;
  1349. default:
  1350. /* TODO: handle FSTN type */
  1351. fprintf(stderr, "FETCHENTRY: entry at %X is of type %d "
  1352. "which is not supported yet\n", entry, NLPTR_TYPE_GET(entry));
  1353. return -1;
  1354. }
  1355. out:
  1356. return again;
  1357. }
  1358. static EHCIQueue *ehci_state_fetchqh(EHCIState *ehci, int async)
  1359. {
  1360. uint32_t entry;
  1361. EHCIQueue *q;
  1362. EHCIqh qh;
  1363. entry = ehci_get_fetch_addr(ehci, async);
  1364. q = ehci_find_queue_by_qh(ehci, entry, async);
  1365. if (q == NULL) {
  1366. q = ehci_alloc_queue(ehci, entry, async);
  1367. }
  1368. q->seen++;
  1369. if (q->seen > 1) {
  1370. /* we are going in circles -- stop processing */
  1371. ehci_set_state(ehci, async, EST_ACTIVE);
  1372. q = NULL;
  1373. goto out;
  1374. }
  1375. if (get_dwords(ehci, NLPTR_GET(q->qhaddr),
  1376. (uint32_t *) &qh, sizeof(EHCIqh) >> 2) < 0) {
  1377. q = NULL;
  1378. goto out;
  1379. }
  1380. ehci_trace_qh(q, NLPTR_GET(q->qhaddr), &qh);
  1381. /*
  1382. * The overlay area of the qh should never be changed by the guest,
  1383. * except when idle, in which case the reset is a nop.
  1384. */
  1385. if (!ehci_verify_qh(q, &qh)) {
  1386. if (ehci_reset_queue(q) > 0) {
  1387. ehci_trace_guest_bug(ehci, "guest updated active QH");
  1388. }
  1389. }
  1390. q->qh = qh;
  1391. q->transact_ctr = get_field(q->qh.epcap, QH_EPCAP_MULT);
  1392. if (q->transact_ctr == 0) { /* Guest bug in some versions of windows */
  1393. q->transact_ctr = 4;
  1394. }
  1395. if (q->dev == NULL) {
  1396. q->dev = ehci_find_device(q->ehci,
  1397. get_field(q->qh.epchar, QH_EPCHAR_DEVADDR));
  1398. }
  1399. if (async && (q->qh.epchar & QH_EPCHAR_H)) {
  1400. /* EHCI spec version 1.0 Section 4.8.3 & 4.10.1 */
  1401. if (ehci->usbsts & USBSTS_REC) {
  1402. ehci_clear_usbsts(ehci, USBSTS_REC);
  1403. } else {
  1404. DPRINTF("FETCHQH: QH 0x%08x. H-bit set, reclamation status reset"
  1405. " - done processing\n", q->qhaddr);
  1406. ehci_set_state(ehci, async, EST_ACTIVE);
  1407. q = NULL;
  1408. goto out;
  1409. }
  1410. }
  1411. #if EHCI_DEBUG
  1412. if (q->qhaddr != q->qh.next) {
  1413. DPRINTF("FETCHQH: QH 0x%08x (h %x halt %x active %x) next 0x%08x\n",
  1414. q->qhaddr,
  1415. q->qh.epchar & QH_EPCHAR_H,
  1416. q->qh.token & QTD_TOKEN_HALT,
  1417. q->qh.token & QTD_TOKEN_ACTIVE,
  1418. q->qh.next);
  1419. }
  1420. #endif
  1421. if (q->qh.token & QTD_TOKEN_HALT) {
  1422. ehci_set_state(ehci, async, EST_HORIZONTALQH);
  1423. } else if ((q->qh.token & QTD_TOKEN_ACTIVE) &&
  1424. (NLPTR_TBIT(q->qh.current_qtd) == 0)) {
  1425. q->qtdaddr = q->qh.current_qtd;
  1426. ehci_set_state(ehci, async, EST_FETCHQTD);
  1427. } else {
  1428. /* EHCI spec version 1.0 Section 4.10.2 */
  1429. ehci_set_state(ehci, async, EST_ADVANCEQUEUE);
  1430. }
  1431. out:
  1432. return q;
  1433. }
  1434. static int ehci_state_fetchitd(EHCIState *ehci, int async)
  1435. {
  1436. uint32_t entry;
  1437. EHCIitd itd;
  1438. assert(!async);
  1439. entry = ehci_get_fetch_addr(ehci, async);
  1440. if (get_dwords(ehci, NLPTR_GET(entry), (uint32_t *) &itd,
  1441. sizeof(EHCIitd) >> 2) < 0) {
  1442. return -1;
  1443. }
  1444. ehci_trace_itd(ehci, entry, &itd);
  1445. if (ehci_process_itd(ehci, &itd, entry) != 0) {
  1446. return -1;
  1447. }
  1448. put_dwords(ehci, NLPTR_GET(entry), (uint32_t *) &itd,
  1449. sizeof(EHCIitd) >> 2);
  1450. ehci_set_fetch_addr(ehci, async, itd.next);
  1451. ehci_set_state(ehci, async, EST_FETCHENTRY);
  1452. return 1;
  1453. }
  1454. static int ehci_state_fetchsitd(EHCIState *ehci, int async)
  1455. {
  1456. uint32_t entry;
  1457. EHCIsitd sitd;
  1458. assert(!async);
  1459. entry = ehci_get_fetch_addr(ehci, async);
  1460. if (get_dwords(ehci, NLPTR_GET(entry), (uint32_t *)&sitd,
  1461. sizeof(EHCIsitd) >> 2) < 0) {
  1462. return 0;
  1463. }
  1464. ehci_trace_sitd(ehci, entry, &sitd);
  1465. if (!(sitd.results & SITD_RESULTS_ACTIVE)) {
  1466. /* siTD is not active, nothing to do */;
  1467. } else {
  1468. /* TODO: split transfers are not implemented */
  1469. fprintf(stderr, "WARNING: Skipping active siTD\n");
  1470. }
  1471. ehci_set_fetch_addr(ehci, async, sitd.next);
  1472. ehci_set_state(ehci, async, EST_FETCHENTRY);
  1473. return 1;
  1474. }
  1475. /* Section 4.10.2 - paragraph 3 */
  1476. static int ehci_state_advqueue(EHCIQueue *q)
  1477. {
  1478. #if 0
  1479. /* TO-DO: 4.10.2 - paragraph 2
  1480. * if I-bit is set to 1 and QH is not active
  1481. * go to horizontal QH
  1482. */
  1483. if (I-bit set) {
  1484. ehci_set_state(ehci, async, EST_HORIZONTALQH);
  1485. goto out;
  1486. }
  1487. #endif
  1488. /*
  1489. * want data and alt-next qTD is valid
  1490. */
  1491. if (((q->qh.token & QTD_TOKEN_TBYTES_MASK) != 0) &&
  1492. (NLPTR_TBIT(q->qh.altnext_qtd) == 0)) {
  1493. q->qtdaddr = q->qh.altnext_qtd;
  1494. ehci_set_state(q->ehci, q->async, EST_FETCHQTD);
  1495. /*
  1496. * next qTD is valid
  1497. */
  1498. } else if (NLPTR_TBIT(q->qh.next_qtd) == 0) {
  1499. q->qtdaddr = q->qh.next_qtd;
  1500. ehci_set_state(q->ehci, q->async, EST_FETCHQTD);
  1501. /*
  1502. * no valid qTD, try next QH
  1503. */
  1504. } else {
  1505. ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH);
  1506. }
  1507. return 1;
  1508. }
  1509. /* Section 4.10.2 - paragraph 4 */
  1510. static int ehci_state_fetchqtd(EHCIQueue *q)
  1511. {
  1512. EHCIqtd qtd;
  1513. EHCIPacket *p;
  1514. int again = 1;
  1515. if (get_dwords(q->ehci, NLPTR_GET(q->qtdaddr), (uint32_t *) &qtd,
  1516. sizeof(EHCIqtd) >> 2) < 0) {
  1517. return 0;
  1518. }
  1519. ehci_trace_qtd(q, NLPTR_GET(q->qtdaddr), &qtd);
  1520. p = QTAILQ_FIRST(&q->packets);
  1521. if (p != NULL) {
  1522. if (!ehci_verify_qtd(p, &qtd)) {
  1523. ehci_cancel_queue(q);
  1524. if (qtd.token & QTD_TOKEN_ACTIVE) {
  1525. ehci_trace_guest_bug(q->ehci, "guest updated active qTD");
  1526. }
  1527. p = NULL;
  1528. } else {
  1529. p->qtd = qtd;
  1530. ehci_qh_do_overlay(q);
  1531. }
  1532. }
  1533. if (!(qtd.token & QTD_TOKEN_ACTIVE)) {
  1534. ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH);
  1535. } else if (p != NULL) {
  1536. switch (p->async) {
  1537. case EHCI_ASYNC_NONE:
  1538. case EHCI_ASYNC_INITIALIZED:
  1539. /* Not yet executed (MULT), or previously nacked (int) packet */
  1540. ehci_set_state(q->ehci, q->async, EST_EXECUTE);
  1541. break;
  1542. case EHCI_ASYNC_INFLIGHT:
  1543. /* Check if the guest has added new tds to the queue */
  1544. again = ehci_fill_queue(QTAILQ_LAST(&q->packets, pkts_head));
  1545. /* Unfinished async handled packet, go horizontal */
  1546. ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH);
  1547. break;
  1548. case EHCI_ASYNC_FINISHED:
  1549. /* Complete executing of the packet */
  1550. ehci_set_state(q->ehci, q->async, EST_EXECUTING);
  1551. break;
  1552. }
  1553. } else {
  1554. p = ehci_alloc_packet(q);
  1555. p->qtdaddr = q->qtdaddr;
  1556. p->qtd = qtd;
  1557. ehci_set_state(q->ehci, q->async, EST_EXECUTE);
  1558. }
  1559. return again;
  1560. }
  1561. static int ehci_state_horizqh(EHCIQueue *q)
  1562. {
  1563. int again = 0;
  1564. if (ehci_get_fetch_addr(q->ehci, q->async) != q->qh.next) {
  1565. ehci_set_fetch_addr(q->ehci, q->async, q->qh.next);
  1566. ehci_set_state(q->ehci, q->async, EST_FETCHENTRY);
  1567. again = 1;
  1568. } else {
  1569. ehci_set_state(q->ehci, q->async, EST_ACTIVE);
  1570. }
  1571. return again;
  1572. }
  1573. /* Returns "again" */
  1574. static int ehci_fill_queue(EHCIPacket *p)
  1575. {
  1576. USBEndpoint *ep = p->packet.ep;
  1577. EHCIQueue *q = p->queue;
  1578. EHCIqtd qtd = p->qtd;
  1579. uint32_t qtdaddr;
  1580. for (;;) {
  1581. if (NLPTR_TBIT(qtd.next) != 0) {
  1582. break;
  1583. }
  1584. qtdaddr = qtd.next;
  1585. /*
  1586. * Detect circular td lists, Windows creates these, counting on the
  1587. * active bit going low after execution to make the queue stop.
  1588. */
  1589. QTAILQ_FOREACH(p, &q->packets, next) {
  1590. if (p->qtdaddr == qtdaddr) {
  1591. goto leave;
  1592. }
  1593. }
  1594. if (get_dwords(q->ehci, NLPTR_GET(qtdaddr),
  1595. (uint32_t *) &qtd, sizeof(EHCIqtd) >> 2) < 0) {
  1596. return -1;
  1597. }
  1598. ehci_trace_qtd(q, NLPTR_GET(qtdaddr), &qtd);
  1599. if (!(qtd.token & QTD_TOKEN_ACTIVE)) {
  1600. break;
  1601. }
  1602. if (!ehci_verify_pid(q, &qtd)) {
  1603. ehci_trace_guest_bug(q->ehci, "guest queued token with wrong pid");
  1604. break;
  1605. }
  1606. p = ehci_alloc_packet(q);
  1607. p->qtdaddr = qtdaddr;
  1608. p->qtd = qtd;
  1609. if (ehci_execute(p, "queue") == -1) {
  1610. return -1;
  1611. }
  1612. assert(p->packet.status == USB_RET_ASYNC);
  1613. p->async = EHCI_ASYNC_INFLIGHT;
  1614. }
  1615. leave:
  1616. usb_device_flush_ep_queue(ep->dev, ep);
  1617. return 1;
  1618. }
  1619. static int ehci_state_execute(EHCIQueue *q)
  1620. {
  1621. EHCIPacket *p = QTAILQ_FIRST(&q->packets);
  1622. int again = 0;
  1623. assert(p != NULL);
  1624. assert(p->qtdaddr == q->qtdaddr);
  1625. if (ehci_qh_do_overlay(q) != 0) {
  1626. return -1;
  1627. }
  1628. // TODO verify enough time remains in the uframe as in 4.4.1.1
  1629. // TODO write back ptr to async list when done or out of time
  1630. /* 4.10.3, bottom of page 82, go horizontal on transaction counter == 0 */
  1631. if (!q->async && q->transact_ctr == 0) {
  1632. ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH);
  1633. again = 1;
  1634. goto out;
  1635. }
  1636. if (q->async) {
  1637. ehci_set_usbsts(q->ehci, USBSTS_REC);
  1638. }
  1639. again = ehci_execute(p, "process");
  1640. if (again == -1) {
  1641. goto out;
  1642. }
  1643. if (p->packet.status == USB_RET_ASYNC) {
  1644. ehci_flush_qh(q);
  1645. trace_usb_ehci_packet_action(p->queue, p, "async");
  1646. p->async = EHCI_ASYNC_INFLIGHT;
  1647. ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH);
  1648. if (q->async) {
  1649. again = ehci_fill_queue(p);
  1650. } else {
  1651. again = 1;
  1652. }
  1653. goto out;
  1654. }
  1655. ehci_set_state(q->ehci, q->async, EST_EXECUTING);
  1656. again = 1;
  1657. out:
  1658. return again;
  1659. }
  1660. static int ehci_state_executing(EHCIQueue *q)
  1661. {
  1662. EHCIPacket *p = QTAILQ_FIRST(&q->packets);
  1663. assert(p != NULL);
  1664. assert(p->qtdaddr == q->qtdaddr);
  1665. ehci_execute_complete(q);
  1666. /* 4.10.3 */
  1667. if (!q->async && q->transact_ctr > 0) {
  1668. q->transact_ctr--;
  1669. }
  1670. /* 4.10.5 */
  1671. if (p->packet.status == USB_RET_NAK) {
  1672. ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH);
  1673. } else {
  1674. ehci_set_state(q->ehci, q->async, EST_WRITEBACK);
  1675. }
  1676. ehci_flush_qh(q);
  1677. return 1;
  1678. }
  1679. static int ehci_state_writeback(EHCIQueue *q)
  1680. {
  1681. EHCIPacket *p = QTAILQ_FIRST(&q->packets);
  1682. uint32_t *qtd, addr;
  1683. int again = 0;
  1684. /* Write back the QTD from the QH area */
  1685. assert(p != NULL);
  1686. assert(p->qtdaddr == q->qtdaddr);
  1687. ehci_trace_qtd(q, NLPTR_GET(p->qtdaddr), (EHCIqtd *) &q->qh.next_qtd);
  1688. qtd = (uint32_t *) &q->qh.next_qtd;
  1689. addr = NLPTR_GET(p->qtdaddr);
  1690. put_dwords(q->ehci, addr + 2 * sizeof(uint32_t), qtd + 2, 2);
  1691. ehci_free_packet(p);
  1692. /*
  1693. * EHCI specs say go horizontal here.
  1694. *
  1695. * We can also advance the queue here for performance reasons. We
  1696. * need to take care to only take that shortcut in case we've
  1697. * processed the qtd just written back without errors, i.e. halt
  1698. * bit is clear.
  1699. */
  1700. if (q->qh.token & QTD_TOKEN_HALT) {
  1701. ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH);
  1702. again = 1;
  1703. } else {
  1704. ehci_set_state(q->ehci, q->async, EST_ADVANCEQUEUE);
  1705. again = 1;
  1706. }
  1707. return again;
  1708. }
  1709. /*
  1710. * This is the state machine that is common to both async and periodic
  1711. */
  1712. static void ehci_advance_state(EHCIState *ehci, int async)
  1713. {
  1714. EHCIQueue *q = NULL;
  1715. int again;
  1716. do {
  1717. switch(ehci_get_state(ehci, async)) {
  1718. case EST_WAITLISTHEAD:
  1719. again = ehci_state_waitlisthead(ehci, async);
  1720. break;
  1721. case EST_FETCHENTRY:
  1722. again = ehci_state_fetchentry(ehci, async);
  1723. break;
  1724. case EST_FETCHQH:
  1725. q = ehci_state_fetchqh(ehci, async);
  1726. if (q != NULL) {
  1727. assert(q->async == async);
  1728. again = 1;
  1729. } else {
  1730. again = 0;
  1731. }
  1732. break;
  1733. case EST_FETCHITD:
  1734. again = ehci_state_fetchitd(ehci, async);
  1735. break;
  1736. case EST_FETCHSITD:
  1737. again = ehci_state_fetchsitd(ehci, async);
  1738. break;
  1739. case EST_ADVANCEQUEUE:
  1740. assert(q != NULL);
  1741. again = ehci_state_advqueue(q);
  1742. break;
  1743. case EST_FETCHQTD:
  1744. assert(q != NULL);
  1745. again = ehci_state_fetchqtd(q);
  1746. break;
  1747. case EST_HORIZONTALQH:
  1748. assert(q != NULL);
  1749. again = ehci_state_horizqh(q);
  1750. break;
  1751. case EST_EXECUTE:
  1752. assert(q != NULL);
  1753. again = ehci_state_execute(q);
  1754. if (async) {
  1755. ehci->async_stepdown = 0;
  1756. }
  1757. break;
  1758. case EST_EXECUTING:
  1759. assert(q != NULL);
  1760. if (async) {
  1761. ehci->async_stepdown = 0;
  1762. }
  1763. again = ehci_state_executing(q);
  1764. break;
  1765. case EST_WRITEBACK:
  1766. assert(q != NULL);
  1767. again = ehci_state_writeback(q);
  1768. if (!async) {
  1769. ehci->periodic_sched_active = PERIODIC_ACTIVE;
  1770. }
  1771. break;
  1772. default:
  1773. fprintf(stderr, "Bad state!\n");
  1774. again = -1;
  1775. g_assert_not_reached();
  1776. break;
  1777. }
  1778. if (again < 0) {
  1779. fprintf(stderr, "processing error - resetting ehci HC\n");
  1780. ehci_reset(ehci);
  1781. again = 0;
  1782. }
  1783. }
  1784. while (again);
  1785. }
  1786. static void ehci_advance_async_state(EHCIState *ehci)
  1787. {
  1788. const int async = 1;
  1789. switch(ehci_get_state(ehci, async)) {
  1790. case EST_INACTIVE:
  1791. if (!ehci_async_enabled(ehci)) {
  1792. break;
  1793. }
  1794. ehci_set_state(ehci, async, EST_ACTIVE);
  1795. // No break, fall through to ACTIVE
  1796. case EST_ACTIVE:
  1797. if (!ehci_async_enabled(ehci)) {
  1798. ehci_queues_rip_all(ehci, async);
  1799. ehci_set_state(ehci, async, EST_INACTIVE);
  1800. break;
  1801. }
  1802. /* make sure guest has acknowledged the doorbell interrupt */
  1803. /* TO-DO: is this really needed? */
  1804. if (ehci->usbsts & USBSTS_IAA) {
  1805. DPRINTF("IAA status bit still set.\n");
  1806. break;
  1807. }
  1808. /* check that address register has been set */
  1809. if (ehci->asynclistaddr == 0) {
  1810. break;
  1811. }
  1812. ehci_set_state(ehci, async, EST_WAITLISTHEAD);
  1813. ehci_advance_state(ehci, async);
  1814. /* If the doorbell is set, the guest wants to make a change to the
  1815. * schedule. The host controller needs to release cached data.
  1816. * (section 4.8.2)
  1817. */
  1818. if (ehci->usbcmd & USBCMD_IAAD) {
  1819. /* Remove all unseen qhs from the async qhs queue */
  1820. ehci_queues_rip_unseen(ehci, async);
  1821. trace_usb_ehci_doorbell_ack();
  1822. ehci->usbcmd &= ~USBCMD_IAAD;
  1823. ehci_raise_irq(ehci, USBSTS_IAA);
  1824. }
  1825. break;
  1826. default:
  1827. /* this should only be due to a developer mistake */
  1828. fprintf(stderr, "ehci: Bad asynchronous state %d. "
  1829. "Resetting to active\n", ehci->astate);
  1830. g_assert_not_reached();
  1831. }
  1832. }
  1833. static void ehci_advance_periodic_state(EHCIState *ehci)
  1834. {
  1835. uint32_t entry;
  1836. uint32_t list;
  1837. const int async = 0;
  1838. // 4.6
  1839. switch(ehci_get_state(ehci, async)) {
  1840. case EST_INACTIVE:
  1841. if (!(ehci->frindex & 7) && ehci_periodic_enabled(ehci)) {
  1842. ehci_set_state(ehci, async, EST_ACTIVE);
  1843. // No break, fall through to ACTIVE
  1844. } else
  1845. break;
  1846. case EST_ACTIVE:
  1847. if (!(ehci->frindex & 7) && !ehci_periodic_enabled(ehci)) {
  1848. ehci_queues_rip_all(ehci, async);
  1849. ehci_set_state(ehci, async, EST_INACTIVE);
  1850. break;
  1851. }
  1852. list = ehci->periodiclistbase & 0xfffff000;
  1853. /* check that register has been set */
  1854. if (list == 0) {
  1855. break;
  1856. }
  1857. list |= ((ehci->frindex & 0x1ff8) >> 1);
  1858. if (get_dwords(ehci, list, &entry, 1) < 0) {
  1859. break;
  1860. }
  1861. DPRINTF("PERIODIC state adv fr=%d. [%08X] -> %08X\n",
  1862. ehci->frindex / 8, list, entry);
  1863. ehci_set_fetch_addr(ehci, async,entry);
  1864. ehci_set_state(ehci, async, EST_FETCHENTRY);
  1865. ehci_advance_state(ehci, async);
  1866. ehci_queues_rip_unused(ehci, async);
  1867. break;
  1868. default:
  1869. /* this should only be due to a developer mistake */
  1870. fprintf(stderr, "ehci: Bad periodic state %d. "
  1871. "Resetting to active\n", ehci->pstate);
  1872. g_assert_not_reached();
  1873. }
  1874. }
  1875. static void ehci_update_frindex(EHCIState *ehci, int uframes)
  1876. {
  1877. int i;
  1878. if (!ehci_enabled(ehci) && ehci->pstate == EST_INACTIVE) {
  1879. return;
  1880. }
  1881. for (i = 0; i < uframes; i++) {
  1882. ehci->frindex++;
  1883. if (ehci->frindex == 0x00002000) {
  1884. ehci_raise_irq(ehci, USBSTS_FLR);
  1885. }
  1886. if (ehci->frindex == 0x00004000) {
  1887. ehci_raise_irq(ehci, USBSTS_FLR);
  1888. ehci->frindex = 0;
  1889. if (ehci->usbsts_frindex >= 0x00004000) {
  1890. ehci->usbsts_frindex -= 0x00004000;
  1891. } else {
  1892. ehci->usbsts_frindex = 0;
  1893. }
  1894. }
  1895. }
  1896. }
  1897. static void ehci_frame_timer(void *opaque)
  1898. {
  1899. EHCIState *ehci = opaque;
  1900. int need_timer = 0;
  1901. int64_t expire_time, t_now;
  1902. uint64_t ns_elapsed;
  1903. int uframes, skipped_uframes;
  1904. int i;
  1905. t_now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  1906. ns_elapsed = t_now - ehci->last_run_ns;
  1907. uframes = ns_elapsed / UFRAME_TIMER_NS;
  1908. if (ehci_periodic_enabled(ehci) || ehci->pstate != EST_INACTIVE) {
  1909. need_timer++;
  1910. if (uframes > (ehci->maxframes * 8)) {
  1911. skipped_uframes = uframes - (ehci->maxframes * 8);
  1912. ehci_update_frindex(ehci, skipped_uframes);
  1913. ehci->last_run_ns += UFRAME_TIMER_NS * skipped_uframes;
  1914. uframes -= skipped_uframes;
  1915. DPRINTF("WARNING - EHCI skipped %d uframes\n", skipped_uframes);
  1916. }
  1917. for (i = 0; i < uframes; i++) {
  1918. /*
  1919. * If we're running behind schedule, we should not catch up
  1920. * too fast, as that will make some guests unhappy:
  1921. * 1) We must process a minimum of MIN_UFR_PER_TICK frames,
  1922. * otherwise we will never catch up
  1923. * 2) Process frames until the guest has requested an irq (IOC)
  1924. */
  1925. if (i >= MIN_UFR_PER_TICK) {
  1926. ehci_commit_irq(ehci);
  1927. if ((ehci->usbsts & USBINTR_MASK) & ehci->usbintr) {
  1928. break;
  1929. }
  1930. }
  1931. if (ehci->periodic_sched_active) {
  1932. ehci->periodic_sched_active--;
  1933. }
  1934. ehci_update_frindex(ehci, 1);
  1935. if ((ehci->frindex & 7) == 0) {
  1936. ehci_advance_periodic_state(ehci);
  1937. }
  1938. ehci->last_run_ns += UFRAME_TIMER_NS;
  1939. }
  1940. } else {
  1941. ehci->periodic_sched_active = 0;
  1942. ehci_update_frindex(ehci, uframes);
  1943. ehci->last_run_ns += UFRAME_TIMER_NS * uframes;
  1944. }
  1945. if (ehci->periodic_sched_active) {
  1946. ehci->async_stepdown = 0;
  1947. } else if (ehci->async_stepdown < ehci->maxframes / 2) {
  1948. ehci->async_stepdown++;
  1949. }
  1950. /* Async is not inside loop since it executes everything it can once
  1951. * called
  1952. */
  1953. if (ehci_async_enabled(ehci) || ehci->astate != EST_INACTIVE) {
  1954. need_timer++;
  1955. ehci_advance_async_state(ehci);
  1956. }
  1957. ehci_commit_irq(ehci);
  1958. if (ehci->usbsts_pending) {
  1959. need_timer++;
  1960. ehci->async_stepdown = 0;
  1961. }
  1962. if (ehci_enabled(ehci) && (ehci->usbintr & USBSTS_FLR)) {
  1963. need_timer++;
  1964. }
  1965. if (need_timer) {
  1966. /* If we've raised int, we speed up the timer, so that we quickly
  1967. * notice any new packets queued up in response */
  1968. if (ehci->int_req_by_async && (ehci->usbsts & USBSTS_INT)) {
  1969. expire_time = t_now +
  1970. NANOSECONDS_PER_SECOND / (FRAME_TIMER_FREQ * 4);
  1971. ehci->int_req_by_async = false;
  1972. } else {
  1973. expire_time = t_now + (NANOSECONDS_PER_SECOND
  1974. * (ehci->async_stepdown+1) / FRAME_TIMER_FREQ);
  1975. }
  1976. timer_mod(ehci->frame_timer, expire_time);
  1977. }
  1978. }
  1979. static const MemoryRegionOps ehci_mmio_caps_ops = {
  1980. .read = ehci_caps_read,
  1981. .valid.min_access_size = 1,
  1982. .valid.max_access_size = 4,
  1983. .impl.min_access_size = 1,
  1984. .impl.max_access_size = 1,
  1985. .endianness = DEVICE_LITTLE_ENDIAN,
  1986. };
  1987. static const MemoryRegionOps ehci_mmio_opreg_ops = {
  1988. .read = ehci_opreg_read,
  1989. .write = ehci_opreg_write,
  1990. .valid.min_access_size = 4,
  1991. .valid.max_access_size = 4,
  1992. .endianness = DEVICE_LITTLE_ENDIAN,
  1993. };
  1994. static const MemoryRegionOps ehci_mmio_port_ops = {
  1995. .read = ehci_port_read,
  1996. .write = ehci_port_write,
  1997. .valid.min_access_size = 4,
  1998. .valid.max_access_size = 4,
  1999. .endianness = DEVICE_LITTLE_ENDIAN,
  2000. };
  2001. static USBPortOps ehci_port_ops = {
  2002. .attach = ehci_attach,
  2003. .detach = ehci_detach,
  2004. .child_detach = ehci_child_detach,
  2005. .wakeup = ehci_wakeup,
  2006. .complete = ehci_async_complete_packet,
  2007. };
  2008. static USBBusOps ehci_bus_ops_companion = {
  2009. .register_companion = ehci_register_companion,
  2010. .wakeup_endpoint = ehci_wakeup_endpoint,
  2011. };
  2012. static USBBusOps ehci_bus_ops_standalone = {
  2013. .wakeup_endpoint = ehci_wakeup_endpoint,
  2014. };
  2015. static void usb_ehci_pre_save(void *opaque)
  2016. {
  2017. EHCIState *ehci = opaque;
  2018. uint32_t new_frindex;
  2019. /* Round down frindex to a multiple of 8 for migration compatibility */
  2020. new_frindex = ehci->frindex & ~7;
  2021. ehci->last_run_ns -= (ehci->frindex - new_frindex) * UFRAME_TIMER_NS;
  2022. ehci->frindex = new_frindex;
  2023. }
  2024. static int usb_ehci_post_load(void *opaque, int version_id)
  2025. {
  2026. EHCIState *s = opaque;
  2027. int i;
  2028. for (i = 0; i < NB_PORTS; i++) {
  2029. USBPort *companion = s->companion_ports[i];
  2030. if (companion == NULL) {
  2031. continue;
  2032. }
  2033. if (s->portsc[i] & PORTSC_POWNER) {
  2034. companion->dev = s->ports[i].dev;
  2035. } else {
  2036. companion->dev = NULL;
  2037. }
  2038. }
  2039. return 0;
  2040. }
  2041. static void usb_ehci_vm_state_change(void *opaque, int running, RunState state)
  2042. {
  2043. EHCIState *ehci = opaque;
  2044. /*
  2045. * We don't migrate the EHCIQueue-s, instead we rebuild them for the
  2046. * schedule in guest memory. We must do the rebuilt ASAP, so that
  2047. * USB-devices which have async handled packages have a packet in the
  2048. * ep queue to match the completion with.
  2049. */
  2050. if (state == RUN_STATE_RUNNING) {
  2051. ehci_advance_async_state(ehci);
  2052. }
  2053. /*
  2054. * The schedule rebuilt from guest memory could cause the migration dest
  2055. * to miss a QH unlink, and fail to cancel packets, since the unlinked QH
  2056. * will never have existed on the destination. Therefor we must flush the
  2057. * async schedule on savevm to catch any not yet noticed unlinks.
  2058. */
  2059. if (state == RUN_STATE_SAVE_VM) {
  2060. ehci_advance_async_state(ehci);
  2061. ehci_queues_rip_unseen(ehci, 1);
  2062. }
  2063. }
  2064. const VMStateDescription vmstate_ehci = {
  2065. .name = "ehci-core",
  2066. .version_id = 2,
  2067. .minimum_version_id = 1,
  2068. .pre_save = usb_ehci_pre_save,
  2069. .post_load = usb_ehci_post_load,
  2070. .fields = (VMStateField[]) {
  2071. /* mmio registers */
  2072. VMSTATE_UINT32(usbcmd, EHCIState),
  2073. VMSTATE_UINT32(usbsts, EHCIState),
  2074. VMSTATE_UINT32_V(usbsts_pending, EHCIState, 2),
  2075. VMSTATE_UINT32_V(usbsts_frindex, EHCIState, 2),
  2076. VMSTATE_UINT32(usbintr, EHCIState),
  2077. VMSTATE_UINT32(frindex, EHCIState),
  2078. VMSTATE_UINT32(ctrldssegment, EHCIState),
  2079. VMSTATE_UINT32(periodiclistbase, EHCIState),
  2080. VMSTATE_UINT32(asynclistaddr, EHCIState),
  2081. VMSTATE_UINT32(configflag, EHCIState),
  2082. VMSTATE_UINT32(portsc[0], EHCIState),
  2083. VMSTATE_UINT32(portsc[1], EHCIState),
  2084. VMSTATE_UINT32(portsc[2], EHCIState),
  2085. VMSTATE_UINT32(portsc[3], EHCIState),
  2086. VMSTATE_UINT32(portsc[4], EHCIState),
  2087. VMSTATE_UINT32(portsc[5], EHCIState),
  2088. /* frame timer */
  2089. VMSTATE_TIMER_PTR(frame_timer, EHCIState),
  2090. VMSTATE_UINT64(last_run_ns, EHCIState),
  2091. VMSTATE_UINT32(async_stepdown, EHCIState),
  2092. /* schedule state */
  2093. VMSTATE_UINT32(astate, EHCIState),
  2094. VMSTATE_UINT32(pstate, EHCIState),
  2095. VMSTATE_UINT32(a_fetch_addr, EHCIState),
  2096. VMSTATE_UINT32(p_fetch_addr, EHCIState),
  2097. VMSTATE_END_OF_LIST()
  2098. }
  2099. };
  2100. void usb_ehci_realize(EHCIState *s, DeviceState *dev, Error **errp)
  2101. {
  2102. int i;
  2103. if (s->portnr > NB_PORTS) {
  2104. error_setg(errp, "Too many ports! Max. port number is %d.",
  2105. NB_PORTS);
  2106. return;
  2107. }
  2108. usb_bus_new(&s->bus, sizeof(s->bus), s->companion_enable ?
  2109. &ehci_bus_ops_companion : &ehci_bus_ops_standalone, dev);
  2110. for (i = 0; i < s->portnr; i++) {
  2111. usb_register_port(&s->bus, &s->ports[i], s, i, &ehci_port_ops,
  2112. USB_SPEED_MASK_HIGH);
  2113. s->ports[i].dev = 0;
  2114. }
  2115. s->frame_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, ehci_frame_timer, s);
  2116. s->async_bh = qemu_bh_new(ehci_frame_timer, s);
  2117. s->device = dev;
  2118. s->vmstate = qemu_add_vm_change_state_handler(usb_ehci_vm_state_change, s);
  2119. }
  2120. void usb_ehci_unrealize(EHCIState *s, DeviceState *dev, Error **errp)
  2121. {
  2122. trace_usb_ehci_unrealize();
  2123. if (s->frame_timer) {
  2124. timer_del(s->frame_timer);
  2125. timer_free(s->frame_timer);
  2126. s->frame_timer = NULL;
  2127. }
  2128. if (s->async_bh) {
  2129. qemu_bh_delete(s->async_bh);
  2130. }
  2131. ehci_queues_rip_all(s, 0);
  2132. ehci_queues_rip_all(s, 1);
  2133. memory_region_del_subregion(&s->mem, &s->mem_caps);
  2134. memory_region_del_subregion(&s->mem, &s->mem_opreg);
  2135. memory_region_del_subregion(&s->mem, &s->mem_ports);
  2136. usb_bus_release(&s->bus);
  2137. if (s->vmstate) {
  2138. qemu_del_vm_change_state_handler(s->vmstate);
  2139. }
  2140. }
  2141. void usb_ehci_init(EHCIState *s, DeviceState *dev)
  2142. {
  2143. /* 2.2 host controller interface version */
  2144. s->caps[0x00] = (uint8_t)(s->opregbase - s->capsbase);
  2145. s->caps[0x01] = 0x00;
  2146. s->caps[0x02] = 0x00;
  2147. s->caps[0x03] = 0x01; /* HC version */
  2148. s->caps[0x04] = s->portnr; /* Number of downstream ports */
  2149. s->caps[0x05] = 0x00; /* No companion ports at present */
  2150. s->caps[0x06] = 0x00;
  2151. s->caps[0x07] = 0x00;
  2152. s->caps[0x08] = 0x80; /* We can cache whole frame, no 64-bit */
  2153. s->caps[0x0a] = 0x00;
  2154. s->caps[0x0b] = 0x00;
  2155. QTAILQ_INIT(&s->aqueues);
  2156. QTAILQ_INIT(&s->pqueues);
  2157. usb_packet_init(&s->ipacket);
  2158. memory_region_init(&s->mem, OBJECT(dev), "ehci", MMIO_SIZE);
  2159. memory_region_init_io(&s->mem_caps, OBJECT(dev), &ehci_mmio_caps_ops, s,
  2160. "capabilities", CAPA_SIZE);
  2161. memory_region_init_io(&s->mem_opreg, OBJECT(dev), &ehci_mmio_opreg_ops, s,
  2162. "operational", s->portscbase);
  2163. memory_region_init_io(&s->mem_ports, OBJECT(dev), &ehci_mmio_port_ops, s,
  2164. "ports", 4 * s->portnr);
  2165. memory_region_add_subregion(&s->mem, s->capsbase, &s->mem_caps);
  2166. memory_region_add_subregion(&s->mem, s->opregbase, &s->mem_opreg);
  2167. memory_region_add_subregion(&s->mem, s->opregbase + s->portscbase,
  2168. &s->mem_ports);
  2169. }
  2170. /*
  2171. * vim: expandtab ts=4
  2172. */