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