Add xHCI USB support
v2 cover Add xHCI USB support
Add basic support for xHCI USB controllers and non root xHCI hubs. The motivation is to use this code on platforms that do not provide user input by runtime services (like BIOS or UEFI platform) do. This is the case when GRUB is used as coreboot payload for example.
The code is based on seabios implementation, but has been heavily modified to match grubs internals.
Changes done in version 2:
* Code cleanup
* Code style fixes
* Don't leak memory buffers
* Compile without warnings
* Add more defines
* Add more helper functions
* Don't assume a 1:1 virtual to physical mapping
* Flush cachelines after writing buffers
* Don't use hardcoded page size
* Proper scratchpad register setup
* xHCI bios ownership handoff
Changes done in version 3:
* Several bug fixes for real hardware
* Fixed a race condition detecting events, which doesn't appear on qemu based xHCI controllers
* Don't accidently disable USB3.0 devices after first command
* Support arbitrary protocol speed IDs
* Coding style cleanup
* Support for non root SuperSpeed hubs
Changes Tested:
* Qemu system x86_64
* virtual USB HID keyboard (usb-kbd)
* virtual USB HID mass storage (usb-storage)
* Intel C246 integrated xHCI (Supermicro X11SSH-F)
* iKVM HID keyboard
* USB3 HID mass storage (controller root port)
* External USB HID keyboard
TODO:
* Test on more hardware
* Test on USB3 hubs
* Support for USB 3.1 and USB 3.2 controllers
Patrick Rudolph (7):
grub-core/bus/usb: Parse SuperSpeed companion descriptors
usb: Add enum for xHCI
usbtrans: Set default maximum packet size grub-core/bus/usb: Add function pointer for attach/detach events grub-core/bus/usb/usbhub: Add new private fields for xHCI controller grub-core/bus/usb: Add xhci support grub-core/bus/usb/usbhub: Add xHCI non root hub support
Makefile.am | 2 +- grub-core/Makefile.core.def | 7 + grub-core/bus/usb/ehci.c | 2 + grub-core/bus/usb/ohci.c | 2 + grub-core/bus/usb/serial/common.c | 2 +- grub-core/bus/usb/uhci.c | 2 + grub-core/bus/usb/usb.c | 44 +- grub-core/bus/usb/usbhub.c | 95 +- grub-core/bus/usb/usbtrans.c | 6 +- grub-core/bus/usb/xhci-pci.c | 195 +++ grub-core/bus/usb/xhci.c | 2496 +++++++++++++++++++++++++++++ grub-core/commands/usbtest.c | 2 +- grub-core/disk/usbms.c | 2 +- grub-core/term/usb_keyboard.c | 2 +- include/grub/usb.h | 18 +- include/grub/usbdesc.h | 12 +- include/grub/usbtrans.h | 4 + 17 files changed, 2852 insertions(+), 41 deletions(-) create mode 100644 grub-core/bus/usb/xhci-pci.c create mode 100644 grub-core/bus/usb/xhci.c
Signature · 1 line
2.26.2
PATCH v2 1/7 grub-core/bus/usb: Parse SuperSpeed companion descriptors
Parse the SS_ENDPOINT_COMPANION descriptor, which is only present on USB 3.0 capable devices and xHCI controllers. Make the descendp an array of pointers to the endpoint descriptor as it's no longer an continous array.
--- grub-core/bus/usb/serial/common.c | 2 +- grub-core/bus/usb/usb.c | 44 +++++++++++++++++++------------ grub-core/bus/usb/usbhub.c | 22 ++++++++++++---- grub-core/commands/usbtest.c | 2 +- grub-core/disk/usbms.c | 2 +- grub-core/term/usb_keyboard.c | 2 +- include/grub/usb.h | 2 +- include/grub/usbdesc.h | 11 +++++++- 8 files changed, 59 insertions(+), 28 deletions(-)
grub-core/bus/usb/serial/common.c +1 -1
@@ -72,7 +72,7 @@ grub_usbserial_attach (grub_usb_device_t usbdev, int configno, int interfno,for (j =0 ; j < interf->endpointcnt; j++) {struct grub_usb_desc_endp *endp; - endp = &usbdev->config[0 ].interf[interfno].descendp[j]; + endp = usbdev->config[0 ].interf[interfno].descendp[j]; if ((endp->endp_addr & 128) && (endp->attrib & 3) == 2 && (in_endp == GRUB_USB_SERIAL_ENDPOINT_LAST_MATCHING
grub-core/bus/usb/usb.c +27 -17
72 lines not on this page · raw message
grub-core/bus/usb/usbhub.c +17 -5
@@ -82,8 +82,14 @@ grub_usb_hub_add_dev (grub_usb_controller_t controller,if (i ==GRUB_USBHUB_MAX_DEVICES ) {grub_error (GRUB_ERR_IO ,"can't assign address to USB device" ); -for (i =0 ; i <8 ; i++) -grub_free (dev->config[i].descconf); +for (i =0 ; i <8 ; i++) { +int currif; + +for (currif =0 ; currif < dev->config[i].descconf->numif; currif++) +grub_free (dev->config[i].interf[currif].descendp); + +grub_free (dev->config[i].descconf); + }grub_free (dev);return NULL ; }
@@ -96,8 +102,14 @@ grub_usb_hub_add_dev (grub_usb_controller_t controller, i, 0, 0, NULL);if (err) { -for (i =0 ; i <8 ; i++) -grub_free (dev->config[i].descconf); +for (i =0 ; i <8 ; i++) { +int currif; + +for (currif =0 ; currif < dev->config[i].descconf->numif; currif++) +grub_free (dev->config[i].interf[currif].descendp); + +grub_free (dev->config[i].descconf); + }grub_free (dev);return NULL ; }
@@ -176,7 +188,7 @@ grub_usb_add_hub (grub_usb_device_t dev) i++) {struct grub_usb_desc_endp *endp =NULL ; - endp = &dev->config[0 ].interf[0 ].descendp[i]; + endp = dev->config[0 ].interf[0 ].descendp[i];if ((endp->endp_addr &128 ) &&grub_usb_get_ep_type (endp) ==GRUB_USB_EP_INTERRUPT )
grub-core/commands/usbtest.c +1 -1
@@ -185,7 +185,7 @@ usb_iterate (grub_usb_device_t dev, void *data __attribute__ ((unused)))for (j =0 ; j < interf->endpointcnt; j++) {struct grub_usb_desc_endp *endp; - endp = &dev->config[0 ].interf[i].descendp[j]; + endp = dev->config[0 ].interf[i].descendp[j]; grub_printf ("Endpoint #%d: %s, max packed size: %d, transfer type: %s, latency: %d\n", endp->endp_addr & 15,
grub-core/disk/usbms.c +1 -1
@@ -184,7 +184,7 @@ grub_usbms_attach (grub_usb_device_t usbdev, int configno, int interfno)for (j =0 ; j < interf->endpointcnt; j++) {struct grub_usb_desc_endp *endp; - endp = &usbdev->config[0 ].interf[interfno].descendp[j]; + endp = usbdev->config[0 ].interf[interfno].descendp[j];if ((endp->endp_addr &128 ) && (endp->attrib &3 ) ==2 )/* Bulk IN endpoint. */
grub-core/term/usb_keyboard.c +1 -1
@@ -175,7 +175,7 @@ grub_usb_keyboard_attach (grub_usb_device_t usbdev, int configno, int interfno)for (j =0 ; j < usbdev->config[configno].interf[interfno].descif->endpointcnt; j++) { - endp = &usbdev->config[configno].interf[interfno].descendp[j]; + endp = usbdev->config[configno].interf[interfno].descendp[j];if ((endp->endp_addr &128 ) &&grub_usb_get_ep_type (endp) ==GRUB_USB_EP_INTERRUPT )
include/grub/usb.h +1 -1
@@ -149,7 +149,7 @@ struct grub_usb_interface {struct grub_usb_desc_if *descif; -struct grub_usb_desc_endp *descendp; +struct grub_usb_desc_endp **descendp;/* A driver is handling this interface. Do we need to support multiple drivers for single interface?
include/grub/usbdesc.h +10 -1
@@ -29,7 +29,8 @@ typedef enum {GRUB_USB_DESCRIPTOR_INTERFACE ,GRUB_USB_DESCRIPTOR_ENDPOINT ,GRUB_USB_DESCRIPTOR_DEBUG =10 , - GRUB_USB_DESCRIPTOR_HUB = 0x29 +GRUB_USB_DESCRIPTOR_HUB =0x29 , + GRUB_USB_DESCRIPTOR_SS_ENDPOINT_COMPANION = 0x30 } grub_usb_descriptor_t;struct grub_usb_desc
@@ -105,6 +106,14 @@ struct grub_usb_desc_endpgrub_uint8_t interval; }GRUB_PACKED ; +struct grub_usb_desc_ssep { +grub_uint8_t length; +grub_uint8_t type; +grub_uint8_t maxburst; +grub_uint8_t attrib; +grub_uint16_t interval; +}GRUB_PACKED ; +struct grub_usb_desc_str {grub_uint8_t length;
Signature · 1 line
2.26.2
Re: [PATCH v2 1/7] grub-core/bus/usb: Parse SuperSpeed companion descriptors
Hi Patrick,
I noticed some style issues while rebasing this code. Also this patch series now looks like it'll need a touch up when rebasing on to the release candidate (you might want to wait until the actual release comes out to send the next version).
Quoted 79 lines · On Mon, 7 Dec 2020 08:41:21 +0100 Patrick Rudolph…
Parse the
SS_ENDPOINT_COMPANIONdescriptor, which is only present on USB 3.0 capable devices and xHCI controllers. Make the descendp an array of pointers to the endpoint descriptor as it's no longer an continous array.Signed-off-by Patrick Rudolph <[email protected]>--- grub-core/bus/usb/serial/common.c | 2 +- grub-core/bus/usb/usb.c | 44 +++++++++++++++++++------------ grub-core/bus/usb/usbhub.c | 22 ++++++++++++---- grub-core/commands/usbtest.c | 2 +- grub-core/disk/usbms.c | 2 +- grub-core/term/usb_keyboard.c | 2 +- include/grub/usb.h | 2 +- include/grub/usbdesc.h | 11 +++++++- 8 files changed, 59 insertions(+), 28 deletions(-)(quoted diff) +0
-0
b/grub-core/bus/usb/serial/common.cindex8e94c7dc0..63e64cc0a100644grub-core/bus/usb/serial/common.c +1
-1@@ -72,7 +72,7 @@ grub_usbserial_attach (grub_usb_device_t usbdev, int configno, int interfno, for (j = 0; j < interf->endpointcnt; j++) {struct grub_usb_desc_endp *endp; - endp = &usbdev->config[0 ].interf[interfno].descendp[j]; + endp = usbdev->config[0 ].interf[interfno].descendp[j]; if ((endp->endp_addr & 128) && (endp->attrib & 3) == 2 && (in_endp == GRUB_USB_SERIAL_ENDPOINT_LAST_MATCHINGgrub-core/bus/usb/usb.c +2
-1@@ -115,7 +115,7 @@ grub_usb_device_initialize (grub_usb_device_t dev)struct grub_usb_desc_device *descdev;struct grub_usb_desc_config config;grub_usb_err_t err; -int i; +int i, j;/* First we have to read first 8 bytes only and determine * max. size of packet */ @@ -149,6 +149,7 @@ grub_usb_device_initialize (grub_usb_device_t dev)int currif;char *data;struct grub_usb_desc *desc; +struct grub_usb_desc_endp *endp;/* First just read the first 4 bytes of the configuration descriptor, after that it is known how many bytes really have @@ -192,24 +193,27 @@
grub_usb_device_initialize(grub_usb_device_tdev) = (structgrub_usb_desc_if*) &data[pos];grub-core/bus/usb/usb.c +5
-15pos += dev->config[i].interf[currif].descif->length; + dev->config[i].interf[currif].descendp =grub_malloc ( + dev->config[i].interf[currif].descif->endpointcnt * +sizeof (struct grub_usb_desc_endp )); + + j =0 ;while (pos < config.totallen) { desc = (struct grub_usb_desc *)&data[pos]; -if (desc->type ==GRUB_USB_DESCRIPTOR_ENDPOINT ) -break ; -if (!desc->length) - { - err =GRUB_USB_ERR_BADDEVICE ; - goto fail; - } - pos += desc->length; - } - -/* Point to the first endpoint. */ - dev->config[i].interf[currif].descendp - = (struct grub_usb_desc_endp *) &data[pos]; - pos += (sizeof (struct grub_usb_desc_endp) - *dev->config[i].interf[currif].descif->endpointcnt);
grub-core/bus/usb/usb.c grub_usb_device_initialize (grub_usb_device_t dev) · line 208
Quoted 5 lines
grub-core/bus/usb/usb.c +5
-0+if (desc->type ==GRUB_USB_DESCRIPTOR_ENDPOINT ) { + endp = (struct grub_usb_desc_endp *) &data[pos]; + dev->config[i].interf[currif].descendp[j++] = endp; + pos += desc->length; + }else {
The above brackets don't follow GRUB convention. I haven't reviewed the other patches, but there may be similar issues.
grub-core/bus/usb/usb.c grub_usb_device_initialize (grub_usb_device_t dev) · line 213
Quoted 5 lines
grub-core/bus/usb/usb.c +5
-0+if (!desc->length) + { + err =GRUB_USB_ERR_BADDEVICE ; + goto fail; + }
They should be formatted like these.
Quoted 10 lines
grub-core/bus/usb/usb.c +3
-0+ pos += desc->length; + } + } } }@@ -217,8 +221,14 @@ grub_usb_device_initialize (grub_usb_device_t dev) fail:
grub-core/bus/usb/usb.c grub_usb_device_initialize (grub_usb_device_t · line 232
Quoted 10 lines
grub-core/bus/usb/usb.c +7
-1-for (i =0 ; i <8 ; i++) +for (i =0 ; i <8 ; i++) { +int currif; + +for (currif =0 ; currif < dev->config[i].descconf->numif; currif++) +grub_free (dev->config[i].interf[currif].descendp); +grub_free (dev->config[i].descconf); + }
Ditto on the brackets. And many more, I think you get the idea.
Quoted 123 lines
grub-core/bus/usb/usb.c +0
-0return err; }grub-core/bus/usb/usbhub.c +8
-2@@ -82,8 +82,14 @@ grub_usb_hub_add_dev (grub_usb_controller_t controller, if (i == GRUB_USBHUB_MAX_DEVICES) {grub_error (GRUB_ERR_IO ,"can't assign address to USB device" ); -for (i =0 ; i <8 ; i++) -grub_free (dev->config[i].descconf); +for (i =0 ; i <8 ; i++) { +int currif; + +for (currif =0 ; currif < dev->config[i].descconf->numif; currif++) +grub_free (dev->config[i].interf[currif].descendp); + +grub_free (dev->config[i].descconf); + }grub_free (dev);return NULL ;}grub-core/bus/usb/usbhub.c +8
-2@@ -96,8 +102,14 @@ grub_usb_hub_add_dev (grub_usb_controller_t controller, i, 0, 0, NULL);if (err) { -for (i =0 ; i <8 ; i++) -grub_free (dev->config[i].descconf); +for (i =0 ; i <8 ; i++) { +int currif; + +for (currif =0 ; currif < dev->config[i].descconf->numif; currif++) +grub_free (dev->config[i].interf[currif].descendp); + +grub_free (dev->config[i].descconf); + }grub_free (dev);return NULL ;}grub-core/bus/usb/usbhub.c +1
-1@@ -176,7 +188,7 @@ grub_usb_add_hub (grub_usb_device_t dev) i++) {struct grub_usb_desc_endp *endp =NULL ; - endp = &dev->config[0 ].interf[0 ].descendp[i]; + endp = dev->config[0 ].interf[0 ].descendp[i];if ((endp->endp_addr &128 ) &&grub_usb_get_ep_type (endp) ==GRUB_USB_EP_INTERRUPT )(quoted diff) +0
-0
b/grub-core/commands/usbtest.cindex2c6d93fe6..55a657635100644grub-core/commands/usbtest.c +1
-1@@ -185,7 +185,7 @@ usb_iterate (grub_usb_device_t dev, void *data __attribute__ ((unused))) for (j = 0; j < interf->endpointcnt; j++) { struct grub_usb_desc_endp *endp; - endp = &dev->config[0].interf[i].descendp[j]; + endp = dev->config[0].interf[i].descendp[j]; grub_printf ("Endpoint #%d: %s, max packed size: %d, transfer type: %s, latency: %d\n", endp->endp_addr & 15,grub-core/disk/usbms.c +1
-1@@ -184,7 +184,7 @@ grub_usbms_attach (grub_usb_device_t usbdev, int configno, int interfno) for (j = 0; j < interf->endpointcnt; j++) {struct grub_usb_desc_endp *endp; - endp = &usbdev->config[0 ].interf[interfno].descendp[j]; + endp = usbdev->config[0 ].interf[interfno].descendp[j];if ((endp->endp_addr &128 ) && (endp->attrib &3 ) ==2 )/* Bulk IN endpoint. */ (quoted diff) +0
-0
b/grub-core/term/usb_keyboard.cindexe67b8f785..d65e3474d100644grub-core/term/usb_keyboard.c +0
-0@@ -175,7 +175,7 @@ grub_usb_keyboard_attach (grub_usb_device_tusbdev, int configno, int interfno) for (j = 0; j < usbdev->config[configno].interf[interfno].descif->endpointcnt; j++)
{grub-core/term/usb_keyboard.c +1
-1- endp = &usbdev->config[configno].interf[interfno].descendp[j]; + endp = usbdev->config[configno].interf[interfno].descendp[j];if ((endp->endp_addr &128 ) &&grub_usb_get_ep_type (endp) ==GRUB_USB_EP_INTERRUPT )include/grub/usb.h +1
-1@@ -149,7 +149,7 @@ struct grub_usb_interface {struct grub_usb_desc_if *descif; -struct grub_usb_desc_endp *descendp; +struct grub_usb_desc_endp **descendp;/* A driver is handling this interface. Do we need to support multiple drivers for single interface?
include/grub/usbdesc.h +3
-1@@ -29,7 +29,8 @@ typedef enum {GRUB_USB_DESCRIPTOR_INTERFACE ,GRUB_USB_DESCRIPTOR_ENDPOINT ,GRUB_USB_DESCRIPTOR_DEBUG =10 , - GRUB_USB_DESCRIPTOR_HUB = 0x29 +GRUB_USB_DESCRIPTOR_HUB =0x29 , + GRUB_USB_DESCRIPTOR_SS_ENDPOINT_COMPANION = 0x30 } grub_usb_descriptor_t;struct grub_usb_desc @@ -105,6 +106,14 @@ struct grub_usb_desc_endpgrub_uint8_t interval; }GRUB_PACKED ; +struct grub_usb_desc_ssep {
include/grub/usbdesc.h struct grub_usb_desc_endp · line 119
Quoted 10 lines
include/grub/usbdesc.h +7
-0+grub_uint8_t length; +grub_uint8_t type; +grub_uint8_t maxburst; +grub_uint8_t attrib; +grub_uint16_t interval; +}GRUB_PACKED ; +struct grub_usb_desc_str {grub_uint8_t length;
Cheers,
Glenn
PATCH v2 2/7 usb: Add enum for xHCI
In reply to Patrick RudolphWill be used in future patches.
--- include/grub/usb.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
include/grub/usb.h +2 -1
@@ -47,7 +47,8 @@ typedef enumGRUB_USB_SPEED_NONE ,GRUB_USB_SPEED_LOW ,GRUB_USB_SPEED_FULL , - GRUB_USB_SPEED_HIGH +GRUB_USB_SPEED_HIGH , + GRUB_USB_SPEED_SUPER } grub_usb_speed_t;typedef int (*grub_usb_iterate_hook_t ) (grub_usb_device_t dev,void *data);
Signature · 1 line
2.26.2
PATCH v2 3/7 usbtrans: Set default maximum packet size
In reply to Patrick RudolphSet the maximum packet size to 512 for SuperSpeed devices.
--- grub-core/bus/usb/usbtrans.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
grub-core/bus/usb/usbtrans.c +5 -1
@@ -128,8 +128,12 @@ grub_usb_control_msg (grub_usb_device_t dev, setupdata_addr =grub_dma_get_phys (setupdata_chunk);/* Determine the maximum packet size. */ -if (dev->descdev.maxsize0) +if (dev->descdev.maxsize0 && dev->speed !=GRUB_USB_SPEED_SUPER ) max = dev->descdev.maxsize0; +else if (dev->descdev.maxsize0 && dev->speed ==GRUB_USB_SPEED_SUPER ) + max =1UL << dev->descdev.maxsize0; +else if (dev->speed ==GRUB_USB_SPEED_SUPER ) + max =512 ;else max =64 ;
Signature · 1 line
2.26.2
PATCH v2 4/7 grub-core/bus/usb: Add function pointer for attach/detach events
In reply to Patrick RudolphThe xHCI code needs to be called for attaching or detaching a device. Introduce two functions pointers and call it from the USB hub code.
Will be used in future commits, currently this doesn't change any functionality.
--- grub-core/bus/usb/ehci.c | 2 ++ grub-core/bus/usb/ohci.c | 2 ++ grub-core/bus/usb/uhci.c | 2 ++ grub-core/bus/usb/usbhub.c | 19 +++++++++++++++++++ include/grub/usb.h | 4 ++++ 5 files changed, 29 insertions(+)
grub-core/bus/usb/ehci.c +2 -0
@@ -1812,6 +1812,8 @@ static struct grub_usb_controller_dev usb_controller = { .hubports = grub_ehci_hubports, .portstatus = grub_ehci_portstatus, .detect_dev = grub_ehci_detect_dev, + .attach_dev = NULL, + .detach_dev = NULL,/* estimated max. count of TDs for one bulk transfer */ .max_bulk_tds =GRUB_EHCI_N_TD *3 /4 };
grub-core/bus/usb/ohci.c +2 -0
@@ -1440,6 +1440,8 @@ static struct grub_usb_controller_dev usb_controller = .hubports = grub_ohci_hubports, .portstatus = grub_ohci_portstatus, .detect_dev = grub_ohci_detect_dev, + .attach_dev = NULL, + .detach_dev = NULL,/* estimated max. count of TDs for one bulk transfer */ .max_bulk_tds =GRUB_OHCI_TDS *3 /4 };
grub-core/bus/usb/uhci.c +2 -0
@@ -845,6 +845,8 @@ static struct grub_usb_controller_dev usb_controller = .hubports = grub_uhci_hubports, .portstatus = grub_uhci_portstatus, .detect_dev = grub_uhci_detect_dev, + .attach_dev = NULL, + .detach_dev = NULL,/* estimated max. count of TDs for one bulk transfer */ .max_bulk_tds =N_TD *3 /4 };
grub-core/bus/usb/usbhub.c +19 -0
@@ -66,6 +66,15 @@ grub_usb_hub_add_dev (grub_usb_controller_t controller, dev->split_hubport = split_hubport; dev->split_hubaddr = split_hubaddr; +if (controller->dev->attach_dev) { + err = controller->dev->attach_dev (controller, dev); +if (err) + { +grub_free (dev); +return NULL ; + } + } + err =grub_usb_device_initialize (dev);if (err) {
@@ -405,6 +414,8 @@ static void detach_device (grub_usb_device_t dev) {unsigned i; +grub_usb_err_t err; +int k;if (!dev)return ;
@@ -425,6 +436,14 @@ detach_device (grub_usb_device_t dev)if (inter && inter->detach_hook) inter->detach_hook (dev, i, k); } +if (dev->controller.dev->detach_dev) { + err = dev->controller.dev->detach_dev (&dev->controller, dev); +if (err) + { +// XXX + } + } + grub_usb_devs[dev->addr] =0 ; }
include/grub/usb.h +4 -0
@@ -122,6 +122,10 @@ struct grub_usb_controller_devgrub_usb_speed_t (*detect_dev) (grub_usb_controller_t dev,int port,int *changed); +grub_usb_err_t (*attach_dev) (grub_usb_controller_t ctrl,grub_usb_device_t dev); + +grub_usb_err_t (*detach_dev) (grub_usb_controller_t ctrl,grub_usb_device_t dev); +/* Per controller flag - port reset pending, don't do another reset */ grub_uint64_t pending_reset;
Signature · 1 line
2.26.2
PATCH v2 5/7 grub-core/bus/usb/usbhub: Add new private fields for xHCI controller
In reply to Patrick RudolphStore the root port number, the route, consisting out of the port ID in each nibble, and a pointer to driver private data.
--- grub-core/bus/usb/usbhub.c | 14 ++++++++++---- include/grub/usb.h | 5 +++++ 2 files changed, 15 insertions(+), 4 deletions(-)
grub-core/bus/usb/usbhub.c +10 -4
@@ -49,7 +49,9 @@ static grub_usb_controller_dev_t grub_usb_list;static grub_usb_device_t grub_usb_hub_add_dev (grub_usb_controller_t controller,grub_usb_speed_t speed, -int split_hubport,int split_hubaddr) +int split_hubport,int split_hubaddr, +int root_portno, +grub_uint32_t route) {grub_usb_device_t dev;int i;
@@ -65,6 +67,8 @@ grub_usb_hub_add_dev (grub_usb_controller_t controller, dev->speed = speed; dev->split_hubport = split_hubport; dev->split_hubaddr = split_hubaddr; + dev->root_port = root_portno; + dev->route = route;if (controller->dev->attach_dev) { err = controller->dev->attach_dev (controller, dev);
@@ -245,7 +249,7 @@ attach_root_port (struct grub_usb_hub *hub, int portno,and full/low speed device connected to OHCI/UHCI needs not transaction translation - e.g. hubport and hubaddr should be always none (zero) for any device connected to any root hub. */ - dev =grub_usb_hub_add_dev (hub->controller, speed,0 ,0 ); + dev =grub_usb_hub_add_dev (hub->controller, speed,0 ,0 , portno,0 ); hub->controller->dev->pending_reset =0 ; npending--;if (! dev)
@@ -673,10 +677,12 @@ poll_nonroot_hub (grub_usb_device_t dev) split_hubport = dev->split_hubport; split_hubaddr = dev->split_hubaddr; } - + +/* Add the device and assign a device address to it. */ next_dev =grub_usb_hub_add_dev (&dev->controller, speed, - split_hubport, split_hubaddr); + split_hubport, split_hubaddr, dev->root_port, + dev->route <<4 | (i &0xf ));if (dev->controller.dev->pending_reset) { dev->controller.dev->pending_reset =0 ;
include/grub/usb.h +5 -0
@@ -233,6 +233,11 @@ struct grub_usb_deviceint split_hubport;int split_hubaddr; + +/* xHCI specific information */ +int root_port; +grub_uint32_t route; +void *xhci_priv; };
Signature · 1 line
2.26.2
PATCH v2 6/7 grub-core/bus/usb: Add xhci support
In reply to Patrick RudolphAdd support for xHCI USB controllers.
The code is based on seabios implementation, but has been heavily modified to match grubs internals.
Changes done in version 2:
* Code cleanup
* Code style fixes
* Don't leak memory buffers
* Compile without warnings
* Add more defines
* Add more helper functions
* Don't assume a 1:1 virtual to physical mapping
* Flush cachelines after writing buffers
* Don't use hardcoded page size
* Proper scratchpad register setup
* xHCI bios ownership handoff
Changes done in version 3:
* Fixed a race condition detecting events, which doesn't appear on qemu based xHCI controllers
* Don't accidently disable USB3.0 devices after first command
* Support arbitrary protocol speed IDs
* Coding style cleanup
Tested:
* Qemu system x86_64
* virtual USB HID keyboard (usb-kbd)
* virtual USB HID mass storage (usb-storage)
* init Supermicro X11SSH-F
* iKVM HID keyboard
* USB3 HID mass storage (controller root port)
* USB HID keyboard
TODO:
* Test on more hardware
* Test on USB3 hubs
* Support for USB 3.1 and USB 3.2 controllers
--- Makefile.am | 2 +- grub-core/Makefile.core.def | 7 + grub-core/bus/usb/xhci-pci.c | 195 +++ grub-core/bus/usb/xhci.c | 2496 ++++++++++++++++++++++++++++++++++ include/grub/usb.h | 4 + 5 files changed, 2703 insertions(+), 1 deletion(-) create mode 100644 grub-core/bus/usb/xhci-pci.c create mode 100644 grub-core/bus/usb/xhci.c
Makefile.am +1 -1
@@ -425,7 +425,7 @@ if COND_i386_coreboot FS_PAYLOAD_MODULES ?= $(shell cat grub-core/fs.lst) default_payload.elf: grub-mkstandalone grub-mkimage FORCE test -f $@ && rm $@ || true - pkgdatadir=. ./grub-mkstandalone --grub-mkimage=./grub-mkimage -O i386-coreboot -o $@ --modules='ahci pata ehci uhci ohci usb_keyboard usbms part_msdos ext2 fat at_keyboard part_gpt usbserial_usbdebug cbfs' --install-modules='ls linux search configfile normal cbtime cbls memrw iorw minicmd lsmmap lspci halt reboot hexdump pcidump regexp setpci lsacpi chain test serial multiboot cbmemc linux16 gzio echo help syslinuxcfg xnu $(FS_PAYLOAD_MODULES) password_pbkdf2 $(EXTRA_PAYLOAD_MODULES)' --fonts= --themes= --locales= -d grub-core/ /boot/grub/grub.cfg=$(srcdir)/coreboot.cfg + pkgdatadir=. ./grub-mkstandalone --grub-mkimage=./grub-mkimage -O i386-coreboot -o $@ --modules='ahci pata xhci ehci uhci ohci usb_keyboard usbms part_msdos ext2 fat at_keyboard part_gpt usbserial_usbdebug cbfs' --install-modules='ls linux search configfile normal cbtime cbls memrw iorw minicmd lsmmap lspci halt reboot hexdump pcidump regexp setpci lsacpi chain test serial multiboot cbmemc linux16 gzio echo help syslinuxcfg xnu $(FS_PAYLOAD_MODULES) password_pbkdf2 $(EXTRA_PAYLOAD_MODULES)' --fonts= --themes= --locales= -d grub-core/ /boot/grub/grub.cfg=$(srcdir)/coreboot.cfg endif endif
grub-core/Makefile.core.def +7 -0
@@ -644,6 +644,13 @@ module = { enable = arm_coreboot; }; +module = { + name = xhci; + common = bus/usb/xhci.c; + pci = bus/usb/xhci-pci.c; + enable = pci; +}; + module = { name = pci; common = bus/pci.c;
grub-core/bus/usb/xhci-pci.c new file +195 -0
195 lines not on this page · raw message
grub-core/bus/usb/xhci.c new file +2496 -0
2,496 lines not on this page · raw message
include/grub/usb.h +4 -0
@@ -334,6 +334,10 @@ grub_usb_cancel_transfer (grub_usb_transfer_t trans);void grub_ehci_init_device (volatile void *regs);void +grub_xhci_init_device (volatile void *regs); +void grub_ehci_pci_scan (void ); +void +grub_xhci_pci_scan (void );#endif /* GRUB_USB_H */
Signature · 1 line
2.26.2
PATCH v2 7/7 grub-core/bus/usb/usbhub: Add xHCI non root hub support
In reply to Patrick RudolphTested on Intel PCH C246, the USB3 hub can be configured by grub.
Issues:
* USB3 devices connected behind that hub are sometimes not detected.
--- grub-core/bus/usb/usbhub.c | 40 ++++++++++++++++++++++++++++++++------ include/grub/usbdesc.h | 1 + include/grub/usbtrans.h | 4 ++++ 3 files changed, 39 insertions(+), 6 deletions(-)
grub-core/bus/usb/usbhub.c +34 -6
@@ -148,19 +148,32 @@ grub_usb_hub_add_dev (grub_usb_controller_t controller,return dev; } - +static grub_usb_err_t +grub_usb_set_hub_depth (grub_usb_device_t dev,grub_uint8_t depth) +{ +return grub_usb_control_msg (dev, (GRUB_USB_REQTYPE_OUT + |GRUB_USB_REQTYPE_CLASS + |GRUB_USB_REQTYPE_TARGET_DEV ), +GRUB_USB_HUB_REQ_SET_HUB_DEPTH , depth, +0 ,0 ,NULL ); +} +static grub_usb_err_t grub_usb_add_hub (grub_usb_device_t dev) {struct grub_usb_usb_hubdesc hubdesc;grub_usb_err_t err; +grub_uint16_t req;int i; - + + req = (dev->speed ==GRUB_USB_SPEED_SUPER ) ?GRUB_USB_DESCRIPTOR_SS_HUB : +GRUB_USB_DESCRIPTOR_HUB ; + err =grub_usb_control_msg (dev, (GRUB_USB_REQTYPE_IN |GRUB_USB_REQTYPE_CLASS |GRUB_USB_REQTYPE_TARGET_DEV ), -GRUB_USB_REQ_GET_DESCRIPTOR , - (GRUB_USB_DESCRIPTOR_HUB <<8 ) |0 , +GRUB_USB_REQ_GET_DESCRIPTOR , + (req <<8 ) |0 ,0 ,sizeof (hubdesc), (char *) &hubdesc);if (err)return err;
@@ -183,6 +196,19 @@ grub_usb_add_hub (grub_usb_device_t dev)return GRUB_USB_ERR_INTERNAL ; } +if (dev->speed ==GRUB_USB_SPEED_SUPER ) + { +grub_uint8_t depth; +grub_uint32_t route; +/* Depth maximum value is 5, but root hubs doesn't count */ +for (depth =0 , route = dev->route; (route &0xf ) >0 ; route >>=4 ) + depth++; + + err =grub_usb_set_hub_depth (dev, depth); +if (err) +return err; + } +/* Power on all Hub ports. */ for (i =1 ; i <= hubdesc.portcnt; i++) {
@@ -637,7 +663,9 @@ poll_nonroot_hub (grub_usb_device_t dev)int split_hubaddr =0 ;/* Determine the device speed. */ -if (status &GRUB_USB_HUB_STATUS_PORT_LOWSPEED ) +if (dev->speed ==GRUB_USB_SPEED_SUPER ) + speed =GRUB_USB_SPEED_SUPER ; +else if (status &GRUB_USB_HUB_STATUS_PORT_LOWSPEED ) speed =GRUB_USB_SPEED_LOW ;else {
@@ -651,7 +679,7 @@ poll_nonroot_hub (grub_usb_device_t dev)grub_millisleep (10 );/* Find correct values for SPLIT hubport and hubaddr */ -if (speed ==GRUB_USB_SPEED_HIGH ) +if (speed ==GRUB_USB_SPEED_HIGH || speed ==GRUB_USB_SPEED_SUPER ) {/* HIGH speed device needs not transaction translation */ split_hubport =0 ;
include/grub/usbdesc.h +1 -0
@@ -30,6 +30,7 @@ typedef enum {GRUB_USB_DESCRIPTOR_ENDPOINT ,GRUB_USB_DESCRIPTOR_DEBUG =10 ,GRUB_USB_DESCRIPTOR_HUB =0x29 , +GRUB_USB_DESCRIPTOR_SS_HUB =0x2a , GRUB_USB_DESCRIPTOR_SS_ENDPOINT_COMPANION = 0x30 } grub_usb_descriptor_t;
include/grub/usbtrans.h +4 -0
@@ -110,6 +110,10 @@ enumGRUB_USB_REQ_SET_INTERFACE =0x0B , GRUB_USB_REQ_SYNC_FRAME = 0x0C }; +enum + { +GRUB_USB_HUB_REQ_SET_HUB_DEPTH =0x0C , + };#define GRUB_USB_FEATURE_ENDP_HALT 0x00#define GRUB_USB_FEATURE_DEV_REMOTE_WU 0x01
Signature · 1 line
2.26.2
Hi Patrick,
Thanks for the contribution. I think this would be a great addition to GRUB. However, there are a few issues I see at the moment.
Quoted 7 lines · On Mon, 7 Dec 2020 08:41:20 +0100 Patrick Rudolph…
Add basic support for xHCI USB controllers and non root xHCI hubs. The motivation is to use this code on platforms that do not provide user input by runtime services (like BIOS or UEFI platform) do. This is the case when GRUB is used as coreboot payload for example.
The code is based on seabios implementation, but has been heavily modified to match grubs internals.
Are these the same changes as in https://github.com/Nitrokey/grub.git
referenced in a previous email? I'm asking because there were some comments that were unaddressed. So, if it is, could you please address Thomas's email (I haven't checked to see if they are still relevant if so)? (see: https://marc.info/?l=grub-devel&m=159769164332293)
Searching in the mailing list, I found this attempt in early 2017. Having not looked at the code at all, could this help in addressing some of Thomas's concerns?
https://github.com/bjornfor/grub/tree/add-coreboot-xhci-driver-2nd-attempt-v2
I've run your code through the GitLab CI I've configured, and there was a build failure on x86_64-efi. Looks like some implicit type casting issues. I believe it failed for both gcc 8.1 and 10.1. Here is the error log when using gcc 10.1:
https://gitlab.com/gwashburn/grub/-/jobs/1025317154#L594
Also, I think that any xhci support should be accompanied by passing make check tests. See uhci_test for an example of how this might be done. Since you've already tested on qemu, I think you're 90% of the way there in making some tests. Both of the ones listed before would be nice. If you'd like some guidance or help, let me know.
Glenn
Quoted 66 lines
Changes done in version 2:
* Code cleanup
* Code style fixes
* Don't leak memory buffers
* Compile without warnings
* Add more defines
* Add more helper functions
* Don't assume a 1:1 virtual to physical mapping
* Flush cachelines after writing buffers
* Don't use hardcoded page size
* Proper scratchpad register setup
* xHCI bios ownership handoffChanges done in version 3:
* Several bug fixes for real hardware
* Fixed a race condition detecting events, which doesn't appear on qemu based xHCI controllers
* Don't accidently disable USB3.0 devices after first command
* Support arbitrary protocol speed IDs
* Coding style cleanup
* Support for non root SuperSpeed hubsChanges Tested:
* Qemu systemx86_64
* virtual USB HID keyboard (usb-kbd)
* virtual USB HID mass storage (usb-storage)
* Intel C246 integrated xHCI (Supermicro X11SSH-F)
* iKVM HID keyboard
* USB3 HID mass storage (controller root port)
* External USB HID keyboardTODO:
* Test on more hardware
* Test on USB3 hubs
* Support for USB 3.1 and USB 3.2 controllersPatrick Rudolph (7):
grub-core/bus/usb: Parse SuperSpeed companion descriptors
usb: Add enum for xHCI
usbtrans: Set default maximum packet size grub-core/bus/usb: Add function pointer for attach/detach events grub-core/bus/usb/usbhub: Add new private fields for xHCI controller grub-core/bus/usb: Add xhci support grub-core/bus/usb/usbhub: Add xHCI non root hub supportMakefile.am | 2 +- grub-core/Makefile.core.def | 7 + grub-core/bus/usb/ehci.c | 2 + grub-core/bus/usb/ohci.c | 2 + grub-core/bus/usb/serial/common.c | 2 +- grub-core/bus/usb/uhci.c | 2 + grub-core/bus/usb/usb.c | 44 +- grub-core/bus/usb/usbhub.c | 95 +- grub-core/bus/usb/usbtrans.c | 6 +- grub-core/bus/usb/xhci-pci.c | 195 +++ grub-core/bus/usb/xhci.c | 2496 +++++++++++++++++++++++++++++ grub-core/commands/usbtest.c | 2 +- grub-core/disk/usbms.c | 2 +- grub-core/term/usb_keyboard.c | 2 +- include/grub/usb.h | 18 +- include/grub/usbdesc.h | 12 +- include/grub/usbtrans.h | 4 + 17 files changed, 2852 insertions(+), 41 deletions(-) create mode 100644 grub-core/bus/usb/xhci-pci.c create mode 100644 grub-core/bus/usb/xhci.c
Hi Glenn,
yes it's the same patch series, but has been cleaned and improved a lot. I've addressed most of the comments received earlier.
As stated in the commit message, USB 3.1 and USB 3.2 are not tested, as I lack hardware to test this. I'm not going to look into this any further into this as it works fine with USB 3.0 compliant xhci controllers.
I'l fix the remaining issues and publish a new patch series.
Kind regards,
Patrick Rudolph
Quoted 108 lines · Glenn Washburn
Hi Patrick,
Thanks for the contribution. I think this would be a great addition to GRUB. However, there are a few issues I see at the moment.
Quoted 7 lines · On Mon, 7 Dec 2020 08:41:20 +0100 Patrick Rudolph…
Add basic support for xHCI USB controllers and non root xHCI hubs. The motivation is to use this code on platforms that do not provide user input by runtime services (like BIOS or UEFI platform) do. This is the case when GRUB is used as coreboot payload for example.
The code is based on seabios implementation, but has been heavily modified to match grubs internals.
Are these the same changes as in https://github.com/Nitrokey/grub.git
referenced in a previous email? I'm asking because there were some comments that were unaddressed. So, if it is, could you please address Thomas's email (I haven't checked to see if they are still relevant if so)? (see: https://marc.info/?l=grub-devel&m=159769164332293)Searching in the mailing list, I found this attempt in early 2017. Having not looked at the code at all, could this help in addressing some of Thomas's concerns?
https://github.com/bjornfor/grub/tree/add-coreboot-xhci-driver-2nd-attempt-v2I've run your code through the GitLab CI I've configured, and there was a build failure on x86_64-efi. Looks like some implicit type casting issues. I believe it failed for both gcc 8.1 and 10.1. Here is the error log when using gcc 10.1:
https://gitlab.com/gwashburn/grub/-/jobs/1025317154#L594Also, I think that any xhci support should be accompanied by passing make check tests. See
uhci_testfor an example of how this might be done. Since you've already tested on qemu, I think you're 90% of the way there in making some tests. Both of the ones listed before would be nice. If you'd like some guidance or help, let me know.Glenn
Quoted 66 lines
Changes done in version 2:
* Code cleanup
* Code style fixes
* Don't leak memory buffers
* Compile without warnings
* Add more defines
* Add more helper functions
* Don't assume a 1:1 virtual to physical mapping
* Flush cachelines after writing buffers
* Don't use hardcoded page size
* Proper scratchpad register setup
* xHCI bios ownership handoffChanges done in version 3:
* Several bug fixes for real hardware
* Fixed a race condition detecting events, which doesn't appear on qemu based xHCI controllers
* Don't accidently disable USB3.0 devices after first command
* Support arbitrary protocol speed IDs
* Coding style cleanup
* Support for non root SuperSpeed hubsChanges Tested:
* Qemu systemx86_64
* virtual USB HID keyboard (usb-kbd)
* virtual USB HID mass storage (usb-storage)
* Intel C246 integrated xHCI (Supermicro X11SSH-F)
* iKVM HID keyboard
* USB3 HID mass storage (controller root port)
* External USB HID keyboardTODO:
* Test on more hardware
* Test on USB3 hubs
* Support for USB 3.1 and USB 3.2 controllersPatrick Rudolph (7):
grub-core/bus/usb: Parse SuperSpeed companion descriptors
usb: Add enum for xHCI
usbtrans: Set default maximum packet size grub-core/bus/usb: Add function pointer for attach/detach events grub-core/bus/usb/usbhub: Add new private fields for xHCI controller grub-core/bus/usb: Add xhci support grub-core/bus/usb/usbhub: Add xHCI non root hub supportMakefile.am | 2 +- grub-core/Makefile.core.def | 7 + grub-core/bus/usb/ehci.c | 2 + grub-core/bus/usb/ohci.c | 2 + grub-core/bus/usb/serial/common.c | 2 +- grub-core/bus/usb/uhci.c | 2 + grub-core/bus/usb/usb.c | 44 +- grub-core/bus/usb/usbhub.c | 95 +- grub-core/bus/usb/usbtrans.c | 6 +- grub-core/bus/usb/xhci-pci.c | 195 +++ grub-core/bus/usb/xhci.c | 2496 +++++++++++++++++++++++++++++ grub-core/commands/usbtest.c | 2 +- grub-core/disk/usbms.c | 2 +- grub-core/term/usb_keyboard.c | 2 +- include/grub/usb.h | 18 +- include/grub/usbdesc.h | 12 +- include/grub/usbtrans.h | 4 + 17 files changed, 2852 insertions(+), 41 deletions(-) create mode 100644 grub-core/bus/usb/xhci-pci.c create mode 100644 grub-core/bus/usb/xhci.c
Good day Patrick,
Quoted 3 lines · On Wed, 24 Feb 2021 07:46:48 +0100 Patrick Rudolph…
Hi Glenn,
yes it's the same patch series, but has been cleaned and improved a lot. I've addressed most of the comments received earlier.
I see that I implied that you hadn't and I didn't mean to. I only meant to suggest that there were possible unaddressed issues. Glad to hear those were already taken care of.
Quoted 6 lines
As stated in the commit message, USB 3.1 and USB 3.2 are not tested, as I lack hardware to test this. I'm not going to look into this any further into this as it works fine with USB 3.0 compliant xhci controllers.
I'l fix the remaining issues and publish a new patch series.
Great. Are you thinking of adding in some tests? If not, I would appreciate knowing the qemu opts used to create the virtual machine (I'm unsure how to turn on xHCI in qemu).
Glenn
Quoted 113 lines
Quoted 110 lines · Glenn Washburn
Hi Patrick,
Thanks for the contribution. I think this would be a great addition to GRUB. However, there are a few issues I see at the moment.
Quoted 8 lines · On Mon, 7 Dec 2020 08:41:20 +0100 Patrick Rudolph…
Add basic support for xHCI USB controllers and non root xHCI hubs. The motivation is to use this code on platforms that do not provide user input by runtime services (like BIOS or UEFI platform) do. This is the case when GRUB is used as coreboot payload for example.
The code is based on seabios implementation, but has been heavily modified to match grubs internals.
Are these the same changes as in
https://github.com/Nitrokey/grub.git referenced in a previous email? I'm asking because there were some comments that were unaddressed. So, if it is, could you please address Thomas's email (I haven't checked to see if they are still relevant if so)? (see:
https://marc.info/?l=grub-devel&m=159769164332293)Searching in the mailing list, I found this attempt in early 2017. Having not looked at the code at all, could this help in addressing some of Thomas's concerns?
https://github.com/bjornfor/grub/tree/add-coreboot-xhci-driver-2nd-attempt-v2I've run your code through the GitLab CI I've configured, and there was a build failure on x86_64-efi. Looks like some implicit type casting issues. I believe it failed for both gcc 8.1 and 10.1. Here is the error log when using gcc 10.1:
https://gitlab.com/gwashburn/grub/-/jobs/1025317154#L594Also, I think that any xhci support should be accompanied by passing make check tests. See
uhci_testfor an example of how this might be done. Since you've already tested on qemu, I think you're 90% of the way there in making some tests. Both of the ones listed before would be nice. If you'd like some guidance or help, let me know.Glenn
Quoted 66 lines
Changes done in version 2:
* Code cleanup
* Code style fixes
* Don't leak memory buffers
* Compile without warnings
* Add more defines
* Add more helper functions
* Don't assume a 1:1 virtual to physical mapping
* Flush cachelines after writing buffers
* Don't use hardcoded page size
* Proper scratchpad register setup
* xHCI bios ownership handoffChanges done in version 3:
* Several bug fixes for real hardware
* Fixed a race condition detecting events, which doesn't appear
on qemu based xHCI controllers
* Don't accidently disable USB3.0 devices after first command
* Support arbitrary protocol speed IDs
* Coding style cleanup
* Support for non root SuperSpeed hubsChanges Tested:
* Qemu systemx86_64
* virtual USB HID keyboard (usb-kbd)
* virtual USB HID mass storage (usb-storage)
* Intel C246 integrated xHCI (Supermicro X11SSH-F)
* iKVM HID keyboard
* USB3 HID mass storage (controller root port)
* External USB HID keyboardTODO:
* Test on more hardware
* Test on USB3 hubs
* Support for USB 3.1 and USB 3.2 controllersPatrick Rudolph (7):
grub-core/bus/usb: Parse SuperSpeed companion descriptors
usb: Add enum for xHCI
usbtrans: Set default maximum packet size grub-core/bus/usb: Add function pointer for attach/detach events grub-core/bus/usb/usbhub: Add new private fields for xHCI
controller grub-core/bus/usb: Add xhci support
grub-core/bus/usb/usbhub: Add xHCI non root hub supportMakefile.am | 2 +- grub-core/Makefile.core.def | 7 + grub-core/bus/usb/ehci.c | 2 + grub-core/bus/usb/ohci.c | 2 + grub-core/bus/usb/serial/common.c | 2 +- grub-core/bus/usb/uhci.c | 2 + grub-core/bus/usb/usb.c | 44 +- grub-core/bus/usb/usbhub.c | 95 +- grub-core/bus/usb/usbtrans.c | 6 +- grub-core/bus/usb/xhci-pci.c | 195 +++ grub-core/bus/usb/xhci.c | 2496 +++++++++++++++++++++++++++++ grub-core/commands/usbtest.c | 2 +- grub-core/disk/usbms.c | 2 +- grub-core/term/usb_keyboard.c | 2 +- include/grub/usb.h | 18 +- include/grub/usbdesc.h | 12 +- include/grub/usbtrans.h | 4 + 17 files changed, 2852 insertions(+), 41 deletions(-) create mode 100644 grub-core/bus/usb/xhci-pci.c create mode 100644 grub-core/bus/usb/xhci.c