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