[Midnightbsd-cvs] src [7986] trunk/sys/dev/isp: maga change for FC-Tape.

laffer1 at midnightbsd.org laffer1 at midnightbsd.org
Thu Sep 15 04:26:26 EDT 2016


Revision: 7986
          http://svnweb.midnightbsd.org/src/?rev=7986
Author:   laffer1
Date:     2016-09-15 04:26:26 -0400 (Thu, 15 Sep 2016)
Log Message:
-----------
maga change for FC-Tape.

Modified Paths:
--------------
    trunk/sys/dev/isp/isp.c
    trunk/sys/dev/isp/isp_freebsd.c
    trunk/sys/dev/isp/isp_freebsd.h
    trunk/sys/dev/isp/isp_library.c
    trunk/sys/dev/isp/isp_library.h
    trunk/sys/dev/isp/isp_pci.c
    trunk/sys/dev/isp/isp_stds.h
    trunk/sys/dev/isp/isp_target.c
    trunk/sys/dev/isp/isp_target.h
    trunk/sys/dev/isp/ispmbox.h
    trunk/sys/dev/isp/ispvar.h

Modified: trunk/sys/dev/isp/isp.c
===================================================================
--- trunk/sys/dev/isp/isp.c	2016-09-15 08:25:48 UTC (rev 7985)
+++ trunk/sys/dev/isp/isp.c	2016-09-15 08:26:26 UTC (rev 7986)
@@ -65,7 +65,7 @@
  */
 #define	MBOX_DELAY_COUNT	1000000 / 100
 #define	ISP_MARK_PORTDB(a, b, c)				\
-    isp_prt(isp, ISP_LOGSANCFG, 				\
+    isp_prt(isp, ISP_LOG_SANCFG, 				\
 	"Chan %d ISP_MARK_PORTDB at LINE %d", b, __LINE__);	\
     isp_mark_portdb(a, b, c)
 
@@ -670,8 +670,7 @@
 			ISP_DELAY(100);
 			if (--loops < 0) {
 				ISP_RESET0(isp);
-				isp_prt(isp, ISP_LOGERR,
-				    "MBOX_BUSY never cleared on reset");
+				isp_prt(isp, ISP_LOGERR, "MBOX_BUSY never cleared on reset");
 				return;
 			}
 		}
@@ -1715,6 +1714,25 @@
 
 		icbp->icb_xfwoptions = fcp->isp_xfwoptions;
 
+		if (ISP_CAP_FCTAPE(isp)) {
+			if (isp->isp_confopts & ISP_CFG_NOFCTAPE)
+				icbp->icb_xfwoptions &= ~ICBXOPT_FCTAPE;
+
+			if (isp->isp_confopts & ISP_CFG_FCTAPE)
+				icbp->icb_xfwoptions |= ICBXOPT_FCTAPE;
+
+			if (icbp->icb_xfwoptions & ICBXOPT_FCTAPE) {
+				icbp->icb_fwoptions &= ~ICBOPT_FULL_LOGIN;	/* per documents */
+				icbp->icb_xfwoptions |= ICBXOPT_FCTAPE_CCQ|ICBXOPT_FCTAPE_CONFIRM;
+				FCPARAM(isp, 0)->fctape_enabled = 1;
+			} else {
+				FCPARAM(isp, 0)->fctape_enabled = 0;
+			}
+		} else {
+			icbp->icb_xfwoptions &= ~ICBXOPT_FCTAPE;
+			FCPARAM(isp, 0)->fctape_enabled = 0;
+		}
+
 		/*
 		 * Prefer or force Point-To-Point instead Loop?
 		 */
@@ -1804,6 +1822,9 @@
 		if (ISP_FW_NEWER_THAN(isp, 3, 16, 0)) {
 			mbs.param[1] |= IFCOPT1_EQFQASYNC|IFCOPT1_CTIO_RETRY;
 			if (fcp->role & ISP_ROLE_TARGET) {
+				if (ISP_FW_NEWER_THAN(isp, 3, 25, 0)) {
+					mbs.param[1] |= IFCOPT1_ENAPURE;
+				}
 				mbs.param[3] = IFCOPT3_NOPRLI;
 			}
 		}
@@ -1813,8 +1834,15 @@
 		}
 	}
 	icbp->icb_logintime = ICB_LOGIN_TOV;
-	icbp->icb_lunetimeout = ICB_LUN_ENABLE_TOV;
 
+#ifdef	ISP_TARGET_MODE
+	if (ISP_FW_NEWER_THAN(isp, 3, 25, 0) && (icbp->icb_fwoptions & ICBOPT_TGT_ENABLE)) {
+		icbp->icb_lunenables = 0xffff;
+		icbp->icb_ccnt = DFLT_CMND_CNT;
+		icbp->icb_icnt = DFLT_INOT_CNT;
+		icbp->icb_lunetimeout = ICB_LUN_ENABLE_TOV;
+	}
+#endif
 	if (fcp->isp_wwnn && fcp->isp_wwpn && (fcp->isp_wwnn >> 60) != 2) {
 		icbp->icb_fwoptions |= ICBOPT_BOTH_WWNS;
 		MAKE_NODE_NAME_FROM_WWN(icbp->icb_nodename, fcp->isp_wwnn);
@@ -1910,7 +1938,7 @@
 		}
 	}
 	if (chan == isp->isp_nchan) {
-		isp_prt(isp, ISP_LOGDEBUG0, "all %d channels with role 'none'", chan);
+		isp_prt(isp, ISP_LOG_WARN1, "all %d channels with role 'none'", chan);
 		isp->isp_state = ISP_INITSTATE;
 		return;
 	}
@@ -1978,6 +2006,19 @@
 		icbp->icb_fwoptions1 |= ICB2400_OPT1_HARD_ADDRESS;
 
 	icbp->icb_fwoptions2 = fcp->isp_xfwoptions;
+	if (isp->isp_confopts & ISP_CFG_NOFCTAPE) {
+		icbp->icb_fwoptions2 &= ~ICB2400_OPT2_FCTAPE;
+	}
+	if (isp->isp_confopts & ISP_CFG_FCTAPE) {
+		icbp->icb_fwoptions2 |= ICB2400_OPT2_FCTAPE;
+	}
+
+	if (icbp->icb_fwoptions2 & ICB2400_OPT2_FCTAPE) {
+		FCPARAM(isp, chan)->fctape_enabled = 1;
+	} else {
+		FCPARAM(isp, chan)->fctape_enabled = 0;
+	}
+
 	switch (isp->isp_confopts & ISP_CFG_PORT_PREF) {
 	case ISP_CFG_NPORT_ONLY:
 		icbp->icb_fwoptions2 &= ~ICB2400_OPT2_TOPO_MASK;
@@ -2336,13 +2377,13 @@
 		msg = buf;
 		break;
 	case PLOGX_IOCBERR_PORTUSED:
-		lev = ISP_LOGSANCFG|ISP_LOGDEBUG0;
+		lev = ISP_LOG_SANCFG|ISP_LOG_WARN1;
 		ISP_SNPRINTF(buf, sizeof (buf), "already logged in with N-Port handle 0x%x", parm1);
 		msg = buf;
 		rval = MBOX_PORT_ID_USED | (parm1 << 16);
 		break;
 	case PLOGX_IOCBERR_HNDLUSED:
-		lev = ISP_LOGSANCFG|ISP_LOGDEBUG0;
+		lev = ISP_LOG_SANCFG|ISP_LOG_WARN1;
 		ISP_SNPRINTF(buf, sizeof (buf), "handle already used for PortID 0x%06x", parm1);
 		msg = buf;
 		rval = MBOX_LOOP_ID_USED;
@@ -2388,15 +2429,11 @@
 
 	switch (mbs.param[0]) {
 	case MBOX_PORT_ID_USED:
-		isp_prt(isp, ISP_LOGDEBUG0,
-		    "isp_port_login: portid 0x%06x already logged in as %u",
-		    portid, mbs.param[1]);
+		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOG_WARN1, "isp_port_login: portid 0x%06x already logged in as %u", portid, mbs.param[1]);
 		return (MBOX_PORT_ID_USED | (mbs.param[1] << 16));
 
 	case MBOX_LOOP_ID_USED:
-		isp_prt(isp, ISP_LOGDEBUG0,
-		    "isp_port_login: handle 0x%04x in use for port id 0x%02xXXXX",
-		    handle, mbs.param[1] & 0xff);
+		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOG_WARN1, "isp_port_login: handle 0x%04x in use for port id 0x%02xXXXX", handle, mbs.param[1] & 0xff);
 		return (MBOX_LOOP_ID_USED);
 
 	case MBOX_COMMAND_COMPLETE:
@@ -2403,20 +2440,15 @@
 		return (0);
 
 	case MBOX_COMMAND_ERROR:
-		isp_prt(isp, ISP_LOGINFO,
-		    "isp_port_login: error 0x%x in PLOGI to port 0x%06x",
-		    mbs.param[1], portid);
+		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOG_WARN1, "isp_port_login: error 0x%x in PLOGI to port 0x%06x", mbs.param[1], portid);
 		return (MBOX_COMMAND_ERROR);
 
 	case MBOX_ALL_IDS_USED:
-		isp_prt(isp, ISP_LOGINFO,
-		    "isp_port_login: all IDs used for fabric login");
+		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOG_WARN1, "isp_port_login: all IDs used for fabric login");
 		return (MBOX_ALL_IDS_USED);
 
 	default:
-		isp_prt(isp, ISP_LOGINFO,
-		    "isp_port_login: error 0x%x on port login of 0x%06x at 0x%0x",
-		    mbs.param[0], portid, handle);
+		isp_prt(isp, ISP_LOG_SANCFG, "isp_port_login: error 0x%x on port login of 0x%06x at 0x%0x", mbs.param[0], portid, handle);
 		return (mbs.param[0]);
 	}
 }
@@ -2483,16 +2515,12 @@
 	if (IS_24XX(isp)) {
 		isp_get_pdb_24xx(isp, fcp->isp_scratch, &un.bill);
 		pdb->handle = un.bill.pdb_handle;
-		pdb->s3_role = un.bill.pdb_prli_svc3;
+		pdb->prli_word3 = un.bill.pdb_prli_svc3;
 		pdb->portid = BITS2WORD_24XX(un.bill.pdb_portid_bits);
 		ISP_MEMCPY(pdb->portname, un.bill.pdb_portname, 8);
 		ISP_MEMCPY(pdb->nodename, un.bill.pdb_nodename, 8);
-		isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
-		    "Chan %d Port 0x%06x flags 0x%x curstate %x",
-		    chan, pdb->portid, un.bill.pdb_flags,
-		    un.bill.pdb_curstate);
-		if (un.bill.pdb_curstate < PDB2400_STATE_PLOGI_DONE ||
-		    un.bill.pdb_curstate > PDB2400_STATE_LOGGED_IN) {
+		isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Port 0x%06x flags 0x%x curstate %x", chan, pdb->portid, un.bill.pdb_flags, un.bill.pdb_curstate);
+		if (un.bill.pdb_curstate < PDB2400_STATE_PLOGI_DONE || un.bill.pdb_curstate > PDB2400_STATE_LOGGED_IN) {
 			mbs.param[0] = MBOX_NOT_LOGGED_IN;
 			if (dolock) {
 				FC_SCRATCH_RELEASE(isp, chan);
@@ -2502,7 +2530,7 @@
 	} else {
 		isp_get_pdb_21xx(isp, fcp->isp_scratch, &un.fred);
 		pdb->handle = un.fred.pdb_loopid;
-		pdb->s3_role = un.fred.pdb_prli_svc3;
+		pdb->prli_word3 = un.fred.pdb_prli_svc3;
 		pdb->portid = BITS2WORD(un.fred.pdb_portid_bits);
 		ISP_MEMCPY(pdb->portname, un.fred.pdb_portname, 8);
 		ISP_MEMCPY(pdb->nodename, un.fred.pdb_nodename, 8);
@@ -2528,7 +2556,7 @@
 		if (isp_getpdb(isp, chan, loopid, &pdb, dolock)) {
 			continue;
 		}
-		isp_prt(isp, ISP_LOGSANCFG|ISP_LOGINFO, "Chan %d Loopid 0x%04x "
+		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGINFO, "Chan %d Loopid 0x%04x "
 		    "PortID 0x%06x WWPN 0x%02x%02x%02x%02x%02x%02x%02x%02x",
 		    chan, loopid, pdb.portid, pdb.portname[0], pdb.portname[1],
 		    pdb.portname[2], pdb.portname[3], pdb.portname[4],
@@ -2606,7 +2634,7 @@
 
 	fcp = FCPARAM(isp, chan);
 
-	isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Chan %d FC Link Test Entry", chan);
+	isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC Link Test Entry", chan);
 	ISP_MARK_PORTDB(isp, chan, 1);
 
 	/*
@@ -2622,7 +2650,7 @@
 		GET_NANOTIME(&hra);
 		isp_fw_state(isp, chan);
 		if (lwfs != fcp->isp_fwstate) {
-			isp_prt(isp, ISP_LOGCONFIG|ISP_LOGSANCFG, "Chan %d Firmware State <%s->%s>", chan, isp_fc_fw_statename((int)lwfs), isp_fc_fw_statename((int)fcp->isp_fwstate));
+			isp_prt(isp, ISP_LOGCONFIG|ISP_LOG_SANCFG, "Chan %d Firmware State <%s->%s>", chan, isp_fc_fw_statename((int)lwfs), isp_fc_fw_statename((int)fcp->isp_fwstate));
 			lwfs = fcp->isp_fwstate;
 		}
 		if (fcp->isp_fwstate == FW_READY) {
@@ -2673,7 +2701,7 @@
 	 * If we haven't gone to 'ready' state, return.
 	 */
 	if (fcp->isp_fwstate != FW_READY) {
-		isp_prt(isp, ISP_LOGSANCFG, "%s: chan %d not at FW_READY state", __func__, chan);
+		isp_prt(isp, ISP_LOG_SANCFG, "%s: chan %d not at FW_READY state", __func__, chan);
 		return (-1);
 	}
 
@@ -2738,7 +2766,9 @@
 				}
 			}
 			if (alpa_map[i] && fcp->isp_loopid != i) {
-				isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Chan %d deriving loopid %d from AL_PA map  (AL_PA 0x%x) and ignoring returned value %d (AL_PA 0x%x)", chan, i, alpa_map[i], fcp->isp_loopid, alpa);
+				isp_prt(isp, ISP_LOG_SANCFG,
+				    "Chan %d deriving loopid %d from AL_PA map (AL_PA 0x%x) and ignoring returned value %d (AL_PA 0x%x)",
+				    chan, i, alpa_map[i], fcp->isp_loopid, alpa);
 				fcp->isp_loopid = i;
 			}
 		}
@@ -2778,11 +2808,11 @@
 		lp->state = FC_PORTDB_STATE_PENDING_VALID;
 		MAKE_WWN_FROM_NODE_NAME(lp->node_wwn, pdb.nodename);
 		MAKE_WWN_FROM_NODE_NAME(lp->port_wwn, pdb.portname);
-		lp->roles = (pdb.s3_role & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
+		lp->prli_word3 = pdb.prli_word3;
 		lp->portid = pdb.portid;
 		lp->handle = pdb.handle;
 		lp->new_portid = lp->portid;
-		lp->new_roles = lp->roles;
+		lp->new_prli_word3 = lp->prli_word3;
 		if (IS_24XX(isp)) {
 			if (check_for_fabric) {
 				/*
@@ -2789,7 +2819,6 @@
 				 * The mbs is still hanging out from the MBOX_GET_LOOP_ID above.
 				 */
 				fcp->isp_fabric_params = mbs.param[7];
-				isp_prt(isp, ISP_LOGCONFIG, "fabric params 0x%x", mbs.param[7]);
 			} else {
 				fcp->isp_fabric_params = 0;
 			}
@@ -2809,7 +2838,7 @@
 			r = isp_register_fc4_type(isp, chan);
 		}
 		if (r) {
-			isp_prt(isp, ISP_LOGWARN|ISP_LOGSANCFG, "%s: register fc4 type failed", __func__);
+			isp_prt(isp, ISP_LOGWARN|ISP_LOG_SANCFG, "%s: register fc4 type failed", __func__);
 			return (-1);
 		}
 	} else {
@@ -2843,8 +2872,8 @@
 	/*
 	 * Announce ourselves, too.
 	 */
-	isp_prt(isp, ISP_LOGSANCFG|ISP_LOGCONFIG, topology, chan, (uint32_t) (fcp->isp_wwpn >> 32), (uint32_t) fcp->isp_wwpn, fcp->isp_portid, fcp->isp_loopid, isp_fc_toponame(fcp));
-	isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Chan %d FC Link Test Complete", chan);
+	isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGCONFIG, topology, chan, (uint32_t) (fcp->isp_wwpn >> 32), (uint32_t) fcp->isp_wwpn, fcp->isp_portid, fcp->isp_loopid, isp_fc_toponame(fcp));
+	isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC Link Test Complete", chan);
 	return (0);
 }
 
@@ -2912,8 +2941,7 @@
 		}
 	}
 
-	isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
-	    "Chan %d Synchronizing PDBs", chan);
+	isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Synchronizing PDBs", chan);
 
 	fcp->isp_loopstate = LOOP_SYNCING_PDB;
 
@@ -2950,7 +2978,7 @@
 			} else {
 				lp->autologin = 0;
 			}
-			lp->new_roles = 0;
+			lp->new_prli_word3 = 0;
 			lp->new_portid = 0;
 			/*
 			 * Note that we might come out of this with our state
@@ -2963,13 +2991,12 @@
 			 * target id in isp_dev_map (if any).
 			 */
 			lp->portid = lp->new_portid;
-			lp->roles = lp->new_roles;
+			lp->prli_word3 = lp->new_prli_word3;
 			lp->state = FC_PORTDB_STATE_VALID;
 			isp_async(isp, ISPASYNC_DEV_ARRIVED, chan, lp);
-			lp->new_roles = 0;
+			lp->new_prli_word3 = 0;
 			lp->new_portid = 0;
-			lp->reserved = 0;
-			lp->new_reserved = 0;
+			lp->announced = 0;
 			break;
 		case FC_PORTDB_STATE_CHANGED:
 /*
@@ -2977,14 +3004,13 @@
  */
 			lp->state = FC_PORTDB_STATE_VALID;
 			isp_async(isp, ISPASYNC_DEV_CHANGED, chan, lp);
-			lp->new_roles = 0;
+			lp->new_prli_word3 = 0;
 			lp->new_portid = 0;
-			lp->reserved = 0;
-			lp->new_reserved = 0;
+			lp->announced = 0;
 			break;
 		case FC_PORTDB_STATE_PENDING_VALID:
 			lp->portid = lp->new_portid;
-			lp->roles = lp->new_roles;
+			lp->prli_word3 = lp->new_prli_word3;
 			if (lp->dev_map_idx) {
 				int t = lp->dev_map_idx - 1;
 				fcp->isp_dev_map[t] = dbidx + 1;
@@ -2992,11 +3018,10 @@
 			lp->state = FC_PORTDB_STATE_VALID;
 			isp_async(isp, ISPASYNC_DEV_STAYED, chan, lp);
 			if (dbidx != FL_ID) {
-				lp->new_roles = 0;
+				lp->new_prli_word3 = 0;
 				lp->new_portid = 0;
 			}
-			lp->reserved = 0;
-			lp->new_reserved = 0;
+			lp->announced = 0;
 			break;
 		case FC_PORTDB_STATE_ZOMBIE:
 			break;
@@ -3054,8 +3079,7 @@
 		break;
 	case TOPO_FL_PORT:
 		if (IS_24XX(isp) && isp->isp_nchan > 1) {
-			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
-			    "Chan %d Skipping Local Loop Scan", chan);
+			isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Skipping Local Loop Scan", chan);
 			fcp->isp_loopstate = LOOP_LSCAN_DONE;
 			return (0);
 		}
@@ -3065,8 +3089,7 @@
 		lim = 2;
 		break;
 	default:
-		isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
-		    "Chan %d no loop topology to scan", chan);
+		isp_prt(isp, ISP_LOG_SANCFG, "Chan %d no loop topology to scan", chan);
 		fcp->isp_loopstate = LOOP_LSCAN_DONE;
 		return (0);
 	}
@@ -3073,8 +3096,7 @@
 
 	fcp->isp_loopstate = LOOP_SCANNING_LOOP;
 
-	isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
-	    "Chan %d FC scan loop 0..%d", chan, lim-1);
+	isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC scan loop 0..%d", chan, lim-1);
 
 
 	/*
@@ -3100,8 +3122,7 @@
 		if (IS_2100(isp) || IS_2200(isp)) {
 			uint64_t node_wwn = isp_get_wwn(isp, chan, handle, 1);
 			if (fcp->isp_loopstate < LOOP_SCANNING_LOOP) {
-				isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
-				    "Chan %d FC scan loop DONE (bad)", chan);
+				isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC scan loop DONE (bad)", chan);
 				return (-1);
 			}
 			if (node_wwn == INI_NONE) {
@@ -3119,8 +3140,7 @@
 			    chan, handle, r);
 			if (fcp->isp_loopstate < LOOP_SCANNING_LOOP) {
 				ISP_MARK_PORTDB(isp, chan, 1);
-				isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
-				    "Chan %d FC scan loop DONE (bad)", chan);
+				isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC scan loop DONE (bad)", chan);
 				return (-1);
 			}
 			continue;
@@ -3128,8 +3148,7 @@
 
 		if (fcp->isp_loopstate < LOOP_SCANNING_LOOP) {
 			ISP_MARK_PORTDB(isp, chan, 1);
-			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
-			    "Chan %d FC scan loop DONE (bad)", chan);
+			isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC scan loop DONE (bad)", chan);
 			return (-1);
 		}
 
@@ -3143,8 +3162,7 @@
 			isp_prt(isp, ISP_LOGWARN,
 			    "Chan %d cannot synchronize port database", chan);
 			ISP_MARK_PORTDB(isp, chan, 1);
-			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
-			    "Chan %d FC scan loop DONE (bad)", chan);
+			isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC scan loop DONE (bad)", chan);
 			return (-1);
 		}
 
@@ -3153,7 +3171,7 @@
 		 */
 		MAKE_WWN_FROM_NODE_NAME(tmp.node_wwn, pdb.nodename);
 		MAKE_WWN_FROM_NODE_NAME(tmp.port_wwn, pdb.portname);
-		tmp.roles = (pdb.s3_role & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
+		tmp.prli_word3 = pdb.prli_word3;
 		tmp.portid = pdb.portid;
 		tmp.handle = pdb.handle;
 
@@ -3192,8 +3210,7 @@
 		for (i = 0; i < MAX_FC_TARG; i++) {
 			lp = &fcp->portdb[i];
 
-			if (lp->state == FC_PORTDB_STATE_NIL ||
-			    lp->target_mode) {
+			if (lp->state == FC_PORTDB_STATE_NIL || lp->target_mode) {
 				continue;
 			}
 			if (lp->node_wwn != tmp.node_wwn) {
@@ -3214,8 +3231,7 @@
 				    chan, i, lp->state);
 				isp_dump_portdb(isp, chan);
 				ISP_MARK_PORTDB(isp, chan, 1);
-				isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
-				    "Chan %d FC scan loop DONE (bad)", chan);
+				isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC scan loop DONE (bad)", chan);
 				return (-1);
 			}
 
@@ -3229,15 +3245,11 @@
 			 * Check to make see if really still the same
 			 * device. If it is, we mark it pending valid.
 			 */
-			if (lp->portid == tmp.portid &&
-			    lp->handle == tmp.handle &&
-			    lp->roles == tmp.roles) {
+			if (lp->portid == tmp.portid && lp->handle == tmp.handle && lp->prli_word3 == tmp.prli_word3) {
 				lp->new_portid = tmp.portid;
-				lp->new_roles = tmp.roles;
+				lp->new_prli_word3 = tmp.prli_word3;
 				lp->state = FC_PORTDB_STATE_PENDING_VALID;
-				isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
-				    "Chan %d Loop Port 0x%06x at 0x%04x Pending "
-				    "Valid", chan, tmp.portid, tmp.handle);
+				isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Loop Port 0x%06x at 0x%04x Pending Valid", chan, tmp.portid, tmp.handle);
 				break;
 			}
 
@@ -3251,12 +3263,10 @@
 			 * Claim that this has changed and let somebody else
 			 * decide what to do.
 			 */
-			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
-			    "Chan %d Loop Port 0x%06x at 0x%04x changed",
-			    chan, tmp.portid, tmp.handle);
+			isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Loop Port 0x%06x at 0x%04x changed", chan, tmp.portid, tmp.handle);
 			lp->state = FC_PORTDB_STATE_CHANGED;
 			lp->new_portid = tmp.portid;
-			lp->new_roles = tmp.roles;
+			lp->new_prli_word3 = tmp.prli_word3;
 			break;
 		}
 
@@ -3290,17 +3300,14 @@
 		lp->autologin = 1;
 		lp->state = FC_PORTDB_STATE_NEW;
 		lp->new_portid = tmp.portid;
-		lp->new_roles = tmp.roles;
+		lp->new_prli_word3 = tmp.prli_word3;
 		lp->handle = tmp.handle;
 		lp->port_wwn = tmp.port_wwn;
 		lp->node_wwn = tmp.node_wwn;
-		isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
-		    "Chan %d Loop Port 0x%06x at 0x%04x is New Entry",
-		    chan, tmp.portid, tmp.handle);
+		isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Loop Port 0x%06x at 0x%04x is New Entry", chan, tmp.portid, tmp.handle);
 	}
 	fcp->isp_loopstate = LOOP_LSCAN_DONE;
-	isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
-	    "Chan %d FC scan loop DONE", chan);
+	isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC scan loop DONE", chan);
 	return (0);
 }
 
@@ -3343,8 +3350,7 @@
 	sns_gid_ft_req_t *rq = &un._x;
 	mbreg_t mbs;
 
-	isp_prt(isp, ISP_LOGDEBUG0,
-	    "Chan %d scanning fabric (GID_FT) via SNS", chan);
+	isp_prt(isp, ISP_LOGDEBUG0, "Chan %d scanning fabric (GID_FT) via SNS", chan);
 
 	ISP_MEMZERO(rq, SNS_GID_FT_REQ_SIZE);
 	rq->snscb_rblen = GIDLEN >> 1;
@@ -3393,8 +3399,7 @@
 	uint32_t *rp;
 	uint8_t *scp = fcp->isp_scratch;
 
-	isp_prt(isp, ISP_LOGDEBUG0,
-	    "Chan %d scanning fabric (GID_FT) via CT", chan);
+	isp_prt(isp, ISP_LOGDEBUG0, "Chan %d scanning fabric (GID_FT) via CT", chan);
 
 	if (!IS_24XX(isp)) {
 		return (1);
@@ -3488,10 +3493,8 @@
 	int portidx, portlim, r;
 	sns_gid_ft_rsp_t *rs0, *rs1;
 
-	isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
-	    "Chan %d FC Scan Fabric", chan);
-	if (fcp->isp_fwstate != FW_READY ||
-	    fcp->isp_loopstate < LOOP_LSCAN_DONE) {
+	isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC Scan Fabric", chan);
+	if (fcp->isp_fwstate != FW_READY || fcp->isp_loopstate < LOOP_LSCAN_DONE) {
 		return (-1);
 	}
 	if (fcp->isp_loopstate > LOOP_SCANNING_FABRIC) {
@@ -3499,8 +3502,7 @@
 	}
 	if (fcp->isp_topo != TOPO_FL_PORT && fcp->isp_topo != TOPO_F_PORT) {
 		fcp->isp_loopstate = LOOP_FSCAN_DONE;
-		isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
-		    "Chan %d FC Scan Fabric Done (no fabric)", chan);
+		isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC Scan Fabric Done (no fabric)", chan);
 		return (0);
 	}
 
@@ -3568,9 +3570,8 @@
 	}
 	if (rs1->snscb_cthdr.ct_cmd_resp != LS_ACC) {
 		int level;
-		if (rs1->snscb_cthdr.ct_reason == 9 &&
-		    rs1->snscb_cthdr.ct_explanation == 7) {
-			level = ISP_LOGSANCFG|ISP_LOGDEBUG0;
+		if (rs1->snscb_cthdr.ct_reason == 9 && rs1->snscb_cthdr.ct_explanation == 7) {
+			level = ISP_LOG_SANCFG;
 		} else {
 			level = ISP_LOGWARN;
 		}
@@ -3614,7 +3615,7 @@
 		    "fabric too big for scratch area: increase ISP_FC_SCRLEN");
 	}
 	portlim = portidx + 1;
-	isp_prt(isp, ISP_LOGSANCFG,
+	isp_prt(isp, ISP_LOG_SANCFG,
 	    "Chan %d got %d ports back from name server", chan, portlim);
 
 	for (portidx = 0; portidx < portlim; portidx++) {
@@ -3639,9 +3640,7 @@
 			rs1->snscb_ports[npidx].portid[0] = 0;
 			rs1->snscb_ports[npidx].portid[1] = 0;
 			rs1->snscb_ports[npidx].portid[2] = 0;
-			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
-			    "Chan %d removing duplicate PortID 0x%06x"
-			    " entry from list", chan, portid);
+			isp_prt(isp, ISP_LOG_SANCFG, "Chan %d removing duplicate PortID 0x%06x entry from list", chan, portid);
 		}
 	}
 
