iPXE
xhci.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27#include <stdlib.h>
28#include <stdio.h>
29#include <unistd.h>
30#include <string.h>
31#include <strings.h>
32#include <errno.h>
33#include <byteswap.h>
34#include <ipxe/malloc.h>
35#include <ipxe/pci.h>
36#include <ipxe/usb.h>
37#include <ipxe/init.h>
38#include <ipxe/profile.h>
39#include <ipxe/xhci.h>
40
41/** @file
42 *
43 * USB eXtensible Host Controller Interface (xHCI) driver
44 *
45 */
46
47/** Message transfer profiler */
48static struct profiler xhci_message_profiler __profiler =
49 { .name = "xhci.message" };
50
51/** Stream transfer profiler */
52static struct profiler xhci_stream_profiler __profiler =
53 { .name = "xhci.stream" };
54
55/** Event ring profiler */
56static struct profiler xhci_event_profiler __profiler =
57 { .name = "xhci.event" };
58
59/** Transfer event profiler */
60static struct profiler xhci_transfer_profiler __profiler =
61 { .name = "xhci.transfer" };
62
63/* Disambiguate the various error causes */
64#define EIO_DATA \
65 __einfo_error ( EINFO_EIO_DATA )
66#define EINFO_EIO_DATA \
67 __einfo_uniqify ( EINFO_EIO, ( 2 - 0 ), \
68 "Data buffer error" )
69#define EIO_BABBLE \
70 __einfo_error ( EINFO_EIO_BABBLE )
71#define EINFO_EIO_BABBLE \
72 __einfo_uniqify ( EINFO_EIO, ( 3 - 0 ), \
73 "Babble detected" )
74#define EIO_USB \
75 __einfo_error ( EINFO_EIO_USB )
76#define EINFO_EIO_USB \
77 __einfo_uniqify ( EINFO_EIO, ( 4 - 0 ), \
78 "USB transaction error" )
79#define EIO_TRB \
80 __einfo_error ( EINFO_EIO_TRB )
81#define EINFO_EIO_TRB \
82 __einfo_uniqify ( EINFO_EIO, ( 5 - 0 ), \
83 "TRB error" )
84#define EIO_STALL \
85 __einfo_error ( EINFO_EIO_STALL )
86#define EINFO_EIO_STALL \
87 __einfo_uniqify ( EINFO_EIO, ( 6 - 0 ), \
88 "Stall error" )
89#define EIO_RESOURCE \
90 __einfo_error ( EINFO_EIO_RESOURCE )
91#define EINFO_EIO_RESOURCE \
92 __einfo_uniqify ( EINFO_EIO, ( 7 - 0 ), \
93 "Resource error" )
94#define EIO_BANDWIDTH \
95 __einfo_error ( EINFO_EIO_BANDWIDTH )
96#define EINFO_EIO_BANDWIDTH \
97 __einfo_uniqify ( EINFO_EIO, ( 8 - 0 ), \
98 "Bandwidth error" )
99#define EIO_NO_SLOTS \
100 __einfo_error ( EINFO_EIO_NO_SLOTS )
101#define EINFO_EIO_NO_SLOTS \
102 __einfo_uniqify ( EINFO_EIO, ( 9 - 0 ), \
103 "No slots available" )
104#define EIO_STREAM_TYPE \
105 __einfo_error ( EINFO_EIO_STREAM_TYPE )
106#define EINFO_EIO_STREAM_TYPE \
107 __einfo_uniqify ( EINFO_EIO, ( 10 - 0 ), \
108 "Invalid stream type" )
109#define EIO_SLOT \
110 __einfo_error ( EINFO_EIO_SLOT )
111#define EINFO_EIO_SLOT \
112 __einfo_uniqify ( EINFO_EIO, ( 11 - 0 ), \
113 "Slot not enabled" )
114#define EIO_ENDPOINT \
115 __einfo_error ( EINFO_EIO_ENDPOINT )
116#define EINFO_EIO_ENDPOINT \
117 __einfo_uniqify ( EINFO_EIO, ( 12 - 0 ), \
118 "Endpoint not enabled" )
119#define EIO_SHORT \
120 __einfo_error ( EINFO_EIO_SHORT )
121#define EINFO_EIO_SHORT \
122 __einfo_uniqify ( EINFO_EIO, ( 13 - 0 ), \
123 "Short packet" )
124#define EIO_UNDERRUN \
125 __einfo_error ( EINFO_EIO_UNDERRUN )
126#define EINFO_EIO_UNDERRUN \
127 __einfo_uniqify ( EINFO_EIO, ( 14 - 0 ), \
128 "Ring underrun" )
129#define EIO_OVERRUN \
130 __einfo_error ( EINFO_EIO_OVERRUN )
131#define EINFO_EIO_OVERRUN \
132 __einfo_uniqify ( EINFO_EIO, ( 15 - 0 ), \
133 "Ring overrun" )
134#define EIO_VF_RING_FULL \
135 __einfo_error ( EINFO_EIO_VF_RING_FULL )
136#define EINFO_EIO_VF_RING_FULL \
137 __einfo_uniqify ( EINFO_EIO, ( 16 - 0 ), \
138 "Virtual function event ring full" )
139#define EIO_PARAMETER \
140 __einfo_error ( EINFO_EIO_PARAMETER )
141#define EINFO_EIO_PARAMETER \
142 __einfo_uniqify ( EINFO_EIO, ( 17 - 0 ), \
143 "Parameter error" )
144#define EIO_BANDWIDTH_OVERRUN \
145 __einfo_error ( EINFO_EIO_BANDWIDTH_OVERRUN )
146#define EINFO_EIO_BANDWIDTH_OVERRUN \
147 __einfo_uniqify ( EINFO_EIO, ( 18 - 0 ), \
148 "Bandwidth overrun" )
149#define EIO_CONTEXT \
150 __einfo_error ( EINFO_EIO_CONTEXT )
151#define EINFO_EIO_CONTEXT \
152 __einfo_uniqify ( EINFO_EIO, ( 19 - 0 ), \
153 "Context state error" )
154#define EIO_NO_PING \
155 __einfo_error ( EINFO_EIO_NO_PING )
156#define EINFO_EIO_NO_PING \
157 __einfo_uniqify ( EINFO_EIO, ( 20 - 0 ), \
158 "No ping response" )
159#define EIO_RING_FULL \
160 __einfo_error ( EINFO_EIO_RING_FULL )
161#define EINFO_EIO_RING_FULL \
162 __einfo_uniqify ( EINFO_EIO, ( 21 - 0 ), \
163 "Event ring full" )
164#define EIO_INCOMPATIBLE \
165 __einfo_error ( EINFO_EIO_INCOMPATIBLE )
166#define EINFO_EIO_INCOMPATIBLE \
167 __einfo_uniqify ( EINFO_EIO, ( 22 - 0 ), \
168 "Incompatible device" )
169#define EIO_MISSED \
170 __einfo_error ( EINFO_EIO_MISSED )
171#define EINFO_EIO_MISSED \
172 __einfo_uniqify ( EINFO_EIO, ( 23 - 0 ), \
173 "Missed service error" )
174#define EIO_CMD_STOPPED \
175 __einfo_error ( EINFO_EIO_CMD_STOPPED )
176#define EINFO_EIO_CMD_STOPPED \
177 __einfo_uniqify ( EINFO_EIO, ( 24 - 0 ), \
178 "Command ring stopped" )
179#define EIO_CMD_ABORTED \
180 __einfo_error ( EINFO_EIO_CMD_ABORTED )
181#define EINFO_EIO_CMD_ABORTED \
182 __einfo_uniqify ( EINFO_EIO, ( 25 - 0 ), \
183 "Command aborted" )
184#define EIO_STOP \
185 __einfo_error ( EINFO_EIO_STOP )
186#define EINFO_EIO_STOP \
187 __einfo_uniqify ( EINFO_EIO, ( 26 - 0 ), \
188 "Stopped" )
189#define EIO_STOP_LEN \
190 __einfo_error ( EINFO_EIO_STOP_LEN )
191#define EINFO_EIO_STOP_LEN \
192 __einfo_uniqify ( EINFO_EIO, ( 27 - 0 ), \
193 "Stopped - length invalid" )
194#define EIO_STOP_SHORT \
195 __einfo_error ( EINFO_EIO_STOP_SHORT )
196#define EINFO_EIO_STOP_SHORT \
197 __einfo_uniqify ( EINFO_EIO, ( 28 - 0 ), \
198 "Stopped - short packet" )
199#define EIO_LATENCY \
200 __einfo_error ( EINFO_EIO_LATENCY )
201#define EINFO_EIO_LATENCY \
202 __einfo_uniqify ( EINFO_EIO, ( 29 - 0 ), \
203 "Maximum exit latency too large" )
204#define EIO_ISOCH \
205 __einfo_error ( EINFO_EIO_ISOCH )
206#define EINFO_EIO_ISOCH \
207 __einfo_uniqify ( EINFO_EIO, ( 31 - 0 ), \
208 "Isochronous buffer overrun" )
209#define EPROTO_LOST \
210 __einfo_error ( EINFO_EPROTO_LOST )
211#define EINFO_EPROTO_LOST \
212 __einfo_uniqify ( EINFO_EPROTO, ( 32 - 32 ), \
213 "Event lost" )
214#define EPROTO_UNDEFINED \
215 __einfo_error ( EINFO_EPROTO_UNDEFINED )
216#define EINFO_EPROTO_UNDEFINED \
217 __einfo_uniqify ( EINFO_EPROTO, ( 33 - 32 ), \
218 "Undefined error" )
219#define EPROTO_STREAM_ID \
220 __einfo_error ( EINFO_EPROTO_STREAM_ID )
221#define EINFO_EPROTO_STREAM_ID \
222 __einfo_uniqify ( EINFO_EPROTO, ( 34 - 32 ), \
223 "Invalid stream ID" )
224#define EPROTO_SECONDARY \
225 __einfo_error ( EINFO_EPROTO_SECONDARY )
226#define EINFO_EPROTO_SECONDARY \
227 __einfo_uniqify ( EINFO_EPROTO, ( 35 - 32 ), \
228 "Secondary bandwidth error" )
229#define EPROTO_SPLIT \
230 __einfo_error ( EINFO_EPROTO_SPLIT )
231#define EINFO_EPROTO_SPLIT \
232 __einfo_uniqify ( EINFO_EPROTO, ( 36 - 32 ), \
233 "Split transaction error" )
234#define ECODE(code) \
235 ( ( (code) < 32 ) ? \
236 EUNIQ ( EINFO_EIO, ( (code) & 31 ), EIO_DATA, EIO_BABBLE, \
237 EIO_USB, EIO_TRB, EIO_STALL, EIO_RESOURCE, \
238 EIO_BANDWIDTH, EIO_NO_SLOTS, EIO_STREAM_TYPE, \
239 EIO_SLOT, EIO_ENDPOINT, EIO_SHORT, EIO_UNDERRUN, \
240 EIO_OVERRUN, EIO_VF_RING_FULL, EIO_PARAMETER, \
241 EIO_BANDWIDTH_OVERRUN, EIO_CONTEXT, EIO_NO_PING, \
242 EIO_RING_FULL, EIO_INCOMPATIBLE, EIO_MISSED, \
243 EIO_CMD_STOPPED, EIO_CMD_ABORTED, EIO_STOP, \
244 EIO_STOP_LEN, EIO_STOP_SHORT, EIO_LATENCY, \
245 EIO_ISOCH ) : \
246 ( (code) < 64 ) ? \
247 EUNIQ ( EINFO_EPROTO, ( (code) & 31 ), EPROTO_LOST, \
248 EPROTO_UNDEFINED, EPROTO_STREAM_ID, \
249 EPROTO_SECONDARY, EPROTO_SPLIT ) : \
250 EFAULT )
251
252/******************************************************************************
253 *
254 * Register access
255 *
256 ******************************************************************************
257 */
258
259/**
260 * Initialise device
261 *
262 * @v xhci xHCI device
263 */
264void xhci_init ( struct xhci_device *xhci ) {
265 uint32_t hcsparams1;
266 uint32_t hcsparams2;
267 uint32_t hccparams1;
268 uint32_t pagesize;
269 size_t caplength;
270 size_t rtsoff;
271 size_t dboff;
272
273 /* Set device name */
274 xhci->name = xhci->dev->name;
275
276 /* Locate capability, operational, runtime, and doorbell registers */
277 xhci->cap = xhci->regs;
278 caplength = readb ( xhci->cap + XHCI_CAP_CAPLENGTH );
279 rtsoff = readl ( xhci->cap + XHCI_CAP_RTSOFF );
280 dboff = readl ( xhci->cap + XHCI_CAP_DBOFF );
281 xhci->op = ( xhci->cap + caplength );
282 xhci->run = ( xhci->cap + rtsoff );
283 xhci->db = ( xhci->cap + dboff );
284 DBGC2 ( xhci, "XHCI %s cap %08lx op %08lx run %08lx db %08lx\n",
285 xhci->name, virt_to_phys ( xhci->cap ),
286 virt_to_phys ( xhci->op ), virt_to_phys ( xhci->run ),
287 virt_to_phys ( xhci->db ) );
288
289 /* Read structural parameters 1 */
290 hcsparams1 = readl ( xhci->cap + XHCI_CAP_HCSPARAMS1 );
291 xhci->slots = XHCI_HCSPARAMS1_SLOTS ( hcsparams1 );
292 xhci->intrs = XHCI_HCSPARAMS1_INTRS ( hcsparams1 );
293 xhci->ports = XHCI_HCSPARAMS1_PORTS ( hcsparams1 );
294 DBGC ( xhci, "XHCI %s has %d slots %d intrs %d ports\n",
295 xhci->name, xhci->slots, xhci->intrs, xhci->ports );
296
297 /* Read structural parameters 2 */
298 hcsparams2 = readl ( xhci->cap + XHCI_CAP_HCSPARAMS2 );
299 xhci->scratch.count = XHCI_HCSPARAMS2_SCRATCHPADS ( hcsparams2 );
300 DBGC2 ( xhci, "XHCI %s needs %d scratchpads\n",
301 xhci->name, xhci->scratch.count );
302
303 /* Read capability parameters 1 */
304 hccparams1 = readl ( xhci->cap + XHCI_CAP_HCCPARAMS1 );
305 xhci->addr64 = XHCI_HCCPARAMS1_ADDR64 ( hccparams1 );
306 xhci->csz_shift = XHCI_HCCPARAMS1_CSZ_SHIFT ( hccparams1 );
307 xhci->xecp = XHCI_HCCPARAMS1_XECP ( hccparams1 );
308
309 /* Read page size */
310 pagesize = readl ( xhci->op + XHCI_OP_PAGESIZE );
311 xhci->pagesize = XHCI_PAGESIZE ( pagesize );
312 assert ( xhci->pagesize != 0 );
313 assert ( ( ( xhci->pagesize ) & ( xhci->pagesize - 1 ) ) == 0 );
314 DBGC2 ( xhci, "XHCI %s page size %zd bytes\n",
315 xhci->name, xhci->pagesize );
316
317 /* Configure DMA device */
318 if ( xhci->dma && xhci->addr64 )
319 dma_set_mask_64bit ( xhci->dma );
320}
321
322/**
323 * Find extended capability
324 *
325 * @v xhci xHCI device
326 * @v id Capability ID
327 * @v offset Offset to previous extended capability instance, or zero
328 * @ret offset Offset to extended capability, or zero if not found
329 */
330static unsigned int xhci_extended_capability ( struct xhci_device *xhci,
331 unsigned int id,
332 unsigned int offset ) {
333 uint32_t xecp;
334 unsigned int next;
335
336 /* Locate the extended capability */
337 while ( 1 ) {
338
339 /* Locate first or next capability as applicable */
340 if ( offset ) {
341 xecp = readl ( xhci->cap + offset );
342 next = XHCI_XECP_NEXT ( xecp );
343 } else {
344 next = xhci->xecp;
345 }
346 if ( ! next )
347 return 0;
348 offset += next;
349
350 /* Check if this is the requested capability */
351 xecp = readl ( xhci->cap + offset );
352 if ( XHCI_XECP_ID ( xecp ) == id )
353 return offset;
354 }
355}
356
357/**
358 * Write potentially 64-bit register
359 *
360 * @v xhci xHCI device
361 * @v value Value
362 * @v reg Register address
363 * @ret rc Return status code
364 */
365static inline __attribute__ (( always_inline )) int
366xhci_writeq ( struct xhci_device *xhci, physaddr_t value, void *reg ) {
367
368 /* If this is a 32-bit build, then this can never fail
369 * (allowing the compiler to optimise out the error path).
370 */
371 if ( sizeof ( value ) <= sizeof ( uint32_t ) ) {
372 writel ( value, reg );
373 writel ( 0, ( reg + sizeof ( uint32_t ) ) );
374 return 0;
375 }
376
377 /* If the device does not support 64-bit addresses and this
378 * address is outside the 32-bit address space, then fail.
379 */
380 if ( ( value & ~0xffffffffULL ) && ! xhci->addr64 ) {
381 DBGC ( xhci, "XHCI %s cannot access address %lx\n",
382 xhci->name, value );
383 return -ENOTSUP;
384 }
385
386 /* If this is a 64-bit build, then writeq() is available */
387 writeq ( value, reg );
388 return 0;
389}
390
391/**
392 * Calculate buffer alignment
393 *
394 * @v len Length
395 * @ret align Buffer alignment
396 *
397 * Determine alignment required for a buffer which must be aligned to
398 * at least XHCI_MIN_ALIGN and which must not cross a page boundary.
399 */
400static inline size_t xhci_align ( size_t len ) {
401 size_t align;
402
403 /* Align to own length (rounded up to a power of two) */
404 align = ( 1 << fls ( len - 1 ) );
405
406 /* Round up to XHCI_MIN_ALIGN if needed */
407 if ( align < XHCI_MIN_ALIGN )
408 align = XHCI_MIN_ALIGN;
409
410 return align;
411}
412
413/**
414 * Calculate device context offset
415 *
416 * @v xhci xHCI device
417 * @v ctx Context index
418 */
419static inline size_t xhci_device_context_offset ( struct xhci_device *xhci,
420 unsigned int ctx ) {
421
422 return ( XHCI_DCI ( ctx ) << xhci->csz_shift );
423}
424
425/**
426 * Calculate input context offset
427 *
428 * @v xhci xHCI device
429 * @v ctx Context index
430 */
431static inline size_t xhci_input_context_offset ( struct xhci_device *xhci,
432 unsigned int ctx ) {
433
434 return ( XHCI_ICI ( ctx ) << xhci->csz_shift );
435}
436
437/******************************************************************************
438 *
439 * Diagnostics
440 *
441 ******************************************************************************
442 */
443
444/**
445 * Dump host controller registers
446 *
447 * @v xhci xHCI device
448 */
449static inline void xhci_dump ( struct xhci_device *xhci ) {
450 uint32_t usbcmd;
451 uint32_t usbsts;
452 uint32_t pagesize;
453 uint32_t dnctrl;
454 uint32_t config;
455
456 /* Do nothing unless debugging is enabled */
457 if ( ! DBG_LOG )
458 return;
459
460 /* Dump USBCMD */
461 usbcmd = readl ( xhci->op + XHCI_OP_USBCMD );
462 DBGC ( xhci, "XHCI %s USBCMD %08x%s%s\n", xhci->name, usbcmd,
463 ( ( usbcmd & XHCI_USBCMD_RUN ) ? " run" : "" ),
464 ( ( usbcmd & XHCI_USBCMD_HCRST ) ? " hcrst" : "" ) );
465
466 /* Dump USBSTS */
467 usbsts = readl ( xhci->op + XHCI_OP_USBSTS );
468 DBGC ( xhci, "XHCI %s USBSTS %08x%s\n", xhci->name, usbsts,
469 ( ( usbsts & XHCI_USBSTS_HCH ) ? " hch" : "" ) );
470
471 /* Dump PAGESIZE */
472 pagesize = readl ( xhci->op + XHCI_OP_PAGESIZE );
473 DBGC ( xhci, "XHCI %s PAGESIZE %08x\n", xhci->name, pagesize );
474
475 /* Dump DNCTRL */
476 dnctrl = readl ( xhci->op + XHCI_OP_DNCTRL );
477 DBGC ( xhci, "XHCI %s DNCTRL %08x\n", xhci->name, dnctrl );
478
479 /* Dump CONFIG */
480 config = readl ( xhci->op + XHCI_OP_CONFIG );
481 DBGC ( xhci, "XHCI %s CONFIG %08x\n", xhci->name, config );
482}
483
484/**
485 * Dump port registers
486 *
487 * @v xhci xHCI device
488 * @v port Port number
489 */
490static inline void xhci_dump_port ( struct xhci_device *xhci,
491 unsigned int port ) {
492 uint32_t portsc;
493 uint32_t portpmsc;
494 uint32_t portli;
495 uint32_t porthlpmc;
496
497 /* Do nothing unless debugging is enabled */
498 if ( ! DBG_LOG )
499 return;
500
501 /* Dump PORTSC */
502 portsc = readl ( xhci->op + XHCI_OP_PORTSC ( port ) );
503 DBGC ( xhci, "XHCI %s-%d PORTSC %08x%s%s%s%s psiv=%d\n",
504 xhci->name, port, portsc,
505 ( ( portsc & XHCI_PORTSC_CCS ) ? " ccs" : "" ),
506 ( ( portsc & XHCI_PORTSC_PED ) ? " ped" : "" ),
507 ( ( portsc & XHCI_PORTSC_PR ) ? " pr" : "" ),
508 ( ( portsc & XHCI_PORTSC_PP ) ? " pp" : "" ),
509 XHCI_PORTSC_PSIV ( portsc ) );
510
511 /* Dump PORTPMSC */
512 portpmsc = readl ( xhci->op + XHCI_OP_PORTPMSC ( port ) );
513 DBGC ( xhci, "XHCI %s-%d PORTPMSC %08x\n", xhci->name, port, portpmsc );
514
515 /* Dump PORTLI */
516 portli = readl ( xhci->op + XHCI_OP_PORTLI ( port ) );
517 DBGC ( xhci, "XHCI %s-%d PORTLI %08x\n", xhci->name, port, portli );
518
519 /* Dump PORTHLPMC */
520 porthlpmc = readl ( xhci->op + XHCI_OP_PORTHLPMC ( port ) );
521 DBGC ( xhci, "XHCI %s-%d PORTHLPMC %08x\n",
522 xhci->name, port, porthlpmc );
523}
524
525/******************************************************************************
526 *
527 * USB legacy support
528 *
529 ******************************************************************************
530 */
531
532/** Prevent the release of ownership back to BIOS */
534
535/**
536 * Initialise USB legacy support
537 *
538 * @v xhci xHCI device
539 */
540static void xhci_legacy_init ( struct xhci_device *xhci ) {
541 unsigned int legacy;
542 uint8_t bios;
543
544 /* Locate USB legacy support capability (if present) */
545 legacy = xhci_extended_capability ( xhci, XHCI_XECP_ID_LEGACY, 0 );
546 if ( ! legacy ) {
547 /* Not an error; capability may not be present */
548 DBGC ( xhci, "XHCI %s has no USB legacy support capability\n",
549 xhci->name );
550 return;
551 }
552
553 /* Check if legacy USB support is enabled */
554 bios = readb ( xhci->cap + legacy + XHCI_USBLEGSUP_BIOS );
555 if ( ! ( bios & XHCI_USBLEGSUP_BIOS_OWNED ) ) {
556 /* Not an error; already owned by OS */
557 DBGC ( xhci, "XHCI %s USB legacy support already disabled\n",
558 xhci->name );
559 return;
560 }
561
562 /* Record presence of USB legacy support capability */
563 xhci->legacy = legacy;
564}
565
566/**
567 * Claim ownership from BIOS
568 *
569 * @v xhci xHCI device
570 */
571static void xhci_legacy_claim ( struct xhci_device *xhci ) {
572 uint32_t ctlsts;
573 uint8_t bios;
574 unsigned int i;
575
576 /* Do nothing unless legacy support capability is present */
577 if ( ! xhci->legacy )
578 return;
579
580 /* Claim ownership */
582 xhci->cap + xhci->legacy + XHCI_USBLEGSUP_OS );
583
584 /* Wait for BIOS to release ownership */
585 for ( i = 0 ; i < XHCI_USBLEGSUP_MAX_WAIT_MS ; i++ ) {
586
587 /* Check if BIOS has released ownership */
588 bios = readb ( xhci->cap + xhci->legacy + XHCI_USBLEGSUP_BIOS );
589 if ( ! ( bios & XHCI_USBLEGSUP_BIOS_OWNED ) ) {
590 DBGC ( xhci, "XHCI %s claimed ownership from BIOS\n",
591 xhci->name );
592 ctlsts = readl ( xhci->cap + xhci->legacy +
594 if ( ctlsts ) {
595 DBGC ( xhci, "XHCI %s warning: BIOS retained "
596 "SMIs: %08x\n", xhci->name, ctlsts );
597 }
598 return;
599 }
600
601 /* Delay */
602 mdelay ( 1 );
603 }
604
605 /* BIOS did not release ownership. Claim it forcibly by
606 * disabling all SMIs.
607 */
608 DBGC ( xhci, "XHCI %s could not claim ownership from BIOS: forcibly "
609 "disabling SMIs\n", xhci->name );
610 writel ( 0, xhci->cap + xhci->legacy + XHCI_USBLEGSUP_CTLSTS );
611}
612
613/**
614 * Release ownership back to BIOS
615 *
616 * @v xhci xHCI device
617 */
618static void xhci_legacy_release ( struct xhci_device *xhci ) {
619
620 /* Do nothing unless legacy support capability is present */
621 if ( ! xhci->legacy )
622 return;
623
624 /* Do nothing if releasing ownership is prevented */
626 DBGC ( xhci, "XHCI %s not releasing ownership to BIOS\n",
627 xhci->name );
628 return;
629 }
630
631 /* Release ownership */
632 writeb ( 0, xhci->cap + xhci->legacy + XHCI_USBLEGSUP_OS );
633 DBGC ( xhci, "XHCI %s released ownership to BIOS\n", xhci->name );
634}
635
636/******************************************************************************
637 *
638 * Supported protocols
639 *
640 ******************************************************************************
641 */
642
643/**
644 * Transcribe port speed (for debugging)
645 *
646 * @v psi Protocol speed ID
647 * @ret speed Transcribed speed
648 */
649static inline const char * xhci_speed_name ( uint32_t psi ) {
650 static const char *exponents[4] = { "", "k", "M", "G" };
651 static char buf[ 10 /* "xxxxxXbps" + NUL */ ];
652 unsigned int mantissa;
653 unsigned int exponent;
654
655 /* Extract mantissa and exponent */
656 mantissa = XHCI_SUPPORTED_PSI_MANTISSA ( psi );
657 exponent = XHCI_SUPPORTED_PSI_EXPONENT ( psi );
658
659 /* Transcribe speed */
660 snprintf ( buf, sizeof ( buf ), "%d%sbps",
661 mantissa, exponents[exponent] );
662 return buf;
663}
664
665/**
666 * Find supported protocol extended capability for a port
667 *
668 * @v xhci xHCI device
669 * @v port Port number
670 * @ret supported Offset to extended capability, or zero if not found
671 */
672static unsigned int xhci_supported_protocol ( struct xhci_device *xhci,
673 unsigned int port ) {
674 unsigned int supported = 0;
675 unsigned int offset;
676 unsigned int count;
677 uint32_t ports;
678
679 /* Iterate over all supported protocol structures */
680 while ( ( supported = xhci_extended_capability ( xhci,
682 supported ) ) ) {
683
684 /* Determine port range */
685 ports = readl ( xhci->cap + supported + XHCI_SUPPORTED_PORTS );
688
689 /* Check if port lies within this range */
690 if ( ( port - offset ) < count )
691 return supported;
692 }
693
694 DBGC ( xhci, "XHCI %s-%d has no supported protocol\n",
695 xhci->name, port );
696 return 0;
697}
698
699/**
700 * Find port protocol
701 *
702 * @v xhci xHCI device
703 * @v port Port number
704 * @ret protocol USB protocol, or zero if not found
705 */
706static unsigned int xhci_port_protocol ( struct xhci_device *xhci,
707 unsigned int port ) {
708 unsigned int supported = xhci_supported_protocol ( xhci, port );
709 union {
711 char text[5];
712 } name;
713 unsigned int protocol;
714 unsigned int type;
715 unsigned int psic;
716 unsigned int psiv;
717 unsigned int i;
719 uint32_t ports;
721 uint32_t psi;
722
723 /* Fail if there is no supported protocol */
724 if ( ! supported )
725 return 0;
726
727 /* Determine protocol version */
730
731 /* Describe port protocol */
732 if ( DBG_EXTRA ) {
733 name.raw = cpu_to_le32 ( readl ( xhci->cap + supported +
735 name.text[4] = '\0';
738 DBGC2 ( xhci, "XHCI %s-%d %sv%04x type %d",
739 xhci->name, port, name.text, protocol, type );
740 ports = readl ( xhci->cap + supported + XHCI_SUPPORTED_PORTS );
741 psic = XHCI_SUPPORTED_PORTS_PSIC ( ports );
742 if ( psic ) {
743 DBGC2 ( xhci, " speeds" );
744 for ( i = 0 ; i < psic ; i++ ) {
745 psi = readl ( xhci->cap + supported +
746 XHCI_SUPPORTED_PSI ( i ) );
747 psiv = XHCI_SUPPORTED_PSI_VALUE ( psi );
748 DBGC2 ( xhci, " %d:%s", psiv,
749 xhci_speed_name ( psi ) );
750 }
751 }
752 if ( xhci->quirks & XHCI_BAD_PSIV )
753 DBGC2 ( xhci, " (ignored)" );
754 DBGC2 ( xhci, "\n" );
755 }
756
757 return protocol;
758}
759
760/**
761 * Find port slot type
762 *
763 * @v xhci xHCI device
764 * @v port Port number
765 * @ret type Slot type, or negative error
766 */
767static int xhci_port_slot_type ( struct xhci_device *xhci, unsigned int port ) {
768 unsigned int supported = xhci_supported_protocol ( xhci, port );
769 unsigned int type;
771
772 /* Fail if there is no supported protocol */
773 if ( ! supported )
774 return -ENOTSUP;
775
776 /* Get slot type */
779
780 return type;
781}
782
783/**
784 * Find port speed
785 *
786 * @v xhci xHCI device
787 * @v port Port number
788 * @v psiv Protocol speed ID value
789 * @ret speed Port speed, or negative error
790 */
791static int xhci_port_speed ( struct xhci_device *xhci, unsigned int port,
792 unsigned int psiv ) {
793 unsigned int supported = xhci_supported_protocol ( xhci, port );
794 unsigned int psic;
795 unsigned int mantissa;
796 unsigned int exponent;
797 unsigned int speed;
798 unsigned int i;
799 uint32_t ports;
800 uint32_t psi;
801
802 /* Fail if there is no supported protocol */
803 if ( ! supported )
804 return -ENOTSUP;
805
806 /* Get protocol speed ID count */
807 ports = readl ( xhci->cap + supported + XHCI_SUPPORTED_PORTS );
808 psic = XHCI_SUPPORTED_PORTS_PSIC ( ports );
809
810 /* Use protocol speed ID table unless device is known to be faulty */
811 if ( ! ( xhci->quirks & XHCI_BAD_PSIV ) ) {
812
813 /* Iterate over PSI dwords looking for a match */
814 for ( i = 0 ; i < psic ; i++ ) {
815 psi = readl ( xhci->cap + supported +
816 XHCI_SUPPORTED_PSI ( i ) );
817 if ( psiv == XHCI_SUPPORTED_PSI_VALUE ( psi ) ) {
818 mantissa = XHCI_SUPPORTED_PSI_MANTISSA ( psi );
819 exponent = XHCI_SUPPORTED_PSI_EXPONENT ( psi );
820 speed = USB_SPEED ( mantissa, exponent );
821 return speed;
822 }
823 }
824
825 /* Record device as faulty if no match is found */
826 if ( psic != 0 ) {
827 DBGC ( xhci, "XHCI %s-%d spurious PSI value %d: "
828 "assuming PSI table is invalid\n",
829 xhci->name, port, psiv );
830 xhci->quirks |= XHCI_BAD_PSIV;
831 }
832 }
833
834 /* Use the default mappings */
835 switch ( psiv ) {
836 case XHCI_SPEED_LOW : return USB_SPEED_LOW;
837 case XHCI_SPEED_FULL : return USB_SPEED_FULL;
838 case XHCI_SPEED_HIGH : return USB_SPEED_HIGH;
839 case XHCI_SPEED_SUPER : return USB_SPEED_SUPER;
840 default:
841 DBGC ( xhci, "XHCI %s-%d unrecognised PSI value %d\n",
842 xhci->name, port, psiv );
843 return -ENOTSUP;
844 }
845}
846
847/**
848 * Find protocol speed ID value
849 *
850 * @v xhci xHCI device
851 * @v port Port number
852 * @v speed USB speed
853 * @ret psiv Protocol speed ID value, or negative error
854 */
855static int xhci_port_psiv ( struct xhci_device *xhci, unsigned int port,
856 unsigned int speed ) {
857 unsigned int supported = xhci_supported_protocol ( xhci, port );
858 unsigned int psic;
859 unsigned int mantissa;
860 unsigned int exponent;
861 unsigned int psiv;
862 unsigned int i;
863 uint32_t ports;
864 uint32_t psi;
865
866 /* Fail if there is no supported protocol */
867 if ( ! supported )
868 return -ENOTSUP;
869
870 /* Get protocol speed ID count */
871 ports = readl ( xhci->cap + supported + XHCI_SUPPORTED_PORTS );
872 psic = XHCI_SUPPORTED_PORTS_PSIC ( ports );
873
874 /* Use the default mappings if applicable */
875 if ( ( psic == 0 ) || ( xhci->quirks & XHCI_BAD_PSIV ) ) {
876 switch ( speed ) {
877 case USB_SPEED_LOW : return XHCI_SPEED_LOW;
878 case USB_SPEED_FULL : return XHCI_SPEED_FULL;
879 case USB_SPEED_HIGH : return XHCI_SPEED_HIGH;
880 case USB_SPEED_SUPER : return XHCI_SPEED_SUPER;
881 default:
882 DBGC ( xhci, "XHCI %s-%d non-standard speed %d\n",
883 xhci->name, port, speed );
884 return -ENOTSUP;
885 }
886 }
887
888 /* Iterate over PSI dwords looking for a match */
889 for ( i = 0 ; i < psic ; i++ ) {
890 psi = readl ( xhci->cap + supported + XHCI_SUPPORTED_PSI ( i ));
891 mantissa = XHCI_SUPPORTED_PSI_MANTISSA ( psi );
892 exponent = XHCI_SUPPORTED_PSI_EXPONENT ( psi );
893 if ( speed == USB_SPEED ( mantissa, exponent ) ) {
894 psiv = XHCI_SUPPORTED_PSI_VALUE ( psi );
895 return psiv;
896 }
897 }
898
899 DBGC ( xhci, "XHCI %s-%d unrepresentable speed %#x\n",
900 xhci->name, port, speed );
901 return -ENOENT;
902}
903
904/******************************************************************************
905 *
906 * Device context base address array
907 *
908 ******************************************************************************
909 */
910
911/**
912 * Allocate device context base address array
913 *
914 * @v xhci xHCI device
915 * @ret rc Return status code
916 */
917static int xhci_dcbaa_alloc ( struct xhci_device *xhci ) {
918 size_t len;
919 physaddr_t dcbaap;
920 int rc;
921
922 /* Allocate and initialise structure. Must be at least
923 * 64-byte aligned and must not cross a page boundary, so
924 * align on its own size (rounded up to a power of two and
925 * with a minimum of 64 bytes).
926 */
927 len = ( ( xhci->slots + 1 ) * sizeof ( xhci->dcbaa.context[0] ) );
928 xhci->dcbaa.context = dma_alloc ( xhci->dma, &xhci->dcbaa.map, len,
929 xhci_align ( len ) );
930 if ( ! xhci->dcbaa.context ) {
931 DBGC ( xhci, "XHCI %s could not allocate DCBAA\n", xhci->name );
932 rc = -ENOMEM;
933 goto err_alloc;
934 }
935 memset ( xhci->dcbaa.context, 0, len );
936
937 /* Program DCBAA pointer */
938 dcbaap = dma ( &xhci->dcbaa.map, xhci->dcbaa.context );
939 if ( ( rc = xhci_writeq ( xhci, dcbaap,
940 xhci->op + XHCI_OP_DCBAAP ) ) != 0 )
941 goto err_writeq;
942
943 DBGC2 ( xhci, "XHCI %s DCBAA at [%08lx,%08lx)\n", xhci->name,
944 virt_to_phys ( xhci->dcbaa.context ),
945 ( virt_to_phys ( xhci->dcbaa.context ) + len ) );
946 return 0;
947
948 err_writeq:
949 dma_free ( &xhci->dcbaa.map, xhci->dcbaa.context, len );
950 err_alloc:
951 return rc;
952}
953
954/**
955 * Free device context base address array
956 *
957 * @v xhci xHCI device
958 */
959static void xhci_dcbaa_free ( struct xhci_device *xhci ) {
960 size_t len;
961 unsigned int i;
962
963 /* Sanity check */
964 for ( i = 0 ; i <= xhci->slots ; i++ )
965 assert ( xhci->dcbaa.context[i] == 0 );
966
967 /* Clear DCBAA pointer */
968 xhci_writeq ( xhci, 0, xhci->op + XHCI_OP_DCBAAP );
969
970 /* Free DCBAA */
971 len = ( ( xhci->slots + 1 ) * sizeof ( xhci->dcbaa.context[0] ) );
972 dma_free ( &xhci->dcbaa.map, xhci->dcbaa.context, len );
973}
974
975/******************************************************************************
976 *
977 * Scratchpad buffers
978 *
979 ******************************************************************************
980 */
981
982/**
983 * Allocate scratchpad buffers
984 *
985 * @v xhci xHCI device
986 * @ret rc Return status code
987 */
988static int xhci_scratchpad_alloc ( struct xhci_device *xhci ) {
989 struct xhci_scratchpad *scratch = &xhci->scratch;
990 size_t buffer_len;
991 size_t array_len;
993 unsigned int i;
994 int rc;
995
996 /* Do nothing if no scratchpad buffers are used */
997 if ( ! scratch->count )
998 return 0;
999
1000 /* Allocate scratchpad buffers */
1001 buffer_len = ( scratch->count * xhci->pagesize );
1002 scratch->buffer = dma_umalloc ( xhci->dma, &scratch->buffer_map,
1003 buffer_len, xhci->pagesize );
1004 if ( ! scratch->buffer ) {
1005 DBGC ( xhci, "XHCI %s could not allocate scratchpad buffers\n",
1006 xhci->name );
1007 rc = -ENOMEM;
1008 goto err_alloc;
1009 }
1010 memset ( scratch->buffer, 0, buffer_len );
1011
1012 /* Allocate scratchpad array */
1013 array_len = ( scratch->count * sizeof ( scratch->array[0] ) );
1014 scratch->array = dma_alloc ( xhci->dma, &scratch->array_map,
1015 array_len, xhci_align ( array_len ) );
1016 if ( ! scratch->array ) {
1017 DBGC ( xhci, "XHCI %s could not allocate scratchpad buffer "
1018 "array\n", xhci->name );
1019 rc = -ENOMEM;
1020 goto err_alloc_array;
1021 }
1022
1023 /* Populate scratchpad array */
1024 addr = dma ( &scratch->buffer_map, scratch->buffer );
1025 for ( i = 0 ; i < scratch->count ; i++ ) {
1026 scratch->array[i] = cpu_to_le64 ( addr );
1027 addr += xhci->pagesize;
1028 }
1029
1030 /* Set scratchpad array pointer */
1031 assert ( xhci->dcbaa.context != NULL );
1032 xhci->dcbaa.context[0] = cpu_to_le64 ( dma ( &scratch->array_map,
1033 scratch->array ) );
1034
1035 DBGC2 ( xhci, "XHCI %s scratchpad [%08lx,%08lx) array [%08lx,%08lx)\n",
1036 xhci->name, virt_to_phys ( scratch->buffer ),
1037 ( virt_to_phys ( scratch->buffer ) + buffer_len ),
1038 virt_to_phys ( scratch->array ),
1039 ( virt_to_phys ( scratch->array ) + array_len ) );
1040 return 0;
1041
1042 dma_free ( &scratch->array_map, scratch->array, array_len );
1043 err_alloc_array:
1044 dma_ufree ( &scratch->buffer_map, scratch->buffer, buffer_len );
1045 err_alloc:
1046 return rc;
1047}
1048
1049/**
1050 * Free scratchpad buffers
1051 *
1052 * @v xhci xHCI device
1053 */
1054static void xhci_scratchpad_free ( struct xhci_device *xhci ) {
1055 struct xhci_scratchpad *scratch = &xhci->scratch;
1056 size_t array_len;
1057 size_t buffer_len;
1058
1059 /* Do nothing if no scratchpad buffers are used */
1060 if ( ! scratch->count )
1061 return;
1062
1063 /* Clear scratchpad array pointer */
1064 assert ( xhci->dcbaa.context != NULL );
1065 xhci->dcbaa.context[0] = 0;
1066
1067 /* Free scratchpad array */
1068 array_len = ( scratch->count * sizeof ( scratch->array[0] ) );
1069 dma_free ( &scratch->array_map, scratch->array, array_len );
1070
1071 /* Free scratchpad buffers */
1072 buffer_len = ( scratch->count * xhci->pagesize );
1073 dma_ufree ( &scratch->buffer_map, scratch->buffer, buffer_len );
1074}
1075
1076/******************************************************************************
1077 *
1078 * Run / stop / reset
1079 *
1080 ******************************************************************************
1081 */
1082
1083/**
1084 * Start xHCI device
1085 *
1086 * @v xhci xHCI device
1087 */
1088static void xhci_run ( struct xhci_device *xhci ) {
1089 uint32_t config;
1090 uint32_t usbcmd;
1091
1092 /* Configure number of device slots */
1093 config = readl ( xhci->op + XHCI_OP_CONFIG );
1095 config |= XHCI_CONFIG_MAX_SLOTS_EN ( xhci->slots );
1096 writel ( config, xhci->op + XHCI_OP_CONFIG );
1097
1098 /* Set run/stop bit */
1099 usbcmd = readl ( xhci->op + XHCI_OP_USBCMD );
1100 usbcmd |= XHCI_USBCMD_RUN;
1101 writel ( usbcmd, xhci->op + XHCI_OP_USBCMD );
1102}
1103
1104/**
1105 * Stop xHCI device
1106 *
1107 * @v xhci xHCI device
1108 * @ret rc Return status code
1109 */
1110static int xhci_stop ( struct xhci_device *xhci ) {
1111 uint32_t usbcmd;
1112 uint32_t usbsts;
1113 unsigned int i;
1114
1115 /* Clear run/stop bit */
1116 usbcmd = readl ( xhci->op + XHCI_OP_USBCMD );
1117 usbcmd &= ~XHCI_USBCMD_RUN;
1118 writel ( usbcmd, xhci->op + XHCI_OP_USBCMD );
1119
1120 /* Wait for device to stop */
1121 for ( i = 0 ; i < XHCI_STOP_MAX_WAIT_MS ; i++ ) {
1122
1123 /* Check if device is stopped */
1124 usbsts = readl ( xhci->op + XHCI_OP_USBSTS );
1125 if ( usbsts & XHCI_USBSTS_HCH )
1126 return 0;
1127
1128 /* Delay */
1129 mdelay ( 1 );
1130 }
1131
1132 DBGC ( xhci, "XHCI %s timed out waiting for stop\n", xhci->name );
1133 return -ETIMEDOUT;
1134}
1135
1136/**
1137 * Reset xHCI device
1138 *
1139 * @v xhci xHCI device
1140 * @ret rc Return status code
1141 */
1142static int xhci_reset ( struct xhci_device *xhci ) {
1143 uint32_t usbcmd;
1144 unsigned int i;
1145 int rc;
1146
1147 /* The xHCI specification states that resetting a running
1148 * device may result in undefined behaviour, so try stopping
1149 * it first.
1150 */
1151 if ( ( rc = xhci_stop ( xhci ) ) != 0 ) {
1152 /* Ignore errors and attempt to reset the device anyway */
1153 }
1154
1155 /* Reset device */
1157
1158 /* Wait for reset to complete */
1159 for ( i = 0 ; i < XHCI_RESET_MAX_WAIT_MS ; i++ ) {
1160
1161 /* Check if reset is complete */
1162 usbcmd = readl ( xhci->op + XHCI_OP_USBCMD );
1163 if ( ! ( usbcmd & XHCI_USBCMD_HCRST ) )
1164 return 0;
1165
1166 /* Delay */
1167 mdelay ( 1 );
1168 }
1169
1170 DBGC ( xhci, "XHCI %s timed out waiting for reset\n", xhci->name );
1171 return -ETIMEDOUT;
1172}
1173
1174/**
1175 * Mark xHCI device as permanently failed
1176 *
1177 * @v xhci xHCI device
1178 * @ret rc Return status code
1179 */
1180static int xhci_fail ( struct xhci_device *xhci ) {
1181 size_t len;
1182 int rc;
1183
1184 /* Mark command mechanism as permanently failed */
1185 xhci->failed = 1;
1186
1187 /* Reset device */
1188 if ( ( rc = xhci_reset ( xhci ) ) != 0 )
1189 return rc;
1190
1191 /* Discard DCBAA entries since DCBAAP has been cleared */
1192 assert ( xhci->dcbaa.context != NULL );
1193 len = ( ( xhci->slots + 1 ) * sizeof ( xhci->dcbaa.context[0] ) );
1194 memset ( xhci->dcbaa.context, 0, len );
1195
1196 return 0;
1197}
1198
1199/******************************************************************************
1200 *
1201 * Transfer request blocks
1202 *
1203 ******************************************************************************
1204 */
1205
1206/**
1207 * Allocate transfer request block ring
1208 *
1209 * @v xhci xHCI device
1210 * @v ring TRB ring
1211 * @v shift Ring size (log2)
1212 * @v slot Device slot
1213 * @v target Doorbell target
1214 * @v stream Doorbell stream ID
1215 * @ret rc Return status code
1216 */
1217static int xhci_ring_alloc ( struct xhci_device *xhci,
1218 struct xhci_trb_ring *ring,
1219 unsigned int shift, unsigned int slot,
1220 unsigned int target, unsigned int stream ) {
1221 struct xhci_trb_link *link;
1222 unsigned int count;
1223 int rc;
1224
1225 /* Sanity check */
1226 assert ( shift > 0 );
1227
1228 /* Initialise structure */
1229 memset ( ring, 0, sizeof ( *ring ) );
1230 ring->shift = shift;
1231 count = ( 1U << shift );
1232 ring->mask = ( count - 1 );
1233 ring->len = ( ( count + 1 /* Link TRB */ ) * sizeof ( ring->trb[0] ) );
1234 ring->db = ( xhci->db + ( slot * sizeof ( ring->dbval ) ) );
1235 ring->dbval = XHCI_DBVAL ( target, stream );
1236
1237 /* Allocate I/O buffers */
1238 ring->iobuf = zalloc ( count * sizeof ( ring->iobuf[0] ) );
1239 if ( ! ring->iobuf ) {
1240 rc = -ENOMEM;
1241 goto err_alloc_iobuf;
1242 }
1243
1244 /* Allocate TRBs */
1245 ring->trb = dma_alloc ( xhci->dma, &ring->map, ring->len,
1246 xhci_align ( ring->len ) );
1247 if ( ! ring->trb ) {
1248 rc = -ENOMEM;
1249 goto err_alloc_trb;
1250 }
1251 memset ( ring->trb, 0, ring->len );
1252
1253 /* Initialise Link TRB */
1254 link = &ring->trb[count].link;
1255 link->next = cpu_to_le64 ( dma ( &ring->map, ring->trb ) );
1256 link->flags = XHCI_TRB_TC;
1257 link->type = XHCI_TRB_LINK;
1258 ring->link = link;
1259
1260 return 0;
1261
1262 dma_free ( &ring->map, ring->trb, ring->len );
1263 err_alloc_trb:
1264 free ( ring->iobuf );
1265 err_alloc_iobuf:
1266 return rc;
1267}
1268
1269/**
1270 * Reset transfer request block ring
1271 *
1272 * @v ring TRB ring
1273 */
1274static void xhci_ring_reset ( struct xhci_trb_ring *ring ) {
1275 unsigned int count = ( 1U << ring->shift );
1276
1277 /* Reset producer and consumer counters */
1278 ring->prod = 0;
1279 ring->cons = 0;
1280
1281 /* Reset TRBs (except Link TRB) */
1282 memset ( ring->trb, 0, ( count * sizeof ( ring->trb[0] ) ) );
1283}
1284
1285/**
1286 * Free transfer request block ring
1287 *
1288 * @v ring TRB ring
1289 */
1290static void xhci_ring_free ( struct xhci_trb_ring *ring ) {
1291 unsigned int count = ( 1U << ring->shift );
1292 unsigned int i;
1293
1294 /* Sanity checks */
1295 assert ( ring->cons == ring->prod );
1296 for ( i = 0 ; i < count ; i++ )
1297 assert ( ring->iobuf[i] == NULL );
1298
1299 /* Free TRBs */
1300 dma_free ( &ring->map, ring->trb, ring->len );
1301
1302 /* Free I/O buffers */
1303 free ( ring->iobuf );
1304}
1305
1306/**
1307 * Enqueue a transfer request block
1308 *
1309 * @v ring TRB ring
1310 * @v iobuf I/O buffer (if any)
1311 * @v trb Transfer request block (with empty Cycle flag)
1312 * @ret rc Return status code
1313 *
1314 * This operation does not implicitly ring the doorbell register.
1315 */
1316static int xhci_enqueue ( struct xhci_trb_ring *ring, struct io_buffer *iobuf,
1317 const union xhci_trb *trb ) {
1318 union xhci_trb *dest;
1319 unsigned int prod;
1320 unsigned int mask;
1321 unsigned int index;
1322 unsigned int cycle;
1323
1324 /* Sanity check */
1325 assert ( ! ( trb->common.flags & XHCI_TRB_C ) );
1326
1327 /* Fail if ring is full */
1328 if ( ! xhci_ring_remaining ( ring ) )
1329 return -ENOBUFS;
1330
1331 /* Update producer counter (and link TRB, if applicable) */
1332 prod = ring->prod++;
1333 mask = ring->mask;
1334 cycle = ( ( ~( prod >> ring->shift ) ) & XHCI_TRB_C );
1335 index = ( prod & mask );
1336 if ( index == 0 )
1337 ring->link->flags = ( XHCI_TRB_TC | ( cycle ^ XHCI_TRB_C ) );
1338
1339 /* Record I/O buffer */
1340 ring->iobuf[index] = iobuf;
1341
1342 /* Enqueue TRB */
1343 dest = &ring->trb[index];
1344 dest->template.parameter = trb->template.parameter;
1345 dest->template.status = trb->template.status;
1346 wmb();
1347 dest->template.control = ( trb->template.control |
1348 cpu_to_le32 ( cycle ) );
1349
1350 return 0;
1351}
1352
1353/**
1354 * Dequeue a transfer request block
1355 *
1356 * @v ring TRB ring
1357 * @ret iobuf I/O buffer
1358 */
1359static struct io_buffer * xhci_dequeue ( struct xhci_trb_ring *ring ) {
1360 struct io_buffer *iobuf;
1361 unsigned int cons;
1362 unsigned int mask;
1363 unsigned int index;
1364
1365 /* Sanity check */
1366 assert ( xhci_ring_fill ( ring ) != 0 );
1367
1368 /* Update consumer counter */
1369 cons = ring->cons++;
1370 mask = ring->mask;
1371 index = ( cons & mask );
1372
1373 /* Retrieve I/O buffer */
1374 iobuf = ring->iobuf[index];
1375 ring->iobuf[index] = NULL;
1376
1377 return iobuf;
1378}
1379
1380/**
1381 * Enqueue multiple transfer request blocks
1382 *
1383 * @v ring TRB ring
1384 * @v iobuf I/O buffer
1385 * @v trbs Transfer request blocks (with empty Cycle flag)
1386 * @v count Number of transfer request blocks
1387 * @ret rc Return status code
1388 *
1389 * This operation does not implicitly ring the doorbell register.
1390 */
1391static int xhci_enqueue_multi ( struct xhci_trb_ring *ring,
1392 struct io_buffer *iobuf,
1393 const union xhci_trb *trbs,
1394 unsigned int count ) {
1395 const union xhci_trb *trb = trbs;
1396 int rc;
1397
1398 /* Sanity check */
1399 assert ( iobuf != NULL );
1400
1401 /* Fail if ring does not have sufficient space */
1402 if ( xhci_ring_remaining ( ring ) < count )
1403 return -ENOBUFS;
1404
1405 /* Enqueue each TRB, recording the I/O buffer with the final TRB */
1406 while ( count-- ) {
1407 rc = xhci_enqueue ( ring, ( count ? NULL : iobuf ), trb++ );
1408 assert ( rc == 0 ); /* Should never be able to fail */
1409 }
1410
1411 return 0;
1412}
1413
1414/**
1415 * Dequeue multiple transfer request blocks
1416 *
1417 * @v ring TRB ring
1418 * @ret iobuf I/O buffer
1419 */
1420static struct io_buffer * xhci_dequeue_multi ( struct xhci_trb_ring *ring ) {
1421 struct io_buffer *iobuf;
1422
1423 /* Dequeue TRBs until we reach the final TRB for an I/O buffer */
1424 do {
1425 iobuf = xhci_dequeue ( ring );
1426 } while ( iobuf == NULL );
1427
1428 return iobuf;
1429}
1430
1431/**
1432 * Ring doorbell register
1433 *
1434 * @v ring TRB ring
1435 */
1436static inline __attribute__ (( always_inline )) void
1438
1439 wmb();
1440 writel ( ring->dbval, ring->db );
1441}
1442
1443/******************************************************************************
1444 *
1445 * Command and event rings
1446 *
1447 ******************************************************************************
1448 */
1449
1450/**
1451 * Allocate command ring
1452 *
1453 * @v xhci xHCI device
1454 * @ret rc Return status code
1455 */
1456static int xhci_command_alloc ( struct xhci_device *xhci ) {
1457 physaddr_t crp;
1458 int rc;
1459
1460 /* Allocate TRB ring */
1461 if ( ( rc = xhci_ring_alloc ( xhci, &xhci->command, XHCI_CMD_TRBS_LOG2,
1462 0, 0, 0 ) ) != 0 )
1463 goto err_ring_alloc;
1464
1465 /* Program command ring control register */
1466 crp = dma ( &xhci->command.map, xhci->command.trb );
1467 if ( ( rc = xhci_writeq ( xhci, ( crp | XHCI_CRCR_RCS ),
1468 xhci->op + XHCI_OP_CRCR ) ) != 0 )
1469 goto err_writeq;
1470
1471 DBGC2 ( xhci, "XHCI %s CRCR at [%08lx,%08lx)\n", xhci->name,
1472 virt_to_phys ( xhci->command.trb ),
1473 ( virt_to_phys ( xhci->command.trb ) + xhci->command.len ) );
1474 return 0;
1475
1476 err_writeq:
1477 xhci_ring_free ( &xhci->command );
1478 err_ring_alloc:
1479 return rc;
1480}
1481
1482/**
1483 * Free command ring
1484 *
1485 * @v xhci xHCI device
1486 */
1487static void xhci_command_free ( struct xhci_device *xhci ) {
1488
1489 /* Sanity check */
1490 assert ( ( readl ( xhci->op + XHCI_OP_CRCR ) & XHCI_CRCR_CRR ) == 0 );
1491
1492 /* Clear command ring control register */
1493 xhci_writeq ( xhci, 0, xhci->op + XHCI_OP_CRCR );
1494
1495 /* Free TRB ring */
1496 xhci_ring_free ( &xhci->command );
1497}
1498
1499/**
1500 * Allocate event ring
1501 *
1502 * @v xhci xHCI device
1503 * @ret rc Return status code
1504 */
1505static int xhci_event_alloc ( struct xhci_device *xhci ) {
1506 struct xhci_event_ring *event = &xhci->event;
1507 unsigned int count;
1508 size_t len;
1509 int rc;
1510
1511 /* Allocate event ring */
1512 count = ( 1 << XHCI_EVENT_TRBS_LOG2 );
1513 len = ( count * sizeof ( event->trb[0] ) );
1514 event->trb = dma_alloc ( xhci->dma, &event->trb_map, len,
1515 xhci_align ( len ) );
1516 if ( ! event->trb ) {
1517 rc = -ENOMEM;
1518 goto err_alloc_trb;
1519 }
1520 memset ( event->trb, 0, len );
1521
1522 /* Allocate event ring segment table */
1523 event->segment = dma_alloc ( xhci->dma, &event->segment_map,
1524 sizeof ( event->segment[0] ),
1525 xhci_align ( sizeof (event->segment[0])));
1526 if ( ! event->segment ) {
1527 rc = -ENOMEM;
1528 goto err_alloc_segment;
1529 }
1530 memset ( event->segment, 0, sizeof ( event->segment[0] ) );
1531 event->segment[0].base = cpu_to_le64 ( dma ( &event->trb_map,
1532 event->trb ) );
1533 event->segment[0].count = cpu_to_le32 ( count );
1534
1535 /* Program event ring registers */
1536 writel ( 1, xhci->run + XHCI_RUN_ERSTSZ ( 0 ) );
1537 if ( ( rc = xhci_writeq ( xhci, dma ( &event->trb_map, event->trb ),
1538 xhci->run + XHCI_RUN_ERDP ( 0 ) ) ) != 0 )
1539 goto err_writeq_erdp;
1540 if ( ( rc = xhci_writeq ( xhci,
1541 dma ( &event->segment_map, event->segment ),
1542 xhci->run + XHCI_RUN_ERSTBA ( 0 ) ) ) != 0 )
1543 goto err_writeq_erstba;
1544
1545 DBGC2 ( xhci, "XHCI %s event ring [%08lx,%08lx) table [%08lx,%08lx)\n",
1546 xhci->name, virt_to_phys ( event->trb ),
1547 ( virt_to_phys ( event->trb ) + len ),
1548 virt_to_phys ( event->segment ),
1549 ( virt_to_phys ( event->segment ) +
1550 sizeof ( event->segment[0] ) ) );
1551 return 0;
1552
1553 xhci_writeq ( xhci, 0, xhci->run + XHCI_RUN_ERSTBA ( 0 ) );
1554 err_writeq_erstba:
1555 xhci_writeq ( xhci, 0, xhci->run + XHCI_RUN_ERDP ( 0 ) );
1556 err_writeq_erdp:
1557 dma_free ( &event->segment_map, event->segment,
1558 sizeof ( event->segment[0] ) );
1559 err_alloc_segment:
1560 dma_free ( &event->trb_map, event->trb, len );
1561 err_alloc_trb:
1562 return rc;
1563}
1564
1565/**
1566 * Free event ring
1567 *
1568 * @v xhci xHCI device
1569 */
1570static void xhci_event_free ( struct xhci_device *xhci ) {
1571 struct xhci_event_ring *event = &xhci->event;
1572 unsigned int count;
1573 size_t len;
1574
1575 /* Clear event ring registers */
1576 writel ( 0, xhci->run + XHCI_RUN_ERSTSZ ( 0 ) );
1577 xhci_writeq ( xhci, 0, xhci->run + XHCI_RUN_ERSTBA ( 0 ) );
1578 xhci_writeq ( xhci, 0, xhci->run + XHCI_RUN_ERDP ( 0 ) );
1579
1580 /* Free event ring segment table */
1581 dma_free ( &event->segment_map, event->segment,
1582 sizeof ( event->segment[0] ) );
1583
1584 /* Free event ring */
1585 count = ( 1 << XHCI_EVENT_TRBS_LOG2 );
1586 len = ( count * sizeof ( event->trb[0] ) );
1587 dma_free ( &event->trb_map, event->trb, len );
1588}
1589
1590/**
1591 * Handle transfer event
1592 *
1593 * @v xhci xHCI device
1594 * @v trb Transfer event TRB
1595 */
1596static void xhci_transfer ( struct xhci_device *xhci,
1597 struct xhci_trb_transfer *trb ) {
1598 struct xhci_slot *slot;
1599 struct xhci_endpoint *endpoint;
1600 struct io_buffer *iobuf;
1601 uint32_t cmplt;
1602 unsigned int code;
1603 int rc;
1604
1605 /* Profile transfer events */
1606 profile_start ( &xhci_transfer_profiler );
1607
1608 /* Identify slot */
1609 if ( ( trb->slot > xhci->slots ) ||
1610 ( ( slot = xhci->slot[trb->slot] ) == NULL ) ) {
1611 DBGC ( xhci, "XHCI %s transfer event invalid slot %d:\n",
1612 xhci->name, trb->slot );
1613 DBGC_HDA ( xhci, 0, trb, sizeof ( *trb ) );
1614 return;
1615 }
1616
1617 /* Identify endpoint */
1618 if ( ( trb->endpoint >= XHCI_CTX_END ) ||
1619 ( ( endpoint = slot->endpoint[trb->endpoint] ) == NULL ) ) {
1620 DBGC ( xhci, "XHCI %s slot %d transfer event invalid epid "
1621 "%d:\n", xhci->name, slot->id, trb->endpoint );
1622 DBGC_HDA ( xhci, 0, trb, sizeof ( *trb ) );
1623 return;
1624 }
1625
1626 /* Dequeue TRB(s) */
1627 iobuf = xhci_dequeue_multi ( &endpoint->ring );
1628 assert ( iobuf != NULL );
1629
1630 /* Unmap I/O buffer */
1631 iob_unmap ( iobuf );
1632
1633 /* Parse completion */
1634 cmplt = le32_to_cpu ( trb->cmplt );
1635 code = XHCI_CMPLT_CODE ( cmplt );
1636
1637 /* Check for errors */
1638 if ( ! ( ( code == XHCI_CMPLT_SUCCESS ) ||
1639 ( code == XHCI_CMPLT_SHORT ) ) ) {
1640
1641 /* Construct error */
1642 rc = -ECODE ( code );
1643 DBGC ( xhci, "XHCI %s slot %d ctx %d failed (code %d): %s\n",
1644 xhci->name, slot->id, endpoint->ctx, code,
1645 strerror ( rc ) );
1646 DBGC_HDA ( xhci, 0, trb, sizeof ( *trb ) );
1647
1648 /* Sanity check */
1649 assert ( ( endpoint->context->state & XHCI_ENDPOINT_STATE_MASK )
1651
1652 /* Report failure to USB core */
1653 usb_complete_err ( endpoint->ep, iobuf, rc );
1654 return;
1655 }
1656
1657 /* Record actual transfer size */
1658 iob_unput ( iobuf, XHCI_CMPLT_RESIDUAL ( cmplt ) );
1659
1660 /* Sanity check (for successful completions only) */
1661 assert ( xhci_ring_consumed ( &endpoint->ring ) ==
1662 le64_to_cpu ( trb->transfer ) );
1663
1664 /* Report completion to USB core */
1665 usb_complete ( endpoint->ep, iobuf );
1666 profile_stop ( &xhci_transfer_profiler );
1667}
1668
1669/**
1670 * Handle command completion event
1671 *
1672 * @v xhci xHCI device
1673 * @v trb Command completion event
1674 */
1675static void xhci_complete ( struct xhci_device *xhci,
1676 struct xhci_trb_complete *trb ) {
1677 int rc;
1678
1679 /* Ignore "command ring stopped" notifications */
1680 if ( trb->code == XHCI_CMPLT_CMD_STOPPED ) {
1681 DBGC2 ( xhci, "XHCI %s command ring stopped\n", xhci->name );
1682 return;
1683 }
1684
1685 /* Ignore unexpected completions */
1686 if ( ! xhci->pending ) {
1687 rc = -ECODE ( trb->code );
1688 DBGC ( xhci, "XHCI %s unexpected completion (code %d): %s\n",
1689 xhci->name, trb->code, strerror ( rc ) );
1690 DBGC_HDA ( xhci, 0, trb, sizeof ( *trb ) );
1691 return;
1692 }
1693
1694 /* Dequeue command TRB */
1695 xhci_dequeue ( &xhci->command );
1696
1697 /* Sanity check */
1698 assert ( xhci_ring_consumed ( &xhci->command ) ==
1699 le64_to_cpu ( trb->command ) );
1700
1701 /* Record completion */
1702 memcpy ( xhci->pending, trb, sizeof ( *xhci->pending ) );
1703 xhci->pending = NULL;
1704}
1705
1706/**
1707 * Handle port status event
1708 *
1709 * @v xhci xHCI device
1710 * @v trb Port status event
1711 */
1712static void xhci_port_status ( struct xhci_device *xhci,
1713 struct xhci_trb_port_status *trb ) {
1714 struct usb_port *port = usb_port ( xhci->bus->hub, trb->port );
1715 uint32_t portsc;
1716
1717 /* Sanity check */
1718 assert ( ( trb->port > 0 ) && ( trb->port <= xhci->ports ) );
1719
1720 /* Record disconnections and clear changes */
1721 portsc = readl ( xhci->op + XHCI_OP_PORTSC ( trb->port ) );
1722 port->disconnected |= ( portsc & XHCI_PORTSC_CSC );
1724 writel ( portsc, xhci->op + XHCI_OP_PORTSC ( trb->port ) );
1725
1726 /* Report port status change */
1728}
1729
1730/**
1731 * Handle host controller event
1732 *
1733 * @v xhci xHCI device
1734 * @v trb Host controller event
1735 */
1736static void xhci_host_controller ( struct xhci_device *xhci,
1737 struct xhci_trb_host_controller *trb ) {
1738 int rc;
1739
1740 /* Construct error */
1741 rc = -ECODE ( trb->code );
1742 DBGC ( xhci, "XHCI %s host controller event (code %d): %s\n",
1743 xhci->name, trb->code, strerror ( rc ) );
1744}
1745
1746/**
1747 * Poll event ring
1748 *
1749 * @v xhci xHCI device
1750 */
1751static void xhci_event_poll ( struct xhci_device *xhci ) {
1752 struct xhci_event_ring *event = &xhci->event;
1753 union xhci_trb *trb;
1754 unsigned int shift = XHCI_EVENT_TRBS_LOG2;
1755 unsigned int count = ( 1 << shift );
1756 unsigned int mask = ( count - 1 );
1757 unsigned int consumed;
1758 unsigned int type;
1759
1760 /* Do nothing if device has permanently failed */
1761 if ( xhci->failed )
1762 return;
1763
1764 /* Poll for events */
1765 profile_start ( &xhci_event_profiler );
1766 for ( consumed = 0 ; ; consumed++ ) {
1767
1768 /* Stop if we reach an empty TRB */
1769 rmb();
1770 trb = &event->trb[ event->cons & mask ];
1771 if ( ! ( ( trb->common.flags ^
1772 ( event->cons >> shift ) ) & XHCI_TRB_C ) )
1773 break;
1774
1775 /* Consume this TRB */
1776 event->cons++;
1777
1778 /* Handle TRB */
1779 type = ( trb->common.type & XHCI_TRB_TYPE_MASK );
1780 switch ( type ) {
1781
1782 case XHCI_TRB_TRANSFER :
1783 xhci_transfer ( xhci, &trb->transfer );
1784 break;
1785
1786 case XHCI_TRB_COMPLETE :
1787 xhci_complete ( xhci, &trb->complete );
1788 break;
1789
1791 xhci_port_status ( xhci, &trb->port );
1792 break;
1793
1795 xhci_host_controller ( xhci, &trb->host );
1796 break;
1797
1798 default:
1799 DBGC ( xhci, "XHCI %s unrecognised event %#x\n:",
1800 xhci->name, ( event->cons - 1 ) );
1801 DBGC_HDA ( xhci, virt_to_phys ( trb ),
1802 trb, sizeof ( *trb ) );
1803 break;
1804 }
1805 }
1806
1807 /* Update dequeue pointer if applicable */
1808 if ( consumed ) {
1809 xhci_writeq ( xhci, dma ( &event->trb_map, trb ),
1810 xhci->run + XHCI_RUN_ERDP ( 0 ) );
1811 profile_stop ( &xhci_event_profiler );
1812 }
1813}
1814
1815/**
1816 * Abort command
1817 *
1818 * @v xhci xHCI device
1819 */
1820static void xhci_abort ( struct xhci_device *xhci ) {
1821 physaddr_t crp;
1822 uint32_t crcr;
1823
1824 /* Abort the command */
1825 DBGC2 ( xhci, "XHCI %s aborting command\n", xhci->name );
1826 xhci_writeq ( xhci, XHCI_CRCR_CA, xhci->op + XHCI_OP_CRCR );
1827
1828 /* Allow time for command to abort */
1830
1831 /* Check for failure to abort */
1832 crcr = readl ( xhci->op + XHCI_OP_CRCR );
1833 if ( crcr & XHCI_CRCR_CRR ) {
1834
1835 /* Device has failed to abort a command and is almost
1836 * certainly beyond repair. Reset device, abandoning
1837 * all state, and mark device as failed to avoid
1838 * delays on any future command attempts.
1839 */
1840 DBGC ( xhci, "XHCI %s failed to abort command\n", xhci->name );
1841 xhci_fail ( xhci );
1842 }
1843
1844 /* Consume (and ignore) any final command status */
1845 xhci_event_poll ( xhci );
1846
1847 /* Reset the command ring control register */
1848 xhci_ring_reset ( &xhci->command );
1849 crp = dma ( &xhci->command.map, xhci->command.trb );
1850 xhci_writeq ( xhci, ( crp | XHCI_CRCR_RCS ), xhci->op + XHCI_OP_CRCR );
1851}
1852
1853/**
1854 * Issue command and wait for completion
1855 *
1856 * @v xhci xHCI device
1857 * @v trb Transfer request block (with empty Cycle flag)
1858 * @ret rc Return status code
1859 *
1860 * On a successful completion, the TRB will be overwritten with the
1861 * completion.
1862 */
1863static int xhci_command ( struct xhci_device *xhci, union xhci_trb *trb ) {
1864 struct xhci_trb_complete *complete = &trb->complete;
1865 unsigned int i;
1866 int rc;
1867
1868 /* Immediately fail all commands if command mechanism has failed */
1869 if ( xhci->failed ) {
1870 rc = -EPIPE;
1871 goto err_failed;
1872 }
1873
1874 /* Sanity check */
1875 if ( xhci->pending ) {
1876 DBGC ( xhci, "XHCI %s command ring busy\n", xhci->name );
1877 rc = -EBUSY;
1878 goto err_pending;
1879 }
1880
1881 /* Record the pending command */
1882 xhci->pending = trb;
1883
1884 /* Enqueue the command */
1885 if ( ( rc = xhci_enqueue ( &xhci->command, NULL, trb ) ) != 0 )
1886 goto err_enqueue;
1887
1888 /* Ring the command doorbell */
1889 xhci_doorbell ( &xhci->command );
1890
1891 /* Wait for the command to complete */
1892 for ( i = 0 ; i < XHCI_COMMAND_MAX_WAIT_MS ; i++ ) {
1893
1894 /* Poll event ring */
1895 xhci_event_poll ( xhci );
1896
1897 /* Check for completion */
1898 if ( ! xhci->pending ) {
1899 if ( complete->code != XHCI_CMPLT_SUCCESS ) {
1900 rc = -ECODE ( complete->code );
1901 DBGC ( xhci, "XHCI %s command failed (code "
1902 "%d): %s\n", xhci->name, complete->code,
1903 strerror ( rc ) );
1904 DBGC_HDA ( xhci, 0, trb, sizeof ( *trb ) );
1905 return rc;
1906 }
1907 return 0;
1908 }
1909
1910 /* Delay */
1911 mdelay ( 1 );
1912 }
1913
1914 /* Timeout */
1915 DBGC ( xhci, "XHCI %s timed out waiting for completion\n", xhci->name );
1916 rc = -ETIMEDOUT;
1917
1918 /* Abort command */
1919 xhci_abort ( xhci );
1920
1921 err_enqueue:
1922 xhci->pending = NULL;
1923 err_pending:
1924 err_failed:
1925 return rc;
1926}
1927
1928/**
1929 * Issue NOP and wait for completion
1930 *
1931 * @v xhci xHCI device
1932 * @ret rc Return status code
1933 */
1934static inline int xhci_nop ( struct xhci_device *xhci ) {
1935 union xhci_trb trb;
1936 struct xhci_trb_common *nop = &trb.common;
1937 int rc;
1938
1939 /* Construct command */
1940 memset ( nop, 0, sizeof ( *nop ) );
1941 nop->flags = XHCI_TRB_IOC;
1942 nop->type = XHCI_TRB_NOP_CMD;
1943
1944 /* Issue command and wait for completion */
1945 if ( ( rc = xhci_command ( xhci, &trb ) ) != 0 ) {
1946 DBGC ( xhci, "XHCI %s NOP failed: %s\n",
1947 xhci->name, strerror ( rc ) );
1948 return rc;
1949 }
1950
1951 DBGC2 ( xhci, "XHCI %s NOP completed successfully\n", xhci->name );
1952 return 0;
1953}
1954
1955/**
1956 * Enable slot
1957 *
1958 * @v xhci xHCI device
1959 * @v type Slot type
1960 * @ret slot Device slot ID, or negative error
1961 */
1962static inline int xhci_enable_slot ( struct xhci_device *xhci,
1963 unsigned int type ) {
1964 union xhci_trb trb;
1965 struct xhci_trb_enable_slot *enable = &trb.enable;
1966 struct xhci_trb_complete *enabled = &trb.complete;
1967 unsigned int slot;
1968 int rc;
1969
1970 /* Construct command */
1971 memset ( enable, 0, sizeof ( *enable ) );
1972 enable->slot = type;
1973 enable->type = XHCI_TRB_ENABLE_SLOT;
1974
1975 /* Issue command and wait for completion */
1976 if ( ( rc = xhci_command ( xhci, &trb ) ) != 0 ) {
1977 DBGC ( xhci, "XHCI %s could not enable new slot: %s\n",
1978 xhci->name, strerror ( rc ) );
1979 return rc;
1980 }
1981
1982 /* Extract slot number */
1983 slot = enabled->slot;
1984
1985 DBGC2 ( xhci, "XHCI %s slot %d enabled\n", xhci->name, slot );
1986 return slot;
1987}
1988
1989/**
1990 * Disable slot
1991 *
1992 * @v xhci xHCI device
1993 * @v slot Device slot
1994 * @ret rc Return status code
1995 */
1996static inline int xhci_disable_slot ( struct xhci_device *xhci,
1997 unsigned int slot ) {
1998 union xhci_trb trb;
1999 struct xhci_trb_disable_slot *disable = &trb.disable;
2000 int rc;
2001
2002 /* Construct command */
2003 memset ( disable, 0, sizeof ( *disable ) );
2004 disable->type = XHCI_TRB_DISABLE_SLOT;
2005 disable->slot = slot;
2006
2007 /* Issue command and wait for completion */
2008 if ( ( rc = xhci_command ( xhci, &trb ) ) != 0 ) {
2009 DBGC ( xhci, "XHCI %s could not disable slot %d: %s\n",
2010 xhci->name, slot, strerror ( rc ) );
2011 return rc;
2012 }
2013
2014 DBGC2 ( xhci, "XHCI %s slot %d disabled\n", xhci->name, slot );
2015 return 0;
2016}
2017
2018/**
2019 * Issue context-based command and wait for completion
2020 *
2021 * @v xhci xHCI device
2022 * @v slot Device slot
2023 * @v endpoint Endpoint
2024 * @v type TRB type
2025 * @v populate Input context populater
2026 * @ret rc Return status code
2027 */
2028static int xhci_context ( struct xhci_device *xhci, struct xhci_slot *slot,
2029 struct xhci_endpoint *endpoint, unsigned int type,
2030 void ( * populate ) ( struct xhci_device *xhci,
2031 struct xhci_slot *slot,
2032 struct xhci_endpoint *endpoint,
2033 void *input ) ) {
2034 union xhci_trb trb;
2035 struct xhci_trb_context *context = &trb.context;
2036 struct dma_mapping map;
2037 size_t len;
2038 void *input;
2039 int rc;
2040
2041 /* Allocate an input context */
2042 memset ( &map, 0, sizeof ( map ) );
2044 input = dma_alloc ( xhci->dma, &map, len, xhci_align ( len ) );
2045 if ( ! input ) {
2046 rc = -ENOMEM;
2047 goto err_alloc;
2048 }
2049 memset ( input, 0, len );
2050
2051 /* Populate input context */
2052 populate ( xhci, slot, endpoint, input );
2053
2054 /* Construct command */
2055 memset ( context, 0, sizeof ( *context ) );
2056 context->type = type;
2057 context->input = cpu_to_le64 ( dma ( &map, input ) );
2058 context->slot = slot->id;
2059
2060 /* Issue command and wait for completion */
2061 if ( ( rc = xhci_command ( xhci, &trb ) ) != 0 )
2062 goto err_command;
2063
2064 err_command:
2065 dma_free ( &map, input, len );
2066 err_alloc:
2067 return rc;
2068}
2069
2070/**
2071 * Populate address device input context
2072 *
2073 * @v xhci xHCI device
2074 * @v slot Device slot
2075 * @v endpoint Endpoint
2076 * @v input Input context
2077 */
2078static void xhci_address_device_input ( struct xhci_device *xhci,
2079 struct xhci_slot *slot,
2080 struct xhci_endpoint *endpoint,
2081 void *input ) {
2082 struct xhci_trb_ring *ring = &endpoint->ring;
2083 struct xhci_control_context *control_ctx;
2084 struct xhci_slot_context *slot_ctx;
2085 struct xhci_endpoint_context *ep_ctx;
2086
2087 /* Sanity checks */
2088 assert ( endpoint->ctx == XHCI_CTX_EP0 );
2089
2090 /* Populate control context */
2091 control_ctx = input;
2092 control_ctx->add = cpu_to_le32 ( ( 1 << XHCI_CTX_SLOT ) |
2093 ( 1 << XHCI_CTX_EP0 ) );
2094
2095 /* Populate slot context */
2096 slot_ctx = ( input + xhci_input_context_offset ( xhci, XHCI_CTX_SLOT ));
2097 slot_ctx->info = cpu_to_le32 ( XHCI_SLOT_INFO ( 1, 0, slot->psiv,
2098 slot->route ) );
2099 slot_ctx->port = slot->port;
2100 slot_ctx->tt_id = slot->tt_id;
2101 slot_ctx->tt_port = slot->tt_port;
2102
2103 /* Populate control endpoint context */
2104 ep_ctx = ( input + xhci_input_context_offset ( xhci, XHCI_CTX_EP0 ) );
2105 ep_ctx->type = XHCI_EP_TYPE_CONTROL;
2106 ep_ctx->burst = endpoint->ep->burst;
2107 ep_ctx->mtu = cpu_to_le16 ( endpoint->ep->mtu );
2108 ep_ctx->dequeue = cpu_to_le64 ( dma ( &ring->map, ring->trb ) |
2109 XHCI_EP_DCS );
2110 ep_ctx->trb_len = cpu_to_le16 ( XHCI_EP0_TRB_LEN );
2111}
2112
2113/**
2114 * Address device
2115 *
2116 * @v xhci xHCI device
2117 * @v slot Device slot
2118 * @ret rc Return status code
2119 */
2120static inline int xhci_address_device ( struct xhci_device *xhci,
2121 struct xhci_slot *slot ) {
2122 struct usb_device *usb = slot->usb;
2123 struct xhci_slot_context *slot_ctx;
2124 int rc;
2125
2126 /* Assign device address */
2127 if ( ( rc = xhci_context ( xhci, slot, slot->endpoint[XHCI_CTX_EP0],
2129 xhci_address_device_input ) ) != 0 ) {
2130 DBGC ( xhci, "XHCI %s slot %d could not assign address: %s\n",
2131 xhci->name, slot->id, strerror ( rc ) );
2132 return rc;
2133 }
2134
2135 /* Get assigned address */
2136 slot_ctx = ( slot->context +
2138 usb->address = slot_ctx->address;
2139 DBGC2 ( xhci, "XHCI %s slot %d assigned address %d to %s\n",
2140 xhci->name, slot->id, usb->address, usb->name );
2141
2142 return 0;
2143}
2144
2145/**
2146 * Populate configure endpoint input context
2147 *
2148 * @v xhci xHCI device
2149 * @v slot Device slot
2150 * @v endpoint Endpoint
2151 * @v input Input context
2152 */
2154 struct xhci_slot *slot,
2155 struct xhci_endpoint *endpoint,
2156 void *input ) {
2157 struct xhci_trb_ring *ring = &endpoint->ring;
2158 struct xhci_control_context *control_ctx;
2159 struct xhci_slot_context *slot_ctx;
2160 struct xhci_endpoint_context *ep_ctx;
2161
2162 /* Populate control context */
2163 control_ctx = input;
2164 control_ctx->add = cpu_to_le32 ( ( 1 << XHCI_CTX_SLOT ) |
2165 ( 1 << endpoint->ctx ) );
2166
2167 /* Populate slot context */
2168 slot_ctx = ( input + xhci_input_context_offset ( xhci, XHCI_CTX_SLOT ));
2169 slot_ctx->info = cpu_to_le32 ( XHCI_SLOT_INFO ( ( XHCI_CTX_END - 1U ),
2170 ( slot->ports ? 1 : 0 ),
2171 slot->psiv, 0 ) );
2172 slot_ctx->ports = slot->ports;
2173
2174 /* Populate endpoint context */
2175 ep_ctx = ( input + xhci_input_context_offset ( xhci, endpoint->ctx ) );
2176 ep_ctx->interval = endpoint->interval;
2177 ep_ctx->type = endpoint->type;
2178 ep_ctx->burst = endpoint->ep->burst;
2179 ep_ctx->mtu = cpu_to_le16 ( endpoint->ep->mtu );
2180 ep_ctx->dequeue = cpu_to_le64 ( dma ( &ring->map, ring->trb ) |
2181 XHCI_EP_DCS );
2182 ep_ctx->trb_len = cpu_to_le16 ( endpoint->ep->mtu ); /* best guess */
2183}
2184
2185/**
2186 * Configure endpoint
2187 *
2188 * @v xhci xHCI device
2189 * @v slot Device slot
2190 * @v endpoint Endpoint
2191 * @ret rc Return status code
2192 */
2193static inline int xhci_configure_endpoint ( struct xhci_device *xhci,
2194 struct xhci_slot *slot,
2195 struct xhci_endpoint *endpoint ) {
2196 int rc;
2197
2198 /* Configure endpoint */
2199 if ( ( rc = xhci_context ( xhci, slot, endpoint,
2202 DBGC ( xhci, "XHCI %s slot %d ctx %d could not configure: %s\n",
2203 xhci->name, slot->id, endpoint->ctx, strerror ( rc ) );
2204 return rc;
2205 }
2206
2207 DBGC2 ( xhci, "XHCI %s slot %d ctx %d configured\n",
2208 xhci->name, slot->id, endpoint->ctx );
2209 return 0;
2210}
2211
2212/**
2213 * Populate deconfigure endpoint input context
2214 *
2215 * @v xhci xHCI device
2216 * @v slot Device slot
2217 * @v endpoint Endpoint
2218 * @v input Input context
2219 */
2220static void
2222 struct xhci_slot *slot __unused,
2223 struct xhci_endpoint *endpoint,
2224 void *input ) {
2225 struct xhci_control_context *control_ctx;
2226 struct xhci_slot_context *slot_ctx;
2227
2228 /* Populate control context */
2229 control_ctx = input;
2230 control_ctx->add = cpu_to_le32 ( 1 << XHCI_CTX_SLOT );
2231 control_ctx->drop = cpu_to_le32 ( 1 << endpoint->ctx );
2232
2233 /* Populate slot context */
2234 slot_ctx = ( input + xhci_input_context_offset ( xhci, XHCI_CTX_SLOT ));
2235 slot_ctx->info = cpu_to_le32 ( XHCI_SLOT_INFO ( ( XHCI_CTX_END - 1U ),
2236 0, 0, 0 ) );
2237}
2238
2239/**
2240 * Deconfigure endpoint
2241 *
2242 * @v xhci xHCI device
2243 * @v slot Device slot
2244 * @v endpoint Endpoint
2245 * @ret rc Return status code
2246 */
2247static inline int xhci_deconfigure_endpoint ( struct xhci_device *xhci,
2248 struct xhci_slot *slot,
2249 struct xhci_endpoint *endpoint ) {
2250 int rc;
2251
2252 /* Deconfigure endpoint */
2253 if ( ( rc = xhci_context ( xhci, slot, endpoint,
2256 DBGC ( xhci, "XHCI %s slot %d ctx %d could not deconfigure: "
2257 "%s\n", xhci->name, slot->id, endpoint->ctx,
2258 strerror ( rc ) );
2259 return rc;
2260 }
2261
2262 DBGC2 ( xhci, "XHCI %s slot %d ctx %d deconfigured\n",
2263 xhci->name, slot->id, endpoint->ctx );
2264 return 0;
2265}
2266
2267/**
2268 * Populate evaluate context input context
2269 *
2270 * @v xhci xHCI device
2271 * @v slot Device slot
2272 * @v endpoint Endpoint
2273 * @v input Input context
2274 */
2275static void xhci_evaluate_context_input ( struct xhci_device *xhci,
2276 struct xhci_slot *slot __unused,
2277 struct xhci_endpoint *endpoint,
2278 void *input ) {
2279 struct xhci_control_context *control_ctx;
2280 struct xhci_slot_context *slot_ctx;
2281 struct xhci_endpoint_context *ep_ctx;
2282
2283 /* Populate control context */
2284 control_ctx = input;
2285 control_ctx->add = cpu_to_le32 ( ( 1 << XHCI_CTX_SLOT ) |
2286 ( 1 << endpoint->ctx ) );
2287
2288 /* Populate slot context */
2289 slot_ctx = ( input + xhci_input_context_offset ( xhci, XHCI_CTX_SLOT ));
2290 slot_ctx->info = cpu_to_le32 ( XHCI_SLOT_INFO ( ( XHCI_CTX_END - 1U ),
2291 0, 0, 0 ) );
2292
2293 /* Populate endpoint context */
2294 ep_ctx = ( input + xhci_input_context_offset ( xhci, endpoint->ctx ) );
2295 ep_ctx->mtu = cpu_to_le16 ( endpoint->ep->mtu );
2296}
2297
2298/**
2299 * Evaluate context
2300 *
2301 * @v xhci xHCI device
2302 * @v slot Device slot
2303 * @v endpoint Endpoint
2304 * @ret rc Return status code
2305 */
2306static inline int xhci_evaluate_context ( struct xhci_device *xhci,
2307 struct xhci_slot *slot,
2308 struct xhci_endpoint *endpoint ) {
2309 int rc;
2310
2311 /* Configure endpoint */
2312 if ( ( rc = xhci_context ( xhci, slot, endpoint,
2314 xhci_evaluate_context_input ) ) != 0 ) {
2315 DBGC ( xhci, "XHCI %s slot %d ctx %d could not (re-)evaluate: "
2316 "%s\n", xhci->name, slot->id, endpoint->ctx,
2317 strerror ( rc ) );
2318 return rc;
2319 }
2320
2321 DBGC2 ( xhci, "XHCI %s slot %d ctx %d (re-)evaluated\n",
2322 xhci->name, slot->id, endpoint->ctx );
2323 return 0;
2324}
2325
2326/**
2327 * Reset endpoint
2328 *
2329 * @v xhci xHCI device
2330 * @v slot Device slot
2331 * @v endpoint Endpoint
2332 * @ret rc Return status code
2333 */
2334static inline int xhci_reset_endpoint ( struct xhci_device *xhci,
2335 struct xhci_slot *slot,
2336 struct xhci_endpoint *endpoint ) {
2337 union xhci_trb trb;
2338 struct xhci_trb_reset_endpoint *reset = &trb.reset;
2339 int rc;
2340
2341 /* Construct command */
2342 memset ( reset, 0, sizeof ( *reset ) );
2343 reset->slot = slot->id;
2344 reset->endpoint = endpoint->ctx;
2346
2347 /* Issue command and wait for completion */
2348 if ( ( rc = xhci_command ( xhci, &trb ) ) != 0 ) {
2349 DBGC ( xhci, "XHCI %s slot %d ctx %d could not reset endpoint "
2350 "in state %d: %s\n", xhci->name, slot->id, endpoint->ctx,
2351 endpoint->context->state, strerror ( rc ) );
2352 return rc;
2353 }
2354
2355 return 0;
2356}
2357
2358/**
2359 * Stop endpoint
2360 *
2361 * @v xhci xHCI device
2362 * @v slot Device slot
2363 * @v endpoint Endpoint
2364 * @ret rc Return status code
2365 */
2366static inline int xhci_stop_endpoint ( struct xhci_device *xhci,
2367 struct xhci_slot *slot,
2368 struct xhci_endpoint *endpoint ) {
2369 union xhci_trb trb;
2370 struct xhci_trb_stop_endpoint *stop = &trb.stop;
2371 int rc;
2372
2373 /* Construct command */
2374 memset ( stop, 0, sizeof ( *stop ) );
2375 stop->slot = slot->id;
2376 stop->endpoint = endpoint->ctx;
2378
2379 /* Issue command and wait for completion */
2380 if ( ( rc = xhci_command ( xhci, &trb ) ) != 0 ) {
2381 DBGC ( xhci, "XHCI %s slot %d ctx %d could not stop endpoint "
2382 "in state %d: %s\n", xhci->name, slot->id, endpoint->ctx,
2383 endpoint->context->state, strerror ( rc ) );
2384 return rc;
2385 }
2386
2387 return 0;
2388}
2389
2390/**
2391 * Set transfer ring dequeue pointer
2392 *
2393 * @v xhci xHCI device
2394 * @v slot Device slot
2395 * @v endpoint Endpoint
2396 * @ret rc Return status code
2397 */
2398static inline int
2400 struct xhci_slot *slot,
2401 struct xhci_endpoint *endpoint ) {
2402 union xhci_trb trb;
2404 struct xhci_trb_ring *ring = &endpoint->ring;
2405 unsigned int cons;
2406 unsigned int mask;
2407 unsigned int index;
2408 unsigned int dcs;
2410 int rc;
2411
2412 /* Construct command */
2413 memset ( dequeue, 0, sizeof ( *dequeue ) );
2414 cons = ring->cons;
2415 mask = ring->mask;
2416 dcs = ( ( ~( cons >> ring->shift ) ) & XHCI_EP_DCS );
2417 index = ( cons & mask );
2418 addr = dma ( &ring->map, &ring->trb[index] );
2419 dequeue->dequeue = cpu_to_le64 ( addr | dcs );
2420 dequeue->slot = slot->id;
2421 dequeue->endpoint = endpoint->ctx;
2423
2424 /* Issue command and wait for completion */
2425 if ( ( rc = xhci_command ( xhci, &trb ) ) != 0 ) {
2426 DBGC ( xhci, "XHCI %s slot %d ctx %d could not set TR dequeue "
2427 "pointer in state %d: %s\n", xhci->name, slot->id,
2428 endpoint->ctx, endpoint->context->state, strerror ( rc));
2429 return rc;
2430 }
2431
2432 return 0;
2433}
2434
2435/******************************************************************************
2436 *
2437 * Endpoint operations
2438 *
2439 ******************************************************************************
2440 */
2441
2442/**
2443 * Open endpoint
2444 *
2445 * @v ep USB endpoint
2446 * @ret rc Return status code
2447 */
2448static int xhci_endpoint_open ( struct usb_endpoint *ep ) {
2449 struct usb_device *usb = ep->usb;
2450 struct xhci_slot *slot = usb_get_hostdata ( usb );
2451 struct xhci_device *xhci = slot->xhci;
2452 struct xhci_endpoint *endpoint;
2453 unsigned int ctx;
2454 unsigned int type;
2455 unsigned int interval;
2456 int rc;
2457
2458 /* Calculate context index */
2459 ctx = XHCI_CTX ( ep->address );
2460 assert ( slot->endpoint[ctx] == NULL );
2461
2462 /* Calculate endpoint type */
2466 if ( ep->address & USB_DIR_IN )
2468
2469 /* Calculate interval */
2470 if ( type & XHCI_EP_TYPE_PERIODIC ) {
2471 interval = ( fls ( ep->interval ) - 1 );
2472 } else {
2473 interval = ep->interval;
2474 }
2475
2476 /* Allocate and initialise structure */
2477 endpoint = zalloc ( sizeof ( *endpoint ) );
2478 if ( ! endpoint ) {
2479 rc = -ENOMEM;
2480 goto err_alloc;
2481 }
2482 usb_endpoint_set_hostdata ( ep, endpoint );
2483 slot->endpoint[ctx] = endpoint;
2484 endpoint->xhci = xhci;
2485 endpoint->slot = slot;
2486 endpoint->ep = ep;
2487 endpoint->ctx = ctx;
2488 endpoint->type = type;
2489 endpoint->interval = interval;
2490 endpoint->context = ( ( ( void * ) slot->context ) +
2492
2493 /* Allocate transfer ring */
2494 if ( ( rc = xhci_ring_alloc ( xhci, &endpoint->ring,
2496 slot->id, ctx, 0 ) ) != 0 )
2497 goto err_ring_alloc;
2498
2499 /* Configure endpoint, if applicable */
2500 if ( ( ctx != XHCI_CTX_EP0 ) &&
2501 ( ( rc = xhci_configure_endpoint ( xhci, slot, endpoint ) ) != 0 ))
2502 goto err_configure_endpoint;
2503
2504 DBGC2 ( xhci, "XHCI %s slot %d ctx %d ring [%08lx,%08lx)\n",
2505 xhci->name, slot->id, ctx, virt_to_phys ( endpoint->ring.trb ),
2506 ( virt_to_phys ( endpoint->ring.trb ) + endpoint->ring.len ) );
2507 return 0;
2508
2509 xhci_deconfigure_endpoint ( xhci, slot, endpoint );
2510 err_configure_endpoint:
2511 xhci_ring_free ( &endpoint->ring );
2512 err_ring_alloc:
2513 slot->endpoint[ctx] = NULL;
2514 free ( endpoint );
2515 err_alloc:
2516 return rc;
2517}
2518
2519/**
2520 * Close endpoint
2521 *
2522 * @v ep USB endpoint
2523 */
2524static void xhci_endpoint_close ( struct usb_endpoint *ep ) {
2525 struct xhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
2526 struct xhci_slot *slot = endpoint->slot;
2527 struct xhci_device *xhci = slot->xhci;
2528 struct io_buffer *iobuf;
2529 unsigned int ctx = endpoint->ctx;
2530
2531 /* Deconfigure endpoint, if applicable */
2532 if ( ctx != XHCI_CTX_EP0 )
2533 xhci_deconfigure_endpoint ( xhci, slot, endpoint );
2534
2535 /* Cancel any incomplete transfers */
2536 while ( xhci_ring_fill ( &endpoint->ring ) ) {
2537 iobuf = xhci_dequeue_multi ( &endpoint->ring );
2538 iob_unmap ( iobuf );
2539 usb_complete_err ( ep, iobuf, -ECANCELED );
2540 }
2541
2542 /* Free endpoint */
2543 xhci_ring_free ( &endpoint->ring );
2544 slot->endpoint[ctx] = NULL;
2545 free ( endpoint );
2546}
2547
2548/**
2549 * Reset endpoint
2550 *
2551 * @v ep USB endpoint
2552 * @ret rc Return status code
2553 */
2554static int xhci_endpoint_reset ( struct usb_endpoint *ep ) {
2555 struct xhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
2556 struct xhci_slot *slot = endpoint->slot;
2557 struct xhci_device *xhci = slot->xhci;
2558 int rc;
2559
2560 /* Reset endpoint context */
2561 if ( ( rc = xhci_reset_endpoint ( xhci, slot, endpoint ) ) != 0 )
2562 return rc;
2563
2564 /* Set transfer ring dequeue pointer */
2565 if ( ( rc = xhci_set_tr_dequeue_pointer ( xhci, slot, endpoint ) ) != 0)
2566 return rc;
2567
2568 /* Ring doorbell to resume processing */
2569 xhci_doorbell ( &endpoint->ring );
2570
2571 DBGC ( xhci, "XHCI %s slot %d ctx %d reset\n",
2572 xhci->name, slot->id, endpoint->ctx );
2573 return 0;
2574}
2575
2576/**
2577 * Update MTU
2578 *
2579 * @v ep USB endpoint
2580 * @ret rc Return status code
2581 */
2582static int xhci_endpoint_mtu ( struct usb_endpoint *ep ) {
2583 struct xhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
2584 struct xhci_slot *slot = endpoint->slot;
2585 struct xhci_device *xhci = slot->xhci;
2586 int rc;
2587
2588 /* Evalulate context */
2589 if ( ( rc = xhci_evaluate_context ( xhci, slot, endpoint ) ) != 0 )
2590 return rc;
2591
2592 return 0;
2593}
2594
2595/**
2596 * Enqueue message transfer
2597 *
2598 * @v ep USB endpoint
2599 * @v iobuf I/O buffer
2600 * @ret rc Return status code
2601 */
2602static int xhci_endpoint_message ( struct usb_endpoint *ep,
2603 struct io_buffer *iobuf ) {
2604 struct xhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
2605 struct xhci_device *xhci = endpoint->xhci;
2606 struct usb_setup_packet *packet;
2607 unsigned int input;
2608 size_t len;
2609 union xhci_trb trbs[ 1 /* setup */ + 1 /* possible data */ +
2610 1 /* status */ ];
2611 union xhci_trb *trb = trbs;
2612 struct xhci_trb_setup *setup;
2613 struct xhci_trb_data *data;
2614 struct xhci_trb_status *status;
2615 int rc;
2616
2617 /* Profile message transfers */
2618 profile_start ( &xhci_message_profiler );
2619
2620 /* Construct setup stage TRB */
2621 memset ( trbs, 0, sizeof ( trbs ) );
2622 assert ( iob_len ( iobuf ) >= sizeof ( *packet ) );
2623 packet = iobuf->data;
2624 iob_pull ( iobuf, sizeof ( *packet ) );
2625 setup = &(trb++)->setup;
2626 memcpy ( &setup->packet, packet, sizeof ( setup->packet ) );
2627 setup->len = cpu_to_le32 ( sizeof ( *packet ) );
2628 setup->flags = XHCI_TRB_IDT;
2629 setup->type = XHCI_TRB_SETUP;
2630 len = iob_len ( iobuf );
2631 input = ( packet->request & cpu_to_le16 ( USB_DIR_IN ) );
2632 if ( len )
2634
2635 /* Map I/O buffer */
2636 if ( ( rc = iob_map ( iobuf, xhci->dma, len,
2637 ( input ? DMA_RX : DMA_TX ) ) ) != 0 )
2638 goto err_map;
2639
2640 /* Construct data stage TRB, if applicable */
2641 if ( len ) {
2642 data = &(trb++)->data;
2643 data->data = cpu_to_le64 ( iob_dma ( iobuf ) );
2644 data->len = cpu_to_le32 ( len );
2645 data->type = XHCI_TRB_DATA;
2646 data->direction = ( input ? XHCI_DATA_IN : XHCI_DATA_OUT );
2647 }
2648
2649 /* Construct status stage TRB */
2650 status = &(trb++)->status;
2651 status->flags = XHCI_TRB_IOC;
2652 status->type = XHCI_TRB_STATUS;
2653 status->direction =
2654 ( ( len && input ) ? XHCI_STATUS_OUT : XHCI_STATUS_IN );
2655
2656 /* Enqueue TRBs */
2657 if ( ( rc = xhci_enqueue_multi ( &endpoint->ring, iobuf, trbs,
2658 ( trb - trbs ) ) ) != 0 )
2659 goto err_enqueue;
2660
2661 /* Ring the doorbell */
2662 xhci_doorbell ( &endpoint->ring );
2663
2664 profile_stop ( &xhci_message_profiler );
2665 return 0;
2666
2667 err_enqueue:
2668 iob_unmap ( iobuf );
2669 err_map:
2670 return rc;
2671}
2672
2673/**
2674 * Calculate number of TRBs
2675 *
2676 * @v len Length of data
2677 * @v zlp Append a zero-length packet
2678 * @ret count Number of transfer descriptors
2679 */
2680static unsigned int xhci_endpoint_count ( size_t len, int zlp ) {
2681 unsigned int count;
2682
2683 /* Split into 64kB TRBs */
2684 count = ( ( len + XHCI_MTU - 1 ) / XHCI_MTU );
2685
2686 /* Append a zero-length TRB if applicable */
2687 if ( zlp || ( count == 0 ) )
2688 count++;
2689
2690 return count;
2691}
2692
2693/**
2694 * Enqueue stream transfer
2695 *
2696 * @v ep USB endpoint
2697 * @v iobuf I/O buffer
2698 * @v zlp Append a zero-length packet
2699 * @ret rc Return status code
2700 */
2701static int xhci_endpoint_stream ( struct usb_endpoint *ep,
2702 struct io_buffer *iobuf, int zlp ) {
2703 struct xhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
2704 struct xhci_device *xhci = endpoint->xhci;
2705 size_t len = iob_len ( iobuf );
2706 unsigned int count = xhci_endpoint_count ( len, zlp );
2707 union xhci_trb trbs[count];
2708 union xhci_trb *trb = trbs;
2709 struct xhci_trb_normal *normal;
2711 unsigned int i;
2712 size_t trb_len;
2713 int rc;
2714
2715 /* Profile stream transfers */
2716 profile_start ( &xhci_stream_profiler );
2717
2718 /* Map I/O buffer */
2719 if ( ( rc = iob_map ( iobuf, xhci->dma, len,
2720 ( ( ep->address & USB_DIR_IN ) ?
2721 DMA_RX : DMA_TX ) ) ) != 0 )
2722 goto err_map;
2723 data = iob_dma ( iobuf );
2724
2725 /* Construct normal TRBs */
2726 memset ( &trbs, 0, sizeof ( trbs ) );
2727 for ( i = 0 ; i < count ; i ++ ) {
2728
2729 /* Calculate TRB length */
2730 trb_len = XHCI_MTU;
2731 if ( trb_len > len )
2732 trb_len = len;
2733
2734 /* Construct normal TRB */
2735 normal = &trb->normal;
2736 normal->data = cpu_to_le64 ( data );
2737 normal->len = cpu_to_le32 ( trb_len );
2738 normal->type = XHCI_TRB_NORMAL;
2739 normal->flags = XHCI_TRB_CH;
2740
2741 /* Move to next TRB */
2742 data += trb_len;
2743 len -= trb_len;
2744 trb++;
2745 }
2746
2747 /* Mark zero-length packet (if present) as a separate transfer */
2748 if ( zlp && ( count > 1 ) )
2749 trb[-2].normal.flags = 0;
2750
2751 /* Generate completion for final TRB */
2752 trb[-1].normal.flags = XHCI_TRB_IOC;
2753
2754 /* Enqueue TRBs */
2755 if ( ( rc = xhci_enqueue_multi ( &endpoint->ring, iobuf, trbs,
2756 count ) ) != 0 )
2757 goto err_enqueue;
2758
2759 /* Ring the doorbell */
2760 xhci_doorbell ( &endpoint->ring );
2761
2762 profile_stop ( &xhci_stream_profiler );
2763 return 0;
2764
2765 err_enqueue:
2766 iob_unmap ( iobuf );
2767 err_map:
2768 return rc;
2769}
2770
2771/******************************************************************************
2772 *
2773 * Device operations
2774 *
2775 ******************************************************************************
2776 */
2777
2778/**
2779 * Open device
2780 *
2781 * @v usb USB device
2782 * @ret rc Return status code
2783 */
2784static int xhci_device_open ( struct usb_device *usb ) {
2785 struct xhci_device *xhci = usb_bus_get_hostdata ( usb->port->hub->bus );
2786 struct usb_port *root_port = usb_root_hub_port ( usb );
2787 struct usb_port *tt = usb_transaction_translator ( usb );
2788 struct xhci_slot *slot;
2789 struct xhci_slot *tt_slot;
2790 size_t len;
2791 int type;
2792 int id;
2793 int rc;
2794
2795 /* Determine applicable slot type */
2796 type = xhci_port_slot_type ( xhci, root_port->address );
2797 if ( type < 0 ) {
2798 rc = type;
2799 DBGC ( xhci, "XHCI %s-%d has no slot type\n",
2800 xhci->name, usb->port->address );
2801 goto err_type;
2802 }
2803
2804 /* Allocate a device slot number */
2805 id = xhci_enable_slot ( xhci, type );
2806 if ( id < 0 ) {
2807 rc = id;
2808 goto err_enable_slot;
2809 }
2810 assert ( ( id > 0 ) && ( ( unsigned int ) id <= xhci->slots ) );
2811 assert ( xhci->slot[id] == NULL );
2812
2813 /* Allocate and initialise structure */
2814 slot = zalloc ( sizeof ( *slot ) );
2815 if ( ! slot ) {
2816 rc = -ENOMEM;
2817 goto err_alloc;
2818 }
2820 xhci->slot[id] = slot;
2821 slot->xhci = xhci;
2822 slot->usb = usb;
2823 slot->id = id;
2824 if ( tt ) {
2825 tt_slot = usb_get_hostdata ( tt->hub->usb );
2826 slot->tt_id = tt_slot->id;
2827 slot->tt_port = tt->address;
2828 }
2829
2830 /* Allocate a device context */
2832 slot->context = dma_alloc ( xhci->dma, &slot->map, len,
2833 xhci_align ( len ) );
2834 if ( ! slot->context ) {
2835 rc = -ENOMEM;
2836 goto err_alloc_context;
2837 }
2838 memset ( slot->context, 0, len );
2839
2840 /* Set device context base address */
2841 assert ( xhci->dcbaa.context[id] == 0 );
2842 xhci->dcbaa.context[id] = cpu_to_le64 ( dma ( &slot->map,
2843 slot->context ) );
2844
2845 DBGC2 ( xhci, "XHCI %s slot %d device context [%08lx,%08lx) for %s\n",
2846 xhci->name, slot->id, virt_to_phys ( slot->context ),
2847 ( virt_to_phys ( slot->context ) + len ), usb->name );
2848 return 0;
2849
2850 xhci->dcbaa.context[id] = 0;
2851 dma_free ( &slot->map, slot->context, len );
2852 err_alloc_context:
2853 xhci->slot[id] = NULL;
2854 free ( slot );
2855 err_alloc:
2856 xhci_disable_slot ( xhci, id );
2857 err_enable_slot:
2858 err_type:
2859 return rc;
2860}
2861
2862/**
2863 * Close device
2864 *
2865 * @v usb USB device
2866 */
2867static void xhci_device_close ( struct usb_device *usb ) {
2868 struct xhci_slot *slot = usb_get_hostdata ( usb );
2869 struct xhci_device *xhci = slot->xhci;
2870 size_t len = xhci_device_context_offset ( xhci, XHCI_CTX_END );
2871 unsigned int id = slot->id;
2872 int rc;
2873
2874 /* Disable slot */
2875 if ( ( rc = xhci_disable_slot ( xhci, id ) ) != 0 ) {
2876 /* Slot is still enabled. Leak the slot context,
2877 * since the controller may still write to this
2878 * memory, and leave the DCBAA entry intact.
2879 *
2880 * If the controller later reports that this same slot
2881 * has been re-enabled, then some assertions will be
2882 * triggered.
2883 */
2884 DBGC ( xhci, "XHCI %s slot %d leaking context memory\n",
2885 xhci->name, slot->id );
2886 slot->context = NULL;
2887 }
2888
2889 /* Free slot */
2890 if ( slot->context ) {
2891 dma_free ( &slot->map, slot->context, len );
2892 xhci->dcbaa.context[id] = 0;
2893 }
2894 xhci->slot[id] = NULL;
2895 free ( slot );
2896}
2897
2898/**
2899 * Assign device address
2900 *
2901 * @v usb USB device
2902 * @ret rc Return status code
2903 */
2904static int xhci_device_address ( struct usb_device *usb ) {
2905 struct xhci_slot *slot = usb_get_hostdata ( usb );
2906 struct xhci_device *xhci = slot->xhci;
2907 struct usb_port *root_port;
2908 int psiv;
2909 int rc;
2910
2911 /* Calculate route string */
2912 slot->route = usb_route_string ( usb );
2913
2914 /* Calculate root hub port number */
2915 root_port = usb_root_hub_port ( usb );
2916 slot->port = root_port->address;
2917
2918 /* Calculate protocol speed ID */
2919 psiv = xhci_port_psiv ( xhci, slot->port, usb->speed );
2920 if ( psiv < 0 ) {
2921 rc = psiv;
2922 return rc;
2923 }
2924 slot->psiv = psiv;
2925
2926 /* Address device */
2927 if ( ( rc = xhci_address_device ( xhci, slot ) ) != 0 )
2928 return rc;
2929
2930 return 0;
2931}
2932
2933/******************************************************************************
2934 *
2935 * Bus operations
2936 *
2937 ******************************************************************************
2938 */
2939
2940/**
2941 * Open USB bus
2942 *
2943 * @v bus USB bus
2944 * @ret rc Return status code
2945 */
2946static int xhci_bus_open ( struct usb_bus *bus ) {
2947 struct xhci_device *xhci = usb_bus_get_hostdata ( bus );
2948 int rc;
2949
2950 /* Allocate device slot array */
2951 xhci->slot = zalloc ( ( xhci->slots + 1 ) * sizeof ( xhci->slot[0] ) );
2952 if ( ! xhci->slot ) {
2953 rc = -ENOMEM;
2954 goto err_slot_alloc;
2955 }
2956
2957 /* Allocate device context base address array */
2958 if ( ( rc = xhci_dcbaa_alloc ( xhci ) ) != 0 )
2959 goto err_dcbaa_alloc;
2960
2961 /* Allocate scratchpad buffers */
2962 if ( ( rc = xhci_scratchpad_alloc ( xhci ) ) != 0 )
2963 goto err_scratchpad_alloc;
2964
2965 /* Allocate command ring */
2966 if ( ( rc = xhci_command_alloc ( xhci ) ) != 0 )
2967 goto err_command_alloc;
2968
2969 /* Allocate event ring */
2970 if ( ( rc = xhci_event_alloc ( xhci ) ) != 0 )
2971 goto err_event_alloc;
2972
2973 /* Start controller */
2974 xhci_run ( xhci );
2975
2976 return 0;
2977
2978 xhci_stop ( xhci );
2979 xhci_event_free ( xhci );
2980 err_event_alloc:
2981 xhci_command_free ( xhci );
2982 err_command_alloc:
2983 xhci_scratchpad_free ( xhci );
2984 err_scratchpad_alloc:
2985 xhci_dcbaa_free ( xhci );
2986 err_dcbaa_alloc:
2987 free ( xhci->slot );
2988 err_slot_alloc:
2989 return rc;
2990}
2991
2992/**
2993 * Close USB bus
2994 *
2995 * @v bus USB bus
2996 */
2997static void xhci_bus_close ( struct usb_bus *bus ) {
2998 struct xhci_device *xhci = usb_bus_get_hostdata ( bus );
2999 unsigned int i;
3000
3001 /* Sanity checks */
3002 assert ( xhci->slot != NULL );
3003 for ( i = 0 ; i <= xhci->slots ; i++ )
3004 assert ( xhci->slot[i] == NULL );
3005
3006 xhci_stop ( xhci );
3007 xhci_event_free ( xhci );
3008 xhci_command_free ( xhci );
3009 xhci_scratchpad_free ( xhci );
3010 xhci_dcbaa_free ( xhci );
3011 free ( xhci->slot );
3012}
3013
3014/**
3015 * Poll USB bus
3016 *
3017 * @v bus USB bus
3018 */
3019static void xhci_bus_poll ( struct usb_bus *bus ) {
3020 struct xhci_device *xhci = usb_bus_get_hostdata ( bus );
3021
3022 /* Poll event ring */
3023 xhci_event_poll ( xhci );
3024}
3025
3026/******************************************************************************
3027 *
3028 * Hub operations
3029 *
3030 ******************************************************************************
3031 */
3032
3033/**
3034 * Open hub
3035 *
3036 * @v hub USB hub
3037 * @ret rc Return status code
3038 */
3039static int xhci_hub_open ( struct usb_hub *hub ) {
3040 struct xhci_slot *slot;
3041
3042 /* Do nothing if this is the root hub */
3043 if ( ! hub->usb )
3044 return 0;
3045
3046 /* Get device slot */
3047 slot = usb_get_hostdata ( hub->usb );
3048
3049 /* Update device slot hub parameters. We don't inform the
3050 * hardware of this information until the hub's interrupt
3051 * endpoint is opened, since the only mechanism for so doing
3052 * provided by the xHCI specification is a Configure Endpoint
3053 * command, and we can't issue that command until we have a
3054 * non-EP0 endpoint to configure.
3055 */
3056 slot->ports = hub->ports;
3057
3058 return 0;
3059}
3060
3061/**
3062 * Close hub
3063 *
3064 * @v hub USB hub
3065 */
3066static void xhci_hub_close ( struct usb_hub *hub __unused ) {
3067
3068 /* Nothing to do */
3069}
3070
3071/******************************************************************************
3072 *
3073 * Root hub operations
3074 *
3075 ******************************************************************************
3076 */
3077
3078/**
3079 * Open root hub
3080 *
3081 * @v hub USB hub
3082 * @ret rc Return status code
3083 */
3084static int xhci_root_open ( struct usb_hub *hub ) {
3085 struct xhci_device *xhci = usb_hub_get_drvdata ( hub );
3086 struct usb_port *port;
3087 uint32_t portsc;
3088 unsigned int i;
3089
3090 /* Enable power to all ports */
3091 for ( i = 1 ; i <= xhci->ports ; i++ ) {
3092 portsc = readl ( xhci->op + XHCI_OP_PORTSC ( i ) );
3093 portsc &= XHCI_PORTSC_PRESERVE;
3094 portsc |= XHCI_PORTSC_PP;
3095 writel ( portsc, xhci->op + XHCI_OP_PORTSC ( i ) );
3096 }
3097
3098 /* xHCI spec requires us to potentially wait 20ms after
3099 * enabling power to a port.
3100 */
3102
3103 /* USB3 ports may power up as Disabled */
3104 for ( i = 1 ; i <= xhci->ports ; i++ ) {
3105 portsc = readl ( xhci->op + XHCI_OP_PORTSC ( i ) );
3106 port = usb_port ( hub, i );
3107 if ( ( port->protocol >= USB_PROTO_3_0 ) &&
3108 ( ( portsc & XHCI_PORTSC_PLS_MASK ) ==
3110 /* Force link state to RxDetect */
3111 portsc &= XHCI_PORTSC_PRESERVE;
3113 writel ( portsc, xhci->op + XHCI_OP_PORTSC ( i ) );
3114 }
3115 }
3116
3117 /* Some xHCI cards seem to require an additional delay after
3118 * setting the link state to RxDetect.
3119 */
3121
3122 return 0;
3123}
3124
3125/**
3126 * Close root hub
3127 *
3128 * @v hub USB hub
3129 */
3130static void xhci_root_close ( struct usb_hub *hub __unused ) {
3131
3132 /* Nothing to do */
3133}
3134
3135/**
3136 * Enable port
3137 *
3138 * @v hub USB hub
3139 * @v port USB port
3140 * @ret rc Return status code
3141 */
3142static int xhci_root_enable ( struct usb_hub *hub, struct usb_port *port ) {
3143 struct xhci_device *xhci = usb_hub_get_drvdata ( hub );
3144 uint32_t portsc;
3145 unsigned int i;
3146
3147 /* Reset port */
3148 portsc = readl ( xhci->op + XHCI_OP_PORTSC ( port->address ) );
3149 portsc &= XHCI_PORTSC_PRESERVE;
3150 portsc |= XHCI_PORTSC_PR;
3151 writel ( portsc, xhci->op + XHCI_OP_PORTSC ( port->address ) );
3152
3153 /* Wait for port to become enabled */
3154 for ( i = 0 ; i < XHCI_PORT_RESET_MAX_WAIT_MS ; i++ ) {
3155
3156 /* Check port status */
3157 portsc = readl ( xhci->op + XHCI_OP_PORTSC ( port->address ) );
3158 if ( portsc & XHCI_PORTSC_PED )
3159 return 0;
3160
3161 /* Delay */
3162 mdelay ( 1 );
3163 }
3164
3165 DBGC ( xhci, "XHCI %s-%d timed out waiting for port to enable\n",
3166 xhci->name, port->address );
3167 return -ETIMEDOUT;
3168}
3169
3170/**
3171 * Disable port
3172 *
3173 * @v hub USB hub
3174 * @v port USB port
3175 * @ret rc Return status code
3176 */
3177static int xhci_root_disable ( struct usb_hub *hub, struct usb_port *port ) {
3178 struct xhci_device *xhci = usb_hub_get_drvdata ( hub );
3179 uint32_t portsc;
3180
3181 /* Disable port */
3182 portsc = readl ( xhci->op + XHCI_OP_PORTSC ( port->address ) );
3183 portsc &= XHCI_PORTSC_PRESERVE;
3184 portsc |= XHCI_PORTSC_PED;
3185 writel ( portsc, xhci->op + XHCI_OP_PORTSC ( port->address ) );
3186
3187 /* Allow time for link state to stabilise */
3189
3190 /* Set link state to RxDetect for USB3 ports */
3191 if ( port->protocol >= USB_PROTO_3_0 ) {
3192 portsc &= XHCI_PORTSC_PRESERVE;
3194 writel ( portsc, xhci->op + XHCI_OP_PORTSC ( port->address ) );
3195 }
3196
3197 /* Allow time for link state to stabilise */
3199
3200 return 0;
3201}
3202
3203/**
3204 * Update root hub port speed
3205 *
3206 * @v hub USB hub
3207 * @v port USB port
3208 * @ret rc Return status code
3209 */
3210static int xhci_root_speed ( struct usb_hub *hub, struct usb_port *port ) {
3211 struct xhci_device *xhci = usb_hub_get_drvdata ( hub );
3212 uint32_t portsc;
3213 unsigned int psiv;
3214 int ccs;
3215 int ped;
3216 int csc;
3217 int speed;
3218 int rc;
3219
3220 /* Read port status */
3221 portsc = readl ( xhci->op + XHCI_OP_PORTSC ( port->address ) );
3222 DBGC2 ( xhci, "XHCI %s-%d status is %08x\n",
3223 xhci->name, port->address, portsc );
3224 ccs = ( portsc & XHCI_PORTSC_CCS );
3225 ped = ( portsc & XHCI_PORTSC_PED );
3226 csc = ( portsc & XHCI_PORTSC_CSC );
3227 psiv = XHCI_PORTSC_PSIV ( portsc );
3228
3229 /* Record disconnections and clear changes */
3230 port->disconnected |= csc;
3232 writel ( portsc, xhci->op + XHCI_OP_PORTSC ( port->address ) );
3233
3234 /* Port speed is not valid unless port is connected */
3235 if ( ! ccs ) {
3236 port->speed = USB_SPEED_NONE;
3237 return 0;
3238 }
3239
3240 /* For USB2 ports, the PSIV field is not valid until the port
3241 * completes reset and becomes enabled.
3242 */
3243 if ( ( port->protocol < USB_PROTO_3_0 ) && ! ped ) {
3244 port->speed = USB_SPEED_FULL;
3245 return 0;
3246 }
3247
3248 /* Get port speed and map to generic USB speed */
3249 speed = xhci_port_speed ( xhci, port->address, psiv );
3250 if ( speed < 0 ) {
3251 rc = speed;
3252 return rc;
3253 }
3254
3255 port->speed = speed;
3256 return 0;
3257}
3258
3259/**
3260 * Clear transaction translator buffer
3261 *
3262 * @v hub USB hub
3263 * @v port USB port
3264 * @v ep USB endpoint
3265 * @ret rc Return status code
3266 */
3267static int xhci_root_clear_tt ( struct usb_hub *hub, struct usb_port *port,
3268 struct usb_endpoint *ep ) {
3269 struct xhci_device *xhci = usb_hub_get_drvdata ( hub );
3270
3271 /* Should never be called; this is a root hub */
3272 DBGC ( xhci, "XHCI %s-%d nonsensical CLEAR_TT for %s %s\n", xhci->name,
3273 port->address, ep->usb->name, usb_endpoint_name ( ep ) );
3274
3275 return -ENOTSUP;
3276}
3277
3278/******************************************************************************
3279 *
3280 * Hardware-independent interface
3281 *
3282 ******************************************************************************
3283 */
3284
3285/** USB host controller operations */
3287 .endpoint = {
3288 .open = xhci_endpoint_open,
3289 .close = xhci_endpoint_close,
3290 .reset = xhci_endpoint_reset,
3291 .mtu = xhci_endpoint_mtu,
3292 .message = xhci_endpoint_message,
3293 .stream = xhci_endpoint_stream,
3294 },
3295 .device = {
3296 .open = xhci_device_open,
3297 .close = xhci_device_close,
3298 .address = xhci_device_address,
3299 },
3300 .bus = {
3301 .open = xhci_bus_open,
3302 .close = xhci_bus_close,
3303 .poll = xhci_bus_poll,
3304 },
3305 .hub = {
3306 .open = xhci_hub_open,
3307 .close = xhci_hub_close,
3308 },
3309 .root = {
3310 .open = xhci_root_open,
3311 .close = xhci_root_close,
3312 .enable = xhci_root_enable,
3313 .disable = xhci_root_disable,
3314 .speed = xhci_root_speed,
3315 .clear_tt = xhci_root_clear_tt,
3316 },
3317};
3318
3319/**
3320 * Register xHCI controller
3321 *
3322 * @v xhci xHCI device
3323 * @ret rc Return status code
3324 */
3325int xhci_register ( struct xhci_device *xhci ) {
3326 struct usb_port *port;
3327 unsigned int i;
3328 int rc;
3329
3330 /* Reset device */
3331 if ( ( rc = xhci_reset ( xhci ) ) != 0 )
3332 goto err_reset;
3333
3334 /* Allocate USB bus */
3335 xhci->bus = alloc_usb_bus ( xhci->dev, xhci->ports, XHCI_MTU,
3336 &xhci_operations );
3337 if ( ! xhci->bus ) {
3338 rc = -ENOMEM;
3339 goto err_alloc_bus;
3340 }
3341 usb_bus_set_hostdata ( xhci->bus, xhci );
3342 usb_hub_set_drvdata ( xhci->bus->hub, xhci );
3343
3344 /* Set port protocols */
3345 for ( i = 1 ; i <= xhci->ports ; i++ ) {
3346 port = usb_port ( xhci->bus->hub, i );
3347 port->protocol = xhci_port_protocol ( xhci, i );
3348 }
3349
3350 /* Register USB bus */
3351 if ( ( rc = register_usb_bus ( xhci->bus ) ) != 0 )
3352 goto err_register;
3353
3354 return 0;
3355
3356 unregister_usb_bus ( xhci->bus );
3357 err_register:
3358 free_usb_bus ( xhci->bus );
3359 err_alloc_bus:
3360 xhci_reset ( xhci );
3361 err_reset:
3362 return rc;
3363}
3364
3365/**
3366 * Unregister xHCI controller
3367 *
3368 * @v xhci xHCI device
3369 */
3370void xhci_unregister ( struct xhci_device *xhci ) {
3371 struct usb_bus *bus = xhci->bus;
3372
3373 /* Unregister and free USB bus */
3375 free_usb_bus ( bus );
3376
3377 /* Reset device */
3378 xhci_reset ( xhci );
3379}
3380
3381/******************************************************************************
3382 *
3383 * PCI interface
3384 *
3385 ******************************************************************************
3386 */
3387
3388/**
3389 * Fix Intel PCH-specific quirks
3390 *
3391 * @v xhci xHCI device
3392 * @v pci PCI device
3393 */
3394static void xhci_pch_fix ( struct xhci_device *xhci, struct pci_device *pci ) {
3395 struct xhci_pch *pch = &xhci->pch;
3397 uint32_t xusb2prm;
3399 uint32_t usb3prm;
3400
3401 /* Enable SuperSpeed capability. Do this before rerouting
3402 * USB2 ports, so that USB3 devices connect at SuperSpeed.
3403 */
3405 pci_read_config_dword ( pci, XHCI_PCH_USB3PRM, &usb3prm );
3406 if ( usb3prm & ~usb3pssen ) {
3407 DBGC ( xhci, "XHCI %s enabling SuperSpeed on ports %08x\n",
3408 xhci->name, ( usb3prm & ~usb3pssen ) );
3409 }
3410 pch->usb3pssen = usb3pssen;
3411 usb3pssen |= usb3prm;
3413
3414 /* Route USB2 ports from EHCI to xHCI */
3416 pci_read_config_dword ( pci, XHCI_PCH_XUSB2PRM, &xusb2prm );
3417 if ( xusb2prm & ~xusb2pr ) {
3418 DBGC ( xhci, "XHCI %s routing ports %08x from EHCI to xHCI\n",
3419 xhci->name, ( xusb2prm & ~xusb2pr ) );
3420 }
3421 pch->xusb2pr = xusb2pr;
3422 xusb2pr |= xusb2prm;
3424}
3425
3426/**
3427 * Undo Intel PCH-specific quirk fixes
3428 *
3429 * @v xhci xHCI device
3430 * @v pci PCI device
3431 */
3432static void xhci_pch_undo ( struct xhci_device *xhci, struct pci_device *pci ) {
3433 struct xhci_pch *pch = &xhci->pch;
3434
3435 /* Restore USB2 port routing to original state */
3437
3438 /* Restore SuperSpeed capability to original state */
3440}
3441
3442/**
3443 * Probe PCI device
3444 *
3445 * @v pci PCI device
3446 * @ret rc Return status code
3447 */
3448static int xhci_probe ( struct pci_device *pci ) {
3449 struct xhci_device *xhci;
3450 unsigned long bar_start;
3451 size_t bar_size;
3452 int rc;
3453
3454 /* Allocate and initialise structure */
3455 xhci = zalloc ( sizeof ( *xhci ) );
3456 if ( ! xhci ) {
3457 rc = -ENOMEM;
3458 goto err_alloc;
3459 }
3460 xhci->dev = &pci->dev;
3461 xhci->dma = &pci->dma;
3462 xhci->quirks = pci->id->driver_data;
3463
3464 /* Fix up PCI device */
3465 adjust_pci_device ( pci );
3466
3467 /* Map registers */
3468 bar_start = pci_bar_start ( pci, XHCI_BAR );
3469 bar_size = pci_bar_size ( pci, XHCI_BAR );
3470 xhci->regs = pci_ioremap ( pci, bar_start, bar_size );
3471 if ( ! xhci->regs ) {
3472 rc = -ENODEV;
3473 goto err_ioremap;
3474 }
3475
3476 /* Initialise xHCI device */
3477 xhci_init ( xhci );
3478
3479 /* Initialise USB legacy support and claim ownership */
3480 xhci_legacy_init ( xhci );
3481 xhci_legacy_claim ( xhci );
3482
3483 /* Fix Intel PCH-specific quirks, if applicable */
3484 if ( xhci->quirks & XHCI_PCH )
3485 xhci_pch_fix ( xhci, pci );
3486
3487 /* Register xHCI device */
3488 if ( ( rc = xhci_register ( xhci ) ) != 0 )
3489 goto err_register;
3490
3491 pci_set_drvdata ( pci, xhci );
3492 return 0;
3493
3494 xhci_unregister ( xhci );
3495 err_register:
3496 if ( xhci->quirks & XHCI_PCH )
3497 xhci_pch_undo ( xhci, pci );
3498 xhci_legacy_release ( xhci );
3499 iounmap ( xhci->regs );
3500 err_ioremap:
3501 free ( xhci );
3502 err_alloc:
3503 return rc;
3504}
3505
3506/**
3507 * Remove PCI device
3508 *
3509 * @v pci PCI device
3510 */
3511static void xhci_remove ( struct pci_device *pci ) {
3512 struct xhci_device *xhci = pci_get_drvdata ( pci );
3514
3515 /* Some systems are observed to disable bus mastering on
3516 * Thunderbolt controllers before we get a chance to shut
3517 * down. Detect this and avoid attempting any DMA operations,
3518 * which are guaranteed to fail and may end up spuriously
3519 * completing after the operating system kernel starts up.
3520 */
3522 if ( ! ( command & PCI_COMMAND_MASTER ) ) {
3523 DBGC ( xhci, "XHCI %s DMA was disabled\n", xhci->name );
3524 xhci_fail ( xhci );
3525 }
3526
3527 /* Unregister xHCI controller */
3528 xhci_unregister ( xhci );
3529
3530 /* Undo any PCH-specific fixes */
3531 if ( xhci->quirks & XHCI_PCH )
3532 xhci_pch_undo ( xhci, pci );
3533
3534 /* Release ownership back to BIOS */
3535 xhci_legacy_release ( xhci );
3536
3537 /* Unmap registers */
3538 iounmap ( xhci->regs );
3539
3540 /* Free device */
3541 free ( xhci );
3542}
3543
3544/** XHCI PCI device IDs */
3545static struct pci_device_id xhci_ids[] = {
3546 PCI_ROM ( 0x8086, 0x9d2f, "xhci-skylake", "xHCI (Skylake)", ( XHCI_PCH | XHCI_BAD_PSIV ) ),
3547 PCI_ROM ( 0x8086, 0xffff, "xhci-pch", "xHCI (Intel PCH)", XHCI_PCH ),
3548 PCI_ROM ( 0xffff, 0xffff, "xhci", "xHCI", 0 ),
3549};
3550
3551/** XHCI PCI driver */
3552struct pci_driver xhci_driver __pci_driver = {
3553 .ids = xhci_ids,
3554 .id_count = ( sizeof ( xhci_ids ) / sizeof ( xhci_ids[0] ) ),
3557 .probe = xhci_probe,
3558 .remove = xhci_remove,
3559};
3560
3561/**
3562 * Prepare for exit
3563 *
3564 * @v booting System is shutting down for OS boot
3565 */
3566static void xhci_shutdown ( int booting ) {
3567 /* If we are shutting down to boot an OS, then prevent the
3568 * release of ownership back to BIOS.
3569 */
3571}
3572
3573/** Startup/shutdown function */
3574struct startup_fn xhci_startup __startup_fn ( STARTUP_LATE ) = {
3575 .name = "xhci",
3576 .shutdown = xhci_shutdown,
3577};
#define NULL
NULL pointer (VOID *).
Definition Base.h:321
struct golan_eq_context ctx
Definition CIB_PRM.h:0
u8 port
Port number.
Definition CIB_PRM.h:3
__be32 raw[7]
Definition CIB_PRM.h:0
#define PCI_CLASS_SERIAL
Definition Pci22.h:266
#define PCI_CLASS_SERIAL_USB
Definition Pci22.h:272
u32 link
Link to next descriptor.
Definition ar9003_mac.h:1
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
pseudo_bit_t value[0x00020]
Definition arbel.h:2
struct arbelprm_completion_queue_entry normal
Definition arbel.h:0
static unsigned int code
Definition hyperv.h:26
unsigned short uint16_t
Definition stdint.h:11
unsigned int uint32_t
Definition stdint.h:12
unsigned long physaddr_t
Definition stdint.h:20
unsigned char uint8_t
Definition stdint.h:10
long index
Definition bigint.h:30
if(len >=6 *4) __asm__ __volatile__("movsl" if(len >=5 *4) __asm__ __volatile__("movsl" if(len >=4 *4) __asm__ __volatile__("movsl" if(len >=3 *4) __asm__ __volatile__("movsl" if(len >=2 *4) __asm__ __volatile__("movsl" if(len >=1 *4) __asm__ __volatile__("movsl" if((len % 4) >=2) __asm__ __volatile__("movsw" if((len % 2) >=1) __asm__ __volatile__("movsb" retur dest)
Definition string.h:151
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:61
const char * name
Definition ath9k_hw.c:1986
uint16_t offset
Offset to command line.
Definition bzimage.h:3
uint32_t next
Next descriptor address.
Definition dwmac.h:11
ring len
Length.
Definition dwmac.h:226
uint32_t addr
Buffer address.
Definition dwmac.h:9
uint8_t bus
Bus.
Definition edd.h:1
uint8_t slot
Slot.
Definition edd.h:3
uint16_t cons
Consumer index.
Definition ena.h:11
uint8_t id
Request identifier.
Definition ena.h:1
uint32_t type
Operating system type.
Definition ena.h:1
uint8_t data[48]
Additional event data.
Definition ena.h:11
uint16_t supported
Bitmask of supported option values.
Definition ena.h:1
uint16_t enabled
Single-entry bitmask of the enabled option value.
Definition ena.h:3
uint8_t status
Status.
Definition ena.h:5
Error codes.
#define __unused
Declare a variable or data structure as unused.
Definition compiler.h:598
#define DBGC2(...)
Definition compiler.h:547
#define DBG_EXTRA
Definition compiler.h:344
#define DBGC(...)
Definition compiler.h:530
#define DBG_LOG
Definition compiler.h:342
#define DBGC_HDA(...)
Definition compiler.h:531
static unsigned int count
Number of entries.
Definition dwmac.h:220
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:921
#define ENOENT
No such file or directory.
Definition errno.h:558
#define ETIMEDOUT
Connection timed out.
Definition errno.h:713
#define EPIPE
Broken pipe.
Definition errno.h:663
#define ENOMEM
Not enough space.
Definition errno.h:578
#define EBUSY
Device or resource busy.
Definition errno.h:382
#define ENOTSUP
Operation not supported.
Definition errno.h:633
#define ECANCELED
Operation canceled.
Definition errno.h:387
#define ENOBUFS
No buffer space available.
Definition errno.h:542
#define ENODEV
No such device.
Definition errno.h:553
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:951
#define STARTUP_LATE
Late startup.
Definition init.h:66
uint32_t revision
Entry point revision.
Definition ib_mad.h:9
#define cpu_to_le64(value)
Definition byteswap.h:109
#define le64_to_cpu(value)
Definition byteswap.h:115
#define le32_to_cpu(value)
Definition byteswap.h:114
#define cpu_to_le32(value)
Definition byteswap.h:108
#define cpu_to_le16(value)
Definition byteswap.h:107
#define __attribute__(x)
Definition compiler.h:10
#define rmb()
Definition io.h:545
#define wmb()
Definition io.h:546
#define writeq(data, io_addr)
Definition io.h:273
void iounmap(volatile const void *io_addr)
Unmap I/O address.
int pci_read_config_dword(struct pci_device *pci, unsigned int where, uint32_t *value)
Read 32-bit dword from PCI configuration space.
int pci_read_config_word(struct pci_device *pci, unsigned int where, uint16_t *value)
Read 16-bit word from PCI configuration space.
void * pci_ioremap(struct pci_device *pci, unsigned long bus_addr, size_t len)
Map PCI bus address as an I/O address.
int pci_write_config_dword(struct pci_device *pci, unsigned int where, uint32_t value)
Write 32-bit dword to PCI configuration space.
Profiling.
#define __profiler
Declare a profiler.
Definition profile.h:61
static void profile_stop(struct profiler *profiler)
Stop profiling.
Definition profile.h:174
static void profile_start(struct profiler *profiler)
Start profiling.
Definition profile.h:161
Universal Serial Bus (USB).
@ USB_PROTO_3_0
USB 3.0.
Definition usb.h:25
static void * usb_hub_get_drvdata(struct usb_hub *hub)
Get USB hub driver private data.
Definition usb.h:958
#define USB_DIR_IN
Data transfer is from device to host.
Definition usb.h:98
static struct usb_port * usb_port(struct usb_hub *hub, unsigned int address)
Get USB port.
Definition usb.h:970
#define USB_SPEED(mantissa, exponent)
Define a USB speed.
Definition usb.h:36
static void * usb_endpoint_get_hostdata(struct usb_endpoint *ep)
Get USB endpoint host controller private data.
Definition usb.h:597
static void usb_complete(struct usb_endpoint *ep, struct io_buffer *iobuf)
Complete transfer (without error).
Definition usb.h:1097
static void * usb_get_hostdata(struct usb_device *usb)
Get USB device host controller private data.
Definition usb.h:805
static void usb_endpoint_set_hostdata(struct usb_endpoint *ep, void *priv)
Set USB endpoint host controller private data.
Definition usb.h:586
static void * usb_bus_get_hostdata(struct usb_bus *bus)
Get USB bus host controller private data.
Definition usb.h:1072
#define USB_ENDPOINT_ATTR_TYPE_MASK
Endpoint attribute transfer type mask.
Definition usb.h:281
@ USB_SPEED_LOW
Low speed (1.5Mbps).
Definition usb.h:49
@ USB_SPEED_FULL
Full speed (12Mbps).
Definition usb.h:51
@ USB_SPEED_HIGH
High speed (480Mbps).
Definition usb.h:53
@ USB_SPEED_NONE
Not connected.
Definition usb.h:47
@ USB_SPEED_SUPER
Super speed (5Gbps).
Definition usb.h:55
#define USB_ENDPOINT_ATTR_CONTROL
Control endpoint transfer type.
Definition usb.h:287
static void usb_set_hostdata(struct usb_device *usb, void *priv)
Set USB device host controller private data.
Definition usb.h:794
static void usb_hub_set_drvdata(struct usb_hub *hub, void *priv)
Set USB hub driver private data.
Definition usb.h:947
static void usb_bus_set_hostdata(struct usb_bus *bus, void *priv)
Set USB bus host controller private data.
Definition usb.h:1061
String functions.
void * memcpy(void *dest, const void *src, size_t len) __nonnull
void * memset(void *dest, int character, size_t len) __nonnull
String functions.
#define fls(x)
Find last (i.e.
Definition strings.h:167
#define __startup_fn(startup_order)
Declare a startup/shutdown function.
Definition init.h:53
static __always_inline void iob_unmap(struct io_buffer *iobuf)
Unmap I/O buffer for DMA.
Definition iobuf.h:339
static __always_inline physaddr_t iob_dma(struct io_buffer *iobuf)
Get I/O buffer DMA address.
Definition iobuf.h:328
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition iobuf.h:220
#define iob_pull(iobuf, len)
Definition iobuf.h:167
#define iob_unput(iobuf, len)
Definition iobuf.h:200
static __always_inline int iob_map(struct io_buffer *iobuf, struct dma_device *dma, size_t len, int flags)
Map I/O buffer for DMA.
Definition iobuf.h:291
static __always_inline void dma_set_mask_64bit(struct dma_device *dma)
Set 64-bit addressable space mask.
Definition dma.h:467
#define DMA_TX
Device will read data from host memory.
Definition dma.h:135
void dma_free(struct dma_mapping *map, void *addr, size_t len)
Unmap and free DMA-coherent buffer.
#define DMA_RX
Device will write data to host memory.
Definition dma.h:138
static __always_inline int struct dma_mapping * map
Definition dma.h:184
void dma_ufree(struct dma_mapping *map, void *addr, size_t len)
Unmap and free DMA-coherent buffer from external (user) memory.
void * dma_alloc(struct dma_device *dma, struct dma_mapping *map, size_t len, size_t align)
Allocate and map DMA-coherent buffer.
physaddr_t dma(struct dma_mapping *map, void *addr)
Get DMA address from virtual address.
void * dma_umalloc(struct dma_device *dma, struct dma_mapping *map, size_t len, size_t align)
Allocate and map DMA-coherent buffer from external (user) memory.
void * zalloc(size_t size)
Allocate cleared memory.
Definition malloc.c:718
Dynamic memory allocation.
static unsigned int unsigned int reg
Definition myson.h:162
unsigned long pci_bar_size(struct pci_device *pci, unsigned int reg)
Get the size of a PCI BAR.
Definition pci.c:178
void adjust_pci_device(struct pci_device *pci)
Enable PCI device.
Definition pci.c:255
unsigned long pci_bar_start(struct pci_device *pci, unsigned int reg)
Find the start of a PCI BAR.
Definition pci.c:111
PCI bus.
#define __pci_driver
Declare a PCI driver.
Definition pci.h:281
#define PCI_CLASS_SERIAL_USB_XHCI
xHCI USB controller
Definition pci.h:144
#define PCI_COMMAND_MASTER
Bus master.
Definition pci.h:29
static void pci_set_drvdata(struct pci_device *pci, void *priv)
Set PCI driver-private data.
Definition pci.h:370
#define PCI_ROM(_vendor, _device, _name, _description, _data)
Definition pci.h:311
static void * pci_get_drvdata(struct pci_device *pci)
Get PCI driver-private data.
Definition pci.h:380
#define PCI_COMMAND
PCI command.
Definition pci.h:26
#define PCI_CLASS_ID(base, sub, progif)
Construct PCI class ID.
Definition pci.h:206
static void(* free)(struct refcnt *refcnt))
Definition refcnt.h:55
uint16_t protocol
Protocol ID.
Definition stp.h:7
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
A command-line command.
Definition command.h:10
char name[40]
Name.
Definition device.h:79
A DMA mapping.
Definition dma.h:33
A persistent I/O buffer.
Definition iobuf.h:98
void * data
Start of data.
Definition iobuf.h:113
A PCI device ID list entry.
Definition pci.h:178
unsigned long driver_data
Arbitrary driver data.
Definition pci.h:186
A PCI device.
Definition pci.h:214
struct device dev
Generic device.
Definition pci.h:216
struct pci_device_id * id
Driver device ID.
Definition pci.h:251
struct dma_device dma
DMA device.
Definition pci.h:218
A PCI driver.
Definition pci.h:255
A data structure for storing profiling information.
Definition profile.h:27
A startup/shutdown function.
Definition init.h:43
A USB bus.
Definition usb.h:976
struct usb_hub * hub
Root hub.
Definition usb.h:1005
A USB device.
Definition usb.h:733
char name[32]
Name.
Definition usb.h:735
struct usb_port * port
USB port.
Definition usb.h:737
unsigned int address
Device address, if assigned.
Definition usb.h:743
struct usb_endpoint * ep[32]
Endpoint list.
Definition usb.h:755
unsigned int speed
Device speed.
Definition usb.h:739
A USB endpoint.
Definition usb.h:414
size_t mtu
Maximum transfer size.
Definition usb.h:422
struct usb_device * usb
USB device.
Definition usb.h:416
unsigned int attributes
Attributes.
Definition usb.h:420
unsigned int burst
Maximum burst size.
Definition usb.h:424
unsigned int interval
Interval (in microframes).
Definition usb.h:426
unsigned int address
Endpoint address.
Definition usb.h:418
USB host controller operations.
Definition usb.h:1041
A USB hub.
Definition usb.h:851
struct usb_bus * bus
USB bus.
Definition usb.h:855
unsigned int ports
Number of ports.
Definition usb.h:861
struct usb_device * usb
Underlying USB device, if any.
Definition usb.h:857
A USB port.
Definition usb.h:823
struct usb_hub * hub
USB hub.
Definition usb.h:825
struct usb_device * usb
Currently attached device (if in use).
Definition usb.h:845
unsigned int address
Port address.
Definition usb.h:827
A USB setup data packet.
Definition usb.h:83
uint16_t request
Request.
Definition usb.h:85
An input control context.
Definition xhci.h:723
uint32_t drop
Drop context flags.
Definition xhci.h:725
uint32_t add
Add context flags.
Definition xhci.h:727
struct dma_mapping map
DMA mapping.
Definition xhci.h:1051
uint64_t * context
Context base addresses.
Definition xhci.h:1049
An xHCI device.
Definition xhci.h:1069
unsigned int csz_shift
Context size shift.
Definition xhci.h:1100
struct xhci_dcbaa dcbaa
Device context base address array.
Definition xhci.h:1111
unsigned int xecp
xHCI extended capabilities offset
Definition xhci.h:1102
struct xhci_slot ** slot
Device slots, indexed by slot ID.
Definition xhci.h:1126
struct device * dev
Underlying hardware device.
Definition xhci.h:1073
unsigned int legacy
USB legacy support capability (if present and enabled).
Definition xhci.h:1108
const char * name
Name.
Definition xhci.h:1077
void * db
Doorbell registers.
Definition xhci.h:1088
struct xhci_pch pch
Intel PCH quirk.
Definition xhci.h:1132
void * op
Operational registers.
Definition xhci.h:1084
void * run
Runtime registers.
Definition xhci.h:1086
unsigned int intrs
Number of interrupters.
Definition xhci.h:1093
unsigned int slots
Number of device slots.
Definition xhci.h:1091
int addr64
64-bit addressing capability
Definition xhci.h:1098
size_t pagesize
Page size.
Definition xhci.h:1105
struct xhci_trb_ring command
Command ring.
Definition xhci.h:1117
union xhci_trb * pending
Current command (if any).
Definition xhci.h:1121
struct xhci_scratchpad scratch
Scratchpad buffer.
Definition xhci.h:1114
unsigned int quirks
Quirks.
Definition xhci.h:1079
void * regs
Registers.
Definition xhci.h:1071
struct dma_device * dma
DMA device.
Definition xhci.h:1075
int failed
Command mechanism has permanently failed.
Definition xhci.h:1123
void * cap
Capability registers.
Definition xhci.h:1082
struct xhci_event_ring event
Event ring.
Definition xhci.h:1119
struct usb_bus * bus
USB bus.
Definition xhci.h:1129
unsigned int ports
Number of ports.
Definition xhci.h:1095
An endpoint context.
Definition xhci.h:771
uint16_t trb_len
Average TRB length.
Definition xhci.h:789
uint8_t burst
Maximum burst size.
Definition xhci.h:783
uint64_t dequeue
Transfer ring dequeue pointer.
Definition xhci.h:787
uint16_t mtu
Maximum packet size.
Definition xhci.h:785
uint8_t interval
Polling interval.
Definition xhci.h:777
uint8_t state
Endpoint state.
Definition xhci.h:773
uint8_t type
Endpoint type.
Definition xhci.h:781
An xHCI endpoint.
Definition xhci.h:1164
struct usb_endpoint * ep
USB endpoint.
Definition xhci.h:1170
struct xhci_slot * slot
xHCI slot
Definition xhci.h:1168
struct xhci_trb_ring ring
Transfer ring.
Definition xhci.h:1180
unsigned int interval
Endpoint interval.
Definition xhci.h:1176
unsigned int ctx
Context index.
Definition xhci.h:1172
struct xhci_endpoint_context * context
Endpoint context.
Definition xhci.h:1178
struct xhci_device * xhci
xHCI device
Definition xhci.h:1166
unsigned int type
Endpoint type.
Definition xhci.h:1174
An event ring.
Definition xhci.h:871
union xhci_trb * trb
Transfer request blocks.
Definition xhci.h:879
Intel PCH quirk.
Definition xhci.h:1021
uint32_t usb3pssen
USB3 port SuperSpeed enable register original value.
Definition xhci.h:1025
uint32_t xusb2pr
USB2 port routing register original value.
Definition xhci.h:1023
Scratchpad buffer.
Definition xhci.h:1055
struct dma_mapping buffer_map
Buffer DMA mapping.
Definition xhci.h:1061
struct dma_mapping array_map
Array DMA mapping.
Definition xhci.h:1065
void * buffer
Scratchpad buffer area.
Definition xhci.h:1059
uint64_t * array
Scratchpad array.
Definition xhci.h:1063
unsigned int count
Number of page-sized scratchpad buffers.
Definition xhci.h:1057
A slot context.
Definition xhci.h:741
uint8_t tt_id
TT hub slot ID.
Definition xhci.h:751
uint8_t port
Root hub port number.
Definition xhci.h:747
uint32_t info
Device info.
Definition xhci.h:743
uint8_t tt_port
TT port number.
Definition xhci.h:753
uint8_t ports
Number of downstream ports.
Definition xhci.h:749
uint8_t address
USB address.
Definition xhci.h:757
An xHCI device slot.
Definition xhci.h:1136
struct xhci_endpoint * endpoint[XHCI_CTX_END]
Endpoints, indexed by context ID.
Definition xhci.h:1160
struct xhci_device * xhci
xHCI device
Definition xhci.h:1138
struct usb_device * usb
USB device.
Definition xhci.h:1140
unsigned int id
Slot ID.
Definition xhci.h:1142
A transfer request block.
Definition xhci.h:315
uint8_t flags
Flags.
Definition xhci.h:321
uint8_t type
Type.
Definition xhci.h:323
A command completion event transfer request block.
Definition xhci.h:614
uint8_t code
Completion code.
Definition xhci.h:620
uint64_t command
Command TRB pointer.
Definition xhci.h:616
A context transfer request block.
Definition xhci.h:504
uint64_t input
Input context.
Definition xhci.h:506
uint8_t type
Type.
Definition xhci.h:512
uint8_t slot
Slot ID.
Definition xhci.h:516
A data stage transfer request block.
Definition xhci.h:396
A disable slot transfer request block.
Definition xhci.h:485
uint8_t slot
Slot ID.
Definition xhci.h:497
uint8_t type
Type.
Definition xhci.h:493
An enable slot transfer request block.
Definition xhci.h:466
uint8_t slot
Slot type.
Definition xhci.h:476
uint8_t type
Type.
Definition xhci.h:474
A port status change transfer request block.
Definition xhci.h:666
uint8_t code
Completion code.
Definition xhci.h:672
A normal transfer request block.
Definition xhci.h:350
uint8_t flags
Flags.
Definition xhci.h:356
A port status change transfer request block.
Definition xhci.h:645
uint8_t port
Port ID.
Definition xhci.h:649
A reset endpoint transfer request block.
Definition xhci.h:529
uint8_t slot
Slot ID.
Definition xhci.h:541
uint8_t type
Type.
Definition xhci.h:537
uint8_t endpoint
Endpoint ID.
Definition xhci.h:539
A transfer request block command/transfer ring.
Definition xhci.h:842
unsigned int shift
Ring size (log2).
Definition xhci.h:848
struct xhci_trb_link * link
Link TRB (if applicable).
Definition xhci.h:862
void * db
Doorbell register.
Definition xhci.h:865
struct io_buffer ** iobuf
I/O buffers.
Definition xhci.h:853
union xhci_trb * trb
Transfer request blocks.
Definition xhci.h:856
size_t len
Length of transfer request blocks.
Definition xhci.h:858
unsigned int mask
Ring counter mask.
Definition xhci.h:850
unsigned int cons
Consumer counter.
Definition xhci.h:846
uint32_t dbval
Doorbell register value.
Definition xhci.h:867
struct dma_mapping map
DMA mapping.
Definition xhci.h:860
unsigned int prod
Producer counter.
Definition xhci.h:844
A set transfer ring dequeue pointer transfer request block.
Definition xhci.h:567
uint8_t endpoint
Endpoint ID.
Definition xhci.h:577
uint64_t dequeue
Dequeue pointer.
Definition xhci.h:569
A setup stage transfer request block.
Definition xhci.h:371
uint8_t type
Type.
Definition xhci.h:379
uint8_t flags
Flags.
Definition xhci.h:377
uint32_t len
Length.
Definition xhci.h:375
uint8_t direction
Transfer direction.
Definition xhci.h:381
struct usb_setup_packet packet
Setup packet.
Definition xhci.h:373
A status stage transfer request block.
Definition xhci.h:421
A stop endpoint transfer request block.
Definition xhci.h:548
uint8_t slot
Slot ID.
Definition xhci.h:560
uint8_t type
Type.
Definition xhci.h:556
uint8_t endpoint
Endpoint ID.
Definition xhci.h:558
uint32_t status
Status.
Definition xhci.h:309
uint64_t parameter
Parameter.
Definition xhci.h:307
uint32_t control
Control.
Definition xhci.h:311
A transfer event transfer request block.
Definition xhci.h:589
uint8_t endpoint
Endpoint ID.
Definition xhci.h:599
uint32_t cmplt
Completion status.
Definition xhci.h:593
uint8_t slot
Slot ID.
Definition xhci.h:601
uint64_t transfer
Transfer TRB pointer.
Definition xhci.h:591
uint8_t nop
Definition tcp.h:0
void mdelay(unsigned long msecs)
Delay for a fixed number of milliseconds.
Definition timer.c:79
A transfer request block.
Definition xhci.h:685
struct xhci_trb_stop_endpoint stop
Stop endpoint TRB.
Definition xhci.h:709
struct xhci_trb_normal normal
Normal TRB.
Definition xhci.h:691
struct xhci_trb_port_status port
Port status changed event.
Definition xhci.h:717
struct xhci_trb_host_controller host
Host controller event.
Definition xhci.h:719
struct xhci_trb_transfer transfer
Transfer event.
Definition xhci.h:713
struct xhci_trb_template template
Template.
Definition xhci.h:687
struct xhci_trb_disable_slot disable
Disable slot TRB.
Definition xhci.h:703
struct xhci_trb_complete complete
Command completion event.
Definition xhci.h:715
struct xhci_trb_enable_slot enable
Enable slot TRB.
Definition xhci.h:701
struct xhci_trb_context context
Input context TRB.
Definition xhci.h:705
struct xhci_trb_set_tr_dequeue_pointer dequeue
Set transfer ring dequeue pointer TRB.
Definition xhci.h:711
struct xhci_trb_common common
Common fields.
Definition xhci.h:689
struct xhci_trb_reset_endpoint reset
Reset endpoint TRB.
Definition xhci.h:707
struct xhci_trb_link link
Link TRB.
Definition xhci.h:699
struct usb_port * usb_transaction_translator(struct usb_device *usb)
Get USB transaction translator.
Definition usb.c:2371
unsigned int usb_route_string(struct usb_device *usb)
Get USB route string.
Definition usb.c:2335
const char * usb_endpoint_name(struct usb_endpoint *ep)
Get USB endpoint name (for debugging).
Definition usb.c:220
void usb_port_changed(struct usb_port *port)
Report port status change.
Definition usb.c:1857
void unregister_usb_bus(struct usb_bus *bus)
Unregister USB bus.
Definition usb.c:2170
int register_usb_bus(struct usb_bus *bus)
Register USB bus.
Definition usb.c:2130
struct usb_bus * alloc_usb_bus(struct device *dev, unsigned int ports, size_t mtu, struct usb_host_operations *op)
Allocate USB bus.
Definition usb.c:2094
void free_usb_bus(struct usb_bus *bus)
Free USB bus.
Definition usb.c:2194
void usb_complete_err(struct usb_endpoint *ep, struct io_buffer *iobuf, int rc)
Complete transfer (possibly with error).
Definition usb.c:586
struct usb_port * usb_root_hub_port(struct usb_device *usb)
Get USB root hub port.
Definition usb.c:2355
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition vsprintf.c:383
#define readl
Definition w89c840.c:157
#define writel
Definition w89c840.c:160
#define readb
Definition w89c840.c:155
#define writeb
Definition w89c840.c:158
static void xhci_transfer(struct xhci_device *xhci, struct xhci_trb_transfer *trb)
Handle transfer event.
Definition xhci.c:1596
static int xhci_context(struct xhci_device *xhci, struct xhci_slot *slot, struct xhci_endpoint *endpoint, unsigned int type, void(*populate)(struct xhci_device *xhci, struct xhci_slot *slot, struct xhci_endpoint *endpoint, void *input))
Issue context-based command and wait for completion.
Definition xhci.c:2028
static int xhci_stop(struct xhci_device *xhci)
Stop xHCI device.
Definition xhci.c:1110
static void xhci_device_close(struct usb_device *usb)
Close device.
Definition xhci.c:2867
static size_t xhci_device_context_offset(struct xhci_device *xhci, unsigned int ctx)
Calculate device context offset.
Definition xhci.c:419
static int xhci_command_alloc(struct xhci_device *xhci)
Allocate command ring.
Definition xhci.c:1456
static void xhci_ring_reset(struct xhci_trb_ring *ring)
Reset transfer request block ring.
Definition xhci.c:1274
static void xhci_abort(struct xhci_device *xhci)
Abort command.
Definition xhci.c:1820
static void xhci_host_controller(struct xhci_device *xhci, struct xhci_trb_host_controller *trb)
Handle host controller event.
Definition xhci.c:1736
static int xhci_writeq(struct xhci_device *xhci, physaddr_t value, void *reg)
Write potentially 64-bit register.
Definition xhci.c:366
static int xhci_endpoint_stream(struct usb_endpoint *ep, struct io_buffer *iobuf, int zlp)
Enqueue stream transfer.
Definition xhci.c:2701
static unsigned int xhci_extended_capability(struct xhci_device *xhci, unsigned int id, unsigned int offset)
Find extended capability.
Definition xhci.c:330
static int xhci_endpoint_message(struct usb_endpoint *ep, struct io_buffer *iobuf)
Enqueue message transfer.
Definition xhci.c:2602
static void xhci_remove(struct pci_device *pci)
Remove PCI device.
Definition xhci.c:3511
static int xhci_root_speed(struct usb_hub *hub, struct usb_port *port)
Update root hub port speed.
Definition xhci.c:3210
static int xhci_stop_endpoint(struct xhci_device *xhci, struct xhci_slot *slot, struct xhci_endpoint *endpoint)
Stop endpoint.
Definition xhci.c:2366
static int xhci_port_slot_type(struct xhci_device *xhci, unsigned int port)
Find port slot type.
Definition xhci.c:767
static int xhci_root_open(struct usb_hub *hub)
Open root hub.
Definition xhci.c:3084
static void xhci_root_close(struct usb_hub *hub __unused)
Close root hub.
Definition xhci.c:3130
static size_t xhci_align(size_t len)
Calculate buffer alignment.
Definition xhci.c:400
static int xhci_nop(struct xhci_device *xhci)
Issue NOP and wait for completion.
Definition xhci.c:1934
static int xhci_enqueue_multi(struct xhci_trb_ring *ring, struct io_buffer *iobuf, const union xhci_trb *trbs, unsigned int count)
Enqueue multiple transfer request blocks.
Definition xhci.c:1391
static int xhci_scratchpad_alloc(struct xhci_device *xhci)
Allocate scratchpad buffers.
Definition xhci.c:988
static int xhci_event_alloc(struct xhci_device *xhci)
Allocate event ring.
Definition xhci.c:1505
static int xhci_legacy_prevent_release
Prevent the release of ownership back to BIOS.
Definition xhci.c:533
static void xhci_port_status(struct xhci_device *xhci, struct xhci_trb_port_status *trb)
Handle port status event.
Definition xhci.c:1712
static void xhci_doorbell(struct xhci_trb_ring *ring)
Ring doorbell register.
Definition xhci.c:1437
static unsigned int xhci_port_protocol(struct xhci_device *xhci, unsigned int port)
Find port protocol.
Definition xhci.c:706
static void xhci_legacy_init(struct xhci_device *xhci)
Initialise USB legacy support.
Definition xhci.c:540
void xhci_unregister(struct xhci_device *xhci)
Unregister xHCI controller.
Definition xhci.c:3370
static void xhci_configure_endpoint_input(struct xhci_device *xhci, struct xhci_slot *slot, struct xhci_endpoint *endpoint, void *input)
Populate configure endpoint input context.
Definition xhci.c:2153
static void xhci_scratchpad_free(struct xhci_device *xhci)
Free scratchpad buffers.
Definition xhci.c:1054
static struct io_buffer * xhci_dequeue(struct xhci_trb_ring *ring)
Dequeue a transfer request block.
Definition xhci.c:1359
static struct pci_device_id xhci_ids[]
XHCI PCI device IDs.
Definition xhci.c:3545
static void xhci_hub_close(struct usb_hub *hub __unused)
Close hub.
Definition xhci.c:3066
static size_t xhci_input_context_offset(struct xhci_device *xhci, unsigned int ctx)
Calculate input context offset.
Definition xhci.c:431
static int xhci_disable_slot(struct xhci_device *xhci, unsigned int slot)
Disable slot.
Definition xhci.c:1996
static int xhci_hub_open(struct usb_hub *hub)
Open hub.
Definition xhci.c:3039
static void xhci_evaluate_context_input(struct xhci_device *xhci, struct xhci_slot *slot __unused, struct xhci_endpoint *endpoint, void *input)
Populate evaluate context input context.
Definition xhci.c:2275
static int xhci_dcbaa_alloc(struct xhci_device *xhci)
Allocate device context base address array.
Definition xhci.c:917
static int xhci_configure_endpoint(struct xhci_device *xhci, struct xhci_slot *slot, struct xhci_endpoint *endpoint)
Configure endpoint.
Definition xhci.c:2193
int xhci_register(struct xhci_device *xhci)
Register xHCI controller.
Definition xhci.c:3325
static struct io_buffer * xhci_dequeue_multi(struct xhci_trb_ring *ring)
Dequeue multiple transfer request blocks.
Definition xhci.c:1420
static int xhci_address_device(struct xhci_device *xhci, struct xhci_slot *slot)
Address device.
Definition xhci.c:2120
static int xhci_reset_endpoint(struct xhci_device *xhci, struct xhci_slot *slot, struct xhci_endpoint *endpoint)
Reset endpoint.
Definition xhci.c:2334
static unsigned int xhci_endpoint_count(size_t len, int zlp)
Calculate number of TRBs.
Definition xhci.c:2680
static void xhci_shutdown(int booting)
Prepare for exit.
Definition xhci.c:3566
static void xhci_event_poll(struct xhci_device *xhci)
Poll event ring.
Definition xhci.c:1751
static int xhci_device_address(struct usb_device *usb)
Assign device address.
Definition xhci.c:2904
static int xhci_deconfigure_endpoint(struct xhci_device *xhci, struct xhci_slot *slot, struct xhci_endpoint *endpoint)
Deconfigure endpoint.
Definition xhci.c:2247
static int xhci_enable_slot(struct xhci_device *xhci, unsigned int type)
Enable slot.
Definition xhci.c:1962
static const char * xhci_speed_name(uint32_t psi)
Transcribe port speed (for debugging).
Definition xhci.c:649
static struct usb_host_operations xhci_operations
USB host controller operations.
Definition xhci.c:3286
static void xhci_dump_port(struct xhci_device *xhci, unsigned int port)
Dump port registers.
Definition xhci.c:490
static void xhci_event_free(struct xhci_device *xhci)
Free event ring.
Definition xhci.c:1570
static int xhci_endpoint_open(struct usb_endpoint *ep)
Open endpoint.
Definition xhci.c:2448
static void xhci_legacy_claim(struct xhci_device *xhci)
Claim ownership from BIOS.
Definition xhci.c:571
static void xhci_address_device_input(struct xhci_device *xhci, struct xhci_slot *slot, struct xhci_endpoint *endpoint, void *input)
Populate address device input context.
Definition xhci.c:2078
static int xhci_ring_alloc(struct xhci_device *xhci, struct xhci_trb_ring *ring, unsigned int shift, unsigned int slot, unsigned int target, unsigned int stream)
Allocate transfer request block ring.
Definition xhci.c:1217
static void xhci_dump(struct xhci_device *xhci)
Dump host controller registers.
Definition xhci.c:449
static int xhci_fail(struct xhci_device *xhci)
Mark xHCI device as permanently failed.
Definition xhci.c:1180
static unsigned int xhci_supported_protocol(struct xhci_device *xhci, unsigned int port)
Find supported protocol extended capability for a port.
Definition xhci.c:672
static int xhci_endpoint_reset(struct usb_endpoint *ep)
Reset endpoint.
Definition xhci.c:2554
static void xhci_pch_undo(struct xhci_device *xhci, struct pci_device *pci)
Undo Intel PCH-specific quirk fixes.
Definition xhci.c:3432
static int xhci_command(struct xhci_device *xhci, union xhci_trb *trb)
Issue command and wait for completion.
Definition xhci.c:1863
static void xhci_endpoint_close(struct usb_endpoint *ep)
Close endpoint.
Definition xhci.c:2524
static void xhci_deconfigure_endpoint_input(struct xhci_device *xhci __unused, struct xhci_slot *slot __unused, struct xhci_endpoint *endpoint, void *input)
Populate deconfigure endpoint input context.
Definition xhci.c:2221
static void xhci_legacy_release(struct xhci_device *xhci)
Release ownership back to BIOS.
Definition xhci.c:618
static void xhci_complete(struct xhci_device *xhci, struct xhci_trb_complete *trb)
Handle command completion event.
Definition xhci.c:1675
static int xhci_reset(struct xhci_device *xhci)
Reset xHCI device.
Definition xhci.c:1142
static int xhci_port_psiv(struct xhci_device *xhci, unsigned int port, unsigned int speed)
Find protocol speed ID value.
Definition xhci.c:855
static int xhci_bus_open(struct usb_bus *bus)
Open USB bus.
Definition xhci.c:2946
static int xhci_evaluate_context(struct xhci_device *xhci, struct xhci_slot *slot, struct xhci_endpoint *endpoint)
Evaluate context.
Definition xhci.c:2306
static void xhci_command_free(struct xhci_device *xhci)
Free command ring.
Definition xhci.c:1487
static int xhci_root_enable(struct usb_hub *hub, struct usb_port *port)
Enable port.
Definition xhci.c:3142
static void xhci_ring_free(struct xhci_trb_ring *ring)
Free transfer request block ring.
Definition xhci.c:1290
static void xhci_bus_close(struct usb_bus *bus)
Close USB bus.
Definition xhci.c:2997
static int xhci_set_tr_dequeue_pointer(struct xhci_device *xhci, struct xhci_slot *slot, struct xhci_endpoint *endpoint)
Set transfer ring dequeue pointer.
Definition xhci.c:2399
#define ECODE(code)
Definition xhci.c:234
static void xhci_run(struct xhci_device *xhci)
Start xHCI device.
Definition xhci.c:1088
static int xhci_endpoint_mtu(struct usb_endpoint *ep)
Update MTU.
Definition xhci.c:2582
void xhci_init(struct xhci_device *xhci)
Initialise device.
Definition xhci.c:264
static int xhci_root_clear_tt(struct usb_hub *hub, struct usb_port *port, struct usb_endpoint *ep)
Clear transaction translator buffer.
Definition xhci.c:3267
static void xhci_pch_fix(struct xhci_device *xhci, struct pci_device *pci)
Fix Intel PCH-specific quirks.
Definition xhci.c:3394
static void xhci_bus_poll(struct usb_bus *bus)
Poll USB bus.
Definition xhci.c:3019
static void xhci_dcbaa_free(struct xhci_device *xhci)
Free device context base address array.
Definition xhci.c:959
static int xhci_enqueue(struct xhci_trb_ring *ring, struct io_buffer *iobuf, const union xhci_trb *trb)
Enqueue a transfer request block.
Definition xhci.c:1316
static int xhci_probe(struct pci_device *pci)
Probe PCI device.
Definition xhci.c:3448
static int xhci_root_disable(struct usb_hub *hub, struct usb_port *port)
Disable port.
Definition xhci.c:3177
static int xhci_port_speed(struct xhci_device *xhci, unsigned int port, unsigned int psiv)
Find port speed.
Definition xhci.c:791
static int xhci_device_open(struct usb_device *usb)
Open device.
Definition xhci.c:2784
USB eXtensible Host Controller Interface (xHCI) driver.
#define XHCI_PORTSC_PP
Port power.
Definition xhci.h:228
#define XHCI_CAP_HCSPARAMS1
Structural parameters 1.
Definition xhci.h:40
#define XHCI_CTX_SLOT
Slot context index.
Definition xhci.h:945
#define XHCI_DATA_IN
Input data direction.
Definition xhci.h:415
#define XHCI_PCH_USB3PRM
Intel PCH USB3 port routing mask register.
Definition xhci.h:1041
#define XHCI_TRB_HOST_CONTROLLER
A port status change transfer request block.
Definition xhci.h:682
#define XHCI_XECP_ID(xecp)
xHCI extended capability ID
Definition xhci.h:77
#define XHCI_STATUS_OUT
Output status direction.
Definition xhci.h:443
#define XHCI_PORTSC_PLS_RXDETECT
RxDetect port link state.
Definition xhci.h:222
#define XHCI_BAD_PSIV
Invalid protocol speed ID values quirk.
Definition xhci.h:1044
@ XHCI_ENDPOINT_RUNNING
Endpoint is running.
Definition xhci.h:801
#define XHCI_TRB_SETUP
A setup stage transfer request block.
Definition xhci.h:387
#define XHCI_OP_PORTPMSC(port)
Port power management status and control register.
Definition xhci.h:287
#define XHCI_OP_PORTHLPMC(port)
Port hardware link power management control register.
Definition xhci.h:293
#define XHCI_PORTSC_CSC
Connect status change.
Definition xhci.h:249
#define XHCI_PCH
Intel PCH quirk flag.
Definition xhci.h:1029
static unsigned int xhci_ring_remaining(struct xhci_trb_ring *ring)
Calculate space remaining in TRB ring.
Definition xhci.h:915
#define XHCI_USBLEGSUP_MAX_WAIT_MS
Maximum time to wait for BIOS to release ownership.
Definition xhci.h:986
#define XHCI_USBLEGSUP_CTLSTS
USB legacy support control/status.
Definition xhci.h:98
#define XHCI_OP_CONFIG
Configure register.
Definition xhci.h:194
#define XHCI_PCH_XUSB2PRM
Intel PCH USB2 port routing mask register.
Definition xhci.h:1035
#define XHCI_TRB_NORMAL
A normal transfer request block.
Definition xhci.h:364
#define XHCI_RUN_ERSTSZ(intr)
Event ring segment table size register.
Definition xhci.h:296
#define XHCI_TRB_NOP_CMD
A no-op command transfer request block.
Definition xhci.h:586
#define XHCI_USBCMD_RUN
Run/stop.
Definition xhci.h:158
#define XHCI_TRB_ADDRESS_DEVICE
An address device transfer request block.
Definition xhci.h:520
#define XHCI_SUPPORTED_SLOT_TYPE(slot)
Supported protocol slot type.
Definition xhci.h:128
#define XHCI_HCCPARAMS1_ADDR64(params)
64-bit addressing capability
Definition xhci.h:62
#define XHCI_EP_DCS
Endpoint dequeue cycle state.
Definition xhci.h:826
#define XHCI_USBLEGSUP_BIOS_OWNED
USB legacy support BIOS ownership flag.
Definition xhci.h:89
#define XHCI_SUPPORTED_PORTS_PSIC(ports)
Supported protocol PSI count.
Definition xhci.h:122
#define XHCI_CRCR_CA
Command abort.
Definition xhci.h:185
#define XHCI_OP_PAGESIZE
Page size register.
Definition xhci.h:170
#define XHCI_SUPPORTED_PORTS
Supported protocol ports.
Definition xhci.h:113
#define XHCI_TRB_CH
Transfer request block chain flag.
Definition xhci.h:335
#define XHCI_TRB_STOP_ENDPOINT
A stop endpoint transfer request block.
Definition xhci.h:564
#define XHCI_PORT_POWER_DELAY_MS
Time to delay after enabling power to a port.
Definition xhci.h:231
#define XHCI_SETUP_IN
Setup stage input data direction.
Definition xhci.h:390
#define XHCI_TRB_PORT_STATUS
A port status change transfer request block.
Definition xhci.h:663
#define XHCI_HCSPARAMS1_SLOTS(params)
Number of device slots.
Definition xhci.h:43
#define XHCI_PORTSC_LWS
Port link state write strobe.
Definition xhci.h:243
#define XHCI_RUN_ERSTBA(intr)
Event ring segment table base address register.
Definition xhci.h:299
#define XHCI_DCI(ctx)
Device context index.
Definition xhci.h:959
#define XHCI_CAP_DBOFF
Doorbell offset.
Definition xhci.h:71
#define XHCI_SUPPORTED_SLOT
Supported protocol slot.
Definition xhci.h:125
#define XHCI_EP0_TRB_LEN
Control endpoint average TRB length.
Definition xhci.h:829
#define XHCI_SLOT_INFO(entries, hub, speed, route)
Construct slot context device info.
Definition xhci.h:767
#define XHCI_USBLEGSUP_OS_OWNED
USB legacy support OS ownership flag.
Definition xhci.h:95
#define XHCI_CTX_EP0
Endpoint zero context index.
Definition xhci.h:953
@ XHCI_CMPLT_SUCCESS
Success.
Definition xhci.h:637
@ XHCI_CMPLT_CMD_STOPPED
Command ring stopped.
Definition xhci.h:641
@ XHCI_CMPLT_SHORT
Short packet.
Definition xhci.h:639
#define XHCI_RUN_ERDP(intr)
Event ring dequeue pointer register.
Definition xhci.h:302
#define XHCI_CMPLT_RESIDUAL(cmplt)
Residual bytes not transferred.
Definition xhci.h:611
#define XHCI_CTX_END
End of contexts.
Definition xhci.h:956
#define XHCI_PORTSC_PSIV(portsc)
Port speed ID value.
Definition xhci.h:234
#define XHCI_OP_PORTLI(port)
Port link info register.
Definition xhci.h:290
#define XHCI_CRCR_RCS
Command ring cycle state.
Definition xhci.h:182
#define XHCI_PORTSC_CCS
Current connect status.
Definition xhci.h:207
#define XHCI_SUPPORTED_PSI(index)
Supported protocol PSI.
Definition xhci.h:131
#define XHCI_TRB_DISABLE_SLOT
A disable slot transfer request block.
Definition xhci.h:501
#define XHCI_PCH_XUSB2PR
Intel PCH USB2 port routing register.
Definition xhci.h:1032
#define XHCI_PORTSC_PED
Port enabled.
Definition xhci.h:210
#define XHCI_PORTSC_PR
Port reset.
Definition xhci.h:213
#define XHCI_CAP_HCSPARAMS2
Structural parameters 2.
Definition xhci.h:52
#define XHCI_CMD_TRBS_LOG2
Number of TRBs (excluding Link TRB) in the command ring.
Definition xhci.h:968
static physaddr_t xhci_ring_consumed(struct xhci_trb_ring *ring)
Calculate physical address of most recently consumed TRB.
Definition xhci.h:938
#define XHCI_USBCMD_HCRST
Host controller reset.
Definition xhci.h:161
#define XHCI_TRANSFER_TRBS_LOG2
Number of TRBs in a transfer ring.
Definition xhci.h:980
#define XHCI_TRB_CONFIGURE_ENDPOINT
A configure endpoint transfer request block.
Definition xhci.h:523
#define XHCI_XECP_ID_SUPPORTED
Supported protocol extended capability.
Definition xhci.h:101
#define XHCI_SUPPORTED_NAME
Supported protocol name.
Definition xhci.h:110
#define XHCI_SUPPORTED_PSI_MANTISSA(psi)
Supported protocol PSI mantissa.
Definition xhci.h:137
#define XHCI_ENDPOINT_STATE_MASK
Endpoint state mask.
Definition xhci.h:811
#define XHCI_TRB_IDT
Transfer request block immediate data flag.
Definition xhci.h:341
#define XHCI_XECP_ID_LEGACY
USB legacy support extended capability.
Definition xhci.h:83
#define XHCI_PAGESIZE(pagesize)
Page size.
Definition xhci.h:173
#define XHCI_EP_TYPE_IN
Input endpoint type.
Definition xhci.h:820
#define XHCI_CAP_RTSOFF
Runtime register space offset.
Definition xhci.h:74
@ XHCI_SPEED_SUPER
Super speed.
Definition xhci.h:151
@ XHCI_SPEED_FULL
Full speed (12Mbps).
Definition xhci.h:145
@ XHCI_SPEED_LOW
Low speed (1.5Mbps).
Definition xhci.h:147
@ XHCI_SPEED_HIGH
High speed (480Mbps).
Definition xhci.h:149
#define XHCI_SUPPORTED_PSI_EXPONENT(psi)
Supported protocol PSI exponent.
Definition xhci.h:140
#define XHCI_HCCPARAMS1_XECP(params)
xHCI extended capabilities pointer
Definition xhci.h:68
#define XHCI_TRB_TRANSFER
A transfer event transfer request block.
Definition xhci.h:605
#define XHCI_DBVAL(target, stream)
Calculate doorbell register value.
Definition xhci.h:891
#define XHCI_USBLEGSUP_OS
USB legacy support OS owned semaphore.
Definition xhci.h:92
#define XHCI_COMMAND_MAX_WAIT_MS
Maximum time to wait for a command to complete.
Definition xhci.h:1006
#define XHCI_OP_DNCTRL
Device notifcation control register.
Definition xhci.h:176
#define XHCI_TRB_DATA
A data stage transfer request block.
Definition xhci.h:412
#define XHCI_CONFIG_MAX_SLOTS_EN_MASK
Maximum device slots enabled mask.
Definition xhci.h:200
#define XHCI_HCSPARAMS1_INTRS(params)
Number of interrupters.
Definition xhci.h:46
#define XHCI_TRB_LINK
A link transfer request block.
Definition xhci.h:460
#define XHCI_STOP_MAX_WAIT_MS
Maximum time to wait for host controller to stop.
Definition xhci.h:992
#define XHCI_SETUP_OUT
Setup stage output data direction.
Definition xhci.h:393
#define XHCI_ICI(ctx)
Input context index.
Definition xhci.h:962
#define XHCI_CTX(address)
Calculate context index from USB endpoint address.
Definition xhci.h:948
#define XHCI_COMMAND_ABORT_DELAY_MS
Time to delay after aborting a command.
Definition xhci.h:1012
#define XHCI_XECP_NEXT(xecp)
Next xHCI extended capability pointer.
Definition xhci.h:80
#define XHCI_SUPPORTED_REVISION_VER(revision)
Supported protocol minor revision.
Definition xhci.h:107
#define XHCI_TRB_SET_TR_DEQUEUE_POINTER
A set transfer ring dequeue pointer transfer request block.
Definition xhci.h:583
#define XHCI_CRCR_CRR
Command ring running.
Definition xhci.h:188
#define XHCI_PORT_RESET_MAX_WAIT_MS
Maximum time to wait for a port reset to complete.
Definition xhci.h:1018
#define XHCI_PORTSC_PRESERVE
Port status and control bits which should be preserved.
Definition xhci.h:284
#define XHCI_PORTSC_CHANGE
Port status change mask.
Definition xhci.h:270
#define XHCI_TRB_EVALUATE_CONTEXT
An evaluate context transfer request block.
Definition xhci.h:526
#define XHCI_TRB_IOC
Transfer request block interrupt on completion flag.
Definition xhci.h:338
#define XHCI_STATUS_IN
Input status direction.
Definition xhci.h:440
#define XHCI_OP_USBCMD
USB command register.
Definition xhci.h:155
#define XHCI_TRB_STATUS
A status stage transfer request block.
Definition xhci.h:437
#define XHCI_TRB_C
Transfer request block cycle bit flag.
Definition xhci.h:329
#define XHCI_SUPPORTED_PORTS_COUNT(ports)
Supported protocol port count.
Definition xhci.h:119
#define XHCI_EP_TYPE(type)
Endpoint type.
Definition xhci.h:814
#define XHCI_TRB_COMPLETE
A command completion event transfer request block.
Definition xhci.h:632
#define XHCI_OP_USBSTS
USB status register.
Definition xhci.h:164
#define XHCI_PORTSC_PLS_DISABLED
Disabled port link state.
Definition xhci.h:219
#define XHCI_BAR
xHCI PCI BAR
Definition xhci.h:31
#define XHCI_SUPPORTED_PORTS_OFFSET(ports)
Supported protocol port offset.
Definition xhci.h:116
#define XHCI_OP_PORTSC(port)
Port status and control register.
Definition xhci.h:204
#define XHCI_LINK_STATE_DELAY_MS
Time to delay after writing the port link state.
Definition xhci.h:246
#define XHCI_EVENT_TRBS_LOG2
Number of TRBs in the event ring.
Definition xhci.h:974
#define XHCI_HCSPARAMS2_SCRATCHPADS(params)
Number of page-sized scratchpad buffers.
Definition xhci.h:55
#define XHCI_MIN_ALIGN
Minimum alignment required for data structures.
Definition xhci.h:25
#define XHCI_USBSTS_HCH
Host controller halted.
Definition xhci.h:167
#define XHCI_DATA_OUT
Output data direction.
Definition xhci.h:418
#define XHCI_TRB_TC
Transfer request block toggle cycle bit flag.
Definition xhci.h:332
#define XHCI_RESET_MAX_WAIT_MS
Maximum time to wait for reset to complete.
Definition xhci.h:998
#define XHCI_CAP_HCCPARAMS1
Capability parameters.
Definition xhci.h:59
#define XHCI_CMPLT_CODE(cmplt)
Completion code.
Definition xhci.h:608
#define XHCI_OP_DCBAAP
Device context base address array pointer.
Definition xhci.h:191
#define XHCI_PCH_USB3PSSEN
Intel PCH SuperSpeed enable register.
Definition xhci.h:1038
static unsigned int xhci_ring_fill(struct xhci_trb_ring *ring)
Calculate space used in TRB ring.
Definition xhci.h:900
#define XHCI_EP_TYPE_CONTROL
Control endpoint type.
Definition xhci.h:817
#define XHCI_OP_CRCR
Command ring control register.
Definition xhci.h:179
#define XHCI_TRB_ENABLE_SLOT
An enable slot transfer request block.
Definition xhci.h:482
#define XHCI_PORTSC_PLS_MASK
Port link state mask.
Definition xhci.h:225
#define XHCI_MTU
Maximum transfer size.
Definition xhci.h:28
#define XHCI_HCCPARAMS1_CSZ_SHIFT(params)
Context size shift.
Definition xhci.h:65
#define XHCI_HCSPARAMS1_PORTS(params)
Number of ports.
Definition xhci.h:49
#define XHCI_TRB_TYPE_MASK
Transfer request block type mask.
Definition xhci.h:347
#define XHCI_USBLEGSUP_BIOS
USB legacy support BIOS owned semaphore.
Definition xhci.h:86
#define XHCI_CAP_CAPLENGTH
Capability register length.
Definition xhci.h:34
#define XHCI_TRB_RESET_ENDPOINT
A reset endpoint transfer request block.
Definition xhci.h:545
#define XHCI_SUPPORTED_REVISION
Supported protocol revision.
Definition xhci.h:104
#define XHCI_SUPPORTED_PSI_VALUE(psi)
Supported protocol PSI value.
Definition xhci.h:134
#define XHCI_EP_TYPE_PERIODIC
Periodic endpoint type.
Definition xhci.h:823
#define XHCI_CONFIG_MAX_SLOTS_EN(slots)
Maximum device slots enabled.
Definition xhci.h:197