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