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