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