git: 866760ff8338 - main - mps(4): add support for hostboot

From: Allan Jude <allanjude_at_FreeBSD.org>
Date: Mon, 16 Nov 2026 01:26:53 UTC
The branch main has been updated by allanjude:

URL: https://cgit.FreeBSD.org/src/commit/?id=866760ff8338a307b10470ea85b5df5ad9fccf6a

commit 866760ff8338a307b10470ea85b5df5ad9fccf6a
Author:     Allan Jude <allanjude@FreeBSD.org>
AuthorDate: 2026-11-16 01:26:10 +0000
Commit:     Allan Jude <allanjude@FreeBSD.org>
CommitDate: 2026-11-16 01:26:10 +0000

    mps(4): add support for hostboot
    
    If the mps/mpr(4) device has corrupted or missing firmware then
    it will fail to attach.
    
    Add an interface to use firmware(9) to load the firmware at boot.
    
    Loosely based on https://github.com/marcan/lsirec/
    
    Assisted-by:    Claude Code (Opus 5)
    Reviewed by:    imp
    Sponsored by:   Klara, Inc.
    Differential Revision:  https://reviews.freebsd.org/D59854
---
 share/man/man4/mpr.4 |  45 ++++++++-
 share/man/man4/mps.4 |  45 ++++++++-
 sys/dev/mpr/mpr.c    | 270 +++++++++++++++++++++++++++++++++++++++++++++++----
 sys/dev/mpr/mprvar.h |   2 +
 sys/dev/mps/mps.c    | 270 +++++++++++++++++++++++++++++++++++++++++++++++----
 sys/dev/mps/mpsvar.h |   2 +
 6 files changed, 595 insertions(+), 39 deletions(-)

diff --git a/share/man/man4/mpr.4 b/share/man/man4/mpr.4
index a88b99ae007b..effbd94c1cc8 100644
--- a/share/man/man4/mpr.4
+++ b/share/man/man4/mpr.4
@@ -37,7 +37,7 @@
 .\"
 .\" $Id$
 .\"
-.Dd September 28, 2025
+.Dd November 15, 2026
 .Dt MPR 4
 .Os
 .Sh NAME
@@ -340,6 +340,48 @@ dev.mpr.X.use_phy_num
 .Ed
 .Pp
 The same set of values are valid as when setting this tunable for all adapters.
+.Sh FIRMWARE
+If a controller's flash firmware is corrupt or missing,
+.Nm
+may fail to attach to the controller.
+As a recovery mechanism, a firmware image can be preloaded by the boot loader
+and staged in host memory, then the driver will boot it directly from that
+image, bypassing the device flash entirely.
+The preloaded image itself is not written to the device flash.
+.Pp
+To configure a recovery image for adapter unit X, choose a variable name
+prefix that is not used by any other preloaded file
+.Po
+it need not match anything else, it only has to be unique
+.Pc .
+It is the
+.Va prefix_type
+value that associates the image with adapter unit X, not the prefix itself,
+so a distinct prefix is needed for each preloaded image.
+For example, to configure separate recovery images for
+.Nm mpr0
+and
+.Nm mpr1 ,
+set these variables in
+.Xr loader.conf 5 :
+.Pp
+.Bd -literal -offset indent
+mpr0fw_load="YES"
+mpr0fw_name="/boot/firmware/mpr0-fw.bin"
+mpr0fw_type="mpr0:firmware"
+
+mpr1fw_load="YES"
+mpr1fw_name="/boot/firmware/mpr1-fw.bin"
+mpr1fw_type="mpr1:firmware"
+.Ed
+.Pp
+The same hostboot recovery can be triggered manually after boot, without
+editing
+.Xr loader.conf 5 ,
+via
+.Xr mprutil 8 Ns 's
+.Cm hostboot
+command.
 .Sh DEBUGGING
 Driver diagnostic printing is controlled in
 .Xr loader.conf 5
@@ -386,6 +428,7 @@ The following levels are available:
 .Xr cd 4 ,
 .Xr ch 4 ,
 .Xr da 4 ,
+.Xr firmware 9 ,
 .Xr mps 4 ,
 .Xr mpt 4 ,
 .Xr pci 4 ,
diff --git a/share/man/man4/mps.4 b/share/man/man4/mps.4
index 5bcb55a54599..ae971957a1a9 100644
--- a/share/man/man4/mps.4
+++ b/share/man/man4/mps.4
@@ -37,7 +37,7 @@
 .\"
 .\" $Id: //depot/SpectraBSD/head/share/man/man4/mps.4#6 $
 .\"
-.Dd June 1, 2019
+.Dd November 15, 2026
 .Dt MPS 4
 .Os
 .Sh NAME
@@ -310,6 +310,48 @@ dev.mps.X.use_phy_num
 .Ed
 .Pp
 The same set of values are valid as when setting this tunable for all adapters.
