xref: /freebsd/sys/dev/isp/isp_target.c (revision 6990ffd8a95caaba6858ad44ff1b3157d1efba8f)
1 /* $FreeBSD$ */
2 /*
3  * Machine and OS Independent Target Mode Code for the Qlogic SCSI/FC adapters.
4  *
5  * Copyright (c) 1999, 2000, 2001 by Matthew Jacob
6  * All rights reserved.
7  * mjacob@feral.com
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice immediately at the beginning of the file, without modification,
14  *    this list of conditions, and the following disclaimer.
15  * 2. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE 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 THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22  * 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  * Include header file appropriate for platform we're building on.
33  */
34 
35 #ifdef	__NetBSD__
36 #include <dev/ic/isp_netbsd.h>
37 #endif
38 #ifdef	__FreeBSD__
39 #include <dev/isp/isp_freebsd.h>
40 #endif
41 #ifdef	__OpenBSD__
42 #include <dev/ic/isp_openbsd.h>
43 #endif
44 #ifdef	__linux__
45 #include "isp_linux.h"
46 #endif
47 
48 #ifdef	ISP_TARGET_MODE
49 static const char atiocope[] =
50     "ATIO returned for lun %d because it was in the middle of Bus Device Reset "
51     "on bus %d";
52 static const char atior[] =
53     "ATIO returned on for lun %d on from IID %d because a Bus Reset occurred "
54     "on bus %d";
55 
56 static void isp_got_msg(struct ispsoftc *, int, in_entry_t *);
57 static void isp_got_msg_fc(struct ispsoftc *, int, in_fcentry_t *);
58 static void isp_notify_ack(struct ispsoftc *, void *);
59 static void isp_handle_atio(struct ispsoftc *, at_entry_t *);
60 static void isp_handle_atio2(struct ispsoftc *, at2_entry_t *);
61 static void isp_handle_ctio(struct ispsoftc *, ct_entry_t *);
62 static void isp_handle_ctio2(struct ispsoftc *, ct2_entry_t *);
63 
64 /*
65  * The Qlogic driver gets an interrupt to look at response queue entries.
66  * Some of these are status completions for initiatior mode commands, but
67  * if target mode is enabled, we get a whole wad of response queue entries
68  * to be handled here.
69  *
70  * Basically the split into 3 main groups: Lun Enable/Modification responses,
71  * SCSI Command processing, and Immediate Notification events.
72  *
73  * You start by writing a request queue entry to enable target mode (and
74  * establish some resource limitations which you can modify later).
75  * The f/w responds with a LUN ENABLE or LUN MODIFY response with
76  * the status of this action. If the enable was successful, you can expect...
77  *
78  * Response queue entries with SCSI commands encapsulate show up in an ATIO
79  * (Accept Target IO) type- sometimes with enough info to stop the command at
80  * this level. Ultimately the driver has to feed back to the f/w's request
81  * queue a sequence of CTIOs (continue target I/O) that describe data to
82  * be moved and/or status to be sent) and finally finishing with sending
83  * to the f/w's response queue an ATIO which then completes the handshake
84  * with the f/w for that command. There's a lot of variations on this theme,
85  * including flags you can set in the CTIO for the Qlogic 2X00 fibre channel
86  * cards that 'auto-replenish' the f/w's ATIO count, but this is the basic
87  * gist of it.
88  *
89  * The third group that can show up in the response queue are Immediate
90  * Notification events. These include things like notifications of SCSI bus
91  * resets, or Bus Device Reset messages or other messages received. This
92  * a classic oddbins area. It can get  a little weird because you then turn
93  * around and acknowledge the Immediate Notify by writing an entry onto the
94  * request queue and then the f/w turns around and gives you an acknowledgement
95  * to *your* acknowledgement on the response queue (the idea being to let
96  * the f/w tell you when the event is *really* over I guess).
97  *
98  */
99 
100 
101 /*
102  * A new response queue entry has arrived. The interrupt service code
103  * has already swizzled it into the platform dependent from canonical form.
104  *
105  * Because of the way this driver is designed, unfortunately most of the
106  * actual synchronization work has to be done in the platform specific
107  * code- we have no synchroniation primitives in the common code.
108  */
109 
110 int
111 isp_target_notify(struct ispsoftc *isp, void *vptr, u_int16_t *optrp)
112 {
113 	u_int16_t status, seqid;
114 	union {
115 		at_entry_t	*atiop;
116 		at2_entry_t	*at2iop;
117 		ct_entry_t	*ctiop;
118 		ct2_entry_t	*ct2iop;
119 		lun_entry_t	*lunenp;
120 		in_entry_t	*inotp;
121 		in_fcentry_t	*inot_fcp;
122 		na_entry_t	*nackp;
123 		na_fcentry_t	*nack_fcp;
124 		isphdr_t	*hp;
125 		void *		*vp;
126 #define	atiop		unp.atiop
127 #define	at2iop		unp.at2iop
128 #define	ctiop		unp.ctiop
129 #define	ct2iop		unp.ct2iop
130 #define	lunenp		unp.lunenp
131 #define	inotp		unp.inotp
132 #define	inot_fcp	unp.inot_fcp
133 #define	nackp		unp.nackp
134 #define	nack_fcp	unp.nack_fcp
135 #define	hdrp		unp.hp
136 	} unp;
137 	int bus, rval = 0;
138 
139 	unp.vp = vptr;
140 
141 	ISP_TDQE(isp, "isp_target_notify", (int) *optrp, vptr);
142 
143 	switch(hdrp->rqs_entry_type) {
144 	case RQSTYPE_ATIO:
145 		isp_handle_atio(isp, atiop);
146 		break;
147 	case RQSTYPE_CTIO:
148 		isp_handle_ctio(isp, ctiop);
149 		break;
150 	case RQSTYPE_ATIO2:
151 		isp_handle_atio2(isp, at2iop);
152 		break;
153 	case RQSTYPE_CTIO2:
154 		isp_handle_ctio2(isp, ct2iop);
155 		break;
156 	case RQSTYPE_ENABLE_LUN:
157 	case RQSTYPE_MODIFY_LUN:
158 		(void) isp_async(isp, ISPASYNC_TARGET_ACTION, vptr);
159 		break;
160 
161 	case RQSTYPE_NOTIFY:
162 		/*
163 		 * Either the ISP received a SCSI message it can't
164 		 * handle, or it's returning an Immed. Notify entry
165 		 * we sent. We can send Immed. Notify entries to
166 		 * increment the firmware's resource count for them
167 		 * (we set this initially in the Enable Lun entry).
168 		 */
169 		bus = 0;
170 		if (IS_FC(isp)) {
171 			status = inot_fcp->in_status;
172 			seqid = inot_fcp->in_seqid;
173 		} else {
174 			status = inotp->in_status & 0xff;
175 			seqid = inotp->in_seqid;
176 			if (IS_DUALBUS(isp)) {
177 				bus = GET_BUS_VAL(inotp->in_iid);
178 				SET_BUS_VAL(inotp->in_iid, 0);
179 			}
180 		}
181 		isp_prt(isp, ISP_LOGTDEBUG0,
182 		    "Immediate Notify On Bus %d, status=0x%x seqid=0x%x",
183 		    bus, status, seqid);
184 
185 		/*
186 		 * ACK it right away.
187 		 */
188 		isp_notify_ack(isp, (status == IN_RESET)? NULL : vptr);
189 		switch (status) {
190 		case IN_RESET:
191 			(void) isp_async(isp, ISPASYNC_BUS_RESET, &bus);
192 			break;
193 		case IN_MSG_RECEIVED:
194 		case IN_IDE_RECEIVED:
195 			if (IS_FC(isp)) {
196 				isp_got_msg_fc(isp, bus, vptr);
197 			} else {
198 				isp_got_msg(isp, bus, vptr);
199 			}
200 			break;
201 		case IN_RSRC_UNAVAIL:
202 			isp_prt(isp, ISP_LOGWARN, "Firmware out of ATIOs");
203 			break;
204 		case IN_ABORT_TASK:
205 			isp_prt(isp, ISP_LOGWARN,
206 			    "Abort Task from IID %d RX_ID 0x%x",
207 			    inot_fcp->in_iid, seqid);
208 			(void) isp_async(isp, ISPASYNC_TARGET_ACTION, &bus);
209 			break;
210 		case IN_PORT_LOGOUT:
211 			isp_prt(isp, ISP_LOGWARN,
212 			    "Port Logout for Initiator %d RX_ID 0x%x",
213 			    inot_fcp->in_iid, seqid);
214 			break;
215 		case IN_PORT_CHANGED:
216 			isp_prt(isp, ISP_LOGWARN,
217 			    "Port Changed for Initiator %d RX_ID 0x%x",
218 			    inot_fcp->in_iid, seqid);
219 			break;
220 		case IN_GLOBAL_LOGO:
221 			isp_prt(isp, ISP_LOGWARN, "All ports logged out");
222 			break;
223 		default:
224 			isp_prt(isp, ISP_LOGERR,
225 			    "bad status (0x%x) in isp_target_notify", status);
226 			break;
227 		}
228 		break;
229 
230 	case RQSTYPE_NOTIFY_ACK:
231 		/*
232 		 * The ISP is acknowledging our acknowledgement of an
233 		 * Immediate Notify entry for some asynchronous event.
234 		 */
235 		if (IS_FC(isp)) {
236 			isp_prt(isp, ISP_LOGTDEBUG1,
237 			    "Notify Ack status=0x%x seqid 0x%x",
238 			    nack_fcp->na_status, nack_fcp->na_seqid);
239 		} else {
240 			isp_prt(isp, ISP_LOGTDEBUG1,
241 			    "Notify Ack event 0x%x status=0x%x seqid 0x%x",
242 			    nackp->na_event, nackp->na_status, nackp->na_seqid);
243 		}
244 		break;
245 	default:
246 		isp_prt(isp, ISP_LOGERR,
247 		    "Unknown entry type 0x%x in isp_target_notify",
248 		    hdrp->rqs_entry_type);
249 		rval = -1;
250 		break;
251 	}
252 #undef	atiop
253 #undef	at2iop
254 #undef	ctiop
255 #undef	ct2iop
256 #undef	lunenp
257 #undef	inotp
258 #undef	inot_fcp
259 #undef	nackp
260 #undef	nack_fcp
261 #undef	hdrp
262 	return (rval);
263 }
264 
265 
266 /*
267  * Toggle (on/off) target mode for bus/target/lun
268  *
269  * The caller has checked for overlap and legality.
270  *
271  * Note that not all of bus, target or lun can be paid attention to.
272  * Note also that this action will not be complete until the f/w writes
273  * response entry. The caller is responsible for synchronizing this.
274  */
275 int
276 isp_lun_cmd(struct ispsoftc *isp, int cmd, int bus, int tgt, int lun,
277     int cmd_cnt, int inot_cnt, u_int32_t opaque)
278 {
279 	lun_entry_t el;
280 	u_int16_t iptr, optr;
281 	void *outp;
282 
283 
284 	MEMZERO(&el, sizeof (el));
285 	if (IS_DUALBUS(isp)) {
286 		el.le_rsvd = (bus & 0x1) << 7;
287 	}
288 	el.le_cmd_count = cmd_cnt;
289 	el.le_in_count = inot_cnt;
290 	if (cmd == RQSTYPE_ENABLE_LUN) {
291 		if (IS_SCSI(isp)) {
292 			el.le_flags = LUN_TQAE|LUN_DISAD;
293 			el.le_cdb6len = 12;
294 			el.le_cdb7len = 12;
295 		}
296 	} else if (cmd == -RQSTYPE_ENABLE_LUN) {
297 		cmd = RQSTYPE_ENABLE_LUN;
298 		el.le_cmd_count = 0;
299 		el.le_in_count = 0;
300 	} else if (cmd == -RQSTYPE_MODIFY_LUN) {
301 		cmd = RQSTYPE_MODIFY_LUN;
302 		el.le_ops = LUN_CCDECR | LUN_INDECR;
303 	} else {
304 		el.le_ops = LUN_CCINCR | LUN_ININCR;
305 	}
306 	el.le_header.rqs_entry_type = cmd;
307 	el.le_header.rqs_entry_count = 1;
308 	el.le_reserved = opaque;
309 	if (IS_SCSI(isp)) {
310 		el.le_tgt = tgt;
311 		el.le_lun = lun;
312 	} else if ((FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) == 0) {
313 		el.le_lun = lun;
314 	}
315 	el.le_timeout = 2;
316 
317 	if (isp_getrqentry(isp, &iptr, &optr, &outp)) {
318 		isp_prt(isp, ISP_LOGWARN,
319 		    "Request Queue Overflow in isp_lun_cmd");
320 		return (-1);
321 	}
322 	ISP_SWIZ_ENABLE_LUN(isp, outp, &el);
323 	ISP_TDQE(isp, "isp_lun_cmd", (int) optr, &el);
324 	ISP_ADD_REQUEST(isp, iptr);
325 	return (0);
326 }
327 
328 
329 int
330 isp_target_put_entry(struct ispsoftc *isp, void *ap)
331 {
332 	void *outp;
333 	u_int16_t iptr, optr;
334 	u_int8_t etype = ((isphdr_t *) ap)->rqs_entry_type;
335 
336 	if (isp_getrqentry(isp, &iptr, &optr, &outp)) {
337 		isp_prt(isp, ISP_LOGWARN,
338 		    "Request Queue Overflow in isp_target_put_entry");
339 		return (-1);
340 	}
341 	switch (etype) {
342 	case RQSTYPE_ATIO:
343 		ISP_SWIZ_ATIO(isp, outp, ap);
344 		break;
345 	case RQSTYPE_ATIO2:
346 		ISP_SWIZ_ATIO2(isp, outp, ap);
347 		break;
348 	case RQSTYPE_CTIO:
349 		ISP_SWIZ_CTIO(isp, outp, ap);
350 		break;
351 	case RQSTYPE_CTIO2:
352 		ISP_SWIZ_CTIO2(isp, outp, ap);
353 		break;
354 	default:
355 		isp_prt(isp, ISP_LOGERR,
356 		    "Unknown type 0x%x in isp_put_entry", etype);
357 		return (-1);
358 	}
359 
360 	ISP_TDQE(isp, "isp_target_put_entry", (int) optr, ap);;
361 
362 	ISP_ADD_REQUEST(isp, iptr);
363 	return (0);
364 }
365 
366 int
367 isp_target_put_atio(struct ispsoftc *isp, void *arg)
368 {
369 	union {
370 		at_entry_t _atio;
371 		at2_entry_t _atio2;
372 	} atun;
373 
374 	MEMZERO(&atun, sizeof atun);
375 	if (IS_FC(isp)) {
376 		at2_entry_t *aep = arg;
377 		atun._atio2.at_header.rqs_entry_type = RQSTYPE_ATIO2;
378 		atun._atio2.at_header.rqs_entry_count = 1;
379 		if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) {
380 			atun._atio2.at_scclun = (u_int16_t) aep->at_scclun;
381 		} else {
382 			atun._atio2.at_lun = (u_int8_t) aep->at_lun;
383 		}
384 		atun._atio2.at_status = CT_OK;
385 	} else {
386 		at_entry_t *aep = arg;
387 		atun._atio.at_header.rqs_entry_type = RQSTYPE_ATIO;
388 		atun._atio.at_header.rqs_entry_count = 1;
389 		atun._atio.at_handle = aep->at_handle;
390 		atun._atio.at_iid = aep->at_iid;
391 		atun._atio.at_tgt = aep->at_tgt;
392 		atun._atio.at_lun = aep->at_lun;
393 		atun._atio.at_tag_type = aep->at_tag_type;
394 		atun._atio.at_tag_val = aep->at_tag_val;
395 		atun._atio.at_status = (aep->at_flags & AT_TQAE);
396 		atun._atio.at_status |= CT_OK;
397 	}
398 	return (isp_target_put_entry(isp, &atun));
399 }
400 
401 /*
402  * Command completion- both for handling cases of no resources or
403  * no blackhole driver, or other cases where we have to, inline,
404  * finish the command sanely, or for normal command completion.
405  *
406  * The 'completion' code value has the scsi status byte in the low 8 bits.
407  * If status is a CHECK CONDITION and bit 8 is nonzero, then bits 12..15 have
408  * the sense key and  bits 16..23 have the ASCQ and bits 24..31 have the ASC
409  * values.
410  *
411  * NB: the key, asc, ascq, cannot be used for parallel SCSI as it doesn't
412  * NB: inline SCSI sense reporting. As such, we lose this information. XXX.
413  *
414  * For both parallel && fibre channel, we use the feature that does
415  * an automatic resource autoreplenish so we don't have then later do
416  * put of an atio to replenish the f/w's resource count.
417  */
418 
419 int
420 isp_endcmd(struct ispsoftc *isp, void *arg, u_int32_t code, u_int16_t hdl)
421 {
422 	int sts;
423 	union {
424 		ct_entry_t _ctio;
425 		ct2_entry_t _ctio2;
426 	} un;
427 
428 	MEMZERO(&un, sizeof un);
429 	sts = code & 0xff;
430 
431 	if (IS_FC(isp)) {
432 		at2_entry_t *aep = arg;
433 		ct2_entry_t *cto = &un._ctio2;
434 
435 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
436 		cto->ct_header.rqs_entry_count = 1;
437 		cto->ct_iid = aep->at_iid;
438 		if ((FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) == 0) {
439 			cto->ct_lun = aep->at_lun;
440 		}
441 		cto->ct_rxid = aep->at_rxid;
442 		cto->rsp.m1.ct_scsi_status = sts & 0xff;
443 		cto->ct_flags = CT2_SENDSTATUS | CT2_NO_DATA | CT2_FLAG_MODE1;
444 		if (hdl == 0) {
445 			cto->ct_flags |= CT2_CCINCR;
446 		}
447 		if (aep->at_datalen) {
448 			cto->ct_resid = aep->at_datalen;
449 			cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER;
450 		}
451 		if ((sts & 0xff) == SCSI_CHECK && (sts & ECMD_SVALID)) {
452 			cto->rsp.m1.ct_resp[0] = 0xf0;
453 			cto->rsp.m1.ct_resp[2] = (code >> 12) & 0xf;
454 			cto->rsp.m1.ct_resp[7] = 8;
455 			cto->rsp.m1.ct_resp[12] = (code >> 24) & 0xff;
456 			cto->rsp.m1.ct_resp[13] = (code >> 16) & 0xff;
457 			cto->rsp.m1.ct_senselen = 16;
458 			cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
459 		}
460 		cto->ct_syshandle = hdl;
461 	} else {
462 		at_entry_t *aep = arg;
463 		ct_entry_t *cto = &un._ctio;
464 
465 		cto->ct_header.rqs_entry_type = RQSTYPE_CTIO;
466 		cto->ct_header.rqs_entry_count = 1;
467 		cto->ct_fwhandle = aep->at_handle;
468 		cto->ct_iid = aep->at_iid;
469 		cto->ct_tgt = aep->at_tgt;
470 		cto->ct_lun = aep->at_lun;
471 		cto->ct_tag_type = aep->at_tag_type;
472 		cto->ct_tag_val = aep->at_tag_val;
473 		if (aep->at_flags & AT_TQAE) {
474 			cto->ct_flags |= CT_TQAE;
475 		}
476 		cto->ct_flags = CT_SENDSTATUS | CT_NO_DATA;
477 		if (hdl == 0) {
478 			cto->ct_flags |= CT_CCINCR;
479 		}
480 		cto->ct_scsi_status = sts;
481 		cto->ct_syshandle = hdl;
482 	}
483 	return (isp_target_put_entry(isp, &un));
484 }
485 
486 void
487 isp_target_async(struct ispsoftc *isp, int bus, int event)
488 {
489 	tmd_event_t evt;
490 	tmd_msg_t msg;
491 
492 	switch (event) {
493 	/*
494 	 * These three we handle here to propagate an effective bus reset
495 	 * upstream, but these do not require any immediate notify actions
496 	 * so we return when done.
497 	 */
498 	case ASYNC_LIP_F8:
499 	case ASYNC_LIP_OCCURRED:
500 	case ASYNC_LOOP_UP:
501 	case ASYNC_LOOP_DOWN:
502 	case ASYNC_LOOP_RESET:
503 	case ASYNC_PTPMODE:
504 		/*
505 		 * These don't require any immediate notify actions. We used
506 		 * treat them like SCSI Bus Resets, but that was just plain
507 		 * wrong. Let the normal CTIO completion report what occurred.
508 		 */
509                 return;
510 
511 	case ASYNC_BUS_RESET:
512 	case ASYNC_TIMEOUT_RESET:
513 		if (IS_FC(isp)) {
514 			return;	/* we'll be getting an inotify instead */
515 		}
516 		evt.ev_bus = bus;
517 		evt.ev_event = event;
518 		(void) isp_async(isp, ISPASYNC_TARGET_EVENT, &evt);
519 		break;
520 	case ASYNC_DEVICE_RESET:
521 		/*
522 		 * Bus Device Reset resets a specific target, so
523 		 * we pass this as a synthesized message.
524 		 */
525 		MEMZERO(&msg, sizeof msg);
526 		if (IS_FC(isp)) {
527 			msg.nt_iid = FCPARAM(isp)->isp_loopid;
528 		} else {
529 			msg.nt_iid = SDPARAM(isp)->isp_initiator_id;
530 		}
531 		msg.nt_bus = bus;
532 		msg.nt_msg[0] = MSG_BUS_DEV_RESET;
533 		(void) isp_async(isp, ISPASYNC_TARGET_MESSAGE, &msg);
534 		break;
535 	default:
536 		isp_prt(isp, ISP_LOGERR,
537 		    "isp_target_async: unknown event 0x%x", event);
538 		break;
539 	}
540 	if (isp->isp_state == ISP_RUNSTATE)
541 		isp_notify_ack(isp, NULL);
542 }
543 
544 
545 /*
546  * Process a received message.
547  * The ISP firmware can handle most messages, there are only
548  * a few that we need to deal with:
549  * - abort: clean up the current command
550  * - abort tag and clear queue
551  */
552 
553 static void
554 isp_got_msg(struct ispsoftc *isp, int bus, in_entry_t *inp)
555 {
556 	u_int8_t status = inp->in_status & ~QLTM_SVALID;
557 
558 	if (status == IN_IDE_RECEIVED || status == IN_MSG_RECEIVED) {
559 		tmd_msg_t msg;
560 
561 		MEMZERO(&msg, sizeof (msg));
562 		msg.nt_bus = bus;
563 		msg.nt_iid = inp->in_iid;
564 		msg.nt_tgt = inp->in_tgt;
565 		msg.nt_lun = inp->in_lun;
566 		msg.nt_tagtype = inp->in_tag_type;
567 		msg.nt_tagval = inp->in_tag_val;
568 		MEMCPY(msg.nt_msg, inp->in_msg, IN_MSGLEN);
569 		(void) isp_async(isp, ISPASYNC_TARGET_MESSAGE, &msg);
570 	} else {
571 		isp_prt(isp, ISP_LOGERR,
572 		    "unknown immediate notify status 0x%x", inp->in_status);
573 	}
574 }
575 
576 /*
577  * Synthesize a message from the task management flags in a FCP_CMND_IU.
578  */
579 static void
580 isp_got_msg_fc(struct ispsoftc *isp, int bus, in_fcentry_t *inp)
581 {
582 	int lun;
583 	static const char f1[] = "%s from iid %d lun %d seq 0x%x";
584 	static const char f2[] =
585 	    "unknown %s 0x%x lun %d iid %d task flags 0x%x seq 0x%x\n";
586 
587 	if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) {
588 		lun = inp->in_scclun;
589 	} else {
590 		lun = inp->in_lun;
591 	}
592 
593 	if (inp->in_status != IN_MSG_RECEIVED) {
594 		isp_prt(isp, ISP_LOGINFO, f2, "immediate notify status",
595 		    inp->in_status, lun, inp->in_iid,
596 		    inp->in_task_flags,  inp->in_seqid);
597 	} else {
598 		tmd_msg_t msg;
599 
600 		MEMZERO(&msg, sizeof (msg));
601 		msg.nt_bus = bus;
602 		msg.nt_iid = inp->in_iid;
603 		msg.nt_tagval = inp->in_seqid;
604 		msg.nt_lun = lun;
605 
606 		if (inp->in_task_flags & TASK_FLAGS_ABORT_TASK) {
607 			isp_prt(isp, ISP_LOGINFO, f1, "ABORT TASK",
608 			    inp->in_iid, msg.nt_lun, inp->in_seqid);
609 			msg.nt_msg[0] = MSG_ABORT_TAG;
610 		} else if (inp->in_task_flags & TASK_FLAGS_CLEAR_TASK_SET) {
611 			isp_prt(isp, ISP_LOGINFO, f1, "CLEAR TASK SET",
612 			    inp->in_iid, msg.nt_lun, inp->in_seqid);
613 			msg.nt_msg[0] = MSG_CLEAR_QUEUE;
614 		} else if (inp->in_task_flags & TASK_FLAGS_TARGET_RESET) {
615 			isp_prt(isp, ISP_LOGINFO, f1, "TARGET RESET",
616 			    inp->in_iid, msg.nt_lun, inp->in_seqid);
617 			msg.nt_msg[0] = MSG_BUS_DEV_RESET;
618 		} else if (inp->in_task_flags & TASK_FLAGS_CLEAR_ACA) {
619 			isp_prt(isp, ISP_LOGINFO, f1, "CLEAR ACA",
620 			    inp->in_iid, msg.nt_lun, inp->in_seqid);
621 			/* ???? */
622 			msg.nt_msg[0] = MSG_REL_RECOVERY;
623 		} else if (inp->in_task_flags & TASK_FLAGS_TERMINATE_TASK) {
624 			isp_prt(isp, ISP_LOGINFO, f1, "TERMINATE TASK",
625 			    inp->in_iid, msg.nt_lun, inp->in_seqid);
626 			msg.nt_msg[0] = MSG_TERM_IO_PROC;
627 		} else {
628 			isp_prt(isp, ISP_LOGWARN, f2, "task flag",
629 			    inp->in_status, msg.nt_lun, inp->in_iid,
630 			    inp->in_task_flags,  inp->in_seqid);
631 		}
632 		if (msg.nt_msg[0]) {
633 			(void) isp_async(isp, ISPASYNC_TARGET_MESSAGE, &msg);
634 		}
635 	}
636 }
637 
638 static void
639 isp_notify_ack(struct ispsoftc *isp, void *arg)
640 {
641 	char storage[QENTRY_LEN];
642 	u_int16_t iptr, optr;
643 	void *outp;
644 
645 	if (isp_getrqentry(isp, &iptr, &optr, &outp)) {
646 		isp_prt(isp, ISP_LOGWARN,
647 		    "Request Queue Overflow For isp_notify_ack");
648 		return;
649 	}
650 
651 	MEMZERO(storage, QENTRY_LEN);
652 
653 	if (IS_FC(isp)) {
654 		na_fcentry_t *na = (na_fcentry_t *) storage;
655 		if (arg) {
656 			in_fcentry_t *inp = arg;
657 			MEMCPY(storage, arg, sizeof (isphdr_t));
658 			na->na_iid = inp->in_iid;
659 			if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) {
660 				na->na_lun = inp->in_scclun;
661 			} else {
662 				na->na_lun = inp->in_lun;
663 			}
664 			na->na_task_flags = inp->in_task_flags;
665 			na->na_seqid = inp->in_seqid;
666 			na->na_flags = NAFC_RCOUNT;
667 			if (inp->in_status == IN_RESET) {
668 				na->na_flags |= NAFC_RST_CLRD;
669 			}
670 		} else {
671 			na->na_flags = NAFC_RST_CLRD;
672 		}
673 		na->na_header.rqs_entry_type = RQSTYPE_NOTIFY_ACK;
674 		na->na_header.rqs_entry_count = 1;
675 		ISP_SWIZ_NOT_ACK_FC(isp, outp, na);
676 	} else {
677 		na_entry_t *na = (na_entry_t *) storage;
678 		if (arg) {
679 			in_entry_t *inp = arg;
680 			MEMCPY(storage, arg, sizeof (isphdr_t));
681 			na->na_iid = inp->in_iid;
682 			na->na_lun = inp->in_lun;
683 			na->na_tgt = inp->in_tgt;
684 			na->na_seqid = inp->in_seqid;
685 			if (inp->in_status == IN_RESET) {
686 				na->na_event = NA_RST_CLRD;
687 			}
688 		} else {
689 			na->na_event = NA_RST_CLRD;
690 		}
691 		na->na_header.rqs_entry_type = RQSTYPE_NOTIFY_ACK;
692 		na->na_header.rqs_entry_count = 1;
693 		ISP_SWIZ_NOT_ACK(isp, outp, na);
694 	}
695 	ISP_TDQE(isp, "isp_notify_ack", (int) optr, storage);
696 	ISP_ADD_REQUEST(isp, iptr);
697 }
698 
699 static void
700 isp_handle_atio(struct ispsoftc *isp, at_entry_t *aep)
701 {
702 	int lun;
703 	lun = aep->at_lun;
704 	/*
705 	 * The firmware status (except for the QLTM_SVALID bit) indicates
706 	 * why this ATIO was sent to us.
707 	 *
708 	 * If QLTM_SVALID is set, the firware has recommended Sense Data.
709 	 *
710 	 * If the DISCONNECTS DISABLED bit is set in the flags field,
711 	 * we're still connected on the SCSI bus - i.e. the initiator
712 	 * did not set DiscPriv in the identify message. We don't care
713 	 * about this so it's ignored.
714 	 */
715 
716 	switch(aep->at_status & ~QLTM_SVALID) {
717 	case AT_PATH_INVALID:
718 		/*
719 		 * ATIO rejected by the firmware due to disabled lun.
720 		 */
721 		isp_prt(isp, ISP_LOGERR,
722 		    "rejected ATIO for disabled lun %d", lun);
723 		break;
724 	case AT_NOCAP:
725 		/*
726 		 * Requested Capability not available
727 		 * We sent an ATIO that overflowed the firmware's
728 		 * command resource count.
729 		 */
730 		isp_prt(isp, ISP_LOGERR,
731 		    "rejected ATIO for lun %d because of command count"
732 		    " overflow", lun);
733 		break;
734 
735 	case AT_BDR_MSG:
736 		/*
737 		 * If we send an ATIO to the firmware to increment
738 		 * its command resource count, and the firmware is
739 		 * recovering from a Bus Device Reset, it returns
740 		 * the ATIO with this status. We set the command
741 		 * resource count in the Enable Lun entry and no
742 		 * not increment it. Therefore we should never get
743 		 * this status here.
744 		 */
745 		isp_prt(isp, ISP_LOGERR, atiocope, lun,
746 		    GET_BUS_VAL(aep->at_iid));
747 		break;
748 
749 	case AT_CDB:		/* Got a CDB */
750 	case AT_PHASE_ERROR:	/* Bus Phase Sequence Error */
751 		/*
752 		 * Punt to platform specific layer.
753 		 */
754 		(void) isp_async(isp, ISPASYNC_TARGET_ACTION, aep);
755 		break;
756 
757 	case AT_RESET:
758 		/*
759 		 * A bus reset came along an blew away this command. Why
760 		 * they do this in addition the async event code stuff,
761 		 * I dunno.
762 		 *
763 		 * Ignore it because the async event will clear things
764 		 * up for us.
765 		 */
766 		isp_prt(isp, ISP_LOGWARN, atior, lun,
767 		    GET_IID_VAL(aep->at_iid), GET_BUS_VAL(aep->at_iid));
768 		break;
769 
770 
771 	default:
772 		isp_prt(isp, ISP_LOGERR,
773 		    "Unknown ATIO status 0x%x from initiator %d for lun %d",
774 		    aep->at_status, aep->at_iid, lun);
775 		(void) isp_target_put_atio(isp, aep);
776 		break;
777 	}
778 }
779 
780 static void
781 isp_handle_atio2(struct ispsoftc *isp, at2_entry_t *aep)
782 {
783 	int lun;
784 
785 	if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) {
786 		lun = aep->at_scclun;
787 	} else {
788 		lun = aep->at_lun;
789 	}
790 
791 	/*
792 	 * The firmware status (except for the QLTM_SVALID bit) indicates
793 	 * why this ATIO was sent to us.
794 	 *
795 	 * If QLTM_SVALID is set, the firware has recommended Sense Data.
796 	 *
797 	 * If the DISCONNECTS DISABLED bit is set in the flags field,
798 	 * we're still connected on the SCSI bus - i.e. the initiator
799 	 * did not set DiscPriv in the identify message. We don't care
800 	 * about this so it's ignored.
801 	 */
802 
803 	switch(aep->at_status & ~QLTM_SVALID) {
804 	case AT_PATH_INVALID:
805 		/*
806 		 * ATIO rejected by the firmware due to disabled lun.
807 		 */
808 		isp_prt(isp, ISP_LOGERR,
809 		    "rejected ATIO2 for disabled lun %d", lun);
810 		break;
811 	case AT_NOCAP:
812 		/*
813 		 * Requested Capability not available
814 		 * We sent an ATIO that overflowed the firmware's
815 		 * command resource count.
816 		 */
817 		isp_prt(isp, ISP_LOGERR,
818 		    "rejected ATIO2 for lun %d- command count overflow", lun);
819 		break;
820 
821 	case AT_BDR_MSG:
822 		/*
823 		 * If we send an ATIO to the firmware to increment
824 		 * its command resource count, and the firmware is
825 		 * recovering from a Bus Device Reset, it returns
826 		 * the ATIO with this status. We set the command
827 		 * resource count in the Enable Lun entry and no
828 		 * not increment it. Therefore we should never get
829 		 * this status here.
830 		 */
831 		isp_prt(isp, ISP_LOGERR, atiocope, lun, 0);
832 		break;
833 
834 	case AT_CDB:		/* Got a CDB */
835 		/*
836 		 * Punt to platform specific layer.
837 		 */
838 		(void) isp_async(isp, ISPASYNC_TARGET_ACTION, aep);
839 		break;
840 
841 	case AT_RESET:
842 		/*
843 		 * A bus reset came along an blew away this command. Why
844 		 * they do this in addition the async event code stuff,
845 		 * I dunno.
846 		 *
847 		 * Ignore it because the async event will clear things
848 		 * up for us.
849 		 */
850 		isp_prt(isp, ISP_LOGERR, atior, lun, aep->at_iid, 0);
851 		break;
852 
853 
854 	default:
855 		isp_prt(isp, ISP_LOGERR,
856 		    "Unknown ATIO2 status 0x%x from initiator %d for lun %d",
857 		    aep->at_status, aep->at_iid, lun);
858 		(void) isp_target_put_atio(isp, aep);
859 		break;
860 	}
861 }
862 
863 static void
864 isp_handle_ctio(struct ispsoftc *isp, ct_entry_t *ct)
865 {
866 	void *xs;
867 	int pl = ISP_LOGTDEBUG2;
868 	char *fmsg = NULL;
869 
870 	if (ct->ct_syshandle) {
871 		xs = isp_find_xs(isp, ct->ct_syshandle);
872 		if (xs == NULL)
873 			pl = ISP_LOGALL;
874 	} else {
875 		xs = NULL;
876 	}
877 
878 	switch(ct->ct_status & ~QLTM_SVALID) {
879 	case CT_OK:
880 		/*
881 		 * There are generally 3 possibilities as to why we'd get
882 		 * this condition:
883 		 * 	We disconnected after receiving a CDB.
884 		 * 	We sent or received data.
885 		 * 	We sent status & command complete.
886 		 */
887 
888 		if (ct->ct_flags & CT_SENDSTATUS) {
889 			break;
890 		} else if ((ct->ct_flags & CT_DATAMASK) == CT_NO_DATA) {
891 			/*
892 			 * Nothing to do in this case.
893 			 */
894 			isp_prt(isp, pl, "CTIO- iid %d disconnected OK",
895 			    ct->ct_iid);
896 			return;
897 		}
898 		break;
899 
900 	case CT_BDR_MSG:
901 		/*
902 		 * Bus Device Reset message received or the SCSI Bus has
903 		 * been Reset; the firmware has gone to Bus Free.
904 		 *
905 		 * The firmware generates an async mailbox interupt to
906 		 * notify us of this and returns outstanding CTIOs with this
907 		 * status. These CTIOs are handled in that same way as
908 		 * CT_ABORTED ones, so just fall through here.
909 		 */
910 		fmsg = "Bus Device Reset";
911 		/*FALLTHROUGH*/
912 	case CT_RESET:
913 		if (fmsg == NULL)
914 			fmsg = "Bus Reset";
915 		/*FALLTHROUGH*/
916 	case CT_ABORTED:
917 		/*
918 		 * When an Abort message is received the firmware goes to
919 		 * Bus Free and returns all outstanding CTIOs with the status
920 		 * set, then sends us an Immediate Notify entry.
921 		 */
922 		if (fmsg == NULL)
923 			fmsg = "ABORT TAG message sent by Initiator";
924 
925 		isp_prt(isp, ISP_LOGWARN, "CTIO destroyed by %s", fmsg);
926 		break;
927 
928 	case CT_INVAL:
929 		/*
930 		 * CTIO rejected by the firmware due to disabled lun.
931 		 * "Cannot Happen".
932 		 */
933 		isp_prt(isp, ISP_LOGERR,
934 		    "Firmware rejected CTIO for disabled lun %d",
935 		    ct->ct_lun);
936 		break;
937 
938 	case CT_NOPATH:
939 		/*
940 		 * CTIO rejected by the firmware due "no path for the
941 		 * nondisconnecting nexus specified". This means that
942 		 * we tried to access the bus while a non-disconnecting
943 		 * command is in process.
944 		 */
945 		isp_prt(isp, ISP_LOGERR,
946 		    "Firmware rejected CTIO for bad nexus %d/%d/%d",
947 		    ct->ct_iid, ct->ct_tgt, ct->ct_lun);
948 		break;
949 
950 	case CT_RSELTMO:
951 		fmsg = "Reselection";
952 		/*FALLTHROUGH*/
953 	case CT_TIMEOUT:
954 		if (fmsg == NULL)
955 			fmsg = "Command";
956 		isp_prt(isp, ISP_LOGERR, "Firmware timed out on %s", fmsg);
957 		break;
958 
959 	case	CT_PANIC:
960 		if (fmsg == NULL)
961 			fmsg = "Unrecoverable Error";
962 		/*FALLTHROUGH*/
963 	case CT_ERR:
964 		if (fmsg == NULL)
965 			fmsg = "Completed with Error";
966 		/*FALLTHROUGH*/
967 	case CT_PHASE_ERROR:
968 		if (fmsg == NULL)
969 			fmsg = "Phase Sequence Error";
970 		/*FALLTHROUGH*/
971 	case CT_TERMINATED:
972 		if (fmsg == NULL)
973 			fmsg = "terminated by TERMINATE TRANSFER";
974 		/*FALLTHROUGH*/
975 	case CT_NOACK:
976 		if (fmsg == NULL)
977 			fmsg = "unacknowledged Immediate Notify pending";
978 		isp_prt(isp, ISP_LOGERR, "CTIO returned by f/w- %s", fmsg);
979 		break;
980 	default:
981 		isp_prt(isp, ISP_LOGERR, "Unknown CTIO status 0x%x",
982 		    ct->ct_status & ~QLTM_SVALID);
983 		break;
984 	}
985 
986 	if (xs == NULL) {
987 		/*
988 		 * There may be more than one CTIO for a data transfer,
989 		 * or this may be a status CTIO we're not monitoring.
990 		 *
991 		 * The assumption is that they'll all be returned in the
992 		 * order we got them.
993 		 */
994 		if (ct->ct_syshandle == 0) {
995 			if ((ct->ct_flags & CT_SENDSTATUS) == 0) {
996 				isp_prt(isp, pl,
997 				    "intermediate CTIO completed ok");
998 			} else {
999 				isp_prt(isp, pl,
1000 				    "unmonitored CTIO completed ok");
1001 			}
1002 		} else {
1003 			isp_prt(isp, pl,
1004 			    "NO xs for CTIO (handle 0x%x) status 0x%x",
1005 			    ct->ct_syshandle, ct->ct_status & ~QLTM_SVALID);
1006 		}
1007 	} else {
1008 		/*
1009 		 * Final CTIO completed. Release DMA resources and
1010 		 * notify platform dependent layers.
1011 		 */
1012 		if ((ct->ct_flags & CT_DATAMASK) != CT_NO_DATA) {
1013 			ISP_DMAFREE(isp, xs, ct->ct_syshandle);
1014 		}
1015 		isp_prt(isp, pl, "final CTIO complete");
1016 		/*
1017 		 * The platform layer will destroy the handle if appropriate.
1018 		 */
1019 		(void) isp_async(isp, ISPASYNC_TARGET_ACTION, ct);
1020 	}
1021 }
1022 
1023 static void
1024 isp_handle_ctio2(struct ispsoftc *isp, ct2_entry_t *ct)
1025 {
1026 	XS_T *xs;
1027 	int pl = ISP_LOGTDEBUG2;
1028 	char *fmsg = NULL;
1029 
1030 	if (ct->ct_syshandle) {
1031 		xs = isp_find_xs(isp, ct->ct_syshandle);
1032 		if (xs == NULL)
1033 			pl = ISP_LOGALL;
1034 	} else {
1035 		xs = NULL;
1036 	}
1037 
1038 	switch(ct->ct_status & ~QLTM_SVALID) {
1039 	case CT_BUS_ERROR:
1040 		isp_prt(isp, ISP_LOGERR, "PCI DMA Bus Error");
1041 		/* FALL Through */
1042 	case CT_DATA_OVER:
1043 	case CT_DATA_UNDER:
1044 	case CT_OK:
1045 		/*
1046 		 * There are generally 2 possibilities as to why we'd get
1047 		 * this condition:
1048 		 * 	We sent or received data.
1049 		 * 	We sent status & command complete.
1050 		 */
1051 
1052 		break;
1053 
1054 	case CT_BDR_MSG:
1055 		/*
1056 		 * Target Reset function received.
1057 		 *
1058 		 * The firmware generates an async mailbox interupt to
1059 		 * notify us of this and returns outstanding CTIOs with this
1060 		 * status. These CTIOs are handled in that same way as
1061 		 * CT_ABORTED ones, so just fall through here.
1062 		 */
1063 		fmsg = "TARGET RESET Task Management Function Received";
1064 		/*FALLTHROUGH*/
1065 	case CT_RESET:
1066 		if (fmsg == NULL)
1067 			fmsg = "LIP Reset";
1068 		/*FALLTHROUGH*/
1069 	case CT_ABORTED:
1070 		/*
1071 		 * When an Abort message is received the firmware goes to
1072 		 * Bus Free and returns all outstanding CTIOs with the status
1073 		 * set, then sends us an Immediate Notify entry.
1074 		 */
1075 		if (fmsg == NULL)
1076 			fmsg = "ABORT Task Management Function Received";
1077 
1078 		isp_prt(isp, ISP_LOGERR, "CTIO2 destroyed by %s", fmsg);
1079 		break;
1080 
1081 	case CT_INVAL:
1082 		/*
1083 		 * CTIO rejected by the firmware - invalid data direction.
1084 		 */
1085 		isp_prt(isp, ISP_LOGERR, "CTIO2 had wrong data directiond");
1086 		break;
1087 
1088 	case CT_RSELTMO:
1089 		fmsg = "failure to reconnect to initiator";
1090 		/*FALLTHROUGH*/
1091 	case CT_TIMEOUT:
1092 		if (fmsg == NULL)
1093 			fmsg = "command";
1094 		isp_prt(isp, ISP_LOGERR, "Firmware timed out on %s", fmsg);
1095 		break;
1096 
1097 	case CT_ERR:
1098 		fmsg = "Completed with Error";
1099 		/*FALLTHROUGH*/
1100 	case CT_LOGOUT:
1101 		if (fmsg == NULL)
1102 			fmsg = "Port Logout";
1103 		/*FALLTHROUGH*/
1104 	case CT_PORTNOTAVAIL:
1105 		if (fmsg == NULL)
1106 			fmsg = "Port not available";
1107 	case CT_PORTCHANGED:
1108 		if (fmsg == NULL)
1109 			fmsg = "Port Changed";
1110 	case CT_NOACK:
1111 		if (fmsg == NULL)
1112 			fmsg = "unacknowledged Immediate Notify pending";
1113 		isp_prt(isp, ISP_LOGERR, "CTIO returned by f/w- %s", fmsg);
1114 		break;
1115 
1116 	case CT_INVRXID:
1117 		/*
1118 		 * CTIO rejected by the firmware because an invalid RX_ID.
1119 		 * Just print a message.
1120 		 */
1121 		isp_prt(isp, ISP_LOGERR,
1122 		    "CTIO2 completed with Invalid RX_ID 0x%x", ct->ct_rxid);
1123 		break;
1124 
1125 	default:
1126 		isp_prt(isp, ISP_LOGERR, "Unknown CTIO2 status 0x%x",
1127 		    ct->ct_status & ~QLTM_SVALID);
1128 		break;
1129 	}
1130 
1131 	if (xs == NULL) {
1132 		/*
1133 		 * There may be more than one CTIO for a data transfer,
1134 		 * or this may be a status CTIO we're not monitoring.
1135 		 *
1136 		 * The assumption is that they'll all be returned in the
1137 		 * order we got them.
1138 		 */
1139 		if (ct->ct_syshandle == 0) {
1140 			if ((ct->ct_flags & CT_SENDSTATUS) == 0) {
1141 				isp_prt(isp, pl,
1142 				    "intermediate CTIO completed ok");
1143 			} else {
1144 				isp_prt(isp, pl,
1145 				    "unmonitored CTIO completed ok");
1146 			}
1147 		} else {
1148 			isp_prt(isp, pl,
1149 			    "NO xs for CTIO (handle 0x%x) status 0x%x",
1150 			    ct->ct_syshandle, ct->ct_status & ~QLTM_SVALID);
1151 		}
1152 	} else {
1153 		if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA) {
1154 			ISP_DMAFREE(isp, xs, ct->ct_syshandle);
1155 		}
1156 		if (ct->ct_flags & CT_SENDSTATUS) {
1157 			/*
1158 			 * Sent status and command complete.
1159 			 *
1160 			 * We're now really done with this command, so we
1161 			 * punt to the platform dependent layers because
1162 			 * only there can we do the appropriate command
1163 			 * complete thread synchronization.
1164 			 */
1165 			isp_prt(isp, pl, "status CTIO complete");
1166 		} else {
1167 			/*
1168 			 * Final CTIO completed. Release DMA resources and
1169 			 * notify platform dependent layers.
1170 			 */
1171 			isp_prt(isp, pl, "data CTIO complete");
1172 		}
1173 		(void) isp_async(isp, ISPASYNC_TARGET_ACTION, ct);
1174 		/*
1175 		 * The platform layer will destroy the handle if appropriate.
1176 		 */
1177 	}
1178 }
1179 #endif
1180