svn commit: r254938 - in stable/9/sys: cam dev/mps sys

Kenneth D. Merry ken at FreeBSD.org
Mon Aug 26 21:34:45 UTC 2013


Author: ken
Date: Mon Aug 26 21:34:43 2013
New Revision: 254938
URL: http://svnweb.freebsd.org/changeset/base/254938

Log:
  MFC mps(4) driver changes 253460, 253549, 253550 and 254615.
  
  There are some slight changes here from the version in head.
  
  __FreeBSD_version has been bumped to 902502 for the inclusion of the
  PIM_RESCAN CAM path inquiry flag.  The ifdefs in the mps(4) driver
  have been changed accordingly.
  
  In head, the TDP_NOSLEEPING thread flag has been removed but it
  still exists in stable/9.
  
    ------------------------------------------------------------------------
    r253460 | scottl | 2013-07-18 18:12:41 -0600 (Thu, 18 Jul 2013) | 5 lines
  
    Overhaul error, information, and debug logging.
  
    Obtained from:	Netflix
  
    ------------------------------------------------------------------------
    r253549 | ken | 2013-07-22 12:37:07 -0600 (Mon, 22 Jul 2013) | 57 lines
  
    CAM and mps(4) driver scanning changes.
  
    Add a PIM_NOSCAN flag to the CAM path inquiry CCB.  This tells CAM
    not to perform a rescan on a bus when it is registered.
  
    We now use this flag in the mps(4) driver.  Since it knows what
    devices it has attached, it is more efficient for it to just issue
    a target rescan on the targets that are attached.
  
    Also, remove the private rescan thread from the mps(4) driver in
    favor of the rescan thread already built into CAM.  Without this
    change, but with the change above, the MPS scanner could run before
    or during CAM's initial setup, which would cause duplicate device
    reprobes and announcements.
  
    sys/param.h:
    	Bump __FreeBSD_version to 1000039 for the inclusion of the
    	PIM_RESCAN CAM path inquiry flag.
  
    sys/cam/cam_ccb.h:
    sys/cam/cam_xpt.c:
    	Added a PIM_NOSCAN flag.  If a SIM sets this in the path
    	inquiry ccb, then CAM won't rescan the bus in
    	xpt_bus_regsister.
  
    sys/dev/mps/mps_sas.c
    	For versions of FreeBSD that have the PIM_NOSCAN path
    	inquiry flag, don't freeze the sim queue during scanning,
    	because CAM won't be scanning this bus.  Instead, hold
    	up the boot.  Don't call mpssas_rescan_target in
    	mpssas_startup_decrement; it's redundant and I don't
    	know why it was in there.
  
    	Set PIM_NOSCAN in path inquiry CCBs.
  
    	Remove methods related to the internal rescan daemon.
  
    	Always use async events to trigger a probe for EEDP support.
    	In older versions of FreeBSD where AC_ADVINFO_CHANGED is
    	not available, use AC_FOUND_DEVICE and issue the
    	necessary READ CAPACITY manually.
  
    	Provide a path to xpt_register_async() so that we only
    	receive events for our own SCSI domain.
  
    	Improve error reporting in cases where setup for EEDP
    	detection fails.
  
    sys/dev/mps/mps_sas.h:
    	Remove softc flags and data related to the scanner thread.
  
    sys/dev/mps/mps_sas_lsi.c:
    	Unconditionally rescan the target whenever a device is added.
  
    Sponsored by:	Spectra Logic
  
    ------------------------------------------------------------------------
    r253550 | ken | 2013-07-22 12:41:53 -0600 (Mon, 22 Jul 2013) | 93 lines
  
    Merge in phase 14+ -> 16 mps driver fixes from LSI:
  
    ---------------------------------------------------------------
    System panics during a Port reset with ouststanding I/O
    ---------------------------------------------------------------
    It is possible to call mps_mapping_free_memory after this
    memory is already freed, causing a panic. Removed this extra
    call to mps_mappiing_free_memory and call mps_mapping_exit
    in place of the mps_mapping_free_memory call so that any
    outstanding mapping items can be flushed before memory is
    freed.
  
    ---------------------------------------------------------------
    Correct memory leak during a Port reset with ouststanding I/O
    ---------------------------------------------------------------
    In mps_reinit function, the mapping memory was not being
    freed before being re-allocated. Added line to call the
    memory free function for mapping memory.
  
    ---------------------------------------------------------------
    Use CAM_SIM_QUEUED flag in Driver IO path.
    ---------------------------------------------------------------
    This flag informs the XPT that successful abort of a CCB
    requires an abort ccb to be issued to the SIM.  While
    processing SCSI IO's, set the CAM_SIM_QUEUED flag in the
    status for the IO. When the command completes, clear this
    flag.
  
    ---------------------------------------------------------------
    Check for CAM_REQ_INPROG in I/O path.
    ---------------------------------------------------------------
    Added a check in mpssas_action_scsiio for the In Progress
    status for the IO. If this flag is set, the IO has already
    been aborted by the upper layer (before CAM_SIM_QUEUED was
    set) and there is no need to send the IO. The request will
    be completed without error.
  
    ---------------------------------------------------------------
    Improve "doorbell handshake method" for mps_get_iocfacts
    ---------------------------------------------------------------
    Removed call to get Port Facts since this information is
    not used currently.
  
    Added mps_iocfacts_allocate function to allocate memory
    that is based on IOC Facts data.  Added mps_iocfacts_free
    function to free memory that is based on IOC Facts data.
    Both of the functions are used when a Diag Reset is performed
    or when the driver is attached/detached. This is needed in
    case IOC Facts changes after a Diag Reset, which could
    happen if FW is upgraded.
  
    Moved call of mps_bases_static_config_pages from the attach
    routine to after the IOC is ready to process accesses based
    on the new memory allocations (instead of polling through
    the Doorbell).
  
    ---------------------------------------------------------------
    Set TimeStamp in INIT message in millisecond format Set the IOC
    ---------------------------------------------------------------
  
    ---------------------------------------------------------------
    Prefer mps_wait_command to mps_request_polled
    ---------------------------------------------------------------
    Instead of using mps_request_polled, call mps_wait_command
    whenever possible. Change the mps_wait_command function to
    check the current context and either use interrupt context
    or poll if required by using the pause or DELAY function.
    Added a check after waiting 50mSecs to see if the command
    has timed out. This is only done if polliing, the msleep
    command will automatically timeout if the command has taken
    too long to complete.
  
    ---------------------------------------------------------------
    Integrated RAID: Volume Activation Failed error message is
    displayed though the volume has been activated.
    ---------------------------------------------------------------
    Instead of failing an IOCTL request that does not have a
    large enough buffer to hold the complete reply, copy as
    much data from the reply as possible into the user's buffer
    and log a message saying that the user's buffer was smaller
    than the returned data.
  
    ---------------------------------------------------------------
    mapping_add_new_device failure due to persistent table FULL
    ---------------------------------------------------------------
    When a new device is added, if it is determined that the
    device persistent table is being used and is full, instead
    of displaying a message for this condition every time, only
    log a message if the MPS_INFO bit is set in the debug_flags.
  
    Submitted by:	LSI
  
    ------------------------------------------------------------------------
    r254615 | ken | 2013-08-21 15:30:56 -0600 (Wed, 21 Aug 2013) | 32 lines
  
    Fix mps(4) driver breakage that came in in change 253550 that
    manifested itself in out of chain frame conditions.
  
    When the driver ran out of chain frames, the request in question
    would get completed early, and go through mpssas_scsiio_complete().
  
    In mpssas_scsiio_complete(), the negation of the CAM status values
    (CAM_STATUS_MASK | CAM_SIM_QUEUED) was ORed in instead of being
    ANDed in.  This resulted in a bogus CAM CCB status value.  This
    didn't show up in the non-error case, because the status was reset
    to something valid (e.g. CAM_REQ_CMP) later on in the function.
  
    But in the error case, such as when the driver ran out of chain
    frames, the CAM_REQUEUE_REQ status was ORed in to the bogus status
    value.  This led to the CAM transport layer repeatedly releasing
    the SIM queue, because it though that the CAM_RELEASE_SIMQ flag had
    been set.  The symptom was messages like this on the console when
    INVARIANTS were enabled:
  
    xpt_release_simq: requested 1 > present 0
    xpt_release_simq: requested 1 > present 0
    xpt_release_simq: requested 1 > present 0
  
    mps_sas.c:	In mpssas_scsiio_complete(), use &= to take status
    		bits out.  |= adds them in.
  
    		In the error case in mpssas_scsiio_complete(), set
    		the status to CAM_REQUEUE_REQ, don't OR it in.
  
    Sponsored by:	Spectra Logic
  
    ------------------------------------------------------------------------

