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