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