Home | History | Annotate | Line # | Download | only in ic
isp_target.c revision 1.33
      1 -/* $NetBSD: isp_target.c,v 1.33 2009/06/25 23:44:02 mjacob Exp $ */
      2 /*-
      3  *  Copyright (c) 1997-2008 by Matthew Jacob
      4  *  All rights reserved.
      5  *
      6  *  Redistribution and use in source and binary forms, with or without
      7  *  modification, are permitted provided that the following conditions
      8  *  are met:
      9  *
     10  *  1. Redistributions of source code must retain the above copyright
     11  *     notice, this list of conditions and the following disclaimer.
     12  *  2. Redistributions in binary form must reproduce the above copyright
     13  *     notice, this list of conditions and the following disclaimer in the
     14  *     documentation and/or other materials provided with the distribution.
     15  *
     16  *  THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  *  ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
     20  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  *  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  *  SUCH DAMAGE.
     27  *
     28  *
     29  *  Alternatively, this software may be distributed under the terms of the
     30  *  the GNU Public License ("GPL") with platforms where the prevalant license
     31  *  is the GNU Public License:
     32  *
     33  *   This program is free software; you can redistribute it and/or modify
     34  *   it under the terms of The Version 2 GNU General Public License as published
     35  *   by the Free Software Foundation.
     36  *
     37  *   This program is distributed in the hope that it will be useful,
     38  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
     39  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     40  *   GNU General Public License for more details.
     41  *
     42  *   You should have received a copy of the GNU General Public License
     43  *   along with this program; if not, write to the Free Software
     44  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
     45  *
     46  *
     47  *  Matthew Jacob
     48  *  Feral Software
     49  *  421 Laurel Avenue
     50  *  Menlo Park, CA 94025
     51  *  USA
     52  *
     53  *  gplbsd at feral com
     54  */
     55 /*
     56  * Machine and OS Independent Target Mode Code for the Qlogic SCSI/FC adapters.
     57  */
     58 /*
     59  * Bug fixes gratefully acknowledged from:
     60  *	Oded Kedem <oded (at) kashya.com>
     61  */
     62 /*
     63  * Include header file appropriate for platform we're building on.
     64  */
     65 
     66 #ifdef	__NetBSD__
     67 #include <sys/cdefs.h>
     68 __KERNEL_RCSID(0, "$NetBSD: isp_target.c,v 1.33 2009/06/25 23:44:02 mjacob Exp $");
     69 #include <dev/ic/isp_netbsd.h>
     70 #endif
     71 #ifdef	__FreeBSD__
     72 #include <sys/cdefs.h>
     73 __FBSDID("$FreeBSD:$");
     74 #include <dev/isp/isp_freebsd.h>
     75 #endif
     76 #ifdef	__OpenBSD__
     77 #include <dev/ic/isp_openbsd.h>
     78 #endif
     79 #ifdef	__linux__
     80 #include "isp_linux.h"
     81 #endif
     82 
     83 #ifdef	ISP_TARGET_MODE
     84 static const char atiocope[] = "ATIO returned for lun %d because it was in the middle of Bus Device Reset on bus %d";
     85 static const char atior[] = "ATIO returned on for lun %d on from loopid %d because a Bus Reset occurred on bus %d";
     86 static const char rqo[] = "%s: Request Queue Overflow";
     87 
     88 static void isp_got_msg(ispsoftc_t *, in_entry_t *);
     89 static void isp_got_msg_fc(ispsoftc_t *, in_fcentry_t *);
     90 static void isp_got_tmf_24xx(ispsoftc_t *, at7_entry_t *);
     91 static void isp_handle_atio(ispsoftc_t *, at_entry_t *);
     92 static void isp_handle_atio2(ispsoftc_t *, at2_entry_t *);
     93 static void isp_handle_ctio(ispsoftc_t *, ct_entry_t *);
     94 static void isp_handle_ctio2(ispsoftc_t *, ct2_entry_t *);
     95 static void isp_handle_ctio7(ispsoftc_t *, ct7_entry_t *);
     96 static void isp_handle_24xx_inotify(ispsoftc_t *, in_fcentry_24xx_t *);
     97 
     98 /*
     99  * The Qlogic driver gets an interrupt to look at response queue entries.
    100  * Some of these are status completions for initiatior mode commands, but
    101  * if target mode is enabled, we get a whole wad of response queue entries
    102  * to be handled here.
    103  *
    104  * Basically the split into 3 main groups: Lun Enable/Modification responses,
    105  * SCSI Command processing, and Immediate Notification events.
    106  *
    107  * You start by writing a request queue entry to enable target mode (and
    108  * establish some resource limitations which you can modify later).
    109  * The f/w responds with a LUN ENABLE or LUN MODIFY response with
    110  * the status of this action. If the enable was successful, you can expect...
    111  *
    112  * Response queue entries with SCSI commands encapsulate show up in an ATIO
    113  * (Accept Target IO) type- sometimes with enough info to stop the command at
    114  * this level. Ultimately the driver has to feed back to the f/w's request
    115  * queue a sequence of CTIOs (continue target I/O) that describe data to
    116  * be moved and/or status to be sent) and finally finishing with sending
    117  * to the f/w's response queue an ATIO which then completes the handshake
    118  * with the f/w for that command. There's a lot of variations on this theme,
    119  * including flags you can set in the CTIO for the Qlogic 2X00 fibre channel
    120  * cards that 'auto-replenish' the f/w's ATIO count, but this is the basic
    121  * gist of it.
    122  *
    123  * The third group that can show up in the response queue are Immediate
    124  * Notification events. These include things like notifications of SCSI bus
    125  * resets, or Bus Device Reset messages or other messages received. This
    126  * a classic oddbins area. It can get  a little weird because you then turn
    127  * around and acknowledge the Immediate Notify by writing an entry onto the
    128  * request queue and then the f/w turns around and gives you an acknowledgement
    129  * to *your* acknowledgement on the response queue (the idea being to let
    130  * the f/w tell you when the event is *really* over I guess).
    131  *
    132  */
    133 
    134 
    135 /*
    136  * A new response queue entry has arrived. The interrupt service code
    137  * has already swizzled it into the platform dependent from canonical form.
    138  *
    139  * Because of the way this driver is designed, unfortunately most of the
    140  * actual synchronization work has to be done in the platform specific
    141  * code- we have no synchroniation primitives in the common code.
    142  */
    143 
    144 int
    145 isp_target_notify(ispsoftc_t *isp, void *vptr, uint32_t *optrp)
    146 {
    147 	uint16_t status;
    148 	uint32_t seqid;
    149 	union {
    150 		at_entry_t	*atiop;
    151 		at2_entry_t	*at2iop;
    152 		at2e_entry_t	*at2eiop;
    153 		at7_entry_t	*at7iop;
    154 		ct_entry_t	*ctiop;
    155 		ct2_entry_t	*ct2iop;
    156 		ct2e_entry_t	*ct2eiop;
    157 		ct7_entry_t	*ct7iop;
    158 		lun_entry_t	*lunenp;
    159 		in_entry_t	*inotp;
    160 		in_fcentry_t	*inot_fcp;
    161 		in_fcentry_e_t	*inote_fcp;
    162 		in_fcentry_24xx_t *inot_24xx;
    163 		na_entry_t	*nackp;
    164 		na_fcentry_t	*nack_fcp;
    165 		na_fcentry_e_t	*nacke_fcp;
    166 		na_fcentry_24xx_t *nack_24xx;
    167 		isphdr_t	*hp;
    168 		abts_t		*abts;
    169 		abts_rsp_t	*abts_rsp;
    170 		els_t		*els;
    171 		void *		*vp;
    172 #define	atiop		unp.atiop
    173 #define	at2iop		unp.at2iop
    174 #define	at2eiop		unp.at2eiop
    175 #define	at7iop		unp.at7iop
    176 #define	ctiop		unp.ctiop
    177 #define	ct2iop		unp.ct2iop
    178 #define	ct2eiop		unp.ct2eiop
    179 #define	ct7iop		unp.ct7iop
    180 #define	lunenp		unp.lunenp
    181 #define	inotp		unp.inotp
    182 #define	inot_fcp	unp.inot_fcp
    183 #define	inote_fcp	unp.inote_fcp
    184 #define	inot_24xx	unp.inot_24xx
    185 #define	nackp		unp.nackp
    186 #define	nack_fcp	unp.nack_fcp
    187 #define	nacke_fcp	unp.nacke_fcp
    188 #define	nack_24xx	unp.nack_24xx
    189 #define	abts		unp.abts
    190 #define	abts_rsp	unp.abts_rsp
    191 #define els		unp.els
    192 #define	hdrp		unp.hp
    193 	} unp;
    194 	uint8_t local[QENTRY_LEN];
    195 	uint16_t iid;
    196 	int bus, type, level, rval = 1;
    197 	isp_notify_t notify;
    198 
    199 	type = isp_get_response_type(isp, (isphdr_t *)vptr);
    200 	unp.vp = vptr;
    201 
    202 	ISP_TDQE(isp, "isp_target_notify", (int) *optrp, vptr);
    203 
    204 	switch (type) {
    205 	case RQSTYPE_ATIO:
    206 		if (IS_24XX(isp)) {
    207 			int len;
    208 
    209 			isp_get_atio7(isp, at7iop, (at7_entry_t *) local);
    210 			at7iop = (at7_entry_t *) local;
    211 			/*
    212 			 * Check for and do something with commands whose
    213 			 * IULEN extends past a single queue entry.
    214 			 */
    215 			len = at7iop->at_ta_len & 0xfffff;
    216 			if (len > (QENTRY_LEN - 8)) {
    217 				len -= (QENTRY_LEN - 8);
    218 				isp_prt(isp, ISP_LOGINFO, "long IU length (%d) ignored", len);
    219 				while (len > 0) {
    220 					*optrp =  ISP_NXT_QENTRY(*optrp, RESULT_QUEUE_LEN(isp));
    221 					len -= QENTRY_LEN;
    222 				}
    223 			}
    224 			/*
    225 			 * Check for a task management function
    226 			 */
    227 			if (at7iop->at_cmnd.fcp_cmnd_task_management) {
    228 				isp_got_tmf_24xx(isp, at7iop);
    229 				break;
    230 			}
    231 			/*
    232 			 * Just go straight to outer layer for this one.
    233 			 */
    234 			isp_async(isp, ISPASYNC_TARGET_ACTION, local);
    235 		} else {
    236 			isp_get_atio(isp, atiop, (at_entry_t *) local);
    237 			isp_handle_atio(isp, (at_entry_t *) local);
    238 		}
    239 		break;
    240 
    241 	case RQSTYPE_CTIO:
    242 		isp_get_ctio(isp, ctiop, (ct_entry_t *) local);
    243 		isp_handle_ctio(isp, (ct_entry_t *) local);
    244 		break;
    245 
    246 	case RQSTYPE_ATIO2:
    247 		if (ISP_CAP_2KLOGIN(isp)) {
    248 			isp_get_atio2e(isp, at2eiop, (at2e_entry_t *) local);
    249 		} else {
    250 			isp_get_atio2(isp, at2iop, (at2_entry_t *) local);
    251 		}
    252 		isp_handle_atio2(isp, (at2_entry_t *) local);
    253 		break;
    254 
    255 	case RQSTYPE_CTIO3:
    256 	case RQSTYPE_CTIO2:
    257 		if (ISP_CAP_2KLOGIN(isp)) {
    258 			isp_get_ctio2e(isp, ct2eiop, (ct2e_entry_t *) local);
    259 		} else {
    260 			isp_get_ctio2(isp, ct2iop, (ct2_entry_t *) local);
    261 		}
    262 		isp_handle_ctio2(isp, (ct2_entry_t *) local);
    263 		break;
    264 
    265 	case RQSTYPE_CTIO7:
    266 		isp_get_ctio7(isp, ct7iop, (ct7_entry_t *) local);
    267 		isp_handle_ctio7(isp, (ct7_entry_t *) local);
    268 		break;
    269 
    270 	case RQSTYPE_ENABLE_LUN:
    271 	case RQSTYPE_MODIFY_LUN:
    272 		isp_get_enable_lun(isp, lunenp, (lun_entry_t *) local);
    273 		isp_async(isp, ISPASYNC_TARGET_ACTION, local);
    274 		break;
    275 
    276 	case RQSTYPE_NOTIFY:
    277 		bus = 0;
    278 		if (IS_24XX(isp)) {
    279 			isp_get_notify_24xx(isp, inot_24xx, (in_fcentry_24xx_t *)local);
    280 			inot_24xx = (in_fcentry_24xx_t *) local;
    281 			isp_handle_24xx_inotify(isp, inot_24xx);
    282 			break;
    283 		}
    284 		if (IS_FC(isp)) {
    285 			if (ISP_CAP_2KLOGIN(isp)) {
    286 				in_fcentry_e_t *ecp = (in_fcentry_e_t *)local;
    287 				isp_get_notify_fc_e(isp, inote_fcp, ecp);
    288 				iid = ecp->in_iid;
    289 				status = ecp->in_status;
    290 				seqid = ecp->in_seqid;
    291 			} else {
    292 				in_fcentry_t *fcp = (in_fcentry_t *)local;
    293 				isp_get_notify_fc(isp, inot_fcp, fcp);
    294 				iid = fcp->in_iid;
    295 				status = fcp->in_status;
    296 				seqid = fcp->in_seqid;
    297 			}
    298 		} else {
    299 			in_entry_t *inp = (in_entry_t *)local;
    300 			isp_get_notify(isp, inotp, inp);
    301 			status = inp->in_status & 0xff;
    302 			seqid = inp->in_seqid;
    303 			iid = inp->in_iid;
    304 			if (IS_DUALBUS(isp)) {
    305 				bus = GET_BUS_VAL(inp->in_iid);
    306 				SET_BUS_VAL(inp->in_iid, 0);
    307 			}
    308 		}
    309 
    310 		isp_prt(isp, ISP_LOGTDEBUG0, "Immediate Notify On Bus %d, status=0x%x seqid=0x%x", bus, status, seqid);
    311 
    312 		switch (status) {
    313 		case IN_MSG_RECEIVED:
    314 		case IN_IDE_RECEIVED:
    315 			if (IS_FC(isp)) {
    316 				isp_got_msg_fc(isp, (in_fcentry_t *)local);
    317 			} else {
    318 				isp_got_msg(isp, (in_entry_t *)local);
    319 			}
    320 			break;
    321 		case IN_RSRC_UNAVAIL:
    322 			isp_prt(isp, ISP_LOGINFO, "Firmware out of ATIOs");
    323 			(void) isp_notify_ack(isp, local);
    324 			break;
    325 
    326 		case IN_RESET:
    327 			ISP_MEMZERO(&notify, sizeof (isp_notify_t));
    328 			notify.nt_hba = isp;
    329 			notify.nt_wwn = INI_ANY;
    330 			notify.nt_tgt = TGT_ANY;
    331 			notify.nt_nphdl = iid;
    332 			notify.nt_sid = PORT_ANY;
    333 			notify.nt_did = PORT_ANY;
    334 			notify.nt_lun = LUN_ANY;
    335 			notify.nt_tagval = TAG_ANY;
    336 			notify.nt_tagval |= (((uint64_t)(isp->isp_serno++)) << 32);
    337 			notify.nt_ncode = NT_BUS_RESET;
    338 			notify.nt_need_ack = 1;
    339 			notify.nt_lreserved = local;
    340 			isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
    341 			break;
    342 
    343 		case IN_PORT_LOGOUT:
    344 			ISP_MEMZERO(&notify, sizeof (isp_notify_t));
    345 			notify.nt_hba = isp;
    346 			notify.nt_wwn = INI_ANY;
    347 			notify.nt_nphdl = iid;
    348 			notify.nt_sid = PORT_ANY;
    349 			notify.nt_did = PORT_ANY;
    350 			notify.nt_ncode = NT_LOGOUT;
    351 			notify.nt_need_ack = 1;
    352 			notify.nt_lreserved = local;
    353 			isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
    354 			break;
    355 
    356 		case IN_ABORT_TASK:
    357 			ISP_MEMZERO(&notify, sizeof (isp_notify_t));
    358 			notify.nt_hba = isp;
    359 			notify.nt_wwn = INI_ANY;
    360 			notify.nt_nphdl = iid;
    361 			notify.nt_sid = PORT_ANY;
    362 			notify.nt_did = PORT_ANY;
    363 			notify.nt_ncode = NT_ABORT_TASK;
    364 			notify.nt_need_ack = 1;
    365 			notify.nt_lreserved = local;
    366 			isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
    367 			break;
    368 
    369 		case IN_GLOBAL_LOGO:
    370 			isp_prt(isp, ISP_LOGTINFO, "%s: all ports logged out", __func__);
    371 			ISP_MEMZERO(&notify, sizeof (isp_notify_t));
    372 			notify.nt_hba = isp;
    373 			notify.nt_wwn = INI_ANY;
    374 			notify.nt_nphdl = NIL_HANDLE;
    375 			notify.nt_sid = PORT_ANY;
    376 			notify.nt_did = PORT_ANY;
    377 			notify.nt_ncode = NT_GLOBAL_LOGOUT;
    378 			isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
    379 			(void) isp_notify_ack(isp, local);
    380 			break;
    381 
    382 		case IN_PORT_CHANGED:
    383 			isp_prt(isp, ISP_LOGTINFO, "%s: port changed", __func__);
    384 			(void) isp_notify_ack(isp, local);
    385 			break;
    386 
    387 		default:
    388 			ISP_SNPRINTF(local, sizeof local, "%s: unknown status to RQSTYPE_NOTIFY (0x%x)", __func__, status);
    389 			isp_print_bytes(isp, local, QENTRY_LEN, vptr);
    390 			(void) isp_notify_ack(isp, local);
    391 			break;
    392 		}
    393 		break;
    394 
    395 	case RQSTYPE_NOTIFY_ACK:
    396 		/*
    397 		 * The ISP is acknowledging our acknowledgement of an
    398 		 * Immediate Notify entry for some asynchronous event.
    399 		 */
    400 		if (IS_24XX(isp)) {
    401 			isp_get_notify_ack_24xx(isp, nack_24xx, (na_fcentry_24xx_t *) local);
    402 			nack_24xx = (na_fcentry_24xx_t *) local;
    403 			if (nack_24xx->na_status != NA_OK) {
    404 				level = ISP_LOGINFO;
    405 			} else {
    406 				level = ISP_LOGTDEBUG1;
    407 			}
    408 			isp_prt(isp, level, "Notify Ack Status=0x%x; Subcode 0x%x seqid=0x%x", nack_24xx->na_status, nack_24xx->na_status_subcode, nack_24xx->na_rxid);
    409 		} else if (IS_FC(isp)) {
    410 			if (ISP_CAP_2KLOGIN(isp)) {
    411 				isp_get_notify_ack_fc_e(isp, nacke_fcp, (na_fcentry_e_t *)local);
    412 			} else {
    413 				isp_get_notify_ack_fc(isp, nack_fcp, (na_fcentry_t *)local);
    414 			}
    415 			nack_fcp = (na_fcentry_t *)local;
    416 			if (nack_fcp->na_status != NA_OK) {
    417 				level = ISP_LOGINFO;
    418 			} else {
    419 				level = ISP_LOGTDEBUG1;
    420 			}
    421 			isp_prt(isp, level, "Notify Ack Status=0x%x seqid 0x%x", nack_fcp->na_status, nack_fcp->na_seqid);
    422 		} else {
    423 			isp_get_notify_ack(isp, nackp, (na_entry_t *)local);
    424 			nackp = (na_entry_t *)local;
    425 			if (nackp->na_status != NA_OK) {
    426 				level = ISP_LOGINFO;
    427 			} else {
    428 				level = ISP_LOGTDEBUG1;
    429 			}
    430 			isp_prt(isp, level, "Notify Ack event 0x%x status=0x%x seqid 0x%x", nackp->na_event, nackp->na_status, nackp->na_seqid);
    431 		}
    432 		break;
    433 
    434 	case RQSTYPE_ABTS_RCVD:
    435 		isp_get_abts(isp, abts, (abts_t *)local);
    436 		isp_async(isp, ISPASYNC_TARGET_ACTION, &local);
    437 		break;
    438 	case RQSTYPE_ABTS_RSP:
    439 		isp_get_abts_rsp(isp, abts_rsp, (abts_rsp_t *)local);
    440 		abts_rsp = (abts_rsp_t *) local;
    441 		if (abts_rsp->abts_rsp_status) {
    442 			level = ISP_LOGINFO;
    443 		} else {
    444 			level = ISP_LOGTDEBUG0;
    445 		}
    446 		isp_prt(isp, level, "ABTS RSP response[0x%x]: status=0x%x sub=(0x%x 0x%x)", abts_rsp->abts_rsp_rxid_task, abts_rsp->abts_rsp_status,
    447 		    abts_rsp->abts_rsp_payload.rsp.subcode1, abts_rsp->abts_rsp_payload.rsp.subcode2);
    448 		break;
    449 	default:
    450 		isp_prt(isp, ISP_LOGERR, "%s: unknown entry type 0x%x", __func__, type);
    451 		rval = 0;
    452 		break;
    453 	}
    454 #undef	atiop
    455 #undef	at2iop
    456 #undef	at2eiop
    457 #undef	at7iop
    458 #undef	ctiop
    459 #undef	ct2iop
    460 #undef	ct2eiop
    461 #undef	ct7iop
    462 #undef	lunenp
    463 #undef	inotp
    464 #undef	inot_fcp
    465 #undef	inote_fcp
    466 #undef	inot_24xx
    467 #undef	nackp
    468 #undef	nack_fcp
    469 #undef	nacke_fcp
    470 #undef	hack_24xx
    471 #undef	abts
    472 #undef	abts_rsp
    473 #undef	els
    474 #undef	hdrp
    475 	return (rval);
    476 }
    477 
    478 
    479 /*
    480  * Toggle (on/off) target mode for bus/target/lun.
    481  *
    482  * The caller has checked for overlap and legality.
    483  *
    484  * Note that not all of bus, target or lun can be paid attention to.
    485  * Note also that this action will not be complete until the f/w writes
    486  * a response entry. The caller is responsible for synchronizing with this.
    487  */
    488 int
    489 isp_lun_cmd(ispsoftc_t *isp, int cmd, int bus, int lun, int cmd_cnt, int inot_cnt)
    490 {
    491 	lun_entry_t el;
    492 	void *outp;
    493 
    494 	ISP_MEMZERO(&el, sizeof (el));
    495 	if (IS_DUALBUS(isp)) {
    496 		el.le_rsvd = (bus & 0x1) << 7;
    497 	}
    498 	el.le_cmd_count = cmd_cnt;
    499 	el.le_in_count = inot_cnt;
    500 	if (cmd == RQSTYPE_ENABLE_LUN) {
    501 		if (IS_SCSI(isp)) {
    502 			el.le_flags = LUN_TQAE|LUN_DISAD;
    503 			el.le_cdb6len = 12;
    504 			el.le_cdb7len = 12;
    505 		}
    506 	} else if (cmd == -RQSTYPE_ENABLE_LUN) {
    507 		cmd = RQSTYPE_ENABLE_LUN;
    508 		el.le_cmd_count = 0;
    509 		el.le_in_count = 0;
    510 	} else if (cmd == -RQSTYPE_MODIFY_LUN) {
    511 		cmd = RQSTYPE_MODIFY_LUN;
    512 		el.le_ops = LUN_CCDECR | LUN_INDECR;
    513 	} else {
    514 		el.le_ops = LUN_CCINCR | LUN_ININCR;
    515 	}
    516 	el.le_header.rqs_entry_type = cmd;
    517 	el.le_header.rqs_entry_count = 1;
    518 	if (IS_SCSI(isp)) {
    519 		el.le_tgt = SDPARAM(isp, bus)->isp_initiator_id;
    520 		el.le_lun = lun;
    521 	} else if (ISP_CAP_SCCFW(isp) == 0) {
    522 		el.le_lun = lun;
    523 	}
    524 	el.le_timeout = 30;
    525 
    526 	outp = isp_getrqentry(isp);
    527 	if (outp == NULL) {
    528 		isp_prt(isp, ISP_LOGERR, rqo, __func__);
    529 		return (-1);
    530 	}
    531 	isp_put_enable_lun(isp, &el, outp);
    532 	ISP_TDQE(isp, "isp_lun_cmd", isp->isp_reqidx, &el);
    533 	ISP_SYNC_REQUEST(isp);
    534 	return (0);
    535 }
    536 
    537 int
    538 isp_target_put_entry(ispsoftc_t *isp, void *ap)
    539 {
    540 	void *outp;
    541 	uint8_t etype = ((isphdr_t *) ap)->rqs_entry_type;
    542 
    543 	outp = isp_getrqentry(isp);
    544 	if (outp == NULL) {
    545 		isp_prt(isp, ISP_LOGWARN, rqo, __func__);
    546 		return (-1);
    547 	}
    548 	switch (etype) {
    549 	case RQSTYPE_ATIO:
    550 		isp_put_atio(isp, (at_entry_t *) ap, (at_entry_t *) outp);
    551 		break;
    552 	case RQSTYPE_ATIO2:
    553 		if (ISP_CAP_2KLOGIN(isp)) {
    554 			isp_put_atio2e(isp, (at2e_entry_t *) ap, (at2e_entry_t *) outp);
    555 		} else {
    556 			isp_put_atio2(isp, (at2_entry_t *) ap, (at2_entry_t *) outp);
    557 		}
    558 		break;
    559 	case RQSTYPE_CTIO:
    560 		isp_put_ctio(isp, (ct_entry_t *) ap, (ct_entry_t *) outp);
    561 		break;
    562 	case RQSTYPE_CTIO2:
    563 		if (ISP_CAP_2KLOGIN(isp)) {
    564 			isp_put_ctio2e(isp, (ct2e_entry_t *) ap, (ct2e_entry_t *) outp);
    565 		} else {
    566 			isp_put_ctio2(isp, (ct2_entry_t *) ap, (ct2_entry_t *) outp);
    567 		}
    568 		break;
    569 	case RQSTYPE_CTIO7:
    570 		isp_put_ctio7(isp, (ct7_entry_t *) ap, (ct7_entry_t *) outp);
    571 		break;
    572 	default:
    573 		isp_prt(isp, ISP_LOGERR, "%s: Unknown type 0x%x", __func__, etype);
    574 		return (-1);
    575 	}
    576 	ISP_TDQE(isp, __func__, isp->isp_reqidx, ap);
    577 	ISP_SYNC_REQUEST(isp);
    578 	return (0);
    579 }
    580 
    581 int
    582 isp_target_put_atio(ispsoftc_t *isp, void *arg)
    583 {
    584 	union {
    585 		at_entry_t _atio;
    586 		at2_entry_t _atio2;
    587 		at2e_entry_t _atio2e;
    588 	} atun;
    589 
    590 	ISP_MEMZERO(&atun, sizeof atun);
    591 	if (IS_FC(isp)) {
    592 		at2_entry_t *aep = arg;
    593 		atun._atio2.at_header.rqs_entry_type = RQSTYPE_ATIO2;
    594 		atun._atio2.at_header.rqs_entry_count = 1;
    595 		if (ISP_CAP_SCCFW(isp)) {
    596 			atun._atio2.at_scclun = aep->at_scclun;
    597 		} else {
    598 			atun._atio2.at_lun = (uint8_t) aep->at_lun;
    599 		}
    600 		if (ISP_CAP_2KLOGIN(isp)) {
    601 			atun._atio2e.at_iid = ((at2e_entry_t *)aep)->at_iid;
    602 		} else {
    603 			atun._atio2.at_iid = aep->at_iid;
    604 		}
    605 		atun._atio2.at_rxid = aep->at_rxid;
    606 		atun._atio2.at_status = CT_OK;
    607 	} else {
    608 		at_entry_t *aep = arg;
    609 		atun._atio.at_header.rqs_entry_type = RQSTYPE_ATIO;
    610 		atun._atio.at_header.rqs_entry_count = 1;
    611 		atun._atio.at_handle = aep->at_handle;
    612 		atun._atio.at_iid = aep->at_iid;
    613 		atun._atio.at_tgt = aep->at_tgt;
    614 		atun._atio.at_lun = aep->at_lun;
    615 		atun._atio.at_tag_type = aep->at_tag_type;
    616 		atun._atio.at_tag_val = aep->at_tag_val;
    617 		atun._atio.at_status = (aep->at_flags & AT_TQAE);
    618 		atun._atio.at_status |= CT_OK;
    619 	}
    620 	return (isp_target_put_entry(isp, &atun));
    621 }
    622 
    623 /*
    624  * Command completion- both for handling cases of no resources or
    625  * no blackhole driver, or other cases where we have to, inline,
    626  * finish the command sanely, or for normal command completion.
    627  *
    628  * The 'completion' code value has the scsi status byte in the low 8 bits.
    629  * If status is a CHECK CONDITION and bit 8 is nonzero, then bits 12..15 have
    630  * the sense key and  bits 16..23 have the ASCQ and bits 24..31 have the ASC
    631  * values.
    632  *
    633  * NB: the key, asc, ascq, cannot be used for parallel SCSI as it doesn't
    634  * NB: inline SCSI sense reporting. As such, we lose this information. XXX.
    635  *
    636  * For both parallel && fibre channel, we use the feature that does
    637  * an automatic resource autoreplenish so we don't have then later do
    638  * put of an atio to replenish the f/w's resource count.
    639  */
    640 
    641 int
    642 isp_endcmd(ispsoftc_t *isp, ...)
    643 {
    644 	uint32_t code, hdl;
    645 	uint8_t sts;
    646 	union {
    647 		ct_entry_t _ctio;
    648 		ct2_entry_t _ctio2;
    649 		ct2e_entry_t _ctio2e;
    650 		ct7_entry_t _ctio7;
    651 	} un;
    652 	va_list ap;
    653 
    654 	ISP_MEMZERO(&un, sizeof un);
    655 
    656 	if (IS_24XX(isp)) {
    657 		int vpidx, nphdl;
    658 		at7_entry_t *aep;
    659 		ct7_entry_t *cto = &un._ctio7;
    660 
    661 		va_start(ap, isp);
    662 		aep = va_arg(ap, at7_entry_t *);
    663 		nphdl = va_arg(ap, int);
    664 		/*
    665 		 * Note that vpidx may equal 0xff (unknown) here
    666 		 */
    667 		vpidx = va_arg(ap, int);
    668 		code = va_arg(ap, uint32_t);
    669 		hdl = va_arg(ap, uint32_t);
    670 		va_end(ap);
    671 		isp_prt(isp, ISP_LOGTDEBUG0, "%s: [RX_ID 0x%x] chan %d code %x", __func__, aep->at_rxid, vpidx, code);
    672 
    673 		sts = code & 0xff;
    674 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
    675 		cto->ct_header.rqs_entry_count = 1;
    676 		cto->ct_nphdl = nphdl;
    677 		cto->ct_rxid = aep->at_rxid;
    678 		cto->ct_iid_lo = (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
    679 		cto->ct_iid_hi = aep->at_hdr.s_id[0];
    680 		cto->ct_oxid = aep->at_hdr.ox_id;
    681 		cto->ct_scsi_status = sts;
    682 		cto->ct_vpidx = vpidx;
    683 		cto->ct_flags = CT7_NOACK;
    684 		if (code & ECMD_TERMINATE) {
    685 			cto->ct_flags |= CT7_TERMINATE;
    686 		} else if (code & ECMD_SVALID) {
    687 			cto->ct_flags |= CT7_FLAG_MODE1 | CT7_SENDSTATUS;
    688 			cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8);
    689 			cto->rsp.m1.ct_resplen = cto->ct_senselen = min(16, MAXRESPLEN_24XX);
    690 			ISP_MEMZERO(cto->rsp.m1.ct_resp, sizeof (cto->rsp.m1.ct_resp));
    691 			cto->rsp.m1.ct_resp[0] = 0xf0;
    692 			cto->rsp.m1.ct_resp[2] = (code >> 12) & 0xf;
    693 			cto->rsp.m1.ct_resp[7] = 8;
    694 			cto->rsp.m1.ct_resp[12] = (code >> 24) & 0xff;
    695 			cto->rsp.m1.ct_resp[13] = (code >> 16) & 0xff;
    696 		} else {
    697 			cto->ct_flags |= CT7_FLAG_MODE1 | CT7_SENDSTATUS;
    698 		}
    699 		if (aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl) {
    700 			cto->ct_resid = aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl;
    701 			if (cto->ct_resid < 0) {
    702 				 cto->ct_scsi_status |= (FCP_RESID_OVERFLOW << 8);
    703 			} else if (cto->ct_resid > 0) {
    704 				 cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8);
    705 			}
    706 		}
    707 		cto->ct_syshandle = hdl;
    708 	} else if (IS_FC(isp)) {
    709 		at2_entry_t *aep;
    710 		ct2_entry_t *cto = &un._ctio2;
    711 
    712 		va_start(ap, isp);
    713 		aep = va_arg(ap, at2_entry_t *);
    714 		code = va_arg(ap, uint32_t);
    715 		hdl = va_arg(ap, uint32_t);
    716 		va_end(ap);
    717 
    718 		isp_prt(isp, ISP_LOGTDEBUG0, "%s: [RX_ID 0x%x] code %x", __func__, aep->at_rxid, code);
    719 
    720 		sts = code & 0xff;
    721 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
    722 		cto->ct_header.rqs_entry_count = 1;
    723 		if (ISP_CAP_SCCFW(isp) == 0) {
    724 			cto->ct_lun = aep->at_lun;
    725 		}
    726 		if (ISP_CAP_2KLOGIN(isp)) {
    727 			un._ctio2e.ct_iid = ((at2e_entry_t *)aep)->at_iid;
    728 		} else {
    729 			cto->ct_iid = aep->at_iid;
    730 		}
    731 		cto->ct_rxid = aep->at_rxid;
    732 		cto->rsp.m1.ct_scsi_status = sts;
    733 		cto->ct_flags = CT2_SENDSTATUS | CT2_NO_DATA | CT2_FLAG_MODE1;
    734 		if (hdl == 0) {
    735 			cto->ct_flags |= CT2_CCINCR;
    736 		}
    737 		if (aep->at_datalen) {
    738 			cto->ct_resid = aep->at_datalen;
    739 			cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER;
    740 		}
    741 		if (sts == SCSI_CHECK && (code & ECMD_SVALID)) {
    742 			cto->rsp.m1.ct_resp[0] = 0xf0;
    743 			cto->rsp.m1.ct_resp[2] = (code >> 12) & 0xf;
    744 			cto->rsp.m1.ct_resp[7] = 8;
    745 			cto->rsp.m1.ct_resp[12] = (code >> 24) & 0xff;
    746 			cto->rsp.m1.ct_resp[13] = (code >> 16) & 0xff;
    747 			cto->rsp.m1.ct_senselen = 16;
    748 			cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
    749 		}
    750 		cto->ct_syshandle = hdl;
    751 	} else {
    752 		at_entry_t *aep;
    753 		ct_entry_t *cto = &un._ctio;
    754 
    755 		va_start(ap, isp);
    756 		aep = va_arg(ap, at_entry_t *);
    757 		code = va_arg(ap, uint32_t);
    758 		hdl = va_arg(ap, uint32_t);
    759 		va_end(ap);
    760 		isp_prt(isp, ISP_LOGTDEBUG0, "%s: [IID %d] code %x", __func__, aep->at_iid, code);
    761 		sts = code;
    762 
    763 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO;
    764 		cto->ct_header.rqs_entry_count = 1;
    765 		cto->ct_fwhandle = aep->at_handle;
    766 		cto->ct_iid = aep->at_iid;
    767 		cto->ct_tgt = aep->at_tgt;
    768 		cto->ct_lun = aep->at_lun;
    769 		cto->ct_tag_type = aep->at_tag_type;
    770 		cto->ct_tag_val = aep->at_tag_val;
    771 		if (aep->at_flags & AT_TQAE) {
    772 			cto->ct_flags |= CT_TQAE;
    773 		}
    774 		cto->ct_flags = CT_SENDSTATUS | CT_NO_DATA;
    775 		if (hdl == 0) {
    776 			cto->ct_flags |= CT_CCINCR;
    777 		}
    778 		cto->ct_scsi_status = sts;
    779 		cto->ct_syshandle = hdl;
    780 	}
    781 	return (isp_target_put_entry(isp, &un));
    782 }
    783 
    784 /*
    785  * These are either broadcast events or specifically CTIO fast completion
    786  */
    787 
    788 int
    789 isp_target_async(ispsoftc_t *isp, int bus, int event)
    790 {
    791 	isp_notify_t notify;
    792 
    793 	ISP_MEMZERO(&notify, sizeof (isp_notify_t));
    794 	notify.nt_hba = isp;
    795 	notify.nt_wwn = INI_ANY;
    796 	notify.nt_nphdl = NIL_HANDLE;
    797 	notify.nt_sid = PORT_ANY;
    798 	notify.nt_did = PORT_ANY;
    799 	notify.nt_tgt = TGT_ANY;
    800 	notify.nt_channel = bus;
    801 	notify.nt_lun = LUN_ANY;
    802 	notify.nt_tagval = TAG_ANY;
    803 	notify.nt_tagval |= (((uint64_t)(isp->isp_serno++)) << 32);
    804 
    805 	switch (event) {
    806 	case ASYNC_LOOP_UP:
    807 	case ASYNC_PTPMODE:
    808 		isp_prt(isp, ISP_LOGTDEBUG0, "%s: LOOP UP", __func__);
    809 		notify.nt_ncode = NT_LINK_UP;
    810 		isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
    811 		break;
    812 	case ASYNC_LOOP_DOWN:
    813 		isp_prt(isp, ISP_LOGTDEBUG0, "%s: LOOP DOWN", __func__);
    814 		notify.nt_ncode = NT_LINK_DOWN;
    815 		isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
    816 		break;
    817 	case ASYNC_LIP_ERROR:
    818 	case ASYNC_LIP_F8:
    819 	case ASYNC_LIP_OCCURRED:
    820 	case ASYNC_LOOP_RESET:
    821 		isp_prt(isp, ISP_LOGTDEBUG0, "%s: LIP RESET", __func__);
    822 		notify.nt_ncode = NT_LIP_RESET;
    823 		isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
    824 		break;
    825 	case ASYNC_BUS_RESET:
    826 	case ASYNC_TIMEOUT_RESET:	/* XXX: where does this come from ? */
    827 		isp_prt(isp, ISP_LOGTDEBUG0, "%s: BUS RESET", __func__);
    828 		notify.nt_ncode = NT_BUS_RESET;
    829 		isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
    830 		break;
    831 	case ASYNC_DEVICE_RESET:
    832 		isp_prt(isp, ISP_LOGTDEBUG0, "%s: DEVICE RESET", __func__);
    833 		notify.nt_ncode = NT_TARGET_RESET;
    834 		isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
    835 		break;
    836 	case ASYNC_CTIO_DONE:
    837 	{
    838 		uint8_t storage[QENTRY_LEN];
    839 		isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO DONE", __func__);
    840 		memset(storage, 0, QENTRY_LEN);
    841 		if (IS_24XX(isp)) {
    842 			ct7_entry_t *ct = (ct7_entry_t *) storage;
    843 			ct->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
    844 			ct->ct_nphdl = CT7_OK;
    845 			ct->ct_syshandle = bus;
    846 			ct->ct_flags = CT7_SENDSTATUS;
    847 		} else if (IS_FC(isp)) {
    848             		/* This should also suffice for 2K login code */
    849 			ct2_entry_t *ct = (ct2_entry_t *) storage;
    850 			ct->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
    851 			ct->ct_status = CT_OK;
    852 			ct->ct_syshandle = bus;
    853 			ct->ct_flags = CT2_SENDSTATUS|CT2_FASTPOST;
    854 		} else {
    855 			ct_entry_t *ct = (ct_entry_t *) storage;
    856 			ct->ct_header.rqs_entry_type = RQSTYPE_CTIO;
    857 			ct->ct_status = CT_OK;
    858 			ct->ct_fwhandle = bus;
    859 			ct->ct_flags = CT_SENDSTATUS;
    860 		}
    861 		isp_async(isp, ISPASYNC_TARGET_ACTION, storage);
    862 		break;
    863 	}
    864 	default:
    865 		isp_prt(isp, ISP_LOGERR, "%s: unknown event 0x%x", __func__, event);
    866 		if (isp->isp_state == ISP_RUNSTATE) {
    867 			(void) isp_notify_ack(isp, NULL);
    868 		}
    869 		break;
    870 	}
    871 	return (0);
    872 }
    873 
    874 
    875 /*
    876  * Process a received message.
    877  * The ISP firmware can handle most messages, there are only
    878  * a few that we need to deal with:
    879  * - abort: clean up the current command
    880  * - abort tag and clear queue
    881  */
    882 
    883 static void
    884 isp_got_msg(ispsoftc_t *isp, in_entry_t *inp)
    885 {
    886 	isp_notify_t notify;
    887 	uint8_t status = inp->in_status & ~QLTM_SVALID;
    888 
    889 	ISP_MEMZERO(&notify, sizeof (notify));
    890 	notify.nt_hba = isp;
    891 	notify.nt_wwn = INI_ANY;
    892 	notify.nt_nphdl = GET_IID_VAL(inp->in_iid);
    893 	notify.nt_sid = PORT_ANY;
    894 	notify.nt_did = PORT_ANY;
    895 	notify.nt_channel = GET_BUS_VAL(inp->in_iid);
    896 	notify.nt_tgt = inp->in_tgt;
    897 	notify.nt_lun = inp->in_lun;
    898 	IN_MAKE_TAGID(notify.nt_tagval, inp);
    899 	notify.nt_tagval |= (((uint64_t)(isp->isp_serno++)) << 32);
    900 	notify.nt_lreserved = inp;
    901 
    902 	if (status == IN_IDE_RECEIVED || status == IN_MSG_RECEIVED) {
    903 		switch (inp->in_msg[0]) {
    904 		case MSG_ABORT:
    905 			notify.nt_ncode = NT_ABORT_TASK_SET;
    906 			break;
    907 		case MSG_BUS_DEV_RESET:
    908 			notify.nt_ncode = NT_TARGET_RESET;
    909 			break;
    910 		case MSG_ABORT_TAG:
    911 			notify.nt_ncode = NT_ABORT_TASK;
    912 			break;
    913 		case MSG_CLEAR_QUEUE:
    914 			notify.nt_ncode = NT_CLEAR_TASK_SET;
    915 			break;
    916 		case MSG_REL_RECOVERY:
    917 			notify.nt_ncode = NT_CLEAR_ACA;
    918 			break;
    919 		case MSG_TERM_IO_PROC:
    920 			notify.nt_ncode = NT_ABORT_TASK;
    921 			break;
    922 		case MSG_LUN_RESET:
    923 			notify.nt_ncode = NT_LUN_RESET;
    924 			break;
    925 		default:
    926 			isp_prt(isp, ISP_LOGERR, "%s: unhandled message 0x%x", __func__, inp->in_msg[0]);
    927 			(void) isp_notify_ack(isp, inp);
    928 			return;
    929 		}
    930 		isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
    931 	} else {
    932 		isp_prt(isp, ISP_LOGERR, "%s: unknown immediate notify status 0x%x", __func__, inp->in_status);
    933 		(void) isp_notify_ack(isp, inp);
    934 	}
    935 }
    936 
    937 /*
    938  * Synthesize a message from the task management flags in a FCP_CMND_IU.
    939  */
    940 static void
    941 isp_got_msg_fc(ispsoftc_t *isp, in_fcentry_t *inp)
    942 {
    943 	isp_notify_t notify;
    944 	static const char f1[] = "%s from N-port handle 0x%x lun %d seq 0x%x";
    945 	static const char f2[] = "unknown %s 0x%x lun %d N-Port handle 0x%x task flags 0x%x seq 0x%x\n";
    946 	uint16_t seqid, loopid;
    947 
    948 	ISP_MEMZERO(&notify, sizeof (isp_notify_t));
    949 	notify.nt_hba = isp;
    950 	notify.nt_wwn = INI_ANY;
    951 	if (ISP_CAP_2KLOGIN(isp)) {
    952 		notify.nt_nphdl = ((in_fcentry_e_t *)inp)->in_iid;
    953 		loopid = ((in_fcentry_e_t *)inp)->in_iid;
    954 		seqid = ((in_fcentry_e_t *)inp)->in_seqid;
    955 	} else {
    956 		notify.nt_nphdl = inp->in_iid;
    957 		loopid = inp->in_iid;
    958 		seqid = inp->in_seqid;
    959 	}
    960 	notify.nt_sid = PORT_ANY;
    961 	notify.nt_did = PORT_ANY;
    962 
    963 	/* nt_tgt set in outer layers */
    964 	if (ISP_CAP_SCCFW(isp)) {
    965 		notify.nt_lun = inp->in_scclun;
    966 	} else {
    967 		notify.nt_lun = inp->in_lun;
    968 	}
    969 	notify.nt_tagval = seqid;
    970 	notify.nt_tagval |= (((uint64_t)(isp->isp_serno++)) << 32);
    971 	notify.nt_need_ack = 1;
    972 	notify.nt_lreserved = inp;
    973 
    974 	if (inp->in_status != IN_MSG_RECEIVED) {
    975 		isp_prt(isp, ISP_LOGINFO, f2, "immediate notify status", inp->in_status, notify.nt_lun, loopid, inp->in_task_flags, inp->in_seqid);
    976 		(void) isp_notify_ack(isp, inp);
    977 		return;
    978 	}
    979 
    980 	if (inp->in_task_flags & TASK_FLAGS_ABORT_TASK_SET) {
    981 		isp_prt(isp, ISP_LOGINFO, f1, "ABORT TASK SET", loopid, notify.nt_lun, inp->in_seqid);
    982 		notify.nt_ncode = NT_ABORT_TASK_SET;
    983 	} else if (inp->in_task_flags & TASK_FLAGS_CLEAR_TASK_SET) {
    984 		isp_prt(isp, ISP_LOGINFO, f1, "CLEAR TASK SET", loopid, notify.nt_lun, inp->in_seqid);
    985 		notify.nt_ncode = NT_CLEAR_TASK_SET;
    986 	} else if (inp->in_task_flags & TASK_FLAGS_LUN_RESET) {
    987 		isp_prt(isp, ISP_LOGINFO, f1, "LUN RESET", loopid, notify.nt_lun, inp->in_seqid);
    988 		notify.nt_ncode = NT_LUN_RESET;
    989 	} else if (inp->in_task_flags & TASK_FLAGS_TARGET_RESET) {
    990 		isp_prt(isp, ISP_LOGINFO, f1, "TARGET RESET", loopid, notify.nt_lun, inp->in_seqid);
    991 		notify.nt_ncode = NT_TARGET_RESET;
    992 	} else if (inp->in_task_flags & TASK_FLAGS_CLEAR_ACA) {
    993 		isp_prt(isp, ISP_LOGINFO, f1, "CLEAR ACA", loopid, notify.nt_lun, inp->in_seqid);
    994 		notify.nt_ncode = NT_CLEAR_ACA;
    995 	} else {
    996 		isp_prt(isp, ISP_LOGWARN, f2, "task flag", inp->in_status, notify.nt_lun, loopid, inp->in_task_flags,  inp->in_seqid);
    997 		(void) isp_notify_ack(isp, inp);
    998 		return;
    999 	}
   1000 	isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
   1001 }
   1002 
   1003 static void
   1004 isp_got_tmf_24xx(ispsoftc_t *isp, at7_entry_t *aep)
   1005 {
   1006 	isp_notify_t notify;
   1007 	static const char f1[] = "%s from PortID 0x%06x lun %d seq 0x%08x";
   1008 	static const char f2[] = "unknown Task Flag 0x%x lun %d PortID 0x%x tag 0x%08x";
   1009 	uint16_t chan;
   1010 	uint32_t sid, did;
   1011 
   1012 	ISP_MEMZERO(&notify, sizeof (isp_notify_t));
   1013 	notify.nt_hba = isp;
   1014 	notify.nt_wwn = INI_ANY;
   1015 	notify.nt_lun = (aep->at_cmnd.fcp_cmnd_lun[0] << 8) | (aep->at_cmnd.fcp_cmnd_lun[1]);
   1016 	notify.nt_tagval = aep->at_rxid;
   1017 	notify.nt_tagval |= (((uint64_t)(isp->isp_serno++)) << 32);
   1018 	notify.nt_lreserved = aep;
   1019 	sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] <<  8) | (aep->at_hdr.s_id[2]);
   1020 
   1021 	/* Channel has to derived from D_ID */
   1022 	did = (aep->at_hdr.d_id[0] << 16) | (aep->at_hdr.d_id[1] << 8) | aep->at_hdr.d_id[2];
   1023 	for (chan = 0; chan < isp->isp_nchan; chan++) {
   1024 		if (FCPARAM(isp, chan)->isp_portid == did) {
   1025 			break;
   1026 		}
   1027 	}
   1028 	if (chan == isp->isp_nchan) {
   1029 		isp_prt(isp, ISP_LOGWARN, "%s: D_ID 0x%x not found on any channel", __func__, did);
   1030 		/* just drop on the floor */
   1031 		return;
   1032 	}
   1033 	notify.nt_nphdl = NIL_HANDLE; /* unknown here */
   1034 	notify.nt_sid = sid;
   1035 	notify.nt_did = did;
   1036 	notify.nt_channel = chan;
   1037 	if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_ABORT_TASK_SET) {
   1038 		isp_prt(isp, ISP_LOGINFO, f1, "ABORT TASK SET", sid, notify.nt_lun, aep->at_rxid);
   1039 		notify.nt_ncode = NT_ABORT_TASK_SET;
   1040 	} else if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_CLEAR_TASK_SET) {
   1041 		isp_prt(isp, ISP_LOGINFO, f1, "CLEAR TASK SET", sid, notify.nt_lun, aep->at_rxid);
   1042 		notify.nt_ncode = NT_CLEAR_TASK_SET;
   1043 	} else if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_LUN_RESET) {
   1044 		isp_prt(isp, ISP_LOGINFO, f1, "LUN RESET", sid, notify.nt_lun, aep->at_rxid);
   1045 		notify.nt_ncode = NT_LUN_RESET;
   1046 	} else if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_TGT_RESET) {
   1047 		isp_prt(isp, ISP_LOGINFO, f1, "TARGET RESET", sid, notify.nt_lun, aep->at_rxid);
   1048 		notify.nt_ncode = NT_TARGET_RESET;
   1049 	} else if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_CLEAR_ACA) {
   1050 		isp_prt(isp, ISP_LOGINFO, f1, "CLEAR ACA", sid, notify.nt_lun, aep->at_rxid);
   1051 		notify.nt_ncode = NT_CLEAR_ACA;
   1052 	} else {
   1053 		isp_prt(isp, ISP_LOGWARN, f2, aep->at_cmnd.fcp_cmnd_task_management, notify.nt_lun, sid, aep->at_rxid);
   1054 		notify.nt_ncode = NT_UNKNOWN;
   1055 		return;
   1056 	}
   1057 	isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
   1058 }
   1059 
   1060 int
   1061 isp_notify_ack(ispsoftc_t *isp, void *arg)
   1062 {
   1063 	char storage[QENTRY_LEN];
   1064 	void *outp;
   1065 
   1066 	/*
   1067 	 * This is in case a Task Management Function ends up here.
   1068 	 */
   1069 	if (IS_24XX(isp) && arg != NULL && (((isphdr_t *)arg)->rqs_entry_type == RQSTYPE_ATIO)) {
   1070 		at7_entry_t *aep = arg;
   1071 		return (isp_endcmd(isp, aep, NIL_HANDLE, 0, 0, 0));
   1072 	}
   1073 
   1074 	outp = isp_getrqentry(isp);
   1075 	if (outp == NULL) {
   1076 		isp_prt(isp, ISP_LOGWARN, rqo, __func__);
   1077 		return (1);
   1078 	}
   1079 
   1080 	ISP_MEMZERO(storage, QENTRY_LEN);
   1081 
   1082 	if (IS_24XX(isp)) {
   1083 		na_fcentry_24xx_t *na = (na_fcentry_24xx_t *) storage;
   1084 		if (arg) {
   1085 			in_fcentry_24xx_t *in = arg;
   1086 			na->na_nphdl = in->in_nphdl;
   1087 			na->na_flags = in->in_flags & IN24XX_FLAG_PUREX_IOCB;
   1088 			na->na_status = in->in_status;
   1089 			na->na_status_subcode = in->in_status_subcode;
   1090 			na->na_rxid = in->in_rxid;
   1091 			na->na_oxid = in->in_oxid;
   1092 			na->na_vpidx = in->in_vpidx;
   1093 			if (in->in_status == IN24XX_SRR_RCVD) {
   1094 				na->na_srr_rxid = in->in_srr_rxid;
   1095 				na->na_srr_reloff_hi = in->in_srr_reloff_hi;
   1096 				na->na_srr_reloff_lo = in->in_srr_reloff_lo;
   1097 				na->na_srr_iu = in->in_srr_iu;
   1098 				na->na_srr_flags = 1;
   1099 				na->na_srr_reject_vunique = 0;
   1100 				na->na_srr_reject_explanation = 1;
   1101 				na->na_srr_reject_code = 1;
   1102 			}
   1103 		}
   1104 		na->na_header.rqs_entry_type = RQSTYPE_NOTIFY_ACK;
   1105 		na->na_header.rqs_entry_count = 1;
   1106 		isp_put_notify_24xx_ack(isp, na, (na_fcentry_24xx_t *)outp);
   1107 	} else if (IS_FC(isp)) {
   1108 		na_fcentry_t *na = (na_fcentry_t *) storage;
   1109 		int iid = 0;
   1110 
   1111 		if (arg) {
   1112 			in_fcentry_t *inp = arg;
   1113 			ISP_MEMCPY(storage, arg, sizeof (isphdr_t));
   1114 			if (ISP_CAP_2KLOGIN(isp)) {
   1115 				((na_fcentry_e_t *)na)->na_iid = ((in_fcentry_e_t *)inp)->in_iid;
   1116 				iid = ((na_fcentry_e_t *)na)->na_iid;
   1117 			} else {
   1118 				na->na_iid = inp->in_iid;
   1119 				iid = na->na_iid;
   1120 			}
   1121 			na->na_task_flags = inp->in_task_flags & TASK_FLAGS_RESERVED_MASK;
   1122 			na->na_seqid = inp->in_seqid;
   1123 			na->na_flags = NAFC_RCOUNT;
   1124 			na->na_status = inp->in_status;
   1125 			if (inp->in_status == IN_RESET) {
   1126 				na->na_flags |= NAFC_RST_CLRD;
   1127 			}
   1128 			if (inp->in_status == IN_MSG_RECEIVED) {
   1129 				na->na_flags |= NAFC_TVALID;
   1130 				na->na_response = 0;	/* XXX SUCCEEDED XXX */
   1131 			}
   1132 		} else {
   1133 			na->na_flags = NAFC_RST_CLRD;
   1134 		}
   1135 		na->na_header.rqs_entry_type = RQSTYPE_NOTIFY_ACK;
   1136 		na->na_header.rqs_entry_count = 1;
   1137 		if (ISP_CAP_2KLOGIN(isp)) {
   1138 			isp_put_notify_ack_fc_e(isp, (na_fcentry_e_t *) na, (na_fcentry_e_t *)outp);
   1139 		} else {
   1140 			isp_put_notify_ack_fc(isp, na, (na_fcentry_t *)outp);
   1141 		}
   1142 		isp_prt(isp, ISP_LOGTDEBUG0, "notify ack loopid %u seqid %x flags %x tflags %x response %x", iid, na->na_seqid,
   1143 		    na->na_flags, na->na_task_flags, na->na_response);
   1144 	} else {
   1145 		na_entry_t *na = (na_entry_t *) storage;
   1146 		if (arg) {
   1147 			in_entry_t *inp = arg;
   1148 			ISP_MEMCPY(storage, arg, sizeof (isphdr_t));
   1149 			na->na_iid = inp->in_iid;
   1150 			na->na_lun = inp->in_lun;
   1151 			na->na_tgt = inp->in_tgt;
   1152 			na->na_seqid = inp->in_seqid;
   1153 			if (inp->in_status == IN_RESET) {
   1154 				na->na_event = NA_RST_CLRD;
   1155 			}
   1156 		} else {
   1157 			na->na_event = NA_RST_CLRD;
   1158 		}
   1159 		na->na_header.rqs_entry_type = RQSTYPE_NOTIFY_ACK;
   1160 		na->na_header.rqs_entry_count = 1;
   1161 		isp_put_notify_ack(isp, na, (na_entry_t *)outp);
   1162 		isp_prt(isp, ISP_LOGTDEBUG0, "notify ack loopid %u lun %u tgt %u seqid %x event %x", na->na_iid, na->na_lun, na->na_tgt, na->na_seqid, na->na_event);
   1163 	}
   1164 	ISP_TDQE(isp, "isp_notify_ack", isp->isp_reqidx, storage);
   1165 	ISP_SYNC_REQUEST(isp);
   1166 	return (0);
   1167 }
   1168 
   1169 int
   1170 isp_acknak_abts(ispsoftc_t *isp, void *arg, int errno)
   1171 {
   1172 	char storage[QENTRY_LEN];
   1173 	uint16_t tmpw;
   1174 	uint8_t tmpb;
   1175 	abts_t *abts = arg;
   1176 	abts_rsp_t *rsp = (abts_rsp_t *) storage;
   1177 	void *outp;
   1178 
   1179 	if (!IS_24XX(isp)) {
   1180 		isp_prt(isp, ISP_LOGERR, "%s: called for non-24XX card", __func__);
   1181 		return (0);
   1182 	}
   1183 
   1184 	if (abts->abts_header.rqs_entry_type != RQSTYPE_ABTS_RCVD) {
   1185 		isp_prt(isp, ISP_LOGERR, "%s: called for non-ABTS entry (0x%x)", __func__, abts->abts_header.rqs_entry_type);
   1186 		return (0);
   1187 	}
   1188 
   1189 	outp = isp_getrqentry(isp);
   1190 	if (outp == NULL) {
   1191 		isp_prt(isp, ISP_LOGWARN, rqo, __func__);
   1192 		return (1);
   1193 	}
   1194 
   1195 	ISP_MEMCPY(rsp, abts, QENTRY_LEN);
   1196 	rsp->abts_rsp_header.rqs_entry_type = RQSTYPE_ABTS_RSP;
   1197 
   1198 	/*
   1199 	 * Swap destination and source for response.
   1200 	 */
   1201 	rsp->abts_rsp_r_ctl = BA_ACC;
   1202 	tmpw = rsp->abts_rsp_did_lo;
   1203 	tmpb = rsp->abts_rsp_did_hi;
   1204 	rsp->abts_rsp_did_lo = rsp->abts_rsp_sid_lo;
   1205 	rsp->abts_rsp_did_hi = rsp->abts_rsp_sid_hi;
   1206 	rsp->abts_rsp_sid_lo = tmpw;
   1207 	rsp->abts_rsp_sid_hi = tmpb;
   1208 
   1209 	rsp->abts_rsp_f_ctl_hi ^= 0x80; 	/* invert Exchange Context */
   1210 	rsp->abts_rsp_f_ctl_hi &= ~0x7f;	/* clear Sequence Initiator and other bits */
   1211 	rsp->abts_rsp_f_ctl_hi |= 0x10;		/* abort the whole exchange */
   1212 	rsp->abts_rsp_f_ctl_hi |= 0x8;		/* last data frame of sequence */
   1213 	rsp->abts_rsp_f_ctl_hi |= 0x1;		/* transfer Sequence Initiative */
   1214 	rsp->abts_rsp_f_ctl_lo = 0;
   1215 
   1216 	if (errno == 0) {
   1217 		uint16_t rx_id, ox_id;
   1218 
   1219 		rx_id = rsp->abts_rsp_rx_id;
   1220 		ox_id = rsp->abts_rsp_ox_id;
   1221 		ISP_MEMZERO(&rsp->abts_rsp_payload.ba_acc, sizeof (rsp->abts_rsp_payload.ba_acc));
   1222                 isp_prt(isp, ISP_LOGTINFO, "[0x%x] ABTS of 0x%x being BA_ACC'd", rsp->abts_rsp_rxid_abts, rsp->abts_rsp_rxid_task);
   1223                 rsp->abts_rsp_payload.ba_acc.aborted_rx_id = rx_id;
   1224                 rsp->abts_rsp_payload.ba_acc.aborted_ox_id = ox_id;
   1225                 rsp->abts_rsp_payload.ba_acc.high_seq_cnt = 0xffff;
   1226 	} else {
   1227 		ISP_MEMZERO(&rsp->abts_rsp_payload.ba_rjt, sizeof (rsp->abts_rsp_payload.ba_acc));
   1228 		switch (errno) {
   1229 		case ENOMEM:
   1230 			rsp->abts_rsp_payload.ba_rjt.reason = 5;	/* Logical Busy */
   1231 			break;
   1232 		default:
   1233 			rsp->abts_rsp_payload.ba_rjt.reason = 9;	/* Unable to perform command request */
   1234 			break;
   1235 		}
   1236 	}
   1237 
   1238 	/*
   1239 	 * The caller will have set response values as appropriate
   1240 	 * in the ABTS structure just before calling us.
   1241 	 */
   1242 	isp_put_abts_rsp(isp, rsp, (abts_rsp_t *)outp);
   1243 	ISP_TDQE(isp, "isp_acknak_abts", isp->isp_reqidx, storage);
   1244 	ISP_SYNC_REQUEST(isp);
   1245 	return (0);
   1246 }
   1247 
   1248 static void
   1249 isp_handle_atio(ispsoftc_t *isp, at_entry_t *aep)
   1250 {
   1251 	int lun;
   1252 	lun = aep->at_lun;
   1253 	/*
   1254 	 * The firmware status (except for the QLTM_SVALID bit) indicates
   1255 	 * why this ATIO was sent to us.
   1256 	 *
   1257 	 * If QLTM_SVALID is set, the firware has recommended Sense Data.
   1258 	 *
   1259 	 * If the DISCONNECTS DISABLED bit is set in the flags field,
   1260 	 * we're still connected on the SCSI bus - i.e. the initiator
   1261 	 * did not set DiscPriv in the identify message. We don't care
   1262 	 * about this so it's ignored.
   1263 	 */
   1264 
   1265 	switch (aep->at_status & ~QLTM_SVALID) {
   1266 	case AT_PATH_INVALID:
   1267 		/*
   1268 		 * ATIO rejected by the firmware due to disabled lun.
   1269 		 */
   1270 		isp_prt(isp, ISP_LOGERR, "rejected ATIO for disabled lun %d", lun);
   1271 		break;
   1272 	case AT_NOCAP:
   1273 		/*
   1274 		 * Requested Capability not available
   1275 		 * We sent an ATIO that overflowed the firmware's
   1276 		 * command resource count.
   1277 		 */
   1278 		isp_prt(isp, ISP_LOGERR, "rejected ATIO for lun %d because of command count overflow", lun);
   1279 		break;
   1280 
   1281 	case AT_BDR_MSG:
   1282 		/*
   1283 		 * If we send an ATIO to the firmware to increment
   1284 		 * its command resource count, and the firmware is
   1285 		 * recovering from a Bus Device Reset, it returns
   1286 		 * the ATIO with this status. We set the command
   1287 		 * resource count in the Enable Lun entry and do
   1288 		 * not increment it. Therefore we should never get
   1289 		 * this status here.
   1290 		 */
   1291 		isp_prt(isp, ISP_LOGERR, atiocope, lun, GET_BUS_VAL(aep->at_iid));
   1292 		break;
   1293 
   1294 	case AT_CDB:		/* Got a CDB */
   1295 	case AT_PHASE_ERROR:	/* Bus Phase Sequence Error */
   1296 		/*
   1297 		 * Punt to platform specific layer.
   1298 		 */
   1299 		isp_async(isp, ISPASYNC_TARGET_ACTION, aep);
   1300 		break;
   1301 
   1302 	case AT_RESET:
   1303 		/*
   1304 		 * A bus reset came along and blew away this command. Why
   1305 		 * they do this in addition the async event code stuff,
   1306 		 * I dunno.
   1307 		 *
   1308 		 * Ignore it because the async event will clear things
   1309 		 * up for us.
   1310 		 */
   1311 		isp_prt(isp, ISP_LOGWARN, atior, lun, GET_IID_VAL(aep->at_iid), GET_BUS_VAL(aep->at_iid));
   1312 		break;
   1313 
   1314 
   1315 	default:
   1316 		isp_prt(isp, ISP_LOGERR, "Unknown ATIO status 0x%x from loopid %d for lun %d", aep->at_status, aep->at_iid, lun);
   1317 		(void) isp_target_put_atio(isp, aep);
   1318 		break;
   1319 	}
   1320 }
   1321 
   1322 static void
   1323 isp_handle_atio2(ispsoftc_t *isp, at2_entry_t *aep)
   1324 {
   1325 	int lun, iid;
   1326 
   1327 	if (ISP_CAP_SCCFW(isp)) {
   1328 		lun = aep->at_scclun;
   1329 	} else {
   1330 		lun = aep->at_lun;
   1331 	}
   1332 
   1333 	if (ISP_CAP_2KLOGIN(isp)) {
   1334 		iid = ((at2e_entry_t *)aep)->at_iid;
   1335 	} else {
   1336 		iid = aep->at_iid;
   1337 	}
   1338 
   1339 	/*
   1340 	 * The firmware status (except for the QLTM_SVALID bit) indicates
   1341 	 * why this ATIO was sent to us.
   1342 	 *
   1343 	 * If QLTM_SVALID is set, the firware has recommended Sense Data.
   1344 	 *
   1345 	 * If the DISCONNECTS DISABLED bit is set in the flags field,
   1346 	 * we're still connected on the SCSI bus - i.e. the initiator
   1347 	 * did not set DiscPriv in the identify message. We don't care
   1348 	 * about this so it's ignored.
   1349 	 */
   1350 
   1351 	switch (aep->at_status & ~QLTM_SVALID) {
   1352 	case AT_PATH_INVALID:
   1353 		/*
   1354 		 * ATIO rejected by the firmware due to disabled lun.
   1355 		 */
   1356 		isp_prt(isp, ISP_LOGERR, "rejected ATIO2 for disabled lun %d", lun);
   1357 		break;
   1358 	case AT_NOCAP:
   1359 		/*
   1360 		 * Requested Capability not available
   1361 		 * We sent an ATIO that overflowed the firmware's
   1362 		 * command resource count.
   1363 		 */
   1364 		isp_prt(isp, ISP_LOGERR, "rejected ATIO2 for lun %d- command count overflow", lun);
   1365 		break;
   1366 
   1367 	case AT_BDR_MSG:
   1368 		/*
   1369 		 * If we send an ATIO to the firmware to increment
   1370 		 * its command resource count, and the firmware is
   1371 		 * recovering from a Bus Device Reset, it returns
   1372 		 * the ATIO with this status. We set the command
   1373 		 * resource count in the Enable Lun entry and no
   1374 		 * not increment it. Therefore we should never get
   1375 		 * this status here.
   1376 		 */
   1377 		isp_prt(isp, ISP_LOGERR, atiocope, lun, 0);
   1378 		break;
   1379 
   1380 	case AT_CDB:		/* Got a CDB */
   1381 		/*
   1382 		 * Punt to platform specific layer.
   1383 		 */
   1384 		isp_async(isp, ISPASYNC_TARGET_ACTION, aep);
   1385 		break;
   1386 
   1387 	case AT_RESET:
   1388 		/*
   1389 		 * A bus reset came along an blew away this command. Why
   1390 		 * they do this in addition the async event code stuff,
   1391 		 * I dunno.
   1392 		 *
   1393 		 * Ignore it because the async event will clear things
   1394 		 * up for us.
   1395 		 */
   1396 		isp_prt(isp, ISP_LOGERR, atior, lun, iid, 0);
   1397 		break;
   1398 
   1399 
   1400 	default:
   1401 		isp_prt(isp, ISP_LOGERR, "Unknown ATIO2 status 0x%x from loopid %d for lun %d", aep->at_status, iid, lun);
   1402 		(void) isp_target_put_atio(isp, aep);
   1403 		break;
   1404 	}
   1405 }
   1406 
   1407 static void
   1408 isp_handle_ctio(ispsoftc_t *isp, ct_entry_t *ct)
   1409 {
   1410 	void *xs;
   1411 	int pl = ISP_LOGTDEBUG2;
   1412 	char *fmsg = NULL;
   1413 
   1414 	if (ct->ct_syshandle) {
   1415 		xs = isp_find_xs_tgt(isp, ct->ct_syshandle);
   1416 		if (xs == NULL) {
   1417 			pl = ISP_LOGALL;
   1418 		}
   1419 	} else {
   1420 		xs = NULL;
   1421 	}
   1422 
   1423 	switch (ct->ct_status & ~QLTM_SVALID) {
   1424 	case CT_OK:
   1425 		/*
   1426 		 * There are generally 3 possibilities as to why we'd get
   1427 		 * this condition:
   1428 		 * 	We disconnected after receiving a CDB.
   1429 		 * 	We sent or received data.
   1430 		 * 	We sent status & command complete.
   1431 		 */
   1432 
   1433 		if (ct->ct_flags & CT_SENDSTATUS) {
   1434 			break;
   1435 		} else if ((ct->ct_flags & CT_DATAMASK) == CT_NO_DATA) {
   1436 			/*
   1437 			 * Nothing to do in this case.
   1438 			 */
   1439 			isp_prt(isp, pl, "CTIO- iid %d disconnected OK", ct->ct_iid);
   1440 			return;
   1441 		}
   1442 		break;
   1443 
   1444 	case CT_BDR_MSG:
   1445 		/*
   1446 		 * Bus Device Reset message received or the SCSI Bus has
   1447 		 * been Reset; the firmware has gone to Bus Free.
   1448 		 *
   1449 		 * The firmware generates an async mailbox interrupt to
   1450 		 * notify us of this and returns outstanding CTIOs with this
   1451 		 * status. These CTIOs are handled in that same way as
   1452 		 * CT_ABORTED ones, so just fall through here.
   1453 		 */
   1454 		fmsg = "Bus Device Reset";
   1455 		/*FALLTHROUGH*/
   1456 	case CT_RESET:
   1457 		if (fmsg == NULL)
   1458 			fmsg = "Bus Reset";
   1459 		/*FALLTHROUGH*/
   1460 	case CT_ABORTED:
   1461 		/*
   1462 		 * When an Abort message is received the firmware goes to
   1463 		 * Bus Free and returns all outstanding CTIOs with the status
   1464 		 * set, then sends us an Immediate Notify entry.
   1465 		 */
   1466 		if (fmsg == NULL)
   1467 			fmsg = "ABORT TAG message sent by Initiator";
   1468 		isp_prt(isp, ISP_LOGTDEBUG0, "CTIO destroyed by %s", fmsg);
   1469 		break;
   1470 
   1471 	case CT_INVAL:
   1472 		/*
   1473 		 * CTIO rejected by the firmware due to disabled lun.
   1474 		 * "Cannot Happen".
   1475 		 */
   1476 		isp_prt(isp, ISP_LOGERR, "Firmware rejected CTIO for disabled lun %d", ct->ct_lun);
   1477 		break;
   1478 
   1479 	case CT_NOPATH:
   1480 		/*
   1481 		 * CTIO rejected by the firmware due "no path for the
   1482 		 * nondisconnecting nexus specified". This means that
   1483 		 * we tried to access the bus while a non-disconnecting
   1484 		 * command is in process.
   1485 		 */
   1486 		isp_prt(isp, ISP_LOGERR, "Firmware rejected CTIO for bad nexus %d/%d/%d", ct->ct_iid, ct->ct_tgt, ct->ct_lun);
   1487 		break;
   1488 
   1489 	case CT_RSELTMO:
   1490 		fmsg = "Reselection";
   1491 		/*FALLTHROUGH*/
   1492 	case CT_TIMEOUT:
   1493 		if (fmsg == NULL)
   1494 			fmsg = "Command";
   1495 		isp_prt(isp, ISP_LOGWARN, "Firmware timed out on %s", fmsg);
   1496 		break;
   1497 
   1498 	case	CT_PANIC:
   1499 		if (fmsg == NULL)
   1500 			fmsg = "Unrecoverable Error";
   1501 		/*FALLTHROUGH*/
   1502 	case CT_ERR:
   1503 		if (fmsg == NULL)
   1504 			fmsg = "Completed with Error";
   1505 		/*FALLTHROUGH*/
   1506 	case CT_PHASE_ERROR:
   1507 		if (fmsg == NULL)
   1508 			fmsg = "Phase Sequence Error";
   1509 		/*FALLTHROUGH*/
   1510 	case CT_TERMINATED:
   1511 		if (fmsg == NULL)
   1512 			fmsg = "terminated by TERMINATE TRANSFER";
   1513 		/*FALLTHROUGH*/
   1514 	case CT_NOACK:
   1515 		if (fmsg == NULL)
   1516 			fmsg = "unacknowledged Immediate Notify pending";
   1517 		isp_prt(isp, ISP_LOGERR, "CTIO returned by f/w- %s", fmsg);
   1518 		break;
   1519 	default:
   1520 		isp_prt(isp, ISP_LOGERR, "Unknown CTIO status 0x%x", ct->ct_status & ~QLTM_SVALID);
   1521 		break;
   1522 	}
   1523 
   1524 	if (xs == NULL) {
   1525 		/*
   1526 		 * There may be more than one CTIO for a data transfer,
   1527 		 * or this may be a status CTIO we're not monitoring.
   1528 		 *
   1529 		 * The assumption is that they'll all be returned in the
   1530 		 * order we got them.
   1531 		 */
   1532 		if (ct->ct_syshandle == 0) {
   1533 			if ((ct->ct_flags & CT_SENDSTATUS) == 0) {
   1534 				isp_prt(isp, pl, "intermediate CTIO completed ok");
   1535 			} else {
   1536 				isp_prt(isp, pl, "unmonitored CTIO completed ok");
   1537 			}
   1538 		} else {
   1539 			isp_prt(isp, pl, "NO xs for CTIO (handle 0x%x) status 0x%x", ct->ct_syshandle, ct->ct_status & ~QLTM_SVALID);
   1540 		}
   1541 	} else {
   1542 		/*
   1543 		 * Final CTIO completed. Release DMA resources and
   1544 		 * notify platform dependent layers.
   1545 		 */
   1546 		if ((ct->ct_flags & CT_DATAMASK) != CT_NO_DATA) {
   1547 			ISP_DMAFREE(isp, xs, ct->ct_syshandle);
   1548 		}
   1549 		isp_prt(isp, pl, "final CTIO complete");
   1550 		/*
   1551 		 * The platform layer will destroy the handle if appropriate.
   1552 		 */
   1553 		isp_async(isp, ISPASYNC_TARGET_ACTION, ct);
   1554 	}
   1555 }
   1556 
   1557 static void
   1558 isp_handle_ctio2(ispsoftc_t *isp, ct2_entry_t *ct)
   1559 {
   1560 	void *xs;
   1561 	int pl = ISP_LOGTDEBUG2;
   1562 	char *fmsg = NULL;
   1563 
   1564 	if (ct->ct_syshandle) {
   1565 		xs = isp_find_xs_tgt(isp, ct->ct_syshandle);
   1566 		if (xs == NULL) {
   1567 			pl = ISP_LOGALL;
   1568 		}
   1569 	} else {
   1570 		xs = NULL;
   1571 	}
   1572 
   1573 	switch (ct->ct_status & ~QLTM_SVALID) {
   1574 	case CT_BUS_ERROR:
   1575 		isp_prt(isp, ISP_LOGERR, "PCI DMA Bus Error");
   1576 		/* FALL Through */
   1577 	case CT_DATA_OVER:
   1578 	case CT_DATA_UNDER:
   1579 	case CT_OK:
   1580 		/*
   1581 		 * There are generally 2 possibilities as to why we'd get
   1582 		 * this condition:
   1583 		 * 	We sent or received data.
   1584 		 * 	We sent status & command complete.
   1585 		 */
   1586 
   1587 		break;
   1588 
   1589 	case CT_BDR_MSG:
   1590 		/*
   1591 		 * Target Reset function received.
   1592 		 *
   1593 		 * The firmware generates an async mailbox interrupt to
   1594 		 * notify us of this and returns outstanding CTIOs with this
   1595 		 * status. These CTIOs are handled in that same way as
   1596 		 * CT_ABORTED ones, so just fall through here.
   1597 		 */
   1598 		fmsg = "TARGET RESET";
   1599 		/*FALLTHROUGH*/
   1600 	case CT_RESET:
   1601 		if (fmsg == NULL)
   1602 			fmsg = "LIP Reset";
   1603 		/*FALLTHROUGH*/
   1604 	case CT_ABORTED:
   1605 		/*
   1606 		 * When an Abort message is received the firmware goes to
   1607 		 * Bus Free and returns all outstanding CTIOs with the status
   1608 		 * set, then sends us an Immediate Notify entry.
   1609 		 */
   1610 		if (fmsg == NULL) {
   1611 			fmsg = "ABORT";
   1612 		}
   1613 
   1614 		isp_prt(isp, ISP_LOGTDEBUG0, "CTIO2 destroyed by %s: RX_ID=0x%x", fmsg, ct->ct_rxid);
   1615 		break;
   1616 
   1617 	case CT_INVAL:
   1618 		/*
   1619 		 * CTIO rejected by the firmware - invalid data direction.
   1620 		 */
   1621 		isp_prt(isp, ISP_LOGERR, "CTIO2 had wrong data direction");
   1622 		break;
   1623 
   1624 	case CT_RSELTMO:
   1625 		fmsg = "failure to reconnect to initiator";
   1626 		/*FALLTHROUGH*/
   1627 	case CT_TIMEOUT:
   1628 		if (fmsg == NULL)
   1629 			fmsg = "command";
   1630 		isp_prt(isp, ISP_LOGWARN, "Firmware timed out on %s", fmsg);
   1631 		break;
   1632 
   1633 	case CT_ERR:
   1634 		fmsg = "Completed with Error";
   1635 		/*FALLTHROUGH*/
   1636 	case CT_LOGOUT:
   1637 		if (fmsg == NULL)
   1638 			fmsg = "Port Logout";
   1639 		/*FALLTHROUGH*/
   1640 	case CT_PORTUNAVAIL:
   1641 		if (fmsg == NULL)
   1642 			fmsg = "Port not available";
   1643 		/*FALLTHROUGH*/
   1644 	case CT_PORTCHANGED:
   1645 		if (fmsg == NULL)
   1646 			fmsg = "Port Changed";
   1647 		/*FALLTHROUGH*/
   1648 	case CT_NOACK:
   1649 		if (fmsg == NULL)
   1650 			fmsg = "unacknowledged Immediate Notify pending";
   1651 		isp_prt(isp, ISP_LOGWARN, "CTIO returned by f/w- %s", fmsg);
   1652 		break;
   1653 
   1654 	case CT_INVRXID:
   1655 		/*
   1656 		 * CTIO rejected by the firmware because an invalid RX_ID.
   1657 		 * Just print a message.
   1658 		 */
   1659 		isp_prt(isp, ISP_LOGWARN, "CTIO2 completed with Invalid RX_ID 0x%x", ct->ct_rxid);
   1660 		break;
   1661 
   1662 	default:
   1663 		isp_prt(isp, ISP_LOGERR, "Unknown CTIO2 status 0x%x", ct->ct_status & ~QLTM_SVALID);
   1664 		break;
   1665 	}
   1666 
   1667 	if (xs == NULL) {
   1668 		/*
   1669 		 * There may be more than one CTIO for a data transfer,
   1670 		 * or this may be a status CTIO we're not monitoring.
   1671 		 *
   1672 		 * The assumption is that they'll all be returned in the
   1673 		 * order we got them.
   1674 		 */
   1675 		if (ct->ct_syshandle == 0) {
   1676 			if ((ct->ct_flags & CT2_SENDSTATUS) == 0) {
   1677 				isp_prt(isp, pl, "intermediate CTIO completed ok");
   1678 			} else {
   1679 				isp_prt(isp, pl, "unmonitored CTIO completed ok");
   1680 			}
   1681 		} else {
   1682 			isp_prt(isp, pl, "NO xs for CTIO (handle 0x%x) status 0x%x", ct->ct_syshandle, ct->ct_status & ~QLTM_SVALID);
   1683 		}
   1684 	} else {
   1685 		if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA) {
   1686 			ISP_DMAFREE(isp, xs, ct->ct_syshandle);
   1687 		}
   1688 		if (ct->ct_flags & CT2_SENDSTATUS) {
   1689 			/*
   1690 			 * Sent status and command complete.
   1691 			 *
   1692 			 * We're now really done with this command, so we
   1693 			 * punt to the platform dependent layers because
   1694 			 * only there can we do the appropriate command
   1695 			 * complete thread synchronization.
   1696 			 */
   1697 			isp_prt(isp, pl, "status CTIO complete");
   1698 		} else {
   1699 			/*
   1700 			 * Final CTIO completed. Release DMA resources and
   1701 			 * notify platform dependent layers.
   1702 			 */
   1703 			isp_prt(isp, pl, "data CTIO complete");
   1704 		}
   1705 		isp_async(isp, ISPASYNC_TARGET_ACTION, ct);
   1706 		/*
   1707 		 * The platform layer will destroy the handle if appropriate.
   1708 		 */
   1709 	}
   1710 }
   1711 
   1712 static void
   1713 isp_handle_ctio7(ispsoftc_t *isp, ct7_entry_t *ct)
   1714 {
   1715 	void *xs;
   1716 	int pl = ISP_LOGTDEBUG2;
   1717 	char *fmsg = NULL;
   1718 
   1719 	if (ct->ct_syshandle) {
   1720 		xs = isp_find_xs_tgt(isp, ct->ct_syshandle);
   1721 		if (xs == NULL) {
   1722 			pl = ISP_LOGALL;
   1723 		}
   1724 	} else {
   1725 		xs = NULL;
   1726 	}
   1727 
   1728 	switch (ct->ct_nphdl) {
   1729 	case CT7_BUS_ERROR:
   1730 		isp_prt(isp, ISP_LOGERR, "PCI DMA Bus Error");
   1731 		/* FALL Through */
   1732 	case CT7_DATA_OVER:
   1733 	case CT7_DATA_UNDER:
   1734 	case CT7_OK:
   1735 		/*
   1736 		 * There are generally 2 possibilities as to why we'd get
   1737 		 * this condition:
   1738 		 * 	We sent or received data.
   1739 		 * 	We sent status & command complete.
   1740 		 */
   1741 
   1742 		break;
   1743 
   1744 	case CT7_RESET:
   1745 		if (fmsg == NULL) {
   1746 			fmsg = "LIP Reset";
   1747 		}
   1748 		/*FALLTHROUGH*/
   1749 	case CT7_ABORTED:
   1750 		/*
   1751 		 * When an Abort message is received the firmware goes to
   1752 		 * Bus Free and returns all outstanding CTIOs with the status
   1753 		 * set, then sends us an Immediate Notify entry.
   1754 		 */
   1755 		if (fmsg == NULL) {
   1756 			fmsg = "ABORT";
   1757 		}
   1758 		isp_prt(isp, ISP_LOGTDEBUG0, "CTIO7 destroyed by %s: RX_ID=0x%x", fmsg, ct->ct_rxid);
   1759 		break;
   1760 
   1761 	case CT7_TIMEOUT:
   1762 		if (fmsg == NULL) {
   1763 			fmsg = "command";
   1764 		}
   1765 		isp_prt(isp, ISP_LOGWARN, "Firmware timed out on %s", fmsg);
   1766 		break;
   1767 
   1768 	case CT7_ERR:
   1769 		fmsg = "Completed with Error";
   1770 		/*FALLTHROUGH*/
   1771 	case CT7_LOGOUT:
   1772 		if (fmsg == NULL) {
   1773 			fmsg = "Port Logout";
   1774 		}
   1775 		/*FALLTHROUGH*/
   1776 	case CT7_PORTUNAVAIL:
   1777 		if (fmsg == NULL) {
   1778 			fmsg = "Port not available";
   1779 		}
   1780 		/*FALLTHROUGH*/
   1781 	case CT7_PORTCHANGED:
   1782 		if (fmsg == NULL) {
   1783 			fmsg = "Port Changed";
   1784 		}
   1785 		isp_prt(isp, ISP_LOGWARN, "CTIO returned by f/w- %s", fmsg);
   1786 		break;
   1787 
   1788 	case CT7_INVRXID:
   1789 		/*
   1790 		 * CTIO rejected by the firmware because an invalid RX_ID.
   1791 		 * Just print a message.
   1792 		 */
   1793 		isp_prt(isp, ISP_LOGWARN, "CTIO7 completed with Invalid RX_ID 0x%x", ct->ct_rxid);
   1794 		break;
   1795 
   1796 	case CT7_REASSY_ERR:
   1797 		isp_prt(isp, ISP_LOGWARN, "reassembly error");
   1798 		break;
   1799 
   1800 	case CT7_SRR:
   1801 		isp_prt(isp, ISP_LOGWARN, "SRR received");
   1802 		break;
   1803 
   1804 	default:
   1805 		isp_prt(isp, ISP_LOGERR, "Unknown CTIO7 status 0x%x", ct->ct_nphdl);
   1806 		break;
   1807 	}
   1808 
   1809 	if (xs == NULL) {
   1810 		/*
   1811 		 * There may be more than one CTIO for a data transfer,
   1812 		 * or this may be a status CTIO we're not monitoring.
   1813 		 *
   1814 		 * The assumption is that they'll all be returned in the
   1815 		 * order we got them.
   1816 		 */
   1817 		if (ct->ct_syshandle == 0) {
   1818 			if (ct->ct_flags & CT7_TERMINATE) {
   1819 				isp_prt(isp, ISP_LOGINFO, "termination of 0x%x complete", ct->ct_rxid);
   1820 			} else if ((ct->ct_flags & CT7_SENDSTATUS) == 0) {
   1821 				isp_prt(isp, pl, "intermediate CTIO completed ok");
   1822 			} else {
   1823 				isp_prt(isp, pl, "unmonitored CTIO completed ok");
   1824 			}
   1825 		} else {
   1826 			isp_prt(isp, pl, "NO xs for CTIO (handle 0x%x) status 0x%x", ct->ct_syshandle, ct->ct_nphdl);
   1827 		}
   1828 	} else {
   1829 		if ((ct->ct_flags & CT7_DATAMASK) != CT7_NO_DATA) {
   1830 			ISP_DMAFREE(isp, xs, ct->ct_syshandle);
   1831 		}
   1832 		if (ct->ct_flags & CT7_SENDSTATUS) {
   1833 			/*
   1834 			 * Sent status and command complete.
   1835 			 *
   1836 			 * We're now really done with this command, so we
   1837 			 * punt to the platform dependent layers because
   1838 			 * only there can we do the appropriate command
   1839 			 * complete thread synchronization.
   1840 			 */
   1841 			isp_prt(isp, pl, "status CTIO complete");
   1842 		} else {
   1843 			/*
   1844 			 * Final CTIO completed. Release DMA resources and
   1845 			 * notify platform dependent layers.
   1846 			 */
   1847 			isp_prt(isp, pl, "data CTIO complete");
   1848 		}
   1849 		isp_async(isp, ISPASYNC_TARGET_ACTION, ct);
   1850 		/*
   1851 		 * The platform layer will destroy the handle if appropriate.
   1852 		 */
   1853 	}
   1854 }
   1855 
   1856 static void
   1857 isp_handle_24xx_inotify(ispsoftc_t *isp, in_fcentry_24xx_t *inot_24xx)
   1858 {
   1859 	uint8_t ochan, chan, lochan, hichan;
   1860 
   1861 	/*
   1862 	 * Check to see whether we got a wildcard channel.
   1863 	 * If so, we have to iterate over all channels.
   1864 	 */
   1865 	ochan = chan = ISP_GET_VPIDX(isp, inot_24xx->in_vpidx);
   1866 	if (chan == 0xff) {
   1867 		lochan = 0;
   1868 		hichan = isp->isp_nchan;
   1869 	} else {
   1870 		if (chan >= isp->isp_nchan) {
   1871 			char buf[64];
   1872 			ISP_SNPRINTF(buf, sizeof buf, "%s: bad channel %d for status 0x%x", __func__, chan, inot_24xx->in_status);
   1873 			isp_print_bytes(isp, buf, QENTRY_LEN, inot_24xx);
   1874 			(void) isp_notify_ack(isp, inot_24xx);
   1875 			return;
   1876 		}
   1877 		lochan = chan;
   1878 		hichan = chan + 1;
   1879 	}
   1880 	isp_prt(isp, ISP_LOGTDEBUG1, "%s: Immediate Notify Channels %d..%d status=0x%x seqid=0x%x", __func__, lochan, hichan-1, inot_24xx->in_status, inot_24xx->in_rxid);
   1881 	for (chan = lochan; chan < hichan; chan++) {
   1882 		switch (inot_24xx->in_status) {
   1883 		case IN24XX_LIP_RESET:
   1884 		case IN24XX_LINK_RESET:
   1885 		case IN24XX_PORT_LOGOUT:
   1886 		case IN24XX_PORT_CHANGED:
   1887 		case IN24XX_LINK_FAILED:
   1888 		case IN24XX_SRR_RCVD:
   1889 		case IN24XX_ELS_RCVD:
   1890 			inot_24xx->in_vpidx = chan;
   1891 			isp_async(isp, ISPASYNC_TARGET_ACTION, inot_24xx);
   1892 			break;
   1893 		default:
   1894 			isp_prt(isp, ISP_LOGINFO, "%s: unhandled status (0x%x) for chan %d", __func__, inot_24xx->in_status, chan);
   1895 			(void) isp_notify_ack(isp, inot_24xx);
   1896 			break;
   1897 		}
   1898 	}
   1899 	inot_24xx->in_vpidx = ochan;
   1900 }
   1901 #endif
   1902