xhci.c revision 1.28.2.16 1 /* $NetBSD: xhci.c,v 1.28.2.16 2015/03/22 08:09:44 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.16 2015/03/22 08:09:44 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(struct usbd_pipe *);
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 struct usbd_xfer *xhci_allocx(struct usbd_bus *);
121 static void xhci_freex(struct usbd_bus *, struct usbd_xfer *);
122 static void xhci_get_lock(struct usbd_bus *, kmutex_t **);
123 static usbd_status xhci_new_device(device_t, struct usbd_bus *, 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(struct usbd_pipe *);
129 static usbd_status xhci_unconfigure_endpoint(struct usbd_pipe *);
130 static usbd_status xhci_reset_endpoint(struct usbd_pipe *);
131 //static usbd_status xhci_stop_endpoint(struct usbd_pipe *);
132
133 static usbd_status xhci_set_dequeue(struct usbd_pipe *);
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(struct usbd_pipe *);
150
151 static usbd_status xhci_root_intr_transfer(struct usbd_xfer *);
152 static usbd_status xhci_root_intr_start(struct usbd_xfer *);
153 static void xhci_root_intr_abort(struct usbd_xfer *);
154 static void xhci_root_intr_close(struct usbd_pipe *);
155 static void xhci_root_intr_done(struct usbd_xfer *);
156
157 static usbd_status xhci_device_ctrl_transfer(struct usbd_xfer *);
158 static usbd_status xhci_device_ctrl_start(struct usbd_xfer *);
159 static void xhci_device_ctrl_abort(struct usbd_xfer *);
160 static void xhci_device_ctrl_close(struct usbd_pipe *);
161 static void xhci_device_ctrl_done(struct usbd_xfer *);
162
163 static usbd_status xhci_device_intr_transfer(struct usbd_xfer *);
164 static usbd_status xhci_device_intr_start(struct usbd_xfer *);
165 static void xhci_device_intr_abort(struct usbd_xfer *);
166 static void xhci_device_intr_close(struct usbd_pipe *);
167 static void xhci_device_intr_done(struct usbd_xfer *);
168
169 static usbd_status xhci_device_bulk_transfer(struct usbd_xfer *);
170 static usbd_status xhci_device_bulk_start(struct usbd_xfer *);
171 static void xhci_device_bulk_abort(struct usbd_xfer *);
172 static void xhci_device_bulk_close(struct usbd_pipe *);
173 static void xhci_device_bulk_done(struct usbd_xfer *);
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(struct usbd_pipe *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 uint8_t eptype = xhci_ep_get_type(pipe->up_endpoint->ue_edesc);
969 cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(dci));
970 if (xfertype == UE_INTERRUPT) {
971 cp[0] = htole32(
972 XHCI_EPCTX_0_IVAL_SET(3) /* XXX */
973 );
974 cp[1] = htole32(
975 XHCI_EPCTX_1_CERR_SET(3) |
976 XHCI_EPCTX_1_EPTYPE_SET(eptype) |
977 XHCI_EPCTX_1_MAXB_SET(0) |
978 XHCI_EPCTX_1_MAXP_SIZE_SET(8) /* XXX */
979 );
980 cp[4] = htole32(
981 XHCI_EPCTX_4_AVG_TRB_LEN_SET(8)
982 );
983 } else {
984 cp[0] = htole32(0);
985 cp[1] = htole32(
986 XHCI_EPCTX_1_CERR_SET(3) |
987 XHCI_EPCTX_1_EPTYPE_SET(eptype) |
988 XHCI_EPCTX_1_MAXB_SET(0) |
989 XHCI_EPCTX_1_MAXP_SIZE_SET(512) /* XXX */
990 );
991 }
992 *(uint64_t *)(&cp[2]) = htole64(
993 xhci_ring_trbp(&xs->xs_ep[dci].xe_tr, 0) |
994 XHCI_EPCTX_2_DCS_SET(1));
995
996 /* sync input contexts before they are read from memory */
997 usb_syncmem(&xs->xs_ic_dma, 0, sc->sc_pgsz, BUS_DMASYNC_PREWRITE);
998 hexdump("input control context", xhci_slot_get_icv(sc, xs, 0),
999 sc->sc_ctxsz * 1);
1000 hexdump("input endpoint context", xhci_slot_get_icv(sc, xs,
1001 xhci_dci_to_ici(dci)), sc->sc_ctxsz * 1);
1002
1003 trb.trb_0 = xhci_slot_get_icp(sc, xs, 0);
1004 trb.trb_2 = 0;
1005 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
1006 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_CONFIGURE_EP);
1007
1008 err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
1009
1010 usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD);
1011 hexdump("output context", xhci_slot_get_dcv(sc, xs, dci),
1012 sc->sc_ctxsz * 1);
1013
1014 return err;
1015 }
1016
1017 static usbd_status
1018 xhci_unconfigure_endpoint(struct usbd_pipe *pipe)
1019 {
1020 #ifdef USB_DEBUG
1021 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
1022 #endif
1023
1024 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1025 DPRINTFN(4, "slot %u", xs->xs_idx, 0, 0, 0);
1026
1027 return USBD_NORMAL_COMPLETION;
1028 }
1029
1030 static usbd_status
1031 xhci_reset_endpoint(struct usbd_pipe *pipe)
1032 {
1033 struct xhci_softc * const sc = pipe->up_dev->ud_bus->ub_hcpriv;
1034 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
1035 const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
1036 struct xhci_trb trb;
1037 usbd_status err;
1038
1039 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1040 DPRINTFN(4, "dci %u", dci, 0, 0, 0);
1041
1042 trb.trb_0 = 0;
1043 trb.trb_2 = 0;
1044 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
1045 XHCI_TRB_3_EP_SET(dci) |
1046 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_RESET_EP);
1047
1048 err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
1049
1050 return err;
1051 }
1052
1053 #if 0
1054 static usbd_status
1055 xhci_stop_endpoint(struct usbd_pipe *pipe)
1056 {
1057 struct xhci_softc * const sc = pipe->up_dev->ud_bus->ub_hcpriv;
1058 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
1059 struct xhci_trb trb;
1060 usbd_status err;
1061 const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
1062
1063 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1064 DPRINTFN(4, "dci %u", dci, 0, 0, 0);
1065
1066 trb.trb_0 = 0;
1067 trb.trb_2 = 0;
1068 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
1069 XHCI_TRB_3_EP_SET(dci) |
1070 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STOP_EP);
1071
1072 err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
1073
1074 return err;
1075 }
1076 #endif
1077
1078 static usbd_status
1079 xhci_set_dequeue(struct usbd_pipe *pipe)
1080 {
1081 struct xhci_softc * const sc = pipe->up_dev->ud_bus->ub_hcpriv;
1082 struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
1083 const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
1084 struct xhci_ring * const xr = &xs->xs_ep[dci].xe_tr;
1085 struct xhci_trb trb;
1086 usbd_status err;
1087
1088 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1089 DPRINTFN(4, "slot %u dci %u", xs->xs_idx, dci, 0, 0);
1090
1091 memset(xr->xr_trb, 0, xr->xr_ntrb * XHCI_TRB_SIZE);
1092 usb_syncmem(&xr->xr_dma, 0, xr->xr_ntrb * XHCI_TRB_SIZE,
1093 BUS_DMASYNC_PREWRITE);
1094
1095 xr->xr_ep = 0;
1096 xr->xr_cs = 1;
1097
1098 trb.trb_0 = xhci_ring_trbp(xr, 0) | 1; /* XXX */
1099 trb.trb_2 = 0;
1100 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
1101 XHCI_TRB_3_EP_SET(dci) |
1102 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SET_TR_DEQUEUE);
1103
1104 err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
1105
1106 return err;
1107 }
1108
1109 static usbd_status
1110 xhci_open(struct usbd_pipe *pipe)
1111 {
1112 struct usbd_device *const dev = pipe->up_dev;
1113 struct xhci_softc * const sc = dev->ud_bus->ub_hcpriv;
1114 usb_endpoint_descriptor_t * const ed = pipe->up_endpoint->ue_edesc;
1115 const uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
1116
1117 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1118 DPRINTFN(1, "addr %d depth %d port %d speed %d",
1119 dev->ud_addr, dev->ud_depth, dev->ud_powersrc->up_portno, dev->ud_speed);
1120
1121 if (sc->sc_dying)
1122 return USBD_IOERROR;
1123
1124 /* Root Hub */
1125 if (dev->ud_depth == 0 && dev->ud_powersrc->up_portno == 0 &&
1126 dev->ud_speed != USB_SPEED_SUPER) {
1127 switch (ed->bEndpointAddress) {
1128 case USB_CONTROL_ENDPOINT:
1129 pipe->up_methods = &roothub_ctrl_methods;
1130 break;
1131 case UE_DIR_IN | USBROOTHUB_INTR_ENDPT:
1132 pipe->up_methods = &xhci_root_intr_methods;
1133 break;
1134 default:
1135 pipe->up_methods = NULL;
1136 DPRINTFN(0, "bad bEndpointAddress 0x%02x",
1137 ed->bEndpointAddress, 0, 0, 0);
1138 return USBD_INVAL;
1139 }
1140 return USBD_NORMAL_COMPLETION;
1141 }
1142
1143 switch (xfertype) {
1144 case UE_CONTROL:
1145 pipe->up_methods = &xhci_device_ctrl_methods;
1146 break;
1147 case UE_ISOCHRONOUS:
1148 pipe->up_methods = &xhci_device_isoc_methods;
1149 return USBD_INVAL;
1150 break;
1151 case UE_BULK:
1152 pipe->up_methods = &xhci_device_bulk_methods;
1153 break;
1154 case UE_INTERRUPT:
1155 pipe->up_methods = &xhci_device_intr_methods;
1156 break;
1157 default:
1158 return USBD_IOERROR;
1159 break;
1160 }
1161
1162 if (ed->bEndpointAddress != USB_CONTROL_ENDPOINT)
1163 xhci_configure_endpoint(pipe);
1164
1165 return USBD_NORMAL_COMPLETION;
1166 }
1167
1168 static void
1169 xhci_rhpsc(struct xhci_softc * const sc, u_int port)
1170 {
1171 struct usbd_xfer *const xfer = sc->sc_intrxfer;
1172 uint8_t *p;
1173
1174 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1175 DPRINTFN(4, "port %u status change", port, 0, 0, 0);
1176
1177 if (xfer == NULL)
1178 return;
1179
1180 if (!(port >= sc->sc_hs_port_start &&
1181 port < sc->sc_hs_port_start + sc->sc_hs_port_count))
1182 return;
1183
1184 port -= sc->sc_hs_port_start;
1185 port += 1;
1186 DPRINTFN(4, "hs port %u status change", port, 0, 0, 0);
1187
1188 p = xfer->ux_buf;
1189 memset(p, 0, xfer->ux_length);
1190 p[port/NBBY] |= 1 << (port%NBBY);
1191 xfer->ux_actlen = xfer->ux_length;
1192 xfer->ux_status = USBD_NORMAL_COMPLETION;
1193 usb_transfer_complete(xfer);
1194 }
1195
1196 static void
1197 xhci_handle_event(struct xhci_softc * const sc,
1198 const struct xhci_trb * const trb)
1199 {
1200 uint64_t trb_0;
1201 uint32_t trb_2, trb_3;
1202
1203 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1204
1205 trb_0 = le64toh(trb->trb_0);
1206 trb_2 = le32toh(trb->trb_2);
1207 trb_3 = le32toh(trb->trb_3);
1208
1209 DPRINTFN(14, "event: %p 0x%016"PRIx64" 0x%08"PRIx32" 0x%08"PRIx32,
1210 trb, trb_0, trb_2, trb_3);
1211
1212 switch (XHCI_TRB_3_TYPE_GET(trb_3)){
1213 case XHCI_TRB_EVENT_TRANSFER: {
1214 u_int slot, dci;
1215 struct xhci_slot *xs;
1216 struct xhci_ring *xr;
1217 struct xhci_xfer *xx;
1218 struct usbd_xfer *xfer;
1219 usbd_status err;
1220
1221 slot = XHCI_TRB_3_SLOT_GET(trb_3);
1222 dci = XHCI_TRB_3_EP_GET(trb_3);
1223
1224 xs = &sc->sc_slots[slot];
1225 xr = &xs->xs_ep[dci].xe_tr;
1226
1227 if ((trb_3 & XHCI_TRB_3_ED_BIT) == 0) {
1228 xx = xr->xr_cookies[(trb_0 - xhci_ring_trbp(xr, 0))/
1229 sizeof(struct xhci_trb)];
1230 } else {
1231 xx = (void *)(uintptr_t)(trb_0 & ~0x3);
1232 }
1233 xfer = &xx->xx_xfer;
1234 DPRINTFN(14, "xfer %p", xfer, 0, 0, 0);
1235
1236 if ((trb_3 & XHCI_TRB_3_ED_BIT) != 0) {
1237 DPRINTFN(14, "transfer event data: "
1238 "0x%016"PRIx64" 0x%08"PRIx32" %02x",
1239 trb_0, XHCI_TRB_2_REM_GET(trb_2),
1240 XHCI_TRB_2_ERROR_GET(trb_2), 0);
1241 if ((trb_0 & 0x3) == 0x3) {
1242 xfer->ux_actlen = XHCI_TRB_2_REM_GET(trb_2);
1243 }
1244 }
1245
1246 if (XHCI_TRB_2_ERROR_GET(trb_2) ==
1247 XHCI_TRB_ERROR_SUCCESS) {
1248 xfer->ux_actlen = xfer->ux_length - XHCI_TRB_2_REM_GET(trb_2);
1249 err = USBD_NORMAL_COMPLETION;
1250 } else if (XHCI_TRB_2_ERROR_GET(trb_2) ==
1251 XHCI_TRB_ERROR_SHORT_PKT) {
1252 xfer->ux_actlen = xfer->ux_length - XHCI_TRB_2_REM_GET(trb_2);
1253 err = USBD_NORMAL_COMPLETION;
1254 } else if (XHCI_TRB_2_ERROR_GET(trb_2) ==
1255 XHCI_TRB_ERROR_STALL) {
1256 err = USBD_STALLED;
1257 xr->is_halted = true;
1258 DPRINTFN(1, "ev: xfer done: err %u slot %u dci %u",
1259 XHCI_TRB_2_ERROR_GET(trb_2), slot, dci, 0);
1260 } else {
1261 err = USBD_IOERROR;
1262 }
1263 xfer->ux_status = err;
1264
1265 //mutex_enter(&sc->sc_lock); /* XXX ??? */
1266 if ((trb_3 & XHCI_TRB_3_ED_BIT) != 0) {
1267 if ((trb_0 & 0x3) == 0x0) {
1268 usb_transfer_complete(xfer);
1269 }
1270 } else {
1271 usb_transfer_complete(xfer);
1272 }
1273 //mutex_exit(&sc->sc_lock); /* XXX ??? */
1274
1275 }
1276 break;
1277 case XHCI_TRB_EVENT_CMD_COMPLETE:
1278 if (trb_0 == sc->sc_command_addr) {
1279 sc->sc_result_trb.trb_0 = trb_0;
1280 sc->sc_result_trb.trb_2 = trb_2;
1281 sc->sc_result_trb.trb_3 = trb_3;
1282 if (XHCI_TRB_2_ERROR_GET(trb_2) !=
1283 XHCI_TRB_ERROR_SUCCESS) {
1284 DPRINTFN(1, "command completion "
1285 "failure: 0x%016"PRIx64" 0x%08"PRIx32" "
1286 "0x%08"PRIx32, trb_0, trb_2, trb_3, 0);
1287 }
1288 cv_signal(&sc->sc_command_cv);
1289 } else {
1290 DPRINTFN(1, "event: %p 0x%016"PRIx64" "
1291 "0x%08"PRIx32" 0x%08"PRIx32, trb, trb_0,
1292 trb_2, trb_3);
1293 }
1294 break;
1295 case XHCI_TRB_EVENT_PORT_STS_CHANGE:
1296 xhci_rhpsc(sc, (uint32_t)((trb_0 >> 24) & 0xff));
1297 break;
1298 default:
1299 break;
1300 }
1301 }
1302
1303 static void
1304 xhci_softintr(void *v)
1305 {
1306 struct usbd_bus *const bus = v;
1307 struct xhci_softc * const sc = bus->ub_hcpriv;
1308 struct xhci_ring * const er = &sc->sc_er;
1309 struct xhci_trb *trb;
1310 int i, j, k;
1311
1312 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1313
1314 i = er->xr_ep;
1315 j = er->xr_cs;
1316
1317 DPRINTFN(16, "xr_ep %d xr_cs %d", i, j, 0, 0);
1318
1319 while (1) {
1320 usb_syncmem(&er->xr_dma, XHCI_TRB_SIZE * i, XHCI_TRB_SIZE,
1321 BUS_DMASYNC_POSTREAD);
1322 trb = &er->xr_trb[i];
1323 k = (le32toh(trb->trb_3) & XHCI_TRB_3_CYCLE_BIT) ? 1 : 0;
1324
1325 if (j != k)
1326 break;
1327
1328 xhci_handle_event(sc, trb);
1329
1330 i++;
1331 if (i == XHCI_EVENT_RING_TRBS) {
1332 i = 0;
1333 j ^= 1;
1334 }
1335 }
1336
1337 er->xr_ep = i;
1338 er->xr_cs = j;
1339
1340 xhci_rt_write_8(sc, XHCI_ERDP(0), xhci_ring_trbp(er, er->xr_ep) |
1341 XHCI_ERDP_LO_BUSY);
1342
1343 DPRINTFN(16, "ends", 0, 0, 0, 0);
1344
1345 return;
1346 }
1347
1348 static void
1349 xhci_poll(struct usbd_bus *bus)
1350 {
1351 struct xhci_softc * const sc = bus->ub_hcpriv;
1352
1353 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1354
1355 mutex_spin_enter(&sc->sc_intr_lock);
1356 xhci_intr1(sc);
1357 mutex_spin_exit(&sc->sc_intr_lock);
1358
1359 return;
1360 }
1361
1362 static struct usbd_xfer *
1363 xhci_allocx(struct usbd_bus *bus)
1364 {
1365 struct xhci_softc * const sc = bus->ub_hcpriv;
1366 struct usbd_xfer *xfer;
1367
1368 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1369
1370 xfer = pool_cache_get(sc->sc_xferpool, PR_NOWAIT);
1371 if (xfer != NULL) {
1372 memset(xfer, 0, sizeof(struct xhci_xfer));
1373 #ifdef DIAGNOSTIC
1374 xfer->ux_state = XFER_BUSY;
1375 #endif
1376 }
1377
1378 return xfer;
1379 }
1380
1381 static void
1382 xhci_freex(struct usbd_bus *bus, struct usbd_xfer *xfer)
1383 {
1384 struct xhci_softc * const sc = bus->ub_hcpriv;
1385
1386 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1387
1388 #ifdef DIAGNOSTIC
1389 if (xfer->ux_state != XFER_BUSY) {
1390 DPRINTFN(0, "xfer=%p not busy, 0x%08x",
1391 xfer, xfer->ux_state, 0, 0);
1392 }
1393 xfer->ux_state = XFER_FREE;
1394 #endif
1395 pool_cache_put(sc->sc_xferpool, xfer);
1396 }
1397
1398 static void
1399 xhci_get_lock(struct usbd_bus *bus, kmutex_t **lock)
1400 {
1401 struct xhci_softc * const sc = bus->ub_hcpriv;
1402
1403 *lock = &sc->sc_lock;
1404 }
1405
1406 extern uint32_t usb_cookie_no;
1407
1408 static usbd_status
1409 xhci_new_device(device_t parent, struct usbd_bus *bus, int depth,
1410 int speed, int port, struct usbd_port *up)
1411 {
1412 struct xhci_softc * const sc = bus->ub_hcpriv;
1413 struct usbd_device *dev;
1414 usbd_status err;
1415 usb_device_descriptor_t *dd;
1416 struct usbd_device *hub;
1417 struct usbd_device *adev;
1418 int rhport = 0;
1419 struct xhci_slot *xs;
1420 uint32_t *cp;
1421 uint8_t slot;
1422 uint8_t addr;
1423
1424 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1425 DPRINTFN(4, "port=%d depth=%d speed=%d upport %d",
1426 port, depth, speed, up->up_portno);
1427
1428 dev = kmem_zalloc(sizeof(*dev), KM_SLEEP);
1429 if (dev == NULL)
1430 return USBD_NOMEM;
1431
1432 dev->ud_bus = bus;
1433
1434 /* Set up default endpoint handle. */
1435 dev->ud_ep0.ue_edesc = &dev->ud_ep0desc;
1436
1437 /* Set up default endpoint descriptor. */
1438 dev->ud_ep0desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
1439 dev->ud_ep0desc.bDescriptorType = UDESC_ENDPOINT;
1440 dev->ud_ep0desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
1441 dev->ud_ep0desc.bmAttributes = UE_CONTROL;
1442 /* XXX */
1443 if (speed == USB_SPEED_LOW)
1444 USETW(dev->ud_ep0desc.wMaxPacketSize, USB_MAX_IPACKET);
1445 else
1446 USETW(dev->ud_ep0desc.wMaxPacketSize, 64);
1447 dev->ud_ep0desc.bInterval = 0;
1448
1449 /* doesn't matter, just don't let it uninitialized */
1450 dev->ud_ep0.ue_toggle = 0;
1451
1452 DPRINTFN(4, "up %p portno %d", up, up->up_portno, 0, 0);
1453
1454 dev->ud_quirks = &usbd_no_quirk;
1455 dev->ud_addr = 0;
1456 dev->ud_ddesc.bMaxPacketSize = 0;
1457 dev->ud_depth = depth;
1458 dev->ud_powersrc = up;
1459 dev->ud_myhub = up->up_parent;
1460
1461 up->up_dev = dev;
1462
1463 /* Locate root hub port */
1464 for (adev = dev, hub = dev;
1465 hub != NULL;
1466 adev = hub, hub = hub->ud_myhub) {
1467 DPRINTFN(4, "hub %p", hub, 0, 0, 0);
1468 }
1469 DPRINTFN(4, "hub %p", hub, 0, 0, 0);
1470
1471 if (hub != NULL) {
1472 for (int p = 0; p < hub->ud_hub->uh_hubdesc.bNbrPorts; p++) {
1473 if (hub->ud_hub->uh_ports[p].up_dev == adev) {
1474 rhport = p;
1475 }
1476 }
1477 } else {
1478 rhport = port;
1479 }
1480 if (speed == USB_SPEED_SUPER) {
1481 rhport += sc->sc_ss_port_start - 1;
1482 } else {
1483 rhport += sc->sc_hs_port_start - 1;
1484 }
1485 DPRINTFN(4, "rhport %d", rhport, 0, 0, 0);
1486
1487 dev->ud_speed = speed;
1488 dev->ud_langid = USBD_NOLANG;
1489 dev->ud_cookie.cookie = ++usb_cookie_no;
1490
1491 /* Establish the default pipe. */
1492 err = usbd_setup_pipe(dev, 0, &dev->ud_ep0, USBD_DEFAULT_INTERVAL,
1493 &dev->ud_pipe0);
1494 if (err) {
1495 usbd_remove_device(dev, up);
1496 return err;
1497 }
1498
1499 dd = &dev->ud_ddesc;
1500
1501 if ((depth == 0) && (port == 0)) {
1502 KASSERT(bus->ub_devices[dev->ud_addr] == NULL);
1503 bus->ub_devices[dev->ud_addr] = dev;
1504 err = usbd_get_initial_ddesc(dev, dd);
1505 if (err)
1506 return err;
1507 err = usbd_reload_device_desc(dev);
1508 if (err)
1509 return err;
1510 } else {
1511 err = xhci_enable_slot(sc, &slot);
1512 if (err)
1513 return err;
1514 err = xhci_init_slot(sc, slot, depth, speed, port, rhport);
1515 if (err)
1516 return err;
1517 xs = &sc->sc_slots[slot];
1518 dev->ud_hcpriv = xs;
1519 cp = xhci_slot_get_dcv(sc, xs, XHCI_DCI_SLOT);
1520 //hexdump("slot context", cp, sc->sc_ctxsz);
1521 addr = XHCI_SCTX_3_DEV_ADDR_GET(cp[3]);
1522 DPRINTFN(4, "device address %u", addr, 0, 0, 0);
1523 /* XXX ensure we know when the hardware does something
1524 we can't yet cope with */
1525 KASSERT(addr >= 1 && addr <= 127);
1526 dev->ud_addr = addr;
1527 /* XXX dev->ud_addr not necessarily unique on bus */
1528 KASSERT(bus->ub_devices[dev->ud_addr] == NULL);
1529 bus->ub_devices[dev->ud_addr] = dev;
1530
1531 err = usbd_get_initial_ddesc(dev, dd);
1532 if (err)
1533 return err;
1534 /* 4.8.2.1 */
1535 if (speed == USB_SPEED_SUPER)
1536 USETW(dev->ud_ep0desc.wMaxPacketSize,
1537 (1 << dd->bMaxPacketSize));
1538 else
1539 USETW(dev->ud_ep0desc.wMaxPacketSize,
1540 dd->bMaxPacketSize);
1541 DPRINTFN(4, "bMaxPacketSize %u", dd->bMaxPacketSize, 0, 0, 0);
1542 xhci_update_ep0_mps(sc, xs,
1543 UGETW(dev->ud_ep0desc.wMaxPacketSize));
1544 err = usbd_reload_device_desc(dev);
1545 if (err)
1546 return err;
1547
1548 usbd_kill_pipe(dev->ud_pipe0);
1549 err = usbd_setup_pipe(dev, 0, &dev->ud_ep0,
1550 USBD_DEFAULT_INTERVAL, &dev->ud_pipe0);
1551 }
1552
1553 DPRINTFN(1, "adding unit addr=%d, rev=%02x,",
1554 dev->ud_addr, UGETW(dd->bcdUSB), 0, 0);
1555 DPRINTFN(1, " class=%d, subclass=%d, protocol=%d,",
1556 dd->bDeviceClass, dd->bDeviceSubClass,
1557 dd->bDeviceProtocol, 0);
1558 DPRINTFN(1, " mps=%d, len=%d, noconf=%d, speed=%d",
1559 dd->bMaxPacketSize, dd->bLength, dd->bNumConfigurations,
1560 dev->ud_speed);
1561
1562 usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
1563
1564 if ((depth == 0) && (port == 0)) {
1565 usbd_attach_roothub(parent, dev);
1566 DPRINTFN(1, "root_hub %p", bus->ub_roothub, 0, 0, 0);
1567 return USBD_NORMAL_COMPLETION;
1568 }
1569
1570
1571 err = usbd_probe_and_attach(parent, dev, port, dev->ud_addr);
1572 if (err) {
1573 usbd_remove_device(dev, up);
1574 return err;
1575 }
1576
1577 return USBD_NORMAL_COMPLETION;
1578 }
1579
1580 static usbd_status
1581 xhci_ring_init(struct xhci_softc * const sc, struct xhci_ring * const xr,
1582 size_t ntrb, size_t align)
1583 {
1584 usbd_status err;
1585 size_t size = ntrb * XHCI_TRB_SIZE;
1586
1587 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1588
1589 err = usb_allocmem(&sc->sc_bus, size, align, &xr->xr_dma);
1590 if (err)
1591 return err;
1592 mutex_init(&xr->xr_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
1593 xr->xr_cookies = kmem_zalloc(sizeof(*xr->xr_cookies) * ntrb, KM_SLEEP);
1594 xr->xr_trb = xhci_ring_trbv(xr, 0);
1595 xr->xr_ntrb = ntrb;
1596 xr->xr_ep = 0;
1597 xr->xr_cs = 1;
1598 memset(xr->xr_trb, 0, size);
1599 usb_syncmem(&xr->xr_dma, 0, size, BUS_DMASYNC_PREWRITE);
1600 xr->is_halted = false;
1601
1602 return USBD_NORMAL_COMPLETION;
1603 }
1604
1605 static void
1606 xhci_ring_free(struct xhci_softc * const sc, struct xhci_ring * const xr)
1607 {
1608 usb_freemem(&sc->sc_bus, &xr->xr_dma);
1609 mutex_destroy(&xr->xr_lock);
1610 kmem_free(xr->xr_cookies, sizeof(*xr->xr_cookies) * xr->xr_ntrb);
1611 }
1612
1613 static void
1614 xhci_ring_put(struct xhci_softc * const sc, struct xhci_ring * const xr,
1615 void *cookie, struct xhci_trb * const trbs, size_t ntrbs)
1616 {
1617 size_t i;
1618 u_int ri;
1619 u_int cs;
1620 uint64_t parameter;
1621 uint32_t status;
1622 uint32_t control;
1623
1624 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1625
1626 for (i = 0; i < ntrbs; i++) {
1627 DPRINTFN(12, "xr %p trbs %p num %zu", xr, trbs, i, 0);
1628 DPRINTFN(12, " %016"PRIx64" %08"PRIx32" %08"PRIx32,
1629 trbs[i].trb_0, trbs[i].trb_2, trbs[i].trb_3, 0);
1630 KASSERT(XHCI_TRB_3_TYPE_GET(trbs[i].trb_3) !=
1631 XHCI_TRB_TYPE_LINK);
1632 }
1633
1634 DPRINTFN(12, "%p xr_ep 0x%x xr_cs %u", xr, xr->xr_ep, xr->xr_cs, 0);
1635
1636 ri = xr->xr_ep;
1637 cs = xr->xr_cs;
1638
1639 /*
1640 * Although the xhci hardware can do scatter/gather dma from
1641 * arbitrary sized buffers, there is a non-obvious restriction
1642 * that a LINK trb is only allowed at the end of a burst of
1643 * transfers - which might be 16kB.
1644 * Arbitrary aligned LINK trb definitely fail on Ivy bridge.
1645 * The simple solution is not to allow a LINK trb in the middle
1646 * of anything - as here.
1647 * XXX: (dsl) There are xhci controllers out there (eg some made by
1648 * ASMedia) that seem to lock up if they process a LINK trb but
1649 * cannot process the linked-to trb yet.
1650 * The code should write the 'cycle' bit on the link trb AFTER
1651 * adding the other trb.
1652 */
1653 if (ri + ntrbs >= (xr->xr_ntrb - 1)) {
1654 parameter = xhci_ring_trbp(xr, 0);
1655 status = 0;
1656 control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK) |
1657 XHCI_TRB_3_TC_BIT | (cs ? XHCI_TRB_3_CYCLE_BIT : 0);
1658 xhci_trb_put(&xr->xr_trb[ri], htole64(parameter),
1659 htole32(status), htole32(control));
1660 usb_syncmem(&xr->xr_dma, XHCI_TRB_SIZE * ri, XHCI_TRB_SIZE * 1,
1661 BUS_DMASYNC_PREWRITE);
1662 xr->xr_cookies[ri] = NULL;
1663 xr->xr_ep = 0;
1664 xr->xr_cs ^= 1;
1665 ri = xr->xr_ep;
1666 cs = xr->xr_cs;
1667 }
1668
1669 ri++;
1670
1671 /* Write any subsequent TRB first */
1672 for (i = 1; i < ntrbs; i++) {
1673 parameter = trbs[i].trb_0;
1674 status = trbs[i].trb_2;
1675 control = trbs[i].trb_3;
1676
1677 if (cs) {
1678 control |= XHCI_TRB_3_CYCLE_BIT;
1679 } else {
1680 control &= ~XHCI_TRB_3_CYCLE_BIT;
1681 }
1682
1683 xhci_trb_put(&xr->xr_trb[ri], htole64(parameter),
1684 htole32(status), htole32(control));
1685 usb_syncmem(&xr->xr_dma, XHCI_TRB_SIZE * ri, XHCI_TRB_SIZE * 1,
1686 BUS_DMASYNC_PREWRITE);
1687 xr->xr_cookies[ri] = cookie;
1688 ri++;
1689 }
1690
1691 /* Write the first TRB last */
1692 i = 0;
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 xr->xr_ep = ri;
1710 xr->xr_cs = cs;
1711
1712 DPRINTFN(12, "%p xr_ep 0x%x xr_cs %u", xr, xr->xr_ep, xr->xr_cs, 0);
1713 }
1714
1715 static usbd_status
1716 xhci_do_command(struct xhci_softc * const sc, struct xhci_trb * const trb,
1717 int timeout)
1718 {
1719 struct xhci_ring * const cr = &sc->sc_cr;
1720 usbd_status err;
1721
1722 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1723 DPRINTFN(12, "input: 0x%016"PRIx64" 0x%08"PRIx32" 0x%08"PRIx32,
1724 trb->trb_0, trb->trb_2, trb->trb_3, 0);
1725
1726 mutex_enter(&sc->sc_lock);
1727
1728 KASSERT(sc->sc_command_addr == 0);
1729 sc->sc_command_addr = xhci_ring_trbp(cr, cr->xr_ep);
1730
1731 mutex_enter(&cr->xr_lock);
1732 xhci_ring_put(sc, cr, NULL, trb, 1);
1733 mutex_exit(&cr->xr_lock);
1734
1735 xhci_db_write_4(sc, XHCI_DOORBELL(0), 0);
1736
1737 if (cv_timedwait(&sc->sc_command_cv, &sc->sc_lock,
1738 MAX(1, mstohz(timeout))) == EWOULDBLOCK) {
1739 err = USBD_TIMEOUT;
1740 goto timedout;
1741 }
1742
1743 trb->trb_0 = sc->sc_result_trb.trb_0;
1744 trb->trb_2 = sc->sc_result_trb.trb_2;
1745 trb->trb_3 = sc->sc_result_trb.trb_3;
1746
1747 DPRINTFN(12, "output: 0x%016"PRIx64" 0x%08"PRIx32" 0x%08"PRIx32"",
1748 trb->trb_0, trb->trb_2, trb->trb_3, 0);
1749
1750 switch (XHCI_TRB_2_ERROR_GET(trb->trb_2)) {
1751 case XHCI_TRB_ERROR_SUCCESS:
1752 err = USBD_NORMAL_COMPLETION;
1753 break;
1754 default:
1755 case 192 ... 223:
1756 err = USBD_IOERROR;
1757 break;
1758 case 224 ... 255:
1759 err = USBD_NORMAL_COMPLETION;
1760 break;
1761 }
1762
1763 timedout:
1764 sc->sc_command_addr = 0;
1765 mutex_exit(&sc->sc_lock);
1766 return err;
1767 }
1768
1769 static usbd_status
1770 xhci_enable_slot(struct xhci_softc * const sc, uint8_t * const slotp)
1771 {
1772 struct xhci_trb trb;
1773 usbd_status err;
1774
1775 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1776
1777 trb.trb_0 = 0;
1778 trb.trb_2 = 0;
1779 trb.trb_3 = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ENABLE_SLOT);
1780
1781 err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
1782 if (err != USBD_NORMAL_COMPLETION) {
1783 return err;
1784 }
1785
1786 *slotp = XHCI_TRB_3_SLOT_GET(trb.trb_3);
1787
1788 return err;
1789 }
1790
1791 static usbd_status
1792 xhci_address_device(struct xhci_softc * const sc,
1793 uint64_t icp, uint8_t slot_id, bool bsr)
1794 {
1795 struct xhci_trb trb;
1796 usbd_status err;
1797
1798 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1799
1800 trb.trb_0 = icp;
1801 trb.trb_2 = 0;
1802 trb.trb_3 = XHCI_TRB_3_SLOT_SET(slot_id) |
1803 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ADDRESS_DEVICE) |
1804 (bsr ? XHCI_TRB_3_BSR_BIT : 0);
1805
1806 err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
1807 return err;
1808 }
1809
1810 static usbd_status
1811 xhci_update_ep0_mps(struct xhci_softc * const sc,
1812 struct xhci_slot * const xs, u_int mps)
1813 {
1814 struct xhci_trb trb;
1815 usbd_status err;
1816 uint32_t * cp;
1817
1818 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1819 DPRINTFN(4, "slot %u mps %u", xs->xs_idx, mps, 0, 0);
1820
1821 cp = xhci_slot_get_icv(sc, xs, XHCI_ICI_INPUT_CONTROL);
1822 cp[0] = htole32(0);
1823 cp[1] = htole32(XHCI_INCTX_1_ADD_MASK(XHCI_DCI_EP_CONTROL));
1824
1825 cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_EP_CONTROL));
1826 cp[1] = htole32(XHCI_EPCTX_1_MAXP_SIZE_SET(mps));
1827
1828 /* sync input contexts before they are read from memory */
1829 usb_syncmem(&xs->xs_ic_dma, 0, sc->sc_pgsz, BUS_DMASYNC_PREWRITE);
1830 hexdump("input context", xhci_slot_get_icv(sc, xs, 0),
1831 sc->sc_ctxsz * 4);
1832
1833 trb.trb_0 = xhci_slot_get_icp(sc, xs, 0);
1834 trb.trb_2 = 0;
1835 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) |
1836 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_EVALUATE_CTX);
1837
1838 err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
1839 KASSERT(err == USBD_NORMAL_COMPLETION); /* XXX */
1840 return err;
1841 }
1842
1843 static void
1844 xhci_set_dcba(struct xhci_softc * const sc, uint64_t dcba, int si)
1845 {
1846 uint64_t * const dcbaa = KERNADDR(&sc->sc_dcbaa_dma, 0);
1847
1848 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1849 DPRINTFN(4, "dcbaa %p dc %016"PRIx64" slot %d",
1850 &dcbaa[si], dcba, si, 0);
1851
1852 dcbaa[si] = htole64(dcba);
1853 usb_syncmem(&sc->sc_dcbaa_dma, si * sizeof(uint64_t), sizeof(uint64_t),
1854 BUS_DMASYNC_PREWRITE);
1855 }
1856
1857 static usbd_status
1858 xhci_init_slot(struct xhci_softc * const sc, uint32_t slot, int depth,
1859 int speed, int port, int rhport)
1860 {
1861 struct xhci_slot *xs;
1862 usbd_status err;
1863 u_int dci;
1864 uint32_t *cp;
1865 uint32_t mps;
1866 uint32_t xspeed;
1867
1868 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1869 DPRINTFN(4, "slot %u depth %d speed %d",
1870 slot, depth, speed, 0);
1871 DPRINTFN(4, " port %d rhport %d",
1872 port, rhport, 0, 0);
1873
1874 switch (speed) {
1875 case USB_SPEED_LOW:
1876 xspeed = 2;
1877 mps = USB_MAX_IPACKET;
1878 break;
1879 case USB_SPEED_FULL:
1880 xspeed = 1;
1881 mps = 64;
1882 break;
1883 case USB_SPEED_HIGH:
1884 xspeed = 3;
1885 mps = USB_2_MAX_CTRL_PACKET;
1886 break;
1887 case USB_SPEED_SUPER:
1888 xspeed = 4;
1889 mps = USB_3_MAX_CTRL_PACKET;
1890 break;
1891 default:
1892 DPRINTFN(0, "impossible speed: %x", speed, 0, 0, 0);
1893 return USBD_INVAL;
1894 }
1895
1896 xs = &sc->sc_slots[slot];
1897 xs->xs_idx = slot;
1898
1899 /* allocate contexts */
1900 err = usb_allocmem(&sc->sc_bus, sc->sc_pgsz, sc->sc_pgsz,
1901 &xs->xs_dc_dma);
1902 if (err)
1903 return err;
1904 memset(KERNADDR(&xs->xs_dc_dma, 0), 0, sc->sc_pgsz);
1905
1906 err = usb_allocmem(&sc->sc_bus, sc->sc_pgsz, sc->sc_pgsz,
1907 &xs->xs_ic_dma);
1908 if (err)
1909 return err;
1910 memset(KERNADDR(&xs->xs_ic_dma, 0), 0, sc->sc_pgsz);
1911
1912 for (dci = 0; dci < 32; dci++) {
1913 //CTASSERT(sizeof(xs->xs_ep[dci]) == sizeof(struct xhci_endpoint));
1914 memset(&xs->xs_ep[dci], 0, sizeof(xs->xs_ep[dci]));
1915 if (dci == XHCI_DCI_SLOT)
1916 continue;
1917 err = xhci_ring_init(sc, &xs->xs_ep[dci].xe_tr,
1918 XHCI_TRANSFER_RING_TRBS, XHCI_TRB_ALIGN);
1919 if (err) {
1920 DPRINTFN(0, "ring init failure", 0, 0, 0, 0);
1921 return err;
1922 }
1923 }
1924
1925 /* set up initial input control context */
1926 cp = xhci_slot_get_icv(sc, xs, XHCI_ICI_INPUT_CONTROL);
1927 cp[0] = htole32(0);
1928 cp[1] = htole32(XHCI_INCTX_1_ADD_MASK(XHCI_DCI_EP_CONTROL)|
1929 XHCI_INCTX_1_ADD_MASK(XHCI_DCI_SLOT));
1930
1931 /* set up input slot context */
1932 cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_SLOT));
1933 cp[0] = htole32(
1934 XHCI_SCTX_0_CTX_NUM_SET(1) |
1935 XHCI_SCTX_0_SPEED_SET(xspeed)
1936 );
1937 cp[1] = htole32(
1938 XHCI_SCTX_1_RH_PORT_SET(rhport)
1939 );
1940 cp[2] = htole32(
1941 XHCI_SCTX_2_IRQ_TARGET_SET(0)
1942 );
1943 cp[3] = htole32(0);
1944
1945 /* set up input EP0 context */
1946 cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_EP_CONTROL));
1947 cp[0] = htole32(0);
1948 cp[1] = htole32(
1949 XHCI_EPCTX_1_MAXP_SIZE_SET(mps) |
1950 XHCI_EPCTX_1_EPTYPE_SET(4) |
1951 XHCI_EPCTX_1_CERR_SET(3)
1952 );
1953 /* can't use xhci_ep_get_dci() yet? */
1954 *(uint64_t *)(&cp[2]) = htole64(
1955 xhci_ring_trbp(&xs->xs_ep[XHCI_DCI_EP_CONTROL].xe_tr, 0) |
1956 XHCI_EPCTX_2_DCS_SET(1));
1957 cp[4] = htole32(
1958 XHCI_EPCTX_4_AVG_TRB_LEN_SET(8)
1959 );
1960
1961 /* sync input contexts before they are read from memory */
1962 usb_syncmem(&xs->xs_ic_dma, 0, sc->sc_pgsz, BUS_DMASYNC_PREWRITE);
1963 hexdump("input context", xhci_slot_get_icv(sc, xs, 0),
1964 sc->sc_ctxsz * 3);
1965
1966 xhci_set_dcba(sc, DMAADDR(&xs->xs_dc_dma, 0), slot);
1967
1968 err = xhci_address_device(sc, xhci_slot_get_icp(sc, xs, 0), slot,
1969 false);
1970
1971 usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD);
1972 hexdump("output context", xhci_slot_get_dcv(sc, xs, 0),
1973 sc->sc_ctxsz * 2);
1974
1975 return err;
1976 }
1977
1978 /* ----- */
1979
1980 static void
1981 xhci_noop(struct usbd_pipe *pipe)
1982 {
1983 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1984 }
1985
1986 static int xhci_roothub_ctrl(struct usbd_bus *bus, usb_device_request_t *req,
1987 void *buf, int buflen)
1988 {
1989 struct xhci_softc * const sc = bus->ub_hcpriv;
1990 usb_port_status_t ps;
1991 int l, totlen = 0;
1992 uint16_t len, value, index;
1993 int port, i;
1994 uint32_t v;
1995
1996 XHCIHIST_FUNC(); XHCIHIST_CALLED();
1997
1998 if (sc->sc_dying)
1999 return -1;
2000
2001 len = UGETW(req->wLength);
2002 value = UGETW(req->wValue);
2003 index = UGETW(req->wIndex);
2004
2005 DPRINTFN(12, "rhreq: %04x %04x %04x %04x",
2006 req->bmRequestType | (req->bRequest << 8), value, index, len);
2007
2008 #define C(x,y) ((x) | ((y) << 8))
2009 switch (C(req->bRequest, req->bmRequestType)) {
2010 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2011 DPRINTFN(8, "getdesc: wValue=0x%04x", value, 0, 0, 0);
2012 if (len == 0)
2013 break;
2014 switch (value) {
2015 #define sd ((usb_string_descriptor_t *)buf)
2016 case C(2, UDESC_STRING):
2017 /* Product */
2018 totlen = usb_makestrdesc(sd, len, "xHCI Root Hub");
2019 break;
2020 #undef sd
2021 default:
2022 /* default from usbroothub */
2023 return buflen;
2024 }
2025 break;
2026
2027 /* Hub requests */
2028 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2029 break;
2030 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2031 DPRINTFN(4, "UR_CLEAR_PORT_FEATURE port=%d feature=%d",
2032 index, value, 0, 0);
2033 if (index < 1 || index > sc->sc_hs_port_count) {
2034 return -1;
2035 }
2036 port = XHCI_PORTSC(sc->sc_hs_port_start - 1 + index);
2037 v = xhci_op_read_4(sc, port);
2038 DPRINTFN(4, "portsc=0x%08x", v, 0, 0, 0);
2039 v &= ~XHCI_PS_CLEAR;
2040 switch (value) {
2041 case UHF_PORT_ENABLE:
2042 xhci_op_write_4(sc, port, v &~ XHCI_PS_PED);
2043 break;
2044 case UHF_PORT_SUSPEND:
2045 return -1;
2046 case UHF_PORT_POWER:
2047 break;
2048 case UHF_PORT_TEST:
2049 case UHF_PORT_INDICATOR:
2050 return -1;
2051 case UHF_C_PORT_CONNECTION:
2052 xhci_op_write_4(sc, port, v | XHCI_PS_CSC);
2053 break;
2054 case UHF_C_PORT_ENABLE:
2055 case UHF_C_PORT_SUSPEND:
2056 case UHF_C_PORT_OVER_CURRENT:
2057 return -1;
2058 case UHF_C_PORT_RESET:
2059 xhci_op_write_4(sc, port, v | XHCI_PS_PRC);
2060 break;
2061 default:
2062 return -1;
2063 }
2064 break;
2065 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2066 if (len == 0)
2067 break;
2068 if ((value & 0xff) != 0) {
2069 return -1;
2070 }
2071 usb_hub_descriptor_t hubd;
2072
2073 totlen = min(buflen, sizeof(hubd));
2074 memcpy(&hubd, buf, totlen);
2075 hubd.bNbrPorts = sc->sc_hs_port_count;
2076 USETW(hubd.wHubCharacteristics, UHD_PWR_NO_SWITCH);
2077 hubd.bPwrOn2PwrGood = 200;
2078 for (i = 0, l = sc->sc_maxports; l > 0; i++, l -= 8)
2079 hubd.DeviceRemovable[i++] = 0; /* XXX can't find out? */
2080 hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
2081 totlen = min(totlen, hubd.bDescLength);
2082 memcpy(buf, &hubd, totlen);
2083 break;
2084 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2085 if (len != 4) {
2086 return -1;
2087 }
2088 memset(buf, 0, len); /* ? XXX */
2089 totlen = len;
2090 break;
2091 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2092 DPRINTFN(8, "get port status i=%d", index, 0, 0, 0);
2093 if (index < 1 || index > sc->sc_maxports) {
2094 return -1;
2095 }
2096 if (len != 4) {
2097 return -1;
2098 }
2099 v = xhci_op_read_4(sc, XHCI_PORTSC(sc->sc_hs_port_start - 1 +
2100 index));
2101 DPRINTFN(4, "READ_CLASS_OTHER GET_STATUS PORTSC %d (%d) %08x",
2102 index, sc->sc_hs_port_start - 1 + index, v, 0);
2103 switch (XHCI_PS_SPEED_GET(v)) {
2104 case 1:
2105 i = UPS_FULL_SPEED;
2106 break;
2107 case 2:
2108 i = UPS_LOW_SPEED;
2109 break;
2110 case 3:
2111 i = UPS_HIGH_SPEED;
2112 break;
2113 default:
2114 i = 0;
2115 break;
2116 }
2117 if (v & XHCI_PS_CCS) i |= UPS_CURRENT_CONNECT_STATUS;
2118 if (v & XHCI_PS_PED) i |= UPS_PORT_ENABLED;
2119 if (v & XHCI_PS_OCA) i |= UPS_OVERCURRENT_INDICATOR;
2120 //if (v & XHCI_PS_SUSP) i |= UPS_SUSPEND;
2121 if (v & XHCI_PS_PR) i |= UPS_RESET;
2122 if (v & XHCI_PS_PP) i |= UPS_PORT_POWER;
2123 USETW(ps.wPortStatus, i);
2124 i = 0;
2125 if (v & XHCI_PS_CSC) i |= UPS_C_CONNECT_STATUS;
2126 if (v & XHCI_PS_PEC) i |= UPS_C_PORT_ENABLED;
2127 if (v & XHCI_PS_OCC) i |= UPS_C_OVERCURRENT_INDICATOR;
2128 if (v & XHCI_PS_PRC) i |= UPS_C_PORT_RESET;
2129 USETW(ps.wPortChange, i);
2130 totlen = min(len, sizeof(ps));
2131 memcpy(buf, &ps, totlen);
2132 break;
2133 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2134 return -1;
2135 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2136 break;
2137 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2138 if (index < 1 || index > sc->sc_hs_port_count) {
2139 return -1;
2140 }
2141 port = XHCI_PORTSC(sc->sc_hs_port_start - 1 + index);
2142 v = xhci_op_read_4(sc, port);
2143 DPRINTFN(4, "portsc=0x%08x", v, 0, 0, 0);
2144 v &= ~XHCI_PS_CLEAR;
2145 switch (value) {
2146 case UHF_PORT_ENABLE:
2147 xhci_op_write_4(sc, port, v | XHCI_PS_PED);
2148 break;
2149 case UHF_PORT_SUSPEND:
2150 /* XXX suspend */
2151 break;
2152 case UHF_PORT_RESET:
2153 v &= ~ (XHCI_PS_PED | XHCI_PS_PR);
2154 xhci_op_write_4(sc, port, v | XHCI_PS_PR);
2155 /* Wait for reset to complete. */
2156 usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
2157 if (sc->sc_dying) {
2158 return -1;
2159 }
2160 v = xhci_op_read_4(sc, port);
2161 if (v & XHCI_PS_PR) {
2162 xhci_op_write_4(sc, port, v & ~XHCI_PS_PR);
2163 usb_delay_ms(&sc->sc_bus, 10);
2164 /* XXX */
2165 }
2166 break;
2167 case UHF_PORT_POWER:
2168 /* XXX power control */
2169 break;
2170 /* XXX more */
2171 case UHF_C_PORT_RESET:
2172 xhci_op_write_4(sc, port, v | XHCI_PS_PRC);
2173 break;
2174 default:
2175 return -1;
2176 }
2177 break;
2178 case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
2179 case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
2180 case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
2181 case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
2182 break;
2183 default:
2184 /* default from usbroothub */
2185 return buflen;
2186 }
2187
2188 return totlen;
2189 }
2190
2191 /* root hub intrerrupt */
2192
2193 static usbd_status
2194 xhci_root_intr_transfer(struct usbd_xfer *xfer)
2195 {
2196 struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
2197 usbd_status err;
2198
2199 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2200
2201 /* Insert last in queue. */
2202 mutex_enter(&sc->sc_lock);
2203 err = usb_insert_transfer(xfer);
2204 mutex_exit(&sc->sc_lock);
2205 if (err)
2206 return err;
2207
2208 /* Pipe isn't running, start first */
2209 return xhci_root_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
2210 }
2211
2212 static usbd_status
2213 xhci_root_intr_start(struct usbd_xfer *xfer)
2214 {
2215 struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
2216
2217 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2218
2219 if (sc->sc_dying)
2220 return USBD_IOERROR;
2221
2222 mutex_enter(&sc->sc_lock);
2223 sc->sc_intrxfer = xfer;
2224 mutex_exit(&sc->sc_lock);
2225
2226 return USBD_IN_PROGRESS;
2227 }
2228
2229 static void
2230 xhci_root_intr_abort(struct usbd_xfer *xfer)
2231 {
2232 struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
2233
2234 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2235
2236 KASSERT(mutex_owned(&sc->sc_lock));
2237 KASSERT(xfer->ux_pipe->up_intrxfer == xfer);
2238
2239 DPRINTFN(1, "remove", 0, 0, 0, 0);
2240
2241 sc->sc_intrxfer = NULL;
2242
2243 xfer->ux_status = USBD_CANCELLED;
2244 usb_transfer_complete(xfer);
2245 }
2246
2247 static void
2248 xhci_root_intr_close(struct usbd_pipe *pipe)
2249 {
2250 struct xhci_softc * const sc = pipe->up_dev->ud_bus->ub_hcpriv;
2251
2252 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2253
2254 KASSERT(mutex_owned(&sc->sc_lock));
2255
2256 sc->sc_intrxfer = NULL;
2257 }
2258
2259 static void
2260 xhci_root_intr_done(struct usbd_xfer *xfer)
2261 {
2262 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2263
2264 xfer->ux_hcpriv = NULL;
2265 }
2266
2267 /* -------------- */
2268 /* device control */
2269
2270 static usbd_status
2271 xhci_device_ctrl_transfer(struct usbd_xfer *xfer)
2272 {
2273 struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
2274 usbd_status err;
2275
2276 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2277
2278 /* Insert last in queue. */
2279 mutex_enter(&sc->sc_lock);
2280 err = usb_insert_transfer(xfer);
2281 mutex_exit(&sc->sc_lock);
2282 if (err)
2283 return err;
2284
2285 /* Pipe isn't running, start first */
2286 return xhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
2287 }
2288
2289 static usbd_status
2290 xhci_device_ctrl_start(struct usbd_xfer *xfer)
2291 {
2292 struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
2293 struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv;
2294 const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc);
2295 struct xhci_ring * const tr = &xs->xs_ep[dci].xe_tr;
2296 struct xhci_xfer * const xx = (void *)xfer;
2297 usb_device_request_t * const req = &xfer->ux_request;
2298 const bool isread = UT_GET_DIR(req->bmRequestType) == UT_READ;
2299 const uint32_t len = UGETW(req->wLength);
2300 usb_dma_t * const dma = &xfer->ux_dmabuf;
2301 uint64_t parameter;
2302 uint32_t status;
2303 uint32_t control;
2304 u_int i;
2305
2306 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2307 DPRINTFN(12, "req: %04x %04x %04x %04x",
2308 req->bmRequestType | (req->bRequest << 8), UGETW(req->wValue),
2309 UGETW(req->wIndex), UGETW(req->wLength));
2310
2311 /* XXX */
2312 if (tr->is_halted) {
2313 xhci_reset_endpoint(xfer->ux_pipe);
2314 tr->is_halted = false;
2315 xhci_set_dequeue(xfer->ux_pipe);
2316 }
2317
2318 /* we rely on the bottom bits for extra info */
2319 KASSERT(((uintptr_t)xfer & 0x3) == 0x0);
2320
2321 KASSERT((xfer->ux_rqflags & URQ_REQUEST) != 0);
2322
2323 i = 0;
2324
2325 /* setup phase */
2326 memcpy(¶meter, req, sizeof(*req));
2327 parameter = le64toh(parameter);
2328 status = XHCI_TRB_2_IRQ_SET(0) | XHCI_TRB_2_BYTES_SET(sizeof(*req));
2329 control = ((len == 0) ? XHCI_TRB_3_TRT_NONE :
2330 (isread ? XHCI_TRB_3_TRT_IN : XHCI_TRB_3_TRT_OUT)) |
2331 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SETUP_STAGE) |
2332 XHCI_TRB_3_IDT_BIT;
2333 xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
2334
2335 if (len == 0)
2336 goto no_data;
2337
2338 /* data phase */
2339 parameter = DMAADDR(dma, 0);
2340 KASSERT(len <= 0x10000);
2341 status = XHCI_TRB_2_IRQ_SET(0) |
2342 XHCI_TRB_2_TDSZ_SET(1) |
2343 XHCI_TRB_2_BYTES_SET(len);
2344 control = (isread ? XHCI_TRB_3_DIR_IN : 0) |
2345 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_DATA_STAGE) |
2346 XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_ENT_BIT;
2347 xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
2348
2349 parameter = (uintptr_t)xfer | 0x3;
2350 status = XHCI_TRB_2_IRQ_SET(0);
2351 control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_EVENT_DATA) |
2352 XHCI_TRB_3_IOC_BIT;
2353 xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
2354
2355 no_data:
2356 parameter = 0;
2357 status = XHCI_TRB_2_IRQ_SET(0);
2358 /* the status stage has inverted direction */
2359 control = ((isread && (len > 0)) ? 0 : XHCI_TRB_3_DIR_IN) |
2360 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STATUS_STAGE) |
2361 XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_ENT_BIT;
2362 xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
2363
2364 parameter = (uintptr_t)xfer | 0x0;
2365 status = XHCI_TRB_2_IRQ_SET(0);
2366 control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_EVENT_DATA) |
2367 XHCI_TRB_3_IOC_BIT;
2368 xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
2369
2370 mutex_enter(&tr->xr_lock);
2371 xhci_ring_put(sc, tr, xfer, xx->xx_trb, i);
2372 mutex_exit(&tr->xr_lock);
2373
2374 xhci_db_write_4(sc, XHCI_DOORBELL(xs->xs_idx), dci);
2375
2376 if (xfer->ux_timeout && !sc->sc_bus.ub_usepolling) {
2377 callout_reset(&xfer->ux_callout, mstohz(xfer->ux_timeout),
2378 xhci_timeout, xfer);
2379 }
2380
2381 if (sc->sc_bus.ub_usepolling) {
2382 DPRINTFN(1, "polling", 0, 0, 0, 0);
2383 //xhci_waitintr(sc, xfer);
2384 }
2385
2386 return USBD_IN_PROGRESS;
2387 }
2388
2389 static void
2390 xhci_device_ctrl_done(struct usbd_xfer *xfer)
2391 {
2392 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2393
2394 callout_stop(&xfer->ux_callout); /* XXX wrong place */
2395
2396 }
2397
2398 static void
2399 xhci_device_ctrl_abort(struct usbd_xfer *xfer)
2400 {
2401 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2402 }
2403
2404 static void
2405 xhci_device_ctrl_close(struct usbd_pipe *pipe)
2406 {
2407 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2408 }
2409
2410 /* ------------------ */
2411 /* device isochronous */
2412
2413 /* ----------- */
2414 /* device bulk */
2415
2416 static usbd_status
2417 xhci_device_bulk_transfer(struct usbd_xfer *xfer)
2418 {
2419 struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
2420 usbd_status err;
2421
2422 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2423
2424 /* Insert last in queue. */
2425 mutex_enter(&sc->sc_lock);
2426 err = usb_insert_transfer(xfer);
2427 mutex_exit(&sc->sc_lock);
2428 if (err)
2429 return err;
2430
2431 /*
2432 * Pipe isn't running (otherwise err would be USBD_INPROG),
2433 * so start it first.
2434 */
2435 return xhci_device_bulk_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
2436 }
2437
2438 static usbd_status
2439 xhci_device_bulk_start(struct usbd_xfer *xfer)
2440 {
2441 struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
2442 struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv;
2443 const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc);
2444 struct xhci_ring * const tr = &xs->xs_ep[dci].xe_tr;
2445 struct xhci_xfer * const xx = (void *)xfer;
2446 const uint32_t len = xfer->ux_length;
2447 usb_dma_t * const dma = &xfer->ux_dmabuf;
2448 uint64_t parameter;
2449 uint32_t status;
2450 uint32_t control;
2451 u_int i = 0;
2452
2453 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2454
2455 DPRINTFN(15, "%p slot %u dci %u", xfer, xs->xs_idx, dci, 0);
2456
2457 if (sc->sc_dying)
2458 return USBD_IOERROR;
2459
2460 KASSERT((xfer->ux_rqflags & URQ_REQUEST) == 0);
2461
2462 parameter = DMAADDR(dma, 0);
2463 /*
2464 * XXX: (dsl) The physical buffer must not cross a 64k boundary.
2465 * If the user supplied buffer crosses such a boundary then 2
2466 * (or more) TRB should be used.
2467 * If multiple TRB are used the td_size field must be set correctly.
2468 * For v1.0 devices (like ivy bridge) this is the number of usb data
2469 * blocks needed to complete the transfer.
2470 * Setting it to 1 in the last TRB causes an extra zero-length
2471 * data block be sent.
2472 * The earlier documentation differs, I don't know how it behaves.
2473 */
2474 KASSERT(len <= 0x10000);
2475 status = XHCI_TRB_2_IRQ_SET(0) |
2476 XHCI_TRB_2_TDSZ_SET(1) |
2477 XHCI_TRB_2_BYTES_SET(len);
2478 control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NORMAL) |
2479 XHCI_TRB_3_ISP_BIT | XHCI_TRB_3_IOC_BIT;
2480 xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
2481
2482 mutex_enter(&tr->xr_lock);
2483 xhci_ring_put(sc, tr, xfer, xx->xx_trb, i);
2484 mutex_exit(&tr->xr_lock);
2485
2486 xhci_db_write_4(sc, XHCI_DOORBELL(xs->xs_idx), dci);
2487
2488 if (sc->sc_bus.ub_usepolling) {
2489 DPRINTFN(1, "polling", 0, 0, 0, 0);
2490 //xhci_waitintr(sc, xfer);
2491 }
2492
2493 return USBD_IN_PROGRESS;
2494 }
2495
2496 static void
2497 xhci_device_bulk_done(struct usbd_xfer *xfer)
2498 {
2499 #ifdef USB_DEBUG
2500 struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv;
2501 const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc);
2502 #endif
2503 const u_int endpt = xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress;
2504 const bool isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2505
2506 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2507
2508 DPRINTFN(15, "%p slot %u dci %u", xfer, xs->xs_idx, dci, 0);
2509
2510 callout_stop(&xfer->ux_callout); /* XXX wrong place */
2511
2512 usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
2513 isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
2514 }
2515
2516 static void
2517 xhci_device_bulk_abort(struct usbd_xfer *xfer)
2518 {
2519 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2520 }
2521
2522 static void
2523 xhci_device_bulk_close(struct usbd_pipe *pipe)
2524 {
2525 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2526 }
2527
2528 /* ---------------- */
2529 /* device interrupt */
2530
2531 static usbd_status
2532 xhci_device_intr_transfer(struct usbd_xfer *xfer)
2533 {
2534 struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
2535 usbd_status err;
2536
2537 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2538
2539 /* Insert last in queue. */
2540 mutex_enter(&sc->sc_lock);
2541 err = usb_insert_transfer(xfer);
2542 mutex_exit(&sc->sc_lock);
2543 if (err)
2544 return err;
2545
2546 /*
2547 * Pipe isn't running (otherwise err would be USBD_INPROG),
2548 * so start it first.
2549 */
2550 return xhci_device_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
2551 }
2552
2553 static usbd_status
2554 xhci_device_intr_start(struct usbd_xfer *xfer)
2555 {
2556 struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
2557 struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv;
2558 const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc);
2559 struct xhci_ring * const tr = &xs->xs_ep[dci].xe_tr;
2560 struct xhci_xfer * const xx = (void *)xfer;
2561 const uint32_t len = xfer->ux_length;
2562 usb_dma_t * const dma = &xfer->ux_dmabuf;
2563 uint64_t parameter;
2564 uint32_t status;
2565 uint32_t control;
2566 u_int i = 0;
2567
2568 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2569
2570 DPRINTFN(15, "%p slot %u dci %u", xfer, xs->xs_idx, dci, 0);
2571
2572 if (sc->sc_dying)
2573 return USBD_IOERROR;
2574
2575 KASSERT((xfer->ux_rqflags & URQ_REQUEST) == 0);
2576
2577 parameter = DMAADDR(dma, 0);
2578 KASSERT(len <= 0x10000);
2579 status = XHCI_TRB_2_IRQ_SET(0) |
2580 XHCI_TRB_2_TDSZ_SET(1) |
2581 XHCI_TRB_2_BYTES_SET(len);
2582 control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NORMAL) |
2583 XHCI_TRB_3_ISP_BIT | XHCI_TRB_3_IOC_BIT;
2584 xhci_trb_put(&xx->xx_trb[i++], parameter, status, control);
2585
2586 mutex_enter(&tr->xr_lock);
2587 xhci_ring_put(sc, tr, xfer, xx->xx_trb, i);
2588 mutex_exit(&tr->xr_lock);
2589
2590 xhci_db_write_4(sc, XHCI_DOORBELL(xs->xs_idx), dci);
2591
2592 if (sc->sc_bus.ub_usepolling) {
2593 DPRINTFN(1, "polling", 0, 0, 0, 0);
2594 //xhci_waitintr(sc, xfer);
2595 }
2596
2597 return USBD_IN_PROGRESS;
2598 }
2599
2600 static void
2601 xhci_device_intr_done(struct usbd_xfer *xfer)
2602 {
2603 struct xhci_softc * const sc __diagused =
2604 xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
2605 #ifdef USB_DEBUG
2606 struct xhci_slot * const xs = xfer->ux_pipe->up_dev->ud_hcpriv;
2607 const u_int dci = xhci_ep_get_dci(xfer->ux_pipe->up_endpoint->ue_edesc);
2608 #endif
2609 const u_int endpt = xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress;
2610 const bool isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2611
2612 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2613
2614 DPRINTFN(15, "%p slot %u dci %u", xfer, xs->xs_idx, dci, 0);
2615
2616 KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
2617
2618 usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
2619 isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
2620
2621 #if 0
2622 device_printf(sc->sc_dev, "");
2623 for (size_t i = 0; i < xfer->ux_length; i++) {
2624 printf(" %02x", ((uint8_t const *)xfer->ux_buffer)[i]);
2625 }
2626 printf("\n");
2627 #endif
2628
2629 if (xfer->ux_pipe->up_repeat) {
2630 xfer->ux_status = xhci_device_intr_start(xfer);
2631 } else {
2632 callout_stop(&xfer->ux_callout); /* XXX */
2633 }
2634
2635 }
2636
2637 static void
2638 xhci_device_intr_abort(struct usbd_xfer *xfer)
2639 {
2640 struct xhci_softc * const sc __diagused =
2641 xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
2642
2643 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2644
2645 KASSERT(mutex_owned(&sc->sc_lock));
2646 DPRINTFN(15, "%p", xfer, 0, 0, 0);
2647 KASSERT(xfer->ux_pipe->up_intrxfer == xfer);
2648 xfer->ux_status = USBD_CANCELLED;
2649 usb_transfer_complete(xfer);
2650 }
2651
2652 static void
2653 xhci_device_intr_close(struct usbd_pipe *pipe)
2654 {
2655 //struct xhci_softc * const sc = pipe->up_dev->ud_bus->ub_hcpriv;
2656
2657 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2658 DPRINTFN(15, "%p", pipe, 0, 0, 0);
2659
2660 xhci_unconfigure_endpoint(pipe);
2661 }
2662
2663 /* ------------ */
2664
2665 static void
2666 xhci_timeout(void *addr)
2667 {
2668 struct xhci_xfer * const xx = addr;
2669 struct usbd_xfer *const xfer = &xx->xx_xfer;
2670 struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
2671
2672 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2673
2674 if (sc->sc_dying) {
2675 return;
2676 }
2677
2678 usb_init_task(&xx->xx_abort_task, xhci_timeout_task, addr,
2679 USB_TASKQ_MPSAFE);
2680 usb_add_task(xx->xx_xfer.ux_pipe->up_dev, &xx->xx_abort_task,
2681 USB_TASKQ_HC);
2682 }
2683
2684 static void
2685 xhci_timeout_task(void *addr)
2686 {
2687 struct usbd_xfer *const xfer = addr;
2688 struct xhci_softc * const sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
2689
2690 XHCIHIST_FUNC(); XHCIHIST_CALLED();
2691
2692 mutex_enter(&sc->sc_lock);
2693 #if 0
2694 xhci_abort_xfer(xfer, USBD_TIMEOUT);
2695 #else
2696 xfer->ux_status = USBD_TIMEOUT;
2697 usb_transfer_complete(xfer);
2698 #endif
2699 mutex_exit(&sc->sc_lock);
2700 }
2701