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