From 83492c139168974233c9bf448096d09f370e4d7b Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Wed, 29 Oct 2014 20:04:19 +0100
Subject: [PATCH] WIP mps(4) upgrade

---
 sys/dev/raid/mps/mpi/mpi2.h           |   6 +-
 sys/dev/raid/mps/mpi/mpi2_cnfg.h      |   6 +-
 sys/dev/raid/mps/mpi/mpi2_hbd.h       |   6 +-
 sys/dev/raid/mps/mpi/mpi2_history.txt |   6 +-
 sys/dev/raid/mps/mpi/mpi2_init.h      |   6 +-
 sys/dev/raid/mps/mpi/mpi2_ioc.h       |   6 +-
 sys/dev/raid/mps/mpi/mpi2_ra.h        |   6 +-
 sys/dev/raid/mps/mpi/mpi2_raid.h      |   6 +-
 sys/dev/raid/mps/mpi/mpi2_sas.h       |   6 +-
 sys/dev/raid/mps/mpi/mpi2_targ.h      |   6 +-
 sys/dev/raid/mps/mpi/mpi2_tool.h      |   6 +-
 sys/dev/raid/mps/mpi/mpi2_type.h      |   6 +-
 sys/dev/raid/mps/mps.c                | 328 ++++++++++++++++++++++++----------
 sys/dev/raid/mps/mps_config.c         |   5 +-
 sys/dev/raid/mps/mps_ioctl.h          |   6 +-
 sys/dev/raid/mps/mps_mapping.c        |   4 +-
 sys/dev/raid/mps/mps_mapping.h        |   4 +-
 sys/dev/raid/mps/mps_pci.c            |   3 +-
 sys/dev/raid/mps/mps_sas.c            |  31 +---
 sys/dev/raid/mps/mpsvar.h             |   7 +-
 20 files changed, 293 insertions(+), 167 deletions(-)

diff --git a/sys/dev/raid/mps/mpi/mpi2.h b/sys/dev/raid/mps/mpi/mpi2.h
index 4196be7..7165389 100644
--- a/sys/dev/raid/mps/mpi/mpi2.h
+++ b/sys/dev/raid/mps/mpi/mpi2.h
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2011 LSI Corp.
+ * Copyright (c) 2011, 2012 LSI Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -25,11 +25,11 @@
  *
  * LSI MPT-Fusion Host Adapter FreeBSD
  *
- * $FreeBSD: src/sys/dev/mps/mpi/mpi2.h,v 1.2 2012/01/26 18:17:21 ken Exp $
+ * $FreeBSD: src/sys/dev/mps/mpi/mpi2.h,v 1.4 2012/11/17 01:52:05 svnexp Exp $
  */
 
 /*
- *  Copyright (c) 2000-2011 LSI Corporation.
+ *  Copyright (c) 2000-2012 LSI Corporation.
  *
  *
  *           Name:  mpi2.h
diff --git a/sys/dev/raid/mps/mpi/mpi2_cnfg.h b/sys/dev/raid/mps/mpi/mpi2_cnfg.h
index fc0f6d8..a0eb19b 100644
--- a/sys/dev/raid/mps/mpi/mpi2_cnfg.h
+++ b/sys/dev/raid/mps/mpi/mpi2_cnfg.h
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2011 LSI Corp.
+ * Copyright (c) 2011, 2012 LSI Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -25,11 +25,11 @@
  *
  * LSI MPT-Fusion Host Adapter FreeBSD
  *
- * $FreeBSD: src/sys/dev/mps/mpi/mpi2_cnfg.h,v 1.2 2012/01/26 18:17:21 ken Exp $
+ * $FreeBSD: src/sys/dev/mps/mpi/mpi2_cnfg.h,v 1.4 2012/11/17 01:52:05 svnexp Exp $
  */
 
 /*
- *  Copyright (c) 2000-2011 LSI Corporation.
+ *  Copyright (c) 2000-2012 LSI Corporation.
  *
  *
  *           Name:  mpi2_cnfg.h
diff --git a/sys/dev/raid/mps/mpi/mpi2_hbd.h b/sys/dev/raid/mps/mpi/mpi2_hbd.h
index 41ac3fb..e6c3a32 100644
--- a/sys/dev/raid/mps/mpi/mpi2_hbd.h
+++ b/sys/dev/raid/mps/mpi/mpi2_hbd.h
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2011 LSI Corp.
+ * Copyright (c) 2011, 2012 LSI Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -25,11 +25,11 @@
  *
  * LSI MPT-Fusion Host Adapter FreeBSD
  *
- * $FreeBSD: src/sys/dev/mps/mpi/mpi2_hbd.h,v 1.2 2012/01/26 18:17:21 ken Exp $
+ * $FreeBSD: src/sys/dev/mps/mpi/mpi2_hbd.h,v 1.4 2012/11/17 01:52:05 svnexp Exp $
  */
 
 /*
- *  Copyright (c) 2009-2011 LSI Corporation.
+ *  Copyright (c) 2009-2012 LSI Corporation.
  *
  *
  *           Name:  mpi2_hbd.h
diff --git a/sys/dev/raid/mps/mpi/mpi2_history.txt b/sys/dev/raid/mps/mpi/mpi2_history.txt
index a87375f..8f910a6 100644
--- a/sys/dev/raid/mps/mpi/mpi2_history.txt
+++ b/sys/dev/raid/mps/mpi/mpi2_history.txt
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2011 LSI Corp.
+ * Copyright (c) 2011, 2012 LSI Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -25,14 +25,14 @@
  *
  * LSI MPT-Fusion Host Adapter FreeBSD
  *
- * $FreeBSD: src/sys/dev/mps/mpi/mpi2_history.txt,v 1.2 2012/01/26 18:17:21 ken Exp $
+ * $FreeBSD: src/sys/dev/mps/mpi/mpi2_history.txt,v 1.4 2012/11/17 01:52:05 svnexp Exp $
  */
 
  ==============================
  Fusion-MPT MPI 2.0 Header File Change History
  ==============================
 
- Copyright (c) 2000-2011 LSI Corporation.
+ Copyright (c) 2000-2012 LSI Corporation.
 
  ---------------------------------------
  Header Set Release Version:    02.00.18
