]> git.proxmox.com Git - qemu.git/blame - hw/usb/hcd-ehci.c
lsi: use qbus_reset_all to reset SCSI bus
[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 <kraxel@redhat.com>
9 * Hans de Goede <hdegoede@redhat.com>
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
f79738b0
HG
625static void ehci_queue_stopped(EHCIQueue *q)
626{
627 int endp = get_field(q->qh.epchar, QH_EPCHAR_EP);
628
629 if (!q->last_pid || !q->dev) {
630 return;
631 }
632
633 usb_device_ep_stopped(q->dev, usb_ep_get(q->dev, q->last_pid, endp));
634}
635
5c514681 636static int ehci_cancel_queue(EHCIQueue *q)
c7cdca3b
GH
637{
638 EHCIPacket *p;
5c514681 639 int packets = 0;
c7cdca3b
GH
640
641 p = QTAILQ_FIRST(&q->packets);
642 if (p == NULL) {
f79738b0 643 goto leave;
c7cdca3b
GH
644 }
645
646 trace_usb_ehci_queue_action(q, "cancel");
647 do {
648 ehci_free_packet(p);
5c514681 649 packets++;
c7cdca3b 650 } while ((p = QTAILQ_FIRST(&q->packets)) != NULL);
f79738b0
HG
651
652leave:
653 ehci_queue_stopped(q);
5c514681 654 return packets;
c7cdca3b
GH
655}
656
5c514681 657static int ehci_reset_queue(EHCIQueue *q)
dafe31fc 658{
5c514681
GH
659 int packets;
660
dafe31fc 661 trace_usb_ehci_queue_action(q, "reset");
5c514681 662 packets = ehci_cancel_queue(q);
dafe31fc
HG
663 q->dev = NULL;
664 q->qtdaddr = 0;
bbbc39cc 665 q->last_pid = 0;
5c514681 666 return packets;
dafe31fc
HG
667}
668
3a8ca08e 669static void ehci_free_queue(EHCIQueue *q, const char *warn)
8ac6d699 670{
ae0138a8 671 EHCIQueueHead *head = q->async ? &q->ehci->aqueues : &q->ehci->pqueues;
3a8ca08e 672 int cancelled;
eb36a88e 673
8ac6d699 674 trace_usb_ehci_queue_action(q, "free");
3a8ca08e
HG
675 cancelled = ehci_cancel_queue(q);
676 if (warn && cancelled > 0) {
677 ehci_trace_guest_bug(q->ehci, warn);
678 }
df5d5c5c 679 QTAILQ_REMOVE(head, q, next);
7267c094 680 g_free(q);
8ac6d699
GH
681}
682
df5d5c5c
HG
683static EHCIQueue *ehci_find_queue_by_qh(EHCIState *ehci, uint32_t addr,
684 int async)
8ac6d699 685{
df5d5c5c 686 EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues;
8ac6d699
GH
687 EHCIQueue *q;
688
df5d5c5c 689 QTAILQ_FOREACH(q, head, next) {
8ac6d699
GH
690 if (addr == q->qhaddr) {
691 return q;
692 }
693 }
694 return NULL;
695}
696
8f5457eb 697static void ehci_queues_rip_unused(EHCIState *ehci, int async)
8ac6d699 698{
df5d5c5c 699 EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues;
8f5457eb 700 const char *warn = async ? "guest unlinked busy QH" : NULL;
3a215326 701 uint64_t maxage = FRAME_TIMER_NS * ehci->maxframes * 4;
8ac6d699
GH
702 EHCIQueue *q, *tmp;
703
df5d5c5c 704 QTAILQ_FOREACH_SAFE(q, head, next, tmp) {
8ac6d699
GH
705 if (q->seen) {
706 q->seen = 0;
adddecb1 707 q->ts = ehci->last_run_ns;
8ac6d699
GH
708 continue;
709 }
8f5457eb 710 if (ehci->last_run_ns < q->ts + maxage) {
8ac6d699
GH
711 continue;
712 }
3a8ca08e 713 ehci_free_queue(q, warn);
8ac6d699
GH
714 }
715}
716
8f5457eb
HG
717static void ehci_queues_rip_unseen(EHCIState *ehci, int async)
718{
719 EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues;
720 EHCIQueue *q, *tmp;
721
722 QTAILQ_FOREACH_SAFE(q, head, next, tmp) {
723 if (!q->seen) {
724 ehci_free_queue(q, NULL);
725 }
726 }
727}
728
df5d5c5c 729static void ehci_queues_rip_device(EHCIState *ehci, USBDevice *dev, int async)
07771f6f 730{
df5d5c5c 731 EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues;
07771f6f
GH
732 EHCIQueue *q, *tmp;
733
df5d5c5c 734 QTAILQ_FOREACH_SAFE(q, head, next, tmp) {
e59928b3 735 if (q->dev != dev) {
07771f6f
GH
736 continue;
737 }
3a8ca08e 738 ehci_free_queue(q, NULL);
07771f6f
GH
739 }
740}
741
df5d5c5c 742static void ehci_queues_rip_all(EHCIState *ehci, int async)
8ac6d699 743{
df5d5c5c 744 EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues;
3a8ca08e 745 const char *warn = async ? "guest stopped busy async schedule" : NULL;
8ac6d699
GH
746 EHCIQueue *q, *tmp;
747
df5d5c5c 748 QTAILQ_FOREACH_SAFE(q, head, next, tmp) {
3a8ca08e 749 ehci_free_queue(q, warn);
8ac6d699
GH
750 }
751}
752
94527ead
GH
753/* Attach or detach a device on root hub */
754
755static void ehci_attach(USBPort *port)
756{
757 EHCIState *s = port->opaque;
758 uint32_t *portsc = &s->portsc[port->index];
30e9d412 759 const char *owner = (*portsc & PORTSC_POWNER) ? "comp" : "ehci";
94527ead 760
30e9d412 761 trace_usb_ehci_port_attach(port->index, owner, port->dev->product_desc);
94527ead 762
a0a3167a
HG
763 if (*portsc & PORTSC_POWNER) {
764 USBPort *companion = s->companion_ports[port->index];
765 companion->dev = port->dev;
766 companion->ops->attach(companion);
767 return;
768 }
769
94527ead
GH
770 *portsc |= PORTSC_CONNECT;
771 *portsc |= PORTSC_CSC;
772
7efc17af 773 ehci_raise_irq(s, USBSTS_PCD);
94527ead
GH
774}
775
776static void ehci_detach(USBPort *port)
777{
778 EHCIState *s = port->opaque;
779 uint32_t *portsc = &s->portsc[port->index];
30e9d412 780 const char *owner = (*portsc & PORTSC_POWNER) ? "comp" : "ehci";
94527ead 781
30e9d412 782 trace_usb_ehci_port_detach(port->index, owner);
94527ead 783
a0a3167a
HG
784 if (*portsc & PORTSC_POWNER) {
785 USBPort *companion = s->companion_ports[port->index];
786 companion->ops->detach(companion);
787 companion->dev = NULL;
f76e1d81
HG
788 /*
789 * EHCI spec 4.2.2: "When a disconnect occurs... On the event,
790 * the port ownership is returned immediately to the EHCI controller."
791 */
792 *portsc &= ~PORTSC_POWNER;
a0a3167a
HG
793 return;
794 }
795
df5d5c5c
HG
796 ehci_queues_rip_device(s, port->dev, 0);
797 ehci_queues_rip_device(s, port->dev, 1);
4706ab6c 798
fbd97532 799 *portsc &= ~(PORTSC_CONNECT|PORTSC_PED);
94527ead
GH
800 *portsc |= PORTSC_CSC;
801
7efc17af 802 ehci_raise_irq(s, USBSTS_PCD);
94527ead
GH
803}
804
4706ab6c
HG
805static void ehci_child_detach(USBPort *port, USBDevice *child)
806{
807 EHCIState *s = port->opaque;
a0a3167a
HG
808 uint32_t portsc = s->portsc[port->index];
809
810 if (portsc & PORTSC_POWNER) {
811 USBPort *companion = s->companion_ports[port->index];
812 companion->ops->child_detach(companion, child);
a0a3167a
HG
813 return;
814 }
4706ab6c 815
df5d5c5c
HG
816 ehci_queues_rip_device(s, child, 0);
817 ehci_queues_rip_device(s, child, 1);
4706ab6c
HG
818}
819
a0a3167a
HG
820static void ehci_wakeup(USBPort *port)
821{
822 EHCIState *s = port->opaque;
823 uint32_t portsc = s->portsc[port->index];
824
825 if (portsc & PORTSC_POWNER) {
826 USBPort *companion = s->companion_ports[port->index];
827 if (companion->ops->wakeup) {
828 companion->ops->wakeup(companion);
829 }
37952117 830 return;
a0a3167a 831 }
37952117
HG
832
833 qemu_bh_schedule(s->async_bh);
a0a3167a
HG
834}
835
836static int ehci_register_companion(USBBus *bus, USBPort *ports[],
837 uint32_t portcount, uint32_t firstport)
838{
839 EHCIState *s = container_of(bus, EHCIState, bus);
840 uint32_t i;
841
842 if (firstport + portcount > NB_PORTS) {
843 qerror_report(QERR_INVALID_PARAMETER_VALUE, "firstport",
844 "firstport on masterbus");
845 error_printf_unless_qmp(
846 "firstport value of %u makes companion take ports %u - %u, which "
847 "is outside of the valid range of 0 - %u\n", firstport, firstport,
848 firstport + portcount - 1, NB_PORTS - 1);
849 return -1;
850 }
851
852 for (i = 0; i < portcount; i++) {
853 if (s->companion_ports[firstport + i]) {
854 qerror_report(QERR_INVALID_PARAMETER_VALUE, "masterbus",
855 "an USB masterbus");
856 error_printf_unless_qmp(
857 "port %u on masterbus %s already has a companion assigned\n",
858 firstport + i, bus->qbus.name);
859 return -1;
860 }
861 }
862
863 for (i = 0; i < portcount; i++) {
864 s->companion_ports[firstport + i] = ports[i];
865 s->ports[firstport + i].speedmask |=
866 USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL;
867 /* Ensure devs attached before the initial reset go to the companion */
868 s->portsc[firstport + i] = PORTSC_POWNER;
869 }
870
871 s->companion_count++;
3e4f910c 872 s->caps[0x05] = (s->companion_count << 4) | portcount;
a0a3167a
HG
873
874 return 0;
875}
876
80826240
HG
877static void ehci_wakeup_endpoint(USBBus *bus, USBEndpoint *ep)
878{
879 EHCIState *s = container_of(bus, EHCIState, bus);
880 uint32_t portsc = s->portsc[ep->dev->port->index];
881
882 if (portsc & PORTSC_POWNER) {
883 return;
884 }
885
886 s->periodic_sched_active = PERIODIC_ACTIVE;
887 qemu_bh_schedule(s->async_bh);
888}
889
828143c6
GH
890static USBDevice *ehci_find_device(EHCIState *ehci, uint8_t addr)
891{
892 USBDevice *dev;
893 USBPort *port;
894 int i;
895
896 for (i = 0; i < NB_PORTS; i++) {
897 port = &ehci->ports[i];
898 if (!(ehci->portsc[i] & PORTSC_PED)) {
899 DPRINTF("Port %d not enabled\n", i);
900 continue;
901 }
902 dev = usb_find_device(port, addr);
903 if (dev != NULL) {
904 return dev;
905 }
906 }
907 return NULL;
908}
909
94527ead
GH
910/* 4.1 host controller initialization */
911static void ehci_reset(void *opaque)
912{
913 EHCIState *s = opaque;
94527ead 914 int i;
a0a3167a 915 USBDevice *devs[NB_PORTS];
94527ead 916
439a97cc 917 trace_usb_ehci_reset();
94527ead 918
a0a3167a
HG
919 /*
920 * Do the detach before touching portsc, so that it correctly gets send to
921 * us or to our companion based on PORTSC_POWNER before the reset.
922 */
923 for(i = 0; i < NB_PORTS; i++) {
924 devs[i] = s->ports[i].dev;
891fb2cd
GH
925 if (devs[i] && devs[i]->attached) {
926 usb_detach(&s->ports[i]);
a0a3167a
HG
927 }
928 }
929
3e4f910c
GH
930 memset(&s->opreg, 0x00, sizeof(s->opreg));
931 memset(&s->portsc, 0x00, sizeof(s->portsc));
94527ead
GH
932
933 s->usbcmd = NB_MAXINTRATE << USBCMD_ITC_SH;
934 s->usbsts = USBSTS_HALT;
7efc17af
GH
935 s->usbsts_pending = 0;
936 s->usbsts_frindex = 0;
94527ead
GH
937
938 s->astate = EST_INACTIVE;
939 s->pstate = EST_INACTIVE;
94527ead
GH
940
941 for(i = 0; i < NB_PORTS; i++) {
a0a3167a
HG
942 if (s->companion_ports[i]) {
943 s->portsc[i] = PORTSC_POWNER | PORTSC_PPOWER;
944 } else {
945 s->portsc[i] = PORTSC_PPOWER;
946 }
891fb2cd
GH
947 if (devs[i] && devs[i]->attached) {
948 usb_attach(&s->ports[i]);
d28f4e2d 949 usb_device_reset(devs[i]);
94527ead
GH
950 }
951 }
df5d5c5c
HG
952 ehci_queues_rip_all(s, 0);
953 ehci_queues_rip_all(s, 1);
81d37739 954 qemu_del_timer(s->frame_timer);
0fb3e299 955 qemu_bh_cancel(s->async_bh);
94527ead
GH
956}
957
a8170e5e 958static uint64_t ehci_caps_read(void *ptr, hwaddr addr,
3e4f910c 959 unsigned size)
94527ead
GH
960{
961 EHCIState *s = ptr;
3e4f910c 962 return s->caps[addr];
94527ead
GH
963}
964
a8170e5e 965static uint64_t ehci_opreg_read(void *ptr, hwaddr addr,
3e4f910c 966 unsigned size)
94527ead
GH
967{
968 EHCIState *s = ptr;
969 uint32_t val;
970
9359a58b
HG
971 switch (addr) {
972 case FRINDEX:
973 /* Round down to mult of 8, else it can go backwards on migration */
974 val = s->frindex & ~7;
975 break;
976 default:
977 val = s->opreg[addr >> 2];
978 }
979
27a11324 980 trace_usb_ehci_opreg_read(addr + s->opregbase, addr2str(addr), val);
94527ead
GH
981 return val;
982}
983
a8170e5e 984static uint64_t ehci_port_read(void *ptr, hwaddr addr,
3e4f910c 985 unsigned size)
94527ead
GH
986{
987 EHCIState *s = ptr;
988 uint32_t val;
989
3e4f910c
GH
990 val = s->portsc[addr >> 2];
991 trace_usb_ehci_portsc_read(addr + PORTSC_BEGIN, addr >> 2, val);
94527ead
GH
992 return val;
993}
994
a0a3167a
HG
995static void handle_port_owner_write(EHCIState *s, int port, uint32_t owner)
996{
997 USBDevice *dev = s->ports[port].dev;
998 uint32_t *portsc = &s->portsc[port];
999 uint32_t orig;
1000
1001 if (s->companion_ports[port] == NULL)
1002 return;
1003
1004 owner = owner & PORTSC_POWNER;
1005 orig = *portsc & PORTSC_POWNER;
1006
1007 if (!(owner ^ orig)) {
1008 return;
1009 }
1010
891fb2cd
GH
1011 if (dev && dev->attached) {
1012 usb_detach(&s->ports[port]);
a0a3167a
HG
1013 }
1014
1015 *portsc &= ~PORTSC_POWNER;
1016 *portsc |= owner;
1017
891fb2cd
GH
1018 if (dev && dev->attached) {
1019 usb_attach(&s->ports[port]);
a0a3167a
HG
1020 }
1021}
1022
a8170e5e 1023static void ehci_port_write(void *ptr, hwaddr addr,
3e4f910c 1024 uint64_t val, unsigned size)
94527ead 1025{
3e4f910c
GH
1026 EHCIState *s = ptr;
1027 int port = addr >> 2;
94527ead 1028 uint32_t *portsc = &s->portsc[port];
3e4f910c 1029 uint32_t old = *portsc;
94527ead
GH
1030 USBDevice *dev = s->ports[port].dev;
1031
3e4f910c
GH
1032 trace_usb_ehci_portsc_write(addr + PORTSC_BEGIN, addr >> 2, val);
1033
fbd97532
HG
1034 /* Clear rwc bits */
1035 *portsc &= ~(val & PORTSC_RWC_MASK);
1036 /* The guest may clear, but not set the PED bit */
1037 *portsc &= val | ~PORTSC_PED;
a0a3167a
HG
1038 /* POWNER is masked out by RO_MASK as it is RO when we've no companion */
1039 handle_port_owner_write(s, port, val);
1040 /* And finally apply RO_MASK */
94527ead
GH
1041 val &= PORTSC_RO_MASK;
1042
94527ead 1043 if ((val & PORTSC_PRESET) && !(*portsc & PORTSC_PRESET)) {
dcbd0b5c 1044 trace_usb_ehci_port_reset(port, 1);
94527ead
GH
1045 }
1046
1047 if (!(val & PORTSC_PRESET) &&(*portsc & PORTSC_PRESET)) {
dcbd0b5c 1048 trace_usb_ehci_port_reset(port, 0);
891fb2cd 1049 if (dev && dev->attached) {
d28f4e2d 1050 usb_port_reset(&s->ports[port]);
94527ead
GH
1051 *portsc &= ~PORTSC_CSC;
1052 }
1053
fbd97532
HG
1054 /*
1055 * Table 2.16 Set the enable bit(and enable bit change) to indicate
94527ead 1056 * to SW that this port has a high speed device attached
94527ead 1057 */
891fb2cd 1058 if (dev && dev->attached && (dev->speedmask & USB_SPEED_MASK_HIGH)) {
fbd97532
HG
1059 val |= PORTSC_PED;
1060 }
94527ead
GH
1061 }
1062
1063 *portsc &= ~PORTSC_RO_MASK;
1064 *portsc |= val;
3e4f910c 1065 trace_usb_ehci_portsc_change(addr + PORTSC_BEGIN, addr >> 2, *portsc, old);
94527ead
GH
1066}
1067
a8170e5e 1068static void ehci_opreg_write(void *ptr, hwaddr addr,
3e4f910c 1069 uint64_t val, unsigned size)
94527ead
GH
1070{
1071 EHCIState *s = ptr;
3e4f910c 1072 uint32_t *mmio = s->opreg + (addr >> 2);
c4f8e211 1073 uint32_t old = *mmio;
94527ead 1074 int i;
439a97cc 1075
27a11324 1076 trace_usb_ehci_opreg_write(addr + s->opregbase, addr2str(addr), val);
94527ead 1077
27a11324 1078 switch (addr) {
94527ead 1079 case USBCMD:
7046530c
GH
1080 if (val & USBCMD_HCRESET) {
1081 ehci_reset(s);
1082 val = s->usbcmd;
1083 break;
1084 }
1085
47d073cc
HG
1086 /* not supporting dynamic frame list size at the moment */
1087 if ((val & USBCMD_FLS) && !(s->usbcmd & USBCMD_FLS)) {
1088 fprintf(stderr, "attempt to set frame list size -- value %d\n",
3e4f910c 1089 (int)val & USBCMD_FLS);
47d073cc
HG
1090 val &= ~USBCMD_FLS;
1091 }
1092
a1c3e4b8
HG
1093 if (val & USBCMD_IAAD) {
1094 /*
1095 * Process IAAD immediately, otherwise the Linux IAAD watchdog may
1096 * trigger and re-use a qh without us seeing the unlink.
1097 */
1098 s->async_stepdown = 0;
1099 qemu_bh_schedule(s->async_bh);
1defcbd1 1100 trace_usb_ehci_doorbell_ring();
a1c3e4b8
HG
1101 }
1102
daf25307
GH
1103 if (((USBCMD_RUNSTOP | USBCMD_PSE | USBCMD_ASE) & val) !=
1104 ((USBCMD_RUNSTOP | USBCMD_PSE | USBCMD_ASE) & s->usbcmd)) {
3a215326 1105 if (s->pstate == EST_INACTIVE) {
daf25307
GH
1106 SET_LAST_RUN_CLOCK(s);
1107 }
47d073cc 1108 s->usbcmd = val; /* Set usbcmd for ehci_update_halt() */
daf25307 1109 ehci_update_halt(s);
3a215326 1110 s->async_stepdown = 0;
0262f65a 1111 qemu_bh_schedule(s->async_bh);
94527ead 1112 }
94527ead
GH
1113 break;
1114
94527ead 1115 case USBSTS:
a31f0531
JM
1116 val &= USBSTS_RO_MASK; // bits 6 through 31 are RO
1117 ehci_clear_usbsts(s, val); // bits 0 through 5 are R/WC
439a97cc 1118 val = s->usbsts;
7efc17af 1119 ehci_update_irq(s);
94527ead
GH
1120 break;
1121
94527ead
GH
1122 case USBINTR:
1123 val &= USBINTR_MASK;
40862309
GH
1124 if (ehci_enabled(s) && (USBSTS_FLR & val)) {
1125 qemu_bh_schedule(s->async_bh);
1126 }
94527ead
GH
1127 break;
1128
8a771f77 1129 case FRINDEX:
9359a58b
HG
1130 val &= 0x00003fff; /* frindex is 14bits */
1131 s->usbsts_frindex = val;
8a771f77
HG
1132 break;
1133
94527ead 1134 case CONFIGFLAG:
94527ead
GH
1135 val &= 0x1;
1136 if (val) {
1137 for(i = 0; i < NB_PORTS; i++)
a0a3167a 1138 handle_port_owner_write(s, i, 0);
94527ead
GH
1139 }
1140 break;
1141
1142 case PERIODICLISTBASE:
ec807d12 1143 if (ehci_periodic_enabled(s)) {
94527ead
GH
1144 fprintf(stderr,
1145 "ehci: PERIODIC list base register set while periodic schedule\n"
1146 " is enabled and HC is enabled\n");
1147 }
94527ead
GH
1148 break;
1149
1150 case ASYNCLISTADDR:
ec807d12 1151 if (ehci_async_enabled(s)) {
94527ead
GH
1152 fprintf(stderr,
1153 "ehci: ASYNC list address register set while async schedule\n"
1154 " is enabled and HC is enabled\n");
1155 }
94527ead
GH
1156 break;
1157 }
1158
c4f8e211 1159 *mmio = val;
27a11324
PC
1160 trace_usb_ehci_opreg_change(addr + s->opregbase, addr2str(addr),
1161 *mmio, old);
94527ead
GH
1162}
1163
a5e0139a
GH
1164/*
1165 * Write the qh back to guest physical memory. This step isn't
1166 * in the EHCI spec but we need to do it since we don't share
1167 * physical memory with our guest VM.
1168 *
1169 * The first three dwords are read-only for the EHCI, so skip them
1170 * when writing back the qh.
1171 */
1172static void ehci_flush_qh(EHCIQueue *q)
1173{
1174 uint32_t *qh = (uint32_t *) &q->qh;
1175 uint32_t dwords = sizeof(EHCIqh) >> 2;
1176 uint32_t addr = NLPTR_GET(q->qhaddr);
1177
1178 put_dwords(q->ehci, addr + 3 * sizeof(uint32_t), qh + 3, dwords - 3);
1179}
1180
94527ead
GH
1181// 4.10.2
1182
0122f472 1183static int ehci_qh_do_overlay(EHCIQueue *q)
94527ead 1184{
eb36a88e 1185 EHCIPacket *p = QTAILQ_FIRST(&q->packets);
94527ead
GH
1186 int i;
1187 int dtoggle;
1188 int ping;
1189 int eps;
1190 int reload;
1191
eb36a88e
GH
1192 assert(p != NULL);
1193 assert(p->qtdaddr == q->qtdaddr);
1194
94527ead
GH
1195 // remember values in fields to preserve in qh after overlay
1196
0122f472
GH
1197 dtoggle = q->qh.token & QTD_TOKEN_DTOGGLE;
1198 ping = q->qh.token & QTD_TOKEN_PING;
94527ead 1199
eb36a88e
GH
1200 q->qh.current_qtd = p->qtdaddr;
1201 q->qh.next_qtd = p->qtd.next;
1202 q->qh.altnext_qtd = p->qtd.altnext;
1203 q->qh.token = p->qtd.token;
94527ead
GH
1204
1205
0122f472 1206 eps = get_field(q->qh.epchar, QH_EPCHAR_EPS);
94527ead 1207 if (eps == EHCI_QH_EPS_HIGH) {
0122f472
GH
1208 q->qh.token &= ~QTD_TOKEN_PING;
1209 q->qh.token |= ping;
94527ead
GH
1210 }
1211
0122f472
GH
1212 reload = get_field(q->qh.epchar, QH_EPCHAR_RL);
1213 set_field(&q->qh.altnext_qtd, reload, QH_ALTNEXT_NAKCNT);
94527ead
GH
1214
1215 for (i = 0; i < 5; i++) {
eb36a88e 1216 q->qh.bufptr[i] = p->qtd.bufptr[i];
94527ead
GH
1217 }
1218
0122f472 1219 if (!(q->qh.epchar & QH_EPCHAR_DTC)) {
94527ead 1220 // preserve QH DT bit
0122f472
GH
1221 q->qh.token &= ~QTD_TOKEN_DTOGGLE;
1222 q->qh.token |= dtoggle;
94527ead
GH
1223 }
1224
0122f472
GH
1225 q->qh.bufptr[1] &= ~BUFPTR_CPROGMASK_MASK;
1226 q->qh.bufptr[2] &= ~BUFPTR_FRAMETAG_MASK;
94527ead 1227
a5e0139a 1228 ehci_flush_qh(q);
94527ead
GH
1229
1230 return 0;
1231}
1232
eb36a88e 1233static int ehci_init_transfer(EHCIPacket *p)
94527ead 1234{
0ce668bc 1235 uint32_t cpage, offset, bytes, plen;
68d55358 1236 dma_addr_t page;
94527ead 1237
eb36a88e
GH
1238 cpage = get_field(p->qtd.token, QTD_TOKEN_CPAGE);
1239 bytes = get_field(p->qtd.token, QTD_TOKEN_TBYTES);
1240 offset = p->qtd.bufptr[0] & ~QTD_BUFPTR_MASK;
7ae6ce02 1241 qemu_sglist_init(&p->sgl, 5, p->queue->ehci->dma);
94527ead 1242
0ce668bc
GH
1243 while (bytes > 0) {
1244 if (cpage > 4) {
1245 fprintf(stderr, "cpage out of range (%d)\n", cpage);
01e26b0e 1246 return -1;
0ce668bc 1247 }
94527ead 1248
eb36a88e 1249 page = p->qtd.bufptr[cpage] & QTD_BUFPTR_MASK;
0ce668bc
GH
1250 page += offset;
1251 plen = bytes;
1252 if (plen > 4096 - offset) {
1253 plen = 4096 - offset;
1254 offset = 0;
1255 cpage++;
94527ead
GH
1256 }
1257
eb36a88e 1258 qemu_sglist_add(&p->sgl, page, plen);
0ce668bc
GH
1259 bytes -= plen;
1260 }
1261 return 0;
1262}
94527ead 1263
e696b1da 1264static void ehci_finish_transfer(EHCIQueue *q, int len)
0ce668bc
GH
1265{
1266 uint32_t cpage, offset;
94527ead 1267
e696b1da 1268 if (len > 0) {
0ce668bc
GH
1269 /* update cpage & offset */
1270 cpage = get_field(q->qh.token, QTD_TOKEN_CPAGE);
1271 offset = q->qh.bufptr[0] & ~QTD_BUFPTR_MASK;
94527ead 1272
e696b1da 1273 offset += len;
0ce668bc
GH
1274 cpage += offset >> QTD_BUFPTR_SH;
1275 offset &= ~QTD_BUFPTR_MASK;
94527ead 1276
0ce668bc
GH
1277 set_field(&q->qh.token, cpage, QTD_TOKEN_CPAGE);
1278 q->qh.bufptr[0] &= QTD_BUFPTR_MASK;
1279 q->qh.bufptr[0] |= offset;
1280 }
94527ead
GH
1281}
1282
d47e59b8 1283static void ehci_async_complete_packet(USBPort *port, USBPacket *packet)
94527ead 1284{
eb36a88e 1285 EHCIPacket *p;
a0a3167a
HG
1286 EHCIState *s = port->opaque;
1287 uint32_t portsc = s->portsc[port->index];
1288
1289 if (portsc & PORTSC_POWNER) {
1290 USBPort *companion = s->companion_ports[port->index];
1291 companion->ops->complete(companion, packet);
1292 return;
1293 }
94527ead 1294
eb36a88e 1295 p = container_of(packet, EHCIPacket, packet);
eb36a88e 1296 assert(p->async == EHCI_ASYNC_INFLIGHT);
0cae7b1a 1297
9a77a0f5 1298 if (packet->status == USB_RET_REMOVE_FROM_QUEUE) {
0cae7b1a
HG
1299 trace_usb_ehci_packet_action(p->queue, p, "remove");
1300 ehci_free_packet(p);
1301 return;
1302 }
1303
1304 trace_usb_ehci_packet_action(p->queue, p, "wakeup");
eb36a88e 1305 p->async = EHCI_ASYNC_FINISHED;
ae710b99 1306
80826240
HG
1307 if (!p->queue->async) {
1308 s->periodic_sched_active = PERIODIC_ACTIVE;
ae710b99 1309 }
80826240 1310 qemu_bh_schedule(s->async_bh);
94527ead
GH
1311}
1312
0122f472 1313static void ehci_execute_complete(EHCIQueue *q)
94527ead 1314{
eb36a88e 1315 EHCIPacket *p = QTAILQ_FIRST(&q->packets);
e696b1da 1316 uint32_t tbytes;
eb36a88e
GH
1317
1318 assert(p != NULL);
1319 assert(p->qtdaddr == q->qtdaddr);
ef5b2344
HG
1320 assert(p->async == EHCI_ASYNC_INITIALIZED ||
1321 p->async == EHCI_ASYNC_FINISHED);
94527ead 1322
e696b1da
HG
1323 DPRINTF("execute_complete: qhaddr 0x%x, next 0x%x, qtdaddr 0x%x, "
1324 "status %d, actual_length %d\n",
1325 q->qhaddr, q->qh.next, q->qtdaddr,
1326 p->packet.status, p->packet.actual_length);
94527ead 1327
e696b1da
HG
1328 switch (p->packet.status) {
1329 case USB_RET_SUCCESS:
1330 break;
1331 case USB_RET_IOERROR:
1332 case USB_RET_NODEV:
1333 q->qh.token |= (QTD_TOKEN_HALT | QTD_TOKEN_XACTERR);
1334 set_field(&q->qh.token, 0, QTD_TOKEN_CERR);
1335 ehci_raise_irq(q->ehci, USBSTS_ERRINT);
1336 break;
1337 case USB_RET_STALL:
1338 q->qh.token |= QTD_TOKEN_HALT;
1339 ehci_raise_irq(q->ehci, USBSTS_ERRINT);
1340 break;
1341 case USB_RET_NAK:
1342 set_field(&q->qh.altnext_qtd, 0, QH_ALTNEXT_NAKCNT);
1343 return; /* We're not done yet with this transaction */
1344 case USB_RET_BABBLE:
1345 q->qh.token |= (QTD_TOKEN_HALT | QTD_TOKEN_BABBLE);
1346 ehci_raise_irq(q->ehci, USBSTS_ERRINT);
1347 break;
1348 default:
1349 /* should not be triggerable */
1350 fprintf(stderr, "USB invalid response %d\n", p->packet.status);
1351 assert(0);
1352 break;
1353 }
1354
1355 /* TODO check 4.12 for splits */
1356 tbytes = get_field(q->qh.token, QTD_TOKEN_TBYTES);
1357 if (tbytes && p->pid == USB_TOKEN_IN) {
1358 tbytes -= p->packet.actual_length;
1359 if (tbytes) {
1360 /* 4.15.1.2 must raise int on a short input packet */
1361 ehci_raise_irq(q->ehci, USBSTS_INT);
52c15e59
HG
1362 if (q->async) {
1363 q->ehci->int_req_by_async = true;
1364 }
94527ead
GH
1365 }
1366 } else {
e696b1da 1367 tbytes = 0;
94527ead 1368 }
e696b1da
HG
1369 DPRINTF("updating tbytes to %d\n", tbytes);
1370 set_field(&q->qh.token, tbytes, QTD_TOKEN_TBYTES);
1371
1372 ehci_finish_transfer(q, p->packet.actual_length);
e2f89926 1373 usb_packet_unmap(&p->packet, &p->sgl);
eb36a88e 1374 qemu_sglist_destroy(&p->sgl);
ef5b2344 1375 p->async = EHCI_ASYNC_NONE;
94527ead 1376
0122f472
GH
1377 q->qh.token ^= QTD_TOKEN_DTOGGLE;
1378 q->qh.token &= ~QTD_TOKEN_ACTIVE;
94527ead 1379
553a6a59 1380 if (q->qh.token & QTD_TOKEN_IOC) {
7efc17af 1381 ehci_raise_irq(q->ehci, USBSTS_INT);
44272b0f
HG
1382 if (q->async) {
1383 q->ehci->int_req_by_async = true;
1384 }
94527ead 1385 }
94527ead
GH
1386}
1387
01e26b0e 1388/* 4.10.3 returns "again" */
773dc9cd 1389static int ehci_execute(EHCIPacket *p, const char *action)
94527ead 1390{
079d0b7f 1391 USBEndpoint *ep;
94527ead 1392 int endp;
6ba43f1f 1393 bool spd;
94527ead 1394
ef5b2344
HG
1395 assert(p->async == EHCI_ASYNC_NONE ||
1396 p->async == EHCI_ASYNC_INITIALIZED);
1397
4224558f
GH
1398 if (!(p->qtd.token & QTD_TOKEN_ACTIVE)) {
1399 fprintf(stderr, "Attempting to execute inactive qtd\n");
01e26b0e 1400 return -1;
94527ead
GH
1401 }
1402
549a3c3d 1403 if (get_field(p->qtd.token, QTD_TOKEN_TBYTES) > BUFF_SIZE) {
3a8ca08e
HG
1404 ehci_trace_guest_bug(p->queue->ehci,
1405 "guest requested more bytes than allowed");
01e26b0e 1406 return -1;
94527ead
GH
1407 }
1408
f79738b0
HG
1409 if (!ehci_verify_pid(p->queue, &p->qtd)) {
1410 ehci_queue_stopped(p->queue); /* Mark the ep in the prev dir stopped */
1411 }
51e0c5d0 1412 p->pid = ehci_get_pid(&p->qtd);
bbbc39cc 1413 p->queue->last_pid = p->pid;
4224558f 1414 endp = get_field(p->queue->qh.epchar, QH_EPCHAR_EP);
e59928b3 1415 ep = usb_ep_get(p->queue->dev, p->pid, endp);
94527ead 1416
ef5b2344
HG
1417 if (p->async == EHCI_ASYNC_NONE) {
1418 if (ehci_init_transfer(p) != 0) {
01e26b0e 1419 return -1;
ef5b2344
HG
1420 }
1421
6ba43f1f 1422 spd = (p->pid == USB_TOKEN_IN && NLPTR_TBIT(p->qtd.altnext) == 0);
a6fb2ddb
HG
1423 usb_packet_setup(&p->packet, p->pid, ep, p->qtdaddr, spd,
1424 (p->qtd.token & QTD_TOKEN_IOC) != 0);
ef5b2344
HG
1425 usb_packet_map(&p->packet, &p->sgl);
1426 p->async = EHCI_ASYNC_INITIALIZED;
1427 }
0ce668bc 1428
773dc9cd 1429 trace_usb_ehci_packet_action(p->queue, p, action);
9a77a0f5
HG
1430 usb_handle_packet(p->queue->dev, &p->packet);
1431 DPRINTF("submit: qh 0x%x next 0x%x qtd 0x%x pid 0x%x len %zd endp 0x%x "
1432 "status %d actual_length %d\n", p->queue->qhaddr, p->qtd.next,
1433 p->qtdaddr, p->pid, p->packet.iov.size, endp, p->packet.status,
1434 p->packet.actual_length);
94527ead 1435
9a77a0f5 1436 if (p->packet.actual_length > BUFF_SIZE) {
94527ead 1437 fprintf(stderr, "ret from usb_handle_packet > BUFF_SIZE\n");
01e26b0e 1438 return -1;
94527ead
GH
1439 }
1440
01e26b0e 1441 return 1;
94527ead
GH
1442}
1443
1444/* 4.7.2
1445 */
1446
1447static int ehci_process_itd(EHCIState *ehci,
e983395d
GH
1448 EHCIitd *itd,
1449 uint32_t addr)
94527ead 1450{
94527ead 1451 USBDevice *dev;
079d0b7f 1452 USBEndpoint *ep;
828143c6 1453 uint32_t i, len, pid, dir, devaddr, endp;
e654887f 1454 uint32_t pg, off, ptr1, ptr2, max, mult;
94527ead 1455
80826240
HG
1456 ehci->periodic_sched_active = PERIODIC_ACTIVE;
1457
94527ead 1458 dir =(itd->bufptr[1] & ITD_BUFPTR_DIRECTION);
e654887f 1459 devaddr = get_field(itd->bufptr[0], ITD_BUFPTR_DEVADDR);
94527ead 1460 endp = get_field(itd->bufptr[0], ITD_BUFPTR_EP);
e654887f
GH
1461 max = get_field(itd->bufptr[1], ITD_BUFPTR_MAXPKT);
1462 mult = get_field(itd->bufptr[2], ITD_BUFPTR_MULT);
94527ead
GH
1463
1464 for(i = 0; i < 8; i++) {
1465 if (itd->transact[i] & ITD_XACT_ACTIVE) {
e654887f
GH
1466 pg = get_field(itd->transact[i], ITD_XACT_PGSEL);
1467 off = itd->transact[i] & ITD_XACT_OFFSET_MASK;
1468 ptr1 = (itd->bufptr[pg] & ITD_BUFPTR_MASK);
1469 ptr2 = (itd->bufptr[pg+1] & ITD_BUFPTR_MASK);
1470 len = get_field(itd->transact[i], ITD_XACT_LENGTH);
1471
1472 if (len > max * mult) {
1473 len = max * mult;
1474 }
94527ead
GH
1475
1476 if (len > BUFF_SIZE) {
01e26b0e 1477 return -1;
94527ead
GH
1478 }
1479
7ae6ce02 1480 qemu_sglist_init(&ehci->isgl, 2, ehci->dma);
e654887f
GH
1481 if (off + len > 4096) {
1482 /* transfer crosses page border */
0ce668bc
GH
1483 uint32_t len2 = off + len - 4096;
1484 uint32_t len1 = len - len2;
1485 qemu_sglist_add(&ehci->isgl, ptr1 + off, len1);
1486 qemu_sglist_add(&ehci->isgl, ptr2, len2);
e654887f 1487 } else {
0ce668bc 1488 qemu_sglist_add(&ehci->isgl, ptr1 + off, len);
e654887f 1489 }
94527ead 1490
0ce668bc 1491 pid = dir ? USB_TOKEN_IN : USB_TOKEN_OUT;
94527ead 1492
079d0b7f
GH
1493 dev = ehci_find_device(ehci, devaddr);
1494 ep = usb_ep_get(dev, pid, endp);
7ce86aa1 1495 if (ep && ep->type == USB_ENDPOINT_XFER_ISOC) {
a6fb2ddb
HG
1496 usb_packet_setup(&ehci->ipacket, pid, ep, addr, false,
1497 (itd->transact[i] & ITD_XACT_IOC) != 0);
aa0568ff 1498 usb_packet_map(&ehci->ipacket, &ehci->isgl);
9a77a0f5 1499 usb_handle_packet(dev, &ehci->ipacket);
e2f89926 1500 usb_packet_unmap(&ehci->ipacket, &ehci->isgl);
aa0568ff
GH
1501 } else {
1502 DPRINTF("ISOCH: attempt to addess non-iso endpoint\n");
e696b1da
HG
1503 ehci->ipacket.status = USB_RET_NAK;
1504 ehci->ipacket.actual_length = 0;
aa0568ff 1505 }
0ce668bc
GH
1506 qemu_sglist_destroy(&ehci->isgl);
1507
e696b1da
HG
1508 switch (ehci->ipacket.status) {
1509 case USB_RET_SUCCESS:
1510 break;
1511 default:
1512 fprintf(stderr, "Unexpected iso usb result: %d\n",
1513 ehci->ipacket.status);
1514 /* Fall through */
1515 case USB_RET_IOERROR:
1516 case USB_RET_NODEV:
1517 /* 3.3.2: XACTERR is only allowed on IN transactions */
1518 if (dir) {
1519 itd->transact[i] |= ITD_XACT_XACTERR;
7efc17af 1520 ehci_raise_irq(ehci, USBSTS_ERRINT);
5eafd438 1521 }
e696b1da
HG
1522 break;
1523 case USB_RET_BABBLE:
1524 itd->transact[i] |= ITD_XACT_BABBLE;
1525 ehci_raise_irq(ehci, USBSTS_ERRINT);
1526 break;
1527 case USB_RET_NAK:
1528 /* no data for us, so do a zero-length transfer */
1529 ehci->ipacket.actual_length = 0;
1530 break;
5eafd438 1531 }
e696b1da
HG
1532 if (!dir) {
1533 set_field(&itd->transact[i], len - ehci->ipacket.actual_length,
1534 ITD_XACT_LENGTH); /* OUT */
1535 } else {
1536 set_field(&itd->transact[i], ehci->ipacket.actual_length,
1537 ITD_XACT_LENGTH); /* IN */
94527ead 1538 }
df787185 1539 if (itd->transact[i] & ITD_XACT_IOC) {
7efc17af 1540 ehci_raise_irq(ehci, USBSTS_INT);
df787185 1541 }
e654887f 1542 itd->transact[i] &= ~ITD_XACT_ACTIVE;
94527ead
GH
1543 }
1544 }
1545 return 0;
1546}
1547
cd665715 1548
94527ead
GH
1549/* This state is the entry point for asynchronous schedule
1550 * processing. Entry here consitutes a EHCI start event state (4.8.5)
1551 */
26d53979 1552static int ehci_state_waitlisthead(EHCIState *ehci, int async)
94527ead 1553{
0122f472 1554 EHCIqh qh;
94527ead
GH
1555 int i = 0;
1556 int again = 0;
1557 uint32_t entry = ehci->asynclistaddr;
1558
1559 /* set reclamation flag at start event (4.8.6) */
1560 if (async) {
439a97cc 1561 ehci_set_usbsts(ehci, USBSTS_REC);
94527ead
GH
1562 }
1563
8f5457eb 1564 ehci_queues_rip_unused(ehci, async);
8ac6d699 1565
94527ead
GH
1566 /* Find the head of the list (4.9.1.1) */
1567 for(i = 0; i < MAX_QH; i++) {
55903f1d
GH
1568 if (get_dwords(ehci, NLPTR_GET(entry), (uint32_t *) &qh,
1569 sizeof(EHCIqh) >> 2) < 0) {
1570 return 0;
1571 }
8ac6d699 1572 ehci_trace_qh(NULL, NLPTR_GET(entry), &qh);
94527ead 1573
0122f472 1574 if (qh.epchar & QH_EPCHAR_H) {
94527ead
GH
1575 if (async) {
1576 entry |= (NLPTR_TYPE_QH << 1);
1577 }
1578
0122f472 1579 ehci_set_fetch_addr(ehci, async, entry);
26d53979 1580 ehci_set_state(ehci, async, EST_FETCHENTRY);
94527ead
GH
1581 again = 1;
1582 goto out;
1583 }
1584
0122f472 1585 entry = qh.next;
94527ead 1586 if (entry == ehci->asynclistaddr) {
94527ead
GH
1587 break;
1588 }
1589 }
1590
1591 /* no head found for list. */
1592
26d53979 1593 ehci_set_state(ehci, async, EST_ACTIVE);
94527ead
GH
1594
1595out:
1596 return again;
1597}
1598
1599
1600/* This state is the entry point for periodic schedule processing as
1601 * well as being a continuation state for async processing.
1602 */
26d53979 1603static int ehci_state_fetchentry(EHCIState *ehci, int async)
94527ead
GH
1604{
1605 int again = 0;
0122f472 1606 uint32_t entry = ehci_get_fetch_addr(ehci, async);
94527ead 1607
2a5ff735 1608 if (NLPTR_TBIT(entry)) {
26d53979 1609 ehci_set_state(ehci, async, EST_ACTIVE);
94527ead
GH
1610 goto out;
1611 }
1612
1613 /* section 4.8, only QH in async schedule */
1614 if (async && (NLPTR_TYPE_GET(entry) != NLPTR_TYPE_QH)) {
1615 fprintf(stderr, "non queue head request in async schedule\n");
1616 return -1;
1617 }
1618
1619 switch (NLPTR_TYPE_GET(entry)) {
1620 case NLPTR_TYPE_QH:
26d53979 1621 ehci_set_state(ehci, async, EST_FETCHQH);
94527ead
GH
1622 again = 1;
1623 break;
1624
1625 case NLPTR_TYPE_ITD:
26d53979 1626 ehci_set_state(ehci, async, EST_FETCHITD);
94527ead
GH
1627 again = 1;
1628 break;
1629
2fe80192
GH
1630 case NLPTR_TYPE_STITD:
1631 ehci_set_state(ehci, async, EST_FETCHSITD);
1632 again = 1;
1633 break;
1634
94527ead 1635 default:
2fe80192 1636 /* TODO: handle FSTN type */
94527ead
GH
1637 fprintf(stderr, "FETCHENTRY: entry at %X is of type %d "
1638 "which is not supported yet\n", entry, NLPTR_TYPE_GET(entry));
1639 return -1;
1640 }
1641
1642out:
1643 return again;
1644}
1645
0122f472 1646static EHCIQueue *ehci_state_fetchqh(EHCIState *ehci, int async)
94527ead 1647{
c6432634 1648 uint32_t entry;
0122f472 1649 EHCIQueue *q;
dafe31fc 1650 EHCIqh qh;
94527ead 1651
0122f472 1652 entry = ehci_get_fetch_addr(ehci, async);
df5d5c5c 1653 q = ehci_find_queue_by_qh(ehci, entry, async);
8ac6d699 1654 if (NULL == q) {
8f6d5e26 1655 q = ehci_alloc_queue(ehci, entry, async);
8ac6d699 1656 }
8ac6d699 1657
8f6d5e26 1658 q->seen++;
8ac6d699
GH
1659 if (q->seen > 1) {
1660 /* we are going in circles -- stop processing */
1661 ehci_set_state(ehci, async, EST_ACTIVE);
1662 q = NULL;
1663 goto out;
1664 }
94527ead 1665
55903f1d
GH
1666 if (get_dwords(ehci, NLPTR_GET(q->qhaddr),
1667 (uint32_t *) &qh, sizeof(EHCIqh) >> 2) < 0) {
1668 q = NULL;
1669 goto out;
1670 }
dafe31fc
HG
1671 ehci_trace_qh(q, NLPTR_GET(q->qhaddr), &qh);
1672
1673 /*
1674 * The overlay area of the qh should never be changed by the guest,
1675 * except when idle, in which case the reset is a nop.
1676 */
c6432634 1677 if (!ehci_verify_qh(q, &qh)) {
5c514681
GH
1678 if (ehci_reset_queue(q) > 0) {
1679 ehci_trace_guest_bug(ehci, "guest updated active QH");
1680 }
dafe31fc
HG
1681 }
1682 q->qh = qh;
1683
cae5d3f4
HG
1684 q->transact_ctr = get_field(q->qh.epcap, QH_EPCAP_MULT);
1685 if (q->transact_ctr == 0) { /* Guest bug in some versions of windows */
1686 q->transact_ctr = 4;
1687 }
1688
e59928b3 1689 if (q->dev == NULL) {
c6432634
HG
1690 q->dev = ehci_find_device(q->ehci,
1691 get_field(q->qh.epchar, QH_EPCHAR_DEVADDR));
e59928b3
GH
1692 }
1693
0122f472 1694 if (async && (q->qh.epchar & QH_EPCHAR_H)) {
94527ead
GH
1695
1696 /* EHCI spec version 1.0 Section 4.8.3 & 4.10.1 */
1697 if (ehci->usbsts & USBSTS_REC) {
439a97cc 1698 ehci_clear_usbsts(ehci, USBSTS_REC);
94527ead
GH
1699 } else {
1700 DPRINTF("FETCHQH: QH 0x%08x. H-bit set, reclamation status reset"
0122f472 1701 " - done processing\n", q->qhaddr);
26d53979 1702 ehci_set_state(ehci, async, EST_ACTIVE);
0122f472 1703 q = NULL;
94527ead
GH
1704 goto out;
1705 }
1706 }
1707
1708#if EHCI_DEBUG
0122f472 1709 if (q->qhaddr != q->qh.next) {
94527ead 1710 DPRINTF("FETCHQH: QH 0x%08x (h %x halt %x active %x) next 0x%08x\n",
0122f472
GH
1711 q->qhaddr,
1712 q->qh.epchar & QH_EPCHAR_H,
1713 q->qh.token & QTD_TOKEN_HALT,
1714 q->qh.token & QTD_TOKEN_ACTIVE,
1715 q->qh.next);
94527ead
GH
1716 }
1717#endif
1718
0122f472 1719 if (q->qh.token & QTD_TOKEN_HALT) {
26d53979 1720 ehci_set_state(ehci, async, EST_HORIZONTALQH);
94527ead 1721
2a5ff735
HG
1722 } else if ((q->qh.token & QTD_TOKEN_ACTIVE) &&
1723 (NLPTR_TBIT(q->qh.current_qtd) == 0)) {
0122f472 1724 q->qtdaddr = q->qh.current_qtd;
26d53979 1725 ehci_set_state(ehci, async, EST_FETCHQTD);
94527ead
GH
1726
1727 } else {
1728 /* EHCI spec version 1.0 Section 4.10.2 */
26d53979 1729 ehci_set_state(ehci, async, EST_ADVANCEQUEUE);
94527ead
GH
1730 }
1731
1732out:
0122f472 1733 return q;
94527ead
GH
1734}
1735
26d53979 1736static int ehci_state_fetchitd(EHCIState *ehci, int async)
94527ead 1737{
0122f472 1738 uint32_t entry;
94527ead
GH
1739 EHCIitd itd;
1740
0122f472
GH
1741 assert(!async);
1742 entry = ehci_get_fetch_addr(ehci, async);
1743
55903f1d
GH
1744 if (get_dwords(ehci, NLPTR_GET(entry), (uint32_t *) &itd,
1745 sizeof(EHCIitd) >> 2) < 0) {
1746 return -1;
1747 }
0122f472 1748 ehci_trace_itd(ehci, entry, &itd);
94527ead 1749
e983395d 1750 if (ehci_process_itd(ehci, &itd, entry) != 0) {
94527ead
GH
1751 return -1;
1752 }
1753
68d55358
DG
1754 put_dwords(ehci, NLPTR_GET(entry), (uint32_t *) &itd,
1755 sizeof(EHCIitd) >> 2);
0122f472 1756 ehci_set_fetch_addr(ehci, async, itd.next);
26d53979 1757 ehci_set_state(ehci, async, EST_FETCHENTRY);
94527ead
GH
1758
1759 return 1;
1760}
1761
2fe80192
GH
1762static int ehci_state_fetchsitd(EHCIState *ehci, int async)
1763{
1764 uint32_t entry;
1765 EHCIsitd sitd;
1766
1767 assert(!async);
1768 entry = ehci_get_fetch_addr(ehci, async);
1769
55903f1d
GH
1770 if (get_dwords(ehci, NLPTR_GET(entry), (uint32_t *)&sitd,
1771 sizeof(EHCIsitd) >> 2) < 0) {
1772 return 0;
1773 }
2fe80192
GH
1774 ehci_trace_sitd(ehci, entry, &sitd);
1775
1776 if (!(sitd.results & SITD_RESULTS_ACTIVE)) {
1777 /* siTD is not active, nothing to do */;
1778 } else {
1779 /* TODO: split transfers are not implemented */
1780 fprintf(stderr, "WARNING: Skipping active siTD\n");
1781 }
1782
1783 ehci_set_fetch_addr(ehci, async, sitd.next);
1784 ehci_set_state(ehci, async, EST_FETCHENTRY);
1785 return 1;
1786}
1787
94527ead 1788/* Section 4.10.2 - paragraph 3 */
ae0138a8 1789static int ehci_state_advqueue(EHCIQueue *q)
94527ead
GH
1790{
1791#if 0
1792 /* TO-DO: 4.10.2 - paragraph 2
1793 * if I-bit is set to 1 and QH is not active
1794 * go to horizontal QH
1795 */
1796 if (I-bit set) {
26d53979 1797 ehci_set_state(ehci, async, EST_HORIZONTALQH);
94527ead
GH
1798 goto out;
1799 }
1800#endif
1801
1802 /*
1803 * want data and alt-next qTD is valid
1804 */
0122f472 1805 if (((q->qh.token & QTD_TOKEN_TBYTES_MASK) != 0) &&
0122f472
GH
1806 (NLPTR_TBIT(q->qh.altnext_qtd) == 0)) {
1807 q->qtdaddr = q->qh.altnext_qtd;
ae0138a8 1808 ehci_set_state(q->ehci, q->async, EST_FETCHQTD);
94527ead
GH
1809
1810 /*
1811 * next qTD is valid
1812 */
2a5ff735 1813 } else if (NLPTR_TBIT(q->qh.next_qtd) == 0) {
0122f472 1814 q->qtdaddr = q->qh.next_qtd;
ae0138a8 1815 ehci_set_state(q->ehci, q->async, EST_FETCHQTD);
94527ead
GH
1816
1817 /*
1818 * no valid qTD, try next QH
1819 */
1820 } else {
ae0138a8 1821 ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH);
94527ead
GH
1822 }
1823
1824 return 1;
1825}
1826
1827/* Section 4.10.2 - paragraph 4 */
ae0138a8 1828static int ehci_state_fetchqtd(EHCIQueue *q)
94527ead 1829{
eb36a88e
GH
1830 EHCIqtd qtd;
1831 EHCIPacket *p;
b4ea8664 1832 int again = 1;
94527ead 1833
55903f1d
GH
1834 if (get_dwords(q->ehci, NLPTR_GET(q->qtdaddr), (uint32_t *) &qtd,
1835 sizeof(EHCIqtd) >> 2) < 0) {
1836 return 0;
1837 }
eb36a88e 1838 ehci_trace_qtd(q, NLPTR_GET(q->qtdaddr), &qtd);
94527ead 1839
773dc9cd 1840 p = QTAILQ_FIRST(&q->packets);
773dc9cd 1841 if (p != NULL) {
c6432634 1842 if (!ehci_verify_qtd(p, &qtd)) {
287fd3f1 1843 ehci_cancel_queue(q);
d066c57b
HG
1844 if (qtd.token & QTD_TOKEN_ACTIVE) {
1845 ehci_trace_guest_bug(q->ehci, "guest updated active qTD");
1846 }
287fd3f1
GH
1847 p = NULL;
1848 } else {
1849 p->qtd = qtd;
1850 ehci_qh_do_overlay(q);
1851 }
1852 }
1853
1854 if (!(qtd.token & QTD_TOKEN_ACTIVE)) {
287fd3f1 1855 ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH);
287fd3f1 1856 } else if (p != NULL) {
adf47834
HG
1857 switch (p->async) {
1858 case EHCI_ASYNC_NONE:
ef5b2344 1859 case EHCI_ASYNC_INITIALIZED:
cae5d3f4 1860 /* Not yet executed (MULT), or previously nacked (int) packet */
ef5b2344
HG
1861 ehci_set_state(q->ehci, q->async, EST_EXECUTE);
1862 break;
adf47834 1863 case EHCI_ASYNC_INFLIGHT:
b4ea8664 1864 /* Check if the guest has added new tds to the queue */
01e26b0e 1865 again = ehci_fill_queue(QTAILQ_LAST(&q->packets, pkts_head));
ef5b2344 1866 /* Unfinished async handled packet, go horizontal */
ae0138a8 1867 ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH);
adf47834
HG
1868 break;
1869 case EHCI_ASYNC_FINISHED:
e3fdfd48 1870 /* Complete executing of the packet */
ae0138a8 1871 ehci_set_state(q->ehci, q->async, EST_EXECUTING);
adf47834 1872 break;
773dc9cd 1873 }
287fd3f1 1874 } else {
eb36a88e
GH
1875 p = ehci_alloc_packet(q);
1876 p->qtdaddr = q->qtdaddr;
1877 p->qtd = qtd;
ae0138a8 1878 ehci_set_state(q->ehci, q->async, EST_EXECUTE);
94527ead
GH
1879 }
1880
1881 return again;
1882}
1883
ae0138a8 1884static int ehci_state_horizqh(EHCIQueue *q)
94527ead
GH
1885{
1886 int again = 0;
1887
ae0138a8
GH
1888 if (ehci_get_fetch_addr(q->ehci, q->async) != q->qh.next) {
1889 ehci_set_fetch_addr(q->ehci, q->async, q->qh.next);
1890 ehci_set_state(q->ehci, q->async, EST_FETCHENTRY);
94527ead
GH
1891 again = 1;
1892 } else {
ae0138a8 1893 ehci_set_state(q->ehci, q->async, EST_ACTIVE);
94527ead
GH
1894 }
1895
1896 return again;
1897}
1898
01e26b0e 1899/* Returns "again" */
eff6dce7 1900static int ehci_fill_queue(EHCIPacket *p)
773dc9cd 1901{
36dfe324 1902 USBEndpoint *ep = p->packet.ep;
773dc9cd
GH
1903 EHCIQueue *q = p->queue;
1904 EHCIqtd qtd = p->qtd;
601a2347 1905 uint32_t qtdaddr;
773dc9cd
GH
1906
1907 for (;;) {
773dc9cd
GH
1908 if (NLPTR_TBIT(qtd.next) != 0) {
1909 break;
1910 }
1911 qtdaddr = qtd.next;
e3a36bce
HG
1912 /*
1913 * Detect circular td lists, Windows creates these, counting on the
1914 * active bit going low after execution to make the queue stop.
1915 */
601a2347
HG
1916 QTAILQ_FOREACH(p, &q->packets, next) {
1917 if (p->qtdaddr == qtdaddr) {
1918 goto leave;
1919 }
e3a36bce 1920 }
55903f1d
GH
1921 if (get_dwords(q->ehci, NLPTR_GET(qtdaddr),
1922 (uint32_t *) &qtd, sizeof(EHCIqtd) >> 2) < 0) {
1923 return -1;
1924 }
773dc9cd
GH
1925 ehci_trace_qtd(q, NLPTR_GET(qtdaddr), &qtd);
1926 if (!(qtd.token & QTD_TOKEN_ACTIVE)) {
1927 break;
1928 }
bbbc39cc
HG
1929 if (!ehci_verify_pid(q, &qtd)) {
1930 ehci_trace_guest_bug(q->ehci, "guest queued token with wrong pid");
1931 break;
1932 }
773dc9cd
GH
1933 p = ehci_alloc_packet(q);
1934 p->qtdaddr = qtdaddr;
1935 p->qtd = qtd;
01e26b0e
HG
1936 if (ehci_execute(p, "queue") == -1) {
1937 return -1;
eff6dce7 1938 }
01e26b0e 1939 assert(p->packet.status == USB_RET_ASYNC);
773dc9cd
GH
1940 p->async = EHCI_ASYNC_INFLIGHT;
1941 }
601a2347 1942leave:
01e26b0e
HG
1943 usb_device_flush_ep_queue(ep->dev, ep);
1944 return 1;
773dc9cd
GH
1945}
1946
ae0138a8 1947static int ehci_state_execute(EHCIQueue *q)
94527ead 1948{
eb36a88e 1949 EHCIPacket *p = QTAILQ_FIRST(&q->packets);
94527ead 1950 int again = 0;
94527ead 1951
eb36a88e
GH
1952 assert(p != NULL);
1953 assert(p->qtdaddr == q->qtdaddr);
1954
0122f472 1955 if (ehci_qh_do_overlay(q) != 0) {
94527ead
GH
1956 return -1;
1957 }
1958
94527ead
GH
1959 // TODO verify enough time remains in the uframe as in 4.4.1.1
1960 // TODO write back ptr to async list when done or out of time
94527ead 1961
cae5d3f4
HG
1962 /* 4.10.3, bottom of page 82, go horizontal on transaction counter == 0 */
1963 if (!q->async && q->transact_ctr == 0) {
1964 ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH);
1965 again = 1;
1966 goto out;
94527ead
GH
1967 }
1968
ae0138a8 1969 if (q->async) {
0122f472 1970 ehci_set_usbsts(q->ehci, USBSTS_REC);
94527ead
GH
1971 }
1972
01e26b0e
HG
1973 again = ehci_execute(p, "process");
1974 if (again == -1) {
94527ead
GH
1975 goto out;
1976 }
01e26b0e 1977 if (p->packet.status == USB_RET_ASYNC) {
8ac6d699 1978 ehci_flush_qh(q);
773dc9cd 1979 trace_usb_ehci_packet_action(p->queue, p, "async");
eb36a88e 1980 p->async = EHCI_ASYNC_INFLIGHT;
ae0138a8 1981 ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH);
cae5d3f4 1982 if (q->async) {
01e26b0e 1983 again = ehci_fill_queue(p);
cae5d3f4
HG
1984 } else {
1985 again = 1;
1986 }
8ac6d699 1987 goto out;
94527ead
GH
1988 }
1989
ae0138a8 1990 ehci_set_state(q->ehci, q->async, EST_EXECUTING);
8ac6d699
GH
1991 again = 1;
1992
94527ead
GH
1993out:
1994 return again;
1995}
1996
ae0138a8 1997static int ehci_state_executing(EHCIQueue *q)
94527ead 1998{
eb36a88e 1999 EHCIPacket *p = QTAILQ_FIRST(&q->packets);
94527ead 2000
eb36a88e
GH
2001 assert(p != NULL);
2002 assert(p->qtdaddr == q->qtdaddr);
2003
0122f472 2004 ehci_execute_complete(q);
94527ead 2005
cae5d3f4
HG
2006 /* 4.10.3 */
2007 if (!q->async && q->transact_ctr > 0) {
2008 q->transact_ctr--;
94527ead
GH
2009 }
2010
94527ead 2011 /* 4.10.5 */
e696b1da 2012 if (p->packet.status == USB_RET_NAK) {
ae0138a8 2013 ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH);
94527ead 2014 } else {
ae0138a8 2015 ehci_set_state(q->ehci, q->async, EST_WRITEBACK);
94527ead
GH
2016 }
2017
8ac6d699 2018 ehci_flush_qh(q);
574ef171 2019 return 1;
94527ead
GH
2020}
2021
2022
ae0138a8 2023static int ehci_state_writeback(EHCIQueue *q)
94527ead 2024{
eb36a88e 2025 EHCIPacket *p = QTAILQ_FIRST(&q->packets);
4ed1c57a 2026 uint32_t *qtd, addr;
94527ead
GH
2027 int again = 0;
2028
2029 /* Write back the QTD from the QH area */
eb36a88e
GH
2030 assert(p != NULL);
2031 assert(p->qtdaddr == q->qtdaddr);
2032
2033 ehci_trace_qtd(q, NLPTR_GET(p->qtdaddr), (EHCIqtd *) &q->qh.next_qtd);
4ed1c57a
GH
2034 qtd = (uint32_t *) &q->qh.next_qtd;
2035 addr = NLPTR_GET(p->qtdaddr);
2036 put_dwords(q->ehci, addr + 2 * sizeof(uint32_t), qtd + 2, 2);
eb36a88e 2037 ehci_free_packet(p);
94527ead 2038
d2bd525f
GH
2039 /*
2040 * EHCI specs say go horizontal here.
2041 *
2042 * We can also advance the queue here for performance reasons. We
2043 * need to take care to only take that shortcut in case we've
2044 * processed the qtd just written back without errors, i.e. halt
2045 * bit is clear.
94527ead 2046 */
d2bd525f 2047 if (q->qh.token & QTD_TOKEN_HALT) {
ae0138a8 2048 ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH);
d2bd525f
GH
2049 again = 1;
2050 } else {
ae0138a8 2051 ehci_set_state(q->ehci, q->async, EST_ADVANCEQUEUE);
94527ead 2052 again = 1;
d2bd525f 2053 }
94527ead
GH
2054 return again;
2055}
2056
2057/*
2058 * This is the state machine that is common to both async and periodic
2059 */
2060
ae0138a8 2061static void ehci_advance_state(EHCIState *ehci, int async)
94527ead 2062{
0122f472 2063 EHCIQueue *q = NULL;
94527ead 2064 int again;
94527ead
GH
2065
2066 do {
26d53979 2067 switch(ehci_get_state(ehci, async)) {
94527ead 2068 case EST_WAITLISTHEAD:
26d53979 2069 again = ehci_state_waitlisthead(ehci, async);
94527ead
GH
2070 break;
2071
2072 case EST_FETCHENTRY:
26d53979 2073 again = ehci_state_fetchentry(ehci, async);
94527ead
GH
2074 break;
2075
2076 case EST_FETCHQH:
0122f472 2077 q = ehci_state_fetchqh(ehci, async);
ae0138a8
GH
2078 if (q != NULL) {
2079 assert(q->async == async);
2080 again = 1;
2081 } else {
2082 again = 0;
2083 }
94527ead
GH
2084 break;
2085
2086 case EST_FETCHITD:
26d53979 2087 again = ehci_state_fetchitd(ehci, async);
94527ead
GH
2088 break;
2089
2fe80192
GH
2090 case EST_FETCHSITD:
2091 again = ehci_state_fetchsitd(ehci, async);
2092 break;
2093
94527ead 2094 case EST_ADVANCEQUEUE:
ae0138a8 2095 again = ehci_state_advqueue(q);
94527ead
GH
2096 break;
2097
2098 case EST_FETCHQTD:
ae0138a8 2099 again = ehci_state_fetchqtd(q);
94527ead
GH
2100 break;
2101
2102 case EST_HORIZONTALQH:
ae0138a8 2103 again = ehci_state_horizqh(q);
94527ead
GH
2104 break;
2105
2106 case EST_EXECUTE:
ae0138a8 2107 again = ehci_state_execute(q);
3a215326
GH
2108 if (async) {
2109 ehci->async_stepdown = 0;
2110 }
94527ead
GH
2111 break;
2112
2113 case EST_EXECUTING:
8ac6d699 2114 assert(q != NULL);
3a215326
GH
2115 if (async) {
2116 ehci->async_stepdown = 0;
2117 }
ae0138a8 2118 again = ehci_state_executing(q);
94527ead
GH
2119 break;
2120
2121 case EST_WRITEBACK:
b2467216 2122 assert(q != NULL);
ae0138a8 2123 again = ehci_state_writeback(q);
80826240
HG
2124 if (!async) {
2125 ehci->periodic_sched_active = PERIODIC_ACTIVE;
2126 }
94527ead
GH
2127 break;
2128
2129 default:
2130 fprintf(stderr, "Bad state!\n");
2131 again = -1;
8ac6d699 2132 assert(0);
94527ead
GH
2133 break;
2134 }
2135
2136 if (again < 0) {
2137 fprintf(stderr, "processing error - resetting ehci HC\n");
2138 ehci_reset(ehci);
2139 again = 0;
2140 }
2141 }
2142 while (again);
94527ead
GH
2143}
2144
2145static void ehci_advance_async_state(EHCIState *ehci)
2146{
df5d5c5c 2147 const int async = 1;
94527ead 2148
26d53979 2149 switch(ehci_get_state(ehci, async)) {
94527ead 2150 case EST_INACTIVE:
ec807d12 2151 if (!ehci_async_enabled(ehci)) {
94527ead
GH
2152 break;
2153 }
26d53979 2154 ehci_set_state(ehci, async, EST_ACTIVE);
94527ead
GH
2155 // No break, fall through to ACTIVE
2156
2157 case EST_ACTIVE:
ec807d12 2158 if (!ehci_async_enabled(ehci)) {
e850c2b4 2159 ehci_queues_rip_all(ehci, async);
26d53979 2160 ehci_set_state(ehci, async, EST_INACTIVE);
94527ead
GH
2161 break;
2162 }
2163
4be23939 2164 /* make sure guest has acknowledged the doorbell interrupt */
94527ead
GH
2165 /* TO-DO: is this really needed? */
2166 if (ehci->usbsts & USBSTS_IAA) {
2167 DPRINTF("IAA status bit still set.\n");
2168 break;
2169 }
2170
94527ead
GH
2171 /* check that address register has been set */
2172 if (ehci->asynclistaddr == 0) {
2173 break;
2174 }
2175
26d53979 2176 ehci_set_state(ehci, async, EST_WAITLISTHEAD);
26d53979 2177 ehci_advance_state(ehci, async);
4be23939
HG
2178
2179 /* If the doorbell is set, the guest wants to make a change to the
2180 * schedule. The host controller needs to release cached data.
2181 * (section 4.8.2)
2182 */
2183 if (ehci->usbcmd & USBCMD_IAAD) {
2184 /* Remove all unseen qhs from the async qhs queue */
8f5457eb 2185 ehci_queues_rip_unseen(ehci, async);
1defcbd1 2186 trace_usb_ehci_doorbell_ack();
4be23939 2187 ehci->usbcmd &= ~USBCMD_IAAD;
7efc17af 2188 ehci_raise_irq(ehci, USBSTS_IAA);
4be23939 2189 }
94527ead
GH
2190 break;
2191
2192 default:
2193 /* this should only be due to a developer mistake */
2194 fprintf(stderr, "ehci: Bad asynchronous state %d. "
2195 "Resetting to active\n", ehci->astate);
0122f472 2196 assert(0);
94527ead
GH
2197 }
2198}
2199
2200static void ehci_advance_periodic_state(EHCIState *ehci)
2201{
2202 uint32_t entry;
2203 uint32_t list;
df5d5c5c 2204 const int async = 0;
94527ead
GH
2205
2206 // 4.6
2207
26d53979 2208 switch(ehci_get_state(ehci, async)) {
94527ead 2209 case EST_INACTIVE:
ec807d12 2210 if (!(ehci->frindex & 7) && ehci_periodic_enabled(ehci)) {
26d53979 2211 ehci_set_state(ehci, async, EST_ACTIVE);
94527ead
GH
2212 // No break, fall through to ACTIVE
2213 } else
2214 break;
2215
2216 case EST_ACTIVE:
ec807d12 2217 if (!(ehci->frindex & 7) && !ehci_periodic_enabled(ehci)) {
e850c2b4 2218 ehci_queues_rip_all(ehci, async);
26d53979 2219 ehci_set_state(ehci, async, EST_INACTIVE);
94527ead
GH
2220 break;
2221 }
2222
2223 list = ehci->periodiclistbase & 0xfffff000;
2224 /* check that register has been set */
2225 if (list == 0) {
2226 break;
2227 }
2228 list |= ((ehci->frindex & 0x1ff8) >> 1);
2229
55903f1d
GH
2230 if (get_dwords(ehci, list, &entry, 1) < 0) {
2231 break;
2232 }
94527ead
GH
2233
2234 DPRINTF("PERIODIC state adv fr=%d. [%08X] -> %08X\n",
2235 ehci->frindex / 8, list, entry);
0122f472 2236 ehci_set_fetch_addr(ehci, async,entry);
26d53979
GH
2237 ehci_set_state(ehci, async, EST_FETCHENTRY);
2238 ehci_advance_state(ehci, async);
8f5457eb 2239 ehci_queues_rip_unused(ehci, async);
94527ead
GH
2240 break;
2241
94527ead
GH
2242 default:
2243 /* this should only be due to a developer mistake */
2244 fprintf(stderr, "ehci: Bad periodic state %d. "
2245 "Resetting to active\n", ehci->pstate);
0122f472 2246 assert(0);
94527ead
GH
2247 }
2248}
2249
9359a58b 2250static void ehci_update_frindex(EHCIState *ehci, int uframes)
6ceced0b
GH
2251{
2252 int i;
2253
9359a58b 2254 if (!ehci_enabled(ehci) && ehci->pstate == EST_INACTIVE) {
6ceced0b
GH
2255 return;
2256 }
2257
9359a58b
HG
2258 for (i = 0; i < uframes; i++) {
2259 ehci->frindex++;
6ceced0b
GH
2260
2261 if (ehci->frindex == 0x00002000) {
7efc17af 2262 ehci_raise_irq(ehci, USBSTS_FLR);
6ceced0b
GH
2263 }
2264
2265 if (ehci->frindex == 0x00004000) {
7efc17af 2266 ehci_raise_irq(ehci, USBSTS_FLR);
6ceced0b 2267 ehci->frindex = 0;
ffa1f2e0 2268 if (ehci->usbsts_frindex >= 0x00004000) {
7efc17af
GH
2269 ehci->usbsts_frindex -= 0x00004000;
2270 } else {
2271 ehci->usbsts_frindex = 0;
2272 }
6ceced0b
GH
2273 }
2274 }
2275}
2276
94527ead
GH
2277static void ehci_frame_timer(void *opaque)
2278{
2279 EHCIState *ehci = opaque;
7efc17af 2280 int need_timer = 0;
94527ead 2281 int64_t expire_time, t_now;
adddecb1 2282 uint64_t ns_elapsed;
9359a58b 2283 int uframes, skipped_uframes;
94527ead 2284 int i;
94527ead 2285
94527ead 2286 t_now = qemu_get_clock_ns(vm_clock);
adddecb1 2287 ns_elapsed = t_now - ehci->last_run_ns;
9359a58b 2288 uframes = ns_elapsed / UFRAME_TIMER_NS;
94527ead 2289
3a215326 2290 if (ehci_periodic_enabled(ehci) || ehci->pstate != EST_INACTIVE) {
7efc17af 2291 need_timer++;
94527ead 2292
9359a58b
HG
2293 if (uframes > (ehci->maxframes * 8)) {
2294 skipped_uframes = uframes - (ehci->maxframes * 8);
2295 ehci_update_frindex(ehci, skipped_uframes);
2296 ehci->last_run_ns += UFRAME_TIMER_NS * skipped_uframes;
2297 uframes -= skipped_uframes;
2298 DPRINTF("WARNING - EHCI skipped %d uframes\n", skipped_uframes);
f020ed36
GH
2299 }
2300
9359a58b 2301 for (i = 0; i < uframes; i++) {
8f74ed1e
HG
2302 /*
2303 * If we're running behind schedule, we should not catch up
2304 * too fast, as that will make some guests unhappy:
9359a58b 2305 * 1) We must process a minimum of MIN_UFR_PER_TICK frames,
8f74ed1e
HG
2306 * otherwise we will never catch up
2307 * 2) Process frames until the guest has requested an irq (IOC)
2308 */
9359a58b 2309 if (i >= MIN_UFR_PER_TICK) {
8f74ed1e
HG
2310 ehci_commit_irq(ehci);
2311 if ((ehci->usbsts & USBINTR_MASK) & ehci->usbintr) {
2312 break;
2313 }
2314 }
80826240
HG
2315 if (ehci->periodic_sched_active) {
2316 ehci->periodic_sched_active--;
2317 }
3a215326 2318 ehci_update_frindex(ehci, 1);
9359a58b
HG
2319 if ((ehci->frindex & 7) == 0) {
2320 ehci_advance_periodic_state(ehci);
2321 }
2322 ehci->last_run_ns += UFRAME_TIMER_NS;
3a215326
GH
2323 }
2324 } else {
80826240 2325 ehci->periodic_sched_active = 0;
9359a58b
HG
2326 ehci_update_frindex(ehci, uframes);
2327 ehci->last_run_ns += UFRAME_TIMER_NS * uframes;
94527ead
GH
2328 }
2329
80826240
HG
2330 if (ehci->periodic_sched_active) {
2331 ehci->async_stepdown = 0;
2332 } else if (ehci->async_stepdown < ehci->maxframes / 2) {
2333 ehci->async_stepdown++;
2334 }
2335
94527ead
GH
2336 /* Async is not inside loop since it executes everything it can once
2337 * called
2338 */
3a215326 2339 if (ehci_async_enabled(ehci) || ehci->astate != EST_INACTIVE) {
7efc17af 2340 need_timer++;
afb7a0b8 2341 ehci_advance_async_state(ehci);
3a215326 2342 }
94527ead 2343
7efc17af
GH
2344 ehci_commit_irq(ehci);
2345 if (ehci->usbsts_pending) {
2346 need_timer++;
2347 ehci->async_stepdown = 0;
daf25307 2348 }
f0ad01f9 2349
40862309
GH
2350 if (ehci_enabled(ehci) && (ehci->usbintr & USBSTS_FLR)) {
2351 need_timer++;
2352 }
2353
7efc17af 2354 if (need_timer) {
44272b0f
HG
2355 /* If we've raised int, we speed up the timer, so that we quickly
2356 * notice any new packets queued up in response */
2357 if (ehci->int_req_by_async && (ehci->usbsts & USBSTS_INT)) {
52c15e59 2358 expire_time = t_now + get_ticks_per_sec() / (FRAME_TIMER_FREQ * 4);
44272b0f
HG
2359 ehci->int_req_by_async = false;
2360 } else {
2361 expire_time = t_now + (get_ticks_per_sec()
afb7a0b8 2362 * (ehci->async_stepdown+1) / FRAME_TIMER_FREQ);
44272b0f 2363 }
7efc17af
GH
2364 qemu_mod_timer(ehci->frame_timer, expire_time);
2365 }
94527ead
GH
2366}
2367
3e4f910c
GH
2368static const MemoryRegionOps ehci_mmio_caps_ops = {
2369 .read = ehci_caps_read,
2370 .valid.min_access_size = 1,
2371 .valid.max_access_size = 4,
2372 .impl.min_access_size = 1,
2373 .impl.max_access_size = 1,
2374 .endianness = DEVICE_LITTLE_ENDIAN,
2375};
2376
2377static const MemoryRegionOps ehci_mmio_opreg_ops = {
2378 .read = ehci_opreg_read,
2379 .write = ehci_opreg_write,
2380 .valid.min_access_size = 4,
2381 .valid.max_access_size = 4,
2382 .endianness = DEVICE_LITTLE_ENDIAN,
2383};
2384
2385static const MemoryRegionOps ehci_mmio_port_ops = {
2386 .read = ehci_port_read,
2387 .write = ehci_port_write,
2388 .valid.min_access_size = 4,
2389 .valid.max_access_size = 4,
e57964f5 2390 .endianness = DEVICE_LITTLE_ENDIAN,
94527ead
GH
2391};
2392
94527ead
GH
2393static USBPortOps ehci_port_ops = {
2394 .attach = ehci_attach,
2395 .detach = ehci_detach,
4706ab6c 2396 .child_detach = ehci_child_detach,
a0a3167a 2397 .wakeup = ehci_wakeup,
94527ead
GH
2398 .complete = ehci_async_complete_packet,
2399};
2400
07771f6f 2401static USBBusOps ehci_bus_ops = {
a0a3167a 2402 .register_companion = ehci_register_companion,
80826240 2403 .wakeup_endpoint = ehci_wakeup_endpoint,
07771f6f
GH
2404};
2405
9359a58b
HG
2406static void usb_ehci_pre_save(void *opaque)
2407{
2408 EHCIState *ehci = opaque;
2409 uint32_t new_frindex;
2410
2411 /* Round down frindex to a multiple of 8 for migration compatibility */
2412 new_frindex = ehci->frindex & ~7;
2413 ehci->last_run_ns -= (ehci->frindex - new_frindex) * UFRAME_TIMER_NS;
2414 ehci->frindex = new_frindex;
2415}
2416
9a773408
GH
2417static int usb_ehci_post_load(void *opaque, int version_id)
2418{
2419 EHCIState *s = opaque;
2420 int i;
2421
2422 for (i = 0; i < NB_PORTS; i++) {
2423 USBPort *companion = s->companion_ports[i];
2424 if (companion == NULL) {
2425 continue;
2426 }
2427 if (s->portsc[i] & PORTSC_POWNER) {
2428 companion->dev = s->ports[i].dev;
2429 } else {
2430 companion->dev = NULL;
2431 }
2432 }
2433
2434 return 0;
2435}
2436
ceab6f96
HG
2437static void usb_ehci_vm_state_change(void *opaque, int running, RunState state)
2438{
2439 EHCIState *ehci = opaque;
2440
2441 /*
2442 * We don't migrate the EHCIQueue-s, instead we rebuild them for the
2443 * schedule in guest memory. We must do the rebuilt ASAP, so that
2444 * USB-devices which have async handled packages have a packet in the
2445 * ep queue to match the completion with.
2446 */
2447 if (state == RUN_STATE_RUNNING) {
2448 ehci_advance_async_state(ehci);
2449 }
2450
2451 /*
2452 * The schedule rebuilt from guest memory could cause the migration dest
2453 * to miss a QH unlink, and fail to cancel packets, since the unlinked QH
2454 * will never have existed on the destination. Therefor we must flush the
2455 * async schedule on savevm to catch any not yet noticed unlinks.
2456 */
2457 if (state == RUN_STATE_SAVE_VM) {
2458 ehci_advance_async_state(ehci);
2459 ehci_queues_rip_unseen(ehci, 1);
2460 }
2461}
2462
0bf96f94 2463const VMStateDescription vmstate_ehci = {
5010d4dc 2464 .name = "ehci-core",
6d3b6d3d
GH
2465 .version_id = 2,
2466 .minimum_version_id = 1,
9359a58b 2467 .pre_save = usb_ehci_pre_save,
9a773408
GH
2468 .post_load = usb_ehci_post_load,
2469 .fields = (VMStateField[]) {
9a773408
GH
2470 /* mmio registers */
2471 VMSTATE_UINT32(usbcmd, EHCIState),
2472 VMSTATE_UINT32(usbsts, EHCIState),
6d3b6d3d
GH
2473 VMSTATE_UINT32_V(usbsts_pending, EHCIState, 2),
2474 VMSTATE_UINT32_V(usbsts_frindex, EHCIState, 2),
9a773408
GH
2475 VMSTATE_UINT32(usbintr, EHCIState),
2476 VMSTATE_UINT32(frindex, EHCIState),
2477 VMSTATE_UINT32(ctrldssegment, EHCIState),
2478 VMSTATE_UINT32(periodiclistbase, EHCIState),
2479 VMSTATE_UINT32(asynclistaddr, EHCIState),
2480 VMSTATE_UINT32(configflag, EHCIState),
2481 VMSTATE_UINT32(portsc[0], EHCIState),
2482 VMSTATE_UINT32(portsc[1], EHCIState),
2483 VMSTATE_UINT32(portsc[2], EHCIState),
2484 VMSTATE_UINT32(portsc[3], EHCIState),
2485 VMSTATE_UINT32(portsc[4], EHCIState),
2486 VMSTATE_UINT32(portsc[5], EHCIState),
2487 /* frame timer */
2488 VMSTATE_TIMER(frame_timer, EHCIState),
2489 VMSTATE_UINT64(last_run_ns, EHCIState),
2490 VMSTATE_UINT32(async_stepdown, EHCIState),
2491 /* schedule state */
2492 VMSTATE_UINT32(astate, EHCIState),
2493 VMSTATE_UINT32(pstate, EHCIState),
2494 VMSTATE_UINT32(a_fetch_addr, EHCIState),
2495 VMSTATE_UINT32(p_fetch_addr, EHCIState),
2496 VMSTATE_END_OF_LIST()
2497 }
9490fb06
GH
2498};
2499
0bf96f94 2500void usb_ehci_initfn(EHCIState *s, DeviceState *dev)
94527ead 2501{
94527ead
GH
2502 int i;
2503
3e4f910c 2504 /* 2.2 host controller interface version */
27a11324 2505 s->caps[0x00] = (uint8_t)(s->opregbase - s->capsbase);
3e4f910c
GH
2506 s->caps[0x01] = 0x00;
2507 s->caps[0x02] = 0x00;
2508 s->caps[0x03] = 0x01; /* HC version */
2509 s->caps[0x04] = NB_PORTS; /* Number of downstream ports */
2510 s->caps[0x05] = 0x00; /* No companion ports at present */
2511 s->caps[0x06] = 0x00;
2512 s->caps[0x07] = 0x00;
2513 s->caps[0x08] = 0x80; /* We can cache whole frame, no 64-bit */
3e4f910c
GH
2514 s->caps[0x0a] = 0x00;
2515 s->caps[0x0b] = 0x00;
94527ead 2516
5010d4dc 2517 usb_bus_new(&s->bus, &ehci_bus_ops, dev);
94527ead
GH
2518 for(i = 0; i < NB_PORTS; i++) {
2519 usb_register_port(&s->bus, &s->ports[i], s, i, &ehci_port_ops,
2520 USB_SPEED_MASK_HIGH);
94527ead
GH
2521 s->ports[i].dev = 0;
2522 }
2523
2524 s->frame_timer = qemu_new_timer_ns(vm_clock, ehci_frame_timer, s);
0262f65a 2525 s->async_bh = qemu_bh_new(ehci_frame_timer, s);
df5d5c5c
HG
2526 QTAILQ_INIT(&s->aqueues);
2527 QTAILQ_INIT(&s->pqueues);
7341ea07 2528 usb_packet_init(&s->ipacket);
94527ead
GH
2529
2530 qemu_register_reset(ehci_reset, s);
ceab6f96 2531 qemu_add_vm_change_state_handler(usb_ehci_vm_state_change, s);
94527ead 2532
3e4f910c
GH
2533 memory_region_init(&s->mem, "ehci", MMIO_SIZE);
2534 memory_region_init_io(&s->mem_caps, &ehci_mmio_caps_ops, s,
27a11324 2535 "capabilities", CAPA_SIZE);
3e4f910c 2536 memory_region_init_io(&s->mem_opreg, &ehci_mmio_opreg_ops, s,
27a11324 2537 "operational", PORTSC_BEGIN);
3e4f910c
GH
2538 memory_region_init_io(&s->mem_ports, &ehci_mmio_port_ops, s,
2539 "ports", PORTSC_END - PORTSC_BEGIN);
2540
27a11324
PC
2541 memory_region_add_subregion(&s->mem, s->capsbase, &s->mem_caps);
2542 memory_region_add_subregion(&s->mem, s->opregbase, &s->mem_opreg);
2543 memory_region_add_subregion(&s->mem, s->opregbase + PORTSC_BEGIN,
2544 &s->mem_ports);
5010d4dc
PC
2545}
2546
94527ead
GH
2547/*
2548 * vim: expandtab ts=4
2549 */