+.Sh FIRMWARE
+If a controller's flash firmware is corrupt or missing,
+.Nm
+may fail to attach to the controller.
+As a recovery mechanism, a firmware image can be preloaded by the boot loader
+and staged in host memory, then the driver will boot it directly from that
+image, bypassing the device flash entirely.
+The preloaded image itself is not written to the device flash.
+.Pp
+To configure a recovery image for adapter unit X, choose a variable name
+prefix that is not used by any other preloaded file
+.Po
+it need not match anything else, it only has to be unique
+.Pc .
+It is the
+.Va prefix_type
+value that associates the image with adapter unit X, not the prefix itself,
+so a distinct prefix is needed for each preloaded image.
+For example, to configure separate recovery images for
+.Nm mps0
+and
+.Nm mps1 ,
+set these variables in
+.Xr loader.conf 5 :
+.Pp
+.Bd -literal -offset indent
+mps0fw_load="YES"
+mps0fw_name="/boot/firmware/mps0-fw.bin"
+mps0fw_type="mps0:firmware"
+
+mps1fw_load="YES"
+mps1fw_name="/boot/firmware/mps1-fw.bin"
+mps1fw_type="mps1:firmware"
+.Ed
+.Pp
+The same hostboot recovery can be triggered manually after boot, without
+editing
+.Xr loader.conf 5 ,
+via
+.Xr mpsutil 8 Ns 's
+.Cm hostboot
+command.
 .Sh DEBUGGING
 Driver diagnostic printing is controlled in
 .Xr loader.conf 5
@@ -356,6 +398,7 @@ The following levels are available:
 .Xr cd 4 ,
 .Xr ch 4 ,
 .Xr da 4 ,
+.Xr firmware 9 ,
 .Xr mpr 4 ,
 .Xr mpt 4 ,
 .Xr pci 4 ,
diff --git a/sys/dev/mpr/mpr.c b/sys/dev/mpr/mpr.c
index 9bceabe637b5..45669d1eba42 100644
--- a/sys/dev/mpr/mpr.c
+++ b/sys/dev/mpr/mpr.c
@@ -1,4 +1,6 @@
 /*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
  * Copyright (c) 2009 Yahoo! Inc.
  * Copyright (c) 2011-2015 LSI Corp.
  * Copyright (c) 2013-2016 Avago Technologies
@@ -55,6 +57,7 @@
 #include <sys/eventhandler.h>
 #include <sys/sbuf.h>
 #include <sys/priv.h>
+#include <sys/linker.h>
 
 #include <machine/bus.h>
 #include <machine/resource.h>
@@ -81,6 +84,8 @@
 #include <dev/mpr/mpr_sas.h>
 
 static int mpr_diag_reset(struct mpr_softc *sc, int sleep_flag);
+static int mpr_diag_unlock(struct mpr_softc *sc, int sleep_flag);
+static int mpr_hostboot_from_preload(struct mpr_softc *sc);
 static int mpr_init_queues(struct mpr_softc *sc);
 static void mpr_resize_queues(struct mpr_softc *sc);
 static int mpr_message_unit_reset(struct mpr_softc *sc, int sleep_flag);
@@ -115,6 +120,12 @@ SYSCTL_NODE(_hw, OID_AUTO, mpr, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
 
 MALLOC_DEFINE(M_MPR, "mpr", "mpr driver memory");
 
+/*
+ * The "Host Code and Data Window" (HCDW/HCB) the image is staged in is
+ * a fixed 2MB window.
+ */
+#define	MPR_HCB_SIZE			(2 * 1024 * 1024)
+
 /*
  * Do a "Diagnostic Reset" aka a hard reset.  This should get the chip out of
  * any state and back to its initialization state machine.
@@ -148,26 +159,11 @@ static struct timeval mpr_chainfail_interval = { 60, 0 };
  * is not hold by driver.
  */
 static int
-mpr_diag_reset(struct mpr_softc *sc,int sleep_flag)
+mpr_diag_unlock(struct mpr_softc *sc, int sleep_flag)
 {
 	uint32_t reg;
 	int i, error, tries = 0;
-	uint8_t first_wait_done = FALSE;
-
-	mpr_dprint(sc, MPR_INIT, "%s entered\n", __func__);
-
-	/* Clear any pending interrupts */
-	mpr_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
-
-	/*
-	 * Force NO_SLEEP for threads prohibited to sleep
- 	 * e.a Thread from interrupt handler are prohibited to sleep.
- 	 */
-	if (curthread->td_no_sleeping)
-		sleep_flag = NO_SLEEP;
 
-	mpr_dprint(sc, MPR_INIT, "sequence start, sleep_flag=%d\n", sleep_flag);
-	/* Push the magic sequence */
 	error = ETIMEDOUT;
 	while (tries++ < 20) {
 		for (i = 0; i < sizeof(mpt2_reset_magic); i++)
@@ -189,13 +185,40 @@ mpr_diag_reset(struct mpr_softc *sc,int sleep_flag)
 			break;
 		}
 	}
