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