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