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