+
+	return (error);
+}
+
+static int
+mpr_diag_reset(struct mpr_softc *sc,int sleep_flag)
+{
+	uint32_t reg;
+	int i, error;
+	uint8_t first_wait_done = FALSE;
+
+	mpr_dprint(sc, MPR_INIT, "%s entered\n", __func__);
+
+	/* Clear any pending interrupts */
+	mpr_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
+
+	/*
+	 * Force NO_SLEEP for threads prohibited to sleep
+	 * e.a Thread from interrupt handler are prohibited to sleep.
+	 */
+	if (curthread->td_no_sleeping)
+		sleep_flag = NO_SLEEP;
+
+	mpr_dprint(sc, MPR_INIT, "sequence start, sleep_flag=%d\n", sleep_flag);
+	/* Push the magic sequence */
+	error = mpr_diag_unlock(sc, sleep_flag);
 	if (error) {
 		mpr_dprint(sc, MPR_INIT, "sequence failed, error=%d, exit\n",
 		    error);
 		return (error);
 	}
 
-	/* Send the actual reset.  XXX need to refresh the reg? */
+	/* Send the actual reset. */
+	reg = mpr_regread(sc, MPI2_HOST_DIAGNOSTIC_OFFSET);
 	reg |= MPI2_DIAG_RESET_ADAPTER;
 	mpr_dprint(sc, MPR_INIT, "sequence success, sending reset, reg= 0x%x\n",
 	    reg);
@@ -923,7 +946,209 @@ mpr_reinit(struct mpr_softc *sc)
 	return 0;
 }
 
