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