diff --git a/sys/dev/raid/mps/mpi/mpi2_init.h b/sys/dev/raid/mps/mpi/mpi2_init.h
index 4c6ec98..056c61d 100644
--- a/sys/dev/raid/mps/mpi/mpi2_init.h
+++ b/sys/dev/raid/mps/mpi/mpi2_init.h
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2011 LSI Corp.
+ * Copyright (c) 2011, 2012 LSI Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -25,11 +25,11 @@
  *
  * LSI MPT-Fusion Host Adapter FreeBSD
  *
- * $FreeBSD: src/sys/dev/mps/mpi/mpi2_init.h,v 1.2 2012/01/26 18:17:21 ken Exp $
+ * $FreeBSD: src/sys/dev/mps/mpi/mpi2_init.h,v 1.4 2012/11/17 01:52:05 svnexp Exp $
  */
 
 /*
- *  Copyright (c) 2000-2011 LSI Corporation.
+ *  Copyright (c) 2000-2012 LSI Corporation.
  *
  *
  *           Name:  mpi2_init.h
diff --git a/sys/dev/raid/mps/mpi/mpi2_ioc.h b/sys/dev/raid/mps/mpi/mpi2_ioc.h
index 7dd0b36..f82928f 100644
--- a/sys/dev/raid/mps/mpi/mpi2_ioc.h
+++ b/sys/dev/raid/mps/mpi/mpi2_ioc.h
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2011 LSI Corp.
+ * Copyright (c) 2011, 2012 LSI Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -25,11 +25,11 @@
  *
  * LSI MPT-Fusion Host Adapter FreeBSD
  *
- * $FreeBSD: src/sys/dev/mps/mpi/mpi2_ioc.h,v 1.2 2012/01/26 18:17:21 ken Exp $
+ * $FreeBSD: src/sys/dev/mps/mpi/mpi2_ioc.h,v 1.4 2012/11/17 01:52:05 svnexp Exp $
  */
 
 /*
- *  Copyright (c) 2000-2011 LSI Corporation.
+ *  Copyright (c) 2000-2012 LSI Corporation.
  *
  *
  *           Name:  mpi2_ioc.h
diff --git a/sys/dev/raid/mps/mpi/mpi2_ra.h b/sys/dev/raid/mps/mpi/mpi2_ra.h
index 4d9e130..4960119 100644
--- a/sys/dev/raid/mps/mpi/mpi2_ra.h
+++ b/sys/dev/raid/mps/mpi/mpi2_ra.h
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2011 LSI Corp.
+ * Copyright (c) 2011, 2012 LSI Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -25,11 +25,11 @@
  *
  * LSI MPT-Fusion Host Adapter FreeBSD
  *
- * $FreeBSD: src/sys/dev/mps/mpi/mpi2_ra.h,v 1.2 2012/01/26 18:17:21 ken Exp $
+ * $FreeBSD: src/sys/dev/mps/mpi/mpi2_ra.h,v 1.4 2012/11/17 01:52:05 svnexp Exp $
  */
 
 /*
- *  Copyright (c) 2011 LSI Corporation.
+ *  Copyright (c) 2012 LSI Corporation.
  *
  *
  *           Name:  mpi2_ra.h
diff --git a/sys/dev/raid/mps/mpi/mpi2_raid.h b/sys/dev/raid/mps/mpi/mpi2_raid.h
index f1f1988..e95244e 100644
--- a/sys/dev/raid/mps/mpi/mpi2_raid.h
+++ b/sys/dev/raid/mps/mpi/mpi2_raid.h
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2011 LSI Corp.
+ * Copyright (c) 2011, 2012 LSI Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -25,11 +25,11 @@
  *
  * LSI MPT-Fusion Host Adapter FreeBSD
  *
- * $FreeBSD: src/sys/dev/mps/mpi/mpi2_raid.h,v 1.2 2012/01/26 18:17:21 ken Exp $
+ * $FreeBSD: src/sys/dev/mps/mpi/mpi2_raid.h,v 1.4 2012/11/17 01:52:05 svnexp Exp $
  */
 
 /*
- *  Copyright (c) 2000-2011 LSI Corporation.
+ *  Copyright (c) 2000-2012 LSI Corporation.
  *
  *
  *           Name:  mpi2_raid.h
diff --git a/sys/dev/raid/mps/mpi/mpi2_sas.h b/sys/dev/raid/mps/mpi/mpi2_sas.h
index a232d92..d316e03 100644
--- a/sys/dev/raid/mps/mpi/mpi2_sas.h
+++ b/sys/dev/raid/mps/mpi/mpi2_sas.h
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2011 LSI Corp.
+ * Copyright (c) 2011, 2012 LSI Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -25,11 +25,11 @@
  *
  * LSI MPT-Fusion Host Adapter FreeBSD
  *
- * $FreeBSD: src/sys/dev/mps/mpi/mpi2_sas.h,v 1.2 2012/01/26 18:17:21 ken Exp $
+ * $FreeBSD: src/sys/dev/mps/mpi/mpi2_sas.h,v 1.4 2012/11/17 01:52:05 svnexp Exp $
  */
 
 /*
- *  Copyright (c) 2000-2011 LSI Corporation.
+ *  Copyright (c) 2000-2012 LSI Corporation.
  *
  *
  *           Name:  mpi2_sas.h
diff --git a/sys/dev/raid/mps/mpi/mpi2_targ.h b/sys/dev/raid/mps/mpi/mpi2_targ.h
index f52d578..7b547f2 100644
--- a/sys/dev/raid/mps/mpi/mpi2_targ.h
+++ b/sys/dev/raid/mps/mpi/mpi2_targ.h
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2011 LSI Corp.
+ * Copyright (c) 2011, 2012 LSI Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -25,11 +25,11 @@
  *
  * LSI MPT-Fusion Host Adapter FreeBSD
  *
- * $FreeBSD: src/sys/dev/mps/mpi/mpi2_targ.h,v 1.2 2012/01/26 18:17:21 ken Exp $
+ * $FreeBSD: src/sys/dev/mps/mpi/mpi2_targ.h,v 1.4 2012/11/17 01:52:05 svnexp Exp $
  */
 
 /*
- *  Copyright (c) 2000-2011 LSI Corporation.
+ *  Copyright (c) 2000-2012 LSI Corporation.
  *
  *
  *           Name:  mpi2_targ.h
diff --git a/sys/dev/raid/mps/mpi/mpi2_tool.h b/sys/dev/raid/mps/mpi/mpi2_tool.h
index 249bb87..5dccbd2 100644
--- a/sys/dev/raid/mps/mpi/mpi2_tool.h
+++ b/sys/dev/raid/mps/mpi/mpi2_tool.h
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2011 LSI Corp.
+ * Copyright (c) 2011, 2012 LSI Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -25,11 +25,11 @@
  *
  * LSI MPT-Fusion Host Adapter FreeBSD
  *
- * $FreeBSD: src/sys/dev/mps/mpi/mpi2_tool.h,v 1.2 2012/01/26 18:17:21 ken Exp $
+ * $FreeBSD: src/sys/dev/mps/mpi/mpi2_tool.h,v 1.4 2012/11/17 01:52:05 svnexp Exp $
  */
 
 /*
- *  Copyright (c) 2000-2011 LSI Corporation.
+ *  Copyright (c) 2000-2012 LSI Corporation.
  *
  *
  *           Name:  mpi2_tool.h
diff --git a/sys/dev/raid/mps/mpi/mpi2_type.h b/sys/dev/raid/mps/mpi/mpi2_type.h
index b005cff..ff5fd32 100644
--- a/sys/dev/raid/mps/mpi/mpi2_type.h
+++ b/sys/dev/raid/mps/mpi/mpi2_type.h
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2011 LSI Corp.
+ * Copyright (c) 2011, 2012 LSI Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -25,11 +25,11 @@
  *
  * LSI MPT-Fusion Host Adapter FreeBSD
  *
- * $FreeBSD: src/sys/dev/mps/mpi/mpi2_type.h,v 1.2 2012/01/26 18:17:21 ken Exp $
+ * $FreeBSD: src/sys/dev/mps/mpi/mpi2_type.h,v 1.4 2012/11/17 01:52:05 svnexp Exp $
  */
 
 /*
- *  Copyright (c) 2000-2011 LSI Corporation.
+ *  Copyright (c) 2000-2012 LSI Corporation.
  *
  *
  *           Name:  mpi2_type.h
diff --git a/sys/dev/raid/mps/mps.c b/sys/dev/raid/mps/mps.c
index 7260fb0..85ccb5b 100644
--- a/sys/dev/raid/mps/mps.c
+++ b/sys/dev/raid/mps/mps.c
@@ -1,31 +1,6 @@
 /*-
  * Copyright (c) 2009 Yahoo! Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- */
