isp.c revision 45040
1/* $Id: isp.c,v 1.14 1999/03/17 05:04:38 mjacob Exp $ */
2/* release_03_25_99 */
3/*
4 * Machine and OS Independent (well, as best as possible)
5 * code for the Qlogic ISP SCSI adapters.
6 *
7 * Copyright (c) 1997, 1998 by Matthew Jacob
8 * NASA/Ames Research Center
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice immediately at the beginning of the file, without modification,
16 *    this list of conditions, and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 * 3. The name of the author may not be used to endorse or promote products
21 *    derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
27 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36/*
37 * Inspiration and ideas about this driver are from Erik Moe's Linux driver
38 * (qlogicisp.c) and Dave Miller's SBus version of same (qlogicisp.c). Some
39 * ideas dredged from the Solaris driver.
40 */
41
42/*
43 * Include header file appropriate for platform we're building on.
44 */
45
46#ifdef	__NetBSD__
47#include <dev/ic/isp_netbsd.h>
48#endif
49#ifdef	__FreeBSD__
50#include <dev/isp/isp_freebsd.h>
51#endif
52#ifdef	__OpenBSD__
53#include <dev/ic/isp_openbsd.h>
54#endif
55#ifdef	__linux__
56#include "isp_linux.h"
57#endif
58
59/*
60 * General defines
61 */
62
63#define	MBOX_DELAY_COUNT	1000000 / 100
64
65/*
66 * Local static data
67 */
68#ifdef	ISP_TARGET_MODE
69static const char tgtiqd[36] = {
70	0x03, 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00,
71	0x51, 0x4C, 0x4F, 0x47, 0x49, 0x43, 0x20, 0x20,
72#ifdef	__NetBSD__
73	0x4E, 0x45, 0x54, 0x42, 0x53, 0x44, 0x20, 0x20,
74#else
75# ifdef	__FreeBSD__
76	0x46, 0x52, 0x45, 0x45, 0x42, 0x52, 0x44, 0x20,
77# else
78#  ifdef __OpenBSD__
79	0x4F, 0x50, 0x45, 0x4E, 0x42, 0x52, 0x44, 0x20,
80#  else
81#   ifdef linux
82	0x4C, 0x49, 0x4E, 0x55, 0x58, 0x20, 0x20, 0x20,
83#   else
84#   endif
85#  endif
86# endif
87#endif
88	0x54, 0x41, 0x52, 0x47, 0x45, 0x54, 0x20, 0x20,
89	0x20, 0x20, 0x20, 0x31
90};
91#endif
92
93
94/*
95 * Local function prototypes.
96 */
97static int isp_parse_async __P((struct ispsoftc *, int));
98static int isp_handle_other_response
99__P((struct ispsoftc *, ispstatusreq_t *, u_int8_t *));
100#ifdef	ISP_TARGET_MODE
101static int isp_modify_lun __P((struct ispsoftc *, int, int, int));
102static void isp_notify_ack __P((struct ispsoftc *, void *));
103static void isp_handle_atio __P((struct ispsoftc *, void *));
104static void isp_handle_atio2 __P((struct ispsoftc *, void *));
105static void isp_handle_ctio __P((struct ispsoftc *, void *));
106static void isp_handle_ctio2 __P((struct ispsoftc *, void *));
107#endif
108static void isp_parse_status
109__P((struct ispsoftc *, ispstatusreq_t *, ISP_SCSI_XFER_T *));
110static void isp_fastpost_complete __P((struct ispsoftc *, int));
111static void isp_fibre_init __P((struct ispsoftc *));
112static void isp_mark_getpdb_all __P((struct ispsoftc *));
113static int isp_getpdb __P((struct ispsoftc *, int, isp_pdb_t *));
114static int isp_fclink_test __P((struct ispsoftc *, int));
115static void isp_fw_state __P((struct ispsoftc *));
116static void isp_dumpregs __P((struct ispsoftc *, const char *));
117static void isp_dumpxflist __P((struct ispsoftc *));
118static void isp_mboxcmd __P((struct ispsoftc *, mbreg_t *));
119
120static void isp_update  __P((struct ispsoftc *));
121static void isp_setdfltparm __P((struct ispsoftc *));
122static int isp_read_nvram __P((struct ispsoftc *));
123static void isp_rdnvram_word __P((struct ispsoftc *, int, u_int16_t *));
124
125/*
126 * Reset Hardware.
127 *
128 * Hit the chip over the head, download new f/w and set it running.
129 *
130 * Locking done elsewhere.
131 */
132void
133isp_reset(isp)
134	struct ispsoftc *isp;
135{
136	mbreg_t mbs;
137	int loops, i, dodnld = 1;
138	char *revname;
139
140	isp->isp_state = ISP_NILSTATE;
141
142	/*
143	 * Basic types (SCSI, FibreChannel and PCI or SBus)
144	 * have been set in the MD code. We figure out more
145	 * here.
146	 */
147	isp->isp_dblev = DFLT_DBLEVEL;
148
149	/*
150	 * After we've fired this chip up, zero out the conf1 register
151	 * for SCSI adapters and other settings for the 2100.
152	 */
153
154	/*
155	 * Get the current running firmware revision out of the
156	 * chip before we hit it over the head (if this is our
157	 * first time through). Note that we store this as the
158	 * 'ROM' firmware revision- which it may not be. In any
159	 * case, we don't really use this yet, but we may in
160	 * the future.
161	 */
162	if (isp->isp_used == 0) {
163		/*
164		 * Just in case it was paused...
165		 */
166		ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
167		mbs.param[0] = MBOX_ABOUT_FIRMWARE;
168		isp_mboxcmd(isp, &mbs);
169		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
170			/*
171			 * If this fails, it probably means we're running
172			 * an old prom, if anything at all...
173			 */
174			isp->isp_romfw_rev = 0;
175		} else {
176			isp->isp_romfw_rev =
177			    (((u_int16_t) mbs.param[1]) << 10) + mbs.param[2];
178		}
179		isp->isp_used = 1;
180	}
181
182	/*
183	 * Put it into PAUSE mode.
184	 */
185	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
186
187	if (IS_FC(isp)) {
188		revname = "2100";
189	} else if (IS_1080(isp)) {
190		u_int16_t l;
191		sdparam *sdp = isp->isp_param;
192		revname = "1080";
193		sdp->isp_clock = 100;
194		l = ISP_READ(isp, SXP_PINS_DIFF) & ISP1080_MODE_MASK;
195		switch (l) {
196		case ISP1080_LVD_MODE:
197			sdp->isp_lvdmode = 1;
198			PRINTF("%s: LVD Mode\n", isp->isp_name);
199			break;
200		case ISP1080_HVD_MODE:
201			sdp->isp_diffmode = 1;
202			PRINTF("%s: Differential Mode\n", isp->isp_name);
203			break;
204		case ISP1080_SE_MODE:
205			sdp->isp_ultramode = 1;
206			PRINTF("%s: Single-Ended Mode\n", isp->isp_name);
207			break;
208		default:
209			/*
210			 * Hmm. Up in a wierd mode. This means all SCSI I/O
211			 * buffer lines are tristated, so we're in a lot of
212			 * trouble if we don't set things up right.
213			 */
214			PRINTF("%s: Illegal Mode 0x%x\n", isp->isp_name, l);
215			break;
216		}
217	} else {
218		sdparam *sdp = isp->isp_param;
219		i = ISP_READ(isp, BIU_CONF0) & BIU_CONF0_HW_MASK;
220		switch (i) {
221		default:
222			PRINTF("%s: unknown chip rev. 0x%x- assuming a 1020\n",
223			    isp->isp_name, i);
224			/* FALLTHROUGH */
225		case 1:
226			revname = "1020";
227			isp->isp_type = ISP_HA_SCSI_1020;
228			sdp->isp_clock = 40;
229			break;
230		case 2:
231			/*
232			 * Some 1020A chips are Ultra Capable, but don't
233			 * run the clock rate up for that unless told to
234			 * do so by the Ultra Capable bits being set.
235			 */
236			revname = "1020A";
237			isp->isp_type = ISP_HA_SCSI_1020A;
238			sdp->isp_clock = 40;
239			break;
240		case 3:
241			revname = "1040";
242			isp->isp_type = ISP_HA_SCSI_1040;
243			sdp->isp_clock = 60;
244			break;
245		case 4:
246			revname = "1040A";
247			isp->isp_type = ISP_HA_SCSI_1040A;
248			sdp->isp_clock = 60;
249			break;
250		case 5:
251			revname = "1040B";
252			isp->isp_type = ISP_HA_SCSI_1040B;
253			sdp->isp_clock = 60;
254			break;
255		}
256		/*
257		 * Now, while we're at it, gather info about ultra
258		 * and/or differential mode.
259		 */
260		if (ISP_READ(isp, SXP_PINS_DIFF) & SXP_PINS_DIFF_MODE) {
261			PRINTF("%s: Differential Mode\n", isp->isp_name);
262			sdp->isp_diffmode = 1;
263		} else {
264			sdp->isp_diffmode = 0;
265		}
266		i = ISP_READ(isp, RISC_PSR);
267		if (isp->isp_bustype == ISP_BT_SBUS) {
268			i &= RISC_PSR_SBUS_ULTRA;
269		} else {
270			i &= RISC_PSR_PCI_ULTRA;
271		}
272		if (i != 0) {
273			PRINTF("%s: Ultra Mode Capable\n", isp->isp_name);
274			sdp->isp_ultramode = 1;
275			/*
276			 * If we're in Ultra Mode, we have to be 60Mhz clock-
277			 * even for the SBus version.
278			 */
279			sdp->isp_clock = 60;
280		} else {
281			sdp->isp_ultramode = 0;
282			/*
283			 * Clock is known. Gronk.
284			 */
285		}
286
287		/*
288		 * Machine dependent clock (if set) overrides
289		 * our generic determinations.
290		 */
291		if (isp->isp_mdvec->dv_clock) {
292			if (isp->isp_mdvec->dv_clock < sdp->isp_clock) {
293				sdp->isp_clock = isp->isp_mdvec->dv_clock;
294			}
295		}
296
297	}
298
299	/*
300	 * Do MD specific pre initialization
301	 */
302	ISP_RESET0(isp);
303
304again:
305
306	/*
307	 * Hit the chip over the head with hammer,
308	 * and give the ISP a chance to recover.
309	 */
310
311	if (IS_SCSI(isp)) {
312		ISP_WRITE(isp, BIU_ICR, BIU_ICR_SOFT_RESET);
313		/*
314		 * A slight delay...
315		 */
316		SYS_DELAY(100);
317
318#if	0
319		PRINTF("%s: mbox0-5: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
320		    isp->isp_name, ISP_READ(isp, OUTMAILBOX0),
321		    ISP_READ(isp, OUTMAILBOX1), ISP_READ(isp, OUTMAILBOX2),
322		    ISP_READ(isp, OUTMAILBOX3), ISP_READ(isp, OUTMAILBOX4),
323		    ISP_READ(isp, OUTMAILBOX5));
324#endif
325
326		/*
327		 * Clear data && control DMA engines.
328		 */
329		ISP_WRITE(isp, CDMA_CONTROL,
330		    DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
331		ISP_WRITE(isp, DDMA_CONTROL,
332		    DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
333
334
335	} else {
336		ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
337		/*
338		 * A slight delay...
339		 */
340		SYS_DELAY(100);
341
342		/*
343		 * Clear data && control DMA engines.
344		 */
345		ISP_WRITE(isp, CDMA2100_CONTROL,
346			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
347		ISP_WRITE(isp, TDMA2100_CONTROL,
348			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
349		ISP_WRITE(isp, RDMA2100_CONTROL,
350			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
351	}
352
353	/*
354	 * Wait for ISP to be ready to go...
355	 */
356	loops = MBOX_DELAY_COUNT;
357	for (;;) {
358		if (isp->isp_type & ISP_HA_SCSI) {
359			if (!(ISP_READ(isp, BIU_ICR) & BIU_ICR_SOFT_RESET))
360				break;
361		} else {
362			if (!(ISP_READ(isp, BIU2100_CSR) & BIU2100_SOFT_RESET))
363				break;
364		}
365		SYS_DELAY(100);
366		if (--loops < 0) {
367			isp_dumpregs(isp, "chip reset timed out");
368			return;
369		}
370	}
371
372	/*
373	 * After we've fired this chip up, zero out the conf1 register
374	 * for SCSI adapters and other settings for the 2100.
375	 */
376
377	if (IS_SCSI(isp)) {
378		ISP_WRITE(isp, BIU_CONF1, 0);
379	} else {
380		ISP_WRITE(isp, BIU2100_CSR, 0);
381	}
382
383	/*
384	 * Reset RISC Processor
385	 */
386	ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
387	SYS_DELAY(100);
388
389	/*
390	 * Establish some initial burst rate stuff.
391	 * (only for the 1XX0 boards). This really should
392	 * be done later after fetching from NVRAM.
393	 */
394	if (IS_SCSI(isp)) {
395		u_int16_t tmp = isp->isp_mdvec->dv_conf1;
396		/*
397		 * Busted FIFO. Turn off all but burst enables.
398		 */
399		if (isp->isp_type == ISP_HA_SCSI_1040A) {
400			tmp &= BIU_BURST_ENABLE;
401		}
402		ISP_SETBITS(isp, BIU_CONF1, tmp);
403		if (tmp & BIU_BURST_ENABLE) {
404			ISP_SETBITS(isp, CDMA_CONF, DMA_ENABLE_BURST);
405			ISP_SETBITS(isp, DDMA_CONF, DMA_ENABLE_BURST);
406		}
407#ifdef	PTI_CARDS
408		if (((sdparam *) isp->isp_param)->isp_ultramode) {
409			while (ISP_READ(isp, RISC_MTR) != 0x1313) {
410				ISP_WRITE(isp, RISC_MTR, 0x1313);
411				ISP_WRITE(isp, HCCR, HCCR_CMD_STEP);
412			}
413		} else {
414			ISP_WRITE(isp, RISC_MTR, 0x1212);
415		}
416		/*
417		 * PTI specific register
418		 */
419		ISP_WRITE(isp, RISC_EMB, DUAL_BANK)
420#else
421		ISP_WRITE(isp, RISC_MTR, 0x1212);
422#endif
423	} else {
424		ISP_WRITE(isp, RISC_MTR2100, 0x1212);
425	}
426
427	ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE); /* release paused processor */
428
429	/*
430	 * Do MD specific post initialization
431	 */
432	ISP_RESET1(isp);
433
434	/*
435	 * Enable interrupts
436	 */
437	ENABLE_INTS(isp);
438
439	/*
440	 * Wait for everything to finish firing up...
441	 */
442	loops = MBOX_DELAY_COUNT;
443	while (ISP_READ(isp, OUTMAILBOX0) == MBOX_BUSY) {
444		SYS_DELAY(100);
445		if (--loops < 0) {
446			PRINTF("%s: MBOX_BUSY never cleared on reset\n",
447			    isp->isp_name);
448			return;
449		}
450	}
451
452	/*
453	 * Up until this point we've done everything by just reading or
454	 * setting registers. From this point on we rely on at least *some*
455	 * kind of firmware running in the card.
456	 */
457
458	/*
459	 * Do some sanity checking.
460	 */
461	mbs.param[0] = MBOX_NO_OP;
462	isp_mboxcmd(isp, &mbs);
463	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
464		isp_dumpregs(isp, "NOP test failed");
465		return;
466	}
467
468	if (isp->isp_type & ISP_HA_SCSI) {
469		mbs.param[0] = MBOX_MAILBOX_REG_TEST;
470		mbs.param[1] = 0xdead;
471		mbs.param[2] = 0xbeef;
472		mbs.param[3] = 0xffff;
473		mbs.param[4] = 0x1111;
474		mbs.param[5] = 0xa5a5;
475		isp_mboxcmd(isp, &mbs);
476		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
477			isp_dumpregs(isp,
478				"Mailbox Register test didn't complete");
479			return;
480		}
481		if (mbs.param[1] != 0xdead || mbs.param[2] != 0xbeef ||
482		    mbs.param[3] != 0xffff || mbs.param[4] != 0x1111 ||
483		    mbs.param[5] != 0xa5a5) {
484			isp_dumpregs(isp, "Register Test Failed");
485			return;
486		}
487
488	}
489
490	/*
491	 * Download new Firmware, unless requested not to do so.
492	 * This is made slightly trickier in some cases where the
493	 * firmware of the ROM revision is newer than the revision
494	 * compiled into the driver. So, where we used to compare
495	 * versions of our f/w and the ROM f/w, now we just see
496	 * whether we have f/w at all and whether a config flag
497	 * has disabled our download.
498	 */
499	if ((isp->isp_mdvec->dv_fwlen == 0) ||
500	    (isp->isp_confopts & ISP_CFG_NORELOAD)) {
501		dodnld = 0;
502	}
503
504	if (dodnld && isp->isp_mdvec->dv_fwlen) {
505		for (i = 0; i < isp->isp_mdvec->dv_fwlen; i++) {
506			mbs.param[0] = MBOX_WRITE_RAM_WORD;
507			mbs.param[1] = isp->isp_mdvec->dv_codeorg + i;
508			mbs.param[2] = isp->isp_mdvec->dv_ispfw[i];
509			isp_mboxcmd(isp, &mbs);
510			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
511				PRINTF("%s: F/W download failed at word %d\n",
512				    isp->isp_name, i);
513				dodnld = 0;
514				goto again;
515			}
516		}
517
518		/*
519		 * Verify that it downloaded correctly.
520		 */
521		mbs.param[0] = MBOX_VERIFY_CHECKSUM;
522		mbs.param[1] = isp->isp_mdvec->dv_codeorg;
523		isp_mboxcmd(isp, &mbs);
524		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
525			isp_dumpregs(isp, "ram checksum failure");
526			return;
527		}
528	} else {
529		IDPRINTF(3, ("%s: skipping f/w download\n", isp->isp_name));
530	}
531
532	/*
533	 * Now start it rolling.
534	 *
535	 * If we didn't actually download f/w,
536	 * we still need to (re)start it.
537	 */
538
539	mbs.param[0] = MBOX_EXEC_FIRMWARE;
540	if (isp->isp_mdvec->dv_codeorg)
541		mbs.param[1] = isp->isp_mdvec->dv_codeorg;
542	else
543		mbs.param[1] = 0x1000;
544	isp_mboxcmd(isp, &mbs);
545
546	if (isp->isp_type & ISP_HA_SCSI) {
547		sdparam *sdp = isp->isp_param;
548		/*
549		 * Set CLOCK RATE, but only if asked to.
550		 */
551		if (sdp->isp_clock) {
552			mbs.param[0] = MBOX_SET_CLOCK_RATE;
553			mbs.param[1] = sdp->isp_clock;
554			isp_mboxcmd(isp, &mbs);
555			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
556				isp_dumpregs(isp, "failed to set CLOCKRATE");
557				/* but continue */
558			} else {
559				IDPRINTF(3, ("%s: setting input clock to %d\n",
560				    isp->isp_name, sdp->isp_clock));
561			}
562		}
563	}
564	mbs.param[0] = MBOX_ABOUT_FIRMWARE;
565	isp_mboxcmd(isp, &mbs);
566	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
567		isp_dumpregs(isp, "ABOUT FIRMWARE command failed");
568		return;
569	}
570	PRINTF("%s: Board Revision %s, %s F/W Revision %d.%d\n",
571		isp->isp_name, revname, dodnld? "loaded" : "resident",
572		mbs.param[1], mbs.param[2]);
573	if (IS_FC(isp)) {
574		if (ISP_READ(isp, BIU2100_CSR) & BIU2100_PCI64) {
575			PRINTF("%s: in 64-Bit PCI slot\n", isp->isp_name);
576		}
577	}
578	isp->isp_fwrev = (((u_int16_t) mbs.param[1]) << 10) + mbs.param[2];
579	if (isp->isp_romfw_rev && dodnld) {
580		PRINTF("%s: Last F/W revision was %d.%d\n", isp->isp_name,
581		    isp->isp_romfw_rev >> 10, isp->isp_romfw_rev & 0x3ff);
582	}
583	isp_fw_state(isp);
584	isp->isp_state = ISP_RESETSTATE;
585}
586
587/*
588 * Initialize Parameters of Hardware to a known state.
589 *
590 * Locks are held before coming here.
591 */
592
593void
594isp_init(isp)
595	struct ispsoftc *isp;
596{
597	sdparam *sdp;
598	mbreg_t mbs;
599	int tgt;
600
601	/*
602	 * Must do first.
603	 */
604	isp_setdfltparm(isp);
605
606	/*
607	 * Set up DMA for the request and result mailboxes.
608	 */
609	if (ISP_MBOXDMASETUP(isp) != 0) {
610		PRINTF("%s: can't setup dma mailboxes\n", isp->isp_name);
611		return;
612	}
613
614	/*
615	 * If we're fibre, we have a completely different
616	 * initialization method.
617	 */
618	if (IS_FC(isp)) {
619		isp_fibre_init(isp);
620		return;
621	}
622	sdp = isp->isp_param;
623
624	/*
625	 * If we have fast memory timing enabled, turn it on.
626	 */
627	if (sdp->isp_fast_mttr) {
628		ISP_WRITE(isp, RISC_MTR, 0x1313);
629	}
630
631	/*
632	 * Set (possibly new) Initiator ID.
633	 */
634	mbs.param[0] = MBOX_SET_INIT_SCSI_ID;
635	mbs.param[1] = sdp->isp_initiator_id;
636	isp_mboxcmd(isp, &mbs);
637	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
638		isp_dumpregs(isp, "failed to set initiator id");
639		return;
640	}
641
642	/*
643	 * Set Retry Delay and Count
644	 */
645	mbs.param[0] = MBOX_SET_RETRY_COUNT;
646	mbs.param[1] = sdp->isp_retry_count;
647	mbs.param[2] = sdp->isp_retry_delay;
648	isp_mboxcmd(isp, &mbs);
649	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
650		isp_dumpregs(isp, "failed to set retry count and delay");
651		return;
652	}
653
654	/*
655	 * Set ASYNC DATA SETUP time. This is very important.
656	 */
657	mbs.param[0] = MBOX_SET_ASYNC_DATA_SETUP_TIME;
658	mbs.param[1] = sdp->isp_async_data_setup;
659	isp_mboxcmd(isp, &mbs);
660	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
661		isp_dumpregs(isp, "failed to set async data setup time");
662		return;
663	}
664
665	/*
666	 * Set ACTIVE Negation State.
667	 */
668	mbs.param[0] = MBOX_SET_ACTIVE_NEG_STATE;
669	mbs.param[1] =
670	    (sdp->isp_req_ack_active_neg << 4) |
671	    (sdp->isp_data_line_active_neg << 5);
672	isp_mboxcmd(isp, &mbs);
673	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
674		isp_dumpregs(isp, "failed to set active neg state");
675		return;
676	}
677
678	/*
679	 * Set the Tag Aging limit
680	 */
681
682	mbs.param[0] = MBOX_SET_TAG_AGE_LIMIT;
683	mbs.param[1] = sdp->isp_tag_aging;
684	isp_mboxcmd(isp, &mbs);
685	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
686		isp_dumpregs(isp, "failed to set tag age limit");
687		return;
688	}
689
690	/*
691	 * Set selection timeout.
692	 */
693
694	mbs.param[0] = MBOX_SET_SELECT_TIMEOUT;
695	mbs.param[1] = sdp->isp_selection_timeout;
696	isp_mboxcmd(isp, &mbs);
697	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
698		isp_dumpregs(isp, "failed to set selection timeout");
699		return;
700	}
701
702	/*
703	 * Set current per-target parameters to a safe minimum.
704	 */
705	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
706		int maxlun, lun;
707		u_int16_t sdf;
708
709		if (sdp->isp_devparam[tgt].dev_enable == 0)
710			continue;
711
712		sdf = DPARM_SAFE_DFLT;
713		/*
714		 * It is not quite clear when this changed over so that
715		 * we could force narrow and async, so assume >= 7.55.
716		 */
717		if (isp->isp_fwrev >= ISP_FW_REV(7, 55)) {
718			sdf |= DPARM_NARROW | DPARM_ASYNC;
719		}
720		mbs.param[0] = MBOX_SET_TARGET_PARAMS;
721		mbs.param[1] = tgt << 8;
722		mbs.param[2] = sdf;
723		mbs.param[3] =
724		    (sdp->isp_devparam[tgt].sync_offset << 8) |
725		    (sdp->isp_devparam[tgt].sync_period);
726		isp_mboxcmd(isp, &mbs);
727		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
728			sdf = DPARM_SAFE_DFLT;
729			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
730			mbs.param[1] = tgt << 8;
731			mbs.param[2] = sdf;
732			mbs.param[3] =
733			    (sdp->isp_devparam[tgt].sync_offset << 8) |
734			    (sdp->isp_devparam[tgt].sync_period);
735			isp_mboxcmd(isp, &mbs);
736			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
737				PRINTF("%s: failed even to set defaults for "
738				    "target %d\n", isp->isp_name, tgt);
739				continue;
740			}
741		}
742		/*
743		 * We don't update dev_flags with what we've set
744		 * because that's not the ultimate goal setting.
745		 * If we succeed with the command, we *do* update
746		 * cur_dflags.
747		 */
748		mbs.param[0] = MBOX_GET_TARGET_PARAMS;
749		mbs.param[1] = (tgt << 8);
750		isp_mboxcmd(isp, &mbs);
751		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
752			/*
753			 * Urrr.... We'll set cur_dflags to DPARM_SAFE_DFLT so
754			 * we don't try and do tags if tags aren't enabled.
755			 */
756			sdp->isp_devparam[tgt].cur_dflags = DPARM_SAFE_DFLT;
757		} else {
758			sdp->isp_devparam[tgt].cur_dflags = mbs.param[2];
759			sdp->isp_devparam[tgt].cur_offset = mbs.param[3] >> 8;
760			sdp->isp_devparam[tgt].cur_period = mbs.param[3] & 0xff;
761		}
762		maxlun = (isp->isp_fwrev >= ISP_FW_REV(7, 55))? 32 : 8;
763		for (lun = 0; lun < maxlun; lun++) {
764			mbs.param[0] = MBOX_SET_DEV_QUEUE_PARAMS;
765			mbs.param[1] = (tgt << 8) | lun;
766			mbs.param[2] = sdp->isp_max_queue_depth;
767			mbs.param[3] = sdp->isp_devparam[tgt].exc_throttle;
768			isp_mboxcmd(isp, &mbs);
769			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
770				PRINTF("%s: failed to set device queue "
771				    "parameters for target %d, lun %d\n",
772				    isp->isp_name, tgt, lun);
773				break;
774			}
775		}
776		/*
777		 * And mark this as an unannounced device
778		 */
779		sdp->isp_devparam[tgt].dev_announced = 0;
780	}
781
782	mbs.param[0] = MBOX_INIT_RES_QUEUE;
783	mbs.param[1] = RESULT_QUEUE_LEN;
784	mbs.param[2] = (u_int16_t) (isp->isp_result_dma >> 16);
785	mbs.param[3] = (u_int16_t) (isp->isp_result_dma & 0xffff);
786	mbs.param[4] = 0;
787	mbs.param[5] = 0;
788	isp_mboxcmd(isp, &mbs);
789	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
790		isp_dumpregs(isp, "set of response queue failed");
791		return;
792	}
793	isp->isp_residx = 0;
794
795	mbs.param[0] = MBOX_INIT_REQ_QUEUE;
796	mbs.param[1] = RQUEST_QUEUE_LEN;
797	mbs.param[2] = (u_int16_t) (isp->isp_rquest_dma >> 16);
798	mbs.param[3] = (u_int16_t) (isp->isp_rquest_dma & 0xffff);
799	mbs.param[4] = 0;
800	mbs.param[5] = 0;
801	isp_mboxcmd(isp, &mbs);
802	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
803		isp_dumpregs(isp, "set of request queue failed");
804		return;
805	}
806	isp->isp_reqidx = isp->isp_reqodx = 0;
807
808	/*
809	 *  Turn on Fast Posting, LVD transitions
810	 */
811
812	if (IS_1080(isp) || isp->isp_fwrev >= ISP_FW_REV(7, 55)) {
813		mbs.param[0] = MBOX_SET_FW_FEATURES;
814#ifndef	ISP_NO_FASTPOST_SCSI
815		mbs.param[1] |= FW_FEATURE_FAST_POST;
816#else
817		mbs.param[1] = 0;
818#endif
819		if (IS_1080(isp))
820			mbs.param[1] |= FW_FEATURE_LVD_NOTIFY;
821		if (mbs.param[1] != 0) {
822			isp_mboxcmd(isp, &mbs);
823			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
824				PRINTF("%s: unable enable FW features\n",
825				    isp->isp_name);
826			}
827		}
828	}
829
830	/*
831	 * Let the outer layers decide whether to issue a SCSI bus reset.
832	 */
833#if	0
834	/*
835	 * XXX: See whether or not for 7.55 F/W or later we
836	 * XXX: can do without this, and see whether we should
837	 * XXX: honor the NVRAM SCSI_RESET_DISABLE token.
838	 */
839	mbs.param[0] = MBOX_BUS_RESET;
840	mbs.param[1] = 3;
841	isp_mboxcmd(isp, &mbs);
842	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
843		isp_dumpregs(isp, "SCSI bus reset failed");
844	}
845	/*
846	 * This is really important to have set after a bus reset.
847	 */
848	isp->isp_sendmarker = 1;
849#endif
850	isp->isp_state = ISP_INITSTATE;
851}
852
853/*
854 * Fibre Channel specific initialization.
855 *
856 * Locks are held before coming here.
857 */
858static void
859isp_fibre_init(isp)
860	struct ispsoftc *isp;
861{
862	fcparam *fcp;
863	isp_icb_t *icbp;
864	mbreg_t mbs;
865	int count, loopid;
866
867	fcp = isp->isp_param;
868
869	/*
870	 * For systems that don't have BIOS methods for which
871	 * we can easily change the NVRAM based loopid, we'll
872	 * override that here. Note that when we initialize
873	 * the firmware we may get back a different loopid than
874	 * we asked for anyway. XXX This is probably not the
875	 * best way to figure this out XXX
876	 */
877#ifndef	__i386__
878	loopid = DEFAULT_LOOPID;
879#else
880	loopid = fcp->isp_loopid;
881#endif
882
883#if	defined(ISP2100_FABRIC) && defined(ISP2100_SCCLUN)
884	PRINTF("%s: Fabric Support, Expanded Lun Support\n", isp->isp_name);
885#endif
886#if	defined(ISP2100_FABRIC) && !defined(ISP2100_SCCLUN)
887	PRINTF("%s: Fabric Support\n", isp->isp_name);
888#endif
889#if	!defined(ISP2100_FABRIC) && defined(ISP2100_SCCLUN)
890	PRINTF("%s: Expanded Lun Support\n", isp->isp_name);
891#endif
892
893	icbp = (isp_icb_t *) fcp->isp_scratch;
894	MEMZERO(icbp, sizeof (*icbp));
895
896	icbp->icb_version = ICB_VERSION1;
897#ifdef	ISP_TARGET_MODE
898	fcp->isp_fwoptions = ICBOPT_TGT_ENABLE|ICBOPT_INI_TGTTYPE;
899#else
900	fcp->isp_fwoptions = 0;
901#endif
902	fcp->isp_fwoptions |= ICBOPT_INI_ADISC|ICBOPT_FAIRNESS;
903	fcp->isp_fwoptions |= ICBOPT_PDBCHANGE_AE;
904	fcp->isp_fwoptions |= ICBOPT_HARD_ADDRESS;
905#ifndef	ISP_NO_FASTPOST_FC
906	fcp->isp_fwoptions |= ICBOPT_FAST_POST;
907#endif
908#ifdef	CHECKME
909	fcp->isp_fwoptions |= ICBOPT_USE_PORTNAME;
910#endif
911#ifdef	ISP2100_FABRIC
912	fcp->isp_fwoptions |= ICBOPT_FULL_LOGIN;
913#endif
914
915	icbp->icb_fwoptions = fcp->isp_fwoptions;
916	icbp->icb_maxfrmlen = fcp->isp_maxfrmlen;
917	if (icbp->icb_maxfrmlen < ICB_MIN_FRMLEN ||
918	    icbp->icb_maxfrmlen > ICB_MAX_FRMLEN) {
919		PRINTF("%s: bad frame length (%d) from NVRAM- using %d\n",
920		    isp->isp_name, fcp->isp_maxfrmlen, ICB_DFLT_FRMLEN);
921	}
922	icbp->icb_maxalloc = fcp->isp_maxalloc;
923	icbp->icb_execthrottle = fcp->isp_execthrottle;
924	icbp->icb_retry_delay = fcp->isp_retry_delay;
925	icbp->icb_retry_count = fcp->isp_retry_count;
926	icbp->icb_hardaddr = loopid;
927
928	MAKE_NODE_NAME_FROM_WWN(icbp->icb_nodename, fcp->isp_wwn);
929	if (icbp->icb_fwoptions & ICBOPT_USE_PORTNAME) {
930		u_int64_t portname = fcp->isp_wwn | (2LL << 56);
931		MAKE_NODE_NAME_FROM_WWN(icbp->icb_nodename, portname);
932	}
933	icbp->icb_rqstqlen = RQUEST_QUEUE_LEN;
934	icbp->icb_rsltqlen = RESULT_QUEUE_LEN;
935	icbp->icb_rqstaddr[RQRSP_ADDR0015] =
936	    (u_int16_t) (isp->isp_rquest_dma & 0xffff);
937	icbp->icb_rqstaddr[RQRSP_ADDR1631] =
938	    (u_int16_t) (isp->isp_rquest_dma >> 16);
939	icbp->icb_respaddr[RQRSP_ADDR0015] =
940	    (u_int16_t) (isp->isp_result_dma & 0xffff);
941	icbp->icb_respaddr[RQRSP_ADDR1631] =
942	    (u_int16_t) (isp->isp_result_dma >> 16);
943	MemoryBarrier();
944
945	for (count = 0; count < 10; count++) {
946		mbs.param[0] = MBOX_INIT_FIRMWARE;
947		mbs.param[1] = 0;
948		mbs.param[2] = (u_int16_t) (fcp->isp_scdma >> 16);
949		mbs.param[3] = (u_int16_t) (fcp->isp_scdma & 0xffff);
950		mbs.param[4] = 0;
951		mbs.param[5] = 0;
952		mbs.param[6] = 0;
953		mbs.param[7] = 0;
954
955		isp_mboxcmd(isp, &mbs);
956
957		switch (mbs.param[0]) {
958		case MBOX_COMMAND_COMPLETE:
959			count = 10;
960			break;
961		case ASYNC_PDB_CHANGED:
962			isp_mark_getpdb_all(isp);
963			/* FALL THROUGH */
964		case ASYNC_LIP_OCCURRED:
965		case ASYNC_LOOP_UP:
966		case ASYNC_LOOP_DOWN:
967		case ASYNC_LOOP_RESET:
968		case ASYNC_CHANGE_NOTIFY:
969			if (count > 9) {
970				PRINTF("%s: too many retries to get going- "
971				    "giving up\n", isp->isp_name);
972				return;
973			}
974			break;
975		default:
976			isp_dumpregs(isp, "INIT FIRMWARE failed");
977			return;
978		}
979	}
980	isp->isp_reqidx = isp->isp_reqodx = 0;
981	isp->isp_residx = 0;
982	isp->isp_sendmarker = 1;
983
984	/*
985	 * Whatever happens, we're now committed to being here.
986	 */
987	isp->isp_state = ISP_INITSTATE;
988	fcp->isp_fwstate = FW_CONFIG_WAIT;
989
990	isp_mark_getpdb_all(isp);
991
992#ifdef	ISP_TARGET_MODE
993	if (isp_modify_lun(isp, 0, 1, 1)) {
994		PRINTF("%s: failed to enable target mode\n", isp->isp_name);
995	}
996#endif
997}
998
999/*
1000 * Fibre Channel Support- get the port database for the id.
1001 *
1002 * Locks are held before coming here. Return 0 if success,
1003 * else failure.
1004 */
1005
1006static void
1007isp_mark_getpdb_all(isp)
1008	struct ispsoftc *isp;
1009{
1010	isp_pdb_t *p;
1011	fcparam *fcp = (fcparam *) isp->isp_param;
1012	for (p = &fcp->isp_pdb[0]; p < &fcp->isp_pdb[MAX_FC_TARG]; p++) {
1013		p->pdb_options = INVALID_PDB_OPTIONS;
1014	}
1015}
1016
1017static int
1018isp_getpdb(isp, id, pdbp)
1019	struct ispsoftc *isp;
1020	int id;
1021	isp_pdb_t *pdbp;
1022{
1023	fcparam *fcp = (fcparam *) isp->isp_param;
1024	mbreg_t mbs;
1025	mbs.param[0] = MBOX_GET_PORT_DB;
1026	mbs.param[1] = id << 8;
1027	mbs.param[2] = (u_int16_t) (fcp->isp_scdma >> 16);
1028	mbs.param[3] = (u_int16_t) (fcp->isp_scdma & 0xffff);
1029	/*
1030	 * Unneeded. For the 2100, except for initializing f/w, registers
1031	 * 4/5 have to not be written to.
1032	 *	mbs.param[4] = 0;
1033	 *	mbs.param[5] = 0;
1034	 *
1035	 */
1036	mbs.param[6] = 0;
1037	mbs.param[7] = 0;
1038	isp_mboxcmd(isp, &mbs);
1039	switch (mbs.param[0]) {
1040	case MBOX_COMMAND_COMPLETE:
1041		MemoryBarrier();
1042		MEMCPY(pdbp, fcp->isp_scratch, sizeof (isp_pdb_t));
1043		break;
1044	case MBOX_HOST_INTERFACE_ERROR:
1045		PRINTF("%s: DMA error getting port database\n", isp->isp_name);
1046		return (-1);
1047	case MBOX_COMMAND_PARAM_ERROR:
1048		/* Not Logged In */
1049		IDPRINTF(3, ("%s: Comand Param Error on Get Port Database\n",
1050		    isp->isp_name));
1051		return (-1);
1052	default:
1053		PRINTF("%s: error 0x%x getting port database for ID %d\n",
1054		    isp->isp_name, mbs.param[0], id);
1055		return (-1);
1056	}
1057	return (0);
1058}
1059
1060/*
1061 * Make sure we have good FC link and know our Loop ID.
1062 */
1063
1064static int
1065isp_fclink_test(isp, waitdelay)
1066	struct ispsoftc *isp;
1067	int waitdelay;
1068{
1069	mbreg_t mbs;
1070	int count;
1071	u_int8_t lwfs;
1072	fcparam *fcp;
1073
1074	fcp = isp->isp_param;
1075
1076	/*
1077	 * Wait up to N microseconds for F/W to go to a ready state.
1078	 */
1079	lwfs = FW_CONFIG_WAIT;
1080	for (count = 0; count < waitdelay; count += 100) {
1081		isp_fw_state(isp);
1082		if (lwfs != fcp->isp_fwstate) {
1083			PRINTF("%s: Firmware State %s -> %s\n",
1084			    isp->isp_name, isp2100_fw_statename((int)lwfs),
1085			    isp2100_fw_statename((int)fcp->isp_fwstate));
1086			lwfs = fcp->isp_fwstate;
1087		}
1088		if (fcp->isp_fwstate == FW_READY) {
1089			break;
1090		}
1091		SYS_DELAY(100);	/* wait 100 microseconds */
1092	}
1093
1094	/*
1095	 * If we haven't gone to 'ready' state, return.
1096	 */
1097	if (fcp->isp_fwstate != FW_READY) {
1098		return (-1);
1099	}
1100
1101	/*
1102	 * Get our Loop ID (if possible). We really need to have it.
1103	 */
1104	mbs.param[0] = MBOX_GET_LOOP_ID;
1105	isp_mboxcmd(isp, &mbs);
1106	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1107		PRINTF("%s: GET LOOP ID failed\n", isp->isp_name);
1108		return (-1);
1109	}
1110	fcp->isp_loopid = mbs.param[1];
1111	fcp->isp_alpa = mbs.param[2];
1112	PRINTF("%s: Loop ID %d, ALPA 0x%x\n", isp->isp_name,
1113	    fcp->isp_loopid, fcp->isp_alpa);
1114	return (0);
1115
1116}
1117
1118/*
1119 * Start a command. Locking is assumed done in the caller.
1120 */
1121
1122int32_t
1123ispscsicmd(xs)
1124	ISP_SCSI_XFER_T *xs;
1125{
1126	struct ispsoftc *isp;
1127	u_int8_t iptr, optr;
1128	union {
1129		ispreq_t *_reqp;
1130		ispreqt2_t *_t2reqp;
1131	} _u;
1132#define	reqp	_u._reqp
1133#define	t2reqp	_u._t2reqp
1134#define	UZSIZE	max(sizeof (ispreq_t), sizeof (ispreqt2_t))
1135	int i, rqidx;
1136
1137	XS_INITERR(xs);
1138	isp = XS_ISP(xs);
1139
1140	if (isp->isp_state != ISP_RUNSTATE) {
1141		PRINTF("%s: adapter not ready\n", isp->isp_name);
1142		XS_SETERR(xs, HBA_BOTCH);
1143		return (CMD_COMPLETE);
1144	}
1145
1146	/*
1147	 * We *could* do the different sequence type that has close
1148	 * to the whole Queue Entry for the command...
1149	 */
1150
1151	if (XS_CDBLEN(xs) > ((isp->isp_type & ISP_HA_FC)? 16 : 12)) {
1152		PRINTF("%s: unsupported cdb length (%d)\n",
1153		    isp->isp_name, XS_CDBLEN(xs));
1154		XS_SETERR(xs, HBA_BOTCH);
1155		return (CMD_COMPLETE);
1156	}
1157
1158	/*
1159	 * Check to see whether we have good firmware state still or
1160	 * need to refresh our port database for this target.
1161	 */
1162	if (IS_FC(isp)) {
1163		fcparam *fcp = isp->isp_param;
1164		isp_pdb_t *pdbp = &fcp->isp_pdb[XS_TGT(xs)];
1165
1166		/*
1167		 * Check for f/w being in ready state. Well, okay,
1168		 * our cached copy of it...
1169		 */
1170		if (fcp->isp_fwstate != FW_READY) {
1171			if (isp_fclink_test(isp, FC_FW_READY_DELAY)) {
1172				XS_SETERR(xs, HBA_SELTIMEOUT);
1173				return (CMD_COMPLETE);
1174			}
1175		}
1176		/*
1177		 * Refresh our port database if needed.
1178		 */
1179		if (pdbp->pdb_options == INVALID_PDB_OPTIONS) {
1180			if (isp_getpdb(isp, XS_TGT(xs), pdbp) == 0) {
1181				isp_async(isp, ISPASYNC_PDB_CHANGE_COMPLETE,
1182				    (void *) (long) XS_TGT(xs));
1183			}
1184		}
1185	}
1186
1187	/*
1188	 * Next check to see if any HBA or Device
1189	 * parameters need to be updated.
1190	 */
1191	if (isp->isp_update) {
1192		isp_update(isp);
1193	}
1194
1195	optr = isp->isp_reqodx = ISP_READ(isp, OUTMAILBOX4);
1196	iptr = isp->isp_reqidx;
1197
1198	reqp = (ispreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, iptr);
1199	iptr = ISP_NXT_QENTRY(iptr, RQUEST_QUEUE_LEN);
1200	if (iptr == optr) {
1201		IDPRINTF(2, ("%s: Request Queue Overflow\n", isp->isp_name));
1202		XS_SETERR(xs, HBA_BOTCH);
1203		return (CMD_EAGAIN);
1204	}
1205
1206	if (isp->isp_sendmarker) {
1207		u_int8_t niptr;
1208		ispmarkreq_t *marker = (ispmarkreq_t *) reqp;
1209
1210		MEMZERO((void *) marker, sizeof (*marker));
1211		marker->req_header.rqs_entry_count = 1;
1212		marker->req_header.rqs_entry_type = RQSTYPE_MARKER;
1213		marker->req_modifier = SYNC_ALL;
1214
1215		/*
1216		 * Unconditionally update the input pointer anyway.
1217		 */
1218		ISP_WRITE(isp, INMAILBOX4, iptr);
1219		isp->isp_reqidx = iptr;
1220
1221		niptr = ISP_NXT_QENTRY(iptr, RQUEST_QUEUE_LEN);
1222		if (niptr == optr) {
1223			IDPRINTF(2, ("%s: Request Queue Overflow+\n",
1224			    isp->isp_name));
1225			XS_SETERR(xs, HBA_BOTCH);
1226			return (CMD_EAGAIN);
1227		}
1228		reqp = (ispreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, iptr);
1229		iptr = niptr;
1230	}
1231
1232	MEMZERO((void *) reqp, UZSIZE);
1233	reqp->req_header.rqs_entry_count = 1;
1234	if (isp->isp_type & ISP_HA_FC) {
1235		reqp->req_header.rqs_entry_type = RQSTYPE_T2RQS;
1236	} else {
1237		reqp->req_header.rqs_entry_type = RQSTYPE_REQUEST;
1238	}
1239	reqp->req_header.rqs_flags = 0;
1240	reqp->req_header.rqs_seqno = isp->isp_seqno++;
1241
1242	for (rqidx = 0; rqidx < RQUEST_QUEUE_LEN; rqidx++) {
1243		if (isp->isp_xflist[rqidx] == NULL)
1244			break;
1245	}
1246	if (rqidx == RQUEST_QUEUE_LEN) {
1247		IDPRINTF(2, ("%s: out of xflist pointers\n", isp->isp_name));
1248		XS_SETERR(xs, HBA_BOTCH);
1249		return (CMD_EAGAIN);
1250	} else {
1251		/*
1252		 * Never have a handle that is zero, so
1253		 * set req_handle off by one.
1254		 */
1255		isp->isp_xflist[rqidx] = xs;
1256		reqp->req_handle = rqidx+1;
1257	}
1258
1259	if (isp->isp_type & ISP_HA_FC) {
1260		/*
1261		 * See comment in isp_intr
1262		 */
1263		XS_RESID(xs) = 0;
1264
1265		/*
1266		 * Fibre Channel always requires some kind of tag.
1267		 * If we're marked as "Can't Tag", just do simple
1268		 * instead of ordered tags. It's pretty clear to me
1269		 * that we shouldn't do head of queue tagging in
1270		 * this case.
1271		 */
1272		if (XS_CANTAG(xs)) {
1273			t2reqp->req_flags = XS_KINDOF_TAG(xs);
1274		} else {
1275			t2reqp->req_flags = REQFLAG_STAG;
1276		}
1277	} else {
1278		sdparam *sdp = (sdparam *)isp->isp_param;
1279		if ((sdp->isp_devparam[XS_TGT(xs)].cur_dflags & DPARM_TQING) &&
1280		    XS_CANTAG(xs)) {
1281			reqp->req_flags = XS_KINDOF_TAG(xs);
1282		} else {
1283			reqp->req_flags = 0;
1284		}
1285	}
1286	reqp->req_target = XS_TGT(xs);
1287	if (isp->isp_type & ISP_HA_SCSI) {
1288		reqp->req_lun_trn = XS_LUN(xs);
1289		reqp->req_cdblen = XS_CDBLEN(xs);
1290	} else {
1291#ifdef	ISP2100_SCCLUN
1292		reqp->req_scclun = XS_LUN(xs);
1293#else
1294		reqp->req_lun_trn = XS_LUN(xs);
1295#endif
1296
1297	}
1298	MEMCPY(reqp->req_cdb, XS_CDBP(xs), XS_CDBLEN(xs));
1299
1300	IDPRINTF(5, ("%s(%d.%d): START%d cmd 0x%x datalen %d\n", isp->isp_name,
1301	    XS_TGT(xs), XS_LUN(xs), reqp->req_header.rqs_seqno,
1302	    reqp->req_cdb[0], XS_XFRLEN(xs)));
1303
1304	reqp->req_time = XS_TIME(xs) / 1000;
1305	if (reqp->req_time == 0 && XS_TIME(xs))
1306		reqp->req_time = 1;
1307
1308	/*
1309	 * Always give a bit more leeway to commands after a bus reset.
1310	 */
1311	if (isp->isp_sendmarker && reqp->req_time < 5)
1312		reqp->req_time = 5;
1313
1314	i = ISP_DMASETUP(isp, xs, reqp, &iptr, optr);
1315	if (i != CMD_QUEUED) {
1316		/*
1317		 * Take memory of it away...
1318		 */
1319		isp->isp_xflist[rqidx] = NULL;
1320		/*
1321		 * dmasetup sets actual error in packet, and
1322		 * return what we were given to return.
1323		 */
1324		return (i);
1325	}
1326	XS_SETERR(xs, HBA_NOERROR);
1327	MemoryBarrier();
1328	ISP_WRITE(isp, INMAILBOX4, iptr);
1329	isp->isp_reqidx = iptr;
1330	isp->isp_nactive++;
1331	if (isp->isp_sendmarker)
1332		isp->isp_sendmarker = 0;
1333	return (CMD_QUEUED);
1334#undef	reqp
1335#undef	t2reqp
1336}
1337
1338/*
1339 * isp control
1340 * Locks (ints blocked) assumed held.
1341 */
1342
1343int
1344isp_control(isp, ctl, arg)
1345	struct ispsoftc *isp;
1346	ispctl_t ctl;
1347	void *arg;
1348{
1349	ISP_SCSI_XFER_T *xs;
1350	mbreg_t mbs;
1351	int i;
1352
1353	switch (ctl) {
1354	default:
1355		PRINTF("%s: isp_control unknown control op %x\n",
1356		    isp->isp_name, ctl);
1357		break;
1358
1359	case ISPCTL_RESET_BUS:
1360		/*
1361		 * This is really important to have set after a bus reset.
1362		 */
1363		isp->isp_sendmarker = 1;
1364
1365		/*
1366		 * Issue a bus reset.
1367		 */
1368		mbs.param[0] = MBOX_BUS_RESET;
1369		if (isp->isp_type & ISP_HA_SCSI) {
1370			mbs.param[1] =
1371			    ((sdparam *) isp->isp_param)->isp_bus_reset_delay;
1372			if (mbs.param[1] < 2)
1373				mbs.param[1] = 2;
1374		} else {
1375			/*
1376			 * Unparameterized.
1377			 */
1378			mbs.param[1] = 5;
1379		}
1380		isp_mboxcmd(isp, &mbs);
1381		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1382			isp_dumpregs(isp, "isp_control SCSI bus reset failed");
1383			break;
1384		}
1385		PRINTF("%s: driver initiated bus reset\n", isp->isp_name);
1386		return (0);
1387
1388	case ISPCTL_RESET_DEV:
1389		mbs.param[0] = MBOX_ABORT_TARGET;
1390		mbs.param[1] = ((long)arg) << 8;
1391		mbs.param[2] = 3;	/* 'delay', in seconds */
1392		isp_mboxcmd(isp, &mbs);
1393		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1394			isp_dumpregs(isp, "Target Reset Failed");
1395			break;
1396		}
1397		PRINTF("%s: Target %d Reset Succeeded\n", isp->isp_name,
1398		    (int) ((long) arg));
1399		isp->isp_sendmarker = 1;
1400		return (0);
1401
1402	case ISPCTL_ABORT_CMD:
1403		xs = (ISP_SCSI_XFER_T *) arg;
1404		for (i = 0; i < RQUEST_QUEUE_LEN; i++) {
1405			if (xs == isp->isp_xflist[i]) {
1406				break;
1407			}
1408		}
1409		if (i == RQUEST_QUEUE_LEN) {
1410			PRINTF("%s: isp_control- cannot find command to abort "
1411			    "in active list\n", isp->isp_name);
1412			break;
1413		}
1414		mbs.param[0] = MBOX_ABORT;
1415#ifdef	ISP2100_SCCLUN
1416		if (isp->isp_type & ISP_HA_FC) {
1417			mbs.param[1] = XS_TGT(xs) << 8;
1418			mbs.param[4] = 0;
1419			mbs.param[5] = 0;
1420			mbs.param[6] = XS_LUN(xs);
1421		} else {
1422			mbs.param[1] = XS_TGT(xs) << 8 | XS_LUN(xs);
1423		}
1424#else
1425		mbs.param[1] = XS_TGT(xs) << 8 | XS_LUN(xs);
1426#endif
1427		mbs.param[2] = (i+1) >> 16;
1428		mbs.param[3] = (i+1) & 0xffff;
1429		isp_mboxcmd(isp, &mbs);
1430		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1431			PRINTF("%s: isp_control MBOX_ABORT failure (code %x)\n",
1432			    isp->isp_name, mbs.param[0]);
1433			break;
1434		}
1435		PRINTF("%s: command for target %d lun %d was aborted\n",
1436		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1437		return (0);
1438
1439	case ISPCTL_UPDATE_PARAMS:
1440		isp_update(isp);
1441		return (0);
1442
1443	case ISPCTL_FCLINK_TEST:
1444		return (isp_fclink_test(isp, FC_FW_READY_DELAY));
1445	}
1446	return (-1);
1447}
1448
1449/*
1450 * Interrupt Service Routine(s).
1451 *
1452 * External (OS) framework has done the appropriate locking,
1453 * and the locking will be held throughout this function.
1454 */
1455
1456int
1457isp_intr(arg)
1458	void *arg;
1459{
1460	ISP_SCSI_XFER_T *complist[RESULT_QUEUE_LEN], *xs;
1461	struct ispsoftc *isp = arg;
1462	u_int8_t iptr, optr;
1463	u_int16_t isr;
1464	int i, nlooked = 0, ndone = 0;
1465
1466	isr = ISP_READ(isp, BIU_ISR);
1467	if (isp->isp_type & ISP_HA_FC) {
1468		if (isr == 0 || (isr & BIU2100_ISR_RISC_INT) == 0) {
1469			if (isr) {
1470				IDPRINTF(4, ("%s: isp_intr isr=%x\n",
1471				    isp->isp_name, isr));
1472			}
1473			return (0);
1474		}
1475	} else {
1476		if (isr == 0 || (isr & BIU_ISR_RISC_INT) == 0) {
1477			if (isr) {
1478				IDPRINTF(4, ("%s: isp_intr isr=%x\n",
1479				    isp->isp_name, isr));
1480			}
1481			return (0);
1482		}
1483	}
1484
1485	if (ISP_READ(isp, BIU_SEMA) & 1) {
1486		u_int16_t mbox = ISP_READ(isp, OUTMAILBOX0);
1487		if (mbox & 0x4000) {
1488			IDPRINTF(3, ("%s: isp_intr sees 0x%x\n",
1489			    isp->isp_name, mbox));
1490			ISP_WRITE(isp, BIU_SEMA, 0);
1491		} else {
1492			u_int32_t fhandle = isp_parse_async(isp, (int) mbox);
1493			ISP_WRITE(isp, BIU_SEMA, 0);
1494			if (fhandle < 0) {
1495				return (1);
1496			} else if (fhandle > 0) {
1497				xs = (void *)isp->isp_xflist[fhandle - 1];
1498				isp->isp_xflist[fhandle - 1] = NULL;
1499				/*
1500				 * Since we don't have a result queue entry
1501				 * item, we must believe that SCSI status is
1502				 * zero and that all data transferred.
1503				 */
1504				XS_RESID(xs) = 0;
1505				XS_STS(xs) = 0;
1506				if (XS_XFRLEN(xs)) {
1507					ISP_DMAFREE(isp, xs, fhandle - 1);
1508				}
1509				if (isp->isp_nactive > 0)
1510				    isp->isp_nactive--;
1511				complist[ndone++] = xs;
1512			}
1513		}
1514	}
1515	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
1516
1517	optr = isp->isp_residx;
1518	iptr = ISP_READ(isp, OUTMAILBOX5);
1519
1520	if (optr == iptr) {
1521		IDPRINTF(4, ("why intr? isr %x iptr %x optr %x\n",
1522		    isr, optr, iptr));
1523	}
1524	ENABLE_INTS(isp);
1525
1526	while (optr != iptr) {
1527		ispstatusreq_t *sp;
1528		u_int8_t oop;
1529		int buddaboom = 0;
1530
1531		sp = (ispstatusreq_t *) ISP_QUEUE_ENTRY(isp->isp_result, optr);
1532		oop = optr;
1533		optr = ISP_NXT_QENTRY(optr, RESULT_QUEUE_LEN);
1534		nlooked++;
1535		MemoryBarrier();
1536		if (sp->req_header.rqs_entry_type != RQSTYPE_RESPONSE) {
1537			if (isp_handle_other_response(isp, sp, &optr) == 0) {
1538				ISP_WRITE(isp, INMAILBOX5, optr);
1539				continue;
1540			}
1541			/*
1542			 * It really has to be a bounced request just copied
1543			 * from the request queue to the response queue.
1544			 */
1545
1546			if (sp->req_header.rqs_entry_type != RQSTYPE_REQUEST) {
1547				ISP_WRITE(isp, INMAILBOX5, optr);
1548				continue;
1549			}
1550			PRINTF("%s: not RESPONSE in RESPONSE Queue "
1551			    "(type 0x%x) @ idx %d (next %d)\n", isp->isp_name,
1552			    sp->req_header.rqs_entry_type, oop, optr);
1553			buddaboom = 1;
1554		}
1555
1556		if (sp->req_header.rqs_flags & 0xf) {
1557			if (sp->req_header.rqs_flags & RQSFLAG_CONTINUATION) {
1558				ISP_WRITE(isp, INMAILBOX5, optr);
1559				continue;
1560			}
1561			PRINTF("%s: rqs_flags=%x", isp->isp_name,
1562				sp->req_header.rqs_flags & 0xf);
1563			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
1564				PRINTF("%s: internal queues full\n",
1565				    isp->isp_name);
1566				/* XXXX: this command *could* get restarted */
1567				buddaboom++;
1568			}
1569			if (sp->req_header.rqs_flags & RQSFLAG_BADHEADER) {
1570				PRINTF("%s: bad header\n", isp->isp_name);
1571				buddaboom++;
1572			}
1573			if (sp->req_header.rqs_flags & RQSFLAG_BADPACKET) {
1574				PRINTF("%s: bad request packet\n",
1575				    isp->isp_name);
1576				buddaboom++;
1577			}
1578		}
1579		if (sp->req_handle > RQUEST_QUEUE_LEN || sp->req_handle < 1) {
1580			PRINTF("%s: bad request handle %d\n", isp->isp_name,
1581				sp->req_handle);
1582			ISP_WRITE(isp, INMAILBOX5, optr);
1583			continue;
1584		}
1585		xs = (void *) isp->isp_xflist[sp->req_handle - 1];
1586		if (xs == NULL) {
1587			PRINTF("%s: NULL xs in xflist (handle %x)\n",
1588			    isp->isp_name, sp->req_handle);
1589			isp_dumpxflist(isp);
1590			ISP_WRITE(isp, INMAILBOX5, optr);
1591			continue;
1592		}
1593		isp->isp_xflist[sp->req_handle - 1] = NULL;
1594		if (sp->req_status_flags & RQSTF_BUS_RESET) {
1595			isp->isp_sendmarker = 1;
1596		}
1597		if (buddaboom) {
1598			XS_SETERR(xs, HBA_BOTCH);
1599		}
1600		XS_STS(xs) = sp->req_scsi_status & 0xff;
1601		if (isp->isp_type & ISP_HA_SCSI) {
1602			if (sp->req_state_flags & RQSF_GOT_SENSE) {
1603				MEMCPY(XS_SNSP(xs), sp->req_sense_data,
1604					XS_SNSLEN(xs));
1605				XS_SNS_IS_VALID(xs);
1606			}
1607			/*
1608			 * A new synchronous rate was negotiated for this
1609			 * target. Mark state such that we'll go look up
1610			 * that which has changed later.
1611			 */
1612			if (sp->req_status_flags & RQSTF_NEGOTIATION) {
1613				sdparam *sdp = isp->isp_param;
1614				isp->isp_update = 1;
1615				sdp->isp_devparam[XS_TGT(xs)].dev_refresh = 1;
1616			}
1617		} else {
1618			if (XS_STS(xs) == SCSI_CHECK) {
1619				XS_SNS_IS_VALID(xs);
1620				MEMCPY(XS_SNSP(xs), sp->req_sense_data,
1621					XS_SNSLEN(xs));
1622				sp->req_state_flags |= RQSF_GOT_SENSE;
1623			}
1624		}
1625		if (XS_NOERR(xs) && XS_STS(xs) == SCSI_BUSY) {
1626			XS_SETERR(xs, HBA_TGTBSY);
1627		}
1628
1629		if (sp->req_header.rqs_entry_type == RQSTYPE_RESPONSE) {
1630			if (XS_NOERR(xs)) {
1631			    if (sp->req_completion_status != RQCS_COMPLETE) {
1632				isp_parse_status(isp, sp, xs);
1633			    } else {
1634				XS_SETERR(xs, HBA_NOERROR);
1635			    }
1636			}
1637		} else {
1638			PRINTF("%s: unknown return %x\n", isp->isp_name,
1639				sp->req_header.rqs_entry_type);
1640			if (XS_NOERR(xs)) {
1641				XS_SETERR(xs, HBA_BOTCH);
1642			}
1643		}
1644		if (isp->isp_type & ISP_HA_SCSI) {
1645			XS_RESID(xs) = sp->req_resid;
1646		} else if (sp->req_scsi_status & RQCS_RU) {
1647			XS_RESID(xs) = sp->req_resid;
1648			IDPRINTF(4, ("%s: cnt %d rsd %d\n", isp->isp_name,
1649				XS_XFRLEN(xs), sp->req_resid));
1650		}
1651		if (XS_XFRLEN(xs)) {
1652			ISP_DMAFREE(isp, xs, sp->req_handle - 1);
1653		}
1654		/*
1655		 * XXX: If we have a check condition, but no Sense Data,
1656		 * XXX: mark it as an error (ARQ failed). We need to
1657		 * XXX: to do a more distinct job because there may
1658		 * XXX: cases where ARQ is disabled.
1659		 */
1660		if (XS_STS(xs) == SCSI_CHECK && !(XS_IS_SNS_VALID(xs))) {
1661			if (XS_NOERR(xs)) {
1662				PRINTF("%s: ARQ failure for target %d lun %d\n",
1663				    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1664				XS_SETERR(xs, HBA_ARQFAIL);
1665			}
1666		}
1667		if ((isp->isp_dblev >= 5) ||
1668		    (isp->isp_dblev > 2 && !XS_NOERR(xs))) {
1669			PRINTF("%s(%d.%d): FIN%d dl%d resid%d STS %x",
1670			    isp->isp_name, XS_TGT(xs), XS_LUN(xs),
1671			    sp->req_header.rqs_seqno, XS_XFRLEN(xs),
1672			    XS_RESID(xs), XS_STS(xs));
1673			if (sp->req_state_flags & RQSF_GOT_SENSE) {
1674				PRINTF(" Skey: %x", XS_SNSKEY(xs));
1675				if (!(XS_IS_SNS_VALID(xs))) {
1676					PRINTF(" BUT NOT SET");
1677				}
1678			}
1679			PRINTF(" XS_ERR=0x%x\n", (unsigned int) XS_ERR(xs));
1680		}
1681
1682		if (isp->isp_nactive > 0)
1683		    isp->isp_nactive--;
1684		complist[ndone++] = xs;	/* defer completion call until later */
1685	}
1686
1687	/*
1688	 * If we looked at any commands, then it's valid to find out
1689	 * what the outpointer is. It also is a trigger to update the
1690	 * ISP's notion of what we've seen so far.
1691	 */
1692	if (nlooked) {
1693		ISP_WRITE(isp, INMAILBOX5, optr);
1694		isp->isp_reqodx = ISP_READ(isp, OUTMAILBOX4);
1695	}
1696	isp->isp_residx = optr;
1697	for (i = 0; i < ndone; i++) {
1698		xs = complist[i];
1699		if (xs) {
1700			XS_CMD_DONE(xs);
1701		}
1702	}
1703	return (1);
1704}
1705
1706/*
1707 * Support routines.
1708 */
1709
1710static int
1711isp_parse_async(isp, mbox)
1712	struct ispsoftc *isp;
1713	int mbox;
1714{
1715	u_int32_t fast_post_handle = 0;
1716
1717	switch (mbox) {
1718	case MBOX_COMMAND_COMPLETE:	/* sometimes these show up */
1719		break;
1720	case ASYNC_BUS_RESET:
1721		isp_async(isp, ISPASYNC_BUS_RESET, NULL);
1722		isp->isp_sendmarker = 1;
1723#ifdef	ISP_TARGET_MODE
1724		isp_notify_ack(isp, NULL);
1725#endif
1726		break;
1727
1728	case ASYNC_SYSTEM_ERROR:
1729		mbox = ISP_READ(isp, OUTMAILBOX1);
1730		PRINTF("%s: Internal FW Error @ RISC Addr 0x%x\n",
1731		    isp->isp_name, mbox);
1732		isp_restart(isp);
1733		/* no point continuing after this */
1734		return (-1);
1735
1736	case ASYNC_RQS_XFER_ERR:
1737		PRINTF("%s: Request Queue Transfer Error\n", isp->isp_name);
1738		break;
1739
1740	case ASYNC_RSP_XFER_ERR:
1741		PRINTF("%s: Response Queue Transfer Error\n", isp->isp_name);
1742		break;
1743
1744	case ASYNC_QWAKEUP:
1745		/* don't need to be chatty */
1746		mbox = ISP_READ(isp, OUTMAILBOX4);
1747		break;
1748
1749	case ASYNC_TIMEOUT_RESET:
1750		PRINTF("%s: timeout initiated SCSI bus reset\n", isp->isp_name);
1751		isp->isp_sendmarker = 1;
1752#ifdef	ISP_TARGET_MODE
1753		isp_notify_ack(isp, NULL);
1754#endif
1755		break;
1756
1757	case ASYNC_DEVICE_RESET:
1758		isp->isp_sendmarker = 1;
1759		PRINTF("%s: device reset\n", isp->isp_name);
1760#ifdef	ISP_TARGET_MODE
1761		isp_notify_ack(isp, NULL);
1762#endif
1763		break;
1764
1765	case ASYNC_EXTMSG_UNDERRUN:
1766		PRINTF("%s: extended message underrun\n", isp->isp_name);
1767		break;
1768
1769	case ASYNC_SCAM_INT:
1770		PRINTF("%s: SCAM interrupt\n", isp->isp_name);
1771		break;
1772
1773	case ASYNC_HUNG_SCSI:
1774		PRINTF("%s: stalled SCSI Bus after DATA Overrun\n",
1775		    isp->isp_name);
1776		/* XXX: Need to issue SCSI reset at this point */
1777		break;
1778
1779	case ASYNC_KILLED_BUS:
1780		PRINTF("%s: SCSI Bus reset after DATA Overrun\n",
1781		    isp->isp_name);
1782		break;
1783
1784	case ASYNC_BUS_TRANSIT:
1785		mbox = ISP_READ(isp, OUTMAILBOX2);
1786		switch (mbox & 0x1c00) {
1787		case SXP_PINS_LVD_MODE:
1788			PRINTF("%s: Transition to LVD mode\n", isp->isp_name);
1789			((sdparam *)isp->isp_param)->isp_diffmode = 0;
1790			((sdparam *)isp->isp_param)->isp_ultramode = 0;
1791			((sdparam *)isp->isp_param)->isp_lvdmode = 1;
1792			break;
1793		case SXP_PINS_HVD_MODE:
1794			PRINTF("%s: Transition to Differential mode\n",
1795			    isp->isp_name);
1796			((sdparam *)isp->isp_param)->isp_diffmode = 1;
1797			((sdparam *)isp->isp_param)->isp_ultramode = 0;
1798			((sdparam *)isp->isp_param)->isp_lvdmode = 0;
1799			break;
1800		case SXP_PINS_SE_MODE:
1801			PRINTF("%s: Transition to Single Ended mode\n",
1802			    isp->isp_name);
1803			((sdparam *)isp->isp_param)->isp_diffmode = 0;
1804			((sdparam *)isp->isp_param)->isp_ultramode = 1;
1805			((sdparam *)isp->isp_param)->isp_lvdmode = 0;
1806			break;
1807		default:
1808			PRINTF("%s: Transition to unknown mode 0x%x\n",
1809			    isp->isp_name, mbox);
1810			break;
1811		}
1812		/*
1813		 * XXX: Set up to renegotiate again!
1814		 */
1815		isp->isp_sendmarker = 1;
1816		break;
1817
1818	case ASYNC_CMD_CMPLT:
1819		fast_post_handle = (ISP_READ(isp, OUTMAILBOX2) << 16) |
1820		    ISP_READ(isp, OUTMAILBOX1);
1821		IDPRINTF(3, ("%s: fast post completion of %u\n", isp->isp_name,
1822		    fast_post_handle));
1823		break;
1824
1825	case ASYNC_CTIO_DONE:
1826		/* Should only occur when Fast Posting Set for 2100s */
1827		PRINTF("%s: CTIO done\n", isp->isp_name);
1828		break;
1829
1830	case ASYNC_LIP_OCCURRED:
1831		isp->isp_sendmarker = 1;
1832		PRINTF("%s: LIP occurred\n", isp->isp_name);
1833		break;
1834
1835	case ASYNC_LOOP_UP:
1836		isp->isp_sendmarker = 1;
1837		isp_async(isp, ISPASYNC_LOOP_UP, NULL);
1838		break;
1839
1840	case ASYNC_LOOP_DOWN:
1841		isp_async(isp, ISPASYNC_LOOP_DOWN, NULL);
1842		break;
1843
1844	case ASYNC_LOOP_RESET:
1845		isp->isp_sendmarker = 1;
1846		PRINTF("%s: Loop RESET\n", isp->isp_name);
1847#ifdef	ISP_TARGET_MODE
1848		isp_notify_ack(isp, NULL);
1849#endif
1850		break;
1851
1852	case ASYNC_PDB_CHANGED:
1853		isp->isp_sendmarker = 1;
1854		isp_mark_getpdb_all(isp);
1855		PRINTF("%s: Port Database Changed\n", isp->isp_name);
1856		break;
1857
1858	case ASYNC_CHANGE_NOTIFY:
1859		break;
1860
1861	default:
1862		PRINTF("%s: unknown async code 0x%x\n", isp->isp_name, mbox);
1863		break;
1864	}
1865	return (fast_post_handle);
1866}
1867
1868static int
1869isp_handle_other_response(isp, sp, optrp)
1870	struct ispsoftc *isp;
1871	ispstatusreq_t *sp;
1872	u_int8_t *optrp;
1873{
1874	u_int8_t iptr, optr;
1875	int reqsize = 0;
1876	void *ireqp = NULL;
1877#ifdef	ISP_TARGET_MODE
1878	union {
1879		at_entry_t	*atio;
1880		at2_entry_t	*at2io;
1881		ct_entry_t	*ctio;
1882		ct2_entry_t	*ct2io;
1883		lun_entry_t	*lunen;
1884		in_entry_t	*inot;
1885		in_fcentry_t	*inot_fc;
1886		na_entry_t	*nack;
1887		na_fcentry_t	*nack_fc;
1888		void		*voidp;
1889#define	atio	un.atio
1890#define	at2io	un.at2io
1891#define	ctio	un.ctio
1892#define	ct2io	un.ct2io
1893#define	lunen	un.lunen
1894#define	inot	un.inot
1895#define	inot_fc	un.inot_fc
1896#define	nack	un.nack
1897#define	nack_fc	un.nack_fc
1898	} un;
1899
1900	un.voidp = sp;
1901#endif
1902
1903
1904	switch (sp->req_header.rqs_entry_type) {
1905	case RQSTYPE_REQUEST:
1906		return (-1);
1907#ifdef	ISP_TARGET_MODE
1908	case RQSTYPE_NOTIFY_ACK:
1909	{
1910		static const char *f =
1911			"%s: Notify Ack Status 0x%x Sequence Id 0x%x\n"
1912		/*
1913		 * The ISP is acknowleding our ack of an Immediate Notify.
1914		 */
1915		if (isp->isp_type & ISP_HA_FC) {
1916			PRINTF(f, isp->isp_name,
1917			    nack_fc->na-status, nack_fc->na_seqid);
1918		} else {
1919			PRINTF(f, isp->isp_name,
1920			    nack->na_status, nack->na_seqid);
1921		}
1922		break;
1923	}
1924	case RQSTYPE_NOTIFY:
1925	{
1926		u_int16_t seqid, status;
1927
1928		/*
1929		 * Either the ISP received a SCSI message it cannot handle
1930		 * or some other out of band condition (e.g., Port Logout)
1931		 * or it is returning an Immediate Notify entry we sent.
1932		 */
1933		if (isp->isp_type & ISP_HA_FC) {
1934			status = inot_fc->status;
1935			seqid = inot_fc->in_seqid;
1936		} else {
1937			status = inot->status;
1938			seqid = inot->seqid & 0xff;
1939		}
1940		PRINTF("%s: Immediate Notify Status 0x%x Sequence Id 0x%x\n",
1941		    isp->isp_name, status, seqid);
1942
1943		switch (status) {
1944		case IN_MSG_RECEIVED:
1945		case IN_IDE_RECEIVED:
1946			ptisp_got_msg(ptp, &inot);
1947			break;
1948		case IN_RSRC_UNAVAIL:
1949			PRINTF("%s: Firmware out of ATIOs\n", isp->isp_name);
1950			break;
1951		case IN_ABORT_TASK:
1952			PRINTF("%s: Abort Task iid %d rx_id 0x%x\n",
1953			    inot_fc->in_iid, seqid);
1954			break;
1955		case IN_PORT_LOGOUT:
1956			PRINTF("%s: Port Logout for Initiator %d\n",
1957			    isp->isp_name, inot_fc->in_iid);
1958			break;
1959		default:
1960			PRINTF("%s: bad status (0x%x) in Immediate Notify\n",
1961			    isp->isp_name, status);
1962			break;
1963
1964		}
1965		isp_notify_ack(isp, un.voidp);
1966		reqsize = 0;
1967		break;
1968	}
1969	case RQSTYPE_ENABLE_LUN:
1970	case RQSTYPE_MODIFY_LUN:
1971		if (lunen->req_status != 1) {
1972		    PRINTF("%s: ENABLE/MODIFY LUN returned status 0x%x\n",
1973			isp->isp_name, lunen->req_status);
1974		}
1975		break;
1976	case RQSTYPE_ATIO2:
1977	{
1978		fcparam *fcp = isp->isp_param;
1979		ispctiot2_t local, *ct2 = NULL;
1980		ispatiot2_t *at2 = (ispatiot2_t *) sp;
1981		int s, lun;
1982
1983#ifdef	ISP2100_SCCLUN
1984		lun = at2->req_scclun;
1985#else
1986		lun = at2->req_lun;
1987#endif
1988		PRINTF("%s: atio2 loopid %d for lun %d rxid 0x%x flags0x%x "
1989		    "tflags0x%x ecodes0x%x rqstatus0x%x\n", isp->isp_name,
1990		    at2->req_initiator, lun, at2->req_rxid,
1991		    at2->req_flags, at2->req_taskflags, at2->req_execodes,
1992		    at2->req_status);
1993
1994		switch (at2->req_status & ~ATIO_SENSEVALID) {
1995		case ATIO_PATH_INVALID:
1996			PRINTF("%s: ATIO2 Path Invalid\n", isp->isp_name);
1997			break;
1998		case ATIO_NOCAP:
1999			PRINTF("%s: ATIO2 No Cap\n", isp->isp_name);
2000			break;
2001		case ATIO_BDR_MSG:
2002			PRINTF("%s: ATIO2 BDR Received\n", isp->isp_name);
2003			break;
2004		case ATIO_CDB_RECEIVED:
2005			ct2 = &local;
2006			break;
2007		default:
2008			PRINTF("%s: unknown req_status 0x%x\n", isp->isp_name,
2009			    at2->req_status);
2010			break;
2011		}
2012		if (ct2 == NULL) {
2013			/*
2014			 * Just do an ACCEPT on this fellow.
2015			 */
2016			at2->req_header.rqs_entry_type = RQSTYPE_ATIO2;
2017			at2->req_header.rqs_flags = 0;
2018			at2->req_flags = 1;
2019			ireqp = at2;
2020			reqsize = sizeof (*at2);
2021			break;
2022		}
2023		PRINTF("%s: datalen %d cdb0=0x%x\n", isp->isp_name,
2024		    at2->req_datalen, at2->req_cdb[0]);
2025		MEMZERO((void *) ct2, sizeof (*ct2));
2026		ct2->req_header.rqs_entry_type = RQSTYPE_CTIO2;
2027		ct2->req_header.rqs_entry_count = 1;
2028		ct2->req_header.rqs_flags = 0;
2029		ct2->req_header.rqs_seqno = isp->isp_seqno++;
2030		ct2->req_handle = (at2->req_initiator << 16) | lun;
2031#ifndef	ISP2100_SCCLUN
2032		ct2->req_lun = lun;
2033#endif
2034		ct2->req_initiator = at2->req_initiator;
2035		ct2->req_rxid = at2->req_rxid;
2036
2037		ct2->req_flags = CTIO_SEND_STATUS;
2038		switch (at2->req_cdb[0]) {
2039		case 0x0:		/* TUR */
2040			ct2->req_flags |= CTIO_NODATA | CTIO2_SMODE0;
2041			ct2->req_m.mode0.req_scsi_status = CTIO2_STATUS_VALID;
2042			break;
2043
2044		case 0x3:		/* REQUEST SENSE */
2045		case 0x12:		/* INQUIRE */
2046			ct2->req_flags |= CTIO_SEND_DATA | CTIO2_SMODE0;
2047			ct2->req_m.mode0.req_scsi_status = CTIO2_STATUS_VALID;
2048			ct2->req_seg_count = 1;
2049			if (at2->req_cdb[0] == 0x12) {
2050				s = sizeof (tgtiqd);
2051				MEMCPY(fcp->isp_scratch, tgtiqd, s);
2052			} else {
2053				s = at2->req_datalen;
2054				MEMZERO(fcp->isp_scratch, s);
2055			}
2056			ct2->req_m.mode0.req_dataseg[0].ds_base =
2057			    fcp->isp_scdma;
2058			ct2->req_m.mode0.req_dataseg[0].ds_count = s;
2059			ct2->req_m.mode0.req_datalen = s;
2060#if	1
2061			if (at2->req_datalen < s) {
2062				ct2->req_m.mode1.req_scsi_status |=
2063				    CTIO2_RESP_VALID|CTIO2_RSPOVERUN;
2064			} else if (at2->req_datalen > s) {
2065				ct2->req_m.mode1.req_scsi_status |=
2066				    CTIO2_RESP_VALID|CTIO2_RSPUNDERUN;
2067			}
2068#endif
2069			break;
2070
2071		default:		/* ALL OTHERS */
2072			ct2->req_flags |= CTIO_NODATA | CTIO2_SMODE1;
2073			ct2->req_m.mode1.req_scsi_status = 0;
2074#if	1
2075			if (at2->req_datalen) {
2076				ct2->req_m.mode1.req_scsi_status |=
2077				    CTIO2_RSPUNDERUN;
2078#if	BYTE_ORDER == BIG_ENDIAN
2079				ct2->req_resid[1] = at2->req_datalen & 0xff;
2080				ct2->req_resid[0] =
2081					(at2->req_datalen >> 8) & 0xff;
2082				ct2->req_resid[3] =
2083					(at2->req_datalen >> 16) & 0xff;
2084				ct2->req_resid[2] =
2085					(at2->req_datalen >> 24) & 0xff;
2086#else
2087				ct2->req_resid[0] = at2->req_datalen & 0xff;
2088				ct2->req_resid[1] =
2089					(at2->req_datalen >> 8) & 0xff;
2090				ct2->req_resid[2] =
2091					(at2->req_datalen >> 16) & 0xff;
2092				ct2->req_resid[3] =
2093					(at2->req_datalen >> 24) & 0xff;
2094#endif
2095			}
2096#endif
2097			if ((at2->req_status & ATIO_SENSEVALID) == 0) {
2098				ct2->req_m.mode1.req_sense_len = 18;
2099				ct2->req_m.mode1.req_scsi_status |= 2;
2100				ct2->req_m.mode1.req_response[0] = 0x70;
2101				ct2->req_m.mode1.req_response[2] = 0x2;
2102			} else {
2103				ct2->req_m.mode1.req_sense_len = 18;
2104				ct2->req_m.mode1.req_scsi_status |=
2105				    at2->req_scsi_status;
2106				MEMCPY(ct2->req_m.mode1.req_response,
2107				    at2->req_sense, sizeof (at2->req_sense));
2108			}
2109			break;
2110		}
2111		reqsize = sizeof (*ct2);
2112		ireqp = ct2;
2113		break;
2114	}
2115	case RQSTYPE_CTIO2:
2116	{
2117		ispatiot2_t *at2;
2118		ispctiot2_t *ct2 = (ispctiot2_t *) sp;
2119		PRINTF("%s: CTIO2 returned status 0x%x\n", isp->isp_name,
2120		    ct2->req_status);
2121		/*
2122		 * Return the ATIO to the board.
2123		 */
2124		at2 = (ispatiot2_t *) sp;
2125		at2->req_header.rqs_entry_type = RQSTYPE_ATIO2;
2126		at2->req_header.rqs_entry_count = 1;
2127		at2->req_header.rqs_flags = 0;
2128		at2->req_header.rqs_seqno = isp->isp_seqno++;
2129		at2->req_status = 1;
2130		reqsize = sizeof (*at2);
2131		ireqp = at2;
2132		break;
2133	}
2134#undef	atio
2135#undef	at2io
2136#undef	ctio
2137#undef	ct2io
2138#undef	lunen
2139#undef	inot
2140#undef	inot_fc
2141#undef	nack
2142#undef	nack_fc
2143#endif
2144	default:
2145		PRINTF("%s: other response type %x\n", isp->isp_name,
2146		    sp->req_header.rqs_entry_type);
2147		break;
2148	}
2149	if (reqsize) {
2150		void *reqp;
2151		optr = isp->isp_reqodx = ISP_READ(isp, OUTMAILBOX4);
2152		iptr = isp->isp_reqidx;
2153		reqp = (void *) ISP_QUEUE_ENTRY(isp->isp_rquest, iptr);
2154		iptr = ISP_NXT_QENTRY(iptr, RQUEST_QUEUE_LEN);
2155		if (iptr == optr) {
2156			PRINTF("%s: Request Queue Overflow other response\n",
2157			    isp->isp_name);
2158		} else {
2159			MEMCPY(reqp, ireqp, reqsize);
2160			ISP_WRITE(isp, INMAILBOX4, iptr);
2161			isp->isp_reqidx = iptr;
2162		}
2163	}
2164	return (0);
2165}
2166
2167#ifdef	ISP_TARGET_MODE
2168
2169static void isp_tmd_newcmd_dflt __P((void *, tmd_cmd_t *));
2170static void isp_tmd_event_dflt __P((void *, int));
2171static void isp_tmd_notify_dflt __P((void *, tmd_notify_t *));
2172
2173static void isp_tgt_data_xfer __P ((tmd_cmd_t *));
2174static void isp_tgt_endcmd __P ((tmd_cmd_t *, u_int8_t));
2175static void isp_tgt_done __P ((tmd_cmd_t *));
2176
2177static void
2178isp_tmd_newcmd_dflt(arg0, cmdp)
2179	void *arg0;
2180	tmd_cmd_t *cmdp;
2181{
2182}
2183
2184static void
2185isp_tmd_event_dflt(arg0, event)
2186	void *arg0;
2187	int event;
2188{
2189}
2190
2191static void
2192isp_tmd_notify_dflt(arg0, npt)
2193	void *arg0;
2194	tmd_notify_t *npt;
2195{
2196}
2197
2198/*
2199 * Locks held, and ints disabled (if FC).
2200 *
2201 * XXX: SETUP ONLY FOR INITIAL ENABLING RIGHT NOW
2202 */
2203static int
2204isp_modify_lun(isp, lun, icnt, ccnt)
2205	struct ispsoftc *isp;
2206	int lun;	/* logical unit to enable, modify, or disable */
2207	int icnt;	/* immediate notify count */
2208	int ccnt;	/* command count */
2209{
2210	isplun_t *ip = NULL;
2211	u_int8_t iptr, optr;
2212
2213	optr = isp->isp_reqodx = ISP_READ(isp, OUTMAILBOX4);
2214	iptr = isp->isp_reqidx;
2215	ip = (isplun_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, iptr);
2216	iptr = ISP_NXT_QENTRY(iptr, RQUEST_QUEUE_LEN);
2217	if (iptr == optr) {
2218		PRINTF("%s: Request Queue Overflow in isp_modify_lun\n",
2219		    isp->isp_name);
2220		return (-1);
2221	}
2222
2223	MEMZERO((void *) ip, sizeof (*ip));
2224	ip->req_header.rqs_entry_type = RQSTYPE_ENABLE_LUN;
2225	ip->req_header.rqs_entry_count = 1;
2226	ip->req_header.rqs_seqno = isp->isp_seqno++;
2227	ip->req_handle = RQSTYPE_ENABLE_LUN;
2228	if (isp->isp_type & ISP_HA_SCSI) {
2229		ip->req_lun = lun;
2230	}
2231	ip->req_cmdcount = ccnt;
2232	ip->req_imcount = icnt;
2233	ip->req_timeout = 0;	/* default 30 seconds */
2234	ISP_WRITE(isp, INMAILBOX4, iptr);
2235	isp->isp_reqidx = iptr;
2236	return (0);
2237}
2238
2239static void
2240isp_notify_ack(isp, ptrp)
2241	struct ispsoftc *isp;
2242	void *ptrp;
2243{
2244	void *reqp;
2245	u_int8_t iptr, optr;
2246	union {
2247		na_fcentry_t _naf;
2248		na_entry_t _nas;
2249	} un;
2250
2251	MEMZERO((caddr_t)&un, sizeof (un));
2252	un._nas.na_header.rqs_entry_type = RQSTYPE_NOTIFY_ACK;
2253	un._nas.na_header.rqs_entry_count = 1;
2254
2255	if (isp->isp_type & ISP_HA_FC) {
2256		na_fcentry_t *na = &un._nas;
2257		if (ptrp) {
2258			in_fcentry_t *inp = ptrp;
2259			na->na_iid = inp->in_iid;
2260			na->na_lun = inp->in_lun;
2261			na->na_task_flags = inp->in_task_flags;
2262			na->na_seqid = inp->in_seqid;
2263			na->na_status = inp->in_status;
2264		} else {
2265			na->na_flags = NAFC_RST_CLRD;
2266		}
2267	} else {
2268		na_entry_t *na = &un._nas;
2269		if (ptrp) {
2270			in_entry_t *inp = ptrp;
2271			na->na_iid = inp->in_iid;
2272			na->na_lun = inp->in_lun;
2273			na->na_tgt = inp->in_tgt;
2274			na->na_seqid = inp->in_seqid;
2275		} else {
2276			na->na_flags = NA_RST_CLRD;
2277		}
2278	}
2279	optr = isp->isp_reqodx = ISP_READ(isp, OUTMAILBOX4);
2280	iptr = isp->isp_reqidx;
2281	reqp = (void *) ISP_QUEUE_ENTRY(isp->isp_rquest, iptr);
2282	iptr = ISP_NXT_QENTRY(iptr, RQUEST_QUEUE_LEN);
2283	if (iptr == optr) {
2284		PRINTF("%s: Request Queue Overflow For isp_notify_ack\n",
2285		    isp->isp_name);
2286	} else {
2287		MEMCPY(reqp, ireqp, sizeof (un));
2288		ISP_WRITE(isp, INMAILBOX4, iptr);
2289		isp->isp_reqidx = iptr;
2290	}
2291}
2292
2293/*
2294 * These are dummy stubs for now until the outside framework is plugged in.
2295 */
2296
2297static void
2298isp_handle_atio (isp, aep)
2299	struct ispsoftc *isp;
2300	at_entry_t *aep;
2301{
2302	int status, connected;
2303	tmd_cmd_t local, *cdp = &local;
2304
2305	/*
2306	 * Get the ATIO status and see if we're still connected.
2307	 */
2308	status = aep->at_status;
2309	connected = ((aep->at_flags & AT_NODISC) != 0);
2310
2311	PRINTF("%s: ATIO status=0x%x, connected=%d\n", isp->isp_name,
2312	    status, connected);
2313
2314	/*
2315	 * The firmware status (except for the SenseValid bit) indicates
2316	 * why this ATIO was sent to us.
2317	 * If SenseValid is set, the firware has recommended Sense Data.
2318	 * If the Disconnects Disabled bit is set in the flags field,
2319	 * we're still connected on the SCSI bus - i.e. the initiator
2320	 * did not set DiscPriv in the identify message. We don't care
2321	 * about this so it's ignored.
2322	 */
2323	switch (status & ~TGTSVALID) {
2324	case AT_PATH_INVALID:
2325		/*
2326		 * ATIO rejected by the firmware due to disabled lun.
2327		 */
2328		PRINTF("%s: Firmware rejected ATIO for disabled lun %d\n",
2329		    isp->isp_name, aep->at_lun);
2330		break;
2331
2332	case AT_PHASE_ERROR:
2333		/*
2334		 * Bus Pase Sequence error.
2335		 *
2336		 * The firmware should have filled in the correct
2337		 * sense data.
2338		 */
2339
2340
2341		if (status & TGTSVALID) {
2342			MEMCPY(&cdp->cd_sensedata, aep->at_sense,
2343			    sizeof (cdp->cd_sensedata));
2344			PRINTF("%s: Bus Phase Sequence error key 0x%x\n",
2345			    isp->isp_name, cdp->cd_sensedata[2] & 0xf);
2346		} else {
2347			PRINTF("%s: Bus Phase Sequence With No Sense\n",
2348			    isp->isp_name);
2349		}
2350		(*isp->isp_tmd_newcmd)(isp, cdp);
2351		break;
2352
2353	case AT_NOCAP:
2354		/*
2355		 * Requested Capability not available
2356		 * We sent an ATIO that overflowed the firmware's
2357		 * command resource count.
2358		 */
2359		PRINTF("%s: Firmware rejected ATIO, command count overflow\n",
2360		    isp->isp_name);
2361		break;
2362
2363	case AT_BDR_MSG:
2364		/*
2365		 * If we send an ATIO to the firmware to increment
2366		 * its command resource count, and the firmware is
2367		 * recovering from a Bus Device Reset, it returns
2368		 * the ATIO with this status.
2369		 */
2370		PRINTF("%s: ATIO returned with BDR received\n", isp->isp_name);
2371		break;
2372
2373	case AT_CDB:
2374		/*
2375		 * New CDB
2376		 */
2377		cdp->cd_hba = isp;
2378		cdp->cd_iid = aep->at_iid;
2379		cdp->cd_tgt = aep->at_tgt;
2380		cdp->cd_lun = aep->at_lun;
2381		cdp->cd_tagtype = aep->at_tag_type;
2382		cdp->cd_tagval = aep->at_tag_val;
2383		MEMCPY(cdp->cd_cdb, aep->at_cdb, 16);
2384		PRINTF("%s: CDB 0x%x itl %d/%d/%d\n", isp->isp_name,
2385		    cdp->cd_cdb[0], cdp->cd_iid, cdp->cd_tgt, cdp->cd_lun);
2386		(*isp->isp_tmd_newcmd)(isp, cdp);
2387		break;
2388
2389	default:
2390		PRINTF("%s: Unknown status (0x%x) in ATIO\n",
2391		    isp->isp_name, status);
2392		cdp->cd_hba = isp;
2393		cdp->cd_iid = aep->at_iid;
2394		cdp->cd_tgt = aep->at_tgt;
2395		cdp->cd_lun = aep->at_lun;
2396		cdp->cd_tagtype = aep->at_tag_type;
2397		cdp->cd_tagval = aep->at_tag_val;
2398		isp_tgtcmd_done(cdp);
2399		break;
2400	}
2401}
2402
2403static void
2404isp_handle_atio2(isp, aep)
2405	struct ispsoftc *isp;
2406	at2_entry_t *aep;
2407{
2408	int status;
2409	tmd_cmd_t local, *cdp = &local;
2410
2411	/*
2412	 * Get the ATIO2 status.
2413	 */
2414	status = aep->at_status;
2415	PRINTD("%s: ATIO2 status=0x%x\n", status);
2416
2417	/*
2418	 * The firmware status (except for the SenseValid bit) indicates
2419	 * why this ATIO was sent to us.
2420	 * If SenseValid is set, the firware has recommended Sense Data.
2421	 */
2422	switch (status & ~TGTSVALID) {
2423	case AT_PATH_INVALID:
2424		/*
2425		 * ATIO rejected by the firmware due to disabled lun.
2426		 */
2427		PRINTF("%s: Firmware rejected ATIO2 for disabled lun %d\n",
2428		    isp->isp_name, aep->at_lun);
2429		break;
2430
2431	case AT_NOCAP:
2432		/*
2433		 * Requested Capability not available
2434		 * We sent an ATIO that overflowed the firmware's
2435		 * command resource count.
2436		 */
2437		PRINTF("%s: Firmware rejected ATIO2, command count overflow\n",
2438		    isp->isp_name);
2439		break;
2440
2441	case AT_BDR_MSG:
2442		/*
2443		 * If we send an ATIO to the firmware to increment
2444		 * its command resource count, and the firmware is
2445		 * recovering from a Bus Device Reset, it returns
2446		 * the ATIO with this status.
2447		 */
2448		PRINTF("%s: ATIO2 returned with BDR rcvd\n", isp->isp_name);
2449		break;
2450
2451	case AT_CDB:
2452		/*
2453		 * New CDB
2454		 */
2455		cdp->cd_hba = isp;
2456		cdp->cd_iid = aep->at_iid;
2457		cdp->cd_tgt = 0;
2458		cdp->cd_lun = aep->at_lun;
2459		MEMCPY(cdp->cd_cdb, aep->at_cdb, 16);
2460		cdp->cd_rxid = aep->at_rxid;
2461		cdp->cp_origdlen = aep->at_datalen;
2462		cdp->cp_totbytes = 0;
2463		PRINTF("%s: CDB 0x%x rx_id 0x%x itl %d/%d/%d dlen %d\n",
2464		    isp->isp_name, cdp->cd_cdb[0], cdp->cd_tagval, cdp->cd_iid,
2465		    cdp->cd_tgt, cdp->cd_lun, aep->at_datalen);
2466		(*isp->isp_tmd_newcmd)(isp, cdp);
2467		break;
2468
2469	default:
2470		PRINTF("%s: Unknown status (0x%x) in ATIO2\n",
2471		    isp->isp_name, status);
2472		cdp->cd_hba = isp;
2473		cdp->cd_iid = aep->at_iid;
2474		cdp->cd_tgt = aep->at_tgt;
2475		cdp->cd_lun = aep->at_lun;
2476		cdp->cp_rxid = aep->at_rxid;
2477		isp_tgtcmd_done(cdp);
2478		break;
2479	}
2480}
2481
2482static void
2483isp_handle_ctio(isp, cep)
2484	struct ispsoftc *isp;
2485	ct_entry_t *aep;
2486{
2487}
2488
2489static void
2490isp_handle_ctio2(isp, cep)
2491	struct ispsoftc *isp;
2492	at2_entry_t *aep;
2493{
2494}
2495#endif
2496
2497static void
2498isp_parse_status(isp, sp, xs)
2499	struct ispsoftc *isp;
2500	ispstatusreq_t *sp;
2501	ISP_SCSI_XFER_T *xs;
2502{
2503	switch (sp->req_completion_status) {
2504	case RQCS_COMPLETE:
2505		XS_SETERR(xs, HBA_NOERROR);
2506		return;
2507
2508	case RQCS_INCOMPLETE:
2509		if ((sp->req_state_flags & RQSF_GOT_TARGET) == 0) {
2510			IDPRINTF(3, ("%s: Selection Timeout for target %d\n",
2511			    isp->isp_name, XS_TGT(xs)));
2512			XS_SETERR(xs, HBA_SELTIMEOUT);
2513			return;
2514		}
2515		PRINTF("%s: command incomplete for target %d lun %d, state "
2516		    "0x%x\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs),
2517		    sp->req_state_flags);
2518		break;
2519
2520	case RQCS_DMA_ERROR:
2521		PRINTF("%s: DMA error for command on target %d, lun %d\n",
2522		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
2523		break;
2524
2525	case RQCS_TRANSPORT_ERROR:
2526		PRINTF("%s: transport error\n", isp->isp_name);
2527		isp_prtstst(sp);
2528		break;
2529
2530	case RQCS_RESET_OCCURRED:
2531		IDPRINTF(2, ("%s: bus reset destroyed command for target %d "
2532		    "lun %d\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs)));
2533		isp->isp_sendmarker = 1;
2534		XS_SETERR(xs, HBA_BUSRESET);
2535		return;
2536
2537	case RQCS_ABORTED:
2538		PRINTF("%s: command aborted for target %d lun %d\n",
2539		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
2540		isp->isp_sendmarker = 1;
2541		XS_SETERR(xs, HBA_ABORTED);
2542		return;
2543
2544	case RQCS_TIMEOUT:
2545		IDPRINTF(2, ("%s: command timed out for target %d lun %d\n",
2546		    isp->isp_name, XS_TGT(xs), XS_LUN(xs)));
2547		XS_SETERR(xs, HBA_CMDTIMEOUT);
2548		return;
2549
2550	case RQCS_DATA_OVERRUN:
2551		if (isp->isp_type & ISP_HA_FC) {
2552			XS_RESID(xs) = sp->req_resid;
2553			break;
2554		}
2555		XS_SETERR(xs, HBA_DATAOVR);
2556		return;
2557
2558	case RQCS_COMMAND_OVERRUN:
2559		PRINTF("%s: command overrun for command on target %d, lun %d\n",
2560		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
2561		break;
2562
2563	case RQCS_STATUS_OVERRUN:
2564		PRINTF("%s: status overrun for command on target %d, lun %d\n",
2565		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
2566		break;
2567
2568	case RQCS_BAD_MESSAGE:
2569		PRINTF("%s: message not COMMAND COMPLETE after status on "
2570		    "target %d, lun %d\n", isp->isp_name, XS_TGT(xs),
2571		    XS_LUN(xs));
2572		break;
2573
2574	case RQCS_NO_MESSAGE_OUT:
2575		PRINTF("%s: No MESSAGE OUT phase after selection on "
2576		    "target %d, lun %d\n", isp->isp_name, XS_TGT(xs),
2577		    XS_LUN(xs));
2578		break;
2579
2580	case RQCS_EXT_ID_FAILED:
2581		PRINTF("%s: EXTENDED IDENTIFY failed on target %d, lun %d\n",
2582		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
2583		break;
2584
2585	case RQCS_IDE_MSG_FAILED:
2586		PRINTF("%s: target %d lun %d rejected INITIATOR DETECTED "
2587		    "ERROR message\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs));
2588		break;
2589
2590	case RQCS_ABORT_MSG_FAILED:
2591		PRINTF("%s: target %d lun %d rejected ABORT message\n",
2592		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
2593		break;
2594
2595	case RQCS_REJECT_MSG_FAILED:
2596		PRINTF("%s: target %d lun %d rejected MESSAGE REJECT message\n",
2597		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
2598		break;
2599
2600	case RQCS_NOP_MSG_FAILED:
2601		PRINTF("%s: target %d lun %d rejected NOP message\n",
2602		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
2603		break;
2604
2605	case RQCS_PARITY_ERROR_MSG_FAILED:
2606		PRINTF("%s: target %d lun %d rejected MESSAGE PARITY ERROR "
2607		    "message\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs));
2608		break;
2609
2610	case RQCS_DEVICE_RESET_MSG_FAILED:
2611		PRINTF("%s: target %d lun %d rejected BUS DEVICE RESET "
2612		    "message\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs));
2613		break;
2614
2615	case RQCS_ID_MSG_FAILED:
2616		PRINTF("%s: target %d lun %d rejected IDENTIFY "
2617		    "message\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs));
2618		break;
2619
2620	case RQCS_UNEXP_BUS_FREE:
2621		PRINTF("%s: target %d lun %d had an unexpected bus free\n",
2622		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
2623		break;
2624
2625	case RQCS_DATA_UNDERRUN:
2626		if (isp->isp_type & ISP_HA_FC) {
2627			XS_RESID(xs) = sp->req_resid;
2628			/* an UNDERRUN is not a botch ??? */
2629		}
2630		XS_SETERR(xs, HBA_NOERROR);
2631		return;
2632
2633	case RQCS_XACT_ERR1:
2634		PRINTF("%s: HBA attempted queued transaction with disconnect "
2635		    "not set for target %d lun %d\n", isp->isp_name, XS_TGT(xs),
2636		    XS_LUN(xs));
2637		break;
2638
2639	case RQCS_XACT_ERR2:
2640		PRINTF("%s: HBA attempted queued transaction to target "
2641		    "routine %d on target %d\n", isp->isp_name, XS_LUN(xs),
2642		    XS_TGT(xs));
2643		break;
2644
2645	case RQCS_XACT_ERR3:
2646		PRINTF("%s: HBA attempted queued transaction for target %d lun "
2647		    "%d when queueing disabled\n", isp->isp_name, XS_TGT(xs),
2648		    XS_LUN(xs));
2649		break;
2650
2651	case RQCS_BAD_ENTRY:
2652		PRINTF("%s: invalid IOCB entry type detected\n", isp->isp_name);
2653		break;
2654
2655	case RQCS_QUEUE_FULL:
2656		PRINTF("%s: internal queues full for target %d lun %d "
2657		    "status 0x%x\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs),
2658		    XS_STS(xs));
2659		/*
2660		 * If QFULL or some other status byte is set, then this
2661		 * isn't an error, per se.
2662		 */
2663		if (XS_STS(xs) != 0) {
2664			XS_SETERR(xs, HBA_NOERROR);
2665			return;
2666		}
2667		break;
2668
2669	case RQCS_PHASE_SKIPPED:
2670		PRINTF("%s: SCSI phase skipped (e.g., COMMAND COMPLETE w/o "
2671		    "STATUS phase) for target %d lun %d\n", isp->isp_name,
2672		    XS_TGT(xs), XS_LUN(xs));
2673		break;
2674
2675	case RQCS_ARQS_FAILED:
2676		PRINTF("%s: Auto Request Sense failed for target %d lun %d\n",
2677		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
2678		XS_SETERR(xs, HBA_ARQFAIL);
2679		return;
2680
2681	case RQCS_WIDE_FAILED:
2682		PRINTF("%s: Wide Negotiation failed for target %d lun %d\n",
2683		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
2684		if (isp->isp_type & ISP_HA_SCSI) {
2685			sdparam *sdp = isp->isp_param;
2686			isp->isp_update = 1;
2687			sdp->isp_devparam[XS_TGT(xs)].dev_flags &= ~DPARM_WIDE;
2688			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
2689		}
2690		XS_SETERR(xs, HBA_NOERROR);
2691		return;
2692
2693	case RQCS_SYNCXFER_FAILED:
2694		PRINTF("%s: SDTR Message failed for target %d lun %d\n",
2695		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
2696		if (isp->isp_type & ISP_HA_SCSI) {
2697			sdparam *sdp = isp->isp_param;
2698			isp->isp_update = 1;
2699			sdp->isp_devparam[XS_TGT(xs)].dev_flags &= ~DPARM_SYNC;
2700			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
2701		}
2702		break;
2703
2704	case RQCS_LVD_BUSERR:
2705		PRINTF("%s: Bad LVD Bus condition while talking to target %d "
2706		    "lun %d\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs));
2707		break;
2708
2709	case RQCS_PORT_UNAVAILABLE:
2710		/*
2711		 * No such port on the loop. Moral equivalent of SELTIMEO
2712		 */
2713		IDPRINTF(3, ("%s: Port Unavailable for target %d\n",
2714		    isp->isp_name, XS_TGT(xs)));
2715		XS_SETERR(xs, HBA_SELTIMEOUT);
2716		return;
2717
2718	case RQCS_PORT_LOGGED_OUT:
2719		/*
2720		 * It was there (maybe)- treat as a selection timeout.
2721		 */
2722		PRINTF("%s: port logout for target %d\n",
2723			isp->isp_name, XS_TGT(xs));
2724		XS_SETERR(xs, HBA_SELTIMEOUT);
2725		return;
2726
2727	case RQCS_PORT_CHANGED:
2728		PRINTF("%s: port changed for target %d\n",
2729			isp->isp_name, XS_TGT(xs));
2730		break;
2731
2732	case RQCS_PORT_BUSY:
2733		PRINTF("%s: port busy for target %d\n",
2734			isp->isp_name, XS_TGT(xs));
2735		XS_SETERR(xs, HBA_TGTBSY);
2736		return;
2737
2738	default:
2739		PRINTF("%s: comp status %x\n", isp->isp_name,
2740		    sp->req_completion_status);
2741		break;
2742	}
2743	XS_SETERR(xs, HBA_BOTCH);
2744}
2745
2746static void
2747isp_fastpost_complete(isp, fph)
2748	struct ispsoftc *isp;
2749	int fph;
2750{
2751	ISP_SCSI_XFER_T *xs;
2752
2753	if (fph < 1)
2754		return;
2755	xs = (ISP_SCSI_XFER_T *) isp->isp_xflist[fph - 1];
2756	isp->isp_xflist[fph - 1] = NULL;
2757	if (xs == NULL) {
2758		PRINTF("%s: fast posting handle 0x%x not found\n",
2759		    isp->isp_name, fph - 1);
2760		return;
2761	}
2762	/*
2763	 * Since we don't have a result queue entry item,
2764	 * we must believe that SCSI status is zero and
2765	 * that all data transferred.
2766	 */
2767	XS_RESID(xs) = 0;
2768	XS_STS(xs) = 0;
2769	if (XS_XFRLEN(xs)) {
2770		ISP_DMAFREE(isp, xs, fph - 1);
2771	}
2772	XS_CMD_DONE(xs);
2773}
2774
2775#define	HINIB(x)			((x) >> 0x4)
2776#define	LONIB(x)			((x)  & 0xf)
2777#define	MAKNIB(a, b)			(((a) << 4) | (b))
2778static u_int8_t mbpcnt[] = {
2779	MAKNIB(1, 1),	/* 0x00: MBOX_NO_OP */
2780	MAKNIB(5, 5),	/* 0x01: MBOX_LOAD_RAM */
2781	MAKNIB(2, 0),	/* 0x02: MBOX_EXEC_FIRMWARE */
2782	MAKNIB(5, 5),	/* 0x03: MBOX_DUMP_RAM */
2783	MAKNIB(3, 3),	/* 0x04: MBOX_WRITE_RAM_WORD */
2784	MAKNIB(2, 3),	/* 0x05: MBOX_READ_RAM_WORD */
2785	MAKNIB(6, 6),	/* 0x06: MBOX_MAILBOX_REG_TEST */
2786	MAKNIB(2, 3),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
2787	MAKNIB(1, 3),	/* 0x08: MBOX_ABOUT_FIRMWARE */
2788	MAKNIB(0, 0),	/* 0x09: */
2789	MAKNIB(0, 0),	/* 0x0a: */
2790	MAKNIB(0, 0),	/* 0x0b: */
2791	MAKNIB(0, 0),	/* 0x0c: */
2792	MAKNIB(0, 0),	/* 0x0d: */
2793	MAKNIB(1, 2),	/* 0x0e: MBOX_CHECK_FIRMWARE */
2794	MAKNIB(0, 0),	/* 0x0f: */
2795	MAKNIB(5, 5),	/* 0x10: MBOX_INIT_REQ_QUEUE */
2796	MAKNIB(6, 6),	/* 0x11: MBOX_INIT_RES_QUEUE */
2797	MAKNIB(4, 4),	/* 0x12: MBOX_EXECUTE_IOCB */
2798	MAKNIB(2, 2),	/* 0x13: MBOX_WAKE_UP	*/
2799	MAKNIB(1, 6),	/* 0x14: MBOX_STOP_FIRMWARE */
2800	MAKNIB(4, 4),	/* 0x15: MBOX_ABORT */
2801	MAKNIB(2, 2),	/* 0x16: MBOX_ABORT_DEVICE */
2802	MAKNIB(3, 3),	/* 0x17: MBOX_ABORT_TARGET */
2803	MAKNIB(3, 1),	/* 0x18: MBOX_BUS_RESET */
2804	MAKNIB(2, 3),	/* 0x19: MBOX_STOP_QUEUE */
2805	MAKNIB(2, 3),	/* 0x1a: MBOX_START_QUEUE */
2806	MAKNIB(2, 3),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
2807	MAKNIB(2, 3),	/* 0x1c: MBOX_ABORT_QUEUE */
2808	MAKNIB(2, 4),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
2809	MAKNIB(0, 0),	/* 0x1e: */
2810	MAKNIB(1, 3),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
2811	MAKNIB(1, 3),	/* 0x20: MBOX_GET_INIT_SCSI_ID, MBOX_GET_LOOP_ID */
2812	MAKNIB(1, 2),	/* 0x21: MBOX_GET_SELECT_TIMEOUT */
2813	MAKNIB(1, 3),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
2814	MAKNIB(1, 2),	/* 0x23: MBOX_GET_TAG_AGE_LIMIT */
2815	MAKNIB(1, 2),	/* 0x24: MBOX_GET_CLOCK_RATE */
2816	MAKNIB(1, 2),	/* 0x25: MBOX_GET_ACT_NEG_STATE */
2817	MAKNIB(1, 2),	/* 0x26: MBOX_GET_ASYNC_DATA_SETUP_TIME */
2818	MAKNIB(1, 3),	/* 0x27: MBOX_GET_PCI_PARAMS */
2819	MAKNIB(2, 4),	/* 0x28: MBOX_GET_TARGET_PARAMS */
2820	MAKNIB(2, 4),	/* 0x29: MBOX_GET_DEV_QUEUE_PARAMS */
2821	MAKNIB(0, 0),	/* 0x2a: */
2822	MAKNIB(0, 0),	/* 0x2b: */
2823	MAKNIB(0, 0),	/* 0x2c: */
2824	MAKNIB(0, 0),	/* 0x2d: */
2825	MAKNIB(0, 0),	/* 0x2e: */
2826	MAKNIB(0, 0),	/* 0x2f: */
2827	MAKNIB(2, 2),	/* 0x30: MBOX_SET_INIT_SCSI_ID */
2828	MAKNIB(2, 2),	/* 0x31: MBOX_SET_SELECT_TIMEOUT */
2829	MAKNIB(3, 3),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
2830	MAKNIB(2, 2),	/* 0x33: MBOX_SET_TAG_AGE_LIMIT */
2831	MAKNIB(2, 2),	/* 0x34: MBOX_SET_CLOCK_RATE */
2832	MAKNIB(2, 2),	/* 0x35: MBOX_SET_ACTIVE_NEG_STATE */
2833	MAKNIB(2, 2),	/* 0x36: MBOX_SET_ASYNC_DATA_SETUP_TIME */
2834	MAKNIB(3, 3),	/* 0x37: MBOX_SET_PCI_CONTROL_PARAMS */
2835	MAKNIB(4, 4),	/* 0x38: MBOX_SET_TARGET_PARAMS */
2836	MAKNIB(4, 4),	/* 0x39: MBOX_SET_DEV_QUEUE_PARAMS */
2837	MAKNIB(0, 0),	/* 0x3a: */
2838	MAKNIB(0, 0),	/* 0x3b: */
2839	MAKNIB(0, 0),	/* 0x3c: */
2840	MAKNIB(0, 0),	/* 0x3d: */
2841	MAKNIB(0, 0),	/* 0x3e: */
2842	MAKNIB(0, 0),	/* 0x3f: */
2843	MAKNIB(1, 2),	/* 0x40: MBOX_RETURN_BIOS_BLOCK_ADDR */
2844	MAKNIB(6, 1),	/* 0x41: MBOX_WRITE_FOUR_RAM_WORDS */
2845	MAKNIB(2, 3),	/* 0x42: MBOX_EXEC_BIOS_IOCB */
2846	MAKNIB(0, 0),	/* 0x43: */
2847	MAKNIB(0, 0),	/* 0x44: */
2848	MAKNIB(0, 0),	/* 0x45: */
2849	MAKNIB(0, 0),	/* 0x46: */
2850	MAKNIB(0, 0),	/* 0x47: */
2851	MAKNIB(0, 0),	/* 0x48: */
2852	MAKNIB(0, 0),	/* 0x49: */
2853	MAKNIB(2, 1),	/* 0x4a: MBOX_SET_FIRMWARE_FEATURES */
2854	MAKNIB(1, 2),	/* 0x4b: MBOX_GET_FIRMWARE_FEATURES */
2855	MAKNIB(0, 0),	/* 0x4c: */
2856	MAKNIB(0, 0),	/* 0x4d: */
2857	MAKNIB(0, 0),	/* 0x4e: */
2858	MAKNIB(0, 0),	/* 0x4f: */
2859	MAKNIB(0, 0),	/* 0x50: */
2860	MAKNIB(0, 0),	/* 0x51: */
2861	MAKNIB(0, 0),	/* 0x52: */
2862	MAKNIB(0, 0),	/* 0x53: */
2863	MAKNIB(8, 0),	/* 0x54: MBOX_EXEC_COMMAND_IOCB_A64 */
2864	MAKNIB(0, 0),	/* 0x55: */
2865	MAKNIB(0, 0),	/* 0x56: */
2866	MAKNIB(0, 0),	/* 0x57: */
2867	MAKNIB(0, 0),	/* 0x58: */
2868	MAKNIB(0, 0),	/* 0x59: */
2869	MAKNIB(0, 0),	/* 0x5a: */
2870	MAKNIB(0, 0),	/* 0x5b: */
2871	MAKNIB(0, 0),	/* 0x5c: */
2872	MAKNIB(0, 0),	/* 0x5d: */
2873	MAKNIB(0, 0),	/* 0x5e: */
2874	MAKNIB(0, 0),	/* 0x5f: */
2875	MAKNIB(8, 6),	/* 0x60: MBOX_INIT_FIRMWARE */
2876	MAKNIB(0, 0),	/* 0x60: MBOX_GET_INIT_CONTROL_BLOCK  (FORMAT?) */
2877	MAKNIB(2, 1),	/* 0x62: MBOX_INIT_LIP */
2878	MAKNIB(8, 1),	/* 0x63: MBOX_GET_FC_AL_POSITION_MAP */
2879	MAKNIB(8, 1),	/* 0x64: MBOX_GET_PORT_DB */
2880	MAKNIB(3, 1),	/* 0x65: MBOX_CLEAR_ACA */
2881	MAKNIB(3, 1),	/* 0x66: MBOX_TARGET_RESET */
2882	MAKNIB(3, 1),	/* 0x67: MBOX_CLEAR_TASK_SET */
2883	MAKNIB(3, 1),	/* 0x68: MBOX_ABORT_TASK_SET */
2884	MAKNIB(1, 2),	/* 0x69: MBOX_GET_FW_STATE */
2885	MAKNIB(2, 8),	/* 0x6a: MBOX_GET_PORT_NAME */
2886	MAKNIB(8, 1),	/* 0x6b: MBOX_GET_LINK_STATUS */
2887	MAKNIB(4, 4),	/* 0x6c: MBOX_INIT_LIP_RESET */
2888	MAKNIB(0, 0),	/* 0x6d: */
2889	MAKNIB(0, 0),	/* 0x6e: */
2890	MAKNIB(0, 0),	/* 0x6f: */
2891	MAKNIB(0, 0),	/* 0x70: */
2892	MAKNIB(0, 0),	/* 0x71: */
2893	MAKNIB(4, 1)	/* 0x72: MBOX_INIT_LIP_LOGIN */
2894};
2895#define	NMBCOM	(sizeof (mbpcnt) / sizeof (mbpcnt[0]))
2896
2897static void
2898isp_mboxcmd(isp, mbp)
2899	struct ispsoftc *isp;
2900	mbreg_t *mbp;
2901{
2902	int outparam, inparam;
2903	int loops, dld = 0;
2904	u_int8_t opcode;
2905
2906	if (mbp->param[0] == ISP2100_SET_PCI_PARAM) {
2907		opcode = mbp->param[0] = MBOX_SET_PCI_PARAMETERS;
2908		inparam = 4;
2909		outparam = 4;
2910		goto command_known;
2911	} else if (mbp->param[0] > NMBCOM) {
2912		PRINTF("%s: bad command %x\n", isp->isp_name, mbp->param[0]);
2913		return;
2914	}
2915
2916	opcode = mbp->param[0];
2917	inparam = HINIB(mbpcnt[mbp->param[0]]);
2918	outparam =  LONIB(mbpcnt[mbp->param[0]]);
2919
2920	if (inparam == 0 && outparam == 0) {
2921		PRINTF("%s: no parameters for %x\n", isp->isp_name,
2922			mbp->param[0]);
2923		return;
2924	}
2925
2926
2927	/*
2928	 * Check for variants
2929	 */
2930#ifdef	ISP2100_SCCLUN
2931	if (isp->isp_type & ISP_HA_FC) {
2932		switch (mbp->param[0]) {
2933		case MBOX_ABORT:
2934			inparam = 7;
2935			break;
2936		case MBOX_ABORT_DEVICE:
2937		case MBOX_START_QUEUE:
2938		case MBOX_STOP_QUEUE:
2939		case MBOX_SINGLE_STEP_QUEUE:
2940		case MBOX_ABORT_QUEUE:
2941		case MBOX_GET_DEV_QUEUE_STATUS:
2942			inparam = 3;
2943			break;
2944		default:
2945			break;
2946		}
2947	}
2948#endif
2949
2950command_known:
2951
2952	/*
2953	 * Set semaphore on mailbox registers to win any races to acquire them.
2954	 */
2955	ISP_WRITE(isp, BIU_SEMA, 1);
2956
2957	/*
2958	 * Make sure we can send some words. Check to see id there's
2959	 * an async mbox event pending.
2960	 */
2961
2962	loops = MBOX_DELAY_COUNT;
2963	while ((ISP_READ(isp, HCCR) & HCCR_HOST_INT) != 0) {
2964		SYS_DELAY(100);
2965		if (ISP_READ(isp, BIU_SEMA) & 1) {
2966			int fph;
2967			u_int16_t mbox = ISP_READ(isp, OUTMAILBOX0);
2968			/*
2969			 * We have a pending MBOX async event.
2970			 */
2971			if (mbox & 0x8000) {
2972				fph = isp_parse_async(isp, (int) mbox);
2973				ISP_WRITE(isp, BIU_SEMA, 0);
2974				ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
2975				if (fph < 0) {
2976					return;
2977				} else if (fph > 0) {
2978					isp_fastpost_complete(isp, fph);
2979				}
2980				SYS_DELAY(100);
2981				goto command_known;
2982			}
2983			/*
2984			 * We have a pending MBOX completion? Might be
2985			 * from a previous command. We can't (sometimes)
2986			 * just clear HOST INTERRUPT, so we'll just silently
2987			 * eat this here.
2988			 */
2989			if (mbox == MBOX_COMMAND_COMPLETE) {
2990				ISP_WRITE(isp, BIU_SEMA, 0);
2991				ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
2992				SYS_DELAY(100);
2993				goto command_known;
2994			}
2995		}
2996		if (--loops < 0) {
2997			if (dld++ > 10) {
2998				PRINTF("%s: isp_mboxcmd could not get command "
2999				    "started\n", isp->isp_name);
3000				return;
3001			}
3002			ISP_WRITE(isp, BIU_SEMA, 0);
3003			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3004			goto command_known;
3005		}
3006	}
3007
3008	/*
3009	 * If we're a 1080 or a 1240, make sure that for a couple of commands
3010	 * the port parameter is set. This is sort of a temporary solution
3011	 * to do it here rather than every place a mailbox command is formed.
3012	 */
3013	if (IS_1080(isp) || IS_12X0(isp)) {
3014		switch (mbp->param[0]) {
3015		case MBOX_BUS_RESET:
3016			mbp->param[2] = isp->isp_port;
3017			break;
3018		default:
3019			break;
3020		}
3021	}
3022
3023	/*
3024	 * Write input parameters.
3025	 */
3026	switch (inparam) {
3027	case 8: ISP_WRITE(isp, INMAILBOX7, mbp->param[7]); mbp->param[7] = 0;
3028	case 7: ISP_WRITE(isp, INMAILBOX6, mbp->param[6]); mbp->param[6] = 0;
3029	case 6:
3030		/*
3031		 * The Qlogic 2100 cannot have registers 4 and 5 written to
3032		 * after initialization or BAD THINGS HAPPEN (tm).
3033		 */
3034		if (IS_SCSI(isp) || mbp->param[0] == MBOX_INIT_FIRMWARE)
3035			ISP_WRITE(isp, INMAILBOX5, mbp->param[5]);
3036		mbp->param[5] = 0;
3037	case 5:
3038		if (IS_SCSI(isp) || mbp->param[0] == MBOX_INIT_FIRMWARE)
3039			ISP_WRITE(isp, INMAILBOX4, mbp->param[4]);
3040		mbp->param[4] = 0;
3041	case 4: ISP_WRITE(isp, INMAILBOX3, mbp->param[3]); mbp->param[3] = 0;
3042	case 3: ISP_WRITE(isp, INMAILBOX2, mbp->param[2]); mbp->param[2] = 0;
3043	case 2: ISP_WRITE(isp, INMAILBOX1, mbp->param[1]); mbp->param[1] = 0;
3044	case 1: ISP_WRITE(isp, INMAILBOX0, mbp->param[0]); mbp->param[0] = 0;
3045	}
3046
3047	/*
3048	 * Clear RISC int condition.
3049	 */
3050	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3051
3052	/*
3053	 * Clear semaphore on mailbox registers so that the Qlogic
3054	 * may update outgoing registers.
3055	 */
3056	ISP_WRITE(isp, BIU_SEMA, 0);
3057
3058	ENABLE_INTS(isp);
3059	/*
3060	 * Set Host Interrupt condition so that RISC will pick up mailbox regs.
3061	 */
3062	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
3063
3064	/*
3065	 * Wait until HOST INT has gone away (meaning that the Qlogic
3066	 * has picked up the mailbox command. Wait a long time.
3067	 */
3068	loops = MBOX_DELAY_COUNT * 5;
3069	while ((ISP_READ(isp, HCCR) & HCCR_CMD_CLEAR_RISC_INT) != 0) {
3070		SYS_DELAY(100);
3071		if (--loops < 0) {
3072			PRINTF("%s: isp_mboxcmd timeout #2\n", isp->isp_name);
3073			return;
3074		}
3075	}
3076
3077	/*
3078	 * While the Semaphore registers isn't set, wait for the Qlogic
3079	 * to process the mailbox command. Again- wait a long time.
3080	 */
3081	loops = MBOX_DELAY_COUNT * 5;
3082	while ((ISP_READ(isp, BIU_SEMA) & 1) == 0) {
3083		SYS_DELAY(100);
3084		/*
3085		 * Wierd- I've seen the case where the semaphore register
3086		 * isn't getting set- sort of a violation of the protocol..
3087		 */
3088		if (ISP_READ(isp, OUTMAILBOX0) & 0x4000)
3089			break;
3090		if (--loops < 0) {
3091			PRINTF("%s: isp_mboxcmd timeout #3\n", isp->isp_name);
3092			return;
3093		}
3094	}
3095
3096	/*
3097	 * Make sure that the MBOX_BUSY has gone away
3098	 */
3099	loops = MBOX_DELAY_COUNT;
3100	for (;;) {
3101		u_int16_t mbox = ISP_READ(isp, OUTMAILBOX0);
3102		if (mbox == MBOX_BUSY) {
3103			if (--loops < 0) {
3104				PRINTF("%s: isp_mboxcmd timeout #4\n",
3105				    isp->isp_name);
3106				return;
3107			}
3108			SYS_DELAY(100);
3109			continue;
3110		}
3111		/*
3112		 * We have a pending MBOX async event.
3113		 */
3114		if (mbox & 0x8000) {
3115			int fph = isp_parse_async(isp, (int) mbox);
3116			ISP_WRITE(isp, BIU_SEMA, 0);
3117			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3118			if (fph < 0) {
3119				return;
3120			} else if (fph > 0) {
3121				isp_fastpost_complete(isp, fph);
3122			}
3123			SYS_DELAY(100);
3124			continue;
3125		}
3126		break;
3127	}
3128
3129	/*
3130	 * Pick up output parameters.
3131	 */
3132	switch (outparam) {
3133	case 8: mbp->param[7] = ISP_READ(isp, OUTMAILBOX7);
3134	case 7: mbp->param[6] = ISP_READ(isp, OUTMAILBOX6);
3135	case 6: mbp->param[5] = ISP_READ(isp, OUTMAILBOX5);
3136	case 5: mbp->param[4] = ISP_READ(isp, OUTMAILBOX4);
3137	case 4: mbp->param[3] = ISP_READ(isp, OUTMAILBOX3);
3138	case 3: mbp->param[2] = ISP_READ(isp, OUTMAILBOX2);
3139	case 2: mbp->param[1] = ISP_READ(isp, OUTMAILBOX1);
3140	case 1: mbp->param[0] = ISP_READ(isp, OUTMAILBOX0);
3141	}
3142
3143	/*
3144	 * Clear RISC int.
3145	 */
3146	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3147
3148	/*
3149	 * Release semaphore on mailbox registers
3150	 */
3151	ISP_WRITE(isp, BIU_SEMA, 0);
3152
3153	/*
3154	 * Just to be chatty here...
3155	 */
3156	switch (mbp->param[0]) {
3157	case MBOX_COMMAND_COMPLETE:
3158		break;
3159	case MBOX_INVALID_COMMAND:
3160		IDPRINTF(2, ("%s: mbox cmd %x failed with INVALID_COMMAND\n",
3161		    isp->isp_name, opcode));
3162		break;
3163	case MBOX_HOST_INTERFACE_ERROR:
3164		PRINTF("%s: mbox cmd %x failed with HOST_INTERFACE_ERROR\n",
3165		    isp->isp_name, opcode);
3166		break;
3167	case MBOX_TEST_FAILED:
3168		PRINTF("%s: mbox cmd %x failed with TEST_FAILED\n",
3169		    isp->isp_name, opcode);
3170		break;
3171	case MBOX_COMMAND_ERROR:
3172		PRINTF("%s: mbox cmd %x failed with COMMAND_ERROR\n",
3173		    isp->isp_name, opcode);
3174		break;
3175	case MBOX_COMMAND_PARAM_ERROR:
3176		switch (opcode) {
3177		case MBOX_GET_PORT_DB:
3178		case MBOX_GET_PORT_NAME:
3179		case MBOX_GET_DEV_QUEUE_PARAMS:
3180			break;
3181		default:
3182			PRINTF("%s: mbox cmd %x failed with "
3183			    "COMMAND_PARAM_ERROR\n", isp->isp_name, opcode);
3184		}
3185		break;
3186
3187	/*
3188	 * Be silent about these...
3189	 */
3190
3191	case ASYNC_LIP_OCCURRED:
3192	case ASYNC_LOOP_UP:
3193	case ASYNC_LOOP_DOWN:
3194	case ASYNC_LOOP_RESET:
3195	case ASYNC_CHANGE_NOTIFY:
3196		break;
3197	case ASYNC_PDB_CHANGED:
3198		isp_mark_getpdb_all(isp);
3199		break;
3200
3201	default:
3202		/*
3203		 * The expected return of EXEC_FIRMWARE is zero.
3204		 */
3205		if ((opcode == MBOX_EXEC_FIRMWARE && mbp->param[0] != 0) ||
3206		    (opcode != MBOX_EXEC_FIRMWARE)) {
3207			PRINTF("%s: mbox cmd %x failed with error %x\n",
3208				isp->isp_name, opcode, mbp->param[0]);
3209		}
3210		break;
3211	}
3212}
3213
3214void
3215isp_lostcmd(isp, xs)
3216	struct ispsoftc *isp;
3217	ISP_SCSI_XFER_T *xs;
3218{
3219	mbreg_t mbs;
3220
3221	mbs.param[0] = MBOX_GET_FIRMWARE_STATUS;
3222	isp_mboxcmd(isp, &mbs);
3223	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3224		isp_dumpregs(isp, "couldn't GET FIRMWARE STATUS");
3225		return;
3226	}
3227	if (mbs.param[1]) {
3228		PRINTF("%s: %d commands on completion queue\n",
3229		    isp->isp_name, mbs.param[1]);
3230	}
3231	if (XS_NULL(xs))
3232		return;
3233
3234	mbs.param[0] = MBOX_GET_DEV_QUEUE_STATUS;
3235	mbs.param[1] = (XS_TGT(xs) << 8) | XS_LUN(xs);
3236	isp_mboxcmd(isp, &mbs);
3237	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3238		isp_dumpregs(isp, "couldn't GET DEVICE QUEUE STATUS");
3239		return;
3240	}
3241	PRINTF("%s: lost command for target %d lun %d, %d active of %d, "
3242		"Queue State: %x\n", isp->isp_name, XS_TGT(xs),
3243		XS_LUN(xs), mbs.param[2], mbs.param[3], mbs.param[1]);
3244
3245	isp_dumpregs(isp, "lost command");
3246	/*
3247	 * XXX: Need to try and do something to recover.
3248	 */
3249}
3250
3251static void
3252isp_dumpregs(isp, msg)
3253	struct ispsoftc *isp;
3254	const char *msg;
3255{
3256	PRINTF("%s: %s\n", isp->isp_name, msg);
3257	if (isp->isp_type & ISP_HA_SCSI)
3258		PRINTF("    biu_conf1=%x", ISP_READ(isp, BIU_CONF1));
3259	else
3260		PRINTF("    biu_csr=%x", ISP_READ(isp, BIU2100_CSR));
3261	PRINTF(" biu_icr=%x biu_isr=%x biu_sema=%x ", ISP_READ(isp, BIU_ICR),
3262	    ISP_READ(isp, BIU_ISR), ISP_READ(isp, BIU_SEMA));
3263	PRINTF("risc_hccr=%x\n", ISP_READ(isp, HCCR));
3264
3265
3266	if (isp->isp_type & ISP_HA_SCSI) {
3267		ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
3268		PRINTF("    cdma_conf=%x cdma_sts=%x cdma_fifostat=%x\n",
3269			ISP_READ(isp, CDMA_CONF), ISP_READ(isp, CDMA_STATUS),
3270			ISP_READ(isp, CDMA_FIFO_STS));
3271		PRINTF("    ddma_conf=%x ddma_sts=%x ddma_fifostat=%x\n",
3272			ISP_READ(isp, DDMA_CONF), ISP_READ(isp, DDMA_STATUS),
3273			ISP_READ(isp, DDMA_FIFO_STS));
3274		PRINTF("    sxp_int=%x sxp_gross=%x sxp(scsi_ctrl)=%x\n",
3275			ISP_READ(isp, SXP_INTERRUPT),
3276			ISP_READ(isp, SXP_GROSS_ERR),
3277			ISP_READ(isp, SXP_PINS_CONTROL));
3278		ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
3279	}
3280	PRINTF("    mbox regs: %x %x %x %x %x\n",
3281	    ISP_READ(isp, OUTMAILBOX0), ISP_READ(isp, OUTMAILBOX1),
3282	    ISP_READ(isp, OUTMAILBOX2), ISP_READ(isp, OUTMAILBOX3),
3283	    ISP_READ(isp, OUTMAILBOX4));
3284	ISP_DUMPREGS(isp);
3285}
3286
3287static void
3288isp_dumpxflist(isp)
3289	struct ispsoftc *isp;
3290{
3291	volatile ISP_SCSI_XFER_T *xs;
3292	int i, hdp;
3293
3294	for (hdp = i = 0; i < RQUEST_QUEUE_LEN; i++) {
3295		xs = isp->isp_xflist[i];
3296		if (xs == NULL) {
3297			continue;
3298		}
3299		if (hdp == 0) {
3300			PRINTF("%s: active requests\n", isp->isp_name);
3301			hdp++;
3302		}
3303		PRINTF(" Active Handle %d: tgt %d lun %d dlen %d\n",
3304		    i+1, XS_TGT(xs), XS_LUN(xs), XS_XFRLEN(xs));
3305	}
3306}
3307
3308static void
3309isp_fw_state(isp)
3310	struct ispsoftc *isp;
3311{
3312	mbreg_t mbs;
3313	if (isp->isp_type & ISP_HA_FC) {
3314		int once = 0;
3315		fcparam *fcp = isp->isp_param;
3316again:
3317		mbs.param[0] = MBOX_GET_FW_STATE;
3318		isp_mboxcmd(isp, &mbs);
3319		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3320			switch (mbs.param[0]) {
3321			case ASYNC_PDB_CHANGED:
3322				isp_mark_getpdb_all(isp);
3323				/* FALL THROUGH */
3324			case ASYNC_LIP_OCCURRED:
3325			case ASYNC_LOOP_UP:
3326			case ASYNC_LOOP_DOWN:
3327			case ASYNC_LOOP_RESET:
3328			case ASYNC_CHANGE_NOTIFY:
3329				if (once++ < 2) {
3330					goto again;
3331				}
3332				break;
3333			}
3334			isp_dumpregs(isp, "GET FIRMWARE STATE failed");
3335			return;
3336		}
3337		fcp->isp_fwstate = mbs.param[1];
3338	}
3339}
3340
3341static void
3342isp_update(isp)
3343	struct ispsoftc *isp;
3344{
3345	int tgt;
3346	mbreg_t mbs;
3347	sdparam *sdp;
3348
3349	isp->isp_update = 0;
3350
3351	if (isp->isp_type & ISP_HA_FC) {
3352		return;
3353	}
3354
3355	sdp = isp->isp_param;
3356	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
3357		u_int16_t flags, period, offset, changed;
3358		int get;
3359
3360		if (sdp->isp_devparam[tgt].dev_enable == 0) {
3361			continue;
3362		}
3363
3364		/*
3365		 * If the goal is to update the status of the device,
3366		 * take what's in dev_flags and try and set the device
3367		 * toward that. Otherwise, if we're just refreshing the
3368		 * current device state, get the current parameters.
3369		 */
3370		if (sdp->isp_devparam[tgt].dev_update) {
3371			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
3372			mbs.param[2] = sdp->isp_devparam[tgt].dev_flags;
3373			mbs.param[3] =
3374				(sdp->isp_devparam[tgt].sync_offset << 8) |
3375				(sdp->isp_devparam[tgt].sync_period);
3376			sdp->isp_devparam[tgt].dev_update = 0;
3377			/*
3378			 * A command completion later that has
3379			 * RQSTF_NEGOTIATION set will cause
3380			 * the dev_refresh/announce cycle.
3381			 *
3382			 * Note: It is really important to update our current
3383			 * flags with at least the state of TAG capabilities-
3384			 * otherwise we might try and send a tagged command
3385			 * when we have it all turned off. So change it here
3386			 * to say that current already matches goal.
3387			 */
3388			sdp->isp_devparam[tgt].cur_dflags &= ~DPARM_TQING;
3389			sdp->isp_devparam[tgt].cur_dflags |=
3390			    (sdp->isp_devparam[tgt].dev_flags & DPARM_TQING);
3391			get = 0;
3392		} else if (sdp->isp_devparam[tgt].dev_refresh) {
3393			mbs.param[0] = MBOX_GET_TARGET_PARAMS;
3394			sdp->isp_devparam[tgt].dev_refresh = 0;
3395			get = 1;
3396		} else {
3397			continue;
3398		}
3399		mbs.param[1] = tgt << 8;
3400		isp_mboxcmd(isp, &mbs);
3401		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3402			PRINTF("%s: failed to %cet SCSI parameters for "
3403			    "target %d\n", isp->isp_name, (get)? 'g' : 's',
3404			    tgt);
3405			continue;
3406		}
3407
3408		if (get == 0) {
3409			/*
3410			 * XXX: Need a SYNC_TARGET for efficiency...
3411			 */
3412			isp->isp_sendmarker = 1;
3413			continue;
3414		}
3415		flags = mbs.param[2];
3416		period = mbs.param[3] & 0xff;
3417		offset = mbs.param[3] >> 8;
3418		if (sdp->isp_devparam[tgt].cur_dflags != flags ||
3419		    sdp->isp_devparam[tgt].cur_period != period ||
3420		    sdp->isp_devparam[tgt].cur_offset != offset) {
3421			IDPRINTF(3, ("%s: tgt %d flags 0x%x period %d "
3422			    "off %d\n", isp->isp_name, tgt, flags,
3423			    period, offset));
3424			changed = 1;
3425		} else {
3426			changed = 0;
3427		}
3428		sdp->isp_devparam[tgt].cur_dflags = flags;
3429		sdp->isp_devparam[tgt].cur_period = period;
3430		sdp->isp_devparam[tgt].cur_offset = offset;
3431		if (sdp->isp_devparam[tgt].dev_announced == 0 || changed) {
3432			if (isp_async(isp, ISPASYNC_NEW_TGT_PARAMS, &tgt))
3433				sdp->isp_devparam[tgt].dev_announced = 0;
3434			else
3435				sdp->isp_devparam[tgt].dev_announced = 1;
3436		}
3437	}
3438}
3439
3440static void
3441isp_setdfltparm(isp)
3442	struct ispsoftc *isp;
3443{
3444	int tgt;
3445	mbreg_t mbs;
3446	sdparam *sdp;
3447
3448	/*
3449	 * Been there, done that, got the T-shirt...
3450	 */
3451	if (isp->isp_gotdparms) {
3452		IDPRINTF(3, ("%s: already have dparms\n", isp->isp_name));
3453		return;
3454	}
3455	isp->isp_gotdparms = 1;
3456
3457	/*
3458	 * If we've not been told to avoid reading NVRAM, try and read it.
3459	 * If we're successful reading it, we can return since NVRAM will
3460	 * tell us the right thing to do. Otherwise, establish some reasonable
3461	 * defaults.
3462	 */
3463
3464	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
3465		if (isp_read_nvram(isp) == 0) {
3466			return;
3467		}
3468	}
3469	if (IS_FC(isp)) {
3470		fcparam *fcp = (fcparam *) isp->isp_param;
3471		fcp->isp_maxfrmlen = ICB_DFLT_FRMLEN;
3472		fcp->isp_maxalloc = 256;
3473		fcp->isp_execthrottle = 16;
3474		fcp->isp_retry_delay = 5;
3475		fcp->isp_retry_count = 0;
3476		/*
3477		 * It would be nice to fake up a WWN in case we don't
3478		 * get one out of NVRAM. Solaris does this for SOCAL
3479		 * cards that don't have SBus properties- it sets up
3480		 * a WWN based upon the system MAC Address.
3481		 */
3482		fcp->isp_wwn = 0;
3483		return;
3484	}
3485
3486	sdp = (sdparam *) isp->isp_param;
3487	mbs.param[0] = MBOX_GET_ACT_NEG_STATE;
3488	isp_mboxcmd(isp, &mbs);
3489	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3490		IDPRINTF(2, ("could not GET ACT NEG STATE\n"));
3491		sdp->isp_req_ack_active_neg = 1;
3492		sdp->isp_data_line_active_neg = 1;
3493	} else {
3494		sdp->isp_req_ack_active_neg = (mbs.param[1] >> 4) & 0x1;
3495		sdp->isp_data_line_active_neg = (mbs.param[1] >> 5) & 0x1;
3496	}
3497
3498	/*
3499	 * The trick here is to establish a default for the default (honk!)
3500	 * state (dev_flags). Then try and get the current status from
3501	 * the card to fill in the current state. We don't, in fact, set
3502	 * the default to the SAFE default state- that's not the goal state.
3503	 */
3504	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
3505		sdp->isp_devparam[tgt].cur_offset = 0;
3506		sdp->isp_devparam[tgt].cur_period = 0;
3507		sdp->isp_devparam[tgt].dev_flags = DPARM_DEFAULT;
3508		sdp->isp_devparam[tgt].cur_dflags = 0;
3509		if (isp->isp_type < ISP_HA_SCSI_1040 ||
3510		    (sdp->isp_clock && sdp->isp_clock < 60)) {
3511			sdp->isp_devparam[tgt].sync_offset =
3512			    ISP_10M_SYNCPARMS >> 8;
3513			sdp->isp_devparam[tgt].sync_period =
3514			    ISP_10M_SYNCPARMS & 0xff;
3515		} else if (IS_1080(isp)) {
3516			sdp->isp_devparam[tgt].sync_offset =
3517			    ISP_40M_SYNCPARMS >> 8;
3518			sdp->isp_devparam[tgt].sync_period =
3519			    ISP_40M_SYNCPARMS & 0xff;
3520		} else {
3521			sdp->isp_devparam[tgt].sync_offset =
3522			    ISP_20M_SYNCPARMS >> 8;
3523			sdp->isp_devparam[tgt].sync_period =
3524			    ISP_20M_SYNCPARMS & 0xff;
3525		}
3526
3527		/*
3528		 * Don't get current target parameters if we've been
3529		 * told not to use NVRAM- it's really the same thing.
3530		 */
3531		if (isp->isp_confopts & ISP_CFG_NONVRAM) {
3532			continue;
3533		}
3534
3535		mbs.param[0] = MBOX_GET_TARGET_PARAMS;
3536		mbs.param[1] = tgt << 8;
3537		isp_mboxcmd(isp, &mbs);
3538		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3539			continue;
3540		}
3541		sdp->isp_devparam[tgt].cur_dflags = mbs.param[2];
3542		sdp->isp_devparam[tgt].dev_flags = mbs.param[2];
3543		sdp->isp_devparam[tgt].cur_period = mbs.param[3] & 0xff;
3544		sdp->isp_devparam[tgt].cur_offset = mbs.param[3] >> 8;
3545
3546		/*
3547		 * The maximum period we can really see
3548		 * here is 100 (decimal), or 400 ns.
3549		 * For some unknown reason we sometimes
3550		 * get back wildass numbers from the
3551		 * boot device's parameters (alpha only).
3552		 */
3553		if ((mbs.param[3] & 0xff) <= 0x64) {
3554			sdp->isp_devparam[tgt].sync_period =
3555			    mbs.param[3] & 0xff;
3556			sdp->isp_devparam[tgt].sync_offset =
3557			    mbs.param[3] >> 8;
3558		}
3559
3560		/*
3561		 * It is not safe to run Ultra Mode with a clock < 60.
3562		 */
3563		if (((sdp->isp_clock && sdp->isp_clock < 60) ||
3564		    (isp->isp_type < ISP_HA_SCSI_1020A)) &&
3565		    (sdp->isp_devparam[tgt].sync_period <=
3566		    (ISP_20M_SYNCPARMS & 0xff))) {
3567			sdp->isp_devparam[tgt].sync_offset =
3568			    ISP_10M_SYNCPARMS >> 8;
3569			sdp->isp_devparam[tgt].sync_period =
3570			    ISP_10M_SYNCPARMS & 0xff;
3571		}
3572	}
3573
3574	/*
3575	 * Set Default Host Adapter Parameters
3576	 */
3577	sdp->isp_cmd_dma_burst_enable = 1;
3578	sdp->isp_data_dma_burst_enabl = 1;
3579	sdp->isp_fifo_threshold = 0;
3580	sdp->isp_initiator_id = 7;
3581	if (isp->isp_type >= ISP_HA_SCSI_1040) {
3582		sdp->isp_async_data_setup = 9;
3583	} else {
3584		sdp->isp_async_data_setup = 6;
3585	}
3586	sdp->isp_selection_timeout = 250;
3587	sdp->isp_max_queue_depth = 128;
3588	sdp->isp_tag_aging = 8;
3589	sdp->isp_bus_reset_delay = 3;
3590	sdp->isp_retry_count = 0;
3591	sdp->isp_retry_delay = 1;
3592
3593	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
3594		sdp->isp_devparam[tgt].exc_throttle = 16;
3595		sdp->isp_devparam[tgt].dev_enable = 1;
3596	}
3597}
3598
3599/*
3600 * Re-initialize the ISP and complete all orphaned commands
3601 * with a 'botched' notice.
3602 *
3603 * Locks held prior to coming here.
3604 */
3605
3606void
3607isp_restart(isp)
3608	struct ispsoftc *isp;
3609{
3610	ISP_SCSI_XFER_T *tlist[RQUEST_QUEUE_LEN], *xs;
3611	int i;
3612
3613	for (i = 0; i < RQUEST_QUEUE_LEN; i++) {
3614		tlist[i] = (ISP_SCSI_XFER_T *) isp->isp_xflist[i];
3615		isp->isp_xflist[i] = NULL;
3616	}
3617	isp_reset(isp);
3618	if (isp->isp_state == ISP_RESETSTATE) {
3619		isp_init(isp);
3620		if (isp->isp_state == ISP_INITSTATE) {
3621			isp->isp_state = ISP_RUNSTATE;
3622		}
3623	}
3624	if (isp->isp_state != ISP_RUNSTATE) {
3625		PRINTF("%s: isp_restart cannot restart ISP\n", isp->isp_name);
3626	}
3627
3628	for (i = 0; i < RQUEST_QUEUE_LEN; i++) {
3629		xs = tlist[i];
3630		if (XS_NULL(xs)) {
3631			continue;
3632		}
3633		if (isp->isp_nactive > 0)
3634		    isp->isp_nactive--;
3635		XS_RESID(xs) = XS_XFRLEN(xs);
3636		XS_SETERR(xs, HBA_BUSRESET);
3637		XS_CMD_DONE(xs);
3638	}
3639}
3640
3641/*
3642 * NVRAM Routines
3643 */
3644
3645static int
3646isp_read_nvram(isp)
3647	struct ispsoftc *isp;
3648{
3649	int i, amt;
3650	u_int8_t csum, minversion;
3651	union {
3652		u_int8_t _x[ISP2100_NVRAM_SIZE];
3653		u_int16_t _s[ISP2100_NVRAM_SIZE>>1];
3654	} _n;
3655#define	nvram_data	_n._x
3656#define	nvram_words	_n._s
3657
3658	if (IS_FC(isp)) {
3659		amt = ISP2100_NVRAM_SIZE;
3660		minversion = 1;
3661	} else {
3662		amt = ISP_NVRAM_SIZE;
3663		minversion = 2;
3664	}
3665
3666	/*
3667	 * Just read the first two words first to see if we have a valid
3668	 * NVRAM to continue reading the rest with.
3669	 */
3670	for (i = 0; i < 2; i++) {
3671		isp_rdnvram_word(isp, i, &nvram_words[i]);
3672	}
3673	if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
3674	    nvram_data[2] != 'P') {
3675		if (isp->isp_bustype != ISP_BT_SBUS) {
3676			PRINTF("%s: invalid NVRAM header\n", isp->isp_name);
3677		}
3678		return (-1);
3679	}
3680	for (i = 2; i < amt>>1; i++) {
3681		isp_rdnvram_word(isp, i, &nvram_words[i]);
3682	}
3683	for (csum = 0, i = 0; i < amt; i++) {
3684		csum += nvram_data[i];
3685	}
3686	if (csum != 0) {
3687		PRINTF("%s: invalid NVRAM checksum\n", isp->isp_name);
3688		return (-1);
3689	}
3690	if (ISP_NVRAM_VERSION(nvram_data) < minversion) {
3691		PRINTF("%s: version %d NVRAM not understood\n", isp->isp_name,
3692		    ISP_NVRAM_VERSION(nvram_data));
3693		return (-1);
3694	}
3695
3696	if (isp->isp_type & ISP_HA_SCSI) {
3697		sdparam *sdp = (sdparam *) isp->isp_param;
3698
3699		sdp->isp_fifo_threshold =
3700			ISP_NVRAM_FIFO_THRESHOLD(nvram_data) |
3701			(ISP_NVRAM_FIFO_THRESHOLD_128(nvram_data) << 2);
3702
3703		sdp->isp_initiator_id =
3704			ISP_NVRAM_INITIATOR_ID(nvram_data);
3705
3706		sdp->isp_bus_reset_delay =
3707			ISP_NVRAM_BUS_RESET_DELAY(nvram_data);
3708
3709		sdp->isp_retry_count =
3710			ISP_NVRAM_BUS_RETRY_COUNT(nvram_data);
3711
3712		sdp->isp_retry_delay =
3713			ISP_NVRAM_BUS_RETRY_DELAY(nvram_data);
3714
3715		sdp->isp_async_data_setup =
3716			ISP_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data);
3717
3718		if (isp->isp_type >= ISP_HA_SCSI_1040) {
3719			if (sdp->isp_async_data_setup < 9) {
3720				sdp->isp_async_data_setup = 9;
3721			}
3722		} else {
3723			if (sdp->isp_async_data_setup != 6) {
3724				sdp->isp_async_data_setup = 6;
3725			}
3726		}
3727
3728		sdp->isp_req_ack_active_neg =
3729			ISP_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data);
3730
3731		sdp->isp_data_line_active_neg =
3732			ISP_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data);
3733
3734		sdp->isp_data_dma_burst_enabl =
3735			ISP_NVRAM_DATA_DMA_BURST_ENABLE(nvram_data);
3736
3737		sdp->isp_cmd_dma_burst_enable =
3738			ISP_NVRAM_CMD_DMA_BURST_ENABLE(nvram_data);
3739
3740		sdp->isp_tag_aging =
3741			ISP_NVRAM_TAG_AGE_LIMIT(nvram_data);
3742
3743		sdp->isp_selection_timeout =
3744			ISP_NVRAM_SELECTION_TIMEOUT(nvram_data);
3745
3746		sdp->isp_max_queue_depth =
3747			ISP_NVRAM_MAX_QUEUE_DEPTH(nvram_data);
3748
3749		sdp->isp_fast_mttr = ISP_NVRAM_FAST_MTTR_ENABLE(nvram_data);
3750		if (isp->isp_dblev > 2) {
3751			static char *true = "true";
3752			static char *false = "false";
3753			PRINTF("%s: NVRAM values:\n", isp->isp_name);
3754			PRINTF("             Fifo Threshold = 0x%x\n",
3755			    sdp->isp_fifo_threshold);
3756			PRINTF("            Bus Reset Delay = %d\n",
3757			    sdp->isp_bus_reset_delay);
3758			PRINTF("                Retry Count = %d\n",
3759			    sdp->isp_retry_count);
3760			PRINTF("                Retry Delay = %d\n",
3761			    sdp->isp_retry_delay);
3762			PRINTF("              Tag Age Limit = %d\n",
3763			    sdp->isp_tag_aging);
3764			PRINTF("          Selection Timeout = %d\n",
3765			    sdp->isp_selection_timeout);
3766			PRINTF("            Max Queue Depth = %d\n",
3767			    sdp->isp_max_queue_depth);
3768			PRINTF("           Async Data Setup = 0x%x\n",
3769			    sdp->isp_async_data_setup);
3770			PRINTF("    REQ/ACK Active Negation = %s\n",
3771			    sdp->isp_req_ack_active_neg? true : false);
3772			PRINTF("  Data Line Active Negation = %s\n",
3773			    sdp->isp_data_line_active_neg? true : false);
3774			PRINTF("      Data DMA Burst Enable = %s\n",
3775			    sdp->isp_data_dma_burst_enabl? true : false);
3776			PRINTF("       Cmd DMA Burst Enable = %s\n",
3777			    sdp->isp_cmd_dma_burst_enable? true : false);
3778			PRINTF("                  Fast MTTR = %s\n",
3779			    sdp->isp_fast_mttr? true : false);
3780		}
3781		for (i = 0; i < MAX_TARGETS; i++) {
3782			sdp->isp_devparam[i].dev_enable =
3783				ISP_NVRAM_TGT_DEVICE_ENABLE(nvram_data, i);
3784			sdp->isp_devparam[i].exc_throttle =
3785				ISP_NVRAM_TGT_EXEC_THROTTLE(nvram_data, i);
3786			sdp->isp_devparam[i].sync_offset =
3787				ISP_NVRAM_TGT_SYNC_OFFSET(nvram_data, i);
3788			sdp->isp_devparam[i].sync_period =
3789				ISP_NVRAM_TGT_SYNC_PERIOD(nvram_data, i);
3790
3791			if (isp->isp_type < ISP_HA_SCSI_1040) {
3792				/*
3793				 * If we're not ultra, we can't possibly
3794				 * be a shorter period than this.
3795				 */
3796				if (sdp->isp_devparam[i].sync_period < 0x19) {
3797					sdp->isp_devparam[i].sync_period =
3798					    0x19;
3799				}
3800				if (sdp->isp_devparam[i].sync_offset > 0xc) {
3801					sdp->isp_devparam[i].sync_offset =
3802					    0x0c;
3803				}
3804			} else {
3805				if (sdp->isp_devparam[i].sync_offset > 0x8) {
3806					sdp->isp_devparam[i].sync_offset = 0x8;
3807				}
3808			}
3809			sdp->isp_devparam[i].dev_flags = 0;
3810			if (ISP_NVRAM_TGT_RENEG(nvram_data, i))
3811				sdp->isp_devparam[i].dev_flags |= DPARM_RENEG;
3812			if (ISP_NVRAM_TGT_QFRZ(nvram_data, i)) {
3813				PRINTF("%s: not supporting QFRZ option for "
3814				    "target %d\n", isp->isp_name, i);
3815			}
3816			sdp->isp_devparam[i].dev_flags |= DPARM_ARQ;
3817			if (ISP_NVRAM_TGT_ARQ(nvram_data, i) == 0) {
3818				PRINTF("%s: not disabling ARQ option for "
3819				    "target %d\n", isp->isp_name, i);
3820			}
3821			if (ISP_NVRAM_TGT_TQING(nvram_data, i))
3822				sdp->isp_devparam[i].dev_flags |= DPARM_TQING;
3823			if (ISP_NVRAM_TGT_SYNC(nvram_data, i))
3824				sdp->isp_devparam[i].dev_flags |= DPARM_SYNC;
3825			if (ISP_NVRAM_TGT_WIDE(nvram_data, i))
3826				sdp->isp_devparam[i].dev_flags |= DPARM_WIDE;
3827			if (ISP_NVRAM_TGT_PARITY(nvram_data, i))
3828				sdp->isp_devparam[i].dev_flags |= DPARM_PARITY;
3829			if (ISP_NVRAM_TGT_DISC(nvram_data, i))
3830				sdp->isp_devparam[i].dev_flags |= DPARM_DISC;
3831			sdp->isp_devparam[i].cur_dflags = 0; /* we don't know */
3832			if (isp->isp_dblev > 2) {
3833				PRINTF("   Target %d: Enabled %d Throttle %d "
3834				    "Offset %d Period %d Flags 0x%x\n", i,
3835				    sdp->isp_devparam[i].dev_enable,
3836				    sdp->isp_devparam[i].exc_throttle,
3837				    sdp->isp_devparam[i].sync_offset,
3838				    sdp->isp_devparam[i].sync_period,
3839				    sdp->isp_devparam[i].dev_flags);
3840			}
3841		}
3842	} else {
3843		fcparam *fcp = (fcparam *) isp->isp_param;
3844		union {
3845			struct {
3846#if	BYTE_ORDER == BIG_ENDIAN
3847				u_int32_t hi32;
3848				u_int32_t lo32;
3849#else
3850				u_int32_t lo32;
3851				u_int32_t hi32;
3852#endif
3853			} wds;
3854			u_int64_t full64;
3855		} wwnstore;
3856
3857		wwnstore.full64 = ISP2100_NVRAM_NODE_NAME(nvram_data);
3858		PRINTF("%s: Adapter WWN 0x%08x%08x\n", isp->isp_name,
3859		    wwnstore.wds.hi32, wwnstore.wds.lo32);
3860		fcp->isp_wwn = wwnstore.full64;
3861		wwnstore.full64 = ISP2100_NVRAM_BOOT_NODE_NAME(nvram_data);
3862		if (wwnstore.full64 != 0) {
3863			PRINTF("%s: BOOT DEVICE WWN 0x%08x%08x\n",
3864			    isp->isp_name, wwnstore.wds.hi32,
3865			    wwnstore.wds.lo32);
3866		}
3867		fcp->isp_maxalloc =
3868			ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data);
3869		fcp->isp_maxfrmlen =
3870			ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data);
3871		fcp->isp_retry_delay =
3872			ISP2100_NVRAM_RETRY_DELAY(nvram_data);
3873		fcp->isp_retry_count =
3874			ISP2100_NVRAM_RETRY_COUNT(nvram_data);
3875		fcp->isp_loopid =
3876			ISP2100_NVRAM_HARDLOOPID(nvram_data);
3877		fcp->isp_execthrottle =
3878			ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data);
3879		fcp->isp_fwoptions = ISP2100_NVRAM_OPTIONS(nvram_data);
3880		if (isp->isp_dblev > 2) {
3881			PRINTF("%s: NVRAM values:\n", isp->isp_name);
3882			PRINTF("  Max IOCB Allocation = %d\n",
3883			    fcp->isp_maxalloc);
3884			PRINTF("     Max Frame Length = %d\n",
3885			    fcp->isp_maxfrmlen);
3886			PRINTF("   Execution Throttle = %d\n",
3887			    fcp->isp_execthrottle);
3888			PRINTF("          Retry Count = %d\n",
3889			    fcp->isp_retry_count);
3890			PRINTF("          Retry Delay = %d\n",
3891			    fcp->isp_retry_delay);
3892			PRINTF("         Hard Loop ID = %d\n",
3893			    fcp->isp_loopid);
3894			PRINTF("              Options = 0x%x\n",
3895			    fcp->isp_fwoptions);
3896			PRINTF("          HBA Options = 0x%x\n",
3897			    ISP2100_NVRAM_HBA_OPTIONS(nvram_data));
3898		}
3899	}
3900	IDPRINTF(3, ("%s: NVRAM is valid\n", isp->isp_name));
3901	return (0);
3902}
3903
3904static void
3905isp_rdnvram_word(isp, wo, rp)
3906	struct ispsoftc *isp;
3907	int wo;
3908	u_int16_t *rp;
3909{
3910	int i, cbits;
3911	u_int16_t bit, rqst;
3912
3913	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
3914	SYS_DELAY(2);
3915	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
3916	SYS_DELAY(2);
3917
3918	if (isp->isp_type & ISP_HA_FC) {
3919		wo &= ((ISP2100_NVRAM_SIZE >> 1) - 1);
3920		rqst = (ISP_NVRAM_READ << 8) | wo;
3921		cbits = 10;
3922	} else {
3923		wo &= ((ISP_NVRAM_SIZE >> 1) - 1);
3924		rqst = (ISP_NVRAM_READ << 6) | wo;
3925		cbits = 8;
3926	}
3927
3928	/*
3929	 * Clock the word select request out...
3930	 */
3931	for (i = cbits; i >= 0; i--) {
3932		if ((rqst >> i) & 1) {
3933			bit = BIU_NVRAM_SELECT | BIU_NVRAM_DATAOUT;
3934		} else {
3935			bit = BIU_NVRAM_SELECT;
3936		}
3937		ISP_WRITE(isp, BIU_NVRAM, bit);
3938		SYS_DELAY(2);
3939		ISP_WRITE(isp, BIU_NVRAM, bit | BIU_NVRAM_CLOCK);
3940		SYS_DELAY(2);
3941		ISP_WRITE(isp, BIU_NVRAM, bit);
3942		SYS_DELAY(2);
3943	}
3944	/*
3945	 * Now read the result back in (bits come back in MSB format).
3946	 */
3947	*rp = 0;
3948	for (i = 0; i < 16; i++) {
3949		u_int16_t rv;
3950		*rp <<= 1;
3951		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
3952		SYS_DELAY(2);
3953		rv = ISP_READ(isp, BIU_NVRAM);
3954		if (rv & BIU_NVRAM_DATAIN) {
3955			*rp |= 1;
3956		}
3957		SYS_DELAY(2);
3958		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
3959		SYS_DELAY(2);
3960	}
3961	ISP_WRITE(isp, BIU_NVRAM, 0);
3962	SYS_DELAY(2);
3963#if	BYTE_ORDER == BIG_ENDIAN
3964	*rp = ((*rp >> 8) | ((*rp & 0xff) << 8));
3965#endif
3966}
3967