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