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