@@ -3671,7 +3670,7 @@
 		    ((rs1->snscb_ports[portidx].portid[2]));
 
 		if (portid == 0) {
-			isp_prt(isp, ISP_LOGSANCFG,
+			isp_prt(isp, ISP_LOG_SANCFG,
 			    "Chan %d skipping null PortID at idx %d",
 			    chan, portidx);
 			continue;
@@ -3687,19 +3686,19 @@
 		 */
 		if (ISP_CAP_MULTI_ID(isp)) {
 			if ((portid >> 8) == (fcp->isp_portid >> 8)) {
-				isp_prt(isp, ISP_LOGSANCFG,
+				isp_prt(isp, ISP_LOG_SANCFG,
 				    "Chan %d skip PortID 0x%06x",
 				    chan, portid);
 				continue;
 			}
 		} else if (portid == fcp->isp_portid) {
-			isp_prt(isp, ISP_LOGSANCFG,
+			isp_prt(isp, ISP_LOG_SANCFG,
 			    "Chan %d skip ourselves on @ PortID 0x%06x",
 			    chan, portid);
 			continue;
 		}
 
-		isp_prt(isp, ISP_LOGSANCFG,
+		isp_prt(isp, ISP_LOG_SANCFG,
 		    "Chan %d Checking Fabric Port 0x%06x", chan, portid);
 
 		/*
@@ -3711,8 +3710,7 @@
 		for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
 			lp = &fcp->portdb[dbidx];
 
-			if (lp->state != FC_PORTDB_STATE_PROBATIONAL ||
-			    lp->target_mode) {
+			if (lp->state != FC_PORTDB_STATE_PROBATIONAL || lp->target_mode) {
 				continue;
 			}
 			if (lp->portid == portid) {
@@ -3754,9 +3752,7 @@
 			if (r != 0) {
 				lp->new_portid = portid;
 				lp->state = FC_PORTDB_STATE_DEAD;
-				isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
-				    "Chan %d Fabric Port 0x%06x is dead",
-				    chan, portid);
+				isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Fabric Port 0x%06x is dead", chan, portid);
 				continue;
 			}
 
@@ -3773,7 +3769,7 @@
 			    pdb.portid != portid ||
 			    wwpn != lp->port_wwn ||
 			    wwnn != lp->node_wwn) {
-				isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
+				isp_prt(isp, ISP_LOG_SANCFG,
 				    fconf, chan, dbidx, pdb.handle, pdb.portid,
 				    (uint32_t) (wwnn >> 32), (uint32_t) wwnn,
 				    (uint32_t) (wwpn >> 32), (uint32_t) wwpn,
@@ -3824,7 +3820,7 @@
 				handle_changed++;
 			}
 
-			nr = (pdb.s3_role & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
+			nr = pdb.prli_word3;
 
 			/*
 			 * Check to see whether the portid and roles have
@@ -3839,17 +3835,12 @@
 			 */
 
 			lp->new_portid = portid;
-			lp->new_roles = nr;
-			if (pdb.portid != lp->portid || nr != lp->roles ||
-			    handle_changed) {
-				isp_prt(isp, ISP_LOGSANCFG,
-				    "Chan %d Fabric Port 0x%06x changed",
-				    chan, portid);
+			lp->new_prli_word3 = nr;
+			if (pdb.portid != lp->portid || nr != lp->prli_word3 || handle_changed) {
+				isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Fabric Port 0x%06x changed", chan, portid);
 				lp->state = FC_PORTDB_STATE_CHANGED;
 			} else {
-				isp_prt(isp, ISP_LOGSANCFG,
-				    "Chan %d Fabric Port 0x%06x "
-				    "Now Pending Valid", chan, portid);
+				isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Fabric Port 0x%06x Now Pending Valid", chan, portid);
 				lp->state = FC_PORTDB_STATE_PENDING_VALID;
 			}
 			continue;
@@ -3935,7 +3926,7 @@
 		handle = pdb.handle;
 		MAKE_WWN_FROM_NODE_NAME(wwnn, pdb.nodename);
 		MAKE_WWN_FROM_NODE_NAME(wwpn, pdb.portname);
-		nr = (pdb.s3_role & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
+		nr = pdb.prli_word3;
 
 		/*
 		 * And go through the database *one* more time to make sure
@@ -3949,8 +3940,7 @@
 			if (fcp->portdb[dbidx].target_mode) {
 				continue;
 			}
-			if (fcp->portdb[dbidx].node_wwn == wwnn &&
-			    fcp->portdb[dbidx].port_wwn == wwpn) {
+			if (fcp->portdb[dbidx].node_wwn == wwnn && fcp->portdb[dbidx].port_wwn == wwpn) {
 				break;
 			}
 		}
@@ -3961,11 +3951,9 @@
 			lp->node_wwn = wwnn;
 			lp->port_wwn = wwpn;
 			lp->new_portid = portid;
-			lp->new_roles = nr;
+			lp->new_prli_word3 = nr;
 			lp->state = FC_PORTDB_STATE_NEW;
-			isp_prt(isp, ISP_LOGSANCFG,
-			    "Chan %d Fabric Port 0x%06x is a New Entry",
-			    chan, portid);
+			isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Fabric Port 0x%06x is a New Entry", chan, portid);
 			continue;
 		}
 
@@ -3991,16 +3979,12 @@
 		lp = &fcp->portdb[dbidx];
 		lp->handle = handle;
 		lp->new_portid = portid;
-		lp->new_roles = nr;
-		if (lp->portid != portid || lp->roles != nr) {
-			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
-			    "Chan %d Zombie Fabric Port 0x%06x Now Changed",
-			    chan, portid);
+		lp->new_prli_word3 = nr;
+		if (lp->portid != portid || lp->prli_word3 != nr) {
+			isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Zombie Fabric Port 0x%06x Now Changed", chan, portid);
 			lp->state = FC_PORTDB_STATE_CHANGED;
 		} else {
-			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
-			    "Chan %d Zombie Fabric Port 0x%06x "
-			    "Now Pending Valid", chan, portid);
+			isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Zombie Fabric Port 0x%06x Now Pending Valid", chan, portid);
 			lp->state = FC_PORTDB_STATE_PENDING_VALID;
 		}
 	}
@@ -4011,8 +3995,7 @@
 		return (-1);
 	}
 	fcp->isp_loopstate = LOOP_FSCAN_DONE;
-	isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
-	    "Chan %d FC Scan Fabric Done", chan);
+	isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC Scan Fabric Done", chan);
 	return (0);
 }
 
@@ -4261,17 +4244,13 @@
 	FC_SCRATCH_RELEASE(isp, chan);
 
 	if (ct->ct_cmd_resp == LS_RJT) {
-		isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
-		    "Chan %d Register FC4 Type rejected", chan);
+		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOG_WARN1, "Chan %d Register FC4 Type rejected", chan);
 		return (-1);
 	} else if (ct->ct_cmd_resp == LS_ACC) {
-		isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
-		    "Chan %d Register FC4 Type accepted", chan);
+		isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Register FC4 Type accepted", chan);
 		return (0);
 	} else {
-		isp_prt(isp, ISP_LOGWARN,
-		    "Chan %d Register FC4 Type: 0x%x",
-		    chan, ct->ct_cmd_resp);
+		isp_prt(isp, ISP_LOGWARN, "Chan %d Register FC4 Type: 0x%x", chan, ct->ct_cmd_resp);
 		return (-1);
 	}
 }
@@ -4369,6 +4348,7 @@
 		fcparam *fcp = FCPARAM(isp, XS_CHANNEL(xs));
 
 		if ((fcp->role & ISP_ROLE_INITIATOR) == 0) {
+			isp_prt(isp, ISP_LOG_WARN1, "%d.%d.%d I am not an initiator", XS_CHANNEL(xs), target, XS_LUN(xs));
 			XS_SETERR(xs, HBA_SELTIMEOUT);
 			return (CMD_COMPLETE);
 		}
@@ -4381,6 +4361,7 @@
 		}
 
 		if (XS_TGT(xs) >= MAX_FC_TARG) {
+			isp_prt(isp, ISP_LOG_WARN1, "%d.%d.%d target too big", XS_CHANNEL(xs), target, XS_LUN(xs));
 			XS_SETERR(xs, HBA_SELTIMEOUT);
 			return (CMD_COMPLETE);
 		}
@@ -4392,9 +4373,11 @@
 			return (CMD_COMPLETE);
 		}
 		if (fcp->portdb[hdlidx].state == FC_PORTDB_STATE_ZOMBIE) {
+			isp_prt(isp, ISP_LOGDEBUG1, "%d.%d.%d target zombie", XS_CHANNEL(xs), target, XS_LUN(xs));
 			return (CMD_RQLATER);
 		}
 		if (fcp->portdb[hdlidx].state != FC_PORTDB_STATE_VALID) {
+			isp_prt(isp, ISP_LOGDEBUG1, "%d.%d.%d bad db port state 0x%x", XS_CHANNEL(xs), target, XS_LUN(xs), fcp->portdb[hdlidx].state);
 			XS_SETERR(xs, HBA_SELTIMEOUT);
 			return (CMD_COMPLETE);
 		}
@@ -4403,6 +4386,7 @@
 	} else {
 		sdparam *sdp = SDPARAM(isp, XS_CHANNEL(xs));
 		if ((sdp->role & ISP_ROLE_INITIATOR) == 0) {
+			isp_prt(isp, ISP_LOGDEBUG1, "%d.%d.%d I am not an initiator", XS_CHANNEL(xs), target, XS_LUN(xs));
 			XS_SETERR(xs, HBA_SELTIMEOUT);
 			return (CMD_COMPLETE);
 		}
@@ -4415,7 +4399,7 @@
 
 	qep = isp_getrqentry(isp);
 	if (qep == NULL) {
-		isp_prt(isp, ISP_LOGDEBUG0, "Request Queue Overflow");
+		isp_prt(isp, ISP_LOG_WARN1, "Request Queue Overflow");
 		XS_SETERR(xs, HBA_BOTCH);
 		return (CMD_EAGAIN);
 	}
@@ -4449,6 +4433,14 @@
 	}
 
 	reqp->req_header.rqs_entry_count = 1;
+
+	/*
+	 * Select and install Header Code.
+	 * Note that it might be overridden before going out
+	 * if we're on a 64 bit platform. The lower level
+	 * code (isp_send_cmd) will select the appropriate
+	 * 64 bit variant if it needs to.
+	 */
 	if (IS_24XX(isp)) {
 		reqp->req_header.rqs_entry_type = RQSTYPE_T7RQS;
 	} else if (IS_FC(isp)) {
@@ -4461,6 +4453,9 @@
 		}
 	}
 
+	/*
+	 * Set task attributes
+	 */
 	if (IS_24XX(isp)) {
 		int ttype;
 		if (XS_TAG_P(xs)) {
@@ -4513,14 +4508,18 @@
 	tptr = &reqp->req_time;
 
 	/*
-	 * NB: we do not support long CDBs
+	 * NB: we do not support long CDBs (yet)
 	 */
 	cdblen = XS_CDBLEN(xs);
 
 	if (IS_SCSI(isp)) {
+		if (cdblen > sizeof (reqp->req_cdb)) {
+			isp_prt(isp, ISP_LOGERR, "Command Length %u too long for this chip", cdblen);
+			XS_SETERR(xs, HBA_BOTCH);
+			return (CMD_COMPLETE);
+		}
 		reqp->req_target = target | (XS_CHANNEL(xs) << 7);
 		reqp->req_lun_trn = XS_LUN(xs);
-		cdblen = ISP_MIN(cdblen, sizeof (reqp->req_cdb));
 		cdbp = reqp->req_cdb;
 		reqp->req_cdblen = cdblen;
 	} else if (IS_24XX(isp)) {
@@ -4527,6 +4526,12 @@
 		ispreqt7_t *t7 = (ispreqt7_t *)local;
 		fcportdb_t *lp;
 
+		if (cdblen > sizeof (t7->req_cdb)) {
+			isp_prt(isp, ISP_LOGERR, "Command Length %u too long for this chip", cdblen);
+			XS_SETERR(xs, HBA_BOTCH);
+			return (CMD_COMPLETE);
+		}
+
 		lp = &FCPARAM(isp, XS_CHANNEL(xs))->portdb[hdlidx];
 		t7->req_nphdl = target;
 		t7->req_tidlo = lp->portid;
@@ -4537,28 +4542,47 @@
 			t7->req_lun[0] |= 0x40;
 		}
 		t7->req_lun[1] = XS_LUN(xs);
-		FCP_NEXT_CRN(isp, xs, t7->req_crn, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
+		if (FCPARAM(isp, XS_CHANNEL(xs))->fctape_enabled && (lp->prli_word3 & PRLI_WD3_RETRY)) {
+			if (FCP_NEXT_CRN(isp, &t7->req_crn, xs)) {
+				isp_prt(isp, ISP_LOG_WARN1, "%d.%d.%d cannot generate next CRN", XS_CHANNEL(xs), target, XS_LUN(xs));
+				XS_SETERR(xs, HBA_BOTCH);
+				return (CMD_EAGAIN);
+			}
+		}
 		tptr = &t7->req_time;
 		cdbp = t7->req_cdb;
-		cdblen = ISP_MIN(cdblen, sizeof (t7->req_cdb));
-	} else if (ISP_CAP_2KLOGIN(isp)) {
-		ispreqt2e_t *t2e = (ispreqt2e_t *)local;
-		t2e->req_target = target;
-		t2e->req_scclun = XS_LUN(xs);
-		cdbp = t2e->req_cdb;
-		cdblen = ISP_MIN(cdblen, sizeof (t2e->req_cdb));
-	} else if (ISP_CAP_SCCFW(isp)) {
-		ispreqt2_t *t2 = (ispreqt2_t *)local;
-		t2->req_target = target;
-		t2->req_scclun = XS_LUN(xs);
-		cdbp = t2->req_cdb;
-		cdblen = ISP_MIN(cdblen, sizeof (t2->req_cdb));
 	} else {
 		ispreqt2_t *t2 = (ispreqt2_t *)local;
-		t2->req_target = target;
-		t2->req_lun_trn = XS_LUN(xs);
-		cdbp = t2->req_cdb;
-		cdblen = ISP_MIN(cdblen, sizeof (t2->req_cdb));
+		fcportdb_t *lp;
+
+		if (cdblen > sizeof t2->req_cdb) {
+			isp_prt(isp, ISP_LOGERR, "Command Length %u too long for this chip", cdblen);
+			XS_SETERR(xs, HBA_BOTCH);
+			return (CMD_COMPLETE);
+		}
+		lp = &FCPARAM(isp, XS_CHANNEL(xs))->portdb[hdlidx];
+		if (FCPARAM(isp, XS_CHANNEL(xs))->fctape_enabled && (lp->prli_word3 & PRLI_WD3_RETRY)) {
+			if (FCP_NEXT_CRN(isp, &t2->req_crn, xs)) {
+				isp_prt(isp, ISP_LOG_WARN1, "%d.%d.%d cannot generate next CRN", XS_CHANNEL(xs), target, XS_LUN(xs));
+				XS_SETERR(xs, HBA_BOTCH);
+				return (CMD_EAGAIN);
+			}
+		}
+		if (ISP_CAP_2KLOGIN(isp)) {
+			ispreqt2e_t *t2e = (ispreqt2e_t *)local;
+			t2e->req_target = target;
+			t2e->req_scclun = XS_LUN(xs);
+			cdbp = t2e->req_cdb;
+		} else if (ISP_CAP_SCCFW(isp)) {
+			ispreqt2_t *t2 = (ispreqt2_t *)local;
+			t2->req_target = target;
+			t2->req_scclun = XS_LUN(xs);
+			cdbp = t2->req_cdb;
+		} else {
+			t2->req_target = target;
+			t2->req_lun_trn = XS_LUN(xs);
+			cdbp = t2->req_cdb;
+		}
 	}
 	ISP_MEMCPY(cdbp, XS_CDBP(xs), cdblen);
 
@@ -4571,7 +4595,7 @@
 	}
 
 	if (isp_allocate_xs(isp, xs, &handle)) {
-		isp_prt(isp, ISP_LOGDEBUG0, "out of xflist pointers");
+		isp_prt(isp, ISP_LOG_WARN1, "out of xflist pointers");
 		XS_SETERR(xs, HBA_BOTCH);
 		return (CMD_EAGAIN);
 	}
@@ -4618,7 +4642,7 @@
 		 * Issue a bus reset.
 		 */
 		if (IS_24XX(isp)) {
-			isp_prt(isp, ISP_LOGWARN, "RESET BUS NOT IMPLEMENTED");
+			isp_prt(isp, ISP_LOGERR, "BUS RESET NOT IMPLEMENTED");
 			break;
 		} else if (IS_FC(isp)) {
 			mbs.param[1] = 10;
@@ -4639,8 +4663,7 @@
 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
 			break;
 		}
-		isp_prt(isp, ISP_LOGINFO,
-		    "driver initiated bus reset of bus %d", chan);
+		isp_prt(isp, ISP_LOGINFO, "driver initiated bus reset of bus %d", chan);
 		return (0);
 
 	case ISPCTL_RESET_DEV:
@@ -4658,17 +4681,12 @@
 
 			hdlidx = fcp->isp_dev_map[tgt] - 1;
 			if (hdlidx < 0 || hdlidx >= MAX_FC_TARG) {
-				isp_prt(isp, ISP_LOGWARN,
-				    "Chan %d bad handle %d trying to reset"
-				    "target %d", chan, hdlidx, tgt);
+				isp_prt(isp, ISP_LOGWARN, "Chan %d bad handle %d trying to reset target %d", chan, hdlidx, tgt);
 				break;
 			}
 			lp = &fcp->portdb[hdlidx];
 			if (lp->state != FC_PORTDB_STATE_VALID) {
-				isp_prt(isp, ISP_LOGWARN,
-				    "Chan %d handle %d for abort of target %d "
-				    "no longer valid", chan,
-				    hdlidx, tgt);
+				isp_prt(isp, ISP_LOGWARN, "Chan %d handle %d for abort of target %d no longer valid", chan, hdlidx, tgt);
 				break;
 			}
 
@@ -4703,18 +4721,14 @@
 				FC_SCRATCH_RELEASE(isp, chan);
 				break;
 			}
-			MEMORYBARRIER(isp, SYNC_SFORCPU, QENTRY_LEN,
-			    QENTRY_LEN, chan);
+			MEMORYBARRIER(isp, SYNC_SFORCPU, QENTRY_LEN, QENTRY_LEN, chan);
 			sp = (isp24xx_statusreq_t *) local;
-			isp_get_24xx_response(isp,
-			    &((isp24xx_statusreq_t *)fcp->isp_scratch)[1], sp);
+			isp_get_24xx_response(isp, &((isp24xx_statusreq_t *)fcp->isp_scratch)[1], sp);
 			FC_SCRATCH_RELEASE(isp, chan);
 			if (sp->req_completion_status == 0) {
 				return (0);
 			}
-			isp_prt(isp, ISP_LOGWARN,
-			    "Chan %d reset of target %d returned 0x%x",
-			    chan, tgt, sp->req_completion_status);
+			isp_prt(isp, ISP_LOGWARN, "Chan %d reset of target %d returned 0x%x", chan, tgt, sp->req_completion_status);
 			break;
 		} else if (IS_FC(isp)) {
 			if (ISP_CAP_2KLOGIN(isp)) {
@@ -4732,8 +4746,7 @@
 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
 			break;
 		}
-		isp_prt(isp, ISP_LOGINFO,
-		    "Target %d on Bus %d Reset Succeeded", tgt, chan);
+		isp_prt(isp, ISP_LOGINFO, "Target %d on Bus %d Reset Succeeded", tgt, chan);
 		ISP_SET_SENDMARKER(isp, chan, 1);
 		return (0);
 
@@ -4747,8 +4760,7 @@
 
 		handle = isp_find_handle(isp, xs);
 		if (handle == 0) {
-			isp_prt(isp, ISP_LOGWARN,
-			    "cannot find handle for command to abort");
+			isp_prt(isp, ISP_LOGWARN, "cannot find handle for command to abort");
 			break;
 		}
 		if (IS_24XX(isp)) {
@@ -4760,21 +4772,15 @@
 			fcp = FCPARAM(isp, chan);
 			hdlidx = fcp->isp_dev_map[tgt] - 1;
 			if (hdlidx < 0 || hdlidx >= MAX_FC_TARG) {
-				isp_prt(isp, ISP_LOGWARN,
-				    "Chan %d bad handle %d trying to abort"
-				    "target %d", chan, hdlidx, tgt);
+				isp_prt(isp, ISP_LOGWARN, "Chan %d bad handle %d trying to abort target %d", chan, hdlidx, tgt);
 				break;
 			}
 			lp = &fcp->portdb[hdlidx];
 			if (lp->state != FC_PORTDB_STATE_VALID) {
-				isp_prt(isp, ISP_LOGWARN,
-				    "Chan %d handle %d for abort of target %d "
-				    "no longer valid", chan, hdlidx, tgt);
+				isp_prt(isp, ISP_LOGWARN, "Chan %d handle %d for abort of target %d no longer valid", chan, hdlidx, tgt);
 				break;
 			}
-			isp_prt(isp, ISP_LOGALL,
-			    "Chan %d Abort Cmd for N-Port 0x%04x @ Port "
-			    "0x%06x %p", chan, lp->handle, lp->portid, xs);
+			isp_prt(isp, ISP_LOGALL, "Chan %d Abort Cmd for N-Port 0x%04x @ Port 0x%06x", chan, lp->handle, lp->portid);
 			ISP_MEMZERO(ab, QENTRY_LEN);
 			ab->abrt_header.rqs_entry_type = RQSTYPE_ABORT_IO;
 			ab->abrt_header.rqs_entry_count = 1;
@@ -4797,8 +4803,7 @@
 				break;
 			}
 			isp_put_24xx_abrt(isp, ab, fcp->isp_scratch);
-			ab2 = (isp24xx_abrt_t *)
-			    &((uint8_t *)fcp->isp_scratch)[QENTRY_LEN];
+			ab2 = (isp24xx_abrt_t *) &((uint8_t *)fcp->isp_scratch)[QENTRY_LEN];
 			ab2->abrt_nphdl = 0xdeaf;
 			MEMORYBARRIER(isp, SYNC_SFORDEV, 0, 2 * QENTRY_LEN, chan);
 			isp_mboxcmd(isp, &mbs);
@@ -4806,16 +4811,13 @@
 				FC_SCRATCH_RELEASE(isp, chan);
 				break;
 			}
-			MEMORYBARRIER(isp, SYNC_SFORCPU, QENTRY_LEN,
-			    QENTRY_LEN, chan);
+			MEMORYBARRIER(isp, SYNC_SFORCPU, QENTRY_LEN, QENTRY_LEN, chan);
 			isp_get_24xx_abrt(isp, ab2, ab);
 			FC_SCRATCH_RELEASE(isp, chan);
 			if (ab->abrt_nphdl == ISP24XX_ABRT_OKAY) {
 				return (0);
 			}
-			isp_prt(isp, ISP_LOGWARN,
-			    "Chan %d handle %d abort returned 0x%x", chan,
-			    hdlidx, ab->abrt_nphdl);
+			isp_prt(isp, ISP_LOGWARN, "Chan %d handle %d abort returned 0x%x", chan, hdlidx, ab->abrt_nphdl);
 			break;
 		} else if (IS_FC(isp)) {
 			if (ISP_CAP_SCCFW(isp)) {
@@ -5003,7 +5005,8 @@
 {
 	XS_T *complist[MAX_REQUESTQ_COMPLETIONS], *xs;
 	uint32_t iptr, optr, junk;
-	int i, nlooked = 0, ndone = 0;
+	int i, nlooked = 0, ndone = 0, continuations_expected = 0;
+	int etype, last_etype = 0;
 
 again:
 	optr = isp->isp_residx;
@@ -5179,10 +5182,10 @@
 		uint8_t qe[QENTRY_LEN];
 		ispstatusreq_t *sp = (ispstatusreq_t *) qe;
 		isphdr_t *hp;
-		int buddaboom, etype, scsi_status, completion_status;
+		int buddaboom, scsi_status, completion_status;
 		int req_status_flags, req_state_flags;
 		uint8_t *snsp, *resp;
-		uint32_t rlen, slen;
+		uint32_t rlen, slen, totslen;
 		long resid;
 		uint16_t oop;
 
@@ -5234,11 +5237,24 @@
 				isp->isp_fpcchiwater = rio->req_header.rqs_seqno;
 			}
 			ISP_MEMZERO(hp, QENTRY_LEN);	/* PERF */
+			last_etype = etype;
 			continue;
 		} else if (etype == RQSTYPE_RIO2) {
-			isp_prt(isp, ISP_LOGERR, "dropping RIO2 response\n");
+			isp_prt(isp, ISP_LOGERR, "dropping RIO2 response");
 			ISP_MEMZERO(hp, QENTRY_LEN);	/* PERF */
+			last_etype = etype;
 			continue;
+		} else if (etype == RQSTYPE_STATUS_CONT) {
+			isp_get_cont_response(isp, (ispstatus_cont_t *) hp, (ispstatus_cont_t *) sp);
+			if (last_etype == RQSTYPE_RESPONSE && continuations_expected && ndone > 0 && (xs = complist[ndone-1]) != NULL) {
+				ispstatus_cont_t *scp = (ispstatus_cont_t *) sp;
+				XS_SENSE_APPEND(xs, scp->req_sense_data, sizeof (scp->req_sense_data));
+				isp_prt(isp, ISP_LOGDEBUG0|ISP_LOG_CWARN, "%d more Status Continuations expected", --continuations_expected);
+			} else {
+				isp_prt(isp, ISP_LOG_WARN1, "Ignored Continuation Response");
+			}
+			ISP_MEMZERO(hp, QENTRY_LEN);	/* PERF */
+			continue;
 		} else {
 			/*
 			 * Somebody reachable via isp_handle_other_response
@@ -5256,12 +5272,12 @@
 			 * optr to be one more than the updated amount.
 			 */
 			while (tsto != oop) {
-				optr = ISP_NXT_QENTRY(tsto,
-				    RESULT_QUEUE_LEN(isp));
+				optr = ISP_NXT_QENTRY(tsto, RESULT_QUEUE_LEN(isp));
 			}
 			if (r > 0) {
 				ISP_WRITE(isp, isp->isp_respoutrp, optr);
 				ISP_MEMZERO(hp, QENTRY_LEN);	/* PERF */
+				last_etype = etype;
 				continue;
 			}
 
@@ -5277,11 +5293,10 @@
 			 * not, something bad has happened.
 			 */
 			if (etype != RQSTYPE_REQUEST) {
-				isp_prt(isp, ISP_LOGERR, notresp,
-				    etype, oop, optr, nlooked);
-				isp_print_bytes(isp,
-				    "Request Queue Entry", QENTRY_LEN, sp);
+				isp_prt(isp, ISP_LOGERR, notresp, etype, oop, optr, nlooked);
+				isp_print_bytes(isp, "Request Queue Entry", QENTRY_LEN, sp);
 				ISP_MEMZERO(hp, QENTRY_LEN);	/* PERF */
+				last_etype = etype;
 				continue;
 			}
 			buddaboom = 1;
@@ -5296,10 +5311,11 @@
 			if (sp->req_header.rqs_flags & RQSFLAG_CONTINUATION) {
 				isp_print_bytes(isp, "unexpected continuation segment", QENTRY_LEN, sp);
 				ISP_WRITE(isp, isp->isp_respoutrp, optr);
+				last_etype = etype;
 				continue;
 			}
 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
-				isp_prt(isp, ISP_LOGDEBUG0, "internal queues full");
+				isp_prt(isp, ISP_LOG_WARN1, "internal queues full");
 				/*
 				 * We'll synthesize a QUEUE FULL message below.
 				 */
@@ -5319,6 +5335,7 @@
 			if (sp->req_header.rqs_flags & RQSFLAG_BADORDER) {
 				isp_print_bytes(isp, "invalid IOCB ordering", QENTRY_LEN, sp);
 				ISP_WRITE(isp, isp->isp_respoutrp, optr);
+				last_etype = etype;
 				continue;
 			}
 		}
@@ -5327,6 +5344,7 @@
 			isp_prt(isp, ISP_LOGERR, "bad request handle 0x%x (iocb type 0x%x)", sp->req_handle, etype);
 			ISP_MEMZERO(hp, QENTRY_LEN);	/* PERF */
 			ISP_WRITE(isp, isp->isp_respoutrp, optr);
+			last_etype = etype;
 			continue;
 		}
 		xs = isp_find_xs(isp, sp->req_handle);
@@ -5343,13 +5361,16 @@
 			}
 			ISP_MEMZERO(hp, QENTRY_LEN);	/* PERF */
 			ISP_WRITE(isp, isp->isp_respoutrp, optr);
+			last_etype = etype;
 			continue;
 		}
 		if (req_status_flags & RQSTF_BUS_RESET) {
+			isp_prt(isp, ISP_LOG_WARN1, "%d.%d.%d bus was reset", XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
 			XS_SETERR(xs, HBA_BUSRESET);
 			ISP_SET_SENDMARKER(isp, XS_CHANNEL(xs), 1);
 		}
 		if (buddaboom) {
+			isp_prt(isp, ISP_LOG_WARN1, "%d.%d.%d buddaboom", XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
 			XS_SETERR(xs, HBA_BOTCH);
 		}
 
@@ -5356,7 +5377,7 @@
 		resp = NULL;
 		rlen = 0;
 		snsp = NULL;
-		slen = 0;
+		totslen = slen = 0;
 		if (IS_24XX(isp) && (scsi_status & (RQCS_RV|RQCS_SV)) != 0) {
 			resp = ((isp24xx_statusreq_t *)sp)->req_rsp_sense;
 			rlen = ((isp24xx_statusreq_t *)sp)->req_response_len;
@@ -5374,14 +5395,23 @@
 			if (IS_24XX(isp)) {
 				snsp = ((isp24xx_statusreq_t *)sp)->req_rsp_sense;
 				snsp += rlen;
-				slen = ((isp24xx_statusreq_t *)sp)->req_sense_len;
+				totslen = ((isp24xx_statusreq_t *)sp)->req_sense_len;
+				slen = (sizeof (((isp24xx_statusreq_t *)sp)->req_rsp_sense)) - rlen;
+				if (totslen < slen)
+					slen = totslen; 
 			} else {
 				snsp = sp->req_sense_data;
-				slen = sp->req_sense_len;
+				totslen = sp->req_sense_len;
+				slen = sizeof (sp->req_sense_data);
+				if (totslen < slen)
+					slen = totslen;
 			}
 		} else if (IS_SCSI(isp) && (req_state_flags & RQSF_GOT_SENSE)) {
 			snsp = sp->req_sense_data;
-			slen = sp->req_sense_len;
+			totslen = sp->req_sense_len;
+			slen = sizeof (sp->req_sense_data);
+			if (totslen < slen)
+				slen = totslen;
 		}
 		if (req_state_flags & RQSF_GOT_STATUS) {
 			*XS_STSP(xs) = scsi_status & 0xff;
@@ -5442,7 +5472,22 @@
 				}
 			}
 			if (snsp && slen) {
-				XS_SAVE_SENSE(xs, snsp, slen);
+				if (totslen > slen) {
+					continuations_expected += ((totslen - slen + QENTRY_LEN - 5) / (QENTRY_LEN - 4));
+					if (ndone > (MAX_REQUESTQ_COMPLETIONS - continuations_expected - 1)) {
+						/* we'll lose some stats, but that's a small price to pay */
+						for (i = 0; i < ndone; i++) {
+							if (complist[i]) {
+								isp->isp_rsltccmplt++;
+								isp_done(complist[i]);
+							}
+						}
+						ndone = 0;
+					}
+					isp_prt(isp, ISP_LOGDEBUG0|ISP_LOG_CWARN, "Expecting %d more Status Continuations for total sense length of %u",
+					    continuations_expected, totslen);
+				}
+				XS_SAVE_SENSE(xs, snsp, totslen, slen);
 			} else if ((req_status_flags & RQSF_GOT_STATUS) && (scsi_status & 0xff) == SCSI_CHECK && IS_FC(isp)) {
 				isp_prt(isp, ISP_LOGWARN, "CHECK CONDITION w/o sense data for CDB=0x%x", XS_CDBP(xs)[0] & 0xff);
 				isp_print_bytes(isp, "CC with no Sense", QENTRY_LEN, qe);
@@ -5461,6 +5506,7 @@
 				*XS_STSP(xs) = SCSI_QFULL;
 				XS_SETERR(xs, HBA_NOERROR);
 			} else if (XS_NOERR(xs)) {
+				isp_prt(isp, ISP_LOG_WARN1, "%d.%d.%d badness at %s:%u", XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), __func__, __LINE__);
 				XS_SETERR(xs, HBA_BOTCH);
 			}
 			XS_SET_RESID(xs, XS_XFRLEN(xs));
@@ -5482,15 +5528,12 @@
 		}
 		isp_destroy_handle(isp, sp->req_handle);
 
-		if (((isp->isp_dblev & (ISP_LOGDEBUG1|ISP_LOGDEBUG2|ISP_LOGDEBUG3))) ||
-		    ((isp->isp_dblev & (ISP_LOGDEBUG0|ISP_LOG_CWARN) && ((!XS_NOERR(xs)) || (*XS_STSP(xs) != SCSI_GOOD))))) {
-			isp_prt_endcmd(isp, xs);
-		}
 		if (isp->isp_nactive > 0) {
 		    isp->isp_nactive--;
 		}
 		complist[ndone++] = xs;	/* defer completion call until later */
 		ISP_MEMZERO(hp, QENTRY_LEN);	/* PERF */
+		last_etype = etype;
 		if (ndone == MAX_REQUESTQ_COMPLETIONS) {
 			break;
 		}
@@ -5525,6 +5568,10 @@
 	for (i = 0; i < ndone; i++) {
 		xs = complist[i];
 		if (xs) {
+			if (((isp->isp_dblev & (ISP_LOGDEBUG1|ISP_LOGDEBUG2|ISP_LOGDEBUG3))) ||
+			    ((isp->isp_dblev & (ISP_LOGDEBUG0|ISP_LOG_CWARN) && ((!XS_NOERR(xs)) || (*XS_STSP(xs) != SCSI_GOOD))))) {
+				isp_prt_endcmd(isp, xs);
+			}
 			isp->isp_rsltccmplt++;
 			isp_done(xs);
 		}
@@ -5547,8 +5594,8 @@
 		ISP_SNPRINTF(cdbstr, sizeof (cdbstr), "%s0x%02x ", cdbstr, XS_CDBP(xs)[i]);
 	}
 	if (XS_SENSE_VALID(xs)) {
-		isp_xs_prt(isp, xs, ISP_LOGALL, "FIN dl%d resid %ld CDB=%s KEY/ASC/ASCQ=0x%02x/0x%02x/0x%02x",
-		    XS_XFRLEN(xs), (long) XS_GET_RESID(xs), cdbstr, XS_SNSKEY(xs), XS_SNSASC(xs), XS_SNSASCQ(xs));
+		isp_xs_prt(isp, xs, ISP_LOGALL, "FIN dl%d resid %ld CDB=%s SenseLength=%u/%u KEY/ASC/ASCQ=0x%02x/0x%02x/0x%02x",
+		    XS_XFRLEN(xs), (long) XS_GET_RESID(xs), cdbstr, XS_CUR_SNSLEN(xs), XS_TOT_SNSLEN(xs), XS_SNSKEY(xs), XS_SNSASC(xs), XS_SNSASCQ(xs));
 	} else {
 		isp_xs_prt(isp, xs, ISP_LOGALL, "FIN dl%d resid %ld CDB=%s STS 0x%x XS_ERR=0x%x", XS_XFRLEN(xs), (long) XS_GET_RESID(xs), cdbstr, *XS_STSP(xs), XS_ERR(xs));
 	}
@@ -5876,6 +5923,7 @@
 						continue;
 					}
 					j++;
