1 // Code for handling XHCI "Super speed" USB controllers.
2 //
3 // Copyright (C) 2013  Gerd Hoffmann <kraxel@redhat.com>
4 // Copyright (C) 2014  Kevin O'Connor <kevin@koconnor.net>
5 //
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
7 
8 #include "config.h" // CONFIG_*
9 #include "malloc.h" // memalign_low
10 #include "memmap.h" // PAGE_SIZE
11 #include "output.h" // dprintf
12 #include "pcidevice.h" // foreachpci
13 #include "pci_ids.h" // PCI_CLASS_SERIAL_USB_XHCI
14 #include "pci_regs.h" // PCI_BASE_ADDRESS_0
15 #include "string.h" // memcpy
16 #include "usb.h" // struct usb_s
17 #include "usb-xhci.h" // struct ehci_qh
18 #include "util.h" // timer_calc
19 #include "x86.h" // readl
20 
21 // --------------------------------------------------------------
22 // configuration
23 
24 #define XHCI_RING_ITEMS          16
25 #define XHCI_RING_SIZE           (XHCI_RING_ITEMS*sizeof(struct xhci_trb))
26 
27 /*
28  *  xhci_ring structs are allocated with XHCI_RING_SIZE alignment,
29  *  then we can get it from a trb pointer (provided by evt ring).
30  */
31 #define XHCI_RING(_trb)          \
32     ((struct xhci_ring*)((u32)(_trb) & ~(XHCI_RING_SIZE-1)))
33 
34 // --------------------------------------------------------------
35 // bit definitions
36 
37 #define XHCI_CMD_RS              (1<<0)
38 #define XHCI_CMD_HCRST           (1<<1)
39 #define XHCI_CMD_INTE            (1<<2)
40 #define XHCI_CMD_HSEE            (1<<3)
41 #define XHCI_CMD_LHCRST          (1<<7)
42 #define XHCI_CMD_CSS             (1<<8)
43 #define XHCI_CMD_CRS             (1<<9)
44 #define XHCI_CMD_EWE             (1<<10)
45 #define XHCI_CMD_EU3S            (1<<11)
46 
47 #define XHCI_STS_HCH             (1<<0)
48 #define XHCI_STS_HSE             (1<<2)
49 #define XHCI_STS_EINT            (1<<3)
50 #define XHCI_STS_PCD             (1<<4)
51 #define XHCI_STS_SSS             (1<<8)
52 #define XHCI_STS_RSS             (1<<9)
53 #define XHCI_STS_SRE             (1<<10)
54 #define XHCI_STS_CNR             (1<<11)
55 #define XHCI_STS_HCE             (1<<12)
56 
57 #define XHCI_PORTSC_CCS          (1<<0)
58 #define XHCI_PORTSC_PED          (1<<1)
59 #define XHCI_PORTSC_OCA          (1<<3)
60 #define XHCI_PORTSC_PR           (1<<4)
61 #define XHCI_PORTSC_PLS_SHIFT        5
62 #define XHCI_PORTSC_PLS_MASK     0xf
63 #define XHCI_PORTSC_PP           (1<<9)
64 #define XHCI_PORTSC_SPEED_SHIFT      10
65 #define XHCI_PORTSC_SPEED_MASK   0xf
66 #define XHCI_PORTSC_SPEED_FULL   (1<<10)
67 #define XHCI_PORTSC_SPEED_LOW    (2<<10)
68 #define XHCI_PORTSC_SPEED_HIGH   (3<<10)
69 #define XHCI_PORTSC_SPEED_SUPER  (4<<10)
70 #define XHCI_PORTSC_PIC_SHIFT        14
71 #define XHCI_PORTSC_PIC_MASK     0x3
72 #define XHCI_PORTSC_LWS          (1<<16)
73 #define XHCI_PORTSC_CSC          (1<<17)
74 #define XHCI_PORTSC_PEC          (1<<18)
75 #define XHCI_PORTSC_WRC          (1<<19)
76 #define XHCI_PORTSC_OCC          (1<<20)
77 #define XHCI_PORTSC_PRC          (1<<21)
78 #define XHCI_PORTSC_PLC          (1<<22)
79 #define XHCI_PORTSC_CEC          (1<<23)
80 #define XHCI_PORTSC_CAS          (1<<24)
81 #define XHCI_PORTSC_WCE          (1<<25)
82 #define XHCI_PORTSC_WDE          (1<<26)
83 #define XHCI_PORTSC_WOE          (1<<27)
84 #define XHCI_PORTSC_DR           (1<<30)
85 #define XHCI_PORTSC_WPR          (1<<31)
86 
87 #define TRB_C               (1<<0)
88 #define TRB_TYPE_SHIFT          10
89 #define TRB_TYPE_MASK       0x3f
90 #define TRB_TYPE(t)         (((t) >> TRB_TYPE_SHIFT) & TRB_TYPE_MASK)
91 
92 #define TRB_EV_ED           (1<<2)
93 
94 #define TRB_TR_ENT          (1<<1)
95 #define TRB_TR_ISP          (1<<2)
96 #define TRB_TR_NS           (1<<3)
97 #define TRB_TR_CH           (1<<4)
98 #define TRB_TR_IOC          (1<<5)
99 #define TRB_TR_IDT          (1<<6)
100 #define TRB_TR_TBC_SHIFT        7
101 #define TRB_TR_TBC_MASK     0x3
102 #define TRB_TR_BEI          (1<<9)
103 #define TRB_TR_TLBPC_SHIFT      16
104 #define TRB_TR_TLBPC_MASK   0xf
105 #define TRB_TR_FRAMEID_SHIFT    20
106 #define TRB_TR_FRAMEID_MASK 0x7ff
107 #define TRB_TR_SIA          (1<<31)
108 
109 #define TRB_TR_DIR          (1<<16)
110 
111 #define TRB_CR_SLOTID_SHIFT     24
112 #define TRB_CR_SLOTID_MASK  0xff
113 #define TRB_CR_EPID_SHIFT       16
114 #define TRB_CR_EPID_MASK    0x1f
115 
116 #define TRB_CR_BSR          (1<<9)
117 #define TRB_CR_DC           (1<<9)
118 
119 #define TRB_LK_TC           (1<<1)
120 
121 #define TRB_INTR_SHIFT          22
122 #define TRB_INTR_MASK       0x3ff
123 #define TRB_INTR(t)         (((t).status >> TRB_INTR_SHIFT) & TRB_INTR_MASK)
124 
125 typedef enum TRBType {
126     TRB_RESERVED = 0,
127     TR_NORMAL,
128     TR_SETUP,
129     TR_DATA,
130     TR_STATUS,
131     TR_ISOCH,
132     TR_LINK,
133     TR_EVDATA,
134     TR_NOOP,
135     CR_ENABLE_SLOT,
136     CR_DISABLE_SLOT,
137     CR_ADDRESS_DEVICE,
138     CR_CONFIGURE_ENDPOINT,
139     CR_EVALUATE_CONTEXT,
140     CR_RESET_ENDPOINT,
141     CR_STOP_ENDPOINT,
142     CR_SET_TR_DEQUEUE,
143     CR_RESET_DEVICE,
144     CR_FORCE_EVENT,
145     CR_NEGOTIATE_BW,
146     CR_SET_LATENCY_TOLERANCE,
147     CR_GET_PORT_BANDWIDTH,
148     CR_FORCE_HEADER,
149     CR_NOOP,
150     ER_TRANSFER = 32,
151     ER_COMMAND_COMPLETE,
152     ER_PORT_STATUS_CHANGE,
153     ER_BANDWIDTH_REQUEST,
154     ER_DOORBELL,
155     ER_HOST_CONTROLLER,
156     ER_DEVICE_NOTIFICATION,
157     ER_MFINDEX_WRAP,
158 } TRBType;
159 
160 typedef enum TRBCCode {
161     CC_INVALID = 0,
162     CC_SUCCESS,
163     CC_DATA_BUFFER_ERROR,
164     CC_BABBLE_DETECTED,
165     CC_USB_TRANSACTION_ERROR,
166     CC_TRB_ERROR,
167     CC_STALL_ERROR,
168     CC_RESOURCE_ERROR,
169     CC_BANDWIDTH_ERROR,
170     CC_NO_SLOTS_ERROR,
171     CC_INVALID_STREAM_TYPE_ERROR,
172     CC_SLOT_NOT_ENABLED_ERROR,
173     CC_EP_NOT_ENABLED_ERROR,
174     CC_SHORT_PACKET,
175     CC_RING_UNDERRUN,
176     CC_RING_OVERRUN,
177     CC_VF_ER_FULL,
178     CC_PARAMETER_ERROR,
179     CC_BANDWIDTH_OVERRUN,
180     CC_CONTEXT_STATE_ERROR,
181     CC_NO_PING_RESPONSE_ERROR,
182     CC_EVENT_RING_FULL_ERROR,
183     CC_INCOMPATIBLE_DEVICE_ERROR,
184     CC_MISSED_SERVICE_ERROR,
185     CC_COMMAND_RING_STOPPED,
186     CC_COMMAND_ABORTED,
187     CC_STOPPED,
188     CC_STOPPED_LENGTH_INVALID,
189     CC_MAX_EXIT_LATENCY_TOO_LARGE_ERROR = 29,
190     CC_ISOCH_BUFFER_OVERRUN = 31,
191     CC_EVENT_LOST_ERROR,
192     CC_UNDEFINED_ERROR,
193     CC_INVALID_STREAM_ID_ERROR,
194     CC_SECONDARY_BANDWIDTH_ERROR,
195     CC_SPLIT_TRANSACTION_ERROR
196 } TRBCCode;
197 
198 enum {
199     PLS_U0              =  0,
200     PLS_U1              =  1,
201     PLS_U2              =  2,
202     PLS_U3              =  3,
203     PLS_DISABLED        =  4,
204     PLS_RX_DETECT       =  5,
205     PLS_INACTIVE        =  6,
206     PLS_POLLING         =  7,
207     PLS_RECOVERY        =  8,
208     PLS_HOT_RESET       =  9,
209     PLS_COMPILANCE_MODE = 10,
210     PLS_TEST_MODE       = 11,
211     PLS_RESUME          = 15,
212 };
213 
214 #define xhci_get_field(data, field)             \
215     (((data) >> field##_SHIFT) & field##_MASK)
216 
217 // --------------------------------------------------------------
218 // state structs
219 
220 struct xhci_ring {
221     struct xhci_trb      ring[XHCI_RING_ITEMS];
222     struct xhci_trb      evt;
223     u32                  eidx;
224     u32                  nidx;
225     u32                  cs;
226     struct mutex_s       lock;
227 };
228 
229 struct xhci_portmap {
230     u8 start;
231     u8 count;
232 };
233 
234 struct usb_xhci_s {
235     struct usb_s         usb;
236 
237     /* devinfo */
238     u32                  xcap;
239     u32                  ports;
240     u32                  slots;
241     u8                   context64;
242     struct xhci_portmap  usb2;
243     struct xhci_portmap  usb3;
244 
245     /* xhci registers */
246     struct xhci_caps     *caps;
247     struct xhci_op       *op;
248     struct xhci_pr       *pr;
249     struct xhci_ir       *ir;
250     struct xhci_db       *db;
251 
252     /* xhci data structures */
253     struct xhci_devlist  *devs;
254     struct xhci_ring     *cmds;
255     struct xhci_ring     *evts;
256     struct xhci_er_seg   *eseg;
257 };
258 
259 struct xhci_pipe {
260     struct xhci_ring     reqs;
261 
262     struct usb_pipe      pipe;
263     u32                  slotid;
264     u32                  epid;
265     void                 *buf;
266     int                  bufused;
267 };
268 
269 // --------------------------------------------------------------
270 // tables
271 
272 static const char *speed_name[16] = {
273     [ 0 ] = " - ",
274     [ 1 ] = "Full",
275     [ 2 ] = "Low",
276     [ 3 ] = "High",
277     [ 4 ] = "Super",
278 };
279 
280 static const int speed_from_xhci[16] = {
281     [ 0 ] = -1,
282     [ 1 ] = USB_FULLSPEED,
283     [ 2 ] = USB_LOWSPEED,
284     [ 3 ] = USB_HIGHSPEED,
285     [ 4 ] = USB_SUPERSPEED,
286     [ 5 ... 15 ] = -1,
287 };
288 
289 static const int speed_to_xhci[] = {
290     [ USB_FULLSPEED  ] = 1,
291     [ USB_LOWSPEED   ] = 2,
292     [ USB_HIGHSPEED  ] = 3,
293     [ USB_SUPERSPEED ] = 4,
294 };
295 
wait_bit(u32 * reg,u32 mask,int value,u32 timeout)296 static int wait_bit(u32 *reg, u32 mask, int value, u32 timeout)
297 {
298     u32 end = timer_calc(timeout);
299 
300     while ((readl(reg) & mask) != value) {
301         if (timer_check(end)) {
302             warn_timeout();
303             return -1;
304         }
305         yield();
306     }
307     return 0;
308 }
309 
310 
311 /****************************************************************
312  * Root hub
313  ****************************************************************/
314 
315 #define XHCI_TIME_POSTPOWER 20
316 
317 // Check if device attached to port
318 static void
xhci_print_port_state(int loglevel,const char * prefix,u32 port,u32 portsc)319 xhci_print_port_state(int loglevel, const char *prefix, u32 port, u32 portsc)
320 {
321     u32 pls = xhci_get_field(portsc, XHCI_PORTSC_PLS);
322     u32 speed = xhci_get_field(portsc, XHCI_PORTSC_SPEED);
323 
324     dprintf(loglevel, "%s port #%d: 0x%08x,%s%s pls %d, speed %d [%s]\n",
325             prefix, port + 1, portsc,
326             (portsc & XHCI_PORTSC_PP)  ? " powered," : "",
327             (portsc & XHCI_PORTSC_PED) ? " enabled," : "",
328             pls, speed, speed_name[speed]);
329 }
330 
331 static int
xhci_hub_detect(struct usbhub_s * hub,u32 port)332 xhci_hub_detect(struct usbhub_s *hub, u32 port)
333 {
334     struct usb_xhci_s *xhci = container_of(hub->cntl, struct usb_xhci_s, usb);
335     u32 portsc = readl(&xhci->pr[port].portsc);
336     return (portsc & XHCI_PORTSC_CCS) ? 1 : 0;
337 }
338 
339 // Reset device on port
340 static int
xhci_hub_reset(struct usbhub_s * hub,u32 port)341 xhci_hub_reset(struct usbhub_s *hub, u32 port)
342 {
343     struct usb_xhci_s *xhci = container_of(hub->cntl, struct usb_xhci_s, usb);
344     u32 portsc = readl(&xhci->pr[port].portsc);
345     if (!(portsc & XHCI_PORTSC_CCS))
346         // Device no longer connected?!
347         return -1;
348 
349     switch (xhci_get_field(portsc, XHCI_PORTSC_PLS)) {
350     case PLS_U0:
351         // A USB3 port - controller automatically performs reset
352         break;
353     case PLS_POLLING:
354         // A USB2 port - perform device reset
355         xhci_print_port_state(3, __func__, port, portsc);
356         writel(&xhci->pr[port].portsc, portsc | XHCI_PORTSC_PR);
357         break;
358     default:
359         return -1;
360     }
361 
362     // Wait for device to complete reset and be enabled
363     u32 end = timer_calc(100);
364     for (;;) {
365         portsc = readl(&xhci->pr[port].portsc);
366         if (!(portsc & XHCI_PORTSC_CCS))
367             // Device disconnected during reset
368             return -1;
369         if (portsc & XHCI_PORTSC_PED)
370             // Reset complete
371             break;
372         if (timer_check(end)) {
373             warn_timeout();
374             return -1;
375         }
376         yield();
377     }
378 
379     int rc = speed_from_xhci[xhci_get_field(portsc, XHCI_PORTSC_SPEED)];
380     xhci_print_port_state(1, "XHCI", port, portsc);
381     return rc;
382 }
383 
384 static int
xhci_hub_portmap(struct usbhub_s * hub,u32 vport)385 xhci_hub_portmap(struct usbhub_s *hub, u32 vport)
386 {
387     struct usb_xhci_s *xhci = container_of(hub->cntl, struct usb_xhci_s, usb);
388     u32 pport = vport + 1;
389 
390     if (vport + 1 >= xhci->usb3.start &&
391         vport + 1 < xhci->usb3.start + xhci->usb3.count)
392         pport = vport + 2 - xhci->usb3.start;
393 
394     if (vport + 1 >= xhci->usb2.start &&
395         vport + 1 < xhci->usb2.start + xhci->usb2.count)
396         pport = vport + 2 - xhci->usb2.start;
397 
398     return pport;
399 }
400 
401 static void
xhci_hub_disconnect(struct usbhub_s * hub,u32 port)402 xhci_hub_disconnect(struct usbhub_s *hub, u32 port)
403 {
404     // XXX - should turn the port power off.
405 }
406 
407 static struct usbhub_op_s xhci_hub_ops = {
408     .detect = xhci_hub_detect,
409     .reset = xhci_hub_reset,
410     .portmap = xhci_hub_portmap,
411     .disconnect = xhci_hub_disconnect,
412 };
413 
414 // Find any devices connected to the root hub.
415 static int
xhci_check_ports(struct usb_xhci_s * xhci)416 xhci_check_ports(struct usb_xhci_s *xhci)
417 {
418     // Wait for port power to stabilize.
419     msleep(XHCI_TIME_POSTPOWER);
420 
421     struct usbhub_s hub;
422     memset(&hub, 0, sizeof(hub));
423     hub.cntl = &xhci->usb;
424     hub.portcount = xhci->ports;
425     hub.op = &xhci_hub_ops;
426     usb_enumerate(&hub);
427     return hub.devcount;
428 }
429 
430 
431 /****************************************************************
432  * Setup
433  ****************************************************************/
434 
435 static void
xhci_free_pipes(struct usb_xhci_s * xhci)436 xhci_free_pipes(struct usb_xhci_s *xhci)
437 {
438     // XXX - should walk list of pipes and free unused pipes.
439 }
440 
441 static void
configure_xhci(void * data)442 configure_xhci(void *data)
443 {
444     struct usb_xhci_s *xhci = data;
445     u32 reg;
446 
447     xhci->devs = memalign_high(64, sizeof(*xhci->devs) * (xhci->slots + 1));
448     xhci->eseg = memalign_high(64, sizeof(*xhci->eseg));
449     xhci->cmds = memalign_high(XHCI_RING_SIZE, sizeof(*xhci->cmds));
450     xhci->evts = memalign_high(XHCI_RING_SIZE, sizeof(*xhci->evts));
451     if (!xhci->devs || !xhci->cmds || !xhci->evts || !xhci->eseg) {
452         warn_noalloc();
453         goto fail;
454     }
455     memset(xhci->devs, 0, sizeof(*xhci->devs) * (xhci->slots + 1));
456     memset(xhci->cmds, 0, sizeof(*xhci->cmds));
457     memset(xhci->evts, 0, sizeof(*xhci->evts));
458     memset(xhci->eseg, 0, sizeof(*xhci->eseg));
459 
460     reg = readl(&xhci->op->usbcmd);
461     if (reg & XHCI_CMD_RS) {
462         reg &= ~XHCI_CMD_RS;
463         writel(&xhci->op->usbcmd, reg);
464         if (wait_bit(&xhci->op->usbsts, XHCI_STS_HCH, XHCI_STS_HCH, 32) != 0)
465             goto fail;
466     }
467 
468     dprintf(3, "%s: resetting\n", __func__);
469     writel(&xhci->op->usbcmd, XHCI_CMD_HCRST);
470     if (wait_bit(&xhci->op->usbcmd, XHCI_CMD_HCRST, 0, 1000) != 0)
471         goto fail;
472     if (wait_bit(&xhci->op->usbsts, XHCI_STS_CNR, 0, 1000) != 0)
473         goto fail;
474 
475     writel(&xhci->op->config, xhci->slots);
476     writel(&xhci->op->dcbaap_low, (u32)xhci->devs);
477     writel(&xhci->op->dcbaap_high, 0);
478     writel(&xhci->op->crcr_low, (u32)xhci->cmds | 1);
479     writel(&xhci->op->crcr_high, 0);
480     xhci->cmds->cs = 1;
481 
482     xhci->eseg->ptr_low = (u32)xhci->evts;
483     xhci->eseg->ptr_high = 0;
484     xhci->eseg->size = XHCI_RING_ITEMS;
485     writel(&xhci->ir->erstsz, 1);
486     writel(&xhci->ir->erdp_low, (u32)xhci->evts);
487     writel(&xhci->ir->erdp_high, 0);
488     writel(&xhci->ir->erstba_low, (u32)xhci->eseg);
489     writel(&xhci->ir->erstba_high, 0);
490     xhci->evts->cs = 1;
491 
492     reg = readl(&xhci->caps->hcsparams2);
493     u32 spb = (reg >> 21 & 0x1f) << 5 | reg >> 27;
494     if (spb) {
495         dprintf(3, "%s: setup %d scratch pad buffers\n", __func__, spb);
496         u64 *spba = memalign_high(64, sizeof(*spba) * spb);
497         void *pad = memalign_high(PAGE_SIZE, PAGE_SIZE * spb);
498         if (!spba || !pad) {
499             warn_noalloc();
500             free(spba);
501             free(pad);
502             goto fail;
503         }
504         int i;
505         for (i = 0; i < spb; i++)
506             spba[i] = (u32)pad + (i * PAGE_SIZE);
507         xhci->devs[0].ptr_low = (u32)spba;
508         xhci->devs[0].ptr_high = 0;
509     }
510 
511     reg = readl(&xhci->op->usbcmd);
512     reg |= XHCI_CMD_RS;
513     writel(&xhci->op->usbcmd, reg);
514 
515     // Find devices
516     int count = xhci_check_ports(xhci);
517     xhci_free_pipes(xhci);
518     if (count)
519         // Success
520         return;
521 
522     // No devices found - shutdown and free controller.
523     dprintf(1, "XHCI no devices found\n");
524     reg = readl(&xhci->op->usbcmd);
525     reg &= ~XHCI_CMD_RS;
526     writel(&xhci->op->usbcmd, reg);
527     wait_bit(&xhci->op->usbsts, XHCI_STS_HCH, XHCI_STS_HCH, 32);
528 
529 fail:
530     free(xhci->eseg);
531     free(xhci->evts);
532     free(xhci->cmds);
533     free(xhci->devs);
534     free(xhci);
535 }
536 
537 static struct usb_xhci_s*
xhci_controller_setup(void * baseaddr)538 xhci_controller_setup(void *baseaddr)
539 {
540     struct usb_xhci_s *xhci = malloc_high(sizeof(*xhci));
541     if (!xhci) {
542         warn_noalloc();
543         return NULL;
544     }
545     memset(xhci, 0, sizeof(*xhci));
546     xhci->caps  = baseaddr;
547     xhci->op    = baseaddr + readb(&xhci->caps->caplength);
548     xhci->pr    = baseaddr + readb(&xhci->caps->caplength) + 0x400;
549     xhci->db    = baseaddr + readl(&xhci->caps->dboff);
550     xhci->ir    = baseaddr + readl(&xhci->caps->rtsoff) + 0x20;
551 
552     u32 hcs1 = readl(&xhci->caps->hcsparams1);
553     u32 hcc  = readl(&xhci->caps->hccparams);
554     xhci->ports = (hcs1 >> 24) & 0xff;
555     xhci->slots = hcs1         & 0xff;
556     xhci->xcap  = ((hcc >> 16) & 0xffff) << 2;
557     xhci->context64 = (hcc & 0x04) ? 1 : 0;
558     xhci->usb.type = USB_TYPE_XHCI;
559 
560     dprintf(1, "XHCI init: regs @ %p, %d ports, %d slots"
561             ", %d byte contexts\n"
562             , xhci->caps, xhci->ports, xhci->slots
563             , xhci->context64 ? 64 : 32);
564 
565     if (xhci->xcap) {
566         u32 off;
567         void *addr = baseaddr + xhci->xcap;
568         do {
569             struct xhci_xcap *xcap = addr;
570             u32 ports, name, cap = readl(&xcap->cap);
571             switch (cap & 0xff) {
572             case 0x02:
573                 name  = readl(&xcap->data[0]);
574                 ports = readl(&xcap->data[1]);
575                 u8 major = (cap >> 24) & 0xff;
576                 u8 minor = (cap >> 16) & 0xff;
577                 u8 count = (ports >> 8) & 0xff;
578                 u8 start = (ports >> 0) & 0xff;
579                 dprintf(1, "XHCI    protocol %c%c%c%c %x.%02x"
580                         ", %d ports (offset %d), def %x\n"
581                         , (name >>  0) & 0xff
582                         , (name >>  8) & 0xff
583                         , (name >> 16) & 0xff
584                         , (name >> 24) & 0xff
585                         , major, minor
586                         , count, start
587                         , ports >> 16);
588                 if (name == 0x20425355 /* "USB " */) {
589                     if (major == 2) {
590                         xhci->usb2.start = start;
591                         xhci->usb2.count = count;
592                     }
593                     if (major == 3) {
594                         xhci->usb3.start = start;
595                         xhci->usb3.count = count;
596                     }
597                 }
598                 break;
599             default:
600                 dprintf(1, "XHCI    extcap 0x%x @ %p\n", cap & 0xff, addr);
601                 break;
602             }
603             off = (cap >> 8) & 0xff;
604             addr += off << 2;
605         } while (off > 0);
606     }
607 
608     u32 pagesize = readl(&xhci->op->pagesize);
609     if (PAGE_SIZE != (pagesize<<12)) {
610         dprintf(1, "XHCI driver does not support page size code %d\n"
611                 , pagesize<<12);
612         free(xhci);
613         return NULL;
614     }
615 
616     return xhci;
617 }
618 
619 static void
xhci_controller_setup_pci(struct pci_device * pci)620 xhci_controller_setup_pci(struct pci_device *pci)
621 {
622     struct usb_xhci_s *xhci;
623     void *baseaddr;
624 
625     baseaddr = pci_enable_membar(pci, PCI_BASE_ADDRESS_0);
626     if (!baseaddr)
627         return;
628 
629     dprintf(1, "PCI: XHCI at %pP (mmio %p)\n", pci, baseaddr);
630     pci_enable_busmaster(pci);
631 
632     xhci = xhci_controller_setup(baseaddr);
633     if (!xhci)
634         return;
635 
636     xhci->usb.pci = pci;
637     run_thread(configure_xhci, xhci);
638 }
639 
640 static void
xhci_controller_setup_acpi(struct acpi_device * dev)641 xhci_controller_setup_acpi(struct acpi_device *dev)
642 {
643     struct usb_xhci_s *xhci;
644     u64 mem, unused;
645     void *baseaddr;
646 
647     if (acpi_dsdt_find_mem(dev, &mem, &unused) < 0)
648         return;
649     if (mem >= 0x100000000ll)
650         return;
651 
652     baseaddr = (void*)(u32)mem;
653     dprintf(1, "ACPI: XHCI at mmio %p\n", baseaddr);
654 
655     xhci = xhci_controller_setup(baseaddr);
656     if (!xhci)
657         return;
658 
659     xhci->usb.mmio = baseaddr;
660     run_thread(configure_xhci, xhci);
661 }
662 
663 void
xhci_setup(void)664 xhci_setup(void)
665 {
666     if (! CONFIG_USB_XHCI)
667         return;
668 
669     struct pci_device *pci;
670     foreachpci(pci) {
671         if (pci_classprog(pci) == PCI_CLASS_SERIAL_USB_XHCI)
672             xhci_controller_setup_pci(pci);
673     }
674 
675     u16 xhci_eisaid = 0x0d10;
676     struct acpi_device *dev;
677     for (dev = acpi_dsdt_find_eisaid(NULL, xhci_eisaid);
678          dev != NULL;
679          dev = acpi_dsdt_find_eisaid(dev, xhci_eisaid)) {
680         xhci_controller_setup_acpi(dev);
681     }
682 }
683 
684 
685 /****************************************************************
686  * End point communication
687  ****************************************************************/
688 
689 // Signal the hardware to process events on a TRB ring
xhci_doorbell(struct usb_xhci_s * xhci,u32 slotid,u32 value)690 static void xhci_doorbell(struct usb_xhci_s *xhci, u32 slotid, u32 value)
691 {
692     dprintf(5, "%s: slotid %d, epid %d\n", __func__, slotid, value);
693     struct xhci_db *db = xhci->db;
694     void *addr = &db[slotid].doorbell;
695     writel(addr, value);
696 }
697 
698 // Dequeue events on the XHCI command ring generated by the hardware
xhci_process_events(struct usb_xhci_s * xhci)699 static void xhci_process_events(struct usb_xhci_s *xhci)
700 {
701     struct xhci_ring *evts = xhci->evts;
702 
703     for (;;) {
704         /* check for event */
705         u32 nidx = evts->nidx;
706         u32 cs = evts->cs;
707         struct xhci_trb *etrb = evts->ring + nidx;
708         u32 control = etrb->control;
709         if ((control & TRB_C) != (cs ? 1 : 0))
710             return;
711 
712         /* process event */
713         u32 evt_type = TRB_TYPE(control);
714         u32 evt_cc = (etrb->status >> 24) & 0xff;
715         switch (evt_type) {
716         case ER_TRANSFER:
717         case ER_COMMAND_COMPLETE:
718         {
719             struct xhci_trb  *rtrb = (void*)etrb->ptr_low;
720             struct xhci_ring *ring = XHCI_RING(rtrb);
721             struct xhci_trb  *evt = &ring->evt;
722             u32 eidx = rtrb - ring->ring + 1;
723             dprintf(5, "%s: ring %p [trb %p, evt %p, type %d, eidx %d, cc %d]\n",
724                     __func__, ring, rtrb, evt, evt_type, eidx, evt_cc);
725             memcpy(evt, etrb, sizeof(*etrb));
726             ring->eidx = eidx;
727             break;
728         }
729         case ER_PORT_STATUS_CHANGE:
730         {
731             u32 port = ((etrb->ptr_low >> 24) & 0xff) - 1;
732             // Read status, and clear port status change bits
733             u32 portsc = readl(&xhci->pr[port].portsc);
734             u32 pclear = (((portsc & ~(XHCI_PORTSC_PED|XHCI_PORTSC_PR))
735                            & ~(XHCI_PORTSC_PLS_MASK<<XHCI_PORTSC_PLS_SHIFT))
736                           | (1<<XHCI_PORTSC_PLS_SHIFT));
737             writel(&xhci->pr[port].portsc, pclear);
738 
739             xhci_print_port_state(3, __func__, port, portsc);
740             break;
741         }
742         default:
743             dprintf(1, "%s: unknown event, type %d, cc %d\n",
744                     __func__, evt_type, evt_cc);
745             break;
746         }
747 
748         /* move ring index, notify xhci */
749         nidx++;
750         if (nidx == XHCI_RING_ITEMS) {
751             nidx = 0;
752             cs = cs ? 0 : 1;
753             evts->cs = cs;
754         }
755         evts->nidx = nidx;
756         struct xhci_ir *ir = xhci->ir;
757         u32 erdp = (u32)(evts->ring + nidx);
758         writel(&ir->erdp_low, erdp);
759         writel(&ir->erdp_high, 0);
760     }
761 }
762 
763 // Check if a ring has any pending TRBs
xhci_ring_busy(struct xhci_ring * ring)764 static int xhci_ring_busy(struct xhci_ring *ring)
765 {
766     u32 eidx = ring->eidx;
767     u32 nidx = ring->nidx;
768     return (eidx != nidx);
769 }
770 
771 // Wait for a ring to empty (all TRBs processed by hardware)
xhci_event_wait(struct usb_xhci_s * xhci,struct xhci_ring * ring,u32 timeout)772 static int xhci_event_wait(struct usb_xhci_s *xhci,
773                            struct xhci_ring *ring,
774                            u32 timeout)
775 {
776     u32 end = timer_calc(timeout);
777 
778     for (;;) {
779         xhci_process_events(xhci);
780         if (!xhci_ring_busy(ring)) {
781             u32 status = ring->evt.status;
782             return (status >> 24) & 0xff;
783         }
784         if (timer_check(end)) {
785             warn_timeout();
786             return -1;
787         }
788         yield();
789     }
790 }
791 
792 // Add a TRB to the given ring
xhci_trb_fill(struct xhci_ring * ring,void * data,u32 xferlen,u32 flags)793 static void xhci_trb_fill(struct xhci_ring *ring
794                           , void *data, u32 xferlen, u32 flags)
795 {
796     struct xhci_trb *dst = &ring->ring[ring->nidx];
797     if (flags & TRB_TR_IDT) {
798         memcpy(&dst->ptr_low, data, xferlen);
799     } else {
800         dst->ptr_low = (u32)data;
801         dst->ptr_high = 0;
802     }
803     dst->status = xferlen;
804     dst->control = flags | (ring->cs ? TRB_C : 0);
805 }
806 
807 // Queue a TRB onto a ring, wrapping ring as needed
xhci_trb_queue(struct xhci_ring * ring,void * data,u32 xferlen,u32 flags)808 static void xhci_trb_queue(struct xhci_ring *ring,
809                            void *data, u32 xferlen, u32 flags)
810 {
811     if (ring->nidx >= ARRAY_SIZE(ring->ring) - 1) {
812         xhci_trb_fill(ring, ring->ring, 0, (TR_LINK << 10) | TRB_LK_TC);
813         ring->nidx = 0;
814         ring->cs ^= 1;
815         dprintf(5, "%s: ring %p [linked]\n", __func__, ring);
816     }
817 
818     xhci_trb_fill(ring, data, xferlen, flags);
819     ring->nidx++;
820     dprintf(5, "%s: ring %p [nidx %d, len %d]\n",
821             __func__, ring, ring->nidx, xferlen);
822 }
823 
824 // Submit a command to the xhci controller ring
xhci_cmd_submit(struct usb_xhci_s * xhci,struct xhci_inctx * inctx,u32 flags)825 static int xhci_cmd_submit(struct usb_xhci_s *xhci, struct xhci_inctx *inctx
826                            , u32 flags)
827 {
828     if (inctx) {
829         struct xhci_slotctx *slot = (void*)&inctx[1 << xhci->context64];
830         u32 port = ((slot->ctx[1] >> 16) & 0xff) - 1;
831         u32 portsc = readl(&xhci->pr[port].portsc);
832         if (!(portsc & XHCI_PORTSC_CCS)) {
833             // Device no longer connected?!
834             xhci_print_port_state(1, __func__, port, portsc);
835             return -1;
836         }
837     }
838 
839     mutex_lock(&xhci->cmds->lock);
840     xhci_trb_queue(xhci->cmds, inctx, 0, flags);
841     xhci_doorbell(xhci, 0, 0);
842     int rc = xhci_event_wait(xhci, xhci->cmds, 1000);
843     mutex_unlock(&xhci->cmds->lock);
844     return rc;
845 }
846 
xhci_cmd_enable_slot(struct usb_xhci_s * xhci)847 static int xhci_cmd_enable_slot(struct usb_xhci_s *xhci)
848 {
849     dprintf(3, "%s:\n", __func__);
850     int cc = xhci_cmd_submit(xhci, NULL, CR_ENABLE_SLOT << 10);
851     if (cc != CC_SUCCESS)
852         return -1;
853     return (xhci->cmds->evt.control >> 24) & 0xff;
854 }
855 
xhci_cmd_disable_slot(struct usb_xhci_s * xhci,u32 slotid)856 static int xhci_cmd_disable_slot(struct usb_xhci_s *xhci, u32 slotid)
857 {
858     dprintf(3, "%s: slotid %d\n", __func__, slotid);
859     return xhci_cmd_submit(xhci, NULL, (CR_DISABLE_SLOT << 10) | (slotid << 24));
860 }
861 
xhci_cmd_address_device(struct usb_xhci_s * xhci,u32 slotid,struct xhci_inctx * inctx)862 static int xhci_cmd_address_device(struct usb_xhci_s *xhci, u32 slotid
863                                    , struct xhci_inctx *inctx)
864 {
865     dprintf(3, "%s: slotid %d\n", __func__, slotid);
866     return xhci_cmd_submit(xhci, inctx
867                            , (CR_ADDRESS_DEVICE << 10) | (slotid << 24));
868 }
869 
xhci_cmd_configure_endpoint(struct usb_xhci_s * xhci,u32 slotid,struct xhci_inctx * inctx)870 static int xhci_cmd_configure_endpoint(struct usb_xhci_s *xhci, u32 slotid
871                                        , struct xhci_inctx *inctx)
872 {
873     dprintf(3, "%s: slotid %d, add 0x%x, del 0x%x\n", __func__,
874             slotid, inctx->add, inctx->del);
875     return xhci_cmd_submit(xhci, inctx
876                            , (CR_CONFIGURE_ENDPOINT << 10) | (slotid << 24));
877 }
878 
xhci_cmd_evaluate_context(struct usb_xhci_s * xhci,u32 slotid,struct xhci_inctx * inctx)879 static int xhci_cmd_evaluate_context(struct usb_xhci_s *xhci, u32 slotid
880                                      , struct xhci_inctx *inctx)
881 {
882     dprintf(3, "%s: slotid %d, add 0x%x, del 0x%x\n", __func__,
883             slotid, inctx->add, inctx->del);
884     return xhci_cmd_submit(xhci, inctx
885                            , (CR_EVALUATE_CONTEXT << 10) | (slotid << 24));
886 }
887 
888 static struct xhci_inctx *
xhci_alloc_inctx(struct usbdevice_s * usbdev,int maxepid)889 xhci_alloc_inctx(struct usbdevice_s *usbdev, int maxepid)
890 {
891     struct usb_xhci_s *xhci = container_of(
892         usbdev->hub->cntl, struct usb_xhci_s, usb);
893     int size = (sizeof(struct xhci_inctx) * 33) << xhci->context64;
894     struct xhci_inctx *in = memalign_tmphigh(2048 << xhci->context64, size);
895     if (!in) {
896         warn_noalloc();
897         return NULL;
898     }
899     memset(in, 0, size);
900 
901     struct xhci_slotctx *slot = (void*)&in[1 << xhci->context64];
902     slot->ctx[0]    |= maxepid << 27; // context entries
903     slot->ctx[0]    |= speed_to_xhci[usbdev->speed] << 20;
904 
905     // Set high-speed hub flags.
906     struct usbdevice_s *hubdev = usbdev->hub->usbdev;
907     if (hubdev) {
908         if (usbdev->speed == USB_LOWSPEED || usbdev->speed == USB_FULLSPEED) {
909             struct xhci_pipe *hpipe = container_of(
910                 hubdev->defpipe, struct xhci_pipe, pipe);
911             if (hubdev->speed == USB_HIGHSPEED) {
912                 slot->ctx[2] |= hpipe->slotid;
913                 slot->ctx[2] |= (usbdev->port+1) << 8;
914             } else {
915                 struct xhci_slotctx *hslot = (void*)xhci->devs[hpipe->slotid].ptr_low;
916                 slot->ctx[2] = hslot->ctx[2];
917             }
918         }
919         u32 route = 0;
920         while (usbdev->hub->usbdev) {
921             route <<= 4;
922             route |= (usbdev->port+1) & 0xf;
923             usbdev = usbdev->hub->usbdev;
924         }
925         slot->ctx[0]    |= route;
926     }
927 
928     slot->ctx[1]    |= (usbdev->port+1) << 16;
929 
930     return in;
931 }
932 
xhci_config_hub(struct usbhub_s * hub)933 static int xhci_config_hub(struct usbhub_s *hub)
934 {
935     struct usb_xhci_s *xhci = container_of(
936         hub->cntl, struct usb_xhci_s, usb);
937     struct xhci_pipe *pipe = container_of(
938         hub->usbdev->defpipe, struct xhci_pipe, pipe);
939     struct xhci_slotctx *hdslot = (void*)xhci->devs[pipe->slotid].ptr_low;
940     if ((hdslot->ctx[3] >> 27) == 3)
941         // Already configured
942         return 0;
943     struct xhci_inctx *in = xhci_alloc_inctx(hub->usbdev, 1);
944     if (!in)
945         return -1;
946     in->add = 0x01;
947     struct xhci_slotctx *slot = (void*)&in[1 << xhci->context64];
948     slot->ctx[0] |= 1 << 26;
949     slot->ctx[1] |= hub->portcount << 24;
950 
951     int cc = xhci_cmd_configure_endpoint(xhci, pipe->slotid, in);
952     free(in);
953     if (cc != CC_SUCCESS) {
954         dprintf(1, "%s: configure hub: failed (cc %d)\n", __func__, cc);
955         return -1;
956     }
957     return 0;
958 }
959 
960 static struct usb_pipe *
xhci_alloc_pipe(struct usbdevice_s * usbdev,struct usb_endpoint_descriptor * epdesc)961 xhci_alloc_pipe(struct usbdevice_s *usbdev
962                 , struct usb_endpoint_descriptor *epdesc)
963 {
964     u8 eptype = epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
965     struct usb_xhci_s *xhci = container_of(
966         usbdev->hub->cntl, struct usb_xhci_s, usb);
967     struct xhci_pipe *pipe;
968     u32 epid;
969 
970     if (epdesc->bEndpointAddress == 0) {
971         epid = 1;
972     } else {
973         epid = (epdesc->bEndpointAddress & 0x0f) * 2;
974         epid += (epdesc->bEndpointAddress & USB_DIR_IN) ? 1 : 0;
975     }
976 
977     if (eptype == USB_ENDPOINT_XFER_CONTROL)
978         pipe = memalign_high(XHCI_RING_SIZE, sizeof(*pipe));
979     else
980         pipe = memalign_low(XHCI_RING_SIZE, sizeof(*pipe));
981     if (!pipe) {
982         warn_noalloc();
983         return NULL;
984     }
985     memset(pipe, 0, sizeof(*pipe));
986 
987     usb_desc2pipe(&pipe->pipe, usbdev, epdesc);
988     pipe->epid = epid;
989     pipe->reqs.cs = 1;
990     if (eptype == USB_ENDPOINT_XFER_INT) {
991         pipe->buf = malloc_high(pipe->pipe.maxpacket);
992         if (!pipe->buf) {
993             warn_noalloc();
994             free(pipe);
995             return NULL;
996         }
997     }
998 
999     // Allocate input context and initialize endpoint info.
1000     struct xhci_inctx *in = xhci_alloc_inctx(usbdev, epid);
1001     if (!in)
1002         goto fail;
1003     in->add = 0x01 | (1 << epid);
1004     struct xhci_epctx *ep = (void*)&in[(pipe->epid+1) << xhci->context64];
1005     if (eptype == USB_ENDPOINT_XFER_INT)
1006         ep->ctx[0] = (usb_get_period(usbdev, epdesc) + 3) << 16;
1007     ep->ctx[1]   |= eptype << 3;
1008     if (epdesc->bEndpointAddress & USB_DIR_IN
1009         || eptype == USB_ENDPOINT_XFER_CONTROL)
1010         ep->ctx[1] |= 1 << 5;
1011     ep->ctx[1]   |= pipe->pipe.maxpacket << 16;
1012     ep->deq_low  = (u32)&pipe->reqs.ring[0];
1013     ep->deq_low  |= 1;         // dcs
1014     ep->length   = pipe->pipe.maxpacket;
1015 
1016     dprintf(3, "%s: usbdev %p, ring %p, slotid %d, epid %d\n", __func__,
1017             usbdev, &pipe->reqs, pipe->slotid, pipe->epid);
1018     if (pipe->epid == 1) {
1019         if (usbdev->hub->usbdev) {
1020             // Make sure parent hub is configured.
1021             int ret = xhci_config_hub(usbdev->hub);
1022             if (ret)
1023                 goto fail;
1024         }
1025         // Enable slot.
1026         u32 size = (sizeof(struct xhci_slotctx) * 32) << xhci->context64;
1027         struct xhci_slotctx *dev = memalign_high(1024 << xhci->context64, size);
1028         if (!dev) {
1029             warn_noalloc();
1030             goto fail;
1031         }
1032         int slotid = xhci_cmd_enable_slot(xhci);
1033         if (slotid < 0) {
1034             dprintf(1, "%s: enable slot: failed\n", __func__);
1035             free(dev);
1036             goto fail;
1037         }
1038         dprintf(3, "%s: enable slot: got slotid %d\n", __func__, slotid);
1039         memset(dev, 0, size);
1040         xhci->devs[slotid].ptr_low = (u32)dev;
1041         xhci->devs[slotid].ptr_high = 0;
1042 
1043         // Send set_address command.
1044         int cc = xhci_cmd_address_device(xhci, slotid, in);
1045         if (cc != CC_SUCCESS) {
1046             dprintf(1, "%s: address device: failed (cc %d)\n", __func__, cc);
1047             cc = xhci_cmd_disable_slot(xhci, slotid);
1048             if (cc != CC_SUCCESS) {
1049                 dprintf(1, "%s: disable failed (cc %d)\n", __func__, cc);
1050                 goto fail;
1051             }
1052             xhci->devs[slotid].ptr_low = 0;
1053             free(dev);
1054             goto fail;
1055         }
1056         pipe->slotid = slotid;
1057     } else {
1058         struct xhci_pipe *defpipe = container_of(
1059             usbdev->defpipe, struct xhci_pipe, pipe);
1060         pipe->slotid = defpipe->slotid;
1061         // Send configure command.
1062         int cc = xhci_cmd_configure_endpoint(xhci, pipe->slotid, in);
1063         if (cc != CC_SUCCESS) {
1064             dprintf(1, "%s: configure endpoint: failed (cc %d)\n", __func__, cc);
1065             goto fail;
1066         }
1067     }
1068     free(in);
1069     return &pipe->pipe;
1070 
1071 fail:
1072     free(pipe->buf);
1073     free(pipe);
1074     free(in);
1075     return NULL;
1076 }
1077 
1078 struct usb_pipe *
xhci_realloc_pipe(struct usbdevice_s * usbdev,struct usb_pipe * upipe,struct usb_endpoint_descriptor * epdesc)1079 xhci_realloc_pipe(struct usbdevice_s *usbdev, struct usb_pipe *upipe
1080                   , struct usb_endpoint_descriptor *epdesc)
1081 {
1082     if (!CONFIG_USB_XHCI)
1083         return NULL;
1084     if (!epdesc) {
1085         usb_add_freelist(upipe);
1086         return NULL;
1087     }
1088     if (!upipe)
1089         return xhci_alloc_pipe(usbdev, epdesc);
1090     u8 eptype = epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
1091     int oldmaxpacket = upipe->maxpacket;
1092     usb_desc2pipe(upipe, usbdev, epdesc);
1093     struct xhci_pipe *pipe = container_of(upipe, struct xhci_pipe, pipe);
1094     struct usb_xhci_s *xhci = container_of(
1095         pipe->pipe.cntl, struct usb_xhci_s, usb);
1096     dprintf(3, "%s: usbdev %p, ring %p, slotid %d, epid %d\n", __func__,
1097             usbdev, &pipe->reqs, pipe->slotid, pipe->epid);
1098     if (eptype != USB_ENDPOINT_XFER_CONTROL || upipe->maxpacket == oldmaxpacket)
1099         return upipe;
1100 
1101     // maxpacket has changed on control endpoint - update controller.
1102     dprintf(1, "%s: reconf ctl endpoint pkt size: %d -> %d\n",
1103             __func__, oldmaxpacket, pipe->pipe.maxpacket);
1104     struct xhci_inctx *in = xhci_alloc_inctx(usbdev, 1);
1105     if (!in)
1106         return upipe;
1107     in->add = (1 << 1);
1108     struct xhci_epctx *ep = (void*)&in[2 << xhci->context64];
1109     ep->ctx[1] |= (pipe->pipe.maxpacket << 16);
1110     int cc = xhci_cmd_evaluate_context(xhci, pipe->slotid, in);
1111     if (cc != CC_SUCCESS) {
1112         dprintf(1, "%s: reconf ctl endpoint: failed (cc %d)\n",
1113                 __func__, cc);
1114     }
1115     free(in);
1116 
1117     return upipe;
1118 }
1119 
1120 // Submit a USB "setup" message request to the pipe's ring
xhci_xfer_setup(struct xhci_pipe * pipe,int dir,void * cmd,void * data,int datalen)1121 static void xhci_xfer_setup(struct xhci_pipe *pipe, int dir, void *cmd
1122                             , void *data, int datalen)
1123 {
1124     struct usb_xhci_s *xhci = container_of(
1125         pipe->pipe.cntl, struct usb_xhci_s, usb);
1126     xhci_trb_queue(&pipe->reqs, cmd, USB_CONTROL_SETUP_SIZE
1127                    , (TR_SETUP << 10) | TRB_TR_IDT
1128                    | ((datalen ? (dir ? 3 : 2) : 0) << 16));
1129     if (datalen)
1130         xhci_trb_queue(&pipe->reqs, data, datalen, (TR_DATA << 10)
1131                        | ((dir ? 1 : 0) << 16));
1132     xhci_trb_queue(&pipe->reqs, NULL, 0, (TR_STATUS << 10) | TRB_TR_IOC
1133                    | ((dir ? 0 : 1) << 16));
1134     xhci_doorbell(xhci, pipe->slotid, pipe->epid);
1135 }
1136 
1137 // Submit a USB transfer request to the pipe's ring
xhci_xfer_normal(struct xhci_pipe * pipe,void * data,int datalen)1138 static void xhci_xfer_normal(struct xhci_pipe *pipe,
1139                              void *data, int datalen)
1140 {
1141     struct usb_xhci_s *xhci = container_of(
1142         pipe->pipe.cntl, struct usb_xhci_s, usb);
1143     xhci_trb_queue(&pipe->reqs, data, datalen, (TR_NORMAL << 10) | TRB_TR_IOC);
1144     xhci_doorbell(xhci, pipe->slotid, pipe->epid);
1145 }
1146 
1147 int
xhci_send_pipe(struct usb_pipe * p,int dir,const void * cmd,void * data,int datalen)1148 xhci_send_pipe(struct usb_pipe *p, int dir, const void *cmd
1149                , void *data, int datalen)
1150 {
1151     if (!CONFIG_USB_XHCI)
1152         return -1;
1153     struct xhci_pipe *pipe = container_of(p, struct xhci_pipe, pipe);
1154     struct usb_xhci_s *xhci = container_of(
1155         pipe->pipe.cntl, struct usb_xhci_s, usb);
1156 
1157     if (cmd) {
1158         const struct usb_ctrlrequest *req = cmd;
1159         if (req->bRequest == USB_REQ_SET_ADDRESS)
1160             // Set address command sent during xhci_alloc_pipe.
1161             return 0;
1162         xhci_xfer_setup(pipe, dir, (void*)req, data, datalen);
1163     } else {
1164         xhci_xfer_normal(pipe, data, datalen);
1165     }
1166 
1167     int cc = xhci_event_wait(xhci, &pipe->reqs, usb_xfer_time(p, datalen));
1168     if (cc != CC_SUCCESS) {
1169         dprintf(1, "%s: xfer failed (cc %d)\n", __func__, cc);
1170         return -1;
1171     }
1172 
1173     return 0;
1174 }
1175 
1176 int VISIBLE32FLAT
xhci_poll_intr(struct usb_pipe * p,void * data)1177 xhci_poll_intr(struct usb_pipe *p, void *data)
1178 {
1179     if (!CONFIG_USB_XHCI)
1180         return -1;
1181 
1182     struct xhci_pipe *pipe = container_of(p, struct xhci_pipe, pipe);
1183     struct usb_xhci_s *xhci = container_of(
1184         pipe->pipe.cntl, struct usb_xhci_s, usb);
1185     u32 len = pipe->pipe.maxpacket;
1186     void *buf = pipe->buf;
1187     int bufused = pipe->bufused;
1188 
1189     if (!bufused) {
1190         xhci_xfer_normal(pipe, buf, len);
1191         bufused = 1;
1192         pipe->bufused = bufused;
1193         return -1;
1194     }
1195 
1196     xhci_process_events(xhci);
1197     if (xhci_ring_busy(&pipe->reqs))
1198         return -1;
1199     dprintf(5, "%s: st %x ct %x [ %p <= %p / %d ]\n", __func__,
1200             pipe->reqs.evt.status,
1201             pipe->reqs.evt.control,
1202             data, buf, len);
1203     memcpy(data, buf, len);
1204     xhci_xfer_normal(pipe, buf, len);
1205     return 0;
1206 }
1207