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