xref: /freebsd/sys/dev/isp/isp_target.c (revision eb9da1ada8b6b2c74378a5c17029ec5a7fb199e6)
1 /*-
2  *  Copyright (c) 1997-2009 by Matthew Jacob
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions
7  *  are met:
8  *
9  *  1. Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  *  2. Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  *
15  *  THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  *  ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  *  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  *  SUCH DAMAGE.
26  *
27  */
28 /*
29  * Machine and OS Independent Target Mode Code for the Qlogic SCSI/FC adapters.
30  */
31 /*
32  * Bug fixes gratefully acknowledged from:
33  *	Oded Kedem <oded@kashya.com>
34  */
35 /*
36  * Include header file appropriate for platform we're building on.
37  */
38 
39 #ifdef	__NetBSD__
40 #include <dev/ic/isp_netbsd.h>
41 #endif
42 #ifdef	__FreeBSD__
43 #include <sys/cdefs.h>
44 __FBSDID("$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[] = "ATIO returned for LUN %x because it was in the middle of Bus Device Reset on bus %d";
56 static const char atior[] = "ATIO returned for LUN %x from handle 0x%x because a Bus Reset occurred on bus %d";
57 static const char rqo[] = "%s: Request Queue Overflow";
58 
59 static void isp_got_msg_fc(ispsoftc_t *, in_fcentry_t *);
60 static void isp_got_tmf_24xx(ispsoftc_t *, at7_entry_t *);
61 static void isp_handle_atio2(ispsoftc_t *, at2_entry_t *);
62 static void isp_handle_ctio2(ispsoftc_t *, ct2_entry_t *);
63 static void isp_handle_ctio7(ispsoftc_t *, ct7_entry_t *);
64 static void isp_handle_24xx_inotify(ispsoftc_t *, in_fcentry_24xx_t *);
65 
66 /*
67  * The Qlogic driver gets an interrupt to look at response queue entries.
68  * Some of these are status completions for initiatior mode commands, but
69  * if target mode is enabled, we get a whole wad of response queue entries
70  * to be handled here.
71  *
72  * Basically the split into 3 main groups: Lun Enable/Modification responses,
73  * SCSI Command processing, and Immediate Notification events.
74  *
75  * You start by writing a request queue entry to enable target mode (and
76  * establish some resource limitations which you can modify later).
77  * The f/w responds with a LUN ENABLE or LUN MODIFY response with
78  * the status of this action. If the enable was successful, you can expect...
79  *
80  * Response queue entries with SCSI commands encapsulate show up in an ATIO
81  * (Accept Target IO) type- sometimes with enough info to stop the command at
82  * this level. Ultimately the driver has to feed back to the f/w's request
83  * queue a sequence of CTIOs (continue target I/O) that describe data to
84  * be moved and/or status to be sent) and finally finishing with sending
85  * to the f/w's response queue an ATIO which then completes the handshake
86  * with the f/w for that command. There's a lot of variations on this theme,
87  * including flags you can set in the CTIO for the Qlogic 2X00 fibre channel
88  * cards that 'auto-replenish' the f/w's ATIO count, but this is the basic
89  * gist of it.
90  *
91  * The third group that can show up in the response queue are Immediate
92  * Notification events. These include things like notifications of SCSI bus
93  * resets, or Bus Device Reset messages or other messages received. This
94  * a classic oddbins area. It can get  a little weird because you then turn
95  * around and acknowledge the Immediate Notify by writing an entry onto the
96  * request queue and then the f/w turns around and gives you an acknowledgement
97  * to *your* acknowledgement on the response queue (the idea being to let
98  * the f/w tell you when the event is *really* over I guess).
99  *
100  */
101 
102 
103 /*
104  * A new response queue entry has arrived. The interrupt service code
105  * has already swizzled it into the platform dependent from canonical form.
106  *
107  * Because of the way this driver is designed, unfortunately most of the
108  * actual synchronization work has to be done in the platform specific
109  * code- we have no synchroniation primitives in the common code.
110  */
111 
112 int
113 isp_target_notify(ispsoftc_t *isp, void *vptr, uint32_t *optrp)
114 {
115 	uint16_t status;
116 	uint32_t seqid;
117 	union {
118 		at2_entry_t	*at2iop;
119 		at2e_entry_t	*at2eiop;
120 		at7_entry_t	*at7iop;
121 		ct2_entry_t	*ct2iop;
122 		ct2e_entry_t	*ct2eiop;
123 		ct7_entry_t	*ct7iop;
124 		lun_entry_t	*lunenp;
125 		in_fcentry_t	*inot_fcp;
126 		in_fcentry_e_t	*inote_fcp;
127 		in_fcentry_24xx_t *inot_24xx;
128 		na_fcentry_t	*nack_fcp;
129 		na_fcentry_e_t	*nacke_fcp;
130 		na_fcentry_24xx_t *nack_24xx;
131 		isphdr_t	*hp;
132 		abts_t		*abts;
133 		abts_rsp_t	*abts_rsp;
134 		els_t		*els;
135 		void *		*vp;
136 #define	at2iop		unp.at2iop
137 #define	at2eiop		unp.at2eiop
138 #define	at7iop		unp.at7iop
139 #define	ct2iop		unp.ct2iop
140 #define	ct2eiop		unp.ct2eiop
141 #define	ct7iop		unp.ct7iop
142 #define	lunenp		unp.lunenp
143 #define	inot_fcp	unp.inot_fcp
144 #define	inote_fcp	unp.inote_fcp
145 #define	inot_24xx	unp.inot_24xx
146 #define	nack_fcp	unp.nack_fcp
147 #define	nacke_fcp	unp.nacke_fcp
148 #define	nack_24xx	unp.nack_24xx
149 #define	abts		unp.abts
150 #define	abts_rsp	unp.abts_rsp
151 #define els		unp.els
152 #define	hdrp		unp.hp
153 	} unp;
154 	uint8_t local[QENTRY_LEN];
155 	uint16_t iid;
156 	int bus, type, len, level, rval = 1;
157 	isp_notify_t notify;
158 
159 	type = isp_get_response_type(isp, (isphdr_t *)vptr);
160 	unp.vp = vptr;
161 
162 	ISP_TDQE(isp, "isp_target_notify", (int) *optrp, vptr);
163 
164 	switch (type) {
165 	case RQSTYPE_ATIO:
166 		isp_get_atio7(isp, at7iop, (at7_entry_t *) local);
167 		at7iop = (at7_entry_t *) local;
168 		/*
169 		 * Check for and do something with commands whose
170 		 * IULEN extends past a single queue entry.
171 		 */
172 		len = at7iop->at_ta_len & 0x0fff;
173 		if (len > (QENTRY_LEN - 8)) {
174 			len -= (QENTRY_LEN - 8);
175 			isp_prt(isp, ISP_LOGINFO, "long IU length (%d) ignored", len);
176 			while (len > 0) {
177 				*optrp =  ISP_NXT_QENTRY(*optrp, RESULT_QUEUE_LEN(isp));
178 				len -= QENTRY_LEN;
179 			}
180 		}
181 		/*
182 		 * Check for a task management function
183 		 */
184 		if (at7iop->at_cmnd.fcp_cmnd_task_management) {
185 			isp_got_tmf_24xx(isp, at7iop);
186 			break;
187 		}
188 		/*
189 		 * Just go straight to outer layer for this one.
190 		 */
191 		isp_async(isp, ISPASYNC_TARGET_ACTION, local);
192 		break;
193 
194 	case RQSTYPE_ATIO2:
195 		if (ISP_CAP_2KLOGIN(isp)) {
196 			isp_get_atio2e(isp, at2eiop, (at2e_entry_t *) local);
197 		} else {
198 			isp_get_atio2(isp, at2iop, (at2_entry_t *) local);
199 		}
200 		isp_handle_atio2(isp, (at2_entry_t *) local);
201 		break;
202 
203 	case RQSTYPE_CTIO3:
204 	case RQSTYPE_CTIO2:
205 		if (ISP_CAP_2KLOGIN(isp)) {
206 			isp_get_ctio2e(isp, ct2eiop, (ct2e_entry_t *) local);
207 		} else {
208 			isp_get_ctio2(isp, ct2iop, (ct2_entry_t *) local);
209 		}
210 		isp_handle_ctio2(isp, (ct2_entry_t *) local);
211 		break;
212 
213 	case RQSTYPE_CTIO7:
214 		isp_get_ctio7(isp, ct7iop, (ct7_entry_t *) local);
215 		isp_handle_ctio7(isp, (ct7_entry_t *) local);
216 		break;
217 
218 	case RQSTYPE_ENABLE_LUN:
219 	case RQSTYPE_MODIFY_LUN:
220 		isp_get_enable_lun(isp, lunenp, (lun_entry_t *) local);
221 		isp_async(isp, ISPASYNC_TARGET_ACTION, local);
222 		break;
223 
224 	case RQSTYPE_NOTIFY:
225 		bus = 0;
226 		if (IS_24XX(isp)) {
227 			isp_get_notify_24xx(isp, inot_24xx, (in_fcentry_24xx_t *)local);
228 			inot_24xx = (in_fcentry_24xx_t *) local;
229 			isp_handle_24xx_inotify(isp, inot_24xx);
230 			break;
231 		} else {
232 			if (ISP_CAP_2KLOGIN(isp)) {
233 				in_fcentry_e_t *ecp = (in_fcentry_e_t *)local;
234 				isp_get_notify_fc_e(isp, inote_fcp, ecp);
235 				iid = ecp->in_iid;
236 				status = ecp->in_status;
237 				seqid = ecp->in_seqid;
238 			} else {
239 				in_fcentry_t *fcp = (in_fcentry_t *)local;
240 				isp_get_notify_fc(isp, inot_fcp, fcp);
241 				iid = fcp->in_iid;
242 				status = fcp->in_status;
243 				seqid = fcp->in_seqid;
244 			}
245 		}
246 
247 		isp_prt(isp, ISP_LOGTDEBUG0, "Immediate Notify On Bus %d, status=0x%x seqid=0x%x", bus, status, seqid);
248 
249 		switch (status) {
250 		case IN_MSG_RECEIVED:
251 		case IN_IDE_RECEIVED:
252 			isp_got_msg_fc(isp, (in_fcentry_t *)local);
253 			break;
254 		case IN_RSRC_UNAVAIL:
255 			isp_prt(isp, ISP_LOGINFO, "Firmware out of ATIOs");
256 			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, local);
257 			break;
258 
259 		case IN_RESET:
260 			ISP_MEMZERO(&notify, sizeof (isp_notify_t));
261 			notify.nt_hba = isp;
262 			notify.nt_wwn = INI_ANY;
263 			notify.nt_tgt = TGT_ANY;
264 			notify.nt_nphdl = iid;
265 			notify.nt_sid = PORT_ANY;
266 			notify.nt_did = PORT_ANY;
267 			notify.nt_lun = LUN_ANY;
268 			notify.nt_tagval = TAG_ANY;
269 			notify.nt_tagval |= (((uint64_t)(isp->isp_serno++)) << 32);
270 			notify.nt_ncode = NT_BUS_RESET;
271 			notify.nt_need_ack = 1;
272 			notify.nt_lreserved = local;
273 			isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
274 			break;
275 
276 		case IN_PORT_LOGOUT:
277 			ISP_MEMZERO(&notify, sizeof (isp_notify_t));
278 			notify.nt_hba = isp;
279 			notify.nt_wwn = INI_ANY;
280 			notify.nt_nphdl = iid;
281 			notify.nt_sid = PORT_ANY;
282 			notify.nt_did = PORT_ANY;
283 			notify.nt_ncode = NT_LOGOUT;
284 			notify.nt_need_ack = 1;
285 			notify.nt_lreserved = local;
286 			isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
287 			break;
288 
289 		case IN_ABORT_TASK:
290 			ISP_MEMZERO(&notify, sizeof (isp_notify_t));
291 			notify.nt_hba = isp;
292 			notify.nt_wwn = INI_ANY;
293 			notify.nt_nphdl = iid;
294 			notify.nt_sid = PORT_ANY;
295 			notify.nt_did = PORT_ANY;
296 			notify.nt_ncode = NT_ABORT_TASK;
297 			notify.nt_need_ack = 1;
298 			notify.nt_lreserved = local;
299 			isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
300 			break;
301 
302 		case IN_GLOBAL_LOGO:
303 			isp_prt(isp, ISP_LOGTINFO, "%s: all ports logged out", __func__);
304 			ISP_MEMZERO(&notify, sizeof (isp_notify_t));
305 			notify.nt_hba = isp;
306 			notify.nt_wwn = INI_ANY;
307 			notify.nt_nphdl = NIL_HANDLE;
308 			notify.nt_sid = PORT_ANY;
309 			notify.nt_did = PORT_ANY;
310 			notify.nt_ncode = NT_GLOBAL_LOGOUT;
311 			notify.nt_need_ack = 1;
312 			notify.nt_lreserved = local;
313 			isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
314 			break;
315 
316 		case IN_PORT_CHANGED:
317 			isp_prt(isp, ISP_LOGTINFO, "%s: port changed", __func__);
318 			ISP_MEMZERO(&notify, sizeof (isp_notify_t));
319 			notify.nt_hba = isp;
320 			notify.nt_wwn = INI_ANY;
321 			notify.nt_nphdl = NIL_HANDLE;
322 			notify.nt_sid = PORT_ANY;
323 			notify.nt_did = PORT_ANY;
324 			notify.nt_ncode = NT_CHANGED;
325 			notify.nt_need_ack = 1;
326 			notify.nt_lreserved = local;
327 			isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
328 			break;
329 
330 		default:
331 			ISP_SNPRINTF(local, sizeof local, "%s: unknown status to RQSTYPE_NOTIFY (0x%x)", __func__, status);
332 			isp_print_bytes(isp, local, QENTRY_LEN, vptr);
333 			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, local);
334 			break;
335 		}
336 		break;
337 
338 	case RQSTYPE_NOTIFY_ACK:
339 		/*
340 		 * The ISP is acknowledging our acknowledgement of an
341 		 * Immediate Notify entry for some asynchronous event.
342 		 */
343 		if (IS_24XX(isp)) {
344 			isp_get_notify_ack_24xx(isp, nack_24xx, (na_fcentry_24xx_t *) local);
345 			nack_24xx = (na_fcentry_24xx_t *) local;
346 			if (nack_24xx->na_status != NA_OK) {
347 				level = ISP_LOGINFO;
348 			} else {
349 				level = ISP_LOGTDEBUG1;
350 			}
351 			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);
352 		} else {
353 			if (ISP_CAP_2KLOGIN(isp)) {
354 				isp_get_notify_ack_fc_e(isp, nacke_fcp, (na_fcentry_e_t *)local);
355 			} else {
356 				isp_get_notify_ack_fc(isp, nack_fcp, (na_fcentry_t *)local);
357 			}
358 			nack_fcp = (na_fcentry_t *)local;
359 			if (nack_fcp->na_status != NA_OK) {
360 				level = ISP_LOGINFO;
361 			} else {
362 				level = ISP_LOGTDEBUG1;
363 			}
364 			isp_prt(isp, level, "Notify Ack Status=0x%x seqid 0x%x", nack_fcp->na_status, nack_fcp->na_seqid);
365 		}
366 		break;
367 
368 	case RQSTYPE_ABTS_RCVD:
369 		isp_get_abts(isp, abts, (abts_t *)local);
370 		isp_async(isp, ISPASYNC_TARGET_ACTION, &local);
371 		break;
372 	case RQSTYPE_ABTS_RSP:
373 		isp_get_abts_rsp(isp, abts_rsp, (abts_rsp_t *)local);
374 		abts_rsp = (abts_rsp_t *) local;
375 		if (abts_rsp->abts_rsp_status) {
376 			level = ISP_LOGINFO;
377 		} else {
378 			level = ISP_LOGTDEBUG0;
379 		}
380 		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,
381 		    abts_rsp->abts_rsp_payload.rsp.subcode1, abts_rsp->abts_rsp_payload.rsp.subcode2);
382 		break;
383 	default:
384 		isp_prt(isp, ISP_LOGERR, "%s: unknown entry type 0x%x", __func__, type);
385 		rval = 0;
386 		break;
387 	}
388 #undef	atiop
389 #undef	at2iop
390 #undef	at2eiop
391 #undef	at7iop
392 #undef	ctiop
393 #undef	ct2iop
394 #undef	ct2eiop
395 #undef	ct7iop
396 #undef	lunenp
397 #undef	inotp
398 #undef	inot_fcp
399 #undef	inote_fcp
400 #undef	inot_24xx
401 #undef	nackp
402 #undef	nack_fcp
403 #undef	nacke_fcp
404 #undef	hack_24xx
405 #undef	abts
406 #undef	abts_rsp
407 #undef	els
408 #undef	hdrp
409 	return (rval);
410 }
411 
412 int
413 isp_target_put_entry(ispsoftc_t *isp, void *ap)
414 {
415 	void *outp;
416 	uint8_t etype = ((isphdr_t *) ap)->rqs_entry_type;
417 
418 	outp = isp_getrqentry(isp);
419 	if (outp == NULL) {
420 		isp_prt(isp, ISP_LOGWARN, rqo, __func__);
421 		return (-1);
422 	}
423 	switch (etype) {
424 	case RQSTYPE_ATIO2:
425 		if (ISP_CAP_2KLOGIN(isp)) {
426 			isp_put_atio2e(isp, (at2e_entry_t *) ap, (at2e_entry_t *) outp);
427 		} else {
428 			isp_put_atio2(isp, (at2_entry_t *) ap, (at2_entry_t *) outp);
429 		}
430 		break;
431 	case RQSTYPE_CTIO2:
432 		if (ISP_CAP_2KLOGIN(isp)) {
433 			isp_put_ctio2e(isp, (ct2e_entry_t *) ap, (ct2e_entry_t *) outp);
434 		} else {
435 			isp_put_ctio2(isp, (ct2_entry_t *) ap, (ct2_entry_t *) outp);
436 		}
437 		break;
438 	case RQSTYPE_CTIO7:
439 		isp_put_ctio7(isp, (ct7_entry_t *) ap, (ct7_entry_t *) outp);
440 		break;
441 	default:
442 		isp_prt(isp, ISP_LOGERR, "%s: Unknown type 0x%x", __func__, etype);
443 		return (-1);
444 	}
445 	ISP_TDQE(isp, __func__, isp->isp_reqidx, ap);
446 	ISP_SYNC_REQUEST(isp);
447 	return (0);
448 }
449 
450 int
451 isp_target_put_atio(ispsoftc_t *isp, void *arg)
452 {
453 	at2_entry_t *aep = arg;
454 	union {
455 		at2_entry_t _atio2;
456 		at2e_entry_t _atio2e;
457 	} atun;
458 
459 	ISP_MEMZERO(&atun, sizeof atun);
460 	atun._atio2.at_header.rqs_entry_type = RQSTYPE_ATIO2;
461 	atun._atio2.at_header.rqs_entry_count = 1;
462 	if (ISP_CAP_SCCFW(isp)) {
463 		atun._atio2.at_scclun = aep->at_scclun;
464 	} else {
465 		atun._atio2.at_lun = (uint8_t) aep->at_lun;
466 	}
467 	if (ISP_CAP_2KLOGIN(isp)) {
468 		atun._atio2e.at_iid = ((at2e_entry_t *)aep)->at_iid;
469 	} else {
470 		atun._atio2.at_iid = aep->at_iid;
471 	}
472 	atun._atio2.at_rxid = aep->at_rxid;
473 	atun._atio2.at_status = CT_OK;
474 	return (isp_target_put_entry(isp, &atun));
475 }
476 
477 /*
478  * Command completion- both for handling cases of no resources or
479  * no blackhole driver, or other cases where we have to, inline,
480  * finish the command sanely, or for normal command completion.
481  *
482  * The 'completion' code value has the scsi status byte in the low 8 bits.
483  * If status is a CHECK CONDITION and bit 8 is nonzero, then bits 12..15 have
484  * the sense key and  bits 16..23 have the ASCQ and bits 24..31 have the ASC
485  * values.
486  *
487  * NB: the key, asc, ascq, cannot be used for parallel SCSI as it doesn't
488  * NB: inline SCSI sense reporting. As such, we lose this information. XXX.
489  *
490  * For both parallel && fibre channel, we use the feature that does
491  * an automatic resource autoreplenish so we don't have then later do
492  * put of an atio to replenish the f/w's resource count.
493  */
494 
495 int
496 isp_endcmd(ispsoftc_t *isp, ...)
497 {
498 	uint32_t code, hdl;
499 	uint8_t sts;
500 	union {
501 		ct2_entry_t _ctio2;
502 		ct2e_entry_t _ctio2e;
503 		ct7_entry_t _ctio7;
504 	} un;
505 	va_list ap;
506 
507 	ISP_MEMZERO(&un, sizeof un);
508 
509 	if (IS_24XX(isp)) {
510 		int vpidx, nphdl;
511 		at7_entry_t *aep;
512 		ct7_entry_t *cto = &un._ctio7;
513 
514 		va_start(ap, isp);
515 		aep = va_arg(ap, at7_entry_t *);
516 		nphdl = va_arg(ap, int);
517 		/*
518 		 * Note that vpidx may equal 0xff (unknown) here
519 		 */
520 		vpidx = va_arg(ap, int);
521 		code = va_arg(ap, uint32_t);
522 		hdl = va_arg(ap, uint32_t);
523 		va_end(ap);
524 		isp_prt(isp, ISP_LOGTDEBUG0, "%s: [RX_ID 0x%x] chan %d code %x", __func__, aep->at_rxid, vpidx, code);
525 
526 		sts = code & 0xff;
527 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
528 		cto->ct_header.rqs_entry_count = 1;
529 		cto->ct_nphdl = nphdl;
530 		cto->ct_rxid = aep->at_rxid;
531 		cto->ct_iid_lo = (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
532 		cto->ct_iid_hi = aep->at_hdr.s_id[0];
533 		cto->ct_oxid = aep->at_hdr.ox_id;
534 		cto->ct_scsi_status = sts;
535 		cto->ct_vpidx = vpidx;
536 		cto->ct_flags = CT7_NOACK;
537 		if (code & ECMD_TERMINATE) {
538 			cto->ct_flags |= CT7_TERMINATE;
539 		} else if (code & ECMD_SVALID) {
540 			cto->ct_flags |= CT7_FLAG_MODE1 | CT7_SENDSTATUS;
541 			cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8);
542 			cto->ct_senselen = min(16, MAXRESPLEN_24XX);
543 			ISP_MEMZERO(cto->rsp.m1.ct_resp, sizeof (cto->rsp.m1.ct_resp));
544 			cto->rsp.m1.ct_resp[0] = 0xf0;
545 			cto->rsp.m1.ct_resp[2] = (code >> 12) & 0xf;
546 			cto->rsp.m1.ct_resp[7] = 8;
547 			cto->rsp.m1.ct_resp[12] = (code >> 16) & 0xff;
548 			cto->rsp.m1.ct_resp[13] = (code >> 24) & 0xff;
549 		} else if (code & ECMD_RVALID) {
550 			cto->ct_flags |= CT7_FLAG_MODE1 | CT7_SENDSTATUS;
551 			cto->ct_scsi_status |= (FCP_RSPLEN_VALID << 8);
552 			cto->rsp.m1.ct_resplen = 4;
553 			ISP_MEMZERO(cto->rsp.m1.ct_resp, sizeof (cto->rsp.m1.ct_resp));
554 			cto->rsp.m1.ct_resp[0] = (code >> 12) & 0xf;
555 			cto->rsp.m1.ct_resp[1] = (code >> 16) & 0xff;
556 			cto->rsp.m1.ct_resp[2] = (code >> 24) & 0xff;
557 			cto->rsp.m1.ct_resp[3] = 0;
558 		} else {
559 			cto->ct_flags |= CT7_FLAG_MODE1 | CT7_SENDSTATUS;
560 		}
561 		if (aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl) {
562 			cto->ct_resid = aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl;
563 			if (cto->ct_resid < 0) {
564 				 cto->ct_scsi_status |= (FCP_RESID_OVERFLOW << 8);
565 			} else if (cto->ct_resid > 0) {
566 				 cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8);
567 			}
568 		}
569 		cto->ct_syshandle = hdl;
570 	} else {
571 		at2_entry_t *aep;
572 		ct2_entry_t *cto = &un._ctio2;
573 
574 		va_start(ap, isp);
575 		aep = va_arg(ap, at2_entry_t *);
576 		code = va_arg(ap, uint32_t);
577 		hdl = va_arg(ap, uint32_t);
578 		va_end(ap);
579 
580 		isp_prt(isp, ISP_LOGTDEBUG0, "%s: [RX_ID 0x%x] code %x", __func__, aep->at_rxid, code);
581 
582 		sts = code & 0xff;
583 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
584 		cto->ct_header.rqs_entry_count = 1;
585 		if (ISP_CAP_SCCFW(isp) == 0) {
586 			cto->ct_lun = aep->at_lun;
587 		}
588 		if (ISP_CAP_2KLOGIN(isp)) {
589 			un._ctio2e.ct_iid = ((at2e_entry_t *)aep)->at_iid;
590 		} else {
591 			cto->ct_iid = aep->at_iid;
592 		}
593 		cto->ct_rxid = aep->at_rxid;
594 		cto->rsp.m1.ct_scsi_status = sts;
595 		cto->ct_flags = CT2_SENDSTATUS | CT2_NO_DATA | CT2_FLAG_MODE1;
596 		if (hdl == 0) {
597 			cto->ct_flags |= CT2_CCINCR;
598 		}
599 		if (aep->at_datalen) {
600 			cto->ct_resid = aep->at_datalen;
601 			cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER;
602 		}
603 		if (sts == SCSI_CHECK && (code & ECMD_SVALID)) {
604 			cto->rsp.m1.ct_resp[0] = 0xf0;
605 			cto->rsp.m1.ct_resp[2] = (code >> 12) & 0xf;
606 			cto->rsp.m1.ct_resp[7] = 8;
607 			cto->rsp.m1.ct_resp[12] = (code >> 24) & 0xff;
608 			cto->rsp.m1.ct_resp[13] = (code >> 16) & 0xff;
609 			cto->rsp.m1.ct_senselen = 16;
610 			cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
611 		}
612 		cto->ct_syshandle = hdl;
613 	}
614 	return (isp_target_put_entry(isp, &un));
615 }
616 
617 /*
618  * These are either broadcast events or specifically CTIO fast completion
619  */
620 
621 int
622 isp_target_async(ispsoftc_t *isp, int bus, int event)
623 {
624 	isp_notify_t notify;
625 
626 	ISP_MEMZERO(&notify, sizeof (isp_notify_t));
627 	notify.nt_hba = isp;
628 	notify.nt_wwn = INI_ANY;
629 	notify.nt_nphdl = NIL_HANDLE;
630 	notify.nt_sid = PORT_ANY;
631 	notify.nt_did = PORT_ANY;
632 	notify.nt_tgt = TGT_ANY;
633 	notify.nt_channel = bus;
634 	notify.nt_lun = LUN_ANY;
635 	notify.nt_tagval = TAG_ANY;
636 	notify.nt_tagval |= (((uint64_t)(isp->isp_serno++)) << 32);
637 
638 	switch (event) {
639 	case ASYNC_LOOP_UP:
640 	case ASYNC_PTPMODE:
641 		isp_prt(isp, ISP_LOGTDEBUG0, "%s: LOOP UP", __func__);
642 		notify.nt_ncode = NT_LINK_UP;
643 		isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
644 		break;
645 	case ASYNC_LOOP_DOWN:
646 		isp_prt(isp, ISP_LOGTDEBUG0, "%s: LOOP DOWN", __func__);
647 		notify.nt_ncode = NT_LINK_DOWN;
648 		isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
649 		break;
650 	case ASYNC_LIP_ERROR:
651 	case ASYNC_LIP_NOS_OLS_RECV:
652 	case ASYNC_LIP_OCCURRED:
653 	case ASYNC_LOOP_RESET:
654 		isp_prt(isp, ISP_LOGTDEBUG0, "%s: LIP RESET", __func__);
655 		notify.nt_ncode = NT_LIP_RESET;
656 		isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
657 		break;
658 	case ASYNC_BUS_RESET:
659 	case ASYNC_TIMEOUT_RESET:	/* XXX: where does this come from ? */
660 		isp_prt(isp, ISP_LOGTDEBUG0, "%s: BUS RESET", __func__);
661 		notify.nt_ncode = NT_BUS_RESET;
662 		isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
663 		break;
664 	case ASYNC_DEVICE_RESET:
665 		isp_prt(isp, ISP_LOGTDEBUG0, "%s: DEVICE RESET", __func__);
666 		notify.nt_ncode = NT_TARGET_RESET;
667 		isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
668 		break;
669 	case ASYNC_CTIO_DONE:
670 	{
671 		uint8_t storage[QENTRY_LEN];
672 		isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO DONE", __func__);
673 		memset(storage, 0, QENTRY_LEN);
674 		if (IS_24XX(isp)) {
675 			ct7_entry_t *ct = (ct7_entry_t *) storage;
676 			ct->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
677 			ct->ct_nphdl = CT7_OK;
678 			ct->ct_syshandle = bus;
679 			ct->ct_flags = CT7_SENDSTATUS;
680 		} else {
681             		/* This should also suffice for 2K login code */
682 			ct2_entry_t *ct = (ct2_entry_t *) storage;
683 			ct->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
684 			ct->ct_status = CT_OK;
685 			ct->ct_syshandle = bus;
686 			ct->ct_flags = CT2_SENDSTATUS|CT2_FASTPOST;
687 		}
688 		isp_async(isp, ISPASYNC_TARGET_ACTION, storage);
689 		break;
690 	}
691 	default:
692 		isp_prt(isp, ISP_LOGERR, "%s: unknown event 0x%x", __func__, event);
693 		if (isp->isp_state == ISP_RUNSTATE) {
694 			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, NULL);
695 		}
696 		break;
697 	}
698 	return (0);
699 }
700 
701 /*
702  * Synthesize a message from the task management flags in a FCP_CMND_IU.
703  */
704 static void
705 isp_got_msg_fc(ispsoftc_t *isp, in_fcentry_t *inp)
706 {
707 	isp_notify_t notify;
708 	static const char f1[] = "%s from N-port handle 0x%x lun %x seq 0x%x";
709 	static const char f2[] = "unknown %s 0x%x lun %x N-Port handle 0x%x task flags 0x%x seq 0x%x\n";
710 	uint16_t seqid, nphdl;
711 
712 	ISP_MEMZERO(&notify, sizeof (isp_notify_t));
713 	notify.nt_hba = isp;
714 	notify.nt_wwn = INI_ANY;
715 	if (ISP_CAP_2KLOGIN(isp)) {
716 		notify.nt_nphdl = ((in_fcentry_e_t *)inp)->in_iid;
717 		nphdl = ((in_fcentry_e_t *)inp)->in_iid;
718 		seqid = ((in_fcentry_e_t *)inp)->in_seqid;
719 	} else {
720 		notify.nt_nphdl = inp->in_iid;
721 		nphdl = inp->in_iid;
722 		seqid = inp->in_seqid;
723 	}
724 	notify.nt_sid = PORT_ANY;
725 	notify.nt_did = PORT_ANY;
726 
727 	/* nt_tgt set in outer layers */
728 	if (ISP_CAP_SCCFW(isp)) {
729 		notify.nt_lun = inp->in_scclun;
730 #if __FreeBSD_version < 1000700
731 		notify.nt_lun &= 0x3fff;
732 #endif
733 	} else {
734 		notify.nt_lun = inp->in_lun;
735 	}
736 	notify.nt_tagval = seqid;
737 	notify.nt_tagval |= (((uint64_t)(isp->isp_serno++)) << 32);
738 	notify.nt_need_ack = 1;
739 	notify.nt_lreserved = inp;
740 
741 	if (inp->in_status != IN_MSG_RECEIVED) {
742 		isp_prt(isp, ISP_LOGINFO, f2, "immediate notify status", inp->in_status, notify.nt_lun, nphdl, inp->in_task_flags, inp->in_seqid);
743 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inp);
744 		return;
745 	}
746 
747 	if (inp->in_task_flags & TASK_FLAGS_ABORT_TASK_SET) {
748 		isp_prt(isp, ISP_LOGINFO, f1, "ABORT TASK SET", nphdl, notify.nt_lun, inp->in_seqid);
749 		notify.nt_ncode = NT_ABORT_TASK_SET;
750 	} else if (inp->in_task_flags & TASK_FLAGS_CLEAR_TASK_SET) {
751 		isp_prt(isp, ISP_LOGINFO, f1, "CLEAR TASK SET", nphdl, notify.nt_lun, inp->in_seqid);
752 		notify.nt_ncode = NT_CLEAR_TASK_SET;
753 	} else if (inp->in_task_flags & TASK_FLAGS_LUN_RESET) {
754 		isp_prt(isp, ISP_LOGINFO, f1, "LUN RESET", nphdl, notify.nt_lun, inp->in_seqid);
755 		notify.nt_ncode = NT_LUN_RESET;
756 	} else if (inp->in_task_flags & TASK_FLAGS_TARGET_RESET) {
757 		isp_prt(isp, ISP_LOGINFO, f1, "TARGET RESET", nphdl, notify.nt_lun, inp->in_seqid);
758 		notify.nt_ncode = NT_TARGET_RESET;
759 	} else if (inp->in_task_flags & TASK_FLAGS_CLEAR_ACA) {
760 		isp_prt(isp, ISP_LOGINFO, f1, "CLEAR ACA", nphdl, notify.nt_lun, inp->in_seqid);
761 		notify.nt_ncode = NT_CLEAR_ACA;
762 	} else {
763 		isp_prt(isp, ISP_LOGWARN, f2, "task flag", inp->in_status, notify.nt_lun, nphdl, inp->in_task_flags,  inp->in_seqid);
764 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inp);
765 		return;
766 	}
767 	isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
768 }
769 
770 static void
771 isp_got_tmf_24xx(ispsoftc_t *isp, at7_entry_t *aep)
772 {
773 	isp_notify_t notify;
774 	static const char f1[] = "%s from PortID 0x%06x lun %x seq 0x%08x";
775 	static const char f2[] = "unknown Task Flag 0x%x lun %x PortID 0x%x tag 0x%08x";
776 	fcportdb_t *lp;
777 	uint16_t chan;
778 	uint32_t sid, did;
779 
780 	ISP_MEMZERO(&notify, sizeof (isp_notify_t));
781 	notify.nt_hba = isp;
782 	notify.nt_wwn = INI_ANY;
783 	notify.nt_lun = (aep->at_cmnd.fcp_cmnd_lun[0] << 8) | (aep->at_cmnd.fcp_cmnd_lun[1]);
784 	notify.nt_tagval = aep->at_rxid;
785 	notify.nt_tagval |= (((uint64_t)(isp->isp_serno++)) << 32);
786 	notify.nt_lreserved = aep;
787 	sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
788 	did = (aep->at_hdr.d_id[0] << 16) | (aep->at_hdr.d_id[1] << 8) | aep->at_hdr.d_id[2];
789 	if (ISP_CAP_MULTI_ID(isp) && isp->isp_nchan > 1) {
790 		/* Channel has to be derived from D_ID */
791 		isp_find_chan_by_did(isp, did, &chan);
792 		if (chan == ISP_NOCHAN) {
793 			isp_prt(isp, ISP_LOGWARN, "%s: D_ID 0x%x not found on any channel", __func__, did);
794 			isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
795 			return;
796 		}
797 	} else {
798 		chan = 0;
799 	}
800 	if (isp_find_pdb_by_portid(isp, chan, sid, &lp))
801 		notify.nt_nphdl = lp->handle;
802 	else
803 		notify.nt_nphdl = NIL_HANDLE;
804 	notify.nt_sid = sid;
805 	notify.nt_did = did;
806 	notify.nt_channel = chan;
807 	if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_QUERY_TASK_SET) {
808 		isp_prt(isp, ISP_LOGINFO, f1, "QUERY TASK SET", sid, notify.nt_lun, aep->at_rxid);
809 		notify.nt_ncode = NT_QUERY_TASK_SET;
810 	} else if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_ABORT_TASK_SET) {
811 		isp_prt(isp, ISP_LOGINFO, f1, "ABORT TASK SET", sid, notify.nt_lun, aep->at_rxid);
812 		notify.nt_ncode = NT_ABORT_TASK_SET;
813 	} else if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_CLEAR_TASK_SET) {
814 		isp_prt(isp, ISP_LOGINFO, f1, "CLEAR TASK SET", sid, notify.nt_lun, aep->at_rxid);
815 		notify.nt_ncode = NT_CLEAR_TASK_SET;
816 	} else if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_QUERY_ASYNC_EVENT) {
817 		isp_prt(isp, ISP_LOGINFO, f1, "QUERY ASYNC EVENT", sid, notify.nt_lun, aep->at_rxid);
818 		notify.nt_ncode = NT_QUERY_ASYNC_EVENT;
819 	} else if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_LUN_RESET) {
820 		isp_prt(isp, ISP_LOGINFO, f1, "LUN RESET", sid, notify.nt_lun, aep->at_rxid);
821 		notify.nt_ncode = NT_LUN_RESET;
822 	} else if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_TGT_RESET) {
823 		isp_prt(isp, ISP_LOGINFO, f1, "TARGET RESET", sid, notify.nt_lun, aep->at_rxid);
824 		notify.nt_ncode = NT_TARGET_RESET;
825 	} else if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_CLEAR_ACA) {
826 		isp_prt(isp, ISP_LOGINFO, f1, "CLEAR ACA", sid, notify.nt_lun, aep->at_rxid);
827 		notify.nt_ncode = NT_CLEAR_ACA;
828 	} else {
829 		isp_prt(isp, ISP_LOGWARN, f2, aep->at_cmnd.fcp_cmnd_task_management, notify.nt_lun, sid, aep->at_rxid);
830 		notify.nt_ncode = NT_UNKNOWN;
831 		isp_endcmd(isp, aep, notify.nt_nphdl, chan, ECMD_RVALID | (0x4 << 12), 0);
832 		return;
833 	}
834 	isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
835 }
836 
837 int
838 isp_notify_ack(ispsoftc_t *isp, void *arg)
839 {
840 	char storage[QENTRY_LEN];
841 	void *outp;
842 
843 	/*
844 	 * This is in case a Task Management Function ends up here.
845 	 */
846 	if (IS_24XX(isp) && arg != NULL && (((isphdr_t *)arg)->rqs_entry_type == RQSTYPE_ATIO)) {
847 		at7_entry_t *aep = arg;
848 		return (isp_endcmd(isp, aep, NIL_HANDLE, 0, 0, 0));
849 	}
850 
851 	outp = isp_getrqentry(isp);
852 	if (outp == NULL) {
853 		isp_prt(isp, ISP_LOGWARN, rqo, __func__);
854 		return (1);
855 	}
856 
857 	ISP_MEMZERO(storage, QENTRY_LEN);
858 
859 	if (IS_24XX(isp)) {
860 		na_fcentry_24xx_t *na = (na_fcentry_24xx_t *) storage;
861 		na->na_header.rqs_entry_type = RQSTYPE_NOTIFY_ACK;
862 		na->na_header.rqs_entry_count = 1;
863 		if (arg) {
864 			in_fcentry_24xx_t *in = arg;
865 			na->na_nphdl = in->in_nphdl;
866 			na->na_flags = in->in_flags;
867 			na->na_status = in->in_status;
868 			na->na_status_subcode = in->in_status_subcode;
869 			na->na_fwhandle = in->in_fwhandle;
870 			na->na_rxid = in->in_rxid;
871 			na->na_oxid = in->in_oxid;
872 			na->na_vpidx = in->in_vpidx;
873 			if (in->in_status == IN24XX_SRR_RCVD) {
874 				na->na_srr_rxid = in->in_srr_rxid;
875 				na->na_srr_reloff_hi = in->in_srr_reloff_hi;
876 				na->na_srr_reloff_lo = in->in_srr_reloff_lo;
877 				na->na_srr_iu = in->in_srr_iu;
878 				/*
879 				 * Whether we're accepting the SRR or rejecting
880 				 * it is determined by looking at the in_reserved
881 				 * field in the original notify structure.
882 				 */
883 				if (in->in_reserved) {
884 					na->na_srr_flags = 1;
885 					na->na_srr_reject_vunique = 0;
886 					na->na_srr_reject_code = 9;		/* unable to perform this command at this time */
887 					na->na_srr_reject_explanation = 0x2a;	/* unable to supply the requested data */
888 				}
889 			}
890 		}
891 		isp_put_notify_24xx_ack(isp, na, (na_fcentry_24xx_t *)outp);
892 	} else {
893 		na_fcentry_t *na = (na_fcentry_t *) storage;
894 		int iid = 0;
895 
896 		if (arg) {
897 			in_fcentry_t *inp = arg;
898 			ISP_MEMCPY(storage, arg, sizeof (isphdr_t));
899 			if (ISP_CAP_2KLOGIN(isp)) {
900 				((na_fcentry_e_t *)na)->na_iid = ((in_fcentry_e_t *)inp)->in_iid;
901 				iid = ((na_fcentry_e_t *)na)->na_iid;
902 			} else {
903 				na->na_iid = inp->in_iid;
904 				iid = na->na_iid;
905 			}
906 			na->na_task_flags = inp->in_task_flags & TASK_FLAGS_RESERVED_MASK;
907 			na->na_seqid = inp->in_seqid;
908 			na->na_status = inp->in_status;
909 			na->na_flags = NAFC_RCOUNT;
910 			if (inp->in_status == IN_RESET) {
911 				na->na_flags = NAFC_RST_CLRD;	/* We do not modify resource counts for LIP resets */
912 			}
913 			if (inp->in_status == IN_MSG_RECEIVED) {
914 				na->na_flags |= NAFC_TVALID;
915 				na->na_response = 0;	/* XXX SUCCEEDED XXX */
916 			}
917 		} else {
918 			na->na_flags = NAFC_RST_CLRD;
919 		}
920 		na->na_header.rqs_entry_type = RQSTYPE_NOTIFY_ACK;
921 		na->na_header.rqs_entry_count = 1;
922 		if (ISP_CAP_2KLOGIN(isp)) {
923 			isp_put_notify_ack_fc_e(isp, (na_fcentry_e_t *) na, (na_fcentry_e_t *)outp);
924 		} else {
925 			isp_put_notify_ack_fc(isp, na, (na_fcentry_t *)outp);
926 		}
927 		isp_prt(isp, ISP_LOGTDEBUG0, "notify ack handle %x seqid %x flags %x tflags %x response %x", iid, na->na_seqid,
928 		    na->na_flags, na->na_task_flags, na->na_response);
929 	}
930 	ISP_TDQE(isp, "isp_notify_ack", isp->isp_reqidx, storage);
931 	ISP_SYNC_REQUEST(isp);
932 	return (0);
933 }
934 
935 int
936 isp_acknak_abts(ispsoftc_t *isp, void *arg, int errno)
937 {
938 	char storage[QENTRY_LEN];
939 	uint16_t tmpw;
940 	uint8_t tmpb;
941 	abts_t *abts = arg;
942 	abts_rsp_t *rsp = (abts_rsp_t *) storage;
943 	void *outp;
944 
945 	if (!IS_24XX(isp)) {
946 		isp_prt(isp, ISP_LOGERR, "%s: called for non-24XX card", __func__);
947 		return (0);
948 	}
949 
950 	if (abts->abts_header.rqs_entry_type != RQSTYPE_ABTS_RCVD) {
951 		isp_prt(isp, ISP_LOGERR, "%s: called for non-ABTS entry (0x%x)", __func__, abts->abts_header.rqs_entry_type);
952 		return (0);
953 	}
954 
955 	outp = isp_getrqentry(isp);
956 	if (outp == NULL) {
957 		isp_prt(isp, ISP_LOGWARN, rqo, __func__);
958 		return (1);
959 	}
960 
961 	ISP_MEMCPY(rsp, abts, QENTRY_LEN);
962 	rsp->abts_rsp_header.rqs_entry_type = RQSTYPE_ABTS_RSP;
963 
964 	/*
965 	 * Swap destination and source for response.
966 	 */
967 	rsp->abts_rsp_r_ctl = BA_ACC;
968 	tmpw = rsp->abts_rsp_did_lo;
969 	tmpb = rsp->abts_rsp_did_hi;
970 	rsp->abts_rsp_did_lo = rsp->abts_rsp_sid_lo;
971 	rsp->abts_rsp_did_hi = rsp->abts_rsp_sid_hi;
972 	rsp->abts_rsp_sid_lo = tmpw;
973 	rsp->abts_rsp_sid_hi = tmpb;
974 
975 	rsp->abts_rsp_f_ctl_hi ^= 0x80; 	/* invert Exchange Context */
976 	rsp->abts_rsp_f_ctl_hi &= ~0x7f;	/* clear Sequence Initiator and other bits */
977 	rsp->abts_rsp_f_ctl_hi |= 0x10;		/* abort the whole exchange */
978 	rsp->abts_rsp_f_ctl_hi |= 0x8;		/* last data frame of sequence */
979 	rsp->abts_rsp_f_ctl_hi |= 0x1;		/* transfer Sequence Initiative */
980 	rsp->abts_rsp_f_ctl_lo = 0;
981 
982 	if (errno == 0) {
983 		uint16_t rx_id, ox_id;
984 
985 		rx_id = rsp->abts_rsp_rx_id;
986 		ox_id = rsp->abts_rsp_ox_id;
987 		ISP_MEMZERO(&rsp->abts_rsp_payload.ba_acc, sizeof (rsp->abts_rsp_payload.ba_acc));
988                 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);
989                 rsp->abts_rsp_payload.ba_acc.aborted_rx_id = rx_id;
990                 rsp->abts_rsp_payload.ba_acc.aborted_ox_id = ox_id;
991                 rsp->abts_rsp_payload.ba_acc.high_seq_cnt = 0xffff;
992 	} else {
993 		ISP_MEMZERO(&rsp->abts_rsp_payload.ba_rjt, sizeof (rsp->abts_rsp_payload.ba_acc));
994 		switch (errno) {
995 		case ENOMEM:
996 			rsp->abts_rsp_payload.ba_rjt.reason = 5;	/* Logical Unit Busy */
997 			break;
998 		default:
999 			rsp->abts_rsp_payload.ba_rjt.reason = 9;	/* Unable to perform command request */
1000 			break;
1001 		}
1002 	}
1003 
1004 	/*
1005 	 * The caller will have set response values as appropriate
1006 	 * in the ABTS structure just before calling us.
1007 	 */
1008 	isp_put_abts_rsp(isp, rsp, (abts_rsp_t *)outp);
1009 	ISP_TDQE(isp, "isp_acknak_abts", isp->isp_reqidx, storage);
1010 	ISP_SYNC_REQUEST(isp);
1011 	return (0);
1012 }
1013 
1014 static void
1015 isp_handle_atio2(ispsoftc_t *isp, at2_entry_t *aep)
1016 {
1017 	int lun, iid;
1018 
1019 	if (ISP_CAP_SCCFW(isp)) {
1020 		lun = aep->at_scclun;
1021 #if __FreeBSD_version < 1000700
1022 		lun &= 0x3fff;
1023 #endif
1024 	} else {
1025 		lun = aep->at_lun;
1026 	}
1027 
1028 	if (ISP_CAP_2KLOGIN(isp)) {
1029 		iid = ((at2e_entry_t *)aep)->at_iid;
1030 	} else {
1031 		iid = aep->at_iid;
1032 	}
1033 
1034 	/*
1035 	 * The firmware status (except for the QLTM_SVALID bit) indicates
1036 	 * why this ATIO was sent to us.
1037 	 *
1038 	 * If QLTM_SVALID is set, the firware has recommended Sense Data.
1039 	 *
1040 	 * If the DISCONNECTS DISABLED bit is set in the flags field,
1041 	 * we're still connected on the SCSI bus - i.e. the initiator
1042 	 * did not set DiscPriv in the identify message. We don't care
1043 	 * about this so it's ignored.
1044 	 */
1045 
1046 	switch (aep->at_status & ~QLTM_SVALID) {
1047 	case AT_PATH_INVALID:
1048 		/*
1049 		 * ATIO rejected by the firmware due to disabled lun.
1050 		 */
1051 		isp_prt(isp, ISP_LOGERR, "rejected ATIO2 for disabled lun %x", lun);
1052 		break;
1053 	case AT_NOCAP:
1054 		/*
1055 		 * Requested Capability not available
1056 		 * We sent an ATIO that overflowed the firmware's
1057 		 * command resource count.
1058 		 */
1059 		isp_prt(isp, ISP_LOGERR, "rejected ATIO2 for lun %x- command count overflow", lun);
1060 		break;
1061 
1062 	case AT_BDR_MSG:
1063 		/*
1064 		 * If we send an ATIO to the firmware to increment
1065 		 * its command resource count, and the firmware is
1066 		 * recovering from a Bus Device Reset, it returns
1067 		 * the ATIO with this status. We set the command
1068 		 * resource count in the Enable Lun entry and no
1069 		 * not increment it. Therefore we should never get
1070 		 * this status here.
1071 		 */
1072 		isp_prt(isp, ISP_LOGERR, atiocope, lun, 0);
1073 		break;
1074 
1075 	case AT_CDB:		/* Got a CDB */
1076 		/*
1077 		 * Punt to platform specific layer.
1078 		 */
1079 		isp_async(isp, ISPASYNC_TARGET_ACTION, aep);
1080 		break;
1081 
1082 	case AT_RESET:
1083 		/*
1084 		 * A bus reset came along an blew away this command. Why
1085 		 * they do this in addition the async event code stuff,
1086 		 * I dunno.
1087 		 *
1088 		 * Ignore it because the async event will clear things
1089 		 * up for us.
1090 		 */
1091 		isp_prt(isp, ISP_LOGERR, atior, lun, iid, 0);
1092 		break;
1093 
1094 
1095 	default:
1096 		isp_prt(isp, ISP_LOGERR, "Unknown ATIO2 status 0x%x from handle %d for lun %x", aep->at_status, iid, lun);
1097 		(void) isp_target_put_atio(isp, aep);
1098 		break;
1099 	}
1100 }
1101 
1102 static void
1103 isp_handle_ctio2(ispsoftc_t *isp, ct2_entry_t *ct)
1104 {
1105 	void *xs;
1106 	int pl = ISP_LOGTDEBUG2;
1107 	char *fmsg = NULL;
1108 
1109 	if (ct->ct_syshandle) {
1110 		xs = isp_find_xs(isp, ct->ct_syshandle);
1111 		if (xs == NULL) {
1112 			pl = ISP_LOGALL;
1113 		}
1114 	} else {
1115 		xs = NULL;
1116 	}
1117 
1118 	switch (ct->ct_status & ~QLTM_SVALID) {
1119 	case CT_BUS_ERROR:
1120 		isp_prt(isp, ISP_LOGERR, "PCI DMA Bus Error");
1121 		/* FALL Through */
1122 	case CT_DATA_OVER:
1123 	case CT_DATA_UNDER:
1124 	case CT_OK:
1125 		/*
1126 		 * There are generally 2 possibilities as to why we'd get
1127 		 * this condition:
1128 		 * 	We sent or received data.
1129 		 * 	We sent status & command complete.
1130 		 */
1131 
1132 		break;
1133 
1134 	case CT_BDR_MSG:
1135 		/*
1136 		 * Target Reset function received.
1137 		 *
1138 		 * The firmware generates an async mailbox interrupt to
1139 		 * notify us of this and returns outstanding CTIOs with this
1140 		 * status. These CTIOs are handled in that same way as
1141 		 * CT_ABORTED ones, so just fall through here.
1142 		 */
1143 		fmsg = "TARGET RESET";
1144 		/*FALLTHROUGH*/
1145 	case CT_RESET:
1146 		if (fmsg == NULL)
1147 			fmsg = "LIP Reset";
1148 		/*FALLTHROUGH*/
1149 	case CT_ABORTED:
1150 		/*
1151 		 * When an Abort message is received the firmware goes to
1152 		 * Bus Free and returns all outstanding CTIOs with the status
1153 		 * set, then sends us an Immediate Notify entry.
1154 		 */
1155 		if (fmsg == NULL) {
1156 			fmsg = "ABORT";
1157 		}
1158 
1159 		isp_prt(isp, ISP_LOGTDEBUG0, "CTIO2 destroyed by %s: RX_ID=0x%x", fmsg, ct->ct_rxid);
1160 		break;
1161 
1162 	case CT_INVAL:
1163 		/*
1164 		 * CTIO rejected by the firmware - invalid data direction.
1165 		 */
1166 		isp_prt(isp, ISP_LOGERR, "CTIO2 had wrong data direction");
1167 		break;
1168 
1169 	case CT_RSELTMO:
1170 		fmsg = "failure to reconnect to initiator";
1171 		/*FALLTHROUGH*/
1172 	case CT_TIMEOUT:
1173 		if (fmsg == NULL)
1174 			fmsg = "command";
1175 		isp_prt(isp, ISP_LOGWARN, "Firmware timed out on %s", fmsg);
1176 		break;
1177 
1178 	case CT_ERR:
1179 		fmsg = "Completed with Error";
1180 		/*FALLTHROUGH*/
1181 	case CT_LOGOUT:
1182 		if (fmsg == NULL)
1183 			fmsg = "Port Logout";
1184 		/*FALLTHROUGH*/
1185 	case CT_PORTUNAVAIL:
1186 		if (fmsg == NULL)
1187 			fmsg = "Port not available";
1188 		/*FALLTHROUGH*/
1189 	case CT_PORTCHANGED:
1190 		if (fmsg == NULL)
1191 			fmsg = "Port Changed";
1192 		/*FALLTHROUGH*/
1193 	case CT_NOACK:
1194 		if (fmsg == NULL)
1195 			fmsg = "unacknowledged Immediate Notify pending";
1196 		isp_prt(isp, ISP_LOGWARN, "CTIO returned by f/w- %s", fmsg);
1197 		break;
1198 
1199 	case CT_INVRXID:
1200 		/*
1201 		 * CTIO rejected by the firmware because an invalid RX_ID.
1202 		 * Just print a message.
1203 		 */
1204 		isp_prt(isp, ISP_LOGWARN, "CTIO2 completed with Invalid RX_ID 0x%x", ct->ct_rxid);
1205 		break;
1206 
1207 	default:
1208 		isp_prt(isp, ISP_LOGERR, "Unknown CTIO2 status 0x%x", ct->ct_status & ~QLTM_SVALID);
1209 		break;
1210 	}
1211 
1212 	if (xs == NULL) {
1213 		/*
1214 		 * There may be more than one CTIO for a data transfer,
1215 		 * or this may be a status CTIO we're not monitoring.
1216 		 *
1217 		 * The assumption is that they'll all be returned in the
1218 		 * order we got them.
1219 		 */
1220 		if (ct->ct_syshandle == 0) {
1221 			if ((ct->ct_flags & CT2_SENDSTATUS) == 0) {
1222 				isp_prt(isp, pl, "intermediate CTIO completed ok");
1223 			} else {
1224 				isp_prt(isp, pl, "unmonitored CTIO completed ok");
1225 			}
1226 		} else {
1227 			isp_prt(isp, pl, "NO xs for CTIO (handle 0x%x) status 0x%x", ct->ct_syshandle, ct->ct_status & ~QLTM_SVALID);
1228 		}
1229 	} else {
1230 		if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA) {
1231 			ISP_DMAFREE(isp, xs, ct->ct_syshandle);
1232 		}
1233 		if (ct->ct_flags & CT2_SENDSTATUS) {
1234 			/*
1235 			 * Sent status and command complete.
1236 			 *
1237 			 * We're now really done with this command, so we
1238 			 * punt to the platform dependent layers because
1239 			 * only there can we do the appropriate command
1240 			 * complete thread synchronization.
1241 			 */
1242 			isp_prt(isp, pl, "status CTIO complete");
1243 		} else {
1244 			/*
1245 			 * Final CTIO completed. Release DMA resources and
1246 			 * notify platform dependent layers.
1247 			 */
1248 			isp_prt(isp, pl, "data CTIO complete");
1249 		}
1250 		isp_async(isp, ISPASYNC_TARGET_ACTION, ct);
1251 		/*
1252 		 * The platform layer will destroy the handle if appropriate.
1253 		 */
1254 	}
1255 }
1256 
1257 static void
1258 isp_handle_ctio7(ispsoftc_t *isp, ct7_entry_t *ct)
1259 {
1260 	void *xs;
1261 	int pl = ISP_LOGTDEBUG2;
1262 	char *fmsg = NULL;
1263 
1264 	if (ct->ct_syshandle) {
1265 		xs = isp_find_xs(isp, ct->ct_syshandle);
1266 		if (xs == NULL) {
1267 			pl = ISP_LOGALL;
1268 		}
1269 	} else {
1270 		xs = NULL;
1271 	}
1272 
1273 	switch (ct->ct_nphdl) {
1274 	case CT7_BUS_ERROR:
1275 		isp_prt(isp, ISP_LOGERR, "PCI DMA Bus Error");
1276 		/* FALL Through */
1277 	case CT7_DATA_OVER:
1278 	case CT7_DATA_UNDER:
1279 	case CT7_OK:
1280 		/*
1281 		 * There are generally 2 possibilities as to why we'd get
1282 		 * this condition:
1283 		 * 	We sent or received data.
1284 		 * 	We sent status & command complete.
1285 		 */
1286 
1287 		break;
1288 
1289 	case CT7_RESET:
1290 		if (fmsg == NULL) {
1291 			fmsg = "LIP Reset";
1292 		}
1293 		/*FALLTHROUGH*/
1294 	case CT7_ABORTED:
1295 		/*
1296 		 * When an Abort message is received the firmware goes to
1297 		 * Bus Free and returns all outstanding CTIOs with the status
1298 		 * set, then sends us an Immediate Notify entry.
1299 		 */
1300 		if (fmsg == NULL) {
1301 			fmsg = "ABORT";
1302 		}
1303 		isp_prt(isp, ISP_LOGTDEBUG0, "CTIO7 destroyed by %s: RX_ID=0x%x", fmsg, ct->ct_rxid);
1304 		break;
1305 
1306 	case CT7_TIMEOUT:
1307 		if (fmsg == NULL) {
1308 			fmsg = "command";
1309 		}
1310 		isp_prt(isp, ISP_LOGWARN, "Firmware timed out on %s", fmsg);
1311 		break;
1312 
1313 	case CT7_ERR:
1314 		fmsg = "Completed with Error";
1315 		/*FALLTHROUGH*/
1316 	case CT7_LOGOUT:
1317 		if (fmsg == NULL) {
1318 			fmsg = "Port Logout";
1319 		}
1320 		/*FALLTHROUGH*/
1321 	case CT7_PORTUNAVAIL:
1322 		if (fmsg == NULL) {
1323 			fmsg = "Port not available";
1324 		}
1325 		/*FALLTHROUGH*/
1326 	case CT7_PORTCHANGED:
1327 		if (fmsg == NULL) {
1328 			fmsg = "Port Changed";
1329 		}
1330 		isp_prt(isp, ISP_LOGWARN, "CTIO returned by f/w- %s", fmsg);
1331 		break;
1332 
1333 	case CT7_INVRXID:
1334 		/*
1335 		 * CTIO rejected by the firmware because an invalid RX_ID.
1336 		 * Just print a message.
1337 		 */
1338 		isp_prt(isp, ISP_LOGWARN, "CTIO7 completed with Invalid RX_ID 0x%x", ct->ct_rxid);
1339 		break;
1340 
1341 	case CT7_REASSY_ERR:
1342 		isp_prt(isp, ISP_LOGWARN, "reassembly error");
1343 		break;
1344 
1345 	case CT7_SRR:
1346 		isp_prt(isp, ISP_LOGTDEBUG0, "SRR received");
1347 		break;
1348 
1349 	default:
1350 		isp_prt(isp, ISP_LOGERR, "Unknown CTIO7 status 0x%x", ct->ct_nphdl);
1351 		break;
1352 	}
1353 
1354 	if (xs == NULL) {
1355 		/*
1356 		 * There may be more than one CTIO for a data transfer,
1357 		 * or this may be a status CTIO we're not monitoring.
1358 		 *
1359 		 * The assumption is that they'll all be returned in the
1360 		 * order we got them.
1361 		 */
1362 		if (ct->ct_syshandle == 0) {
1363 			if (ct->ct_flags & CT7_TERMINATE) {
1364 				isp_prt(isp, ISP_LOGINFO, "termination of [RX_ID 0x%x] complete", ct->ct_rxid);
1365 			} else if ((ct->ct_flags & CT7_SENDSTATUS) == 0) {
1366 				isp_prt(isp, pl, "intermediate CTIO completed ok");
1367 			} else {
1368 				isp_prt(isp, pl, "unmonitored CTIO completed ok");
1369 			}
1370 		} else {
1371 			isp_prt(isp, pl, "NO xs for CTIO (handle 0x%x) status 0x%x", ct->ct_syshandle, ct->ct_nphdl);
1372 		}
1373 	} else {
1374 		if ((ct->ct_flags & CT7_DATAMASK) != CT7_NO_DATA) {
1375 			ISP_DMAFREE(isp, xs, ct->ct_syshandle);
1376 		}
1377 		if (ct->ct_flags & CT7_SENDSTATUS) {
1378 			/*
1379 			 * Sent status and command complete.
1380 			 *
1381 			 * We're now really done with this command, so we
1382 			 * punt to the platform dependent layers because
1383 			 * only there can we do the appropriate command
1384 			 * complete thread synchronization.
1385 			 */
1386 			isp_prt(isp, pl, "status CTIO complete");
1387 		} else {
1388 			/*
1389 			 * Final CTIO completed. Release DMA resources and
1390 			 * notify platform dependent layers.
1391 			 */
1392 			isp_prt(isp, pl, "data CTIO complete");
1393 		}
1394 		isp_async(isp, ISPASYNC_TARGET_ACTION, ct);
1395 		/*
1396 		 * The platform layer will destroy the handle if appropriate.
1397 		 */
1398 	}
1399 }
1400 
1401 static void
1402 isp_handle_24xx_inotify(ispsoftc_t *isp, in_fcentry_24xx_t *inot_24xx)
1403 {
1404 	uint8_t ochan, chan, lochan, hichan;
1405 
1406 	/*
1407 	 * Check to see whether we got a wildcard channel.
1408 	 * If so, we have to iterate over all channels.
1409 	 */
1410 	ochan = chan = ISP_GET_VPIDX(isp, inot_24xx->in_vpidx);
1411 	if (chan == 0xff) {
1412 		lochan = 0;
1413 		hichan = isp->isp_nchan;
1414 	} else {
1415 		if (chan >= isp->isp_nchan) {
1416 			char buf[64];
1417 			ISP_SNPRINTF(buf, sizeof buf, "%s: bad channel %d for status 0x%x", __func__, chan, inot_24xx->in_status);
1418 			isp_print_bytes(isp, buf, QENTRY_LEN, inot_24xx);
1419 			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot_24xx);
1420 			return;
1421 		}
1422 		lochan = chan;
1423 		hichan = chan + 1;
1424 	}
1425 	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);
1426 	for (chan = lochan; chan < hichan; chan++) {
1427 		if (FCPARAM(isp, chan)->role == ISP_ROLE_NONE)
1428 			continue;
1429 		switch (inot_24xx->in_status) {
1430 		case IN24XX_LIP_RESET:
1431 		case IN24XX_LINK_RESET:
1432 		case IN24XX_PORT_LOGOUT:
1433 		case IN24XX_PORT_CHANGED:
1434 		case IN24XX_LINK_FAILED:
1435 		case IN24XX_SRR_RCVD:
1436 		case IN24XX_ELS_RCVD:
1437 			inot_24xx->in_reserved = 0;	/* clear this for later usage */
1438 			inot_24xx->in_vpidx = chan;
1439 			isp_async(isp, ISPASYNC_TARGET_ACTION, inot_24xx);
1440 			break;
1441 		default:
1442 			isp_prt(isp, ISP_LOGINFO, "%s: unhandled status (0x%x) for chan %d", __func__, inot_24xx->in_status, chan);
1443 			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot_24xx);
1444 			break;
1445 		}
1446 	}
1447 	inot_24xx->in_vpidx = ochan;
1448 }
1449 #endif
1450