+					isp_prt(isp, ISP_LOG_WARN1, "%d.%d.%d bus reset set at %s:%u", XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), __func__, __LINE__);
 					XS_SETERR(xs, HBA_BUSRESET);
 				}
 				if (j) {
@@ -6102,10 +6150,10 @@
 {
 	switch (type) {
 	case RQSTYPE_STATUS_CONT:
-		isp_prt(isp, ISP_LOGDEBUG0, "Ignored Continuation Response");
+		isp_prt(isp, ISP_LOG_WARN1, "Ignored Continuation Response");
 		return (1);
 	case RQSTYPE_MARKER:
-		isp_prt(isp, ISP_LOGDEBUG0, "Marker Response");
+		isp_prt(isp, ISP_LOG_WARN1, "Marker Response");
 		return (1);
 	case RQSTYPE_ATIO:
 	case RQSTYPE_CTIO:
@@ -6170,7 +6218,7 @@
 
 	case RQCS_INCOMPLETE:
 		if ((sp->req_state_flags & RQSF_GOT_TARGET) == 0) {
-			isp_xs_prt(isp, xs, ISP_LOGDEBUG1, "Selection Timeout");
+			isp_xs_prt(isp, xs, ISP_LOG_WARN1, "Selection Timeout @ %s:%d", __func__, __LINE__);
 			if (XS_NOERR(xs)) {
 				XS_SETERR(xs, HBA_SELTIMEOUT);
 				*rp = XS_XFRLEN(xs);
@@ -6366,7 +6414,7 @@
 		break;
 
 	case RQCS_QUEUE_FULL:
-		isp_xs_prt(isp, xs, ISP_LOGDEBUG0, "internal queues full status 0x%x", *XS_STSP(xs));
+		isp_xs_prt(isp, xs, ISP_LOG_WARN1, "internal queues full status 0x%x", *XS_STSP(xs));
 
 		/*
 		 * If QFULL or some other status byte is set, then this
@@ -6374,17 +6422,14 @@
 		 *
 		 * Unfortunately, some QLogic f/w writers have, in
 		 * some cases, ommitted to *set* status to QFULL.
-		 *
-
+		 */
+#if	0
 		if (*XS_STSP(xs) != SCSI_GOOD && XS_NOERR(xs)) {
 			XS_SETERR(xs, HBA_NOERROR);
 			return;
 		}
 
-		 *
-		 *
-		 */
-
+#endif
 		*XS_STSP(xs) = SCSI_QFULL;
 		XS_SETERR(xs, HBA_NOERROR);
 		return;
@@ -6446,8 +6491,7 @@
 			reason = "logout";
 		}
 
-		isp_prt(isp, ISP_LOGINFO, "port %s for target %d",
-		    reason, XS_TGT(xs));
+		isp_prt(isp, ISP_LOGINFO, "port %s for target %d", reason, XS_TGT(xs));
 
 		/*
 		 * If we're on a local loop, force a LIP (which is overkill)
@@ -6469,8 +6513,7 @@
 		return;
 	}
 	case RQCS_PORT_CHANGED:
-		isp_prt(isp, ISP_LOGWARN,
-		    "port changed for target %d", XS_TGT(xs));
+		isp_prt(isp, ISP_LOGWARN, "port changed for target %d", XS_TGT(xs));
 		if (XS_NOERR(xs)) {
 			XS_SETERR(xs, HBA_SELTIMEOUT);
 		}
@@ -6477,8 +6520,7 @@
 		return;
 
 	case RQCS_PORT_BUSY:
-		isp_prt(isp, ISP_LOGWARN,
-		    "port busy for target %d", XS_TGT(xs));
+		isp_prt(isp, ISP_LOGWARN, "port busy for target %d", XS_TGT(xs));
 		if (XS_NOERR(xs)) {
 			XS_SETERR(xs, HBA_TGTBSY);
 		}
@@ -6485,8 +6527,7 @@
 		return;
 
 	default:
-		isp_prt(isp, ISP_LOGERR, "Unknown Completion Status 0x%x",
-		    sp->req_completion_status);
+		isp_prt(isp, ISP_LOGERR, "Unknown Completion Status 0x%x", sp->req_completion_status);
 		break;
 	}
 	if (XS_NOERR(xs)) {
@@ -6547,9 +6588,7 @@
 		return;
 
 	case RQCS_24XX_DRE:	/* data reassembly error */
-		isp_prt(isp, ISP_LOGERR,
-		    "Chan %d data reassembly error for target %d",
-		    chan, XS_TGT(xs));
+		isp_prt(isp, ISP_LOGERR, "Chan %d data reassembly error for target %d", chan, XS_TGT(xs));
 		if (XS_NOERR(xs)) {
 			XS_SETERR(xs, HBA_ABORTED);
 		}
@@ -6557,8 +6596,7 @@
 		return;
 
 	case RQCS_24XX_TABORT:	/* aborted by target */
-		isp_prt(isp, ISP_LOGERR, "Chan %d target %d sent ABTS",
-		    chan, XS_TGT(xs));
+		isp_prt(isp, ISP_LOGERR, "Chan %d target %d sent ABTS", chan, XS_TGT(xs));
 		if (XS_NOERR(xs)) {
 			XS_SETERR(xs, HBA_ABORTED);
 		}
@@ -6580,7 +6618,7 @@
 			return;
 		}
 		XS_SET_RESID(xs, sp->req_resid);
-		isp_xs_prt(isp, xs, ISP_LOGDEBUG0, "Data Underrun (%d) for command 0x%x", sp->req_resid, XS_CDBP(xs)[0] & 0xff);
+		isp_xs_prt(isp, xs, ISP_LOG_WARN1, "Data Underrun (%d) for command 0x%x", sp->req_resid, XS_CDBP(xs)[0] & 0xff);
 		if (XS_NOERR(xs)) {
 			XS_SETERR(xs, HBA_NOERROR);
 		}
@@ -6616,8 +6654,7 @@
 		return;
 	}
 	case RQCS_PORT_CHANGED:
-		isp_prt(isp, ISP_LOGWARN,
-		    "port changed for target %d chan %d", XS_TGT(xs), chan);
+		isp_prt(isp, ISP_LOGWARN, "port changed for target %d chan %d", XS_TGT(xs), chan);
 		if (XS_NOERR(xs)) {
 			XS_SETERR(xs, HBA_SELTIMEOUT);
 		}
@@ -6625,9 +6662,7 @@
 
 
 	case RQCS_24XX_ENOMEM:	/* f/w resource unavailable */
-		isp_prt(isp, ISP_LOGWARN,
-		    "f/w resource unavailable for target %d chan %d",
-		    XS_TGT(xs), chan);
+		isp_prt(isp, ISP_LOGWARN, "f/w resource unavailable for target %d chan %d", XS_TGT(xs), chan);
 		if (XS_NOERR(xs)) {
 			*XS_STSP(xs) = SCSI_BUSY;
 			XS_SETERR(xs, HBA_TGTBSY);
@@ -6635,9 +6670,7 @@
 		return;
 
 	case RQCS_24XX_TMO:	/* task management overrun */
-		isp_prt(isp, ISP_LOGWARN,
-		    "command for target %d overlapped task management for "
-		    "chan %d", XS_TGT(xs), chan);
+		isp_prt(isp, ISP_LOGWARN, "command for target %d overlapped task management for chan %d", XS_TGT(xs), chan);
 		if (XS_NOERR(xs)) {
 			*XS_STSP(xs) = SCSI_BUSY;
 			XS_SETERR(xs, HBA_TGTBSY);
@@ -6645,9 +6678,7 @@
 		return;
 
 	default:
-		isp_prt(isp, ISP_LOGERR,
-		    "Unknown Completion Status 0x%x on chan %d",
-		    sp->req_completion_status, chan);
+		isp_prt(isp, ISP_LOGERR, "Unknown Completion Status 0x%x on chan %d", sp->req_completion_status, chan);
 		break;
 	}
 	if (XS_NOERR(xs)) {
@@ -6991,7 +7022,7 @@
 	ISP_FC_OPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
 	ISP_FC_OPMAP_FULL(0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff),	/* 0x06: MBOX_MAILBOX_REG_TEST */
 	ISP_FC_OPMAP(0x07, 0x07),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
-	ISP_FC_OPMAP_FULL(0x0, 0x0, 0x0, 0x01, 0x0, 0x3, 0x80, 0x4f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
+	ISP_FC_OPMAP_FULL(0x0, 0x0, 0x0, 0x01, 0x0, 0x3, 0x80, 0x7f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
 	ISP_FC_OPMAP(0xdf, 0x01),	/* 0x09: MBOX_LOAD_RISC_RAM_2100 */
 	ISP_FC_OPMAP(0xdf, 0x01),	/* 0x0a: DUMP RAM */
 	ISP_FC_OPMAP_HALF(0x1, 0xff, 0x0, 0x01),	/* 0x0b: MBOX_LOAD_RISC_RAM */

Modified: trunk/sys/dev/isp/isp_freebsd.c
===================================================================
--- trunk/sys/dev/isp/isp_freebsd.c	2016-09-15 08:25:48 UTC (rev 7985)
+++ trunk/sys/dev/isp/isp_freebsd.c	2016-09-15 08:26:26 UTC (rev 7986)
@@ -55,9 +55,6 @@
 int isp_quickboot_time = 7;	/* don't wait more than N secs for loop up */
 int isp_gone_device_time = 30;	/* grace time before reporting device lost */
 int isp_autoconfig = 1;		/* automatically attach/detach devices */
-static const char *roles[4] = {
-    "(none)", "Target", "Initiator", "Target/Initiator"
-};
 static const char prom3[] = "Chan %d PortID 0x%06x Departed from Target %u because of %s";
 static const char rqo[] = "%s: Request Queue Overflow\n";
 
@@ -120,7 +117,10 @@
 	csa.event_enable = AC_LOST_DEVICE;
 	csa.callback = isp_cam_async;
 	csa.callback_arg = sim;
+
+	ISP_LOCK(isp);
 	xpt_action((union ccb *)&csa);
+	ISP_UNLOCK(isp);
 
 	if (IS_SCSI(isp)) {
 		struct isp_spi *spi = ISP_SPI_PC(isp, chan);
@@ -154,15 +154,14 @@
 		if (fcp->role & ISP_ROLE_INITIATOR) {
 			isp_freeze_loopdown(isp, chan, "isp_attach");
 			callout_reset(&fc->ldt, isp_quickboot_time * hz, isp_ldt, fc);
-			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Starting Initial Loop Down Timer @ %lu", (unsigned long) time_uptime);
+			isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Starting Initial Loop Down Timer @ %lu", (unsigned long) time_uptime);
 		}
 		ISP_UNLOCK(isp);
 		if (THREAD_CREATE(isp_kthread, fc, &fc->kproc, 0, 0, "%s: fc_thrd%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
 			xpt_free_path(fc->path);
 			ISP_LOCK(isp);
-			if (callout_active(&fc->ldt)) {
+			if (callout_active(&fc->ldt))
 				callout_stop(&fc->ldt);
-			}
 			xpt_bus_deregister(cam_sim_path(fc->sim));
 			ISP_UNLOCK(isp);
 			cam_sim_free(fc->sim, FALSE);
@@ -182,6 +181,9 @@
 			SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "wwpn", CTLFLAG_RD, &FCPARAM(isp, 0)->isp_wwpn, "World Wide Port Name");
 			SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "loop_down_limit", CTLFLAG_RW, &ISP_FC_PC(isp, 0)->loop_down_limit, 0, "Loop Down Limit");
 			SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "gone_device_time", CTLFLAG_RW, &ISP_FC_PC(isp, 0)->gone_device_time, 0, "Gone Device Time");
+#if defined(ISP_TARGET_MODE) && defined(DEBUG)
+			SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "inject_lost_data_frame", CTLFLAG_RW, &ISP_FC_PC(isp, 0)->inject_lost_data_frame, 0, "Cause a Lost Frame on a Read");
+#endif
 		}
 	}
 	return (0);
@@ -301,7 +303,9 @@
 		csa.event_enable = 0;
 		csa.callback = isp_cam_async;
 		csa.callback_arg = sim;
+		ISP_LOCK(isp);
 		xpt_action((union ccb *)&csa);
+		ISP_UNLOCK(isp);
 		xpt_free_path(path);
 		xpt_bus_deregister(cam_sim_path(sim));
 		cam_sim_free(sim, FALSE);
@@ -346,7 +350,7 @@
 		int wasfrozen = fc->simqfrozen & SIMQFRZ_LOOPDOWN;
 		fc->simqfrozen &= ~SIMQFRZ_LOOPDOWN;
 		if (wasfrozen && fc->simqfrozen == 0) {
-			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d releasing simq", __func__, chan);
+			isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d releasing simq", __func__, chan);
 			xpt_release_simq(fc->sim, 1);
 		}
 	}
@@ -360,7 +364,7 @@
 	int nr, chan, retval = ENOTTY;
 
 	isp = dev->si_drv1;
-	
+
 	switch (c) {
 	case ISP_SDBLEV:
 	{
@@ -479,7 +483,7 @@
 		}
 		lp = &FCPARAM(isp, ifc->chan)->portdb[ifc->loopid];
 		if (lp->state == FC_PORTDB_STATE_VALID || lp->target_mode) {
-			ifc->role = lp->roles;
+			ifc->role = (lp->prli_word3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
 			ifc->loopid = lp->handle;
 			ifc->portid = lp->portid;
 			ifc->node_wwn = lp->node_wwn;
@@ -772,14 +776,16 @@
 static ISP_INLINE void
 isp_free_pcmd(ispsoftc_t *isp, union ccb *ccb)
 {
-	((struct isp_pcmd *)ISP_PCMD(ccb))->next = isp->isp_osinfo.pcmd_free;
-	isp->isp_osinfo.pcmd_free = ISP_PCMD(ccb);
-	ISP_PCMD(ccb) = NULL;
+	if (ISP_PCMD(ccb)) {
+		((struct isp_pcmd *)ISP_PCMD(ccb))->next = isp->isp_osinfo.pcmd_free;
+		isp->isp_osinfo.pcmd_free = ISP_PCMD(ccb);
+		ISP_PCMD(ccb) = NULL;
+	}
 }
+
 /*
  * Put the target mode functions here, because some are inlines
  */
-
 #ifdef	ISP_TARGET_MODE
 static ISP_INLINE void isp_tmlock(ispsoftc_t *, const char *);
 static ISP_INLINE void isp_tmunlk(ispsoftc_t *);
@@ -804,9 +810,11 @@
 static int isp_disable_target_mode(ispsoftc_t *, int);
 static void isp_ledone(ispsoftc_t *, lun_entry_t *);
 static timeout_t isp_refire_putback_atio;
+static timeout_t isp_refire_notify_ack;
 static void isp_complete_ctio(union ccb *);
 static void isp_target_putback_atio(union ccb *);
-static void isp_target_start_ctio(ispsoftc_t *, union ccb *);
+enum Start_Ctio_How { FROM_CAM, FROM_SRR, FROM_CTIO_DONE };
+static void isp_target_start_ctio(ispsoftc_t *, union ccb *, enum Start_Ctio_How);
 static void isp_handle_platform_atio(ispsoftc_t *, at_entry_t *);
 static void isp_handle_platform_atio2(ispsoftc_t *, at2_entry_t *);
 static void isp_handle_platform_atio7(ispsoftc_t *, at7_entry_t *);
@@ -1026,8 +1034,10 @@
 static ISP_INLINE void
 isp_put_atpd(ispsoftc_t *isp, tstate_t *tptr, atio_private_data_t *atp)
 {
-	atp->tag = 0;
-	atp->dead = 0;
+	if (atp->ests) {
+		isp_put_ecmd(isp, atp->ests);
+	}
+	memset(atp, 0, sizeof (*atp));
 	atp->next = tptr->atfree;
 	tptr->atfree = atp;
 }
@@ -1248,7 +1258,7 @@
 		ISP_SET_PC(isp, bus, tm_enable_defer, 1);
 		ccb->ccb_h.status = CAM_REQ_CMP;
 		xpt_print(ccb->ccb_h.path, "Target Mode not enabled yet- lun enable deferred\n");
-		goto done;
+		goto done1;
 	}
 
 	/*
@@ -1265,6 +1275,7 @@
 	} else {
 		tptr->enabled = 1;
 	}
+done1:
 	if (tptr) {
 		rls_lun_statep(isp, tptr);
 	}
@@ -1283,6 +1294,7 @@
 	struct tslist *lhp;
 	int i, n;
 
+
 	ISP_GET_PC(isp, bus, tm_enabled, i);
 	if (i == 1) {
 		return (CAM_REQ_CMP);
@@ -1303,7 +1315,7 @@
 		SLIST_FOREACH(tptr, lhp, next) {
 			tptr->hold++;
 			if (tptr->enabled == 0) {
-				if (isp_enable_deferred(isp, bus, xpt_path_lun_id(tptr->owner)) == 0) {
+				if (isp_enable_deferred(isp, bus, xpt_path_lun_id(tptr->owner)) == CAM_REQ_CMP) {
 					tptr->enabled = 1;
 					n++;
 				}
@@ -1325,9 +1337,10 @@
 isp_enable_deferred(ispsoftc_t *isp, int bus, lun_id_t lun)
 {
 	cam_status status;
-	int luns_already_enabled = ISP_FC_PC(isp, bus)->tm_luns_enabled;
+	int luns_already_enabled;
 
-	isp_prt(isp, ISP_LOGTINFO, "%s: bus %d lun %u", __func__, bus, lun);
+	ISP_GET_PC(isp, bus, tm_luns_enabled, luns_already_enabled);
+	isp_prt(isp, ISP_LOGTINFO, "%s: bus %d lun %u luns_enabled %d", __func__, bus, lun, luns_already_enabled);
 	if (IS_24XX(isp) || (IS_FC(isp) && luns_already_enabled)) {
 		status = CAM_REQ_CMP;
 	} else {
@@ -1342,7 +1355,7 @@
 		}
 		status = CAM_REQ_INPROG;
 		isp->isp_osinfo.rptr = &status;
-		if (isp_lun_cmd(isp, RQSTYPE_ENABLE_LUN, bus, lun, DFLT_CMND_CNT, DFLT_INOT_CNT)) {
+		if (isp_lun_cmd(isp, RQSTYPE_ENABLE_LUN, bus, lun == CAM_LUN_WILDCARD? 0 : lun, cmd_cnt, not_cnt)) {
 			status = CAM_RESRC_UNAVAIL;
 		} else {
 			mtx_sleep(&status, &isp->isp_lock, PRIBIO, "isp_enable_deferred", 0);
@@ -1412,8 +1425,9 @@
 	/*
 	 * For SCC FW, we only deal with lun zero.
 	 */
-	if (IS_FC(isp)) {
-		lun = 0;
+	if (IS_FC(isp) && lun > 0) {
+		status = CAM_REQ_CMP;
+		goto done;
 	}
 	isp->isp_osinfo.rptr = &status;
 	if (isp_lun_cmd(isp, RQSTYPE_ENABLE_LUN, bus, lun, 0, 0)) {
@@ -1426,7 +1440,8 @@
 	if (status == CAM_REQ_CMP) {
 		tptr->enabled = 0;
 		/*
-		 * If we have no more luns enabled for this bus, delete all tracked wwns for it (if we are FC)
+		 * If we have no more luns enabled for this bus,
+		 * delete all tracked wwns for it (if we are FC), 
 		 * and disable target mode.
 		 */
 		if (is_any_lun_enabled(isp, bus) == 0) {
@@ -1492,7 +1507,7 @@
 		}
 	}
 	ISP_SET_PC(isp, bus, tm_enabled, 0);
-	isp_prt(isp, ISP_LOGINFO, "Target Role disabled onon  Bus %d", bus);
+	isp_prt(isp, ISP_LOGINFO, "Target Role disabled on Bus %d", bus);
 	return (0);
 }
 
@@ -1517,21 +1532,25 @@
 }
 
 static void
-isp_target_start_ctio(ispsoftc_t *isp, union ccb *ccb)
+isp_target_start_ctio(ispsoftc_t *isp, union ccb *ccb, enum Start_Ctio_How how)
 {
+
 	void *qe;
+	int fctape, sendstatus, resid, repval = ISP_LOGTDEBUG0;
 	tstate_t *tptr;
+	fcparam *fcp;
 	atio_private_data_t *atp;
 	struct ccb_scsiio *cso = &ccb->csio;
-	uint32_t dmaresult, handle;
+	uint32_t dmaresult, handle, xfrlen, sense_length;
 	uint8_t local[QENTRY_LEN];
 
 	/*
 	 * Do some sanity checks.
 	 */
-	if (cso->dxfer_len == 0) {
+	xfrlen = cso->dxfer_len;
+	if (xfrlen == 0) {
 		if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
-			xpt_print(ccb->ccb_h.path, "a data transfer length of zero but no status to send is wrong\n");
+			ISP_PATH_PRT(isp, ISP_LOGERR, ccb->ccb_h.path, "a data transfer length of zero but no status to send is wrong\n");
 			ccb->ccb_h.status = CAM_REQ_INVALID;
 			xpt_done(ccb);
 			return;
@@ -1542,8 +1561,7 @@
 	if (tptr == NULL) {
 		tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
 		if (tptr == NULL) {
-			xpt_print(ccb->ccb_h.path, "%s: [0x%x] cannot find tstate pointer in %s\n", __func__, cso->tag_id);
-			dump_tstates(isp, XS_CHANNEL(ccb));
+			ISP_PATH_PRT(isp, ISP_LOGERR, ccb->ccb_h.path, "%s: [0x%x] cannot find tstate pointer in %s\n", __func__, cso->tag_id);
 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
 			xpt_done(ccb);
 			return;
@@ -1552,14 +1570,18 @@
 
 	atp = isp_get_atpd(isp, tptr, cso->tag_id);
 	if (atp == NULL) {
-		xpt_print(ccb->ccb_h.path, "%s: [0x%x] cannot find private data adjunct\n", __func__, cso->tag_id);
+		ISP_PATH_PRT(isp, ISP_LOGERR, ccb->ccb_h.path, "%s: [0x%x] cannot find private data adjunct\n", __func__, cso->tag_id);
 		isp_dump_atpd(isp, tptr);
 		ccb->ccb_h.status = CAM_REQ_CMP_ERR;
 		xpt_done(ccb);
 		return;
 	}
+
+	/*
+	 * Is this command a dead duck?
+	 */
 	if (atp->dead) {
-		xpt_print(ccb->ccb_h.path, "%s: [0x%x] stopping sending a CTIO for a dead command\n", __func__, cso->tag_id);
+		ISP_PATH_PRT(isp, ISP_LOGERR, ccb->ccb_h.path, "%s: [0x%x] not sending a CTIO for a dead command\n", __func__, cso->tag_id);
 		ccb->ccb_h.status = CAM_REQ_ABORTED;
 		xpt_done(ccb);
 		return;
@@ -1568,8 +1590,9 @@
 	/*
 	 * Check to make sure we're still in target mode.
 	 */
-	if ((FCPARAM(isp, XS_CHANNEL(ccb))->role & ISP_ROLE_TARGET) == 0) {
-		xpt_print(ccb->ccb_h.path, "%s: [0x%x] stopping sending a CTIO because we're no longer in target mode\n", __func__, cso->tag_id);
+	fcp = FCPARAM(isp, XS_CHANNEL(ccb));
+	if ((fcp->role & ISP_ROLE_TARGET) == 0) {
+		ISP_PATH_PRT(isp, ISP_LOGERR, ccb->ccb_h.path, "%s: [0x%x] stopping sending a CTIO because we're no longer in target mode\n", __func__, cso->tag_id);
 		ccb->ccb_h.status = CAM_PROVIDE_FAIL;
 		xpt_done(ccb);
 		return;
@@ -1576,30 +1599,74 @@
 	}
 
 	/*
+	 * We're only handling one outstanding CTIO at a time (which
+	 * could be split into two to split data and status)
+	 */
+	if (atp->ctcnt) {
+		ISP_PATH_PRT(isp, ISP_LOGINFO, ccb->ccb_h.path, "sending only one CTIO at a time\n");
+		goto restart_delay;
+	}
+
+
+	/*
 	 * Get some resources
 	 */
 	if (isp_get_pcmd(isp, ccb)) {
-		rls_lun_statep(isp, tptr);
-		xpt_print(ccb->ccb_h.path, "out of PCMDs\n");
-		cam_freeze_devq(ccb->ccb_h.path);
-		cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
-		ccb->ccb_h.status = CAM_REQUEUE_REQ;
-		xpt_done(ccb);
-		return;
+		ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "out of PCMDs\n");
+		goto restart_delay;
 	}
 	qe = isp_getrqentry(isp);
 	if (qe == NULL) {
-		xpt_print(ccb->ccb_h.path, rqo, __func__);
-		cam_freeze_devq(ccb->ccb_h.path);
-		cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
-		ccb->ccb_h.status = CAM_REQUEUE_REQ;
-		goto out;
+		ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, rqo, __func__);
+		goto restart_delay;
 	}
 	memset(local, 0, QENTRY_LEN);
 
 	/*
-	 * We're either moving data or completing a command here.
+	 * Does the initiator expect FC-Tape style responses?
+	 * Can we provide them?
 	 */
+	if ((atp->word3 & PRLI_WD3_RETRY) && fcp->fctape_enabled) {
+		fctape = 1;
+	} else {
+		fctape = 0;
+	}
+
+	/*
+	 * If we already did the data xfer portion of a CTIO that sends data
+	 * and status, don't do it again and do the status portion now.
+	 */
+	if (atp->sendst) {
+		xfrlen = 0;	/* we already did the data transfer */
+		atp->sendst = 0;
+	}
+	if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
+		sendstatus = 1;
+	} else {
+		sendstatus = 0;
+	}
+
+	if (ccb->ccb_h.flags & CAM_SEND_SENSE) {
+		/*
+		 * Sense length is not the entire sense data structure size. Periph
+		 * drivers don't seem to be setting sense_len to reflect the actual
+		 * size. We'll peek inside to get the right amount.
+		 */
+		sense_length = cso->sense_len;
+
+		/*
+		 * This 'cannot' happen
+		 */
+		if (sense_length > (XCMD_SIZE - MIN_FCP_RESPONSE_SIZE)) {
+			sense_length = XCMD_SIZE - MIN_FCP_RESPONSE_SIZE;
+		}
+	} else {
+		sense_length = 0;
+	}
+
+	if (how == FROM_SRR || atp->nsrr)
+		repval = ISP_LOGINFO;
+
 	if (IS_24XX(isp)) {
 		ct7_entry_t *cto = (ct7_entry_t *) local;
 
@@ -1612,21 +1679,103 @@
 		cto->ct_iid_hi = atp->portid >> 16;
 		cto->ct_oxid = atp->oxid;
 		cto->ct_vpidx = ISP_GET_VPIDX(isp, XS_CHANNEL(ccb));
-		cto->ct_scsi_status = cso->scsi_status;
 		cto->ct_timeout = 120;
 		cto->ct_flags = atp->tattr << CT7_TASK_ATTR_SHIFT;
-		if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
-			cto->ct_flags |= CT7_SENDSTATUS;
+
+		/*
+		 * Mode 1, status, no data. Only possible when we are sending status, have
+		 * no data to transfer, and any sense length can fit in the ct7_entry.
+		 *
+		 * Mode 2, status, no data. We have to use this in the case sense data
+		 * won't fit into a ct7_entry_t.
+		 *
+		 */
+		if (sendstatus && xfrlen == 0) {
+			cto->ct_flags |= CT7_SENDSTATUS | CT7_NO_DATA;
+			resid = atp->orig_datalen - atp->bytes_xfered;
+			if (sense_length <= MAXRESPLEN_24XX) {
+				if (resid < 0) {
+					cto->ct_resid = -resid;
+				} else if (resid > 0) {
+					cto->ct_resid = resid;
+				}
+				cto->ct_flags |= CT7_FLAG_MODE1;
+				cto->ct_scsi_status = cso->scsi_status;
+				if (resid < 0) {
+					cto->ct_scsi_status |= (FCP_RESID_OVERFLOW << 8);
+				} else if (resid > 0) {
+					cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8);
+				}
+				if (fctape) {
+					cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
+				}
+				if (sense_length) {
+					cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8);
+					cto->rsp.m1.ct_resplen = cto->ct_senselen = sense_length;
+					memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
+				}
+			} else {
+				bus_addr_t addr;
+				char buf[XCMD_SIZE];
+				fcp_rsp_iu_t *rp;
+
+				if (atp->ests == NULL) {
+					atp->ests = isp_get_ecmd(isp);
+					if (atp->ests == NULL) {
+						goto restart_delay;
+					}
+				}
+				memset(buf, 0, sizeof (buf));
+				rp = (fcp_rsp_iu_t *)buf;
+				if (fctape) {
+					cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
+					rp->fcp_rsp_bits |= FCP_CONF_REQ;
+				}
+				cto->ct_flags |= CT7_FLAG_MODE2;
+        			rp->fcp_rsp_scsi_status = cso->scsi_status;
+				if (resid < 0) {
+					rp->fcp_rsp_resid = -resid;
+					rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW;
+				} else if (resid > 0) {
+					rp->fcp_rsp_resid = resid;
+					rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
+				}
+				if (sense_length) {
+        				rp->fcp_rsp_snslen = sense_length;
+					cto->ct_senselen = sense_length;
+					rp->fcp_rsp_bits |= FCP_SNSLEN_VALID;
+					isp_put_fcp_rsp_iu(isp, rp, atp->ests);
+					memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
+				} else {
+					isp_put_fcp_rsp_iu(isp, rp, atp->ests);
+				}
+				if (isp->isp_dblev & ISP_LOGTDEBUG1) {
+					isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
+				}
+				addr = isp->isp_osinfo.ecmd_dma;
+				addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
+				isp_prt(isp, repval, "%s: ests base %p vaddr %p ecmd_dma %jx addr %jx len %u", __func__, isp->isp_osinfo.ecmd_base, atp->ests,
+				    (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
+				cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
+				cto->rsp.m2.ct_fcp_rsp_iudata.ds_base = DMA_LO32(addr);
+				cto->rsp.m2.ct_fcp_rsp_iudata.ds_basehi = DMA_HI32(addr);
+				cto->rsp.m2.ct_fcp_rsp_iudata.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
+			}
+			if (sense_length) {
+				isp_prt(isp, repval, "%s: CTIO7[0x%x] CDB0=%x sstatus=0x%x flags=0x%x resid=%d slen %u sense: %x %x/%x/%x", __func__,
+				    cto->ct_rxid, atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid, sense_length, cso->sense_data.error_code,
+				    cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
+			} else {
+				isp_prt(isp, repval, "%s: CTIO7[0x%x] CDB0=%x sstatus=0x%x flags=0x%x resid=%d", __func__,
+				    cto->ct_rxid, atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid);
+			}
+			atp->state = ATPD_STATE_LAST_CTIO;
 		}
-		if (cso->dxfer_len == 0) {
-			cto->ct_flags |= CT7_FLAG_MODE1 | CT7_NO_DATA;
-			if ((ccb->ccb_h.flags & CAM_SEND_SENSE) != 0) {
-				int m = min(cso->sense_len, sizeof (struct scsi_sense_data));
-				cto->rsp.m1.ct_resplen = cto->ct_senselen = min(m, MAXRESPLEN_24XX);
-				memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, cto->ct_senselen);
-				cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8);
-			}
-		} else {
+
+		/*
+		 * Mode 0 data transfers, *possibly* with status.
+		 */
+		if (xfrlen != 0) {
 			cto->ct_flags |= CT7_FLAG_MODE0;
 			if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
 				cto->ct_flags |= CT7_DATA_IN;
@@ -1633,37 +1782,52 @@
 			} else {
 				cto->ct_flags |= CT7_DATA_OUT;
 			}
-			cto->rsp.m0.reloff = atp->bytes_xfered;
+
 			/*
-			 * Don't overrun the limits placed on us
+			 * Don't overrun the limits placed on us, but record it as
+			 * if we had so that we can set an overflow bit later.
 			 */
-			if (atp->bytes_xfered + cso->dxfer_len > atp->orig_datalen) {
-				cso->dxfer_len = atp->orig_datalen - atp->bytes_xfered;
+			atp->last_xframt = xfrlen;
+			if (atp->bytes_xfered >= atp->orig_datalen) {
+				resid = atp->orig_datalen - (atp->bytes_xfered + xfrlen);
+			} else if (atp->bytes_xfered + xfrlen > atp->orig_datalen) {
+				resid = atp->orig_datalen - (atp->bytes_xfered + xfrlen);
+				xfrlen = atp->orig_datalen - atp->bytes_xfered;
+			} else {
+				resid = atp->orig_datalen - xfrlen;
 			}
-			atp->last_xframt = cso->dxfer_len;
-			cto->rsp.m0.ct_xfrlen = cso->dxfer_len;
-		}
-		if (cto->ct_flags & CT7_SENDSTATUS) {
-			int lvl = (cso->scsi_status)? ISP_LOGTINFO : ISP_LOGTDEBUG0;
-			cto->ct_resid = atp->orig_datalen - (atp->bytes_xfered + cso->dxfer_len);
-			if (cto->ct_resid < 0) {
-				cto->ct_scsi_status |= (FCP_RESID_OVERFLOW << 8);
-			} else if (cto->ct_resid > 0) {
-				cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8);
+			cto->rsp.m0.reloff = atp->bytes_xfered;
+			cto->rsp.m0.ct_xfrlen = xfrlen;
+
+#ifdef	DEBUG
+			if (ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame && xfrlen > ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame) {
+				isp_prt(isp, ISP_LOGWARN, "%s: truncating data frame with xfrlen %d to %d", __func__, xfrlen, xfrlen - (xfrlen >> 2));
+				ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame = 0;
+				cto->rsp.m0.ct_xfrlen -= xfrlen >> 2;
 			}
-			atp->state = ATPD_STATE_LAST_CTIO;
-			ISP_PATH_PRT(isp, lvl, cso->ccb_h.path, "%s: CTIO7[%x] CDB0=%x scsi status %x flags %x resid %d xfrlen %u offset %u\n", __func__, cto->ct_rxid,
-			    atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid, cso->dxfer_len, atp->bytes_xfered);
-		} else {
-			cto->ct_resid = 0;
-			ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, cso->ccb_h.path, "%s: CTIO7[%x] flags %x xfrlen %u offset %u\n", __func__, cto->ct_rxid, cto->ct_flags,
-			    cso->dxfer_len, atp->bytes_xfered);
-			atp->state = ATPD_STATE_CTIO;
+#endif
+			if (sendstatus) {
+				if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 && fctape == 0) {
+					cto->ct_flags |= CT7_SENDSTATUS;
+					atp->state = ATPD_STATE_LAST_CTIO;
+				} else {
+					atp->sendst = 1;	/* send status later */
+					cto->ct_header.rqs_seqno = 0;
+					atp->state = ATPD_STATE_CTIO;
+				}
+			} else {
+				atp->state = ATPD_STATE_CTIO;
+			}
+			isp_prt(isp, repval, "%s: CTIO7[0x%x] CDB0=%x sstatus=0x%x flags=0x%x xfrlen=%u off=%u", __func__,
+			    cto->ct_rxid, atp->cdb0, cto->ct_scsi_status, cto->ct_flags, xfrlen, atp->bytes_xfered);
 		}
 	} else if (IS_FC(isp)) {
 		ct2_entry_t *cto = (ct2_entry_t *) local;
 
-		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
+		if (isp->isp_osinfo.sixtyfourbit)
+			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO3;
+		else
+			cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
 		cto->ct_header.rqs_entry_count = 1;
 		cto->ct_header.rqs_seqno = 1;
 		if (ISP_CAP_2KLOGIN(isp) == 0) {
@@ -1674,30 +1838,108 @@
 				cto->ct_lun = ccb->ccb_h.target_lun;
 			}
 		}
+		cto->ct_timeout = 10;
+		cto->ct_rxid = cso->tag_id;
 
+		/*
+		 * Mode 1, status, no data. Only possible when we are sending status, have
+		 * no data to transfer, and the sense length can fit in the ct7_entry.
+		 *
+		 * Mode 2, status, no data. We have to use this in the case the the response
+		 * length won't fit into a ct2_entry_t.
+		 *
+		 * We'll fill out this structure with information as if this were a
+		 * Mode 1. The hardware layer will create the Mode 2 FCP RSP IU as
+		 * needed based upon this.
+		 */
+		if (sendstatus && xfrlen == 0) {
+			cto->ct_flags |= CT2_SENDSTATUS | CT2_NO_DATA;
+			resid = atp->orig_datalen - atp->bytes_xfered;
+			if (sense_length <= MAXRESPLEN) {
+				if (resid < 0) {
+					cto->ct_resid = -resid;
+				} else if (resid > 0) {
+					cto->ct_resid = resid;
+				}
+				cto->ct_flags |= CT2_FLAG_MODE1;
+				cto->rsp.m1.ct_scsi_status = cso->scsi_status;
+				if (resid < 0) {
+					cto->rsp.m1.ct_scsi_status |= CT2_DATA_OVER;
+				} else if (resid > 0) {
+					cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER;
+				}
+				if (fctape) {
+					cto->ct_flags |= CT2_CONFIRM;
+				}
+				if (sense_length) {
+					cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
+					cto->rsp.m1.ct_resplen = cto->rsp.m1.ct_senselen = sense_length;
+					memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
+				}
+			} else {
+				bus_addr_t addr;
+				char buf[XCMD_SIZE];
+				fcp_rsp_iu_t *rp;
 
-		cto->ct_rxid = cso->tag_id;
-		if (cso->dxfer_len == 0) {
-			cto->ct_flags |= CT2_FLAG_MODE1 | CT2_NO_DATA | CT2_SENDSTATUS;
-			cto->rsp.m1.ct_scsi_status = cso->scsi_status;
-			cto->ct_resid = atp->orig_datalen - atp->bytes_xfered;
-			if (cto->ct_resid < 0) {
-				cto->rsp.m1.ct_scsi_status |= CT2_DATA_OVER;
-			} else if (cto->ct_resid > 0) {
-				cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER;
+				if (atp->ests == NULL) {
+					atp->ests = isp_get_ecmd(isp);
+					if (atp->ests == NULL) {
+						goto restart_delay;
+					}
+				}
+				memset(buf, 0, sizeof (buf));
+				rp = (fcp_rsp_iu_t *)buf;
+				if (fctape) {
+					cto->ct_flags |= CT2_CONFIRM;
+					rp->fcp_rsp_bits |= FCP_CONF_REQ;
+				}
+				cto->ct_flags |= CT2_FLAG_MODE2;
+        			rp->fcp_rsp_scsi_status = cso->scsi_status;
+				if (resid < 0) {
+					rp->fcp_rsp_resid = -resid;
+					rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW;
+				} else if (resid > 0) {
+					rp->fcp_rsp_resid = resid;
+					rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
+				}
+				if (sense_length) {
+        				rp->fcp_rsp_snslen = sense_length;
+					rp->fcp_rsp_bits |= FCP_SNSLEN_VALID;
+					isp_put_fcp_rsp_iu(isp, rp, atp->ests);
+					memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
+				} else {
+					isp_put_fcp_rsp_iu(isp, rp, atp->ests);
+				}
+				if (isp->isp_dblev & ISP_LOGTDEBUG1) {
+					isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
+				}
+				addr = isp->isp_osinfo.ecmd_dma;
+				addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
+				isp_prt(isp, repval, "%s: ests base %p vaddr %p ecmd_dma %jx addr %jx len %u", __func__, isp->isp_osinfo.ecmd_base, atp->ests,
+				    (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
+				cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
+				if (isp->isp_osinfo.sixtyfourbit) {
+					cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_base = DMA_LO32(addr);
+					cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_basehi = DMA_HI32(addr);
+					cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
+				} else {
+					cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_base = DMA_LO32(addr);
+					cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
+				}
 			}
-			if ((ccb->ccb_h.flags & CAM_SEND_SENSE) != 0) {
-				int m = min(cso->sense_len, MAXRESPLEN);
-				memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, m);
-				cto->rsp.m1.ct_senselen = m;
-				cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
-			} else if (cso->scsi_status == SCSI_STATUS_CHECK_COND) {
-				/*
-				 * XXX: DEBUG
-				 */
-				xpt_print(ccb->ccb_h.path, "CHECK CONDITION being sent without associated SENSE DATA for CDB=0x%x\n", atp->cdb0);
+			if (sense_length) {
+				isp_prt(isp, repval, "%s: CTIO2[0x%x] CDB0=%x sstatus=0x%x flags=0x%x resid=%d sense: %x %x/%x/%x", __func__,
+				    cto->ct_rxid, atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid, cso->sense_data.error_code,
+				    cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
+			} else {
+				isp_prt(isp, repval, "%s: CTIO2[0x%x] CDB0=%x sstatus=0x%x flags=0x%x resid=%d", __func__,
+				    cto->ct_rxid, atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid);
 			}
-		} else {
+			atp->state = ATPD_STATE_LAST_CTIO;
+		}
+
+		if (xfrlen != 0) {
+			int resid = 0;
 			cto->ct_flags |= CT2_FLAG_MODE0;
 			if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
 				cto->ct_flags |= CT2_DATA_IN;
@@ -1704,46 +1946,34 @@
 			} else {
 				cto->ct_flags |= CT2_DATA_OUT;
 			}
-			cto->ct_reloff = atp->bytes_xfered;
-			cto->rsp.m0.ct_xfrlen = cso->dxfer_len;
+
 			/*
-			 * Don't overrun the limits placed on us
+			 * Don't overrun the limits placed on us, but record it as
+			 * if we had so that we can set an overflow bit later.
 			 */
-			if (atp->bytes_xfered + cso->dxfer_len > atp->orig_datalen) {
-				cso->dxfer_len = atp->orig_datalen - atp->bytes_xfered;
+			atp->last_xframt = xfrlen;
+			if (atp->bytes_xfered + xfrlen > atp->orig_datalen) {
+				resid = 1;
+				xfrlen = atp->orig_datalen - atp->bytes_xfered;
 			}
-			if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
-				cto->ct_flags |= CT2_SENDSTATUS;
-				cto->rsp.m0.ct_scsi_status = cso->scsi_status;
-				cto->ct_resid = atp->orig_datalen - (atp->bytes_xfered + cso->dxfer_len);
-				if (cto->ct_resid < 0) {
-					cto->rsp.m0.ct_scsi_status |= CT2_DATA_OVER;
-				} else if (cto->ct_resid > 0) {
-					cto->rsp.m0.ct_scsi_status |= CT2_DATA_UNDER;
+			cto->ct_reloff = atp->bytes_xfered;
+			cto->rsp.m0.ct_xfrlen = xfrlen;
+
+			if (sendstatus) {
+				if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 && fctape == 0) {
+					cto->ct_flags |= CT2_SENDSTATUS;
+					atp->state = ATPD_STATE_LAST_CTIO;
+				} else {
+					atp->sendst = 1;	/* send status later */
+					cto->ct_header.rqs_seqno = 0;
+					atp->state = ATPD_STATE_CTIO;
 				}
 			} else {
-				atp->last_xframt = cso->dxfer_len;
+				atp->state = ATPD_STATE_CTIO;
 			}
-			/*
-			 * If we're sending data and status back together,
-			 * we can't also send back sense data as well.
-			 */
-			ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
 		}
-
-		if (cto->ct_flags & CT2_SENDSTATUS) {
-			int lvl = (cso->scsi_status)? ISP_LOGTINFO : ISP_LOGTDEBUG0;
-			cto->ct_flags |= CT2_CCINCR;
-			atp->state = ATPD_STATE_LAST_CTIO;
-			ISP_PATH_PRT(isp, lvl, cso->ccb_h.path, "%s: CTIO2[%x] CDB0=%x scsi status %x flags %x resid %d xfrlen %u offset %u\n", __func__, cto->ct_rxid,
-			    atp->cdb0, cto->rsp.m0.ct_scsi_status, cto->ct_flags, cto->ct_resid, cso->dxfer_len, atp->bytes_xfered);
-		} else {
-			cto->ct_resid = 0;
-			atp->state = ATPD_STATE_CTIO;
-			ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "%s: CTIO2[%x] flags %x xfrlen %u offset %u\n", __func__, cto->ct_rxid, cto->ct_flags,
-			    cso->dxfer_len, atp->bytes_xfered);
-		}
-		cto->ct_timeout = 10;
+		isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO2[%x] CDB0=%x scsi status %x flags %x resid %d xfrlen %u offset %u", __func__, cto->ct_rxid,
+		    atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid, cso->dxfer_len, atp->bytes_xfered);
 	} else {
 		ct_entry_t *cto = (ct_entry_t *) local;
 
@@ -1754,9 +1984,9 @@
 		cto->ct_iid |= XS_CHANNEL(ccb) << 7;
 		cto->ct_tgt = ccb->ccb_h.target_id;
 		cto->ct_lun = ccb->ccb_h.target_lun;
-		cto->ct_fwhandle = cso->tag_id >> 16;
-		if (AT_HAS_TAG(cso->tag_id)) {
-			cto->ct_tag_val = cso->tag_id;
+		cto->ct_fwhandle = cso->tag_id;
+		if (atp->rxid) {
+			cto->ct_tag_val = atp->rxid;
 			cto->ct_flags |= CT_TQAE;
 		}
 		if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) {
@@ -1773,7 +2003,7 @@
 			cto->ct_flags |= CT_SENDSTATUS|CT_CCINCR;
 			cto->ct_scsi_status = cso->scsi_status;
 			cto->ct_resid = cso->resid;
-			ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "%s: CTIO[%x] scsi status %x resid %d tag_id %x\n", __func__,
+			isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO[%x] scsi status %x resid %d tag_id %x", __func__,
 			    cto->ct_fwhandle, cso->scsi_status, cso->resid, cso->tag_id);
 		}
 		ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
@@ -1781,12 +2011,10 @@
 	}
 
 	if (isp_allocate_xs_tgt(isp, ccb, &handle)) {
-		xpt_print(ccb->ccb_h.path, "No XFLIST pointers for %s\n", __func__);
-		ccb->ccb_h.status = CAM_REQUEUE_REQ;
-		goto out;
+		ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "No XFLIST pointers for %s\n", __func__);
+		goto restart_delay;
 	}
 
-
 	/*
 	 * Call the dma setup routines for this entry (and any subsequent
 	 * CTIOs) if there's data to move, and then tell the f/w it's got
@@ -1810,16 +2038,25 @@
 	dmaresult = ISP_DMASETUP(isp, cso, (ispreq_t *) local);
 	if (dmaresult == CMD_QUEUED) {
 		isp->isp_nactive++;
-		ccb->ccb_h.status |= CAM_SIM_QUEUED;
+		if (xfrlen) {
+			ccb->ccb_h.spriv_field0 = atp->bytes_xfered;
+		} else {
+			ccb->ccb_h.spriv_field0 = ~0;
+		}
+		ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
+		atp->ctcnt++;
 		rls_lun_statep(isp, tptr);
 		return;
 	}
-	if (dmaresult == CMD_EAGAIN) {
-		ccb->ccb_h.status = CAM_REQUEUE_REQ;
-	} else {
+	isp_destroy_tgt_handle(isp, handle);
+	if (dmaresult != CMD_EAGAIN) {
 		ccb->ccb_h.status = CAM_REQ_CMP_ERR;
+		goto out;
 	}
-	isp_destroy_tgt_handle(isp, handle);
+restart_delay:
+	cam_freeze_devq(ccb->ccb_h.path);
+	cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
+	ccb->ccb_h.status = CAM_REQUEUE_REQ;
 out:
 	rls_lun_statep(isp, tptr);
 	isp_free_pcmd(isp, ccb);
@@ -1837,6 +2074,21 @@
 }
 
 static void
+isp_refire_notify_ack(void *arg)
+{
+	isp_tna_t *tp  = arg;
+	ispsoftc_t *isp = tp->isp;
+	ISP_LOCK(isp);
+	if (isp_notify_ack(isp, tp->not)) {
+		(void) timeout(isp_refire_notify_ack, tp, 5);
+	} else {
+		free(tp, M_DEVBUF);
+	}
+	ISP_UNLOCK(isp);
+}
+
+
+static void
 isp_target_putback_atio(union ccb *ccb)
 {
 	ispsoftc_t *isp;
@@ -1889,12 +2141,10 @@
 static void
 isp_complete_ctio(union ccb *ccb)
 {
-	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG) {
-		ccb->ccb_h.status |= CAM_REQ_CMP;
+	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
+		ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
+		xpt_done(ccb);
 	}
-	ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
-	isp_free_pcmd(XS_ISP(ccb), ccb);
-	xpt_done(ccb);
 }
 
 /*
@@ -1975,14 +2225,12 @@
 		rls_lun_statep(isp, tptr);
 		return;
 	}
-	atp->tag = aep->at_tag_val;
-	if (atp->tag == 0) {
-		atp->tag = ~0;
-	}
+	atp->tag = aep->at_handle;
+	atp->rxid = aep->at_tag_val;
 	atp->state = ATPD_STATE_ATIO;
 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
 	tptr->atio_count--;
-	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
+	ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
 	atiop->ccb_h.target_id = aep->at_tgt;
 	atiop->ccb_h.target_lun = aep->at_lun;
 	if (aep->at_flags & AT_NODISC) {
@@ -2022,7 +2270,7 @@
 	atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
 	atp->tattr = aep->at_tag_type;
 	atp->state = ATPD_STATE_CAM;
-	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "ATIO[%x] CDB=0x%x lun %d\n", aep->at_tag_val, atp->cdb0, atp->lun);
+	isp_prt(isp, ISP_LOGTDEBUG0, "ATIO[0x%x] CDB=0x%x lun %d", aep->at_tag_val, atp->cdb0, atp->lun);
 	rls_lun_statep(isp, tptr);
 }
 
@@ -2063,8 +2311,12 @@
 	if (tptr == NULL) {
 		tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
 		if (tptr == NULL) {
-			isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] no state pointer for lun %d", aep->at_rxid, lun);
-			isp_endcmd(isp, aep, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
+			isp_prt(isp, ISP_LOGWARN, "%s: [0x%x] no state pointer for lun %d or wildcard", __func__, aep->at_rxid, lun);
+			if (lun == 0) {
+				isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
+			} else {
+				isp_endcmd(isp, aep, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
+			}
 			return;
 		}
 	}
@@ -2112,7 +2364,7 @@
 	atp->state = ATPD_STATE_ATIO;
 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
 	tptr->atio_count--;
-	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
+	isp_prt(isp, ISP_LOGTDEBUG2, "Take FREE ATIO count now %d", tptr->atio_count);
 	atiop->ccb_h.target_id = FCPARAM(isp, 0)->isp_loopid;
 	atiop->ccb_h.target_lun = lun;
 
@@ -2146,7 +2398,7 @@
 		if (isp_find_pdb_by_wwn(isp, 0, iid, &lp)) {
 			isp_del_wwn_entry(isp, 0, iid, lp->handle, lp->portid);
 		}
-		isp_add_wwn_entry(isp, 0, iid, atiop->init_id, PORT_ANY);
+		isp_add_wwn_entry(isp, 0, iid, atiop->init_id, PORT_ANY, 0);
 	}
 	atiop->cdb_len = ATIO2_CDBLEN;
 	ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, ATIO2_CDBLEN);
@@ -2183,7 +2435,7 @@
 	atp->tattr = aep->at_taskflags & ATIO2_TC_ATTR_MASK;
 	atp->state = ATPD_STATE_CAM;
 	xpt_done((union ccb *)atiop);
-	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "ATIO2[%x] CDB=0x%x lun %d datalen %u\n", aep->at_rxid, atp->cdb0, lun, atp->orig_datalen);
+	isp_prt(isp, ISP_LOGTDEBUG0, "ATIO2[0x%x] CDB=0x%x lun %d datalen %u", aep->at_rxid, atp->cdb0, lun, atp->orig_datalen);
 	rls_lun_statep(isp, tptr);
 	return;
 noresrc:
@@ -2281,8 +2533,12 @@
 	if (tptr == NULL) {
 		tptr = get_lun_statep(isp, chan, CAM_LUN_WILDCARD);
 		if (tptr == NULL) {
-			isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] no state pointer for lun %d or wildcard", aep->at_rxid, lun);
-			isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
+			isp_prt(isp, ISP_LOGWARN, "%s: [0x%x] no state pointer for lun %d or wildcard", __func__, aep->at_rxid, lun);
+			if (lun == 0) {
+				isp_endcmd(isp, aep, nphdl, SCSI_STATUS_BUSY, 0);
+			} else {
+				isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
+			}
 			return;
 		}
 	}
@@ -2344,7 +2600,7 @@
 	}
 	oatp = isp_get_atpd(isp, tptr, aep->at_rxid);
 	if (oatp) {
-		isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] tag wraparound in isp_handle_platforms_atio7 (N-Port Handle 0x%04x S_ID 0x%04x OX_ID 0x%04x) oatp state %d\n",
+		isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] tag wraparound in isp_handle_platforms_atio7 (N-Port Handle 0x%04x S_ID 0x%04x OX_ID 0x%04x) oatp state %d",
 		    aep->at_rxid, nphdl, sid, aep->at_hdr.ox_id, oatp->state);
 		/*
 		 * It's not a "no resource" condition- but we can treat it like one
@@ -2351,11 +2607,12 @@
 		 */
 		goto noresrc;
 	}
+	atp->word3 = lp->prli_word3;
 	atp->tag = aep->at_rxid;
 	atp->state = ATPD_STATE_ATIO;
 	SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
 	tptr->atio_count--;
-	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
+	ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
 	atiop->init_id = nphdl;
 	atiop->ccb_h.target_id = FCPARAM(isp, chan)->isp_loopid;
 	atiop->ccb_h.target_lun = lun;
@@ -2400,7 +2657,7 @@
 	atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
 	atp->tattr = aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK;
 	atp->state = ATPD_STATE_CAM;
-	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "ATIO7[%x] CDB=0x%x lun %d datalen %u\n", aep->at_rxid, atp->cdb0, lun, atp->orig_datalen);
+	isp_prt(isp, ISP_LOGTDEBUG0, "ATIO7[0x%x] CDB=0x%x lun %d datalen %u", aep->at_rxid, atp->cdb0, lun, atp->orig_datalen);
 	xpt_done((union ccb *)atiop);
 	rls_lun_statep(isp, tptr);
 	return;
@@ -2420,15 +2677,157 @@
 	rls_lun_statep(isp, tptr);
 }
 
+
+/*
+ * Handle starting an SRR (sequence retransmit request)
+ * We get here when we've gotten the immediate notify
+ * and the return of all outstanding CTIOs for this
+ * transaction.
+ */
 static void
+isp_handle_srr_start(ispsoftc_t *isp, tstate_t *tptr, atio_private_data_t *atp)
+{
+	in_fcentry_24xx_t *inot;
+	uint32_t srr_off, ccb_off, ccb_len, ccb_end;
+	union ccb *ccb;
+
+	inot = (in_fcentry_24xx_t *)atp->srr;
+	srr_off = inot->in_srr_reloff_lo | (inot->in_srr_reloff_hi << 16);
+	ccb = atp->srr_ccb;
+	atp->srr_ccb = NULL;
+	atp->nsrr++;
+	if (ccb == NULL) {
+		isp_prt(isp, ISP_LOGWARN, "SRR[0x%08x] null ccb", atp->tag);
+		goto fail;
+	}
+
+	ccb_off = ccb->ccb_h.spriv_field0;
+	ccb_len = ccb->csio.dxfer_len;
+        ccb_end = (ccb_off == ~0)? ~0 : ccb_off + ccb_len;
+
+	switch (inot->in_srr_iu) {
+	case R_CTL_INFO_SOLICITED_DATA:
+		/*
+		 * We have to restart a FCP_DATA data out transaction
+		 */
+		atp->sendst = 0;
+		atp->last_xframt = 0;
+		atp->bytes_xfered = srr_off;
+		if (ccb_len == 0) {
+			isp_prt(isp, ISP_LOGWARN, "SRR[0x%08x] SRR offset 0x%x but current CCB doesn't transfer data", atp->tag, srr_off);
+			goto mdp;
+		}
+ 		if (srr_off < ccb_off || ccb_off > srr_off + ccb_len) {
+			isp_prt(isp, ISP_LOGWARN, "SRR[0x%08x] SRR offset 0x%x not covered by current CCB data range [0x%x..0x%x]", atp->tag, srr_off, ccb_off, ccb_end);
+			goto mdp;
+		}
+		isp_prt(isp, ISP_LOGWARN, "SRR[0x%08x] SRR offset 0x%x covered by current CCB data range [0x%x..0x%x]", atp->tag, srr_off, ccb_off, ccb_end);
+		break;
+	case R_CTL_INFO_COMMAND_STATUS:
+		isp_prt(isp, ISP_LOGTINFO, "SRR[0x%08x] Got an FCP RSP SRR- resending status", atp->tag);
+		atp->sendst = 1;
+		/*
+		 * We have to restart a FCP_RSP IU transaction
+		 */
+		break;
+	case R_CTL_INFO_DATA_DESCRIPTOR:
+		/*
+		 * We have to restart an FCP DATA in transaction
+		 */
+		isp_prt(isp, ISP_LOGWARN, "Got an FCP DATA IN SRR- dropping");
+		goto fail;
+		
+	default:
+		isp_prt(isp, ISP_LOGWARN, "Got an unknown information (%x) SRR- dropping", inot->in_srr_iu);
+		goto fail;
+	}
+
+	/*
+	 * We can't do anything until this is acked, so we might as well start it now.
+	 * We aren't going to do the usual asynchronous ack issue because we need
+	 * to make sure this gets on the wire first.
+	 */
+	if (isp_notify_ack(isp, inot)) {
+		isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
+		goto fail;
+	}
+	isp_target_start_ctio(isp, ccb, FROM_SRR);
+	return;
+fail:
+	inot->in_reserved = 1;
+	isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
+	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
+	ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
+	isp_complete_ctio(ccb);
+	return;
+mdp:
+	if (isp_notify_ack(isp, inot)) {
+		isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
+		goto fail;
+	}
+	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
+	ccb->ccb_h.status = CAM_MESSAGE_RECV;
+	/*
+	 * This is not a strict interpretation of MDP, but it's close
+	 */
+	ccb->csio.msg_ptr = &ccb->csio.sense_data.sense_buf[SSD_FULL_SIZE - 16];
+	ccb->csio.msg_len = 7;
+	ccb->csio.msg_ptr[0] = MSG_EXTENDED;
+	ccb->csio.msg_ptr[1] = 5;
+	ccb->csio.msg_ptr[2] = 0;	/* modify data pointer */
+	ccb->csio.msg_ptr[3] = srr_off >> 24;
+	ccb->csio.msg_ptr[4] = srr_off >> 16;
+	ccb->csio.msg_ptr[5] = srr_off >> 8;
+	ccb->csio.msg_ptr[6] = srr_off;
+	isp_complete_ctio(ccb);
+}
+
+
+static void
+isp_handle_srr_notify(ispsoftc_t *isp, void *inot_raw)
+{
+	tstate_t *tptr;
+	in_fcentry_24xx_t *inot = inot_raw;
+	atio_private_data_t *atp;
+	uint32_t tag = inot->in_rxid;
+	uint32_t bus = inot->in_vpidx;
+
+	if (!IS_24XX(isp)) {
+		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot_raw);
+		return;
+	}
+
+	tptr = get_lun_statep_from_tag(isp, bus, tag);
+	if (tptr == NULL) {
+		isp_prt(isp, ISP_LOGERR, "%s: cannot find tptr for tag %x in SRR Notify", __func__, tag);
+		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
+		return;
+	}
+	atp = isp_get_atpd(isp, tptr, tag);
+	if (atp == NULL) {
+		rls_lun_statep(isp, tptr);
+		isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x in SRR Notify", __func__, tag);
+		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
+		return;
+	}
+	atp->srr_notify_rcvd = 1;
+	memcpy(atp->srr, inot, sizeof (atp->srr));
+	isp_prt(isp, ISP_LOGTINFO /* ISP_LOGTDEBUG0 */, "SRR[0x%08x] inot->in_rxid flags 0x%x srr_iu=%x reloff 0x%x", inot->in_rxid, inot->in_flags, inot->in_srr_iu,
+	    inot->in_srr_reloff_lo | (inot->in_srr_reloff_hi << 16));
+	if (atp->srr_ccb)
+		isp_handle_srr_start(isp, tptr, atp);
+	rls_lun_statep(isp, tptr);
+}
+
+static void
 isp_handle_platform_ctio(ispsoftc_t *isp, void *arg)
 {
 	union ccb *ccb;
-	int sentstatus, ok, notify_cam, resid = 0;
+	int sentstatus = 0, ok = 0, notify_cam = 0, resid = 0, moved_data = 0, failure = 0;
 	tstate_t *tptr = NULL;
 	atio_private_data_t *atp = NULL;
 	int bus;
-	uint32_t tval, handle;
+	uint32_t handle;
 
 	/*
 	 * CTIO handles are 16 bits.
@@ -2446,140 +2845,148 @@
 		return;
 	}
 	isp_destroy_tgt_handle(isp, handle);
+	isp_free_pcmd(isp, ccb);
+	if (isp->isp_nactive) {
+		isp->isp_nactive--;
+	}
+
 	bus = XS_CHANNEL(ccb);
 	tptr = get_lun_statep(isp, bus, XS_LUN(ccb));
 	if (tptr == NULL) {
 		tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
 	}
-	KASSERT((tptr != NULL), ("cannot get state pointer"));
-	if (isp->isp_nactive) {
-		isp->isp_nactive++;
+	if (tptr == NULL) {
+		isp_prt(isp, ISP_LOGERR, "%s: cannot find tptr for tag %x after I/O", __func__, ccb->csio.tag_id);
+		return;
 	}
+
 	if (IS_24XX(isp)) {
+		atp = isp_get_atpd(isp, tptr, ((ct7_entry_t *)arg)->ct_rxid);
+	} else if (IS_FC(isp)) {
+		atp = isp_get_atpd(isp, tptr, ((ct2_entry_t *)arg)->ct_rxid);
+	} else {
+		atp = isp_get_atpd(isp, tptr, ((ct_entry_t *)arg)->ct_fwhandle);
+	}
+	if (atp == NULL) {
+		rls_lun_statep(isp, tptr);
+		isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ccb->csio.tag_id);
+		return;
+	}
+	KASSERT((atp->ctcnt > 0), ("ctio count not greater than zero"));
+	atp->ctcnt -= 1;
+	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
+
+	if (IS_24XX(isp)) {
 		ct7_entry_t *ct = arg;
 
-		atp = isp_get_atpd(isp, tptr, ct->ct_rxid);
-		if (atp == NULL) {
+		if (ct->ct_nphdl == CT7_SRR) {
+			atp->srr_ccb = ccb;
+			if (atp->srr_notify_rcvd)
+				isp_handle_srr_start(isp, tptr, atp);
 			rls_lun_statep(isp, tptr);
-			isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ct->ct_rxid);
 			return;
 		}
-
-		sentstatus = ct->ct_flags & CT7_SENDSTATUS;
-		ok = (ct->ct_nphdl == CT7_OK);
-		if (ok && sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) {
-			ccb->ccb_h.status |= CAM_SENT_SENSE;
-		}
-		notify_cam = ct->ct_header.rqs_seqno & 0x1;
-		if ((ct->ct_flags & CT7_DATAMASK) != CT7_NO_DATA) {
-			resid = ct->ct_resid;
-			atp->bytes_xfered += (atp->last_xframt - resid);
-			atp->last_xframt = 0;
-		}
 		if (ct->ct_nphdl == CT_HBA_RESET) {
-			ok = 0;
-			notify_cam = 1;
-			sentstatus = 1;
-			ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
-		} else if (!ok) {
-			ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
+			failure = CAM_UNREC_HBA_ERROR;
+		} else {
+			sentstatus = ct->ct_flags & CT7_SENDSTATUS;
+			ok = (ct->ct_nphdl == CT7_OK);
+			notify_cam = ct->ct_header.rqs_seqno & 0x1;
+			if ((ct->ct_flags & CT7_DATAMASK) != CT7_NO_DATA) {
+				resid = ct->ct_resid;
+				moved_data = 1;
+			}
 		}
-		tval = atp->tag;
 		isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO7[%x] sts 0x%x flg 0x%x sns %d resid %d %s", __func__,
 		    ct->ct_rxid, ct->ct_nphdl, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
-		atp->state = ATPD_STATE_PDON; /* XXX: should really come after isp_complete_ctio */
 	} else if (IS_FC(isp)) {
 		ct2_entry_t *ct = arg;
-
-		atp = isp_get_atpd(isp, tptr, ct->ct_rxid);
-		if (atp == NULL) {
+		if (ct->ct_status == CT_SRR) {
+			atp->srr_ccb = ccb;
+			if (atp->srr_notify_rcvd)
+				isp_handle_srr_start(isp, tptr, atp);
 			rls_lun_statep(isp, tptr);
-			isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ct->ct_rxid);
+			isp_target_putback_atio(ccb);
 			return;
 		}
-		sentstatus = ct->ct_flags & CT2_SENDSTATUS;
-		ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
-		if (ok && sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) {
-			ccb->ccb_h.status |= CAM_SENT_SENSE;
-		}
-		notify_cam = ct->ct_header.rqs_seqno & 0x1;
-		if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA) {
-			resid = ct->ct_resid;
-			atp->bytes_xfered += (atp->last_xframt - resid);
-			atp->last_xframt = 0;
-		}
 		if (ct->ct_status == CT_HBA_RESET) {
-			ok = 0;
-			notify_cam = 1;
-			sentstatus = 1;
-			ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
-		} else if (!ok) {
-			ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
+			failure = CAM_UNREC_HBA_ERROR;
+		} else {
+			sentstatus = ct->ct_flags & CT2_SENDSTATUS;
+			ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
+			notify_cam = ct->ct_header.rqs_seqno & 0x1;
+			if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA) {
+				resid = ct->ct_resid;
+				moved_data = 1;
+			}
 		}
 		isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO2[%x] sts 0x%x flg 0x%x sns %d resid %d %s", __func__,
 		    ct->ct_rxid, ct->ct_status, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
-		tval = atp->tag;
-		atp->state = ATPD_STATE_PDON; /* XXX: should really come after isp_complete_ctio */
 	} else {
 		ct_entry_t *ct = arg;
-		sentstatus = ct->ct_flags & CT_SENDSTATUS;
-		ok = (ct->ct_status  & ~QLTM_SVALID) == CT_OK;
-		/*
-		 * We *ought* to be able to get back to the original ATIO
-		 * here, but for some reason this gets lost. It's just as
-		 * well because it's squirrelled away as part of periph
-		 * private data.
-		 *
-		 * We can live without it as long as we continue to use
-		 * the auto-replenish feature for CTIOs.
-		 */
-		notify_cam = ct->ct_header.rqs_seqno & 0x1;
+
 		if (ct->ct_status == (CT_HBA_RESET & 0xff)) {
-			ok = 0;
-			notify_cam = 1;
-			sentstatus = 1;
-			ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
-		} else if (!ok) {
-			ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
-		} else if (ct->ct_status & QLTM_SVALID) {
-			char *sp = (char *)ct;
-			sp += CTIO_SENSE_OFFSET;
-			ccb->csio.sense_len = min(sizeof (ccb->csio.sense_data), QLTM_SENSELEN);
-			ISP_MEMCPY(&ccb->csio.sense_data, sp, ccb->csio.sense_len);
-			ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
+			failure = CAM_UNREC_HBA_ERROR;
+		} else {
+			sentstatus = ct->ct_flags & CT_SENDSTATUS;
+			ok = (ct->ct_status  & ~QLTM_SVALID) == CT_OK;
+			notify_cam = ct->ct_header.rqs_seqno & 0x1;
 		}
 		if ((ct->ct_flags & CT_DATAMASK) != CT_NO_DATA) {
 			resid = ct->ct_resid;
+			moved_data = 1;
 		}
 		isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO[%x] tag %x S_ID 0x%x lun %d sts %x flg %x resid %d %s", __func__,
 		    ct->ct_fwhandle, ct->ct_tag_val, ct->ct_iid, ct->ct_lun, ct->ct_status, ct->ct_flags, resid, sentstatus? "FIN" : "MID");