-/*-
- * Copyright (c) 2011 LSI Corp.
+ * Copyright (c) 2012 LSI Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -51,7 +26,7 @@
  *
  * LSI MPT-Fusion Host Adapter FreeBSD
  *
- * $FreeBSD: src/sys/dev/mps/mps.c,v 1.14 2012/01/26 18:17:21 ken Exp $
+ * $FreeBSD: src/sys/dev/mps/mps.c,v 1.17 2012/08/01 17:31:31 mav Exp $
  */
 
 /* Communications core for LSI MPT2 */
@@ -76,6 +51,7 @@
 #include <sys/eventhandler.h>
 
 #include <sys/rman.h>
+#include <sys/proc.h>
 
 #include <bus/pci/pcivar.h>
 
@@ -106,7 +82,7 @@ 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_wait_db_ack(struct mps_softc *sc, int timeout);
 SYSCTL_NODE(_hw, OID_AUTO, mps, CTLFLAG_RD, 0, "MPS Driver Parameters");
 
 MALLOC_DEFINE(M_MPT2, "mps", "mpt2 driver memory");
@@ -117,6 +93,29 @@ MALLOC_DEFINE(M_MPT2, "mps", "mpt2 driver memory");
  */
 static char mpt2_reset_magic[] = { 0x00, 0x0f, 0x04, 0x0b, 0x02, 0x07, 0x0d };
 
