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