-		tval = ct->ct_fwhandle;
 	}
-	ccb->csio.resid += resid;
+	if (ok) {
+		if (moved_data) {
+			ccb->csio.resid += resid;
+			atp->bytes_xfered += (atp->last_xframt - resid);
+			atp->last_xframt = 0;
+		}
+		if (sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) {
+			ccb->ccb_h.status |= CAM_SENT_SENSE;
+		}
+		ccb->ccb_h.status |= CAM_REQ_CMP;
+	} else {
+		notify_cam = 1;
+		if (failure == CAM_UNREC_HBA_ERROR)
+			ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
+		else
+			ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
+	}
+	atp->state = ATPD_STATE_PDON;
+	rls_lun_statep(isp, tptr);
 
 	/*
-	 * We're here either because intermediate data transfers are done
-	 * and/or the final status CTIO (which may have joined with a
-	 * Data Transfer) is done.
-	 *
-	 * In any case, for this platform, the upper layers figure out
-	 * what to do next, so all we do here is collect status and
-	 * pass information along. Any DMA handles have already been
-	 * freed.
+	 * We never *not* notify CAM when there has been any error (ok == 0),
+	 * so we never need to do an ATIO putback if we're not notifying CAM.
 	 */
+	isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done (notify_cam=%d nowsendstatus=%d)", (sentstatus)? "  FINAL " : "MIDTERM ", atp->tag, notify_cam, atp->sendst);
 	if (notify_cam == 0) {
-		isp_prt(isp, ISP_LOGTDEBUG0, "  INTER CTIO[0x%x] done", tval);
+		if (atp->sendst) {
+			isp_target_start_ctio(isp, ccb, FROM_CTIO_DONE);
+		}
 		return;
 	}
-	if (tptr) {
-		rls_lun_statep(isp, tptr);
-	}
-	isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done", (sentstatus)? "  FINAL " : "MIDTERM ", tval);
 
-	if (!ok && !IS_24XX(isp)) {
+	/*
+	 * We're telling CAM we're done with this CTIO transaction.
+	 *
+	 * 24XX cards never need an ATIO put back.
+	 *
+	 * Other cards need one put back only on error.
+	 * In the latter case, a timeout will re-fire
+	 * and try again in case we didn't have
+	 * queue resources to do so at first. In any case,
+	 * once the putback is done we do the completion
+	 * call.
+	 */
+	if (ok || IS_24XX(isp)) {
+		isp_complete_ctio(ccb);
+	} else {
 		isp_target_putback_atio(ccb);
-	} else {
-		isp_complete_ctio(ccb);
 	}
 }
 
@@ -2586,7 +2993,7 @@
 static void
 isp_handle_platform_notify_scsi(ispsoftc_t *isp, in_entry_t *inot)
 {
-	(void) isp_notify_ack(isp, inot);
+	isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
 }
 
 static void
@@ -2649,9 +3056,9 @@
 			if (inot) {
 				tptr->inot_count--;
 				SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
-				ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
+				ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
 			} else {
-				ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "out of INOT structures\n");
+				ISP_PATH_PRT(isp, ISP_LOGWARN, tptr->owner, "out of INOT structures\n");
 			}
 		} else {
 			ISP_PATH_PRT(isp, ISP_LOGWARN, tptr->owner, "abort task RX_ID %x from wwn 0x%016llx, state unknown\n", inp->in_seqid, wwn);
@@ -2680,7 +3087,7 @@
 		break;
 	}
 	if (needack) {
-		(void) isp_notify_ack(isp, inp);
+		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inp);
 	}
 }
 
@@ -2688,6 +3095,7 @@
 isp_handle_platform_notify_24xx(ispsoftc_t *isp, in_fcentry_24xx_t *inot)
 {
 	uint16_t nphdl;
+	uint16_t prli_options = 0;
 	uint32_t portid;
 	fcportdb_t *lp;
 	uint8_t *ptr = NULL;
@@ -2737,10 +3145,12 @@
 			/*
 			 * Treat PRLI the same as PLOGI and make a database entry for it.
 			 */
-			if (inot->in_status_subcode == PLOGI)
+			if (inot->in_status_subcode == PLOGI) {
 				msg = "PLOGI";
-			else
+			} else {
+				prli_options = inot->in_prli_options;
 				msg = "PRLI";
+			}
 			if (ISP_FW_NEWER_THAN(isp, 4, 0, 25)) {
 				ptr = (uint8_t *)inot;  /* point to unswizzled entry! */
 				wwn =	(((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF])   << 56) |
@@ -2754,7 +3164,7 @@
 			} else {
 				wwn = INI_NONE;
 			}
-			isp_add_wwn_entry(isp, chan, wwn, nphdl, portid);
+			isp_add_wwn_entry(isp, chan, wwn, nphdl, portid, prli_options);
 			break;
 		case PDISC:
 			msg = "PDISC";
@@ -2773,7 +3183,7 @@
 		}
 		isp_prt(isp, ISP_LOGTDEBUG0, "%s Chan %d ELS N-port handle %x PortID 0x%06x RX_ID 0x%x OX_ID 0x%x", msg, chan, nphdl, portid,
 		    inot->in_rxid, inot->in_oxid);
-		(void) isp_notify_ack(isp, inot);
+		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
 		break;
 	}
 
@@ -2800,14 +3210,30 @@
 		 * this initiator (known by N-port handle).
 		 */
 		/* XXX IMPLEMENT XXX */
-		(void) isp_notify_ack(isp, inot);
+		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
 		break;
 
+	case IN24XX_SRR_RCVD:
+#ifdef	ISP_TARGET_MODE
+		isp_handle_srr_notify(isp, inot);
+		break;
+#else
+		if (ptr == NULL) {
+			ptr = "SRR RCVD";
+		}
+		/* FALLTHROUGH */
+#endif
 	case IN24XX_LINK_RESET:
+		if (ptr == NULL) {
+			ptr = "LINK RESET";
+		}
 	case IN24XX_LINK_FAILED:
-	case IN24XX_SRR_RCVD:
+		if (ptr == NULL) {
+			ptr = "LINK FAILED";
+		}
 	default:
-		(void) isp_notify_ack(isp, inot);
+		isp_prt(isp, ISP_LOGWARN, "Chan %d %s", ISP_GET_VPIDX(isp, inot->in_vpidx), ptr);
+		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
 		break;
 	}
 }
@@ -2905,6 +3331,9 @@
 	 */
 	if (mp->nt_need_ack) {
 		isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) being acked", mp->nt_ncode, mp->nt_lreserved != NULL);
+		/*
+		 * Don't need to use the guaranteed send because the caller can retry
+		 */
 		return (isp_notify_ack(isp, mp->nt_lreserved));
 	}
 	return (0);
@@ -3003,7 +3432,7 @@
 	tptr->inot_count--;
 	SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
 	rls_lun_statep(isp, tptr);
-	ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
+	ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
 	inot->ccb_h.status = CAM_MESSAGE_RECV;
 	xpt_done((union ccb *)inot);
 	return;
