xref: /freebsd/sys/dev/isp/isp.c (revision 8847579c57d6aff2b3371c707dce7a2cee8389aa)
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 	isp_mboxcmd(isp, &mbs, MBLOGALL);
1478 	FC_SCRATCH_RELEASE(isp);
1479 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1480 		return;
1481 	}
1482 	isp->isp_reqidx = isp->isp_reqodx = 0;
1483 	isp->isp_residx = 0;
1484 	isp->isp_sendmarker = 1;
1485 
1486 	/*
1487 	 * Whatever happens, we're now committed to being here.
1488 	 */
1489 	isp->isp_state = ISP_INITSTATE;
1490 }
1491 
1492 /*
1493  * Fibre Channel Support- get the port database for the id.
1494  *
1495  * Locks are held before coming here. Return 0 if success,
1496  * else failure.
1497  */
1498 
1499 static int
1500 isp_getmap(ispsoftc_t *isp, fcpos_map_t *map)
1501 {
1502 	fcparam *fcp = (fcparam *) isp->isp_param;
1503 	mbreg_t mbs;
1504 
1505 	MEMZERO(&mbs, sizeof (mbs));
1506 	mbs.param[0] = MBOX_GET_FC_AL_POSITION_MAP;
1507 	mbs.param[1] = 0;
1508 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
1509 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
1510 	/*
1511 	 * Unneeded. For the 2100, except for initializing f/w, registers
1512 	 * 4/5 have to not be written to.
1513 	 *	mbs.param[4] = 0;
1514 	 *	mbs.param[5] = 0;
1515 	 *
1516 	 */
1517 	mbs.param[6] = 0;
1518 	mbs.param[7] = 0;
1519 	FC_SCRATCH_ACQUIRE(isp);
1520 	isp_mboxcmd(isp, &mbs, MBLOGALL & ~MBOX_COMMAND_PARAM_ERROR);
1521 	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1522 		MEMCPY(map, fcp->isp_scratch, sizeof (fcpos_map_t));
1523 		map->fwmap = mbs.param[1] != 0;
1524 		FC_SCRATCH_RELEASE(isp);
1525 		return (0);
1526 	}
1527 	FC_SCRATCH_RELEASE(isp);
1528 	return (-1);
1529 }
1530 
1531 static void
1532 isp_mark_getpdb_all(ispsoftc_t *isp)
1533 {
1534 	fcparam *fcp = (fcparam *) isp->isp_param;
1535 	int i;
1536 	for (i = 0; i < MAX_FC_TARG; i++) {
1537 		fcp->portdb[i].valid = fcp->portdb[i].fabric_dev = 0;
1538 	}
1539 }
1540 
1541 static int
1542 isp_getpdb(ispsoftc_t *isp, int id, isp_pdb_t *pdbp)
1543 {
1544 	fcparam *fcp = (fcparam *) isp->isp_param;
1545 	mbreg_t mbs;
1546 
1547 	MEMZERO(&mbs, sizeof (mbs));
1548 	mbs.param[0] = MBOX_GET_PORT_DB;
1549 	if (IS_2KLOGIN(isp)) {
1550 		mbs.param[1] = id;
1551 		mbs.obits |= (1 << 10);
1552 	} else {
1553 		mbs.param[1] = id << 8;
1554 	}
1555 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
1556 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
1557 	/*
1558 	 * Unneeded. For the 2100, except for initializing f/w, registers
1559 	 * 4/5 have to not be written to.
1560 	 *	mbs.param[4] = 0;
1561 	 *	mbs.param[5] = 0;
1562 	 *
1563 	 */
1564 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
1565 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
1566 	FC_SCRATCH_ACQUIRE(isp);
1567 	isp_mboxcmd(isp, &mbs, MBLOGALL & ~MBOX_COMMAND_PARAM_ERROR);
1568 	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1569 		isp_get_pdb(isp, (isp_pdb_t *)fcp->isp_scratch, pdbp);
1570 		FC_SCRATCH_RELEASE(isp);
1571 		return (0);
1572 	}
1573 	FC_SCRATCH_RELEASE(isp);
1574 	return (-1);
1575 }
1576 
1577 static uint64_t
1578 isp_get_portname(ispsoftc_t *isp, int loopid, int nodename)
1579 {
1580 	uint64_t wwn = 0;
1581 	mbreg_t mbs;
1582 
1583 	MEMZERO(&mbs, sizeof (mbs));
1584 	mbs.param[0] = MBOX_GET_PORT_NAME;
1585 	if (IS_2KLOGIN(isp)) {
1586 		mbs.param[1] = loopid;
1587 		mbs.obits |= (1 << 10);
1588 		if (nodename) {
1589 			mbs.param[10] = 1;
1590 		}
1591 	} else {
1592 		mbs.param[1] = loopid << 8;
1593 		if (nodename) {
1594 			mbs.param[1] |= 1;
1595 		}
1596 	}
1597 	isp_mboxcmd(isp, &mbs, MBLOGALL & ~MBOX_COMMAND_PARAM_ERROR);
1598 	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1599 		wwn =
1600 		    (((uint64_t)(mbs.param[2] & 0xff))  << 56) |
1601 		    (((uint64_t)(mbs.param[2] >> 8))	<< 48) |
1602 		    (((uint64_t)(mbs.param[3] & 0xff))	<< 40) |
1603 		    (((uint64_t)(mbs.param[3] >> 8))	<< 32) |
1604 		    (((uint64_t)(mbs.param[6] & 0xff))	<< 24) |
1605 		    (((uint64_t)(mbs.param[6] >> 8))	<< 16) |
1606 		    (((uint64_t)(mbs.param[7] & 0xff))	<<  8) |
1607 		    (((uint64_t)(mbs.param[7] >> 8)));
1608 	}
1609 	return (wwn);
1610 }
1611 
1612 /*
1613  * Make sure we have good FC link and know our Loop ID.
1614  */
1615 
1616 static int
1617 isp_fclink_test(ispsoftc_t *isp, int usdelay)
1618 {
1619 	static char *toponames[] = {
1620 		"Private Loop",
1621 		"FL Port",
1622 		"N-Port to N-Port",
1623 		"F Port",
1624 		"F Port (no FLOGI_ACC response)"
1625 	};
1626 	mbreg_t mbs;
1627 	int count, check_for_fabric;
1628 	uint8_t lwfs;
1629 	fcparam *fcp;
1630 	struct lportdb *lp;
1631 	isp_pdb_t pdb;
1632 
1633 	fcp = isp->isp_param;
1634 
1635 	isp_prt(isp, ISP_LOGDEBUG0, "FC Link Test Entry");
1636 
1637 	/*
1638 	 * XXX: Here is where we would start a 'loop dead' timeout
1639 	 */
1640 
1641 	/*
1642 	 * Wait up to N microseconds for F/W to go to a ready state.
1643 	 */
1644 	lwfs = FW_CONFIG_WAIT;
1645 	count = 0;
1646 	while (count < usdelay) {
1647 		uint64_t enano;
1648 		uint32_t wrk;
1649 		NANOTIME_T hra, hrb;
1650 
1651 		GET_NANOTIME(&hra);
1652 		isp_fw_state(isp);
1653 		if (lwfs != fcp->isp_fwstate) {
1654 			isp_prt(isp, ISP_LOGINFO, "Firmware State <%s->%s>",
1655 			    isp2100_fw_statename((int)lwfs),
1656 			    isp2100_fw_statename((int)fcp->isp_fwstate));
1657 			lwfs = fcp->isp_fwstate;
1658 		}
1659 		if (fcp->isp_fwstate == FW_READY) {
1660 			break;
1661 		}
1662 		GET_NANOTIME(&hrb);
1663 
1664 		/*
1665 		 * Get the elapsed time in nanoseconds.
1666 		 * Always guaranteed to be non-zero.
1667 		 */
1668 		enano = NANOTIME_SUB(&hrb, &hra);
1669 
1670 		isp_prt(isp, ISP_LOGDEBUG1,
1671 		    "usec%d: 0x%lx->0x%lx enano 0x%x%08x",
1672 		    count, (long) GET_NANOSEC(&hra), (long) GET_NANOSEC(&hrb),
1673 		    (uint32_t)(enano >> 32), (uint32_t)(enano & 0xffffffff));
1674 
1675 		/*
1676 		 * If the elapsed time is less than 1 millisecond,
1677 		 * delay a period of time up to that millisecond of
1678 		 * waiting.
1679 		 *
1680 		 * This peculiar code is an attempt to try and avoid
1681 		 * invoking uint64_t math support functions for some
1682 		 * platforms where linkage is a problem.
1683 		 */
1684 		if (enano < (1000 * 1000)) {
1685 			count += 1000;
1686 			enano = (1000 * 1000) - enano;
1687 			while (enano > (uint64_t) 4000000000U) {
1688 				USEC_SLEEP(isp, 4000000);
1689 				enano -= (uint64_t) 4000000000U;
1690 			}
1691 			wrk = enano;
1692 			wrk /= 1000;
1693 			USEC_SLEEP(isp, wrk);
1694 		} else {
1695 			while (enano > (uint64_t) 4000000000U) {
1696 				count += 4000000;
1697 				enano -= (uint64_t) 4000000000U;
1698 			}
1699 			wrk = enano;
1700 			count += (wrk / 1000);
1701 		}
1702 	}
1703 
1704 	/*
1705 	 * If we haven't gone to 'ready' state, return.
1706 	 */
1707 	if (fcp->isp_fwstate != FW_READY) {
1708 		return (-1);
1709 	}
1710 
1711 	/*
1712 	 * Get our Loop ID (if possible). We really need to have it.
1713 	 */
1714 	MEMZERO(&mbs, sizeof (mbs));
1715 	mbs.param[0] = MBOX_GET_LOOP_ID;
1716 	isp_mboxcmd(isp, &mbs, MBLOGALL);
1717 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1718 		return (-1);
1719 	}
1720 	if (IS_2KLOGIN(isp)) {
1721 		fcp->isp_loopid = mbs.param[1];
1722 	} else {
1723 		fcp->isp_loopid = mbs.param[1] & 0xff;
1724 	}
1725 	if (fcp->isp_loopid == 0xffff) {	/* happens with 2k login f/w */
1726 		fcp->isp_loopid = MAX_FC_TARG-1;
1727 	} else if (fcp->isp_loopid >= MAX_FC_TARG) {
1728 		isp_prt(isp, ISP_LOGWARN, "bad initiator loopid (0x%x)", fcp->isp_loopid);
1729 		fcp->isp_loopid = MAX_FC_TARG-1;
1730 	}
1731 	if (IS_2200(isp) || IS_23XX(isp)) {
1732 		int topo = (int) mbs.param[6];
1733 		if (topo < TOPO_NL_PORT || topo > TOPO_PTP_STUB)
1734 			topo = TOPO_PTP_STUB;
1735 		fcp->isp_topo = topo;
1736 	} else {
1737 		fcp->isp_topo = TOPO_NL_PORT;
1738 	}
1739 	/*
1740 	 * Get the port id.
1741 	 */
1742 	fcp->isp_portid = mbs.param[2] | (mbs.param[3] << 16);
1743 
1744 	/*
1745 	 * Check to see if we're on a fabric by trying to see if we
1746 	 * can talk to the fabric name server. This can be a bit
1747 	 * tricky because if we're a 2100, we should check always
1748 	 * (in case we're connected to a server doing aliasing).
1749 	 */
1750 	fcp->isp_onfabric = 0;
1751 
1752 	if (IS_2100(isp)) {
1753 		/*
1754 		 * Don't bother with fabric if we are using really old
1755 		 * 2100 firmware. It's just not worth it.
1756 		 */
1757 		if (ISP_FW_NEWER_THAN(isp, 1, 15, 37)) {
1758 			check_for_fabric = 1;
1759 		} else {
1760 			check_for_fabric = 0;
1761 		}
1762 	} else if (fcp->isp_topo == TOPO_FL_PORT ||
1763 	    fcp->isp_topo == TOPO_F_PORT) {
1764 		check_for_fabric = 1;
1765 	} else
1766 		check_for_fabric = 0;
1767 
1768 	if (check_for_fabric && isp_getpdb(isp, FL_PORT_ID, &pdb) == 0) {
1769 		int loopid = FL_PORT_ID;
1770 		if (IS_2100(isp)) {
1771 			fcp->isp_topo = TOPO_FL_PORT;
1772 		}
1773 
1774 		if (BITS2WORD(pdb.pdb_portid_bits) == 0) {
1775 			/*
1776 			 * Crock.
1777 			 */
1778 			fcp->isp_topo = TOPO_NL_PORT;
1779 			goto not_on_fabric;
1780 		}
1781 		fcp->isp_portid = mbs.param[2] | ((int) mbs.param[3] << 16);
1782 
1783 		/*
1784 		 * Save the Fabric controller's port database entry.
1785 		 */
1786 		lp = &fcp->portdb[loopid];
1787 		lp->node_wwn =
1788 		    (((uint64_t)pdb.pdb_nodename[0]) << 56) |
1789 		    (((uint64_t)pdb.pdb_nodename[1]) << 48) |
1790 		    (((uint64_t)pdb.pdb_nodename[2]) << 40) |
1791 		    (((uint64_t)pdb.pdb_nodename[3]) << 32) |
1792 		    (((uint64_t)pdb.pdb_nodename[4]) << 24) |
1793 		    (((uint64_t)pdb.pdb_nodename[5]) << 16) |
1794 		    (((uint64_t)pdb.pdb_nodename[6]) <<  8) |
1795 		    (((uint64_t)pdb.pdb_nodename[7]));
1796 		lp->port_wwn =
1797 		    (((uint64_t)pdb.pdb_portname[0]) << 56) |
1798 		    (((uint64_t)pdb.pdb_portname[1]) << 48) |
1799 		    (((uint64_t)pdb.pdb_portname[2]) << 40) |
1800 		    (((uint64_t)pdb.pdb_portname[3]) << 32) |
1801 		    (((uint64_t)pdb.pdb_portname[4]) << 24) |
1802 		    (((uint64_t)pdb.pdb_portname[5]) << 16) |
1803 		    (((uint64_t)pdb.pdb_portname[6]) <<  8) |
1804 		    (((uint64_t)pdb.pdb_portname[7]));
1805 		lp->roles =
1806 		    (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
1807 		lp->portid = BITS2WORD(pdb.pdb_portid_bits);
1808 		lp->loopid = pdb.pdb_loopid;
1809 		lp->loggedin = lp->valid = 1;
1810 		fcp->isp_onfabric = 1;
1811 		(void) isp_async(isp, ISPASYNC_PROMENADE, &loopid);
1812 		isp_register_fc4_type(isp);
1813 	} else {
1814 not_on_fabric:
1815 		fcp->isp_onfabric = 0;
1816 		fcp->portdb[FL_PORT_ID].valid = 0;
1817 	}
1818 
1819 	fcp->isp_gbspeed = 1;
1820 	if (IS_23XX(isp)) {
1821 		mbs.param[0] = MBOX_GET_SET_DATA_RATE;
1822 		mbs.param[1] = MBGSD_GET_RATE;
1823 		/* mbs.param[2] undefined if we're just getting rate */
1824 		isp_mboxcmd(isp, &mbs, MBLOGALL);
1825 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1826 			if (mbs.param[1] == MBGSD_TWOGB) {
1827 				isp_prt(isp, ISP_LOGINFO, "2Gb link speed/s");
1828 				fcp->isp_gbspeed = 2;
1829 			}
1830 		}
1831 	}
1832 
1833 	isp_prt(isp, ISP_LOGCONFIG, topology, fcp->isp_loopid,
1834 	    fcp->isp_portid, fcp->isp_loopstate, toponames[fcp->isp_topo]);
1835 
1836 	/*
1837 	 * Announce ourselves, too. This involves synthesizing an entry.
1838 	 */
1839 	if (fcp->isp_iid_set == 0) {
1840 		fcp->isp_iid_set = 1;
1841 		fcp->isp_iid = fcp->isp_loopid;
1842 		lp = &fcp->portdb[fcp->isp_iid];
1843 	} else {
1844 		lp = &fcp->portdb[fcp->isp_iid];
1845 		if (fcp->isp_portid != lp->portid ||
1846 		    fcp->isp_loopid != lp->loopid ||
1847 		    fcp->isp_nodewwn != ISP_NODEWWN(isp) ||
1848 		    fcp->isp_portwwn != ISP_PORTWWN(isp)) {
1849 			lp->valid = 0;
1850 			count = fcp->isp_iid;
1851 			(void) isp_async(isp, ISPASYNC_PROMENADE, &count);
1852 		}
1853 	}
1854 	lp->loopid = fcp->isp_loopid;
1855 	lp->portid = fcp->isp_portid;
1856 	lp->node_wwn = ISP_NODEWWN(isp);
1857 	lp->port_wwn = ISP_PORTWWN(isp);
1858 	switch (isp->isp_role) {
1859 	case ISP_ROLE_NONE:
1860 		lp->roles = 0;
1861 		break;
1862 	case ISP_ROLE_TARGET:
1863 		lp->roles = SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT;
1864 		break;
1865 	case ISP_ROLE_INITIATOR:
1866 		lp->roles = SVC3_INI_ROLE >> SVC3_ROLE_SHIFT;
1867 		break;
1868 	case ISP_ROLE_BOTH:
1869 		lp->roles = (SVC3_INI_ROLE|SVC3_TGT_ROLE) >> SVC3_ROLE_SHIFT;
1870 		break;
1871 	}
1872 	lp->loggedin = lp->valid = 1;
1873 	count = fcp->isp_iid;
1874 	(void) isp_async(isp, ISPASYNC_PROMENADE, &count);
1875 	isp_prt(isp, ISP_LOGDEBUG0, "FC Link Test Complete");
1876 	return (0);
1877 }
1878 
1879 static const char *
1880 isp2100_fw_statename(int state)
1881 {
1882 	switch(state) {
1883 	case FW_CONFIG_WAIT:	return "Config Wait";
1884 	case FW_WAIT_AL_PA:	return "Waiting for AL_PA";
1885 	case FW_WAIT_LOGIN:	return "Wait Login";
1886 	case FW_READY:		return "Ready";
1887 	case FW_LOSS_OF_SYNC:	return "Loss Of Sync";
1888 	case FW_ERROR:		return "Error";
1889 	case FW_REINIT:		return "Re-Init";
1890 	case FW_NON_PART:	return "Nonparticipating";
1891 	default:		return "?????";
1892 	}
1893 }
1894 
1895 /*
1896  * Synchronize our soft copy of the port database with what the f/w thinks
1897  * (with a view toward possibly for a specific target....)
1898  */
1899 
1900 static int
1901 isp_pdb_sync(ispsoftc_t *isp)
1902 {
1903 	struct lportdb *lp;
1904 	fcparam *fcp = isp->isp_param;
1905 	isp_pdb_t pdb;
1906 	int loopid, base, lim;
1907 
1908 	/*
1909 	 * Make sure we're okay for doing this right now.
1910 	 */
1911 	if (fcp->isp_loopstate != LOOP_PDB_RCVD &&
1912 	    fcp->isp_loopstate != LOOP_FSCAN_DONE &&
1913 	    fcp->isp_loopstate != LOOP_LSCAN_DONE) {
1914 		return (-1);
1915 	}
1916 
1917 	if (fcp->isp_topo == TOPO_FL_PORT || fcp->isp_topo == TOPO_NL_PORT ||
1918 	    fcp->isp_topo == TOPO_N_PORT) {
1919 		if (fcp->isp_loopstate < LOOP_LSCAN_DONE) {
1920 			if (isp_scan_loop(isp) != 0) {
1921 				return (-1);
1922 			}
1923 		}
1924 	}
1925 	fcp->isp_loopstate = LOOP_SYNCING_PDB;
1926 
1927 	/*
1928 	 * If we get this far, we've settled our differences with the f/w
1929 	 * (for local loop device) and we can say that the loop state is ready.
1930 	 */
1931 
1932 	if (fcp->isp_topo == TOPO_NL_PORT) {
1933 		fcp->loop_seen_once = 1;
1934 		fcp->isp_loopstate = LOOP_READY;
1935 		return (0);
1936 	}
1937 
1938 	/*
1939 	 * Find all Fabric Entities that didn't make it from one scan to the
1940 	 * next and let the world know they went away. Scan the whole database.
1941 	 */
1942 	for (lp = &fcp->portdb[0]; lp < &fcp->portdb[MAX_FC_TARG]; lp++) {
1943 		if (lp->was_fabric_dev && lp->fabric_dev == 0) {
1944 			loopid = lp - fcp->portdb;
1945 			lp->valid = 0;	/* should already be set */
1946 			(void) isp_async(isp, ISPASYNC_PROMENADE, &loopid);
1947 			MEMZERO((void *) lp, sizeof (*lp));
1948 			continue;
1949 		}
1950 		lp->was_fabric_dev = lp->fabric_dev;
1951 	}
1952 
1953 	if (fcp->isp_topo == TOPO_FL_PORT)
1954 		base = FC_SNS_ID+1;
1955 	else
1956 		base = 0;
1957 
1958 	if (fcp->isp_topo == TOPO_N_PORT)
1959 		lim = 1;
1960 	else
1961 		lim = MAX_FC_TARG;
1962 
1963 	/*
1964 	 * Now log in any fabric devices that the outer layer has
1965 	 * left for us to see. This seems the most sane policy
1966 	 * for the moment.
1967 	 */
1968 	for (lp = &fcp->portdb[base]; lp < &fcp->portdb[lim]; lp++) {
1969 		uint32_t portid;
1970 		mbreg_t mbs;
1971 
1972 		loopid = lp - fcp->portdb;
1973 		if (loopid >= FL_PORT_ID && loopid <= FC_SNS_ID) {
1974 			continue;
1975 		}
1976 
1977 		/*
1978 		 * Anything here?
1979 		 */
1980 		if (lp->port_wwn == 0) {
1981 			continue;
1982 		}
1983 
1984 		/*
1985 		 * Don't try to log into yourself.
1986 		 */
1987 		if ((portid = lp->portid) == fcp->isp_portid) {
1988 			continue;
1989 		}
1990 
1991 
1992 		/*
1993 		 * If we'd been logged in- see if we still are and we haven't
1994 		 * changed. If so, no need to log ourselves out, etc..
1995 		 *
1996 		 * Unfortunately, our charming Qlogic f/w has decided to
1997 		 * return a valid port database entry for a fabric device
1998 		 * that has, in fact, gone away. And it hangs trying to
1999 		 * log it out.
2000 		 */
2001 		if (lp->loggedin && lp->force_logout == 0 &&
2002 		    isp_getpdb(isp, lp->loopid, &pdb) == 0) {
2003 			int nrole;
2004 			uint64_t nwwnn, nwwpn;
2005 			nwwnn =
2006 			    (((uint64_t)pdb.pdb_nodename[0]) << 56) |
2007 			    (((uint64_t)pdb.pdb_nodename[1]) << 48) |
2008 			    (((uint64_t)pdb.pdb_nodename[2]) << 40) |
2009 			    (((uint64_t)pdb.pdb_nodename[3]) << 32) |
2010 			    (((uint64_t)pdb.pdb_nodename[4]) << 24) |
2011 			    (((uint64_t)pdb.pdb_nodename[5]) << 16) |
2012 			    (((uint64_t)pdb.pdb_nodename[6]) <<  8) |
2013 			    (((uint64_t)pdb.pdb_nodename[7]));
2014 			nwwpn =
2015 			    (((uint64_t)pdb.pdb_portname[0]) << 56) |
2016 			    (((uint64_t)pdb.pdb_portname[1]) << 48) |
2017 			    (((uint64_t)pdb.pdb_portname[2]) << 40) |
2018 			    (((uint64_t)pdb.pdb_portname[3]) << 32) |
2019 			    (((uint64_t)pdb.pdb_portname[4]) << 24) |
2020 			    (((uint64_t)pdb.pdb_portname[5]) << 16) |
2021 			    (((uint64_t)pdb.pdb_portname[6]) <<  8) |
2022 			    (((uint64_t)pdb.pdb_portname[7]));
2023 			nrole = (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >>
2024 			    SVC3_ROLE_SHIFT;
2025 			if (pdb.pdb_loopid == lp->loopid && lp->portid ==
2026 			    (uint32_t) BITS2WORD(pdb.pdb_portid_bits) &&
2027 			    nwwnn == lp->node_wwn && nwwpn == lp->port_wwn &&
2028 			    lp->roles == nrole && lp->force_logout == 0) {
2029 				lp->loggedin = lp->valid = 1;
2030 				isp_prt(isp, ISP_LOGCONFIG, lretained,
2031 				    (int) (lp - fcp->portdb),
2032 				    (int) lp->loopid, lp->portid);
2033 				continue;
2034 			}
2035 		}
2036 
2037 		if (fcp->isp_fwstate != FW_READY ||
2038 		    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
2039 			return (-1);
2040 		}
2041 
2042 		/*
2043 		 * Force a logout if we were logged in.
2044 		 */
2045 		if (lp->loggedin) {
2046 			if (lp->force_logout ||
2047 			    isp_getpdb(isp, lp->loopid, &pdb) == 0) {
2048 				MEMZERO(&mbs, sizeof (mbs));
2049 				mbs.param[0] = MBOX_FABRIC_LOGOUT;
2050 				if (IS_2KLOGIN(isp)) {
2051 					mbs.param[1] = lp->loopid;
2052 					mbs.obits |= (1 << 10);
2053 				} else {
2054 					mbs.param[1] = lp->loopid << 8;
2055 				}
2056 				mbs.param[2] = 0;
2057 				mbs.param[3] = 0;
2058 				isp_mboxcmd(isp, &mbs, MBLOGNONE);
2059 				isp_prt(isp, ISP_LOGINFO, plogout,
2060 				    (int) (lp - fcp->portdb), lp->loopid,
2061 				    lp->portid);
2062 			}
2063 			lp->force_logout = lp->loggedin = 0;
2064 			if (fcp->isp_fwstate != FW_READY ||
2065 			    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
2066 				return (-1);
2067 			}
2068 		}
2069 
2070 		/*
2071 		 * And log in....
2072 		 */
2073 		loopid = lp - fcp->portdb;
2074 		lp->loopid = FL_PORT_ID;
2075 		do {
2076 			MEMZERO(&mbs, sizeof (mbs));
2077 			mbs.param[0] = MBOX_FABRIC_LOGIN;
2078 			if (IS_2KLOGIN(isp)) {
2079 				mbs.param[1] = loopid;
2080 				mbs.obits |= (1 << 10);
2081 			} else {
2082 				mbs.param[1] = loopid << 8;
2083 			}
2084 			mbs.param[2] = portid >> 16;
2085 			mbs.param[3] = portid & 0xffff;
2086 			isp_mboxcmd(isp, &mbs, MBLOGALL & ~(MBOX_LOOP_ID_USED |
2087 			    MBOX_PORT_ID_USED | MBOX_COMMAND_ERROR));
2088 			if (fcp->isp_fwstate != FW_READY ||
2089 			    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
2090 				return (-1);
2091 			}
2092 			switch (mbs.param[0]) {
2093 			case MBOX_LOOP_ID_USED:
2094 				/*
2095 				 * Try the next available loop id.
2096 				 */
2097 				loopid++;
2098 				break;
2099 			case MBOX_PORT_ID_USED:
2100 				/*
2101 				 * This port is already logged in.
2102 				 * Snaffle the loop id it's using if it's
2103 				 * nonzero, otherwise we're hosed.
2104 				 */
2105 				if (mbs.param[1] != 0) {
2106 					loopid = mbs.param[1];
2107 					if (loopid >= MAX_FC_TARG) {
2108 						loopid = MAX_FC_TARG;
2109 						break;
2110 					}
2111 					isp_prt(isp, ISP_LOGINFO, retained,
2112 					    loopid, (int) (lp - fcp->portdb),
2113 					    lp->portid);
2114 				} else {
2115 					loopid = MAX_FC_TARG;
2116 					break;
2117 				}
2118 				/* FALLTHROUGH */
2119 			case MBOX_COMMAND_COMPLETE:
2120 				lp->loggedin = 1;
2121 				lp->loopid = loopid;
2122 				break;
2123 			case MBOX_COMMAND_ERROR:
2124 				isp_prt(isp, ISP_LOGINFO, plogierr,
2125 				    portid, mbs.param[1]);
2126 				/* FALLTHROUGH */
2127 			case MBOX_ALL_IDS_USED: /* We're outta IDs */
2128 			default:
2129 				loopid = MAX_FC_TARG;
2130 				break;
2131 			}
2132 		} while (lp->loopid == FL_PORT_ID && loopid < MAX_FC_TARG);
2133 
2134 		/*
2135 		 * If we get here and we haven't set a Loop ID,
2136 		 * we failed to log into this device.
2137 		 */
2138 
2139 		if (lp->loopid == FL_PORT_ID) {
2140 			lp->loopid = 0;
2141 			continue;
2142 		}
2143 
2144 		/*
2145 		 * Make sure we can get the approriate port information.
2146 		 */
2147 		if (isp_getpdb(isp, lp->loopid, &pdb) != 0) {
2148 			isp_prt(isp, ISP_LOGWARN, nopdb, lp->portid);
2149 			goto dump_em;
2150 		}
2151 
2152 		if (fcp->isp_fwstate != FW_READY ||
2153 		    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
2154 			return (-1);
2155 		}
2156 
2157 		if (pdb.pdb_loopid != lp->loopid) {
2158 			isp_prt(isp, ISP_LOGWARN, pdbmfail1,
2159 			    lp->portid, pdb.pdb_loopid);
2160 			goto dump_em;
2161 		}
2162 
2163 		if (lp->portid != (uint32_t) BITS2WORD(pdb.pdb_portid_bits)) {
2164 			isp_prt(isp, ISP_LOGWARN, pdbmfail2,
2165 			    lp->portid, BITS2WORD(pdb.pdb_portid_bits));
2166 			goto dump_em;
2167 		}
2168 
2169 		lp->roles =
2170 		    (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
2171 		lp->node_wwn =
2172 		    (((uint64_t)pdb.pdb_nodename[0]) << 56) |
2173 		    (((uint64_t)pdb.pdb_nodename[1]) << 48) |
2174 		    (((uint64_t)pdb.pdb_nodename[2]) << 40) |
2175 		    (((uint64_t)pdb.pdb_nodename[3]) << 32) |
2176 		    (((uint64_t)pdb.pdb_nodename[4]) << 24) |
2177 		    (((uint64_t)pdb.pdb_nodename[5]) << 16) |
2178 		    (((uint64_t)pdb.pdb_nodename[6]) <<  8) |
2179 		    (((uint64_t)pdb.pdb_nodename[7]));
2180 		lp->port_wwn =
2181 		    (((uint64_t)pdb.pdb_portname[0]) << 56) |
2182 		    (((uint64_t)pdb.pdb_portname[1]) << 48) |
2183 		    (((uint64_t)pdb.pdb_portname[2]) << 40) |
2184 		    (((uint64_t)pdb.pdb_portname[3]) << 32) |
2185 		    (((uint64_t)pdb.pdb_portname[4]) << 24) |
2186 		    (((uint64_t)pdb.pdb_portname[5]) << 16) |
2187 		    (((uint64_t)pdb.pdb_portname[6]) <<  8) |
2188 		    (((uint64_t)pdb.pdb_portname[7]));
2189 		/*
2190 		 * Check to make sure this all makes sense.
2191 		 */
2192 		if (lp->node_wwn && lp->port_wwn) {
2193 			lp->valid = 1;
2194 			loopid = lp - fcp->portdb;
2195 			(void) isp_async(isp, ISPASYNC_PROMENADE, &loopid);
2196 			continue;
2197 		}
2198 dump_em:
2199 		lp->valid = 0;
2200 		isp_prt(isp, ISP_LOGINFO,
2201 		    ldumped, loopid, lp->loopid, lp->portid);
2202 		MEMZERO(&mbs, sizeof (mbs));
2203 		mbs.param[0] = MBOX_FABRIC_LOGOUT;
2204 		if (IS_2KLOGIN(isp)) {
2205 			mbs.param[1] = lp->loopid;
2206 			mbs.obits |= (1 << 10);
2207 		} else {
2208 			mbs.param[1] = lp->loopid << 8;
2209 		}
2210 		isp_mboxcmd(isp, &mbs, MBLOGNONE);
2211 		if (fcp->isp_fwstate != FW_READY ||
2212 		    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
2213 			return (-1);
2214 		}
2215 	}
2216 	/*
2217 	 * If we get here, we've for sure seen not only a valid loop
2218 	 * but know what is or isn't on it, so mark this for usage
2219 	 * in isp_start.
2220 	 */
2221 	fcp->loop_seen_once = 1;
2222 	fcp->isp_loopstate = LOOP_READY;
2223 	return (0);
2224 }
2225 
2226 static int
2227 isp_scan_loop(ispsoftc_t *isp)
2228 {
2229 	struct lportdb *lp;
2230 	fcparam *fcp = isp->isp_param;
2231 	isp_pdb_t pdb;
2232 	int loopid, lim, hival;
2233 
2234 	switch (fcp->isp_topo) {
2235 	case TOPO_NL_PORT:
2236 		hival = FL_PORT_ID;
2237 		break;
2238 	case TOPO_N_PORT:
2239 		hival = 2;
2240 		break;
2241 	case TOPO_FL_PORT:
2242 		hival = FC_PORT_ID;
2243 		break;
2244 	default:
2245 		isp_prt(isp, ISP_LOGDEBUG0, "no loop topology to scan");
2246 		fcp->isp_loopstate = LOOP_LSCAN_DONE;
2247 		return (0);
2248 	}
2249 	fcp->isp_loopstate = LOOP_SCANNING_LOOP;
2250 
2251 	isp_prt(isp, ISP_LOGDEBUG0, "scanning local loop 0..%d", hival);
2252 
2253 
2254 	/*
2255 	 * make sure the temp port database is clean...
2256 	 */
2257 	MEMZERO((void *)fcp->tport, sizeof (fcp->tport));
2258 
2259 	/*
2260 	 * Run through the local loop ports and get port database info
2261 	 * for each loop ID.
2262 	 *
2263 	 * There's a somewhat unexplained situation where the f/w passes back
2264 	 * the wrong database entity- if that happens, just restart (up to
2265 	 * FL_PORT_ID times).
2266 	 */
2267 	for (lim = loopid = 0; loopid < hival; loopid++) {
2268 		lp = &fcp->tport[loopid];
2269 
2270 		/*
2271 		 * Don't even try for ourselves...
2272 	 	 */
2273 		if (loopid == fcp->isp_loopid) {
2274 			continue;
2275 		}
2276 
2277 		if (IS_2100(isp) || IS_2200(isp)) {
2278 			lp->node_wwn = isp_get_portname(isp, loopid, 1);
2279 			if (fcp->isp_loopstate < LOOP_SCANNING_LOOP) {
2280 				return (-1);
2281 			}
2282 			if (lp->node_wwn == 0) {
2283 				continue;
2284 			}
2285 		}
2286 
2287 		/*
2288 		 * Get an entry....
2289 		 */
2290 		if (isp_getpdb(isp, loopid, &pdb) != 0) {
2291 			if (fcp->isp_loopstate < LOOP_SCANNING_LOOP) {
2292 				return (-1);
2293 			}
2294 			continue;
2295 		}
2296 
2297 		if (fcp->isp_loopstate < LOOP_SCANNING_LOOP) {
2298 			return (-1);
2299 		}
2300 
2301 		/*
2302 		 * If the returned database element doesn't match what we
2303 		 * asked for, restart the process entirely (up to a point...).
2304 		 */
2305 		if (pdb.pdb_loopid != loopid) {
2306 			loopid = 0;
2307 			if (lim++ < hival) {
2308 				continue;
2309 			}
2310 			isp_prt(isp, ISP_LOGWARN,
2311 			    "giving up on synchronizing the port database");
2312 			return (-1);
2313 		}
2314 
2315 		/*
2316 		 * Save the pertinent info locally.
2317 		 */
2318 		lp->node_wwn =
2319 		    (((uint64_t)pdb.pdb_nodename[0]) << 56) |
2320 		    (((uint64_t)pdb.pdb_nodename[1]) << 48) |
2321 		    (((uint64_t)pdb.pdb_nodename[2]) << 40) |
2322 		    (((uint64_t)pdb.pdb_nodename[3]) << 32) |
2323 		    (((uint64_t)pdb.pdb_nodename[4]) << 24) |
2324 		    (((uint64_t)pdb.pdb_nodename[5]) << 16) |
2325 		    (((uint64_t)pdb.pdb_nodename[6]) <<  8) |
2326 		    (((uint64_t)pdb.pdb_nodename[7]));
2327 		lp->port_wwn =
2328 		    (((uint64_t)pdb.pdb_portname[0]) << 56) |
2329 		    (((uint64_t)pdb.pdb_portname[1]) << 48) |
2330 		    (((uint64_t)pdb.pdb_portname[2]) << 40) |
2331 		    (((uint64_t)pdb.pdb_portname[3]) << 32) |
2332 		    (((uint64_t)pdb.pdb_portname[4]) << 24) |
2333 		    (((uint64_t)pdb.pdb_portname[5]) << 16) |
2334 		    (((uint64_t)pdb.pdb_portname[6]) <<  8) |
2335 		    (((uint64_t)pdb.pdb_portname[7]));
2336 		lp->roles =
2337 		    (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
2338 		lp->portid = BITS2WORD(pdb.pdb_portid_bits);
2339 		lp->loopid = pdb.pdb_loopid;
2340 	}
2341 
2342 	/*
2343 	 * Mark all of the permanent local loop database entries as invalid
2344 	 * (except our own entry).
2345 	 */
2346 	for (loopid = 0; loopid < hival; loopid++) {
2347 		if (loopid == fcp->isp_iid) {
2348 			fcp->portdb[loopid].valid = 1;
2349 			fcp->portdb[loopid].loopid = fcp->isp_loopid;
2350 			continue;
2351 		}
2352 		fcp->portdb[loopid].valid = 0;
2353 	}
2354 
2355 	/*
2356 	 * Now merge our local copy of the port database into our saved copy.
2357 	 * Notify the outer layers of new devices arriving.
2358 	 */
2359 	for (loopid = 0; loopid < hival; loopid++) {
2360 		int i;
2361 
2362 		/*
2363 		 * If we don't have a non-zero Port WWN, we're not here.
2364 		 */
2365 		if (fcp->tport[loopid].port_wwn == 0) {
2366 			continue;
2367 		}
2368 
2369 		/*
2370 		 * Skip ourselves.
2371 		 */
2372 		if (loopid == fcp->isp_iid) {
2373 			continue;
2374 		}
2375 
2376 		/*
2377 		 * For the purposes of deciding whether this is the
2378 		 * 'same' device or not, we only search for an identical
2379 		 * Port WWN. Node WWNs may or may not be the same as
2380 		 * the Port WWN, and there may be multiple different
2381 		 * Port WWNs with the same Node WWN. It would be chaos
2382 		 * to have multiple identical Port WWNs, so we don't
2383 		 * allow that.
2384 		 */
2385 
2386 		for (i = 0; i < hival; i++) {
2387 			int j;
2388 			if (fcp->portdb[i].port_wwn == 0)
2389 				continue;
2390 			if (fcp->portdb[i].port_wwn !=
2391 			    fcp->tport[loopid].port_wwn)
2392 				continue;
2393 			/*
2394 			 * We found this WWN elsewhere- it's changed
2395 			 * loopids then. We don't change it's actual
2396 			 * position in our cached port database- we
2397 			 * just change the actual loop ID we'd use.
2398 			 */
2399 			if (fcp->portdb[i].loopid != loopid) {
2400 				isp_prt(isp, ISP_LOGINFO, portshift, i,
2401 				    fcp->portdb[i].loopid,
2402 				    fcp->portdb[i].portid, loopid,
2403 				    fcp->tport[loopid].portid);
2404 			}
2405 			fcp->portdb[i].portid = fcp->tport[loopid].portid;
2406 			fcp->portdb[i].loopid = loopid;
2407 			fcp->portdb[i].valid = 1;
2408 			fcp->portdb[i].roles = fcp->tport[loopid].roles;
2409 
2410 			/*
2411 			 * Now make sure this Port WWN doesn't exist elsewhere
2412 			 * in the port database.
2413 			 */
2414 			for (j = i+1; j < hival; j++) {
2415 				if (fcp->portdb[i].port_wwn !=
2416 				    fcp->portdb[j].port_wwn) {
2417 					continue;
2418 				}
2419 				isp_prt(isp, ISP_LOGWARN, portdup, j, i);
2420 				/*
2421 				 * Invalidate the 'old' *and* 'new' ones.
2422 				 * This is really harsh and not quite right,
2423 				 * but if this happens, we really don't know
2424 				 * who is what at this point.
2425 				 */
2426 				fcp->portdb[i].valid = 0;
2427 				fcp->portdb[j].valid = 0;
2428 			}
2429 			break;
2430 		}
2431 
2432 		/*
2433 		 * If we didn't traverse the entire port database,
2434 		 * then we found (and remapped) an existing entry.
2435 		 * No need to notify anyone- go for the next one.
2436 		 */
2437 		if (i < hival) {
2438 			isp_prt(isp, ISP_LOGINFO, retained,
2439 			    fcp->portdb[i].loopid, i, fcp->portdb[i].portid);
2440 			continue;
2441 		}
2442 
2443 		/*
2444 		 * We've not found this Port WWN anywhere. It's a new entry.
2445 		 * See if we can leave it where it is (with target == loopid).
2446 		 */
2447 		if (fcp->portdb[loopid].port_wwn != 0) {
2448 			for (lim = 0; lim < hival; lim++) {
2449 				if (fcp->portdb[lim].port_wwn == 0)
2450 					break;
2451 			}
2452 			/* "Cannot Happen" */
2453 			if (lim == hival) {
2454 				isp_prt(isp, ISP_LOGWARN, "Remap Overflow");
2455 				continue;
2456 			}
2457 			i = lim;
2458 		} else {
2459 			i = loopid;
2460 		}
2461 
2462 		/*
2463 		 * NB:	The actual loopid we use here is loopid- we may
2464 		 *	in fact be at a completely different index (target).
2465 		 */
2466 		fcp->portdb[i].loopid = loopid;
2467 		fcp->portdb[i].port_wwn = fcp->tport[loopid].port_wwn;
2468 		fcp->portdb[i].node_wwn = fcp->tport[loopid].node_wwn;
2469 		fcp->portdb[i].roles = fcp->tport[loopid].roles;
2470 		fcp->portdb[i].portid = fcp->tport[loopid].portid;
2471 		fcp->portdb[i].valid = 1;
2472 
2473 		/*
2474 		 * Tell the outside world we've arrived.
2475 		 */
2476 		(void) isp_async(isp, ISPASYNC_PROMENADE, &i);
2477 	}
2478 
2479 	/*
2480 	 * Now find all previously used targets that are now invalid and
2481 	 * notify the outer layers that they're gone.
2482 	 */
2483 	for (lp = &fcp->portdb[0]; lp < &fcp->portdb[hival]; lp++) {
2484 		if (lp->valid || lp->port_wwn == 0) {
2485 			continue;
2486 		}
2487 
2488 		/*
2489 		 * Tell the outside world we've gone
2490 		 * away and erase our pdb entry.
2491 		 *
2492 		 */
2493 		loopid = lp - fcp->portdb;
2494 		(void) isp_async(isp, ISPASYNC_PROMENADE, &loopid);
2495 		MEMZERO((void *) lp, sizeof (*lp));
2496 	}
2497 	fcp->isp_loopstate = LOOP_LSCAN_DONE;
2498 	return (0);
2499 }
2500 
2501 
2502 static int
2503 isp_fabric_mbox_cmd(ispsoftc_t *isp, mbreg_t *mbp)
2504 {
2505 	isp_mboxcmd(isp, mbp, MBLOGNONE);
2506 	if (mbp->param[0] != MBOX_COMMAND_COMPLETE) {
2507 		if (FCPARAM(isp)->isp_loopstate == LOOP_SCANNING_FABRIC) {
2508 			FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
2509 		}
2510 		if (mbp->param[0] == MBOX_COMMAND_ERROR) {
2511 			char tbuf[16];
2512 			char *m;
2513 			switch (mbp->param[1]) {
2514 			case 1:
2515 				m = "No Loop";
2516 				break;
2517 			case 2:
2518 				m = "Failed to allocate IOCB buffer";
2519 				break;
2520 			case 3:
2521 				m = "Failed to allocate XCB buffer";
2522 				break;
2523 			case 4:
2524 				m = "timeout or transmit failed";
2525 				break;
2526 			case 5:
2527 				m = "no fabric loop";
2528 				break;
2529 			case 6:
2530 				m = "remote device not a target";
2531 				break;
2532 			default:
2533 				SNPRINTF(tbuf, sizeof tbuf, "%x",
2534 				    mbp->param[1]);
2535 				m = tbuf;
2536 				break;
2537 			}
2538 			isp_prt(isp, ISP_LOGERR, "SNS Failed- %s", m);
2539 		}
2540 		return (-1);
2541 	}
2542 
2543 	if (FCPARAM(isp)->isp_fwstate != FW_READY ||
2544 	    FCPARAM(isp)->isp_loopstate < LOOP_SCANNING_FABRIC) {
2545 		return (-1);
2546 	}
2547 	return(0);
2548 }
2549 
2550 #ifdef	ISP_USE_GA_NXT
2551 static int
2552 isp_scan_fabric(ispsoftc_t *isp, int ftype)
2553 {
2554 	fcparam *fcp = isp->isp_param;
2555 	uint32_t portid, first_portid, last_portid;
2556 	int hicap, last_port_same;
2557 
2558 	if (fcp->isp_onfabric == 0) {
2559 		fcp->isp_loopstate = LOOP_FSCAN_DONE;
2560 		return (0);
2561 	}
2562 
2563 	isp_prt(isp, ISP_LOGDEBUG0, "scanning fabric (GA_NXT)");
2564 
2565 	FC_SCRATCH_ACQUIRE(isp);
2566 
2567 	/*
2568 	 * Since Port IDs are 24 bits, we can check against having seen
2569 	 * anything yet with this value.
2570 	 */
2571 	last_port_same = 0;
2572 	last_portid = 0xffffffff;	/* not a port */
2573 	first_portid = portid = fcp->isp_portid;
2574 	fcp->isp_loopstate = LOOP_SCANNING_FABRIC;
2575 
2576 	for (hicap = 0; hicap < GA_NXT_MAX; hicap++) {
2577 		mbreg_t mbs;
2578 		sns_screq_t *rq;
2579 		sns_ga_nxt_rsp_t *rs0, *rs1;
2580 		struct lportdb lcl;
2581 		uint8_t sc[SNS_GA_NXT_RESP_SIZE];
2582 
2583 		rq = (sns_screq_t *)sc;
2584 		MEMZERO((void *) rq, SNS_GA_NXT_REQ_SIZE);
2585 		rq->snscb_rblen = SNS_GA_NXT_RESP_SIZE >> 1;
2586 		rq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+0x100);
2587 		rq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+0x100);
2588 		rq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+0x100);
2589 		rq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+0x100);
2590 		rq->snscb_sblen = 6;
2591 		rq->snscb_data[0] = SNS_GA_NXT;
2592 		rq->snscb_data[4] = portid & 0xffff;
2593 		rq->snscb_data[5] = (portid >> 16) & 0xff;
2594 		isp_put_sns_request(isp, rq, (sns_screq_t *) fcp->isp_scratch);
2595 		MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GA_NXT_REQ_SIZE);
2596 		MEMZERO(&mbs, sizeof (mbs));
2597 		mbs.param[0] = MBOX_SEND_SNS;
2598 		mbs.param[1] = SNS_GA_NXT_REQ_SIZE >> 1;
2599 		mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2600 		mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2601 		/*
2602 		 * Leave 4 and 5 alone
2603 		 */
2604 		mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2605 		mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2606 		if (isp_fabric_mbox_cmd(isp, &mbs)) {
2607 			if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
2608 				fcp->isp_loopstate = LOOP_PDB_RCVD;
2609 			}
2610 			FC_SCRATCH_RELEASE(isp);
2611 			return (-1);
2612 		}
2613 		MEMORYBARRIER(isp, SYNC_SFORCPU, 0x100, SNS_GA_NXT_RESP_SIZE);
2614 		rs1 = (sns_ga_nxt_rsp_t *) sc;
2615 		rs0 = (sns_ga_nxt_rsp_t *) ((uint8_t *)fcp->isp_scratch+0x100);
2616 		isp_get_ga_nxt_response(isp, rs0, rs1);
2617 		if (rs1->snscb_cthdr.ct_response != FS_ACC) {
2618 			int level;
2619 			if (rs1->snscb_cthdr.ct_reason == 9 &&
2620 			    rs1->snscb_cthdr.ct_explanation == 7)
2621 				level = ISP_LOGDEBUG0;
2622 			else
2623 				level = ISP_LOGWARN;
2624 			isp_prt(isp, level, swrej, "GA_NXT",
2625 			    rs1->snscb_cthdr.ct_reason,
2626 			    rs1->snscb_cthdr.ct_explanation, portid);
2627 			FC_SCRATCH_RELEASE(isp);
2628 			fcp->isp_loopstate = LOOP_FSCAN_DONE;
2629 			return (0);
2630 		}
2631 		portid =
2632 		    (((uint32_t) rs1->snscb_port_id[0]) << 16) |
2633 		    (((uint32_t) rs1->snscb_port_id[1]) << 8) |
2634 		    (((uint32_t) rs1->snscb_port_id[2]));
2635 
2636 		/*
2637 		 * XXX: We should check to make sure that this entry
2638 		 * XXX: supports the type(s) we are interested in.
2639 		 */
2640 		/*
2641 		 * Okay, we now have information about a fabric object.
2642 		 * If it is the type we're interested in, tell the outer layers
2643 		 * about it. The outer layer needs to  know: Port ID, WWNN,
2644 		 * WWPN, FC4 type, and port type.
2645 		 *
2646 		 * The lportdb structure is adequate for this.
2647 		 */
2648 		MEMZERO(&lcl, sizeof (lcl));
2649 		lcl.port_type = rs1->snscb_port_type;
2650 		lcl.fc4_type = ftype;
2651 		lcl.portid = portid;
2652 		lcl.node_wwn =
2653 		    (((uint64_t)rs1->snscb_nodename[0]) << 56) |
2654 		    (((uint64_t)rs1->snscb_nodename[1]) << 48) |
2655 		    (((uint64_t)rs1->snscb_nodename[2]) << 40) |
2656 		    (((uint64_t)rs1->snscb_nodename[3]) << 32) |
2657 		    (((uint64_t)rs1->snscb_nodename[4]) << 24) |
2658 		    (((uint64_t)rs1->snscb_nodename[5]) << 16) |
2659 		    (((uint64_t)rs1->snscb_nodename[6]) <<  8) |
2660 		    (((uint64_t)rs1->snscb_nodename[7]));
2661 		lcl.port_wwn =
2662 		    (((uint64_t)rs1->snscb_portname[0]) << 56) |
2663 		    (((uint64_t)rs1->snscb_portname[1]) << 48) |
2664 		    (((uint64_t)rs1->snscb_portname[2]) << 40) |
2665 		    (((uint64_t)rs1->snscb_portname[3]) << 32) |
2666 		    (((uint64_t)rs1->snscb_portname[4]) << 24) |
2667 		    (((uint64_t)rs1->snscb_portname[5]) << 16) |
2668 		    (((uint64_t)rs1->snscb_portname[6]) <<  8) |
2669 		    (((uint64_t)rs1->snscb_portname[7]));
2670 
2671 		/*
2672 		 * Does this fabric object support the type we want?
2673 		 * If not, skip it.
2674 		 */
2675 		if (rs1->snscb_fc4_types[ftype >> 5] & (1 << (ftype & 0x1f))) {
2676 			if (first_portid == portid) {
2677 				lcl.last_fabric_dev = 1;
2678 			} else {
2679 				lcl.last_fabric_dev = 0;
2680 			}
2681 			(void) isp_async(isp, ISPASYNC_FABRIC_DEV, &lcl);
2682 		} else {
2683 			isp_prt(isp, ISP_LOGDEBUG0,
2684 			    "PortID 0x%x doesn't support FC4 type 0x%x",
2685 			    portid, ftype);
2686 		}
2687 		if (first_portid == portid) {
2688 			fcp->isp_loopstate = LOOP_FSCAN_DONE;
2689 			FC_SCRATCH_RELEASE(isp);
2690 			return (0);
2691 		}
2692 		if (portid == last_portid) {
2693 			if (last_port_same++ > 20) {
2694 				isp_prt(isp, ISP_LOGWARN,
2695 				    "tangled fabric database detected");
2696 				break;
2697 			}
2698 		} else {
2699 			last_port_same = 0 ;
2700 			last_portid = portid;
2701 		}
2702 	}
2703 	FC_SCRATCH_RELEASE(isp);
2704 	if (hicap >= GA_NXT_MAX) {
2705 		isp_prt(isp, ISP_LOGWARN, "fabric too big (> %d)", GA_NXT_MAX);
2706 	}
2707 	fcp->isp_loopstate = LOOP_FSCAN_DONE;
2708 	return (0);
2709 }
2710 #else
2711 #define	GIDLEN	((ISP2100_SCRLEN >> 1) + 16)
2712 #define	NGENT	((GIDLEN - 16) >> 2)
2713 
2714 #define	IGPOFF	(ISP2100_SCRLEN - GIDLEN)
2715 #define	GXOFF	(256)
2716 
2717 static int
2718 isp_scan_fabric(ispsoftc_t *isp, int ftype)
2719 {
2720 	fcparam *fcp = FCPARAM(isp);
2721 	mbreg_t mbs;
2722 	int i;
2723 	sns_gid_ft_req_t *rq;
2724 	sns_gid_ft_rsp_t *rs0, *rs1;
2725 
2726 	if (fcp->isp_onfabric == 0) {
2727 		fcp->isp_loopstate = LOOP_FSCAN_DONE;
2728 		return (0);
2729 	}
2730 
2731 	isp_prt(isp, ISP_LOGDEBUG0, "scanning fabric (GID_FT)");
2732 
2733 	FC_SCRATCH_ACQUIRE(isp);
2734 	fcp->isp_loopstate = LOOP_SCANNING_FABRIC;
2735 
2736 	rq = (sns_gid_ft_req_t *)fcp->tport;
2737 	MEMZERO((void *) rq, SNS_GID_FT_REQ_SIZE);
2738 	rq->snscb_rblen = GIDLEN >> 1;
2739 	rq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+IGPOFF);
2740 	rq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+IGPOFF);
2741 	rq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+IGPOFF);
2742 	rq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+IGPOFF);
2743 	rq->snscb_sblen = 6;
2744 	rq->snscb_cmd = SNS_GID_FT;
2745 	rq->snscb_mword_div_2 = NGENT;
2746 	rq->snscb_fc4_type = ftype;
2747 	isp_put_gid_ft_request(isp, rq, (sns_gid_ft_req_t *) fcp->isp_scratch);
2748 	MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GID_FT_REQ_SIZE);
2749 	MEMZERO(&mbs, sizeof (mbs));
2750 	mbs.param[0] = MBOX_SEND_SNS;
2751 	mbs.param[1] = SNS_GID_FT_REQ_SIZE >> 1;
2752 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2753 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2754 
2755 	/*
2756 	 * Leave 4 and 5 alone
2757 	 */
2758 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2759 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2760 	if (isp_fabric_mbox_cmd(isp, &mbs)) {
2761 		if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
2762 			fcp->isp_loopstate = LOOP_PDB_RCVD;
2763 		}
2764 		FC_SCRATCH_RELEASE(isp);
2765 		return (-1);
2766 	}
2767 	if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2768 		FC_SCRATCH_RELEASE(isp);
2769 		return (-1);
2770 	}
2771 	MEMORYBARRIER(isp, SYNC_SFORCPU, IGPOFF, GIDLEN);
2772 	rs1 = (sns_gid_ft_rsp_t *) fcp->tport;
2773 	rs0 = (sns_gid_ft_rsp_t *) ((uint8_t *)fcp->isp_scratch+IGPOFF);
2774 	isp_get_gid_ft_response(isp, rs0, rs1, NGENT);
2775 	if (rs1->snscb_cthdr.ct_response != FS_ACC) {
2776 		int level;
2777 		if (rs1->snscb_cthdr.ct_reason == 9 &&
2778 		    rs1->snscb_cthdr.ct_explanation == 7)
2779 			level = ISP_LOGDEBUG0;
2780 		else
2781 			level = ISP_LOGWARN;
2782 		isp_prt(isp, level, swrej, "GID_FT",
2783 		    rs1->snscb_cthdr.ct_reason,
2784 		    rs1->snscb_cthdr.ct_explanation, 0);
2785 		FC_SCRATCH_RELEASE(isp);
2786 		fcp->isp_loopstate = LOOP_FSCAN_DONE;
2787 		return (0);
2788 	}
2789 
2790 	/*
2791 	 * Okay, we now have a list of Port IDs for this class of device.
2792 	 * Go through the list and for each one get the WWPN/WWNN for it
2793 	 * and tell the outer layers about it. The outer layer needs to
2794 	 * know: Port ID, WWNN, WWPN, FC4 type, and (possibly) port type.
2795 	 *
2796 	 * The lportdb structure is adequate for this.
2797 	 */
2798 	i = -1;
2799 	do {
2800 		sns_gxn_id_req_t grqbuf, *gq = &grqbuf;
2801 		sns_gxn_id_rsp_t *gs0, grsbuf, *gs1 = &grsbuf;
2802 		struct lportdb lcl;
2803 #if	0
2804 		sns_gff_id_rsp_t *fs0, ffsbuf, *fs1 = &ffsbuf;
2805 #endif
2806 
2807 		i++;
2808 		MEMZERO(&lcl, sizeof (lcl));
2809 		lcl.fc4_type = ftype;
2810 		lcl.portid =
2811 		    (((uint32_t) rs1->snscb_ports[i].portid[0]) << 16) |
2812 		    (((uint32_t) rs1->snscb_ports[i].portid[1]) << 8) |
2813 		    (((uint32_t) rs1->snscb_ports[i].portid[2]));
2814 
2815 		MEMZERO((void *) gq, sizeof (sns_gxn_id_req_t));
2816 		gq->snscb_rblen = SNS_GXN_ID_RESP_SIZE >> 1;
2817 		gq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+GXOFF);
2818 		gq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+GXOFF);
2819 		gq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+GXOFF);
2820 		gq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+GXOFF);
2821 		gq->snscb_sblen = 6;
2822 		gq->snscb_cmd = SNS_GPN_ID;
2823 		gq->snscb_portid = lcl.portid;
2824 		isp_put_gxn_id_request(isp, gq,
2825 		    (sns_gxn_id_req_t *) fcp->isp_scratch);
2826 		MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GXN_ID_REQ_SIZE);
2827 		MEMZERO(&mbs, sizeof (mbs));
2828 		mbs.param[0] = MBOX_SEND_SNS;
2829 		mbs.param[1] = SNS_GXN_ID_REQ_SIZE >> 1;
2830 		mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2831 		mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2832 		/*
2833 		 * Leave 4 and 5 alone
2834 		 */
2835 		mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2836 		mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2837 		if (isp_fabric_mbox_cmd(isp, &mbs)) {
2838 			if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
2839 				fcp->isp_loopstate = LOOP_PDB_RCVD;
2840 			}
2841 			FC_SCRATCH_RELEASE(isp);
2842 			return (-1);
2843 		}
2844 		if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2845 			FC_SCRATCH_RELEASE(isp);
2846 			return (-1);
2847 		}
2848 		MEMORYBARRIER(isp, SYNC_SFORCPU, GXOFF, SNS_GXN_ID_RESP_SIZE);
2849 		gs0 = (sns_gxn_id_rsp_t *) ((uint8_t *)fcp->isp_scratch+GXOFF);
2850 		isp_get_gxn_id_response(isp, gs0, gs1);
2851 		if (gs1->snscb_cthdr.ct_response != FS_ACC) {
2852 			isp_prt(isp, ISP_LOGWARN, swrej, "GPN_ID",
2853 			    gs1->snscb_cthdr.ct_reason,
2854 			    gs1->snscb_cthdr.ct_explanation, lcl.portid);
2855 			if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2856 				FC_SCRATCH_RELEASE(isp);
2857 				return (-1);
2858 			}
2859 			continue;
2860 		}
2861 		lcl.port_wwn =
2862 		    (((uint64_t)gs1->snscb_wwn[0]) << 56) |
2863 		    (((uint64_t)gs1->snscb_wwn[1]) << 48) |
2864 		    (((uint64_t)gs1->snscb_wwn[2]) << 40) |
2865 		    (((uint64_t)gs1->snscb_wwn[3]) << 32) |
2866 		    (((uint64_t)gs1->snscb_wwn[4]) << 24) |
2867 		    (((uint64_t)gs1->snscb_wwn[5]) << 16) |
2868 		    (((uint64_t)gs1->snscb_wwn[6]) <<  8) |
2869 		    (((uint64_t)gs1->snscb_wwn[7]));
2870 
2871 		MEMZERO((void *) gq, sizeof (sns_gxn_id_req_t));
2872 		gq->snscb_rblen = SNS_GXN_ID_RESP_SIZE >> 1;
2873 		gq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+GXOFF);
2874 		gq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+GXOFF);
2875 		gq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+GXOFF);
2876 		gq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+GXOFF);
2877 		gq->snscb_sblen = 6;
2878 		gq->snscb_cmd = SNS_GNN_ID;
2879 		gq->snscb_portid = lcl.portid;
2880 		isp_put_gxn_id_request(isp, gq,
2881 		    (sns_gxn_id_req_t *) fcp->isp_scratch);
2882 		MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GXN_ID_REQ_SIZE);
2883 		MEMZERO(&mbs, sizeof (mbs));
2884 		mbs.param[0] = MBOX_SEND_SNS;
2885 		mbs.param[1] = SNS_GXN_ID_REQ_SIZE >> 1;
2886 		mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2887 		mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2888 		/*
2889 		 * Leave 4 and 5 alone
2890 		 */
2891 		mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2892 		mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2893 		if (isp_fabric_mbox_cmd(isp, &mbs)) {
2894 			if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
2895 				fcp->isp_loopstate = LOOP_PDB_RCVD;
2896 			}
2897 			FC_SCRATCH_RELEASE(isp);
2898 			return (-1);
2899 		}
2900 		if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2901 			FC_SCRATCH_RELEASE(isp);
2902 			return (-1);
2903 		}
2904 		MEMORYBARRIER(isp, SYNC_SFORCPU, GXOFF, SNS_GXN_ID_RESP_SIZE);
2905 		gs0 = (sns_gxn_id_rsp_t *) ((uint8_t *)fcp->isp_scratch+GXOFF);
2906 		isp_get_gxn_id_response(isp, gs0, gs1);
2907 		if (gs1->snscb_cthdr.ct_response != FS_ACC) {
2908 			isp_prt(isp, ISP_LOGWARN, swrej, "GNN_ID",
2909 			    gs1->snscb_cthdr.ct_reason,
2910 			    gs1->snscb_cthdr.ct_explanation, lcl.portid);
2911 			if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2912 				FC_SCRATCH_RELEASE(isp);
2913 				return (-1);
2914 			}
2915 			continue;
2916 		}
2917 		lcl.node_wwn =
2918 		    (((uint64_t)gs1->snscb_wwn[0]) << 56) |
2919 		    (((uint64_t)gs1->snscb_wwn[1]) << 48) |
2920 		    (((uint64_t)gs1->snscb_wwn[2]) << 40) |
2921 		    (((uint64_t)gs1->snscb_wwn[3]) << 32) |
2922 		    (((uint64_t)gs1->snscb_wwn[4]) << 24) |
2923 		    (((uint64_t)gs1->snscb_wwn[5]) << 16) |
2924 		    (((uint64_t)gs1->snscb_wwn[6]) <<  8) |
2925 		    (((uint64_t)gs1->snscb_wwn[7]));
2926 
2927 		/*
2928 		 * The QLogic f/w is bouncing this with a parameter error.
2929 		 */
2930 #if	0
2931 		/*
2932 		 * Try and get FC4 Features (FC-GS-3 only).
2933 		 * We can use the sns_gxn_id_req_t for this request.
2934 		 */
2935 		MEMZERO((void *) gq, sizeof (sns_gxn_id_req_t));
2936 		gq->snscb_rblen = SNS_GFF_ID_RESP_SIZE >> 1;
2937 		gq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+GXOFF);
2938 		gq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+GXOFF);
2939 		gq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+GXOFF);
2940 		gq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+GXOFF);
2941 		gq->snscb_sblen = 6;
2942 		gq->snscb_cmd = SNS_GFF_ID;
2943 		gq->snscb_portid = lcl.portid;
2944 		isp_put_gxn_id_request(isp, gq,
2945 		    (sns_gxn_id_req_t *) fcp->isp_scratch);
2946 		MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GXN_ID_REQ_SIZE);
2947 		MEMZERO(&mbs, sizeof (mbs));
2948 		mbs.param[0] = MBOX_SEND_SNS;
2949 		mbs.param[1] = SNS_GXN_ID_REQ_SIZE >> 1;
2950 		mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2951 		mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2952 		/*
2953 		 * Leave 4 and 5 alone
2954 		 */
2955 		mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2956 		mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2957 		if (isp_fabric_mbox_cmd(isp, &mbs)) {
2958 			if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
2959 				fcp->isp_loopstate = LOOP_PDB_RCVD;
2960 			}
2961 			FC_SCRATCH_RELEASE(isp);
2962 			return (-1);
2963 		}
2964 		if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2965 			FC_SCRATCH_RELEASE(isp);
2966 			return (-1);
2967 		}
2968 		MEMORYBARRIER(isp, SYNC_SFORCPU, GXOFF, SNS_GFF_ID_RESP_SIZE);
2969 		fs0 = (sns_gff_id_rsp_t *) ((uint8_t *)fcp->isp_scratch+GXOFF);
2970 		isp_get_gff_id_response(isp, fs0, fs1);
2971 		if (fs1->snscb_cthdr.ct_response != FS_ACC) {
2972 			isp_prt(isp, /* ISP_LOGDEBUG0 */ ISP_LOGWARN,
2973 			    swrej, "GFF_ID",
2974 			    fs1->snscb_cthdr.ct_reason,
2975 			    fs1->snscb_cthdr.ct_explanation, lcl.portid);
2976 			if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2977 				FC_SCRATCH_RELEASE(isp);
2978 				return (-1);
2979 			}
2980 		} else {
2981 			int index = (ftype >> 3);
2982 			int bshft = (ftype & 0x7) * 4;
2983 			int fc4_fval =
2984 			    (fs1->snscb_fc4_features[index] >> bshft) & 0xf;
2985 			if (fc4_fval & 0x1) {
2986 				lcl.roles |=
2987 				    (SVC3_INI_ROLE >> SVC3_ROLE_SHIFT);
2988 			}
2989 			if (fc4_fval & 0x2) {
2990 				lcl.roles |=
2991 				    (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT);
2992 			}
2993 		}
2994 #endif
2995 
2996 		/*
2997 		 * If we really want to know what kind of port type this is,
2998 		 * we have to run another CT command. Otherwise, we'll leave
2999 		 * it as undefined.
3000 		 *
3001 		lcl.port_type = 0;
3002 		 */
3003 		if (rs1->snscb_ports[i].control & 0x80) {
3004 			lcl.last_fabric_dev = 1;
3005 		} else {
3006 			lcl.last_fabric_dev = 0;
3007 		}
3008 		(void) isp_async(isp, ISPASYNC_FABRIC_DEV, &lcl);
3009 
3010 	} while ((rs1->snscb_ports[i].control & 0x80) == 0 && i < NGENT-1);
3011 
3012 	/*
3013 	 * If we're not at the last entry, our list isn't big enough.
3014 	 */
3015 	if ((rs1->snscb_ports[i].control & 0x80) == 0) {
3016 		isp_prt(isp, ISP_LOGWARN, "fabric too big for scratch area");
3017 	}
3018 
3019 	FC_SCRATCH_RELEASE(isp);
3020 	fcp->isp_loopstate = LOOP_FSCAN_DONE;
3021 	return (0);
3022 }
3023 #endif
3024 
3025 static void
3026 isp_register_fc4_type(ispsoftc_t *isp)
3027 {
3028 	fcparam *fcp = isp->isp_param;
3029 	uint8_t local[SNS_RFT_ID_REQ_SIZE];
3030 	sns_screq_t *reqp = (sns_screq_t *) local;
3031 	mbreg_t mbs;
3032 
3033 	MEMZERO((void *) reqp, SNS_RFT_ID_REQ_SIZE);
3034 	reqp->snscb_rblen = SNS_RFT_ID_RESP_SIZE >> 1;
3035 	reqp->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma + 0x100);
3036 	reqp->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma + 0x100);
3037 	reqp->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma + 0x100);
3038 	reqp->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma + 0x100);
3039 	reqp->snscb_sblen = 22;
3040 	reqp->snscb_data[0] = SNS_RFT_ID;
3041 	reqp->snscb_data[4] = fcp->isp_portid & 0xffff;
3042 	reqp->snscb_data[5] = (fcp->isp_portid >> 16) & 0xff;
3043 	reqp->snscb_data[6] = (1 << FC4_SCSI);
3044 #if	0
3045 	reqp->snscb_data[6] |= (1 << FC4_IP);	/* ISO/IEC 8802-2 LLC/SNAP */
3046 #endif
3047 	FC_SCRATCH_ACQUIRE(isp);
3048 	isp_put_sns_request(isp, reqp, (sns_screq_t *) fcp->isp_scratch);
3049 	MEMZERO(&mbs, sizeof (mbs));
3050 	mbs.param[0] = MBOX_SEND_SNS;
3051 	mbs.param[1] = SNS_RFT_ID_REQ_SIZE >> 1;
3052 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
3053 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
3054 	/*
3055 	 * Leave 4 and 5 alone
3056 	 */
3057 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
3058 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
3059 	isp_mboxcmd(isp, &mbs, MBLOGALL);
3060 	FC_SCRATCH_RELEASE(isp);
3061 	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
3062 		isp_prt(isp, ISP_LOGDEBUG0, "Register FC4 types succeeded");
3063 	}
3064 }
3065 
3066 /*
3067  * Start a command. Locking is assumed done in the caller.
3068  */
3069 
3070 int
3071 isp_start(XS_T *xs)
3072 {
3073 	ispsoftc_t *isp;
3074 	uint16_t nxti, optr, handle;
3075 	uint8_t local[QENTRY_LEN];
3076 	ispreq_t *reqp, *qep;
3077 	int target, i;
3078 
3079 	XS_INITERR(xs);
3080 	isp = XS_ISP(xs);
3081 
3082 	/*
3083 	 * Check to make sure we're supporting initiator role.
3084 	 */
3085 	if ((isp->isp_role & ISP_ROLE_INITIATOR) == 0) {
3086 		XS_SETERR(xs, HBA_SELTIMEOUT);
3087 		return (CMD_COMPLETE);
3088 	}
3089 
3090 	/*
3091 	 * Now make sure we're running.
3092 	 */
3093 
3094 	if (isp->isp_state != ISP_RUNSTATE) {
3095 		isp_prt(isp, ISP_LOGERR, "Adapter not at RUNSTATE");
3096 		XS_SETERR(xs, HBA_BOTCH);
3097 		return (CMD_COMPLETE);
3098 	}
3099 
3100 	/*
3101 	 * Check command CDB length, etc.. We really are limited to 16 bytes
3102 	 * for Fibre Channel, but can do up to 44 bytes in parallel SCSI,
3103 	 * but probably only if we're running fairly new firmware (we'll
3104 	 * let the old f/w choke on an extended command queue entry).
3105 	 */
3106 
3107 	if (XS_CDBLEN(xs) > (IS_FC(isp)? 16 : 44) || XS_CDBLEN(xs) == 0) {
3108 		isp_prt(isp, ISP_LOGERR,
3109 		    "unsupported cdb length (%d, CDB[0]=0x%x)",
3110 		    XS_CDBLEN(xs), XS_CDBP(xs)[0] & 0xff);
3111 		XS_SETERR(xs, HBA_BOTCH);
3112 		return (CMD_COMPLETE);
3113 	}
3114 
3115 	/*
3116 	 * Check to see whether we have good firmware state still or
3117 	 * need to refresh our port database for this target.
3118 	 */
3119 	target = XS_TGT(xs);
3120 	if (IS_FC(isp)) {
3121 		fcparam *fcp = isp->isp_param;
3122 		struct lportdb *lp;
3123 #ifdef	HANDLE_LOOPSTATE_IN_OUTER_LAYERS
3124 		if (fcp->isp_fwstate != FW_READY ||
3125 		    fcp->isp_loopstate != LOOP_READY) {
3126 			return (CMD_RQLATER);
3127 		}
3128 
3129 		/*
3130 		 * If we're not on a Fabric, we can't have a target
3131 		 * above FL_PORT_ID-1.
3132 		 *
3133 		 * If we're on a fabric and *not* connected as an F-port,
3134 		 * we can't have a target less than FC_SNS_ID+1. This
3135 		 * keeps us from having to sort out the difference between
3136 		 * local public loop devices and those which we might get
3137 		 * from a switch's database.
3138 		 */
3139 		if (fcp->isp_onfabric == 0) {
3140 			if (target >= FL_PORT_ID) {
3141 				XS_SETERR(xs, HBA_SELTIMEOUT);
3142 				return (CMD_COMPLETE);
3143 			}
3144 		} else {
3145 			if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
3146 				XS_SETERR(xs, HBA_SELTIMEOUT);
3147 				return (CMD_COMPLETE);
3148 			}
3149 			/*
3150 			 * We used to exclude having local loop ports
3151 			 * at the same time that we have fabric ports.
3152 			 * That is, we used to exclude having ports
3153 			 * at < FL_PORT_ID if we're FL-port.
3154 			 *
3155 			 * That's wrong. The only thing that could be
3156 			 * dicey is if the switch you're connected to
3157 			 * has these local loop ports appear on the
3158 			 * fabric and we somehow attach them twice.
3159 			 */
3160 		}
3161 #else
3162 		/*
3163 		 * Check for f/w being in ready state. If the f/w
3164 		 * isn't in ready state, then we don't know our
3165 		 * loop ID and the f/w hasn't completed logging
3166 		 * into all targets on the loop. If this is the
3167 		 * case, then bounce the command. We pretend this is
3168 		 * a SELECTION TIMEOUT error if we've never gone to
3169 		 * FW_READY state at all- in this case we may not
3170 		 * be hooked to a loop at all and we shouldn't hang
3171 		 * the machine for this. Otherwise, defer this command
3172 		 * until later.
3173 		 */
3174 		if (fcp->isp_fwstate != FW_READY) {
3175 			/*
3176 			 * Give ourselves at most a 250ms delay.
3177 			 */
3178 			if (isp_fclink_test(isp, 250000)) {
3179 				XS_SETERR(xs, HBA_SELTIMEOUT);
3180 				if (fcp->loop_seen_once) {
3181 					return (CMD_RQLATER);
3182 				} else {
3183 					return (CMD_COMPLETE);
3184 				}
3185 			}
3186 		}
3187 
3188 		/*
3189 		 * If we're not on a Fabric, we can't have a target
3190 		 * above FL_PORT_ID-1.
3191 		 *
3192 		 * If we're on a fabric and *not* connected as an F-port,
3193 		 * we can't have a target less than FC_SNS_ID+1. This
3194 		 * keeps us from having to sort out the difference between
3195 		 * local public loop devices and those which we might get
3196 		 * from a switch's database.
3197 		 */
3198 		if (fcp->isp_onfabric == 0) {
3199 			if (target >= FL_PORT_ID) {
3200 				XS_SETERR(xs, HBA_SELTIMEOUT);
3201 				return (CMD_COMPLETE);
3202 			}
3203 		} else {
3204 			if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
3205 				XS_SETERR(xs, HBA_SELTIMEOUT);
3206 				return (CMD_COMPLETE);
3207 			}
3208 			if (fcp->isp_topo != TOPO_F_PORT &&
3209 			    target < FL_PORT_ID) {
3210 				XS_SETERR(xs, HBA_SELTIMEOUT);
3211 				return (CMD_COMPLETE);
3212 			}
3213 		}
3214 
3215 		/*
3216 		 * If our loop state is such that we haven't yet received
3217 		 * a "Port Database Changed" notification (after a LIP or
3218 		 * a Loop Reset or firmware initialization), then defer
3219 		 * sending commands for a little while, but only if we've
3220 		 * seen a valid loop at one point (otherwise we can get
3221 		 * stuck at initialization time).
3222 		 */
3223 		if (fcp->isp_loopstate < LOOP_PDB_RCVD) {
3224 			XS_SETERR(xs, HBA_SELTIMEOUT);
3225 			if (fcp->loop_seen_once) {
3226 				return (CMD_RQLATER);
3227 			} else {
3228 				return (CMD_COMPLETE);
3229 			}
3230 		}
3231 
3232 		/*
3233 		 * If we're in the middle of loop or fabric scanning
3234 		 * or merging the port databases, retry this command later.
3235 		 */
3236 		if (fcp->isp_loopstate == LOOP_SCANNING_FABRIC ||
3237 		    fcp->isp_loopstate == LOOP_SCANNING_LOOP ||
3238 		    fcp->isp_loopstate == LOOP_SYNCING_PDB) {
3239 			return (CMD_RQLATER);
3240 		}
3241 
3242 		/*
3243 		 * If our loop state is now such that we've just now
3244 		 * received a Port Database Change notification, then
3245 		 * we have to go off and (re)scan the fabric. We back
3246 		 * out and try again later if this doesn't work.
3247 		 */
3248 		if (fcp->isp_loopstate == LOOP_PDB_RCVD && fcp->isp_onfabric) {
3249 			if (isp_scan_fabric(isp, FC4_SCSI)) {
3250 				return (CMD_RQLATER);
3251 			}
3252 			if (fcp->isp_fwstate != FW_READY ||
3253 			    fcp->isp_loopstate < LOOP_FSCAN_DONE) {
3254 				return (CMD_RQLATER);
3255 			}
3256 		}
3257 
3258 		/*
3259 		 * If our loop state is now such that we've just now
3260 		 * received a Port Database Change notification, then
3261 		 * we have to go off and (re)synchronize our port
3262 		 * database.
3263 		 */
3264 		if (fcp->isp_loopstate < LOOP_READY) {
3265 			if (isp_pdb_sync(isp)) {
3266 				return (CMD_RQLATER);
3267 			}
3268 			if (fcp->isp_fwstate != FW_READY ||
3269 			    fcp->isp_loopstate != LOOP_READY) {
3270 				return (CMD_RQLATER);
3271 			}
3272 		}
3273 
3274 		/*
3275 		 * XXX: Here's were we would cancel any loop_dead flag
3276 		 * XXX: also cancel in dead_loop timeout that's running
3277 		 */
3278 #endif
3279 
3280 		/*
3281 		 * Now check whether we should even think about pursuing this.
3282 		 */
3283 		lp = &fcp->portdb[target];
3284 		if (lp->valid == 0) {
3285 			XS_SETERR(xs, HBA_SELTIMEOUT);
3286 			return (CMD_COMPLETE);
3287 		}
3288 		if ((lp->roles & (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT)) == 0) {
3289 			isp_prt(isp, ISP_LOGDEBUG2,
3290 			    "Target %d does not have target service", target);
3291 			XS_SETERR(xs, HBA_SELTIMEOUT);
3292 			return (CMD_COMPLETE);
3293 		}
3294 		/*
3295 		 * Now turn target into what the actual Loop ID is.
3296 		 */
3297 		target = lp->loopid;
3298 	}
3299 
3300 	/*
3301 	 * Next check to see if any HBA or Device
3302 	 * parameters need to be updated.
3303 	 */
3304 	if (isp->isp_update != 0) {
3305 		isp_update(isp);
3306 	}
3307 
3308 	if (isp_getrqentry(isp, &nxti, &optr, (void *)&qep)) {
3309 		isp_prt(isp, ISP_LOGDEBUG0, "Request Queue Overflow");
3310 		XS_SETERR(xs, HBA_BOTCH);
3311 		return (CMD_EAGAIN);
3312 	}
3313 
3314 	/*
3315 	 * Now see if we need to synchronize the ISP with respect to anything.
3316 	 * We do dual duty here (cough) for synchronizing for busses other
3317 	 * than which we got here to send a command to.
3318 	 */
3319 	reqp = (ispreq_t *) local;
3320 	if (isp->isp_sendmarker) {
3321 		uint8_t n = (IS_DUALBUS(isp)? 2: 1);
3322 		/*
3323 		 * Check ports to send markers for...
3324 		 */
3325 		for (i = 0; i < n; i++) {
3326 			if ((isp->isp_sendmarker & (1 << i)) == 0) {
3327 				continue;
3328 			}
3329 			MEMZERO((void *) reqp, QENTRY_LEN);
3330 			reqp->req_header.rqs_entry_count = 1;
3331 			reqp->req_header.rqs_entry_type = RQSTYPE_MARKER;
3332 			reqp->req_modifier = SYNC_ALL;
3333 			reqp->req_target = i << 7;	/* insert bus number */
3334 			isp_put_request(isp, reqp, qep);
3335 			ISP_ADD_REQUEST(isp, nxti);
3336 			isp->isp_sendmarker &= ~(1 << i);
3337 			if (isp_getrqentry(isp, &nxti, &optr, (void *) &qep)) {
3338 				isp_prt(isp, ISP_LOGDEBUG0,
3339 				    "Request Queue Overflow+");
3340 				XS_SETERR(xs, HBA_BOTCH);
3341 				return (CMD_EAGAIN);
3342 			}
3343 		}
3344 	}
3345 
3346 	MEMZERO((void *)reqp, QENTRY_LEN);
3347 	reqp->req_header.rqs_entry_count = 1;
3348 	if (IS_FC(isp)) {
3349 		reqp->req_header.rqs_entry_type = RQSTYPE_T2RQS;
3350 	} else {
3351 		if (XS_CDBLEN(xs) > 12)
3352 			reqp->req_header.rqs_entry_type = RQSTYPE_CMDONLY;
3353 		else
3354 			reqp->req_header.rqs_entry_type = RQSTYPE_REQUEST;
3355 	}
3356 	/* reqp->req_header.rqs_flags = 0; */
3357 	/* reqp->req_header.rqs_seqno = 0; */
3358 	if (IS_FC(isp)) {
3359 		/*
3360 		 * See comment in isp_intr
3361 		 */
3362 		/* XS_RESID(xs) = 0; */
3363 
3364 		/*
3365 		 * Fibre Channel always requires some kind of tag.
3366 		 * The Qlogic drivers seem be happy not to use a tag,
3367 		 * but this breaks for some devices (IBM drives).
3368 		 */
3369 		if (XS_TAG_P(xs)) {
3370 			((ispreqt2_t *)reqp)->req_flags = XS_TAG_TYPE(xs);
3371 		} else {
3372 			/*
3373 			 * If we don't know what tag to use, use HEAD OF QUEUE
3374 			 * for Request Sense or Simple.
3375 			 */
3376 			if (XS_CDBP(xs)[0] == 0x3)	/* REQUEST SENSE */
3377 				((ispreqt2_t *)reqp)->req_flags = REQFLAG_HTAG;
3378 			else
3379 				((ispreqt2_t *)reqp)->req_flags = REQFLAG_STAG;
3380 		}
3381 	} else {
3382 		sdparam *sdp = (sdparam *)isp->isp_param;
3383 		sdp += XS_CHANNEL(xs);
3384 		if ((sdp->isp_devparam[target].actv_flags & DPARM_TQING) &&
3385 		    XS_TAG_P(xs)) {
3386 			reqp->req_flags = XS_TAG_TYPE(xs);
3387 		}
3388 	}
3389 	if (IS_SCSI(isp)) {
3390 		reqp->req_target = target | (XS_CHANNEL(xs) << 7);
3391 		reqp->req_lun_trn = XS_LUN(xs);
3392 		reqp->req_cdblen = XS_CDBLEN(xs);
3393 	} else if (IS_2KLOGIN(isp)) {
3394 		((ispreqt2e_t *)reqp)->req_target = target;
3395 		((ispreqt2e_t *)reqp)->req_scclun = XS_LUN(xs);
3396 	} else if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) {
3397 		((ispreqt2_t *)reqp)->req_target = target;
3398 		((ispreqt2_t *)reqp)->req_scclun = XS_LUN(xs);
3399 	} else {
3400 		((ispreqt2_t *)reqp)->req_target = target;
3401 		((ispreqt2_t *)reqp)->req_lun_trn = XS_LUN(xs);
3402 	}
3403 	MEMCPY(reqp->req_cdb, XS_CDBP(xs), XS_CDBLEN(xs));
3404 
3405 	reqp->req_time = XS_TIME(xs) / 1000;
3406 	if (reqp->req_time == 0 && XS_TIME(xs)) {
3407 		reqp->req_time = 1;
3408 	}
3409 
3410 	if (isp_save_xs(isp, xs, &handle)) {
3411 		isp_prt(isp, ISP_LOGDEBUG0, "out of xflist pointers");
3412 		XS_SETERR(xs, HBA_BOTCH);
3413 		return (CMD_EAGAIN);
3414 	}
3415 	reqp->req_handle = handle;
3416 
3417 	/*
3418 	 * Set up DMA and/or do any bus swizzling of the request entry
3419 	 * so that the Qlogic F/W understands what is being asked of it.
3420 	 */
3421 	i = ISP_DMASETUP(isp, xs, reqp, &nxti, optr);
3422 	if (i != CMD_QUEUED) {
3423 		isp_destroy_handle(isp, handle);
3424 		/*
3425 		 * dmasetup sets actual error in packet, and
3426 		 * return what we were given to return.
3427 		 */
3428 		return (i);
3429 	}
3430 	XS_SETERR(xs, HBA_NOERROR);
3431 	isp_prt(isp, ISP_LOGDEBUG2,
3432 	    "START cmd for %d.%d.%d cmd 0x%x datalen %ld",
3433 	    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), XS_CDBP(xs)[0],
3434 	    (long) XS_XFRLEN(xs));
3435 	ISP_ADD_REQUEST(isp, nxti);
3436 	isp->isp_nactive++;
3437 	return (CMD_QUEUED);
3438 }
3439 
3440 /*
3441  * isp control
3442  * Locks (ints blocked) assumed held.
3443  */
3444 
3445 int
3446 isp_control(ispsoftc_t *isp, ispctl_t ctl, void *arg)
3447 {
3448 	XS_T *xs;
3449 	mbreg_t mbs;
3450 	int bus, tgt;
3451 	uint16_t handle;
3452 
3453 	MEMZERO(&mbs, sizeof (mbs));
3454 
3455 	switch (ctl) {
3456 	default:
3457 		isp_prt(isp, ISP_LOGERR, "Unknown Control Opcode 0x%x", ctl);
3458 		break;
3459 
3460 	case ISPCTL_RESET_BUS:
3461 		/*
3462 		 * Issue a bus reset.
3463 		 */
3464 		mbs.param[0] = MBOX_BUS_RESET;
3465 		mbs.param[2] = 0;
3466 		if (IS_SCSI(isp)) {
3467 			mbs.param[1] =
3468 			    ((sdparam *) isp->isp_param)->isp_bus_reset_delay;
3469 			if (mbs.param[1] < 2) {
3470 				mbs.param[1] = 2;
3471 			}
3472 			bus = *((int *) arg);
3473 			if (IS_DUALBUS(isp)) {
3474 				mbs.param[2] = bus;
3475 			}
3476 		} else {
3477 			mbs.param[1] = 10;
3478 			bus = 0;
3479 		}
3480 		isp->isp_sendmarker |= (1 << bus);
3481 		isp_mboxcmd(isp, &mbs, MBLOGALL);
3482 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3483 			break;
3484 		}
3485 		isp_prt(isp, ISP_LOGINFO,
3486 		    "driver initiated bus reset of bus %d", bus);
3487 		return (0);
3488 
3489 	case ISPCTL_RESET_DEV:
3490 		tgt = (*((int *) arg)) & 0xffff;
3491 		bus = (*((int *) arg)) >> 16;
3492 		mbs.param[0] = MBOX_ABORT_TARGET;
3493 		if (IS_SCSI(isp)) {
3494 			mbs.param[1] = (tgt << 8) | (bus << 15);
3495 		} else {
3496 			if (IS_2KLOGIN(isp)) {
3497 				mbs.param[1] = tgt;
3498 				mbs.obits |= (1 << 10);
3499 			} else {
3500 				mbs.param[1] = (tgt << 8);
3501 			}
3502 		}
3503 		mbs.param[2] = 3;	/* 'delay', in seconds */
3504 		isp_mboxcmd(isp, &mbs, MBLOGALL);
3505 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3506 			break;
3507 		}
3508 		isp_prt(isp, ISP_LOGINFO,
3509 		    "Target %d on Bus %d Reset Succeeded", tgt, bus);
3510 		isp->isp_sendmarker |= (1 << bus);
3511 		return (0);
3512 
3513 	case ISPCTL_ABORT_CMD:
3514 		xs = (XS_T *) arg;
3515 		tgt = XS_TGT(xs);
3516 		handle = isp_find_handle(isp, xs);
3517 		if (handle == 0) {
3518 			isp_prt(isp, ISP_LOGWARN,
3519 			    "cannot find handle for command to abort");
3520 			break;
3521 		}
3522 		bus = XS_CHANNEL(xs);
3523 		mbs.param[0] = MBOX_ABORT;
3524 		if (IS_FC(isp)) {
3525 			if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN)  {
3526 				if (IS_2KLOGIN(isp)) {
3527 					mbs.param[1] = tgt;
3528 				} else {
3529 					mbs.param[1] = tgt << 8;
3530 				}
3531 				mbs.param[4] = 0;
3532 				mbs.param[5] = 0;
3533 				mbs.param[6] = XS_LUN(xs);
3534 			} else {
3535 				mbs.param[1] = tgt << 8 | XS_LUN(xs);
3536 			}
3537 		} else {
3538 			mbs.param[1] =
3539 			    (bus << 15) | (XS_TGT(xs) << 8) | XS_LUN(xs);
3540 		}
3541 		mbs.param[3] = 0;
3542 		mbs.param[2] = handle;
3543 		isp_mboxcmd(isp, &mbs, MBLOGALL & ~MBOX_COMMAND_ERROR);
3544 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
3545 			return (0);
3546 		}
3547 		/*
3548 		 * XXX: Look for command in the REQUEST QUEUE. That is,
3549 		 * XXX: It hasen't been picked up by firmware yet.
3550 		 */
3551 		break;
3552 
3553 	case ISPCTL_UPDATE_PARAMS:
3554 
3555 		isp_update(isp);
3556 		return (0);
3557 
3558 	case ISPCTL_FCLINK_TEST:
3559 
3560 		if (IS_FC(isp)) {
3561 			int usdelay = *((int *) arg);
3562 			if (usdelay == 0) {
3563 				usdelay =  250000;
3564 			}
3565 			return (isp_fclink_test(isp, usdelay));
3566 		}
3567 		break;
3568 
3569 	case ISPCTL_SCAN_FABRIC:
3570 
3571 		if (IS_FC(isp)) {
3572 			int ftype = (arg)? *((int *) arg) : FC4_SCSI;
3573 			return (isp_scan_fabric(isp, ftype));
3574 		}
3575 		break;
3576 
3577 	case ISPCTL_SCAN_LOOP:
3578 
3579 		if (IS_FC(isp)) {
3580 			return (isp_scan_loop(isp));
3581 		}
3582 		break;
3583 
3584 	case ISPCTL_PDB_SYNC:
3585 
3586 		if (IS_FC(isp)) {
3587 			return (isp_pdb_sync(isp));
3588 		}
3589 		break;
3590 
3591 	case ISPCTL_SEND_LIP:
3592 
3593 		if (IS_FC(isp)) {
3594 			mbs.param[0] = MBOX_INIT_LIP;
3595 			if (IS_2KLOGIN(isp)) {
3596 				mbs.obits |= (1 << 10);
3597 			}
3598 			isp_mboxcmd(isp, &mbs, MBLOGALL);
3599 			if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
3600 				return (0);
3601 			}
3602 		}
3603 		break;
3604 
3605 	case ISPCTL_GET_POSMAP:
3606 
3607 		if (IS_FC(isp) && arg) {
3608 			return (isp_getmap(isp, arg));
3609 		}
3610 		break;
3611 
3612 
3613 	case ISPCTL_GET_PDB:
3614 		if (IS_FC(isp) && arg) {
3615 			int id = *((int *)arg);
3616 			isp_pdb_t *pdb = arg;
3617 			return (isp_getpdb(isp, id, pdb));
3618 		}
3619 		break;
3620 
3621 	case ISPCTL_RUN_MBOXCMD:
3622 
3623 		isp_mboxcmd(isp, arg, MBLOGALL);
3624 		return(0);
3625 
3626 #ifdef	ISP_TARGET_MODE
3627 	case ISPCTL_TOGGLE_TMODE:
3628 	{
3629 
3630 		/*
3631 		 * We don't check/set against role here- that's the
3632 		 * responsibility for the outer layer to coordinate.
3633 		 */
3634 		if (IS_SCSI(isp)) {
3635 			int param = *(int *)arg;
3636 			mbs.param[0] = MBOX_ENABLE_TARGET_MODE;
3637 			mbs.param[1] = param & 0xffff;
3638 			mbs.param[2] = param >> 16;
3639 			isp_mboxcmd(isp, &mbs, MBLOGALL);
3640 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3641 				break;
3642 			}
3643 		}
3644 		return (0);
3645 	}
3646 #endif
3647 	}
3648 	return (-1);
3649 }
3650 
3651 /*
3652  * Interrupt Service Routine(s).
3653  *
3654  * External (OS) framework has done the appropriate locking,
3655  * and the locking will be held throughout this function.
3656  */
3657 
3658 /*
3659  * Limit our stack depth by sticking with the max likely number
3660  * of completions on a request queue at any one time.
3661  */
3662 #ifndef	MAX_REQUESTQ_COMPLETIONS
3663 #define	MAX_REQUESTQ_COMPLETIONS	64
3664 #endif
3665 
3666 void
3667 isp_intr(ispsoftc_t *isp, uint16_t isr, uint16_t sema, uint16_t mbox)
3668 {
3669 	XS_T *complist[MAX_REQUESTQ_COMPLETIONS], *xs;
3670 	uint16_t iptr, optr, junk;
3671 	int i, nlooked = 0, ndone = 0;
3672 
3673 again:
3674 	/*
3675 	 * Is this a mailbox related interrupt?
3676 	 * The mailbox semaphore will be nonzero if so.
3677 	 */
3678 	if (sema) {
3679 		if (mbox & 0x4000) {
3680 			isp->isp_intmboxc++;
3681 			if (isp->isp_mboxbsy) {
3682 				int i = 0, obits = isp->isp_obits;
3683 				isp->isp_mboxtmp[i++] = mbox;
3684 				for (i = 1; i < MAX_MAILBOX(isp); i++) {
3685 					if ((obits & (1 << i)) == 0) {
3686 						continue;
3687 					}
3688 					isp->isp_mboxtmp[i] =
3689 					    ISP_READ(isp, MBOX_OFF(i));
3690 				}
3691 				if (isp->isp_mbxwrk0) {
3692 					if (isp_mbox_continue(isp) == 0) {
3693 						return;
3694 					}
3695 				}
3696 				MBOX_NOTIFY_COMPLETE(isp);
3697 			} else {
3698 				isp_prt(isp, ISP_LOGWARN,
3699 				    "Mbox Command Async (0x%x) with no waiters",
3700 				    mbox);
3701 			}
3702 		} else if (isp_parse_async(isp, mbox) < 0) {
3703 			return;
3704 		}
3705 		if ((IS_FC(isp) && mbox != ASYNC_RIO_RESP) ||
3706 		    isp->isp_state != ISP_RUNSTATE) {
3707 			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3708 			ISP_WRITE(isp, BIU_SEMA, 0);
3709 			return;
3710 		}
3711 	}
3712 
3713 	/*
3714 	 * We can't be getting this now.
3715 	 */
3716 	if (isp->isp_state != ISP_RUNSTATE) {
3717 		isp_prt(isp, ISP_LOGWARN,
3718 		    "interrupt (ISR=%x SEMA=%x) when not ready", isr, sema);
3719 		/*
3720 		 * Thank you very much!  *Burrrp*!
3721 		 */
3722 		WRITE_RESPONSE_QUEUE_OUT_POINTER(isp,
3723 		    READ_RESPONSE_QUEUE_IN_POINTER(isp));
3724 
3725 		ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3726 		ISP_WRITE(isp, BIU_SEMA, 0);
3727 		return;
3728 	}
3729 
3730 	/*
3731 	 * Get the current Response Queue Out Pointer.
3732 	 *
3733 	 * If we're a 2300, we can ask what hardware what it thinks.
3734 	 */
3735 	if (IS_23XX(isp)) {
3736 		optr = ISP_READ(isp, isp->isp_respoutrp);
3737 		/*
3738 		 * Debug: to be taken out eventually
3739 		 */
3740 		if (isp->isp_residx != optr) {
3741 			isp_prt(isp, ISP_LOGWARN, "optr %x soft optr %x",
3742 			    optr, isp->isp_residx);
3743 		}
3744 	} else {
3745 		optr = isp->isp_residx;
3746 	}
3747 
3748 	/*
3749 	 * You *must* read the Response Queue In Pointer
3750 	 * prior to clearing the RISC interrupt.
3751 	 *
3752 	 * Debounce the 2300 if revision less than 2.
3753 	 */
3754 	if (IS_2100(isp) || (IS_2300(isp) && isp->isp_revision < 2)) {
3755 		i = 0;
3756 		do {
3757 			iptr = READ_RESPONSE_QUEUE_IN_POINTER(isp);
3758 			junk = READ_RESPONSE_QUEUE_IN_POINTER(isp);
3759 		} while (junk != iptr && ++i < 1000);
3760 
3761 		if (iptr != junk) {
3762 			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3763 			isp_prt(isp, ISP_LOGWARN,
3764 			    "Response Queue Out Pointer Unstable (%x, %x)",
3765 			    iptr, junk);
3766 			return;
3767 		}
3768 	} else {
3769 		iptr = READ_RESPONSE_QUEUE_IN_POINTER(isp);
3770 	}
3771 	isp->isp_resodx = iptr;
3772 
3773 
3774 	if (optr == iptr && sema == 0) {
3775 		/*
3776 		 * There are a lot of these- reasons unknown- mostly on
3777 		 * faster Alpha machines.
3778 		 *
3779 		 * I tried delaying after writing HCCR_CMD_CLEAR_RISC_INT to
3780 		 * make sure the old interrupt went away (to avoid 'ringing'
3781 		 * effects), but that didn't stop this from occurring.
3782 		 */
3783 		if (IS_23XX(isp)) {
3784 			USEC_DELAY(100);
3785 			iptr = READ_RESPONSE_QUEUE_IN_POINTER(isp);
3786 			junk = ISP_READ(isp, BIU_R2HSTSLO);
3787 		} else {
3788 			junk = ISP_READ(isp, BIU_ISR);
3789 		}
3790 		if (optr == iptr) {
3791 			if (IS_23XX(isp)) {
3792 				;
3793 			} else {
3794 				sema = ISP_READ(isp, BIU_SEMA);
3795 				mbox = ISP_READ(isp, OUTMAILBOX0);
3796 				if ((sema & 0x3) && (mbox & 0x8000)) {
3797 					goto again;
3798 				}
3799 			}
3800 			isp->isp_intbogus++;
3801 			isp_prt(isp, ISP_LOGDEBUG1,
3802 			    "bogus intr- isr %x (%x) iptr %x optr %x",
3803 			    isr, junk, iptr, optr);
3804 		}
3805 	}
3806 	isp->isp_resodx = iptr;
3807 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3808 	ISP_WRITE(isp, BIU_SEMA, 0);
3809 
3810 	if (isp->isp_rspbsy) {
3811 		return;
3812 	}
3813 	isp->isp_rspbsy = 1;
3814 
3815 	while (optr != iptr) {
3816 		ispstatusreq_t local, *sp = &local;
3817 		isphdr_t *hp;
3818 		int type;
3819 		uint16_t oop;
3820 		int buddaboom = 0;
3821 
3822 		hp = (isphdr_t *) ISP_QUEUE_ENTRY(isp->isp_result, optr);
3823 		oop = optr;
3824 		optr = ISP_NXT_QENTRY(optr, RESULT_QUEUE_LEN(isp));
3825 		nlooked++;
3826  read_again:
3827 		/*
3828 		 * Synchronize our view of this response queue entry.
3829 		 */
3830 		MEMORYBARRIER(isp, SYNC_RESULT, oop, QENTRY_LEN);
3831 
3832 		type = isp_get_response_type(isp, hp);
3833 
3834 		if (type == RQSTYPE_RESPONSE) {
3835 			isp_get_response(isp, (ispstatusreq_t *) hp, sp);
3836 		} else if (type == RQSTYPE_RIO2) {
3837 			isp_rio2_t rio;
3838 			isp_get_rio2(isp, (isp_rio2_t *) hp, &rio);
3839 			for (i = 0; i < rio.req_header.rqs_seqno; i++) {
3840 				isp_fastpost_complete(isp, rio.req_handles[i]);
3841 			}
3842 			if (isp->isp_fpcchiwater < rio.req_header.rqs_seqno)
3843 				isp->isp_fpcchiwater = rio.req_header.rqs_seqno;
3844 			MEMZERO(hp, QENTRY_LEN);	/* PERF */
3845 			continue;
3846 		} else {
3847 			/*
3848 			 * Somebody reachable via isp_handle_other_response
3849 			 * may have updated the response queue pointers for
3850 			 * us, so we reload our goal index.
3851 			 */
3852 			int i = isp_handle_other_response(isp, type, hp, &optr);
3853 			if (i < 0) {
3854 				goto read_again;
3855 			} else if (i > 0) {
3856 				iptr = isp->isp_resodx;
3857 				MEMZERO(hp, QENTRY_LEN);	/* PERF */
3858 				continue;
3859 			}
3860 
3861 			/*
3862 			 * After this point, we'll just look at the header as
3863 			 * we don't know how to deal with the rest of the
3864 			 * response.
3865 			 */
3866 			isp_get_response(isp, (ispstatusreq_t *) hp, sp);
3867 
3868 			/*
3869 			 * It really has to be a bounced request just copied
3870 			 * from the request queue to the response queue. If
3871 			 * not, something bad has happened.
3872 			 */
3873 			if (sp->req_header.rqs_entry_type != RQSTYPE_REQUEST) {
3874 				isp_prt(isp, ISP_LOGERR, notresp,
3875 				    sp->req_header.rqs_entry_type, oop, optr,
3876 				    nlooked);
3877 				if (isp->isp_dblev & ISP_LOGDEBUG0) {
3878 					isp_print_bytes(isp, "Queue Entry",
3879 					    QENTRY_LEN, sp);
3880 				}
3881 				MEMZERO(hp, QENTRY_LEN);	/* PERF */
3882 				continue;
3883 			}
3884 			buddaboom = 1;
3885 		}
3886 
3887 		if (sp->req_header.rqs_flags & 0xf) {
3888 #define	_RQS_OFLAGS	\
3889 	~(RQSFLAG_CONTINUATION|RQSFLAG_FULL|RQSFLAG_BADHEADER|RQSFLAG_BADPACKET)
3890 			if (sp->req_header.rqs_flags & RQSFLAG_CONTINUATION) {
3891 				isp_prt(isp, ISP_LOGWARN,
3892 				    "continuation segment");
3893 				WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
3894 				continue;
3895 			}
3896 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
3897 				isp_prt(isp, ISP_LOGDEBUG1,
3898 				    "internal queues full");
3899 				/*
3900 				 * We'll synthesize a QUEUE FULL message below.
3901 				 */
3902 			}
3903 			if (sp->req_header.rqs_flags & RQSFLAG_BADHEADER) {
3904 				isp_prt(isp, ISP_LOGERR,  "bad header flag");
3905 				buddaboom++;
3906 			}
3907 			if (sp->req_header.rqs_flags & RQSFLAG_BADPACKET) {
3908 				isp_prt(isp, ISP_LOGERR, "bad request packet");
3909 				buddaboom++;
3910 			}
3911 			if (sp->req_header.rqs_flags & _RQS_OFLAGS) {
3912 				isp_prt(isp, ISP_LOGERR,
3913 				    "unknown flags (0x%x) in response",
3914 				    sp->req_header.rqs_flags);
3915 				buddaboom++;
3916 			}
3917 #undef	_RQS_OFLAGS
3918 		}
3919 		if (sp->req_handle > isp->isp_maxcmds || sp->req_handle < 1) {
3920 			MEMZERO(hp, QENTRY_LEN);	/* PERF */
3921 			isp_prt(isp, ISP_LOGERR,
3922 			    "bad request handle %d (type 0x%x, flags 0x%x)",
3923 			    sp->req_handle, sp->req_header.rqs_entry_type,
3924 			    sp->req_header.rqs_flags);
3925 			WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
3926 			continue;
3927 		}
3928 		xs = isp_find_xs(isp, sp->req_handle);
3929 		if (xs == NULL) {
3930 			uint8_t ts = sp->req_completion_status & 0xff;
3931 			MEMZERO(hp, QENTRY_LEN);	/* PERF */
3932 			/*
3933 			 * Only whine if this isn't the expected fallout of
3934 			 * aborting the command.
3935 			 */
3936 			if (sp->req_header.rqs_entry_type != RQSTYPE_RESPONSE) {
3937 				isp_prt(isp, ISP_LOGERR,
3938 				    "cannot find handle 0x%x (type 0x%x)",
3939 				    sp->req_handle,
3940 				    sp->req_header.rqs_entry_type);
3941 			} else if (ts != RQCS_ABORTED) {
3942 				isp_prt(isp, ISP_LOGERR,
3943 				    "cannot find handle 0x%x (status 0x%x)",
3944 				    sp->req_handle, ts);
3945 			}
3946 			WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
3947 			continue;
3948 		}
3949 		isp_destroy_handle(isp, sp->req_handle);
3950 		if (sp->req_status_flags & RQSTF_BUS_RESET) {
3951 			XS_SETERR(xs, HBA_BUSRESET);
3952 			isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
3953 		}
3954 		if (buddaboom) {
3955 			XS_SETERR(xs, HBA_BOTCH);
3956 		}
3957 
3958 		if (IS_FC(isp) && (sp->req_scsi_status & RQCS_SV)) {
3959 			/*
3960 			 * Fibre Channel F/W doesn't say we got status
3961 			 * if there's Sense Data instead. I guess they
3962 			 * think it goes w/o saying.
3963 			 */
3964 			sp->req_state_flags |= RQSF_GOT_STATUS;
3965 		}
3966 		if (sp->req_state_flags & RQSF_GOT_STATUS) {
3967 			*XS_STSP(xs) = sp->req_scsi_status & 0xff;
3968 		}
3969 
3970 		switch (sp->req_header.rqs_entry_type) {
3971 		case RQSTYPE_RESPONSE:
3972 			XS_SET_STATE_STAT(isp, xs, sp);
3973 			isp_parse_status(isp, sp, xs);
3974 			if ((XS_NOERR(xs) || XS_ERR(xs) == HBA_NOERROR) &&
3975 			    (*XS_STSP(xs) == SCSI_BUSY)) {
3976 				XS_SETERR(xs, HBA_TGTBSY);
3977 			}
3978 			if (IS_SCSI(isp)) {
3979 				XS_RESID(xs) = sp->req_resid;
3980 				if ((sp->req_state_flags & RQSF_GOT_STATUS) &&
3981 				    (*XS_STSP(xs) == SCSI_CHECK) &&
3982 				    (sp->req_state_flags & RQSF_GOT_SENSE)) {
3983 					XS_SAVE_SENSE(xs, sp);
3984 				}
3985 				/*
3986 				 * A new synchronous rate was negotiated for
3987 				 * this target. Mark state such that we'll go
3988 				 * look up that which has changed later.
3989 				 */
3990 				if (sp->req_status_flags & RQSTF_NEGOTIATION) {
3991 					int t = XS_TGT(xs);
3992 					sdparam *sdp = isp->isp_param;
3993 					sdp += XS_CHANNEL(xs);
3994 					sdp->isp_devparam[t].dev_refresh = 1;
3995 					isp->isp_update |=
3996 					    (1 << XS_CHANNEL(xs));
3997 				}
3998 			} else {
3999 				if (sp->req_status_flags & RQSF_XFER_COMPLETE) {
4000 					XS_RESID(xs) = 0;
4001 				} else if (sp->req_scsi_status & RQCS_RESID) {
4002 					XS_RESID(xs) = sp->req_resid;
4003 				} else {
4004 					XS_RESID(xs) = 0;
4005 				}
4006 				if ((sp->req_state_flags & RQSF_GOT_STATUS) &&
4007 				    (*XS_STSP(xs) == SCSI_CHECK) &&
4008 				    (sp->req_scsi_status & RQCS_SV)) {
4009 					XS_SAVE_SENSE(xs, sp);
4010 					/* solely for the benefit of debug */
4011 					sp->req_state_flags |= RQSF_GOT_SENSE;
4012 				}
4013 			}
4014 			isp_prt(isp, ISP_LOGDEBUG2,
4015 			   "asked for %ld got raw resid %ld settled for %ld",
4016 			    (long) XS_XFRLEN(xs), (long) sp->req_resid,
4017 			    (long) XS_RESID(xs));
4018 			break;
4019 		case RQSTYPE_REQUEST:
4020 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
4021 				/*
4022 				 * Force Queue Full status.
4023 				 */
4024 				*XS_STSP(xs) = SCSI_QFULL;
4025 				XS_SETERR(xs, HBA_NOERROR);
4026 			} else if (XS_NOERR(xs)) {
4027 				/*
4028 				 * ????
4029 				 */
4030 				isp_prt(isp, ISP_LOGDEBUG0,
4031 				    "Request Queue Entry bounced back");
4032 				XS_SETERR(xs, HBA_BOTCH);
4033 			}
4034 			XS_RESID(xs) = XS_XFRLEN(xs);
4035 			break;
4036 		default:
4037 			isp_prt(isp, ISP_LOGWARN,
4038 			    "unhandled response queue type 0x%x",
4039 			    sp->req_header.rqs_entry_type);
4040 			if (XS_NOERR(xs)) {
4041 				XS_SETERR(xs, HBA_BOTCH);
4042 			}
4043 			break;
4044 		}
4045 
4046 		/*
4047 		 * Free any DMA resources. As a side effect, this may
4048 		 * also do any cache flushing necessary for data coherence.			 */
4049 		if (XS_XFRLEN(xs)) {
4050 			ISP_DMAFREE(isp, xs, sp->req_handle);
4051 		}
4052 
4053 		if (((isp->isp_dblev & (ISP_LOGDEBUG2|ISP_LOGDEBUG3))) ||
4054 		    ((isp->isp_dblev & ISP_LOGDEBUG1) && ((!XS_NOERR(xs)) ||
4055 		    (*XS_STSP(xs) != SCSI_GOOD)))) {
4056 			char skey;
4057 			if (sp->req_state_flags & RQSF_GOT_SENSE) {
4058 				skey = XS_SNSKEY(xs) & 0xf;
4059 				if (skey < 10)
4060 					skey += '0';
4061 				else
4062 					skey += 'a' - 10;
4063 			} else if (*XS_STSP(xs) == SCSI_CHECK) {
4064 				skey = '?';
4065 			} else {
4066 				skey = '.';
4067 			}
4068 			isp_prt(isp, ISP_LOGALL, finmsg, XS_CHANNEL(xs),
4069 			    XS_TGT(xs), XS_LUN(xs), XS_XFRLEN(xs), XS_RESID(xs),
4070 			    *XS_STSP(xs), skey, XS_ERR(xs));
4071 		}
4072 
4073 		if (isp->isp_nactive > 0)
4074 		    isp->isp_nactive--;
4075 		complist[ndone++] = xs;	/* defer completion call until later */
4076 		MEMZERO(hp, QENTRY_LEN);	/* PERF */
4077 		if (ndone == MAX_REQUESTQ_COMPLETIONS) {
4078 			break;
4079 		}
4080 	}
4081 
4082 	/*
4083 	 * If we looked at any commands, then it's valid to find out
4084 	 * what the outpointer is. It also is a trigger to update the
4085 	 * ISP's notion of what we've seen so far.
4086 	 */
4087 	if (nlooked) {
4088 		WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
4089 		/*
4090 		 * While we're at it, read the requst queue out pointer.
4091 		 */
4092 		isp->isp_reqodx = READ_REQUEST_QUEUE_OUT_POINTER(isp);
4093 		if (isp->isp_rscchiwater < ndone)
4094 			isp->isp_rscchiwater = ndone;
4095 	}
4096 
4097 	isp->isp_residx = optr;
4098 	isp->isp_rspbsy = 0;
4099 	for (i = 0; i < ndone; i++) {
4100 		xs = complist[i];
4101 		if (xs) {
4102 			isp->isp_rsltccmplt++;
4103 			isp_done(xs);
4104 		}
4105 	}
4106 }
4107 
4108 /*
4109  * Support routines.
4110  */
4111 
4112 static int
4113 isp_parse_async(ispsoftc_t *isp, uint16_t mbox)
4114 {
4115 	int rval = 0;
4116 	int bus;
4117 
4118 	if (IS_DUALBUS(isp)) {
4119 		bus = ISP_READ(isp, OUTMAILBOX6);
4120 	} else {
4121 		bus = 0;
4122 	}
4123 	isp_prt(isp, ISP_LOGDEBUG2, "Async Mbox 0x%x", mbox);
4124 
4125 	switch (mbox) {
4126 	case ASYNC_BUS_RESET:
4127 		isp->isp_sendmarker |= (1 << bus);
4128 #ifdef	ISP_TARGET_MODE
4129 		if (isp_target_async(isp, bus, mbox))
4130 			rval = -1;
4131 #endif
4132 		isp_async(isp, ISPASYNC_BUS_RESET, &bus);
4133 		break;
4134 	case ASYNC_SYSTEM_ERROR:
4135 		isp->isp_state = ISP_CRASHED;
4136 		if (IS_FC(isp)) {
4137 			FCPARAM(isp)->isp_loopstate = LOOP_NIL;
4138 			FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4139 		}
4140 		/*
4141 		 * Were we waiting for a mailbox command to complete?
4142 		 * If so, it's dead, so wake up the waiter.
4143 		 */
4144 		if (isp->isp_mboxbsy) {
4145 			isp->isp_obits = 1;
4146 			isp->isp_mboxtmp[0] = MBOX_HOST_INTERFACE_ERROR;
4147 			MBOX_NOTIFY_COMPLETE(isp);
4148 		}
4149 #ifdef	ISP_FW_CRASH_DUMP
4150 		/*
4151 		 * If we have crash dumps enabled, it's up to the handler
4152 		 * for isp_async to reinit stuff and restart the firmware
4153 		 * after performing the crash dump. The reason we do things
4154 		 * this way is that we may need to activate a kernel thread
4155 		 * to do all the crash dump goop.
4156 		 */
4157 		isp_async(isp, ISPASYNC_FW_CRASH, NULL);
4158 #else
4159 		isp_async(isp, ISPASYNC_FW_CRASH, NULL);
4160 		isp_reinit(isp);
4161 		isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
4162 #endif
4163 		rval = -1;
4164 		break;
4165 
4166 	case ASYNC_RQS_XFER_ERR:
4167 		isp_prt(isp, ISP_LOGERR, "Request Queue Transfer Error");
4168 		break;
4169 
4170 	case ASYNC_RSP_XFER_ERR:
4171 		isp_prt(isp, ISP_LOGERR, "Response Queue Transfer Error");
4172 		break;
4173 
4174 	case ASYNC_QWAKEUP:
4175 		/*
4176 		 * We've just been notified that the Queue has woken up.
4177 		 * We don't need to be chatty about this- just unlatch things
4178 		 * and move on.
4179 		 */
4180 		mbox = READ_REQUEST_QUEUE_OUT_POINTER(isp);
4181 		break;
4182 
4183 	case ASYNC_TIMEOUT_RESET:
4184 		isp_prt(isp, ISP_LOGWARN,
4185 		    "timeout initiated SCSI bus reset of bus %d", bus);
4186 		isp->isp_sendmarker |= (1 << bus);
4187 #ifdef	ISP_TARGET_MODE
4188 		if (isp_target_async(isp, bus, mbox))
4189 			rval = -1;
4190 #endif
4191 		break;
4192 
4193 	case ASYNC_DEVICE_RESET:
4194 		isp_prt(isp, ISP_LOGINFO, "device reset on bus %d", bus);
4195 		isp->isp_sendmarker |= (1 << bus);
4196 #ifdef	ISP_TARGET_MODE
4197 		if (isp_target_async(isp, bus, mbox))
4198 			rval = -1;
4199 #endif
4200 		break;
4201 
4202 	case ASYNC_EXTMSG_UNDERRUN:
4203 		isp_prt(isp, ISP_LOGWARN, "extended message underrun");
4204 		break;
4205 
4206 	case ASYNC_SCAM_INT:
4207 		isp_prt(isp, ISP_LOGINFO, "SCAM interrupt");
4208 		break;
4209 
4210 	case ASYNC_HUNG_SCSI:
4211 		isp_prt(isp, ISP_LOGERR,
4212 		    "stalled SCSI Bus after DATA Overrun");
4213 		/* XXX: Need to issue SCSI reset at this point */
4214 		break;
4215 
4216 	case ASYNC_KILLED_BUS:
4217 		isp_prt(isp, ISP_LOGERR, "SCSI Bus reset after DATA Overrun");
4218 		break;
4219 
4220 	case ASYNC_BUS_TRANSIT:
4221 		mbox = ISP_READ(isp, OUTMAILBOX2);
4222 		switch (mbox & 0x1c00) {
4223 		case SXP_PINS_LVD_MODE:
4224 			isp_prt(isp, ISP_LOGINFO, "Transition to LVD mode");
4225 			SDPARAM(isp)->isp_diffmode = 0;
4226 			SDPARAM(isp)->isp_ultramode = 0;
4227 			SDPARAM(isp)->isp_lvdmode = 1;
4228 			break;
4229 		case SXP_PINS_HVD_MODE:
4230 			isp_prt(isp, ISP_LOGINFO,
4231 			    "Transition to Differential mode");
4232 			SDPARAM(isp)->isp_diffmode = 1;
4233 			SDPARAM(isp)->isp_ultramode = 0;
4234 			SDPARAM(isp)->isp_lvdmode = 0;
4235 			break;
4236 		case SXP_PINS_SE_MODE:
4237 			isp_prt(isp, ISP_LOGINFO,
4238 			    "Transition to Single Ended mode");
4239 			SDPARAM(isp)->isp_diffmode = 0;
4240 			SDPARAM(isp)->isp_ultramode = 1;
4241 			SDPARAM(isp)->isp_lvdmode = 0;
4242 			break;
4243 		default:
4244 			isp_prt(isp, ISP_LOGWARN,
4245 			    "Transition to Unknown Mode 0x%x", mbox);
4246 			break;
4247 		}
4248 		/*
4249 		 * XXX: Set up to renegotiate again!
4250 		 */
4251 		/* Can only be for a 1080... */
4252 		isp->isp_sendmarker |= (1 << bus);
4253 		break;
4254 
4255 	/*
4256 	 * We can use bus, which will always be zero for FC cards,
4257 	 * as a mailbox pattern accumulator to be checked below.
4258 	 */
4259 	case ASYNC_RIO5:
4260 		bus = 0x1ce;	/* outgoing mailbox regs 1-3, 6-7 */
4261 		break;
4262 
4263 	case ASYNC_RIO4:
4264 		bus = 0x14e;	/* outgoing mailbox regs 1-3, 6 */
4265 		break;
4266 
4267 	case ASYNC_RIO3:
4268 		bus = 0x10e;	/* outgoing mailbox regs 1-3 */
4269 		break;
4270 
4271 	case ASYNC_RIO2:
4272 		bus = 0x106;	/* outgoing mailbox regs 1-2 */
4273 		break;
4274 
4275 	case ASYNC_RIO1:
4276 	case ASYNC_CMD_CMPLT:
4277 		bus = 0x102;	/* outgoing mailbox regs 1 */
4278 		break;
4279 
4280 	case ASYNC_RIO_RESP:
4281 		return (rval);
4282 
4283 	case ASYNC_CTIO_DONE:
4284 	{
4285 #ifdef	ISP_TARGET_MODE
4286 		int handle =
4287 		    (ISP_READ(isp, OUTMAILBOX2) << 16) |
4288 		    (ISP_READ(isp, OUTMAILBOX1));
4289 		if (isp_target_async(isp, handle, mbox)) {
4290 			rval = -1;
4291 		} else {
4292 			/* count it as a fast posting intr */
4293 			isp->isp_fphccmplt++;
4294 		}
4295 #else
4296 		isp_prt(isp, ISP_LOGINFO, "Fast Posting CTIO done");
4297 		isp->isp_fphccmplt++;	/* count it as a fast posting intr */
4298 #endif
4299 		break;
4300 	}
4301 	case ASYNC_LIP_F8:
4302 	case ASYNC_LIP_OCCURRED:
4303 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4304 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
4305 		isp->isp_sendmarker = 1;
4306 		isp_mark_getpdb_all(isp);
4307 		isp_async(isp, ISPASYNC_LIP, NULL);
4308 #ifdef	ISP_TARGET_MODE
4309 		if (isp_target_async(isp, bus, mbox))
4310 			rval = -1;
4311 #endif
4312 		/*
4313 		 * We've had problems with data corruption occuring on
4314 		 * commands that complete (with no apparent error) after
4315 		 * we receive a LIP. This has been observed mostly on
4316 		 * Local Loop topologies. To be safe, let's just mark
4317 		 * all active commands as dead.
4318 		 */
4319 		if (FCPARAM(isp)->isp_topo == TOPO_NL_PORT ||
4320 		    FCPARAM(isp)->isp_topo == TOPO_FL_PORT) {
4321 			int i, j;
4322 			for (i = j = 0; i < isp->isp_maxcmds; i++) {
4323 				XS_T *xs;
4324 				xs = isp->isp_xflist[i];
4325 				if (xs != NULL) {
4326 					j++;
4327 					XS_SETERR(xs, HBA_BUSRESET);
4328 				}
4329 			}
4330 			if (j) {
4331 				isp_prt(isp, ISP_LOGERR,
4332 				    "LIP destroyed %d active commands", j);
4333 			}
4334 		}
4335 		break;
4336 
4337 	case ASYNC_LOOP_UP:
4338 		isp->isp_sendmarker = 1;
4339 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4340 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
4341 		isp_mark_getpdb_all(isp);
4342 		isp_async(isp, ISPASYNC_LOOP_UP, NULL);
4343 #ifdef	ISP_TARGET_MODE
4344 		if (isp_target_async(isp, bus, mbox))
4345 			rval = -1;
4346 #endif
4347 		break;
4348 
4349 	case ASYNC_LOOP_DOWN:
4350 		isp->isp_sendmarker = 1;
4351 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4352 		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
4353 		isp_mark_getpdb_all(isp);
4354 		isp_async(isp, ISPASYNC_LOOP_DOWN, NULL);
4355 #ifdef	ISP_TARGET_MODE
4356 		if (isp_target_async(isp, bus, mbox))
4357 			rval = -1;
4358 #endif
4359 		break;
4360 
4361 	case ASYNC_LOOP_RESET:
4362 		isp->isp_sendmarker = 1;
4363 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4364 		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
4365 		isp_mark_getpdb_all(isp);
4366 		isp_async(isp, ISPASYNC_LOOP_RESET, NULL);
4367 #ifdef	ISP_TARGET_MODE
4368 		if (isp_target_async(isp, bus, mbox))
4369 			rval = -1;
4370 #endif
4371 		break;
4372 
4373 	case ASYNC_PDB_CHANGED:
4374 		isp->isp_sendmarker = 1;
4375 		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
4376 		isp_mark_getpdb_all(isp);
4377 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_PDB);
4378 		break;
4379 
4380 	case ASYNC_CHANGE_NOTIFY:
4381 		/*
4382 		 * Not correct, but it will force us to rescan the loop.
4383 		 */
4384 		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
4385 		isp_mark_getpdb_all(isp);
4386 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_SNS);
4387 		break;
4388 
4389 	case ASYNC_PTPMODE:
4390 		if (FCPARAM(isp)->isp_onfabric)
4391 			FCPARAM(isp)->isp_topo = TOPO_F_PORT;
4392 		else
4393 			FCPARAM(isp)->isp_topo = TOPO_N_PORT;
4394 		isp_mark_getpdb_all(isp);
4395 		isp->isp_sendmarker = 1;
4396 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4397 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
4398 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
4399 #ifdef	ISP_TARGET_MODE
4400 		if (isp_target_async(isp, bus, mbox))
4401 			rval = -1;
4402 #endif
4403 		isp_prt(isp, ISP_LOGINFO, "Point-to-Point mode");
4404 		break;
4405 
4406 	case ASYNC_CONNMODE:
4407 		mbox = ISP_READ(isp, OUTMAILBOX1);
4408 		isp_mark_getpdb_all(isp);
4409 		switch (mbox) {
4410 		case ISP_CONN_LOOP:
4411 			isp_prt(isp, ISP_LOGINFO,
4412 			    "Point-to-Point -> Loop mode");
4413 			break;
4414 		case ISP_CONN_PTP:
4415 			isp_prt(isp, ISP_LOGINFO,
4416 			    "Loop -> Point-to-Point mode");
4417 			break;
4418 		case ISP_CONN_BADLIP:
4419 			isp_prt(isp, ISP_LOGWARN,
4420 			    "Point-to-Point -> Loop mode (BAD LIP)");
4421 			break;
4422 		case ISP_CONN_FATAL:
4423 			isp_prt(isp, ISP_LOGERR, "FATAL CONNECTION ERROR");
4424 #ifdef	ISP_FW_CRASH_DUMP
4425 			isp_async(isp, ISPASYNC_FW_CRASH, NULL);
4426 #else
4427 			isp_async(isp, ISPASYNC_FW_CRASH, NULL);
4428 			isp_reinit(isp);
4429 			isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
4430 #endif
4431 			return (-1);
4432 		case ISP_CONN_LOOPBACK:
4433 			isp_prt(isp, ISP_LOGWARN,
4434 			    "Looped Back in Point-to-Point mode");
4435 			break;
4436 		default:
4437 			isp_prt(isp, ISP_LOGWARN,
4438 			    "Unknown connection mode (0x%x)", mbox);
4439 			break;
4440 		}
4441 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
4442 		isp->isp_sendmarker = 1;
4443 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4444 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
4445 		break;
4446 
4447 	default:
4448 		isp_prt(isp, ISP_LOGWARN, "Unknown Async Code 0x%x", mbox);
4449 		break;
4450 	}
4451 
4452 	if (bus & 0x100) {
4453 		int i, nh;
4454 		uint16_t handles[16];
4455 
4456 		for (nh = 0, i = 1; i < MAX_MAILBOX(isp); i++) {
4457 			if ((bus & (1 << i)) == 0) {
4458 				continue;
4459 			}
4460 			handles[nh++] = ISP_READ(isp, MBOX_OFF(i));
4461 		}
4462 		for (i = 0; i < nh; i++) {
4463 			isp_fastpost_complete(isp, handles[i]);
4464 			isp_prt(isp,  ISP_LOGDEBUG3,
4465 			    "fast post completion of %u", handles[i]);
4466 		}
4467 		if (isp->isp_fpcchiwater < nh)
4468 			isp->isp_fpcchiwater = nh;
4469 	} else {
4470 		isp->isp_intoasync++;
4471 	}
4472 	return (rval);
4473 }
4474 
4475 /*
4476  * Handle other response entries. A pointer to the request queue output
4477  * index is here in case we want to eat several entries at once, although
4478  * this is not used currently.
4479  */
4480 
4481 static int
4482 isp_handle_other_response(ispsoftc_t *isp, int type,
4483     isphdr_t *hp, uint16_t *optrp)
4484 {
4485 	switch (type) {
4486 	case RQSTYPE_STATUS_CONT:
4487 		isp_prt(isp, ISP_LOGINFO, "Ignored Continuation Response");
4488 		return (1);
4489 	case RQSTYPE_ATIO:
4490 	case RQSTYPE_CTIO:
4491 	case RQSTYPE_ENABLE_LUN:
4492 	case RQSTYPE_MODIFY_LUN:
4493 	case RQSTYPE_NOTIFY:
4494 	case RQSTYPE_NOTIFY_ACK:
4495 	case RQSTYPE_CTIO1:
4496 	case RQSTYPE_ATIO2:
4497 	case RQSTYPE_CTIO2:
4498 	case RQSTYPE_CTIO3:
4499 		isp->isp_rsltccmplt++;	/* count as a response completion */
4500 #ifdef	ISP_TARGET_MODE
4501 		if (isp_target_notify(isp, (ispstatusreq_t *) hp, optrp)) {
4502 			return (1);
4503 		}
4504 #endif
4505 		/* FALLTHROUGH */
4506 	case RQSTYPE_REQUEST:
4507 	default:
4508 		USEC_DELAY(100);
4509 		if (type != isp_get_response_type(isp, hp)) {
4510 			/*
4511 			 * This is questionable- we're just papering over
4512 			 * something we've seen on SMP linux in target
4513 			 * mode- we don't really know what's happening
4514 			 * here that causes us to think we've gotten
4515 			 * an entry, but that either the entry isn't
4516 			 * filled out yet or our CPU read data is stale.
4517 			 */
4518 			isp_prt(isp, ISP_LOGINFO,
4519 				"unstable type in response queue");
4520 			return (-1);
4521 		}
4522 		isp_prt(isp, ISP_LOGWARN, "Unhandled Response Type 0x%x",
4523 		    isp_get_response_type(isp, hp));
4524 		if (isp_async(isp, ISPASYNC_UNHANDLED_RESPONSE, hp)) {
4525 			return (1);
4526 		}
4527 		return (0);
4528 	}
4529 }
4530 
4531 static void
4532 isp_parse_status(ispsoftc_t *isp, ispstatusreq_t *sp, XS_T *xs)
4533 {
4534 	switch (sp->req_completion_status & 0xff) {
4535 	case RQCS_COMPLETE:
4536 		if (XS_NOERR(xs)) {
4537 			XS_SETERR(xs, HBA_NOERROR);
4538 		}
4539 		return;
4540 
4541 	case RQCS_INCOMPLETE:
4542 		if ((sp->req_state_flags & RQSF_GOT_TARGET) == 0) {
4543 			isp_prt(isp, ISP_LOGDEBUG1,
4544 			    "Selection Timeout for %d.%d.%d",
4545 			    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4546 			if (XS_NOERR(xs)) {
4547 				XS_SETERR(xs, HBA_SELTIMEOUT);
4548 			}
4549 			return;
4550 		}
4551 		isp_prt(isp, ISP_LOGERR,
4552 		    "command incomplete for %d.%d.%d, state 0x%x",
4553 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs),
4554 		    sp->req_state_flags);
4555 		break;
4556 
4557 	case RQCS_DMA_ERROR:
4558 		isp_prt(isp, ISP_LOGERR, "DMA error for command on %d.%d.%d",
4559 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4560 		break;
4561 
4562 	case RQCS_TRANSPORT_ERROR:
4563 	{
4564 		char buf[172];
4565 		SNPRINTF(buf, sizeof (buf), "states=>");
4566 		if (sp->req_state_flags & RQSF_GOT_BUS) {
4567 			SNPRINTF(buf, sizeof (buf), "%s GOT_BUS", buf);
4568 		}
4569 		if (sp->req_state_flags & RQSF_GOT_TARGET) {
4570 			SNPRINTF(buf, sizeof (buf), "%s GOT_TGT", buf);
4571 		}
4572 		if (sp->req_state_flags & RQSF_SENT_CDB) {
4573 			SNPRINTF(buf, sizeof (buf), "%s SENT_CDB", buf);
4574 		}
4575 		if (sp->req_state_flags & RQSF_XFRD_DATA) {
4576 			SNPRINTF(buf, sizeof (buf), "%s XFRD_DATA", buf);
4577 		}
4578 		if (sp->req_state_flags & RQSF_GOT_STATUS) {
4579 			SNPRINTF(buf, sizeof (buf), "%s GOT_STS", buf);
4580 		}
4581 		if (sp->req_state_flags & RQSF_GOT_SENSE) {
4582 			SNPRINTF(buf, sizeof (buf), "%s GOT_SNS", buf);
4583 		}
4584 		if (sp->req_state_flags & RQSF_XFER_COMPLETE) {
4585 			SNPRINTF(buf, sizeof (buf), "%s XFR_CMPLT", buf);
4586 		}
4587 		SNPRINTF(buf, sizeof (buf), "%s\nstatus=>", buf);
4588 		if (sp->req_status_flags & RQSTF_DISCONNECT) {
4589 			SNPRINTF(buf, sizeof (buf), "%s Disconnect", buf);
4590 		}
4591 		if (sp->req_status_flags & RQSTF_SYNCHRONOUS) {
4592 			SNPRINTF(buf, sizeof (buf), "%s Sync_xfr", buf);
4593 		}
4594 		if (sp->req_status_flags & RQSTF_PARITY_ERROR) {
4595 			SNPRINTF(buf, sizeof (buf), "%s Parity", buf);
4596 		}
4597 		if (sp->req_status_flags & RQSTF_BUS_RESET) {
4598 			SNPRINTF(buf, sizeof (buf), "%s Bus_Reset", buf);
4599 		}
4600 		if (sp->req_status_flags & RQSTF_DEVICE_RESET) {
4601 			SNPRINTF(buf, sizeof (buf), "%s Device_Reset", buf);
4602 		}
4603 		if (sp->req_status_flags & RQSTF_ABORTED) {
4604 			SNPRINTF(buf, sizeof (buf), "%s Aborted", buf);
4605 		}
4606 		if (sp->req_status_flags & RQSTF_TIMEOUT) {
4607 			SNPRINTF(buf, sizeof (buf), "%s Timeout", buf);
4608 		}
4609 		if (sp->req_status_flags & RQSTF_NEGOTIATION) {
4610 			SNPRINTF(buf, sizeof (buf), "%s Negotiation", buf);
4611 		}
4612 		isp_prt(isp, ISP_LOGERR, "%s", buf);
4613 		isp_prt(isp, ISP_LOGERR, "transport error for %d.%d.%d:\n%s",
4614 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), buf);
4615 		break;
4616 	}
4617 	case RQCS_RESET_OCCURRED:
4618 		isp_prt(isp, ISP_LOGWARN,
4619 		    "bus reset destroyed command for %d.%d.%d",
4620 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4621 		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
4622 		if (XS_NOERR(xs)) {
4623 			XS_SETERR(xs, HBA_BUSRESET);
4624 		}
4625 		return;
4626 
4627 	case RQCS_ABORTED:
4628 		isp_prt(isp, ISP_LOGERR, "command aborted for %d.%d.%d",
4629 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4630 		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
4631 		if (XS_NOERR(xs)) {
4632 			XS_SETERR(xs, HBA_ABORTED);
4633 		}
4634 		return;
4635 
4636 	case RQCS_TIMEOUT:
4637 		isp_prt(isp, ISP_LOGWARN, "command timed out for %d.%d.%d",
4638 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4639 		/*
4640 	 	 * Check to see if we logged out the device.
4641 		 */
4642 		if (IS_FC(isp)) {
4643 			if ((sp->req_completion_status & RQSTF_LOGOUT) &&
4644 			    FCPARAM(isp)->portdb[XS_TGT(xs)].valid &&
4645 			    FCPARAM(isp)->portdb[XS_TGT(xs)].fabric_dev) {
4646 				FCPARAM(isp)->portdb[XS_TGT(xs)].relogin = 1;
4647 			}
4648 		}
4649 		if (XS_NOERR(xs)) {
4650 			XS_SETERR(xs, HBA_CMDTIMEOUT);
4651 		}
4652 		return;
4653 
4654 	case RQCS_DATA_OVERRUN:
4655 		XS_RESID(xs) = sp->req_resid;
4656 		isp_prt(isp, ISP_LOGERR, "data overrun for command on %d.%d.%d",
4657 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4658 		if (XS_NOERR(xs)) {
4659 			XS_SETERR(xs, HBA_DATAOVR);
4660 		}
4661 		return;
4662 
4663 	case RQCS_COMMAND_OVERRUN:
4664 		isp_prt(isp, ISP_LOGERR,
4665 		    "command overrun for command on %d.%d.%d",
4666 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4667 		break;
4668 
4669 	case RQCS_STATUS_OVERRUN:
4670 		isp_prt(isp, ISP_LOGERR,
4671 		    "status overrun for command on %d.%d.%d",
4672 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4673 		break;
4674 
4675 	case RQCS_BAD_MESSAGE:
4676 		isp_prt(isp, ISP_LOGERR,
4677 		    "msg not COMMAND COMPLETE after status %d.%d.%d",
4678 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4679 		break;
4680 
4681 	case RQCS_NO_MESSAGE_OUT:
4682 		isp_prt(isp, ISP_LOGERR,
4683 		    "No MESSAGE OUT phase after selection on %d.%d.%d",
4684 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4685 		break;
4686 
4687 	case RQCS_EXT_ID_FAILED:
4688 		isp_prt(isp, ISP_LOGERR, "EXTENDED IDENTIFY failed %d.%d.%d",
4689 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4690 		break;
4691 
4692 	case RQCS_IDE_MSG_FAILED:
4693 		isp_prt(isp, ISP_LOGERR,
4694 		    "INITIATOR DETECTED ERROR rejected by %d.%d.%d",
4695 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4696 		break;
4697 
4698 	case RQCS_ABORT_MSG_FAILED:
4699 		isp_prt(isp, ISP_LOGERR, "ABORT OPERATION rejected by %d.%d.%d",
4700 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4701 		break;
4702 
4703 	case RQCS_REJECT_MSG_FAILED:
4704 		isp_prt(isp, ISP_LOGERR, "MESSAGE REJECT rejected by %d.%d.%d",
4705 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4706 		break;
4707 
4708 	case RQCS_NOP_MSG_FAILED:
4709 		isp_prt(isp, ISP_LOGERR, "NOP rejected by %d.%d.%d",
4710 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4711 		break;
4712 
4713 	case RQCS_PARITY_ERROR_MSG_FAILED:
4714 		isp_prt(isp, ISP_LOGERR,
4715 		    "MESSAGE PARITY ERROR rejected by %d.%d.%d",
4716 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4717 		break;
4718 
4719 	case RQCS_DEVICE_RESET_MSG_FAILED:
4720 		isp_prt(isp, ISP_LOGWARN,
4721 		    "BUS DEVICE RESET rejected by %d.%d.%d",
4722 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4723 		break;
4724 
4725 	case RQCS_ID_MSG_FAILED:
4726 		isp_prt(isp, ISP_LOGERR, "IDENTIFY rejected by %d.%d.%d",
4727 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4728 		break;
4729 
4730 	case RQCS_UNEXP_BUS_FREE:
4731 		isp_prt(isp, ISP_LOGERR, "%d.%d.%d had an unexpected bus free",
4732 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4733 		break;
4734 
4735 	case RQCS_DATA_UNDERRUN:
4736 	{
4737 		if (IS_FC(isp)) {
4738 			int ru_marked = (sp->req_scsi_status & RQCS_RU) != 0;
4739 			if (!ru_marked || sp->req_resid > XS_XFRLEN(xs)) {
4740 				isp_prt(isp, ISP_LOGWARN, bun, XS_TGT(xs),
4741 				    XS_LUN(xs), XS_XFRLEN(xs), sp->req_resid,
4742 				    (ru_marked)? "marked" : "not marked");
4743 				if (XS_NOERR(xs)) {
4744 					XS_SETERR(xs, HBA_BOTCH);
4745 				}
4746 				return;
4747 			}
4748 		}
4749 		XS_RESID(xs) = sp->req_resid;
4750 		if (XS_NOERR(xs)) {
4751 			XS_SETERR(xs, HBA_NOERROR);
4752 		}
4753 		return;
4754 	}
4755 
4756 	case RQCS_XACT_ERR1:
4757 		isp_prt(isp, ISP_LOGERR, xact1, XS_CHANNEL(xs),
4758 		    XS_TGT(xs), XS_LUN(xs));
4759 		break;
4760 
4761 	case RQCS_XACT_ERR2:
4762 		isp_prt(isp, ISP_LOGERR, xact2,
4763 		    XS_LUN(xs), XS_TGT(xs), XS_CHANNEL(xs));
4764 		break;
4765 
4766 	case RQCS_XACT_ERR3:
4767 		isp_prt(isp, ISP_LOGERR, xact3,
4768 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4769 		break;
4770 
4771 	case RQCS_BAD_ENTRY:
4772 		isp_prt(isp, ISP_LOGERR, "Invalid IOCB entry type detected");
4773 		break;
4774 
4775 	case RQCS_QUEUE_FULL:
4776 		isp_prt(isp, ISP_LOGDEBUG0,
4777 		    "internal queues full for %d.%d.%d status 0x%x",
4778 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), *XS_STSP(xs));
4779 
4780 		/*
4781 		 * If QFULL or some other status byte is set, then this
4782 		 * isn't an error, per se.
4783 		 *
4784 		 * Unfortunately, some QLogic f/w writers have, in
4785 		 * some cases, ommitted to *set* status to QFULL.
4786 		 *
4787 
4788 		if (*XS_STSP(xs) != SCSI_GOOD && XS_NOERR(xs)) {
4789 			XS_SETERR(xs, HBA_NOERROR);
4790 			return;
4791 		}
4792 
4793 		 *
4794 		 *
4795 		 */
4796 
4797 		*XS_STSP(xs) = SCSI_QFULL;
4798 		XS_SETERR(xs, HBA_NOERROR);
4799 		return;
4800 
4801 	case RQCS_PHASE_SKIPPED:
4802 		isp_prt(isp, ISP_LOGERR, pskip, XS_CHANNEL(xs),
4803 		    XS_TGT(xs), XS_LUN(xs));
4804 		break;
4805 
4806 	case RQCS_ARQS_FAILED:
4807 		isp_prt(isp, ISP_LOGERR,
4808 		    "Auto Request Sense failed for %d.%d.%d",
4809 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4810 		if (XS_NOERR(xs)) {
4811 			XS_SETERR(xs, HBA_ARQFAIL);
4812 		}
4813 		return;
4814 
4815 	case RQCS_WIDE_FAILED:
4816 		isp_prt(isp, ISP_LOGERR,
4817 		    "Wide Negotiation failed for %d.%d.%d",
4818 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
4819 		if (IS_SCSI(isp)) {
4820 			sdparam *sdp = isp->isp_param;
4821 			sdp += XS_CHANNEL(xs);
4822 			sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_WIDE;
4823 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
4824 			isp->isp_update |= (1 << XS_CHANNEL(xs));
4825 		}
4826 		if (XS_NOERR(xs)) {
4827 			XS_SETERR(xs, HBA_NOERROR);
4828 		}
4829 		return;
4830 
4831 	case RQCS_SYNCXFER_FAILED:
4832 		isp_prt(isp, ISP_LOGERR,
4833 		    "SDTR Message failed for target %d.%d.%d",
4834 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
4835 		if (IS_SCSI(isp)) {
4836 			sdparam *sdp = isp->isp_param;
4837 			sdp += XS_CHANNEL(xs);
4838 			sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_SYNC;
4839 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
4840 			isp->isp_update |= (1 << XS_CHANNEL(xs));
4841 		}
4842 		break;
4843 
4844 	case RQCS_LVD_BUSERR:
4845 		isp_prt(isp, ISP_LOGERR,
4846 		    "Bad LVD condition while talking to %d.%d.%d",
4847 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
4848 		break;
4849 
4850 	case RQCS_PORT_UNAVAILABLE:
4851 		/*
4852 		 * No such port on the loop. Moral equivalent of SELTIMEO
4853 		 */
4854 	case RQCS_PORT_LOGGED_OUT:
4855 		/*
4856 		 * It was there (maybe)- treat as a selection timeout.
4857 		 */
4858 		if ((sp->req_completion_status & 0xff) == RQCS_PORT_UNAVAILABLE)
4859 			isp_prt(isp, ISP_LOGINFO,
4860 			    "port unavailable for target %d", XS_TGT(xs));
4861 		else
4862 			isp_prt(isp, ISP_LOGINFO,
4863 			    "port logout for target %d", XS_TGT(xs));
4864 		/*
4865 		 * If we're on a local loop, force a LIP (which is overkill)
4866 		 * to force a re-login of this unit. If we're on fabric,
4867 		 * then we'll have to relogin as a matter of course.
4868 		 */
4869 		if (FCPARAM(isp)->isp_topo == TOPO_NL_PORT ||
4870 		    FCPARAM(isp)->isp_topo == TOPO_FL_PORT) {
4871 			mbreg_t mbs;
4872 			MEMZERO(&mbs, sizeof (mbs));
4873 			mbs.param[0] = MBOX_INIT_LIP;
4874 			if (IS_2KLOGIN(isp)) {
4875 				mbs.obits |= (1 << 10);
4876 			}
4877 			isp_mboxcmd_qnw(isp, &mbs, 1);
4878 		}
4879 
4880 		/*
4881 		 * Probably overkill.
4882 		 */
4883 		isp->isp_sendmarker = 1;
4884 		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
4885 		isp_mark_getpdb_all(isp);
4886 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
4887 		if (XS_NOERR(xs)) {
4888 			XS_SETERR(xs, HBA_SELTIMEOUT);
4889 		}
4890 		return;
4891 
4892 	case RQCS_PORT_CHANGED:
4893 		isp_prt(isp, ISP_LOGWARN,
4894 		    "port changed for target %d", XS_TGT(xs));
4895 		if (XS_NOERR(xs)) {
4896 			XS_SETERR(xs, HBA_SELTIMEOUT);
4897 		}
4898 		return;
4899 
4900 	case RQCS_PORT_BUSY:
4901 		isp_prt(isp, ISP_LOGWARN,
4902 		    "port busy for target %d", XS_TGT(xs));
4903 		if (XS_NOERR(xs)) {
4904 			XS_SETERR(xs, HBA_TGTBSY);
4905 		}
4906 		return;
4907 
4908 	default:
4909 		isp_prt(isp, ISP_LOGERR, "Unknown Completion Status 0x%x",
4910 		    sp->req_completion_status);
4911 		break;
4912 	}
4913 	if (XS_NOERR(xs)) {
4914 		XS_SETERR(xs, HBA_BOTCH);
4915 	}
4916 }
4917 
4918 static void
4919 isp_fastpost_complete(ispsoftc_t *isp, uint16_t fph)
4920 {
4921 	XS_T *xs;
4922 
4923 	if (fph == 0) {
4924 		return;
4925 	}
4926 	xs = isp_find_xs(isp, fph);
4927 	if (xs == NULL) {
4928 		isp_prt(isp, ISP_LOGWARN,
4929 		    "Command for fast post handle 0x%x not found", fph);
4930 		return;
4931 	}
4932 	isp_destroy_handle(isp, fph);
4933 
4934 	/*
4935 	 * Since we don't have a result queue entry item,
4936 	 * we must believe that SCSI status is zero and
4937 	 * that all data transferred.
4938 	 */
4939 	XS_SET_STATE_STAT(isp, xs, NULL);
4940 	XS_RESID(xs) = 0;
4941 	*XS_STSP(xs) = SCSI_GOOD;
4942 	if (XS_XFRLEN(xs)) {
4943 		ISP_DMAFREE(isp, xs, fph);
4944 	}
4945 	if (isp->isp_nactive)
4946 		isp->isp_nactive--;
4947 	isp->isp_fphccmplt++;
4948 	isp_done(xs);
4949 }
4950 
4951 static int
4952 isp_mbox_continue(ispsoftc_t *isp)
4953 {
4954 	mbreg_t mbs;
4955 	uint16_t *ptr;
4956 
4957 	switch (isp->isp_lastmbxcmd) {
4958 	case MBOX_WRITE_RAM_WORD:
4959 	case MBOX_READ_RAM_WORD:
4960 	case MBOX_WRITE_RAM_WORD_EXTENDED:
4961 	case MBOX_READ_RAM_WORD_EXTENDED:
4962 		break;
4963 	default:
4964 		return (1);
4965 	}
4966 	if (isp->isp_mboxtmp[0] != MBOX_COMMAND_COMPLETE) {
4967 		isp->isp_mbxwrk0 = 0;
4968 		return (-1);
4969 	}
4970 
4971 	/*
4972 	 * Clear the previous interrupt.
4973 	 */
4974 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
4975 	ISP_WRITE(isp, BIU_SEMA, 0);
4976 
4977 	/*
4978 	 * Continue with next word.
4979 	 */
4980 	MEMZERO(&mbs, sizeof (mbs));
4981 	ptr = isp->isp_mbxworkp;
4982 	switch (isp->isp_lastmbxcmd) {
4983 	case MBOX_WRITE_RAM_WORD:
4984 		mbs.param[2] = *ptr++;
4985 		mbs.param[1] = isp->isp_mbxwrk1++;
4986 		break;
4987 	case MBOX_WRITE_RAM_WORD_EXTENDED:
4988 		mbs.param[2] = *ptr++;
4989 		mbs.param[1] = isp->isp_mbxwrk1++;
4990 		if (isp->isp_mbxwrk1 == 0) {
4991 			isp->isp_mbxwrk8++;
4992 		}
4993 		mbs.param[8] = isp->isp_mbxwrk8;
4994 		break;
4995 	case MBOX_READ_RAM_WORD:
4996 		*ptr++ = isp->isp_mboxtmp[2];
4997 		mbs.param[1] = isp->isp_mbxwrk1++;
4998 		break;
4999 	case MBOX_READ_RAM_WORD_EXTENDED:
5000 		*ptr++ = isp->isp_mboxtmp[2];
5001 		mbs.param[1] = isp->isp_mbxwrk1++;
5002 		if (isp->isp_mbxwrk1 == 0) {
5003 			isp->isp_mbxwrk8++;
5004 		}
5005 		mbs.param[8] = isp->isp_mbxwrk8;
5006 		break;
5007 	}
5008 	isp->isp_mbxworkp = ptr;
5009 	mbs.param[0] = isp->isp_lastmbxcmd;
5010 	isp->isp_mbxwrk0 -= 1;
5011 	isp_mboxcmd_qnw(isp, &mbs, 0);
5012 	return (0);
5013 }
5014 
5015 
5016 #define	HIWRD(x)			((x) >> 16)
5017 #define	LOWRD(x)			((x)  & 0xffff)
5018 #define	ISPOPMAP(a, b)			(((a) << 16) | (b))
5019 static const uint32_t mbpscsi[] = {
5020 	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
5021 	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
5022 	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
5023 	ISPOPMAP(0x1f, 0x01),	/* 0x03: MBOX_DUMP_RAM */
5024 	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
5025 	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
5026 	ISPOPMAP(0x3f, 0x3f),	/* 0x06: MBOX_MAILBOX_REG_TEST */
5027 	ISPOPMAP(0x03, 0x07),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
5028 	ISPOPMAP(0x01, 0x0f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
5029 	ISPOPMAP(0x00, 0x00),	/* 0x09: */
5030 	ISPOPMAP(0x00, 0x00),	/* 0x0a: */
5031 	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
5032 	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
5033 	ISPOPMAP(0x00, 0x00),	/* 0x0d: */
5034 	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
5035 	ISPOPMAP(0x00, 0x00),	/* 0x0f: */
5036 	ISPOPMAP(0x1f, 0x1f),	/* 0x10: MBOX_INIT_REQ_QUEUE */
5037 	ISPOPMAP(0x3f, 0x3f),	/* 0x11: MBOX_INIT_RES_QUEUE */
5038 	ISPOPMAP(0x0f, 0x0f),	/* 0x12: MBOX_EXECUTE_IOCB */
5039 	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
5040 	ISPOPMAP(0x01, 0x3f),	/* 0x14: MBOX_STOP_FIRMWARE */
5041 	ISPOPMAP(0x0f, 0x0f),	/* 0x15: MBOX_ABORT */
5042 	ISPOPMAP(0x03, 0x03),	/* 0x16: MBOX_ABORT_DEVICE */
5043 	ISPOPMAP(0x07, 0x07),	/* 0x17: MBOX_ABORT_TARGET */
5044 	ISPOPMAP(0x07, 0x07),	/* 0x18: MBOX_BUS_RESET */
5045 	ISPOPMAP(0x03, 0x07),	/* 0x19: MBOX_STOP_QUEUE */
5046 	ISPOPMAP(0x03, 0x07),	/* 0x1a: MBOX_START_QUEUE */
5047 	ISPOPMAP(0x03, 0x07),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
5048 	ISPOPMAP(0x03, 0x07),	/* 0x1c: MBOX_ABORT_QUEUE */
5049 	ISPOPMAP(0x03, 0x4f),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
5050 	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
5051 	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
5052 	ISPOPMAP(0x01, 0x07),	/* 0x20: MBOX_GET_INIT_SCSI_ID */
5053 	ISPOPMAP(0x01, 0x07),	/* 0x21: MBOX_GET_SELECT_TIMEOUT */
5054 	ISPOPMAP(0x01, 0xc7),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
5055 	ISPOPMAP(0x01, 0x07),	/* 0x23: MBOX_GET_TAG_AGE_LIMIT */
5056 	ISPOPMAP(0x01, 0x03),	/* 0x24: MBOX_GET_CLOCK_RATE */
5057 	ISPOPMAP(0x01, 0x07),	/* 0x25: MBOX_GET_ACT_NEG_STATE */
5058 	ISPOPMAP(0x01, 0x07),	/* 0x26: MBOX_GET_ASYNC_DATA_SETUP_TIME */
5059 	ISPOPMAP(0x01, 0x07),	/* 0x27: MBOX_GET_PCI_PARAMS */
5060 	ISPOPMAP(0x03, 0x4f),	/* 0x28: MBOX_GET_TARGET_PARAMS */
5061 	ISPOPMAP(0x03, 0x0f),	/* 0x29: MBOX_GET_DEV_QUEUE_PARAMS */
5062 	ISPOPMAP(0x01, 0x07),	/* 0x2a: MBOX_GET_RESET_DELAY_PARAMS */
5063 	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
5064 	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
5065 	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
5066 	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
5067 	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
5068 	ISPOPMAP(0x03, 0x03),	/* 0x30: MBOX_SET_INIT_SCSI_ID */
5069 	ISPOPMAP(0x07, 0x07),	/* 0x31: MBOX_SET_SELECT_TIMEOUT */
5070 	ISPOPMAP(0xc7, 0xc7),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
5071 	ISPOPMAP(0x07, 0x07),	/* 0x33: MBOX_SET_TAG_AGE_LIMIT */
5072 	ISPOPMAP(0x03, 0x03),	/* 0x34: MBOX_SET_CLOCK_RATE */
5073 	ISPOPMAP(0x07, 0x07),	/* 0x35: MBOX_SET_ACT_NEG_STATE */
5074 	ISPOPMAP(0x07, 0x07),	/* 0x36: MBOX_SET_ASYNC_DATA_SETUP_TIME */
5075 	ISPOPMAP(0x07, 0x07),	/* 0x37: MBOX_SET_PCI_CONTROL_PARAMS */
5076 	ISPOPMAP(0x4f, 0x4f),	/* 0x38: MBOX_SET_TARGET_PARAMS */
5077 	ISPOPMAP(0x0f, 0x0f),	/* 0x39: MBOX_SET_DEV_QUEUE_PARAMS */
5078 	ISPOPMAP(0x07, 0x07),	/* 0x3a: MBOX_SET_RESET_DELAY_PARAMS */
5079 	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
5080 	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
5081 	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
5082 	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
5083 	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
5084 	ISPOPMAP(0x01, 0x03),	/* 0x40: MBOX_RETURN_BIOS_BLOCK_ADDR */
5085 	ISPOPMAP(0x3f, 0x01),	/* 0x41: MBOX_WRITE_FOUR_RAM_WORDS */
5086 	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_EXEC_BIOS_IOCB */
5087 	ISPOPMAP(0x00, 0x00),	/* 0x43: */
5088 	ISPOPMAP(0x00, 0x00),	/* 0x44: */
5089 	ISPOPMAP(0x03, 0x03),	/* 0x45: SET SYSTEM PARAMETER */
5090 	ISPOPMAP(0x01, 0x03),	/* 0x46: GET SYSTEM PARAMETER */
5091 	ISPOPMAP(0x00, 0x00),	/* 0x47: */
5092 	ISPOPMAP(0x01, 0xcf),	/* 0x48: GET SCAM CONFIGURATION */
5093 	ISPOPMAP(0xcf, 0xcf),	/* 0x49: SET SCAM CONFIGURATION */
5094 	ISPOPMAP(0x03, 0x03),	/* 0x4a: MBOX_SET_FIRMWARE_FEATURES */
5095 	ISPOPMAP(0x01, 0x03),	/* 0x4b: MBOX_GET_FIRMWARE_FEATURES */
5096 	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
5097 	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
5098 	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
5099 	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
5100 	ISPOPMAP(0xdf, 0xdf),	/* 0x50: LOAD RAM A64 */
5101 	ISPOPMAP(0xdf, 0xdf),	/* 0x51: DUMP RAM A64 */
5102 	ISPOPMAP(0xdf, 0xff),	/* 0x52: INITIALIZE REQUEST QUEUE A64 */
5103 	ISPOPMAP(0xef, 0xff),	/* 0x53: INITIALIZE RESPONSE QUEUE A64 */
5104 	ISPOPMAP(0xcf, 0x01),	/* 0x54: EXECUTE IOCB A64 */
5105 	ISPOPMAP(0x07, 0x01),	/* 0x55: ENABLE TARGET MODE */
5106 	ISPOPMAP(0x03, 0x0f),	/* 0x56: GET TARGET STATUS */
5107 	ISPOPMAP(0x00, 0x00),	/* 0x57: */
5108 	ISPOPMAP(0x00, 0x00),	/* 0x58: */
5109 	ISPOPMAP(0x00, 0x00),	/* 0x59: */
5110 	ISPOPMAP(0x03, 0x03),	/* 0x5a: SET DATA OVERRUN RECOVERY MODE */
5111 	ISPOPMAP(0x01, 0x03),	/* 0x5b: GET DATA OVERRUN RECOVERY MODE */
5112 	ISPOPMAP(0x0f, 0x0f),	/* 0x5c: SET HOST DATA */
5113 	ISPOPMAP(0x01, 0x01)	/* 0x5d: GET NOST DATA */
5114 };
5115 
5116 #ifndef	ISP_STRIPPED
5117 static char *scsi_mbcmd_names[] = {
5118 	"NO-OP",
5119 	"LOAD RAM",
5120 	"EXEC FIRMWARE",
5121 	"DUMP RAM",
5122 	"WRITE RAM WORD",
5123 	"READ RAM WORD",
5124 	"MAILBOX REG TEST",
5125 	"VERIFY CHECKSUM",
5126 	"ABOUT FIRMWARE",
5127 	NULL,
5128 	NULL,
5129 	NULL,
5130 	NULL,
5131 	NULL,
5132 	"CHECK FIRMWARE",
5133 	NULL,
5134 	"INIT REQUEST QUEUE",
5135 	"INIT RESULT QUEUE",
5136 	"EXECUTE IOCB",
5137 	"WAKE UP",
5138 	"STOP FIRMWARE",
5139 	"ABORT",
5140 	"ABORT DEVICE",
5141 	"ABORT TARGET",
5142 	"BUS RESET",
5143 	"STOP QUEUE",
5144 	"START QUEUE",
5145 	"SINGLE STEP QUEUE",
5146 	"ABORT QUEUE",
5147 	"GET DEV QUEUE STATUS",
5148 	NULL,
5149 	"GET FIRMWARE STATUS",
5150 	"GET INIT SCSI ID",
5151 	"GET SELECT TIMEOUT",
5152 	"GET RETRY COUNT",
5153 	"GET TAG AGE LIMIT",
5154 	"GET CLOCK RATE",
5155 	"GET ACT NEG STATE",
5156 	"GET ASYNC DATA SETUP TIME",
5157 	"GET PCI PARAMS",
5158 	"GET TARGET PARAMS",
5159 	"GET DEV QUEUE PARAMS",
5160 	"GET RESET DELAY PARAMS",
5161 	NULL,
5162 	NULL,
5163 	NULL,
5164 	NULL,
5165 	NULL,
5166 	"SET INIT SCSI ID",
5167 	"SET SELECT TIMEOUT",
5168 	"SET RETRY COUNT",
5169 	"SET TAG AGE LIMIT",
5170 	"SET CLOCK RATE",
5171 	"SET ACT NEG STATE",
5172 	"SET ASYNC DATA SETUP TIME",
5173 	"SET PCI CONTROL PARAMS",
5174 	"SET TARGET PARAMS",
5175 	"SET DEV QUEUE PARAMS",
5176 	"SET RESET DELAY PARAMS",
5177 	NULL,
5178 	NULL,
5179 	NULL,
5180 	NULL,
5181 	NULL,
5182 	"RETURN BIOS BLOCK ADDR",
5183 	"WRITE FOUR RAM WORDS",
5184 	"EXEC BIOS IOCB",
5185 	NULL,
5186 	NULL,
5187 	"SET SYSTEM PARAMETER",
5188 	"GET SYSTEM PARAMETER",
5189 	NULL,
5190 	"GET SCAM CONFIGURATION",
5191 	"SET SCAM CONFIGURATION",
5192 	"SET FIRMWARE FEATURES",
5193 	"GET FIRMWARE FEATURES",
5194 	NULL,
5195 	NULL,
5196 	NULL,
5197 	NULL,
5198 	"LOAD RAM A64",
5199 	"DUMP RAM A64",
5200 	"INITIALIZE REQUEST QUEUE A64",
5201 	"INITIALIZE RESPONSE QUEUE A64",
5202 	"EXECUTE IOCB A64",
5203 	"ENABLE TARGET MODE",
5204 	"GET TARGET MODE STATE",
5205 	NULL,
5206 	NULL,
5207 	NULL,
5208 	"SET DATA OVERRUN RECOVERY MODE",
5209 	"GET DATA OVERRUN RECOVERY MODE",
5210 	"SET HOST DATA",
5211 	"GET NOST DATA",
5212 };
5213 #endif
5214 
5215 static const uint32_t mbpfc[] = {
5216 	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
5217 	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
5218 	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
5219 	ISPOPMAP(0xdf, 0x01),	/* 0x03: MBOX_DUMP_RAM */
5220 	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
5221 	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
5222 	ISPOPMAP(0xff, 0xff),	/* 0x06: MBOX_MAILBOX_REG_TEST */
5223 	ISPOPMAP(0x03, 0x05),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
5224 	ISPOPMAP(0x01, 0x4f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
5225 	ISPOPMAP(0xdf, 0x01),	/* 0x09: LOAD RAM */
5226 	ISPOPMAP(0xdf, 0x01),	/* 0x0a: DUMP RAM */
5227 	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
5228 	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
5229 	ISPOPMAP(0x13, 0x01),	/* 0x0d: MBOX_WRITE_RAM_WORD_EXTENDED) */
5230 	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
5231 	ISPOPMAP(0x13, 0x05),	/* 0x0f: MBOX_READ_RAM_WORD_EXTENDED */
5232 	ISPOPMAP(0x1f, 0x11),	/* 0x10: MBOX_INIT_REQ_QUEUE */
5233 	ISPOPMAP(0x2f, 0x21),	/* 0x11: MBOX_INIT_RES_QUEUE */
5234 	ISPOPMAP(0x0f, 0x01),	/* 0x12: MBOX_EXECUTE_IOCB */
5235 	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
5236 	ISPOPMAP(0x01, 0xff),	/* 0x14: MBOX_STOP_FIRMWARE */
5237 	ISPOPMAP(0x4f, 0x01),	/* 0x15: MBOX_ABORT */
5238 	ISPOPMAP(0x07, 0x01),	/* 0x16: MBOX_ABORT_DEVICE */
5239 	ISPOPMAP(0x07, 0x01),	/* 0x17: MBOX_ABORT_TARGET */
5240 	ISPOPMAP(0x03, 0x03),	/* 0x18: MBOX_BUS_RESET */
5241 	ISPOPMAP(0x07, 0x05),	/* 0x19: MBOX_STOP_QUEUE */
5242 	ISPOPMAP(0x07, 0x05),	/* 0x1a: MBOX_START_QUEUE */
5243 	ISPOPMAP(0x07, 0x05),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
5244 	ISPOPMAP(0x07, 0x05),	/* 0x1c: MBOX_ABORT_QUEUE */
5245 	ISPOPMAP(0x07, 0x03),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
5246 	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
5247 	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
5248 	ISPOPMAP(0x01, 0x4f),	/* 0x20: MBOX_GET_LOOP_ID */
5249 	ISPOPMAP(0x00, 0x00),	/* 0x21: */
5250 	ISPOPMAP(0x01, 0x07),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
5251 	ISPOPMAP(0x00, 0x00),	/* 0x23: */
5252 	ISPOPMAP(0x00, 0x00),	/* 0x24: */
5253 	ISPOPMAP(0x00, 0x00),	/* 0x25: */
5254 	ISPOPMAP(0x00, 0x00),	/* 0x26: */
5255 	ISPOPMAP(0x00, 0x00),	/* 0x27: */
5256 	ISPOPMAP(0x01, 0x03),	/* 0x28: MBOX_GET_FIRMWARE_OPTIONS */
5257 	ISPOPMAP(0x03, 0x07),	/* 0x29: MBOX_GET_PORT_QUEUE_PARAMS */
5258 	ISPOPMAP(0x00, 0x00),	/* 0x2a: */
5259 	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
5260 	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
5261 	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
5262 	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
5263 	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
5264 	ISPOPMAP(0x00, 0x00),	/* 0x30: */
5265 	ISPOPMAP(0x00, 0x00),	/* 0x31: */
5266 	ISPOPMAP(0x07, 0x07),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
5267 	ISPOPMAP(0x00, 0x00),	/* 0x33: */
5268 	ISPOPMAP(0x00, 0x00),	/* 0x34: */
5269 	ISPOPMAP(0x00, 0x00),	/* 0x35: */
5270 	ISPOPMAP(0x00, 0x00),	/* 0x36: */
5271 	ISPOPMAP(0x00, 0x00),	/* 0x37: */
5272 	ISPOPMAP(0x0f, 0x01),	/* 0x38: MBOX_SET_FIRMWARE_OPTIONS */
5273 	ISPOPMAP(0x0f, 0x07),	/* 0x39: MBOX_SET_PORT_QUEUE_PARAMS */
5274 	ISPOPMAP(0x00, 0x00),	/* 0x3a: */
5275 	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
5276 	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
5277 	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
5278 	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
5279 	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
5280 	ISPOPMAP(0x03, 0x01),	/* 0x40: MBOX_LOOP_PORT_BYPASS */
5281 	ISPOPMAP(0x03, 0x01),	/* 0x41: MBOX_LOOP_PORT_ENABLE */
5282 	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_GET_RESOURCE_COUNTS */
5283 	ISPOPMAP(0x01, 0x01),	/* 0x43: MBOX_REQUEST_NON_PARTICIPATING_MODE */
5284 	ISPOPMAP(0x00, 0x00),	/* 0x44: */
5285 	ISPOPMAP(0x00, 0x00),	/* 0x45: */
5286 	ISPOPMAP(0x00, 0x00),	/* 0x46: */
5287 	ISPOPMAP(0xcf, 0x03),	/* 0x47: GET PORT_DATABASE ENHANCED */
5288 	ISPOPMAP(0x00, 0x00),	/* 0x48: */
5289 	ISPOPMAP(0x00, 0x00),	/* 0x49: */
5290 	ISPOPMAP(0x00, 0x00),	/* 0x4a: */
5291 	ISPOPMAP(0x00, 0x00),	/* 0x4b: */
5292 	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
5293 	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
5294 	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
5295 	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
5296 	ISPOPMAP(0x00, 0x00),	/* 0x50: */
5297 	ISPOPMAP(0x00, 0x00),	/* 0x51: */
5298 	ISPOPMAP(0x00, 0x00),	/* 0x52: */
5299 	ISPOPMAP(0x00, 0x00),	/* 0x53: */
5300 	ISPOPMAP(0xcf, 0x01),	/* 0x54: EXECUTE IOCB A64 */
5301 	ISPOPMAP(0x00, 0x00),	/* 0x55: */
5302 	ISPOPMAP(0x00, 0x00),	/* 0x56: */
5303 	ISPOPMAP(0x00, 0x00),	/* 0x57: */
5304 	ISPOPMAP(0x00, 0x00),	/* 0x58: */
5305 	ISPOPMAP(0x00, 0x00),	/* 0x59: */
5306 	ISPOPMAP(0x00, 0x00),	/* 0x5a: */
5307 	ISPOPMAP(0x03, 0x01),	/* 0x5b: MBOX_DRIVER_HEARTBEAT */
5308 	ISPOPMAP(0xcf, 0x01),	/* 0x5c: MBOX_FW_HEARTBEAT */
5309 	ISPOPMAP(0x07, 0x03),	/* 0x5d: MBOX_GET_SET_DATA_RATE */
5310 	ISPOPMAP(0x00, 0x00),	/* 0x5e: */
5311 	ISPOPMAP(0x00, 0x00),	/* 0x5f: */
5312 	ISPOPMAP(0xfd, 0x31),	/* 0x60: MBOX_INIT_FIRMWARE */
5313 	ISPOPMAP(0x00, 0x00),	/* 0x61: */
5314 	ISPOPMAP(0x01, 0x01),	/* 0x62: MBOX_INIT_LIP */
5315 	ISPOPMAP(0xcd, 0x03),	/* 0x63: MBOX_GET_FC_AL_POSITION_MAP */
5316 	ISPOPMAP(0xcf, 0x01),	/* 0x64: MBOX_GET_PORT_DB */
5317 	ISPOPMAP(0x07, 0x01),	/* 0x65: MBOX_CLEAR_ACA */
5318 	ISPOPMAP(0x07, 0x01),	/* 0x66: MBOX_TARGET_RESET */
5319 	ISPOPMAP(0x07, 0x01),	/* 0x67: MBOX_CLEAR_TASK_SET */
5320 	ISPOPMAP(0x07, 0x01),	/* 0x68: MBOX_ABORT_TASK_SET */
5321 	ISPOPMAP(0x01, 0x07),	/* 0x69: MBOX_GET_FW_STATE */
5322 	ISPOPMAP(0x03, 0xcf),	/* 0x6a: MBOX_GET_PORT_NAME */
5323 	ISPOPMAP(0xcf, 0x01),	/* 0x6b: MBOX_GET_LINK_STATUS */
5324 	ISPOPMAP(0x0f, 0x01),	/* 0x6c: MBOX_INIT_LIP_RESET */
5325 	ISPOPMAP(0x00, 0x00),	/* 0x6d: */
5326 	ISPOPMAP(0xcf, 0x03),	/* 0x6e: MBOX_SEND_SNS */
5327 	ISPOPMAP(0x0f, 0x07),	/* 0x6f: MBOX_FABRIC_LOGIN */
5328 	ISPOPMAP(0x03, 0x01),	/* 0x70: MBOX_SEND_CHANGE_REQUEST */
5329 	ISPOPMAP(0x03, 0x03),	/* 0x71: MBOX_FABRIC_LOGOUT */
5330 	ISPOPMAP(0x0f, 0x0f),	/* 0x72: MBOX_INIT_LIP_LOGIN */
5331 	ISPOPMAP(0x00, 0x00),	/* 0x73: */
5332 	ISPOPMAP(0x07, 0x01),	/* 0x74: LOGIN LOOP PORT */
5333 	ISPOPMAP(0xcf, 0x03),	/* 0x75: GET PORT/NODE NAME LIST */
5334 	ISPOPMAP(0x4f, 0x01),	/* 0x76: SET VENDOR ID */
5335 	ISPOPMAP(0xcd, 0x01),	/* 0x77: INITIALIZE IP MAILBOX */
5336 	ISPOPMAP(0x00, 0x00),	/* 0x78: */
5337 	ISPOPMAP(0x00, 0x00),	/* 0x79: */
5338 	ISPOPMAP(0x00, 0x00),	/* 0x7a: */
5339 	ISPOPMAP(0x00, 0x00),	/* 0x7b: */
5340 	ISPOPMAP(0x4f, 0x03),	/* 0x7c: Get ID List */
5341 	ISPOPMAP(0xcf, 0x01),	/* 0x7d: SEND LFA */
5342 	ISPOPMAP(0x0f, 0x01)	/* 0x7e: LUN RESET */
5343 };
5344 /*
5345  * Footnotes
5346  *
5347  * (1): this sets bits 21..16 in mailbox register #8, which we nominally
5348  *	do not access at this time in the core driver. The caller is
5349  *	responsible for setting this register first (Gross!). The assumption
5350  *	is that we won't overflow.
5351  */
5352 
5353 #ifndef	ISP_STRIPPED
5354 static char *fc_mbcmd_names[] = {
5355 	"NO-OP",
5356 	"LOAD RAM",
5357 	"EXEC FIRMWARE",
5358 	"DUMP RAM",
5359 	"WRITE RAM WORD",
5360 	"READ RAM WORD",
5361 	"MAILBOX REG TEST",
5362 	"VERIFY CHECKSUM",
5363 	"ABOUT FIRMWARE",
5364 	"LOAD RAM",
5365 	"DUMP RAM",
5366 	"WRITE RAM WORD EXTENDED",
5367 	NULL,
5368 	"READ RAM WORD EXTENDED",
5369 	"CHECK FIRMWARE",
5370 	NULL,
5371 	"INIT REQUEST QUEUE",
5372 	"INIT RESULT QUEUE",
5373 	"EXECUTE IOCB",
5374 	"WAKE UP",
5375 	"STOP FIRMWARE",
5376 	"ABORT",
5377 	"ABORT DEVICE",
5378 	"ABORT TARGET",
5379 	"BUS RESET",
5380 	"STOP QUEUE",
5381 	"START QUEUE",
5382 	"SINGLE STEP QUEUE",
5383 	"ABORT QUEUE",
5384 	"GET DEV QUEUE STATUS",
5385 	NULL,
5386 	"GET FIRMWARE STATUS",
5387 	"GET LOOP ID",
5388 	NULL,
5389 	"GET RETRY COUNT",
5390 	NULL,
5391 	NULL,
5392 	NULL,
5393 	NULL,
5394 	NULL,
5395 	"GET FIRMWARE OPTIONS",
5396 	"GET PORT QUEUE PARAMS",
5397 	NULL,
5398 	NULL,
5399 	NULL,
5400 	NULL,
5401 	NULL,
5402 	NULL,
5403 	NULL,
5404 	NULL,
5405 	"SET RETRY COUNT",
5406 	NULL,
5407 	NULL,
5408 	NULL,
5409 	NULL,
5410 	NULL,
5411 	"SET FIRMWARE OPTIONS",
5412 	"SET PORT QUEUE PARAMS",
5413 	NULL,
5414 	NULL,
5415 	NULL,
5416 	NULL,
5417 	NULL,
5418 	NULL,
5419 	"LOOP PORT BYPASS",
5420 	"LOOP PORT ENABLE",
5421 	"GET RESOURCE COUNTS",
5422 	"REQUEST NON PARTICIPATING MODE",
5423 	NULL,
5424 	NULL,
5425 	NULL,
5426 	"GET PORT DATABASE ENHANCED",
5427 	NULL,
5428 	NULL,
5429 	NULL,
5430 	NULL,
5431 	NULL,
5432 	NULL,
5433 	NULL,
5434 	NULL,
5435 	NULL,
5436 	NULL,
5437 	NULL,
5438 	NULL,
5439 	"EXECUTE IOCB A64",
5440 	NULL,
5441 	NULL,
5442 	NULL,
5443 	NULL,
5444 	NULL,
5445 	NULL,
5446 	"DRIVER HEARTBEAT",
5447 	NULL,
5448 	"GET/SET DATA RATE",
5449 	NULL,
5450 	NULL,
5451 	"INIT FIRMWARE",
5452 	NULL,
5453 	"INIT LIP",
5454 	"GET FC-AL POSITION MAP",
5455 	"GET PORT DATABASE",
5456 	"CLEAR ACA",
5457 	"TARGET RESET",
5458 	"CLEAR TASK SET",
5459 	"ABORT TASK SET",
5460 	"GET FW STATE",
5461 	"GET PORT NAME",
5462 	"GET LINK STATUS",
5463 	"INIT LIP RESET",
5464 	NULL,
5465 	"SEND SNS",
5466 	"FABRIC LOGIN",
5467 	"SEND CHANGE REQUEST",
5468 	"FABRIC LOGOUT",
5469 	"INIT LIP LOGIN",
5470 	NULL,
5471 	"LOGIN LOOP PORT",
5472 	"GET PORT/NODE NAME LIST",
5473 	"SET VENDOR ID",
5474 	"INITIALIZE IP MAILBOX",
5475 	NULL,
5476 	NULL,
5477 	NULL,
5478 	NULL,
5479 	"Get ID List",
5480 	"SEND LFA",
5481 	"Lun RESET"
5482 };
5483 #endif
5484 
5485 static void
5486 isp_mboxcmd_qnw(ispsoftc_t *isp, mbreg_t *mbp, int nodelay)
5487 {
5488 	unsigned int ibits, obits, box, opcode;
5489 	const uint32_t *mcp;
5490 
5491 	if (IS_FC(isp)) {
5492 		mcp = mbpfc;
5493 	} else {
5494 		mcp = mbpscsi;
5495 	}
5496 	opcode = mbp->param[0];
5497 	ibits = HIWRD(mcp[opcode]) & NMBOX_BMASK(isp);
5498 	obits = LOWRD(mcp[opcode]) & NMBOX_BMASK(isp);
5499 	ibits |= mbp->ibits;
5500 	obits |= mbp->obits;
5501 	for (box = 0; box < MAX_MAILBOX(isp); box++) {
5502 		if (ibits & (1 << box)) {
5503 			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
5504 		}
5505 		if (nodelay == 0) {
5506 			isp->isp_mboxtmp[box] = mbp->param[box] = 0;
5507 		}
5508 	}
5509 	if (nodelay == 0) {
5510 		isp->isp_lastmbxcmd = opcode;
5511 		isp->isp_obits = obits;
5512 		isp->isp_mboxbsy = 1;
5513 	}
5514 	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
5515 	/*
5516 	 * Oddly enough, if we're not delaying for an answer,
5517 	 * delay a bit to give the f/w a chance to pick up the
5518 	 * command.
5519 	 */
5520 	if (nodelay) {
5521 		USEC_DELAY(1000);
5522 	}
5523 }
5524 
5525 static void
5526 isp_mboxcmd(ispsoftc_t *isp, mbreg_t *mbp, int logmask)
5527 {
5528 	char *cname, *xname, tname[16], mname[16];
5529 	unsigned int lim, ibits, obits, box, opcode;
5530 	const uint32_t *mcp;
5531 
5532 	if (IS_FC(isp)) {
5533 		mcp = mbpfc;
5534 		lim = (sizeof (mbpfc) / sizeof (mbpfc[0]));
5535 	} else {
5536 		mcp = mbpscsi;
5537 		lim = (sizeof (mbpscsi) / sizeof (mbpscsi[0]));
5538 	}
5539 
5540 	if ((opcode = mbp->param[0]) >= lim) {
5541 		mbp->param[0] = MBOX_INVALID_COMMAND;
5542 		isp_prt(isp, ISP_LOGERR, "Unknown Command 0x%x", opcode);
5543 		return;
5544 	}
5545 
5546 	ibits = HIWRD(mcp[opcode]) & NMBOX_BMASK(isp);
5547 	obits = LOWRD(mcp[opcode]) & NMBOX_BMASK(isp);
5548 
5549 	/*
5550 	 * Pick up any additional bits that the caller might have set.
5551 	 */
5552 	ibits |= mbp->ibits;
5553 	obits |= mbp->obits;
5554 
5555 	if (ibits == 0 && obits == 0) {
5556 		mbp->param[0] = MBOX_COMMAND_PARAM_ERROR;
5557 		isp_prt(isp, ISP_LOGERR, "no parameters for 0x%x", opcode);
5558 		return;
5559 	}
5560 
5561 	/*
5562 	 * Get exclusive usage of mailbox registers.
5563 	 */
5564 	MBOX_ACQUIRE(isp);
5565 
5566 	for (box = 0; box < MAX_MAILBOX(isp); box++) {
5567 		if (ibits & (1 << box)) {
5568 			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
5569 		}
5570 		isp->isp_mboxtmp[box] = mbp->param[box] = 0;
5571 	}
5572 
5573 	isp->isp_lastmbxcmd = opcode;
5574 
5575 	/*
5576 	 * We assume that we can't overwrite a previous command.
5577 	 */
5578 	isp->isp_obits = obits;
5579 	isp->isp_mboxbsy = 1;
5580 
5581 	/*
5582 	 * Set Host Interrupt condition so that RISC will pick up mailbox regs.
5583 	 */
5584 	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
5585 
5586 	/*
5587 	 * While we haven't finished the command, spin our wheels here.
5588 	 */
5589 	MBOX_WAIT_COMPLETE(isp);
5590 
5591 	/*
5592 	 * Did the command time out?
5593 	 */
5594 	if (isp->isp_mboxbsy) {
5595 		isp->isp_mboxbsy = 0;
5596 		MBOX_RELEASE(isp);
5597 		return;
5598 	}
5599 
5600 	/*
5601 	 * Copy back output registers.
5602 	 */
5603 	for (box = 0; box < MAX_MAILBOX(isp); box++) {
5604 		if (obits & (1 << box)) {
5605 			mbp->param[box] = isp->isp_mboxtmp[box];
5606 		}
5607 	}
5608 
5609 	MBOX_RELEASE(isp);
5610 
5611 	if (logmask == 0 || opcode == MBOX_EXEC_FIRMWARE) {
5612 		return;
5613 	}
5614 #ifdef	ISP_STRIPPED
5615 	cname = NULL;
5616 #else
5617 	cname = (IS_FC(isp))? fc_mbcmd_names[opcode] : scsi_mbcmd_names[opcode];
5618 #endif
5619 	if (cname == NULL) {
5620 		cname = tname;
5621 		SNPRINTF(tname, sizeof tname, "opcode %x", opcode);
5622 	}
5623 
5624 	/*
5625 	 * Just to be chatty here...
5626 	 */
5627 	xname = NULL;
5628 	switch (mbp->param[0]) {
5629 	case MBOX_COMMAND_COMPLETE:
5630 		break;
5631 	case MBOX_INVALID_COMMAND:
5632 		if (logmask & MBLOGMASK(MBOX_COMMAND_COMPLETE))
5633 			xname = "INVALID COMMAND";
5634 		break;
5635 	case MBOX_HOST_INTERFACE_ERROR:
5636 		if (logmask & MBLOGMASK(MBOX_HOST_INTERFACE_ERROR))
5637 			xname = "HOST INTERFACE ERROR";
5638 		break;
5639 	case MBOX_TEST_FAILED:
5640 		if (logmask & MBLOGMASK(MBOX_TEST_FAILED))
5641 			xname = "TEST FAILED";
5642 		break;
5643 	case MBOX_COMMAND_ERROR:
5644 		if (logmask & MBLOGMASK(MBOX_COMMAND_ERROR))
5645 			xname = "COMMAND ERROR";
5646 		break;
5647 	case MBOX_COMMAND_PARAM_ERROR:
5648 		if (logmask & MBLOGMASK(MBOX_COMMAND_PARAM_ERROR))
5649 			xname = "COMMAND PARAMETER ERROR";
5650 		break;
5651 	case MBOX_LOOP_ID_USED:
5652 		if (logmask & MBLOGMASK(MBOX_LOOP_ID_USED))
5653 			xname = "LOOP ID ALREADY IN USE";
5654 		break;
5655 	case MBOX_PORT_ID_USED:
5656 		if (logmask & MBLOGMASK(MBOX_PORT_ID_USED))
5657 			xname = "PORT ID ALREADY IN USE";
5658 		break;
5659 	case MBOX_ALL_IDS_USED:
5660 		if (logmask & MBLOGMASK(MBOX_ALL_IDS_USED))
5661 			xname = "ALL LOOP IDS IN USE";
5662 		break;
5663 	case 0:		/* special case */
5664 		xname = "TIMEOUT";
5665 		break;
5666 	default:
5667 		SNPRINTF(mname, sizeof mname, "error 0x%x", mbp->param[0]);
5668 		xname = mname;
5669 		break;
5670 	}
5671 	if (xname)
5672 		isp_prt(isp, ISP_LOGALL, "Mailbox Command '%s' failed (%s)",
5673 		    cname, xname);
5674 }
5675 
5676 static void
5677 isp_fw_state(ispsoftc_t *isp)
5678 {
5679 	if (IS_FC(isp)) {
5680 		mbreg_t mbs;
5681 		fcparam *fcp = isp->isp_param;
5682 
5683 		MEMZERO(&mbs, sizeof (mbs));
5684 		mbs.param[0] = MBOX_GET_FW_STATE;
5685 		isp_mboxcmd(isp, &mbs, MBLOGALL);
5686 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
5687 			fcp->isp_fwstate = mbs.param[1];
5688 		}
5689 	}
5690 }
5691 
5692 static void
5693 isp_update(ispsoftc_t *isp)
5694 {
5695 	int bus, upmask;
5696 
5697 	for (bus = 0, upmask = isp->isp_update; upmask != 0; bus++) {
5698 		if (upmask & (1 << bus)) {
5699 			isp_update_bus(isp, bus);
5700 		}
5701 		upmask &= ~(1 << bus);
5702 	}
5703 }
5704 
5705 static void
5706 isp_update_bus(ispsoftc_t *isp, int bus)
5707 {
5708 	int tgt;
5709 	mbreg_t mbs;
5710 	sdparam *sdp;
5711 
5712 	isp->isp_update &= ~(1 << bus);
5713 	if (IS_FC(isp)) {
5714 		/*
5715 		 * There are no 'per-bus' settings for Fibre Channel.
5716 		 */
5717 		return;
5718 	}
5719 	sdp = isp->isp_param;
5720 	sdp += bus;
5721 	MEMZERO(&mbs, sizeof (mbs));
5722 
5723 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
5724 		uint16_t flags, period, offset;
5725 		int get;
5726 
5727 		if (sdp->isp_devparam[tgt].dev_enable == 0) {
5728 			sdp->isp_devparam[tgt].dev_update = 0;
5729 			sdp->isp_devparam[tgt].dev_refresh = 0;
5730 			isp_prt(isp, ISP_LOGDEBUG0,
5731 	 		    "skipping target %d bus %d update", tgt, bus);
5732 			continue;
5733 		}
5734 		/*
5735 		 * If the goal is to update the status of the device,
5736 		 * take what's in goal_flags and try and set the device
5737 		 * toward that. Otherwise, if we're just refreshing the
5738 		 * current device state, get the current parameters.
5739 		 */
5740 
5741 		/*
5742 		 * Refresh overrides set
5743 		 */
5744 		if (sdp->isp_devparam[tgt].dev_refresh) {
5745 			mbs.param[0] = MBOX_GET_TARGET_PARAMS;
5746 			sdp->isp_devparam[tgt].dev_refresh = 0;
5747 			get = 1;
5748 		} else if (sdp->isp_devparam[tgt].dev_update) {
5749 			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
5750 			/*
5751 			 * Make sure goal_flags has "Renegotiate on Error"
5752 			 * on and "Freeze Queue on Error" off.
5753 			 */
5754 			sdp->isp_devparam[tgt].goal_flags |= DPARM_RENEG;
5755 			sdp->isp_devparam[tgt].goal_flags &= ~DPARM_QFRZ;
5756 
5757 			mbs.param[2] = sdp->isp_devparam[tgt].goal_flags;
5758 
5759 			/*
5760 			 * Insist that PARITY must be enabled
5761 			 * if SYNC or WIDE is enabled.
5762 			 */
5763 			if ((mbs.param[2] & (DPARM_SYNC|DPARM_WIDE)) != 0) {
5764 				mbs.param[2] |= DPARM_PARITY;
5765 			}
5766 
5767 			if ((mbs.param[2] & DPARM_SYNC) == 0) {
5768 				mbs.param[3] = 0;
5769 			} else {
5770 				mbs.param[3] =
5771 				    (sdp->isp_devparam[tgt].goal_offset << 8) |
5772 				    (sdp->isp_devparam[tgt].goal_period);
5773 			}
5774 			/*
5775 			 * A command completion later that has
5776 			 * RQSTF_NEGOTIATION set can cause
5777 			 * the dev_refresh/announce cycle also.
5778 			 *
5779 			 * Note: It is really important to update our current
5780 			 * flags with at least the state of TAG capabilities-
5781 			 * otherwise we might try and send a tagged command
5782 			 * when we have it all turned off. So change it here
5783 			 * to say that current already matches goal.
5784 			 */
5785 			sdp->isp_devparam[tgt].actv_flags &= ~DPARM_TQING;
5786 			sdp->isp_devparam[tgt].actv_flags |=
5787 			    (sdp->isp_devparam[tgt].goal_flags & DPARM_TQING);
5788 			isp_prt(isp, ISP_LOGDEBUG0,
5789 			    "bus %d set tgt %d flags 0x%x off 0x%x period 0x%x",
5790 			    bus, tgt, mbs.param[2], mbs.param[3] >> 8,
5791 			    mbs.param[3] & 0xff);
5792 			sdp->isp_devparam[tgt].dev_update = 0;
5793 			sdp->isp_devparam[tgt].dev_refresh = 1;
5794 			get = 0;
5795 		} else {
5796 			continue;
5797 		}
5798 		mbs.param[1] = (bus << 15) | (tgt << 8);
5799 		isp_mboxcmd(isp, &mbs, MBLOGALL);
5800 		if (get == 0) {
5801 			isp->isp_sendmarker |= (1 << bus);
5802 			continue;
5803 		}
5804 		flags = mbs.param[2];
5805 		period = mbs.param[3] & 0xff;
5806 		offset = mbs.param[3] >> 8;
5807 		sdp->isp_devparam[tgt].actv_flags = flags;
5808 		sdp->isp_devparam[tgt].actv_period = period;
5809 		sdp->isp_devparam[tgt].actv_offset = offset;
5810 		get = (bus << 16) | tgt;
5811 		(void) isp_async(isp, ISPASYNC_NEW_TGT_PARAMS, &get);
5812 	}
5813 
5814 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
5815 		if (sdp->isp_devparam[tgt].dev_update ||
5816 		    sdp->isp_devparam[tgt].dev_refresh) {
5817 			isp->isp_update |= (1 << bus);
5818 			break;
5819 		}
5820 	}
5821 }
5822 
5823 #ifndef	DEFAULT_FRAMESIZE
5824 #define	DEFAULT_FRAMESIZE(isp)		ICB_DFLT_FRMLEN
5825 #endif
5826 #ifndef	DEFAULT_EXEC_THROTTLE
5827 #define	DEFAULT_EXEC_THROTTLE(isp)	ISP_EXEC_THROTTLE
5828 #endif
5829 
5830 static void
5831 isp_setdfltparm(ispsoftc_t *isp, int channel)
5832 {
5833 	int tgt;
5834 	mbreg_t mbs;
5835 	sdparam *sdp;
5836 
5837 	MEMZERO(&mbs, sizeof (mbs));
5838 	if (IS_FC(isp)) {
5839 		fcparam *fcp = (fcparam *) isp->isp_param;
5840 		int nvfail;
5841 
5842 		fcp += channel;
5843 		if (fcp->isp_gotdparms) {
5844 			return;
5845 		}
5846 		fcp->isp_gotdparms = 1;
5847 		fcp->isp_maxfrmlen = DEFAULT_FRAMESIZE(isp);
5848 		fcp->isp_maxalloc = ICB_DFLT_ALLOC;
5849 		fcp->isp_execthrottle = DEFAULT_EXEC_THROTTLE(isp);
5850 		fcp->isp_retry_delay = ICB_DFLT_RDELAY;
5851 		fcp->isp_retry_count = ICB_DFLT_RCOUNT;
5852 		/* Platform specific.... */
5853 		fcp->isp_loopid = DEFAULT_LOOPID(isp);
5854 		fcp->isp_nodewwn = DEFAULT_NODEWWN(isp);
5855 		fcp->isp_portwwn = DEFAULT_PORTWWN(isp);
5856 		fcp->isp_fwoptions = 0;
5857 		fcp->isp_fwoptions |= ICBOPT_FAIRNESS;
5858 		fcp->isp_fwoptions |= ICBOPT_PDBCHANGE_AE;
5859 		fcp->isp_fwoptions |= ICBOPT_HARD_ADDRESS;
5860 		fcp->isp_fwoptions |= ICBOPT_FAST_POST;
5861 		if (isp->isp_confopts & ISP_CFG_FULL_DUPLEX)
5862 			fcp->isp_fwoptions |= ICBOPT_FULL_DUPLEX;
5863 
5864 		/*
5865 		 * Make sure this is turned off now until we get
5866 		 * extended options from NVRAM
5867 		 */
5868 		fcp->isp_fwoptions &= ~ICBOPT_EXTENDED;
5869 
5870 		/*
5871 		 * Now try and read NVRAM unless told to not do so.
5872 		 * This will set fcparam's isp_nodewwn && isp_portwwn.
5873 		 */
5874 		if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
5875 		    	nvfail = isp_read_nvram(isp);
5876 			if (nvfail) {
5877 				isp->isp_confopts |= ISP_CFG_NONVRAM;
5878 			}
5879 		} else {
5880 			nvfail = 1;
5881 		}
5882 		/*
5883 		 * Set node && port to override platform set defaults
5884 		 * unless the nvram read failed (or none was done),
5885 		 * or the platform code wants to use what had been
5886 		 * set in the defaults.
5887 		 */
5888 		if (nvfail) {
5889 			isp->isp_confopts |= ISP_CFG_OWNWWPN|ISP_CFG_OWNWWNN;
5890 		}
5891 		if (isp->isp_confopts & ISP_CFG_OWNWWNN) {
5892 			isp_prt(isp, ISP_LOGCONFIG, "Using Node WWN 0x%08x%08x",
5893 			    (uint32_t) (DEFAULT_NODEWWN(isp) >> 32),
5894 			    (uint32_t) (DEFAULT_NODEWWN(isp) & 0xffffffff));
5895 			ISP_NODEWWN(isp) = DEFAULT_NODEWWN(isp);
5896 		} else {
5897 			/*
5898 			 * We always start out with values derived
5899 			 * from NVRAM or our platform default.
5900 			 */
5901 			ISP_NODEWWN(isp) = fcp->isp_nodewwn;
5902 			if (fcp->isp_nodewwn == 0) {
5903 				isp_prt(isp, ISP_LOGCONFIG,
5904 				    "bad WWNN- using default\n");
5905 				ISP_NODEWWN(isp) = DEFAULT_NODEWWN(isp);
5906 			}
5907 		}
5908 		if (isp->isp_confopts & ISP_CFG_OWNWWPN) {
5909 			isp_prt(isp, ISP_LOGCONFIG, "Using Port WWN 0x%08x%08x",
5910 			    (uint32_t) (DEFAULT_PORTWWN(isp) >> 32),
5911 			    (uint32_t) (DEFAULT_PORTWWN(isp) & 0xffffffff));
5912 			ISP_PORTWWN(isp) = DEFAULT_PORTWWN(isp);
5913 		} else {
5914 			/*
5915 			 * We always start out with values derived
5916 			 * from NVRAM or our platform default.
5917 			 */
5918 			ISP_PORTWWN(isp) = fcp->isp_portwwn;
5919 			if (fcp->isp_portwwn == 0) {
5920 				isp_prt(isp, ISP_LOGCONFIG,
5921 				    "bad WWPN- using default\n");
5922 				ISP_PORTWWN(isp) = DEFAULT_PORTWWN(isp);
5923 			}
5924 		}
5925 		return;
5926 	}
5927 
5928 	sdp = (sdparam *) isp->isp_param;
5929 	sdp += channel;
5930 
5931 	/*
5932 	 * Been there, done that, got the T-shirt...
5933 	 */
5934 	if (sdp->isp_gotdparms) {
5935 		return;
5936 	}
5937 	sdp->isp_gotdparms = 1;
5938 
5939 	/*
5940 	 * Establish some default parameters.
5941 	 */
5942 	sdp->isp_cmd_dma_burst_enable = 0;
5943 	sdp->isp_data_dma_burst_enabl = 1;
5944 	sdp->isp_fifo_threshold = 0;
5945 	sdp->isp_initiator_id = DEFAULT_IID(isp);
5946 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
5947 		sdp->isp_async_data_setup = 9;
5948 	} else {
5949 		sdp->isp_async_data_setup = 6;
5950 	}
5951 	sdp->isp_selection_timeout = 250;
5952 	sdp->isp_max_queue_depth = MAXISPREQUEST(isp);
5953 	sdp->isp_tag_aging = 8;
5954 	sdp->isp_bus_reset_delay = 5;
5955 	/*
5956 	 * Don't retry selection, busy or queue full automatically- reflect
5957 	 * these back to us.
5958 	 */
5959 	sdp->isp_retry_count = 0;
5960 	sdp->isp_retry_delay = 0;
5961 
5962 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
5963 		sdp->isp_devparam[tgt].exc_throttle = ISP_EXEC_THROTTLE;
5964 		sdp->isp_devparam[tgt].dev_enable = 1;
5965 	}
5966 
5967 	/*
5968 	 * If we've not been told to avoid reading NVRAM, try and read it.
5969 	 * If we're successful reading it, we can then return because NVRAM
5970 	 * will tell us what the desired settings are. Otherwise, we establish
5971 	 * some reasonable 'fake' nvram and goal defaults.
5972 	 */
5973 
5974 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
5975 		if (isp_read_nvram(isp) == 0) {
5976 			return;
5977 		}
5978 	}
5979 
5980 	/*
5981 	 * Now try and see whether we have specific values for them.
5982 	 */
5983 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
5984 		mbs.param[0] = MBOX_GET_ACT_NEG_STATE;
5985 		isp_mboxcmd(isp, &mbs, MBLOGNONE);
5986 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
5987 			sdp->isp_req_ack_active_neg = 1;
5988 			sdp->isp_data_line_active_neg = 1;
5989 		} else {
5990 			sdp->isp_req_ack_active_neg =
5991 			    (mbs.param[1+channel] >> 4) & 0x1;
5992 			sdp->isp_data_line_active_neg =
5993 			    (mbs.param[1+channel] >> 5) & 0x1;
5994 		}
5995 	}
5996 
5997 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc3,
5998 	    0, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
5999 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
6000 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
6001 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc3,
6002 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
6003 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
6004 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
6005 
6006 	/*
6007 	 * The trick here is to establish a default for the default (honk!)
6008 	 * state (goal_flags). Then try and get the current status from
6009 	 * the card to fill in the current state. We don't, in fact, set
6010 	 * the default to the SAFE default state- that's not the goal state.
6011 	 */
6012 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
6013 		uint8_t off, per;
6014 		sdp->isp_devparam[tgt].actv_offset = 0;
6015 		sdp->isp_devparam[tgt].actv_period = 0;
6016 		sdp->isp_devparam[tgt].actv_flags = 0;
6017 
6018 		sdp->isp_devparam[tgt].goal_flags =
6019 		    sdp->isp_devparam[tgt].nvrm_flags = DPARM_DEFAULT;
6020 
6021 		/*
6022 		 * We default to Wide/Fast for versions less than a 1040
6023 		 * (unless it's SBus).
6024 		 */
6025 		if (IS_ULTRA3(isp)) {
6026 			off = ISP_80M_SYNCPARMS >> 8;
6027 			per = ISP_80M_SYNCPARMS & 0xff;
6028 		} else if (IS_ULTRA2(isp)) {
6029 			off = ISP_40M_SYNCPARMS >> 8;
6030 			per = ISP_40M_SYNCPARMS & 0xff;
6031 		} else if (IS_1240(isp)) {
6032 			off = ISP_20M_SYNCPARMS >> 8;
6033 			per = ISP_20M_SYNCPARMS & 0xff;
6034 		} else if ((isp->isp_bustype == ISP_BT_SBUS &&
6035 		    isp->isp_type < ISP_HA_SCSI_1020A) ||
6036 		    (isp->isp_bustype == ISP_BT_PCI &&
6037 		    isp->isp_type < ISP_HA_SCSI_1040) ||
6038 		    (isp->isp_clock && isp->isp_clock < 60) ||
6039 		    (sdp->isp_ultramode == 0)) {
6040 			off = ISP_10M_SYNCPARMS >> 8;
6041 			per = ISP_10M_SYNCPARMS & 0xff;
6042 		} else {
6043 			off = ISP_20M_SYNCPARMS_1040 >> 8;
6044 			per = ISP_20M_SYNCPARMS_1040 & 0xff;
6045 		}
6046 		sdp->isp_devparam[tgt].goal_offset =
6047 		    sdp->isp_devparam[tgt].nvrm_offset = off;
6048 		sdp->isp_devparam[tgt].goal_period =
6049 		    sdp->isp_devparam[tgt].nvrm_period = per;
6050 
6051 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc3,
6052 		    channel, tgt, sdp->isp_devparam[tgt].nvrm_flags,
6053 		    sdp->isp_devparam[tgt].nvrm_offset,
6054 		    sdp->isp_devparam[tgt].nvrm_period);
6055 	}
6056 }
6057 
6058 /*
6059  * Re-initialize the ISP and complete all orphaned commands
6060  * with a 'botched' notice. The reset/init routines should
6061  * not disturb an already active list of commands.
6062  *
6063  * Locks held prior to coming here.
6064  */
6065 
6066 void
6067 isp_reinit(ispsoftc_t *isp)
6068 {
6069 	XS_T *xs;
6070 	int i;
6071 
6072 	if (IS_FC(isp)) {
6073 		isp_mark_getpdb_all(isp);
6074 	}
6075 	isp_reset(isp);
6076 	if (isp->isp_state != ISP_RESETSTATE) {
6077 		isp_prt(isp, ISP_LOGERR, "isp_reinit cannot reset card");
6078 	} else if (isp->isp_role != ISP_ROLE_NONE) {
6079 		isp_init(isp);
6080 		if (isp->isp_state == ISP_INITSTATE) {
6081 			isp->isp_state = ISP_RUNSTATE;
6082 		}
6083 		if (isp->isp_state != ISP_RUNSTATE) {
6084 			isp_prt(isp, ISP_LOGERR,
6085 			    "isp_reinit cannot restart card");
6086 		}
6087 	}
6088 	isp->isp_nactive = 0;
6089 
6090 	for (i = 0; i < isp->isp_maxcmds; i++) {
6091 		uint16_t handle;
6092 		xs = isp->isp_xflist[i];
6093 		if (xs == NULL) {
6094 			continue;
6095 		}
6096 		handle = isp_index_handle(i);
6097 		isp_destroy_handle(isp, handle);
6098 		if (XS_XFRLEN(xs)) {
6099 			ISP_DMAFREE(isp, xs, handle);
6100 			XS_RESID(xs) = XS_XFRLEN(xs);
6101 		} else {
6102 			XS_RESID(xs) = 0;
6103 		}
6104 		XS_SETERR(xs, HBA_BUSRESET);
6105 		isp_done(xs);
6106 	}
6107 }
6108 
6109 /*
6110  * NVRAM Routines
6111  */
6112 static int
6113 isp_read_nvram(ispsoftc_t *isp)
6114 {
6115 	int i, amt, retval;
6116 	uint8_t csum, minversion;
6117 	union {
6118 		uint8_t _x[ISP2100_NVRAM_SIZE];
6119 		uint16_t _s[ISP2100_NVRAM_SIZE>>1];
6120 	} _n;
6121 #define	nvram_data	_n._x
6122 #define	nvram_words	_n._s
6123 
6124 	if (IS_FC(isp)) {
6125 		amt = ISP2100_NVRAM_SIZE;
6126 		minversion = 1;
6127 	} else if (IS_ULTRA2(isp)) {
6128 		amt = ISP1080_NVRAM_SIZE;
6129 		minversion = 0;
6130 	} else {
6131 		amt = ISP_NVRAM_SIZE;
6132 		minversion = 2;
6133 	}
6134 
6135 	/*
6136 	 * Just read the first two words first to see if we have a valid
6137 	 * NVRAM to continue reading the rest with.
6138 	 */
6139 	for (i = 0; i < 2; i++) {
6140 		isp_rdnvram_word(isp, i, &nvram_words[i]);
6141 	}
6142 	if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
6143 	    nvram_data[2] != 'P') {
6144 		if (isp->isp_bustype != ISP_BT_SBUS) {
6145 			isp_prt(isp, ISP_LOGWARN, "invalid NVRAM header");
6146 			isp_prt(isp, ISP_LOGDEBUG0, "%x %x %x",
6147 			    nvram_data[0], nvram_data[1], nvram_data[2]);
6148 		}
6149 		retval = -1;
6150 		goto out;
6151 	}
6152 	for (i = 2; i < amt>>1; i++) {
6153 		isp_rdnvram_word(isp, i, &nvram_words[i]);
6154 	}
6155 	for (csum = 0, i = 0; i < amt; i++) {
6156 		csum += nvram_data[i];
6157 	}
6158 	if (csum != 0) {
6159 		isp_prt(isp, ISP_LOGWARN, "invalid NVRAM checksum");
6160 		retval = -1;
6161 		goto out;
6162 	}
6163 	if (ISP_NVRAM_VERSION(nvram_data) < minversion) {
6164 		isp_prt(isp, ISP_LOGWARN, "version %d NVRAM not understood",
6165 		    ISP_NVRAM_VERSION(nvram_data));
6166 		retval = -1;
6167 		goto out;
6168 	}
6169 
6170 	if (IS_ULTRA3(isp)) {
6171 		isp_parse_nvram_12160(isp, 0, nvram_data);
6172 		if (IS_12160(isp))
6173 			isp_parse_nvram_12160(isp, 1, nvram_data);
6174 	} else if (IS_1080(isp)) {
6175 		isp_parse_nvram_1080(isp, 0, nvram_data);
6176 	} else if (IS_1280(isp) || IS_1240(isp)) {
6177 		isp_parse_nvram_1080(isp, 0, nvram_data);
6178 		isp_parse_nvram_1080(isp, 1, nvram_data);
6179 	} else if (IS_SCSI(isp)) {
6180 		isp_parse_nvram_1020(isp, nvram_data);
6181 	} else {
6182 		isp_parse_nvram_2100(isp, nvram_data);
6183 	}
6184 	retval = 0;
6185 out:
6186 	return (retval);
6187 #undef	nvram_data
6188 #undef	nvram_words
6189 }
6190 
6191 static void
6192 isp_rdnvram_word(ispsoftc_t *isp, int wo, uint16_t *rp)
6193 {
6194 	int i, cbits;
6195 	uint16_t bit, rqst, junk;
6196 
6197 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
6198 	USEC_DELAY(10);
6199 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
6200 	USEC_DELAY(10);
6201 
6202 	if (IS_FC(isp)) {
6203 		wo &= ((ISP2100_NVRAM_SIZE >> 1) - 1);
6204 		if (IS_2312(isp) && isp->isp_port) {
6205 			wo += 128;
6206 		}
6207 		rqst = (ISP_NVRAM_READ << 8) | wo;
6208 		cbits = 10;
6209 	} else if (IS_ULTRA2(isp)) {
6210 		wo &= ((ISP1080_NVRAM_SIZE >> 1) - 1);
6211 		rqst = (ISP_NVRAM_READ << 8) | wo;
6212 		cbits = 10;
6213 	} else {
6214 		wo &= ((ISP_NVRAM_SIZE >> 1) - 1);
6215 		rqst = (ISP_NVRAM_READ << 6) | wo;
6216 		cbits = 8;
6217 	}
6218 
6219 	/*
6220 	 * Clock the word select request out...
6221 	 */
6222 	for (i = cbits; i >= 0; i--) {
6223 		if ((rqst >> i) & 1) {
6224 			bit = BIU_NVRAM_SELECT | BIU_NVRAM_DATAOUT;
6225 		} else {
6226 			bit = BIU_NVRAM_SELECT;
6227 		}
6228 		ISP_WRITE(isp, BIU_NVRAM, bit);
6229 		USEC_DELAY(10);
6230 		junk = ISP_READ(isp, BIU_NVRAM);	/* force PCI flush */
6231 		ISP_WRITE(isp, BIU_NVRAM, bit | BIU_NVRAM_CLOCK);
6232 		USEC_DELAY(10);
6233 		junk = ISP_READ(isp, BIU_NVRAM);	/* force PCI flush */
6234 		ISP_WRITE(isp, BIU_NVRAM, bit);
6235 		USEC_DELAY(10);
6236 		junk = ISP_READ(isp, BIU_NVRAM);	/* force PCI flush */
6237 	}
6238 	/*
6239 	 * Now read the result back in (bits come back in MSB format).
6240 	 */
6241 	*rp = 0;
6242 	for (i = 0; i < 16; i++) {
6243 		uint16_t rv;
6244 		*rp <<= 1;
6245 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
6246 		USEC_DELAY(10);
6247 		rv = ISP_READ(isp, BIU_NVRAM);
6248 		if (rv & BIU_NVRAM_DATAIN) {
6249 			*rp |= 1;
6250 		}
6251 		USEC_DELAY(10);
6252 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
6253 		USEC_DELAY(10);
6254 		junk = ISP_READ(isp, BIU_NVRAM);	/* force PCI flush */
6255 	}
6256 	ISP_WRITE(isp, BIU_NVRAM, 0);
6257 	USEC_DELAY(10);
6258 	junk = ISP_READ(isp, BIU_NVRAM);	/* force PCI flush */
6259 	ISP_SWIZZLE_NVRAM_WORD(isp, rp);
6260 }
6261 
6262 static void
6263 isp_parse_nvram_1020(ispsoftc_t *isp, uint8_t *nvram_data)
6264 {
6265 	sdparam *sdp = (sdparam *) isp->isp_param;
6266 	int tgt;
6267 
6268 	sdp->isp_fifo_threshold =
6269 		ISP_NVRAM_FIFO_THRESHOLD(nvram_data) |
6270 		(ISP_NVRAM_FIFO_THRESHOLD_128(nvram_data) << 2);
6271 
6272 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
6273 		sdp->isp_initiator_id =
6274 			ISP_NVRAM_INITIATOR_ID(nvram_data);
6275 
6276 	sdp->isp_bus_reset_delay =
6277 		ISP_NVRAM_BUS_RESET_DELAY(nvram_data);
6278 
6279 	sdp->isp_retry_count =
6280 		ISP_NVRAM_BUS_RETRY_COUNT(nvram_data);
6281 
6282 	sdp->isp_retry_delay =
6283 		ISP_NVRAM_BUS_RETRY_DELAY(nvram_data);
6284 
6285 	sdp->isp_async_data_setup =
6286 		ISP_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data);
6287 
6288 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
6289 		if (sdp->isp_async_data_setup < 9) {
6290 			sdp->isp_async_data_setup = 9;
6291 		}
6292 	} else {
6293 		if (sdp->isp_async_data_setup != 6) {
6294 			sdp->isp_async_data_setup = 6;
6295 		}
6296 	}
6297 
6298 	sdp->isp_req_ack_active_neg =
6299 		ISP_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data);
6300 
6301 	sdp->isp_data_line_active_neg =
6302 		ISP_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data);
6303 
6304 	sdp->isp_data_dma_burst_enabl =
6305 		ISP_NVRAM_DATA_DMA_BURST_ENABLE(nvram_data);
6306 
6307 	sdp->isp_cmd_dma_burst_enable =
6308 		ISP_NVRAM_CMD_DMA_BURST_ENABLE(nvram_data);
6309 
6310 	sdp->isp_tag_aging =
6311 		ISP_NVRAM_TAG_AGE_LIMIT(nvram_data);
6312 
6313 	sdp->isp_selection_timeout =
6314 		ISP_NVRAM_SELECTION_TIMEOUT(nvram_data);
6315 
6316 	sdp->isp_max_queue_depth =
6317 		ISP_NVRAM_MAX_QUEUE_DEPTH(nvram_data);
6318 
6319 	sdp->isp_fast_mttr = ISP_NVRAM_FAST_MTTR_ENABLE(nvram_data);
6320 
6321 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
6322 	    0, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
6323 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
6324 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
6325 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
6326 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
6327 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
6328 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
6329 
6330 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
6331 		sdp->isp_devparam[tgt].dev_enable =
6332 			ISP_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt);
6333 		sdp->isp_devparam[tgt].exc_throttle =
6334 			ISP_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt);
6335 		sdp->isp_devparam[tgt].nvrm_offset =
6336 			ISP_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt);
6337 		sdp->isp_devparam[tgt].nvrm_period =
6338 			ISP_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt);
6339 		/*
6340 		 * We probably shouldn't lie about this, but it
6341 		 * it makes it much safer if we limit NVRAM values
6342 		 * to sanity.
6343 		 */
6344 		if (isp->isp_type < ISP_HA_SCSI_1040) {
6345 			/*
6346 			 * If we're not ultra, we can't possibly
6347 			 * be a shorter period than this.
6348 			 */
6349 			if (sdp->isp_devparam[tgt].nvrm_period < 0x19) {
6350 				sdp->isp_devparam[tgt].nvrm_period = 0x19;
6351 			}
6352 			if (sdp->isp_devparam[tgt].nvrm_offset > 0xc) {
6353 				sdp->isp_devparam[tgt].nvrm_offset = 0x0c;
6354 			}
6355 		} else {
6356 			if (sdp->isp_devparam[tgt].nvrm_offset > 0x8) {
6357 				sdp->isp_devparam[tgt].nvrm_offset = 0x8;
6358 			}
6359 		}
6360 		sdp->isp_devparam[tgt].nvrm_flags = 0;
6361 		if (ISP_NVRAM_TGT_RENEG(nvram_data, tgt))
6362 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
6363 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
6364 		if (ISP_NVRAM_TGT_TQING(nvram_data, tgt))
6365 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
6366 		if (ISP_NVRAM_TGT_SYNC(nvram_data, tgt))
6367 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
6368 		if (ISP_NVRAM_TGT_WIDE(nvram_data, tgt))
6369 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
6370 		if (ISP_NVRAM_TGT_PARITY(nvram_data, tgt))
6371 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
6372 		if (ISP_NVRAM_TGT_DISC(nvram_data, tgt))
6373 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
6374 		sdp->isp_devparam[tgt].actv_flags = 0; /* we don't know */
6375 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
6376 		    0, tgt, sdp->isp_devparam[tgt].nvrm_flags,
6377 		    sdp->isp_devparam[tgt].nvrm_offset,
6378 		    sdp->isp_devparam[tgt].nvrm_period);
6379 		sdp->isp_devparam[tgt].goal_offset =
6380 		    sdp->isp_devparam[tgt].nvrm_offset;
6381 		sdp->isp_devparam[tgt].goal_period =
6382 		    sdp->isp_devparam[tgt].nvrm_period;
6383 		sdp->isp_devparam[tgt].goal_flags =
6384 		    sdp->isp_devparam[tgt].nvrm_flags;
6385 	}
6386 }
6387 
6388 static void
6389 isp_parse_nvram_1080(ispsoftc_t *isp, int bus, uint8_t *nvram_data)
6390 {
6391 	sdparam *sdp = (sdparam *) isp->isp_param;
6392 	int tgt;
6393 
6394 	sdp += bus;
6395 
6396 	sdp->isp_fifo_threshold =
6397 	    ISP1080_NVRAM_FIFO_THRESHOLD(nvram_data);
6398 
6399 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
6400 		sdp->isp_initiator_id =
6401 		    ISP1080_NVRAM_INITIATOR_ID(nvram_data, bus);
6402 
6403 	sdp->isp_bus_reset_delay =
6404 	    ISP1080_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
6405 
6406 	sdp->isp_retry_count =
6407 	    ISP1080_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
6408 
6409 	sdp->isp_retry_delay =
6410 	    ISP1080_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
6411 
6412 	sdp->isp_async_data_setup =
6413 	    ISP1080_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
6414 
6415 	sdp->isp_req_ack_active_neg =
6416 	    ISP1080_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
6417 
6418 	sdp->isp_data_line_active_neg =
6419 	    ISP1080_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
6420 
6421 	sdp->isp_data_dma_burst_enabl =
6422 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
6423 
6424 	sdp->isp_cmd_dma_burst_enable =
6425 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
6426 
6427 	sdp->isp_selection_timeout =
6428 	    ISP1080_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
6429 
6430 	sdp->isp_max_queue_depth =
6431 	     ISP1080_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
6432 
6433 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
6434 	    bus, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
6435 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
6436 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
6437 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
6438 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
6439 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
6440 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
6441 
6442 
6443 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
6444 		sdp->isp_devparam[tgt].dev_enable =
6445 		    ISP1080_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
6446 		sdp->isp_devparam[tgt].exc_throttle =
6447 			ISP1080_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
6448 		sdp->isp_devparam[tgt].nvrm_offset =
6449 			ISP1080_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
6450 		sdp->isp_devparam[tgt].nvrm_period =
6451 			ISP1080_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
6452 		sdp->isp_devparam[tgt].nvrm_flags = 0;
6453 		if (ISP1080_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
6454 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
6455 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
6456 		if (ISP1080_NVRAM_TGT_TQING(nvram_data, tgt, bus))
6457 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
6458 		if (ISP1080_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
6459 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
6460 		if (ISP1080_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
6461 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
6462 		if (ISP1080_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
6463 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
6464 		if (ISP1080_NVRAM_TGT_DISC(nvram_data, tgt, bus))
6465 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
6466 		sdp->isp_devparam[tgt].actv_flags = 0;
6467 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
6468 		    bus, tgt, sdp->isp_devparam[tgt].nvrm_flags,
6469 		    sdp->isp_devparam[tgt].nvrm_offset,
6470 		    sdp->isp_devparam[tgt].nvrm_period);
6471 		sdp->isp_devparam[tgt].goal_offset =
6472 		    sdp->isp_devparam[tgt].nvrm_offset;
6473 		sdp->isp_devparam[tgt].goal_period =
6474 		    sdp->isp_devparam[tgt].nvrm_period;
6475 		sdp->isp_devparam[tgt].goal_flags =
6476 		    sdp->isp_devparam[tgt].nvrm_flags;
6477 	}
6478 }
6479 
6480 static void
6481 isp_parse_nvram_12160(ispsoftc_t *isp, int bus, uint8_t *nvram_data)
6482 {
6483 	sdparam *sdp = (sdparam *) isp->isp_param;
6484 	int tgt;
6485 
6486 	sdp += bus;
6487 
6488 	sdp->isp_fifo_threshold =
6489 	    ISP12160_NVRAM_FIFO_THRESHOLD(nvram_data);
6490 
6491 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
6492 		sdp->isp_initiator_id =
6493 		    ISP12160_NVRAM_INITIATOR_ID(nvram_data, bus);
6494 
6495 	sdp->isp_bus_reset_delay =
6496 	    ISP12160_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
6497 
6498 	sdp->isp_retry_count =
6499 	    ISP12160_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
6500 
6501 	sdp->isp_retry_delay =
6502 	    ISP12160_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
6503 
6504 	sdp->isp_async_data_setup =
6505 	    ISP12160_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
6506 
6507 	sdp->isp_req_ack_active_neg =
6508 	    ISP12160_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
6509 
6510 	sdp->isp_data_line_active_neg =
6511 	    ISP12160_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
6512 
6513 	sdp->isp_data_dma_burst_enabl =
6514 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
6515 
6516 	sdp->isp_cmd_dma_burst_enable =
6517 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
6518 
6519 	sdp->isp_selection_timeout =
6520 	    ISP12160_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
6521 
6522 	sdp->isp_max_queue_depth =
6523 	     ISP12160_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
6524 
6525 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
6526 	    bus, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
6527 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
6528 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
6529 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
6530 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
6531 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
6532 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
6533 
6534 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
6535 		sdp->isp_devparam[tgt].dev_enable =
6536 		    ISP12160_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
6537 		sdp->isp_devparam[tgt].exc_throttle =
6538 			ISP12160_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
6539 		sdp->isp_devparam[tgt].nvrm_offset =
6540 			ISP12160_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
6541 		sdp->isp_devparam[tgt].nvrm_period =
6542 			ISP12160_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
6543 		sdp->isp_devparam[tgt].nvrm_flags = 0;
6544 		if (ISP12160_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
6545 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
6546 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
6547 		if (ISP12160_NVRAM_TGT_TQING(nvram_data, tgt, bus))
6548 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
6549 		if (ISP12160_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
6550 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
6551 		if (ISP12160_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
6552 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
6553 		if (ISP12160_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
6554 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
6555 		if (ISP12160_NVRAM_TGT_DISC(nvram_data, tgt, bus))
6556 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
6557 		sdp->isp_devparam[tgt].actv_flags = 0;
6558 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
6559 		    bus, tgt, sdp->isp_devparam[tgt].nvrm_flags,
6560 		    sdp->isp_devparam[tgt].nvrm_offset,
6561 		    sdp->isp_devparam[tgt].nvrm_period);
6562 		sdp->isp_devparam[tgt].goal_offset =
6563 		    sdp->isp_devparam[tgt].nvrm_offset;
6564 		sdp->isp_devparam[tgt].goal_period =
6565 		    sdp->isp_devparam[tgt].nvrm_period;
6566 		sdp->isp_devparam[tgt].goal_flags =
6567 		    sdp->isp_devparam[tgt].nvrm_flags;
6568 	}
6569 }
6570 
6571 static void
6572 isp_parse_nvram_2100(ispsoftc_t *isp, uint8_t *nvram_data)
6573 {
6574 	fcparam *fcp = (fcparam *) isp->isp_param;
6575 	uint64_t wwn;
6576 
6577 	/*
6578 	 * There is NVRAM storage for both Port and Node entities-
6579 	 * but the Node entity appears to be unused on all the cards
6580 	 * I can find. However, we should account for this being set
6581 	 * at some point in the future.
6582 	 *
6583 	 * Qlogic WWNs have an NAA of 2, but usually nothing shows up in
6584 	 * bits 48..60. In the case of the 2202, it appears that they do
6585 	 * use bit 48 to distinguish between the two instances on the card.
6586 	 * The 2204, which I've never seen, *probably* extends this method.
6587 	 */
6588 	wwn = ISP2100_NVRAM_PORT_NAME(nvram_data);
6589 	if (wwn) {
6590 		isp_prt(isp, ISP_LOGCONFIG, "NVRAM Port WWN 0x%08x%08x",
6591 		    (uint32_t) (wwn >> 32), (uint32_t) (wwn & 0xffffffff));
6592 		if ((wwn >> 60) == 0) {
6593 			wwn |= (((uint64_t) 2)<< 60);
6594 		}
6595 	}
6596 	fcp->isp_portwwn = wwn;
6597 	if (IS_2200(isp) || IS_23XX(isp)) {
6598 		wwn = ISP2100_NVRAM_NODE_NAME(nvram_data);
6599 		if (wwn) {
6600 			isp_prt(isp, ISP_LOGCONFIG, "NVRAM Node WWN 0x%08x%08x",
6601 			    (uint32_t) (wwn >> 32),
6602 			    (uint32_t) (wwn & 0xffffffff));
6603 			if ((wwn >> 60) == 0) {
6604 				wwn |= (((uint64_t) 2)<< 60);
6605 			}
6606 		}
6607 	} else {
6608 		wwn &= ~((uint64_t) 0xfff << 48);
6609 	}
6610 	fcp->isp_nodewwn = wwn;
6611 
6612 	/*
6613 	 * Make sure we have both Node and Port as non-zero values.
6614 	 */
6615 	if (fcp->isp_nodewwn != 0 && fcp->isp_portwwn == 0) {
6616 		fcp->isp_portwwn = fcp->isp_nodewwn;
6617 	} else if (fcp->isp_nodewwn == 0 && fcp->isp_portwwn != 0) {
6618 		fcp->isp_nodewwn = fcp->isp_portwwn;
6619 	}
6620 
6621 	/*
6622 	 * Make the Node and Port values sane if they're NAA == 2.
6623 	 * This means to clear bits 48..56 for the Node WWN and
6624 	 * make sure that there's some non-zero value in 48..56
6625 	 * for the Port WWN.
6626 	 */
6627 	if (fcp->isp_nodewwn && fcp->isp_portwwn) {
6628 		if ((fcp->isp_nodewwn & (((uint64_t) 0xfff) << 48)) != 0 &&
6629 		    (fcp->isp_nodewwn >> 60) == 2) {
6630 			fcp->isp_nodewwn &= ~((uint64_t) 0xfff << 48);
6631 		}
6632 		if ((fcp->isp_portwwn & (((uint64_t) 0xfff) << 48)) == 0 &&
6633 		    (fcp->isp_portwwn >> 60) == 2) {
6634 			fcp->isp_portwwn |= ((uint64_t) 1 << 56);
6635 		}
6636 	}
6637 
6638 	fcp->isp_maxalloc =
6639 		ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data);
6640 	if ((isp->isp_confopts & ISP_CFG_OWNFSZ) == 0)
6641 		fcp->isp_maxfrmlen =
6642 			ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data);
6643 	fcp->isp_retry_delay =
6644 		ISP2100_NVRAM_RETRY_DELAY(nvram_data);
6645 	fcp->isp_retry_count =
6646 		ISP2100_NVRAM_RETRY_COUNT(nvram_data);
6647 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
6648 		fcp->isp_loopid =
6649 			ISP2100_NVRAM_HARDLOOPID(nvram_data);
6650 	if ((isp->isp_confopts & ISP_CFG_OWNEXCTHROTTLE) == 0)
6651 		fcp->isp_execthrottle =
6652 			ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data);
6653 	fcp->isp_fwoptions = ISP2100_NVRAM_OPTIONS(nvram_data);
6654 	isp_prt(isp, ISP_LOGDEBUG0,
6655 	    "NVRAM 0x%08x%08x 0x%08x%08x maxalloc %d maxframelen %d",
6656 	    (uint32_t) (fcp->isp_nodewwn >> 32), (uint32_t) fcp->isp_nodewwn,
6657 	    (uint32_t) (fcp->isp_portwwn >> 32), (uint32_t) fcp->isp_portwwn,
6658 	    ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data),
6659 	    ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data));
6660 	isp_prt(isp, ISP_LOGDEBUG0,
6661 	    "execthrottle %d fwoptions 0x%x hardloop %d tov %d",
6662 	    ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data),
6663 	    ISP2100_NVRAM_OPTIONS(nvram_data),
6664 	    ISP2100_NVRAM_HARDLOOPID(nvram_data),
6665 	    ISP2100_NVRAM_TOV(nvram_data));
6666 	fcp->isp_xfwoptions = ISP2100_XFW_OPTIONS(nvram_data);
6667 	fcp->isp_zfwoptions = ISP2100_ZFW_OPTIONS(nvram_data);
6668 	isp_prt(isp, ISP_LOGDEBUG0,
6669 	    "xfwoptions 0x%x zfw options 0x%x",
6670 	    ISP2100_XFW_OPTIONS(nvram_data), ISP2100_ZFW_OPTIONS(nvram_data));
6671 }
6672 
6673 #ifdef	ISP_FW_CRASH_DUMP
6674 static void isp2200_fw_dump(ispsoftc_t *);
6675 static void isp2300_fw_dump(ispsoftc_t *);
6676 
6677 static void
6678 isp2200_fw_dump(ispsoftc_t *isp)
6679 {
6680 	int i, j;
6681 	mbreg_t mbs;
6682 	uint16_t *ptr;
6683 
6684 	MEMZERO(&mbs, sizeof (mbs));
6685 	ptr = FCPARAM(isp)->isp_dump_data;
6686 	if (ptr == NULL) {
6687 		isp_prt(isp, ISP_LOGERR,
6688 		   "No place to dump RISC registers and SRAM");
6689 		return;
6690 	}
6691 	if (*ptr++) {
6692 		isp_prt(isp, ISP_LOGERR,
6693 		   "dump area for RISC registers and SRAM already used");
6694 		return;
6695 	}
6696 	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
6697 	for (i = 0; i < 100; i++) {
6698 		USEC_DELAY(100);
6699 		if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6700 			break;
6701 		}
6702 	}
6703 	if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6704 		/*
6705 		 * PBIU Registers
6706 		 */
6707 		for (i = 0; i < 8; i++) {
6708 			*ptr++ = ISP_READ(isp, BIU_BLOCK + (i << 1));
6709 		}
6710 
6711 		/*
6712 		 * Mailbox Registers
6713 		 */
6714 		for (i = 0; i < 8; i++) {
6715 			*ptr++ = ISP_READ(isp, MBOX_BLOCK + (i << 1));
6716 		}
6717 
6718 		/*
6719 		 * DMA Registers
6720 		 */
6721 		for (i = 0; i < 48; i++) {
6722 			*ptr++ = ISP_READ(isp, DMA_BLOCK + 0x20 + (i << 1));
6723 		}
6724 
6725 		/*
6726 		 * RISC H/W Registers
6727 		 */
6728 		ISP_WRITE(isp, BIU2100_CSR, 0);
6729 		for (i = 0; i < 16; i++) {
6730 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0xA0 + (i << 1));
6731 		}
6732 
6733 		/*
6734 		 * RISC GP Registers
6735 		 */
6736 		for (j = 0; j < 8; j++) {
6737 			ISP_WRITE(isp, BIU_BLOCK + 0xA4, 0x2000 + (j << 8));
6738 			for (i = 0; i < 16; i++) {
6739 				*ptr++ =
6740 				    ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6741 			}
6742 		}
6743 
6744 		/*
6745 		 * Frame Buffer Hardware Registers
6746 		 */
6747 		ISP_WRITE(isp, BIU2100_CSR, 0x10);
6748 		for (i = 0; i < 16; i++) {
6749 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6750 		}
6751 
6752 		/*
6753 		 * Fibre Protocol Module 0 Hardware Registers
6754 		 */
6755 		ISP_WRITE(isp, BIU2100_CSR, 0x20);
6756 		for (i = 0; i < 64; i++) {
6757 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6758 		}
6759 
6760 		/*
6761 		 * Fibre Protocol Module 1 Hardware Registers
6762 		 */
6763 		ISP_WRITE(isp, BIU2100_CSR, 0x30);
6764 		for (i = 0; i < 64; i++) {
6765 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6766 		}
6767 	} else {
6768 		isp_prt(isp, ISP_LOGERR, "RISC Would Not Pause");
6769 		return;
6770 	}
6771 	isp_prt(isp, ISP_LOGALL,
6772 	   "isp_fw_dump: RISC registers dumped successfully");
6773 	ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
6774 	for (i = 0; i < 100; i++) {
6775 		USEC_DELAY(100);
6776 		if (ISP_READ(isp, OUTMAILBOX0) == 0) {
6777 			break;
6778 		}
6779 	}
6780 	if (ISP_READ(isp, OUTMAILBOX0) != 0) {
6781 		isp_prt(isp, ISP_LOGERR, "Board Would Not Reset");
6782 		return;
6783 	}
6784 	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
6785 	for (i = 0; i < 100; i++) {
6786 		USEC_DELAY(100);
6787 		if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6788 			break;
6789 		}
6790 	}
6791 	if ((ISP_READ(isp, HCCR) & HCCR_PAUSE) == 0) {
6792 		isp_prt(isp, ISP_LOGERR, "RISC Would Not Pause After Reset");
6793 		return;
6794 	}
6795 	ISP_WRITE(isp, RISC_EMB, 0xf2);
6796 	ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
6797 	for (i = 0; i < 100; i++) {
6798 		USEC_DELAY(100);
6799 		if ((ISP_READ(isp, HCCR) & HCCR_PAUSE) == 0) {
6800 			break;
6801 		}
6802 	}
6803 	ENABLE_INTS(isp);
6804 	mbs.param[0] = MBOX_READ_RAM_WORD;
6805 	mbs.param[1] = 0x1000;
6806 	isp->isp_mbxworkp = (void *) ptr;
6807 	isp->isp_mbxwrk0 = 0xefff;	/* continuation count */
6808 	isp->isp_mbxwrk1 = 0x1001;	/* next SRAM address */
6809 	isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
6810 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
6811 		isp_prt(isp, ISP_LOGWARN,
6812 		    "RAM DUMP FAILED @ WORD %x", isp->isp_mbxwrk1);
6813 		return;
6814 	}
6815 	ptr = isp->isp_mbxworkp;	/* finish fetch of final word */
6816 	*ptr++ = isp->isp_mboxtmp[2];
6817 	isp_prt(isp, ISP_LOGALL, "isp_fw_dump: SRAM dumped successfully");
6818 	FCPARAM(isp)->isp_dump_data[0] = isp->isp_type; /* now used */
6819 	(void) isp_async(isp, ISPASYNC_FW_DUMPED, 0);
6820 }
6821 
6822 static void
6823 isp2300_fw_dump(ispsoftc_t *isp)
6824 {
6825 	int i, j;
6826 	mbreg_t mbs;
6827 	uint16_t *ptr;
6828 
6829 	MEMZERO(&mbs, sizeof (mbs));
6830 	ptr = FCPARAM(isp)->isp_dump_data;
6831 	if (ptr == NULL) {
6832 		isp_prt(isp, ISP_LOGERR,
6833 		   "No place to dump RISC registers and SRAM");
6834 		return;
6835 	}
6836 	if (*ptr++) {
6837 		isp_prt(isp, ISP_LOGERR,
6838 		   "dump area for RISC registers and SRAM already used");
6839 		return;
6840 	}
6841 	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
6842 	for (i = 0; i < 100; i++) {
6843 		USEC_DELAY(100);
6844 		if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6845 			break;
6846 		}
6847 	}
6848 	if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6849 		/*
6850 		 * PBIU registers
6851 		 */
6852 		for (i = 0; i < 8; i++) {
6853 			*ptr++ = ISP_READ(isp, BIU_BLOCK + (i << 1));
6854 		}
6855 
6856 		/*
6857 		 * ReqQ-RspQ-Risc2Host Status registers
6858 		 */
6859 		for (i = 0; i < 8; i++) {
6860 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x10 + (i << 1));
6861 		}
6862 
6863 		/*
6864 		 * Mailbox Registers
6865 		 */
6866 		for (i = 0; i < 32; i++) {
6867 			*ptr++ =
6868 			    ISP_READ(isp, PCI_MBOX_REGS2300_OFF + (i << 1));
6869 		}
6870 
6871 		/*
6872 		 * Auto Request Response DMA registers
6873 		 */
6874 		ISP_WRITE(isp, BIU2100_CSR, 0x40);
6875 		for (i = 0; i < 32; i++) {
6876 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6877 		}
6878 
6879 		/*
6880 		 * DMA registers
6881 		 */
6882 		ISP_WRITE(isp, BIU2100_CSR, 0x50);
6883 		for (i = 0; i < 48; i++) {
6884 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6885 		}
6886 
6887 		/*
6888 		 * RISC hardware registers
6889 		 */
6890 		ISP_WRITE(isp, BIU2100_CSR, 0);
6891 		for (i = 0; i < 16; i++) {
6892 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0xA0 + (i << 1));
6893 		}
6894 
6895 		/*
6896 		 * RISC GP? registers
6897 		 */
6898 		for (j = 0; j < 8; j++) {
6899 			ISP_WRITE(isp, BIU_BLOCK + 0xA4, 0x2000 + (j << 9));
6900 			for (i = 0; i < 16; i++) {
6901 				*ptr++ =
6902 				    ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6903 			}
6904 		}
6905 
6906 		/*
6907 		 * frame buffer hardware registers
6908 		 */
6909 		ISP_WRITE(isp, BIU2100_CSR, 0x10);
6910 		for (i = 0; i < 64; i++) {
6911 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6912 		}
6913 
6914 		/*
6915 		 * FPM B0 hardware registers
6916 		 */
6917 		ISP_WRITE(isp, BIU2100_CSR, 0x20);
6918 		for (i = 0; i < 64; i++) {
6919 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6920 		}
6921 
6922 		/*
6923 		 * FPM B1 hardware registers
6924 		 */
6925 		ISP_WRITE(isp, BIU2100_CSR, 0x30);
6926 		for (i = 0; i < 64; i++) {
6927 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6928 		}
6929 	} else {
6930 		isp_prt(isp, ISP_LOGERR, "RISC Would Not Pause");
6931 		return;
6932 	}
6933 	isp_prt(isp, ISP_LOGALL,
6934 	   "isp_fw_dump: RISC registers dumped successfully");
6935 	ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
6936 	for (i = 0; i < 100; i++) {
6937 		USEC_DELAY(100);
6938 		if (ISP_READ(isp, OUTMAILBOX0) == 0) {
6939 			break;
6940 		}
6941 	}
6942 	if (ISP_READ(isp, OUTMAILBOX0) != 0) {
6943 		isp_prt(isp, ISP_LOGERR, "Board Would Not Reset");
6944 		return;
6945 	}
6946 	ENABLE_INTS(isp);
6947 	mbs.param[0] = MBOX_READ_RAM_WORD;
6948 	mbs.param[1] = 0x800;
6949 	isp->isp_mbxworkp = (void *) ptr;
6950 	isp->isp_mbxwrk0 = 0xf7ff;	/* continuation count */
6951 	isp->isp_mbxwrk1 = 0x801;	/* next SRAM address */
6952 	isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
6953 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
6954 		isp_prt(isp, ISP_LOGWARN,
6955 		    "RAM DUMP FAILED @ WORD %x", isp->isp_mbxwrk1);
6956 		return;
6957 	}
6958 	ptr = isp->isp_mbxworkp;	/* finish fetch of final word */
6959 	*ptr++ = isp->isp_mboxtmp[2];
6960 	mbs.param[0] = MBOX_READ_RAM_WORD_EXTENDED;
6961 	mbs.param[1] = 0;
6962 	mbs.param[8] = 1;
6963 	isp->isp_mbxworkp = (void *) ptr;
6964 	isp->isp_mbxwrk0 = 0xffff;	/* continuation count */
6965 	isp->isp_mbxwrk1 = 0x1;		/* next SRAM address */
6966 	isp->isp_mbxwrk8 = 0x1;
6967 	isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
6968 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
6969 		isp_prt(isp, ISP_LOGWARN,
6970 		    "RAM DUMP FAILED @ WORD %x", 0x10000 + isp->isp_mbxwrk1);
6971 		return;
6972 	}
6973 	ptr = isp->isp_mbxworkp;	/* finish final word */
6974 	*ptr++ = mbs.param[2];
6975 	isp_prt(isp, ISP_LOGALL, "isp_fw_dump: SRAM dumped successfully");
6976 	FCPARAM(isp)->isp_dump_data[0] = isp->isp_type; /* now used */
6977 	(void) isp_async(isp, ISPASYNC_FW_DUMPED, 0);
6978 }
6979 
6980 void
6981 isp_fw_dump(ispsoftc_t *isp)
6982 {
6983 	if (IS_2200(isp))
6984 		isp2200_fw_dump(isp);
6985 	else if (IS_23XX(isp))
6986 		isp2300_fw_dump(isp);
6987 }
6988 #endif
6989