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