@@ -3013,9 +3442,11 @@
 	}
 	if (notify->nt_need_ack && notify->nt_lreserved) {
 		if (((isphdr_t *)notify->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
-			(void) isp_acknak_abts(isp, notify->nt_lreserved, ENOMEM);
+			if (isp_acknak_abts(isp, notify->nt_lreserved, ENOMEM)) {
+				isp_prt(isp, ISP_LOGWARN, "you lose- unable to send an ACKNAK");
+			}
 		} else {
-			(void) isp_notify_ack(isp, notify->nt_lreserved);
+			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, notify->nt_lreserved);
 		}
 	}
 }
@@ -3182,7 +3613,7 @@
 		'O', 'R', 'Y', ' ', 'D', 'I', 'S', 'K',
 		'0', '0', '0', '1'
 	};
-	int i, more = 0, last;
+	int r, i, more = 0, last;
 	struct isptarg_softc *softc = periph->softc;
 	struct ccb_scsiio *csio;
 	lun_id_t return_lun;
@@ -3193,7 +3624,7 @@
 	struct ccb_hdr *ccbh;
 	    
 	mtx_assert(periph->sim->mtx, MA_OWNED);
-	ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, iccb->ccb_h.path, "%s: function code 0x%x INOTQ=%c WORKQ=%c REWORKQ=%c\n", __func__, iccb->ccb_h.func_code,
+	ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG1, iccb->ccb_h.path, "%s: function code 0x%x INOTQ=%c WORKQ=%c REWORKQ=%c\n", __func__, iccb->ccb_h.func_code,
 	    TAILQ_FIRST(&softc->inot_queue)? 'y' : 'n', TAILQ_FIRST(&softc->work_queue)? 'y' : 'n', TAILQ_FIRST(&softc->rework_queue)? 'y' : 'n');
 	/*
 	 * Check for immediate notifies first
@@ -3219,7 +3650,7 @@
 	} else {
 		ccbh = TAILQ_FIRST(&softc->work_queue);
 		if (ccbh == NULL) {
-			ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, iccb->ccb_h.path, "%s: woken up but no work?\n", __func__);
+			ISP_PATH_PRT(softc->isp, ISP_LOGWARN, iccb->ccb_h.path, "%s: woken up but no work?\n", __func__);
 			xpt_release_ccb(iccb);
 			return;
 		}
@@ -3240,7 +3671,7 @@
 	flags = CAM_SEND_STATUS;
 	memset(&atio->sense_data, 0, sizeof (atio->sense_data));
 	cdb = atio->cdb_io.cdb_bytes;
-	ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, ccbh->path, "%s: [0x%x] processing ATIO from 0x%x CDB=0x%x data_offset=%u\n", __func__, atio->tag_id, atio->init_id,
+	ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG1, ccbh->path, "%s: [0x%x] processing ATIO from 0x%x CDB=0x%x data_offset=%u\n", __func__, atio->tag_id, atio->init_id,
 	    cdb[0], atio->ccb_h.ccb_data_offset);
 
 	return_lun = XS_LUN(atio);
@@ -3248,9 +3679,9 @@
 		xpt_print(atio->ccb_h.path, "[0x%x] Non-Zero Lun %d: cdb0=0x%x\n", atio->tag_id, return_lun, cdb[0]);
 		if (cdb[0] != INQUIRY && cdb[0] != REPORT_LUNS && cdb[0] != REQUEST_SENSE) {
 			status = SCSI_STATUS_CHECK_COND;
-			SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_ILLEGAL_REQUEST;
-			SDFIXED(atio->sense_data)->add_sense_code = 0x25;
-			SDFIXED(atio->sense_data)->add_sense_code_qual = 0x0;
+			SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR;
+			SDFIXED(atio->sense_data)->flags = SSD_KEY_ILLEGAL_REQUEST;
+			SDFIXED(atio->sense_data)->add_sense_code = 0x25;	/* LOGICAL UNIT NOT SUPPORTED */
 			atio->sense_len = SSD_MIN_SIZE;
 		}
 		return_lun = CAM_LUN_WILDCARD;
@@ -3273,11 +3704,16 @@
 	case READ_10:
 	case READ_12:
 	case READ_16:
-		if (isptarg_rwparm(cdb, disk_data, disk_size, atio->ccb_h.ccb_data_offset, &data_ptr, &data_len, &last)) {
+		r = isptarg_rwparm(cdb, disk_data, disk_size, atio->ccb_h.ccb_data_offset, &data_ptr, &data_len, &last);
+		if (r != 0) {
 			status = SCSI_STATUS_CHECK_COND;
-			SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
-			SDFIXED(atio->sense_data)->add_sense_code = 0x5;
-			SDFIXED(atio->sense_data)->add_sense_code_qual = 0x24;
+			SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR;
+			SDFIXED(atio->sense_data)->flags = SSD_KEY_ILLEGAL_REQUEST;
+			if (r == -1) {
+				SDFIXED(atio->sense_data)->add_sense_code = 0x21;	/* LOGICAL BLOCK ADDRESS OUT OF RANGE */
+			} else {
+				SDFIXED(atio->sense_data)->add_sense_code = 0x20;	/* INVALID COMMAND OPERATION CODE */
+			}
 			atio->sense_len = SSD_MIN_SIZE;
 		} else {
 #ifdef	ISP_FORCE_TIMEOUT
@@ -3312,11 +3748,16 @@
 	case WRITE_10:
 	case WRITE_12:
 	case WRITE_16:
-		if (isptarg_rwparm(cdb, disk_data, disk_size, atio->ccb_h.ccb_data_offset, &data_ptr, &data_len, &last)) {
+		r = isptarg_rwparm(cdb, disk_data, disk_size, atio->ccb_h.ccb_data_offset, &data_ptr, &data_len, &last);
+		if (r != 0) {
 			status = SCSI_STATUS_CHECK_COND;
-			SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
-			SDFIXED(atio->sense_data)->add_sense_code = 0x5;
-			SDFIXED(atio->sense_data)->add_sense_code_qual = 0x24;
+			SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR;
+			SDFIXED(atio->sense_data)->flags = SSD_KEY_ILLEGAL_REQUEST;
+			if (r == -1) {
+				SDFIXED(atio->sense_data)->add_sense_code = 0x21;	/* LOGICAL BLOCK ADDRESS OUT OF RANGE */
+			} else {
+				SDFIXED(atio->sense_data)->add_sense_code = 0x20;	/* INVALID COMMAND OPERATION CODE */
+			}
 			atio->sense_len = SSD_MIN_SIZE;
 		} else {
 #ifdef	ISP_FORCE_TIMEOUT
@@ -3351,9 +3792,9 @@
 		flags |= CAM_DIR_IN;
 		if (cdb[1] || cdb[2] || cdb[3]) {
 			status = SCSI_STATUS_CHECK_COND;
-			SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
-			SDFIXED(atio->sense_data)->add_sense_code = 0x5;
-			SDFIXED(atio->sense_data)->add_sense_code_qual = 0x20;
+			SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR;
+			SDFIXED(atio->sense_data)->flags = SSD_KEY_UNIT_ATTENTION;
+			SDFIXED(atio->sense_data)->add_sense_code = 0x24;	/* INVALID FIELD IN CDB */
 			atio->sense_len = SSD_MIN_SIZE;
 			break;
 		}
@@ -3375,9 +3816,9 @@
 		if (ca) {
 			ca = 0;
 			status = SCSI_STATUS_CHECK_COND;
-			SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
-			SDFIXED(atio->sense_data)->add_sense_code = 0x28;
-			SDFIXED(atio->sense_data)->add_sense_code_qual = 0x0;
+			SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR;
+			SDFIXED(atio->sense_data)->flags = SSD_KEY_UNIT_ATTENTION;
+			SDFIXED(atio->sense_data)->add_sense_code = 0x29;	/* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */
 			atio->sense_len = SSD_MIN_SIZE;
 		}
 		break;
@@ -3393,9 +3834,9 @@
 		flags |= CAM_DIR_IN;
 		if (cdb[2] || cdb[3] || cdb[4] || cdb[5]) {
 			status = SCSI_STATUS_CHECK_COND;
-			SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
-			SDFIXED(atio->sense_data)->add_sense_code = 0x5;
-			SDFIXED(atio->sense_data)->add_sense_code_qual = 0x24;
+			SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR;
+			SDFIXED(atio->sense_data)->flags = SSD_KEY_ILLEGAL_REQUEST;
+			SDFIXED(atio->sense_data)->add_sense_code =  0x24;	/* INVALID FIELD IN CDB */
 			atio->sense_len = SSD_MIN_SIZE;
 			break;
 		}
@@ -3447,9 +3888,9 @@
 	default:
 		flags |= CAM_DIR_NONE;
 		status = SCSI_STATUS_CHECK_COND;
-		SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_UNIT_ATTENTION;
-		SDFIXED(atio->sense_data)->add_sense_code = 0x5;
-		SDFIXED(atio->sense_data)->add_sense_code_qual = 0x20;
+		SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR;
+		SDFIXED(atio->sense_data)->flags = SSD_KEY_ILLEGAL_REQUEST;
+		SDFIXED(atio->sense_data)->add_sense_code = 0x20;	/* INVALID COMMAND OPERATION CODE */
 		atio->sense_len = SSD_MIN_SIZE;
 		break;
 	}
@@ -3492,7 +3933,7 @@
 	periph->softc = softc;
 	softc->periph = periph;
 	softc->path = periph->path;
-	ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, periph->path, "%s called\n", __func__);
+	ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG1, periph->path, "%s called\n", __func__);
 	return (CAM_REQ_CMP);
 }
 
@@ -3501,7 +3942,7 @@
 {
 	struct isptarg_softc *softc;
 	softc = (struct isptarg_softc *)periph->softc;
-	ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, periph->path, "%s called\n", __func__);
+	ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG1, periph->path, "%s called\n", __func__);
 	softc->periph = NULL;
 	softc->path = NULL;
 	periph->softc = NULL;
@@ -3523,13 +3964,13 @@
 	switch (ccb->ccb_h.func_code) {
 	case XPT_ACCEPT_TARGET_IO:
 		atio = (struct ccb_accept_tio *) ccb;
-		ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] ATIO seen in %s\n", atio->tag_id, __func__);
+		ISP_PATH_PRT(isp, ISP_LOGTDEBUG1, ccb->ccb_h.path, "[0x%x] ATIO seen in %s\n", atio->tag_id, __func__);
 		TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h, periph_links.tqe); 
 		xpt_schedule(periph, 1);
 		break;
 	case XPT_IMMEDIATE_NOTIFY:
 		inot = (struct ccb_immediate_notify *) ccb;
-		ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] INOT for 0x%x seen in %s\n", inot->tag_id, inot->seq_id, __func__);
+		ISP_PATH_PRT(isp, ISP_LOGTDEBUG1, ccb->ccb_h.path, "[0x%x] INOT for 0x%x seen in %s\n", inot->tag_id, inot->seq_id, __func__);
 		TAILQ_INSERT_TAIL(&softc->inot_queue, &ccb->ccb_h, periph_links.tqe); 
 		xpt_schedule(periph, 1);
 		break;
@@ -3540,8 +3981,16 @@
 		}
 		atio = ccb->ccb_h.ccb_atio;
 		if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
-			cam_error_print(ccb, CAM_ESF_ALL, CAM_EPF_ALL);
-			xpt_action((union ccb *)atio);
+			if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_MESSAGE_RECV) {
+				uint32_t newoff = (ccb->csio.msg_ptr[3] << 24) | (ccb->csio.msg_ptr[4] << 16) | (ccb->csio.msg_ptr[5] << 8) | (ccb->csio.msg_ptr[6]);
+				ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "[0x%08x] got message to return to reset offset to 0x%x\n", atio->tag_id, newoff);
+				atio->ccb_h.ccb_data_offset = newoff;
+				TAILQ_INSERT_TAIL(&softc->rework_queue, &atio->ccb_h, periph_links.tqe); 
+				xpt_schedule(periph, 1);
+			} else {
+				cam_error_print(ccb, CAM_ESF_ALL, CAM_EPF_ALL);
+				xpt_action((union ccb *)atio);
+			}
 		} else if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
 			ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] MID CTIO seen in %s\n", atio->tag_id, __func__);
 			TAILQ_INSERT_TAIL(&softc->rework_queue, &atio->ccb_h, periph_links.tqe); 
@@ -3558,7 +4007,7 @@
 			ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
 		}
 		inot = ccb->ccb_h.ccb_inot;
-		ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, inot->ccb_h.path, "[0x%x] recycle notify for tag 0x%x\n", inot->tag_id, inot->seq_id);
+		ISP_PATH_PRT(isp, ISP_LOGTDEBUG1, inot->ccb_h.path, "[0x%x] recycle notify for tag 0x%x\n", inot->tag_id, inot->seq_id);
 		xpt_release_ccb(ccb);
 		xpt_action((union ccb *)inot);
 		break;
@@ -3890,7 +4339,7 @@
 	}
 
 	if (lba + cnt > dl) {
-		return (-1);
+		return (-2);
 	}
 
 
@@ -3976,14 +4425,6 @@
 
 	handle = isp_find_handle(isp, xs);
 
-	if (handle != ISP_HANDLE_FREE && !XS_CMD_WPEND_P(xs)) {
-		isp_xs_prt(isp, xs, ISP_LOGWARN, "first watchdog (handle 0x%x) timed out- deferring for grace period", handle);
-		callout_reset(&PISP_PCMD(xs)->wdog, 2 * hz, isp_watchdog, xs);
-		XS_CMD_S_WPEND(xs);
-		return;
-	}
-	XS_C_TACTIVE(xs);
-
 	/*
 	 * Hand crank the interrupt code just to be sure the command isn't stuck somewhere.
 	 */
@@ -4031,7 +4472,8 @@
 		} 
 		isp_destroy_handle(isp, handle);
 		isp_prt(isp, ISP_LOGERR, "%s: timeout for handle 0x%x", __func__, handle);
-		XS_SETERR(xs, CAM_CMD_TIMEOUT);
+		xs->ccb_h.status &= ~CAM_STATUS_MASK;
+		xs->ccb_h.status |= CAM_CMD_TIMEOUT;
 		isp_prt_endcmd(isp, xs);
 		isp_done(xs);
 	} else {
@@ -4122,7 +4564,7 @@
 			continue;
 		}
 		if (lp->gone_timer != 0) {
-			isp_prt(isp, ISP_LOGSANCFG, "%s: Chan %d more to do for target %u (timer=%u)", __func__, chan, lp->dev_map_idx - 1, lp->gone_timer);
+			isp_prt(isp, ISP_LOG_SANCFG, "%s: Chan %d more to do for target %u (timer=%u)", __func__, chan, lp->dev_map_idx - 1, lp->gone_timer);
 			lp->gone_timer -= 1;
 			more_to_do++;
 			continue;
@@ -4139,7 +4581,7 @@
 			callout_reset(&fc->gdt, hz, isp_gdt, fc);
 		} else {
 			callout_deactivate(&fc->gdt);
-			isp_prt(isp, ISP_LOGSANCFG, "Chan %d Stopping Gone Device Timer @ %lu", chan, (unsigned long) time_uptime);
+			isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Stopping Gone Device Timer @ %lu", chan, (unsigned long) time_uptime);
 		}
 	}
 	ISP_UNLOCK(isp);
@@ -4171,7 +4613,7 @@
 	int dbidx, tgt, i;
 
 	ISP_LOCK(isp);
-	isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Chan %d Loop Down Timer expired @ %lu", chan, (unsigned long) time_uptime);
+	isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop Down Timer expired @ %lu", chan, (unsigned long) time_uptime);
 	callout_deactivate(&fc->ldt);
 
 	/*
@@ -4190,8 +4632,8 @@
 		/*
 		 * XXX: CLEAN UP AND COMPLETE ANY PENDING COMMANDS FIRST!
 		 */
-		
 
+
 		for (i = 0; i < isp->isp_maxcmds; i++) {
 			struct ccb_scsiio *xs;
 
@@ -4211,7 +4653,7 @@
 		/*
 		 * Mark that we've announced that this device is gone....
 		 */
-		lp->reserved = 1;
+		lp->announced = 1;
 
 		/*
 		 * but *don't* change the state of the entry. Just clear
@@ -4254,7 +4696,7 @@
 	for (;;) {
 		int lb, lim;
 
-		isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d checking FC state", __func__, chan);
+		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d checking FC state", __func__, chan);
 		lb = isp_fc_runstate(isp, chan, 250000);
 
 		/*
@@ -4272,9 +4714,9 @@
 			fc->loop_down_time += slp;
 
 			if (lb < 0) {
-				isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC loop not up (down count %d)", __func__, chan, fc->loop_down_time);
+				isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC loop not up (down count %d)", __func__, chan, fc->loop_down_time);
 			} else {
-				isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC got to %d (down count %d)", __func__, chan, lb, fc->loop_down_time);
+				isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC got to %d (down count %d)", __func__, chan, lb, fc->loop_down_time);
 			}
 
 			/*
@@ -4304,11 +4746,14 @@
 			}
 
 		} else if (lb) {
-			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC Loop Down", __func__, chan);
+			isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC Loop Down", __func__, chan);
 			fc->loop_down_time += slp;
-			slp = 60;
+			if (fc->loop_down_time > 300)
+				slp = 0;
+			else
+				slp = 60;
 		} else {
-			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC state OK", __func__, chan);
+			isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC state OK", __func__, chan);
 			fc->loop_down_time = 0;
 			slp = 0;
 		}
@@ -4326,7 +4771,7 @@
 			isp_unfreeze_loopdown(isp, chan);
 		}
 
-		isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d sleep time %d", __func__, chan, slp);
+		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d sleep time %d", __func__, chan, slp);
 
 		msleep(fc, &isp->isp_osinfo.lock, PRIBIO, "ispf", slp * hz);
 
@@ -4338,7 +4783,7 @@
 		 * to settle.
 		 */
 		if (slp == 0 && fc->hysteresis) {
-			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "%s: Chan %d sleep hysteresis ticks %d", __func__, chan, fc->hysteresis * hz);
+			isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d sleep hysteresis ticks %d", __func__, chan, fc->hysteresis * hz);
 			mtx_unlock(&isp->isp_osinfo.lock);
 			pause("ispt", fc->hysteresis * hz);
 			mtx_lock(&isp->isp_osinfo.lock);
@@ -4355,7 +4800,7 @@
 	struct ccb_trans_settings *cts;
 
 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("isp_action\n"));
-	
+
 	isp = (ispsoftc_t *)cam_sim_softc(sim);
 	mtx_assert(&isp->isp_lock, MA_OWNED);
 
@@ -4388,6 +4833,7 @@
 				break;
 			}
 		}
+		ccb->csio.req_map = NULL;
 #ifdef	DIAGNOSTIC
 		if (ccb->ccb_h.target_id > (ISP_MAX_TARGETS(isp) - 1)) {
 			xpt_print(ccb->ccb_h.path, "invalid target\n");
@@ -4412,7 +4858,6 @@
 		error = isp_start((XS_T *) ccb);
 		switch (error) {
 		case CMD_QUEUED:
-			XS_CMD_S_CLEAR(ccb);
 			ccb->ccb_h.status |= CAM_SIM_QUEUED;
 			if (ccb->ccb_h.timeout == CAM_TIME_INFINITY) {
 				break;
@@ -4422,7 +4867,6 @@
 				ts = 60*1000;
 			}
 			ts = isp_mstohz(ts);
-			XS_S_TACTIVE(ccb);
 			callout_reset(&PISP_PCMD(ccb)->wdog, ts, isp_watchdog, ccb);
 			break;
 		case CMD_RQLATER:
@@ -4449,7 +4893,7 @@
 			isp_prt(isp, ISP_LOGDEBUG0, "%d.%d retry later", XS_TGT(ccb), XS_LUN(ccb));
 			cam_freeze_devq(ccb->ccb_h.path);
 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
-			XS_SETERR(ccb, CAM_REQUEUE_REQ);
+			ccb->ccb_h.status = CAM_REQUEUE_REQ;
 			isp_free_pcmd(isp, ccb);
 			xpt_done(ccb);
 			break;
@@ -4457,7 +4901,7 @@
 			isp_free_pcmd(isp, ccb);
 			cam_freeze_devq(ccb->ccb_h.path);
 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 100, 0);
-			XS_SETERR(ccb, CAM_REQUEUE_REQ);
+			ccb->ccb_h.status = CAM_REQUEUE_REQ;
 			xpt_done(ccb);
 			break;
 		case CMD_COMPLETE:
@@ -4465,7 +4909,7 @@
 			break;
 		default:
 			isp_prt(isp, ISP_LOGERR, "What's this? 0x%x at %d in file %s", error, __LINE__, __FILE__);
-			XS_SETERR(ccb, CAM_REQ_CMP_ERR);
+			ccb->ccb_h.status = CAM_REQUEUE_REQ;
 			isp_free_pcmd(isp, ccb);
 			xpt_done(ccb);
 		}
@@ -4503,8 +4947,8 @@
 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
 			break;
 		}
-		ccb->ccb_h.sim_priv.entries[0].field = 0;
-		ccb->ccb_h.sim_priv.entries[1].ptr = isp;
+		ccb->ccb_h.spriv_field0 = 0;
+		ccb->ccb_h.spriv_ptr1 = isp;
 		ccb->ccb_h.flags = 0;
 
 		if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
@@ -4516,7 +4960,7 @@
 			}
 			tptr->atio_count++;
 			SLIST_INSERT_HEAD(&tptr->atios, &ccb->ccb_h, sim_links.sle);
-			ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "Put FREE ATIO (tag id 0x%x), count now %d\n",
+			ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path, "Put FREE ATIO (tag id 0x%x), count now %d\n",
 			    ccb->atio.tag_id, tptr->atio_count);
 		} else if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
 			if (ccb->cin1.tag_id) {
@@ -4527,12 +4971,12 @@
 			}
 			tptr->inot_count++;
 			SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
-			ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "Put FREE INOT, (seq id 0x%x) count now %d\n",
+			ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path, "Put FREE INOT, (seq id 0x%x) count now %d\n",
 			    ccb->cin1.seq_id, tptr->inot_count);
 		} else if (ccb->ccb_h.func_code == XPT_IMMED_NOTIFY) {
 			tptr->inot_count++;
 			SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
-			ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "Put FREE INOT, (seq id 0x%x) count now %d\n",
+			ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path, "Put FREE INOT, (seq id 0x%x) count now %d\n",
 			    ccb->cin1.seq_id, tptr->inot_count);
 		}
 		rls_lun_statep(isp, tptr);
@@ -4567,7 +5011,8 @@
 			rls_lun_statep(isp, tptr);
 			cam_freeze_devq(ccb->ccb_h.path);
 			cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
-			XS_SETERR(ccb, CAM_REQUEUE_REQ);
+			ccb->ccb_h.status &= ~CAM_STATUS_MASK;
+			ccb->ccb_h.status |= CAM_REQUEUE_REQ;
 			break;
 		}
 		isp_put_ntpd(isp, tptr, ntp);
@@ -4578,7 +5023,7 @@
 		break;
 	}
 	case XPT_CONT_TARGET_IO:
-		isp_target_start_ctio(isp, ccb);
+		isp_target_start_ctio(isp, ccb, FROM_CAM);
 		break;
 #endif
 	case XPT_RESET_DEV:		/* BDR the specified SCSI device */
@@ -4874,13 +5319,24 @@
 			}
 			if (rchange) {
 				ISP_PATH_PRT(isp, ISP_LOGCONFIG, ccb->ccb_h.path, "changing role on from %d to %d\n", fcp->role, newrole);
+#ifdef	ISP_TARGET_MODE
+				ISP_SET_PC(isp, bus, tm_enabled, 0);
+				ISP_SET_PC(isp, bus, tm_luns_enabled, 0);
+#endif
 				if (isp_fc_change_role(isp, bus, newrole) != 0) {
 					ccb->ccb_h.status = CAM_REQ_CMP_ERR;
+					xpt_done(ccb);
+					break;
+				}
 #ifdef	ISP_TARGET_MODE
-				} else if (newrole == ISP_ROLE_TARGET || newrole == ISP_ROLE_BOTH) {
+				if (newrole == ISP_ROLE_TARGET || newrole == ISP_ROLE_BOTH) {
+					/*
+					 * Give the new role a chance to complain and settle
+					 */
+					msleep(isp, &isp->isp_lock, PRIBIO, "taking a breather", 2);
 					ccb->ccb_h.status = isp_enable_deferred_luns(isp, bus);
+				}
 #endif
-				}
 			}
 		}
 		xpt_done(ccb);
@@ -5026,10 +5482,8 @@
 		xpt_print(sccb->ccb_h.path, "cam completion status 0x%x\n", sccb->ccb_h.status);
 	}
 
-	XS_CMD_S_DONE(sccb);
-	if (XS_TACTIVE_P(sccb))
+	if (callout_active(&PISP_PCMD(sccb)->wdog))
 		callout_stop(&PISP_PCMD(sccb)->wdog);
-	XS_CMD_S_CLEAR(sccb);
 	isp_free_pcmd(isp, (union ccb *) sccb);
 	xpt_done((union ccb *) sccb);
 }
@@ -5038,8 +5492,9 @@
 isp_async(ispsoftc_t *isp, ispasync_t cmd, ...)
 {
 	int bus;
-	static const char prom[] = "Chan %d PortID 0x%06x handle 0x%x role %s %s WWPN 0x%08x%08x";
-	static const char prom2[] = "Chan %d PortID 0x%06x handle 0x%x role %s %s tgt %u WWPN 0x%08x%08x";
+	static const char prom0[] = "Chan %d PortID 0x%06x handle 0x%x %s %s WWPN 0x%08x%08x";
+	static const char prom2[] = "Chan %d PortID 0x%06x handle 0x%x %s %s tgt %u WWPN 0x%08x%08x";
+	char buf[64];
 	char *msg = NULL;
 	target_id_t tgt;
 	fcportdb_t *lp;
@@ -5148,7 +5603,7 @@
 				}
 				if (!callout_active(&fc->ldt)) {
 					callout_reset(&fc->ldt, fc->loop_down_limit * hz, isp_ldt, fc);
-					isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Starting Loop Down Timer @ %lu", (unsigned long) time_uptime);
+					isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Starting Loop Down Timer @ %lu", (unsigned long) time_uptime);
 				}
 			}
 		}
@@ -5177,9 +5632,9 @@
 		lp = va_arg(ap, fcportdb_t *);
 		va_end(ap);
 		fc = ISP_FC_PC(isp, bus);
-		lp->reserved = 0;
+		lp->announced = 0;
 		lp->gone_timer = 0;
-		if ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) && (lp->roles & (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT))) {
+		if ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) && (lp->prli_word3 & PRLI_WD3_TARGET_FUNCTION)) {
 			int dbidx = lp - FCPARAM(isp, bus)->portdb;
 			int i;
 
@@ -5199,12 +5654,13 @@
 				isp_dump_portdb(isp, bus);
 			}
 		}
