xref: /freebsd-13.1/sys/dev/isp/isp.c (revision dfd1ff19)
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 (IS_2200(isp) || IS_23XX(isp)) {
1692 		int topo = (int) mbs.param[6];
1693 		if (topo < TOPO_NL_PORT || topo > TOPO_PTP_STUB)
1694 			topo = TOPO_PTP_STUB;
1695 		fcp->isp_topo = topo;
1696 	} else {
1697 		fcp->isp_topo = TOPO_NL_PORT;
1698 	}
1699 	/*
1700 	 * XXX: We can get the AL_PA (low 8 bits) from here.
1701 	 * XXX: Where do we get the upper 16 bits?
1702 	 */
1703 	fcp->isp_portid = mbs.param[2] & 0xff;
1704 
1705 	/*
1706 	 * Check to see if we're on a fabric by trying to see if we
1707 	 * can talk to the fabric name server. This can be a bit
1708 	 * tricky because if we're a 2100, we should check always
1709 	 * (in case we're connected to a server doing aliasing).
1710 	 */
1711 	fcp->isp_onfabric = 0;
1712 
1713 	if (IS_2100(isp)) {
1714 		/*
1715 		 * Don't bother with fabric if we are using really old
1716 		 * 2100 firmware. It's just not worth it.
1717 		 */
1718 		if (ISP_FW_NEWER_THAN(isp, 1, 15, 37)) {
1719 			check_for_fabric = 1;
1720 		} else {
1721 			check_for_fabric = 0;
1722 		}
1723 	} else if (fcp->isp_topo == TOPO_FL_PORT ||
1724 	    fcp->isp_topo == TOPO_F_PORT) {
1725 		check_for_fabric = 1;
1726 	} else
1727 		check_for_fabric = 0;
1728 
1729 	if (check_for_fabric && isp_getpdb(isp, FL_PORT_ID, &pdb) == 0) {
1730 		int loopid = FL_PORT_ID;
1731 		if (IS_2100(isp)) {
1732 			fcp->isp_topo = TOPO_FL_PORT;
1733 		}
1734 
1735 		if (BITS2WORD(pdb.pdb_portid_bits) == 0) {
1736 			/*
1737 			 * Crock.
1738 			 */
1739 			fcp->isp_topo = TOPO_NL_PORT;
1740 			goto not_on_fabric;
1741 		}
1742 		fcp->isp_portid = mbs.param[2] | ((int) mbs.param[3] << 16);
1743 
1744 		/*
1745 		 * Save the Fabric controller's port database entry.
1746 		 */
1747 		lp = &fcp->portdb[loopid];
1748 		lp->node_wwn =
1749 		    (((u_int64_t)pdb.pdb_nodename[0]) << 56) |
1750 		    (((u_int64_t)pdb.pdb_nodename[1]) << 48) |
1751 		    (((u_int64_t)pdb.pdb_nodename[2]) << 40) |
1752 		    (((u_int64_t)pdb.pdb_nodename[3]) << 32) |
1753 		    (((u_int64_t)pdb.pdb_nodename[4]) << 24) |
1754 		    (((u_int64_t)pdb.pdb_nodename[5]) << 16) |
1755 		    (((u_int64_t)pdb.pdb_nodename[6]) <<  8) |
1756 		    (((u_int64_t)pdb.pdb_nodename[7]));
1757 		lp->port_wwn =
1758 		    (((u_int64_t)pdb.pdb_portname[0]) << 56) |
1759 		    (((u_int64_t)pdb.pdb_portname[1]) << 48) |
1760 		    (((u_int64_t)pdb.pdb_portname[2]) << 40) |
1761 		    (((u_int64_t)pdb.pdb_portname[3]) << 32) |
1762 		    (((u_int64_t)pdb.pdb_portname[4]) << 24) |
1763 		    (((u_int64_t)pdb.pdb_portname[5]) << 16) |
1764 		    (((u_int64_t)pdb.pdb_portname[6]) <<  8) |
1765 		    (((u_int64_t)pdb.pdb_portname[7]));
1766 		lp->roles =
1767 		    (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
1768 		lp->portid = BITS2WORD(pdb.pdb_portid_bits);
1769 		lp->loopid = pdb.pdb_loopid;
1770 		lp->loggedin = lp->valid = 1;
1771 		fcp->isp_onfabric = 1;
1772 		(void) isp_async(isp, ISPASYNC_PROMENADE, &loopid);
1773 		isp_register_fc4_type(isp);
1774 	} else {
1775 not_on_fabric:
1776 		fcp->isp_onfabric = 0;
1777 		fcp->portdb[FL_PORT_ID].valid = 0;
1778 	}
1779 
1780 	fcp->isp_gbspeed = 1;
1781 	if (IS_23XX(isp)) {
1782 		mbs.param[0] = MBOX_GET_SET_DATA_RATE;
1783 		mbs.param[1] = MBGSD_GET_RATE;
1784 		/* mbs.param[2] undefined if we're just getting rate */
1785 		isp_mboxcmd(isp, &mbs, MBLOGALL);
1786 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1787 			if (mbs.param[1] == MBGSD_TWOGB) {
1788 				isp_prt(isp, ISP_LOGINFO, "2Gb link speed/s");
1789 				fcp->isp_gbspeed = 2;
1790 			}
1791 		}
1792 	}
1793 
1794 	isp_prt(isp, ISP_LOGCONFIG, topology, fcp->isp_loopid,
1795 	    fcp->isp_portid, fcp->isp_loopstate, toponames[fcp->isp_topo]);
1796 
1797 	/*
1798 	 * Announce ourselves, too. This involves synthesizing an entry.
1799 	 */
1800 	if (fcp->isp_iid_set == 0) {
1801 		fcp->isp_iid_set = 1;
1802 		fcp->isp_iid = fcp->isp_loopid;
1803 		lp = &fcp->portdb[fcp->isp_iid];
1804 	} else {
1805 		lp = &fcp->portdb[fcp->isp_iid];
1806 		if (fcp->isp_portid != lp->portid ||
1807 		    fcp->isp_loopid != lp->loopid ||
1808 		    fcp->isp_nodewwn != ISP_NODEWWN(isp) ||
1809 		    fcp->isp_portwwn != ISP_PORTWWN(isp)) {
1810 			lp->valid = 0;
1811 			count = fcp->isp_iid;
1812 			(void) isp_async(isp, ISPASYNC_PROMENADE, &count);
1813 		}
1814 	}
1815 	lp->loopid = fcp->isp_loopid;
1816 	lp->portid = fcp->isp_portid;
1817 	lp->node_wwn = ISP_NODEWWN(isp);
1818 	lp->port_wwn = ISP_PORTWWN(isp);
1819 	switch (isp->isp_role) {
1820 	case ISP_ROLE_NONE:
1821 		lp->roles = 0;
1822 		break;
1823 	case ISP_ROLE_TARGET:
1824 		lp->roles = SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT;
1825 		break;
1826 	case ISP_ROLE_INITIATOR:
1827 		lp->roles = SVC3_INI_ROLE >> SVC3_ROLE_SHIFT;
1828 		break;
1829 	case ISP_ROLE_BOTH:
1830 		lp->roles = (SVC3_INI_ROLE|SVC3_TGT_ROLE) >> SVC3_ROLE_SHIFT;
1831 		break;
1832 	}
1833 	lp->loggedin = lp->valid = 1;
1834 	count = fcp->isp_iid;
1835 	(void) isp_async(isp, ISPASYNC_PROMENADE, &count);
1836 	return (0);
1837 }
1838 
1839 static char *
1840 isp2100_fw_statename(int state)
1841 {
1842 	switch(state) {
1843 	case FW_CONFIG_WAIT:	return "Config Wait";
1844 	case FW_WAIT_AL_PA:	return "Waiting for AL_PA";
1845 	case FW_WAIT_LOGIN:	return "Wait Login";
1846 	case FW_READY:		return "Ready";
1847 	case FW_LOSS_OF_SYNC:	return "Loss Of Sync";
1848 	case FW_ERROR:		return "Error";
1849 	case FW_REINIT:		return "Re-Init";
1850 	case FW_NON_PART:	return "Nonparticipating";
1851 	default:		return "?????";
1852 	}
1853 }
1854 
1855 /*
1856  * Synchronize our soft copy of the port database with what the f/w thinks
1857  * (with a view toward possibly for a specific target....)
1858  */
1859 
1860 static int
1861 isp_pdb_sync(struct ispsoftc *isp)
1862 {
1863 	struct lportdb *lp;
1864 	fcparam *fcp = isp->isp_param;
1865 	isp_pdb_t pdb;
1866 	int loopid, base, lim;
1867 
1868 	/*
1869 	 * Make sure we're okay for doing this right now.
1870 	 */
1871 	if (fcp->isp_loopstate != LOOP_PDB_RCVD &&
1872 	    fcp->isp_loopstate != LOOP_FSCAN_DONE &&
1873 	    fcp->isp_loopstate != LOOP_LSCAN_DONE) {
1874 		return (-1);
1875 	}
1876 
1877 	if (fcp->isp_topo == TOPO_FL_PORT || fcp->isp_topo == TOPO_NL_PORT ||
1878 	    fcp->isp_topo == TOPO_N_PORT) {
1879 		if (fcp->isp_loopstate < LOOP_LSCAN_DONE) {
1880 			if (isp_scan_loop(isp) != 0) {
1881 				return (-1);
1882 			}
1883 		}
1884 	}
1885 	fcp->isp_loopstate = LOOP_SYNCING_PDB;
1886 
1887 	/*
1888 	 * If we get this far, we've settled our differences with the f/w
1889 	 * (for local loop device) and we can say that the loop state is ready.
1890 	 */
1891 
1892 	if (fcp->isp_topo == TOPO_NL_PORT) {
1893 		fcp->loop_seen_once = 1;
1894 		fcp->isp_loopstate = LOOP_READY;
1895 		return (0);
1896 	}
1897 
1898 	/*
1899 	 * Find all Fabric Entities that didn't make it from one scan to the
1900 	 * next and let the world know they went away. Scan the whole database.
1901 	 */
1902 	for (lp = &fcp->portdb[0]; lp < &fcp->portdb[MAX_FC_TARG]; lp++) {
1903 		if (lp->was_fabric_dev && lp->fabric_dev == 0) {
1904 			loopid = lp - fcp->portdb;
1905 			lp->valid = 0;	/* should already be set */
1906 			(void) isp_async(isp, ISPASYNC_PROMENADE, &loopid);
1907 			MEMZERO((void *) lp, sizeof (*lp));
1908 			continue;
1909 		}
1910 		lp->was_fabric_dev = lp->fabric_dev;
1911 	}
1912 
1913 	if (fcp->isp_topo == TOPO_FL_PORT)
1914 		base = FC_SNS_ID+1;
1915 	else
1916 		base = 0;
1917 
1918 	if (fcp->isp_topo == TOPO_N_PORT)
1919 		lim = 1;
1920 	else
1921 		lim = MAX_FC_TARG;
1922 
1923 	/*
1924 	 * Now log in any fabric devices that the outer layer has
1925 	 * left for us to see. This seems the most sane policy
1926 	 * for the moment.
1927 	 */
1928 	for (lp = &fcp->portdb[base]; lp < &fcp->portdb[lim]; lp++) {
1929 		u_int32_t portid;
1930 		mbreg_t mbs;
1931 
1932 		loopid = lp - fcp->portdb;
1933 		if (loopid >= FL_PORT_ID && loopid <= FC_SNS_ID) {
1934 			continue;
1935 		}
1936 
1937 		/*
1938 		 * Anything here?
1939 		 */
1940 		if (lp->port_wwn == 0) {
1941 			continue;
1942 		}
1943 
1944 		/*
1945 		 * Don't try to log into yourself.
1946 		 */
1947 		if ((portid = lp->portid) == fcp->isp_portid) {
1948 			continue;
1949 		}
1950 
1951 
1952 		/*
1953 		 * If we'd been logged in- see if we still are and we haven't
1954 		 * changed. If so, no need to log ourselves out, etc..
1955 		 *
1956 		 * Unfortunately, our charming Qlogic f/w has decided to
1957 		 * return a valid port database entry for a fabric device
1958 		 * that has, in fact, gone away. And it hangs trying to
1959 		 * log it out.
1960 		 */
1961 		if (lp->loggedin && lp->force_logout == 0 &&
1962 		    isp_getpdb(isp, lp->loopid, &pdb) == 0) {
1963 			int nrole;
1964 			u_int64_t nwwnn, nwwpn;
1965 			nwwnn =
1966 			    (((u_int64_t)pdb.pdb_nodename[0]) << 56) |
1967 			    (((u_int64_t)pdb.pdb_nodename[1]) << 48) |
1968 			    (((u_int64_t)pdb.pdb_nodename[2]) << 40) |
1969 			    (((u_int64_t)pdb.pdb_nodename[3]) << 32) |
1970 			    (((u_int64_t)pdb.pdb_nodename[4]) << 24) |
1971 			    (((u_int64_t)pdb.pdb_nodename[5]) << 16) |
1972 			    (((u_int64_t)pdb.pdb_nodename[6]) <<  8) |
1973 			    (((u_int64_t)pdb.pdb_nodename[7]));
1974 			nwwpn =
1975 			    (((u_int64_t)pdb.pdb_portname[0]) << 56) |
1976 			    (((u_int64_t)pdb.pdb_portname[1]) << 48) |
1977 			    (((u_int64_t)pdb.pdb_portname[2]) << 40) |
1978 			    (((u_int64_t)pdb.pdb_portname[3]) << 32) |
1979 			    (((u_int64_t)pdb.pdb_portname[4]) << 24) |
1980 			    (((u_int64_t)pdb.pdb_portname[5]) << 16) |
1981 			    (((u_int64_t)pdb.pdb_portname[6]) <<  8) |
1982 			    (((u_int64_t)pdb.pdb_portname[7]));
1983 			nrole = (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >>
1984 			    SVC3_ROLE_SHIFT;
1985 			if (pdb.pdb_loopid == lp->loopid && lp->portid ==
1986 			    (u_int32_t) BITS2WORD(pdb.pdb_portid_bits) &&
1987 			    nwwnn == lp->node_wwn && nwwpn == lp->port_wwn &&
1988 			    lp->roles == nrole && lp->force_logout == 0) {
1989 				lp->loggedin = lp->valid = 1;
1990 				isp_prt(isp, ISP_LOGCONFIG, lretained,
1991 				    (int) (lp - fcp->portdb),
1992 				    (int) lp->loopid, lp->portid);
1993 				continue;
1994 			}
1995 		}
1996 
1997 		if (fcp->isp_fwstate != FW_READY ||
1998 		    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
1999 			return (-1);
2000 		}
2001 
2002 		/*
2003 		 * Force a logout if we were logged in.
2004 		 */
2005 		if (lp->loggedin) {
2006 			if (lp->force_logout ||
2007 			    isp_getpdb(isp, lp->loopid, &pdb) == 0) {
2008 				MEMZERO(&mbs, sizeof (mbs));
2009 				mbs.param[0] = MBOX_FABRIC_LOGOUT;
2010 				if (IS_2KLOGIN(isp)) {
2011 					mbs.param[1] = lp->loopid;
2012 					mbs.obits |= (1 << 10);
2013 				} else {
2014 					mbs.param[1] = lp->loopid << 8;
2015 				}
2016 				mbs.param[2] = 0;
2017 				mbs.param[3] = 0;
2018 				isp_mboxcmd(isp, &mbs, MBLOGNONE);
2019 				isp_prt(isp, ISP_LOGINFO, plogout,
2020 				    (int) (lp - fcp->portdb), lp->loopid,
2021 				    lp->portid);
2022 			}
2023 			lp->force_logout = lp->loggedin = 0;
2024 			if (fcp->isp_fwstate != FW_READY ||
2025 			    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
2026 				return (-1);
2027 			}
2028 		}
2029 
2030 		/*
2031 		 * And log in....
2032 		 */
2033 		loopid = lp - fcp->portdb;
2034 		lp->loopid = FL_PORT_ID;
2035 		do {
2036 			MEMZERO(&mbs, sizeof (mbs));
2037 			mbs.param[0] = MBOX_FABRIC_LOGIN;
2038 			if (IS_2KLOGIN(isp)) {
2039 				mbs.param[1] = loopid;
2040 				mbs.obits |= (1 << 10);
2041 			} else {
2042 				mbs.param[1] = loopid << 8;
2043 			}
2044 			mbs.param[2] = portid >> 16;
2045 			mbs.param[3] = portid & 0xffff;
2046 			isp_mboxcmd(isp, &mbs, MBLOGALL & ~(MBOX_LOOP_ID_USED |
2047 			    MBOX_PORT_ID_USED | MBOX_COMMAND_ERROR));
2048 			if (fcp->isp_fwstate != FW_READY ||
2049 			    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
2050 				return (-1);
2051 			}
2052 			switch (mbs.param[0]) {
2053 			case MBOX_LOOP_ID_USED:
2054 				/*
2055 				 * Try the next available loop id.
2056 				 */
2057 				loopid++;
2058 				break;
2059 			case MBOX_PORT_ID_USED:
2060 				/*
2061 				 * This port is already logged in.
2062 				 * Snaffle the loop id it's using if it's
2063 				 * nonzero, otherwise we're hosed.
2064 				 */
2065 				if (mbs.param[1] != 0) {
2066 					loopid = mbs.param[1];
2067 					isp_prt(isp, ISP_LOGINFO, retained,
2068 					    loopid, (int) (lp - fcp->portdb),
2069 					    lp->portid);
2070 				} else {
2071 					loopid = MAX_FC_TARG;
2072 					break;
2073 				}
2074 				/* FALLTHROUGH */
2075 			case MBOX_COMMAND_COMPLETE:
2076 				lp->loggedin = 1;
2077 				lp->loopid = loopid;
2078 				break;
2079 			case MBOX_COMMAND_ERROR:
2080 				isp_prt(isp, ISP_LOGINFO, plogierr,
2081 				    portid, mbs.param[1]);
2082 				/* FALLTHROUGH */
2083 			case MBOX_ALL_IDS_USED: /* We're outta IDs */
2084 			default:
2085 				loopid = MAX_FC_TARG;
2086 				break;
2087 			}
2088 		} while (lp->loopid == FL_PORT_ID && loopid < MAX_FC_TARG);
2089 
2090 		/*
2091 		 * If we get here and we haven't set a Loop ID,
2092 		 * we failed to log into this device.
2093 		 */
2094 
2095 		if (lp->loopid == FL_PORT_ID) {
2096 			lp->loopid = 0;
2097 			continue;
2098 		}
2099 
2100 		/*
2101 		 * Make sure we can get the approriate port information.
2102 		 */
2103 		if (isp_getpdb(isp, lp->loopid, &pdb) != 0) {
2104 			isp_prt(isp, ISP_LOGWARN, nopdb, lp->portid);
2105 			goto dump_em;
2106 		}
2107 
2108 		if (fcp->isp_fwstate != FW_READY ||
2109 		    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
2110 			return (-1);
2111 		}
2112 
2113 		if (pdb.pdb_loopid != lp->loopid) {
2114 			isp_prt(isp, ISP_LOGWARN, pdbmfail1,
2115 			    lp->portid, pdb.pdb_loopid);
2116 			goto dump_em;
2117 		}
2118 
2119 		if (lp->portid != (u_int32_t) BITS2WORD(pdb.pdb_portid_bits)) {
2120 			isp_prt(isp, ISP_LOGWARN, pdbmfail2,
2121 			    lp->portid, BITS2WORD(pdb.pdb_portid_bits));
2122 			goto dump_em;
2123 		}
2124 
2125 		lp->roles =
2126 		    (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
2127 		lp->node_wwn =
2128 		    (((u_int64_t)pdb.pdb_nodename[0]) << 56) |
2129 		    (((u_int64_t)pdb.pdb_nodename[1]) << 48) |
2130 		    (((u_int64_t)pdb.pdb_nodename[2]) << 40) |
2131 		    (((u_int64_t)pdb.pdb_nodename[3]) << 32) |
2132 		    (((u_int64_t)pdb.pdb_nodename[4]) << 24) |
2133 		    (((u_int64_t)pdb.pdb_nodename[5]) << 16) |
2134 		    (((u_int64_t)pdb.pdb_nodename[6]) <<  8) |
2135 		    (((u_int64_t)pdb.pdb_nodename[7]));
2136 		lp->port_wwn =
2137 		    (((u_int64_t)pdb.pdb_portname[0]) << 56) |
2138 		    (((u_int64_t)pdb.pdb_portname[1]) << 48) |
2139 		    (((u_int64_t)pdb.pdb_portname[2]) << 40) |
2140 		    (((u_int64_t)pdb.pdb_portname[3]) << 32) |
2141 		    (((u_int64_t)pdb.pdb_portname[4]) << 24) |
2142 		    (((u_int64_t)pdb.pdb_portname[5]) << 16) |
2143 		    (((u_int64_t)pdb.pdb_portname[6]) <<  8) |
2144 		    (((u_int64_t)pdb.pdb_portname[7]));
2145 		/*
2146 		 * Check to make sure this all makes sense.
2147 		 */
2148 		if (lp->node_wwn && lp->port_wwn) {
2149 			lp->valid = 1;
2150 			loopid = lp - fcp->portdb;
2151 			(void) isp_async(isp, ISPASYNC_PROMENADE, &loopid);
2152 			continue;
2153 		}
2154 dump_em:
2155 		lp->valid = 0;
2156 		isp_prt(isp, ISP_LOGINFO,
2157 		    ldumped, loopid, lp->loopid, lp->portid);
2158 		MEMZERO(&mbs, sizeof (mbs));
2159 		mbs.param[0] = MBOX_FABRIC_LOGOUT;
2160 		if (IS_2KLOGIN(isp)) {
2161 			mbs.param[1] = lp->loopid;
2162 			mbs.obits |= (1 << 10);
2163 		} else {
2164 			mbs.param[1] = lp->loopid << 8;
2165 		}
2166 		isp_mboxcmd(isp, &mbs, MBLOGNONE);
2167 		if (fcp->isp_fwstate != FW_READY ||
2168 		    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
2169 			return (-1);
2170 		}
2171 	}
2172 	/*
2173 	 * If we get here, we've for sure seen not only a valid loop
2174 	 * but know what is or isn't on it, so mark this for usage
2175 	 * in isp_start.
2176 	 */
2177 	fcp->loop_seen_once = 1;
2178 	fcp->isp_loopstate = LOOP_READY;
2179 	return (0);
2180 }
2181 
2182 static int
2183 isp_scan_loop(struct ispsoftc *isp)
2184 {
2185 	struct lportdb *lp;
2186 	fcparam *fcp = isp->isp_param;
2187 	isp_pdb_t pdb;
2188 	int loopid, lim, hival;
2189 
2190 	switch (fcp->isp_topo) {
2191 	case TOPO_NL_PORT:
2192 		hival = FL_PORT_ID;
2193 		break;
2194 	case TOPO_N_PORT:
2195 		hival = 2;
2196 		break;
2197 	case TOPO_FL_PORT:
2198 		hival = FC_PORT_ID;
2199 		break;
2200 	default:
2201 		fcp->isp_loopstate = LOOP_LSCAN_DONE;
2202 		return (0);
2203 	}
2204 	fcp->isp_loopstate = LOOP_SCANNING_LOOP;
2205 
2206 	/*
2207 	 * make sure the temp port database is clean...
2208 	 */
2209 	MEMZERO((void *)fcp->tport, sizeof (fcp->tport));
2210 
2211 	/*
2212 	 * Run through the local loop ports and get port database info
2213 	 * for each loop ID.
2214 	 *
2215 	 * There's a somewhat unexplained situation where the f/w passes back
2216 	 * the wrong database entity- if that happens, just restart (up to
2217 	 * FL_PORT_ID times).
2218 	 */
2219 	for (lim = loopid = 0; loopid < hival; loopid++) {
2220 		lp = &fcp->tport[loopid];
2221 
2222 		/*
2223 		 * Don't even try for ourselves...
2224 	 	 */
2225 		if (loopid == fcp->isp_loopid)
2226 			continue;
2227 
2228 		lp->node_wwn = isp_get_portname(isp, loopid, 1);
2229 		if (fcp->isp_loopstate < LOOP_SCANNING_LOOP)
2230 			return (-1);
2231 		if (lp->node_wwn == 0)
2232 			continue;
2233 		lp->port_wwn = isp_get_portname(isp, loopid, 0);
2234 		if (fcp->isp_loopstate < LOOP_SCANNING_LOOP)
2235 			return (-1);
2236 		if (lp->port_wwn == 0) {
2237 			lp->node_wwn = 0;
2238 			continue;
2239 		}
2240 
2241 		/*
2242 		 * Get an entry....
2243 		 */
2244 		if (isp_getpdb(isp, loopid, &pdb) != 0) {
2245 			if (fcp->isp_loopstate < LOOP_SCANNING_LOOP)
2246 				return (-1);
2247 			continue;
2248 		}
2249 		if (fcp->isp_loopstate < LOOP_SCANNING_LOOP) {
2250 			return (-1);
2251 		}
2252 
2253 		/*
2254 		 * If the returned database element doesn't match what we
2255 		 * asked for, restart the process entirely (up to a point...).
2256 		 */
2257 		if (pdb.pdb_loopid != loopid) {
2258 			loopid = 0;
2259 			if (lim++ < hival) {
2260 				continue;
2261 			}
2262 			isp_prt(isp, ISP_LOGWARN,
2263 			    "giving up on synchronizing the port database");
2264 			return (-1);
2265 		}
2266 
2267 		/*
2268 		 * Save the pertinent info locally.
2269 		 */
2270 		lp->node_wwn =
2271 		    (((u_int64_t)pdb.pdb_nodename[0]) << 56) |
2272 		    (((u_int64_t)pdb.pdb_nodename[1]) << 48) |
2273 		    (((u_int64_t)pdb.pdb_nodename[2]) << 40) |
2274 		    (((u_int64_t)pdb.pdb_nodename[3]) << 32) |
2275 		    (((u_int64_t)pdb.pdb_nodename[4]) << 24) |
2276 		    (((u_int64_t)pdb.pdb_nodename[5]) << 16) |
2277 		    (((u_int64_t)pdb.pdb_nodename[6]) <<  8) |
2278 		    (((u_int64_t)pdb.pdb_nodename[7]));
2279 		lp->port_wwn =
2280 		    (((u_int64_t)pdb.pdb_portname[0]) << 56) |
2281 		    (((u_int64_t)pdb.pdb_portname[1]) << 48) |
2282 		    (((u_int64_t)pdb.pdb_portname[2]) << 40) |
2283 		    (((u_int64_t)pdb.pdb_portname[3]) << 32) |
2284 		    (((u_int64_t)pdb.pdb_portname[4]) << 24) |
2285 		    (((u_int64_t)pdb.pdb_portname[5]) << 16) |
2286 		    (((u_int64_t)pdb.pdb_portname[6]) <<  8) |
2287 		    (((u_int64_t)pdb.pdb_portname[7]));
2288 		lp->roles =
2289 		    (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
2290 		lp->portid = BITS2WORD(pdb.pdb_portid_bits);
2291 		lp->loopid = pdb.pdb_loopid;
2292 	}
2293 
2294 	/*
2295 	 * Mark all of the permanent local loop database entries as invalid
2296 	 * (except our own entry).
2297 	 */
2298 	for (loopid = 0; loopid < hival; loopid++) {
2299 		if (loopid == fcp->isp_iid) {
2300 			fcp->portdb[loopid].valid = 1;
2301 			fcp->portdb[loopid].loopid = fcp->isp_loopid;
2302 			continue;
2303 		}
2304 		fcp->portdb[loopid].valid = 0;
2305 	}
2306 
2307 	/*
2308 	 * Now merge our local copy of the port database into our saved copy.
2309 	 * Notify the outer layers of new devices arriving.
2310 	 */
2311 	for (loopid = 0; loopid < hival; loopid++) {
2312 		int i;
2313 
2314 		/*
2315 		 * If we don't have a non-zero Port WWN, we're not here.
2316 		 */
2317 		if (fcp->tport[loopid].port_wwn == 0) {
2318 			continue;
2319 		}
2320 
2321 		/*
2322 		 * Skip ourselves.
2323 		 */
2324 		if (loopid == fcp->isp_iid) {
2325 			continue;
2326 		}
2327 
2328 		/*
2329 		 * For the purposes of deciding whether this is the
2330 		 * 'same' device or not, we only search for an identical
2331 		 * Port WWN. Node WWNs may or may not be the same as
2332 		 * the Port WWN, and there may be multiple different
2333 		 * Port WWNs with the same Node WWN. It would be chaos
2334 		 * to have multiple identical Port WWNs, so we don't
2335 		 * allow that.
2336 		 */
2337 
2338 		for (i = 0; i < hival; i++) {
2339 			int j;
2340 			if (fcp->portdb[i].port_wwn == 0)
2341 				continue;
2342 			if (fcp->portdb[i].port_wwn !=
2343 			    fcp->tport[loopid].port_wwn)
2344 				continue;
2345 			/*
2346 			 * We found this WWN elsewhere- it's changed
2347 			 * loopids then. We don't change it's actual
2348 			 * position in our cached port database- we
2349 			 * just change the actual loop ID we'd use.
2350 			 */
2351 			if (fcp->portdb[i].loopid != loopid) {
2352 				isp_prt(isp, ISP_LOGINFO, portshift, i,
2353 				    fcp->portdb[i].loopid,
2354 				    fcp->portdb[i].portid, loopid,
2355 				    fcp->tport[loopid].portid);
2356 			}
2357 			fcp->portdb[i].portid = fcp->tport[loopid].portid;
2358 			fcp->portdb[i].loopid = loopid;
2359 			fcp->portdb[i].valid = 1;
2360 			fcp->portdb[i].roles = fcp->tport[loopid].roles;
2361 
2362 			/*
2363 			 * Now make sure this Port WWN doesn't exist elsewhere
2364 			 * in the port database.
2365 			 */
2366 			for (j = i+1; j < hival; j++) {
2367 				if (fcp->portdb[i].port_wwn !=
2368 				    fcp->portdb[j].port_wwn) {
2369 					continue;
2370 				}
2371 				isp_prt(isp, ISP_LOGWARN, portdup, j, i);
2372 				/*
2373 				 * Invalidate the 'old' *and* 'new' ones.
2374 				 * This is really harsh and not quite right,
2375 				 * but if this happens, we really don't know
2376 				 * who is what at this point.
2377 				 */
2378 				fcp->portdb[i].valid = 0;
2379 				fcp->portdb[j].valid = 0;
2380 			}
2381 			break;
2382 		}
2383 
2384 		/*
2385 		 * If we didn't traverse the entire port database,
2386 		 * then we found (and remapped) an existing entry.
2387 		 * No need to notify anyone- go for the next one.
2388 		 */
2389 		if (i < hival) {
2390 			isp_prt(isp, ISP_LOGINFO, retained,
2391 			    fcp->portdb[i].loopid, i, fcp->portdb[i].portid);
2392 			continue;
2393 		}
2394 
2395 		/*
2396 		 * We've not found this Port WWN anywhere. It's a new entry.
2397 		 * See if we can leave it where it is (with target == loopid).
2398 		 */
2399 		if (fcp->portdb[loopid].port_wwn != 0) {
2400 			for (lim = 0; lim < hival; lim++) {
2401 				if (fcp->portdb[lim].port_wwn == 0)
2402 					break;
2403 			}
2404 			/* "Cannot Happen" */
2405 			if (lim == hival) {
2406 				isp_prt(isp, ISP_LOGWARN, "Remap Overflow");
2407 				continue;
2408 			}
2409 			i = lim;
2410 		} else {
2411 			i = loopid;
2412 		}
2413 
2414 		/*
2415 		 * NB:	The actual loopid we use here is loopid- we may
2416 		 *	in fact be at a completely different index (target).
2417 		 */
2418 		fcp->portdb[i].loopid = loopid;
2419 		fcp->portdb[i].port_wwn = fcp->tport[loopid].port_wwn;
2420 		fcp->portdb[i].node_wwn = fcp->tport[loopid].node_wwn;
2421 		fcp->portdb[i].roles = fcp->tport[loopid].roles;
2422 		fcp->portdb[i].portid = fcp->tport[loopid].portid;
2423 		fcp->portdb[i].valid = 1;
2424 
2425 		/*
2426 		 * Tell the outside world we've arrived.
2427 		 */
2428 		(void) isp_async(isp, ISPASYNC_PROMENADE, &i);
2429 	}
2430 
2431 	/*
2432 	 * Now find all previously used targets that are now invalid and
2433 	 * notify the outer layers that they're gone.
2434 	 */
2435 	for (lp = &fcp->portdb[0]; lp < &fcp->portdb[hival]; lp++) {
2436 		if (lp->valid || lp->port_wwn == 0) {
2437 			continue;
2438 		}
2439 
2440 		/*
2441 		 * Tell the outside world we've gone
2442 		 * away and erase our pdb entry.
2443 		 *
2444 		 */
2445 		loopid = lp - fcp->portdb;
2446 		(void) isp_async(isp, ISPASYNC_PROMENADE, &loopid);
2447 		MEMZERO((void *) lp, sizeof (*lp));
2448 	}
2449 	fcp->isp_loopstate = LOOP_LSCAN_DONE;
2450 	return (0);
2451 }
2452 
2453 
2454 static int
2455 isp_fabric_mbox_cmd(struct ispsoftc *isp, mbreg_t *mbp)
2456 {
2457 	isp_mboxcmd(isp, mbp, MBLOGNONE);
2458 	if (mbp->param[0] != MBOX_COMMAND_COMPLETE) {
2459 		if (FCPARAM(isp)->isp_loopstate == LOOP_SCANNING_FABRIC) {
2460 			FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
2461 		}
2462 		if (mbp->param[0] == MBOX_COMMAND_ERROR) {
2463 			char tbuf[16];
2464 			char *m;
2465 			switch (mbp->param[1]) {
2466 			case 1:
2467 				m = "No Loop";
2468 				break;
2469 			case 2:
2470 				m = "Failed to allocate IOCB buffer";
2471 				break;
2472 			case 3:
2473 				m = "Failed to allocate XCB buffer";
2474 				break;
2475 			case 4:
2476 				m = "timeout or transmit failed";
2477 				break;
2478 			case 5:
2479 				m = "no fabric loop";
2480 				break;
2481 			case 6:
2482 				m = "remote device not a target";
2483 				break;
2484 			default:
2485 				SNPRINTF(tbuf, sizeof tbuf, "%x",
2486 				    mbp->param[1]);
2487 				m = tbuf;
2488 				break;
2489 			}
2490 			isp_prt(isp, ISP_LOGERR, "SNS Failed- %s", m);
2491 		}
2492 		return (-1);
2493 	}
2494 
2495 	if (FCPARAM(isp)->isp_fwstate != FW_READY ||
2496 	    FCPARAM(isp)->isp_loopstate < LOOP_SCANNING_FABRIC) {
2497 		return (-1);
2498 	}
2499 	return(0);
2500 }
2501 
2502 #ifdef	ISP_USE_GA_NXT
2503 static int
2504 isp_scan_fabric(struct ispsoftc *isp, int ftype)
2505 {
2506 	fcparam *fcp = isp->isp_param;
2507 	u_int32_t portid, first_portid, last_portid;
2508 	int hicap, last_port_same;
2509 
2510 	if (fcp->isp_onfabric == 0) {
2511 		fcp->isp_loopstate = LOOP_FSCAN_DONE;
2512 		return (0);
2513 	}
2514 
2515 	FC_SCRATCH_ACQUIRE(isp);
2516 
2517 	/*
2518 	 * Since Port IDs are 24 bits, we can check against having seen
2519 	 * anything yet with this value.
2520 	 */
2521 	last_port_same = 0;
2522 	last_portid = 0xffffffff;	/* not a port */
2523 	first_portid = portid = fcp->isp_portid;
2524 	fcp->isp_loopstate = LOOP_SCANNING_FABRIC;
2525 
2526 	for (hicap = 0; hicap < GA_NXT_MAX; hicap++) {
2527 		mbreg_t mbs;
2528 		sns_screq_t *rq;
2529 		sns_ga_nxt_rsp_t *rs0, *rs1;
2530 		struct lportdb lcl;
2531 		u_int8_t sc[SNS_GA_NXT_RESP_SIZE];
2532 
2533 		rq = (sns_screq_t *)sc;
2534 		MEMZERO((void *) rq, SNS_GA_NXT_REQ_SIZE);
2535 		rq->snscb_rblen = SNS_GA_NXT_RESP_SIZE >> 1;
2536 		rq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+0x100);
2537 		rq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+0x100);
2538 		rq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+0x100);
2539 		rq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+0x100);
2540 		rq->snscb_sblen = 6;
2541 		rq->snscb_data[0] = SNS_GA_NXT;
2542 		rq->snscb_data[4] = portid & 0xffff;
2543 		rq->snscb_data[5] = (portid >> 16) & 0xff;
2544 		isp_put_sns_request(isp, rq, (sns_screq_t *) fcp->isp_scratch);
2545 		MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GA_NXT_REQ_SIZE);
2546 		MEMZERO(&mbs, sizeof (mbs));
2547 		mbs.param[0] = MBOX_SEND_SNS;
2548 		mbs.param[1] = SNS_GA_NXT_REQ_SIZE >> 1;
2549 		mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2550 		mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2551 		/*
2552 		 * Leave 4 and 5 alone
2553 		 */
2554 		mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2555 		mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2556 		if (isp_fabric_mbox_cmd(isp, &mbs)) {
2557 			if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
2558 				fcp->isp_loopstate = LOOP_PDB_RCVD;
2559 			}
2560 			FC_SCRATCH_RELEASE(isp);
2561 			return (-1);
2562 		}
2563 		MEMORYBARRIER(isp, SYNC_SFORCPU, 0x100, SNS_GA_NXT_RESP_SIZE);
2564 		rs1 = (sns_ga_nxt_rsp_t *) sc;
2565 		rs0 = (sns_ga_nxt_rsp_t *) ((u_int8_t *)fcp->isp_scratch+0x100);
2566 		isp_get_ga_nxt_response(isp, rs0, rs1);
2567 		if (rs1->snscb_cthdr.ct_response != FS_ACC) {
2568 			int level;
2569 			if (rs1->snscb_cthdr.ct_reason == 9 &&
2570 			    rs1->snscb_cthdr.ct_explanation == 7)
2571 				level = ISP_LOGDEBUG0;
2572 			else
2573 				level = ISP_LOGWARN;
2574 			isp_prt(isp, level, swrej, "GA_NXT",
2575 			    rs1->snscb_cthdr.ct_reason,
2576 			    rs1->snscb_cthdr.ct_explanation, portid);
2577 			FC_SCRATCH_RELEASE(isp);
2578 			fcp->isp_loopstate = LOOP_FSCAN_DONE;
2579 			return (0);
2580 		}
2581 		portid =
2582 		    (((u_int32_t) rs1->snscb_port_id[0]) << 16) |
2583 		    (((u_int32_t) rs1->snscb_port_id[1]) << 8) |
2584 		    (((u_int32_t) rs1->snscb_port_id[2]));
2585 
2586 		/*
2587 		 * XXX: We should check to make sure that this entry
2588 		 * XXX: supports the type(s) we are interested in.
2589 		 */
2590 		/*
2591 		 * Okay, we now have information about a fabric object.
2592 		 * If it is the type we're interested in, tell the outer layers
2593 		 * about it. The outer layer needs to  know: Port ID, WWNN,
2594 		 * WWPN, FC4 type, and port type.
2595 		 *
2596 		 * The lportdb structure is adequate for this.
2597 		 */
2598 		MEMZERO(&lcl, sizeof (lcl));
2599 		lcl.port_type = rs1->snscb_port_type;
2600 		lcl.fc4_type = ftype;
2601 		lcl.portid = portid;
2602 		lcl.node_wwn =
2603 		    (((u_int64_t)rs1->snscb_nodename[0]) << 56) |
2604 		    (((u_int64_t)rs1->snscb_nodename[1]) << 48) |
2605 		    (((u_int64_t)rs1->snscb_nodename[2]) << 40) |
2606 		    (((u_int64_t)rs1->snscb_nodename[3]) << 32) |
2607 		    (((u_int64_t)rs1->snscb_nodename[4]) << 24) |
2608 		    (((u_int64_t)rs1->snscb_nodename[5]) << 16) |
2609 		    (((u_int64_t)rs1->snscb_nodename[6]) <<  8) |
2610 		    (((u_int64_t)rs1->snscb_nodename[7]));
2611 		lcl.port_wwn =
2612 		    (((u_int64_t)rs1->snscb_portname[0]) << 56) |
2613 		    (((u_int64_t)rs1->snscb_portname[1]) << 48) |
2614 		    (((u_int64_t)rs1->snscb_portname[2]) << 40) |
2615 		    (((u_int64_t)rs1->snscb_portname[3]) << 32) |
2616 		    (((u_int64_t)rs1->snscb_portname[4]) << 24) |
2617 		    (((u_int64_t)rs1->snscb_portname[5]) << 16) |
2618 		    (((u_int64_t)rs1->snscb_portname[6]) <<  8) |
2619 		    (((u_int64_t)rs1->snscb_portname[7]));
2620 
2621 		/*
2622 		 * Does this fabric object support the type we want?
2623 		 * If not, skip it.
2624 		 */
2625 		if (rs1->snscb_fc4_types[ftype >> 5] & (1 << (ftype & 0x1f))) {
2626 			if (first_portid == portid) {
2627 				lcl.last_fabric_dev = 1;
2628 			} else {
2629 				lcl.last_fabric_dev = 0;
2630 			}
2631 			(void) isp_async(isp, ISPASYNC_FABRIC_DEV, &lcl);
2632 		} else {
2633 			isp_prt(isp, ISP_LOGDEBUG0,
2634 			    "PortID 0x%x doesn't support FC4 type 0x%x",
2635 			    portid, ftype);
2636 		}
2637 		if (first_portid == portid) {
2638 			fcp->isp_loopstate = LOOP_FSCAN_DONE;
2639 			FC_SCRATCH_RELEASE(isp);
2640 			return (0);
2641 		}
2642 		if (portid == last_portid) {
2643 			if (last_port_same++ > 20) {
2644 				isp_prt(isp, ISP_LOGWARN,
2645 				    "tangled fabric database detected");
2646 				break;
2647 			}
2648 		} else {
2649 			last_port_same = 0 ;
2650 			last_portid = portid;
2651 		}
2652 	}
2653 	FC_SCRATCH_RELEASE(isp);
2654 	if (hicap >= GA_NXT_MAX) {
2655 		isp_prt(isp, ISP_LOGWARN, "fabric too big (> %d)", GA_NXT_MAX);
2656 	}
2657 	fcp->isp_loopstate = LOOP_FSCAN_DONE;
2658 	return (0);
2659 }
2660 #else
2661 #define	GIDLEN	((ISP2100_SCRLEN >> 1) + 16)
2662 #define	NGENT	((GIDLEN - 16) >> 2)
2663 
2664 #define	IGPOFF	(ISP2100_SCRLEN - GIDLEN)
2665 #define	GXOFF	(256)
2666 
2667 static int
2668 isp_scan_fabric(struct ispsoftc *isp, int ftype)
2669 {
2670 	fcparam *fcp = FCPARAM(isp);
2671 	mbreg_t mbs;
2672 	int i;
2673 	sns_gid_ft_req_t *rq;
2674 	sns_gid_ft_rsp_t *rs0, *rs1;
2675 
2676 	if (fcp->isp_onfabric == 0) {
2677 		fcp->isp_loopstate = LOOP_FSCAN_DONE;
2678 		return (0);
2679 	}
2680 
2681 	FC_SCRATCH_ACQUIRE(isp);
2682 	fcp->isp_loopstate = LOOP_SCANNING_FABRIC;
2683 
2684 	rq = (sns_gid_ft_req_t *)fcp->tport;
2685 	MEMZERO((void *) rq, SNS_GID_FT_REQ_SIZE);
2686 	rq->snscb_rblen = GIDLEN >> 1;
2687 	rq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+IGPOFF);
2688 	rq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+IGPOFF);
2689 	rq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+IGPOFF);
2690 	rq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+IGPOFF);
2691 	rq->snscb_sblen = 6;
2692 	rq->snscb_cmd = SNS_GID_FT;
2693 	rq->snscb_mword_div_2 = NGENT;
2694 	rq->snscb_fc4_type = ftype;
2695 	isp_put_gid_ft_request(isp, rq, (sns_gid_ft_req_t *) fcp->isp_scratch);
2696 	MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GID_FT_REQ_SIZE);
2697 	MEMZERO(&mbs, sizeof (mbs));
2698 	mbs.param[0] = MBOX_SEND_SNS;
2699 	mbs.param[1] = SNS_GID_FT_REQ_SIZE >> 1;
2700 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2701 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2702 
2703 	/*
2704 	 * Leave 4 and 5 alone
2705 	 */
2706 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2707 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2708 	if (isp_fabric_mbox_cmd(isp, &mbs)) {
2709 		if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
2710 			fcp->isp_loopstate = LOOP_PDB_RCVD;
2711 		}
2712 		FC_SCRATCH_RELEASE(isp);
2713 		return (-1);
2714 	}
2715 	if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2716 		FC_SCRATCH_RELEASE(isp);
2717 		return (-1);
2718 	}
2719 	MEMORYBARRIER(isp, SYNC_SFORCPU, IGPOFF, GIDLEN);
2720 	rs1 = (sns_gid_ft_rsp_t *) fcp->tport;
2721 	rs0 = (sns_gid_ft_rsp_t *) ((u_int8_t *)fcp->isp_scratch+IGPOFF);
2722 	isp_get_gid_ft_response(isp, rs0, rs1, NGENT);
2723 	if (rs1->snscb_cthdr.ct_response != FS_ACC) {
2724 		int level;
2725 		if (rs1->snscb_cthdr.ct_reason == 9 &&
2726 		    rs1->snscb_cthdr.ct_explanation == 7)
2727 			level = ISP_LOGDEBUG0;
2728 		else
2729 			level = ISP_LOGWARN;
2730 		isp_prt(isp, level, swrej, "GID_FT",
2731 		    rs1->snscb_cthdr.ct_reason,
2732 		    rs1->snscb_cthdr.ct_explanation, 0);
2733 		FC_SCRATCH_RELEASE(isp);
2734 		fcp->isp_loopstate = LOOP_FSCAN_DONE;
2735 		return (0);
2736 	}
2737 
2738 	/*
2739 	 * Okay, we now have a list of Port IDs for this class of device.
2740 	 * Go through the list and for each one get the WWPN/WWNN for it
2741 	 * and tell the outer layers about it. The outer layer needs to
2742 	 * know: Port ID, WWNN, WWPN, FC4 type, and (possibly) port type.
2743 	 *
2744 	 * The lportdb structure is adequate for this.
2745 	 */
2746 	i = -1;
2747 	do {
2748 		sns_gxn_id_req_t grqbuf, *gq = &grqbuf;
2749 		sns_gxn_id_rsp_t *gs0, grsbuf, *gs1 = &grsbuf;
2750 		struct lportdb lcl;
2751 #if	0
2752 		sns_gff_id_rsp_t *fs0, ffsbuf, *fs1 = &ffsbuf;
2753 #endif
2754 
2755 		i++;
2756 		MEMZERO(&lcl, sizeof (lcl));
2757 		lcl.fc4_type = ftype;
2758 		lcl.portid =
2759 		    (((u_int32_t) rs1->snscb_ports[i].portid[0]) << 16) |
2760 		    (((u_int32_t) rs1->snscb_ports[i].portid[1]) << 8) |
2761 		    (((u_int32_t) rs1->snscb_ports[i].portid[2]));
2762 
2763 		MEMZERO((void *) gq, sizeof (sns_gxn_id_req_t));
2764 		gq->snscb_rblen = SNS_GXN_ID_RESP_SIZE >> 1;
2765 		gq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+GXOFF);
2766 		gq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+GXOFF);
2767 		gq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+GXOFF);
2768 		gq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+GXOFF);
2769 		gq->snscb_sblen = 6;
2770 		gq->snscb_cmd = SNS_GPN_ID;
2771 		gq->snscb_portid = lcl.portid;
2772 		isp_put_gxn_id_request(isp, gq,
2773 		    (sns_gxn_id_req_t *) fcp->isp_scratch);
2774 		MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GXN_ID_REQ_SIZE);
2775 		MEMZERO(&mbs, sizeof (mbs));
2776 		mbs.param[0] = MBOX_SEND_SNS;
2777 		mbs.param[1] = SNS_GXN_ID_REQ_SIZE >> 1;
2778 		mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2779 		mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2780 		/*
2781 		 * Leave 4 and 5 alone
2782 		 */
2783 		mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2784 		mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2785 		if (isp_fabric_mbox_cmd(isp, &mbs)) {
2786 			if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
2787 				fcp->isp_loopstate = LOOP_PDB_RCVD;
2788 			}
2789 			FC_SCRATCH_RELEASE(isp);
2790 			return (-1);
2791 		}
2792 		if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2793 			FC_SCRATCH_RELEASE(isp);
2794 			return (-1);
2795 		}
2796 		MEMORYBARRIER(isp, SYNC_SFORCPU, GXOFF, SNS_GXN_ID_RESP_SIZE);
2797 		gs0 = (sns_gxn_id_rsp_t *) ((u_int8_t *)fcp->isp_scratch+GXOFF);
2798 		isp_get_gxn_id_response(isp, gs0, gs1);
2799 		if (gs1->snscb_cthdr.ct_response != FS_ACC) {
2800 			isp_prt(isp, ISP_LOGWARN, swrej, "GPN_ID",
2801 			    gs1->snscb_cthdr.ct_reason,
2802 			    gs1->snscb_cthdr.ct_explanation, lcl.portid);
2803 			if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2804 				FC_SCRATCH_RELEASE(isp);
2805 				return (-1);
2806 			}
2807 			continue;
2808 		}
2809 		lcl.port_wwn =
2810 		    (((u_int64_t)gs1->snscb_wwn[0]) << 56) |
2811 		    (((u_int64_t)gs1->snscb_wwn[1]) << 48) |
2812 		    (((u_int64_t)gs1->snscb_wwn[2]) << 40) |
2813 		    (((u_int64_t)gs1->snscb_wwn[3]) << 32) |
2814 		    (((u_int64_t)gs1->snscb_wwn[4]) << 24) |
2815 		    (((u_int64_t)gs1->snscb_wwn[5]) << 16) |
2816 		    (((u_int64_t)gs1->snscb_wwn[6]) <<  8) |
2817 		    (((u_int64_t)gs1->snscb_wwn[7]));
2818 
2819 		MEMZERO((void *) gq, sizeof (sns_gxn_id_req_t));
2820 		gq->snscb_rblen = SNS_GXN_ID_RESP_SIZE >> 1;
2821 		gq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+GXOFF);
2822 		gq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+GXOFF);
2823 		gq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+GXOFF);
2824 		gq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+GXOFF);
2825 		gq->snscb_sblen = 6;
2826 		gq->snscb_cmd = SNS_GNN_ID;
2827 		gq->snscb_portid = lcl.portid;
2828 		isp_put_gxn_id_request(isp, gq,
2829 		    (sns_gxn_id_req_t *) fcp->isp_scratch);
2830 		MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GXN_ID_REQ_SIZE);
2831 		MEMZERO(&mbs, sizeof (mbs));
2832 		mbs.param[0] = MBOX_SEND_SNS;
2833 		mbs.param[1] = SNS_GXN_ID_REQ_SIZE >> 1;
2834 		mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2835 		mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2836 		/*
2837 		 * Leave 4 and 5 alone
2838 		 */
2839 		mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2840 		mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2841 		if (isp_fabric_mbox_cmd(isp, &mbs)) {
2842 			if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
2843 				fcp->isp_loopstate = LOOP_PDB_RCVD;
2844 			}
2845 			FC_SCRATCH_RELEASE(isp);
2846 			return (-1);
2847 		}
2848 		if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2849 			FC_SCRATCH_RELEASE(isp);
2850 			return (-1);
2851 		}
2852 		MEMORYBARRIER(isp, SYNC_SFORCPU, GXOFF, SNS_GXN_ID_RESP_SIZE);
2853 		gs0 = (sns_gxn_id_rsp_t *) ((u_int8_t *)fcp->isp_scratch+GXOFF);
2854 		isp_get_gxn_id_response(isp, gs0, gs1);
2855 		if (gs1->snscb_cthdr.ct_response != FS_ACC) {
2856 			isp_prt(isp, ISP_LOGWARN, swrej, "GNN_ID",
2857 			    gs1->snscb_cthdr.ct_reason,
2858 			    gs1->snscb_cthdr.ct_explanation, lcl.portid);
2859 			if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2860 				FC_SCRATCH_RELEASE(isp);
2861 				return (-1);
2862 			}
2863 			continue;
2864 		}
2865 		lcl.node_wwn =
2866 		    (((u_int64_t)gs1->snscb_wwn[0]) << 56) |
2867 		    (((u_int64_t)gs1->snscb_wwn[1]) << 48) |
2868 		    (((u_int64_t)gs1->snscb_wwn[2]) << 40) |
2869 		    (((u_int64_t)gs1->snscb_wwn[3]) << 32) |
2870 		    (((u_int64_t)gs1->snscb_wwn[4]) << 24) |
2871 		    (((u_int64_t)gs1->snscb_wwn[5]) << 16) |
2872 		    (((u_int64_t)gs1->snscb_wwn[6]) <<  8) |
2873 		    (((u_int64_t)gs1->snscb_wwn[7]));
2874 
2875 		/*
2876 		 * The QLogic f/w is bouncing this with a parameter error.
2877 		 */
2878 #if	0
2879 		/*
2880 		 * Try and get FC4 Features (FC-GS-3 only).
2881 		 * We can use the sns_gxn_id_req_t for this request.
2882 		 */
2883 		MEMZERO((void *) gq, sizeof (sns_gxn_id_req_t));
2884 		gq->snscb_rblen = SNS_GFF_ID_RESP_SIZE >> 1;
2885 		gq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+GXOFF);
2886 		gq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+GXOFF);
2887 		gq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+GXOFF);
2888 		gq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+GXOFF);
2889 		gq->snscb_sblen = 6;
2890 		gq->snscb_cmd = SNS_GFF_ID;
2891 		gq->snscb_portid = lcl.portid;
2892 		isp_put_gxn_id_request(isp, gq,
2893 		    (sns_gxn_id_req_t *) fcp->isp_scratch);
2894 		MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GXN_ID_REQ_SIZE);
2895 		MEMZERO(&mbs, sizeof (mbs));
2896 		mbs.param[0] = MBOX_SEND_SNS;
2897 		mbs.param[1] = SNS_GXN_ID_REQ_SIZE >> 1;
2898 		mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2899 		mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2900 		/*
2901 		 * Leave 4 and 5 alone
2902 		 */
2903 		mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2904 		mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2905 		if (isp_fabric_mbox_cmd(isp, &mbs)) {
2906 			if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
2907 				fcp->isp_loopstate = LOOP_PDB_RCVD;
2908 			}
2909 			FC_SCRATCH_RELEASE(isp);
2910 			return (-1);
2911 		}
2912 		if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2913 			FC_SCRATCH_RELEASE(isp);
2914 			return (-1);
2915 		}
2916 		MEMORYBARRIER(isp, SYNC_SFORCPU, GXOFF, SNS_GFF_ID_RESP_SIZE);
2917 		fs0 = (sns_gff_id_rsp_t *) ((u_int8_t *)fcp->isp_scratch+GXOFF);
2918 		isp_get_gff_id_response(isp, fs0, fs1);
2919 		if (fs1->snscb_cthdr.ct_response != FS_ACC) {
2920 			isp_prt(isp, /* ISP_LOGDEBUG0 */ ISP_LOGWARN,
2921 			    swrej, "GFF_ID",
2922 			    fs1->snscb_cthdr.ct_reason,
2923 			    fs1->snscb_cthdr.ct_explanation, lcl.portid);
2924 			if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2925 				FC_SCRATCH_RELEASE(isp);
2926 				return (-1);
2927 			}
2928 		} else {
2929 			int index = (ftype >> 3);
2930 			int bshft = (ftype & 0x7) * 4;
2931 			int fc4_fval =
2932 			    (fs1->snscb_fc4_features[index] >> bshft) & 0xf;
2933 			if (fc4_fval & 0x1) {
2934 				lcl.roles |=
2935 				    (SVC3_INI_ROLE >> SVC3_ROLE_SHIFT);
2936 			}
2937 			if (fc4_fval & 0x2) {
2938 				lcl.roles |=
2939 				    (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT);
2940 			}
2941 		}
2942 #endif
2943 
2944 		/*
2945 		 * If we really want to know what kind of port type this is,
2946 		 * we have to run another CT command. Otherwise, we'll leave
2947 		 * it as undefined.
2948 		 *
2949 		lcl.port_type = 0;
2950 		 */
2951 		if (rs1->snscb_ports[i].control & 0x80) {
2952 			lcl.last_fabric_dev = 1;
2953 		} else {
2954 			lcl.last_fabric_dev = 0;
2955 		}
2956 		(void) isp_async(isp, ISPASYNC_FABRIC_DEV, &lcl);
2957 
2958 	} while ((rs1->snscb_ports[i].control & 0x80) == 0 && i < NGENT-1);
2959 
2960 	/*
2961 	 * If we're not at the last entry, our list isn't big enough.
2962 	 */
2963 	if ((rs1->snscb_ports[i].control & 0x80) == 0) {
2964 		isp_prt(isp, ISP_LOGWARN, "fabric too big for scratch area");
2965 	}
2966 
2967 	FC_SCRATCH_RELEASE(isp);
2968 	fcp->isp_loopstate = LOOP_FSCAN_DONE;
2969 	return (0);
2970 }
2971 #endif
2972 
2973 static void
2974 isp_register_fc4_type(struct ispsoftc *isp)
2975 {
2976 	fcparam *fcp = isp->isp_param;
2977 	u_int8_t local[SNS_RFT_ID_REQ_SIZE];
2978 	sns_screq_t *reqp = (sns_screq_t *) local;
2979 	mbreg_t mbs;
2980 
2981 	MEMZERO((void *) reqp, SNS_RFT_ID_REQ_SIZE);
2982 	reqp->snscb_rblen = SNS_RFT_ID_RESP_SIZE >> 1;
2983 	reqp->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma + 0x100);
2984 	reqp->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma + 0x100);
2985 	reqp->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma + 0x100);
2986 	reqp->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma + 0x100);
2987 	reqp->snscb_sblen = 22;
2988 	reqp->snscb_data[0] = SNS_RFT_ID;
2989 	reqp->snscb_data[4] = fcp->isp_portid & 0xffff;
2990 	reqp->snscb_data[5] = (fcp->isp_portid >> 16) & 0xff;
2991 	reqp->snscb_data[6] = (1 << FC4_SCSI);
2992 #if	0
2993 	reqp->snscb_data[6] |= (1 << FC4_IP);	/* ISO/IEC 8802-2 LLC/SNAP */
2994 #endif
2995 	FC_SCRATCH_ACQUIRE(isp);
2996 	isp_put_sns_request(isp, reqp, (sns_screq_t *) fcp->isp_scratch);
2997 	MEMZERO(&mbs, sizeof (mbs));
2998 	mbs.param[0] = MBOX_SEND_SNS;
2999 	mbs.param[1] = SNS_RFT_ID_REQ_SIZE >> 1;
3000 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
3001 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
3002 	/*
3003 	 * Leave 4 and 5 alone
3004 	 */
3005 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
3006 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
3007 	isp_mboxcmd(isp, &mbs, MBLOGALL);
3008 	FC_SCRATCH_RELEASE(isp);
3009 	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
3010 		isp_prt(isp, ISP_LOGDEBUG0, "Register FC4 types succeeded");
3011 	}
3012 }
3013 
3014 /*
3015  * Start a command. Locking is assumed done in the caller.
3016  */
3017 
3018 int
3019 isp_start(XS_T *xs)
3020 {
3021 	struct ispsoftc *isp;
3022 	u_int16_t nxti, optr, handle;
3023 	u_int8_t local[QENTRY_LEN];
3024 	ispreq_t *reqp, *qep;
3025 	int target, i;
3026 
3027 	XS_INITERR(xs);
3028 	isp = XS_ISP(xs);
3029 
3030 	/*
3031 	 * Check to make sure we're supporting initiator role.
3032 	 */
3033 	if ((isp->isp_role & ISP_ROLE_INITIATOR) == 0) {
3034 		XS_SETERR(xs, HBA_SELTIMEOUT);
3035 		return (CMD_COMPLETE);
3036 	}
3037 
3038 	/*
3039 	 * Now make sure we're running.
3040 	 */
3041 
3042 	if (isp->isp_state != ISP_RUNSTATE) {
3043 		isp_prt(isp, ISP_LOGERR, "Adapter not at RUNSTATE");
3044 		XS_SETERR(xs, HBA_BOTCH);
3045 		return (CMD_COMPLETE);
3046 	}
3047 
3048 	/*
3049 	 * Check command CDB length, etc.. We really are limited to 16 bytes
3050 	 * for Fibre Channel, but can do up to 44 bytes in parallel SCSI,
3051 	 * but probably only if we're running fairly new firmware (we'll
3052 	 * let the old f/w choke on an extended command queue entry).
3053 	 */
3054 
3055 	if (XS_CDBLEN(xs) > (IS_FC(isp)? 16 : 44) || XS_CDBLEN(xs) == 0) {
3056 		isp_prt(isp, ISP_LOGERR,
3057 		    "unsupported cdb length (%d, CDB[0]=0x%x)",
3058 		    XS_CDBLEN(xs), XS_CDBP(xs)[0] & 0xff);
3059 		XS_SETERR(xs, HBA_BOTCH);
3060 		return (CMD_COMPLETE);
3061 	}
3062 
3063 	/*
3064 	 * Check to see whether we have good firmware state still or
3065 	 * need to refresh our port database for this target.
3066 	 */
3067 	target = XS_TGT(xs);
3068 	if (IS_FC(isp)) {
3069 		fcparam *fcp = isp->isp_param;
3070 		struct lportdb *lp;
3071 #ifdef	HANDLE_LOOPSTATE_IN_OUTER_LAYERS
3072 		if (fcp->isp_fwstate != FW_READY ||
3073 		    fcp->isp_loopstate != LOOP_READY) {
3074 			return (CMD_RQLATER);
3075 		}
3076 
3077 		/*
3078 		 * If we're not on a Fabric, we can't have a target
3079 		 * above FL_PORT_ID-1.
3080 		 *
3081 		 * If we're on a fabric and *not* connected as an F-port,
3082 		 * we can't have a target less than FC_SNS_ID+1. This
3083 		 * keeps us from having to sort out the difference between
3084 		 * local public loop devices and those which we might get
3085 		 * from a switch's database.
3086 		 */
3087 		if (fcp->isp_onfabric == 0) {
3088 			if (target >= FL_PORT_ID) {
3089 				XS_SETERR(xs, HBA_SELTIMEOUT);
3090 				return (CMD_COMPLETE);
3091 			}
3092 		} else {
3093 			if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
3094 				XS_SETERR(xs, HBA_SELTIMEOUT);
3095 				return (CMD_COMPLETE);
3096 			}
3097 			/*
3098 			 * We used to exclude having local loop ports
3099 			 * at the same time that we have fabric ports.
3100 			 * That is, we used to exclude having ports
3101 			 * at < FL_PORT_ID if we're FL-port.
3102 			 *
3103 			 * That's wrong. The only thing that could be
3104 			 * dicey is if the switch you're connected to
3105 			 * has these local loop ports appear on the
3106 			 * fabric and we somehow attach them twice.
3107 			 */
3108 		}
3109 #else
3110 		/*
3111 		 * Check for f/w being in ready state. If the f/w
3112 		 * isn't in ready state, then we don't know our
3113 		 * loop ID and the f/w hasn't completed logging
3114 		 * into all targets on the loop. If this is the
3115 		 * case, then bounce the command. We pretend this is
3116 		 * a SELECTION TIMEOUT error if we've never gone to
3117 		 * FW_READY state at all- in this case we may not
3118 		 * be hooked to a loop at all and we shouldn't hang
3119 		 * the machine for this. Otherwise, defer this command
3120 		 * until later.
3121 		 */
3122 		if (fcp->isp_fwstate != FW_READY) {
3123 			/*
3124 			 * Give ourselves at most a 250ms delay.
3125 			 */
3126 			if (isp_fclink_test(isp, 250000)) {
3127 				XS_SETERR(xs, HBA_SELTIMEOUT);
3128 				if (fcp->loop_seen_once) {
3129 					return (CMD_RQLATER);
3130 				} else {
3131 					return (CMD_COMPLETE);
3132 				}
3133 			}
3134 		}
3135 
3136 		/*
3137 		 * If we're not on a Fabric, we can't have a target
3138 		 * above FL_PORT_ID-1.
3139 		 *
3140 		 * If we're on a fabric and *not* connected as an F-port,
3141 		 * we can't have a target less than FC_SNS_ID+1. This
3142 		 * keeps us from having to sort out the difference between
3143 		 * local public loop devices and those which we might get
3144 		 * from a switch's database.
3145 		 */
3146 		if (fcp->isp_onfabric == 0) {
3147 			if (target >= FL_PORT_ID) {
3148 				XS_SETERR(xs, HBA_SELTIMEOUT);
3149 				return (CMD_COMPLETE);
3150 			}
3151 		} else {
3152 			if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
3153 				XS_SETERR(xs, HBA_SELTIMEOUT);
3154 				return (CMD_COMPLETE);
3155 			}
3156 			if (fcp->isp_topo != TOPO_F_PORT &&
3157 			    target < FL_PORT_ID) {
3158 				XS_SETERR(xs, HBA_SELTIMEOUT);
3159 				return (CMD_COMPLETE);
3160 			}
3161 		}
3162 
3163 		/*
3164 		 * If our loop state is such that we haven't yet received
3165 		 * a "Port Database Changed" notification (after a LIP or
3166 		 * a Loop Reset or firmware initialization), then defer
3167 		 * sending commands for a little while, but only if we've
3168 		 * seen a valid loop at one point (otherwise we can get
3169 		 * stuck at initialization time).
3170 		 */
3171 		if (fcp->isp_loopstate < LOOP_PDB_RCVD) {
3172 			XS_SETERR(xs, HBA_SELTIMEOUT);
3173 			if (fcp->loop_seen_once) {
3174 				return (CMD_RQLATER);
3175 			} else {
3176 				return (CMD_COMPLETE);
3177 			}
3178 		}
3179 
3180 		/*
3181 		 * If we're in the middle of loop or fabric scanning
3182 		 * or merging the port databases, retry this command later.
3183 		 */
3184 		if (fcp->isp_loopstate == LOOP_SCANNING_FABRIC ||
3185 		    fcp->isp_loopstate == LOOP_SCANNING_LOOP ||
3186 		    fcp->isp_loopstate == LOOP_SYNCING_PDB) {
3187 			return (CMD_RQLATER);
3188 		}
3189 
3190 		/*
3191 		 * If our loop state is now such that we've just now
3192 		 * received a Port Database Change notification, then
3193 		 * we have to go off and (re)scan the fabric. We back
3194 		 * out and try again later if this doesn't work.
3195 		 */
3196 		if (fcp->isp_loopstate == LOOP_PDB_RCVD && fcp->isp_onfabric) {
3197 			if (isp_scan_fabric(isp, FC4_SCSI)) {
3198 				return (CMD_RQLATER);
3199 			}
3200 			if (fcp->isp_fwstate != FW_READY ||
3201 			    fcp->isp_loopstate < LOOP_FSCAN_DONE) {
3202 				return (CMD_RQLATER);
3203 			}
3204 		}
3205 
3206 		/*
3207 		 * If our loop state is now such that we've just now
3208 		 * received a Port Database Change notification, then
3209 		 * we have to go off and (re)synchronize our port
3210 		 * database.
3211 		 */
3212 		if (fcp->isp_loopstate < LOOP_READY) {
3213 			if (isp_pdb_sync(isp)) {
3214 				return (CMD_RQLATER);
3215 			}
3216 			if (fcp->isp_fwstate != FW_READY ||
3217 			    fcp->isp_loopstate != LOOP_READY) {
3218 				return (CMD_RQLATER);
3219 			}
3220 		}
3221 
3222 		/*
3223 		 * XXX: Here's were we would cancel any loop_dead flag
3224 		 * XXX: also cancel in dead_loop timeout that's running
3225 		 */
3226 #endif
3227 
3228 		/*
3229 		 * Now check whether we should even think about pursuing this.
3230 		 */
3231 		lp = &fcp->portdb[target];
3232 		if (lp->valid == 0) {
3233 			XS_SETERR(xs, HBA_SELTIMEOUT);
3234 			return (CMD_COMPLETE);
3235 		}
3236 		if ((lp->roles & (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT)) == 0) {
3237 			isp_prt(isp, ISP_LOGDEBUG2,
3238 			    "Target %d does not have target service", target);
3239 			XS_SETERR(xs, HBA_SELTIMEOUT);
3240 			return (CMD_COMPLETE);
3241 		}
3242 		/*
3243 		 * Now turn target into what the actual Loop ID is.
3244 		 */
3245 		target = lp->loopid;
3246 	}
3247 
3248 	/*
3249 	 * Next check to see if any HBA or Device
3250 	 * parameters need to be updated.
3251 	 */
3252 	if (isp->isp_update != 0) {
3253 		isp_update(isp);
3254 	}
3255 
3256 	if (isp_getrqentry(isp, &nxti, &optr, (void *)&qep)) {
3257 		isp_prt(isp, ISP_LOGDEBUG0, "Request Queue Overflow");
3258 		XS_SETERR(xs, HBA_BOTCH);
3259 		return (CMD_EAGAIN);
3260 	}
3261 
3262 	/*
3263 	 * Now see if we need to synchronize the ISP with respect to anything.
3264 	 * We do dual duty here (cough) for synchronizing for busses other
3265 	 * than which we got here to send a command to.
3266 	 */
3267 	reqp = (ispreq_t *) local;
3268 	if (isp->isp_sendmarker) {
3269 		u_int8_t n = (IS_DUALBUS(isp)? 2: 1);
3270 		/*
3271 		 * Check ports to send markers for...
3272 		 */
3273 		for (i = 0; i < n; i++) {
3274 			if ((isp->isp_sendmarker & (1 << i)) == 0) {
3275 				continue;
3276 			}
3277 			MEMZERO((void *) reqp, QENTRY_LEN);
3278 			reqp->req_header.rqs_entry_count = 1;
3279 			reqp->req_header.rqs_entry_type = RQSTYPE_MARKER;
3280 			reqp->req_modifier = SYNC_ALL;
3281 			reqp->req_target = i << 7;	/* insert bus number */
3282 			isp_put_request(isp, reqp, qep);
3283 			ISP_ADD_REQUEST(isp, nxti);
3284 			isp->isp_sendmarker &= ~(1 << i);
3285 			if (isp_getrqentry(isp, &nxti, &optr, (void *) &qep)) {
3286 				isp_prt(isp, ISP_LOGDEBUG0,
3287 				    "Request Queue Overflow+");
3288 				XS_SETERR(xs, HBA_BOTCH);
3289 				return (CMD_EAGAIN);
3290 			}
3291 		}
3292 	}
3293 
3294 	MEMZERO((void *)reqp, QENTRY_LEN);
3295 	reqp->req_header.rqs_entry_count = 1;
3296 	if (IS_FC(isp)) {
3297 		reqp->req_header.rqs_entry_type = RQSTYPE_T2RQS;
3298 	} else {
3299 		if (XS_CDBLEN(xs) > 12)
3300 			reqp->req_header.rqs_entry_type = RQSTYPE_CMDONLY;
3301 		else
3302 			reqp->req_header.rqs_entry_type = RQSTYPE_REQUEST;
3303 	}
3304 	/* reqp->req_header.rqs_flags = 0; */
3305 	/* reqp->req_header.rqs_seqno = 0; */
3306 	if (IS_FC(isp)) {
3307 		/*
3308 		 * See comment in isp_intr
3309 		 */
3310 		/* XS_RESID(xs) = 0; */
3311 
3312 		/*
3313 		 * Fibre Channel always requires some kind of tag.
3314 		 * The Qlogic drivers seem be happy not to use a tag,
3315 		 * but this breaks for some devices (IBM drives).
3316 		 */
3317 		if (XS_TAG_P(xs)) {
3318 			((ispreqt2_t *)reqp)->req_flags = XS_TAG_TYPE(xs);
3319 		} else {
3320 			/*
3321 			 * If we don't know what tag to use, use HEAD OF QUEUE
3322 			 * for Request Sense or Simple.
3323 			 */
3324 			if (XS_CDBP(xs)[0] == 0x3)	/* REQUEST SENSE */
3325 				((ispreqt2_t *)reqp)->req_flags = REQFLAG_HTAG;
3326 			else
3327 				((ispreqt2_t *)reqp)->req_flags = REQFLAG_STAG;
3328 		}
3329 	} else {
3330 		sdparam *sdp = (sdparam *)isp->isp_param;
3331 		sdp += XS_CHANNEL(xs);
3332 		if ((sdp->isp_devparam[target].actv_flags & DPARM_TQING) &&
3333 		    XS_TAG_P(xs)) {
3334 			reqp->req_flags = XS_TAG_TYPE(xs);
3335 		}
3336 	}
3337 	if (IS_SCSI(isp)) {
3338 		reqp->req_target = target | (XS_CHANNEL(xs) << 7);
3339 		reqp->req_lun_trn = XS_LUN(xs);
3340 		reqp->req_cdblen = XS_CDBLEN(xs);
3341 	} else if (IS_2KLOGIN(isp)) {
3342 		((ispreqt2e_t *)reqp)->req_target = target;
3343 		((ispreqt2e_t *)reqp)->req_scclun = XS_LUN(xs);
3344 	} else if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) {
3345 		((ispreqt2_t *)reqp)->req_target = target;
3346 		((ispreqt2_t *)reqp)->req_scclun = XS_LUN(xs);
3347 	} else {
3348 		((ispreqt2_t *)reqp)->req_target = target;
3349 		((ispreqt2_t *)reqp)->req_lun_trn = XS_LUN(xs);
3350 	}
3351 	MEMCPY(reqp->req_cdb, XS_CDBP(xs), XS_CDBLEN(xs));
3352 
3353 	reqp->req_time = XS_TIME(xs) / 1000;
3354 	if (reqp->req_time == 0 && XS_TIME(xs)) {
3355 		reqp->req_time = 1;
3356 	}
3357 
3358 	if (isp_save_xs(isp, xs, &handle)) {
3359 		isp_prt(isp, ISP_LOGDEBUG0, "out of xflist pointers");
3360 		XS_SETERR(xs, HBA_BOTCH);
3361 		return (CMD_EAGAIN);
3362 	}
3363 	reqp->req_handle = handle;
3364 
3365 	/*
3366 	 * Set up DMA and/or do any bus swizzling of the request entry
3367 	 * so that the Qlogic F/W understands what is being asked of it.
3368 	 */
3369 	i = ISP_DMASETUP(isp, xs, reqp, &nxti, optr);
3370 	if (i != CMD_QUEUED) {
3371 		isp_destroy_handle(isp, handle);
3372 		/*
3373 		 * dmasetup sets actual error in packet, and
3374 		 * return what we were given to return.
3375 		 */
3376 		return (i);
3377 	}
3378 	XS_SETERR(xs, HBA_NOERROR);
3379 	isp_prt(isp, ISP_LOGDEBUG2,
3380 	    "START cmd for %d.%d.%d cmd 0x%x datalen %ld",
3381 	    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), XS_CDBP(xs)[0],
3382 	    (long) XS_XFRLEN(xs));
3383 	ISP_ADD_REQUEST(isp, nxti);
3384 	isp->isp_nactive++;
3385 	return (CMD_QUEUED);
3386 }
3387 
3388 /*
3389  * isp control
3390  * Locks (ints blocked) assumed held.
3391  */
3392 
3393 int
3394 isp_control(struct ispsoftc *isp, ispctl_t ctl, void *arg)
3395 {
3396 	XS_T *xs;
3397 	mbreg_t mbs;
3398 	int bus, tgt;
3399 	u_int16_t handle;
3400 
3401 	MEMZERO(&mbs, sizeof (mbs));
3402 
3403 	switch (ctl) {
3404 	default:
3405 		isp_prt(isp, ISP_LOGERR, "Unknown Control Opcode 0x%x", ctl);
3406 		break;
3407 
3408 	case ISPCTL_RESET_BUS:
3409 		/*
3410 		 * Issue a bus reset.
3411 		 */
3412 		mbs.param[0] = MBOX_BUS_RESET;
3413 		mbs.param[2] = 0;
3414 		if (IS_SCSI(isp)) {
3415 			mbs.param[1] =
3416 			    ((sdparam *) isp->isp_param)->isp_bus_reset_delay;
3417 			if (mbs.param[1] < 2)
3418 				mbs.param[1] = 2;
3419 			bus = *((int *) arg);
3420 			if (IS_DUALBUS(isp))
3421 				mbs.param[2] = bus;
3422 		} else {
3423 			mbs.param[1] = 10;
3424 			bus = 0;
3425 		}
3426 		isp->isp_sendmarker |= (1 << bus);
3427 		isp_mboxcmd(isp, &mbs, MBLOGALL);
3428 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3429 			break;
3430 		}
3431 		isp_prt(isp, ISP_LOGINFO,
3432 		    "driver initiated bus reset of bus %d", bus);
3433 		return (0);
3434 
3435 	case ISPCTL_RESET_DEV:
3436 		tgt = (*((int *) arg)) & 0xffff;
3437 		bus = (*((int *) arg)) >> 16;
3438 		mbs.param[0] = MBOX_ABORT_TARGET;
3439 		if (IS_SCSI(isp)) {
3440 			mbs.param[1] = (tgt << 8) | (bus << 15);
3441 		} else {
3442 			if (IS_2KLOGIN(isp)) {
3443 				mbs.param[1] = tgt;
3444 				mbs.obits |= (1 << 10);
3445 			} else {
3446 				mbs.param[1] = (tgt << 8);
3447 			}
3448 		}
3449 		mbs.param[2] = 3;	/* 'delay', in seconds */
3450 		isp_mboxcmd(isp, &mbs, MBLOGALL);
3451 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3452 			break;
3453 		}
3454 		isp_prt(isp, ISP_LOGINFO,
3455 		    "Target %d on Bus %d Reset Succeeded", tgt, bus);
3456 		isp->isp_sendmarker |= (1 << bus);
3457 		return (0);
3458 
3459 	case ISPCTL_ABORT_CMD:
3460 		xs = (XS_T *) arg;
3461 		tgt = XS_TGT(xs);
3462 		handle = isp_find_handle(isp, xs);
3463 		if (handle == 0) {
3464 			isp_prt(isp, ISP_LOGWARN,
3465 			    "cannot find handle for command to abort");
3466 			break;
3467 		}
3468 		bus = XS_CHANNEL(xs);
3469 		mbs.param[0] = MBOX_ABORT;
3470 		if (IS_FC(isp)) {
3471 			if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN)  {
3472 				if (IS_2KLOGIN(isp)) {
3473 					mbs.param[1] = tgt;
3474 				} else {
3475 					mbs.param[1] = tgt << 8;
3476 				}
3477 				mbs.param[4] = 0;
3478 				mbs.param[5] = 0;
3479 				mbs.param[6] = XS_LUN(xs);
3480 			} else {
3481 				mbs.param[1] = tgt << 8 | XS_LUN(xs);
3482 			}
3483 		} else {
3484 			mbs.param[1] =
3485 			    (bus << 15) | (XS_TGT(xs) << 8) | XS_LUN(xs);
3486 		}
3487 		mbs.param[3] = 0;
3488 		mbs.param[2] = handle;
3489 		isp_mboxcmd(isp, &mbs, MBLOGALL & ~MBOX_COMMAND_ERROR);
3490 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
3491 			return (0);
3492 		}
3493 		/*
3494 		 * XXX: Look for command in the REQUEST QUEUE. That is,
3495 		 * XXX: It hasen't been picked up by firmware yet.
3496 		 */
3497 		break;
3498 
3499 	case ISPCTL_UPDATE_PARAMS:
3500 
3501 		isp_update(isp);
3502 		return (0);
3503 
3504 	case ISPCTL_FCLINK_TEST:
3505 
3506 		if (IS_FC(isp)) {
3507 			int usdelay = (arg)? *((int *) arg) : 250000;
3508 			return (isp_fclink_test(isp, usdelay));
3509 		}
3510 		break;
3511 
3512 	case ISPCTL_SCAN_FABRIC:
3513 
3514 		if (IS_FC(isp)) {
3515 			int ftype = (arg)? *((int *) arg) : FC4_SCSI;
3516 			return (isp_scan_fabric(isp, ftype));
3517 		}
3518 		break;
3519 
3520 	case ISPCTL_SCAN_LOOP:
3521 
3522 		if (IS_FC(isp)) {
3523 			return (isp_scan_loop(isp));
3524 		}
3525 		break;
3526 
3527 	case ISPCTL_PDB_SYNC:
3528 
3529 		if (IS_FC(isp)) {
3530 			return (isp_pdb_sync(isp));
3531 		}
3532 		break;
3533 
3534 	case ISPCTL_SEND_LIP:
3535 
3536 		if (IS_FC(isp)) {
3537 			mbs.param[0] = MBOX_INIT_LIP;
3538 			isp_mboxcmd(isp, &mbs, MBLOGALL);
3539 			if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
3540 				return (0);
3541 			}
3542 		}
3543 		break;
3544 
3545 	case ISPCTL_GET_POSMAP:
3546 
3547 		if (IS_FC(isp) && arg) {
3548 			return (isp_getmap(isp, arg));
3549 		}
3550 		break;
3551 
3552 
3553 	case ISPCTL_GET_PDB:
3554 		if (IS_FC(isp) && arg) {
3555 			int id = *((int *)arg);
3556 			isp_pdb_t *pdb = arg;
3557 			return (isp_getpdb(isp, id, pdb));
3558 		}
3559 		break;
3560 
3561 	case ISPCTL_RUN_MBOXCMD:
3562 
3563 		isp_mboxcmd(isp, arg, MBLOGALL);
3564 		return(0);
3565 
3566 #ifdef	ISP_TARGET_MODE
3567 	case ISPCTL_TOGGLE_TMODE:
3568 	{
3569 
3570 		/*
3571 		 * We don't check/set against role here- that's the
3572 		 * responsibility for the outer layer to coordinate.
3573 		 */
3574 		if (IS_SCSI(isp)) {
3575 			int param = *(int *)arg;
3576 			mbs.param[0] = MBOX_ENABLE_TARGET_MODE;
3577 			mbs.param[1] = param & 0xffff;
3578 			mbs.param[2] = param >> 16;
3579 			isp_mboxcmd(isp, &mbs, MBLOGALL);
3580 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3581 				break;
3582 			}
3583 		}
3584 		return (0);
3585 	}
3586 #endif
3587 	}
3588 	return (-1);
3589 }
3590 
3591 /*
3592  * Interrupt Service Routine(s).
3593  *
3594  * External (OS) framework has done the appropriate locking,
3595  * and the locking will be held throughout this function.
3596  */
3597 
3598 /*
3599  * Limit our stack depth by sticking with the max likely number
3600  * of completions on a request queue at any one time.
3601  */
3602 #ifndef	MAX_REQUESTQ_COMPLETIONS
3603 #define	MAX_REQUESTQ_COMPLETIONS	64
3604 #endif
3605 
3606 void
3607 isp_intr(struct ispsoftc *isp, u_int16_t isr, u_int16_t sema, u_int16_t mbox)
3608 {
3609 	XS_T *complist[MAX_REQUESTQ_COMPLETIONS], *xs;
3610 	u_int16_t iptr, optr, junk;
3611 	int i, nlooked = 0, ndone = 0;
3612 
3613 again:
3614 	/*
3615 	 * Is this a mailbox related interrupt?
3616 	 * The mailbox semaphore will be nonzero if so.
3617 	 */
3618 	if (sema) {
3619 		if (mbox & 0x4000) {
3620 			isp->isp_intmboxc++;
3621 			if (isp->isp_mboxbsy) {
3622 				int i = 0, obits = isp->isp_obits;
3623 				isp->isp_mboxtmp[i++] = mbox;
3624 				for (i = 1; i < MAX_MAILBOX(isp); i++) {
3625 					if ((obits & (1 << i)) == 0) {
3626 						continue;
3627 					}
3628 					isp->isp_mboxtmp[i] =
3629 					    ISP_READ(isp, MBOX_OFF(i));
3630 				}
3631 				if (isp->isp_mbxwrk0) {
3632 					if (isp_mbox_continue(isp) == 0) {
3633 						return;
3634 					}
3635 				}
3636 				MBOX_NOTIFY_COMPLETE(isp);
3637 			} else {
3638 				isp_prt(isp, ISP_LOGWARN,
3639 				    "Mbox Command Async (0x%x) with no waiters",
3640 				    mbox);
3641 			}
3642 		} else if (isp_parse_async(isp, mbox) < 0) {
3643 			return;
3644 		}
3645 		if ((IS_FC(isp) && mbox != ASYNC_RIO_RESP) ||
3646 		    isp->isp_state != ISP_RUNSTATE) {
3647 			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3648 			ISP_WRITE(isp, BIU_SEMA, 0);
3649 			return;
3650 		}
3651 	}
3652 
3653 	/*
3654 	 * We can't be getting this now.
3655 	 */
3656 	if (isp->isp_state != ISP_RUNSTATE) {
3657 		isp_prt(isp, ISP_LOGWARN,
3658 		    "interrupt (ISR=%x SEMA=%x) when not ready", isr, sema);
3659 		/*
3660 		 * Thank you very much!  *Burrrp*!
3661 		 */
3662 		WRITE_RESPONSE_QUEUE_OUT_POINTER(isp,
3663 		    READ_RESPONSE_QUEUE_IN_POINTER(isp));
3664 
3665 		ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3666 		ISP_WRITE(isp, BIU_SEMA, 0);
3667 		return;
3668 	}
3669 
3670 	/*
3671 	 * Get the current Response Queue Out Pointer.
3672 	 *
3673 	 * If we're a 2300, we can ask what hardware what it thinks.
3674 	 */
3675 	if (IS_23XX(isp)) {
3676 		optr = ISP_READ(isp, isp->isp_respoutrp);
3677 		/*
3678 		 * Debug: to be taken out eventually
3679 		 */
3680 		if (isp->isp_residx != optr) {
3681 			isp_prt(isp, ISP_LOGWARN, "optr %x soft optr %x",
3682 			    optr, isp->isp_residx);
3683 		}
3684 	} else {
3685 		optr = isp->isp_residx;
3686 	}
3687 
3688 	/*
3689 	 * You *must* read the Response Queue In Pointer
3690 	 * prior to clearing the RISC interrupt.
3691 	 *
3692 	 * Debounce the 2300 if revision less than 2.
3693 	 */
3694 	if (IS_2100(isp) || (IS_2300(isp) && isp->isp_revision < 2)) {
3695 		i = 0;
3696 		do {
3697 			iptr = READ_RESPONSE_QUEUE_IN_POINTER(isp);
3698 			junk = READ_RESPONSE_QUEUE_IN_POINTER(isp);
3699 		} while (junk != iptr && ++i < 1000);
3700 
3701 		if (iptr != junk) {
3702 			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3703 			isp_prt(isp, ISP_LOGWARN,
3704 			    "Response Queue Out Pointer Unstable (%x, %x)",
3705 			    iptr, junk);
3706 			return;
3707 		}
3708 	} else {
3709 		iptr = READ_RESPONSE_QUEUE_IN_POINTER(isp);
3710 	}
3711 	isp->isp_resodx = iptr;
3712 
3713 
3714 	if (optr == iptr && sema == 0) {
3715 		/*
3716 		 * There are a lot of these- reasons unknown- mostly on
3717 		 * faster Alpha machines.
3718 		 *
3719 		 * I tried delaying after writing HCCR_CMD_CLEAR_RISC_INT to
3720 		 * make sure the old interrupt went away (to avoid 'ringing'
3721 		 * effects), but that didn't stop this from occurring.
3722 		 */
3723 		if (IS_23XX(isp)) {
3724 			USEC_DELAY(100);
3725 			iptr = READ_RESPONSE_QUEUE_IN_POINTER(isp);
3726 			junk = ISP_READ(isp, BIU_R2HSTSLO);
3727 		} else {
3728 			junk = ISP_READ(isp, BIU_ISR);
3729 		}
3730 		if (optr == iptr) {
3731 			if (IS_23XX(isp)) {
3732 				;
3733 			} else {
3734 				sema = ISP_READ(isp, BIU_SEMA);
3735 				mbox = ISP_READ(isp, OUTMAILBOX0);
3736 				if ((sema & 0x3) && (mbox & 0x8000)) {
3737 					goto again;
3738 				}
3739 			}
3740 			isp->isp_intbogus++;
3741 			isp_prt(isp, ISP_LOGDEBUG1,
3742 			    "bogus intr- isr %x (%x) iptr %x optr %x",
3743 			    isr, junk, iptr, optr);
3744 		}
3745 	}
3746 	isp->isp_resodx = iptr;
3747 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3748 	ISP_WRITE(isp, BIU_SEMA, 0);
3749 
3750 	if (isp->isp_rspbsy) {
3751 		return;
3752 	}
3753 	isp->isp_rspbsy = 1;
3754 
3755 	while (optr != iptr) {
3756 		ispstatusreq_t local, *sp = &local;
3757 		isphdr_t *hp;
3758 		int type;
3759 		u_int16_t oop;
3760 		int buddaboom = 0;
3761 
3762 		hp = (isphdr_t *) ISP_QUEUE_ENTRY(isp->isp_result, optr);
3763 		oop = optr;
3764 		optr = ISP_NXT_QENTRY(optr, RESULT_QUEUE_LEN(isp));
3765 		nlooked++;
3766  read_again:
3767 		/*
3768 		 * Synchronize our view of this response queue entry.
3769 		 */
3770 		MEMORYBARRIER(isp, SYNC_RESULT, oop, QENTRY_LEN);
3771 
3772 		type = isp_get_response_type(isp, hp);
3773 
3774 		if (type == RQSTYPE_RESPONSE) {
3775 			isp_get_response(isp, (ispstatusreq_t *) hp, sp);
3776 		} else if (type == RQSTYPE_RIO2) {
3777 			isp_rio2_t rio;
3778 			isp_get_rio2(isp, (isp_rio2_t *) hp, &rio);
3779 			for (i = 0; i < rio.req_header.rqs_seqno; i++) {
3780 				isp_fastpost_complete(isp, rio.req_handles[i]);
3781 			}
3782 			if (isp->isp_fpcchiwater < rio.req_header.rqs_seqno)
3783 				isp->isp_fpcchiwater = rio.req_header.rqs_seqno;
3784 			MEMZERO(hp, QENTRY_LEN);	/* PERF */
3785 			continue;
3786 		} else {
3787 			/*
3788 			 * Somebody reachable via isp_handle_other_response
3789 			 * may have updated the response queue pointers for
3790 			 * us, so we reload our goal index.
3791 			 */
3792 			int i = isp_handle_other_response(isp, type, hp, &optr);
3793 			if (i < 0) {
3794 				goto read_again;
3795 			} else if (i > 0) {
3796 				iptr = isp->isp_resodx;
3797 				MEMZERO(hp, QENTRY_LEN);	/* PERF */
3798 				continue;
3799 			}
3800 
3801 			/*
3802 			 * After this point, we'll just look at the header as
3803 			 * we don't know how to deal with the rest of the
3804 			 * response.
3805 			 */
3806 			isp_get_response(isp, (ispstatusreq_t *) hp, sp);
3807 
3808 			/*
3809 			 * It really has to be a bounced request just copied
3810 			 * from the request queue to the response queue. If
3811 			 * not, something bad has happened.
3812 			 */
3813 			if (sp->req_header.rqs_entry_type != RQSTYPE_REQUEST) {
3814 				isp_prt(isp, ISP_LOGERR, notresp,
3815 				    sp->req_header.rqs_entry_type, oop, optr,
3816 				    nlooked);
3817 				if (isp->isp_dblev & ISP_LOGDEBUG0) {
3818 					isp_print_bytes(isp, "Queue Entry",
3819 					    QENTRY_LEN, sp);
3820 				}
3821 				MEMZERO(hp, QENTRY_LEN);	/* PERF */
3822 				continue;
3823 			}
3824 			buddaboom = 1;
3825 		}
3826 
3827 		if (sp->req_header.rqs_flags & 0xf) {
3828 #define	_RQS_OFLAGS	\
3829 	~(RQSFLAG_CONTINUATION|RQSFLAG_FULL|RQSFLAG_BADHEADER|RQSFLAG_BADPACKET)
3830 			if (sp->req_header.rqs_flags & RQSFLAG_CONTINUATION) {
3831 				isp_prt(isp, ISP_LOGWARN,
3832 				    "continuation segment");
3833 				WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
3834 				continue;
3835 			}
3836 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
3837 				isp_prt(isp, ISP_LOGDEBUG1,
3838 				    "internal queues full");
3839 				/*
3840 				 * We'll synthesize a QUEUE FULL message below.
3841 				 */
3842 			}
3843 			if (sp->req_header.rqs_flags & RQSFLAG_BADHEADER) {
3844 				isp_prt(isp, ISP_LOGERR,  "bad header flag");
3845 				buddaboom++;
3846 			}
3847 			if (sp->req_header.rqs_flags & RQSFLAG_BADPACKET) {
3848 				isp_prt(isp, ISP_LOGERR, "bad request packet");
3849 				buddaboom++;
3850 			}
3851 			if (sp->req_header.rqs_flags & _RQS_OFLAGS) {
3852 				isp_prt(isp, ISP_LOGERR,
3853 				    "unknown flags (0x%x) in response",
3854 				    sp->req_header.rqs_flags);
3855 				buddaboom++;
3856 			}
3857 #undef	_RQS_OFLAGS
3858 		}
3859 		if (sp->req_handle > isp->isp_maxcmds || sp->req_handle < 1) {
3860 			MEMZERO(hp, QENTRY_LEN);	/* PERF */
3861 			isp_prt(isp, ISP_LOGERR,
3862 			    "bad request handle %d (type 0x%x, flags 0x%x)",
3863 			    sp->req_handle, sp->req_header.rqs_entry_type,
3864 			    sp->req_header.rqs_flags);
3865 			WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
3866 			continue;
3867 		}
3868 		xs = isp_find_xs(isp, sp->req_handle);
3869 		if (xs == NULL) {
3870 			u_int8_t ts = sp->req_completion_status & 0xff;
3871 			MEMZERO(hp, QENTRY_LEN);	/* PERF */
3872 			/*
3873 			 * Only whine if this isn't the expected fallout of
3874 			 * aborting the command.
3875 			 */
3876 			if (sp->req_header.rqs_entry_type != RQSTYPE_RESPONSE) {
3877 				isp_prt(isp, ISP_LOGERR,
3878 				    "cannot find handle 0x%x (type 0x%x)",
3879 				    sp->req_handle,
3880 				    sp->req_header.rqs_entry_type);
3881 			} else if (ts != RQCS_ABORTED) {
3882 				isp_prt(isp, ISP_LOGERR,
3883 				    "cannot find handle 0x%x (status 0x%x)",
3884 				    sp->req_handle, ts);
3885 			}
3886 			WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
3887 			continue;
3888 		}
3889 		isp_destroy_handle(isp, sp->req_handle);
3890 		if (sp->req_status_flags & RQSTF_BUS_RESET) {
3891 			XS_SETERR(xs, HBA_BUSRESET);
3892 			isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
3893 		}
3894 		if (buddaboom) {
3895 			XS_SETERR(xs, HBA_BOTCH);
3896 		}
3897 
3898 		if (IS_FC(isp) && (sp->req_scsi_status & RQCS_SV)) {
3899 			/*
3900 			 * Fibre Channel F/W doesn't say we got status
3901 			 * if there's Sense Data instead. I guess they
3902 			 * think it goes w/o saying.
3903 			 */
3904 			sp->req_state_flags |= RQSF_GOT_STATUS;
3905 		}
3906 		if (sp->req_state_flags & RQSF_GOT_STATUS) {
3907 			*XS_STSP(xs) = sp->req_scsi_status & 0xff;
3908 		}
3909 
3910 		switch (sp->req_header.rqs_entry_type) {
3911 		case RQSTYPE_RESPONSE:
3912 			XS_SET_STATE_STAT(isp, xs, sp);
3913 			isp_parse_status(isp, sp, xs);
3914 			if ((XS_NOERR(xs) || XS_ERR(xs) == HBA_NOERROR) &&
3915 			    (*XS_STSP(xs) == SCSI_BUSY)) {
3916 				XS_SETERR(xs, HBA_TGTBSY);
3917 			}
3918 			if (IS_SCSI(isp)) {
3919 				XS_RESID(xs) = sp->req_resid;
3920 				if ((sp->req_state_flags & RQSF_GOT_STATUS) &&
3921 				    (*XS_STSP(xs) == SCSI_CHECK) &&
3922 				    (sp->req_state_flags & RQSF_GOT_SENSE)) {
3923 					XS_SAVE_SENSE(xs, sp);
3924 				}
3925 				/*
3926 				 * A new synchronous rate was negotiated for
3927 				 * this target. Mark state such that we'll go
3928 				 * look up that which has changed later.
3929 				 */
3930 				if (sp->req_status_flags & RQSTF_NEGOTIATION) {
3931 					int t = XS_TGT(xs);
3932 					sdparam *sdp = isp->isp_param;
3933 					sdp += XS_CHANNEL(xs);
3934 					sdp->isp_devparam[t].dev_refresh = 1;
3935 					isp->isp_update |=
3936 					    (1 << XS_CHANNEL(xs));
3937 				}
3938 			} else {
3939 				if (sp->req_status_flags & RQSF_XFER_COMPLETE) {
3940 					XS_RESID(xs) = 0;
3941 				} else if (sp->req_scsi_status & RQCS_RESID) {
3942 					XS_RESID(xs) = sp->req_resid;
3943 				} else {
3944 					XS_RESID(xs) = 0;
3945 				}
3946 				if ((sp->req_state_flags & RQSF_GOT_STATUS) &&
3947 				    (*XS_STSP(xs) == SCSI_CHECK) &&
3948 				    (sp->req_scsi_status & RQCS_SV)) {
3949 					XS_SAVE_SENSE(xs, sp);
3950 					/* solely for the benefit of debug */
3951 					sp->req_state_flags |= RQSF_GOT_SENSE;
3952 				}
3953 			}
3954 			isp_prt(isp, ISP_LOGDEBUG2,
3955 			   "asked for %ld got resid %ld", (long) XS_XFRLEN(xs),
3956 			   (long) sp->req_resid);
3957 			break;
3958 		case RQSTYPE_REQUEST:
3959 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
3960 				/*
3961 				 * Force Queue Full status.
3962 				 */
3963 				*XS_STSP(xs) = SCSI_QFULL;
3964 				XS_SETERR(xs, HBA_NOERROR);
3965 			} else if (XS_NOERR(xs)) {
3966 				/*
3967 				 * ????
3968 				 */
3969 				isp_prt(isp, ISP_LOGDEBUG0,
3970 				    "Request Queue Entry bounced back");
3971 				XS_SETERR(xs, HBA_BOTCH);
3972 			}
3973 			XS_RESID(xs) = XS_XFRLEN(xs);
3974 			break;
3975 		default:
3976 			isp_prt(isp, ISP_LOGWARN,
3977 			    "unhandled response queue type 0x%x",
3978 			    sp->req_header.rqs_entry_type);
3979 			if (XS_NOERR(xs)) {
3980 				XS_SETERR(xs, HBA_BOTCH);
3981 			}
3982 			break;
3983 		}
3984 
3985 		/*
3986 		 * Free any DMA resources. As a side effect, this may
3987 		 * also do any cache flushing necessary for data coherence.			 */
3988 		if (XS_XFRLEN(xs)) {
3989 			ISP_DMAFREE(isp, xs, sp->req_handle);
3990 		}
3991 
3992 		if (((isp->isp_dblev & (ISP_LOGDEBUG2|ISP_LOGDEBUG3))) ||
3993 		    ((isp->isp_dblev & ISP_LOGDEBUG1) && ((!XS_NOERR(xs)) ||
3994 		    (*XS_STSP(xs) != SCSI_GOOD)))) {
3995 			char skey;
3996 			if (sp->req_state_flags & RQSF_GOT_SENSE) {
3997 				skey = XS_SNSKEY(xs) & 0xf;
3998 				if (skey < 10)
3999 					skey += '0';
4000 				else
4001 					skey += 'a' - 10;
4002 			} else if (*XS_STSP(xs) == SCSI_CHECK) {
4003 				skey = '?';
4004 			} else {
4005 				skey = '.';
4006 			}
4007 			isp_prt(isp, ISP_LOGALL, finmsg, XS_CHANNEL(xs),
4008 			    XS_TGT(xs), XS_LUN(xs), XS_XFRLEN(xs), XS_RESID(xs),
4009 			    *XS_STSP(xs), skey, XS_ERR(xs));
4010 		}
4011 
4012 		if (isp->isp_nactive > 0)
4013 		    isp->isp_nactive--;
4014 		complist[ndone++] = xs;	/* defer completion call until later */
4015 		MEMZERO(hp, QENTRY_LEN);	/* PERF */
4016 		if (ndone == MAX_REQUESTQ_COMPLETIONS) {
4017 			break;
4018 		}
4019 	}
4020 
4021 	/*
4022 	 * If we looked at any commands, then it's valid to find out
4023 	 * what the outpointer is. It also is a trigger to update the
4024 	 * ISP's notion of what we've seen so far.
4025 	 */
4026 	if (nlooked) {
4027 		WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
4028 		/*
4029 		 * While we're at it, read the requst queue out pointer.
4030 		 */
4031 		isp->isp_reqodx = READ_REQUEST_QUEUE_OUT_POINTER(isp);
4032 		if (isp->isp_rscchiwater < ndone)
4033 			isp->isp_rscchiwater = ndone;
4034 	}
4035 
4036 	isp->isp_residx = optr;
4037 	isp->isp_rspbsy = 0;
4038 	for (i = 0; i < ndone; i++) {
4039 		xs = complist[i];
4040 		if (xs) {
4041 			isp->isp_rsltccmplt++;
4042 			isp_done(xs);
4043 		}
4044 	}
4045 }
4046 
4047 /*
4048  * Support routines.
4049  */
4050 
4051 static int
4052 isp_parse_async(struct ispsoftc *isp, u_int16_t mbox)
4053 {
4054 	int rval = 0;
4055 	int bus;
4056 
4057 	if (IS_DUALBUS(isp)) {
4058 		bus = ISP_READ(isp, OUTMAILBOX6);
4059 	} else {
4060 		bus = 0;
4061 	}
4062 	isp_prt(isp, ISP_LOGDEBUG2, "Async Mbox 0x%x", mbox);
4063 
4064 	switch (mbox) {
4065 	case ASYNC_BUS_RESET:
4066 		isp->isp_sendmarker |= (1 << bus);
4067 #ifdef	ISP_TARGET_MODE
4068 		if (isp_target_async(isp, bus, mbox))
4069 			rval = -1;
4070 #endif
4071 		isp_async(isp, ISPASYNC_BUS_RESET, &bus);
4072 		break;
4073 	case ASYNC_SYSTEM_ERROR:
4074 #ifdef	ISP_FW_CRASH_DUMP
4075 		/*
4076 		 * If we have crash dumps enabled, it's up to the handler
4077 		 * for isp_async to reinit stuff and restart the firmware
4078 		 * after performing the crash dump. The reason we do things
4079 		 * this way is that we may need to activate a kernel thread
4080 		 * to do all the crash dump goop.
4081 		 */
4082 		isp_async(isp, ISPASYNC_FW_CRASH, NULL);
4083 #else
4084 		isp_async(isp, ISPASYNC_FW_CRASH, NULL);
4085 		isp_reinit(isp);
4086 		isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
4087 #endif
4088 		rval = -1;
4089 		break;
4090 
4091 	case ASYNC_RQS_XFER_ERR:
4092 		isp_prt(isp, ISP_LOGERR, "Request Queue Transfer Error");
4093 		break;
4094 
4095 	case ASYNC_RSP_XFER_ERR:
4096 		isp_prt(isp, ISP_LOGERR, "Response Queue Transfer Error");
4097 		break;
4098 
4099 	case ASYNC_QWAKEUP:
4100 		/*
4101 		 * We've just been notified that the Queue has woken up.
4102 		 * We don't need to be chatty about this- just unlatch things
4103 		 * and move on.
4104 		 */
4105 		mbox = READ_REQUEST_QUEUE_OUT_POINTER(isp);
4106 		break;
4107 
4108 	case ASYNC_TIMEOUT_RESET:
4109 		isp_prt(isp, ISP_LOGWARN,
4110 		    "timeout initiated SCSI bus reset of bus %d", bus);
4111 		isp->isp_sendmarker |= (1 << bus);
4112 #ifdef	ISP_TARGET_MODE
4113 		if (isp_target_async(isp, bus, mbox))
4114 			rval = -1;
4115 #endif
4116 		break;
4117 
4118 	case ASYNC_DEVICE_RESET:
4119 		isp_prt(isp, ISP_LOGINFO, "device reset on 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_EXTMSG_UNDERRUN:
4128 		isp_prt(isp, ISP_LOGWARN, "extended message underrun");
4129 		break;
4130 
4131 	case ASYNC_SCAM_INT:
4132 		isp_prt(isp, ISP_LOGINFO, "SCAM interrupt");
4133 		break;
4134 
4135 	case ASYNC_HUNG_SCSI:
4136 		isp_prt(isp, ISP_LOGERR,
4137 		    "stalled SCSI Bus after DATA Overrun");
4138 		/* XXX: Need to issue SCSI reset at this point */
4139 		break;
4140 
4141 	case ASYNC_KILLED_BUS:
4142 		isp_prt(isp, ISP_LOGERR, "SCSI Bus reset after DATA Overrun");
4143 		break;
4144 
4145 	case ASYNC_BUS_TRANSIT:
4146 		mbox = ISP_READ(isp, OUTMAILBOX2);
4147 		switch (mbox & 0x1c00) {
4148 		case SXP_PINS_LVD_MODE:
4149 			isp_prt(isp, ISP_LOGINFO, "Transition to LVD mode");
4150 			SDPARAM(isp)->isp_diffmode = 0;
4151 			SDPARAM(isp)->isp_ultramode = 0;
4152 			SDPARAM(isp)->isp_lvdmode = 1;
4153 			break;
4154 		case SXP_PINS_HVD_MODE:
4155 			isp_prt(isp, ISP_LOGINFO,
4156 			    "Transition to Differential mode");
4157 			SDPARAM(isp)->isp_diffmode = 1;
4158 			SDPARAM(isp)->isp_ultramode = 0;
4159 			SDPARAM(isp)->isp_lvdmode = 0;
4160 			break;
4161 		case SXP_PINS_SE_MODE:
4162 			isp_prt(isp, ISP_LOGINFO,
4163 			    "Transition to Single Ended mode");
4164 			SDPARAM(isp)->isp_diffmode = 0;
4165 			SDPARAM(isp)->isp_ultramode = 1;
4166 			SDPARAM(isp)->isp_lvdmode = 0;
4167 			break;
4168 		default:
4169 			isp_prt(isp, ISP_LOGWARN,
4170 			    "Transition to Unknown Mode 0x%x", mbox);
4171 			break;
4172 		}
4173 		/*
4174 		 * XXX: Set up to renegotiate again!
4175 		 */
4176 		/* Can only be for a 1080... */
4177 		isp->isp_sendmarker |= (1 << bus);
4178 		break;
4179 
4180 	/*
4181 	 * We can use bus, which will always be zero for FC cards,
4182 	 * as a mailbox pattern accumulator to be checked below.
4183 	 */
4184 	case ASYNC_RIO5:
4185 		bus = 0x1ce;	/* outgoing mailbox regs 1-3, 6-7 */
4186 		break;
4187 
4188 	case ASYNC_RIO4:
4189 		bus = 0x14e;	/* outgoing mailbox regs 1-3, 6 */
4190 		break;
4191 
4192 	case ASYNC_RIO3:
4193 		bus = 0x10e;	/* outgoing mailbox regs 1-3 */
4194 		break;
4195 
4196 	case ASYNC_RIO2:
4197 		bus = 0x106;	/* outgoing mailbox regs 1-2 */
4198 		break;
4199 
4200 	case ASYNC_RIO1:
4201 	case ASYNC_CMD_CMPLT:
4202 		bus = 0x102;	/* outgoing mailbox regs 1 */
4203 		break;
4204 
4205 	case ASYNC_RIO_RESP:
4206 		return (rval);
4207 
4208 	case ASYNC_CTIO_DONE:
4209 	{
4210 #ifdef	ISP_TARGET_MODE
4211 		int handle =
4212 		    (ISP_READ(isp, OUTMAILBOX2) << 16) |
4213 		    (ISP_READ(isp, OUTMAILBOX1));
4214 		if (isp_target_async(isp, handle, mbox)) {
4215 			rval = -1;
4216 		} else {
4217 			/* count it as a fast posting intr */
4218 			isp->isp_fphccmplt++;
4219 		}
4220 #else
4221 		isp_prt(isp, ISP_LOGINFO, "Fast Posting CTIO done");
4222 		isp->isp_fphccmplt++;	/* count it as a fast posting intr */
4223 #endif
4224 		break;
4225 	}
4226 	case ASYNC_LIP_F8:
4227 	case ASYNC_LIP_OCCURRED:
4228 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4229 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
4230 		isp->isp_sendmarker = 1;
4231 		isp_mark_getpdb_all(isp);
4232 		isp_async(isp, ISPASYNC_LIP, NULL);
4233 #ifdef	ISP_TARGET_MODE
4234 		if (isp_target_async(isp, bus, mbox))
4235 			rval = -1;
4236 #endif
4237 		/*
4238 		 * We've had problems with data corruption occuring on
4239 		 * commands that complete (with no apparent error) after
4240 		 * we receive a LIP. This has been observed mostly on
4241 		 * Local Loop topologies. To be safe, let's just mark
4242 		 * all active commands as dead.
4243 		 */
4244 		if (FCPARAM(isp)->isp_topo == TOPO_NL_PORT ||
4245 		    FCPARAM(isp)->isp_topo == TOPO_FL_PORT) {
4246 			int i, j;
4247 			for (i = j = 0; i < isp->isp_maxcmds; i++) {
4248 				XS_T *xs;
4249 				xs = isp->isp_xflist[i];
4250 				if (xs != NULL) {
4251 					j++;
4252 					XS_SETERR(xs, HBA_BUSRESET);
4253 				}
4254 			}
4255 			if (j) {
4256 				isp_prt(isp, ISP_LOGERR,
4257 				    "LIP destroyed %d active commands", j);
4258 			}
4259 		}
4260 		break;
4261 
4262 	case ASYNC_LOOP_UP:
4263 		isp->isp_sendmarker = 1;
4264 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4265 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
4266 		isp_mark_getpdb_all(isp);
4267 		isp_async(isp, ISPASYNC_LOOP_UP, NULL);
4268 #ifdef	ISP_TARGET_MODE
4269 		if (isp_target_async(isp, bus, mbox))
4270 			rval = -1;
4271 #endif
4272 		break;
4273 
4274 	case ASYNC_LOOP_DOWN:
4275 		isp->isp_sendmarker = 1;
4276 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4277 		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
4278 		isp_mark_getpdb_all(isp);
4279 		isp_async(isp, ISPASYNC_LOOP_DOWN, NULL);
4280 #ifdef	ISP_TARGET_MODE
4281 		if (isp_target_async(isp, bus, mbox))
4282 			rval = -1;
4283 #endif
4284 		break;
4285 
4286 	case ASYNC_LOOP_RESET:
4287 		isp->isp_sendmarker = 1;
4288 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4289 		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
4290 		isp_mark_getpdb_all(isp);
4291 		isp_async(isp, ISPASYNC_LOOP_RESET, NULL);
4292 #ifdef	ISP_TARGET_MODE
4293 		if (isp_target_async(isp, bus, mbox))
4294 			rval = -1;
4295 #endif
4296 		break;
4297 
4298 	case ASYNC_PDB_CHANGED:
4299 		isp->isp_sendmarker = 1;
4300 		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
4301 		isp_mark_getpdb_all(isp);
4302 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_PDB);
4303 		break;
4304 
4305 	case ASYNC_CHANGE_NOTIFY:
4306 		/*
4307 		 * Not correct, but it will force us to rescan the loop.
4308 		 */
4309 		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
4310 		isp_mark_getpdb_all(isp);
4311 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_SNS);
4312 		break;
4313 
4314 	case ASYNC_PTPMODE:
4315 		if (FCPARAM(isp)->isp_onfabric)
4316 			FCPARAM(isp)->isp_topo = TOPO_F_PORT;
4317 		else
4318 			FCPARAM(isp)->isp_topo = TOPO_N_PORT;
4319 		isp_mark_getpdb_all(isp);
4320 		isp->isp_sendmarker = 1;
4321 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4322 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
4323 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
4324 #ifdef	ISP_TARGET_MODE
4325 		if (isp_target_async(isp, bus, mbox))
4326 			rval = -1;
4327 #endif
4328 		isp_prt(isp, ISP_LOGINFO, "Point-to-Point mode");
4329 		break;
4330 
4331 	case ASYNC_CONNMODE:
4332 		mbox = ISP_READ(isp, OUTMAILBOX1);
4333 		isp_mark_getpdb_all(isp);
4334 		switch (mbox) {
4335 		case ISP_CONN_LOOP:
4336 			isp_prt(isp, ISP_LOGINFO,
4337 			    "Point-to-Point -> Loop mode");
4338 			break;
4339 		case ISP_CONN_PTP:
4340 			isp_prt(isp, ISP_LOGINFO,
4341 			    "Loop -> Point-to-Point mode");
4342 			break;
4343 		case ISP_CONN_BADLIP:
4344 			isp_prt(isp, ISP_LOGWARN,
4345 			    "Point-to-Point -> Loop mode (BAD LIP)");
4346 			break;
4347 		case ISP_CONN_FATAL:
4348 			isp_prt(isp, ISP_LOGERR, "FATAL CONNECTION ERROR");
4349 #ifdef	ISP_FW_CRASH_DUMP
4350 			isp_async(isp, ISPASYNC_FW_CRASH, NULL);
4351 #else
4352 			isp_async(isp, ISPASYNC_FW_CRASH, NULL);
4353 			isp_reinit(isp);
4354 			isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
4355 #endif
4356 			return (-1);
4357 		case ISP_CONN_LOOPBACK:
4358 			isp_prt(isp, ISP_LOGWARN,
4359 			    "Looped Back in Point-to-Point mode");
4360 			break;
4361 		default:
4362 			isp_prt(isp, ISP_LOGWARN,
4363 			    "Unknown connection mode (0x%x)", mbox);
4364 			break;
4365 		}
4366 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
4367 		isp->isp_sendmarker = 1;
4368 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4369 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
4370 		break;
4371 
4372 	default:
4373 		isp_prt(isp, ISP_LOGWARN, "Unknown Async Code 0x%x", mbox);
4374 		break;
4375 	}
4376 
4377 	if (bus & 0x100) {
4378 		int i, nh;
4379 		u_int16_t handles[16];
4380 
4381 		for (nh = 0, i = 1; i < MAX_MAILBOX(isp); i++) {
4382 			if ((bus & (1 << i)) == 0) {
4383 				continue;
4384 			}
4385 			handles[nh++] = ISP_READ(isp, MBOX_OFF(i));
4386 		}
4387 		for (i = 0; i < nh; i++) {
4388 			isp_fastpost_complete(isp, handles[i]);
4389 			isp_prt(isp,  ISP_LOGDEBUG3,
4390 			    "fast post completion of %u", handles[i]);
4391 		}
4392 		if (isp->isp_fpcchiwater < nh)
4393 			isp->isp_fpcchiwater = nh;
4394 	} else {
4395 		isp->isp_intoasync++;
4396 	}
4397 	return (rval);
4398 }
4399 
4400 /*
4401  * Handle other response entries. A pointer to the request queue output
4402  * index is here in case we want to eat several entries at once, although
4403  * this is not used currently.
4404  */
4405 
4406 static int
4407 isp_handle_other_response(struct ispsoftc *isp, int type,
4408     isphdr_t *hp, u_int16_t *optrp)
4409 {
4410 	switch (type) {
4411 	case RQSTYPE_STATUS_CONT:
4412 		isp_prt(isp, ISP_LOGINFO, "Ignored Continuation Response");
4413 		return (1);
4414 	case RQSTYPE_ATIO:
4415 	case RQSTYPE_CTIO:
4416 	case RQSTYPE_ENABLE_LUN:
4417 	case RQSTYPE_MODIFY_LUN:
4418 	case RQSTYPE_NOTIFY:
4419 	case RQSTYPE_NOTIFY_ACK:
4420 	case RQSTYPE_CTIO1:
4421 	case RQSTYPE_ATIO2:
4422 	case RQSTYPE_CTIO2:
4423 	case RQSTYPE_CTIO3:
4424 		isp->isp_rsltccmplt++;	/* count as a response completion */
4425 #ifdef	ISP_TARGET_MODE
4426 		if (isp_target_notify(isp, (ispstatusreq_t *) hp, optrp)) {
4427 			return (1);
4428 		}
4429 #endif
4430 		/* FALLTHROUGH */
4431 	case RQSTYPE_REQUEST:
4432 	default:
4433 		USEC_DELAY(100);
4434 		if (type != isp_get_response_type(isp, hp)) {
4435 			/*
4436 			 * This is questionable- we're just papering over
4437 			 * something we've seen on SMP linux in target
4438 			 * mode- we don't really know what's happening
4439 			 * here that causes us to think we've gotten
4440 			 * an entry, but that either the entry isn't
4441 			 * filled out yet or our CPU read data is stale.
4442 			 */
4443 			isp_prt(isp, ISP_LOGINFO,
4444 				"unstable type in response queue");
4445 			return (-1);
4446 		}
4447 		isp_prt(isp, ISP_LOGWARN, "Unhandled Response Type 0x%x",
4448 		    isp_get_response_type(isp, hp));
4449 		if (isp_async(isp, ISPASYNC_UNHANDLED_RESPONSE, hp)) {
4450 			return (1);
4451 		}
4452 		return (0);
4453 	}
4454 }
4455 
4456 static void
4457 isp_parse_status(struct ispsoftc *isp, ispstatusreq_t *sp, XS_T *xs)
4458 {
4459 	switch (sp->req_completion_status & 0xff) {
4460 	case RQCS_COMPLETE:
4461 		if (XS_NOERR(xs)) {
4462 			XS_SETERR(xs, HBA_NOERROR);
4463 		}
4464 		return;
4465 
4466 	case RQCS_INCOMPLETE:
4467 		if ((sp->req_state_flags & RQSF_GOT_TARGET) == 0) {
4468 			isp_prt(isp, ISP_LOGDEBUG1,
4469 			    "Selection Timeout for %d.%d.%d",
4470 			    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4471 			if (XS_NOERR(xs)) {
4472 				XS_SETERR(xs, HBA_SELTIMEOUT);
4473 			}
4474 			return;
4475 		}
4476 		isp_prt(isp, ISP_LOGERR,
4477 		    "command incomplete for %d.%d.%d, state 0x%x",
4478 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs),
4479 		    sp->req_state_flags);
4480 		break;
4481 
4482 	case RQCS_DMA_ERROR:
4483 		isp_prt(isp, ISP_LOGERR, "DMA error for command on %d.%d.%d",
4484 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4485 		break;
4486 
4487 	case RQCS_TRANSPORT_ERROR:
4488 	{
4489 		char buf[172];
4490 		SNPRINTF(buf, sizeof (buf), "states=>");
4491 		if (sp->req_state_flags & RQSF_GOT_BUS) {
4492 			SNPRINTF(buf, sizeof (buf), "%s GOT_BUS", buf);
4493 		}
4494 		if (sp->req_state_flags & RQSF_GOT_TARGET) {
4495 			SNPRINTF(buf, sizeof (buf), "%s GOT_TGT", buf);
4496 		}
4497 		if (sp->req_state_flags & RQSF_SENT_CDB) {
4498 			SNPRINTF(buf, sizeof (buf), "%s SENT_CDB", buf);
4499 		}
4500 		if (sp->req_state_flags & RQSF_XFRD_DATA) {
4501 			SNPRINTF(buf, sizeof (buf), "%s XFRD_DATA", buf);
4502 		}
4503 		if (sp->req_state_flags & RQSF_GOT_STATUS) {
4504 			SNPRINTF(buf, sizeof (buf), "%s GOT_STS", buf);
4505 		}
4506 		if (sp->req_state_flags & RQSF_GOT_SENSE) {
4507 			SNPRINTF(buf, sizeof (buf), "%s GOT_SNS", buf);
4508 		}
4509 		if (sp->req_state_flags & RQSF_XFER_COMPLETE) {
4510 			SNPRINTF(buf, sizeof (buf), "%s XFR_CMPLT", buf);
4511 		}
4512 		SNPRINTF(buf, sizeof (buf), "%s\nstatus=>", buf);
4513 		if (sp->req_status_flags & RQSTF_DISCONNECT) {
4514 			SNPRINTF(buf, sizeof (buf), "%s Disconnect", buf);
4515 		}
4516 		if (sp->req_status_flags & RQSTF_SYNCHRONOUS) {
4517 			SNPRINTF(buf, sizeof (buf), "%s Sync_xfr", buf);
4518 		}
4519 		if (sp->req_status_flags & RQSTF_PARITY_ERROR) {
4520 			SNPRINTF(buf, sizeof (buf), "%s Parity", buf);
4521 		}
4522 		if (sp->req_status_flags & RQSTF_BUS_RESET) {
4523 			SNPRINTF(buf, sizeof (buf), "%s Bus_Reset", buf);
4524 		}
4525 		if (sp->req_status_flags & RQSTF_DEVICE_RESET) {
4526 			SNPRINTF(buf, sizeof (buf), "%s Device_Reset", buf);
4527 		}
4528 		if (sp->req_status_flags & RQSTF_ABORTED) {
4529 			SNPRINTF(buf, sizeof (buf), "%s Aborted", buf);
4530 		}
4531 		if (sp->req_status_flags & RQSTF_TIMEOUT) {
4532 			SNPRINTF(buf, sizeof (buf), "%s Timeout", buf);
4533 		}
4534 		if (sp->req_status_flags & RQSTF_NEGOTIATION) {
4535 			SNPRINTF(buf, sizeof (buf), "%s Negotiation", buf);
4536 		}
4537 		isp_prt(isp, ISP_LOGERR, "%s", buf);
4538 		isp_prt(isp, ISP_LOGERR, "transport error for %d.%d.%d:\n%s",
4539 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), buf);
4540 		break;
4541 	}
4542 	case RQCS_RESET_OCCURRED:
4543 		isp_prt(isp, ISP_LOGWARN,
4544 		    "bus reset destroyed command for %d.%d.%d",
4545 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4546 		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
4547 		if (XS_NOERR(xs)) {
4548 			XS_SETERR(xs, HBA_BUSRESET);
4549 		}
4550 		return;
4551 
4552 	case RQCS_ABORTED:
4553 		isp_prt(isp, ISP_LOGERR, "command aborted 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_ABORTED);
4558 		}
4559 		return;
4560 
4561 	case RQCS_TIMEOUT:
4562 		isp_prt(isp, ISP_LOGWARN, "command timed out for %d.%d.%d",
4563 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4564 		/*
4565 	 	 * Check to see if we logged out the device.
4566 		 */
4567 		if (IS_FC(isp)) {
4568 			if ((sp->req_completion_status & RQSTF_LOGOUT) &&
4569 			    FCPARAM(isp)->portdb[XS_TGT(xs)].valid &&
4570 			    FCPARAM(isp)->portdb[XS_TGT(xs)].fabric_dev) {
4571 				FCPARAM(isp)->portdb[XS_TGT(xs)].relogin = 1;
4572 			}
4573 		}
4574 		if (XS_NOERR(xs)) {
4575 			XS_SETERR(xs, HBA_CMDTIMEOUT);
4576 		}
4577 		return;
4578 
4579 	case RQCS_DATA_OVERRUN:
4580 		XS_RESID(xs) = sp->req_resid;
4581 		isp_prt(isp, ISP_LOGERR, "data overrun for command on %d.%d.%d",
4582 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4583 		if (XS_NOERR(xs)) {
4584 			XS_SETERR(xs, HBA_DATAOVR);
4585 		}
4586 		return;
4587 
4588 	case RQCS_COMMAND_OVERRUN:
4589 		isp_prt(isp, ISP_LOGERR,
4590 		    "command overrun for command on %d.%d.%d",
4591 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4592 		break;
4593 
4594 	case RQCS_STATUS_OVERRUN:
4595 		isp_prt(isp, ISP_LOGERR,
4596 		    "status overrun for command on %d.%d.%d",
4597 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4598 		break;
4599 
4600 	case RQCS_BAD_MESSAGE:
4601 		isp_prt(isp, ISP_LOGERR,
4602 		    "msg not COMMAND COMPLETE after status %d.%d.%d",
4603 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4604 		break;
4605 
4606 	case RQCS_NO_MESSAGE_OUT:
4607 		isp_prt(isp, ISP_LOGERR,
4608 		    "No MESSAGE OUT phase after selection on %d.%d.%d",
4609 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4610 		break;
4611 
4612 	case RQCS_EXT_ID_FAILED:
4613 		isp_prt(isp, ISP_LOGERR, "EXTENDED IDENTIFY failed %d.%d.%d",
4614 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4615 		break;
4616 
4617 	case RQCS_IDE_MSG_FAILED:
4618 		isp_prt(isp, ISP_LOGERR,
4619 		    "INITIATOR DETECTED ERROR rejected by %d.%d.%d",
4620 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4621 		break;
4622 
4623 	case RQCS_ABORT_MSG_FAILED:
4624 		isp_prt(isp, ISP_LOGERR, "ABORT OPERATION rejected by %d.%d.%d",
4625 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4626 		break;
4627 
4628 	case RQCS_REJECT_MSG_FAILED:
4629 		isp_prt(isp, ISP_LOGERR, "MESSAGE REJECT rejected by %d.%d.%d",
4630 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4631 		break;
4632 
4633 	case RQCS_NOP_MSG_FAILED:
4634 		isp_prt(isp, ISP_LOGERR, "NOP rejected by %d.%d.%d",
4635 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4636 		break;
4637 
4638 	case RQCS_PARITY_ERROR_MSG_FAILED:
4639 		isp_prt(isp, ISP_LOGERR,
4640 		    "MESSAGE PARITY ERROR rejected by %d.%d.%d",
4641 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4642 		break;
4643 
4644 	case RQCS_DEVICE_RESET_MSG_FAILED:
4645 		isp_prt(isp, ISP_LOGWARN,
4646 		    "BUS DEVICE RESET rejected by %d.%d.%d",
4647 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4648 		break;
4649 
4650 	case RQCS_ID_MSG_FAILED:
4651 		isp_prt(isp, ISP_LOGERR, "IDENTIFY rejected by %d.%d.%d",
4652 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4653 		break;
4654 
4655 	case RQCS_UNEXP_BUS_FREE:
4656 		isp_prt(isp, ISP_LOGERR, "%d.%d.%d had an unexpected bus free",
4657 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4658 		break;
4659 
4660 	case RQCS_DATA_UNDERRUN:
4661 	{
4662 		if (IS_FC(isp)) {
4663 			int ru_marked = (sp->req_scsi_status & RQCS_RU) != 0;
4664 			if (!ru_marked || sp->req_resid > XS_XFRLEN(xs)) {
4665 				isp_prt(isp, ISP_LOGWARN, bun, XS_TGT(xs),
4666 				    XS_LUN(xs), XS_XFRLEN(xs), sp->req_resid,
4667 				    (ru_marked)? "marked" : "not marked");
4668 				if (XS_NOERR(xs)) {
4669 					XS_SETERR(xs, HBA_BOTCH);
4670 				}
4671 				return;
4672 			}
4673 		}
4674 		XS_RESID(xs) = sp->req_resid;
4675 		if (XS_NOERR(xs)) {
4676 			XS_SETERR(xs, HBA_NOERROR);
4677 		}
4678 		return;
4679 	}
4680 
4681 	case RQCS_XACT_ERR1:
4682 		isp_prt(isp, ISP_LOGERR, xact1, XS_CHANNEL(xs),
4683 		    XS_TGT(xs), XS_LUN(xs));
4684 		break;
4685 
4686 	case RQCS_XACT_ERR2:
4687 		isp_prt(isp, ISP_LOGERR, xact2,
4688 		    XS_LUN(xs), XS_TGT(xs), XS_CHANNEL(xs));
4689 		break;
4690 
4691 	case RQCS_XACT_ERR3:
4692 		isp_prt(isp, ISP_LOGERR, xact3,
4693 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4694 		break;
4695 
4696 	case RQCS_BAD_ENTRY:
4697 		isp_prt(isp, ISP_LOGERR, "Invalid IOCB entry type detected");
4698 		break;
4699 
4700 	case RQCS_QUEUE_FULL:
4701 		isp_prt(isp, ISP_LOGDEBUG0,
4702 		    "internal queues full for %d.%d.%d status 0x%x",
4703 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), *XS_STSP(xs));
4704 
4705 		/*
4706 		 * If QFULL or some other status byte is set, then this
4707 		 * isn't an error, per se.
4708 		 *
4709 		 * Unfortunately, some QLogic f/w writers have, in
4710 		 * some cases, ommitted to *set* status to QFULL.
4711 		 *
4712 
4713 		if (*XS_STSP(xs) != SCSI_GOOD && XS_NOERR(xs)) {
4714 			XS_SETERR(xs, HBA_NOERROR);
4715 			return;
4716 		}
4717 
4718 		 *
4719 		 *
4720 		 */
4721 
4722 		*XS_STSP(xs) = SCSI_QFULL;
4723 		XS_SETERR(xs, HBA_NOERROR);
4724 		return;
4725 
4726 	case RQCS_PHASE_SKIPPED:
4727 		isp_prt(isp, ISP_LOGERR, pskip, XS_CHANNEL(xs),
4728 		    XS_TGT(xs), XS_LUN(xs));
4729 		break;
4730 
4731 	case RQCS_ARQS_FAILED:
4732 		isp_prt(isp, ISP_LOGERR,
4733 		    "Auto Request Sense failed for %d.%d.%d",
4734 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4735 		if (XS_NOERR(xs)) {
4736 			XS_SETERR(xs, HBA_ARQFAIL);
4737 		}
4738 		return;
4739 
4740 	case RQCS_WIDE_FAILED:
4741 		isp_prt(isp, ISP_LOGERR,
4742 		    "Wide Negotiation failed for %d.%d.%d",
4743 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
4744 		if (IS_SCSI(isp)) {
4745 			sdparam *sdp = isp->isp_param;
4746 			sdp += XS_CHANNEL(xs);
4747 			sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_WIDE;
4748 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
4749 			isp->isp_update |= (1 << XS_CHANNEL(xs));
4750 		}
4751 		if (XS_NOERR(xs)) {
4752 			XS_SETERR(xs, HBA_NOERROR);
4753 		}
4754 		return;
4755 
4756 	case RQCS_SYNCXFER_FAILED:
4757 		isp_prt(isp, ISP_LOGERR,
4758 		    "SDTR Message failed for target %d.%d.%d",
4759 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
4760 		if (IS_SCSI(isp)) {
4761 			sdparam *sdp = isp->isp_param;
4762 			sdp += XS_CHANNEL(xs);
4763 			sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_SYNC;
4764 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
4765 			isp->isp_update |= (1 << XS_CHANNEL(xs));
4766 		}
4767 		break;
4768 
4769 	case RQCS_LVD_BUSERR:
4770 		isp_prt(isp, ISP_LOGERR,
4771 		    "Bad LVD condition while talking to %d.%d.%d",
4772 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
4773 		break;
4774 
4775 	case RQCS_PORT_UNAVAILABLE:
4776 		/*
4777 		 * No such port on the loop. Moral equivalent of SELTIMEO
4778 		 */
4779 	case RQCS_PORT_LOGGED_OUT:
4780 		/*
4781 		 * It was there (maybe)- treat as a selection timeout.
4782 		 */
4783 		if ((sp->req_completion_status & 0xff) == RQCS_PORT_UNAVAILABLE)
4784 			isp_prt(isp, ISP_LOGINFO,
4785 			    "port unavailable for target %d", XS_TGT(xs));
4786 		else
4787 			isp_prt(isp, ISP_LOGINFO,
4788 			    "port logout for target %d", XS_TGT(xs));
4789 		/*
4790 		 * If we're on a local loop, force a LIP (which is overkill)
4791 		 * to force a re-login of this unit. If we're on fabric,
4792 		 * then we'll have to relogin as a matter of course.
4793 		 */
4794 		if (FCPARAM(isp)->isp_topo == TOPO_NL_PORT ||
4795 		    FCPARAM(isp)->isp_topo == TOPO_FL_PORT) {
4796 			mbreg_t mbs;
4797 			MEMZERO(&mbs, sizeof (mbs));
4798 			mbs.param[0] = MBOX_INIT_LIP;
4799 			isp_mboxcmd_qnw(isp, &mbs, 1);
4800 		}
4801 
4802 		/*
4803 		 * Probably overkill.
4804 		 */
4805 		isp->isp_sendmarker = 1;
4806 		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
4807 		isp_mark_getpdb_all(isp);
4808 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
4809 		if (XS_NOERR(xs)) {
4810 			XS_SETERR(xs, HBA_SELTIMEOUT);
4811 		}
4812 		return;
4813 
4814 	case RQCS_PORT_CHANGED:
4815 		isp_prt(isp, ISP_LOGWARN,
4816 		    "port changed for target %d", XS_TGT(xs));
4817 		if (XS_NOERR(xs)) {
4818 			XS_SETERR(xs, HBA_SELTIMEOUT);
4819 		}
4820 		return;
4821 
4822 	case RQCS_PORT_BUSY:
4823 		isp_prt(isp, ISP_LOGWARN,
4824 		    "port busy for target %d", XS_TGT(xs));
4825 		if (XS_NOERR(xs)) {
4826 			XS_SETERR(xs, HBA_TGTBSY);
4827 		}
4828 		return;
4829 
4830 	default:
4831 		isp_prt(isp, ISP_LOGERR, "Unknown Completion Status 0x%x",
4832 		    sp->req_completion_status);
4833 		break;
4834 	}
4835 	if (XS_NOERR(xs)) {
4836 		XS_SETERR(xs, HBA_BOTCH);
4837 	}
4838 }
4839 
4840 static void
4841 isp_fastpost_complete(struct ispsoftc *isp, u_int16_t fph)
4842 {
4843 	XS_T *xs;
4844 
4845 	if (fph == 0) {
4846 		return;
4847 	}
4848 	xs = isp_find_xs(isp, fph);
4849 	if (xs == NULL) {
4850 		isp_prt(isp, ISP_LOGWARN,
4851 		    "Command for fast post handle 0x%x not found", fph);
4852 		return;
4853 	}
4854 	isp_destroy_handle(isp, fph);
4855 
4856 	/*
4857 	 * Since we don't have a result queue entry item,
4858 	 * we must believe that SCSI status is zero and
4859 	 * that all data transferred.
4860 	 */
4861 	XS_SET_STATE_STAT(isp, xs, NULL);
4862 	XS_RESID(xs) = 0;
4863 	*XS_STSP(xs) = SCSI_GOOD;
4864 	if (XS_XFRLEN(xs)) {
4865 		ISP_DMAFREE(isp, xs, fph);
4866 	}
4867 	if (isp->isp_nactive)
4868 		isp->isp_nactive--;
4869 	isp->isp_fphccmplt++;
4870 	isp_done(xs);
4871 }
4872 
4873 static int
4874 isp_mbox_continue(struct ispsoftc *isp)
4875 {
4876 	mbreg_t mbs;
4877 	u_int16_t *ptr;
4878 
4879 	switch (isp->isp_lastmbxcmd) {
4880 	case MBOX_WRITE_RAM_WORD:
4881 	case MBOX_READ_RAM_WORD:
4882 	case MBOX_READ_RAM_WORD_EXTENDED:
4883 		break;
4884 	default:
4885 		return (1);
4886 	}
4887 	if (isp->isp_mboxtmp[0] != MBOX_COMMAND_COMPLETE) {
4888 		isp->isp_mbxwrk0 = 0;
4889 		return (-1);
4890 	}
4891 
4892 	/*
4893 	 * Clear the previous interrupt.
4894 	 */
4895 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
4896 	ISP_WRITE(isp, BIU_SEMA, 0);
4897 
4898 	/*
4899 	 * Continue with next word.
4900 	 */
4901 	MEMZERO(&mbs, sizeof (mbs));
4902 	ptr = isp->isp_mbxworkp;
4903 	switch (isp->isp_lastmbxcmd) {
4904 	case MBOX_WRITE_RAM_WORD:
4905 		mbs.param[2] = *ptr++;
4906 		mbs.param[1] = isp->isp_mbxwrk1++;
4907 		break;
4908 	case MBOX_READ_RAM_WORD:
4909 	case MBOX_READ_RAM_WORD_EXTENDED:
4910 		*ptr++ = isp->isp_mboxtmp[2];
4911 		mbs.param[1] = isp->isp_mbxwrk1++;
4912 		break;
4913 	}
4914 	isp->isp_mbxworkp = ptr;
4915 	mbs.param[0] = isp->isp_lastmbxcmd;
4916 	isp->isp_mbxwrk0 -= 1;
4917 	isp_mboxcmd_qnw(isp, &mbs, 0);
4918 	return (0);
4919 }
4920 
4921 
4922 #define	HIWRD(x)			((x) >> 16)
4923 #define	LOWRD(x)			((x)  & 0xffff)
4924 #define	ISPOPMAP(a, b)			(((a) << 16) | (b))
4925 static const u_int32_t mbpscsi[] = {
4926 	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
4927 	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
4928 	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
4929 	ISPOPMAP(0x1f, 0x01),	/* 0x03: MBOX_DUMP_RAM */
4930 	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
4931 	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
4932 	ISPOPMAP(0x3f, 0x3f),	/* 0x06: MBOX_MAILBOX_REG_TEST */
4933 	ISPOPMAP(0x03, 0x07),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
4934 	ISPOPMAP(0x01, 0x0f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
4935 	ISPOPMAP(0x00, 0x00),	/* 0x09: */
4936 	ISPOPMAP(0x00, 0x00),	/* 0x0a: */
4937 	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
4938 	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
4939 	ISPOPMAP(0x00, 0x00),	/* 0x0d: */
4940 	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
4941 	ISPOPMAP(0x00, 0x00),	/* 0x0f: */
4942 	ISPOPMAP(0x1f, 0x1f),	/* 0x10: MBOX_INIT_REQ_QUEUE */
4943 	ISPOPMAP(0x3f, 0x3f),	/* 0x11: MBOX_INIT_RES_QUEUE */
4944 	ISPOPMAP(0x0f, 0x0f),	/* 0x12: MBOX_EXECUTE_IOCB */
4945 	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
4946 	ISPOPMAP(0x01, 0x3f),	/* 0x14: MBOX_STOP_FIRMWARE */
4947 	ISPOPMAP(0x0f, 0x0f),	/* 0x15: MBOX_ABORT */
4948 	ISPOPMAP(0x03, 0x03),	/* 0x16: MBOX_ABORT_DEVICE */
4949 	ISPOPMAP(0x07, 0x07),	/* 0x17: MBOX_ABORT_TARGET */
4950 	ISPOPMAP(0x07, 0x07),	/* 0x18: MBOX_BUS_RESET */
4951 	ISPOPMAP(0x03, 0x07),	/* 0x19: MBOX_STOP_QUEUE */
4952 	ISPOPMAP(0x03, 0x07),	/* 0x1a: MBOX_START_QUEUE */
4953 	ISPOPMAP(0x03, 0x07),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
4954 	ISPOPMAP(0x03, 0x07),	/* 0x1c: MBOX_ABORT_QUEUE */
4955 	ISPOPMAP(0x03, 0x4f),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
4956 	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
4957 	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
4958 	ISPOPMAP(0x01, 0x07),	/* 0x20: MBOX_GET_INIT_SCSI_ID */
4959 	ISPOPMAP(0x01, 0x07),	/* 0x21: MBOX_GET_SELECT_TIMEOUT */
4960 	ISPOPMAP(0x01, 0xc7),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
4961 	ISPOPMAP(0x01, 0x07),	/* 0x23: MBOX_GET_TAG_AGE_LIMIT */
4962 	ISPOPMAP(0x01, 0x03),	/* 0x24: MBOX_GET_CLOCK_RATE */
4963 	ISPOPMAP(0x01, 0x07),	/* 0x25: MBOX_GET_ACT_NEG_STATE */
4964 	ISPOPMAP(0x01, 0x07),	/* 0x26: MBOX_GET_ASYNC_DATA_SETUP_TIME */
4965 	ISPOPMAP(0x01, 0x07),	/* 0x27: MBOX_GET_PCI_PARAMS */
4966 	ISPOPMAP(0x03, 0x4f),	/* 0x28: MBOX_GET_TARGET_PARAMS */
4967 	ISPOPMAP(0x03, 0x0f),	/* 0x29: MBOX_GET_DEV_QUEUE_PARAMS */
4968 	ISPOPMAP(0x01, 0x07),	/* 0x2a: MBOX_GET_RESET_DELAY_PARAMS */
4969 	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
4970 	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
4971 	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
4972 	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
4973 	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
4974 	ISPOPMAP(0x03, 0x03),	/* 0x30: MBOX_SET_INIT_SCSI_ID */
4975 	ISPOPMAP(0x07, 0x07),	/* 0x31: MBOX_SET_SELECT_TIMEOUT */
4976 	ISPOPMAP(0xc7, 0xc7),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
4977 	ISPOPMAP(0x07, 0x07),	/* 0x33: MBOX_SET_TAG_AGE_LIMIT */
4978 	ISPOPMAP(0x03, 0x03),	/* 0x34: MBOX_SET_CLOCK_RATE */
4979 	ISPOPMAP(0x07, 0x07),	/* 0x35: MBOX_SET_ACT_NEG_STATE */
4980 	ISPOPMAP(0x07, 0x07),	/* 0x36: MBOX_SET_ASYNC_DATA_SETUP_TIME */
4981 	ISPOPMAP(0x07, 0x07),	/* 0x37: MBOX_SET_PCI_CONTROL_PARAMS */
4982 	ISPOPMAP(0x4f, 0x4f),	/* 0x38: MBOX_SET_TARGET_PARAMS */
4983 	ISPOPMAP(0x0f, 0x0f),	/* 0x39: MBOX_SET_DEV_QUEUE_PARAMS */
4984 	ISPOPMAP(0x07, 0x07),	/* 0x3a: MBOX_SET_RESET_DELAY_PARAMS */
4985 	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
4986 	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
4987 	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
4988 	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
4989 	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
4990 	ISPOPMAP(0x01, 0x03),	/* 0x40: MBOX_RETURN_BIOS_BLOCK_ADDR */
4991 	ISPOPMAP(0x3f, 0x01),	/* 0x41: MBOX_WRITE_FOUR_RAM_WORDS */
4992 	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_EXEC_BIOS_IOCB */
4993 	ISPOPMAP(0x00, 0x00),	/* 0x43: */
4994 	ISPOPMAP(0x00, 0x00),	/* 0x44: */
4995 	ISPOPMAP(0x03, 0x03),	/* 0x45: SET SYSTEM PARAMETER */
4996 	ISPOPMAP(0x01, 0x03),	/* 0x46: GET SYSTEM PARAMETER */
4997 	ISPOPMAP(0x00, 0x00),	/* 0x47: */
4998 	ISPOPMAP(0x01, 0xcf),	/* 0x48: GET SCAM CONFIGURATION */
4999 	ISPOPMAP(0xcf, 0xcf),	/* 0x49: SET SCAM CONFIGURATION */
5000 	ISPOPMAP(0x03, 0x03),	/* 0x4a: MBOX_SET_FIRMWARE_FEATURES */
5001 	ISPOPMAP(0x01, 0x03),	/* 0x4b: MBOX_GET_FIRMWARE_FEATURES */
5002 	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
5003 	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
5004 	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
5005 	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
5006 	ISPOPMAP(0xdf, 0xdf),	/* 0x50: LOAD RAM A64 */
5007 	ISPOPMAP(0xdf, 0xdf),	/* 0x51: DUMP RAM A64 */
5008 	ISPOPMAP(0xdf, 0xff),	/* 0x52: INITIALIZE REQUEST QUEUE A64 */
5009 	ISPOPMAP(0xef, 0xff),	/* 0x53: INITIALIZE RESPONSE QUEUE A64 */
5010 	ISPOPMAP(0xcf, 0x01),	/* 0x54: EXECUTE IOCB A64 */
5011 	ISPOPMAP(0x07, 0x01),	/* 0x55: ENABLE TARGET MODE */
5012 	ISPOPMAP(0x03, 0x0f),	/* 0x56: GET TARGET STATUS */
5013 	ISPOPMAP(0x00, 0x00),	/* 0x57: */
5014 	ISPOPMAP(0x00, 0x00),	/* 0x58: */
5015 	ISPOPMAP(0x00, 0x00),	/* 0x59: */
5016 	ISPOPMAP(0x03, 0x03),	/* 0x5a: SET DATA OVERRUN RECOVERY MODE */
5017 	ISPOPMAP(0x01, 0x03),	/* 0x5b: GET DATA OVERRUN RECOVERY MODE */
5018 	ISPOPMAP(0x0f, 0x0f),	/* 0x5c: SET HOST DATA */
5019 	ISPOPMAP(0x01, 0x01)	/* 0x5d: GET NOST DATA */
5020 };
5021 
5022 #ifndef	ISP_STRIPPED
5023 static char *scsi_mbcmd_names[] = {
5024 	"NO-OP",
5025 	"LOAD RAM",
5026 	"EXEC FIRMWARE",
5027 	"DUMP RAM",
5028 	"WRITE RAM WORD",
5029 	"READ RAM WORD",
5030 	"MAILBOX REG TEST",
5031 	"VERIFY CHECKSUM",
5032 	"ABOUT FIRMWARE",
5033 	NULL,
5034 	NULL,
5035 	NULL,
5036 	NULL,
5037 	NULL,
5038 	"CHECK FIRMWARE",
5039 	NULL,
5040 	"INIT REQUEST QUEUE",
5041 	"INIT RESULT QUEUE",
5042 	"EXECUTE IOCB",
5043 	"WAKE UP",
5044 	"STOP FIRMWARE",
5045 	"ABORT",
5046 	"ABORT DEVICE",
5047 	"ABORT TARGET",
5048 	"BUS RESET",
5049 	"STOP QUEUE",
5050 	"START QUEUE",
5051 	"SINGLE STEP QUEUE",
5052 	"ABORT QUEUE",
5053 	"GET DEV QUEUE STATUS",
5054 	NULL,
5055 	"GET FIRMWARE STATUS",
5056 	"GET INIT SCSI ID",
5057 	"GET SELECT TIMEOUT",
5058 	"GET RETRY COUNT",
5059 	"GET TAG AGE LIMIT",
5060 	"GET CLOCK RATE",
5061 	"GET ACT NEG STATE",
5062 	"GET ASYNC DATA SETUP TIME",
5063 	"GET PCI PARAMS",
5064 	"GET TARGET PARAMS",
5065 	"GET DEV QUEUE PARAMS",
5066 	"GET RESET DELAY PARAMS",
5067 	NULL,
5068 	NULL,
5069 	NULL,
5070 	NULL,
5071 	NULL,
5072 	"SET INIT SCSI ID",
5073 	"SET SELECT TIMEOUT",
5074 	"SET RETRY COUNT",
5075 	"SET TAG AGE LIMIT",
5076 	"SET CLOCK RATE",
5077 	"SET ACT NEG STATE",
5078 	"SET ASYNC DATA SETUP TIME",
5079 	"SET PCI CONTROL PARAMS",
5080 	"SET TARGET PARAMS",
5081 	"SET DEV QUEUE PARAMS",
5082 	"SET RESET DELAY PARAMS",
5083 	NULL,
5084 	NULL,
5085 	NULL,
5086 	NULL,
5087 	NULL,
5088 	"RETURN BIOS BLOCK ADDR",
5089 	"WRITE FOUR RAM WORDS",
5090 	"EXEC BIOS IOCB",
5091 	NULL,
5092 	NULL,
5093 	"SET SYSTEM PARAMETER",
5094 	"GET SYSTEM PARAMETER",
5095 	NULL,
5096 	"GET SCAM CONFIGURATION",
5097 	"SET SCAM CONFIGURATION",
5098 	"SET FIRMWARE FEATURES",
5099 	"GET FIRMWARE FEATURES",
5100 	NULL,
5101 	NULL,
5102 	NULL,
5103 	NULL,
5104 	"LOAD RAM A64",
5105 	"DUMP RAM A64",
5106 	"INITIALIZE REQUEST QUEUE A64",
5107 	"INITIALIZE RESPONSE QUEUE A64",
5108 	"EXECUTE IOCB A64",
5109 	"ENABLE TARGET MODE",
5110 	"GET TARGET MODE STATE",
5111 	NULL,
5112 	NULL,
5113 	NULL,
5114 	"SET DATA OVERRUN RECOVERY MODE",
5115 	"GET DATA OVERRUN RECOVERY MODE",
5116 	"SET HOST DATA",
5117 	"GET NOST DATA",
5118 };
5119 #endif
5120 
5121 static const u_int32_t mbpfc[] = {
5122 	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
5123 	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
5124 	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
5125 	ISPOPMAP(0xdf, 0x01),	/* 0x03: MBOX_DUMP_RAM */
5126 	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
5127 	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
5128 	ISPOPMAP(0xff, 0xff),	/* 0x06: MBOX_MAILBOX_REG_TEST */
5129 	ISPOPMAP(0x03, 0x05),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
5130 	ISPOPMAP(0x01, 0x4f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
5131 	ISPOPMAP(0xdf, 0x01),	/* 0x09: LOAD RAM */
5132 	ISPOPMAP(0xdf, 0x01),	/* 0x0a: DUMP RAM */
5133 	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
5134 	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
5135 	ISPOPMAP(0x00, 0x00),	/* 0x0d: */
5136 	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
5137 	ISPOPMAP(0x03, 0x07),	/* 0x0f: MBOX_READ_RAM_WORD_EXTENDED(1) */
5138 	ISPOPMAP(0x1f, 0x11),	/* 0x10: MBOX_INIT_REQ_QUEUE */
5139 	ISPOPMAP(0x2f, 0x21),	/* 0x11: MBOX_INIT_RES_QUEUE */
5140 	ISPOPMAP(0x0f, 0x01),	/* 0x12: MBOX_EXECUTE_IOCB */
5141 	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
5142 	ISPOPMAP(0x01, 0xff),	/* 0x14: MBOX_STOP_FIRMWARE */
5143 	ISPOPMAP(0x4f, 0x01),	/* 0x15: MBOX_ABORT */
5144 	ISPOPMAP(0x07, 0x01),	/* 0x16: MBOX_ABORT_DEVICE */
5145 	ISPOPMAP(0x07, 0x01),	/* 0x17: MBOX_ABORT_TARGET */
5146 	ISPOPMAP(0x03, 0x03),	/* 0x18: MBOX_BUS_RESET */
5147 	ISPOPMAP(0x07, 0x05),	/* 0x19: MBOX_STOP_QUEUE */
5148 	ISPOPMAP(0x07, 0x05),	/* 0x1a: MBOX_START_QUEUE */
5149 	ISPOPMAP(0x07, 0x05),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
5150 	ISPOPMAP(0x07, 0x05),	/* 0x1c: MBOX_ABORT_QUEUE */
5151 	ISPOPMAP(0x07, 0x03),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
5152 	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
5153 	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
5154 	ISPOPMAP(0x01, 0x4f),	/* 0x20: MBOX_GET_LOOP_ID */
5155 	ISPOPMAP(0x00, 0x00),	/* 0x21: */
5156 	ISPOPMAP(0x01, 0x07),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
5157 	ISPOPMAP(0x00, 0x00),	/* 0x23: */
5158 	ISPOPMAP(0x00, 0x00),	/* 0x24: */
5159 	ISPOPMAP(0x00, 0x00),	/* 0x25: */
5160 	ISPOPMAP(0x00, 0x00),	/* 0x26: */
5161 	ISPOPMAP(0x00, 0x00),	/* 0x27: */
5162 	ISPOPMAP(0x01, 0x03),	/* 0x28: MBOX_GET_FIRMWARE_OPTIONS */
5163 	ISPOPMAP(0x03, 0x07),	/* 0x29: MBOX_GET_PORT_QUEUE_PARAMS */
5164 	ISPOPMAP(0x00, 0x00),	/* 0x2a: */
5165 	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
5166 	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
5167 	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
5168 	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
5169 	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
5170 	ISPOPMAP(0x00, 0x00),	/* 0x30: */
5171 	ISPOPMAP(0x00, 0x00),	/* 0x31: */
5172 	ISPOPMAP(0x07, 0x07),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
5173 	ISPOPMAP(0x00, 0x00),	/* 0x33: */
5174 	ISPOPMAP(0x00, 0x00),	/* 0x34: */
5175 	ISPOPMAP(0x00, 0x00),	/* 0x35: */
5176 	ISPOPMAP(0x00, 0x00),	/* 0x36: */
5177 	ISPOPMAP(0x00, 0x00),	/* 0x37: */
5178 	ISPOPMAP(0x0f, 0x01),	/* 0x38: MBOX_SET_FIRMWARE_OPTIONS */
5179 	ISPOPMAP(0x0f, 0x07),	/* 0x39: MBOX_SET_PORT_QUEUE_PARAMS */
5180 	ISPOPMAP(0x00, 0x00),	/* 0x3a: */
5181 	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
5182 	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
5183 	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
5184 	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
5185 	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
5186 	ISPOPMAP(0x03, 0x01),	/* 0x40: MBOX_LOOP_PORT_BYPASS */
5187 	ISPOPMAP(0x03, 0x01),	/* 0x41: MBOX_LOOP_PORT_ENABLE */
5188 	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_GET_RESOURCE_COUNTS */
5189 	ISPOPMAP(0x01, 0x01),	/* 0x43: MBOX_REQUEST_NON_PARTICIPATING_MODE */
5190 	ISPOPMAP(0x00, 0x00),	/* 0x44: */
5191 	ISPOPMAP(0x00, 0x00),	/* 0x45: */
5192 	ISPOPMAP(0x00, 0x00),	/* 0x46: */
5193 	ISPOPMAP(0xcf, 0x03),	/* 0x47: GET PORT_DATABASE ENHANCED */
5194 	ISPOPMAP(0x00, 0x00),	/* 0x48: */
5195 	ISPOPMAP(0x00, 0x00),	/* 0x49: */
5196 	ISPOPMAP(0x00, 0x00),	/* 0x4a: */
5197 	ISPOPMAP(0x00, 0x00),	/* 0x4b: */
5198 	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
5199 	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
5200 	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
5201 	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
5202 	ISPOPMAP(0x00, 0x00),	/* 0x50: */
5203 	ISPOPMAP(0x00, 0x00),	/* 0x51: */
5204 	ISPOPMAP(0x00, 0x00),	/* 0x52: */
5205 	ISPOPMAP(0x00, 0x00),	/* 0x53: */
5206 	ISPOPMAP(0xcf, 0x01),	/* 0x54: EXECUTE IOCB A64 */
5207 	ISPOPMAP(0x00, 0x00),	/* 0x55: */
5208 	ISPOPMAP(0x00, 0x00),	/* 0x56: */
5209 	ISPOPMAP(0x00, 0x00),	/* 0x57: */
5210 	ISPOPMAP(0x00, 0x00),	/* 0x58: */
5211 	ISPOPMAP(0x00, 0x00),	/* 0x59: */
5212 	ISPOPMAP(0x00, 0x00),	/* 0x5a: */
5213 	ISPOPMAP(0x03, 0x01),	/* 0x5b: MBOX_DRIVER_HEARTBEAT */
5214 	ISPOPMAP(0xcf, 0x01),	/* 0x5c: MBOX_FW_HEARTBEAT */
5215 	ISPOPMAP(0x07, 0x03),	/* 0x5d: MBOX_GET_SET_DATA_RATE */
5216 	ISPOPMAP(0x00, 0x00),	/* 0x5e: */
5217 	ISPOPMAP(0x00, 0x00),	/* 0x5f: */
5218 	ISPOPMAP(0xfd, 0x31),	/* 0x60: MBOX_INIT_FIRMWARE */
5219 	ISPOPMAP(0x00, 0x00),	/* 0x61: */
5220 	ISPOPMAP(0x01, 0x01),	/* 0x62: MBOX_INIT_LIP */
5221 	ISPOPMAP(0xcd, 0x03),	/* 0x63: MBOX_GET_FC_AL_POSITION_MAP */
5222 	ISPOPMAP(0xcf, 0x01),	/* 0x64: MBOX_GET_PORT_DB */
5223 	ISPOPMAP(0x07, 0x01),	/* 0x65: MBOX_CLEAR_ACA */
5224 	ISPOPMAP(0x07, 0x01),	/* 0x66: MBOX_TARGET_RESET */
5225 	ISPOPMAP(0x07, 0x01),	/* 0x67: MBOX_CLEAR_TASK_SET */
5226 	ISPOPMAP(0x07, 0x01),	/* 0x68: MBOX_ABORT_TASK_SET */
5227 	ISPOPMAP(0x01, 0x07),	/* 0x69: MBOX_GET_FW_STATE */
5228 	ISPOPMAP(0x03, 0xcf),	/* 0x6a: MBOX_GET_PORT_NAME */
5229 	ISPOPMAP(0xcf, 0x01),	/* 0x6b: MBOX_GET_LINK_STATUS */
5230 	ISPOPMAP(0x0f, 0x01),	/* 0x6c: MBOX_INIT_LIP_RESET */
5231 	ISPOPMAP(0x00, 0x00),	/* 0x6d: */
5232 	ISPOPMAP(0xcf, 0x03),	/* 0x6e: MBOX_SEND_SNS */
5233 	ISPOPMAP(0x0f, 0x07),	/* 0x6f: MBOX_FABRIC_LOGIN */
5234 	ISPOPMAP(0x03, 0x01),	/* 0x70: MBOX_SEND_CHANGE_REQUEST */
5235 	ISPOPMAP(0x03, 0x03),	/* 0x71: MBOX_FABRIC_LOGOUT */
5236 	ISPOPMAP(0x0f, 0x0f),	/* 0x72: MBOX_INIT_LIP_LOGIN */
5237 	ISPOPMAP(0x00, 0x00),	/* 0x73: */
5238 	ISPOPMAP(0x07, 0x01),	/* 0x74: LOGIN LOOP PORT */
5239 	ISPOPMAP(0xcf, 0x03),	/* 0x75: GET PORT/NODE NAME LIST */
5240 	ISPOPMAP(0x4f, 0x01),	/* 0x76: SET VENDOR ID */
5241 	ISPOPMAP(0xcd, 0x01),	/* 0x77: INITIALIZE IP MAILBOX */
5242 	ISPOPMAP(0x00, 0x00),	/* 0x78: */
5243 	ISPOPMAP(0x00, 0x00),	/* 0x79: */
5244 	ISPOPMAP(0x00, 0x00),	/* 0x7a: */
5245 	ISPOPMAP(0x00, 0x00),	/* 0x7b: */
5246 	ISPOPMAP(0x4f, 0x03),	/* 0x7c: Get ID List */
5247 	ISPOPMAP(0xcf, 0x01),	/* 0x7d: SEND LFA */
5248 	ISPOPMAP(0x0f, 0x01)	/* 0x7e: LUN RESET */
5249 };
5250 /*
5251  * Footnotes
5252  *
5253  * (1): this sets bits 21..16 in mailbox register #8, which we nominally
5254  *	do not access at this time in the core driver. The caller is
5255  *	responsible for setting this register first (Gross!).
5256  */
5257 
5258 #ifndef	ISP_STRIPPED
5259 static char *fc_mbcmd_names[] = {
5260 	"NO-OP",
5261 	"LOAD RAM",
5262 	"EXEC FIRMWARE",
5263 	"DUMP RAM",
5264 	"WRITE RAM WORD",
5265 	"READ RAM WORD",
5266 	"MAILBOX REG TEST",
5267 	"VERIFY CHECKSUM",
5268 	"ABOUT FIRMWARE",
5269 	"LOAD RAM",
5270 	"DUMP RAM",
5271 	NULL,
5272 	NULL,
5273 	"READ RAM WORD EXTENDED",
5274 	"CHECK FIRMWARE",
5275 	NULL,
5276 	"INIT REQUEST QUEUE",
5277 	"INIT RESULT QUEUE",
5278 	"EXECUTE IOCB",
5279 	"WAKE UP",
5280 	"STOP FIRMWARE",
5281 	"ABORT",
5282 	"ABORT DEVICE",
5283 	"ABORT TARGET",
5284 	"BUS RESET",
5285 	"STOP QUEUE",
5286 	"START QUEUE",
5287 	"SINGLE STEP QUEUE",
5288 	"ABORT QUEUE",
5289 	"GET DEV QUEUE STATUS",
5290 	NULL,
5291 	"GET FIRMWARE STATUS",
5292 	"GET LOOP ID",
5293 	NULL,
5294 	"GET RETRY COUNT",
5295 	NULL,
5296 	NULL,
5297 	NULL,
5298 	NULL,
5299 	NULL,
5300 	"GET FIRMWARE OPTIONS",
5301 	"GET PORT QUEUE PARAMS",
5302 	NULL,
5303 	NULL,
5304 	NULL,
5305 	NULL,
5306 	NULL,
5307 	NULL,
5308 	NULL,
5309 	NULL,
5310 	"SET RETRY COUNT",
5311 	NULL,
5312 	NULL,
5313 	NULL,
5314 	NULL,
5315 	NULL,
5316 	"SET FIRMWARE OPTIONS",
5317 	"SET PORT QUEUE PARAMS",
5318 	NULL,
5319 	NULL,
5320 	NULL,
5321 	NULL,
5322 	NULL,
5323 	NULL,
5324 	"LOOP PORT BYPASS",
5325 	"LOOP PORT ENABLE",
5326 	"GET RESOURCE COUNTS",
5327 	"REQUEST NON PARTICIPATING MODE",
5328 	NULL,
5329 	NULL,
5330 	NULL,
5331 	"GET PORT DATABASE,, ENHANCED",
5332 	NULL,
5333 	NULL,
5334 	NULL,
5335 	NULL,
5336 	NULL,
5337 	NULL,
5338 	NULL,
5339 	NULL,
5340 	NULL,
5341 	NULL,
5342 	NULL,
5343 	NULL,
5344 	"EXECUTE IOCB A64",
5345 	NULL,
5346 	NULL,
5347 	NULL,
5348 	NULL,
5349 	NULL,
5350 	NULL,
5351 	"DRIVER HEARTBEAT",
5352 	NULL,
5353 	"GET/SET DATA RATE",
5354 	NULL,
5355 	NULL,
5356 	"INIT FIRMWARE",
5357 	NULL,
5358 	"INIT LIP",
5359 	"GET FC-AL POSITION MAP",
5360 	"GET PORT DATABASE",
5361 	"CLEAR ACA",
5362 	"TARGET RESET",
5363 	"CLEAR TASK SET",
5364 	"ABORT TASK SET",
5365 	"GET FW STATE",
5366 	"GET PORT NAME",
5367 	"GET LINK STATUS",
5368 	"INIT LIP RESET",
5369 	NULL,
5370 	"SEND SNS",
5371 	"FABRIC LOGIN",
5372 	"SEND CHANGE REQUEST",
5373 	"FABRIC LOGOUT",
5374 	"INIT LIP LOGIN",
5375 	NULL,
5376 	"LOGIN LOOP PORT",
5377 	"GET PORT/NODE NAME LIST",
5378 	"SET VENDOR ID",
5379 	"INITIALIZE IP MAILBOX",
5380 	NULL,
5381 	NULL,
5382 	NULL,
5383 	NULL,
5384 	"Get ID List",
5385 	"SEND LFA",
5386 	"Lun RESET"
5387 };
5388 #endif
5389 
5390 static void
5391 isp_mboxcmd_qnw(struct ispsoftc *isp, mbreg_t *mbp, int nodelay)
5392 {
5393 	unsigned int ibits, obits, box, opcode;
5394 	const u_int32_t *mcp;
5395 
5396 	if (IS_FC(isp)) {
5397 		mcp = mbpfc;
5398 	} else {
5399 		mcp = mbpscsi;
5400 	}
5401 	opcode = mbp->param[0];
5402 	ibits = HIWRD(mcp[opcode]) & NMBOX_BMASK(isp);
5403 	obits = LOWRD(mcp[opcode]) & NMBOX_BMASK(isp);
5404 	ibits |= mbp->ibits;
5405 	obits |= mbp->obits;
5406 	for (box = 0; box < MAX_MAILBOX(isp); box++) {
5407 		if (ibits & (1 << box)) {
5408 			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
5409 		}
5410 		if (nodelay == 0) {
5411 			isp->isp_mboxtmp[box] = mbp->param[box] = 0;
5412 		}
5413 	}
5414 	if (nodelay == 0) {
5415 		isp->isp_lastmbxcmd = opcode;
5416 		isp->isp_obits = obits;
5417 		isp->isp_mboxbsy = 1;
5418 	}
5419 	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
5420 	/*
5421 	 * Oddly enough, if we're not delaying for an answer,
5422 	 * delay a bit to give the f/w a chance to pick up the
5423 	 * command.
5424 	 */
5425 	if (nodelay) {
5426 		USEC_DELAY(1000);
5427 	}
5428 }
5429 
5430 static void
5431 isp_mboxcmd(struct ispsoftc *isp, mbreg_t *mbp, int logmask)
5432 {
5433 	char *cname, *xname, tname[16], mname[16];
5434 	unsigned int lim, ibits, obits, box, opcode;
5435 	const u_int32_t *mcp;
5436 
5437 	if (IS_FC(isp)) {
5438 		mcp = mbpfc;
5439 		lim = (sizeof (mbpfc) / sizeof (mbpfc[0]));
5440 	} else {
5441 		mcp = mbpscsi;
5442 		lim = (sizeof (mbpscsi) / sizeof (mbpscsi[0]));
5443 	}
5444 
5445 	if ((opcode = mbp->param[0]) >= lim) {
5446 		mbp->param[0] = MBOX_INVALID_COMMAND;
5447 		isp_prt(isp, ISP_LOGERR, "Unknown Command 0x%x", opcode);
5448 		return;
5449 	}
5450 
5451 	ibits = HIWRD(mcp[opcode]) & NMBOX_BMASK(isp);
5452 	obits = LOWRD(mcp[opcode]) & NMBOX_BMASK(isp);
5453 
5454 	ibits |= mbp->ibits;
5455 	obits |= mbp->obits;
5456 
5457 	if (ibits == 0 && obits == 0) {
5458 		mbp->param[0] = MBOX_COMMAND_PARAM_ERROR;
5459 		isp_prt(isp, ISP_LOGERR, "no parameters for 0x%x", opcode);
5460 		return;
5461 	}
5462 
5463 	/*
5464 	 * Get exclusive usage of mailbox registers.
5465 	 */
5466 	MBOX_ACQUIRE(isp);
5467 
5468 	for (box = 0; box < MAX_MAILBOX(isp); box++) {
5469 		if (ibits & (1 << box)) {
5470 			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
5471 		}
5472 		isp->isp_mboxtmp[box] = mbp->param[box] = 0;
5473 	}
5474 
5475 	isp->isp_lastmbxcmd = opcode;
5476 
5477 	/*
5478 	 * We assume that we can't overwrite a previous command.
5479 	 */
5480 	isp->isp_obits = obits;
5481 	isp->isp_mboxbsy = 1;
5482 
5483 	/*
5484 	 * Set Host Interrupt condition so that RISC will pick up mailbox regs.
5485 	 */
5486 	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
5487 
5488 	/*
5489 	 * While we haven't finished the command, spin our wheels here.
5490 	 */
5491 	MBOX_WAIT_COMPLETE(isp);
5492 
5493 	if (isp->isp_mboxbsy) {
5494 		/*
5495 		 * Command timed out.
5496 		 */
5497 		isp->isp_mboxbsy = 0;
5498 		MBOX_RELEASE(isp);
5499 		return;
5500 	}
5501 
5502 	/*
5503 	 * Copy back output registers.
5504 	 */
5505 	for (box = 0; box < MAX_MAILBOX(isp); box++) {
5506 		if (obits & (1 << box)) {
5507 			mbp->param[box] = isp->isp_mboxtmp[box];
5508 		}
5509 	}
5510 
5511 	MBOX_RELEASE(isp);
5512 
5513 	if (logmask == 0 || opcode == MBOX_EXEC_FIRMWARE) {
5514 		return;
5515 	}
5516 #ifdef	ISP_STRIPPED
5517 	cname = NULL;
5518 #else
5519 	cname = (IS_FC(isp))? fc_mbcmd_names[opcode] : scsi_mbcmd_names[opcode];
5520 #endif
5521 	if (cname == NULL) {
5522 		cname = tname;
5523 		SNPRINTF(tname, sizeof tname, "opcode %x", opcode);
5524 	}
5525 
5526 	/*
5527 	 * Just to be chatty here...
5528 	 */
5529 	xname = NULL;
5530 	switch (mbp->param[0]) {
5531 	case MBOX_COMMAND_COMPLETE:
5532 		break;
5533 	case MBOX_INVALID_COMMAND:
5534 		if (logmask & MBLOGMASK(MBOX_COMMAND_COMPLETE))
5535 			xname = "INVALID COMMAND";
5536 		break;
5537 	case MBOX_HOST_INTERFACE_ERROR:
5538 		if (logmask & MBLOGMASK(MBOX_HOST_INTERFACE_ERROR))
5539 			xname = "HOST INTERFACE ERROR";
5540 		break;
5541 	case MBOX_TEST_FAILED:
5542 		if (logmask & MBLOGMASK(MBOX_TEST_FAILED))
5543 			xname = "TEST FAILED";
5544 		break;
5545 	case MBOX_COMMAND_ERROR:
5546 		if (logmask & MBLOGMASK(MBOX_COMMAND_ERROR))
5547 			xname = "COMMAND ERROR";
5548 		break;
5549 	case MBOX_COMMAND_PARAM_ERROR:
5550 		if (logmask & MBLOGMASK(MBOX_COMMAND_PARAM_ERROR))
5551 			xname = "COMMAND PARAMETER ERROR";
5552 		break;
5553 	case MBOX_LOOP_ID_USED:
5554 		if (logmask & MBLOGMASK(MBOX_LOOP_ID_USED))
5555 			xname = "LOOP ID ALREADY IN USE";
5556 		break;
5557 	case MBOX_PORT_ID_USED:
5558 		if (logmask & MBLOGMASK(MBOX_PORT_ID_USED))
5559 			xname = "PORT ID ALREADY IN USE";
5560 		break;
5561 	case MBOX_ALL_IDS_USED:
5562 		if (logmask & MBLOGMASK(MBOX_ALL_IDS_USED))
5563 			xname = "ALL LOOP IDS IN USE";
5564 		break;
5565 	case 0:		/* special case */
5566 		xname = "TIMEOUT";
5567 		break;
5568 	default:
5569 		SNPRINTF(mname, sizeof mname, "error 0x%x", mbp->param[0]);
5570 		xname = mname;
5571 		break;
5572 	}
5573 	if (xname)
5574 		isp_prt(isp, ISP_LOGALL, "Mailbox Command '%s' failed (%s)",
5575 		    cname, xname);
5576 }
5577 
5578 static void
5579 isp_fw_state(struct ispsoftc *isp)
5580 {
5581 	if (IS_FC(isp)) {
5582 		mbreg_t mbs;
5583 		fcparam *fcp = isp->isp_param;
5584 
5585 		MEMZERO(&mbs, sizeof (mbs));
5586 		mbs.param[0] = MBOX_GET_FW_STATE;
5587 		isp_mboxcmd(isp, &mbs, MBLOGALL);
5588 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
5589 			fcp->isp_fwstate = mbs.param[1];
5590 		}
5591 	}
5592 }
5593 
5594 static void
5595 isp_update(struct ispsoftc *isp)
5596 {
5597 	int bus, upmask;
5598 
5599 	for (bus = 0, upmask = isp->isp_update; upmask != 0; bus++) {
5600 		if (upmask & (1 << bus)) {
5601 			isp_update_bus(isp, bus);
5602 		}
5603 		upmask &= ~(1 << bus);
5604 	}
5605 }
5606 
5607 static void
5608 isp_update_bus(struct ispsoftc *isp, int bus)
5609 {
5610 	int tgt;
5611 	mbreg_t mbs;
5612 	sdparam *sdp;
5613 
5614 	isp->isp_update &= ~(1 << bus);
5615 	if (IS_FC(isp)) {
5616 		/*
5617 		 * There are no 'per-bus' settings for Fibre Channel.
5618 		 */
5619 		return;
5620 	}
5621 	sdp = isp->isp_param;
5622 	sdp += bus;
5623 	MEMZERO(&mbs, sizeof (mbs));
5624 
5625 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
5626 		u_int16_t flags, period, offset;
5627 		int get;
5628 
5629 		if (sdp->isp_devparam[tgt].dev_enable == 0) {
5630 			sdp->isp_devparam[tgt].dev_update = 0;
5631 			sdp->isp_devparam[tgt].dev_refresh = 0;
5632 			isp_prt(isp, ISP_LOGDEBUG0,
5633 	 		    "skipping target %d bus %d update", tgt, bus);
5634 			continue;
5635 		}
5636 		/*
5637 		 * If the goal is to update the status of the device,
5638 		 * take what's in goal_flags and try and set the device
5639 		 * toward that. Otherwise, if we're just refreshing the
5640 		 * current device state, get the current parameters.
5641 		 */
5642 
5643 		/*
5644 		 * Refresh overrides set
5645 		 */
5646 		if (sdp->isp_devparam[tgt].dev_refresh) {
5647 			mbs.param[0] = MBOX_GET_TARGET_PARAMS;
5648 			sdp->isp_devparam[tgt].dev_refresh = 0;
5649 			get = 1;
5650 		} else if (sdp->isp_devparam[tgt].dev_update) {
5651 			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
5652 			/*
5653 			 * Make sure goal_flags has "Renegotiate on Error"
5654 			 * on and "Freeze Queue on Error" off.
5655 			 */
5656 			sdp->isp_devparam[tgt].goal_flags |= DPARM_RENEG;
5657 			sdp->isp_devparam[tgt].goal_flags &= ~DPARM_QFRZ;
5658 
5659 			mbs.param[2] = sdp->isp_devparam[tgt].goal_flags;
5660 
5661 			/*
5662 			 * Insist that PARITY must be enabled
5663 			 * if SYNC or WIDE is enabled.
5664 			 */
5665 			if ((mbs.param[2] & (DPARM_SYNC|DPARM_WIDE)) != 0) {
5666 				mbs.param[2] |= DPARM_PARITY;
5667 			}
5668 
5669 			if ((mbs.param[2] & DPARM_SYNC) == 0) {
5670 				mbs.param[3] = 0;
5671 			} else {
5672 				mbs.param[3] =
5673 				    (sdp->isp_devparam[tgt].goal_offset << 8) |
5674 				    (sdp->isp_devparam[tgt].goal_period);
5675 			}
5676 			/*
5677 			 * A command completion later that has
5678 			 * RQSTF_NEGOTIATION set can cause
5679 			 * the dev_refresh/announce cycle also.
5680 			 *
5681 			 * Note: It is really important to update our current
5682 			 * flags with at least the state of TAG capabilities-
5683 			 * otherwise we might try and send a tagged command
5684 			 * when we have it all turned off. So change it here
5685 			 * to say that current already matches goal.
5686 			 */
5687 			sdp->isp_devparam[tgt].actv_flags &= ~DPARM_TQING;
5688 			sdp->isp_devparam[tgt].actv_flags |=
5689 			    (sdp->isp_devparam[tgt].goal_flags & DPARM_TQING);
5690 			isp_prt(isp, ISP_LOGDEBUG0,
5691 			    "bus %d set tgt %d flags 0x%x off 0x%x period 0x%x",
5692 			    bus, tgt, mbs.param[2], mbs.param[3] >> 8,
5693 			    mbs.param[3] & 0xff);
5694 			sdp->isp_devparam[tgt].dev_update = 0;
5695 			sdp->isp_devparam[tgt].dev_refresh = 1;
5696 			get = 0;
5697 		} else {
5698 			continue;
5699 		}
5700 		mbs.param[1] = (bus << 15) | (tgt << 8);
5701 		isp_mboxcmd(isp, &mbs, MBLOGALL);
5702 		if (get == 0) {
5703 			isp->isp_sendmarker |= (1 << bus);
5704 			continue;
5705 		}
5706 		flags = mbs.param[2];
5707 		period = mbs.param[3] & 0xff;
5708 		offset = mbs.param[3] >> 8;
5709 		sdp->isp_devparam[tgt].actv_flags = flags;
5710 		sdp->isp_devparam[tgt].actv_period = period;
5711 		sdp->isp_devparam[tgt].actv_offset = offset;
5712 		get = (bus << 16) | tgt;
5713 		(void) isp_async(isp, ISPASYNC_NEW_TGT_PARAMS, &get);
5714 	}
5715 
5716 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
5717 		if (sdp->isp_devparam[tgt].dev_update ||
5718 		    sdp->isp_devparam[tgt].dev_refresh) {
5719 			isp->isp_update |= (1 << bus);
5720 			break;
5721 		}
5722 	}
5723 }
5724 
5725 #ifndef	DEFAULT_FRAMESIZE
5726 #define	DEFAULT_FRAMESIZE(isp)		ICB_DFLT_FRMLEN
5727 #endif
5728 #ifndef	DEFAULT_EXEC_THROTTLE
5729 #define	DEFAULT_EXEC_THROTTLE(isp)	ISP_EXEC_THROTTLE
5730 #endif
5731 
5732 static void
5733 isp_setdfltparm(struct ispsoftc *isp, int channel)
5734 {
5735 	int tgt;
5736 	mbreg_t mbs;
5737 	sdparam *sdp;
5738 
5739 	MEMZERO(&mbs, sizeof (mbs));
5740 	if (IS_FC(isp)) {
5741 		fcparam *fcp = (fcparam *) isp->isp_param;
5742 		int nvfail;
5743 
5744 		fcp += channel;
5745 		if (fcp->isp_gotdparms) {
5746 			return;
5747 		}
5748 		fcp->isp_gotdparms = 1;
5749 		fcp->isp_maxfrmlen = DEFAULT_FRAMESIZE(isp);
5750 		fcp->isp_maxalloc = ICB_DFLT_ALLOC;
5751 		fcp->isp_execthrottle = DEFAULT_EXEC_THROTTLE(isp);
5752 		fcp->isp_retry_delay = ICB_DFLT_RDELAY;
5753 		fcp->isp_retry_count = ICB_DFLT_RCOUNT;
5754 		/* Platform specific.... */
5755 		fcp->isp_loopid = DEFAULT_LOOPID(isp);
5756 		fcp->isp_nodewwn = DEFAULT_NODEWWN(isp);
5757 		fcp->isp_portwwn = DEFAULT_PORTWWN(isp);
5758 		fcp->isp_fwoptions = 0;
5759 		fcp->isp_fwoptions |= ICBOPT_FAIRNESS;
5760 		fcp->isp_fwoptions |= ICBOPT_PDBCHANGE_AE;
5761 		fcp->isp_fwoptions |= ICBOPT_HARD_ADDRESS;
5762 #ifndef	ISP_NO_FASTPOST_FC
5763 		fcp->isp_fwoptions |= ICBOPT_FAST_POST;
5764 #endif
5765 		if (isp->isp_confopts & ISP_CFG_FULL_DUPLEX)
5766 			fcp->isp_fwoptions |= ICBOPT_FULL_DUPLEX;
5767 
5768 		/*
5769 		 * Make sure this is turned off now until we get
5770 		 * extended options from NVRAM
5771 		 */
5772 		fcp->isp_fwoptions &= ~ICBOPT_EXTENDED;
5773 
5774 		/*
5775 		 * Now try and read NVRAM unless told to not do so.
5776 		 * This will set fcparam's isp_nodewwn && isp_portwwn.
5777 		 */
5778 		if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
5779 		    	nvfail = isp_read_nvram(isp);
5780 			if (nvfail)
5781 				isp->isp_confopts |= ISP_CFG_NONVRAM;
5782 		} else {
5783 			nvfail = 1;
5784 		}
5785 		/*
5786 		 * Set node && port to override platform set defaults
5787 		 * unless the nvram read failed (or none was done),
5788 		 * or the platform code wants to use what had been
5789 		 * set in the defaults.
5790 		 */
5791 		if (nvfail) {
5792 			isp->isp_confopts |= ISP_CFG_OWNWWPN|ISP_CFG_OWNWWNN;
5793 		}
5794 		if (isp->isp_confopts & ISP_CFG_OWNWWNN) {
5795 			isp_prt(isp, ISP_LOGCONFIG, "Using Node WWN 0x%08x%08x",
5796 			    (u_int32_t) (DEFAULT_NODEWWN(isp) >> 32),
5797 			    (u_int32_t) (DEFAULT_NODEWWN(isp) & 0xffffffff));
5798 			ISP_NODEWWN(isp) = DEFAULT_NODEWWN(isp);
5799 		} else {
5800 			/*
5801 			 * We always start out with values derived
5802 			 * from NVRAM or our platform default.
5803 			 */
5804 			ISP_NODEWWN(isp) = fcp->isp_nodewwn;
5805 		}
5806 		if (isp->isp_confopts & ISP_CFG_OWNWWPN) {
5807 			isp_prt(isp, ISP_LOGCONFIG, "Using Port WWN 0x%08x%08x",
5808 			    (u_int32_t) (DEFAULT_PORTWWN(isp) >> 32),
5809 			    (u_int32_t) (DEFAULT_PORTWWN(isp) & 0xffffffff));
5810 			ISP_PORTWWN(isp) = DEFAULT_PORTWWN(isp);
5811 		} else {
5812 			/*
5813 			 * We always start out with values derived
5814 			 * from NVRAM or our platform default.
5815 			 */
5816 			ISP_PORTWWN(isp) = fcp->isp_portwwn;
5817 		}
5818 		return;
5819 	}
5820 
5821 	sdp = (sdparam *) isp->isp_param;
5822 	sdp += channel;
5823 
5824 	/*
5825 	 * Been there, done that, got the T-shirt...
5826 	 */
5827 	if (sdp->isp_gotdparms) {
5828 		return;
5829 	}
5830 	sdp->isp_gotdparms = 1;
5831 
5832 	/*
5833 	 * Establish some default parameters.
5834 	 */
5835 	sdp->isp_cmd_dma_burst_enable = 0;
5836 	sdp->isp_data_dma_burst_enabl = 1;
5837 	sdp->isp_fifo_threshold = 0;
5838 	sdp->isp_initiator_id = DEFAULT_IID(isp);
5839 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
5840 		sdp->isp_async_data_setup = 9;
5841 	} else {
5842 		sdp->isp_async_data_setup = 6;
5843 	}
5844 	sdp->isp_selection_timeout = 250;
5845 	sdp->isp_max_queue_depth = MAXISPREQUEST(isp);
5846 	sdp->isp_tag_aging = 8;
5847 	sdp->isp_bus_reset_delay = 5;
5848 	/*
5849 	 * Don't retry selection, busy or queue full automatically- reflect
5850 	 * these back to us.
5851 	 */
5852 	sdp->isp_retry_count = 0;
5853 	sdp->isp_retry_delay = 0;
5854 
5855 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
5856 		sdp->isp_devparam[tgt].exc_throttle = ISP_EXEC_THROTTLE;
5857 		sdp->isp_devparam[tgt].dev_enable = 1;
5858 	}
5859 
5860 	/*
5861 	 * If we've not been told to avoid reading NVRAM, try and read it.
5862 	 * If we're successful reading it, we can then return because NVRAM
5863 	 * will tell us what the desired settings are. Otherwise, we establish
5864 	 * some reasonable 'fake' nvram and goal defaults.
5865 	 */
5866 
5867 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
5868 		if (isp_read_nvram(isp) == 0) {
5869 			return;
5870 		}
5871 	}
5872 
5873 	/*
5874 	 * Now try and see whether we have specific values for them.
5875 	 */
5876 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
5877 		mbs.param[0] = MBOX_GET_ACT_NEG_STATE;
5878 		isp_mboxcmd(isp, &mbs, MBLOGNONE);
5879 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
5880 			sdp->isp_req_ack_active_neg = 1;
5881 			sdp->isp_data_line_active_neg = 1;
5882 		} else {
5883 			sdp->isp_req_ack_active_neg =
5884 			    (mbs.param[1+channel] >> 4) & 0x1;
5885 			sdp->isp_data_line_active_neg =
5886 			    (mbs.param[1+channel] >> 5) & 0x1;
5887 		}
5888 	}
5889 
5890 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc3,
5891 	    0, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
5892 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
5893 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
5894 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc3,
5895 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
5896 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
5897 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
5898 
5899 	/*
5900 	 * The trick here is to establish a default for the default (honk!)
5901 	 * state (goal_flags). Then try and get the current status from
5902 	 * the card to fill in the current state. We don't, in fact, set
5903 	 * the default to the SAFE default state- that's not the goal state.
5904 	 */
5905 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
5906 		u_int8_t off, per;
5907 		sdp->isp_devparam[tgt].actv_offset = 0;
5908 		sdp->isp_devparam[tgt].actv_period = 0;
5909 		sdp->isp_devparam[tgt].actv_flags = 0;
5910 
5911 		sdp->isp_devparam[tgt].goal_flags =
5912 		    sdp->isp_devparam[tgt].nvrm_flags = DPARM_DEFAULT;
5913 
5914 		/*
5915 		 * We default to Wide/Fast for versions less than a 1040
5916 		 * (unless it's SBus).
5917 		 */
5918 		if (IS_ULTRA3(isp)) {
5919 			off = ISP_80M_SYNCPARMS >> 8;
5920 			per = ISP_80M_SYNCPARMS & 0xff;
5921 		} else if (IS_ULTRA2(isp)) {
5922 			off = ISP_40M_SYNCPARMS >> 8;
5923 			per = ISP_40M_SYNCPARMS & 0xff;
5924 		} else if (IS_1240(isp)) {
5925 			off = ISP_20M_SYNCPARMS >> 8;
5926 			per = ISP_20M_SYNCPARMS & 0xff;
5927 		} else if ((isp->isp_bustype == ISP_BT_SBUS &&
5928 		    isp->isp_type < ISP_HA_SCSI_1020A) ||
5929 		    (isp->isp_bustype == ISP_BT_PCI &&
5930 		    isp->isp_type < ISP_HA_SCSI_1040) ||
5931 		    (isp->isp_clock && isp->isp_clock < 60) ||
5932 		    (sdp->isp_ultramode == 0)) {
5933 			off = ISP_10M_SYNCPARMS >> 8;
5934 			per = ISP_10M_SYNCPARMS & 0xff;
5935 		} else {
5936 			off = ISP_20M_SYNCPARMS_1040 >> 8;
5937 			per = ISP_20M_SYNCPARMS_1040 & 0xff;
5938 		}
5939 		sdp->isp_devparam[tgt].goal_offset =
5940 		    sdp->isp_devparam[tgt].nvrm_offset = off;
5941 		sdp->isp_devparam[tgt].goal_period =
5942 		    sdp->isp_devparam[tgt].nvrm_period = per;
5943 
5944 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc3,
5945 		    channel, tgt, sdp->isp_devparam[tgt].nvrm_flags,
5946 		    sdp->isp_devparam[tgt].nvrm_offset,
5947 		    sdp->isp_devparam[tgt].nvrm_period);
5948 	}
5949 }
5950 
5951 /*
5952  * Re-initialize the ISP and complete all orphaned commands
5953  * with a 'botched' notice. The reset/init routines should
5954  * not disturb an already active list of commands.
5955  *
5956  * Locks held prior to coming here.
5957  */
5958 
5959 void
5960 isp_reinit(struct ispsoftc *isp)
5961 {
5962 	XS_T *xs;
5963 	int i;
5964 
5965 	if (IS_FC(isp)) {
5966 		isp_mark_getpdb_all(isp);
5967 	}
5968 	isp_reset(isp);
5969 	if (isp->isp_state != ISP_RESETSTATE) {
5970 		isp_prt(isp, ISP_LOGERR, "isp_reinit cannot reset card");
5971 	} else if (isp->isp_role != ISP_ROLE_NONE) {
5972 		isp_init(isp);
5973 		if (isp->isp_state == ISP_INITSTATE) {
5974 			isp->isp_state = ISP_RUNSTATE;
5975 		}
5976 		if (isp->isp_state != ISP_RUNSTATE) {
5977 			isp_prt(isp, ISP_LOGERR,
5978 			    "isp_reinit cannot restart card");
5979 		}
5980 	}
5981 	isp->isp_nactive = 0;
5982 
5983 	for (i = 0; i < isp->isp_maxcmds; i++) {
5984 		u_int16_t handle;
5985 		xs = isp->isp_xflist[i];
5986 		if (xs == NULL) {
5987 			continue;
5988 		}
5989 		handle = isp_index_handle(i);
5990 		isp_destroy_handle(isp, handle);
5991 		if (XS_XFRLEN(xs)) {
5992 			ISP_DMAFREE(isp, xs, handle);
5993 			XS_RESID(xs) = XS_XFRLEN(xs);
5994 		} else {
5995 			XS_RESID(xs) = 0;
5996 		}
5997 		XS_SETERR(xs, HBA_BUSRESET);
5998 		isp_done(xs);
5999 	}
6000 }
6001 
6002 /*
6003  * NVRAM Routines
6004  */
6005 static int
6006 isp_read_nvram(struct ispsoftc *isp)
6007 {
6008 	int i, amt;
6009 	u_int8_t csum, minversion;
6010 	union {
6011 		u_int8_t _x[ISP2100_NVRAM_SIZE];
6012 		u_int16_t _s[ISP2100_NVRAM_SIZE>>1];
6013 	} _n;
6014 #define	nvram_data	_n._x
6015 #define	nvram_words	_n._s
6016 
6017 	if (IS_FC(isp)) {
6018 		amt = ISP2100_NVRAM_SIZE;
6019 		minversion = 1;
6020 	} else if (IS_ULTRA2(isp)) {
6021 		amt = ISP1080_NVRAM_SIZE;
6022 		minversion = 0;
6023 	} else {
6024 		amt = ISP_NVRAM_SIZE;
6025 		minversion = 2;
6026 	}
6027 
6028 	/*
6029 	 * Just read the first two words first to see if we have a valid
6030 	 * NVRAM to continue reading the rest with.
6031 	 */
6032 	for (i = 0; i < 2; i++) {
6033 		isp_rdnvram_word(isp, i, &nvram_words[i]);
6034 	}
6035 	if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
6036 	    nvram_data[2] != 'P') {
6037 		if (isp->isp_bustype != ISP_BT_SBUS) {
6038 			isp_prt(isp, ISP_LOGWARN, "invalid NVRAM header");
6039 			isp_prt(isp, ISP_LOGDEBUG0, "%x %x %x",
6040 			    nvram_data[0], nvram_data[1], nvram_data[2]);
6041 		}
6042 		return (-1);
6043 	}
6044 	for (i = 2; i < amt>>1; i++) {
6045 		isp_rdnvram_word(isp, i, &nvram_words[i]);
6046 	}
6047 	for (csum = 0, i = 0; i < amt; i++) {
6048 		csum += nvram_data[i];
6049 	}
6050 	if (csum != 0) {
6051 		isp_prt(isp, ISP_LOGWARN, "invalid NVRAM checksum");
6052 		return (-1);
6053 	}
6054 	if (ISP_NVRAM_VERSION(nvram_data) < minversion) {
6055 		isp_prt(isp, ISP_LOGWARN, "version %d NVRAM not understood",
6056 		    ISP_NVRAM_VERSION(nvram_data));
6057 		return (-1);
6058 	}
6059 
6060 	if (IS_ULTRA3(isp)) {
6061 		isp_parse_nvram_12160(isp, 0, nvram_data);
6062 		if (IS_12160(isp))
6063 			isp_parse_nvram_12160(isp, 1, nvram_data);
6064 	} else if (IS_1080(isp)) {
6065 		isp_parse_nvram_1080(isp, 0, nvram_data);
6066 	} else if (IS_1280(isp) || IS_1240(isp)) {
6067 		isp_parse_nvram_1080(isp, 0, nvram_data);
6068 		isp_parse_nvram_1080(isp, 1, nvram_data);
6069 	} else if (IS_SCSI(isp)) {
6070 		isp_parse_nvram_1020(isp, nvram_data);
6071 	} else {
6072 		isp_parse_nvram_2100(isp, nvram_data);
6073 	}
6074 	return (0);
6075 #undef	nvram_data
6076 #undef	nvram_words
6077 }
6078 
6079 static void
6080 isp_rdnvram_word(struct ispsoftc *isp, int wo, u_int16_t *rp)
6081 {
6082 	int i, cbits;
6083 	u_int16_t bit, rqst;
6084 
6085 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
6086 	USEC_DELAY(2);
6087 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
6088 	USEC_DELAY(2);
6089 
6090 	if (IS_FC(isp)) {
6091 		wo &= ((ISP2100_NVRAM_SIZE >> 1) - 1);
6092 		if (IS_2312(isp) && isp->isp_port) {
6093 			wo += 128;
6094 		}
6095 		rqst = (ISP_NVRAM_READ << 8) | wo;
6096 		cbits = 10;
6097 	} else if (IS_ULTRA2(isp)) {
6098 		wo &= ((ISP1080_NVRAM_SIZE >> 1) - 1);
6099 		rqst = (ISP_NVRAM_READ << 8) | wo;
6100 		cbits = 10;
6101 	} else {
6102 		wo &= ((ISP_NVRAM_SIZE >> 1) - 1);
6103 		rqst = (ISP_NVRAM_READ << 6) | wo;
6104 		cbits = 8;
6105 	}
6106 
6107 	/*
6108 	 * Clock the word select request out...
6109 	 */
6110 	for (i = cbits; i >= 0; i--) {
6111 		if ((rqst >> i) & 1) {
6112 			bit = BIU_NVRAM_SELECT | BIU_NVRAM_DATAOUT;
6113 		} else {
6114 			bit = BIU_NVRAM_SELECT;
6115 		}
6116 		ISP_WRITE(isp, BIU_NVRAM, bit);
6117 		USEC_DELAY(2);
6118 		ISP_WRITE(isp, BIU_NVRAM, bit | BIU_NVRAM_CLOCK);
6119 		USEC_DELAY(2);
6120 		ISP_WRITE(isp, BIU_NVRAM, bit);
6121 		USEC_DELAY(2);
6122 	}
6123 	/*
6124 	 * Now read the result back in (bits come back in MSB format).
6125 	 */
6126 	*rp = 0;
6127 	for (i = 0; i < 16; i++) {
6128 		u_int16_t rv;
6129 		*rp <<= 1;
6130 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
6131 		USEC_DELAY(2);
6132 		rv = ISP_READ(isp, BIU_NVRAM);
6133 		if (rv & BIU_NVRAM_DATAIN) {
6134 			*rp |= 1;
6135 		}
6136 		USEC_DELAY(2);
6137 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
6138 		USEC_DELAY(2);
6139 	}
6140 	ISP_WRITE(isp, BIU_NVRAM, 0);
6141 	USEC_DELAY(2);
6142 	ISP_SWIZZLE_NVRAM_WORD(isp, rp);
6143 }
6144 
6145 static void
6146 isp_parse_nvram_1020(struct ispsoftc *isp, u_int8_t *nvram_data)
6147 {
6148 	sdparam *sdp = (sdparam *) isp->isp_param;
6149 	int tgt;
6150 
6151 	sdp->isp_fifo_threshold =
6152 		ISP_NVRAM_FIFO_THRESHOLD(nvram_data) |
6153 		(ISP_NVRAM_FIFO_THRESHOLD_128(nvram_data) << 2);
6154 
6155 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
6156 		sdp->isp_initiator_id =
6157 			ISP_NVRAM_INITIATOR_ID(nvram_data);
6158 
6159 	sdp->isp_bus_reset_delay =
6160 		ISP_NVRAM_BUS_RESET_DELAY(nvram_data);
6161 
6162 	sdp->isp_retry_count =
6163 		ISP_NVRAM_BUS_RETRY_COUNT(nvram_data);
6164 
6165 	sdp->isp_retry_delay =
6166 		ISP_NVRAM_BUS_RETRY_DELAY(nvram_data);
6167 
6168 	sdp->isp_async_data_setup =
6169 		ISP_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data);
6170 
6171 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
6172 		if (sdp->isp_async_data_setup < 9) {
6173 			sdp->isp_async_data_setup = 9;
6174 		}
6175 	} else {
6176 		if (sdp->isp_async_data_setup != 6) {
6177 			sdp->isp_async_data_setup = 6;
6178 		}
6179 	}
6180 
6181 	sdp->isp_req_ack_active_neg =
6182 		ISP_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data);
6183 
6184 	sdp->isp_data_line_active_neg =
6185 		ISP_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data);
6186 
6187 	sdp->isp_data_dma_burst_enabl =
6188 		ISP_NVRAM_DATA_DMA_BURST_ENABLE(nvram_data);
6189 
6190 	sdp->isp_cmd_dma_burst_enable =
6191 		ISP_NVRAM_CMD_DMA_BURST_ENABLE(nvram_data);
6192 
6193 	sdp->isp_tag_aging =
6194 		ISP_NVRAM_TAG_AGE_LIMIT(nvram_data);
6195 
6196 	sdp->isp_selection_timeout =
6197 		ISP_NVRAM_SELECTION_TIMEOUT(nvram_data);
6198 
6199 	sdp->isp_max_queue_depth =
6200 		ISP_NVRAM_MAX_QUEUE_DEPTH(nvram_data);
6201 
6202 	sdp->isp_fast_mttr = ISP_NVRAM_FAST_MTTR_ENABLE(nvram_data);
6203 
6204 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
6205 	    0, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
6206 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
6207 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
6208 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
6209 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
6210 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
6211 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
6212 
6213 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
6214 		sdp->isp_devparam[tgt].dev_enable =
6215 			ISP_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt);
6216 		sdp->isp_devparam[tgt].exc_throttle =
6217 			ISP_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt);
6218 		sdp->isp_devparam[tgt].nvrm_offset =
6219 			ISP_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt);
6220 		sdp->isp_devparam[tgt].nvrm_period =
6221 			ISP_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt);
6222 		/*
6223 		 * We probably shouldn't lie about this, but it
6224 		 * it makes it much safer if we limit NVRAM values
6225 		 * to sanity.
6226 		 */
6227 		if (isp->isp_type < ISP_HA_SCSI_1040) {
6228 			/*
6229 			 * If we're not ultra, we can't possibly
6230 			 * be a shorter period than this.
6231 			 */
6232 			if (sdp->isp_devparam[tgt].nvrm_period < 0x19) {
6233 				sdp->isp_devparam[tgt].nvrm_period = 0x19;
6234 			}
6235 			if (sdp->isp_devparam[tgt].nvrm_offset > 0xc) {
6236 				sdp->isp_devparam[tgt].nvrm_offset = 0x0c;
6237 			}
6238 		} else {
6239 			if (sdp->isp_devparam[tgt].nvrm_offset > 0x8) {
6240 				sdp->isp_devparam[tgt].nvrm_offset = 0x8;
6241 			}
6242 		}
6243 		sdp->isp_devparam[tgt].nvrm_flags = 0;
6244 		if (ISP_NVRAM_TGT_RENEG(nvram_data, tgt))
6245 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
6246 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
6247 		if (ISP_NVRAM_TGT_TQING(nvram_data, tgt))
6248 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
6249 		if (ISP_NVRAM_TGT_SYNC(nvram_data, tgt))
6250 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
6251 		if (ISP_NVRAM_TGT_WIDE(nvram_data, tgt))
6252 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
6253 		if (ISP_NVRAM_TGT_PARITY(nvram_data, tgt))
6254 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
6255 		if (ISP_NVRAM_TGT_DISC(nvram_data, tgt))
6256 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
6257 		sdp->isp_devparam[tgt].actv_flags = 0; /* we don't know */
6258 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
6259 		    0, tgt, sdp->isp_devparam[tgt].nvrm_flags,
6260 		    sdp->isp_devparam[tgt].nvrm_offset,
6261 		    sdp->isp_devparam[tgt].nvrm_period);
6262 		sdp->isp_devparam[tgt].goal_offset =
6263 		    sdp->isp_devparam[tgt].nvrm_offset;
6264 		sdp->isp_devparam[tgt].goal_period =
6265 		    sdp->isp_devparam[tgt].nvrm_period;
6266 		sdp->isp_devparam[tgt].goal_flags =
6267 		    sdp->isp_devparam[tgt].nvrm_flags;
6268 	}
6269 }
6270 
6271 static void
6272 isp_parse_nvram_1080(struct ispsoftc *isp, int bus, u_int8_t *nvram_data)
6273 {
6274 	sdparam *sdp = (sdparam *) isp->isp_param;
6275 	int tgt;
6276 
6277 	sdp += bus;
6278 
6279 	sdp->isp_fifo_threshold =
6280 	    ISP1080_NVRAM_FIFO_THRESHOLD(nvram_data);
6281 
6282 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
6283 		sdp->isp_initiator_id =
6284 		    ISP1080_NVRAM_INITIATOR_ID(nvram_data, bus);
6285 
6286 	sdp->isp_bus_reset_delay =
6287 	    ISP1080_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
6288 
6289 	sdp->isp_retry_count =
6290 	    ISP1080_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
6291 
6292 	sdp->isp_retry_delay =
6293 	    ISP1080_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
6294 
6295 	sdp->isp_async_data_setup =
6296 	    ISP1080_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
6297 
6298 	sdp->isp_req_ack_active_neg =
6299 	    ISP1080_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
6300 
6301 	sdp->isp_data_line_active_neg =
6302 	    ISP1080_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
6303 
6304 	sdp->isp_data_dma_burst_enabl =
6305 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
6306 
6307 	sdp->isp_cmd_dma_burst_enable =
6308 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
6309 
6310 	sdp->isp_selection_timeout =
6311 	    ISP1080_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
6312 
6313 	sdp->isp_max_queue_depth =
6314 	     ISP1080_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
6315 
6316 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
6317 	    bus, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
6318 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
6319 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
6320 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
6321 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
6322 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
6323 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
6324 
6325 
6326 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
6327 		sdp->isp_devparam[tgt].dev_enable =
6328 		    ISP1080_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
6329 		sdp->isp_devparam[tgt].exc_throttle =
6330 			ISP1080_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
6331 		sdp->isp_devparam[tgt].nvrm_offset =
6332 			ISP1080_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
6333 		sdp->isp_devparam[tgt].nvrm_period =
6334 			ISP1080_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
6335 		sdp->isp_devparam[tgt].nvrm_flags = 0;
6336 		if (ISP1080_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
6337 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
6338 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
6339 		if (ISP1080_NVRAM_TGT_TQING(nvram_data, tgt, bus))
6340 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
6341 		if (ISP1080_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
6342 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
6343 		if (ISP1080_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
6344 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
6345 		if (ISP1080_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
6346 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
6347 		if (ISP1080_NVRAM_TGT_DISC(nvram_data, tgt, bus))
6348 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
6349 		sdp->isp_devparam[tgt].actv_flags = 0;
6350 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
6351 		    bus, tgt, sdp->isp_devparam[tgt].nvrm_flags,
6352 		    sdp->isp_devparam[tgt].nvrm_offset,
6353 		    sdp->isp_devparam[tgt].nvrm_period);
6354 		sdp->isp_devparam[tgt].goal_offset =
6355 		    sdp->isp_devparam[tgt].nvrm_offset;
6356 		sdp->isp_devparam[tgt].goal_period =
6357 		    sdp->isp_devparam[tgt].nvrm_period;
6358 		sdp->isp_devparam[tgt].goal_flags =
6359 		    sdp->isp_devparam[tgt].nvrm_flags;
6360 	}
6361 }
6362 
6363 static void
6364 isp_parse_nvram_12160(struct ispsoftc *isp, int bus, u_int8_t *nvram_data)
6365 {
6366 	sdparam *sdp = (sdparam *) isp->isp_param;
6367 	int tgt;
6368 
6369 	sdp += bus;
6370 
6371 	sdp->isp_fifo_threshold =
6372 	    ISP12160_NVRAM_FIFO_THRESHOLD(nvram_data);
6373 
6374 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
6375 		sdp->isp_initiator_id =
6376 		    ISP12160_NVRAM_INITIATOR_ID(nvram_data, bus);
6377 
6378 	sdp->isp_bus_reset_delay =
6379 	    ISP12160_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
6380 
6381 	sdp->isp_retry_count =
6382 	    ISP12160_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
6383 
6384 	sdp->isp_retry_delay =
6385 	    ISP12160_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
6386 
6387 	sdp->isp_async_data_setup =
6388 	    ISP12160_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
6389 
6390 	sdp->isp_req_ack_active_neg =
6391 	    ISP12160_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
6392 
6393 	sdp->isp_data_line_active_neg =
6394 	    ISP12160_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
6395 
6396 	sdp->isp_data_dma_burst_enabl =
6397 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
6398 
6399 	sdp->isp_cmd_dma_burst_enable =
6400 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
6401 
6402 	sdp->isp_selection_timeout =
6403 	    ISP12160_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
6404 
6405 	sdp->isp_max_queue_depth =
6406 	     ISP12160_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
6407 
6408 	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
6409 	    bus, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
6410 	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
6411 	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
6412 	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
6413 	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
6414 	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
6415 	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
6416 
6417 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
6418 		sdp->isp_devparam[tgt].dev_enable =
6419 		    ISP12160_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
6420 		sdp->isp_devparam[tgt].exc_throttle =
6421 			ISP12160_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
6422 		sdp->isp_devparam[tgt].nvrm_offset =
6423 			ISP12160_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
6424 		sdp->isp_devparam[tgt].nvrm_period =
6425 			ISP12160_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
6426 		sdp->isp_devparam[tgt].nvrm_flags = 0;
6427 		if (ISP12160_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
6428 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
6429 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
6430 		if (ISP12160_NVRAM_TGT_TQING(nvram_data, tgt, bus))
6431 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
6432 		if (ISP12160_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
6433 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
6434 		if (ISP12160_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
6435 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
6436 		if (ISP12160_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
6437 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
6438 		if (ISP12160_NVRAM_TGT_DISC(nvram_data, tgt, bus))
6439 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
6440 		sdp->isp_devparam[tgt].actv_flags = 0;
6441 		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
6442 		    bus, tgt, sdp->isp_devparam[tgt].nvrm_flags,
6443 		    sdp->isp_devparam[tgt].nvrm_offset,
6444 		    sdp->isp_devparam[tgt].nvrm_period);
6445 		sdp->isp_devparam[tgt].goal_offset =
6446 		    sdp->isp_devparam[tgt].nvrm_offset;
6447 		sdp->isp_devparam[tgt].goal_period =
6448 		    sdp->isp_devparam[tgt].nvrm_period;
6449 		sdp->isp_devparam[tgt].goal_flags =
6450 		    sdp->isp_devparam[tgt].nvrm_flags;
6451 	}
6452 }
6453 
6454 static void
6455 isp_parse_nvram_2100(struct ispsoftc *isp, u_int8_t *nvram_data)
6456 {
6457 	fcparam *fcp = (fcparam *) isp->isp_param;
6458 	u_int64_t wwn;
6459 
6460 	/*
6461 	 * There is NVRAM storage for both Port and Node entities-
6462 	 * but the Node entity appears to be unused on all the cards
6463 	 * I can find. However, we should account for this being set
6464 	 * at some point in the future.
6465 	 *
6466 	 * Qlogic WWNs have an NAA of 2, but usually nothing shows up in
6467 	 * bits 48..60. In the case of the 2202, it appears that they do
6468 	 * use bit 48 to distinguish between the two instances on the card.
6469 	 * The 2204, which I've never seen, *probably* extends this method.
6470 	 */
6471 	wwn = ISP2100_NVRAM_PORT_NAME(nvram_data);
6472 	if (wwn) {
6473 		isp_prt(isp, ISP_LOGCONFIG, "NVRAM Port WWN 0x%08x%08x",
6474 		    (u_int32_t) (wwn >> 32), (u_int32_t) (wwn & 0xffffffff));
6475 		if ((wwn >> 60) == 0) {
6476 			wwn |= (((u_int64_t) 2)<< 60);
6477 		}
6478 	}
6479 	fcp->isp_portwwn = wwn;
6480 	if (IS_2200(isp) || IS_23XX(isp)) {
6481 		wwn = ISP2200_NVRAM_NODE_NAME(nvram_data);
6482 		if (wwn) {
6483 			isp_prt(isp, ISP_LOGCONFIG, "NVRAM Node WWN 0x%08x%08x",
6484 			    (u_int32_t) (wwn >> 32),
6485 			    (u_int32_t) (wwn & 0xffffffff));
6486 			if ((wwn >> 60) == 0) {
6487 				wwn |= (((u_int64_t) 2)<< 60);
6488 			}
6489 		}
6490 	} else {
6491 		wwn &= ~((u_int64_t) 0xfff << 48);
6492 	}
6493 	fcp->isp_nodewwn = wwn;
6494 
6495 	/*
6496 	 * Make sure we have both Node and Port as non-zero values.
6497 	 */
6498 	if (fcp->isp_nodewwn != 0 && fcp->isp_portwwn == 0) {
6499 		fcp->isp_portwwn = fcp->isp_nodewwn;
6500 	} else if (fcp->isp_nodewwn == 0 && fcp->isp_portwwn != 0) {
6501 		fcp->isp_nodewwn = fcp->isp_portwwn;
6502 	}
6503 
6504 	/*
6505 	 * Make the Node and Port values sane if they're NAA == 2.
6506 	 * This means to clear bits 48..56 for the Node WWN and
6507 	 * make sure that there's some non-zero value in 48..56
6508 	 * for the Port WWN.
6509 	 */
6510 	if (fcp->isp_nodewwn && fcp->isp_portwwn) {
6511 		if ((fcp->isp_nodewwn & (((u_int64_t) 0xfff) << 48)) != 0 &&
6512 		    (fcp->isp_nodewwn >> 60) == 2) {
6513 			fcp->isp_nodewwn &= ~((u_int64_t) 0xfff << 48);
6514 		}
6515 		if ((fcp->isp_portwwn & (((u_int64_t) 0xfff) << 48)) == 0 &&
6516 		    (fcp->isp_portwwn >> 60) == 2) {
6517 			fcp->isp_portwwn |= ((u_int64_t) 1 << 56);
6518 		}
6519 	}
6520 
6521 	isp_prt(isp, ISP_LOGDEBUG0,
6522 	    "NVRAM: maxfrmlen %d execthrottle %d fwoptions 0x%x loopid %x",
6523 	    ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data),
6524 	    ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data),
6525 	    ISP2100_NVRAM_OPTIONS(nvram_data),
6526 	    ISP2100_NVRAM_HARDLOOPID(nvram_data));
6527 
6528 	fcp->isp_maxalloc =
6529 		ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data);
6530 	if ((isp->isp_confopts & ISP_CFG_OWNFSZ) == 0)
6531 		fcp->isp_maxfrmlen =
6532 			ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data);
6533 	fcp->isp_retry_delay =
6534 		ISP2100_NVRAM_RETRY_DELAY(nvram_data);
6535 	fcp->isp_retry_count =
6536 		ISP2100_NVRAM_RETRY_COUNT(nvram_data);
6537 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
6538 		fcp->isp_loopid =
6539 			ISP2100_NVRAM_HARDLOOPID(nvram_data);
6540 	if ((isp->isp_confopts & ISP_CFG_OWNEXCTHROTTLE) == 0)
6541 		fcp->isp_execthrottle =
6542 			ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data);
6543 	fcp->isp_fwoptions = ISP2100_NVRAM_OPTIONS(nvram_data);
6544 }
6545 
6546 #ifdef	ISP_FW_CRASH_DUMP
6547 static void isp2200_fw_dump(struct ispsoftc *);
6548 static void isp2300_fw_dump(struct ispsoftc *);
6549 
6550 static void
6551 isp2200_fw_dump(struct ispsoftc *isp)
6552 {
6553 	int i, j;
6554 	mbreg_t mbs;
6555 	u_int16_t *ptr;
6556 
6557 	MEMZERO(&mbs, sizeof (mbs));
6558 	ptr = FCPARAM(isp)->isp_dump_data;
6559 	if (ptr == NULL) {
6560 		isp_prt(isp, ISP_LOGERR,
6561 		   "No place to dump RISC registers and SRAM");
6562 		return;
6563 	}
6564 	if (*ptr++) {
6565 		isp_prt(isp, ISP_LOGERR,
6566 		   "dump area for RISC registers and SRAM already used");
6567 		return;
6568 	}
6569 	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
6570 	for (i = 0; i < 100; i++) {
6571 		USEC_DELAY(100);
6572 		if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6573 			break;
6574 		}
6575 	}
6576 	if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6577 		/*
6578 		 * PBIU Registers
6579 		 */
6580 		for (i = 0; i < 8; i++) {
6581 			*ptr++ = ISP_READ(isp, BIU_BLOCK + (i << 1));
6582 		}
6583 
6584 		/*
6585 		 * Mailbox Registers
6586 		 */
6587 		for (i = 0; i < 8; i++) {
6588 			*ptr++ = ISP_READ(isp, MBOX_BLOCK + (i << 1));
6589 		}
6590 
6591 		/*
6592 		 * DMA Registers
6593 		 */
6594 		for (i = 0; i < 48; i++) {
6595 			*ptr++ = ISP_READ(isp, DMA_BLOCK + 0x20 + (i << 1));
6596 		}
6597 
6598 		/*
6599 		 * RISC H/W Registers
6600 		 */
6601 		ISP_WRITE(isp, BIU2100_CSR, 0);
6602 		for (i = 0; i < 16; i++) {
6603 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0xA0 + (i << 1));
6604 		}
6605 
6606 		/*
6607 		 * RISC GP Registers
6608 		 */
6609 		for (j = 0; j < 8; j++) {
6610 			ISP_WRITE(isp, BIU_BLOCK + 0xA4, 0x2000 + (j << 8));
6611 			for (i = 0; i < 16; i++) {
6612 				*ptr++ =
6613 				    ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6614 			}
6615 		}
6616 
6617 		/*
6618 		 * Frame Buffer Hardware Registers
6619 		 */
6620 		ISP_WRITE(isp, BIU2100_CSR, 0x10);
6621 		for (i = 0; i < 16; i++) {
6622 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6623 		}
6624 
6625 		/*
6626 		 * Fibre Protocol Module 0 Hardware Registers
6627 		 */
6628 		ISP_WRITE(isp, BIU2100_CSR, 0x20);
6629 		for (i = 0; i < 64; i++) {
6630 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6631 		}
6632 
6633 		/*
6634 		 * Fibre Protocol Module 1 Hardware Registers
6635 		 */
6636 		ISP_WRITE(isp, BIU2100_CSR, 0x30);
6637 		for (i = 0; i < 64; i++) {
6638 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6639 		}
6640 	} else {
6641 		isp_prt(isp, ISP_LOGERR, "RISC Would Not Pause");
6642 		return;
6643 	}
6644 	isp_prt(isp, ISP_LOGALL,
6645 	   "isp_fw_dump: RISC registers dumped successfully");
6646 	ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
6647 	for (i = 0; i < 100; i++) {
6648 		USEC_DELAY(100);
6649 		if (ISP_READ(isp, OUTMAILBOX0) == 0) {
6650 			break;
6651 		}
6652 	}
6653 	if (ISP_READ(isp, OUTMAILBOX0) != 0) {
6654 		isp_prt(isp, ISP_LOGERR, "Board Would Not Reset");
6655 		return;
6656 	}
6657 	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
6658 	for (i = 0; i < 100; i++) {
6659 		USEC_DELAY(100);
6660 		if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6661 			break;
6662 		}
6663 	}
6664 	if ((ISP_READ(isp, HCCR) & HCCR_PAUSE) == 0) {
6665 		isp_prt(isp, ISP_LOGERR, "RISC Would Not Pause After Reset");
6666 		return;
6667 	}
6668 	ISP_WRITE(isp, RISC_EMB, 0xf2);
6669 	ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
6670 	for (i = 0; i < 100; i++) {
6671 		USEC_DELAY(100);
6672 		if ((ISP_READ(isp, HCCR) & HCCR_PAUSE) == 0) {
6673 			break;
6674 		}
6675 	}
6676 	ENABLE_INTS(isp);
6677 	mbs.param[0] = MBOX_READ_RAM_WORD;
6678 	mbs.param[1] = 0x1000;
6679 	isp->isp_mbxworkp = (void *) ptr;
6680 	isp->isp_mbxwrk0 = 0xefff;	/* continuation count */
6681 	isp->isp_mbxwrk1 = 0x1001;	/* next SRAM address */
6682 	isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
6683 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
6684 		isp_prt(isp, ISP_LOGWARN,
6685 		    "RAM DUMP FAILED @ WORD %x", isp->isp_mbxwrk1);
6686 		return;
6687 	}
6688 	ptr = isp->isp_mbxworkp;	/* finish fetch of final word */
6689 	*ptr++ = isp->isp_mboxtmp[2];
6690 	isp_prt(isp, ISP_LOGALL, "isp_fw_dump: SRAM dumped successfully");
6691 	FCPARAM(isp)->isp_dump_data[0] = isp->isp_type; /* now used */
6692 	(void) isp_async(isp, ISPASYNC_FW_DUMPED, 0);
6693 }
6694 
6695 static void
6696 isp2300_fw_dump(struct ispsoftc *isp)
6697 {
6698 	int i, j;
6699 	mbreg_t mbs;
6700 	u_int16_t *ptr;
6701 
6702 	MEMZERO(&mbs, sizeof (mbs));
6703 	ptr = FCPARAM(isp)->isp_dump_data;
6704 	if (ptr == NULL) {
6705 		isp_prt(isp, ISP_LOGERR,
6706 		   "No place to dump RISC registers and SRAM");
6707 		return;
6708 	}
6709 	if (*ptr++) {
6710 		isp_prt(isp, ISP_LOGERR,
6711 		   "dump area for RISC registers and SRAM already used");
6712 		return;
6713 	}
6714 	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
6715 	for (i = 0; i < 100; i++) {
6716 		USEC_DELAY(100);
6717 		if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6718 			break;
6719 		}
6720 	}
6721 	if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6722 		/*
6723 		 * PBIU registers
6724 		 */
6725 		for (i = 0; i < 8; i++) {
6726 			*ptr++ = ISP_READ(isp, BIU_BLOCK + (i << 1));
6727 		}
6728 
6729 		/*
6730 		 * ReqQ-RspQ-Risc2Host Status registers
6731 		 */
6732 		for (i = 0; i < 8; i++) {
6733 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x10 + (i << 1));
6734 		}
6735 
6736 		/*
6737 		 * Mailbox Registers
6738 		 */
6739 		for (i = 0; i < 32; i++) {
6740 			*ptr++ =
6741 			    ISP_READ(isp, PCI_MBOX_REGS2300_OFF + (i << 1));
6742 		}
6743 
6744 		/*
6745 		 * Auto Request Response DMA registers
6746 		 */
6747 		ISP_WRITE(isp, BIU2100_CSR, 0x40);
6748 		for (i = 0; i < 32; i++) {
6749 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6750 		}
6751 
6752 		/*
6753 		 * DMA registers
6754 		 */
6755 		ISP_WRITE(isp, BIU2100_CSR, 0x50);
6756 		for (i = 0; i < 48; i++) {
6757 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6758 		}
6759 
6760 		/*
6761 		 * RISC hardware registers
6762 		 */
6763 		ISP_WRITE(isp, BIU2100_CSR, 0);
6764 		for (i = 0; i < 16; i++) {
6765 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0xA0 + (i << 1));
6766 		}
6767 
6768 		/*
6769 		 * RISC GP? registers
6770 		 */
6771 		for (j = 0; j < 8; j++) {
6772 			ISP_WRITE(isp, BIU_BLOCK + 0xA4, 0x2000 + (j << 9));
6773 			for (i = 0; i < 16; i++) {
6774 				*ptr++ =
6775 				    ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6776 			}
6777 		}
6778 
6779 		/*
6780 		 * frame buffer hardware registers
6781 		 */
6782 		ISP_WRITE(isp, BIU2100_CSR, 0x10);
6783 		for (i = 0; i < 64; i++) {
6784 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6785 		}
6786 
6787 		/*
6788 		 * FPM B0 hardware registers
6789 		 */
6790 		ISP_WRITE(isp, BIU2100_CSR, 0x20);
6791 		for (i = 0; i < 64; i++) {
6792 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6793 		}
6794 
6795 		/*
6796 		 * FPM B1 hardware registers
6797 		 */
6798 		ISP_WRITE(isp, BIU2100_CSR, 0x30);
6799 		for (i = 0; i < 64; i++) {
6800 			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6801 		}
6802 	} else {
6803 		isp_prt(isp, ISP_LOGERR, "RISC Would Not Pause");
6804 		return;
6805 	}
6806 	isp_prt(isp, ISP_LOGALL,
6807 	   "isp_fw_dump: RISC registers dumped successfully");
6808 	ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
6809 	for (i = 0; i < 100; i++) {
6810 		USEC_DELAY(100);
6811 		if (ISP_READ(isp, OUTMAILBOX0) == 0) {
6812 			break;
6813 		}
6814 	}
6815 	if (ISP_READ(isp, OUTMAILBOX0) != 0) {
6816 		isp_prt(isp, ISP_LOGERR, "Board Would Not Reset");
6817 		return;
6818 	}
6819 	ENABLE_INTS(isp);
6820 	mbs.param[0] = MBOX_READ_RAM_WORD;
6821 	mbs.param[1] = 0x800;
6822 	isp->isp_mbxworkp = (void *) ptr;
6823 	isp->isp_mbxwrk0 = 0xf7ff;	/* continuation count */
6824 	isp->isp_mbxwrk1 = 0x801;	/* next SRAM address */
6825 	isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
6826 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
6827 		isp_prt(isp, ISP_LOGWARN,
6828 		    "RAM DUMP FAILED @ WORD %x", isp->isp_mbxwrk1);
6829 		return;
6830 	}
6831 	ptr = isp->isp_mbxworkp;	/* finish fetch of final word */
6832 	*ptr++ = isp->isp_mboxtmp[2];
6833 
6834 	/*
6835 	 * We don't have access to mailbox registers 8.. onward
6836 	 * in our 'common' device model- so we have to set it
6837 	 * here and hope it stays the same!
6838 	 */
6839 	ISP_WRITE(isp, PCI_MBOX_REGS2300_OFF + (8 << 1), 0x1);
6840 
6841 	mbs.param[0] = MBOX_READ_RAM_WORD_EXTENDED;
6842 	mbs.param[1] = 0;
6843 	isp->isp_mbxworkp = (void *) ptr;
6844 	isp->isp_mbxwrk0 = 0xffff;	/* continuation count */
6845 	isp->isp_mbxwrk1 = 0x1;		/* next SRAM address */
6846 	isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
6847 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
6848 		isp_prt(isp, ISP_LOGWARN,
6849 		    "RAM DUMP FAILED @ WORD %x", 0x10000 + isp->isp_mbxwrk1);
6850 		return;
6851 	}
6852 	ptr = isp->isp_mbxworkp;	/* finish final word */
6853 	*ptr++ = mbs.param[2];
6854 	isp_prt(isp, ISP_LOGALL, "isp_fw_dump: SRAM dumped successfully");
6855 	FCPARAM(isp)->isp_dump_data[0] = isp->isp_type; /* now used */
6856 	(void) isp_async(isp, ISPASYNC_FW_DUMPED, 0);
6857 }
6858 
6859 void
6860 isp_fw_dump(struct ispsoftc *isp)
6861 {
6862 	if (IS_2200(isp))
6863 		isp2200_fw_dump(isp);
6864 	else if (IS_23XX(isp))
6865 		isp2300_fw_dump(isp);
6866 }
6867 #endif
6868