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