+		isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
 		if (lp->dev_map_idx) {
 			tgt = lp->dev_map_idx - 1;
-			isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, roles[lp->roles], "arrived at", tgt, (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
+			isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, buf, "arrived at", tgt, (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
 			isp_make_here(isp, bus, tgt);
 		} else {
-			isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "arrived", (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
+			isp_prt(isp, ISP_LOGCONFIG, prom0, bus, lp->portid, lp->handle, buf, "arrived", (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
 		}
 		break;
 	case ISPASYNC_DEV_CHANGED:
@@ -5213,7 +5669,7 @@
 		lp = va_arg(ap, fcportdb_t *);
 		va_end(ap);
 		fc = ISP_FC_PC(isp, bus);
-		lp->reserved = 0;
+		lp->announced = 0;
 		lp->gone_timer = 0;
 		if (isp_change_is_bad) {
 			lp->state = FC_PORTDB_STATE_NIL;
@@ -5224,20 +5680,22 @@
 				isp_prt(isp, ISP_LOGCONFIG, prom3, bus, lp->portid, tgt, "change is bad");
 				isp_make_gone(isp, bus, tgt);
 			} else {
-				isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "changed and departed",
+				isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
+				isp_prt(isp, ISP_LOGCONFIG, prom0, bus, lp->portid, lp->handle, buf, "changed and departed",
 				    (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
 			}
 		} else {
 			lp->portid = lp->new_portid;
-			lp->roles = lp->new_roles;
+			lp->prli_word3 = lp->new_prli_word3;
+			isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
 			if (lp->dev_map_idx) {
 				int t = lp->dev_map_idx - 1;
 				FCPARAM(isp, bus)->isp_dev_map[t] = (lp - FCPARAM(isp, bus)->portdb) + 1;
 				tgt = lp->dev_map_idx - 1;
-				isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, roles[lp->roles], "changed at", tgt,
+				isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, buf, "changed at", tgt,
 				    (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
 			} else {
-				isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "changed", (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
+				isp_prt(isp, ISP_LOGCONFIG, prom0, bus, lp->portid, lp->handle, buf, "changed", (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
 			}
 		}
 		break;
@@ -5246,12 +5704,13 @@
 		bus = va_arg(ap, int);
 		lp = va_arg(ap, fcportdb_t *);
 		va_end(ap);
+		isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
 		if (lp->dev_map_idx) {
 			tgt = lp->dev_map_idx - 1;
-			isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, roles[lp->roles], "stayed at", tgt,
+			isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, buf, "stayed at", tgt,
 		    	    (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
 		} else {
-			isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "stayed",
+			isp_prt(isp, ISP_LOGCONFIG, prom0, bus, lp->portid, lp->handle, buf, "stayed",
 		    	    (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
 		}
 		break;
@@ -5270,18 +5729,19 @@
 		 * announce that it's gone.
 		 *
 		 */
-		if (lp->dev_map_idx && lp->reserved == 0) {
-			lp->reserved = 1;
+		isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
+		if (lp->dev_map_idx && lp->announced == 0) {
+			lp->announced = 1;
 			lp->state = FC_PORTDB_STATE_ZOMBIE;
 			lp->gone_timer = ISP_FC_PC(isp, bus)->gone_device_time;
 			if (fc->ready && !callout_active(&fc->gdt)) {
-				isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Chan %d Starting Gone Device Timer with %u seconds time now %lu", bus, lp->gone_timer, (unsigned long)time_uptime);
+				isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Starting Gone Device Timer with %u seconds time now %lu", bus, lp->gone_timer, (unsigned long)time_uptime);
 				callout_reset(&fc->gdt, hz, isp_gdt, fc);
 			}
 			tgt = lp->dev_map_idx - 1;
-			isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, roles[lp->roles], "gone zombie at", tgt, (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
-		} else if (lp->reserved == 0) {
-			isp_prt(isp, ISP_LOGCONFIG, prom, bus, lp->portid, lp->handle, roles[lp->roles], "departed", (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
+			isp_prt(isp, ISP_LOGCONFIG, prom2, bus, lp->portid, lp->handle, buf, "gone zombie at", tgt, (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
+		} else if (lp->announced == 0) {
+			isp_prt(isp, ISP_LOGCONFIG, prom0, bus, lp->portid, lp->handle, buf, "departed", (uint32_t) (lp->port_wwn >> 32), (uint32_t) lp->port_wwn);
 		}
 		break;
 	case ISPASYNC_CHANGE_NOTIFY:
@@ -5315,7 +5775,7 @@
 		 * If the loop down timer is running, cancel it.
 		 */
 		if (fc->ready && callout_active(&fc->ldt)) {
-			isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Stopping Loop Down Timer @ %lu", (unsigned long) time_uptime);
+			isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Stopping Loop Down Timer @ %lu", (unsigned long) time_uptime);
 			callout_stop(&fc->ldt);
 		}
 		isp_prt(isp, ISP_LOGINFO, msg, bus);
@@ -5355,6 +5815,7 @@
 		case NT_HBA_RESET:
 			isp_del_all_wwn_entries(isp, ISP_NOCHAN);
 			break;
+		case NT_GLOBAL_LOGOUT:
 		case NT_LOGOUT:
 			/*
 			 * This is device arrival/departure notification
@@ -5396,6 +5857,29 @@
 		}
 		break;
 	}
+	case ISPASYNC_TARGET_NOTIFY_ACK:
+	{
+		void *inot;
+		va_start(ap, cmd);
+		inot = va_arg(ap, void *);
+		va_end(ap);
+		if (isp_notify_ack(isp, inot)) {
+			isp_tna_t *tp = malloc(sizeof (*tp), M_DEVBUF, M_NOWAIT);
+			if (tp) {
+				tp->isp = isp;
+				if (inot) {
+					memcpy(tp->data, inot, sizeof (tp->data));
+					tp->not = tp->data;
+				} else {
+					tp->not = NULL;
+				}
+				(void) timeout(isp_refire_notify_ack, tp, 5);
+			} else {
+				isp_prt(isp, ISP_LOGERR, "you lose- cannot allocate a notify refire");
+			}
+		}
+		break;
+	}
 	case ISPASYNC_TARGET_ACTION:
 	{
 		isphdr_t *hp;
@@ -5628,13 +6112,13 @@
 isp_prt(ispsoftc_t *isp, int level, const char *fmt, ...)
 {
 	int loc;
-	char lbuf[128];
+	char lbuf[200];
 	va_list ap;
 
 	if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
 		return;
 	}
-	sprintf(lbuf, "%s: ", device_get_nameunit(isp->isp_dev));
+	snprintf(lbuf, sizeof (lbuf), "%s: ", device_get_nameunit(isp->isp_dev));
 	loc = strlen(lbuf);
 	va_start(ap, fmt);
 	vsnprintf(&lbuf[loc], sizeof (lbuf) - loc - 1, fmt, ap); 
@@ -5805,6 +6289,47 @@
 	bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
 }
 
+int
+isp_fcp_next_crn(ispsoftc_t *isp, uint8_t *crnp, XS_T *cmd)
+{
+	uint32_t chan = XS_CHANNEL(cmd);
+	uint32_t tgt = XS_TGT(cmd);
+	uint32_t lun = XS_LUN(cmd);
+	struct isp_fc *fc = &isp->isp_osinfo.pc.fc[chan];
+	int idx = NEXUS_HASH(tgt, lun);
+	struct isp_nexus *nxp = fc->nexus_hash[idx];
+
+	while (nxp) {
+		if (nxp->tgt == tgt && nxp->lun == lun)
+			break;
+		nxp = nxp->next;
+	}
+	if (nxp == NULL) {
+		nxp = fc->nexus_free_list;
+		if (nxp == NULL) {
+			nxp = malloc(sizeof (struct isp_nexus), M_DEVBUF, M_ZERO|M_NOWAIT);
+			if (nxp == NULL) {
+				return (-1);
+			}
+		} else {
+			fc->nexus_free_list = nxp->next;
+		}
+		nxp->tgt = tgt;
+		nxp->lun = lun;
+		nxp->next = fc->nexus_hash[idx];
+		fc->nexus_hash[idx] = nxp;
+	}
+	if (nxp) {
+		if (nxp->crnseed == 0)
+			nxp->crnseed = 1;
+		if (cmd)
+			PISP_PCMD(cmd)->crn = nxp->crnseed;
+		*crnp = nxp->crnseed++;
+		return (0);
+	}
+	return (-1);
+}
+
 void
 isp_timer(void *arg)
 {
@@ -5814,3 +6339,20 @@
 #endif
 	callout_reset(&isp->isp_osinfo.tmo, hz, isp_timer, isp);
 }
+
+isp_ecmd_t *
+isp_get_ecmd(ispsoftc_t *isp)
+{
+	isp_ecmd_t *ecmd = isp->isp_osinfo.ecmd_free;
+	if (ecmd) {
+		isp->isp_osinfo.ecmd_free = ecmd->next;
+	}
+	return (ecmd);
+}
+
+void
+isp_put_ecmd(ispsoftc_t *isp, isp_ecmd_t *ecmd)
+{
+	ecmd->next = isp->isp_osinfo.ecmd_free;
+	isp->isp_osinfo.ecmd_free = ecmd;
+}

Modified: trunk/sys/dev/isp/isp_freebsd.h
===================================================================
--- trunk/sys/dev/isp/isp_freebsd.h	2016-09-15 08:25:48 UTC (rev 7985)
+++ trunk/sys/dev/isp/isp_freebsd.h	2016-09-15 08:26:26 UTC (rev 7986)
@@ -75,6 +75,16 @@
 
 #define	ISP_IFLAGS	INTR_TYPE_CAM | INTR_ENTROPY | INTR_MPSAFE
 
+#define	N_XCMDS		64
+#define	XCMD_SIZE	512
+struct ispsoftc;
+typedef union isp_ecmd {
+	union isp_ecmd *	next;
+	uint8_t			data[XCMD_SIZE];
+} isp_ecmd_t;
+isp_ecmd_t *	isp_get_ecmd(struct ispsoftc *);
+void		isp_put_ecmd(struct ispsoftc *, isp_ecmd_t *);
+
 #ifdef	ISP_TARGET_MODE
 /* Not quite right, but there was no bump for this change */
 #if __FreeBSD_version < 225469
@@ -100,12 +110,22 @@
 	uint32_t	portid;
 	uint16_t	rxid;	/* wire rxid */
 	uint16_t	oxid;	/* wire oxid */
+	uint16_t	word3;	/* PRLI word3 params */
+	uint16_t	ctcnt;	/* number of CTIOs currently active */
 	uint32_t
-			cdb0	: 8,
-				: 1,
-			dead	: 1,
-			tattr	: 3,
-			state	: 3;
+			srr_notify_rcvd	: 1,
+			cdb0		: 8,
+			sendst		: 1,
+			dead		: 1,
+			tattr		: 3,
+			state		: 3;
+	void *		ests;
+	/*
+	 * The current SRR notify copy
+	 */
+	uint8_t		srr[64];	/*  sb QENTRY_LEN, but order of definitions is wrong */
+	void *		srr_ccb;
+	uint32_t	nsrr;
 } atio_private_data_t;
 #define	ATPD_STATE_FREE			0
 #define	ATPD_STATE_ATIO			1
@@ -123,6 +143,11 @@
 		uint32_t tag_id, seq_id;
 	} rd;
 };
+typedef struct isp_timed_notify_ack {
+	void *isp;
+	void *not;
+	uint8_t data[64];	 /* sb QENTRY_LEN, but order of definitions is wrong */
+} isp_tna_t;
 
 typedef struct tstate {
 	SLIST_ENTRY(tstate) next;
@@ -151,15 +176,32 @@
  */
 struct isp_pcmd {
 	struct isp_pcmd *	next;
-	bus_dmamap_t 		dmap;	/* dma map for this command */
-	struct ispsoftc *	isp;	/* containing isp */
-	struct callout		wdog;	/* watchdog timer */
-	uint8_t 		crn;	/* command reference number */
+	bus_dmamap_t 		dmap;		/* dma map for this command */
+	struct ispsoftc *	isp;		/* containing isp */
+	struct callout		wdog;		/* watchdog timer */
+	uint8_t			totslen;	/* sense length on status response */
+	uint8_t			cumslen;	/* sense length on status response */
+	uint8_t 		crn;		/* command reference number */
 };
 #define	ISP_PCMD(ccb)		(ccb)->ccb_h.spriv_ptr1
 #define	PISP_PCMD(ccb)		((struct isp_pcmd *)ISP_PCMD(ccb))
 
 /*
+ * Per nexus info.
+ */
+struct isp_nexus {
+	struct isp_nexus *	next;
+	uint32_t
+		crnseed	:	8;	/* next command reference number */
+	uint32_t
+		tgt	:	16,	/* TGT for target */
+		lun	:	16;	/* LUN for target */
+};
+#define	NEXUS_HASH_WIDTH	32
+#define	INITIAL_NEXUS_COUNT	MAX_FC_TARG
+#define	NEXUS_HASH(tgt, lun)	((tgt + lun) % NEXUS_HASH_WIDTH)
+
+/*
  * Per channel information
  */
 SLIST_HEAD(tslist, tstate);
@@ -176,6 +218,11 @@
 	uint32_t loop_down_time;
 	uint32_t loop_down_limit;
 	uint32_t gone_device_time;
+	/*
+	 * Per target/lun info- just to keep a per-ITL nexus crn count
+	 */
+	struct isp_nexus *nexus_hash[NEXUS_HASH_WIDTH];
+	struct isp_nexus *nexus_free_list;
 	uint32_t
 #ifdef	ISP_TARGET_MODE
 #ifdef	ISP_INTERNAL_TARGET
@@ -202,8 +249,10 @@
 #ifdef	ISP_INTERNAL_TARGET
 	struct proc *		target_proc;
 #endif
+#if defined(DEBUG)
+	unsigned int inject_lost_data_frame;
 #endif
-	uint8_t	crnseed;	/* next command reference number */
+#endif
 };
 
 struct isp_spi {
@@ -266,6 +315,7 @@
 #else
 				: 2,
 #endif
+		sixtyfourbit	: 1,	/* sixtyfour bit platform */
 		timer_active	: 1,
 		autoconf	: 1,
 		ehook_active	: 1,
@@ -288,6 +338,10 @@
 	cam_status *		rptr;
 #endif
 
+	bus_addr_t		ecmd_dma;
+	isp_ecmd_t *		ecmd_base;
+	isp_ecmd_t *		ecmd_free;
+
 	/*
 	 * Per-type private storage...
 	 */
@@ -318,16 +372,7 @@
 		ISP_FC_PC(isp, chan)-> tag = val;	\
 	}
 
-#define	FCP_NEXT_CRN(isp, cmd, rslt, chan, tgt, lun)				\
-	if ((isp)->isp_osinfo.pc.fc[(chan)].crnseed == 0) {			\
-		(isp)->isp_osinfo.pc.fc[(chan)].crnseed = 1;			\
-	}									\
-	if (cmd) {								\
-		PISP_PCMD(cmd)->crn = (isp)->isp_osinfo.pc.fc[(chan)].crnseed;	\
-	}									\
-	(rslt) = (isp)->isp_osinfo.pc.fc[(chan)].crnseed++
-	
-
+#define	FCP_NEXT_CRN	isp_fcp_next_crn
 #define	isp_lock	isp_osinfo.lock
 #define	isp_bus_tag	isp_osinfo.bus_tag
 #define	isp_bus_handle	isp_osinfo.bus_handle
@@ -341,7 +386,6 @@
 /*
  * Required Macros/Defines
  */
-
 #define	ISP_FC_SCRLEN		0x1000
 
 #define	ISP_MEMZERO(a, b)	memset(a, 0, b)
@@ -459,20 +503,17 @@
 #define	XS_STSP(ccb)		(&(ccb)->scsi_status)
 #define	XS_SNSP(ccb)		(&(ccb)->sense_data)
 
-#define	XS_SNSLEN(ccb)		\
-	imin((sizeof((ccb)->sense_data)), ccb->sense_len - ccb->sense_resid)
+#define	XS_TOT_SNSLEN(ccb)	ccb->sense_len
+#define	XS_CUR_SNSLEN(ccb)	(ccb->sense_len - ccb->sense_resid)
 
 #define	XS_SNSKEY(ccb)		(scsi_get_sense_key(&(ccb)->sense_data, \
-				 ccb->sense_len - ccb->sense_resid, 	\
-				 /*show_errors*/ 1))
+				 ccb->sense_len - ccb->sense_resid, 1))
 
 #define	XS_SNSASC(ccb)		(scsi_get_asc(&(ccb)->sense_data,	\
-				 ccb->sense_len - ccb->sense_resid, 	\
-				 /*show_errors*/ 1))
+				 ccb->sense_len - ccb->sense_resid, 1))
 
 #define	XS_SNSASCQ(ccb)		(scsi_get_ascq(&(ccb)->sense_data,	\
-				 ccb->sense_len - ccb->sense_resid, 	\
-				 /*show_errors*/ 1))
+				 ccb->sense_len - ccb->sense_resid, 1))
 #define	XS_TAG_P(ccb)	\
 	(((ccb)->ccb_h.flags & CAM_TAG_ACTION_VALID) && \
 	 (ccb)->tag_action != CAM_TAG_ACTION_NONE)
@@ -483,8 +524,7 @@
 		
 
 #define	XS_SETERR(ccb, v)	(ccb)->ccb_h.status &= ~CAM_STATUS_MASK, \
-				(ccb)->ccb_h.status |= v, \
-				(ccb)->ccb_h.spriv_field0 |= ISP_SPRIV_ERRSET
+				(ccb)->ccb_h.status |= v
 
 #	define	HBA_NOERROR		CAM_REQ_INPROG
 #	define	HBA_BOTCH		CAM_UNREC_HBA_ERROR
@@ -499,22 +539,32 @@
 
 #define	XS_ERR(ccb)		((ccb)->ccb_h.status & CAM_STATUS_MASK)
 
-#define	XS_NOERR(ccb)		\
-	(((ccb)->ccb_h.spriv_field0 & ISP_SPRIV_ERRSET) == 0 || \
-	 ((ccb)->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG)
+#define	XS_NOERR(ccb)		(((ccb)->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG)
 
-#define	XS_INITERR(ccb)		\
-	XS_SETERR(ccb, CAM_REQ_INPROG), (ccb)->ccb_h.spriv_field0 = 0
+#define	XS_INITERR(ccb)		XS_SETERR(ccb, CAM_REQ_INPROG), ccb->sense_resid = ccb->sense_len
 
-#define	XS_SAVE_SENSE(xs, sense_ptr, slen)	do {			\
-		(xs)->ccb_h.status |= CAM_AUTOSNS_VALID;		\
-		memset(&(xs)->sense_data, 0, sizeof((xs)->sense_data));	\
-		memcpy(&(xs)->sense_data, sense_ptr, imin(XS_SNSLEN(xs),\
-		       slen)); 						\
-		if (slen < (xs)->sense_len) 				\
-			(xs)->sense_resid = (xs)->sense_len - slen;	\
-	} while (0);
+#define	XS_SAVE_SENSE(xs, sense_ptr, totslen, slen)	do {			\
+		uint32_t tlen = slen;						\
+		if (tlen > (xs)->sense_len)					\
+			tlen = (xs)->sense_len;					\
+		PISP_PCMD(xs)->totslen = imin((xs)->sense_len, totslen);	\
+		PISP_PCMD(xs)->cumslen = tlen;					\
+		memcpy(&(xs)->sense_data, sense_ptr, tlen);			\
+		(xs)->sense_resid = (xs)->sense_len - tlen;			\
+		(xs)->ccb_h.status |= CAM_AUTOSNS_VALID;			\
+	} while (0)
 
+#define	XS_SENSE_APPEND(xs, xsnsp, xsnsl)	do {				\
+		uint32_t off = PISP_PCMD(xs)->cumslen;				\
+		uint8_t *ptr = &((uint8_t *)(&(xs)->sense_data))[off];		\
+		uint32_t amt = imin(xsnsl, PISP_PCMD(xs)->totslen - off);	\
+		if (amt) {							\
+			memcpy(ptr, xsnsp, amt);				\
+			(xs)->sense_resid -= amt;				\
+			PISP_PCMD(xs)->cumslen += amt;				\
+		}								\
+	} while (0)
+
 #define	XS_SENSE_VALID(xs)	(((xs)->ccb_h.status & CAM_AUTOSNS_VALID) != 0)
 
 #define	DEFAULT_FRAMESIZE(isp)		isp->isp_osinfo.framesize
@@ -634,25 +684,7 @@
 /*
  * Platform private flags
  */
-#define	ISP_SPRIV_ERRSET	0x1
-#define	ISP_SPRIV_TACTIVE	0x2
-#define	ISP_SPRIV_DONE		0x8
-#define	ISP_SPRIV_WPEND		0x10
 
-#define	XS_S_TACTIVE(sccb)	(sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_TACTIVE
-#define	XS_C_TACTIVE(sccb)	(sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_TACTIVE
-#define	XS_TACTIVE_P(sccb)	((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_TACTIVE)
-
-#define	XS_CMD_S_DONE(sccb)	(sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_DONE
-#define	XS_CMD_C_DONE(sccb)	(sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_DONE
-#define	XS_CMD_DONE_P(sccb)	((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_DONE)
-
-#define	XS_CMD_S_WPEND(sccb)	(sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_WPEND
-#define	XS_CMD_C_WPEND(sccb)	(sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_WPEND
-#define	XS_CMD_WPEND_P(sccb)	((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_WPEND)
-
-#define	XS_CMD_S_CLEAR(sccb)	(sccb)->ccb_h.spriv_field0 = 0
-
 /*
  * Platform Library Functions
  */
@@ -667,6 +699,7 @@
 int isp_mstohz(int);
 void isp_platform_intr(void *);
 void isp_common_dmateardown(ispsoftc_t *, struct ccb_scsiio *, uint32_t);
+int isp_fcp_next_crn(ispsoftc_t *, uint8_t *, XS_T *);
 
 /*
  * Platform Version specific defines

Modified: trunk/sys/dev/isp/isp_library.c
===================================================================
--- trunk/sys/dev/isp/isp_library.c	2016-09-15 08:25:48 UTC (rev 7985)
+++ trunk/sys/dev/isp/isp_library.c	2016-09-15 08:26:26 UTC (rev 7986)
@@ -58,7 +58,7 @@
  * Called with the first queue entry at least partially filled out.
  */
 int
-isp_send_cmd(ispsoftc_t *isp, void *fqe, void *segp, uint32_t nsegs, uint32_t totalcnt, isp_ddir_t ddir)
+isp_send_cmd(ispsoftc_t *isp, void *fqe, void *segp, uint32_t nsegs, uint32_t totalcnt, isp_ddir_t ddir, ispds64_t *ecmd)
 {
 	uint8_t storage[QENTRY_LEN];
 	uint8_t type, nqe;
@@ -396,11 +396,11 @@
 	}
 	if (fcp->isp_fwstate < FW_READY || fcp->isp_loopstate < LOOP_PDB_RCVD) {
 		if (isp_control(isp, ISPCTL_FCLINK_TEST, chan, tval) != 0) {
-			isp_prt(isp, ISP_LOGSANCFG, "isp_fc_runstate: linktest failed for channel %d", chan);
+			isp_prt(isp, ISP_LOG_SANCFG, "isp_fc_runstate: linktest failed for channel %d", chan);
 			return (-1);
 		}
 		if (fcp->isp_fwstate != FW_READY || fcp->isp_loopstate < LOOP_PDB_RCVD) {
-			isp_prt(isp, ISP_LOGSANCFG, "isp_fc_runstate: f/w not ready for channel %d", chan);
+			isp_prt(isp, ISP_LOG_SANCFG, "isp_fc_runstate: f/w not ready for channel %d", chan);
 			return (-1);
 		}
 	}
@@ -410,19 +410,19 @@
 	}
 
 	if (isp_control(isp, ISPCTL_SCAN_LOOP, chan) != 0) {
-		isp_prt(isp, ISP_LOGSANCFG, "isp_fc_runstate: scan loop fails on channel %d", chan);
+		isp_prt(isp, ISP_LOG_SANCFG, "isp_fc_runstate: scan loop fails on channel %d", chan);
 		return (LOOP_PDB_RCVD);
 	}
 	if (isp_control(isp, ISPCTL_SCAN_FABRIC, chan) != 0) {
-		isp_prt(isp, ISP_LOGSANCFG, "isp_fc_runstate: scan fabric fails on channel %d", chan);
+		isp_prt(isp, ISP_LOG_SANCFG, "isp_fc_runstate: scan fabric fails on channel %d", chan);
 		return (LOOP_LSCAN_DONE);
 	}
 	if (isp_control(isp, ISPCTL_PDB_SYNC, chan) != 0) {
-		isp_prt(isp, ISP_LOGSANCFG, "isp_fc_runstate: pdb_sync fails on channel %d", chan);
+		isp_prt(isp, ISP_LOG_SANCFG, "isp_fc_runstate: pdb_sync fails on channel %d", chan);
 		return (LOOP_FSCAN_DONE);
 	}
 	if (fcp->isp_fwstate != FW_READY || fcp->isp_loopstate != LOOP_READY) {
-		isp_prt(isp, ISP_LOGSANCFG, "isp_fc_runstate: f/w not ready again on channel %d", chan);
+		isp_prt(isp, ISP_LOG_SANCFG, "isp_fc_runstate: f/w not ready again on channel %d", chan);
 		return (-1);
 	}
 	return (0);
@@ -438,7 +438,7 @@
 	int i;
 
 	for (i = 0; i < MAX_FC_TARG; i++) {
-		char mb[4];
+		char mb[4], buf1[64], buf2[64];
 		const char *dbs[8] = {
 			"NIL ",
 			"PROB",
@@ -449,9 +449,6 @@
 			"ZOMB",
 			"VLD "
 		};
-		const char *roles[4] = {
-			" UNK", " TGT", " INI", "TINI"
-		};
 		fcportdb_t *lp = &fcp->portdb[i];
 
 		if (lp->state == FC_PORTDB_STATE_NIL && lp->target_mode == 0) {
@@ -462,12 +459,81 @@
 		} else {
 			ISP_SNPRINTF(mb, sizeof (mb), "---");
 		}
+		isp_gen_role_str(buf1, sizeof (buf1), lp->prli_word3);
+		isp_gen_role_str(buf2, sizeof (buf2), lp->new_prli_word3);
 		isp_prt(isp, ISP_LOGALL, "Chan %d [%d]: hdl 0x%x %s al%d tgt %s %s 0x%06x =>%s 0x%06x; WWNN 0x%08x%08x WWPN 0x%08x%08x",
-		    chan, i, lp->handle, dbs[lp->state], lp->autologin, mb, roles[lp->roles], lp->portid, roles[lp->new_roles], lp->new_portid,
+		    chan, i, lp->handle, dbs[lp->state], lp->autologin, mb, buf1, lp->portid, buf2, lp->new_portid,
 		    (uint32_t) (lp->node_wwn >> 32), (uint32_t) (lp->node_wwn), (uint32_t) (lp->port_wwn >> 32), (uint32_t) (lp->port_wwn));
 	}
 }
 
+void
+isp_gen_role_str(char *buf, size_t len, uint16_t p3)
+{
+	int nd = 0;
+	buf[0] = '(';
+	buf[1] = 0;
+	if (p3 & PRLI_WD3_ENHANCED_DISCOVERY) {
+		nd++;
+		strlcat(buf, "EDisc", len);
+	}
+	if (p3 & PRLI_WD3_REC_SUPPORT) {
+		if (nd++) {
+			strlcat(buf, ",", len);
+		}
+		strlcat(buf, "REC", len);
+	}
+	if (p3 & PRLI_WD3_TASK_RETRY_IDENTIFICATION_REQUESTED) {
+		if (nd++) {
+			strlcat(buf, ",", len);
+		}
+		strlcat(buf, "RetryID", len);
+	}
+	if (p3 & PRLI_WD3_RETRY) {
+		if (nd++) {
+			strlcat(buf, ",", len);
+		}
+		strlcat(buf, "Retry", len);
+	}
+	if (p3 & PRLI_WD3_CONFIRMED_COMPLETION_ALLOWED) {
+		if (nd++) {
+			strlcat(buf, ",", len);
+		}
+		strlcat(buf, "CNFRM", len);
+	}
+	if (p3 & PRLI_WD3_DATA_OVERLAY_ALLOWED) {
+		if (nd++) {
+			strlcat(buf, ",", len);
+		}
+		strlcat(buf, "DOver", len);
+	}
+	if (p3 & PRLI_WD3_INITIATOR_FUNCTION) {
+		if (nd++) {
+			strlcat(buf, ",", len);
+		}
+		strlcat(buf, "INI", len);
+	}
+	if (p3 & PRLI_WD3_TARGET_FUNCTION) {
+		if (nd++) {
+			strlcat(buf, ",", len);
+		}
+		strlcat(buf, "TGT", len);
+	}
+	if (p3 & PRLI_WD3_READ_FCP_XFER_RDY_DISABLED) {
+		if (nd++) {
+			strlcat(buf, ",", len);
+		}
+		strlcat(buf, "RdXfrDis", len);
+	}
+	if (p3 & PRLI_WD3_WRITE_FCP_XFER_RDY_DISABLED) {
+		if (nd++) {
+			strlcat(buf, ",", len);
+		}
+		strlcat(buf, "XfrDis", len);
+	}
+	strlcat(buf, ")", len);
+}
+
 const char *
 isp_fc_fw_statename(int state)
 {
@@ -535,7 +601,6 @@
 		isp_del_all_wwn_entries(isp, chan);
 #endif
 		isp_clear_commands(isp);
-
 		isp_reset(isp, 0);
 		if (isp->isp_state != ISP_RESETSTATE) {
 			isp_prt(isp, ISP_LOGERR, "%s: cannot reset card", __func__);
@@ -896,7 +961,8 @@
 	ISP_IOXPUT_8(isp, src->req_target, &dst->req_target);
 	ISP_IOXPUT_16(isp, src->req_scclun, &dst->req_scclun);
 	ISP_IOXPUT_16(isp, src->req_flags,  &dst->req_flags);
-	ISP_IOXPUT_16(isp, src->req_reserved, &dst->req_reserved);
+	ISP_IOXPUT_8(isp, src->req_crn, &dst->req_crn);
+	ISP_IOXPUT_8(isp, src->req_reserved, &dst->req_reserved);
 	ISP_IOXPUT_16(isp, src->req_time, &dst->req_time);
 	ISP_IOXPUT_16(isp, src->req_seg_count, &dst->req_seg_count);
 	for (i = 0; i < ASIZE(src->req_cdb); i++) {
@@ -919,7 +985,8 @@
 	ISP_IOXPUT_16(isp, src->req_target, &dst->req_target);
 	ISP_IOXPUT_16(isp, src->req_scclun, &dst->req_scclun);
 	ISP_IOXPUT_16(isp, src->req_flags,  &dst->req_flags);
-	ISP_IOXPUT_16(isp, src->req_reserved, &dst->req_reserved);
+	ISP_IOXPUT_8(isp, src->req_crn, &dst->req_crn);
+	ISP_IOXPUT_8(isp, src->req_reserved, &dst->req_reserved);
 	ISP_IOXPUT_16(isp, src->req_time, &dst->req_time);
 	ISP_IOXPUT_16(isp, src->req_seg_count, &dst->req_seg_count);
 	for (i = 0; i < ASIZE(src->req_cdb); i++) {
@@ -1077,15 +1144,34 @@
 	ISP_IOXGET_16(isp, &src->req_time, dst->req_time);
 	ISP_IOXGET_16(isp, &src->req_sense_len, dst->req_sense_len);
 	ISP_IOXGET_32(isp, &src->req_resid, dst->req_resid);
-	for (i = 0; i < 8; i++) {
+	for (i = 0; i < sizeof (src->req_response); i++) {
 		ISP_IOXGET_8(isp, &src->req_response[i], dst->req_response[i]);
 	}
-	for (i = 0; i < 32; i++) {
+	for (i = 0; i < sizeof (src->req_sense_data); i++) {
 		ISP_IOXGET_8(isp, &src->req_sense_data[i], dst->req_sense_data[i]);
 	}
 }
 
 void
+isp_get_cont_response(ispsoftc_t *isp, ispstatus_cont_t *src, ispstatus_cont_t *dst)
+{
+	int i;
+	isp_get_hdr(isp, &src->req_header, &dst->req_header);
+	if (IS_24XX(isp)) {
+		uint32_t *a, *b;
+		a = (uint32_t *) src->req_sense_data;
+		b = (uint32_t *) dst->req_sense_data;
+		for (i = 0; i < (sizeof (src->req_sense_data) / sizeof (uint32_t)); i++) {
+			ISP_IOZGET_32(isp, a++, *b++);
+		}
+	} else {
+		for (i = 0; i < sizeof (src->req_sense_data); i++) {
+			ISP_IOXGET_8(isp, &src->req_sense_data[i], dst->req_sense_data[i]);
+		}
+	}
+}
+
+void
 isp_get_24xx_response(ispsoftc_t *isp, isp24xx_statusreq_t *src, isp24xx_statusreq_t *dst)
 {
 	int i;
@@ -1929,6 +2015,29 @@
 }
 
 void
+isp_put_fc_hdr(ispsoftc_t *isp, fc_hdr_t *src, fc_hdr_t *dst)
+{
+        ISP_IOZPUT_8(isp, src->r_ctl, &dst->r_ctl);
+        ISP_IOZPUT_8(isp, src->d_id[0], &dst->d_id[0]);
+        ISP_IOZPUT_8(isp, src->d_id[1], &dst->d_id[1]);
+        ISP_IOZPUT_8(isp, src->d_id[2], &dst->d_id[2]);
+        ISP_IOZPUT_8(isp, src->cs_ctl, &dst->cs_ctl);
+        ISP_IOZPUT_8(isp, src->s_id[0], &dst->s_id[0]);
+        ISP_IOZPUT_8(isp, src->s_id[1], &dst->s_id[1]);
+        ISP_IOZPUT_8(isp, src->s_id[2], &dst->s_id[2]);
+        ISP_IOZPUT_8(isp, src->type, &dst->type);
+        ISP_IOZPUT_8(isp, src->f_ctl[0], &dst->f_ctl[0]);
+        ISP_IOZPUT_8(isp, src->f_ctl[1], &dst->f_ctl[1]);
+        ISP_IOZPUT_8(isp, src->f_ctl[2], &dst->f_ctl[2]);
+        ISP_IOZPUT_8(isp, src->seq_id, &dst->seq_id);
+        ISP_IOZPUT_8(isp, src->df_ctl, &dst->df_ctl);
+        ISP_IOZPUT_16(isp, src->seq_cnt, &dst->seq_cnt);
+        ISP_IOZPUT_16(isp, src->ox_id, &dst->ox_id);
+        ISP_IOZPUT_16(isp, src->rx_id, &dst->rx_id);
+        ISP_IOZPUT_32(isp, src->parameter, &dst->parameter);
+}
+
+void
 isp_get_fcp_cmnd_iu(ispsoftc_t *isp, fcp_cmnd_iu_t *src, fcp_cmnd_iu_t *dst)
 {
 	int i;
@@ -1998,6 +2107,21 @@
 	ISP_IOZPUT_8(isp, src->ct_vunique, &dst->ct_vunique);
 }
 
+void
+isp_put_fcp_rsp_iu(ispsoftc_t *isp, fcp_rsp_iu_t *src, fcp_rsp_iu_t *dst)
+{
+	int i;
+	for (i = 0; i < ((sizeof (src->fcp_rsp_reserved))/(sizeof (src->fcp_rsp_reserved[0]))); i++) {
+		ISP_IOZPUT_8(isp, src->fcp_rsp_reserved[i], &dst->fcp_rsp_reserved[i]);
+	}
+	ISP_IOZPUT_16(isp, src->fcp_rsp_status_qualifier, &dst->fcp_rsp_status_qualifier);
+	ISP_IOZPUT_8(isp, src->fcp_rsp_bits, &dst->fcp_rsp_bits);
+	ISP_IOZPUT_8(isp, src->fcp_rsp_scsi_status, &dst->fcp_rsp_scsi_status);
+	ISP_IOZPUT_32(isp, src->fcp_rsp_resid, &dst->fcp_rsp_resid);
+	ISP_IOZPUT_32(isp, src->fcp_rsp_snslen, &dst->fcp_rsp_snslen);
+	ISP_IOZPUT_32(isp, src->fcp_rsp_rsplen, &dst->fcp_rsp_rsplen);
+}
+
 #ifdef	ISP_TARGET_MODE
 
 /*
@@ -2004,17 +2128,22 @@
  * Command shipping- finish off first queue entry and do dma mapping and
  * additional segments as needed.
  *
- * Called with the first queue entry at least partially filled out.
+ * Called with the first queue entry mostly filled out.
+ * Our job here is to finish that and add additional data
+ * segments if needed.
+ *
+ * We used to do synthetic entries to split data and status
+ * at this level, but that started getting too tricky.
  */
 int
 isp_send_tgt_cmd(ispsoftc_t *isp, void *fqe, void *segp, uint32_t nsegs, uint32_t totalcnt, isp_ddir_t ddir, void *snsptr, uint32_t snslen)
 {
-	uint8_t storage[QENTRY_LEN], storage2[QENTRY_LEN];
+	uint8_t storage[QENTRY_LEN];
 	uint8_t type, nqe;
 	uint32_t seg, curseg, seglim, nxt, nxtnxt;
 	ispds_t *dsp = NULL;
 	ispds64_t *dsp64 = NULL;
-	void *qe0, *qe1, *sqe = NULL;
+	void *qe0, *qe1;
 
 	qe0 = isp_getrqentry(isp);
 	if (qe0 == NULL) {
@@ -2027,7 +2156,7 @@
 	seglim = 0;
 
 	/*
-	 * If we have no data to transmit, just copy the first IOCB and start it up.
+	 * If we have data to transmit, figure out how many segments can fit into the first entry.
 	 */
 	if (ddir != ISP_NOXFR) {
 		/*
@@ -2039,93 +2168,17 @@
 			seglim = ISP_RQDSEG;
 			break;
 		case RQSTYPE_CTIO2:
+			dsp = ((ct2_entry_t *)fqe)->rsp.m0.u.ct_dataseg;
+			seglim = ISP_RQDSEG_T2;
+			break;
 		case RQSTYPE_CTIO3:
-		{
-			ct2_entry_t *ct = fqe, *ct2 = (ct2_entry_t *) storage2;
-			uint16_t swd = ct->rsp.m0.ct_scsi_status & 0xff;
-
-			if ((ct->ct_flags & CT2_SENDSTATUS) && (swd || ct->ct_resid)) {
-				memcpy(ct2, ct, QENTRY_LEN);
-				/*
-				 * Clear fields from first CTIO2 that now need to be cleared
-				 */
-				ct->ct_header.rqs_seqno = 0;
-				ct->ct_flags &= ~(CT2_SENDSTATUS|CT2_CCINCR|CT2_FASTPOST);
-				ct->ct_resid = 0;
-				ct->ct_syshandle = 0;
-				ct->rsp.m0.ct_scsi_status = 0;
-
-				/*
-				 * Reset fields in the second CTIO2 as appropriate.
-				 */
-				ct2->ct_flags &= ~(CT2_FLAG_MMASK|CT2_DATAMASK|CT2_FASTPOST);
-				ct2->ct_flags |= CT2_NO_DATA|CT2_FLAG_MODE1;
-				ct2->ct_seg_count = 0;
-				ct2->ct_reloff = 0;
-				memset(&ct2->rsp, 0, sizeof (ct2->rsp));
-				if (swd == SCSI_CHECK && snsptr && snslen) {
-					ct2->rsp.m1.ct_senselen = min(snslen, MAXRESPLEN);
-					memcpy(ct2->rsp.m1.ct_resp, snsptr, ct2->rsp.m1.ct_senselen);
-					swd |= CT2_SNSLEN_VALID;
-				}
-				if (ct2->ct_resid > 0) {
-					swd |= CT2_DATA_UNDER;
-				} else if (ct2->ct_resid < 0) {
-					swd |= CT2_DATA_OVER;
-				}
-				ct2->rsp.m1.ct_scsi_status = swd;
-				sqe = storage2;
-			}
-			if (type == RQSTYPE_CTIO2) {
-				dsp = ct->rsp.m0.u.ct_dataseg;
-				seglim = ISP_RQDSEG_T2;
-			} else {
-				dsp64 = ct->rsp.m0.u.ct_dataseg64;
-				seglim = ISP_RQDSEG_T3;
-			}
+			dsp64 = ((ct2_entry_t *)fqe)->rsp.m0.u.ct_dataseg64;
+			seglim = ISP_RQDSEG_T3;
 			break;
-		}
 		case RQSTYPE_CTIO7:
-		{
-			ct7_entry_t *ct = fqe, *ct2 = (ct7_entry_t *)storage2;
-			uint16_t swd = ct->ct_scsi_status & 0xff;
-
-			dsp64 = &ct->rsp.m0.ds;
+			dsp64 = &((ct7_entry_t *)fqe)->rsp.m0.ds;
 			seglim = 1;
-			if ((ct->ct_flags & CT7_SENDSTATUS) && (swd || ct->ct_resid)) {
-				memcpy(ct2, ct, sizeof (ct7_entry_t));
-
-				/*
-				 * Clear fields from first CTIO7 that now need to be cleared
-				 */
-				ct->ct_header.rqs_seqno = 0;
-				ct->ct_flags &= ~CT7_SENDSTATUS;
-				ct->ct_resid = 0;
-				ct->ct_syshandle = 0;
-				ct->ct_scsi_status = 0;
-
-				/*
-				 * Reset fields in the second CTIO7 as appropriate.
-				 */
-				ct2->ct_flags &= ~(CT7_FLAG_MMASK|CT7_DATAMASK);
-				ct2->ct_flags |= CT7_NO_DATA|CT7_NO_DATA|CT7_FLAG_MODE1;
-				ct2->ct_seg_count = 0;
-				memset(&ct2->rsp, 0, sizeof (ct2->rsp));
-				if (swd == SCSI_CHECK && snsptr && snslen) {
-					ct2->rsp.m1.ct_resplen = min(snslen, MAXRESPLEN_24XX);
-					memcpy(ct2->rsp.m1.ct_resp, snsptr, ct2->rsp.m1.ct_resplen);
-					swd |= (FCP_SNSLEN_VALID << 8);
-				}
-				if (ct2->ct_resid < 0) {
-					swd |= (FCP_RESID_OVERFLOW << 8);
-				} else if (ct2->ct_resid > 0) {
-					swd |= (FCP_RESID_UNDERFLOW << 8);
-				}
-				ct2->ct_scsi_status = swd;
-				sqe = storage2;
-			}
 			break;
-		}
 		default:
 			return (CMD_COMPLETE);
 		}
@@ -2132,7 +2185,8 @@
 	}
 
 	/*
-	 * Fill out the data transfer stuff in the first queue entry
+	 * First, fill out any of the data transfer stuff that fits
+	 * in the first queue entry.
 	 */
 	if (seglim > nsegs) {
 		seglim = nsegs;
@@ -2147,12 +2201,6 @@
 	}
 
 	/*
-	 * First, if we are sending status with data and we have a non-zero
-	 * status or non-zero residual, we have to make a synthetic extra CTIO
-	 * that contains the status that we'll ship separately (FC cards only).
-	 */
-
-	/*
 	 * Second, start building additional continuation segments as needed.
 	 */
 	while (seg < nsegs) {
@@ -2198,25 +2246,10 @@
         }
 
 	/*
-	 * If we have a synthetic queue entry to complete things, do it here.
+	 * Third, not patch up the first queue entry with the number of segments
+	 * we actually are going to be transmitting. At the same time, handle
+	 * any mode 2 requests.
 	 */
-	if (sqe) {
-		nxtnxt = ISP_NXT_QENTRY(nxt, RQUEST_QUEUE_LEN(isp));
-		if (nxtnxt == isp->isp_reqodx) {
-			return (CMD_EAGAIN);
-		}
-		qe1 = ISP_QUEUE_ENTRY(isp->isp_rquest, nxt);
-		nxt = nxtnxt;
-		if (type == RQSTYPE_CTIO7) {
-			isp_put_ctio7(isp, sqe, qe1);
-		} else {
-			isp_put_ctio2(isp, sqe, qe1);
-		}
-		if (isp->isp_dblev & ISP_LOGTDEBUG1) {
-			isp_print_bytes(isp, "synthetic final queue entry", QENTRY_LEN, storage2);
-		}
-	}
-
 	((isphdr_t *)fqe)->rqs_entry_count = nqe;
 	switch (type) {
 	case RQSTYPE_CTIO:
@@ -2225,7 +2258,11 @@
 		break;
 	case RQSTYPE_CTIO2:
 	case RQSTYPE_CTIO3:
-		((ct2_entry_t *)fqe)->ct_seg_count = nsegs;
+		if (((ct2_entry_t *)fqe)->ct_flags & CT2_FLAG_MODE2) {
+			((ct2_entry_t *)fqe)->ct_seg_count = 1;
+		} else {
+			((ct2_entry_t *)fqe)->ct_seg_count = nsegs;
+		}
 		if (ISP_CAP_2KLOGIN(isp)) {
 			isp_put_ctio2e(isp, fqe, qe0);
 		} else {
@@ -2233,7 +2270,11 @@
 		}
 		break;
 	case RQSTYPE_CTIO7:
-		((ct7_entry_t *)fqe)->ct_seg_count = nsegs;
+		if (((ct7_entry_t *)fqe)->ct_flags & CT7_FLAG_MODE2) {
+			((ct7_entry_t *)fqe)->ct_seg_count = 1;
+		} else {
+			((ct7_entry_t *)fqe)->ct_seg_count = nsegs;
+		}
 		isp_put_ctio7(isp, fqe, qe0);
 		break;
 	default:
@@ -2405,8 +2446,9 @@
  * Add an initiator device to the port database
  */
 void
-isp_add_wwn_entry(ispsoftc_t *isp, int chan, uint64_t ini, uint16_t nphdl, uint32_t s_id)
+isp_add_wwn_entry(ispsoftc_t *isp, int chan, uint64_t ini, uint16_t nphdl, uint32_t s_id, uint16_t prli_params)
 {
+	char buf[64];
 	fcparam *fcp;
 	fcportdb_t *lp;
 	isp_notify_t nt;
@@ -2415,8 +2457,8 @@
 	fcp = FCPARAM(isp, chan);
 
 	if (nphdl >= MAX_NPORT_HANDLE) {
-		isp_prt(isp, ISP_LOGWARN, "%s: Chan %d IID 0x%016llx bad N-Port handle 0x%04x Port ID 0x%06x",
-		    __func__, chan, (unsigned long long) ini, nphdl, s_id);
+		isp_prt(isp, ISP_LOGWARN, "Chan %d IID 0x%016llx bad N-Port handle 0x%04x Port ID 0x%06x",
+		    chan, (unsigned long long) ini, nphdl, s_id);
 		return;
 	}
 
@@ -2451,8 +2493,8 @@
 	if (lp) {
 		int something = 0;
 		if (lp->handle != nphdl) {
-			isp_prt(isp, ISP_LOGWARN, "%s: Chan %d attempt to re-enter N-port handle 0x%04x IID 0x%016llx Port ID 0x%06x finds IID 0x%016llx N-Port Handle 0x%04x Port ID 0x%06x",
-			    __func__, chan, nphdl, (unsigned long long)ini, s_id, (unsigned long long) lp->port_wwn, lp->handle, lp->portid);
+			isp_prt(isp, ISP_LOGWARN, "Chan %d attempt to re-enter N-port handle 0x%04x IID 0x%016llx Port ID 0x%06x finds IID 0x%016llx N-Port Handle 0x%04x Port ID 0x%06x",
+			    chan, nphdl, (unsigned long long)ini, s_id, (unsigned long long) lp->port_wwn, lp->handle, lp->portid);
 			isp_dump_portdb(isp, chan);
 			return;
 		}
@@ -2459,11 +2501,10 @@
 		if (s_id != PORT_NONE) {
 			if (lp->portid == PORT_NONE) {
 				lp->portid = s_id;
-				isp_prt(isp, ISP_LOGTINFO, "%s: Chan %d N-port handle 0x%04x gets Port ID 0x%06x", __func__, chan, nphdl, s_id);
+				isp_prt(isp, ISP_LOGTINFO, "Chan %d N-port handle 0x%04x gets Port ID 0x%06x", chan, nphdl, s_id);
 				something++;
 			} else if (lp->portid != s_id) {
-				isp_prt(isp, ISP_LOGTINFO, "%s: Chan %d N-port handle 0x%04x tries to change Port ID 0x%06x to 0x%06x", __func__, chan, nphdl,
-				    lp->portid, s_id);
+				isp_prt(isp, ISP_LOGTINFO, "Chan %d N-port handle 0x%04x tries to change Port ID 0x%06x to 0x%06x", chan, nphdl, lp->portid, s_id);
 				isp_dump_portdb(isp, chan);
 				return;
 			}
@@ -2471,18 +2512,24 @@
 		if (VALID_INI(ini)) {
 			if (!VALID_INI(lp->port_wwn)) {
 				lp->port_wwn = ini;
-				isp_prt(isp, ISP_LOGTINFO, "%s: Chan %d N-port handle 0x%04x gets WWN 0x%016llxx", __func__, chan, nphdl, (unsigned long long) ini);
+				isp_prt(isp, ISP_LOGTINFO, "Chan %d N-port handle 0x%04x gets WWN 0x%016llxx", chan, nphdl, (unsigned long long) ini);
 				something++;
 			} else if (lp->port_wwn != ini) {
-				isp_prt(isp, ISP_LOGWARN, "%s: Chan %d N-port handle 0x%04x tries to change WWN 0x%016llx to 0x%016llx", __func__, chan, nphdl,
+				isp_prt(isp, ISP_LOGWARN, "Chan %d N-port handle 0x%04x tries to change WWN 0x%016llx to 0x%016llx", chan, nphdl,
 				    (unsigned long long) lp->port_wwn, (unsigned long long) ini);
 				isp_dump_portdb(isp, chan);
 				return;
 			}
 		}
-
+		if (prli_params != lp->prli_word3) {
+			lp->prli_word3 = prli_params;
+			isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
+			isp_prt(isp, ISP_LOGTINFO|ISP_LOGCONFIG, "Chan %d IID 0x%016llx N-Port Handle 0x%04x Port ID 0x%06x new PRLI Word 3 params %s", chan,
+			    (unsigned long long) lp->port_wwn, lp->handle, lp->portid, buf);
+			something++;
+		}
 		if (!something) {
-			isp_prt(isp, ISP_LOGWARN, "%s: Chan %d IID 0x%016llx N-Port Handle 0x%04x Port ID 0x%06x reentered", __func__, chan,
+			isp_prt(isp, ISP_LOGWARN, "Chan %d IID 0x%016llx N-Port Handle 0x%04x Port ID 0x%06x reentered", chan,
 			    (unsigned long long) lp->port_wwn, lp->handle, lp->portid);
 		}
 		return;
@@ -2500,8 +2547,8 @@
 		}
 	}
 	if (i < 0) {
-		isp_prt(isp, ISP_LOGWARN, "%s: Chan %d IID 0x%016llx N-Port Handle 0x%04x Port ID 0x%06x- no room in port database",
-		    __func__, chan, (unsigned long long) ini, nphdl, s_id);
+		isp_prt(isp, ISP_LOGWARN, "Chan %d IID 0x%016llx N-Port Handle 0x%04x Port ID 0x%06x- no room in port database",
+		    chan, (unsigned long long) ini, nphdl, s_id);
 		return;
 	}
 
@@ -2511,9 +2558,12 @@
 	lp->handle = nphdl;
 	lp->portid = s_id;
 	lp->port_wwn = ini;
+	lp->prli_word3 = prli_params;
+	isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
 	fcp->isp_tgt_map[nphdl] = i + 1;
 
-	isp_prt(isp, ISP_LOGTINFO, "%s: Chan %d IID 0x%016llx N-Port Handle 0x%04x Port ID 0x%06x vtgt %d added", __func__, chan, (unsigned long long) ini, nphdl, s_id, fcp->isp_tgt_map[nphdl] - 1);
+	isp_prt(isp, ISP_LOGTINFO, "Chan %d IID 0x%016llx N-Port Handle 0x%04x Port ID 0x%06x vtgt %d %s added", chan,
+	    (unsigned long long) ini, nphdl, s_id, fcp->isp_tgt_map[nphdl] - 1, buf);
 
 	ISP_MEMZERO(&nt, sizeof (nt));
 	nt.nt_hba = isp;
@@ -2538,8 +2588,8 @@
 	fcportdb_t *lp;
 
 	if (nphdl >= MAX_NPORT_HANDLE) {
-		isp_prt(isp, ISP_LOGWARN, "%s: Chan %d IID 0x%016llx bad N-Port handle 0x%04x Port ID 0x%06x",
-		    __func__, chan, (unsigned long long) ini, nphdl, s_id);
+		isp_prt(isp, ISP_LOGWARN, "Chan %d IID 0x%016llx bad N-Port handle 0x%04x Port ID 0x%06x",
+		    chan, (unsigned long long) ini, nphdl, s_id);
 		return;
 	}
 
@@ -2553,13 +2603,13 @@
 		}
 	}
 	if (lp == NULL) {
-		isp_prt(isp, ISP_LOGWARN, "%s: Chan %d IID 0x%016llx N-Port Handle 0x%04x Port ID 0x%06x cannot be found to be cleared",
-		    __func__, chan, (unsigned long long) ini, nphdl, s_id);
+		isp_prt(isp, ISP_LOGWARN, "Chan %d IID 0x%016llx N-Port Handle 0x%04x Port ID 0x%06x cannot be found to be cleared",
+		    chan, (unsigned long long) ini, nphdl, s_id);
 		isp_dump_portdb(isp, chan);
 		return;
 	}
-	isp_prt(isp, ISP_LOGTINFO, "%s: Chan %d IID 0x%016llx N-Port Handle 0x%04x Port ID 0x%06x vtgt %d cleared",
-	    __func__, chan, (unsigned long long) lp->port_wwn, nphdl, lp->portid, fcp->isp_tgt_map[nphdl] - 1);
+	isp_prt(isp, ISP_LOGTINFO, "Chan %d IID 0x%016llx N-Port Handle 0x%04x Port ID 0x%06x vtgt %d cleared",
+	    chan, (unsigned long long) lp->port_wwn, nphdl, lp->portid, fcp->isp_tgt_map[nphdl] - 1);
 	fcp->isp_tgt_map[nphdl] = 0;
 
 	ISP_MEMZERO(&nt, sizeof (nt));
@@ -2650,7 +2700,7 @@
 			return;
 		}
 	}
-	isp_prt(isp, ISP_LOGWARN, "%s: Chan %d unable to find entry to delete N-port handle 0x%04x initiator WWN 0x%016llx Port ID 0x%06x", __func__,
+	isp_prt(isp, ISP_LOGWARN, "Chan %d unable to find entry to delete N-port handle 0x%04x initiator WWN 0x%016llx Port ID 0x%06x",
 	    mp->nt_channel, mp->nt_nphdl, (unsigned long long) mp->nt_wwn, mp->nt_sid);
 }
 
@@ -2973,8 +3023,14 @@
 		ISP_IOXPUT_16(isp, src->rsp.m2._reserved2, &dst->rsp.m2._reserved2);
 		ISP_IOXPUT_16(isp, src->rsp.m2._reserved3, &dst->rsp.m2._reserved3);
 		ISP_IOXPUT_32(isp, src->rsp.m2.ct_datalen, &dst->rsp.m2.ct_datalen);
-		ISP_IOXPUT_32(isp, src->rsp.m2.ct_fcp_rsp_iudata.ds_base, &dst->rsp.m2.ct_fcp_rsp_iudata.ds_base);
-		ISP_IOXPUT_32(isp, src->rsp.m2.ct_fcp_rsp_iudata.ds_count, &dst->rsp.m2.ct_fcp_rsp_iudata.ds_count);
+		if (src->ct_header.rqs_entry_type == RQSTYPE_CTIO2) {
+			ISP_IOXPUT_32(isp, src->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_base, &dst->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_base);
+			ISP_IOXPUT_32(isp, src->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_count, &dst->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_count);
+		} else {
+			ISP_IOXPUT_32(isp, src->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_base, &dst->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_base);
+			ISP_IOXPUT_32(isp, src->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_basehi, &dst->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_basehi);
+			ISP_IOXPUT_32(isp, src->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_count, &dst->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_count);
+		}
 	}
 }
 
@@ -3026,8 +3082,14 @@
 		ISP_IOXPUT_16(isp, src->rsp.m2._reserved2, &dst->rsp.m2._reserved2);
 		ISP_IOXPUT_16(isp, src->rsp.m2._reserved3, &dst->rsp.m2._reserved3);
 		ISP_IOXPUT_32(isp, src->rsp.m2.ct_datalen, &dst->rsp.m2.ct_datalen);
-		ISP_IOXPUT_32(isp, src->rsp.m2.ct_fcp_rsp_iudata.ds_base, &dst->rsp.m2.ct_fcp_rsp_iudata.ds_base);
-		ISP_IOXPUT_32(isp, src->rsp.m2.ct_fcp_rsp_iudata.ds_count, &dst->rsp.m2.ct_fcp_rsp_iudata.ds_count);
+		if (src->ct_header.rqs_entry_type == RQSTYPE_CTIO2) {
+			ISP_IOXPUT_32(isp, src->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_base, &dst->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_base);
+			ISP_IOXPUT_32(isp, src->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_count, &dst->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_count);
+		} else {
+			ISP_IOXPUT_32(isp, src->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_base, &dst->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_base);
+			ISP_IOXPUT_32(isp, src->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_basehi, &dst->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_basehi);
+			ISP_IOXPUT_32(isp, src->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_count, &dst->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_count);
+		}
 	}
 }
 
