xref: /freebsd/sys/dev/isp/isp_target.c (revision d056fa046c6a91b90cd98165face0e42a33a5173)
1 /*-
2  * Machine and OS Independent Target Mode Code for the Qlogic SCSI/FC adapters.
3  *
4  * Copyright (c) 1997-2006 by Matthew Jacob
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice immediately at the beginning of the file, without modification,
12  *    this list of conditions, and the following disclaimer.
13  * 2. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE 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 THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20  * 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 #ifdef	__FreeBSD__
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 #endif
32 
33 /*
34  * Bug fixes gratefully acknowledged from:
35  *	Oded Kedem <oded@kashya.com>
36  */
37 /*
38  * Include header file appropriate for platform we're building on.
39  */
40 
41 #ifdef	__NetBSD__
42 #include <dev/ic/isp_netbsd.h>
43 #endif
44 #ifdef	__FreeBSD__
45 #include <dev/isp/isp_freebsd.h>
46 #endif
47 #ifdef	__OpenBSD__
48 #include <dev/ic/isp_openbsd.h>
49 #endif
50 #ifdef	__linux__
51 #include "isp_linux.h"
52 #endif
53 
54 #ifdef	ISP_TARGET_MODE
55 static const char atiocope[] =
56     "ATIO returned for lun %d because it was in the middle of Bus Device Reset "
57     "on bus %d";
58 static const char atior[] =
59     "ATIO returned on for lun %d on from IID %d because a Bus Reset occurred "
60     "on bus %d";
61 
62 static void isp_got_msg(ispsoftc_t *, in_entry_t *);
63 static void isp_got_msg_fc(ispsoftc_t *, in_fcentry_t *);
64 static void isp_handle_atio(ispsoftc_t *, at_entry_t *);
65 static void isp_handle_atio2(ispsoftc_t *, at2_entry_t *);
66 static void isp_handle_ctio(ispsoftc_t *, ct_entry_t *);
67 static void isp_handle_ctio2(ispsoftc_t *, ct2_entry_t *);
68 
69 /*
70  * The Qlogic driver gets an interrupt to look at response queue entries.
71  * Some of these are status completions for initiatior mode commands, but
72  * if target mode is enabled, we get a whole wad of response queue entries
73  * to be handled here.
74  *
75  * Basically the split into 3 main groups: Lun Enable/Modification responses,
76  * SCSI Command processing, and Immediate Notification events.
77  *
78  * You start by writing a request queue entry to enable target mode (and
79  * establish some resource limitations which you can modify later).
80  * The f/w responds with a LUN ENABLE or LUN MODIFY response with
81  * the status of this action. If the enable was successful, you can expect...
82  *
83  * Response queue entries with SCSI commands encapsulate show up in an ATIO
84  * (Accept Target IO) type- sometimes with enough info to stop the command at
85  * this level. Ultimately the driver has to feed back to the f/w's request
86  * queue a sequence of CTIOs (continue target I/O) that describe data to
87  * be moved and/or status to be sent) and finally finishing with sending
88  * to the f/w's response queue an ATIO which then completes the handshake
89  * with the f/w for that command. There's a lot of variations on this theme,
90  * including flags you can set in the CTIO for the Qlogic 2X00 fibre channel
91  * cards that 'auto-replenish' the f/w's ATIO count, but this is the basic
92  * gist of it.
93  *
94  * The third group that can show up in the response queue are Immediate
95  * Notification events. These include things like notifications of SCSI bus
96  * resets, or Bus Device Reset messages or other messages received. This
97  * a classic oddbins area. It can get  a little weird because you then turn
98  * around and acknowledge the Immediate Notify by writing an entry onto the
99  * request queue and then the f/w turns around and gives you an acknowledgement
100  * to *your* acknowledgement on the response queue (the idea being to let
101  * the f/w tell you when the event is *really* over I guess).
102  *
103  */
104 
105 
106 /*
107  * A new response queue entry has arrived. The interrupt service code
108  * has already swizzled it into the platform dependent from canonical form.
109  *
110  * Because of the way this driver is designed, unfortunately most of the
111  * actual synchronization work has to be done in the platform specific
112  * code- we have no synchroniation primitives in the common code.
113  */
114 
115 int
116 isp_target_notify(ispsoftc_t *isp, void *vptr, uint16_t *optrp)
117 {
118 	uint16_t status, seqid;
119 	union {
120 		at_entry_t	*atiop;
121 		at2_entry_t	*at2iop;
122 		at2e_entry_t	*at2eiop;
123 		ct_entry_t	*ctiop;
124 		ct2_entry_t	*ct2iop;
125 		ct2e_entry_t	*ct2eiop;
126 		lun_entry_t	*lunenp;
127 		in_entry_t	*inotp;
128 		in_fcentry_t	*inot_fcp;
129 		in_fcentry_e_t	*inote_fcp;
130 		na_entry_t	*nackp;
131 		na_fcentry_t	*nack_fcp;
132 		na_fcentry_e_t	*nacke_fcp;
133 		isphdr_t	*hp;
134 		void *		*vp;
135 #define	atiop		unp.atiop
136 #define	at2iop		unp.at2iop
137 #define	at2eiop		unp.at2eiop
138 #define	ctiop		unp.ctiop
139 #define	ct2iop		unp.ct2iop
140 #define	ct2eiop		unp.ct2eiop
141 #define	lunenp		unp.lunenp
142 #define	inotp		unp.inotp
143 #define	inot_fcp	unp.inot_fcp
144 #define	inote_fcp	unp.inote_fcp
145 #define	nackp		unp.nackp
146 #define	nack_fcp	unp.nack_fcp
147 #define	nacke_fcp	unp.nacke_fcp
148 #define	hdrp		unp.hp
149 	} unp;
150 	uint8_t local[QENTRY_LEN];
151 	int bus, type, rval = 1;
152 
153 	type = isp_get_response_type(isp, (isphdr_t *)vptr);
154 	unp.vp = vptr;
155 
156 	ISP_TDQE(isp, "isp_target_notify", (int) *optrp, vptr);
157 
158 	switch(type) {
159 	case RQSTYPE_ATIO:
160 		isp_get_atio(isp, atiop, (at_entry_t *) local);
161 		isp_handle_atio(isp, (at_entry_t *) local);
162 		break;
163 	case RQSTYPE_CTIO:
164 		isp_get_ctio(isp, ctiop, (ct_entry_t *) local);
165 		isp_handle_ctio(isp, (ct_entry_t *) local);
166 		break;
167 	case RQSTYPE_ATIO2:
168 		if (IS_2KLOGIN(isp)) {
169 			isp_get_atio2e(isp, at2eiop, (at2e_entry_t *) local);
170 		} else {
171 			isp_get_atio2(isp, at2iop, (at2_entry_t *) local);
172         }
173 		isp_handle_atio2(isp, (at2_entry_t *) local);
174 		break;
175 	case RQSTYPE_CTIO3:
176 	case RQSTYPE_CTIO2:
177 		if (IS_2KLOGIN(isp)) {
178 			isp_get_ctio2e(isp, ct2eiop, (ct2e_entry_t *) local);
179 		} else {
180 			isp_get_ctio2(isp, ct2iop, (ct2_entry_t *) local);
181         }
182 		isp_handle_ctio2(isp, (ct2_entry_t *) local);
183 		break;
184 	case RQSTYPE_ENABLE_LUN:
185 	case RQSTYPE_MODIFY_LUN:
186 		isp_get_enable_lun(isp, lunenp, (lun_entry_t *) local);
187 		(void) isp_async(isp, ISPASYNC_TARGET_ACTION, local);
188 		break;
189 
190 	case RQSTYPE_NOTIFY:
191 		/*
192 		 * Either the ISP received a SCSI message it can't
193 		 * handle, or it's returning an Immed. Notify entry
194 		 * we sent. We can send Immed. Notify entries to
195 		 * increment the firmware's resource count for them
196 		 * (we set this initially in the Enable Lun entry).
197 		 */
198 		bus = 0;
199 		if (IS_FC(isp)) {
200 			if (IS_2KLOGIN(isp)) {
201 				isp_get_notify_fc_e(isp, inote_fcp, (in_fcentry_e_t *)local);
202             } else {
203                 isp_get_notify_fc(isp, inot_fcp, (in_fcentry_t *)local);
204             }
205 			inot_fcp = (in_fcentry_t *) local;
206 			status = inot_fcp->in_status;
207 			seqid = inot_fcp->in_seqid;
208 		} else {
209 			isp_get_notify(isp, inotp, (in_entry_t *)local);
210 			inotp = (in_entry_t *) local;
211 			status = inotp->in_status & 0xff;
212 			seqid = inotp->in_seqid;
213 			if (IS_DUALBUS(isp)) {
214 				bus = GET_BUS_VAL(inotp->in_iid);
215 				SET_BUS_VAL(inotp->in_iid, 0);
216 			}
217 		}
218 		isp_prt(isp, ISP_LOGTDEBUG0,
219 		    "Immediate Notify On Bus %d, status=0x%x seqid=0x%x",
220 		    bus, status, seqid);
221 
222 		switch (status) {
223 		case IN_MSG_RECEIVED:
224 		case IN_IDE_RECEIVED:
225 			if (IS_FC(isp)) {
226 				isp_got_msg_fc(isp, (in_fcentry_t *)local);
227 			} else {
228 				isp_got_msg(isp, (in_entry_t *)local);
229 			}
230 			break;
231 		case IN_RSRC_UNAVAIL:
232 			isp_prt(isp, ISP_LOGWARN, "Firmware out of ATIOs");
233 			isp_notify_ack(isp, local);
234 			break;
235 		case IN_RESET:
236 			(void) isp_target_async(isp, 0, ASYNC_BUS_RESET);
237 			break;
238 		case IN_PORT_LOGOUT:
239 		case IN_ABORT_TASK:
240 		case IN_PORT_CHANGED:
241 		case IN_GLOBAL_LOGO:
242 			(void) isp_async(isp, ISPASYNC_TARGET_ACTION, &local);
243 			break;
244 		default:
245 			isp_prt(isp, ISP_LOGERR,
246 			    "bad status (0x%x) in isp_target_notify", status);
247 			isp_notify_ack(isp, local);
248 			break;
249 		}
250 		break;
251 
252 	case RQSTYPE_NOTIFY_ACK:
253 		/*
254 		 * The ISP is acknowledging our acknowledgement of an
255 		 * Immediate Notify entry for some asynchronous event.
256 		 */
257 		if (IS_FC(isp)) {
258 			if (IS_2KLOGIN(isp)) {
259 				isp_get_notify_ack_fc_e(isp, nacke_fcp,
260 				    (na_fcentry_e_t *)local);
261 			} else {
262 				isp_get_notify_ack_fc(isp, nack_fcp,
263 				    (na_fcentry_t *)local);
264             }
265 			nack_fcp = (na_fcentry_t *)local;
266 			isp_prt(isp, ISP_LOGTDEBUG1,
267 			    "Notify Ack status=0x%x seqid 0x%x",
268 			    nack_fcp->na_status, nack_fcp->na_seqid);
269 		} else {
270 			isp_get_notify_ack(isp, nackp, (na_entry_t *)local);
271 			nackp = (na_entry_t *)local;
272 			isp_prt(isp, ISP_LOGTDEBUG1,
273 			    "Notify Ack event 0x%x status=0x%x seqid 0x%x",
274 			    nackp->na_event, nackp->na_status, nackp->na_seqid);
275 		}
276 		break;
277 	default:
278 		isp_prt(isp, ISP_LOGERR,
279 		    "Unknown entry type 0x%x in isp_target_notify", type);
280 		rval = 0;
281 		break;
282 	}
283 #undef	atiop
284 #undef	at2iop
285 #undef	at2eiop
286 #undef	ctiop
287 #undef	ct2iop
288 #undef	ct2eiop
289 #undef	lunenp
290 #undef	inotp
291 #undef	inot_fcp
292 #undef	inote_fcp
293 #undef	nackp
294 #undef	nack_fcp
295 #undef	nacke_fcp
296 #undef	hdrp
297 	return (rval);
298 }
299 
300 
301 /*
302  * Toggle (on/off) target mode for bus/target/lun
303  *
304  * The caller has checked for overlap and legality.
305  *
306  * Note that not all of bus, target or lun can be paid attention to.
307  * Note also that this action will not be complete until the f/w writes
308  * response entry. The caller is responsible for synchronizing this.
309  */
310 int
311 isp_lun_cmd(ispsoftc_t *isp, int cmd, int bus, int tgt, int lun,
312     int cmd_cnt, int inot_cnt, uint32_t opaque)
313 {
314 	lun_entry_t el;
315 	uint16_t nxti, optr;
316 	void *outp;
317 
318 
319 	MEMZERO(&el, sizeof (el));
320 	if (IS_DUALBUS(isp)) {
321 		el.le_rsvd = (bus & 0x1) << 7;
322 	}
323 	el.le_cmd_count = cmd_cnt;
324 	el.le_in_count = inot_cnt;
325 	if (cmd == RQSTYPE_ENABLE_LUN) {
326 		if (IS_SCSI(isp)) {
327 			el.le_flags = LUN_TQAE|LUN_DISAD;
328 			el.le_cdb6len = 12;
329 			el.le_cdb7len = 12;
330 		}
331 	} else if (cmd == -RQSTYPE_ENABLE_LUN) {
332 		cmd = RQSTYPE_ENABLE_LUN;
333 		el.le_cmd_count = 0;
334 		el.le_in_count = 0;
335 	} else if (cmd == -RQSTYPE_MODIFY_LUN) {
336 		cmd = RQSTYPE_MODIFY_LUN;
337 		el.le_ops = LUN_CCDECR | LUN_INDECR;
338 	} else {
339 		el.le_ops = LUN_CCINCR | LUN_ININCR;
340 	}
341 	el.le_header.rqs_entry_type = cmd;
342 	el.le_header.rqs_entry_count = 1;
343 	el.le_reserved = opaque;
344 	if (IS_SCSI(isp)) {
345 		el.le_tgt = tgt;
346 		el.le_lun = lun;
347 	} else if ((FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) == 0) {
348 		el.le_lun = lun;
349 	}
350 
351 	if (isp_getrqentry(isp, &nxti, &optr, &outp)) {
352 		isp_prt(isp, ISP_LOGERR,
353 		    "Request Queue Overflow in isp_lun_cmd");
354 		return (-1);
355 	}
356 	ISP_TDQE(isp, "isp_lun_cmd", (int) optr, &el);
357 	isp_put_enable_lun(isp, &el, outp);
358 	ISP_ADD_REQUEST(isp, nxti);
359 	return (0);
360 }
361 
362 
363 int
364 isp_target_put_entry(ispsoftc_t *isp, void *ap)
365 {
366 	void *outp;
367 	uint16_t nxti, optr;
368 	uint8_t etype = ((isphdr_t *) ap)->rqs_entry_type;
369 
370 	if (isp_getrqentry(isp, &nxti, &optr, &outp)) {
371 		isp_prt(isp, ISP_LOGWARN,
372 		    "Request Queue Overflow in isp_target_put_entry");
373 		return (-1);
374 	}
375 	switch (etype) {
376 	case RQSTYPE_ATIO:
377 		isp_put_atio(isp, (at_entry_t *) ap, (at_entry_t *) outp);
378 		break;
379 	case RQSTYPE_ATIO2:
380         if (IS_2KLOGIN(isp)) {
381             isp_put_atio2e(isp, (at2e_entry_t *) ap, (at2e_entry_t *) outp);
382         } else {
383             isp_put_atio2(isp, (at2_entry_t *) ap, (at2_entry_t *) outp);
384         }
385 		break;
386 	case RQSTYPE_CTIO:
387 		isp_put_ctio(isp, (ct_entry_t *) ap, (ct_entry_t *) outp);
388 		break;
389 	case RQSTYPE_CTIO2:
390         if (IS_2KLOGIN(isp)) {
391             isp_put_ctio2e(isp, (ct2e_entry_t *) ap, (ct2e_entry_t *) outp);
392         } else {
393             isp_put_ctio2(isp, (ct2_entry_t *) ap, (ct2_entry_t *) outp);
394         }
395 		break;
396 	default:
397 		isp_prt(isp, ISP_LOGERR,
398 		    "Unknown type 0x%x in isp_put_entry", etype);
399 		return (-1);
400 	}
401 
402 	ISP_TDQE(isp, "isp_target_put_entry", (int) optr, ap);
403 	ISP_ADD_REQUEST(isp, nxti);
404 	return (0);
405 }
406 
407 int
408 isp_target_put_atio(ispsoftc_t *isp, void *arg)
409 {
410 	union {
411 		at_entry_t _atio;
412 		at2_entry_t _atio2;
413 		at2e_entry_t _atio2e;
414 	} atun;
415 
416 	MEMZERO(&atun, sizeof atun);
417 	if (IS_FC(isp)) {
418 		at2_entry_t *aep = arg;
419 		atun._atio2.at_header.rqs_entry_type = RQSTYPE_ATIO2;
420 		atun._atio2.at_header.rqs_entry_count = 1;
421 		if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) {
422 			atun._atio2.at_scclun = (uint16_t) aep->at_scclun;
423 		} else {
424 			atun._atio2.at_lun = (uint8_t) aep->at_lun;
425 		}
426 		if (IS_2KLOGIN(isp)) {
427 			atun._atio2e.at_iid = ((at2e_entry_t *)aep)->at_iid;
428 		} else {
429 			atun._atio2.at_iid = aep->at_iid;
430 		}
431 		atun._atio2.at_rxid = aep->at_rxid;
432 		atun._atio2.at_status = CT_OK;
433 	} else {
434 		at_entry_t *aep = arg;
435 		atun._atio.at_header.rqs_entry_type = RQSTYPE_ATIO;
436 		atun._atio.at_header.rqs_entry_count = 1;
437 		atun._atio.at_handle = aep->at_handle;
438 		atun._atio.at_iid = aep->at_iid;
439 		atun._atio.at_tgt = aep->at_tgt;
440 		atun._atio.at_lun = aep->at_lun;
441 		atun._atio.at_tag_type = aep->at_tag_type;
442 		atun._atio.at_tag_val = aep->at_tag_val;
443 		atun._atio.at_status = (aep->at_flags & AT_TQAE);
444 		atun._atio.at_status |= CT_OK;
445 	}
446 	return (isp_target_put_entry(isp, &atun));
447 }
448 
449 /*
450  * Command completion- both for handling cases of no resources or
451  * no blackhole driver, or other cases where we have to, inline,
452  * finish the command sanely, or for normal command completion.
453  *
454  * The 'completion' code value has the scsi status byte in the low 8 bits.
455  * If status is a CHECK CONDITION and bit 8 is nonzero, then bits 12..15 have
456  * the sense key and  bits 16..23 have the ASCQ and bits 24..31 have the ASC
457  * values.
458  *
459  * NB: the key, asc, ascq, cannot be used for parallel SCSI as it doesn't
460  * NB: inline SCSI sense reporting. As such, we lose this information. XXX.
461  *
462  * For both parallel && fibre channel, we use the feature that does
463  * an automatic resource autoreplenish so we don't have then later do
464  * put of an atio to replenish the f/w's resource count.
465  */
466 
467 int
468 isp_endcmd(ispsoftc_t *isp, void *arg, uint32_t code, uint16_t hdl)
469 {
470 	int sts;
471 	union {
472 		ct_entry_t _ctio;
473 		ct2_entry_t _ctio2;
474 		ct2e_entry_t _ctio2e;
475 	} un;
476 
477 	MEMZERO(&un, sizeof un);
478 	sts = code & 0xff;
479 
480 	if (IS_FC(isp)) {
481 		at2_entry_t *aep = arg;
482 		ct2_entry_t *cto = &un._ctio2;
483 
484 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
485 		cto->ct_header.rqs_entry_count = 1;
486 		if ((FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) == 0) {
487 			cto->ct_lun = aep->at_lun;
488 		}
489 		if (IS_2KLOGIN(isp)) {
490 			un._ctio2e.ct_iid = ((at2e_entry_t *)aep)->at_iid;
491 		} else {
492 			cto->ct_iid = aep->at_iid;
493 		}
494 		cto->ct_rxid = aep->at_rxid;
495 		cto->rsp.m1.ct_scsi_status = sts;
496 		cto->ct_flags = CT2_SENDSTATUS | CT2_NO_DATA | CT2_FLAG_MODE1;
497 		if (hdl == 0) {
498 			cto->ct_flags |= CT2_CCINCR;
499 		}
500 		if (aep->at_datalen) {
501 			cto->ct_resid = aep->at_datalen;
502 			cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER;
503 		}
504 		if (sts == SCSI_CHECK && (code & ECMD_SVALID)) {
505 			cto->rsp.m1.ct_resp[0] = 0xf0;
506 			cto->rsp.m1.ct_resp[2] = (code >> 12) & 0xf;
507 			cto->rsp.m1.ct_resp[7] = 8;
508 			cto->rsp.m1.ct_resp[12] = (code >> 24) & 0xff;
509 			cto->rsp.m1.ct_resp[13] = (code >> 16) & 0xff;
510 			cto->rsp.m1.ct_senselen = 16;
511 			cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
512 		}
513 		cto->ct_syshandle = hdl;
514 	} else {
515 		at_entry_t *aep = arg;
516 		ct_entry_t *cto = &un._ctio;
517 
518 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO;
519 		cto->ct_header.rqs_entry_count = 1;
520 		cto->ct_fwhandle = aep->at_handle;
521 		cto->ct_iid = aep->at_iid;
522 		cto->ct_tgt = aep->at_tgt;
523 		cto->ct_lun = aep->at_lun;
524 		cto->ct_tag_type = aep->at_tag_type;
525 		cto->ct_tag_val = aep->at_tag_val;
526 		if (aep->at_flags & AT_TQAE) {
527 			cto->ct_flags |= CT_TQAE;
528 		}
529 		cto->ct_flags = CT_SENDSTATUS | CT_NO_DATA;
530 		if (hdl == 0) {
531 			cto->ct_flags |= CT_CCINCR;
532 		}
533 		cto->ct_scsi_status = sts;
534 		cto->ct_syshandle = hdl;
535 	}
536 	return (isp_target_put_entry(isp, &un));
537 }
538 
539 /*
540  * These are either broadcast events or specifically CTIO fast completion
541  */
542 int
543 isp_target_async(ispsoftc_t *isp, int bus, int event)
544 {
545 	tmd_notify_t notify;
546 
547 	MEMZERO(&notify, sizeof (tmd_notify_t));
548 	notify.nt_hba = isp;
549 	notify.nt_iid = INI_ANY;
550 	/* nt_tgt set in outer layers */
551 	notify.nt_lun = LUN_ANY;
552 	notify.nt_tagval = TAG_ANY;
553 
554 	if (IS_SCSI(isp)) {
555 		TAG_INSERT_BUS(notify.nt_tagval, bus);
556 	}
557 
558 	switch (event) {
559 	case ASYNC_LOOP_UP:
560 	case ASYNC_PTPMODE:
561 		notify.nt_ncode = NT_LINK_UP;
562 		(void) isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
563 		break;
564 	case ASYNC_LOOP_DOWN:
565 		notify.nt_ncode = NT_LINK_DOWN;
566 		(void) isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
567 		break;
568 	case ASYNC_LIP_F8:
569 	case ASYNC_LIP_OCCURRED:
570 	case ASYNC_LOOP_RESET:
571 		notify.nt_ncode = NT_LIP_RESET;
572 		(void) isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
573 		break;
574 	case ASYNC_BUS_RESET:
575 	case ASYNC_TIMEOUT_RESET:	/* XXX: where does this come from ? */
576 		notify.nt_ncode = NT_BUS_RESET;
577 		(void) isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
578 		break;
579 	case ASYNC_DEVICE_RESET:
580 		notify.nt_ncode = NT_TARGET_RESET;
581 		(void) isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
582 		break;
583 	case ASYNC_CTIO_DONE:
584 	{
585 		uint8_t storage[QENTRY_LEN];
586 		memset(storage, 0, QENTRY_LEN);
587 		if (IS_FC(isp)) {
588             		/* This should also suffice for 2K login code */
589 			ct2_entry_t *ct = (ct2_entry_t *) storage;
590 			ct->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
591 			ct->ct_status = CT_OK;
592 			ct->ct_syshandle = bus;
593 			ct->ct_flags = CT2_SENDSTATUS|CT2_FASTPOST;
594 		} else {
595 			ct_entry_t *ct = (ct_entry_t *) storage;
596 			ct->ct_header.rqs_entry_type = RQSTYPE_CTIO;
597 			ct->ct_status = CT_OK;
598 			ct->ct_fwhandle = bus;
599 			ct->ct_flags = CT_SENDSTATUS;
600 		}
601 		(void) isp_async(isp, ISPASYNC_TARGET_ACTION, storage);
602 		return (0);
603 	}
604 	default:
605 		isp_prt(isp, ISP_LOGERR,
606 		    "isp_target_async: unknown event 0x%x", event);
607 		if (isp->isp_state == ISP_RUNSTATE) {
608 			isp_notify_ack(isp, NULL);
609 		}
610 		break;
611 	}
612 	return (0);
613 }
614 
615 
616 /*
617  * Process a received message.
618  * The ISP firmware can handle most messages, there are only
619  * a few that we need to deal with:
620  * - abort: clean up the current command
621  * - abort tag and clear queue
622  */
623 
624 static void
625 isp_got_msg(ispsoftc_t *isp, in_entry_t *inp)
626 {
627 	tmd_notify_t nt;
628 	uint8_t status = inp->in_status & ~QLTM_SVALID;
629 
630 	MEMZERO(&nt, sizeof (nt));
631 	nt.nt_hba = isp;
632 	nt.nt_iid = GET_IID_VAL(inp->in_iid);
633 	nt.nt_tgt = inp->in_tgt;
634 	nt.nt_lun = inp->in_lun;
635 	IN_MAKE_TAGID(nt.nt_tagval, 0, inp);
636 	nt.nt_lreserved = inp;
637 
638 	if (status == IN_IDE_RECEIVED || status == IN_MSG_RECEIVED) {
639 		switch (inp->in_msg[0]) {
640 		case MSG_ABORT:
641 			nt.nt_ncode = NT_ABORT_TASK_SET;
642 			break;
643 		case MSG_BUS_DEV_RESET:
644 			nt.nt_ncode = NT_TARGET_RESET;
645 			break;
646 		case MSG_ABORT_TAG:
647 			nt.nt_ncode = NT_ABORT_TASK;
648 			break;
649 		case MSG_CLEAR_QUEUE:
650 			nt.nt_ncode = NT_CLEAR_TASK_SET;
651 			break;
652 		case MSG_REL_RECOVERY:
653 			nt.nt_ncode = NT_CLEAR_ACA;
654 			break;
655 		case MSG_TERM_IO_PROC:
656 			nt.nt_ncode = NT_ABORT_TASK;
657 			break;
658 		case MSG_LUN_RESET:
659 			nt.nt_ncode = NT_LUN_RESET;
660 			break;
661 		default:
662 			isp_prt(isp, ISP_LOGERR,
663 			    "unhandled message 0x%x", inp->in_msg[0]);
664 			isp_notify_ack(isp, inp);
665 			return;
666 		}
667 		(void) isp_async(isp, ISPASYNC_TARGET_NOTIFY, &nt);
668 	} else {
669 		isp_prt(isp, ISP_LOGERR,
670 		    "unknown immediate notify status 0x%x", inp->in_status);
671 		isp_notify_ack(isp, inp);
672 	}
673 }
674 
675 /*
676  * Synthesize a message from the task management flags in a FCP_CMND_IU.
677  */
678 static void
679 isp_got_msg_fc(ispsoftc_t *isp, in_fcentry_t *inp)
680 {
681 	tmd_notify_t nt;
682 	static const char f1[] = "%s from iid 0x%08x%08x lun %d seq 0x%x";
683 	static const char f2[] =
684 	    "unknown %s 0x%x lun %d iid 0x%08x%08x task flags 0x%x seq 0x%x\n";
685 	uint16_t seqid;
686 
687 	MEMZERO(&nt, sizeof (tmd_notify_t));
688 	nt.nt_hba = isp;
689 	if (IS_2KLOGIN(isp)) {
690 		nt.nt_iid = ((in_fcentry_e_t *)inp)->in_iid;
691 		seqid = ((in_fcentry_e_t *)inp)->in_seqid;
692 	} else {
693 		nt.nt_iid = inp->in_iid;
694 		seqid = inp->in_seqid;
695 	}
696 	/* nt_tgt set in outer layers */
697 	if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) {
698 		nt.nt_lun = inp->in_scclun;
699 	} else {
700 		nt.nt_lun = inp->in_lun;
701 	}
702 	IN_FC_MAKE_TAGID(nt.nt_tagval, 0, seqid);
703 	nt.nt_lreserved = inp;
704 
705 	if (inp->in_status != IN_MSG_RECEIVED) {
706 		isp_prt(isp, ISP_LOGINFO, f2, "immediate notify status",
707 		    inp->in_status, nt.nt_lun, (uint32_t) (nt.nt_iid >> 32), (uint32_t) nt.nt_iid,
708 		    inp->in_task_flags,  inp->in_seqid);
709 		isp_notify_ack(isp, inp);
710 		return;
711 	}
712 
713 	if (inp->in_task_flags & TASK_FLAGS_ABORT_TASK_SET) {
714 		isp_prt(isp, ISP_LOGINFO, f1, "ABORT TASK SET",
715 		    (uint32_t) (nt.nt_iid >> 32), (uint32_t) nt.nt_iid, nt.nt_lun, inp->in_seqid);
716 		nt.nt_ncode = NT_ABORT_TASK_SET;
717 	} else if (inp->in_task_flags & TASK_FLAGS_CLEAR_TASK_SET) {
718 		isp_prt(isp, ISP_LOGINFO, f1, "CLEAR TASK SET",
719 		    (uint32_t) (nt.nt_iid >> 32), (uint32_t) nt.nt_iid, nt.nt_lun, inp->in_seqid);
720 		nt.nt_ncode = NT_CLEAR_TASK_SET;
721 	} else if (inp->in_task_flags & TASK_FLAGS_LUN_RESET) {
722 		isp_prt(isp, ISP_LOGINFO, f1, "LUN RESET",
723 		    (uint32_t) (nt.nt_iid >> 32), (uint32_t) nt.nt_iid, nt.nt_lun, inp->in_seqid);
724 		nt.nt_ncode = NT_LUN_RESET;
725 	} else if (inp->in_task_flags & TASK_FLAGS_TARGET_RESET) {
726 		isp_prt(isp, ISP_LOGINFO, f1, "TARGET RESET",
727 		    (uint32_t) (nt.nt_iid >> 32), (uint32_t) nt.nt_iid, nt.nt_lun, inp->in_seqid);
728 		nt.nt_ncode = NT_TARGET_RESET;
729 	} else if (inp->in_task_flags & TASK_FLAGS_CLEAR_ACA) {
730 		isp_prt(isp, ISP_LOGINFO, f1, "CLEAR ACA",
731 		    (uint32_t) (nt.nt_iid >> 32), (uint32_t) nt.nt_iid, nt.nt_lun, inp->in_seqid);
732 		nt.nt_ncode = NT_CLEAR_ACA;
733 	} else {
734 		isp_prt(isp, ISP_LOGWARN, f2, "task flag",
735 		    inp->in_status, nt.nt_lun, (uint32_t) (nt.nt_iid >> 32), (uint32_t) nt.nt_iid,
736 		    inp->in_task_flags,  inp->in_seqid);
737 		isp_notify_ack(isp, inp);
738 		return;
739 	}
740 	(void) isp_async(isp, ISPASYNC_TARGET_NOTIFY, &nt);
741 }
742 
743 void
744 isp_notify_ack(ispsoftc_t *isp, void *arg)
745 {
746 	char storage[QENTRY_LEN];
747 	uint16_t nxti, optr;
748 	void *outp;
749 
750 	if (isp_getrqentry(isp, &nxti, &optr, &outp)) {
751 		isp_prt(isp, ISP_LOGWARN,
752 		    "Request Queue Overflow For isp_notify_ack");
753 		return;
754 	}
755 
756 	MEMZERO(storage, QENTRY_LEN);
757 
758 	if (IS_FC(isp)) {
759 		na_fcentry_t *na = (na_fcentry_t *) storage;
760 		if (arg) {
761 			in_fcentry_t *inp = arg;
762 			MEMCPY(storage, arg, sizeof (isphdr_t));
763 			if (IS_2KLOGIN(isp)) {
764 				((na_fcentry_e_t *)na)->na_iid = ((in_fcentry_e_t *)inp)->in_iid;
765 			} else {
766 				na->na_iid = inp->in_iid;
767 			}
768 			if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) {
769 				na->na_lun = inp->in_scclun;
770 			} else {
771 				na->na_lun = inp->in_lun;
772 			}
773 			na->na_task_flags = inp->in_task_flags;
774 			na->na_seqid = inp->in_seqid;
775 			na->na_flags = NAFC_RCOUNT;
776 			na->na_status = inp->in_status;
777 			if (inp->in_status == IN_RESET) {
778 				na->na_flags |= NAFC_RST_CLRD;
779 			}
780 		} else {
781 			na->na_flags = NAFC_RST_CLRD;
782 		}
783 		na->na_header.rqs_entry_type = RQSTYPE_NOTIFY_ACK;
784 		na->na_header.rqs_entry_count = 1;
785 		if (IS_2KLOGIN(isp)) {
786 			isp_put_notify_ack_fc_e(isp, (na_fcentry_e_t *) na, (na_fcentry_e_t *)outp);
787 		} else {
788 			isp_put_notify_ack_fc(isp, na, (na_fcentry_t *)outp);
789 		}
790 	} else {
791 		na_entry_t *na = (na_entry_t *) storage;
792 		if (arg) {
793 			in_entry_t *inp = arg;
794 			MEMCPY(storage, arg, sizeof (isphdr_t));
795 			na->na_iid = inp->in_iid;
796 			na->na_lun = inp->in_lun;
797 			na->na_tgt = inp->in_tgt;
798 			na->na_seqid = inp->in_seqid;
799 			if (inp->in_status == IN_RESET) {
800 				na->na_event = NA_RST_CLRD;
801 			}
802 		} else {
803 			na->na_event = NA_RST_CLRD;
804 		}
805 		na->na_header.rqs_entry_type = RQSTYPE_NOTIFY_ACK;
806 		na->na_header.rqs_entry_count = 1;
807 		isp_put_notify_ack(isp, na, (na_entry_t *)outp);
808 	}
809 	ISP_TDQE(isp, "isp_notify_ack", (int) optr, storage);
810 	ISP_ADD_REQUEST(isp, nxti);
811 }
812 
813 static void
814 isp_handle_atio(ispsoftc_t *isp, at_entry_t *aep)
815 {
816 	int lun;
817 	lun = aep->at_lun;
818 	/*
819 	 * The firmware status (except for the QLTM_SVALID bit) indicates
820 	 * why this ATIO was sent to us.
821 	 *
822 	 * If QLTM_SVALID is set, the firware has recommended Sense Data.
823 	 *
824 	 * If the DISCONNECTS DISABLED bit is set in the flags field,
825 	 * we're still connected on the SCSI bus - i.e. the initiator
826 	 * did not set DiscPriv in the identify message. We don't care
827 	 * about this so it's ignored.
828 	 */
829 
830 	switch(aep->at_status & ~QLTM_SVALID) {
831 	case AT_PATH_INVALID:
832 		/*
833 		 * ATIO rejected by the firmware due to disabled lun.
834 		 */
835 		isp_prt(isp, ISP_LOGERR,
836 		    "rejected ATIO for disabled lun %d", lun);
837 		break;
838 	case AT_NOCAP:
839 		/*
840 		 * Requested Capability not available
841 		 * We sent an ATIO that overflowed the firmware's
842 		 * command resource count.
843 		 */
844 		isp_prt(isp, ISP_LOGERR,
845 		    "rejected ATIO for lun %d because of command count"
846 		    " overflow", lun);
847 		break;
848 
849 	case AT_BDR_MSG:
850 		/*
851 		 * If we send an ATIO to the firmware to increment
852 		 * its command resource count, and the firmware is
853 		 * recovering from a Bus Device Reset, it returns
854 		 * the ATIO with this status. We set the command
855 		 * resource count in the Enable Lun entry and do
856 		 * not increment it. Therefore we should never get
857 		 * this status here.
858 		 */
859 		isp_prt(isp, ISP_LOGERR, atiocope, lun,
860 		    GET_BUS_VAL(aep->at_iid));
861 		break;
862 
863 	case AT_CDB:		/* Got a CDB */
864 	case AT_PHASE_ERROR:	/* Bus Phase Sequence Error */
865 		/*
866 		 * Punt to platform specific layer.
867 		 */
868 		(void) isp_async(isp, ISPASYNC_TARGET_ACTION, aep);
869 		break;
870 
871 	case AT_RESET:
872 		/*
873 		 * A bus reset came along and blew away this command. Why
874 		 * they do this in addition the async event code stuff,
875 		 * I dunno.
876 		 *
877 		 * Ignore it because the async event will clear things
878 		 * up for us.
879 		 */
880 		isp_prt(isp, ISP_LOGWARN, atior, lun,
881 		    GET_IID_VAL(aep->at_iid), GET_BUS_VAL(aep->at_iid));
882 		break;
883 
884 
885 	default:
886 		isp_prt(isp, ISP_LOGERR,
887 		    "Unknown ATIO status 0x%x from initiator %d for lun %d",
888 		    aep->at_status, aep->at_iid, lun);
889 		(void) isp_target_put_atio(isp, aep);
890 		break;
891 	}
892 }
893 
894 static void
895 isp_handle_atio2(ispsoftc_t *isp, at2_entry_t *aep)
896 {
897 	int lun, iid;
898 
899 	if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) {
900 		lun = aep->at_scclun;
901 	} else {
902 		lun = aep->at_lun;
903 	}
904 
905 	if (IS_2KLOGIN(isp)) {
906 		iid = ((at2e_entry_t *)aep)->at_iid;
907 	} else {
908 		iid = aep->at_iid;
909 	}
910 
911 	/*
912 	 * The firmware status (except for the QLTM_SVALID bit) indicates
913 	 * why this ATIO was sent to us.
914 	 *
915 	 * If QLTM_SVALID is set, the firware has recommended Sense Data.
916 	 *
917 	 * If the DISCONNECTS DISABLED bit is set in the flags field,
918 	 * we're still connected on the SCSI bus - i.e. the initiator
919 	 * did not set DiscPriv in the identify message. We don't care
920 	 * about this so it's ignored.
921 	 */
922 
923 	switch(aep->at_status & ~QLTM_SVALID) {
924 	case AT_PATH_INVALID:
925 		/*
926 		 * ATIO rejected by the firmware due to disabled lun.
927 		 */
928 		isp_prt(isp, ISP_LOGERR,
929 		    "rejected ATIO2 for disabled lun %d", lun);
930 		break;
931 	case AT_NOCAP:
932 		/*
933 		 * Requested Capability not available
934 		 * We sent an ATIO that overflowed the firmware's
935 		 * command resource count.
936 		 */
937 		isp_prt(isp, ISP_LOGERR,
938 		    "rejected ATIO2 for lun %d- command count overflow", lun);
939 		break;
940 
941 	case AT_BDR_MSG:
942 		/*
943 		 * If we send an ATIO to the firmware to increment
944 		 * its command resource count, and the firmware is
945 		 * recovering from a Bus Device Reset, it returns
946 		 * the ATIO with this status. We set the command
947 		 * resource count in the Enable Lun entry and no
948 		 * not increment it. Therefore we should never get
949 		 * this status here.
950 		 */
951 		isp_prt(isp, ISP_LOGERR, atiocope, lun, 0);
952 		break;
953 
954 	case AT_CDB:		/* Got a CDB */
955 		/*
956 		 * Punt to platform specific layer.
957 		 */
958 		(void) isp_async(isp, ISPASYNC_TARGET_ACTION, aep);
959 		break;
960 
961 	case AT_RESET:
962 		/*
963 		 * A bus reset came along an blew away this command. Why
964 		 * they do this in addition the async event code stuff,
965 		 * I dunno.
966 		 *
967 		 * Ignore it because the async event will clear things
968 		 * up for us.
969 		 */
970 		isp_prt(isp, ISP_LOGERR, atior, lun, iid, 0);
971 		break;
972 
973 
974 	default:
975 		isp_prt(isp, ISP_LOGERR,
976 		    "Unknown ATIO2 status 0x%x from initiator %d for lun %d",
977 		    aep->at_status, iid, lun);
978 		(void) isp_target_put_atio(isp, aep);
979 		break;
980 	}
981 }
982 
983 static void
984 isp_handle_ctio(ispsoftc_t *isp, ct_entry_t *ct)
985 {
986 	void *xs;
987 	int pl = ISP_LOGTDEBUG2;
988 	char *fmsg = NULL;
989 
990 	if (ct->ct_syshandle) {
991 		xs = isp_find_xs_tgt(isp, ct->ct_syshandle);
992 		if (xs == NULL)
993 			pl = ISP_LOGALL;
994 	} else {
995 		xs = NULL;
996 	}
997 
998 	switch(ct->ct_status & ~QLTM_SVALID) {
999 	case CT_OK:
1000 		/*
1001 		 * There are generally 3 possibilities as to why we'd get
1002 		 * this condition:
1003 		 * 	We disconnected after receiving a CDB.
1004 		 * 	We sent or received data.
1005 		 * 	We sent status & command complete.
1006 		 */
1007 
1008 		if (ct->ct_flags & CT_SENDSTATUS) {
1009 			break;
1010 		} else if ((ct->ct_flags & CT_DATAMASK) == CT_NO_DATA) {
1011 			/*
1012 			 * Nothing to do in this case.
1013 			 */
1014 			isp_prt(isp, pl, "CTIO- iid %d disconnected OK",
1015 			    ct->ct_iid);
1016 			return;
1017 		}
1018 		break;
1019 
1020 	case CT_BDR_MSG:
1021 		/*
1022 		 * Bus Device Reset message received or the SCSI Bus has
1023 		 * been Reset; the firmware has gone to Bus Free.
1024 		 *
1025 		 * The firmware generates an async mailbox interupt to
1026 		 * notify us of this and returns outstanding CTIOs with this
1027 		 * status. These CTIOs are handled in that same way as
1028 		 * CT_ABORTED ones, so just fall through here.
1029 		 */
1030 		fmsg = "Bus Device Reset";
1031 		/*FALLTHROUGH*/
1032 	case CT_RESET:
1033 		if (fmsg == NULL)
1034 			fmsg = "Bus Reset";
1035 		/*FALLTHROUGH*/
1036 	case CT_ABORTED:
1037 		/*
1038 		 * When an Abort message is received the firmware goes to
1039 		 * Bus Free and returns all outstanding CTIOs with the status
1040 		 * set, then sends us an Immediate Notify entry.
1041 		 */
1042 		if (fmsg == NULL)
1043 			fmsg = "ABORT TAG message sent by Initiator";
1044 
1045 		isp_prt(isp, ISP_LOGWARN, "CTIO destroyed by %s", fmsg);
1046 		break;
1047 
1048 	case CT_INVAL:
1049 		/*
1050 		 * CTIO rejected by the firmware due to disabled lun.
1051 		 * "Cannot Happen".
1052 		 */
1053 		isp_prt(isp, ISP_LOGERR,
1054 		    "Firmware rejected CTIO for disabled lun %d",
1055 		    ct->ct_lun);
1056 		break;
1057 
1058 	case CT_NOPATH:
1059 		/*
1060 		 * CTIO rejected by the firmware due "no path for the
1061 		 * nondisconnecting nexus specified". This means that
1062 		 * we tried to access the bus while a non-disconnecting
1063 		 * command is in process.
1064 		 */
1065 		isp_prt(isp, ISP_LOGERR,
1066 		    "Firmware rejected CTIO for bad nexus %d/%d/%d",
1067 		    ct->ct_iid, ct->ct_tgt, ct->ct_lun);
1068 		break;
1069 
1070 	case CT_RSELTMO:
1071 		fmsg = "Reselection";
1072 		/*FALLTHROUGH*/
1073 	case CT_TIMEOUT:
1074 		if (fmsg == NULL)
1075 			fmsg = "Command";
1076 		isp_prt(isp, ISP_LOGERR, "Firmware timed out on %s", fmsg);
1077 		break;
1078 
1079 	case	CT_PANIC:
1080 		if (fmsg == NULL)
1081 			fmsg = "Unrecoverable Error";
1082 		/*FALLTHROUGH*/
1083 	case CT_ERR:
1084 		if (fmsg == NULL)
1085 			fmsg = "Completed with Error";
1086 		/*FALLTHROUGH*/
1087 	case CT_PHASE_ERROR:
1088 		if (fmsg == NULL)
1089 			fmsg = "Phase Sequence Error";
1090 		/*FALLTHROUGH*/
1091 	case CT_TERMINATED:
1092 		if (fmsg == NULL)
1093 			fmsg = "terminated by TERMINATE TRANSFER";
1094 		/*FALLTHROUGH*/
1095 	case CT_NOACK:
1096 		if (fmsg == NULL)
1097 			fmsg = "unacknowledged Immediate Notify pending";
1098 		isp_prt(isp, ISP_LOGERR, "CTIO returned by f/w- %s", fmsg);
1099 		break;
1100 	default:
1101 		isp_prt(isp, ISP_LOGERR, "Unknown CTIO status 0x%x",
1102 		    ct->ct_status & ~QLTM_SVALID);
1103 		break;
1104 	}
1105 
1106 	if (xs == NULL) {
1107 		/*
1108 		 * There may be more than one CTIO for a data transfer,
1109 		 * or this may be a status CTIO we're not monitoring.
1110 		 *
1111 		 * The assumption is that they'll all be returned in the
1112 		 * order we got them.
1113 		 */
1114 		if (ct->ct_syshandle == 0) {
1115 			if ((ct->ct_flags & CT_SENDSTATUS) == 0) {
1116 				isp_prt(isp, pl,
1117 				    "intermediate CTIO completed ok");
1118 			} else {
1119 				isp_prt(isp, pl,
1120 				    "unmonitored CTIO completed ok");
1121 			}
1122 		} else {
1123 			isp_prt(isp, pl,
1124 			    "NO xs for CTIO (handle 0x%x) status 0x%x",
1125 			    ct->ct_syshandle, ct->ct_status & ~QLTM_SVALID);
1126 		}
1127 	} else {
1128 		/*
1129 		 * Final CTIO completed. Release DMA resources and
1130 		 * notify platform dependent layers.
1131 		 */
1132 		if ((ct->ct_flags & CT_DATAMASK) != CT_NO_DATA) {
1133 			ISP_DMAFREE(isp, xs, ct->ct_syshandle);
1134 		}
1135 		isp_prt(isp, pl, "final CTIO complete");
1136 		/*
1137 		 * The platform layer will destroy the handle if appropriate.
1138 		 */
1139 		(void) isp_async(isp, ISPASYNC_TARGET_ACTION, ct);
1140 	}
1141 }
1142 
1143 static void
1144 isp_handle_ctio2(ispsoftc_t *isp, ct2_entry_t *ct)
1145 {
1146 	XS_T *xs;
1147 	int pl = ISP_LOGTDEBUG2;
1148 	char *fmsg = NULL;
1149 
1150 	if (ct->ct_syshandle) {
1151 		xs = isp_find_xs_tgt(isp, ct->ct_syshandle);
1152 		if (xs == NULL)
1153 			pl = ISP_LOGALL;
1154 	} else {
1155 		xs = NULL;
1156 	}
1157 
1158 	switch(ct->ct_status & ~QLTM_SVALID) {
1159 	case CT_BUS_ERROR:
1160 		isp_prt(isp, ISP_LOGERR, "PCI DMA Bus Error");
1161 		/* FALL Through */
1162 	case CT_DATA_OVER:
1163 	case CT_DATA_UNDER:
1164 	case CT_OK:
1165 		/*
1166 		 * There are generally 2 possibilities as to why we'd get
1167 		 * this condition:
1168 		 * 	We sent or received data.
1169 		 * 	We sent status & command complete.
1170 		 */
1171 
1172 		break;
1173 
1174 	case CT_BDR_MSG:
1175 		/*
1176 		 * Target Reset function received.
1177 		 *
1178 		 * The firmware generates an async mailbox interupt to
1179 		 * notify us of this and returns outstanding CTIOs with this
1180 		 * status. These CTIOs are handled in that same way as
1181 		 * CT_ABORTED ones, so just fall through here.
1182 		 */
1183 		fmsg = "TARGET RESET Task Management Function Received";
1184 		/*FALLTHROUGH*/
1185 	case CT_RESET:
1186 		if (fmsg == NULL)
1187 			fmsg = "LIP Reset";
1188 		/*FALLTHROUGH*/
1189 	case CT_ABORTED:
1190 		/*
1191 		 * When an Abort message is received the firmware goes to
1192 		 * Bus Free and returns all outstanding CTIOs with the status
1193 		 * set, then sends us an Immediate Notify entry.
1194 		 */
1195 		if (fmsg == NULL)
1196 			fmsg = "ABORT Task Management Function Received";
1197 
1198 		isp_prt(isp, ISP_LOGERR, "CTIO2 destroyed by %s: RX_ID=0x%x", fmsg, ct->ct_rxid);
1199 		break;
1200 
1201 	case CT_INVAL:
1202 		/*
1203 		 * CTIO rejected by the firmware - invalid data direction.
1204 		 */
1205 		isp_prt(isp, ISP_LOGERR, "CTIO2 had wrong data direction");
1206 		break;
1207 
1208 	case CT_RSELTMO:
1209 		fmsg = "failure to reconnect to initiator";
1210 		/*FALLTHROUGH*/
1211 	case CT_TIMEOUT:
1212 		if (fmsg == NULL)
1213 			fmsg = "command";
1214 		isp_prt(isp, ISP_LOGERR, "Firmware timed out on %s", fmsg);
1215 		break;
1216 
1217 	case CT_ERR:
1218 		fmsg = "Completed with Error";
1219 		/*FALLTHROUGH*/
1220 	case CT_LOGOUT:
1221 		if (fmsg == NULL)
1222 			fmsg = "Port Logout";
1223 		/*FALLTHROUGH*/
1224 	case CT_PORTNOTAVAIL:
1225 		if (fmsg == NULL)
1226 			fmsg = "Port not available";
1227 		/*FALLTHROUGH*/
1228 	case CT_PORTCHANGED:
1229 		if (fmsg == NULL)
1230 			fmsg = "Port Changed";
1231 		/*FALLTHROUGH*/
1232 	case CT_NOACK:
1233 		if (fmsg == NULL)
1234 			fmsg = "unacknowledged Immediate Notify pending";
1235 		isp_prt(isp, ISP_LOGERR, "CTIO returned by f/w- %s", fmsg);
1236 		break;
1237 
1238 	case CT_INVRXID:
1239 		/*
1240 		 * CTIO rejected by the firmware because an invalid RX_ID.
1241 		 * Just print a message.
1242 		 */
1243 		isp_prt(isp, ISP_LOGERR,
1244 		    "CTIO2 completed with Invalid RX_ID 0x%x", ct->ct_rxid);
1245 		break;
1246 
1247 	default:
1248 		isp_prt(isp, ISP_LOGERR, "Unknown CTIO2 status 0x%x",
1249 		    ct->ct_status & ~QLTM_SVALID);
1250 		break;
1251 	}
1252 
1253 	if (xs == NULL) {
1254 		/*
1255 		 * There may be more than one CTIO for a data transfer,
1256 		 * or this may be a status CTIO we're not monitoring.
1257 		 *
1258 		 * The assumption is that they'll all be returned in the
1259 		 * order we got them.
1260 		 */
1261 		if (ct->ct_syshandle == 0) {
1262 			if ((ct->ct_flags & CT2_SENDSTATUS) == 0) {
1263 				isp_prt(isp, pl,
1264 				    "intermediate CTIO completed ok");
1265 			} else {
1266 				isp_prt(isp, pl,
1267 				    "unmonitored CTIO completed ok");
1268 			}
1269 		} else {
1270 			isp_prt(isp, pl,
1271 			    "NO xs for CTIO (handle 0x%x) status 0x%x",
1272 			    ct->ct_syshandle, ct->ct_status & ~QLTM_SVALID);
1273 		}
1274 	} else {
1275 		if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA) {
1276 			ISP_DMAFREE(isp, xs, ct->ct_syshandle);
1277 		}
1278 		if (ct->ct_flags & CT2_SENDSTATUS) {
1279 			/*
1280 			 * Sent status and command complete.
1281 			 *
1282 			 * We're now really done with this command, so we
1283 			 * punt to the platform dependent layers because
1284 			 * only there can we do the appropriate command
1285 			 * complete thread synchronization.
1286 			 */
1287 			isp_prt(isp, pl, "status CTIO complete");
1288 		} else {
1289 			/*
1290 			 * Final CTIO completed. Release DMA resources and
1291 			 * notify platform dependent layers.
1292 			 */
1293 			isp_prt(isp, pl, "data CTIO complete");
1294 		}
1295 		(void) isp_async(isp, ISPASYNC_TARGET_ACTION, ct);
1296 		/*
1297 		 * The platform layer will destroy the handle if appropriate.
1298 		 */
1299 	}
1300 }
1301 #endif
1302