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