@@ -3072,8 +3134,9 @@
 		}
 	} else {
 		ISP_IOXPUT_32(isp, src->rsp.m2.reserved0, &dst->rsp.m2.reserved0);
+		ISP_IOXPUT_32(isp, src->rsp.m2.reserved1, &dst->rsp.m2.reserved1);
 		ISP_IOXPUT_32(isp, src->rsp.m2.ct_datalen, &dst->rsp.m2.ct_datalen);
-		ISP_IOXPUT_32(isp, src->rsp.m2.reserved1, &dst->rsp.m2.reserved1);
+		ISP_IOXPUT_32(isp, src->rsp.m2.reserved2, &dst->rsp.m2.reserved2);
 		ISP_IOXPUT_32(isp, src->rsp.m2.ct_fcp_rsp_iudata.ds_base, &dst->rsp.m2.ct_fcp_rsp_iudata.ds_base);
 		ISP_IOXPUT_32(isp, src->rsp.m2.ct_fcp_rsp_iudata.ds_basehi, &dst->rsp.m2.ct_fcp_rsp_iudata.ds_basehi);
 		ISP_IOXPUT_32(isp, src->rsp.m2.ct_fcp_rsp_iudata.ds_count, &dst->rsp.m2.ct_fcp_rsp_iudata.ds_count);
@@ -3132,8 +3195,14 @@
 		ISP_IOXGET_16(isp, &src->rsp.m2._reserved2, dst->rsp.m2._reserved2);
 		ISP_IOXGET_16(isp, &src->rsp.m2._reserved3, dst->rsp.m2._reserved3);
 		ISP_IOXGET_32(isp, &src->rsp.m2.ct_datalen, dst->rsp.m2.ct_datalen);
-		ISP_IOXGET_32(isp, &src->rsp.m2.ct_fcp_rsp_iudata.ds_base, dst->rsp.m2.ct_fcp_rsp_iudata.ds_base);
-		ISP_IOXGET_32(isp, &src->rsp.m2.ct_fcp_rsp_iudata.ds_count, dst->rsp.m2.ct_fcp_rsp_iudata.ds_count);
+		if (src->ct_header.rqs_entry_type == RQSTYPE_CTIO2) {
+			ISP_IOXGET_32(isp, &src->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_base, dst->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_base);
+			ISP_IOXGET_32(isp, &src->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_count, dst->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_count);
+		} else {
+			ISP_IOXGET_32(isp, &src->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_base, dst->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_base);
+			ISP_IOXGET_32(isp, &src->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_basehi, dst->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_basehi);
+			ISP_IOXGET_32(isp, &src->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_count, dst->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_count);
+		}
 	}
 }
 
@@ -3187,8 +3256,14 @@
 		ISP_IOXGET_16(isp, &src->rsp.m2._reserved2, dst->rsp.m2._reserved2);
 		ISP_IOXGET_16(isp, &src->rsp.m2._reserved3, dst->rsp.m2._reserved3);
 		ISP_IOXGET_32(isp, &src->rsp.m2.ct_datalen, dst->rsp.m2.ct_datalen);
-		ISP_IOXGET_32(isp, &src->rsp.m2.ct_fcp_rsp_iudata.ds_base, dst->rsp.m2.ct_fcp_rsp_iudata.ds_base);
-		ISP_IOXGET_32(isp, &src->rsp.m2.ct_fcp_rsp_iudata.ds_count, dst->rsp.m2.ct_fcp_rsp_iudata.ds_count);
+		if (src->ct_header.rqs_entry_type == RQSTYPE_CTIO2) {
+			ISP_IOXGET_32(isp, &src->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_base, dst->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_base);
+			ISP_IOXGET_32(isp, &src->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_count, dst->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_count);
+		} else {
+			ISP_IOXGET_32(isp, &src->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_base, dst->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_base);
+			ISP_IOXGET_32(isp, &src->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_basehi, dst->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_basehi);
+			ISP_IOXGET_32(isp, &src->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_count, dst->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_count);
+		}
 	}
 }
 

Modified: trunk/sys/dev/isp/isp_library.h
===================================================================
--- trunk/sys/dev/isp/isp_library.h	2016-09-15 08:25:48 UTC (rev 7985)
+++ trunk/sys/dev/isp/isp_library.h	2016-09-15 08:26:26 UTC (rev 7986)
@@ -36,7 +36,7 @@
  * stuff figured out, you can make all the code in one spot.
  */
 typedef enum { ISP_TO_DEVICE, ISP_FROM_DEVICE, ISP_NOXFR} isp_ddir_t;
-int isp_send_cmd(ispsoftc_t *, void *, void *, uint32_t, uint32_t, isp_ddir_t);
+int isp_send_cmd(ispsoftc_t *, void *, void *, uint32_t, uint32_t, isp_ddir_t, ispds64_t *);
 
 /*
  * Handle management functions.
@@ -66,6 +66,7 @@
 extern const char *isp_class3_roles[4];
 int isp_fc_runstate(ispsoftc_t *, int, int);
 void isp_dump_portdb(ispsoftc_t *, int);
+void isp_gen_role_str(char *, size_t, uint16_t);
 
 const char *isp_fc_fw_statename(int);
 const char *isp_fc_loop_statename(int);
@@ -106,6 +107,7 @@
 void isp_put_cont_req(ispsoftc_t *, ispcontreq_t *, ispcontreq_t *);
 void isp_put_cont64_req(ispsoftc_t *, ispcontreq64_t *, ispcontreq64_t *);
 void isp_get_response(ispsoftc_t *, ispstatusreq_t *, ispstatusreq_t *);
+void isp_get_cont_response(ispsoftc_t *, ispstatus_cont_t *, ispstatus_cont_t *);
 void isp_get_24xx_response(ispsoftc_t *, isp24xx_statusreq_t *, isp24xx_statusreq_t *);
 void isp_get_24xx_abrt(ispsoftc_t *, isp24xx_abrt_t *, isp24xx_abrt_t *);
 void isp_get_rio1(ispsoftc_t *, isp_rio1_t *, isp_rio1_t *);
@@ -139,10 +141,12 @@
 void isp_get_els(ispsoftc_t *, els_t *, els_t *);
 void isp_put_els(ispsoftc_t *, els_t *, els_t *);
 void isp_get_fc_hdr(ispsoftc_t *, fc_hdr_t *, fc_hdr_t *);
+void isp_put_fc_hdr(ispsoftc_t *, fc_hdr_t *, fc_hdr_t *);
 void isp_get_fcp_cmnd_iu(ispsoftc_t *, fcp_cmnd_iu_t *, fcp_cmnd_iu_t *);
 void isp_put_rft_id(ispsoftc_t *, rft_id_t *, rft_id_t *);
 void isp_get_ct_hdr(ispsoftc_t *isp, ct_hdr_t *, ct_hdr_t *);
 void isp_put_ct_hdr(ispsoftc_t *isp, ct_hdr_t *, ct_hdr_t *);
+void isp_put_fcp_rsp_iu(ispsoftc_t *isp, fcp_rsp_iu_t *, fcp_rsp_iu_t *);
 
 #define ISP_HANDLE_MASK  0x7fff
 
@@ -166,7 +170,7 @@
 int isp_find_pdb_by_loopid(ispsoftc_t *, int, uint32_t, fcportdb_t **);
 int isp_find_pdb_by_sid(ispsoftc_t *, int, uint32_t, fcportdb_t **);
 void isp_find_chan_by_did(ispsoftc_t *, uint32_t, uint16_t *);
-void isp_add_wwn_entry(ispsoftc_t *, int, uint64_t, uint16_t, uint32_t);
+void isp_add_wwn_entry(ispsoftc_t *, int, uint64_t, uint16_t, uint32_t, uint16_t);
 void isp_del_wwn_entry(ispsoftc_t *, int, uint64_t, uint16_t, uint32_t);
 void isp_del_all_wwn_entries(ispsoftc_t *, int);
 void isp_del_wwn_entries(ispsoftc_t *, isp_notify_t *);

Modified: trunk/sys/dev/isp/isp_pci.c
===================================================================
--- trunk/sys/dev/isp/isp_pci.c	2016-09-15 08:25:48 UTC (rev 7985)
+++ trunk/sys/dev/isp/isp_pci.c	2016-09-15 08:26:26 UTC (rev 7986)
@@ -588,6 +588,20 @@
 		}
 	}
 
+	tval = 0;
+	(void) resource_int_value(device_get_name(dev), device_get_unit(dev), "nofctape", &tval);
+	if (tval) {
+		isp->isp_confopts |= ISP_CFG_NOFCTAPE;
+	}
+
+	tval = 0;
+	(void) resource_int_value(device_get_name(dev), device_get_unit(dev), "fctape", &tval);
+	if (tval) {
+		isp->isp_confopts &= ~ISP_CFG_NOFCTAPE;
+		isp->isp_confopts |= ISP_CFG_FCTAPE;
+	}
+
+
 	/*
 	 * Because the resource_*_value functions can neither return
 	 * 64 bit integer values, nor can they be directly coerced
@@ -664,6 +678,8 @@
 	isp = &pcs->pci_isp;
 	isp->isp_dev = dev;
 	isp->isp_nchan = 1;
+	if (sizeof (bus_addr_t) > 4)
+		isp->isp_osinfo.sixtyfourbit = 1;
 
 	/*
 	 * Get Generic Options
@@ -1028,6 +1044,9 @@
 		pci_release_msi(dev);
 	}
 	(void) bus_release_resource(dev, pcs->rtp, pcs->rgd, pcs->regs);
+	/*
+	 * XXX: THERE IS A LOT OF LEAKAGE HERE
+	 */
 	if (pcs->pci_isp.isp_param) {
 		free(pcs->pci_isp.isp_param, M_DEVBUF);
 		pcs->pci_isp.isp_param = NULL;
@@ -1441,6 +1460,7 @@
 imc(void *arg, bus_dma_segment_t *segs, int nseg, int error)
 {
 	struct imush *imushp = (struct imush *) arg;
+	isp_ecmd_t *ecmd;
 
 	if (error) {
 		imushp->error = error;
@@ -1451,17 +1471,31 @@
 		return;
 	}
 	isp_prt(imushp->isp, ISP_LOGDEBUG0, "request/result area @ 0x%jx/0x%jx", (uintmax_t) segs->ds_addr, (uintmax_t) segs->ds_len);
+
 	imushp->isp->isp_rquest = imushp->vbase;
 	imushp->isp->isp_rquest_dma = segs->ds_addr;
 	segs->ds_addr += ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(imushp->isp));
 	imushp->vbase += ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(imushp->isp));
+
 	imushp->isp->isp_result_dma = segs->ds_addr;
 	imushp->isp->isp_result = imushp->vbase;
+	segs->ds_addr += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(imushp->isp));
+	imushp->vbase += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(imushp->isp));
 
+	imushp->isp->isp_osinfo.ecmd_dma = segs->ds_addr;
+	imushp->isp->isp_osinfo.ecmd_free = (isp_ecmd_t *)imushp->vbase;
+	imushp->isp->isp_osinfo.ecmd_base = imushp->isp->isp_osinfo.ecmd_free;
+	for (ecmd = imushp->isp->isp_osinfo.ecmd_free; ecmd < &imushp->isp->isp_osinfo.ecmd_free[N_XCMDS]; ecmd++) {
+		if (ecmd == &imushp->isp->isp_osinfo.ecmd_free[N_XCMDS - 1]) {
+			ecmd->next = NULL;
+		} else {
+			ecmd->next = ecmd + 1;
+		}
+	}
 #ifdef	ISP_TARGET_MODE
+	segs->ds_addr += (N_XCMDS * XCMD_SIZE);
+	imushp->vbase += (N_XCMDS * XCMD_SIZE);
 	if (IS_24XX(imushp->isp)) {
-		segs->ds_addr += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(imushp->isp));
-		imushp->vbase += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(imushp->isp));
 		imushp->isp->isp_atioq_dma = segs->ds_addr;
 		imushp->isp->isp_atioq = imushp->vbase;
 	}
@@ -1531,11 +1565,11 @@
 		return (1);
 	}
 
+#ifdef	ISP_TARGET_MODE
 	/*
 	 * XXX: We don't really support 64 bit target mode for parallel scsi yet
 	 */
-#ifdef	ISP_TARGET_MODE
-	if (IS_SCSI(isp) && sizeof (bus_addr_t) > 4) {
+	if (IS_SCSI(isp) && isp->isp_osinfo.sixtyfourbit) {
 		free(isp->isp_osinfo.pcmd_pool, M_DEVBUF);
 		isp_prt(isp, ISP_LOGERR, "we cannot do DAC for SPI cards yet");
 		ISP_LOCK(isp);
@@ -1580,7 +1614,9 @@
 
 	/*
 	 * Allocate and map the request and result queues (and ATIO queue
-	 * if we're a 2400 supporting target mode).
+	 * if we're a 2400 supporting target mode), and a region for
+	 * external dma addressable command/status structures (23XX and
+	 * later).
 	 */
 	len = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
 	len += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
@@ -1589,7 +1625,9 @@
 		len += ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
 	}
 #endif
-
+	if (isp->isp_type >= ISP_HA_FC_2300) {
+		len += (N_XCMDS * XCMD_SIZE);
+	}
 	ns = (len / PAGE_SIZE) + 1;
 
 	/*
@@ -1650,6 +1688,19 @@
 				bus_dma_tag_destroy(fc->tdmat);
 				goto bad;
 			}
+			for (i = 0; i < INITIAL_NEXUS_COUNT; i++) {
+				struct isp_nexus *n = malloc(sizeof (struct isp_nexus), M_DEVBUF, M_NOWAIT | M_ZERO);
+				if (n == NULL) {
+					while (fc->nexus_free_list) {
+						n = fc->nexus_free_list;
+						fc->nexus_free_list = n->next;
+						free(n, M_DEVBUF);
+					}
+					goto bad;
+				}
+				n->next = fc->nexus_free_list;
+				fc->nexus_free_list = n;
+			}
 		}
 	}
 
@@ -1679,6 +1730,11 @@
 		struct isp_fc *fc = ISP_FC_PC(isp, cmap);
 		bus_dmamem_free(fc->tdmat, base, fc->tdmap);
 		bus_dma_tag_destroy(fc->tdmat);
+		while (fc->nexus_free_list) {
+			struct isp_nexus *n = fc->nexus_free_list;
+			fc->nexus_free_list = n->next;
+			free(n, M_DEVBUF);
+		}
 	}
 	bus_dmamem_free(isp->isp_osinfo.cdmat, base, isp->isp_osinfo.cdmap);
 	bus_dma_tag_destroy(isp->isp_osinfo.cdmat);
@@ -1733,7 +1789,7 @@
 	isp = mp->isp;
 	rq = mp->rq;
 	if (nseg) {
-		if (sizeof (bus_addr_t) > 4) {
+		if (isp->isp_osinfo.sixtyfourbit) {
 			if (nseg >= ISP_NSEG64_MAX) {
 				isp_prt(isp, ISP_LOGERR, "number of segments (%d) exceed maximum we can support (%d)", nseg, ISP_NSEG64_MAX);
 				mp->error = EFAULT;
@@ -1766,8 +1822,14 @@
 		ddir = ISP_NOXFR;
 	}
 
-	if (isp_send_tgt_cmd(isp, rq, dm_segs, nseg, XS_XFRLEN(csio), ddir, &csio->sense_data, csio->sense_len) != CMD_QUEUED) {
+	error = isp_send_tgt_cmd(isp, rq, dm_segs, nseg, XS_XFRLEN(csio), ddir, &csio->sense_data, csio->sense_len);
+	switch (error) {
+	case CMD_EAGAIN:
 		mp->error = MUSHERR_NOQENTRIES;
+	case CMD_QUEUED:
+		break;
+	default:
+		mp->error = EIO;
 	}
 }
 #endif
@@ -1802,7 +1864,7 @@
 	isp = mp->isp;
 	rq = mp->rq;
 	if (nseg) {
-		if (sizeof (bus_addr_t) > 4) {
+		if (isp->isp_osinfo.sixtyfourbit) {
 			if (nseg >= ISP_NSEG64_MAX) {
 				isp_prt(isp, ISP_LOGERR, "number of segments (%d) exceed maximum we can support (%d)", nseg, ISP_NSEG64_MAX);
 				mp->error = EFAULT;
@@ -1835,8 +1897,16 @@
 		ddir = ISP_NOXFR;
 	}
 
-	if (isp_send_cmd(isp, rq, dm_segs, nseg, XS_XFRLEN(csio), ddir) != CMD_QUEUED) {
+	error = isp_send_cmd(isp, rq, dm_segs, nseg, XS_XFRLEN(csio), ddir, (ispds64_t *)csio->req_map);
+	switch (error) {
+	case CMD_EAGAIN:
 		mp->error = MUSHERR_NOQENTRIES;
+		break;
+	case CMD_QUEUED:
+		break;
+	default:
+		mp->error = EIO;
+		break;
 	}
 }
 
@@ -1936,11 +2006,11 @@
 		if (mp->error == MUSHERR_NOQENTRIES) {
 			retval = CMD_EAGAIN;
 		} else if (mp->error == EFBIG) {
-			XS_SETERR(csio, CAM_REQ_TOO_BIG);
+			csio->ccb_h.status = CAM_REQ_TOO_BIG;
 		} else if (mp->error == EINVAL) {
-			XS_SETERR(csio, CAM_REQ_INVALID);
+			csio->ccb_h.status = CAM_REQ_INVALID;
 		} else {
-			XS_SETERR(csio, CAM_UNREC_HBA_ERROR);
+			csio->ccb_h.status = CAM_UNREC_HBA_ERROR;
 		}
 		return (retval);
 	}

Modified: trunk/sys/dev/isp/isp_stds.h
===================================================================
--- trunk/sys/dev/isp/isp_stds.h	2016-09-15 08:25:48 UTC (rev 7985)
+++ trunk/sys/dev/isp/isp_stds.h	2016-09-15 08:26:26 UTC (rev 7986)
@@ -137,9 +137,30 @@
 } rft_id_t;
 
 /*
- * FCP Response IU Bits of interest
- * Source: NCITS T10, Project 1144D, Revision 08 (aka FCP2r08)
+ * FCP Response IU and bits of interest
+ * Source: NCITS T10, Project 1828D, Revision 02b (aka FCP4r02b)
  */
+typedef struct {
+	uint8_t		fcp_rsp_reserved[8];
+	uint16_t	fcp_rsp_status_qualifier;	/* SAM-5 Status Qualifier */
+	uint8_t		fcp_rsp_bits;
+	uint8_t		fcp_rsp_scsi_status;		/* SAM-5 SCSI Status Byte */
+	uint32_t	fcp_rsp_resid;
+	uint32_t	fcp_rsp_snslen;
+	uint32_t	fcp_rsp_rsplen;
+	/*
+	 * In the bytes that follow, it's going to be
+	 * FCP RESPONSE INFO (max 8 bytes, possibly 0)
+	 * FCP SENSE INFO (if any)
+	 * FCP BIDIRECTIONAL READ RESID (if any)
+	 */
+	uint8_t		fcp_rsp_extra[0];
+} fcp_rsp_iu_t;
+#define	MIN_FCP_RESPONSE_SIZE		24
+
+#define	FCP_BIDIR_RSP			0x80	/* Bi-Directional response */
+#define	FCP_BIDIR_RESID_UNDERFLOW	0x40
+#define	FCP_BIDIR_RESID_OVERFLOW	0x20
 #define	FCP_CONF_REQ			0x10
 #define	FCP_RESID_UNDERFLOW		0x08
 #define	FCP_RESID_OVERFLOW		0x04
@@ -162,17 +183,41 @@
 #define	FCP_RSPNS_TMF_SUCCEEDED		8
 #define	FCP_RSPNS_TMF_INCORRECT_LUN	9
 
+/*
+ * R_CTL field definitions
+ *
+ * Bits 31-28 are ROUTING
+ * Bits 27-24 are INFORMATION
+ *
+ * These are nibble values, not bits
+ */
+#define	R_CTL_ROUTE_DATA	0x00
+#define	R_CTL_ROUTE_ELS		0x02
+#define	R_CTL_ROUTE_FC4_LINK	0x03
+#define	R_CTL_ROUTE_VDATA	0x04
+#define	R_CTL_ROUTE_EXENDED	0x05
+#define	R_CTL_ROUTE_BASIC	0x08
+#define	R_CTL_ROUTE_LINK	0x0c
+#define	R_CTL_ROUTE_EXT_ROUTING	0x0f
 
+#define	R_CTL_INFO_UNCATEGORIZED	0x00
+#define	R_CTL_INFO_SOLICITED_DATA	0x01
+#define	R_CTL_INFO_UNSOLICITED_CONTROL	0x02
+#define	R_CTL_INFO_SOLICITED_CONTROL	0x03
+#define	R_CTL_INFO_UNSOLICITED_DATA	0x04
+#define	R_CTL_INFO_DATA_DESCRIPTOR	0x05
+#define	R_CTL_INFO_UNSOLICITED_COMMAND	0x06
+#define	R_CTL_INFO_COMMAND_STATUS	0x07
+
+#define	MAKE_RCTL(a, b)	(((a) << 4) | (b))
+
 /* unconverted miscellany */
 /*
  * Basic FC Link Service defines
  */
-/*
- * These are in the R_CTL field.
- */
-#define	ABTS			0x81
-#define	BA_ACC			0x84	/* of ABORT SEQUENCE */
-#define	BA_RJT			0x85	/* of ABORT SEQUENCE */
+/* #define	ABTS	MAKE_RCTL(R_CTL_ROUTE_BASIC, R_CTL_INFO_SOLICITED_DATA) */
+#define	BA_ACC	MAKE_RCTL(R_CTL_ROUTE_BASIC, R_CTL_INFO_UNSOLICITED_DATA)	/* of ABORT */
+#define	BA_RJT	MAKE_RCTL(R_CTL_ROUTE_BASIC, R_CTL_INFO_DATA_DESCRIPTOR)	/* of ABORT */
 
 /*
  * Link Service Accept/Reject
@@ -196,6 +241,23 @@
 #define	RNC			0x53
 
 /*
+ * PRLI Word 3 definitions
+ * FPC4-r02b January, 2011
+ */
+#define	PRLI_WD3_ENHANCED_DISCOVERY			(1 << 11)
+#define	PRLI_WD3_REC_SUPPORT				(1 << 10)
+#define	PRLI_WD3_TASK_RETRY_IDENTIFICATION_REQUESTED	(1 << 9)
+#define	PRLI_WD3_RETRY					(1 << 8)
+#define	PRLI_WD3_CONFIRMED_COMPLETION_ALLOWED		(1 << 7)
+#define	PRLI_WD3_DATA_OVERLAY_ALLOWED			(1 << 6)
+#define	PRLI_WD3_INITIATOR_FUNCTION			(1 << 5)
+#define	PRLI_WD3_TARGET_FUNCTION			(1 << 4)
+#define	PRLI_WD3_READ_FCP_XFER_RDY_DISABLED		(1 << 1)	/* definitely supposed to be set */
+#define	PRLI_WD3_WRITE_FCP_XFER_RDY_DISABLED		(1 << 0)
+
+
+
+/*
  * FC4 defines
  */
 #define	FC4_IP		5	/* ISO/EEC 8802-2 LLC/SNAP */

Modified: trunk/sys/dev/isp/isp_target.c
===================================================================
--- trunk/sys/dev/isp/isp_target.c	2016-09-15 08:25:48 UTC (rev 7985)
+++ trunk/sys/dev/isp/isp_target.c	2016-09-15 08:26:26 UTC (rev 7986)
@@ -291,7 +291,7 @@
 			break;
 		case IN_RSRC_UNAVAIL:
 			isp_prt(isp, ISP_LOGINFO, "Firmware out of ATIOs");
-			(void) isp_notify_ack(isp, local);
+			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, local);
 			break;
 
 		case IN_RESET:
