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