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