isp_target.c revision 291528
1160814Ssimon/*-
2280304Sjkim *  Copyright (c) 1997-2009 by Matthew Jacob
3280304Sjkim *  All rights reserved.
4280304Sjkim *
5160814Ssimon *  Redistribution and use in source and binary forms, with or without
6160814Ssimon *  modification, are permitted provided that the following conditions
7160814Ssimon *  are met:
8160814Ssimon *
9160814Ssimon *  1. Redistributions of source code must retain the above copyright
10160814Ssimon *     notice, this list of conditions and the following disclaimer.
11160814Ssimon *  2. Redistributions in binary form must reproduce the above copyright
12160814Ssimon *     notice, this list of conditions and the following disclaimer in the
13160814Ssimon *     documentation and/or other materials provided with the distribution.
14280304Sjkim *
15160814Ssimon *  THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16160814Ssimon *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17160814Ssimon *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18160814Ssimon *  ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19160814Ssimon *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20160814Ssimon *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21160814Ssimon *  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22160814Ssimon *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23160814Ssimon *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24160814Ssimon *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25160814Ssimon *  SUCH DAMAGE.
26160814Ssimon *
27160814Ssimon */
28160814Ssimon/*
29160814Ssimon * Machine and OS Independent Target Mode Code for the Qlogic SCSI/FC adapters.
30160814Ssimon */
31160814Ssimon/*
32160814Ssimon * Bug fixes gratefully acknowledged from:
33160814Ssimon *	Oded Kedem <oded@kashya.com>
34160814Ssimon */
35160814Ssimon/*
36160814Ssimon * Include header file appropriate for platform we're building on.
37160814Ssimon */
38160814Ssimon
39160814Ssimon#ifdef	__NetBSD__
40160814Ssimon#include <dev/ic/isp_netbsd.h>
41160814Ssimon#endif
42160814Ssimon#ifdef	__FreeBSD__
43160814Ssimon#include <sys/cdefs.h>
44160814Ssimon__FBSDID("$FreeBSD: stable/10/sys/dev/isp/isp_target.c 291528 2015-11-30 21:55:35Z mav $");
45160814Ssimon#include <dev/isp/isp_freebsd.h>
46160814Ssimon#endif
47160814Ssimon#ifdef	__OpenBSD__
48160814Ssimon#include <dev/ic/isp_openbsd.h>
49160814Ssimon#endif
50160814Ssimon#ifdef	__linux__
51160814Ssimon#include "isp_linux.h"
52160814Ssimon#endif
53160814Ssimon
54160814Ssimon#ifdef	ISP_TARGET_MODE
55160814Ssimonstatic const char atiocope[] = "ATIO returned for LUN %x because it was in the middle of Bus Device Reset on bus %d";
56160814Ssimonstatic const char atior[] = "ATIO returned for LUN %x from handle 0x%x because a Bus Reset occurred on bus %d";
57160814Ssimonstatic const char rqo[] = "%s: Request Queue Overflow";
58160814Ssimon
59160814Ssimonstatic void isp_got_msg_fc(ispsoftc_t *, in_fcentry_t *);
60280304Sjkimstatic void isp_got_tmf_24xx(ispsoftc_t *, at7_entry_t *);
61280304Sjkimstatic void isp_handle_atio2(ispsoftc_t *, at2_entry_t *);
62280304Sjkimstatic void isp_handle_ctio2(ispsoftc_t *, ct2_entry_t *);
63280304Sjkimstatic void isp_handle_ctio7(ispsoftc_t *, ct7_entry_t *);
64280304Sjkimstatic void isp_handle_24xx_inotify(ispsoftc_t *, in_fcentry_24xx_t *);
65280304Sjkim
66280304Sjkim/*
67160814Ssimon * The Qlogic driver gets an interrupt to look at response queue entries.
68280304Sjkim * Some of these are status completions for initiatior mode commands, but
69280304Sjkim * if target mode is enabled, we get a whole wad of response queue entries
70160814Ssimon * to be handled here.
71160814Ssimon *
72160814Ssimon * Basically the split into 3 main groups: Lun Enable/Modification responses,
73160814Ssimon * SCSI Command processing, and Immediate Notification events.
74160814Ssimon *
75160814Ssimon * You start by writing a request queue entry to enable target mode (and
76160814Ssimon * establish some resource limitations which you can modify later).
77160814Ssimon * The f/w responds with a LUN ENABLE or LUN MODIFY response with
78160814Ssimon * the status of this action. If the enable was successful, you can expect...
79160814Ssimon *
80160814Ssimon * Response queue entries with SCSI commands encapsulate show up in an ATIO
81160814Ssimon * (Accept Target IO) type- sometimes with enough info to stop the command at
82160814Ssimon * this level. Ultimately the driver has to feed back to the f/w's request
83160814Ssimon * queue a sequence of CTIOs (continue target I/O) that describe data to
84160814Ssimon * be moved and/or status to be sent) and finally finishing with sending
85160814Ssimon * to the f/w's response queue an ATIO which then completes the handshake
86160814Ssimon * with the f/w for that command. There's a lot of variations on this theme,
87160814Ssimon * including flags you can set in the CTIO for the Qlogic 2X00 fibre channel
88160814Ssimon * cards that 'auto-replenish' the f/w's ATIO count, but this is the basic
89160814Ssimon * gist of it.
90160814Ssimon *
91160814Ssimon * The third group that can show up in the response queue are Immediate
92238405Sjkim * Notification events. These include things like notifications of SCSI bus
93280304Sjkim * resets, or Bus Device Reset messages or other messages received. This
94238405Sjkim * a classic oddbins area. It can get  a little weird because you then turn
95194206Ssimon * around and acknowledge the Immediate Notify by writing an entry onto the
96160814Ssimon * request queue and then the f/w turns around and gives you an acknowledgement
97160814Ssimon * to *your* acknowledgement on the response queue (the idea being to let
98280304Sjkim * the f/w tell you when the event is *really* over I guess).
99160814Ssimon *
100280304Sjkim */
101160814Ssimon
102280304Sjkim
103280304Sjkim/*
104160814Ssimon * A new response queue entry has arrived. The interrupt service code
105160814Ssimon * has already swizzled it into the platform dependent from canonical form.
106160814Ssimon *
107160814Ssimon * Because of the way this driver is designed, unfortunately most of the
108280304Sjkim * actual synchronization work has to be done in the platform specific
109160814Ssimon * code- we have no synchroniation primitives in the common code.
110280304Sjkim */
111160814Ssimon
112280304Sjkimint
113280304Sjkimisp_target_notify(ispsoftc_t *isp, void *vptr, uint32_t *optrp)
114160814Ssimon{
115280304Sjkim	uint16_t status;
116160814Ssimon	uint32_t seqid;
117160814Ssimon	union {
118280304Sjkim		at2_entry_t	*at2iop;
119160814Ssimon		at2e_entry_t	*at2eiop;
120280304Sjkim		at7_entry_t	*at7iop;
121280304Sjkim		ct2_entry_t	*ct2iop;
122280304Sjkim		ct2e_entry_t	*ct2eiop;
123280304Sjkim		ct7_entry_t	*ct7iop;
124280304Sjkim		lun_entry_t	*lunenp;
125280304Sjkim		in_fcentry_t	*inot_fcp;
126280304Sjkim		in_fcentry_e_t	*inote_fcp;
127280304Sjkim		in_fcentry_24xx_t *inot_24xx;
128160814Ssimon		na_fcentry_t	*nack_fcp;
129280304Sjkim		na_fcentry_e_t	*nacke_fcp;
130160814Ssimon		na_fcentry_24xx_t *nack_24xx;
131280304Sjkim		isphdr_t	*hp;
132280304Sjkim		abts_t		*abts;
133280304Sjkim		abts_rsp_t	*abts_rsp;
134280304Sjkim		els_t		*els;
135280304Sjkim		void *		*vp;
136280304Sjkim#define	at2iop		unp.at2iop
137280304Sjkim#define	at2eiop		unp.at2eiop
138280304Sjkim#define	at7iop		unp.at7iop
139280304Sjkim#define	ct2iop		unp.ct2iop
140280304Sjkim#define	ct2eiop		unp.ct2eiop
141280304Sjkim#define	ct7iop		unp.ct7iop
142280304Sjkim#define	lunenp		unp.lunenp
143280304Sjkim#define	inot_fcp	unp.inot_fcp
144280304Sjkim#define	inote_fcp	unp.inote_fcp
145280304Sjkim#define	inot_24xx	unp.inot_24xx
146280304Sjkim#define	nack_fcp	unp.nack_fcp
147280304Sjkim#define	nacke_fcp	unp.nacke_fcp
148280304Sjkim#define	nack_24xx	unp.nack_24xx
149280304Sjkim#define	abts		unp.abts
150280304Sjkim#define	abts_rsp	unp.abts_rsp
151280304Sjkim#define els		unp.els
152160814Ssimon#define	hdrp		unp.hp
153160814Ssimon	} unp;
154160814Ssimon	uint8_t local[QENTRY_LEN];
155160814Ssimon	uint16_t iid;
156160814Ssimon	int bus, type, len, level, rval = 1;
157280304Sjkim	isp_notify_t notify;
158280304Sjkim
159280304Sjkim	type = isp_get_response_type(isp, (isphdr_t *)vptr);
160280304Sjkim	unp.vp = vptr;
161160814Ssimon
162280304Sjkim	ISP_TDQE(isp, "isp_target_notify", (int) *optrp, vptr);
163280304Sjkim
164280304Sjkim	switch (type) {
165280304Sjkim	case RQSTYPE_ATIO:
166280304Sjkim		isp_get_atio7(isp, at7iop, (at7_entry_t *) local);
167280304Sjkim		at7iop = (at7_entry_t *) local;
168280304Sjkim		/*
169280304Sjkim		 * Check for and do something with commands whose
170280304Sjkim		 * IULEN extends past a single queue entry.
171280304Sjkim		 */
172280304Sjkim		len = at7iop->at_ta_len & 0xfffff;
173280304Sjkim		if (len > (QENTRY_LEN - 8)) {
174280304Sjkim			len -= (QENTRY_LEN - 8);
175280304Sjkim			isp_prt(isp, ISP_LOGINFO, "long IU length (%d) ignored", len);
176280304Sjkim			while (len > 0) {
177160814Ssimon				*optrp =  ISP_NXT_QENTRY(*optrp, RESULT_QUEUE_LEN(isp));
178280304Sjkim				len -= QENTRY_LEN;
179280304Sjkim			}
180280304Sjkim		}
181280304Sjkim		/*
182280304Sjkim		 * Check for a task management function
183280304Sjkim		 */
184280304Sjkim		if (at7iop->at_cmnd.fcp_cmnd_task_management) {
185280304Sjkim			isp_got_tmf_24xx(isp, at7iop);
186160814Ssimon			break;
187280304Sjkim		}
188280304Sjkim		/*
189280304Sjkim		 * Just go straight to outer layer for this one.
190280304Sjkim		 */
191160814Ssimon		isp_async(isp, ISPASYNC_TARGET_ACTION, local);
192160814Ssimon		break;
193280304Sjkim
194280304Sjkim	case RQSTYPE_ATIO2:
195280304Sjkim		if (ISP_CAP_2KLOGIN(isp)) {
196280304Sjkim			isp_get_atio2e(isp, at2eiop, (at2e_entry_t *) local);
197280304Sjkim		} else {
198280304Sjkim			isp_get_atio2(isp, at2iop, (at2_entry_t *) local);
199280304Sjkim		}
200280304Sjkim		isp_handle_atio2(isp, (at2_entry_t *) local);
201280304Sjkim		break;
202280304Sjkim
203160814Ssimon	case RQSTYPE_CTIO3:
204160814Ssimon	case RQSTYPE_CTIO2:
205280304Sjkim		if (ISP_CAP_2KLOGIN(isp)) {
206280304Sjkim			isp_get_ctio2e(isp, ct2eiop, (ct2e_entry_t *) local);
207280304Sjkim		} else {
208280304Sjkim			isp_get_ctio2(isp, ct2iop, (ct2_entry_t *) local);
209280304Sjkim		}
210280304Sjkim		isp_handle_ctio2(isp, (ct2_entry_t *) local);
211280304Sjkim		break;
212280304Sjkim
213280304Sjkim	case RQSTYPE_CTIO7:
214160814Ssimon		isp_get_ctio7(isp, ct7iop, (ct7_entry_t *) local);
215280304Sjkim		isp_handle_ctio7(isp, (ct7_entry_t *) local);
216160814Ssimon		break;
217160814Ssimon
218280304Sjkim	case RQSTYPE_ENABLE_LUN:
219160814Ssimon	case RQSTYPE_MODIFY_LUN:
220160814Ssimon		isp_get_enable_lun(isp, lunenp, (lun_entry_t *) local);
221280304Sjkim		isp_async(isp, ISPASYNC_TARGET_ACTION, local);
222280304Sjkim		break;
223280304Sjkim
224280304Sjkim	case RQSTYPE_NOTIFY:
225160814Ssimon		bus = 0;
226160814Ssimon		if (IS_24XX(isp)) {
227160814Ssimon			isp_get_notify_24xx(isp, inot_24xx, (in_fcentry_24xx_t *)local);
228280304Sjkim			inot_24xx = (in_fcentry_24xx_t *) local;
229280304Sjkim			isp_handle_24xx_inotify(isp, inot_24xx);
230280304Sjkim			break;
231280304Sjkim		} else {
232280304Sjkim			if (ISP_CAP_2KLOGIN(isp)) {
233280304Sjkim				in_fcentry_e_t *ecp = (in_fcentry_e_t *)local;
234280304Sjkim				isp_get_notify_fc_e(isp, inote_fcp, ecp);
235280304Sjkim				iid = ecp->in_iid;
236280304Sjkim				status = ecp->in_status;
237280304Sjkim				seqid = ecp->in_seqid;
238280304Sjkim			} else {
239160814Ssimon				in_fcentry_t *fcp = (in_fcentry_t *)local;
240160814Ssimon				isp_get_notify_fc(isp, inot_fcp, fcp);
241280304Sjkim				iid = fcp->in_iid;
242280304Sjkim				status = fcp->in_status;
243280304Sjkim				seqid = fcp->in_seqid;
244160814Ssimon			}
245280304Sjkim		}
246280304Sjkim
247280304Sjkim		isp_prt(isp, ISP_LOGTDEBUG0, "Immediate Notify On Bus %d, status=0x%x seqid=0x%x", bus, status, seqid);
248160814Ssimon
249280304Sjkim		switch (status) {
250280304Sjkim		case IN_MSG_RECEIVED:
251280304Sjkim		case IN_IDE_RECEIVED:
252280304Sjkim			isp_got_msg_fc(isp, (in_fcentry_t *)local);
253280304Sjkim			break;
254280304Sjkim		case IN_RSRC_UNAVAIL:
255280304Sjkim			isp_prt(isp, ISP_LOGINFO, "Firmware out of ATIOs");
256280304Sjkim			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, local);
257280304Sjkim			break;
258280304Sjkim
259280304Sjkim		case IN_RESET:
260160814Ssimon			ISP_MEMZERO(&notify, sizeof (isp_notify_t));
261280304Sjkim			notify.nt_hba = isp;
262280304Sjkim			notify.nt_wwn = INI_ANY;
263160814Ssimon			notify.nt_tgt = TGT_ANY;
264280304Sjkim			notify.nt_nphdl = iid;
265280304Sjkim			notify.nt_sid = PORT_ANY;
266280304Sjkim			notify.nt_did = PORT_ANY;
267280304Sjkim			notify.nt_lun = LUN_ANY;
268160814Ssimon			notify.nt_tagval = TAG_ANY;
269280304Sjkim			notify.nt_tagval |= (((uint64_t)(isp->isp_serno++)) << 32);
270280304Sjkim			notify.nt_ncode = NT_BUS_RESET;
271280304Sjkim			notify.nt_need_ack = 1;
272280304Sjkim			notify.nt_lreserved = local;
273280304Sjkim			isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
274280304Sjkim			break;
275280304Sjkim
276280304Sjkim		case IN_PORT_LOGOUT:
277280304Sjkim			ISP_MEMZERO(&notify, sizeof (isp_notify_t));
278280304Sjkim			notify.nt_hba = isp;
279280304Sjkim			notify.nt_wwn = INI_ANY;
280280304Sjkim			notify.nt_nphdl = iid;
281280304Sjkim			notify.nt_sid = PORT_ANY;
282280304Sjkim			notify.nt_did = PORT_ANY;
283280304Sjkim			notify.nt_ncode = NT_LOGOUT;
284280304Sjkim			notify.nt_need_ack = 1;
285280304Sjkim			notify.nt_lreserved = local;
286280304Sjkim			isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
287280304Sjkim			break;
288280304Sjkim
289280304Sjkim		case IN_ABORT_TASK:
290280304Sjkim			ISP_MEMZERO(&notify, sizeof (isp_notify_t));
291160814Ssimon			notify.nt_hba = isp;
292160814Ssimon			notify.nt_wwn = INI_ANY;
293280304Sjkim			notify.nt_nphdl = iid;
294280304Sjkim			notify.nt_sid = PORT_ANY;
295280304Sjkim			notify.nt_did = PORT_ANY;
296280304Sjkim			notify.nt_ncode = NT_ABORT_TASK;
297280304Sjkim			notify.nt_need_ack = 1;
298280304Sjkim			notify.nt_lreserved = local;
299280304Sjkim			isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
300280304Sjkim			break;
301280304Sjkim
302280304Sjkim		case IN_GLOBAL_LOGO:
303280304Sjkim			isp_prt(isp, ISP_LOGTINFO, "%s: all ports logged out", __func__);
304280304Sjkim			ISP_MEMZERO(&notify, sizeof (isp_notify_t));
305280304Sjkim			notify.nt_hba = isp;
306280304Sjkim			notify.nt_wwn = INI_ANY;
307280304Sjkim			notify.nt_nphdl = NIL_HANDLE;
308280304Sjkim			notify.nt_sid = PORT_ANY;
309280304Sjkim			notify.nt_did = PORT_ANY;
310280304Sjkim			notify.nt_ncode = NT_GLOBAL_LOGOUT;
311280304Sjkim			notify.nt_need_ack = 1;
312280304Sjkim			notify.nt_lreserved = local;
313280304Sjkim			isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
314280304Sjkim			break;
315280304Sjkim
316280304Sjkim		case IN_PORT_CHANGED:
317160814Ssimon			isp_prt(isp, ISP_LOGTINFO, "%s: port changed", __func__);
318280304Sjkim			ISP_MEMZERO(&notify, sizeof (isp_notify_t));
319280304Sjkim			notify.nt_hba = isp;
320280304Sjkim			notify.nt_wwn = INI_ANY;
321280304Sjkim			notify.nt_nphdl = NIL_HANDLE;
322280304Sjkim			notify.nt_sid = PORT_ANY;
323280304Sjkim			notify.nt_did = PORT_ANY;
324280304Sjkim			notify.nt_ncode = NT_CHANGED;
325280304Sjkim			notify.nt_need_ack = 1;
326280304Sjkim			notify.nt_lreserved = local;
327280304Sjkim			isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
328280304Sjkim			break;
329280304Sjkim
330280304Sjkim		default:
331160814Ssimon			ISP_SNPRINTF(local, sizeof local, "%s: unknown status to RQSTYPE_NOTIFY (0x%x)", __func__, status);
332160814Ssimon			isp_print_bytes(isp, local, QENTRY_LEN, vptr);
333280304Sjkim			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, local);
334280304Sjkim			break;
335280304Sjkim		}
336280304Sjkim		break;
337280304Sjkim
338280304Sjkim	case RQSTYPE_NOTIFY_ACK:
339280304Sjkim		/*
340280304Sjkim		 * The ISP is acknowledging our acknowledgement of an
341280304Sjkim		 * Immediate Notify entry for some asynchronous event.
342280304Sjkim		 */
343280304Sjkim		if (IS_24XX(isp)) {
344280304Sjkim			isp_get_notify_ack_24xx(isp, nack_24xx, (na_fcentry_24xx_t *) local);
345280304Sjkim			nack_24xx = (na_fcentry_24xx_t *) local;
346280304Sjkim			if (nack_24xx->na_status != NA_OK) {
347280304Sjkim				level = ISP_LOGINFO;
348280304Sjkim			} else {
349280304Sjkim				level = ISP_LOGTDEBUG1;
350280304Sjkim			}
351280304Sjkim			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);
352280304Sjkim		} else {
353280304Sjkim			if (ISP_CAP_2KLOGIN(isp)) {
354280304Sjkim				isp_get_notify_ack_fc_e(isp, nacke_fcp, (na_fcentry_e_t *)local);
355280304Sjkim			} else {
356280304Sjkim				isp_get_notify_ack_fc(isp, nack_fcp, (na_fcentry_t *)local);
357280304Sjkim			}
358280304Sjkim			nack_fcp = (na_fcentry_t *)local;
359280304Sjkim			if (nack_fcp->na_status != NA_OK) {
360280304Sjkim				level = ISP_LOGINFO;
361280304Sjkim			} else {
362280304Sjkim				level = ISP_LOGTDEBUG1;
363280304Sjkim			}
364280304Sjkim			isp_prt(isp, level, "Notify Ack Status=0x%x seqid 0x%x", nack_fcp->na_status, nack_fcp->na_seqid);
365280304Sjkim		}
366280304Sjkim		break;
367280304Sjkim
368280304Sjkim	case RQSTYPE_ABTS_RCVD:
369280304Sjkim		isp_get_abts(isp, abts, (abts_t *)local);
370280304Sjkim		isp_async(isp, ISPASYNC_TARGET_ACTION, &local);
371280304Sjkim		break;
372280304Sjkim	case RQSTYPE_ABTS_RSP:
373280304Sjkim		isp_get_abts_rsp(isp, abts_rsp, (abts_rsp_t *)local);
374280304Sjkim		abts_rsp = (abts_rsp_t *) local;
375280304Sjkim		if (abts_rsp->abts_rsp_status) {
376280304Sjkim			level = ISP_LOGINFO;
377280304Sjkim		} else {
378280304Sjkim			level = ISP_LOGTDEBUG0;
379280304Sjkim		}
380280304Sjkim		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,
381280304Sjkim		    abts_rsp->abts_rsp_payload.rsp.subcode1, abts_rsp->abts_rsp_payload.rsp.subcode2);
382280304Sjkim		break;
383280304Sjkim	default:
384280304Sjkim		isp_prt(isp, ISP_LOGERR, "%s: unknown entry type 0x%x", __func__, type);
385280304Sjkim		rval = 0;
386160814Ssimon		break;
387160814Ssimon	}
388280304Sjkim#undef	atiop
389280304Sjkim#undef	at2iop
390280304Sjkim#undef	at2eiop
391280304Sjkim#undef	at7iop
392280304Sjkim#undef	ctiop
393280304Sjkim#undef	ct2iop
394280304Sjkim#undef	ct2eiop
395280304Sjkim#undef	ct7iop
396280304Sjkim#undef	lunenp
397280304Sjkim#undef	inotp
398280304Sjkim#undef	inot_fcp
399280304Sjkim#undef	inote_fcp
400280304Sjkim#undef	inot_24xx
401280304Sjkim#undef	nackp
402280304Sjkim#undef	nack_fcp
403280304Sjkim#undef	nacke_fcp
404280304Sjkim#undef	hack_24xx
405280304Sjkim#undef	abts
406280304Sjkim#undef	abts_rsp
407280304Sjkim#undef	els
408160814Ssimon#undef	hdrp
409280304Sjkim	return (rval);
410280304Sjkim}
411280304Sjkim
412280304Sjkimint
413280304Sjkimisp_target_put_entry(ispsoftc_t *isp, void *ap)
414160814Ssimon{
415280304Sjkim	void *outp;
416280304Sjkim	uint8_t etype = ((isphdr_t *) ap)->rqs_entry_type;
417280304Sjkim
418280304Sjkim	outp = isp_getrqentry(isp);
419280304Sjkim	if (outp == NULL) {
420280304Sjkim		isp_prt(isp, ISP_LOGWARN, rqo, __func__);
421280304Sjkim		return (-1);
422280304Sjkim	}
423280304Sjkim	switch (etype) {
424160814Ssimon	case RQSTYPE_ATIO2:
425280304Sjkim		if (ISP_CAP_2KLOGIN(isp)) {
426280304Sjkim			isp_put_atio2e(isp, (at2e_entry_t *) ap, (at2e_entry_t *) outp);
427280304Sjkim		} else {
428160814Ssimon			isp_put_atio2(isp, (at2_entry_t *) ap, (at2_entry_t *) outp);
429280304Sjkim		}
430280304Sjkim		break;
431280304Sjkim	case RQSTYPE_CTIO2:
432280304Sjkim		if (ISP_CAP_2KLOGIN(isp)) {
433280304Sjkim			isp_put_ctio2e(isp, (ct2e_entry_t *) ap, (ct2e_entry_t *) outp);
434280304Sjkim		} else {
435160814Ssimon			isp_put_ctio2(isp, (ct2_entry_t *) ap, (ct2_entry_t *) outp);
436280304Sjkim		}
437280304Sjkim		break;
438160814Ssimon	case RQSTYPE_CTIO7:
439280304Sjkim		isp_put_ctio7(isp, (ct7_entry_t *) ap, (ct7_entry_t *) outp);
440280304Sjkim		break;
441160814Ssimon	default:
442280304Sjkim		isp_prt(isp, ISP_LOGERR, "%s: Unknown type 0x%x", __func__, etype);
443160814Ssimon		return (-1);
444280304Sjkim	}
445280304Sjkim	ISP_TDQE(isp, __func__, isp->isp_reqidx, ap);
446280304Sjkim	ISP_SYNC_REQUEST(isp);
447280304Sjkim	return (0);
448160814Ssimon}
449280304Sjkim
450280304Sjkimint
451280304Sjkimisp_target_put_atio(ispsoftc_t *isp, void *arg)
452280304Sjkim{
453160814Ssimon	at2_entry_t *aep = arg;
454280304Sjkim	union {
455280304Sjkim		at2_entry_t _atio2;
456280304Sjkim		at2e_entry_t _atio2e;
457160814Ssimon	} atun;
458280304Sjkim
459280304Sjkim	ISP_MEMZERO(&atun, sizeof atun);
460280304Sjkim	atun._atio2.at_header.rqs_entry_type = RQSTYPE_ATIO2;
461160814Ssimon	atun._atio2.at_header.rqs_entry_count = 1;
462280304Sjkim	if (ISP_CAP_SCCFW(isp)) {
463194206Ssimon		atun._atio2.at_scclun = aep->at_scclun;
464280304Sjkim	} else {
465280304Sjkim		atun._atio2.at_lun = (uint8_t) aep->at_lun;
466280304Sjkim	}
467280304Sjkim	if (ISP_CAP_2KLOGIN(isp)) {
468280304Sjkim		atun._atio2e.at_iid = ((at2e_entry_t *)aep)->at_iid;
469194206Ssimon	} else {
470280304Sjkim		atun._atio2.at_iid = aep->at_iid;
471160814Ssimon	}
472280304Sjkim	atun._atio2.at_rxid = aep->at_rxid;
473280304Sjkim	atun._atio2.at_status = CT_OK;
474280304Sjkim	return (isp_target_put_entry(isp, &atun));
475280304Sjkim}
476280304Sjkim
477280304Sjkim/*
478280304Sjkim * Command completion- both for handling cases of no resources or
479280304Sjkim * no blackhole driver, or other cases where we have to, inline,
480160814Ssimon * finish the command sanely, or for normal command completion.
481280304Sjkim *
482194206Ssimon * The 'completion' code value has the scsi status byte in the low 8 bits.
483280304Sjkim * If status is a CHECK CONDITION and bit 8 is nonzero, then bits 12..15 have
484238405Sjkim * the sense key and  bits 16..23 have the ASCQ and bits 24..31 have the ASC
485280304Sjkim * values.
486280304Sjkim *
487280304Sjkim * NB: the key, asc, ascq, cannot be used for parallel SCSI as it doesn't
488280304Sjkim * NB: inline SCSI sense reporting. As such, we lose this information. XXX.
489280304Sjkim *
490280304Sjkim * For both parallel && fibre channel, we use the feature that does
491160814Ssimon * an automatic resource autoreplenish so we don't have then later do
492280304Sjkim * put of an atio to replenish the f/w's resource count.
493 */
494
495int
496isp_endcmd(ispsoftc_t *isp, ...)
497{
498	uint32_t code, hdl;
499	uint8_t sts;
500	union {
501		ct2_entry_t _ctio2;
502		ct2e_entry_t _ctio2e;
503		ct7_entry_t _ctio7;
504	} un;
505	va_list ap;
506
507	ISP_MEMZERO(&un, sizeof un);
508
509	if (IS_24XX(isp)) {
510		int vpidx, nphdl;
511		at7_entry_t *aep;
512		ct7_entry_t *cto = &un._ctio7;
513
514		va_start(ap, isp);
515		aep = va_arg(ap, at7_entry_t *);
516		nphdl = va_arg(ap, int);
517		/*
518		 * Note that vpidx may equal 0xff (unknown) here
519		 */
520		vpidx = va_arg(ap, int);
521		code = va_arg(ap, uint32_t);
522		hdl = va_arg(ap, uint32_t);
523		va_end(ap);
524		isp_prt(isp, ISP_LOGTDEBUG0, "%s: [RX_ID 0x%x] chan %d code %x", __func__, aep->at_rxid, vpidx, code);
525
526		sts = code & 0xff;
527		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
528		cto->ct_header.rqs_entry_count = 1;
529		cto->ct_nphdl = nphdl;
530		cto->ct_rxid = aep->at_rxid;
531		cto->ct_iid_lo = (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
532		cto->ct_iid_hi = aep->at_hdr.s_id[0];
533		cto->ct_oxid = aep->at_hdr.ox_id;
534		cto->ct_scsi_status = sts;
535		cto->ct_vpidx = vpidx;
536		cto->ct_flags = CT7_NOACK;
537		if (code & ECMD_TERMINATE) {
538			cto->ct_flags |= CT7_TERMINATE;
539		} else if (code & ECMD_SVALID) {
540			cto->ct_flags |= CT7_FLAG_MODE1 | CT7_SENDSTATUS;
541			cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8);
542			cto->rsp.m1.ct_resplen = cto->ct_senselen = min(16, MAXRESPLEN_24XX);
543			ISP_MEMZERO(cto->rsp.m1.ct_resp, sizeof (cto->rsp.m1.ct_resp));
544			cto->rsp.m1.ct_resp[0] = 0xf0;
545			cto->rsp.m1.ct_resp[2] = (code >> 12) & 0xf;
546			cto->rsp.m1.ct_resp[7] = 8;
547			cto->rsp.m1.ct_resp[12] = (code >> 16) & 0xff;
548			cto->rsp.m1.ct_resp[13] = (code >> 24) & 0xff;
549		} else {
550			cto->ct_flags |= CT7_FLAG_MODE1 | CT7_SENDSTATUS;
551		}
552		if (aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl) {
553			cto->ct_resid = aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl;
554			if (cto->ct_resid < 0) {
555				 cto->ct_scsi_status |= (FCP_RESID_OVERFLOW << 8);
556			} else if (cto->ct_resid > 0) {
557				 cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8);
558			}
559		}
560		cto->ct_syshandle = hdl;
561	} else {
562		at2_entry_t *aep;
563		ct2_entry_t *cto = &un._ctio2;
564
565		va_start(ap, isp);
566		aep = va_arg(ap, at2_entry_t *);
567		code = va_arg(ap, uint32_t);
568		hdl = va_arg(ap, uint32_t);
569		va_end(ap);
570
571		isp_prt(isp, ISP_LOGTDEBUG0, "%s: [RX_ID 0x%x] code %x", __func__, aep->at_rxid, code);
572
573		sts = code & 0xff;
574		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
575		cto->ct_header.rqs_entry_count = 1;
576		if (ISP_CAP_SCCFW(isp) == 0) {
577			cto->ct_lun = aep->at_lun;
578		}
579		if (ISP_CAP_2KLOGIN(isp)) {
580			un._ctio2e.ct_iid = ((at2e_entry_t *)aep)->at_iid;
581		} else {
582			cto->ct_iid = aep->at_iid;
583		}
584		cto->ct_rxid = aep->at_rxid;
585		cto->rsp.m1.ct_scsi_status = sts;
586		cto->ct_flags = CT2_SENDSTATUS | CT2_NO_DATA | CT2_FLAG_MODE1;
587		if (hdl == 0) {
588			cto->ct_flags |= CT2_CCINCR;
589		}
590		if (aep->at_datalen) {
591			cto->ct_resid = aep->at_datalen;
592			cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER;
593		}
594		if (sts == SCSI_CHECK && (code & ECMD_SVALID)) {
595			cto->rsp.m1.ct_resp[0] = 0xf0;
596			cto->rsp.m1.ct_resp[2] = (code >> 12) & 0xf;
597			cto->rsp.m1.ct_resp[7] = 8;
598			cto->rsp.m1.ct_resp[12] = (code >> 24) & 0xff;
599			cto->rsp.m1.ct_resp[13] = (code >> 16) & 0xff;
600			cto->rsp.m1.ct_senselen = 16;
601			cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
602		}
603		cto->ct_syshandle = hdl;
604	}
605	return (isp_target_put_entry(isp, &un));
606}
607
608/*
609 * These are either broadcast events or specifically CTIO fast completion
610 */
611
612int
613isp_target_async(ispsoftc_t *isp, int bus, int event)
614{
615	isp_notify_t notify;
616
617	ISP_MEMZERO(&notify, sizeof (isp_notify_t));
618	notify.nt_hba = isp;
619	notify.nt_wwn = INI_ANY;
620	notify.nt_nphdl = NIL_HANDLE;
621	notify.nt_sid = PORT_ANY;
622	notify.nt_did = PORT_ANY;
623	notify.nt_tgt = TGT_ANY;
624	notify.nt_channel = bus;
625	notify.nt_lun = LUN_ANY;
626	notify.nt_tagval = TAG_ANY;
627	notify.nt_tagval |= (((uint64_t)(isp->isp_serno++)) << 32);
628
629	switch (event) {
630	case ASYNC_LOOP_UP:
631	case ASYNC_PTPMODE:
632		isp_prt(isp, ISP_LOGTDEBUG0, "%s: LOOP UP", __func__);
633		notify.nt_ncode = NT_LINK_UP;
634		isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
635		break;
636	case ASYNC_LOOP_DOWN:
637		isp_prt(isp, ISP_LOGTDEBUG0, "%s: LOOP DOWN", __func__);
638		notify.nt_ncode = NT_LINK_DOWN;
639		isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
640		break;
641	case ASYNC_LIP_ERROR:
642	case ASYNC_LIP_F8:
643	case ASYNC_LIP_OCCURRED:
644	case ASYNC_LOOP_RESET:
645		isp_prt(isp, ISP_LOGTDEBUG0, "%s: LIP RESET", __func__);
646		notify.nt_ncode = NT_LIP_RESET;
647		isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
648		break;
649	case ASYNC_BUS_RESET:
650	case ASYNC_TIMEOUT_RESET:	/* XXX: where does this come from ? */
651		isp_prt(isp, ISP_LOGTDEBUG0, "%s: BUS RESET", __func__);
652		notify.nt_ncode = NT_BUS_RESET;
653		isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
654		break;
655	case ASYNC_DEVICE_RESET:
656		isp_prt(isp, ISP_LOGTDEBUG0, "%s: DEVICE RESET", __func__);
657		notify.nt_ncode = NT_TARGET_RESET;
658		isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
659		break;
660	case ASYNC_CTIO_DONE:
661	{
662		uint8_t storage[QENTRY_LEN];
663		isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO DONE", __func__);
664		memset(storage, 0, QENTRY_LEN);
665		if (IS_24XX(isp)) {
666			ct7_entry_t *ct = (ct7_entry_t *) storage;
667			ct->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
668			ct->ct_nphdl = CT7_OK;
669			ct->ct_syshandle = bus;
670			ct->ct_flags = CT7_SENDSTATUS;
671		} else {
672            		/* This should also suffice for 2K login code */
673			ct2_entry_t *ct = (ct2_entry_t *) storage;
674			ct->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
675			ct->ct_status = CT_OK;
676			ct->ct_syshandle = bus;
677			ct->ct_flags = CT2_SENDSTATUS|CT2_FASTPOST;
678		}
679		isp_async(isp, ISPASYNC_TARGET_ACTION, storage);
680		break;
681	}
682	default:
683		isp_prt(isp, ISP_LOGERR, "%s: unknown event 0x%x", __func__, event);
684		if (isp->isp_state == ISP_RUNSTATE) {
685			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, NULL);
686		}
687		break;
688	}
689	return (0);
690}
691
692/*
693 * Synthesize a message from the task management flags in a FCP_CMND_IU.
694 */
695static void
696isp_got_msg_fc(ispsoftc_t *isp, in_fcentry_t *inp)
697{
698	isp_notify_t notify;
699	static const char f1[] = "%s from N-port handle 0x%x lun %x seq 0x%x";
700	static const char f2[] = "unknown %s 0x%x lun %x N-Port handle 0x%x task flags 0x%x seq 0x%x\n";
701	uint16_t seqid, nphdl;
702
703	ISP_MEMZERO(&notify, sizeof (isp_notify_t));
704	notify.nt_hba = isp;
705	notify.nt_wwn = INI_ANY;
706	if (ISP_CAP_2KLOGIN(isp)) {
707		notify.nt_nphdl = ((in_fcentry_e_t *)inp)->in_iid;
708		nphdl = ((in_fcentry_e_t *)inp)->in_iid;
709		seqid = ((in_fcentry_e_t *)inp)->in_seqid;
710	} else {
711		notify.nt_nphdl = inp->in_iid;
712		nphdl = inp->in_iid;
713		seqid = inp->in_seqid;
714	}
715	notify.nt_sid = PORT_ANY;
716	notify.nt_did = PORT_ANY;
717
718	/* nt_tgt set in outer layers */
719	if (ISP_CAP_SCCFW(isp)) {
720		notify.nt_lun = inp->in_scclun;
721#if __FreeBSD_version < 1000700
722		notify.nt_lun &= 0x3fff;
723#endif
724	} else {
725		notify.nt_lun = inp->in_lun;
726	}
727	notify.nt_tagval = seqid;
728	notify.nt_tagval |= (((uint64_t)(isp->isp_serno++)) << 32);
729	notify.nt_need_ack = 1;
730	notify.nt_lreserved = inp;
731
732	if (inp->in_status != IN_MSG_RECEIVED) {
733		isp_prt(isp, ISP_LOGINFO, f2, "immediate notify status", inp->in_status, notify.nt_lun, nphdl, inp->in_task_flags, inp->in_seqid);
734		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inp);
735		return;
736	}
737
738	if (inp->in_task_flags & TASK_FLAGS_ABORT_TASK_SET) {
739		isp_prt(isp, ISP_LOGINFO, f1, "ABORT TASK SET", nphdl, notify.nt_lun, inp->in_seqid);
740		notify.nt_ncode = NT_ABORT_TASK_SET;
741	} else if (inp->in_task_flags & TASK_FLAGS_CLEAR_TASK_SET) {
742		isp_prt(isp, ISP_LOGINFO, f1, "CLEAR TASK SET", nphdl, notify.nt_lun, inp->in_seqid);
743		notify.nt_ncode = NT_CLEAR_TASK_SET;
744	} else if (inp->in_task_flags & TASK_FLAGS_LUN_RESET) {
745		isp_prt(isp, ISP_LOGINFO, f1, "LUN RESET", nphdl, notify.nt_lun, inp->in_seqid);
746		notify.nt_ncode = NT_LUN_RESET;
747	} else if (inp->in_task_flags & TASK_FLAGS_TARGET_RESET) {
748		isp_prt(isp, ISP_LOGINFO, f1, "TARGET RESET", nphdl, notify.nt_lun, inp->in_seqid);
749		notify.nt_ncode = NT_TARGET_RESET;
750	} else if (inp->in_task_flags & TASK_FLAGS_CLEAR_ACA) {
751		isp_prt(isp, ISP_LOGINFO, f1, "CLEAR ACA", nphdl, notify.nt_lun, inp->in_seqid);
752		notify.nt_ncode = NT_CLEAR_ACA;
753	} else {
754		isp_prt(isp, ISP_LOGWARN, f2, "task flag", inp->in_status, notify.nt_lun, nphdl, inp->in_task_flags,  inp->in_seqid);
755		isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inp);
756		return;
757	}
758	isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
759}
760
761static void
762isp_got_tmf_24xx(ispsoftc_t *isp, at7_entry_t *aep)
763{
764	isp_notify_t notify;
765	static const char f1[] = "%s from PortID 0x%06x lun %x seq 0x%08x";
766	static const char f2[] = "unknown Task Flag 0x%x lun %x PortID 0x%x tag 0x%08x";
767	uint16_t chan;
768	uint32_t sid, did;
769
770	ISP_MEMZERO(&notify, sizeof (isp_notify_t));
771	notify.nt_hba = isp;
772	notify.nt_wwn = INI_ANY;
773	notify.nt_lun = (aep->at_cmnd.fcp_cmnd_lun[0] << 8) | (aep->at_cmnd.fcp_cmnd_lun[1]);
774	notify.nt_tagval = aep->at_rxid;
775	notify.nt_tagval |= (((uint64_t)(isp->isp_serno++)) << 32);
776	notify.nt_lreserved = aep;
777	sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] <<  8) | (aep->at_hdr.s_id[2]);
778
779	/* Channel has to derived from D_ID */
780	did = (aep->at_hdr.d_id[0] << 16) | (aep->at_hdr.d_id[1] << 8) | aep->at_hdr.d_id[2];
781	for (chan = 0; chan < isp->isp_nchan; chan++) {
782		if (FCPARAM(isp, chan)->isp_portid == did) {
783			break;
784		}
785	}
786	if (chan == isp->isp_nchan) {
787		isp_prt(isp, ISP_LOGWARN, "%s: D_ID 0x%x not found on any channel", __func__, did);
788		/* just drop on the floor */
789		return;
790	}
791	notify.nt_nphdl = NIL_HANDLE; /* unknown here */
792	notify.nt_sid = sid;
793	notify.nt_did = did;
794	notify.nt_channel = chan;
795	if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_QUERY_TASK_SET) {
796		isp_prt(isp, ISP_LOGINFO, f1, "QUERY TASK SET", sid, notify.nt_lun, aep->at_rxid);
797		notify.nt_ncode = NT_QUERY_TASK_SET;
798	} else if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_ABORT_TASK_SET) {
799		isp_prt(isp, ISP_LOGINFO, f1, "ABORT TASK SET", sid, notify.nt_lun, aep->at_rxid);
800		notify.nt_ncode = NT_ABORT_TASK_SET;
801	} else if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_CLEAR_TASK_SET) {
802		isp_prt(isp, ISP_LOGINFO, f1, "CLEAR TASK SET", sid, notify.nt_lun, aep->at_rxid);
803		notify.nt_ncode = NT_CLEAR_TASK_SET;
804	} else if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_QUERY_ASYNC_EVENT) {
805		isp_prt(isp, ISP_LOGINFO, f1, "QUERY ASYNC EVENT", sid, notify.nt_lun, aep->at_rxid);
806		notify.nt_ncode = NT_QUERY_ASYNC_EVENT;
807	} else if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_LUN_RESET) {
808		isp_prt(isp, ISP_LOGINFO, f1, "LUN RESET", sid, notify.nt_lun, aep->at_rxid);
809		notify.nt_ncode = NT_LUN_RESET;
810	} else if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_TGT_RESET) {
811		isp_prt(isp, ISP_LOGINFO, f1, "TARGET RESET", sid, notify.nt_lun, aep->at_rxid);
812		notify.nt_ncode = NT_TARGET_RESET;
813	} else if (aep->at_cmnd.fcp_cmnd_task_management & FCP_CMND_TMF_CLEAR_ACA) {
814		isp_prt(isp, ISP_LOGINFO, f1, "CLEAR ACA", sid, notify.nt_lun, aep->at_rxid);
815		notify.nt_ncode = NT_CLEAR_ACA;
816	} else {
817		isp_prt(isp, ISP_LOGWARN, f2, aep->at_cmnd.fcp_cmnd_task_management, notify.nt_lun, sid, aep->at_rxid);
818		notify.nt_ncode = NT_UNKNOWN;
819		return;
820	}
821	isp_async(isp, ISPASYNC_TARGET_NOTIFY, &notify);
822}
823
824int
825isp_notify_ack(ispsoftc_t *isp, void *arg)
826{
827	char storage[QENTRY_LEN];
828	void *outp;
829
830	/*
831	 * This is in case a Task Management Function ends up here.
832	 */
833	if (IS_24XX(isp) && arg != NULL && (((isphdr_t *)arg)->rqs_entry_type == RQSTYPE_ATIO)) {
834		at7_entry_t *aep = arg;
835		return (isp_endcmd(isp, aep, NIL_HANDLE, 0, 0, 0));
836	}
837
838	outp = isp_getrqentry(isp);
839	if (outp == NULL) {
840		isp_prt(isp, ISP_LOGWARN, rqo, __func__);
841		return (1);
842	}
843
844	ISP_MEMZERO(storage, QENTRY_LEN);
845
846	if (IS_24XX(isp)) {
847		na_fcentry_24xx_t *na = (na_fcentry_24xx_t *) storage;
848		na->na_header.rqs_entry_type = RQSTYPE_NOTIFY_ACK;
849		na->na_header.rqs_entry_count = 1;
850		if (arg) {
851			in_fcentry_24xx_t *in = arg;
852			na->na_nphdl = in->in_nphdl;
853			na->na_flags = in->in_flags;
854			na->na_status = in->in_status;
855			na->na_status_subcode = in->in_status_subcode;
856			na->na_fwhandle = in->in_fwhandle;
857			na->na_rxid = in->in_rxid;
858			na->na_oxid = in->in_oxid;
859			na->na_vpidx = in->in_vpidx;
860			if (in->in_status == IN24XX_SRR_RCVD) {
861				na->na_srr_rxid = in->in_srr_rxid;
862				na->na_srr_reloff_hi = in->in_srr_reloff_hi;
863				na->na_srr_reloff_lo = in->in_srr_reloff_lo;
864				na->na_srr_iu = in->in_srr_iu;
865				/*
866				 * Whether we're accepting the SRR or rejecting
867				 * it is determined by looking at the in_reserved
868				 * field in the original notify structure.
869				 */
870				if (in->in_reserved) {
871					na->na_srr_flags = 1;
872					na->na_srr_reject_vunique = 0;
873					na->na_srr_reject_code = 9;		/* unable to perform this command at this time */
874					na->na_srr_reject_explanation = 0x2a;	/* unable to supply the requested data */
875				}
876			}
877		}
878		isp_put_notify_24xx_ack(isp, na, (na_fcentry_24xx_t *)outp);
879	} else {
880		na_fcentry_t *na = (na_fcentry_t *) storage;
881		int iid = 0;
882
883		if (arg) {
884			in_fcentry_t *inp = arg;
885			ISP_MEMCPY(storage, arg, sizeof (isphdr_t));
886			if (ISP_CAP_2KLOGIN(isp)) {
887				((na_fcentry_e_t *)na)->na_iid = ((in_fcentry_e_t *)inp)->in_iid;
888				iid = ((na_fcentry_e_t *)na)->na_iid;
889			} else {
890				na->na_iid = inp->in_iid;
891				iid = na->na_iid;
892			}
893			na->na_task_flags = inp->in_task_flags & TASK_FLAGS_RESERVED_MASK;
894			na->na_seqid = inp->in_seqid;
895			na->na_status = inp->in_status;
896			na->na_flags = NAFC_RCOUNT;
897			if (inp->in_status == IN_RESET) {
898				na->na_flags = NAFC_RST_CLRD;	/* We do not modify resource counts for LIP resets */
899			}
900			if (inp->in_status == IN_MSG_RECEIVED) {
901				na->na_flags |= NAFC_TVALID;
902				na->na_response = 0;	/* XXX SUCCEEDED XXX */
903			}
904		} else {
905			na->na_flags = NAFC_RST_CLRD;
906		}
907		na->na_header.rqs_entry_type = RQSTYPE_NOTIFY_ACK;
908		na->na_header.rqs_entry_count = 1;
909		if (ISP_CAP_2KLOGIN(isp)) {
910			isp_put_notify_ack_fc_e(isp, (na_fcentry_e_t *) na, (na_fcentry_e_t *)outp);
911		} else {
912			isp_put_notify_ack_fc(isp, na, (na_fcentry_t *)outp);
913		}
914		isp_prt(isp, ISP_LOGTDEBUG0, "notify ack handle %x seqid %x flags %x tflags %x response %x", iid, na->na_seqid,
915		    na->na_flags, na->na_task_flags, na->na_response);
916	}
917	ISP_TDQE(isp, "isp_notify_ack", isp->isp_reqidx, storage);
918	ISP_SYNC_REQUEST(isp);
919	return (0);
920}
921
922int
923isp_acknak_abts(ispsoftc_t *isp, void *arg, int errno)
924{
925	char storage[QENTRY_LEN];
926	uint16_t tmpw;
927	uint8_t tmpb;
928	abts_t *abts = arg;
929	abts_rsp_t *rsp = (abts_rsp_t *) storage;
930	void *outp;
931
932	if (!IS_24XX(isp)) {
933		isp_prt(isp, ISP_LOGERR, "%s: called for non-24XX card", __func__);
934		return (0);
935	}
936
937	if (abts->abts_header.rqs_entry_type != RQSTYPE_ABTS_RCVD) {
938		isp_prt(isp, ISP_LOGERR, "%s: called for non-ABTS entry (0x%x)", __func__, abts->abts_header.rqs_entry_type);
939		return (0);
940	}
941
942	outp = isp_getrqentry(isp);
943	if (outp == NULL) {
944		isp_prt(isp, ISP_LOGWARN, rqo, __func__);
945		return (1);
946	}
947
948	ISP_MEMCPY(rsp, abts, QENTRY_LEN);
949	rsp->abts_rsp_header.rqs_entry_type = RQSTYPE_ABTS_RSP;
950
951	/*
952	 * Swap destination and source for response.
953	 */
954	rsp->abts_rsp_r_ctl = BA_ACC;
955	tmpw = rsp->abts_rsp_did_lo;
956	tmpb = rsp->abts_rsp_did_hi;
957	rsp->abts_rsp_did_lo = rsp->abts_rsp_sid_lo;
958	rsp->abts_rsp_did_hi = rsp->abts_rsp_sid_hi;
959	rsp->abts_rsp_sid_lo = tmpw;
960	rsp->abts_rsp_sid_hi = tmpb;
961
962	rsp->abts_rsp_f_ctl_hi ^= 0x80; 	/* invert Exchange Context */
963	rsp->abts_rsp_f_ctl_hi &= ~0x7f;	/* clear Sequence Initiator and other bits */
964	rsp->abts_rsp_f_ctl_hi |= 0x10;		/* abort the whole exchange */
965	rsp->abts_rsp_f_ctl_hi |= 0x8;		/* last data frame of sequence */
966	rsp->abts_rsp_f_ctl_hi |= 0x1;		/* transfer Sequence Initiative */
967	rsp->abts_rsp_f_ctl_lo = 0;
968
969	if (errno == 0) {
970		uint16_t rx_id, ox_id;
971
972		rx_id = rsp->abts_rsp_rx_id;
973		ox_id = rsp->abts_rsp_ox_id;
974		ISP_MEMZERO(&rsp->abts_rsp_payload.ba_acc, sizeof (rsp->abts_rsp_payload.ba_acc));
975                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);
976                rsp->abts_rsp_payload.ba_acc.aborted_rx_id = rx_id;
977                rsp->abts_rsp_payload.ba_acc.aborted_ox_id = ox_id;
978                rsp->abts_rsp_payload.ba_acc.high_seq_cnt = 0xffff;
979	} else {
980		ISP_MEMZERO(&rsp->abts_rsp_payload.ba_rjt, sizeof (rsp->abts_rsp_payload.ba_acc));
981		switch (errno) {
982		case ENOMEM:
983			rsp->abts_rsp_payload.ba_rjt.reason = 5;	/* Logical Unit Busy */
984			break;
985		default:
986			rsp->abts_rsp_payload.ba_rjt.reason = 9;	/* Unable to perform command request */
987			break;
988		}
989	}
990
991	/*
992	 * The caller will have set response values as appropriate
993	 * in the ABTS structure just before calling us.
994	 */
995	isp_put_abts_rsp(isp, rsp, (abts_rsp_t *)outp);
996	ISP_TDQE(isp, "isp_acknak_abts", isp->isp_reqidx, storage);
997	ISP_SYNC_REQUEST(isp);
998	return (0);
999}
1000
1001static void
1002isp_handle_atio2(ispsoftc_t *isp, at2_entry_t *aep)
1003{
1004	int lun, iid;
1005
1006	if (ISP_CAP_SCCFW(isp)) {
1007		lun = aep->at_scclun;
1008#if __FreeBSD_version < 1000700
1009		lun &= 0x3fff;
1010#endif
1011	} else {
1012		lun = aep->at_lun;
1013	}
1014
1015	if (ISP_CAP_2KLOGIN(isp)) {
1016		iid = ((at2e_entry_t *)aep)->at_iid;
1017	} else {
1018		iid = aep->at_iid;
1019	}
1020
1021	/*
1022	 * The firmware status (except for the QLTM_SVALID bit) indicates
1023	 * why this ATIO was sent to us.
1024	 *
1025	 * If QLTM_SVALID is set, the firware has recommended Sense Data.
1026	 *
1027	 * If the DISCONNECTS DISABLED bit is set in the flags field,
1028	 * we're still connected on the SCSI bus - i.e. the initiator
1029	 * did not set DiscPriv in the identify message. We don't care
1030	 * about this so it's ignored.
1031	 */
1032
1033	switch (aep->at_status & ~QLTM_SVALID) {
1034	case AT_PATH_INVALID:
1035		/*
1036		 * ATIO rejected by the firmware due to disabled lun.
1037		 */
1038		isp_prt(isp, ISP_LOGERR, "rejected ATIO2 for disabled lun %x", lun);
1039		break;
1040	case AT_NOCAP:
1041		/*
1042		 * Requested Capability not available
1043		 * We sent an ATIO that overflowed the firmware's
1044		 * command resource count.
1045		 */
1046		isp_prt(isp, ISP_LOGERR, "rejected ATIO2 for lun %x- command count overflow", lun);
1047		break;
1048
1049	case AT_BDR_MSG:
1050		/*
1051		 * If we send an ATIO to the firmware to increment
1052		 * its command resource count, and the firmware is
1053		 * recovering from a Bus Device Reset, it returns
1054		 * the ATIO with this status. We set the command
1055		 * resource count in the Enable Lun entry and no
1056		 * not increment it. Therefore we should never get
1057		 * this status here.
1058		 */
1059		isp_prt(isp, ISP_LOGERR, atiocope, lun, 0);
1060		break;
1061
1062	case AT_CDB:		/* Got a CDB */
1063		/*
1064		 * Punt to platform specific layer.
1065		 */
1066		isp_async(isp, ISPASYNC_TARGET_ACTION, aep);
1067		break;
1068
1069	case AT_RESET:
1070		/*
1071		 * A bus reset came along an blew away this command. Why
1072		 * they do this in addition the async event code stuff,
1073		 * I dunno.
1074		 *
1075		 * Ignore it because the async event will clear things
1076		 * up for us.
1077		 */
1078		isp_prt(isp, ISP_LOGERR, atior, lun, iid, 0);
1079		break;
1080
1081
1082	default:
1083		isp_prt(isp, ISP_LOGERR, "Unknown ATIO2 status 0x%x from handle %d for lun %x", aep->at_status, iid, lun);
1084		(void) isp_target_put_atio(isp, aep);
1085		break;
1086	}
1087}
1088
1089static void
1090isp_handle_ctio2(ispsoftc_t *isp, ct2_entry_t *ct)
1091{
1092	void *xs;
1093	int pl = ISP_LOGTDEBUG2;
1094	char *fmsg = NULL;
1095
1096	if (ct->ct_syshandle) {
1097		xs = isp_find_xs_tgt(isp, ct->ct_syshandle);
1098		if (xs == NULL) {
1099			pl = ISP_LOGALL;
1100		}
1101	} else {
1102		xs = NULL;
1103	}
1104
1105	switch (ct->ct_status & ~QLTM_SVALID) {
1106	case CT_BUS_ERROR:
1107		isp_prt(isp, ISP_LOGERR, "PCI DMA Bus Error");
1108		/* FALL Through */
1109	case CT_DATA_OVER:
1110	case CT_DATA_UNDER:
1111	case CT_OK:
1112		/*
1113		 * There are generally 2 possibilities as to why we'd get
1114		 * this condition:
1115		 * 	We sent or received data.
1116		 * 	We sent status & command complete.
1117		 */
1118
1119		break;
1120
1121	case CT_BDR_MSG:
1122		/*
1123		 * Target Reset function received.
1124		 *
1125		 * The firmware generates an async mailbox interrupt to
1126		 * notify us of this and returns outstanding CTIOs with this
1127		 * status. These CTIOs are handled in that same way as
1128		 * CT_ABORTED ones, so just fall through here.
1129		 */
1130		fmsg = "TARGET RESET";
1131		/*FALLTHROUGH*/
1132	case CT_RESET:
1133		if (fmsg == NULL)
1134			fmsg = "LIP Reset";
1135		/*FALLTHROUGH*/
1136	case CT_ABORTED:
1137		/*
1138		 * When an Abort message is received the firmware goes to
1139		 * Bus Free and returns all outstanding CTIOs with the status
1140		 * set, then sends us an Immediate Notify entry.
1141		 */
1142		if (fmsg == NULL) {
1143			fmsg = "ABORT";
1144		}
1145
1146		isp_prt(isp, ISP_LOGTDEBUG0, "CTIO2 destroyed by %s: RX_ID=0x%x", fmsg, ct->ct_rxid);
1147		break;
1148
1149	case CT_INVAL:
1150		/*
1151		 * CTIO rejected by the firmware - invalid data direction.
1152		 */
1153		isp_prt(isp, ISP_LOGERR, "CTIO2 had wrong data direction");
1154		break;
1155
1156	case CT_RSELTMO:
1157		fmsg = "failure to reconnect to initiator";
1158		/*FALLTHROUGH*/
1159	case CT_TIMEOUT:
1160		if (fmsg == NULL)
1161			fmsg = "command";
1162		isp_prt(isp, ISP_LOGWARN, "Firmware timed out on %s", fmsg);
1163		break;
1164
1165	case CT_ERR:
1166		fmsg = "Completed with Error";
1167		/*FALLTHROUGH*/
1168	case CT_LOGOUT:
1169		if (fmsg == NULL)
1170			fmsg = "Port Logout";
1171		/*FALLTHROUGH*/
1172	case CT_PORTUNAVAIL:
1173		if (fmsg == NULL)
1174			fmsg = "Port not available";
1175		/*FALLTHROUGH*/
1176	case CT_PORTCHANGED:
1177		if (fmsg == NULL)
1178			fmsg = "Port Changed";
1179		/*FALLTHROUGH*/
1180	case CT_NOACK:
1181		if (fmsg == NULL)
1182			fmsg = "unacknowledged Immediate Notify pending";
1183		isp_prt(isp, ISP_LOGWARN, "CTIO returned by f/w- %s", fmsg);
1184		break;
1185
1186	case CT_INVRXID:
1187		/*
1188		 * CTIO rejected by the firmware because an invalid RX_ID.
1189		 * Just print a message.
1190		 */
1191		isp_prt(isp, ISP_LOGWARN, "CTIO2 completed with Invalid RX_ID 0x%x", ct->ct_rxid);
1192		break;
1193
1194	default:
1195		isp_prt(isp, ISP_LOGERR, "Unknown CTIO2 status 0x%x", ct->ct_status & ~QLTM_SVALID);
1196		break;
1197	}
1198
1199	if (xs == NULL) {
1200		/*
1201		 * There may be more than one CTIO for a data transfer,
1202		 * or this may be a status CTIO we're not monitoring.
1203		 *
1204		 * The assumption is that they'll all be returned in the
1205		 * order we got them.
1206		 */
1207		if (ct->ct_syshandle == 0) {
1208			if ((ct->ct_flags & CT2_SENDSTATUS) == 0) {
1209				isp_prt(isp, pl, "intermediate CTIO completed ok");
1210			} else {
1211				isp_prt(isp, pl, "unmonitored CTIO completed ok");
1212			}
1213		} else {
1214			isp_prt(isp, pl, "NO xs for CTIO (handle 0x%x) status 0x%x", ct->ct_syshandle, ct->ct_status & ~QLTM_SVALID);
1215		}
1216	} else {
1217		if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA) {
1218			ISP_DMAFREE(isp, xs, ct->ct_syshandle);
1219		}
1220		if (ct->ct_flags & CT2_SENDSTATUS) {
1221			/*
1222			 * Sent status and command complete.
1223			 *
1224			 * We're now really done with this command, so we
1225			 * punt to the platform dependent layers because
1226			 * only there can we do the appropriate command
1227			 * complete thread synchronization.
1228			 */
1229			isp_prt(isp, pl, "status CTIO complete");
1230		} else {
1231			/*
1232			 * Final CTIO completed. Release DMA resources and
1233			 * notify platform dependent layers.
1234			 */
1235			isp_prt(isp, pl, "data CTIO complete");
1236		}
1237		isp_async(isp, ISPASYNC_TARGET_ACTION, ct);
1238		/*
1239		 * The platform layer will destroy the handle if appropriate.
1240		 */
1241	}
1242}
1243
1244static void
1245isp_handle_ctio7(ispsoftc_t *isp, ct7_entry_t *ct)
1246{
1247	void *xs;
1248	int pl = ISP_LOGTDEBUG2;
1249	char *fmsg = NULL;
1250
1251	if (ct->ct_syshandle) {
1252		xs = isp_find_xs_tgt(isp, ct->ct_syshandle);
1253		if (xs == NULL) {
1254			pl = ISP_LOGALL;
1255		}
1256	} else {
1257		xs = NULL;
1258	}
1259
1260	switch (ct->ct_nphdl) {
1261	case CT7_BUS_ERROR:
1262		isp_prt(isp, ISP_LOGERR, "PCI DMA Bus Error");
1263		/* FALL Through */
1264	case CT7_DATA_OVER:
1265	case CT7_DATA_UNDER:
1266	case CT7_OK:
1267		/*
1268		 * There are generally 2 possibilities as to why we'd get
1269		 * this condition:
1270		 * 	We sent or received data.
1271		 * 	We sent status & command complete.
1272		 */
1273
1274		break;
1275
1276	case CT7_RESET:
1277		if (fmsg == NULL) {
1278			fmsg = "LIP Reset";
1279		}
1280		/*FALLTHROUGH*/
1281	case CT7_ABORTED:
1282		/*
1283		 * When an Abort message is received the firmware goes to
1284		 * Bus Free and returns all outstanding CTIOs with the status
1285		 * set, then sends us an Immediate Notify entry.
1286		 */
1287		if (fmsg == NULL) {
1288			fmsg = "ABORT";
1289		}
1290		isp_prt(isp, ISP_LOGTDEBUG0, "CTIO7 destroyed by %s: RX_ID=0x%x", fmsg, ct->ct_rxid);
1291		break;
1292
1293	case CT7_TIMEOUT:
1294		if (fmsg == NULL) {
1295			fmsg = "command";
1296		}
1297		isp_prt(isp, ISP_LOGWARN, "Firmware timed out on %s", fmsg);
1298		break;
1299
1300	case CT7_ERR:
1301		fmsg = "Completed with Error";
1302		/*FALLTHROUGH*/
1303	case CT7_LOGOUT:
1304		if (fmsg == NULL) {
1305			fmsg = "Port Logout";
1306		}
1307		/*FALLTHROUGH*/
1308	case CT7_PORTUNAVAIL:
1309		if (fmsg == NULL) {
1310			fmsg = "Port not available";
1311		}
1312		/*FALLTHROUGH*/
1313	case CT7_PORTCHANGED:
1314		if (fmsg == NULL) {
1315			fmsg = "Port Changed";
1316		}
1317		isp_prt(isp, ISP_LOGWARN, "CTIO returned by f/w- %s", fmsg);
1318		break;
1319
1320	case CT7_INVRXID:
1321		/*
1322		 * CTIO rejected by the firmware because an invalid RX_ID.
1323		 * Just print a message.
1324		 */
1325		isp_prt(isp, ISP_LOGWARN, "CTIO7 completed with Invalid RX_ID 0x%x", ct->ct_rxid);
1326		break;
1327
1328	case CT7_REASSY_ERR:
1329		isp_prt(isp, ISP_LOGWARN, "reassembly error");
1330		break;
1331
1332	case CT7_SRR:
1333		isp_prt(isp, ISP_LOGTDEBUG0, "SRR received");
1334		break;
1335
1336	default:
1337		isp_prt(isp, ISP_LOGERR, "Unknown CTIO7 status 0x%x", ct->ct_nphdl);
1338		break;
1339	}
1340
1341	if (xs == NULL) {
1342		/*
1343		 * There may be more than one CTIO for a data transfer,
1344		 * or this may be a status CTIO we're not monitoring.
1345		 *
1346		 * The assumption is that they'll all be returned in the
1347		 * order we got them.
1348		 */
1349		if (ct->ct_syshandle == 0) {
1350			if (ct->ct_flags & CT7_TERMINATE) {
1351				isp_prt(isp, ISP_LOGINFO, "termination of [RX_ID 0x%x] complete", ct->ct_rxid);
1352			} else if ((ct->ct_flags & CT7_SENDSTATUS) == 0) {
1353				isp_prt(isp, pl, "intermediate CTIO completed ok");
1354			} else {
1355				isp_prt(isp, pl, "unmonitored CTIO completed ok");
1356			}
1357		} else {
1358			isp_prt(isp, pl, "NO xs for CTIO (handle 0x%x) status 0x%x", ct->ct_syshandle, ct->ct_nphdl);
1359		}
1360	} else {
1361		if ((ct->ct_flags & CT7_DATAMASK) != CT7_NO_DATA) {
1362			ISP_DMAFREE(isp, xs, ct->ct_syshandle);
1363		}
1364		if (ct->ct_flags & CT7_SENDSTATUS) {
1365			/*
1366			 * Sent status and command complete.
1367			 *
1368			 * We're now really done with this command, so we
1369			 * punt to the platform dependent layers because
1370			 * only there can we do the appropriate command
1371			 * complete thread synchronization.
1372			 */
1373			isp_prt(isp, pl, "status CTIO complete");
1374		} else {
1375			/*
1376			 * Final CTIO completed. Release DMA resources and
1377			 * notify platform dependent layers.
1378			 */
1379			isp_prt(isp, pl, "data CTIO complete");
1380		}
1381		isp_async(isp, ISPASYNC_TARGET_ACTION, ct);
1382		/*
1383		 * The platform layer will destroy the handle if appropriate.
1384		 */
1385	}
1386}
1387
1388static void
1389isp_handle_24xx_inotify(ispsoftc_t *isp, in_fcentry_24xx_t *inot_24xx)
1390{
1391	uint8_t ochan, chan, lochan, hichan;
1392
1393	/*
1394	 * Check to see whether we got a wildcard channel.
1395	 * If so, we have to iterate over all channels.
1396	 */
1397	ochan = chan = ISP_GET_VPIDX(isp, inot_24xx->in_vpidx);
1398	if (chan == 0xff) {
1399		lochan = 0;
1400		hichan = isp->isp_nchan;
1401	} else {
1402		if (chan >= isp->isp_nchan) {
1403			char buf[64];
1404			ISP_SNPRINTF(buf, sizeof buf, "%s: bad channel %d for status 0x%x", __func__, chan, inot_24xx->in_status);
1405			isp_print_bytes(isp, buf, QENTRY_LEN, inot_24xx);
1406			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot_24xx);
1407			return;
1408		}
1409		lochan = chan;
1410		hichan = chan + 1;
1411	}
1412	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);
1413	for (chan = lochan; chan < hichan; chan++) {
1414		if (FCPARAM(isp, chan)->role == ISP_ROLE_NONE)
1415			continue;
1416		switch (inot_24xx->in_status) {
1417		case IN24XX_LIP_RESET:
1418		case IN24XX_LINK_RESET:
1419		case IN24XX_PORT_LOGOUT:
1420		case IN24XX_PORT_CHANGED:
1421		case IN24XX_LINK_FAILED:
1422		case IN24XX_SRR_RCVD:
1423		case IN24XX_ELS_RCVD:
1424			inot_24xx->in_reserved = 0;	/* clear this for later usage */
1425			inot_24xx->in_vpidx = chan;
1426			isp_async(isp, ISPASYNC_TARGET_ACTION, inot_24xx);
1427			break;
1428		default:
1429			isp_prt(isp, ISP_LOGINFO, "%s: unhandled status (0x%x) for chan %d", __func__, inot_24xx->in_status, chan);
1430			isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot_24xx);
1431			break;
1432		}
1433	}
1434	inot_24xx->in_vpidx = ochan;
1435}
1436#endif
1437