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