Modified:
  stable/9/sys/cam/cam_ccb.h
  stable/9/sys/cam/cam_xpt.c
  stable/9/sys/dev/mps/mps.c
  stable/9/sys/dev/mps/mps_config.c
  stable/9/sys/dev/mps/mps_mapping.c
  stable/9/sys/dev/mps/mps_pci.c
  stable/9/sys/dev/mps/mps_sas.c
  stable/9/sys/dev/mps/mps_sas.h
  stable/9/sys/dev/mps/mps_sas_lsi.c
  stable/9/sys/dev/mps/mps_table.c
  stable/9/sys/dev/mps/mps_user.c
  stable/9/sys/dev/mps/mpsvar.h
  stable/9/sys/sys/param.h
Directory Properties:
  stable/9/sys/   (props changed)
  stable/9/sys/dev/   (props changed)
  stable/9/sys/sys/   (props changed)

Modified: stable/9/sys/cam/cam_ccb.h
==============================================================================
--- stable/9/sys/cam/cam_ccb.h	Mon Aug 26 21:15:50 2013	(r254937)
+++ stable/9/sys/cam/cam_ccb.h	Mon Aug 26 21:34:43 2013	(r254938)
@@ -578,6 +578,7 @@ typedef enum {
 	PIM_NO_6_BYTE	= 0x08,	/* Do not send 6-byte commands */
 	PIM_SEQSCAN	= 0x04,	/* Do bus scans sequentially, not in parallel */
 	PIM_UNMAPPED	= 0x02,
+	PIM_NOSCAN	= 0x01	/* SIM does its own scanning */
 } pi_miscflag;
 
 /* Path Inquiry CCB */

