xref: /freebsd/sys/dev/isp/isp_target.c (revision b8e2395ec5785741caa9f79956b890358cdfb61e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  *  Copyright (c) 2009-2020 Alexander Motin <mav@FreeBSD.org>
5  *  Copyright (c) 1997-2009 by Matthew Jacob
6  *  All rights reserved.
7  *
8  *  Redistribution and use in source and binary forms, with or without
9  *  modification, are permitted provided that the following conditions
10  *  are met:
11  *
12  *  1. Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *
18  *  THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  *  ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  *  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  *  SUCH DAMAGE.
29  *
30  */
31 /*
32  * Machine and OS Independent Target Mode Code for the Qlogic FC adapters.
33  */
34 /*
35  * Bug fixes gratefully acknowledged from:
36  *	Oded Kedem <oded@kashya.com>
37  */
38 /*
39  * Include header file appropriate for platform we're building on.
40  */
41 
42 #ifdef	__NetBSD__
43 #include <dev/ic/isp_netbsd.h>
44 #endif
45 #ifdef	__FreeBSD__
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
48 #include <dev/isp/isp_freebsd.h>
49 #endif
50 #ifdef	__OpenBSD__
51 #include <dev/ic/isp_openbsd.h>
52 #endif
53 #ifdef	__linux__
54 #include "isp_linux.h"
55 #endif
56 
57 #ifdef	ISP_TARGET_MODE
58 static const char rqo[] = "%s: Request Queue Overflow";
59 
60 static void isp_got_tmf_24xx(ispsoftc_t *, at7_entry_t *);
61 static void isp_handle_abts(ispsoftc_t *, abts_t *);
62 static void isp_handle_ctio7(ispsoftc_t *, ct7_entry_t *);
63 static void isp_handle_notify_24xx(ispsoftc_t *, in_fcentry_24xx_t *);
64 
65 /*
66  * The Qlogic driver gets an interrupt to look at response queue entries.
67  * Some of these are status completions for initiatior mode commands, but
68  * if target mode is enabled, we get a whole wad of response queue entries
69  * to be handled here.
70  *
71  * Basically the split into 3 main groups: Lun Enable/Modification responses,
72  * SCSI Command processing, and Immediate Notification events.
73  *
74  * You start by writing a request queue entry to enable target mode (and
75  * establish some resource limitations which you can modify later).
76  * The f/w responds with a LUN ENABLE or LUN MODIFY response with
77  * the status of this action. If the enable was successful, you can expect...
78  *
79  * Response queue entries with SCSI commands encapsulate show up in an ATIO
80  * (Accept Target IO) type- sometimes with enough info to stop the command at
81  * this level. Ultimately the driver has to feed back to the f/w's request
82  * queue a sequence of CTIOs (continue target I/O) that describe data to
83  * be moved and/or status to be sent) and finally finishing with sending
84  * to the f/w's response queue an ATIO which then completes the handshake
85  * with the f/w for that command. There's a lot of variations on this theme,
86  * including flags you can set in the CTIO for the Qlogic 2X00 fibre channel
87  * cards that 'auto-replenish' the f/w's ATIO count, but this is the basic
88  * gist of it.
89  *
90  * The third group that can show up in the response queue are Immediate
91  * Notification events. These include things like notifications of SCSI bus
92  * resets, or Bus Device Reset messages or other messages received. This
93  * a classic oddbins area. It can get  a little weird because you then turn
94  * around and acknowledge the Immediate Notify by writing an entry onto the
95  * request queue and then the f/w turns around and gives you an acknowledgement
96  * to *your* acknowledgement on the response queue (the idea being to let
97  * the f/w tell you when the event is *really* over I guess).
98  *
99  */
100 
101 
102 /*
103  * A new response queue entry has arrived. The interrupt service code
104  * has already swizzled it into the platform dependent from canonical form.
105  *
106  * Because of the way this driver is designed, unfortunately most of the
107  * actual synchronization work has to be done in the platform specific
108  * code- we have no synchroniation primitives in the common code.
109  */
110 
111 int
112 isp_target_notify(ispsoftc_t *isp, void *vptr, uint32_t *optrp, uint16_t ql)
113 {
114 	union {
115 		at7_entry_t	*at7iop;
116 		ct7_entry_t	*ct7iop;
117 		in_fcentry_24xx_t *inot_24xx;
118 		na_fcentry_24xx_t *nack_24xx;
119 		isphdr_t	*hp;
120 		abts_t		*abts;
121 		abts_rsp_t	*abts_rsp;
122 		void *		*vp;
123 #define	at7iop		unp.at7iop
124 #define	ct7iop		unp.ct7iop
125 #define	inot_24xx	unp.inot_24xx
126 #define	nack_24xx	unp.nack_24xx
127 #define	abts		unp.abts
128 #define	abts_rsp	unp.abts_rsp
129 #define	hdrp		unp.hp
130 	} unp;
131 	uint8_t local[QENTRY_LEN];
132 	int type, len, level, rval = 1;
133 
134 	type = isp_get_response_type(isp, (isphdr_t *)vptr);
135 	unp.vp = vptr;
136 
137 	ISP_TDQE(isp, "isp_target_notify", (int) *optrp, vptr);
138 
139 	switch (type) {
140 	case RQSTYPE_ATIO:
141 		isp_get_atio7(isp, at7iop, (at7_entry_t *) local);
142 		at7iop = (at7_entry_t *) local;
143 		/*
144 		 * Check for and do something with commands whose
145 		 * IULEN extends past a single queue entry.
146 		 */
147 		len = at7iop->at_ta_len & 0x0fff;
148 		if (len > (QENTRY_LEN - 8)) {
149 			len -= (QENTRY_LEN - 8);
150 			isp_prt(isp, ISP_LOGINFO, "long IU length (%d) ignored", len);
151 			while (len > 0) {
152 				*optrp = ISP_NXT_QENTRY(*optrp, ql);
153 				len -= QENTRY_LEN;
154 			}
155 		}
156 		/*
157 		 * Check for a task management function
158 		 */
159 		if (at7iop->at_cmnd.fcp_cmnd_task_management) {
160 			isp_got_tmf_24xx(isp, at7iop);
161 			break;
162 		}
163 		/*
164 		 * Just go straight to outer layer for this one.
165 		 */
166 		isp_async(isp, ISPASYNC_TARGET_ACTION, local);
167 		break;
168 
169 	case RQSTYPE_CTIO7:
170 		isp_get_ctio7(isp, ct7iop, (ct7_entry_t *) local);
171 		isp_handle_ctio7(isp, (ct7_entry_t *) local);
172 		break;
173 
174 	case RQSTYPE_NOTIFY:
175 		isp_get_notify_24xx(isp, inot_24xx, (in_fcentry_24xx_t *)local);
176 		isp_handle_notify_24xx(isp, (in_fcentry_24xx_t *)local);
177 
178 	case RQSTYPE_NOTIFY_ACK:
179 		/*
180 		 * The ISP is acknowledging our acknowledgement of an
181 		 * Immediate Notify entry for some asynchronous event.
182 		 */
183 		isp_get_notify_ack_24xx(isp, nack_24xx, (na_fcentry_24xx_t *) local);
184 		nack_24xx = (na_fcentry_24xx_t *) local;
185 		if (nack_24xx->na_status != NA_OK)
186 			level = ISP_LOGINFO;
187 		else
188 			level = ISP_LOGTDEBUG1;
189 		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);
190 		break;
191 
192 	case RQSTYPE_ABTS_RCVD:
193 		isp_get_abts(isp, abts, (abts_t *)local);
194 		isp_handle_abts(isp, (abts_t *)local);
195 		break;
196 	case RQSTYPE_ABTS_RSP:
197 		isp_get_abts_rsp(isp, abts_rsp, (abts_rsp_t *)local);
198 		abts_rsp = (abts_rsp_t *) local;
199 		if (abts_rsp->abts_rsp_status)
200 			level = ISP_LOGINFO;
201 		else
202 			level = ISP_LOGTDEBUG0;
203 		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,
204 		    abts_rsp->abts_rsp_payload.rsp.subcode1, abts_rsp->abts_rsp_payload.rsp.subcode2);
205 		break;
206 	default:
207 		isp_prt(isp, ISP_LOGERR, "%s: unknown entry type 0x%x", __func__, type);
208 		rval = 0;
209 		break;
210 	}
211 #undef	at7iop
212 #undef	ct7iop
213 #undef	inot_24xx
214 #undef	hack_24xx
215 #undef	abts
216 #undef	abts_rsp
217 #undef	hdrp
218 	return (rval);
219 }
220 
221 int
222 isp_target_put_entry(ispsoftc_t *isp, void *ap)
223 {
224 	void *outp;
225 	uint8_t etype = ((isphdr_t *) ap)->rqs_entry_type;
226 
227 	outp = isp_getrqentry(isp);
228 	if (outp == NULL) {
229 		isp_prt(isp, ISP_LOGWARN, rqo, __func__);
230 		return (-1);
231 	}
232 	switch (etype) {
233 	case RQSTYPE_NOTIFY_ACK:
234 		isp_put_notify_ack_24xx(isp, (na_fcentry_24xx_t *)ap,
235 		    (na_fcentry_24xx_t *)outp);
236 		break;
237 	case RQSTYPE_CTIO7:
238 		isp_put_ctio7(isp, (ct7_entry_t *)ap, (ct7_entry_t *)outp);
239 		break;
240 	case RQSTYPE_ABTS_RSP:
241 		isp_put_abts_rsp(isp, (abts_rsp_t *)ap, (abts_rsp_t *)outp);
242 		break;
243 	default:
244 		isp_prt(isp, ISP_LOGERR, "%s: Unknown type 0x%x", __func__, etype);
245 		return (-1);
246 	}
247 	ISP_TDQE(isp, __func__, isp->isp_reqidx, ap);
248 	ISP_SYNC_REQUEST(isp);
249 	return (0);
250 }
251 
252 /*
253  * Command completion- both for handling cases of no resources or
254  * no blackhole driver, or other cases where we have to, inline,
255  * finish the command sanely, or for normal command completion.
256  *
257  * The 'completion' code value has the scsi status byte in the low 8 bits.
258  * If status is a CHECK CONDITION and bit 8 is nonzero, then bits 12..15 have
259  * the sense key and  bits 16..23 have the ASCQ and bits 24..31 have the ASC
260  * values.
261  *
262  * NB: the key, asc, ascq, cannot be used for parallel SCSI as it doesn't
263  * NB: inline SCSI sense reporting. As such, we lose this information. XXX.
264  *
265  * For both parallel && fibre channel, we use the feature that does
266  * an automatic resource autoreplenish so we don't have then later do
267  * put of an atio to replenish the f/w's resource count.
268  */
269 
270 int
271 isp_endcmd(ispsoftc_t *isp, ...)
272 {
273 	uint32_t code, hdl;
274 	uint8_t sts;
275 	at7_entry_t *aep;
276 	ct7_entry_t _ctio7, *cto = &_ctio7;
277 	va_list ap;
278 	int vpidx, nphdl;
279 
280 	va_start(ap, isp);
281 	aep = va_arg(ap, at7_entry_t *);
282 	nphdl = va_arg(ap, int);
283 	/*
284 	 * Note that vpidx may equal 0xff (unknown) here
285 	 */
286 	vpidx = va_arg(ap, int);
287 	code = va_arg(ap, uint32_t);
288 	hdl = va_arg(ap, uint32_t);
289 	va_end(ap);
290 	isp_prt(isp, ISP_LOGTDEBUG0, "%s: [RX_ID 0x%x] chan %d code %x", __func__, aep->at_rxid, vpidx, code);
291 
292 	sts = code & 0xff;
293 	ISP_MEMZERO(cto, sizeof(*cto));
294 	cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
295 	cto->ct_header.rqs_entry_count = 1;
296 	cto->ct_nphdl = nphdl;
297 	cto->ct_rxid = aep->at_rxid;
298 	cto->ct_iid_lo = (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
299 	cto->ct_iid_hi = aep->at_hdr.s_id[0];
300 	cto->ct_oxid = aep->at_hdr.ox_id;
301 	cto->ct_scsi_status = sts;
302 	cto->ct_vpidx = vpidx;
303 	cto->ct_flags = CT7_NOACK;
304 	if (code & ECMD_TERMINATE) {
305 		cto->ct_flags |= CT7_TERMINATE;
306 	} else if (code & ECMD_SVALID) {
307 		cto->ct_flags |= CT7_FLAG_MODE1 | CT7_SENDSTATUS;
308 		cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8);
309 		cto->ct_senselen = min(16, MAXRESPLEN_24XX);
310 		ISP_MEMZERO(cto->rsp.m1.ct_resp, sizeof (cto->rsp.m1.ct_resp));
311 		cto->rsp.m1.ct_resp[0] = 0xf0;
312 		cto->rsp.m1.ct_resp[2] = (code >> 12) & 0xf;
313 		cto->rsp.m1.ct_resp[7] = 8;
314 		cto->rsp.m1.ct_resp[12] = (code >> 16) & 0xff;
315 		cto->rsp.m1.ct_resp[13] = (code >> 24) & 0xff;
316 	} else if (code & ECMD_RVALID) {
317 		cto->ct_flags |= CT7_FLAG_MODE1 | CT7_SENDSTATUS;
318 		cto->ct_scsi_status |= (FCP_RSPLEN_VALID << 8);
319 		cto->rsp.m1.ct_resplen = 4;
320 		ISP_MEMZERO(cto->rsp.m1.ct_resp, sizeof (cto->rsp.m1.ct_resp));
321 		cto->rsp.m1.ct_resp[0] = (code >> 12) & 0xf;
322 		cto->rsp.m1.ct_resp[1] = (code >> 16) & 0xff;
323 		cto->rsp.m1.ct_resp[2] = (code >> 24) & 0xff;
324 		cto->rsp.m1.ct_resp[3] = 0;
325 	} else {
326 		cto->ct_flags |= CT7_FLAG_MODE1 | CT7_SENDSTATUS;
327 	}
328 	if (aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl != 0) {
329 		cto->ct_resid = aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl;
330 		cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8);
331 	}
332 	cto->ct_syshandle = hdl;
333 	return (isp_target_put_entry(isp, cto));
334 }
335 
336 /*
337  * These are either broadcast events or specifically CTIO fast completion
338  */
339 
340 void
341 isp_target_async(ispsoftc_t *isp, int bus, int event)
342 {
343 	isp_notify_t notify;
344 
345 	ISP_MEMZERO(&notify, sizeof (isp_notify_t));
346 	notify.nt_hba = isp;
347 	notify.nt_wwn = INI_ANY;
348 	notify.nt_nphdl = NIL_HANDLE;
349 	notify.nt_sid = PORT_ANY;
350 	notify.nt_did = PORT_ANY;
351 	notify.nt_tgt = TGT_ANY;
352 	notify.nt_channel = bus;
353 	notify.nt_lun = LUN_ANY;
354 	notify.nt_tagval = TAG_ANY;
355 	notify.nt_tagval |= (((uint64_t)(isp->isp_serno++)) << 32);
356 
357 	switch (event) {
358 	case ASYNC_LOOP_UP:
359 	case ASYNC_PTPMODE:
360 		isp_prt(isp, ISP_LOGTDEBUG0, "%s: LOOP UP", __func__);
361 		notify.nt_ncode = NT_LINK_UP;
362 		isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
363 		break;
364 	case ASYNC_LOOP_DOWN:
365 		isp_prt(isp, ISP_LOGTDEBUG0, "%s: LOOP DOWN", __func__);
366 		notify.nt_ncode = NT_LINK_DOWN;
367 		isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
368 		break;
369 	case ASYNC_LIP_ERROR:
370 	case ASYNC_LIP_NOS_OLS_RECV:
371 	case ASYNC_LIP_OCCURRED:
372 	case ASYNC_LOOP_RESET:
373 		isp_prt(isp, ISP_LOGTDEBUG0, "%s: LIP RESET", __func__);
374 		notify.nt_ncode = NT_LIP_RESET;
375 		isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
376 		break;
377 	case ASYNC_BUS_RESET:
378 	case ASYNC_TIMEOUT_RESET:	/* XXX: where does this come from ? */
379 		isp_prt(isp, ISP_LOGTDEBUG0, "%s: BUS RESET", __func__);
380 		notify.nt_ncode = NT_BUS_RESET;
381 		isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
382 		break;
383 	case ASYNC_DEVICE_RESET:
384 		isp_prt(isp, ISP_LOGTDEBUG0, "%s: DEVICE RESET", __func__);
385 		notify.nt_ncode = NT_TARGET_RESET;
386 		isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
387 		break;
388 	default:
389 		isp_prt(isp, ISP_LOGERR, "%s: unknown event 0x%x", __func__, event);
390 		break;
391 	}
392 }
393 
394 static void
395 isp_got_tmf_24xx(ispsoftc_t *isp, at7_entry_t *aep)
396 {
397 	isp_notify_t notify;
398 	static const char f1[] = "%s from PortID 0x%06x lun %jx seq 0x%08x";
399 	static const char f2[] = "unknown Task Flag 0x%x lun %jx PortID 0x%x tag 0x%08x";
400 	fcportdb_t *lp;
401 	uint16_t chan;
402 	uint32_t sid, did;
403 
404 	ISP_MEMZERO(&notify, sizeof (isp_notify_t));
405 	notify.nt_hba = isp;
406 	notify.nt_wwn = INI_ANY;
407 	notify.nt_lun = CAM_EXTLUN_BYTE_SWIZZLE(be64dec(aep->at_cmnd.fcp_cmnd_lun));
408 	notify.nt_tagval = aep->at_rxid;
409 	notify.nt_tagval |= (((uint64_t)(isp->isp_serno++)) << 32);
410 	notify.nt_lreserved = aep;
411 	sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
412 	did = (aep->at_hdr.d_id[0] << 16) | (aep->at_hdr.d_id[1] << 8) | aep->at_hdr.d_id[2];
413 	if (ISP_CAP_MULTI_ID(isp) && isp->isp_nchan > 1) {
414 		/* Channel has to be derived from D_ID */
415 		isp_find_chan_by_did(isp, did, &chan);
416 		if (chan == ISP_NOCHAN) {
417 			isp_prt(isp, ISP_LOGWARN,
418 			    "%s: D_ID 0x%x not found on any channel",
419 			    __func__, did);
420 			isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN,
421 			    ECMD_TERMINATE, 0);
422 			return;
423 		}
424 	} else {
425 		chan = 0;
426 	}
427 	if (isp_find_pdb_by_portid(isp, chan, sid, &lp))
428 		notify.nt_nphdl = lp->handle;
429 	else
430 		notify.nt_nphdl = NIL_HANDLE;
431 	notify.nt_sid = sid;
432 	notify.nt_did = did;
433 	notify.nt_channel = chan;
434 	if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_QUERY_TASK_SET) {
435 		isp_prt(isp, ISP_LOGINFO, f1, "QUERY TASK SET", sid, notify.nt_lun, aep->at_rxid);
436 		notify.nt_ncode = NT_QUERY_TASK_SET;
437 	} else if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_ABORT_TASK_SET) {
438 		isp_prt(isp, ISP_LOGINFO, f1, "ABORT TASK SET", sid, notify.nt_lun, aep->at_rxid);
439 		notify.nt_ncode = NT_ABORT_TASK_SET;
440 	} else if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_CLEAR_TASK_SET) {
441 		isp_prt(isp, ISP_LOGINFO, f1, "CLEAR TASK SET", sid, notify.nt_lun, aep->at_rxid);
442 		notify.nt_ncode = NT_CLEAR_TASK_SET;
443 	} else if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_QUERY_ASYNC_EVENT) {
444 		isp_prt(isp, ISP_LOGINFO, f1, "QUERY ASYNC EVENT", sid, notify.nt_lun, aep->at_rxid);
445 		notify.nt_ncode = NT_QUERY_ASYNC_EVENT;
446 	} else if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_LUN_RESET) {
447 		isp_prt(isp, ISP_LOGINFO, f1, "LUN RESET", sid, notify.nt_lun, aep->at_rxid);
448 		notify.nt_ncode = NT_LUN_RESET;
449 	} else if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_TGT_RESET) {
450 		isp_prt(isp, ISP_LOGINFO, f1, "TARGET RESET", sid, notify.nt_lun, aep->at_rxid);
451 		notify.nt_ncode = NT_TARGET_RESET;
452 	} else if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_CLEAR_ACA) {
453 		isp_prt(isp, ISP_LOGINFO, f1, "CLEAR ACA", sid, notify.nt_lun, aep->at_rxid);
454 		notify.nt_ncode = NT_CLEAR_ACA;
455 	} else {
456 		isp_prt(isp, ISP_LOGWARN, f2, aep->at_cmnd.fcp_cmnd_task_management, notify.nt_lun, sid, aep->at_rxid);
457 		notify.nt_ncode = NT_UNKNOWN;
458 		isp_endcmd(isp, aep, notify.nt_nphdl, chan, ECMD_RVALID | (0x4 << 12), 0);
459 		return;
460 	}
461 	isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
462 }
463 
464 int
465 isp_notify_ack(ispsoftc_t *isp, void *arg)
466 {
467 	na_fcentry_24xx_t _na, *na = &_na;
468 
469 	/*
470 	 * This is in case a Task Management Function ends up here.
471 	 */
472 	if (((isphdr_t *)arg)->rqs_entry_type == RQSTYPE_ATIO)
473 		return (isp_endcmd(isp, arg, NIL_HANDLE, 0, 0, 0));
474 
475 	in_fcentry_24xx_t *in = arg;
476 
477 	ISP_MEMZERO(na, sizeof(*na));
478 	na->na_header.rqs_entry_type = RQSTYPE_NOTIFY_ACK;
479 	na->na_header.rqs_entry_count = 1;
480 	na->na_nphdl = in->in_nphdl;
481 	na->na_flags = in->in_flags;
482 	na->na_status = in->in_status;
483 	na->na_status_subcode = in->in_status_subcode;
484 	na->na_fwhandle = in->in_fwhandle;
485 	na->na_rxid = in->in_rxid;
486 	na->na_oxid = in->in_oxid;
487 	na->na_vpidx = in->in_vpidx;
488 	if (in->in_status == IN24XX_SRR_RCVD) {
489 		na->na_srr_rxid = in->in_srr_rxid;
490 		na->na_srr_reloff_hi = in->in_srr_reloff_hi;
491 		na->na_srr_reloff_lo = in->in_srr_reloff_lo;
492 		na->na_srr_iu = in->in_srr_iu;
493 		/*
494 		 * Whether we're accepting the SRR or rejecting
495 		 * it is determined by looking at the in_reserved
496 		 * field in the original notify structure.
497 		 */
498 		if (in->in_reserved) {
499 			na->na_srr_flags = 1;
500 			na->na_srr_reject_vunique = 0;
501 			/* Unable to perform this command at this time. */
502 			na->na_srr_reject_code = 9;
503 			/* Unable to supply the requested data. */
504 			na->na_srr_reject_explanation = 0x2a;
505 		}
506 	}
507 	return (isp_target_put_entry(isp, na));
508 }
509 
510 int
511 isp_acknak_abts(ispsoftc_t *isp, void *arg, int errno)
512 {
513 	char storage[QENTRY_LEN];
514 	uint16_t tmpw;
515 	uint8_t tmpb;
516 	abts_t *abts = arg;
517 	abts_rsp_t *rsp = (abts_rsp_t *) storage;
518 
519 	if (abts->abts_header.rqs_entry_type != RQSTYPE_ABTS_RCVD) {
520 		isp_prt(isp, ISP_LOGERR, "%s: called for non-ABTS entry (0x%x)", __func__, abts->abts_header.rqs_entry_type);
521 		return (0);
522 	}
523 
524 	ISP_MEMCPY(rsp, abts, QENTRY_LEN);
525 	rsp->abts_rsp_header.rqs_entry_type = RQSTYPE_ABTS_RSP;
526 
527 	/*
528 	 * Swap destination and source for response.
529 	 */
530 	rsp->abts_rsp_r_ctl = BA_ACC;
531 	tmpw = rsp->abts_rsp_did_lo;
532 	tmpb = rsp->abts_rsp_did_hi;
533 	rsp->abts_rsp_did_lo = rsp->abts_rsp_sid_lo;
534 	rsp->abts_rsp_did_hi = rsp->abts_rsp_sid_hi;
535 	rsp->abts_rsp_sid_lo = tmpw;
536 	rsp->abts_rsp_sid_hi = tmpb;
537 
538 	rsp->abts_rsp_f_ctl_hi ^= 0x80; 	/* invert Exchange Context */
539 	rsp->abts_rsp_f_ctl_hi &= ~0x7f;	/* clear Sequence Initiator and other bits */
540 	rsp->abts_rsp_f_ctl_hi |= 0x10;		/* abort the whole exchange */
541 	rsp->abts_rsp_f_ctl_hi |= 0x8;		/* last data frame of sequence */
542 	rsp->abts_rsp_f_ctl_hi |= 0x1;		/* transfer Sequence Initiative */
543 	rsp->abts_rsp_f_ctl_lo = 0;
544 
545 	if (errno == 0) {
546 		uint16_t rx_id, ox_id;
547 
548 		rx_id = rsp->abts_rsp_rx_id;
549 		ox_id = rsp->abts_rsp_ox_id;
550 		ISP_MEMZERO(&rsp->abts_rsp_payload.ba_acc, sizeof (rsp->abts_rsp_payload.ba_acc));
551                 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);
552                 rsp->abts_rsp_payload.ba_acc.aborted_rx_id = rx_id;
553                 rsp->abts_rsp_payload.ba_acc.aborted_ox_id = ox_id;
554                 rsp->abts_rsp_payload.ba_acc.high_seq_cnt = 0xffff;
555 	} else {
556 		ISP_MEMZERO(&rsp->abts_rsp_payload.ba_rjt, sizeof (rsp->abts_rsp_payload.ba_acc));
557 		switch (errno) {
558 		case ENOMEM:
559 			rsp->abts_rsp_payload.ba_rjt.reason = 5;	/* Logical Unit Busy */
560 			break;
561 		default:
562 			rsp->abts_rsp_payload.ba_rjt.reason = 9;	/* Unable to perform command request */
563 			break;
564 		}
565 	}
566 	return (isp_target_put_entry(isp, rsp));
567 }
568 
569 static void
570 isp_handle_abts(ispsoftc_t *isp, abts_t *abts)
571 {
572 	isp_notify_t notify, *nt = &notify;
573 	fcportdb_t *lp;
574 	uint16_t chan;
575 	uint32_t sid, did;
576 
577 	did = (abts->abts_did_hi << 16) | abts->abts_did_lo;
578 	sid = (abts->abts_sid_hi << 16) | abts->abts_sid_lo;
579 	ISP_MEMZERO(nt, sizeof (isp_notify_t));
580 
581 	nt->nt_hba = isp;
582 	nt->nt_did = did;
583 	nt->nt_nphdl = abts->abts_nphdl;
584 	nt->nt_sid = sid;
585 	if (ISP_CAP_MULTI_ID(isp) && isp->isp_nchan > 1) {
586 		/* Channel has to be derived from D_ID */
587 		isp_find_chan_by_did(isp, did, &chan);
588 		if (chan == ISP_NOCHAN) {
589 			isp_prt(isp, ISP_LOGWARN,
590 			    "%s: D_ID 0x%x not found on any channel",
591 			    __func__, did);
592 			isp_acknak_abts(isp, abts, ENXIO);
593 			return;
594 		}
595 	} else
596 		chan = 0;
597 	nt->nt_tgt = FCPARAM(isp, chan)->isp_wwpn;
598 	if (isp_find_pdb_by_handle(isp, chan, abts->abts_nphdl, &lp))
599 		nt->nt_wwn = lp->port_wwn;
600 	else
601 		nt->nt_wwn = INI_ANY;
602 	nt->nt_lun = LUN_ANY;
603 	nt->nt_need_ack = 1;
604 	nt->nt_tagval = abts->abts_rxid_task;
605 	nt->nt_tagval |= (((uint64_t) abts->abts_rxid_abts) << 32);
606 	isp_prt(isp, ISP_LOGTINFO, "[0x%x] ABTS from N-Port handle 0x%x"
607 	    " Port 0x%06x for task 0x%x (rx_id 0x%04x ox_id 0x%04x)",
608 	    abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rxid_task,
609 	    abts->abts_rx_id, abts->abts_ox_id);
610 	nt->nt_channel = chan;
611 	nt->nt_ncode = NT_ABORT_TASK;
612 	nt->nt_lreserved = abts;
613 	isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
614 }
615 
616 static void
617 isp_handle_ctio7(ispsoftc_t *isp, ct7_entry_t *ct)
618 {
619 	void *xs;
620 	int pl = ISP_LOGTDEBUG2;
621 	char *fmsg = NULL;
622 
623 	if (ct->ct_syshandle) {
624 		xs = isp_find_xs(isp, ct->ct_syshandle);
625 		if (xs == NULL) {
626 			pl = ISP_LOGALL;
627 		}
628 	} else {
629 		xs = NULL;
630 	}
631 
632 	switch (ct->ct_nphdl) {
633 	case CT7_BUS_ERROR:
634 		isp_prt(isp, ISP_LOGERR, "PCI DMA Bus Error");
635 		/* FALL Through */
636 	case CT7_DATA_OVER:
637 	case CT7_DATA_UNDER:
638 	case CT7_OK:
639 		/*
640 		 * There are generally 2 possibilities as to why we'd get
641 		 * this condition:
642 		 * 	We sent or received data.
643 		 * 	We sent status & command complete.
644 		 */
645 
646 		break;
647 
648 	case CT7_RESET:
649 		if (fmsg == NULL) {
650 			fmsg = "LIP Reset";
651 		}
652 		/*FALLTHROUGH*/
653 	case CT7_ABORTED:
654 		/*
655 		 * When an Abort message is received the firmware goes to
656 		 * Bus Free and returns all outstanding CTIOs with the status
657 		 * set, then sends us an Immediate Notify entry.
658 		 */
659 		if (fmsg == NULL) {
660 			fmsg = "ABORT";
661 		}
662 		isp_prt(isp, ISP_LOGTDEBUG0, "CTIO7 destroyed by %s: RX_ID=0x%x", fmsg, ct->ct_rxid);
663 		break;
664 
665 	case CT7_TIMEOUT:
666 		if (fmsg == NULL) {
667 			fmsg = "command";
668 		}
669 		isp_prt(isp, ISP_LOGWARN, "Firmware timed out on %s", fmsg);
670 		break;
671 
672 	case CT7_ERR:
673 		fmsg = "Completed with Error";
674 		/*FALLTHROUGH*/
675 	case CT7_LOGOUT:
676 		if (fmsg == NULL) {
677 			fmsg = "Port Logout";
678 		}
679 		/*FALLTHROUGH*/
680 	case CT7_PORTUNAVAIL:
681 		if (fmsg == NULL) {
682 			fmsg = "Port not available";
683 		}
684 		/*FALLTHROUGH*/
685 	case CT7_PORTCHANGED:
686 		if (fmsg == NULL) {
687 			fmsg = "Port Changed";
688 		}
689 		isp_prt(isp, ISP_LOGWARN, "CTIO returned by f/w- %s", fmsg);
690 		break;
691 
692 	case CT7_INVRXID:
693 		/*
694 		 * CTIO rejected by the firmware because an invalid RX_ID.
695 		 * Just print a message.
696 		 */
697 		isp_prt(isp, ISP_LOGWARN, "CTIO7 completed with Invalid RX_ID 0x%x", ct->ct_rxid);
698 		break;
699 
700 	case CT7_REASSY_ERR:
701 		isp_prt(isp, ISP_LOGWARN, "reassembly error");
702 		break;
703 
704 	case CT7_SRR:
705 		isp_prt(isp, ISP_LOGTDEBUG0, "SRR received");
706 		break;
707 
708 	default:
709 		isp_prt(isp, ISP_LOGERR, "Unknown CTIO7 status 0x%x", ct->ct_nphdl);
710 		break;
711 	}
712 
713 	if (xs == NULL) {
714 		/*
715 		 * There may be more than one CTIO for a data transfer,
716 		 * or this may be a status CTIO we're not monitoring.
717 		 *
718 		 * The assumption is that they'll all be returned in the
719 		 * order we got them.
720 		 */
721 		if (ct->ct_syshandle == 0) {
722 			if (ct->ct_flags & CT7_TERMINATE) {
723 				isp_prt(isp, ISP_LOGINFO, "termination of [RX_ID 0x%x] complete", ct->ct_rxid);
724 			} else if ((ct->ct_flags & CT7_SENDSTATUS) == 0) {
725 				isp_prt(isp, pl, "intermediate CTIO completed ok");
726 			} else {
727 				isp_prt(isp, pl, "unmonitored CTIO completed ok");
728 			}
729 		} else {
730 			isp_prt(isp, pl, "NO xs for CTIO (handle 0x%x) status 0x%x", ct->ct_syshandle, ct->ct_nphdl);
731 		}
732 	} else {
733 		ISP_DMAFREE(isp, xs);
734 		if (ct->ct_flags & CT7_SENDSTATUS) {
735 			/*
736 			 * Sent status and command complete.
737 			 *
738 			 * We're now really done with this command, so we
739 			 * punt to the platform dependent layers because
740 			 * only there can we do the appropriate command
741 			 * complete thread synchronization.
742 			 */
743 			isp_prt(isp, pl, "status CTIO complete");
744 		} else {
745 			/*
746 			 * Final CTIO completed. Release DMA resources and
747 			 * notify platform dependent layers.
748 			 */
749 			isp_prt(isp, pl, "data CTIO complete");
750 		}
751 		isp_async(isp, ISPASYNC_TARGET_ACTION, ct);
752 		/*
753 		 * The platform layer will destroy the handle if appropriate.
754 		 */
755 	}
756 }
757 
758 static void
759 isp_handle_notify_24xx(ispsoftc_t *isp, in_fcentry_24xx_t *inot)
760 {
761 	uint8_t chan;
762 	uint16_t nphdl, prli_options = 0;
763 	uint32_t portid;
764 	fcportdb_t *lp;
765 	char *msg = NULL;
766 	uint8_t *ptr = (uint8_t *)inot;
767 	uint64_t wwpn = INI_NONE, wwnn = INI_NONE;
768 	isp_notify_t notify;
769 	char buf[16];
770 
771 	nphdl = inot->in_nphdl;
772 	if (nphdl != NIL_HANDLE) {
773 		portid = inot->in_portid_hi << 16 | inot->in_portid_lo;
774 	} else {
775 		portid = PORT_ANY;
776 	}
777 
778 	chan = ISP_GET_VPIDX(isp, inot->in_vpidx);
779 	if (chan >= isp->isp_nchan &&
780 	    inot->in_status != IN24XX_LIP_RESET &&
781 	    inot->in_status != IN24XX_LINK_RESET &&
782 	    inot->in_status != IN24XX_LINK_FAILED) {
783 		isp_prt(isp, ISP_LOGWARN, "%s: Received INOT with status %x on VP %x",
784 		    __func__, inot->in_status, chan);
785 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
786 		return;
787 	}
788 
789 	switch (inot->in_status) {
790 	case IN24XX_ELS_RCVD:
791 	{
792 		/*
793 		 * Note that we're just getting notification that an ELS was
794 		 * received (possibly with some associated information sent
795 		 * upstream).  This is *not* the same as being given the ELS
796 		 * frame to accept or reject.
797 		 */
798 		switch (inot->in_status_subcode) {
799 		case LOGO:
800 			msg = "LOGO";
801 			wwpn = be64dec(&ptr[IN24XX_PLOGI_WWPN_OFF]);
802 			isp_del_wwn_entry(isp, chan, wwpn, nphdl, portid);
803 			break;
804 		case PRLO:
805 			msg = "PRLO";
806 			break;
807 		case PLOGI:
808 			msg = "PLOGI";
809 			wwnn = be64dec(&ptr[IN24XX_PLOGI_WWNN_OFF]);
810 			wwpn = be64dec(&ptr[IN24XX_PLOGI_WWPN_OFF]);
811 			isp_add_wwn_entry(isp, chan, wwpn, wwnn,
812 			    nphdl, portid, prli_options);
813 			break;
814 		case PRLI:
815 			msg = "PRLI";
816 			prli_options = inot->in_prli_options;
817 			if (inot->in_flags & IN24XX_FLAG_PN_NN_VALID)
818 				wwnn = be64dec(&ptr[IN24XX_PRLI_WWNN_OFF]);
819 			wwpn = be64dec(&ptr[IN24XX_PRLI_WWPN_OFF]);
820 			isp_add_wwn_entry(isp, chan, wwpn, wwnn,
821 			    nphdl, portid, prli_options);
822 			break;
823 		case PDISC:
824 			msg = "PDISC";
825 			break;
826 		case ADISC:
827 			msg = "ADISC";
828 			break;
829 		default:
830 			ISP_SNPRINTF(buf, sizeof (buf), "ELS 0x%x",
831 			    inot->in_status_subcode);
832 			msg = buf;
833 			break;
834 		}
835 		if (inot->in_flags & IN24XX_FLAG_PUREX_IOCB) {
836 			isp_prt(isp, ISP_LOGERR, "%s Chan %d ELS N-port handle %x"
837 			    " PortID 0x%06x marked as needing a PUREX response",
838 			    msg, chan, nphdl, portid);
839 			break;
840 		}
841 		isp_prt(isp, ISP_LOGTDEBUG0, "%s Chan %d ELS N-port handle %x"
842 		    " PortID 0x%06x RX_ID 0x%x OX_ID 0x%x", msg, chan, nphdl,
843 		    portid, inot->in_rxid, inot->in_oxid);
844 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
845 		break;
846 	}
847 
848 	case IN24XX_PORT_LOGOUT:
849 		msg = "PORT LOGOUT";
850 		if (isp_find_pdb_by_handle(isp, chan, nphdl, &lp))
851 			isp_del_wwn_entry(isp, chan, lp->port_wwn, nphdl, lp->portid);
852 		/* FALLTHROUGH */
853 	case IN24XX_PORT_CHANGED:
854 		if (msg == NULL)
855 			msg = "PORT CHANGED";
856 		/* FALLTHROUGH */
857 	case IN24XX_LIP_RESET:
858 		if (msg == NULL)
859 			msg = "LIP RESET";
860 		isp_prt(isp, ISP_LOGINFO, "Chan %d %s (sub-status 0x%x) for "
861 		    "N-port handle 0x%x",
862 		    chan, msg, inot->in_status_subcode, nphdl);
863 
864 		/*
865 		 * All subcodes here are irrelevant. What is relevant
866 		 * is that we need to terminate all active commands from
867 		 * this initiator (known by N-port handle).
868 		 */
869 		/* XXX IMPLEMENT XXX */
870 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
871 		break;
872 
873 	case IN24XX_SRR_RCVD:
874 #ifdef	ISP_TARGET_MODE
875 		ISP_MEMZERO(&notify, sizeof (isp_notify_t));
876 		notify.nt_hba = isp;
877 		notify.nt_wwn = INI_ANY;
878 		notify.nt_tgt = FCPARAM(isp, chan)->isp_wwpn;
879 		notify.nt_nphdl = nphdl;
880 		notify.nt_sid = portid;
881 		notify.nt_did = PORT_ANY;
882 		notify.nt_lun = LUN_ANY;
883 		notify.nt_tagval = inot->in_rxid;
884 		notify.nt_tagval |= ((uint64_t)inot->in_srr_rxid << 32);
885 		notify.nt_need_ack = 1;
886 		notify.nt_channel = chan;
887 		notify.nt_lreserved = inot;
888 		notify.nt_ncode = NT_SRR;
889 		isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
890 		break;
891 #else
892 		if (msg == NULL)
893 			msg = "SRR RCVD";
894 		/* FALLTHROUGH */
895 #endif
896 	case IN24XX_LINK_RESET:
897 		if (msg == NULL)
898 			msg = "LINK RESET";
899 	case IN24XX_LINK_FAILED:
900 		if (msg == NULL)
901 			msg = "LINK FAILED";
902 	default:
903 		isp_prt(isp, ISP_LOGWARN, "Chan %d %s", chan, msg);
904 		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
905 		break;
906 	}
907 }
908 #endif
909