isp_target.c revision 299691
1139749Simp/*-
2196008Smjacob *  Copyright (c) 1997-2009 by Matthew Jacob
3167403Smjacob *  All rights reserved.
4196008Smjacob *
5167403Smjacob *  Redistribution and use in source and binary forms, with or without
6167403Smjacob *  modification, are permitted provided that the following conditions
7167403Smjacob *  are met:
8196008Smjacob *
9167403Smjacob *  1. Redistributions of source code must retain the above copyright
10167403Smjacob *     notice, this list of conditions and the following disclaimer.
11167403Smjacob *  2. Redistributions in binary form must reproduce the above copyright
12167403Smjacob *     notice, this list of conditions and the following disclaimer in the
13167403Smjacob *     documentation and/or other materials provided with the distribution.
14196008Smjacob *
15167403Smjacob *  THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16167403Smjacob *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17167403Smjacob *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18167403Smjacob *  ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19167403Smjacob *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20167403Smjacob *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21167403Smjacob *  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22167403Smjacob *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23167403Smjacob *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24167403Smjacob *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25167403Smjacob *  SUCH DAMAGE.
26196008Smjacob *
27167403Smjacob */
28167403Smjacob/*
2955373Smjacob * Machine and OS Independent Target Mode Code for the Qlogic SCSI/FC adapters.
3055373Smjacob */
3155373Smjacob/*
3290224Smjacob * Bug fixes gratefully acknowledged from:
3390224Smjacob *	Oded Kedem <oded@kashya.com>
3490224Smjacob */
3590224Smjacob/*
3655373Smjacob * Include header file appropriate for platform we're building on.
3755373Smjacob */
3855373Smjacob
3955373Smjacob#ifdef	__NetBSD__
4055373Smjacob#include <dev/ic/isp_netbsd.h>
4155373Smjacob#endif
4255373Smjacob#ifdef	__FreeBSD__
43160410Smjacob#include <sys/cdefs.h>
44160410Smjacob__FBSDID("$FreeBSD: head/sys/dev/isp/isp_target.c 299691 2016-05-13 18:55:03Z mav $");
4555373Smjacob#include <dev/isp/isp_freebsd.h>
4655373Smjacob#endif
4755373Smjacob#ifdef	__OpenBSD__
4855373Smjacob#include <dev/ic/isp_openbsd.h>
4955373Smjacob#endif
5055373Smjacob#ifdef	__linux__
5155373Smjacob#include "isp_linux.h"
5255373Smjacob#endif
5355373Smjacob
5455373Smjacob#ifdef	ISP_TARGET_MODE
55291013Smavstatic const char atiocope[] = "ATIO returned for LUN %x because it was in the middle of Bus Device Reset on bus %d";
56291013Smavstatic const char atior[] = "ATIO returned for LUN %x from handle 0x%x because a Bus Reset occurred on bus %d";
57196008Smjacobstatic const char rqo[] = "%s: Request Queue Overflow";
5855373Smjacob
59157943Smjacobstatic void isp_got_msg_fc(ispsoftc_t *, in_fcentry_t *);
60163899Smjacobstatic void isp_got_tmf_24xx(ispsoftc_t *, at7_entry_t *);
61157943Smjacobstatic void isp_handle_atio2(ispsoftc_t *, at2_entry_t *);
62157943Smjacobstatic void isp_handle_ctio2(ispsoftc_t *, ct2_entry_t *);
63163899Smjacobstatic void isp_handle_ctio7(ispsoftc_t *, ct7_entry_t *);
64196008Smjacobstatic void isp_handle_24xx_inotify(ispsoftc_t *, in_fcentry_24xx_t *);
6555373Smjacob
6655373Smjacob/*
6755373Smjacob * The Qlogic driver gets an interrupt to look at response queue entries.
6855373Smjacob * Some of these are status completions for initiatior mode commands, but
6955373Smjacob * if target mode is enabled, we get a whole wad of response queue entries
7055373Smjacob * to be handled here.
7155373Smjacob *
7255373Smjacob * Basically the split into 3 main groups: Lun Enable/Modification responses,
7355373Smjacob * SCSI Command processing, and Immediate Notification events.
7455373Smjacob *
7555373Smjacob * You start by writing a request queue entry to enable target mode (and
7655373Smjacob * establish some resource limitations which you can modify later).
7755373Smjacob * The f/w responds with a LUN ENABLE or LUN MODIFY response with
7855373Smjacob * the status of this action. If the enable was successful, you can expect...
7955373Smjacob *
8055373Smjacob * Response queue entries with SCSI commands encapsulate show up in an ATIO
8155373Smjacob * (Accept Target IO) type- sometimes with enough info to stop the command at
8255373Smjacob * this level. Ultimately the driver has to feed back to the f/w's request
8355373Smjacob * queue a sequence of CTIOs (continue target I/O) that describe data to
8455373Smjacob * be moved and/or status to be sent) and finally finishing with sending
8555373Smjacob * to the f/w's response queue an ATIO which then completes the handshake
8655373Smjacob * with the f/w for that command. There's a lot of variations on this theme,
8755373Smjacob * including flags you can set in the CTIO for the Qlogic 2X00 fibre channel
8855373Smjacob * cards that 'auto-replenish' the f/w's ATIO count, but this is the basic
8955373Smjacob * gist of it.
9055373Smjacob *
9155373Smjacob * The third group that can show up in the response queue are Immediate
9255373Smjacob * Notification events. These include things like notifications of SCSI bus
9355373Smjacob * resets, or Bus Device Reset messages or other messages received. This
9472082Sasmodai * a classic oddbins area. It can get  a little weird because you then turn
9555373Smjacob * around and acknowledge the Immediate Notify by writing an entry onto the
9655373Smjacob * request queue and then the f/w turns around and gives you an acknowledgement
9755373Smjacob * to *your* acknowledgement on the response queue (the idea being to let
9855373Smjacob * the f/w tell you when the event is *really* over I guess).
9955373Smjacob *
10055373Smjacob */
10155373Smjacob
10255373Smjacob
10355373Smjacob/*
10455373Smjacob * A new response queue entry has arrived. The interrupt service code
10555373Smjacob * has already swizzled it into the platform dependent from canonical form.
10655373Smjacob *
10755373Smjacob * Because of the way this driver is designed, unfortunately most of the
10855373Smjacob * actual synchronization work has to be done in the platform specific
10955373Smjacob * code- we have no synchroniation primitives in the common code.
11055373Smjacob */
11155373Smjacob
11255373Smjacobint
113163899Smjacobisp_target_notify(ispsoftc_t *isp, void *vptr, uint32_t *optrp)
11455373Smjacob{
115163899Smjacob	uint16_t status;
116163899Smjacob	uint32_t seqid;
11755373Smjacob	union {
11855373Smjacob		at2_entry_t	*at2iop;
119154704Smjacob		at2e_entry_t	*at2eiop;
120163899Smjacob		at7_entry_t	*at7iop;
12155373Smjacob		ct2_entry_t	*ct2iop;
122154704Smjacob		ct2e_entry_t	*ct2eiop;
123163899Smjacob		ct7_entry_t	*ct7iop;
12455373Smjacob		lun_entry_t	*lunenp;
12555373Smjacob		in_fcentry_t	*inot_fcp;
126154704Smjacob		in_fcentry_e_t	*inote_fcp;
127163899Smjacob		in_fcentry_24xx_t *inot_24xx;
12855373Smjacob		na_fcentry_t	*nack_fcp;
129154704Smjacob		na_fcentry_e_t	*nacke_fcp;
130163899Smjacob		na_fcentry_24xx_t *nack_24xx;
13155373Smjacob		isphdr_t	*hp;
132163899Smjacob		abts_t		*abts;
133163899Smjacob		abts_rsp_t	*abts_rsp;
134163899Smjacob		els_t		*els;
13555373Smjacob		void *		*vp;
13655373Smjacob#define	at2iop		unp.at2iop
137154704Smjacob#define	at2eiop		unp.at2eiop
138163899Smjacob#define	at7iop		unp.at7iop
13955373Smjacob#define	ct2iop		unp.ct2iop
140154704Smjacob#define	ct2eiop		unp.ct2eiop
141163899Smjacob#define	ct7iop		unp.ct7iop
14255373Smjacob#define	lunenp		unp.lunenp
14355373Smjacob#define	inot_fcp	unp.inot_fcp
144154704Smjacob#define	inote_fcp	unp.inote_fcp
145163899Smjacob#define	inot_24xx	unp.inot_24xx
14655373Smjacob#define	nack_fcp	unp.nack_fcp
147154704Smjacob#define	nacke_fcp	unp.nacke_fcp
148163899Smjacob#define	nack_24xx	unp.nack_24xx
149163899Smjacob#define	abts		unp.abts
150163899Smjacob#define	abts_rsp	unp.abts_rsp
151163899Smjacob#define els		unp.els
15255373Smjacob#define	hdrp		unp.hp
15355373Smjacob	} unp;
154155704Smjacob	uint8_t local[QENTRY_LEN];
155196008Smjacob	uint16_t iid;
156291188Smav	int bus, type, len, level, rval = 1;
157196008Smjacob	isp_notify_t notify;
15855373Smjacob
15987635Smjacob	type = isp_get_response_type(isp, (isphdr_t *)vptr);
16055373Smjacob	unp.vp = vptr;
16155373Smjacob
16255373Smjacob	ISP_TDQE(isp, "isp_target_notify", (int) *optrp, vptr);
16355373Smjacob
164196008Smjacob	switch (type) {
16555373Smjacob	case RQSTYPE_ATIO:
166291188Smav		isp_get_atio7(isp, at7iop, (at7_entry_t *) local);
167291188Smav		at7iop = (at7_entry_t *) local;
168291188Smav		/*
169291188Smav		 * Check for and do something with commands whose
170291188Smav		 * IULEN extends past a single queue entry.
171291188Smav		 */
172299691Smav		len = at7iop->at_ta_len & 0x0fff;
173291188Smav		if (len > (QENTRY_LEN - 8)) {
174291188Smav			len -= (QENTRY_LEN - 8);
175291188Smav			isp_prt(isp, ISP_LOGINFO, "long IU length (%d) ignored", len);
176291188Smav			while (len > 0) {
177291188Smav				*optrp =  ISP_NXT_QENTRY(*optrp, RESULT_QUEUE_LEN(isp));
178291188Smav				len -= QENTRY_LEN;
179163899Smjacob			}
180163899Smjacob		}
181291188Smav		/*
182291188Smav		 * Check for a task management function
183291188Smav		 */
184291188Smav		if (at7iop->at_cmnd.fcp_cmnd_task_management) {
185291188Smav			isp_got_tmf_24xx(isp, at7iop);
186291188Smav			break;
187291188Smav		}
188291188Smav		/*
189291188Smav		 * Just go straight to outer layer for this one.
190291188Smav		 */
191291188Smav		isp_async(isp, ISPASYNC_TARGET_ACTION, local);
19255373Smjacob		break;
193163899Smjacob
19455373Smjacob	case RQSTYPE_ATIO2:
195196008Smjacob		if (ISP_CAP_2KLOGIN(isp)) {
196154704Smjacob			isp_get_atio2e(isp, at2eiop, (at2e_entry_t *) local);
197160251Smjacob		} else {
198154704Smjacob			isp_get_atio2(isp, at2iop, (at2_entry_t *) local);
199163899Smjacob		}
20087635Smjacob		isp_handle_atio2(isp, (at2_entry_t *) local);
20155373Smjacob		break;
202163899Smjacob
203125187Smjacob	case RQSTYPE_CTIO3:
20455373Smjacob	case RQSTYPE_CTIO2:
205196008Smjacob		if (ISP_CAP_2KLOGIN(isp)) {
206154704Smjacob			isp_get_ctio2e(isp, ct2eiop, (ct2e_entry_t *) local);
207160251Smjacob		} else {
208154704Smjacob			isp_get_ctio2(isp, ct2iop, (ct2_entry_t *) local);
209163899Smjacob		}
21087635Smjacob		isp_handle_ctio2(isp, (ct2_entry_t *) local);
21155373Smjacob		break;
212163899Smjacob
213163899Smjacob	case RQSTYPE_CTIO7:
214163899Smjacob		isp_get_ctio7(isp, ct7iop, (ct7_entry_t *) local);
215163899Smjacob		isp_handle_ctio7(isp, (ct7_entry_t *) local);
216163899Smjacob		break;
217163899Smjacob
21855373Smjacob	case RQSTYPE_ENABLE_LUN:
21955373Smjacob	case RQSTYPE_MODIFY_LUN:
22087635Smjacob		isp_get_enable_lun(isp, lunenp, (lun_entry_t *) local);
221196008Smjacob		isp_async(isp, ISPASYNC_TARGET_ACTION, local);
22255373Smjacob		break;
22355373Smjacob
22455373Smjacob	case RQSTYPE_NOTIFY:
22557216Smjacob		bus = 0;
226163899Smjacob		if (IS_24XX(isp)) {
227196008Smjacob			isp_get_notify_24xx(isp, inot_24xx, (in_fcentry_24xx_t *)local);
228163899Smjacob			inot_24xx = (in_fcentry_24xx_t *) local;
229196008Smjacob			isp_handle_24xx_inotify(isp, inot_24xx);
230163899Smjacob			break;
231291188Smav		} else {
232196008Smjacob			if (ISP_CAP_2KLOGIN(isp)) {
233196008Smjacob				in_fcentry_e_t *ecp = (in_fcentry_e_t *)local;
234196008Smjacob				isp_get_notify_fc_e(isp, inote_fcp, ecp);
235196008Smjacob				iid = ecp->in_iid;
236196008Smjacob				status = ecp->in_status;
237196008Smjacob				seqid = ecp->in_seqid;
238160978Smjacob			} else {
239196008Smjacob				in_fcentry_t *fcp = (in_fcentry_t *)local;
240196008Smjacob				isp_get_notify_fc(isp, inot_fcp, fcp);
241196008Smjacob				iid = fcp->in_iid;
242196008Smjacob				status = fcp->in_status;
243196008Smjacob				seqid = fcp->in_seqid;
244160978Smjacob			}
24555373Smjacob		}
246160978Smjacob
247196008Smjacob		isp_prt(isp, ISP_LOGTDEBUG0, "Immediate Notify On Bus %d, status=0x%x seqid=0x%x", bus, status, seqid);
24883027Smjacob
24955373Smjacob		switch (status) {
25055373Smjacob		case IN_MSG_RECEIVED:
25155373Smjacob		case IN_IDE_RECEIVED:
252291188Smav			isp_got_msg_fc(isp, (in_fcentry_t *)local);
25355373Smjacob			break;
25455373Smjacob		case IN_RSRC_UNAVAIL:
255163899Smjacob			isp_prt(isp, ISP_LOGINFO, "Firmware out of ATIOs");
256238869Smjacob			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, local);
25755373Smjacob			break;
258196008Smjacob
259154704Smjacob		case IN_RESET:
260196008Smjacob			ISP_MEMZERO(&notify, sizeof (isp_notify_t));
261163899Smjacob			notify.nt_hba = isp;
262196008Smjacob			notify.nt_wwn = INI_ANY;
263196008Smjacob			notify.nt_tgt = TGT_ANY;
264196008Smjacob			notify.nt_nphdl = iid;
265196008Smjacob			notify.nt_sid = PORT_ANY;
266196008Smjacob			notify.nt_did = PORT_ANY;
267163899Smjacob			notify.nt_lun = LUN_ANY;
268163899Smjacob			notify.nt_tagval = TAG_ANY;
269196008Smjacob			notify.nt_tagval |= (((uint64_t)(isp->isp_serno++)) << 32);
270163899Smjacob			notify.nt_ncode = NT_BUS_RESET;
271163899Smjacob			notify.nt_need_ack = 1;
272196008Smjacob			notify.nt_lreserved = local;
273196008Smjacob			isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
274154704Smjacob			break;
275196008Smjacob
27698284Smjacob		case IN_PORT_LOGOUT:
277196008Smjacob			ISP_MEMZERO(&notify, sizeof (isp_notify_t));
278196008Smjacob			notify.nt_hba = isp;
279196008Smjacob			notify.nt_wwn = INI_ANY;
280196008Smjacob			notify.nt_nphdl = iid;
281196008Smjacob			notify.nt_sid = PORT_ANY;
282196008Smjacob			notify.nt_did = PORT_ANY;
283196008Smjacob			notify.nt_ncode = NT_LOGOUT;
284196008Smjacob			notify.nt_need_ack = 1;
285196008Smjacob			notify.nt_lreserved = local;
286196008Smjacob			isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
287196008Smjacob			break;
288196008Smjacob
28955373Smjacob		case IN_ABORT_TASK:
290196008Smjacob			ISP_MEMZERO(&notify, sizeof (isp_notify_t));
291196008Smjacob			notify.nt_hba = isp;
292196008Smjacob			notify.nt_wwn = INI_ANY;
293196008Smjacob			notify.nt_nphdl = iid;
294196008Smjacob			notify.nt_sid = PORT_ANY;
295196008Smjacob			notify.nt_did = PORT_ANY;
296196008Smjacob			notify.nt_ncode = NT_ABORT_TASK;
297196008Smjacob			notify.nt_need_ack = 1;
298196008Smjacob			notify.nt_lreserved = local;
299196008Smjacob			isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
300196008Smjacob			break;
301196008Smjacob
30255373Smjacob		case IN_GLOBAL_LOGO:
303196008Smjacob			isp_prt(isp, ISP_LOGTINFO, "%s: all ports logged out", __func__);
304196008Smjacob			ISP_MEMZERO(&notify, sizeof (isp_notify_t));
305196008Smjacob			notify.nt_hba = isp;
306196008Smjacob			notify.nt_wwn = INI_ANY;
307196008Smjacob			notify.nt_nphdl = NIL_HANDLE;
308196008Smjacob			notify.nt_sid = PORT_ANY;
309196008Smjacob			notify.nt_did = PORT_ANY;
310196008Smjacob			notify.nt_ncode = NT_GLOBAL_LOGOUT;
311238869Smjacob			notify.nt_need_ack = 1;
312238869Smjacob			notify.nt_lreserved = local;
313196008Smjacob			isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
31455373Smjacob			break;
315196008Smjacob
316196008Smjacob		case IN_PORT_CHANGED:
317196008Smjacob			isp_prt(isp, ISP_LOGTINFO, "%s: port changed", __func__);
318238869Smjacob			ISP_MEMZERO(&notify, sizeof (isp_notify_t));
319238869Smjacob			notify.nt_hba = isp;
320238869Smjacob			notify.nt_wwn = INI_ANY;
321238869Smjacob			notify.nt_nphdl = NIL_HANDLE;
322238869Smjacob			notify.nt_sid = PORT_ANY;
323238869Smjacob			notify.nt_did = PORT_ANY;
324238869Smjacob			notify.nt_ncode = NT_CHANGED;
325238869Smjacob			notify.nt_need_ack = 1;
326238869Smjacob			notify.nt_lreserved = local;
327238869Smjacob			isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
328196008Smjacob			break;
329196008Smjacob
33055373Smjacob		default:
331196008Smjacob			ISP_SNPRINTF(local, sizeof local, "%s: unknown status to RQSTYPE_NOTIFY (0x%x)", __func__, status);
332196008Smjacob			isp_print_bytes(isp, local, QENTRY_LEN, vptr);
333238869Smjacob			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, local);
33455373Smjacob			break;
33555373Smjacob		}
33655373Smjacob		break;
33755373Smjacob
33855373Smjacob	case RQSTYPE_NOTIFY_ACK:
33955373Smjacob		/*
34055373Smjacob		 * The ISP is acknowledging our acknowledgement of an
34155373Smjacob		 * Immediate Notify entry for some asynchronous event.
34255373Smjacob		 */
343163899Smjacob		if (IS_24XX(isp)) {
344196008Smjacob			isp_get_notify_ack_24xx(isp, nack_24xx, (na_fcentry_24xx_t *) local);
345163899Smjacob			nack_24xx = (na_fcentry_24xx_t *) local;
346163899Smjacob			if (nack_24xx->na_status != NA_OK) {
347163899Smjacob				level = ISP_LOGINFO;
348163899Smjacob			} else {
349163899Smjacob				level = ISP_LOGTDEBUG1;
350163899Smjacob			}
351196008Smjacob			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);
352291188Smav		} else {
353196008Smjacob			if (ISP_CAP_2KLOGIN(isp)) {
354196008Smjacob				isp_get_notify_ack_fc_e(isp, nacke_fcp, (na_fcentry_e_t *)local);
355160251Smjacob			} else {
356196008Smjacob				isp_get_notify_ack_fc(isp, nack_fcp, (na_fcentry_t *)local);
357163899Smjacob			}
35887635Smjacob			nack_fcp = (na_fcentry_t *)local;
359163899Smjacob			if (nack_fcp->na_status != NA_OK) {
360163899Smjacob				level = ISP_LOGINFO;
361163899Smjacob			} else {
362163899Smjacob				level = ISP_LOGTDEBUG1;
363163899Smjacob			}
364196008Smjacob			isp_prt(isp, level, "Notify Ack Status=0x%x seqid 0x%x", nack_fcp->na_status, nack_fcp->na_seqid);
36555373Smjacob		}
36655373Smjacob		break;
367163899Smjacob
368163899Smjacob	case RQSTYPE_ABTS_RCVD:
369163899Smjacob		isp_get_abts(isp, abts, (abts_t *)local);
370196008Smjacob		isp_async(isp, ISPASYNC_TARGET_ACTION, &local);
371163899Smjacob		break;
372163899Smjacob	case RQSTYPE_ABTS_RSP:
373163899Smjacob		isp_get_abts_rsp(isp, abts_rsp, (abts_rsp_t *)local);
374163899Smjacob		abts_rsp = (abts_rsp_t *) local;
375163899Smjacob		if (abts_rsp->abts_rsp_status) {
376163899Smjacob			level = ISP_LOGINFO;
377163899Smjacob		} else {
378163899Smjacob			level = ISP_LOGTDEBUG0;
379163899Smjacob		}
380196008Smjacob		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,
381196008Smjacob		    abts_rsp->abts_rsp_payload.rsp.subcode1, abts_rsp->abts_rsp_payload.rsp.subcode2);
382163899Smjacob		break;
38355373Smjacob	default:
384196008Smjacob		isp_prt(isp, ISP_LOGERR, "%s: unknown entry type 0x%x", __func__, type);
38598284Smjacob		rval = 0;
38655373Smjacob		break;
38755373Smjacob	}
38855373Smjacob#undef	atiop
38955373Smjacob#undef	at2iop
390154704Smjacob#undef	at2eiop
391163899Smjacob#undef	at7iop
39255373Smjacob#undef	ctiop
39355373Smjacob#undef	ct2iop
394154704Smjacob#undef	ct2eiop
395163899Smjacob#undef	ct7iop
39655373Smjacob#undef	lunenp
39755373Smjacob#undef	inotp
39855373Smjacob#undef	inot_fcp
399154704Smjacob#undef	inote_fcp
400163899Smjacob#undef	inot_24xx
40155373Smjacob#undef	nackp
40255373Smjacob#undef	nack_fcp
403154704Smjacob#undef	nacke_fcp
404163899Smjacob#undef	hack_24xx
405163899Smjacob#undef	abts
406163899Smjacob#undef	abts_rsp
407163899Smjacob#undef	els
40855373Smjacob#undef	hdrp
40955373Smjacob	return (rval);
41055373Smjacob}
41155373Smjacob
41255373Smjacobint
413157943Smjacobisp_target_put_entry(ispsoftc_t *isp, void *ap)
41455373Smjacob{
41555373Smjacob	void *outp;
416155704Smjacob	uint8_t etype = ((isphdr_t *) ap)->rqs_entry_type;
41755373Smjacob
418196008Smjacob	outp = isp_getrqentry(isp);
419196008Smjacob	if (outp == NULL) {
420196008Smjacob		isp_prt(isp, ISP_LOGWARN, rqo, __func__);
42155373Smjacob		return (-1);
42255373Smjacob	}
42355373Smjacob	switch (etype) {
42455373Smjacob	case RQSTYPE_ATIO2:
425196008Smjacob		if (ISP_CAP_2KLOGIN(isp)) {
426196008Smjacob			isp_put_atio2e(isp, (at2e_entry_t *) ap, (at2e_entry_t *) outp);
427163899Smjacob		} else {
428196008Smjacob			isp_put_atio2(isp, (at2_entry_t *) ap, (at2_entry_t *) outp);
429163899Smjacob		}
43055373Smjacob		break;
43155373Smjacob	case RQSTYPE_CTIO2:
432196008Smjacob		if (ISP_CAP_2KLOGIN(isp)) {
433196008Smjacob			isp_put_ctio2e(isp, (ct2e_entry_t *) ap, (ct2e_entry_t *) outp);
434163899Smjacob		} else {
435196008Smjacob			isp_put_ctio2(isp, (ct2_entry_t *) ap, (ct2_entry_t *) outp);
436163899Smjacob		}
43755373Smjacob		break;
438163899Smjacob	case RQSTYPE_CTIO7:
439163899Smjacob		isp_put_ctio7(isp, (ct7_entry_t *) ap, (ct7_entry_t *) outp);
440163899Smjacob		break;
44155373Smjacob	default:
442196008Smjacob		isp_prt(isp, ISP_LOGERR, "%s: Unknown type 0x%x", __func__, etype);
44355373Smjacob		return (-1);
44455373Smjacob	}
445196008Smjacob	ISP_TDQE(isp, __func__, isp->isp_reqidx, ap);
446196008Smjacob	ISP_SYNC_REQUEST(isp);
44755373Smjacob	return (0);
44855373Smjacob}
44955373Smjacob
45055373Smjacobint
451157943Smjacobisp_target_put_atio(ispsoftc_t *isp, void *arg)
45255373Smjacob{
453291188Smav	at2_entry_t *aep = arg;
45455373Smjacob	union {
45555373Smjacob		at2_entry_t _atio2;
456154704Smjacob		at2e_entry_t _atio2e;
45755373Smjacob	} atun;
45855373Smjacob
459196008Smjacob	ISP_MEMZERO(&atun, sizeof atun);
460291188Smav	atun._atio2.at_header.rqs_entry_type = RQSTYPE_ATIO2;
461291188Smav	atun._atio2.at_header.rqs_entry_count = 1;
462291188Smav	if (ISP_CAP_SCCFW(isp)) {
463291188Smav		atun._atio2.at_scclun = aep->at_scclun;
46455373Smjacob	} else {
465291188Smav		atun._atio2.at_lun = (uint8_t) aep->at_lun;
46655373Smjacob	}
467291188Smav	if (ISP_CAP_2KLOGIN(isp)) {
468291188Smav		atun._atio2e.at_iid = ((at2e_entry_t *)aep)->at_iid;
469291188Smav	} else {
470291188Smav		atun._atio2.at_iid = aep->at_iid;
471291188Smav	}
472291188Smav	atun._atio2.at_rxid = aep->at_rxid;
473291188Smav	atun._atio2.at_status = CT_OK;
47455373Smjacob	return (isp_target_put_entry(isp, &atun));
47555373Smjacob}
47655373Smjacob
47755373Smjacob/*
47855373Smjacob * Command completion- both for handling cases of no resources or
47955373Smjacob * no blackhole driver, or other cases where we have to, inline,
48055373Smjacob * finish the command sanely, or for normal command completion.
48155373Smjacob *
48255373Smjacob * The 'completion' code value has the scsi status byte in the low 8 bits.
48355373Smjacob * If status is a CHECK CONDITION and bit 8 is nonzero, then bits 12..15 have
48455373Smjacob * the sense key and  bits 16..23 have the ASCQ and bits 24..31 have the ASC
48555373Smjacob * values.
48655373Smjacob *
48755373Smjacob * NB: the key, asc, ascq, cannot be used for parallel SCSI as it doesn't
48875196Smjacob * NB: inline SCSI sense reporting. As such, we lose this information. XXX.
48955373Smjacob *
49055373Smjacob * For both parallel && fibre channel, we use the feature that does
49155373Smjacob * an automatic resource autoreplenish so we don't have then later do
49255373Smjacob * put of an atio to replenish the f/w's resource count.
49355373Smjacob */
49455373Smjacob
49555373Smjacobint
496196008Smjacobisp_endcmd(ispsoftc_t *isp, ...)
49755373Smjacob{
498196008Smjacob	uint32_t code, hdl;
499196008Smjacob	uint8_t sts;
50055373Smjacob	union {
50155373Smjacob		ct2_entry_t _ctio2;
502154704Smjacob		ct2e_entry_t _ctio2e;
503163899Smjacob		ct7_entry_t _ctio7;
50455373Smjacob	} un;
505196008Smjacob	va_list ap;
50655373Smjacob
507196008Smjacob	ISP_MEMZERO(&un, sizeof un);
50855373Smjacob
509163899Smjacob	if (IS_24XX(isp)) {
510196008Smjacob		int vpidx, nphdl;
511196008Smjacob		at7_entry_t *aep;
512163899Smjacob		ct7_entry_t *cto = &un._ctio7;
513163899Smjacob
514196008Smjacob		va_start(ap, isp);
515196008Smjacob		aep = va_arg(ap, at7_entry_t *);
516196008Smjacob		nphdl = va_arg(ap, int);
517196008Smjacob		/*
518196008Smjacob		 * Note that vpidx may equal 0xff (unknown) here
519196008Smjacob		 */
520196008Smjacob		vpidx = va_arg(ap, int);
521196008Smjacob		code = va_arg(ap, uint32_t);
522196008Smjacob		hdl = va_arg(ap, uint32_t);
523196008Smjacob		va_end(ap);
524196008Smjacob		isp_prt(isp, ISP_LOGTDEBUG0, "%s: [RX_ID 0x%x] chan %d code %x", __func__, aep->at_rxid, vpidx, code);
525196008Smjacob
526196008Smjacob		sts = code & 0xff;
527163899Smjacob		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
528163899Smjacob		cto->ct_header.rqs_entry_count = 1;
529196008Smjacob		cto->ct_nphdl = nphdl;
530163899Smjacob		cto->ct_rxid = aep->at_rxid;
531196008Smjacob		cto->ct_iid_lo = (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
532163899Smjacob		cto->ct_iid_hi = aep->at_hdr.s_id[0];
533163899Smjacob		cto->ct_oxid = aep->at_hdr.ox_id;
534163899Smjacob		cto->ct_scsi_status = sts;
535196008Smjacob		cto->ct_vpidx = vpidx;
536196008Smjacob		cto->ct_flags = CT7_NOACK;
537196008Smjacob		if (code & ECMD_TERMINATE) {
538196008Smjacob			cto->ct_flags |= CT7_TERMINATE;
539196008Smjacob		} else if (code & ECMD_SVALID) {
540196008Smjacob			cto->ct_flags |= CT7_FLAG_MODE1 | CT7_SENDSTATUS;
541196008Smjacob			cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8);
542196008Smjacob			cto->rsp.m1.ct_resplen = cto->ct_senselen = min(16, MAXRESPLEN_24XX);
543196008Smjacob			ISP_MEMZERO(cto->rsp.m1.ct_resp, sizeof (cto->rsp.m1.ct_resp));
544163899Smjacob			cto->rsp.m1.ct_resp[0] = 0xf0;
545163899Smjacob			cto->rsp.m1.ct_resp[2] = (code >> 12) & 0xf;
546163899Smjacob			cto->rsp.m1.ct_resp[7] = 8;
547238869Smjacob			cto->rsp.m1.ct_resp[12] = (code >> 16) & 0xff;
548238869Smjacob			cto->rsp.m1.ct_resp[13] = (code >> 24) & 0xff;
549196008Smjacob		} else {
550196008Smjacob			cto->ct_flags |= CT7_FLAG_MODE1 | CT7_SENDSTATUS;
551163899Smjacob		}
552163899Smjacob		if (aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl) {
553163899Smjacob			cto->ct_resid = aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl;
554196008Smjacob			if (cto->ct_resid < 0) {
555196008Smjacob				 cto->ct_scsi_status |= (FCP_RESID_OVERFLOW << 8);
556196008Smjacob			} else if (cto->ct_resid > 0) {
557196008Smjacob				 cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8);
558196008Smjacob			}
559163899Smjacob		}
560163899Smjacob		cto->ct_syshandle = hdl;
561291188Smav	} else {
562196008Smjacob		at2_entry_t *aep;
56355373Smjacob		ct2_entry_t *cto = &un._ctio2;
56455373Smjacob
565196008Smjacob		va_start(ap, isp);
566196008Smjacob		aep = va_arg(ap, at2_entry_t *);
567196008Smjacob		code = va_arg(ap, uint32_t);
568196008Smjacob		hdl = va_arg(ap, uint32_t);
569196008Smjacob		va_end(ap);
570196008Smjacob
571196008Smjacob		isp_prt(isp, ISP_LOGTDEBUG0, "%s: [RX_ID 0x%x] code %x", __func__, aep->at_rxid, code);
572196008Smjacob
573196008Smjacob		sts = code & 0xff;
57455373Smjacob		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
57555373Smjacob		cto->ct_header.rqs_entry_count = 1;
576196008Smjacob		if (ISP_CAP_SCCFW(isp) == 0) {
57761774Smjacob			cto->ct_lun = aep->at_lun;
57861774Smjacob		}
579196008Smjacob		if (ISP_CAP_2KLOGIN(isp)) {
580154704Smjacob			un._ctio2e.ct_iid = ((at2e_entry_t *)aep)->at_iid;
581154704Smjacob		} else {
582154704Smjacob			cto->ct_iid = aep->at_iid;
583154704Smjacob		}
58455373Smjacob		cto->ct_rxid = aep->at_rxid;
585125189Smjacob		cto->rsp.m1.ct_scsi_status = sts;
58655373Smjacob		cto->ct_flags = CT2_SENDSTATUS | CT2_NO_DATA | CT2_FLAG_MODE1;
58755373Smjacob		if (hdl == 0) {
58855373Smjacob			cto->ct_flags |= CT2_CCINCR;
58955373Smjacob		}
59055373Smjacob		if (aep->at_datalen) {
59155373Smjacob			cto->ct_resid = aep->at_datalen;
59277365Smjacob			cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER;
59355373Smjacob		}
594125189Smjacob		if (sts == SCSI_CHECK && (code & ECMD_SVALID)) {
59555373Smjacob			cto->rsp.m1.ct_resp[0] = 0xf0;
59655373Smjacob			cto->rsp.m1.ct_resp[2] = (code >> 12) & 0xf;
59755373Smjacob			cto->rsp.m1.ct_resp[7] = 8;
59855373Smjacob			cto->rsp.m1.ct_resp[12] = (code >> 24) & 0xff;
59955373Smjacob			cto->rsp.m1.ct_resp[13] = (code >> 16) & 0xff;
60055373Smjacob			cto->rsp.m1.ct_senselen = 16;
60177365Smjacob			cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
60255373Smjacob		}
60374232Smjacob		cto->ct_syshandle = hdl;
60455373Smjacob	}
60555373Smjacob	return (isp_target_put_entry(isp, &un));
60655373Smjacob}
60755373Smjacob
608157943Smjacob/*
609157943Smjacob * These are either broadcast events or specifically CTIO fast completion
610157943Smjacob */
611196008Smjacob
61298284Smjacobint
613157943Smjacobisp_target_async(ispsoftc_t *isp, int bus, int event)
61455373Smjacob{
615196008Smjacob	isp_notify_t notify;
61655373Smjacob
617196008Smjacob	ISP_MEMZERO(&notify, sizeof (isp_notify_t));
618154704Smjacob	notify.nt_hba = isp;
619196008Smjacob	notify.nt_wwn = INI_ANY;
620196008Smjacob	notify.nt_nphdl = NIL_HANDLE;
621196008Smjacob	notify.nt_sid = PORT_ANY;
622196008Smjacob	notify.nt_did = PORT_ANY;
623196008Smjacob	notify.nt_tgt = TGT_ANY;
624196008Smjacob	notify.nt_channel = bus;
625154704Smjacob	notify.nt_lun = LUN_ANY;
626154704Smjacob	notify.nt_tagval = TAG_ANY;
627196008Smjacob	notify.nt_tagval |= (((uint64_t)(isp->isp_serno++)) << 32);
628154704Smjacob
62955373Smjacob	switch (event) {
630154704Smjacob	case ASYNC_LOOP_UP:
631154704Smjacob	case ASYNC_PTPMODE:
632196008Smjacob		isp_prt(isp, ISP_LOGTDEBUG0, "%s: LOOP UP", __func__);
633154704Smjacob		notify.nt_ncode = NT_LINK_UP;
634196008Smjacob		isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
635154704Smjacob		break;
636154704Smjacob	case ASYNC_LOOP_DOWN:
637196008Smjacob		isp_prt(isp, ISP_LOGTDEBUG0, "%s: LOOP DOWN", __func__);
638154704Smjacob		notify.nt_ncode = NT_LINK_DOWN;
639196008Smjacob		isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
640154704Smjacob		break;
641163899Smjacob	case ASYNC_LIP_ERROR:
642291265Smav	case ASYNC_LIP_NOS_OLS_RECV:
64355373Smjacob	case ASYNC_LIP_OCCURRED:
64483027Smjacob	case ASYNC_LOOP_RESET:
645196008Smjacob		isp_prt(isp, ISP_LOGTDEBUG0, "%s: LIP RESET", __func__);
646154704Smjacob		notify.nt_ncode = NT_LIP_RESET;
647196008Smjacob		isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
648154704Smjacob		break;
64955373Smjacob	case ASYNC_BUS_RESET:
650154704Smjacob	case ASYNC_TIMEOUT_RESET:	/* XXX: where does this come from ? */
651196008Smjacob		isp_prt(isp, ISP_LOGTDEBUG0, "%s: BUS RESET", __func__);
652154704Smjacob		notify.nt_ncode = NT_BUS_RESET;
653196008Smjacob		isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
65455373Smjacob		break;
65555373Smjacob	case ASYNC_DEVICE_RESET:
656196008Smjacob		isp_prt(isp, ISP_LOGTDEBUG0, "%s: DEVICE RESET", __func__);
657154704Smjacob		notify.nt_ncode = NT_TARGET_RESET;
658196008Smjacob		isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
659154704Smjacob		break;
660154704Smjacob	case ASYNC_CTIO_DONE:
661154704Smjacob	{
662154704Smjacob		uint8_t storage[QENTRY_LEN];
663196008Smjacob		isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO DONE", __func__);
664154704Smjacob		memset(storage, 0, QENTRY_LEN);
665163899Smjacob		if (IS_24XX(isp)) {
666163899Smjacob			ct7_entry_t *ct = (ct7_entry_t *) storage;
667163899Smjacob			ct->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
668163899Smjacob			ct->ct_nphdl = CT7_OK;
669163899Smjacob			ct->ct_syshandle = bus;
670196008Smjacob			ct->ct_flags = CT7_SENDSTATUS;
671291188Smav		} else {
672160251Smjacob            		/* This should also suffice for 2K login code */
673154704Smjacob			ct2_entry_t *ct = (ct2_entry_t *) storage;
674154704Smjacob			ct->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
675154704Smjacob			ct->ct_status = CT_OK;
676154704Smjacob			ct->ct_syshandle = bus;
677154704Smjacob			ct->ct_flags = CT2_SENDSTATUS|CT2_FASTPOST;
67855373Smjacob		}
679196008Smjacob		isp_async(isp, ISPASYNC_TARGET_ACTION, storage);
680163899Smjacob		break;
681154704Smjacob	}
68255373Smjacob	default:
683196008Smjacob		isp_prt(isp, ISP_LOGERR, "%s: unknown event 0x%x", __func__, event);
684154704Smjacob		if (isp->isp_state == ISP_RUNSTATE) {
685238869Smjacob			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, NULL);
686154704Smjacob		}
68755373Smjacob		break;
68855373Smjacob	}
689154704Smjacob	return (0);
69055373Smjacob}
69155373Smjacob
69255373Smjacob/*
69355373Smjacob * Synthesize a message from the task management flags in a FCP_CMND_IU.
69455373Smjacob */
69555373Smjacobstatic void
696157943Smjacobisp_got_msg_fc(ispsoftc_t *isp, in_fcentry_t *inp)
69755373Smjacob{
698196008Smjacob	isp_notify_t notify;
699291013Smav	static const char f1[] = "%s from N-port handle 0x%x lun %x seq 0x%x";
700291013Smav	static const char f2[] = "unknown %s 0x%x lun %x N-Port handle 0x%x task flags 0x%x seq 0x%x\n";
701291013Smav	uint16_t seqid, nphdl;
70255373Smjacob
703196008Smjacob	ISP_MEMZERO(&notify, sizeof (isp_notify_t));
704196008Smjacob	notify.nt_hba = isp;
705196008Smjacob	notify.nt_wwn = INI_ANY;
706196008Smjacob	if (ISP_CAP_2KLOGIN(isp)) {
707196008Smjacob		notify.nt_nphdl = ((in_fcentry_e_t *)inp)->in_iid;
708291013Smav		nphdl = ((in_fcentry_e_t *)inp)->in_iid;
709157943Smjacob		seqid = ((in_fcentry_e_t *)inp)->in_seqid;
710154704Smjacob	} else {
711196008Smjacob		notify.nt_nphdl = inp->in_iid;
712291013Smav		nphdl = inp->in_iid;
713157943Smjacob		seqid = inp->in_seqid;
714154704Smjacob	}
715196008Smjacob	notify.nt_sid = PORT_ANY;
716196008Smjacob	notify.nt_did = PORT_ANY;
717196008Smjacob
718154704Smjacob	/* nt_tgt set in outer layers */
719196008Smjacob	if (ISP_CAP_SCCFW(isp)) {
720196008Smjacob		notify.nt_lun = inp->in_scclun;
721289882Smav#if __FreeBSD_version < 1000700
722289882Smav		notify.nt_lun &= 0x3fff;
723289882Smav#endif
72482843Smjacob	} else {
725196008Smjacob		notify.nt_lun = inp->in_lun;
72682843Smjacob	}
727196008Smjacob	notify.nt_tagval = seqid;
728196008Smjacob	notify.nt_tagval |= (((uint64_t)(isp->isp_serno++)) << 32);
729196008Smjacob	notify.nt_need_ack = 1;
730196008Smjacob	notify.nt_lreserved = inp;
73182843Smjacob
73255373Smjacob	if (inp->in_status != IN_MSG_RECEIVED) {
733291013Smav		isp_prt(isp, ISP_LOGINFO, f2, "immediate notify status", inp->in_status, notify.nt_lun, nphdl, inp->in_task_flags, inp->in_seqid);
734238869Smjacob		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inp);
735154704Smjacob		return;
736154704Smjacob	}
737154704Smjacob
738154704Smjacob	if (inp->in_task_flags & TASK_FLAGS_ABORT_TASK_SET) {
739291013Smav		isp_prt(isp, ISP_LOGINFO, f1, "ABORT TASK SET", nphdl, notify.nt_lun, inp->in_seqid);
740196008Smjacob		notify.nt_ncode = NT_ABORT_TASK_SET;
741154704Smjacob	} else if (inp->in_task_flags & TASK_FLAGS_CLEAR_TASK_SET) {
742291013Smav		isp_prt(isp, ISP_LOGINFO, f1, "CLEAR TASK SET", nphdl, notify.nt_lun, inp->in_seqid);
743196008Smjacob		notify.nt_ncode = NT_CLEAR_TASK_SET;
744154704Smjacob	} else if (inp->in_task_flags & TASK_FLAGS_LUN_RESET) {
745291013Smav		isp_prt(isp, ISP_LOGINFO, f1, "LUN RESET", nphdl, notify.nt_lun, inp->in_seqid);
746196008Smjacob		notify.nt_ncode = NT_LUN_RESET;
747154704Smjacob	} else if (inp->in_task_flags & TASK_FLAGS_TARGET_RESET) {
748291013Smav		isp_prt(isp, ISP_LOGINFO, f1, "TARGET RESET", nphdl, notify.nt_lun, inp->in_seqid);
749196008Smjacob		notify.nt_ncode = NT_TARGET_RESET;
750154704Smjacob	} else if (inp->in_task_flags & TASK_FLAGS_CLEAR_ACA) {
751291013Smav		isp_prt(isp, ISP_LOGINFO, f1, "CLEAR ACA", nphdl, notify.nt_lun, inp->in_seqid);
752196008Smjacob		notify.nt_ncode = NT_CLEAR_ACA;
75355373Smjacob	} else {
754291013Smav		isp_prt(isp, ISP_LOGWARN, f2, "task flag", inp->in_status, notify.nt_lun, nphdl, inp->in_task_flags,  inp->in_seqid);
755238869Smjacob		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inp);
756154704Smjacob		return;
75755373Smjacob	}
758196008Smjacob	isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
75955373Smjacob}
76055373Smjacob
761163899Smjacobstatic void
762163899Smjacobisp_got_tmf_24xx(ispsoftc_t *isp, at7_entry_t *aep)
763163899Smjacob{
764196008Smjacob	isp_notify_t notify;
765291013Smav	static const char f1[] = "%s from PortID 0x%06x lun %x seq 0x%08x";
766291013Smav	static const char f2[] = "unknown Task Flag 0x%x lun %x PortID 0x%x tag 0x%08x";
767196008Smjacob	uint16_t chan;
768196008Smjacob	uint32_t sid, did;
769163899Smjacob
770196008Smjacob	ISP_MEMZERO(&notify, sizeof (isp_notify_t));
771196008Smjacob	notify.nt_hba = isp;
772196008Smjacob	notify.nt_wwn = INI_ANY;
773196008Smjacob	notify.nt_lun = (aep->at_cmnd.fcp_cmnd_lun[0] << 8) | (aep->at_cmnd.fcp_cmnd_lun[1]);
774196008Smjacob	notify.nt_tagval = aep->at_rxid;
775196008Smjacob	notify.nt_tagval |= (((uint64_t)(isp->isp_serno++)) << 32);
776196008Smjacob	notify.nt_lreserved = aep;
777196008Smjacob	sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] <<  8) | (aep->at_hdr.s_id[2]);
778163899Smjacob
779196008Smjacob	/* Channel has to derived from D_ID */
780196008Smjacob	did = (aep->at_hdr.d_id[0] << 16) | (aep->at_hdr.d_id[1] << 8) | aep->at_hdr.d_id[2];
781196008Smjacob	for (chan = 0; chan < isp->isp_nchan; chan++) {
782196008Smjacob		if (FCPARAM(isp, chan)->isp_portid == did) {
783196008Smjacob			break;
784196008Smjacob		}
785196008Smjacob	}
786196008Smjacob	if (chan == isp->isp_nchan) {
787196008Smjacob		isp_prt(isp, ISP_LOGWARN, "%s: D_ID 0x%x not found on any channel", __func__, did);
788196008Smjacob		/* just drop on the floor */
789196008Smjacob		return;
790196008Smjacob	}
791196008Smjacob	notify.nt_nphdl = NIL_HANDLE; /* unknown here */
792196008Smjacob	notify.nt_sid = sid;
793196008Smjacob	notify.nt_did = did;
794196008Smjacob	notify.nt_channel = chan;
795289843Smav	if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_QUERY_TASK_SET) {
796289843Smav		isp_prt(isp, ISP_LOGINFO, f1, "QUERY TASK SET", sid, notify.nt_lun, aep->at_rxid);
797289843Smav		notify.nt_ncode = NT_QUERY_TASK_SET;
798289843Smav	} else if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_ABORT_TASK_SET) {
799196008Smjacob		isp_prt(isp, ISP_LOGINFO, f1, "ABORT TASK SET", sid, notify.nt_lun, aep->at_rxid);
800196008Smjacob		notify.nt_ncode = NT_ABORT_TASK_SET;
801196008Smjacob	} else if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_CLEAR_TASK_SET) {
802196008Smjacob		isp_prt(isp, ISP_LOGINFO, f1, "CLEAR TASK SET", sid, notify.nt_lun, aep->at_rxid);
803196008Smjacob		notify.nt_ncode = NT_CLEAR_TASK_SET;
804289843Smav	} else if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_QUERY_ASYNC_EVENT) {
805289843Smav		isp_prt(isp, ISP_LOGINFO, f1, "QUERY ASYNC EVENT", sid, notify.nt_lun, aep->at_rxid);
806289843Smav		notify.nt_ncode = NT_QUERY_ASYNC_EVENT;
807196008Smjacob	} else if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_LUN_RESET) {
808196008Smjacob		isp_prt(isp, ISP_LOGINFO, f1, "LUN RESET", sid, notify.nt_lun, aep->at_rxid);
809196008Smjacob		notify.nt_ncode = NT_LUN_RESET;
810196008Smjacob	} else if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_TGT_RESET) {
811196008Smjacob		isp_prt(isp, ISP_LOGINFO, f1, "TARGET RESET", sid, notify.nt_lun, aep->at_rxid);
812196008Smjacob		notify.nt_ncode = NT_TARGET_RESET;
813196008Smjacob	} else if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_CLEAR_ACA) {
814196008Smjacob		isp_prt(isp, ISP_LOGINFO, f1, "CLEAR ACA", sid, notify.nt_lun, aep->at_rxid);
815196008Smjacob		notify.nt_ncode = NT_CLEAR_ACA;
816163899Smjacob	} else {
817196008Smjacob		isp_prt(isp, ISP_LOGWARN, f2, aep->at_cmnd.fcp_cmnd_task_management, notify.nt_lun, sid, aep->at_rxid);
818196008Smjacob		notify.nt_ncode = NT_UNKNOWN;
819163899Smjacob		return;
820163899Smjacob	}
821196008Smjacob	isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
822163899Smjacob}
823163899Smjacob
824196008Smjacobint
825157943Smjacobisp_notify_ack(ispsoftc_t *isp, void *arg)
82655373Smjacob{
82755373Smjacob	char storage[QENTRY_LEN];
82855373Smjacob	void *outp;
82955373Smjacob
830196008Smjacob	/*
831196008Smjacob	 * This is in case a Task Management Function ends up here.
832196008Smjacob	 */
833196008Smjacob	if (IS_24XX(isp) && arg != NULL && (((isphdr_t *)arg)->rqs_entry_type == RQSTYPE_ATIO)) {
834196008Smjacob		at7_entry_t *aep = arg;
835196008Smjacob		return (isp_endcmd(isp, aep, NIL_HANDLE, 0, 0, 0));
83655373Smjacob	}
83755373Smjacob
838196008Smjacob	outp = isp_getrqentry(isp);
839196008Smjacob	if (outp == NULL) {
840196008Smjacob		isp_prt(isp, ISP_LOGWARN, rqo, __func__);
841196008Smjacob		return (1);
842196008Smjacob	}
84355373Smjacob
844196008Smjacob	ISP_MEMZERO(storage, QENTRY_LEN);
845196008Smjacob
846196008Smjacob	if (IS_24XX(isp)) {
847163899Smjacob		na_fcentry_24xx_t *na = (na_fcentry_24xx_t *) storage;
848238869Smjacob		na->na_header.rqs_entry_type = RQSTYPE_NOTIFY_ACK;
849238869Smjacob		na->na_header.rqs_entry_count = 1;
850163899Smjacob		if (arg) {
851163899Smjacob			in_fcentry_24xx_t *in = arg;
852163899Smjacob			na->na_nphdl = in->in_nphdl;
853238869Smjacob			na->na_flags = in->in_flags;
854163899Smjacob			na->na_status = in->in_status;
855163899Smjacob			na->na_status_subcode = in->in_status_subcode;
856289838Smav			na->na_fwhandle = in->in_fwhandle;
857163899Smjacob			na->na_rxid = in->in_rxid;
858163899Smjacob			na->na_oxid = in->in_oxid;
859196008Smjacob			na->na_vpidx = in->in_vpidx;
860163899Smjacob			if (in->in_status == IN24XX_SRR_RCVD) {
861163899Smjacob				na->na_srr_rxid = in->in_srr_rxid;
862163899Smjacob				na->na_srr_reloff_hi = in->in_srr_reloff_hi;
863163899Smjacob				na->na_srr_reloff_lo = in->in_srr_reloff_lo;
864163899Smjacob				na->na_srr_iu = in->in_srr_iu;
865238869Smjacob				/*
866238869Smjacob				 * Whether we're accepting the SRR or rejecting
867238869Smjacob				 * it is determined by looking at the in_reserved
868238869Smjacob				 * field in the original notify structure.
869238869Smjacob				 */
870238869Smjacob				if (in->in_reserved) {
871238869Smjacob					na->na_srr_flags = 1;
872238869Smjacob					na->na_srr_reject_vunique = 0;
873238869Smjacob					na->na_srr_reject_code = 9;		/* unable to perform this command at this time */
874238869Smjacob					na->na_srr_reject_explanation = 0x2a;	/* unable to supply the requested data */
875238869Smjacob				}
876163899Smjacob			}
877163899Smjacob		}
878163899Smjacob		isp_put_notify_24xx_ack(isp, na, (na_fcentry_24xx_t *)outp);
879291188Smav	} else {
88055373Smjacob		na_fcentry_t *na = (na_fcentry_t *) storage;
881160978Smjacob		int iid = 0;
882160978Smjacob
88355373Smjacob		if (arg) {
88455373Smjacob			in_fcentry_t *inp = arg;
885196008Smjacob			ISP_MEMCPY(storage, arg, sizeof (isphdr_t));
886196008Smjacob			if (ISP_CAP_2KLOGIN(isp)) {
887196008Smjacob				((na_fcentry_e_t *)na)->na_iid = ((in_fcentry_e_t *)inp)->in_iid;
888160978Smjacob				iid = ((na_fcentry_e_t *)na)->na_iid;
889154704Smjacob			} else {
890154704Smjacob				na->na_iid = inp->in_iid;
891160978Smjacob				iid = na->na_iid;
892154704Smjacob			}
893196008Smjacob			na->na_task_flags = inp->in_task_flags & TASK_FLAGS_RESERVED_MASK;
89455373Smjacob			na->na_seqid = inp->in_seqid;
895238869Smjacob			na->na_status = inp->in_status;
89655373Smjacob			na->na_flags = NAFC_RCOUNT;
89755373Smjacob			if (inp->in_status == IN_RESET) {
898238869Smjacob				na->na_flags = NAFC_RST_CLRD;	/* We do not modify resource counts for LIP resets */
89955373Smjacob			}
900160978Smjacob			if (inp->in_status == IN_MSG_RECEIVED) {
901160978Smjacob				na->na_flags |= NAFC_TVALID;
902160978Smjacob				na->na_response = 0;	/* XXX SUCCEEDED XXX */
903160978Smjacob			}
90455373Smjacob		} else {
90555373Smjacob			na->na_flags = NAFC_RST_CLRD;
90655373Smjacob		}
90759453Smjacob		na->na_header.rqs_entry_type = RQSTYPE_NOTIFY_ACK;
90859453Smjacob		na->na_header.rqs_entry_count = 1;
909196008Smjacob		if (ISP_CAP_2KLOGIN(isp)) {
910196008Smjacob			isp_put_notify_ack_fc_e(isp, (na_fcentry_e_t *) na, (na_fcentry_e_t *)outp);
911154704Smjacob		} else {
912154704Smjacob			isp_put_notify_ack_fc(isp, na, (na_fcentry_t *)outp);
913154704Smjacob		}
914291013Smav		isp_prt(isp, ISP_LOGTDEBUG0, "notify ack handle %x seqid %x flags %x tflags %x response %x", iid, na->na_seqid,
915160978Smjacob		    na->na_flags, na->na_task_flags, na->na_response);
91655373Smjacob	}
917196008Smjacob	ISP_TDQE(isp, "isp_notify_ack", isp->isp_reqidx, storage);
918196008Smjacob	ISP_SYNC_REQUEST(isp);
919196008Smjacob	return (0);
92055373Smjacob}
92155373Smjacob
922196008Smjacobint
923196008Smjacobisp_acknak_abts(ispsoftc_t *isp, void *arg, int errno)
924196008Smjacob{
925196008Smjacob	char storage[QENTRY_LEN];
926196008Smjacob	uint16_t tmpw;
927196008Smjacob	uint8_t tmpb;
928196008Smjacob	abts_t *abts = arg;
929196008Smjacob	abts_rsp_t *rsp = (abts_rsp_t *) storage;
930196008Smjacob	void *outp;
931196008Smjacob
932196008Smjacob	if (!IS_24XX(isp)) {
933196008Smjacob		isp_prt(isp, ISP_LOGERR, "%s: called for non-24XX card", __func__);
934196008Smjacob		return (0);
935196008Smjacob	}
936196008Smjacob
937196008Smjacob	if (abts->abts_header.rqs_entry_type != RQSTYPE_ABTS_RCVD) {
938196008Smjacob		isp_prt(isp, ISP_LOGERR, "%s: called for non-ABTS entry (0x%x)", __func__, abts->abts_header.rqs_entry_type);
939196008Smjacob		return (0);
940196008Smjacob	}
941196008Smjacob
942196008Smjacob	outp = isp_getrqentry(isp);
943196008Smjacob	if (outp == NULL) {
944196008Smjacob		isp_prt(isp, ISP_LOGWARN, rqo, __func__);
945196008Smjacob		return (1);
946196008Smjacob	}
947196008Smjacob
948196008Smjacob	ISP_MEMCPY(rsp, abts, QENTRY_LEN);
949196008Smjacob	rsp->abts_rsp_header.rqs_entry_type = RQSTYPE_ABTS_RSP;
950196008Smjacob
951196008Smjacob	/*
952196008Smjacob	 * Swap destination and source for response.
953196008Smjacob	 */
954196008Smjacob	rsp->abts_rsp_r_ctl = BA_ACC;
955196008Smjacob	tmpw = rsp->abts_rsp_did_lo;
956196008Smjacob	tmpb = rsp->abts_rsp_did_hi;
957196008Smjacob	rsp->abts_rsp_did_lo = rsp->abts_rsp_sid_lo;
958196008Smjacob	rsp->abts_rsp_did_hi = rsp->abts_rsp_sid_hi;
959196008Smjacob	rsp->abts_rsp_sid_lo = tmpw;
960196008Smjacob	rsp->abts_rsp_sid_hi = tmpb;
961196008Smjacob
962196008Smjacob	rsp->abts_rsp_f_ctl_hi ^= 0x80; 	/* invert Exchange Context */
963196008Smjacob	rsp->abts_rsp_f_ctl_hi &= ~0x7f;	/* clear Sequence Initiator and other bits */
964196008Smjacob	rsp->abts_rsp_f_ctl_hi |= 0x10;		/* abort the whole exchange */
965196008Smjacob	rsp->abts_rsp_f_ctl_hi |= 0x8;		/* last data frame of sequence */
966196008Smjacob	rsp->abts_rsp_f_ctl_hi |= 0x1;		/* transfer Sequence Initiative */
967196008Smjacob	rsp->abts_rsp_f_ctl_lo = 0;
968196008Smjacob
969196008Smjacob	if (errno == 0) {
970196008Smjacob		uint16_t rx_id, ox_id;
971196008Smjacob
972196008Smjacob		rx_id = rsp->abts_rsp_rx_id;
973196008Smjacob		ox_id = rsp->abts_rsp_ox_id;
974196008Smjacob		ISP_MEMZERO(&rsp->abts_rsp_payload.ba_acc, sizeof (rsp->abts_rsp_payload.ba_acc));
975196008Smjacob                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);
976196008Smjacob                rsp->abts_rsp_payload.ba_acc.aborted_rx_id = rx_id;
977196008Smjacob                rsp->abts_rsp_payload.ba_acc.aborted_ox_id = ox_id;
978196008Smjacob                rsp->abts_rsp_payload.ba_acc.high_seq_cnt = 0xffff;
979196008Smjacob	} else {
980196008Smjacob		ISP_MEMZERO(&rsp->abts_rsp_payload.ba_rjt, sizeof (rsp->abts_rsp_payload.ba_acc));
981196008Smjacob		switch (errno) {
982196008Smjacob		case ENOMEM:
983238869Smjacob			rsp->abts_rsp_payload.ba_rjt.reason = 5;	/* Logical Unit Busy */
984196008Smjacob			break;
985196008Smjacob		default:
986196008Smjacob			rsp->abts_rsp_payload.ba_rjt.reason = 9;	/* Unable to perform command request */
987196008Smjacob			break;
988196008Smjacob		}
989196008Smjacob	}
990196008Smjacob
991196008Smjacob	/*
992196008Smjacob	 * The caller will have set response values as appropriate
993196008Smjacob	 * in the ABTS structure just before calling us.
994196008Smjacob	 */
995196008Smjacob	isp_put_abts_rsp(isp, rsp, (abts_rsp_t *)outp);
996196008Smjacob	ISP_TDQE(isp, "isp_acknak_abts", isp->isp_reqidx, storage);
997196008Smjacob	ISP_SYNC_REQUEST(isp);
998196008Smjacob	return (0);
999196008Smjacob}
1000196008Smjacob
100155373Smjacobstatic void
1002157943Smjacobisp_handle_atio2(ispsoftc_t *isp, at2_entry_t *aep)
100355373Smjacob{
1004154704Smjacob	int lun, iid;
100561774Smjacob
1006196008Smjacob	if (ISP_CAP_SCCFW(isp)) {
100761774Smjacob		lun = aep->at_scclun;
1008289882Smav#if __FreeBSD_version < 1000700
1009289882Smav		lun &= 0x3fff;
1010289882Smav#endif
101161774Smjacob	} else {
101261774Smjacob		lun = aep->at_lun;
101361774Smjacob	}
101461774Smjacob
1015196008Smjacob	if (ISP_CAP_2KLOGIN(isp)) {
1016154704Smjacob		iid = ((at2e_entry_t *)aep)->at_iid;
1017154704Smjacob	} else {
1018154704Smjacob		iid = aep->at_iid;
1019154704Smjacob	}
1020154704Smjacob
102155373Smjacob	/*
102255373Smjacob	 * The firmware status (except for the QLTM_SVALID bit) indicates
102355373Smjacob	 * why this ATIO was sent to us.
102455373Smjacob	 *
102555373Smjacob	 * If QLTM_SVALID is set, the firware has recommended Sense Data.
102655373Smjacob	 *
102755373Smjacob	 * If the DISCONNECTS DISABLED bit is set in the flags field,
102855373Smjacob	 * we're still connected on the SCSI bus - i.e. the initiator
102955373Smjacob	 * did not set DiscPriv in the identify message. We don't care
103055373Smjacob	 * about this so it's ignored.
103155373Smjacob	 */
103255373Smjacob
1033196008Smjacob	switch (aep->at_status & ~QLTM_SVALID) {
103455373Smjacob	case AT_PATH_INVALID:
103555373Smjacob		/*
103655373Smjacob		 * ATIO rejected by the firmware due to disabled lun.
103755373Smjacob		 */
1038289882Smav		isp_prt(isp, ISP_LOGERR, "rejected ATIO2 for disabled lun %x", lun);
103955373Smjacob		break;
104055373Smjacob	case AT_NOCAP:
104155373Smjacob		/*
104255373Smjacob		 * Requested Capability not available
104355373Smjacob		 * We sent an ATIO that overflowed the firmware's
104455373Smjacob		 * command resource count.
104555373Smjacob		 */
1046289882Smav		isp_prt(isp, ISP_LOGERR, "rejected ATIO2 for lun %x- command count overflow", lun);
104755373Smjacob		break;
104855373Smjacob
104955373Smjacob	case AT_BDR_MSG:
105055373Smjacob		/*
105155373Smjacob		 * If we send an ATIO to the firmware to increment
105255373Smjacob		 * its command resource count, and the firmware is
105355373Smjacob		 * recovering from a Bus Device Reset, it returns
105455373Smjacob		 * the ATIO with this status. We set the command
105555373Smjacob		 * resource count in the Enable Lun entry and no
105655373Smjacob		 * not increment it. Therefore we should never get
105755373Smjacob		 * this status here.
105855373Smjacob		 */
105983027Smjacob		isp_prt(isp, ISP_LOGERR, atiocope, lun, 0);
106055373Smjacob		break;
106155373Smjacob
106255373Smjacob	case AT_CDB:		/* Got a CDB */
106355373Smjacob		/*
106455373Smjacob		 * Punt to platform specific layer.
106555373Smjacob		 */
1066196008Smjacob		isp_async(isp, ISPASYNC_TARGET_ACTION, aep);
106755373Smjacob		break;
106855373Smjacob
106955373Smjacob	case AT_RESET:
107055373Smjacob		/*
107155373Smjacob		 * A bus reset came along an blew away this command. Why
107255373Smjacob		 * they do this in addition the async event code stuff,
107355373Smjacob		 * I dunno.
107455373Smjacob		 *
107555373Smjacob		 * Ignore it because the async event will clear things
107655373Smjacob		 * up for us.
107755373Smjacob		 */
1078154704Smjacob		isp_prt(isp, ISP_LOGERR, atior, lun, iid, 0);
107955373Smjacob		break;
108055373Smjacob
108155373Smjacob
108255373Smjacob	default:
1083291013Smav		isp_prt(isp, ISP_LOGERR, "Unknown ATIO2 status 0x%x from handle %d for lun %x", aep->at_status, iid, lun);
108475196Smjacob		(void) isp_target_put_atio(isp, aep);
108555373Smjacob		break;
108655373Smjacob	}
108755373Smjacob}
108855373Smjacob
108955373Smjacobstatic void
1090157943Smjacobisp_handle_ctio2(ispsoftc_t *isp, ct2_entry_t *ct)
109155373Smjacob{
1092163899Smjacob	void *xs;
109364090Smjacob	int pl = ISP_LOGTDEBUG2;
109455373Smjacob	char *fmsg = NULL;
109555373Smjacob
109674232Smjacob	if (ct->ct_syshandle) {
1097292725Smav		xs = isp_find_xs(isp, ct->ct_syshandle);
1098163899Smjacob		if (xs == NULL) {
109964090Smjacob			pl = ISP_LOGALL;
1100163899Smjacob		}
110155373Smjacob	} else {
110255373Smjacob		xs = NULL;
110355373Smjacob	}
110455373Smjacob
1105196008Smjacob	switch (ct->ct_status & ~QLTM_SVALID) {
110677365Smjacob	case CT_BUS_ERROR:
110777365Smjacob		isp_prt(isp, ISP_LOGERR, "PCI DMA Bus Error");
110877365Smjacob		/* FALL Through */
110977365Smjacob	case CT_DATA_OVER:
111077365Smjacob	case CT_DATA_UNDER:
111155373Smjacob	case CT_OK:
111255373Smjacob		/*
111355373Smjacob		 * There are generally 2 possibilities as to why we'd get
111455373Smjacob		 * this condition:
111555373Smjacob		 * 	We sent or received data.
111655373Smjacob		 * 	We sent status & command complete.
111755373Smjacob		 */
111855373Smjacob
111955373Smjacob		break;
112055373Smjacob
112155373Smjacob	case CT_BDR_MSG:
112255373Smjacob		/*
112377365Smjacob		 * Target Reset function received.
112455373Smjacob		 *
1125172568Skevlo		 * The firmware generates an async mailbox interrupt to
112655373Smjacob		 * notify us of this and returns outstanding CTIOs with this
112755373Smjacob		 * status. These CTIOs are handled in that same way as
112855373Smjacob		 * CT_ABORTED ones, so just fall through here.
112955373Smjacob		 */
1130163899Smjacob		fmsg = "TARGET RESET";
113155373Smjacob		/*FALLTHROUGH*/
113255373Smjacob	case CT_RESET:
113355373Smjacob		if (fmsg == NULL)
113477365Smjacob			fmsg = "LIP Reset";
113555373Smjacob		/*FALLTHROUGH*/
113655373Smjacob	case CT_ABORTED:
113755373Smjacob		/*
113855373Smjacob		 * When an Abort message is received the firmware goes to
113955373Smjacob		 * Bus Free and returns all outstanding CTIOs with the status
114055373Smjacob		 * set, then sends us an Immediate Notify entry.
114155373Smjacob		 */
1142163899Smjacob		if (fmsg == NULL) {
1143163899Smjacob			fmsg = "ABORT";
1144163899Smjacob		}
114555373Smjacob
1146196008Smjacob		isp_prt(isp, ISP_LOGTDEBUG0, "CTIO2 destroyed by %s: RX_ID=0x%x", fmsg, ct->ct_rxid);
114755373Smjacob		break;
114855373Smjacob
114955373Smjacob	case CT_INVAL:
115055373Smjacob		/*
115156006Smjacob		 * CTIO rejected by the firmware - invalid data direction.
115255373Smjacob		 */
1153120016Smjacob		isp_prt(isp, ISP_LOGERR, "CTIO2 had wrong data direction");
115455373Smjacob		break;
115555373Smjacob
115655373Smjacob	case CT_RSELTMO:
115777365Smjacob		fmsg = "failure to reconnect to initiator";
115855373Smjacob		/*FALLTHROUGH*/
115955373Smjacob	case CT_TIMEOUT:
116055373Smjacob		if (fmsg == NULL)
116177365Smjacob			fmsg = "command";
1162196008Smjacob		isp_prt(isp, ISP_LOGWARN, "Firmware timed out on %s", fmsg);
116355373Smjacob		break;
116455373Smjacob
116555373Smjacob	case CT_ERR:
116655373Smjacob		fmsg = "Completed with Error";
116755373Smjacob		/*FALLTHROUGH*/
116855373Smjacob	case CT_LOGOUT:
116955373Smjacob		if (fmsg == NULL)
117055373Smjacob			fmsg = "Port Logout";
117155373Smjacob		/*FALLTHROUGH*/
1172163899Smjacob	case CT_PORTUNAVAIL:
117355373Smjacob		if (fmsg == NULL)
117455373Smjacob			fmsg = "Port not available";
1175115521Sphk		/*FALLTHROUGH*/
117677365Smjacob	case CT_PORTCHANGED:
117777365Smjacob		if (fmsg == NULL)
117877365Smjacob			fmsg = "Port Changed";
1179115521Sphk		/*FALLTHROUGH*/
118055373Smjacob	case CT_NOACK:
118155373Smjacob		if (fmsg == NULL)
118255373Smjacob			fmsg = "unacknowledged Immediate Notify pending";
1183163899Smjacob		isp_prt(isp, ISP_LOGWARN, "CTIO returned by f/w- %s", fmsg);
118455373Smjacob		break;
118555373Smjacob
118655373Smjacob	case CT_INVRXID:
118755373Smjacob		/*
118855373Smjacob		 * CTIO rejected by the firmware because an invalid RX_ID.
118955373Smjacob		 * Just print a message.
119055373Smjacob		 */
1191196008Smjacob		isp_prt(isp, ISP_LOGWARN, "CTIO2 completed with Invalid RX_ID 0x%x", ct->ct_rxid);
119255373Smjacob		break;
119355373Smjacob
119455373Smjacob	default:
1195196008Smjacob		isp_prt(isp, ISP_LOGERR, "Unknown CTIO2 status 0x%x", ct->ct_status & ~QLTM_SVALID);
119655373Smjacob		break;
119755373Smjacob	}
119855373Smjacob
119955373Smjacob	if (xs == NULL) {
120055373Smjacob		/*
120155373Smjacob		 * There may be more than one CTIO for a data transfer,
120255373Smjacob		 * or this may be a status CTIO we're not monitoring.
120355373Smjacob		 *
120455373Smjacob		 * The assumption is that they'll all be returned in the
120555373Smjacob		 * order we got them.
120655373Smjacob		 */
120774232Smjacob		if (ct->ct_syshandle == 0) {
1208125187Smjacob			if ((ct->ct_flags & CT2_SENDSTATUS) == 0) {
1209196008Smjacob				isp_prt(isp, pl, "intermediate CTIO completed ok");
121055373Smjacob			} else {
1211196008Smjacob				isp_prt(isp, pl, "unmonitored CTIO completed ok");
121255373Smjacob			}
121355373Smjacob		} else {
1214196008Smjacob			isp_prt(isp, pl, "NO xs for CTIO (handle 0x%x) status 0x%x", ct->ct_syshandle, ct->ct_status & ~QLTM_SVALID);
121555373Smjacob		}
121655373Smjacob	} else {
121777365Smjacob		if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA) {
121877365Smjacob			ISP_DMAFREE(isp, xs, ct->ct_syshandle);
121977365Smjacob		}
1220125187Smjacob		if (ct->ct_flags & CT2_SENDSTATUS) {
122155373Smjacob			/*
122255373Smjacob			 * Sent status and command complete.
122355373Smjacob			 *
122455373Smjacob			 * We're now really done with this command, so we
122555373Smjacob			 * punt to the platform dependent layers because
122655373Smjacob			 * only there can we do the appropriate command
122755373Smjacob			 * complete thread synchronization.
122855373Smjacob			 */
122964090Smjacob			isp_prt(isp, pl, "status CTIO complete");
123055373Smjacob		} else {
123155373Smjacob			/*
123255373Smjacob			 * Final CTIO completed. Release DMA resources and
123355373Smjacob			 * notify platform dependent layers.
123455373Smjacob			 */
123564090Smjacob			isp_prt(isp, pl, "data CTIO complete");
123655373Smjacob		}
1237196008Smjacob		isp_async(isp, ISPASYNC_TARGET_ACTION, ct);
123855373Smjacob		/*
123955373Smjacob		 * The platform layer will destroy the handle if appropriate.
124055373Smjacob		 */
124155373Smjacob	}
124255373Smjacob}
1243163899Smjacob
1244163899Smjacobstatic void
1245163899Smjacobisp_handle_ctio7(ispsoftc_t *isp, ct7_entry_t *ct)
1246163899Smjacob{
1247163899Smjacob	void *xs;
1248163899Smjacob	int pl = ISP_LOGTDEBUG2;
1249163899Smjacob	char *fmsg = NULL;
1250163899Smjacob
1251163899Smjacob	if (ct->ct_syshandle) {
1252292725Smav		xs = isp_find_xs(isp, ct->ct_syshandle);
1253163899Smjacob		if (xs == NULL) {
1254163899Smjacob			pl = ISP_LOGALL;
1255163899Smjacob		}
1256163899Smjacob	} else {
1257163899Smjacob		xs = NULL;
1258163899Smjacob	}
1259163899Smjacob
1260196008Smjacob	switch (ct->ct_nphdl) {
1261163899Smjacob	case CT7_BUS_ERROR:
1262163899Smjacob		isp_prt(isp, ISP_LOGERR, "PCI DMA Bus Error");
1263163899Smjacob		/* FALL Through */
1264163899Smjacob	case CT7_DATA_OVER:
1265163899Smjacob	case CT7_DATA_UNDER:
1266163899Smjacob	case CT7_OK:
1267163899Smjacob		/*
1268163899Smjacob		 * There are generally 2 possibilities as to why we'd get
1269163899Smjacob		 * this condition:
1270163899Smjacob		 * 	We sent or received data.
1271163899Smjacob		 * 	We sent status & command complete.
1272163899Smjacob		 */
1273163899Smjacob
1274163899Smjacob		break;
1275163899Smjacob
1276163899Smjacob	case CT7_RESET:
1277163899Smjacob		if (fmsg == NULL) {
1278163899Smjacob			fmsg = "LIP Reset";
1279163899Smjacob		}
1280163899Smjacob		/*FALLTHROUGH*/
1281163899Smjacob	case CT7_ABORTED:
1282163899Smjacob		/*
1283163899Smjacob		 * When an Abort message is received the firmware goes to
1284163899Smjacob		 * Bus Free and returns all outstanding CTIOs with the status
1285163899Smjacob		 * set, then sends us an Immediate Notify entry.
1286163899Smjacob		 */
1287163899Smjacob		if (fmsg == NULL) {
1288163899Smjacob			fmsg = "ABORT";
1289163899Smjacob		}
1290196008Smjacob		isp_prt(isp, ISP_LOGTDEBUG0, "CTIO7 destroyed by %s: RX_ID=0x%x", fmsg, ct->ct_rxid);
1291163899Smjacob		break;
1292163899Smjacob
1293163899Smjacob	case CT7_TIMEOUT:
1294163899Smjacob		if (fmsg == NULL) {
1295163899Smjacob			fmsg = "command";
1296163899Smjacob		}
1297196008Smjacob		isp_prt(isp, ISP_LOGWARN, "Firmware timed out on %s", fmsg);
1298163899Smjacob		break;
1299163899Smjacob
1300163899Smjacob	case CT7_ERR:
1301163899Smjacob		fmsg = "Completed with Error";
1302163899Smjacob		/*FALLTHROUGH*/
1303163899Smjacob	case CT7_LOGOUT:
1304163899Smjacob		if (fmsg == NULL) {
1305163899Smjacob			fmsg = "Port Logout";
1306163899Smjacob		}
1307163899Smjacob		/*FALLTHROUGH*/
1308163899Smjacob	case CT7_PORTUNAVAIL:
1309163899Smjacob		if (fmsg == NULL) {
1310163899Smjacob			fmsg = "Port not available";
1311163899Smjacob		}
1312163899Smjacob		/*FALLTHROUGH*/
1313163899Smjacob	case CT7_PORTCHANGED:
1314163899Smjacob		if (fmsg == NULL) {
1315163899Smjacob			fmsg = "Port Changed";
1316163899Smjacob		}
1317163899Smjacob		isp_prt(isp, ISP_LOGWARN, "CTIO returned by f/w- %s", fmsg);
1318163899Smjacob		break;
1319163899Smjacob
1320163899Smjacob	case CT7_INVRXID:
1321163899Smjacob		/*
1322163899Smjacob		 * CTIO rejected by the firmware because an invalid RX_ID.
1323163899Smjacob		 * Just print a message.
1324163899Smjacob		 */
1325196008Smjacob		isp_prt(isp, ISP_LOGWARN, "CTIO7 completed with Invalid RX_ID 0x%x", ct->ct_rxid);
1326163899Smjacob		break;
1327163899Smjacob
1328163899Smjacob	case CT7_REASSY_ERR:
1329163899Smjacob		isp_prt(isp, ISP_LOGWARN, "reassembly error");
1330163899Smjacob		break;
1331163899Smjacob
1332163899Smjacob	case CT7_SRR:
1333238869Smjacob		isp_prt(isp, ISP_LOGTDEBUG0, "SRR received");
1334163899Smjacob		break;
1335163899Smjacob
1336163899Smjacob	default:
1337196008Smjacob		isp_prt(isp, ISP_LOGERR, "Unknown CTIO7 status 0x%x", ct->ct_nphdl);
1338163899Smjacob		break;
1339163899Smjacob	}
1340163899Smjacob
1341163899Smjacob	if (xs == NULL) {
1342163899Smjacob		/*
1343163899Smjacob		 * There may be more than one CTIO for a data transfer,
1344163899Smjacob		 * or this may be a status CTIO we're not monitoring.
1345163899Smjacob		 *
1346163899Smjacob		 * The assumption is that they'll all be returned in the
1347163899Smjacob		 * order we got them.
1348163899Smjacob		 */
1349163899Smjacob		if (ct->ct_syshandle == 0) {
1350163899Smjacob			if (ct->ct_flags & CT7_TERMINATE) {
1351238869Smjacob				isp_prt(isp, ISP_LOGINFO, "termination of [RX_ID 0x%x] complete", ct->ct_rxid);
1352163899Smjacob			} else if ((ct->ct_flags & CT7_SENDSTATUS) == 0) {
1353196008Smjacob				isp_prt(isp, pl, "intermediate CTIO completed ok");
1354163899Smjacob			} else {
1355196008Smjacob				isp_prt(isp, pl, "unmonitored CTIO completed ok");
1356163899Smjacob			}
1357163899Smjacob		} else {
1358196008Smjacob			isp_prt(isp, pl, "NO xs for CTIO (handle 0x%x) status 0x%x", ct->ct_syshandle, ct->ct_nphdl);
1359163899Smjacob		}
1360163899Smjacob	} else {
1361196008Smjacob		if ((ct->ct_flags & CT7_DATAMASK) != CT7_NO_DATA) {
1362163899Smjacob			ISP_DMAFREE(isp, xs, ct->ct_syshandle);
1363163899Smjacob		}
1364196008Smjacob		if (ct->ct_flags & CT7_SENDSTATUS) {
1365163899Smjacob			/*
1366163899Smjacob			 * Sent status and command complete.
1367163899Smjacob			 *
1368163899Smjacob			 * We're now really done with this command, so we
1369163899Smjacob			 * punt to the platform dependent layers because
1370163899Smjacob			 * only there can we do the appropriate command
1371163899Smjacob			 * complete thread synchronization.
1372163899Smjacob			 */
1373163899Smjacob			isp_prt(isp, pl, "status CTIO complete");
1374163899Smjacob		} else {
1375163899Smjacob			/*
1376163899Smjacob			 * Final CTIO completed. Release DMA resources and
1377163899Smjacob			 * notify platform dependent layers.
1378163899Smjacob			 */
1379163899Smjacob			isp_prt(isp, pl, "data CTIO complete");
1380163899Smjacob		}
1381196008Smjacob		isp_async(isp, ISPASYNC_TARGET_ACTION, ct);
1382163899Smjacob		/*
1383163899Smjacob		 * The platform layer will destroy the handle if appropriate.
1384163899Smjacob		 */
1385163899Smjacob	}
1386163899Smjacob}
1387196008Smjacob
1388196008Smjacobstatic void
1389196008Smjacobisp_handle_24xx_inotify(ispsoftc_t *isp, in_fcentry_24xx_t *inot_24xx)
1390196008Smjacob{
1391196008Smjacob	uint8_t ochan, chan, lochan, hichan;
1392196008Smjacob
1393196008Smjacob	/*
1394196008Smjacob	 * Check to see whether we got a wildcard channel.
1395196008Smjacob	 * If so, we have to iterate over all channels.
1396196008Smjacob	 */
1397196008Smjacob	ochan = chan = ISP_GET_VPIDX(isp, inot_24xx->in_vpidx);
1398196008Smjacob	if (chan == 0xff) {
1399196008Smjacob		lochan = 0;
1400196008Smjacob		hichan = isp->isp_nchan;
1401196008Smjacob	} else {
1402196008Smjacob		if (chan >= isp->isp_nchan) {
1403196008Smjacob			char buf[64];
1404196008Smjacob			ISP_SNPRINTF(buf, sizeof buf, "%s: bad channel %d for status 0x%x", __func__, chan, inot_24xx->in_status);
1405196008Smjacob			isp_print_bytes(isp, buf, QENTRY_LEN, inot_24xx);
1406238869Smjacob			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot_24xx);
1407196008Smjacob			return;
1408196008Smjacob		}
1409196008Smjacob		lochan = chan;
1410196008Smjacob		hichan = chan + 1;
1411196008Smjacob	}
1412196008Smjacob	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);
1413196008Smjacob	for (chan = lochan; chan < hichan; chan++) {
1414289942Smav		if (FCPARAM(isp, chan)->role == ISP_ROLE_NONE)
1415289942Smav			continue;
1416196008Smjacob		switch (inot_24xx->in_status) {
1417196008Smjacob		case IN24XX_LIP_RESET:
1418196008Smjacob		case IN24XX_LINK_RESET:
1419196008Smjacob		case IN24XX_PORT_LOGOUT:
1420196008Smjacob		case IN24XX_PORT_CHANGED:
1421196008Smjacob		case IN24XX_LINK_FAILED:
1422196008Smjacob		case IN24XX_SRR_RCVD:
1423196008Smjacob		case IN24XX_ELS_RCVD:
1424238869Smjacob			inot_24xx->in_reserved = 0;	/* clear this for later usage */
1425196008Smjacob			inot_24xx->in_vpidx = chan;
1426196008Smjacob			isp_async(isp, ISPASYNC_TARGET_ACTION, inot_24xx);
1427196008Smjacob			break;
1428196008Smjacob		default:
1429196008Smjacob			isp_prt(isp, ISP_LOGINFO, "%s: unhandled status (0x%x) for chan %d", __func__, inot_24xx->in_status, chan);
1430238869Smjacob			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot_24xx);
1431196008Smjacob			break;
1432196008Smjacob		}
1433196008Smjacob	}
1434196008Smjacob	inot_24xx->in_vpidx = ochan;
1435196008Smjacob}
143655373Smjacob#endif
1437