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