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