xref: /freebsd/sys/dev/isp/isp.c (revision d056fa046c6a91b90cd98165face0e42a33a5173)
1 /*-
2  * Machine and OS Independent (well, as best as possible)
3  * code for the Qlogic ISP SCSI adapters.
4  *
5  * Copyright (c) 1997-2006 by Matthew Jacob
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice immediately at the beginning of the file, without modification,
13  *    this list of conditions, and the following disclaimer.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 /*
31  * Inspiration and ideas about this driver are from Erik Moe's Linux driver
32  * (qlogicisp.c) and Dave Miller's SBus version of same (qlogicisp.c). Some
33  * ideas dredged from the Solaris driver.
34  */
35 #ifdef	__FreeBSD__
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 #endif
39 
40 /*
41  * Include header file appropriate for platform we're building on.
42  */
43 #ifdef	__NetBSD__
44 #include <dev/ic/isp_netbsd.h>
45 #endif
46 #ifdef	__FreeBSD__
47 #include <dev/isp/isp_freebsd.h>
48 #endif
49 #ifdef	__OpenBSD__
50 #include <dev/ic/isp_openbsd.h>
51 #endif
52 #ifdef	__linux__
53 #include "isp_linux.h"
54 #endif
55 #ifdef	__svr4__
56 #include "isp_solaris.h"
57 #endif
58 
59 /*
60  * General defines
61  */
62 
63 #define	MBOX_DELAY_COUNT	1000000 / 100
64 
65 /*
66  * Local static data
67  */
68 static const char portshift[] =
69     "Target %d Loop ID 0x%x (Port 0x%x) => Loop 0x%x (Port 0x%x)";
70 static const char portdup[] =
71     "Target %d duplicates Target %d- killing off both";
72 static const char retained[] =
73     "Retaining Loop ID 0x%x for Target %d (Port 0x%x)";
74 static const char lretained[] =
75     "Retained login of Target %d (Loop ID 0x%x) Port 0x%x";
76 static const char plogout[] =
77     "Logging out Target %d at Loop ID 0x%x (Port 0x%x)";
78 static const char plogierr[] =
79     "Command Error in PLOGI for Port 0x%x (0x%x)";
80 static const char nopdb[] =
81     "Could not get PDB for Device @ Port 0x%x";
82 static const char pdbmfail1[] =
83     "PDB Loop ID info for Device @ Port 0x%x does not match up (0x%x)";
84 static const char pdbmfail2[] =
85     "PDB Port info for Device @ Port 0x%x does not match up (0x%x)";
86 static const char ldumped[] =
87     "Target %d (Loop ID 0x%x) Port 0x%x dumped after login info mismatch";
88 static const char notresp[] =
89   "Not RESPONSE in RESPONSE Queue (type 0x%x) @ idx %d (next %d) nlooked %d";
90 static const char xact1[] =
91     "HBA attempted queued transaction with disconnect not set for %d.%d.%d";
92 static const char xact2[] =
93     "HBA attempted queued transaction to target routine %d on target %d bus %d";
94 static const char xact3[] =
95     "HBA attempted queued cmd for %d.%d.%d when queueing disabled";
96 static const char pskip[] =
97     "SCSI phase skipped for target %d.%d.%d";
98 static const char topology[] =
99     "Loop ID %d, Port ID 0x%x, Loop State 0x%x, Topology '%s'";
100 static const char swrej[] =
101     "Fabric Nameserver rejected %s (Reason=0x%x Expl=0x%x) for Port ID 0x%x";
102 static const char finmsg[] =
103     "%d.%d.%d: FIN dl%d resid %d STS 0x%x SKEY %c XS_ERR=0x%x";
104 static const char sc0[] =
105     "%s CHAN %d FTHRSH %d IID %d RESETD %d RETRYC %d RETRYD %d ASD 0x%x";
106 static const char sc1[] =
107     "%s RAAN 0x%x DLAN 0x%x DDMAB 0x%x CDMAB 0x%x SELTIME %d MQD %d";
108 static const char sc2[] = "%s CHAN %d TGT %d FLAGS 0x%x 0x%x/0x%x";
109 static const char sc3[] = "Generated";
110 static const char sc4[] = "NVRAM";
111 static const char bun[] =
112     "bad underrun for %d.%d (count %d, resid %d, status %s)";
113 
114 /*
115  * Local function prototypes.
116  */
117 static int isp_parse_async(ispsoftc_t *, uint16_t);
118 static int isp_handle_other_response(ispsoftc_t *, int, isphdr_t *,
119     uint16_t *);
120 static void
121 isp_parse_status(ispsoftc_t *, ispstatusreq_t *, XS_T *);
122 static void isp_fastpost_complete(ispsoftc_t *, uint16_t);
123 static int isp_mbox_continue(ispsoftc_t *);
124 static void isp_scsi_init(ispsoftc_t *);
125 static void isp_scsi_channel_init(ispsoftc_t *, int);
126 static void isp_fibre_init(ispsoftc_t *);
127 static void isp_mark_getpdb_all(ispsoftc_t *);
128 static int isp_getmap(ispsoftc_t *, fcpos_map_t *);
129 static int isp_getpdb(ispsoftc_t *, int, isp_pdb_t *);
130 static uint64_t isp_get_portname(ispsoftc_t *, int, int);
131 static int isp_fclink_test(ispsoftc_t *, int);
132 static const char *isp2100_fw_statename(int);
133 static int isp_pdb_sync(ispsoftc_t *);
134 static int isp_scan_loop(ispsoftc_t *);
135 static int isp_fabric_mbox_cmd(ispsoftc_t *, mbreg_t *);
136 static int isp_scan_fabric(ispsoftc_t *, int);
137 static void isp_register_fc4_type(ispsoftc_t *);
138 static void isp_fw_state(ispsoftc_t *);
139 static void isp_mboxcmd_qnw(ispsoftc_t *, mbreg_t *, int);
140 static void isp_mboxcmd(ispsoftc_t *, mbreg_t *, int);
141 
142 static void isp_update(ispsoftc_t *);
143 static void isp_update_bus(ispsoftc_t *, int);
144 static void isp_setdfltparm(ispsoftc_t *, int);
145 static int isp_read_nvram(ispsoftc_t *);
146 static void isp_rdnvram_word(ispsoftc_t *, int, uint16_t *);
147 static void isp_parse_nvram_1020(ispsoftc_t *, uint8_t *);
148 static void isp_parse_nvram_1080(ispsoftc_t *, int, uint8_t *);
149 static void isp_parse_nvram_12160(ispsoftc_t *, int, uint8_t *);
150 static void isp_parse_nvram_2100(ispsoftc_t *, uint8_t *);
151 
152 /*
153  * Reset Hardware.
154  *
155  * Hit the chip over the head, download new f/w if available and set it running.
156  *
157  * Locking done elsewhere.
158  */
159 
160 void
161 isp_reset(ispsoftc_t *isp)
162 {
163 	mbreg_t mbs;
164 	uint16_t code_org;
165 	int loops, i, dodnld = 1;
166 	char *btype = "????";
167 
168 	isp->isp_state = ISP_NILSTATE;
169 	MEMZERO(&mbs, sizeof (mbs));
170 
171 	/*
172 	 * Basic types (SCSI, FibreChannel and PCI or SBus)
173 	 * have been set in the MD code. We figure out more
174 	 * here. Possibly more refined types based upon PCI
175 	 * identification. Chip revision has been gathered.
176 	 *
177 	 * After we've fired this chip up, zero out the conf1 register
178 	 * for SCSI adapters and do other settings for the 2100.
179 	 */
180 
181 	/*
182 	 * Get the current running firmware revision out of the
183 	 * chip before we hit it over the head (if this is our
184 	 * first time through). Note that we store this as the
185 	 * 'ROM' firmware revision- which it may not be. In any
186 	 * case, we don't really use this yet, but we may in
187 	 * the future.
188 	 */
189 	if (isp->isp_touched == 0) {
190 		/*
191 		 * First see whether or not we're sitting in the ISP PROM.
192 		 * If we've just been reset, we'll have the string "ISP   "
193 		 * spread through outgoing mailbox registers 1-3. We do
194 		 * this for PCI cards because otherwise we really don't
195 		 * know what state the card is in and we could hang if
196 		 * we try this command otherwise.
197 		 *
198 		 * For SBus cards, we just do this because they almost
199 		 * certainly will be running firmware by now.
200 		 */
201 		if (ISP_READ(isp, OUTMAILBOX1) != 0x4953 ||
202 		    ISP_READ(isp, OUTMAILBOX2) != 0x5020 ||
203 		    ISP_READ(isp, OUTMAILBOX3) != 0x2020) {
204 			/*
205 			 * Just in case it was paused...
206 			 */
207 			ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
208 			mbs.param[0] = MBOX_ABOUT_FIRMWARE;
209 			isp_mboxcmd(isp, &mbs, MBLOGNONE);
210 			if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
211 				isp->isp_romfw_rev[0] = mbs.param[1];
212 				isp->isp_romfw_rev[1] = mbs.param[2];
213 				isp->isp_romfw_rev[2] = mbs.param[3];
214 			}
215 		}
216 		isp->isp_touched = 1;
217 	}
218 
219 	DISABLE_INTS(isp);
220 
221 	/*
222 	 * Set up default request/response queue in-pointer/out-pointer
223 	 * register indices.
224 	 */
225 	if (IS_23XX(isp)) {
226 		isp->isp_rqstinrp = BIU_REQINP;
227 		isp->isp_rqstoutrp = BIU_REQOUTP;
228 		isp->isp_respinrp = BIU_RSPINP;
229 		isp->isp_respoutrp = BIU_RSPOUTP;
230 	} else {
231 		isp->isp_rqstinrp = INMAILBOX4;
232 		isp->isp_rqstoutrp = OUTMAILBOX4;
233 		isp->isp_respinrp = OUTMAILBOX5;
234 		isp->isp_respoutrp = INMAILBOX5;
235 	}
236 
237 	/*
238 	 * Put the board into PAUSE mode (so we can read the SXP registers
239 	 * or write FPM/FBM registers).
240 	 */
241 	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
242 
243 	if (IS_FC(isp)) {
244 		switch (isp->isp_type) {
245 		case ISP_HA_FC_2100:
246 			btype = "2100";
247 			break;
248 		case ISP_HA_FC_2200:
249 			btype = "2200";
250 			break;
251 		case ISP_HA_FC_2300:
252 			btype = "2300";
253 			break;
254 		case ISP_HA_FC_2312:
255 			btype = "2312";
256 			break;
257 		case ISP_HA_FC_2322:
258 			btype = "2322";
259 			break;
260 		case ISP_HA_FC_2422:
261 			btype = "2422";
262 			break;
263 		default:
264 			break;
265 		}
266 		/*
267 		 * While we're paused, reset the FPM module and FBM fifos.
268 		 */
269 		ISP_WRITE(isp, BIU2100_CSR, BIU2100_FPM0_REGS);
270 		ISP_WRITE(isp, FPM_DIAG_CONFIG, FPM_SOFT_RESET);
271 		ISP_WRITE(isp, BIU2100_CSR, BIU2100_FB_REGS);
272 		ISP_WRITE(isp, FBM_CMD, FBMCMD_FIFO_RESET_ALL);
273 		ISP_WRITE(isp, BIU2100_CSR, BIU2100_RISC_REGS);
274 	} else if (IS_1240(isp)) {
275 		sdparam *sdp = isp->isp_param;
276 		btype = "1240";
277 		isp->isp_clock = 60;
278 		sdp->isp_ultramode = 1;
279 		sdp++;
280 		sdp->isp_ultramode = 1;
281 		/*
282 		 * XXX: Should probably do some bus sensing.
283 		 */
284 	} else if (IS_ULTRA2(isp)) {
285 		static const char m[] = "bus %d is in %s Mode";
286 		uint16_t l;
287 		sdparam *sdp = isp->isp_param;
288 
289 		isp->isp_clock = 100;
290 
291 		if (IS_1280(isp))
292 			btype = "1280";
293 		else if (IS_1080(isp))
294 			btype = "1080";
295 		else if (IS_10160(isp))
296 			btype = "10160";
297 		else if (IS_12160(isp))
298 			btype = "12160";
299 		else
300 			btype = "<UNKLVD>";
301 
302 		l = ISP_READ(isp, SXP_PINS_DIFF) & ISP1080_MODE_MASK;
303 		switch (l) {
304 		case ISP1080_LVD_MODE:
305 			sdp->isp_lvdmode = 1;
306 			isp_prt(isp, ISP_LOGCONFIG, m, 0, "LVD");
307 			break;
308 		case ISP1080_HVD_MODE:
309 			sdp->isp_diffmode = 1;
310 			isp_prt(isp, ISP_LOGCONFIG, m, 0, "Differential");
311 			break;
312 		case ISP1080_SE_MODE:
313 			sdp->isp_ultramode = 1;
314 			isp_prt(isp, ISP_LOGCONFIG, m, 0, "Single-Ended");
315 			break;
316 		default:
317 			isp_prt(isp, ISP_LOGERR,
318 			    "unknown mode on bus %d (0x%x)", 0, l);
319 			break;
320 		}
321 
322 		if (IS_DUALBUS(isp)) {
323 			sdp++;
324 			l = ISP_READ(isp, SXP_PINS_DIFF|SXP_BANK1_SELECT);
325 			l &= ISP1080_MODE_MASK;
326 			switch(l) {
327 			case ISP1080_LVD_MODE:
328 				sdp->isp_lvdmode = 1;
329 				isp_prt(isp, ISP_LOGCONFIG, m, 1, "LVD");
330 				break;
331 			case ISP1080_HVD_MODE:
332 				sdp->isp_diffmode = 1;
333 				isp_prt(isp, ISP_LOGCONFIG,
334 				    m, 1, "Differential");
335 				break;
336 			case ISP1080_SE_MODE:
337 				sdp->isp_ultramode = 1;
338 				isp_prt(isp, ISP_LOGCONFIG,
339 				    m, 1, "Single-Ended");
340 				break;
341 			default:
342 				isp_prt(isp, ISP_LOGERR,
343 				    "unknown mode on bus %d (0x%x)", 1, l);
344 				break;
345 			}
346 		}
347 	} else {
348 		sdparam *sdp = isp->isp_param;
349 		i = ISP_READ(isp, BIU_CONF0) & BIU_CONF0_HW_MASK;
350 		switch (i) {
351 		default:
352 			isp_prt(isp, ISP_LOGALL, "Unknown Chip Type 0x%x", i);
353 			/* FALLTHROUGH */
354 		case 1:
355 			btype = "1020";
356 			isp->isp_type = ISP_HA_SCSI_1020;
357 			isp->isp_clock = 40;
358 			break;
359 		case 2:
360 			/*
361 			 * Some 1020A chips are Ultra Capable, but don't
362 			 * run the clock rate up for that unless told to
363 			 * do so by the Ultra Capable bits being set.
364 			 */
365 			btype = "1020A";
366 			isp->isp_type = ISP_HA_SCSI_1020A;
367 			isp->isp_clock = 40;
368 			break;
369 		case 3:
370 			btype = "1040";
371 			isp->isp_type = ISP_HA_SCSI_1040;
372 			isp->isp_clock = 60;
373 			break;
374 		case 4:
375 			btype = "1040A";
376 			isp->isp_type = ISP_HA_SCSI_1040A;
377 			isp->isp_clock = 60;
378 			break;
379 		case 5:
380 			btype = "1040B";
381 			isp->isp_type = ISP_HA_SCSI_1040B;
382 			isp->isp_clock = 60;
383 			break;
384 		case 6:
385 			btype = "1040C";
386 			isp->isp_type = ISP_HA_SCSI_1040C;
387 			isp->isp_clock = 60;
388                         break;
389 		}
390 		/*
391 		 * Now, while we're at it, gather info about ultra
392 		 * and/or differential mode.
393 		 */
394 		if (ISP_READ(isp, SXP_PINS_DIFF) & SXP_PINS_DIFF_MODE) {
395 			isp_prt(isp, ISP_LOGCONFIG, "Differential Mode");
396 			sdp->isp_diffmode = 1;
397 		} else {
398 			sdp->isp_diffmode = 0;
399 		}
400 		i = ISP_READ(isp, RISC_PSR);
401 		if (isp->isp_bustype == ISP_BT_SBUS) {
402 			i &= RISC_PSR_SBUS_ULTRA;
403 		} else {
404 			i &= RISC_PSR_PCI_ULTRA;
405 		}
406 		if (i != 0) {
407 			isp_prt(isp, ISP_LOGCONFIG, "Ultra Mode Capable");
408 			sdp->isp_ultramode = 1;
409 			/*
410 			 * If we're in Ultra Mode, we have to be 60MHz clock-
411 			 * even for the SBus version.
412 			 */
413 			isp->isp_clock = 60;
414 		} else {
415 			sdp->isp_ultramode = 0;
416 			/*
417 			 * Clock is known. Gronk.
418 			 */
419 		}
420 
421 		/*
422 		 * Machine dependent clock (if set) overrides
423 		 * our generic determinations.
424 		 */
425 		if (isp->isp_mdvec->dv_clock) {
426 			if (isp->isp_mdvec->dv_clock < isp->isp_clock) {
427 				isp->isp_clock = isp->isp_mdvec->dv_clock;
428 			}
429 		}
430 
431 	}
432 
433 	/*
434 	 * Clear instrumentation
435 	 */
436 	isp->isp_intcnt = isp->isp_intbogus = 0;
437 
438 	/*
439 	 * Do MD specific pre initialization
440 	 */
441 	ISP_RESET0(isp);
442 
443 again:
444 
445 	/*
446 	 * Hit the chip over the head with hammer,
447 	 * and give the ISP a chance to recover.
448 	 */
449 
450 	if (IS_SCSI(isp)) {
451 		ISP_WRITE(isp, BIU_ICR, BIU_ICR_SOFT_RESET);
452 		/*
453 		 * A slight delay...
454 		 */
455 		USEC_DELAY(100);
456 
457 		/*
458 		 * Clear data && control DMA engines.
459 		 */
460 		ISP_WRITE(isp, CDMA_CONTROL,
461 		    DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
462 		ISP_WRITE(isp, DDMA_CONTROL,
463 		    DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
464 
465 
466 	} else {
467 		ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
468 		/*
469 		 * A slight delay...
470 		 */
471 		USEC_DELAY(100);
472 
473 		/*
474 		 * Clear data && control DMA engines.
475 		 */
476 		ISP_WRITE(isp, CDMA2100_CONTROL,
477 			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
478 		ISP_WRITE(isp, TDMA2100_CONTROL,
479 			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
480 		ISP_WRITE(isp, RDMA2100_CONTROL,
481 			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
482 	}
483 
484 	/*
485 	 * Wait for ISP to be ready to go...
486 	 */
487 	loops = MBOX_DELAY_COUNT;
488 	for (;;) {
489 		if (IS_SCSI(isp)) {
490 			if (!(ISP_READ(isp, BIU_ICR) & BIU_ICR_SOFT_RESET))
491 				break;
492 		} else {
493 			if (!(ISP_READ(isp, BIU2100_CSR) & BIU2100_SOFT_RESET))
494 				break;
495 		}
496 		USEC_DELAY(100);
497 		if (--loops < 0) {
498 			ISP_DUMPREGS(isp, "chip reset timed out");
499 			return;
500 		}
501 	}
502 
503 	/*
504 	 * After we've fired this chip up, zero out the conf1 register
505 	 * for SCSI adapters and other settings for the 2100.
506 	 */
507 
508 	if (IS_SCSI(isp)) {
509 		ISP_WRITE(isp, BIU_CONF1, 0);
510 	} else {
511 		ISP_WRITE(isp, BIU2100_CSR, 0);
512 	}
513 
514 	/*
515 	 * Reset RISC Processor
516 	 */
517 	ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
518 	USEC_DELAY(100);
519 	/* Clear semaphore register (just to be sure) */
520 	ISP_WRITE(isp, BIU_SEMA, 0);
521 
522 	/*
523 	 * Establish some initial burst rate stuff.
524 	 * (only for the 1XX0 boards). This really should
525 	 * be done later after fetching from NVRAM.
526 	 */
527 	if (IS_SCSI(isp)) {
528 		uint16_t tmp = isp->isp_mdvec->dv_conf1;
529 		/*
530 		 * Busted FIFO. Turn off all but burst enables.
531 		 */
532 		if (isp->isp_type == ISP_HA_SCSI_1040A) {
533 			tmp &= BIU_BURST_ENABLE;
534 		}
535 		ISP_SETBITS(isp, BIU_CONF1, tmp);
536 		if (tmp & BIU_BURST_ENABLE) {
537 			ISP_SETBITS(isp, CDMA_CONF, DMA_ENABLE_BURST);
538 			ISP_SETBITS(isp, DDMA_CONF, DMA_ENABLE_BURST);
539 		}
540 #ifdef	PTI_CARDS
541 		if (((sdparam *) isp->isp_param)->isp_ultramode) {
542 			while (ISP_READ(isp, RISC_MTR) != 0x1313) {
543 				ISP_WRITE(isp, RISC_MTR, 0x1313);
544 				ISP_WRITE(isp, HCCR, HCCR_CMD_STEP);
545 			}
546 		} else {
547 			ISP_WRITE(isp, RISC_MTR, 0x1212);
548 		}
549 		/*
550 		 * PTI specific register
551 		 */
552 		ISP_WRITE(isp, RISC_EMB, DUAL_BANK)
553 #else
554 		ISP_WRITE(isp, RISC_MTR, 0x1212);
555 #endif
556 	} else {
557 		ISP_WRITE(isp, RISC_MTR2100, 0x1212);
558 		if (IS_2200(isp) || IS_23XX(isp)) {
559 			ISP_WRITE(isp, HCCR, HCCR_2X00_DISABLE_PARITY_PAUSE);
560 		}
561 	}
562 
563 	ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE); /* release paused processor */
564 
565 	/*
566 	 * Do MD specific post initialization
567 	 */
568 	ISP_RESET1(isp);
569 
570 	/*
571 	 * Wait for everything to finish firing up.
572 	 *
573 	 * Avoid doing this on the 2312 because you can generate a PCI
574 	 * parity error (chip breakage).
575 	 */
576 	if (IS_23XX(isp)) {
577 		USEC_DELAY(5);
578 	} else {
579 		loops = MBOX_DELAY_COUNT;
580 		while (ISP_READ(isp, OUTMAILBOX0) == MBOX_BUSY) {
581 			USEC_DELAY(100);
582 			if (--loops < 0) {
583 				isp_prt(isp, ISP_LOGERR,
584 				    "MBOX_BUSY never cleared on reset");
585 				return;
586 			}
587 		}
588 	}
589 
590 	/*
591 	 * Up until this point we've done everything by just reading or
592 	 * setting registers. From this point on we rely on at least *some*
593 	 * kind of firmware running in the card.
594 	 */
595 
596 	/*
597 	 * Do some sanity checking.
598 	 */
599 	mbs.param[0] = MBOX_NO_OP;
600 	isp_mboxcmd(isp, &mbs, MBLOGALL);
601 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
602 		return;
603 	}
604 
605 	if (IS_SCSI(isp)) {
606 		mbs.param[0] = MBOX_MAILBOX_REG_TEST;
607 		mbs.param[1] = 0xdead;
608 		mbs.param[2] = 0xbeef;
609 		mbs.param[3] = 0xffff;
610 		mbs.param[4] = 0x1111;
611 		mbs.param[5] = 0xa5a5;
612 		isp_mboxcmd(isp, &mbs, MBLOGALL);
613 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
614 			return;
615 		}
616 		if (mbs.param[1] != 0xdead || mbs.param[2] != 0xbeef ||
617 		    mbs.param[3] != 0xffff || mbs.param[4] != 0x1111 ||
618 		    mbs.param[5] != 0xa5a5) {
619 			isp_prt(isp, ISP_LOGERR,
620 			    "Register Test Failed (0x%x 0x%x 0x%x 0x%x 0x%x)",
621 			    mbs.param[1], mbs.param[2], mbs.param[3],
622 			    mbs.param[4], mbs.param[5]);
623 			return;
624 		}
625 
626 	}
627 
628 	/*
629 	 * Download new Firmware, unless requested not to do so.
630 	 * This is made slightly trickier in some cases where the
631 	 * firmware of the ROM revision is newer than the revision
632 	 * compiled into the driver. So, where we used to compare
633 	 * versions of our f/w and the ROM f/w, now we just see
634 	 * whether we have f/w at all and whether a config flag
635 	 * has disabled our download.
636 	 */
637 	if ((isp->isp_mdvec->dv_ispfw == NULL) ||
638 	    (isp->isp_confopts & ISP_CFG_NORELOAD)) {
639 		dodnld = 0;
640 	}
641 
642 	if (IS_23XX(isp)) {
643 		code_org = ISP_CODE_ORG_2300;
644 	} else {
645 		code_org = ISP_CODE_ORG;
646 	}
647 	if (dodnld) {
648 		uint16_t *ptr = isp->isp_mdvec->dv_ispfw;
649 
650 		isp->isp_mbxworkp = &ptr[1];
651 		isp->isp_mbxwrk0 = ptr[3] - 1;
652 		isp->isp_mbxwrk1 = code_org + 1;
653 		mbs.param[0] = MBOX_WRITE_RAM_WORD;
654 		mbs.param[1] = code_org;
655 		mbs.param[2] = ptr[0];
656 		isp_mboxcmd(isp, &mbs, MBLOGNONE);
657 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
658 			isp_prt(isp, ISP_LOGERR,
659 			    "F/W download failed at word %d",
660 			    isp->isp_mbxwrk1 - code_org);
661 			dodnld = 0;
662 			goto again;
663 		}
664 
665 		/*
666 		 * Verify that it downloaded correctly.
667 		 */
668 		mbs.param[0] = MBOX_VERIFY_CHECKSUM;
669 		mbs.param[1] = code_org;
670 		isp_mboxcmd(isp, &mbs, MBLOGNONE);
671 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
672 			isp_prt(isp, ISP_LOGERR, "Ram Checksum Failure");
673 			return;
674 		}
675 
676 		/*
677 		 * If we're a 2322 or 2422, the firmware actually comes
678 		 * in three chunks. We loaded the first at the code_org
679 		 * address. The other two chunks, which follow right
680 		 * after each other in memory here, get loaded at addresses
681 		 * specfied at offset 0x9..0xB.
682 		 */
683 		if (IS_2322(isp)) {
684 
685 			ptr = &ptr[ptr[3]];
686 			isp->isp_mbxworkp = &ptr[1];
687 			isp->isp_mbxwrk0 = ptr[3] - 1;
688 			isp->isp_mbxwrk1 = ptr[5] + 1;
689 			isp->isp_mbxwrk8 = ptr[4];
690 			mbs.param[0] = MBOX_WRITE_RAM_WORD_EXTENDED;
691 			mbs.param[8] = ptr[4];
692 			mbs.param[1] = ptr[5];
693 			mbs.param[2] = ptr[0];
694 			isp_mboxcmd(isp, &mbs, MBLOGNONE);
695 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
696 				isp_prt(isp, ISP_LOGERR,
697 				    "Receive Sequencer F/W Load Failed");
698 				return;
699 			}
700 
701 			ptr = &ptr[ptr[3]];
702 			isp->isp_mbxworkp = &ptr[1];
703 			isp->isp_mbxwrk0 = ptr[3] - 1;
704 			isp->isp_mbxwrk1 = ptr[5] + 1;
705 			isp->isp_mbxwrk8 = ptr[4];
706 			mbs.param[0] = MBOX_WRITE_RAM_WORD_EXTENDED;
707 			mbs.param[8] = ptr[4];
708 			mbs.param[1] = ptr[5];
709 			mbs.param[2] = ptr[0];
710 			isp_mboxcmd(isp, &mbs, MBLOGNONE);
711 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
712 				isp_prt(isp, ISP_LOGERR,
713 				    "Transmit Sequencer F/W Load Failed");
714 				return;
715 			}
716 		}
717 
718 		isp->isp_loaded_fw = 1;
719 	} else {
720 		isp->isp_loaded_fw = 0;
721 		isp_prt(isp, ISP_LOGDEBUG2, "skipping f/w download");
722 	}
723 
724 	/*
725 	 * Now start it rolling.
726 	 *
727 	 * If we didn't actually download f/w,
728 	 * we still need to (re)start it.
729 	 */
730 
731 
732 	mbs.param[0] = MBOX_EXEC_FIRMWARE;
733 	mbs.param[1] = code_org;
734 	if (IS_2322(isp) || IS_24XX(isp)) {
735 		if (isp->isp_loaded_fw) {
736 			mbs.param[2] = 0;
737 		} else {
738 			mbs.param[2] = 1;
739 		}
740 		mbs.obits |= 2;
741 	}
742 
743 	isp_mboxcmd(isp, &mbs, MBLOGNONE);
744 	/*
745 	 * Give it a chance to start.
746 	 */
747 	USEC_DELAY(500);
748 
749 	if (IS_SCSI(isp)) {
750 		/*
751 		 * Set CLOCK RATE, but only if asked to.
752 		 */
753 		if (isp->isp_clock) {
754 			mbs.param[0] = MBOX_SET_CLOCK_RATE;
755 			mbs.param[1] = isp->isp_clock;
756 			isp_mboxcmd(isp, &mbs, MBLOGALL);
757 			/* we will try not to care if this fails */
758 		}
759 	}
760 
761 	mbs.param[0] = MBOX_ABOUT_FIRMWARE;
762 	isp_mboxcmd(isp, &mbs, MBLOGALL);
763 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
764 		return;
765 	}
766 
767 	/*
768 	 * The SBus firmware that we are using apparently does not return
769 	 * major, minor, micro revisions in the mailbox registers, which
770 	 * is really, really, annoying.
771 	 */
772 	if (ISP_SBUS_SUPPORTED && isp->isp_bustype == ISP_BT_SBUS) {
773 		if (dodnld) {
774 #ifdef	ISP_TARGET_MODE
775 			isp->isp_fwrev[0] = 7;
776 			isp->isp_fwrev[1] = 55;
777 #else
778 			isp->isp_fwrev[0] = 1;
779 			isp->isp_fwrev[1] = 37;
780 #endif
781 			isp->isp_fwrev[2] = 0;
782 		}
783 	} else {
784 		isp->isp_fwrev[0] = mbs.param[1];
785 		isp->isp_fwrev[1] = mbs.param[2];
786 		isp->isp_fwrev[2] = mbs.param[3];
787 	}
788 	isp_prt(isp, ISP_LOGCONFIG,
789 	    "Board Type %s, Chip Revision 0x%x, %s F/W Revision %d.%d.%d",
790 	    btype, isp->isp_revision, dodnld? "loaded" : "resident",
791 	    isp->isp_fwrev[0], isp->isp_fwrev[1], isp->isp_fwrev[2]);
792 
793 	if (IS_FC(isp)) {
794 		/*
795 		 * We do not believe firmware attributes for 2100 code less
796 		 * than 1.17.0, unless it's the firmware we specifically
797 		 * are loading.
798 		 *
799 		 * Note that all 22XX and 23XX f/w is greater than 1.X.0.
800 		 */
801 		if (!(ISP_FW_NEWER_THAN(isp, 1, 17, 0))) {
802 #ifdef	USE_SMALLER_2100_FIRMWARE
803 			FCPARAM(isp)->isp_fwattr = ISP_FW_ATTR_SCCLUN;
804 #else
805 			FCPARAM(isp)->isp_fwattr = 0;
806 #endif
807 		} else {
808 			FCPARAM(isp)->isp_fwattr = mbs.param[6];
809 			isp_prt(isp, ISP_LOGDEBUG0,
810 			    "Firmware Attributes = 0x%x", mbs.param[6]);
811 		}
812 		if (IS_2KLOGIN(isp)) {
813 			isp_prt(isp, ISP_LOGCONFIG, "2K Logins Supported");
814 		}
815 	}
816 
817 	if (isp->isp_romfw_rev[0] || isp->isp_romfw_rev[1] ||
818 	    isp->isp_romfw_rev[2]) {
819 		isp_prt(isp, ISP_LOGCONFIG, "Last F/W revision was %d.%d.%d",
820 		    isp->isp_romfw_rev[0], isp->isp_romfw_rev[1],
821 		    isp->isp_romfw_rev[2]);
822 	}
823 
824 	mbs.param[0] = MBOX_GET_FIRMWARE_STATUS;
825 	isp_mboxcmd(isp, &mbs, MBLOGALL);
826 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
827 		return;
828 	}
829 	isp->isp_maxcmds = mbs.param[2];
830 	isp_prt(isp, ISP_LOGINFO,
831 	    "%d max I/O commands supported", mbs.param[2]);
832 	isp_fw_state(isp);
833 
834 	/*
835 	 * Set up DMA for the request and result mailboxes.
836 	 */
837 	if (ISP_MBOXDMASETUP(isp) != 0) {
838 		isp_prt(isp, ISP_LOGERR, "Cannot setup DMA");
839 		return;
840 	}
841 	isp->isp_state = ISP_RESETSTATE;
842 
843 	/*
844 	 * Okay- now that we have new firmware running, we now (re)set our
845 	 * notion of how many luns we support. This is somewhat tricky because
846 	 * if we haven't loaded firmware, we sometimes do not have an easy way
847 	 * of knowing how many luns we support.
848 	 *
849 	 * Expanded lun firmware gives you 32 luns for SCSI cards and
850 	 * 16384 luns for Fibre Channel cards.
851 	 *
852 	 * It turns out that even for QLogic 2100s with ROM 1.10 and above
853 	 * we do get a firmware attributes word returned in mailbox register 6.
854 	 *
855 	 * Because the lun is in a different position in the Request Queue
856 	 * Entry structure for Fibre Channel with expanded lun firmware, we
857 	 * can only support one lun (lun zero) when we don't know what kind
858 	 * of firmware we're running.
859 	 */
860 	if (IS_SCSI(isp)) {
861 		if (dodnld) {
862 			if (IS_ULTRA2(isp) || IS_ULTRA3(isp)) {
863 				isp->isp_maxluns = 32;
864 			} else {
865 				isp->isp_maxluns = 8;
866 			}
867 		} else {
868 			isp->isp_maxluns = 8;
869 		}
870 	} else {
871 		if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) {
872 			isp->isp_maxluns = 16384;
873 		} else {
874 			isp->isp_maxluns = 16;
875 		}
876 	}
877 }
878 
879 /*
880  * Initialize Parameters of Hardware to a known state.
881  *
882  * Locks are held before coming here.
883  */
884 
885 void
886 isp_init(ispsoftc_t *isp)
887 {
888 	/*
889 	 * Must do this first to get defaults established.
890 	 */
891 	isp_setdfltparm(isp, 0);
892 	if (IS_DUALBUS(isp)) {
893 		isp_setdfltparm(isp, 1);
894 	}
895 	if (IS_FC(isp)) {
896 		isp_fibre_init(isp);
897 	} else {
898 		isp_scsi_init(isp);
899 	}
900 }
901 
902 static void
903 isp_scsi_init(ispsoftc_t *isp)
904 {
905 	sdparam *sdp_chan0, *sdp_chan1;
906 	mbreg_t mbs;
907 
908 	sdp_chan0 = isp->isp_param;
909 	sdp_chan1 = sdp_chan0;
910 	if (IS_DUALBUS(isp)) {
911 		sdp_chan1++;
912 	}
913 
914 	/*
915 	 * If we have no role (neither target nor initiator), return.
916 	 */
917 	if (isp->isp_role == ISP_ROLE_NONE) {
918 		return;
919 	}
920 
921 	/* First do overall per-card settings. */
922 
923 	/*
924 	 * If we have fast memory timing enabled, turn it on.
925 	 */
926 	if (sdp_chan0->isp_fast_mttr) {
927 		ISP_WRITE(isp, RISC_MTR, 0x1313);
928 	}
929 
930 	/*
931 	 * Set Retry Delay and Count.
932 	 * You set both channels at the same time.
933 	 */
934 	MEMZERO(&mbs, sizeof (mbs));
935 	mbs.param[0] = MBOX_SET_RETRY_COUNT;
936 	mbs.param[1] = sdp_chan0->isp_retry_count;
937 	mbs.param[2] = sdp_chan0->isp_retry_delay;
938 	mbs.param[6] = sdp_chan1->isp_retry_count;
939 	mbs.param[7] = sdp_chan1->isp_retry_delay;
940 
941 	isp_mboxcmd(isp, &mbs, MBLOGALL);
942 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
943 		return;
944 	}
945 
946 	/*
947 	 * Set ASYNC DATA SETUP time. This is very important.
948 	 */
949 	mbs.param[0] = MBOX_SET_ASYNC_DATA_SETUP_TIME;
950 	mbs.param[1] = sdp_chan0->isp_async_data_setup;
951 	mbs.param[2] = sdp_chan1->isp_async_data_setup;
952 	isp_mboxcmd(isp, &mbs, MBLOGALL);
953 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
954 		return;
955 	}
956 
957 	/*
958 	 * Set ACTIVE Negation State.
959 	 */
960 	mbs.param[0] = MBOX_SET_ACT_NEG_STATE;
961 	mbs.param[1] =
962 	    (sdp_chan0->isp_req_ack_active_neg << 4) |
963 	    (sdp_chan0->isp_data_line_active_neg << 5);
964 	mbs.param[2] =
965 	    (sdp_chan1->isp_req_ack_active_neg << 4) |
966 	    (sdp_chan1->isp_data_line_active_neg << 5);
967 
968 	isp_mboxcmd(isp, &mbs, MBLOGNONE);
969 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
970 		isp_prt(isp, ISP_LOGERR,
971 		    "failed to set active negation state (%d,%d), (%d,%d)",
972 		    sdp_chan0->isp_req_ack_active_neg,
973 		    sdp_chan0->isp_data_line_active_neg,
974 		    sdp_chan1->isp_req_ack_active_neg,
975 		    sdp_chan1->isp_data_line_active_neg);
976 		/*
977 		 * But don't return.
978 		 */
979 	}
980 
981 	/*
982 	 * Set the Tag Aging limit
983 	 */
984 	mbs.param[0] = MBOX_SET_TAG_AGE_LIMIT;
985 	mbs.param[1] = sdp_chan0->isp_tag_aging;
986 	mbs.param[2] = sdp_chan1->isp_tag_aging;
987 	isp_mboxcmd(isp, &mbs, MBLOGALL);
988 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
989 		isp_prt(isp, ISP_LOGERR, "failed to set tag age limit (%d,%d)",
990 		    sdp_chan0->isp_tag_aging, sdp_chan1->isp_tag_aging);
991 		return;
992 	}
993 
994 	/*
995 	 * Set selection timeout.
996 	 */
997 	mbs.param[0] = MBOX_SET_SELECT_TIMEOUT;
998 	mbs.param[1] = sdp_chan0->isp_selection_timeout;
999 	mbs.param[2] = sdp_chan1->isp_selection_timeout;
1000 	isp_mboxcmd(isp, &mbs, MBLOGALL);
1001 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1002 		return;
1003 	}
1004 
1005 	/* now do per-channel settings */
1006 	isp_scsi_channel_init(isp, 0);
1007 	if (IS_DUALBUS(isp))
1008 		isp_scsi_channel_init(isp, 1);
1009 
1010 	/*
1011 	 * Now enable request/response queues
1012 	 */
1013 
1014 	if (IS_ULTRA2(isp) || IS_1240(isp)) {
1015 		mbs.param[0] = MBOX_INIT_RES_QUEUE_A64;
1016 		mbs.param[1] = RESULT_QUEUE_LEN(isp);
1017 		mbs.param[2] = DMA_WD1(isp->isp_result_dma);
1018 		mbs.param[3] = DMA_WD0(isp->isp_result_dma);
1019 		mbs.param[4] = 0;
1020 		mbs.param[6] = DMA_WD3(isp->isp_result_dma);
1021 		mbs.param[7] = DMA_WD2(isp->isp_result_dma);
1022 		isp_mboxcmd(isp, &mbs, MBLOGALL);
1023 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1024 			return;
1025 		}
1026 		isp->isp_residx = mbs.param[5];
1027 
1028 		mbs.param[0] = MBOX_INIT_REQ_QUEUE_A64;
1029 		mbs.param[1] = RQUEST_QUEUE_LEN(isp);
1030 		mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
1031 		mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
1032 		mbs.param[5] = 0;
1033 		mbs.param[6] = DMA_WD3(isp->isp_result_dma);
1034 		mbs.param[7] = DMA_WD2(isp->isp_result_dma);
1035 		isp_mboxcmd(isp, &mbs, MBLOGALL);
1036 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1037 			return;
1038 		}
1039 		isp->isp_reqidx = isp->isp_reqodx = mbs.param[4];
1040 	} else {
1041 		mbs.param[0] = MBOX_INIT_RES_QUEUE;
1042 		mbs.param[1] = RESULT_QUEUE_LEN(isp);
1043 		mbs.param[2] = DMA_WD1(isp->isp_result_dma);
1044 		mbs.param[3] = DMA_WD0(isp->isp_result_dma);
1045 		mbs.param[4] = 0;
1046 		isp_mboxcmd(isp, &mbs, MBLOGALL);
1047 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1048 			return;
1049 		}
1050 		isp->isp_residx = mbs.param[5];
1051 
1052 		mbs.param[0] = MBOX_INIT_REQ_QUEUE;
1053 		mbs.param[1] = RQUEST_QUEUE_LEN(isp);
1054 		mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
1055 		mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
1056 		mbs.param[5] = 0;
1057 		isp_mboxcmd(isp, &mbs, MBLOGALL);
1058 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1059 			return;
1060 		}
1061 		isp->isp_reqidx = isp->isp_reqodx = mbs.param[4];
1062 	}
1063 
1064 	/*
1065 	 * Turn on Fast Posting, LVD transitions
1066 	 *
1067 	 * Ultra2 F/W always has had fast posting (and LVD transitions)
1068 	 *
1069 	 * Ultra and older (i.e., SBus) cards may not. It's just safer
1070 	 * to assume not for them.
1071 	 */
1072 
1073 	mbs.param[0] = MBOX_SET_FW_FEATURES;
1074 	mbs.param[1] = 0;
1075 	if (IS_ULTRA2(isp))
1076 		mbs.param[1] |= FW_FEATURE_LVD_NOTIFY;
1077 #ifndef	ISP_NO_RIO
1078 	if (IS_ULTRA2(isp) || IS_1240(isp))
1079 		mbs.param[1] |= FW_FEATURE_RIO_16BIT;
1080 #else
1081 	if (IS_ULTRA2(isp) || IS_1240(isp))
1082 		mbs.param[1] |= FW_FEATURE_FAST_POST;
1083 #endif
1084 	if (mbs.param[1] != 0) {
1085 		uint16_t sfeat = mbs.param[1];
1086 		isp_mboxcmd(isp, &mbs, MBLOGALL);
1087 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1088 			isp_prt(isp, ISP_LOGINFO,
1089 			    "Enabled FW features (0x%x)", sfeat);
1090 		}
1091 	}
1092 
1093 	/*
1094 	 * Let the outer layers decide whether to issue a SCSI bus reset.
1095 	 */
1096 	isp->isp_state = ISP_INITSTATE;
1097 }
1098 
1099 static void
1100 isp_scsi_channel_init(ispsoftc_t *isp, int channel)
1101 {
1102 	sdparam *sdp;
1103 	mbreg_t mbs;
1104 	int tgt;
1105 
1106 	sdp = isp->isp_param;
1107 	sdp += channel;
1108 
1109 	/*
1110 	 * Set (possibly new) Initiator ID.
1111 	 */
1112 	MEMZERO(&mbs, sizeof (mbs));
1113 	mbs.param[0] = MBOX_SET_INIT_SCSI_ID;
1114 	mbs.param[1] = (channel << 7) | sdp->isp_initiator_id;
1115 	isp_mboxcmd(isp, &mbs, MBLOGALL);
1116 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1117 		return;
1118 	}
1119 	isp_prt(isp, ISP_LOGINFO, "Initiator ID is %d on Channel %d",
1120 	    sdp->isp_initiator_id, channel);
1121 
1122 
1123 	/*
1124 	 * Set current per-target parameters to an initial safe minimum.
1125 	 */
1126 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
1127 		int lun;
1128 		uint16_t sdf;
1129 
1130 		if (sdp->isp_devparam[tgt].dev_enable == 0) {
1131 			continue;
1132 		}
1133 #ifndef	ISP_TARGET_MODE
1134 		sdf = sdp->isp_devparam[tgt].goal_flags;
1135 		sdf &= DPARM_SAFE_DFLT;
1136 		/*
1137 		 * It is not quite clear when this changed over so that
1138 		 * we could force narrow and async for 1000/1020 cards,
1139 		 * but assume that this is only the case for loaded
1140 		 * firmware.
1141 		 */
1142 		if (isp->isp_loaded_fw) {
1143 			sdf |= DPARM_NARROW | DPARM_ASYNC;
1144 		}
1145 #else
1146 		/*
1147 		 * The !$*!)$!$)* f/w uses the same index into some
1148 		 * internal table to decide how to respond to negotiations,
1149 		 * so if we've said "let's be safe" for ID X, and ID X
1150 		 * selects *us*, the negotiations will back to 'safe'
1151 		 * (as in narrow/async). What the f/w *should* do is
1152 		 * use the initiator id settings to decide how to respond.
1153 		 */
1154 		sdp->isp_devparam[tgt].goal_flags = sdf = DPARM_DEFAULT;
1155 #endif
1156 		mbs.param[0] = MBOX_SET_TARGET_PARAMS;
1157 		mbs.param[1] = (channel << 15) | (tgt << 8);
1158 		mbs.param[2] = sdf;
1159 		if ((sdf & DPARM_SYNC) == 0) {
1160 			mbs.param[3] = 0;
1161 		} else {
1162 			mbs.param[3] =
1163 			    (sdp->isp_devparam[tgt].goal_offset << 8) |
1164 			    (sdp->isp_devparam[tgt].goal_period);
1165 		}
1166 		isp_prt(isp, ISP_LOGDEBUG0,
1167 		    "Initial Settings bus%d tgt%d flags 0x%x off 0x%x per 0x%x",
1168 		    channel, tgt, mbs.param[2], mbs.param[3] >> 8,
1169 		    mbs.param[3] & 0xff);
1170 		isp_mboxcmd(isp, &mbs, MBLOGNONE);
1171 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1172 			sdf = DPARM_SAFE_DFLT;
1173 			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
1174 			mbs.param[1] = (tgt << 8) | (channel << 15);
1175 			mbs.param[2] = sdf;
1176 			mbs.param[3] = 0;
1177 			isp_mboxcmd(isp, &mbs, MBLOGALL);
1178 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1179 				continue;
1180 			}
1181 		}
1182 
1183 		/*
1184 		 * We don't update any information directly from the f/w
1185 		 * because we need to run at least one command to cause a
1186 		 * new state to be latched up. So, we just assume that we
1187 		 * converge to the values we just had set.
1188 		 *
1189 		 * Ensure that we don't believe tagged queuing is enabled yet.
1190 		 * It turns out that sometimes the ISP just ignores our
1191 		 * attempts to set parameters for devices that it hasn't
1192 		 * seen yet.
1193 		 */
1194 		sdp->isp_devparam[tgt].actv_flags = sdf & ~DPARM_TQING;
1195 		for (lun = 0; lun < (int) isp->isp_maxluns; lun++) {
1196 			mbs.param[0] = MBOX_SET_DEV_QUEUE_PARAMS;
1197 			mbs.param[1] = (channel << 15) | (tgt << 8) | lun;
1198 			mbs.param[2] = sdp->isp_max_queue_depth;
1199 			mbs.param[3] = sdp->isp_devparam[tgt].exc_throttle;
1200 			isp_mboxcmd(isp, &mbs, MBLOGALL);
1201 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1202 				break;
1203 			}
1204 		}
1205 	}
1206 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
1207 		if (sdp->isp_devparam[tgt].dev_refresh) {
1208 			isp->isp_sendmarker |= (1 << channel);
1209 			isp->isp_update |= (1 << channel);
1210 			break;
1211 		}
1212 	}
1213 }
1214 
1215 /*
1216  * Fibre Channel specific initialization.
1217  *
1218  * Locks are held before coming here.
1219  */
1220 static void
1221 isp_fibre_init(ispsoftc_t *isp)
1222 {
1223 	fcparam *fcp;
1224 	isp_icb_t local, *icbp = &local;
1225 	mbreg_t mbs;
1226 	int loopid;
1227 	uint64_t nwwn, pwwn;
1228 
1229 	fcp = isp->isp_param;
1230 
1231 	/*
1232 	 * Do this *before* initializing the firmware.
1233 	 */
1234 	isp_mark_getpdb_all(isp);
1235 	fcp->isp_fwstate = FW_CONFIG_WAIT;
1236 	fcp->isp_loopstate = LOOP_NIL;
1237 
1238 	/*
1239 	 * If we have no role (neither target nor initiator), return.
1240 	 */
1241 	if (isp->isp_role == ISP_ROLE_NONE) {
1242 		return;
1243 	}
1244 
1245 	loopid = fcp->isp_loopid;
1246 	MEMZERO(icbp, sizeof (*icbp));
1247 	icbp->icb_version = ICB_VERSION1;
1248 
1249 	/*
1250 	 * Firmware Options are either retrieved from NVRAM or
1251 	 * are patched elsewhere. We check them for sanity here
1252 	 * and make changes based on board revision, but otherwise
1253 	 * let others decide policy.
1254 	 */
1255 
1256 	/*
1257 	 * If this is a 2100 < revision 5, we have to turn off FAIRNESS.
1258 	 */
1259 	if ((isp->isp_type == ISP_HA_FC_2100) && isp->isp_revision < 5) {
1260 		fcp->isp_fwoptions &= ~ICBOPT_FAIRNESS;
1261 	}
1262 
1263 	/*
1264 	 * We have to use FULL LOGIN even though it resets the loop too much
1265 	 * because otherwise port database entries don't get updated after
1266 	 * a LIP- this is a known f/w bug for 2100 f/w less than 1.17.0.
1267 	 */
1268 	if (!ISP_FW_NEWER_THAN(isp, 1, 17, 0)) {
1269 		fcp->isp_fwoptions |= ICBOPT_FULL_LOGIN;
1270 	}
1271 
1272 	/*
1273 	 * Insist on Port Database Update Async notifications
1274 	 */
1275 	fcp->isp_fwoptions |= ICBOPT_PDBCHANGE_AE;
1276 
1277 	/*
1278 	 * Make sure that target role reflects into fwoptions.
1279 	 */
1280 	if (isp->isp_role & ISP_ROLE_TARGET) {
1281 		fcp->isp_fwoptions |= ICBOPT_TGT_ENABLE;
1282 	} else {
1283 		fcp->isp_fwoptions &= ~ICBOPT_TGT_ENABLE;
1284 	}
1285 
1286 	if (isp->isp_role & ISP_ROLE_INITIATOR) {
1287 		fcp->isp_fwoptions &= ~ICBOPT_INI_DISABLE;
1288 	} else {
1289 		fcp->isp_fwoptions |= ICBOPT_INI_DISABLE;
1290 	}
1291 
1292 	/*
1293 	 * Propagate all of this into the ICB structure.
1294 	 */
1295 	icbp->icb_fwoptions = fcp->isp_fwoptions;
1296 	icbp->icb_maxfrmlen = fcp->isp_maxfrmlen;
1297 	if (icbp->icb_maxfrmlen < ICB_MIN_FRMLEN ||
1298 	    icbp->icb_maxfrmlen > ICB_MAX_FRMLEN) {
1299 		isp_prt(isp, ISP_LOGERR,
1300 		    "bad frame length (%d) from NVRAM- using %d",
1301 		    fcp->isp_maxfrmlen, ICB_DFLT_FRMLEN);
1302 		icbp->icb_maxfrmlen = ICB_DFLT_FRMLEN;
1303 	}
1304 	icbp->icb_maxalloc = fcp->isp_maxalloc;
1305 	if (icbp->icb_maxalloc < 1) {
1306 		isp_prt(isp, ISP_LOGERR,
1307 		    "bad maximum allocation (%d)- using 16", fcp->isp_maxalloc);
1308 		icbp->icb_maxalloc = 16;
1309 	}
1310 	icbp->icb_execthrottle = fcp->isp_execthrottle;
1311 	if (icbp->icb_execthrottle < 1) {
1312 		isp_prt(isp, ISP_LOGERR,
1313 		    "bad execution throttle of %d- using 16",
1314 		    fcp->isp_execthrottle);
1315 		icbp->icb_execthrottle = ICB_DFLT_THROTTLE;
1316 	}
1317 	icbp->icb_retry_delay = fcp->isp_retry_delay;
1318 	icbp->icb_retry_count = fcp->isp_retry_count;
1319 	icbp->icb_hardaddr = loopid;
1320 	if (icbp->icb_hardaddr > 125) {
1321 		/*
1322 		 * We end up with these Loop IDs for F-Port topologies
1323 		 */
1324 		if (icbp->icb_hardaddr != 0xff && icbp->icb_hardaddr != 0x800) {
1325 		    isp_prt(isp, ISP_LOGERR,
1326 			"bad hard address %u- resetting to zero",
1327 			icbp->icb_hardaddr);
1328 		}
1329 		icbp->icb_hardaddr = 0;
1330 	}
1331 
1332 	/*
1333 	 * Our life seems so much better with 2200s and later with
1334 	 * the latest f/w if we set Hard Address.
1335 	 */
1336 	if (ISP_FW_NEWER_THAN(isp, 2, 2, 5)) {
1337 		icbp->icb_fwoptions |= ICBOPT_HARD_ADDRESS;
1338 	}
1339 
1340 	/*
1341 	 * Right now we just set extended options to prefer point-to-point
1342 	 * over loop based upon some soft config options.
1343 	 *
1344 	 * NB: for the 2300, ICBOPT_EXTENDED is required.
1345 	 */
1346 	if (IS_2200(isp) || IS_23XX(isp)) {
1347 		icbp->icb_fwoptions |= ICBOPT_EXTENDED;
1348 		/*
1349 		 * Prefer or force Point-To-Point instead Loop?
1350 		 */
1351 		switch(isp->isp_confopts & ISP_CFG_PORT_PREF) {
1352 		case ISP_CFG_NPORT:
1353 			icbp->icb_xfwoptions |= ICBXOPT_PTP_2_LOOP;
1354 			break;
1355 		case ISP_CFG_NPORT_ONLY:
1356 			icbp->icb_xfwoptions |= ICBXOPT_PTP_ONLY;
1357 			break;
1358 		case ISP_CFG_LPORT_ONLY:
1359 			icbp->icb_xfwoptions |= ICBXOPT_LOOP_ONLY;
1360 			break;
1361 		default:
1362 			icbp->icb_xfwoptions |= ICBXOPT_LOOP_2_PTP;
1363 			break;
1364 		}
1365 		if (IS_2200(isp)) {
1366 			if (ISP_FW_NEWER_THAN(isp, 1, 17, 0)) {
1367 				icbp->icb_xfwoptions |= ICBXOPT_RIO_16BIT;
1368 				icbp->icb_racctimer = 4;
1369 				icbp->icb_idelaytimer = 8;
1370 			}
1371 			icbp->icb_fwoptions |= ICBOPT_FAST_POST;
1372 		} else {
1373 			/*
1374 			 * QLogic recommends that FAST Posting be turned
1375 			 * off for 23XX cards and instead allow the HBA
1376 			 * to write response queue entries and interrupt
1377 			 * after a delay (ZIO).
1378 			 */
1379 			icbp->icb_fwoptions &= ~ICBOPT_FAST_POST;
1380 			if ((fcp->isp_xfwoptions & ICBXOPT_TIMER_MASK) ==
1381 			    ICBXOPT_ZIO) {
1382 				icbp->icb_xfwoptions |= ICBXOPT_ZIO;
1383 				icbp->icb_idelaytimer = 10;
1384 			}
1385 			if (isp->isp_confopts & ISP_CFG_ONEGB) {
1386 				icbp->icb_zfwoptions |= ICBZOPT_RATE_ONEGB;
1387 			} else if (isp->isp_confopts & ISP_CFG_TWOGB) {
1388 				icbp->icb_zfwoptions |= ICBZOPT_RATE_TWOGB;
1389 			} else {
1390 				icbp->icb_zfwoptions |= ICBZOPT_RATE_AUTO;
1391 			}
1392 			if (fcp->isp_zfwoptions & ICBZOPT_50_OHM) {
1393 				icbp->icb_zfwoptions |= ICBZOPT_50_OHM;
1394 			}
1395 		}
1396 	}
1397 
1398 	MEMZERO(&mbs, sizeof (mbs));
1399 
1400 	/*
1401 	 * For 22XX > 2.1.26 && 23XX, set some options.
1402 	 * XXX: Probably okay for newer 2100 f/w too.
1403 	 */
1404 	if (ISP_FW_NEWER_THAN(isp, 2, 26, 0)) {
1405 		/*
1406 		 * Turn on LIP F8 async event (1)
1407 		 * Turn on generate AE 8013 on all LIP Resets (2)
1408 		 * Disable LIP F7 switching (8)
1409 		 */
1410 		mbs.param[0] = MBOX_SET_FIRMWARE_OPTIONS;
1411 		mbs.param[1] = 0xb;
1412 		mbs.param[2] = 0;
1413 		mbs.param[3] = 0;
1414 		isp_mboxcmd(isp, &mbs, MBLOGALL);
1415 	}
1416 	icbp->icb_logintime = ICB_LOGIN_TOV;
1417 	icbp->icb_lunetimeout = ICB_LUN_ENABLE_TOV;
1418 
1419 	if (IS_23XX(isp)) {
1420 		ISP_WRITE(isp, isp->isp_rqstinrp, 0);
1421         	ISP_WRITE(isp, isp->isp_rqstoutrp, 0);
1422         	ISP_WRITE(isp, isp->isp_respinrp, 0);
1423 		ISP_WRITE(isp, isp->isp_respoutrp, 0);
1424 	}
1425 
1426 	nwwn = ISP_NODEWWN(isp);
1427 	pwwn = ISP_PORTWWN(isp);
1428 	if (nwwn && pwwn) {
1429 		icbp->icb_fwoptions |= ICBOPT_BOTH_WWNS;
1430 		MAKE_NODE_NAME_FROM_WWN(icbp->icb_nodename, nwwn);
1431 		MAKE_NODE_NAME_FROM_WWN(icbp->icb_portname, pwwn);
1432 		isp_prt(isp, ISP_LOGDEBUG1,
1433 		    "Setting ICB Node 0x%08x%08x Port 0x%08x%08x",
1434 		    ((uint32_t) (nwwn >> 32)),
1435 		    ((uint32_t) (nwwn & 0xffffffff)),
1436 		    ((uint32_t) (pwwn >> 32)),
1437 		    ((uint32_t) (pwwn & 0xffffffff)));
1438 	} else {
1439 		isp_prt(isp, ISP_LOGDEBUG1, "Not using any WWNs");
1440 		icbp->icb_fwoptions &= ~(ICBOPT_BOTH_WWNS|ICBOPT_FULL_LOGIN);
1441 	}
1442 	icbp->icb_rqstqlen = RQUEST_QUEUE_LEN(isp);
1443 	if (icbp->icb_rqstqlen < 1) {
1444 		isp_prt(isp, ISP_LOGERR, "bad request queue length");
1445 	}
1446 	icbp->icb_rsltqlen = RESULT_QUEUE_LEN(isp);
1447 	if (icbp->icb_rsltqlen < 1) {
1448 		isp_prt(isp, ISP_LOGERR, "bad result queue length");
1449 	}
1450 	icbp->icb_rqstaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_rquest_dma);
1451 	icbp->icb_rqstaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_rquest_dma);
1452 	icbp->icb_rqstaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_rquest_dma);
1453 	icbp->icb_rqstaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_rquest_dma);
1454 	icbp->icb_respaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_result_dma);
1455 	icbp->icb_respaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_result_dma);
1456 	icbp->icb_respaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_result_dma);
1457 	icbp->icb_respaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_result_dma);
1458 
1459 	isp_prt(isp, ISP_LOGDEBUG0,
1460 	    "isp_fibre_init: fwopt 0x%x xfwopt 0x%x zfwopt 0x%x",
1461 	    icbp->icb_fwoptions, icbp->icb_xfwoptions, icbp->icb_zfwoptions);
1462 
1463 	FC_SCRATCH_ACQUIRE(isp);
1464 	isp_put_icb(isp, icbp, (isp_icb_t *)fcp->isp_scratch);
1465 
1466 	/*
1467 	 * Init the firmware
1468 	 */
1469 	mbs.param[0] = MBOX_INIT_FIRMWARE;
1470 	mbs.param[1] = 0;
1471 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
1472 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
1473 	mbs.param[4] = 0;
1474 	mbs.param[5] = 0;
1475 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
1476 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
1477 	MEMORYBARRIER(isp, SYNC_SFORDEV, 0, sizeof (*icbp));
1478 	isp_mboxcmd(isp, &mbs, MBLOGALL);
1479 	FC_SCRATCH_RELEASE(isp);
1480 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1481 		return;
1482 	}
1483 	isp->isp_reqidx = isp->isp_reqodx = 0;
1484 	isp->isp_residx = 0;
1485 	isp->isp_sendmarker = 1;
1486 
1487 	/*
1488 	 * Whatever happens, we're now committed to being here.
1489 	 */
1490 	isp->isp_state = ISP_INITSTATE;
1491 }
1492 
1493 /*
1494  * Fibre Channel Support- get the port database for the id.
1495  *
1496  * Locks are held before coming here. Return 0 if success,
1497  * else failure.
1498  */
1499 
1500 static int
1501 isp_getmap(ispsoftc_t *isp, fcpos_map_t *map)
1502 {
1503 	fcparam *fcp = (fcparam *) isp->isp_param;
1504 	mbreg_t mbs;
1505 
1506 	MEMZERO(&mbs, sizeof (mbs));
1507 	mbs.param[0] = MBOX_GET_FC_AL_POSITION_MAP;
1508 	mbs.param[1] = 0;
1509 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
1510 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
1511 	/*
1512 	 * Unneeded. For the 2100, except for initializing f/w, registers
1513 	 * 4/5 have to not be written to.
1514 	 *	mbs.param[4] = 0;
1515 	 *	mbs.param[5] = 0;
1516 	 *
1517 	 */
1518 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
1519 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
1520 	FC_SCRATCH_ACQUIRE(isp);
1521 	MEMORYBARRIER(isp, SYNC_SFORDEV, 0, sizeof (fcpos_map_t));
1522 	isp_mboxcmd(isp, &mbs, MBLOGALL & ~MBOX_COMMAND_PARAM_ERROR);
1523 	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1524 		MEMCPY(map, fcp->isp_scratch, sizeof (fcpos_map_t));
1525 		map->fwmap = mbs.param[1] != 0;
1526 		FC_SCRATCH_RELEASE(isp);
1527 		return (0);
1528 	}
1529 	FC_SCRATCH_RELEASE(isp);
1530 	return (-1);
1531 }
1532 
1533 static void
1534 isp_mark_getpdb_all(ispsoftc_t *isp)
1535 {
1536 	fcparam *fcp = (fcparam *) isp->isp_param;
1537 	int i;
1538 	for (i = 0; i < MAX_FC_TARG; i++) {
1539 		fcp->portdb[i].valid = fcp->portdb[i].fabric_dev = 0;
1540 	}
1541 }
1542 
1543 static int
1544 isp_getpdb(ispsoftc_t *isp, int id, isp_pdb_t *pdbp)
1545 {
1546 	fcparam *fcp = (fcparam *) isp->isp_param;
1547 	mbreg_t mbs;
1548 
1549 	MEMZERO(&mbs, sizeof (mbs));
1550 	mbs.param[0] = MBOX_GET_PORT_DB;
1551 	if (IS_2KLOGIN(isp)) {
1552 		mbs.param[1] = id;
1553 		mbs.obits |= (1 << 10);
1554 	} else {
1555 		mbs.param[1] = id << 8;
1556 	}
1557 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
1558 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
1559 	/*
1560 	 * Unneeded. For the 2100, except for initializing f/w, registers
1561 	 * 4/5 have to not be written to.
1562 	 *	mbs.param[4] = 0;
1563 	 *	mbs.param[5] = 0;
1564 	 *
1565 	 */
1566 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
1567 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
1568 	FC_SCRATCH_ACQUIRE(isp);
1569 	MEMORYBARRIER(isp, SYNC_SFORDEV, 0, sizeof (isp_pdb_t));
1570 	isp_mboxcmd(isp, &mbs, MBLOGALL & ~MBOX_COMMAND_PARAM_ERROR);
1571 	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1572 		isp_get_pdb(isp, (isp_pdb_t *)fcp->isp_scratch, pdbp);
1573 		FC_SCRATCH_RELEASE(isp);
1574 		return (0);
1575 	}
1576 	FC_SCRATCH_RELEASE(isp);
1577 	return (-1);
1578 }
1579 
1580 static uint64_t
1581 isp_get_portname(ispsoftc_t *isp, int loopid, int nodename)
1582 {
1583 	uint64_t wwn = 0;
1584 	mbreg_t mbs;
1585 
1586 	MEMZERO(&mbs, sizeof (mbs));
1587 	mbs.param[0] = MBOX_GET_PORT_NAME;
1588 	if (IS_2KLOGIN(isp)) {
1589 		mbs.param[1] = loopid;
1590 		mbs.obits |= (1 << 10);
1591 		if (nodename) {
1592 			mbs.param[10] = 1;
1593 		}
1594 	} else {
1595 		mbs.param[1] = loopid << 8;
1596 		if (nodename) {
1597 			mbs.param[1] |= 1;
1598 		}
1599 	}
1600 	isp_mboxcmd(isp, &mbs, MBLOGALL & ~MBOX_COMMAND_PARAM_ERROR);
1601 	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1602 		wwn =
1603 		    (((uint64_t)(mbs.param[2] & 0xff))  << 56) |
1604 		    (((uint64_t)(mbs.param[2] >> 8))	<< 48) |
1605 		    (((uint64_t)(mbs.param[3] & 0xff))	<< 40) |
1606 		    (((uint64_t)(mbs.param[3] >> 8))	<< 32) |
1607 		    (((uint64_t)(mbs.param[6] & 0xff))	<< 24) |
1608 		    (((uint64_t)(mbs.param[6] >> 8))	<< 16) |
1609 		    (((uint64_t)(mbs.param[7] & 0xff))	<<  8) |
1610 		    (((uint64_t)(mbs.param[7] >> 8)));
1611 	}
1612 	return (wwn);
1613 }
1614 
1615 /*
1616  * Make sure we have good FC link and know our Loop ID.
1617  */
1618 
1619 static int
1620 isp_fclink_test(ispsoftc_t *isp, int usdelay)
1621 {
1622 	static char *toponames[] = {
1623 		"Private Loop",
1624 		"FL Port",
1625 		"N-Port to N-Port",
1626 		"F Port",
1627 		"F Port (no FLOGI_ACC response)"
1628 	};
1629 	mbreg_t mbs;
1630 	int count, check_for_fabric;
1631 	uint8_t lwfs;
1632 	fcparam *fcp;
1633 	struct lportdb *lp;
1634 	isp_pdb_t pdb;
1635 
1636 	fcp = isp->isp_param;
1637 
1638 	isp_prt(isp, ISP_LOGDEBUG0, "FC Link Test Entry");
1639 
1640 	/*
1641 	 * XXX: Here is where we would start a 'loop dead' timeout
1642 	 */
1643 
1644 	/*
1645 	 * Wait up to N microseconds for F/W to go to a ready state.
1646 	 */
1647 	lwfs = FW_CONFIG_WAIT;
1648 	count = 0;
1649 	while (count < usdelay) {
1650 		uint64_t enano;
1651 		uint32_t wrk;
1652 		NANOTIME_T hra, hrb;
1653 
1654 		GET_NANOTIME(&hra);
1655 		isp_fw_state(isp);
1656 		if (lwfs != fcp->isp_fwstate) {
1657 			isp_prt(isp, ISP_LOGINFO, "Firmware State <%s->%s>",
1658 			    isp2100_fw_statename((int)lwfs),
1659 			    isp2100_fw_statename((int)fcp->isp_fwstate));
1660 			lwfs = fcp->isp_fwstate;
1661 		}
1662 		if (fcp->isp_fwstate == FW_READY) {
1663 			break;
1664 		}
1665 		GET_NANOTIME(&hrb);
1666 
1667 		/*
1668 		 * Get the elapsed time in nanoseconds.
1669 		 * Always guaranteed to be non-zero.
1670 		 */
1671 		enano = NANOTIME_SUB(&hrb, &hra);
1672 
1673 		isp_prt(isp, ISP_LOGDEBUG1,
1674 		    "usec%d: 0x%lx->0x%lx enano 0x%x%08x",
1675 		    count, (long) GET_NANOSEC(&hra), (long) GET_NANOSEC(&hrb),
1676 		    (uint32_t)(enano >> 32), (uint32_t)(enano & 0xffffffff));
1677 
1678 		/*
1679 		 * If the elapsed time is less than 1 millisecond,
1680 		 * delay a period of time up to that millisecond of
1681 		 * waiting.
1682 		 *
1683 		 * This peculiar code is an attempt to try and avoid
1684 		 * invoking uint64_t math support functions for some
1685 		 * platforms where linkage is a problem.
1686 		 */
1687 		if (enano < (1000 * 1000)) {
1688 			count += 1000;
1689 			enano = (1000 * 1000) - enano;
1690 			while (enano > (uint64_t) 4000000000U) {
1691 				USEC_SLEEP(isp, 4000000);
1692 				enano -= (uint64_t) 4000000000U;
1693 			}
1694 			wrk = enano;
1695 			wrk /= 1000;
1696 			USEC_SLEEP(isp, wrk);
1697 		} else {
1698 			while (enano > (uint64_t) 4000000000U) {
1699 				count += 4000000;
1700 				enano -= (uint64_t) 4000000000U;
1701 			}
1702 			wrk = enano;
1703 			count += (wrk / 1000);
1704 		}
1705 	}
1706 
1707 	/*
1708 	 * If we haven't gone to 'ready' state, return.
1709 	 */
1710 	if (fcp->isp_fwstate != FW_READY) {
1711 		return (-1);
1712 	}
1713 
1714 	/*
1715 	 * Get our Loop ID (if possible). We really need to have it.
1716 	 */
1717 	MEMZERO(&mbs, sizeof (mbs));
1718 	mbs.param[0] = MBOX_GET_LOOP_ID;
1719 	isp_mboxcmd(isp, &mbs, MBLOGALL);
1720 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1721 		return (-1);
1722 	}
1723 	if (IS_2KLOGIN(isp)) {
1724 		fcp->isp_loopid = mbs.param[1];
1725 	} else {
1726 		fcp->isp_loopid = mbs.param[1] & 0xff;
1727 	}
1728 	if (fcp->isp_loopid == 0xffff) {	/* happens with 2k login f/w */
1729 		fcp->isp_loopid = MAX_FC_TARG-1;
1730 	} else if (fcp->isp_loopid >= MAX_FC_TARG) {
1731 		isp_prt(isp, ISP_LOGWARN, "bad initiator loopid (0x%x)", fcp->isp_loopid);
1732 		fcp->isp_loopid = MAX_FC_TARG-1;
1733 	}
1734 	if (IS_2200(isp) || IS_23XX(isp)) {
1735 		int topo = (int) mbs.param[6];
1736 		if (topo < TOPO_NL_PORT || topo > TOPO_PTP_STUB)
1737 			topo = TOPO_PTP_STUB;
1738 		fcp->isp_topo = topo;
1739 	} else {
1740 		fcp->isp_topo = TOPO_NL_PORT;
1741 	}
1742 	/*
1743 	 * Get the port id.
1744 	 */
1745 	fcp->isp_portid = mbs.param[2] | (mbs.param[3] << 16);
1746 
1747 	/*
1748 	 * Check to see if we're on a fabric by trying to see if we
1749 	 * can talk to the fabric name server. This can be a bit
1750 	 * tricky because if we're a 2100, we should check always
1751 	 * (in case we're connected to a server doing aliasing).
1752 	 */
1753 	fcp->isp_onfabric = 0;
1754 
1755 	if (IS_2100(isp)) {
1756 		/*
1757 		 * Don't bother with fabric if we are using really old
1758 		 * 2100 firmware. It's just not worth it.
1759 		 */
1760 		if (ISP_FW_NEWER_THAN(isp, 1, 15, 37)) {
1761 			check_for_fabric = 1;
1762 		} else {
1763 			check_for_fabric = 0;
1764 		}
1765 	} else if (fcp->isp_topo == TOPO_FL_PORT ||
1766 	    fcp->isp_topo == TOPO_F_PORT) {
1767 		check_for_fabric = 1;
1768 	} else
1769 		check_for_fabric = 0;
1770 
1771 	if (check_for_fabric && isp_getpdb(isp, FL_PORT_ID, &pdb) == 0) {
1772 		int loopid = FL_PORT_ID;
1773 		if (IS_2100(isp)) {
1774 			fcp->isp_topo = TOPO_FL_PORT;
1775 		}
1776 
1777 		if (BITS2WORD(pdb.pdb_portid_bits) == 0) {
1778 			/*
1779 			 * Crock.
1780 			 */
1781 			fcp->isp_topo = TOPO_NL_PORT;
1782 			goto not_on_fabric;
1783 		}
1784 		fcp->isp_portid = mbs.param[2] | ((int) mbs.param[3] << 16);
1785 
1786 		/*
1787 		 * Save the Fabric controller's port database entry.
1788 		 */
1789 		lp = &fcp->portdb[loopid];
1790 		lp->node_wwn =
1791 		    (((uint64_t)pdb.pdb_nodename[0]) << 56) |
1792 		    (((uint64_t)pdb.pdb_nodename[1]) << 48) |
1793 		    (((uint64_t)pdb.pdb_nodename[2]) << 40) |
1794 		    (((uint64_t)pdb.pdb_nodename[3]) << 32) |
1795 		    (((uint64_t)pdb.pdb_nodename[4]) << 24) |
1796 		    (((uint64_t)pdb.pdb_nodename[5]) << 16) |
1797 		    (((uint64_t)pdb.pdb_nodename[6]) <<  8) |
1798 		    (((uint64_t)pdb.pdb_nodename[7]));
1799 		lp->port_wwn =
1800 		    (((uint64_t)pdb.pdb_portname[0]) << 56) |
1801 		    (((uint64_t)pdb.pdb_portname[1]) << 48) |
1802 		    (((uint64_t)pdb.pdb_portname[2]) << 40) |
1803 		    (((uint64_t)pdb.pdb_portname[3]) << 32) |
1804 		    (((uint64_t)pdb.pdb_portname[4]) << 24) |
1805 		    (((uint64_t)pdb.pdb_portname[5]) << 16) |
1806 		    (((uint64_t)pdb.pdb_portname[6]) <<  8) |
1807 		    (((uint64_t)pdb.pdb_portname[7]));
1808 		lp->roles =
1809 		    (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
1810 		lp->portid = BITS2WORD(pdb.pdb_portid_bits);
1811 		lp->loopid = pdb.pdb_loopid;
1812 		lp->loggedin = lp->valid = 1;
1813 		fcp->isp_onfabric = 1;
1814 		(void) isp_async(isp, ISPASYNC_PROMENADE, &loopid);
1815 		isp_register_fc4_type(isp);
1816 	} else {
1817 not_on_fabric:
1818 		fcp->isp_onfabric = 0;
1819 		fcp->portdb[FL_PORT_ID].valid = 0;
1820 	}
1821 
1822 	fcp->isp_gbspeed = 1;
1823 	if (IS_23XX(isp)) {
1824 		mbs.param[0] = MBOX_GET_SET_DATA_RATE;
1825 		mbs.param[1] = MBGSD_GET_RATE;
1826 		/* mbs.param[2] undefined if we're just getting rate */
1827 		isp_mboxcmd(isp, &mbs, MBLOGALL);
1828 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1829 			if (mbs.param[1] == MBGSD_TWOGB) {
1830 				isp_prt(isp, ISP_LOGINFO, "2Gb link speed/s");
1831 				fcp->isp_gbspeed = 2;
1832 			}
1833 		}
1834 	}
1835 
1836 	isp_prt(isp, ISP_LOGCONFIG, topology, fcp->isp_loopid,
1837 	    fcp->isp_portid, fcp->isp_loopstate, toponames[fcp->isp_topo]);
1838 
1839 	/*
1840 	 * Announce ourselves, too. This involves synthesizing an entry.
1841 	 */
1842 	if (fcp->isp_iid_set == 0) {
1843 		fcp->isp_iid_set = 1;
1844 		fcp->isp_iid = fcp->isp_loopid;
1845 		lp = &fcp->portdb[fcp->isp_iid];
1846 	} else {
1847 		lp = &fcp->portdb[fcp->isp_iid];
1848 		if (fcp->isp_portid != lp->portid ||
1849 		    fcp->isp_loopid != lp->loopid ||
1850 		    fcp->isp_nodewwn != ISP_NODEWWN(isp) ||
1851 		    fcp->isp_portwwn != ISP_PORTWWN(isp)) {
1852 			lp->valid = 0;
1853 			count = fcp->isp_iid;
1854 			(void) isp_async(isp, ISPASYNC_PROMENADE, &count);
1855 		}
1856 	}
1857 	lp->loopid = fcp->isp_loopid;
1858 	lp->portid = fcp->isp_portid;
1859 	lp->node_wwn = ISP_NODEWWN(isp);
1860 	lp->port_wwn = ISP_PORTWWN(isp);
1861 	switch (isp->isp_role) {
1862 	case ISP_ROLE_NONE:
1863 		lp->roles = 0;
1864 		break;
1865 	case ISP_ROLE_TARGET:
1866 		lp->roles = SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT;
1867 		break;
1868 	case ISP_ROLE_INITIATOR:
1869 		lp->roles = SVC3_INI_ROLE >> SVC3_ROLE_SHIFT;
1870 		break;
1871 	case ISP_ROLE_BOTH:
1872 		lp->roles = (SVC3_INI_ROLE|SVC3_TGT_ROLE) >> SVC3_ROLE_SHIFT;
1873 		break;
1874 	}
1875 	lp->loggedin = lp->valid = 1;
1876 	count = fcp->isp_iid;
1877 	(void) isp_async(isp, ISPASYNC_PROMENADE, &count);
1878 	isp_prt(isp, ISP_LOGDEBUG0, "FC Link Test Complete");
1879 	return (0);
1880 }
1881 
1882 static const char *
1883 isp2100_fw_statename(int state)
1884 {
1885 	switch(state) {
1886 	case FW_CONFIG_WAIT:	return "Config Wait";
1887 	case FW_WAIT_AL_PA:	return "Waiting for AL_PA";
1888 	case FW_WAIT_LOGIN:	return "Wait Login";
1889 	case FW_READY:		return "Ready";
1890 	case FW_LOSS_OF_SYNC:	return "Loss Of Sync";
1891 	case FW_ERROR:		return "Error";
1892 	case FW_REINIT:		return "Re-Init";
1893 	case FW_NON_PART:	return "Nonparticipating";
1894 	default:		return "?????";
1895 	}
1896 }
1897 
1898 /*
1899  * Synchronize our soft copy of the port database with what the f/w thinks
1900  * (with a view toward possibly for a specific target....)
1901  */
1902 
1903 static int
1904 isp_pdb_sync(ispsoftc_t *isp)
1905 {
1906 	struct lportdb *lp;
1907 	fcparam *fcp = isp->isp_param;
1908 	isp_pdb_t pdb;
1909 	int loopid, base, lim;
1910 
1911 	/*
1912 	 * Make sure we're okay for doing this right now.
1913 	 */
1914 	if (fcp->isp_loopstate != LOOP_PDB_RCVD &&
1915 	    fcp->isp_loopstate != LOOP_FSCAN_DONE &&
1916 	    fcp->isp_loopstate != LOOP_LSCAN_DONE) {
1917 		return (-1);
1918 	}
1919 
1920 	if (fcp->isp_topo == TOPO_FL_PORT || fcp->isp_topo == TOPO_NL_PORT ||
1921 	    fcp->isp_topo == TOPO_N_PORT) {
1922 		if (fcp->isp_loopstate < LOOP_LSCAN_DONE) {
1923 			if (isp_scan_loop(isp) != 0) {
1924 				return (-1);
1925 			}
1926 		}
1927 	}
1928 	fcp->isp_loopstate = LOOP_SYNCING_PDB;
1929 
1930 	/*
1931 	 * If we get this far, we've settled our differences with the f/w
1932 	 * (for local loop device) and we can say that the loop state is ready.
1933 	 */
1934 
1935 	if (fcp->isp_topo == TOPO_NL_PORT) {
1936 		fcp->loop_seen_once = 1;
1937 		fcp->isp_loopstate = LOOP_READY;
1938 		return (0);
1939 	}
1940 
1941 	/*
1942 	 * Find all Fabric Entities that didn't make it from one scan to the
1943 	 * next and let the world know they went away. Scan the whole database.
1944 	 */
1945 	for (lp = &fcp->portdb[0]; lp < &fcp->portdb[MAX_FC_TARG]; lp++) {
1946 		if (lp->was_fabric_dev && lp->fabric_dev == 0) {
1947 			loopid = lp - fcp->portdb;
1948 			lp->valid = 0;	/* should already be set */
1949 			(void) isp_async(isp, ISPASYNC_PROMENADE, &loopid);
1950 			MEMZERO((void *) lp, sizeof (*lp));
1951 			continue;
1952 		}
1953 		lp->was_fabric_dev = lp->fabric_dev;
1954 	}
1955 
1956 	if (fcp->isp_topo == TOPO_FL_PORT)
1957 		base = FC_SNS_ID+1;
1958 	else
1959 		base = 0;
1960 
1961 	if (fcp->isp_topo == TOPO_N_PORT)
1962 		lim = 1;
1963 	else
1964 		lim = MAX_FC_TARG;
1965 
1966 	/*
1967 	 * Now log in any fabric devices that the outer layer has
1968 	 * left for us to see. This seems the most sane policy
1969 	 * for the moment.
1970 	 */
1971 	for (lp = &fcp->portdb[base]; lp < &fcp->portdb[lim]; lp++) {
1972 		uint32_t portid;
1973 		mbreg_t mbs;
1974 
1975 		loopid = lp - fcp->portdb;
1976 		if (loopid >= FL_PORT_ID && loopid <= FC_SNS_ID) {
1977 			continue;
1978 		}
1979 
1980 		/*
1981 		 * Anything here?
1982 		 */
1983 		if (lp->port_wwn == 0) {
1984 			continue;
1985 		}
1986 
1987 		/*
1988 		 * Don't try to log into yourself.
1989 		 */
1990 		if ((portid = lp->portid) == fcp->isp_portid) {
1991 			continue;
1992 		}
1993 
1994 
1995 		/*
1996 		 * If we'd been logged in- see if we still are and we haven't
1997 		 * changed. If so, no need to log ourselves out, etc..
1998 		 *
1999 		 * Unfortunately, our charming Qlogic f/w has decided to
2000 		 * return a valid port database entry for a fabric device
2001 		 * that has, in fact, gone away. And it hangs trying to
2002 		 * log it out.
2003 		 */
2004 		if (lp->loggedin && lp->force_logout == 0 &&
2005 		    isp_getpdb(isp, lp->loopid, &pdb) == 0) {
2006 			int nrole;
2007 			uint64_t nwwnn, nwwpn;
2008 			nwwnn =
2009 			    (((uint64_t)pdb.pdb_nodename[0]) << 56) |
2010 			    (((uint64_t)pdb.pdb_nodename[1]) << 48) |
2011 			    (((uint64_t)pdb.pdb_nodename[2]) << 40) |
2012 			    (((uint64_t)pdb.pdb_nodename[3]) << 32) |
2013 			    (((uint64_t)pdb.pdb_nodename[4]) << 24) |
2014 			    (((uint64_t)pdb.pdb_nodename[5]) << 16) |
2015 			    (((uint64_t)pdb.pdb_nodename[6]) <<  8) |
2016 			    (((uint64_t)pdb.pdb_nodename[7]));
2017 			nwwpn =
2018 			    (((uint64_t)pdb.pdb_portname[0]) << 56) |
2019 			    (((uint64_t)pdb.pdb_portname[1]) << 48) |
2020 			    (((uint64_t)pdb.pdb_portname[2]) << 40) |
2021 			    (((uint64_t)pdb.pdb_portname[3]) << 32) |
2022 			    (((uint64_t)pdb.pdb_portname[4]) << 24) |
2023 			    (((uint64_t)pdb.pdb_portname[5]) << 16) |
2024 			    (((uint64_t)pdb.pdb_portname[6]) <<  8) |
2025 			    (((uint64_t)pdb.pdb_portname[7]));
2026 			nrole = (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >>
2027 			    SVC3_ROLE_SHIFT;
2028 			if (pdb.pdb_loopid == lp->loopid && lp->portid ==
2029 			    (uint32_t) BITS2WORD(pdb.pdb_portid_bits) &&
2030 			    nwwnn == lp->node_wwn && nwwpn == lp->port_wwn &&
2031 			    lp->roles == nrole && lp->force_logout == 0) {
2032 				lp->loggedin = lp->valid = 1;
2033 				isp_prt(isp, ISP_LOGCONFIG, lretained,
2034 				    (int) (lp - fcp->portdb),
2035 				    (int) lp->loopid, lp->portid);
2036 				continue;
2037 			}
2038 		}
2039 
2040 		if (fcp->isp_fwstate != FW_READY ||
2041 		    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
2042 			return (-1);
2043 		}
2044 
2045 		/*
2046 		 * Force a logout if we were logged in.
2047 		 */
2048 		if (lp->loggedin) {
2049 			if (lp->force_logout ||
2050 			    isp_getpdb(isp, lp->loopid, &pdb) == 0) {
2051 				MEMZERO(&mbs, sizeof (mbs));
2052 				mbs.param[0] = MBOX_FABRIC_LOGOUT;
2053 				if (IS_2KLOGIN(isp)) {
2054 					mbs.param[1] = lp->loopid;
2055 					mbs.obits |= (1 << 10);
2056 				} else {
2057 					mbs.param[1] = lp->loopid << 8;
2058 				}
2059 				mbs.param[2] = 0;
2060 				mbs.param[3] = 0;
2061 				isp_mboxcmd(isp, &mbs, MBLOGNONE);
2062 				isp_prt(isp, ISP_LOGINFO, plogout,
2063 				    (int) (lp - fcp->portdb), lp->loopid,
2064 				    lp->portid);
2065 			}
2066 			lp->force_logout = lp->loggedin = 0;
2067 			if (fcp->isp_fwstate != FW_READY ||
2068 			    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
2069 				return (-1);
2070 			}
2071 		}
2072 
2073 		/*
2074 		 * And log in....
2075 		 */
2076 		loopid = lp - fcp->portdb;
2077 		lp->loopid = FL_PORT_ID;
2078 		do {
2079 			MEMZERO(&mbs, sizeof (mbs));
2080 			mbs.param[0] = MBOX_FABRIC_LOGIN;
2081 			if (IS_2KLOGIN(isp)) {
2082 				mbs.param[1] = loopid;
2083 				mbs.obits |= (1 << 10);
2084 			} else {
2085 				mbs.param[1] = loopid << 8;
2086 			}
2087 			mbs.param[2] = portid >> 16;
2088 			mbs.param[3] = portid & 0xffff;
2089 			isp_mboxcmd(isp, &mbs, MBLOGALL & ~(MBOX_LOOP_ID_USED |
2090 			    MBOX_PORT_ID_USED | MBOX_COMMAND_ERROR));
2091 			if (fcp->isp_fwstate != FW_READY ||
2092 			    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
2093 				return (-1);
2094 			}
2095 			switch (mbs.param[0]) {
2096 			case MBOX_LOOP_ID_USED:
2097 				/*
2098 				 * Try the next available loop id.
2099 				 */
2100 				loopid++;
2101 				break;
2102 			case MBOX_PORT_ID_USED:
2103 				/*
2104 				 * This port is already logged in.
2105 				 * Snaffle the loop id it's using if it's
2106 				 * nonzero, otherwise we're hosed.
2107 				 */
2108 				if (mbs.param[1] != 0) {
2109 					loopid = mbs.param[1];
2110 					if (loopid >= MAX_FC_TARG) {
2111 						loopid = MAX_FC_TARG;
2112 						break;
2113 					}
2114 					isp_prt(isp, ISP_LOGINFO, retained,
2115 					    loopid, (int) (lp - fcp->portdb),
2116 					    lp->portid);
2117 				} else {
2118 					loopid = MAX_FC_TARG;
2119 					break;
2120 				}
2121 				/* FALLTHROUGH */
2122 			case MBOX_COMMAND_COMPLETE:
2123 				lp->loggedin = 1;
2124 				lp->loopid = loopid;
2125 				break;
2126 			case MBOX_COMMAND_ERROR:
2127 				isp_prt(isp, ISP_LOGINFO, plogierr,
2128 				    portid, mbs.param[1]);
2129 				/* FALLTHROUGH */
2130 			case MBOX_ALL_IDS_USED: /* We're outta IDs */
2131 			default:
2132 				loopid = MAX_FC_TARG;
2133 				break;
2134 			}
2135 		} while (lp->loopid == FL_PORT_ID && loopid < MAX_FC_TARG);
2136 
2137 		/*
2138 		 * If we get here and we haven't set a Loop ID,
2139 		 * we failed to log into this device.
2140 		 */
2141 
2142 		if (lp->loopid == FL_PORT_ID) {
2143 			lp->loopid = 0;
2144 			continue;
2145 		}
2146 
2147 		/*
2148 		 * Make sure we can get the approriate port information.
2149 		 */
2150 		if (isp_getpdb(isp, lp->loopid, &pdb) != 0) {
2151 			isp_prt(isp, ISP_LOGWARN, nopdb, lp->portid);
2152 			goto dump_em;
2153 		}
2154 
2155 		if (fcp->isp_fwstate != FW_READY ||
2156 		    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
2157 			return (-1);
2158 		}
2159 
2160 		if (pdb.pdb_loopid != lp->loopid) {
2161 			isp_prt(isp, ISP_LOGWARN, pdbmfail1,
2162 			    lp->portid, pdb.pdb_loopid);
2163 			goto dump_em;
2164 		}
2165 
2166 		if (lp->portid != (uint32_t) BITS2WORD(pdb.pdb_portid_bits)) {
2167 			isp_prt(isp, ISP_LOGWARN, pdbmfail2,
2168 			    lp->portid, BITS2WORD(pdb.pdb_portid_bits));
2169 			goto dump_em;
2170 		}
2171 
2172 		lp->roles =
2173 		    (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
2174 		lp->node_wwn =
2175 		    (((uint64_t)pdb.pdb_nodename[0]) << 56) |
2176 		    (((uint64_t)pdb.pdb_nodename[1]) << 48) |
2177 		    (((uint64_t)pdb.pdb_nodename[2]) << 40) |
2178 		    (((uint64_t)pdb.pdb_nodename[3]) << 32) |
2179 		    (((uint64_t)pdb.pdb_nodename[4]) << 24) |
2180 		    (((uint64_t)pdb.pdb_nodename[5]) << 16) |
2181 		    (((uint64_t)pdb.pdb_nodename[6]) <<  8) |
2182 		    (((uint64_t)pdb.pdb_nodename[7]));
2183 		lp->port_wwn =
2184 		    (((uint64_t)pdb.pdb_portname[0]) << 56) |
2185 		    (((uint64_t)pdb.pdb_portname[1]) << 48) |
2186 		    (((uint64_t)pdb.pdb_portname[2]) << 40) |
2187 		    (((uint64_t)pdb.pdb_portname[3]) << 32) |
2188 		    (((uint64_t)pdb.pdb_portname[4]) << 24) |
2189 		    (((uint64_t)pdb.pdb_portname[5]) << 16) |
2190 		    (((uint64_t)pdb.pdb_portname[6]) <<  8) |
2191 		    (((uint64_t)pdb.pdb_portname[7]));
2192 		/*
2193 		 * Check to make sure this all makes sense.
2194 		 */
2195 		if (lp->node_wwn && lp->port_wwn) {
2196 			lp->valid = 1;
2197 			loopid = lp - fcp->portdb;
2198 			(void) isp_async(isp, ISPASYNC_PROMENADE, &loopid);
2199 			continue;
2200 		}
2201 dump_em:
2202 		lp->valid = 0;
2203 		isp_prt(isp, ISP_LOGINFO,
2204 		    ldumped, loopid, lp->loopid, lp->portid);
2205 		MEMZERO(&mbs, sizeof (mbs));
2206 		mbs.param[0] = MBOX_FABRIC_LOGOUT;
2207 		if (IS_2KLOGIN(isp)) {
2208 			mbs.param[1] = lp->loopid;
2209 			mbs.obits |= (1 << 10);
2210 		} else {
2211 			mbs.param[1] = lp->loopid << 8;
2212 		}
2213 		isp_mboxcmd(isp, &mbs, MBLOGNONE);
2214 		if (fcp->isp_fwstate != FW_READY ||
2215 		    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
2216 			return (-1);
2217 		}
2218 	}
2219 	/*
2220 	 * If we get here, we've for sure seen not only a valid loop
2221 	 * but know what is or isn't on it, so mark this for usage
2222 	 * in isp_start.
2223 	 */
2224 	fcp->loop_seen_once = 1;
2225 	fcp->isp_loopstate = LOOP_READY;
2226 	return (0);
2227 }
2228 
2229 static int
2230 isp_scan_loop(ispsoftc_t *isp)
2231 {
2232 	struct lportdb *lp;
2233 	fcparam *fcp = isp->isp_param;
2234 	isp_pdb_t pdb;
2235 	int loopid, lim, hival;
2236 
2237 	switch (fcp->isp_topo) {
2238 	case TOPO_NL_PORT:
2239 		hival = FL_PORT_ID;
2240 		break;
2241 	case TOPO_N_PORT:
2242 		hival = 2;
2243 		break;
2244 	case TOPO_FL_PORT:
2245 		hival = FC_PORT_ID;
2246 		break;
2247 	default:
2248 		isp_prt(isp, ISP_LOGDEBUG0, "no loop topology to scan");
2249 		fcp->isp_loopstate = LOOP_LSCAN_DONE;
2250 		return (0);
2251 	}
2252 	fcp->isp_loopstate = LOOP_SCANNING_LOOP;
2253 
2254 	isp_prt(isp, ISP_LOGDEBUG0, "scanning local loop 0..%d", hival);
2255 
2256 
2257 	/*
2258 	 * make sure the temp port database is clean...
2259 	 */
2260 	MEMZERO((void *)fcp->tport, sizeof (fcp->tport));
2261 
2262 	/*
2263 	 * Run through the local loop ports and get port database info
2264 	 * for each loop ID.
2265 	 *
2266 	 * There's a somewhat unexplained situation where the f/w passes back
2267 	 * the wrong database entity- if that happens, just restart (up to
2268 	 * FL_PORT_ID times).
2269 	 */
2270 	for (lim = loopid = 0; loopid < hival; loopid++) {
2271 		lp = &fcp->tport[loopid];
2272 
2273 		/*
2274 		 * Don't even try for ourselves...
2275 	 	 */
2276 		if (loopid == fcp->isp_loopid) {
2277 			continue;
2278 		}
2279 
2280 		if (IS_2100(isp) || IS_2200(isp)) {
2281 			lp->node_wwn = isp_get_portname(isp, loopid, 1);
2282 			if (fcp->isp_loopstate < LOOP_SCANNING_LOOP) {
2283 				return (-1);
2284 			}
2285 			if (lp->node_wwn == 0) {
2286 				continue;
2287 			}
2288 		}
2289 
2290 		/*
2291 		 * Get an entry....
2292 		 */
2293 		if (isp_getpdb(isp, loopid, &pdb) != 0) {
2294 			if (fcp->isp_loopstate < LOOP_SCANNING_LOOP) {
2295 				return (-1);
2296 			}
2297 			continue;
2298 		}
2299 
2300 		if (fcp->isp_loopstate < LOOP_SCANNING_LOOP) {
2301 			return (-1);
2302 		}
2303 
2304 		/*
2305 		 * If the returned database element doesn't match what we
2306 		 * asked for, restart the process entirely (up to a point...).
2307 		 */
2308 		if (pdb.pdb_loopid != loopid) {
2309 			loopid = 0;
2310 			if (lim++ < hival) {
2311 				continue;
2312 			}
2313 			isp_prt(isp, ISP_LOGWARN,
2314 			    "giving up on synchronizing the port database");
2315 			return (-1);
2316 		}
2317 
2318 		/*
2319 		 * Save the pertinent info locally.
2320 		 */
2321 		lp->node_wwn =
2322 		    (((uint64_t)pdb.pdb_nodename[0]) << 56) |
2323 		    (((uint64_t)pdb.pdb_nodename[1]) << 48) |
2324 		    (((uint64_t)pdb.pdb_nodename[2]) << 40) |
2325 		    (((uint64_t)pdb.pdb_nodename[3]) << 32) |
2326 		    (((uint64_t)pdb.pdb_nodename[4]) << 24) |
2327 		    (((uint64_t)pdb.pdb_nodename[5]) << 16) |
2328 		    (((uint64_t)pdb.pdb_nodename[6]) <<  8) |
2329 		    (((uint64_t)pdb.pdb_nodename[7]));
2330 		lp->port_wwn =
2331 		    (((uint64_t)pdb.pdb_portname[0]) << 56) |
2332 		    (((uint64_t)pdb.pdb_portname[1]) << 48) |
2333 		    (((uint64_t)pdb.pdb_portname[2]) << 40) |
2334 		    (((uint64_t)pdb.pdb_portname[3]) << 32) |
2335 		    (((uint64_t)pdb.pdb_portname[4]) << 24) |
2336 		    (((uint64_t)pdb.pdb_portname[5]) << 16) |
2337 		    (((uint64_t)pdb.pdb_portname[6]) <<  8) |
2338 		    (((uint64_t)pdb.pdb_portname[7]));
2339 		lp->roles =
2340 		    (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
2341 		lp->portid = BITS2WORD(pdb.pdb_portid_bits);
2342 		lp->loopid = pdb.pdb_loopid;
2343 	}
2344 
2345 	/*
2346 	 * Mark all of the permanent local loop database entries as invalid
2347 	 * (except our own entry).
2348 	 */
2349 	for (loopid = 0; loopid < hival; loopid++) {
2350 		if (loopid == fcp->isp_iid) {
2351 			fcp->portdb[loopid].valid = 1;
2352 			fcp->portdb[loopid].loopid = fcp->isp_loopid;
2353 			continue;
2354 		}
2355 		fcp->portdb[loopid].valid = 0;
2356 	}
2357 
2358 	/*
2359 	 * Now merge our local copy of the port database into our saved copy.
2360 	 * Notify the outer layers of new devices arriving.
2361 	 */
2362 	for (loopid = 0; loopid < hival; loopid++) {
2363 		int i;
2364 
2365 		/*
2366 		 * If we don't have a non-zero Port WWN, we're not here.
2367 		 */
2368 		if (fcp->tport[loopid].port_wwn == 0) {
2369 			continue;
2370 		}
2371 
2372 		/*
2373 		 * Skip ourselves.
2374 		 */
2375 		if (loopid == fcp->isp_iid) {
2376 			continue;
2377 		}
2378 
2379 		/*
2380 		 * For the purposes of deciding whether this is the
2381 		 * 'same' device or not, we only search for an identical
2382 		 * Port WWN. Node WWNs may or may not be the same as
2383 		 * the Port WWN, and there may be multiple different
2384 		 * Port WWNs with the same Node WWN. It would be chaos
2385 		 * to have multiple identical Port WWNs, so we don't
2386 		 * allow that.
2387 		 */
2388 
2389 		for (i = 0; i < hival; i++) {
2390 			int j;
2391 			if (fcp->portdb[i].port_wwn == 0)
2392 				continue;
2393 			if (fcp->portdb[i].port_wwn !=
2394 			    fcp->tport[loopid].port_wwn)
2395 				continue;
2396 			/*
2397 			 * We found this WWN elsewhere- it's changed
2398 			 * loopids then. We don't change it's actual
2399 			 * position in our cached port database- we
2400 			 * just change the actual loop ID we'd use.
2401 			 */
2402 			if (fcp->portdb[i].loopid != loopid) {
2403 				isp_prt(isp, ISP_LOGINFO, portshift, i,
2404 				    fcp->portdb[i].loopid,
2405 				    fcp->portdb[i].portid, loopid,
2406 				    fcp->tport[loopid].portid);
2407 			}
2408 			fcp->portdb[i].portid = fcp->tport[loopid].portid;
2409 			fcp->portdb[i].loopid = loopid;
2410 			fcp->portdb[i].valid = 1;
2411 			fcp->portdb[i].roles = fcp->tport[loopid].roles;
2412 
2413 			/*
2414 			 * Now make sure this Port WWN doesn't exist elsewhere
2415 			 * in the port database.
2416 			 */
2417 			for (j = i+1; j < hival; j++) {
2418 				if (fcp->portdb[i].port_wwn !=
2419 				    fcp->portdb[j].port_wwn) {
2420 					continue;
2421 				}
2422 				isp_prt(isp, ISP_LOGWARN, portdup, j, i);
2423 				/*
2424 				 * Invalidate the 'old' *and* 'new' ones.
2425 				 * This is really harsh and not quite right,
2426 				 * but if this happens, we really don't know
2427 				 * who is what at this point.
2428 				 */
2429 				fcp->portdb[i].valid = 0;
2430 				fcp->portdb[j].valid = 0;
2431 			}
2432 			break;
2433 		}
2434 
2435 		/*
2436 		 * If we didn't traverse the entire port database,
2437 		 * then we found (and remapped) an existing entry.
2438 		 * No need to notify anyone- go for the next one.
2439 		 */
2440 		if (i < hival) {
2441 			isp_prt(isp, ISP_LOGINFO, retained,
2442 			    fcp->portdb[i].loopid, i, fcp->portdb[i].portid);
2443 			continue;
2444 		}
2445 
2446 		/*
2447 		 * We've not found this Port WWN anywhere. It's a new entry.
2448 		 * See if we can leave it where it is (with target == loopid).
2449 		 */
2450 		if (fcp->portdb[loopid].port_wwn != 0) {
2451 			for (lim = 0; lim < hival; lim++) {
2452 				if (fcp->portdb[lim].port_wwn == 0)
2453 					break;
2454 			}
2455 			/* "Cannot Happen" */
2456 			if (lim == hival) {
2457 				isp_prt(isp, ISP_LOGWARN, "Remap Overflow");
2458 				continue;
2459 			}
2460 			i = lim;
2461 		} else {
2462 			i = loopid;
2463 		}
2464 
2465 		/*
2466 		 * NB:	The actual loopid we use here is loopid- we may
2467 		 *	in fact be at a completely different index (target).
2468 		 */
2469 		fcp->portdb[i].loopid = loopid;
2470 		fcp->portdb[i].port_wwn = fcp->tport[loopid].port_wwn;
2471 		fcp->portdb[i].node_wwn = fcp->tport[loopid].node_wwn;
2472 		fcp->portdb[i].roles = fcp->tport[loopid].roles;
2473 		fcp->portdb[i].portid = fcp->tport[loopid].portid;
2474 		fcp->portdb[i].valid = 1;
2475 
2476 		/*
2477 		 * Tell the outside world we've arrived.
2478 		 */
2479 		(void) isp_async(isp, ISPASYNC_PROMENADE, &i);
2480 	}
2481 
2482 	/*
2483 	 * Now find all previously used targets that are now invalid and
2484 	 * notify the outer layers that they're gone.
2485 	 */
2486 	for (lp = &fcp->portdb[0]; lp < &fcp->portdb[hival]; lp++) {
2487 		if (lp->valid || lp->port_wwn == 0) {
2488 			continue;
2489 		}
2490 
2491 		/*
2492 		 * Tell the outside world we've gone
2493 		 * away and erase our pdb entry.
2494 		 *
2495 		 */
2496 		loopid = lp - fcp->portdb;
2497 		(void) isp_async(isp, ISPASYNC_PROMENADE, &loopid);
2498 		MEMZERO((void *) lp, sizeof (*lp));
2499 	}
2500 	fcp->isp_loopstate = LOOP_LSCAN_DONE;
2501 	return (0);
2502 }
2503 
2504 
2505 static int
2506 isp_fabric_mbox_cmd(ispsoftc_t *isp, mbreg_t *mbp)
2507 {
2508 	isp_mboxcmd(isp, mbp, MBLOGNONE);
2509 	if (mbp->param[0] != MBOX_COMMAND_COMPLETE) {
2510 		if (FCPARAM(isp)->isp_loopstate == LOOP_SCANNING_FABRIC) {
2511 			FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
2512 		}
2513 		if (mbp->param[0] == MBOX_COMMAND_ERROR) {
2514 			char tbuf[16];
2515 			char *m;
2516 			switch (mbp->param[1]) {
2517 			case 1:
2518 				m = "No Loop";
2519 				break;
2520 			case 2:
2521 				m = "Failed to allocate IOCB buffer";
2522 				break;
2523 			case 3:
2524 				m = "Failed to allocate XCB buffer";
2525 				break;
2526 			case 4:
2527 				m = "timeout or transmit failed";
2528 				break;
2529 			case 5:
2530 				m = "no fabric loop";
2531 				break;
2532 			case 6:
2533 				m = "remote device not a target";
2534 				break;
2535 			default:
2536 				SNPRINTF(tbuf, sizeof tbuf, "%x",
2537 				    mbp->param[1]);
2538 				m = tbuf;
2539 				break;
2540 			}
2541 			isp_prt(isp, ISP_LOGERR, "SNS Failed- %s", m);
2542 		}
2543 		return (-1);
2544 	}
2545 
2546 	if (FCPARAM(isp)->isp_fwstate != FW_READY ||
2547 	    FCPARAM(isp)->isp_loopstate < LOOP_SCANNING_FABRIC) {
2548 		return (-1);
2549 	}
2550 	return(0);
2551 }
2552 
2553 #ifdef	ISP_USE_GA_NXT
2554 static int
2555 isp_scan_fabric(ispsoftc_t *isp, int ftype)
2556 {
2557 	fcparam *fcp = isp->isp_param;
2558 	uint32_t portid, first_portid, last_portid;
2559 	int hicap, last_port_same;
2560 
2561 	if (fcp->isp_onfabric == 0) {
2562 		fcp->isp_loopstate = LOOP_FSCAN_DONE;
2563 		return (0);
2564 	}
2565 
2566 	isp_prt(isp, ISP_LOGDEBUG0, "scanning fabric (GA_NXT)");
2567 
2568 	FC_SCRATCH_ACQUIRE(isp);
2569 
2570 	/*
2571 	 * Since Port IDs are 24 bits, we can check against having seen
2572 	 * anything yet with this value.
2573 	 */
2574 	last_port_same = 0;
2575 	last_portid = 0xffffffff;	/* not a port */
2576 	first_portid = portid = fcp->isp_portid;
2577 	fcp->isp_loopstate = LOOP_SCANNING_FABRIC;
2578 
2579 	for (hicap = 0; hicap < GA_NXT_MAX; hicap++) {
2580 		mbreg_t mbs;
2581 		sns_screq_t *rq;
2582 		sns_ga_nxt_rsp_t *rs0, *rs1;
2583 		struct lportdb lcl;
2584 		uint8_t sc[SNS_GA_NXT_RESP_SIZE];
2585 
2586 		rq = (sns_screq_t *)sc;
2587 		MEMZERO((void *) rq, SNS_GA_NXT_REQ_SIZE);
2588 		rq->snscb_rblen = SNS_GA_NXT_RESP_SIZE >> 1;
2589 		rq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+0x100);
2590 		rq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+0x100);
2591 		rq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+0x100);
2592 		rq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+0x100);
2593 		rq->snscb_sblen = 6;
2594 		rq->snscb_data[0] = SNS_GA_NXT;
2595 		rq->snscb_data[4] = portid & 0xffff;
2596 		rq->snscb_data[5] = (portid >> 16) & 0xff;
2597 		isp_put_sns_request(isp, rq, (sns_screq_t *) fcp->isp_scratch);
2598 		MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GA_NXT_REQ_SIZE);
2599 		MEMZERO(&mbs, sizeof (mbs));
2600 		mbs.param[0] = MBOX_SEND_SNS;
2601 		mbs.param[1] = SNS_GA_NXT_REQ_SIZE >> 1;
2602 		mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2603 		mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2604 		/*
2605 		 * Leave 4 and 5 alone
2606 		 */
2607 		mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2608 		mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2609 		if (isp_fabric_mbox_cmd(isp, &mbs)) {
2610 			if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
2611 				fcp->isp_loopstate = LOOP_PDB_RCVD;
2612 			}
2613 			FC_SCRATCH_RELEASE(isp);
2614 			return (-1);
2615 		}
2616 		MEMORYBARRIER(isp, SYNC_SFORCPU, 0x100, SNS_GA_NXT_RESP_SIZE);
2617 		rs1 = (sns_ga_nxt_rsp_t *) sc;
2618 		rs0 = (sns_ga_nxt_rsp_t *) ((uint8_t *)fcp->isp_scratch+0x100);
2619 		isp_get_ga_nxt_response(isp, rs0, rs1);
2620 		if (rs1->snscb_cthdr.ct_response != FS_ACC) {
2621 			int level;
2622 			if (rs1->snscb_cthdr.ct_reason == 9 &&
2623 			    rs1->snscb_cthdr.ct_explanation == 7)
2624 				level = ISP_LOGDEBUG0;
2625 			else
2626 				level = ISP_LOGWARN;
2627 			isp_prt(isp, level, swrej, "GA_NXT",
2628 			    rs1->snscb_cthdr.ct_reason,
2629 			    rs1->snscb_cthdr.ct_explanation, portid);
2630 			FC_SCRATCH_RELEASE(isp);
2631 			fcp->isp_loopstate = LOOP_FSCAN_DONE;
2632 			return (0);
2633 		}
2634 		portid =
2635 		    (((uint32_t) rs1->snscb_port_id[0]) << 16) |
2636 		    (((uint32_t) rs1->snscb_port_id[1]) << 8) |
2637 		    (((uint32_t) rs1->snscb_port_id[2]));
2638 
2639 		/*
2640 		 * XXX: We should check to make sure that this entry
2641 		 * XXX: supports the type(s) we are interested in.
2642 		 */
2643 		/*
2644 		 * Okay, we now have information about a fabric object.
2645 		 * If it is the type we're interested in, tell the outer layers
2646 		 * about it. The outer layer needs to  know: Port ID, WWNN,
2647 		 * WWPN, FC4 type, and port type.
2648 		 *
2649 		 * The lportdb structure is adequate for this.
2650 		 */
2651 		MEMZERO(&lcl, sizeof (lcl));
2652 		lcl.port_type = rs1->snscb_port_type;
2653 		lcl.fc4_type = ftype;
2654 		lcl.portid = portid;
2655 		lcl.node_wwn =
2656 		    (((uint64_t)rs1->snscb_nodename[0]) << 56) |
2657 		    (((uint64_t)rs1->snscb_nodename[1]) << 48) |
2658 		    (((uint64_t)rs1->snscb_nodename[2]) << 40) |
2659 		    (((uint64_t)rs1->snscb_nodename[3]) << 32) |
2660 		    (((uint64_t)rs1->snscb_nodename[4]) << 24) |
2661 		    (((uint64_t)rs1->snscb_nodename[5]) << 16) |
2662 		    (((uint64_t)rs1->snscb_nodename[6]) <<  8) |
2663 		    (((uint64_t)rs1->snscb_nodename[7]));
2664 		lcl.port_wwn =
2665 		    (((uint64_t)rs1->snscb_portname[0]) << 56) |
2666 		    (((uint64_t)rs1->snscb_portname[1]) << 48) |
2667 		    (((uint64_t)rs1->snscb_portname[2]) << 40) |
2668 		    (((uint64_t)rs1->snscb_portname[3]) << 32) |
2669 		    (((uint64_t)rs1->snscb_portname[4]) << 24) |
2670 		    (((uint64_t)rs1->snscb_portname[5]) << 16) |
2671 		    (((uint64_t)rs1->snscb_portname[6]) <<  8) |
2672 		    (((uint64_t)rs1->snscb_portname[7]));
2673 
2674 		/*
2675 		 * Does this fabric object support the type we want?
2676 		 * If not, skip it.
2677 		 */
2678 		if (rs1->snscb_fc4_types[ftype >> 5] & (1 << (ftype & 0x1f))) {
2679 			if (first_portid == portid) {
2680 				lcl.last_fabric_dev = 1;
2681 			} else {
2682 				lcl.last_fabric_dev = 0;
2683 			}
2684 			(void) isp_async(isp, ISPASYNC_FABRIC_DEV, &lcl);
2685 		} else {
2686 			isp_prt(isp, ISP_LOGDEBUG0,
2687 			    "PortID 0x%x doesn't support FC4 type 0x%x",
2688 			    portid, ftype);
2689 		}
2690 		if (first_portid == portid) {
2691 			fcp->isp_loopstate = LOOP_FSCAN_DONE;
2692 			FC_SCRATCH_RELEASE(isp);
2693 			return (0);
2694 		}
2695 		if (portid == last_portid) {
2696 			if (last_port_same++ > 20) {
2697 				isp_prt(isp, ISP_LOGWARN,
2698 				    "tangled fabric database detected");
2699 				break;
2700 			}
2701 		} else {
2702 			last_port_same = 0 ;
2703 			last_portid = portid;
2704 		}
2705 	}
2706 	FC_SCRATCH_RELEASE(isp);
2707 	if (hicap >= GA_NXT_MAX) {
2708 		isp_prt(isp, ISP_LOGWARN, "fabric too big (> %d)", GA_NXT_MAX);
2709 	}
2710 	fcp->isp_loopstate = LOOP_FSCAN_DONE;
2711 	return (0);
2712 }
2713 #else
2714 #define	GIDLEN	((ISP2100_SCRLEN >> 1) + 16)
2715 #define	NGENT	((GIDLEN - 16) >> 2)
2716 
2717 #define	IGPOFF	(ISP2100_SCRLEN - GIDLEN)
2718 #define	GXOFF	(256)
2719 
2720 static int
2721 isp_scan_fabric(ispsoftc_t *isp, int ftype)
2722 {
2723 	fcparam *fcp = FCPARAM(isp);
2724 	mbreg_t mbs;
2725 	int i;
2726 	sns_gid_ft_req_t *rq;
2727 	sns_gid_ft_rsp_t *rs0, *rs1;
2728 
2729 	if (fcp->isp_onfabric == 0) {
2730 		fcp->isp_loopstate = LOOP_FSCAN_DONE;
2731 		return (0);
2732 	}
2733 
2734 	isp_prt(isp, ISP_LOGDEBUG0, "scanning fabric (GID_FT)");
2735 
2736 	FC_SCRATCH_ACQUIRE(isp);
2737 	fcp->isp_loopstate = LOOP_SCANNING_FABRIC;
2738 
2739 	rq = (sns_gid_ft_req_t *)fcp->tport;
2740 	MEMZERO((void *) rq, SNS_GID_FT_REQ_SIZE);
2741 	rq->snscb_rblen = GIDLEN >> 1;
2742 	rq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+IGPOFF);
2743 	rq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+IGPOFF);
2744 	rq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+IGPOFF);
2745 	rq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+IGPOFF);
2746 	rq->snscb_sblen = 6;
2747 	rq->snscb_cmd = SNS_GID_FT;
2748 	rq->snscb_mword_div_2 = NGENT;
2749 	rq->snscb_fc4_type = ftype;
2750 	isp_put_gid_ft_request(isp, rq, (sns_gid_ft_req_t *) fcp->isp_scratch);
2751 	MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GID_FT_REQ_SIZE);
2752 	MEMZERO(&mbs, sizeof (mbs));
2753 	mbs.param[0] = MBOX_SEND_SNS;
2754 	mbs.param[1] = SNS_GID_FT_REQ_SIZE >> 1;
2755 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2756 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2757 
2758 	/*
2759 	 * Leave 4 and 5 alone
2760 	 */
2761 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2762 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2763 	if (isp_fabric_mbox_cmd(isp, &mbs)) {
2764 		if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
2765 			fcp->isp_loopstate = LOOP_PDB_RCVD;
2766 		}
2767 		FC_SCRATCH_RELEASE(isp);
2768 		return (-1);
2769 	}
2770 	if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2771 		FC_SCRATCH_RELEASE(isp);
2772 		return (-1);
2773 	}
2774 	MEMORYBARRIER(isp, SYNC_SFORCPU, IGPOFF, GIDLEN);
2775 	rs1 = (sns_gid_ft_rsp_t *) fcp->tport;
2776 	rs0 = (sns_gid_ft_rsp_t *) ((uint8_t *)fcp->isp_scratch+IGPOFF);
2777 	isp_get_gid_ft_response(isp, rs0, rs1, NGENT);
2778 	if (rs1->snscb_cthdr.ct_response != FS_ACC) {
2779 		int level;
2780 		if (rs1->snscb_cthdr.ct_reason == 9 &&
2781 		    rs1->snscb_cthdr.ct_explanation == 7)
2782 			level = ISP_LOGDEBUG0;
2783 		else
2784 			level = ISP_LOGWARN;
2785 		isp_prt(isp, level, swrej, "GID_FT",
2786 		    rs1->snscb_cthdr.ct_reason,
2787 		    rs1->snscb_cthdr.ct_explanation, 0);
2788 		FC_SCRATCH_RELEASE(isp);
2789 		fcp->isp_loopstate = LOOP_FSCAN_DONE;
2790 		return (0);
2791 	}
2792 
2793 	/*
2794 	 * Okay, we now have a list of Port IDs for this class of device.
2795 	 * Go through the list and for each one get the WWPN/WWNN for it
2796 	 * and tell the outer layers about it. The outer layer needs to
2797 	 * know: Port ID, WWNN, WWPN, FC4 type, and (possibly) port type.
2798 	 *
2799 	 * The lportdb structure is adequate for this.
2800 	 */
2801 	i = -1;
2802 	do {
2803 		sns_gxn_id_req_t grqbuf, *gq = &grqbuf;
2804 		sns_gxn_id_rsp_t *gs0, grsbuf, *gs1 = &grsbuf;
2805 		struct lportdb lcl;
2806 #if	0
2807 		sns_gff_id_rsp_t *fs0, ffsbuf, *fs1 = &ffsbuf;
2808 #endif
2809 
2810 		i++;
2811 		MEMZERO(&lcl, sizeof (lcl));
2812 		lcl.fc4_type = ftype;
2813 		lcl.portid =
2814 		    (((uint32_t) rs1->snscb_ports[i].portid[0]) << 16) |
2815 		    (((uint32_t) rs1->snscb_ports[i].portid[1]) << 8) |
2816 		    (((uint32_t) rs1->snscb_ports[i].portid[2]));
2817 
2818 		MEMZERO((void *) gq, sizeof (sns_gxn_id_req_t));
2819 		gq->snscb_rblen = SNS_GXN_ID_RESP_SIZE >> 1;
2820 		gq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+GXOFF);
2821 		gq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+GXOFF);
2822 		gq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+GXOFF);
2823 		gq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+GXOFF);
2824 		gq->snscb_sblen = 6;
2825 		gq->snscb_cmd = SNS_GPN_ID;
2826 		gq->snscb_portid = lcl.portid;
2827 		isp_put_gxn_id_request(isp, gq,
2828 		    (sns_gxn_id_req_t *) fcp->isp_scratch);
2829 		MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GXN_ID_REQ_SIZE);
2830 		MEMZERO(&mbs, sizeof (mbs));
2831 		mbs.param[0] = MBOX_SEND_SNS;
2832 		mbs.param[1] = SNS_GXN_ID_REQ_SIZE >> 1;
2833 		mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2834 		mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2835 		/*
2836 		 * Leave 4 and 5 alone
2837 		 */
2838 		mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2839 		mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2840 		if (isp_fabric_mbox_cmd(isp, &mbs)) {
2841 			if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
2842 				fcp->isp_loopstate = LOOP_PDB_RCVD;
2843 			}
2844 			FC_SCRATCH_RELEASE(isp);
2845 			return (-1);
2846 		}
2847 		if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2848 			FC_SCRATCH_RELEASE(isp);
2849 			return (-1);
2850 		}
2851 		MEMORYBARRIER(isp, SYNC_SFORCPU, GXOFF, SNS_GXN_ID_RESP_SIZE);
2852 		gs0 = (sns_gxn_id_rsp_t *) ((uint8_t *)fcp->isp_scratch+GXOFF);
2853 		isp_get_gxn_id_response(isp, gs0, gs1);
2854 		if (gs1->snscb_cthdr.ct_response != FS_ACC) {
2855 			isp_prt(isp, ISP_LOGWARN, swrej, "GPN_ID",
2856 			    gs1->snscb_cthdr.ct_reason,
2857 			    gs1->snscb_cthdr.ct_explanation, lcl.portid);
2858 			if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2859 				FC_SCRATCH_RELEASE(isp);
2860 				return (-1);
2861 			}
2862 			continue;
2863 		}
2864 		lcl.port_wwn =
2865 		    (((uint64_t)gs1->snscb_wwn[0]) << 56) |
2866 		    (((uint64_t)gs1->snscb_wwn[1]) << 48) |
2867 		    (((uint64_t)gs1->snscb_wwn[2]) << 40) |
2868 		    (((uint64_t)gs1->snscb_wwn[3]) << 32) |
2869 		    (((uint64_t)gs1->snscb_wwn[4]) << 24) |
2870 		    (((uint64_t)gs1->snscb_wwn[5]) << 16) |
2871 		    (((uint64_t)gs1->snscb_wwn[6]) <<  8) |
2872 		    (((uint64_t)gs1->snscb_wwn[7]));
2873 
2874 		MEMZERO((void *) gq, sizeof (sns_gxn_id_req_t));
2875 		gq->snscb_rblen = SNS_GXN_ID_RESP_SIZE >> 1;
2876 		gq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+GXOFF);
2877 		gq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+GXOFF);
2878 		gq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+GXOFF);
2879 		gq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+GXOFF);
2880 		gq->snscb_sblen = 6;
2881 		gq->snscb_cmd = SNS_GNN_ID;
2882 		gq->snscb_portid = lcl.portid;
2883 		isp_put_gxn_id_request(isp, gq,
2884 		    (sns_gxn_id_req_t *) fcp->isp_scratch);
2885 		MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GXN_ID_REQ_SIZE);
2886 		MEMZERO(&mbs, sizeof (mbs));
2887 		mbs.param[0] = MBOX_SEND_SNS;
2888 		mbs.param[1] = SNS_GXN_ID_REQ_SIZE >> 1;
2889 		mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2890 		mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2891 		/*
2892 		 * Leave 4 and 5 alone
2893 		 */
2894 		mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2895 		mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2896 		if (isp_fabric_mbox_cmd(isp, &mbs)) {
2897 			if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
2898 				fcp->isp_loopstate = LOOP_PDB_RCVD;
2899 			}
2900 			FC_SCRATCH_RELEASE(isp);
2901 			return (-1);
2902 		}
2903 		if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2904 			FC_SCRATCH_RELEASE(isp);
2905 			return (-1);
2906 		}
2907 		MEMORYBARRIER(isp, SYNC_SFORCPU, GXOFF, SNS_GXN_ID_RESP_SIZE);
2908 		gs0 = (sns_gxn_id_rsp_t *) ((uint8_t *)fcp->isp_scratch+GXOFF);
2909 		isp_get_gxn_id_response(isp, gs0, gs1);
2910 		if (gs1->snscb_cthdr.ct_response != FS_ACC) {
2911 			isp_prt(isp, ISP_LOGWARN, swrej, "GNN_ID",
2912 			    gs1->snscb_cthdr.ct_reason,
2913 			    gs1->snscb_cthdr.ct_explanation, lcl.portid);
2914 			if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2915 				FC_SCRATCH_RELEASE(isp);
2916 				return (-1);
2917 			}
2918 			continue;
2919 		}
2920 		lcl.node_wwn =
2921 		    (((uint64_t)gs1->snscb_wwn[0]) << 56) |
2922 		    (((uint64_t)gs1->snscb_wwn[1]) << 48) |
2923 		    (((uint64_t)gs1->snscb_wwn[2]) << 40) |
2924 		    (((uint64_t)gs1->snscb_wwn[3]) << 32) |
2925 		    (((uint64_t)gs1->snscb_wwn[4]) << 24) |
2926 		    (((uint64_t)gs1->snscb_wwn[5]) << 16) |
2927 		    (((uint64_t)gs1->snscb_wwn[6]) <<  8) |
2928 		    (((uint64_t)gs1->snscb_wwn[7]));
2929 
2930 		/*
2931 		 * The QLogic f/w is bouncing this with a parameter error.
2932 		 */
2933 #if	0
2934 		/*
2935 		 * Try and get FC4 Features (FC-GS-3 only).
2936 		 * We can use the sns_gxn_id_req_t for this request.
2937 		 */
2938 		MEMZERO((void *) gq, sizeof (sns_gxn_id_req_t));
2939 		gq->snscb_rblen = SNS_GFF_ID_RESP_SIZE >> 1;
2940 		gq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+GXOFF);
2941 		gq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+GXOFF);
2942 		gq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+GXOFF);
2943 		gq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+GXOFF);
2944 		gq->snscb_sblen = 6;
2945 		gq->snscb_cmd = SNS_GFF_ID;
2946 		gq->snscb_portid = lcl.portid;
2947 		isp_put_gxn_id_request(isp, gq,
2948 		    (sns_gxn_id_req_t *) fcp->isp_scratch);
2949 		MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GXN_ID_REQ_SIZE);
2950 		MEMZERO(&mbs, sizeof (mbs));
2951 		mbs.param[0] = MBOX_SEND_SNS;
2952 		mbs.param[1] = SNS_GXN_ID_REQ_SIZE >> 1;
2953 		mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2954 		mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2955 		/*
2956 		 * Leave 4 and 5 alone
2957 		 */
2958 		mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2959 		mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2960 		if (isp_fabric_mbox_cmd(isp, &mbs)) {
2961 			if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
2962 				fcp->isp_loopstate = LOOP_PDB_RCVD;
2963 			}
2964 			FC_SCRATCH_RELEASE(isp);
2965 			return (-1);
2966 		}
2967 		if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2968 			FC_SCRATCH_RELEASE(isp);
2969 			return (-1);
2970 		}
2971 		MEMORYBARRIER(isp, SYNC_SFORCPU, GXOFF, SNS_GFF_ID_RESP_SIZE);
2972 		fs0 = (sns_gff_id_rsp_t *) ((uint8_t *)fcp->isp_scratch+GXOFF);
2973 		isp_get_gff_id_response(isp, fs0, fs1);
2974 		if (fs1->snscb_cthdr.ct_response != FS_ACC) {
2975 			isp_prt(isp, /* ISP_LOGDEBUG0 */ ISP_LOGWARN,
2976 			    swrej, "GFF_ID",
2977 			    fs1->snscb_cthdr.ct_reason,
2978 			    fs1->snscb_cthdr.ct_explanation, lcl.portid);
2979 			if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2980 				FC_SCRATCH_RELEASE(isp);
2981 				return (-1);
2982 			}
2983 		} else {
2984 			int index = (ftype >> 3);
2985 			int bshft = (ftype & 0x7) * 4;
2986 			int fc4_fval =
2987 			    (fs1->snscb_fc4_features[index] >> bshft) & 0xf;
2988 			if (fc4_fval & 0x1) {
2989 				lcl.roles |=
2990 				    (SVC3_INI_ROLE >> SVC3_ROLE_SHIFT);
2991 			}
2992 			if (fc4_fval & 0x2) {
2993 				lcl.roles |=
2994 				    (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT);
2995 			}
2996 		}
2997 #endif
2998 
2999 		/*
3000 		 * If we really want to know what kind of port type this is,
3001 		 * we have to run another CT command. Otherwise, we'll leave
3002 		 * it as undefined.
3003 		 *
3004 		lcl.port_type = 0;
3005 		 */
3006 		if (rs1->snscb_ports[i].control & 0x80) {
3007 			lcl.last_fabric_dev = 1;
3008 		} else {
3009 			lcl.last_fabric_dev = 0;
3010 		}
3011 		(void) isp_async(isp, ISPASYNC_FABRIC_DEV, &lcl);
3012 
3013 	} while ((rs1->snscb_ports[i].control & 0x80) == 0 && i < NGENT-1);
3014 
3015 	/*
3016 	 * If we're not at the last entry, our list isn't big enough.
3017 	 */
3018 	if ((rs1->snscb_ports[i].control & 0x80) == 0) {
3019 		isp_prt(isp, ISP_LOGWARN, "fabric too big for scratch area");
3020 	}
3021 
3022 	FC_SCRATCH_RELEASE(isp);
3023 	fcp->isp_loopstate = LOOP_FSCAN_DONE;
3024 	return (0);
3025 }
3026 #endif
3027 
3028 static void
3029 isp_register_fc4_type(ispsoftc_t *isp)
3030 {
3031 	fcparam *fcp = isp->isp_param;
3032 	uint8_t local[SNS_RFT_ID_REQ_SIZE];
3033 	sns_screq_t *reqp = (sns_screq_t *) local;
3034 	mbreg_t mbs;
3035 
3036 	MEMZERO((void *) reqp, SNS_RFT_ID_REQ_SIZE);
3037 	reqp->snscb_rblen = SNS_RFT_ID_RESP_SIZE >> 1;
3038 	reqp->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma + 0x100);
3039 	reqp->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma + 0x100);
3040 	reqp->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma + 0x100);
3041 	reqp->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma + 0x100);
3042 	reqp->snscb_sblen = 22;
3043 	reqp->snscb_data[0] = SNS_RFT_ID;
3044 	reqp->snscb_data[4] = fcp->isp_portid & 0xffff;
3045 	reqp->snscb_data[5] = (fcp->isp_portid >> 16) & 0xff;
3046 	reqp->snscb_data[6] = (1 << FC4_SCSI);
3047 #if	0
3048 	reqp->snscb_data[6] |= (1 << FC4_IP);	/* ISO/IEC 8802-2 LLC/SNAP */
3049 #endif
3050 	FC_SCRATCH_ACQUIRE(isp);
3051 	isp_put_sns_request(isp, reqp, (sns_screq_t *) fcp->isp_scratch);
3052 	MEMZERO(&mbs, sizeof (mbs));
3053 	mbs.param[0] = MBOX_SEND_SNS;
3054 	mbs.param[1] = SNS_RFT_ID_REQ_SIZE >> 1;
3055 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
3056 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
3057 	/*
3058 	 * Leave 4 and 5 alone
3059 	 */
3060 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
3061 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
3062 	MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_RFT_ID_REQ_SIZE);
3063 	isp_mboxcmd(isp, &mbs, MBLOGALL);
3064 	FC_SCRATCH_RELEASE(isp);
3065 	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
3066 		isp_prt(isp, ISP_LOGDEBUG0, "Register FC4 types succeeded");
3067 	}
3068 }
3069 
3070 /*
3071  * Start a command. Locking is assumed done in the caller.
3072  */
3073 
3074 int
3075 isp_start(XS_T *xs)
3076 {
3077 	ispsoftc_t *isp;
3078 	uint16_t nxti, optr, handle;
3079 	uint8_t local[QENTRY_LEN];
3080 	ispreq_t *reqp, *qep;
3081 	int target, i;
3082 
3083 	XS_INITERR(xs);
3084 	isp = XS_ISP(xs);
3085 
3086 	/*
3087 	 * Check to make sure we're supporting initiator role.
3088 	 */
3089 	if ((isp->isp_role & ISP_ROLE_INITIATOR) == 0) {
3090 		XS_SETERR(xs, HBA_SELTIMEOUT);
3091 		return (CMD_COMPLETE);
3092 	}
3093 
3094 	/*
3095 	 * Now make sure we're running.
3096 	 */
3097 
3098 	if (isp->isp_state != ISP_RUNSTATE) {
3099 		isp_prt(isp, ISP_LOGERR, "Adapter not at RUNSTATE");
3100 		XS_SETERR(xs, HBA_BOTCH);
3101 		return (CMD_COMPLETE);
3102 	}
3103 
3104 	/*
3105 	 * Check command CDB length, etc.. We really are limited to 16 bytes
3106 	 * for Fibre Channel, but can do up to 44 bytes in parallel SCSI,
3107 	 * but probably only if we're running fairly new firmware (we'll
3108 	 * let the old f/w choke on an extended command queue entry).
3109 	 */
3110 
3111 	if (XS_CDBLEN(xs) > (IS_FC(isp)? 16 : 44) || XS_CDBLEN(xs) == 0) {
3112 		isp_prt(isp, ISP_LOGERR,
3113 		    "unsupported cdb length (%d, CDB[0]=0x%x)",
3114 		    XS_CDBLEN(xs), XS_CDBP(xs)[0] & 0xff);
3115 		XS_SETERR(xs, HBA_BOTCH);
3116 		return (CMD_COMPLETE);
3117 	}
3118 
3119 	/*
3120 	 * Check to see whether we have good firmware state still or
3121 	 * need to refresh our port database for this target.
3122 	 */
3123 	target = XS_TGT(xs);
3124 	if (IS_FC(isp)) {
3125 		fcparam *fcp = isp->isp_param;
3126 		struct lportdb *lp;
3127 #ifdef	HANDLE_LOOPSTATE_IN_OUTER_LAYERS
3128 		if (fcp->isp_fwstate != FW_READY ||
3129 		    fcp->isp_loopstate != LOOP_READY) {
3130 			return (CMD_RQLATER);
3131 		}
3132 
3133 		/*
3134 		 * If we're not on a Fabric, we can't have a target
3135 		 * above FL_PORT_ID-1.
3136 		 *
3137 		 * If we're on a fabric and *not* connected as an F-port,
3138 		 * we can't have a target less than FC_SNS_ID+1. This
3139 		 * keeps us from having to sort out the difference between
3140 		 * local public loop devices and those which we might get
3141 		 * from a switch's database.
3142 		 */
3143 		if (fcp->isp_onfabric == 0) {
3144 			if (target >= FL_PORT_ID) {
3145 				XS_SETERR(xs, HBA_SELTIMEOUT);
3146 				return (CMD_COMPLETE);
3147 			}
3148 		} else {
3149 			if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
3150 				XS_SETERR(xs, HBA_SELTIMEOUT);
3151 				return (CMD_COMPLETE);
3152 			}
3153 			/*
3154 			 * We used to exclude having local loop ports
3155 			 * at the same time that we have fabric ports.
3156 			 * That is, we used to exclude having ports
3157 			 * at < FL_PORT_ID if we're FL-port.
3158 			 *
3159 			 * That's wrong. The only thing that could be
3160 			 * dicey is if the switch you're connected to
3161 			 * has these local loop ports appear on the
3162 			 * fabric and we somehow attach them twice.
3163 			 */
3164 		}
3165 #else
3166 		/*
3167 		 * Check for f/w being in ready state. If the f/w
3168 		 * isn't in ready state, then we don't know our
3169 		 * loop ID and the f/w hasn't completed logging
3170 		 * into all targets on the loop. If this is the
3171 		 * case, then bounce the command. We pretend this is
3172 		 * a SELECTION TIMEOUT error if we've never gone to
3173 		 * FW_READY state at all- in this case we may not
3174 		 * be hooked to a loop at all and we shouldn't hang
3175 		 * the machine for this. Otherwise, defer this command
3176 		 * until later.
3177 		 */
3178 		if (fcp->isp_fwstate != FW_READY) {
3179 			/*
3180 			 * Give ourselves at most a 250ms delay.
3181 			 */
3182 			if (isp_fclink_test(isp, 250000)) {
3183 				XS_SETERR(xs, HBA_SELTIMEOUT);
3184 				if (fcp->loop_seen_once) {
3185 					return (CMD_RQLATER);
3186 				} else {
3187 					return (CMD_COMPLETE);
3188 				}
3189 			}
3190 		}
3191 
3192 		/*
3193 		 * If we're not on a Fabric, we can't have a target
3194 		 * above FL_PORT_ID-1.
3195 		 *
3196 		 * If we're on a fabric and *not* connected as an F-port,
3197 		 * we can't have a target less than FC_SNS_ID+1. This
3198 		 * keeps us from having to sort out the difference between
3199 		 * local public loop devices and those which we might get
3200 		 * from a switch's database.
3201 		 */
3202 		if (fcp->isp_onfabric == 0) {
3203 			if (target >= FL_PORT_ID) {
3204 				XS_SETERR(xs, HBA_SELTIMEOUT);
3205 				return (CMD_COMPLETE);
3206 			}
3207 		} else {
3208 			if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
3209 				XS_SETERR(xs, HBA_SELTIMEOUT);
3210 				return (CMD_COMPLETE);
3211 			}
3212 			if (fcp->isp_topo != TOPO_F_PORT &&
3213 			    target < FL_PORT_ID) {
3214 				XS_SETERR(xs, HBA_SELTIMEOUT);
3215 				return (CMD_COMPLETE);
3216 			}
3217 		}
3218 
3219 		/*
3220 		 * If our loop state is such that we haven't yet received
3221 		 * a "Port Database Changed" notification (after a LIP or
3222 		 * a Loop Reset or firmware initialization), then defer
3223 		 * sending commands for a little while, but only if we've
3224 		 * seen a valid loop at one point (otherwise we can get
3225 		 * stuck at initialization time).
3226 		 */
3227 		if (fcp->isp_loopstate < LOOP_PDB_RCVD) {
3228 			XS_SETERR(xs, HBA_SELTIMEOUT);
3229 			if (fcp->loop_seen_once) {
3230 				return (CMD_RQLATER);
3231 			} else {
3232 				return (CMD_COMPLETE);
3233 			}
3234 		}
3235 
3236 		/*
3237 		 * If we're in the middle of loop or fabric scanning
3238 		 * or merging the port databases, retry this command later.
3239 		 */
3240 		if (fcp->isp_loopstate == LOOP_SCANNING_FABRIC ||
3241 		    fcp->isp_loopstate == LOOP_SCANNING_LOOP ||
3242 		    fcp->isp_loopstate == LOOP_SYNCING_PDB) {
3243 			return (CMD_RQLATER);
3244 		}
3245 
3246 		/*
3247 		 * If our loop state is now such that we've just now
3248 		 * received a Port Database Change notification, then
3249 		 * we have to go off and (re)scan the fabric. We back
3250 		 * out and try again later if this doesn't work.
3251 		 */
3252 		if (fcp->isp_loopstate == LOOP_PDB_RCVD && fcp->isp_onfabric) {
3253 			if (isp_scan_fabric(isp, FC4_SCSI)) {
3254 				return (CMD_RQLATER);
3255 			}
3256 			if (fcp->isp_fwstate != FW_READY ||
3257 			    fcp->isp_loopstate < LOOP_FSCAN_DONE) {
3258 				return (CMD_RQLATER);
3259 			}
3260 		}
3261 
3262 		/*
3263 		 * If our loop state is now such that we've just now
3264 		 * received a Port Database Change notification, then
3265 		 * we have to go off and (re)synchronize our port
3266 		 * database.
3267 		 */
3268 		if (fcp->isp_loopstate < LOOP_READY) {
3269 			if (isp_pdb_sync(isp)) {
3270 				return (CMD_RQLATER);
3271 			}
3272 			if (fcp->isp_fwstate != FW_READY ||
3273 			    fcp->isp_loopstate != LOOP_READY) {
3274 				return (CMD_RQLATER);
3275 			}
3276 		}
3277 
3278 		/*
3279 		 * XXX: Here's were we would cancel any loop_dead flag
3280 		 * XXX: also cancel in dead_loop timeout that's running
3281 		 */
3282 #endif
3283 
3284 		/*
3285 		 * Now check whether we should even think about pursuing this.
3286 		 */
3287 		lp = &fcp->portdb[target];
3288 		if (lp->valid == 0) {
3289 			XS_SETERR(xs, HBA_SELTIMEOUT);
3290 			return (CMD_COMPLETE);
3291 		}
3292 		if ((lp->roles & (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT)) == 0) {
3293 			isp_prt(isp, ISP_LOGDEBUG2,
3294 			    "Target %d does not have target service", target);
3295 			XS_SETERR(xs, HBA_SELTIMEOUT);
3296 			return (CMD_COMPLETE);
3297 		}
3298 		/*
3299 		 * Now turn target into what the actual Loop ID is.
3300 		 */
3301 		target = lp->loopid;
3302 	}
3303 
3304 	/*
3305 	 * Next check to see if any HBA or Device
3306 	 * parameters need to be updated.
3307 	 */
3308 	if (isp->isp_update != 0) {
3309 		isp_update(isp);
3310 	}
3311 
3312 	if (isp_getrqentry(isp, &nxti, &optr, (void *)&qep)) {
3313 		isp_prt(isp, ISP_LOGDEBUG0, "Request Queue Overflow");
3314 		XS_SETERR(xs, HBA_BOTCH);
3315 		return (CMD_EAGAIN);
3316 	}
3317 
3318 	/*
3319 	 * Now see if we need to synchronize the ISP with respect to anything.
3320 	 * We do dual duty here (cough) for synchronizing for busses other
3321 	 * than which we got here to send a command to.
3322 	 */
3323 	reqp = (ispreq_t *) local;
3324 	if (isp->isp_sendmarker) {
3325 		uint8_t n = (IS_DUALBUS(isp)? 2: 1);
3326 		/*
3327 		 * Check ports to send markers for...
3328 		 */
3329 		for (i = 0; i < n; i++) {
3330 			if ((isp->isp_sendmarker & (1 << i)) == 0) {
3331 				continue;
3332 			}
3333 			MEMZERO((void *) reqp, QENTRY_LEN);
3334 			reqp->req_header.rqs_entry_count = 1;
3335 			reqp->req_header.rqs_entry_type = RQSTYPE_MARKER;
3336 			reqp->req_modifier = SYNC_ALL;
3337 			reqp->req_target = i << 7;	/* insert bus number */
3338 			isp_put_request(isp, reqp, qep);
3339 			ISP_ADD_REQUEST(isp, nxti);
3340 			isp->isp_sendmarker &= ~(1 << i);
3341 			if (isp_getrqentry(isp, &nxti, &optr, (void *) &qep)) {
3342 				isp_prt(isp, ISP_LOGDEBUG0,
3343 				    "Request Queue Overflow+");
3344 				XS_SETERR(xs, HBA_BOTCH);
3345 				return (CMD_EAGAIN);
3346 			}
3347 		}
3348 	}
3349 
3350 	MEMZERO((void *)reqp, QENTRY_LEN);
3351 	reqp->req_header.rqs_entry_count = 1;
3352 	if (IS_FC(isp)) {
3353 		reqp->req_header.rqs_entry_type = RQSTYPE_T2RQS;
3354 	} else {
3355 		if (XS_CDBLEN(xs) > 12)
3356 			reqp->req_header.rqs_entry_type = RQSTYPE_CMDONLY;
3357 		else
3358 			reqp->req_header.rqs_entry_type = RQSTYPE_REQUEST;
3359 	}
3360 	/* reqp->req_header.rqs_flags = 0; */
3361 	/* reqp->req_header.rqs_seqno = 0; */
3362 	if (IS_FC(isp)) {
3363 		/*
3364 		 * See comment in isp_intr
3365 		 */
3366 		/* XS_RESID(xs) = 0; */
3367 
3368 		/*
3369 		 * Fibre Channel always requires some kind of tag.
3370 		 * The Qlogic drivers seem be happy not to use a tag,
3371 		 * but this breaks for some devices (IBM drives).
3372 		 */
3373 		if (XS_TAG_P(xs)) {
3374 			((ispreqt2_t *)reqp)->req_flags = XS_TAG_TYPE(xs);
3375 		} else {
3376 			/*
3377 			 * If we don't know what tag to use, use HEAD OF QUEUE
3378 			 * for Request Sense or Simple.
3379 			 */
3380 			if (XS_CDBP(xs)[0] == 0x3)	/* REQUEST SENSE */
3381 				((ispreqt2_t *)reqp)->req_flags = REQFLAG_HTAG;
3382 			else
3383 				((ispreqt2_t *)reqp)->req_flags = REQFLAG_STAG;
3384 		}
3385 	} else {
3386 		sdparam *sdp = (sdparam *)isp->isp_param;
3387 		sdp += XS_CHANNEL(xs);
3388 		if ((sdp->isp_devparam[target].actv_flags & DPARM_TQING) &&
3389 		    XS_TAG_P(xs)) {
3390 			reqp->req_flags = XS_TAG_TYPE(xs);
3391 		}
3392 	}
3393 	if (IS_SCSI(isp)) {
3394 		reqp->req_target = target | (XS_CHANNEL(xs) << 7);
3395 		reqp->req_lun_trn = XS_LUN(xs);
3396 		reqp->req_cdblen = XS_CDBLEN(xs);
3397 	} else if (IS_2KLOGIN(isp)) {
3398 		((ispreqt2e_t *)reqp)->req_target = target;
3399 		((ispreqt2e_t *)reqp)->req_scclun = XS_LUN(xs);
3400 	} else if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) {
3401 		((ispreqt2_t *)reqp)->req_target = target;
3402 		((ispreqt2_t *)reqp)->req_scclun = XS_LUN(xs);
3403 	} else {
3404 		((ispreqt2_t *)reqp)->req_target = target;
3405 		((ispreqt2_t *)reqp)->req_lun_trn = XS_LUN(xs);
3406 	}
3407 	MEMCPY(reqp->req_cdb, XS_CDBP(xs), XS_CDBLEN(xs));
3408 
3409 	reqp->req_time = XS_TIME(xs) / 1000;
3410 	if (reqp->req_time == 0 && XS_TIME(xs)) {
3411 		reqp->req_time = 1;
3412 	}
3413 
3414 	if (isp_save_xs(isp, xs, &handle)) {
3415 		isp_prt(isp, ISP_LOGDEBUG0, "out of xflist pointers");
3416 		XS_SETERR(xs, HBA_BOTCH);
3417 		return (CMD_EAGAIN);
3418 	}
3419 	reqp->req_handle = handle;
3420 
3421 	/*
3422 	 * Set up DMA and/or do any bus swizzling of the request entry
3423 	 * so that the Qlogic F/W understands what is being asked of it.
3424 	 */
3425 	i = ISP_DMASETUP(isp, xs, reqp, &nxti, optr);
3426 	if (i != CMD_QUEUED) {
3427 		isp_destroy_handle(isp, handle);
3428 		/*
3429 		 * dmasetup sets actual error in packet, and
3430 		 * return what we were given to return.
3431 		 */
3432 		return (i);
3433 	}
3434 	XS_SETERR(xs, HBA_NOERROR);
3435 	isp_prt(isp, ISP_LOGDEBUG2,
3436 	    "START cmd for %d.%d.%d cmd 0x%x datalen %ld",
3437 	    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), XS_CDBP(xs)[0],
3438 	    (long) XS_XFRLEN(xs));
3439 	ISP_ADD_REQUEST(isp, nxti);
3440 	isp->isp_nactive++;
3441 	return (CMD_QUEUED);
3442 }
3443 
3444 /*
3445  * isp control
3446  * Locks (ints blocked) assumed held.
3447  */
3448 
3449 int
3450 isp_control(ispsoftc_t *isp, ispctl_t ctl, void *arg)
3451 {
3452 	XS_T *xs;
3453 	mbreg_t mbs;
3454 	int bus, tgt;
3455 	uint16_t handle;
3456 
3457 	MEMZERO(&mbs, sizeof (mbs));
3458 
3459 	switch (ctl) {
3460 	default:
3461 		isp_prt(isp, ISP_LOGERR, "Unknown Control Opcode 0x%x", ctl);
3462 		break;
3463 
3464 	case ISPCTL_RESET_BUS:
3465 		/*
3466 		 * Issue a bus reset.
3467 		 */
3468 		mbs.param[0] = MBOX_BUS_RESET;
3469 		mbs.param[2] = 0;
3470 		if (IS_SCSI(isp)) {
3471 			mbs.param[1] =
3472 			    ((sdparam *) isp->isp_param)->isp_bus_reset_delay;
3473 			if (mbs.param[1] < 2) {
3474 				mbs.param[1] = 2;
3475 			}
3476 			bus = *((int *) arg);
3477 			if (IS_DUALBUS(isp)) {
3478 				mbs.param[2] = bus;
3479 			}
3480 		} else {
3481 			mbs.param[1] = 10;
3482 			bus = 0;
3483 		}
3484 		isp->isp_sendmarker |= (1 << bus);
3485 		isp_mboxcmd(isp, &mbs, MBLOGALL);
3486 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3487 			break;
3488 		}
3489 		isp_prt(isp, ISP_LOGINFO,
3490 		    "driver initiated bus reset of bus %d", bus);
3491 		return (0);
3492 
3493 	case ISPCTL_RESET_DEV:
3494 		tgt = (*((int *) arg)) & 0xffff;
3495 		bus = (*((int *) arg)) >> 16;
3496 		mbs.param[0] = MBOX_ABORT_TARGET;
3497 		if (IS_SCSI(isp)) {
3498 			mbs.param[1] = (tgt << 8) | (bus << 15);
3499 		} else {
3500 			if (IS_2KLOGIN(isp)) {
3501 				mbs.param[1] = tgt;
3502 				mbs.obits |= (1 << 10);
3503 			} else {
3504 				mbs.param[1] = (tgt << 8);
3505 			}
3506 		}
3507 		mbs.param[2] = 3;	/* 'delay', in seconds */
3508 		isp_mboxcmd(isp, &mbs, MBLOGALL);
3509 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3510 			break;
3511 		}
3512 		isp_prt(isp, ISP_LOGINFO,
3513 		    "Target %d on Bus %d Reset Succeeded", tgt, bus);
3514 		isp->isp_sendmarker |= (1 << bus);
3515 		return (0);
3516 
3517 	case ISPCTL_ABORT_CMD:
3518 		xs = (XS_T *) arg;
3519 		tgt = XS_TGT(xs);
3520 		handle = isp_find_handle(isp, xs);
3521 		if (handle == 0) {
3522 			isp_prt(isp, ISP_LOGWARN,
3523 			    "cannot find handle for command to abort");
3524 			break;
3525 		}
3526 		bus = XS_CHANNEL(xs);
3527 		mbs.param[0] = MBOX_ABORT;
3528 		if (IS_FC(isp)) {
3529 			if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN)  {
3530 				if (IS_2KLOGIN(isp)) {
3531 					mbs.param[1] = tgt;
3532 				} else {
3533 					mbs.param[1] = tgt << 8;
3534 				}
3535 				mbs.param[4] = 0;
3536 				mbs.param[5] = 0;
3537 				mbs.param[6] = XS_LUN(xs);
3538 			} else {
3539 				mbs.param[1] = tgt << 8 | XS_LUN(xs);
3540 			}
3541 		} else {
3542 			mbs.param[1] =
3543 			    (bus << 15) | (XS_TGT(xs) << 8) | XS_LUN(xs);
3544 		}
3545 		mbs.param[3] = 0;
3546 		mbs.param[2] = handle;
3547 		isp_mboxcmd(isp, &mbs, MBLOGALL & ~MBOX_COMMAND_ERROR);
3548 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
3549 			return (0);
3550 		}
3551 		/*
3552 		 * XXX: Look for command in the REQUEST QUEUE. That is,
3553 		 * XXX: It hasen't been picked up by firmware yet.
3554 		 */
3555 		break;
3556 
3557 	case ISPCTL_UPDATE_PARAMS:
3558 
3559 		isp_update(isp);
3560 		return (0);
3561 
3562 	case ISPCTL_FCLINK_TEST:
3563 
3564 		if (IS_FC(isp)) {
3565 			int usdelay = *((int *) arg);
3566 			if (usdelay == 0) {
3567 				usdelay =  250000;
3568 			}
3569 			return (isp_fclink_test(isp, usdelay));
3570 		}
3571 		break;
3572 
3573 	case ISPCTL_SCAN_FABRIC:
3574 
3575 		if (IS_FC(isp)) {
3576 			int ftype = (arg)? *((int *) arg) : FC4_SCSI;
3577 			return (isp_scan_fabric(isp, ftype));
3578 		}
3579 		break;
3580 
3581 	case ISPCTL_SCAN_LOOP:
3582 
3583 		if (IS_FC(isp)) {
3584 			return (isp_scan_loop(isp));
3585 		}
3586 		break;
3587 
3588 	case ISPCTL_PDB_SYNC:
3589 
3590 		if (IS_FC(isp)) {
3591 			return (isp_pdb_sync(isp));
3592 		}
3593 		break;
3594 
3595 	case ISPCTL_SEND_LIP:
3596 
3597 		if (IS_FC(isp)) {
3598 			mbs.param[0] = MBOX_INIT_LIP;
3599 			if (IS_2KLOGIN(isp)) {
3600 				mbs.obits |= (1 << 10);
3601 			}
3602 			isp_mboxcmd(isp, &mbs, MBLOGALL);
3603 			if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
3604 				return (0);
3605 			}
3606 		}
3607 		break;
3608 
3609 	case ISPCTL_GET_POSMAP:
3610 
3611 		if (IS_FC(isp) && arg) {
3612 			return (isp_getmap(isp, arg));
3613 		}
3614 		break;
3615 
3616 
3617 	case ISPCTL_GET_PDB:
3618 		if (IS_FC(isp) && arg) {
3619 			int id = *((int *)arg);
3620 			isp_pdb_t *pdb = arg;
3621 			return (isp_getpdb(isp, id, pdb));
3622 		}
3623 		break;
3624 
3625 	case ISPCTL_RUN_MBOXCMD:
3626 
3627 		isp_mboxcmd(isp, arg, MBLOGALL);
3628 		return(0);
3629 
3630 #ifdef	ISP_TARGET_MODE
3631 	case ISPCTL_TOGGLE_TMODE:
3632 	{
3633 
3634 		/*
3635 		 * We don't check/set against role here- that's the
3636 		 * responsibility for the outer layer to coordinate.
3637 		 */
3638 		if (IS_SCSI(isp)) {
3639 			int param = *(int *)arg;
3640 			mbs.param[0] = MBOX_ENABLE_TARGET_MODE;
3641 			mbs.param[1] = param & 0xffff;
3642 			mbs.param[2] = param >> 16;
3643 			isp_mboxcmd(isp, &mbs, MBLOGALL);
3644 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3645 				break;
3646 			}
3647 		}
3648 		return (0);
3649 	}
3650 #endif
3651 	}
3652 	return (-1);
3653 }
3654 
3655 /*
3656  * Interrupt Service Routine(s).
3657  *
3658  * External (OS) framework has done the appropriate locking,
3659  * and the locking will be held throughout this function.
3660  */
3661 
3662 /*
3663  * Limit our stack depth by sticking with the max likely number
3664  * of completions on a request queue at any one time.
3665  */
3666 #ifndef	MAX_REQUESTQ_COMPLETIONS
3667 #define	MAX_REQUESTQ_COMPLETIONS	64
3668 #endif
3669 
3670 void
3671 isp_intr(ispsoftc_t *isp, uint16_t isr, uint16_t sema, uint16_t mbox)
3672 {
3673 	XS_T *complist[MAX_REQUESTQ_COMPLETIONS], *xs;
3674 	uint16_t iptr, optr, junk;
3675 	int i, nlooked = 0, ndone = 0;
3676 
3677 again:
3678 	/*
3679 	 * Is this a mailbox related interrupt?
3680 	 * The mailbox semaphore will be nonzero if so.
3681 	 */
3682 	if (sema) {
3683 		if (mbox & 0x4000) {
3684 			isp->isp_intmboxc++;
3685 			if (isp->isp_mboxbsy) {
3686 				int i = 0, obits = isp->isp_obits;
3687 				isp->isp_mboxtmp[i++] = mbox;
3688 				for (i = 1; i < MAX_MAILBOX(isp); i++) {
3689 					if ((obits & (1 << i)) == 0) {
3690 						continue;
3691 					}
3692 					isp->isp_mboxtmp[i] =
3693 					    ISP_READ(isp, MBOX_OFF(i));
3694 				}
3695 				if (isp->isp_mbxwrk0) {
3696 					if (isp_mbox_continue(isp) == 0) {
3697 						return;
3698 					}
3699 				}
3700 				MBOX_NOTIFY_COMPLETE(isp);
3701 			} else {
3702 				isp_prt(isp, ISP_LOGWARN,
3703 				    "Mbox Command Async (0x%x) with no waiters",
3704 				    mbox);
3705 			}
3706 		} else if (isp_parse_async(isp, mbox) < 0) {
3707 			return;
3708 		}
3709 		if ((IS_FC(isp) && mbox != ASYNC_RIO_RESP) ||
3710 		    isp->isp_state != ISP_RUNSTATE) {
3711 			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3712 			ISP_WRITE(isp, BIU_SEMA, 0);
3713 			return;
3714 		}
3715 	}
3716 
3717 	/*
3718 	 * We can't be getting this now.
3719 	 */
3720 	if (isp->isp_state != ISP_RUNSTATE) {
3721 		isp_prt(isp, ISP_LOGWARN,
3722 		    "interrupt (ISR=%x SEMA=%x) when not ready", isr, sema);
3723 		/*
3724 		 * Thank you very much!  *Burrrp*!
3725 		 */
3726 		WRITE_RESPONSE_QUEUE_OUT_POINTER(isp,
3727 		    READ_RESPONSE_QUEUE_IN_POINTER(isp));
3728 
3729 		ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3730 		ISP_WRITE(isp, BIU_SEMA, 0);
3731 		return;
3732 	}
3733 
3734 	/*
3735 	 * Get the current Response Queue Out Pointer.
3736 	 *
3737 	 * If we're a 2300, we can ask what hardware what it thinks.
3738 	 */
3739 	if (IS_23XX(isp)) {
3740 		optr = ISP_READ(isp, isp->isp_respoutrp);
3741 		/*
3742 		 * Debug: to be taken out eventually
3743 		 */
3744 		if (isp->isp_residx != optr) {
3745 			isp_prt(isp, ISP_LOGWARN, "optr %x soft optr %x",
3746 			    optr, isp->isp_residx);
3747 		}
3748 	} else {
3749 		optr = isp->isp_residx;
3750 	}
3751 
3752 	/*
3753 	 * You *must* read the Response Queue In Pointer
3754 	 * prior to clearing the RISC interrupt.
3755 	 *
3756 	 * Debounce the 2300 if revision less than 2.
3757 	 */
3758 	if (IS_2100(isp) || (IS_2300(isp) && isp->isp_revision < 2)) {
3759 		i = 0;
3760 		do {
3761 			iptr = READ_RESPONSE_QUEUE_IN_POINTER(isp);
3762 			junk = READ_RESPONSE_QUEUE_IN_POINTER(isp);
3763 		} while (junk != iptr && ++i < 1000);
3764 
3765 		if (iptr != junk) {
3766 			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3767 			isp_prt(isp, ISP_LOGWARN,
3768 			    "Response Queue Out Pointer Unstable (%x, %x)",
3769 			    iptr, junk);
3770 			return;
3771 		}
3772 	} else {
3773 		iptr = READ_RESPONSE_QUEUE_IN_POINTER(isp);
3774 	}
3775 	isp->isp_resodx = iptr;
3776 
3777 
3778 	if (optr == iptr && sema == 0) {
3779 		/*
3780 		 * There are a lot of these- reasons unknown- mostly on
3781 		 * faster Alpha machines.
3782 		 *
3783 		 * I tried delaying after writing HCCR_CMD_CLEAR_RISC_INT to
3784 		 * make sure the old interrupt went away (to avoid 'ringing'
3785 		 * effects), but that didn't stop this from occurring.
3786 		 */
3787 		if (IS_23XX(isp)) {
3788 			USEC_DELAY(100);
3789 			iptr = READ_RESPONSE_QUEUE_IN_POINTER(isp);
3790 			junk = ISP_READ(isp, BIU_R2HSTSLO);
3791 		} else {
3792 			junk = ISP_READ(isp, BIU_ISR);
3793 		}
3794 		if (optr == iptr) {
3795 			if (IS_23XX(isp)) {
3796 				;
3797 			} else {
3798 				sema = ISP_READ(isp, BIU_SEMA);
3799 				mbox = ISP_READ(isp, OUTMAILBOX0);
3800 				if ((sema & 0x3) && (mbox & 0x8000)) {
3801 					goto again;
3802 				}
3803 			}
3804 			isp->isp_intbogus++;
3805 			isp_prt(isp, ISP_LOGDEBUG1,
3806 			    "bogus intr- isr %x (%x) iptr %x optr %x",
3807 			    isr, junk, iptr, optr);
3808 		}
3809 	}
3810 	isp->isp_resodx = iptr;
3811 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3812 	ISP_WRITE(isp, BIU_SEMA, 0);
3813 
3814 	if (isp->isp_rspbsy) {
3815 		return;
3816 	}
3817 	isp->isp_rspbsy = 1;
3818 
3819 	while (optr != iptr) {
3820 		ispstatusreq_t local, *sp = &local;
3821 		isphdr_t *hp;
3822 		int type;
3823 		uint16_t oop;
3824 		int buddaboom = 0;
3825 
3826 		hp = (isphdr_t *) ISP_QUEUE_ENTRY(isp->isp_result, optr);
3827 		oop = optr;
3828 		optr = ISP_NXT_QENTRY(optr, RESULT_QUEUE_LEN(isp));
3829 		nlooked++;
3830  read_again:
3831 		/*
3832 		 * Synchronize our view of this response queue entry.
3833 		 */
3834 		MEMORYBARRIER(isp, SYNC_RESULT, oop, QENTRY_LEN);
3835 
3836 		type = isp_get_response_type(isp, hp);
3837 
3838 		if (type == RQSTYPE_RESPONSE) {
3839 			isp_get_response(isp, (ispstatusreq_t *) hp, sp);
3840 		} else if (type == RQSTYPE_RIO2) {
3841 			isp_rio2_t rio;
3842 			isp_get_rio2(isp, (isp_rio2_t *) hp, &rio);
3843 			for (i = 0; i < rio.req_header.rqs_seqno; i++) {
3844 				isp_fastpost_complete(isp, rio.req_handles[i]);
3845 			}
3846 			if (isp->isp_fpcchiwater < rio.req_header.rqs_seqno)
3847 				isp->isp_fpcchiwater = rio.req_header.rqs_seqno;
3848 			MEMZERO(hp, QENTRY_LEN);	/* PERF */
3849 			continue;
3850 		} else {
3851 			/*
3852 			 * Somebody reachable via isp_handle_other_response
3853 			 * may have updated the response queue pointers for
3854 			 * us, so we reload our goal index.
3855 			 */
3856 			int i = isp_handle_other_response(isp, type, hp, &optr);
3857 			if (i < 0) {
3858 				goto read_again;
3859 			} else if (i > 0) {
3860 				iptr = isp->isp_resodx;
3861 				MEMZERO(hp, QENTRY_LEN);	/* PERF */
3862 				continue;
3863 			}
3864 
3865 			/*
3866 			 * After this point, we'll just look at the header as
3867 			 * we don't know how to deal with the rest of the
3868 			 * response.
3869 			 */
3870 			isp_get_response(isp, (ispstatusreq_t *) hp, sp);
3871 
3872 			/*
3873 			 * It really has to be a bounced request just copied
3874 			 * from the request queue to the response queue. If
3875 			 * not, something bad has happened.
3876 			 */
3877 			if (sp->req_header.rqs_entry_type != RQSTYPE_REQUEST) {
3878 				isp_prt(isp, ISP_LOGERR, notresp,
3879 				    sp->req_header.rqs_entry_type, oop, optr,
3880 				    nlooked);
3881 				if (isp->isp_dblev & ISP_LOGDEBUG0) {
3882 					isp_print_bytes(isp, "Queue Entry",
3883 					    QENTRY_LEN, sp);
3884 				}
3885 				MEMZERO(hp, QENTRY_LEN);	/* PERF */
3886 				continue;
3887 			}
3888 			buddaboom = 1;
3889 		}
3890 
3891 		if (sp->req_header.rqs_flags & 0xf) {
3892 #define	_RQS_OFLAGS	\
3893 	~(RQSFLAG_CONTINUATION|RQSFLAG_FULL|RQSFLAG_BADHEADER|RQSFLAG_BADPACKET)
3894 			if (sp->req_header.rqs_flags & RQSFLAG_CONTINUATION) {
3895 				isp_prt(isp, ISP_LOGWARN,
3896 				    "continuation segment");
3897 				WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
3898 				continue;
3899 			}
3900 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
3901 				isp_prt(isp, ISP_LOGDEBUG1,
3902 				    "internal queues full");
3903 				/*
3904 				 * We'll synthesize a QUEUE FULL message below.
3905 				 */
3906 			}
3907 			if (sp->req_header.rqs_flags & RQSFLAG_BADHEADER) {
3908 				isp_prt(isp, ISP_LOGERR,  "bad header flag");
3909 				buddaboom++;
3910 			}
3911 			if (sp->req_header.rqs_flags & RQSFLAG_BADPACKET) {
3912 				isp_prt(isp, ISP_LOGERR, "bad request packet");
3913 				buddaboom++;
3914 			}
3915 			if (sp->req_header.rqs_flags & _RQS_OFLAGS) {
3916 				isp_prt(isp, ISP_LOGERR,
3917 				    "unknown flags (0x%x) in response",
3918 				    sp->req_header.rqs_flags);
3919 				buddaboom++;
3920 			}
3921 #undef	_RQS_OFLAGS
3922 		}
3923 		if (sp->req_handle > isp->isp_maxcmds || sp->req_handle < 1) {
3924 			MEMZERO(hp, QENTRY_LEN);	/* PERF */
3925 			isp_prt(isp, ISP_LOGERR,
3926 			    "bad request handle %d (type 0x%x, flags 0x%x)",
3927 			    sp->req_handle, sp->req_header.rqs_entry_type,
3928 			    sp->req_header.rqs_flags);
3929 			WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
3930 			continue;
3931 		}
3932 		xs = isp_find_xs(isp, sp->req_handle);
3933 		if (xs == NULL) {
3934 			uint8_t ts = sp->req_completion_status & 0xff;
3935 			MEMZERO(hp, QENTRY_LEN);	/* PERF */
3936 			/*
3937 			 * Only whine if this isn't the expected fallout of
3938 			 * aborting the command.
3939 			 */
3940 			if (sp->req_header.rqs_entry_type != RQSTYPE_RESPONSE) {
3941 				isp_prt(isp, ISP_LOGERR,
3942 				    "cannot find handle 0x%x (type 0x%x)",
3943 				    sp->req_handle,
3944 				    sp->req_header.rqs_entry_type);
3945 			} else if (ts != RQCS_ABORTED) {
3946 				isp_prt(isp, ISP_LOGERR,
3947 				    "cannot find handle 0x%x (status 0x%x)",
3948 				    sp->req_handle, ts);
3949 			}
3950 			WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
3951 			continue;
3952 		}
3953 		isp_destroy_handle(isp, sp->req_handle);
3954 		if (sp->req_status_flags & RQSTF_BUS_RESET) {
3955 			XS_SETERR(xs, HBA_BUSRESET);
3956 			isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
3957 		}
3958 		if (buddaboom) {
3959 			XS_SETERR(xs, HBA_BOTCH);
3960 		}
3961 
3962 		if (IS_FC(isp) && (sp->req_scsi_status & RQCS_SV)) {
3963 			/*
3964 			 * Fibre Channel F/W doesn't say we got status
3965 			 * if there's Sense Data instead. I guess they
3966 			 * think it goes w/o saying.
3967 			 */
3968 			sp->req_state_flags |= RQSF_GOT_STATUS;
3969 		}
3970 		if (sp->req_state_flags & RQSF_GOT_STATUS) {
3971 			*XS_STSP(xs) = sp->req_scsi_status & 0xff;
3972 		}
3973 
3974 		switch (sp->req_header.rqs_entry_type) {
3975 		case RQSTYPE_RESPONSE:
3976 			XS_SET_STATE_STAT(isp, xs, sp);
3977 			isp_parse_status(isp, sp, xs);
3978 			if ((XS_NOERR(xs) || XS_ERR(xs) == HBA_NOERROR) &&
3979 			    (*XS_STSP(xs) == SCSI_BUSY)) {
3980 				XS_SETERR(xs, HBA_TGTBSY);
3981 			}
3982 			if (IS_SCSI(isp)) {
3983 				XS_RESID(xs) = sp->req_resid;
3984 				if ((sp->req_state_flags & RQSF_GOT_STATUS) &&
3985 				    (*XS_STSP(xs) == SCSI_CHECK) &&
3986 				    (sp->req_state_flags & RQSF_GOT_SENSE)) {
3987 					XS_SAVE_SENSE(xs, sp);
3988 				}
3989 				/*
3990 				 * A new synchronous rate was negotiated for
3991 				 * this target. Mark state such that we'll go
3992 				 * look up that which has changed later.
3993 				 */
3994 				if (sp->req_status_flags & RQSTF_NEGOTIATION) {
3995 					int t = XS_TGT(xs);
3996 					sdparam *sdp = isp->isp_param;
3997 					sdp += XS_CHANNEL(xs);
3998 					sdp->isp_devparam[t].dev_refresh = 1;
3999 					isp->isp_update |=
4000 					    (1 << XS_CHANNEL(xs));
4001 				}
4002 			} else {
4003 				if (sp->req_status_flags & RQSF_XFER_COMPLETE) {
4004 					XS_RESID(xs) = 0;
4005 				} else if (sp->req_scsi_status & RQCS_RESID) {
4006 					XS_RESID(xs) = sp->req_resid;
4007 				} else {
4008 					XS_RESID(xs) = 0;
4009 				}
4010 				if ((sp->req_state_flags & RQSF_GOT_STATUS) &&
4011 				    (*XS_STSP(xs) == SCSI_CHECK) &&
4012 				    (sp->req_scsi_status & RQCS_SV)) {
4013 					XS_SAVE_SENSE(xs, sp);
4014 					/* solely for the benefit of debug */
4015 					sp->req_state_flags |= RQSF_GOT_SENSE;
4016 				}
4017 			}
4018 			isp_prt(isp, ISP_LOGDEBUG2,
4019 			   "asked for %ld got raw resid %ld settled for %ld",
4020 			    (long) XS_XFRLEN(xs), (long) sp->req_resid,
4021 			    (long) XS_RESID(xs));
4022 			break;
4023 		case RQSTYPE_REQUEST:
4024 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
4025 				/*
4026 				 * Force Queue Full status.
4027 				 */
4028 				*XS_STSP(xs) = SCSI_QFULL;
4029 				XS_SETERR(xs, HBA_NOERROR);
4030 			} else if (XS_NOERR(xs)) {
4031 				/*
4032 				 * ????
4033 				 */
4034 				isp_prt(isp, ISP_LOGDEBUG0,
4035 				    "Request Queue Entry bounced back");
4036 				XS_SETERR(xs, HBA_BOTCH);
4037 			}
4038 			XS_RESID(xs) = XS_XFRLEN(xs);
4039 			break;
4040 		default:
4041 			isp_prt(isp, ISP_LOGWARN,
4042 			    "unhandled response queue type 0x%x",
4043 			    sp->req_header.rqs_entry_type);
4044 			if (XS_NOERR(xs)) {
4045 				XS_SETERR(xs, HBA_BOTCH);
4046 			}
4047 			break;
4048 		}
4049 
4050 		/*
4051 		 * Free any DMA resources. As a side effect, this may
4052 		 * also do any cache flushing necessary for data coherence.			 */
4053 		if (XS_XFRLEN(xs)) {
4054 			ISP_DMAFREE(isp, xs, sp->req_handle);
4055 		}
4056 
4057 		if (((isp->isp_dblev & (ISP_LOGDEBUG2|ISP_LOGDEBUG3))) ||
4058 		    ((isp->isp_dblev & ISP_LOGDEBUG1) && ((!XS_NOERR(xs)) ||
4059 		    (*XS_STSP(xs) != SCSI_GOOD)))) {
4060 			char skey;
4061 			if (sp->req_state_flags & RQSF_GOT_SENSE) {
4062 				skey = XS_SNSKEY(xs) & 0xf;
4063 				if (skey < 10)
4064 					skey += '0';
4065 				else
4066 					skey += 'a' - 10;
4067 			} else if (*XS_STSP(xs) == SCSI_CHECK) {
4068 				skey = '?';
4069 			} else {
4070 				skey = '.';
4071 			}
4072 			isp_prt(isp, ISP_LOGALL, finmsg, XS_CHANNEL(xs),
4073 			    XS_TGT(xs), XS_LUN(xs), XS_XFRLEN(xs), XS_RESID(xs),
4074 			    *XS_STSP(xs), skey, XS_ERR(xs));
4075 		}
4076 
4077 		if (isp->isp_nactive > 0)
4078 		    isp->isp_nactive--;
4079 		complist[ndone++] = xs;	/* defer completion call until later */
4080 		MEMZERO(hp, QENTRY_LEN);	/* PERF */
4081 		if (ndone == MAX_REQUESTQ_COMPLETIONS) {
4082 			break;
4083 		}
4084 	}
4085 
4086 	/*
4087 	 * If we looked at any commands, then it's valid to find out
4088 	 * what the outpointer is. It also is a trigger to update the
4089 	 * ISP's notion of what we've seen so far.
4090 	 */
4091 	if (nlooked) {
4092 		WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
4093 		/*
4094 		 * While we're at it, read the requst queue out pointer.
4095 		 */
4096 		isp->isp_reqodx = READ_REQUEST_QUEUE_OUT_POINTER(isp);
4097 		if (isp->isp_rscchiwater < ndone)
4098 			isp->isp_rscchiwater = ndone;
4099 	}
4100 
4101 	isp->isp_residx = optr;
4102 	isp->isp_rspbsy = 0;
4103 	for (i = 0; i < ndone; i++) {
4104 		xs = complist[i];
4105 		if (xs) {
4106 			isp->isp_rsltccmplt++;
4107 			isp_done(xs);
4108 		}
4109 	}
4110 }
4111 
4112 /*
4113  * Support routines.
4114  */
4115 
4116 static int
4117 isp_parse_async(ispsoftc_t *isp, uint16_t mbox)
4118 {
4119 	int rval = 0;
4120 	int bus;
4121 
4122 	if (IS_DUALBUS(isp)) {
4123 		bus = ISP_READ(isp, OUTMAILBOX6);
4124 	} else {
4125 		bus = 0;
4126 	}
4127 	isp_prt(isp, ISP_LOGDEBUG2, "Async Mbox 0x%x", mbox);
4128 
4129 	switch (mbox) {
4130 	case ASYNC_BUS_RESET:
4131 		isp->isp_sendmarker |= (1 << bus);
4132 #ifdef	ISP_TARGET_MODE
4133 		if (isp_target_async(isp, bus, mbox))
4134 			rval = -1;
4135 #endif
4136 		isp_async(isp, ISPASYNC_BUS_RESET, &bus);
4137 		break;
4138 	case ASYNC_SYSTEM_ERROR:
4139 		isp->isp_state = ISP_CRASHED;
4140 		if (IS_FC(isp)) {
4141 			FCPARAM(isp)->isp_loopstate = LOOP_NIL;
4142 			FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4143 		}
4144 		/*
4145 		 * Were we waiting for a mailbox command to complete?
4146 		 * If so, it's dead, so wake up the waiter.
4147 		 */
4148 		if (isp->isp_mboxbsy) {
4149 			isp->isp_obits = 1;
4150 			isp->isp_mboxtmp[0] = MBOX_HOST_INTERFACE_ERROR;
4151 			MBOX_NOTIFY_COMPLETE(isp);
4152 		}
4153 #ifdef	ISP_FW_CRASH_DUMP
4154 		/*
4155 		 * If we have crash dumps enabled, it's up to the handler
4156 		 * for isp_async to reinit stuff and restart the firmware
4157 		 * after performing the crash dump. The reason we do things
4158 		 * this way is that we may need to activate a kernel thread
4159 		 * to do all the crash dump goop.
4160 		 */
4161 		isp_async(isp, ISPASYNC_FW_CRASH, NULL);
4162 #else
4163 		isp_async(isp, ISPASYNC_FW_CRASH, NULL);
4164 		isp_reinit(isp);
4165 		isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
4166 #endif
4167 		rval = -1;
4168 		break;
4169 
4170 	case ASYNC_RQS_XFER_ERR:
4171 		isp_prt(isp, ISP_LOGERR, "Request Queue Transfer Error");
4172 		break;
4173 
4174 	case ASYNC_RSP_XFER_ERR:
4175 		isp_prt(isp, ISP_LOGERR, "Response Queue Transfer Error");
4176 		break;
4177 
4178 	case ASYNC_QWAKEUP:
4179 		/*
4180 		 * We've just been notified that the Queue has woken up.
4181 		 * We don't need to be chatty about this- just unlatch things
4182 		 * and move on.
4183 		 */
4184 		mbox = READ_REQUEST_QUEUE_OUT_POINTER(isp);
4185 		break;
4186 
4187 	case ASYNC_TIMEOUT_RESET:
4188 		isp_prt(isp, ISP_LOGWARN,
4189 		    "timeout initiated SCSI bus reset of bus %d", bus);
4190 		isp->isp_sendmarker |= (1 << bus);
4191 #ifdef	ISP_TARGET_MODE
4192 		if (isp_target_async(isp, bus, mbox))
4193 			rval = -1;
4194 #endif
4195 		break;
4196 
4197 	case ASYNC_DEVICE_RESET:
4198 		isp_prt(isp, ISP_LOGINFO, "device reset on bus %d", bus);
4199 		isp->isp_sendmarker |= (1 << bus);
4200 #ifdef	ISP_TARGET_MODE
4201 		if (isp_target_async(isp, bus, mbox))
4202 			rval = -1;
4203 #endif
4204 		break;
4205 
4206 	case ASYNC_EXTMSG_UNDERRUN:
4207 		isp_prt(isp, ISP_LOGWARN, "extended message underrun");
4208 		break;
4209 
4210 	case ASYNC_SCAM_INT:
4211 		isp_prt(isp, ISP_LOGINFO, "SCAM interrupt");
4212 		break;
4213 
4214 	case ASYNC_HUNG_SCSI:
4215 		isp_prt(isp, ISP_LOGERR,
4216 		    "stalled SCSI Bus after DATA Overrun");
4217 		/* XXX: Need to issue SCSI reset at this point */
4218 		break;
4219 
4220 	case ASYNC_KILLED_BUS:
4221 		isp_prt(isp, ISP_LOGERR, "SCSI Bus reset after DATA Overrun");
4222 		break;
4223 
4224 	case ASYNC_BUS_TRANSIT:
4225 		mbox = ISP_READ(isp, OUTMAILBOX2);
4226 		switch (mbox & 0x1c00) {
4227 		case SXP_PINS_LVD_MODE:
4228 			isp_prt(isp, ISP_LOGINFO, "Transition to LVD mode");
4229 			SDPARAM(isp)->isp_diffmode = 0;
4230 			SDPARAM(isp)->isp_ultramode = 0;
4231 			SDPARAM(isp)->isp_lvdmode = 1;
4232 			break;
4233 		case SXP_PINS_HVD_MODE:
4234 			isp_prt(isp, ISP_LOGINFO,
4235 			    "Transition to Differential mode");
4236 			SDPARAM(isp)->isp_diffmode = 1;
4237 			SDPARAM(isp)->isp_ultramode = 0;
4238 			SDPARAM(isp)->isp_lvdmode = 0;
4239 			break;
4240 		case SXP_PINS_SE_MODE:
4241 			isp_prt(isp, ISP_LOGINFO,
4242 			    "Transition to Single Ended mode");
4243 			SDPARAM(isp)->isp_diffmode = 0;
4244 			SDPARAM(isp)->isp_ultramode = 1;
4245 			SDPARAM(isp)->isp_lvdmode = 0;
4246 			break;
4247 		default:
4248 			isp_prt(isp, ISP_LOGWARN,
4249 			    "Transition to Unknown Mode 0x%x", mbox);
4250 			break;
4251 		}
4252 		/*
4253 		 * XXX: Set up to renegotiate again!
4254 		 */
4255 		/* Can only be for a 1080... */
4256 		isp->isp_sendmarker |= (1 << bus);
4257 		break;
4258 
4259 	/*
4260 	 * We can use bus, which will always be zero for FC cards,
4261 	 * as a mailbox pattern accumulator to be checked below.
4262 	 */
4263 	case ASYNC_RIO5:
4264 		bus = 0x1ce;	/* outgoing mailbox regs 1-3, 6-7 */
4265 		break;
4266 
4267 	case ASYNC_RIO4:
4268 		bus = 0x14e;	/* outgoing mailbox regs 1-3, 6 */
4269 		break;
4270 
4271 	case ASYNC_RIO3:
4272 		bus = 0x10e;	/* outgoing mailbox regs 1-3 */
4273 		break;
4274 
4275 	case ASYNC_RIO2:
4276 		bus = 0x106;	/* outgoing mailbox regs 1-2 */
4277 		break;
4278 
4279 	case ASYNC_RIO1:
4280 	case ASYNC_CMD_CMPLT:
4281 		bus = 0x102;	/* outgoing mailbox regs 1 */
4282 		break;
4283 
4284 	case ASYNC_RIO_RESP:
4285 		return (rval);
4286 
4287 	case ASYNC_CTIO_DONE:
4288 	{
4289 #ifdef	ISP_TARGET_MODE
4290 		int handle =
4291 		    (ISP_READ(isp, OUTMAILBOX2) << 16) |
4292 		    (ISP_READ(isp, OUTMAILBOX1));
4293 		if (isp_target_async(isp, handle, mbox)) {
4294 			rval = -1;
4295 		} else {
4296 			/* count it as a fast posting intr */
4297 			isp->isp_fphccmplt++;
4298 		}
4299 #else
4300 		isp_prt(isp, ISP_LOGINFO, "Fast Posting CTIO done");
4301 		isp->isp_fphccmplt++;	/* count it as a fast posting intr */
4302 #endif
4303 		break;
4304 	}
4305 	case ASYNC_LIP_F8:
4306 	case ASYNC_LIP_OCCURRED:
4307 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4308 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
4309 		isp->isp_sendmarker = 1;
4310 		isp_mark_getpdb_all(isp);
4311 		isp_async(isp, ISPASYNC_LIP, NULL);
4312 #ifdef	ISP_TARGET_MODE
4313 		if (isp_target_async(isp, bus, mbox))
4314 			rval = -1;
4315 #endif
4316 		/*
4317 		 * We've had problems with data corruption occuring on
4318 		 * commands that complete (with no apparent error) after
4319 		 * we receive a LIP. This has been observed mostly on
4320 		 * Local Loop topologies. To be safe, let's just mark
4321 		 * all active commands as dead.
4322 		 */
4323 		if (FCPARAM(isp)->isp_topo == TOPO_NL_PORT ||
4324 		    FCPARAM(isp)->isp_topo == TOPO_FL_PORT) {
4325 			int i, j;
4326 			for (i = j = 0; i < isp->isp_maxcmds; i++) {
4327 				XS_T *xs;
4328 				xs = isp->isp_xflist[i];
4329 				if (xs != NULL) {
4330 					j++;
4331 					XS_SETERR(xs, HBA_BUSRESET);
4332 				}
4333 			}
4334 			if (j) {
4335 				isp_prt(isp, ISP_LOGERR,
4336 				    "LIP destroyed %d active commands", j);
4337 			}
4338 		}
4339 		break;
4340 
4341 	case ASYNC_LOOP_UP:
4342 		isp->isp_sendmarker = 1;
4343 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4344 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
4345 		isp_mark_getpdb_all(isp);
4346 		isp_async(isp, ISPASYNC_LOOP_UP, NULL);
4347 #ifdef	ISP_TARGET_MODE
4348 		if (isp_target_async(isp, bus, mbox)) {
4349 			rval = -1;
4350 		}
4351 #endif
4352 		break;
4353 
4354 	case ASYNC_LOOP_DOWN:
4355 		isp->isp_sendmarker = 1;
4356 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4357 		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
4358 		isp_mark_getpdb_all(isp);
4359 		isp_async(isp, ISPASYNC_LOOP_DOWN, NULL);
4360 #ifdef	ISP_TARGET_MODE
4361 		if (isp_target_async(isp, bus, mbox)) {
4362 			rval = -1;
4363 		}
4364 #endif
4365 		break;
4366 
4367 	case ASYNC_LOOP_RESET:
4368 		isp->isp_sendmarker = 1;
4369 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4370 		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
4371 		isp_mark_getpdb_all(isp);
4372 		isp_async(isp, ISPASYNC_LOOP_RESET, NULL);
4373 #ifdef	ISP_TARGET_MODE
4374 		if (isp_target_async(isp, bus, mbox)) {
4375 			rval = -1;
4376 		}
4377 #endif
4378 		break;
4379 
4380 	case ASYNC_PDB_CHANGED:
4381 		isp->isp_sendmarker = 1;
4382 		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
4383 		isp_mark_getpdb_all(isp);
4384 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_PDB);
4385 		break;
4386 
4387 	case ASYNC_CHANGE_NOTIFY:
4388 		/*
4389 		 * Not correct, but it will force us to rescan the loop.
4390 		 */
4391 		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
4392 		isp_mark_getpdb_all(isp);
4393 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_SNS);
4394 		break;
4395 
4396 	case ASYNC_PTPMODE:
4397 		if (FCPARAM(isp)->isp_onfabric)
4398 			FCPARAM(isp)->isp_topo = TOPO_F_PORT;
4399 		else
4400 			FCPARAM(isp)->isp_topo = TOPO_N_PORT;
4401 		isp_mark_getpdb_all(isp);
4402 		isp->isp_sendmarker = 1;
4403 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4404 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
4405 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
4406 #ifdef	ISP_TARGET_MODE
4407 		if (isp_target_async(isp, bus, mbox))
4408 			rval = -1;
4409 #endif
4410 		isp_prt(isp, ISP_LOGINFO, "Point-to-Point mode");
4411 		break;
4412 
4413 	case ASYNC_CONNMODE:
4414 		mbox = ISP_READ(isp, OUTMAILBOX1);
4415 		isp_mark_getpdb_all(isp);
4416 		switch (mbox) {
4417 		case ISP_CONN_LOOP:
4418 			isp_prt(isp, ISP_LOGINFO,
4419 			    "Point-to-Point -> Loop mode");
4420 			break;
4421 		case ISP_CONN_PTP:
4422 			isp_prt(isp, ISP_LOGINFO,
4423 			    "Loop -> Point-to-Point mode");
4424 			break;
4425 		case ISP_CONN_BADLIP:
4426 			isp_prt(isp, ISP_LOGWARN,
4427 			    "Point-to-Point -> Loop mode (BAD LIP)");
4428 			break;
4429 		case ISP_CONN_FATAL:
4430 			isp_prt(isp, ISP_LOGERR, "FATAL CONNECTION ERROR");
4431 #ifdef	ISP_FW_CRASH_DUMP
4432 			isp_async(isp, ISPASYNC_FW_CRASH, NULL);
4433 #else
4434 			isp_async(isp, ISPASYNC_FW_CRASH, NULL);
4435 			isp_reinit(isp);
4436 			isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
4437 #endif
4438 			return (-1);
4439 		case ISP_CONN_LOOPBACK:
4440 			isp_prt(isp, ISP_LOGWARN,
4441 			    "Looped Back in Point-to-Point mode");
4442 			break;
4443 		default:
4444 			isp_prt(isp, ISP_LOGWARN,
4445 			    "Unknown connection mode (0x%x)", mbox);
4446 			break;
4447 		}
4448 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
4449 		isp->isp_sendmarker = 1;
4450 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4451 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
4452 		break;
4453 
4454 	default:
4455 		isp_prt(isp, ISP_LOGWARN, "Unknown Async Code 0x%x", mbox);
4456 		break;
4457 	}
4458 
4459 	if (bus & 0x100) {
4460 		int i, nh;
4461 		uint16_t handles[16];
4462 
4463 		for (nh = 0, i = 1; i < MAX_MAILBOX(isp); i++) {
4464 			if ((bus & (1 << i)) == 0) {
4465 				continue;
4466 			}
4467 			handles[nh++] = ISP_READ(isp, MBOX_OFF(i));
4468 		}
4469 		for (i = 0; i < nh; i++) {
4470 			isp_fastpost_complete(isp, handles[i]);
4471 			isp_prt(isp,  ISP_LOGDEBUG3,
4472 			    "fast post completion of %u", handles[i]);
4473 		}
4474 		if (isp->isp_fpcchiwater < nh)
4475 			isp->isp_fpcchiwater = nh;
4476 	} else {
4477 		isp->isp_intoasync++;
4478 	}
4479 	return (rval);
4480 }
4481 
4482 /*
4483  * Handle other response entries. A pointer to the request queue output
4484  * index is here in case we want to eat several entries at once, although
4485  * this is not used currently.
4486  */
4487 
4488 static int
4489 isp_handle_other_response(ispsoftc_t *isp, int type,
4490     isphdr_t *hp, uint16_t *optrp)
4491 {
4492 	switch (type) {
4493 	case RQSTYPE_STATUS_CONT:
4494 		isp_prt(isp, ISP_LOGINFO, "Ignored Continuation Response");
4495 		return (1);
4496 	case RQSTYPE_ATIO:
4497 	case RQSTYPE_CTIO:
4498 	case RQSTYPE_ENABLE_LUN:
4499 	case RQSTYPE_MODIFY_LUN:
4500 	case RQSTYPE_NOTIFY:
4501 	case RQSTYPE_NOTIFY_ACK:
4502 	case RQSTYPE_CTIO1:
4503 	case RQSTYPE_ATIO2:
4504 	case RQSTYPE_CTIO2:
4505 	case RQSTYPE_CTIO3:
4506 		isp->isp_rsltccmplt++;	/* count as a response completion */
4507 #ifdef	ISP_TARGET_MODE
4508 		if (isp_target_notify(isp, (ispstatusreq_t *) hp, optrp)) {
4509 			return (1);
4510 		}
4511 #endif
4512 		/* FALLTHROUGH */
4513 	case RQSTYPE_REQUEST:
4514 	default:
4515 		USEC_DELAY(100);
4516 		if (type != isp_get_response_type(isp, hp)) {
4517 			/*
4518 			 * This is questionable- we're just papering over
4519 			 * something we've seen on SMP linux in target
4520 			 * mode- we don't really know what's happening
4521 			 * here that causes us to think we've gotten
4522 			 * an entry, but that either the entry isn't
4523 			 * filled out yet or our CPU read data is stale.
4524 			 */
4525 			isp_prt(isp, ISP_LOGINFO,
4526 				"unstable type in response queue");
4527 			return (-1);
4528 		}
4529 		isp_prt(isp, ISP_LOGWARN, "Unhandled Response Type 0x%x",
4530 		    isp_get_response_type(isp, hp));
4531 		if (isp_async(isp, ISPASYNC_UNHANDLED_RESPONSE, hp)) {
4532 			return (1);
4533 		}
4534 		return (0);
4535 	}
4536 }
4537 
4538 static void
4539 isp_parse_status(ispsoftc_t *isp, ispstatusreq_t *sp, XS_T *xs)
4540 {
4541 	switch (sp->req_completion_status & 0xff) {
4542 	case RQCS_COMPLETE:
4543 		if (XS_NOERR(xs)) {
4544 			XS_SETERR(xs, HBA_NOERROR);
4545 		}
4546 		return;
4547 
4548 	case RQCS_INCOMPLETE:
4549 		if ((sp->req_state_flags & RQSF_GOT_TARGET) == 0) {
4550 			isp_prt(isp, ISP_LOGDEBUG1,
4551 			    "Selection Timeout for %d.%d.%d",
4552 			    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4553 			if (XS_NOERR(xs)) {
4554 				XS_SETERR(xs, HBA_SELTIMEOUT);
4555 			}
4556 			return;
4557 		}
4558 		isp_prt(isp, ISP_LOGERR,
4559 		    "command incomplete for %d.%d.%d, state 0x%x",
4560 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs),
4561 		    sp->req_state_flags);
4562 		break;
4563 
4564 	case RQCS_DMA_ERROR:
4565 		isp_prt(isp, ISP_LOGERR, "DMA error for command on %d.%d.%d",
4566 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4567 		break;
4568 
4569 	case RQCS_TRANSPORT_ERROR:
4570 	{
4571 		char buf[172];
4572 		SNPRINTF(buf, sizeof (buf), "states=>");
4573 		if (sp->req_state_flags & RQSF_GOT_BUS) {
4574 			SNPRINTF(buf, sizeof (buf), "%s GOT_BUS", buf);
4575 		}
4576 		if (sp->req_state_flags & RQSF_GOT_TARGET) {
4577 			SNPRINTF(buf, sizeof (buf), "%s GOT_TGT", buf);
4578 		}
4579 		if (sp->req_state_flags & RQSF_SENT_CDB) {
4580 			SNPRINTF(buf, sizeof (buf), "%s SENT_CDB", buf);
4581 		}
4582 		if (sp->req_state_flags & RQSF_XFRD_DATA) {
4583 			SNPRINTF(buf, sizeof (buf), "%s XFRD_DATA", buf);
4584 		}
4585 		if (sp->req_state_flags & RQSF_GOT_STATUS) {
4586 			SNPRINTF(buf, sizeof (buf), "%s GOT_STS", buf);
4587 		}
4588 		if (sp->req_state_flags & RQSF_GOT_SENSE) {
4589 			SNPRINTF(buf, sizeof (buf), "%s GOT_SNS", buf);
4590 		}
4591 		if (sp->req_state_flags & RQSF_XFER_COMPLETE) {
4592 			SNPRINTF(buf, sizeof (buf), "%s XFR_CMPLT", buf);
4593 		}
4594 		SNPRINTF(buf, sizeof (buf), "%s\nstatus=>", buf);
4595 		if (sp->req_status_flags & RQSTF_DISCONNECT) {
4596 			SNPRINTF(buf, sizeof (buf), "%s Disconnect", buf);
4597 		}
4598 		if (sp->req_status_flags & RQSTF_SYNCHRONOUS) {
4599 			SNPRINTF(buf, sizeof (buf), "%s Sync_xfr", buf);
4600 		}
4601 		if (sp->req_status_flags & RQSTF_PARITY_ERROR) {
4602 			SNPRINTF(buf, sizeof (buf), "%s Parity", buf);
4603 		}
4604 		if (sp->req_status_flags & RQSTF_BUS_RESET) {
4605 			SNPRINTF(buf, sizeof (buf), "%s Bus_Reset", buf);
4606 		}
4607 		if (sp->req_status_flags & RQSTF_DEVICE_RESET) {
4608 			SNPRINTF(buf, sizeof (buf), "%s Device_Reset", buf);
4609 		}
4610 		if (sp->req_status_flags & RQSTF_ABORTED) {
4611 			SNPRINTF(buf, sizeof (buf), "%s Aborted", buf);
4612 		}
4613 		if (sp->req_status_flags & RQSTF_TIMEOUT) {
4614 			SNPRINTF(buf, sizeof (buf), "%s Timeout", buf);
4615 		}
4616 		if (sp->req_status_flags & RQSTF_NEGOTIATION) {
4617 			SNPRINTF(buf, sizeof (buf), "%s Negotiation", buf);
4618 		}
4619 		isp_prt(isp, ISP_LOGERR, "%s", buf);
4620 		isp_prt(isp, ISP_LOGERR, "transport error for %d.%d.%d:\n%s",
4621 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), buf);
4622 		break;
4623 	}
4624 	case RQCS_RESET_OCCURRED:
4625 		isp_prt(isp, ISP_LOGWARN,
4626 		    "bus reset destroyed command for %d.%d.%d",
4627 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4628 		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
4629 		if (XS_NOERR(xs)) {
4630 			XS_SETERR(xs, HBA_BUSRESET);
4631 		}
4632 		return;
4633 
4634 	case RQCS_ABORTED:
4635 		isp_prt(isp, ISP_LOGERR, "command aborted for %d.%d.%d",
4636 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4637 		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
4638 		if (XS_NOERR(xs)) {
4639 			XS_SETERR(xs, HBA_ABORTED);
4640 		}
4641 		return;
4642 
4643 	case RQCS_TIMEOUT:
4644 		isp_prt(isp, ISP_LOGWARN, "command timed out for %d.%d.%d",
4645 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4646 		/*
4647 	 	 * Check to see if we logged out the device.
4648 		 */
4649 		if (IS_FC(isp)) {
4650 			if ((sp->req_completion_status & RQSTF_LOGOUT) &&
4651 			    FCPARAM(isp)->portdb[XS_TGT(xs)].valid &&
4652 			    FCPARAM(isp)->portdb[XS_TGT(xs)].fabric_dev) {
4653 				FCPARAM(isp)->portdb[XS_TGT(xs)].relogin = 1;
4654 			}
4655 		}
4656 		if (XS_NOERR(xs)) {
4657 			XS_SETERR(xs, HBA_CMDTIMEOUT);
4658 		}
4659 		return;
4660 
4661 	case RQCS_DATA_OVERRUN:
4662 		XS_RESID(xs) = sp->req_resid;
4663 		isp_prt(isp, ISP_LOGERR, "data overrun for command on %d.%d.%d",
4664 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4665 		if (XS_NOERR(xs)) {
4666 			XS_SETERR(xs, HBA_DATAOVR);
4667 		}
4668 		return;
4669 
4670 	case RQCS_COMMAND_OVERRUN:
4671 		isp_prt(isp, ISP_LOGERR,
4672 		    "command overrun for command on %d.%d.%d",
4673 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4674 		break;
4675 
4676 	case RQCS_STATUS_OVERRUN:
4677 		isp_prt(isp, ISP_LOGERR,
4678 		    "status overrun for command on %d.%d.%d",
4679 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4680 		break;
4681 
4682 	case RQCS_BAD_MESSAGE:
4683 		isp_prt(isp, ISP_LOGERR,
4684 		    "msg not COMMAND COMPLETE after status %d.%d.%d",
4685 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4686 		break;
4687 
4688 	case RQCS_NO_MESSAGE_OUT:
4689 		isp_prt(isp, ISP_LOGERR,
4690 		    "No MESSAGE OUT phase after selection on %d.%d.%d",
4691 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4692 		break;
4693 
4694 	case RQCS_EXT_ID_FAILED:
4695 		isp_prt(isp, ISP_LOGERR, "EXTENDED IDENTIFY failed %d.%d.%d",
4696 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4697 		break;
4698 
4699 	case RQCS_IDE_MSG_FAILED:
4700 		isp_prt(isp, ISP_LOGERR,
4701 		    "INITIATOR DETECTED ERROR rejected by %d.%d.%d",
4702 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4703 		break;
4704 
4705 	case RQCS_ABORT_MSG_FAILED:
4706 		isp_prt(isp, ISP_LOGERR, "ABORT OPERATION rejected by %d.%d.%d",
4707 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4708 		break;
4709 
4710 	case RQCS_REJECT_MSG_FAILED:
4711 		isp_prt(isp, ISP_LOGERR, "MESSAGE REJECT rejected by %d.%d.%d",
4712 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4713 		break;
4714 
4715 	case RQCS_NOP_MSG_FAILED:
4716 		isp_prt(isp, ISP_LOGERR, "NOP rejected by %d.%d.%d",
4717 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4718 		break;
4719 
4720 	case RQCS_PARITY_ERROR_MSG_FAILED:
4721 		isp_prt(isp, ISP_LOGERR,
4722 		    "MESSAGE PARITY ERROR rejected by %d.%d.%d",
4723 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4724 		break;
4725 
4726 	case RQCS_DEVICE_RESET_MSG_FAILED:
4727 		isp_prt(isp, ISP_LOGWARN,
4728 		    "BUS DEVICE RESET rejected by %d.%d.%d",
4729 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4730 		break;
4731 
4732 	case RQCS_ID_MSG_FAILED:
4733 		isp_prt(isp, ISP_LOGERR, "IDENTIFY rejected by %d.%d.%d",
4734 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4735 		break;
4736 
4737 	case RQCS_UNEXP_BUS_FREE:
4738 		isp_prt(isp, ISP_LOGERR, "%d.%d.%d had an unexpected bus free",
4739 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4740 		break;
4741 
4742 	case RQCS_DATA_UNDERRUN:
4743 	{
4744 		if (IS_FC(isp)) {
4745 			int ru_marked = (sp->req_scsi_status & RQCS_RU) != 0;
4746 			if (!ru_marked || sp->req_resid > XS_XFRLEN(xs)) {
4747 				isp_prt(isp, ISP_LOGWARN, bun, XS_TGT(xs),
4748 				    XS_LUN(xs), XS_XFRLEN(xs), sp->req_resid,
4749 				    (ru_marked)? "marked" : "not marked");
4750 				if (XS_NOERR(xs)) {
4751 					XS_SETERR(xs, HBA_BOTCH);
4752 				}
4753 				return;
4754 			}
4755 		}
4756 		XS_RESID(xs) = sp->req_resid;
4757 		if (XS_NOERR(xs)) {
4758 			XS_SETERR(xs, HBA_NOERROR);
4759 		}
4760 		return;
4761 	}
4762 
4763 	case RQCS_XACT_ERR1:
4764 		isp_prt(isp, ISP_LOGERR, xact1, XS_CHANNEL(xs),
4765 		    XS_TGT(xs), XS_LUN(xs));
4766 		break;
4767 
4768 	case RQCS_XACT_ERR2:
4769 		isp_prt(isp, ISP_LOGERR, xact2,
4770 		    XS_LUN(xs), XS_TGT(xs), XS_CHANNEL(xs));
4771 		break;
4772 
4773 	case RQCS_XACT_ERR3:
4774 		isp_prt(isp, ISP_LOGERR, xact3,
4775 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4776 		break;
4777 
4778 	case RQCS_BAD_ENTRY:
4779 		isp_prt(isp, ISP_LOGERR, "Invalid IOCB entry type detected");
4780 		break;
4781 
4782 	case RQCS_QUEUE_FULL:
4783 		isp_prt(isp, ISP_LOGDEBUG0,
4784 		    "internal queues full for %d.%d.%d status 0x%x",
4785 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), *XS_STSP(xs));
4786 
4787 		/*
4788 		 * If QFULL or some other status byte is set, then this
4789 		 * isn't an error, per se.
4790 		 *
4791 		 * Unfortunately, some QLogic f/w writers have, in
4792 		 * some cases, ommitted to *set* status to QFULL.
4793 		 *
4794 
4795 		if (*XS_STSP(xs) != SCSI_GOOD && XS_NOERR(xs)) {
4796 			XS_SETERR(xs, HBA_NOERROR);
4797 			return;
4798 		}
4799 
4800 		 *
4801 		 *
4802 		 */
4803 
4804 		*XS_STSP(xs) = SCSI_QFULL;
4805 		XS_SETERR(xs, HBA_NOERROR);
4806 		return;
4807 
4808 	case RQCS_PHASE_SKIPPED:
4809 		isp_prt(isp, ISP_LOGERR, pskip, XS_CHANNEL(xs),
4810 		    XS_TGT(xs), XS_LUN(xs));
4811 		break;
4812 
4813 	case RQCS_ARQS_FAILED:
4814 		isp_prt(isp, ISP_LOGERR,
4815 		    "Auto Request Sense failed for %d.%d.%d",
4816 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4817 		if (XS_NOERR(xs)) {
4818 			XS_SETERR(xs, HBA_ARQFAIL);
4819 		}
4820 		return;
4821 
4822 	case RQCS_WIDE_FAILED:
4823 		isp_prt(isp, ISP_LOGERR,
4824 		    "Wide Negotiation failed for %d.%d.%d",
4825 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
4826 		if (IS_SCSI(isp)) {
4827 			sdparam *sdp = isp->isp_param;
4828 			sdp += XS_CHANNEL(xs);
4829 			sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_WIDE;
4830 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
4831 			isp->isp_update |= (1 << XS_CHANNEL(xs));
4832 		}
4833 		if (XS_NOERR(xs)) {
4834 			XS_SETERR(xs, HBA_NOERROR);
4835 		}
4836 		return;
4837 
4838 	case RQCS_SYNCXFER_FAILED:
4839 		isp_prt(isp, ISP_LOGERR,
4840 		    "SDTR Message failed for target %d.%d.%d",
4841 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
4842 		if (IS_SCSI(isp)) {
4843 			sdparam *sdp = isp->isp_param;
4844 			sdp += XS_CHANNEL(xs);
4845 			sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_SYNC;
4846 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
4847 			isp->isp_update |= (1 << XS_CHANNEL(xs));
4848 		}
4849 		break;
4850 
4851 	case RQCS_LVD_BUSERR:
4852 		isp_prt(isp, ISP_LOGERR,
4853 		    "Bad LVD condition while talking to %d.%d.%d",
4854 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
4855 		break;
4856 
4857 	case RQCS_PORT_UNAVAILABLE:
4858 		/*
4859 		 * No such port on the loop. Moral equivalent of SELTIMEO
4860 		 */
4861 	case RQCS_PORT_LOGGED_OUT:
4862 		/*
4863 		 * It was there (maybe)- treat as a selection timeout.
4864 		 */
4865 		if ((sp->req_completion_status & 0xff) == RQCS_PORT_UNAVAILABLE)
4866 			isp_prt(isp, ISP_LOGINFO,
4867 			    "port unavailable for target %d", XS_TGT(xs));
4868 		else
4869 			isp_prt(isp, ISP_LOGINFO,
4870 			    "port logout for target %d", XS_TGT(xs));
4871 		/*
4872 		 * If we're on a local loop, force a LIP (which is overkill)
4873 		 * to force a re-login of this unit. If we're on fabric,
4874 		 * then we'll have to relogin as a matter of course.
4875 		 */
4876 		if (FCPARAM(isp)->isp_topo == TOPO_NL_PORT ||
4877 		    FCPARAM(isp)->isp_topo == TOPO_FL_PORT) {
4878 			mbreg_t mbs;
4879 			MEMZERO(&mbs, sizeof (mbs));
4880 			mbs.param[0] = MBOX_INIT_LIP;
4881 			if (IS_2KLOGIN(isp)) {
4882 				mbs.obits |= (1 << 10);
4883 			}
4884 			isp_mboxcmd_qnw(isp, &mbs, 1);
4885 		}
4886 
4887 		/*
4888 		 * Probably overkill.
4889 		 */
4890 		isp->isp_sendmarker = 1;
4891 		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
4892 		isp_mark_getpdb_all(isp);
4893 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
4894 		if (XS_NOERR(xs)) {
4895 			XS_SETERR(xs, HBA_SELTIMEOUT);
4896 		}
4897 		return;
4898 
4899 	case RQCS_PORT_CHANGED:
4900 		isp_prt(isp, ISP_LOGWARN,
4901 		    "port changed for target %d", XS_TGT(xs));
4902 		if (XS_NOERR(xs)) {
4903 			XS_SETERR(xs, HBA_SELTIMEOUT);
4904 		}
4905 		return;
4906 
4907 	case RQCS_PORT_BUSY:
4908 		isp_prt(isp, ISP_LOGWARN,
4909 		    "port busy for target %d", XS_TGT(xs));
4910 		if (XS_NOERR(xs)) {
4911 			XS_SETERR(xs, HBA_TGTBSY);
4912 		}
4913 		return;
4914 
4915 	default:
4916 		isp_prt(isp, ISP_LOGERR, "Unknown Completion Status 0x%x",
4917 		    sp->req_completion_status);
4918 		break;
4919 	}
4920 	if (XS_NOERR(xs)) {
4921 		XS_SETERR(xs, HBA_BOTCH);
4922 	}
4923 }
4924 
4925 static void
4926 isp_fastpost_complete(ispsoftc_t *isp, uint16_t fph)
4927 {
4928 	XS_T *xs;
4929 
4930 	if (fph == 0) {
4931 		return;
4932 	}
4933 	xs = isp_find_xs(isp, fph);
4934 	if (xs == NULL) {
4935 		isp_prt(isp, ISP_LOGWARN,
4936 		    "Command for fast post handle 0x%x not found", fph);
4937 		return;
4938 	}
4939 	isp_destroy_handle(isp, fph);
4940 
4941 	/*
4942 	 * Since we don't have a result queue entry item,
4943 	 * we must believe that SCSI status is zero and
4944 	 * that all data transferred.
4945 	 */
4946 	XS_SET_STATE_STAT(isp, xs, NULL);
4947 	XS_RESID(xs) = 0;
4948 	*XS_STSP(xs) = SCSI_GOOD;
4949 	if (XS_XFRLEN(xs)) {
4950 		ISP_DMAFREE(isp, xs, fph);
4951 	}
4952 	if (isp->isp_nactive)
4953 		isp->isp_nactive--;
4954 	isp->isp_fphccmplt++;
4955 	isp_done(xs);
4956 }
4957 
4958 static int
4959 isp_mbox_continue(ispsoftc_t *isp)
4960 {
4961 	mbreg_t mbs;
4962 	uint16_t *ptr;
4963 
4964 	switch (isp->isp_lastmbxcmd) {
4965 	case MBOX_WRITE_RAM_WORD:
4966 	case MBOX_READ_RAM_WORD:
4967 	case MBOX_WRITE_RAM_WORD_EXTENDED:
4968 	case MBOX_READ_RAM_WORD_EXTENDED:
4969 		break;
4970 	default:
4971 		return (1);
4972 	}
4973 	if (isp->isp_mboxtmp[0] != MBOX_COMMAND_COMPLETE) {
4974 		isp->isp_mbxwrk0 = 0;
4975 		return (-1);
4976 	}
4977 
4978 	/*
4979 	 * Clear the previous interrupt.
4980 	 */
4981 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
4982 	ISP_WRITE(isp, BIU_SEMA, 0);
4983 
4984 	/*
4985 	 * Continue with next word.
4986 	 */
4987 	MEMZERO(&mbs, sizeof (mbs));
4988 	ptr = isp->isp_mbxworkp;
4989 	switch (isp->isp_lastmbxcmd) {
4990 	case MBOX_WRITE_RAM_WORD:
4991 		mbs.param[2] = *ptr++;
4992 		mbs.param[1] = isp->isp_mbxwrk1++;
4993 		break;
4994 	case MBOX_WRITE_RAM_WORD_EXTENDED:
4995 		mbs.param[2] = *ptr++;
4996 		mbs.param[1] = isp->isp_mbxwrk1++;
4997 		if (isp->isp_mbxwrk1 == 0) {
4998 			isp->isp_mbxwrk8++;
4999 		}
5000 		mbs.param[8] = isp->isp_mbxwrk8;
5001 		break;
5002 	case MBOX_READ_RAM_WORD:
5003 		*ptr++ = isp->isp_mboxtmp[2];
5004 		mbs.param[1] = isp->isp_mbxwrk1++;
5005 		break;
5006 	case MBOX_READ_RAM_WORD_EXTENDED:
5007 		*ptr++ = isp->isp_mboxtmp[2];
5008 		mbs.param[1] = isp->isp_mbxwrk1++;
5009 		if (isp->isp_mbxwrk1 == 0) {
5010 			isp->isp_mbxwrk8++;
5011 		}
5012 		mbs.param[8] = isp->isp_mbxwrk8;
5013 		break;
5014 	}
5015 	isp->isp_mbxworkp = ptr;
5016 	mbs.param[0] = isp->isp_lastmbxcmd;
5017 	isp->isp_mbxwrk0 -= 1;
5018 	isp_mboxcmd_qnw(isp, &mbs, 0);
5019 	return (0);
5020 }
5021 
5022 
5023 #define	HIWRD(x)			((x) >> 16)
5024 #define	LOWRD(x)			((x)  & 0xffff)
5025 #define	ISPOPMAP(a, b)			(((a) << 16) | (b))
5026 static const uint32_t mbpscsi[] = {
5027 	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
5028 	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
5029 	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
5030 	ISPOPMAP(0x1f, 0x01),	/* 0x03: MBOX_DUMP_RAM */
5031 	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
5032 	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
5033 	ISPOPMAP(0x3f, 0x3f),	/* 0x06: MBOX_MAILBOX_REG_TEST */
5034 	ISPOPMAP(0x03, 0x07),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
5035 	ISPOPMAP(0x01, 0x0f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
5036 	ISPOPMAP(0x00, 0x00),	/* 0x09: */
5037 	ISPOPMAP(0x00, 0x00),	/* 0x0a: */
5038 	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
5039 	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
5040 	ISPOPMAP(0x00, 0x00),	/* 0x0d: */
5041 	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
5042 	ISPOPMAP(0x00, 0x00),	/* 0x0f: */
5043 	ISPOPMAP(0x1f, 0x1f),	/* 0x10: MBOX_INIT_REQ_QUEUE */
5044 	ISPOPMAP(0x3f, 0x3f),	/* 0x11: MBOX_INIT_RES_QUEUE */
5045 	ISPOPMAP(0x0f, 0x0f),	/* 0x12: MBOX_EXECUTE_IOCB */
5046 	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
5047 	ISPOPMAP(0x01, 0x3f),	/* 0x14: MBOX_STOP_FIRMWARE */
5048 	ISPOPMAP(0x0f, 0x0f),	/* 0x15: MBOX_ABORT */
5049 	ISPOPMAP(0x03, 0x03),	/* 0x16: MBOX_ABORT_DEVICE */
5050 	ISPOPMAP(0x07, 0x07),	/* 0x17: MBOX_ABORT_TARGET */
5051 	ISPOPMAP(0x07, 0x07),	/* 0x18: MBOX_BUS_RESET */
5052 	ISPOPMAP(0x03, 0x07),	/* 0x19: MBOX_STOP_QUEUE */
5053 	ISPOPMAP(0x03, 0x07),	/* 0x1a: MBOX_START_QUEUE */
5054 	ISPOPMAP(0x03, 0x07),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
5055 	ISPOPMAP(0x03, 0x07),	/* 0x1c: MBOX_ABORT_QUEUE */
5056 	ISPOPMAP(0x03, 0x4f),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
5057 	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
5058 	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
5059 	ISPOPMAP(0x01, 0x07),	/* 0x20: MBOX_GET_INIT_SCSI_ID */
5060 	ISPOPMAP(0x01, 0x07),	/* 0x21: MBOX_GET_SELECT_TIMEOUT */
5061 	ISPOPMAP(0x01, 0xc7),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
5062 	ISPOPMAP(0x01, 0x07),	/* 0x23: MBOX_GET_TAG_AGE_LIMIT */
5063 	ISPOPMAP(0x01, 0x03),	/* 0x24: MBOX_GET_CLOCK_RATE */
5064 	ISPOPMAP(0x01, 0x07),	/* 0x25: MBOX_GET_ACT_NEG_STATE */
5065 	ISPOPMAP(0x01, 0x07),	/* 0x26: MBOX_GET_ASYNC_DATA_SETUP_TIME */
5066 	ISPOPMAP(0x01, 0x07),	/* 0x27: MBOX_GET_PCI_PARAMS */
5067 	ISPOPMAP(0x03, 0x4f),	/* 0x28: MBOX_GET_TARGET_PARAMS */
5068 	ISPOPMAP(0x03, 0x0f),	/* 0x29: MBOX_GET_DEV_QUEUE_PARAMS */
5069 	ISPOPMAP(0x01, 0x07),	/* 0x2a: MBOX_GET_RESET_DELAY_PARAMS */
5070 	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
5071 	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
5072 	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
5073 	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
5074 	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
5075 	ISPOPMAP(0x03, 0x03),	/* 0x30: MBOX_SET_INIT_SCSI_ID */
5076 	ISPOPMAP(0x07, 0x07),	/* 0x31: MBOX_SET_SELECT_TIMEOUT */
5077 	ISPOPMAP(0xc7, 0xc7),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
5078 	ISPOPMAP(0x07, 0x07),	/* 0x33: MBOX_SET_TAG_AGE_LIMIT */
5079 	ISPOPMAP(0x03, 0x03),	/* 0x34: MBOX_SET_CLOCK_RATE */
5080 	ISPOPMAP(0x07, 0x07),	/* 0x35: MBOX_SET_ACT_NEG_STATE */
5081 	ISPOPMAP(0x07, 0x07),	/* 0x36: MBOX_SET_ASYNC_DATA_SETUP_TIME */
5082 	ISPOPMAP(0x07, 0x07),	/* 0x37: MBOX_SET_PCI_CONTROL_PARAMS */
5083 	ISPOPMAP(0x4f, 0x4f),	/* 0x38: MBOX_SET_TARGET_PARAMS */
5084 	ISPOPMAP(0x0f, 0x0f),	/* 0x39: MBOX_SET_DEV_QUEUE_PARAMS */
5085 	ISPOPMAP(0x07, 0x07),	/* 0x3a: MBOX_SET_RESET_DELAY_PARAMS */
5086 	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
5087 	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
5088 	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
5089 	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
5090 	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
5091 	ISPOPMAP(0x01, 0x03),	/* 0x40: MBOX_RETURN_BIOS_BLOCK_ADDR */
5092 	ISPOPMAP(0x3f, 0x01),	/* 0x41: MBOX_WRITE_FOUR_RAM_WORDS */
5093 	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_EXEC_BIOS_IOCB */
5094 	ISPOPMAP(0x00, 0x00),	/* 0x43: */
5095 	ISPOPMAP(0x00, 0x00),	/* 0x44: */
5096 	ISPOPMAP(0x03, 0x03),	/* 0x45: SET SYSTEM PARAMETER */
5097 	ISPOPMAP(0x01, 0x03),	/* 0x46: GET SYSTEM PARAMETER */
5098 	ISPOPMAP(0x00, 0x00),	/* 0x47: */
5099 	ISPOPMAP(0x01, 0xcf),	/* 0x48: GET SCAM CONFIGURATION */
5100 	ISPOPMAP(0xcf, 0xcf),	/* 0x49: SET SCAM CONFIGURATION */
5101 	ISPOPMAP(0x03, 0x03),	/* 0x4a: MBOX_SET_FIRMWARE_FEATURES */
5102 	ISPOPMAP(0x01, 0x03),	/* 0x4b: MBOX_GET_FIRMWARE_FEATURES */
5103 	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
5104 	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
5105 	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
5106 	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
5107 	ISPOPMAP(0xdf, 0xdf),	/* 0x50: LOAD RAM A64 */
5108 	ISPOPMAP(0xdf, 0xdf),	/* 0x51: DUMP RAM A64 */
5109 	ISPOPMAP(0xdf, 0xff),	/* 0x52: INITIALIZE REQUEST QUEUE A64 */
5110 	ISPOPMAP(0xef, 0xff),	/* 0x53: INITIALIZE RESPONSE QUEUE A64 */
5111 	ISPOPMAP(0xcf, 0x01),	/* 0x54: EXECUTE IOCB A64 */
5112 	ISPOPMAP(0x07, 0x01),	/* 0x55: ENABLE TARGET MODE */
5113 	ISPOPMAP(0x03, 0x0f),	/* 0x56: GET TARGET STATUS */
5114 	ISPOPMAP(0x00, 0x00),	/* 0x57: */
5115 	ISPOPMAP(0x00, 0x00),	/* 0x58: */
5116 	ISPOPMAP(0x00, 0x00),	/* 0x59: */
5117 	ISPOPMAP(0x03, 0x03),	/* 0x5a: SET DATA OVERRUN RECOVERY MODE */
5118 	ISPOPMAP(0x01, 0x03),	/* 0x5b: GET DATA OVERRUN RECOVERY MODE */
5119 	ISPOPMAP(0x0f, 0x0f),	/* 0x5c: SET HOST DATA */
5120 	ISPOPMAP(0x01, 0x01)	/* 0x5d: GET NOST DATA */
5121 };
5122 
5123 #ifndef	ISP_STRIPPED
5124 static char *scsi_mbcmd_names[] = {
5125 	"NO-OP",
5126 	"LOAD RAM",
5127 	"EXEC FIRMWARE",
5128 	"DUMP RAM",
5129 	"WRITE RAM WORD",
5130 	"READ RAM WORD",
5131 	"MAILBOX REG TEST",
5132 	"VERIFY CHECKSUM",
5133 	"ABOUT FIRMWARE",
5134 	NULL,
5135 	NULL,
5136 	NULL,
5137 	NULL,
5138 	NULL,
5139 	"CHECK FIRMWARE",
5140 	NULL,
5141 	"INIT REQUEST QUEUE",
5142 	"INIT RESULT QUEUE",
5143 	"EXECUTE IOCB",
5144 	"WAKE UP",
5145 	"STOP FIRMWARE",
5146 	"ABORT",
5147 	"ABORT DEVICE",
5148 	"ABORT TARGET",
5149 	"BUS RESET",
5150 	"STOP QUEUE",
5151 	"START QUEUE",
5152 	"SINGLE STEP QUEUE",
5153 	"ABORT QUEUE",
5154 	"GET DEV QUEUE STATUS",
5155 	NULL,
5156 	"GET FIRMWARE STATUS",
5157 	"GET INIT SCSI ID",
5158 	"GET SELECT TIMEOUT",
5159 	"GET RETRY COUNT",
5160 	"GET TAG AGE LIMIT",
5161 	"GET CLOCK RATE",
5162 	"GET ACT NEG STATE",
5163 	"GET ASYNC DATA SETUP TIME",
5164 	"GET PCI PARAMS",
5165 	"GET TARGET PARAMS",
5166 	"GET DEV QUEUE PARAMS",
5167 	"GET RESET DELAY PARAMS",
5168 	NULL,
5169 	NULL,
5170 	NULL,
5171 	NULL,
5172 	NULL,
5173 	"SET INIT SCSI ID",
5174 	"SET SELECT TIMEOUT",
5175 	"SET RETRY COUNT",
5176 	"SET TAG AGE LIMIT",
5177 	"SET CLOCK RATE",
5178 	"SET ACT NEG STATE",
5179 	"SET ASYNC DATA SETUP TIME",
5180 	"SET PCI CONTROL PARAMS",
5181 	"SET TARGET PARAMS",
5182 	"SET DEV QUEUE PARAMS",
5183 	"SET RESET DELAY PARAMS",
5184 	NULL,
5185 	NULL,
5186 	NULL,
5187 	NULL,
5188 	NULL,
5189 	"RETURN BIOS BLOCK ADDR",
5190 	"WRITE FOUR RAM WORDS",
5191 	"EXEC BIOS IOCB",
5192 	NULL,
5193 	NULL,
5194 	"SET SYSTEM PARAMETER",
5195 	"GET SYSTEM PARAMETER",
5196 	NULL,
5197 	"GET SCAM CONFIGURATION",
5198 	"SET SCAM CONFIGURATION",
5199 	"SET FIRMWARE FEATURES",
5200 	"GET FIRMWARE FEATURES",
5201 	NULL,
5202 	NULL,
5203 	NULL,
5204 	NULL,
5205 	"LOAD RAM A64",
5206 	"DUMP RAM A64",
5207 	"INITIALIZE REQUEST QUEUE A64",
5208 	"INITIALIZE RESPONSE QUEUE A64",
5209 	"EXECUTE IOCB A64",
5210 	"ENABLE TARGET MODE",
5211 	"GET TARGET MODE STATE",
5212 	NULL,
5213 	NULL,
5214 	NULL,
5215 	"SET DATA OVERRUN RECOVERY MODE",
5216 	"GET DATA OVERRUN RECOVERY MODE",
5217 	"SET HOST DATA",
5218 	"GET NOST DATA",
5219 };
5220 #endif
5221 
5222 static const uint32_t mbpfc[] = {
5223 	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
5224 	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
5225 	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
5226 	ISPOPMAP(0xdf, 0x01),	/* 0x03: MBOX_DUMP_RAM */
5227 	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
5228 	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
5229 	ISPOPMAP(0xff, 0xff),	/* 0x06: MBOX_MAILBOX_REG_TEST */
5230 	ISPOPMAP(0x03, 0x05),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
5231 	ISPOPMAP(0x01, 0x4f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
5232 	ISPOPMAP(0xdf, 0x01),	/* 0x09: LOAD RAM */
5233 	ISPOPMAP(0xdf, 0x01),	/* 0x0a: DUMP RAM */
5234 	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
5235 	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
5236 	ISPOPMAP(0x13, 0x01),	/* 0x0d: MBOX_WRITE_RAM_WORD_EXTENDED) */
5237 	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
5238 	ISPOPMAP(0x13, 0x05),	/* 0x0f: MBOX_READ_RAM_WORD_EXTENDED */
5239 	ISPOPMAP(0x1f, 0x11),	/* 0x10: MBOX_INIT_REQ_QUEUE */
5240 	ISPOPMAP(0x2f, 0x21),	/* 0x11: MBOX_INIT_RES_QUEUE */
5241 	ISPOPMAP(0x0f, 0x01),	/* 0x12: MBOX_EXECUTE_IOCB */
5242 	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
5243 	ISPOPMAP(0x01, 0xff),	/* 0x14: MBOX_STOP_FIRMWARE */
5244 	ISPOPMAP(0x4f, 0x01),	/* 0x15: MBOX_ABORT */
5245 	ISPOPMAP(0x07, 0x01),	/* 0x16: MBOX_ABORT_DEVICE */
5246 	ISPOPMAP(0x07, 0x01),	/* 0x17: MBOX_ABORT_TARGET */
5247 	ISPOPMAP(0x03, 0x03),	/* 0x18: MBOX_BUS_RESET */
5248 	ISPOPMAP(0x07, 0x05),	/* 0x19: MBOX_STOP_QUEUE */
5249 	ISPOPMAP(0x07, 0x05),	/* 0x1a: MBOX_START_QUEUE */
5250 	ISPOPMAP(0x07, 0x05),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
5251 	ISPOPMAP(0x07, 0x05),	/* 0x1c: MBOX_ABORT_QUEUE */
5252 	ISPOPMAP(0x07, 0x03),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
5253 	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
5254 	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
5255 	ISPOPMAP(0x01, 0x4f),	/* 0x20: MBOX_GET_LOOP_ID */
5256 	ISPOPMAP(0x00, 0x00),	/* 0x21: */
5257 	ISPOPMAP(0x01, 0x07),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
5258 	ISPOPMAP(0x00, 0x00),	/* 0x23: */
5259 	ISPOPMAP(0x00, 0x00),	/* 0x24: */
5260 	ISPOPMAP(0x00, 0x00),	/* 0x25: */
5261 	ISPOPMAP(0x00, 0x00),	/* 0x26: */
5262 	ISPOPMAP(0x00, 0x00),	/* 0x27: */
5263 	ISPOPMAP(0x01, 0x03),	/* 0x28: MBOX_GET_FIRMWARE_OPTIONS */
5264 	ISPOPMAP(0x03, 0x07),	/* 0x29: MBOX_GET_PORT_QUEUE_PARAMS */
5265 	ISPOPMAP(0x00, 0x00),	/* 0x2a: */
5266 	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
5267 	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
5268 	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
5269 	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
5270 	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
5271 	ISPOPMAP(0x00, 0x00),	/* 0x30: */
5272 	ISPOPMAP(0x00, 0x00),	/* 0x31: */
5273 	ISPOPMAP(0x07, 0x07),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
5274 	ISPOPMAP(0x00, 0x00),	/* 0x33: */
5275 	ISPOPMAP(0x00, 0x00),	/* 0x34: */
5276 	ISPOPMAP(0x00, 0x00),	/* 0x35: */
5277 	ISPOPMAP(0x00, 0x00),	/* 0x36: */
5278 	ISPOPMAP(0x00, 0x00),	/* 0x37: */
5279 	ISPOPMAP(0x0f, 0x01),	/* 0x38: MBOX_SET_FIRMWARE_OPTIONS */
5280 	ISPOPMAP(0x0f, 0x07),	/* 0x39: MBOX_SET_PORT_QUEUE_PARAMS */
5281 	ISPOPMAP(0x00, 0x00),	/* 0x3a: */
5282 	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
5283 	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
5284 	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
5285 	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
5286 	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
5287 	ISPOPMAP(0x03, 0x01),	/* 0x40: MBOX_LOOP_PORT_BYPASS */
5288 	ISPOPMAP(0x03, 0x01),	/* 0x41: MBOX_LOOP_PORT_ENABLE */
5289 	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_GET_RESOURCE_COUNTS */
5290 	ISPOPMAP(0x01, 0x01),	/* 0x43: MBOX_REQUEST_NON_PARTICIPATING_MODE */
5291 	ISPOPMAP(0x00, 0x00),	/* 0x44: */
5292 	ISPOPMAP(0x00, 0x00),	/* 0x45: */
5293 	ISPOPMAP(0x00, 0x00),	/* 0x46: */
5294 	ISPOPMAP(0xcf, 0x03),	/* 0x47: GET PORT_DATABASE ENHANCED */
5295 	ISPOPMAP(0x00, 0x00),	/* 0x48: */
5296 	ISPOPMAP(0x00, 0x00),	/* 0x49: */
5297 	ISPOPMAP(0x00, 0x00),	/* 0x4a: */
5298 	ISPOPMAP(0x00, 0x00),	/* 0x4b: */
5299 	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
5300 	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
5301 	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
5302 	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
5303 	ISPOPMAP(0x00, 0x00),	/* 0x50: */
5304 	ISPOPMAP(0x00, 0x00),	/* 0x51: */
5305 	ISPOPMAP(0x00, 0x00),	/* 0x52: */
5306 	ISPOPMAP(0x00, 0x00),	/* 0x53: */
5307 	ISPOPMAP(0xcf, 0x01),	/* 0x54: EXECUTE IOCB A64 */
5308 	ISPOPMAP(0x00, 0x00),	/* 0x55: */
5309 	ISPOPMAP(0x00, 0x00),	/* 0x56: */
5310 	ISPOPMAP(0x00, 0x00),	/* 0x57: */
5311 	ISPOPMAP(0x00, 0x00),	/* 0x58: */
5312 	ISPOPMAP(0x00, 0x00),	/* 0x59: */
5313 	ISPOPMAP(0x00, 0x00),	/* 0x5a: */
5314 	ISPOPMAP(0x03, 0x01),	/* 0x5b: MBOX_DRIVER_HEARTBEAT */
5315 	ISPOPMAP(0xcf, 0x01),	/* 0x5c: MBOX_FW_HEARTBEAT */
5316 	ISPOPMAP(0x07, 0x03),	/* 0x5d: MBOX_GET_SET_DATA_RATE */
5317 	ISPOPMAP(0x00, 0x00),	/* 0x5e: */
5318 	ISPOPMAP(0x00, 0x00),	/* 0x5f: */
5319 	ISPOPMAP(0xfd, 0x31),	/* 0x60: MBOX_INIT_FIRMWARE */
5320 	ISPOPMAP(0x00, 0x00),	/* 0x61: */
5321 	ISPOPMAP(0x01, 0x01),	/* 0x62: MBOX_INIT_LIP */
5322 	ISPOPMAP(0xcd, 0x03),	/* 0x63: MBOX_GET_FC_AL_POSITION_MAP */
5323 	ISPOPMAP(0xcf, 0x01),	/* 0x64: MBOX_GET_PORT_DB */
5324 	ISPOPMAP(0x07, 0x01),	/* 0x65: MBOX_CLEAR_ACA */
5325 	ISPOPMAP(0x07, 0x01),	/* 0x66: MBOX_TARGET_RESET */
5326 	ISPOPMAP(0x07, 0x01),	/* 0x67: MBOX_CLEAR_TASK_SET */
5327 	ISPOPMAP(0x07, 0x01),	/* 0x68: MBOX_ABORT_TASK_SET */
5328 	ISPOPMAP(0x01, 0x07),	/* 0x69: MBOX_GET_FW_STATE */
5329 	ISPOPMAP(0x03, 0xcf),	/* 0x6a: MBOX_GET_PORT_NAME */
5330 	ISPOPMAP(0xcf, 0x01),	/* 0x6b: MBOX_GET_LINK_STATUS */
5331 	ISPOPMAP(0x0f, 0x01),	/* 0x6c: MBOX_INIT_LIP_RESET */
5332 	ISPOPMAP(0x00, 0x00),	/* 0x6d: */
5333 	ISPOPMAP(0xcf, 0x03),	/* 0x6e: MBOX_SEND_SNS */
5334 	ISPOPMAP(0x0f, 0x07),	/* 0x6f: MBOX_FABRIC_LOGIN */
5335 	ISPOPMAP(0x03, 0x01),	/* 0x70: MBOX_SEND_CHANGE_REQUEST */
5336 	ISPOPMAP(0x03, 0x03),	/* 0x71: MBOX_FABRIC_LOGOUT */
5337 	ISPOPMAP(0x0f, 0x0f),	/* 0x72: MBOX_INIT_LIP_LOGIN */
5338 	ISPOPMAP(0x00, 0x00),	/* 0x73: */
5339 	ISPOPMAP(0x07, 0x01),	/* 0x74: LOGIN LOOP PORT */
5340 	ISPOPMAP(0xcf, 0x03),	/* 0x75: GET PORT/NODE NAME LIST */
5341 	ISPOPMAP(0x4f, 0x01),	/* 0x76: SET VENDOR ID */
5342 	ISPOPMAP(0xcd, 0x01),	/* 0x77: INITIALIZE IP MAILBOX */
5343 	ISPOPMAP(0x00, 0x00),	/* 0x78: */
5344 	ISPOPMAP(0x00, 0x00),	/* 0x79: */
5345 	ISPOPMAP(0x00, 0x00),	/* 0x7a: */
5346 	ISPOPMAP(0x00, 0x00),	/* 0x7b: */
5347 	ISPOPMAP(0x4f, 0x03),	/* 0x7c: Get ID List */
5348 	ISPOPMAP(0xcf, 0x01),	/* 0x7d: SEND LFA */
5349 	ISPOPMAP(0x0f, 0x01)	/* 0x7e: LUN RESET */
5350 };
5351 /*
5352  * Footnotes
5353  *
5354  * (1): this sets bits 21..16 in mailbox register #8, which we nominally
5355  *	do not access at this time in the core driver. The caller is
5356  *	responsible for setting this register first (Gross!). The assumption
5357  *	is that we won't overflow.
5358  */
5359 
5360 #ifndef	ISP_STRIPPED
5361 static char *fc_mbcmd_names[] = {
5362 	"NO-OP",
5363 	"LOAD RAM",
5364 	"EXEC FIRMWARE",
5365 	"DUMP RAM",
5366 	"WRITE RAM WORD",
5367 	"READ RAM WORD",
5368 	"MAILBOX REG TEST",
5369 	"VERIFY CHECKSUM",
5370 	"ABOUT FIRMWARE",
5371 	"LOAD RAM",
5372 	"DUMP RAM",
5373 	"WRITE RAM WORD EXTENDED",
5374 	NULL,
5375 	"READ RAM WORD EXTENDED",
5376 	"CHECK FIRMWARE",
5377 	NULL,
5378 	"INIT REQUEST QUEUE",
5379 	"INIT RESULT QUEUE",
5380 	"EXECUTE IOCB",
5381 	"WAKE UP",
5382 	"STOP FIRMWARE",
5383 	"ABORT",
5384 	"ABORT DEVICE",
5385 	"ABORT TARGET",
5386 	"BUS RESET",
5387 	"STOP QUEUE",
5388 	"START QUEUE",
5389 	"SINGLE STEP QUEUE",
5390 	"ABORT QUEUE",
5391 	"GET DEV QUEUE STATUS",
5392 	NULL,
5393 	"GET FIRMWARE STATUS",
5394 	"GET LOOP ID",
5395 	NULL,
5396 	"GET RETRY COUNT",
5397 	NULL,
5398 	NULL,
5399 	NULL,
5400 	NULL,
5401 	NULL,
5402 	"GET FIRMWARE OPTIONS",
5403 	"GET PORT QUEUE PARAMS",
5404 	NULL,
5405 	NULL,
5406 	NULL,
5407 	NULL,
5408 	NULL,
5409 	NULL,
5410 	NULL,
5411 	NULL,
5412 	"SET RETRY COUNT",
5413 	NULL,
5414 	NULL,
5415 	NULL,
5416 	NULL,
5417 	NULL,
5418 	"SET FIRMWARE OPTIONS",
5419 	"SET PORT QUEUE PARAMS",
5420 	NULL,
5421 	NULL,
5422 	NULL,
5423 	NULL,
5424 	NULL,
5425 	NULL,
5426 	"LOOP PORT BYPASS",
5427 	"LOOP PORT ENABLE",
5428 	"GET RESOURCE COUNTS",
5429 	"REQUEST NON PARTICIPATING MODE",
5430 	NULL,
5431 	NULL,
5432 	NULL,
5433 	"GET PORT DATABASE ENHANCED",
5434 	NULL,
5435 	NULL,
5436 	NULL,
5437 	NULL,
5438 	NULL,
5439 	NULL,
5440 	NULL,
5441 	NULL,
5442 	NULL,
5443 	NULL,
5444 	NULL,
5445 	NULL,
5446 	"EXECUTE IOCB A64",
5447 	NULL,
5448 	NULL,
5449 	NULL,
5450 	NULL,
5451 	NULL,
5452 	NULL,
5453 	"DRIVER HEARTBEAT",
5454 	NULL,
5455 	"GET/SET DATA RATE",
5456 	NULL,
5457 	NULL,
5458 	"INIT FIRMWARE",
5459 	NULL,
5460 	"INIT LIP",
5461 	"GET FC-AL POSITION MAP",
5462 	"GET PORT DATABASE",
5463 	"CLEAR ACA",
5464 	"TARGET RESET",
5465 	"CLEAR TASK SET",
5466 	"ABORT TASK SET",
5467 	"GET FW STATE",
5468 	"GET PORT NAME",
5469 	"GET LINK STATUS",
5470 	"INIT LIP RESET",
5471 	NULL,
5472 	"SEND SNS",
5473 	"FABRIC LOGIN",
5474 	"SEND CHANGE REQUEST",
5475 	"FABRIC LOGOUT",
5476 	"INIT LIP LOGIN",
5477 	NULL,
5478 	"LOGIN LOOP PORT",
5479 	"GET PORT/NODE NAME LIST",
5480 	"SET VENDOR ID",
5481 	"INITIALIZE IP MAILBOX",
5482 	NULL,
5483 	NULL,
5484 	NULL,
5485 	NULL,
5486 	"Get ID List",
5487 	"SEND LFA",
5488 	"Lun RESET"
5489 };
5490 #endif
5491 
5492 static void
5493 isp_mboxcmd_qnw(ispsoftc_t *isp, mbreg_t *mbp, int nodelay)
5494 {
5495 	unsigned int ibits, obits, box, opcode;
5496 	const uint32_t *mcp;
5497 
5498 	if (IS_FC(isp)) {
5499 		mcp = mbpfc;
5500 	} else {
5501 		mcp = mbpscsi;
5502 	}
5503 	opcode = mbp->param[0];
5504 	ibits = HIWRD(mcp[opcode]) & NMBOX_BMASK(isp);
5505 	obits = LOWRD(mcp[opcode]) & NMBOX_BMASK(isp);
5506 	ibits |= mbp->ibits;
5507 	obits |= mbp->obits;
5508 	for (box = 0; box < MAX_MAILBOX(isp); box++) {
5509 		if (ibits & (1 << box)) {
5510 			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
5511 		}
5512 		if (nodelay == 0) {
5513 			isp->isp_mboxtmp[box] = mbp->param[box] = 0;
5514 		}
5515 	}
5516 	if (nodelay == 0) {
5517 		isp->isp_lastmbxcmd = opcode;
5518 		isp->isp_obits = obits;
5519 		isp->isp_mboxbsy = 1;
5520 	}
5521 	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
5522 	/*
5523 	 * Oddly enough, if we're not delaying for an answer,
5524 	 * delay a bit to give the f/w a chance to pick up the
5525 	 * command.
5526 	 */
5527 	if (nodelay) {
5528 		USEC_DELAY(1000);
5529 	}
5530 }
5531 
5532 static void
5533 isp_mboxcmd(ispsoftc_t *isp, mbreg_t *mbp, int logmask)
5534 {
5535 	char *cname, *xname, tname[16], mname[16];
5536 	unsigned int lim, ibits, obits, box, opcode;
5537 	const uint32_t *mcp;
5538 
5539 	if (IS_FC(isp)) {
5540 		mcp = mbpfc;
5541 		lim = (sizeof (mbpfc) / sizeof (mbpfc[0]));
5542 	} else {
5543 		mcp = mbpscsi;
5544 		lim = (sizeof (mbpscsi) / sizeof (mbpscsi[0]));
5545 	}
5546 
5547 	if ((opcode = mbp->param[0]) >= lim) {
5548 		mbp->param[0] = MBOX_INVALID_COMMAND;
5549 		isp_prt(isp, ISP_LOGERR, "Unknown Command 0x%x", opcode);
5550 		return;
5551 	}
5552 
5553 	ibits = HIWRD(mcp[opcode]) & NMBOX_BMASK(isp);
5554 	obits = LOWRD(mcp[opcode]) & NMBOX_BMASK(isp);
5555 
5556 	/*
5557 	 * Pick up any additional bits that the caller might have set.
5558 	 */
5559 	ibits |= mbp->ibits;
5560 	obits |= mbp->obits;
5561 
5562 	if (ibits == 0 && obits == 0) {
5563 		mbp->param[0] = MBOX_COMMAND_PARAM_ERROR;
5564 		isp_prt(isp, ISP_LOGERR, "no parameters for 0x%x", opcode);
5565 		return;
5566 	}
5567 
5568 	/*
5569 	 * Get exclusive usage of mailbox registers.
5570 	 */
5571 	MBOX_ACQUIRE(isp);
5572 
5573 	for (box = 0; box < MAX_MAILBOX(isp); box++) {
5574 		if (ibits & (1 << box)) {
5575 			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
5576 		}
5577 		isp->isp_mboxtmp[box] = mbp->param[box] = 0;
5578 	}
5579 
5580 	isp->isp_lastmbxcmd = opcode;
5581 
5582 	/*
5583 	 * We assume that we can't overwrite a previous command.
5584 	 */
5585 	isp->isp_obits = obits;
5586 	isp->isp_mboxbsy = 1;
5587 
5588 	/*
5589 	 * Set Host Interrupt condition so that RISC will pick up mailbox regs.
5590 	 */
5591 	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
5592 
5593 	/*
5594 	 * While we haven't finished the command, spin our wheels here.
5595 	 */
5596 	MBOX_WAIT_COMPLETE(isp);
5597 
5598 	/*
5599 	 * Did the command time out?
5600 	 */
5601 	if (isp->isp_mboxbsy) {
5602 		isp->isp_mboxbsy = 0;
5603 		MBOX_RELEASE(isp);
5604 		return;
5605 	}
5606 
5607 	/*
5608 	 * Copy back output registers.
5609 	 */
5610 	for (box = 0; box < MAX_MAILBOX(isp); box++) {
5611 		if (obits & (1 << box)) {
5612 			mbp->param[box] = isp->isp_mboxtmp[box];
5613 		}
5614 	}
5615 
5616 	MBOX_RELEASE(isp);
5617 
5618 	if (logmask == 0 || opcode == MBOX_EXEC_FIRMWARE) {
5619 		return;
5620 	}
5621 #ifdef	ISP_STRIPPED
5622 	cname = NULL;
5623 #else
5624 	cname = (IS_FC(isp))? fc_mbcmd_names[opcode] : scsi_mbcmd_names[opcode];
5625 #endif
5626 	if (cname == NULL) {
5627 		cname = tname;
5628 		SNPRINTF(tname, sizeof tname, "opcode %x", opcode);
5629 	}
5630 
5631 	/*
5632 	 * Just to be chatty here...
5633 	 */
5634 	xname = NULL;
5635 	switch (mbp->param[0]) {
5636 	case MBOX_COMMAND_COMPLETE:
5637 		break;
5638 	case MBOX_INVALID_COMMAND:
5639 		if (logmask & MBLOGMASK(MBOX_COMMAND_COMPLETE)) {
5640 			xname = "INVALID COMMAND";
5641 		}
5642 		break;
5643 	case MBOX_HOST_INTERFACE_ERROR:
5644 		if (logmask & MBLOGMASK(MBOX_HOST_INTERFACE_ERROR)) {
5645 			xname = "HOST INTERFACE ERROR";
5646 		}
5647 		break;
5648 	case MBOX_TEST_FAILED:
5649 		if (logmask & MBLOGMASK(MBOX_TEST_FAILED)) {
5650 			xname = "TEST FAILED";
5651 		}
5652 		break;
5653 	case MBOX_COMMAND_ERROR:
5654 		if (logmask & MBLOGMASK(MBOX_COMMAND_ERROR)) {
5655 			xname = "COMMAND ERROR";
5656 		}
5657 		break;
5658 	case MBOX_COMMAND_PARAM_ERROR:
5659 		if (logmask & MBLOGMASK(MBOX_COMMAND_PARAM_ERROR)) {
5660 			xname = "COMMAND PARAMETER ERROR";
5661 		}
5662 		break;
5663 	case MBOX_LOOP_ID_USED:
5664 		if (logmask & MBLOGMASK(MBOX_LOOP_ID_USED)) {
5665 			xname = "LOOP ID ALREADY IN USE";
5666 		}
5667 		break;
5668 	case MBOX_PORT_ID_USED:
5669 		if (logmask & MBLOGMASK(MBOX_PORT_ID_USED)) {
5670 			xname = "PORT ID ALREADY IN USE";
5671 		}
5672 		break;
5673 	case MBOX_ALL_IDS_USED:
5674 		if (logmask & MBLOGMASK(MBOX_ALL_IDS_USED)) {
5675 			xname = "ALL LOOP IDS IN USE";
5676 		}
5677 		break;
5678 	case 0:		/* special case */
5679 		xname = "TIMEOUT";
5680 		break;
5681 	default:
5682 		SNPRINTF(mname, sizeof mname, "error 0x%x", mbp->param[0]);
5683 		xname = mname;
5684 		break;
5685 	}
5686 	if (xname) {
5687 		isp_prt(isp, ISP_LOGALL, "Mailbox Command '%s' failed (%s)",
5688 		    cname, xname);
5689 	}
5690 }
5691 
5692 static void
5693 isp_fw_state(ispsoftc_t *isp)
5694 {
5695 	if (IS_FC(isp)) {
5696 		mbreg_t mbs;
5697 		fcparam *fcp = isp->isp_param;
5698 
5699 		MEMZERO(&mbs, sizeof (mbs));
5700 		mbs.param[0] = MBOX_GET_FW_STATE;
5701 		isp_mboxcmd(isp, &mbs, MBLOGALL);
5702 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
5703 			fcp->isp_fwstate = mbs.param[1];
5704 		}
5705 	}
5706 }
5707 
5708 static void
5709 isp_update(ispsoftc_t *isp)
5710 {
5711 	int bus, upmask;
5712 
5713 	for (bus = 0, upmask = isp->isp_update; upmask != 0; bus++) {
5714 		if (upmask & (1 << bus)) {
5715 			isp_update_bus(isp, bus);
5716 		}
5717 		upmask &= ~(1 << bus);
5718 	}
5719 }
5720 
5721 static void
5722 isp_update_bus(ispsoftc_t *isp, int bus)
5723 {
5724 	int tgt;
5725 	mbreg_t mbs;
5726 	sdparam *sdp;
5727 
5728 	isp->isp_update &= ~(1 << bus);
5729 	if (IS_FC(isp)) {
5730 		/*
5731 		 * There are no 'per-bus' settings for Fibre Channel.
5732 		 */
5733 		return;
5734 	}
5735 	sdp = isp->isp_param;
5736 	sdp += bus;
5737 	MEMZERO(&mbs, sizeof (mbs));
5738 
5739 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
5740 		uint16_t flags, period, offset;
5741 		int get;
5742 
5743 		if (sdp->isp_devparam[tgt].dev_enable == 0) {
5744 			sdp->isp_devparam[tgt].dev_update = 0;
5745 			sdp->isp_devparam[tgt].dev_refresh = 0;
5746 			isp_prt(isp, ISP_LOGDEBUG0,
5747 	 		    "skipping target %d bus %d update", tgt, bus);
5748 			continue;
5749 		}
5750 		/*
5751 		 * If the goal is to update the status of the device,
5752 		 * take what's in goal_flags and try and set the device
5753 		 * toward that. Otherwise, if we're just refreshing the
5754 		 * current device state, get the current parameters.
5755 		 */
5756 
5757 		/*
5758 		 * Refresh overrides set
5759 		 */
5760 		if (sdp->isp_devparam[tgt].dev_refresh) {
5761 			mbs.param[0] = MBOX_GET_TARGET_PARAMS;
5762 			sdp->isp_devparam[tgt].dev_refresh = 0;
5763 			get = 1;
5764 		} else if (sdp->isp_devparam[tgt].dev_update) {
5765 			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
5766 			/*
5767 			 * Make sure goal_flags has "Renegotiate on Error"
5768 			 * on and "Freeze Queue on Error" off.
5769 			 */
5770 			sdp->isp_devparam[tgt].goal_flags |= DPARM_RENEG;
5771 			sdp->isp_devparam[tgt].goal_flags &= ~DPARM_QFRZ;
5772 
5773 			mbs.param[2] = sdp->isp_devparam[tgt].goal_flags;
5774 
5775 			/*
5776 			 * Insist that PARITY must be enabled
5777 			 * if SYNC or WIDE is enabled.
5778 			 */
5779 			if ((mbs.param[2] & (DPARM_SYNC|DPARM_WIDE)) != 0) {
5780 				mbs.param[2] |= DPARM_PARITY;
5781 			}
5782 
5783 			if ((mbs.param[2] & DPARM_SYNC) == 0) {
5784 				mbs.param[3] = 0;
5785 			} else {
5786 				mbs.param[3] =
5787 				    (sdp->isp_devparam[tgt].goal_offset << 8) |
5788 				    (sdp->isp_devparam[tgt].goal_period);
5789 			}
5790 			/*
5791 			 * A command completion later that has
5792 			 * RQSTF_NEGOTIATION set can cause
5793 			 * the dev_refresh/announce cycle also.
5794 			 *
5795 			 * Note: It is really important to update our current
5796 			 * flags with at least the state of TAG capabilities-
5797 			 * otherwise we might try and send a tagged command
5798 			 * when we have it all turned off. So change it here
5799 			 * to say that current already matches goal.
5800 			 */
5801 			sdp->isp_devparam[tgt].actv_flags &= ~DPARM_TQING;
5802 			sdp->isp_devparam[tgt].actv_flags |=
5803 			    (sdp->isp_devparam[tgt].goal_flags & DPARM_TQING);
5804 			isp_prt(isp, ISP_LOGDEBUG0,
5805 			    "bus %d set tgt %d flags 0x%x off 0x%x period 0x%x",
5806 			    bus, tgt, mbs.param[2], mbs.param[3] >> 8,
5807 			    mbs.param[3] & 0xff);
5808 			sdp->isp_devparam[tgt].dev_update = 0;
5809 			sdp->isp_devparam[tgt].dev_refresh = 1;
5810 			get = 0;
5811 		} else {
5812 			continue;
5813 		}
5814 		mbs.param[1] = (bus << 15) | (tgt << 8);
5815 		isp_mboxcmd(isp, &mbs, MBLOGALL);
5816 		if (get == 0) {
5817 			isp->isp_sendmarker |= (1 << bus);
5818 			continue;
5819 		}
5820 		flags = mbs.param[2];
5821 		period = mbs.param[3] & 0xff;
5822 		offset = mbs.param[3] >> 8;
5823 		sdp->isp_devparam[tgt].actv_flags = flags;
5824 		sdp->isp_devparam[tgt].actv_period = period;
5825 		sdp->isp_devparam[tgt].actv_offset = offset;
5826 		get = (bus << 16) | tgt;
5827 		(void) isp_async(isp, ISPASYNC_NEW_TGT_PARAMS, &get);
5828 	}
5829 
5830 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
5831 		if (sdp->isp_devparam[tgt].dev_update ||
5832 		    sdp->isp_devparam[tgt].dev_refresh) {
5833 			isp->isp_update |= (1 << bus);
5834 			break;
5835 		}
5836 	}
5837 }
5838 
5839 #ifndef	DEFAULT_FRAMESIZE
5840 #define	DEFAULT_FRAMESIZE(isp)		ICB_DFLT_FRMLEN
5841 #endif
5842 #ifndef	DEFAULT_EXEC_THROTTLE
5843 #define	DEFAULT_EXEC_THROTTLE(isp)	ISP_EXEC_THROTTLE
5844 #endif
5845 
5846 static void
5847 isp_setdfltparm(ispsoftc_t *isp, int channel)
5848 {
5849 	int tgt;
5850 	mbreg_t mbs;
5851 	sdparam *sdp;
5852 
5853 	MEMZERO(&mbs, sizeof (mbs));
5854 	if (IS_FC(isp)) {
5855 		fcparam *fcp = (fcparam *) isp->isp_param;
5856 		int nvfail;
5857 
5858 		fcp += channel;
5859 		if (fcp->isp_gotdparms) {
5860 			return;
5861 		}
5862 		fcp->isp_gotdparms = 1;
5863 		fcp->isp_maxfrmlen = DEFAULT_FRAMESIZE(isp);
5864 		fcp->isp_maxalloc = ICB_DFLT_ALLOC;
5865 		fcp->isp_execthrottle = DEFAULT_EXEC_THROTTLE(isp);
5866 		fcp->isp_retry_delay = ICB_DFLT_RDELAY;
5867 		fcp->isp_retry_count = ICB_DFLT_RCOUNT;
5868 		/* Platform specific.... */
5869 		fcp->isp_loopid = DEFAULT_LOOPID(isp);
5870 		fcp->isp_nodewwn = DEFAULT_NODEWWN(isp);
5871 		fcp->isp_portwwn = DEFAULT_PORTWWN(isp);
5872 		fcp->isp_fwoptions = 0;
5873 		fcp->isp_fwoptions |= ICBOPT_FAIRNESS;
5874 		fcp->isp_fwoptions |= ICBOPT_PDBCHANGE_AE;
5875 		fcp->isp_fwoptions |= ICBOPT_HARD_ADDRESS;
5876 		fcp->isp_fwoptions |= ICBOPT_FAST_POST;
5877 		if (isp->isp_confopts & ISP_CFG_FULL_DUPLEX)
5878 			fcp->isp_fwoptions |= ICBOPT_FULL_DUPLEX;
5879 
5880 		/*
5881 		 * Make sure this is turned off now until we get
5882 		 * extended options from NVRAM
5883 		 */
5884 		fcp->isp_fwoptions &= ~ICBOPT_EXTENDED;
5885 
5886 		/*
5887 		 * Now try and read NVRAM unless told to not do so.
5888 		 * This will set fcparam's isp_nodewwn && isp_portwwn.
5889 		 */
5890 		if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
5891 		    	nvfail = isp_read_nvram(isp);
5892 			if (nvfail) {
5893 				isp->isp_confopts |= ISP_CFG_NONVRAM;
5894 			}
5895 		} else {
5896 			nvfail = 1;
5897 		}
5898 		/*
5899 		 * Set node && port to override platform set defaults
5900 		 * unless the nvram read failed (or none was done),
5901 		 * or the platform code wants to use what had been
5902 		 * set in the defaults.
5903 		 */
5904 		if (nvfail) {
5905 			isp->isp_confopts |= ISP_CFG_OWNWWPN|ISP_CFG_OWNWWNN;
5906 		}
5907 		if (isp->isp_confopts & ISP_CFG_OWNWWNN) {
5908 			isp_prt(isp, ISP_LOGCONFIG, "Using Node WWN 0x%08x%08x",
5909 			    (uint32_t) (DEFAULT_NODEWWN(isp) >> 32),
5910 			    (uint32_t) (DEFAULT_NODEWWN(isp) & 0xffffffff));
5911 			ISP_NODEWWN(isp) = DEFAULT_NODEWWN(isp);
5912 		} else {
5913 			/*
5914 			 * We always start out with values derived
5915 			 * from NVRAM or our platform default.
5916 			 */
5917 			ISP_NODEWWN(isp) = fcp->isp_nodewwn;
5918 			if (fcp->isp_nodewwn == 0) {
5919 				isp_prt(isp, ISP_LOGCONFIG,
5920 				    "bad WWNN- using default\n");
5921 				ISP_NODEWWN(isp) = DEFAULT_NODEWWN(isp);
5922 			}
5923 		}
5924 		if (isp->isp_confopts & ISP_CFG_OWNWWPN) {
5925 			isp_prt(isp, ISP_LOGCONFIG, "Using Port WWN 0x%08x%08x",
5926 			    (uint32_t) (DEFAULT_PORTWWN(isp) >> 32),
5927 			    (uint32_t) (DEFAULT_PORTWWN(isp) & 0xffffffff));
5928 			ISP_PORTWWN(isp) = DEFAULT_PORTWWN(isp);
5929 		} else {
5930 			/*
5931 			 * We always start out with values derived
5932 			 * from NVRAM or our platform default.
5933 			 */
5934 			ISP_PORTWWN(isp) = fcp->isp_portwwn;
5935 			if (fcp->isp_portwwn == 0) {
5936 				isp_prt(isp, ISP_LOGCONFIG,
5937 				    "bad WWPN- using default\n");
5938 				ISP_PORTWWN(isp) = DEFAULT_PORTWWN(isp);
5939 			}
5940 		}
5941 		return;
5942 	}
5943 
5944 	sdp = (sdparam *) isp->isp_param;
5945 	sdp += channel;
5946 
5947 	/*
5948 	 * Been there, done that, got the T-shirt...
5949 	 */
5950 	if (sdp->isp_gotdparms) {
5951 		return;
5952 	}
5953 	sdp->isp_gotdparms = 1;
5954 
5955 	/*
5956 	 * Establish some default parameters.
5957 	 */
5958 	sdp->isp_cmd_dma_burst_enable = 0;
5959 	sdp->isp_data_dma_burst_enabl = 1;
5960 	sdp->isp_fifo_threshold = 0;
5961 	sdp->isp_initiator_id = DEFAULT_IID(isp);
5962 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
5963 		sdp->isp_async_data_setup = 9;
5964 	} else {
5965 		sdp->isp_async_data_setup = 6;
5966 	}
5967 	sdp->isp_selection_timeout = 250;
5968 	sdp->isp_max_queue_depth = MAXISPREQUEST(isp);
5969 	sdp->isp_tag_aging = 8;
5970 	sdp->isp_bus_reset_delay = 5;
5971 	/*
5972 	 * Don't retry selection, busy or queue full automatically- reflect
5973 	 * these back to us.
5974 	 */
5975 	sdp->isp_retry_count = 0;
5976 	sdp->isp_retry_delay = 0;
5977 
5978 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
5979 		sdp->isp_devparam[tgt].exc_throttle = ISP_EXEC_THROTTLE;
5980 		sdp->isp_devparam[tgt].dev_enable = 1;
5981 	}
5982 
5983 	/*
5984 	 * If we've not been told to avoid reading NVRAM, try and read it.
5985 	 * If we're successful reading it, we can then return because NVRAM
5986 	 * will tell us what the desired settings are. Otherwise, we establish
5987 	 * some reasonable 'fake' nvram and goal defaults.
5988 	 */
5989 
5990 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
5991 		if (isp_read_nvram(isp) == 0) {
5992 			return;
5993 		}
5994 	}
5995 
5996 	/*
5997 	 * Now try and see whether we have specific values for them.
5998 	 */
5999 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
6000 		mbs.param[0] = MBOX_GET_ACT_NEG_STATE;
6001 		isp_mboxcmd(isp, &mbs, MBLOGNONE);
6002 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
6003 			sdp->isp_req_ack_active_neg = 1;
6004 			sdp->isp_data_line_active_neg = 1;
6005 		} else {
6006 			sdp->isp_req_ack_active_neg =
6007 			    (mbs.param[1+channel] >> 4) & 0x1;
6008 			sdp->isp_data_line_active_neg =
6009 			    (mbs.param[1+channel] >> 5) & 0x1;
6010 		}
6011 	}
6012 
6013 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc3,
6014 	    0, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
6015 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
6016 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
6017 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc3,
6018 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
6019 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
6020 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
6021 
6022 	/*
6023 	 * The trick here is to establish a default for the default (honk!)
6024 	 * state (goal_flags). Then try and get the current status from
6025 	 * the card to fill in the current state. We don't, in fact, set
6026 	 * the default to the SAFE default state- that's not the goal state.
6027 	 */
6028 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
6029 		uint8_t off, per;
6030 		sdp->isp_devparam[tgt].actv_offset = 0;
6031 		sdp->isp_devparam[tgt].actv_period = 0;
6032 		sdp->isp_devparam[tgt].actv_flags = 0;
6033 
6034 		sdp->isp_devparam[tgt].goal_flags =
6035 		    sdp->isp_devparam[tgt].nvrm_flags = DPARM_DEFAULT;
6036 
6037 		/*
6038 		 * We default to Wide/Fast for versions less than a 1040
6039 		 * (unless it's SBus).
6040 		 */
6041 		if (IS_ULTRA3(isp)) {
6042 			off = ISP_80M_SYNCPARMS >> 8;
6043 			per = ISP_80M_SYNCPARMS & 0xff;
6044 		} else if (IS_ULTRA2(isp)) {
6045 			off = ISP_40M_SYNCPARMS >> 8;
6046 			per = ISP_40M_SYNCPARMS & 0xff;
6047 		} else if (IS_1240(isp)) {
6048 			off = ISP_20M_SYNCPARMS >> 8;
6049 			per = ISP_20M_SYNCPARMS & 0xff;
6050 		} else if ((isp->isp_bustype == ISP_BT_SBUS &&
6051 		    isp->isp_type < ISP_HA_SCSI_1020A) ||
6052 		    (isp->isp_bustype == ISP_BT_PCI &&
6053 		    isp->isp_type < ISP_HA_SCSI_1040) ||
6054 		    (isp->isp_clock && isp->isp_clock < 60) ||
6055 		    (sdp->isp_ultramode == 0)) {
6056 			off = ISP_10M_SYNCPARMS >> 8;
6057 			per = ISP_10M_SYNCPARMS & 0xff;
6058 		} else {
6059 			off = ISP_20M_SYNCPARMS_1040 >> 8;
6060 			per = ISP_20M_SYNCPARMS_1040 & 0xff;
6061 		}
6062 		sdp->isp_devparam[tgt].goal_offset =
6063 		    sdp->isp_devparam[tgt].nvrm_offset = off;
6064 		sdp->isp_devparam[tgt].goal_period =
6065 		    sdp->isp_devparam[tgt].nvrm_period = per;
6066 
6067 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc3,
6068 		    channel, tgt, sdp->isp_devparam[tgt].nvrm_flags,
6069 		    sdp->isp_devparam[tgt].nvrm_offset,
6070 		    sdp->isp_devparam[tgt].nvrm_period);
6071 	}
6072 }
6073 
6074 /*
6075  * Re-initialize the ISP and complete all orphaned commands
6076  * with a 'botched' notice. The reset/init routines should
6077  * not disturb an already active list of commands.
6078  *
6079  * Locks held prior to coming here.
6080  */
6081 
6082 void
6083 isp_reinit(ispsoftc_t *isp)
6084 {
6085 	XS_T *xs;
6086 	int i;
6087 
6088 	if (IS_FC(isp)) {
6089 		isp_mark_getpdb_all(isp);
6090 	}
6091 	isp_reset(isp);
6092 	if (isp->isp_state != ISP_RESETSTATE) {
6093 		isp_prt(isp, ISP_LOGERR, "isp_reinit cannot reset card");
6094 	} else if (isp->isp_role != ISP_ROLE_NONE) {
6095 		isp_init(isp);
6096 		if (isp->isp_state == ISP_INITSTATE) {
6097 			isp->isp_state = ISP_RUNSTATE;
6098 		}
6099 		if (isp->isp_state != ISP_RUNSTATE) {
6100 			isp_prt(isp, ISP_LOGERR,
6101 			    "isp_reinit cannot restart card");
6102 		}
6103 	}
6104 	isp->isp_nactive = 0;
6105 
6106 	for (i = 0; i < isp->isp_maxcmds; i++) {
6107 		uint16_t handle;
6108 		xs = isp->isp_xflist[i];
6109 		if (xs == NULL) {
6110 			continue;
6111 		}
6112 		handle = isp_index_handle(i);
6113 		isp_destroy_handle(isp, handle);
6114 		if (XS_XFRLEN(xs)) {
6115 			ISP_DMAFREE(isp, xs, handle);
6116 			XS_RESID(xs) = XS_XFRLEN(xs);
6117 		} else {
6118 			XS_RESID(xs) = 0;
6119 		}
6120 		XS_SETERR(xs, HBA_BUSRESET);
6121 		isp_done(xs);
6122 	}
6123 }
6124 
6125 /*
6126  * NVRAM Routines
6127  */
6128 static int
6129 isp_read_nvram(ispsoftc_t *isp)
6130 {
6131 	int i, amt, retval;
6132 	uint8_t csum, minversion;
6133 	union {
6134 		uint8_t _x[ISP2100_NVRAM_SIZE];
6135 		uint16_t _s[ISP2100_NVRAM_SIZE>>1];
6136 	} _n;
6137 #define	nvram_data	_n._x
6138 #define	nvram_words	_n._s
6139 
6140 	if (IS_FC(isp)) {
6141 		amt = ISP2100_NVRAM_SIZE;
6142 		minversion = 1;
6143 	} else if (IS_ULTRA2(isp)) {
6144 		amt = ISP1080_NVRAM_SIZE;
6145 		minversion = 0;
6146 	} else {
6147 		amt = ISP_NVRAM_SIZE;
6148 		minversion = 2;
6149 	}
6150 
6151 	/*
6152 	 * Just read the first two words first to see if we have a valid
6153 	 * NVRAM to continue reading the rest with.
6154 	 */
6155 	for (i = 0; i < 2; i++) {
6156 		isp_rdnvram_word(isp, i, &nvram_words[i]);
6157 	}
6158 	if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
6159 	    nvram_data[2] != 'P') {
6160 		if (isp->isp_bustype != ISP_BT_SBUS) {
6161 			isp_prt(isp, ISP_LOGWARN, "invalid NVRAM header");
6162 			isp_prt(isp, ISP_LOGDEBUG0, "%x %x %x",
6163 			    nvram_data[0], nvram_data[1], nvram_data[2]);
6164 		}
6165 		retval = -1;
6166 		goto out;
6167 	}
6168 	for (i = 2; i < amt>>1; i++) {
6169 		isp_rdnvram_word(isp, i, &nvram_words[i]);
6170 	}
6171 	for (csum = 0, i = 0; i < amt; i++) {
6172 		csum += nvram_data[i];
6173 	}
6174 	if (csum != 0) {
6175 		isp_prt(isp, ISP_LOGWARN, "invalid NVRAM checksum");
6176 		retval = -1;
6177 		goto out;
6178 	}
6179 	if (ISP_NVRAM_VERSION(nvram_data) < minversion) {
6180 		isp_prt(isp, ISP_LOGWARN, "version %d NVRAM not understood",
6181 		    ISP_NVRAM_VERSION(nvram_data));
6182 		retval = -1;
6183 		goto out;
6184 	}
6185 
6186 	if (IS_ULTRA3(isp)) {
6187 		isp_parse_nvram_12160(isp, 0, nvram_data);
6188 		if (IS_12160(isp))
6189 			isp_parse_nvram_12160(isp, 1, nvram_data);
6190 	} else if (IS_1080(isp)) {
6191 		isp_parse_nvram_1080(isp, 0, nvram_data);
6192 	} else if (IS_1280(isp) || IS_1240(isp)) {
6193 		isp_parse_nvram_1080(isp, 0, nvram_data);
6194 		isp_parse_nvram_1080(isp, 1, nvram_data);
6195 	} else if (IS_SCSI(isp)) {
6196 		isp_parse_nvram_1020(isp, nvram_data);
6197 	} else {
6198 		isp_parse_nvram_2100(isp, nvram_data);
6199 	}
6200 	retval = 0;
6201 out:
6202 	return (retval);
6203 #undef	nvram_data
6204 #undef	nvram_words
6205 }
6206 
6207 static void
6208 isp_rdnvram_word(ispsoftc_t *isp, int wo, uint16_t *rp)
6209 {
6210 	int i, cbits;
6211 	uint16_t bit, rqst, junk;
6212 
6213 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
6214 	USEC_DELAY(10);
6215 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
6216 	USEC_DELAY(10);
6217 
6218 	if (IS_FC(isp)) {
6219 		wo &= ((ISP2100_NVRAM_SIZE >> 1) - 1);
6220 		if (IS_2312(isp) && isp->isp_port) {
6221 			wo += 128;
6222 		}
6223 		rqst = (ISP_NVRAM_READ << 8) | wo;
6224 		cbits = 10;
6225 	} else if (IS_ULTRA2(isp)) {
6226 		wo &= ((ISP1080_NVRAM_SIZE >> 1) - 1);
6227 		rqst = (ISP_NVRAM_READ << 8) | wo;
6228 		cbits = 10;
6229 	} else {
6230 		wo &= ((ISP_NVRAM_SIZE >> 1) - 1);
6231 		rqst = (ISP_NVRAM_READ << 6) | wo;
6232 		cbits = 8;
6233 	}
6234 
6235 	/*
6236 	 * Clock the word select request out...
6237 	 */
6238 	for (i = cbits; i >= 0; i--) {
6239 		if ((rqst >> i) & 1) {
6240 			bit = BIU_NVRAM_SELECT | BIU_NVRAM_DATAOUT;
6241 		} else {
6242 			bit = BIU_NVRAM_SELECT;
6243 		}
6244 		ISP_WRITE(isp, BIU_NVRAM, bit);
6245 		USEC_DELAY(10);
6246 		junk = ISP_READ(isp, BIU_NVRAM);	/* force PCI flush */
6247 		ISP_WRITE(isp, BIU_NVRAM, bit | BIU_NVRAM_CLOCK);
6248 		USEC_DELAY(10);
6249 		junk = ISP_READ(isp, BIU_NVRAM);	/* force PCI flush */
6250 		ISP_WRITE(isp, BIU_NVRAM, bit);
6251 		USEC_DELAY(10);
6252 		junk = ISP_READ(isp, BIU_NVRAM);	/* force PCI flush */
6253 	}
6254 	/*
6255 	 * Now read the result back in (bits come back in MSB format).
6256 	 */
6257 	*rp = 0;
6258 	for (i = 0; i < 16; i++) {
6259 		uint16_t rv;
6260 		*rp <<= 1;
6261 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
6262 		USEC_DELAY(10);
6263 		rv = ISP_READ(isp, BIU_NVRAM);
6264 		if (rv & BIU_NVRAM_DATAIN) {
6265 			*rp |= 1;
6266 		}
6267 		USEC_DELAY(10);
6268 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
6269 		USEC_DELAY(10);
6270 		junk = ISP_READ(isp, BIU_NVRAM);	/* force PCI flush */
6271 	}
6272 	ISP_WRITE(isp, BIU_NVRAM, 0);
6273 	USEC_DELAY(10);
6274 	junk = ISP_READ(isp, BIU_NVRAM);	/* force PCI flush */
6275 	ISP_SWIZZLE_NVRAM_WORD(isp, rp);
6276 }
6277 
6278 static void
6279 isp_parse_nvram_1020(ispsoftc_t *isp, uint8_t *nvram_data)
6280 {
6281 	sdparam *sdp = (sdparam *) isp->isp_param;
6282 	int tgt;
6283 
6284 	sdp->isp_fifo_threshold =
6285 		ISP_NVRAM_FIFO_THRESHOLD(nvram_data) |
6286 		(ISP_NVRAM_FIFO_THRESHOLD_128(nvram_data) << 2);
6287 
6288 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
6289 		sdp->isp_initiator_id =
6290 			ISP_NVRAM_INITIATOR_ID(nvram_data);
6291 
6292 	sdp->isp_bus_reset_delay =
6293 		ISP_NVRAM_BUS_RESET_DELAY(nvram_data);
6294 
6295 	sdp->isp_retry_count =
6296 		ISP_NVRAM_BUS_RETRY_COUNT(nvram_data);
6297 
6298 	sdp->isp_retry_delay =
6299 		ISP_NVRAM_BUS_RETRY_DELAY(nvram_data);
6300 
6301 	sdp->isp_async_data_setup =
6302 		ISP_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data);
6303 
6304 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
6305 		if (sdp->isp_async_data_setup < 9) {
6306 			sdp->isp_async_data_setup = 9;
6307 		}
6308 	} else {
6309 		if (sdp->isp_async_data_setup != 6) {
6310 			sdp->isp_async_data_setup = 6;
6311 		}
6312 	}
6313 
6314 	sdp->isp_req_ack_active_neg =
6315 		ISP_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data);
6316 
6317 	sdp->isp_data_line_active_neg =
6318 		ISP_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data);
6319 
6320 	sdp->isp_data_dma_burst_enabl =
6321 		ISP_NVRAM_DATA_DMA_BURST_ENABLE(nvram_data);
6322 
6323 	sdp->isp_cmd_dma_burst_enable =
6324 		ISP_NVRAM_CMD_DMA_BURST_ENABLE(nvram_data);
6325 
6326 	sdp->isp_tag_aging =
6327 		ISP_NVRAM_TAG_AGE_LIMIT(nvram_data);
6328 
6329 	sdp->isp_selection_timeout =
6330 		ISP_NVRAM_SELECTION_TIMEOUT(nvram_data);
6331 
6332 	sdp->isp_max_queue_depth =
6333 		ISP_NVRAM_MAX_QUEUE_DEPTH(nvram_data);
6334 
6335 	sdp->isp_fast_mttr = ISP_NVRAM_FAST_MTTR_ENABLE(nvram_data);
6336 
6337 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
6338 	    0, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
6339 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
6340 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
6341 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
6342 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
6343 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
6344 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
6345 
6346 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
6347 		sdp->isp_devparam[tgt].dev_enable =
6348 			ISP_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt);
6349 		sdp->isp_devparam[tgt].exc_throttle =
6350 			ISP_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt);
6351 		sdp->isp_devparam[tgt].nvrm_offset =
6352 			ISP_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt);
6353 		sdp->isp_devparam[tgt].nvrm_period =
6354 			ISP_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt);
6355 		/*
6356 		 * We probably shouldn't lie about this, but it
6357 		 * it makes it much safer if we limit NVRAM values
6358 		 * to sanity.
6359 		 */
6360 		if (isp->isp_type < ISP_HA_SCSI_1040) {
6361 			/*
6362 			 * If we're not ultra, we can't possibly
6363 			 * be a shorter period than this.
6364 			 */
6365 			if (sdp->isp_devparam[tgt].nvrm_period < 0x19) {
6366 				sdp->isp_devparam[tgt].nvrm_period = 0x19;
6367 			}
6368 			if (sdp->isp_devparam[tgt].nvrm_offset > 0xc) {
6369 				sdp->isp_devparam[tgt].nvrm_offset = 0x0c;
6370 			}
6371 		} else {
6372 			if (sdp->isp_devparam[tgt].nvrm_offset > 0x8) {
6373 				sdp->isp_devparam[tgt].nvrm_offset = 0x8;
6374 			}
6375 		}
6376 		sdp->isp_devparam[tgt].nvrm_flags = 0;
6377 		if (ISP_NVRAM_TGT_RENEG(nvram_data, tgt))
6378 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
6379 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
6380 		if (ISP_NVRAM_TGT_TQING(nvram_data, tgt))
6381 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
6382 		if (ISP_NVRAM_TGT_SYNC(nvram_data, tgt))
6383 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
6384 		if (ISP_NVRAM_TGT_WIDE(nvram_data, tgt))
6385 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
6386 		if (ISP_NVRAM_TGT_PARITY(nvram_data, tgt))
6387 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
6388 		if (ISP_NVRAM_TGT_DISC(nvram_data, tgt))
6389 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
6390 		sdp->isp_devparam[tgt].actv_flags = 0; /* we don't know */
6391 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
6392 		    0, tgt, sdp->isp_devparam[tgt].nvrm_flags,
6393 		    sdp->isp_devparam[tgt].nvrm_offset,
6394 		    sdp->isp_devparam[tgt].nvrm_period);
6395 		sdp->isp_devparam[tgt].goal_offset =
6396 		    sdp->isp_devparam[tgt].nvrm_offset;
6397 		sdp->isp_devparam[tgt].goal_period =
6398 		    sdp->isp_devparam[tgt].nvrm_period;
6399 		sdp->isp_devparam[tgt].goal_flags =
6400 		    sdp->isp_devparam[tgt].nvrm_flags;
6401 	}
6402 }
6403 
6404 static void
6405 isp_parse_nvram_1080(ispsoftc_t *isp, int bus, uint8_t *nvram_data)
6406 {
6407 	sdparam *sdp = (sdparam *) isp->isp_param;
6408 	int tgt;
6409 
6410 	sdp += bus;
6411 
6412 	sdp->isp_fifo_threshold =
6413 	    ISP1080_NVRAM_FIFO_THRESHOLD(nvram_data);
6414 
6415 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
6416 		sdp->isp_initiator_id =
6417 		    ISP1080_NVRAM_INITIATOR_ID(nvram_data, bus);
6418 
6419 	sdp->isp_bus_reset_delay =
6420 	    ISP1080_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
6421 
6422 	sdp->isp_retry_count =
6423 	    ISP1080_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
6424 
6425 	sdp->isp_retry_delay =
6426 	    ISP1080_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
6427 
6428 	sdp->isp_async_data_setup =
6429 	    ISP1080_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
6430 
6431 	sdp->isp_req_ack_active_neg =
6432 	    ISP1080_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
6433 
6434 	sdp->isp_data_line_active_neg =
6435 	    ISP1080_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
6436 
6437 	sdp->isp_data_dma_burst_enabl =
6438 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
6439 
6440 	sdp->isp_cmd_dma_burst_enable =
6441 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
6442 
6443 	sdp->isp_selection_timeout =
6444 	    ISP1080_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
6445 
6446 	sdp->isp_max_queue_depth =
6447 	     ISP1080_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
6448 
6449 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
6450 	    bus, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
6451 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
6452 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
6453 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
6454 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
6455 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
6456 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
6457 
6458 
6459 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
6460 		sdp->isp_devparam[tgt].dev_enable =
6461 		    ISP1080_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
6462 		sdp->isp_devparam[tgt].exc_throttle =
6463 			ISP1080_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
6464 		sdp->isp_devparam[tgt].nvrm_offset =
6465 			ISP1080_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
6466 		sdp->isp_devparam[tgt].nvrm_period =
6467 			ISP1080_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
6468 		sdp->isp_devparam[tgt].nvrm_flags = 0;
6469 		if (ISP1080_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
6470 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
6471 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
6472 		if (ISP1080_NVRAM_TGT_TQING(nvram_data, tgt, bus))
6473 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
6474 		if (ISP1080_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
6475 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
6476 		if (ISP1080_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
6477 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
6478 		if (ISP1080_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
6479 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
6480 		if (ISP1080_NVRAM_TGT_DISC(nvram_data, tgt, bus))
6481 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
6482 		sdp->isp_devparam[tgt].actv_flags = 0;
6483 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
6484 		    bus, tgt, sdp->isp_devparam[tgt].nvrm_flags,
6485 		    sdp->isp_devparam[tgt].nvrm_offset,
6486 		    sdp->isp_devparam[tgt].nvrm_period);
6487 		sdp->isp_devparam[tgt].goal_offset =
6488 		    sdp->isp_devparam[tgt].nvrm_offset;
6489 		sdp->isp_devparam[tgt].goal_period =
6490 		    sdp->isp_devparam[tgt].nvrm_period;
6491 		sdp->isp_devparam[tgt].goal_flags =
6492 		    sdp->isp_devparam[tgt].nvrm_flags;
6493 	}
6494 }
6495 
6496 static void
6497 isp_parse_nvram_12160(ispsoftc_t *isp, int bus, uint8_t *nvram_data)
6498 {
6499 	sdparam *sdp = (sdparam *) isp->isp_param;
6500 	int tgt;
6501 
6502 	sdp += bus;
6503 
6504 	sdp->isp_fifo_threshold =
6505 	    ISP12160_NVRAM_FIFO_THRESHOLD(nvram_data);
6506 
6507 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
6508 		sdp->isp_initiator_id =
6509 		    ISP12160_NVRAM_INITIATOR_ID(nvram_data, bus);
6510 
6511 	sdp->isp_bus_reset_delay =
6512 	    ISP12160_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
6513 
6514 	sdp->isp_retry_count =
6515 	    ISP12160_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
6516 
6517 	sdp->isp_retry_delay =
6518 	    ISP12160_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
6519 
6520 	sdp->isp_async_data_setup =
6521 	    ISP12160_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
6522 
6523 	sdp->isp_req_ack_active_neg =
6524 	    ISP12160_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
6525 
6526 	sdp->isp_data_line_active_neg =
6527 	    ISP12160_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
6528 
6529 	sdp->isp_data_dma_burst_enabl =
6530 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
6531 
6532 	sdp->isp_cmd_dma_burst_enable =
6533 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
6534 
6535 	sdp->isp_selection_timeout =
6536 	    ISP12160_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
6537 
6538 	sdp->isp_max_queue_depth =
6539 	     ISP12160_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
6540 
6541 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
6542 	    bus, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
6543 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
6544 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
6545 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
6546 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
6547 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
6548 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
6549 
6550 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
6551 		sdp->isp_devparam[tgt].dev_enable =
6552 		    ISP12160_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
6553 		sdp->isp_devparam[tgt].exc_throttle =
6554 			ISP12160_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
6555 		sdp->isp_devparam[tgt].nvrm_offset =
6556 			ISP12160_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
6557 		sdp->isp_devparam[tgt].nvrm_period =
6558 			ISP12160_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
6559 		sdp->isp_devparam[tgt].nvrm_flags = 0;
6560 		if (ISP12160_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
6561 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
6562 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
6563 		if (ISP12160_NVRAM_TGT_TQING(nvram_data, tgt, bus))
6564 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
6565 		if (ISP12160_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
6566 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
6567 		if (ISP12160_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
6568 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
6569 		if (ISP12160_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
6570 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
6571 		if (ISP12160_NVRAM_TGT_DISC(nvram_data, tgt, bus))
6572 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
6573 		sdp->isp_devparam[tgt].actv_flags = 0;
6574 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
6575 		    bus, tgt, sdp->isp_devparam[tgt].nvrm_flags,
6576 		    sdp->isp_devparam[tgt].nvrm_offset,
6577 		    sdp->isp_devparam[tgt].nvrm_period);
6578 		sdp->isp_devparam[tgt].goal_offset =
6579 		    sdp->isp_devparam[tgt].nvrm_offset;
6580 		sdp->isp_devparam[tgt].goal_period =
6581 		    sdp->isp_devparam[tgt].nvrm_period;
6582 		sdp->isp_devparam[tgt].goal_flags =
6583 		    sdp->isp_devparam[tgt].nvrm_flags;
6584 	}
6585 }
6586 
6587 static void
6588 isp_parse_nvram_2100(ispsoftc_t *isp, uint8_t *nvram_data)
6589 {
6590 	fcparam *fcp = (fcparam *) isp->isp_param;
6591 	uint64_t wwn;
6592 
6593 	/*
6594 	 * There is NVRAM storage for both Port and Node entities-
6595 	 * but the Node entity appears to be unused on all the cards
6596 	 * I can find. However, we should account for this being set
6597 	 * at some point in the future.
6598 	 *
6599 	 * Qlogic WWNs have an NAA of 2, but usually nothing shows up in
6600 	 * bits 48..60. In the case of the 2202, it appears that they do
6601 	 * use bit 48 to distinguish between the two instances on the card.
6602 	 * The 2204, which I've never seen, *probably* extends this method.
6603 	 */
6604 	wwn = ISP2100_NVRAM_PORT_NAME(nvram_data);
6605 	if (wwn) {
6606 		isp_prt(isp, ISP_LOGCONFIG, "NVRAM Port WWN 0x%08x%08x",
6607 		    (uint32_t) (wwn >> 32), (uint32_t) (wwn & 0xffffffff));
6608 		if ((wwn >> 60) == 0) {
6609 			wwn |= (((uint64_t) 2)<< 60);
6610 		}
6611 	}
6612 	fcp->isp_portwwn = wwn;
6613 	if (IS_2200(isp) || IS_23XX(isp)) {
6614 		wwn = ISP2100_NVRAM_NODE_NAME(nvram_data);
6615 		if (wwn) {
6616 			isp_prt(isp, ISP_LOGCONFIG, "NVRAM Node WWN 0x%08x%08x",
6617 			    (uint32_t) (wwn >> 32),
6618 			    (uint32_t) (wwn & 0xffffffff));
6619 			if ((wwn >> 60) == 0) {
6620 				wwn |= (((uint64_t) 2)<< 60);
6621 			}
6622 		}
6623 	} else {
6624 		wwn &= ~((uint64_t) 0xfff << 48);
6625 	}
6626 	fcp->isp_nodewwn = wwn;
6627 
6628 	/*
6629 	 * Make sure we have both Node and Port as non-zero values.
6630 	 */
6631 	if (fcp->isp_nodewwn != 0 && fcp->isp_portwwn == 0) {
6632 		fcp->isp_portwwn = fcp->isp_nodewwn;
6633 	} else if (fcp->isp_nodewwn == 0 && fcp->isp_portwwn != 0) {
6634 		fcp->isp_nodewwn = fcp->isp_portwwn;
6635 	}
6636 
6637 	/*
6638 	 * Make the Node and Port values sane if they're NAA == 2.
6639 	 * This means to clear bits 48..56 for the Node WWN and
6640 	 * make sure that there's some non-zero value in 48..56
6641 	 * for the Port WWN.
6642 	 */
6643 	if (fcp->isp_nodewwn && fcp->isp_portwwn) {
6644 		if ((fcp->isp_nodewwn & (((uint64_t) 0xfff) << 48)) != 0 &&
6645 		    (fcp->isp_nodewwn >> 60) == 2) {
6646 			fcp->isp_nodewwn &= ~((uint64_t) 0xfff << 48);
6647 		}
6648 		if ((fcp->isp_portwwn & (((uint64_t) 0xfff) << 48)) == 0 &&
6649 		    (fcp->isp_portwwn >> 60) == 2) {
6650 			fcp->isp_portwwn |= ((uint64_t) 1 << 56);
6651 		}
6652 	}
6653 
6654 	fcp->isp_maxalloc =
6655 		ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data);
6656 	if ((isp->isp_confopts & ISP_CFG_OWNFSZ) == 0)
6657 		fcp->isp_maxfrmlen =
6658 			ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data);
6659 	fcp->isp_retry_delay =
6660 		ISP2100_NVRAM_RETRY_DELAY(nvram_data);
6661 	fcp->isp_retry_count =
6662 		ISP2100_NVRAM_RETRY_COUNT(nvram_data);
6663 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
6664 		fcp->isp_loopid =
6665 			ISP2100_NVRAM_HARDLOOPID(nvram_data);
6666 	if ((isp->isp_confopts & ISP_CFG_OWNEXCTHROTTLE) == 0)
6667 		fcp->isp_execthrottle =
6668 			ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data);
6669 	fcp->isp_fwoptions = ISP2100_NVRAM_OPTIONS(nvram_data);
6670 	isp_prt(isp, ISP_LOGDEBUG0,
6671 	    "NVRAM 0x%08x%08x 0x%08x%08x maxalloc %d maxframelen %d",
6672 	    (uint32_t) (fcp->isp_nodewwn >> 32), (uint32_t) fcp->isp_nodewwn,
6673 	    (uint32_t) (fcp->isp_portwwn >> 32), (uint32_t) fcp->isp_portwwn,
6674 	    ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data),
6675 	    ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data));
6676 	isp_prt(isp, ISP_LOGDEBUG0,
6677 	    "execthrottle %d fwoptions 0x%x hardloop %d tov %d",
6678 	    ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data),
6679 	    ISP2100_NVRAM_OPTIONS(nvram_data),
6680 	    ISP2100_NVRAM_HARDLOOPID(nvram_data),
6681 	    ISP2100_NVRAM_TOV(nvram_data));
6682 	fcp->isp_xfwoptions = ISP2100_XFW_OPTIONS(nvram_data);
6683 	fcp->isp_zfwoptions = ISP2100_ZFW_OPTIONS(nvram_data);
6684 	isp_prt(isp, ISP_LOGDEBUG0,
6685 	    "xfwoptions 0x%x zfw options 0x%x",
6686 	    ISP2100_XFW_OPTIONS(nvram_data), ISP2100_ZFW_OPTIONS(nvram_data));
6687 }
6688 
6689 #ifdef	ISP_FW_CRASH_DUMP
6690 static void isp2200_fw_dump(ispsoftc_t *);
6691 static void isp2300_fw_dump(ispsoftc_t *);
6692 
6693 static void
6694 isp2200_fw_dump(ispsoftc_t *isp)
6695 {
6696 	int i, j;
6697 	mbreg_t mbs;
6698 	uint16_t *ptr;
6699 
6700 	MEMZERO(&mbs, sizeof (mbs));
6701 	ptr = FCPARAM(isp)->isp_dump_data;
6702 	if (ptr == NULL) {
6703 		isp_prt(isp, ISP_LOGERR,
6704 		   "No place to dump RISC registers and SRAM");
6705 		return;
6706 	}
6707 	if (*ptr++) {
6708 		isp_prt(isp, ISP_LOGERR,
6709 		   "dump area for RISC registers and SRAM already used");
6710 		return;
6711 	}
6712 	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
6713 	for (i = 0; i < 100; i++) {
6714 		USEC_DELAY(100);
6715 		if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6716 			break;
6717 		}
6718 	}
6719 	if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6720 		/*
6721 		 * PBIU Registers
6722 		 */
6723 		for (i = 0; i < 8; i++) {
6724 			*ptr++ = ISP_READ(isp, BIU_BLOCK + (i << 1));
6725 		}
6726 
6727 		/*
6728 		 * Mailbox Registers
6729 		 */
6730 		for (i = 0; i < 8; i++) {
6731 			*ptr++ = ISP_READ(isp, MBOX_BLOCK + (i << 1));
6732 		}
6733 
6734 		/*
6735 		 * DMA Registers
6736 		 */
6737 		for (i = 0; i < 48; i++) {
6738 			*ptr++ = ISP_READ(isp, DMA_BLOCK + 0x20 + (i << 1));
6739 		}
6740 
6741 		/*
6742 		 * RISC H/W Registers
6743 		 */
6744 		ISP_WRITE(isp, BIU2100_CSR, 0);
6745 		for (i = 0; i < 16; i++) {
6746 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0xA0 + (i << 1));
6747 		}
6748 
6749 		/*
6750 		 * RISC GP Registers
6751 		 */
6752 		for (j = 0; j < 8; j++) {
6753 			ISP_WRITE(isp, BIU_BLOCK + 0xA4, 0x2000 + (j << 8));
6754 			for (i = 0; i < 16; i++) {
6755 				*ptr++ =
6756 				    ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6757 			}
6758 		}
6759 
6760 		/*
6761 		 * Frame Buffer Hardware Registers
6762 		 */
6763 		ISP_WRITE(isp, BIU2100_CSR, 0x10);
6764 		for (i = 0; i < 16; i++) {
6765 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6766 		}
6767 
6768 		/*
6769 		 * Fibre Protocol Module 0 Hardware Registers
6770 		 */
6771 		ISP_WRITE(isp, BIU2100_CSR, 0x20);
6772 		for (i = 0; i < 64; i++) {
6773 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6774 		}
6775 
6776 		/*
6777 		 * Fibre Protocol Module 1 Hardware Registers
6778 		 */
6779 		ISP_WRITE(isp, BIU2100_CSR, 0x30);
6780 		for (i = 0; i < 64; i++) {
6781 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6782 		}
6783 	} else {
6784 		isp_prt(isp, ISP_LOGERR, "RISC Would Not Pause");
6785 		return;
6786 	}
6787 	isp_prt(isp, ISP_LOGALL,
6788 	   "isp_fw_dump: RISC registers dumped successfully");
6789 	ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
6790 	for (i = 0; i < 100; i++) {
6791 		USEC_DELAY(100);
6792 		if (ISP_READ(isp, OUTMAILBOX0) == 0) {
6793 			break;
6794 		}
6795 	}
6796 	if (ISP_READ(isp, OUTMAILBOX0) != 0) {
6797 		isp_prt(isp, ISP_LOGERR, "Board Would Not Reset");
6798 		return;
6799 	}
6800 	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
6801 	for (i = 0; i < 100; i++) {
6802 		USEC_DELAY(100);
6803 		if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6804 			break;
6805 		}
6806 	}
6807 	if ((ISP_READ(isp, HCCR) & HCCR_PAUSE) == 0) {
6808 		isp_prt(isp, ISP_LOGERR, "RISC Would Not Pause After Reset");
6809 		return;
6810 	}
6811 	ISP_WRITE(isp, RISC_EMB, 0xf2);
6812 	ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
6813 	for (i = 0; i < 100; i++) {
6814 		USEC_DELAY(100);
6815 		if ((ISP_READ(isp, HCCR) & HCCR_PAUSE) == 0) {
6816 			break;
6817 		}
6818 	}
6819 	ENABLE_INTS(isp);
6820 	mbs.param[0] = MBOX_READ_RAM_WORD;
6821 	mbs.param[1] = 0x1000;
6822 	isp->isp_mbxworkp = (void *) ptr;
6823 	isp->isp_mbxwrk0 = 0xefff;	/* continuation count */
6824 	isp->isp_mbxwrk1 = 0x1001;	/* next SRAM address */
6825 	isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
6826 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
6827 		isp_prt(isp, ISP_LOGWARN,
6828 		    "RAM DUMP FAILED @ WORD %x", isp->isp_mbxwrk1);
6829 		return;
6830 	}
6831 	ptr = isp->isp_mbxworkp;	/* finish fetch of final word */
6832 	*ptr++ = isp->isp_mboxtmp[2];
6833 	isp_prt(isp, ISP_LOGALL, "isp_fw_dump: SRAM dumped successfully");
6834 	FCPARAM(isp)->isp_dump_data[0] = isp->isp_type; /* now used */
6835 	(void) isp_async(isp, ISPASYNC_FW_DUMPED, 0);
6836 }
6837 
6838 static void
6839 isp2300_fw_dump(ispsoftc_t *isp)
6840 {
6841 	int i, j;
6842 	mbreg_t mbs;
6843 	uint16_t *ptr;
6844 
6845 	MEMZERO(&mbs, sizeof (mbs));
6846 	ptr = FCPARAM(isp)->isp_dump_data;
6847 	if (ptr == NULL) {
6848 		isp_prt(isp, ISP_LOGERR,
6849 		   "No place to dump RISC registers and SRAM");
6850 		return;
6851 	}
6852 	if (*ptr++) {
6853 		isp_prt(isp, ISP_LOGERR,
6854 		   "dump area for RISC registers and SRAM already used");
6855 		return;
6856 	}
6857 	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
6858 	for (i = 0; i < 100; i++) {
6859 		USEC_DELAY(100);
6860 		if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6861 			break;
6862 		}
6863 	}
6864 	if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6865 		/*
6866 		 * PBIU registers
6867 		 */
6868 		for (i = 0; i < 8; i++) {
6869 			*ptr++ = ISP_READ(isp, BIU_BLOCK + (i << 1));
6870 		}
6871 
6872 		/*
6873 		 * ReqQ-RspQ-Risc2Host Status registers
6874 		 */
6875 		for (i = 0; i < 8; i++) {
6876 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x10 + (i << 1));
6877 		}
6878 
6879 		/*
6880 		 * Mailbox Registers
6881 		 */
6882 		for (i = 0; i < 32; i++) {
6883 			*ptr++ =
6884 			    ISP_READ(isp, PCI_MBOX_REGS2300_OFF + (i << 1));
6885 		}
6886 
6887 		/*
6888 		 * Auto Request Response DMA registers
6889 		 */
6890 		ISP_WRITE(isp, BIU2100_CSR, 0x40);
6891 		for (i = 0; i < 32; i++) {
6892 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6893 		}
6894 
6895 		/*
6896 		 * DMA registers
6897 		 */
6898 		ISP_WRITE(isp, BIU2100_CSR, 0x50);
6899 		for (i = 0; i < 48; i++) {
6900 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6901 		}
6902 
6903 		/*
6904 		 * RISC hardware registers
6905 		 */
6906 		ISP_WRITE(isp, BIU2100_CSR, 0);
6907 		for (i = 0; i < 16; i++) {
6908 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0xA0 + (i << 1));
6909 		}
6910 
6911 		/*
6912 		 * RISC GP? registers
6913 		 */
6914 		for (j = 0; j < 8; j++) {
6915 			ISP_WRITE(isp, BIU_BLOCK + 0xA4, 0x2000 + (j << 9));
6916 			for (i = 0; i < 16; i++) {
6917 				*ptr++ =
6918 				    ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6919 			}
6920 		}
6921 
6922 		/*
6923 		 * frame buffer hardware registers
6924 		 */
6925 		ISP_WRITE(isp, BIU2100_CSR, 0x10);
6926 		for (i = 0; i < 64; i++) {
6927 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6928 		}
6929 
6930 		/*
6931 		 * FPM B0 hardware registers
6932 		 */
6933 		ISP_WRITE(isp, BIU2100_CSR, 0x20);
6934 		for (i = 0; i < 64; i++) {
6935 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6936 		}
6937 
6938 		/*
6939 		 * FPM B1 hardware registers
6940 		 */
6941 		ISP_WRITE(isp, BIU2100_CSR, 0x30);
6942 		for (i = 0; i < 64; i++) {
6943 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6944 		}
6945 	} else {
6946 		isp_prt(isp, ISP_LOGERR, "RISC Would Not Pause");
6947 		return;
6948 	}
6949 	isp_prt(isp, ISP_LOGALL,
6950 	   "isp_fw_dump: RISC registers dumped successfully");
6951 	ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
6952 	for (i = 0; i < 100; i++) {
6953 		USEC_DELAY(100);
6954 		if (ISP_READ(isp, OUTMAILBOX0) == 0) {
6955 			break;
6956 		}
6957 	}
6958 	if (ISP_READ(isp, OUTMAILBOX0) != 0) {
6959 		isp_prt(isp, ISP_LOGERR, "Board Would Not Reset");
6960 		return;
6961 	}
6962 	ENABLE_INTS(isp);
6963 	mbs.param[0] = MBOX_READ_RAM_WORD;
6964 	mbs.param[1] = 0x800;
6965 	isp->isp_mbxworkp = (void *) ptr;
6966 	isp->isp_mbxwrk0 = 0xf7ff;	/* continuation count */
6967 	isp->isp_mbxwrk1 = 0x801;	/* next SRAM address */
6968 	isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
6969 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
6970 		isp_prt(isp, ISP_LOGWARN,
6971 		    "RAM DUMP FAILED @ WORD %x", isp->isp_mbxwrk1);
6972 		return;
6973 	}
6974 	ptr = isp->isp_mbxworkp;	/* finish fetch of final word */
6975 	*ptr++ = isp->isp_mboxtmp[2];
6976 	mbs.param[0] = MBOX_READ_RAM_WORD_EXTENDED;
6977 	mbs.param[1] = 0;
6978 	mbs.param[8] = 1;
6979 	isp->isp_mbxworkp = (void *) ptr;
6980 	isp->isp_mbxwrk0 = 0xffff;	/* continuation count */
6981 	isp->isp_mbxwrk1 = 0x1;		/* next SRAM address */
6982 	isp->isp_mbxwrk8 = 0x1;
6983 	isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
6984 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
6985 		isp_prt(isp, ISP_LOGWARN,
6986 		    "RAM DUMP FAILED @ WORD %x", 0x10000 + isp->isp_mbxwrk1);
6987 		return;
6988 	}
6989 	ptr = isp->isp_mbxworkp;	/* finish final word */
6990 	*ptr++ = mbs.param[2];
6991 	isp_prt(isp, ISP_LOGALL, "isp_fw_dump: SRAM dumped successfully");
6992 	FCPARAM(isp)->isp_dump_data[0] = isp->isp_type; /* now used */
6993 	(void) isp_async(isp, ISPASYNC_FW_DUMPED, 0);
6994 }
6995 
6996 void
6997 isp_fw_dump(ispsoftc_t *isp)
6998 {
6999 	if (IS_2200(isp))
7000 		isp2200_fw_dump(isp);
7001 	else if (IS_23XX(isp))
7002 		isp2300_fw_dump(isp);
7003 }
7004 #endif
7005