libpayload: usb: Fix up usb_shutdown() code paths

This patch combines a few minor fixes and refactoring to the various
host controller and root hub drivers to ensure they all do the right
thing on a call to usb_exit(). It puts a usb_detach_device(0) call
into detach_controller() so that the HCD doesn't need to remember to
tear down the root hub itself, and makes sure all root hubs properly
detach the subtree of devices connected to their ports first (as
generic_hub and by extension XHCI had already been doing).

It also fixes up some missing free() calls and replaces most 'ptr =
malloc(); if (!ptr) fatal()' idioms with the new x(z)alloc().

BUG=chromium:343415
TEST=Tested EHCI on Big and OHCI, EHCI, and XHCI on Snow. Could not test
UHCI (unless anyone volunteers to port coreboot to a ZGB? ;) ), but the
changes are really tame.

Original-Change-Id: I6eca51ff2685d0946fe4267ad7d3ec48ad7fc510
Original-Signed-off-by: Julius Werner <jwerner@chromium.org>
Original-Reviewed-on: https://chromium-review.googlesource.com/193731
Original-Reviewed-by: Stefan Reinauer <reinauer@chromium.org>
(cherry picked from commit 5791b546e5a21a360d0c65888a5b92d5f48f8178)
Signed-off-by: Marc Jones <marc.jones@se-eng.com>

Change-Id: I00138f0aeceb12ed721f7368c7788c9b6bee227d
Reviewed-on: http://review.coreboot.org/7222
Tested-by: build bot (Jenkins)
Reviewed-by: David Hendricks <dhendrix@chromium.org>
diff --git a/payloads/libpayload/drivers/usb/xhci.c b/payloads/libpayload/drivers/usb/xhci.c
index 2331485..c60a967 100644
--- a/payloads/libpayload/drivers/usb/xhci.c
+++ b/payloads/libpayload/drivers/usb/xhci.c
@@ -150,11 +150,6 @@
 	/* First, allocate and initialize static controller structures */
 
 	hci_t *const controller = new_controller();
-	if (!controller) {
-		xhci_debug("Could not create USB controller instance\n");
-		return controller;
-	}
-
 	controller->type		= XHCI;
 	controller->start		= xhci_start;
 	controller->stop		= xhci_stop;
@@ -170,17 +165,10 @@
 	controller->destroy_intr_queue	= xhci_destroy_intr_queue;
 	controller->poll_intr_queue	= xhci_poll_intr_queue;
 	controller->pcidev		= 0;
-	for (i = 0; i < 128; ++i) {
-		controller->devices[i] = NULL;
-	}
 
-	controller->instance = malloc(sizeof(xhci_t));
-	if (!controller->instance) {
-		xhci_debug("Out of memory creating xHCI controller instance\n");
-		goto _free_controller;
-	}
+	controller->reg_base = (u32)physical_bar;
+	controller->instance = xzalloc(sizeof(xhci_t));
 	xhci_t *const xhci = (xhci_t *)controller->instance;
-	memset(xhci, 0x00, sizeof(*xhci));
 
 	init_device_entry(controller, 0);
 	xhci->roothub = controller->devices[0];
@@ -282,6 +270,7 @@
 	return controller;
 
 _free_xhci_structs:
+	free(xhci->dma_buffer);
 	if (xhci->sp_ptrs) {
 		for (i = 0; i < max_sp_bufs; ++i) {
 			if (xhci->sp_ptrs[i])
@@ -410,13 +399,10 @@
 
 	if (controller == 0)
 		return;
-	xhci_t *const xhci = XHCI_INST(controller);
 
 	detach_controller(controller);
 
-	/* Detach device hierarchy (starting at root hub) */
-	usb_detach_device(controller, 0);
-
+	xhci_t *const xhci = XHCI_INST(controller);
 	xhci_stop(controller);
 
         if (controller->pcidev)
@@ -430,6 +416,7 @@
 		}
 	}
 	free(xhci->sp_ptrs);
+	free(xhci->dma_buffer);
 	free(xhci->dcbaa);
 	free(xhci->dev);
 	free((void *)xhci->ev_ring_table);