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