@@ -346,19 +346,29 @@
 			notify.nt_sid = PORT_ANY;
 			notify.nt_did = PORT_ANY;
 			notify.nt_ncode = NT_GLOBAL_LOGOUT;
+			notify.nt_need_ack = 1;
+			notify.nt_lreserved = local;
 			isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
-			(void) isp_notify_ack(isp, local);
 			break;
 
 		case IN_PORT_CHANGED:
 			isp_prt(isp, ISP_LOGTINFO, "%s: port changed", __func__);
-			(void) isp_notify_ack(isp, local);
+			ISP_MEMZERO(&notify, sizeof (isp_notify_t));
+			notify.nt_hba = isp;
+			notify.nt_wwn = INI_ANY;
+			notify.nt_nphdl = NIL_HANDLE;
+			notify.nt_sid = PORT_ANY;
+			notify.nt_did = PORT_ANY;
+			notify.nt_ncode = NT_CHANGED;
+			notify.nt_need_ack = 1;
+			notify.nt_lreserved = local;
+			isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
 			break;
 
 		default:
 			ISP_SNPRINTF(local, sizeof local, "%s: unknown status to RQSTYPE_NOTIFY (0x%x)", __func__, status);
 			isp_print_bytes(isp, local, QENTRY_LEN, vptr);
-			(void) isp_notify_ack(isp, local);
+			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, local);
 			break;
 		}
 		break;
@@ -466,8 +476,8 @@
 	if (IS_DUALBUS(isp)) {
 		el.le_rsvd = (bus & 0x1) << 7;
 	}
-	el.le_cmd_count = cmd_cnt;
-	el.le_in_count = inot_cnt;
+	el.le_cmd_count = (cmd_cnt < 0)? -cmd_cnt : cmd_cnt;
+	el.le_in_count = (inot_cnt < 0)? -inot_cnt : inot_cnt;
 	if (cmd == RQSTYPE_ENABLE_LUN) {
 		if (IS_SCSI(isp)) {
 			el.le_flags = LUN_TQAE|LUN_DISAD;
@@ -474,15 +484,22 @@
 			el.le_cdb6len = 12;
 			el.le_cdb7len = 12;
 		}
-	} else if (cmd == -RQSTYPE_ENABLE_LUN) {
-		cmd = RQSTYPE_ENABLE_LUN;
-		el.le_cmd_count = 0;
-		el.le_in_count = 0;
-	} else if (cmd == -RQSTYPE_MODIFY_LUN) {
-		cmd = RQSTYPE_MODIFY_LUN;
-		el.le_ops = LUN_CCDECR | LUN_INDECR;
+	} else if (cmd == RQSTYPE_MODIFY_LUN) {
+		if (cmd_cnt == 0 && inot_cnt == 0) {
+			isp_prt(isp, ISP_LOGWARN, "makes no sense to modify a lun with both command and immediate notify counts as zero");
+			return (0);
+		}
+		if (cmd_cnt < 0)
+			el.le_ops |= LUN_CCDECR;
+		else
+			el.le_ops |= LUN_CCINCR;
+		if (inot_cnt < 0)
+			el.le_ops |= LUN_INDECR;
+		else
+			el.le_ops |= LUN_ININCR;
 	} else {
-		el.le_ops = LUN_CCINCR | LUN_ININCR;
+		isp_prt(isp, ISP_LOGWARN, "unknown cmd (0x%x) in %s", cmd, __func__);
+		return (-1);
 	}
 	el.le_header.rqs_entry_type = cmd;
 	el.le_header.rqs_entry_count = 1;
@@ -662,8 +679,8 @@
 			cto->rsp.m1.ct_resp[0] = 0xf0;
 			cto->rsp.m1.ct_resp[2] = (code >> 12) & 0xf;
 			cto->rsp.m1.ct_resp[7] = 8;
-			cto->rsp.m1.ct_resp[12] = (code >> 24) & 0xff;
-			cto->rsp.m1.ct_resp[13] = (code >> 16) & 0xff;
+			cto->rsp.m1.ct_resp[12] = (code >> 16) & 0xff;
+			cto->rsp.m1.ct_resp[13] = (code >> 24) & 0xff;
 		} else {
 			cto->ct_flags |= CT7_FLAG_MODE1 | CT7_SENDSTATUS;
 		}
@@ -837,7 +854,7 @@
 	default:
 		isp_prt(isp, ISP_LOGERR, "%s: unknown event 0x%x", __func__, event);
 		if (isp->isp_state == ISP_RUNSTATE) {
-			(void) isp_notify_ack(isp, NULL);
+			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, NULL);
 		}
 		break;
 	}
@@ -897,13 +914,13 @@
 			break;
 		default:
 			isp_prt(isp, ISP_LOGERR, "%s: unhandled message 0x%x", __func__, inp->in_msg[0]);
-			(void) isp_notify_ack(isp, inp);
+			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inp);
 			return;
 		}
 		isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
 	} else {
 		isp_prt(isp, ISP_LOGERR, "%s: unknown immediate notify status 0x%x", __func__, inp->in_status);
-		(void) isp_notify_ack(isp, inp);
+		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inp);
 	}
 }
 
@@ -946,7 +963,7 @@
 
 	if (inp->in_status != IN_MSG_RECEIVED) {
 		isp_prt(isp, ISP_LOGINFO, f2, "immediate notify status", inp->in_status, notify.nt_lun, loopid, inp->in_task_flags, inp->in_seqid);
-		(void) isp_notify_ack(isp, inp);
+		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inp);
 		return;
 	}
 
@@ -967,7 +984,7 @@
 		notify.nt_ncode = NT_CLEAR_ACA;
 	} else {
 		isp_prt(isp, ISP_LOGWARN, f2, "task flag", inp->in_status, notify.nt_lun, loopid, inp->in_task_flags,  inp->in_seqid);
-		(void) isp_notify_ack(isp, inp);
+		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inp);
 		return;
 	}
 	isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
@@ -1054,10 +1071,12 @@
 
 	if (IS_24XX(isp)) {
 		na_fcentry_24xx_t *na = (na_fcentry_24xx_t *) storage;
+		na->na_header.rqs_entry_type = RQSTYPE_NOTIFY_ACK;
+		na->na_header.rqs_entry_count = 1;
 		if (arg) {
 			in_fcentry_24xx_t *in = arg;
 			na->na_nphdl = in->in_nphdl;
-			na->na_flags = in->in_flags & IN24XX_FLAG_PUREX_IOCB;
+			na->na_flags = in->in_flags;
 			na->na_status = in->in_status;
 			na->na_status_subcode = in->in_status_subcode;
 			na->na_rxid = in->in_rxid;
@@ -1068,14 +1087,19 @@
 				na->na_srr_reloff_hi = in->in_srr_reloff_hi;
 				na->na_srr_reloff_lo = in->in_srr_reloff_lo;
 				na->na_srr_iu = in->in_srr_iu;
-				na->na_srr_flags = 1;
-				na->na_srr_reject_vunique = 0;
-				na->na_srr_reject_explanation = 1;
-				na->na_srr_reject_code = 1;
+				/*
+				 * Whether we're accepting the SRR or rejecting
+				 * it is determined by looking at the in_reserved
+				 * field in the original notify structure.
+				 */
+				if (in->in_reserved) {
+					na->na_srr_flags = 1;
+					na->na_srr_reject_vunique = 0;
+					na->na_srr_reject_code = 9;		/* unable to perform this command at this time */
+					na->na_srr_reject_explanation = 0x2a;	/* unable to supply the requested data */
+				}
 			}
 		}
-		na->na_header.rqs_entry_type = RQSTYPE_NOTIFY_ACK;
-		na->na_header.rqs_entry_count = 1;
 		isp_put_notify_24xx_ack(isp, na, (na_fcentry_24xx_t *)outp);
 	} else if (IS_FC(isp)) {
 		na_fcentry_t *na = (na_fcentry_t *) storage;
@@ -1093,10 +1117,10 @@
 			}
 			na->na_task_flags = inp->in_task_flags & TASK_FLAGS_RESERVED_MASK;
 			na->na_seqid = inp->in_seqid;
+			na->na_status = inp->in_status;
 			na->na_flags = NAFC_RCOUNT;
-			na->na_status = inp->in_status;
 			if (inp->in_status == IN_RESET) {
-				na->na_flags |= NAFC_RST_CLRD;
+				na->na_flags = NAFC_RST_CLRD;	/* We do not modify resource counts for LIP resets */
 			}
 			if (inp->in_status == IN_MSG_RECEIVED) {
 				na->na_flags |= NAFC_TVALID;
@@ -1200,7 +1224,7 @@
 		ISP_MEMZERO(&rsp->abts_rsp_payload.ba_rjt, sizeof (rsp->abts_rsp_payload.ba_acc));
 		switch (errno) {
 		case ENOMEM:
-			rsp->abts_rsp_payload.ba_rjt.reason = 5;	/* Logical Busy */
+			rsp->abts_rsp_payload.ba_rjt.reason = 5;	/* Logical Unit Busy */
 			break;
 		default:
 			rsp->abts_rsp_payload.ba_rjt.reason = 9;	/* Unable to perform command request */
@@ -1771,7 +1795,7 @@
 		break;
 
 	case CT7_SRR:
-		isp_prt(isp, ISP_LOGWARN, "SRR received");
+		isp_prt(isp, ISP_LOGTDEBUG0, "SRR received");
 		break;
 
 	default:
@@ -1789,7 +1813,7 @@
 		 */
 		if (ct->ct_syshandle == 0) {
 			if (ct->ct_flags & CT7_TERMINATE) {
-				isp_prt(isp, ISP_LOGINFO, "termination of 0x%x complete", ct->ct_rxid);
+				isp_prt(isp, ISP_LOGINFO, "termination of [RX_ID 0x%x] complete", ct->ct_rxid);
 			} else if ((ct->ct_flags & CT7_SENDSTATUS) == 0) {
 				isp_prt(isp, pl, "intermediate CTIO completed ok");
 			} else {
@@ -1844,7 +1868,7 @@
 			char buf[64];
 			ISP_SNPRINTF(buf, sizeof buf, "%s: bad channel %d for status 0x%x", __func__, chan, inot_24xx->in_status);
 			isp_print_bytes(isp, buf, QENTRY_LEN, inot_24xx);
-			(void) isp_notify_ack(isp, inot_24xx);
+			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot_24xx);
 			return;
 		}
 		lochan = chan;
@@ -1860,12 +1884,13 @@
 		case IN24XX_LINK_FAILED:
 		case IN24XX_SRR_RCVD:
 		case IN24XX_ELS_RCVD:
+			inot_24xx->in_reserved = 0;	/* clear this for later usage */
 			inot_24xx->in_vpidx = chan;
 			isp_async(isp, ISPASYNC_TARGET_ACTION, inot_24xx);
 			break;
 		default:
 			isp_prt(isp, ISP_LOGINFO, "%s: unhandled status (0x%x) for chan %d", __func__, inot_24xx->in_status, chan);
-			(void) isp_notify_ack(isp, inot_24xx);
+			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot_24xx);
 			break;
 		}
 	}

Modified: trunk/sys/dev/isp/isp_target.h
===================================================================
--- trunk/sys/dev/isp/isp_target.h	2016-09-15 08:25:48 UTC (rev 7985)
+++ trunk/sys/dev/isp/isp_target.h	2016-09-15 08:26:26 UTC (rev 7986)
@@ -50,6 +50,7 @@
 	NT_LINK_DOWN,
 	NT_LOGOUT,
 	NT_GLOBAL_LOGOUT,
+	NT_CHANGED,
 	NT_ARRIVED,
 	NT_DEPARTED,
 	NT_HBA_RESET

Modified: trunk/sys/dev/isp/ispmbox.h
===================================================================
--- trunk/sys/dev/isp/ispmbox.h	2016-09-15 08:25:48 UTC (rev 7985)
+++ trunk/sys/dev/isp/ispmbox.h	2016-09-15 08:26:26 UTC (rev 7986)
@@ -428,7 +428,8 @@
 	uint8_t		req_target;
 	uint16_t	req_scclun;
 	uint16_t	req_flags;
-	uint16_t	req_reserved;
+	uint8_t		req_crn;
+	uint8_t		req_reserved;
 	uint16_t	req_time;
 	uint16_t	req_seg_count;
 	uint8_t		req_cdb[16];
@@ -458,7 +459,8 @@
 	uint8_t		req_target;
 	uint16_t	req_scclun;
 	uint16_t	req_flags;
-	uint16_t	req_reserved;
+	uint8_t		req_crn;
+	uint8_t		req_reserved;
 	uint16_t	req_time;
 	uint16_t	req_seg_count;
 	uint8_t		req_cdb[16];
@@ -473,7 +475,8 @@
 	uint16_t	req_target;
 	uint16_t	req_scclun;
 	uint16_t	req_flags;
-	uint16_t	req_reserved;
+	uint8_t		req_crn;
+	uint8_t		req_reserved;
 	uint16_t	req_time;
 	uint16_t	req_seg_count;
 	uint8_t		req_cdb[16];
@@ -515,15 +518,10 @@
 	uint8_t		req_cdb[44];
 } ispextreq_t;
 
-/* 24XX only */
-typedef struct {
-	uint16_t	fcd_length;
-	uint16_t	fcd_a1500;
-	uint16_t	fcd_a3116;
-	uint16_t	fcd_a4732;
-	uint16_t	fcd_a6348;
-} fcp_cmnd_ds_t;
 
+/*
+ * ISP24XX structures
+ */
 typedef struct {
 	isphdr_t	req_header;
 	uint32_t	req_handle;
@@ -530,31 +528,6 @@
 	uint16_t	req_nphdl;
 	uint16_t	req_time;
 	uint16_t	req_seg_count;
-	uint16_t	req_fc_rsp_dsd_length;
-	uint8_t		req_lun[8];
-	uint16_t	req_flags;
-	uint16_t	req_fc_cmnd_dsd_length;
-	uint16_t	req_fc_cmnd_dsd_a1500;
-	uint16_t	req_fc_cmnd_dsd_a3116;
-	uint16_t	req_fc_cmnd_dsd_a4732;
-	uint16_t	req_fc_cmnd_dsd_a6348;
-	uint16_t	req_fc_rsp_dsd_a1500;
-	uint16_t	req_fc_rsp_dsd_a3116;
-	uint16_t	req_fc_rsp_dsd_a4732;
-	uint16_t	req_fc_rsp_dsd_a6348;
-	uint32_t	req_totalcnt;
-	uint16_t	req_tidlo;
-	uint8_t		req_tidhi;
-	uint8_t		req_vpidx;
-	ispds64_t	req_dataseg;
-} ispreqt6_t;
-
-typedef struct {
-	isphdr_t	req_header;
-	uint32_t	req_handle;
-	uint16_t	req_nphdl;
-	uint16_t	req_time;
-	uint16_t	req_seg_count;
 	uint16_t	req_reserved;
 	uint8_t		req_lun[8];
 	uint8_t		req_alen_datadir;
@@ -945,7 +918,7 @@
 #define	ICBOPT_SRCHDOWN		0x0400
 #define	ICBOPT_NOLIP		0x0200
 #define	ICBOPT_PDBCHANGE_AE	0x0100
-#define	ICBOPT_INI_TGTTYPE	0x0080
+#define	ICBOPT_TGT_TYPE		0x0080
 #define	ICBOPT_INI_ADISC	0x0040
 #define	ICBOPT_INI_DISABLE	0x0020
 #define	ICBOPT_TGT_ENABLE	0x0010
@@ -1044,7 +1017,7 @@
 #define	ICB_DFLT_RCOUNT		3
 
 #define	ICB_LOGIN_TOV		30
-#define	ICB_LUN_ENABLE_TOV	180
+#define	ICB_LUN_ENABLE_TOV	15
 
 
 /*
@@ -1272,10 +1245,8 @@
 #define	PDB_STATE_PLOGO		10
 #define	PDB_STATE_PLOG_ACK	11
 
-#define		SVC3_TGT_ROLE		0x10
-#define 	SVC3_INI_ROLE		0x20
-#define			SVC3_ROLE_MASK	0x30
-#define			SVC3_ROLE_SHIFT	4
+#define	SVC3_ROLE_MASK		0x30
+#define	SVC3_ROLE_SHIFT		4
 
 #define	BITS2WORD(x)		((x)[0] << 16 | (x)[3] << 8 | (x)[2])
 #define	BITS2WORD_24XX(x)	((x)[0] << 16 | (x)[1] << 8 | (x)[2])
@@ -1321,8 +1292,8 @@
  */
 typedef struct {
 	uint16_t	handle;
-	uint16_t	reserved;
-	uint32_t	s3_role	: 8,
+	uint16_t	prli_word3;
+	uint32_t		: 8,
 			portid	: 24;
 	uint8_t		portname[8];
 	uint8_t		nodename[8];
@@ -1781,6 +1752,7 @@
 #define	IN_PORT_CHANGED	0x2A	/* port changed */
 #define	IN_GLOBAL_LOGO	0x2E	/* all ports logged out */
 #define	IN_NO_NEXUS	0x3B	/* Nexus not established */
+#define	IN_SRR_RCVD	0x45	/* SRR received */
 
 /*
  * Values for the in_task_flags field- should only get one at a time!
@@ -1811,8 +1783,8 @@
 	uint16_t	in_srr_iu;
 	uint16_t	in_srr_oxid;
 	/*
-	 * If bit 2 is set in in_flags, the following
-	 * two tags are valid. If the received ELS is
+	 * If bit 2 is set in in_flags, the N-Port and
+	 * handle tags are valid. If the received ELS is
 	 * a LOGO, then these tags contain the N Port ID
 	 * from the LOGO payload. If the received ELS
 	 * request is TPRLO, these tags contain the
@@ -1819,16 +1791,9 @@
 	 * Third Party Originator N Port ID.
 	 */
 	uint16_t	in_nport_id_hi;
+#define	in_prli_options in_nport_id_hi
 	uint8_t		in_nport_id_lo;
 	uint8_t		in_reserved3;
-	/*
-	 * If bit 2 is set in in_flags, the following
-	 * tag is valid. If the received ELS is a LOGO,
-	 * then this tag contains the n-port handle
-	 * from the LOGO payload. If the received ELS
-	 * request is TPRLO, this tag contain the
-	 * n-port handle for the Third Party Originator.
-	 */
 	uint16_t	in_np_handle;
 	uint8_t		in_reserved4[12];
 	uint8_t		in_reserved5;
@@ -2183,7 +2148,7 @@
 	uint8_t 	ct_tag_val;	/* tag value */
 	uint8_t 	ct_tag_type;	/* tag type */
 	uint32_t	ct_xfrlen;	/* transfer length */
-	int32_t		ct_resid;	/* residual length */
+	uint32_t	ct_resid;	/* residual length */
 	uint16_t	ct_timeout;
 	uint16_t	ct_seg_count;
 	ispds_t		ct_dataseg[ISP_RQDSEG];
@@ -2206,8 +2171,8 @@
  * ct_flags values
  */
 #define CT_TQAE		0x00000002	/* bit  1, Tagged Queue Action enable */
-#define CT_DATA_IN	0x00000040	/* bits 6&7, Data direction */
-#define CT_DATA_OUT	0x00000080	/* bits 6&7, Data direction */
+#define CT_DATA_IN	0x00000040	/* bits 6&7, Data direction - *to* initiator */
+#define CT_DATA_OUT	0x00000080	/* bits 6&7, Data direction - *from* initiator */
 #define CT_NO_DATA	0x000000C0	/* bits 6&7, Data direction */
 #define	CT_CCINCR	0x00000100	/* bit 8, autoincrement atio count */
 #define CT_DATAMASK	0x000000C0	/* bits 6&7, Data direction */
@@ -2278,7 +2243,7 @@
 	uint16_t	ct_timeout;
 	uint16_t	ct_seg_count;
 	uint32_t	ct_reloff;	/* relative offset */
-	int32_t		ct_resid;	/* residual length */
+	uint32_t	ct_resid;	/* residual length */
 	union {
 		/*
 		 * The three different modes that the target driver
@@ -2317,7 +2282,10 @@
 			uint16_t _reserved2;
 			uint16_t _reserved3;
 			uint32_t ct_datalen;
-			ispds_t ct_fcp_rsp_iudata;
+			union {
+				ispds_t	ct_fcp_rsp_iudata_32;
+				ispds64_t ct_fcp_rsp_iudata_64;
+			} u;
 		} m2;
 	} rsp;
 } ct2_entry_t;
@@ -2332,7 +2300,7 @@
 	uint16_t	ct_timeout;
 	uint16_t	ct_seg_count;
 	uint32_t	ct_reloff;	/* relative offset */
-	int32_t		ct_resid;	/* residual length */
+	uint32_t	ct_resid;	/* residual length */
 	union {
 		struct {
 			uint32_t _reserved;
@@ -2358,7 +2326,10 @@
 			uint16_t _reserved2;
 			uint16_t _reserved3;
 			uint32_t ct_datalen;
-			ispds_t ct_fcp_rsp_iudata;
+			union {
+				ispds_t	ct_fcp_rsp_iudata_32;
+				ispds64_t ct_fcp_rsp_iudata_64;
+			} u;
 		} m2;
 	} rsp;
 } ct2e_entry_t;
@@ -2370,8 +2341,8 @@
 #define	CT2_FLAG_MODE1	0x0001
 #define	CT2_FLAG_MODE2	0x0002
 #define		CT2_FLAG_MMASK	0x0003
-#define CT2_DATA_IN	0x0040
-#define CT2_DATA_OUT	0x0080
+#define CT2_DATA_IN	0x0040	/* *to* initiator */
+#define CT2_DATA_OUT	0x0080	/* *from* initiator */
 #define CT2_NO_DATA	0x00C0
 #define 	CT2_DATAMASK	0x00C0
 #define	CT2_CCINCR	0x0100
@@ -2412,7 +2383,7 @@
 	uint32_t	ct_rxid;
 	uint16_t	ct_senselen;	/* mode 1 only */
 	uint16_t	ct_flags;
-	int32_t		ct_resid;	/* residual length */
+	uint32_t	ct_resid;	/* residual length */
 	uint16_t	ct_oxid;
 	uint16_t	ct_scsi_status;	/* modes 0 && 1 only */
 	union {
@@ -2430,8 +2401,9 @@
 		} m1;
 		struct {
 			uint32_t reserved0;
+			uint32_t reserved1;
 			uint32_t ct_datalen;
-			uint32_t reserved1;
+			uint32_t reserved2;
 			ispds64_t ct_fcp_rsp_iudata;
 		} m2;
 	} rsp;
@@ -2440,10 +2412,10 @@
 /*
  * ct_flags values for CTIO7
  */
-#define CT7_DATA_IN	0x0002
-#define CT7_DATA_OUT	0x0001
 #define CT7_NO_DATA	0x0000
-#define 	CT7_DATAMASK	0x003
+#define CT7_DATA_OUT	0x0001	/* *from* initiator */
+#define CT7_DATA_IN	0x0002	/* *to* initiator */
+#define 	CT7_DATAMASK	0x3
 #define	CT7_DSD_ENABLE	0x0004
 #define	CT7_CONF_STSFD	0x0010
 #define	CT7_EXPLCT_CONF	0x0020
@@ -2451,9 +2423,9 @@
 #define	CT7_FLAG_MODE1	0x0040
 #define	CT7_FLAG_MODE2	0x0080
 #define		CT7_FLAG_MMASK	0x00C0
-#define	CT7_NOACK	0x0100
+#define	CT7_NOACK	    0x0100
 #define	CT7_TASK_ATTR_SHIFT	9
-#define	CT7_CONFIRM	0x2000
+#define	CT7_CONFIRM     0x2000
 #define	CT7_TERMINATE	0x4000
 #define CT7_SENDSTATUS	0x8000
 

Modified: trunk/sys/dev/isp/ispvar.h
===================================================================
--- trunk/sys/dev/isp/ispvar.h	2016-09-15 08:25:48 UTC (rev 7985)
+++ trunk/sys/dev/isp/ispvar.h	2016-09-15 08:26:26 UTC (rev 7986)
@@ -77,7 +77,7 @@
  */
 #define	MAX_TARGETS		16
 #ifndef	MAX_FC_TARG
-#define	MAX_FC_TARG		512
+#define	MAX_FC_TARG		256
 #endif
 #define	ISP_MAX_TARGETS(isp)	(IS_FC(isp)? MAX_FC_TARG : MAX_TARGETS)
 #define	ISP_MAX_LUNS(isp)	(isp)->isp_maxluns
@@ -400,10 +400,10 @@
 	 * A device is 'autologin' if the firmware automatically logs into
 	 * it (re-logins as needed). Basically, local private loop devices.
 	 *
+	 * PRLI word 3 parameters contains role as well as other things.
+	 *
 	 * The state is the current state of this entry.
 	 *
-	 * Role is Initiator, Target, Both
-	 *
 	 * Portid is obvious, as are node && port WWNs. The new_role and
 	 * new_portid is for when we are pending a change.
 	 *
@@ -412,17 +412,18 @@
 	 * You should also never see anything with an initiator role
 	 * with this set.
 	 */
+	uint16_t	prli_word3;		/* PRLI parameters */
+	uint16_t	new_prli_word3;		/* Incoming new PRLI parameters */
 	uint16_t	dev_map_idx	: 12,
 			autologin	: 1,	/* F/W does PLOGI/PLOGO */
 			state		: 3;
-	uint32_t	reserved	: 5,
+	uint32_t			: 7,
 			target_mode	: 1,
-			roles		: 2,
 			portid		: 24;
 	uint32_t
+					: 6,
+			announced	: 1,
 			dirty		: 1,	/* commands have been run */
-			new_reserved	: 5,
-			new_roles	: 2,
 			new_portid	: 24;
 	uint64_t	node_wwn;
 	uint64_t	port_wwn;
@@ -447,6 +448,7 @@
 
 typedef struct {
 	uint32_t
+				fctape_enabled	: 1,
 				link_active	: 1,
 				sendmarker	: 1,
 				role		: 2,
@@ -680,21 +682,24 @@
 /*
  * ISP Runtime Configuration Options
  */
-#define	ISP_CFG_NORELOAD	0x80	/* don't download f/w */
-#define	ISP_CFG_NONVRAM		0x40	/* ignore NVRAM */
-#define	ISP_CFG_TWOGB		0x20	/* force 2GB connection (23XX only) */
-#define	ISP_CFG_ONEGB		0x10	/* force 1GB connection (23XX only) */
 #define	ISP_CFG_FULL_DUPLEX	0x01	/* Full Duplex (Fibre Channel only) */
-#define	ISP_CFG_PORT_PREF	0x0C	/* Mask for Port Prefs (2200 only) */
+#define	ISP_CFG_PORT_PREF	0x0c	/* Mask for Port Prefs (all FC except 2100) */
 #define	ISP_CFG_LPORT		0x00	/* prefer {N/F}L-Port connection */
 #define	ISP_CFG_NPORT		0x04	/* prefer {N/F}-Port connection */
 #define	ISP_CFG_NPORT_ONLY	0x08	/* insist on {N/F}-Port connection */
-#define	ISP_CFG_LPORT_ONLY	0x0C	/* insist on {N/F}L-Port connection */
+#define	ISP_CFG_LPORT_ONLY	0x0c	/* insist on {N/F}L-Port connection */
+#define	ISP_CFG_ONEGB		0x10	/* force 1GB connection (23XX only) */
+#define	ISP_CFG_TWOGB		0x20	/* force 2GB connection (23XX only) */
+#define	ISP_CFG_NORELOAD	0x80	/* don't download f/w */
+#define	ISP_CFG_NONVRAM		0x40	/* ignore NVRAM */
+#define	ISP_CFG_NOFCTAPE	0x100	/* disable FC-Tape */
+#define	ISP_CFG_FCTAPE		0x200	/* enable FC-Tape */
 #define	ISP_CFG_OWNFSZ		0x400	/* override NVRAM frame size */
 #define	ISP_CFG_OWNLOOPID	0x800	/* override NVRAM loopid */
 #define	ISP_CFG_OWNEXCTHROTTLE	0x1000	/* override NVRAM execution throttle */
 #define	ISP_CFG_FOURGB		0x2000	/* force 4GB connection (24XX only) */
 #define	ISP_CFG_EIGHTGB		0x4000	/* force 8GB connection (25XX only) */
+#define	ISP_CFG_SIXTEENGB	0x8000	/* force 16GB connection (82XX only) */
 
 /*
  * For each channel, the outer layers should know what role that channel
@@ -951,6 +956,7 @@
 	ISPASYNC_DEV_STAYED,		/* FC Device Stayed */
 	ISPASYNC_DEV_GONE,		/* FC Device Departure */
 	ISPASYNC_TARGET_NOTIFY,		/* All target async notification */
+	ISPASYNC_TARGET_NOTIFY_ACK,	/* All target notify ack required */
 	ISPASYNC_TARGET_ACTION,		/* All target action requested */
 	ISPASYNC_FW_CRASH,		/* All Firmware has crashed */
 	ISPASYNC_FW_RESTARTED		/* All Firmware has been restarted */
@@ -987,8 +993,9 @@
 #define	ISP_LOGDEBUG1	0x20	/* log intermediate debug messages */
 #define	ISP_LOGDEBUG2	0x40	/* log most debug messages */
 #define	ISP_LOGDEBUG3	0x80	/* log high frequency debug messages */
-#define	ISP_LOGSANCFG	0x100	/* log SAN configuration */
+#define	ISP_LOG_SANCFG	0x100	/* log SAN configuration */
 #define	ISP_LOG_CWARN	0x200	/* log SCSI command "warnings" (e.g., check conditions) */
+#define	ISP_LOG_WARN1	0x400	/* log WARNS we might be interested at some time */
 #define	ISP_LOGTINFO	0x1000	/* log informational messages (target mode) */
 #define	ISP_LOGTDEBUG0	0x2000	/* log simple debug messages (target mode) */
 #define	ISP_LOGTDEBUG1	0x4000	/* log intermediate debug messages (target) */
@@ -1063,7 +1070,8 @@
  *	XS_GET_RESID(xs, resid)	sets the current residual count
  *	XS_STSP(xs)		gets a pointer to the SCSI status byte ""
  *	XS_SNSP(xs)		gets a pointer to the associate sense data
- *	XS_SNSLEN(xs)		gets the length of sense data storage
+ *	XS_TOT_SNSLEN(xs)	gets the total length of sense data storage
+ *	XS_CUR_SNSLEN(xs)	gets the currently used lenght of sense data storage
  *	XS_SNSKEY(xs)		dereferences XS_SNSP to get the current stored Sense Key
  *	XS_SNSASC(xs)		dereferences XS_SNSP to get the current stored Additional Sense Code
  *	XS_SNSASCQ(xs)		dereferences XS_SNSP to get the current stored Additional Sense Code Qualifier
@@ -1085,8 +1093,10 @@
  *	XS_NOERR(xs)	there is no error currently set
  *	XS_INITERR(xs)	initialize error state
  *
- *	XS_SAVE_SENSE(xs, sp, len)	save sense data
+ *	XS_SAVE_SENSE(xs, sp, total_len, this_len)	save sense data (total and current amount)
  *
+ *	XS_APPEND_SENSE(xs, sp, len)	append more sense data
+ *
  *	XS_SENSE_VALID(xs)		indicates whether sense is valid
  *
  *	DEFAULT_FRAMESIZE(ispsoftc_t *)		Default Frame Size
@@ -1154,8 +1164,8 @@
  * Enable/Disable/Modify a logical unit.
  * (softc, cmd, bus, tgt, lun, cmd_cnt, inotify_cnt)
  */
-#define	DFLT_CMND_CNT	0xfe	/* unmonitored */
-#define	DFLT_INOT_CNT	0xfe	/* unmonitored */
+#define	DFLT_CMND_CNT	0xff	/* unmonitored */
+#define	DFLT_INOT_CNT	0xff	/* unmonitored */
 int isp_lun_cmd(ispsoftc_t *, int, int, int, int, int);
 
 /*



More information about the Midnightbsd-cvs mailing list