Sun May 23 21:12:29 2021 UTC ()
xhci(4): Block commands and issue Stop Endpoint on suspend.


(riastradh)
diff -r1.139 -r1.140 src/sys/dev/usb/xhci.c
diff -r1.18 -r1.19 src/sys/dev/usb/xhcivar.h

cvs diff -r1.139 -r1.140 src/sys/dev/usb/xhci.c (switch to unified diff)

--- src/sys/dev/usb/xhci.c 2021/05/23 11:49:45 1.139
+++ src/sys/dev/usb/xhci.c 2021/05/23 21:12:28 1.140
@@ -1,4129 +1,4198 @@ @@ -1,4129 +1,4198 @@
1/* $NetBSD: xhci.c,v 1.139 2021/05/23 11:49:45 riastradh Exp $ */ 1/* $NetBSD: xhci.c,v 1.140 2021/05/23 21:12:28 riastradh Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2013 Jonathan A. Kollasch 4 * Copyright (c) 2013 Jonathan A. Kollasch
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
15 * 15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29/* 29/*
30 * USB rev 2.0 and rev 3.1 specification 30 * USB rev 2.0 and rev 3.1 specification
31 * http://www.usb.org/developers/docs/ 31 * http://www.usb.org/developers/docs/
32 * xHCI rev 1.1 specification 32 * xHCI rev 1.1 specification
33 * http://www.intel.com/technology/usb/spec.htm 33 * http://www.intel.com/technology/usb/spec.htm
34 */ 34 */
35 35
36#include <sys/cdefs.h> 36#include <sys/cdefs.h>
37__KERNEL_RCSID(0, "$NetBSD: xhci.c,v 1.139 2021/05/23 11:49:45 riastradh Exp $"); 37__KERNEL_RCSID(0, "$NetBSD: xhci.c,v 1.140 2021/05/23 21:12:28 riastradh Exp $");
38 38
39#ifdef _KERNEL_OPT 39#ifdef _KERNEL_OPT
40#include "opt_usb.h" 40#include "opt_usb.h"
41#endif 41#endif
42 42
43#include <sys/param.h> 43#include <sys/param.h>
44#include <sys/systm.h> 44#include <sys/systm.h>
45#include <sys/kernel.h> 45#include <sys/kernel.h>
46#include <sys/kmem.h> 46#include <sys/kmem.h>
47#include <sys/device.h> 47#include <sys/device.h>
48#include <sys/select.h> 48#include <sys/select.h>
49#include <sys/proc.h> 49#include <sys/proc.h>
50#include <sys/queue.h> 50#include <sys/queue.h>
51#include <sys/mutex.h> 51#include <sys/mutex.h>
52#include <sys/condvar.h> 52#include <sys/condvar.h>
53#include <sys/bus.h> 53#include <sys/bus.h>
54#include <sys/cpu.h> 54#include <sys/cpu.h>
55#include <sys/sysctl.h> 55#include <sys/sysctl.h>
56 56
57#include <machine/endian.h> 57#include <machine/endian.h>
58 58
59#include <dev/usb/usb.h> 59#include <dev/usb/usb.h>
60#include <dev/usb/usbdi.h> 60#include <dev/usb/usbdi.h>
61#include <dev/usb/usbdivar.h> 61#include <dev/usb/usbdivar.h>
62#include <dev/usb/usbdi_util.h> 62#include <dev/usb/usbdi_util.h>
63#include <dev/usb/usbhist.h> 63#include <dev/usb/usbhist.h>
64#include <dev/usb/usb_mem.h> 64#include <dev/usb/usb_mem.h>
65#include <dev/usb/usb_quirks.h> 65#include <dev/usb/usb_quirks.h>
66 66
67#include <dev/usb/xhcireg.h> 67#include <dev/usb/xhcireg.h>
68#include <dev/usb/xhcivar.h> 68#include <dev/usb/xhcivar.h>
69#include <dev/usb/usbroothub.h> 69#include <dev/usb/usbroothub.h>
70 70
71 71
72#ifdef USB_DEBUG 72#ifdef USB_DEBUG
73#ifndef XHCI_DEBUG 73#ifndef XHCI_DEBUG
74#define xhcidebug 0 74#define xhcidebug 0
75#else /* !XHCI_DEBUG */ 75#else /* !XHCI_DEBUG */
76#define HEXDUMP(a, b, c) \ 76#define HEXDUMP(a, b, c) \
77 do { \ 77 do { \
78 if (xhcidebug > 0) \ 78 if (xhcidebug > 0) \
79 hexdump(printf, a, b, c); \ 79 hexdump(printf, a, b, c); \
80 } while (/*CONSTCOND*/0) 80 } while (/*CONSTCOND*/0)
81static int xhcidebug = 0; 81static int xhcidebug = 0;
82 82
83SYSCTL_SETUP(sysctl_hw_xhci_setup, "sysctl hw.xhci setup") 83SYSCTL_SETUP(sysctl_hw_xhci_setup, "sysctl hw.xhci setup")
84{ 84{
85 int err; 85 int err;
86 const struct sysctlnode *rnode; 86 const struct sysctlnode *rnode;
87 const struct sysctlnode *cnode; 87 const struct sysctlnode *cnode;
88 88
89 err = sysctl_createv(clog, 0, NULL, &rnode, 89 err = sysctl_createv(clog, 0, NULL, &rnode,
90 CTLFLAG_PERMANENT, CTLTYPE_NODE, "xhci", 90 CTLFLAG_PERMANENT, CTLTYPE_NODE, "xhci",
91 SYSCTL_DESCR("xhci global controls"), 91 SYSCTL_DESCR("xhci global controls"),
92 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL); 92 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
93 93
94 if (err) 94 if (err)
95 goto fail; 95 goto fail;
96 96
97 /* control debugging printfs */ 97 /* control debugging printfs */
98 err = sysctl_createv(clog, 0, &rnode, &cnode, 98 err = sysctl_createv(clog, 0, &rnode, &cnode,
99 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, 99 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
100 "debug", SYSCTL_DESCR("Enable debugging output"), 100 "debug", SYSCTL_DESCR("Enable debugging output"),
101 NULL, 0, &xhcidebug, sizeof(xhcidebug), CTL_CREATE, CTL_EOL); 101 NULL, 0, &xhcidebug, sizeof(xhcidebug), CTL_CREATE, CTL_EOL);
102 if (err) 102 if (err)
103 goto fail; 103 goto fail;
104 104
105 return; 105 return;
106fail: 106fail:
107 aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err); 107 aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
108} 108}
109 109
110#endif /* !XHCI_DEBUG */ 110#endif /* !XHCI_DEBUG */
111#endif /* USB_DEBUG */ 111#endif /* USB_DEBUG */
112 112
113#ifndef HEXDUMP 113#ifndef HEXDUMP
114#define HEXDUMP(a, b, c) 114#define HEXDUMP(a, b, c)
115#endif 115#endif
116 116
117#define DPRINTF(FMT,A,B,C,D) USBHIST_LOG(xhcidebug,FMT,A,B,C,D) 117#define DPRINTF(FMT,A,B,C,D) USBHIST_LOG(xhcidebug,FMT,A,B,C,D)
118#define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(xhcidebug,N,FMT,A,B,C,D) 118#define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(xhcidebug,N,FMT,A,B,C,D)
119#define XHCIHIST_FUNC() USBHIST_FUNC() 119#define XHCIHIST_FUNC() USBHIST_FUNC()
120#define XHCIHIST_CALLED(name) USBHIST_CALLED(xhcidebug) 120#define XHCIHIST_CALLED(name) USBHIST_CALLED(xhcidebug)
121#define XHCIHIST_CALLARGS(FMT,A,B,C,D) \ 121#define XHCIHIST_CALLARGS(FMT,A,B,C,D) \
122 USBHIST_CALLARGS(xhcidebug,FMT,A,B,C,D) 122 USBHIST_CALLARGS(xhcidebug,FMT,A,B,C,D)
123 123
124#define XHCI_DCI_SLOT 0 124#define XHCI_DCI_SLOT 0
125#define XHCI_DCI_EP_CONTROL 1 125#define XHCI_DCI_EP_CONTROL 1
126 126
127#define XHCI_ICI_INPUT_CONTROL 0 127#define XHCI_ICI_INPUT_CONTROL 0
128 128
129struct xhci_pipe { 129struct xhci_pipe {
130 struct usbd_pipe xp_pipe; 130 struct usbd_pipe xp_pipe;
131 struct usb_task xp_async_task; 131 struct usb_task xp_async_task;
132 int16_t xp_isoc_next; /* next frame */ 132 int16_t xp_isoc_next; /* next frame */
133 uint8_t xp_maxb; /* max burst */ 133 uint8_t xp_maxb; /* max burst */
134 uint8_t xp_mult; 134 uint8_t xp_mult;
135}; 135};
136 136
137#define XHCI_COMMAND_RING_TRBS 256 137#define XHCI_COMMAND_RING_TRBS 256
138#define XHCI_EVENT_RING_TRBS 256 138#define XHCI_EVENT_RING_TRBS 256
139#define XHCI_EVENT_RING_SEGMENTS 1 139#define XHCI_EVENT_RING_SEGMENTS 1
140#define XHCI_TRB_3_ED_BIT XHCI_TRB_3_ISP_BIT 140#define XHCI_TRB_3_ED_BIT XHCI_TRB_3_ISP_BIT
141 141
142static usbd_status xhci_open(struct usbd_pipe *); 142static usbd_status xhci_open(struct usbd_pipe *);
143static void xhci_close_pipe(struct usbd_pipe *); 143static void xhci_close_pipe(struct usbd_pipe *);
144static int xhci_intr1(struct xhci_softc * const); 144static int xhci_intr1(struct xhci_softc * const);
145static void xhci_softintr(void *); 145static void xhci_softintr(void *);
146static void xhci_poll(struct usbd_bus *); 146static void xhci_poll(struct usbd_bus *);
147static struct usbd_xfer *xhci_allocx(struct usbd_bus *, unsigned int); 147static struct usbd_xfer *xhci_allocx(struct usbd_bus *, unsigned int);
148static void xhci_freex(struct usbd_bus *, struct usbd_xfer *); 148static void xhci_freex(struct usbd_bus *, struct usbd_xfer *);
149static void xhci_abortx(struct usbd_xfer *); 149static void xhci_abortx(struct usbd_xfer *);
150static bool xhci_dying(struct usbd_bus *); 150static bool xhci_dying(struct usbd_bus *);
151static void xhci_get_lock(struct usbd_bus *, kmutex_t **); 151static void xhci_get_lock(struct usbd_bus *, kmutex_t **);
152static usbd_status xhci_new_device(device_t, struct usbd_bus *, int, int, int, 152static usbd_status xhci_new_device(device_t, struct usbd_bus *, int, int, int,
153 struct usbd_port *); 153 struct usbd_port *);
154static int xhci_roothub_ctrl(struct usbd_bus *, usb_device_request_t *, 154static int xhci_roothub_ctrl(struct usbd_bus *, usb_device_request_t *,
155 void *, int); 155 void *, int);
156 156
157static usbd_status xhci_configure_endpoint(struct usbd_pipe *); 157static usbd_status xhci_configure_endpoint(struct usbd_pipe *);
158//static usbd_status xhci_unconfigure_endpoint(struct usbd_pipe *); 158//static usbd_status xhci_unconfigure_endpoint(struct usbd_pipe *);
159static usbd_status xhci_reset_endpoint(struct usbd_pipe *); 159static usbd_status xhci_reset_endpoint(struct usbd_pipe *);
 160static usbd_status xhci_stop_endpoint_cmd(struct xhci_softc *,
 161 struct xhci_slot *, u_int, uint32_t);
160static usbd_status xhci_stop_endpoint(struct usbd_pipe *); 162static usbd_status xhci_stop_endpoint(struct usbd_pipe *);
161 163
162static void xhci_host_dequeue(struct xhci_ring * const); 164static void xhci_host_dequeue(struct xhci_ring * const);
163static usbd_status xhci_set_dequeue(struct usbd_pipe *); 165static usbd_status xhci_set_dequeue(struct usbd_pipe *);
164 166
165static usbd_status xhci_do_command(struct xhci_softc * const, 167static usbd_status xhci_do_command(struct xhci_softc * const,
166 struct xhci_soft_trb * const, int); 168 struct xhci_soft_trb * const, int);
167static usbd_status xhci_do_command_locked(struct xhci_softc * const, 169static usbd_status xhci_do_command_locked(struct xhci_softc * const,
168 struct xhci_soft_trb * const, int); 170 struct xhci_soft_trb * const, int);
169static usbd_status xhci_init_slot(struct usbd_device *, uint32_t); 171static usbd_status xhci_init_slot(struct usbd_device *, uint32_t);
170static void xhci_free_slot(struct xhci_softc *, struct xhci_slot *); 172static void xhci_free_slot(struct xhci_softc *, struct xhci_slot *);
171static usbd_status xhci_set_address(struct usbd_device *, uint32_t, bool); 173static usbd_status xhci_set_address(struct usbd_device *, uint32_t, bool);
172static usbd_status xhci_enable_slot(struct xhci_softc * const, 174static usbd_status xhci_enable_slot(struct xhci_softc * const,
173 uint8_t * const); 175 uint8_t * const);
174static usbd_status xhci_disable_slot(struct xhci_softc * const, uint8_t); 176static usbd_status xhci_disable_slot(struct xhci_softc * const, uint8_t);
175static usbd_status xhci_address_device(struct xhci_softc * const, 177static usbd_status xhci_address_device(struct xhci_softc * const,
176 uint64_t, uint8_t, bool); 178 uint64_t, uint8_t, bool);
177static void xhci_set_dcba(struct xhci_softc * const, uint64_t, int); 179static void xhci_set_dcba(struct xhci_softc * const, uint64_t, int);
178static usbd_status xhci_update_ep0_mps(struct xhci_softc * const, 180static usbd_status xhci_update_ep0_mps(struct xhci_softc * const,
179 struct xhci_slot * const, u_int); 181 struct xhci_slot * const, u_int);
180static usbd_status xhci_ring_init(struct xhci_softc * const, 182static usbd_status xhci_ring_init(struct xhci_softc * const,
181 struct xhci_ring **, size_t, size_t); 183 struct xhci_ring **, size_t, size_t);
182static void xhci_ring_free(struct xhci_softc * const, 184static void xhci_ring_free(struct xhci_softc * const,
183 struct xhci_ring ** const); 185 struct xhci_ring ** const);
184 186
185static void xhci_setup_ctx(struct usbd_pipe *); 187static void xhci_setup_ctx(struct usbd_pipe *);
186static void xhci_setup_route(struct usbd_pipe *, uint32_t *); 188static void xhci_setup_route(struct usbd_pipe *, uint32_t *);
187static void xhci_setup_tthub(struct usbd_pipe *, uint32_t *); 189static void xhci_setup_tthub(struct usbd_pipe *, uint32_t *);
188static void xhci_setup_maxburst(struct usbd_pipe *, uint32_t *); 190static void xhci_setup_maxburst(struct usbd_pipe *, uint32_t *);
189static uint32_t xhci_bival2ival(uint32_t, uint32_t); 191static uint32_t xhci_bival2ival(uint32_t, uint32_t);
190 192
191static void xhci_noop(struct usbd_pipe *); 193static void xhci_noop(struct usbd_pipe *);
192 194
193static usbd_status xhci_root_intr_transfer(struct usbd_xfer *); 195static usbd_status xhci_root_intr_transfer(struct usbd_xfer *);
194static usbd_status xhci_root_intr_start(struct usbd_xfer *); 196static usbd_status xhci_root_intr_start(struct usbd_xfer *);
195static void xhci_root_intr_abort(struct usbd_xfer *); 197static void xhci_root_intr_abort(struct usbd_xfer *);
196static void xhci_root_intr_close(struct usbd_pipe *); 198static void xhci_root_intr_close(struct usbd_pipe *);
197static void xhci_root_intr_done(struct usbd_xfer *); 199static void xhci_root_intr_done(struct usbd_xfer *);
198 200
199static usbd_status xhci_device_ctrl_transfer(struct usbd_xfer *); 201static usbd_status xhci_device_ctrl_transfer(struct usbd_xfer *);
200static usbd_status xhci_device_ctrl_start(struct usbd_xfer *); 202static usbd_status xhci_device_ctrl_start(struct usbd_xfer *);
201static void xhci_device_ctrl_abort(struct usbd_xfer *); 203static void xhci_device_ctrl_abort(struct usbd_xfer *);
202static void xhci_device_ctrl_close(struct usbd_pipe *); 204static void xhci_device_ctrl_close(struct usbd_pipe *);
203static void xhci_device_ctrl_done(struct usbd_xfer *); 205static void xhci_device_ctrl_done(struct usbd_xfer *);
204 206
205static usbd_status xhci_device_isoc_transfer(struct usbd_xfer *); 207static usbd_status xhci_device_isoc_transfer(struct usbd_xfer *);
206static usbd_status xhci_device_isoc_enter(struct usbd_xfer *); 208static usbd_status xhci_device_isoc_enter(struct usbd_xfer *);
207static void xhci_device_isoc_abort(struct usbd_xfer *); 209static void xhci_device_isoc_abort(struct usbd_xfer *);
208static void xhci_device_isoc_close(struct usbd_pipe *); 210static void xhci_device_isoc_close(struct usbd_pipe *);
209static void xhci_device_isoc_done(struct usbd_xfer *); 211static void xhci_device_isoc_done(struct usbd_xfer *);
210 212
211static usbd_status xhci_device_intr_transfer(struct usbd_xfer *); 213static usbd_status xhci_device_intr_transfer(struct usbd_xfer *);
212static usbd_status xhci_device_intr_start(struct usbd_xfer *); 214static usbd_status xhci_device_intr_start(struct usbd_xfer *);
213static void xhci_device_intr_abort(struct usbd_xfer *); 215static void xhci_device_intr_abort(struct usbd_xfer *);
214static void xhci_device_intr_close(struct usbd_pipe *); 216static void xhci_device_intr_close(struct usbd_pipe *);
215static void xhci_device_intr_done(struct usbd_xfer *); 217static void xhci_device_intr_done(struct usbd_xfer *);
216 218
217static usbd_status xhci_device_bulk_transfer(struct usbd_xfer *); 219static usbd_status xhci_device_bulk_transfer(struct usbd_xfer *);
218static usbd_status xhci_device_bulk_start(struct usbd_xfer *); 220static usbd_status xhci_device_bulk_start(struct usbd_xfer *);
219static void xhci_device_bulk_abort(struct usbd_xfer *); 221static void xhci_device_bulk_abort(struct usbd_xfer *);
220static void xhci_device_bulk_close(struct usbd_pipe *); 222static void xhci_device_bulk_close(struct usbd_pipe *);
221static void xhci_device_bulk_done(struct usbd_xfer *); 223static void xhci_device_bulk_done(struct usbd_xfer *);
222 224
223static const struct usbd_bus_methods xhci_bus_methods = { 225static const struct usbd_bus_methods xhci_bus_methods = {
224 .ubm_open = xhci_open, 226 .ubm_open = xhci_open,
225 .ubm_softint = xhci_softintr, 227 .ubm_softint = xhci_softintr,
226 .ubm_dopoll = xhci_poll, 228 .ubm_dopoll = xhci_poll,
227 .ubm_allocx = xhci_allocx, 229 .ubm_allocx = xhci_allocx,
228 .ubm_freex = xhci_freex, 230 .ubm_freex = xhci_freex,
229 .ubm_abortx = xhci_abortx, 231 .ubm_abortx = xhci_abortx,
230 .ubm_dying = xhci_dying, 232 .ubm_dying = xhci_dying,
231 .ubm_getlock = xhci_get_lock, 233 .ubm_getlock = xhci_get_lock,
232 .ubm_newdev = xhci_new_device, 234 .ubm_newdev = xhci_new_device,
233 .ubm_rhctrl = xhci_roothub_ctrl, 235 .ubm_rhctrl = xhci_roothub_ctrl,
234}; 236};
235 237
236static const struct usbd_pipe_methods xhci_root_intr_methods = { 238static const struct usbd_pipe_methods xhci_root_intr_methods = {
237 .upm_transfer = xhci_root_intr_transfer, 239 .upm_transfer = xhci_root_intr_transfer,
238 .upm_start = xhci_root_intr_start, 240 .upm_start = xhci_root_intr_start,
239 .upm_abort = xhci_root_intr_abort, 241 .upm_abort = xhci_root_intr_abort,
240 .upm_close = xhci_root_intr_close, 242 .upm_close = xhci_root_intr_close,
241 .upm_cleartoggle = xhci_noop, 243 .upm_cleartoggle = xhci_noop,
242 .upm_done = xhci_root_intr_done, 244 .upm_done = xhci_root_intr_done,
243}; 245};
244 246
245 247
246static const struct usbd_pipe_methods xhci_device_ctrl_methods = { 248static const struct usbd_pipe_methods xhci_device_ctrl_methods = {
247 .upm_transfer = xhci_device_ctrl_transfer, 249 .upm_transfer = xhci_device_ctrl_transfer,
248 .upm_start = xhci_device_ctrl_start, 250 .upm_start = xhci_device_ctrl_start,
249 .upm_abort = xhci_device_ctrl_abort, 251 .upm_abort = xhci_device_ctrl_abort,
250 .upm_close = xhci_device_ctrl_close, 252 .upm_close = xhci_device_ctrl_close,
251 .upm_cleartoggle = xhci_noop, 253 .upm_cleartoggle = xhci_noop,
252 .upm_done = xhci_device_ctrl_done, 254 .upm_done = xhci_device_ctrl_done,
253}; 255};
254 256
255static const struct usbd_pipe_methods xhci_device_isoc_methods = { 257static const struct usbd_pipe_methods xhci_device_isoc_methods = {
256 .upm_transfer = xhci_device_isoc_transfer, 258 .upm_transfer = xhci_device_isoc_transfer,
257 .upm_abort = xhci_device_isoc_abort, 259 .upm_abort = xhci_device_isoc_abort,
258 .upm_close = xhci_device_isoc_close, 260 .upm_close = xhci_device_isoc_close,
259 .upm_cleartoggle = xhci_noop, 261 .upm_cleartoggle = xhci_noop,
260 .upm_done = xhci_device_isoc_done, 262 .upm_done = xhci_device_isoc_done,
261}; 263};
262 264
263static const struct usbd_pipe_methods xhci_device_bulk_methods = { 265static const struct usbd_pipe_methods xhci_device_bulk_methods = {
264 .upm_transfer = xhci_device_bulk_transfer, 266 .upm_transfer = xhci_device_bulk_transfer,
265 .upm_start = xhci_device_bulk_start, 267 .upm_start = xhci_device_bulk_start,
266 .upm_abort = xhci_device_bulk_abort, 268 .upm_abort = xhci_device_bulk_abort,
267 .upm_close = xhci_device_bulk_close, 269 .upm_close = xhci_device_bulk_close,
268 .upm_cleartoggle = xhci_noop, 270 .upm_cleartoggle = xhci_noop,
269 .upm_done = xhci_device_bulk_done, 271 .upm_done = xhci_device_bulk_done,
270}; 272};
271 273
272static const struct usbd_pipe_methods xhci_device_intr_methods = { 274static const struct usbd_pipe_methods xhci_device_intr_methods = {
273 .upm_transfer = xhci_device_intr_transfer, 275 .upm_transfer = xhci_device_intr_transfer,
274 .upm_start = xhci_device_intr_start, 276 .upm_start = xhci_device_intr_start,
275 .upm_abort = xhci_device_intr_abort, 277 .upm_abort = xhci_device_intr_abort,
276 .upm_close = xhci_device_intr_close, 278 .upm_close = xhci_device_intr_close,
277 .upm_cleartoggle = xhci_noop, 279 .upm_cleartoggle = xhci_noop,
278 .upm_done = xhci_device_intr_done, 280 .upm_done = xhci_device_intr_done,
279}; 281};
280 282
281static inline uint32_t 283static inline uint32_t
282xhci_read_1(const struct xhci_softc * const sc, bus_size_t offset) 284xhci_read_1(const struct xhci_softc * const sc, bus_size_t offset)
283{ 285{
284 return bus_space_read_1(sc->sc_iot, sc->sc_ioh, offset); 286 return bus_space_read_1(sc->sc_iot, sc->sc_ioh, offset);
285} 287}
286 288
287static inline uint32_t 289static inline uint32_t
288xhci_read_2(const struct xhci_softc * const sc, bus_size_t offset) 290xhci_read_2(const struct xhci_softc * const sc, bus_size_t offset)
289{ 291{
290 return bus_space_read_2(sc->sc_iot, sc->sc_ioh, offset); 292 return bus_space_read_2(sc->sc_iot, sc->sc_ioh, offset);
291} 293}
292 294
293static inline uint32_t 295static inline uint32_t
294xhci_read_4(const struct xhci_softc * const sc, bus_size_t offset) 296xhci_read_4(const struct xhci_softc * const sc, bus_size_t offset)
295{ 297{
296 return bus_space_read_4(sc->sc_iot, sc->sc_ioh, offset); 298 return bus_space_read_4(sc->sc_iot, sc->sc_ioh, offset);
297} 299}
298 300
299static inline void 301static inline void
300xhci_write_1(const struct xhci_softc * const sc, bus_size_t offset, 302xhci_write_1(const struct xhci_softc * const sc, bus_size_t offset,
301 uint32_t value) 303 uint32_t value)
302{ 304{
303 bus_space_write_1(sc->sc_iot, sc->sc_ioh, offset, value); 305 bus_space_write_1(sc->sc_iot, sc->sc_ioh, offset, value);
304} 306}
305 307
306#if 0 /* unused */ 308#if 0 /* unused */
307static inline void 309static inline void
308xhci_write_4(const struct xhci_softc * const sc, bus_size_t offset, 310xhci_write_4(const struct xhci_softc * const sc, bus_size_t offset,
309 uint32_t value) 311 uint32_t value)
310{ 312{
311 bus_space_write_4(sc->sc_iot, sc->sc_ioh, offset, value); 313 bus_space_write_4(sc->sc_iot, sc->sc_ioh, offset, value);
312} 314}
313#endif /* unused */ 315#endif /* unused */
314 316
315static inline void 317static inline void
316xhci_barrier(const struct xhci_softc * const sc, int flags) 318xhci_barrier(const struct xhci_softc * const sc, int flags)
317{ 319{
318 bus_space_barrier(sc->sc_iot, sc->sc_ioh, 0, sc->sc_ios, flags); 320 bus_space_barrier(sc->sc_iot, sc->sc_ioh, 0, sc->sc_ios, flags);
319} 321}
320 322
321static inline uint32_t 323static inline uint32_t
322xhci_cap_read_4(const struct xhci_softc * const sc, bus_size_t offset) 324xhci_cap_read_4(const struct xhci_softc * const sc, bus_size_t offset)
323{ 325{
324 return bus_space_read_4(sc->sc_iot, sc->sc_cbh, offset); 326 return bus_space_read_4(sc->sc_iot, sc->sc_cbh, offset);
325} 327}
326 328
327static inline uint32_t 329static inline uint32_t
328xhci_op_read_4(const struct xhci_softc * const sc, bus_size_t offset) 330xhci_op_read_4(const struct xhci_softc * const sc, bus_size_t offset)
329{ 331{
330 return bus_space_read_4(sc->sc_iot, sc->sc_obh, offset); 332 return bus_space_read_4(sc->sc_iot, sc->sc_obh, offset);
331} 333}
332 334
333static inline void 335static inline void
334xhci_op_write_4(const struct xhci_softc * const sc, bus_size_t offset, 336xhci_op_write_4(const struct xhci_softc * const sc, bus_size_t offset,
335 uint32_t value) 337 uint32_t value)
336{ 338{
337 bus_space_write_4(sc->sc_iot, sc->sc_obh, offset, value); 339 bus_space_write_4(sc->sc_iot, sc->sc_obh, offset, value);
338} 340}
339 341
340static inline uint64_t 342static inline uint64_t
341xhci_op_read_8(const struct xhci_softc * const sc, bus_size_t offset) 343xhci_op_read_8(const struct xhci_softc * const sc, bus_size_t offset)
342{ 344{
343 uint64_t value; 345 uint64_t value;
344 346
345 if (XHCI_HCC_AC64(sc->sc_hcc)) { 347 if (XHCI_HCC_AC64(sc->sc_hcc)) {
346#ifdef XHCI_USE_BUS_SPACE_8 348#ifdef XHCI_USE_BUS_SPACE_8
347 value = bus_space_read_8(sc->sc_iot, sc->sc_obh, offset); 349 value = bus_space_read_8(sc->sc_iot, sc->sc_obh, offset);
348#else 350#else
349 value = bus_space_read_4(sc->sc_iot, sc->sc_obh, offset); 351 value = bus_space_read_4(sc->sc_iot, sc->sc_obh, offset);
350 value |= (uint64_t)bus_space_read_4(sc->sc_iot, sc->sc_obh, 352 value |= (uint64_t)bus_space_read_4(sc->sc_iot, sc->sc_obh,
351 offset + 4) << 32; 353 offset + 4) << 32;
352#endif 354#endif
353 } else { 355 } else {
354 value = bus_space_read_4(sc->sc_iot, sc->sc_obh, offset); 356 value = bus_space_read_4(sc->sc_iot, sc->sc_obh, offset);
355 } 357 }
356 358
357 return value; 359 return value;
358} 360}
359 361
360static inline void 362static inline void
361xhci_op_write_8(const struct xhci_softc * const sc, bus_size_t offset, 363xhci_op_write_8(const struct xhci_softc * const sc, bus_size_t offset,
362 uint64_t value) 364 uint64_t value)
363{ 365{
364 if (XHCI_HCC_AC64(sc->sc_hcc)) { 366 if (XHCI_HCC_AC64(sc->sc_hcc)) {
365#ifdef XHCI_USE_BUS_SPACE_8 367#ifdef XHCI_USE_BUS_SPACE_8
366 bus_space_write_8(sc->sc_iot, sc->sc_obh, offset, value); 368 bus_space_write_8(sc->sc_iot, sc->sc_obh, offset, value);
367#else 369#else
368 bus_space_write_4(sc->sc_iot, sc->sc_obh, offset + 0, 370 bus_space_write_4(sc->sc_iot, sc->sc_obh, offset + 0,
369 (value >> 0) & 0xffffffff); 371 (value >> 0) & 0xffffffff);
370 bus_space_write_4(sc->sc_iot, sc->sc_obh, offset + 4, 372 bus_space_write_4(sc->sc_iot, sc->sc_obh, offset + 4,
371 (value >> 32) & 0xffffffff); 373 (value >> 32) & 0xffffffff);
372#endif 374#endif
373 } else { 375 } else {
374 bus_space_write_4(sc->sc_iot, sc->sc_obh, offset, value); 376 bus_space_write_4(sc->sc_iot, sc->sc_obh, offset, value);
375 } 377 }
376} 378}
377 379
378static inline uint32_t 380static inline uint32_t
379xhci_rt_read_4(const struct xhci_softc * const sc, bus_size_t offset) 381xhci_rt_read_4(const struct xhci_softc * const sc, bus_size_t offset)
380{ 382{
381 return bus_space_read_4(sc->sc_iot, sc->sc_rbh, offset); 383 return bus_space_read_4(sc->sc_iot, sc->sc_rbh, offset);
382} 384}
383 385
384static inline void 386static inline void
385xhci_rt_write_4(const struct xhci_softc * const sc, bus_size_t offset, 387xhci_rt_write_4(const struct xhci_softc * const sc, bus_size_t offset,
386 uint32_t value) 388 uint32_t value)
387{ 389{
388 bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset, value); 390 bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset, value);
389} 391}
390 392
391static inline uint64_t 393static inline uint64_t
392xhci_rt_read_8(const struct xhci_softc * const sc, bus_size_t offset) 394xhci_rt_read_8(const struct xhci_softc * const sc, bus_size_t offset)
393{ 395{
394 uint64_t value; 396 uint64_t value;
395 397
396 if (XHCI_HCC_AC64(sc->sc_hcc)) { 398 if (XHCI_HCC_AC64(sc->sc_hcc)) {
397#ifdef XHCI_USE_BUS_SPACE_8 399#ifdef XHCI_USE_BUS_SPACE_8
398 value = bus_space_read_8(sc->sc_iot, sc->sc_rbh, offset); 400 value = bus_space_read_8(sc->sc_iot, sc->sc_rbh, offset);
399#else 401#else
400 value = bus_space_read_4(sc->sc_iot, sc->sc_rbh, offset); 402 value = bus_space_read_4(sc->sc_iot, sc->sc_rbh, offset);
401 value |= (uint64_t)bus_space_read_4(sc->sc_iot, sc->sc_rbh, 403 value |= (uint64_t)bus_space_read_4(sc->sc_iot, sc->sc_rbh,
402 offset + 4) << 32; 404 offset + 4) << 32;
403#endif 405#endif
404 } else { 406 } else {
405 value = bus_space_read_4(sc->sc_iot, sc->sc_rbh, offset); 407 value = bus_space_read_4(sc->sc_iot, sc->sc_rbh, offset);
406 } 408 }
407 409
408 return value; 410 return value;
409} 411}
410 412
411static inline void 413static inline void
412xhci_rt_write_8(const struct xhci_softc * const sc, bus_size_t offset, 414xhci_rt_write_8(const struct xhci_softc * const sc, bus_size_t offset,
413 uint64_t value) 415 uint64_t value)
414{ 416{
415 if (XHCI_HCC_AC64(sc->sc_hcc)) { 417 if (XHCI_HCC_AC64(sc->sc_hcc)) {
416#ifdef XHCI_USE_BUS_SPACE_8 418#ifdef XHCI_USE_BUS_SPACE_8
417 bus_space_write_8(sc->sc_iot, sc->sc_rbh, offset, value); 419 bus_space_write_8(sc->sc_iot, sc->sc_rbh, offset, value);
418#else 420#else
419 bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset + 0, 421 bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset + 0,
420 (value >> 0) & 0xffffffff); 422 (value >> 0) & 0xffffffff);
421 bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset + 4, 423 bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset + 4,
422 (value >> 32) & 0xffffffff); 424 (value >> 32) & 0xffffffff);
423#endif 425#endif
424 } else { 426 } else {
425 bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset, value); 427 bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset, value);
426 } 428 }
427} 429}
428 430
429#if 0 /* unused */ 431#if 0 /* unused */
430static inline uint32_t 432static inline uint32_t
431xhci_db_read_4(const struct xhci_softc * const sc, bus_size_t offset) 433xhci_db_read_4(const struct xhci_softc * const sc, bus_size_t offset)
432{ 434{
433 return bus_space_read_4(sc->sc_iot, sc->sc_dbh, offset); 435 return bus_space_read_4(sc->sc_iot, sc->sc_dbh, offset);
434} 436}
435#endif /* unused */ 437#endif /* unused */
436 438
437static inline void 439static inline void
438xhci_db_write_4(const struct xhci_softc * const sc, bus_size_t offset, 440xhci_db_write_4(const struct xhci_softc * const sc, bus_size_t offset,
439 uint32_t value) 441 uint32_t value)
440{ 442{
441 bus_space_write_4(sc->sc_iot, sc->sc_dbh, offset, value); 443 bus_space_write_4(sc->sc_iot, sc->sc_dbh, offset, value);
442} 444}
443 445
444/* --- */ 446/* --- */
445 447
446static inline uint8_t 448static inline uint8_t
447xhci_ep_get_type(usb_endpoint_descriptor_t * const ed) 449xhci_ep_get_type(usb_endpoint_descriptor_t * const ed)
448{ 450{
449 u_int eptype = 0; 451 u_int eptype = 0;
450 452
451 switch (UE_GET_XFERTYPE(ed->bmAttributes)) { 453 switch (UE_GET_XFERTYPE(ed->bmAttributes)) {
452 case UE_CONTROL: 454 case UE_CONTROL:
453 eptype = 0x0; 455 eptype = 0x0;
454 break; 456 break;
455 case UE_ISOCHRONOUS: 457 case UE_ISOCHRONOUS:
456 eptype = 0x1; 458 eptype = 0x1;
457 break; 459 break;
458 case UE_BULK: 460 case UE_BULK:
459 eptype = 0x2; 461 eptype = 0x2;
460 break; 462 break;
461 case UE_INTERRUPT: 463 case UE_INTERRUPT:
462 eptype = 0x3; 464 eptype = 0x3;
463 break; 465 break;
464 } 466 }
465 467
466 if ((UE_GET_XFERTYPE(ed->bmAttributes) == UE_CONTROL) || 468 if ((UE_GET_XFERTYPE(ed->bmAttributes) == UE_CONTROL) ||
467 (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)) 469 (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN))
468 return eptype | 0x4; 470 return eptype | 0x4;
469 else 471 else
470 return eptype; 472 return eptype;
471} 473}
472 474
473static u_int 475static u_int
474xhci_ep_get_dci(usb_endpoint_descriptor_t * const ed) 476xhci_ep_get_dci(usb_endpoint_descriptor_t * const ed)
475{ 477{
476 /* xHCI 1.0 section 4.5.1 */ 478 /* xHCI 1.0 section 4.5.1 */
477 u_int epaddr = UE_GET_ADDR(ed->bEndpointAddress); 479 u_int epaddr = UE_GET_ADDR(ed->bEndpointAddress);
478 u_int in = 0; 480 u_int in = 0;
479 481
480 if ((UE_GET_XFERTYPE(ed->bmAttributes) == UE_CONTROL) || 482 if ((UE_GET_XFERTYPE(ed->bmAttributes) == UE_CONTROL) ||
481 (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)) 483 (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN))
482 in = 1; 484 in = 1;
483 485
484 return epaddr * 2 + in; 486 return epaddr * 2 + in;
485} 487}
486 488
487static inline u_int 489static inline u_int
488xhci_dci_to_ici(const u_int i) 490xhci_dci_to_ici(const u_int i)
489{ 491{
490 return i + 1; 492 return i + 1;
491} 493}
492 494
493static inline void * 495static inline void *
494xhci_slot_get_dcv(struct xhci_softc * const sc, struct xhci_slot * const xs, 496xhci_slot_get_dcv(struct xhci_softc * const sc, struct xhci_slot * const xs,
495 const u_int dci) 497 const u_int dci)
496{ 498{
497 return KERNADDR(&xs->xs_dc_dma, sc->sc_ctxsz * dci); 499 return KERNADDR(&xs->xs_dc_dma, sc->sc_ctxsz * dci);
498} 500}
499 501
500#if 0 /* unused */ 502#if 0 /* unused */
501static inline bus_addr_t 503static inline bus_addr_t
502xhci_slot_get_dcp(struct xhci_softc * const sc, struct xhci_slot * const xs, 504xhci_slot_get_dcp(struct xhci_softc * const sc, struct xhci_slot * const xs,
503 const u_int dci) 505 const u_int dci)
504{ 506{
505 return DMAADDR(&xs->xs_dc_dma, sc->sc_ctxsz * dci); 507 return DMAADDR(&xs->xs_dc_dma, sc->sc_ctxsz * dci);
506} 508}
507#endif /* unused */ 509#endif /* unused */
508 510
509static inline void * 511static inline void *
510xhci_slot_get_icv(struct xhci_softc * const sc, struct xhci_slot * const xs, 512xhci_slot_get_icv(struct xhci_softc * const sc, struct xhci_slot * const xs,
511 const u_int ici) 513 const u_int ici)
512{ 514{
513 return KERNADDR(&xs->xs_ic_dma, sc->sc_ctxsz * ici); 515 return KERNADDR(&xs->xs_ic_dma, sc->sc_ctxsz * ici);
514} 516}
515 517
516static inline bus_addr_t 518static inline bus_addr_t
517xhci_slot_get_icp(struct xhci_softc * const sc, struct xhci_slot * const xs, 519xhci_slot_get_icp(struct xhci_softc * const sc, struct xhci_slot * const xs,
518 const u_int ici) 520 const u_int ici)
519{ 521{
520 return DMAADDR(&xs->xs_ic_dma, sc->sc_ctxsz * ici); 522 return DMAADDR(&xs->xs_ic_dma, sc->sc_ctxsz * ici);
521} 523}
522 524
523static inline struct xhci_trb * 525static inline struct xhci_trb *
524xhci_ring_trbv(struct xhci_ring * const xr, u_int idx) 526xhci_ring_trbv(struct xhci_ring * const xr, u_int idx)
525{ 527{
526 return KERNADDR(&xr->xr_dma, XHCI_TRB_SIZE * idx); 528 return KERNADDR(&xr->xr_dma, XHCI_TRB_SIZE * idx);
527} 529}
528 530
529static inline bus_addr_t 531static inline bus_addr_t
530xhci_ring_trbp(struct xhci_ring * const xr, u_int idx) 532xhci_ring_trbp(struct xhci_ring * const xr, u_int idx)
531{ 533{
532 return DMAADDR(&xr->xr_dma, XHCI_TRB_SIZE * idx); 534 return DMAADDR(&xr->xr_dma, XHCI_TRB_SIZE * idx);
533} 535}
534 536
535static inline void 537static inline void
536xhci_xfer_put_trb(struct xhci_xfer * const xx, u_int idx, 538xhci_xfer_put_trb(struct xhci_xfer * const xx, u_int idx,
537 uint64_t parameter, uint32_t status, uint32_t control) 539 uint64_t parameter, uint32_t status, uint32_t control)
538{ 540{
539 KASSERTMSG(idx < xx->xx_ntrb, "idx=%u xx_ntrb=%u", idx, xx->xx_ntrb); 541 KASSERTMSG(idx < xx->xx_ntrb, "idx=%u xx_ntrb=%u", idx, xx->xx_ntrb);
540 xx->xx_trb[idx].trb_0 = parameter; 542 xx->xx_trb[idx].trb_0 = parameter;
541 xx->xx_trb[idx].trb_2 = status; 543 xx->xx_trb[idx].trb_2 = status;
542 xx->xx_trb[idx].trb_3 = control; 544 xx->xx_trb[idx].trb_3 = control;
543} 545}
544 546
545static inline void 547static inline void
546xhci_trb_put(struct xhci_trb * const trb, uint64_t parameter, uint32_t status, 548xhci_trb_put(struct xhci_trb * const trb, uint64_t parameter, uint32_t status,
547 uint32_t control) 549 uint32_t control)
548{ 550{
549 trb->trb_0 = htole64(parameter); 551 trb->trb_0 = htole64(parameter);
550 trb->trb_2 = htole32(status); 552 trb->trb_2 = htole32(status);
551 trb->trb_3 = htole32(control); 553 trb->trb_3 = htole32(control);
552} 554}
553 555
554static int 556static int
555xhci_trb_get_idx(struct xhci_ring *xr, uint64_t trb_0, int *idx) 557xhci_trb_get_idx(struct xhci_ring *xr, uint64_t trb_0, int *idx)
556{ 558{
557 /* base address of TRBs */ 559 /* base address of TRBs */
558 bus_addr_t trbp = xhci_ring_trbp(xr, 0); 560 bus_addr_t trbp = xhci_ring_trbp(xr, 0);
559 561
560 /* trb_0 range sanity check */ 562 /* trb_0 range sanity check */
561 if (trb_0 == 0 || trb_0 < trbp || 563 if (trb_0 == 0 || trb_0 < trbp ||
562 (trb_0 - trbp) % sizeof(struct xhci_trb) != 0 || 564 (trb_0 - trbp) % sizeof(struct xhci_trb) != 0 ||
563 (trb_0 - trbp) / sizeof(struct xhci_trb) >= xr->xr_ntrb) { 565 (trb_0 - trbp) / sizeof(struct xhci_trb) >= xr->xr_ntrb) {
564 return 1; 566 return 1;
565 } 567 }
566 *idx = (trb_0 - trbp) / sizeof(struct xhci_trb); 568 *idx = (trb_0 - trbp) / sizeof(struct xhci_trb);
567 return 0; 569 return 0;
568} 570}
569 571
570static unsigned int 572static unsigned int
571xhci_get_epstate(struct xhci_softc * const sc, struct xhci_slot * const xs, 573xhci_get_epstate(struct xhci_softc * const sc, struct xhci_slot * const xs,
572 u_int dci) 574 u_int dci)
573{ 575{
574 uint32_t *cp; 576 uint32_t *cp;
575 577
576 usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD); 578 usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD);
577 cp = xhci_slot_get_dcv(sc, xs, dci); 579 cp = xhci_slot_get_dcv(sc, xs, dci);
578 return XHCI_EPCTX_0_EPSTATE_GET(le32toh(cp[0])); 580 return XHCI_EPCTX_0_EPSTATE_GET(le32toh(cp[0]));
579} 581}
580 582
581static inline unsigned int 583static inline unsigned int
582xhci_ctlrport2bus(struct xhci_softc * const sc, unsigned int ctlrport) 584xhci_ctlrport2bus(struct xhci_softc * const sc, unsigned int ctlrport)
583{ 585{
584 const unsigned int port = ctlrport - 1; 586 const unsigned int port = ctlrport - 1;
585 const uint8_t bit = __BIT(port % NBBY); 587 const uint8_t bit = __BIT(port % NBBY);
586 588
587 return __SHIFTOUT(sc->sc_ctlrportbus[port / NBBY], bit); 589 return __SHIFTOUT(sc->sc_ctlrportbus[port / NBBY], bit);
588} 590}
589 591
590/* 592/*
591 * Return the roothub port for a controller port. Both are 1..n. 593 * Return the roothub port for a controller port. Both are 1..n.
592 */ 594 */
593static inline unsigned int 595static inline unsigned int
594xhci_ctlrport2rhport(struct xhci_softc * const sc, unsigned int ctrlport) 596xhci_ctlrport2rhport(struct xhci_softc * const sc, unsigned int ctrlport)
595{ 597{
596 598
597 return sc->sc_ctlrportmap[ctrlport - 1]; 599 return sc->sc_ctlrportmap[ctrlport - 1];
598} 600}
599 601
600/* 602/*
601 * Return the controller port for a bus roothub port. Both are 1..n. 603 * Return the controller port for a bus roothub port. Both are 1..n.
602 */ 604 */
603static inline unsigned int 605static inline unsigned int
604xhci_rhport2ctlrport(struct xhci_softc * const sc, unsigned int bn, 606xhci_rhport2ctlrport(struct xhci_softc * const sc, unsigned int bn,
605 unsigned int rhport) 607 unsigned int rhport)
606{ 608{
607 609
608 return sc->sc_rhportmap[bn][rhport - 1]; 610 return sc->sc_rhportmap[bn][rhport - 1];
609} 611}
610 612
611/* --- */ 613/* --- */
612 614
613void 615void
614xhci_childdet(device_t self, device_t child) 616xhci_childdet(device_t self, device_t child)
615{ 617{
616 struct xhci_softc * const sc = device_private(self); 618 struct xhci_softc * const sc = device_private(self);
617 619
618 KASSERT((sc->sc_child == child) || (sc->sc_child2 == child)); 620 KASSERT((sc->sc_child == child) || (sc->sc_child2 == child));
619 if (child == sc->sc_child2) 621 if (child == sc->sc_child2)
620 sc->sc_child2 = NULL; 622 sc->sc_child2 = NULL;
621 else if (child == sc->sc_child) 623 else if (child == sc->sc_child)
622 sc->sc_child = NULL; 624 sc->sc_child = NULL;
623} 625}
624 626
625int 627int
626xhci_detach(struct xhci_softc *sc, int flags) 628xhci_detach(struct xhci_softc *sc, int flags)
627{ 629{
628 int rv = 0; 630 int rv = 0;
629 631
630 if (sc->sc_child2 != NULL) { 632 if (sc->sc_child2 != NULL) {
631 rv = config_detach(sc->sc_child2, flags); 633 rv = config_detach(sc->sc_child2, flags);
632 if (rv != 0) 634 if (rv != 0)
633 return rv; 635 return rv;
634 KASSERT(sc->sc_child2 == NULL); 636 KASSERT(sc->sc_child2 == NULL);
635 } 637 }
636 638
637 if (sc->sc_child != NULL) { 639 if (sc->sc_child != NULL) {
638 rv = config_detach(sc->sc_child, flags); 640 rv = config_detach(sc->sc_child, flags);
639 if (rv != 0) 641 if (rv != 0)
640 return rv; 642 return rv;
641 KASSERT(sc->sc_child == NULL); 643 KASSERT(sc->sc_child == NULL);
642 } 644 }
643 645
644 /* XXX unconfigure/free slots */ 646 /* XXX unconfigure/free slots */
645 647
646 /* verify: */ 648 /* verify: */
647 xhci_rt_write_4(sc, XHCI_IMAN(0), 0); 649 xhci_rt_write_4(sc, XHCI_IMAN(0), 0);
648 xhci_op_write_4(sc, XHCI_USBCMD, 0); 650 xhci_op_write_4(sc, XHCI_USBCMD, 0);
649 /* do we need to wait for stop? */ 651 /* do we need to wait for stop? */
650 652
651 xhci_op_write_8(sc, XHCI_CRCR, 0); 653 xhci_op_write_8(sc, XHCI_CRCR, 0);
652 xhci_ring_free(sc, &sc->sc_cr); 654 xhci_ring_free(sc, &sc->sc_cr);
653 cv_destroy(&sc->sc_command_cv); 655 cv_destroy(&sc->sc_command_cv);
654 cv_destroy(&sc->sc_cmdbusy_cv); 656 cv_destroy(&sc->sc_cmdbusy_cv);
655 657
656 xhci_rt_write_4(sc, XHCI_ERSTSZ(0), 0); 658 xhci_rt_write_4(sc, XHCI_ERSTSZ(0), 0);
657 xhci_rt_write_8(sc, XHCI_ERSTBA(0), 0); 659 xhci_rt_write_8(sc, XHCI_ERSTBA(0), 0);
658 xhci_rt_write_8(sc, XHCI_ERDP(0), 0 | XHCI_ERDP_BUSY); 660 xhci_rt_write_8(sc, XHCI_ERDP(0), 0 | XHCI_ERDP_BUSY);
659 xhci_ring_free(sc, &sc->sc_er); 661 xhci_ring_free(sc, &sc->sc_er);
660 662
661 usb_freemem(&sc->sc_bus, &sc->sc_eventst_dma); 663 usb_freemem(&sc->sc_bus, &sc->sc_eventst_dma);
662 664
663 xhci_op_write_8(sc, XHCI_DCBAAP, 0); 665 xhci_op_write_8(sc, XHCI_DCBAAP, 0);
664 usb_freemem(&sc->sc_bus, &sc->sc_dcbaa_dma); 666 usb_freemem(&sc->sc_bus, &sc->sc_dcbaa_dma);
665 667
666 kmem_free(sc->sc_slots, sizeof(*sc->sc_slots) * sc->sc_maxslots); 668 kmem_free(sc->sc_slots, sizeof(*sc->sc_slots) * sc->sc_maxslots);
667 669
668 kmem_free(sc->sc_ctlrportbus, 670 kmem_free(sc->sc_ctlrportbus,
669 howmany(sc->sc_maxports * sizeof(uint8_t), NBBY)); 671 howmany(sc->sc_maxports * sizeof(uint8_t), NBBY));
670 kmem_free(sc->sc_ctlrportmap, sc->sc_maxports * sizeof(int)); 672 kmem_free(sc->sc_ctlrportmap, sc->sc_maxports * sizeof(int));
671 673
672 for (size_t j = 0; j < __arraycount(sc->sc_rhportmap); j++) { 674 for (size_t j = 0; j < __arraycount(sc->sc_rhportmap); j++) {
673 kmem_free(sc->sc_rhportmap[j], sc->sc_maxports * sizeof(int)); 675 kmem_free(sc->sc_rhportmap[j], sc->sc_maxports * sizeof(int));
674 } 676 }
675 677
676 mutex_destroy(&sc->sc_lock); 678 mutex_destroy(&sc->sc_lock);
677 mutex_destroy(&sc->sc_intr_lock); 679 mutex_destroy(&sc->sc_intr_lock);
678 680
679 pool_cache_destroy(sc->sc_xferpool); 681 pool_cache_destroy(sc->sc_xferpool);
680 682
681 return rv; 683 return rv;
682} 684}
683 685
684int 686int
685xhci_activate(device_t self, enum devact act) 687xhci_activate(device_t self, enum devact act)
686{ 688{
687 struct xhci_softc * const sc = device_private(self); 689 struct xhci_softc * const sc = device_private(self);
688 690
689 switch (act) { 691 switch (act) {
690 case DVACT_DEACTIVATE: 692 case DVACT_DEACTIVATE:
691 sc->sc_dying = true; 693 sc->sc_dying = true;
692 return 0; 694 return 0;
693 default: 695 default:
694 return EOPNOTSUPP; 696 return EOPNOTSUPP;
695 } 697 }
696} 698}
697 699
698bool 700bool
699xhci_suspend(device_t self, const pmf_qual_t *qual) 701xhci_suspend(device_t self, const pmf_qual_t *qual)
700{ 702{
701 struct xhci_softc * const sc = device_private(self); 703 struct xhci_softc * const sc = device_private(self);
702 size_t i, j, bn; 704 size_t i, j, bn, dci;
703 int port; 705 int port;
704 uint32_t v; 706 uint32_t v;
 707 usbd_status err;
 708 bool ok = false;
705 709
706 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 710 XHCIHIST_FUNC(); XHCIHIST_CALLED();
707 711
 712 mutex_enter(&sc->sc_lock);
 713
 714 /*
 715 * Block issuance of new commands, and wait for all pending
 716 * commands to complete.
 717 */
 718 KASSERT(sc->sc_suspender == NULL);
 719 sc->sc_suspender = curlwp;
 720 while (sc->sc_command_addr != 0)
 721 cv_wait(&sc->sc_cmdbusy_cv, &sc->sc_lock);
 722
 723 /*
 724 * xHCI Requirements Specification 1.2, May 2019, Sec. 4.23.2:
 725 * xHCI Power Management, p. 342
 726 * https://www.intel.com/content/dam/www/public/us/en/documents/technical-specifications/extensible-host-controler-interface-usb-xhci.pdf#page=342
 727 */
 728
 729 /*
 730 * `1. Stop all USB activity by issuing Stop Endpoint Commands
 731 * for Busy endpoints in the Running state. If the Force
 732 * Save Context Capability (FSC = ``0'') is not supported,
 733 * then Stop Endpoint Commands shall be issued for all idle
 734 * endpoints in the Running state as well. The Stop
 735 * Endpoint Command causes the xHC to update the respective
 736 * Endpoint or Stream Contexts in system memory, e.g. the
 737 * TR Dequeue Pointer, DCS, etc. fields. Refer to
 738 * Implementation Note "0".'
 739 */
 740 for (i = 0; i < sc->sc_maxslots; i++) {
 741 struct xhci_slot *xs = &sc->sc_slots[i];
 742
 743 /* Skip if the slot is not in use. */
 744 if (xs->xs_idx == 0)
 745 continue;
 746
 747 for (dci = XHCI_DCI_SLOT; dci <= XHCI_MAX_DCI; dci++) {
 748 /* Skip if the endpoint is not Running. */
 749 /* XXX What about Busy? */
 750 if (xhci_get_epstate(sc, xs, dci) !=
 751 XHCI_EPSTATE_RUNNING)
 752 continue;
 753
 754 /* Stop endpoint. */
 755 err = xhci_stop_endpoint_cmd(sc, xs, dci,
 756 XHCI_TRB_3_SUSP_EP_BIT);
 757 if (err) {
 758 device_printf(self, "failed to stop endpoint"
 759 " slot %zu dci %zu err %d\n",
 760 i, dci, err);
 761 goto out;
 762 }
 763 }
 764 }
 765
708 /* 766 /*
709 * First, suspend all the ports: 767 * Next, suspend all the ports:
710 * 768 *
711 * xHCI Requirements Specification 1.2, May 2019, Sec. 4.15: 769 * xHCI Requirements Specification 1.2, May 2019, Sec. 4.15:
712 * Suspend-Resume, pp. 276-283 770 * Suspend-Resume, pp. 276-283
713 * https://www.intel.com/content/dam/www/public/us/en/documents/technical-specifications/extensible-host-controler-interface-usb-xhci.pdf#page=276 771 * https://www.intel.com/content/dam/www/public/us/en/documents/technical-specifications/extensible-host-controler-interface-usb-xhci.pdf#page=276
714 */ 772 */
715 for (bn = 0; bn < 2; bn++) { 773 for (bn = 0; bn < 2; bn++) {
716 for (i = 1; i <= sc->sc_rhportcount[bn]; i++) { 774 for (i = 1; i <= sc->sc_rhportcount[bn]; i++) {
717 /* 4.15.1: Port Suspend. */ 775 /* 4.15.1: Port Suspend. */
718 port = XHCI_PORTSC(xhci_rhport2ctlrport(sc, bn, i)); 776 port = XHCI_PORTSC(xhci_rhport2ctlrport(sc, bn, i));
719 777
720 /* 778 /*
721 * `System software places individual ports 779 * `System software places individual ports
722 * into suspend mode by writing a ``3'' into 780 * into suspend mode by writing a ``3'' into
723 * the appropriate PORTSC register Port Link 781 * the appropriate PORTSC register Port Link
724 * State (PLS) field (refer to Section 5.4.8). 782 * State (PLS) field (refer to Section 5.4.8).
725 * Software should only set the PLS field to 783 * Software should only set the PLS field to
726 * ``3'' when the port is in the Enabled 784 * ``3'' when the port is in the Enabled
727 * state.' 785 * state.'
728 * 786 *
729 * `Software should not attempt to suspend a 787 * `Software should not attempt to suspend a
730 * port unless the port reports that it is in 788 * port unless the port reports that it is in
731 * the enabled (PED = ``1''; PLS < ``3'') 789 * the enabled (PED = ``1''; PLS < ``3'')
732 * state (refer to Section 5.4.8 for more 790 * state (refer to Section 5.4.8 for more
733 * information about PED and PLS).' 791 * information about PED and PLS).'
734 */ 792 */
735 v = xhci_op_read_4(sc, port); 793 v = xhci_op_read_4(sc, port);
736 if (((v & XHCI_PS_PED) == 0) || 794 if (((v & XHCI_PS_PED) == 0) ||
737 XHCI_PS_PLS_GET(v) >= XHCI_PS_PLS_U3) 795 XHCI_PS_PLS_GET(v) >= XHCI_PS_PLS_U3)
738 continue; 796 continue;
739 v &= ~(XHCI_PS_PLS_MASK | XHCI_PS_CLEAR); 797 v &= ~(XHCI_PS_PLS_MASK | XHCI_PS_CLEAR);
740 v |= XHCI_PS_LWS | XHCI_PS_PLS_SET(XHCI_PS_PLS_SETU3); 798 v |= XHCI_PS_LWS | XHCI_PS_PLS_SET(XHCI_PS_PLS_SETU3);
741 xhci_op_write_4(sc, port, v); 799 xhci_op_write_4(sc, port, v);
742 800
743 /* 801 /*
744 * `When the PLS field is written with U3 802 * `When the PLS field is written with U3
745 * (``3''), the status of the PLS bit will not 803 * (``3''), the status of the PLS bit will not
746 * change to the target U state U3 until the 804 * change to the target U state U3 until the
747 * suspend signaling has completed to the 805 * suspend signaling has completed to the
748 * attached device (which may be as long as 806 * attached device (which may be as long as
749 * 10ms.).' 807 * 10ms.).'
750 * 808 *
751 * `Software is required to wait for U3 809 * `Software is required to wait for U3
752 * transitions to complete before it puts the 810 * transitions to complete before it puts the
753 * xHC into a low power state, and before 811 * xHC into a low power state, and before
754 * resuming the port.' 812 * resuming the port.'
755 * 813 *
756 * XXX Take advantage of the technique to 814 * XXX Take advantage of the technique to
757 * reduce polling on host controllers that 815 * reduce polling on host controllers that
758 * support the U3C capability. 816 * support the U3C capability.
759 */ 817 */
760 for (j = 0; j < XHCI_WAIT_PLS_U3; j++) { 818 for (j = 0; j < XHCI_WAIT_PLS_U3; j++) {
761 v = xhci_op_read_4(sc, port); 819 v = xhci_op_read_4(sc, port);
762 if (XHCI_PS_PLS_GET(v) == XHCI_PS_PLS_U3) 820 if (XHCI_PS_PLS_GET(v) == XHCI_PS_PLS_U3)
763 break; 821 break;
764 usb_delay_ms(&sc->sc_bus, 1); 822 usb_delay_ms(&sc->sc_bus, 1);
765 } 823 }
766 if (j == XHCI_WAIT_PLS_U3) { 824 if (j == XHCI_WAIT_PLS_U3) {
767 device_printf(self, 825 device_printf(self,
768 "suspend timeout on bus %zu port %zu\n", 826 "suspend timeout on bus %zu port %zu\n",
769 bn, i); 827 bn, i);
770 return false; 828 goto out;
771 } 829 }
772 } 830 }
773 } 831 }
774 832
775 /* 833 /*
776 * xHCI Requirements Specification 1.2, May 2019, Sec. 4.23.2: 
777 * xHCI Power Management, p. 342 
778 * https://www.intel.com/content/dam/www/public/us/en/documents/technical-specifications/extensible-host-controler-interface-usb-xhci.pdf#page=342 
779 */ 
780 
781 /* 
782 * `1. Stop all USB activity by issuing Stop Endpoint Commands 
783 * for Busy endpoints in the Running state. If the Force 
784 * Save Context Capability (FSC = ``0'') is not supported, 
785 * then Stop Endpoint Commands shall be issued for all Idle 
786 * endpoints in the Running state as well. The Stop 
787 * Endpoint Command causes the xHC to update the respective 
788 * Endpoint or Stream Contexts in system memory, e.g. the 
789 * TR Dequeue Pointer, DCS, etc. fields. Refer to 
790 * Implementation Note "0".' 
791 * 
792 * XXX Not entirely sure if this is necessary for us; also it 
793 * probably has to happen before suspending the ports. 
794 */ 
795 
796 /* 
797 * `2. Ensure that the Command Ring is in the Stopped state 834 * `2. Ensure that the Command Ring is in the Stopped state
798 * (CRR = ``0'') or Idle (i.e. the Command Transfer Ring is 835 * (CRR = ``0'') or Idle (i.e. the Command Transfer Ring is
799 * empty), and all Command Completion Events associated 836 * empty), and all Command Completion Events associated
800 * with them have been received.' 837 * with them have been received.'
801 * 838 *
802 * XXX 839 * XXX
803 */ 840 */
804 841
805 /* `3. Stop the controller by setting Run/Stop (R/S) = ``0''.' */ 842 /* `3. Stop the controller by setting Run/Stop (R/S) = ``0''.' */
806 xhci_op_write_4(sc, XHCI_USBCMD, 843 xhci_op_write_4(sc, XHCI_USBCMD,
807 xhci_op_read_4(sc, XHCI_USBCMD) & ~XHCI_CMD_RS); 844 xhci_op_read_4(sc, XHCI_USBCMD) & ~XHCI_CMD_RS);
808 845
809 /* 846 /*
810 * `4. Read the Operational Runtime, and VTIO registers in the 847 * `4. Read the Operational Runtime, and VTIO registers in the
811 * following order: USBCMD, DNCTRL, DCBAAP, CONFIG, ERSTSZ, 848 * following order: USBCMD, DNCTRL, DCBAAP, CONFIG, ERSTSZ,
812 * ERSTBA, ERDP, IMAN, IMOD, and VTIO and save their 849 * ERSTBA, ERDP, IMAN, IMOD, and VTIO and save their
813 * state.' 850 * state.'
814 * 851 *
815 * (We don't use VTIO here (XXX for now?).) 852 * (We don't use VTIO here (XXX for now?).)
816 */ 853 */
817 sc->sc_regs.usbcmd = xhci_op_read_4(sc, XHCI_USBCMD); 854 sc->sc_regs.usbcmd = xhci_op_read_4(sc, XHCI_USBCMD);
818 sc->sc_regs.dnctrl = xhci_op_read_4(sc, XHCI_DNCTRL); 855 sc->sc_regs.dnctrl = xhci_op_read_4(sc, XHCI_DNCTRL);
819 sc->sc_regs.dcbaap = xhci_op_read_8(sc, XHCI_DCBAAP); 856 sc->sc_regs.dcbaap = xhci_op_read_8(sc, XHCI_DCBAAP);
820 sc->sc_regs.config = xhci_op_read_4(sc, XHCI_CONFIG); 857 sc->sc_regs.config = xhci_op_read_4(sc, XHCI_CONFIG);
821 sc->sc_regs.erstsz0 = xhci_rt_read_4(sc, XHCI_ERSTSZ(0)); 858 sc->sc_regs.erstsz0 = xhci_rt_read_4(sc, XHCI_ERSTSZ(0));
822 sc->sc_regs.erstba0 = xhci_rt_read_8(sc, XHCI_ERSTBA(0)); 859 sc->sc_regs.erstba0 = xhci_rt_read_8(sc, XHCI_ERSTBA(0));
823 sc->sc_regs.erdp0 = xhci_rt_read_8(sc, XHCI_ERDP(0)); 860 sc->sc_regs.erdp0 = xhci_rt_read_8(sc, XHCI_ERDP(0));
824 sc->sc_regs.iman0 = xhci_rt_read_4(sc, XHCI_IMAN(0)); 861 sc->sc_regs.iman0 = xhci_rt_read_4(sc, XHCI_IMAN(0));
825 sc->sc_regs.imod0 = xhci_rt_read_4(sc, XHCI_IMOD(0)); 862 sc->sc_regs.imod0 = xhci_rt_read_4(sc, XHCI_IMOD(0));
826 863
827 /* 864 /*
828 * `5. Set the Controller Save State (CSS) flag in the USBCMD 865 * `5. Set the Controller Save State (CSS) flag in the USBCMD
829 * register (5.4.1)...' 866 * register (5.4.1)...'
830 */ 867 */
831 xhci_op_write_4(sc, XHCI_USBCMD, 868 xhci_op_write_4(sc, XHCI_USBCMD,
832 xhci_op_read_4(sc, XHCI_USBCMD) | XHCI_CMD_CSS); 869 xhci_op_read_4(sc, XHCI_USBCMD) | XHCI_CMD_CSS);
833 870
834 /* 871 /*
835 * `...and wait for the Save State Status (SSS) flag in the 872 * `...and wait for the Save State Status (SSS) flag in the
836 * USBSTS register (5.4.2) to transition to ``0''.' 873 * USBSTS register (5.4.2) to transition to ``0''.'
837 */ 874 */
838 for (i = 0; i < XHCI_WAIT_SSS; i++) { 875 for (i = 0; i < XHCI_WAIT_SSS; i++) {
839 if ((xhci_op_read_4(sc, XHCI_USBSTS) & XHCI_STS_SSS) == 0) 876 if ((xhci_op_read_4(sc, XHCI_USBSTS) & XHCI_STS_SSS) == 0)
840 break; 877 break;
841 usb_delay_ms(&sc->sc_bus, 1); 878 usb_delay_ms(&sc->sc_bus, 1);
842 } 879 }
843 if (i >= XHCI_WAIT_SSS) { 880 if (i >= XHCI_WAIT_SSS) {
844 device_printf(self, "suspend timeout, USBSTS.SSS\n"); 881 device_printf(self, "suspend timeout, USBSTS.SSS\n");
845 /* 882 /*
846 * Just optimistically go on and check SRE anyway -- 883 * Just optimistically go on and check SRE anyway --
847 * what's the worst that could happen? 884 * what's the worst that could happen?
848 */ 885 */
849 } 886 }
850 887
851 /* 888 /*
852 * `Note: After a Save or Restore operation completes, the 889 * `Note: After a Save or Restore operation completes, the
853 * Save/Restore Error (SRE) flag in the USBSTS register should 890 * Save/Restore Error (SRE) flag in the USBSTS register should
854 * be checked to ensure that the operation completed 891 * be checked to ensure that the operation completed
855 * successfully.' 892 * successfully.'
856 */ 893 */
857 if (xhci_op_read_4(sc, XHCI_USBSTS) & XHCI_STS_SRE) { 894 if (xhci_op_read_4(sc, XHCI_USBSTS) & XHCI_STS_SRE) {
858 device_printf(self, "suspend error, USBSTS.SRE\n"); 895 device_printf(self, "suspend error, USBSTS.SRE\n");
859 return false; 896 goto out;
860 } 897 }
861 898
862 return true; 899 /* Success! */
 900 ok = true;
 901
 902out: mutex_exit(&sc->sc_lock);
 903 return ok;
863} 904}
864 905
865bool 906bool
866xhci_resume(device_t self, const pmf_qual_t *qual) 907xhci_resume(device_t self, const pmf_qual_t *qual)
867{ 908{
868 struct xhci_softc * const sc = device_private(self); 909 struct xhci_softc * const sc = device_private(self);
869 size_t i, j, bn, dci; 910 size_t i, j, bn, dci;
870 int port; 911 int port;
871 uint32_t v; 912 uint32_t v;
 913 bool ok = false;
872 914
873 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 915 XHCIHIST_FUNC(); XHCIHIST_CALLED();
874 916
 917 mutex_enter(&sc->sc_lock);
 918 KASSERT(sc->sc_suspender);
 919
875 /* 920 /*
876 * xHCI Requirements Specification 1.2, May 2019, Sec. 4.23.2: 921 * xHCI Requirements Specification 1.2, May 2019, Sec. 4.23.2:
877 * xHCI Power Management, p. 343 922 * xHCI Power Management, p. 343
878 * https://www.intel.com/content/dam/www/public/us/en/documents/technical-specifications/extensible-host-controler-interface-usb-xhci.pdf#page=343 923 * https://www.intel.com/content/dam/www/public/us/en/documents/technical-specifications/extensible-host-controler-interface-usb-xhci.pdf#page=343
879 */ 924 */
880 925
881 /* 926 /*
882 * `4. Restore the Operational Runtime, and VTIO registers with 927 * `4. Restore the Operational Runtime, and VTIO registers with
883 * their previously saved state in the following order: 928 * their previously saved state in the following order:
884 * DNCTRL, DCBAAP, CONFIG, ERSTSZ, ERSTBA, ERDP, IMAN, 929 * DNCTRL, DCBAAP, CONFIG, ERSTSZ, ERSTBA, ERDP, IMAN,
885 * IMOD, and VTIO.' 930 * IMOD, and VTIO.'
886 * 931 *
887 * (We don't use VTIO here (for now?).) 932 * (We don't use VTIO here (for now?).)
888 */ 933 */
889 xhci_op_write_4(sc, XHCI_USBCMD, sc->sc_regs.usbcmd); 934 xhci_op_write_4(sc, XHCI_USBCMD, sc->sc_regs.usbcmd);
890 xhci_op_write_4(sc, XHCI_DNCTRL, sc->sc_regs.dnctrl); 935 xhci_op_write_4(sc, XHCI_DNCTRL, sc->sc_regs.dnctrl);
891 xhci_op_write_8(sc, XHCI_DCBAAP, sc->sc_regs.dcbaap); 936 xhci_op_write_8(sc, XHCI_DCBAAP, sc->sc_regs.dcbaap);
892 xhci_op_write_4(sc, XHCI_CONFIG, sc->sc_regs.config); 937 xhci_op_write_4(sc, XHCI_CONFIG, sc->sc_regs.config);
893 xhci_rt_write_4(sc, XHCI_ERSTSZ(0), sc->sc_regs.erstsz0); 938 xhci_rt_write_4(sc, XHCI_ERSTSZ(0), sc->sc_regs.erstsz0);
894 xhci_rt_write_8(sc, XHCI_ERSTBA(0), sc->sc_regs.erstba0); 939 xhci_rt_write_8(sc, XHCI_ERSTBA(0), sc->sc_regs.erstba0);
895 xhci_rt_write_8(sc, XHCI_ERDP(0), sc->sc_regs.erdp0); 940 xhci_rt_write_8(sc, XHCI_ERDP(0), sc->sc_regs.erdp0);
896 xhci_rt_write_4(sc, XHCI_IMAN(0), sc->sc_regs.iman0); 941 xhci_rt_write_4(sc, XHCI_IMAN(0), sc->sc_regs.iman0);
897 xhci_rt_write_4(sc, XHCI_IMOD(0), sc->sc_regs.imod0); 942 xhci_rt_write_4(sc, XHCI_IMOD(0), sc->sc_regs.imod0);
898 943
899 memset(&sc->sc_regs, 0, sizeof(sc->sc_regs)); /* paranoia */ 944 memset(&sc->sc_regs, 0, sizeof(sc->sc_regs)); /* paranoia */
900 945
901 /* 946 /*
902 * `5. Set the Controller Restore State (CRS) flag in the 947 * `5. Set the Controller Restore State (CRS) flag in the
903 * USBCMD register (5.4.1) to ``1''...' 948 * USBCMD register (5.4.1) to ``1''...'
904 */ 949 */
905 xhci_op_write_4(sc, XHCI_USBCMD, 950 xhci_op_write_4(sc, XHCI_USBCMD,
906 xhci_op_read_4(sc, XHCI_USBCMD) | XHCI_CMD_CRS); 951 xhci_op_read_4(sc, XHCI_USBCMD) | XHCI_CMD_CRS);
907 952
908 /* 953 /*
909 * `...and wait for the Restore State Status (RSS) in the 954 * `...and wait for the Restore State Status (RSS) in the
910 * USBSTS register (5.4.2) to transition to ``0''.' 955 * USBSTS register (5.4.2) to transition to ``0''.'
911 */ 956 */
912 for (i = 0; i < XHCI_WAIT_RSS; i++) { 957 for (i = 0; i < XHCI_WAIT_RSS; i++) {
913 if ((xhci_op_read_4(sc, XHCI_USBSTS) & XHCI_STS_RSS) == 0) 958 if ((xhci_op_read_4(sc, XHCI_USBSTS) & XHCI_STS_RSS) == 0)
914 break; 959 break;
915 usb_delay_ms(&sc->sc_bus, 1); 960 usb_delay_ms(&sc->sc_bus, 1);
916 } 961 }
917 if (i >= XHCI_WAIT_RSS) { 962 if (i >= XHCI_WAIT_RSS) {
918 device_printf(self, "suspend timeout, USBSTS.RSS\n"); 963 device_printf(self, "suspend timeout, USBSTS.RSS\n");
919 return false; 964 goto out;
920 } 965 }
921 966
922 /* 967 /*
923 * `6. Reinitialize the Command Ring, i.e. so its Cycle bits 968 * `6. Reinitialize the Command Ring, i.e. so its Cycle bits
924 * are consistent with the RCS values to be written to the 969 * are consistent with the RCS values to be written to the
925 * CRCR.' 970 * CRCR.'
926 * 971 *
927 * XXX Hope just zeroing it is good enough! 972 * XXX Hope just zeroing it is good enough!
928 */ 973 */
929 xhci_host_dequeue(sc->sc_cr); 974 xhci_host_dequeue(sc->sc_cr);
930 975
931 /* 976 /*
932 * `7. Write the CRCR with the address and RCS value of the 977 * `7. Write the CRCR with the address and RCS value of the
933 * reinitialized Command Ring. Note that this write will 978 * reinitialized Command Ring. Note that this write will
934 * cause the Command Ring to restart at the address 979 * cause the Command Ring to restart at the address
935 * specified by the CRCR.' 980 * specified by the CRCR.'
936 */ 981 */
937 xhci_op_write_8(sc, XHCI_CRCR, xhci_ring_trbp(sc->sc_cr, 0) | 982 xhci_op_write_8(sc, XHCI_CRCR, xhci_ring_trbp(sc->sc_cr, 0) |
938 sc->sc_cr->xr_cs); 983 sc->sc_cr->xr_cs);
939 984
940 /* 985 /*
941 * `8. Enable the controller by setting Run/Stop (R/S) = 986 * `8. Enable the controller by setting Run/Stop (R/S) =
942 * ``1''.' 987 * ``1''.'
943 */ 988 */
944 xhci_op_write_4(sc, XHCI_USBCMD, 989 xhci_op_write_4(sc, XHCI_USBCMD,
945 xhci_op_read_4(sc, XHCI_USBCMD) | XHCI_CMD_RS); 990 xhci_op_read_4(sc, XHCI_USBCMD) | XHCI_CMD_RS);
946 991
947 /* 992 /*
948 * `9. Software shall walk the USB topology and initialize each 993 * `9. Software shall walk the USB topology and initialize each
949 * of the xHC PORTSC, PORTPMSC, and PORTLI registers, and 994 * of the xHC PORTSC, PORTPMSC, and PORTLI registers, and
950 * external hub ports attached to USB devices.' 995 * external hub ports attached to USB devices.'
951 * 996 *
952 * This follows the procedure in 4.15 `Suspend-Resume', 4.15.2 997 * This follows the procedure in 4.15 `Suspend-Resume', 4.15.2
953 * `Port Resume', 4.15.2.1 `Host Initiated'. 998 * `Port Resume', 4.15.2.1 `Host Initiated'.
954 * 999 *
955 * XXX We should maybe batch up initiating the state 1000 * XXX We should maybe batch up initiating the state
956 * transitions, and then wait for them to complete all at once. 1001 * transitions, and then wait for them to complete all at once.
957 */ 1002 */
958 for (bn = 0; bn < 2; bn++) { 1003 for (bn = 0; bn < 2; bn++) {
959 for (i = 1; i <= sc->sc_rhportcount[bn]; i++) { 1004 for (i = 1; i <= sc->sc_rhportcount[bn]; i++) {
960 port = XHCI_PORTSC(xhci_rhport2ctlrport(sc, bn, i)); 1005 port = XHCI_PORTSC(xhci_rhport2ctlrport(sc, bn, i));
961 1006
962 /* `When a port is in the U3 state: ...' */ 1007 /* `When a port is in the U3 state: ...' */
963 v = xhci_op_read_4(sc, port); 1008 v = xhci_op_read_4(sc, port);
964 if (XHCI_PS_PLS_GET(v) != XHCI_PS_PLS_U3) 1009 if (XHCI_PS_PLS_GET(v) != XHCI_PS_PLS_U3)
965 continue; 1010 continue;
966 1011
967 /* 1012 /*
968 * `For a USB2 protocol port, software shall 1013 * `For a USB2 protocol port, software shall
969 * write a ``15'' (Resume) to the PLS field to 1014 * write a ``15'' (Resume) to the PLS field to
970 * initiate resume signaling. The port shall 1015 * initiate resume signaling. The port shall
971 * transition to the Resume substate and the 1016 * transition to the Resume substate and the
972 * xHC shall transmit the resume signaling 1017 * xHC shall transmit the resume signaling
973 * within 1ms (T_URSM). Software shall ensure 1018 * within 1ms (T_URSM). Software shall ensure
974 * that resume is signaled for at least 20ms 1019 * that resume is signaled for at least 20ms
975 * (T_DRSMDN). Software shall start timing 1020 * (T_DRSMDN). Software shall start timing
976 * T_DRSMDN from the write of ``15'' (Resume) 1021 * T_DRSMDN from the write of ``15'' (Resume)
977 * to PLS.' 1022 * to PLS.'
978 */ 1023 */
979 if (bn == 1) { 1024 if (bn == 1) {
980 KASSERT(sc->sc_bus2.ub_revision == USBREV_2_0); 1025 KASSERT(sc->sc_bus2.ub_revision == USBREV_2_0);
981 v &= ~(XHCI_PS_PLS_MASK | XHCI_PS_CLEAR); 1026 v &= ~(XHCI_PS_PLS_MASK | XHCI_PS_CLEAR);
982 v |= XHCI_PS_LWS; 1027 v |= XHCI_PS_LWS;
983 v |= XHCI_PS_PLS_SET(XHCI_PS_PLS_SETRESUME); 1028 v |= XHCI_PS_PLS_SET(XHCI_PS_PLS_SETRESUME);
984 xhci_op_write_4(sc, port, v); 1029 xhci_op_write_4(sc, port, v);
985 usb_delay_ms(&sc->sc_bus, 20); 1030 usb_delay_ms(&sc->sc_bus, 20);
986 } else { 1031 } else {
987 KASSERT(sc->sc_bus.ub_revision > USBREV_2_0); 1032 KASSERT(sc->sc_bus.ub_revision > USBREV_2_0);
988 } 1033 }
989 1034
990 /* 1035 /*
991 * `For a USB3 protocol port [and a USB2 1036 * `For a USB3 protocol port [and a USB2
992 * protocol port after transitioning to 1037 * protocol port after transitioning to
993 * Resume], software shall write a ``0'' (U0) 1038 * Resume], software shall write a ``0'' (U0)
994 * to the PLS field...' 1039 * to the PLS field...'
995 */ 1040 */
996 v = xhci_op_read_4(sc, port); 1041 v = xhci_op_read_4(sc, port);
997 v &= ~(XHCI_PS_PLS_MASK | XHCI_PS_CLEAR); 1042 v &= ~(XHCI_PS_PLS_MASK | XHCI_PS_CLEAR);
998 v |= XHCI_PS_LWS | XHCI_PS_PLS_SET(XHCI_PS_PLS_SETU0); 1043 v |= XHCI_PS_LWS | XHCI_PS_PLS_SET(XHCI_PS_PLS_SETU0);
999 xhci_op_write_4(sc, port, v); 1044 xhci_op_write_4(sc, port, v);
1000 1045
1001 for (j = 0; j < XHCI_WAIT_PLS_U0; j++) { 1046 for (j = 0; j < XHCI_WAIT_PLS_U0; j++) {
1002 v = xhci_op_read_4(sc, port); 1047 v = xhci_op_read_4(sc, port);
1003 if (XHCI_PS_PLS_GET(v) == XHCI_PS_PLS_U0) 1048 if (XHCI_PS_PLS_GET(v) == XHCI_PS_PLS_U0)
1004 break; 1049 break;
1005 usb_delay_ms(&sc->sc_bus, 1); 1050 usb_delay_ms(&sc->sc_bus, 1);
1006 } 1051 }
1007 if (j == XHCI_WAIT_PLS_U0) { 1052 if (j == XHCI_WAIT_PLS_U0) {
1008 device_printf(self, 1053 device_printf(self,
1009 "resume timeout on bus %zu port %zu\n", 1054 "resume timeout on bus %zu port %zu\n",
1010 bn, i); 1055 bn, i);
1011 return false; 1056 goto out;
1012 } 1057 }
1013 } 1058 }
1014 } 1059 }
1015 1060
1016 /* 1061 /*
1017 * `10. Restart each of the previously Running endpoints by 1062 * `10. Restart each of the previously Running endpoints by
1018 * ringing their doorbells.' 1063 * ringing their doorbells.'
1019 */ 1064 */
1020 for (i = 0; i < sc->sc_maxslots; i++) { 1065 for (i = 0; i < sc->sc_maxslots; i++) {
1021 struct xhci_slot *xs = &sc->sc_slots[i]; 1066 struct xhci_slot *xs = &sc->sc_slots[i];
1022 1067
1023 /* Skip if the slot is not in use. */ 1068 /* Skip if the slot is not in use. */
1024 if (xs->xs_idx == 0) 1069 if (xs->xs_idx == 0)
1025 continue; 1070 continue;
1026 1071
1027 for (dci = XHCI_DCI_SLOT; dci <= XHCI_MAX_DCI; dci++) { 1072 for (dci = XHCI_DCI_SLOT; dci <= XHCI_MAX_DCI; dci++) {
1028 /* Skip if the endpoint is not Running. */ 1073 /* Skip if the endpoint is not Running. */
1029 if (xhci_get_epstate(sc, xs, dci) != 1074 if (xhci_get_epstate(sc, xs, dci) !=
1030 XHCI_EPSTATE_RUNNING) 1075 XHCI_EPSTATE_RUNNING)
1031 continue; 1076 continue;
1032 1077
1033 /* Ring the doorbell. */ 1078 /* Ring the doorbell. */
1034 xhci_db_write_4(sc, XHCI_DOORBELL(xs->xs_idx), dci); 1079 xhci_db_write_4(sc, XHCI_DOORBELL(xs->xs_idx), dci);
1035 } 1080 }
1036 } 1081 }
1037 1082
1038 /* 1083 /*
1039 * `Note: After a Save or Restore operation completes, the 1084 * `Note: After a Save or Restore operation completes, the
1040 * Save/Restore Error (SRE) flag in the USBSTS register should 1085 * Save/Restore Error (SRE) flag in the USBSTS register should
1041 * be checked to ensure that the operation completed 1086 * be checked to ensure that the operation completed
1042 * successfully.' 1087 * successfully.'
1043 */ 1088 */
1044 if (xhci_op_read_4(sc, XHCI_USBSTS) & XHCI_STS_SRE) { 1089 if (xhci_op_read_4(sc, XHCI_USBSTS) & XHCI_STS_SRE) {
1045 device_printf(self, "resume error, USBSTS.SRE\n"); 1090 device_printf(self, "resume error, USBSTS.SRE\n");
1046 return false; 1091 goto out;
1047 } 1092 }
1048 1093
1049 return true; 1094 /* Resume command issuance. */
 1095 sc->sc_suspender = NULL;
 1096 cv_broadcast(&sc->sc_cmdbusy_cv);
 1097
 1098 /* Success! */
 1099 ok = true;
 1100
 1101out: mutex_exit(&sc->sc_lock);
 1102 return ok;
1050} 1103}
1051 1104
1052bool 1105bool
1053xhci_shutdown(device_t self, int flags) 1106xhci_shutdown(device_t self, int flags)
1054{ 1107{
1055 return false; 1108 return false;
1056} 1109}
1057 1110
1058static int 1111static int
1059xhci_hc_reset(struct xhci_softc * const sc) 1112xhci_hc_reset(struct xhci_softc * const sc)
1060{ 1113{
1061 uint32_t usbcmd, usbsts; 1114 uint32_t usbcmd, usbsts;
1062 int i; 1115 int i;
1063 1116
1064 /* Check controller not ready */ 1117 /* Check controller not ready */
1065 for (i = 0; i < XHCI_WAIT_CNR; i++) { 1118 for (i = 0; i < XHCI_WAIT_CNR; i++) {
1066 usbsts = xhci_op_read_4(sc, XHCI_USBSTS); 1119 usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
1067 if ((usbsts & XHCI_STS_CNR) == 0) 1120 if ((usbsts & XHCI_STS_CNR) == 0)
1068 break; 1121 break;
1069 usb_delay_ms(&sc->sc_bus, 1); 1122 usb_delay_ms(&sc->sc_bus, 1);
1070 } 1123 }
1071 if (i >= XHCI_WAIT_CNR) { 1124 if (i >= XHCI_WAIT_CNR) {
1072 aprint_error_dev(sc->sc_dev, "controller not ready timeout\n"); 1125 aprint_error_dev(sc->sc_dev, "controller not ready timeout\n");
1073 return EIO; 1126 return EIO;
1074 } 1127 }
1075 1128
1076 /* Halt controller */ 1129 /* Halt controller */
1077 usbcmd = 0; 1130 usbcmd = 0;
1078 xhci_op_write_4(sc, XHCI_USBCMD, usbcmd); 1131 xhci_op_write_4(sc, XHCI_USBCMD, usbcmd);
1079 usb_delay_ms(&sc->sc_bus, 1); 1132 usb_delay_ms(&sc->sc_bus, 1);
1080 1133
1081 /* Reset controller */ 1134 /* Reset controller */
1082 usbcmd = XHCI_CMD_HCRST; 1135 usbcmd = XHCI_CMD_HCRST;
1083 xhci_op_write_4(sc, XHCI_USBCMD, usbcmd); 1136 xhci_op_write_4(sc, XHCI_USBCMD, usbcmd);
1084 for (i = 0; i < XHCI_WAIT_HCRST; i++) { 1137 for (i = 0; i < XHCI_WAIT_HCRST; i++) {
1085 /* 1138 /*
1086 * Wait 1ms first. Existing Intel xHCI requies 1ms delay to 1139 * Wait 1ms first. Existing Intel xHCI requies 1ms delay to
1087 * prevent system hang (Errata). 1140 * prevent system hang (Errata).
1088 */ 1141 */
1089 usb_delay_ms(&sc->sc_bus, 1); 1142 usb_delay_ms(&sc->sc_bus, 1);
1090 usbcmd = xhci_op_read_4(sc, XHCI_USBCMD); 1143 usbcmd = xhci_op_read_4(sc, XHCI_USBCMD);
1091 if ((usbcmd & XHCI_CMD_HCRST) == 0) 1144 if ((usbcmd & XHCI_CMD_HCRST) == 0)
1092 break; 1145 break;
1093 } 1146 }
1094 if (i >= XHCI_WAIT_HCRST) { 1147 if (i >= XHCI_WAIT_HCRST) {
1095 aprint_error_dev(sc->sc_dev, "host controller reset timeout\n"); 1148 aprint_error_dev(sc->sc_dev, "host controller reset timeout\n");
1096 return EIO; 1149 return EIO;
1097 } 1150 }
1098 1151
1099 /* Check controller not ready */ 1152 /* Check controller not ready */
1100 for (i = 0; i < XHCI_WAIT_CNR; i++) { 1153 for (i = 0; i < XHCI_WAIT_CNR; i++) {
1101 usbsts = xhci_op_read_4(sc, XHCI_USBSTS); 1154 usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
1102 if ((usbsts & XHCI_STS_CNR) == 0) 1155 if ((usbsts & XHCI_STS_CNR) == 0)
1103 break; 1156 break;
1104 usb_delay_ms(&sc->sc_bus, 1); 1157 usb_delay_ms(&sc->sc_bus, 1);
1105 } 1158 }
1106 if (i >= XHCI_WAIT_CNR) { 1159 if (i >= XHCI_WAIT_CNR) {
1107 aprint_error_dev(sc->sc_dev, 1160 aprint_error_dev(sc->sc_dev,
1108 "controller not ready timeout after reset\n"); 1161 "controller not ready timeout after reset\n");
1109 return EIO; 1162 return EIO;
1110 } 1163 }
1111 1164
1112 return 0; 1165 return 0;
1113} 1166}
1114 1167
1115/* 7.2 xHCI Support Protocol Capability */ 1168/* 7.2 xHCI Support Protocol Capability */
1116static void 1169static void
1117xhci_id_protocols(struct xhci_softc *sc, bus_size_t ecp) 1170xhci_id_protocols(struct xhci_softc *sc, bus_size_t ecp)
1118{ 1171{
1119 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 1172 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1120 1173
1121 /* XXX Cache this lot */ 1174 /* XXX Cache this lot */
1122 1175
1123 const uint32_t w0 = xhci_read_4(sc, ecp); 1176 const uint32_t w0 = xhci_read_4(sc, ecp);
1124 const uint32_t w4 = xhci_read_4(sc, ecp + 4); 1177 const uint32_t w4 = xhci_read_4(sc, ecp + 4);
1125 const uint32_t w8 = xhci_read_4(sc, ecp + 8); 1178 const uint32_t w8 = xhci_read_4(sc, ecp + 8);
1126 const uint32_t wc = xhci_read_4(sc, ecp + 0xc); 1179 const uint32_t wc = xhci_read_4(sc, ecp + 0xc);
1127 1180
1128 aprint_debug_dev(sc->sc_dev, 1181 aprint_debug_dev(sc->sc_dev,
1129 " SP: 0x%08x 0x%08x 0x%08x 0x%08x\n", w0, w4, w8, wc); 1182 " SP: 0x%08x 0x%08x 0x%08x 0x%08x\n", w0, w4, w8, wc);
1130 1183
1131 if (w4 != XHCI_XECP_USBID) 1184 if (w4 != XHCI_XECP_USBID)
1132 return; 1185 return;
1133 1186
1134 const int major = XHCI_XECP_SP_W0_MAJOR(w0); 1187 const int major = XHCI_XECP_SP_W0_MAJOR(w0);
1135 const int minor = XHCI_XECP_SP_W0_MINOR(w0); 1188 const int minor = XHCI_XECP_SP_W0_MINOR(w0);
1136 const uint8_t cpo = XHCI_XECP_SP_W8_CPO(w8); 1189 const uint8_t cpo = XHCI_XECP_SP_W8_CPO(w8);
1137 const uint8_t cpc = XHCI_XECP_SP_W8_CPC(w8); 1190 const uint8_t cpc = XHCI_XECP_SP_W8_CPC(w8);
1138 1191
1139 const uint16_t mm = __SHIFTOUT(w0, __BITS(31, 16)); 1192 const uint16_t mm = __SHIFTOUT(w0, __BITS(31, 16));
1140 switch (mm) { 1193 switch (mm) {
1141 case 0x0200: 1194 case 0x0200:
1142 case 0x0300: 1195 case 0x0300:
1143 case 0x0301: 1196 case 0x0301:
1144 case 0x0310: 1197 case 0x0310:
1145 aprint_debug_dev(sc->sc_dev, " %s ports %d - %d\n", 1198 aprint_debug_dev(sc->sc_dev, " %s ports %d - %d\n",
1146 major == 3 ? "ss" : "hs", cpo, cpo + cpc -1); 1199 major == 3 ? "ss" : "hs", cpo, cpo + cpc -1);
1147 break; 1200 break;
1148 default: 1201 default:
1149 aprint_error_dev(sc->sc_dev, " unknown major/minor (%d/%d)\n", 1202 aprint_error_dev(sc->sc_dev, " unknown major/minor (%d/%d)\n",
1150 major, minor); 1203 major, minor);
1151 return; 1204 return;
1152 } 1205 }
1153 1206
1154 const size_t bus = (major == 3) ? 0 : 1; 1207 const size_t bus = (major == 3) ? 0 : 1;
1155 1208
1156 /* Index arrays with 0..n-1 where ports are numbered 1..n */ 1209 /* Index arrays with 0..n-1 where ports are numbered 1..n */
1157 for (size_t cp = cpo - 1; cp < cpo + cpc - 1; cp++) { 1210 for (size_t cp = cpo - 1; cp < cpo + cpc - 1; cp++) {
1158 if (sc->sc_ctlrportmap[cp] != 0) { 1211 if (sc->sc_ctlrportmap[cp] != 0) {
1159 aprint_error_dev(sc->sc_dev, "controller port %zu " 1212 aprint_error_dev(sc->sc_dev, "controller port %zu "
1160 "already assigned", cp); 1213 "already assigned", cp);
1161 continue; 1214 continue;
1162 } 1215 }
1163 1216
1164 sc->sc_ctlrportbus[cp / NBBY] |= 1217 sc->sc_ctlrportbus[cp / NBBY] |=
1165 bus == 0 ? 0 : __BIT(cp % NBBY); 1218 bus == 0 ? 0 : __BIT(cp % NBBY);
1166 1219
1167 const size_t rhp = sc->sc_rhportcount[bus]++; 1220 const size_t rhp = sc->sc_rhportcount[bus]++;
1168 1221
1169 KASSERTMSG(sc->sc_rhportmap[bus][rhp] == 0, 1222 KASSERTMSG(sc->sc_rhportmap[bus][rhp] == 0,
1170 "bus %zu rhp %zu is %d", bus, rhp, 1223 "bus %zu rhp %zu is %d", bus, rhp,
1171 sc->sc_rhportmap[bus][rhp]); 1224 sc->sc_rhportmap[bus][rhp]);
1172 1225
1173 sc->sc_rhportmap[bus][rhp] = cp + 1; 1226 sc->sc_rhportmap[bus][rhp] = cp + 1;
1174 sc->sc_ctlrportmap[cp] = rhp + 1; 1227 sc->sc_ctlrportmap[cp] = rhp + 1;
1175 } 1228 }
1176} 1229}
1177 1230
1178/* Process extended capabilities */ 1231/* Process extended capabilities */
1179static void 1232static void
1180xhci_ecp(struct xhci_softc *sc) 1233xhci_ecp(struct xhci_softc *sc)
1181{ 1234{
1182 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 1235 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1183 1236
1184 bus_size_t ecp = XHCI_HCC_XECP(sc->sc_hcc) * 4; 1237 bus_size_t ecp = XHCI_HCC_XECP(sc->sc_hcc) * 4;
1185 while (ecp != 0) { 1238 while (ecp != 0) {
1186 uint32_t ecr = xhci_read_4(sc, ecp); 1239 uint32_t ecr = xhci_read_4(sc, ecp);
1187 aprint_debug_dev(sc->sc_dev, "ECR: 0x%08x\n", ecr); 1240 aprint_debug_dev(sc->sc_dev, "ECR: 0x%08x\n", ecr);
1188 switch (XHCI_XECP_ID(ecr)) { 1241 switch (XHCI_XECP_ID(ecr)) {
1189 case XHCI_ID_PROTOCOLS: { 1242 case XHCI_ID_PROTOCOLS: {
1190 xhci_id_protocols(sc, ecp); 1243 xhci_id_protocols(sc, ecp);
1191 break; 1244 break;
1192 } 1245 }
1193 case XHCI_ID_USB_LEGACY: { 1246 case XHCI_ID_USB_LEGACY: {
1194 uint8_t bios_sem; 1247 uint8_t bios_sem;
1195 1248
1196 /* Take host controller ownership from BIOS */ 1249 /* Take host controller ownership from BIOS */
1197 bios_sem = xhci_read_1(sc, ecp + XHCI_XECP_BIOS_SEM); 1250 bios_sem = xhci_read_1(sc, ecp + XHCI_XECP_BIOS_SEM);
1198 if (bios_sem) { 1251 if (bios_sem) {
1199 /* sets xHCI to be owned by OS */ 1252 /* sets xHCI to be owned by OS */
1200 xhci_write_1(sc, ecp + XHCI_XECP_OS_SEM, 1); 1253 xhci_write_1(sc, ecp + XHCI_XECP_OS_SEM, 1);
1201 aprint_debug_dev(sc->sc_dev, 1254 aprint_debug_dev(sc->sc_dev,
1202 "waiting for BIOS to give up control\n"); 1255 "waiting for BIOS to give up control\n");
1203 for (int i = 0; i < 5000; i++) { 1256 for (int i = 0; i < 5000; i++) {
1204 bios_sem = xhci_read_1(sc, ecp + 1257 bios_sem = xhci_read_1(sc, ecp +
1205 XHCI_XECP_BIOS_SEM); 1258 XHCI_XECP_BIOS_SEM);
1206 if (bios_sem == 0) 1259 if (bios_sem == 0)
1207 break; 1260 break;
1208 DELAY(1000); 1261 DELAY(1000);
1209 } 1262 }
1210 if (bios_sem) { 1263 if (bios_sem) {
1211 aprint_error_dev(sc->sc_dev, 1264 aprint_error_dev(sc->sc_dev,
1212 "timed out waiting for BIOS\n"); 1265 "timed out waiting for BIOS\n");
1213 } 1266 }
1214 } 1267 }
1215 break; 1268 break;
1216 } 1269 }
1217 default: 1270 default:
1218 break; 1271 break;
1219 } 1272 }
1220 ecr = xhci_read_4(sc, ecp); 1273 ecr = xhci_read_4(sc, ecp);
1221 if (XHCI_XECP_NEXT(ecr) == 0) { 1274 if (XHCI_XECP_NEXT(ecr) == 0) {
1222 ecp = 0; 1275 ecp = 0;
1223 } else { 1276 } else {
1224 ecp += XHCI_XECP_NEXT(ecr) * 4; 1277 ecp += XHCI_XECP_NEXT(ecr) * 4;
1225 } 1278 }
1226 } 1279 }
1227} 1280}
1228 1281
1229#define XHCI_HCCPREV1_BITS \ 1282#define XHCI_HCCPREV1_BITS \
1230 "\177\020" /* New bitmask */ \ 1283 "\177\020" /* New bitmask */ \
1231 "f\020\020XECP\0" \ 1284 "f\020\020XECP\0" \
1232 "f\014\4MAXPSA\0" \ 1285 "f\014\4MAXPSA\0" \
1233 "b\013CFC\0" \ 1286 "b\013CFC\0" \
1234 "b\012SEC\0" \ 1287 "b\012SEC\0" \
1235 "b\011SBD\0" \ 1288 "b\011SBD\0" \
1236 "b\010FSE\0" \ 1289 "b\010FSE\0" \
1237 "b\7NSS\0" \ 1290 "b\7NSS\0" \
1238 "b\6LTC\0" \ 1291 "b\6LTC\0" \
1239 "b\5LHRC\0" \ 1292 "b\5LHRC\0" \
1240 "b\4PIND\0" \ 1293 "b\4PIND\0" \
1241 "b\3PPC\0" \ 1294 "b\3PPC\0" \
1242 "b\2CZC\0" \ 1295 "b\2CZC\0" \
1243 "b\1BNC\0" \ 1296 "b\1BNC\0" \
1244 "b\0AC64\0" \ 1297 "b\0AC64\0" \
1245 "\0" 1298 "\0"
1246#define XHCI_HCCV1_x_BITS \ 1299#define XHCI_HCCV1_x_BITS \
1247 "\177\020" /* New bitmask */ \ 1300 "\177\020" /* New bitmask */ \
1248 "f\020\020XECP\0" \ 1301 "f\020\020XECP\0" \
1249 "f\014\4MAXPSA\0" \ 1302 "f\014\4MAXPSA\0" \
1250 "b\013CFC\0" \ 1303 "b\013CFC\0" \
1251 "b\012SEC\0" \ 1304 "b\012SEC\0" \
1252 "b\011SPC\0" \ 1305 "b\011SPC\0" \
1253 "b\010PAE\0" \ 1306 "b\010PAE\0" \
1254 "b\7NSS\0" \ 1307 "b\7NSS\0" \
1255 "b\6LTC\0" \ 1308 "b\6LTC\0" \
1256 "b\5LHRC\0" \ 1309 "b\5LHRC\0" \
1257 "b\4PIND\0" \ 1310 "b\4PIND\0" \
1258 "b\3PPC\0" \ 1311 "b\3PPC\0" \
1259 "b\2CSZ\0" \ 1312 "b\2CSZ\0" \
1260 "b\1BNC\0" \ 1313 "b\1BNC\0" \
1261 "b\0AC64\0" \ 1314 "b\0AC64\0" \
1262 "\0" 1315 "\0"
1263 1316
1264#define XHCI_HCC2_BITS \ 1317#define XHCI_HCC2_BITS \
1265 "\177\020" /* New bitmask */ \ 1318 "\177\020" /* New bitmask */ \
1266 "b\7ETC_TSC\0" \ 1319 "b\7ETC_TSC\0" \
1267 "b\6ETC\0" \ 1320 "b\6ETC\0" \
1268 "b\5CIC\0" \ 1321 "b\5CIC\0" \
1269 "b\4LEC\0" \ 1322 "b\4LEC\0" \
1270 "b\3CTC\0" \ 1323 "b\3CTC\0" \
1271 "b\2FSC\0" \ 1324 "b\2FSC\0" \
1272 "b\1CMC\0" \ 1325 "b\1CMC\0" \
1273 "b\0U3C\0" \ 1326 "b\0U3C\0" \
1274 "\0" 1327 "\0"
1275 1328
1276void 1329void
1277xhci_start(struct xhci_softc *sc) 1330xhci_start(struct xhci_softc *sc)
1278{ 1331{
1279 xhci_rt_write_4(sc, XHCI_IMAN(0), XHCI_IMAN_INTR_ENA); 1332 xhci_rt_write_4(sc, XHCI_IMAN(0), XHCI_IMAN_INTR_ENA);
1280 if ((sc->sc_quirks & XHCI_QUIRK_INTEL) != 0) 1333 if ((sc->sc_quirks & XHCI_QUIRK_INTEL) != 0)
1281 /* Intel xhci needs interrupt rate moderated. */ 1334 /* Intel xhci needs interrupt rate moderated. */
1282 xhci_rt_write_4(sc, XHCI_IMOD(0), XHCI_IMOD_DEFAULT_LP); 1335 xhci_rt_write_4(sc, XHCI_IMOD(0), XHCI_IMOD_DEFAULT_LP);
1283 else 1336 else
1284 xhci_rt_write_4(sc, XHCI_IMOD(0), 0); 1337 xhci_rt_write_4(sc, XHCI_IMOD(0), 0);
1285 aprint_debug_dev(sc->sc_dev, "current IMOD %u\n", 1338 aprint_debug_dev(sc->sc_dev, "current IMOD %u\n",
1286 xhci_rt_read_4(sc, XHCI_IMOD(0))); 1339 xhci_rt_read_4(sc, XHCI_IMOD(0)));
1287 1340
1288 /* Go! */ 1341 /* Go! */
1289 xhci_op_write_4(sc, XHCI_USBCMD, XHCI_CMD_INTE|XHCI_CMD_RS); 1342 xhci_op_write_4(sc, XHCI_USBCMD, XHCI_CMD_INTE|XHCI_CMD_RS);
1290 aprint_debug_dev(sc->sc_dev, "USBCMD 0x%08"PRIx32"\n", 1343 aprint_debug_dev(sc->sc_dev, "USBCMD 0x%08"PRIx32"\n",
1291 xhci_op_read_4(sc, XHCI_USBCMD)); 1344 xhci_op_read_4(sc, XHCI_USBCMD));
1292} 1345}
1293 1346
1294int 1347int
1295xhci_init(struct xhci_softc *sc) 1348xhci_init(struct xhci_softc *sc)
1296{ 1349{
1297 bus_size_t bsz; 1350 bus_size_t bsz;
1298 uint32_t hcs1, hcs2, hcs3, dboff, rtsoff; 1351 uint32_t hcs1, hcs2, hcs3, dboff, rtsoff;
1299 uint32_t pagesize, config; 1352 uint32_t pagesize, config;
1300 int i = 0; 1353 int i = 0;
1301 uint16_t hciversion; 1354 uint16_t hciversion;
1302 uint8_t caplength; 1355 uint8_t caplength;
1303 1356
1304 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 1357 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1305 1358
1306 /* Set up the bus struct for the usb 3 and usb 2 buses */ 1359 /* Set up the bus struct for the usb 3 and usb 2 buses */
1307 sc->sc_bus.ub_methods = &xhci_bus_methods; 1360 sc->sc_bus.ub_methods = &xhci_bus_methods;
1308 sc->sc_bus.ub_pipesize = sizeof(struct xhci_pipe); 1361 sc->sc_bus.ub_pipesize = sizeof(struct xhci_pipe);
1309 sc->sc_bus.ub_usedma = true; 1362 sc->sc_bus.ub_usedma = true;
1310 sc->sc_bus.ub_hcpriv = sc; 1363 sc->sc_bus.ub_hcpriv = sc;
1311 1364
1312 sc->sc_bus2.ub_methods = &xhci_bus_methods; 1365 sc->sc_bus2.ub_methods = &xhci_bus_methods;
1313 sc->sc_bus2.ub_pipesize = sizeof(struct xhci_pipe); 1366 sc->sc_bus2.ub_pipesize = sizeof(struct xhci_pipe);
1314 sc->sc_bus2.ub_revision = USBREV_2_0; 1367 sc->sc_bus2.ub_revision = USBREV_2_0;
1315 sc->sc_bus2.ub_usedma = true; 1368 sc->sc_bus2.ub_usedma = true;
1316 sc->sc_bus2.ub_hcpriv = sc; 1369 sc->sc_bus2.ub_hcpriv = sc;
1317 sc->sc_bus2.ub_dmatag = sc->sc_bus.ub_dmatag; 1370 sc->sc_bus2.ub_dmatag = sc->sc_bus.ub_dmatag;
1318 1371
1319 caplength = xhci_read_1(sc, XHCI_CAPLENGTH); 1372 caplength = xhci_read_1(sc, XHCI_CAPLENGTH);
1320 hciversion = xhci_read_2(sc, XHCI_HCIVERSION); 1373 hciversion = xhci_read_2(sc, XHCI_HCIVERSION);
1321 1374
1322 if (hciversion < XHCI_HCIVERSION_0_96 || 1375 if (hciversion < XHCI_HCIVERSION_0_96 ||
1323 hciversion >= 0x0200) { 1376 hciversion >= 0x0200) {
1324 aprint_normal_dev(sc->sc_dev, 1377 aprint_normal_dev(sc->sc_dev,
1325 "xHCI version %x.%x not known to be supported\n", 1378 "xHCI version %x.%x not known to be supported\n",
1326 (hciversion >> 8) & 0xff, (hciversion >> 0) & 0xff); 1379 (hciversion >> 8) & 0xff, (hciversion >> 0) & 0xff);
1327 } else { 1380 } else {
1328 aprint_verbose_dev(sc->sc_dev, "xHCI version %x.%x\n", 1381 aprint_verbose_dev(sc->sc_dev, "xHCI version %x.%x\n",
1329 (hciversion >> 8) & 0xff, (hciversion >> 0) & 0xff); 1382 (hciversion >> 8) & 0xff, (hciversion >> 0) & 0xff);
1330 } 1383 }
1331 1384
1332 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 0, caplength, 1385 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 0, caplength,
1333 &sc->sc_cbh) != 0) { 1386 &sc->sc_cbh) != 0) {
1334 aprint_error_dev(sc->sc_dev, "capability subregion failure\n"); 1387 aprint_error_dev(sc->sc_dev, "capability subregion failure\n");
1335 return ENOMEM; 1388 return ENOMEM;
1336 } 1389 }
1337 1390
1338 hcs1 = xhci_cap_read_4(sc, XHCI_HCSPARAMS1); 1391 hcs1 = xhci_cap_read_4(sc, XHCI_HCSPARAMS1);
1339 sc->sc_maxslots = XHCI_HCS1_MAXSLOTS(hcs1); 1392 sc->sc_maxslots = XHCI_HCS1_MAXSLOTS(hcs1);
1340 sc->sc_maxintrs = XHCI_HCS1_MAXINTRS(hcs1); 1393 sc->sc_maxintrs = XHCI_HCS1_MAXINTRS(hcs1);
1341 sc->sc_maxports = XHCI_HCS1_MAXPORTS(hcs1); 1394 sc->sc_maxports = XHCI_HCS1_MAXPORTS(hcs1);
1342 hcs2 = xhci_cap_read_4(sc, XHCI_HCSPARAMS2); 1395 hcs2 = xhci_cap_read_4(sc, XHCI_HCSPARAMS2);
1343 hcs3 = xhci_cap_read_4(sc, XHCI_HCSPARAMS3); 1396 hcs3 = xhci_cap_read_4(sc, XHCI_HCSPARAMS3);
1344 aprint_debug_dev(sc->sc_dev, 1397 aprint_debug_dev(sc->sc_dev,
1345 "hcs1=%"PRIx32" hcs2=%"PRIx32" hcs3=%"PRIx32"\n", hcs1, hcs2, hcs3); 1398 "hcs1=%"PRIx32" hcs2=%"PRIx32" hcs3=%"PRIx32"\n", hcs1, hcs2, hcs3);
1346 1399
1347 sc->sc_hcc = xhci_cap_read_4(sc, XHCI_HCCPARAMS); 1400 sc->sc_hcc = xhci_cap_read_4(sc, XHCI_HCCPARAMS);
1348 sc->sc_ctxsz = XHCI_HCC_CSZ(sc->sc_hcc) ? 64 : 32; 1401 sc->sc_ctxsz = XHCI_HCC_CSZ(sc->sc_hcc) ? 64 : 32;
1349 1402
1350 char sbuf[128]; 1403 char sbuf[128];
1351 if (hciversion < XHCI_HCIVERSION_1_0) 1404 if (hciversion < XHCI_HCIVERSION_1_0)
1352 snprintb(sbuf, sizeof(sbuf), XHCI_HCCPREV1_BITS, sc->sc_hcc); 1405 snprintb(sbuf, sizeof(sbuf), XHCI_HCCPREV1_BITS, sc->sc_hcc);
1353 else 1406 else
1354 snprintb(sbuf, sizeof(sbuf), XHCI_HCCV1_x_BITS, sc->sc_hcc); 1407 snprintb(sbuf, sizeof(sbuf), XHCI_HCCV1_x_BITS, sc->sc_hcc);
1355 aprint_debug_dev(sc->sc_dev, "hcc=%s\n", sbuf); 1408 aprint_debug_dev(sc->sc_dev, "hcc=%s\n", sbuf);
1356 aprint_debug_dev(sc->sc_dev, "xECP %" __PRIxBITS "\n", 1409 aprint_debug_dev(sc->sc_dev, "xECP %" __PRIxBITS "\n",
1357 XHCI_HCC_XECP(sc->sc_hcc) * 4); 1410 XHCI_HCC_XECP(sc->sc_hcc) * 4);
1358 if (hciversion >= XHCI_HCIVERSION_1_1) { 1411 if (hciversion >= XHCI_HCIVERSION_1_1) {
1359 sc->sc_hcc2 = xhci_cap_read_4(sc, XHCI_HCCPARAMS2); 1412 sc->sc_hcc2 = xhci_cap_read_4(sc, XHCI_HCCPARAMS2);
1360 snprintb(sbuf, sizeof(sbuf), XHCI_HCC2_BITS, sc->sc_hcc2); 1413 snprintb(sbuf, sizeof(sbuf), XHCI_HCC2_BITS, sc->sc_hcc2);
1361 aprint_debug_dev(sc->sc_dev, "hcc2=%s\n", sbuf); 1414 aprint_debug_dev(sc->sc_dev, "hcc2=%s\n", sbuf);
1362 } 1415 }
1363 1416
1364 /* default all ports to bus 0, i.e. usb 3 */ 1417 /* default all ports to bus 0, i.e. usb 3 */
1365 sc->sc_ctlrportbus = kmem_zalloc( 1418 sc->sc_ctlrportbus = kmem_zalloc(
1366 howmany(sc->sc_maxports * sizeof(uint8_t), NBBY), KM_SLEEP); 1419 howmany(sc->sc_maxports * sizeof(uint8_t), NBBY), KM_SLEEP);
1367 sc->sc_ctlrportmap = kmem_zalloc(sc->sc_maxports * sizeof(int), KM_SLEEP); 1420 sc->sc_ctlrportmap = kmem_zalloc(sc->sc_maxports * sizeof(int), KM_SLEEP);
1368 1421
1369 /* controller port to bus roothub port map */ 1422 /* controller port to bus roothub port map */
1370 for (size_t j = 0; j < __arraycount(sc->sc_rhportmap); j++) { 1423 for (size_t j = 0; j < __arraycount(sc->sc_rhportmap); j++) {
1371 sc->sc_rhportmap[j] = kmem_zalloc(sc->sc_maxports * sizeof(int), KM_SLEEP); 1424 sc->sc_rhportmap[j] = kmem_zalloc(sc->sc_maxports * sizeof(int), KM_SLEEP);
1372 } 1425 }
1373 1426
1374 /* 1427 /*
1375 * Process all Extended Capabilities 1428 * Process all Extended Capabilities
1376 */ 1429 */
1377 xhci_ecp(sc); 1430 xhci_ecp(sc);
1378 1431
1379 bsz = XHCI_PORTSC(sc->sc_maxports); 1432 bsz = XHCI_PORTSC(sc->sc_maxports);
1380 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, caplength, bsz, 1433 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, caplength, bsz,
1381 &sc->sc_obh) != 0) { 1434 &sc->sc_obh) != 0) {
1382 aprint_error_dev(sc->sc_dev, "operational subregion failure\n"); 1435 aprint_error_dev(sc->sc_dev, "operational subregion failure\n");
1383 return ENOMEM; 1436 return ENOMEM;
1384 } 1437 }
1385 1438
1386 dboff = xhci_cap_read_4(sc, XHCI_DBOFF); 1439 dboff = xhci_cap_read_4(sc, XHCI_DBOFF);
1387 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, dboff, 1440 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, dboff,
1388 sc->sc_maxslots * 4, &sc->sc_dbh) != 0) { 1441 sc->sc_maxslots * 4, &sc->sc_dbh) != 0) {
1389 aprint_error_dev(sc->sc_dev, "doorbell subregion failure\n"); 1442 aprint_error_dev(sc->sc_dev, "doorbell subregion failure\n");
1390 return ENOMEM; 1443 return ENOMEM;
1391 } 1444 }
1392 1445
1393 rtsoff = xhci_cap_read_4(sc, XHCI_RTSOFF); 1446 rtsoff = xhci_cap_read_4(sc, XHCI_RTSOFF);
1394 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, rtsoff, 1447 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, rtsoff,
1395 sc->sc_maxintrs * 0x20, &sc->sc_rbh) != 0) { 1448 sc->sc_maxintrs * 0x20, &sc->sc_rbh) != 0) {
1396 aprint_error_dev(sc->sc_dev, "runtime subregion failure\n"); 1449 aprint_error_dev(sc->sc_dev, "runtime subregion failure\n");
1397 return ENOMEM; 1450 return ENOMEM;
1398 } 1451 }
1399 1452
1400 int rv; 1453 int rv;
1401 rv = xhci_hc_reset(sc); 1454 rv = xhci_hc_reset(sc);
1402 if (rv != 0) { 1455 if (rv != 0) {
1403 return rv; 1456 return rv;
1404 } 1457 }
1405 1458
1406 if (sc->sc_vendor_init) 1459 if (sc->sc_vendor_init)
1407 sc->sc_vendor_init(sc); 1460 sc->sc_vendor_init(sc);
1408 1461
1409 pagesize = xhci_op_read_4(sc, XHCI_PAGESIZE); 1462 pagesize = xhci_op_read_4(sc, XHCI_PAGESIZE);
1410 aprint_debug_dev(sc->sc_dev, "PAGESIZE 0x%08x\n", pagesize); 1463 aprint_debug_dev(sc->sc_dev, "PAGESIZE 0x%08x\n", pagesize);
1411 pagesize = ffs(pagesize); 1464 pagesize = ffs(pagesize);
1412 if (pagesize == 0) { 1465 if (pagesize == 0) {
1413 aprint_error_dev(sc->sc_dev, "pagesize is 0\n"); 1466 aprint_error_dev(sc->sc_dev, "pagesize is 0\n");
1414 return EIO; 1467 return EIO;
1415 } 1468 }
1416 sc->sc_pgsz = 1 << (12 + (pagesize - 1)); 1469 sc->sc_pgsz = 1 << (12 + (pagesize - 1));
1417 aprint_debug_dev(sc->sc_dev, "sc_pgsz 0x%08x\n", (uint32_t)sc->sc_pgsz); 1470 aprint_debug_dev(sc->sc_dev, "sc_pgsz 0x%08x\n", (uint32_t)sc->sc_pgsz);
1418 aprint_debug_dev(sc->sc_dev, "sc_maxslots 0x%08x\n", 1471 aprint_debug_dev(sc->sc_dev, "sc_maxslots 0x%08x\n",
1419 (uint32_t)sc->sc_maxslots); 1472 (uint32_t)sc->sc_maxslots);
1420 aprint_debug_dev(sc->sc_dev, "sc_maxports %d\n", sc->sc_maxports); 1473 aprint_debug_dev(sc->sc_dev, "sc_maxports %d\n", sc->sc_maxports);
1421 1474
1422 int err; 1475 int err;
1423 sc->sc_maxspbuf = XHCI_HCS2_MAXSPBUF(hcs2); 1476 sc->sc_maxspbuf = XHCI_HCS2_MAXSPBUF(hcs2);
1424 aprint_debug_dev(sc->sc_dev, "sc_maxspbuf %d\n", sc->sc_maxspbuf); 1477 aprint_debug_dev(sc->sc_dev, "sc_maxspbuf %d\n", sc->sc_maxspbuf);
1425 if (sc->sc_maxspbuf != 0) { 1478 if (sc->sc_maxspbuf != 0) {
1426 err = usb_allocmem(&sc->sc_bus, 1479 err = usb_allocmem(&sc->sc_bus,
1427 sizeof(uint64_t) * sc->sc_maxspbuf, sizeof(uint64_t), 1480 sizeof(uint64_t) * sc->sc_maxspbuf, sizeof(uint64_t),
1428 USBMALLOC_COHERENT | USBMALLOC_ZERO, 1481 USBMALLOC_COHERENT | USBMALLOC_ZERO,
1429 &sc->sc_spbufarray_dma); 1482 &sc->sc_spbufarray_dma);
1430 if (err) { 1483 if (err) {
1431 aprint_error_dev(sc->sc_dev, 1484 aprint_error_dev(sc->sc_dev,
1432 "spbufarray init fail, err %d\n", err); 1485 "spbufarray init fail, err %d\n", err);
1433 return ENOMEM; 1486 return ENOMEM;
1434 } 1487 }
1435 1488
1436 sc->sc_spbuf_dma = kmem_zalloc(sizeof(*sc->sc_spbuf_dma) * 1489 sc->sc_spbuf_dma = kmem_zalloc(sizeof(*sc->sc_spbuf_dma) *
1437 sc->sc_maxspbuf, KM_SLEEP); 1490 sc->sc_maxspbuf, KM_SLEEP);
1438 uint64_t *spbufarray = KERNADDR(&sc->sc_spbufarray_dma, 0); 1491 uint64_t *spbufarray = KERNADDR(&sc->sc_spbufarray_dma, 0);
1439 for (i = 0; i < sc->sc_maxspbuf; i++) { 1492 for (i = 0; i < sc->sc_maxspbuf; i++) {
1440 usb_dma_t * const dma = &sc->sc_spbuf_dma[i]; 1493 usb_dma_t * const dma = &sc->sc_spbuf_dma[i];
1441 /* allocate contexts */ 1494 /* allocate contexts */
1442 err = usb_allocmem(&sc->sc_bus, sc->sc_pgsz, 1495 err = usb_allocmem(&sc->sc_bus, sc->sc_pgsz,
1443 sc->sc_pgsz, USBMALLOC_COHERENT | USBMALLOC_ZERO, 1496 sc->sc_pgsz, USBMALLOC_COHERENT | USBMALLOC_ZERO,
1444 dma); 1497 dma);
1445 if (err) { 1498 if (err) {
1446 aprint_error_dev(sc->sc_dev, 1499 aprint_error_dev(sc->sc_dev,
1447 "spbufarray_dma init fail, err %d\n", err); 1500 "spbufarray_dma init fail, err %d\n", err);
1448 rv = ENOMEM; 1501 rv = ENOMEM;
1449 goto bad1; 1502 goto bad1;
1450 } 1503 }
1451 spbufarray[i] = htole64(DMAADDR(dma, 0)); 1504 spbufarray[i] = htole64(DMAADDR(dma, 0));
1452 usb_syncmem(dma, 0, sc->sc_pgsz, 1505 usb_syncmem(dma, 0, sc->sc_pgsz,
1453 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 1506 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1454 } 1507 }
1455 1508
1456 usb_syncmem(&sc->sc_spbufarray_dma, 0, 1509 usb_syncmem(&sc->sc_spbufarray_dma, 0,
1457 sizeof(uint64_t) * sc->sc_maxspbuf, BUS_DMASYNC_PREWRITE); 1510 sizeof(uint64_t) * sc->sc_maxspbuf, BUS_DMASYNC_PREWRITE);
1458 } 1511 }
1459 1512
1460 config = xhci_op_read_4(sc, XHCI_CONFIG); 1513 config = xhci_op_read_4(sc, XHCI_CONFIG);
1461 config &= ~0xFF; 1514 config &= ~0xFF;
1462 config |= sc->sc_maxslots & 0xFF; 1515 config |= sc->sc_maxslots & 0xFF;
1463 xhci_op_write_4(sc, XHCI_CONFIG, config); 1516 xhci_op_write_4(sc, XHCI_CONFIG, config);
1464 1517
1465 err = xhci_ring_init(sc, &sc->sc_cr, XHCI_COMMAND_RING_TRBS, 1518 err = xhci_ring_init(sc, &sc->sc_cr, XHCI_COMMAND_RING_TRBS,
1466 XHCI_COMMAND_RING_SEGMENTS_ALIGN); 1519 XHCI_COMMAND_RING_SEGMENTS_ALIGN);
1467 if (err) { 1520 if (err) {
1468 aprint_error_dev(sc->sc_dev, "command ring init fail, err %d\n", 1521 aprint_error_dev(sc->sc_dev, "command ring init fail, err %d\n",
1469 err); 1522 err);
1470 rv = ENOMEM; 1523 rv = ENOMEM;
1471 goto bad1; 1524 goto bad1;
1472 } 1525 }
1473 1526
1474 err = xhci_ring_init(sc, &sc->sc_er, XHCI_EVENT_RING_TRBS, 1527 err = xhci_ring_init(sc, &sc->sc_er, XHCI_EVENT_RING_TRBS,
1475 XHCI_EVENT_RING_SEGMENTS_ALIGN); 1528 XHCI_EVENT_RING_SEGMENTS_ALIGN);
1476 if (err) { 1529 if (err) {
1477 aprint_error_dev(sc->sc_dev, "event ring init fail, err %d\n", 1530 aprint_error_dev(sc->sc_dev, "event ring init fail, err %d\n",
1478 err); 1531 err);
1479 rv = ENOMEM; 1532 rv = ENOMEM;
1480 goto bad2; 1533 goto bad2;
1481 } 1534 }
1482 1535
1483 usb_dma_t *dma; 1536 usb_dma_t *dma;
1484 size_t size; 1537 size_t size;
1485 size_t align; 1538 size_t align;
1486 1539
1487 dma = &sc->sc_eventst_dma; 1540 dma = &sc->sc_eventst_dma;
1488 size = roundup2(XHCI_EVENT_RING_SEGMENTS * XHCI_ERSTE_SIZE, 1541 size = roundup2(XHCI_EVENT_RING_SEGMENTS * XHCI_ERSTE_SIZE,
1489 XHCI_EVENT_RING_SEGMENT_TABLE_ALIGN); 1542 XHCI_EVENT_RING_SEGMENT_TABLE_ALIGN);
1490 KASSERTMSG(size <= (512 * 1024), "eventst size %zu too large", size); 1543 KASSERTMSG(size <= (512 * 1024), "eventst size %zu too large", size);
1491 align = XHCI_EVENT_RING_SEGMENT_TABLE_ALIGN; 1544 align = XHCI_EVENT_RING_SEGMENT_TABLE_ALIGN;
1492 err = usb_allocmem(&sc->sc_bus, size, align, 1545 err = usb_allocmem(&sc->sc_bus, size, align,
1493 USBMALLOC_COHERENT | USBMALLOC_ZERO, dma); 1546 USBMALLOC_COHERENT | USBMALLOC_ZERO, dma);
1494 if (err) { 1547 if (err) {
1495 aprint_error_dev(sc->sc_dev, "eventst init fail, err %d\n", 1548 aprint_error_dev(sc->sc_dev, "eventst init fail, err %d\n",
1496 err); 1549 err);
1497 rv = ENOMEM; 1550 rv = ENOMEM;
1498 goto bad3; 1551 goto bad3;
1499 } 1552 }
1500 1553
1501 aprint_debug_dev(sc->sc_dev, "eventst: 0x%016jx %p %zx\n", 1554 aprint_debug_dev(sc->sc_dev, "eventst: 0x%016jx %p %zx\n",
1502 (uintmax_t)DMAADDR(&sc->sc_eventst_dma, 0), 1555 (uintmax_t)DMAADDR(&sc->sc_eventst_dma, 0),
1503 KERNADDR(&sc->sc_eventst_dma, 0), 1556 KERNADDR(&sc->sc_eventst_dma, 0),
1504 sc->sc_eventst_dma.udma_block->size); 1557 sc->sc_eventst_dma.udma_block->size);
1505 1558
1506 dma = &sc->sc_dcbaa_dma; 1559 dma = &sc->sc_dcbaa_dma;
1507 size = (1 + sc->sc_maxslots) * sizeof(uint64_t); 1560 size = (1 + sc->sc_maxslots) * sizeof(uint64_t);
1508 KASSERTMSG(size <= 2048, "dcbaa size %zu too large", size); 1561 KASSERTMSG(size <= 2048, "dcbaa size %zu too large", size);
1509 align = XHCI_DEVICE_CONTEXT_BASE_ADDRESS_ARRAY_ALIGN; 1562 align = XHCI_DEVICE_CONTEXT_BASE_ADDRESS_ARRAY_ALIGN;
1510 err = usb_allocmem(&sc->sc_bus, size, align, 1563 err = usb_allocmem(&sc->sc_bus, size, align,
1511 USBMALLOC_COHERENT | USBMALLOC_ZERO, dma); 1564 USBMALLOC_COHERENT | USBMALLOC_ZERO, dma);
1512 if (err) { 1565 if (err) {
1513 aprint_error_dev(sc->sc_dev, "dcbaa init fail, err %d\n", err); 1566 aprint_error_dev(sc->sc_dev, "dcbaa init fail, err %d\n", err);
1514 rv = ENOMEM; 1567 rv = ENOMEM;
1515 goto bad4; 1568 goto bad4;
1516 } 1569 }
1517 aprint_debug_dev(sc->sc_dev, "dcbaa: 0x%016jx %p %zx\n", 1570 aprint_debug_dev(sc->sc_dev, "dcbaa: 0x%016jx %p %zx\n",
1518 (uintmax_t)DMAADDR(&sc->sc_dcbaa_dma, 0), 1571 (uintmax_t)DMAADDR(&sc->sc_dcbaa_dma, 0),
1519 KERNADDR(&sc->sc_dcbaa_dma, 0), 1572 KERNADDR(&sc->sc_dcbaa_dma, 0),
1520 sc->sc_dcbaa_dma.udma_block->size); 1573 sc->sc_dcbaa_dma.udma_block->size);
1521 1574
1522 if (sc->sc_maxspbuf != 0) { 1575 if (sc->sc_maxspbuf != 0) {
1523 /* 1576 /*
1524 * DCBA entry 0 hold the scratchbuf array pointer. 1577 * DCBA entry 0 hold the scratchbuf array pointer.
1525 */ 1578 */
1526 *(uint64_t *)KERNADDR(dma, 0) = 1579 *(uint64_t *)KERNADDR(dma, 0) =
1527 htole64(DMAADDR(&sc->sc_spbufarray_dma, 0)); 1580 htole64(DMAADDR(&sc->sc_spbufarray_dma, 0));
1528 usb_syncmem(dma, 0, size, BUS_DMASYNC_PREWRITE); 1581 usb_syncmem(dma, 0, size, BUS_DMASYNC_PREWRITE);
1529 } 1582 }
1530 1583
1531 sc->sc_slots = kmem_zalloc(sizeof(*sc->sc_slots) * sc->sc_maxslots, 1584 sc->sc_slots = kmem_zalloc(sizeof(*sc->sc_slots) * sc->sc_maxslots,
1532 KM_SLEEP); 1585 KM_SLEEP);
1533 if (sc->sc_slots == NULL) { 1586 if (sc->sc_slots == NULL) {
1534 aprint_error_dev(sc->sc_dev, "slots init fail, err %d\n", err); 1587 aprint_error_dev(sc->sc_dev, "slots init fail, err %d\n", err);
1535 rv = ENOMEM; 1588 rv = ENOMEM;
1536 goto bad; 1589 goto bad;
1537 } 1590 }
1538 1591
1539 sc->sc_xferpool = pool_cache_init(sizeof(struct xhci_xfer), 0, 0, 0, 1592 sc->sc_xferpool = pool_cache_init(sizeof(struct xhci_xfer), 0, 0, 0,
1540 "xhcixfer", NULL, IPL_USB, NULL, NULL, NULL); 1593 "xhcixfer", NULL, IPL_USB, NULL, NULL, NULL);
1541 if (sc->sc_xferpool == NULL) { 1594 if (sc->sc_xferpool == NULL) {
1542 aprint_error_dev(sc->sc_dev, "pool_cache init fail, err %d\n", 1595 aprint_error_dev(sc->sc_dev, "pool_cache init fail, err %d\n",
1543 err); 1596 err);
1544 rv = ENOMEM; 1597 rv = ENOMEM;
1545 goto bad; 1598 goto bad;
1546 } 1599 }
1547 1600
1548 cv_init(&sc->sc_command_cv, "xhcicmd"); 1601 cv_init(&sc->sc_command_cv, "xhcicmd");
1549 cv_init(&sc->sc_cmdbusy_cv, "xhcicmdq"); 1602 cv_init(&sc->sc_cmdbusy_cv, "xhcicmdq");
1550 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB); 1603 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
1551 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_USB); 1604 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_USB);
1552 1605
1553 struct xhci_erste *erst; 1606 struct xhci_erste *erst;
1554 erst = KERNADDR(&sc->sc_eventst_dma, 0); 1607 erst = KERNADDR(&sc->sc_eventst_dma, 0);
1555 erst[0].erste_0 = htole64(xhci_ring_trbp(sc->sc_er, 0)); 1608 erst[0].erste_0 = htole64(xhci_ring_trbp(sc->sc_er, 0));
1556 erst[0].erste_2 = htole32(sc->sc_er->xr_ntrb); 1609 erst[0].erste_2 = htole32(sc->sc_er->xr_ntrb);
1557 erst[0].erste_3 = htole32(0); 1610 erst[0].erste_3 = htole32(0);
1558 usb_syncmem(&sc->sc_eventst_dma, 0, 1611 usb_syncmem(&sc->sc_eventst_dma, 0,
1559 XHCI_ERSTE_SIZE * XHCI_EVENT_RING_SEGMENTS, BUS_DMASYNC_PREWRITE); 1612 XHCI_ERSTE_SIZE * XHCI_EVENT_RING_SEGMENTS, BUS_DMASYNC_PREWRITE);
1560 1613
1561 xhci_rt_write_4(sc, XHCI_ERSTSZ(0), XHCI_EVENT_RING_SEGMENTS); 1614 xhci_rt_write_4(sc, XHCI_ERSTSZ(0), XHCI_EVENT_RING_SEGMENTS);
1562 xhci_rt_write_8(sc, XHCI_ERSTBA(0), DMAADDR(&sc->sc_eventst_dma, 0)); 1615 xhci_rt_write_8(sc, XHCI_ERSTBA(0), DMAADDR(&sc->sc_eventst_dma, 0));
1563 xhci_rt_write_8(sc, XHCI_ERDP(0), xhci_ring_trbp(sc->sc_er, 0) | 1616 xhci_rt_write_8(sc, XHCI_ERDP(0), xhci_ring_trbp(sc->sc_er, 0) |
1564 XHCI_ERDP_BUSY); 1617 XHCI_ERDP_BUSY);
1565 1618
1566 xhci_op_write_8(sc, XHCI_DCBAAP, DMAADDR(&sc->sc_dcbaa_dma, 0)); 1619 xhci_op_write_8(sc, XHCI_DCBAAP, DMAADDR(&sc->sc_dcbaa_dma, 0));
1567 xhci_op_write_8(sc, XHCI_CRCR, xhci_ring_trbp(sc->sc_cr, 0) | 1620 xhci_op_write_8(sc, XHCI_CRCR, xhci_ring_trbp(sc->sc_cr, 0) |
1568 sc->sc_cr->xr_cs); 1621 sc->sc_cr->xr_cs);
1569 1622
1570 xhci_barrier(sc, BUS_SPACE_BARRIER_WRITE); 1623 xhci_barrier(sc, BUS_SPACE_BARRIER_WRITE);
1571 1624
1572 HEXDUMP("eventst", KERNADDR(&sc->sc_eventst_dma, 0), 1625 HEXDUMP("eventst", KERNADDR(&sc->sc_eventst_dma, 0),
1573 XHCI_ERSTE_SIZE * XHCI_EVENT_RING_SEGMENTS); 1626 XHCI_ERSTE_SIZE * XHCI_EVENT_RING_SEGMENTS);
1574 1627
1575 if ((sc->sc_quirks & XHCI_DEFERRED_START) == 0) 1628 if ((sc->sc_quirks & XHCI_DEFERRED_START) == 0)
1576 xhci_start(sc); 1629 xhci_start(sc);
1577 1630
1578 return 0; 1631 return 0;
1579 1632
1580 bad: 1633 bad:
1581 if (sc->sc_xferpool) { 1634 if (sc->sc_xferpool) {
1582 pool_cache_destroy(sc->sc_xferpool); 1635 pool_cache_destroy(sc->sc_xferpool);
1583 sc->sc_xferpool = NULL; 1636 sc->sc_xferpool = NULL;
1584 } 1637 }
1585 1638
1586 if (sc->sc_slots) { 1639 if (sc->sc_slots) {
1587 kmem_free(sc->sc_slots, sizeof(*sc->sc_slots) * 1640 kmem_free(sc->sc_slots, sizeof(*sc->sc_slots) *
1588 sc->sc_maxslots); 1641 sc->sc_maxslots);
1589 sc->sc_slots = NULL; 1642 sc->sc_slots = NULL;
1590 } 1643 }
1591 1644
1592 usb_freemem(&sc->sc_bus, &sc->sc_dcbaa_dma); 1645 usb_freemem(&sc->sc_bus, &sc->sc_dcbaa_dma);
1593 bad4: 1646 bad4:
1594 usb_freemem(&sc->sc_bus, &sc->sc_eventst_dma); 1647 usb_freemem(&sc->sc_bus, &sc->sc_eventst_dma);
1595 bad3: 1648 bad3:
1596 xhci_ring_free(sc, &sc->sc_er); 1649 xhci_ring_free(sc, &sc->sc_er);
1597 bad2: 1650 bad2:
1598 xhci_ring_free(sc, &sc->sc_cr); 1651 xhci_ring_free(sc, &sc->sc_cr);
1599 i = sc->sc_maxspbuf; 1652 i = sc->sc_maxspbuf;
1600 bad1: 1653 bad1:
1601 for (int j = 0; j < i; j++) 1654 for (int j = 0; j < i; j++)
1602 usb_freemem(&sc->sc_bus, &sc->sc_spbuf_dma[j]); 1655 usb_freemem(&sc->sc_bus, &sc->sc_spbuf_dma[j]);
1603 usb_freemem(&sc->sc_bus, &sc->sc_spbufarray_dma); 1656 usb_freemem(&sc->sc_bus, &sc->sc_spbufarray_dma);
1604 1657
1605 return rv; 1658 return rv;
1606} 1659}
1607 1660
1608static inline bool 1661static inline bool
1609xhci_polling_p(struct xhci_softc * const sc) 1662xhci_polling_p(struct xhci_softc * const sc)
1610{ 1663{
1611 return sc->sc_bus.ub_usepolling || sc->sc_bus2.ub_usepolling; 1664 return sc->sc_bus.ub_usepolling || sc->sc_bus2.ub_usepolling;
1612} 1665}
1613 1666
1614int 1667int
1615xhci_intr(void *v) 1668xhci_intr(void *v)
1616{ 1669{
1617 struct xhci_softc * const sc = v; 1670 struct xhci_softc * const sc = v;
1618 int ret = 0; 1671 int ret = 0;
1619 1672
1620 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 1673 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1621 1674
1622 if (sc == NULL) 1675 if (sc == NULL)
1623 return 0; 1676 return 0;
1624 1677
1625 mutex_spin_enter(&sc->sc_intr_lock); 1678 mutex_spin_enter(&sc->sc_intr_lock);
1626 1679
1627 if (sc->sc_dying || !device_has_power(sc->sc_dev)) 1680 if (sc->sc_dying || !device_has_power(sc->sc_dev))
1628 goto done; 1681 goto done;
1629 1682
1630 /* If we get an interrupt while polling, then just ignore it. */ 1683 /* If we get an interrupt while polling, then just ignore it. */
1631 if (xhci_polling_p(sc)) { 1684 if (xhci_polling_p(sc)) {
1632#ifdef DIAGNOSTIC 1685#ifdef DIAGNOSTIC
1633 DPRINTFN(16, "ignored interrupt while polling", 0, 0, 0, 0); 1686 DPRINTFN(16, "ignored interrupt while polling", 0, 0, 0, 0);
1634#endif 1687#endif
1635 goto done; 1688 goto done;
1636 } 1689 }
1637 1690
1638 ret = xhci_intr1(sc); 1691 ret = xhci_intr1(sc);
1639 if (ret) { 1692 if (ret) {
1640 KASSERT(sc->sc_child || sc->sc_child2); 1693 KASSERT(sc->sc_child || sc->sc_child2);
1641 1694
1642 /* 1695 /*
1643 * One of child busses could be already detached. It doesn't 1696 * One of child busses could be already detached. It doesn't
1644 * matter on which of the two the softintr is scheduled. 1697 * matter on which of the two the softintr is scheduled.
1645 */ 1698 */
1646 if (sc->sc_child) 1699 if (sc->sc_child)
1647 usb_schedsoftintr(&sc->sc_bus); 1700 usb_schedsoftintr(&sc->sc_bus);
1648 else 1701 else
1649 usb_schedsoftintr(&sc->sc_bus2); 1702 usb_schedsoftintr(&sc->sc_bus2);
1650 } 1703 }
1651done: 1704done:
1652 mutex_spin_exit(&sc->sc_intr_lock); 1705 mutex_spin_exit(&sc->sc_intr_lock);
1653 return ret; 1706 return ret;
1654} 1707}
1655 1708
1656int 1709int
1657xhci_intr1(struct xhci_softc * const sc) 1710xhci_intr1(struct xhci_softc * const sc)
1658{ 1711{
1659 uint32_t usbsts; 1712 uint32_t usbsts;
1660 uint32_t iman; 1713 uint32_t iman;
1661 1714
1662 XHCIHIST_FUNC(); 1715 XHCIHIST_FUNC();
1663 1716
1664 usbsts = xhci_op_read_4(sc, XHCI_USBSTS); 1717 usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
1665 XHCIHIST_CALLARGS("USBSTS 0x%08jx", usbsts, 0, 0, 0); 1718 XHCIHIST_CALLARGS("USBSTS 0x%08jx", usbsts, 0, 0, 0);
1666 if ((usbsts & (XHCI_STS_HSE | XHCI_STS_EINT | XHCI_STS_PCD | 1719 if ((usbsts & (XHCI_STS_HSE | XHCI_STS_EINT | XHCI_STS_PCD |
1667 XHCI_STS_HCE)) == 0) { 1720 XHCI_STS_HCE)) == 0) {
1668 DPRINTFN(16, "ignored intr not for %jd", 1721 DPRINTFN(16, "ignored intr not for %jd",
1669 device_unit(sc->sc_dev), 0, 0, 0); 1722 device_unit(sc->sc_dev), 0, 0, 0);
1670 return 0; 1723 return 0;
1671 } 1724 }
1672 1725
1673 /* 1726 /*
1674 * Clear EINT and other transient flags, to not misenterpret 1727 * Clear EINT and other transient flags, to not misenterpret
1675 * next shared interrupt. Also, to avoid race, EINT must be cleared 1728 * next shared interrupt. Also, to avoid race, EINT must be cleared
1676 * before XHCI_IMAN_INTR_PEND is cleared. 1729 * before XHCI_IMAN_INTR_PEND is cleared.
1677 */ 1730 */
1678 xhci_op_write_4(sc, XHCI_USBSTS, usbsts & XHCI_STS_RSVDP0); 1731 xhci_op_write_4(sc, XHCI_USBSTS, usbsts & XHCI_STS_RSVDP0);
1679 1732
1680#ifdef XHCI_DEBUG 1733#ifdef XHCI_DEBUG
1681 usbsts = xhci_op_read_4(sc, XHCI_USBSTS); 1734 usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
1682 DPRINTFN(16, "USBSTS 0x%08jx", usbsts, 0, 0, 0); 1735 DPRINTFN(16, "USBSTS 0x%08jx", usbsts, 0, 0, 0);
1683#endif 1736#endif
1684 1737
1685 iman = xhci_rt_read_4(sc, XHCI_IMAN(0)); 1738 iman = xhci_rt_read_4(sc, XHCI_IMAN(0));
1686 DPRINTFN(16, "IMAN0 0x%08jx", iman, 0, 0, 0); 1739 DPRINTFN(16, "IMAN0 0x%08jx", iman, 0, 0, 0);
1687 iman |= XHCI_IMAN_INTR_PEND; 1740 iman |= XHCI_IMAN_INTR_PEND;
1688 xhci_rt_write_4(sc, XHCI_IMAN(0), iman); 1741 xhci_rt_write_4(sc, XHCI_IMAN(0), iman);
1689 1742
1690#ifdef XHCI_DEBUG 1743#ifdef XHCI_DEBUG
1691 iman = xhci_rt_read_4(sc, XHCI_IMAN(0)); 1744 iman = xhci_rt_read_4(sc, XHCI_IMAN(0));
1692 DPRINTFN(16, "IMAN0 0x%08jx", iman, 0, 0, 0); 1745 DPRINTFN(16, "IMAN0 0x%08jx", iman, 0, 0, 0);
1693 usbsts = xhci_op_read_4(sc, XHCI_USBSTS); 1746 usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
1694 DPRINTFN(16, "USBSTS 0x%08jx", usbsts, 0, 0, 0); 1747 DPRINTFN(16, "USBSTS 0x%08jx", usbsts, 0, 0, 0);
1695#endif 1748#endif
1696 1749
1697 return 1; 1750 return 1;
1698} 1751}
1699 1752
1700/* 1753/*
1701 * 3 port speed types used in USB stack 1754 * 3 port speed types used in USB stack
1702 * 1755 *
1703 * usbdi speed 1756 * usbdi speed
1704 * definition: USB_SPEED_* in usb.h 1757 * definition: USB_SPEED_* in usb.h
1705 * They are used in struct usbd_device in USB stack. 1758 * They are used in struct usbd_device in USB stack.
1706 * ioctl interface uses these values too. 1759 * ioctl interface uses these values too.
1707 * port_status speed 1760 * port_status speed
1708 * definition: UPS_*_SPEED in usb.h 1761 * definition: UPS_*_SPEED in usb.h
1709 * They are used in usb_port_status_t and valid only for USB 2.0. 1762 * They are used in usb_port_status_t and valid only for USB 2.0.
1710 * Speed value is always 0 for Super Speed or more, and dwExtPortStatus 1763 * Speed value is always 0 for Super Speed or more, and dwExtPortStatus
1711 * of usb_port_status_ext_t indicates port speed. 1764 * of usb_port_status_ext_t indicates port speed.
1712 * Note that some 3.0 values overlap with 2.0 values. 1765 * Note that some 3.0 values overlap with 2.0 values.
1713 * (e.g. 0x200 means UPS_POER_POWER_SS in SS and 1766 * (e.g. 0x200 means UPS_POER_POWER_SS in SS and
1714 * means UPS_LOW_SPEED in HS.) 1767 * means UPS_LOW_SPEED in HS.)
1715 * port status returned from hub also uses these values. 1768 * port status returned from hub also uses these values.
1716 * On NetBSD UPS_OTHER_SPEED indicates port speed is super speed 1769 * On NetBSD UPS_OTHER_SPEED indicates port speed is super speed
1717 * or more. 1770 * or more.
1718 * xspeed: 1771 * xspeed:
1719 * definition: Protocol Speed ID (PSI) (xHCI 1.1 7.2.1) 1772 * definition: Protocol Speed ID (PSI) (xHCI 1.1 7.2.1)
1720 * They are used in only slot context and PORTSC reg of xhci. 1773 * They are used in only slot context and PORTSC reg of xhci.
1721 * The difference between usbdi speed and xspeed is 1774 * The difference between usbdi speed and xspeed is
1722 * that FS and LS values are swapped. 1775 * that FS and LS values are swapped.
1723 */ 1776 */
1724 1777
1725/* convert usbdi speed to xspeed */ 1778/* convert usbdi speed to xspeed */
1726static int 1779static int
1727xhci_speed2xspeed(int speed) 1780xhci_speed2xspeed(int speed)
1728{ 1781{
1729 switch (speed) { 1782 switch (speed) {
1730 case USB_SPEED_LOW: return 2; 1783 case USB_SPEED_LOW: return 2;
1731 case USB_SPEED_FULL: return 1; 1784 case USB_SPEED_FULL: return 1;
1732 default: return speed; 1785 default: return speed;
1733 } 1786 }
1734} 1787}
1735 1788
1736#if 0 1789#if 0
1737/* convert xspeed to usbdi speed */ 1790/* convert xspeed to usbdi speed */
1738static int 1791static int
1739xhci_xspeed2speed(int xspeed) 1792xhci_xspeed2speed(int xspeed)
1740{ 1793{
1741 switch (xspeed) { 1794 switch (xspeed) {
1742 case 1: return USB_SPEED_FULL; 1795 case 1: return USB_SPEED_FULL;
1743 case 2: return USB_SPEED_LOW; 1796 case 2: return USB_SPEED_LOW;
1744 default: return xspeed; 1797 default: return xspeed;
1745 } 1798 }
1746} 1799}
1747#endif 1800#endif
1748 1801
1749/* convert xspeed to port status speed */ 1802/* convert xspeed to port status speed */
1750static int 1803static int
1751xhci_xspeed2psspeed(int xspeed) 1804xhci_xspeed2psspeed(int xspeed)
1752{ 1805{
1753 switch (xspeed) { 1806 switch (xspeed) {
1754 case 0: return 0; 1807 case 0: return 0;
1755 case 1: return UPS_FULL_SPEED; 1808 case 1: return UPS_FULL_SPEED;
1756 case 2: return UPS_LOW_SPEED; 1809 case 2: return UPS_LOW_SPEED;
1757 case 3: return UPS_HIGH_SPEED; 1810 case 3: return UPS_HIGH_SPEED;
1758 default: return UPS_OTHER_SPEED; 1811 default: return UPS_OTHER_SPEED;
1759 } 1812 }
1760} 1813}
1761 1814
1762/* 1815/*
1763 * Construct input contexts and issue TRB to open pipe. 1816 * Construct input contexts and issue TRB to open pipe.
1764 */ 1817 */
1765static usbd_status 1818static usbd_status
1766xhci_configure_endpoint(struct usbd_pipe *pipe) 1819xhci_configure_endpoint(struct usbd_pipe *pipe)
1767{ 1820{
1768 struct xhci_softc * const sc = XHCI_PIPE2SC(pipe); 1821 struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
1769 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv; 1822 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
1770#ifdef USB_DEBUG 1823#ifdef USB_DEBUG
1771 const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc); 1824 const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
1772#endif 1825#endif
1773 struct xhci_soft_trb trb; 1826 struct xhci_soft_trb trb;
1774 usbd_status err; 1827 usbd_status err;
1775 1828
1776 XHCIHIST_FUNC(); 1829 XHCIHIST_FUNC();
1777 XHCIHIST_CALLARGS("slot %ju dci %ju epaddr 0x%02jx attr 0x%02jx", 1830 XHCIHIST_CALLARGS("slot %ju dci %ju epaddr 0x%02jx attr 0x%02jx",
1778 xs->xs_idx, dci, pipe->up_endpoint->ue_edesc->bEndpointAddress, 1831 xs->xs_idx, dci, pipe->up_endpoint->ue_edesc->bEndpointAddress,
1779 pipe->up_endpoint->ue_edesc->bmAttributes); 1832 pipe->up_endpoint->ue_edesc->bmAttributes);
1780 1833
1781 /* XXX ensure input context is available? */ 1834 /* XXX ensure input context is available? */
1782 1835
1783 memset(xhci_slot_get_icv(sc, xs, 0), 0, sc->sc_pgsz); 1836 memset(xhci_slot_get_icv(sc, xs, 0), 0, sc->sc_pgsz);
1784 1837
1785 /* set up context */ 1838 /* set up context */
1786 xhci_setup_ctx(pipe); 1839 xhci_setup_ctx(pipe);
1787 1840
1788 HEXDUMP("input control context", xhci_slot_get_icv(sc, xs, 0), 1841 HEXDUMP("input control context", xhci_slot_get_icv(sc, xs, 0),
1789 sc->sc_ctxsz * 1); 1842 sc->sc_ctxsz * 1);
1790 HEXDUMP("input endpoint context", xhci_slot_get_icv(sc, xs, 1843 HEXDUMP("input endpoint context", xhci_slot_get_icv(sc, xs,
1791 xhci_dci_to_ici(dci)), sc->sc_ctxsz * 1); 1844 xhci_dci_to_ici(dci)), sc->sc_ctxsz * 1);
1792 1845
1793 trb.trb_0 = xhci_slot_get_icp(sc, xs, 0); 1846 trb.trb_0 = xhci_slot_get_icp(sc, xs, 0);
1794 trb.trb_2 = 0; 1847 trb.trb_2 = 0;
1795 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) | 1848 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
1796 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_CONFIGURE_EP); 1849 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_CONFIGURE_EP);
1797 1850
1798 err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT); 1851 err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
1799 1852
1800 usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD); 1853 usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD);
1801 HEXDUMP("output context", xhci_slot_get_dcv(sc, xs, dci), 1854 HEXDUMP("output context", xhci_slot_get_dcv(sc, xs, dci),
1802 sc->sc_ctxsz * 1); 1855 sc->sc_ctxsz * 1);
1803 1856
1804 return err; 1857 return err;
1805} 1858}
1806 1859
1807#if 0 1860#if 0
1808static usbd_status 1861static usbd_status
1809xhci_unconfigure_endpoint(struct usbd_pipe *pipe) 1862xhci_unconfigure_endpoint(struct usbd_pipe *pipe)
1810{ 1863{
1811#ifdef USB_DEBUG 1864#ifdef USB_DEBUG
1812 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv; 1865 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
1813#endif 1866#endif
1814 1867
1815 XHCIHIST_FUNC(); 1868 XHCIHIST_FUNC();
1816 XHCIHIST_CALLARGS("slot %ju", xs->xs_idx, 0, 0, 0); 1869 XHCIHIST_CALLARGS("slot %ju", xs->xs_idx, 0, 0, 0);
1817 1870
1818 return USBD_NORMAL_COMPLETION; 1871 return USBD_NORMAL_COMPLETION;
1819} 1872}
1820#endif 1873#endif
1821 1874
1822/* 4.6.8, 6.4.3.7 */ 1875/* 4.6.8, 6.4.3.7 */
1823static usbd_status 1876static usbd_status
1824xhci_reset_endpoint_locked(struct usbd_pipe *pipe) 1877xhci_reset_endpoint_locked(struct usbd_pipe *pipe)
1825{ 1878{
1826 struct xhci_softc * const sc = XHCI_PIPE2SC(pipe); 1879 struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
1827 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv; 1880 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
1828 const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc); 1881 const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
1829 struct xhci_soft_trb trb; 1882 struct xhci_soft_trb trb;
1830 usbd_status err; 1883 usbd_status err;
1831 1884
1832 XHCIHIST_FUNC(); 1885 XHCIHIST_FUNC();
1833 XHCIHIST_CALLARGS("slot %ju dci %ju", xs->xs_idx, dci, 0, 0); 1886 XHCIHIST_CALLARGS("slot %ju dci %ju", xs->xs_idx, dci, 0, 0);
1834 1887
1835 KASSERT(mutex_owned(&sc->sc_lock)); 1888 KASSERT(mutex_owned(&sc->sc_lock));
1836 1889
1837 trb.trb_0 = 0; 1890 trb.trb_0 = 0;
1838 trb.trb_2 = 0; 1891 trb.trb_2 = 0;
1839 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) | 1892 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
1840 XHCI_TRB_3_EP_SET(dci) | 1893 XHCI_TRB_3_EP_SET(dci) |
1841 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_RESET_EP); 1894 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_RESET_EP);
1842 1895
1843 err = xhci_do_command_locked(sc, &trb, USBD_DEFAULT_TIMEOUT); 1896 err = xhci_do_command_locked(sc, &trb, USBD_DEFAULT_TIMEOUT);
1844 1897
1845 return err; 1898 return err;
1846} 1899}
1847 1900
1848static usbd_status 1901static usbd_status
1849xhci_reset_endpoint(struct usbd_pipe *pipe) 1902xhci_reset_endpoint(struct usbd_pipe *pipe)
1850{ 1903{
1851 struct xhci_softc * const sc = XHCI_PIPE2SC(pipe); 1904 struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
1852 1905
1853 mutex_enter(&sc->sc_lock); 1906 mutex_enter(&sc->sc_lock);
1854 usbd_status ret = xhci_reset_endpoint_locked(pipe); 1907 usbd_status ret = xhci_reset_endpoint_locked(pipe);
1855 mutex_exit(&sc->sc_lock); 1908 mutex_exit(&sc->sc_lock);
1856 1909
1857 return ret; 1910 return ret;
1858} 1911}
1859 1912
1860/* 1913/*
1861 * 4.6.9, 6.4.3.8 1914 * 4.6.9, 6.4.3.8
1862 * Stop execution of TDs on xfer ring. 1915 * Stop execution of TDs on xfer ring.
1863 * Should be called with sc_lock held. 1916 * Should be called with sc_lock held.
1864 */ 1917 */
1865static usbd_status 1918static usbd_status
1866xhci_stop_endpoint(struct usbd_pipe *pipe) 1919xhci_stop_endpoint_cmd(struct xhci_softc *sc, struct xhci_slot *xs, u_int dci,
 1920 uint32_t trb3flags)
1867{ 1921{
1868 struct xhci_softc * const sc = XHCI_PIPE2SC(pipe); 
1869 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv; 
1870 struct xhci_soft_trb trb; 1922 struct xhci_soft_trb trb;
1871 usbd_status err; 1923 usbd_status err;
1872 const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc); 
1873 1924
1874 XHCIHIST_FUNC(); 1925 XHCIHIST_FUNC();
1875 XHCIHIST_CALLARGS("slot %ju dci %ju", xs->xs_idx, dci, 0, 0); 1926 XHCIHIST_CALLARGS("slot %ju dci %ju", xs->xs_idx, dci, 0, 0);
1876 1927
1877 KASSERT(mutex_owned(&sc->sc_lock)); 1928 KASSERT(mutex_owned(&sc->sc_lock));
1878 1929
1879 trb.trb_0 = 0; 1930 trb.trb_0 = 0;
1880 trb.trb_2 = 0; 1931 trb.trb_2 = 0;
1881 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) | 1932 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
1882 XHCI_TRB_3_EP_SET(dci) | 1933 XHCI_TRB_3_EP_SET(dci) |
1883 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STOP_EP); 1934 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STOP_EP) |
 1935 trb3flags;
1884 1936
1885 err = xhci_do_command_locked(sc, &trb, USBD_DEFAULT_TIMEOUT); 1937 err = xhci_do_command_locked(sc, &trb, USBD_DEFAULT_TIMEOUT);
1886 1938
1887 return err; 1939 return err;
1888} 1940}
1889 1941
 1942static usbd_status
 1943xhci_stop_endpoint(struct usbd_pipe *pipe)
 1944{
 1945 struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
 1946 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
 1947 const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
 1948
 1949 XHCIHIST_FUNC();
 1950 XHCIHIST_CALLARGS("slot %ju dci %ju", xs->xs_idx, dci, 0, 0);
 1951
 1952 KASSERT(mutex_owned(&sc->sc_lock));
 1953
 1954 return xhci_stop_endpoint_cmd(sc, xs, dci, 0);
 1955}
 1956
1890/* 1957/*
1891 * Set TR Dequeue Pointer. 1958 * Set TR Dequeue Pointer.
1892 * xHCI 1.1 4.6.10 6.4.3.9 1959 * xHCI 1.1 4.6.10 6.4.3.9
1893 * Purge all of the TRBs on ring and reinitialize ring. 1960 * Purge all of the TRBs on ring and reinitialize ring.
1894 * Set TR dequeue Pointr to 0 and Cycle State to 1. 1961 * Set TR dequeue Pointr to 0 and Cycle State to 1.
1895 * EPSTATE of endpoint must be ERROR or STOPPED, otherwise CONTEXT_STATE 1962 * EPSTATE of endpoint must be ERROR or STOPPED, otherwise CONTEXT_STATE
1896 * error will be generated. 1963 * error will be generated.
1897 */ 1964 */
1898static usbd_status 1965static usbd_status
1899xhci_set_dequeue_locked(struct usbd_pipe *pipe) 1966xhci_set_dequeue_locked(struct usbd_pipe *pipe)
1900{ 1967{
1901 struct xhci_softc * const sc = XHCI_PIPE2SC(pipe); 1968 struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
1902 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv; 1969 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
1903 const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc); 1970 const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
1904 struct xhci_ring * const xr = xs->xs_xr[dci]; 1971 struct xhci_ring * const xr = xs->xs_xr[dci];
1905 struct xhci_soft_trb trb; 1972 struct xhci_soft_trb trb;
1906 usbd_status err; 1973 usbd_status err;
1907 1974
1908 XHCIHIST_FUNC(); 1975 XHCIHIST_FUNC();
1909 XHCIHIST_CALLARGS("slot %ju dci %ju", xs->xs_idx, dci, 0, 0); 1976 XHCIHIST_CALLARGS("slot %ju dci %ju", xs->xs_idx, dci, 0, 0);
1910 1977
1911 KASSERT(mutex_owned(&sc->sc_lock)); 1978 KASSERT(mutex_owned(&sc->sc_lock));
1912 KASSERT(xr != NULL); 1979 KASSERT(xr != NULL);
1913 1980
1914 xhci_host_dequeue(xr); 1981 xhci_host_dequeue(xr);
1915 1982
1916 /* set DCS */ 1983 /* set DCS */
1917 trb.trb_0 = xhci_ring_trbp(xr, 0) | 1; /* XXX */ 1984 trb.trb_0 = xhci_ring_trbp(xr, 0) | 1; /* XXX */
1918 trb.trb_2 = 0; 1985 trb.trb_2 = 0;
1919 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) | 1986 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
1920 XHCI_TRB_3_EP_SET(dci) | 1987 XHCI_TRB_3_EP_SET(dci) |
1921 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SET_TR_DEQUEUE); 1988 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SET_TR_DEQUEUE);
1922 1989
1923 err = xhci_do_command_locked(sc, &trb, USBD_DEFAULT_TIMEOUT); 1990 err = xhci_do_command_locked(sc, &trb, USBD_DEFAULT_TIMEOUT);
1924 1991
1925 return err; 1992 return err;
1926} 1993}
1927 1994
1928static usbd_status 1995static usbd_status
1929xhci_set_dequeue(struct usbd_pipe *pipe) 1996xhci_set_dequeue(struct usbd_pipe *pipe)
1930{ 1997{
1931 struct xhci_softc * const sc = XHCI_PIPE2SC(pipe); 1998 struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
1932 1999
1933 mutex_enter(&sc->sc_lock); 2000 mutex_enter(&sc->sc_lock);
1934 usbd_status ret = xhci_set_dequeue_locked(pipe); 2001 usbd_status ret = xhci_set_dequeue_locked(pipe);
1935 mutex_exit(&sc->sc_lock); 2002 mutex_exit(&sc->sc_lock);
1936 2003
1937 return ret; 2004 return ret;
1938} 2005}
1939 2006
1940/* 2007/*
1941 * Open new pipe: called from usbd_setup_pipe_flags. 2008 * Open new pipe: called from usbd_setup_pipe_flags.
1942 * Fills methods of pipe. 2009 * Fills methods of pipe.
1943 * If pipe is not for ep0, calls configure_endpoint. 2010 * If pipe is not for ep0, calls configure_endpoint.
1944 */ 2011 */
1945static usbd_status 2012static usbd_status
1946xhci_open(struct usbd_pipe *pipe) 2013xhci_open(struct usbd_pipe *pipe)
1947{ 2014{
1948 struct usbd_device * const dev = pipe->up_dev; 2015 struct usbd_device * const dev = pipe->up_dev;
1949 struct xhci_pipe * const xpipe = (struct xhci_pipe *)pipe; 2016 struct xhci_pipe * const xpipe = (struct xhci_pipe *)pipe;
1950 struct xhci_softc * const sc = XHCI_BUS2SC(dev->ud_bus); 2017 struct xhci_softc * const sc = XHCI_BUS2SC(dev->ud_bus);
1951 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv; 2018 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
1952 usb_endpoint_descriptor_t * const ed = pipe->up_endpoint->ue_edesc; 2019 usb_endpoint_descriptor_t * const ed = pipe->up_endpoint->ue_edesc;
1953 const u_int dci = xhci_ep_get_dci(ed); 2020 const u_int dci = xhci_ep_get_dci(ed);
1954 const uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes); 2021 const uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
1955 usbd_status err; 2022 usbd_status err;
1956 2023
1957 XHCIHIST_FUNC(); 2024 XHCIHIST_FUNC();
1958 XHCIHIST_CALLARGS("addr %jd depth %jd port %jd speed %jd", dev->ud_addr, 2025 XHCIHIST_CALLARGS("addr %jd depth %jd port %jd speed %jd", dev->ud_addr,
1959 dev->ud_depth, dev->ud_powersrc->up_portno, dev->ud_speed); 2026 dev->ud_depth, dev->ud_powersrc->up_portno, dev->ud_speed);
1960 DPRINTFN(1, " dci %ju type 0x%02jx epaddr 0x%02jx attr 0x%02jx", 2027 DPRINTFN(1, " dci %ju type 0x%02jx epaddr 0x%02jx attr 0x%02jx",
1961 xhci_ep_get_dci(ed), ed->bDescriptorType, ed->bEndpointAddress, 2028 xhci_ep_get_dci(ed), ed->bDescriptorType, ed->bEndpointAddress,
1962 ed->bmAttributes); 2029 ed->bmAttributes);
1963 DPRINTFN(1, " mps %ju ival %ju", UGETW(ed->wMaxPacketSize), 2030 DPRINTFN(1, " mps %ju ival %ju", UGETW(ed->wMaxPacketSize),
1964 ed->bInterval, 0, 0); 2031 ed->bInterval, 0, 0);
1965 2032
1966 if (sc->sc_dying) 2033 if (sc->sc_dying)
1967 return USBD_IOERROR; 2034 return USBD_IOERROR;
1968 2035
1969 /* Root Hub */ 2036 /* Root Hub */
1970 if (dev->ud_depth == 0 && dev->ud_powersrc->up_portno == 0) { 2037 if (dev->ud_depth == 0 && dev->ud_powersrc->up_portno == 0) {
1971 switch (ed->bEndpointAddress) { 2038 switch (ed->bEndpointAddress) {
1972 case USB_CONTROL_ENDPOINT: 2039 case USB_CONTROL_ENDPOINT:
1973 pipe->up_methods = &roothub_ctrl_methods; 2040 pipe->up_methods = &roothub_ctrl_methods;
1974 break; 2041 break;
1975 case UE_DIR_IN | USBROOTHUB_INTR_ENDPT: 2042 case UE_DIR_IN | USBROOTHUB_INTR_ENDPT:
1976 pipe->up_methods = &xhci_root_intr_methods; 2043 pipe->up_methods = &xhci_root_intr_methods;
1977 break; 2044 break;
1978 default: 2045 default:
1979 pipe->up_methods = NULL; 2046 pipe->up_methods = NULL;
1980 DPRINTFN(0, "bad bEndpointAddress 0x%02jx", 2047 DPRINTFN(0, "bad bEndpointAddress 0x%02jx",
1981 ed->bEndpointAddress, 0, 0, 0); 2048 ed->bEndpointAddress, 0, 0, 0);
1982 return USBD_INVAL; 2049 return USBD_INVAL;
1983 } 2050 }
1984 return USBD_NORMAL_COMPLETION; 2051 return USBD_NORMAL_COMPLETION;
1985 } 2052 }
1986 2053
1987 switch (xfertype) { 2054 switch (xfertype) {
1988 case UE_CONTROL: 2055 case UE_CONTROL:
1989 pipe->up_methods = &xhci_device_ctrl_methods; 2056 pipe->up_methods = &xhci_device_ctrl_methods;
1990 break; 2057 break;
1991 case UE_ISOCHRONOUS: 2058 case UE_ISOCHRONOUS:
1992 pipe->up_methods = &xhci_device_isoc_methods; 2059 pipe->up_methods = &xhci_device_isoc_methods;
1993 pipe->up_serialise = false; 2060 pipe->up_serialise = false;
1994 xpipe->xp_isoc_next = -1; 2061 xpipe->xp_isoc_next = -1;
1995 break; 2062 break;
1996 case UE_BULK: 2063 case UE_BULK:
1997 pipe->up_methods = &xhci_device_bulk_methods; 2064 pipe->up_methods = &xhci_device_bulk_methods;
1998 break; 2065 break;
1999 case UE_INTERRUPT: 2066 case UE_INTERRUPT:
2000 pipe->up_methods = &xhci_device_intr_methods; 2067 pipe->up_methods = &xhci_device_intr_methods;
2001 break; 2068 break;
2002 default: 2069 default:
2003 return USBD_IOERROR; 2070 return USBD_IOERROR;
2004 break; 2071 break;
2005 } 2072 }
2006 2073
2007 KASSERT(xs != NULL); 2074 KASSERT(xs != NULL);
2008 KASSERT(xs->xs_xr[dci] == NULL); 2075 KASSERT(xs->xs_xr[dci] == NULL);
2009 2076
2010 /* allocate transfer ring */ 2077 /* allocate transfer ring */
2011 err = xhci_ring_init(sc, &xs->xs_xr[dci], XHCI_TRANSFER_RING_TRBS, 2078 err = xhci_ring_init(sc, &xs->xs_xr[dci], XHCI_TRANSFER_RING_TRBS,
2012 XHCI_TRB_ALIGN); 2079 XHCI_TRB_ALIGN);
2013 if (err) { 2080 if (err) {
2014 DPRINTFN(1, "ring alloc failed %jd", err, 0, 0, 0); 2081 DPRINTFN(1, "ring alloc failed %jd", err, 0, 0, 0);
2015 return err; 2082 return err;
2016 } 2083 }
2017 2084
2018 if (ed->bEndpointAddress != USB_CONTROL_ENDPOINT) 2085 if (ed->bEndpointAddress != USB_CONTROL_ENDPOINT)
2019 return xhci_configure_endpoint(pipe); 2086 return xhci_configure_endpoint(pipe);
2020 2087
2021 return USBD_NORMAL_COMPLETION; 2088 return USBD_NORMAL_COMPLETION;
2022} 2089}
2023 2090
2024/* 2091/*
2025 * Closes pipe, called from usbd_kill_pipe via close methods. 2092 * Closes pipe, called from usbd_kill_pipe via close methods.
2026 * If the endpoint to be closed is ep0, disable_slot. 2093 * If the endpoint to be closed is ep0, disable_slot.
2027 * Should be called with sc_lock held. 2094 * Should be called with sc_lock held.
2028 */ 2095 */
2029static void 2096static void
2030xhci_close_pipe(struct usbd_pipe *pipe) 2097xhci_close_pipe(struct usbd_pipe *pipe)
2031{ 2098{
2032 struct xhci_softc * const sc = XHCI_PIPE2SC(pipe); 2099 struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
2033 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv; 2100 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
2034 usb_endpoint_descriptor_t * const ed = pipe->up_endpoint->ue_edesc; 2101 usb_endpoint_descriptor_t * const ed = pipe->up_endpoint->ue_edesc;
2035 const u_int dci = xhci_ep_get_dci(ed); 2102 const u_int dci = xhci_ep_get_dci(ed);
2036 struct xhci_soft_trb trb; 2103 struct xhci_soft_trb trb;
2037 uint32_t *cp; 2104 uint32_t *cp;
2038 2105
2039 XHCIHIST_FUNC(); 2106 XHCIHIST_FUNC();
2040 2107
2041 if (sc->sc_dying) 2108 if (sc->sc_dying)
2042 return; 2109 return;
2043 2110
2044 /* xs is uninitialized before xhci_init_slot */ 2111 /* xs is uninitialized before xhci_init_slot */
2045 if (xs == NULL || xs->xs_idx == 0) 2112 if (xs == NULL || xs->xs_idx == 0)
2046 return; 2113 return;
2047 2114
2048 XHCIHIST_CALLARGS("pipe %#jx slot %ju dci %ju", 2115 XHCIHIST_CALLARGS("pipe %#jx slot %ju dci %ju",
2049 (uintptr_t)pipe, xs->xs_idx, dci, 0); 2116 (uintptr_t)pipe, xs->xs_idx, dci, 0);
2050 2117
2051 KASSERTMSG(!cpu_intr_p() && !cpu_softintr_p(), "called from intr ctx"); 2118 KASSERTMSG(!cpu_intr_p() && !cpu_softintr_p(), "called from intr ctx");
2052 KASSERT(mutex_owned(&sc->sc_lock)); 2119 KASSERT(mutex_owned(&sc->sc_lock));
2053 2120
2054 if (pipe->up_dev->ud_depth == 0) 2121 if (pipe->up_dev->ud_depth == 0)
2055 return; 2122 return;
2056 2123
2057 if (dci == XHCI_DCI_EP_CONTROL) { 2124 if (dci == XHCI_DCI_EP_CONTROL) {
2058 DPRINTFN(4, "closing ep0", 0, 0, 0, 0); 2125 DPRINTFN(4, "closing ep0", 0, 0, 0, 0);
2059 /* This frees all rings */ 2126 /* This frees all rings */
2060 xhci_disable_slot(sc, xs->xs_idx); 2127 xhci_disable_slot(sc, xs->xs_idx);
2061 return; 2128 return;
2062 } 2129 }
2063 2130
2064 if (xhci_get_epstate(sc, xs, dci) != XHCI_EPSTATE_STOPPED) 2131 if (xhci_get_epstate(sc, xs, dci) != XHCI_EPSTATE_STOPPED)
2065 (void)xhci_stop_endpoint(pipe); 2132 (void)xhci_stop_endpoint(pipe);
2066 2133
2067 /* 2134 /*
2068 * set appropriate bit to be dropped. 2135 * set appropriate bit to be dropped.
2069 * don't set DC bit to 1, otherwise all endpoints 2136 * don't set DC bit to 1, otherwise all endpoints
2070 * would be deconfigured. 2137 * would be deconfigured.
2071 */ 2138 */
2072 cp = xhci_slot_get_icv(sc, xs, XHCI_ICI_INPUT_CONTROL); 2139 cp = xhci_slot_get_icv(sc, xs, XHCI_ICI_INPUT_CONTROL);
2073 cp[0] = htole32(XHCI_INCTX_0_DROP_MASK(dci)); 2140 cp[0] = htole32(XHCI_INCTX_0_DROP_MASK(dci));
2074 cp[1] = htole32(0); 2141 cp[1] = htole32(0);
2075 2142
2076 /* XXX should be most significant one, not dci? */ 2143 /* XXX should be most significant one, not dci? */
2077 cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_SLOT)); 2144 cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_SLOT));
2078 cp[0] = htole32(XHCI_SCTX_0_CTX_NUM_SET(dci)); 2145 cp[0] = htole32(XHCI_SCTX_0_CTX_NUM_SET(dci));
2079 2146
2080 /* configure ep context performs an implicit dequeue */ 2147 /* configure ep context performs an implicit dequeue */
2081 xhci_host_dequeue(xs->xs_xr[dci]); 2148 xhci_host_dequeue(xs->xs_xr[dci]);
2082 2149
2083 /* sync input contexts before they are read from memory */ 2150 /* sync input contexts before they are read from memory */
2084 usb_syncmem(&xs->xs_ic_dma, 0, sc->sc_pgsz, BUS_DMASYNC_PREWRITE); 2151 usb_syncmem(&xs->xs_ic_dma, 0, sc->sc_pgsz, BUS_DMASYNC_PREWRITE);
2085 2152
2086 trb.trb_0 = xhci_slot_get_icp(sc, xs, 0); 2153 trb.trb_0 = xhci_slot_get_icp(sc, xs, 0);
2087 trb.trb_2 = 0; 2154 trb.trb_2 = 0;
2088 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) | 2155 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
2089 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_CONFIGURE_EP); 2156 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_CONFIGURE_EP);
2090 2157
2091 (void)xhci_do_command_locked(sc, &trb, USBD_DEFAULT_TIMEOUT); 2158 (void)xhci_do_command_locked(sc, &trb, USBD_DEFAULT_TIMEOUT);
2092 usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD); 2159 usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD);
2093 2160
2094 xhci_ring_free(sc, &xs->xs_xr[dci]); 2161 xhci_ring_free(sc, &xs->xs_xr[dci]);
2095} 2162}
2096 2163
2097/* 2164/*
2098 * Abort transfer. 2165 * Abort transfer.
2099 * Should be called with sc_lock held. 2166 * Should be called with sc_lock held.
2100 */ 2167 */
2101static void 2168static void
2102xhci_abortx(struct usbd_xfer *xfer) 2169xhci_abortx(struct usbd_xfer *xfer)
2103{ 2170{
2104 XHCIHIST_FUNC(); 2171 XHCIHIST_FUNC();
2105 struct xhci_softc * const sc = XHCI_XFER2SC(xfer); 2172 struct xhci_softc * const sc = XHCI_XFER2SC(xfer);
2106 struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv; 2173 struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv;
2107 const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc); 2174 const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc);
2108 2175
2109 XHCIHIST_CALLARGS("xfer %#jx pipe %#jx", 2176 XHCIHIST_CALLARGS("xfer %#jx pipe %#jx",
2110 (uintptr_t)xfer, (uintptr_t)xfer->ux_pipe, 0, 0); 2177 (uintptr_t)xfer, (uintptr_t)xfer->ux_pipe, 0, 0);
2111 2178
2112 KASSERT(mutex_owned(&sc->sc_lock)); 2179 KASSERT(mutex_owned(&sc->sc_lock));
2113 ASSERT_SLEEPABLE(); 2180 ASSERT_SLEEPABLE();
2114 2181
2115 KASSERTMSG((xfer->ux_status == USBD_CANCELLED || 2182 KASSERTMSG((xfer->ux_status == USBD_CANCELLED ||
2116 xfer->ux_status == USBD_TIMEOUT), 2183 xfer->ux_status == USBD_TIMEOUT),
2117 "bad abort status: %d", xfer->ux_status); 2184 "bad abort status: %d", xfer->ux_status);
2118 2185
2119 /* 2186 /*
2120 * If we're dying, skip the hardware action and just notify the 2187 * If we're dying, skip the hardware action and just notify the
2121 * software that we're done. 2188 * software that we're done.
2122 */ 2189 */
2123 if (sc->sc_dying) { 2190 if (sc->sc_dying) {
2124 DPRINTFN(4, "xfer %#jx dying %ju", (uintptr_t)xfer, 2191 DPRINTFN(4, "xfer %#jx dying %ju", (uintptr_t)xfer,
2125 xfer->ux_status, 0, 0); 2192 xfer->ux_status, 0, 0);
2126 goto dying; 2193 goto dying;
2127 } 2194 }
2128 2195
2129 /* 2196 /*
2130 * HC Step 1: Stop execution of TD on the ring. 2197 * HC Step 1: Stop execution of TD on the ring.
2131 */ 2198 */
2132 switch (xhci_get_epstate(sc, xs, dci)) { 2199 switch (xhci_get_epstate(sc, xs, dci)) {
2133 case XHCI_EPSTATE_HALTED: 2200 case XHCI_EPSTATE_HALTED:
2134 (void)xhci_reset_endpoint_locked(xfer->ux_pipe); 2201 (void)xhci_reset_endpoint_locked(xfer->ux_pipe);
2135 break; 2202 break;
2136 case XHCI_EPSTATE_STOPPED: 2203 case XHCI_EPSTATE_STOPPED:
2137 break; 2204 break;
2138 default: 2205 default:
2139 (void)xhci_stop_endpoint(xfer->ux_pipe); 2206 (void)xhci_stop_endpoint(xfer->ux_pipe);
2140 break; 2207 break;
2141 } 2208 }
2142#ifdef DIAGNOSTIC 2209#ifdef DIAGNOSTIC
2143 uint32_t epst = xhci_get_epstate(sc, xs, dci); 2210 uint32_t epst = xhci_get_epstate(sc, xs, dci);
2144 if (epst != XHCI_EPSTATE_STOPPED) 2211 if (epst != XHCI_EPSTATE_STOPPED)
2145 DPRINTFN(4, "dci %ju not stopped %ju", dci, epst, 0, 0); 2212 DPRINTFN(4, "dci %ju not stopped %ju", dci, epst, 0, 0);
2146#endif 2213#endif
2147 2214
2148 /* 2215 /*
2149 * HC Step 2: Remove any vestiges of the xfer from the ring. 2216 * HC Step 2: Remove any vestiges of the xfer from the ring.
2150 */ 2217 */
2151 xhci_set_dequeue_locked(xfer->ux_pipe); 2218 xhci_set_dequeue_locked(xfer->ux_pipe);
2152 2219
2153 /* 2220 /*
2154 * Final Step: Notify completion to waiting xfers. 2221 * Final Step: Notify completion to waiting xfers.
2155 */ 2222 */
2156dying: 2223dying:
2157 usb_transfer_complete(xfer); 2224 usb_transfer_complete(xfer);
2158 DPRINTFN(14, "end", 0, 0, 0, 0); 2225 DPRINTFN(14, "end", 0, 0, 0, 0);
2159 2226
2160 KASSERT(mutex_owned(&sc->sc_lock)); 2227 KASSERT(mutex_owned(&sc->sc_lock));
2161} 2228}
2162 2229
2163static void 2230static void
2164xhci_host_dequeue(struct xhci_ring * const xr) 2231xhci_host_dequeue(struct xhci_ring * const xr)
2165{ 2232{
2166 /* When dequeueing the controller, update our struct copy too */ 2233 /* When dequeueing the controller, update our struct copy too */
2167 memset(xr->xr_trb, 0, xr->xr_ntrb * XHCI_TRB_SIZE); 2234 memset(xr->xr_trb, 0, xr->xr_ntrb * XHCI_TRB_SIZE);
2168 usb_syncmem(&xr->xr_dma, 0, xr->xr_ntrb * XHCI_TRB_SIZE, 2235 usb_syncmem(&xr->xr_dma, 0, xr->xr_ntrb * XHCI_TRB_SIZE,
2169 BUS_DMASYNC_PREWRITE); 2236 BUS_DMASYNC_PREWRITE);
2170 memset(xr->xr_cookies, 0, xr->xr_ntrb * sizeof(*xr->xr_cookies)); 2237 memset(xr->xr_cookies, 0, xr->xr_ntrb * sizeof(*xr->xr_cookies));
2171 2238
2172 xr->xr_ep = 0; 2239 xr->xr_ep = 0;
2173 xr->xr_cs = 1; 2240 xr->xr_cs = 1;
2174} 2241}
2175 2242
2176/* 2243/*
2177 * Recover STALLed endpoint. 2244 * Recover STALLed endpoint.
2178 * xHCI 1.1 sect 4.10.2.1 2245 * xHCI 1.1 sect 4.10.2.1
2179 * Issue RESET_EP to recover halt condition and SET_TR_DEQUEUE to remove 2246 * Issue RESET_EP to recover halt condition and SET_TR_DEQUEUE to remove
2180 * all transfers on transfer ring. 2247 * all transfers on transfer ring.
2181 * These are done in thread context asynchronously. 2248 * These are done in thread context asynchronously.
2182 */ 2249 */
2183static void 2250static void
2184xhci_clear_endpoint_stall_async_task(void *cookie) 2251xhci_clear_endpoint_stall_async_task(void *cookie)
2185{ 2252{
2186 struct usbd_xfer * const xfer = cookie; 2253 struct usbd_xfer * const xfer = cookie;
2187 struct xhci_softc * const sc = XHCI_XFER2SC(xfer); 2254 struct xhci_softc * const sc = XHCI_XFER2SC(xfer);
2188 struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv; 2255 struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv;
2189 const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc); 2256 const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc);
2190 struct xhci_ring * const tr = xs->xs_xr[dci]; 2257 struct xhci_ring * const tr = xs->xs_xr[dci];
2191 2258
2192 XHCIHIST_FUNC(); 2259 XHCIHIST_FUNC();
2193 XHCIHIST_CALLARGS("xfer %#jx slot %ju dci %ju", (uintptr_t)xfer, xs->xs_idx, 2260 XHCIHIST_CALLARGS("xfer %#jx slot %ju dci %ju", (uintptr_t)xfer, xs->xs_idx,
2194 dci, 0); 2261 dci, 0);
2195 2262
2196 /* 2263 /*
2197 * XXXMRG: Stall task can run after slot is disabled when yanked. 2264 * XXXMRG: Stall task can run after slot is disabled when yanked.
2198 * This hack notices that the xs has been memset() in 2265 * This hack notices that the xs has been memset() in
2199 * xhci_disable_slot() and returns. Both xhci_reset_endpoint() 2266 * xhci_disable_slot() and returns. Both xhci_reset_endpoint()
2200 * and xhci_set_dequeue() rely upon a valid ring setup for correct 2267 * and xhci_set_dequeue() rely upon a valid ring setup for correct
2201 * operation, and the latter will fault, as would 2268 * operation, and the latter will fault, as would
2202 * usb_transfer_complete() if it got that far. 2269 * usb_transfer_complete() if it got that far.
2203 */ 2270 */
2204 if (xs->xs_idx == 0) { 2271 if (xs->xs_idx == 0) {
2205 DPRINTFN(4, "ends xs_idx is 0", 0, 0, 0, 0); 2272 DPRINTFN(4, "ends xs_idx is 0", 0, 0, 0, 0);
2206 return; 2273 return;
2207 } 2274 }
2208 2275
2209 KASSERT(tr != NULL); 2276 KASSERT(tr != NULL);
2210 2277
2211 xhci_reset_endpoint(xfer->ux_pipe); 2278 xhci_reset_endpoint(xfer->ux_pipe);
2212 xhci_set_dequeue(xfer->ux_pipe); 2279 xhci_set_dequeue(xfer->ux_pipe);
2213 2280
2214 mutex_enter(&sc->sc_lock); 2281 mutex_enter(&sc->sc_lock);
2215 tr->is_halted = false; 2282 tr->is_halted = false;
2216 usb_transfer_complete(xfer); 2283 usb_transfer_complete(xfer);
2217 mutex_exit(&sc->sc_lock); 2284 mutex_exit(&sc->sc_lock);
2218 DPRINTFN(4, "ends", 0, 0, 0, 0); 2285 DPRINTFN(4, "ends", 0, 0, 0, 0);
2219} 2286}
2220 2287
2221static usbd_status 2288static usbd_status
2222xhci_clear_endpoint_stall_async(struct usbd_xfer *xfer) 2289xhci_clear_endpoint_stall_async(struct usbd_xfer *xfer)
2223{ 2290{
2224 struct xhci_softc * const sc = XHCI_XFER2SC(xfer); 2291 struct xhci_softc * const sc = XHCI_XFER2SC(xfer);
2225 struct xhci_pipe * const xp = (struct xhci_pipe *)xfer->ux_pipe; 2292 struct xhci_pipe * const xp = (struct xhci_pipe *)xfer->ux_pipe;
2226 2293
2227 XHCIHIST_FUNC(); 2294 XHCIHIST_FUNC();
2228 XHCIHIST_CALLARGS("xfer %#jx", (uintptr_t)xfer, 0, 0, 0); 2295 XHCIHIST_CALLARGS("xfer %#jx", (uintptr_t)xfer, 0, 0, 0);
2229 2296
2230 if (sc->sc_dying) { 2297 if (sc->sc_dying) {
2231 return USBD_IOERROR; 2298 return USBD_IOERROR;
2232 } 2299 }
2233 2300
2234 usb_init_task(&xp->xp_async_task, 2301 usb_init_task(&xp->xp_async_task,
2235 xhci_clear_endpoint_stall_async_task, xfer, USB_TASKQ_MPSAFE); 2302 xhci_clear_endpoint_stall_async_task, xfer, USB_TASKQ_MPSAFE);
2236 usb_add_task(xfer->ux_pipe->up_dev, &xp->xp_async_task, USB_TASKQ_HC); 2303 usb_add_task(xfer->ux_pipe->up_dev, &xp->xp_async_task, USB_TASKQ_HC);
2237 DPRINTFN(4, "ends", 0, 0, 0, 0); 2304 DPRINTFN(4, "ends", 0, 0, 0, 0);
2238 2305
2239 return USBD_NORMAL_COMPLETION; 2306 return USBD_NORMAL_COMPLETION;
2240} 2307}
2241 2308
2242/* Process roothub port status/change events and notify to uhub_intr. */ 2309/* Process roothub port status/change events and notify to uhub_intr. */
2243static void 2310static void
2244xhci_rhpsc(struct xhci_softc * const sc, u_int ctlrport) 2311xhci_rhpsc(struct xhci_softc * const sc, u_int ctlrport)
2245{ 2312{
2246 XHCIHIST_FUNC(); 2313 XHCIHIST_FUNC();
2247 XHCIHIST_CALLARGS("xhci%jd: port %ju status change", 2314 XHCIHIST_CALLARGS("xhci%jd: port %ju status change",
2248 device_unit(sc->sc_dev), ctlrport, 0, 0); 2315 device_unit(sc->sc_dev), ctlrport, 0, 0);
2249 2316
2250 if (ctlrport > sc->sc_maxports) 2317 if (ctlrport > sc->sc_maxports)
2251 return; 2318 return;
2252 2319
2253 const size_t bn = xhci_ctlrport2bus(sc, ctlrport); 2320 const size_t bn = xhci_ctlrport2bus(sc, ctlrport);
2254 const size_t rhp = xhci_ctlrport2rhport(sc, ctlrport); 2321 const size_t rhp = xhci_ctlrport2rhport(sc, ctlrport);
2255 struct usbd_xfer * const xfer = sc->sc_intrxfer[bn]; 2322 struct usbd_xfer * const xfer = sc->sc_intrxfer[bn];
2256 2323
2257 DPRINTFN(4, "xhci%jd: bus %jd bp %ju xfer %#jx status change", 2324 DPRINTFN(4, "xhci%jd: bus %jd bp %ju xfer %#jx status change",
2258 device_unit(sc->sc_dev), bn, rhp, (uintptr_t)xfer); 2325 device_unit(sc->sc_dev), bn, rhp, (uintptr_t)xfer);
2259 2326
2260 if (xfer == NULL) 2327 if (xfer == NULL)
2261 return; 2328 return;
2262 KASSERT(xfer->ux_status == USBD_IN_PROGRESS); 2329 KASSERT(xfer->ux_status == USBD_IN_PROGRESS);
2263 2330
2264 uint8_t *p = xfer->ux_buf; 2331 uint8_t *p = xfer->ux_buf;
2265 memset(p, 0, xfer->ux_length); 2332 memset(p, 0, xfer->ux_length);
2266 p[rhp / NBBY] |= 1 << (rhp % NBBY); 2333 p[rhp / NBBY] |= 1 << (rhp % NBBY);
2267 xfer->ux_actlen = xfer->ux_length; 2334 xfer->ux_actlen = xfer->ux_length;
2268 xfer->ux_status = USBD_NORMAL_COMPLETION; 2335 xfer->ux_status = USBD_NORMAL_COMPLETION;
2269 usb_transfer_complete(xfer); 2336 usb_transfer_complete(xfer);
2270} 2337}
2271 2338
2272/* Process Transfer Events */ 2339/* Process Transfer Events */
2273static void 2340static void
2274xhci_event_transfer(struct xhci_softc * const sc, 2341xhci_event_transfer(struct xhci_softc * const sc,
2275 const struct xhci_trb * const trb) 2342 const struct xhci_trb * const trb)
2276{ 2343{
2277 uint64_t trb_0; 2344 uint64_t trb_0;
2278 uint32_t trb_2, trb_3; 2345 uint32_t trb_2, trb_3;
2279 uint8_t trbcode; 2346 uint8_t trbcode;
2280 u_int slot, dci; 2347 u_int slot, dci;
2281 struct xhci_slot *xs; 2348 struct xhci_slot *xs;
2282 struct xhci_ring *xr; 2349 struct xhci_ring *xr;
2283 struct xhci_xfer *xx; 2350 struct xhci_xfer *xx;
2284 struct usbd_xfer *xfer; 2351 struct usbd_xfer *xfer;
2285 usbd_status err; 2352 usbd_status err;
2286 2353
2287 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2354 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2288 2355
2289 trb_0 = le64toh(trb->trb_0); 2356 trb_0 = le64toh(trb->trb_0);
2290 trb_2 = le32toh(trb->trb_2); 2357 trb_2 = le32toh(trb->trb_2);
2291 trb_3 = le32toh(trb->trb_3); 2358 trb_3 = le32toh(trb->trb_3);
2292 trbcode = XHCI_TRB_2_ERROR_GET(trb_2); 2359 trbcode = XHCI_TRB_2_ERROR_GET(trb_2);
2293 slot = XHCI_TRB_3_SLOT_GET(trb_3); 2360 slot = XHCI_TRB_3_SLOT_GET(trb_3);
2294 dci = XHCI_TRB_3_EP_GET(trb_3); 2361 dci = XHCI_TRB_3_EP_GET(trb_3);
2295 xs = &sc->sc_slots[slot]; 2362 xs = &sc->sc_slots[slot];
2296 xr = xs->xs_xr[dci]; 2363 xr = xs->xs_xr[dci];
2297 2364
2298 /* sanity check */ 2365 /* sanity check */
2299 KASSERT(xr != NULL); 2366 KASSERT(xr != NULL);
2300 KASSERTMSG(xs->xs_idx != 0 && xs->xs_idx <= sc->sc_maxslots, 2367 KASSERTMSG(xs->xs_idx != 0 && xs->xs_idx <= sc->sc_maxslots,
2301 "invalid xs_idx %u slot %u", xs->xs_idx, slot); 2368 "invalid xs_idx %u slot %u", xs->xs_idx, slot);
2302 2369
2303 int idx = 0; 2370 int idx = 0;
2304 if ((trb_3 & XHCI_TRB_3_ED_BIT) == 0) { 2371 if ((trb_3 & XHCI_TRB_3_ED_BIT) == 0) {
2305 if (xhci_trb_get_idx(xr, trb_0, &idx)) { 2372 if (xhci_trb_get_idx(xr, trb_0, &idx)) {
2306 DPRINTFN(0, "invalid trb_0 %#jx", trb_0, 0, 0, 0); 2373 DPRINTFN(0, "invalid trb_0 %#jx", trb_0, 0, 0, 0);
2307 return; 2374 return;
2308 } 2375 }
2309 xx = xr->xr_cookies[idx]; 2376 xx = xr->xr_cookies[idx];
2310 2377
2311 /* clear cookie of consumed TRB */ 2378 /* clear cookie of consumed TRB */
2312 xr->xr_cookies[idx] = NULL; 2379 xr->xr_cookies[idx] = NULL;
2313 2380
2314 /* 2381 /*
2315 * xx is NULL if pipe is opened but xfer is not started. 2382 * xx is NULL if pipe is opened but xfer is not started.
2316 * It happens when stopping idle pipe. 2383 * It happens when stopping idle pipe.
2317 */ 2384 */
2318 if (xx == NULL || trbcode == XHCI_TRB_ERROR_LENGTH) { 2385 if (xx == NULL || trbcode == XHCI_TRB_ERROR_LENGTH) {
2319 DPRINTFN(1, "Ignore #%ju: cookie %#jx cc %ju dci %ju", 2386 DPRINTFN(1, "Ignore #%ju: cookie %#jx cc %ju dci %ju",
2320 idx, (uintptr_t)xx, trbcode, dci); 2387 idx, (uintptr_t)xx, trbcode, dci);
2321 DPRINTFN(1, " orig TRB %#jx type %ju", trb_0, 2388 DPRINTFN(1, " orig TRB %#jx type %ju", trb_0,
2322 XHCI_TRB_3_TYPE_GET(le32toh(xr->xr_trb[idx].trb_3)), 2389 XHCI_TRB_3_TYPE_GET(le32toh(xr->xr_trb[idx].trb_3)),
2323 0, 0); 2390 0, 0);
2324 return; 2391 return;
2325 } 2392 }
2326 } else { 2393 } else {
2327 /* When ED != 0, trb_0 is virtual addr of struct xhci_xfer. */ 2394 /* When ED != 0, trb_0 is virtual addr of struct xhci_xfer. */
2328 xx = (void *)(uintptr_t)(trb_0 & ~0x3); 2395 xx = (void *)(uintptr_t)(trb_0 & ~0x3);
2329 } 2396 }
2330 /* XXX this may not happen */ 2397 /* XXX this may not happen */
2331 if (xx == NULL) { 2398 if (xx == NULL) {
2332 DPRINTFN(1, "xfer done: xx is NULL", 0, 0, 0, 0); 2399 DPRINTFN(1, "xfer done: xx is NULL", 0, 0, 0, 0);
2333 return; 2400 return;
2334 } 2401 }
2335 xfer = &xx->xx_xfer; 2402 xfer = &xx->xx_xfer;
2336 /* XXX this may happen when detaching */ 2403 /* XXX this may happen when detaching */
2337 if (xfer == NULL) { 2404 if (xfer == NULL) {
2338 DPRINTFN(1, "xx(%#jx)->xx_xfer is NULL trb_0 %#jx", 2405 DPRINTFN(1, "xx(%#jx)->xx_xfer is NULL trb_0 %#jx",
2339 (uintptr_t)xx, trb_0, 0, 0); 2406 (uintptr_t)xx, trb_0, 0, 0);
2340 return; 2407 return;
2341 } 2408 }
2342 DPRINTFN(14, "xfer %#jx", (uintptr_t)xfer, 0, 0, 0); 2409 DPRINTFN(14, "xfer %#jx", (uintptr_t)xfer, 0, 0, 0);
2343 /* XXX I dunno why this happens */ 2410 /* XXX I dunno why this happens */
2344 KASSERTMSG(xfer->ux_pipe != NULL, "xfer(%p)->ux_pipe is NULL", xfer); 2411 KASSERTMSG(xfer->ux_pipe != NULL, "xfer(%p)->ux_pipe is NULL", xfer);
2345 2412
2346 if (!xfer->ux_pipe->up_repeat && 2413 if (!xfer->ux_pipe->up_repeat &&
2347 SIMPLEQ_EMPTY(&xfer->ux_pipe->up_queue)) { 2414 SIMPLEQ_EMPTY(&xfer->ux_pipe->up_queue)) {
2348 DPRINTFN(1, "xfer(%#jx)->pipe not queued", (uintptr_t)xfer, 2415 DPRINTFN(1, "xfer(%#jx)->pipe not queued", (uintptr_t)xfer,
2349 0, 0, 0); 2416 0, 0, 0);
2350 return; 2417 return;
2351 } 2418 }
2352 2419
2353 const uint8_t xfertype = 2420 const uint8_t xfertype =
2354 UE_GET_XFERTYPE(xfer->ux_pipe->up_endpoint->ue_edesc->bmAttributes); 2421 UE_GET_XFERTYPE(xfer->ux_pipe->up_endpoint->ue_edesc->bmAttributes);
2355 2422
2356 /* 4.11.5.2 Event Data TRB */ 2423 /* 4.11.5.2 Event Data TRB */
2357 if ((trb_3 & XHCI_TRB_3_ED_BIT) != 0) { 2424 if ((trb_3 & XHCI_TRB_3_ED_BIT) != 0) {
2358 DPRINTFN(14, "transfer Event Data: 0x%016jx 0x%08jx" 2425 DPRINTFN(14, "transfer Event Data: 0x%016jx 0x%08jx"
2359 " %02jx", trb_0, XHCI_TRB_2_REM_GET(trb_2), trbcode, 0); 2426 " %02jx", trb_0, XHCI_TRB_2_REM_GET(trb_2), trbcode, 0);
2360 if ((trb_0 & 0x3) == 0x3) { 2427 if ((trb_0 & 0x3) == 0x3) {
2361 xfer->ux_actlen = XHCI_TRB_2_REM_GET(trb_2); 2428 xfer->ux_actlen = XHCI_TRB_2_REM_GET(trb_2);
2362 } 2429 }
2363 } 2430 }
2364 2431
2365 switch (trbcode) { 2432 switch (trbcode) {
2366 case XHCI_TRB_ERROR_SHORT_PKT: 2433 case XHCI_TRB_ERROR_SHORT_PKT:
2367 case XHCI_TRB_ERROR_SUCCESS: 2434 case XHCI_TRB_ERROR_SUCCESS:
2368 /* 2435 /*
2369 * A ctrl transfer can generate two events if it has a Data 2436 * A ctrl transfer can generate two events if it has a Data
2370 * stage. A short data stage can be OK and should not 2437 * stage. A short data stage can be OK and should not
2371 * complete the transfer as the status stage needs to be 2438 * complete the transfer as the status stage needs to be
2372 * performed. 2439 * performed.
2373 * 2440 *
2374 * Note: Data and Status stage events point at same xfer. 2441 * Note: Data and Status stage events point at same xfer.
2375 * ux_actlen and ux_dmabuf will be passed to 2442 * ux_actlen and ux_dmabuf will be passed to
2376 * usb_transfer_complete after the Status stage event. 2443 * usb_transfer_complete after the Status stage event.
2377 * 2444 *
2378 * It can be distingished which stage generates the event: 2445 * It can be distingished which stage generates the event:
2379 * + by checking least 3 bits of trb_0 if ED==1. 2446 * + by checking least 3 bits of trb_0 if ED==1.
2380 * (see xhci_device_ctrl_start). 2447 * (see xhci_device_ctrl_start).
2381 * + by checking the type of original TRB if ED==0. 2448 * + by checking the type of original TRB if ED==0.
2382 * 2449 *
2383 * In addition, intr, bulk, and isoc transfer currently 2450 * In addition, intr, bulk, and isoc transfer currently
2384 * consists of single TD, so the "skip" is not needed. 2451 * consists of single TD, so the "skip" is not needed.
2385 * ctrl xfer uses EVENT_DATA, and others do not. 2452 * ctrl xfer uses EVENT_DATA, and others do not.
2386 * Thus driver can switch the flow by checking ED bit. 2453 * Thus driver can switch the flow by checking ED bit.
2387 */ 2454 */
2388 if (xfertype == UE_ISOCHRONOUS) { 2455 if (xfertype == UE_ISOCHRONOUS) {
2389 xfer->ux_frlengths[xx->xx_isoc_done] -= 2456 xfer->ux_frlengths[xx->xx_isoc_done] -=
2390 XHCI_TRB_2_REM_GET(trb_2); 2457 XHCI_TRB_2_REM_GET(trb_2);
2391 xfer->ux_actlen += xfer->ux_frlengths[xx->xx_isoc_done]; 2458 xfer->ux_actlen += xfer->ux_frlengths[xx->xx_isoc_done];
2392 if (++xx->xx_isoc_done < xfer->ux_nframes) 2459 if (++xx->xx_isoc_done < xfer->ux_nframes)
2393 return; 2460 return;
2394 } else 2461 } else
2395 if ((trb_3 & XHCI_TRB_3_ED_BIT) == 0) { 2462 if ((trb_3 & XHCI_TRB_3_ED_BIT) == 0) {
2396 if (xfer->ux_actlen == 0) 2463 if (xfer->ux_actlen == 0)
2397 xfer->ux_actlen = xfer->ux_length - 2464 xfer->ux_actlen = xfer->ux_length -
2398 XHCI_TRB_2_REM_GET(trb_2); 2465 XHCI_TRB_2_REM_GET(trb_2);
2399 if (XHCI_TRB_3_TYPE_GET(le32toh(xr->xr_trb[idx].trb_3)) 2466 if (XHCI_TRB_3_TYPE_GET(le32toh(xr->xr_trb[idx].trb_3))
2400 == XHCI_TRB_TYPE_DATA_STAGE) { 2467 == XHCI_TRB_TYPE_DATA_STAGE) {
2401 return; 2468 return;
2402 } 2469 }
2403 } else if ((trb_0 & 0x3) == 0x3) { 2470 } else if ((trb_0 & 0x3) == 0x3) {
2404 return; 2471 return;
2405 } 2472 }
2406 err = USBD_NORMAL_COMPLETION; 2473 err = USBD_NORMAL_COMPLETION;
2407 break; 2474 break;
2408 case XHCI_TRB_ERROR_STOPPED: 2475 case XHCI_TRB_ERROR_STOPPED:
2409 case XHCI_TRB_ERROR_LENGTH: 2476 case XHCI_TRB_ERROR_LENGTH:
2410 case XHCI_TRB_ERROR_STOPPED_SHORT: 2477 case XHCI_TRB_ERROR_STOPPED_SHORT:
2411 err = USBD_IOERROR; 2478 err = USBD_IOERROR;
2412 break; 2479 break;
2413 case XHCI_TRB_ERROR_STALL: 2480 case XHCI_TRB_ERROR_STALL:
2414 case XHCI_TRB_ERROR_BABBLE: 2481 case XHCI_TRB_ERROR_BABBLE:
2415 DPRINTFN(1, "ERR %ju slot %ju dci %ju", trbcode, slot, dci, 0); 2482 DPRINTFN(1, "ERR %ju slot %ju dci %ju", trbcode, slot, dci, 0);
2416 xr->is_halted = true; 2483 xr->is_halted = true;
2417 /* 2484 /*
2418 * Try to claim this xfer for completion. If it has already 2485 * Try to claim this xfer for completion. If it has already
2419 * completed or aborted, drop it on the floor. 2486 * completed or aborted, drop it on the floor.
2420 */ 2487 */
2421 if (!usbd_xfer_trycomplete(xfer)) 2488 if (!usbd_xfer_trycomplete(xfer))
2422 return; 2489 return;
2423 2490
2424 /* 2491 /*
2425 * Stalled endpoints can be recoverd by issuing 2492 * Stalled endpoints can be recoverd by issuing
2426 * command TRB TYPE_RESET_EP on xHCI instead of 2493 * command TRB TYPE_RESET_EP on xHCI instead of
2427 * issuing request CLEAR_FEATURE UF_ENDPOINT_HALT 2494 * issuing request CLEAR_FEATURE UF_ENDPOINT_HALT
2428 * on the endpoint. However, this function may be 2495 * on the endpoint. However, this function may be
2429 * called from softint context (e.g. from umass), 2496 * called from softint context (e.g. from umass),
2430 * in that case driver gets KASSERT in cv_timedwait 2497 * in that case driver gets KASSERT in cv_timedwait
2431 * in xhci_do_command. 2498 * in xhci_do_command.
2432 * To avoid this, this runs reset_endpoint and 2499 * To avoid this, this runs reset_endpoint and
2433 * usb_transfer_complete in usb task thread 2500 * usb_transfer_complete in usb task thread
2434 * asynchronously (and then umass issues clear 2501 * asynchronously (and then umass issues clear
2435 * UF_ENDPOINT_HALT). 2502 * UF_ENDPOINT_HALT).
2436 */ 2503 */
2437 2504
2438 /* Override the status. */ 2505 /* Override the status. */
2439 xfer->ux_status = USBD_STALLED; 2506 xfer->ux_status = USBD_STALLED;
2440 2507
2441 xhci_clear_endpoint_stall_async(xfer); 2508 xhci_clear_endpoint_stall_async(xfer);
2442 return; 2509 return;
2443 default: 2510 default:
2444 DPRINTFN(1, "ERR %ju slot %ju dci %ju", trbcode, slot, dci, 0); 2511 DPRINTFN(1, "ERR %ju slot %ju dci %ju", trbcode, slot, dci, 0);
2445 err = USBD_IOERROR; 2512 err = USBD_IOERROR;
2446 break; 2513 break;
2447 } 2514 }
2448 2515
2449 /* 2516 /*
2450 * Try to claim this xfer for completion. If it has already 2517 * Try to claim this xfer for completion. If it has already
2451 * completed or aborted, drop it on the floor. 2518 * completed or aborted, drop it on the floor.
2452 */ 2519 */
2453 if (!usbd_xfer_trycomplete(xfer)) 2520 if (!usbd_xfer_trycomplete(xfer))
2454 return; 2521 return;
2455 2522
2456 /* Set the status. */ 2523 /* Set the status. */
2457 xfer->ux_status = err; 2524 xfer->ux_status = err;
2458 2525
2459 if ((trb_3 & XHCI_TRB_3_ED_BIT) == 0 || 2526 if ((trb_3 & XHCI_TRB_3_ED_BIT) == 0 ||
2460 (trb_0 & 0x3) == 0x0) { 2527 (trb_0 & 0x3) == 0x0) {
2461 usb_transfer_complete(xfer); 2528 usb_transfer_complete(xfer);
2462 } 2529 }
2463} 2530}
2464 2531
2465/* Process Command complete events */ 2532/* Process Command complete events */
2466static void 2533static void
2467xhci_event_cmd(struct xhci_softc * const sc, const struct xhci_trb * const trb) 2534xhci_event_cmd(struct xhci_softc * const sc, const struct xhci_trb * const trb)
2468{ 2535{
2469 uint64_t trb_0; 2536 uint64_t trb_0;
2470 uint32_t trb_2, trb_3; 2537 uint32_t trb_2, trb_3;
2471 2538
2472 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2539 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2473 2540
2474 KASSERT(mutex_owned(&sc->sc_lock)); 2541 KASSERT(mutex_owned(&sc->sc_lock));
2475 2542
2476 trb_0 = le64toh(trb->trb_0); 2543 trb_0 = le64toh(trb->trb_0);
2477 trb_2 = le32toh(trb->trb_2); 2544 trb_2 = le32toh(trb->trb_2);
2478 trb_3 = le32toh(trb->trb_3); 2545 trb_3 = le32toh(trb->trb_3);
2479 2546
2480 if (trb_0 == sc->sc_command_addr) { 2547 if (trb_0 == sc->sc_command_addr) {
2481 sc->sc_resultpending = false; 2548 sc->sc_resultpending = false;
2482 2549
2483 sc->sc_result_trb.trb_0 = trb_0; 2550 sc->sc_result_trb.trb_0 = trb_0;
2484 sc->sc_result_trb.trb_2 = trb_2; 2551 sc->sc_result_trb.trb_2 = trb_2;
2485 sc->sc_result_trb.trb_3 = trb_3; 2552 sc->sc_result_trb.trb_3 = trb_3;
2486 if (XHCI_TRB_2_ERROR_GET(trb_2) != 2553 if (XHCI_TRB_2_ERROR_GET(trb_2) !=
2487 XHCI_TRB_ERROR_SUCCESS) { 2554 XHCI_TRB_ERROR_SUCCESS) {
2488 DPRINTFN(1, "command completion " 2555 DPRINTFN(1, "command completion "
2489 "failure: 0x%016jx 0x%08jx 0x%08jx", 2556 "failure: 0x%016jx 0x%08jx 0x%08jx",
2490 trb_0, trb_2, trb_3, 0); 2557 trb_0, trb_2, trb_3, 0);
2491 } 2558 }
2492 cv_signal(&sc->sc_command_cv); 2559 cv_signal(&sc->sc_command_cv);
2493 } else { 2560 } else {
2494 DPRINTFN(1, "spurious event: %#jx 0x%016jx " 2561 DPRINTFN(1, "spurious event: %#jx 0x%016jx "
2495 "0x%08jx 0x%08jx", (uintptr_t)trb, trb_0, trb_2, trb_3); 2562 "0x%08jx 0x%08jx", (uintptr_t)trb, trb_0, trb_2, trb_3);
2496 } 2563 }
2497} 2564}
2498 2565
2499/* 2566/*
2500 * Process events. 2567 * Process events.
2501 * called from xhci_softintr 2568 * called from xhci_softintr
2502 */ 2569 */
2503static void 2570static void
2504xhci_handle_event(struct xhci_softc * const sc, 2571xhci_handle_event(struct xhci_softc * const sc,
2505 const struct xhci_trb * const trb) 2572 const struct xhci_trb * const trb)
2506{ 2573{
2507 uint64_t trb_0; 2574 uint64_t trb_0;
2508 uint32_t trb_2, trb_3; 2575 uint32_t trb_2, trb_3;
2509 2576
2510 XHCIHIST_FUNC(); 2577 XHCIHIST_FUNC();
2511 2578
2512 trb_0 = le64toh(trb->trb_0); 2579 trb_0 = le64toh(trb->trb_0);
2513 trb_2 = le32toh(trb->trb_2); 2580 trb_2 = le32toh(trb->trb_2);
2514 trb_3 = le32toh(trb->trb_3); 2581 trb_3 = le32toh(trb->trb_3);
2515 2582
2516 XHCIHIST_CALLARGS("event: %#jx 0x%016jx 0x%08jx 0x%08jx", 2583 XHCIHIST_CALLARGS("event: %#jx 0x%016jx 0x%08jx 0x%08jx",
2517 (uintptr_t)trb, trb_0, trb_2, trb_3); 2584 (uintptr_t)trb, trb_0, trb_2, trb_3);
2518 2585
2519 /* 2586 /*
2520 * 4.11.3.1, 6.4.2.1 2587 * 4.11.3.1, 6.4.2.1
2521 * TRB Pointer is invalid for these completion codes. 2588 * TRB Pointer is invalid for these completion codes.
2522 */ 2589 */
2523 switch (XHCI_TRB_2_ERROR_GET(trb_2)) { 2590 switch (XHCI_TRB_2_ERROR_GET(trb_2)) {
2524 case XHCI_TRB_ERROR_RING_UNDERRUN: 2591 case XHCI_TRB_ERROR_RING_UNDERRUN:
2525 case XHCI_TRB_ERROR_RING_OVERRUN: 2592 case XHCI_TRB_ERROR_RING_OVERRUN:
2526 case XHCI_TRB_ERROR_VF_RING_FULL: 2593 case XHCI_TRB_ERROR_VF_RING_FULL:
2527 return; 2594 return;
2528 default: 2595 default:
2529 if (trb_0 == 0) { 2596 if (trb_0 == 0) {
2530 return; 2597 return;
2531 } 2598 }
2532 break; 2599 break;
2533 } 2600 }
2534 2601
2535 switch (XHCI_TRB_3_TYPE_GET(trb_3)) { 2602 switch (XHCI_TRB_3_TYPE_GET(trb_3)) {
2536 case XHCI_TRB_EVENT_TRANSFER: 2603 case XHCI_TRB_EVENT_TRANSFER:
2537 xhci_event_transfer(sc, trb); 2604 xhci_event_transfer(sc, trb);
2538 break; 2605 break;
2539 case XHCI_TRB_EVENT_CMD_COMPLETE: 2606 case XHCI_TRB_EVENT_CMD_COMPLETE:
2540 xhci_event_cmd(sc, trb); 2607 xhci_event_cmd(sc, trb);
2541 break; 2608 break;
2542 case XHCI_TRB_EVENT_PORT_STS_CHANGE: 2609 case XHCI_TRB_EVENT_PORT_STS_CHANGE:
2543 xhci_rhpsc(sc, (uint32_t)((trb_0 >> 24) & 0xff)); 2610 xhci_rhpsc(sc, (uint32_t)((trb_0 >> 24) & 0xff));
2544 break; 2611 break;
2545 default: 2612 default:
2546 break; 2613 break;
2547 } 2614 }
2548} 2615}
2549 2616
2550static void 2617static void
2551xhci_softintr(void *v) 2618xhci_softintr(void *v)
2552{ 2619{
2553 struct usbd_bus * const bus = v; 2620 struct usbd_bus * const bus = v;
2554 struct xhci_softc * const sc = XHCI_BUS2SC(bus); 2621 struct xhci_softc * const sc = XHCI_BUS2SC(bus);
2555 struct xhci_ring * const er = sc->sc_er; 2622 struct xhci_ring * const er = sc->sc_er;
2556 struct xhci_trb *trb; 2623 struct xhci_trb *trb;
2557 int i, j, k; 2624 int i, j, k;
2558 2625
2559 XHCIHIST_FUNC(); 2626 XHCIHIST_FUNC();
2560 2627
2561 KASSERT(xhci_polling_p(sc) || mutex_owned(&sc->sc_lock)); 2628 KASSERT(xhci_polling_p(sc) || mutex_owned(&sc->sc_lock));
2562 2629
2563 i = er->xr_ep; 2630 i = er->xr_ep;
2564 j = er->xr_cs; 2631 j = er->xr_cs;
2565 2632
2566 XHCIHIST_CALLARGS("er: xr_ep %jd xr_cs %jd", i, j, 0, 0); 2633 XHCIHIST_CALLARGS("er: xr_ep %jd xr_cs %jd", i, j, 0, 0);
2567 2634
2568 while (1) { 2635 while (1) {
2569 usb_syncmem(&er->xr_dma, XHCI_TRB_SIZE * i, XHCI_TRB_SIZE, 2636 usb_syncmem(&er->xr_dma, XHCI_TRB_SIZE * i, XHCI_TRB_SIZE,
2570 BUS_DMASYNC_POSTREAD); 2637 BUS_DMASYNC_POSTREAD);
2571 trb = &er->xr_trb[i]; 2638 trb = &er->xr_trb[i];
2572 k = (le32toh(trb->trb_3) & XHCI_TRB_3_CYCLE_BIT) ? 1 : 0; 2639 k = (le32toh(trb->trb_3) & XHCI_TRB_3_CYCLE_BIT) ? 1 : 0;
2573 2640
2574 if (j != k) 2641 if (j != k)
2575 break; 2642 break;
2576 2643
2577 xhci_handle_event(sc, trb); 2644 xhci_handle_event(sc, trb);
2578 2645
2579 i++; 2646 i++;
2580 if (i == er->xr_ntrb) { 2647 if (i == er->xr_ntrb) {
2581 i = 0; 2648 i = 0;
2582 j ^= 1; 2649 j ^= 1;
2583 } 2650 }
2584 } 2651 }
2585 2652
2586 er->xr_ep = i; 2653 er->xr_ep = i;
2587 er->xr_cs = j; 2654 er->xr_cs = j;
2588 2655
2589 xhci_rt_write_8(sc, XHCI_ERDP(0), xhci_ring_trbp(er, er->xr_ep) | 2656 xhci_rt_write_8(sc, XHCI_ERDP(0), xhci_ring_trbp(er, er->xr_ep) |
2590 XHCI_ERDP_BUSY); 2657 XHCI_ERDP_BUSY);
2591 2658
2592 DPRINTFN(16, "ends", 0, 0, 0, 0); 2659 DPRINTFN(16, "ends", 0, 0, 0, 0);
2593 2660
2594 return; 2661 return;
2595} 2662}
2596 2663
2597static void 2664static void
2598xhci_poll(struct usbd_bus *bus) 2665xhci_poll(struct usbd_bus *bus)
2599{ 2666{
2600 struct xhci_softc * const sc = XHCI_BUS2SC(bus); 2667 struct xhci_softc * const sc = XHCI_BUS2SC(bus);
2601 2668
2602 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2669 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2603 2670
2604 mutex_enter(&sc->sc_intr_lock); 2671 mutex_enter(&sc->sc_intr_lock);
2605 int ret = xhci_intr1(sc); 2672 int ret = xhci_intr1(sc);
2606 if (ret) { 2673 if (ret) {
2607 xhci_softintr(bus); 2674 xhci_softintr(bus);
2608 } 2675 }
2609 mutex_exit(&sc->sc_intr_lock); 2676 mutex_exit(&sc->sc_intr_lock);
2610 2677
2611 return; 2678 return;
2612} 2679}
2613 2680
2614static struct usbd_xfer * 2681static struct usbd_xfer *
2615xhci_allocx(struct usbd_bus *bus, unsigned int nframes) 2682xhci_allocx(struct usbd_bus *bus, unsigned int nframes)
2616{ 2683{
2617 struct xhci_softc * const sc = XHCI_BUS2SC(bus); 2684 struct xhci_softc * const sc = XHCI_BUS2SC(bus);
2618 struct xhci_xfer *xx; 2685 struct xhci_xfer *xx;
2619 u_int ntrbs; 2686 u_int ntrbs;
2620 2687
2621 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2688 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2622 2689
2623 ntrbs = uimax(3, nframes); 2690 ntrbs = uimax(3, nframes);
2624 const size_t trbsz = sizeof(*xx->xx_trb) * ntrbs; 2691 const size_t trbsz = sizeof(*xx->xx_trb) * ntrbs;
2625 2692
2626 xx = pool_cache_get(sc->sc_xferpool, PR_WAITOK); 2693 xx = pool_cache_get(sc->sc_xferpool, PR_WAITOK);
2627 if (xx != NULL) { 2694 if (xx != NULL) {
2628 memset(xx, 0, sizeof(*xx)); 2695 memset(xx, 0, sizeof(*xx));
2629 if (ntrbs > 0) { 2696 if (ntrbs > 0) {
2630 xx->xx_trb = kmem_alloc(trbsz, KM_SLEEP); 2697 xx->xx_trb = kmem_alloc(trbsz, KM_SLEEP);
2631 xx->xx_ntrb = ntrbs; 2698 xx->xx_ntrb = ntrbs;
2632 } 2699 }
2633#ifdef DIAGNOSTIC 2700#ifdef DIAGNOSTIC
2634 xx->xx_xfer.ux_state = XFER_BUSY; 2701 xx->xx_xfer.ux_state = XFER_BUSY;
2635#endif 2702#endif
2636 } 2703 }
2637 2704
2638 return &xx->xx_xfer; 2705 return &xx->xx_xfer;
2639} 2706}
2640 2707
2641static void 2708static void
2642xhci_freex(struct usbd_bus *bus, struct usbd_xfer *xfer) 2709xhci_freex(struct usbd_bus *bus, struct usbd_xfer *xfer)
2643{ 2710{
2644 struct xhci_softc * const sc = XHCI_BUS2SC(bus); 2711 struct xhci_softc * const sc = XHCI_BUS2SC(bus);
2645 struct xhci_xfer * const xx = XHCI_XFER2XXFER(xfer); 2712 struct xhci_xfer * const xx = XHCI_XFER2XXFER(xfer);
2646 2713
2647 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2714 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2648 2715
2649#ifdef DIAGNOSTIC 2716#ifdef DIAGNOSTIC
2650 if (xfer->ux_state != XFER_BUSY && 2717 if (xfer->ux_state != XFER_BUSY &&
2651 xfer->ux_status != USBD_NOT_STARTED) { 2718 xfer->ux_status != USBD_NOT_STARTED) {
2652 DPRINTFN(0, "xfer=%#jx not busy, 0x%08jx", 2719 DPRINTFN(0, "xfer=%#jx not busy, 0x%08jx",
2653 (uintptr_t)xfer, xfer->ux_state, 0, 0); 2720 (uintptr_t)xfer, xfer->ux_state, 0, 0);
2654 } 2721 }
2655 xfer->ux_state = XFER_FREE; 2722 xfer->ux_state = XFER_FREE;
2656#endif 2723#endif
2657 if (xx->xx_ntrb > 0) { 2724 if (xx->xx_ntrb > 0) {
2658 kmem_free(xx->xx_trb, xx->xx_ntrb * sizeof(*xx->xx_trb)); 2725 kmem_free(xx->xx_trb, xx->xx_ntrb * sizeof(*xx->xx_trb));
2659 xx->xx_trb = NULL; 2726 xx->xx_trb = NULL;
2660 xx->xx_ntrb = 0; 2727 xx->xx_ntrb = 0;
2661 } 2728 }
2662 pool_cache_put(sc->sc_xferpool, xx); 2729 pool_cache_put(sc->sc_xferpool, xx);
2663} 2730}
2664 2731
2665static bool 2732static bool
2666xhci_dying(struct usbd_bus *bus) 2733xhci_dying(struct usbd_bus *bus)
2667{ 2734{
2668 struct xhci_softc * const sc = XHCI_BUS2SC(bus); 2735 struct xhci_softc * const sc = XHCI_BUS2SC(bus);
2669 2736
2670 return sc->sc_dying; 2737 return sc->sc_dying;
2671} 2738}
2672 2739
2673static void 2740static void
2674xhci_get_lock(struct usbd_bus *bus, kmutex_t **lock) 2741xhci_get_lock(struct usbd_bus *bus, kmutex_t **lock)
2675{ 2742{
2676 struct xhci_softc * const sc = XHCI_BUS2SC(bus); 2743 struct xhci_softc * const sc = XHCI_BUS2SC(bus);
2677 2744
2678 *lock = &sc->sc_lock; 2745 *lock = &sc->sc_lock;
2679} 2746}
2680 2747
2681extern uint32_t usb_cookie_no; 2748extern uint32_t usb_cookie_no;
2682 2749
2683/* 2750/*
2684 * xHCI 4.3 2751 * xHCI 4.3
2685 * Called when uhub_explore finds a new device (via usbd_new_device). 2752 * Called when uhub_explore finds a new device (via usbd_new_device).
2686 * Port initialization and speed detection (4.3.1) are already done in uhub.c. 2753 * Port initialization and speed detection (4.3.1) are already done in uhub.c.
2687 * This function does: 2754 * This function does:
2688 * Allocate and construct dev structure of default endpoint (ep0). 2755 * Allocate and construct dev structure of default endpoint (ep0).
2689 * Allocate and open pipe of ep0. 2756 * Allocate and open pipe of ep0.
2690 * Enable slot and initialize slot context. 2757 * Enable slot and initialize slot context.
2691 * Set Address. 2758 * Set Address.
2692 * Read initial device descriptor. 2759 * Read initial device descriptor.
2693 * Determine initial MaxPacketSize (mps) by speed. 2760 * Determine initial MaxPacketSize (mps) by speed.
2694 * Read full device descriptor. 2761 * Read full device descriptor.
2695 * Register this device. 2762 * Register this device.
2696 * Finally state of device transitions ADDRESSED. 2763 * Finally state of device transitions ADDRESSED.
2697 */ 2764 */
2698static usbd_status 2765static usbd_status
2699xhci_new_device(device_t parent, struct usbd_bus *bus, int depth, 2766xhci_new_device(device_t parent, struct usbd_bus *bus, int depth,
2700 int speed, int port, struct usbd_port *up) 2767 int speed, int port, struct usbd_port *up)
2701{ 2768{
2702 struct xhci_softc * const sc = XHCI_BUS2SC(bus); 2769 struct xhci_softc * const sc = XHCI_BUS2SC(bus);
2703 struct usbd_device *dev; 2770 struct usbd_device *dev;
2704 usbd_status err; 2771 usbd_status err;
2705 usb_device_descriptor_t *dd; 2772 usb_device_descriptor_t *dd;
2706 struct xhci_slot *xs; 2773 struct xhci_slot *xs;
2707 uint32_t *cp; 2774 uint32_t *cp;
2708 2775
2709 XHCIHIST_FUNC(); 2776 XHCIHIST_FUNC();
2710 XHCIHIST_CALLARGS("port %ju depth %ju speed %ju up %#jx", 2777 XHCIHIST_CALLARGS("port %ju depth %ju speed %ju up %#jx",
2711 port, depth, speed, (uintptr_t)up); 2778 port, depth, speed, (uintptr_t)up);
2712 2779
2713 dev = kmem_zalloc(sizeof(*dev), KM_SLEEP); 2780 dev = kmem_zalloc(sizeof(*dev), KM_SLEEP);
2714 dev->ud_bus = bus; 2781 dev->ud_bus = bus;
2715 dev->ud_quirks = &usbd_no_quirk; 2782 dev->ud_quirks = &usbd_no_quirk;
2716 dev->ud_addr = 0; 2783 dev->ud_addr = 0;
2717 dev->ud_ddesc.bMaxPacketSize = 0; 2784 dev->ud_ddesc.bMaxPacketSize = 0;
2718 dev->ud_depth = depth; 2785 dev->ud_depth = depth;
2719 dev->ud_powersrc = up; 2786 dev->ud_powersrc = up;
2720 dev->ud_myhub = up->up_parent; 2787 dev->ud_myhub = up->up_parent;
2721 dev->ud_speed = speed; 2788 dev->ud_speed = speed;
2722 dev->ud_langid = USBD_NOLANG; 2789 dev->ud_langid = USBD_NOLANG;
2723 dev->ud_cookie.cookie = ++usb_cookie_no; 2790 dev->ud_cookie.cookie = ++usb_cookie_no;
2724 2791
2725 /* Set up default endpoint handle. */ 2792 /* Set up default endpoint handle. */
2726 dev->ud_ep0.ue_edesc = &dev->ud_ep0desc; 2793 dev->ud_ep0.ue_edesc = &dev->ud_ep0desc;
2727 /* doesn't matter, just don't let it uninitialized */ 2794 /* doesn't matter, just don't let it uninitialized */
2728 dev->ud_ep0.ue_toggle = 0; 2795 dev->ud_ep0.ue_toggle = 0;
2729 2796
2730 /* Set up default endpoint descriptor. */ 2797 /* Set up default endpoint descriptor. */
2731 dev->ud_ep0desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE; 2798 dev->ud_ep0desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
2732 dev->ud_ep0desc.bDescriptorType = UDESC_ENDPOINT; 2799 dev->ud_ep0desc.bDescriptorType = UDESC_ENDPOINT;
2733 dev->ud_ep0desc.bEndpointAddress = USB_CONTROL_ENDPOINT; 2800 dev->ud_ep0desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
2734 dev->ud_ep0desc.bmAttributes = UE_CONTROL; 2801 dev->ud_ep0desc.bmAttributes = UE_CONTROL;
2735 dev->ud_ep0desc.bInterval = 0; 2802 dev->ud_ep0desc.bInterval = 0;
2736 2803
2737 /* 4.3, 4.8.2.1 */ 2804 /* 4.3, 4.8.2.1 */
2738 switch (speed) { 2805 switch (speed) {
2739 case USB_SPEED_SUPER: 2806 case USB_SPEED_SUPER:
2740 case USB_SPEED_SUPER_PLUS: 2807 case USB_SPEED_SUPER_PLUS:
2741 USETW(dev->ud_ep0desc.wMaxPacketSize, USB_3_MAX_CTRL_PACKET); 2808 USETW(dev->ud_ep0desc.wMaxPacketSize, USB_3_MAX_CTRL_PACKET);
2742 break; 2809 break;
2743 case USB_SPEED_FULL: 2810 case USB_SPEED_FULL:
2744 /* XXX using 64 as initial mps of ep0 in FS */ 2811 /* XXX using 64 as initial mps of ep0 in FS */
2745 case USB_SPEED_HIGH: 2812 case USB_SPEED_HIGH:
2746 USETW(dev->ud_ep0desc.wMaxPacketSize, USB_2_MAX_CTRL_PACKET); 2813 USETW(dev->ud_ep0desc.wMaxPacketSize, USB_2_MAX_CTRL_PACKET);
2747 break; 2814 break;
2748 case USB_SPEED_LOW: 2815 case USB_SPEED_LOW:
2749 default: 2816 default:
2750 USETW(dev->ud_ep0desc.wMaxPacketSize, USB_MAX_IPACKET); 2817 USETW(dev->ud_ep0desc.wMaxPacketSize, USB_MAX_IPACKET);
2751 break; 2818 break;
2752 } 2819 }
2753 2820
2754 up->up_dev = dev; 2821 up->up_dev = dev;
2755 2822
2756 dd = &dev->ud_ddesc; 2823 dd = &dev->ud_ddesc;
2757 2824
2758 if (depth == 0 && port == 0) { 2825 if (depth == 0 && port == 0) {
2759 KASSERT(bus->ub_devices[USB_ROOTHUB_INDEX] == NULL); 2826 KASSERT(bus->ub_devices[USB_ROOTHUB_INDEX] == NULL);
2760 bus->ub_devices[USB_ROOTHUB_INDEX] = dev; 2827 bus->ub_devices[USB_ROOTHUB_INDEX] = dev;
2761 2828
2762 /* Establish the default pipe. */ 2829 /* Establish the default pipe. */
2763 err = usbd_setup_pipe(dev, 0, &dev->ud_ep0, 2830 err = usbd_setup_pipe(dev, 0, &dev->ud_ep0,
2764 USBD_DEFAULT_INTERVAL, &dev->ud_pipe0); 2831 USBD_DEFAULT_INTERVAL, &dev->ud_pipe0);
2765 if (err) { 2832 if (err) {
2766 DPRINTFN(1, "setup default pipe failed %jd", err,0,0,0); 2833 DPRINTFN(1, "setup default pipe failed %jd", err,0,0,0);
2767 goto bad; 2834 goto bad;
2768 } 2835 }
2769 err = usbd_get_initial_ddesc(dev, dd); 2836 err = usbd_get_initial_ddesc(dev, dd);
2770 if (err) { 2837 if (err) {
2771 DPRINTFN(1, "get_initial_ddesc %ju", err, 0, 0, 0); 2838 DPRINTFN(1, "get_initial_ddesc %ju", err, 0, 0, 0);
2772 goto bad; 2839 goto bad;
2773 } 2840 }
2774 } else { 2841 } else {
2775 uint8_t slot = 0; 2842 uint8_t slot = 0;
2776 2843
2777 /* 4.3.2 */ 2844 /* 4.3.2 */
2778 err = xhci_enable_slot(sc, &slot); 2845 err = xhci_enable_slot(sc, &slot);
2779 if (err) { 2846 if (err) {
2780 DPRINTFN(1, "enable slot %ju", err, 0, 0, 0); 2847 DPRINTFN(1, "enable slot %ju", err, 0, 0, 0);
2781 goto bad; 2848 goto bad;
2782 } 2849 }
2783 2850
2784 xs = &sc->sc_slots[slot]; 2851 xs = &sc->sc_slots[slot];
2785 dev->ud_hcpriv = xs; 2852 dev->ud_hcpriv = xs;
2786 2853
2787 /* 4.3.3 initialize slot structure */ 2854 /* 4.3.3 initialize slot structure */
2788 err = xhci_init_slot(dev, slot); 2855 err = xhci_init_slot(dev, slot);
2789 if (err) { 2856 if (err) {
2790 DPRINTFN(1, "init slot %ju", err, 0, 0, 0); 2857 DPRINTFN(1, "init slot %ju", err, 0, 0, 0);
2791 dev->ud_hcpriv = NULL; 2858 dev->ud_hcpriv = NULL;
2792 /* 2859 /*
2793 * We have to disable_slot here because 2860 * We have to disable_slot here because
2794 * xs->xs_idx == 0 when xhci_init_slot fails, 2861 * xs->xs_idx == 0 when xhci_init_slot fails,
2795 * in that case usbd_remove_dev won't work. 2862 * in that case usbd_remove_dev won't work.
2796 */ 2863 */
2797 mutex_enter(&sc->sc_lock); 2864 mutex_enter(&sc->sc_lock);
2798 xhci_disable_slot(sc, slot); 2865 xhci_disable_slot(sc, slot);
2799 mutex_exit(&sc->sc_lock); 2866 mutex_exit(&sc->sc_lock);
2800 goto bad; 2867 goto bad;
2801 } 2868 }
2802 2869
2803 /* 2870 /*
2804 * We have to establish the default pipe _after_ slot 2871 * We have to establish the default pipe _after_ slot
2805 * structure has been prepared. 2872 * structure has been prepared.
2806 */ 2873 */
2807 err = usbd_setup_pipe(dev, 0, &dev->ud_ep0, 2874 err = usbd_setup_pipe(dev, 0, &dev->ud_ep0,
2808 USBD_DEFAULT_INTERVAL, &dev->ud_pipe0); 2875 USBD_DEFAULT_INTERVAL, &dev->ud_pipe0);
2809 if (err) { 2876 if (err) {
2810 DPRINTFN(1, "setup default pipe failed %jd", err, 0, 0, 2877 DPRINTFN(1, "setup default pipe failed %jd", err, 0, 0,
2811 0); 2878 0);
2812 goto bad; 2879 goto bad;
2813 } 2880 }
2814 2881
2815 /* 4.3.4 Address Assignment */ 2882 /* 4.3.4 Address Assignment */
2816 err = xhci_set_address(dev, slot, false); 2883 err = xhci_set_address(dev, slot, false);
2817 if (err) { 2884 if (err) {
2818 DPRINTFN(1, "failed! to set address: %ju", err, 0, 0, 0); 2885 DPRINTFN(1, "failed! to set address: %ju", err, 0, 0, 0);
2819 goto bad; 2886 goto bad;
2820 } 2887 }
2821 2888
2822 /* Allow device time to set new address */ 2889 /* Allow device time to set new address */
2823 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE); 2890 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
2824 2891
2825 usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD); 2892 usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD);
2826 cp = xhci_slot_get_dcv(sc, xs, XHCI_DCI_SLOT); 2893 cp = xhci_slot_get_dcv(sc, xs, XHCI_DCI_SLOT);
2827 HEXDUMP("slot context", cp, sc->sc_ctxsz); 2894 HEXDUMP("slot context", cp, sc->sc_ctxsz);
2828 uint8_t addr = XHCI_SCTX_3_DEV_ADDR_GET(le32toh(cp[3])); 2895 uint8_t addr = XHCI_SCTX_3_DEV_ADDR_GET(le32toh(cp[3]));
2829 DPRINTFN(4, "device address %ju", addr, 0, 0, 0); 2896 DPRINTFN(4, "device address %ju", addr, 0, 0, 0);
2830 /* 2897 /*
2831 * XXX ensure we know when the hardware does something 2898 * XXX ensure we know when the hardware does something
2832 * we can't yet cope with 2899 * we can't yet cope with
2833 */ 2900 */
2834 KASSERTMSG(addr >= 1 && addr <= 127, "addr %d", addr); 2901 KASSERTMSG(addr >= 1 && addr <= 127, "addr %d", addr);
2835 dev->ud_addr = addr; 2902 dev->ud_addr = addr;
2836 2903
2837 KASSERTMSG(bus->ub_devices[usb_addr2dindex(dev->ud_addr)] == NULL, 2904 KASSERTMSG(bus->ub_devices[usb_addr2dindex(dev->ud_addr)] == NULL,
2838 "addr %d already allocated", dev->ud_addr); 2905 "addr %d already allocated", dev->ud_addr);
2839 /* 2906 /*
2840 * The root hub is given its own slot 2907 * The root hub is given its own slot
2841 */ 2908 */
2842 bus->ub_devices[usb_addr2dindex(dev->ud_addr)] = dev; 2909 bus->ub_devices[usb_addr2dindex(dev->ud_addr)] = dev;
2843 2910
2844 err = usbd_get_initial_ddesc(dev, dd); 2911 err = usbd_get_initial_ddesc(dev, dd);
2845 if (err) { 2912 if (err) {
2846 DPRINTFN(1, "get_initial_ddesc %ju", err, 0, 0, 0); 2913 DPRINTFN(1, "get_initial_ddesc %ju", err, 0, 0, 0);
2847 goto bad; 2914 goto bad;
2848 } 2915 }
2849 2916
2850 /* 4.8.2.1 */ 2917 /* 4.8.2.1 */
2851 if (USB_IS_SS(speed)) { 2918 if (USB_IS_SS(speed)) {
2852 if (dd->bMaxPacketSize != 9) { 2919 if (dd->bMaxPacketSize != 9) {
2853 printf("%s: invalid mps 2^%u for SS ep0," 2920 printf("%s: invalid mps 2^%u for SS ep0,"
2854 " using 512\n", 2921 " using 512\n",
2855 device_xname(sc->sc_dev), 2922 device_xname(sc->sc_dev),
2856 dd->bMaxPacketSize); 2923 dd->bMaxPacketSize);
2857 dd->bMaxPacketSize = 9; 2924 dd->bMaxPacketSize = 9;
2858 } 2925 }
2859 USETW(dev->ud_ep0desc.wMaxPacketSize, 2926 USETW(dev->ud_ep0desc.wMaxPacketSize,
2860 (1 << dd->bMaxPacketSize)); 2927 (1 << dd->bMaxPacketSize));
2861 } else 2928 } else
2862 USETW(dev->ud_ep0desc.wMaxPacketSize, 2929 USETW(dev->ud_ep0desc.wMaxPacketSize,
2863 dd->bMaxPacketSize); 2930 dd->bMaxPacketSize);
2864 DPRINTFN(4, "bMaxPacketSize %ju", dd->bMaxPacketSize, 0, 0, 0); 2931 DPRINTFN(4, "bMaxPacketSize %ju", dd->bMaxPacketSize, 0, 0, 0);
2865 err = xhci_update_ep0_mps(sc, xs, 2932 err = xhci_update_ep0_mps(sc, xs,
2866 UGETW(dev->ud_ep0desc.wMaxPacketSize)); 2933 UGETW(dev->ud_ep0desc.wMaxPacketSize));
2867 if (err) { 2934 if (err) {
2868 DPRINTFN(1, "update mps of ep0 %ju", err, 0, 0, 0); 2935 DPRINTFN(1, "update mps of ep0 %ju", err, 0, 0, 0);
2869 goto bad; 2936 goto bad;
2870 } 2937 }
2871 } 2938 }
2872 2939
2873 err = usbd_reload_device_desc(dev); 2940 err = usbd_reload_device_desc(dev);
2874 if (err) { 2941 if (err) {
2875 DPRINTFN(1, "reload desc %ju", err, 0, 0, 0); 2942 DPRINTFN(1, "reload desc %ju", err, 0, 0, 0);
2876 goto bad; 2943 goto bad;
2877 } 2944 }
2878 2945
2879 DPRINTFN(1, "adding unit addr=%jd, rev=%02jx,", 2946 DPRINTFN(1, "adding unit addr=%jd, rev=%02jx,",
2880 dev->ud_addr, UGETW(dd->bcdUSB), 0, 0); 2947 dev->ud_addr, UGETW(dd->bcdUSB), 0, 0);
2881 DPRINTFN(1, " class=%jd, subclass=%jd, protocol=%jd,", 2948 DPRINTFN(1, " class=%jd, subclass=%jd, protocol=%jd,",
2882 dd->bDeviceClass, dd->bDeviceSubClass, 2949 dd->bDeviceClass, dd->bDeviceSubClass,
2883 dd->bDeviceProtocol, 0); 2950 dd->bDeviceProtocol, 0);
2884 DPRINTFN(1, " mps=%jd, len=%jd, noconf=%jd, speed=%jd", 2951 DPRINTFN(1, " mps=%jd, len=%jd, noconf=%jd, speed=%jd",
2885 dd->bMaxPacketSize, dd->bLength, dd->bNumConfigurations, 2952 dd->bMaxPacketSize, dd->bLength, dd->bNumConfigurations,
2886 dev->ud_speed); 2953 dev->ud_speed);
2887 2954
2888 usbd_get_device_strings(dev); 2955 usbd_get_device_strings(dev);
2889 2956
2890 usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev); 2957 usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
2891 2958
2892 if (depth == 0 && port == 0) { 2959 if (depth == 0 && port == 0) {
2893 usbd_attach_roothub(parent, dev); 2960 usbd_attach_roothub(parent, dev);
2894 DPRINTFN(1, "root hub %#jx", (uintptr_t)dev, 0, 0, 0); 2961 DPRINTFN(1, "root hub %#jx", (uintptr_t)dev, 0, 0, 0);
2895 return USBD_NORMAL_COMPLETION; 2962 return USBD_NORMAL_COMPLETION;
2896 } 2963 }
2897 2964
2898 err = usbd_probe_and_attach(parent, dev, port, dev->ud_addr); 2965 err = usbd_probe_and_attach(parent, dev, port, dev->ud_addr);
2899 bad: 2966 bad:
2900 if (err != USBD_NORMAL_COMPLETION) { 2967 if (err != USBD_NORMAL_COMPLETION) {
2901 usbd_remove_device(dev, up); 2968 usbd_remove_device(dev, up);
2902 } 2969 }
2903 2970
2904 return err; 2971 return err;
2905} 2972}
2906 2973
2907static usbd_status 2974static usbd_status
2908xhci_ring_init(struct xhci_softc * const sc, struct xhci_ring **xrp, 2975xhci_ring_init(struct xhci_softc * const sc, struct xhci_ring **xrp,
2909 size_t ntrb, size_t align) 2976 size_t ntrb, size_t align)
2910{ 2977{
2911 size_t size = ntrb * XHCI_TRB_SIZE; 2978 size_t size = ntrb * XHCI_TRB_SIZE;
2912 struct xhci_ring *xr; 2979 struct xhci_ring *xr;
2913 2980
2914 XHCIHIST_FUNC(); 2981 XHCIHIST_FUNC();
2915 XHCIHIST_CALLARGS("xr %#jx ntrb %#jx align %#jx", 2982 XHCIHIST_CALLARGS("xr %#jx ntrb %#jx align %#jx",
2916 (uintptr_t)*xrp, ntrb, align, 0); 2983 (uintptr_t)*xrp, ntrb, align, 0);
2917 2984
2918 xr = kmem_zalloc(sizeof(struct xhci_ring), KM_SLEEP); 2985 xr = kmem_zalloc(sizeof(struct xhci_ring), KM_SLEEP);
2919 DPRINTFN(1, "ring %#jx", (uintptr_t)xr, 0, 0, 0); 2986 DPRINTFN(1, "ring %#jx", (uintptr_t)xr, 0, 0, 0);
2920 2987
2921 int err = usb_allocmem(&sc->sc_bus, size, align, 2988 int err = usb_allocmem(&sc->sc_bus, size, align,
2922 USBMALLOC_COHERENT | USBMALLOC_ZERO, &xr->xr_dma); 2989 USBMALLOC_COHERENT | USBMALLOC_ZERO, &xr->xr_dma);
2923 if (err) { 2990 if (err) {
2924 kmem_free(xr, sizeof(struct xhci_ring)); 2991 kmem_free(xr, sizeof(struct xhci_ring));
2925 DPRINTFN(1, "alloc xr_dma failed %jd", err, 0, 0, 0); 2992 DPRINTFN(1, "alloc xr_dma failed %jd", err, 0, 0, 0);
2926 return err; 2993 return err;
2927 } 2994 }
2928 mutex_init(&xr->xr_lock, MUTEX_DEFAULT, IPL_SOFTUSB); 2995 mutex_init(&xr->xr_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
2929 xr->xr_cookies = kmem_zalloc(sizeof(*xr->xr_cookies) * ntrb, KM_SLEEP); 2996 xr->xr_cookies = kmem_zalloc(sizeof(*xr->xr_cookies) * ntrb, KM_SLEEP);
2930 xr->xr_trb = xhci_ring_trbv(xr, 0); 2997 xr->xr_trb = xhci_ring_trbv(xr, 0);
2931 xr->xr_ntrb = ntrb; 2998 xr->xr_ntrb = ntrb;
2932 xr->is_halted = false; 2999 xr->is_halted = false;
2933 xhci_host_dequeue(xr); 3000 xhci_host_dequeue(xr);
2934 *xrp = xr; 3001 *xrp = xr;
2935 3002
2936 return USBD_NORMAL_COMPLETION; 3003 return USBD_NORMAL_COMPLETION;
2937} 3004}
2938 3005
2939static void 3006static void
2940xhci_ring_free(struct xhci_softc * const sc, struct xhci_ring ** const xr) 3007xhci_ring_free(struct xhci_softc * const sc, struct xhci_ring ** const xr)
2941{ 3008{
2942 if (*xr == NULL) 3009 if (*xr == NULL)
2943 return; 3010 return;
2944 3011
2945 usb_freemem(&sc->sc_bus, &(*xr)->xr_dma); 3012 usb_freemem(&sc->sc_bus, &(*xr)->xr_dma);
2946 mutex_destroy(&(*xr)->xr_lock); 3013 mutex_destroy(&(*xr)->xr_lock);
2947 kmem_free((*xr)->xr_cookies, 3014 kmem_free((*xr)->xr_cookies,
2948 sizeof(*(*xr)->xr_cookies) * (*xr)->xr_ntrb); 3015 sizeof(*(*xr)->xr_cookies) * (*xr)->xr_ntrb);
2949 kmem_free(*xr, sizeof(struct xhci_ring)); 3016 kmem_free(*xr, sizeof(struct xhci_ring));
2950 *xr = NULL; 3017 *xr = NULL;
2951} 3018}
2952 3019
2953static void 3020static void
2954xhci_ring_put(struct xhci_softc * const sc, struct xhci_ring * const xr, 3021xhci_ring_put(struct xhci_softc * const sc, struct xhci_ring * const xr,
2955 void *cookie, struct xhci_soft_trb * const trbs, size_t ntrbs) 3022 void *cookie, struct xhci_soft_trb * const trbs, size_t ntrbs)
2956{ 3023{
2957 size_t i; 3024 size_t i;
2958 u_int ri; 3025 u_int ri;
2959 u_int cs; 3026 u_int cs;
2960 uint64_t parameter; 3027 uint64_t parameter;
2961 uint32_t status; 3028 uint32_t status;
2962 uint32_t control; 3029 uint32_t control;
2963 3030
2964 XHCIHIST_FUNC(); 3031 XHCIHIST_FUNC();
2965 XHCIHIST_CALLARGS("%#jx xr_ep %#jx xr_cs %ju", 3032 XHCIHIST_CALLARGS("%#jx xr_ep %#jx xr_cs %ju",
2966 (uintptr_t)xr, xr->xr_ep, xr->xr_cs, 0); 3033 (uintptr_t)xr, xr->xr_ep, xr->xr_cs, 0);
2967 3034
2968 KASSERTMSG(ntrbs < xr->xr_ntrb, "ntrbs %zu, xr->xr_ntrb %u", 3035 KASSERTMSG(ntrbs < xr->xr_ntrb, "ntrbs %zu, xr->xr_ntrb %u",
2969 ntrbs, xr->xr_ntrb); 3036 ntrbs, xr->xr_ntrb);
2970 for (i = 0; i < ntrbs; i++) { 3037 for (i = 0; i < ntrbs; i++) {
2971 DPRINTFN(12, "xr %#jx trbs %#jx num %ju", (uintptr_t)xr, 3038 DPRINTFN(12, "xr %#jx trbs %#jx num %ju", (uintptr_t)xr,
2972 (uintptr_t)trbs, i, 0); 3039 (uintptr_t)trbs, i, 0);
2973 DPRINTFN(12, " 0x%016jx 0x%08jx 0x%08jx", 3040 DPRINTFN(12, " 0x%016jx 0x%08jx 0x%08jx",
2974 trbs[i].trb_0, trbs[i].trb_2, trbs[i].trb_3, 0); 3041 trbs[i].trb_0, trbs[i].trb_2, trbs[i].trb_3, 0);
2975 KASSERTMSG(XHCI_TRB_3_TYPE_GET(trbs[i].trb_3) != 3042 KASSERTMSG(XHCI_TRB_3_TYPE_GET(trbs[i].trb_3) !=
2976 XHCI_TRB_TYPE_LINK, "trbs[%zu].trb3 %#x", i, trbs[i].trb_3); 3043 XHCI_TRB_TYPE_LINK, "trbs[%zu].trb3 %#x", i, trbs[i].trb_3);
2977 } 3044 }
2978 3045
2979 ri = xr->xr_ep; 3046 ri = xr->xr_ep;
2980 cs = xr->xr_cs; 3047 cs = xr->xr_cs;
2981 3048
2982 /* 3049 /*
2983 * Although the xhci hardware can do scatter/gather dma from 3050 * Although the xhci hardware can do scatter/gather dma from
2984 * arbitrary sized buffers, there is a non-obvious restriction 3051 * arbitrary sized buffers, there is a non-obvious restriction
2985 * that a LINK trb is only allowed at the end of a burst of 3052 * that a LINK trb is only allowed at the end of a burst of
2986 * transfers - which might be 16kB. 3053 * transfers - which might be 16kB.
2987 * Arbitrary aligned LINK trb definitely fail on Ivy bridge. 3054 * Arbitrary aligned LINK trb definitely fail on Ivy bridge.
2988 * The simple solution is not to allow a LINK trb in the middle 3055 * The simple solution is not to allow a LINK trb in the middle
2989 * of anything - as here. 3056 * of anything - as here.
2990 * XXX: (dsl) There are xhci controllers out there (eg some made by 3057 * XXX: (dsl) There are xhci controllers out there (eg some made by
2991 * ASMedia) that seem to lock up if they process a LINK trb but 3058 * ASMedia) that seem to lock up if they process a LINK trb but
2992 * cannot process the linked-to trb yet. 3059 * cannot process the linked-to trb yet.
2993 * The code should write the 'cycle' bit on the link trb AFTER 3060 * The code should write the 'cycle' bit on the link trb AFTER
2994 * adding the other trb. 3061 * adding the other trb.
2995 */ 3062 */
2996 u_int firstep = xr->xr_ep; 3063 u_int firstep = xr->xr_ep;
2997 u_int firstcs = xr->xr_cs; 3064 u_int firstcs = xr->xr_cs;
2998 3065
2999 for (i = 0; i < ntrbs; ) { 3066 for (i = 0; i < ntrbs; ) {
3000 u_int oldri = ri; 3067 u_int oldri = ri;
3001 u_int oldcs = cs; 3068 u_int oldcs = cs;
3002 3069
3003 if (ri >= (xr->xr_ntrb - 1)) { 3070 if (ri >= (xr->xr_ntrb - 1)) {
3004 /* Put Link TD at the end of ring */ 3071 /* Put Link TD at the end of ring */
3005 parameter = xhci_ring_trbp(xr, 0); 3072 parameter = xhci_ring_trbp(xr, 0);
3006 status = 0; 3073 status = 0;
3007 control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK) | 3074 control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK) |
3008 XHCI_TRB_3_TC_BIT; 3075 XHCI_TRB_3_TC_BIT;
3009 xr->xr_cookies[ri] = NULL; 3076 xr->xr_cookies[ri] = NULL;
3010 xr->xr_ep = 0; 3077 xr->xr_ep = 0;
3011 xr->xr_cs ^= 1; 3078 xr->xr_cs ^= 1;
3012 ri = xr->xr_ep; 3079 ri = xr->xr_ep;
3013 cs = xr->xr_cs; 3080 cs = xr->xr_cs;
3014 } else { 3081 } else {
3015 parameter = trbs[i].trb_0; 3082 parameter = trbs[i].trb_0;
3016 status = trbs[i].trb_2; 3083 status = trbs[i].trb_2;
3017 control = trbs[i].trb_3; 3084 control = trbs[i].trb_3;
3018 3085
3019 xr->xr_cookies[ri] = cookie; 3086 xr->xr_cookies[ri] = cookie;
3020 ri++; 3087 ri++;
3021 i++; 3088 i++;
3022 } 3089 }
3023 /* 3090 /*
3024 * If this is a first TRB, mark it invalid to prevent 3091 * If this is a first TRB, mark it invalid to prevent
3025 * xHC from running it immediately. 3092 * xHC from running it immediately.
3026 */ 3093 */
3027 if (oldri == firstep) { 3094 if (oldri == firstep) {
3028 if (oldcs) { 3095 if (oldcs) {
3029 control &= ~XHCI_TRB_3_CYCLE_BIT; 3096 control &= ~XHCI_TRB_3_CYCLE_BIT;
3030 } else { 3097 } else {
3031 control |= XHCI_TRB_3_CYCLE_BIT; 3098 control |= XHCI_TRB_3_CYCLE_BIT;
3032 } 3099 }
3033 } else { 3100 } else {
3034 if (oldcs) { 3101 if (oldcs) {
3035 control |= XHCI_TRB_3_CYCLE_BIT; 3102 control |= XHCI_TRB_3_CYCLE_BIT;
3036 } else { 3103 } else {
3037 control &= ~XHCI_TRB_3_CYCLE_BIT; 3104 control &= ~XHCI_TRB_3_CYCLE_BIT;
3038 } 3105 }
3039 } 3106 }
3040 xhci_trb_put(&xr->xr_trb[oldri], parameter, status, control); 3107 xhci_trb_put(&xr->xr_trb[oldri], parameter, status, control);
3041 usb_syncmem(&xr->xr_dma, XHCI_TRB_SIZE * oldri, 3108 usb_syncmem(&xr->xr_dma, XHCI_TRB_SIZE * oldri,
3042 XHCI_TRB_SIZE * 1, BUS_DMASYNC_PREWRITE); 3109 XHCI_TRB_SIZE * 1, BUS_DMASYNC_PREWRITE);
3043 } 3110 }
3044 3111
3045 /* Now invert cycle bit of first TRB */ 3112 /* Now invert cycle bit of first TRB */
3046 if (firstcs) { 3113 if (firstcs) {
3047 xr->xr_trb[firstep].trb_3 |= htole32(XHCI_TRB_3_CYCLE_BIT); 3114 xr->xr_trb[firstep].trb_3 |= htole32(XHCI_TRB_3_CYCLE_BIT);
3048 } else { 3115 } else {
3049 xr->xr_trb[firstep].trb_3 &= ~htole32(XHCI_TRB_3_CYCLE_BIT); 3116 xr->xr_trb[firstep].trb_3 &= ~htole32(XHCI_TRB_3_CYCLE_BIT);
3050 } 3117 }
3051 usb_syncmem(&xr->xr_dma, XHCI_TRB_SIZE * firstep, 3118 usb_syncmem(&xr->xr_dma, XHCI_TRB_SIZE * firstep,
3052 XHCI_TRB_SIZE * 1, BUS_DMASYNC_PREWRITE); 3119 XHCI_TRB_SIZE * 1, BUS_DMASYNC_PREWRITE);
3053 3120
3054 xr->xr_ep = ri; 3121 xr->xr_ep = ri;
3055 xr->xr_cs = cs; 3122 xr->xr_cs = cs;
3056 3123
3057 DPRINTFN(12, "%#jx xr_ep %#jx xr_cs %ju", (uintptr_t)xr, xr->xr_ep, 3124 DPRINTFN(12, "%#jx xr_ep %#jx xr_cs %ju", (uintptr_t)xr, xr->xr_ep,
3058 xr->xr_cs, 0); 3125 xr->xr_cs, 0);
3059} 3126}
3060 3127
3061static inline void 3128static inline void
3062xhci_ring_put_xfer(struct xhci_softc * const sc, struct xhci_ring * const tr, 3129xhci_ring_put_xfer(struct xhci_softc * const sc, struct xhci_ring * const tr,
3063 struct xhci_xfer *xx, u_int ntrb) 3130 struct xhci_xfer *xx, u_int ntrb)
3064{ 3131{
3065 KASSERT(ntrb <= xx->xx_ntrb); 3132 KASSERT(ntrb <= xx->xx_ntrb);
3066 xhci_ring_put(sc, tr, xx, xx->xx_trb, ntrb); 3133 xhci_ring_put(sc, tr, xx, xx->xx_trb, ntrb);
3067} 3134}
3068 3135
3069/* 3136/*
3070 * Stop execution commands, purge all commands on command ring, and 3137 * Stop execution commands, purge all commands on command ring, and
3071 * rewind dequeue pointer. 3138 * rewind dequeue pointer.
3072 */ 3139 */
3073static void 3140static void
3074xhci_abort_command(struct xhci_softc *sc) 3141xhci_abort_command(struct xhci_softc *sc)
3075{ 3142{
3076 struct xhci_ring * const cr = sc->sc_cr; 3143 struct xhci_ring * const cr = sc->sc_cr;
3077 uint64_t crcr; 3144 uint64_t crcr;
3078 int i; 3145 int i;
3079 3146
3080 XHCIHIST_FUNC(); 3147 XHCIHIST_FUNC();
3081 XHCIHIST_CALLARGS("command %#jx timeout, aborting", 3148 XHCIHIST_CALLARGS("command %#jx timeout, aborting",
3082 sc->sc_command_addr, 0, 0, 0); 3149 sc->sc_command_addr, 0, 0, 0);
3083 3150
3084 mutex_enter(&cr->xr_lock); 3151 mutex_enter(&cr->xr_lock);
3085 3152
3086 /* 4.6.1.2 Aborting a Command */ 3153 /* 4.6.1.2 Aborting a Command */
3087 crcr = xhci_op_read_8(sc, XHCI_CRCR); 3154 crcr = xhci_op_read_8(sc, XHCI_CRCR);
3088 xhci_op_write_8(sc, XHCI_CRCR, crcr | XHCI_CRCR_LO_CA); 3155 xhci_op_write_8(sc, XHCI_CRCR, crcr | XHCI_CRCR_LO_CA);
3089 3156
3090 for (i = 0; i < 500; i++) { 3157 for (i = 0; i < 500; i++) {
3091 crcr = xhci_op_read_8(sc, XHCI_CRCR); 3158 crcr = xhci_op_read_8(sc, XHCI_CRCR);
3092 if ((crcr & XHCI_CRCR_LO_CRR) == 0) 3159 if ((crcr & XHCI_CRCR_LO_CRR) == 0)
3093 break; 3160 break;
3094 usb_delay_ms(&sc->sc_bus, 1); 3161 usb_delay_ms(&sc->sc_bus, 1);
3095 } 3162 }
3096 if ((crcr & XHCI_CRCR_LO_CRR) != 0) { 3163 if ((crcr & XHCI_CRCR_LO_CRR) != 0) {
3097 DPRINTFN(1, "Command Abort timeout", 0, 0, 0, 0); 3164 DPRINTFN(1, "Command Abort timeout", 0, 0, 0, 0);
3098 /* reset HC here? */ 3165 /* reset HC here? */
3099 } 3166 }
3100 3167
3101 /* reset command ring dequeue pointer */ 3168 /* reset command ring dequeue pointer */
3102 cr->xr_ep = 0; 3169 cr->xr_ep = 0;
3103 cr->xr_cs = 1; 3170 cr->xr_cs = 1;
3104 xhci_op_write_8(sc, XHCI_CRCR, xhci_ring_trbp(cr, 0) | cr->xr_cs); 3171 xhci_op_write_8(sc, XHCI_CRCR, xhci_ring_trbp(cr, 0) | cr->xr_cs);
3105 3172
3106 mutex_exit(&cr->xr_lock); 3173 mutex_exit(&cr->xr_lock);
3107} 3174}
3108 3175
3109/* 3176/*
3110 * Put a command on command ring, ring bell, set timer, and cv_timedwait. 3177 * Put a command on command ring, ring bell, set timer, and cv_timedwait.
3111 * Command completion is notified by cv_signal from xhci_event_cmd() 3178 * Command completion is notified by cv_signal from xhci_event_cmd()
3112 * (called from xhci_softint), or timed-out. 3179 * (called from xhci_softint), or timed-out.
3113 * The completion code is copied to sc->sc_result_trb in xhci_event_cmd(), 3180 * The completion code is copied to sc->sc_result_trb in xhci_event_cmd(),
3114 * then do_command examines it. 3181 * then do_command examines it.
3115 */ 3182 */
3116static usbd_status 3183static usbd_status
3117xhci_do_command_locked(struct xhci_softc * const sc, 3184xhci_do_command_locked(struct xhci_softc * const sc,
3118 struct xhci_soft_trb * const trb, int timeout) 3185 struct xhci_soft_trb * const trb, int timeout)
3119{ 3186{
3120 struct xhci_ring * const cr = sc->sc_cr; 3187 struct xhci_ring * const cr = sc->sc_cr;
3121 usbd_status err; 3188 usbd_status err;
3122 3189
3123 XHCIHIST_FUNC(); 3190 XHCIHIST_FUNC();
3124 XHCIHIST_CALLARGS("input: 0x%016jx 0x%08jx 0x%08jx", 3191 XHCIHIST_CALLARGS("input: 0x%016jx 0x%08jx 0x%08jx",
3125 trb->trb_0, trb->trb_2, trb->trb_3, 0); 3192 trb->trb_0, trb->trb_2, trb->trb_3, 0);
3126 3193
3127 KASSERTMSG(!cpu_intr_p() && !cpu_softintr_p(), "called from intr ctx"); 3194 KASSERTMSG(!cpu_intr_p() && !cpu_softintr_p(), "called from intr ctx");
3128 KASSERT(mutex_owned(&sc->sc_lock)); 3195 KASSERT(mutex_owned(&sc->sc_lock));
3129 3196
3130 while (sc->sc_command_addr != 0) 3197 while (sc->sc_command_addr != 0 &&
 3198 sc->sc_suspender != NULL &&
 3199 sc->sc_suspender != curlwp)
3131 cv_wait(&sc->sc_cmdbusy_cv, &sc->sc_lock); 3200 cv_wait(&sc->sc_cmdbusy_cv, &sc->sc_lock);
3132 3201
3133 /* 3202 /*
3134 * If enqueue pointer points at last of ring, it's Link TRB, 3203 * If enqueue pointer points at last of ring, it's Link TRB,
3135 * command TRB will be stored in 0th TRB. 3204 * command TRB will be stored in 0th TRB.
3136 */ 3205 */
3137 if (cr->xr_ep == cr->xr_ntrb - 1) 3206 if (cr->xr_ep == cr->xr_ntrb - 1)
3138 sc->sc_command_addr = xhci_ring_trbp(cr, 0); 3207 sc->sc_command_addr = xhci_ring_trbp(cr, 0);
3139 else 3208 else
3140 sc->sc_command_addr = xhci_ring_trbp(cr, cr->xr_ep); 3209 sc->sc_command_addr = xhci_ring_trbp(cr, cr->xr_ep);
3141 3210
3142 sc->sc_resultpending = true; 3211 sc->sc_resultpending = true;
3143 3212
3144 mutex_enter(&cr->xr_lock); 3213 mutex_enter(&cr->xr_lock);
3145 xhci_ring_put(sc, cr, NULL, trb, 1); 3214 xhci_ring_put(sc, cr, NULL, trb, 1);
3146 mutex_exit(&cr->xr_lock); 3215 mutex_exit(&cr->xr_lock);
3147 3216
3148 xhci_db_write_4(sc, XHCI_DOORBELL(0), 0); 3217 xhci_db_write_4(sc, XHCI_DOORBELL(0), 0);
3149 3218
3150 while (sc->sc_resultpending) { 3219 while (sc->sc_resultpending) {
3151 if (cv_timedwait(&sc->sc_command_cv, &sc->sc_lock, 3220 if (cv_timedwait(&sc->sc_command_cv, &sc->sc_lock,
3152 MAX(1, mstohz(timeout))) == EWOULDBLOCK) { 3221 MAX(1, mstohz(timeout))) == EWOULDBLOCK) {
3153 xhci_abort_command(sc); 3222 xhci_abort_command(sc);
3154 err = USBD_TIMEOUT; 3223 err = USBD_TIMEOUT;
3155 goto timedout; 3224 goto timedout;
3156 } 3225 }
3157 } 3226 }
3158 3227
3159 trb->trb_0 = sc->sc_result_trb.trb_0; 3228 trb->trb_0 = sc->sc_result_trb.trb_0;
3160 trb->trb_2 = sc->sc_result_trb.trb_2; 3229 trb->trb_2 = sc->sc_result_trb.trb_2;
3161 trb->trb_3 = sc->sc_result_trb.trb_3; 3230 trb->trb_3 = sc->sc_result_trb.trb_3;
3162 3231
3163 DPRINTFN(12, "output: 0x%016jx 0x%08jx 0x%08jx", 3232 DPRINTFN(12, "output: 0x%016jx 0x%08jx 0x%08jx",
3164 trb->trb_0, trb->trb_2, trb->trb_3, 0); 3233 trb->trb_0, trb->trb_2, trb->trb_3, 0);
3165 3234
3166 switch (XHCI_TRB_2_ERROR_GET(trb->trb_2)) { 3235 switch (XHCI_TRB_2_ERROR_GET(trb->trb_2)) {
3167 case XHCI_TRB_ERROR_SUCCESS: 3236 case XHCI_TRB_ERROR_SUCCESS:
3168 err = USBD_NORMAL_COMPLETION; 3237 err = USBD_NORMAL_COMPLETION;
3169 break; 3238 break;
3170 default: 3239 default:
3171 case 192 ... 223: 3240 case 192 ... 223:
3172 DPRINTFN(5, "error %#jx", 3241 DPRINTFN(5, "error %#jx",
3173 XHCI_TRB_2_ERROR_GET(trb->trb_2), 0, 0, 0); 3242 XHCI_TRB_2_ERROR_GET(trb->trb_2), 0, 0, 0);
3174 err = USBD_IOERROR; 3243 err = USBD_IOERROR;
3175 break; 3244 break;
3176 case 224 ... 255: 3245 case 224 ... 255:
3177 err = USBD_NORMAL_COMPLETION; 3246 err = USBD_NORMAL_COMPLETION;
3178 break; 3247 break;
3179 } 3248 }
3180 3249
3181timedout: 3250timedout:
3182 sc->sc_resultpending = false; 3251 sc->sc_resultpending = false;
3183 sc->sc_command_addr = 0; 3252 sc->sc_command_addr = 0;
3184 cv_broadcast(&sc->sc_cmdbusy_cv); 3253 cv_broadcast(&sc->sc_cmdbusy_cv);
3185 3254
3186 return err; 3255 return err;
3187} 3256}
3188 3257
3189static usbd_status 3258static usbd_status
3190xhci_do_command(struct xhci_softc * const sc, struct xhci_soft_trb * const trb, 3259xhci_do_command(struct xhci_softc * const sc, struct xhci_soft_trb * const trb,
3191 int timeout) 3260 int timeout)
3192{ 3261{
3193 3262
3194 mutex_enter(&sc->sc_lock); 3263 mutex_enter(&sc->sc_lock);
3195 usbd_status ret = xhci_do_command_locked(sc, trb, timeout); 3264 usbd_status ret = xhci_do_command_locked(sc, trb, timeout);
3196 mutex_exit(&sc->sc_lock); 3265 mutex_exit(&sc->sc_lock);
3197 3266
3198 return ret; 3267 return ret;
3199} 3268}
3200 3269
3201static usbd_status 3270static usbd_status
3202xhci_enable_slot(struct xhci_softc * const sc, uint8_t * const slotp) 3271xhci_enable_slot(struct xhci_softc * const sc, uint8_t * const slotp)
3203{ 3272{
3204 struct xhci_soft_trb trb; 3273 struct xhci_soft_trb trb;
3205 usbd_status err; 3274 usbd_status err;
3206 3275
3207 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 3276 XHCIHIST_FUNC(); XHCIHIST_CALLED();
3208 3277
3209 trb.trb_0 = 0; 3278 trb.trb_0 = 0;
3210 trb.trb_2 = 0; 3279 trb.trb_2 = 0;
3211 trb.trb_3 = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ENABLE_SLOT); 3280 trb.trb_3 = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ENABLE_SLOT);
3212 3281
3213 err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT); 3282 err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
3214 if (err != USBD_NORMAL_COMPLETION) { 3283 if (err != USBD_NORMAL_COMPLETION) {
3215 return err; 3284 return err;
3216 } 3285 }
3217 3286
3218 *slotp = XHCI_TRB_3_SLOT_GET(trb.trb_3); 3287 *slotp = XHCI_TRB_3_SLOT_GET(trb.trb_3);
3219 3288
3220 return err; 3289 return err;
3221} 3290}
3222 3291
3223/* 3292/*
3224 * xHCI 4.6.4 3293 * xHCI 4.6.4
3225 * Deallocate ring and device/input context DMA buffers, and disable_slot. 3294 * Deallocate ring and device/input context DMA buffers, and disable_slot.
3226 * All endpoints in the slot should be stopped. 3295 * All endpoints in the slot should be stopped.
3227 * Should be called with sc_lock held. 3296 * Should be called with sc_lock held.
3228 */ 3297 */
3229static usbd_status 3298static usbd_status
3230xhci_disable_slot(struct xhci_softc * const sc, uint8_t slot) 3299xhci_disable_slot(struct xhci_softc * const sc, uint8_t slot)
3231{ 3300{
3232 struct xhci_soft_trb trb; 3301 struct xhci_soft_trb trb;
3233 struct xhci_slot *xs; 3302 struct xhci_slot *xs;
3234 usbd_status err; 3303 usbd_status err;
3235 3304
3236 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 3305 XHCIHIST_FUNC(); XHCIHIST_CALLED();
3237 3306
3238 if (sc->sc_dying) 3307 if (sc->sc_dying)
3239 return USBD_IOERROR; 3308 return USBD_IOERROR;
3240 3309
3241 trb.trb_0 = 0; 3310 trb.trb_0 = 0;
3242 trb.trb_2 = 0; 3311 trb.trb_2 = 0;
3243 trb.trb_3 = XHCI_TRB_3_SLOT_SET(slot) | 3312 trb.trb_3 = XHCI_TRB_3_SLOT_SET(slot) |
3244 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_DISABLE_SLOT); 3313 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_DISABLE_SLOT);
3245 3314
3246 err = xhci_do_command_locked(sc, &trb, USBD_DEFAULT_TIMEOUT); 3315 err = xhci_do_command_locked(sc, &trb, USBD_DEFAULT_TIMEOUT);
3247 3316
3248 if (!err) { 3317 if (!err) {
3249 xs = &sc->sc_slots[slot]; 3318 xs = &sc->sc_slots[slot];
3250 if (xs->xs_idx != 0) { 3319 if (xs->xs_idx != 0) {
3251 xhci_free_slot(sc, xs); 3320 xhci_free_slot(sc, xs);
3252 xhci_set_dcba(sc, 0, slot); 3321 xhci_set_dcba(sc, 0, slot);
3253 memset(xs, 0, sizeof(*xs)); 3322 memset(xs, 0, sizeof(*xs));
3254 } 3323 }
3255 } 3324 }
3256 3325
3257 return err; 3326 return err;
3258} 3327}
3259 3328
3260/* 3329/*
3261 * Set address of device and transition slot state from ENABLED to ADDRESSED 3330 * Set address of device and transition slot state from ENABLED to ADDRESSED
3262 * if Block Setaddress Request (BSR) is false. 3331 * if Block Setaddress Request (BSR) is false.
3263 * If BSR==true, transition slot state from ENABLED to DEFAULT. 3332 * If BSR==true, transition slot state from ENABLED to DEFAULT.
3264 * see xHCI 1.1 4.5.3, 3.3.4 3333 * see xHCI 1.1 4.5.3, 3.3.4
3265 * Should be called without sc_lock held. 3334 * Should be called without sc_lock held.
3266 */ 3335 */
3267static usbd_status 3336static usbd_status
3268xhci_address_device(struct xhci_softc * const sc, 3337xhci_address_device(struct xhci_softc * const sc,
3269 uint64_t icp, uint8_t slot_id, bool bsr) 3338 uint64_t icp, uint8_t slot_id, bool bsr)
3270{ 3339{
3271 struct xhci_soft_trb trb; 3340 struct xhci_soft_trb trb;
3272 usbd_status err; 3341 usbd_status err;
3273 3342
3274 XHCIHIST_FUNC(); 3343 XHCIHIST_FUNC();
3275 if (bsr) { 3344 if (bsr) {
3276 XHCIHIST_CALLARGS("icp %#jx slot %#jx with bsr", 3345 XHCIHIST_CALLARGS("icp %#jx slot %#jx with bsr",
3277 icp, slot_id, 0, 0); 3346 icp, slot_id, 0, 0);
3278 } else { 3347 } else {
3279 XHCIHIST_CALLARGS("icp %#jx slot %#jx nobsr", 3348 XHCIHIST_CALLARGS("icp %#jx slot %#jx nobsr",
3280 icp, slot_id, 0, 0); 3349 icp, slot_id, 0, 0);
3281 } 3350 }
3282 3351
3283 trb.trb_0 = icp; 3352 trb.trb_0 = icp;
3284 trb.trb_2 = 0; 3353 trb.trb_2 = 0;
3285 trb.trb_3 = XHCI_TRB_3_SLOT_SET(slot_id) | 3354 trb.trb_3 = XHCI_TRB_3_SLOT_SET(slot_id) |
3286 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ADDRESS_DEVICE) | 3355 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ADDRESS_DEVICE) |
3287 (bsr ? XHCI_TRB_3_BSR_BIT : 0); 3356 (bsr ? XHCI_TRB_3_BSR_BIT : 0);
3288 3357
3289 err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT); 3358 err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
3290 3359
3291 if (XHCI_TRB_2_ERROR_GET(trb.trb_2) == XHCI_TRB_ERROR_NO_SLOTS) 3360 if (XHCI_TRB_2_ERROR_GET(trb.trb_2) == XHCI_TRB_ERROR_NO_SLOTS)
3292 err = USBD_NO_ADDR; 3361 err = USBD_NO_ADDR;
3293 3362
3294 return err; 3363 return err;
3295} 3364}
3296 3365
3297static usbd_status 3366static usbd_status
3298xhci_update_ep0_mps(struct xhci_softc * const sc, 3367xhci_update_ep0_mps(struct xhci_softc * const sc,
3299 struct xhci_slot * const xs, u_int mps) 3368 struct xhci_slot * const xs, u_int mps)
3300{ 3369{
3301 struct xhci_soft_trb trb; 3370 struct xhci_soft_trb trb;
3302 usbd_status err; 3371 usbd_status err;
3303 uint32_t * cp; 3372 uint32_t * cp;
3304 3373
3305 XHCIHIST_FUNC(); 3374 XHCIHIST_FUNC();
3306 XHCIHIST_CALLARGS("slot %ju mps %ju", xs->xs_idx, mps, 0, 0); 3375 XHCIHIST_CALLARGS("slot %ju mps %ju", xs->xs_idx, mps, 0, 0);
3307 3376
3308 cp = xhci_slot_get_icv(sc, xs, XHCI_ICI_INPUT_CONTROL); 3377 cp = xhci_slot_get_icv(sc, xs, XHCI_ICI_INPUT_CONTROL);
3309 cp[0] = htole32(0); 3378 cp[0] = htole32(0);
3310 cp[1] = htole32(XHCI_INCTX_1_ADD_MASK(XHCI_DCI_EP_CONTROL)); 3379 cp[1] = htole32(XHCI_INCTX_1_ADD_MASK(XHCI_DCI_EP_CONTROL));
3311 3380
3312 cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_EP_CONTROL)); 3381 cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_EP_CONTROL));
3313 cp[1] = htole32(XHCI_EPCTX_1_MAXP_SIZE_SET(mps)); 3382 cp[1] = htole32(XHCI_EPCTX_1_MAXP_SIZE_SET(mps));
3314 3383
3315 /* sync input contexts before they are read from memory */ 3384 /* sync input contexts before they are read from memory */
3316 usb_syncmem(&xs->xs_ic_dma, 0, sc->sc_pgsz, BUS_DMASYNC_PREWRITE); 3385 usb_syncmem(&xs->xs_ic_dma, 0, sc->sc_pgsz, BUS_DMASYNC_PREWRITE);
3317 HEXDUMP("input context", xhci_slot_get_icv(sc, xs, 0), 3386 HEXDUMP("input context", xhci_slot_get_icv(sc, xs, 0),
3318 sc->sc_ctxsz * 4); 3387 sc->sc_ctxsz * 4);
3319 3388
3320 trb.trb_0 = xhci_slot_get_icp(sc, xs, 0); 3389 trb.trb_0 = xhci_slot_get_icp(sc, xs, 0);
3321 trb.trb_2 = 0; 3390 trb.trb_2 = 0;
3322 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) | 3391 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
3323 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_EVALUATE_CTX); 3392 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_EVALUATE_CTX);
3324 3393
3325 err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT); 3394 err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
3326 return err; 3395 return err;
3327} 3396}
3328 3397
3329static void 3398static void
3330xhci_set_dcba(struct xhci_softc * const sc, uint64_t dcba, int si) 3399xhci_set_dcba(struct xhci_softc * const sc, uint64_t dcba, int si)
3331{ 3400{
3332 uint64_t * const dcbaa = KERNADDR(&sc->sc_dcbaa_dma, 0); 3401 uint64_t * const dcbaa = KERNADDR(&sc->sc_dcbaa_dma, 0);
3333 3402
3334 XHCIHIST_FUNC(); 3403 XHCIHIST_FUNC();
3335 XHCIHIST_CALLARGS("dcbaa %#jx dc 0x%016jx slot %jd", 3404 XHCIHIST_CALLARGS("dcbaa %#jx dc 0x%016jx slot %jd",
3336 (uintptr_t)&dcbaa[si], dcba, si, 0); 3405 (uintptr_t)&dcbaa[si], dcba, si, 0);
3337 3406
3338 dcbaa[si] = htole64(dcba); 3407 dcbaa[si] = htole64(dcba);
3339 usb_syncmem(&sc->sc_dcbaa_dma, si * sizeof(uint64_t), sizeof(uint64_t), 3408 usb_syncmem(&sc->sc_dcbaa_dma, si * sizeof(uint64_t), sizeof(uint64_t),
3340 BUS_DMASYNC_PREWRITE); 3409 BUS_DMASYNC_PREWRITE);
3341} 3410}
3342 3411
3343/* 3412/*
3344 * Allocate device and input context DMA buffer, and 3413 * Allocate device and input context DMA buffer, and
3345 * TRB DMA buffer for each endpoint. 3414 * TRB DMA buffer for each endpoint.
3346 */ 3415 */
3347static usbd_status 3416static usbd_status
3348xhci_init_slot(struct usbd_device *dev, uint32_t slot) 3417xhci_init_slot(struct usbd_device *dev, uint32_t slot)
3349{ 3418{
3350 struct xhci_softc * const sc = XHCI_BUS2SC(dev->ud_bus); 3419 struct xhci_softc * const sc = XHCI_BUS2SC(dev->ud_bus);
3351 struct xhci_slot *xs; 3420 struct xhci_slot *xs;
3352 3421
3353 XHCIHIST_FUNC(); 3422 XHCIHIST_FUNC();
3354 XHCIHIST_CALLARGS("slot %ju", slot, 0, 0, 0); 3423 XHCIHIST_CALLARGS("slot %ju", slot, 0, 0, 0);
3355 3424
3356 xs = &sc->sc_slots[slot]; 3425 xs = &sc->sc_slots[slot];
3357 3426
3358 /* allocate contexts */ 3427 /* allocate contexts */
3359 int err = usb_allocmem(&sc->sc_bus, sc->sc_pgsz, sc->sc_pgsz, 3428 int err = usb_allocmem(&sc->sc_bus, sc->sc_pgsz, sc->sc_pgsz,
3360 USBMALLOC_COHERENT | USBMALLOC_ZERO, &xs->xs_dc_dma); 3429 USBMALLOC_COHERENT | USBMALLOC_ZERO, &xs->xs_dc_dma);
3361 if (err) { 3430 if (err) {
3362 DPRINTFN(1, "failed to allocmem output device context %jd", 3431 DPRINTFN(1, "failed to allocmem output device context %jd",
3363 err, 0, 0, 0); 3432 err, 0, 0, 0);
3364 return USBD_NOMEM; 3433 return USBD_NOMEM;
3365 } 3434 }
3366 3435
3367 err = usb_allocmem(&sc->sc_bus, sc->sc_pgsz, sc->sc_pgsz, 3436 err = usb_allocmem(&sc->sc_bus, sc->sc_pgsz, sc->sc_pgsz,
3368 USBMALLOC_COHERENT | USBMALLOC_ZERO, &xs->xs_ic_dma); 3437 USBMALLOC_COHERENT | USBMALLOC_ZERO, &xs->xs_ic_dma);
3369 if (err) { 3438 if (err) {
3370 DPRINTFN(1, "failed to allocmem input device context %jd", 3439 DPRINTFN(1, "failed to allocmem input device context %jd",
3371 err, 0, 0, 0); 3440 err, 0, 0, 0);
3372 return USBD_NOMEM; 3441 return USBD_NOMEM;
3373 } 3442 }
3374 3443
3375 memset(&xs->xs_xr[0], 0, sizeof(xs->xs_xr)); 3444 memset(&xs->xs_xr[0], 0, sizeof(xs->xs_xr));
3376 xs->xs_idx = slot; 3445 xs->xs_idx = slot;
3377 3446
3378 return USBD_NORMAL_COMPLETION; 3447 return USBD_NORMAL_COMPLETION;
3379 3448
3380 usb_freemem(&sc->sc_bus, &xs->xs_dc_dma); 3449 usb_freemem(&sc->sc_bus, &xs->xs_dc_dma);
3381 xs->xs_idx = 0; 3450 xs->xs_idx = 0;
3382 return USBD_NOMEM; 3451 return USBD_NOMEM;
3383} 3452}
3384 3453
3385static void 3454static void
3386xhci_free_slot(struct xhci_softc *sc, struct xhci_slot *xs) 3455xhci_free_slot(struct xhci_softc *sc, struct xhci_slot *xs)
3387{ 3456{
3388 u_int dci; 3457 u_int dci;
3389 3458
3390 XHCIHIST_FUNC(); 3459 XHCIHIST_FUNC();
3391 XHCIHIST_CALLARGS("slot %ju", xs->xs_idx, 0, 0, 0); 3460 XHCIHIST_CALLARGS("slot %ju", xs->xs_idx, 0, 0, 0);
3392 3461
3393 /* deallocate all allocated rings in the slot */ 3462 /* deallocate all allocated rings in the slot */
3394 for (dci = XHCI_DCI_SLOT; dci <= XHCI_MAX_DCI; dci++) { 3463 for (dci = XHCI_DCI_SLOT; dci <= XHCI_MAX_DCI; dci++) {
3395 if (xs->xs_xr[dci] != NULL) 3464 if (xs->xs_xr[dci] != NULL)
3396 xhci_ring_free(sc, &xs->xs_xr[dci]); 3465 xhci_ring_free(sc, &xs->xs_xr[dci]);
3397 } 3466 }
3398 usb_freemem(&sc->sc_bus, &xs->xs_ic_dma); 3467 usb_freemem(&sc->sc_bus, &xs->xs_ic_dma);
3399 usb_freemem(&sc->sc_bus, &xs->xs_dc_dma); 3468 usb_freemem(&sc->sc_bus, &xs->xs_dc_dma);
3400 xs->xs_idx = 0; 3469 xs->xs_idx = 0;
3401} 3470}
3402 3471
3403/* 3472/*
3404 * Setup slot context, set Device Context Base Address, and issue 3473 * Setup slot context, set Device Context Base Address, and issue
3405 * Set Address Device command. 3474 * Set Address Device command.
3406 */ 3475 */
3407static usbd_status 3476static usbd_status
3408xhci_set_address(struct usbd_device *dev, uint32_t slot, bool bsr) 3477xhci_set_address(struct usbd_device *dev, uint32_t slot, bool bsr)
3409{ 3478{
3410 struct xhci_softc * const sc = XHCI_BUS2SC(dev->ud_bus); 3479 struct xhci_softc * const sc = XHCI_BUS2SC(dev->ud_bus);
3411 struct xhci_slot *xs; 3480 struct xhci_slot *xs;
3412 usbd_status err; 3481 usbd_status err;
3413 3482
3414 XHCIHIST_FUNC(); 3483 XHCIHIST_FUNC();
3415 XHCIHIST_CALLARGS("slot %ju bsr %ju", slot, bsr, 0, 0); 3484 XHCIHIST_CALLARGS("slot %ju bsr %ju", slot, bsr, 0, 0);
3416 3485
3417 xs = &sc->sc_slots[slot]; 3486 xs = &sc->sc_slots[slot];
3418 3487
3419 xhci_setup_ctx(dev->ud_pipe0); 3488 xhci_setup_ctx(dev->ud_pipe0);
3420 3489
3421 HEXDUMP("input context", xhci_slot_get_icv(sc, xs, 0), 3490 HEXDUMP("input context", xhci_slot_get_icv(sc, xs, 0),
3422 sc->sc_ctxsz * 3); 3491 sc->sc_ctxsz * 3);
3423 3492
3424 xhci_set_dcba(sc, DMAADDR(&xs->xs_dc_dma, 0), slot); 3493 xhci_set_dcba(sc, DMAADDR(&xs->xs_dc_dma, 0), slot);
3425 3494
3426 err = xhci_address_device(sc, xhci_slot_get_icp(sc, xs, 0), slot, bsr); 3495 err = xhci_address_device(sc, xhci_slot_get_icp(sc, xs, 0), slot, bsr);
3427 3496
3428 usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD); 3497 usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD);
3429 HEXDUMP("output context", xhci_slot_get_dcv(sc, xs, 0), 3498 HEXDUMP("output context", xhci_slot_get_dcv(sc, xs, 0),
3430 sc->sc_ctxsz * 2); 3499 sc->sc_ctxsz * 2);
3431 3500
3432 return err; 3501 return err;
3433} 3502}
3434 3503
3435/* 3504/*
3436 * 4.8.2, 6.2.3.2 3505 * 4.8.2, 6.2.3.2
3437 * construct slot/endpoint context parameters and do syncmem 3506 * construct slot/endpoint context parameters and do syncmem
3438 */ 3507 */
3439static void 3508static void
3440xhci_setup_ctx(struct usbd_pipe *pipe) 3509xhci_setup_ctx(struct usbd_pipe *pipe)
3441{ 3510{
3442 struct xhci_softc * const sc = XHCI_PIPE2SC(pipe); 3511 struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
3443 struct usbd_device *dev = pipe->up_dev; 3512 struct usbd_device *dev = pipe->up_dev;
3444 struct xhci_slot * const xs = dev->ud_hcpriv; 3513 struct xhci_slot * const xs = dev->ud_hcpriv;
3445 usb_endpoint_descriptor_t * const ed = pipe->up_endpoint->ue_edesc; 3514 usb_endpoint_descriptor_t * const ed = pipe->up_endpoint->ue_edesc;
3446 const u_int dci = xhci_ep_get_dci(ed); 3515 const u_int dci = xhci_ep_get_dci(ed);
3447 const uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes); 3516 const uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
3448 uint32_t *cp; 3517 uint32_t *cp;
3449 uint16_t mps = UGETW(ed->wMaxPacketSize); 3518 uint16_t mps = UGETW(ed->wMaxPacketSize);
3450 uint8_t speed = dev->ud_speed; 3519 uint8_t speed = dev->ud_speed;
3451 uint8_t ival = ed->bInterval; 3520 uint8_t ival = ed->bInterval;
3452 3521
3453 XHCIHIST_FUNC(); 3522 XHCIHIST_FUNC();
3454 XHCIHIST_CALLARGS("pipe %#jx: slot %ju dci %ju speed %ju", 3523 XHCIHIST_CALLARGS("pipe %#jx: slot %ju dci %ju speed %ju",
3455 (uintptr_t)pipe, xs->xs_idx, dci, speed); 3524 (uintptr_t)pipe, xs->xs_idx, dci, speed);
3456 3525
3457 /* set up initial input control context */ 3526 /* set up initial input control context */
3458 cp = xhci_slot_get_icv(sc, xs, XHCI_ICI_INPUT_CONTROL); 3527 cp = xhci_slot_get_icv(sc, xs, XHCI_ICI_INPUT_CONTROL);
3459 cp[0] = htole32(0); 3528 cp[0] = htole32(0);
3460 cp[1] = htole32(XHCI_INCTX_1_ADD_MASK(dci)); 3529 cp[1] = htole32(XHCI_INCTX_1_ADD_MASK(dci));
3461 cp[1] |= htole32(XHCI_INCTX_1_ADD_MASK(XHCI_DCI_SLOT)); 3530 cp[1] |= htole32(XHCI_INCTX_1_ADD_MASK(XHCI_DCI_SLOT));
3462 cp[7] = htole32(0); 3531 cp[7] = htole32(0);
3463 3532
3464 /* set up input slot context */ 3533 /* set up input slot context */
3465 cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_SLOT)); 3534 cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_SLOT));
3466 cp[0] = 3535 cp[0] =
3467 XHCI_SCTX_0_CTX_NUM_SET(dci) | 3536 XHCI_SCTX_0_CTX_NUM_SET(dci) |
3468 XHCI_SCTX_0_SPEED_SET(xhci_speed2xspeed(speed)); 3537 XHCI_SCTX_0_SPEED_SET(xhci_speed2xspeed(speed));
3469 cp[1] = 0; 3538 cp[1] = 0;
3470 cp[2] = XHCI_SCTX_2_IRQ_TARGET_SET(0); 3539 cp[2] = XHCI_SCTX_2_IRQ_TARGET_SET(0);
3471 cp[3] = 0; 3540 cp[3] = 0;
3472 xhci_setup_route(pipe, cp); 3541 xhci_setup_route(pipe, cp);
3473 xhci_setup_tthub(pipe, cp); 3542 xhci_setup_tthub(pipe, cp);
3474 3543
3475 cp[0] = htole32(cp[0]); 3544 cp[0] = htole32(cp[0]);
3476 cp[1] = htole32(cp[1]); 3545 cp[1] = htole32(cp[1]);
3477 cp[2] = htole32(cp[2]); 3546 cp[2] = htole32(cp[2]);
3478 cp[3] = htole32(cp[3]); 3547 cp[3] = htole32(cp[3]);
3479 3548
3480 /* set up input endpoint context */ 3549 /* set up input endpoint context */
3481 cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(dci)); 3550 cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(dci));
3482 cp[0] = 3551 cp[0] =
3483 XHCI_EPCTX_0_EPSTATE_SET(0) | 3552 XHCI_EPCTX_0_EPSTATE_SET(0) |
3484 XHCI_EPCTX_0_MULT_SET(0) | 3553 XHCI_EPCTX_0_MULT_SET(0) |
3485 XHCI_EPCTX_0_MAXP_STREAMS_SET(0) | 3554 XHCI_EPCTX_0_MAXP_STREAMS_SET(0) |
3486 XHCI_EPCTX_0_LSA_SET(0) | 3555 XHCI_EPCTX_0_LSA_SET(0) |
3487 XHCI_EPCTX_0_MAX_ESIT_PAYLOAD_HI_SET(0); 3556 XHCI_EPCTX_0_MAX_ESIT_PAYLOAD_HI_SET(0);
3488 cp[1] = 3557 cp[1] =
3489 XHCI_EPCTX_1_EPTYPE_SET(xhci_ep_get_type(ed)) | 3558 XHCI_EPCTX_1_EPTYPE_SET(xhci_ep_get_type(ed)) |
3490 XHCI_EPCTX_1_HID_SET(0) | 3559 XHCI_EPCTX_1_HID_SET(0) |
3491 XHCI_EPCTX_1_MAXB_SET(0); 3560 XHCI_EPCTX_1_MAXB_SET(0);
3492 3561
3493 if (xfertype != UE_ISOCHRONOUS) 3562 if (xfertype != UE_ISOCHRONOUS)
3494 cp[1] |= XHCI_EPCTX_1_CERR_SET(3); 3563 cp[1] |= XHCI_EPCTX_1_CERR_SET(3);
3495 3564
3496 if (xfertype == UE_CONTROL) 3565 if (xfertype == UE_CONTROL)
3497 cp[4] = XHCI_EPCTX_4_AVG_TRB_LEN_SET(8); /* 6.2.3 */ 3566 cp[4] = XHCI_EPCTX_4_AVG_TRB_LEN_SET(8); /* 6.2.3 */
3498 else if (USB_IS_SS(speed)) 3567 else if (USB_IS_SS(speed))
3499 cp[4] = XHCI_EPCTX_4_AVG_TRB_LEN_SET(mps); 3568 cp[4] = XHCI_EPCTX_4_AVG_TRB_LEN_SET(mps);
3500 else 3569 else
3501 cp[4] = XHCI_EPCTX_4_AVG_TRB_LEN_SET(UE_GET_SIZE(mps)); 3570 cp[4] = XHCI_EPCTX_4_AVG_TRB_LEN_SET(UE_GET_SIZE(mps));
3502 3571
3503 xhci_setup_maxburst(pipe, cp); 3572 xhci_setup_maxburst(pipe, cp);
3504 3573
3505 switch (xfertype) { 3574 switch (xfertype) {
3506 case UE_CONTROL: 3575 case UE_CONTROL:
3507 break; 3576 break;
3508 case UE_BULK: 3577 case UE_BULK:
3509 /* XXX Set MaxPStreams, HID, and LSA if streams enabled */ 3578 /* XXX Set MaxPStreams, HID, and LSA if streams enabled */
3510 break; 3579 break;
3511 case UE_INTERRUPT: 3580 case UE_INTERRUPT:
3512 if (pipe->up_interval != USBD_DEFAULT_INTERVAL) 3581 if (pipe->up_interval != USBD_DEFAULT_INTERVAL)
3513 ival = pipe->up_interval; 3582 ival = pipe->up_interval;
3514 3583
3515 ival = xhci_bival2ival(ival, speed); 3584 ival = xhci_bival2ival(ival, speed);
3516 cp[0] |= XHCI_EPCTX_0_IVAL_SET(ival); 3585 cp[0] |= XHCI_EPCTX_0_IVAL_SET(ival);
3517 break; 3586 break;
3518 case UE_ISOCHRONOUS: 3587 case UE_ISOCHRONOUS:
3519 if (pipe->up_interval != USBD_DEFAULT_INTERVAL) 3588 if (pipe->up_interval != USBD_DEFAULT_INTERVAL)
3520 ival = pipe->up_interval; 3589 ival = pipe->up_interval;
3521 3590
3522 /* xHCI 6.2.3.6 Table 65, USB 2.0 9.6.6 */ 3591 /* xHCI 6.2.3.6 Table 65, USB 2.0 9.6.6 */
3523 if (speed == USB_SPEED_FULL) 3592 if (speed == USB_SPEED_FULL)
3524 ival += 3; /* 1ms -> 125us */ 3593 ival += 3; /* 1ms -> 125us */
3525 ival--; 3594 ival--;
3526 cp[0] |= XHCI_EPCTX_0_IVAL_SET(ival); 3595 cp[0] |= XHCI_EPCTX_0_IVAL_SET(ival);
3527 break; 3596 break;
3528 default: 3597 default:
3529 break; 3598 break;
3530 } 3599 }
3531 DPRINTFN(4, "setting ival %ju MaxBurst %#jx", 3600 DPRINTFN(4, "setting ival %ju MaxBurst %#jx",
3532 XHCI_EPCTX_0_IVAL_GET(cp[0]), XHCI_EPCTX_1_MAXB_GET(cp[1]), 0, 0); 3601 XHCI_EPCTX_0_IVAL_GET(cp[0]), XHCI_EPCTX_1_MAXB_GET(cp[1]), 0, 0);
3533 3602
3534 /* rewind TR dequeue pointer in xHC */ 3603 /* rewind TR dequeue pointer in xHC */
3535 /* can't use xhci_ep_get_dci() yet? */ 3604 /* can't use xhci_ep_get_dci() yet? */
3536 *(uint64_t *)(&cp[2]) = htole64( 3605 *(uint64_t *)(&cp[2]) = htole64(
3537 xhci_ring_trbp(xs->xs_xr[dci], 0) | 3606 xhci_ring_trbp(xs->xs_xr[dci], 0) |
3538 XHCI_EPCTX_2_DCS_SET(1)); 3607 XHCI_EPCTX_2_DCS_SET(1));
3539 3608
3540 cp[0] = htole32(cp[0]); 3609 cp[0] = htole32(cp[0]);
3541 cp[1] = htole32(cp[1]); 3610 cp[1] = htole32(cp[1]);
3542 cp[4] = htole32(cp[4]); 3611 cp[4] = htole32(cp[4]);
3543 3612
3544 /* rewind TR dequeue pointer in driver */ 3613 /* rewind TR dequeue pointer in driver */
3545 struct xhci_ring *xr = xs->xs_xr[dci]; 3614 struct xhci_ring *xr = xs->xs_xr[dci];
3546 mutex_enter(&xr->xr_lock); 3615 mutex_enter(&xr->xr_lock);
3547 xhci_host_dequeue(xr); 3616 xhci_host_dequeue(xr);
3548 mutex_exit(&xr->xr_lock); 3617 mutex_exit(&xr->xr_lock);
3549 3618
3550 /* sync input contexts before they are read from memory */ 3619 /* sync input contexts before they are read from memory */
3551 usb_syncmem(&xs->xs_ic_dma, 0, sc->sc_pgsz, BUS_DMASYNC_PREWRITE); 3620 usb_syncmem(&xs->xs_ic_dma, 0, sc->sc_pgsz, BUS_DMASYNC_PREWRITE);
3552} 3621}
3553 3622
3554/* 3623/*
3555 * Setup route string and roothub port of given device for slot context 3624 * Setup route string and roothub port of given device for slot context
3556 */ 3625 */
3557static void 3626static void
3558xhci_setup_route(struct usbd_pipe *pipe, uint32_t *cp) 3627xhci_setup_route(struct usbd_pipe *pipe, uint32_t *cp)
3559{ 3628{
3560 struct xhci_softc * const sc = XHCI_PIPE2SC(pipe); 3629 struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
3561 struct usbd_device *dev = pipe->up_dev; 3630 struct usbd_device *dev = pipe->up_dev;
3562 struct usbd_port *up = dev->ud_powersrc; 3631 struct usbd_port *up = dev->ud_powersrc;
3563 struct usbd_device *hub; 3632 struct usbd_device *hub;
3564 struct usbd_device *adev; 3633 struct usbd_device *adev;
3565 uint8_t rhport = 0; 3634 uint8_t rhport = 0;
3566 uint32_t route = 0; 3635 uint32_t route = 0;
3567 3636
3568 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 3637 XHCIHIST_FUNC(); XHCIHIST_CALLED();
3569 3638
3570 /* Locate root hub port and Determine route string */ 3639 /* Locate root hub port and Determine route string */
3571 /* 4.3.3 route string does not include roothub port */ 3640 /* 4.3.3 route string does not include roothub port */
3572 for (hub = dev; hub != NULL; hub = hub->ud_myhub) { 3641 for (hub = dev; hub != NULL; hub = hub->ud_myhub) {
3573 uint32_t dep; 3642 uint32_t dep;
3574 3643
3575 DPRINTFN(4, "hub %#jx depth %jd upport %#jx upportno %jd", 3644 DPRINTFN(4, "hub %#jx depth %jd upport %#jx upportno %jd",
3576 (uintptr_t)hub, hub->ud_depth, (uintptr_t)hub->ud_powersrc, 3645 (uintptr_t)hub, hub->ud_depth, (uintptr_t)hub->ud_powersrc,
3577 hub->ud_powersrc ? (uintptr_t)hub->ud_powersrc->up_portno : 3646 hub->ud_powersrc ? (uintptr_t)hub->ud_powersrc->up_portno :
3578 -1); 3647 -1);
3579 3648
3580 if (hub->ud_powersrc == NULL) 3649 if (hub->ud_powersrc == NULL)
3581 break; 3650 break;
3582 dep = hub->ud_depth; 3651 dep = hub->ud_depth;
3583 if (dep == 0) 3652 if (dep == 0)
3584 break; 3653 break;
3585 rhport = hub->ud_powersrc->up_portno; 3654 rhport = hub->ud_powersrc->up_portno;
3586 if (dep > USB_HUB_MAX_DEPTH) 3655 if (dep > USB_HUB_MAX_DEPTH)
3587 continue; 3656 continue;
3588 3657
3589 route |= 3658 route |=
3590 (rhport > UHD_SS_NPORTS_MAX ? UHD_SS_NPORTS_MAX : rhport) 3659 (rhport > UHD_SS_NPORTS_MAX ? UHD_SS_NPORTS_MAX : rhport)
3591 << ((dep - 1) * 4); 3660 << ((dep - 1) * 4);
3592 } 3661 }
3593 route = route >> 4; 3662 route = route >> 4;
3594 size_t bn = hub == sc->sc_bus.ub_roothub ? 0 : 1; 3663 size_t bn = hub == sc->sc_bus.ub_roothub ? 0 : 1;
3595 3664
3596 /* Locate port on upstream high speed hub */ 3665 /* Locate port on upstream high speed hub */
3597 for (adev = dev, hub = up->up_parent; 3666 for (adev = dev, hub = up->up_parent;
3598 hub != NULL && hub->ud_speed != USB_SPEED_HIGH; 3667 hub != NULL && hub->ud_speed != USB_SPEED_HIGH;
3599 adev = hub, hub = hub->ud_myhub) 3668 adev = hub, hub = hub->ud_myhub)
3600 ; 3669 ;
3601 if (hub) { 3670 if (hub) {
3602 int p; 3671 int p;
3603 for (p = 1; p <= hub->ud_hub->uh_hubdesc.bNbrPorts; p++) { 3672 for (p = 1; p <= hub->ud_hub->uh_hubdesc.bNbrPorts; p++) {
3604 if (hub->ud_hub->uh_ports[p - 1].up_dev == adev) { 3673 if (hub->ud_hub->uh_ports[p - 1].up_dev == adev) {
3605 dev->ud_myhsport = &hub->ud_hub->uh_ports[p - 1]; 3674 dev->ud_myhsport = &hub->ud_hub->uh_ports[p - 1];
3606 goto found; 3675 goto found;
3607 } 3676 }
3608 } 3677 }
3609 panic("%s: cannot find HS port", __func__); 3678 panic("%s: cannot find HS port", __func__);
3610 found: 3679 found:
3611 DPRINTFN(4, "high speed port %jd", p, 0, 0, 0); 3680 DPRINTFN(4, "high speed port %jd", p, 0, 0, 0);
3612 } else { 3681 } else {
3613 dev->ud_myhsport = NULL; 3682 dev->ud_myhsport = NULL;
3614 } 3683 }
3615 3684
3616 const size_t ctlrport = xhci_rhport2ctlrport(sc, bn, rhport); 3685 const size_t ctlrport = xhci_rhport2ctlrport(sc, bn, rhport);
3617 3686
3618 DPRINTFN(4, "rhport %ju ctlrport %ju Route %05jx hub %#jx", rhport, 3687 DPRINTFN(4, "rhport %ju ctlrport %ju Route %05jx hub %#jx", rhport,
3619 ctlrport, route, (uintptr_t)hub); 3688 ctlrport, route, (uintptr_t)hub);
3620 3689
3621 cp[0] |= XHCI_SCTX_0_ROUTE_SET(route); 3690 cp[0] |= XHCI_SCTX_0_ROUTE_SET(route);
3622 cp[1] |= XHCI_SCTX_1_RH_PORT_SET(ctlrport); 3691 cp[1] |= XHCI_SCTX_1_RH_PORT_SET(ctlrport);
3623} 3692}
3624 3693
3625/* 3694/*
3626 * Setup whether device is hub, whether device uses MTT, and 3695 * Setup whether device is hub, whether device uses MTT, and
3627 * TT informations if it uses MTT. 3696 * TT informations if it uses MTT.
3628 */ 3697 */
3629static void 3698static void
3630xhci_setup_tthub(struct usbd_pipe *pipe, uint32_t *cp) 3699xhci_setup_tthub(struct usbd_pipe *pipe, uint32_t *cp)
3631{ 3700{
3632 struct usbd_device *dev = pipe->up_dev; 3701 struct usbd_device *dev = pipe->up_dev;
3633 struct usbd_port *myhsport = dev->ud_myhsport; 3702 struct usbd_port *myhsport = dev->ud_myhsport;
3634 usb_device_descriptor_t * const dd = &dev->ud_ddesc; 3703 usb_device_descriptor_t * const dd = &dev->ud_ddesc;
3635 uint32_t speed = dev->ud_speed; 3704 uint32_t speed = dev->ud_speed;
3636 uint8_t rhaddr = dev->ud_bus->ub_rhaddr; 3705 uint8_t rhaddr = dev->ud_bus->ub_rhaddr;
3637 uint8_t tthubslot, ttportnum; 3706 uint8_t tthubslot, ttportnum;
3638 bool ishub; 3707 bool ishub;
3639 bool usemtt; 3708 bool usemtt;
3640 3709
3641 XHCIHIST_FUNC(); 3710 XHCIHIST_FUNC();
3642 3711
3643 /* 3712 /*
3644 * 6.2.2, Table 57-60, 6.2.2.1, 6.2.2.2 3713 * 6.2.2, Table 57-60, 6.2.2.1, 6.2.2.2
3645 * tthubslot: 3714 * tthubslot:
3646 * This is the slot ID of parent HS hub 3715 * This is the slot ID of parent HS hub
3647 * if LS/FS device is connected && connected through HS hub. 3716 * if LS/FS device is connected && connected through HS hub.
3648 * This is 0 if device is not LS/FS device || 3717 * This is 0 if device is not LS/FS device ||
3649 * parent hub is not HS hub || 3718 * parent hub is not HS hub ||
3650 * attached to root hub. 3719 * attached to root hub.
3651 * ttportnum: 3720 * ttportnum:
3652 * This is the downstream facing port of parent HS hub 3721 * This is the downstream facing port of parent HS hub
3653 * if LS/FS device is connected. 3722 * if LS/FS device is connected.
3654 * This is 0 if device is not LS/FS device || 3723 * This is 0 if device is not LS/FS device ||
3655 * parent hub is not HS hub || 3724 * parent hub is not HS hub ||
3656 * attached to root hub. 3725 * attached to root hub.
3657 */ 3726 */
3658 if (myhsport && 3727 if (myhsport &&
3659 myhsport->up_parent->ud_addr != rhaddr && 3728 myhsport->up_parent->ud_addr != rhaddr &&
3660 (speed == USB_SPEED_LOW || speed == USB_SPEED_FULL)) { 3729 (speed == USB_SPEED_LOW || speed == USB_SPEED_FULL)) {
3661 ttportnum = myhsport->up_portno; 3730 ttportnum = myhsport->up_portno;
3662 tthubslot = myhsport->up_parent->ud_addr; 3731 tthubslot = myhsport->up_parent->ud_addr;
3663 } else { 3732 } else {
3664 ttportnum = 0; 3733 ttportnum = 0;
3665 tthubslot = 0; 3734 tthubslot = 0;
3666 } 3735 }
3667 XHCIHIST_CALLARGS("myhsport %#jx ttportnum=%jd tthubslot=%jd", 3736 XHCIHIST_CALLARGS("myhsport %#jx ttportnum=%jd tthubslot=%jd",
3668 (uintptr_t)myhsport, ttportnum, tthubslot, 0); 3737 (uintptr_t)myhsport, ttportnum, tthubslot, 0);
3669 3738
3670 /* ishub is valid after reading UDESC_DEVICE */ 3739 /* ishub is valid after reading UDESC_DEVICE */
3671 ishub = (dd->bDeviceClass == UDCLASS_HUB); 3740 ishub = (dd->bDeviceClass == UDCLASS_HUB);
3672 3741
3673 /* dev->ud_hub is valid after reading UDESC_HUB */ 3742 /* dev->ud_hub is valid after reading UDESC_HUB */
3674 if (ishub && dev->ud_hub) { 3743 if (ishub && dev->ud_hub) {
3675 usb_hub_descriptor_t *hd = &dev->ud_hub->uh_hubdesc; 3744 usb_hub_descriptor_t *hd = &dev->ud_hub->uh_hubdesc;
3676 uint8_t ttt = 3745 uint8_t ttt =
3677 __SHIFTOUT(UGETW(hd->wHubCharacteristics), UHD_TT_THINK); 3746 __SHIFTOUT(UGETW(hd->wHubCharacteristics), UHD_TT_THINK);
3678 3747
3679 cp[1] |= XHCI_SCTX_1_NUM_PORTS_SET(hd->bNbrPorts); 3748 cp[1] |= XHCI_SCTX_1_NUM_PORTS_SET(hd->bNbrPorts);
3680 cp[2] |= XHCI_SCTX_2_TT_THINK_TIME_SET(ttt); 3749 cp[2] |= XHCI_SCTX_2_TT_THINK_TIME_SET(ttt);
3681 DPRINTFN(4, "nports=%jd ttt=%jd", hd->bNbrPorts, ttt, 0, 0); 3750 DPRINTFN(4, "nports=%jd ttt=%jd", hd->bNbrPorts, ttt, 0, 0);
3682 } 3751 }
3683 3752
3684#define IS_MTTHUB(dd) \ 3753#define IS_MTTHUB(dd) \
3685 ((dd)->bDeviceProtocol == UDPROTO_HSHUBMTT) 3754 ((dd)->bDeviceProtocol == UDPROTO_HSHUBMTT)
3686 3755
3687 /* 3756 /*
3688 * MTT flag is set if 3757 * MTT flag is set if
3689 * 1. this is HS hub && MTTs are supported and enabled; or 3758 * 1. this is HS hub && MTTs are supported and enabled; or
3690 * 2. this is LS or FS device && there is a parent HS hub where MTTs 3759 * 2. this is LS or FS device && there is a parent HS hub where MTTs
3691 * are supported and enabled. 3760 * are supported and enabled.
3692 * 3761 *
3693 * XXX enabled is not tested yet 3762 * XXX enabled is not tested yet
3694 */ 3763 */
3695 if (ishub && speed == USB_SPEED_HIGH && IS_MTTHUB(dd)) 3764 if (ishub && speed == USB_SPEED_HIGH && IS_MTTHUB(dd))
3696 usemtt = true; 3765 usemtt = true;
3697 else if ((speed == USB_SPEED_LOW || speed == USB_SPEED_FULL) && 3766 else if ((speed == USB_SPEED_LOW || speed == USB_SPEED_FULL) &&
3698 myhsport && 3767 myhsport &&
3699 myhsport->up_parent->ud_addr != rhaddr && 3768 myhsport->up_parent->ud_addr != rhaddr &&
3700 IS_MTTHUB(&myhsport->up_parent->ud_ddesc)) 3769 IS_MTTHUB(&myhsport->up_parent->ud_ddesc))
3701 usemtt = true; 3770 usemtt = true;
3702 else 3771 else
3703 usemtt = false; 3772 usemtt = false;
3704 DPRINTFN(4, "class %ju proto %ju ishub %jd usemtt %jd", 3773 DPRINTFN(4, "class %ju proto %ju ishub %jd usemtt %jd",
3705 dd->bDeviceClass, dd->bDeviceProtocol, ishub, usemtt); 3774 dd->bDeviceClass, dd->bDeviceProtocol, ishub, usemtt);
3706 3775
3707#undef IS_MTTHUB 3776#undef IS_MTTHUB
3708 3777
3709 cp[0] |= 3778 cp[0] |=
3710 XHCI_SCTX_0_HUB_SET(ishub ? 1 : 0) | 3779 XHCI_SCTX_0_HUB_SET(ishub ? 1 : 0) |
3711 XHCI_SCTX_0_MTT_SET(usemtt ? 1 : 0); 3780 XHCI_SCTX_0_MTT_SET(usemtt ? 1 : 0);
3712 cp[2] |= 3781 cp[2] |=
3713 XHCI_SCTX_2_TT_HUB_SID_SET(tthubslot) | 3782 XHCI_SCTX_2_TT_HUB_SID_SET(tthubslot) |
3714 XHCI_SCTX_2_TT_PORT_NUM_SET(ttportnum); 3783 XHCI_SCTX_2_TT_PORT_NUM_SET(ttportnum);
3715} 3784}
3716 3785
3717/* set up params for periodic endpoint */ 3786/* set up params for periodic endpoint */
3718static void 3787static void
3719xhci_setup_maxburst(struct usbd_pipe *pipe, uint32_t *cp) 3788xhci_setup_maxburst(struct usbd_pipe *pipe, uint32_t *cp)
3720{ 3789{
3721 struct xhci_pipe * const xpipe = (struct xhci_pipe *)pipe; 3790 struct xhci_pipe * const xpipe = (struct xhci_pipe *)pipe;
3722 struct usbd_device *dev = pipe->up_dev; 3791 struct usbd_device *dev = pipe->up_dev;
3723 usb_endpoint_descriptor_t * const ed = pipe->up_endpoint->ue_edesc; 3792 usb_endpoint_descriptor_t * const ed = pipe->up_endpoint->ue_edesc;
3724 const uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes); 3793 const uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
3725 usbd_desc_iter_t iter; 3794 usbd_desc_iter_t iter;
3726 const usb_cdc_descriptor_t *cdcd; 3795 const usb_cdc_descriptor_t *cdcd;
3727 uint32_t maxb = 0; 3796 uint32_t maxb = 0;
3728 uint16_t mps = UGETW(ed->wMaxPacketSize); 3797 uint16_t mps = UGETW(ed->wMaxPacketSize);
3729 uint8_t speed = dev->ud_speed; 3798 uint8_t speed = dev->ud_speed;
3730 uint8_t mult = 0; 3799 uint8_t mult = 0;
3731 uint8_t ep; 3800 uint8_t ep;
3732 3801
3733 /* config desc is NULL when opening ep0 */ 3802 /* config desc is NULL when opening ep0 */
3734 if (dev == NULL || dev->ud_cdesc == NULL) 3803 if (dev == NULL || dev->ud_cdesc == NULL)
3735 goto no_cdcd; 3804 goto no_cdcd;
3736 cdcd = (const usb_cdc_descriptor_t *)usb_find_desc(dev, 3805 cdcd = (const usb_cdc_descriptor_t *)usb_find_desc(dev,
3737 UDESC_INTERFACE, USBD_CDCSUBTYPE_ANY); 3806 UDESC_INTERFACE, USBD_CDCSUBTYPE_ANY);
3738 if (cdcd == NULL) 3807 if (cdcd == NULL)
3739 goto no_cdcd; 3808 goto no_cdcd;
3740 usb_desc_iter_init(dev, &iter); 3809 usb_desc_iter_init(dev, &iter);
3741 iter.cur = (const void *)cdcd; 3810 iter.cur = (const void *)cdcd;
3742 3811
3743 /* find endpoint_ss_comp desc for ep of this pipe */ 3812 /* find endpoint_ss_comp desc for ep of this pipe */
3744 for (ep = 0;;) { 3813 for (ep = 0;;) {
3745 cdcd = (const usb_cdc_descriptor_t *)usb_desc_iter_next(&iter); 3814 cdcd = (const usb_cdc_descriptor_t *)usb_desc_iter_next(&iter);
3746 if (cdcd == NULL) 3815 if (cdcd == NULL)
3747 break; 3816 break;
3748 if (ep == 0 && cdcd->bDescriptorType == UDESC_ENDPOINT) { 3817 if (ep == 0 && cdcd->bDescriptorType == UDESC_ENDPOINT) {
3749 ep = ((const usb_endpoint_descriptor_t *)cdcd)-> 3818 ep = ((const usb_endpoint_descriptor_t *)cdcd)->
3750 bEndpointAddress; 3819 bEndpointAddress;
3751 if (UE_GET_ADDR(ep) == 3820 if (UE_GET_ADDR(ep) ==
3752 UE_GET_ADDR(ed->bEndpointAddress)) { 3821 UE_GET_ADDR(ed->bEndpointAddress)) {
3753 cdcd = (const usb_cdc_descriptor_t *) 3822 cdcd = (const usb_cdc_descriptor_t *)
3754 usb_desc_iter_next(&iter); 3823 usb_desc_iter_next(&iter);
3755 break; 3824 break;
3756 } 3825 }
3757 ep = 0; 3826 ep = 0;
3758 } 3827 }
3759 } 3828 }
3760 if (cdcd != NULL && cdcd->bDescriptorType == UDESC_ENDPOINT_SS_COMP) { 3829 if (cdcd != NULL && cdcd->bDescriptorType == UDESC_ENDPOINT_SS_COMP) {
3761 const usb_endpoint_ss_comp_descriptor_t * esscd = 3830 const usb_endpoint_ss_comp_descriptor_t * esscd =
3762 (const usb_endpoint_ss_comp_descriptor_t *)cdcd; 3831 (const usb_endpoint_ss_comp_descriptor_t *)cdcd;
3763 maxb = esscd->bMaxBurst; 3832 maxb = esscd->bMaxBurst;
3764 mult = UE_GET_SS_ISO_MULT(esscd->bmAttributes); 3833 mult = UE_GET_SS_ISO_MULT(esscd->bmAttributes);
3765 } 3834 }
3766 3835
3767 no_cdcd: 3836 no_cdcd:
3768 /* 6.2.3.4, 4.8.2.4 */ 3837 /* 6.2.3.4, 4.8.2.4 */
3769 if (USB_IS_SS(speed)) { 3838 if (USB_IS_SS(speed)) {
3770 /* USB 3.1 9.6.6 */ 3839 /* USB 3.1 9.6.6 */
3771 cp[1] |= XHCI_EPCTX_1_MAXP_SIZE_SET(mps); 3840 cp[1] |= XHCI_EPCTX_1_MAXP_SIZE_SET(mps);
3772 /* USB 3.1 9.6.7 */ 3841 /* USB 3.1 9.6.7 */
3773 cp[1] |= XHCI_EPCTX_1_MAXB_SET(maxb); 3842 cp[1] |= XHCI_EPCTX_1_MAXB_SET(maxb);
3774#ifdef notyet 3843#ifdef notyet
3775 if (xfertype == UE_ISOCHRONOUS) { 3844 if (xfertype == UE_ISOCHRONOUS) {
3776 } 3845 }
3777 if (XHCI_HCC2_LEC(sc->sc_hcc2) != 0) { 3846 if (XHCI_HCC2_LEC(sc->sc_hcc2) != 0) {
3778 /* use ESIT */ 3847 /* use ESIT */
3779 cp[4] |= XHCI_EPCTX_4_MAX_ESIT_PAYLOAD_SET(x); 3848 cp[4] |= XHCI_EPCTX_4_MAX_ESIT_PAYLOAD_SET(x);
3780 cp[0] |= XHCI_EPCTX_0_MAX_ESIT_PAYLOAD_HI_SET(x); 3849 cp[0] |= XHCI_EPCTX_0_MAX_ESIT_PAYLOAD_HI_SET(x);
3781 3850
3782 /* XXX if LEC = 1, set ESIT instead */ 3851 /* XXX if LEC = 1, set ESIT instead */
3783 cp[0] |= XHCI_EPCTX_0_MULT_SET(0); 3852 cp[0] |= XHCI_EPCTX_0_MULT_SET(0);
3784 } else { 3853 } else {
3785 /* use ival */ 3854 /* use ival */
3786 } 3855 }
3787#endif 3856#endif
3788 } else { 3857 } else {
3789 /* USB 2.0 9.6.6 */ 3858 /* USB 2.0 9.6.6 */
3790 cp[1] |= XHCI_EPCTX_1_MAXP_SIZE_SET(UE_GET_SIZE(mps)); 3859 cp[1] |= XHCI_EPCTX_1_MAXP_SIZE_SET(UE_GET_SIZE(mps));
3791 3860
3792 /* 6.2.3.4 */ 3861 /* 6.2.3.4 */
3793 if (speed == USB_SPEED_HIGH && 3862 if (speed == USB_SPEED_HIGH &&
3794 (xfertype == UE_ISOCHRONOUS || xfertype == UE_INTERRUPT)) { 3863 (xfertype == UE_ISOCHRONOUS || xfertype == UE_INTERRUPT)) {
3795 maxb = UE_GET_TRANS(mps); 3864 maxb = UE_GET_TRANS(mps);
3796 } else { 3865 } else {
3797 /* LS/FS or HS CTRL or HS BULK */ 3866 /* LS/FS or HS CTRL or HS BULK */
3798 maxb = 0; 3867 maxb = 0;
3799 } 3868 }
3800 cp[1] |= XHCI_EPCTX_1_MAXB_SET(maxb); 3869 cp[1] |= XHCI_EPCTX_1_MAXB_SET(maxb);
3801 } 3870 }
3802 xpipe->xp_maxb = maxb + 1; 3871 xpipe->xp_maxb = maxb + 1;
3803 xpipe->xp_mult = mult + 1; 3872 xpipe->xp_mult = mult + 1;
3804} 3873}
3805 3874
3806/* 3875/*
3807 * Convert endpoint bInterval value to endpoint context interval value 3876 * Convert endpoint bInterval value to endpoint context interval value
3808 * for Interrupt pipe. 3877 * for Interrupt pipe.
3809 * xHCI 6.2.3.6 Table 65, USB 2.0 9.6.6 3878 * xHCI 6.2.3.6 Table 65, USB 2.0 9.6.6
3810 */ 3879 */
3811static uint32_t 3880static uint32_t
3812xhci_bival2ival(uint32_t ival, uint32_t speed) 3881xhci_bival2ival(uint32_t ival, uint32_t speed)
3813{ 3882{
3814 if (speed == USB_SPEED_LOW || speed == USB_SPEED_FULL) { 3883 if (speed == USB_SPEED_LOW || speed == USB_SPEED_FULL) {
3815 int i; 3884 int i;
3816 3885
3817 /* 3886 /*
3818 * round ival down to "the nearest base 2 multiple of 3887 * round ival down to "the nearest base 2 multiple of
3819 * bInterval * 8". 3888 * bInterval * 8".
3820 * bInterval is at most 255 as its type is uByte. 3889 * bInterval is at most 255 as its type is uByte.
3821 * 255(ms) = 2040(x 125us) < 2^11, so start with 10. 3890 * 255(ms) = 2040(x 125us) < 2^11, so start with 10.
3822 */ 3891 */
3823 for (i = 10; i > 0; i--) { 3892 for (i = 10; i > 0; i--) {
3824 if ((ival * 8) >= (1 << i)) 3893 if ((ival * 8) >= (1 << i))
3825 break; 3894 break;
3826 } 3895 }
3827 ival = i; 3896 ival = i;
3828 } else { 3897 } else {
3829 /* Interval = bInterval-1 for SS/HS */ 3898 /* Interval = bInterval-1 for SS/HS */
3830 ival--; 3899 ival--;
3831 } 3900 }
3832 3901
3833 return ival; 3902 return ival;
3834} 3903}
3835 3904
3836/* ----- */ 3905/* ----- */
3837 3906
3838static void 3907static void
3839xhci_noop(struct usbd_pipe *pipe) 3908xhci_noop(struct usbd_pipe *pipe)
3840{ 3909{
3841 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 3910 XHCIHIST_FUNC(); XHCIHIST_CALLED();
3842} 3911}
3843 3912
3844/* 3913/*
3845 * Process root hub request. 3914 * Process root hub request.
3846 */ 3915 */
3847static int 3916static int
3848xhci_roothub_ctrl(struct usbd_bus *bus, usb_device_request_t *req, 3917xhci_roothub_ctrl(struct usbd_bus *bus, usb_device_request_t *req,
3849 void *buf, int buflen) 3918 void *buf, int buflen)
3850{ 3919{
3851 struct xhci_softc * const sc = XHCI_BUS2SC(bus); 3920 struct xhci_softc * const sc = XHCI_BUS2SC(bus);
3852 usb_port_status_t ps; 3921 usb_port_status_t ps;
3853 int l, totlen = 0; 3922 int l, totlen = 0;
3854 uint16_t len, value, index; 3923 uint16_t len, value, index;
3855 int port, i; 3924 int port, i;
3856 uint32_t v; 3925 uint32_t v;
3857 3926
3858 XHCIHIST_FUNC(); 3927 XHCIHIST_FUNC();
3859 3928
3860 if (sc->sc_dying) 3929 if (sc->sc_dying)
3861 return -1; 3930 return -1;
3862 3931
3863 size_t bn = bus == &sc->sc_bus ? 0 : 1; 3932 size_t bn = bus == &sc->sc_bus ? 0 : 1;
3864 3933
3865 len = UGETW(req->wLength); 3934 len = UGETW(req->wLength);
3866 value = UGETW(req->wValue); 3935 value = UGETW(req->wValue);
3867 index = UGETW(req->wIndex); 3936 index = UGETW(req->wIndex);
3868 3937
3869 XHCIHIST_CALLARGS("rhreq: %04jx %04jx %04jx %04jx", 3938 XHCIHIST_CALLARGS("rhreq: %04jx %04jx %04jx %04jx",
3870 req->bmRequestType | (req->bRequest << 8), value, index, len); 3939 req->bmRequestType | (req->bRequest << 8), value, index, len);
3871 3940
3872#define C(x,y) ((x) | ((y) << 8)) 3941#define C(x,y) ((x) | ((y) << 8))
3873 switch (C(req->bRequest, req->bmRequestType)) { 3942 switch (C(req->bRequest, req->bmRequestType)) {
3874 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE): 3943 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
3875 DPRINTFN(8, "getdesc: wValue=0x%04jx", value, 0, 0, 0); 3944 DPRINTFN(8, "getdesc: wValue=0x%04jx", value, 0, 0, 0);
3876 if (len == 0) 3945 if (len == 0)
3877 break; 3946 break;
3878 switch (value) { 3947 switch (value) {
3879#define sd ((usb_string_descriptor_t *)buf) 3948#define sd ((usb_string_descriptor_t *)buf)
3880 case C(2, UDESC_STRING): 3949 case C(2, UDESC_STRING):
3881 /* Product */ 3950 /* Product */
3882 totlen = usb_makestrdesc(sd, len, "xHCI root hub"); 3951 totlen = usb_makestrdesc(sd, len, "xHCI root hub");
3883 break; 3952 break;
3884#undef sd 3953#undef sd
3885 default: 3954 default:
3886 /* default from usbroothub */ 3955 /* default from usbroothub */
3887 return buflen; 3956 return buflen;
3888 } 3957 }
3889 break; 3958 break;
3890 3959
3891 /* Hub requests */ 3960 /* Hub requests */
3892 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE): 3961 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
3893 break; 3962 break;
3894 /* Clear Port Feature request */ 3963 /* Clear Port Feature request */
3895 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER): { 3964 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER): {
3896 const size_t cp = xhci_rhport2ctlrport(sc, bn, index); 3965 const size_t cp = xhci_rhport2ctlrport(sc, bn, index);
3897 3966
3898 DPRINTFN(4, "UR_CLEAR_PORT_FEAT bp=%jd feat=%jd bus=%jd cp=%jd", 3967 DPRINTFN(4, "UR_CLEAR_PORT_FEAT bp=%jd feat=%jd bus=%jd cp=%jd",
3899 index, value, bn, cp); 3968 index, value, bn, cp);
3900 if (index < 1 || index > sc->sc_rhportcount[bn]) { 3969 if (index < 1 || index > sc->sc_rhportcount[bn]) {
3901 return -1; 3970 return -1;
3902 } 3971 }
3903 port = XHCI_PORTSC(cp); 3972 port = XHCI_PORTSC(cp);
3904 v = xhci_op_read_4(sc, port); 3973 v = xhci_op_read_4(sc, port);
3905 DPRINTFN(4, "portsc=0x%08jx", v, 0, 0, 0); 3974 DPRINTFN(4, "portsc=0x%08jx", v, 0, 0, 0);
3906 v &= ~XHCI_PS_CLEAR; 3975 v &= ~XHCI_PS_CLEAR;
3907 switch (value) { 3976 switch (value) {
3908 case UHF_PORT_ENABLE: 3977 case UHF_PORT_ENABLE:
3909 xhci_op_write_4(sc, port, v & ~XHCI_PS_PED); 3978 xhci_op_write_4(sc, port, v & ~XHCI_PS_PED);
3910 break; 3979 break;
3911 case UHF_PORT_SUSPEND: 3980 case UHF_PORT_SUSPEND:
3912 return -1; 3981 return -1;
3913 case UHF_PORT_POWER: 3982 case UHF_PORT_POWER:
3914 break; 3983 break;
3915 case UHF_PORT_TEST: 3984 case UHF_PORT_TEST:
3916 case UHF_PORT_INDICATOR: 3985 case UHF_PORT_INDICATOR:
3917 return -1; 3986 return -1;
3918 case UHF_C_PORT_CONNECTION: 3987 case UHF_C_PORT_CONNECTION:
3919 xhci_op_write_4(sc, port, v | XHCI_PS_CSC); 3988 xhci_op_write_4(sc, port, v | XHCI_PS_CSC);
3920 break; 3989 break;
3921 case UHF_C_PORT_ENABLE: 3990 case UHF_C_PORT_ENABLE:
3922 case UHF_C_PORT_SUSPEND: 3991 case UHF_C_PORT_SUSPEND:
3923 case UHF_C_PORT_OVER_CURRENT: 3992 case UHF_C_PORT_OVER_CURRENT:
3924 return -1; 3993 return -1;
3925 case UHF_C_BH_PORT_RESET: 3994 case UHF_C_BH_PORT_RESET:
3926 xhci_op_write_4(sc, port, v | XHCI_PS_WRC); 3995 xhci_op_write_4(sc, port, v | XHCI_PS_WRC);
3927 break; 3996 break;
3928 case UHF_C_PORT_RESET: 3997 case UHF_C_PORT_RESET:
3929 xhci_op_write_4(sc, port, v | XHCI_PS_PRC); 3998 xhci_op_write_4(sc, port, v | XHCI_PS_PRC);
3930 break; 3999 break;
3931 case UHF_C_PORT_LINK_STATE: 4000 case UHF_C_PORT_LINK_STATE:
3932 xhci_op_write_4(sc, port, v | XHCI_PS_PLC); 4001 xhci_op_write_4(sc, port, v | XHCI_PS_PLC);
3933 break; 4002 break;
3934 case UHF_C_PORT_CONFIG_ERROR: 4003 case UHF_C_PORT_CONFIG_ERROR:
3935 xhci_op_write_4(sc, port, v | XHCI_PS_CEC); 4004 xhci_op_write_4(sc, port, v | XHCI_PS_CEC);
3936 break; 4005 break;
3937 default: 4006 default:
3938 return -1; 4007 return -1;
3939 } 4008 }
3940 break; 4009 break;
3941 } 4010 }
3942 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE): 4011 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
3943 if (len == 0) 4012 if (len == 0)
3944 break; 4013 break;
3945 if ((value & 0xff) != 0) { 4014 if ((value & 0xff) != 0) {
3946 return -1; 4015 return -1;
3947 } 4016 }
3948 usb_hub_descriptor_t hubd; 4017 usb_hub_descriptor_t hubd;
3949 4018
3950 totlen = uimin(buflen, sizeof(hubd)); 4019 totlen = uimin(buflen, sizeof(hubd));
3951 memcpy(&hubd, buf, totlen); 4020 memcpy(&hubd, buf, totlen);
3952 hubd.bNbrPorts = sc->sc_rhportcount[bn]; 4021 hubd.bNbrPorts = sc->sc_rhportcount[bn];
3953 USETW(hubd.wHubCharacteristics, UHD_PWR_NO_SWITCH); 4022 USETW(hubd.wHubCharacteristics, UHD_PWR_NO_SWITCH);
3954 hubd.bPwrOn2PwrGood = 200; 4023 hubd.bPwrOn2PwrGood = 200;
3955 for (i = 0, l = sc->sc_rhportcount[bn]; l > 0; i++, l -= 8) { 4024 for (i = 0, l = sc->sc_rhportcount[bn]; l > 0; i++, l -= 8) {
3956 /* XXX can't find out? */ 4025 /* XXX can't find out? */
3957 hubd.DeviceRemovable[i++] = 0; 4026 hubd.DeviceRemovable[i++] = 0;
3958 } 4027 }
3959 hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i; 4028 hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
3960 totlen = uimin(totlen, hubd.bDescLength); 4029 totlen = uimin(totlen, hubd.bDescLength);
3961 memcpy(buf, &hubd, totlen); 4030 memcpy(buf, &hubd, totlen);
3962 break; 4031 break;
3963 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE): 4032 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3964 if (len != 4) { 4033 if (len != 4) {
3965 return -1; 4034 return -1;
3966 } 4035 }
3967 memset(buf, 0, len); /* ? XXX */ 4036 memset(buf, 0, len); /* ? XXX */
3968 totlen = len; 4037 totlen = len;
3969 break; 4038 break;
3970 /* Get Port Status request */ 4039 /* Get Port Status request */
3971 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER): { 4040 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER): {
3972 const size_t cp = xhci_rhport2ctlrport(sc, bn, index); 4041 const size_t cp = xhci_rhport2ctlrport(sc, bn, index);
3973 4042
3974 DPRINTFN(8, "get port status bn=%jd i=%jd cp=%ju", 4043 DPRINTFN(8, "get port status bn=%jd i=%jd cp=%ju",
3975 bn, index, cp, 0); 4044 bn, index, cp, 0);
3976 if (index < 1 || index > sc->sc_rhportcount[bn]) { 4045 if (index < 1 || index > sc->sc_rhportcount[bn]) {
3977 DPRINTFN(5, "bad get port status: index=%jd bn=%jd " 4046 DPRINTFN(5, "bad get port status: index=%jd bn=%jd "
3978 "portcount=%jd", 4047 "portcount=%jd",
3979 index, bn, sc->sc_rhportcount[bn], 0); 4048 index, bn, sc->sc_rhportcount[bn], 0);
3980 return -1; 4049 return -1;
3981 } 4050 }
3982 if (len != 4) { 4051 if (len != 4) {
3983 DPRINTFN(5, "bad get port status: len %jd != 4", 4052 DPRINTFN(5, "bad get port status: len %jd != 4",
3984 len, 0, 0, 0); 4053 len, 0, 0, 0);
3985 return -1; 4054 return -1;
3986 } 4055 }
3987 v = xhci_op_read_4(sc, XHCI_PORTSC(cp)); 4056 v = xhci_op_read_4(sc, XHCI_PORTSC(cp));
3988 DPRINTFN(4, "getrhportsc %jd 0x%08jx", cp, v, 0, 0); 4057 DPRINTFN(4, "getrhportsc %jd 0x%08jx", cp, v, 0, 0);
3989 i = xhci_xspeed2psspeed(XHCI_PS_SPEED_GET(v)); 4058 i = xhci_xspeed2psspeed(XHCI_PS_SPEED_GET(v));
3990 if (v & XHCI_PS_CCS) i |= UPS_CURRENT_CONNECT_STATUS; 4059 if (v & XHCI_PS_CCS) i |= UPS_CURRENT_CONNECT_STATUS;
3991 if (v & XHCI_PS_PED) i |= UPS_PORT_ENABLED; 4060 if (v & XHCI_PS_PED) i |= UPS_PORT_ENABLED;
3992 if (v & XHCI_PS_OCA) i |= UPS_OVERCURRENT_INDICATOR; 4061 if (v & XHCI_PS_OCA) i |= UPS_OVERCURRENT_INDICATOR;
3993 //if (v & XHCI_PS_SUSP) i |= UPS_SUSPEND; 4062 //if (v & XHCI_PS_SUSP) i |= UPS_SUSPEND;
3994 if (v & XHCI_PS_PR) i |= UPS_RESET; 4063 if (v & XHCI_PS_PR) i |= UPS_RESET;
3995 if (v & XHCI_PS_PP) { 4064 if (v & XHCI_PS_PP) {
3996 if (i & UPS_OTHER_SPEED) 4065 if (i & UPS_OTHER_SPEED)
3997 i |= UPS_PORT_POWER_SS; 4066 i |= UPS_PORT_POWER_SS;
3998 else 4067 else
3999 i |= UPS_PORT_POWER; 4068 i |= UPS_PORT_POWER;
4000 } 4069 }
4001 if (i & UPS_OTHER_SPEED) 4070 if (i & UPS_OTHER_SPEED)
4002 i |= UPS_PORT_LS_SET(XHCI_PS_PLS_GET(v)); 4071 i |= UPS_PORT_LS_SET(XHCI_PS_PLS_GET(v));
4003 if (sc->sc_vendor_port_status) 4072 if (sc->sc_vendor_port_status)
4004 i = sc->sc_vendor_port_status(sc, v, i); 4073 i = sc->sc_vendor_port_status(sc, v, i);
4005 USETW(ps.wPortStatus, i); 4074 USETW(ps.wPortStatus, i);
4006 i = 0; 4075 i = 0;
4007 if (v & XHCI_PS_CSC) i |= UPS_C_CONNECT_STATUS; 4076 if (v & XHCI_PS_CSC) i |= UPS_C_CONNECT_STATUS;
4008 if (v & XHCI_PS_PEC) i |= UPS_C_PORT_ENABLED; 4077 if (v & XHCI_PS_PEC) i |= UPS_C_PORT_ENABLED;
4009 if (v & XHCI_PS_OCC) i |= UPS_C_OVERCURRENT_INDICATOR; 4078 if (v & XHCI_PS_OCC) i |= UPS_C_OVERCURRENT_INDICATOR;
4010 if (v & XHCI_PS_PRC) i |= UPS_C_PORT_RESET; 4079 if (v & XHCI_PS_PRC) i |= UPS_C_PORT_RESET;
4011 if (v & XHCI_PS_WRC) i |= UPS_C_BH_PORT_RESET; 4080 if (v & XHCI_PS_WRC) i |= UPS_C_BH_PORT_RESET;
4012 if (v & XHCI_PS_PLC) i |= UPS_C_PORT_LINK_STATE; 4081 if (v & XHCI_PS_PLC) i |= UPS_C_PORT_LINK_STATE;
4013 if (v & XHCI_PS_CEC) i |= UPS_C_PORT_CONFIG_ERROR; 4082 if (v & XHCI_PS_CEC) i |= UPS_C_PORT_CONFIG_ERROR;
4014 USETW(ps.wPortChange, i); 4083 USETW(ps.wPortChange, i);
4015 totlen = uimin(len, sizeof(ps)); 4084 totlen = uimin(len, sizeof(ps));
4016 memcpy(buf, &ps, totlen); 4085 memcpy(buf, &ps, totlen);
4017 DPRINTFN(5, "get port status: wPortStatus %#jx wPortChange %#jx" 4086 DPRINTFN(5, "get port status: wPortStatus %#jx wPortChange %#jx"
4018 " totlen %jd", 4087 " totlen %jd",
4019 UGETW(ps.wPortStatus), UGETW(ps.wPortChange), totlen, 0); 4088 UGETW(ps.wPortStatus), UGETW(ps.wPortChange), totlen, 0);
4020 break; 4089 break;
4021 } 4090 }
4022 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE): 4091 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
4023 return -1; 4092 return -1;
4024 case C(UR_SET_HUB_DEPTH, UT_WRITE_CLASS_DEVICE): 4093 case C(UR_SET_HUB_DEPTH, UT_WRITE_CLASS_DEVICE):
4025 break; 4094 break;
4026 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE): 4095 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
4027 break; 4096 break;
4028 /* Set Port Feature request */ 4097 /* Set Port Feature request */
4029 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER): { 4098 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER): {
4030 int optval = (index >> 8) & 0xff; 4099 int optval = (index >> 8) & 0xff;
4031 index &= 0xff; 4100 index &= 0xff;
4032 if (index < 1 || index > sc->sc_rhportcount[bn]) { 4101 if (index < 1 || index > sc->sc_rhportcount[bn]) {
4033 return -1; 4102 return -1;
4034 } 4103 }
4035 4104
4036 const size_t cp = xhci_rhport2ctlrport(sc, bn, index); 4105 const size_t cp = xhci_rhport2ctlrport(sc, bn, index);
4037 4106
4038 port = XHCI_PORTSC(cp); 4107 port = XHCI_PORTSC(cp);
4039 v = xhci_op_read_4(sc, port); 4108 v = xhci_op_read_4(sc, port);
4040 DPRINTFN(4, "index %jd cp %jd portsc=0x%08jx", index, cp, v, 0); 4109 DPRINTFN(4, "index %jd cp %jd portsc=0x%08jx", index, cp, v, 0);
4041 v &= ~XHCI_PS_CLEAR; 4110 v &= ~XHCI_PS_CLEAR;
4042 switch (value) { 4111 switch (value) {
4043 case UHF_PORT_ENABLE: 4112 case UHF_PORT_ENABLE:
4044 xhci_op_write_4(sc, port, v | XHCI_PS_PED); 4113 xhci_op_write_4(sc, port, v | XHCI_PS_PED);
4045 break; 4114 break;
4046 case UHF_PORT_SUSPEND: 4115 case UHF_PORT_SUSPEND:
4047 /* XXX suspend */ 4116 /* XXX suspend */
4048 break; 4117 break;
4049 case UHF_PORT_RESET: 4118 case UHF_PORT_RESET:
4050 v &= ~(XHCI_PS_PED | XHCI_PS_PR); 4119 v &= ~(XHCI_PS_PED | XHCI_PS_PR);
4051 xhci_op_write_4(sc, port, v | XHCI_PS_PR); 4120 xhci_op_write_4(sc, port, v | XHCI_PS_PR);
4052 /* Wait for reset to complete. */ 4121 /* Wait for reset to complete. */
4053 usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY); 4122 usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
4054 if (sc->sc_dying) { 4123 if (sc->sc_dying) {
4055 return -1; 4124 return -1;
4056 } 4125 }
4057 v = xhci_op_read_4(sc, port); 4126 v = xhci_op_read_4(sc, port);
4058 if (v & XHCI_PS_PR) { 4127 if (v & XHCI_PS_PR) {
4059 xhci_op_write_4(sc, port, v & ~XHCI_PS_PR); 4128 xhci_op_write_4(sc, port, v & ~XHCI_PS_PR);
4060 usb_delay_ms(&sc->sc_bus, 10); 4129 usb_delay_ms(&sc->sc_bus, 10);
4061 /* XXX */ 4130 /* XXX */
4062 } 4131 }
4063 break; 4132 break;
4064 case UHF_PORT_POWER: 4133 case UHF_PORT_POWER:
4065 /* XXX power control */ 4134 /* XXX power control */
4066 break; 4135 break;
4067 /* XXX more */ 4136 /* XXX more */
4068 case UHF_C_PORT_RESET: 4137 case UHF_C_PORT_RESET:
4069 xhci_op_write_4(sc, port, v | XHCI_PS_PRC); 4138 xhci_op_write_4(sc, port, v | XHCI_PS_PRC);
4070 break; 4139 break;
4071 case UHF_PORT_U1_TIMEOUT: 4140 case UHF_PORT_U1_TIMEOUT:
4072 if (XHCI_PS_SPEED_GET(v) < XHCI_PS_SPEED_SS) { 4141 if (XHCI_PS_SPEED_GET(v) < XHCI_PS_SPEED_SS) {
4073 return -1; 4142 return -1;
4074 } 4143 }
4075 port = XHCI_PORTPMSC(cp); 4144 port = XHCI_PORTPMSC(cp);
4076 v = xhci_op_read_4(sc, port); 4145 v = xhci_op_read_4(sc, port);
4077 DPRINTFN(4, "index %jd cp %jd portpmsc=0x%08jx", 4146 DPRINTFN(4, "index %jd cp %jd portpmsc=0x%08jx",
4078 index, cp, v, 0); 4147 index, cp, v, 0);
4079 v &= ~XHCI_PM3_U1TO_SET(0xff); 4148 v &= ~XHCI_PM3_U1TO_SET(0xff);
4080 v |= XHCI_PM3_U1TO_SET(optval); 4149 v |= XHCI_PM3_U1TO_SET(optval);
4081 xhci_op_write_4(sc, port, v); 4150 xhci_op_write_4(sc, port, v);
4082 break; 4151 break;
4083 case UHF_PORT_U2_TIMEOUT: 4152 case UHF_PORT_U2_TIMEOUT:
4084 if (XHCI_PS_SPEED_GET(v) < XHCI_PS_SPEED_SS) { 4153 if (XHCI_PS_SPEED_GET(v) < XHCI_PS_SPEED_SS) {
4085 return -1; 4154 return -1;
4086 } 4155 }
4087 port = XHCI_PORTPMSC(cp); 4156 port = XHCI_PORTPMSC(cp);
4088 v = xhci_op_read_4(sc, port); 4157 v = xhci_op_read_4(sc, port);
4089 DPRINTFN(4, "index %jd cp %jd portpmsc=0x%08jx", 4158 DPRINTFN(4, "index %jd cp %jd portpmsc=0x%08jx",
4090 index, cp, v, 0); 4159 index, cp, v, 0);
4091 v &= ~XHCI_PM3_U2TO_SET(0xff); 4160 v &= ~XHCI_PM3_U2TO_SET(0xff);
4092 v |= XHCI_PM3_U2TO_SET(optval); 4161 v |= XHCI_PM3_U2TO_SET(optval);
4093 xhci_op_write_4(sc, port, v); 4162 xhci_op_write_4(sc, port, v);
4094 break; 4163 break;
4095 default: 4164 default:
4096 return -1; 4165 return -1;
4097 } 4166 }
4098 } 4167 }
4099 break; 4168 break;
4100 case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER): 4169 case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
4101 case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER): 4170 case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
4102 case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER): 4171 case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
4103 case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER): 4172 case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
4104 break; 4173 break;
4105 default: 4174 default:
4106 /* default from usbroothub */ 4175 /* default from usbroothub */
4107 return buflen; 4176 return buflen;
4108 } 4177 }
4109 4178
4110 return totlen; 4179 return totlen;
4111} 4180}
4112 4181
4113/* root hub interrupt */ 4182/* root hub interrupt */
4114 4183
4115static usbd_status 4184static usbd_status
4116xhci_root_intr_transfer(struct usbd_xfer *xfer) 4185xhci_root_intr_transfer(struct usbd_xfer *xfer)
4117{ 4186{
4118 struct xhci_softc * const sc = XHCI_XFER2SC(xfer); 4187 struct xhci_softc * const sc = XHCI_XFER2SC(xfer);
4119 usbd_status err; 4188 usbd_status err;
4120 4189
4121 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 4190 XHCIHIST_FUNC(); XHCIHIST_CALLED();
4122 4191
4123 /* Insert last in queue. */ 4192 /* Insert last in queue. */
4124 mutex_enter(&sc->sc_lock); 4193 mutex_enter(&sc->sc_lock);
4125 err = usb_insert_transfer(xfer); 4194 err = usb_insert_transfer(xfer);
4126 mutex_exit(&sc->sc_lock); 4195 mutex_exit(&sc->sc_lock);
4127 if (err) 4196 if (err)
4128 return err; 4197 return err;
4129 4198

cvs diff -r1.18 -r1.19 src/sys/dev/usb/xhcivar.h (switch to unified diff)

--- src/sys/dev/usb/xhcivar.h 2021/05/23 11:49:45 1.18
+++ src/sys/dev/usb/xhcivar.h 2021/05/23 21:12:28 1.19
@@ -1,166 +1,167 @@ @@ -1,166 +1,167 @@
1/* $NetBSD: xhcivar.h,v 1.18 2021/05/23 11:49:45 riastradh Exp $ */ 1/* $NetBSD: xhcivar.h,v 1.19 2021/05/23 21:12:28 riastradh Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2013 Jonathan A. Kollasch 4 * Copyright (c) 2013 Jonathan A. Kollasch
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
15 * 15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29#ifndef _DEV_USB_XHCIVAR_H_ 29#ifndef _DEV_USB_XHCIVAR_H_
30#define _DEV_USB_XHCIVAR_H_ 30#define _DEV_USB_XHCIVAR_H_
31 31
32#include <sys/pool.h> 32#include <sys/pool.h>
33 33
34#define XHCI_MAX_DCI 31 34#define XHCI_MAX_DCI 31
35 35
36struct xhci_soft_trb { 36struct xhci_soft_trb {
37 uint64_t trb_0; 37 uint64_t trb_0;
38 uint32_t trb_2; 38 uint32_t trb_2;
39 uint32_t trb_3; 39 uint32_t trb_3;
40}; 40};
41 41
42struct xhci_xfer { 42struct xhci_xfer {
43 struct usbd_xfer xx_xfer; 43 struct usbd_xfer xx_xfer;
44 struct xhci_soft_trb *xx_trb; 44 struct xhci_soft_trb *xx_trb;
45 u_int xx_ntrb; 45 u_int xx_ntrb;
46 u_int xx_isoc_done; 46 u_int xx_isoc_done;
47}; 47};
48 48
49#define XHCI_BUS2SC(bus) ((bus)->ub_hcpriv) 49#define XHCI_BUS2SC(bus) ((bus)->ub_hcpriv)
50#define XHCI_PIPE2SC(pipe) XHCI_BUS2SC((pipe)->up_dev->ud_bus) 50#define XHCI_PIPE2SC(pipe) XHCI_BUS2SC((pipe)->up_dev->ud_bus)
51#define XHCI_XFER2SC(xfer) XHCI_BUS2SC((xfer)->ux_bus) 51#define XHCI_XFER2SC(xfer) XHCI_BUS2SC((xfer)->ux_bus)
52#define XHCI_XFER2BUS(xfer) ((xfer)->ux_bus) 52#define XHCI_XFER2BUS(xfer) ((xfer)->ux_bus)
53#define XHCI_XPIPE2SC(d) XHCI_BUS2SC((d)->xp_pipe.up_dev->ud_bus) 53#define XHCI_XPIPE2SC(d) XHCI_BUS2SC((d)->xp_pipe.up_dev->ud_bus)
54 54
55#define XHCI_XFER2XXFER(xfer) ((struct xhci_xfer *)(xfer)) 55#define XHCI_XFER2XXFER(xfer) ((struct xhci_xfer *)(xfer))
56 56
57struct xhci_ring { 57struct xhci_ring {
58 usb_dma_t xr_dma; 58 usb_dma_t xr_dma;
59 kmutex_t xr_lock; 59 kmutex_t xr_lock;
60 struct xhci_trb * xr_trb; 60 struct xhci_trb * xr_trb;
61 void **xr_cookies; 61 void **xr_cookies;
62 u_int xr_ntrb; /* number of elements for above */ 62 u_int xr_ntrb; /* number of elements for above */
63 u_int xr_ep; /* enqueue pointer */ 63 u_int xr_ep; /* enqueue pointer */
64 u_int xr_cs; /* cycle state */ 64 u_int xr_cs; /* cycle state */
65 bool is_halted; 65 bool is_halted;
66}; 66};
67 67
68struct xhci_slot { 68struct xhci_slot {
69 usb_dma_t xs_dc_dma; /* device context page */ 69 usb_dma_t xs_dc_dma; /* device context page */
70 usb_dma_t xs_ic_dma; /* input context page */ 70 usb_dma_t xs_ic_dma; /* input context page */
71 struct xhci_ring *xs_xr[XHCI_MAX_DCI + 1]; 71 struct xhci_ring *xs_xr[XHCI_MAX_DCI + 1];
72 /* transfer rings */ 72 /* transfer rings */
73 u_int xs_idx; /* slot index */ 73 u_int xs_idx; /* slot index */
74}; 74};
75 75
76struct xhci_softc { 76struct xhci_softc {
77 device_t sc_dev; 77 device_t sc_dev;
78 device_t sc_child; 78 device_t sc_child;
79 device_t sc_child2; 79 device_t sc_child2;
80 bus_size_t sc_ios; 80 bus_size_t sc_ios;
81 bus_space_tag_t sc_iot; 81 bus_space_tag_t sc_iot;
82 bus_space_handle_t sc_ioh; /* Base */ 82 bus_space_handle_t sc_ioh; /* Base */
83 bus_space_handle_t sc_cbh; /* Capability Base */ 83 bus_space_handle_t sc_cbh; /* Capability Base */
84 bus_space_handle_t sc_obh; /* Operational Base */ 84 bus_space_handle_t sc_obh; /* Operational Base */
85 bus_space_handle_t sc_rbh; /* Runtime Base */ 85 bus_space_handle_t sc_rbh; /* Runtime Base */
86 bus_space_handle_t sc_dbh; /* Doorbell Registers */ 86 bus_space_handle_t sc_dbh; /* Doorbell Registers */
87 struct usbd_bus sc_bus; /* USB 3 bus */ 87 struct usbd_bus sc_bus; /* USB 3 bus */
88 struct usbd_bus sc_bus2; /* USB 2 bus */ 88 struct usbd_bus sc_bus2; /* USB 2 bus */
89 89
90 kmutex_t sc_lock; 90 kmutex_t sc_lock;
91 kmutex_t sc_intr_lock; 91 kmutex_t sc_intr_lock;
92 92
93 pool_cache_t sc_xferpool; 93 pool_cache_t sc_xferpool;
94 94
95 bus_size_t sc_pgsz; /* xHCI page size */ 95 bus_size_t sc_pgsz; /* xHCI page size */
96 uint32_t sc_ctxsz; 96 uint32_t sc_ctxsz;
97 int sc_maxslots; 97 int sc_maxslots;
98 int sc_maxintrs; 98 int sc_maxintrs;
99 int sc_maxspbuf; 99 int sc_maxspbuf;
100 100
101 /* 101 /*
102 * Port routing and root hub - xHCI 4.19.7 102 * Port routing and root hub - xHCI 4.19.7
103 */ 103 */
104 int sc_maxports; /* number of controller ports */ 104 int sc_maxports; /* number of controller ports */
105 105
106 uint8_t *sc_ctlrportbus; /* a bus bit per port */ 106 uint8_t *sc_ctlrportbus; /* a bus bit per port */
107 107
108 int *sc_ctlrportmap; 108 int *sc_ctlrportmap;
109 int *sc_rhportmap[2]; 109 int *sc_rhportmap[2];
110 int sc_rhportcount[2]; 110 int sc_rhportcount[2];
111 struct usbd_xfer *sc_intrxfer[2]; 111 struct usbd_xfer *sc_intrxfer[2];
112 112
113 113
114 struct xhci_slot * sc_slots; 114 struct xhci_slot * sc_slots;
115 115
116 struct xhci_ring *sc_cr; /* command ring */ 116 struct xhci_ring *sc_cr; /* command ring */
117 struct xhci_ring *sc_er; /* event ring */ 117 struct xhci_ring *sc_er; /* event ring */
118 118
119 usb_dma_t sc_eventst_dma; 119 usb_dma_t sc_eventst_dma;
120 usb_dma_t sc_dcbaa_dma; 120 usb_dma_t sc_dcbaa_dma;
121 usb_dma_t sc_spbufarray_dma; 121 usb_dma_t sc_spbufarray_dma;
122 usb_dma_t *sc_spbuf_dma; 122 usb_dma_t *sc_spbuf_dma;
123 123
124 kcondvar_t sc_cmdbusy_cv; 124 kcondvar_t sc_cmdbusy_cv;
125 kcondvar_t sc_command_cv; 125 kcondvar_t sc_command_cv;
126 bus_addr_t sc_command_addr; 126 bus_addr_t sc_command_addr;
127 struct xhci_soft_trb sc_result_trb; 127 struct xhci_soft_trb sc_result_trb;
128 bool sc_resultpending; 128 bool sc_resultpending;
129 129
130 bool sc_dying; 130 bool sc_dying;
 131 struct lwp *sc_suspender;
131 132
132 void (*sc_vendor_init)(struct xhci_softc *); 133 void (*sc_vendor_init)(struct xhci_softc *);
133 int (*sc_vendor_port_status)(struct xhci_softc *, uint32_t, int); 134 int (*sc_vendor_port_status)(struct xhci_softc *, uint32_t, int);
134 135
135 int sc_quirks; 136 int sc_quirks;
136#define XHCI_QUIRK_INTEL __BIT(0) /* Intel xhci chip */ 137#define XHCI_QUIRK_INTEL __BIT(0) /* Intel xhci chip */
137#define XHCI_DEFERRED_START __BIT(1) 138#define XHCI_DEFERRED_START __BIT(1)
138 uint32_t sc_hcc; /* copy of HCCPARAMS1 */ 139 uint32_t sc_hcc; /* copy of HCCPARAMS1 */
139 uint32_t sc_hcc2; /* copy of HCCPARAMS2 */ 140 uint32_t sc_hcc2; /* copy of HCCPARAMS2 */
140 141
141 struct xhci_registers { 142 struct xhci_registers {
142 uint32_t usbcmd; 143 uint32_t usbcmd;
143 uint32_t dnctrl; 144 uint32_t dnctrl;
144 uint64_t dcbaap; 145 uint64_t dcbaap;
145 uint32_t config; 146 uint32_t config;
146 uint32_t erstsz0; 147 uint32_t erstsz0;
147 uint64_t erstba0; 148 uint64_t erstba0;
148 uint64_t erdp0; 149 uint64_t erdp0;
149 uint32_t iman0; 150 uint32_t iman0;
150 uint32_t imod0; 151 uint32_t imod0;
151 } sc_regs; 152 } sc_regs;
152}; 153};
153 154
154int xhci_init(struct xhci_softc *); 155int xhci_init(struct xhci_softc *);
155void xhci_start(struct xhci_softc *); 156void xhci_start(struct xhci_softc *);
156int xhci_intr(void *); 157int xhci_intr(void *);
157int xhci_detach(struct xhci_softc *, int); 158int xhci_detach(struct xhci_softc *, int);
158int xhci_activate(device_t, enum devact); 159int xhci_activate(device_t, enum devact);
159void xhci_childdet(device_t, device_t); 160void xhci_childdet(device_t, device_t);
160bool xhci_suspend(device_t, const pmf_qual_t *); 161bool xhci_suspend(device_t, const pmf_qual_t *);
161bool xhci_resume(device_t, const pmf_qual_t *); 162bool xhci_resume(device_t, const pmf_qual_t *);
162bool xhci_shutdown(device_t, int); 163bool xhci_shutdown(device_t, int);
163 164
164#define XHCI_TRANSFER_RING_TRBS 256 165#define XHCI_TRANSFER_RING_TRBS 256
165 166
166#endif /* _DEV_USB_XHCIVAR_H_ */ 167#endif /* _DEV_USB_XHCIVAR_H_ */