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