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