-/* Wait for the chip to ACK a word that we've put into its FIFO 
+/*
+ * Boot the IOC directly from a firmware image staged in host memory,
+ * bypassing flash.  This is a recovery mechanism for an IOC whose flash
+ * firmware is corrupt or missing and can't otherwise be brought up.
+ * It forces the IOC into HCB (Host Code Boot) mode across a diag reset,
+ * points the HCDW (Host Code Data Window) at a DMA buffer holding the
+ * firmware image, then selects HCDW as the boot device and release the IOC
+ * from reset.
+ */
+int
+mpr_hostboot(struct mpr_softc *sc, uint8_t *fw_image, uint32_t fw_len)
+{
+	bus_dma_template_t t;
+	bus_dma_tag_t hcb_dmat;
+	bus_dmamap_t hcb_map;
+	uint8_t *hcb_buf;
+	bus_addr_t hcb_busaddr;
+	uint32_t reg;
+	int i, error, sleep_flag;
+
+	sleep_flag = (sc->mpr_flags & MPR_FLAGS_ATTACH_DONE)
+	    ? CAN_SLEEP : NO_SLEEP;
+
+	if (curthread->td_no_sleeping != 0)
+		sleep_flag = NO_SLEEP;
+
+	MPR_FUNCTRACE(sc);
+	mtx_assert(&sc->mpr_mtx, MA_OWNED);
+
+	if (fw_len > MPR_HCB_SIZE) {
+		mpr_dprint(sc, MPR_FAULT, "%s: firmware image (%u bytes) is "
+		    "larger than the %u byte HCB window\n", __func__, fw_len,
+		    MPR_HCB_SIZE);
+		return (EFBIG);
+	}
+
+	bus_dma_template_init(&t, sc->mpr_parent_dmat);
+	BUS_DMA_TEMPLATE_FILL(&t, BD_ALIGNMENT(PAGE_SIZE),
+	    BD_MAXSIZE(MPR_HCB_SIZE), BD_MAXSEGSIZE(MPR_HCB_SIZE),
+	    BD_NSEGMENTS(1), BD_LOWADDR(BUS_SPACE_MAXADDR_32BIT));
+	if (bus_dma_template_tag(&t, &hcb_dmat)) {
+		mpr_dprint(sc, MPR_ERROR, "%s: cannot allocate HCB DMA tag\n",
+		    __func__);
+		return (ENOMEM);
+	}
+	if (bus_dmamem_alloc(hcb_dmat, (void **)&hcb_buf, BUS_DMA_NOWAIT,
+	    &hcb_map)) {
+		mpr_dprint(sc, MPR_ERROR, "%s: cannot allocate HCB memory\n",
+		    __func__);
+		bus_dma_tag_destroy(hcb_dmat);
+		return (ENOMEM);
+	}
+	bus_dmamap_load(hcb_dmat, hcb_map, hcb_buf, MPR_HCB_SIZE,
+	    mpr_memaddr_cb, &hcb_busaddr, 0);
+
+	/* Right-justify the image at the end of the window. */
+	bzero(hcb_buf, MPR_HCB_SIZE);
+	bcopy(fw_image, hcb_buf + MPR_HCB_SIZE - fw_len, fw_len);
+	bus_dmamap_sync(hcb_dmat, hcb_map, BUS_DMASYNC_PREWRITE);
+
+	mpr_dprint(sc, MPR_INIT|MPR_INFO, "hostboot: HCB busaddr=%#016jx "
+	    "size=%d fw_len=%d\n", (uintmax_t)hcb_busaddr, MPR_HCB_SIZE,
+	    fw_len);
+
+	mpr_mask_intr(sc);
+	mpr_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
+
+	/* Force HCB mode across a diag reset. */
+	mpr_dprint(sc, MPR_INIT, "hostboot: resetting adapter in HCB mode\n");
+	reg = mpr_regread(sc, MPI2_HOST_DIAGNOSTIC_OFFSET);
+	reg |= MPI2_DIAG_FORCE_HCB_ON_RESET;
+	mpr_regwrite(sc, MPI2_HOST_DIAGNOSTIC_OFFSET, reg);
+	reg |= MPI2_DIAG_RESET_ADAPTER;
+	mpr_regwrite(sc, MPI2_HOST_DIAGNOSTIC_OFFSET, reg);
+
+	DELAY(1000000);
+
+	/* The reset re-locks the diag register; unlock it again. */
+	error = mpr_diag_unlock(sc, sleep_flag);
+	if (error != 0) {
+		mpr_dprint(sc, MPR_FAULT, "hostboot: failed to re-unlock "
+		    "diag write access after HCB reset, error= %d\n", error);
+		goto out;
+	}
+
+	reg = mpr_regread(sc, MPI2_HOST_DIAGNOSTIC_OFFSET);
+	reg &= ~MPI2_DIAG_FORCE_HCB_ON_RESET;
+	mpr_regwrite(sc, MPI2_HOST_DIAGNOSTIC_OFFSET, reg);
+
+	reg = mpr_regread(sc, MPI2_DOORBELL_OFFSET);
+	if ((reg & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_RESET) {
+		mpr_dprint(sc, MPR_FAULT, "hostboot: IOC failed to stay in "
+		    "reset, Doorbell= 0x%x\n", reg);
+		error = EIO;
+		goto out;
+	}
+
+	/* Prep the diag register for an HCDW boot. */
+	reg = mpr_regread(sc, MPI2_HOST_DIAGNOSTIC_OFFSET);
+	reg |= MPI2_DIAG_CLEAR_FLASH_BAD_SIG;
+	reg &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
+	reg &= ~MPI2_DIAG_FORCE_HCB_ON_RESET;
+	reg &= ~MPI2_DIAG_RESET_HISTORY;
+	mpr_regwrite(sc, MPI2_HOST_DIAGNOSTIC_OFFSET, reg);
+
+	/* Point the HCB window at the DMA buffer. */
+	mpr_regwrite(sc, MPI2_HCB_SIZE_OFFSET,
+	    (MPI2_HCB_SIZE_SIZE_MASK & ~(MPR_HCB_SIZE - 1)) |
+	    MPI2_HCB_SIZE_HCB_ENABLE);
+	mpr_regwrite(sc, MPI2_HCB_ADDRESS_LOW_OFFSET,
+	    (uint32_t)(hcb_busaddr & 0xffffffff));
+	mpr_regwrite(sc, MPI2_HCB_ADDRESS_HIGH_OFFSET,
+	    (uint32_t)(hcb_busaddr >> 32));
+
+	/* Select HCDW as the boot device, then release the IOC from reset. */
+	mpr_dprint(sc, MPR_INIT, "hostboot: booting IOC from host memory\n");
+	reg = mpr_regread(sc, MPI2_HOST_DIAGNOSTIC_OFFSET);
+	reg |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
+	mpr_regwrite(sc, MPI2_HOST_DIAGNOSTIC_OFFSET, reg);
+
+	reg &= ~MPI2_DIAG_HOLD_IOC_RESET;
+	reg &= ~MPI2_DIAG_FORCE_HCB_ON_RESET;
+	mpr_regwrite(sc, MPI2_HOST_DIAGNOSTIC_OFFSET, reg);
+
+	error = ETIMEDOUT;
+	for (i = 0; i < 200; i++) {
+		DELAY(10000);
+		reg = mpr_regread(sc, MPI2_DOORBELL_OFFSET);
+		if ((reg & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY) {
+			error = 0;
+			break;
+		}
+	}
+
+	if (error != 0) {
+		mpr_dprint(sc, MPR_FAULT, "hostboot: IOC failed to become "
+		    "ready, Doorbell= 0x%x\n", reg);
+		goto out;
+	}
+
+	/* Extra settle time */
+	DELAY(500000);
+	mpr_dprint(sc, MPR_INIT|MPR_INFO, "hostboot: IOC host boot "
+	    "successful, Doorbell= 0x%x\n", reg);
+
+out:
+	/* Tear down the HCB window regardless of outcome. */
+	mpr_regwrite(sc, MPI2_HCB_SIZE_OFFSET, 0);
+	mpr_regwrite(sc, MPI2_HCB_ADDRESS_LOW_OFFSET, 0);
+	mpr_regwrite(sc, MPI2_HCB_ADDRESS_HIGH_OFFSET, 0);
+
+	bus_dmamap_unload(hcb_dmat, hcb_map);
+	bus_dmamem_free(hcb_dmat, hcb_buf, hcb_map);
+	bus_dma_tag_destroy(hcb_dmat);
+
+	mpr_unmask_intr(sc);
+
+	return (error);
+}
+
+/*
+ * If a firmware image was preloaded for this unit via loader.conf
+ * (see mpr(4)), boot it directly from host memory, bypassing flash.
+ * loader.conf marks the preloaded file with a type of
+ * "<nameunit>:firmware", e.g. "mpr0:firmware", so that units without a
+ * configured recovery image are left alone.
+ *
+ * Returns 0 if an image was found and the hostboot succeeded, ENOENT
+ * if no image was preloaded for this unit, or another errno if an
+ * image was found but the hostboot itself failed.  Must be called
+ * with sc->mpr_mtx held.
+ */
+static int
+mpr_hostboot_from_preload(struct mpr_softc *sc)
+{
+	char type[64];
+	caddr_t mod;
+	void *fw_image;
+	size_t fw_len;
+
+	snprintf(type, sizeof(type), "%s:firmware",
+	    device_get_nameunit(sc->mpr_dev));
+
+	mod = preload_search_by_type(type);
+	if (mod == NULL)
+		return (ENOENT);
+
+	fw_image = preload_fetch_addr(mod);
+	fw_len = preload_fetch_size(mod);
+	if (fw_image == NULL || fw_len == 0) {
+		mpr_dprint(sc, MPR_FAULT, "hostboot: preloaded module '%s' "
+		    "has no data\n", type);
+		return (ENOENT);
+	}
+
+	mpr_dprint(sc, MPR_INIT|MPR_INFO, "hostboot: IOC did not come up "
+	    "normally; booting preloaded recovery image '%s' (%zu bytes) "
+	    "from host memory\n", type, fw_len);
+
+	return (mpr_hostboot(sc, fw_image, (uint32_t)fw_len));
+}
+
+/* Wait for the chip to ACK a word that we've put into its FIFO
  * Wait for <timeout> seconds. In single loop wait for busy loop
  * for 500 microseconds.
  * Total is [ 0.5 * (2000 * <timeout>) ] in miliseconds.
@@ -2165,6 +2390,15 @@ mpr_attach(struct mpr_softc *sc)
 	TAILQ_INIT(&sc->event_list);
 	timevalclear(&sc->lastfail);
 
+	error = mpr_hostboot_from_preload(sc);
+	if (error == ENOENT) {
+		error = 0;
+	} else if (error != 0) {
+		/* Non-fatal */
+		mpr_dprint(sc, MPR_INIT|MPR_FAULT,
+		    "failed to apply preloaded firmware\n");
+	}
+
 	if ((error = mpr_transition_ready(sc)) != 0) {
 		mpr_dprint(sc, MPR_INIT|MPR_FAULT,
 		    "Failed to transition ready\n");
diff --git a/sys/dev/mpr/mprvar.h b/sys/dev/mpr/mprvar.h
index 93f3fbffe079..d5a77e91998a 100644
--- a/sys/dev/mpr/mprvar.h
+++ b/sys/dev/mpr/mprvar.h
@@ -878,6 +878,8 @@ void mprsas_ir_shutdown(struct mpr_softc *sc, int howto);
 int mpr_reinit(struct mpr_softc *sc);
 void mprsas_handle_reinit(struct mpr_softc *sc);
 
+int mpr_hostboot(struct mpr_softc *sc, uint8_t *fw_image, uint32_t fw_len);
+
 void mpr_base_static_config_pages(struct mpr_softc *sc);
 
 int mpr_mapping_initialize(struct mpr_softc *);
diff --git a/sys/dev/mps/mps.c b/sys/dev/mps/mps.c
index cb82045356fc..f76160e56932 100644
--- a/sys/dev/mps/mps.c
+++ b/sys/dev/mps/mps.c
@@ -55,6 +55,7 @@
 #include <sys/eventhandler.h>
 #include <sys/sbuf.h>
 #include <sys/priv.h>
+#include <sys/linker.h>
 
 #include <machine/bus.h>
 #include <machine/resource.h>
@@ -78,6 +79,8 @@
 #include <dev/mps/mps_table.h>
 
 static int mps_diag_reset(struct mps_softc *sc, int sleep_flag);
+static int mps_diag_unlock(struct mps_softc *sc, int sleep_flag);
+static int mps_hostboot_from_preload(struct mps_softc *sc);
 static int mps_init_queues(struct mps_softc *sc);
 static void mps_resize_queues(struct mps_softc *sc);
 static int mps_message_unit_reset(struct mps_softc *sc, int sleep_flag);
@@ -111,6 +114,12 @@ SYSCTL_NODE(_hw, OID_AUTO, mps, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
 MALLOC_DEFINE(M_MPT2, "mps", "mpt2 driver memory");
 MALLOC_DECLARE(M_MPSUSER);
 
+/*
+ * The "Host Code and Data Window" (HCDW/HCB) the image is staged in is
+ * a fixed 2MB window.
+ */
+#define	MPS_HCB_SIZE			(2 * 1024 * 1024)
+
 /*
  * Do a "Diagnostic Reset" aka a hard reset.  This should get the chip out of
  * any state and back to its initialization state machine.
@@ -145,27 +154,11 @@ static struct timeval mps_chainfail_interval = { 60, 0 };
  *
  */
 static int
-mps_diag_reset(struct mps_softc *sc,int sleep_flag)
+mps_diag_unlock(struct mps_softc *sc, int sleep_flag)
 {
 	uint32_t reg;
 	int i, error, tries = 0;
-	uint8_t first_wait_done = FALSE;
-
-	mps_dprint(sc, MPS_INIT, "%s entered\n", __func__);
-
-	/* Clear any pending interrupts */
-	mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
-
-	/*
-	 * Force NO_SLEEP for threads prohibited to sleep
- 	 * e.a Thread from interrupt handler are prohibited to sleep.
- 	 */
-	if (curthread->td_no_sleeping != 0)
-		sleep_flag = NO_SLEEP;
 
-	mps_dprint(sc, MPS_INIT, "sequence start, sleep_flag= %d\n", sleep_flag);
-
-	/* Push the magic sequence */
 	error = ETIMEDOUT;
 	while (tries++ < 20) {
 		for (i = 0; i < sizeof(mpt2_reset_magic); i++)
@@ -186,13 +179,41 @@ mps_diag_reset(struct mps_softc *sc,int sleep_flag)
 			break;
 		}
 	}
+
+	return (error);
+}
+
+static int
+mps_diag_reset(struct mps_softc *sc,int sleep_flag)
+{
+	uint32_t reg;
+	int i, error;
+	uint8_t first_wait_done = FALSE;
+
+	mps_dprint(sc, MPS_INIT, "%s entered\n", __func__);
+
+	/* Clear any pending interrupts */
+	mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
+
+	/*
+	 * Force NO_SLEEP for threads prohibited to sleep
+	 * e.a Thread from interrupt handler are prohibited to sleep.
+	 */
+	if (curthread->td_no_sleeping != 0)
+		sleep_flag = NO_SLEEP;
+
+	mps_dprint(sc, MPS_INIT, "sequence start, sleep_flag= %d\n", sleep_flag);
+
+	/* Push the magic sequence */
+	error = mps_diag_unlock(sc, sleep_flag);
 	if (error) {
 		mps_dprint(sc, MPS_INIT, "sequence failed, error=%d, exit\n",
 		    error);
 		return (error);
 	}
 
-	/* Send the actual reset.  XXX need to refresh the reg? */
+	/* Send the actual reset. */
+	reg = mps_regread(sc, MPI2_HOST_DIAGNOSTIC_OFFSET);
 	reg |= MPI2_DIAG_RESET_ADAPTER;
 	mps_dprint(sc, MPS_INIT, "sequence success, sending reset, reg= 0x%x\n",
 		reg);
@@ -895,7 +916,209 @@ mps_reinit(struct mps_softc *sc)
 	return 0;
 }
 
-/* Wait for the chip to ACK a word that we've put into its FIFO 
+/*
+ * Boot the IOC directly from a firmware image staged in host memory,
+ * bypassing flash.  This is a recovery mechanism for an IOC whose flash
+ * firmware is corrupt or missing and can't otherwise be brought up.
+ * It forces the IOC into HCB (Host Code Boot) mode across a diag reset,
+ * points the HCDW (Host Code Data Window) at a DMA buffer holding the
+ * firmware image, then selects HCDW as the boot device and release the IOC
+ * from reset.
+ */
+int
+mps_hostboot(struct mps_softc *sc, uint8_t *fw_image, uint32_t fw_len)
+{
+	bus_dma_template_t t;
+	bus_dma_tag_t hcb_dmat;
+	bus_dmamap_t hcb_map;
+	uint8_t *hcb_buf;
+	bus_addr_t hcb_busaddr;
+	uint32_t reg;
+	int i, error, sleep_flag;
+
+	sleep_flag = (sc->mps_flags & MPS_FLAGS_ATTACH_DONE)
+	    ? CAN_SLEEP : NO_SLEEP;
+
+	if (curthread->td_no_sleeping != 0)
+		sleep_flag = NO_SLEEP;
+
+	MPS_FUNCTRACE(sc);
+	mtx_assert(&sc->mps_mtx, MA_OWNED);
+
+	if (fw_len > MPS_HCB_SIZE) {
+		mps_dprint(sc, MPS_FAULT, "%s: firmware image (%u bytes) is "
+		    "larger than the %u byte HCB window\n", __func__, fw_len,
+		    MPS_HCB_SIZE);
+		return (EFBIG);
+	}
+
+	bus_dma_template_init(&t, sc->mps_parent_dmat);
+	BUS_DMA_TEMPLATE_FILL(&t, BD_ALIGNMENT(PAGE_SIZE),
+	    BD_MAXSIZE(MPS_HCB_SIZE), BD_MAXSEGSIZE(MPS_HCB_SIZE),
+	    BD_NSEGMENTS(1), BD_LOWADDR(BUS_SPACE_MAXADDR_32BIT));
+	if (bus_dma_template_tag(&t, &hcb_dmat)) {
+		mps_dprint(sc, MPS_ERROR, "%s: cannot allocate HCB DMA tag\n",
+		    __func__);
+		return (ENOMEM);
+	}
+	if (bus_dmamem_alloc(hcb_dmat, (void **)&hcb_buf, BUS_DMA_NOWAIT,
+	    &hcb_map)) {
+		mps_dprint(sc, MPS_ERROR, "%s: cannot allocate HCB memory\n",
+		    __func__);
+		bus_dma_tag_destroy(hcb_dmat);
+		return (ENOMEM);
+	}
+	bus_dmamap_load(hcb_dmat, hcb_map, hcb_buf, MPS_HCB_SIZE,
+	    mps_memaddr_cb, &hcb_busaddr, 0);
+
+	/* Right-justify the image at the end of the window. */
+	bzero(hcb_buf, MPS_HCB_SIZE);
+	bcopy(fw_image, hcb_buf + MPS_HCB_SIZE - fw_len, fw_len);
+	bus_dmamap_sync(hcb_dmat, hcb_map, BUS_DMASYNC_PREWRITE);
+
+	mps_dprint(sc, MPS_INIT|MPS_INFO, "hostboot: HCB busaddr=%#016jx "
+	    "size=%d fw_len=%d\n", (uintmax_t)hcb_busaddr, MPS_HCB_SIZE,
+	    fw_len);
+
+	mps_mask_intr(sc);
+	mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
+
+	/* Force HCB mode across a diag reset. */
+	mps_dprint(sc, MPS_INIT, "hostboot: resetting adapter in HCB mode\n");
+	reg = mps_regread(sc, MPI2_HOST_DIAGNOSTIC_OFFSET);
+	reg |= MPI2_DIAG_FORCE_HCB_ON_RESET;
+	mps_regwrite(sc, MPI2_HOST_DIAGNOSTIC_OFFSET, reg);
+	reg |= MPI2_DIAG_RESET_ADAPTER;
+	mps_regwrite(sc, MPI2_HOST_DIAGNOSTIC_OFFSET, reg);
+
+	DELAY(1000000);
+
+	/* The reset re-locks the diag register; unlock it again. */
+	error = mps_diag_unlock(sc, sleep_flag);
+	if (error != 0) {
+		mps_dprint(sc, MPS_FAULT, "hostboot: failed to re-unlock "
+		    "diag write access after HCB reset, error= %d\n", error);
+		goto out;
+	}
+
+	reg = mps_regread(sc, MPI2_HOST_DIAGNOSTIC_OFFSET);
+	reg &= ~MPI2_DIAG_FORCE_HCB_ON_RESET;
+	mps_regwrite(sc, MPI2_HOST_DIAGNOSTIC_OFFSET, reg);
+
+	reg = mps_regread(sc, MPI2_DOORBELL_OFFSET);
+	if ((reg & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_RESET) {
+		mps_dprint(sc, MPS_FAULT, "hostboot: IOC failed to stay in "
+		    "reset, Doorbell= 0x%x\n", reg);
+		error = EIO;
+		goto out;
+	}
+
+	/* Prep the diag register for an HCDW boot. */
+	reg = mps_regread(sc, MPI2_HOST_DIAGNOSTIC_OFFSET);
+	reg |= MPI2_DIAG_CLEAR_FLASH_BAD_SIG;
+	reg &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
+	reg &= ~MPI2_DIAG_FORCE_HCB_ON_RESET;
+	reg &= ~MPI2_DIAG_RESET_HISTORY;
+	mps_regwrite(sc, MPI2_HOST_DIAGNOSTIC_OFFSET, reg);
+
+	/* Point the HCB window at the DMA buffer. */
+	mps_regwrite(sc, MPI2_HCB_SIZE_OFFSET,
+	    (MPI2_HCB_SIZE_SIZE_MASK & ~(MPS_HCB_SIZE - 1)) |
+	    MPI2_HCB_SIZE_HCB_ENABLE);
+	mps_regwrite(sc, MPI2_HCB_ADDRESS_LOW_OFFSET,
+	    (uint32_t)(hcb_busaddr & 0xffffffff));
+	mps_regwrite(sc, MPI2_HCB_ADDRESS_HIGH_OFFSET,
+	    (uint32_t)(hcb_busaddr >> 32));
+
+	/* Select HCDW as the boot device, then release the IOC from reset. */
+	mps_dprint(sc, MPS_INIT, "hostboot: booting IOC from host memory\n");
+	reg = mps_regread(sc, MPI2_HOST_DIAGNOSTIC_OFFSET);
+	reg |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
+	mps_regwrite(sc, MPI2_HOST_DIAGNOSTIC_OFFSET, reg);
+
+	reg &= ~MPI2_DIAG_HOLD_IOC_RESET;
+	reg &= ~MPI2_DIAG_FORCE_HCB_ON_RESET;
+	mps_regwrite(sc, MPI2_HOST_DIAGNOSTIC_OFFSET, reg);
+
+	error = ETIMEDOUT;
+	for (i = 0; i < 200; i++) {
+		DELAY(10000);
+		reg = mps_regread(sc, MPI2_DOORBELL_OFFSET);
+		if ((reg & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY) {
+			error = 0;
+			break;
+		}
+	}
+
+	if (error != 0) {
+		mps_dprint(sc, MPS_FAULT, "hostboot: IOC failed to become "
+		    "ready, Doorbell= 0x%x\n", reg);
+		goto out;
+	}
+
+	/* Extra settle time */
+	DELAY(500000);
+	mps_dprint(sc, MPS_INIT|MPS_INFO, "hostboot: IOC host boot "
+	    "successful, Doorbell= 0x%x\n", reg);
+
+out:
+	/* Tear down the HCB window regardless of outcome. */
+	mps_regwrite(sc, MPI2_HCB_SIZE_OFFSET, 0);
+	mps_regwrite(sc, MPI2_HCB_ADDRESS_LOW_OFFSET, 0);
+	mps_regwrite(sc, MPI2_HCB_ADDRESS_HIGH_OFFSET, 0);
+
+	bus_dmamap_unload(hcb_dmat, hcb_map);
+	bus_dmamem_free(hcb_dmat, hcb_buf, hcb_map);
+	bus_dma_tag_destroy(hcb_dmat);
+
+	mps_unmask_intr(sc);
+
+	return (error);
+}
+
+/*
+ * If a firmware image was preloaded for this unit via loader.conf
+ * (see mps(4)), boot it directly from host memory, bypassing flash.
+ * loader.conf marks the preloaded file with a type of
+ * "<nameunit>:firmware", e.g. "mps0:firmware", so that units without a
+ * configured recovery image are left alone.
+ *
+ * Returns 0 if an image was found and the hostboot succeeded, ENOENT
+ * if no image was preloaded for this unit, or another errno if an
+ * image was found but the hostboot itself failed.  Must be called
+ * with sc->mps_mtx held.
+ */
+static int
+mps_hostboot_from_preload(struct mps_softc *sc)
+{
+	char type[64];
+	caddr_t mod;
+	void *fw_image;
+	size_t fw_len;
+
+	snprintf(type, sizeof(type), "%s:firmware",
+	    device_get_nameunit(sc->mps_dev));
+
+	mod = preload_search_by_type(type);
+	if (mod == NULL)
+		return (ENOENT);
+
+	fw_image = preload_fetch_addr(mod);
+	fw_len = preload_fetch_size(mod);
+	if (fw_image == NULL || fw_len == 0) {
+		mps_dprint(sc, MPS_FAULT, "hostboot: preloaded module '%s' "
+		    "has no data\n", type);
+		return (ENOENT);
+	}
+
+	mps_dprint(sc, MPS_INIT|MPS_INFO,
+	    "hostboot: booting preloaded recovery image '%s' (%zu bytes) "
+	    "from host memory\n", type, fw_len);
+
+	return (mps_hostboot(sc, fw_image, (uint32_t)fw_len));
+}
+
+/* Wait for the chip to ACK a word that we've put into its FIFO
  * Wait for <timeout> seconds. In single loop wait for busy loop
  * for 500 microseconds.
  * Total is [ 0.5 * (2000 * <timeout>) ] in miliseconds.
@@ -2011,6 +2234,15 @@ mps_attach(struct mps_softc *sc)
 	TAILQ_INIT(&sc->event_list);
 	timevalclear(&sc->lastfail);
 
+	error = mps_hostboot_from_preload(sc);
+	if (error == ENOENT) {
+		error = 0;
+	} else if (error != 0) {
+		/* Non-fatal */
+		mps_dprint(sc, MPS_INIT|MPS_FAULT,
+		    "failed to apply preloaded firmware\n");
+	}
+
 	if ((error = mps_transition_ready(sc)) != 0) {
 		mps_dprint(sc, MPS_INIT|MPS_FAULT, "failed to transition "
 		    "ready\n");
diff --git a/sys/dev/mps/mpsvar.h b/sys/dev/mps/mpsvar.h
index 8b09df115095..8d26a71ef572 100644
--- a/sys/dev/mps/mpsvar.h
+++ b/sys/dev/mps/mpsvar.h
@@ -788,6 +788,8 @@ void mpssas_ir_shutdown(struct mps_softc *sc, int howto);
 int mps_reinit(struct mps_softc *sc);
 void mpssas_handle_reinit(struct mps_softc *sc);
 
+int mps_hostboot(struct mps_softc *sc, uint8_t *fw_image, uint32_t fw_len);
+
 void mps_base_static_config_pages(struct mps_softc *sc);
 void mps_wd_config_pages(struct mps_softc *sc);