+/* Added this union to smoothly convert le64toh cm->cm_desc.Words.
+ * Compiler only support unint64_t to be passed as argument.
+ * Otherwise it will through below error
+ * "aggregate value used where an integer was expected"
+ */
+
+typedef union _reply_descriptor {
+        u64 word;
+        struct {
+                u32 low;
+                u32 high;
+        } u;
+} reply_descriptor,address_descriptor;
+
+/*
+ * sleep_flag can be either CAN_SLEEP or NO_SLEEP.
+ * If this function is called from process context, it can sleep
+ * and there is no harm to sleep, in case if this fuction is called
+ * from Interrupt handler, we can not sleep and need NO_SLEEP flag set.
+ * based on sleep flags driver will call either msleep, pause or DELAY.
+ * msleep and pause are of same variant, but pause is used when mps_mtx
+ * is not hold by driver.
+ */
 static int
 mps_diag_reset(struct mps_softc *sc)
 {
@@ -134,8 +133,12 @@ mps_diag_reset(struct mps_softc *sc)
 		for (i = 0; i < sizeof(mpt2_reset_magic); i++)
 			mps_regwrite(sc, MPI2_WRITE_SEQUENCE_OFFSET,
 			    mpt2_reset_magic[i]);
-
-		DELAY(100 * 1000);
+		/* wait 100 msec */
+		if (lockowned(&sc->mps_lock))
+			lksleep(&sc->msleep_fake_chan, &sc->mps_lock, 0,
+			    "mpsdiag", hz/10);
+		else
+			tsleep(mps_diag_reset, 0, "mpsdiag", hz/10);
 
 		reg = mps_regread(sc, MPI2_HOST_DIAGNOSTIC_OFFSET);
 		if (reg & MPI2_DIAG_DIAG_WRITE_ENABLE) {
@@ -153,7 +156,13 @@ mps_diag_reset(struct mps_softc *sc)
 	/* Wait up to 300 seconds in 50ms intervals */
 	error = ETIMEDOUT;
 	for (i = 0; i < 60000; i++) {
-		DELAY(50000);
+		/* wait 50 msec */
+		if (lockowned(&sc->mps_lock))
+			lksleep(&sc->msleep_fake_chan, &sc->mps_lock, 0,
+			    "mpsdiag", hz/20);
+		else
+			tsleep(mps_diag_reset, 0, "mpsdiag", hz/20);
+
 		reg = mps_regread(sc, MPI2_DOORBELL_OFFSET);
 		if ((reg & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_RESET) {
 			error = 0;
@@ -177,7 +186,12 @@ mps_message_unit_reset(struct mps_softc *sc)
 	mps_regwrite(sc, MPI2_DOORBELL_OFFSET,
 	    MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET <<
 	    MPI2_DOORBELL_FUNCTION_SHIFT);
-	DELAY(50000);
+
+	if (mps_wait_db_ack(sc, 5) != 0) {
+		mps_dprint(sc, MPS_FAULT,
+		    "Doorbell handshake failed : <%s>\n", __func__);
+		return (ETIMEDOUT);
+	}
 
 	return (0);
 }
@@ -362,18 +376,56 @@ mps_reinit(struct mps_softc *sc)
 	return 0;
 }
 
-/* Wait for the chip to ACK a word that we've put into its FIFO */
+/*
+ * 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.
+ */
 static int
-mps_wait_db_ack(struct mps_softc *sc)
+mps_wait_db_ack(struct mps_softc *sc, int timeout)
 {
-	int retry;
+	u32 cntdn, count;
+	u32 int_status;
+	u32 doorbell;
+
+	count = 0;
+	cntdn = 1000 * timeout;
+	do {
+		int_status =
+		    mps_regread(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET);
+		if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
+			mps_dprint(sc, MPS_INFO,
+			    "%s: successfull count(%d), timeout(%d)\n",
+			    __func__, count, timeout);
+			return 0;
+		} else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
+			doorbell = mps_regread(sc, MPI2_DOORBELL_OFFSET);
+			if ((doorbell & MPI2_IOC_STATE_MASK) ==
+			    MPI2_IOC_STATE_FAULT) {
+				mps_dprint(sc, MPS_FAULT,
+				    "fault_state(0x%04x)!\n", doorbell);
+				return (EFAULT);
+			}
+		} else if (int_status == 0xFFFFFFFF) {
+			goto out;
+		}
 
-	for (retry = 0; retry < MPS_DB_MAX_WAIT; retry++) {
-		if ((mps_regread(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET) &
-		    MPI2_HIS_SYS2IOC_DB_STATUS) == 0)
-			return (0);
-		DELAY(2000);
-	}
+		/*
+		 * If it can sleep, sleep for 1 milisecond, else busy loop
+		 * for 0.5 milisecond
+		 */
+		if (lockowned(&sc->mps_lock))
+			lksleep(&sc->msleep_fake_chan, &sc->mps_lock, 0,
+			    "mpsdba", hz/1000);
+		else
+			tsleep(mps_wait_db_ack, 0, "mpsdba", hz/1000);
+		count++;
+	} while (--cntdn);
+
+out:
+	mps_dprint(sc, MPS_FAULT, "%s: failed due to timeout count(%d), "
+	    "int_status(%x)!\n", __func__, count, int_status);
 	return (ETIMEDOUT);
 }
 
@@ -419,12 +471,13 @@ mps_request_sync(struct mps_softc *sc, void *req, MPI2_DEFAULT_REPLY *reply,
 
 	/* Step 4 */
 	if (mps_wait_db_int(sc) ||
-	    (mps_regread(sc, MPI2_DOORBELL_OFFSET) & MPI2_DOORBELL_USED) == 0) {
+	    (mps_regread(sc, MPI2_DOORBELL_OFFSET) & MPI2_DOORBELL_USED) == 0)
+	{
 		mps_dprint(sc, MPS_FAULT, "Doorbell failed to activate\n");
 		return (ENXIO);
 	}
 	mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
-	if (mps_wait_db_ack(sc) != 0) {
+	if (mps_wait_db_ack(sc, 5) != 0) {
 		mps_dprint(sc, MPS_FAULT, "Doorbell handshake failed\n");
 		return (ENXIO);
 	}
@@ -433,8 +486,8 @@ mps_request_sync(struct mps_softc *sc, void *req, MPI2_DEFAULT_REPLY *reply,
 	/* Clock out the message data synchronously in 32-bit dwords*/
 	data32 = (uint32_t *)req;
 	for (i = 0; i < count; i++) {
-		mps_regwrite(sc, MPI2_DOORBELL_OFFSET, data32[i]);
-		if (mps_wait_db_ack(sc) != 0) {
+		mps_regwrite(sc, MPI2_DOORBELL_OFFSET, htole32(data32[i]));
+		if (mps_wait_db_ack(sc, 5) != 0) {
 			mps_dprint(sc, MPS_FAULT,
 			    "Timeout while writing doorbell\n");
 			return (ENXIO);
@@ -519,6 +572,7 @@ mps_request_sync(struct mps_softc *sc, void *req, MPI2_DEFAULT_REPLY *reply,
 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__,
 	    cm->cm_desc.Default.SMID, cm, cm->cm_ccb);
@@ -530,11 +584,12 @@ mps_enqueue_request(struct mps_softc *sc, struct mps_command *cm)
 
 	if (++sc->io_cmds_active > sc->io_cmds_highwater)
 		sc->io_cmds_highwater++;
-
-	mps_regwrite(sc, MPI2_REQUEST_DESCRIPTOR_POST_LOW_OFFSET,
-	    cm->cm_desc.Words.Low);
-	mps_regwrite(sc, MPI2_REQUEST_DESCRIPTOR_POST_HIGH_OFFSET,
-	    cm->cm_desc.Words.High);
+	rd.u.low = cm->cm_desc.Words.Low;
+	rd.u.high = cm->cm_desc.Words.High;
+	rd.word = htole64(rd.word);
+	/* TODO-We may need to make below regwrite atomic */
+	mps_regwrite(sc, MPI2_REQUEST_DESCRIPTOR_POST_LOW_OFFSET, rd.u.low);
+	mps_regwrite(sc, MPI2_REQUEST_DESCRIPTOR_POST_HIGH_OFFSET, rd.u.high);
 }
 
 /*
@@ -618,19 +673,19 @@ mps_send_iocinit(struct mps_softc *sc)
 	 */
 	init.Function = MPI2_FUNCTION_IOC_INIT;
 	init.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
-	init.MsgVersion = MPI2_VERSION;
-	init.HeaderVersion = MPI2_HEADER_VERSION;
-	init.SystemRequestFrameSize = sc->facts->IOCRequestFrameSize;
-	init.ReplyDescriptorPostQueueDepth = sc->pqdepth;
-	init.ReplyFreeQueueDepth = sc->fqdepth;
+	init.MsgVersion = htole16(MPI2_VERSION);
+	init.HeaderVersion = htole16(MPI2_HEADER_VERSION);
+	init.SystemRequestFrameSize = htole16(sc->facts->IOCRequestFrameSize);
+	init.ReplyDescriptorPostQueueDepth = htole16(sc->pqdepth);
+	init.ReplyFreeQueueDepth = htole16(sc->fqdepth);
 	init.SenseBufferAddressHigh = 0;
 	init.SystemReplyAddressHigh = 0;
 	init.SystemRequestFrameBaseAddress.High = 0;
-	init.SystemRequestFrameBaseAddress.Low = (uint32_t)sc->req_busaddr;
+	init.SystemRequestFrameBaseAddress.Low = htole32((uint32_t)sc->req_busaddr);
 	init.ReplyDescriptorPostQueueAddress.High = 0;
-	init.ReplyDescriptorPostQueueAddress.Low = (uint32_t)sc->post_busaddr;
+	init.ReplyDescriptorPostQueueAddress.Low = htole32((uint32_t)sc->post_busaddr);
 	init.ReplyFreeQueueAddress.High = 0;
-	init.ReplyFreeQueueAddress.Low = (uint32_t)sc->free_busaddr;
+	init.ReplyFreeQueueAddress.Low = htole32((uint32_t)sc->free_busaddr);
 	init.TimeStamp.High = 0;
 	init.TimeStamp.Low = (uint32_t)time_uptime;
 
@@ -1289,7 +1344,7 @@ mps_log_evt_handler(struct mps_softc *sc, uintptr_t data,
 static int
 mps_attach_log(struct mps_softc *sc)
 {
-	uint8_t events[16];
+	u32 events[MPI2_EVENT_NOTIFY_EVENTMASK_WORDS];
 
 	bzero(events, 16);
 	setbit(events, MPI2_EVENT_LOG_DATA);
@@ -1436,6 +1491,64 @@ mps_complete_command(struct mps_command *cm)
 	}
 }
 
+static void
+mps_sas_log_info(struct mps_softc *sc , u32 log_info)
+{
+	union loginfo_type {
+		u32     loginfo;
+		struct {
+			u32     subcode:16;
+			u32     code:8;
+			u32     originator:4;
+			u32     bus_type:4;
+		} dw;
+	};
+	union loginfo_type sas_loginfo;
+	char *originator_str = NULL;
+
+	sas_loginfo.loginfo = log_info;
+	if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
+		return;
+
+	/* each nexus loss loginfo */
+	if (log_info == 0x31170000)
+		return;
+
+	/* eat the loginfos associated with task aborts */
+	if ((log_info == 30050000 || log_info ==
+	    0x31140000 || log_info == 0x31130000))
+		return;
+
+	switch (sas_loginfo.dw.originator) {
+	case 0:
+		originator_str = "IOP";
+		break;
+	case 1:
+		originator_str = "PL";
+		break;
+	case 2:
+		originator_str = "IR";
+		break;
+	}
+
+	mps_dprint(sc, MPS_INFO, "log_info(0x%08x): originator(%s), "
+	    "code(0x%02x), sub_code(0x%04x)\n", log_info,
+	originator_str, sas_loginfo.dw.code,
+	sas_loginfo.dw.subcode);
+}
+
+static void
+mps_display_reply_info(struct mps_softc *sc, uint8_t *reply)
+{
+	MPI2DefaultReply_t *mpi_reply;
+	u16 sc_status;
+
+	mpi_reply = (MPI2DefaultReply_t*)reply;
+	sc_status = le16toh(mpi_reply->IOCStatus);
+	if (sc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
+		mps_sas_log_info(sc, le32toh(mpi_reply->IOCLogInfo));
+}
+
 void
 mps_intr(void *data)
 {
@@ -1503,7 +1616,7 @@ mps_intr_locked(void *data)
 		flags = desc->Default.ReplyFlags &
 		    MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
 		if ((flags == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
-		 || (desc->Words.High == 0xffffffff))
+		 || (le32toh(desc->Words.High) == 0xffffffff))
 			break;
 
 		/* increment the replypostindex now, so that event handlers
@@ -1518,7 +1631,7 @@ mps_intr_locked(void *data)
 
 		switch (flags) {
 		case MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS:
-			cm = &sc->commands[desc->SCSIIOSuccess.SMID];
+			cm = &sc->commands[le16toh(desc->SCSIIOSuccess.SMID)];
 			cm->cm_reply = NULL;
 			break;
 		case MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY:
@@ -1554,9 +1667,10 @@ mps_intr_locked(void *data)
 				       sc->reply_frames, sc->fqdepth,
 				       sc->facts->ReplyFrameSize * 4);
 				kprintf("%s: baddr %#x,\n", __func__, baddr);
+				/* LSI-TODO. See Linux Code. Need Gracefull exit*/
 				panic("Reply address out of range");
 			}
-			if (desc->AddressReply.SMID == 0) {
+			if (le16toh(desc->AddressReply.SMID) == 0) {
 				if (((MPI2_DEFAULT_REPLY *)reply)->Function ==
 				    MPI2_FUNCTION_DIAG_BUFFER_POST) {
 					/*
@@ -1568,7 +1682,7 @@ mps_intr_locked(void *data)
 					 */
 					rel_rep =
 					    (MPI2_DIAG_RELEASE_REPLY *)reply;
-					if (rel_rep->IOCStatus ==
+					if (le16toh(rel_rep->IOCStatus) ==
 					    MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED)
 					    {
 						pBuffer =
@@ -1584,10 +1698,10 @@ mps_intr_locked(void *data)
 					    (MPI2_EVENT_NOTIFICATION_REPLY *)
 					    reply);
 			} else {
-				cm = &sc->commands[desc->AddressReply.SMID];
+				cm = &sc->commands[le16toh(desc->AddressReply.SMID)];
 				cm->cm_reply = reply;
 				cm->cm_reply_data =
-				    desc->AddressReply.ReplyFrameAddress;
+				    le32toh(desc->AddressReply.ReplyFrameAddress);
 			}
 			break;
 		}
@@ -1602,8 +1716,12 @@ mps_intr_locked(void *data)
 			break;
 		}
 
-		if (cm != NULL)
+		if (cm != NULL) {
+			/* Print Error reply frame */
+			if (cm->cm_reply)
+				mps_display_reply_info(sc,cm->cm_reply);
 			mps_complete_command(cm);
+		}
 
 		desc->Words.Low = 0xffffffff;
 		desc->Words.High = 0xffffffff;
@@ -1626,7 +1744,7 @@ mps_dispatch_event(struct mps_softc *sc, uintptr_t data,
 	struct mps_event_handle *eh;
 	int event, handled = 0;
 
-	event = reply->Event;
+	event = le16toh(reply->Event);
 	TAILQ_FOREACH(eh, &sc->event_list, eh_list) {
 		if (isset(eh->mask, event)) {
 			eh->callback(sc, data, reply);
@@ -1635,7 +1753,8 @@ mps_dispatch_event(struct mps_softc *sc, uintptr_t data,
 	}
 
 	if (handled == 0)
-		device_printf(sc->mps_dev, "Unhandled event 0x%x\n", event);
+		device_printf(sc->mps_dev, "Unhandled event 0x%x\n",
+		    le16toh(event));
 
 	/*
 	 * This is the only place that the event/reply should be freed.
@@ -1666,7 +1785,7 @@ mps_reregister_events_complete(struct mps_softc *sc, struct mps_command *cm)
  * suitable for the controller.
  */
 int
-mps_register_events(struct mps_softc *sc, uint8_t *mask,
+mps_register_events(struct mps_softc *sc, u32 *mask,
     mps_evt_callback_t *cb, void *data, struct mps_event_handle **handle)
 {
 	struct mps_event_handle *eh;
@@ -1685,24 +1804,24 @@ mps_register_events(struct mps_softc *sc, uint8_t *mask,
 
 int
 mps_update_events(struct mps_softc *sc, struct mps_event_handle *handle,
-    uint8_t *mask)
+    u32 *mask)
 {
 	MPI2_EVENT_NOTIFICATION_REQUEST *evtreq;
 	MPI2_EVENT_NOTIFICATION_REPLY *reply;
 	struct mps_command *cm;
-	struct mps_event_handle *eh;
 	int error, i;
 
 	mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
 
 	if ((mask != NULL) && (handle != NULL))
-		bcopy(mask, &handle->mask[0], 16);
-	memset(sc->event_mask, 0xff, 16);
+		bcopy(mask, &handle->mask[0], sizeof(u32) *
+		    MPI2_EVENT_NOTIFY_EVENTMASK_WORDS);
 
-	TAILQ_FOREACH(eh, &sc->event_list, eh_list) {
-		for (i = 0; i < 16; i++)
-			sc->event_mask[i] &= ~eh->mask[i];
-	}
+	for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
+		sc->event_mask[i] = -1;
+
+	for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
+		sc->event_mask[i] &= ~handle->mask[i];
 
 	if ((cm = mps_alloc_command(sc)) == NULL)
 		return (EBUSY);
@@ -1714,10 +1833,12 @@ mps_update_events(struct mps_softc *sc, struct mps_event_handle *handle,
 	{
 		u_char fullmask[16];
 		memset(fullmask, 0x00, 16);
-		bcopy(fullmask, (uint8_t *)&evtreq->EventMasks, 16);
+		bcopy(fullmask, &evtreq->EventMasks[0], sizeof(u32) *
+		    MPI2_EVENT_NOTIFY_EVENTMASK_WORDS);
 	}
 #else
-		bcopy(sc->event_mask, (uint8_t *)&evtreq->EventMasks, 16);
+	for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
+		evtreq->EventMasks[i] = htole32(sc->event_mask[i]);
 #endif
 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
 	cm->cm_data = NULL;
@@ -1746,10 +1867,11 @@ mps_reregister_events(struct mps_softc *sc)
 
 	/* first, reregister events */
 
-	memset(sc->event_mask, 0xff, 16);
+	for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
+		sc->event_mask[i] = -1;
 
 	TAILQ_FOREACH(eh, &sc->event_list, eh_list) {
-		for (i = 0; i < 16; i++)
+		for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
 			sc->event_mask[i] &= ~eh->mask[i];
 	}
 
@@ -1763,10 +1885,12 @@ mps_reregister_events(struct mps_softc *sc)
 	{
 		u_char fullmask[16];
 		memset(fullmask, 0x00, 16);
-		bcopy(fullmask, (uint8_t *)&evtreq->EventMasks, 16);
+		bcopy(fullmask, &evtreq->EventMasks[0], sizeof(u32) *
+		    MPI2_EVENT_NOTIFY_EVENTMASK_WORDS);
 	}
 #else
-		bcopy(sc->event_mask, (uint8_t *)&evtreq->EventMasks, 16);
+	for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
+		evtreq->EventMasks[i] = htole32(sc->event_mask[i]);
 #endif
 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
 	cm->cm_data = NULL;
@@ -1778,13 +1902,12 @@ mps_reregister_events(struct mps_softc *sc)
 	return (error);
 }
 
-int
+void
 mps_deregister_events(struct mps_softc *sc, struct mps_event_handle *handle)
 {
 
 	TAILQ_REMOVE(&sc->event_list, handle, eh_list);
 	kfree(handle, M_MPT2);
-	return (mps_update_events(sc, NULL, NULL));
 }
 
 /*
@@ -1814,10 +1937,17 @@ mps_add_chain(struct mps_command *cm)
 	TAILQ_INSERT_TAIL(&cm->cm_chain_list, chain, chain_link);
 
 	sgc = (MPI2_SGE_CHAIN32 *)&cm->cm_sge->MpiChain;
-	sgc->Length = space;
+	sgc->Length = htole16(space);
 	sgc->NextChainOffset = 0;
+	/*
+	 * TODO Looks like bug in Setting sgc->Flags.
+	 *	sgc->Flags = ( MPI2_SGE_FLAGS_CHAIN_ELEMENT | MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
+	 *	            MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT
+	 *	This is fine.. because we are not using simple element. In case of
+	 *	MPI2_SGE_CHAIN32, we have seperate Length and Flags feild.
+	 */
 	sgc->Flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT;
-	sgc->Address = chain->chain_busaddr;
+	sgc->Address = htole32(chain->chain_busaddr);
 
 	cm->cm_sge = (MPI2_SGE_IO_UNION *)&chain->chain->MpiSimple;
 	cm->cm_sglsize = space;
@@ -1895,7 +2025,7 @@ mps_push_sge(struct mps_command *cm, void *sgep, size_t len, int segsleft)
 		 */
 		if (type == MPI2_SGE_FLAGS_SIMPLE_ELEMENT) {
 			sge->FlagsLength |= htole32(
-				MPI2_SGE_FLAGS_LAST_ELEMENT << MPI2_SGE_FLAGS_SHIFT);
+			    MPI2_SGE_FLAGS_LAST_ELEMENT << MPI2_SGE_FLAGS_SHIFT);
 		}
 
 		/*
@@ -2123,18 +2253,26 @@ mps_map_command(struct mps_softc *sc, struct mps_command *cm)
 int
 mps_wait_command(struct mps_softc *sc, struct mps_command *cm, int timeout)
 {
-	int error;
+	int error, rc;
 
 	KKASSERT(lockstatus(&sc->mps_lock, curthread) != 0);
 
+	if(sc->mps_flags & MPS_FLAGS_DIAGRESET)
+		return  EBUSY;
+
 	cm->cm_complete = NULL;
 	cm->cm_flags |= MPS_CM_FLAGS_WAKEUP;
 	error = mps_map_command(sc, cm);
 	if ((error != 0) && (error != EINPROGRESS))
 		return (error);
 	error = lksleep(cm, &sc->mps_lock, 0, "mpswait", timeout*hz);
-	if (error == EWOULDBLOCK)
+	if (error == EWOULDBLOCK) {
+		mps_dprint(sc, MPS_FAULT, "Calling Reinit from %s\n", __func__);
+		rc = mps_reinit(sc);
+		mps_dprint(sc, MPS_FAULT, "Reinit %s\n",
+				(rc == 0) ? "success" : "failed");
 		error = ETIMEDOUT;
+	}
 	return (error);
 }
 
@@ -2145,7 +2283,7 @@ mps_wait_command(struct mps_softc *sc, struct mps_command *cm, int timeout)
 int
 mps_request_polled(struct mps_softc *sc, struct mps_command *cm)
 {
-	int error, timeout = 0;
+	int error, timeout = 0, rc;
 
 	error = 0;
 
@@ -2155,6 +2293,7 @@ mps_request_polled(struct mps_softc *sc, struct mps_command *cm)
 
 	while ((cm->cm_flags & MPS_CM_FLAGS_COMPLETE) == 0) {
 		mps_intr_locked(sc);
+
 		DELAY(50 * 1000);
 		if (timeout++ > 1000) {
 			mps_dprint(sc, MPS_FAULT, "polling failed\n");
@@ -2163,6 +2302,13 @@ mps_request_polled(struct mps_softc *sc, struct mps_command *cm)
 		}
 	}
 
+	if (error) {
+		mps_dprint(sc, MPS_FAULT, "Calling Reinit from %s\n", __func__);
+		rc = mps_reinit(sc);
+		mps_dprint(sc, MPS_FAULT, "Reinit %s\n",
+				(rc == 0) ? "success" : "failed");
+	}
+
 	return (error);
 }
 
diff --git a/sys/dev/raid/mps/mps_config.c b/sys/dev/raid/mps/mps_config.c
index 10090ab..ba36fd0 100644
--- a/sys/dev/raid/mps/mps_config.c
+++ b/sys/dev/raid/mps/mps_config.c
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2011 LSI Corp.
+ * Copyright (c) 2011, 2012 LSI Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -25,7 +25,7 @@
  *
  * LSI MPT-Fusion Host Adapter FreeBSD
  *
- * $FreeBSD: src/sys/dev/mps/mps_config.c,v 1.1 2012/01/26 18:17:21 ken Exp $
+ * $FreeBSD: src/sys/dev/mps/mps_config.c,v 1.2 2012/06/28 03:48:54 ken Exp $
  */
 
 /* TODO Move headers to mpsvar */
@@ -677,6 +677,7 @@ int mps_config_set_dpm_pg0(struct mps_softc *sc, Mpi2ConfigReply_t *mpi_reply,
 	request->ExtPageType = MPI2_CONFIG_EXTPAGETYPE_DRIVER_MAPPING;
 	request->Header.PageNumber = 0;
 	request->Header.PageVersion = MPI2_DRIVERMAPPING0_PAGEVERSION;
+	/* We can remove below two lines ???? */
 	request->PageAddress = 1 << MPI2_DPM_PGAD_ENTRY_COUNT_SHIFT;
 	request->PageAddress |= htole16(entry_idx);
 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
diff --git a/sys/dev/raid/mps/mps_ioctl.h b/sys/dev/raid/mps/mps_ioctl.h
index e562992..ccd49e7 100644
--- a/sys/dev/raid/mps/mps_ioctl.h
+++ b/sys/dev/raid/mps/mps_ioctl.h
@@ -29,10 +29,10 @@
  *
  * LSI MPT-Fusion Host Adapter FreeBSD userland interface
  *
- * $FreeBSD: src/sys/dev/mps/mps_ioctl.h,v 1.3 2012/01/26 18:17:21 ken Exp $
+ * $FreeBSD: src/sys/dev/mps/mps_ioctl.h,v 1.4 2012/06/28 03:48:54 ken Exp $
  */
 /*-
- * Copyright (c) 2011 LSI Corp.
+ * Copyright (c) 2011, 2012 LSI Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -58,7 +58,7 @@
  *
  * LSI MPT-Fusion Host Adapter FreeBSD
  *
- * $FreeBSD: src/sys/dev/mps/mps_ioctl.h,v 1.3 2012/01/26 18:17:21 ken Exp $
+ * $FreeBSD: src/sys/dev/mps/mps_ioctl.h,v 1.4 2012/06/28 03:48:54 ken Exp $
  */
 
 #ifndef _MPS_IOCTL_H_
diff --git a/sys/dev/raid/mps/mps_mapping.c b/sys/dev/raid/mps/mps_mapping.c
index e0bf234..c913d6c 100644
--- a/sys/dev/raid/mps/mps_mapping.c
+++ b/sys/dev/raid/mps/mps_mapping.c
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2011 LSI Corp.
+ * Copyright (c) 2011, 2012 LSI Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -25,7 +25,7 @@
  *
  * LSI MPT-Fusion Host Adapter FreeBSD
  *
- * $FreeBSD: src/sys/dev/mps/mps_mapping.c,v 1.1 2012/01/26 18:17:21 ken Exp $
+ * $FreeBSD: src/sys/dev/mps/mps_mapping.c,v 1.2 2012/06/28 03:48:54 ken Exp $
  */
 
 /* TODO Move headers to mpsvar */
diff --git a/sys/dev/raid/mps/mps_mapping.h b/sys/dev/raid/mps/mps_mapping.h
index 07a48a4..79b80db 100644
--- a/sys/dev/raid/mps/mps_mapping.h
+++ b/sys/dev/raid/mps/mps_mapping.h
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2011 LSI Corp.
+ * Copyright (c) 2011, 2012 LSI Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -25,7 +25,7 @@
  *
  * LSI MPT-Fusion Host Adapter FreeBSD
  *
- * $FreeBSD: src/sys/dev/mps/mps_mapping.h,v 1.1 2012/01/26 18:17:21 ken Exp $
+ * $FreeBSD: src/sys/dev/mps/mps_mapping.h,v 1.2 2012/06/28 03:48:54 ken Exp $
  */
 
 #ifndef _MPS_MAPPING_H
diff --git a/sys/dev/raid/mps/mps_pci.c b/sys/dev/raid/mps/mps_pci.c
index 58ba019..7473d73 100644
--- a/sys/dev/raid/mps/mps_pci.c
+++ b/sys/dev/raid/mps/mps_pci.c
@@ -23,7 +23,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD: src/sys/dev/mps/mps_pci.c,v 1.4 2012/01/26 18:17:21 ken Exp $
+ * $FreeBSD: src/sys/dev/mps/mps_pci.c,v 1.6 2012/03/07 22:39:11 ken Exp $
  */
 
 /* PCI/PCI-X/PCIe bus interface for the LSI MPT2 controllers */
@@ -87,6 +87,7 @@ static driver_t mps_pci_driver = {
 
 static devclass_t	mps_devclass;
 DRIVER_MODULE(mps, pci, mps_pci_driver, mps_devclass, NULL, NULL);
+MODULE_DEPEND(mps, cam, 1, 1, 1);
 MODULE_VERSION(mps, 1);
 
 struct mps_ident {
diff --git a/sys/dev/raid/mps/mps_sas.c b/sys/dev/raid/mps/mps_sas.c
index 568a135..eb93c33 100644
--- a/sys/dev/raid/mps/mps_sas.c
+++ b/sys/dev/raid/mps/mps_sas.c
@@ -1,30 +1,6 @@
 /*-
  * Copyright (c) 2009 Yahoo! Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-/*-
- * Copyright (c) 2011 LSI Corp.
+ * Copyright (c) 2011, 2012 LSI Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -50,7 +26,7 @@
  *
  * LSI MPT-Fusion Host Adapter FreeBSD
  *
- * $FreeBSD: src/sys/dev/mps/mps_sas.c,v 1.16 2012/01/26 18:17:21 ken Exp $
+ * $FreeBSD: src/sys/dev/mps/mps_sas.c,v 1.23 2012/09/14 21:28:56 eadler Exp $
  */
 
 /* Communications core for LSI MPT2 */
@@ -298,6 +274,7 @@ mpssas_rescan_target(struct mps_softc *sc, struct mpssas_target *targ)
 
 	/* XXX Hardwired to scan the bus for now */
 	ccb->ccb_h.func_code = XPT_SCAN_BUS;
+
 	mps_dprint(sc, MPS_TRACE, "%s targetid %u\n", __func__, targetid);
 	mpssas_rescan(sassc, ccb);
 }
@@ -659,7 +636,7 @@ mpssas_remove_complete(struct mps_softc *sc, struct mps_command *tm)
 static int
 mpssas_register_events(struct mps_softc *sc)
 {
-	uint8_t events[16];
+	u32 events[MPI2_EVENT_NOTIFY_EVENTMASK_WORDS];
 
 	bzero(events, 16);
 	setbit(events, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
diff --git a/sys/dev/raid/mps/mpsvar.h b/sys/dev/raid/mps/mpsvar.h
index d196dc5..7aaceab 100644
--- a/sys/dev/raid/mps/mpsvar.h
+++ b/sys/dev/raid/mps/mpsvar.h
@@ -426,6 +426,7 @@ struct mps_softc {
 #define	MPS_DIAG_RESET_TIMEOUT	300000
 	uint8_t				wait_for_port_enable;
 	uint8_t				port_enable_complete;
+	uint8_t				msleep_fake_chan;
 
 	/* WD controller */
 	uint8_t				WD_valid_config;
@@ -685,11 +686,11 @@ int mps_free(struct mps_softc *sc);
 void mps_intr(void *);
 void mps_intr_msi(void *);
 void mps_intr_locked(void *);
-int mps_register_events(struct mps_softc *, uint8_t *, mps_evt_callback_t *,
+int mps_register_events(struct mps_softc *, u32 *, mps_evt_callback_t *,
     void *, struct mps_event_handle **);
 int mps_restart(struct mps_softc *);
-int mps_update_events(struct mps_softc *, struct mps_event_handle *, uint8_t *);
-int mps_deregister_events(struct mps_softc *, struct mps_event_handle *);
+int mps_update_events(struct mps_softc *, struct mps_event_handle *, u32 *);
+void mps_deregister_events(struct mps_softc *, struct mps_event_handle *);
 int mps_push_sge(struct mps_command *, void *, size_t, int);
 int mps_add_dmaseg(struct mps_command *, vm_paddr_t, size_t, u_int, int);
 int mps_attach_sas(struct mps_softc *sc);
-- 
1.8.4.1

