xhci.c revision 1.28.2.13 1 /* $NetBSD: xhci.c,v 1.28.2.13 2014/12/05 09:37:50 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 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: xhci.c,v 1.28.2.13 2014/12/05 09:37:50 skrll Exp $");
31
32 #include "opt_usb.h"
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/kmem.h>
38 #include <sys/device.h>
39 #include <sys/select.h>
40 #include <sys/proc.h>
41 #include <sys/queue.h>
42 #include <sys/mutex.h>
43 #include <sys/condvar.h>
44 #include <sys/bus.h>
45 #include <sys/cpu.h>
46 #include <sys/sysctl.h>
47
48 #include <machine/endian.h>
49
50 #include <dev/usb/usb.h>
51 #include <dev/usb/usbdi.h>
52 #include <dev/usb/usbdivar.h>
53 #include <dev/usb/usbhist.h>
54 #include <dev/usb/usb_mem.h>
55 #include <dev/usb/usb_quirks.h>
56
57 #include <dev/usb/xhcireg.h>
58 #include <dev/usb/xhcivar.h>
59 #include <dev/usb/usbroothub.h>
60
61
62 #ifdef USB_DEBUG
63 #ifndef XHCI_DEBUG
64 #define xhcidebug 0
65 #else
66 static int xhcidebug = 0;
67
68 SYSCTL_SETUP(sysctl_hw_xhci_setup, "sysctl hw.xhci setup")
69 {
70 int err;
71 const struct sysctlnode *rnode;
72 const struct sysctlnode *cnode;
73
74 err = sysctl_createv(clog, 0, NULL, &rnode,
75 CTLFLAG_PERMANENT, CTLTYPE_NODE, "xhci",
76 SYSCTL_DESCR("xhci global controls"),
77 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
78
79 if (err)
80 goto fail;
81
82 /* control debugging printfs */
83 err = sysctl_createv(clog, 0, &rnode, &cnode,
84 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
85 "debug", SYSCTL_DESCR("Enable debugging output"),
86 NULL, 0, &xhcidebug, sizeof(xhcidebug), CTL_CREATE, CTL_EOL);
87 if (err)
88 goto fail;
89
90 return;
91 fail:
92 aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
93 }
94
95 #endif /* XHCI_DEBUG */
96 #endif /* USB_DEBUG */
97
98 #define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(xhcidebug,N,FMT,A,B,C,D)
99 #define XHCIHIST_FUNC() USBHIST_FUNC()
100 #define XHCIHIST_CALLED(name) USBHIST_CALLED(xhcidebug)
101
102 #define XHCI_DCI_SLOT 0
103 #define XHCI_DCI_EP_CONTROL 1
104
105 #define XHCI_ICI_INPUT_CONTROL 0
106
107 struct xhci_pipe {
108 struct usbd_pipe xp_pipe;
109 };
110
111 #define XHCI_COMMAND_RING_TRBS 256
112 #define XHCI_EVENT_RING_TRBS 256
113 #define XHCI_EVENT_RING_SEGMENTS 1
114 #define XHCI_TRB_3_ED_BIT XHCI_TRB_3_ISP_BIT
115
116 static usbd_status xhci_open(usbd_pipe_handle);
117 static int xhci_intr1(struct xhci_softc * const);
118 static void xhci_softintr(void *);
119 static void xhci_poll(struct usbd_bus *);
120 static usbd_xfer_handle xhci_allocx(struct usbd_bus *);
121 static void xhci_freex(struct usbd_bus *, usbd_xfer_handle);
122 static void xhci_get_lock(struct usbd_bus *, kmutex_t **);
123 static usbd_status xhci_new_device(device_t, usbd_bus_handle, int, int, int,
124 struct usbd_port *);
125 static int xhci_roothub_ctrl(struct usbd_bus *, usb_device_request_t *,
126 void *, int);
127
128 static usbd_status xhci_configure_endpoint(usbd_pipe_handle);
129 static usbd_status xhci_unconfigure_endpoint(usbd_pipe_handle);
130 static usbd_status xhci_reset_endpoint(usbd_pipe_handle);
131 //static usbd_status xhci_stop_endpoint(usbd_pipe_handle);
132
133 static usbd_status xhci_set_dequeue(usbd_pipe_handle);
134
135 static usbd_status xhci_do_command(struct xhci_softc * const,
136 struct xhci_trb * const, int);
137 static usbd_status xhci_init_slot(struct xhci_softc * const, uint32_t,
138 int, int, int, int);
139 static usbd_status xhci_enable_slot(struct xhci_softc * const,
140 uint8_t * const);
141 static usbd_status xhci_address_device(struct xhci_softc * const,
142 uint64_t, uint8_t, bool);
143 static usbd_status xhci_update_ep0_mps(struct xhci_softc * const,
144 struct xhci_slot * const, u_int);
145 static usbd_status xhci_ring_init(struct xhci_softc * const,
146 struct xhci_ring * const, size_t, size_t);
147 static void xhci_ring_free(struct xhci_softc * const, struct xhci_ring * const);
148
149 static void xhci_noop(usbd_pipe_handle);
150
151 static usbd_status xhci_root_intr_transfer(usbd_xfer_handle);
152 static usbd_status xhci_root_intr_start(usbd_xfer_handle);
153 static void xhci_root_intr_abort(usbd_xfer_handle);
154 static void xhci_root_intr_close(usbd_pipe_handle);
155 static void xhci_root_intr_done(usbd_xfer_handle);
156
157 static usbd_status xhci_device_ctrl_transfer(usbd_xfer_handle);
158 static usbd_status xhci_device_ctrl_start(usbd_xfer_handle);
159 static void xhci_device_ctrl_abort(usbd_xfer_handle);
160 static void xhci_device_ctrl_close(usbd_pipe_handle);
161 static void xhci_device_ctrl_done(usbd_xfer_handle);
162
163 static usbd_status xhci_device_intr_transfer(usbd_xfer_handle);
164 static usbd_status xhci_device_intr_start(usbd_xfer_handle);
165 static void xhci_device_intr_abort(usbd_xfer_handle);
166 static void xhci_device_intr_close(usbd_pipe_handle);
167 static void xhci_device_intr_done(usbd_xfer_handle);
168
169 static usbd_status xhci_device_bulk_transfer(usbd_xfer_handle);
170 static usbd_status xhci_device_bulk_start(usbd_xfer_handle);
171 static void xhci_device_bulk_abort(usbd_xfer_handle);
172 static void xhci_device_bulk_close(usbd_pipe_handle);
173 static void xhci_device_bulk_done(usbd_xfer_handle);
174
175 static void xhci_timeout(void *);
176 static void xhci_timeout_task(void *);
177
178 static const struct usbd_bus_methods xhci_bus_methods = {
179 .ubm_open = xhci_open,
180 .ubm_softint = xhci_softintr,
181 .ubm_dopoll = xhci_poll,
182 .ubm_allocx = xhci_allocx,
183 .ubm_freex = xhci_freex,
184 .ubm_getlock = xhci_get_lock,
185 .ubm_newdev = xhci_new_device,
186 .ubm_rhctrl = xhci_roothub_ctrl,
187 };
188
189 static const struct usbd_pipe_methods xhci_root_intr_methods = {
190 .upm_transfer = xhci_root_intr_transfer,
191 .upm_start = xhci_root_intr_start,
192 .upm_abort = xhci_root_intr_abort,
193 .upm_close = xhci_root_intr_close,
194 .upm_cleartoggle = xhci_noop,
195 .upm_done = xhci_root_intr_done,
196 };
197
198
199 static const struct usbd_pipe_methods xhci_device_ctrl_methods = {
200 .upm_transfer = xhci_device_ctrl_transfer,
201 .upm_start = xhci_device_ctrl_start,
202 .upm_abort = xhci_device_ctrl_abort,
203 .upm_close = xhci_device_ctrl_close,
204 .upm_cleartoggle = xhci_noop,
205 .upm_done = xhci_device_ctrl_done,
206 };
207
208 static const struct usbd_pipe_methods xhci_device_isoc_methods = {
209 .upm_cleartoggle = xhci_noop,
210 };
211
212 static const struct usbd_pipe_methods xhci_device_bulk_methods = {
213 .upm_transfer = xhci_device_bulk_transfer,
214 .upm_start = xhci_device_bulk_start,
215 .upm_abort = xhci_device_bulk_abort,
216 .upm_close = xhci_device_bulk_close,
217 .upm_cleartoggle = xhci_noop,
218 .upm_done = xhci_device_bulk_done,
219 };
220
221 static const struct usbd_pipe_methods xhci_device_intr_methods = {
222 .upm_transfer = xhci_device_intr_transfer,
223 .upm_start = xhci_device_intr_start,
224 .upm_abort = xhci_device_intr_abort,
225 .upm_close = xhci_device_intr_close,
226 .upm_cleartoggle = xhci_noop,
227 .upm_done = xhci_device_intr_done,
228 };
229
230 static inline uint32_t
231 xhci_read_4(const struct xhci_softc * const sc, bus_size_t offset)
232 {
233 return bus_space_read_4(sc->sc_iot, sc->sc_ioh, offset);
234 }
235
236 #if 0 /* unused */
237 static inline void
238 xhci_write_4(const struct xhci_softc * const sc, bus_size_t offset,
239 uint32_t value)
240 {
241 bus_space_write_4(sc->sc_iot, sc->sc_ioh, offset, value);
242 }
243 #endif /* unused */
244
245 static inline uint32_t
246 xhci_cap_read_4(const struct xhci_softc * const sc, bus_size_t offset)
247 {
248 return bus_space_read_4(sc->sc_iot, sc->sc_cbh, offset);
249 }
250
251 static inline uint32_t
252 xhci_op_read_4(const struct xhci_softc * const sc, bus_size_t offset)
253 {
254 return bus_space_read_4(sc->sc_iot, sc->sc_obh, offset);
255 }
256
257 static inline void
258 xhci_op_write_4(const struct xhci_softc * const sc, bus_size_t offset,
259 uint32_t value)
260 {
261 bus_space_write_4(sc->sc_iot, sc->sc_obh, offset, value);
262 }
263
264 #if 0 /* unused */
265 static inline uint64_t
266 xhci_op_read_8(const struct xhci_softc * const sc, bus_size_t offset)
267 {
268 uint64_t value;
269
270 if (sc->sc_ac64) {
271 #ifdef XHCI_USE_BUS_SPACE_8
272 value = bus_space_read_8(sc->sc_iot, sc->sc_obh, offset);
273 #else
274 value = bus_space_read_4(sc->sc_iot, sc->sc_obh, offset);
275 value |= (uint64_t)bus_space_read_4(sc->sc_iot, sc->sc_obh,
276 offset + 4) << 32;
277 #endif
278 } else {
279 value = bus_space_read_4(sc->sc_iot, sc->sc_obh, offset);
280 }
281
282 return value;
283 }
284 #endif /* unused */
285
286 static inline void
287 xhci_op_write_8(const struct xhci_softc * const sc, bus_size_t offset,
288 uint64_t value)
289 {
290 if (sc->sc_ac64) {
291 #ifdef XHCI_USE_BUS_SPACE_8
292 bus_space_write_8(sc->sc_iot, sc->sc_obh, offset, value);
293 #else
294 bus_space_write_4(sc->sc_iot, sc->sc_obh, offset + 0,
295 (value >> 0) & 0xffffffff);
296 bus_space_write_4(sc->sc_iot, sc->sc_obh, offset + 4,
297 (value >> 32) & 0xffffffff);
298 #endif
299 } else {
300 bus_space_write_4(sc->sc_iot, sc->sc_obh, offset, value);
301 }
302 }
303
304 static inline uint32_t
305 xhci_rt_read_4(const struct xhci_softc * const sc, bus_size_t offset)
306 {
307 return bus_space_read_4(sc->sc_iot, sc->sc_rbh, offset);
308 }
309
310 static inline void
311 xhci_rt_write_4(const struct xhci_softc * const sc, bus_size_t offset,
312 uint32_t value)
313 {
314 bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset, value);
315 }
316
317 #if 0 /* unused */
318 static inline uint64_t
319 xhci_rt_read_8(const struct xhci_softc * const sc, bus_size_t offset)
320 {
321 uint64_t value;
322
323 if (sc->sc_ac64) {
324 #ifdef XHCI_USE_BUS_SPACE_8
325 value = bus_space_read_8(sc->sc_iot, sc->sc_rbh, offset);
326 #else
327 value = bus_space_read_4(sc->sc_iot, sc->sc_rbh, offset);
328 value |= (uint64_t)bus_space_read_4(sc->sc_iot, sc->sc_rbh,
329 offset + 4) << 32;
330 #endif
331 } else {
332 value = bus_space_read_4(sc->sc_iot, sc->sc_rbh, offset);
333 }
334
335 return value;
336 }
337 #endif /* unused */
338
339 static inline void
340 xhci_rt_write_8(const struct xhci_softc * const sc, bus_size_t offset,
341 uint64_t value)
342 {
343 if (sc->sc_ac64) {
344 #ifdef XHCI_USE_BUS_SPACE_8
345 bus_space_write_8(sc->sc_iot, sc->sc_rbh, offset, value);
346 #else
347 bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset + 0,
348 (value >> 0) & 0xffffffff);
349 bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset + 4,
350 (value >> 32) & 0xffffffff);
351 #endif
352 } else {
353 bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset, value);
354 }
355 }
356
357 #if 0 /* unused */
358 static inline uint32_t
359 xhci_db_read_4(const struct xhci_softc * const sc, bus_size_t offset)
360 {
361 return bus_space_read_4(sc->sc_iot, sc->sc_dbh, offset);
362 }
363 #endif /* unused */
364
365 static inline void
366 xhci_db_write_4(const struct xhci_softc * const sc, bus_size_t offset,
367 uint32_t value)
368 {
369 bus_space_write_4(sc->sc_iot, sc->sc_dbh, offset, value);
370 }
371
372 /* --- */
373
374 static inline uint8_t
375 xhci_ep_get_type(usb_endpoint_descriptor_t * const ed)
376 {
377 u_int eptype;
378
379 switch (UE_GET_XFERTYPE(ed->bmAttributes)) {
380 case UE_CONTROL:
381 eptype = 0x0;
382 break;
383 case UE_ISOCHRONOUS:
384 eptype = 0x1;
385 break;
386 case UE_BULK:
387 eptype = 0x2;
388 break;
389 case UE_INTERRUPT:
390 eptype = 0x3;
391 break;
392 }
393
394 if ((UE_GET_XFERTYPE(ed->bmAttributes) == UE_CONTROL) ||
395 (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN))
396 return eptype | 0x4;
397 else
398 return eptype;
399 }
400
401 static u_int
402 xhci_ep_get_dci(usb_endpoint_descriptor_t * const ed)
403 {
404 /* xHCI 1.0 section 4.5.1 */
405 u_int epaddr = UE_GET_ADDR(ed->bEndpointAddress);
406 u_int in = 0;
407
408 if ((UE_GET_XFERTYPE(ed->bmAttributes) == UE_CONTROL) ||
409 (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN))
410 in = 1;
411
412 return epaddr * 2 + in;
413 }
414
415 static inline u_int
416 xhci_dci_to_ici(const u_int i)
417 {
418 return i + 1;
419 }
420
421 static inline void *
422 xhci_slot_get_dcv(struct xhci_softc * const sc, struct xhci_slot * const xs,
423 const u_int dci)
424 {
425 return KERNADDR(&xs->xs_dc_dma, sc->sc_ctxsz * dci);
426 }
427
428 #if 0 /* unused */
429 static inline bus_addr_t
430 xhci_slot_get_dcp(struct xhci_softc * const sc, struct xhci_slot * const xs,
431 const u_int dci)
432 {
433 return DMAADDR(&xs->xs_dc_dma, sc->sc_ctxsz * dci);
434 }
435 #endif /* unused */
436
437 static inline void *
438 xhci_slot_get_icv(struct xhci_softc * const sc, struct xhci_slot * const xs,
439 const u_int ici)
440 {
441 return KERNADDR(&xs->xs_ic_dma, sc->sc_ctxsz * ici);
442 }
443
444 static inline bus_addr_t
445 xhci_slot_get_icp(struct xhci_softc * const sc, struct xhci_slot * const xs,
446 const u_int ici)
447 {
448 return DMAADDR(&xs->xs_ic_dma, sc->sc_ctxsz * ici);
449 }
450
451 static inline struct xhci_trb *
452 xhci_ring_trbv(struct xhci_ring * const xr, u_int idx)
453 {
454 return KERNADDR(&xr->xr_dma, XHCI_TRB_SIZE * idx);
455 }
456
457 static inline bus_addr_t
458 xhci_ring_trbp(struct xhci_ring * const xr, u_int idx)
459 {
460 return DMAADDR(&xr->xr_dma, XHCI_TRB_SIZE * idx);
461 }
462
463 static inline void
464 xhci_trb_put(struct xhci_trb * const trb, uint64_t parameter, uint32_t status,
465 uint32_t control)
466 {
467 trb->trb_0 = parameter;
468 trb->trb_2 = status;
469 trb->trb_3 = control;
470 }
471
472 /* --- */
473
474 void
475 xhci_childdet(device_t self, device_t child)
476 {
477 struct xhci_softc * const sc = device_private(self);
478
479 KASSERT(sc->sc_child == child);
480 if (child == sc->sc_child)
481 sc->sc_child = NULL;
482 }
483
484 int
485 xhci_detach(struct xhci_softc *sc, int flags)
486 {
487 int rv = 0;
488
489 if (sc->sc_child != NULL)
490 rv = config_detach(sc->sc_child, flags);
491
492 if (rv != 0)
493 return rv;
494
495 /* XXX unconfigure/free slots */
496
497 /* verify: */
498 xhci_rt_write_4(sc, XHCI_IMAN(0), 0);
499 xhci_op_write_4(sc, XHCI_USBCMD, 0);
500 /* do we need to wait for stop? */
501
502 xhci_op_write_8(sc, XHCI_CRCR, 0);
503 xhci_ring_free(sc, &sc->sc_cr);
504 cv_destroy(&sc->sc_command_cv);
505
506 xhci_rt_write_4(sc, XHCI_ERSTSZ(0), 0);
507 xhci_rt_write_8(sc, XHCI_ERSTBA(0), 0);
508 xhci_rt_write_8(sc, XHCI_ERDP(0), 0|XHCI_ERDP_LO_BUSY);
509 xhci_ring_free(sc, &sc->sc_er);
510
511 usb_freemem(&sc->sc_bus, &sc->sc_eventst_dma);
512
513 xhci_op_write_8(sc, XHCI_DCBAAP, 0);
514 usb_freemem(&sc->sc_bus, &sc->sc_dcbaa_dma);
515
516 kmem_free(sc->sc_slots, sizeof(*sc->sc_slots) * sc->sc_maxslots);
517
518 mutex_destroy(&sc->sc_lock);
519 mutex_destroy(&sc->sc_intr_lock);
520
521 pool_cache_destroy(sc->sc_xferpool);
522
523 return rv;
524 }
525
526 int
527 xhci_activate(device_t self, enum devact act)
528 {
529 struct xhci_softc * const sc = device_private(self);
530
531 switch (act) {
532 case DVACT_DEACTIVATE:
533 sc->sc_dying = true;
534 return 0;
535 default:
536 return EOPNOTSUPP;
537 }
538 }
539
540 bool
541 xhci_suspend(device_t dv, const pmf_qual_t *qual)
542 {
543 return false;
544 }
545
546 bool
547 xhci_resume(device_t dv, const pmf_qual_t *qual)
548 {
549 return false;
550 }
551
552 bool
553 xhci_shutdown(device_t self, int flags)
554 {
555 return false;
556 }
557
558
559 static void
560 hexdump(const char *msg, const void *base, size_t len)
561 {
562 #if 0
563 size_t cnt;
564 const uint32_t *p;
565 extern paddr_t vtophys(vaddr_t);
566
567 p = base;
568 cnt = 0;
569
570 printf("*** %s (%zu bytes @ %p %p)\n", msg, len, base,
571 (void *)vtophys((vaddr_t)base));
572
573 while (cnt < len) {
574 if (cnt % 16 == 0)
575 printf("%p: ", p);
576 else if (cnt % 8 == 0)
577 printf(" |");
578 printf(" %08x", *p++);
579 cnt += 4;
580 if (cnt % 16 == 0)
581 printf("\n");
582 }
583 #endif
584 }
585
586
587 int
588 xhci_init(struct xhci_softc *sc)
589 {
590 bus_size_t bsz;
591 uint32_t cap, hcs1, hcs2, hcc, dboff, rtsoff;
592 uint32_t ecp, ecr;
593 uint32_t usbcmd, usbsts, pagesize, config;
594 int i;
595 uint16_t hciversion;
596 uint8_t caplength;
597
598 XHCIHIST_FUNC(); XHCIHIST_CALLED();
599
600 /* XXX Low/Full/High speeds for now */
601 sc->sc_bus.ub_revision = USBREV_2_0;
602 sc->sc_bus.ub_usedma = true;
603
604 cap = xhci_read_4(sc, XHCI_CAPLENGTH);
605 caplength = XHCI_CAP_CAPLENGTH(cap);
606 hciversion = XHCI_CAP_HCIVERSION(cap);
607
608 if ((hciversion < 0x0096) || (hciversion > 0x0100)) {
609 aprint_normal_dev(sc->sc_dev,
610 "xHCI version %x.%x not known to be supported\n",
611 (hciversion >> 8) & 0xff, (hciversion >> 0) & 0xff);
612 } else {
613 aprint_verbose_dev(sc->sc_dev, "xHCI version %x.%x\n",
614 (hciversion >> 8) & 0xff, (hciversion >> 0) & 0xff);
615 }
616
617 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 0, caplength,
618 &sc->sc_cbh) != 0) {
619 aprint_error_dev(sc->sc_dev, "capability subregion failure\n");
620 return ENOMEM;
621 }
622
623 hcs1 = xhci_cap_read_4(sc, XHCI_HCSPARAMS1);
624 sc->sc_maxslots = XHCI_HCS1_MAXSLOTS(hcs1);
625 sc->sc_maxintrs = XHCI_HCS1_MAXINTRS(hcs1);
626 sc->sc_maxports = XHCI_HCS1_MAXPORTS(hcs1);
627 hcs2 = xhci_cap_read_4(sc, XHCI_HCSPARAMS2);
628 (void)xhci_cap_read_4(sc, XHCI_HCSPARAMS3);
629 hcc = xhci_cap_read_4(sc, XHCI_HCCPARAMS);
630
631 sc->sc_ac64 = XHCI_HCC_AC64(hcc);
632 sc->sc_ctxsz = XHCI_HCC_CSZ(hcc) ? 64 : 32;
633 aprint_debug_dev(sc->sc_dev, "ac64 %d ctxsz %d\n", sc->sc_ac64,
634 sc->sc_ctxsz);
635
636 aprint_debug_dev(sc->sc_dev, "xECP %x\n", XHCI_HCC_XECP(hcc) * 4);
637 ecp = XHCI_HCC_XECP(hcc) * 4;
638 while (ecp != 0) {
639 ecr = xhci_read_4(sc, ecp);
640 aprint_debug_dev(sc->sc_dev, "ECR %x: %08x\n", ecp, ecr);
641 switch (XHCI_XECP_ID(ecr)) {
642 case XHCI_ID_PROTOCOLS: {
643 uint32_t w0, w4, w8;
644 uint16_t w2;
645 w0 = xhci_read_4(sc, ecp + 0);
646 w2 = (w0 >> 16) & 0xffff;
647 w4 = xhci_read_4(sc, ecp + 4);
648 w8 = xhci_read_4(sc, ecp + 8);
649 aprint_debug_dev(sc->sc_dev, "SP: %08x %08x %08x\n",
650 w0, w4, w8);
651 if (w4 == 0x20425355 && w2 == 0x0300) {
652 sc->sc_ss_port_start = (w8 >> 0) & 0xff;;
653 sc->sc_ss_port_count = (w8 >> 8) & 0xff;;
654 }
655 if (w4 == 0x20425355 && w2 == 0x0200) {
656 sc->sc_hs_port_start = (w8 >> 0) & 0xff;
657 sc->sc_hs_port_count = (w8 >> 8) & 0xff;
658 }
659 break;
660 }
661 default:
662 break;
663 }
664 ecr = xhci_read_4(sc, ecp);
665 if (XHCI_XECP_NEXT(ecr) == 0) {
666 ecp = 0;
667 } else {
668 ecp += XHCI_XECP_NEXT(ecr) * 4;
669 }
670 }
671
672 bsz = XHCI_PORTSC(sc->sc_maxports + 1);
673 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, caplength, bsz,
674 &sc->sc_obh) != 0) {
675 aprint_error_dev(sc->sc_dev, "operational subregion failure\n");
676 return ENOMEM;
677 }
678
679 dboff = xhci_cap_read_4(sc, XHCI_DBOFF);
680 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, dboff,
681 sc->sc_maxslots * 4, &sc->sc_dbh) != 0) {
682 aprint_error_dev(sc->sc_dev, "doorbell subregion failure\n");
683 return ENOMEM;
684 }
685
686 rtsoff = xhci_cap_read_4(sc, XHCI_RTSOFF);
687 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, rtsoff,
688 sc->sc_maxintrs * 0x20, &sc->sc_rbh) != 0) {
689 aprint_error_dev(sc->sc_dev, "runtime subregion failure\n");
690 return ENOMEM;
691 }
692
693 for (i = 0; i < 100; i++) {
694 usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
695 if ((usbsts & XHCI_STS_CNR) == 0)
696 break;
697 usb_delay_ms(&sc->sc_bus, 1);
698 }
699 if (i >= 100)
700 return EIO;
701
702 usbcmd = 0;
703 xhci_op_write_4(sc, XHCI_USBCMD, usbcmd);
704 usb_delay_ms(&sc->sc_bus, 1);
705
706 usbcmd = XHCI_CMD_HCRST;
707 xhci_op_write_4(sc, XHCI_USBCMD, usbcmd);
708 for (i = 0; i < 100; i++) {
709 usbcmd = xhci_op_read_4(sc, XHCI_USBCMD);
710 if ((usbcmd & XHCI_CMD_HCRST) == 0)
711 break;
712 usb_delay_ms(&sc->sc_bus, 1);
713 }
714 if (i >= 100)
715 return EIO;
716
717 for (i = 0; i < 100; i++) {
718 usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
719 if ((usbsts & XHCI_STS_CNR) == 0)
720 break;
721 usb_delay_ms(&sc->sc_bus, 1);
722 }
723 if (i >= 100)
724 return EIO;
725
726 pagesize = xhci_op_read_4(sc, XHCI_PAGESIZE);
727 aprint_debug_dev(sc->sc_dev, "PAGESIZE 0x%08x\n", pagesize);
728 pagesize = ffs(pagesize);
729 if (pagesize == 0)
730 return EIO;
731 sc->sc_pgsz = 1 << (12 + (pagesize - 1));
732 aprint_debug_dev(sc->sc_dev, "sc_pgsz 0x%08x\n", (uint32_t)sc->sc_pgsz);
733 aprint_debug_dev(sc->sc_dev, "sc_maxslots 0x%08x\n",
734 (uint32_t)sc->sc_maxslots);
735
736 usbd_status err;
737
738 sc->sc_maxspbuf = XHCI_HCS2_MAXSPBUF(hcs2);
739 aprint_debug_dev(sc->sc_dev, "sc_maxspbuf %d\n", sc->sc_maxspbuf);
740 if (sc->sc_maxspbuf != 0) {
741 err = usb_allocmem(&sc->sc_bus,
742 sizeof(uint64_t) * sc->sc_maxspbuf, sizeof(uint64_t),
743 &sc->sc_spbufarray_dma);
744 if (err)
745 return err;
746
747 sc->sc_spbuf_dma = kmem_zalloc(sizeof(*sc->sc_spbuf_dma) * sc->sc_maxspbuf, KM_SLEEP);
748 uint64_t *spbufarray = KERNADDR(&sc->sc_spbufarray_dma, 0);
749 for (i = 0; i < sc->sc_maxspbuf; i++) {
750 usb_dma_t * const dma = &sc->sc_spbuf_dma[i];
751 /* allocate contexts */
752 err = usb_allocmem(&sc->sc_bus, sc->sc_pgsz,
753 sc->sc_pgsz, dma);
754 if (err)
755 return err;
756 spbufarray[i] = htole64(DMAADDR(dma, 0));
757 usb_syncmem(dma, 0, sc->sc_pgsz,
758 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
759 }
760
761 usb_syncmem(&sc->sc_spbufarray_dma, 0,
762 sizeof(uint64_t) * sc->sc_maxspbuf, BUS_DMASYNC_PREWRITE);
763 }
764
765 config = xhci_op_read_4(sc, XHCI_CONFIG);
766 config &= ~0xFF;
767 config |= sc->sc_maxslots & 0xFF;
768 xhci_op_write_4(sc, XHCI_CONFIG, config);
769
770 err = xhci_ring_init(sc, &sc->sc_cr, XHCI_COMMAND_RING_TRBS,
771 XHCI_COMMAND_RING_SEGMENTS_ALIGN);
772 if (err) {
773 aprint_error_dev(sc->sc_dev, "command ring init fail\n");
774 return err;
775 }
776
777 err = xhci_ring_init(sc, &sc->sc_er, XHCI_EVENT_RING_TRBS,
778 XHCI_EVENT_RING_SEGMENTS_ALIGN);
779 if (err) {
780 aprint_error_dev(sc->sc_dev, "event ring init fail\n");
781 return err;
782 }
783
784 usb_dma_t *dma;
785 size_t size;
786 size_t align;
787
788 dma = &sc->sc_eventst_dma;
789 size = roundup2(XHCI_EVENT_RING_SEGMENTS * XHCI_ERSTE_SIZE,
790 XHCI_EVENT_RING_SEGMENT_TABLE_ALIGN);
791 KASSERT(size <= (512 * 1024));
792 align = XHCI_EVENT_RING_SEGMENT_TABLE_ALIGN;
793 err = usb_allocmem(&sc->sc_bus, size, align, dma);
794
795 memset(KERNADDR(dma, 0), 0, size);
796 usb_syncmem(dma, 0, size, BUS_DMASYNC_PREWRITE);
797 aprint_debug_dev(sc->sc_dev, "eventst: %s %016jx %p %zx\n",
798 usbd_errstr(err),
799 (uintmax_t)DMAADDR(&sc->sc_eventst_dma, 0),
800 KERNADDR(&sc->sc_eventst_dma, 0),
801 sc->sc_eventst_dma.udma_block->size);
802
803 dma = &sc->sc_dcbaa_dma;
804 size = (1 + sc->sc_maxslots) * sizeof(uint64_t);
805 KASSERT(size <= 2048);
806 align = XHCI_DEVICE_CONTEXT_BASE_ADDRESS_ARRAY_ALIGN;
807 err = usb_allocmem(&sc->sc_bus, size, align, dma);
808
809 memset(KERNADDR(dma, 0), 0, size);
810 if (sc->sc_maxspbuf != 0) {
811 /*
812 * DCBA entry 0 hold the scratchbuf array pointer.
813 */
814 *(uint64_t *)KERNADDR(dma, 0) =
815 htole64(DMAADDR(&sc->sc_spbufarray_dma, 0));
816 }
817 usb_syncmem(dma, 0, size, BUS_DMASYNC_PREWRITE);
818 aprint_debug_dev(sc->sc_dev, "dcbaa: %s %016jx %p %zx\n",
819 usbd_errstr(err),
820 (uintmax_t)DMAADDR(&sc->sc_dcbaa_dma, 0),
821 KERNADDR(&sc->sc_dcbaa_dma, 0),
822 sc->sc_dcbaa_dma.udma_block->size);
823
824 sc->sc_slots = kmem_zalloc(sizeof(*sc->sc_slots) * sc->sc_maxslots,
825 KM_SLEEP);
826
827 cv_init(&sc->sc_command_cv, "xhcicmd");
828
829 struct xhci_erste *erst;
830 erst = KERNADDR(&sc->sc_eventst_dma, 0);
831 erst[0].erste_0 = htole64(xhci_ring_trbp(&sc->sc_er, 0));
832 erst[0].erste_2 = htole32(XHCI_EVENT_RING_TRBS);
833 erst[0].erste_3 = htole32(0);
834 usb_syncmem(&sc->sc_eventst_dma, 0,
835 XHCI_ERSTE_SIZE * XHCI_EVENT_RING_SEGMENTS, BUS_DMASYNC_PREWRITE);
836
837 xhci_rt_write_4(sc, XHCI_ERSTSZ(0), XHCI_EVENT_RING_SEGMENTS);
838 xhci_rt_write_8(sc, XHCI_ERSTBA(0), DMAADDR(&sc->sc_eventst_dma, 0));
839 xhci_rt_write_8(sc, XHCI_ERDP(0), xhci_ring_trbp(&sc->sc_er, 0) |
840 XHCI_ERDP_LO_BUSY);
841 xhci_op_write_8(sc, XHCI_DCBAAP, DMAADDR(&sc->sc_dcbaa_dma, 0));
842 xhci_op_write_8(sc, XHCI_CRCR, xhci_ring_trbp(&sc->sc_cr, 0) |
843 sc->sc_cr.xr_cs);
844
845 #if 0
846 hexdump("eventst", KERNADDR(&sc->sc_eventst_dma, 0),
847 XHCI_ERSTE_SIZE * XHCI_EVENT_RING_SEGMENTS);
848 #endif
849
850 xhci_rt_write_4(sc, XHCI_IMAN(0), XHCI_IMAN_INTR_ENA);
851 xhci_rt_write_4(sc, XHCI_IMOD(0), 0);
852
853 xhci_op_write_4(sc, XHCI_USBCMD, XHCI_CMD_INTE|XHCI_CMD_RS); /* Go! */
854 aprint_debug_dev(sc->sc_dev, "USBCMD %08"PRIx32"\n",
855 xhci_op_read_4(sc, XHCI_USBCMD));
856
857 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
858 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SCHED);
859 cv_init(&sc->sc_softwake_cv, "xhciab");
860
861 sc->sc_xferpool = pool_cache_init(sizeof(struct xhci_xfer), 0, 0, 0,
862 "xhcixfer", NULL, IPL_USB, NULL, NULL, NULL);
863
864 /* Set up the bus struct. */
865 sc->sc_bus.ub_methods = &xhci_bus_methods;
866 sc->sc_bus.ub_pipesize = sizeof(struct xhci_pipe);
867
868 return USBD_NORMAL_COMPLETION;
869 }
870
871 int
872 xhci_intr(void *v)
873 {
874 struct xhci_softc * const sc = v;
875 int ret = 0;
876
877 XHCIHIST_FUNC(); XHCIHIST_CALLED();
878
879 if (sc == NULL)
880 return 0;
881
882 mutex_spin_enter(&sc->sc_intr_lock);
883
884 if (sc->sc_dying || !device_has_power(sc->sc_dev))
885 goto done;
886
887 /* If we get an interrupt while polling, then just ignore it. */
888 if (sc->sc_bus.ub_usepolling) {
889 #ifdef DIAGNOSTIC
890 DPRINTFN(16, "ignored interrupt while polling", 0, 0, 0, 0);
891 #endif
892 goto done;
893 }
894
895 ret = xhci_intr1(sc);
896 done:
897 mutex_spin_exit(&sc->sc_intr_lock);
898 return ret;
899 }
900
901 int
902 xhci_intr1(struct xhci_softc * const sc)
903 {
904 uint32_t usbsts;
905 uint32_t iman;
906
907 XHCIHIST_FUNC(); XHCIHIST_CALLED();
908
909 usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
910 DPRINTFN(16, "USBSTS %08x", usbsts, 0, 0, 0);
911 #if 0
912 if ((usbsts & (XHCI_STS_EINT|XHCI_STS_PCD)) == 0) {
913 return 0;
914 }
915 #endif
916 xhci_op_write_4(sc, XHCI_USBSTS,
917 usbsts & (2|XHCI_STS_EINT|XHCI_STS_PCD)); /* XXX */
918 usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
919 DPRINTFN(16, "USBSTS %08x", usbsts, 0, 0, 0);
920
921 iman = xhci_rt_read_4(sc, XHCI_IMAN(0));
922 DPRINTFN(16, "IMAN0 %08x", iman, 0, 0, 0);
923 if ((iman & XHCI_IMAN_INTR_PEND) == 0) {
924 return 0;
925 }
926 xhci_rt_write_4(sc, XHCI_IMAN(0), iman);
927 iman = xhci_rt_read_4(sc, XHCI_IMAN(0));
928 DPRINTFN(16, "IMAN0 %08x", iman, 0, 0, 0);
929 usbsts = xhci_op_read_4(sc, XHCI_USBSTS);
930 DPRINTFN(16, "USBSTS %08x", usbsts, 0, 0, 0);
931
932 usb_schedsoftintr(&sc->sc_bus);
933
934 return 1;
935 }
936
937 static usbd_status
938 xhci_configure_endpoint(usbd_pipe_handle pipe)
939 {
940 struct xhci_softc * const sc = pipe->up_dev->ud_bus->ub_hcpriv;
941 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
942 const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
943 usb_endpoint_descriptor_t * const ed = pipe->up_endpoint->ue_edesc;
944 const uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
945 struct xhci_trb trb;
946 usbd_status err;
947 uint32_t *cp;
948
949 XHCIHIST_FUNC(); XHCIHIST_CALLED();
950 DPRINTFN(4, "dci %u epaddr 0x%02x attr 0x%02x",
951 dci, ed->bEndpointAddress, ed->bmAttributes, 0);
952
953 /* XXX ensure input context is available? */
954
955 memset(xhci_slot_get_icv(sc, xs, 0), 0, sc->sc_pgsz);
956
957 cp = xhci_slot_get_icv(sc, xs, XHCI_ICI_INPUT_CONTROL);
958 cp[0] = htole32(0);
959 cp[1] = htole32(XHCI_INCTX_1_ADD_MASK(dci));
960
961 /* set up input slot context */
962 cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_SLOT));
963 cp[0] = htole32(XHCI_SCTX_0_CTX_NUM_SET(dci));
964 cp[1] = htole32(0);
965 cp[2] = htole32(0);
966 cp[3] = htole32(0);
967
968 cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(dci));
969 if (xfertype == UE_INTERRUPT) {
970 cp[0] = htole32(
971 XHCI_EPCTX_0_IVAL_SET(3) /* XXX */
972 );
973 cp[1] = htole32(
974 XHCI_EPCTX_1_CERR_SET(3) |
975 XHCI_EPCTX_1_EPTYPE_SET(xhci_ep_get_type(pipe->up_endpoint->ue_edesc)) |
976 XHCI_EPCTX_1_MAXB_SET(0) |
977 XHCI_EPCTX_1_MAXP_SIZE_SET(8) /* XXX */
978 );
979 cp[4] = htole32(
980 XHCI_EPCTX_4_AVG_TRB_LEN_SET(8)
981 );
982 } else {
983 cp[0] = htole32(0);
984 cp[1] = htole32(
985 XHCI_EPCTX_1_CERR_SET(3) |
986 XHCI_EPCTX_1_EPTYPE_SET(xhci_ep_get_type(pipe->up_endpoint->ue_edesc)) |
987 XHCI_EPCTX_1_MAXB_SET(0) |
988 XHCI_EPCTX_1_MAXP_SIZE_SET(512) /* XXX */
989 );
990 }
991 *(uint64_t *)(&cp[2]) = htole64(
992 xhci_ring_trbp(&xs->xs_ep[dci].xe_tr, 0) |
993 XHCI_EPCTX_2_DCS_SET(1));
994
995 /* sync input contexts before they are read from memory */
996 usb_syncmem(&xs->xs_ic_dma, 0, sc->sc_pgsz, BUS_DMASYNC_PREWRITE);
997 hexdump("input control context", xhci_slot_get_icv(sc, xs, 0),
998 sc->sc_ctxsz * 1);
999 hexdump("input endpoint context", xhci_slot_get_icv(sc, xs,
1000 xhci_dci_to_ici(dci)), sc->sc_ctxsz * 1);
1001
1002 trb.trb_0 = xhci_slot_get_icp(sc, xs, 0);
1003 trb.trb_2 = 0;
1004 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
1005 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_CONFIGURE_EP);
1006
1007 err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
1008
1009 usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD);
1010 hexdump("output context", xhci_slot_get_dcv(sc, xs, dci),
1011 sc->sc_ctxsz * 1);
1012
1013 return err;
1014 }
1015
1016 static usbd_status
1017 xhci_unconfigure_endpoint(usbd_pipe_handle pipe)
1018 {
1019 #ifdef USB_DEBUG
1020 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
1021 #endif
1022
1023 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1024 DPRINTFN(4, "slot %u", xs->xs_idx, 0, 0, 0);
1025
1026 return USBD_NORMAL_COMPLETION;
1027 }
1028
1029 static usbd_status
1030 xhci_reset_endpoint(usbd_pipe_handle pipe)
1031 {
1032 struct xhci_softc * const sc = pipe->up_dev->ud_bus->ub_hcpriv;
1033 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
1034 const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
1035 struct xhci_trb trb;
1036 usbd_status err;
1037
1038 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1039 DPRINTFN(4, "dci %u", dci, 0, 0, 0);
1040
1041 trb.trb_0 = 0;
1042 trb.trb_2 = 0;
1043 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
1044 XHCI_TRB_3_EP_SET(dci) |
1045 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_RESET_EP);
1046
1047 err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
1048
1049 return err;
1050 }
1051
1052 #if 0
1053 static usbd_status
1054 xhci_stop_endpoint(usbd_pipe_handle pipe)
1055 {
1056 struct xhci_softc * const sc = pipe->up_dev->ud_bus->ub_hcpriv;
1057 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
1058 struct xhci_trb trb;
1059 usbd_status err;
1060 const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
1061
1062 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1063 DPRINTFN(4, "dci %u", dci, 0, 0, 0);
1064
1065 trb.trb_0 = 0;
1066 trb.trb_2 = 0;
1067 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
1068 XHCI_TRB_3_EP_SET(dci) |
1069 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STOP_EP);
1070
1071 err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
1072
1073 return err;
1074 }
1075 #endif
1076
1077 static usbd_status
1078 xhci_set_dequeue(usbd_pipe_handle pipe)
1079 {
1080 struct xhci_softc * const sc = pipe->up_dev->ud_bus->ub_hcpriv;
1081 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
1082 const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
1083 struct xhci_ring * const xr = &xs->xs_ep[dci].xe_tr;
1084 struct xhci_trb trb;
1085 usbd_status err;
1086
1087 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1088 DPRINTFN(4, "slot %u dci %u", xs->xs_idx, dci, 0, 0);
1089
1090 memset(xr->xr_trb, 0, xr->xr_ntrb * XHCI_TRB_SIZE);
1091 usb_syncmem(&xr->xr_dma, 0, xr->xr_ntrb * XHCI_TRB_SIZE,
1092 BUS_DMASYNC_PREWRITE);
1093
1094 xr->xr_ep = 0;
1095 xr->xr_cs = 1;
1096
1097 trb.trb_0 = xhci_ring_trbp(xr, 0) | 1; /* XXX */
1098 trb.trb_2 = 0;
1099 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
1100 XHCI_TRB_3_EP_SET(dci) |
1101 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SET_TR_DEQUEUE);
1102
1103 err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
1104
1105 return err;
1106 }
1107
1108 static usbd_status
1109 xhci_open(usbd_pipe_handle pipe)
1110 {
1111 usbd_device_handle const dev = pipe->up_dev;
1112 struct xhci_softc * const sc = dev->ud_bus->ub_hcpriv;
1113 usb_endpoint_descriptor_t * const ed = pipe->up_endpoint->ue_edesc;
1114 const uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
1115
1116 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1117 DPRINTFN(1, "addr %d depth %d port %d speed %d",
1118 dev->ud_addr, dev->ud_depth, dev->ud_powersrc->up_portno, dev->ud_speed);
1119
1120 if (sc->sc_dying)
1121 return USBD_IOERROR;
1122
1123 /* Root Hub */
1124 if (dev->ud_depth == 0 && dev->ud_powersrc->up_portno == 0 &&
1125 dev->ud_speed != USB_SPEED_SUPER) {
1126 switch (ed->bEndpointAddress) {
1127 case USB_CONTROL_ENDPOINT:
1128 pipe->up_methods = &roothub_ctrl_methods;
1129 break;
1130 case UE_DIR_IN | USBROOTHUB_INTR_ENDPT:
1131 pipe->up_methods = &xhci_root_intr_methods;
1132 break;
1133 default:
1134 pipe->up_methods = NULL;
1135 DPRINTFN(0, "bad bEndpointAddress 0x%02x",
1136 ed->bEndpointAddress, 0, 0, 0);
1137 return USBD_INVAL;
1138 }
1139 return USBD_NORMAL_COMPLETION;
1140 }
1141
1142 switch (xfertype) {
1143 case UE_CONTROL:
1144 pipe->up_methods = &xhci_device_ctrl_methods;
1145 break;
1146 case UE_ISOCHRONOUS:
1147 pipe->up_methods = &xhci_device_isoc_methods;
1148 return USBD_INVAL;
1149 break;
1150 case UE_BULK:
1151 pipe->up_methods = &xhci_device_bulk_methods;
1152 break;
1153 case UE_INTERRUPT:
1154 pipe->up_methods = &xhci_device_intr_methods;
1155 break;
1156 default:
1157 return USBD_IOERROR;
1158 break;
1159 }
1160
1161 if (ed->bEndpointAddress != USB_CONTROL_ENDPOINT)
1162 xhci_configure_endpoint(pipe);
1163
1164 return USBD_NORMAL_COMPLETION;
1165 }
1166
1167 static void
1168 xhci_rhpsc(struct xhci_softc * const sc, u_int port)
1169 {
1170 usbd_xfer_handle const xfer = sc->sc_intrxfer;
1171 uint8_t *p;
1172
1173 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1174 DPRINTFN(4, "port %u status change", port, 0, 0, 0);
1175
1176 if (xfer == NULL)
1177 return;
1178
1179 if (!(port >= sc->sc_hs_port_start &&
1180 port < sc->sc_hs_port_start + sc->sc_hs_port_count))
1181 return;
1182
1183 port -= sc->sc_hs_port_start;
1184 port += 1;
1185 DPRINTFN(4, "hs port %u status change", port, 0, 0, 0);
1186
1187 p = xfer->ux_buf;
1188 memset(p, 0, xfer->ux_length);
1189 p[port/NBBY] |= 1 << (port%NBBY);
1190 xfer->ux_actlen = xfer->ux_length;
1191 xfer->ux_status = USBD_NORMAL_COMPLETION;
1192 usb_transfer_complete(xfer);
1193 }
1194
1195 static void
1196 xhci_handle_event(struct xhci_softc * const sc,
1197 const struct xhci_trb * const trb)
1198 {
1199 uint64_t trb_0;
1200 uint32_t trb_2, trb_3;
1201
1202 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1203
1204 trb_0 = le64toh(trb->trb_0);
1205 trb_2 = le32toh(trb->trb_2);
1206 trb_3 = le32toh(trb->trb_3);
1207
1208 DPRINTFN(14, "event: %p 0x%016"PRIx64" 0x%08"PRIx32" 0x%08"PRIx32,
1209 trb, trb_0, trb_2, trb_3);
1210
1211 switch (XHCI_TRB_3_TYPE_GET(trb_3)){
1212 case XHCI_TRB_EVENT_TRANSFER: {
1213 u_int slot, dci;
1214 struct xhci_slot *xs;
1215 struct xhci_ring *xr;
1216 struct xhci_xfer *xx;
1217 usbd_xfer_handle xfer;
1218 usbd_status err;
1219
1220 slot = XHCI_TRB_3_SLOT_GET(trb_3);
1221 dci = XHCI_TRB_3_EP_GET(trb_3);
1222
1223 xs = &sc->sc_slots[slot];
1224 xr = &xs->xs_ep[dci].xe_tr;
1225
1226 if ((trb_3 & XHCI_TRB_3_ED_BIT) == 0) {
1227 xx = xr->xr_cookies[(trb_0 - xhci_ring_trbp(xr, 0))/
1228 sizeof(struct xhci_trb)];
1229 } else {
1230 xx = (void *)(uintptr_t)(trb_0 & ~0x3);
1231 }
1232 xfer = &xx->xx_xfer;
1233 DPRINTFN(14, "xfer %p", xfer, 0, 0, 0);
1234
1235 if ((trb_3 & XHCI_TRB_3_ED_BIT) != 0) {
1236 DPRINTFN(14, "transfer event data: "
1237 "0x%016"PRIx64" 0x%08"PRIx32" %02x",
1238 trb_0, XHCI_TRB_2_REM_GET(trb_2),
1239 XHCI_TRB_2_ERROR_GET(trb_2), 0);
1240 if ((trb_0 & 0x3) == 0x3) {
1241 xfer->ux_actlen = XHCI_TRB_2_REM_GET(trb_2);
1242 }
1243 }
1244
1245 if (XHCI_TRB_2_ERROR_GET(trb_2) ==
1246 XHCI_TRB_ERROR_SUCCESS) {
1247 xfer->ux_actlen = xfer->ux_length - XHCI_TRB_2_REM_GET(trb_2);
1248 err = USBD_NORMAL_COMPLETION;
1249 } else if (XHCI_TRB_2_ERROR_GET(trb_2) ==
1250 XHCI_TRB_ERROR_SHORT_PKT) {
1251 xfer->ux_actlen = xfer->ux_length - XHCI_TRB_2_REM_GET(trb_2);
1252 err = USBD_NORMAL_COMPLETION;
1253 } else if (XHCI_TRB_2_ERROR_GET(trb_2) ==
1254 XHCI_TRB_ERROR_STALL) {
1255 err = USBD_STALLED;
1256 xr->is_halted = true;
1257 DPRINTFN(1, "ev: xfer done: err %u slot %u dci %u",
1258 XHCI_TRB_2_ERROR_GET(trb_2), slot, dci, 0);
1259 } else {
1260 err = USBD_IOERROR;
1261 }
1262 xfer->ux_status = err;
1263
1264 //mutex_enter(&sc->sc_lock); /* XXX ??? */
1265 if ((trb_3 & XHCI_TRB_3_ED_BIT) != 0) {
1266 if ((trb_0 & 0x3) == 0x0) {
1267 usb_transfer_complete(xfer);
1268 }
1269 } else {
1270 usb_transfer_complete(xfer);
1271 }
1272 //mutex_exit(&sc->sc_lock); /* XXX ??? */
1273
1274 }
1275 break;
1276 case XHCI_TRB_EVENT_CMD_COMPLETE:
1277 if (trb_0 == sc->sc_command_addr) {
1278 sc->sc_result_trb.trb_0 = trb_0;
1279 sc->sc_result_trb.trb_2 = trb_2;
1280 sc->sc_result_trb.trb_3 = trb_3;
1281 if (XHCI_TRB_2_ERROR_GET(trb_2) !=
1282 XHCI_TRB_ERROR_SUCCESS) {
1283 DPRINTFN(1, "command completion "
1284 "failure: 0x%016"PRIx64" 0x%08"PRIx32" "
1285 "0x%08"PRIx32, trb_0, trb_2, trb_3, 0);
1286 }
1287 cv_signal(&sc->sc_command_cv);
1288 } else {
1289 DPRINTFN(1, "event: %p 0x%016"PRIx64" "
1290 "0x%08"PRIx32" 0x%08"PRIx32, trb, trb_0,
1291 trb_2, trb_3);
1292 }
1293 break;
1294 case XHCI_TRB_EVENT_PORT_STS_CHANGE:
1295 xhci_rhpsc(sc, (uint32_t)((trb_0 >> 24) & 0xff));
1296 break;
1297 default:
1298 break;
1299 }
1300 }
1301
1302 static void
1303 xhci_softintr(void *v)
1304 {
1305 struct usbd_bus * const bus = v;
1306 struct xhci_softc * const sc = bus->ub_hcpriv;
1307 struct xhci_ring * const er = &sc->sc_er;
1308 struct xhci_trb *trb;
1309 int i, j, k;
1310
1311 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1312
1313 i = er->xr_ep;
1314 j = er->xr_cs;
1315
1316 DPRINTFN(16, "xr_ep %d xr_cs %d", i, j, 0, 0);
1317
1318 while (1) {
1319 usb_syncmem(&er->xr_dma, XHCI_TRB_SIZE * i, XHCI_TRB_SIZE,
1320 BUS_DMASYNC_POSTREAD);
1321 trb = &er->xr_trb[i];
1322 k = (le32toh(trb->trb_3) & XHCI_TRB_3_CYCLE_BIT) ? 1 : 0;
1323
1324 if (j != k)
1325 break;
1326
1327 xhci_handle_event(sc, trb);
1328
1329 i++;
1330 if (i == XHCI_EVENT_RING_TRBS) {
1331 i = 0;
1332 j ^= 1;
1333 }
1334 }
1335
1336 er->xr_ep = i;
1337 er->xr_cs = j;
1338
1339 xhci_rt_write_8(sc, XHCI_ERDP(0), xhci_ring_trbp(er, er->xr_ep) |
1340 XHCI_ERDP_LO_BUSY);
1341
1342 DPRINTFN(16, "ends", 0, 0, 0, 0);
1343
1344 return;
1345 }
1346
1347 static void
1348 xhci_poll(struct usbd_bus *bus)
1349 {
1350 struct xhci_softc * const sc = bus->ub_hcpriv;
1351
1352 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1353
1354 mutex_spin_enter(&sc->sc_intr_lock);
1355 xhci_intr1(sc);
1356 mutex_spin_exit(&sc->sc_intr_lock);
1357
1358 return;
1359 }
1360
1361 static usbd_xfer_handle
1362 xhci_allocx(struct usbd_bus *bus)
1363 {
1364 struct xhci_softc * const sc = bus->ub_hcpriv;
1365 usbd_xfer_handle xfer;
1366
1367 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1368
1369 xfer = pool_cache_get(sc->sc_xferpool, PR_NOWAIT);
1370 if (xfer != NULL) {
1371 memset(xfer, 0, sizeof(struct xhci_xfer));
1372 #ifdef DIAGNOSTIC
1373 xfer->ux_state = XFER_BUSY;
1374 #endif
1375 }
1376
1377 return xfer;
1378 }
1379
1380 static void
1381 xhci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
1382 {
1383 struct xhci_softc * const sc = bus->ub_hcpriv;
1384
1385 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1386
1387 #ifdef DIAGNOSTIC
1388 if (xfer->ux_state != XFER_BUSY) {
1389 DPRINTFN(0, "xfer=%p not busy, 0x%08x",
1390 xfer, xfer->ux_state, 0, 0);
1391 }
1392 xfer->ux_state = XFER_FREE;
1393 #endif
1394 pool_cache_put(sc->sc_xferpool, xfer);
1395 }
1396
1397 static void
1398 xhci_get_lock(struct usbd_bus *bus, kmutex_t **lock)
1399 {
1400 struct xhci_softc * const sc = bus->ub_hcpriv;
1401
1402 *lock = &sc->sc_lock;
1403 }
1404
1405 extern uint32_t usb_cookie_no;
1406
1407 static usbd_status
1408 xhci_new_device(device_t parent, usbd_bus_handle bus, int depth,
1409 int speed, int port, struct usbd_port *up)
1410 {
1411 struct xhci_softc * const sc = bus->ub_hcpriv;
1412 usbd_device_handle dev;
1413 usbd_status err;
1414 usb_device_descriptor_t *dd;
1415 struct usbd_device *hub;
1416 struct usbd_device *adev;
1417 int rhport = 0;
1418 struct xhci_slot *xs;
1419 uint32_t *cp;
1420 uint8_t slot;
1421 uint8_t addr;
1422
1423 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1424 DPRINTFN(4, "port=%d depth=%d speed=%d upport %d",
1425 port, depth, speed, up->up_portno);
1426
1427 dev = kmem_zalloc(sizeof(*dev), KM_SLEEP);
1428 if (dev == NULL)
1429 return USBD_NOMEM;
1430
1431 dev->ud_bus = bus;
1432
1433 /* Set up default endpoint handle. */
1434 dev->ud_ep0.ue_edesc = &dev->ud_ep0desc;
1435
1436 /* Set up default endpoint descriptor. */
1437 dev->ud_ep0desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
1438 dev->ud_ep0desc.bDescriptorType = UDESC_ENDPOINT;
1439 dev->ud_ep0desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
1440 dev->ud_ep0desc.bmAttributes = UE_CONTROL;
1441 /* XXX */
1442 if (speed == USB_SPEED_LOW)
1443 USETW(dev->ud_ep0desc.wMaxPacketSize, USB_MAX_IPACKET);
1444 else
1445 USETW(dev->ud_ep0desc.wMaxPacketSize, 64);
1446 dev->ud_ep0desc.bInterval = 0;
1447
1448 /* doesn't matter, just don't let it uninitialized */
1449 dev->ud_ep0.ue_toggle = 0;
1450
1451 DPRINTFN(4, "up %p portno %d", up, up->up_portno, 0, 0);
1452
1453 dev->ud_quirks = &usbd_no_quirk;
1454 dev->ud_addr = 0;
1455 dev->ud_ddesc.bMaxPacketSize = 0;
1456 dev->ud_depth = depth;
1457 dev->ud_powersrc = up;
1458 dev->ud_myhub = up->up_parent;
1459
1460 up->up_dev = dev;
1461
1462 /* Locate root hub port */
1463 for (adev = dev, hub = dev;
1464 hub != NULL;
1465 adev = hub, hub = hub->ud_myhub) {
1466 DPRINTFN(4, "hub %p", hub, 0, 0, 0);
1467 }
1468 DPRINTFN(4, "hub %p", hub, 0, 0, 0);
1469
1470 if (hub != NULL) {
1471 for (int p = 0; p < hub->ud_hub->uh_hubdesc.bNbrPorts; p++) {
1472 if (hub->ud_hub->uh_ports[p].up_dev == adev) {
1473 rhport = p;
1474 }
1475 }
1476 } else {
1477 rhport = port;
1478 }
1479 if (speed == USB_SPEED_SUPER) {
1480 rhport += sc->sc_ss_port_start - 1;
1481 } else {
1482 rhport += sc->sc_hs_port_start - 1;
1483 }
1484 DPRINTFN(4, "rhport %d", rhport, 0, 0, 0);
1485
1486 dev->ud_speed = speed;
1487 dev->ud_langid = USBD_NOLANG;
1488 dev->ud_cookie.cookie = ++usb_cookie_no;
1489
1490 /* Establish the default pipe. */
1491 err = usbd_setup_pipe(dev, 0, &dev->ud_ep0, USBD_DEFAULT_INTERVAL,
1492 &dev->ud_pipe0);
1493 if (err) {
1494 usbd_remove_device(dev, up);
1495 return err;
1496 }
1497
1498 dd = &dev->ud_ddesc;
1499
1500 if ((depth == 0) && (port == 0)) {
1501 KASSERT(bus->ub_devices[dev->ud_addr] == NULL);
1502 bus->ub_devices[dev->ud_addr] = dev;
1503 err = usbd_get_initial_ddesc(dev, dd);
1504 if (err)
1505 return err;
1506 err = usbd_reload_device_desc(dev);
1507 if (err)
1508 return err;
1509 } else {
1510 err = xhci_enable_slot(sc, &slot);
1511 if (err)
1512 return err;
1513 err = xhci_init_slot(sc, slot, depth, speed, port, rhport);
1514 if (err)
1515 return err;
1516 xs = &sc->sc_slots[slot];
1517 dev->ud_hcpriv = xs;
1518 cp = xhci_slot_get_dcv(sc, xs, XHCI_DCI_SLOT);
1519 //hexdump("slot context", cp, sc->sc_ctxsz);
1520 addr = XHCI_SCTX_3_DEV_ADDR_GET(cp[3]);
1521 DPRINTFN(4, "device address %u", addr, 0, 0, 0);
1522 /* XXX ensure we know when the hardware does something
1523 we can't yet cope with */
1524 KASSERT(addr >= 1 && addr <= 127);
1525 dev->ud_addr = addr;
1526 /* XXX dev->ud_addr not necessarily unique on bus */
1527 KASSERT(bus->ub_devices[dev->ud_addr] == NULL);
1528 bus->ub_devices[dev->ud_addr] = dev;
1529
1530 err = usbd_get_initial_ddesc(dev, dd);
1531 if (err)
1532 return err;
1533 /* 4.8.2.1 */
1534 if (speed == USB_SPEED_SUPER)
1535 USETW(dev->ud_ep0desc.wMaxPacketSize,
1536 (1 << dd->bMaxPacketSize));
1537 else
1538 USETW(dev->ud_ep0desc.wMaxPacketSize,
1539 dd->bMaxPacketSize);
1540 DPRINTFN(4, "bMaxPacketSize %u", dd->bMaxPacketSize, 0, 0, 0);
1541 xhci_update_ep0_mps(sc, xs,
1542 UGETW(dev->ud_ep0desc.wMaxPacketSize));
1543 err = usbd_reload_device_desc(dev);
1544 if (err)
1545 return err;
1546
1547 usbd_kill_pipe(dev->ud_pipe0);
1548 err = usbd_setup_pipe(dev, 0, &dev->ud_ep0,
1549 USBD_DEFAULT_INTERVAL, &dev->ud_pipe0);
1550 }
1551
1552 DPRINTFN(1, "adding unit addr=%d, rev=%02x,",
1553 dev->ud_addr, UGETW(dd->bcdUSB), 0, 0);
1554 DPRINTFN(1, " class=%d, subclass=%d, protocol=%d,",
1555 dd->bDeviceClass, dd->bDeviceSubClass,
1556 dd->bDeviceProtocol, 0);
1557 DPRINTFN(1, " mps=%d, len=%d, noconf=%d, speed=%d",
1558 dd->bMaxPacketSize, dd->bLength, dd->bNumConfigurations,
1559 dev->ud_speed);
1560
1561 usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
1562
1563 if ((depth == 0) && (port == 0)) {
1564 usbd_attach_roothub(parent, dev);
1565 DPRINTFN(1, "root_hub %p", bus->ub_roothub, 0, 0, 0);
1566 return USBD_NORMAL_COMPLETION;
1567 }
1568
1569
1570 err = usbd_probe_and_attach(parent, dev, port, dev->ud_addr);
1571 if (err) {
1572 usbd_remove_device(dev, up);
1573 return err;
1574 }
1575
1576 return USBD_NORMAL_COMPLETION;
1577 }
1578
1579 static usbd_status
1580 xhci_ring_init(struct xhci_softc * const sc, struct xhci_ring * const xr,
1581 size_t ntrb, size_t align)
1582 {
1583 usbd_status err;
1584 size_t size = ntrb * XHCI_TRB_SIZE;
1585
1586 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1587
1588 err = usb_allocmem(&sc->sc_bus, size, align, &xr->xr_dma);
1589 if (err)
1590 return err;
1591 mutex_init(&xr->xr_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
1592 xr->xr_cookies = kmem_zalloc(sizeof(*xr->xr_cookies) * ntrb, KM_SLEEP);
1593 xr->xr_trb = xhci_ring_trbv(xr, 0);
1594 xr->xr_ntrb = ntrb;
1595 xr->xr_ep = 0;
1596 xr->xr_cs = 1;
1597 memset(xr->xr_trb, 0, size);
1598 usb_syncmem(&xr->xr_dma, 0, size, BUS_DMASYNC_PREWRITE);
1599 xr->is_halted = false;
1600
1601 return USBD_NORMAL_COMPLETION;
1602 }
1603
1604 static void
1605 xhci_ring_free(struct xhci_softc * const sc, struct xhci_ring * const xr)
1606 {
1607 usb_freemem(&sc->sc_bus, &xr->xr_dma);
1608 mutex_destroy(&xr->xr_lock);
1609 kmem_free(xr->xr_cookies, sizeof(*xr->xr_cookies) * xr->xr_ntrb);
1610 }
1611
1612 static void
1613 xhci_ring_put(struct xhci_softc * const sc, struct xhci_ring * const xr,
1614 void *cookie, struct xhci_trb * const trbs, size_t ntrbs)
1615 {
1616 size_t i;
1617 u_int ri;
1618 u_int cs;
1619 uint64_t parameter;
1620 uint32_t status;
1621 uint32_t control;
1622
1623 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1624
1625 for (i = 0; i < ntrbs; i++) {
1626 DPRINTFN(12, "xr %p trbs %p num %zu", xr, trbs, i, 0);
1627 DPRINTFN(12, " %016"PRIx64" %08"PRIx32" %08"PRIx32,
1628 trbs[i].trb_0, trbs[i].trb_2, trbs[i].trb_3, 0);
1629 KASSERT(XHCI_TRB_3_TYPE_GET(trbs[i].trb_3) !=
1630 XHCI_TRB_TYPE_LINK);
1631 }
1632
1633 DPRINTFN(12, "%p xr_ep 0x%x xr_cs %u", xr, xr->xr_ep, xr->xr_cs, 0);
1634
1635 ri = xr->xr_ep;
1636 cs = xr->xr_cs;
1637
1638 /*
1639 * Although the xhci hardware can do scatter/gather dma from
1640 * arbitrary sized buffers, there is a non-obvious restriction
1641 * that a LINK trb is only allowed at the end of a burst of
1642 * transfers - which might be 16kB.
1643 * Arbitrary aligned LINK trb definitely fail on Ivy bridge.
1644 * The simple solution is not to allow a LINK trb in the middle
1645 * of anything - as here.
1646 * XXX: (dsl) There are xhci controllers out there (eg some made by
1647 * ASMedia) that seem to lock up if they process a LINK trb but
1648 * cannot process the linked-to trb yet.
1649 * The code should write the 'cycle' bit on the link trb AFTER
1650 * adding the other trb.
1651 */
1652 if (ri + ntrbs >= (xr->xr_ntrb - 1)) {
1653 parameter = xhci_ring_trbp(xr, 0);
1654 status = 0;
1655 control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK) |
1656 XHCI_TRB_3_TC_BIT | (cs ? XHCI_TRB_3_CYCLE_BIT : 0);
1657 xhci_trb_put(&xr->xr_trb[ri], htole64(parameter),
1658 htole32(status), htole32(control));
1659 usb_syncmem(&xr->xr_dma, XHCI_TRB_SIZE * ri, XHCI_TRB_SIZE * 1,
1660 BUS_DMASYNC_PREWRITE);
1661 xr->xr_cookies[ri] = NULL;
1662 xr->xr_ep = 0;
1663 xr->xr_cs ^= 1;
1664 ri = xr->xr_ep;
1665 cs = xr->xr_cs;
1666 }
1667
1668 ri++;
1669
1670 /* Write any subsequent TRB first */
1671 for (i = 1; i < ntrbs; i++) {
1672 parameter = trbs[i].trb_0;
1673 status = trbs[i].trb_2;
1674 control = trbs[i].trb_3;
1675
1676 if (cs) {
1677 control |= XHCI_TRB_3_CYCLE_BIT;
1678 } else {
1679 control &= ~XHCI_TRB_3_CYCLE_BIT;
1680 }
1681
1682 xhci_trb_put(&xr->xr_trb[ri], htole64(parameter),
1683 htole32(status), htole32(control));
1684 usb_syncmem(&xr->xr_dma, XHCI_TRB_SIZE * ri, XHCI_TRB_SIZE * 1,
1685 BUS_DMASYNC_PREWRITE);
1686 xr->xr_cookies[ri] = cookie;
1687 ri++;
1688 }
1689
1690 /* Write the first TRB last */
1691 i = 0;
1692 {
1693 parameter = trbs[i].trb_0;
1694 status = trbs[i].trb_2;
1695 control = trbs[i].trb_3;
1696
1697 if (xr->xr_cs) {
1698 control |= XHCI_TRB_3_CYCLE_BIT;
1699 } else {
1700 control &= ~XHCI_TRB_3_CYCLE_BIT;
1701 }
1702
1703 xhci_trb_put(&xr->xr_trb[xr->xr_ep], htole64(parameter),
1704 htole32(status), htole32(control));
1705 usb_syncmem(&xr->xr_dma, XHCI_TRB_SIZE * ri, XHCI_TRB_SIZE * 1,
1706 BUS_DMASYNC_PREWRITE);
1707 xr->xr_cookies[xr->xr_ep] = cookie;
1708 }
1709
1710 xr->xr_ep = ri;
1711 xr->xr_cs = cs;
1712
1713 DPRINTFN(12, "%p xr_ep 0x%x xr_cs %u", xr, xr->xr_ep, xr->xr_cs, 0);
1714 }
1715
1716 static usbd_status
1717 xhci_do_command(struct xhci_softc * const sc, struct xhci_trb * const trb,
1718 int timeout)
1719 {
1720 struct xhci_ring * const cr = &sc->sc_cr;
1721 usbd_status err;
1722
1723 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1724 DPRINTFN(12, "input: 0x%016"PRIx64" 0x%08"PRIx32" 0x%08"PRIx32,
1725 trb->trb_0, trb->trb_2, trb->trb_3, 0);
1726
1727 mutex_enter(&sc->sc_lock);
1728
1729 KASSERT(sc->sc_command_addr == 0);
1730 sc->sc_command_addr = xhci_ring_trbp(cr, cr->xr_ep);
1731
1732 mutex_enter(&cr->xr_lock);
1733 xhci_ring_put(sc, cr, NULL, trb, 1);
1734 mutex_exit(&cr->xr_lock);
1735
1736 xhci_db_write_4(sc, XHCI_DOORBELL(0), 0);
1737
1738 if (cv_timedwait(&sc->sc_command_cv, &sc->sc_lock,
1739 MAX(1, mstohz(timeout))) == EWOULDBLOCK) {
1740 err = USBD_TIMEOUT;
1741 goto timedout;
1742 }
1743
1744 trb->trb_0 = sc->sc_result_trb.trb_0;
1745 trb->trb_2 = sc->sc_result_trb.trb_2;
1746 trb->trb_3 = sc->sc_result_trb.trb_3;
1747
1748 DPRINTFN(12, "output: 0x%016"PRIx64" 0x%08"PRIx32" 0x%08"PRIx32"",
1749 trb->trb_0, trb->trb_2, trb->trb_3, 0);
1750
1751 switch (XHCI_TRB_2_ERROR_GET(trb->trb_2)) {
1752 case XHCI_TRB_ERROR_SUCCESS:
1753 err = USBD_NORMAL_COMPLETION;
1754 break;
1755 default:
1756 case 192 ... 223:
1757 err = USBD_IOERROR;
1758 break;
1759 case 224 ... 255:
1760 err = USBD_NORMAL_COMPLETION;
1761 break;
1762 }
1763
1764 timedout:
1765 sc->sc_command_addr = 0;
1766 mutex_exit(&sc->sc_lock);
1767 return err;
1768 }
1769
1770 static usbd_status
1771 xhci_enable_slot(struct xhci_softc * const sc, uint8_t * const slotp)
1772 {
1773 struct xhci_trb trb;
1774 usbd_status err;
1775
1776 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1777
1778 trb.trb_0 = 0;
1779 trb.trb_2 = 0;
1780 trb.trb_3 = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ENABLE_SLOT);
1781
1782 err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
1783 if (err != USBD_NORMAL_COMPLETION) {
1784 return err;
1785 }
1786
1787 *slotp = XHCI_TRB_3_SLOT_GET(trb.trb_3);
1788
1789 return err;
1790 }
1791
1792 static usbd_status
1793 xhci_address_device(struct xhci_softc * const sc,
1794 uint64_t icp, uint8_t slot_id, bool bsr)
1795 {
1796 struct xhci_trb trb;
1797 usbd_status err;
1798
1799 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1800
1801 trb.trb_0 = icp;
1802 trb.trb_2 = 0;
1803 trb.trb_3 = XHCI_TRB_3_SLOT_SET(slot_id) |
1804 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ADDRESS_DEVICE) |
1805 (bsr ? XHCI_TRB_3_BSR_BIT : 0);
1806
1807 err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
1808 return err;
1809 }
1810
1811 static usbd_status
1812 xhci_update_ep0_mps(struct xhci_softc * const sc,
1813 struct xhci_slot * const xs, u_int mps)
1814 {
1815 struct xhci_trb trb;
1816 usbd_status err;
1817 uint32_t * cp;
1818
1819 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1820 DPRINTFN(4, "slot %u mps %u", xs->xs_idx, mps, 0, 0);
1821
1822 cp = xhci_slot_get_icv(sc, xs, XHCI_ICI_INPUT_CONTROL);
1823 cp[0] = htole32(0);
1824 cp[1] = htole32(XHCI_INCTX_1_ADD_MASK(XHCI_DCI_EP_CONTROL));
1825
1826 cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_EP_CONTROL));
1827 cp[1] = htole32(XHCI_EPCTX_1_MAXP_SIZE_SET(mps));
1828
1829 /* sync input contexts before they are read from memory */
1830 usb_syncmem(&xs->xs_ic_dma, 0, sc->sc_pgsz, BUS_DMASYNC_PREWRITE);
1831 hexdump("input context", xhci_slot_get_icv(sc, xs, 0),
1832 sc->sc_ctxsz * 4);
1833
1834 trb.trb_0 = xhci_slot_get_icp(sc, xs, 0);
1835 trb.trb_2 = 0;
1836 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
1837 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_EVALUATE_CTX);
1838
1839 err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
1840 KASSERT(err == USBD_NORMAL_COMPLETION); /* XXX */
1841 return err;
1842 }
1843
1844 static void
1845 xhci_set_dcba(struct xhci_softc * const sc, uint64_t dcba, int si)
1846 {
1847 uint64_t * const dcbaa = KERNADDR(&sc->sc_dcbaa_dma, 0);
1848
1849 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1850 DPRINTFN(4, "dcbaa %p dc %016"PRIx64" slot %d",
1851 &dcbaa[si], dcba, si, 0);
1852
1853 dcbaa[si] = htole64(dcba);
1854 usb_syncmem(&sc->sc_dcbaa_dma, si * sizeof(uint64_t), sizeof(uint64_t),
1855 BUS_DMASYNC_PREWRITE);
1856 }
1857
1858 static usbd_status
1859 xhci_init_slot(struct xhci_softc * const sc, uint32_t slot, int depth,
1860 int speed, int port, int rhport)
1861 {
1862 struct xhci_slot *xs;
1863 usbd_status err;
1864 u_int dci;
1865 uint32_t *cp;
1866 uint32_t mps;
1867 uint32_t xspeed;
1868
1869 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1870 DPRINTFN(4, "slot %u depth %d speed %d",
1871 slot, depth, speed, 0);
1872 DPRINTFN(4, " port %d rhport %d",
1873 port, rhport, 0, 0);
1874
1875 switch (speed) {
1876 case USB_SPEED_LOW:
1877 xspeed = 2;
1878 mps = USB_MAX_IPACKET;
1879 break;
1880 case USB_SPEED_FULL:
1881 xspeed = 1;
1882 mps = 64;
1883 break;
1884 case USB_SPEED_HIGH:
1885 xspeed = 3;
1886 mps = USB_2_MAX_CTRL_PACKET;
1887 break;
1888 case USB_SPEED_SUPER:
1889 xspeed = 4;
1890 mps = USB_3_MAX_CTRL_PACKET;
1891 break;
1892 default:
1893 DPRINTFN(0, "impossible speed: %x", speed, 0, 0, 0);
1894 return USBD_INVAL;
1895 }
1896
1897 xs = &sc->sc_slots[slot];
1898 xs->xs_idx = slot;
1899
1900 /* allocate contexts */
1901 err = usb_allocmem(&sc->sc_bus, sc->sc_pgsz, sc->sc_pgsz,
1902 &xs->xs_dc_dma);
1903 if (err)
1904 return err;
1905 memset(KERNADDR(&xs->xs_dc_dma, 0), 0, sc->sc_pgsz);
1906
1907 err = usb_allocmem(&sc->sc_bus, sc->sc_pgsz, sc->sc_pgsz,
1908 &xs->xs_ic_dma);
1909 if (err)
1910 return err;
1911 memset(KERNADDR(&xs->xs_ic_dma, 0), 0, sc->sc_pgsz);
1912
1913 for (dci = 0; dci < 32; dci++) {
1914 //CTASSERT(sizeof(xs->xs_ep[dci]) == sizeof(struct xhci_endpoint));
1915 memset(&xs->xs_ep[dci], 0, sizeof(xs->xs_ep[dci]));
1916 if (dci == XHCI_DCI_SLOT)
1917 continue;
1918 err = xhci_ring_init(sc, &xs->xs_ep[dci].xe_tr,
1919 XHCI_TRANSFER_RING_TRBS, XHCI_TRB_ALIGN);
1920 if (err) {
1921 DPRINTFN(0, "ring init failure", 0, 0, 0, 0);
1922 return err;
1923 }
1924 }
1925
1926 /* set up initial input control context */
1927 cp = xhci_slot_get_icv(sc, xs, XHCI_ICI_INPUT_CONTROL);
1928 cp[0] = htole32(0);
1929 cp[1] = htole32(XHCI_INCTX_1_ADD_MASK(XHCI_DCI_EP_CONTROL)|
1930 XHCI_INCTX_1_ADD_MASK(XHCI_DCI_SLOT));
1931
1932 /* set up input slot context */
1933 cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_SLOT));
1934 cp[0] = htole32(
1935 XHCI_SCTX_0_CTX_NUM_SET(1) |
1936 XHCI_SCTX_0_SPEED_SET(xspeed)
1937 );
1938 cp[1] = htole32(
1939 XHCI_SCTX_1_RH_PORT_SET(rhport)
1940 );
1941 cp[2] = htole32(
1942 XHCI_SCTX_2_IRQ_TARGET_SET(0)
1943 );
1944 cp[3] = htole32(0);
1945
1946 /* set up input EP0 context */
1947 cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_EP_CONTROL));
1948 cp[0] = htole32(0);
1949 cp[1] = htole32(
1950 XHCI_EPCTX_1_MAXP_SIZE_SET(mps) |
1951 XHCI_EPCTX_1_EPTYPE_SET(4) |
1952 XHCI_EPCTX_1_CERR_SET(3)
1953 );
1954 /* can't use xhci_ep_get_dci() yet? */
1955 *(uint64_t *)(&cp[2]) = htole64(
1956 xhci_ring_trbp(&xs->xs_ep[XHCI_DCI_EP_CONTROL].xe_tr, 0) |
1957 XHCI_EPCTX_2_DCS_SET(1));
1958 cp[4] = htole32(
1959 XHCI_EPCTX_4_AVG_TRB_LEN_SET(8)
1960 );
1961
1962 /* sync input contexts before they are read from memory */
1963 usb_syncmem(&xs->xs_ic_dma, 0, sc->sc_pgsz, BUS_DMASYNC_PREWRITE);
1964 hexdump("input context", xhci_slot_get_icv(sc, xs, 0),
1965 sc->sc_ctxsz * 3);
1966
1967 xhci_set_dcba(sc, DMAADDR(&xs->xs_dc_dma, 0), slot);
1968
1969 err = xhci_address_device(sc, xhci_slot_get_icp(sc, xs, 0), slot,
1970 false);
1971
1972 usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD);
1973 hexdump("output context", xhci_slot_get_dcv(sc, xs, 0),
1974 sc->sc_ctxsz * 2);
1975
1976 return err;
1977 }
1978
1979 /* ----- */
1980
1981 static void
1982 xhci_noop(usbd_pipe_handle pipe)
1983 {
1984 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1985 }
1986
1987 static int xhci_roothub_ctrl(struct usbd_bus *bus, usb_device_request_t *req,
1988 void *buf, int buflen)
1989 {
1990 struct xhci_softc * const sc = bus->ub_hcpriv;
1991 usb_port_status_t ps;
1992 int l, totlen = 0;
1993 uint16_t len, value, index;
1994 int port, i;
1995 uint32_t v;
1996
1997 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1998
1999 if (sc->sc_dying)
2000 return -1;
2001
2002 len = UGETW(req->wLength);
2003 value = UGETW(req->wValue);
2004 index = UGETW(req->wIndex);
2005
2006 DPRINTFN(12, "rhreq: %04x %04x %04x %04x",
2007 req->bmRequestType | (req->bRequest << 8), value, index, len);
2008
2009 #define C(x,y) ((x) | ((y) << 8))
2010 switch (C(req->bRequest, req->bmRequestType)) {
2011 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2012 DPRINTFN(8, "getdesc: wValue=0x%04x", value, 0, 0, 0);
2013 if (len == 0)
2014 break;
2015 switch (value) {
2016 #define sd ((usb_string_descriptor_t *)buf)
2017 case C(2, UDESC_STRING):
2018 /* Product */
2019 totlen = usb_makestrdesc(sd, len, "xHCI Root Hub");
2020 break;
2021 #undef sd
2022 default:
2023 /* default from usbroothub */
2024 return buflen;
2025 }
2026 break;
2027
2028 /* Hub requests */
2029 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2030 break;
2031 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2032 DPRINTFN(4, "UR_CLEAR_PORT_FEATURE port=%d feature=%d",
2033 index, value, 0, 0);
2034 if (index < 1 || index > sc->sc_hs_port_count) {
2035 return -1;
2036 }
2037 port = XHCI_PORTSC(sc->sc_hs_port_start - 1 + index);
2038 v = xhci_op_read_4(sc, port);
2039 DPRINTFN(4, "portsc=0x%08x", v, 0, 0, 0);
2040 v &= ~XHCI_PS_CLEAR;
2041 switch (value) {
2042 case UHF_PORT_ENABLE:
2043 xhci_op_write_4(sc, port, v &~ XHCI_PS_PED);
2044 break;
2045 case UHF_PORT_SUSPEND:
2046 return -1;
2047 case UHF_PORT_POWER:
2048 break;
2049 case UHF_PORT_TEST:
2050 case UHF_PORT_INDICATOR:
2051 return -1;
2052 case UHF_C_PORT_CONNECTION:
2053 xhci_op_write_4(sc, port, v | XHCI_PS_CSC);
2054 break;
2055 case UHF_C_PORT_ENABLE:
2056 case UHF_C_PORT_SUSPEND:
2057 case UHF_C_PORT_OVER_CURRENT:
2058 return -1;
2059 case UHF_C_PORT_RESET:
2060 xhci_op_write_4(sc, port, v | XHCI_PS_PRC);
2061 break;
2062 default:
2063 return -1;
2064 }
2065 break;
2066 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2067 if (len == 0)
2068 break;
2069 if ((value & 0xff) != 0) {
2070 return -1;
2071 }
2072 usb_hub_descriptor_t hubd;
2073
2074 totlen = min(buflen, sizeof(hubd));
2075 memcpy(&hubd, buf, totlen);
2076 hubd.bNbrPorts = sc->sc_hs_port_count;
2077 USETW(hubd.wHubCharacteristics, UHD_PWR_NO_SWITCH);
2078 hubd.bPwrOn2PwrGood = 200;
2079 for (i = 0, l = sc->sc_maxports; l > 0; i++, l -= 8)
2080 hubd.DeviceRemovable[i++] = 0; /* XXX can't find out? */
2081 hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
2082 totlen = min(totlen, hubd.bDescLength);
2083 memcpy(buf, &hubd, totlen);
2084 break;
2085 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2086 if (len != 4) {
2087 return -1;
2088 }
2089 memset(buf, 0, len); /* ? XXX */
2090 totlen = len;
2091 break;
2092 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2093 DPRINTFN(8, "get port status i=%d", index, 0, 0, 0);
2094 if (index < 1 || index > sc->sc_maxports) {
2095 return -1;
2096 }
2097 if (len != 4) {
2098 return -1;
2099 }
2100 v = xhci_op_read_4(sc, XHCI_PORTSC(sc->sc_hs_port_start - 1 +
2101 index));
2102 DPRINTFN(4, "READ_CLASS_OTHER GET_STATUS PORTSC %d (%d) %08x",
2103 index, sc->sc_hs_port_start - 1 + index, v, 0);
2104 switch (XHCI_PS_SPEED_GET(v)) {
2105 case 1:
2106 i = UPS_FULL_SPEED;
2107 break;
2108 case 2:
2109 i = UPS_LOW_SPEED;
2110 break;
2111 case 3:
2112 i = UPS_HIGH_SPEED;
2113 break;
2114 default:
2115 i = 0;
2116 break;
2117 }
2118 if (v & XHCI_PS_CCS) i |= UPS_CURRENT_CONNECT_STATUS;
2119 if (v & XHCI_PS_PED) i |= UPS_PORT_ENABLED;
2120 if (v & XHCI_PS_OCA) i |= UPS_OVERCURRENT_INDICATOR;
2121 //if (v & XHCI_PS_SUSP) i |= UPS_SUSPEND;
2122 if (v & XHCI_PS_PR) i |= UPS_RESET;
2123 if (v & XHCI_PS_PP) i |= UPS_PORT_POWER;
2124 USETW(ps.wPortStatus, i);
2125 i = 0;
2126 if (v & XHCI_PS_CSC) i |= UPS_C_CONNECT_STATUS;
2127 if (v & XHCI_PS_PEC) i |= UPS_C_PORT_ENABLED;
2128 if (v & XHCI_PS_OCC) i |= UPS_C_OVERCURRENT_INDICATOR;
2129 if (v & XHCI_PS_PRC) i |= UPS_C_PORT_RESET;
2130 USETW(ps.wPortChange, i);
2131 totlen = min(len, sizeof(ps));
2132 memcpy(buf, &ps, totlen);
2133 break;
2134 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2135 return -1;
2136 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2137 break;
2138 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2139 if (index < 1 || index > sc->sc_hs_port_count) {
2140 return -1;
2141 }
2142 port = XHCI_PORTSC(sc->sc_hs_port_start - 1 + index);
2143 v = xhci_op_read_4(sc, port);
2144 DPRINTFN(4, "portsc=0x%08x", v, 0, 0, 0);
2145 v &= ~XHCI_PS_CLEAR;
2146 switch (value) {
2147 case UHF_PORT_ENABLE:
2148 xhci_op_write_4(sc, port, v | XHCI_PS_PED);
2149 break;
2150 case UHF_PORT_SUSPEND:
2151 /* XXX suspend */
2152 break;
2153 case UHF_PORT_RESET:
2154 v &= ~ (XHCI_PS_PED | XHCI_PS_PR);
2155 xhci_op_write_4(sc, port, v | XHCI_PS_PR);
2156 /* Wait for reset to complete. */
2157 usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
2158 if (sc->sc_dying) {
2159 return -1;
2160 }
2161 v = xhci_op_read_4(sc, port);
2162 if (v & XHCI_PS_PR) {
2163 xhci_op_write_4(sc, port, v & ~XHCI_PS_PR);
2164 usb_delay_ms(&sc->sc_bus, 10);
2165 /* XXX */
2166 }
2167 break;
2168 case UHF_PORT_POWER:
2169 /* XXX power control */
2170 break;
2171 /* XXX more */
2172 case UHF_C_PORT_RESET:
2173 xhci_op_write_4(sc, port, v | XHCI_PS_PRC);
2174 break;
2175 default:
2176 return -1;
2177 }
2178 break;
2179 case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
2180 case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
2181 case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
2182 case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
2183 break;
2184 default:
2185 /* default from usbroothub */
2186 return buflen;
2187 }
2188
2189 return totlen;
2190 }
2191
2192 /* root hub intrerrupt */
2193
2194 static usbd_status
2195 xhci_root_intr_transfer(usbd_xfer_handle xfer)
2196 {
2197 struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
2198 usbd_status err;
2199
2200 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2201
2202 /* Insert last in queue. */
2203 mutex_enter(&sc->sc_lock);
2204 err = usb_insert_transfer(xfer);
2205 mutex_exit(&sc->sc_lock);
2206 if (err)
2207 return err;
2208
2209 /* Pipe isn't running, start first */
2210 return xhci_root_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
2211 }
2212
2213 static usbd_status
2214 xhci_root_intr_start(usbd_xfer_handle xfer)
2215 {
2216 struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
2217
2218 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2219
2220 if (sc->sc_dying)
2221 return USBD_IOERROR;
2222
2223 mutex_enter(&sc->sc_lock);
2224 sc->sc_intrxfer = xfer;
2225 mutex_exit(&sc->sc_lock);
2226
2227 return USBD_IN_PROGRESS;
2228 }
2229
2230 static void
2231 xhci_root_intr_abort(usbd_xfer_handle xfer)
2232 {
2233 struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
2234
2235 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2236
2237 KASSERT(mutex_owned(&sc->sc_lock));
2238 KASSERT(xfer->ux_pipe->up_intrxfer == xfer);
2239
2240 DPRINTFN(1, "remove", 0, 0, 0, 0);
2241
2242 sc->sc_intrxfer = NULL;
2243
2244 xfer->ux_status = USBD_CANCELLED;
2245 usb_transfer_complete(xfer);
2246 }
2247
2248 static void
2249 xhci_root_intr_close(usbd_pipe_handle pipe)
2250 {
2251 struct xhci_softc * const sc = pipe->up_dev->ud_bus->ub_hcpriv;
2252
2253 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2254
2255 KASSERT(mutex_owned(&sc->sc_lock));
2256
2257 sc->sc_intrxfer = NULL;
2258 }
2259
2260 static void
2261 xhci_root_intr_done(usbd_xfer_handle xfer)
2262 {
2263 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2264
2265 xfer->ux_hcpriv = NULL;
2266 }
2267
2268 /* -------------- */
2269 /* device control */
2270
2271 static usbd_status
2272 xhci_device_ctrl_transfer(usbd_xfer_handle xfer)
2273 {
2274 struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
2275 usbd_status err;
2276
2277 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2278
2279 /* Insert last in queue. */
2280 mutex_enter(&sc->sc_lock);
2281 err = usb_insert_transfer(xfer);
2282 mutex_exit(&sc->sc_lock);
2283 if (err)
2284 return err;
2285
2286 /* Pipe isn't running, start first */
2287 return xhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
2288 }
2289
2290 static usbd_status
2291 xhci_device_ctrl_start(usbd_xfer_handle xfer)
2292 {
2293 struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
2294 struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv;
2295 const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc);
2296 struct xhci_ring * const tr = &xs->xs_ep[dci].xe_tr;
2297 struct xhci_xfer * const xx = (void *)xfer;
2298 usb_device_request_t * const req = &xfer->ux_request;
2299 const bool isread = UT_GET_DIR(req->bmRequestType) == UT_READ;
2300 const uint32_t len = UGETW(req->wLength);
2301 usb_dma_t * const dma = &xfer->ux_dmabuf;
2302 uint64_t parameter;
2303 uint32_t status;
2304 uint32_t control;
2305 u_int i;
2306
2307 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2308 DPRINTFN(12, "req: %04x %04x %04x %04x",
2309 req->bmRequestType | (req->bRequest << 8), UGETW(req->wValue),
2310 UGETW(req->wIndex), UGETW(req->wLength));
2311
2312 /* XXX */
2313 if (tr->is_halted) {
2314 xhci_reset_endpoint(xfer->ux_pipe);
2315 tr->is_halted = false;
2316 xhci_set_dequeue(xfer->ux_pipe);
2317 }
2318
2319 /* we rely on the bottom bits for extra info */
2320 KASSERT(((uintptr_t)xfer & 0x3) == 0x0);
2321
2322 KASSERT((xfer->ux_rqflags & URQ_REQUEST) != 0);
2323
2324 i = 0;
2325
2326 /* setup phase */
2327 memcpy(¶meter, req, sizeof(*req));
2328 parameter = le64toh(parameter);
2329 status = XHCI_TRB_2_IRQ_SET(0) | XHCI_TRB_2_BYTES_SET(sizeof(*req));
2330 control = ((len == 0) ? XHCI_TRB_3_TRT_NONE :
2331 (isread ? XHCI_TRB_3_TRT_IN : XHCI_TRB_3_TRT_OUT)) |
2332 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SETUP_STAGE) |
2333 XHCI_TRB_3_IDT_BIT;
2334 xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
2335
2336 if (len == 0)
2337 goto no_data;
2338
2339 /* data phase */
2340 parameter = DMAADDR(dma, 0);
2341 KASSERT(len <= 0x10000);
2342 status = XHCI_TRB_2_IRQ_SET(0) |
2343 XHCI_TRB_2_TDSZ_SET(1) |
2344 XHCI_TRB_2_BYTES_SET(len);
2345 control = (isread ? XHCI_TRB_3_DIR_IN : 0) |
2346 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_DATA_STAGE) |
2347 XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_ENT_BIT;
2348 xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
2349
2350 parameter = (uintptr_t)xfer | 0x3;
2351 status = XHCI_TRB_2_IRQ_SET(0);
2352 control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_EVENT_DATA) |
2353 XHCI_TRB_3_IOC_BIT;
2354 xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
2355
2356 no_data:
2357 parameter = 0;
2358 status = XHCI_TRB_2_IRQ_SET(0);
2359 /* the status stage has inverted direction */
2360 control = ((isread && (len > 0)) ? 0 : XHCI_TRB_3_DIR_IN) |
2361 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STATUS_STAGE) |
2362 XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_ENT_BIT;
2363 xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
2364
2365 parameter = (uintptr_t)xfer | 0x0;
2366 status = XHCI_TRB_2_IRQ_SET(0);
2367 control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_EVENT_DATA) |
2368 XHCI_TRB_3_IOC_BIT;
2369 xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
2370
2371 mutex_enter(&tr->xr_lock);
2372 xhci_ring_put(sc, tr, xfer, xx->xx_trb, i);
2373 mutex_exit(&tr->xr_lock);
2374
2375 xhci_db_write_4(sc, XHCI_DOORBELL(xs->xs_idx), dci);
2376
2377 if (xfer->ux_timeout && !sc->sc_bus.ub_usepolling) {
2378 callout_reset(&xfer->ux_callout, mstohz(xfer->ux_timeout),
2379 xhci_timeout, xfer);
2380 }
2381
2382 if (sc->sc_bus.ub_usepolling) {
2383 DPRINTFN(1, "polling", 0, 0, 0, 0);
2384 //xhci_waitintr(sc, xfer);
2385 }
2386
2387 return USBD_IN_PROGRESS;
2388 }
2389
2390 static void
2391 xhci_device_ctrl_done(usbd_xfer_handle xfer)
2392 {
2393 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2394
2395 callout_stop(&xfer->ux_callout); /* XXX wrong place */
2396
2397 }
2398
2399 static void
2400 xhci_device_ctrl_abort(usbd_xfer_handle xfer)
2401 {
2402 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2403 }
2404
2405 static void
2406 xhci_device_ctrl_close(usbd_pipe_handle pipe)
2407 {
2408 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2409 }
2410
2411 /* ----------------- */
2412 /* device isochronus */
2413
2414 /* ----------- */
2415 /* device bulk */
2416
2417 static usbd_status
2418 xhci_device_bulk_transfer(usbd_xfer_handle xfer)
2419 {
2420 struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
2421 usbd_status err;
2422
2423 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2424
2425 /* Insert last in queue. */
2426 mutex_enter(&sc->sc_lock);
2427 err = usb_insert_transfer(xfer);
2428 mutex_exit(&sc->sc_lock);
2429 if (err)
2430 return err;
2431
2432 /*
2433 * Pipe isn't running (otherwise err would be USBD_INPROG),
2434 * so start it first.
2435 */
2436 return xhci_device_bulk_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
2437 }
2438
2439 static usbd_status
2440 xhci_device_bulk_start(usbd_xfer_handle xfer)
2441 {
2442 struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
2443 struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv;
2444 const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc);
2445 struct xhci_ring * const tr = &xs->xs_ep[dci].xe_tr;
2446 struct xhci_xfer * const xx = (void *)xfer;
2447 const uint32_t len = xfer->ux_length;
2448 usb_dma_t * const dma = &xfer->ux_dmabuf;
2449 uint64_t parameter;
2450 uint32_t status;
2451 uint32_t control;
2452 u_int i = 0;
2453
2454 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2455
2456 DPRINTFN(15, "%p slot %u dci %u", xfer, xs->xs_idx, dci, 0);
2457
2458 if (sc->sc_dying)
2459 return USBD_IOERROR;
2460
2461 KASSERT((xfer->ux_rqflags & URQ_REQUEST) == 0);
2462
2463 parameter = DMAADDR(dma, 0);
2464 /*
2465 * XXX: (dsl) The physical buffer must not cross a 64k boundary.
2466 * If the user supplied buffer crosses such a boundary then 2
2467 * (or more) TRB should be used.
2468 * If multiple TRB are used the td_size field must be set correctly.
2469 * For v1.0 devices (like ivy bridge) this is the number of usb data
2470 * blocks needed to complete the transfer.
2471 * Setting it to 1 in the last TRB causes an extra zero-length
2472 * data block be sent.
2473 * The earlier documentation differs, I don't know how it behaves.
2474 */
2475 KASSERT(len <= 0x10000);
2476 status = XHCI_TRB_2_IRQ_SET(0) |
2477 XHCI_TRB_2_TDSZ_SET(1) |
2478 XHCI_TRB_2_BYTES_SET(len);
2479 control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NORMAL) |
2480 XHCI_TRB_3_ISP_BIT | XHCI_TRB_3_IOC_BIT;
2481 xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
2482
2483 mutex_enter(&tr->xr_lock);
2484 xhci_ring_put(sc, tr, xfer, xx->xx_trb, i);
2485 mutex_exit(&tr->xr_lock);
2486
2487 xhci_db_write_4(sc, XHCI_DOORBELL(xs->xs_idx), dci);
2488
2489 if (sc->sc_bus.ub_usepolling) {
2490 DPRINTFN(1, "polling", 0, 0, 0, 0);
2491 //xhci_waitintr(sc, xfer);
2492 }
2493
2494 return USBD_IN_PROGRESS;
2495 }
2496
2497 static void
2498 xhci_device_bulk_done(usbd_xfer_handle xfer)
2499 {
2500 #ifdef USB_DEBUG
2501 struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv;
2502 const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc);
2503 #endif
2504 const u_int endpt = xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress;
2505 const bool isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2506
2507 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2508
2509 DPRINTFN(15, "%p slot %u dci %u", xfer, xs->xs_idx, dci, 0);
2510
2511 callout_stop(&xfer->ux_callout); /* XXX wrong place */
2512
2513 usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
2514 isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
2515
2516
2517 }
2518
2519 static void
2520 xhci_device_bulk_abort(usbd_xfer_handle xfer)
2521 {
2522 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2523 }
2524
2525 static void
2526 xhci_device_bulk_close(usbd_pipe_handle pipe)
2527 {
2528 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2529 }
2530
2531 /* --------------- */
2532 /* device intrrupt */
2533
2534 static usbd_status
2535 xhci_device_intr_transfer(usbd_xfer_handle xfer)
2536 {
2537 struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
2538 usbd_status err;
2539
2540 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2541
2542 /* Insert last in queue. */
2543 mutex_enter(&sc->sc_lock);
2544 err = usb_insert_transfer(xfer);
2545 mutex_exit(&sc->sc_lock);
2546 if (err)
2547 return err;
2548
2549 /*
2550 * Pipe isn't running (otherwise err would be USBD_INPROG),
2551 * so start it first.
2552 */
2553 return xhci_device_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
2554 }
2555
2556 static usbd_status
2557 xhci_device_intr_start(usbd_xfer_handle xfer)
2558 {
2559 struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
2560 struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv;
2561 const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc);
2562 struct xhci_ring * const tr = &xs->xs_ep[dci].xe_tr;
2563 struct xhci_xfer * const xx = (void *)xfer;
2564 const uint32_t len = xfer->ux_length;
2565 usb_dma_t * const dma = &xfer->ux_dmabuf;
2566 uint64_t parameter;
2567 uint32_t status;
2568 uint32_t control;
2569 u_int i = 0;
2570
2571 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2572
2573 DPRINTFN(15, "%p slot %u dci %u", xfer, xs->xs_idx, dci, 0);
2574
2575 if (sc->sc_dying)
2576 return USBD_IOERROR;
2577
2578 KASSERT((xfer->ux_rqflags & URQ_REQUEST) == 0);
2579
2580 parameter = DMAADDR(dma, 0);
2581 KASSERT(len <= 0x10000);
2582 status = XHCI_TRB_2_IRQ_SET(0) |
2583 XHCI_TRB_2_TDSZ_SET(1) |
2584 XHCI_TRB_2_BYTES_SET(len);
2585 control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NORMAL) |
2586 XHCI_TRB_3_ISP_BIT | XHCI_TRB_3_IOC_BIT;
2587 xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
2588
2589 mutex_enter(&tr->xr_lock);
2590 xhci_ring_put(sc, tr, xfer, xx->xx_trb, i);
2591 mutex_exit(&tr->xr_lock);
2592
2593 xhci_db_write_4(sc, XHCI_DOORBELL(xs->xs_idx), dci);
2594
2595 if (sc->sc_bus.ub_usepolling) {
2596 DPRINTFN(1, "polling", 0, 0, 0, 0);
2597 //xhci_waitintr(sc, xfer);
2598 }
2599
2600 return USBD_IN_PROGRESS;
2601 }
2602
2603 static void
2604 xhci_device_intr_done(usbd_xfer_handle xfer)
2605 {
2606 struct xhci_softc * const sc __diagused =
2607 xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
2608 #ifdef USB_DEBUG
2609 struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv;
2610 const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc);
2611 #endif
2612 const u_int endpt = xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress;
2613 const bool isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2614
2615 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2616
2617 DPRINTFN(15, "%p slot %u dci %u", xfer, xs->xs_idx, dci, 0);
2618
2619 KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
2620
2621 usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
2622 isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
2623
2624 #if 0
2625 device_printf(sc->sc_dev, "");
2626 for (size_t i = 0; i < xfer->ux_length; i++) {
2627 printf(" %02x", ((uint8_t const *)xfer->ux_buffer)[i]);
2628 }
2629 printf("\n");
2630 #endif
2631
2632 if (xfer->ux_pipe->up_repeat) {
2633 xfer->ux_status = xhci_device_intr_start(xfer);
2634 } else {
2635 callout_stop(&xfer->ux_callout); /* XXX */
2636 }
2637
2638 }
2639
2640 static void
2641 xhci_device_intr_abort(usbd_xfer_handle xfer)
2642 {
2643 struct xhci_softc * const sc __diagused =
2644 xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
2645
2646 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2647
2648 KASSERT(mutex_owned(&sc->sc_lock));
2649 DPRINTFN(15, "%p", xfer, 0, 0, 0);
2650 KASSERT(xfer->ux_pipe->up_intrxfer == xfer);
2651 xfer->ux_status = USBD_CANCELLED;
2652 usb_transfer_complete(xfer);
2653 }
2654
2655 static void
2656 xhci_device_intr_close(usbd_pipe_handle pipe)
2657 {
2658 //struct xhci_softc * const sc = pipe->up_dev->ud_bus->ub_hcpriv;
2659
2660 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2661 DPRINTFN(15, "%p", pipe, 0, 0, 0);
2662
2663 xhci_unconfigure_endpoint(pipe);
2664 }
2665
2666 /* ------------ */
2667
2668 static void
2669 xhci_timeout(void *addr)
2670 {
2671 struct xhci_xfer * const xx = addr;
2672 usbd_xfer_handle const xfer = &xx->xx_xfer;
2673 struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
2674
2675 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2676
2677 if (sc->sc_dying) {
2678 return;
2679 }
2680
2681 usb_init_task(&xx->xx_abort_task, xhci_timeout_task, addr,
2682 USB_TASKQ_MPSAFE);
2683 usb_add_task(xx->xx_xfer.ux_pipe->up_dev, &xx->xx_abort_task,
2684 USB_TASKQ_HC);
2685 }
2686
2687 static void
2688 xhci_timeout_task(void *addr)
2689 {
2690 usbd_xfer_handle const xfer = addr;
2691 struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
2692
2693 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2694
2695 mutex_enter(&sc->sc_lock);
2696 #if 0
2697 xhci_abort_xfer(xfer, USBD_TIMEOUT);
2698 #else
2699 xfer->ux_status = USBD_TIMEOUT;
2700 usb_transfer_complete(xfer);
2701 #endif
2702 mutex_exit(&sc->sc_lock);
2703 }
2704