Statistics
| Branch: | Revision:

root / hw / usb / hcd-ehci.c @ f487b677

History | View | Annotate | Download (73.8 kB)

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