]> git.proxmox.com Git - mirror_qemu.git/blame - hw/usb/hcd-xhci.c
qemu-timer: introduce timer_deinit
[mirror_qemu.git] / hw / usb / hcd-xhci.c
CommitLineData
62c6ae04
HM
1/*
2 * USB xHCI controller emulation
3 *
4 * Copyright (c) 2011 Securiforest
5 * Date: 2011-05-11 ; Author: Hector Martin <hector@marcansoft.com>
6 * Based on usb-ohci.c, emulates Renesas NEC USB 3.0
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 */
f1ae32a1 21#include "hw/hw.h"
1de7afc9 22#include "qemu/timer.h"
f1ae32a1 23#include "hw/usb.h"
a2cb15b0
MT
24#include "hw/pci/pci.h"
25#include "hw/pci/msi.h"
26#include "hw/pci/msix.h"
2d754a10 27#include "trace.h"
62c6ae04
HM
28
29//#define DEBUG_XHCI
30//#define DEBUG_DATA
31
32#ifdef DEBUG_XHCI
33#define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
34#else
35#define DPRINTF(...) do {} while (0)
36#endif
024426ac
GH
37#define FIXME(_msg) do { fprintf(stderr, "FIXME %s:%d %s\n", \
38 __func__, __LINE__, _msg); abort(); } while (0)
62c6ae04 39
d95e74ea
GH
40#define MAXPORTS_2 15
41#define MAXPORTS_3 15
62c6ae04 42
0846e635 43#define MAXPORTS (MAXPORTS_2+MAXPORTS_3)
d95e74ea
GH
44#define MAXSLOTS 64
45#define MAXINTRS 16
62c6ae04
HM
46
47#define TD_QUEUE 24
62c6ae04
HM
48
49/* Very pessimistic, let's hope it's enough for all cases */
50#define EV_QUEUE (((3*TD_QUEUE)+16)*MAXSLOTS)
51/* Do not deliver ER Full events. NEC's driver does some things not bound
52 * to the specs when it gets them */
53#define ER_FULL_HACK
54
55#define LEN_CAP 0x40
62c6ae04 56#define LEN_OPER (0x400 + 0x10 * MAXPORTS)
106b214c 57#define LEN_RUNTIME ((MAXINTRS + 1) * 0x20)
62c6ae04
HM
58#define LEN_DOORBELL ((MAXSLOTS + 1) * 0x20)
59
106b214c
GH
60#define OFF_OPER LEN_CAP
61#define OFF_RUNTIME 0x1000
62#define OFF_DOORBELL 0x2000
4c47f800
GH
63#define OFF_MSIX_TABLE 0x3000
64#define OFF_MSIX_PBA 0x3800
62c6ae04 65/* must be power of 2 */
106b214c 66#define LEN_REGS 0x4000
62c6ae04 67
106b214c
GH
68#if (OFF_OPER + LEN_OPER) > OFF_RUNTIME
69#error Increase OFF_RUNTIME
70#endif
71#if (OFF_RUNTIME + LEN_RUNTIME) > OFF_DOORBELL
72#error Increase OFF_DOORBELL
73#endif
62c6ae04
HM
74#if (OFF_DOORBELL + LEN_DOORBELL) > LEN_REGS
75# error Increase LEN_REGS
76#endif
77
62c6ae04
HM
78/* bit definitions */
79#define USBCMD_RS (1<<0)
80#define USBCMD_HCRST (1<<1)
81#define USBCMD_INTE (1<<2)
82#define USBCMD_HSEE (1<<3)
83#define USBCMD_LHCRST (1<<7)
84#define USBCMD_CSS (1<<8)
85#define USBCMD_CRS (1<<9)
86#define USBCMD_EWE (1<<10)
87#define USBCMD_EU3S (1<<11)
88
89#define USBSTS_HCH (1<<0)
90#define USBSTS_HSE (1<<2)
91#define USBSTS_EINT (1<<3)
92#define USBSTS_PCD (1<<4)
93#define USBSTS_SSS (1<<8)
94#define USBSTS_RSS (1<<9)
95#define USBSTS_SRE (1<<10)
96#define USBSTS_CNR (1<<11)
97#define USBSTS_HCE (1<<12)
98
99
100#define PORTSC_CCS (1<<0)
101#define PORTSC_PED (1<<1)
102#define PORTSC_OCA (1<<3)
103#define PORTSC_PR (1<<4)
104#define PORTSC_PLS_SHIFT 5
105#define PORTSC_PLS_MASK 0xf
106#define PORTSC_PP (1<<9)
107#define PORTSC_SPEED_SHIFT 10
108#define PORTSC_SPEED_MASK 0xf
109#define PORTSC_SPEED_FULL (1<<10)
110#define PORTSC_SPEED_LOW (2<<10)
111#define PORTSC_SPEED_HIGH (3<<10)
112#define PORTSC_SPEED_SUPER (4<<10)
113#define PORTSC_PIC_SHIFT 14
114#define PORTSC_PIC_MASK 0x3
115#define PORTSC_LWS (1<<16)
116#define PORTSC_CSC (1<<17)
117#define PORTSC_PEC (1<<18)
118#define PORTSC_WRC (1<<19)
119#define PORTSC_OCC (1<<20)
120#define PORTSC_PRC (1<<21)
121#define PORTSC_PLC (1<<22)
122#define PORTSC_CEC (1<<23)
123#define PORTSC_CAS (1<<24)
124#define PORTSC_WCE (1<<25)
125#define PORTSC_WDE (1<<26)
126#define PORTSC_WOE (1<<27)
127#define PORTSC_DR (1<<30)
128#define PORTSC_WPR (1<<31)
129
130#define CRCR_RCS (1<<0)
131#define CRCR_CS (1<<1)
132#define CRCR_CA (1<<2)
133#define CRCR_CRR (1<<3)
134
135#define IMAN_IP (1<<0)
136#define IMAN_IE (1<<1)
137
138#define ERDP_EHB (1<<3)
139
140#define TRB_SIZE 16
141typedef struct XHCITRB {
142 uint64_t parameter;
143 uint32_t status;
144 uint32_t control;
59a70ccd 145 dma_addr_t addr;
62c6ae04
HM
146 bool ccs;
147} XHCITRB;
148
85e05d82
GH
149enum {
150 PLS_U0 = 0,
151 PLS_U1 = 1,
152 PLS_U2 = 2,
153 PLS_U3 = 3,
154 PLS_DISABLED = 4,
155 PLS_RX_DETECT = 5,
156 PLS_INACTIVE = 6,
157 PLS_POLLING = 7,
158 PLS_RECOVERY = 8,
159 PLS_HOT_RESET = 9,
160 PLS_COMPILANCE_MODE = 10,
161 PLS_TEST_MODE = 11,
162 PLS_RESUME = 15,
163};
62c6ae04
HM
164
165typedef enum TRBType {
166 TRB_RESERVED = 0,
167 TR_NORMAL,
168 TR_SETUP,
169 TR_DATA,
170 TR_STATUS,
171 TR_ISOCH,
172 TR_LINK,
173 TR_EVDATA,
174 TR_NOOP,
175 CR_ENABLE_SLOT,
176 CR_DISABLE_SLOT,
177 CR_ADDRESS_DEVICE,
178 CR_CONFIGURE_ENDPOINT,
179 CR_EVALUATE_CONTEXT,
180 CR_RESET_ENDPOINT,
181 CR_STOP_ENDPOINT,
182 CR_SET_TR_DEQUEUE,
183 CR_RESET_DEVICE,
184 CR_FORCE_EVENT,
185 CR_NEGOTIATE_BW,
186 CR_SET_LATENCY_TOLERANCE,
187 CR_GET_PORT_BANDWIDTH,
188 CR_FORCE_HEADER,
189 CR_NOOP,
190 ER_TRANSFER = 32,
191 ER_COMMAND_COMPLETE,
192 ER_PORT_STATUS_CHANGE,
193 ER_BANDWIDTH_REQUEST,
194 ER_DOORBELL,
195 ER_HOST_CONTROLLER,
196 ER_DEVICE_NOTIFICATION,
197 ER_MFINDEX_WRAP,
198 /* vendor specific bits */
199 CR_VENDOR_VIA_CHALLENGE_RESPONSE = 48,
200 CR_VENDOR_NEC_FIRMWARE_REVISION = 49,
201 CR_VENDOR_NEC_CHALLENGE_RESPONSE = 50,
202} TRBType;
203
204#define CR_LINK TR_LINK
205
206typedef enum TRBCCode {
207 CC_INVALID = 0,
208 CC_SUCCESS,
209 CC_DATA_BUFFER_ERROR,
210 CC_BABBLE_DETECTED,
211 CC_USB_TRANSACTION_ERROR,
212 CC_TRB_ERROR,
213 CC_STALL_ERROR,
214 CC_RESOURCE_ERROR,
215 CC_BANDWIDTH_ERROR,
216 CC_NO_SLOTS_ERROR,
217 CC_INVALID_STREAM_TYPE_ERROR,
218 CC_SLOT_NOT_ENABLED_ERROR,
219 CC_EP_NOT_ENABLED_ERROR,
220 CC_SHORT_PACKET,
221 CC_RING_UNDERRUN,
222 CC_RING_OVERRUN,
223 CC_VF_ER_FULL,
224 CC_PARAMETER_ERROR,
225 CC_BANDWIDTH_OVERRUN,
226 CC_CONTEXT_STATE_ERROR,
227 CC_NO_PING_RESPONSE_ERROR,
228 CC_EVENT_RING_FULL_ERROR,
229 CC_INCOMPATIBLE_DEVICE_ERROR,
230 CC_MISSED_SERVICE_ERROR,
231 CC_COMMAND_RING_STOPPED,
232 CC_COMMAND_ABORTED,
233 CC_STOPPED,
234 CC_STOPPED_LENGTH_INVALID,
235 CC_MAX_EXIT_LATENCY_TOO_LARGE_ERROR = 29,
236 CC_ISOCH_BUFFER_OVERRUN = 31,
237 CC_EVENT_LOST_ERROR,
238 CC_UNDEFINED_ERROR,
239 CC_INVALID_STREAM_ID_ERROR,
240 CC_SECONDARY_BANDWIDTH_ERROR,
241 CC_SPLIT_TRANSACTION_ERROR
242} TRBCCode;
243
244#define TRB_C (1<<0)
245#define TRB_TYPE_SHIFT 10
246#define TRB_TYPE_MASK 0x3f
247#define TRB_TYPE(t) (((t).control >> TRB_TYPE_SHIFT) & TRB_TYPE_MASK)
248
249#define TRB_EV_ED (1<<2)
250
251#define TRB_TR_ENT (1<<1)
252#define TRB_TR_ISP (1<<2)
253#define TRB_TR_NS (1<<3)
254#define TRB_TR_CH (1<<4)
255#define TRB_TR_IOC (1<<5)
256#define TRB_TR_IDT (1<<6)
257#define TRB_TR_TBC_SHIFT 7
258#define TRB_TR_TBC_MASK 0x3
259#define TRB_TR_BEI (1<<9)
260#define TRB_TR_TLBPC_SHIFT 16
261#define TRB_TR_TLBPC_MASK 0xf
262#define TRB_TR_FRAMEID_SHIFT 20
263#define TRB_TR_FRAMEID_MASK 0x7ff
264#define TRB_TR_SIA (1<<31)
265
266#define TRB_TR_DIR (1<<16)
267
268#define TRB_CR_SLOTID_SHIFT 24
269#define TRB_CR_SLOTID_MASK 0xff
270#define TRB_CR_EPID_SHIFT 16
271#define TRB_CR_EPID_MASK 0x1f
272
273#define TRB_CR_BSR (1<<9)
274#define TRB_CR_DC (1<<9)
275
276#define TRB_LK_TC (1<<1)
277
2d1de850
GH
278#define TRB_INTR_SHIFT 22
279#define TRB_INTR_MASK 0x3ff
280#define TRB_INTR(t) (((t).status >> TRB_INTR_SHIFT) & TRB_INTR_MASK)
281
62c6ae04
HM
282#define EP_TYPE_MASK 0x7
283#define EP_TYPE_SHIFT 3
284
285#define EP_STATE_MASK 0x7
286#define EP_DISABLED (0<<0)
287#define EP_RUNNING (1<<0)
288#define EP_HALTED (2<<0)
289#define EP_STOPPED (3<<0)
290#define EP_ERROR (4<<0)
291
292#define SLOT_STATE_MASK 0x1f
293#define SLOT_STATE_SHIFT 27
294#define SLOT_STATE(s) (((s)>>SLOT_STATE_SHIFT)&SLOT_STATE_MASK)
295#define SLOT_ENABLED 0
296#define SLOT_DEFAULT 1
297#define SLOT_ADDRESSED 2
298#define SLOT_CONFIGURED 3
299
300#define SLOT_CONTEXT_ENTRIES_MASK 0x1f
301#define SLOT_CONTEXT_ENTRIES_SHIFT 27
302
1d8a4e69 303typedef struct XHCIState XHCIState;
024426ac
GH
304typedef struct XHCIStreamContext XHCIStreamContext;
305typedef struct XHCIEPContext XHCIEPContext;
1d8a4e69 306
85e05d82
GH
307#define get_field(data, field) \
308 (((data) >> field##_SHIFT) & field##_MASK)
309
310#define set_field(data, newval, field) do { \
311 uint32_t val = *data; \
312 val &= ~(field##_MASK << field##_SHIFT); \
313 val |= ((newval) & field##_MASK) << field##_SHIFT; \
314 *data = val; \
315 } while (0)
316
62c6ae04
HM
317typedef enum EPType {
318 ET_INVALID = 0,
319 ET_ISO_OUT,
320 ET_BULK_OUT,
321 ET_INTR_OUT,
322 ET_CONTROL,
323 ET_ISO_IN,
324 ET_BULK_IN,
325 ET_INTR_IN,
326} EPType;
327
328typedef struct XHCIRing {
59a70ccd 329 dma_addr_t dequeue;
62c6ae04
HM
330 bool ccs;
331} XHCIRing;
332
333typedef struct XHCIPort {
1d8a4e69 334 XHCIState *xhci;
62c6ae04 335 uint32_t portsc;
0846e635
GH
336 uint32_t portnr;
337 USBPort *uport;
338 uint32_t speedmask;
1d8a4e69
GH
339 char name[16];
340 MemoryRegion mem;
62c6ae04
HM
341} XHCIPort;
342
62c6ae04
HM
343typedef struct XHCITransfer {
344 XHCIState *xhci;
345 USBPacket packet;
d5a15814 346 QEMUSGList sgl;
7c605a23
GH
347 bool running_async;
348 bool running_retry;
62c6ae04 349 bool complete;
a6fb2ddb 350 bool int_req;
62c6ae04
HM
351 unsigned int iso_pkts;
352 unsigned int slotid;
353 unsigned int epid;
024426ac 354 unsigned int streamid;
62c6ae04
HM
355 bool in_xfer;
356 bool iso_xfer;
4d7a81c0 357 bool timed_xfer;
62c6ae04
HM
358
359 unsigned int trb_count;
360 unsigned int trb_alloced;
361 XHCITRB *trbs;
362
62c6ae04
HM
363 TRBCCode status;
364
365 unsigned int pkts;
366 unsigned int pktsize;
367 unsigned int cur_pkt;
3d139684
GH
368
369 uint64_t mfindex_kick;
62c6ae04
HM
370} XHCITransfer;
371
024426ac
GH
372struct XHCIStreamContext {
373 dma_addr_t pctx;
374 unsigned int sct;
375 XHCIRing ring;
024426ac
GH
376};
377
378struct XHCIEPContext {
3d139684
GH
379 XHCIState *xhci;
380 unsigned int slotid;
381 unsigned int epid;
382
62c6ae04
HM
383 XHCIRing ring;
384 unsigned int next_xfer;
385 unsigned int comp_xfer;
386 XHCITransfer transfers[TD_QUEUE];
7c605a23 387 XHCITransfer *retry;
62c6ae04 388 EPType type;
59a70ccd 389 dma_addr_t pctx;
62c6ae04 390 unsigned int max_psize;
62c6ae04 391 uint32_t state;
3d139684 392
024426ac
GH
393 /* streams */
394 unsigned int max_pstreams;
395 bool lsa;
396 unsigned int nr_pstreams;
397 XHCIStreamContext *pstreams;
398
3d139684
GH
399 /* iso xfer scheduling */
400 unsigned int interval;
401 int64_t mfindex_last;
402 QEMUTimer *kick_timer;
024426ac 403};
62c6ae04
HM
404
405typedef struct XHCISlot {
406 bool enabled;
4034e693 407 bool addressed;
59a70ccd 408 dma_addr_t ctx;
ccaf87a0 409 USBPort *uport;
62c6ae04
HM
410 XHCIEPContext * eps[31];
411} XHCISlot;
412
413typedef struct XHCIEvent {
414 TRBType type;
415 TRBCCode ccode;
416 uint64_t ptr;
417 uint32_t length;
418 uint32_t flags;
419 uint8_t slotid;
420 uint8_t epid;
421} XHCIEvent;
422
962d11e1
GH
423typedef struct XHCIInterrupter {
424 uint32_t iman;
425 uint32_t imod;
426 uint32_t erstsz;
427 uint32_t erstba_low;
428 uint32_t erstba_high;
429 uint32_t erdp_low;
430 uint32_t erdp_high;
431
432 bool msix_used, er_pcs, er_full;
433
434 dma_addr_t er_start;
435 uint32_t er_size;
436 unsigned int er_ep_idx;
437
438 XHCIEvent ev_buffer[EV_QUEUE];
439 unsigned int ev_buffer_put;
440 unsigned int ev_buffer_get;
441
442} XHCIInterrupter;
443
62c6ae04 444struct XHCIState {
9b7d3334
AF
445 /*< private >*/
446 PCIDevice parent_obj;
447 /*< public >*/
448
62c6ae04 449 USBBus bus;
62c6ae04 450 MemoryRegion mem;
1b067564
GH
451 MemoryRegion mem_cap;
452 MemoryRegion mem_oper;
453 MemoryRegion mem_runtime;
454 MemoryRegion mem_doorbell;
62c6ae04 455
0846e635
GH
456 /* properties */
457 uint32_t numports_2;
458 uint32_t numports_3;
91062ae0
GH
459 uint32_t numintrs;
460 uint32_t numslots;
c5e9b02d 461 uint32_t flags;
2aa6bfcb 462 uint32_t max_pstreams_mask;
0846e635 463
62c6ae04
HM
464 /* Operational Registers */
465 uint32_t usbcmd;
466 uint32_t usbsts;
467 uint32_t dnctrl;
468 uint32_t crcr_low;
469 uint32_t crcr_high;
470 uint32_t dcbaap_low;
471 uint32_t dcbaap_high;
472 uint32_t config;
473
0846e635 474 USBPort uports[MAX(MAXPORTS_2, MAXPORTS_3)];
62c6ae04
HM
475 XHCIPort ports[MAXPORTS];
476 XHCISlot slots[MAXSLOTS];
0846e635 477 uint32_t numports;
62c6ae04
HM
478
479 /* Runtime Registers */
01546fa6
GH
480 int64_t mfindex_start;
481 QEMUTimer *mfwrap_timer;
962d11e1 482 XHCIInterrupter intr[MAXINTRS];
62c6ae04
HM
483
484 XHCIRing cmd_ring;
485};
486
37034575
PC
487#define TYPE_XHCI "nec-usb-xhci"
488
489#define XHCI(obj) \
490 OBJECT_CHECK(XHCIState, (obj), TYPE_XHCI)
491
62c6ae04
HM
492typedef struct XHCIEvRingSeg {
493 uint32_t addr_low;
494 uint32_t addr_high;
495 uint32_t size;
496 uint32_t rsvd;
497} XHCIEvRingSeg;
498
c5e9b02d
GH
499enum xhci_flags {
500 XHCI_FLAG_USE_MSI = 1,
4c47f800 501 XHCI_FLAG_USE_MSI_X,
7bafd888 502 XHCI_FLAG_SS_FIRST,
e6043e92 503 XHCI_FLAG_FORCE_PCIE_ENDCAP,
2aa6bfcb 504 XHCI_FLAG_ENABLE_STREAMS,
c5e9b02d
GH
505};
506
01546fa6 507static void xhci_kick_ep(XHCIState *xhci, unsigned int slotid,
024426ac 508 unsigned int epid, unsigned int streamid);
0bc85da6
GH
509static TRBCCode xhci_disable_ep(XHCIState *xhci, unsigned int slotid,
510 unsigned int epid);
582d6f4a 511static void xhci_xfer_report(XHCITransfer *xfer);
962d11e1
GH
512static void xhci_event(XHCIState *xhci, XHCIEvent *event, int v);
513static void xhci_write_event(XHCIState *xhci, XHCIEvent *event, int v);
518ad5f2
HG
514static USBEndpoint *xhci_epid_to_usbep(XHCIState *xhci,
515 unsigned int slotid, unsigned int epid);
01546fa6 516
f10de44e
GH
517static const char *TRBType_names[] = {
518 [TRB_RESERVED] = "TRB_RESERVED",
519 [TR_NORMAL] = "TR_NORMAL",
520 [TR_SETUP] = "TR_SETUP",
521 [TR_DATA] = "TR_DATA",
522 [TR_STATUS] = "TR_STATUS",
523 [TR_ISOCH] = "TR_ISOCH",
524 [TR_LINK] = "TR_LINK",
525 [TR_EVDATA] = "TR_EVDATA",
526 [TR_NOOP] = "TR_NOOP",
527 [CR_ENABLE_SLOT] = "CR_ENABLE_SLOT",
528 [CR_DISABLE_SLOT] = "CR_DISABLE_SLOT",
529 [CR_ADDRESS_DEVICE] = "CR_ADDRESS_DEVICE",
530 [CR_CONFIGURE_ENDPOINT] = "CR_CONFIGURE_ENDPOINT",
531 [CR_EVALUATE_CONTEXT] = "CR_EVALUATE_CONTEXT",
532 [CR_RESET_ENDPOINT] = "CR_RESET_ENDPOINT",
533 [CR_STOP_ENDPOINT] = "CR_STOP_ENDPOINT",
534 [CR_SET_TR_DEQUEUE] = "CR_SET_TR_DEQUEUE",
535 [CR_RESET_DEVICE] = "CR_RESET_DEVICE",
536 [CR_FORCE_EVENT] = "CR_FORCE_EVENT",
537 [CR_NEGOTIATE_BW] = "CR_NEGOTIATE_BW",
538 [CR_SET_LATENCY_TOLERANCE] = "CR_SET_LATENCY_TOLERANCE",
539 [CR_GET_PORT_BANDWIDTH] = "CR_GET_PORT_BANDWIDTH",
540 [CR_FORCE_HEADER] = "CR_FORCE_HEADER",
541 [CR_NOOP] = "CR_NOOP",
542 [ER_TRANSFER] = "ER_TRANSFER",
543 [ER_COMMAND_COMPLETE] = "ER_COMMAND_COMPLETE",
544 [ER_PORT_STATUS_CHANGE] = "ER_PORT_STATUS_CHANGE",
545 [ER_BANDWIDTH_REQUEST] = "ER_BANDWIDTH_REQUEST",
546 [ER_DOORBELL] = "ER_DOORBELL",
547 [ER_HOST_CONTROLLER] = "ER_HOST_CONTROLLER",
548 [ER_DEVICE_NOTIFICATION] = "ER_DEVICE_NOTIFICATION",
549 [ER_MFINDEX_WRAP] = "ER_MFINDEX_WRAP",
550 [CR_VENDOR_VIA_CHALLENGE_RESPONSE] = "CR_VENDOR_VIA_CHALLENGE_RESPONSE",
551 [CR_VENDOR_NEC_FIRMWARE_REVISION] = "CR_VENDOR_NEC_FIRMWARE_REVISION",
552 [CR_VENDOR_NEC_CHALLENGE_RESPONSE] = "CR_VENDOR_NEC_CHALLENGE_RESPONSE",
553};
554
873123fe
GH
555static const char *TRBCCode_names[] = {
556 [CC_INVALID] = "CC_INVALID",
557 [CC_SUCCESS] = "CC_SUCCESS",
558 [CC_DATA_BUFFER_ERROR] = "CC_DATA_BUFFER_ERROR",
559 [CC_BABBLE_DETECTED] = "CC_BABBLE_DETECTED",
560 [CC_USB_TRANSACTION_ERROR] = "CC_USB_TRANSACTION_ERROR",
561 [CC_TRB_ERROR] = "CC_TRB_ERROR",
562 [CC_STALL_ERROR] = "CC_STALL_ERROR",
563 [CC_RESOURCE_ERROR] = "CC_RESOURCE_ERROR",
564 [CC_BANDWIDTH_ERROR] = "CC_BANDWIDTH_ERROR",
565 [CC_NO_SLOTS_ERROR] = "CC_NO_SLOTS_ERROR",
566 [CC_INVALID_STREAM_TYPE_ERROR] = "CC_INVALID_STREAM_TYPE_ERROR",
567 [CC_SLOT_NOT_ENABLED_ERROR] = "CC_SLOT_NOT_ENABLED_ERROR",
568 [CC_EP_NOT_ENABLED_ERROR] = "CC_EP_NOT_ENABLED_ERROR",
569 [CC_SHORT_PACKET] = "CC_SHORT_PACKET",
570 [CC_RING_UNDERRUN] = "CC_RING_UNDERRUN",
571 [CC_RING_OVERRUN] = "CC_RING_OVERRUN",
572 [CC_VF_ER_FULL] = "CC_VF_ER_FULL",
573 [CC_PARAMETER_ERROR] = "CC_PARAMETER_ERROR",
574 [CC_BANDWIDTH_OVERRUN] = "CC_BANDWIDTH_OVERRUN",
575 [CC_CONTEXT_STATE_ERROR] = "CC_CONTEXT_STATE_ERROR",
576 [CC_NO_PING_RESPONSE_ERROR] = "CC_NO_PING_RESPONSE_ERROR",
577 [CC_EVENT_RING_FULL_ERROR] = "CC_EVENT_RING_FULL_ERROR",
578 [CC_INCOMPATIBLE_DEVICE_ERROR] = "CC_INCOMPATIBLE_DEVICE_ERROR",
579 [CC_MISSED_SERVICE_ERROR] = "CC_MISSED_SERVICE_ERROR",
580 [CC_COMMAND_RING_STOPPED] = "CC_COMMAND_RING_STOPPED",
581 [CC_COMMAND_ABORTED] = "CC_COMMAND_ABORTED",
582 [CC_STOPPED] = "CC_STOPPED",
583 [CC_STOPPED_LENGTH_INVALID] = "CC_STOPPED_LENGTH_INVALID",
584 [CC_MAX_EXIT_LATENCY_TOO_LARGE_ERROR]
585 = "CC_MAX_EXIT_LATENCY_TOO_LARGE_ERROR",
586 [CC_ISOCH_BUFFER_OVERRUN] = "CC_ISOCH_BUFFER_OVERRUN",
587 [CC_EVENT_LOST_ERROR] = "CC_EVENT_LOST_ERROR",
588 [CC_UNDEFINED_ERROR] = "CC_UNDEFINED_ERROR",
589 [CC_INVALID_STREAM_ID_ERROR] = "CC_INVALID_STREAM_ID_ERROR",
590 [CC_SECONDARY_BANDWIDTH_ERROR] = "CC_SECONDARY_BANDWIDTH_ERROR",
591 [CC_SPLIT_TRANSACTION_ERROR] = "CC_SPLIT_TRANSACTION_ERROR",
592};
593
1c82392a
GH
594static const char *ep_state_names[] = {
595 [EP_DISABLED] = "disabled",
596 [EP_RUNNING] = "running",
597 [EP_HALTED] = "halted",
598 [EP_STOPPED] = "stopped",
599 [EP_ERROR] = "error",
600};
601
f10de44e
GH
602static const char *lookup_name(uint32_t index, const char **list, uint32_t llen)
603{
604 if (index >= llen || list[index] == NULL) {
605 return "???";
606 }
607 return list[index];
608}
609
610static const char *trb_name(XHCITRB *trb)
611{
612 return lookup_name(TRB_TYPE(*trb), TRBType_names,
613 ARRAY_SIZE(TRBType_names));
614}
f10de44e 615
873123fe
GH
616static const char *event_name(XHCIEvent *event)
617{
618 return lookup_name(event->ccode, TRBCCode_names,
619 ARRAY_SIZE(TRBCCode_names));
620}
621
1c82392a
GH
622static const char *ep_state_name(uint32_t state)
623{
624 return lookup_name(state, ep_state_names,
625 ARRAY_SIZE(ep_state_names));
626}
627
f9955235
GH
628static bool xhci_get_flag(XHCIState *xhci, enum xhci_flags bit)
629{
630 return xhci->flags & (1 << bit);
631}
632
01546fa6
GH
633static uint64_t xhci_mfindex_get(XHCIState *xhci)
634{
bc72ad67 635 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
01546fa6
GH
636 return (now - xhci->mfindex_start) / 125000;
637}
638
639static void xhci_mfwrap_update(XHCIState *xhci)
640{
641 const uint32_t bits = USBCMD_RS | USBCMD_EWE;
642 uint32_t mfindex, left;
643 int64_t now;
644
645 if ((xhci->usbcmd & bits) == bits) {
bc72ad67 646 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
01546fa6
GH
647 mfindex = ((now - xhci->mfindex_start) / 125000) & 0x3fff;
648 left = 0x4000 - mfindex;
bc72ad67 649 timer_mod(xhci->mfwrap_timer, now + left * 125000);
01546fa6 650 } else {
bc72ad67 651 timer_del(xhci->mfwrap_timer);
01546fa6
GH
652 }
653}
654
655static void xhci_mfwrap_timer(void *opaque)
656{
657 XHCIState *xhci = opaque;
658 XHCIEvent wrap = { ER_MFINDEX_WRAP, CC_SUCCESS };
659
962d11e1 660 xhci_event(xhci, &wrap, 0);
01546fa6
GH
661 xhci_mfwrap_update(xhci);
662}
62c6ae04 663
59a70ccd 664static inline dma_addr_t xhci_addr64(uint32_t low, uint32_t high)
62c6ae04 665{
59a70ccd
DG
666 if (sizeof(dma_addr_t) == 4) {
667 return low;
668 } else {
669 return low | (((dma_addr_t)high << 16) << 16);
670 }
62c6ae04
HM
671}
672
59a70ccd 673static inline dma_addr_t xhci_mask64(uint64_t addr)
62c6ae04 674{
59a70ccd
DG
675 if (sizeof(dma_addr_t) == 4) {
676 return addr & 0xffffffff;
677 } else {
678 return addr;
679 }
62c6ae04
HM
680}
681
616b5d53
DG
682static inline void xhci_dma_read_u32s(XHCIState *xhci, dma_addr_t addr,
683 uint32_t *buf, size_t len)
684{
685 int i;
686
687 assert((len % sizeof(uint32_t)) == 0);
688
9b7d3334 689 pci_dma_read(PCI_DEVICE(xhci), addr, buf, len);
616b5d53
DG
690
691 for (i = 0; i < (len / sizeof(uint32_t)); i++) {
692 buf[i] = le32_to_cpu(buf[i]);
693 }
694}
695
696static inline void xhci_dma_write_u32s(XHCIState *xhci, dma_addr_t addr,
697 uint32_t *buf, size_t len)
698{
699 int i;
700 uint32_t tmp[len / sizeof(uint32_t)];
701
702 assert((len % sizeof(uint32_t)) == 0);
703
704 for (i = 0; i < (len / sizeof(uint32_t)); i++) {
705 tmp[i] = cpu_to_le32(buf[i]);
706 }
9b7d3334 707 pci_dma_write(PCI_DEVICE(xhci), addr, tmp, len);
616b5d53
DG
708}
709
0846e635
GH
710static XHCIPort *xhci_lookup_port(XHCIState *xhci, struct USBPort *uport)
711{
712 int index;
713
714 if (!uport->dev) {
715 return NULL;
716 }
717 switch (uport->dev->speed) {
718 case USB_SPEED_LOW:
719 case USB_SPEED_FULL:
720 case USB_SPEED_HIGH:
7bafd888
GH
721 if (xhci_get_flag(xhci, XHCI_FLAG_SS_FIRST)) {
722 index = uport->index + xhci->numports_3;
723 } else {
724 index = uport->index;
725 }
0846e635
GH
726 break;
727 case USB_SPEED_SUPER:
7bafd888
GH
728 if (xhci_get_flag(xhci, XHCI_FLAG_SS_FIRST)) {
729 index = uport->index;
730 } else {
731 index = uport->index + xhci->numports_2;
732 }
0846e635
GH
733 break;
734 default:
735 return NULL;
736 }
737 return &xhci->ports[index];
738}
739
4c4abe7c 740static void xhci_intx_update(XHCIState *xhci)
62c6ae04 741{
9b7d3334 742 PCIDevice *pci_dev = PCI_DEVICE(xhci);
62c6ae04
HM
743 int level = 0;
744
9b7d3334
AF
745 if (msix_enabled(pci_dev) ||
746 msi_enabled(pci_dev)) {
4c4abe7c
GH
747 return;
748 }
749
962d11e1
GH
750 if (xhci->intr[0].iman & IMAN_IP &&
751 xhci->intr[0].iman & IMAN_IE &&
215bff17 752 xhci->usbcmd & USBCMD_INTE) {
62c6ae04
HM
753 level = 1;
754 }
755
4c4abe7c 756 trace_usb_xhci_irq_intx(level);
9e64f8a3 757 pci_set_irq(pci_dev, level);
4c4abe7c
GH
758}
759
962d11e1 760static void xhci_msix_update(XHCIState *xhci, int v)
4c47f800 761{
9b7d3334 762 PCIDevice *pci_dev = PCI_DEVICE(xhci);
4c47f800
GH
763 bool enabled;
764
9b7d3334 765 if (!msix_enabled(pci_dev)) {
4c47f800
GH
766 return;
767 }
768
962d11e1
GH
769 enabled = xhci->intr[v].iman & IMAN_IE;
770 if (enabled == xhci->intr[v].msix_used) {
4c47f800
GH
771 return;
772 }
773
774 if (enabled) {
962d11e1 775 trace_usb_xhci_irq_msix_use(v);
9b7d3334 776 msix_vector_use(pci_dev, v);
962d11e1 777 xhci->intr[v].msix_used = true;
4c47f800 778 } else {
962d11e1 779 trace_usb_xhci_irq_msix_unuse(v);
9b7d3334 780 msix_vector_unuse(pci_dev, v);
962d11e1 781 xhci->intr[v].msix_used = false;
4c47f800
GH
782 }
783}
784
962d11e1 785static void xhci_intr_raise(XHCIState *xhci, int v)
4c4abe7c 786{
9b7d3334
AF
787 PCIDevice *pci_dev = PCI_DEVICE(xhci);
788
962d11e1
GH
789 xhci->intr[v].erdp_low |= ERDP_EHB;
790 xhci->intr[v].iman |= IMAN_IP;
2cae4119
GH
791 xhci->usbsts |= USBSTS_EINT;
792
962d11e1 793 if (!(xhci->intr[v].iman & IMAN_IE)) {
4c4abe7c
GH
794 return;
795 }
796
797 if (!(xhci->usbcmd & USBCMD_INTE)) {
798 return;
799 }
800
9b7d3334 801 if (msix_enabled(pci_dev)) {
962d11e1 802 trace_usb_xhci_irq_msix(v);
9b7d3334 803 msix_notify(pci_dev, v);
4c47f800
GH
804 return;
805 }
806
9b7d3334 807 if (msi_enabled(pci_dev)) {
962d11e1 808 trace_usb_xhci_irq_msi(v);
9b7d3334 809 msi_notify(pci_dev, v);
4c4abe7c 810 return;
62c6ae04 811 }
4c4abe7c 812
962d11e1
GH
813 if (v == 0) {
814 trace_usb_xhci_irq_intx(1);
9e64f8a3 815 pci_irq_assert(pci_dev);
962d11e1 816 }
62c6ae04
HM
817}
818
819static inline int xhci_running(XHCIState *xhci)
820{
962d11e1 821 return !(xhci->usbsts & USBSTS_HCH) && !xhci->intr[0].er_full;
62c6ae04
HM
822}
823
824static void xhci_die(XHCIState *xhci)
825{
826 xhci->usbsts |= USBSTS_HCE;
d6bb65fc 827 DPRINTF("xhci: asserted controller error\n");
62c6ae04
HM
828}
829
962d11e1 830static void xhci_write_event(XHCIState *xhci, XHCIEvent *event, int v)
62c6ae04 831{
9b7d3334 832 PCIDevice *pci_dev = PCI_DEVICE(xhci);
962d11e1 833 XHCIInterrupter *intr = &xhci->intr[v];
62c6ae04 834 XHCITRB ev_trb;
59a70ccd 835 dma_addr_t addr;
62c6ae04
HM
836
837 ev_trb.parameter = cpu_to_le64(event->ptr);
838 ev_trb.status = cpu_to_le32(event->length | (event->ccode << 24));
839 ev_trb.control = (event->slotid << 24) | (event->epid << 16) |
840 event->flags | (event->type << TRB_TYPE_SHIFT);
962d11e1 841 if (intr->er_pcs) {
62c6ae04
HM
842 ev_trb.control |= TRB_C;
843 }
844 ev_trb.control = cpu_to_le32(ev_trb.control);
845
962d11e1 846 trace_usb_xhci_queue_event(v, intr->er_ep_idx, trb_name(&ev_trb),
873123fe
GH
847 event_name(event), ev_trb.parameter,
848 ev_trb.status, ev_trb.control);
62c6ae04 849
962d11e1 850 addr = intr->er_start + TRB_SIZE*intr->er_ep_idx;
9b7d3334 851 pci_dma_write(pci_dev, addr, &ev_trb, TRB_SIZE);
62c6ae04 852
962d11e1
GH
853 intr->er_ep_idx++;
854 if (intr->er_ep_idx >= intr->er_size) {
855 intr->er_ep_idx = 0;
856 intr->er_pcs = !intr->er_pcs;
62c6ae04
HM
857 }
858}
859
962d11e1 860static void xhci_events_update(XHCIState *xhci, int v)
62c6ae04 861{
962d11e1 862 XHCIInterrupter *intr = &xhci->intr[v];
59a70ccd 863 dma_addr_t erdp;
62c6ae04
HM
864 unsigned int dp_idx;
865 bool do_irq = 0;
866
867 if (xhci->usbsts & USBSTS_HCH) {
868 return;
869 }
870
962d11e1
GH
871 erdp = xhci_addr64(intr->erdp_low, intr->erdp_high);
872 if (erdp < intr->er_start ||
873 erdp >= (intr->er_start + TRB_SIZE*intr->er_size)) {
d6bb65fc
GH
874 DPRINTF("xhci: ERDP out of bounds: "DMA_ADDR_FMT"\n", erdp);
875 DPRINTF("xhci: ER[%d] at "DMA_ADDR_FMT" len %d\n",
962d11e1 876 v, intr->er_start, intr->er_size);
62c6ae04
HM
877 xhci_die(xhci);
878 return;
879 }
962d11e1
GH
880 dp_idx = (erdp - intr->er_start) / TRB_SIZE;
881 assert(dp_idx < intr->er_size);
62c6ae04
HM
882
883 /* NEC didn't read section 4.9.4 of the spec (v1.0 p139 top Note) and thus
884 * deadlocks when the ER is full. Hack it by holding off events until
885 * the driver decides to free at least half of the ring */
962d11e1
GH
886 if (intr->er_full) {
887 int er_free = dp_idx - intr->er_ep_idx;
62c6ae04 888 if (er_free <= 0) {
962d11e1 889 er_free += intr->er_size;
62c6ae04 890 }
962d11e1 891 if (er_free < (intr->er_size/2)) {
62c6ae04
HM
892 DPRINTF("xhci_events_update(): event ring still "
893 "more than half full (hack)\n");
894 return;
895 }
896 }
897
962d11e1
GH
898 while (intr->ev_buffer_put != intr->ev_buffer_get) {
899 assert(intr->er_full);
900 if (((intr->er_ep_idx+1) % intr->er_size) == dp_idx) {
62c6ae04
HM
901 DPRINTF("xhci_events_update(): event ring full again\n");
902#ifndef ER_FULL_HACK
903 XHCIEvent full = {ER_HOST_CONTROLLER, CC_EVENT_RING_FULL_ERROR};
962d11e1 904 xhci_write_event(xhci, &full, v);
62c6ae04
HM
905#endif
906 do_irq = 1;
907 break;
908 }
962d11e1
GH
909 XHCIEvent *event = &intr->ev_buffer[intr->ev_buffer_get];
910 xhci_write_event(xhci, event, v);
911 intr->ev_buffer_get++;
62c6ae04 912 do_irq = 1;
962d11e1
GH
913 if (intr->ev_buffer_get == EV_QUEUE) {
914 intr->ev_buffer_get = 0;
62c6ae04
HM
915 }
916 }
917
918 if (do_irq) {
962d11e1 919 xhci_intr_raise(xhci, v);
62c6ae04
HM
920 }
921
962d11e1 922 if (intr->er_full && intr->ev_buffer_put == intr->ev_buffer_get) {
62c6ae04 923 DPRINTF("xhci_events_update(): event ring no longer full\n");
962d11e1 924 intr->er_full = 0;
62c6ae04 925 }
62c6ae04
HM
926}
927
962d11e1 928static void xhci_event(XHCIState *xhci, XHCIEvent *event, int v)
62c6ae04 929{
2d1de850 930 XHCIInterrupter *intr;
59a70ccd 931 dma_addr_t erdp;
62c6ae04
HM
932 unsigned int dp_idx;
933
91062ae0
GH
934 if (v >= xhci->numintrs) {
935 DPRINTF("intr nr out of range (%d >= %d)\n", v, xhci->numintrs);
2d1de850
GH
936 return;
937 }
938 intr = &xhci->intr[v];
939
962d11e1 940 if (intr->er_full) {
62c6ae04 941 DPRINTF("xhci_event(): ER full, queueing\n");
962d11e1 942 if (((intr->ev_buffer_put+1) % EV_QUEUE) == intr->ev_buffer_get) {
d6bb65fc 943 DPRINTF("xhci: event queue full, dropping event!\n");
62c6ae04
HM
944 return;
945 }
962d11e1
GH
946 intr->ev_buffer[intr->ev_buffer_put++] = *event;
947 if (intr->ev_buffer_put == EV_QUEUE) {
948 intr->ev_buffer_put = 0;
62c6ae04
HM
949 }
950 return;
951 }
952
962d11e1
GH
953 erdp = xhci_addr64(intr->erdp_low, intr->erdp_high);
954 if (erdp < intr->er_start ||
955 erdp >= (intr->er_start + TRB_SIZE*intr->er_size)) {
d6bb65fc
GH
956 DPRINTF("xhci: ERDP out of bounds: "DMA_ADDR_FMT"\n", erdp);
957 DPRINTF("xhci: ER[%d] at "DMA_ADDR_FMT" len %d\n",
962d11e1 958 v, intr->er_start, intr->er_size);
62c6ae04
HM
959 xhci_die(xhci);
960 return;
961 }
962
962d11e1
GH
963 dp_idx = (erdp - intr->er_start) / TRB_SIZE;
964 assert(dp_idx < intr->er_size);
62c6ae04 965
962d11e1 966 if ((intr->er_ep_idx+1) % intr->er_size == dp_idx) {
62c6ae04
HM
967 DPRINTF("xhci_event(): ER full, queueing\n");
968#ifndef ER_FULL_HACK
969 XHCIEvent full = {ER_HOST_CONTROLLER, CC_EVENT_RING_FULL_ERROR};
970 xhci_write_event(xhci, &full);
971#endif
962d11e1
GH
972 intr->er_full = 1;
973 if (((intr->ev_buffer_put+1) % EV_QUEUE) == intr->ev_buffer_get) {
d6bb65fc 974 DPRINTF("xhci: event queue full, dropping event!\n");
62c6ae04
HM
975 return;
976 }
962d11e1
GH
977 intr->ev_buffer[intr->ev_buffer_put++] = *event;
978 if (intr->ev_buffer_put == EV_QUEUE) {
979 intr->ev_buffer_put = 0;
62c6ae04
HM
980 }
981 } else {
962d11e1 982 xhci_write_event(xhci, event, v);
62c6ae04
HM
983 }
984
962d11e1 985 xhci_intr_raise(xhci, v);
62c6ae04
HM
986}
987
988static void xhci_ring_init(XHCIState *xhci, XHCIRing *ring,
59a70ccd 989 dma_addr_t base)
62c6ae04 990{
62c6ae04
HM
991 ring->dequeue = base;
992 ring->ccs = 1;
993}
994
995static TRBType xhci_ring_fetch(XHCIState *xhci, XHCIRing *ring, XHCITRB *trb,
59a70ccd 996 dma_addr_t *addr)
62c6ae04 997{
9b7d3334
AF
998 PCIDevice *pci_dev = PCI_DEVICE(xhci);
999
62c6ae04
HM
1000 while (1) {
1001 TRBType type;
9b7d3334 1002 pci_dma_read(pci_dev, ring->dequeue, trb, TRB_SIZE);
62c6ae04
HM
1003 trb->addr = ring->dequeue;
1004 trb->ccs = ring->ccs;
1005 le64_to_cpus(&trb->parameter);
1006 le32_to_cpus(&trb->status);
1007 le32_to_cpus(&trb->control);
1008
0703a4a7
GH
1009 trace_usb_xhci_fetch_trb(ring->dequeue, trb_name(trb),
1010 trb->parameter, trb->status, trb->control);
62c6ae04
HM
1011
1012 if ((trb->control & TRB_C) != ring->ccs) {
1013 return 0;
1014 }
1015
1016 type = TRB_TYPE(*trb);
1017
1018 if (type != TR_LINK) {
1019 if (addr) {
1020 *addr = ring->dequeue;
1021 }
1022 ring->dequeue += TRB_SIZE;
1023 return type;
1024 } else {
1025 ring->dequeue = xhci_mask64(trb->parameter);
1026 if (trb->control & TRB_LK_TC) {
1027 ring->ccs = !ring->ccs;
1028 }
1029 }
1030 }
1031}
1032
1033static int xhci_ring_chain_length(XHCIState *xhci, const XHCIRing *ring)
1034{
9b7d3334 1035 PCIDevice *pci_dev = PCI_DEVICE(xhci);
62c6ae04
HM
1036 XHCITRB trb;
1037 int length = 0;
59a70ccd 1038 dma_addr_t dequeue = ring->dequeue;
62c6ae04
HM
1039 bool ccs = ring->ccs;
1040 /* hack to bundle together the two/three TDs that make a setup transfer */
1041 bool control_td_set = 0;
1042
1043 while (1) {
1044 TRBType type;
9b7d3334 1045 pci_dma_read(pci_dev, dequeue, &trb, TRB_SIZE);
62c6ae04
HM
1046 le64_to_cpus(&trb.parameter);
1047 le32_to_cpus(&trb.status);
1048 le32_to_cpus(&trb.control);
1049
62c6ae04
HM
1050 if ((trb.control & TRB_C) != ccs) {
1051 return -length;
1052 }
1053
1054 type = TRB_TYPE(trb);
1055
1056 if (type == TR_LINK) {
1057 dequeue = xhci_mask64(trb.parameter);
1058 if (trb.control & TRB_LK_TC) {
1059 ccs = !ccs;
1060 }
1061 continue;
1062 }
1063
1064 length += 1;
1065 dequeue += TRB_SIZE;
1066
1067 if (type == TR_SETUP) {
1068 control_td_set = 1;
1069 } else if (type == TR_STATUS) {
1070 control_td_set = 0;
1071 }
1072
1073 if (!control_td_set && !(trb.control & TRB_TR_CH)) {
1074 return length;
1075 }
1076 }
1077}
1078
962d11e1 1079static void xhci_er_reset(XHCIState *xhci, int v)
62c6ae04 1080{
962d11e1 1081 XHCIInterrupter *intr = &xhci->intr[v];
62c6ae04
HM
1082 XHCIEvRingSeg seg;
1083
e099ad4b
GH
1084 if (intr->erstsz == 0) {
1085 /* disabled */
1086 intr->er_start = 0;
1087 intr->er_size = 0;
1088 return;
1089 }
62c6ae04 1090 /* cache the (sole) event ring segment location */
962d11e1 1091 if (intr->erstsz != 1) {
d6bb65fc 1092 DPRINTF("xhci: invalid value for ERSTSZ: %d\n", intr->erstsz);
62c6ae04
HM
1093 xhci_die(xhci);
1094 return;
1095 }
962d11e1 1096 dma_addr_t erstba = xhci_addr64(intr->erstba_low, intr->erstba_high);
9b7d3334 1097 pci_dma_read(PCI_DEVICE(xhci), erstba, &seg, sizeof(seg));
62c6ae04
HM
1098 le32_to_cpus(&seg.addr_low);
1099 le32_to_cpus(&seg.addr_high);
1100 le32_to_cpus(&seg.size);
1101 if (seg.size < 16 || seg.size > 4096) {
d6bb65fc 1102 DPRINTF("xhci: invalid value for segment size: %d\n", seg.size);
62c6ae04
HM
1103 xhci_die(xhci);
1104 return;
1105 }
962d11e1
GH
1106 intr->er_start = xhci_addr64(seg.addr_low, seg.addr_high);
1107 intr->er_size = seg.size;
62c6ae04 1108
962d11e1
GH
1109 intr->er_ep_idx = 0;
1110 intr->er_pcs = 1;
1111 intr->er_full = 0;
62c6ae04 1112
962d11e1
GH
1113 DPRINTF("xhci: event ring[%d]:" DMA_ADDR_FMT " [%d]\n",
1114 v, intr->er_start, intr->er_size);
62c6ae04
HM
1115}
1116
1117static void xhci_run(XHCIState *xhci)
1118{
fc0ddaca 1119 trace_usb_xhci_run();
62c6ae04 1120 xhci->usbsts &= ~USBSTS_HCH;
bc72ad67 1121 xhci->mfindex_start = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
62c6ae04
HM
1122}
1123
1124static void xhci_stop(XHCIState *xhci)
1125{
fc0ddaca 1126 trace_usb_xhci_stop();
62c6ae04
HM
1127 xhci->usbsts |= USBSTS_HCH;
1128 xhci->crcr_low &= ~CRCR_CRR;
1129}
1130
024426ac
GH
1131static XHCIStreamContext *xhci_alloc_stream_contexts(unsigned count,
1132 dma_addr_t base)
1133{
1134 XHCIStreamContext *stctx;
1135 unsigned int i;
1136
1137 stctx = g_new0(XHCIStreamContext, count);
1138 for (i = 0; i < count; i++) {
1139 stctx[i].pctx = base + i * 16;
1140 stctx[i].sct = -1;
1141 }
1142 return stctx;
1143}
1144
1145static void xhci_reset_streams(XHCIEPContext *epctx)
1146{
1147 unsigned int i;
1148
1149 for (i = 0; i < epctx->nr_pstreams; i++) {
1150 epctx->pstreams[i].sct = -1;
024426ac
GH
1151 }
1152}
1153
1154static void xhci_alloc_streams(XHCIEPContext *epctx, dma_addr_t base)
1155{
1156 assert(epctx->pstreams == NULL);
f90e160b 1157 epctx->nr_pstreams = 2 << epctx->max_pstreams;
024426ac
GH
1158 epctx->pstreams = xhci_alloc_stream_contexts(epctx->nr_pstreams, base);
1159}
1160
1161static void xhci_free_streams(XHCIEPContext *epctx)
1162{
024426ac
GH
1163 assert(epctx->pstreams != NULL);
1164
024426ac
GH
1165 g_free(epctx->pstreams);
1166 epctx->pstreams = NULL;
1167 epctx->nr_pstreams = 0;
1168}
1169
72391da5
HG
1170static int xhci_epmask_to_eps_with_streams(XHCIState *xhci,
1171 unsigned int slotid,
1172 uint32_t epmask,
1173 XHCIEPContext **epctxs,
1174 USBEndpoint **eps)
1175{
1176 XHCISlot *slot;
1177 XHCIEPContext *epctx;
1178 USBEndpoint *ep;
1179 int i, j;
1180
1181 assert(slotid >= 1 && slotid <= xhci->numslots);
1182
1183 slot = &xhci->slots[slotid - 1];
1184
1185 for (i = 2, j = 0; i <= 31; i++) {
3d80365b 1186 if (!(epmask & (1u << i))) {
72391da5
HG
1187 continue;
1188 }
1189
1190 epctx = slot->eps[i - 1];
1191 ep = xhci_epid_to_usbep(xhci, slotid, i);
1192 if (!epctx || !epctx->nr_pstreams || !ep) {
1193 continue;
1194 }
1195
1196 if (epctxs) {
1197 epctxs[j] = epctx;
1198 }
1199 eps[j++] = ep;
1200 }
1201 return j;
1202}
1203
1204static void xhci_free_device_streams(XHCIState *xhci, unsigned int slotid,
1205 uint32_t epmask)
1206{
1207 USBEndpoint *eps[30];
1208 int nr_eps;
1209
1210 nr_eps = xhci_epmask_to_eps_with_streams(xhci, slotid, epmask, NULL, eps);
1211 if (nr_eps) {
1212 usb_device_free_streams(eps[0]->dev, eps, nr_eps);
1213 }
1214}
1215
1216static TRBCCode xhci_alloc_device_streams(XHCIState *xhci, unsigned int slotid,
1217 uint32_t epmask)
1218{
1219 XHCIEPContext *epctxs[30];
1220 USBEndpoint *eps[30];
1221 int i, r, nr_eps, req_nr_streams, dev_max_streams;
1222
1223 nr_eps = xhci_epmask_to_eps_with_streams(xhci, slotid, epmask, epctxs,
1224 eps);
1225 if (nr_eps == 0) {
1226 return CC_SUCCESS;
1227 }
1228
1229 req_nr_streams = epctxs[0]->nr_pstreams;
1230 dev_max_streams = eps[0]->max_streams;
1231
1232 for (i = 1; i < nr_eps; i++) {
1233 /*
1234 * HdG: I don't expect these to ever trigger, but if they do we need
1235 * to come up with another solution, ie group identical endpoints
1236 * together and make an usb_device_alloc_streams call per group.
1237 */
1238 if (epctxs[i]->nr_pstreams != req_nr_streams) {
1239 FIXME("guest streams config not identical for all eps");
1240 return CC_RESOURCE_ERROR;
1241 }
1242 if (eps[i]->max_streams != dev_max_streams) {
1243 FIXME("device streams config not identical for all eps");
1244 return CC_RESOURCE_ERROR;
1245 }
1246 }
1247
1248 /*
1249 * max-streams in both the device descriptor and in the controller is a
1250 * power of 2. But stream id 0 is reserved, so if a device can do up to 4
1251 * streams the guest will ask for 5 rounded up to the next power of 2 which
1252 * becomes 8. For emulated devices usb_device_alloc_streams is a nop.
1253 *
1254 * For redirected devices however this is an issue, as there we must ask
1255 * the real xhci controller to alloc streams, and the host driver for the
1256 * real xhci controller will likely disallow allocating more streams then
1257 * the device can handle.
1258 *
1259 * So we limit the requested nr_streams to the maximum number the device
1260 * can handle.
1261 */
1262 if (req_nr_streams > dev_max_streams) {
1263 req_nr_streams = dev_max_streams;
1264 }
1265
1266 r = usb_device_alloc_streams(eps[0]->dev, eps, nr_eps, req_nr_streams);
1267 if (r != 0) {
d6bb65fc 1268 DPRINTF("xhci: alloc streams failed\n");
72391da5
HG
1269 return CC_RESOURCE_ERROR;
1270 }
1271
1272 return CC_SUCCESS;
1273}
1274
024426ac
GH
1275static XHCIStreamContext *xhci_find_stream(XHCIEPContext *epctx,
1276 unsigned int streamid,
1277 uint32_t *cc_error)
1278{
1279 XHCIStreamContext *sctx;
1280 dma_addr_t base;
1281 uint32_t ctx[2], sct;
1282
1283 assert(streamid != 0);
1284 if (epctx->lsa) {
1285 if (streamid >= epctx->nr_pstreams) {
1286 *cc_error = CC_INVALID_STREAM_ID_ERROR;
1287 return NULL;
1288 }
1289 sctx = epctx->pstreams + streamid;
1290 } else {
1291 FIXME("secondary streams not implemented yet");
1292 }
1293
1294 if (sctx->sct == -1) {
1295 xhci_dma_read_u32s(epctx->xhci, sctx->pctx, ctx, sizeof(ctx));
024426ac
GH
1296 sct = (ctx[0] >> 1) & 0x07;
1297 if (epctx->lsa && sct != 1) {
1298 *cc_error = CC_INVALID_STREAM_TYPE_ERROR;
1299 return NULL;
1300 }
1301 sctx->sct = sct;
1302 base = xhci_addr64(ctx[0] & ~0xf, ctx[1]);
1303 xhci_ring_init(epctx->xhci, &sctx->ring, base);
1304 }
1305 return sctx;
1306}
1307
62c6ae04 1308static void xhci_set_ep_state(XHCIState *xhci, XHCIEPContext *epctx,
024426ac 1309 XHCIStreamContext *sctx, uint32_t state)
62c6ae04 1310{
c90daa1c 1311 XHCIRing *ring = NULL;
62c6ae04 1312 uint32_t ctx[5];
024426ac 1313 uint32_t ctx2[2];
62c6ae04 1314
616b5d53 1315 xhci_dma_read_u32s(xhci, epctx->pctx, ctx, sizeof(ctx));
62c6ae04
HM
1316 ctx[0] &= ~EP_STATE_MASK;
1317 ctx[0] |= state;
024426ac
GH
1318
1319 /* update ring dequeue ptr */
1320 if (epctx->nr_pstreams) {
1321 if (sctx != NULL) {
c90daa1c 1322 ring = &sctx->ring;
024426ac
GH
1323 xhci_dma_read_u32s(xhci, sctx->pctx, ctx2, sizeof(ctx2));
1324 ctx2[0] &= 0xe;
1325 ctx2[0] |= sctx->ring.dequeue | sctx->ring.ccs;
1326 ctx2[1] = (sctx->ring.dequeue >> 16) >> 16;
1327 xhci_dma_write_u32s(xhci, sctx->pctx, ctx2, sizeof(ctx2));
1328 }
1329 } else {
c90daa1c
HG
1330 ring = &epctx->ring;
1331 }
1332 if (ring) {
1333 ctx[2] = ring->dequeue | ring->ccs;
1334 ctx[3] = (ring->dequeue >> 16) >> 16;
1335
024426ac
GH
1336 DPRINTF("xhci: set epctx: " DMA_ADDR_FMT " state=%d dequeue=%08x%08x\n",
1337 epctx->pctx, state, ctx[3], ctx[2]);
1338 }
1339
616b5d53 1340 xhci_dma_write_u32s(xhci, epctx->pctx, ctx, sizeof(ctx));
1c82392a
GH
1341 if (epctx->state != state) {
1342 trace_usb_xhci_ep_state(epctx->slotid, epctx->epid,
1343 ep_state_name(epctx->state),
1344 ep_state_name(state));
1345 }
62c6ae04
HM
1346 epctx->state = state;
1347}
1348
3d139684
GH
1349static void xhci_ep_kick_timer(void *opaque)
1350{
1351 XHCIEPContext *epctx = opaque;
024426ac 1352 xhci_kick_ep(epctx->xhci, epctx->slotid, epctx->epid, 0);
3d139684
GH
1353}
1354
492b21f6
GH
1355static XHCIEPContext *xhci_alloc_epctx(XHCIState *xhci,
1356 unsigned int slotid,
1357 unsigned int epid)
1358{
1359 XHCIEPContext *epctx;
1360 int i;
1361
1362 epctx = g_new0(XHCIEPContext, 1);
1363 epctx->xhci = xhci;
1364 epctx->slotid = slotid;
1365 epctx->epid = epid;
1366
1367 for (i = 0; i < ARRAY_SIZE(epctx->transfers); i++) {
4c5d82ec
HG
1368 epctx->transfers[i].xhci = xhci;
1369 epctx->transfers[i].slotid = slotid;
1370 epctx->transfers[i].epid = epid;
492b21f6
GH
1371 usb_packet_init(&epctx->transfers[i].packet);
1372 }
bc72ad67 1373 epctx->kick_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, xhci_ep_kick_timer, epctx);
492b21f6
GH
1374
1375 return epctx;
1376}
1377
003e15a1
GH
1378static void xhci_init_epctx(XHCIEPContext *epctx,
1379 dma_addr_t pctx, uint32_t *ctx)
62c6ae04 1380{
59a70ccd 1381 dma_addr_t dequeue;
62c6ae04 1382
62c6ae04 1383 dequeue = xhci_addr64(ctx[2] & ~0xf, ctx[3]);
62c6ae04
HM
1384
1385 epctx->type = (ctx[1] >> EP_TYPE_SHIFT) & EP_TYPE_MASK;
62c6ae04
HM
1386 epctx->pctx = pctx;
1387 epctx->max_psize = ctx[1]>>16;
1388 epctx->max_psize *= 1+((ctx[1]>>8)&0xff);
2aa6bfcb 1389 epctx->max_pstreams = (ctx[0] >> 10) & epctx->xhci->max_pstreams_mask;
024426ac 1390 epctx->lsa = (ctx[0] >> 15) & 1;
024426ac
GH
1391 if (epctx->max_pstreams) {
1392 xhci_alloc_streams(epctx, dequeue);
1393 } else {
003e15a1 1394 xhci_ring_init(epctx->xhci, &epctx->ring, dequeue);
024426ac
GH
1395 epctx->ring.ccs = ctx[2] & 1;
1396 }
62c6ae04 1397
ca716278 1398 epctx->interval = 1 << ((ctx[0] >> 16) & 0xff);
003e15a1
GH
1399}
1400
1401static TRBCCode xhci_enable_ep(XHCIState *xhci, unsigned int slotid,
1402 unsigned int epid, dma_addr_t pctx,
1403 uint32_t *ctx)
1404{
1405 XHCISlot *slot;
1406 XHCIEPContext *epctx;
1407
1408 trace_usb_xhci_ep_enable(slotid, epid);
1409 assert(slotid >= 1 && slotid <= xhci->numslots);
1410 assert(epid >= 1 && epid <= 31);
1411
1412 slot = &xhci->slots[slotid-1];
1413 if (slot->eps[epid-1]) {
1414 xhci_disable_ep(xhci, slotid, epid);
1415 }
1416
1417 epctx = xhci_alloc_epctx(xhci, slotid, epid);
1418 slot->eps[epid-1] = epctx;
1419 xhci_init_epctx(epctx, pctx, ctx);
1420
8c244210
GA
1421 DPRINTF("xhci: endpoint %d.%d type is %d, max transaction (burst) "
1422 "size is %d\n", epid/2, epid%2, epctx->type, epctx->max_psize);
1423
3d139684 1424 epctx->mfindex_last = 0;
3d139684 1425
62c6ae04
HM
1426 epctx->state = EP_RUNNING;
1427 ctx[0] &= ~EP_STATE_MASK;
1428 ctx[0] |= EP_RUNNING;
1429
1430 return CC_SUCCESS;
1431}
1432
582d6f4a 1433static int xhci_ep_nuke_one_xfer(XHCITransfer *t, TRBCCode report)
3151f209
HG
1434{
1435 int killed = 0;
1436
582d6f4a
HG
1437 if (report && (t->running_async || t->running_retry)) {
1438 t->status = report;
1439 xhci_xfer_report(t);
1440 }
1441
3151f209
HG
1442 if (t->running_async) {
1443 usb_cancel_packet(&t->packet);
1444 t->running_async = 0;
3151f209
HG
1445 killed = 1;
1446 }
1447 if (t->running_retry) {
1448 XHCIEPContext *epctx = t->xhci->slots[t->slotid-1].eps[t->epid-1];
1449 if (epctx) {
1450 epctx->retry = NULL;
bc72ad67 1451 timer_del(epctx->kick_timer);
3151f209
HG
1452 }
1453 t->running_retry = 0;
582d6f4a 1454 killed = 1;
3151f209
HG
1455 }
1456 if (t->trbs) {
1457 g_free(t->trbs);
1458 }
1459
1460 t->trbs = NULL;
1461 t->trb_count = t->trb_alloced = 0;
1462
1463 return killed;
1464}
1465
62c6ae04 1466static int xhci_ep_nuke_xfers(XHCIState *xhci, unsigned int slotid,
582d6f4a 1467 unsigned int epid, TRBCCode report)
62c6ae04
HM
1468{
1469 XHCISlot *slot;
1470 XHCIEPContext *epctx;
1471 int i, xferi, killed = 0;
f79738b0 1472 USBEndpoint *ep = NULL;
91062ae0 1473 assert(slotid >= 1 && slotid <= xhci->numslots);
62c6ae04
HM
1474 assert(epid >= 1 && epid <= 31);
1475
1476 DPRINTF("xhci_ep_nuke_xfers(%d, %d)\n", slotid, epid);
1477
1478 slot = &xhci->slots[slotid-1];
1479
1480 if (!slot->eps[epid-1]) {
1481 return 0;
1482 }
1483
1484 epctx = slot->eps[epid-1];
1485
1486 xferi = epctx->next_xfer;
1487 for (i = 0; i < TD_QUEUE; i++) {
582d6f4a
HG
1488 killed += xhci_ep_nuke_one_xfer(&epctx->transfers[xferi], report);
1489 if (killed) {
1490 report = 0; /* Only report once */
1491 }
0cb41e2c 1492 epctx->transfers[xferi].packet.ep = NULL;
62c6ae04
HM
1493 xferi = (xferi + 1) % TD_QUEUE;
1494 }
518ad5f2
HG
1495
1496 ep = xhci_epid_to_usbep(xhci, slotid, epid);
f79738b0
HG
1497 if (ep) {
1498 usb_device_ep_stopped(ep->dev, ep);
1499 }
62c6ae04
HM
1500 return killed;
1501}
1502
1503static TRBCCode xhci_disable_ep(XHCIState *xhci, unsigned int slotid,
1504 unsigned int epid)
1505{
1506 XHCISlot *slot;
1507 XHCIEPContext *epctx;
b21da4e5 1508 int i;
62c6ae04 1509
c1f6b493 1510 trace_usb_xhci_ep_disable(slotid, epid);
91062ae0 1511 assert(slotid >= 1 && slotid <= xhci->numslots);
62c6ae04
HM
1512 assert(epid >= 1 && epid <= 31);
1513
62c6ae04
HM
1514 slot = &xhci->slots[slotid-1];
1515
1516 if (!slot->eps[epid-1]) {
1517 DPRINTF("xhci: slot %d ep %d already disabled\n", slotid, epid);
1518 return CC_SUCCESS;
1519 }
1520
582d6f4a 1521 xhci_ep_nuke_xfers(xhci, slotid, epid, 0);
62c6ae04
HM
1522
1523 epctx = slot->eps[epid-1];
1524
024426ac
GH
1525 if (epctx->nr_pstreams) {
1526 xhci_free_streams(epctx);
1527 }
1528
b21da4e5
HG
1529 for (i = 0; i < ARRAY_SIZE(epctx->transfers); i++) {
1530 usb_packet_cleanup(&epctx->transfers[i].packet);
1531 }
1532
024426ac 1533 xhci_set_ep_state(xhci, epctx, NULL, EP_DISABLED);
62c6ae04 1534
bc72ad67 1535 timer_free(epctx->kick_timer);
62c6ae04
HM
1536 g_free(epctx);
1537 slot->eps[epid-1] = NULL;
1538
1539 return CC_SUCCESS;
1540}
1541
1542static TRBCCode xhci_stop_ep(XHCIState *xhci, unsigned int slotid,
1543 unsigned int epid)
1544{
1545 XHCISlot *slot;
1546 XHCIEPContext *epctx;
1547
c1f6b493 1548 trace_usb_xhci_ep_stop(slotid, epid);
91062ae0 1549 assert(slotid >= 1 && slotid <= xhci->numslots);
62c6ae04
HM
1550
1551 if (epid < 1 || epid > 31) {
d6bb65fc 1552 DPRINTF("xhci: bad ep %d\n", epid);
62c6ae04
HM
1553 return CC_TRB_ERROR;
1554 }
1555
1556 slot = &xhci->slots[slotid-1];
1557
1558 if (!slot->eps[epid-1]) {
1559 DPRINTF("xhci: slot %d ep %d not enabled\n", slotid, epid);
1560 return CC_EP_NOT_ENABLED_ERROR;
1561 }
1562
582d6f4a 1563 if (xhci_ep_nuke_xfers(xhci, slotid, epid, CC_STOPPED) > 0) {
d6bb65fc 1564 DPRINTF("xhci: FIXME: endpoint stopped w/ xfers running, "
62c6ae04
HM
1565 "data might be lost\n");
1566 }
1567
1568 epctx = slot->eps[epid-1];
1569
024426ac
GH
1570 xhci_set_ep_state(xhci, epctx, NULL, EP_STOPPED);
1571
1572 if (epctx->nr_pstreams) {
1573 xhci_reset_streams(epctx);
1574 }
62c6ae04
HM
1575
1576 return CC_SUCCESS;
1577}
1578
1579static TRBCCode xhci_reset_ep(XHCIState *xhci, unsigned int slotid,
1580 unsigned int epid)
1581{
1582 XHCISlot *slot;
1583 XHCIEPContext *epctx;
62c6ae04 1584
c1f6b493 1585 trace_usb_xhci_ep_reset(slotid, epid);
91062ae0 1586 assert(slotid >= 1 && slotid <= xhci->numslots);
62c6ae04 1587
62c6ae04 1588 if (epid < 1 || epid > 31) {
d6bb65fc 1589 DPRINTF("xhci: bad ep %d\n", epid);
62c6ae04
HM
1590 return CC_TRB_ERROR;
1591 }
1592
1593 slot = &xhci->slots[slotid-1];
1594
1595 if (!slot->eps[epid-1]) {
1596 DPRINTF("xhci: slot %d ep %d not enabled\n", slotid, epid);
1597 return CC_EP_NOT_ENABLED_ERROR;
1598 }
1599
1600 epctx = slot->eps[epid-1];
1601
1602 if (epctx->state != EP_HALTED) {
d6bb65fc 1603 DPRINTF("xhci: reset EP while EP %d not halted (%d)\n",
62c6ae04
HM
1604 epid, epctx->state);
1605 return CC_CONTEXT_STATE_ERROR;
1606 }
1607
582d6f4a 1608 if (xhci_ep_nuke_xfers(xhci, slotid, epid, 0) > 0) {
d6bb65fc 1609 DPRINTF("xhci: FIXME: endpoint reset w/ xfers running, "
62c6ae04
HM
1610 "data might be lost\n");
1611 }
1612
75cc1c1f 1613 if (!xhci->slots[slotid-1].uport ||
de9de157
HG
1614 !xhci->slots[slotid-1].uport->dev ||
1615 !xhci->slots[slotid-1].uport->dev->attached) {
62c6ae04
HM
1616 return CC_USB_TRANSACTION_ERROR;
1617 }
1618
024426ac
GH
1619 xhci_set_ep_state(xhci, epctx, NULL, EP_STOPPED);
1620
1621 if (epctx->nr_pstreams) {
1622 xhci_reset_streams(epctx);
1623 }
62c6ae04
HM
1624
1625 return CC_SUCCESS;
1626}
1627
1628static TRBCCode xhci_set_ep_dequeue(XHCIState *xhci, unsigned int slotid,
024426ac
GH
1629 unsigned int epid, unsigned int streamid,
1630 uint64_t pdequeue)
62c6ae04
HM
1631{
1632 XHCISlot *slot;
1633 XHCIEPContext *epctx;
024426ac 1634 XHCIStreamContext *sctx;
59a70ccd 1635 dma_addr_t dequeue;
62c6ae04 1636
91062ae0 1637 assert(slotid >= 1 && slotid <= xhci->numslots);
62c6ae04
HM
1638
1639 if (epid < 1 || epid > 31) {
d6bb65fc 1640 DPRINTF("xhci: bad ep %d\n", epid);
62c6ae04
HM
1641 return CC_TRB_ERROR;
1642 }
1643
024426ac 1644 trace_usb_xhci_ep_set_dequeue(slotid, epid, streamid, pdequeue);
62c6ae04
HM
1645 dequeue = xhci_mask64(pdequeue);
1646
1647 slot = &xhci->slots[slotid-1];
1648
1649 if (!slot->eps[epid-1]) {
1650 DPRINTF("xhci: slot %d ep %d not enabled\n", slotid, epid);
1651 return CC_EP_NOT_ENABLED_ERROR;
1652 }
1653
1654 epctx = slot->eps[epid-1];
1655
62c6ae04 1656 if (epctx->state != EP_STOPPED) {
d6bb65fc 1657 DPRINTF("xhci: set EP dequeue pointer while EP %d not stopped\n", epid);
62c6ae04
HM
1658 return CC_CONTEXT_STATE_ERROR;
1659 }
1660
024426ac
GH
1661 if (epctx->nr_pstreams) {
1662 uint32_t err;
1663 sctx = xhci_find_stream(epctx, streamid, &err);
1664 if (sctx == NULL) {
1665 return err;
1666 }
1667 xhci_ring_init(xhci, &sctx->ring, dequeue & ~0xf);
1668 sctx->ring.ccs = dequeue & 1;
1669 } else {
1670 sctx = NULL;
1671 xhci_ring_init(xhci, &epctx->ring, dequeue & ~0xF);
1672 epctx->ring.ccs = dequeue & 1;
1673 }
62c6ae04 1674
024426ac 1675 xhci_set_ep_state(xhci, epctx, sctx, EP_STOPPED);
62c6ae04
HM
1676
1677 return CC_SUCCESS;
1678}
1679
a6fb2ddb 1680static int xhci_xfer_create_sgl(XHCITransfer *xfer, int in_xfer)
62c6ae04 1681{
62c6ae04 1682 XHCIState *xhci = xfer->xhci;
d5a15814 1683 int i;
62c6ae04 1684
a6fb2ddb 1685 xfer->int_req = false;
9b7d3334 1686 pci_dma_sglist_init(&xfer->sgl, PCI_DEVICE(xhci), xfer->trb_count);
62c6ae04
HM
1687 for (i = 0; i < xfer->trb_count; i++) {
1688 XHCITRB *trb = &xfer->trbs[i];
59a70ccd 1689 dma_addr_t addr;
62c6ae04
HM
1690 unsigned int chunk = 0;
1691
a6fb2ddb
HG
1692 if (trb->control & TRB_TR_IOC) {
1693 xfer->int_req = true;
1694 }
1695
62c6ae04
HM
1696 switch (TRB_TYPE(*trb)) {
1697 case TR_DATA:
1698 if ((!(trb->control & TRB_TR_DIR)) != (!in_xfer)) {
d6bb65fc 1699 DPRINTF("xhci: data direction mismatch for TR_DATA\n");
d5a15814 1700 goto err;
62c6ae04
HM
1701 }
1702 /* fallthrough */
1703 case TR_NORMAL:
1704 case TR_ISOCH:
1705 addr = xhci_mask64(trb->parameter);
d5a15814
GH
1706 chunk = trb->status & 0x1ffff;
1707 if (trb->control & TRB_TR_IDT) {
1708 if (chunk > 8 || in_xfer) {
d6bb65fc 1709 DPRINTF("xhci: invalid immediate data TRB\n");
d5a15814
GH
1710 goto err;
1711 }
1712 qemu_sglist_add(&xfer->sgl, trb->addr, chunk);
1713 } else {
1714 qemu_sglist_add(&xfer->sgl, addr, chunk);
1715 }
1716 break;
1717 }
1718 }
1719
d5a15814
GH
1720 return 0;
1721
1722err:
1723 qemu_sglist_destroy(&xfer->sgl);
1724 xhci_die(xhci);
1725 return -1;
1726}
1727
1728static void xhci_xfer_unmap(XHCITransfer *xfer)
1729{
1730 usb_packet_unmap(&xfer->packet, &xfer->sgl);
1731 qemu_sglist_destroy(&xfer->sgl);
1732}
1733
1734static void xhci_xfer_report(XHCITransfer *xfer)
1735{
1736 uint32_t edtla = 0;
1737 unsigned int left;
1738 bool reported = 0;
1739 bool shortpkt = 0;
1740 XHCIEvent event = {ER_TRANSFER, CC_SUCCESS};
1741 XHCIState *xhci = xfer->xhci;
1742 int i;
1743
9b8251c5 1744 left = xfer->packet.actual_length;
d5a15814
GH
1745
1746 for (i = 0; i < xfer->trb_count; i++) {
1747 XHCITRB *trb = &xfer->trbs[i];
1748 unsigned int chunk = 0;
1749
1750 switch (TRB_TYPE(*trb)) {
1751 case TR_DATA:
1752 case TR_NORMAL:
1753 case TR_ISOCH:
62c6ae04
HM
1754 chunk = trb->status & 0x1ffff;
1755 if (chunk > left) {
1756 chunk = left;
d5a15814
GH
1757 if (xfer->status == CC_SUCCESS) {
1758 shortpkt = 1;
62c6ae04
HM
1759 }
1760 }
1761 left -= chunk;
62c6ae04 1762 edtla += chunk;
62c6ae04
HM
1763 break;
1764 case TR_STATUS:
1765 reported = 0;
1766 shortpkt = 0;
1767 break;
1768 }
1769
d5a15814
GH
1770 if (!reported && ((trb->control & TRB_TR_IOC) ||
1771 (shortpkt && (trb->control & TRB_TR_ISP)) ||
9b8251c5 1772 (xfer->status != CC_SUCCESS && left == 0))) {
62c6ae04
HM
1773 event.slotid = xfer->slotid;
1774 event.epid = xfer->epid;
1775 event.length = (trb->status & 0x1ffff) - chunk;
1776 event.flags = 0;
1777 event.ptr = trb->addr;
1778 if (xfer->status == CC_SUCCESS) {
1779 event.ccode = shortpkt ? CC_SHORT_PACKET : CC_SUCCESS;
1780 } else {
1781 event.ccode = xfer->status;
1782 }
1783 if (TRB_TYPE(*trb) == TR_EVDATA) {
1784 event.ptr = trb->parameter;
1785 event.flags |= TRB_EV_ED;
1786 event.length = edtla & 0xffffff;
1787 DPRINTF("xhci_xfer_data: EDTLA=%d\n", event.length);
1788 edtla = 0;
1789 }
2d1de850 1790 xhci_event(xhci, &event, TRB_INTR(*trb));
62c6ae04 1791 reported = 1;
d5a15814
GH
1792 if (xfer->status != CC_SUCCESS) {
1793 return;
1794 }
62c6ae04
HM
1795 }
1796 }
62c6ae04
HM
1797}
1798
1799static void xhci_stall_ep(XHCITransfer *xfer)
1800{
1801 XHCIState *xhci = xfer->xhci;
1802 XHCISlot *slot = &xhci->slots[xfer->slotid-1];
1803 XHCIEPContext *epctx = slot->eps[xfer->epid-1];
024426ac
GH
1804 uint32_t err;
1805 XHCIStreamContext *sctx;
62c6ae04 1806
024426ac
GH
1807 if (epctx->nr_pstreams) {
1808 sctx = xhci_find_stream(epctx, xfer->streamid, &err);
1809 if (sctx == NULL) {
1810 return;
1811 }
1812 sctx->ring.dequeue = xfer->trbs[0].addr;
1813 sctx->ring.ccs = xfer->trbs[0].ccs;
1814 xhci_set_ep_state(xhci, epctx, sctx, EP_HALTED);
1815 } else {
1816 epctx->ring.dequeue = xfer->trbs[0].addr;
1817 epctx->ring.ccs = xfer->trbs[0].ccs;
1818 xhci_set_ep_state(xhci, epctx, NULL, EP_HALTED);
1819 }
62c6ae04
HM
1820}
1821
1822static int xhci_submit(XHCIState *xhci, XHCITransfer *xfer,
1823 XHCIEPContext *epctx);
1824
5c08106f
GH
1825static int xhci_setup_packet(XHCITransfer *xfer)
1826{
1827 XHCIState *xhci = xfer->xhci;
079d0b7f
GH
1828 USBEndpoint *ep;
1829 int dir;
1830
1831 dir = xfer->in_xfer ? USB_TOKEN_IN : USB_TOKEN_OUT;
5c08106f
GH
1832
1833 if (xfer->packet.ep) {
1834 ep = xfer->packet.ep;
5c08106f 1835 } else {
518ad5f2
HG
1836 ep = xhci_epid_to_usbep(xhci, xfer->slotid, xfer->epid);
1837 if (!ep) {
d6bb65fc 1838 DPRINTF("xhci: slot %d has no device\n",
ccaf87a0 1839 xfer->slotid);
5c08106f
GH
1840 return -1;
1841 }
5c08106f
GH
1842 }
1843
a6fb2ddb 1844 xhci_xfer_create_sgl(xfer, dir == USB_TOKEN_IN); /* Also sets int_req */
024426ac 1845 usb_packet_setup(&xfer->packet, dir, ep, xfer->streamid,
8550a02d 1846 xfer->trbs[0].addr, false, xfer->int_req);
a6fb2ddb 1847 usb_packet_map(&xfer->packet, &xfer->sgl);
62c6ae04 1848 DPRINTF("xhci: setup packet pid 0x%x addr %d ep %d\n",
518ad5f2 1849 xfer->packet.pid, ep->dev->addr, ep->nr);
62c6ae04
HM
1850 return 0;
1851}
1852
9a77a0f5 1853static int xhci_complete_packet(XHCITransfer *xfer)
62c6ae04 1854{
9a77a0f5 1855 if (xfer->packet.status == USB_RET_ASYNC) {
97df650b 1856 trace_usb_xhci_xfer_async(xfer);
7c605a23
GH
1857 xfer->running_async = 1;
1858 xfer->running_retry = 0;
1859 xfer->complete = 0;
7c605a23 1860 return 0;
9a77a0f5 1861 } else if (xfer->packet.status == USB_RET_NAK) {
97df650b 1862 trace_usb_xhci_xfer_nak(xfer);
7c605a23
GH
1863 xfer->running_async = 0;
1864 xfer->running_retry = 1;
62c6ae04 1865 xfer->complete = 0;
62c6ae04
HM
1866 return 0;
1867 } else {
7c605a23
GH
1868 xfer->running_async = 0;
1869 xfer->running_retry = 0;
62c6ae04 1870 xfer->complete = 1;
d5a15814 1871 xhci_xfer_unmap(xfer);
62c6ae04
HM
1872 }
1873
9a77a0f5
HG
1874 if (xfer->packet.status == USB_RET_SUCCESS) {
1875 trace_usb_xhci_xfer_success(xfer, xfer->packet.actual_length);
d5a15814
GH
1876 xfer->status = CC_SUCCESS;
1877 xhci_xfer_report(xfer);
62c6ae04
HM
1878 return 0;
1879 }
1880
1881 /* error */
9a77a0f5
HG
1882 trace_usb_xhci_xfer_error(xfer, xfer->packet.status);
1883 switch (xfer->packet.status) {
62c6ae04 1884 case USB_RET_NODEV:
ed60ff02 1885 case USB_RET_IOERROR:
62c6ae04 1886 xfer->status = CC_USB_TRANSACTION_ERROR;
d5a15814 1887 xhci_xfer_report(xfer);
62c6ae04
HM
1888 xhci_stall_ep(xfer);
1889 break;
1890 case USB_RET_STALL:
1891 xfer->status = CC_STALL_ERROR;
d5a15814 1892 xhci_xfer_report(xfer);
62c6ae04
HM
1893 xhci_stall_ep(xfer);
1894 break;
4e906d56
GH
1895 case USB_RET_BABBLE:
1896 xfer->status = CC_BABBLE_DETECTED;
1897 xhci_xfer_report(xfer);
1898 xhci_stall_ep(xfer);
1899 break;
62c6ae04 1900 default:
d6bb65fc 1901 DPRINTF("%s: FIXME: status = %d\n", __func__,
9a77a0f5 1902 xfer->packet.status);
024426ac 1903 FIXME("unhandled USB_RET_*");
62c6ae04
HM
1904 }
1905 return 0;
1906}
1907
1908static int xhci_fire_ctl_transfer(XHCIState *xhci, XHCITransfer *xfer)
1909{
1910 XHCITRB *trb_setup, *trb_status;
2850ca9e 1911 uint8_t bmRequestType;
62c6ae04 1912
62c6ae04
HM
1913 trb_setup = &xfer->trbs[0];
1914 trb_status = &xfer->trbs[xfer->trb_count-1];
1915
024426ac 1916 trace_usb_xhci_xfer_start(xfer, xfer->slotid, xfer->epid, xfer->streamid);
97df650b 1917
62c6ae04
HM
1918 /* at most one Event Data TRB allowed after STATUS */
1919 if (TRB_TYPE(*trb_status) == TR_EVDATA && xfer->trb_count > 2) {
1920 trb_status--;
1921 }
1922
1923 /* do some sanity checks */
1924 if (TRB_TYPE(*trb_setup) != TR_SETUP) {
d6bb65fc 1925 DPRINTF("xhci: ep0 first TD not SETUP: %d\n",
62c6ae04
HM
1926 TRB_TYPE(*trb_setup));
1927 return -1;
1928 }
1929 if (TRB_TYPE(*trb_status) != TR_STATUS) {
d6bb65fc 1930 DPRINTF("xhci: ep0 last TD not STATUS: %d\n",
62c6ae04
HM
1931 TRB_TYPE(*trb_status));
1932 return -1;
1933 }
1934 if (!(trb_setup->control & TRB_TR_IDT)) {
d6bb65fc 1935 DPRINTF("xhci: Setup TRB doesn't have IDT set\n");
62c6ae04
HM
1936 return -1;
1937 }
1938 if ((trb_setup->status & 0x1ffff) != 8) {
d6bb65fc 1939 DPRINTF("xhci: Setup TRB has bad length (%d)\n",
62c6ae04
HM
1940 (trb_setup->status & 0x1ffff));
1941 return -1;
1942 }
1943
1944 bmRequestType = trb_setup->parameter;
62c6ae04 1945
62c6ae04
HM
1946 xfer->in_xfer = bmRequestType & USB_DIR_IN;
1947 xfer->iso_xfer = false;
4d7a81c0 1948 xfer->timed_xfer = false;
62c6ae04 1949
5c08106f
GH
1950 if (xhci_setup_packet(xfer) < 0) {
1951 return -1;
1952 }
2850ca9e 1953 xfer->packet.parameter = trb_setup->parameter;
2850ca9e 1954
9a77a0f5 1955 usb_handle_packet(xfer->packet.ep->dev, &xfer->packet);
62c6ae04 1956
9a77a0f5 1957 xhci_complete_packet(xfer);
7c605a23 1958 if (!xfer->running_async && !xfer->running_retry) {
024426ac 1959 xhci_kick_ep(xhci, xfer->slotid, xfer->epid, 0);
62c6ae04
HM
1960 }
1961 return 0;
1962}
1963
4d7a81c0
GH
1964static void xhci_calc_intr_kick(XHCIState *xhci, XHCITransfer *xfer,
1965 XHCIEPContext *epctx, uint64_t mfindex)
1966{
1967 uint64_t asap = ((mfindex + epctx->interval - 1) &
1968 ~(epctx->interval-1));
1969 uint64_t kick = epctx->mfindex_last + epctx->interval;
1970
1971 assert(epctx->interval != 0);
1972 xfer->mfindex_kick = MAX(asap, kick);
1973}
1974
3d139684
GH
1975static void xhci_calc_iso_kick(XHCIState *xhci, XHCITransfer *xfer,
1976 XHCIEPContext *epctx, uint64_t mfindex)
1977{
1978 if (xfer->trbs[0].control & TRB_TR_SIA) {
1979 uint64_t asap = ((mfindex + epctx->interval - 1) &
1980 ~(epctx->interval-1));
1981 if (asap >= epctx->mfindex_last &&
1982 asap <= epctx->mfindex_last + epctx->interval * 4) {
1983 xfer->mfindex_kick = epctx->mfindex_last + epctx->interval;
1984 } else {
1985 xfer->mfindex_kick = asap;
1986 }
1987 } else {
786ad214
GH
1988 xfer->mfindex_kick = ((xfer->trbs[0].control >> TRB_TR_FRAMEID_SHIFT)
1989 & TRB_TR_FRAMEID_MASK) << 3;
3d139684 1990 xfer->mfindex_kick |= mfindex & ~0x3fff;
cc03ff9d 1991 if (xfer->mfindex_kick + 0x100 < mfindex) {
3d139684
GH
1992 xfer->mfindex_kick += 0x4000;
1993 }
1994 }
1995}
1996
4d7a81c0
GH
1997static void xhci_check_intr_iso_kick(XHCIState *xhci, XHCITransfer *xfer,
1998 XHCIEPContext *epctx, uint64_t mfindex)
3d139684
GH
1999{
2000 if (xfer->mfindex_kick > mfindex) {
bc72ad67 2001 timer_mod(epctx->kick_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
3d139684
GH
2002 (xfer->mfindex_kick - mfindex) * 125000);
2003 xfer->running_retry = 1;
2004 } else {
2005 epctx->mfindex_last = xfer->mfindex_kick;
bc72ad67 2006 timer_del(epctx->kick_timer);
3d139684
GH
2007 xfer->running_retry = 0;
2008 }
2009}
2010
2011
62c6ae04
HM
2012static int xhci_submit(XHCIState *xhci, XHCITransfer *xfer, XHCIEPContext *epctx)
2013{
3d139684 2014 uint64_t mfindex;
62c6ae04
HM
2015
2016 DPRINTF("xhci_submit(slotid=%d,epid=%d)\n", xfer->slotid, xfer->epid);
62c6ae04
HM
2017
2018 xfer->in_xfer = epctx->type>>2;
62c6ae04 2019
62c6ae04
HM
2020 switch(epctx->type) {
2021 case ET_INTR_OUT:
2022 case ET_INTR_IN:
4d7a81c0
GH
2023 xfer->pkts = 0;
2024 xfer->iso_xfer = false;
2025 xfer->timed_xfer = true;
2026 mfindex = xhci_mfindex_get(xhci);
2027 xhci_calc_intr_kick(xhci, xfer, epctx, mfindex);
2028 xhci_check_intr_iso_kick(xhci, xfer, epctx, mfindex);
2029 if (xfer->running_retry) {
2030 return -1;
2031 }
2032 break;
62c6ae04
HM
2033 case ET_BULK_OUT:
2034 case ET_BULK_IN:
3d139684
GH
2035 xfer->pkts = 0;
2036 xfer->iso_xfer = false;
4d7a81c0 2037 xfer->timed_xfer = false;
62c6ae04
HM
2038 break;
2039 case ET_ISO_OUT:
2040 case ET_ISO_IN:
3d139684
GH
2041 xfer->pkts = 1;
2042 xfer->iso_xfer = true;
4d7a81c0 2043 xfer->timed_xfer = true;
3d139684
GH
2044 mfindex = xhci_mfindex_get(xhci);
2045 xhci_calc_iso_kick(xhci, xfer, epctx, mfindex);
4d7a81c0 2046 xhci_check_intr_iso_kick(xhci, xfer, epctx, mfindex);
3d139684
GH
2047 if (xfer->running_retry) {
2048 return -1;
2049 }
62c6ae04
HM
2050 break;
2051 default:
4f9cc734 2052 trace_usb_xhci_unimplemented("endpoint type", epctx->type);
62c6ae04
HM
2053 return -1;
2054 }
2055
5c08106f
GH
2056 if (xhci_setup_packet(xfer) < 0) {
2057 return -1;
2058 }
9a77a0f5 2059 usb_handle_packet(xfer->packet.ep->dev, &xfer->packet);
62c6ae04 2060
9a77a0f5 2061 xhci_complete_packet(xfer);
7c605a23 2062 if (!xfer->running_async && !xfer->running_retry) {
024426ac 2063 xhci_kick_ep(xhci, xfer->slotid, xfer->epid, xfer->streamid);
62c6ae04
HM
2064 }
2065 return 0;
2066}
2067
2068static int xhci_fire_transfer(XHCIState *xhci, XHCITransfer *xfer, XHCIEPContext *epctx)
2069{
024426ac 2070 trace_usb_xhci_xfer_start(xfer, xfer->slotid, xfer->epid, xfer->streamid);
331e9406 2071 return xhci_submit(xhci, xfer, epctx);
62c6ae04
HM
2072}
2073
024426ac
GH
2074static void xhci_kick_ep(XHCIState *xhci, unsigned int slotid,
2075 unsigned int epid, unsigned int streamid)
62c6ae04 2076{
024426ac 2077 XHCIStreamContext *stctx;
62c6ae04 2078 XHCIEPContext *epctx;
024426ac 2079 XHCIRing *ring;
36dfe324 2080 USBEndpoint *ep = NULL;
3d139684 2081 uint64_t mfindex;
62c6ae04
HM
2082 int length;
2083 int i;
2084
024426ac 2085 trace_usb_xhci_ep_kick(slotid, epid, streamid);
91062ae0 2086 assert(slotid >= 1 && slotid <= xhci->numslots);
62c6ae04 2087 assert(epid >= 1 && epid <= 31);
62c6ae04
HM
2088
2089 if (!xhci->slots[slotid-1].enabled) {
d6bb65fc 2090 DPRINTF("xhci: xhci_kick_ep for disabled slot %d\n", slotid);
62c6ae04
HM
2091 return;
2092 }
2093 epctx = xhci->slots[slotid-1].eps[epid-1];
2094 if (!epctx) {
d6bb65fc 2095 DPRINTF("xhci: xhci_kick_ep for disabled endpoint %d,%d\n",
62c6ae04
HM
2096 epid, slotid);
2097 return;
2098 }
2099
de9de157
HG
2100 /* If the device has been detached, but the guest has not noticed this
2101 yet the 2 above checks will succeed, but we must NOT continue */
2102 if (!xhci->slots[slotid - 1].uport ||
2103 !xhci->slots[slotid - 1].uport->dev ||
2104 !xhci->slots[slotid - 1].uport->dev->attached) {
2105 return;
2106 }
2107
7c605a23 2108 if (epctx->retry) {
7c605a23 2109 XHCITransfer *xfer = epctx->retry;
7c605a23 2110
97df650b 2111 trace_usb_xhci_xfer_retry(xfer);
7c605a23 2112 assert(xfer->running_retry);
4d7a81c0
GH
2113 if (xfer->timed_xfer) {
2114 /* time to kick the transfer? */
3d139684 2115 mfindex = xhci_mfindex_get(xhci);
4d7a81c0 2116 xhci_check_intr_iso_kick(xhci, xfer, epctx, mfindex);
3d139684
GH
2117 if (xfer->running_retry) {
2118 return;
2119 }
4d7a81c0
GH
2120 xfer->timed_xfer = 0;
2121 xfer->running_retry = 1;
2122 }
2123 if (xfer->iso_xfer) {
2124 /* retry iso transfer */
3d139684
GH
2125 if (xhci_setup_packet(xfer) < 0) {
2126 return;
2127 }
9a77a0f5
HG
2128 usb_handle_packet(xfer->packet.ep->dev, &xfer->packet);
2129 assert(xfer->packet.status != USB_RET_NAK);
2130 xhci_complete_packet(xfer);
3d139684
GH
2131 } else {
2132 /* retry nak'ed transfer */
2133 if (xhci_setup_packet(xfer) < 0) {
2134 return;
2135 }
9a77a0f5
HG
2136 usb_handle_packet(xfer->packet.ep->dev, &xfer->packet);
2137 if (xfer->packet.status == USB_RET_NAK) {
3d139684
GH
2138 return;
2139 }
9a77a0f5 2140 xhci_complete_packet(xfer);
7c605a23 2141 }
7c605a23
GH
2142 assert(!xfer->running_retry);
2143 epctx->retry = NULL;
2144 }
2145
62c6ae04
HM
2146 if (epctx->state == EP_HALTED) {
2147 DPRINTF("xhci: ep halted, not running schedule\n");
2148 return;
2149 }
2150
024426ac
GH
2151
2152 if (epctx->nr_pstreams) {
2153 uint32_t err;
2154 stctx = xhci_find_stream(epctx, streamid, &err);
2155 if (stctx == NULL) {
2156 return;
2157 }
2158 ring = &stctx->ring;
2159 xhci_set_ep_state(xhci, epctx, stctx, EP_RUNNING);
2160 } else {
2161 ring = &epctx->ring;
2162 streamid = 0;
2163 xhci_set_ep_state(xhci, epctx, NULL, EP_RUNNING);
2164 }
7d04c2b7 2165 assert(ring->dequeue != 0);
62c6ae04
HM
2166
2167 while (1) {
2168 XHCITransfer *xfer = &epctx->transfers[epctx->next_xfer];
331e9406 2169 if (xfer->running_async || xfer->running_retry) {
62c6ae04
HM
2170 break;
2171 }
024426ac 2172 length = xhci_ring_chain_length(xhci, ring);
62c6ae04 2173 if (length < 0) {
62c6ae04
HM
2174 break;
2175 } else if (length == 0) {
2176 break;
2177 }
62c6ae04
HM
2178 if (xfer->trbs && xfer->trb_alloced < length) {
2179 xfer->trb_count = 0;
2180 xfer->trb_alloced = 0;
2181 g_free(xfer->trbs);
2182 xfer->trbs = NULL;
2183 }
2184 if (!xfer->trbs) {
2185 xfer->trbs = g_malloc(sizeof(XHCITRB) * length);
2186 xfer->trb_alloced = length;
2187 }
2188 xfer->trb_count = length;
2189
2190 for (i = 0; i < length; i++) {
024426ac 2191 assert(xhci_ring_fetch(xhci, ring, &xfer->trbs[i], NULL));
62c6ae04 2192 }
024426ac 2193 xfer->streamid = streamid;
62c6ae04
HM
2194
2195 if (epid == 1) {
2196 if (xhci_fire_ctl_transfer(xhci, xfer) >= 0) {
2197 epctx->next_xfer = (epctx->next_xfer + 1) % TD_QUEUE;
36dfe324 2198 ep = xfer->packet.ep;
62c6ae04 2199 } else {
d6bb65fc 2200 DPRINTF("xhci: error firing CTL transfer\n");
62c6ae04
HM
2201 }
2202 } else {
2203 if (xhci_fire_transfer(xhci, xfer, epctx) >= 0) {
2204 epctx->next_xfer = (epctx->next_xfer + 1) % TD_QUEUE;
2205 } else {
4d7a81c0 2206 if (!xfer->timed_xfer) {
d6bb65fc 2207 DPRINTF("xhci: error firing data transfer\n");
3d139684 2208 }
62c6ae04
HM
2209 }
2210 }
2211
3c4866e0 2212 if (epctx->state == EP_HALTED) {
3c4866e0
GH
2213 break;
2214 }
7c605a23
GH
2215 if (xfer->running_retry) {
2216 DPRINTF("xhci: xfer nacked, stopping schedule\n");
2217 epctx->retry = xfer;
2218 break;
2219 }
62c6ae04 2220 }
518ad5f2
HG
2221
2222 ep = xhci_epid_to_usbep(xhci, slotid, epid);
36dfe324
HG
2223 if (ep) {
2224 usb_device_flush_ep_queue(ep->dev, ep);
2225 }
62c6ae04
HM
2226}
2227
2228static TRBCCode xhci_enable_slot(XHCIState *xhci, unsigned int slotid)
2229{
348f1037 2230 trace_usb_xhci_slot_enable(slotid);
91062ae0 2231 assert(slotid >= 1 && slotid <= xhci->numslots);
62c6ae04 2232 xhci->slots[slotid-1].enabled = 1;
ccaf87a0 2233 xhci->slots[slotid-1].uport = NULL;
62c6ae04
HM
2234 memset(xhci->slots[slotid-1].eps, 0, sizeof(XHCIEPContext*)*31);
2235
2236 return CC_SUCCESS;
2237}
2238
2239static TRBCCode xhci_disable_slot(XHCIState *xhci, unsigned int slotid)
2240{
2241 int i;
2242
348f1037 2243 trace_usb_xhci_slot_disable(slotid);
91062ae0 2244 assert(slotid >= 1 && slotid <= xhci->numslots);
62c6ae04
HM
2245
2246 for (i = 1; i <= 31; i++) {
2247 if (xhci->slots[slotid-1].eps[i-1]) {
2248 xhci_disable_ep(xhci, slotid, i);
2249 }
2250 }
2251
2252 xhci->slots[slotid-1].enabled = 0;
4034e693 2253 xhci->slots[slotid-1].addressed = 0;
5c67dd7b 2254 xhci->slots[slotid-1].uport = NULL;
62c6ae04
HM
2255 return CC_SUCCESS;
2256}
2257
ccaf87a0
GH
2258static USBPort *xhci_lookup_uport(XHCIState *xhci, uint32_t *slot_ctx)
2259{
2260 USBPort *uport;
2261 char path[32];
2262 int i, pos, port;
2263
2264 port = (slot_ctx[1]>>16) & 0xFF;
f2ad97ff
GH
2265 if (port < 1 || port > xhci->numports) {
2266 return NULL;
2267 }
ccaf87a0
GH
2268 port = xhci->ports[port-1].uport->index+1;
2269 pos = snprintf(path, sizeof(path), "%d", port);
2270 for (i = 0; i < 5; i++) {
2271 port = (slot_ctx[0] >> 4*i) & 0x0f;
2272 if (!port) {
2273 break;
2274 }
2275 pos += snprintf(path + pos, sizeof(path) - pos, ".%d", port);
2276 }
2277
2278 QTAILQ_FOREACH(uport, &xhci->bus.used, next) {
2279 if (strcmp(uport->path, path) == 0) {
2280 return uport;
2281 }
2282 }
2283 return NULL;
2284}
2285
62c6ae04
HM
2286static TRBCCode xhci_address_slot(XHCIState *xhci, unsigned int slotid,
2287 uint64_t pictx, bool bsr)
2288{
2289 XHCISlot *slot;
ccaf87a0 2290 USBPort *uport;
62c6ae04 2291 USBDevice *dev;
59a70ccd 2292 dma_addr_t ictx, octx, dcbaap;
62c6ae04
HM
2293 uint64_t poctx;
2294 uint32_t ictl_ctx[2];
2295 uint32_t slot_ctx[4];
2296 uint32_t ep0_ctx[5];
62c6ae04
HM
2297 int i;
2298 TRBCCode res;
2299
91062ae0 2300 assert(slotid >= 1 && slotid <= xhci->numslots);
62c6ae04
HM
2301
2302 dcbaap = xhci_addr64(xhci->dcbaap_low, xhci->dcbaap_high);
9b7d3334 2303 poctx = ldq_le_pci_dma(PCI_DEVICE(xhci), dcbaap + 8 * slotid);
62c6ae04 2304 ictx = xhci_mask64(pictx);
616b5d53 2305 octx = xhci_mask64(poctx);
62c6ae04 2306
59a70ccd
DG
2307 DPRINTF("xhci: input context at "DMA_ADDR_FMT"\n", ictx);
2308 DPRINTF("xhci: output context at "DMA_ADDR_FMT"\n", octx);
62c6ae04 2309
616b5d53 2310 xhci_dma_read_u32s(xhci, ictx, ictl_ctx, sizeof(ictl_ctx));
62c6ae04
HM
2311
2312 if (ictl_ctx[0] != 0x0 || ictl_ctx[1] != 0x3) {
d6bb65fc 2313 DPRINTF("xhci: invalid input context control %08x %08x\n",
62c6ae04
HM
2314 ictl_ctx[0], ictl_ctx[1]);
2315 return CC_TRB_ERROR;
2316 }
2317
616b5d53
DG
2318 xhci_dma_read_u32s(xhci, ictx+32, slot_ctx, sizeof(slot_ctx));
2319 xhci_dma_read_u32s(xhci, ictx+64, ep0_ctx, sizeof(ep0_ctx));
62c6ae04
HM
2320
2321 DPRINTF("xhci: input slot context: %08x %08x %08x %08x\n",
2322 slot_ctx[0], slot_ctx[1], slot_ctx[2], slot_ctx[3]);
2323
2324 DPRINTF("xhci: input ep0 context: %08x %08x %08x %08x %08x\n",
2325 ep0_ctx[0], ep0_ctx[1], ep0_ctx[2], ep0_ctx[3], ep0_ctx[4]);
2326
ccaf87a0
GH
2327 uport = xhci_lookup_uport(xhci, slot_ctx);
2328 if (uport == NULL) {
d6bb65fc 2329 DPRINTF("xhci: port not found\n");
62c6ae04 2330 return CC_TRB_ERROR;
ccaf87a0 2331 }
65d81ed4 2332 trace_usb_xhci_slot_address(slotid, uport->path);
ccaf87a0
GH
2333
2334 dev = uport->dev;
de9de157 2335 if (!dev || !dev->attached) {
d6bb65fc 2336 DPRINTF("xhci: port %s not connected\n", uport->path);
62c6ae04
HM
2337 return CC_USB_TRANSACTION_ERROR;
2338 }
2339
91062ae0 2340 for (i = 0; i < xhci->numslots; i++) {
0bc85da6
GH
2341 if (i == slotid-1) {
2342 continue;
2343 }
ccaf87a0 2344 if (xhci->slots[i].uport == uport) {
d6bb65fc 2345 DPRINTF("xhci: port %s already assigned to slot %d\n",
ccaf87a0 2346 uport->path, i+1);
62c6ae04
HM
2347 return CC_TRB_ERROR;
2348 }
2349 }
2350
2351 slot = &xhci->slots[slotid-1];
ccaf87a0 2352 slot->uport = uport;
62c6ae04
HM
2353 slot->ctx = octx;
2354
2355 if (bsr) {
2356 slot_ctx[3] = SLOT_DEFAULT << SLOT_STATE_SHIFT;
2357 } else {
a820b575 2358 USBPacket p;
a6718874
GH
2359 uint8_t buf[1];
2360
af203be3 2361 slot_ctx[3] = (SLOT_ADDRESSED << SLOT_STATE_SHIFT) | slotid;
0bc85da6 2362 usb_device_reset(dev);
a6718874
GH
2363 memset(&p, 0, sizeof(p));
2364 usb_packet_addbuf(&p, buf, sizeof(buf));
a820b575 2365 usb_packet_setup(&p, USB_TOKEN_OUT,
8550a02d 2366 usb_ep_get(dev, USB_TOKEN_OUT, 0), 0,
a820b575
GH
2367 0, false, false);
2368 usb_device_handle_control(dev, &p,
62c6ae04 2369 DeviceOutRequest | USB_REQ_SET_ADDRESS,
af203be3 2370 slotid, 0, 0, NULL);
a820b575 2371 assert(p.status != USB_RET_ASYNC);
62c6ae04
HM
2372 }
2373
2374 res = xhci_enable_ep(xhci, slotid, 1, octx+32, ep0_ctx);
2375
2376 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
2377 slot_ctx[0], slot_ctx[1], slot_ctx[2], slot_ctx[3]);
2378 DPRINTF("xhci: output ep0 context: %08x %08x %08x %08x %08x\n",
2379 ep0_ctx[0], ep0_ctx[1], ep0_ctx[2], ep0_ctx[3], ep0_ctx[4]);
2380
616b5d53
DG
2381 xhci_dma_write_u32s(xhci, octx, slot_ctx, sizeof(slot_ctx));
2382 xhci_dma_write_u32s(xhci, octx+32, ep0_ctx, sizeof(ep0_ctx));
62c6ae04 2383
4034e693 2384 xhci->slots[slotid-1].addressed = 1;
62c6ae04
HM
2385 return res;
2386}
2387
2388
2389static TRBCCode xhci_configure_slot(XHCIState *xhci, unsigned int slotid,
2390 uint64_t pictx, bool dc)
2391{
59a70ccd 2392 dma_addr_t ictx, octx;
62c6ae04
HM
2393 uint32_t ictl_ctx[2];
2394 uint32_t slot_ctx[4];
2395 uint32_t islot_ctx[4];
2396 uint32_t ep_ctx[5];
2397 int i;
2398 TRBCCode res;
2399
348f1037 2400 trace_usb_xhci_slot_configure(slotid);
91062ae0 2401 assert(slotid >= 1 && slotid <= xhci->numslots);
62c6ae04
HM
2402
2403 ictx = xhci_mask64(pictx);
2404 octx = xhci->slots[slotid-1].ctx;
2405
59a70ccd
DG
2406 DPRINTF("xhci: input context at "DMA_ADDR_FMT"\n", ictx);
2407 DPRINTF("xhci: output context at "DMA_ADDR_FMT"\n", octx);
62c6ae04
HM
2408
2409 if (dc) {
2410 for (i = 2; i <= 31; i++) {
2411 if (xhci->slots[slotid-1].eps[i-1]) {
2412 xhci_disable_ep(xhci, slotid, i);
2413 }
2414 }
2415
616b5d53 2416 xhci_dma_read_u32s(xhci, octx, slot_ctx, sizeof(slot_ctx));
62c6ae04
HM
2417 slot_ctx[3] &= ~(SLOT_STATE_MASK << SLOT_STATE_SHIFT);
2418 slot_ctx[3] |= SLOT_ADDRESSED << SLOT_STATE_SHIFT;
2419 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
2420 slot_ctx[0], slot_ctx[1], slot_ctx[2], slot_ctx[3]);
616b5d53 2421 xhci_dma_write_u32s(xhci, octx, slot_ctx, sizeof(slot_ctx));
62c6ae04
HM
2422
2423 return CC_SUCCESS;
2424 }
2425
616b5d53 2426 xhci_dma_read_u32s(xhci, ictx, ictl_ctx, sizeof(ictl_ctx));
62c6ae04
HM
2427
2428 if ((ictl_ctx[0] & 0x3) != 0x0 || (ictl_ctx[1] & 0x3) != 0x1) {
d6bb65fc 2429 DPRINTF("xhci: invalid input context control %08x %08x\n",
62c6ae04
HM
2430 ictl_ctx[0], ictl_ctx[1]);
2431 return CC_TRB_ERROR;
2432 }
2433
616b5d53
DG
2434 xhci_dma_read_u32s(xhci, ictx+32, islot_ctx, sizeof(islot_ctx));
2435 xhci_dma_read_u32s(xhci, octx, slot_ctx, sizeof(slot_ctx));
62c6ae04
HM
2436
2437 if (SLOT_STATE(slot_ctx[3]) < SLOT_ADDRESSED) {
d6bb65fc 2438 DPRINTF("xhci: invalid slot state %08x\n", slot_ctx[3]);
62c6ae04
HM
2439 return CC_CONTEXT_STATE_ERROR;
2440 }
2441
72391da5
HG
2442 xhci_free_device_streams(xhci, slotid, ictl_ctx[0] | ictl_ctx[1]);
2443
62c6ae04
HM
2444 for (i = 2; i <= 31; i++) {
2445 if (ictl_ctx[0] & (1<<i)) {
2446 xhci_disable_ep(xhci, slotid, i);
2447 }
2448 if (ictl_ctx[1] & (1<<i)) {
616b5d53 2449 xhci_dma_read_u32s(xhci, ictx+32+(32*i), ep_ctx, sizeof(ep_ctx));
62c6ae04
HM
2450 DPRINTF("xhci: input ep%d.%d context: %08x %08x %08x %08x %08x\n",
2451 i/2, i%2, ep_ctx[0], ep_ctx[1], ep_ctx[2],
2452 ep_ctx[3], ep_ctx[4]);
2453 xhci_disable_ep(xhci, slotid, i);
2454 res = xhci_enable_ep(xhci, slotid, i, octx+(32*i), ep_ctx);
2455 if (res != CC_SUCCESS) {
2456 return res;
2457 }
2458 DPRINTF("xhci: output ep%d.%d context: %08x %08x %08x %08x %08x\n",
2459 i/2, i%2, ep_ctx[0], ep_ctx[1], ep_ctx[2],
2460 ep_ctx[3], ep_ctx[4]);
616b5d53 2461 xhci_dma_write_u32s(xhci, octx+(32*i), ep_ctx, sizeof(ep_ctx));
62c6ae04
HM
2462 }
2463 }
2464
72391da5
HG
2465 res = xhci_alloc_device_streams(xhci, slotid, ictl_ctx[1]);
2466 if (res != CC_SUCCESS) {
2467 for (i = 2; i <= 31; i++) {
3d80365b 2468 if (ictl_ctx[1] & (1u << i)) {
72391da5
HG
2469 xhci_disable_ep(xhci, slotid, i);
2470 }
2471 }
2472 return res;
2473 }
2474
62c6ae04
HM
2475 slot_ctx[3] &= ~(SLOT_STATE_MASK << SLOT_STATE_SHIFT);
2476 slot_ctx[3] |= SLOT_CONFIGURED << SLOT_STATE_SHIFT;
2477 slot_ctx[0] &= ~(SLOT_CONTEXT_ENTRIES_MASK << SLOT_CONTEXT_ENTRIES_SHIFT);
2478 slot_ctx[0] |= islot_ctx[0] & (SLOT_CONTEXT_ENTRIES_MASK <<
2479 SLOT_CONTEXT_ENTRIES_SHIFT);
2480 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
2481 slot_ctx[0], slot_ctx[1], slot_ctx[2], slot_ctx[3]);
2482
616b5d53 2483 xhci_dma_write_u32s(xhci, octx, slot_ctx, sizeof(slot_ctx));
62c6ae04
HM
2484
2485 return CC_SUCCESS;
2486}
2487
2488
2489static TRBCCode xhci_evaluate_slot(XHCIState *xhci, unsigned int slotid,
2490 uint64_t pictx)
2491{
59a70ccd 2492 dma_addr_t ictx, octx;
62c6ae04
HM
2493 uint32_t ictl_ctx[2];
2494 uint32_t iep0_ctx[5];
2495 uint32_t ep0_ctx[5];
2496 uint32_t islot_ctx[4];
2497 uint32_t slot_ctx[4];
2498
348f1037 2499 trace_usb_xhci_slot_evaluate(slotid);
91062ae0 2500 assert(slotid >= 1 && slotid <= xhci->numslots);
62c6ae04
HM
2501
2502 ictx = xhci_mask64(pictx);
2503 octx = xhci->slots[slotid-1].ctx;
2504
59a70ccd
DG
2505 DPRINTF("xhci: input context at "DMA_ADDR_FMT"\n", ictx);
2506 DPRINTF("xhci: output context at "DMA_ADDR_FMT"\n", octx);
62c6ae04 2507
616b5d53 2508 xhci_dma_read_u32s(xhci, ictx, ictl_ctx, sizeof(ictl_ctx));
62c6ae04
HM
2509
2510 if (ictl_ctx[0] != 0x0 || ictl_ctx[1] & ~0x3) {
d6bb65fc 2511 DPRINTF("xhci: invalid input context control %08x %08x\n",
62c6ae04
HM
2512 ictl_ctx[0], ictl_ctx[1]);
2513 return CC_TRB_ERROR;
2514 }
2515
2516 if (ictl_ctx[1] & 0x1) {
616b5d53 2517 xhci_dma_read_u32s(xhci, ictx+32, islot_ctx, sizeof(islot_ctx));
62c6ae04
HM
2518
2519 DPRINTF("xhci: input slot context: %08x %08x %08x %08x\n",
2520 islot_ctx[0], islot_ctx[1], islot_ctx[2], islot_ctx[3]);
2521
616b5d53 2522 xhci_dma_read_u32s(xhci, octx, slot_ctx, sizeof(slot_ctx));
62c6ae04
HM
2523
2524 slot_ctx[1] &= ~0xFFFF; /* max exit latency */
2525 slot_ctx[1] |= islot_ctx[1] & 0xFFFF;
2526 slot_ctx[2] &= ~0xFF00000; /* interrupter target */
2527 slot_ctx[2] |= islot_ctx[2] & 0xFF000000;
2528
2529 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
2530 slot_ctx[0], slot_ctx[1], slot_ctx[2], slot_ctx[3]);
2531
616b5d53 2532 xhci_dma_write_u32s(xhci, octx, slot_ctx, sizeof(slot_ctx));
62c6ae04
HM
2533 }
2534
2535 if (ictl_ctx[1] & 0x2) {
616b5d53 2536 xhci_dma_read_u32s(xhci, ictx+64, iep0_ctx, sizeof(iep0_ctx));
62c6ae04
HM
2537
2538 DPRINTF("xhci: input ep0 context: %08x %08x %08x %08x %08x\n",
2539 iep0_ctx[0], iep0_ctx[1], iep0_ctx[2],
2540 iep0_ctx[3], iep0_ctx[4]);
2541
616b5d53 2542 xhci_dma_read_u32s(xhci, octx+32, ep0_ctx, sizeof(ep0_ctx));
62c6ae04
HM
2543
2544 ep0_ctx[1] &= ~0xFFFF0000; /* max packet size*/
2545 ep0_ctx[1] |= iep0_ctx[1] & 0xFFFF0000;
2546
2547 DPRINTF("xhci: output ep0 context: %08x %08x %08x %08x %08x\n",
2548 ep0_ctx[0], ep0_ctx[1], ep0_ctx[2], ep0_ctx[3], ep0_ctx[4]);
2549
616b5d53 2550 xhci_dma_write_u32s(xhci, octx+32, ep0_ctx, sizeof(ep0_ctx));
62c6ae04
HM
2551 }
2552
2553 return CC_SUCCESS;
2554}
2555
2556static TRBCCode xhci_reset_slot(XHCIState *xhci, unsigned int slotid)
2557{
2558 uint32_t slot_ctx[4];
59a70ccd 2559 dma_addr_t octx;
62c6ae04
HM
2560 int i;
2561
348f1037 2562 trace_usb_xhci_slot_reset(slotid);
91062ae0 2563 assert(slotid >= 1 && slotid <= xhci->numslots);
62c6ae04
HM
2564
2565 octx = xhci->slots[slotid-1].ctx;
2566
59a70ccd 2567 DPRINTF("xhci: output context at "DMA_ADDR_FMT"\n", octx);
62c6ae04
HM
2568
2569 for (i = 2; i <= 31; i++) {
2570 if (xhci->slots[slotid-1].eps[i-1]) {
2571 xhci_disable_ep(xhci, slotid, i);
2572 }
2573 }
2574
616b5d53 2575 xhci_dma_read_u32s(xhci, octx, slot_ctx, sizeof(slot_ctx));
62c6ae04
HM
2576 slot_ctx[3] &= ~(SLOT_STATE_MASK << SLOT_STATE_SHIFT);
2577 slot_ctx[3] |= SLOT_DEFAULT << SLOT_STATE_SHIFT;
2578 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
2579 slot_ctx[0], slot_ctx[1], slot_ctx[2], slot_ctx[3]);
616b5d53 2580 xhci_dma_write_u32s(xhci, octx, slot_ctx, sizeof(slot_ctx));
62c6ae04
HM
2581
2582 return CC_SUCCESS;
2583}
2584
2585static unsigned int xhci_get_slot(XHCIState *xhci, XHCIEvent *event, XHCITRB *trb)
2586{
2587 unsigned int slotid;
2588 slotid = (trb->control >> TRB_CR_SLOTID_SHIFT) & TRB_CR_SLOTID_MASK;
91062ae0 2589 if (slotid < 1 || slotid > xhci->numslots) {
d6bb65fc 2590 DPRINTF("xhci: bad slot id %d\n", slotid);
62c6ae04
HM
2591 event->ccode = CC_TRB_ERROR;
2592 return 0;
2593 } else if (!xhci->slots[slotid-1].enabled) {
d6bb65fc 2594 DPRINTF("xhci: slot id %d not enabled\n", slotid);
62c6ae04
HM
2595 event->ccode = CC_SLOT_NOT_ENABLED_ERROR;
2596 return 0;
2597 }
2598 return slotid;
2599}
2600
81251841
GH
2601/* cleanup slot state on usb device detach */
2602static void xhci_detach_slot(XHCIState *xhci, USBPort *uport)
2603{
0cb41e2c 2604 int slot, ep;
81251841
GH
2605
2606 for (slot = 0; slot < xhci->numslots; slot++) {
2607 if (xhci->slots[slot].uport == uport) {
2608 break;
2609 }
2610 }
2611 if (slot == xhci->numslots) {
2612 return;
2613 }
2614
0cb41e2c
GH
2615 for (ep = 0; ep < 31; ep++) {
2616 if (xhci->slots[slot].eps[ep]) {
582d6f4a 2617 xhci_ep_nuke_xfers(xhci, slot + 1, ep + 1, 0);
0cb41e2c
GH
2618 }
2619 }
81251841
GH
2620 xhci->slots[slot].uport = NULL;
2621}
2622
62c6ae04
HM
2623static TRBCCode xhci_get_port_bandwidth(XHCIState *xhci, uint64_t pctx)
2624{
59a70ccd 2625 dma_addr_t ctx;
0846e635 2626 uint8_t bw_ctx[xhci->numports+1];
62c6ae04
HM
2627
2628 DPRINTF("xhci_get_port_bandwidth()\n");
2629
2630 ctx = xhci_mask64(pctx);
2631
59a70ccd 2632 DPRINTF("xhci: bandwidth context at "DMA_ADDR_FMT"\n", ctx);
62c6ae04
HM
2633
2634 /* TODO: actually implement real values here */
2635 bw_ctx[0] = 0;
0846e635 2636 memset(&bw_ctx[1], 80, xhci->numports); /* 80% */
9b7d3334 2637 pci_dma_write(PCI_DEVICE(xhci), ctx, bw_ctx, sizeof(bw_ctx));
62c6ae04
HM
2638
2639 return CC_SUCCESS;
2640}
2641
2642static uint32_t rotl(uint32_t v, unsigned count)
2643{
2644 count &= 31;
2645 return (v << count) | (v >> (32 - count));
2646}
2647
2648
2649static uint32_t xhci_nec_challenge(uint32_t hi, uint32_t lo)
2650{
2651 uint32_t val;
2652 val = rotl(lo - 0x49434878, 32 - ((hi>>8) & 0x1F));
2653 val += rotl(lo + 0x49434878, hi & 0x1F);
2654 val -= rotl(hi ^ 0x49434878, (lo >> 16) & 0x1F);
2655 return ~val;
2656}
2657
59a70ccd 2658static void xhci_via_challenge(XHCIState *xhci, uint64_t addr)
62c6ae04 2659{
9b7d3334 2660 PCIDevice *pci_dev = PCI_DEVICE(xhci);
62c6ae04
HM
2661 uint32_t buf[8];
2662 uint32_t obuf[8];
59a70ccd 2663 dma_addr_t paddr = xhci_mask64(addr);
62c6ae04 2664
9b7d3334 2665 pci_dma_read(pci_dev, paddr, &buf, 32);
62c6ae04
HM
2666
2667 memcpy(obuf, buf, sizeof(obuf));
2668
2669 if ((buf[0] & 0xff) == 2) {
2670 obuf[0] = 0x49932000 + 0x54dc200 * buf[2] + 0x7429b578 * buf[3];
2671 obuf[0] |= (buf[2] * buf[3]) & 0xff;
2672 obuf[1] = 0x0132bb37 + 0xe89 * buf[2] + 0xf09 * buf[3];
2673 obuf[2] = 0x0066c2e9 + 0x2091 * buf[2] + 0x19bd * buf[3];
2674 obuf[3] = 0xd5281342 + 0x2cc9691 * buf[2] + 0x2367662 * buf[3];
2675 obuf[4] = 0x0123c75c + 0x1595 * buf[2] + 0x19ec * buf[3];
2676 obuf[5] = 0x00f695de + 0x26fd * buf[2] + 0x3e9 * buf[3];
2677 obuf[6] = obuf[2] ^ obuf[3] ^ 0x29472956;
2678 obuf[7] = obuf[2] ^ obuf[3] ^ 0x65866593;
2679 }
2680
9b7d3334 2681 pci_dma_write(pci_dev, paddr, &obuf, 32);
62c6ae04
HM
2682}
2683
2684static void xhci_process_commands(XHCIState *xhci)
2685{
2686 XHCITRB trb;
2687 TRBType type;
2688 XHCIEvent event = {ER_COMMAND_COMPLETE, CC_SUCCESS};
59a70ccd 2689 dma_addr_t addr;
62c6ae04
HM
2690 unsigned int i, slotid = 0;
2691
2692 DPRINTF("xhci_process_commands()\n");
2693 if (!xhci_running(xhci)) {
2694 DPRINTF("xhci_process_commands() called while xHC stopped or paused\n");
2695 return;
2696 }
2697
2698 xhci->crcr_low |= CRCR_CRR;
2699
2700 while ((type = xhci_ring_fetch(xhci, &xhci->cmd_ring, &trb, &addr))) {
2701 event.ptr = addr;
2702 switch (type) {
2703 case CR_ENABLE_SLOT:
91062ae0 2704 for (i = 0; i < xhci->numslots; i++) {
62c6ae04
HM
2705 if (!xhci->slots[i].enabled) {
2706 break;
2707 }
2708 }
91062ae0 2709 if (i >= xhci->numslots) {
d6bb65fc 2710 DPRINTF("xhci: no device slots available\n");
62c6ae04
HM
2711 event.ccode = CC_NO_SLOTS_ERROR;
2712 } else {
2713 slotid = i+1;
2714 event.ccode = xhci_enable_slot(xhci, slotid);
2715 }
2716 break;
2717 case CR_DISABLE_SLOT:
2718 slotid = xhci_get_slot(xhci, &event, &trb);
2719 if (slotid) {
2720 event.ccode = xhci_disable_slot(xhci, slotid);
2721 }
2722 break;
2723 case CR_ADDRESS_DEVICE:
2724 slotid = xhci_get_slot(xhci, &event, &trb);
2725 if (slotid) {
2726 event.ccode = xhci_address_slot(xhci, slotid, trb.parameter,
2727 trb.control & TRB_CR_BSR);
2728 }
2729 break;
2730 case CR_CONFIGURE_ENDPOINT:
2731 slotid = xhci_get_slot(xhci, &event, &trb);
2732 if (slotid) {
2733 event.ccode = xhci_configure_slot(xhci, slotid, trb.parameter,
2734 trb.control & TRB_CR_DC);
2735 }
2736 break;
2737 case CR_EVALUATE_CONTEXT:
2738 slotid = xhci_get_slot(xhci, &event, &trb);
2739 if (slotid) {
2740 event.ccode = xhci_evaluate_slot(xhci, slotid, trb.parameter);
2741 }
2742 break;
2743 case CR_STOP_ENDPOINT:
2744 slotid = xhci_get_slot(xhci, &event, &trb);
2745 if (slotid) {
2746 unsigned int epid = (trb.control >> TRB_CR_EPID_SHIFT)
2747 & TRB_CR_EPID_MASK;
2748 event.ccode = xhci_stop_ep(xhci, slotid, epid);
2749 }
2750 break;
2751 case CR_RESET_ENDPOINT:
2752 slotid = xhci_get_slot(xhci, &event, &trb);
2753 if (slotid) {
2754 unsigned int epid = (trb.control >> TRB_CR_EPID_SHIFT)
2755 & TRB_CR_EPID_MASK;
2756 event.ccode = xhci_reset_ep(xhci, slotid, epid);
2757 }
2758 break;
2759 case CR_SET_TR_DEQUEUE:
2760 slotid = xhci_get_slot(xhci, &event, &trb);
2761 if (slotid) {
2762 unsigned int epid = (trb.control >> TRB_CR_EPID_SHIFT)
2763 & TRB_CR_EPID_MASK;
024426ac
GH
2764 unsigned int streamid = (trb.status >> 16) & 0xffff;
2765 event.ccode = xhci_set_ep_dequeue(xhci, slotid,
2766 epid, streamid,
62c6ae04
HM
2767 trb.parameter);
2768 }
2769 break;
2770 case CR_RESET_DEVICE:
2771 slotid = xhci_get_slot(xhci, &event, &trb);
2772 if (slotid) {
2773 event.ccode = xhci_reset_slot(xhci, slotid);
2774 }
2775 break;
2776 case CR_GET_PORT_BANDWIDTH:
2777 event.ccode = xhci_get_port_bandwidth(xhci, trb.parameter);
2778 break;
2779 case CR_VENDOR_VIA_CHALLENGE_RESPONSE:
59a70ccd 2780 xhci_via_challenge(xhci, trb.parameter);
62c6ae04
HM
2781 break;
2782 case CR_VENDOR_NEC_FIRMWARE_REVISION:
2783 event.type = 48; /* NEC reply */
2784 event.length = 0x3025;
2785 break;
2786 case CR_VENDOR_NEC_CHALLENGE_RESPONSE:
2787 {
2788 uint32_t chi = trb.parameter >> 32;
2789 uint32_t clo = trb.parameter;
2790 uint32_t val = xhci_nec_challenge(chi, clo);
2791 event.length = val & 0xFFFF;
2792 event.epid = val >> 16;
2793 slotid = val >> 24;
2794 event.type = 48; /* NEC reply */
2795 }
2796 break;
2797 default:
0ab966cf 2798 trace_usb_xhci_unimplemented("command", type);
62c6ae04
HM
2799 event.ccode = CC_TRB_ERROR;
2800 break;
2801 }
2802 event.slotid = slotid;
2d1de850 2803 xhci_event(xhci, &event, 0);
62c6ae04
HM
2804 }
2805}
2806
6a32f80f
GH
2807static bool xhci_port_have_device(XHCIPort *port)
2808{
2809 if (!port->uport->dev || !port->uport->dev->attached) {
2810 return false; /* no device present */
2811 }
2812 if (!((1 << port->uport->dev->speed) & port->speedmask)) {
2813 return false; /* speed mismatch */
2814 }
2815 return true;
2816}
2817
f705a362
GH
2818static void xhci_port_notify(XHCIPort *port, uint32_t bits)
2819{
2820 XHCIEvent ev = { ER_PORT_STATUS_CHANGE, CC_SUCCESS,
2821 port->portnr << 24 };
2822
2823 if ((port->portsc & bits) == bits) {
2824 return;
2825 }
bdfce20d 2826 trace_usb_xhci_port_notify(port->portnr, bits);
f705a362
GH
2827 port->portsc |= bits;
2828 if (!xhci_running(port->xhci)) {
2829 return;
2830 }
2831 xhci_event(port->xhci, &ev, 0);
2832}
2833
f3214027 2834static void xhci_port_update(XHCIPort *port, int is_detach)
62c6ae04 2835{
b62b0828
GH
2836 uint32_t pls = PLS_RX_DETECT;
2837
62c6ae04 2838 port->portsc = PORTSC_PP;
6a32f80f 2839 if (!is_detach && xhci_port_have_device(port)) {
62c6ae04 2840 port->portsc |= PORTSC_CCS;
0846e635 2841 switch (port->uport->dev->speed) {
62c6ae04
HM
2842 case USB_SPEED_LOW:
2843 port->portsc |= PORTSC_SPEED_LOW;
b62b0828 2844 pls = PLS_POLLING;
62c6ae04
HM
2845 break;
2846 case USB_SPEED_FULL:
2847 port->portsc |= PORTSC_SPEED_FULL;
b62b0828 2848 pls = PLS_POLLING;
62c6ae04
HM
2849 break;
2850 case USB_SPEED_HIGH:
2851 port->portsc |= PORTSC_SPEED_HIGH;
b62b0828 2852 pls = PLS_POLLING;
62c6ae04 2853 break;
0846e635
GH
2854 case USB_SPEED_SUPER:
2855 port->portsc |= PORTSC_SPEED_SUPER;
b62b0828
GH
2856 port->portsc |= PORTSC_PED;
2857 pls = PLS_U0;
0846e635 2858 break;
62c6ae04
HM
2859 }
2860 }
b62b0828 2861 set_field(&port->portsc, pls, PORTSC_PLS);
4f47f0f8 2862 trace_usb_xhci_port_link(port->portnr, pls);
f705a362 2863 xhci_port_notify(port, PORTSC_CSC);
62c6ae04
HM
2864}
2865
dad5b9ea 2866static void xhci_port_reset(XHCIPort *port, bool warm_reset)
40030130 2867{
7bd3055f 2868 trace_usb_xhci_port_reset(port->portnr, warm_reset);
4f47f0f8 2869
b62b0828
GH
2870 if (!xhci_port_have_device(port)) {
2871 return;
2872 }
2873
40030130 2874 usb_device_reset(port->uport->dev);
b62b0828
GH
2875
2876 switch (port->uport->dev->speed) {
dad5b9ea
GH
2877 case USB_SPEED_SUPER:
2878 if (warm_reset) {
2879 port->portsc |= PORTSC_WRC;
2880 }
2881 /* fall through */
b62b0828
GH
2882 case USB_SPEED_LOW:
2883 case USB_SPEED_FULL:
2884 case USB_SPEED_HIGH:
2885 set_field(&port->portsc, PLS_U0, PORTSC_PLS);
4f47f0f8 2886 trace_usb_xhci_port_link(port->portnr, PLS_U0);
b62b0828
GH
2887 port->portsc |= PORTSC_PED;
2888 break;
2889 }
2890
2891 port->portsc &= ~PORTSC_PR;
2892 xhci_port_notify(port, PORTSC_PRC);
40030130
GH
2893}
2894
64619739 2895static void xhci_reset(DeviceState *dev)
62c6ae04 2896{
37034575 2897 XHCIState *xhci = XHCI(dev);
62c6ae04
HM
2898 int i;
2899
2d754a10 2900 trace_usb_xhci_reset();
62c6ae04 2901 if (!(xhci->usbsts & USBSTS_HCH)) {
d6bb65fc 2902 DPRINTF("xhci: reset while running!\n");
62c6ae04
HM
2903 }
2904
2905 xhci->usbcmd = 0;
2906 xhci->usbsts = USBSTS_HCH;
2907 xhci->dnctrl = 0;
2908 xhci->crcr_low = 0;
2909 xhci->crcr_high = 0;
2910 xhci->dcbaap_low = 0;
2911 xhci->dcbaap_high = 0;
2912 xhci->config = 0;
62c6ae04 2913
91062ae0 2914 for (i = 0; i < xhci->numslots; i++) {
62c6ae04
HM
2915 xhci_disable_slot(xhci, i+1);
2916 }
2917
0846e635 2918 for (i = 0; i < xhci->numports; i++) {
f3214027 2919 xhci_port_update(xhci->ports + i, 0);
62c6ae04
HM
2920 }
2921
91062ae0 2922 for (i = 0; i < xhci->numintrs; i++) {
962d11e1
GH
2923 xhci->intr[i].iman = 0;
2924 xhci->intr[i].imod = 0;
2925 xhci->intr[i].erstsz = 0;
2926 xhci->intr[i].erstba_low = 0;
2927 xhci->intr[i].erstba_high = 0;
2928 xhci->intr[i].erdp_low = 0;
2929 xhci->intr[i].erdp_high = 0;
2930 xhci->intr[i].msix_used = 0;
62c6ae04 2931
962d11e1
GH
2932 xhci->intr[i].er_ep_idx = 0;
2933 xhci->intr[i].er_pcs = 1;
2934 xhci->intr[i].er_full = 0;
2935 xhci->intr[i].ev_buffer_put = 0;
2936 xhci->intr[i].ev_buffer_get = 0;
2937 }
01546fa6 2938
bc72ad67 2939 xhci->mfindex_start = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
01546fa6 2940 xhci_mfwrap_update(xhci);
62c6ae04
HM
2941}
2942
a8170e5e 2943static uint64_t xhci_cap_read(void *ptr, hwaddr reg, unsigned size)
62c6ae04 2944{
1b067564 2945 XHCIState *xhci = ptr;
2d754a10 2946 uint32_t ret;
62c6ae04
HM
2947
2948 switch (reg) {
2949 case 0x00: /* HCIVERSION, CAPLENGTH */
2d754a10
GH
2950 ret = 0x01000000 | LEN_CAP;
2951 break;
62c6ae04 2952 case 0x04: /* HCSPARAMS 1 */
0846e635 2953 ret = ((xhci->numports_2+xhci->numports_3)<<24)
91062ae0 2954 | (xhci->numintrs<<8) | xhci->numslots;
2d754a10 2955 break;
62c6ae04 2956 case 0x08: /* HCSPARAMS 2 */
2d754a10
GH
2957 ret = 0x0000000f;
2958 break;
62c6ae04 2959 case 0x0c: /* HCSPARAMS 3 */
2d754a10
GH
2960 ret = 0x00000000;
2961 break;
62c6ae04 2962 case 0x10: /* HCCPARAMS */
2d754a10 2963 if (sizeof(dma_addr_t) == 4) {
2aa6bfcb 2964 ret = 0x00080000 | (xhci->max_pstreams_mask << 12);
2d754a10 2965 } else {
2aa6bfcb 2966 ret = 0x00080001 | (xhci->max_pstreams_mask << 12);
2d754a10
GH
2967 }
2968 break;
62c6ae04 2969 case 0x14: /* DBOFF */
2d754a10
GH
2970 ret = OFF_DOORBELL;
2971 break;
62c6ae04 2972 case 0x18: /* RTSOFF */
2d754a10
GH
2973 ret = OFF_RUNTIME;
2974 break;
62c6ae04
HM
2975
2976 /* extended capabilities */
2977 case 0x20: /* Supported Protocol:00 */
2d754a10
GH
2978 ret = 0x02000402; /* USB 2.0 */
2979 break;
62c6ae04 2980 case 0x24: /* Supported Protocol:04 */
0ebfb144 2981 ret = 0x20425355; /* "USB " */
2d754a10 2982 break;
62c6ae04 2983 case 0x28: /* Supported Protocol:08 */
7bafd888
GH
2984 if (xhci_get_flag(xhci, XHCI_FLAG_SS_FIRST)) {
2985 ret = (xhci->numports_2<<8) | (xhci->numports_3+1);
2986 } else {
2987 ret = (xhci->numports_2<<8) | 1;
2988 }
2d754a10 2989 break;
62c6ae04 2990 case 0x2c: /* Supported Protocol:0c */
2d754a10
GH
2991 ret = 0x00000000; /* reserved */
2992 break;
62c6ae04 2993 case 0x30: /* Supported Protocol:00 */
2d754a10
GH
2994 ret = 0x03000002; /* USB 3.0 */
2995 break;
62c6ae04 2996 case 0x34: /* Supported Protocol:04 */
0ebfb144 2997 ret = 0x20425355; /* "USB " */
2d754a10 2998 break;
62c6ae04 2999 case 0x38: /* Supported Protocol:08 */
7bafd888
GH
3000 if (xhci_get_flag(xhci, XHCI_FLAG_SS_FIRST)) {
3001 ret = (xhci->numports_3<<8) | 1;
3002 } else {
3003 ret = (xhci->numports_3<<8) | (xhci->numports_2+1);
3004 }
2d754a10 3005 break;
62c6ae04 3006 case 0x3c: /* Supported Protocol:0c */
2d754a10
GH
3007 ret = 0x00000000; /* reserved */
3008 break;
62c6ae04 3009 default:
0ab966cf 3010 trace_usb_xhci_unimplemented("cap read", reg);
2d754a10 3011 ret = 0;
62c6ae04 3012 }
2d754a10
GH
3013
3014 trace_usb_xhci_cap_read(reg, ret);
3015 return ret;
62c6ae04
HM
3016}
3017
a8170e5e 3018static uint64_t xhci_port_read(void *ptr, hwaddr reg, unsigned size)
62c6ae04 3019{
1d8a4e69 3020 XHCIPort *port = ptr;
2d754a10
GH
3021 uint32_t ret;
3022
1d8a4e69 3023 switch (reg) {
62c6ae04 3024 case 0x00: /* PORTSC */
1d8a4e69 3025 ret = port->portsc;
2d754a10 3026 break;
62c6ae04
HM
3027 case 0x04: /* PORTPMSC */
3028 case 0x08: /* PORTLI */
2d754a10
GH
3029 ret = 0;
3030 break;
62c6ae04
HM
3031 case 0x0c: /* reserved */
3032 default:
0ab966cf 3033 trace_usb_xhci_unimplemented("port read", reg);
2d754a10 3034 ret = 0;
62c6ae04 3035 }
2d754a10 3036
1d8a4e69 3037 trace_usb_xhci_port_read(port->portnr, reg, ret);
2d754a10 3038 return ret;
62c6ae04
HM
3039}
3040
a8170e5e 3041static void xhci_port_write(void *ptr, hwaddr reg,
1d8a4e69 3042 uint64_t val, unsigned size)
62c6ae04 3043{
1d8a4e69 3044 XHCIPort *port = ptr;
bdfce20d 3045 uint32_t portsc, notify;
62c6ae04 3046
1d8a4e69 3047 trace_usb_xhci_port_write(port->portnr, reg, val);
2d754a10 3048
1d8a4e69 3049 switch (reg) {
62c6ae04 3050 case 0x00: /* PORTSC */
bdfce20d 3051 /* write-1-to-start bits */
dad5b9ea
GH
3052 if (val & PORTSC_WPR) {
3053 xhci_port_reset(port, true);
3054 break;
3055 }
bdfce20d 3056 if (val & PORTSC_PR) {
dad5b9ea 3057 xhci_port_reset(port, false);
bdfce20d
GH
3058 break;
3059 }
3060
1d8a4e69 3061 portsc = port->portsc;
bdfce20d 3062 notify = 0;
62c6ae04
HM
3063 /* write-1-to-clear bits*/
3064 portsc &= ~(val & (PORTSC_CSC|PORTSC_PEC|PORTSC_WRC|PORTSC_OCC|
3065 PORTSC_PRC|PORTSC_PLC|PORTSC_CEC));
3066 if (val & PORTSC_LWS) {
3067 /* overwrite PLS only when LWS=1 */
bdfce20d
GH
3068 uint32_t old_pls = get_field(port->portsc, PORTSC_PLS);
3069 uint32_t new_pls = get_field(val, PORTSC_PLS);
3070 switch (new_pls) {
3071 case PLS_U0:
3072 if (old_pls != PLS_U0) {
3073 set_field(&portsc, new_pls, PORTSC_PLS);
3074 trace_usb_xhci_port_link(port->portnr, new_pls);
3075 notify = PORTSC_PLC;
3076 }
3077 break;
3078 case PLS_U3:
3079 if (old_pls < PLS_U3) {
3080 set_field(&portsc, new_pls, PORTSC_PLS);
3081 trace_usb_xhci_port_link(port->portnr, new_pls);
3082 }
3083 break;
3084 case PLS_RESUME:
3085 /* windows does this for some reason, don't spam stderr */
3086 break;
3087 default:
d6bb65fc 3088 DPRINTF("%s: ignore pls write (old %d, new %d)\n",
bdfce20d
GH
3089 __func__, old_pls, new_pls);
3090 break;
3091 }
62c6ae04
HM
3092 }
3093 /* read/write bits */
3094 portsc &= ~(PORTSC_PP|PORTSC_WCE|PORTSC_WDE|PORTSC_WOE);
3095 portsc |= (val & (PORTSC_PP|PORTSC_WCE|PORTSC_WDE|PORTSC_WOE));
40030130 3096 port->portsc = portsc;
bdfce20d
GH
3097 if (notify) {
3098 xhci_port_notify(port, notify);
62c6ae04 3099 }
62c6ae04
HM
3100 break;
3101 case 0x04: /* PORTPMSC */
3102 case 0x08: /* PORTLI */
3103 default:
0ab966cf 3104 trace_usb_xhci_unimplemented("port write", reg);
62c6ae04
HM
3105 }
3106}
3107
a8170e5e 3108static uint64_t xhci_oper_read(void *ptr, hwaddr reg, unsigned size)
62c6ae04 3109{
1b067564 3110 XHCIState *xhci = ptr;
2d754a10 3111 uint32_t ret;
62c6ae04 3112
62c6ae04
HM
3113 switch (reg) {
3114 case 0x00: /* USBCMD */
2d754a10
GH
3115 ret = xhci->usbcmd;
3116 break;
62c6ae04 3117 case 0x04: /* USBSTS */
2d754a10
GH
3118 ret = xhci->usbsts;
3119 break;
62c6ae04 3120 case 0x08: /* PAGESIZE */
2d754a10
GH
3121 ret = 1; /* 4KiB */
3122 break;
62c6ae04 3123 case 0x14: /* DNCTRL */
2d754a10
GH
3124 ret = xhci->dnctrl;
3125 break;
62c6ae04 3126 case 0x18: /* CRCR low */
2d754a10
GH
3127 ret = xhci->crcr_low & ~0xe;
3128 break;
62c6ae04 3129 case 0x1c: /* CRCR high */
2d754a10
GH
3130 ret = xhci->crcr_high;
3131 break;
62c6ae04 3132 case 0x30: /* DCBAAP low */
2d754a10
GH
3133 ret = xhci->dcbaap_low;
3134 break;
62c6ae04 3135 case 0x34: /* DCBAAP high */
2d754a10
GH
3136 ret = xhci->dcbaap_high;
3137 break;
62c6ae04 3138 case 0x38: /* CONFIG */
2d754a10
GH
3139 ret = xhci->config;
3140 break;
62c6ae04 3141 default:
0ab966cf 3142 trace_usb_xhci_unimplemented("oper read", reg);
2d754a10 3143 ret = 0;
62c6ae04 3144 }
2d754a10
GH
3145
3146 trace_usb_xhci_oper_read(reg, ret);
3147 return ret;
62c6ae04
HM
3148}
3149
a8170e5e 3150static void xhci_oper_write(void *ptr, hwaddr reg,
1b067564 3151 uint64_t val, unsigned size)
62c6ae04 3152{
1b067564 3153 XHCIState *xhci = ptr;
37034575 3154 DeviceState *d = DEVICE(ptr);
1b067564 3155
2d754a10
GH
3156 trace_usb_xhci_oper_write(reg, val);
3157
62c6ae04
HM
3158 switch (reg) {
3159 case 0x00: /* USBCMD */
3160 if ((val & USBCMD_RS) && !(xhci->usbcmd & USBCMD_RS)) {
3161 xhci_run(xhci);
3162 } else if (!(val & USBCMD_RS) && (xhci->usbcmd & USBCMD_RS)) {
3163 xhci_stop(xhci);
3164 }
f1f8bc21
GH
3165 if (val & USBCMD_CSS) {
3166 /* save state */
3167 xhci->usbsts &= ~USBSTS_SRE;
3168 }
3169 if (val & USBCMD_CRS) {
3170 /* restore state */
3171 xhci->usbsts |= USBSTS_SRE;
3172 }
62c6ae04 3173 xhci->usbcmd = val & 0xc0f;
01546fa6 3174 xhci_mfwrap_update(xhci);
62c6ae04 3175 if (val & USBCMD_HCRST) {
37034575 3176 xhci_reset(d);
62c6ae04 3177 }
4c4abe7c 3178 xhci_intx_update(xhci);
62c6ae04
HM
3179 break;
3180
3181 case 0x04: /* USBSTS */
3182 /* these bits are write-1-to-clear */
3183 xhci->usbsts &= ~(val & (USBSTS_HSE|USBSTS_EINT|USBSTS_PCD|USBSTS_SRE));
4c4abe7c 3184 xhci_intx_update(xhci);
62c6ae04
HM
3185 break;
3186
3187 case 0x14: /* DNCTRL */
3188 xhci->dnctrl = val & 0xffff;
3189 break;
3190 case 0x18: /* CRCR low */
3191 xhci->crcr_low = (val & 0xffffffcf) | (xhci->crcr_low & CRCR_CRR);
3192 break;
3193 case 0x1c: /* CRCR high */
3194 xhci->crcr_high = val;
3195 if (xhci->crcr_low & (CRCR_CA|CRCR_CS) && (xhci->crcr_low & CRCR_CRR)) {
3196 XHCIEvent event = {ER_COMMAND_COMPLETE, CC_COMMAND_RING_STOPPED};
3197 xhci->crcr_low &= ~CRCR_CRR;
2d1de850 3198 xhci_event(xhci, &event, 0);
62c6ae04
HM
3199 DPRINTF("xhci: command ring stopped (CRCR=%08x)\n", xhci->crcr_low);
3200 } else {
59a70ccd 3201 dma_addr_t base = xhci_addr64(xhci->crcr_low & ~0x3f, val);
62c6ae04
HM
3202 xhci_ring_init(xhci, &xhci->cmd_ring, base);
3203 }
3204 xhci->crcr_low &= ~(CRCR_CA | CRCR_CS);
3205 break;
3206 case 0x30: /* DCBAAP low */
3207 xhci->dcbaap_low = val & 0xffffffc0;
3208 break;
3209 case 0x34: /* DCBAAP high */
3210 xhci->dcbaap_high = val;
3211 break;
3212 case 0x38: /* CONFIG */
3213 xhci->config = val & 0xff;
3214 break;
3215 default:
0ab966cf 3216 trace_usb_xhci_unimplemented("oper write", reg);
62c6ae04
HM
3217 }
3218}
3219
a8170e5e 3220static uint64_t xhci_runtime_read(void *ptr, hwaddr reg,
1b067564 3221 unsigned size)
62c6ae04 3222{
1b067564 3223 XHCIState *xhci = ptr;
43d9d604 3224 uint32_t ret = 0;
62c6ae04 3225
43d9d604
GH
3226 if (reg < 0x20) {
3227 switch (reg) {
3228 case 0x00: /* MFINDEX */
3229 ret = xhci_mfindex_get(xhci) & 0x3fff;
3230 break;
3231 default:
0ab966cf 3232 trace_usb_xhci_unimplemented("runtime read", reg);
43d9d604
GH
3233 break;
3234 }
3235 } else {
3236 int v = (reg - 0x20) / 0x20;
3237 XHCIInterrupter *intr = &xhci->intr[v];
3238 switch (reg & 0x1f) {
3239 case 0x00: /* IMAN */
3240 ret = intr->iman;
3241 break;
3242 case 0x04: /* IMOD */
3243 ret = intr->imod;
3244 break;
3245 case 0x08: /* ERSTSZ */
3246 ret = intr->erstsz;
3247 break;
3248 case 0x10: /* ERSTBA low */
3249 ret = intr->erstba_low;
3250 break;
3251 case 0x14: /* ERSTBA high */
3252 ret = intr->erstba_high;
3253 break;
3254 case 0x18: /* ERDP low */
3255 ret = intr->erdp_low;
3256 break;
3257 case 0x1c: /* ERDP high */
3258 ret = intr->erdp_high;
3259 break;
3260 }
62c6ae04 3261 }
2d754a10
GH
3262
3263 trace_usb_xhci_runtime_read(reg, ret);
3264 return ret;
62c6ae04
HM
3265}
3266
a8170e5e 3267static void xhci_runtime_write(void *ptr, hwaddr reg,
1b067564 3268 uint64_t val, unsigned size)
62c6ae04 3269{
1b067564 3270 XHCIState *xhci = ptr;
43d9d604
GH
3271 int v = (reg - 0x20) / 0x20;
3272 XHCIInterrupter *intr = &xhci->intr[v];
8e9f18b6 3273 trace_usb_xhci_runtime_write(reg, val);
62c6ae04 3274
43d9d604 3275 if (reg < 0x20) {
0ab966cf 3276 trace_usb_xhci_unimplemented("runtime write", reg);
43d9d604
GH
3277 return;
3278 }
3279
3280 switch (reg & 0x1f) {
3281 case 0x00: /* IMAN */
62c6ae04 3282 if (val & IMAN_IP) {
962d11e1 3283 intr->iman &= ~IMAN_IP;
62c6ae04 3284 }
962d11e1
GH
3285 intr->iman &= ~IMAN_IE;
3286 intr->iman |= val & IMAN_IE;
43d9d604
GH
3287 if (v == 0) {
3288 xhci_intx_update(xhci);
3289 }
3290 xhci_msix_update(xhci, v);
62c6ae04 3291 break;
43d9d604 3292 case 0x04: /* IMOD */
962d11e1 3293 intr->imod = val;
62c6ae04 3294 break;
43d9d604 3295 case 0x08: /* ERSTSZ */
962d11e1 3296 intr->erstsz = val & 0xffff;
62c6ae04 3297 break;
43d9d604 3298 case 0x10: /* ERSTBA low */
62c6ae04 3299 /* XXX NEC driver bug: it doesn't align this to 64 bytes
962d11e1
GH
3300 intr->erstba_low = val & 0xffffffc0; */
3301 intr->erstba_low = val & 0xfffffff0;
62c6ae04 3302 break;
43d9d604 3303 case 0x14: /* ERSTBA high */
962d11e1 3304 intr->erstba_high = val;
43d9d604 3305 xhci_er_reset(xhci, v);
62c6ae04 3306 break;
43d9d604 3307 case 0x18: /* ERDP low */
62c6ae04 3308 if (val & ERDP_EHB) {
962d11e1 3309 intr->erdp_low &= ~ERDP_EHB;
62c6ae04 3310 }
962d11e1 3311 intr->erdp_low = (val & ~ERDP_EHB) | (intr->erdp_low & ERDP_EHB);
62c6ae04 3312 break;
43d9d604 3313 case 0x1c: /* ERDP high */
962d11e1 3314 intr->erdp_high = val;
43d9d604 3315 xhci_events_update(xhci, v);
62c6ae04
HM
3316 break;
3317 default:
0ab966cf 3318 trace_usb_xhci_unimplemented("oper write", reg);
62c6ae04
HM
3319 }
3320}
3321
a8170e5e 3322static uint64_t xhci_doorbell_read(void *ptr, hwaddr reg,
1b067564 3323 unsigned size)
62c6ae04 3324{
62c6ae04 3325 /* doorbells always read as 0 */
2d754a10 3326 trace_usb_xhci_doorbell_read(reg, 0);
62c6ae04
HM
3327 return 0;
3328}
3329
a8170e5e 3330static void xhci_doorbell_write(void *ptr, hwaddr reg,
1b067564 3331 uint64_t val, unsigned size)
62c6ae04 3332{
1b067564 3333 XHCIState *xhci = ptr;
024426ac 3334 unsigned int epid, streamid;
1b067564 3335
2d754a10 3336 trace_usb_xhci_doorbell_write(reg, val);
62c6ae04
HM
3337
3338 if (!xhci_running(xhci)) {
d6bb65fc 3339 DPRINTF("xhci: wrote doorbell while xHC stopped or paused\n");
62c6ae04
HM
3340 return;
3341 }
3342
3343 reg >>= 2;
3344
3345 if (reg == 0) {
3346 if (val == 0) {
3347 xhci_process_commands(xhci);
3348 } else {
d6bb65fc 3349 DPRINTF("xhci: bad doorbell 0 write: 0x%x\n",
1b067564 3350 (uint32_t)val);
62c6ae04
HM
3351 }
3352 } else {
024426ac
GH
3353 epid = val & 0xff;
3354 streamid = (val >> 16) & 0xffff;
91062ae0 3355 if (reg > xhci->numslots) {
d6bb65fc 3356 DPRINTF("xhci: bad doorbell %d\n", (int)reg);
024426ac 3357 } else if (epid > 31) {
d6bb65fc 3358 DPRINTF("xhci: bad doorbell %d write: 0x%x\n",
1b067564 3359 (int)reg, (uint32_t)val);
62c6ae04 3360 } else {
024426ac 3361 xhci_kick_ep(xhci, reg, epid, streamid);
62c6ae04
HM
3362 }
3363 }
3364}
3365
6d3bc22e
GH
3366static void xhci_cap_write(void *opaque, hwaddr addr, uint64_t val,
3367 unsigned width)
3368{
3369 /* nothing */
3370}
3371
1b067564
GH
3372static const MemoryRegionOps xhci_cap_ops = {
3373 .read = xhci_cap_read,
6d3bc22e 3374 .write = xhci_cap_write,
6ee021d4 3375 .valid.min_access_size = 1,
1b067564 3376 .valid.max_access_size = 4,
6ee021d4
GH
3377 .impl.min_access_size = 4,
3378 .impl.max_access_size = 4,
1b067564
GH
3379 .endianness = DEVICE_LITTLE_ENDIAN,
3380};
62c6ae04 3381
1b067564
GH
3382static const MemoryRegionOps xhci_oper_ops = {
3383 .read = xhci_oper_read,
3384 .write = xhci_oper_write,
3385 .valid.min_access_size = 4,
3386 .valid.max_access_size = 4,
3387 .endianness = DEVICE_LITTLE_ENDIAN,
3388};
62c6ae04 3389
1d8a4e69
GH
3390static const MemoryRegionOps xhci_port_ops = {
3391 .read = xhci_port_read,
3392 .write = xhci_port_write,
3393 .valid.min_access_size = 4,
3394 .valid.max_access_size = 4,
3395 .endianness = DEVICE_LITTLE_ENDIAN,
3396};
3397
1b067564
GH
3398static const MemoryRegionOps xhci_runtime_ops = {
3399 .read = xhci_runtime_read,
3400 .write = xhci_runtime_write,
3401 .valid.min_access_size = 4,
3402 .valid.max_access_size = 4,
3403 .endianness = DEVICE_LITTLE_ENDIAN,
3404};
62c6ae04 3405
1b067564
GH
3406static const MemoryRegionOps xhci_doorbell_ops = {
3407 .read = xhci_doorbell_read,
3408 .write = xhci_doorbell_write,
62c6ae04
HM
3409 .valid.min_access_size = 4,
3410 .valid.max_access_size = 4,
3411 .endianness = DEVICE_LITTLE_ENDIAN,
3412};
3413
3414static void xhci_attach(USBPort *usbport)
3415{
3416 XHCIState *xhci = usbport->opaque;
0846e635 3417 XHCIPort *port = xhci_lookup_port(xhci, usbport);
62c6ae04 3418
f3214027 3419 xhci_port_update(port, 0);
62c6ae04
HM
3420}
3421
3422static void xhci_detach(USBPort *usbport)
3423{
3424 XHCIState *xhci = usbport->opaque;
0846e635 3425 XHCIPort *port = xhci_lookup_port(xhci, usbport);
62c6ae04 3426
f3dcf638 3427 xhci_detach_slot(xhci, usbport);
f3214027 3428 xhci_port_update(port, 1);
62c6ae04
HM
3429}
3430
8c735e43
GH
3431static void xhci_wakeup(USBPort *usbport)
3432{
3433 XHCIState *xhci = usbport->opaque;
0846e635 3434 XHCIPort *port = xhci_lookup_port(xhci, usbport);
8c735e43 3435
85e05d82 3436 if (get_field(port->portsc, PORTSC_PLS) != PLS_U3) {
8c735e43
GH
3437 return;
3438 }
85e05d82 3439 set_field(&port->portsc, PLS_RESUME, PORTSC_PLS);
f705a362 3440 xhci_port_notify(port, PORTSC_PLC);
8c735e43
GH
3441}
3442
62c6ae04
HM
3443static void xhci_complete(USBPort *port, USBPacket *packet)
3444{
3445 XHCITransfer *xfer = container_of(packet, XHCITransfer, packet);
3446
9a77a0f5 3447 if (packet->status == USB_RET_REMOVE_FROM_QUEUE) {
582d6f4a 3448 xhci_ep_nuke_one_xfer(xfer, 0);
0cae7b1a
HG
3449 return;
3450 }
9a77a0f5 3451 xhci_complete_packet(xfer);
024426ac 3452 xhci_kick_ep(xfer->xhci, xfer->slotid, xfer->epid, xfer->streamid);
62c6ae04
HM
3453}
3454
ccaf87a0 3455static void xhci_child_detach(USBPort *uport, USBDevice *child)
62c6ae04 3456{
ccaf87a0
GH
3457 USBBus *bus = usb_bus_from_device(child);
3458 XHCIState *xhci = container_of(bus, XHCIState, bus);
ccaf87a0 3459
463c534d 3460 xhci_detach_slot(xhci, child->port);
62c6ae04
HM
3461}
3462
1d8a4e69 3463static USBPortOps xhci_uport_ops = {
62c6ae04
HM
3464 .attach = xhci_attach,
3465 .detach = xhci_detach,
8c735e43 3466 .wakeup = xhci_wakeup,
62c6ae04
HM
3467 .complete = xhci_complete,
3468 .child_detach = xhci_child_detach,
3469};
3470
7c605a23
GH
3471static int xhci_find_epid(USBEndpoint *ep)
3472{
3473 if (ep->nr == 0) {
3474 return 1;
3475 }
3476 if (ep->pid == USB_TOKEN_IN) {
3477 return ep->nr * 2 + 1;
3478 } else {
3479 return ep->nr * 2;
3480 }
3481}
3482
518ad5f2
HG
3483static USBEndpoint *xhci_epid_to_usbep(XHCIState *xhci,
3484 unsigned int slotid, unsigned int epid)
3485{
3486 assert(slotid >= 1 && slotid <= xhci->numslots);
3487
3488 if (!xhci->slots[slotid - 1].uport) {
3489 return NULL;
3490 }
3491
3492 return usb_ep_get(xhci->slots[slotid - 1].uport->dev,
3493 (epid & 1) ? USB_TOKEN_IN : USB_TOKEN_OUT, epid >> 1);
3494}
3495
8550a02d
GH
3496static void xhci_wakeup_endpoint(USBBus *bus, USBEndpoint *ep,
3497 unsigned int stream)
7c605a23
GH
3498{
3499 XHCIState *xhci = container_of(bus, XHCIState, bus);
3500 int slotid;
3501
3502 DPRINTF("%s\n", __func__);
af203be3 3503 slotid = ep->dev->addr;
7c605a23
GH
3504 if (slotid == 0 || !xhci->slots[slotid-1].enabled) {
3505 DPRINTF("%s: oops, no slot for dev %d\n", __func__, ep->dev->addr);
3506 return;
3507 }
024426ac 3508 xhci_kick_ep(xhci, slotid, xhci_find_epid(ep), stream);
7c605a23
GH
3509}
3510
62c6ae04 3511static USBBusOps xhci_bus_ops = {
7c605a23 3512 .wakeup_endpoint = xhci_wakeup_endpoint,
62c6ae04
HM
3513};
3514
37034575 3515static void usb_xhci_init(XHCIState *xhci)
62c6ae04 3516{
37034575 3517 DeviceState *dev = DEVICE(xhci);
0846e635
GH
3518 XHCIPort *port;
3519 int i, usbports, speedmask;
62c6ae04
HM
3520
3521 xhci->usbsts = USBSTS_HCH;
3522
0846e635
GH
3523 if (xhci->numports_2 > MAXPORTS_2) {
3524 xhci->numports_2 = MAXPORTS_2;
3525 }
3526 if (xhci->numports_3 > MAXPORTS_3) {
3527 xhci->numports_3 = MAXPORTS_3;
3528 }
3529 usbports = MAX(xhci->numports_2, xhci->numports_3);
3530 xhci->numports = xhci->numports_2 + xhci->numports_3;
3531
c889b3a5 3532 usb_bus_new(&xhci->bus, sizeof(xhci->bus), &xhci_bus_ops, dev);
62c6ae04 3533
0846e635
GH
3534 for (i = 0; i < usbports; i++) {
3535 speedmask = 0;
3536 if (i < xhci->numports_2) {
7bafd888
GH
3537 if (xhci_get_flag(xhci, XHCI_FLAG_SS_FIRST)) {
3538 port = &xhci->ports[i + xhci->numports_3];
3539 port->portnr = i + 1 + xhci->numports_3;
3540 } else {
3541 port = &xhci->ports[i];
3542 port->portnr = i + 1;
3543 }
0846e635
GH
3544 port->uport = &xhci->uports[i];
3545 port->speedmask =
3546 USB_SPEED_MASK_LOW |
3547 USB_SPEED_MASK_FULL |
3548 USB_SPEED_MASK_HIGH;
1d8a4e69 3549 snprintf(port->name, sizeof(port->name), "usb2 port #%d", i+1);
0846e635
GH
3550 speedmask |= port->speedmask;
3551 }
3552 if (i < xhci->numports_3) {
7bafd888
GH
3553 if (xhci_get_flag(xhci, XHCI_FLAG_SS_FIRST)) {
3554 port = &xhci->ports[i];
3555 port->portnr = i + 1;
3556 } else {
3557 port = &xhci->ports[i + xhci->numports_2];
3558 port->portnr = i + 1 + xhci->numports_2;
3559 }
0846e635
GH
3560 port->uport = &xhci->uports[i];
3561 port->speedmask = USB_SPEED_MASK_SUPER;
1d8a4e69 3562 snprintf(port->name, sizeof(port->name), "usb3 port #%d", i+1);
0846e635
GH
3563 speedmask |= port->speedmask;
3564 }
3565 usb_register_port(&xhci->bus, &xhci->uports[i], xhci, i,
1d8a4e69 3566 &xhci_uport_ops, speedmask);
62c6ae04 3567 }
62c6ae04
HM
3568}
3569
3570static int usb_xhci_initfn(struct PCIDevice *dev)
3571{
1d8a4e69 3572 int i, ret;
62c6ae04 3573
37034575 3574 XHCIState *xhci = XHCI(dev);
62c6ae04 3575
9b7d3334
AF
3576 dev->config[PCI_CLASS_PROG] = 0x30; /* xHCI */
3577 dev->config[PCI_INTERRUPT_PIN] = 0x01; /* interrupt pin 1 */
3578 dev->config[PCI_CACHE_LINE_SIZE] = 0x10;
3579 dev->config[0x60] = 0x30; /* release number */
62c6ae04 3580
37034575 3581 usb_xhci_init(xhci);
62c6ae04 3582
91062ae0
GH
3583 if (xhci->numintrs > MAXINTRS) {
3584 xhci->numintrs = MAXINTRS;
3585 }
c94a7c69
GH
3586 while (xhci->numintrs & (xhci->numintrs - 1)) { /* ! power of 2 */
3587 xhci->numintrs++;
3588 }
91062ae0
GH
3589 if (xhci->numintrs < 1) {
3590 xhci->numintrs = 1;
3591 }
3592 if (xhci->numslots > MAXSLOTS) {
3593 xhci->numslots = MAXSLOTS;
3594 }
3595 if (xhci->numslots < 1) {
3596 xhci->numslots = 1;
3597 }
2aa6bfcb
GH
3598 if (xhci_get_flag(xhci, XHCI_FLAG_ENABLE_STREAMS)) {
3599 xhci->max_pstreams_mask = 7; /* == 256 primary streams */
3600 } else {
3601 xhci->max_pstreams_mask = 0;
3602 }
91062ae0 3603
bc72ad67 3604 xhci->mfwrap_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, xhci_mfwrap_timer, xhci);
01546fa6 3605
22fc860b
PB
3606 memory_region_init(&xhci->mem, OBJECT(xhci), "xhci", LEN_REGS);
3607 memory_region_init_io(&xhci->mem_cap, OBJECT(xhci), &xhci_cap_ops, xhci,
1b067564 3608 "capabilities", LEN_CAP);
22fc860b 3609 memory_region_init_io(&xhci->mem_oper, OBJECT(xhci), &xhci_oper_ops, xhci,
1d8a4e69 3610 "operational", 0x400);
22fc860b 3611 memory_region_init_io(&xhci->mem_runtime, OBJECT(xhci), &xhci_runtime_ops, xhci,
1b067564 3612 "runtime", LEN_RUNTIME);
22fc860b 3613 memory_region_init_io(&xhci->mem_doorbell, OBJECT(xhci), &xhci_doorbell_ops, xhci,
1b067564
GH
3614 "doorbell", LEN_DOORBELL);
3615
3616 memory_region_add_subregion(&xhci->mem, 0, &xhci->mem_cap);
3617 memory_region_add_subregion(&xhci->mem, OFF_OPER, &xhci->mem_oper);
3618 memory_region_add_subregion(&xhci->mem, OFF_RUNTIME, &xhci->mem_runtime);
3619 memory_region_add_subregion(&xhci->mem, OFF_DOORBELL, &xhci->mem_doorbell);
3620
1d8a4e69
GH
3621 for (i = 0; i < xhci->numports; i++) {
3622 XHCIPort *port = &xhci->ports[i];
3623 uint32_t offset = OFF_OPER + 0x400 + 0x10 * i;
3624 port->xhci = xhci;
22fc860b 3625 memory_region_init_io(&port->mem, OBJECT(xhci), &xhci_port_ops, port,
1d8a4e69
GH
3626 port->name, 0x10);
3627 memory_region_add_subregion(&xhci->mem, offset, &port->mem);
3628 }
3629
9b7d3334 3630 pci_register_bar(dev, 0,
62c6ae04
HM
3631 PCI_BASE_ADDRESS_SPACE_MEMORY|PCI_BASE_ADDRESS_MEM_TYPE_64,
3632 &xhci->mem);
3633
e6043e92
DDAG
3634 if (pci_bus_is_express(dev->bus) ||
3635 xhci_get_flag(xhci, XHCI_FLAG_FORCE_PCIE_ENDCAP)) {
058fdcf5
GH
3636 ret = pcie_endpoint_cap_init(dev, 0xa0);
3637 assert(ret >= 0);
3638 }
62c6ae04 3639
f9955235 3640 if (xhci_get_flag(xhci, XHCI_FLAG_USE_MSI)) {
9b7d3334 3641 msi_init(dev, 0x70, xhci->numintrs, true, false);
62c6ae04 3642 }
f9955235 3643 if (xhci_get_flag(xhci, XHCI_FLAG_USE_MSI_X)) {
9b7d3334 3644 msix_init(dev, xhci->numintrs,
4c47f800
GH
3645 &xhci->mem, 0, OFF_MSIX_TABLE,
3646 &xhci->mem, 0, OFF_MSIX_PBA,
3647 0x90);
3648 }
62c6ae04
HM
3649
3650 return 0;
3651}
3652
53c30545
GA
3653static void usb_xhci_exit(PCIDevice *dev)
3654{
3655 int i;
3656 XHCIState *xhci = XHCI(dev);
3657
d733f74c
GA
3658 trace_usb_xhci_exit();
3659
53c30545
GA
3660 for (i = 0; i < xhci->numslots; i++) {
3661 xhci_disable_slot(xhci, i + 1);
3662 }
3663
3664 if (xhci->mfwrap_timer) {
3665 timer_del(xhci->mfwrap_timer);
3666 timer_free(xhci->mfwrap_timer);
3667 xhci->mfwrap_timer = NULL;
3668 }
3669
3670 memory_region_del_subregion(&xhci->mem, &xhci->mem_cap);
3671 memory_region_del_subregion(&xhci->mem, &xhci->mem_oper);
3672 memory_region_del_subregion(&xhci->mem, &xhci->mem_runtime);
3673 memory_region_del_subregion(&xhci->mem, &xhci->mem_doorbell);
3674
3675 for (i = 0; i < xhci->numports; i++) {
3676 XHCIPort *port = &xhci->ports[i];
3677 memory_region_del_subregion(&xhci->mem, &port->mem);
3678 }
3679
3680 /* destroy msix memory region */
3681 if (dev->msix_table && dev->msix_pba
3682 && dev->msix_entry_used) {
3683 memory_region_del_subregion(&xhci->mem, &dev->msix_table_mmio);
3684 memory_region_del_subregion(&xhci->mem, &dev->msix_pba_mmio);
3685 }
3686
3687 usb_bus_release(&xhci->bus);
3688}
3689
37352df3
GH
3690static int usb_xhci_post_load(void *opaque, int version_id)
3691{
3692 XHCIState *xhci = opaque;
9b7d3334 3693 PCIDevice *pci_dev = PCI_DEVICE(xhci);
37352df3
GH
3694 XHCISlot *slot;
3695 XHCIEPContext *epctx;
3696 dma_addr_t dcbaap, pctx;
3697 uint32_t slot_ctx[4];
3698 uint32_t ep_ctx[5];
3699 int slotid, epid, state, intr;
3700
3701 dcbaap = xhci_addr64(xhci->dcbaap_low, xhci->dcbaap_high);
3702
3703 for (slotid = 1; slotid <= xhci->numslots; slotid++) {
3704 slot = &xhci->slots[slotid-1];
3705 if (!slot->addressed) {
3706 continue;
3707 }
3708 slot->ctx =
9b7d3334 3709 xhci_mask64(ldq_le_pci_dma(pci_dev, dcbaap + 8 * slotid));
37352df3
GH
3710 xhci_dma_read_u32s(xhci, slot->ctx, slot_ctx, sizeof(slot_ctx));
3711 slot->uport = xhci_lookup_uport(xhci, slot_ctx);
f2ad97ff
GH
3712 if (!slot->uport) {
3713 /* should not happen, but may trigger on guest bugs */
3714 slot->enabled = 0;
3715 slot->addressed = 0;
3716 continue;
3717 }
37352df3
GH
3718 assert(slot->uport && slot->uport->dev);
3719
f6969b9f 3720 for (epid = 1; epid <= 31; epid++) {
37352df3
GH
3721 pctx = slot->ctx + 32 * epid;
3722 xhci_dma_read_u32s(xhci, pctx, ep_ctx, sizeof(ep_ctx));
3723 state = ep_ctx[0] & EP_STATE_MASK;
3724 if (state == EP_DISABLED) {
3725 continue;
3726 }
3727 epctx = xhci_alloc_epctx(xhci, slotid, epid);
3728 slot->eps[epid-1] = epctx;
3729 xhci_init_epctx(epctx, pctx, ep_ctx);
3730 epctx->state = state;
3731 if (state == EP_RUNNING) {
3732 /* kick endpoint after vmload is finished */
bc72ad67 3733 timer_mod(epctx->kick_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
37352df3
GH
3734 }
3735 }
3736 }
3737
3738 for (intr = 0; intr < xhci->numintrs; intr++) {
3739 if (xhci->intr[intr].msix_used) {
9b7d3334 3740 msix_vector_use(pci_dev, intr);
37352df3 3741 } else {
9b7d3334 3742 msix_vector_unuse(pci_dev, intr);
37352df3
GH
3743 }
3744 }
3745
3746 return 0;
3747}
3748
3749static const VMStateDescription vmstate_xhci_ring = {
3750 .name = "xhci-ring",
3751 .version_id = 1,
3752 .fields = (VMStateField[]) {
3753 VMSTATE_UINT64(dequeue, XHCIRing),
3754 VMSTATE_BOOL(ccs, XHCIRing),
3755 VMSTATE_END_OF_LIST()
3756 }
3757};
3758
3759static const VMStateDescription vmstate_xhci_port = {
3760 .name = "xhci-port",
3761 .version_id = 1,
3762 .fields = (VMStateField[]) {
3763 VMSTATE_UINT32(portsc, XHCIPort),
3764 VMSTATE_END_OF_LIST()
3765 }
3766};
3767
3768static const VMStateDescription vmstate_xhci_slot = {
3769 .name = "xhci-slot",
3770 .version_id = 1,
3771 .fields = (VMStateField[]) {
3772 VMSTATE_BOOL(enabled, XHCISlot),
3773 VMSTATE_BOOL(addressed, XHCISlot),
3774 VMSTATE_END_OF_LIST()
3775 }
3776};
3777
3778static const VMStateDescription vmstate_xhci_event = {
3779 .name = "xhci-event",
3780 .version_id = 1,
3781 .fields = (VMStateField[]) {
3782 VMSTATE_UINT32(type, XHCIEvent),
3783 VMSTATE_UINT32(ccode, XHCIEvent),
3784 VMSTATE_UINT64(ptr, XHCIEvent),
3785 VMSTATE_UINT32(length, XHCIEvent),
3786 VMSTATE_UINT32(flags, XHCIEvent),
3787 VMSTATE_UINT8(slotid, XHCIEvent),
3788 VMSTATE_UINT8(epid, XHCIEvent),
3afca1d6 3789 VMSTATE_END_OF_LIST()
37352df3
GH
3790 }
3791};
3792
3793static bool xhci_er_full(void *opaque, int version_id)
3794{
3795 struct XHCIInterrupter *intr = opaque;
3796 return intr->er_full;
3797}
3798
3799static const VMStateDescription vmstate_xhci_intr = {
3800 .name = "xhci-intr",
3801 .version_id = 1,
3802 .fields = (VMStateField[]) {
3803 /* registers */
3804 VMSTATE_UINT32(iman, XHCIInterrupter),
3805 VMSTATE_UINT32(imod, XHCIInterrupter),
3806 VMSTATE_UINT32(erstsz, XHCIInterrupter),
3807 VMSTATE_UINT32(erstba_low, XHCIInterrupter),
3808 VMSTATE_UINT32(erstba_high, XHCIInterrupter),
3809 VMSTATE_UINT32(erdp_low, XHCIInterrupter),
3810 VMSTATE_UINT32(erdp_high, XHCIInterrupter),
3811
3812 /* state */
3813 VMSTATE_BOOL(msix_used, XHCIInterrupter),
3814 VMSTATE_BOOL(er_pcs, XHCIInterrupter),
3815 VMSTATE_UINT64(er_start, XHCIInterrupter),
3816 VMSTATE_UINT32(er_size, XHCIInterrupter),
3817 VMSTATE_UINT32(er_ep_idx, XHCIInterrupter),
3818
3819 /* event queue (used if ring is full) */
3820 VMSTATE_BOOL(er_full, XHCIInterrupter),
3821 VMSTATE_UINT32_TEST(ev_buffer_put, XHCIInterrupter, xhci_er_full),
3822 VMSTATE_UINT32_TEST(ev_buffer_get, XHCIInterrupter, xhci_er_full),
3823 VMSTATE_STRUCT_ARRAY_TEST(ev_buffer, XHCIInterrupter, EV_QUEUE,
3824 xhci_er_full, 1,
3825 vmstate_xhci_event, XHCIEvent),
3826
3827 VMSTATE_END_OF_LIST()
3828 }
3829};
3830
62c6ae04
HM
3831static const VMStateDescription vmstate_xhci = {
3832 .name = "xhci",
37352df3
GH
3833 .version_id = 1,
3834 .post_load = usb_xhci_post_load,
3835 .fields = (VMStateField[]) {
9b7d3334
AF
3836 VMSTATE_PCIE_DEVICE(parent_obj, XHCIState),
3837 VMSTATE_MSIX(parent_obj, XHCIState),
37352df3
GH
3838
3839 VMSTATE_STRUCT_VARRAY_UINT32(ports, XHCIState, numports, 1,
3840 vmstate_xhci_port, XHCIPort),
3841 VMSTATE_STRUCT_VARRAY_UINT32(slots, XHCIState, numslots, 1,
3842 vmstate_xhci_slot, XHCISlot),
3843 VMSTATE_STRUCT_VARRAY_UINT32(intr, XHCIState, numintrs, 1,
3844 vmstate_xhci_intr, XHCIInterrupter),
3845
3846 /* Operational Registers */
3847 VMSTATE_UINT32(usbcmd, XHCIState),
3848 VMSTATE_UINT32(usbsts, XHCIState),
3849 VMSTATE_UINT32(dnctrl, XHCIState),
3850 VMSTATE_UINT32(crcr_low, XHCIState),
3851 VMSTATE_UINT32(crcr_high, XHCIState),
3852 VMSTATE_UINT32(dcbaap_low, XHCIState),
3853 VMSTATE_UINT32(dcbaap_high, XHCIState),
3854 VMSTATE_UINT32(config, XHCIState),
3855
3856 /* Runtime Registers & state */
3857 VMSTATE_INT64(mfindex_start, XHCIState),
3858 VMSTATE_TIMER(mfwrap_timer, XHCIState),
3859 VMSTATE_STRUCT(cmd_ring, XHCIState, 1, vmstate_xhci_ring, XHCIRing),
3860
3861 VMSTATE_END_OF_LIST()
3862 }
62c6ae04
HM
3863};
3864
39bffca2 3865static Property xhci_properties[] = {
91062ae0
GH
3866 DEFINE_PROP_BIT("msi", XHCIState, flags, XHCI_FLAG_USE_MSI, true),
3867 DEFINE_PROP_BIT("msix", XHCIState, flags, XHCI_FLAG_USE_MSI_X, true),
7bafd888
GH
3868 DEFINE_PROP_BIT("superspeed-ports-first",
3869 XHCIState, flags, XHCI_FLAG_SS_FIRST, true),
e6043e92
DDAG
3870 DEFINE_PROP_BIT("force-pcie-endcap", XHCIState, flags,
3871 XHCI_FLAG_FORCE_PCIE_ENDCAP, false),
2aa6bfcb
GH
3872 DEFINE_PROP_BIT("streams", XHCIState, flags,
3873 XHCI_FLAG_ENABLE_STREAMS, true),
91062ae0
GH
3874 DEFINE_PROP_UINT32("intrs", XHCIState, numintrs, MAXINTRS),
3875 DEFINE_PROP_UINT32("slots", XHCIState, numslots, MAXSLOTS),
3876 DEFINE_PROP_UINT32("p2", XHCIState, numports_2, 4),
3877 DEFINE_PROP_UINT32("p3", XHCIState, numports_3, 4),
39bffca2
AL
3878 DEFINE_PROP_END_OF_LIST(),
3879};
3880
40021f08
AL
3881static void xhci_class_init(ObjectClass *klass, void *data)
3882{
3883 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
39bffca2 3884 DeviceClass *dc = DEVICE_CLASS(klass);
40021f08 3885
39bffca2
AL
3886 dc->vmsd = &vmstate_xhci;
3887 dc->props = xhci_properties;
64619739 3888 dc->reset = xhci_reset;
125ee0ed 3889 set_bit(DEVICE_CATEGORY_USB, dc->categories);
40021f08 3890 k->init = usb_xhci_initfn;
53c30545 3891 k->exit = usb_xhci_exit;
40021f08
AL
3892 k->vendor_id = PCI_VENDOR_ID_NEC;
3893 k->device_id = PCI_DEVICE_ID_NEC_UPD720200;
3894 k->class_id = PCI_CLASS_SERIAL_USB;
3895 k->revision = 0x03;
3896 k->is_express = 1;
40021f08
AL
3897}
3898
8c43a6f0 3899static const TypeInfo xhci_info = {
37034575 3900 .name = TYPE_XHCI,
39bffca2
AL
3901 .parent = TYPE_PCI_DEVICE,
3902 .instance_size = sizeof(XHCIState),
3903 .class_init = xhci_class_init,
62c6ae04
HM
3904};
3905
83f7d43a 3906static void xhci_register_types(void)
62c6ae04 3907{
39bffca2 3908 type_register_static(&xhci_info);
62c6ae04 3909}
83f7d43a
AF
3910
3911type_init(xhci_register_types)