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