xref: /netbsd-src/sys/dev/usb/ehci.c (revision 796c32c94f6e154afc9de0f63da35c91bb739b45)
1 /*	$NetBSD: ehci.c,v 1.257 2017/11/17 08:22:02 skrll Exp $ */
2 
3 /*
4  * Copyright (c) 2004-2012 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Lennart Augustsson (lennart@augustsson.net), Charles M. Hannum,
9  * Jeremy Morse (jeremy.morse@gmail.com), Jared D. McNeill
10  * (jmcneill@invisible.ca) and Matthew R. Green (mrg@eterna.com.au).
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller.
36  *
37  * The EHCI 1.0 spec can be found at
38  * http://www.intel.com/technology/usb/spec.htm
39  * and the USB 2.0 spec at
40  * http://www.usb.org/developers/docs/
41  *
42  */
43 
44 /*
45  * TODO:
46  * 1) hold off explorations by companion controllers until ehci has started.
47  *
48  * 2) The hub driver needs to handle and schedule the transaction translator,
49  *    to assign place in frame where different devices get to go. See chapter
50  *    on hubs in USB 2.0 for details.
51  *
52  * 3) Command failures are not recovered correctly.
53  */
54 
55 #include <sys/cdefs.h>
56 __KERNEL_RCSID(0, "$NetBSD: ehci.c,v 1.257 2017/11/17 08:22:02 skrll Exp $");
57 
58 #include "ohci.h"
59 #include "uhci.h"
60 
61 #ifdef _KERNEL_OPT
62 #include "opt_usb.h"
63 #endif
64 
65 #include <sys/param.h>
66 
67 #include <sys/bus.h>
68 #include <sys/cpu.h>
69 #include <sys/device.h>
70 #include <sys/kernel.h>
71 #include <sys/kmem.h>
72 #include <sys/mutex.h>
73 #include <sys/proc.h>
74 #include <sys/queue.h>
75 #include <sys/select.h>
76 #include <sys/sysctl.h>
77 #include <sys/systm.h>
78 
79 #include <machine/endian.h>
80 
81 #include <dev/usb/usb.h>
82 #include <dev/usb/usbdi.h>
83 #include <dev/usb/usbdivar.h>
84 #include <dev/usb/usbhist.h>
85 #include <dev/usb/usb_mem.h>
86 #include <dev/usb/usb_quirks.h>
87 
88 #include <dev/usb/ehcireg.h>
89 #include <dev/usb/ehcivar.h>
90 #include <dev/usb/usbroothub.h>
91 
92 
93 #ifdef USB_DEBUG
94 #ifndef EHCI_DEBUG
95 #define ehcidebug 0
96 #else
97 static int ehcidebug = 0;
98 
99 SYSCTL_SETUP(sysctl_hw_ehci_setup, "sysctl hw.ehci setup")
100 {
101 	int err;
102 	const struct sysctlnode *rnode;
103 	const struct sysctlnode *cnode;
104 
105 	err = sysctl_createv(clog, 0, NULL, &rnode,
106 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "ehci",
107 	    SYSCTL_DESCR("ehci global controls"),
108 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
109 
110 	if (err)
111 		goto fail;
112 
113 	/* control debugging printfs */
114 	err = sysctl_createv(clog, 0, &rnode, &cnode,
115 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
116 	    "debug", SYSCTL_DESCR("Enable debugging output"),
117 	    NULL, 0, &ehcidebug, sizeof(ehcidebug), CTL_CREATE, CTL_EOL);
118 	if (err)
119 		goto fail;
120 
121 	return;
122 fail:
123 	aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
124 }
125 
126 #endif /* EHCI_DEBUG */
127 #endif /* USB_DEBUG */
128 
129 #define	DPRINTF(FMT,A,B,C,D)	USBHIST_LOG(ehcidebug,FMT,A,B,C,D)
130 #define	DPRINTFN(N,FMT,A,B,C,D)	USBHIST_LOGN(ehcidebug,N,FMT,A,B,C,D)
131 #define	EHCIHIST_FUNC()		USBHIST_FUNC()
132 #define	EHCIHIST_CALLED()	USBHIST_CALLED(ehcidebug)
133 
134 struct ehci_pipe {
135 	struct usbd_pipe pipe;
136 	int nexttoggle;
137 
138 	ehci_soft_qh_t *sqh;
139 	union {
140 		/* Control pipe */
141 		struct {
142 			usb_dma_t reqdma;
143 		} ctrl;
144 		/* Interrupt pipe */
145 		struct {
146 			u_int length;
147 		} intr;
148 		/* Iso pipe */
149 		struct {
150 			u_int next_frame;
151 			u_int cur_xfers;
152 		} isoc;
153 	};
154 };
155 
156 typedef TAILQ_HEAD(ex_completeq, ehci_xfer) ex_completeq_t;
157 
158 Static usbd_status	ehci_open(struct usbd_pipe *);
159 Static void		ehci_poll(struct usbd_bus *);
160 Static void		ehci_softintr(void *);
161 Static int		ehci_intr1(ehci_softc_t *);
162 Static void		ehci_check_qh_intr(ehci_softc_t *, struct ehci_xfer *,
163 			    ex_completeq_t *);
164 Static void		ehci_check_itd_intr(ehci_softc_t *, struct ehci_xfer *,
165 			    ex_completeq_t *);
166 Static void		ehci_check_sitd_intr(ehci_softc_t *, struct ehci_xfer *,
167 			    ex_completeq_t *);
168 Static void		ehci_idone(struct ehci_xfer *, ex_completeq_t *);
169 Static void		ehci_timeout(void *);
170 Static void		ehci_timeout_task(void *);
171 Static void		ehci_intrlist_timeout(void *);
172 Static void		ehci_doorbell(void *);
173 Static void		ehci_pcd(void *);
174 
175 Static struct usbd_xfer *
176 			ehci_allocx(struct usbd_bus *, unsigned int);
177 Static void		ehci_freex(struct usbd_bus *, struct usbd_xfer *);
178 
179 Static void		ehci_get_lock(struct usbd_bus *, kmutex_t **);
180 Static int		ehci_roothub_ctrl(struct usbd_bus *,
181 			    usb_device_request_t *, void *, int);
182 
183 Static usbd_status	ehci_root_intr_transfer(struct usbd_xfer *);
184 Static usbd_status	ehci_root_intr_start(struct usbd_xfer *);
185 Static void		ehci_root_intr_abort(struct usbd_xfer *);
186 Static void		ehci_root_intr_close(struct usbd_pipe *);
187 Static void		ehci_root_intr_done(struct usbd_xfer *);
188 
189 Static int		ehci_device_ctrl_init(struct usbd_xfer *);
190 Static void		ehci_device_ctrl_fini(struct usbd_xfer *);
191 Static usbd_status	ehci_device_ctrl_transfer(struct usbd_xfer *);
192 Static usbd_status	ehci_device_ctrl_start(struct usbd_xfer *);
193 Static void		ehci_device_ctrl_abort(struct usbd_xfer *);
194 Static void		ehci_device_ctrl_close(struct usbd_pipe *);
195 Static void		ehci_device_ctrl_done(struct usbd_xfer *);
196 
197 Static int		ehci_device_bulk_init(struct usbd_xfer *);
198 Static void		ehci_device_bulk_fini(struct usbd_xfer *);
199 Static usbd_status	ehci_device_bulk_transfer(struct usbd_xfer *);
200 Static usbd_status	ehci_device_bulk_start(struct usbd_xfer *);
201 Static void		ehci_device_bulk_abort(struct usbd_xfer *);
202 Static void		ehci_device_bulk_close(struct usbd_pipe *);
203 Static void		ehci_device_bulk_done(struct usbd_xfer *);
204 
205 Static int		ehci_device_intr_init(struct usbd_xfer *);
206 Static void		ehci_device_intr_fini(struct usbd_xfer *);
207 Static usbd_status	ehci_device_intr_transfer(struct usbd_xfer *);
208 Static usbd_status	ehci_device_intr_start(struct usbd_xfer *);
209 Static void		ehci_device_intr_abort(struct usbd_xfer *);
210 Static void		ehci_device_intr_close(struct usbd_pipe *);
211 Static void		ehci_device_intr_done(struct usbd_xfer *);
212 
213 Static int		ehci_device_isoc_init(struct usbd_xfer *);
214 Static void		ehci_device_isoc_fini(struct usbd_xfer *);
215 Static usbd_status	ehci_device_isoc_transfer(struct usbd_xfer *);
216 Static void		ehci_device_isoc_abort(struct usbd_xfer *);
217 Static void		ehci_device_isoc_close(struct usbd_pipe *);
218 Static void		ehci_device_isoc_done(struct usbd_xfer *);
219 
220 Static int		ehci_device_fs_isoc_init(struct usbd_xfer *);
221 Static void		ehci_device_fs_isoc_fini(struct usbd_xfer *);
222 Static usbd_status	ehci_device_fs_isoc_transfer(struct usbd_xfer *);
223 Static void		ehci_device_fs_isoc_abort(struct usbd_xfer *);
224 Static void		ehci_device_fs_isoc_close(struct usbd_pipe *);
225 Static void		ehci_device_fs_isoc_done(struct usbd_xfer *);
226 
227 Static void		ehci_device_clear_toggle(struct usbd_pipe *);
228 Static void		ehci_noop(struct usbd_pipe *);
229 
230 Static void		ehci_disown(ehci_softc_t *, int, int);
231 
232 Static ehci_soft_qh_t *	ehci_alloc_sqh(ehci_softc_t *);
233 Static void		ehci_free_sqh(ehci_softc_t *, ehci_soft_qh_t *);
234 
235 Static ehci_soft_qtd_t *ehci_alloc_sqtd(ehci_softc_t *);
236 Static void		ehci_free_sqtd(ehci_softc_t *, ehci_soft_qtd_t *);
237 Static int		ehci_alloc_sqtd_chain(ehci_softc_t *,
238 			    struct usbd_xfer *, int, int, ehci_soft_qtd_t **);
239 Static void		ehci_free_sqtds(ehci_softc_t *, struct ehci_xfer *);
240 
241 Static void		ehci_reset_sqtd_chain(ehci_softc_t *, struct usbd_xfer *,
242 			    int, int, int *, ehci_soft_qtd_t **);
243 Static void		ehci_append_sqtd(ehci_soft_qtd_t *, ehci_soft_qtd_t *);
244 
245 Static ehci_soft_itd_t *ehci_alloc_itd(ehci_softc_t *);
246 Static ehci_soft_sitd_t *
247 			ehci_alloc_sitd(ehci_softc_t *);
248 
249 Static void 		ehci_remove_itd_chain(ehci_softc_t *, ehci_soft_itd_t *);
250 Static void		ehci_remove_sitd_chain(ehci_softc_t *, ehci_soft_sitd_t *);
251 Static void 		ehci_free_itd_chain(ehci_softc_t *, ehci_soft_itd_t *);
252 Static void		ehci_free_sitd_chain(ehci_softc_t *, ehci_soft_sitd_t *);
253 
254 static inline void
255 ehci_free_itd_locked(ehci_softc_t *sc, ehci_soft_itd_t *itd)
256 {
257 
258 	LIST_INSERT_HEAD(&sc->sc_freeitds, itd, free_list);
259 }
260 
261 static inline void
262 ehci_free_sitd_locked(ehci_softc_t *sc, ehci_soft_sitd_t *sitd)
263 {
264 
265 	LIST_INSERT_HEAD(&sc->sc_freesitds, sitd, free_list);
266 }
267 
268 Static void 		ehci_abort_isoc_xfer(struct usbd_xfer *, usbd_status);
269 
270 Static usbd_status	ehci_device_setintr(ehci_softc_t *, ehci_soft_qh_t *,
271 			    int);
272 
273 Static void		ehci_add_qh(ehci_softc_t *, ehci_soft_qh_t *,
274 				    ehci_soft_qh_t *);
275 Static void		ehci_rem_qh(ehci_softc_t *, ehci_soft_qh_t *,
276 				    ehci_soft_qh_t *);
277 Static void		ehci_set_qh_qtd(ehci_soft_qh_t *, ehci_soft_qtd_t *);
278 Static void		ehci_sync_hc(ehci_softc_t *);
279 
280 Static void		ehci_close_pipe(struct usbd_pipe *, ehci_soft_qh_t *);
281 Static void		ehci_abort_xfer(struct usbd_xfer *, usbd_status);
282 
283 #ifdef EHCI_DEBUG
284 Static ehci_softc_t 	*theehci;
285 void			ehci_dump(void);
286 #endif
287 
288 #ifdef EHCI_DEBUG
289 Static void		ehci_dump_regs(ehci_softc_t *);
290 Static void		ehci_dump_sqtds(ehci_soft_qtd_t *);
291 Static void		ehci_dump_sqtd(ehci_soft_qtd_t *);
292 Static void		ehci_dump_qtd(ehci_qtd_t *);
293 Static void		ehci_dump_sqh(ehci_soft_qh_t *);
294 Static void		ehci_dump_sitd(struct ehci_soft_itd *);
295 Static void 		ehci_dump_itds(ehci_soft_itd_t *);
296 Static void		ehci_dump_itd(struct ehci_soft_itd *);
297 Static void		ehci_dump_exfer(struct ehci_xfer *);
298 #endif
299 
300 #define EHCI_NULL htole32(EHCI_LINK_TERMINATE)
301 
302 static inline void
303 ehci_add_intr_list(ehci_softc_t *sc, struct ehci_xfer *ex)
304 {
305 
306 	TAILQ_INSERT_TAIL(&sc->sc_intrhead, ex, ex_next);
307 }
308 
309 static inline void
310 ehci_del_intr_list(ehci_softc_t *sc, struct ehci_xfer *ex)
311 {
312 
313 	TAILQ_REMOVE(&sc->sc_intrhead, ex, ex_next);
314 }
315 
316 Static const struct usbd_bus_methods ehci_bus_methods = {
317 	.ubm_open =	ehci_open,
318 	.ubm_softint =	ehci_softintr,
319 	.ubm_dopoll =	ehci_poll,
320 	.ubm_allocx =	ehci_allocx,
321 	.ubm_freex =	ehci_freex,
322 	.ubm_getlock =	ehci_get_lock,
323 	.ubm_rhctrl =	ehci_roothub_ctrl,
324 };
325 
326 Static const struct usbd_pipe_methods ehci_root_intr_methods = {
327 	.upm_transfer =	ehci_root_intr_transfer,
328 	.upm_start =	ehci_root_intr_start,
329 	.upm_abort =	ehci_root_intr_abort,
330 	.upm_close =	ehci_root_intr_close,
331 	.upm_cleartoggle =	ehci_noop,
332 	.upm_done =	ehci_root_intr_done,
333 };
334 
335 Static const struct usbd_pipe_methods ehci_device_ctrl_methods = {
336 	.upm_init =	ehci_device_ctrl_init,
337 	.upm_fini =	ehci_device_ctrl_fini,
338 	.upm_transfer =	ehci_device_ctrl_transfer,
339 	.upm_start =	ehci_device_ctrl_start,
340 	.upm_abort =	ehci_device_ctrl_abort,
341 	.upm_close =	ehci_device_ctrl_close,
342 	.upm_cleartoggle =	ehci_noop,
343 	.upm_done =	ehci_device_ctrl_done,
344 };
345 
346 Static const struct usbd_pipe_methods ehci_device_intr_methods = {
347 	.upm_init =	ehci_device_intr_init,
348 	.upm_fini =	ehci_device_intr_fini,
349 	.upm_transfer =	ehci_device_intr_transfer,
350 	.upm_start =	ehci_device_intr_start,
351 	.upm_abort =	ehci_device_intr_abort,
352 	.upm_close =	ehci_device_intr_close,
353 	.upm_cleartoggle =	ehci_device_clear_toggle,
354 	.upm_done =	ehci_device_intr_done,
355 };
356 
357 Static const struct usbd_pipe_methods ehci_device_bulk_methods = {
358 	.upm_init =	ehci_device_bulk_init,
359 	.upm_fini =	ehci_device_bulk_fini,
360 	.upm_transfer =	ehci_device_bulk_transfer,
361 	.upm_start =	ehci_device_bulk_start,
362 	.upm_abort =	ehci_device_bulk_abort,
363 	.upm_close =	ehci_device_bulk_close,
364 	.upm_cleartoggle =	ehci_device_clear_toggle,
365 	.upm_done =	ehci_device_bulk_done,
366 };
367 
368 Static const struct usbd_pipe_methods ehci_device_isoc_methods = {
369 	.upm_init =	ehci_device_isoc_init,
370 	.upm_fini =	ehci_device_isoc_fini,
371 	.upm_transfer =	ehci_device_isoc_transfer,
372 	.upm_abort =	ehci_device_isoc_abort,
373 	.upm_close =	ehci_device_isoc_close,
374 	.upm_cleartoggle =	ehci_noop,
375 	.upm_done =	ehci_device_isoc_done,
376 };
377 
378 Static const struct usbd_pipe_methods ehci_device_fs_isoc_methods = {
379 	.upm_init =	ehci_device_fs_isoc_init,
380 	.upm_fini =	ehci_device_fs_isoc_fini,
381 	.upm_transfer =	ehci_device_fs_isoc_transfer,
382 	.upm_abort =	ehci_device_fs_isoc_abort,
383 	.upm_close =	ehci_device_fs_isoc_close,
384 	.upm_cleartoggle = ehci_noop,
385 	.upm_done =	ehci_device_fs_isoc_done,
386 };
387 
388 static const uint8_t revbits[EHCI_MAX_POLLRATE] = {
389 0x00,0x40,0x20,0x60,0x10,0x50,0x30,0x70,0x08,0x48,0x28,0x68,0x18,0x58,0x38,0x78,
390 0x04,0x44,0x24,0x64,0x14,0x54,0x34,0x74,0x0c,0x4c,0x2c,0x6c,0x1c,0x5c,0x3c,0x7c,
391 0x02,0x42,0x22,0x62,0x12,0x52,0x32,0x72,0x0a,0x4a,0x2a,0x6a,0x1a,0x5a,0x3a,0x7a,
392 0x06,0x46,0x26,0x66,0x16,0x56,0x36,0x76,0x0e,0x4e,0x2e,0x6e,0x1e,0x5e,0x3e,0x7e,
393 0x01,0x41,0x21,0x61,0x11,0x51,0x31,0x71,0x09,0x49,0x29,0x69,0x19,0x59,0x39,0x79,
394 0x05,0x45,0x25,0x65,0x15,0x55,0x35,0x75,0x0d,0x4d,0x2d,0x6d,0x1d,0x5d,0x3d,0x7d,
395 0x03,0x43,0x23,0x63,0x13,0x53,0x33,0x73,0x0b,0x4b,0x2b,0x6b,0x1b,0x5b,0x3b,0x7b,
396 0x07,0x47,0x27,0x67,0x17,0x57,0x37,0x77,0x0f,0x4f,0x2f,0x6f,0x1f,0x5f,0x3f,0x7f,
397 };
398 
399 int
400 ehci_init(ehci_softc_t *sc)
401 {
402 	uint32_t vers, sparams, cparams, hcr;
403 	u_int i;
404 	usbd_status err;
405 	ehci_soft_qh_t *sqh;
406 	u_int ncomp;
407 
408 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
409 #ifdef EHCI_DEBUG
410 	theehci = sc;
411 #endif
412 
413 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
414 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_USB);
415 	cv_init(&sc->sc_softwake_cv, "ehciab");
416 	cv_init(&sc->sc_doorbell, "ehcidi");
417 
418 	sc->sc_xferpool = pool_cache_init(sizeof(struct ehci_xfer), 0, 0, 0,
419 	    "ehcixfer", NULL, IPL_USB, NULL, NULL, NULL);
420 
421 	sc->sc_doorbell_si = softint_establish(SOFTINT_USB | SOFTINT_MPSAFE,
422 	    ehci_doorbell, sc);
423 	KASSERT(sc->sc_doorbell_si != NULL);
424 	sc->sc_pcd_si = softint_establish(SOFTINT_USB | SOFTINT_MPSAFE,
425 	    ehci_pcd, sc);
426 	KASSERT(sc->sc_pcd_si != NULL);
427 
428 	sc->sc_offs = EREAD1(sc, EHCI_CAPLENGTH);
429 
430 	vers = EREAD2(sc, EHCI_HCIVERSION);
431 	aprint_verbose("%s: EHCI version %x.%x\n", device_xname(sc->sc_dev),
432 	    vers >> 8, vers & 0xff);
433 
434 	sparams = EREAD4(sc, EHCI_HCSPARAMS);
435 	DPRINTF("sparams=%#jx", sparams, 0, 0, 0);
436 	sc->sc_npcomp = EHCI_HCS_N_PCC(sparams);
437 	ncomp = EHCI_HCS_N_CC(sparams);
438 	if (ncomp != sc->sc_ncomp) {
439 		aprint_verbose("%s: wrong number of companions (%d != %d)\n",
440 			       device_xname(sc->sc_dev), ncomp, sc->sc_ncomp);
441 #if NOHCI == 0 || NUHCI == 0
442 		aprint_error("%s: ohci or uhci probably not configured\n",
443 			     device_xname(sc->sc_dev));
444 #endif
445 		if (ncomp < sc->sc_ncomp)
446 			sc->sc_ncomp = ncomp;
447 	}
448 	if (sc->sc_ncomp > 0) {
449 		KASSERT(!(sc->sc_flags & EHCIF_ETTF));
450 		aprint_normal("%s: %d companion controller%s, %d port%s%s",
451 		    device_xname(sc->sc_dev), sc->sc_ncomp,
452 		    sc->sc_ncomp!=1 ? "s" : "",
453 		    EHCI_HCS_N_PCC(sparams),
454 		    EHCI_HCS_N_PCC(sparams)!=1 ? "s" : "",
455 		    sc->sc_ncomp!=1 ? " each" : "");
456 		if (sc->sc_comps[0]) {
457 			aprint_normal(":");
458 			for (i = 0; i < sc->sc_ncomp; i++)
459 				aprint_normal(" %s",
460 				    device_xname(sc->sc_comps[i]));
461 		}
462 		aprint_normal("\n");
463 	}
464 	sc->sc_noport = EHCI_HCS_N_PORTS(sparams);
465 	sc->sc_hasppc = EHCI_HCS_PPC(sparams);
466 
467 	cparams = EREAD4(sc, EHCI_HCCPARAMS);
468 	DPRINTF("cparams=%#jx", cparams, 0, 0, 0);
469 
470 	if (EHCI_HCC_64BIT(cparams)) {
471 		/* MUST clear segment register if 64 bit capable. */
472 		EOWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
473 	}
474 
475 	if (cparams & EHCI_HCC_IST_FULLFRAME) {
476 		sc->sc_istthreshold = 0;
477 	} else {
478 		sc->sc_istthreshold = EHCI_HCC_GET_IST_THRESHOLD(cparams);
479 	}
480 
481 	sc->sc_bus.ub_revision = USBREV_2_0;
482 	sc->sc_bus.ub_usedma = true;
483 	sc->sc_bus.ub_dmaflags = USBMALLOC_MULTISEG;
484 
485 	/* Reset the controller */
486 	DPRINTF("resetting", 0, 0, 0, 0);
487 	EOWRITE4(sc, EHCI_USBCMD, 0);	/* Halt controller */
488 	usb_delay_ms(&sc->sc_bus, 1);
489 	EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
490 	for (i = 0; i < 100; i++) {
491 		usb_delay_ms(&sc->sc_bus, 1);
492 		hcr = EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_HCRESET;
493 		if (!hcr)
494 			break;
495 	}
496 	if (hcr) {
497 		aprint_error("%s: reset timeout\n", device_xname(sc->sc_dev));
498 		return EIO;
499 	}
500 	if (sc->sc_vendor_init)
501 		sc->sc_vendor_init(sc);
502 
503 	/* XXX need proper intr scheduling */
504 	sc->sc_rand = 96;
505 
506 	/* frame list size at default, read back what we got and use that */
507 	switch (EHCI_CMD_FLS(EOREAD4(sc, EHCI_USBCMD))) {
508 	case 0: sc->sc_flsize = 1024; break;
509 	case 1: sc->sc_flsize = 512; break;
510 	case 2: sc->sc_flsize = 256; break;
511 	case 3: return EIO;
512 	}
513 	err = usb_allocmem(&sc->sc_bus, sc->sc_flsize * sizeof(ehci_link_t),
514 	    EHCI_FLALIGN_ALIGN, &sc->sc_fldma);
515 	if (err)
516 		return err;
517 	DPRINTF("flsize=%jd", sc->sc_flsize, 0, 0, 0);
518 	sc->sc_flist = KERNADDR(&sc->sc_fldma, 0);
519 
520 	for (i = 0; i < sc->sc_flsize; i++) {
521 		sc->sc_flist[i] = EHCI_NULL;
522 	}
523 
524 	EOWRITE4(sc, EHCI_PERIODICLISTBASE, DMAADDR(&sc->sc_fldma, 0));
525 
526 	sc->sc_softitds = kmem_zalloc(sc->sc_flsize * sizeof(ehci_soft_itd_t *),
527 				     KM_SLEEP);
528 	if (sc->sc_softitds == NULL)
529 		return ENOMEM;
530 	LIST_INIT(&sc->sc_freeitds);
531 	LIST_INIT(&sc->sc_freesitds);
532 	TAILQ_INIT(&sc->sc_intrhead);
533 
534 	/* Set up the bus struct. */
535 	sc->sc_bus.ub_methods = &ehci_bus_methods;
536 	sc->sc_bus.ub_pipesize= sizeof(struct ehci_pipe);
537 
538 	sc->sc_eintrs = EHCI_NORMAL_INTRS;
539 
540 	/*
541 	 * Allocate the interrupt dummy QHs. These are arranged to give poll
542 	 * intervals that are powers of 2 times 1ms.
543 	 */
544 	for (i = 0; i < EHCI_INTRQHS; i++) {
545 		sqh = ehci_alloc_sqh(sc);
546 		if (sqh == NULL) {
547 			err = ENOMEM;
548 			goto bad1;
549 		}
550 		sc->sc_islots[i].sqh = sqh;
551 	}
552 	for (i = 0; i < EHCI_INTRQHS; i++) {
553 		sqh = sc->sc_islots[i].sqh;
554 		if (i == 0) {
555 			/* The last (1ms) QH terminates. */
556 			sqh->qh.qh_link = EHCI_NULL;
557 			sqh->next = NULL;
558 		} else {
559 			/* Otherwise the next QH has half the poll interval */
560 			sqh->next = sc->sc_islots[(i + 1) / 2 - 1].sqh;
561 			sqh->qh.qh_link = htole32(sqh->next->physaddr |
562 			    EHCI_LINK_QH);
563 		}
564 		sqh->qh.qh_endp = htole32(EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH));
565 		sqh->qh.qh_endphub = htole32(EHCI_QH_SET_MULT(1));
566 		sqh->qh.qh_curqtd = EHCI_NULL;
567 		sqh->qh.qh_qtd.qtd_next = EHCI_NULL;
568 		sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
569 		sqh->qh.qh_qtd.qtd_status = htole32(EHCI_QTD_HALTED);
570 		sqh->sqtd = NULL;
571 		usb_syncmem(&sqh->dma, sqh->offs, sizeof(sqh->qh),
572 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
573 	}
574 	/* Point the frame list at the last level (128ms). */
575 	for (i = 0; i < sc->sc_flsize; i++) {
576 		int j;
577 
578 		j = (i & ~(EHCI_MAX_POLLRATE-1)) |
579 		    revbits[i & (EHCI_MAX_POLLRATE-1)];
580 		sc->sc_flist[j] = htole32(EHCI_LINK_QH |
581 		    sc->sc_islots[EHCI_IQHIDX(EHCI_IPOLLRATES - 1,
582 		    i)].sqh->physaddr);
583 	}
584 	usb_syncmem(&sc->sc_fldma, 0, sc->sc_flsize * sizeof(ehci_link_t),
585 	    BUS_DMASYNC_PREWRITE);
586 
587 	/* Allocate dummy QH that starts the async list. */
588 	sqh = ehci_alloc_sqh(sc);
589 	if (sqh == NULL) {
590 		err = ENOMEM;
591 		goto bad1;
592 	}
593 	/* Fill the QH */
594 	sqh->qh.qh_endp =
595 	    htole32(EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH) | EHCI_QH_HRECL);
596 	sqh->qh.qh_link =
597 	    htole32(sqh->physaddr | EHCI_LINK_QH);
598 	sqh->qh.qh_curqtd = EHCI_NULL;
599 	sqh->next = NULL;
600 	/* Fill the overlay qTD */
601 	sqh->qh.qh_qtd.qtd_next = EHCI_NULL;
602 	sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
603 	sqh->qh.qh_qtd.qtd_status = htole32(EHCI_QTD_HALTED);
604 	sqh->sqtd = NULL;
605 	usb_syncmem(&sqh->dma, sqh->offs, sizeof(sqh->qh),
606 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
607 #ifdef EHCI_DEBUG
608 	DPRINTFN(5, "--- dump start ---", 0, 0, 0, 0);
609 	ehci_dump_sqh(sqh);
610 	DPRINTFN(5, "--- dump end ---", 0, 0, 0, 0);
611 #endif
612 
613 	/* Point to async list */
614 	sc->sc_async_head = sqh;
615 	EOWRITE4(sc, EHCI_ASYNCLISTADDR, sqh->physaddr | EHCI_LINK_QH);
616 
617 	callout_init(&sc->sc_tmo_intrlist, CALLOUT_MPSAFE);
618 
619 	/* Turn on controller */
620 	EOWRITE4(sc, EHCI_USBCMD,
621 		 EHCI_CMD_ITC_2 | /* 2 microframes interrupt delay */
622 		 (EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_FLS_M) |
623 		 EHCI_CMD_ASE |
624 		 EHCI_CMD_PSE |
625 		 EHCI_CMD_RS);
626 
627 	/* Take over port ownership */
628 	EOWRITE4(sc, EHCI_CONFIGFLAG, EHCI_CONF_CF);
629 
630 	for (i = 0; i < 100; i++) {
631 		usb_delay_ms(&sc->sc_bus, 1);
632 		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
633 		if (!hcr)
634 			break;
635 	}
636 	if (hcr) {
637 		aprint_error("%s: run timeout\n", device_xname(sc->sc_dev));
638 		return EIO;
639 	}
640 
641 	/* Enable interrupts */
642 	DPRINTF("enabling interupts", 0, 0, 0, 0);
643 	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
644 
645 	return 0;
646 
647 #if 0
648  bad2:
649 	ehci_free_sqh(sc, sc->sc_async_head);
650 #endif
651  bad1:
652 	usb_freemem(&sc->sc_bus, &sc->sc_fldma);
653 	return err;
654 }
655 
656 int
657 ehci_intr(void *v)
658 {
659 	ehci_softc_t *sc = v;
660 	int ret = 0;
661 
662 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
663 
664 	if (sc == NULL)
665 		return 0;
666 
667 	mutex_spin_enter(&sc->sc_intr_lock);
668 
669 	if (sc->sc_dying || !device_has_power(sc->sc_dev))
670 		goto done;
671 
672 	/* If we get an interrupt while polling, then just ignore it. */
673 	if (sc->sc_bus.ub_usepolling) {
674 		uint32_t intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
675 
676 		if (intrs)
677 			EOWRITE4(sc, EHCI_USBSTS, intrs); /* Acknowledge */
678 		DPRINTFN(16, "ignored interrupt while polling", 0, 0, 0, 0);
679 		goto done;
680 	}
681 
682 	ret = ehci_intr1(sc);
683 
684 done:
685 	mutex_spin_exit(&sc->sc_intr_lock);
686 	return ret;
687 }
688 
689 Static int
690 ehci_intr1(ehci_softc_t *sc)
691 {
692 	uint32_t intrs, eintrs;
693 
694 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
695 
696 	/* In case the interrupt occurs before initialization has completed. */
697 	if (sc == NULL) {
698 #ifdef DIAGNOSTIC
699 		printf("ehci_intr1: sc == NULL\n");
700 #endif
701 		return 0;
702 	}
703 
704 	KASSERT(mutex_owned(&sc->sc_intr_lock));
705 
706 	intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
707 	if (!intrs)
708 		return 0;
709 
710 	eintrs = intrs & sc->sc_eintrs;
711 	DPRINTF("sc=%#jx intrs=%#jx(%#jx) eintrs=%#jx", (uintptr_t)sc, intrs,
712 	    EOREAD4(sc, EHCI_USBSTS), eintrs);
713 	if (!eintrs)
714 		return 0;
715 
716 	EOWRITE4(sc, EHCI_USBSTS, intrs); /* Acknowledge */
717 	if (eintrs & EHCI_STS_IAA) {
718 		DPRINTF("door bell", 0, 0, 0, 0);
719 		kpreempt_disable();
720 		KASSERT(sc->sc_doorbell_si != NULL);
721 		softint_schedule(sc->sc_doorbell_si);
722 		kpreempt_enable();
723 		eintrs &= ~EHCI_STS_IAA;
724 	}
725 	if (eintrs & (EHCI_STS_INT | EHCI_STS_ERRINT)) {
726 		DPRINTF("INT=%jd  ERRINT=%jd",
727 		    eintrs & EHCI_STS_INT ? 1 : 0,
728 		    eintrs & EHCI_STS_ERRINT ? 1 : 0, 0, 0);
729 		usb_schedsoftintr(&sc->sc_bus);
730 		eintrs &= ~(EHCI_STS_INT | EHCI_STS_ERRINT);
731 	}
732 	if (eintrs & EHCI_STS_HSE) {
733 		printf("%s: unrecoverable error, controller halted\n",
734 		       device_xname(sc->sc_dev));
735 		/* XXX what else */
736 	}
737 	if (eintrs & EHCI_STS_PCD) {
738 		kpreempt_disable();
739 		KASSERT(sc->sc_pcd_si != NULL);
740 		softint_schedule(sc->sc_pcd_si);
741 		kpreempt_enable();
742 		eintrs &= ~EHCI_STS_PCD;
743 	}
744 
745 	if (eintrs != 0) {
746 		/* Block unprocessed interrupts. */
747 		sc->sc_eintrs &= ~eintrs;
748 		EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
749 		printf("%s: blocking intrs 0x%x\n",
750 		       device_xname(sc->sc_dev), eintrs);
751 	}
752 
753 	return 1;
754 }
755 
756 Static void
757 ehci_doorbell(void *addr)
758 {
759 	ehci_softc_t *sc = addr;
760 
761 	mutex_enter(&sc->sc_lock);
762 	cv_broadcast(&sc->sc_doorbell);
763 	mutex_exit(&sc->sc_lock);
764 }
765 
766 Static void
767 ehci_pcd(void *addr)
768 {
769 	ehci_softc_t *sc = addr;
770 	struct usbd_xfer *xfer;
771 	u_char *p;
772 	int i, m;
773 
774 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
775 
776 	mutex_enter(&sc->sc_lock);
777 	xfer = sc->sc_intrxfer;
778 
779 	if (xfer == NULL) {
780 		/* Just ignore the change. */
781 		goto done;
782 	}
783 
784 	p = xfer->ux_buf;
785 	m = min(sc->sc_noport, xfer->ux_length * 8 - 1);
786 	memset(p, 0, xfer->ux_length);
787 	for (i = 1; i <= m; i++) {
788 		/* Pick out CHANGE bits from the status reg. */
789 		if (EOREAD4(sc, EHCI_PORTSC(i)) & EHCI_PS_CLEAR)
790 			p[i/8] |= 1 << (i%8);
791 		if (i % 8 == 7)
792 			DPRINTF("change(%jd)=0x%02jx", i / 8, p[i/8], 0, 0);
793 	}
794 	xfer->ux_actlen = xfer->ux_length;
795 	xfer->ux_status = USBD_NORMAL_COMPLETION;
796 
797 	usb_transfer_complete(xfer);
798 
799 done:
800 	mutex_exit(&sc->sc_lock);
801 }
802 
803 Static void
804 ehci_softintr(void *v)
805 {
806 	struct usbd_bus *bus = v;
807 	ehci_softc_t *sc = EHCI_BUS2SC(bus);
808 	struct ehci_xfer *ex, *nextex;
809 
810 	KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
811 
812 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
813 
814 	ex_completeq_t cq;
815 	TAILQ_INIT(&cq);
816 
817 	/*
818 	 * The only explanation I can think of for why EHCI is as brain dead
819 	 * as UHCI interrupt-wise is that Intel was involved in both.
820 	 * An interrupt just tells us that something is done, we have no
821 	 * clue what, so we need to scan through all active transfers. :-(
822 	 */
823 
824 	/*
825 	 * ehci_idone will remove transfer from sc->sc_intrhead if it's
826 	 * complete and add to our cq list
827 	 *
828 	 */
829 	TAILQ_FOREACH_SAFE(ex, &sc->sc_intrhead, ex_next, nextex) {
830 		switch (ex->ex_type) {
831 		case EX_CTRL:
832 		case EX_BULK:
833 		case EX_INTR:
834 			ehci_check_qh_intr(sc, ex, &cq);
835 			break;
836 		case EX_ISOC:
837 			ehci_check_itd_intr(sc, ex, &cq);
838 			break;
839 		case EX_FS_ISOC:
840 			ehci_check_sitd_intr(sc, ex, &cq);
841 			break;
842 		default:
843 			KASSERT(false);
844 		}
845 
846 	}
847 
848 	/*
849 	 * We abuse ex_next for the interrupt and complete lists and
850 	 * interrupt transfers will get re-added here so use
851 	 * the _SAFE version of TAILQ_FOREACH.
852 	 */
853 	TAILQ_FOREACH_SAFE(ex, &cq, ex_next, nextex) {
854 		usb_transfer_complete(&ex->ex_xfer);
855 	}
856 
857 	/* Schedule a callout to catch any dropped transactions. */
858 	if ((sc->sc_flags & EHCIF_DROPPED_INTR_WORKAROUND) &&
859 	    !TAILQ_EMPTY(&sc->sc_intrhead))
860 		callout_reset(&sc->sc_tmo_intrlist,
861 		    hz, ehci_intrlist_timeout, sc);
862 
863 	if (sc->sc_softwake) {
864 		sc->sc_softwake = 0;
865 		cv_broadcast(&sc->sc_softwake_cv);
866 	}
867 }
868 
869 Static void
870 ehci_check_qh_intr(ehci_softc_t *sc, struct ehci_xfer *ex, ex_completeq_t *cq)
871 {
872 	ehci_soft_qtd_t *sqtd, *fsqtd, *lsqtd;
873 	uint32_t status;
874 
875 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
876 
877 	KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
878 
879 	if (ex->ex_type == EX_CTRL) {
880 		fsqtd = ex->ex_setup;
881 		lsqtd = ex->ex_status;
882 	} else {
883 		fsqtd = ex->ex_sqtdstart;
884 		lsqtd = ex->ex_sqtdend;
885 	}
886 	KASSERTMSG(fsqtd != NULL && lsqtd != NULL,
887 	    "xfer %p xt %d fsqtd %p lsqtd %p", ex, ex->ex_type, fsqtd, lsqtd);
888 
889 	/*
890 	 * If the last TD is still active we need to check whether there
891 	 * is an error somewhere in the middle, or whether there was a
892 	 * short packet (SPD and not ACTIVE).
893 	 */
894 	usb_syncmem(&lsqtd->dma,
895 	    lsqtd->offs + offsetof(ehci_qtd_t, qtd_status),
896 	    sizeof(lsqtd->qtd.qtd_status),
897 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
898 	status = le32toh(lsqtd->qtd.qtd_status);
899 	usb_syncmem(&lsqtd->dma,
900 	    lsqtd->offs + offsetof(ehci_qtd_t, qtd_status),
901 	    sizeof(lsqtd->qtd.qtd_status), BUS_DMASYNC_PREREAD);
902 	if (status & EHCI_QTD_ACTIVE) {
903 		DPRINTFN(10, "active ex=%#jx", (uintptr_t)ex, 0, 0, 0);
904 
905 		/* last qTD has already been checked */
906 		for (sqtd = fsqtd; sqtd != lsqtd; sqtd = sqtd->nextqtd) {
907 			usb_syncmem(&sqtd->dma,
908 			    sqtd->offs + offsetof(ehci_qtd_t, qtd_status),
909 			    sizeof(sqtd->qtd.qtd_status),
910 			    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
911 			status = le32toh(sqtd->qtd.qtd_status);
912 			usb_syncmem(&sqtd->dma,
913 			    sqtd->offs + offsetof(ehci_qtd_t, qtd_status),
914 			    sizeof(sqtd->qtd.qtd_status), BUS_DMASYNC_PREREAD);
915 			/* If there's an active QTD the xfer isn't done. */
916 			if (status & EHCI_QTD_ACTIVE)
917 				break;
918 			/* Any kind of error makes the xfer done. */
919 			if (status & EHCI_QTD_HALTED)
920 				goto done;
921 			/* Handle short packets */
922 			if (EHCI_QTD_GET_BYTES(status) != 0) {
923 				/*
924 				 * If we get here for a control transfer then
925 				 * we need to let the hardware complete the
926 				 * status phase.  That is, we're not done
927 				 * quite yet.
928 				 *
929 				 * Otherwise, we're done.
930 				 */
931 				if (ex->ex_type == EX_CTRL) {
932 					break;
933 				}
934 				goto done;
935 			}
936 		}
937 		DPRINTFN(10, "ex=%#jx std=%#jx still active",
938 		    (uintptr_t)ex, (uintptr_t)ex->ex_sqtdstart, 0, 0);
939 #ifdef EHCI_DEBUG
940 		DPRINTFN(5, "--- still active start ---", 0, 0, 0, 0);
941 		ehci_dump_sqtds(ex->ex_sqtdstart);
942 		DPRINTFN(5, "--- still active end ---", 0, 0, 0, 0);
943 #endif
944 		return;
945 	}
946  done:
947 	DPRINTFN(10, "ex=%#jx done", (uintptr_t)ex, 0, 0, 0);
948 	callout_stop(&ex->ex_xfer.ux_callout);
949 	ehci_idone(ex, cq);
950 }
951 
952 Static void
953 ehci_check_itd_intr(ehci_softc_t *sc, struct ehci_xfer *ex, ex_completeq_t *cq)
954 {
955 	ehci_soft_itd_t *itd;
956 	int i;
957 
958 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
959 
960 	KASSERT(mutex_owned(&sc->sc_lock));
961 
962 	if (&ex->ex_xfer != SIMPLEQ_FIRST(&ex->ex_xfer.ux_pipe->up_queue))
963 		return;
964 
965 	KASSERTMSG(ex->ex_itdstart != NULL && ex->ex_itdend != NULL,
966 	    "xfer %p fitd %p litd %p", ex, ex->ex_itdstart, ex->ex_itdend);
967 
968 	itd = ex->ex_itdend;
969 
970 	/*
971 	 * check no active transfers in last itd, meaning we're finished
972 	 */
973 
974 	usb_syncmem(&itd->dma, itd->offs + offsetof(ehci_itd_t, itd_ctl),
975 	    sizeof(itd->itd.itd_ctl),
976 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
977 
978 	for (i = 0; i < EHCI_ITD_NUFRAMES; i++) {
979 		if (le32toh(itd->itd.itd_ctl[i]) & EHCI_ITD_ACTIVE)
980 			break;
981 	}
982 
983 	if (i == EHCI_ITD_NUFRAMES) {
984 		goto done; /* All 8 descriptors inactive, it's done */
985 	}
986 
987 	usb_syncmem(&itd->dma, itd->offs + offsetof(ehci_itd_t, itd_ctl),
988 	    sizeof(itd->itd.itd_ctl), BUS_DMASYNC_PREREAD);
989 
990 	DPRINTFN(10, "ex %#jx itd %#jx still active",
991 	    (uintptr_t)ex, (uintptr_t)ex->ex_itdstart, 0, 0);
992 	return;
993 done:
994 	DPRINTF("ex %#jx done", (uintptr_t)ex, 0, 0, 0);
995 	callout_stop(&ex->ex_xfer.ux_callout);
996 	ehci_idone(ex, cq);
997 }
998 
999 void
1000 ehci_check_sitd_intr(ehci_softc_t *sc, struct ehci_xfer *ex, ex_completeq_t *cq)
1001 {
1002 	ehci_soft_sitd_t *sitd;
1003 
1004 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
1005 
1006 	KASSERT(mutex_owned(&sc->sc_lock));
1007 
1008 	if (&ex->ex_xfer != SIMPLEQ_FIRST(&ex->ex_xfer.ux_pipe->up_queue))
1009 		return;
1010 
1011 	KASSERTMSG(ex->ex_sitdstart != NULL && ex->ex_sitdend != NULL,
1012 	    "xfer %p fsitd %p lsitd %p", ex, ex->ex_sitdstart, ex->ex_sitdend);
1013 
1014 	sitd = ex->ex_sitdend;
1015 
1016 	/*
1017 	 * check no active transfers in last sitd, meaning we're finished
1018 	 */
1019 
1020 	usb_syncmem(&sitd->dma, sitd->offs + offsetof(ehci_sitd_t, sitd_trans),
1021 	    sizeof(sitd->sitd.sitd_trans),
1022 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
1023 
1024 	bool active = ((le32toh(sitd->sitd.sitd_trans) & EHCI_SITD_ACTIVE) != 0);
1025 
1026 	usb_syncmem(&sitd->dma, sitd->offs + offsetof(ehci_sitd_t, sitd_trans),
1027 	    sizeof(sitd->sitd.sitd_trans), BUS_DMASYNC_PREREAD);
1028 
1029 	if (active)
1030 		return;
1031 
1032 	DPRINTFN(10, "ex=%#jx done", (uintptr_t)ex, 0, 0, 0);
1033 	callout_stop(&(ex->ex_xfer.ux_callout));
1034 	ehci_idone(ex, cq);
1035 }
1036 
1037 
1038 Static void
1039 ehci_idone(struct ehci_xfer *ex, ex_completeq_t *cq)
1040 {
1041 	struct usbd_xfer *xfer = &ex->ex_xfer;
1042 	struct ehci_pipe *epipe = EHCI_XFER2EPIPE(xfer);
1043 	struct ehci_softc *sc = EHCI_XFER2SC(xfer);
1044 	ehci_soft_qtd_t *sqtd, *fsqtd, *lsqtd;
1045 	uint32_t status = 0, nstatus = 0;
1046 	int actlen = 0;
1047 
1048 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
1049 
1050 	KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
1051 
1052 	DPRINTF("ex=%#jx", (uintptr_t)ex, 0, 0, 0);
1053 
1054 	if (xfer->ux_status == USBD_CANCELLED ||
1055 	    xfer->ux_status == USBD_TIMEOUT) {
1056 		DPRINTF("aborted xfer=%#jx", (uintptr_t)xfer, 0, 0, 0);
1057 		return;
1058 	}
1059 
1060 #ifdef DIAGNOSTIC
1061 #ifdef EHCI_DEBUG
1062 	if (ex->ex_isdone) {
1063 		DPRINTFN(5, "--- dump start ---", 0, 0, 0, 0);
1064 		ehci_dump_exfer(ex);
1065 		DPRINTFN(5, "--- dump end ---", 0, 0, 0, 0);
1066 	}
1067 #endif
1068 	KASSERTMSG(!ex->ex_isdone, "xfer %p type %d status %d", xfer,
1069 	    ex->ex_type, xfer->ux_status);
1070 	ex->ex_isdone = true;
1071 #endif
1072 
1073 	DPRINTF("xfer=%#jx, pipe=%#jx ready", (uintptr_t)xfer,
1074 	    (uintptr_t)epipe, 0, 0);
1075 
1076 	/* The transfer is done, compute actual length and status. */
1077 	if (ex->ex_type == EX_ISOC) {
1078 		/* HS isoc transfer */
1079 
1080 		struct ehci_soft_itd *itd;
1081 		int i, nframes, len, uframes;
1082 
1083 		nframes = 0;
1084 
1085 #ifdef EHCI_DEBUG
1086 		DPRINTFN(5, "--- dump start ---", 0, 0, 0, 0);
1087 		ehci_dump_itds(ex->ex_itdstart);
1088 		DPRINTFN(5, "--- dump end ---", 0, 0, 0, 0);
1089 #endif
1090 
1091 		i = xfer->ux_pipe->up_endpoint->ue_edesc->bInterval;
1092 		uframes = min(1 << (i - 1), USB_UFRAMES_PER_FRAME);
1093 
1094 		for (itd = ex->ex_itdstart; itd != NULL; itd = itd->xfer_next) {
1095 			usb_syncmem(&itd->dma,
1096 			    itd->offs + offsetof(ehci_itd_t,itd_ctl),
1097 			    sizeof(itd->itd.itd_ctl),
1098 			    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
1099 
1100 			for (i = 0; i < EHCI_ITD_NUFRAMES; i += uframes) {
1101 				/*
1102 				 * XXX - driver didn't fill in the frame full
1103 				 *   of uframes. This leads to scheduling
1104 				 *   inefficiencies, but working around
1105 				 *   this doubles complexity of tracking
1106 				 *   an xfer.
1107 				 */
1108 				if (nframes >= xfer->ux_nframes)
1109 					break;
1110 
1111 				status = le32toh(itd->itd.itd_ctl[i]);
1112 				len = EHCI_ITD_GET_LEN(status);
1113 				if (EHCI_ITD_GET_STATUS(status) != 0)
1114 					len = 0; /*No valid data on error*/
1115 
1116 				xfer->ux_frlengths[nframes++] = len;
1117 				actlen += len;
1118 			}
1119 			usb_syncmem(&itd->dma,
1120 			    itd->offs + offsetof(ehci_itd_t,itd_ctl),
1121 			    sizeof(itd->itd.itd_ctl), BUS_DMASYNC_PREREAD);
1122 
1123 			if (nframes >= xfer->ux_nframes)
1124 				break;
1125 		}
1126 
1127 		xfer->ux_actlen = actlen;
1128 		xfer->ux_status = USBD_NORMAL_COMPLETION;
1129 		goto end;
1130 	} else if (ex->ex_type == EX_FS_ISOC) {
1131 		/* FS isoc transfer */
1132 		struct ehci_soft_sitd *sitd;
1133 		int nframes, len;
1134 
1135 		nframes = 0;
1136 
1137 		for (sitd = ex->ex_sitdstart; sitd != NULL;
1138 		     sitd = sitd->xfer_next) {
1139 			usb_syncmem(&sitd->dma,
1140 			    sitd->offs + offsetof(ehci_sitd_t, sitd_trans),
1141 			    sizeof(sitd->sitd.sitd_trans),
1142 			    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
1143 
1144 			/*
1145 			 * XXX - driver didn't fill in the frame full
1146 			 *   of uframes. This leads to scheduling
1147 			 *   inefficiencies, but working around
1148 			 *   this doubles complexity of tracking
1149 			 *   an xfer.
1150 			 */
1151 			if (nframes >= xfer->ux_nframes)
1152 				break;
1153 
1154 			status = le32toh(sitd->sitd.sitd_trans);
1155 			usb_syncmem(&sitd->dma,
1156 			    sitd->offs + offsetof(ehci_sitd_t, sitd_trans),
1157 			    sizeof(sitd->sitd.sitd_trans), BUS_DMASYNC_PREREAD);
1158 
1159 			len = EHCI_SITD_GET_LEN(status);
1160 			if (status & (EHCI_SITD_ERR|EHCI_SITD_BUFERR|
1161 			    EHCI_SITD_BABBLE|EHCI_SITD_XACTERR|EHCI_SITD_MISS)) {
1162 				/* No valid data on error */
1163 				len = xfer->ux_frlengths[nframes];
1164 			}
1165 
1166 			/*
1167 			 * frlengths[i]: # of bytes to send
1168 			 * len: # of bytes host didn't send
1169 			 */
1170 			xfer->ux_frlengths[nframes] -= len;
1171 			/* frlengths[i]: # of bytes host sent */
1172 			actlen += xfer->ux_frlengths[nframes++];
1173 
1174 			if (nframes >= xfer->ux_nframes)
1175 				break;
1176 	    	}
1177 
1178 		xfer->ux_actlen = actlen;
1179 		xfer->ux_status = USBD_NORMAL_COMPLETION;
1180 		goto end;
1181 	}
1182 	KASSERT(ex->ex_type == EX_CTRL || ex->ex_type == EX_INTR ||
1183 	   ex->ex_type == EX_BULK);
1184 
1185 	/* Continue processing xfers using queue heads */
1186 	if (ex->ex_type == EX_CTRL) {
1187 		fsqtd = ex->ex_setup;
1188 		lsqtd = ex->ex_status;
1189 	} else {
1190 		fsqtd = ex->ex_sqtdstart;
1191 		lsqtd = ex->ex_sqtdend;
1192 	}
1193 #ifdef EHCI_DEBUG
1194 	DPRINTFN(5, "--- dump start ---", 0, 0, 0, 0);
1195 	ehci_dump_sqtds(fsqtd);
1196 	DPRINTFN(5, "--- dump end ---", 0, 0, 0, 0);
1197 #endif
1198 
1199 	for (sqtd = fsqtd; sqtd != lsqtd->nextqtd; sqtd = sqtd->nextqtd) {
1200 		usb_syncmem(&sqtd->dma, sqtd->offs, sizeof(sqtd->qtd),
1201 		    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
1202 		nstatus = le32toh(sqtd->qtd.qtd_status);
1203 		usb_syncmem(&sqtd->dma, sqtd->offs, sizeof(sqtd->qtd),
1204 		    BUS_DMASYNC_PREREAD);
1205 		if (nstatus & EHCI_QTD_ACTIVE)
1206 			break;
1207 
1208 		status = nstatus;
1209 		if (EHCI_QTD_GET_PID(status) != EHCI_QTD_PID_SETUP)
1210 			actlen += sqtd->len - EHCI_QTD_GET_BYTES(status);
1211 	}
1212 
1213 	/*
1214 	 * If there are left over TDs we need to update the toggle.
1215 	 * The default pipe doesn't need it since control transfers
1216 	 * start the toggle at 0 every time.
1217 	 * For a short transfer we need to update the toggle for the missing
1218 	 * packets within the qTD.
1219 	 */
1220 	if ((sqtd != lsqtd->nextqtd || EHCI_QTD_GET_BYTES(status)) &&
1221 	    xfer->ux_pipe->up_dev->ud_pipe0 != xfer->ux_pipe) {
1222 		DPRINTF("toggle update status=0x%08jx nstatus=0x%08jx",
1223 		    status, nstatus, 0, 0);
1224 #if 0
1225 		ehci_dump_sqh(epipe->sqh);
1226 		ehci_dump_sqtds(ex->ex_sqtdstart);
1227 #endif
1228 		epipe->nexttoggle = EHCI_QTD_GET_TOGGLE(nstatus);
1229 	}
1230 
1231 	DPRINTF("len=%jd actlen=%jd status=0x%08jx", xfer->ux_length, actlen,
1232 	    status, 0);
1233 	xfer->ux_actlen = actlen;
1234 	if (status & EHCI_QTD_HALTED) {
1235 #ifdef EHCI_DEBUG
1236 		DPRINTF("halted addr=%jd endpt=0x%02jx",
1237 		    xfer->ux_pipe->up_dev->ud_addr,
1238 		    xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress,
1239 		    0, 0);
1240 		DPRINTF("cerr=%jd pid=%jd",
1241 		    EHCI_QTD_GET_CERR(status), EHCI_QTD_GET_PID(status),
1242 		    0, 0);
1243 		DPRINTF("active =%jd halted=%jd buferr=%jd babble=%jd",
1244 		    status & EHCI_QTD_ACTIVE ? 1 : 0,
1245 		    status & EHCI_QTD_HALTED ? 1 : 0,
1246 		    status & EHCI_QTD_BUFERR ? 1 : 0,
1247 		    status & EHCI_QTD_BABBLE ? 1 : 0);
1248 
1249 		DPRINTF("xacterr=%jd missed=%jd split =%jd ping  =%jd",
1250 		    status & EHCI_QTD_XACTERR ? 1 : 0,
1251 		    status & EHCI_QTD_MISSEDMICRO ? 1 : 0,
1252 		    status & EHCI_QTD_SPLITXSTATE ? 1 : 0,
1253 		    status & EHCI_QTD_PINGSTATE ? 1 : 0);
1254 
1255 		DPRINTFN(5, "--- dump start ---", 0, 0, 0, 0);
1256 		ehci_dump_sqh(epipe->sqh);
1257 		ehci_dump_sqtds(ex->ex_sqtdstart);
1258 		DPRINTFN(5, "--- dump end ---", 0, 0, 0, 0);
1259 #endif
1260 		/* low&full speed has an extra error flag */
1261 		if (EHCI_QH_GET_EPS(epipe->sqh->qh.qh_endp) !=
1262 		    EHCI_QH_SPEED_HIGH)
1263 			status &= EHCI_QTD_STATERRS | EHCI_QTD_PINGSTATE;
1264 		else
1265 			status &= EHCI_QTD_STATERRS;
1266 		if (status == 0) /* no other errors means a stall */ {
1267 			xfer->ux_status = USBD_STALLED;
1268 		} else {
1269 			xfer->ux_status = USBD_IOERROR; /* more info XXX */
1270 		}
1271 		/* XXX need to reset TT on missed microframe */
1272 		if (status & EHCI_QTD_MISSEDMICRO) {
1273 			printf("%s: missed microframe, TT reset not "
1274 			    "implemented, hub might be inoperational\n",
1275 			    device_xname(sc->sc_dev));
1276 		}
1277 	} else {
1278 		xfer->ux_status = USBD_NORMAL_COMPLETION;
1279 	}
1280 
1281     end:
1282 
1283 	ehci_del_intr_list(sc, ex);
1284 	TAILQ_INSERT_TAIL(cq, ex, ex_next);
1285 
1286 	DPRINTF("ex=%#jx done", (uintptr_t)ex, 0, 0, 0);
1287 }
1288 
1289 Static void
1290 ehci_poll(struct usbd_bus *bus)
1291 {
1292 	ehci_softc_t *sc = EHCI_BUS2SC(bus);
1293 
1294 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
1295 
1296 #ifdef EHCI_DEBUG
1297 	static int last;
1298 	int new;
1299 	new = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
1300 	if (new != last) {
1301 		DPRINTF("intrs=0x%04jx", new, 0, 0, 0);
1302 		last = new;
1303 	}
1304 #endif
1305 
1306 	if (EOREAD4(sc, EHCI_USBSTS) & sc->sc_eintrs) {
1307 		mutex_spin_enter(&sc->sc_intr_lock);
1308 		ehci_intr1(sc);
1309 		mutex_spin_exit(&sc->sc_intr_lock);
1310 	}
1311 }
1312 
1313 void
1314 ehci_childdet(device_t self, device_t child)
1315 {
1316 	struct ehci_softc *sc = device_private(self);
1317 
1318 	KASSERT(sc->sc_child == child);
1319 	sc->sc_child = NULL;
1320 }
1321 
1322 int
1323 ehci_detach(struct ehci_softc *sc, int flags)
1324 {
1325 	int rv = 0;
1326 
1327 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
1328 
1329 	if (sc->sc_child != NULL)
1330 		rv = config_detach(sc->sc_child, flags);
1331 
1332 	if (rv != 0)
1333 		return rv;
1334 
1335 	callout_halt(&sc->sc_tmo_intrlist, NULL);
1336 	callout_destroy(&sc->sc_tmo_intrlist);
1337 
1338 	/* XXX free other data structures XXX */
1339 	if (sc->sc_softitds)
1340 		kmem_free(sc->sc_softitds,
1341 		    sc->sc_flsize * sizeof(ehci_soft_itd_t *));
1342 	cv_destroy(&sc->sc_doorbell);
1343 	cv_destroy(&sc->sc_softwake_cv);
1344 
1345 #if 0
1346 	/* XXX destroyed in ehci_pci.c as it controls ehci_intr access */
1347 
1348 	softint_disestablish(sc->sc_doorbell_si);
1349 	softint_disestablish(sc->sc_pcd_si);
1350 
1351 	mutex_destroy(&sc->sc_lock);
1352 	mutex_destroy(&sc->sc_intr_lock);
1353 #endif
1354 
1355 	pool_cache_destroy(sc->sc_xferpool);
1356 
1357 	EOWRITE4(sc, EHCI_CONFIGFLAG, 0);
1358 
1359 	return rv;
1360 }
1361 
1362 
1363 int
1364 ehci_activate(device_t self, enum devact act)
1365 {
1366 	struct ehci_softc *sc = device_private(self);
1367 
1368 	switch (act) {
1369 	case DVACT_DEACTIVATE:
1370 		sc->sc_dying = 1;
1371 		return 0;
1372 	default:
1373 		return EOPNOTSUPP;
1374 	}
1375 }
1376 
1377 /*
1378  * Handle suspend/resume.
1379  *
1380  * We need to switch to polling mode here, because this routine is
1381  * called from an interrupt context.  This is all right since we
1382  * are almost suspended anyway.
1383  *
1384  * Note that this power handler isn't to be registered directly; the
1385  * bus glue needs to call out to it.
1386  */
1387 bool
1388 ehci_suspend(device_t dv, const pmf_qual_t *qual)
1389 {
1390 	ehci_softc_t *sc = device_private(dv);
1391 	int i;
1392 	uint32_t cmd, hcr;
1393 
1394 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
1395 
1396 	mutex_spin_enter(&sc->sc_intr_lock);
1397 	sc->sc_bus.ub_usepolling++;
1398 	mutex_spin_exit(&sc->sc_intr_lock);
1399 
1400 	for (i = 1; i <= sc->sc_noport; i++) {
1401 		cmd = EOREAD4(sc, EHCI_PORTSC(i)) & ~EHCI_PS_CLEAR;
1402 		if ((cmd & EHCI_PS_PO) == 0 && (cmd & EHCI_PS_PE) == EHCI_PS_PE)
1403 			EOWRITE4(sc, EHCI_PORTSC(i), cmd | EHCI_PS_SUSP);
1404 	}
1405 
1406 	sc->sc_cmd = EOREAD4(sc, EHCI_USBCMD);
1407 
1408 	cmd = sc->sc_cmd & ~(EHCI_CMD_ASE | EHCI_CMD_PSE);
1409 	EOWRITE4(sc, EHCI_USBCMD, cmd);
1410 
1411 	for (i = 0; i < 100; i++) {
1412 		hcr = EOREAD4(sc, EHCI_USBSTS) & (EHCI_STS_ASS | EHCI_STS_PSS);
1413 		if (hcr == 0)
1414 			break;
1415 
1416 		usb_delay_ms(&sc->sc_bus, 1);
1417 	}
1418 	if (hcr != 0)
1419 		printf("%s: reset timeout\n", device_xname(dv));
1420 
1421 	cmd &= ~EHCI_CMD_RS;
1422 	EOWRITE4(sc, EHCI_USBCMD, cmd);
1423 
1424 	for (i = 0; i < 100; i++) {
1425 		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
1426 		if (hcr == EHCI_STS_HCH)
1427 			break;
1428 
1429 		usb_delay_ms(&sc->sc_bus, 1);
1430 	}
1431 	if (hcr != EHCI_STS_HCH)
1432 		printf("%s: config timeout\n", device_xname(dv));
1433 
1434 	mutex_spin_enter(&sc->sc_intr_lock);
1435 	sc->sc_bus.ub_usepolling--;
1436 	mutex_spin_exit(&sc->sc_intr_lock);
1437 
1438 	return true;
1439 }
1440 
1441 bool
1442 ehci_resume(device_t dv, const pmf_qual_t *qual)
1443 {
1444 	ehci_softc_t *sc = device_private(dv);
1445 	int i;
1446 	uint32_t cmd, hcr;
1447 
1448 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
1449 
1450 	/* restore things in case the bios sucks */
1451 	EOWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
1452 	EOWRITE4(sc, EHCI_PERIODICLISTBASE, DMAADDR(&sc->sc_fldma, 0));
1453 	EOWRITE4(sc, EHCI_ASYNCLISTADDR,
1454 	    sc->sc_async_head->physaddr | EHCI_LINK_QH);
1455 
1456 	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs & ~EHCI_INTR_PCIE);
1457 
1458 	EOWRITE4(sc, EHCI_USBCMD, sc->sc_cmd);
1459 
1460 	hcr = 0;
1461 	for (i = 1; i <= sc->sc_noport; i++) {
1462 		cmd = EOREAD4(sc, EHCI_PORTSC(i)) & ~EHCI_PS_CLEAR;
1463 		if ((cmd & EHCI_PS_PO) == 0 &&
1464 		    (cmd & EHCI_PS_SUSP) == EHCI_PS_SUSP) {
1465 			EOWRITE4(sc, EHCI_PORTSC(i), cmd | EHCI_PS_FPR);
1466 			hcr = 1;
1467 		}
1468 	}
1469 
1470 	if (hcr) {
1471 		usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
1472 
1473 		for (i = 1; i <= sc->sc_noport; i++) {
1474 			cmd = EOREAD4(sc, EHCI_PORTSC(i)) & ~EHCI_PS_CLEAR;
1475 			if ((cmd & EHCI_PS_PO) == 0 &&
1476 			    (cmd & EHCI_PS_SUSP) == EHCI_PS_SUSP)
1477 				EOWRITE4(sc, EHCI_PORTSC(i),
1478 				    cmd & ~EHCI_PS_FPR);
1479 		}
1480 	}
1481 
1482 	EOWRITE4(sc, EHCI_USBCMD, sc->sc_cmd);
1483 	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
1484 
1485 	for (i = 0; i < 100; i++) {
1486 		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
1487 		if (hcr != EHCI_STS_HCH)
1488 			break;
1489 
1490 		usb_delay_ms(&sc->sc_bus, 1);
1491 	}
1492 	if (hcr == EHCI_STS_HCH)
1493 		printf("%s: config timeout\n", device_xname(dv));
1494 
1495 	return true;
1496 }
1497 
1498 /*
1499  * Shut down the controller when the system is going down.
1500  */
1501 bool
1502 ehci_shutdown(device_t self, int flags)
1503 {
1504 	ehci_softc_t *sc = device_private(self);
1505 
1506 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
1507 
1508 	EOWRITE4(sc, EHCI_USBCMD, 0);	/* Halt controller */
1509 	EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
1510 	return true;
1511 }
1512 
1513 Static struct usbd_xfer *
1514 ehci_allocx(struct usbd_bus *bus, unsigned int nframes)
1515 {
1516 	struct ehci_softc *sc = EHCI_BUS2SC(bus);
1517 	struct usbd_xfer *xfer;
1518 
1519 	xfer = pool_cache_get(sc->sc_xferpool, PR_WAITOK);
1520 	if (xfer != NULL) {
1521 		memset(xfer, 0, sizeof(struct ehci_xfer));
1522 #ifdef DIAGNOSTIC
1523 		struct ehci_xfer *ex = EHCI_XFER2EXFER(xfer);
1524 		ex->ex_isdone = true;
1525 		xfer->ux_state = XFER_BUSY;
1526 #endif
1527 	}
1528 	return xfer;
1529 }
1530 
1531 Static void
1532 ehci_freex(struct usbd_bus *bus, struct usbd_xfer *xfer)
1533 {
1534 	struct ehci_softc *sc = EHCI_BUS2SC(bus);
1535 	struct ehci_xfer *ex __diagused = EHCI_XFER2EXFER(xfer);
1536 
1537 	KASSERTMSG(xfer->ux_state == XFER_BUSY, "xfer %p state %d\n", xfer,
1538 	    xfer->ux_state);
1539 	KASSERT(ex->ex_isdone);
1540 
1541 #ifdef DIAGNOSTIC
1542 	xfer->ux_state = XFER_FREE;
1543 #endif
1544 
1545 	pool_cache_put(sc->sc_xferpool, xfer);
1546 }
1547 
1548 Static void
1549 ehci_get_lock(struct usbd_bus *bus, kmutex_t **lock)
1550 {
1551 	struct ehci_softc *sc = EHCI_BUS2SC(bus);
1552 
1553 	*lock = &sc->sc_lock;
1554 }
1555 
1556 Static void
1557 ehci_device_clear_toggle(struct usbd_pipe *pipe)
1558 {
1559 	struct ehci_pipe *epipe = EHCI_PIPE2EPIPE(pipe);
1560 
1561 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
1562 
1563 	DPRINTF("epipe=%#jx status=0x%08jx", (uintptr_t)epipe,
1564 	    epipe->sqh->qh.qh_qtd.qtd_status, 0, 0);
1565 #ifdef EHCI_DEBUG
1566 	if (ehcidebug)
1567 		usbd_dump_pipe(pipe);
1568 #endif
1569 	epipe->nexttoggle = 0;
1570 }
1571 
1572 Static void
1573 ehci_noop(struct usbd_pipe *pipe)
1574 {
1575 }
1576 
1577 #ifdef EHCI_DEBUG
1578 /*
1579  * Unused function - this is meant to be called from a kernel
1580  * debugger.
1581  */
1582 void
1583 ehci_dump(void)
1584 {
1585 	ehci_softc_t *sc = theehci;
1586 	int i;
1587 	printf("cmd=0x%08x, sts=0x%08x, ien=0x%08x\n",
1588 	    EOREAD4(sc, EHCI_USBCMD),
1589 	    EOREAD4(sc, EHCI_USBSTS),
1590 	    EOREAD4(sc, EHCI_USBINTR));
1591 	printf("frindex=0x%08x ctrdsegm=0x%08x periodic=0x%08x async=0x%08x\n",
1592 	    EOREAD4(sc, EHCI_FRINDEX),
1593 	    EOREAD4(sc, EHCI_CTRLDSSEGMENT),
1594 	    EOREAD4(sc, EHCI_PERIODICLISTBASE),
1595 	    EOREAD4(sc, EHCI_ASYNCLISTADDR));
1596 	for (i = 1; i <= sc->sc_noport; i++)
1597 		printf("port %d status=0x%08x\n", i,
1598 		    EOREAD4(sc, EHCI_PORTSC(i)));
1599 }
1600 
1601 Static void
1602 ehci_dump_regs(ehci_softc_t *sc)
1603 {
1604 	int i;
1605 
1606 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
1607 
1608 	DPRINTF("cmd     = 0x%08jx  sts      = 0x%08jx  ien      = 0x%08jx",
1609 	    EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS),
1610 	    EOREAD4(sc, EHCI_USBINTR), 0);
1611 	DPRINTF("frindex = 0x%08jx  ctrdsegm = 0x%08jx  periodic = 0x%08jx  "
1612 	    "async   = 0x%08jx",
1613 	    EOREAD4(sc, EHCI_FRINDEX), EOREAD4(sc, EHCI_CTRLDSSEGMENT),
1614 	    EOREAD4(sc, EHCI_PERIODICLISTBASE),
1615 	    EOREAD4(sc, EHCI_ASYNCLISTADDR));
1616 	for (i = 1; i <= sc->sc_noport; i += 2) {
1617 		if (i == sc->sc_noport) {
1618 			DPRINTF("port %jd status = 0x%08jx", i,
1619 			    EOREAD4(sc, EHCI_PORTSC(i)), 0, 0);
1620 		} else {
1621 			DPRINTF("port %jd status = 0x%08jx  port %jd "
1622 			    "status = 0x%08jx",
1623 			    i, EOREAD4(sc, EHCI_PORTSC(i)),
1624 			    i+1, EOREAD4(sc, EHCI_PORTSC(i+1)));
1625 		}
1626 	}
1627 }
1628 
1629 #define ehci_dump_link(link, type) do {					\
1630 	DPRINTF("    link 0x%08jx (T = %jd):",				\
1631 	    link,							\
1632 	    link & EHCI_LINK_TERMINATE ? 1 : 0, 0, 0);			\
1633 	if (type) {							\
1634 		DPRINTF(						\
1635 		    "        ITD  = %jd  QH   = %jd  SITD = %jd  FSTN = %jd",\
1636 		    EHCI_LINK_TYPE(link) == EHCI_LINK_ITD ? 1 : 0,	\
1637 		    EHCI_LINK_TYPE(link) == EHCI_LINK_QH ? 1 : 0,	\
1638 		    EHCI_LINK_TYPE(link) == EHCI_LINK_SITD ? 1 : 0,	\
1639 		    EHCI_LINK_TYPE(link) == EHCI_LINK_FSTN ? 1 : 0);	\
1640 	}								\
1641 } while(0)
1642 
1643 Static void
1644 ehci_dump_sqtds(ehci_soft_qtd_t *sqtd)
1645 {
1646 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
1647 	int i;
1648 	uint32_t stop = 0;
1649 
1650 	for (i = 0; sqtd && i < 20 && !stop; sqtd = sqtd->nextqtd, i++) {
1651 		ehci_dump_sqtd(sqtd);
1652 		usb_syncmem(&sqtd->dma,
1653 		    sqtd->offs + offsetof(ehci_qtd_t, qtd_next),
1654 		    sizeof(sqtd->qtd),
1655 		    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
1656 		stop = sqtd->qtd.qtd_next & htole32(EHCI_LINK_TERMINATE);
1657 		usb_syncmem(&sqtd->dma,
1658 		    sqtd->offs + offsetof(ehci_qtd_t, qtd_next),
1659 		    sizeof(sqtd->qtd), BUS_DMASYNC_PREREAD);
1660 	}
1661 	if (!stop)
1662 		DPRINTF("dump aborted, too many TDs", 0, 0, 0, 0);
1663 }
1664 
1665 Static void
1666 ehci_dump_sqtd(ehci_soft_qtd_t *sqtd)
1667 {
1668 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
1669 
1670 	usb_syncmem(&sqtd->dma, sqtd->offs,
1671 	    sizeof(sqtd->qtd), BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
1672 
1673 	DPRINTFN(10, "QTD(%#jx) at 0x%08jx:", (uintptr_t)sqtd, sqtd->physaddr,
1674 	    0, 0);
1675 	ehci_dump_qtd(&sqtd->qtd);
1676 
1677 	usb_syncmem(&sqtd->dma, sqtd->offs,
1678 	    sizeof(sqtd->qtd), BUS_DMASYNC_PREREAD);
1679 }
1680 
1681 Static void
1682 ehci_dump_qtd(ehci_qtd_t *qtd)
1683 {
1684 	EHCIHIST_FUNC();	EHCIHIST_CALLED();
1685 	uint32_t s = le32toh(qtd->qtd_status);
1686 
1687 	DPRINTFN(10,
1688 	    "     next = 0x%08jx  altnext = 0x%08jx  status = 0x%08jx",
1689 	    qtd->qtd_next, qtd->qtd_altnext, s, 0);
1690 	DPRINTFN(10,
1691 	    "   toggle = %jd ioc = %jd bytes = %#jx c_page = %#jx",
1692 	    EHCI_QTD_GET_TOGGLE(s), EHCI_QTD_GET_IOC(s),
1693 	    EHCI_QTD_GET_BYTES(s), EHCI_QTD_GET_C_PAGE(s));
1694 	DPRINTFN(10,
1695 	    "     cerr = %jd pid = %jd stat  = %jx",
1696 	    EHCI_QTD_GET_CERR(s), EHCI_QTD_GET_PID(s), EHCI_QTD_GET_STATUS(s),
1697 	    0);
1698 	DPRINTFN(10,
1699 	    "active =%jd halted=%jd buferr=%jd babble=%jd",
1700 	    s & EHCI_QTD_ACTIVE ? 1 : 0,
1701 	    s & EHCI_QTD_HALTED ? 1 : 0,
1702 	    s & EHCI_QTD_BUFERR ? 1 : 0,
1703 	    s & EHCI_QTD_BABBLE ? 1 : 0);
1704 	DPRINTFN(10,
1705 	    "xacterr=%jd missed=%jd split =%jd ping  =%jd",
1706 	    s & EHCI_QTD_XACTERR ? 1 : 0,
1707 	    s & EHCI_QTD_MISSEDMICRO ? 1 : 0,
1708 	    s & EHCI_QTD_SPLITXSTATE ? 1 : 0,
1709 	    s & EHCI_QTD_PINGSTATE ? 1 : 0);
1710 	DPRINTFN(10,
1711 	    "buffer[0] = %#jx  buffer[1] = %#jx  "
1712 	    "buffer[2] = %#jx  buffer[3] = %#jx",
1713 	    le32toh(qtd->qtd_buffer[0]), le32toh(qtd->qtd_buffer[1]),
1714 	    le32toh(qtd->qtd_buffer[2]), le32toh(qtd->qtd_buffer[3]));
1715 	DPRINTFN(10,
1716 	    "buffer[4] = %#jx", le32toh(qtd->qtd_buffer[4]), 0, 0, 0);
1717 }
1718 
1719 Static void
1720 ehci_dump_sqh(ehci_soft_qh_t *sqh)
1721 {
1722 	ehci_qh_t *qh = &sqh->qh;
1723 	ehci_link_t link;
1724 	uint32_t endp, endphub;
1725 	EHCIHIST_FUNC();	EHCIHIST_CALLED();
1726 
1727 	usb_syncmem(&sqh->dma, sqh->offs,
1728 	    sizeof(sqh->qh), BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
1729 
1730 	DPRINTFN(10, "QH(%#jx) at %#jx:", (uintptr_t)sqh, sqh->physaddr, 0, 0);
1731 	link = le32toh(qh->qh_link);
1732 	ehci_dump_link(link, true);
1733 
1734 	endp = le32toh(qh->qh_endp);
1735 	DPRINTFN(10, "    endp = %#jx", endp, 0, 0, 0);
1736 	DPRINTFN(10, "        addr = 0x%02jx  inact = %jd  endpt = %jd  "
1737 	    "eps = %jd",
1738 	    EHCI_QH_GET_ADDR(endp), EHCI_QH_GET_INACT(endp),
1739 	    EHCI_QH_GET_ENDPT(endp), EHCI_QH_GET_EPS(endp));
1740 	DPRINTFN(10, "        dtc  = %jd     hrecl = %jd",
1741 	    EHCI_QH_GET_DTC(endp), EHCI_QH_GET_HRECL(endp), 0, 0);
1742 	DPRINTFN(10, "        ctl  = %jd     nrl   = %jd  mpl   = %#jx(%jd)",
1743 	    EHCI_QH_GET_CTL(endp),EHCI_QH_GET_NRL(endp),
1744 	    EHCI_QH_GET_MPL(endp), EHCI_QH_GET_MPL(endp));
1745 
1746 	endphub = le32toh(qh->qh_endphub);
1747 	DPRINTFN(10, " endphub = %#jx", endphub, 0, 0, 0);
1748 	DPRINTFN(10, "      smask = 0x%02jx  cmask = 0x%02jx one %jx",
1749 	    EHCI_QH_GET_SMASK(endphub), EHCI_QH_GET_CMASK(endphub), 1, 0);
1750 	DPRINTFN(10, "      huba  = 0x%02jx  port  = %jd  mult = %jd",
1751 	    EHCI_QH_GET_HUBA(endphub), EHCI_QH_GET_PORT(endphub),
1752 	    EHCI_QH_GET_MULT(endphub), 0);
1753 
1754 	link = le32toh(qh->qh_curqtd);
1755 	ehci_dump_link(link, false);
1756 	DPRINTFN(10, "Overlay qTD:", 0, 0, 0, 0);
1757 	ehci_dump_qtd(&qh->qh_qtd);
1758 
1759 	usb_syncmem(&sqh->dma, sqh->offs, sizeof(sqh->qh),
1760 	    BUS_DMASYNC_PREREAD);
1761 }
1762 
1763 Static void
1764 ehci_dump_itds(ehci_soft_itd_t *itd)
1765 {
1766 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
1767 	int i;
1768 	uint32_t stop = 0;
1769 
1770 	for (i = 0; itd && i < 20 && !stop; itd = itd->xfer_next, i++) {
1771 		ehci_dump_itd(itd);
1772 		usb_syncmem(&itd->dma,
1773 		    itd->offs + offsetof(ehci_itd_t, itd_next),
1774 		    sizeof(itd->itd),
1775 		    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
1776 		stop = itd->itd.itd_next & htole32(EHCI_LINK_TERMINATE);
1777 		usb_syncmem(&itd->dma,
1778 		    itd->offs + offsetof(ehci_itd_t, itd_next),
1779 		    sizeof(itd->itd), BUS_DMASYNC_PREREAD);
1780 	}
1781 	if (!stop)
1782 		DPRINTF("dump aborted, too many TDs", 0, 0, 0, 0);
1783 }
1784 
1785 Static void
1786 ehci_dump_itd(struct ehci_soft_itd *itd)
1787 {
1788 	ehci_isoc_trans_t t;
1789 	ehci_isoc_bufr_ptr_t b, b2, b3;
1790 	int i;
1791 
1792 	EHCIHIST_FUNC();	EHCIHIST_CALLED();
1793 
1794 	DPRINTF("ITD: next phys = %#jx", itd->itd.itd_next, 0, 0, 0);
1795 
1796 	for (i = 0; i < EHCI_ITD_NUFRAMES; i++) {
1797 		t = le32toh(itd->itd.itd_ctl[i]);
1798 		DPRINTF("ITDctl %jd: stat = %jx len = %jx",
1799 		    i, EHCI_ITD_GET_STATUS(t), EHCI_ITD_GET_LEN(t), 0);
1800 		DPRINTF("     ioc = %jx pg = %jx offs = %jx",
1801 		    EHCI_ITD_GET_IOC(t), EHCI_ITD_GET_PG(t),
1802 		    EHCI_ITD_GET_OFFS(t), 0);
1803 	}
1804 	DPRINTF("ITDbufr: ", 0, 0, 0, 0);
1805 	for (i = 0; i < EHCI_ITD_NBUFFERS; i++)
1806 		DPRINTF("      %jx",
1807 		    EHCI_ITD_GET_BPTR(le32toh(itd->itd.itd_bufr[i])), 0, 0, 0);
1808 
1809 	b = le32toh(itd->itd.itd_bufr[0]);
1810 	b2 = le32toh(itd->itd.itd_bufr[1]);
1811 	b3 = le32toh(itd->itd.itd_bufr[2]);
1812 	DPRINTF("     ep = %jx daddr = %jx dir = %jd",
1813 	    EHCI_ITD_GET_EP(b), EHCI_ITD_GET_DADDR(b), EHCI_ITD_GET_DIR(b2), 0);
1814 	DPRINTF("     maxpkt = %jx multi = %jx",
1815 	    EHCI_ITD_GET_MAXPKT(b2), EHCI_ITD_GET_MULTI(b3), 0, 0);
1816 }
1817 
1818 Static void
1819 ehci_dump_sitd(struct ehci_soft_itd *itd)
1820 {
1821 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
1822 
1823 	DPRINTF("SITD %#jx next = %p prev = %#jx",
1824 	    (uintptr_t)itd, (uintptr_t)itd->frame_list.next,
1825 	    (uintptr_t)itd->frame_list.prev, 0);
1826 	DPRINTF("        xfernext=%#jx physaddr=%jX slot=%jd",
1827 	    (uintptr_t)itd->xfer_next, itd->physaddr, itd->slot, 0);
1828 }
1829 
1830 Static void
1831 ehci_dump_exfer(struct ehci_xfer *ex)
1832 {
1833 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
1834 
1835 	DPRINTF("ex = %#jx type %jd isdone %jd", (uintptr_t)ex, ex->ex_type,
1836 	    ex->ex_isdone, 0);
1837 
1838 	switch (ex->ex_type) {
1839 	case EX_CTRL:
1840 		DPRINTF("   setup = %#jx data = %#jx status = %#jx",
1841 		    (uintptr_t)ex->ex_setup, (uintptr_t)ex->ex_data,
1842 		    (uintptr_t)ex->ex_status, 0);
1843 		break;
1844 	case EX_BULK:
1845 	case EX_INTR:
1846 		DPRINTF("   qtdstart = %#jx qtdend = %#jx",
1847 		    (uintptr_t)ex->ex_sqtdstart, (uintptr_t)ex->ex_sqtdend,
1848 		    0, 0);
1849 		break;
1850 	case EX_ISOC:
1851 		DPRINTF("   itdstart = %#jx itdend = %#jx",
1852 		    (uintptr_t)ex->ex_itdstart, (uintptr_t)ex->ex_itdend, 0, 0);
1853 		break;
1854 	case EX_FS_ISOC:
1855 		DPRINTF("   sitdstart = %#jx sitdend = %#jx",
1856 		    (uintptr_t)ex->ex_sitdstart, (uintptr_t)ex->ex_sitdend,
1857 		    0, 0);
1858 		break;
1859 	default:
1860 		DPRINTF("   unknown type", 0, 0, 0, 0);
1861 	}
1862 }
1863 #endif
1864 
1865 Static usbd_status
1866 ehci_open(struct usbd_pipe *pipe)
1867 {
1868 	struct usbd_device *dev = pipe->up_dev;
1869 	ehci_softc_t *sc = EHCI_PIPE2SC(pipe);
1870 	usb_endpoint_descriptor_t *ed = pipe->up_endpoint->ue_edesc;
1871 	uint8_t rhaddr = dev->ud_bus->ub_rhaddr;
1872 	uint8_t addr = dev->ud_addr;
1873 	uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
1874 	struct ehci_pipe *epipe = EHCI_PIPE2EPIPE(pipe);
1875 	ehci_soft_qh_t *sqh;
1876 	usbd_status err;
1877 	int ival, speed, naks;
1878 	int hshubaddr, hshubport;
1879 
1880 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
1881 
1882 	DPRINTF("pipe=%#jx, addr=%jd, endpt=%jd (%jd)", (uintptr_t)pipe, addr,
1883 	    ed->bEndpointAddress, rhaddr);
1884 
1885 	if (dev->ud_myhsport) {
1886 		/*
1887 		 * When directly attached FS/LS device while doing embedded
1888 		 * transaction translations and we are the hub, set the hub
1889 		 * address to 0 (us).
1890 		 */
1891 		if (!(sc->sc_flags & EHCIF_ETTF)
1892 		    || (dev->ud_myhsport->up_parent->ud_addr != rhaddr)) {
1893 			hshubaddr = dev->ud_myhsport->up_parent->ud_addr;
1894 		} else {
1895 			hshubaddr = 0;
1896 		}
1897 		hshubport = dev->ud_myhsport->up_portno;
1898 	} else {
1899 		hshubaddr = 0;
1900 		hshubport = 0;
1901 	}
1902 
1903 	if (sc->sc_dying)
1904 		return USBD_IOERROR;
1905 
1906 	/* toggle state needed for bulk endpoints */
1907 	epipe->nexttoggle = pipe->up_endpoint->ue_toggle;
1908 
1909 	if (addr == rhaddr) {
1910 		switch (ed->bEndpointAddress) {
1911 		case USB_CONTROL_ENDPOINT:
1912 			pipe->up_methods = &roothub_ctrl_methods;
1913 			break;
1914 		case UE_DIR_IN | USBROOTHUB_INTR_ENDPT:
1915 			pipe->up_methods = &ehci_root_intr_methods;
1916 			break;
1917 		default:
1918 			DPRINTF("bad bEndpointAddress 0x%02jx",
1919 			    ed->bEndpointAddress, 0, 0, 0);
1920 			return USBD_INVAL;
1921 		}
1922 		return USBD_NORMAL_COMPLETION;
1923 	}
1924 
1925 	/* XXX All this stuff is only valid for async. */
1926 	switch (dev->ud_speed) {
1927 	case USB_SPEED_LOW:  speed = EHCI_QH_SPEED_LOW;  break;
1928 	case USB_SPEED_FULL: speed = EHCI_QH_SPEED_FULL; break;
1929 	case USB_SPEED_HIGH: speed = EHCI_QH_SPEED_HIGH; break;
1930 	default: panic("ehci_open: bad device speed %d", dev->ud_speed);
1931 	}
1932 	if (speed == EHCI_QH_SPEED_LOW && xfertype == UE_ISOCHRONOUS) {
1933 		DPRINTF("hshubaddr=%jd hshubport=%jd", hshubaddr, hshubport, 0,
1934 		    0);
1935 		return USBD_INVAL;
1936 	}
1937 
1938 	/*
1939 	 * For interrupt transfer, nak throttling must be disabled, but for
1940 	 * the other transfer type, nak throttling should be enabled from the
1941 	 * viewpoint that avoids the memory thrashing.
1942 	 */
1943 	naks = (xfertype == UE_INTERRUPT) ? 0
1944 	    : ((speed == EHCI_QH_SPEED_HIGH) ? 4 : 0);
1945 
1946 	/* Allocate sqh for everything, save isoc xfers */
1947 	if (xfertype != UE_ISOCHRONOUS) {
1948 		sqh = ehci_alloc_sqh(sc);
1949 		if (sqh == NULL)
1950 			return USBD_NOMEM;
1951 		/* qh_link filled when the QH is added */
1952 		sqh->qh.qh_endp = htole32(
1953 		    EHCI_QH_SET_ADDR(addr) |
1954 		    EHCI_QH_SET_ENDPT(UE_GET_ADDR(ed->bEndpointAddress)) |
1955 		    EHCI_QH_SET_EPS(speed) |
1956 		    EHCI_QH_DTC |
1957 		    EHCI_QH_SET_MPL(UGETW(ed->wMaxPacketSize)) |
1958 		    (speed != EHCI_QH_SPEED_HIGH && xfertype == UE_CONTROL ?
1959 		     EHCI_QH_CTL : 0) |
1960 		    EHCI_QH_SET_NRL(naks)
1961 		    );
1962 		sqh->qh.qh_endphub = htole32(
1963 		    EHCI_QH_SET_MULT(1) |
1964 		    EHCI_QH_SET_SMASK(xfertype == UE_INTERRUPT ? 0x02 : 0)
1965 		    );
1966 		if (speed != EHCI_QH_SPEED_HIGH)
1967 			sqh->qh.qh_endphub |= htole32(
1968 			    EHCI_QH_SET_PORT(hshubport) |
1969 			    EHCI_QH_SET_HUBA(hshubaddr) |
1970 			    (xfertype == UE_INTERRUPT ?
1971 				 EHCI_QH_SET_CMASK(0x08) : 0)
1972 			);
1973 		sqh->qh.qh_curqtd = EHCI_NULL;
1974 		/* Fill the overlay qTD */
1975 		sqh->qh.qh_qtd.qtd_next = EHCI_NULL;
1976 		sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
1977 		sqh->qh.qh_qtd.qtd_status = htole32(0);
1978 
1979 		usb_syncmem(&sqh->dma, sqh->offs, sizeof(sqh->qh),
1980 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
1981 		epipe->sqh = sqh;
1982 	} else {
1983 		sqh = NULL;
1984 	} /*xfertype == UE_ISOC*/
1985 
1986 	switch (xfertype) {
1987 	case UE_CONTROL:
1988 		err = usb_allocmem(&sc->sc_bus, sizeof(usb_device_request_t),
1989 				   0, &epipe->ctrl.reqdma);
1990 #ifdef EHCI_DEBUG
1991 		if (err)
1992 			printf("ehci_open: usb_allocmem()=%d\n", err);
1993 #endif
1994 		if (err)
1995 			goto bad;
1996 		pipe->up_methods = &ehci_device_ctrl_methods;
1997 		mutex_enter(&sc->sc_lock);
1998 		ehci_add_qh(sc, sqh, sc->sc_async_head);
1999 		mutex_exit(&sc->sc_lock);
2000 		break;
2001 	case UE_BULK:
2002 		pipe->up_methods = &ehci_device_bulk_methods;
2003 		mutex_enter(&sc->sc_lock);
2004 		ehci_add_qh(sc, sqh, sc->sc_async_head);
2005 		mutex_exit(&sc->sc_lock);
2006 		break;
2007 	case UE_INTERRUPT:
2008 		pipe->up_methods = &ehci_device_intr_methods;
2009 		ival = pipe->up_interval;
2010 		if (ival == USBD_DEFAULT_INTERVAL) {
2011 			if (speed == EHCI_QH_SPEED_HIGH) {
2012 				if (ed->bInterval > 16) {
2013 					/*
2014 					 * illegal with high-speed, but there
2015 					 * were documentation bugs in the spec,
2016 					 * so be generous
2017 					 */
2018 					ival = 256;
2019 				} else
2020 					ival = (1 << (ed->bInterval - 1)) / 8;
2021 			} else
2022 				ival = ed->bInterval;
2023 		}
2024 		err = ehci_device_setintr(sc, sqh, ival);
2025 		if (err)
2026 			goto bad;
2027 		break;
2028 	case UE_ISOCHRONOUS:
2029 		pipe->up_serialise = false;
2030 		if (speed == EHCI_QH_SPEED_HIGH)
2031 			pipe->up_methods = &ehci_device_isoc_methods;
2032 		else
2033 			pipe->up_methods = &ehci_device_fs_isoc_methods;
2034 		if (ed->bInterval == 0 || ed->bInterval > 16) {
2035 			printf("ehci: opening pipe with invalid bInterval\n");
2036 			err = USBD_INVAL;
2037 			goto bad;
2038 		}
2039 		if (UGETW(ed->wMaxPacketSize) == 0) {
2040 			printf("ehci: zero length endpoint open request\n");
2041 			err = USBD_INVAL;
2042 			goto bad;
2043 		}
2044 		epipe->isoc.next_frame = 0;
2045 		epipe->isoc.cur_xfers = 0;
2046 		break;
2047 	default:
2048 		DPRINTF("bad xfer type %jd", xfertype, 0, 0, 0);
2049 		err = USBD_INVAL;
2050 		goto bad;
2051 	}
2052 	return USBD_NORMAL_COMPLETION;
2053 
2054  bad:
2055 	if (sqh != NULL) {
2056 		mutex_enter(&sc->sc_lock);
2057 		ehci_free_sqh(sc, sqh);
2058 		mutex_exit(&sc->sc_lock);
2059 	}
2060 	return err;
2061 }
2062 
2063 /*
2064  * Add an ED to the schedule.  Called with USB lock held.
2065  */
2066 Static void
2067 ehci_add_qh(ehci_softc_t *sc, ehci_soft_qh_t *sqh, ehci_soft_qh_t *head)
2068 {
2069 
2070 	KASSERT(mutex_owned(&sc->sc_lock));
2071 
2072 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
2073 
2074 	usb_syncmem(&head->dma, head->offs + offsetof(ehci_qh_t, qh_link),
2075 	    sizeof(head->qh.qh_link), BUS_DMASYNC_POSTWRITE);
2076 
2077 	sqh->next = head->next;
2078 	sqh->qh.qh_link = head->qh.qh_link;
2079 
2080 	usb_syncmem(&sqh->dma, sqh->offs + offsetof(ehci_qh_t, qh_link),
2081 	    sizeof(sqh->qh.qh_link), BUS_DMASYNC_PREWRITE);
2082 
2083 	head->next = sqh;
2084 	head->qh.qh_link = htole32(sqh->physaddr | EHCI_LINK_QH);
2085 
2086 	usb_syncmem(&head->dma, head->offs + offsetof(ehci_qh_t, qh_link),
2087 	    sizeof(head->qh.qh_link), BUS_DMASYNC_PREWRITE);
2088 
2089 #ifdef EHCI_DEBUG
2090 	DPRINTFN(5, "--- dump start ---", 0, 0, 0, 0);
2091 	ehci_dump_sqh(sqh);
2092 	DPRINTFN(5, "--- dump end ---", 0, 0, 0, 0);
2093 #endif
2094 }
2095 
2096 /*
2097  * Remove an ED from the schedule.  Called with USB lock held.
2098  */
2099 Static void
2100 ehci_rem_qh(ehci_softc_t *sc, ehci_soft_qh_t *sqh, ehci_soft_qh_t *head)
2101 {
2102 	ehci_soft_qh_t *p;
2103 
2104 	KASSERT(mutex_owned(&sc->sc_lock));
2105 
2106 	/* XXX */
2107 	for (p = head; p != NULL && p->next != sqh; p = p->next)
2108 		;
2109 	if (p == NULL)
2110 		panic("ehci_rem_qh: ED not found");
2111 	usb_syncmem(&sqh->dma, sqh->offs + offsetof(ehci_qh_t, qh_link),
2112 	    sizeof(sqh->qh.qh_link), BUS_DMASYNC_POSTWRITE);
2113 	p->next = sqh->next;
2114 	p->qh.qh_link = sqh->qh.qh_link;
2115 	usb_syncmem(&p->dma, p->offs + offsetof(ehci_qh_t, qh_link),
2116 	    sizeof(p->qh.qh_link), BUS_DMASYNC_PREWRITE);
2117 
2118 	ehci_sync_hc(sc);
2119 }
2120 
2121 Static void
2122 ehci_set_qh_qtd(ehci_soft_qh_t *sqh, ehci_soft_qtd_t *sqtd)
2123 {
2124 	int i;
2125 	uint32_t status;
2126 
2127 	/* Save toggle bit and ping status. */
2128 	usb_syncmem(&sqh->dma, sqh->offs, sizeof(sqh->qh),
2129 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
2130 	status = sqh->qh.qh_qtd.qtd_status &
2131 	    htole32(EHCI_QTD_TOGGLE_MASK |
2132 		    EHCI_QTD_SET_STATUS(EHCI_QTD_PINGSTATE));
2133 	/* Set HALTED to make hw leave it alone. */
2134 	sqh->qh.qh_qtd.qtd_status =
2135 	    htole32(EHCI_QTD_SET_STATUS(EHCI_QTD_HALTED));
2136 	usb_syncmem(&sqh->dma,
2137 	    sqh->offs + offsetof(ehci_qh_t, qh_qtd.qtd_status),
2138 	    sizeof(sqh->qh.qh_qtd.qtd_status),
2139 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2140 	sqh->qh.qh_curqtd = 0;
2141 	sqh->qh.qh_qtd.qtd_next = htole32(sqtd->physaddr);
2142 	sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
2143 	for (i = 0; i < EHCI_QTD_NBUFFERS; i++)
2144 		sqh->qh.qh_qtd.qtd_buffer[i] = 0;
2145 	sqh->sqtd = sqtd;
2146 	usb_syncmem(&sqh->dma, sqh->offs, sizeof(sqh->qh),
2147 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2148 	/* Set !HALTED && !ACTIVE to start execution, preserve some fields */
2149 	sqh->qh.qh_qtd.qtd_status = status;
2150 	usb_syncmem(&sqh->dma,
2151 	    sqh->offs + offsetof(ehci_qh_t, qh_qtd.qtd_status),
2152 	    sizeof(sqh->qh.qh_qtd.qtd_status),
2153 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2154 }
2155 
2156 /*
2157  * Ensure that the HC has released all references to the QH.  We do this
2158  * by asking for a Async Advance Doorbell interrupt and then we wait for
2159  * the interrupt.
2160  * To make this easier we first obtain exclusive use of the doorbell.
2161  */
2162 Static void
2163 ehci_sync_hc(ehci_softc_t *sc)
2164 {
2165 	int error __diagused;
2166 
2167 	KASSERT(mutex_owned(&sc->sc_lock));
2168 
2169 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
2170 
2171 	if (sc->sc_dying) {
2172 		DPRINTF("dying", 0, 0, 0, 0);
2173 		return;
2174 	}
2175 	/* ask for doorbell */
2176 	EOWRITE4(sc, EHCI_USBCMD, EOREAD4(sc, EHCI_USBCMD) | EHCI_CMD_IAAD);
2177 	DPRINTF("cmd = 0x%08jx sts = 0x%08jx",
2178 	    EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS), 0, 0);
2179 
2180 	error = cv_timedwait(&sc->sc_doorbell, &sc->sc_lock, hz); /* bell wait */
2181 
2182 	DPRINTF("cmd = 0x%08jx sts = 0x%08jx ... done",
2183 	    EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS), 0, 0);
2184 #ifdef DIAGNOSTIC
2185 	if (error == EWOULDBLOCK) {
2186 		printf("ehci_sync_hc: timed out\n");
2187 	} else if (error) {
2188 		printf("ehci_sync_hc: cv_timedwait: error %d\n", error);
2189 	}
2190 #endif
2191 }
2192 
2193 Static void
2194 ehci_remove_itd_chain(ehci_softc_t *sc, struct ehci_soft_itd *itd)
2195 {
2196 
2197 	KASSERT(mutex_owned(&sc->sc_lock));
2198 
2199 	for (; itd != NULL; itd = itd->xfer_next) {
2200 		struct ehci_soft_itd *prev = itd->frame_list.prev;
2201 
2202 		/* Unlink itd from hardware chain, or frame array */
2203 		if (prev == NULL) { /* We're at the table head */
2204 			sc->sc_softitds[itd->slot] = itd->frame_list.next;
2205 			sc->sc_flist[itd->slot] = itd->itd.itd_next;
2206 			usb_syncmem(&sc->sc_fldma,
2207 			    sizeof(ehci_link_t) * itd->slot,
2208 			    sizeof(ehci_link_t),
2209 			    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2210 
2211 			if (itd->frame_list.next != NULL)
2212 				itd->frame_list.next->frame_list.prev = NULL;
2213 		} else {
2214 			/* XXX this part is untested... */
2215 			prev->itd.itd_next = itd->itd.itd_next;
2216 			usb_syncmem(&itd->dma,
2217 			    itd->offs + offsetof(ehci_itd_t, itd_next),
2218 			    sizeof(itd->itd.itd_next), BUS_DMASYNC_PREWRITE);
2219 
2220 			prev->frame_list.next = itd->frame_list.next;
2221 			if (itd->frame_list.next != NULL)
2222 				itd->frame_list.next->frame_list.prev = prev;
2223 		}
2224 	}
2225 }
2226 
2227 Static void
2228 ehci_free_itd_chain(ehci_softc_t *sc, struct ehci_soft_itd *itd)
2229 {
2230 	struct ehci_soft_itd *next;
2231 
2232 	mutex_enter(&sc->sc_lock);
2233 	next = NULL;
2234 	for (; itd != NULL; itd = next) {
2235 		next = itd->xfer_next;
2236 		ehci_free_itd_locked(sc, itd);
2237 	}
2238 	mutex_exit(&sc->sc_lock);
2239 }
2240 
2241 Static void
2242 ehci_remove_sitd_chain(ehci_softc_t *sc, struct ehci_soft_sitd *sitd)
2243 {
2244 
2245 	KASSERT(mutex_owned(&sc->sc_lock));
2246 
2247 	for (; sitd != NULL; sitd = sitd->xfer_next) {
2248 		struct ehci_soft_sitd *prev = sitd->frame_list.prev;
2249 
2250 		/* Unlink sitd from hardware chain, or frame array */
2251 		if (prev == NULL) { /* We're at the table head */
2252 			sc->sc_softsitds[sitd->slot] = sitd->frame_list.next;
2253 			sc->sc_flist[sitd->slot] = sitd->sitd.sitd_next;
2254 			usb_syncmem(&sc->sc_fldma,
2255 			    sizeof(ehci_link_t) * sitd->slot,
2256 			    sizeof(ehci_link_t),
2257 			    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2258 
2259 			if (sitd->frame_list.next != NULL)
2260 				sitd->frame_list.next->frame_list.prev = NULL;
2261 		} else {
2262 			/* XXX this part is untested... */
2263 			prev->sitd.sitd_next = sitd->sitd.sitd_next;
2264 			usb_syncmem(&sitd->dma,
2265 			    sitd->offs + offsetof(ehci_sitd_t, sitd_next),
2266 			    sizeof(sitd->sitd.sitd_next), BUS_DMASYNC_PREWRITE);
2267 
2268 			prev->frame_list.next = sitd->frame_list.next;
2269 			if (sitd->frame_list.next != NULL)
2270 				sitd->frame_list.next->frame_list.prev = prev;
2271 		}
2272 	}
2273 }
2274 
2275 Static void
2276 ehci_free_sitd_chain(ehci_softc_t *sc, struct ehci_soft_sitd *sitd)
2277 {
2278 
2279 	mutex_enter(&sc->sc_lock);
2280 	struct ehci_soft_sitd *next  = NULL;
2281 	for (; sitd != NULL; sitd = next) {
2282 		next = sitd->xfer_next;
2283 		ehci_free_sitd_locked(sc, sitd);
2284 	}
2285 	mutex_exit(&sc->sc_lock);
2286 }
2287 
2288 /***********/
2289 
2290 Static int
2291 ehci_roothub_ctrl(struct usbd_bus *bus, usb_device_request_t *req,
2292     void *buf, int buflen)
2293 {
2294 	ehci_softc_t *sc = EHCI_BUS2SC(bus);
2295 	usb_hub_descriptor_t hubd;
2296 	usb_port_status_t ps;
2297 	uint16_t len, value, index;
2298 	int l, totlen = 0;
2299 	int port, i;
2300 	uint32_t v;
2301 
2302 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
2303 
2304 	if (sc->sc_dying)
2305 		return -1;
2306 
2307 	DPRINTF("type=0x%02jx request=%02jx", req->bmRequestType, req->bRequest,
2308 	    0, 0);
2309 
2310 	len = UGETW(req->wLength);
2311 	value = UGETW(req->wValue);
2312 	index = UGETW(req->wIndex);
2313 
2314 #define C(x,y) ((x) | ((y) << 8))
2315 	switch (C(req->bRequest, req->bmRequestType)) {
2316 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2317 		if (len == 0)
2318 			break;
2319 		switch (value) {
2320 		case C(0, UDESC_DEVICE): {
2321 			usb_device_descriptor_t devd;
2322 			totlen = min(buflen, sizeof(devd));
2323 			memcpy(&devd, buf, totlen);
2324 			USETW(devd.idVendor, sc->sc_id_vendor);
2325 			memcpy(buf, &devd, totlen);
2326 			break;
2327 
2328 		}
2329 #define sd ((usb_string_descriptor_t *)buf)
2330 		case C(1, UDESC_STRING):
2331 			/* Vendor */
2332 			totlen = usb_makestrdesc(sd, len, sc->sc_vendor);
2333 			break;
2334 		case C(2, UDESC_STRING):
2335 			/* Product */
2336 			totlen = usb_makestrdesc(sd, len, "EHCI root hub");
2337 			break;
2338 #undef sd
2339 		default:
2340 			/* default from usbroothub */
2341 			return buflen;
2342 		}
2343 		break;
2344 
2345 	/* Hub requests */
2346 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2347 		break;
2348 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2349 		DPRINTF("UR_CLEAR_PORT_FEATURE port=%jd feature=%jd", index,
2350 		    value, 0, 0);
2351 		if (index < 1 || index > sc->sc_noport) {
2352 			return -1;
2353 		}
2354 		port = EHCI_PORTSC(index);
2355 		v = EOREAD4(sc, port);
2356 		DPRINTF("portsc=0x%08jx", v, 0, 0, 0);
2357 		v &= ~EHCI_PS_CLEAR;
2358 		switch (value) {
2359 		case UHF_PORT_ENABLE:
2360 			EOWRITE4(sc, port, v &~ EHCI_PS_PE);
2361 			break;
2362 		case UHF_PORT_SUSPEND:
2363 			if (!(v & EHCI_PS_SUSP)) /* not suspended */
2364 				break;
2365 			v &= ~EHCI_PS_SUSP;
2366 			EOWRITE4(sc, port, v | EHCI_PS_FPR);
2367 			/* see USB2 spec ch. 7.1.7.7 */
2368 			usb_delay_ms(&sc->sc_bus, 20);
2369 			EOWRITE4(sc, port, v);
2370 			usb_delay_ms(&sc->sc_bus, 2);
2371 #ifdef DEBUG
2372 			v = EOREAD4(sc, port);
2373 			if (v & (EHCI_PS_FPR | EHCI_PS_SUSP))
2374 				printf("ehci: resume failed: %x\n", v);
2375 #endif
2376 			break;
2377 		case UHF_PORT_POWER:
2378 			if (sc->sc_hasppc)
2379 				EOWRITE4(sc, port, v &~ EHCI_PS_PP);
2380 			break;
2381 		case UHF_PORT_TEST:
2382 			DPRINTF("clear port test %jd", index, 0, 0, 0);
2383 			break;
2384 		case UHF_PORT_INDICATOR:
2385 			DPRINTF("clear port ind %jd", index, 0, 0, 0);
2386 			EOWRITE4(sc, port, v &~ EHCI_PS_PIC);
2387 			break;
2388 		case UHF_C_PORT_CONNECTION:
2389 			EOWRITE4(sc, port, v | EHCI_PS_CSC);
2390 			break;
2391 		case UHF_C_PORT_ENABLE:
2392 			EOWRITE4(sc, port, v | EHCI_PS_PEC);
2393 			break;
2394 		case UHF_C_PORT_SUSPEND:
2395 			/* how? */
2396 			break;
2397 		case UHF_C_PORT_OVER_CURRENT:
2398 			EOWRITE4(sc, port, v | EHCI_PS_OCC);
2399 			break;
2400 		case UHF_C_PORT_RESET:
2401 			sc->sc_isreset[index] = 0;
2402 			break;
2403 		default:
2404 			return -1;
2405 		}
2406 #if 0
2407 		switch(value) {
2408 		case UHF_C_PORT_CONNECTION:
2409 		case UHF_C_PORT_ENABLE:
2410 		case UHF_C_PORT_SUSPEND:
2411 		case UHF_C_PORT_OVER_CURRENT:
2412 		case UHF_C_PORT_RESET:
2413 		default:
2414 			break;
2415 		}
2416 #endif
2417 		break;
2418 	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2419 		if (len == 0)
2420 			break;
2421 		if ((value & 0xff) != 0) {
2422 			return -1;
2423 		}
2424 		totlen = min(buflen, sizeof(hubd));
2425 		memcpy(&hubd, buf, totlen);
2426 		hubd.bNbrPorts = sc->sc_noport;
2427 		v = EOREAD4(sc, EHCI_HCSPARAMS);
2428 		USETW(hubd.wHubCharacteristics,
2429 		    EHCI_HCS_PPC(v) ? UHD_PWR_INDIVIDUAL : UHD_PWR_NO_SWITCH |
2430 		    EHCI_HCS_P_INDICATOR(EREAD4(sc, EHCI_HCSPARAMS))
2431 			? UHD_PORT_IND : 0);
2432 		hubd.bPwrOn2PwrGood = 200; /* XXX can't find out? */
2433 		for (i = 0, l = sc->sc_noport; l > 0; i++, l -= 8, v >>= 8)
2434 			hubd.DeviceRemovable[i++] = 0; /* XXX can't find out? */
2435 		hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
2436 		totlen = min(totlen, hubd.bDescLength);
2437 		memcpy(buf, &hubd, totlen);
2438 		break;
2439 	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2440 		if (len != 4) {
2441 			return -1;
2442 		}
2443 		memset(buf, 0, len); /* ? XXX */
2444 		totlen = len;
2445 		break;
2446 	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2447 		DPRINTF("get port status i=%jd", index, 0, 0, 0);
2448 		if (index < 1 || index > sc->sc_noport) {
2449 			return -1;
2450 		}
2451 		if (len != 4) {
2452 			return -1;
2453 		}
2454 		v = EOREAD4(sc, EHCI_PORTSC(index));
2455 		DPRINTF("port status=0x%04jx", v, 0, 0, 0);
2456 
2457 		i = UPS_HIGH_SPEED;
2458 		if (sc->sc_flags & EHCIF_ETTF) {
2459 			/*
2460 			 * If we are doing embedded transaction translation,
2461 			 * then directly attached LS/FS devices are reset by
2462 			 * the EHCI controller itself.  PSPD is encoded
2463 			 * the same way as in USBSTATUS.
2464 			 */
2465 			i = __SHIFTOUT(v, EHCI_PS_PSPD) * UPS_LOW_SPEED;
2466 		}
2467 		if (v & EHCI_PS_CS)	i |= UPS_CURRENT_CONNECT_STATUS;
2468 		if (v & EHCI_PS_PE)	i |= UPS_PORT_ENABLED;
2469 		if (v & EHCI_PS_SUSP)	i |= UPS_SUSPEND;
2470 		if (v & EHCI_PS_OCA)	i |= UPS_OVERCURRENT_INDICATOR;
2471 		if (v & EHCI_PS_PR)	i |= UPS_RESET;
2472 		if (v & EHCI_PS_PP)	i |= UPS_PORT_POWER;
2473 		if (sc->sc_vendor_port_status)
2474 			i = sc->sc_vendor_port_status(sc, v, i);
2475 		USETW(ps.wPortStatus, i);
2476 		i = 0;
2477 		if (v & EHCI_PS_CSC)	i |= UPS_C_CONNECT_STATUS;
2478 		if (v & EHCI_PS_PEC)	i |= UPS_C_PORT_ENABLED;
2479 		if (v & EHCI_PS_OCC)	i |= UPS_C_OVERCURRENT_INDICATOR;
2480 		if (sc->sc_isreset[index]) i |= UPS_C_PORT_RESET;
2481 		USETW(ps.wPortChange, i);
2482 		totlen = min(len, sizeof(ps));
2483 		memcpy(buf, &ps, totlen);
2484 		break;
2485 	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2486 		return -1;
2487 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2488 		break;
2489 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2490 		if (index < 1 || index > sc->sc_noport) {
2491 			return -1;
2492 		}
2493 		port = EHCI_PORTSC(index);
2494 		v = EOREAD4(sc, port);
2495 		DPRINTF("portsc=0x%08jx", v, 0, 0, 0);
2496 		v &= ~EHCI_PS_CLEAR;
2497 		switch(value) {
2498 		case UHF_PORT_ENABLE:
2499 			EOWRITE4(sc, port, v | EHCI_PS_PE);
2500 			break;
2501 		case UHF_PORT_SUSPEND:
2502 			EOWRITE4(sc, port, v | EHCI_PS_SUSP);
2503 			break;
2504 		case UHF_PORT_RESET:
2505 			DPRINTF("reset port %jd", index, 0, 0, 0);
2506 			if (EHCI_PS_IS_LOWSPEED(v)
2507 			    && sc->sc_ncomp > 0
2508 			    && !(sc->sc_flags & EHCIF_ETTF)) {
2509 				/*
2510 				 * Low speed device on non-ETTF controller or
2511 				 * unaccompanied controller, give up ownership.
2512 				 */
2513 				ehci_disown(sc, index, 1);
2514 				break;
2515 			}
2516 			/* Start reset sequence. */
2517 			v &= ~ (EHCI_PS_PE | EHCI_PS_PR);
2518 			EOWRITE4(sc, port, v | EHCI_PS_PR);
2519 			/* Wait for reset to complete. */
2520 			usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
2521 			if (sc->sc_dying) {
2522 				return -1;
2523 			}
2524 			/*
2525 			 * An embedded transaction translator will automatically
2526 			 * terminate the reset sequence so there's no need to
2527 			 * it.
2528 			 */
2529 			v = EOREAD4(sc, port);
2530 			if (v & EHCI_PS_PR) {
2531 				/* Terminate reset sequence. */
2532 				EOWRITE4(sc, port, v & ~EHCI_PS_PR);
2533 				/* Wait for HC to complete reset. */
2534 				usb_delay_ms(&sc->sc_bus,
2535 				    EHCI_PORT_RESET_COMPLETE);
2536 				if (sc->sc_dying) {
2537 					return -1;
2538 				}
2539 			}
2540 
2541 			v = EOREAD4(sc, port);
2542 			DPRINTF("ehci after reset, status=0x%08jx", v, 0, 0, 0);
2543 			if (v & EHCI_PS_PR) {
2544 				printf("%s: port reset timeout\n",
2545 				       device_xname(sc->sc_dev));
2546 				return USBD_TIMEOUT;
2547 			}
2548 			if (!(v & EHCI_PS_PE)) {
2549 				/* Not a high speed device, give up ownership.*/
2550 				ehci_disown(sc, index, 0);
2551 				break;
2552 			}
2553 			sc->sc_isreset[index] = 1;
2554 			DPRINTF("ehci port %jd reset, status = 0x%08jx", index,
2555 			    v, 0, 0);
2556 			break;
2557 		case UHF_PORT_POWER:
2558 			DPRINTF("set port power %jd (has PPC = %jd)", index,
2559 			    sc->sc_hasppc, 0, 0);
2560 			if (sc->sc_hasppc)
2561 				EOWRITE4(sc, port, v | EHCI_PS_PP);
2562 			break;
2563 		case UHF_PORT_TEST:
2564 			DPRINTF("set port test %jd", index, 0, 0, 0);
2565 			break;
2566 		case UHF_PORT_INDICATOR:
2567 			DPRINTF("set port ind %jd", index, 0, 0, 0);
2568 			EOWRITE4(sc, port, v | EHCI_PS_PIC);
2569 			break;
2570 		default:
2571 			return -1;
2572 		}
2573 		break;
2574 	case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
2575 	case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
2576 	case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
2577 	case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
2578 		break;
2579 	default:
2580 		/* default from usbroothub */
2581 		DPRINTF("returning %jd (usbroothub default)", buflen, 0, 0, 0);
2582 
2583 		return buflen;
2584 	}
2585 
2586 	DPRINTF("returning %jd", totlen, 0, 0, 0);
2587 
2588 	return totlen;
2589 }
2590 
2591 Static void
2592 ehci_disown(ehci_softc_t *sc, int index, int lowspeed)
2593 {
2594 	int port;
2595 	uint32_t v;
2596 
2597 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
2598 
2599 	DPRINTF("index=%jd lowspeed=%jd", index, lowspeed, 0, 0);
2600 #ifdef DIAGNOSTIC
2601 	if (sc->sc_npcomp != 0) {
2602 		int i = (index-1) / sc->sc_npcomp;
2603 		if (i >= sc->sc_ncomp)
2604 			printf("%s: strange port\n",
2605 			       device_xname(sc->sc_dev));
2606 		else
2607 			printf("%s: handing over %s speed device on "
2608 			       "port %d to %s\n",
2609 			       device_xname(sc->sc_dev),
2610 			       lowspeed ? "low" : "full",
2611 			       index, sc->sc_comps[i] ?
2612 			         device_xname(sc->sc_comps[i]) :
2613 			         "companion controller");
2614 	} else {
2615 		printf("%s: npcomp == 0\n", device_xname(sc->sc_dev));
2616 	}
2617 #endif
2618 	port = EHCI_PORTSC(index);
2619 	v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR;
2620 	EOWRITE4(sc, port, v | EHCI_PS_PO);
2621 }
2622 
2623 Static usbd_status
2624 ehci_root_intr_transfer(struct usbd_xfer *xfer)
2625 {
2626 	ehci_softc_t *sc = EHCI_XFER2SC(xfer);
2627 	usbd_status err;
2628 
2629 	/* Insert last in queue. */
2630 	mutex_enter(&sc->sc_lock);
2631 	err = usb_insert_transfer(xfer);
2632 	mutex_exit(&sc->sc_lock);
2633 	if (err)
2634 		return err;
2635 
2636 	/* Pipe isn't running, start first */
2637 	return ehci_root_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
2638 }
2639 
2640 Static usbd_status
2641 ehci_root_intr_start(struct usbd_xfer *xfer)
2642 {
2643 	ehci_softc_t *sc = EHCI_XFER2SC(xfer);
2644 
2645 	if (sc->sc_dying)
2646 		return USBD_IOERROR;
2647 
2648 	mutex_enter(&sc->sc_lock);
2649 	sc->sc_intrxfer = xfer;
2650 	mutex_exit(&sc->sc_lock);
2651 
2652 	return USBD_IN_PROGRESS;
2653 }
2654 
2655 /* Abort a root interrupt request. */
2656 Static void
2657 ehci_root_intr_abort(struct usbd_xfer *xfer)
2658 {
2659 	ehci_softc_t *sc = EHCI_XFER2SC(xfer);
2660 
2661 	KASSERT(mutex_owned(&sc->sc_lock));
2662 	KASSERT(xfer->ux_pipe->up_intrxfer == xfer);
2663 
2664 	sc->sc_intrxfer = NULL;
2665 
2666 	xfer->ux_status = USBD_CANCELLED;
2667 	usb_transfer_complete(xfer);
2668 }
2669 
2670 /* Close the root pipe. */
2671 Static void
2672 ehci_root_intr_close(struct usbd_pipe *pipe)
2673 {
2674 	ehci_softc_t *sc = EHCI_PIPE2SC(pipe);
2675 
2676 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
2677 
2678 	KASSERT(mutex_owned(&sc->sc_lock));
2679 
2680 	sc->sc_intrxfer = NULL;
2681 }
2682 
2683 Static void
2684 ehci_root_intr_done(struct usbd_xfer *xfer)
2685 {
2686 }
2687 
2688 /************************/
2689 
2690 Static ehci_soft_qh_t *
2691 ehci_alloc_sqh(ehci_softc_t *sc)
2692 {
2693 	ehci_soft_qh_t *sqh;
2694 	usbd_status err;
2695 	int i, offs;
2696 	usb_dma_t dma;
2697 
2698 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
2699 
2700 	mutex_enter(&sc->sc_lock);
2701 	if (sc->sc_freeqhs == NULL) {
2702 		DPRINTF("allocating chunk", 0, 0, 0, 0);
2703 		mutex_exit(&sc->sc_lock);
2704 
2705 		err = usb_allocmem(&sc->sc_bus, EHCI_SQH_SIZE * EHCI_SQH_CHUNK,
2706 			  EHCI_PAGE_SIZE, &dma);
2707 #ifdef EHCI_DEBUG
2708 		if (err)
2709 			printf("ehci_alloc_sqh: usb_allocmem()=%d\n", err);
2710 #endif
2711 		if (err)
2712 			return NULL;
2713 
2714 		mutex_enter(&sc->sc_lock);
2715 		for (i = 0; i < EHCI_SQH_CHUNK; i++) {
2716 			offs = i * EHCI_SQH_SIZE;
2717 			sqh = KERNADDR(&dma, offs);
2718 			sqh->physaddr = DMAADDR(&dma, offs);
2719 			sqh->dma = dma;
2720 			sqh->offs = offs;
2721 			sqh->next = sc->sc_freeqhs;
2722 			sc->sc_freeqhs = sqh;
2723 		}
2724 	}
2725 	sqh = sc->sc_freeqhs;
2726 	sc->sc_freeqhs = sqh->next;
2727 	mutex_exit(&sc->sc_lock);
2728 
2729 	memset(&sqh->qh, 0, sizeof(ehci_qh_t));
2730 	sqh->next = NULL;
2731 	return sqh;
2732 }
2733 
2734 Static void
2735 ehci_free_sqh(ehci_softc_t *sc, ehci_soft_qh_t *sqh)
2736 {
2737 	KASSERT(mutex_owned(&sc->sc_lock));
2738 
2739 	sqh->next = sc->sc_freeqhs;
2740 	sc->sc_freeqhs = sqh;
2741 }
2742 
2743 Static ehci_soft_qtd_t *
2744 ehci_alloc_sqtd(ehci_softc_t *sc)
2745 {
2746 	ehci_soft_qtd_t *sqtd = NULL;
2747 	usbd_status err;
2748 	int i, offs;
2749 	usb_dma_t dma;
2750 
2751 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
2752 
2753 	mutex_enter(&sc->sc_lock);
2754 	if (sc->sc_freeqtds == NULL) {
2755 		DPRINTF("allocating chunk", 0, 0, 0, 0);
2756 		mutex_exit(&sc->sc_lock);
2757 
2758 		err = usb_allocmem(&sc->sc_bus, EHCI_SQTD_SIZE*EHCI_SQTD_CHUNK,
2759 			  EHCI_PAGE_SIZE, &dma);
2760 #ifdef EHCI_DEBUG
2761 		if (err)
2762 			printf("ehci_alloc_sqtd: usb_allocmem()=%d\n", err);
2763 #endif
2764 		if (err)
2765 			goto done;
2766 
2767 		mutex_enter(&sc->sc_lock);
2768 		for (i = 0; i < EHCI_SQTD_CHUNK; i++) {
2769 			offs = i * EHCI_SQTD_SIZE;
2770 			sqtd = KERNADDR(&dma, offs);
2771 			sqtd->physaddr = DMAADDR(&dma, offs);
2772 			sqtd->dma = dma;
2773 			sqtd->offs = offs;
2774 
2775 			sqtd->nextqtd = sc->sc_freeqtds;
2776 			sc->sc_freeqtds = sqtd;
2777 		}
2778 	}
2779 
2780 	sqtd = sc->sc_freeqtds;
2781 	sc->sc_freeqtds = sqtd->nextqtd;
2782 	mutex_exit(&sc->sc_lock);
2783 
2784 	memset(&sqtd->qtd, 0, sizeof(ehci_qtd_t));
2785 	sqtd->nextqtd = NULL;
2786 	sqtd->xfer = NULL;
2787 
2788 done:
2789 	return sqtd;
2790 }
2791 
2792 Static void
2793 ehci_free_sqtd(ehci_softc_t *sc, ehci_soft_qtd_t *sqtd)
2794 {
2795 
2796 	mutex_enter(&sc->sc_lock);
2797 	sqtd->nextqtd = sc->sc_freeqtds;
2798 	sc->sc_freeqtds = sqtd;
2799 	mutex_exit(&sc->sc_lock);
2800 }
2801 
2802 Static int
2803 ehci_alloc_sqtd_chain(ehci_softc_t *sc, struct usbd_xfer *xfer,
2804     int alen, int rd, ehci_soft_qtd_t **sp)
2805 {
2806 	struct ehci_xfer *exfer = EHCI_XFER2EXFER(xfer);
2807 	uint16_t flags = xfer->ux_flags;
2808 
2809 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
2810 
2811 	ASSERT_SLEEPABLE();
2812 	KASSERT(sp);
2813 	KASSERT(alen != 0 || (!rd && (flags & USBD_FORCE_SHORT_XFER)));
2814 
2815 	size_t nsqtd = (!rd && (flags & USBD_FORCE_SHORT_XFER)) ? 1 : 0;
2816 	nsqtd += ((alen + EHCI_PAGE_SIZE - 1) / EHCI_PAGE_SIZE);
2817 	exfer->ex_sqtds = kmem_zalloc(sizeof(ehci_soft_qtd_t *) * nsqtd,
2818 	    KM_SLEEP);
2819 	exfer->ex_nsqtd = nsqtd;
2820 
2821 	DPRINTF("xfer %#jx len %jd nsqtd %jd flags %jx", (uintptr_t)xfer,
2822 	    alen, nsqtd, flags);
2823 
2824 	for (size_t j = 0; j < exfer->ex_nsqtd;) {
2825 		ehci_soft_qtd_t *cur = ehci_alloc_sqtd(sc);
2826 		if (cur == NULL)
2827 			goto nomem;
2828 		exfer->ex_sqtds[j++] = cur;
2829 
2830 		cur->xfer = xfer;
2831 		cur->len = 0;
2832 
2833 	}
2834 
2835 	*sp = exfer->ex_sqtds[0];
2836 	DPRINTF("return sqtd=%#jx", (uintptr_t)*sp, 0, 0, 0);
2837 
2838 	return 0;
2839 
2840  nomem:
2841 	ehci_free_sqtds(sc, exfer);
2842 	kmem_free(exfer->ex_sqtds, sizeof(ehci_soft_qtd_t *) * nsqtd);
2843 	DPRINTF("no memory", 0, 0, 0, 0);
2844 	return ENOMEM;
2845 }
2846 
2847 Static void
2848 ehci_free_sqtds(ehci_softc_t *sc, struct ehci_xfer *exfer)
2849 {
2850 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
2851 	DPRINTF("exfer=%#jx", (uintptr_t)exfer, 0, 0, 0);
2852 
2853 	mutex_enter(&sc->sc_lock);
2854 	for (size_t i = 0; i < exfer->ex_nsqtd; i++) {
2855 		ehci_soft_qtd_t *sqtd = exfer->ex_sqtds[i];
2856 
2857 		if (sqtd == NULL)
2858 			break;
2859 
2860 		sqtd->nextqtd = sc->sc_freeqtds;
2861 		sc->sc_freeqtds = sqtd;
2862 	}
2863 	mutex_exit(&sc->sc_lock);
2864 }
2865 
2866 Static void
2867 ehci_append_sqtd(ehci_soft_qtd_t *sqtd, ehci_soft_qtd_t *prev)
2868 {
2869 	if (prev) {
2870 		prev->nextqtd = sqtd;
2871 		prev->qtd.qtd_next = htole32(sqtd->physaddr);
2872 		prev->qtd.qtd_altnext = prev->qtd.qtd_next;
2873 		usb_syncmem(&prev->dma, prev->offs, sizeof(prev->qtd),
2874 		    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
2875 	}
2876 }
2877 
2878 Static void
2879 ehci_reset_sqtd_chain(ehci_softc_t *sc, struct usbd_xfer *xfer,
2880     int length, int isread, int *toggle, ehci_soft_qtd_t **lsqtd)
2881 {
2882 	struct ehci_xfer *exfer = EHCI_XFER2EXFER(xfer);
2883 	usb_dma_t *dma = &xfer->ux_dmabuf;
2884 	uint16_t flags = xfer->ux_flags;
2885 	ehci_soft_qtd_t *sqtd, *prev;
2886 	int tog = *toggle;
2887 	int mps = UGETW(xfer->ux_pipe->up_endpoint->ue_edesc->wMaxPacketSize);
2888 	int len = length;
2889 
2890 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
2891 	DPRINTF("xfer=%#jx len %jd isread %jd toggle %jd", (uintptr_t)xfer,
2892 	    len, isread, tog);
2893 	DPRINTF("    VA %#jx", (uintptr_t)KERNADDR(&xfer->ux_dmabuf, 0),
2894 	    0, 0, 0);
2895 
2896 	KASSERT(length != 0 || (!isread && (flags & USBD_FORCE_SHORT_XFER)));
2897 
2898 	const uint32_t qtdstatus = EHCI_QTD_ACTIVE |
2899 	    EHCI_QTD_SET_PID(isread ? EHCI_QTD_PID_IN : EHCI_QTD_PID_OUT) |
2900 	    EHCI_QTD_SET_CERR(3)
2901 	    ;
2902 
2903 	sqtd = prev = NULL;
2904 	size_t curoffs = 0;
2905 	size_t j = 0;
2906 	for (; len != 0 && j < exfer->ex_nsqtd; prev = sqtd) {
2907 		sqtd = exfer->ex_sqtds[j++];
2908 		DPRINTF("sqtd[%jd]=%#jx prev %#jx", j, (uintptr_t)sqtd,
2909 		    (uintptr_t)prev, 0);
2910 
2911 		/*
2912 		 * The EHCI hardware can handle at most 5 pages and they do
2913 		 * not have to be contiguous
2914 		 */
2915 		vaddr_t va = (vaddr_t)KERNADDR(dma, curoffs);
2916 		vaddr_t va_offs = EHCI_PAGE_OFFSET(va);
2917 		size_t curlen = len;
2918 		if (curlen >= EHCI_QTD_MAXTRANSFER - va_offs) {
2919 			/* must use multiple TDs, fill as much as possible. */
2920 			curlen = EHCI_QTD_MAXTRANSFER - va_offs;
2921 
2922 			/* the length must be a multiple of the max size */
2923 			curlen -= curlen % mps;
2924 		}
2925 		KASSERT(curlen != 0);
2926 		DPRINTF("    len=%jd curlen=%jd curoffs=%ju", len, curlen,
2927 		    curoffs, 0);
2928 
2929 		/* Fill the qTD */
2930 		sqtd->qtd.qtd_next = sqtd->qtd.qtd_altnext = EHCI_NULL;
2931 		sqtd->qtd.qtd_status = htole32(
2932 		    qtdstatus |
2933 		    EHCI_QTD_SET_BYTES(curlen) |
2934 		    EHCI_QTD_SET_TOGGLE(tog));
2935 
2936 		/* Find number of pages we'll be using, insert dma addresses */
2937 		size_t pages = EHCI_NPAGES(curlen);
2938 		KASSERT(pages <= EHCI_QTD_NBUFFERS);
2939 		size_t pageoffs = EHCI_PAGE(curoffs);
2940 		for (size_t i = 0; i < pages; i++) {
2941 			paddr_t a = DMAADDR(dma,
2942 			    pageoffs + i * EHCI_PAGE_SIZE);
2943 			sqtd->qtd.qtd_buffer[i] = htole32(EHCI_PAGE(a));
2944 			/* Cast up to avoid compiler warnings */
2945 			sqtd->qtd.qtd_buffer_hi[i] = htole32((uint64_t)a >> 32);
2946 			DPRINTF("      buffer[%jd/%jd] 0x%08jx 0x%08jx",
2947 			    i, pages,
2948 			    le32toh(sqtd->qtd.qtd_buffer_hi[i]),
2949 			    le32toh(sqtd->qtd.qtd_buffer[i]));
2950 		}
2951 		/* First buffer pointer requires a page offset to start at */
2952 		sqtd->qtd.qtd_buffer[0] |= htole32(va_offs);
2953 
2954 		usb_syncmem(&sqtd->dma, sqtd->offs, sizeof(sqtd->qtd),
2955 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2956 
2957 		sqtd->len = curlen;
2958 
2959 		DPRINTF("    va %#jx pa %#jx len %jd", (uintptr_t)va,
2960 		    (uintptr_t)DMAADDR(&xfer->ux_dmabuf, curoffs), curlen, 0);
2961 
2962 		ehci_append_sqtd(sqtd, prev);
2963 
2964 		if (((curlen + mps - 1) / mps) & 1) {
2965 			tog ^= 1;
2966 		}
2967 
2968 		curoffs += curlen;
2969 		len -= curlen;
2970 	}
2971 	KASSERTMSG(len == 0, "xfer %p olen %d len %d mps %d ex_nsqtd %zu j %zu",
2972 	    xfer, length, len, mps, exfer->ex_nsqtd, j);
2973 
2974 	if (!isread &&
2975 	    (flags & USBD_FORCE_SHORT_XFER) &&
2976 	    length % mps == 0) {
2977 		/* Force a 0 length transfer at the end. */
2978 
2979 		KASSERTMSG(j < exfer->ex_nsqtd, "j=%zu nsqtd=%zu", j,
2980 		    exfer->ex_nsqtd);
2981 		prev = sqtd;
2982 		sqtd = exfer->ex_sqtds[j++];
2983 		memset(&sqtd->qtd, 0, sizeof(sqtd->qtd));
2984 		sqtd->qtd.qtd_next = sqtd->qtd.qtd_altnext = EHCI_NULL;
2985 		sqtd->qtd.qtd_status = htole32(
2986 		    qtdstatus |
2987 		    EHCI_QTD_SET_BYTES(0) |
2988 		    EHCI_QTD_SET_TOGGLE(tog));
2989 
2990 		usb_syncmem(&sqtd->dma, sqtd->offs, sizeof(sqtd->qtd),
2991 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2992 
2993 		ehci_append_sqtd(sqtd, prev);
2994 		tog ^= 1;
2995 	}
2996 
2997 	*lsqtd = sqtd;
2998 	*toggle = tog;
2999 }
3000 
3001 Static ehci_soft_itd_t *
3002 ehci_alloc_itd(ehci_softc_t *sc)
3003 {
3004 	struct ehci_soft_itd *itd, *freeitd;
3005 	usbd_status err;
3006 	usb_dma_t dma;
3007 
3008 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
3009 
3010 	mutex_enter(&sc->sc_lock);
3011 
3012 	freeitd = LIST_FIRST(&sc->sc_freeitds);
3013 	if (freeitd == NULL) {
3014 		DPRINTF("allocating chunk", 0, 0, 0, 0);
3015 		mutex_exit(&sc->sc_lock);
3016 		err = usb_allocmem(&sc->sc_bus, EHCI_ITD_SIZE * EHCI_ITD_CHUNK,
3017 				EHCI_PAGE_SIZE, &dma);
3018 
3019 		if (err) {
3020 			DPRINTF("alloc returned %jd", err, 0, 0, 0);
3021 			return NULL;
3022 		}
3023 		mutex_enter(&sc->sc_lock);
3024 
3025 		for (int i = 0; i < EHCI_ITD_CHUNK; i++) {
3026 			int offs = i * EHCI_ITD_SIZE;
3027 			itd = KERNADDR(&dma, offs);
3028 			itd->physaddr = DMAADDR(&dma, offs);
3029 	 		itd->dma = dma;
3030 			itd->offs = offs;
3031 			LIST_INSERT_HEAD(&sc->sc_freeitds, itd, free_list);
3032 		}
3033 		freeitd = LIST_FIRST(&sc->sc_freeitds);
3034 	}
3035 
3036 	itd = freeitd;
3037 	LIST_REMOVE(itd, free_list);
3038 	mutex_exit(&sc->sc_lock);
3039 	memset(&itd->itd, 0, sizeof(ehci_itd_t));
3040 
3041 	itd->frame_list.next = NULL;
3042 	itd->frame_list.prev = NULL;
3043 	itd->xfer_next = NULL;
3044 	itd->slot = 0;
3045 
3046 	return itd;
3047 }
3048 
3049 Static ehci_soft_sitd_t *
3050 ehci_alloc_sitd(ehci_softc_t *sc)
3051 {
3052 	struct ehci_soft_sitd *sitd, *freesitd;
3053 	usbd_status err;
3054 	int i, offs;
3055 	usb_dma_t dma;
3056 
3057 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
3058 
3059 	mutex_enter(&sc->sc_lock);
3060 	freesitd = LIST_FIRST(&sc->sc_freesitds);
3061 	if (freesitd == NULL) {
3062 		DPRINTF("allocating chunk", 0, 0, 0, 0);
3063 		mutex_exit(&sc->sc_lock);
3064 		err = usb_allocmem(&sc->sc_bus, EHCI_SITD_SIZE * EHCI_SITD_CHUNK,
3065 				EHCI_PAGE_SIZE, &dma);
3066 
3067 		if (err) {
3068 			DPRINTF("alloc returned %jd", err, 0, 0,
3069 			    0);
3070 			return NULL;
3071 		}
3072 
3073 		mutex_enter(&sc->sc_lock);
3074 		for (i = 0; i < EHCI_SITD_CHUNK; i++) {
3075 			offs = i * EHCI_SITD_SIZE;
3076 			sitd = KERNADDR(&dma, offs);
3077 			sitd->physaddr = DMAADDR(&dma, offs);
3078 	 		sitd->dma = dma;
3079 			sitd->offs = offs;
3080 			LIST_INSERT_HEAD(&sc->sc_freesitds, sitd, free_list);
3081 		}
3082 		freesitd = LIST_FIRST(&sc->sc_freesitds);
3083 	}
3084 
3085 	sitd = freesitd;
3086 	LIST_REMOVE(sitd, free_list);
3087 	mutex_exit(&sc->sc_lock);
3088 
3089 	memset(&sitd->sitd, 0, sizeof(ehci_sitd_t));
3090 
3091 	sitd->frame_list.next = NULL;
3092 	sitd->frame_list.prev = NULL;
3093 	sitd->xfer_next = NULL;
3094 	sitd->slot = 0;
3095 
3096 	return sitd;
3097 }
3098 
3099 /****************/
3100 
3101 /*
3102  * Close a reqular pipe.
3103  * Assumes that there are no pending transactions.
3104  */
3105 Static void
3106 ehci_close_pipe(struct usbd_pipe *pipe, ehci_soft_qh_t *head)
3107 {
3108 	struct ehci_pipe *epipe = EHCI_PIPE2EPIPE(pipe);
3109 	ehci_softc_t *sc = EHCI_PIPE2SC(pipe);
3110 	ehci_soft_qh_t *sqh = epipe->sqh;
3111 
3112 	KASSERT(mutex_owned(&sc->sc_lock));
3113 
3114 	ehci_rem_qh(sc, sqh, head);
3115 	ehci_free_sqh(sc, epipe->sqh);
3116 }
3117 
3118 /*
3119  * Abort a device request.
3120  * If this routine is called at splusb() it guarantees that the request
3121  * will be removed from the hardware scheduling and that the callback
3122  * for it will be called with USBD_CANCELLED status.
3123  * It's impossible to guarantee that the requested transfer will not
3124  * have happened since the hardware runs concurrently.
3125  * If the transaction has already happened we rely on the ordinary
3126  * interrupt processing to process it.
3127  * XXX This is most probably wrong.
3128  * XXXMRG this doesn't make sense anymore.
3129  */
3130 Static void
3131 ehci_abort_xfer(struct usbd_xfer *xfer, usbd_status status)
3132 {
3133 	struct ehci_pipe *epipe = EHCI_XFER2EPIPE(xfer);
3134 	struct ehci_xfer *exfer = EHCI_XFER2EXFER(xfer);
3135 	ehci_softc_t *sc = EHCI_XFER2SC(xfer);
3136 	ehci_soft_qh_t *sqh = epipe->sqh;
3137 	ehci_soft_qtd_t *sqtd, *fsqtd, *lsqtd;
3138 	ehci_physaddr_t cur;
3139 	uint32_t qhstatus;
3140 	int hit;
3141 	int wake;
3142 
3143 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
3144 
3145 	DPRINTF("xfer=%#jx pipe=%#jx", (uintptr_t)xfer, (uintptr_t)epipe, 0, 0);
3146 
3147 	KASSERT(mutex_owned(&sc->sc_lock));
3148 	ASSERT_SLEEPABLE();
3149 
3150 	if (sc->sc_dying) {
3151 		/* If we're dying, just do the software part. */
3152 		xfer->ux_status = status;	/* make software ignore it */
3153 		callout_stop(&xfer->ux_callout);
3154 		usb_transfer_complete(xfer);
3155 		return;
3156 	}
3157 
3158 	/*
3159 	 * If an abort is already in progress then just wait for it to
3160 	 * complete and return.
3161 	 */
3162 	if (xfer->ux_hcflags & UXFER_ABORTING) {
3163 		DPRINTF("already aborting", 0, 0, 0, 0);
3164 #ifdef DIAGNOSTIC
3165 		if (status == USBD_TIMEOUT)
3166 			printf("ehci_abort_xfer: TIMEOUT while aborting\n");
3167 #endif
3168 		/* Override the status which might be USBD_TIMEOUT. */
3169 		xfer->ux_status = status;
3170 		DPRINTF("waiting for abort to finish", 0, 0, 0, 0);
3171 		xfer->ux_hcflags |= UXFER_ABORTWAIT;
3172 		while (xfer->ux_hcflags & UXFER_ABORTING)
3173 			cv_wait(&xfer->ux_hccv, &sc->sc_lock);
3174 		return;
3175 	}
3176 	xfer->ux_hcflags |= UXFER_ABORTING;
3177 
3178 	/*
3179 	 * Step 1: Make interrupt routine and hardware ignore xfer.
3180 	 */
3181 	xfer->ux_status = status;	/* make software ignore it */
3182 	callout_stop(&xfer->ux_callout);
3183 	ehci_del_intr_list(sc, exfer);
3184 
3185 	usb_syncmem(&sqh->dma,
3186 	    sqh->offs + offsetof(ehci_qh_t, qh_qtd.qtd_status),
3187 	    sizeof(sqh->qh.qh_qtd.qtd_status),
3188 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
3189 	qhstatus = sqh->qh.qh_qtd.qtd_status;
3190 	sqh->qh.qh_qtd.qtd_status = qhstatus | htole32(EHCI_QTD_HALTED);
3191 	usb_syncmem(&sqh->dma,
3192 	    sqh->offs + offsetof(ehci_qh_t, qh_qtd.qtd_status),
3193 	    sizeof(sqh->qh.qh_qtd.qtd_status),
3194 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
3195 
3196 	if (exfer->ex_type == EX_CTRL) {
3197 		fsqtd = exfer->ex_setup;
3198 		lsqtd = exfer->ex_status;
3199 	} else {
3200 		fsqtd = exfer->ex_sqtdstart;
3201 		lsqtd = exfer->ex_sqtdend;
3202 	}
3203 	for (sqtd = fsqtd; ; sqtd = sqtd->nextqtd) {
3204 		usb_syncmem(&sqtd->dma,
3205 		    sqtd->offs + offsetof(ehci_qtd_t, qtd_status),
3206 		    sizeof(sqtd->qtd.qtd_status),
3207 		    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
3208 		sqtd->qtd.qtd_status |= htole32(EHCI_QTD_HALTED);
3209 		usb_syncmem(&sqtd->dma,
3210 		    sqtd->offs + offsetof(ehci_qtd_t, qtd_status),
3211 		    sizeof(sqtd->qtd.qtd_status),
3212 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
3213 		if (sqtd == lsqtd)
3214 			break;
3215 	}
3216 
3217 	/*
3218 	 * Step 2: Wait until we know hardware has finished any possible
3219 	 * use of the xfer.  Also make sure the soft interrupt routine
3220 	 * has run.
3221 	 */
3222 	ehci_sync_hc(sc);
3223 	sc->sc_softwake = 1;
3224 	usb_schedsoftintr(&sc->sc_bus);
3225 	cv_wait(&sc->sc_softwake_cv, &sc->sc_lock);
3226 
3227 	/*
3228 	 * Step 3: Remove any vestiges of the xfer from the hardware.
3229 	 * The complication here is that the hardware may have executed
3230 	 * beyond the xfer we're trying to abort.  So as we're scanning
3231 	 * the TDs of this xfer we check if the hardware points to
3232 	 * any of them.
3233 	 */
3234 
3235 	usb_syncmem(&sqh->dma,
3236 	    sqh->offs + offsetof(ehci_qh_t, qh_curqtd),
3237 	    sizeof(sqh->qh.qh_curqtd),
3238 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
3239 	cur = EHCI_LINK_ADDR(le32toh(sqh->qh.qh_curqtd));
3240 	hit = 0;
3241 	for (sqtd = fsqtd; ; sqtd = sqtd->nextqtd) {
3242 		hit |= cur == sqtd->physaddr;
3243 		if (sqtd == lsqtd)
3244 			break;
3245 	}
3246 	sqtd = sqtd->nextqtd;
3247 	/* Zap curqtd register if hardware pointed inside the xfer. */
3248 	if (hit && sqtd != NULL) {
3249 		DPRINTF("cur=0x%08jx", sqtd->physaddr, 0, 0, 0);
3250 		sqh->qh.qh_curqtd = htole32(sqtd->physaddr); /* unlink qTDs */
3251 		usb_syncmem(&sqh->dma,
3252 		    sqh->offs + offsetof(ehci_qh_t, qh_curqtd),
3253 		    sizeof(sqh->qh.qh_curqtd),
3254 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
3255 		sqh->qh.qh_qtd.qtd_status = qhstatus;
3256 		usb_syncmem(&sqh->dma,
3257 		    sqh->offs + offsetof(ehci_qh_t, qh_qtd.qtd_status),
3258 		    sizeof(sqh->qh.qh_qtd.qtd_status),
3259 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
3260 	} else {
3261 		DPRINTF("no hit", 0, 0, 0, 0);
3262 		usb_syncmem(&sqh->dma,
3263 		    sqh->offs + offsetof(ehci_qh_t, qh_curqtd),
3264 		    sizeof(sqh->qh.qh_curqtd),
3265 		    BUS_DMASYNC_PREREAD);
3266 	}
3267 
3268 	/*
3269 	 * Step 4: Execute callback.
3270 	 */
3271 #ifdef DIAGNOSTIC
3272 	exfer->ex_isdone = true;
3273 #endif
3274 	wake = xfer->ux_hcflags & UXFER_ABORTWAIT;
3275 	xfer->ux_hcflags &= ~(UXFER_ABORTING | UXFER_ABORTWAIT);
3276 	usb_transfer_complete(xfer);
3277 	if (wake) {
3278 		cv_broadcast(&xfer->ux_hccv);
3279 	}
3280 
3281 	KASSERT(mutex_owned(&sc->sc_lock));
3282 }
3283 
3284 Static void
3285 ehci_abort_isoc_xfer(struct usbd_xfer *xfer, usbd_status status)
3286 {
3287 	ehci_isoc_trans_t trans_status;
3288 	struct ehci_xfer *exfer;
3289 	ehci_softc_t *sc;
3290 	struct ehci_soft_itd *itd;
3291 	struct ehci_soft_sitd *sitd;
3292 	int i, wake;
3293 
3294 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
3295 
3296 	exfer = EHCI_XFER2EXFER(xfer);
3297 	sc = EHCI_XFER2SC(xfer);
3298 
3299 	DPRINTF("xfer %#jx pipe %#jx", (uintptr_t)xfer,
3300 	    (uintptr_t)xfer->ux_pipe, 0, 0);
3301 
3302 	KASSERT(mutex_owned(&sc->sc_lock));
3303 
3304 	if (sc->sc_dying) {
3305 		xfer->ux_status = status;
3306 		callout_stop(&xfer->ux_callout);
3307 		usb_transfer_complete(xfer);
3308 		return;
3309 	}
3310 
3311 	if (xfer->ux_hcflags & UXFER_ABORTING) {
3312 		DPRINTF("already aborting", 0, 0, 0, 0);
3313 
3314 #ifdef DIAGNOSTIC
3315 		if (status == USBD_TIMEOUT)
3316 			printf("ehci_abort_isoc_xfer: TIMEOUT while aborting\n");
3317 #endif
3318 
3319 		xfer->ux_status = status;
3320 		DPRINTF("waiting for abort to finish", 0, 0, 0, 0);
3321 		xfer->ux_hcflags |= UXFER_ABORTWAIT;
3322 		while (xfer->ux_hcflags & UXFER_ABORTING)
3323 			cv_wait(&xfer->ux_hccv, &sc->sc_lock);
3324 		goto done;
3325 	}
3326 	xfer->ux_hcflags |= UXFER_ABORTING;
3327 
3328 	xfer->ux_status = status;
3329 	callout_stop(&xfer->ux_callout);
3330 	ehci_del_intr_list(sc, exfer);
3331 
3332 	if (xfer->ux_pipe->up_dev->ud_speed == USB_SPEED_HIGH) {
3333 		for (itd = exfer->ex_itdstart; itd != NULL;
3334 		     itd = itd->xfer_next) {
3335 			usb_syncmem(&itd->dma,
3336 			    itd->offs + offsetof(ehci_itd_t, itd_ctl),
3337 			    sizeof(itd->itd.itd_ctl),
3338 			    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
3339 
3340 			for (i = 0; i < 8; i++) {
3341 				trans_status = le32toh(itd->itd.itd_ctl[i]);
3342 				trans_status &= ~EHCI_ITD_ACTIVE;
3343 				itd->itd.itd_ctl[i] = htole32(trans_status);
3344 			}
3345 
3346 			usb_syncmem(&itd->dma,
3347 			    itd->offs + offsetof(ehci_itd_t, itd_ctl),
3348 			    sizeof(itd->itd.itd_ctl),
3349 			    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
3350 		}
3351 	} else {
3352 		for (sitd = exfer->ex_sitdstart; sitd != NULL;
3353 		     sitd = sitd->xfer_next) {
3354 			usb_syncmem(&sitd->dma,
3355 			    sitd->offs + offsetof(ehci_sitd_t, sitd_buffer),
3356 			    sizeof(sitd->sitd.sitd_buffer),
3357 			    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
3358 
3359 			trans_status = le32toh(sitd->sitd.sitd_trans);
3360 			trans_status &= ~EHCI_SITD_ACTIVE;
3361 			sitd->sitd.sitd_trans = htole32(trans_status);
3362 
3363 			usb_syncmem(&sitd->dma,
3364 			    sitd->offs + offsetof(ehci_sitd_t, sitd_buffer),
3365 			    sizeof(sitd->sitd.sitd_buffer),
3366 			    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
3367 		}
3368 	}
3369 
3370 	sc->sc_softwake = 1;
3371 	usb_schedsoftintr(&sc->sc_bus);
3372 	cv_wait(&sc->sc_softwake_cv, &sc->sc_lock);
3373 
3374 #ifdef DIAGNOSTIC
3375 	exfer->ex_isdone = true;
3376 #endif
3377 	wake = xfer->ux_hcflags & UXFER_ABORTWAIT;
3378 	xfer->ux_hcflags &= ~(UXFER_ABORTING | UXFER_ABORTWAIT);
3379 	usb_transfer_complete(xfer);
3380 	if (wake) {
3381 		cv_broadcast(&xfer->ux_hccv);
3382 	}
3383 
3384 done:
3385 	KASSERT(mutex_owned(&sc->sc_lock));
3386 	return;
3387 }
3388 
3389 Static void
3390 ehci_timeout(void *addr)
3391 {
3392 	struct usbd_xfer *xfer = addr;
3393 	struct ehci_xfer *exfer = EHCI_XFER2EXFER(xfer);
3394 	struct usbd_pipe *pipe = xfer->ux_pipe;
3395 	struct usbd_device *dev = pipe->up_dev;
3396 	ehci_softc_t *sc = EHCI_XFER2SC(xfer);
3397 
3398 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
3399 
3400 	DPRINTF("exfer %#jx", (uintptr_t)exfer, 0, 0, 0);
3401 #ifdef EHCI_DEBUG
3402 	if (ehcidebug >= 2)
3403 		usbd_dump_pipe(pipe);
3404 #endif
3405 
3406 	if (sc->sc_dying) {
3407 		mutex_enter(&sc->sc_lock);
3408 		ehci_abort_xfer(xfer, USBD_TIMEOUT);
3409 		mutex_exit(&sc->sc_lock);
3410 		return;
3411 	}
3412 
3413 	/* Execute the abort in a process context. */
3414 	usb_init_task(&exfer->ex_aborttask, ehci_timeout_task, xfer,
3415 	    USB_TASKQ_MPSAFE);
3416 	usb_add_task(dev, &exfer->ex_aborttask, USB_TASKQ_HC);
3417 }
3418 
3419 Static void
3420 ehci_timeout_task(void *addr)
3421 {
3422 	struct usbd_xfer *xfer = addr;
3423 	ehci_softc_t *sc = EHCI_XFER2SC(xfer);
3424 
3425 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
3426 
3427 	DPRINTF("xfer=%#jx", (uintptr_t)xfer, 0, 0, 0);
3428 
3429 	mutex_enter(&sc->sc_lock);
3430 	ehci_abort_xfer(xfer, USBD_TIMEOUT);
3431 	mutex_exit(&sc->sc_lock);
3432 }
3433 
3434 /************************/
3435 
3436 Static int
3437 ehci_device_ctrl_init(struct usbd_xfer *xfer)
3438 {
3439 	struct ehci_xfer *exfer = EHCI_XFER2EXFER(xfer);
3440 	struct ehci_pipe *epipe = EHCI_XFER2EPIPE(xfer);
3441 	ehci_softc_t *sc = EHCI_XFER2SC(xfer);
3442 	usb_device_request_t *req = &xfer->ux_request;
3443 	ehci_soft_qtd_t *setup, *status, *next;
3444 	int isread = req->bmRequestType & UT_READ;
3445 	int len = xfer->ux_bufsize;
3446 	int err;
3447 
3448 	exfer->ex_type = EX_CTRL;
3449 	exfer->ex_status = NULL;
3450 	exfer->ex_data = NULL;
3451 	exfer->ex_setup = ehci_alloc_sqtd(sc);
3452 	if (exfer->ex_setup == NULL) {
3453 		err = ENOMEM;
3454 		goto bad1;
3455 	}
3456 	exfer->ex_status = ehci_alloc_sqtd(sc);
3457 	if (exfer->ex_status == NULL) {
3458 		err = ENOMEM;
3459 		goto bad2;
3460 	}
3461 	setup = exfer->ex_setup;
3462 	status = exfer->ex_status;
3463 	exfer->ex_nsqtd = 0;
3464 	next = status;
3465 	/* Set up data transaction */
3466 	if (len != 0) {
3467 		err = ehci_alloc_sqtd_chain(sc, xfer, len, isread,
3468 		    &exfer->ex_data);
3469 		if (err)
3470 			goto bad3;
3471 		next = exfer->ex_data;
3472 	}
3473 
3474 	/* Clear toggle */
3475 	setup->qtd.qtd_status = htole32(
3476 	    EHCI_QTD_SET_PID(EHCI_QTD_PID_SETUP) |
3477 	    EHCI_QTD_SET_TOGGLE(0) |
3478 	    EHCI_QTD_SET_BYTES(sizeof(*req))
3479 	    );
3480 	setup->qtd.qtd_buffer[0] = htole32(DMAADDR(&epipe->ctrl.reqdma, 0));
3481 	setup->qtd.qtd_buffer_hi[0] = 0;
3482 	setup->qtd.qtd_next = setup->qtd.qtd_altnext = htole32(next->physaddr);
3483 	setup->nextqtd = next;
3484 	setup->xfer = xfer;
3485 	setup->len = sizeof(*req);
3486 
3487 	status->qtd.qtd_status = htole32(
3488 	    EHCI_QTD_SET_PID(isread ? EHCI_QTD_PID_OUT : EHCI_QTD_PID_IN) |
3489 	    EHCI_QTD_SET_TOGGLE(1) |
3490 	    EHCI_QTD_IOC
3491 	    );
3492 	status->qtd.qtd_buffer[0] = 0;
3493 	status->qtd.qtd_buffer_hi[0] = 0;
3494 	status->qtd.qtd_next = status->qtd.qtd_altnext = EHCI_NULL;
3495 	status->nextqtd = NULL;
3496 	status->xfer = xfer;
3497 	status->len = 0;
3498 
3499 	return 0;
3500 bad3:
3501 	ehci_free_sqtd(sc, exfer->ex_status);
3502 bad2:
3503 	ehci_free_sqtd(sc, exfer->ex_setup);
3504 bad1:
3505 	return err;
3506 }
3507 
3508 Static void
3509 ehci_device_ctrl_fini(struct usbd_xfer *xfer)
3510 {
3511 	ehci_softc_t *sc = EHCI_XFER2SC(xfer);
3512 	struct ehci_xfer *ex = EHCI_XFER2EXFER(xfer);
3513 
3514 	KASSERT(ex->ex_type == EX_CTRL);
3515 
3516 	ehci_free_sqtd(sc, ex->ex_setup);
3517 	ehci_free_sqtd(sc, ex->ex_status);
3518 	ehci_free_sqtds(sc, ex);
3519 	if (ex->ex_nsqtd)
3520 		kmem_free(ex->ex_sqtds,
3521 		    sizeof(ehci_soft_qtd_t *) * ex->ex_nsqtd);
3522 }
3523 
3524 Static usbd_status
3525 ehci_device_ctrl_transfer(struct usbd_xfer *xfer)
3526 {
3527 	ehci_softc_t *sc = EHCI_XFER2SC(xfer);
3528 	usbd_status err;
3529 
3530 	/* Insert last in queue. */
3531 	mutex_enter(&sc->sc_lock);
3532 	err = usb_insert_transfer(xfer);
3533 	mutex_exit(&sc->sc_lock);
3534 	if (err)
3535 		return err;
3536 
3537 	/* Pipe isn't running, start first */
3538 	return ehci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
3539 }
3540 
3541 Static usbd_status
3542 ehci_device_ctrl_start(struct usbd_xfer *xfer)
3543 {
3544 	struct ehci_pipe *epipe = EHCI_XFER2EPIPE(xfer);
3545 	struct ehci_xfer *exfer = EHCI_XFER2EXFER(xfer);
3546 	usb_device_request_t *req = &xfer->ux_request;
3547 	ehci_softc_t *sc = EHCI_XFER2SC(xfer);
3548 	ehci_soft_qtd_t *setup, *status, *next;
3549 	ehci_soft_qh_t *sqh;
3550 
3551 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
3552 
3553 	KASSERT(xfer->ux_rqflags & URQ_REQUEST);
3554 
3555 	if (sc->sc_dying)
3556 		return USBD_IOERROR;
3557 
3558 	const int isread = req->bmRequestType & UT_READ;
3559 	const int len = UGETW(req->wLength);
3560 
3561 	DPRINTF("type=0x%02jx, request=0x%02jx, wValue=0x%04jx, wIndex=0x%04jx",
3562 	    req->bmRequestType, req->bRequest, UGETW(req->wValue),
3563 	    UGETW(req->wIndex));
3564 	DPRINTF("len=%jd, addr=%jd, endpt=%jd",
3565 	    len, epipe->pipe.up_dev->ud_addr,
3566 	    epipe->pipe.up_endpoint->ue_edesc->bEndpointAddress, 0);
3567 
3568 	sqh = epipe->sqh;
3569 
3570 	KASSERTMSG(EHCI_QH_GET_ADDR(le32toh(sqh->qh.qh_endp)) == epipe->pipe.up_dev->ud_addr,
3571 	    "address QH %" __PRIuBIT " pipe %d\n",
3572 	    EHCI_QH_GET_ADDR(le32toh(sqh->qh.qh_endp)),
3573 	    epipe->pipe.up_dev->ud_addr);
3574 	KASSERTMSG(EHCI_QH_GET_MPL(le32toh(sqh->qh.qh_endp)) ==
3575 	    UGETW(epipe->pipe.up_endpoint->ue_edesc->wMaxPacketSize),
3576 	    "MPS QH %" __PRIuBIT " pipe %d\n",
3577 	    EHCI_QH_GET_MPL(le32toh(sqh->qh.qh_endp)),
3578 	    UGETW(epipe->pipe.up_endpoint->ue_edesc->wMaxPacketSize));
3579 
3580 	setup = exfer->ex_setup;
3581 	status = exfer->ex_status;
3582 
3583 	DPRINTF("setup %#jx status %#jx data %#jx",
3584 	    (uintptr_t)setup, (uintptr_t)status, (uintptr_t)exfer->ex_data, 0);
3585 	KASSERTMSG(setup != NULL && status != NULL,
3586 	    "Failed memory allocation, setup %p status %p",
3587 	    setup, status);
3588 
3589 	memcpy(KERNADDR(&epipe->ctrl.reqdma, 0), req, sizeof(*req));
3590 	usb_syncmem(&epipe->ctrl.reqdma, 0, sizeof(*req), BUS_DMASYNC_PREWRITE);
3591 
3592 	/* Clear toggle */
3593 	setup->qtd.qtd_status &= ~htole32(
3594 	    EHCI_QTD_STATUS_MASK |
3595 	    EHCI_QTD_BYTES_MASK |
3596 	    EHCI_QTD_TOGGLE_MASK |
3597 	    EHCI_QTD_CERR_MASK
3598 	    );
3599 	setup->qtd.qtd_status |= htole32(
3600 	    EHCI_QTD_ACTIVE |
3601 	    EHCI_QTD_SET_CERR(3) |
3602 	    EHCI_QTD_SET_TOGGLE(0) |
3603 	    EHCI_QTD_SET_BYTES(sizeof(*req))
3604 	    );
3605 	setup->qtd.qtd_buffer[0] = htole32(DMAADDR(&epipe->ctrl.reqdma, 0));
3606 	setup->qtd.qtd_buffer_hi[0] = 0;
3607 
3608 	next = status;
3609 	status->qtd.qtd_status &= ~htole32(
3610 	    EHCI_QTD_STATUS_MASK |
3611 	    EHCI_QTD_PID_MASK |
3612 	    EHCI_QTD_BYTES_MASK |
3613 	    EHCI_QTD_TOGGLE_MASK |
3614 	    EHCI_QTD_CERR_MASK
3615 	    );
3616 	status->qtd.qtd_status |= htole32(
3617 	    EHCI_QTD_ACTIVE |
3618 	    EHCI_QTD_SET_PID(isread ? EHCI_QTD_PID_OUT : EHCI_QTD_PID_IN) |
3619 	    EHCI_QTD_SET_CERR(3) |
3620 	    EHCI_QTD_SET_TOGGLE(1) |
3621 	    EHCI_QTD_SET_BYTES(0) |
3622 	    EHCI_QTD_IOC
3623 	    );
3624 	KASSERT(status->qtd.qtd_status & htole32(EHCI_QTD_TOGGLE_MASK));
3625 
3626 	KASSERT(exfer->ex_isdone);
3627 #ifdef DIAGNOSTIC
3628 	exfer->ex_isdone = false;
3629 #endif
3630 
3631 	/* Set up data transaction */
3632 	if (len != 0) {
3633 		ehci_soft_qtd_t *end;
3634 
3635 		/* Start toggle at 1. */
3636 		int toggle = 1;
3637 		next = exfer->ex_data;
3638 		KASSERTMSG(next != NULL, "Failed memory allocation");
3639 		ehci_reset_sqtd_chain(sc, xfer, len, isread, &toggle, &end);
3640 		end->nextqtd = status;
3641 		end->qtd.qtd_next = end->qtd.qtd_altnext =
3642 		    htole32(status->physaddr);
3643 
3644 		usb_syncmem(&end->dma, end->offs, sizeof(end->qtd),
3645 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
3646 
3647 		usb_syncmem(&xfer->ux_dmabuf, 0, len,
3648 		    isread ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
3649 	}
3650 
3651 	setup->nextqtd = next;
3652 	setup->qtd.qtd_next = setup->qtd.qtd_altnext = htole32(next->physaddr);
3653 
3654 	usb_syncmem(&setup->dma, setup->offs, sizeof(setup->qtd),
3655 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
3656 
3657 	 usb_syncmem(&status->dma, status->offs, sizeof(status->qtd),
3658 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
3659 
3660 	KASSERT(status->qtd.qtd_status & htole32(EHCI_QTD_TOGGLE_MASK));
3661 
3662 #ifdef EHCI_DEBUG
3663 	DPRINTFN(5, "--- dump start ---", 0, 0, 0, 0);
3664 	ehci_dump_sqh(sqh);
3665 	ehci_dump_sqtds(setup);
3666 	DPRINTFN(5, "--- dump end ---", 0, 0, 0, 0);
3667 #endif
3668 
3669 	mutex_enter(&sc->sc_lock);
3670 
3671 	/* Insert qTD in QH list - also does usb_syncmem(sqh) */
3672 	ehci_set_qh_qtd(sqh, setup);
3673 	if (xfer->ux_timeout && !sc->sc_bus.ub_usepolling) {
3674 		callout_reset(&xfer->ux_callout, mstohz(xfer->ux_timeout),
3675 		    ehci_timeout, xfer);
3676 	}
3677 	ehci_add_intr_list(sc, exfer);
3678 	xfer->ux_status = USBD_IN_PROGRESS;
3679 	mutex_exit(&sc->sc_lock);
3680 
3681 #if 0
3682 #ifdef EHCI_DEBUG
3683 	DPRINTFN(10, "status=%jx, dump:", EOREAD4(sc, EHCI_USBSTS), 0, 0, 0);
3684 //	delay(10000);
3685 	ehci_dump_regs(sc);
3686 	ehci_dump_sqh(sc->sc_async_head);
3687 	ehci_dump_sqh(sqh);
3688 	ehci_dump_sqtds(setup);
3689 #endif
3690 #endif
3691 
3692 	return USBD_IN_PROGRESS;
3693 }
3694 
3695 Static void
3696 ehci_device_ctrl_done(struct usbd_xfer *xfer)
3697 {
3698 	ehci_softc_t *sc __diagused = EHCI_XFER2SC(xfer);
3699 	struct ehci_pipe *epipe = EHCI_XFER2EPIPE(xfer);
3700 	usb_device_request_t *req = &xfer->ux_request;
3701 	int len = UGETW(req->wLength);
3702 	int rd = req->bmRequestType & UT_READ;
3703 
3704 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
3705 	DPRINTF("xfer=%#jx", (uintptr_t)xfer, 0, 0, 0);
3706 
3707 	KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
3708 	KASSERT(xfer->ux_rqflags & URQ_REQUEST);
3709 
3710 	usb_syncmem(&epipe->ctrl.reqdma, 0, sizeof(*req),
3711 	    BUS_DMASYNC_POSTWRITE);
3712 	if (len)
3713 		usb_syncmem(&xfer->ux_dmabuf, 0, len,
3714 		    rd ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
3715 
3716 	DPRINTF("length=%jd", xfer->ux_actlen, 0, 0, 0);
3717 }
3718 
3719 /* Abort a device control request. */
3720 Static void
3721 ehci_device_ctrl_abort(struct usbd_xfer *xfer)
3722 {
3723 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
3724 
3725 	DPRINTF("xfer=%#jx", (uintptr_t)xfer, 0, 0, 0);
3726 	ehci_abort_xfer(xfer, USBD_CANCELLED);
3727 }
3728 
3729 /* Close a device control pipe. */
3730 Static void
3731 ehci_device_ctrl_close(struct usbd_pipe *pipe)
3732 {
3733 	ehci_softc_t *sc = EHCI_PIPE2SC(pipe);
3734 	/*struct ehci_pipe *epipe = EHCI_PIPE2EPIPE(pipe);*/
3735 
3736 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
3737 
3738 	KASSERT(mutex_owned(&sc->sc_lock));
3739 
3740 	DPRINTF("pipe=%#jx", (uintptr_t)pipe, 0, 0, 0);
3741 
3742 	ehci_close_pipe(pipe, sc->sc_async_head);
3743 }
3744 
3745 /*
3746  * Some EHCI chips from VIA seem to trigger interrupts before writing back the
3747  * qTD status, or miss signalling occasionally under heavy load.  If the host
3748  * machine is too fast, we we can miss transaction completion - when we scan
3749  * the active list the transaction still seems to be active.  This generally
3750  * exhibits itself as a umass stall that never recovers.
3751  *
3752  * We work around this behaviour by setting up this callback after any softintr
3753  * that completes with transactions still pending, giving us another chance to
3754  * check for completion after the writeback has taken place.
3755  */
3756 Static void
3757 ehci_intrlist_timeout(void *arg)
3758 {
3759 	ehci_softc_t *sc = arg;
3760 
3761 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
3762 
3763 	usb_schedsoftintr(&sc->sc_bus);
3764 }
3765 
3766 /************************/
3767 
3768 Static int
3769 ehci_device_bulk_init(struct usbd_xfer *xfer)
3770 {
3771 	ehci_softc_t *sc = EHCI_XFER2SC(xfer);
3772 	struct ehci_xfer *exfer = EHCI_XFER2EXFER(xfer);
3773 	usb_endpoint_descriptor_t *ed = xfer->ux_pipe->up_endpoint->ue_edesc;
3774 	int endpt = ed->bEndpointAddress;
3775 	int isread = UE_GET_DIR(endpt) == UE_DIR_IN;
3776 	int len = xfer->ux_bufsize;
3777 	int err = 0;
3778 
3779 	exfer->ex_type = EX_BULK;
3780 	exfer->ex_nsqtd = 0;
3781 	err = ehci_alloc_sqtd_chain(sc, xfer, len, isread,
3782 	    &exfer->ex_sqtdstart);
3783 
3784 	return err;
3785 }
3786 
3787 Static void
3788 ehci_device_bulk_fini(struct usbd_xfer *xfer)
3789 {
3790 	ehci_softc_t *sc = EHCI_XFER2SC(xfer);
3791 	struct ehci_xfer *ex = EHCI_XFER2EXFER(xfer);
3792 
3793 	KASSERT(ex->ex_type == EX_BULK);
3794 
3795 	ehci_free_sqtds(sc, ex);
3796 	if (ex->ex_nsqtd)
3797 		kmem_free(ex->ex_sqtds, sizeof(ehci_soft_qtd_t *) * ex->ex_nsqtd);
3798 }
3799 
3800 Static usbd_status
3801 ehci_device_bulk_transfer(struct usbd_xfer *xfer)
3802 {
3803 	ehci_softc_t *sc = EHCI_XFER2SC(xfer);
3804 	usbd_status err;
3805 
3806 	/* Insert last in queue. */
3807 	mutex_enter(&sc->sc_lock);
3808 	err = usb_insert_transfer(xfer);
3809 	mutex_exit(&sc->sc_lock);
3810 	if (err)
3811 		return err;
3812 
3813 	/* Pipe isn't running, start first */
3814 	return ehci_device_bulk_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
3815 }
3816 
3817 Static usbd_status
3818 ehci_device_bulk_start(struct usbd_xfer *xfer)
3819 {
3820 	struct ehci_pipe *epipe = EHCI_XFER2EPIPE(xfer);
3821 	struct ehci_xfer *exfer = EHCI_XFER2EXFER(xfer);
3822 	ehci_softc_t *sc = EHCI_XFER2SC(xfer);
3823 	ehci_soft_qh_t *sqh;
3824 	ehci_soft_qtd_t *end;
3825 	int len, isread, endpt;
3826 
3827 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
3828 
3829 	DPRINTF("xfer=%#jx len=%jd flags=%jd", (uintptr_t)xfer, xfer->ux_length,
3830 	    xfer->ux_flags, 0);
3831 
3832 	if (sc->sc_dying)
3833 		return USBD_IOERROR;
3834 
3835 	KASSERT(!(xfer->ux_rqflags & URQ_REQUEST));
3836 	KASSERT(xfer->ux_length <= xfer->ux_bufsize);
3837 
3838 	len = xfer->ux_length;
3839 	endpt = epipe->pipe.up_endpoint->ue_edesc->bEndpointAddress;
3840 	isread = UE_GET_DIR(endpt) == UE_DIR_IN;
3841 	sqh = epipe->sqh;
3842 
3843 	KASSERT(exfer->ex_isdone);
3844 #ifdef DIAGNOSTIC
3845 	exfer->ex_isdone = false;
3846 #endif
3847 
3848 	/* Take lock here to protect nexttoggle */
3849 	mutex_enter(&sc->sc_lock);
3850 
3851 	ehci_reset_sqtd_chain(sc, xfer, len, isread, &epipe->nexttoggle, &end);
3852 
3853 	exfer->ex_sqtdend = end;
3854 	end->qtd.qtd_status |= htole32(EHCI_QTD_IOC);
3855 	usb_syncmem(&end->dma, end->offs, sizeof(end->qtd),
3856 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
3857 
3858 #ifdef EHCI_DEBUG
3859 	DPRINTFN(5, "--- dump start ---", 0, 0, 0, 0);
3860 	ehci_dump_sqh(sqh);
3861 	ehci_dump_sqtds(exfer->ex_sqtdstart);
3862 	DPRINTFN(5, "--- dump end ---", 0, 0, 0, 0);
3863 #endif
3864 
3865 	usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
3866 	    isread ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
3867 
3868 	/* also does usb_syncmem(sqh) */
3869 	ehci_set_qh_qtd(sqh, exfer->ex_sqtdstart);
3870 	if (xfer->ux_timeout && !sc->sc_bus.ub_usepolling) {
3871 		callout_reset(&xfer->ux_callout, mstohz(xfer->ux_timeout),
3872 		    ehci_timeout, xfer);
3873 	}
3874 	ehci_add_intr_list(sc, exfer);
3875 	xfer->ux_status = USBD_IN_PROGRESS;
3876 	mutex_exit(&sc->sc_lock);
3877 
3878 #if 0
3879 #ifdef EHCI_DEBUG
3880 	DPRINTFN(5, "data(2)", 0, 0, 0, 0);
3881 //	delay(10000);
3882 	DPRINTFN(5, "data(3)", 0, 0, 0, 0);
3883 	ehci_dump_regs(sc);
3884 #if 0
3885 	printf("async_head:\n");
3886 	ehci_dump_sqh(sc->sc_async_head);
3887 #endif
3888 	DPRINTF("sqh:", 0, 0, 0, 0);
3889 	ehci_dump_sqh(sqh);
3890 	ehci_dump_sqtds(exfer->ex_sqtdstart);
3891 #endif
3892 #endif
3893 
3894 	return USBD_IN_PROGRESS;
3895 }
3896 
3897 Static void
3898 ehci_device_bulk_abort(struct usbd_xfer *xfer)
3899 {
3900 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
3901 
3902 	DPRINTF("xfer %#jx", (uintptr_t)xfer, 0, 0, 0);
3903 	ehci_abort_xfer(xfer, USBD_CANCELLED);
3904 }
3905 
3906 /*
3907  * Close a device bulk pipe.
3908  */
3909 Static void
3910 ehci_device_bulk_close(struct usbd_pipe *pipe)
3911 {
3912 	ehci_softc_t *sc = EHCI_PIPE2SC(pipe);
3913 	struct ehci_pipe *epipe = EHCI_PIPE2EPIPE(pipe);
3914 
3915 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
3916 
3917 	KASSERT(mutex_owned(&sc->sc_lock));
3918 
3919 	DPRINTF("pipe=%#jx", (uintptr_t)pipe, 0, 0, 0);
3920 	pipe->up_endpoint->ue_toggle = epipe->nexttoggle;
3921 	ehci_close_pipe(pipe, sc->sc_async_head);
3922 }
3923 
3924 Static void
3925 ehci_device_bulk_done(struct usbd_xfer *xfer)
3926 {
3927 	ehci_softc_t *sc __diagused = EHCI_XFER2SC(xfer);
3928 	struct ehci_pipe *epipe = EHCI_XFER2EPIPE(xfer);
3929 	int endpt = epipe->pipe.up_endpoint->ue_edesc->bEndpointAddress;
3930 	int rd = UE_GET_DIR(endpt) == UE_DIR_IN;
3931 
3932 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
3933 
3934 	DPRINTF("xfer=%#jx, actlen=%jd", (uintptr_t)xfer, xfer->ux_actlen, 0, 0);
3935 
3936 	KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
3937 
3938 	usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
3939 	    rd ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
3940 
3941 	DPRINTF("length=%jd", xfer->ux_actlen, 0, 0, 0);
3942 }
3943 
3944 /************************/
3945 
3946 Static usbd_status
3947 ehci_device_setintr(ehci_softc_t *sc, ehci_soft_qh_t *sqh, int ival)
3948 {
3949 	struct ehci_soft_islot *isp;
3950 	int islot, lev;
3951 
3952 	/* Find a poll rate that is large enough. */
3953 	for (lev = EHCI_IPOLLRATES - 1; lev > 0; lev--)
3954 		if (EHCI_ILEV_IVAL(lev) <= ival)
3955 			break;
3956 
3957 	/* Pick an interrupt slot at the right level. */
3958 	/* XXX could do better than picking at random */
3959 	sc->sc_rand = (sc->sc_rand + 191) % sc->sc_flsize;
3960 	islot = EHCI_IQHIDX(lev, sc->sc_rand);
3961 
3962 	sqh->islot = islot;
3963 	isp = &sc->sc_islots[islot];
3964 	mutex_enter(&sc->sc_lock);
3965 	ehci_add_qh(sc, sqh, isp->sqh);
3966 	mutex_exit(&sc->sc_lock);
3967 
3968 	return USBD_NORMAL_COMPLETION;
3969 }
3970 
3971 
3972 Static int
3973 ehci_device_intr_init(struct usbd_xfer *xfer)
3974 {
3975 	struct ehci_xfer *exfer = EHCI_XFER2EXFER(xfer);
3976 	ehci_softc_t *sc = EHCI_XFER2SC(xfer);
3977 	usb_endpoint_descriptor_t *ed = xfer->ux_pipe->up_endpoint->ue_edesc;
3978 	int endpt = ed->bEndpointAddress;
3979 	int isread = UE_GET_DIR(endpt) == UE_DIR_IN;
3980 	int len = xfer->ux_bufsize;
3981 	int err;
3982 
3983 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
3984 
3985 	DPRINTF("xfer=%#jx len=%jd flags=%jd", (uintptr_t)xfer, xfer->ux_length,
3986 	    xfer->ux_flags, 0);
3987 
3988 	KASSERT(!(xfer->ux_rqflags & URQ_REQUEST));
3989 	KASSERT(len != 0);
3990 
3991 	exfer->ex_type = EX_INTR;
3992 	exfer->ex_nsqtd = 0;
3993 	err = ehci_alloc_sqtd_chain(sc, xfer, len, isread,
3994 	    &exfer->ex_sqtdstart);
3995 
3996 	return err;
3997 }
3998 
3999 Static void
4000 ehci_device_intr_fini(struct usbd_xfer *xfer)
4001 {
4002 	ehci_softc_t *sc = EHCI_XFER2SC(xfer);
4003 	struct ehci_xfer *ex = EHCI_XFER2EXFER(xfer);
4004 
4005 	KASSERT(ex->ex_type == EX_INTR);
4006 
4007 	ehci_free_sqtds(sc, ex);
4008 	if (ex->ex_nsqtd)
4009 		kmem_free(ex->ex_sqtds, sizeof(ehci_soft_qtd_t *) * ex->ex_nsqtd);
4010 }
4011 
4012 Static usbd_status
4013 ehci_device_intr_transfer(struct usbd_xfer *xfer)
4014 {
4015 	ehci_softc_t *sc = EHCI_XFER2SC(xfer);
4016 	usbd_status err;
4017 
4018 	/* Insert last in queue. */
4019 	mutex_enter(&sc->sc_lock);
4020 	err = usb_insert_transfer(xfer);
4021 	mutex_exit(&sc->sc_lock);
4022 	if (err)
4023 		return err;
4024 
4025 	/*
4026 	 * Pipe isn't running (otherwise err would be USBD_INPROG),
4027 	 * so start it first.
4028 	 */
4029 	return ehci_device_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
4030 }
4031 
4032 Static usbd_status
4033 ehci_device_intr_start(struct usbd_xfer *xfer)
4034 {
4035 	struct ehci_pipe *epipe = EHCI_XFER2EPIPE(xfer);
4036 	struct ehci_xfer *exfer = EHCI_XFER2EXFER(xfer);
4037 	ehci_softc_t *sc = EHCI_XFER2SC(xfer);
4038 	ehci_soft_qtd_t *end;
4039 	ehci_soft_qh_t *sqh;
4040 	int len, isread, endpt;
4041 
4042 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
4043 
4044 	DPRINTF("xfer=%#jx len=%jd flags=%jd", (uintptr_t)xfer, xfer->ux_length,
4045 	    xfer->ux_flags, 0);
4046 
4047 	if (sc->sc_dying)
4048 		return USBD_IOERROR;
4049 
4050 	KASSERT(!(xfer->ux_rqflags & URQ_REQUEST));
4051 	KASSERT(xfer->ux_length <= xfer->ux_bufsize);
4052 
4053 	len = xfer->ux_length;
4054 	endpt = epipe->pipe.up_endpoint->ue_edesc->bEndpointAddress;
4055 	isread = UE_GET_DIR(endpt) == UE_DIR_IN;
4056 	sqh = epipe->sqh;
4057 
4058 	KASSERT(exfer->ex_isdone);
4059 #ifdef DIAGNOSTIC
4060 	exfer->ex_isdone = false;
4061 #endif
4062 
4063 	/* Take lock to protect nexttoggle */
4064 	mutex_enter(&sc->sc_lock);
4065 
4066 	ehci_reset_sqtd_chain(sc, xfer, len, isread, &epipe->nexttoggle, &end);
4067 
4068 	end->qtd.qtd_status |= htole32(EHCI_QTD_IOC);
4069 	usb_syncmem(&end->dma, end->offs, sizeof(end->qtd),
4070 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
4071 	exfer->ex_sqtdend = end;
4072 
4073 #ifdef EHCI_DEBUG
4074 	DPRINTFN(5, "--- dump start ---", 0, 0, 0, 0);
4075 	ehci_dump_sqh(sqh);
4076 	ehci_dump_sqtds(exfer->ex_sqtdstart);
4077 	DPRINTFN(5, "--- dump end ---", 0, 0, 0, 0);
4078 #endif
4079 
4080 	usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
4081 	    isread ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
4082 
4083 	/* also does usb_syncmem(sqh) */
4084 	ehci_set_qh_qtd(sqh, exfer->ex_sqtdstart);
4085 	if (xfer->ux_timeout && !sc->sc_bus.ub_usepolling) {
4086 		callout_reset(&xfer->ux_callout, mstohz(xfer->ux_timeout),
4087 		    ehci_timeout, xfer);
4088 	}
4089 	ehci_add_intr_list(sc, exfer);
4090 	xfer->ux_status = USBD_IN_PROGRESS;
4091 	mutex_exit(&sc->sc_lock);
4092 
4093 #if 0
4094 #ifdef EHCI_DEBUG
4095 	DPRINTFN(5, "data(2)", 0, 0, 0, 0);
4096 //	delay(10000);
4097 	DPRINTFN(5, "data(3)", 0, 0, 0, 0);
4098 	ehci_dump_regs(sc);
4099 	DPRINTFN(5, "sqh:", 0, 0, 0, 0);
4100 	ehci_dump_sqh(sqh);
4101 	ehci_dump_sqtds(exfer->ex_sqtdstart);
4102 #endif
4103 #endif
4104 
4105 	return USBD_IN_PROGRESS;
4106 }
4107 
4108 Static void
4109 ehci_device_intr_abort(struct usbd_xfer *xfer)
4110 {
4111 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
4112 
4113 	DPRINTF("xfer=%#jx", (uintptr_t)xfer, 0, 0, 0);
4114 	KASSERT(xfer->ux_pipe->up_intrxfer == xfer);
4115 
4116 	/*
4117 	 * XXX - abort_xfer uses ehci_sync_hc, which syncs via the advance
4118 	 *       async doorbell. That's dependent on the async list, wheras
4119 	 *       intr xfers are periodic, should not use this?
4120 	 */
4121 	ehci_abort_xfer(xfer, USBD_CANCELLED);
4122 }
4123 
4124 Static void
4125 ehci_device_intr_close(struct usbd_pipe *pipe)
4126 {
4127 	ehci_softc_t *sc = EHCI_PIPE2SC(pipe);
4128 	struct ehci_pipe *epipe = EHCI_PIPE2EPIPE(pipe);
4129 	struct ehci_soft_islot *isp;
4130 
4131 	KASSERT(mutex_owned(&sc->sc_lock));
4132 
4133 	isp = &sc->sc_islots[epipe->sqh->islot];
4134 	ehci_close_pipe(pipe, isp->sqh);
4135 }
4136 
4137 Static void
4138 ehci_device_intr_done(struct usbd_xfer *xfer)
4139 {
4140 	ehci_softc_t *sc __diagused = EHCI_XFER2SC(xfer);
4141 	struct ehci_pipe *epipe = EHCI_XFER2EPIPE(xfer);
4142 	int isread, endpt;
4143 
4144 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
4145 
4146 	DPRINTF("xfer=%#jx, actlen=%jd", (uintptr_t)xfer, xfer->ux_actlen, 0, 0);
4147 
4148 	KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
4149 
4150 	endpt = epipe->pipe.up_endpoint->ue_edesc->bEndpointAddress;
4151 	isread = UE_GET_DIR(endpt) == UE_DIR_IN;
4152 	usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
4153 	    isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
4154 }
4155 
4156 /************************/
4157 Static int
4158 ehci_device_fs_isoc_init(struct usbd_xfer *xfer)
4159 {
4160 	struct ehci_pipe *epipe = EHCI_PIPE2EPIPE(xfer->ux_pipe);
4161 	struct usbd_device *dev = xfer->ux_pipe->up_dev;
4162 	ehci_softc_t *sc = EHCI_XFER2SC(xfer);
4163 	struct ehci_xfer *exfer = EHCI_XFER2EXFER(xfer);
4164 	ehci_soft_sitd_t *sitd, *prev, *start, *stop;
4165 	int i, k, frames;
4166 	u_int huba, dir;
4167 	int err;
4168 
4169 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
4170 
4171 	start = NULL;
4172 	sitd = NULL;
4173 
4174 	DPRINTF("xfer %#jx len %jd flags %jd", (uintptr_t)xfer, xfer->ux_length,
4175 	    xfer->ux_flags, 0);
4176 
4177 	KASSERT(!(xfer->ux_rqflags & URQ_REQUEST));
4178 	KASSERT(xfer->ux_nframes != 0);
4179 	KASSERT(exfer->ex_isdone);
4180 
4181 	exfer->ex_type = EX_FS_ISOC;
4182 	/*
4183 	 * Step 1: Allocate and initialize sitds.
4184 	 */
4185 	i = epipe->pipe.up_endpoint->ue_edesc->bInterval;
4186 	if (i > 16 || i == 0) {
4187 		/* Spec page 271 says intervals > 16 are invalid */
4188 		DPRINTF("bInterval %jd invalid", i, 0, 0, 0);
4189 
4190 		return EINVAL;
4191 	}
4192 
4193 	frames = xfer->ux_nframes;
4194 	for (i = 0, prev = NULL; i < frames; i++, prev = sitd) {
4195 		sitd = ehci_alloc_sitd(sc);
4196 		if (sitd == NULL) {
4197 			err = ENOMEM;
4198 			goto fail;
4199 		}
4200 
4201 		if (prev)
4202 			prev->xfer_next = sitd;
4203 		else
4204 			start = sitd;
4205 
4206 		huba = dev->ud_myhsport->up_parent->ud_addr;
4207 
4208 #if 0
4209 		if (sc->sc_flags & EHCIF_FREESCALE) {
4210 			// Set hub address to 0 if embedded TT is used.
4211 			if (huba == sc->sc_addr)
4212 				huba = 0;
4213 		}
4214 #endif
4215 
4216 		k = epipe->pipe.up_endpoint->ue_edesc->bEndpointAddress;
4217 		dir = UE_GET_DIR(k) ? 1 : 0;
4218 		sitd->sitd.sitd_endp =
4219 		    htole32(EHCI_SITD_SET_ENDPT(UE_GET_ADDR(k)) |
4220 		    EHCI_SITD_SET_DADDR(dev->ud_addr) |
4221 		    EHCI_SITD_SET_PORT(dev->ud_myhsport->up_portno) |
4222 		    EHCI_SITD_SET_HUBA(huba) |
4223 		    EHCI_SITD_SET_DIR(dir));
4224 
4225 		sitd->sitd.sitd_back = htole32(EHCI_LINK_TERMINATE);
4226 	} /* End of frame */
4227 
4228 	sitd->sitd.sitd_trans |= htole32(EHCI_SITD_IOC);
4229 
4230 	stop = sitd;
4231 	stop->xfer_next = NULL;
4232 	exfer->ex_sitdstart = start;
4233 	exfer->ex_sitdend = stop;
4234 
4235 	return 0;
4236 
4237 fail:
4238 	mutex_enter(&sc->sc_lock);
4239 	ehci_soft_sitd_t *next;
4240 	for (sitd = start; sitd; sitd = next) {
4241 		next = sitd->xfer_next;
4242 		ehci_free_sitd_locked(sc, sitd);
4243 	}
4244 	mutex_exit(&sc->sc_lock);
4245 
4246 	return err;
4247 }
4248 
4249 Static void
4250 ehci_device_fs_isoc_fini(struct usbd_xfer *xfer)
4251 {
4252 	ehci_softc_t *sc = EHCI_XFER2SC(xfer);
4253 	struct ehci_xfer *ex = EHCI_XFER2EXFER(xfer);
4254 
4255 	KASSERT(ex->ex_type == EX_FS_ISOC);
4256 
4257 	ehci_free_sitd_chain(sc, ex->ex_sitdstart);
4258 }
4259 
4260 Static usbd_status
4261 ehci_device_fs_isoc_transfer(struct usbd_xfer *xfer)
4262 {
4263 	ehci_softc_t *sc = EHCI_XFER2SC(xfer);
4264 	usbd_status __diagused err;
4265 
4266 	mutex_enter(&sc->sc_lock);
4267 	err = usb_insert_transfer(xfer);
4268 	mutex_exit(&sc->sc_lock);
4269 
4270 	KASSERT(err == USBD_NORMAL_COMPLETION);
4271 
4272 	struct ehci_pipe *epipe = EHCI_XFER2EPIPE(xfer);;
4273 	struct usbd_device *dev = xfer->ux_pipe->up_dev;;
4274 	struct ehci_xfer *exfer = EHCI_XFER2EXFER(xfer);
4275 	ehci_soft_sitd_t *sitd;
4276 	usb_dma_t *dma_buf;
4277 	int i, j, k, frames;
4278 	int offs, total_length;
4279 	int frindex;
4280 	u_int dir;
4281 
4282 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
4283 
4284 	sitd = NULL;
4285 	total_length = 0;
4286 
4287 
4288 	DPRINTF("xfer %#jx len %jd flags %jd", (uintptr_t)xfer, xfer->ux_length,
4289 	    xfer->ux_flags, 0);
4290 
4291 	if (sc->sc_dying)
4292 		return USBD_IOERROR;
4293 
4294 	/*
4295 	 * To avoid complication, don't allow a request right now that'll span
4296 	 * the entire frame table. To within 4 frames, to allow some leeway
4297 	 * on either side of where the hc currently is.
4298 	 */
4299 	if (epipe->pipe.up_endpoint->ue_edesc->bInterval *
4300 			xfer->ux_nframes >= sc->sc_flsize - 4) {
4301 		printf("ehci: isoc descriptor requested that spans the entire"
4302 		    "frametable, too many frames\n");
4303 		return USBD_INVAL;
4304 	}
4305 
4306 	KASSERT(xfer->ux_nframes != 0 && xfer->ux_frlengths);
4307 	KASSERT(!(xfer->ux_rqflags & URQ_REQUEST));
4308 	KASSERT(exfer->ex_isdone);
4309 #ifdef DIAGNOSTIC
4310 	exfer->ex_isdone = false;
4311 #endif
4312 
4313 	/*
4314 	 * Step 1: Initialize sitds.
4315 	 */
4316 
4317 	frames = xfer->ux_nframes;
4318 	dma_buf = &xfer->ux_dmabuf;
4319 	offs = 0;
4320 
4321 	for (sitd = exfer->ex_sitdstart, i = 0; i < frames;
4322 	    i++, sitd = sitd->xfer_next) {
4323 		KASSERT(sitd != NULL);
4324 		KASSERT(xfer->ux_frlengths[i] <= 0x3ff);
4325 
4326 		sitd->sitd.sitd_trans = htole32(EHCI_SITD_ACTIVE |
4327 		    EHCI_SITD_SET_LEN(xfer->ux_frlengths[i]));
4328 
4329 		/* Set page0 index and offset - TP and T-offset are set below */
4330 		sitd->sitd.sitd_buffer[0] = htole32(DMAADDR(dma_buf, offs));
4331 
4332 		total_length += xfer->ux_frlengths[i];
4333 		offs += xfer->ux_frlengths[i];
4334 
4335 		sitd->sitd.sitd_buffer[1] =
4336 		    htole32(EHCI_SITD_SET_BPTR(DMAADDR(dma_buf, offs - 1)));
4337 
4338 		u_int huba __diagused = dev->ud_myhsport->up_parent->ud_addr;
4339 
4340 #if 0
4341 		if (sc->sc_flags & EHCIF_FREESCALE) {
4342 			// Set hub address to 0 if embedded TT is used.
4343 			if (huba == sc->sc_addr)
4344 				huba = 0;
4345 		}
4346 #endif
4347 
4348 		k = epipe->pipe.up_endpoint->ue_edesc->bEndpointAddress;
4349 		dir = UE_GET_DIR(k) ? 1 : 0;
4350 		KASSERT(sitd->sitd.sitd_endp == htole32(
4351 		    EHCI_SITD_SET_ENDPT(UE_GET_ADDR(k)) |
4352 		    EHCI_SITD_SET_DADDR(dev->ud_addr) |
4353 		    EHCI_SITD_SET_PORT(dev->ud_myhsport->up_portno) |
4354 		    EHCI_SITD_SET_HUBA(huba) |
4355 		    EHCI_SITD_SET_DIR(dir)));
4356 		KASSERT(sitd->sitd.sitd_back == htole32(EHCI_LINK_TERMINATE));
4357 
4358 		uint8_t sa = 0;
4359 		uint8_t sb = 0;
4360 		u_int temp, tlen;
4361 
4362 		if (dir == 0) {	/* OUT */
4363 			temp = 0;
4364 			tlen = xfer->ux_frlengths[i];
4365 			if (tlen <= 188) {
4366 				temp |= 1;	/* T-count = 1, TP = ALL */
4367 				tlen = 1;
4368 			} else {
4369 				tlen += 187;
4370 				tlen /= 188;
4371 				temp |= tlen;	/* T-count = [1..6] */
4372 				temp |= 8;	/* TP = Begin */
4373 			}
4374 			sitd->sitd.sitd_buffer[1] |= htole32(temp);
4375 
4376 			tlen += sa;
4377 
4378 			if (tlen >= 8) {
4379 				sb = 0;
4380 			} else {
4381 				sb = (1 << tlen);
4382 			}
4383 
4384 			sa = (1 << sa);
4385 			sa = (sb - sa) & 0x3F;
4386 			sb = 0;
4387 		} else {
4388 			sb = (-(4 << sa)) & 0xFE;
4389 			sa = (1 << sa) & 0x3F;
4390 			sa = 0x01;
4391 			sb = 0xfc;
4392 		}
4393 
4394 		sitd->sitd.sitd_sched = htole32(
4395 		    EHCI_SITD_SET_SMASK(sa) |
4396 		    EHCI_SITD_SET_CMASK(sb)
4397 		    );
4398 
4399 		usb_syncmem(&sitd->dma, sitd->offs, sizeof(ehci_sitd_t),
4400 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
4401 	} /* End of frame */
4402 
4403 	sitd = exfer->ex_sitdend;
4404 	sitd->sitd.sitd_trans |= htole32(EHCI_SITD_IOC);
4405 
4406 	usb_syncmem(&sitd->dma, sitd->offs + offsetof(ehci_sitd_t, sitd_trans),
4407 	    sizeof(sitd->sitd.sitd_trans),
4408 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
4409 
4410 	usb_syncmem(&exfer->ex_xfer.ux_dmabuf, 0, total_length,
4411 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4412 
4413 	/*
4414 	 * Part 2: Transfer descriptors have now been set up, now they must
4415 	 * be scheduled into the periodic frame list. Erk. Not wanting to
4416 	 * complicate matters, transfer is denied if the transfer spans
4417 	 * more than the period frame list.
4418 	 */
4419 
4420 	mutex_enter(&sc->sc_lock);
4421 
4422 	/* Start inserting frames */
4423 	if (epipe->isoc.cur_xfers > 0) {
4424 		frindex = epipe->isoc.next_frame;
4425 	} else {
4426 		frindex = EOREAD4(sc, EHCI_FRINDEX);
4427 		frindex = frindex >> 3; /* Erase microframe index */
4428 		frindex += 2;
4429 	}
4430 
4431 	if (frindex >= sc->sc_flsize)
4432 		frindex &= (sc->sc_flsize - 1);
4433 
4434 	/* Whats the frame interval? */
4435 	i = epipe->pipe.up_endpoint->ue_edesc->bInterval;
4436 
4437 	for (sitd = exfer->ex_sitdstart, j = 0; j < frames;
4438 	    j++, sitd = sitd->xfer_next) {
4439 		KASSERT(sitd);
4440 
4441 		usb_syncmem(&sc->sc_fldma,
4442 		    sizeof(ehci_link_t) * frindex,
4443 		    sizeof(ehci_link_t),
4444 		    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
4445 
4446 		sitd->sitd.sitd_next = sc->sc_flist[frindex];
4447 		if (sitd->sitd.sitd_next == 0)
4448 			/*
4449 			 * FIXME: frindex table gets initialized to NULL
4450 			 * or EHCI_NULL?
4451 			 */
4452 			sitd->sitd.sitd_next = EHCI_NULL;
4453 
4454 		usb_syncmem(&sitd->dma,
4455 		    sitd->offs + offsetof(ehci_sitd_t, sitd_next),
4456 		    sizeof(ehci_sitd_t),
4457 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
4458 
4459 		sc->sc_flist[frindex] =
4460 		    htole32(EHCI_LINK_SITD | sitd->physaddr);
4461 
4462 		usb_syncmem(&sc->sc_fldma,
4463 		    sizeof(ehci_link_t) * frindex,
4464 		    sizeof(ehci_link_t),
4465 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
4466 
4467 		sitd->frame_list.next = sc->sc_softsitds[frindex];
4468 		sc->sc_softsitds[frindex] = sitd;
4469 		if (sitd->frame_list.next != NULL)
4470 			sitd->frame_list.next->frame_list.prev = sitd;
4471 		sitd->slot = frindex;
4472 		sitd->frame_list.prev = NULL;
4473 
4474 		frindex += i;
4475 		if (frindex >= sc->sc_flsize)
4476 			frindex -= sc->sc_flsize;
4477 	}
4478 
4479 	epipe->isoc.cur_xfers++;
4480 	epipe->isoc.next_frame = frindex;
4481 
4482 	ehci_add_intr_list(sc, exfer);
4483 	xfer->ux_status = USBD_IN_PROGRESS;
4484 
4485 	mutex_exit(&sc->sc_lock);
4486 
4487 	return USBD_IN_PROGRESS;
4488 }
4489 
4490 Static void
4491 ehci_device_fs_isoc_abort(struct usbd_xfer *xfer)
4492 {
4493 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
4494 
4495 	DPRINTF("xfer = %#jx", (uintptr_t)xfer, 0, 0, 0);
4496 	ehci_abort_isoc_xfer(xfer, USBD_CANCELLED);
4497 }
4498 
4499 Static void
4500 ehci_device_fs_isoc_close(struct usbd_pipe *pipe)
4501 {
4502 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
4503 
4504 	DPRINTF("nothing in the pipe to free?", 0, 0, 0, 0);
4505 }
4506 
4507 Static void
4508 ehci_device_fs_isoc_done(struct usbd_xfer *xfer)
4509 {
4510 	struct ehci_xfer *exfer = EHCI_XFER2EXFER(xfer);
4511 	ehci_softc_t *sc = EHCI_XFER2SC(xfer);
4512 	struct ehci_pipe *epipe = EHCI_XFER2EPIPE(xfer);
4513 
4514 	KASSERT(mutex_owned(&sc->sc_lock));
4515 
4516 	epipe->isoc.cur_xfers--;
4517 	ehci_remove_sitd_chain(sc, exfer->ex_itdstart);
4518 
4519 	usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
4520 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
4521 }
4522 
4523 
4524 /************************/
4525 
4526 
4527 Static int
4528 ehci_device_isoc_init(struct usbd_xfer *xfer)
4529 {
4530 	ehci_softc_t *sc = EHCI_XFER2SC(xfer);
4531 	struct ehci_pipe *epipe = EHCI_XFER2EPIPE(xfer);
4532 	struct ehci_xfer *exfer = EHCI_XFER2EXFER(xfer);
4533 	ehci_soft_itd_t *itd, *prev, *start, *stop;
4534 	int i, j, k;
4535 	int frames, ufrperframe;
4536 	int err;
4537 
4538 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
4539 
4540 	start = NULL;
4541 	prev = NULL;
4542 	itd = NULL;
4543 
4544 	KASSERT(xfer->ux_nframes != 0);
4545 	KASSERT(!(xfer->ux_rqflags & URQ_REQUEST));
4546 	KASSERT(exfer->ex_isdone);
4547 
4548 	exfer->ex_type = EX_ISOC;
4549 
4550 	/*
4551 	 * Step 1: Allocate and initialize itds, how many do we need?
4552 	 * One per transfer if interval >= 8 microframes, less if we use
4553 	 * multiple microframes per frame.
4554 	 */
4555 	i = epipe->pipe.up_endpoint->ue_edesc->bInterval;
4556 	if (i > 16 || i == 0) {
4557 		/* Spec page 271 says intervals > 16 are invalid */
4558 		DPRINTF("bInterval %jd invalid", i, 0, 0, 0);
4559 		return USBD_INVAL;
4560 	}
4561 
4562 	ufrperframe = max(1, USB_UFRAMES_PER_FRAME / (1 << (i - 1)));
4563 	frames = (xfer->ux_nframes + (ufrperframe - 1)) / ufrperframe;
4564 
4565 	for (i = 0, prev = NULL; i < frames; i++, prev = itd) {
4566 		itd = ehci_alloc_itd(sc);
4567 		if (itd == NULL) {
4568 			err = ENOMEM;
4569 			goto fail;
4570 		}
4571 
4572 		if (prev != NULL) {
4573 			/* Maybe not as it's updated by the scheduling? */
4574 			prev->itd.itd_next =
4575 			    htole32(itd->physaddr | EHCI_LINK_ITD);
4576 
4577 			prev->xfer_next = itd;
4578 		} else {
4579 			start = itd;
4580 		}
4581 
4582 		/*
4583 		 * Other special values
4584 		 */
4585 		k = epipe->pipe.up_endpoint->ue_edesc->bEndpointAddress;
4586 		itd->itd.itd_bufr[0] = htole32(
4587 		    EHCI_ITD_SET_EP(UE_GET_ADDR(k)) |
4588 		    EHCI_ITD_SET_DADDR(epipe->pipe.up_dev->ud_addr));
4589 
4590 		k = (UE_GET_DIR(epipe->pipe.up_endpoint->ue_edesc->bEndpointAddress))
4591 		    ? 1 : 0;
4592 		j = UGETW(epipe->pipe.up_endpoint->ue_edesc->wMaxPacketSize);
4593 		itd->itd.itd_bufr[1] |= htole32(
4594 		    EHCI_ITD_SET_DIR(k) |
4595 		    EHCI_ITD_SET_MAXPKT(UE_GET_SIZE(j)));
4596 
4597 		/* FIXME: handle invalid trans - should be done in openpipe */
4598 		itd->itd.itd_bufr[2] |=
4599 		    htole32(EHCI_ITD_SET_MULTI(UE_GET_TRANS(j)+1));
4600 	} /* End of frame */
4601 
4602 	stop = itd;
4603 	stop->xfer_next = NULL;
4604 
4605 	exfer->ex_itdstart = start;
4606 	exfer->ex_itdend = stop;
4607 
4608 	return 0;
4609 fail:
4610 	mutex_enter(&sc->sc_lock);
4611 	ehci_soft_itd_t *next;
4612 	for (itd = start; itd; itd = next) {
4613 		next = itd->xfer_next;
4614 		ehci_free_itd_locked(sc, itd);
4615 	}
4616 	mutex_exit(&sc->sc_lock);
4617 
4618 	return err;
4619 
4620 }
4621 
4622 Static void
4623 ehci_device_isoc_fini(struct usbd_xfer *xfer)
4624 {
4625 	ehci_softc_t *sc = EHCI_XFER2SC(xfer);
4626 	struct ehci_xfer *ex = EHCI_XFER2EXFER(xfer);
4627 
4628 	KASSERT(ex->ex_type == EX_ISOC);
4629 
4630 	ehci_free_itd_chain(sc, ex->ex_itdstart);
4631 }
4632 
4633 Static usbd_status
4634 ehci_device_isoc_transfer(struct usbd_xfer *xfer)
4635 {
4636 	ehci_softc_t *sc = EHCI_XFER2SC(xfer);
4637 	usbd_status __diagused err;
4638 
4639 	mutex_enter(&sc->sc_lock);
4640 	err = usb_insert_transfer(xfer);
4641 	mutex_exit(&sc->sc_lock);
4642 
4643 	KASSERT(err == USBD_NORMAL_COMPLETION);
4644 
4645 	struct ehci_pipe *epipe = EHCI_XFER2EPIPE(xfer);
4646 	struct ehci_xfer *exfer = EHCI_XFER2EXFER(xfer);
4647 	ehci_soft_itd_t *itd, *prev;
4648 	usb_dma_t *dma_buf;
4649 	int i, j;
4650 	int frames, uframes, ufrperframe;
4651 	int trans_count, offs, total_length;
4652 	int frindex;
4653 
4654 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
4655 
4656 	prev = NULL;
4657 	itd = NULL;
4658 	trans_count = 0;
4659 	total_length = 0;
4660 
4661 	DPRINTF("xfer %#jx flags %jd", (uintptr_t)xfer, xfer->ux_flags, 0, 0);
4662 
4663 	if (sc->sc_dying)
4664 		return USBD_IOERROR;
4665 
4666 	/*
4667 	 * To avoid complication, don't allow a request right now that'll span
4668 	 * the entire frame table. To within 4 frames, to allow some leeway
4669 	 * on either side of where the hc currently is.
4670 	 */
4671 	if ((1 << (epipe->pipe.up_endpoint->ue_edesc->bInterval)) *
4672 			xfer->ux_nframes >= (sc->sc_flsize - 4) * 8) {
4673 		DPRINTF(
4674 		    "isoc descriptor spans entire frametable", 0, 0, 0, 0);
4675 		printf("ehci: isoc descriptor requested that spans the entire frametable, too many frames\n");
4676 		return USBD_INVAL;
4677 	}
4678 
4679 	KASSERT(xfer->ux_nframes != 0 && xfer->ux_frlengths);
4680 	KASSERT(!(xfer->ux_rqflags & URQ_REQUEST));
4681 	KASSERT(exfer->ex_isdone);
4682 #ifdef DIAGNOSTIC
4683 	exfer->ex_isdone = false;
4684 #endif
4685 
4686 	/*
4687 	 * Step 1: Re-Initialize itds
4688 	 */
4689 
4690 	i = epipe->pipe.up_endpoint->ue_edesc->bInterval;
4691 	if (i > 16 || i == 0) {
4692 		/* Spec page 271 says intervals > 16 are invalid */
4693 		DPRINTF("bInterval %jd invalid", i, 0, 0, 0);
4694 		return USBD_INVAL;
4695 	}
4696 
4697 	ufrperframe = max(1, USB_UFRAMES_PER_FRAME / (1 << (i - 1)));
4698 	frames = (xfer->ux_nframes + (ufrperframe - 1)) / ufrperframe;
4699 	uframes = USB_UFRAMES_PER_FRAME / ufrperframe;
4700 
4701 	if (frames == 0) {
4702 		DPRINTF("frames == 0", 0, 0, 0, 0);
4703 		return USBD_INVAL;
4704 	}
4705 
4706 	dma_buf = &xfer->ux_dmabuf;
4707 	offs = 0;
4708 
4709 	itd = exfer->ex_itdstart;
4710 	for (i = 0; i < frames; i++, itd = itd->xfer_next) {
4711 		int froffs = offs;
4712 
4713 		if (prev != NULL) {
4714 			prev->itd.itd_next =
4715 			    htole32(itd->physaddr | EHCI_LINK_ITD);
4716 			usb_syncmem(&prev->dma,
4717 			    prev->offs + offsetof(ehci_itd_t, itd_next),
4718 			    sizeof(prev->itd.itd_next), BUS_DMASYNC_POSTWRITE);
4719 			prev->xfer_next = itd;
4720 		}
4721 
4722 		/*
4723 		 * Step 1.5, initialize uframes
4724 		 */
4725 		for (j = 0; j < EHCI_ITD_NUFRAMES; j += uframes) {
4726 			/* Calculate which page in the list this starts in */
4727 			int addr = DMAADDR(dma_buf, froffs);
4728 			addr = EHCI_PAGE_OFFSET(addr);
4729 			addr += (offs - froffs);
4730 			addr = EHCI_PAGE(addr);
4731 			addr /= EHCI_PAGE_SIZE;
4732 
4733 			/*
4734 			 * This gets the initial offset into the first page,
4735 			 * looks how far further along the current uframe
4736 			 * offset is. Works out how many pages that is.
4737 			 */
4738 
4739 			itd->itd.itd_ctl[j] = htole32 ( EHCI_ITD_ACTIVE |
4740 			    EHCI_ITD_SET_LEN(xfer->ux_frlengths[trans_count]) |
4741 			    EHCI_ITD_SET_PG(addr) |
4742 			    EHCI_ITD_SET_OFFS(EHCI_PAGE_OFFSET(DMAADDR(dma_buf,offs))));
4743 
4744 			total_length += xfer->ux_frlengths[trans_count];
4745 			offs += xfer->ux_frlengths[trans_count];
4746 			trans_count++;
4747 
4748 			if (trans_count >= xfer->ux_nframes) { /*Set IOC*/
4749 				itd->itd.itd_ctl[j] |= htole32(EHCI_ITD_IOC);
4750 				break;
4751 			}
4752 		}
4753 
4754 		/*
4755 		 * Step 1.75, set buffer pointers. To simplify matters, all
4756 		 * pointers are filled out for the next 7 hardware pages in
4757 		 * the dma block, so no need to worry what pages to cover
4758 		 * and what to not.
4759 		 */
4760 
4761 		for (j = 0; j < EHCI_ITD_NBUFFERS; j++) {
4762 			/*
4763 			 * Don't try to lookup a page that's past the end
4764 			 * of buffer
4765 			 */
4766 			int page_offs = EHCI_PAGE(froffs + (EHCI_PAGE_SIZE * j));
4767 			if (page_offs >= dma_buf->udma_block->size)
4768 				break;
4769 
4770 			uint64_t page = DMAADDR(dma_buf, page_offs);
4771 			page = EHCI_PAGE(page);
4772 			itd->itd.itd_bufr[j] = htole32(EHCI_ITD_SET_BPTR(page));
4773 			itd->itd.itd_bufr_hi[j] = htole32(page >> 32);
4774 		}
4775 		/*
4776 		 * Other special values
4777 		 */
4778 
4779 		int k = epipe->pipe.up_endpoint->ue_edesc->bEndpointAddress;
4780 		itd->itd.itd_bufr[0] |= htole32(EHCI_ITD_SET_EP(UE_GET_ADDR(k)) |
4781 		    EHCI_ITD_SET_DADDR(epipe->pipe.up_dev->ud_addr));
4782 
4783 		k = (UE_GET_DIR(epipe->pipe.up_endpoint->ue_edesc->bEndpointAddress))
4784 		    ? 1 : 0;
4785 		j = UGETW(epipe->pipe.up_endpoint->ue_edesc->wMaxPacketSize);
4786 		itd->itd.itd_bufr[1] |= htole32(EHCI_ITD_SET_DIR(k) |
4787 		    EHCI_ITD_SET_MAXPKT(UE_GET_SIZE(j)));
4788 
4789 		/* FIXME: handle invalid trans */
4790 		itd->itd.itd_bufr[2] |=
4791 		    htole32(EHCI_ITD_SET_MULTI(UE_GET_TRANS(j)+1));
4792 
4793 		usb_syncmem(&itd->dma, itd->offs, sizeof(ehci_itd_t),
4794 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
4795 
4796 		prev = itd;
4797 	} /* End of frame */
4798 
4799 	usb_syncmem(&exfer->ex_xfer.ux_dmabuf, 0, total_length,
4800 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4801 
4802 	/*
4803 	 * Part 2: Transfer descriptors have now been set up, now they must
4804 	 * be scheduled into the period frame list. Erk. Not wanting to
4805 	 * complicate matters, transfer is denied if the transfer spans
4806 	 * more than the period frame list.
4807 	 */
4808 
4809 	mutex_enter(&sc->sc_lock);
4810 
4811 	/* Start inserting frames */
4812 	if (epipe->isoc.cur_xfers > 0) {
4813 		frindex = epipe->isoc.next_frame;
4814 	} else {
4815 		frindex = EOREAD4(sc, EHCI_FRINDEX);
4816 		frindex = frindex >> 3; /* Erase microframe index */
4817 		frindex += 2;
4818 	}
4819 
4820 	if (frindex >= sc->sc_flsize)
4821 		frindex &= (sc->sc_flsize - 1);
4822 
4823 	/* What's the frame interval? */
4824 	i = (1 << (epipe->pipe.up_endpoint->ue_edesc->bInterval - 1));
4825 	if (i / USB_UFRAMES_PER_FRAME == 0)
4826 		i = 1;
4827 	else
4828 		i /= USB_UFRAMES_PER_FRAME;
4829 
4830 	itd = exfer->ex_itdstart;
4831 	for (j = 0; j < frames; j++) {
4832 		KASSERTMSG(itd != NULL, "frame %d\n", j);
4833 
4834 		usb_syncmem(&sc->sc_fldma,
4835 		    sizeof(ehci_link_t) * frindex,
4836 		    sizeof(ehci_link_t),
4837 		    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
4838 
4839 		itd->itd.itd_next = sc->sc_flist[frindex];
4840 		if (itd->itd.itd_next == 0)
4841 			/*
4842 			 * FIXME: frindex table gets initialized to NULL
4843 			 * or EHCI_NULL?
4844 			 */
4845 			itd->itd.itd_next = EHCI_NULL;
4846 
4847 		usb_syncmem(&itd->dma,
4848 		    itd->offs + offsetof(ehci_itd_t, itd_next),
4849 		    sizeof(itd->itd.itd_next),
4850 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
4851 
4852 		sc->sc_flist[frindex] = htole32(EHCI_LINK_ITD | itd->physaddr);
4853 
4854 		usb_syncmem(&sc->sc_fldma,
4855 		    sizeof(ehci_link_t) * frindex,
4856 		    sizeof(ehci_link_t),
4857 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
4858 
4859 		itd->frame_list.next = sc->sc_softitds[frindex];
4860 		sc->sc_softitds[frindex] = itd;
4861 		if (itd->frame_list.next != NULL)
4862 			itd->frame_list.next->frame_list.prev = itd;
4863 		itd->slot = frindex;
4864 		itd->frame_list.prev = NULL;
4865 
4866 		frindex += i;
4867 		if (frindex >= sc->sc_flsize)
4868 			frindex -= sc->sc_flsize;
4869 
4870 		itd = itd->xfer_next;
4871 	}
4872 
4873 	epipe->isoc.cur_xfers++;
4874 	epipe->isoc.next_frame = frindex;
4875 
4876 	ehci_add_intr_list(sc, exfer);
4877 	xfer->ux_status = USBD_IN_PROGRESS;
4878 
4879 	mutex_exit(&sc->sc_lock);
4880 
4881 	return USBD_IN_PROGRESS;
4882 }
4883 
4884 Static void
4885 ehci_device_isoc_abort(struct usbd_xfer *xfer)
4886 {
4887 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
4888 
4889 	DPRINTF("xfer = %#jx", (uintptr_t)xfer, 0, 0, 0);
4890 	ehci_abort_isoc_xfer(xfer, USBD_CANCELLED);
4891 }
4892 
4893 Static void
4894 ehci_device_isoc_close(struct usbd_pipe *pipe)
4895 {
4896 	EHCIHIST_FUNC(); EHCIHIST_CALLED();
4897 
4898 	DPRINTF("nothing in the pipe to free?", 0, 0, 0, 0);
4899 }
4900 
4901 Static void
4902 ehci_device_isoc_done(struct usbd_xfer *xfer)
4903 {
4904 	struct ehci_xfer *exfer = EHCI_XFER2EXFER(xfer);
4905 	ehci_softc_t *sc = EHCI_XFER2SC(xfer);
4906 	struct ehci_pipe *epipe = EHCI_XFER2EPIPE(xfer);
4907 
4908 	KASSERT(mutex_owned(&sc->sc_lock));
4909 
4910 	epipe->isoc.cur_xfers--;
4911 	ehci_remove_itd_chain(sc, exfer->ex_sitdstart);
4912 	usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
4913 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
4914 }
4915