Modified: stable/9/sys/cam/cam_xpt.c
==============================================================================
--- stable/9/sys/cam/cam_xpt.c	Mon Aug 26 21:15:50 2013	(r254937)
+++ stable/9/sys/cam/cam_xpt.c	Mon Aug 26 21:34:43 2013	(r254938)
@@ -4012,15 +4012,23 @@ xpt_bus_register(struct cam_sim *sim, de
 
 	/* Notify interested parties */
 	if (sim->path_id != CAM_XPT_PATH_ID) {
-		union	ccb *scan_ccb;
 
 		xpt_async(AC_PATH_REGISTERED, path, &cpi);
-		/* Initiate bus rescan. */
-		scan_ccb = xpt_alloc_ccb_nowait();
-		scan_ccb->ccb_h.path = path;
-		scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
-		scan_ccb->crcn.flags = 0;
-		xpt_rescan(scan_ccb);
+		if ((cpi.hba_misc & PIM_NOSCAN) == 0) {
+			union	ccb *scan_ccb;
+
+			/* Initiate bus rescan. */
+			scan_ccb = xpt_alloc_ccb_nowait();
+			if (scan_ccb != NULL) {
+				scan_ccb->ccb_h.path = path;
+				scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
+				scan_ccb->crcn.flags = 0;
+				xpt_rescan(scan_ccb);
+			} else
+				xpt_print(path,
+					  "Can't allocate CCB to scan bus\n");
+		} else
+			xpt_free_path(path);
 	} else
 		xpt_free_path(path);
 	return (CAM_SUCCESS);

Modified: stable/9/sys/dev/mps/mps.c
==============================================================================
--- stable/9/sys/dev/mps/mps.c	Mon Aug 26 21:15:50 2013	(r254937)
+++ stable/9/sys/dev/mps/mps.c	Mon Aug 26 21:34:43 2013	(r254938)
@@ -51,6 +51,7 @@ __FBSDID("$FreeBSD$");
 #include <sys/sysctl.h>
 #include <sys/queue.h>
 #include <sys/kthread.h>
+#include <sys/taskqueue.h>
 #include <sys/endian.h>
 #include <sys/eventhandler.h>
 
@@ -61,6 +62,7 @@ __FBSDID("$FreeBSD$");
 
 #include <dev/pci/pcivar.h>
 
+#include <cam/cam.h>
 #include <cam/scsi/scsi_all.h>
 
 #include <dev/mps/mpi/mpi2_type.h>
@@ -73,21 +75,29 @@ __FBSDID("$FreeBSD$");
 #include <dev/mps/mps_ioctl.h>
 #include <dev/mps/mpsvar.h>
 #include <dev/mps/mps_table.h>
+#include <dev/mps/mps_sas.h>
 
 static int mps_diag_reset(struct mps_softc *sc, int sleep_flag);
 static int mps_init_queues(struct mps_softc *sc);
 static int mps_message_unit_reset(struct mps_softc *sc, int sleep_flag);
 static int mps_transition_operational(struct mps_softc *sc);
+static int mps_iocfacts_allocate(struct mps_softc *sc, uint8_t attaching);
+static void mps_iocfacts_free(struct mps_softc *sc);
 static void mps_startup(void *arg);
 static int mps_send_iocinit(struct mps_softc *sc);
+static int mps_alloc_queues(struct mps_softc *sc);
+static int mps_alloc_replies(struct mps_softc *sc);
+static int mps_alloc_requests(struct mps_softc *sc);
 static int mps_attach_log(struct mps_softc *sc);
-static __inline void mps_complete_command(struct mps_command *cm);
+static __inline void mps_complete_command(struct mps_softc *sc,
+    struct mps_command *cm);
 static void mps_dispatch_event(struct mps_softc *sc, uintptr_t data,
     MPI2_EVENT_NOTIFICATION_REPLY *reply);
 static void mps_config_complete(struct mps_softc *sc, struct mps_command *cm);
 static void mps_periodic(void *);
 static int mps_reregister_events(struct mps_softc *sc);
 static void mps_enqueue_request(struct mps_softc *sc, struct mps_command *cm);
+static int mps_get_iocfacts(struct mps_softc *sc, MPI2_IOC_FACTS_REPLY *facts);
 static int mps_wait_db_ack(struct mps_softc *sc, int timeout, int sleep_flag);
 SYSCTL_NODE(_hw, OID_AUTO, mps, CTLFLAG_RD, 0, "MPS Driver Parameters");
 
@@ -148,7 +158,8 @@ mps_diag_reset(struct mps_softc *sc,int 
 			    mpt2_reset_magic[i]);
 		/* wait 100 msec */
 		if (mtx_owned(&sc->mps_mtx) && sleep_flag == CAN_SLEEP)
-			msleep(&sc->msleep_fake_chan, &sc->mps_mtx, 0, "mpsdiag", hz/10);
+			msleep(&sc->msleep_fake_chan, &sc->mps_mtx, 0,
+			    "mpsdiag", hz/10);
 		else if (sleep_flag == CAN_SLEEP)
 			pause("mpsdiag", hz/10);
 		else
@@ -172,7 +183,8 @@ mps_diag_reset(struct mps_softc *sc,int 
 	for (i = 0; i < 60000; i++) {
 		/* wait 50 msec */
 		if (mtx_owned(&sc->mps_mtx) && sleep_flag == CAN_SLEEP)
-			msleep(&sc->msleep_fake_chan, &sc->mps_mtx, 0, "mpsdiag", hz/20);
+			msleep(&sc->msleep_fake_chan, &sc->mps_mtx, 0,
+			    "mpsdiag", hz/20);
 		else if (sleep_flag == CAN_SLEEP)
 			pause("mpsdiag", hz/20);
 		else
@@ -195,7 +207,7 @@ static int
 mps_message_unit_reset(struct mps_softc *sc, int sleep_flag)
 {
 
-	mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
+	MPS_FUNCTRACE(sc);
 
 	mps_regwrite(sc, MPI2_DOORBELL_OFFSET,
 	    MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET <<
@@ -217,14 +229,14 @@ mps_transition_ready(struct mps_softc *s
 	int error, tries = 0;
 	int sleep_flags;
 
-	mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
+	MPS_FUNCTRACE(sc);
 	/* If we are in attach call, do not sleep */
 	sleep_flags = (sc->mps_flags & MPS_FLAGS_ATTACH_DONE)
 					? CAN_SLEEP:NO_SLEEP;
 	error = 0;
 	while (tries++ < 5) {
 		reg = mps_regread(sc, MPI2_DOORBELL_OFFSET);
-		mps_dprint(sc, MPS_INFO, "Doorbell= 0x%x\n", reg);
+		mps_dprint(sc, MPS_INIT, "Doorbell= 0x%x\n", reg);
 
 		/*
 		 * Ensure the IOC is ready to talk.  If it's not, try
@@ -250,7 +262,7 @@ mps_transition_ready(struct mps_softc *s
 			error = 0;
 			break;
 		} else if (state == MPI2_IOC_STATE_FAULT) {
-			mps_dprint(sc, MPS_INFO, "IOC in fault state 0x%x\n",
+			mps_dprint(sc, MPS_FAULT, "IOC in fault state 0x%x, resetting\n",
 			    state & MPI2_DOORBELL_FAULT_CODE_MASK);
 			mps_diag_reset(sc, sleep_flags);
 		} else if (state == MPI2_IOC_STATE_OPERATIONAL) {
@@ -283,11 +295,11 @@ mps_transition_operational(struct mps_so
 	uint32_t reg, state;
 	int error;
 
-	mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
+	MPS_FUNCTRACE(sc);
 
 	error = 0;
 	reg = mps_regread(sc, MPI2_DOORBELL_OFFSET);
-	mps_dprint(sc, MPS_INFO, "Doorbell= 0x%x\n", reg);
+	mps_dprint(sc, MPS_INIT, "Doorbell= 0x%x\n", reg);
 
 	state = reg & MPI2_IOC_STATE_MASK;
 	if (state != MPI2_IOC_STATE_READY) {
@@ -302,9 +314,357 @@ mps_transition_operational(struct mps_so
 	return (error);
 }
 
+/*
+ * This is called during attach and when re-initializing due to a Diag Reset.
+ * IOC Facts is used to allocate many of the structures needed by the driver.
+ * If called from attach, de-allocation is not required because the driver has
+ * not allocated any structures yet, but if called from a Diag Reset, previously
+ * allocated structures based on IOC Facts will need to be freed and re-
+ * allocated bases on the latest IOC Facts.
+ */
+static int
+mps_iocfacts_allocate(struct mps_softc *sc, uint8_t attaching)
+{
+	int error, i;
+	Mpi2IOCFactsReply_t saved_facts;
+	uint8_t saved_mode, reallocating;
+	struct mpssas_lun *lun, *lun_tmp;
+	struct mpssas_target *targ;
+
+	mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
+
+	/* Save old IOC Facts and then only reallocate if Facts have changed */
+	if (!attaching) {
+		bcopy(sc->facts, &saved_facts, sizeof(MPI2_IOC_FACTS_REPLY));
+	}
+
+	/*
+	 * Get IOC Facts.  In all cases throughout this function, panic if doing
+	 * a re-initialization and only return the error if attaching so the OS
+	 * can handle it.
+	 */
+	if ((error = mps_get_iocfacts(sc, sc->facts)) != 0) {
+		if (attaching) {
+			mps_dprint(sc, MPS_FAULT, "%s failed to get IOC Facts "
+			    "with error %d\n", __func__, error);
+			return (error);
+		} else {
+			panic("%s failed to get IOC Facts with error %d\n",
+			    __func__, error);
+		}
+	}
+
+	mps_print_iocfacts(sc, sc->facts);
+
+	snprintf(sc->fw_version, sizeof(sc->fw_version), 
+	    "%02d.%02d.%02d.%02d", 
+	    sc->facts->FWVersion.Struct.Major,
+	    sc->facts->FWVersion.Struct.Minor,
+	    sc->facts->FWVersion.Struct.Unit,
+	    sc->facts->FWVersion.Struct.Dev);
+
+	mps_printf(sc, "Firmware: %s, Driver: %s\n", sc->fw_version,
+	    MPS_DRIVER_VERSION);
+	mps_printf(sc, "IOCCapabilities: %b\n", sc->facts->IOCCapabilities,
+	    "\20" "\3ScsiTaskFull" "\4DiagTrace" "\5SnapBuf" "\6ExtBuf"
+	    "\7EEDP" "\10BiDirTarg" "\11Multicast" "\14TransRetry" "\15IR"
+	    "\16EventReplay" "\17RaidAccel" "\20MSIXIndex" "\21HostDisc");
+
+	/*
+	 * If the chip doesn't support event replay then a hard reset will be
+	 * required to trigger a full discovery.  Do the reset here then
+	 * retransition to Ready.  A hard reset might have already been done,
+	 * but it doesn't hurt to do it again.  Only do this if attaching, not
+	 * for a Diag Reset.
+	 */
+	if (attaching) {
+		if ((sc->facts->IOCCapabilities &
+		    MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY) == 0) {
+			mps_diag_reset(sc, NO_SLEEP);
+			if ((error = mps_transition_ready(sc)) != 0) {
+				mps_dprint(sc, MPS_FAULT, "%s failed to "
+				    "transition to ready with error %d\n",
+				    __func__, error);
+				return (error);
+			}
+		}
+	}
+
+	/*
+	 * Set flag if IR Firmware is loaded.  If the RAID Capability has
+	 * changed from the previous IOC Facts, log a warning, but only if
+	 * checking this after a Diag Reset and not during attach.
+	 */
+	saved_mode = sc->ir_firmware;
+	if (sc->facts->IOCCapabilities &
+	    MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID)
+		sc->ir_firmware = 1;
+	if (!attaching) {
+		if (sc->ir_firmware != saved_mode) {
+			mps_dprint(sc, MPS_FAULT, "%s new IR/IT mode in IOC "
+			    "Facts does not match previous mode\n", __func__);
+		}
+	}
+
+	/* Only deallocate and reallocate if relevant IOC Facts have changed */
+	reallocating = FALSE;
+	if ((!attaching) &&
+	    ((saved_facts.MsgVersion != sc->facts->MsgVersion) ||
+	    (saved_facts.HeaderVersion != sc->facts->HeaderVersion) ||
+	    (saved_facts.MaxChainDepth != sc->facts->MaxChainDepth) ||
+	    (saved_facts.RequestCredit != sc->facts->RequestCredit) ||
+	    (saved_facts.ProductID != sc->facts->ProductID) ||
+	    (saved_facts.IOCCapabilities != sc->facts->IOCCapabilities) ||
+	    (saved_facts.IOCRequestFrameSize !=
+	    sc->facts->IOCRequestFrameSize) ||
+	    (saved_facts.MaxTargets != sc->facts->MaxTargets) ||
+	    (saved_facts.MaxSasExpanders != sc->facts->MaxSasExpanders) ||
+	    (saved_facts.MaxEnclosures != sc->facts->MaxEnclosures) ||
+	    (saved_facts.HighPriorityCredit != sc->facts->HighPriorityCredit) ||
+	    (saved_facts.MaxReplyDescriptorPostQueueDepth !=
+	    sc->facts->MaxReplyDescriptorPostQueueDepth) ||
+	    (saved_facts.ReplyFrameSize != sc->facts->ReplyFrameSize) ||
+	    (saved_facts.MaxVolumes != sc->facts->MaxVolumes) ||
+	    (saved_facts.MaxPersistentEntries !=
+	    sc->facts->MaxPersistentEntries))) {
+		reallocating = TRUE;
+	}
+
+	/*
+	 * Some things should be done if attaching or re-allocating after a Diag
+	 * Reset, but are not needed after a Diag Reset if the FW has not
+	 * changed.
+	 */
+	if (attaching || reallocating) {
+		/*
+		 * Check if controller supports FW diag buffers and set flag to
+		 * enable each type.
+		 */
+		if (sc->facts->IOCCapabilities &
+		    MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
+			sc->fw_diag_buffer_list[MPI2_DIAG_BUF_TYPE_TRACE].
+			    enabled = TRUE;
+		if (sc->facts->IOCCapabilities &
+		    MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
+			sc->fw_diag_buffer_list[MPI2_DIAG_BUF_TYPE_SNAPSHOT].
+			    enabled = TRUE;
+		if (sc->facts->IOCCapabilities &
+		    MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
+			sc->fw_diag_buffer_list[MPI2_DIAG_BUF_TYPE_EXTENDED].
+			    enabled = TRUE;
+
+		/*
+		 * Set flag if EEDP is supported and if TLR is supported.
+		 */
+		if (sc->facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP)
+			sc->eedp_enabled = TRUE;
+		if (sc->facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR)
+			sc->control_TLR = TRUE;
+
+		/*
+		 * Size the queues. Since the reply queues always need one free
+		 * entry, we'll just deduct one reply message here.
+		 */
+		sc->num_reqs = MIN(MPS_REQ_FRAMES, sc->facts->RequestCredit);
+		sc->num_replies = MIN(MPS_REPLY_FRAMES + MPS_EVT_REPLY_FRAMES,
+		    sc->facts->MaxReplyDescriptorPostQueueDepth) - 1;
+
+		/*
+		 * Initialize all Tail Queues
+		 */
+		TAILQ_INIT(&sc->req_list);
+		TAILQ_INIT(&sc->high_priority_req_list);
+		TAILQ_INIT(&sc->chain_list);
+		TAILQ_INIT(&sc->tm_list);
+	}
+
+	/*
+	 * If doing a Diag Reset and the FW is significantly different
+	 * (reallocating will be set above in IOC Facts comparison), then all
+	 * buffers based on the IOC Facts will need to be freed before they are
+	 * reallocated.
+	 */
+	if (reallocating) {
+		mps_iocfacts_free(sc);
+
+		/*
+		 * The number of targets is based on IOC Facts, so free all of
+		 * the allocated LUNs for each target and then the target buffer
+		 * itself.
+		 */
+		for (i=0; i< saved_facts.MaxTargets; i++) {
+			targ = &sc->sassc->targets[i];
+			SLIST_FOREACH_SAFE(lun, &targ->luns, lun_link,
+			    lun_tmp) {
+				free(lun, M_MPT2);
+			}
+		}
+		free(sc->sassc->targets, M_MPT2);
+
+		sc->sassc->targets = malloc(sizeof(struct mpssas_target) *
+		    sc->facts->MaxTargets, M_MPT2, M_WAITOK|M_ZERO);
+		if (!sc->sassc->targets) {
+			panic("%s failed to alloc targets with error %d\n",
+			    __func__, ENOMEM);
+		}
+	}
+
+	/*
+	 * Any deallocation has been completed.  Now start reallocating
+	 * if needed.  Will only need to reallocate if attaching or if the new
+	 * IOC Facts are different from the previous IOC Facts after a Diag
+	 * Reset. Targets have already been allocated above if needed.
+	 */
+	if (attaching || reallocating) {
+		if (((error = mps_alloc_queues(sc)) != 0) ||
+		    ((error = mps_alloc_replies(sc)) != 0) ||
+		    ((error = mps_alloc_requests(sc)) != 0)) {
+			if (attaching ) {
+				mps_dprint(sc, MPS_FAULT, "%s failed to alloc "
+				    "queues with error %d\n", __func__, error);
+				mps_free(sc);
+				return (error);
+			} else {
+				panic("%s failed to alloc queues with error "
+				    "%d\n", __func__, error);
+			}
+		}
+	}
+
+	/* Always initialize the queues */
+	bzero(sc->free_queue, sc->fqdepth * 4);
+	mps_init_queues(sc);
+
+	/*
+	 * Always get the chip out of the reset state, but only panic if not
+	 * attaching.  If attaching and there is an error, that is handled by
+	 * the OS.
+	 */
+	error = mps_transition_operational(sc);
+	if (error != 0) {
+		if (attaching) {
+			mps_printf(sc, "%s failed to transition to operational "
+			    "with error %d\n", __func__, error);
+			mps_free(sc);
+			return (error);
+		} else {
+			panic("%s failed to transition to operational with "
+			    "error %d\n", __func__, error);
+		}
+	}
+
+	/*
+	 * Finish the queue initialization.
+	 * These are set here instead of in mps_init_queues() because the
+	 * IOC resets these values during the state transition in
+	 * mps_transition_operational().  The free index is set to 1
+	 * because the corresponding index in the IOC is set to 0, and the
+	 * IOC treats the queues as full if both are set to the same value.
+	 * Hence the reason that the queue can't hold all of the possible
+	 * replies.
+	 */
+	sc->replypostindex = 0;
+	mps_regwrite(sc, MPI2_REPLY_FREE_HOST_INDEX_OFFSET, sc->replyfreeindex);
+	mps_regwrite(sc, MPI2_REPLY_POST_HOST_INDEX_OFFSET, 0);
+
+	/*
+	 * Attach the subsystems so they can prepare their event masks.
+	 */
+	/* XXX Should be dynamic so that IM/IR and user modules can attach */
+	if (attaching) {
+		if (((error = mps_attach_log(sc)) != 0) ||
+		    ((error = mps_attach_sas(sc)) != 0) ||
+		    ((error = mps_attach_user(sc)) != 0)) {
+			mps_printf(sc, "%s failed to attach all subsystems: "
+			    "error %d\n", __func__, error);
+			mps_free(sc);
+			return (error);
+		}
+
+		if ((error = mps_pci_setup_interrupts(sc)) != 0) {
+			mps_printf(sc, "%s failed to setup interrupts\n",
+			    __func__);
+			mps_free(sc);
+			return (error);
+		}
+	}
+
+	/*
+	 * Set flag if this is a WD controller.  This shouldn't ever change, but
+	 * reset it after a Diag Reset, just in case.
+	 */
+	sc->WD_available = FALSE;
+	if (pci_get_device(sc->mps_dev) == MPI2_MFGPAGE_DEVID_SSS6200)
+		sc->WD_available = TRUE;
+
+	return (error);
+}
+
+/*
+ * This is called if memory is being free (during detach for example) and when
+ * buffers need to be reallocated due to a Diag Reset.
+ */
+static void
+mps_iocfacts_free(struct mps_softc *sc)
+{
+	struct mps_command *cm;
+	int i;
+
+	mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
+
+	if (sc->post_busaddr != 0)
+		bus_dmamap_unload(sc->queues_dmat, sc->queues_map);
+	if (sc->post_queue != NULL)
+		bus_dmamem_free(sc->queues_dmat, sc->post_queue,
+		    sc->queues_map);
+	if (sc->queues_dmat != NULL)
+		bus_dma_tag_destroy(sc->queues_dmat);
+
+	if (sc->chain_busaddr != 0)
+		bus_dmamap_unload(sc->chain_dmat, sc->chain_map);
+	if (sc->chain_frames != NULL)
+		bus_dmamem_free(sc->chain_dmat, sc->chain_frames,
+		    sc->chain_map);
+	if (sc->chain_dmat != NULL)
+		bus_dma_tag_destroy(sc->chain_dmat);
+
+	if (sc->sense_busaddr != 0)
+		bus_dmamap_unload(sc->sense_dmat, sc->sense_map);
+	if (sc->sense_frames != NULL)
+		bus_dmamem_free(sc->sense_dmat, sc->sense_frames,
+		    sc->sense_map);
+	if (sc->sense_dmat != NULL)
+		bus_dma_tag_destroy(sc->sense_dmat);
+
+	if (sc->reply_busaddr != 0)
+		bus_dmamap_unload(sc->reply_dmat, sc->reply_map);
+	if (sc->reply_frames != NULL)
+		bus_dmamem_free(sc->reply_dmat, sc->reply_frames,
+		    sc->reply_map);
+	if (sc->reply_dmat != NULL)
+		bus_dma_tag_destroy(sc->reply_dmat);
+
+	if (sc->req_busaddr != 0)
+		bus_dmamap_unload(sc->req_dmat, sc->req_map);
+	if (sc->req_frames != NULL)
+		bus_dmamem_free(sc->req_dmat, sc->req_frames, sc->req_map);
+	if (sc->req_dmat != NULL)
+		bus_dma_tag_destroy(sc->req_dmat);
+
+	if (sc->chains != NULL)
+		free(sc->chains, M_MPT2);
+	if (sc->commands != NULL) {
+		for (i = 1; i < sc->num_reqs; i++) {
+			cm = &sc->commands[i];
+			bus_dmamap_destroy(sc->buffer_dmat, cm->cm_dmamap);
+		}
+		free(sc->commands, M_MPT2);
+	}
+	if (sc->buffer_dmat != NULL)
+		bus_dma_tag_destroy(sc->buffer_dmat);
+}
+
 /* 
- * XXX Some of this should probably move to mps.c
- *
  * The terms diag reset and hard reset are used interchangeably in the MPI
  * docs to mean resetting the controller chip.  In this code diag reset
  * cleans everything up, and the hard reset function just sends the reset
@@ -316,27 +676,32 @@ int
 mps_reinit(struct mps_softc *sc)
 {
 	int error;
-	uint32_t db;
 
-	mps_printf(sc, "%s sc %p\n", __func__, sc);
+	MPS_FUNCTRACE(sc);
 
 	mtx_assert(&sc->mps_mtx, MA_OWNED);
 
 	if (sc->mps_flags & MPS_FLAGS_DIAGRESET) {
-		mps_printf(sc, "%s reset already in progress\n", __func__);
+		mps_dprint(sc, MPS_INIT, "%s reset already in progress\n",
+			   __func__);
 		return 0;
 	}
 
+	mps_dprint(sc, MPS_INFO, "Reinitializing controller,\n");
 	/* make sure the completion callbacks can recognize they're getting
 	 * a NULL cm_reply due to a reset.
 	 */
 	sc->mps_flags |= MPS_FLAGS_DIAGRESET;
 
-	mps_printf(sc, "%s mask interrupts\n", __func__);
+	/*
+	 * Mask interrupts here.
+	 */
+	mps_dprint(sc, MPS_INIT, "%s mask interrupts\n", __func__);
 	mps_mask_intr(sc);
 
 	error = mps_diag_reset(sc, CAN_SLEEP);
 	if (error != 0) {
+		/* XXXSL No need to panic here */
 		panic("%s hard reset failed with error %d\n",
 		    __func__, error);
 	}
@@ -347,48 +712,49 @@ mps_reinit(struct mps_softc *sc)
 	/* Give the I/O subsystem special priority to get itself prepared */
 	mpssas_handle_reinit(sc);
 
-	/* reinitialize queues after the reset */
-	bzero(sc->free_queue, sc->fqdepth * 4);
-	mps_init_queues(sc);
-
-	/* get the chip out of the reset state */
-	error = mps_transition_operational(sc);
-	if (error != 0)
-		panic("%s transition operational failed with error %d\n",
+	/*
+	 * Get IOC Facts and allocate all structures based on this information.
+	 * The attach function will also call mps_iocfacts_allocate at startup.
+	 * If relevant values have changed in IOC Facts, this function will free
+	 * all of the memory based on IOC Facts and reallocate that memory.
+	 */
+	if ((error = mps_iocfacts_allocate(sc, FALSE)) != 0) {
+		panic("%s IOC Facts based allocation failed with error %d\n",
 		    __func__, error);
+	}
 
-	/* Reinitialize the reply queue. This is delicate because this
-	 * function is typically invoked by task mgmt completion callbacks,
-	 * which are called by the interrupt thread.  We need to make sure
-	 * the interrupt handler loop will exit when we return to it, and
-	 * that it will recognize the indexes we've changed.
+	/*
+	 * Mapping structures will be re-allocated after getting IOC Page8, so
+	 * free these structures here.
 	 */
-	sc->replypostindex = 0;
-	mps_regwrite(sc, MPI2_REPLY_FREE_HOST_INDEX_OFFSET, sc->replyfreeindex);
-	mps_regwrite(sc, MPI2_REPLY_POST_HOST_INDEX_OFFSET, sc->replypostindex);
-
-	db = mps_regread(sc, MPI2_DOORBELL_OFFSET);
-	mps_printf(sc, "%s doorbell 0x%08x\n", __func__, db);
-
-	mps_printf(sc, "%s unmask interrupts post %u free %u\n", __func__,
-	    sc->replypostindex, sc->replyfreeindex);
+	mps_mapping_exit(sc);
 
+	/*
+	 * The static page function currently read is IOC Page8.  Others can be
+	 * added in future.  It's possible that the values in IOC Page8 have
+	 * changed after a Diag Reset due to user modification, so always read
+	 * these.  Interrupts are masked, so unmask them before getting config
+	 * pages.
+	 */
 	mps_unmask_intr(sc);
+	sc->mps_flags &= ~MPS_FLAGS_DIAGRESET;
+	mps_base_static_config_pages(sc);
 
-	mps_printf(sc, "%s restarting post %u free %u\n", __func__,
-	    sc->replypostindex, sc->replyfreeindex);
+	/*
+	 * Some mapping info is based in IOC Page8 data, so re-initialize the
+	 * mapping tables.
+	 */
+	mps_mapping_initialize(sc);
 
-	/* restart will reload the event masks clobbered by the reset, and
+	/*
+	 * Restart will reload the event masks clobbered by the reset, and
 	 * then enable the port.
 	 */
 	mps_reregister_events(sc);
 
 	/* the end of discovery will release the simq, so we're done. */
-	mps_printf(sc, "%s finished sc %p post %u free %u\n", 
-	    __func__, sc, 
-	    sc->replypostindex, sc->replyfreeindex);
-
-	sc->mps_flags &= ~MPS_FLAGS_DIAGRESET;
+	mps_dprint(sc, MPS_INFO, "%s finished sc %p post %u free %u\n", 
+	    __func__, sc, sc->replypostindex, sc->replyfreeindex);
 
 	return 0;
 }
@@ -411,7 +777,7 @@ mps_wait_db_ack(struct mps_softc *sc, in
 	do {
 		int_status = mps_regread(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET);
 		if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
-			mps_dprint(sc, MPS_INFO, 
+			mps_dprint(sc, MPS_INIT, 
 			"%s: successfull count(%d), timeout(%d)\n",
 			__func__, count, timeout);
 		return 0;
@@ -546,7 +912,7 @@ mps_request_sync(struct mps_softc *sc, v
 	count = MIN((reply_sz / 4), ioc_sz) * 2;
 	if (count < ioc_sz * 2) {
 		residual = ioc_sz * 2 - count;
-		mps_dprint(sc, MPS_FAULT, "Driver error, throwing away %d "
+		mps_dprint(sc, MPS_ERROR, "Driver error, throwing away %d "
 		    "residual message words\n", residual);
 	}
 
@@ -592,7 +958,8 @@ static void
 mps_enqueue_request(struct mps_softc *sc, struct mps_command *cm)
 {
 	reply_descriptor rd;
-	mps_dprint(sc, MPS_TRACE, "%s SMID %u cm %p ccb %p\n", __func__,
+	MPS_FUNCTRACE(sc);
+	mps_dprint(sc, MPS_TRACE, "SMID %u cm %p ccb %p\n",
 	    cm->cm_desc.Default.SMID, cm, cm->cm_ccb);
 
 	if (sc->mps_flags & MPS_FLAGS_ATTACH_DONE && !(sc->mps_flags & MPS_FLAGS_SHUTDOWN))
@@ -620,7 +987,7 @@ mps_get_iocfacts(struct mps_softc *sc, M
 	MPI2_IOC_FACTS_REQUEST request;
 	int error, req_sz, reply_sz;
 
-	mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
+	MPS_FUNCTRACE(sc);
 
 	req_sz = sizeof(MPI2_IOC_FACTS_REQUEST);
 	reply_sz = sizeof(MPI2_IOC_FACTS_REPLY);
@@ -634,50 +1001,15 @@ mps_get_iocfacts(struct mps_softc *sc, M
 }
 
 static int
-mps_get_portfacts(struct mps_softc *sc, MPI2_PORT_FACTS_REPLY *facts, int port)
-{
-	MPI2_PORT_FACTS_REQUEST *request;
-	MPI2_PORT_FACTS_REPLY *reply;
-	struct mps_command *cm;
-	int error;
-
-	mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
-
-	if ((cm = mps_alloc_command(sc)) == NULL)
-		return (EBUSY);
-	request = (MPI2_PORT_FACTS_REQUEST *)cm->cm_req;
-	request->Function = MPI2_FUNCTION_PORT_FACTS;
-	request->PortNumber = port;
-	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
-	cm->cm_data = NULL;
-	error = mps_request_polled(sc, cm);
-	reply = (MPI2_PORT_FACTS_REPLY *)cm->cm_reply;
-	if (reply == NULL) {
-		mps_printf(sc, "%s NULL reply\n", __func__);
-		goto done;
-	}
-	if ((reply->IOCStatus & MPI2_IOCSTATUS_MASK) != MPI2_IOCSTATUS_SUCCESS) {
-		mps_printf(sc, 
-		    "%s error %d iocstatus 0x%x iocloginfo 0x%x type 0x%x\n",
-		    __func__, error, reply->IOCStatus, reply->IOCLogInfo, 
-		    reply->PortType);
-		error = ENXIO;
-	}
-	bcopy(reply, facts, sizeof(MPI2_PORT_FACTS_REPLY));
-done:
-	mps_free_command(sc, cm);
-
-	return (error);
-}
-
-static int
 mps_send_iocinit(struct mps_softc *sc)
 {
 	MPI2_IOC_INIT_REQUEST	init;
 	MPI2_DEFAULT_REPLY	reply;
 	int req_sz, reply_sz, error;
+	struct timeval now;
+	uint64_t time_in_msec;
 
-	mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
+	MPS_FUNCTRACE(sc);
 
 	req_sz = sizeof(MPI2_IOC_INIT_REQUEST);
 	reply_sz = sizeof(MPI2_IOC_INIT_REPLY);
@@ -704,14 +1036,16 @@ mps_send_iocinit(struct mps_softc *sc)
 	init.ReplyDescriptorPostQueueAddress.Low = htole32((uint32_t)sc->post_busaddr);
 	init.ReplyFreeQueueAddress.High = 0;
 	init.ReplyFreeQueueAddress.Low = htole32((uint32_t)sc->free_busaddr);
-	init.TimeStamp.High = 0;
-	init.TimeStamp.Low = htole32((uint32_t)time_uptime);
+	getmicrotime(&now);
+	time_in_msec = (now.tv_sec * 1000 + now.tv_usec/1000);
+	init.TimeStamp.High = htole32((time_in_msec >> 32) & 0xFFFFFFFF);
+	init.TimeStamp.Low = htole32(time_in_msec & 0xFFFFFFFF);
 
 	error = mps_request_sync(sc, &init, &reply, req_sz, reply_sz, 5);
 	if ((reply.IOCStatus & MPI2_IOCSTATUS_MASK) != MPI2_IOCSTATUS_SUCCESS)
 		error = ENXIO;
 
-	mps_dprint(sc, MPS_INFO, "IOCInit status= 0x%x\n", reply.IOCStatus);
+	mps_dprint(sc, MPS_INIT, "IOCInit status= 0x%x\n", reply.IOCStatus);
 	return (error);
 }
 
@@ -1008,7 +1342,7 @@ mps_get_tunables(struct mps_softc *sc)
 	char tmpstr[80];
 
 	/* XXX default to some debugging for now */
-	sc->mps_debug = MPS_FAULT;
+	sc->mps_debug = MPS_INFO|MPS_FAULT;
 	sc->disable_msix = 0;
 	sc->disable_msi = 0;
 	sc->max_chains = MPS_CHAIN_FRAMES;
@@ -1119,11 +1453,11 @@ mps_setup_sysctl(struct mps_softc *sc)
 int
 mps_attach(struct mps_softc *sc)
 {
-	int i, error;
+	int error;
 
 	mps_get_tunables(sc);
 
-	mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
+	MPS_FUNCTRACE(sc);
 
 	mtx_init(&sc->mps_mtx, "MPT2SAS lock", NULL, MTX_DEF);
 	callout_init_mtx(&sc->periodic, &sc->mps_mtx, 0);
@@ -1141,151 +1475,20 @@ mps_attach(struct mps_softc *sc)
 		 __func__, __LINE__);
 		return (ENOMEM);
 	}
-	if ((error = mps_get_iocfacts(sc, sc->facts)) != 0)
-		return (error);
-
-	mps_print_iocfacts(sc, sc->facts);
-
-	snprintf(sc->fw_version, sizeof(sc->fw_version), 
-	    "%02d.%02d.%02d.%02d", 
-	    sc->facts->FWVersion.Struct.Major,
-	    sc->facts->FWVersion.Struct.Minor,
-	    sc->facts->FWVersion.Struct.Unit,
-	    sc->facts->FWVersion.Struct.Dev);
-
-	mps_printf(sc, "Firmware: %s, Driver: %s\n", sc->fw_version,
-	    MPS_DRIVER_VERSION);
-	mps_printf(sc, "IOCCapabilities: %b\n", sc->facts->IOCCapabilities,
-	    "\20" "\3ScsiTaskFull" "\4DiagTrace" "\5SnapBuf" "\6ExtBuf"
-	    "\7EEDP" "\10BiDirTarg" "\11Multicast" "\14TransRetry" "\15IR"
-	    "\16EventReplay" "\17RaidAccel" "\20MSIXIndex" "\21HostDisc");
-
-	/*
-	 * If the chip doesn't support event replay then a hard reset will be
-	 * required to trigger a full discovery.  Do the reset here then
-	 * retransition to Ready.  A hard reset might have already been done,
-	 * but it doesn't hurt to do it again.
-	 */
-	if ((sc->facts->IOCCapabilities &
-	    MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY) == 0) {
-		mps_diag_reset(sc, NO_SLEEP);
-		if ((error = mps_transition_ready(sc)) != 0)
-			return (error);
-	}
-
-	/*
-	 * Set flag if IR Firmware is loaded.
-	 */
-	if (sc->facts->IOCCapabilities &
-	    MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID)
-		sc->ir_firmware = 1;
 
 	/*
-	 * Check if controller supports FW diag buffers and set flag to enable
-	 * each type.
-	 */
-	if (sc->facts->IOCCapabilities &
-	    MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
-		sc->fw_diag_buffer_list[MPI2_DIAG_BUF_TYPE_TRACE].enabled =
-		    TRUE;
-	if (sc->facts->IOCCapabilities &
-	    MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
-		sc->fw_diag_buffer_list[MPI2_DIAG_BUF_TYPE_SNAPSHOT].enabled =
-		    TRUE;
-	if (sc->facts->IOCCapabilities &
-	    MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
-		sc->fw_diag_buffer_list[MPI2_DIAG_BUF_TYPE_EXTENDED].enabled =
-		    TRUE;
-
-	/*
-	 * Set flag if EEDP is supported and if TLR is supported.
-	 */
-	if (sc->facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP)
-		sc->eedp_enabled = TRUE;
-	if (sc->facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR)
-		sc->control_TLR = TRUE;
-
-	/*
-	 * Size the queues. Since the reply queues always need one free entry,
-	 * we'll just deduct one reply message here.
-	 */
-	sc->num_reqs = MIN(MPS_REQ_FRAMES, sc->facts->RequestCredit);
-	sc->num_replies = MIN(MPS_REPLY_FRAMES + MPS_EVT_REPLY_FRAMES,
-	    sc->facts->MaxReplyDescriptorPostQueueDepth) - 1;
-	TAILQ_INIT(&sc->req_list);
-	TAILQ_INIT(&sc->high_priority_req_list);
-	TAILQ_INIT(&sc->chain_list);
-	TAILQ_INIT(&sc->tm_list);
-
-	if (((error = mps_alloc_queues(sc)) != 0) ||
-	    ((error = mps_alloc_replies(sc)) != 0) ||
-	    ((error = mps_alloc_requests(sc)) != 0)) {
-		mps_printf(sc, "%s failed to alloc\n", __func__);
-		mps_free(sc);
-		return (error);
-	}
-
-	if (((error = mps_init_queues(sc)) != 0) ||
-	    ((error = mps_transition_operational(sc)) != 0)) {
-		mps_printf(sc, "%s failed to transition operational\n", __func__);
-		mps_free(sc);
+	 * Get IOC Facts and allocate all structures based on this information.
+	 * A Diag Reset will also call mps_iocfacts_allocate and re-read the IOC
+	 * Facts. If relevant values have changed in IOC Facts, this function
+	 * will free all of the memory based on IOC Facts and reallocate that
+	 * memory.  If this fails, any allocated memory should already be freed.
+	 */
+	if ((error = mps_iocfacts_allocate(sc, TRUE)) != 0) {
+		mps_dprint(sc, MPS_FAULT, "%s IOC Facts based allocation "
+		    "failed with error %d\n", __func__, error);
 		return (error);
 	}
 
-	/*
-	 * Finish the queue initialization.
-	 * These are set here instead of in mps_init_queues() because the
-	 * IOC resets these values during the state transition in
-	 * mps_transition_operational().  The free index is set to 1
-	 * because the corresponding index in the IOC is set to 0, and the
-	 * IOC treats the queues as full if both are set to the same value.
-	 * Hence the reason that the queue can't hold all of the possible
-	 * replies.
-	 */
-	sc->replypostindex = 0;
-	mps_regwrite(sc, MPI2_REPLY_FREE_HOST_INDEX_OFFSET, sc->replyfreeindex);
-	mps_regwrite(sc, MPI2_REPLY_POST_HOST_INDEX_OFFSET, 0);
-
-	sc->pfacts = malloc(sizeof(MPI2_PORT_FACTS_REPLY) *
-	    sc->facts->NumberOfPorts, M_MPT2, M_ZERO|M_WAITOK);
-	if(!sc->pfacts) {
-		device_printf(sc->mps_dev, "Cannot allocate memory %s %d\n",
-		 __func__, __LINE__);
-		return (ENOMEM);
-	}
-	for (i = 0; i < sc->facts->NumberOfPorts; i++) {
-		if ((error = mps_get_portfacts(sc, &sc->pfacts[i], i)) != 0) {
-			mps_printf(sc, "%s failed to get portfacts for port %d\n",
-			    __func__, i);
-			mps_free(sc);
-			return (error);
-		}
-		mps_print_portfacts(sc, &sc->pfacts[i]);
-	}
-
-	/* Attach the subsystems so they can prepare their event masks. */
-	/* XXX Should be dynamic so that IM/IR and user modules can attach */
-	if (((error = mps_attach_log(sc)) != 0) ||
-	    ((error = mps_attach_sas(sc)) != 0) ||
-	    ((error = mps_attach_user(sc)) != 0)) {
-		mps_printf(sc, "%s failed to attach all subsystems: error %d\n",
-		    __func__, error);
-		mps_free(sc);
-		return (error);
-	}
-
-	if ((error = mps_pci_setup_interrupts(sc)) != 0) {
-		mps_printf(sc, "%s failed to setup interrupts\n", __func__);
-		mps_free(sc);
-		return (error);
-	}
-
-	/*
-	 * The static page function currently read is ioc page8.  Others can be
-	 * added in future.
-	 */
-	mps_base_static_config_pages(sc);
-
 	/* Start the periodic watchdog check on the IOC Doorbell */
 	mps_periodic(sc);
 
@@ -1297,7 +1500,7 @@ mps_attach(struct mps_softc *sc)
 	sc->mps_ich.ich_func = mps_startup;
 	sc->mps_ich.ich_arg = sc;
 	if (config_intrhook_establish(&sc->mps_ich) != 0) {
-		mps_dprint(sc, MPS_FAULT, "Cannot establish MPS config hook\n");
+		mps_dprint(sc, MPS_ERROR, "Cannot establish MPS config hook\n");
 		error = EINVAL;
 	}
 
@@ -1308,7 +1511,7 @@ mps_attach(struct mps_softc *sc)
 	    mpssas_ir_shutdown, sc, SHUTDOWN_PRI_DEFAULT);
 
 	if (sc->shutdown_eh == NULL)
-		mps_dprint(sc, MPS_FAULT, "shutdown event registration "
+		mps_dprint(sc, MPS_ERROR, "shutdown event registration "
 		    "failed\n");
 
 	mps_setup_sysctl(sc);
@@ -1328,7 +1531,9 @@ mps_startup(void *arg)
 
 	mps_lock(sc);
 	mps_unmask_intr(sc);
+
 	/* initialize device mapping tables */
+	mps_base_static_config_pages(sc);
 	mps_mapping_initialize(sc);
 	mpssas_startup(sc);
 	mps_unlock(sc);
@@ -1347,8 +1552,7 @@ mps_periodic(void *arg)
 
 	db = mps_regread(sc, MPI2_DOORBELL_OFFSET);
 	if ((db & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
-		device_printf(sc->mps_dev, "IOC Fault 0x%08x, Resetting\n", db);
-
+		mps_dprint(sc, MPS_FAULT, "IOC Fault 0x%08x, Resetting\n", db);
 		mps_reinit(sc);
 	}
 
@@ -1365,12 +1569,13 @@ mps_log_evt_handler(struct mps_softc *sc
 
 	switch (event->Event) {
 	case MPI2_EVENT_LOG_DATA:
-		device_printf(sc->mps_dev, "MPI2_EVENT_LOG_DATA:\n");
-		hexdump(event->EventData, event->EventDataLength, NULL, 0);
+		mps_dprint(sc, MPS_EVENT, "MPI2_EVENT_LOG_DATA:\n");
+		if (sc->mps_debug & MPS_EVENT)
+			hexdump(event->EventData, event->EventDataLength, NULL, 0);
 		break;
 	case MPI2_EVENT_LOG_ENTRY_ADDED:
 		entry = (MPI2_EVENT_DATA_LOG_ENTRY_ADDED *)event->EventData;
-		mps_dprint(sc, MPS_INFO, "MPI2_EVENT_LOG_ENTRY_ADDED event "
+		mps_dprint(sc, MPS_EVENT, "MPI2_EVENT_LOG_ENTRY_ADDED event "
 		    "0x%x Sequence %d:\n", entry->LogEntryQualifier,
 		     entry->LogSequence);
 		break;
@@ -1411,8 +1616,7 @@ mps_detach_log(struct mps_softc *sc)
 int
 mps_free(struct mps_softc *sc)
 {
-	struct mps_command *cm;
-	int i, error;
+	int error;
 
 	/* Turn off the watchdog */
 	mps_lock(sc);
@@ -1438,62 +1642,15 @@ mps_free(struct mps_softc *sc)
 	if (sc->facts != NULL)

*** DIFF OUTPUT TRUNCATED AT 1000 LINES ***


More information about the svn-src-stable-9 mailing list