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