xref: /freebsd/sys/dev/isp/isp.c (revision 9886bcdf9326929b650dc843802a25400f597365)
1 /* $FreeBSD$ */
2 /*
3  * Machine and OS Independent (well, as best as possible)
4  * code for the Qlogic ISP SCSI adapters.
5  *
6  * Copyright (c) 1997, 1998, 1999, 2000 by Matthew Jacob
7  * Feral Software
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice immediately at the beginning of the file, without modification,
15  *    this list of conditions, and the following disclaimer.
16  * 2. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * Inspiration and ideas about this driver are from Erik Moe's Linux driver
34  * (qlogicisp.c) and Dave Miller's SBus version of same (qlogicisp.c). Some
35  * ideas dredged from the Solaris driver.
36  */
37 
38 /*
39  * Include header file appropriate for platform we're building on.
40  */
41 
42 #ifdef	__NetBSD__
43 #include <dev/ic/isp_netbsd.h>
44 #endif
45 #ifdef	__FreeBSD__
46 #include <dev/isp/isp_freebsd.h>
47 #endif
48 #ifdef	__OpenBSD__
49 #include <dev/ic/isp_openbsd.h>
50 #endif
51 #ifdef	__linux__
52 #include "isp_linux.h"
53 #endif
54 #ifdef	__svr4__
55 #include "isp_solaris.h"
56 #endif
57 
58 /*
59  * General defines
60  */
61 
62 #define	MBOX_DELAY_COUNT	1000000 / 100
63 
64 /*
65  * Local static data
66  */
67 static char *warnlun =
68     "WARNING- cannot determine Expanded LUN capability- limiting to one LUN";
69 static char *portshift =
70     "Target %d Loop ID 0x%x (Port 0x%x) => Loop 0x%x (Port 0x%x)";
71 static char *portdup =
72     "Target %d duplicates Target %d- killing off both";
73 static char *retained =
74     "Retaining Loop ID 0x%x for Target %d (Port 0x%x)";
75 #ifdef	ISP2100_FABRIC
76 static char *lretained =
77     "Retained login of Target %d (Loop ID 0x%x) Port 0x%x";
78 static char *plogout =
79     "Logging out Target %d at Loop ID 0x%x (Port 0x%x)";
80 static char *plogierr =
81     "Command Error in PLOGI for Port 0x%x (0x%x)";
82 static char *nopdb =
83     "Could not get PDB for Device @ Port 0x%x";
84 static char *pdbmfail1 =
85     "PDB Loop ID info for Device @ Port 0x%x does not match up (0x%x)";
86 static char *pdbmfail2 =
87     "PDB Port info for Device @ Port 0x%x does not match up (0x%x)";
88 static char *ldumped =
89     "Target %d (Loop ID 0x%x) Port 0x%x dumped after login info mismatch";
90 #endif
91 static char *notresp =
92   "Not RESPONSE in RESPONSE Queue (type 0x%x) @ idx %d (next %d) nlooked %d";
93 static char *xact1 =
94     "HBA attempted queued transaction with disconnect not set for %d.%d.%d";
95 static char *xact2 =
96     "HBA attempted queued transaction to target routine %d on target %d bus %d";
97 static char *xact3 =
98     "HBA attempted queued cmd for %d.%d.%d when queueing disabled";
99 static char *pskip =
100     "SCSI phase skipped for target %d.%d.%d";
101 static char *topology =
102     "Loop ID %d, AL_PA 0x%x, Port ID 0x%x, Loop State 0x%x, Topology '%s'";
103 static char *finmsg =
104     "(%d.%d.%d): FIN dl%d resid%d STS 0x%x SKEY %c XS_ERR=0x%x";
105 /*
106  * Local function prototypes.
107  */
108 static int isp_parse_async __P((struct ispsoftc *, int));
109 static int isp_handle_other_response
110 __P((struct ispsoftc *, ispstatusreq_t *, u_int16_t *));
111 static void isp_parse_status
112 __P((struct ispsoftc *, ispstatusreq_t *, XS_T *));
113 static void isp_fastpost_complete __P((struct ispsoftc *, u_int32_t));
114 static void isp_scsi_init __P((struct ispsoftc *));
115 static void isp_scsi_channel_init __P((struct ispsoftc *, int));
116 static void isp_fibre_init __P((struct ispsoftc *));
117 static void isp_mark_getpdb_all __P((struct ispsoftc *));
118 static int isp_getpdb __P((struct ispsoftc *, int, isp_pdb_t *));
119 static u_int64_t isp_get_portname __P((struct ispsoftc *, int, int));
120 static int isp_fclink_test __P((struct ispsoftc *, int));
121 static char *isp2100_fw_statename __P((int));
122 static int isp_same_lportdb __P((struct lportdb *, struct lportdb *));
123 static int isp_pdb_sync __P((struct ispsoftc *, int));
124 #ifdef	ISP2100_FABRIC
125 static int isp_scan_fabric __P((struct ispsoftc *));
126 #endif
127 static void isp_fw_state __P((struct ispsoftc *));
128 static void isp_mboxcmd __P((struct ispsoftc *, mbreg_t *, int));
129 
130 static void isp_update __P((struct ispsoftc *));
131 static void isp_update_bus __P((struct ispsoftc *, int));
132 static void isp_setdfltparm __P((struct ispsoftc *, int));
133 static int isp_read_nvram __P((struct ispsoftc *));
134 static void isp_rdnvram_word __P((struct ispsoftc *, int, u_int16_t *));
135 static void isp_parse_nvram_1020 __P((struct ispsoftc *, u_int8_t *));
136 static void isp_parse_nvram_1080 __P((struct ispsoftc *, int, u_int8_t *));
137 static void isp_parse_nvram_12160 __P((struct ispsoftc *, int, u_int8_t *));
138 static void isp_parse_nvram_2100 __P((struct ispsoftc *, u_int8_t *));
139 
140 /*
141  * Reset Hardware.
142  *
143  * Hit the chip over the head, download new f/w if available and set it running.
144  *
145  * Locking done elsewhere.
146  */
147 void
148 isp_reset(isp)
149 	struct ispsoftc *isp;
150 {
151 	mbreg_t mbs;
152 	int loops, i, touched, dodnld = 1;
153 	char *revname;
154 
155 	isp->isp_state = ISP_NILSTATE;
156 
157 
158 	/*
159 	 * Basic types (SCSI, FibreChannel and PCI or SBus)
160 	 * have been set in the MD code. We figure out more
161 	 * here.
162 	 *
163 	 * After we've fired this chip up, zero out the conf1 register
164 	 * for SCSI adapters and do other settings for the 2100.
165 	 */
166 
167 	/*
168 	 * Get the current running firmware revision out of the
169 	 * chip before we hit it over the head (if this is our
170 	 * first time through). Note that we store this as the
171 	 * 'ROM' firmware revision- which it may not be. In any
172 	 * case, we don't really use this yet, but we may in
173 	 * the future.
174 	 */
175 	if ((touched = isp->isp_touched) == 0) {
176 		/*
177 		 * First see whether or not we're sitting in the ISP PROM.
178 		 * If we've just been reset, we'll have the string "ISP   "
179 		 * spread through outgoing mailbox registers 1-3.
180 		 */
181 		if (ISP_READ(isp, OUTMAILBOX1) != 0x4953 ||
182 		    ISP_READ(isp, OUTMAILBOX2) != 0x5020 ||
183 		    ISP_READ(isp, OUTMAILBOX3) != 0x2020) {
184 			/*
185 			 * Just in case it was paused...
186 			 */
187 			ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
188 			mbs.param[0] = MBOX_ABOUT_FIRMWARE;
189 			isp_mboxcmd(isp, &mbs, MBLOGNONE);
190 			/*
191 			 * This *shouldn't* fail.....
192 			 */
193 			if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
194 				isp->isp_romfw_rev[0] = mbs.param[1];
195 				isp->isp_romfw_rev[1] = mbs.param[2];
196 				isp->isp_romfw_rev[2] = mbs.param[3];
197 			}
198 		}
199 		isp->isp_touched = 1;
200 	}
201 
202 	DISABLE_INTS(isp);
203 
204 	/*
205 	 * Put the board into PAUSE mode (so we can read the SXP registers).
206 	 */
207 	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
208 
209 	if (IS_FC(isp)) {
210 		revname = "2X00";
211 		switch (isp->isp_type) {
212 		case ISP_HA_FC_2100:
213 			revname[1] = '1';
214 			break;
215 		case ISP_HA_FC_2200:
216 			revname[1] = '2';
217 			break;
218 		default:
219 			break;
220 		}
221 	} else if (IS_1240(isp)) {
222 		sdparam *sdp = isp->isp_param;
223 		revname = "1240";
224 		isp->isp_clock = 60;
225 		sdp->isp_ultramode = 1;
226 		sdp++;
227 		sdp->isp_ultramode = 1;
228 		/*
229 		 * XXX: Should probably do some bus sensing.
230 		 */
231 	} else if (IS_ULTRA2(isp)) {
232 		static char *m = "bus %d is in %s Mode";
233 		u_int16_t l;
234 		sdparam *sdp = isp->isp_param;
235 
236 		isp->isp_clock = 100;
237 
238 		if (IS_1280(isp))
239 			revname = "1280";
240 		else if (IS_1080(isp))
241 			revname = "1080";
242 		else if (IS_12160(isp))
243 			revname = "12160";
244 		else
245 			revname = "<UNKLVD>";
246 
247 		l = ISP_READ(isp, SXP_PINS_DIFF) & ISP1080_MODE_MASK;
248 		switch (l) {
249 		case ISP1080_LVD_MODE:
250 			sdp->isp_lvdmode = 1;
251 			isp_prt(isp, ISP_LOGCONFIG, m, 0, "LVD");
252 			break;
253 		case ISP1080_HVD_MODE:
254 			sdp->isp_diffmode = 1;
255 			isp_prt(isp, ISP_LOGCONFIG, m, 0, "Differential");
256 			break;
257 		case ISP1080_SE_MODE:
258 			sdp->isp_ultramode = 1;
259 			isp_prt(isp, ISP_LOGCONFIG, m, 0, "Single-Ended");
260 			break;
261 		default:
262 			isp_prt(isp, ISP_LOGERR,
263 			    "unknown mode on bus %d (0x%x)", 0, l);
264 			break;
265 		}
266 
267 		if (IS_DUALBUS(isp)) {
268 			sdp++;
269 			l = ISP_READ(isp, SXP_PINS_DIFF|SXP_BANK1_SELECT);
270 			l &= ISP1080_MODE_MASK;
271 			switch(l) {
272 			case ISP1080_LVD_MODE:
273 				sdp->isp_lvdmode = 1;
274 				isp_prt(isp, ISP_LOGCONFIG, m, 1, "LVD");
275 				break;
276 			case ISP1080_HVD_MODE:
277 				sdp->isp_diffmode = 1;
278 				isp_prt(isp, ISP_LOGCONFIG,
279 				    m, 1, "Differential");
280 				break;
281 			case ISP1080_SE_MODE:
282 				sdp->isp_ultramode = 1;
283 				isp_prt(isp, ISP_LOGCONFIG,
284 				    m, 1, "Single-Ended");
285 				break;
286 			default:
287 				isp_prt(isp, ISP_LOGERR,
288 				    "unknown mode on bus %d (0x%x)", 1, l);
289 				break;
290 			}
291 		}
292 	} else {
293 		sdparam *sdp = isp->isp_param;
294 		i = ISP_READ(isp, BIU_CONF0) & BIU_CONF0_HW_MASK;
295 		switch (i) {
296 		default:
297 			isp_prt(isp, ISP_LOGALL, "Unknown Chip Type 0x%x", i);
298 			/* FALLTHROUGH */
299 		case 1:
300 			revname = "1020";
301 			isp->isp_type = ISP_HA_SCSI_1020;
302 			isp->isp_clock = 40;
303 			break;
304 		case 2:
305 			/*
306 			 * Some 1020A chips are Ultra Capable, but don't
307 			 * run the clock rate up for that unless told to
308 			 * do so by the Ultra Capable bits being set.
309 			 */
310 			revname = "1020A";
311 			isp->isp_type = ISP_HA_SCSI_1020A;
312 			isp->isp_clock = 40;
313 			break;
314 		case 3:
315 			revname = "1040";
316 			isp->isp_type = ISP_HA_SCSI_1040;
317 			isp->isp_clock = 60;
318 			break;
319 		case 4:
320 			revname = "1040A";
321 			isp->isp_type = ISP_HA_SCSI_1040A;
322 			isp->isp_clock = 60;
323 			break;
324 		case 5:
325 			revname = "1040B";
326 			isp->isp_type = ISP_HA_SCSI_1040B;
327 			isp->isp_clock = 60;
328 			break;
329 		case 6:
330 			revname = "1040C";
331 			isp->isp_type = ISP_HA_SCSI_1040C;
332 			isp->isp_clock = 60;
333                         break;
334 		}
335 		/*
336 		 * Now, while we're at it, gather info about ultra
337 		 * and/or differential mode.
338 		 */
339 		if (ISP_READ(isp, SXP_PINS_DIFF) & SXP_PINS_DIFF_MODE) {
340 			isp_prt(isp, ISP_LOGCONFIG, "Differential Mode");
341 			sdp->isp_diffmode = 1;
342 		} else {
343 			sdp->isp_diffmode = 0;
344 		}
345 		i = ISP_READ(isp, RISC_PSR);
346 		if (isp->isp_bustype == ISP_BT_SBUS) {
347 			i &= RISC_PSR_SBUS_ULTRA;
348 		} else {
349 			i &= RISC_PSR_PCI_ULTRA;
350 		}
351 		if (i != 0) {
352 			isp_prt(isp, ISP_LOGCONFIG, "Ultra Mode Capable");
353 			sdp->isp_ultramode = 1;
354 			/*
355 			 * If we're in Ultra Mode, we have to be 60Mhz clock-
356 			 * even for the SBus version.
357 			 */
358 			isp->isp_clock = 60;
359 		} else {
360 			sdp->isp_ultramode = 0;
361 			/*
362 			 * Clock is known. Gronk.
363 			 */
364 		}
365 
366 		/*
367 		 * Machine dependent clock (if set) overrides
368 		 * our generic determinations.
369 		 */
370 		if (isp->isp_mdvec->dv_clock) {
371 			if (isp->isp_mdvec->dv_clock < isp->isp_clock) {
372 				isp->isp_clock = isp->isp_mdvec->dv_clock;
373 			}
374 		}
375 
376 	}
377 
378 	/*
379 	 * Clear instrumentation
380 	 */
381 	isp->isp_intcnt = isp->isp_intbogus = 0;
382 
383 	/*
384 	 * Do MD specific pre initialization
385 	 */
386 	ISP_RESET0(isp);
387 
388 again:
389 
390 	/*
391 	 * Hit the chip over the head with hammer,
392 	 * and give the ISP a chance to recover.
393 	 */
394 
395 	if (IS_SCSI(isp)) {
396 		ISP_WRITE(isp, BIU_ICR, BIU_ICR_SOFT_RESET);
397 		/*
398 		 * A slight delay...
399 		 */
400 		USEC_DELAY(100);
401 
402 		/*
403 		 * Clear data && control DMA engines.
404 		 */
405 		ISP_WRITE(isp, CDMA_CONTROL,
406 		    DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
407 		ISP_WRITE(isp, DDMA_CONTROL,
408 		    DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
409 
410 
411 	} else {
412 		ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
413 		/*
414 		 * A slight delay...
415 		 */
416 		USEC_DELAY(100);
417 
418 		/*
419 		 * Clear data && control DMA engines.
420 		 */
421 		ISP_WRITE(isp, CDMA2100_CONTROL,
422 			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
423 		ISP_WRITE(isp, TDMA2100_CONTROL,
424 			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
425 		ISP_WRITE(isp, RDMA2100_CONTROL,
426 			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
427 	}
428 
429 	/*
430 	 * Wait for ISP to be ready to go...
431 	 */
432 	loops = MBOX_DELAY_COUNT;
433 	for (;;) {
434 		if (IS_SCSI(isp)) {
435 			if (!(ISP_READ(isp, BIU_ICR) & BIU_ICR_SOFT_RESET))
436 				break;
437 		} else {
438 			if (!(ISP_READ(isp, BIU2100_CSR) & BIU2100_SOFT_RESET))
439 				break;
440 		}
441 		USEC_DELAY(100);
442 		if (--loops < 0) {
443 			ISP_DUMPREGS(isp, "chip reset timed out");
444 			return;
445 		}
446 	}
447 
448 	/*
449 	 * After we've fired this chip up, zero out the conf1 register
450 	 * for SCSI adapters and other settings for the 2100.
451 	 */
452 
453 	if (IS_SCSI(isp)) {
454 		ISP_WRITE(isp, BIU_CONF1, 0);
455 	} else {
456 		ISP_WRITE(isp, BIU2100_CSR, 0);
457 	}
458 
459 	/*
460 	 * Reset RISC Processor
461 	 */
462 	ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
463 	USEC_DELAY(100);
464 
465 	/*
466 	 * Establish some initial burst rate stuff.
467 	 * (only for the 1XX0 boards). This really should
468 	 * be done later after fetching from NVRAM.
469 	 */
470 	if (IS_SCSI(isp)) {
471 		u_int16_t tmp = isp->isp_mdvec->dv_conf1;
472 		/*
473 		 * Busted FIFO. Turn off all but burst enables.
474 		 */
475 		if (isp->isp_type == ISP_HA_SCSI_1040A) {
476 			tmp &= BIU_BURST_ENABLE;
477 		}
478 		ISP_SETBITS(isp, BIU_CONF1, tmp);
479 		if (tmp & BIU_BURST_ENABLE) {
480 			ISP_SETBITS(isp, CDMA_CONF, DMA_ENABLE_BURST);
481 			ISP_SETBITS(isp, DDMA_CONF, DMA_ENABLE_BURST);
482 		}
483 #ifdef	PTI_CARDS
484 		if (((sdparam *) isp->isp_param)->isp_ultramode) {
485 			while (ISP_READ(isp, RISC_MTR) != 0x1313) {
486 				ISP_WRITE(isp, RISC_MTR, 0x1313);
487 				ISP_WRITE(isp, HCCR, HCCR_CMD_STEP);
488 			}
489 		} else {
490 			ISP_WRITE(isp, RISC_MTR, 0x1212);
491 		}
492 		/*
493 		 * PTI specific register
494 		 */
495 		ISP_WRITE(isp, RISC_EMB, DUAL_BANK)
496 #else
497 		ISP_WRITE(isp, RISC_MTR, 0x1212);
498 #endif
499 	} else {
500 		ISP_WRITE(isp, RISC_MTR2100, 0x1212);
501 	}
502 
503 	ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE); /* release paused processor */
504 
505 	/*
506 	 * Do MD specific post initialization
507 	 */
508 	ISP_RESET1(isp);
509 
510 	/*
511 	 * Wait for everything to finish firing up...
512 	 */
513 	loops = MBOX_DELAY_COUNT;
514 	while (ISP_READ(isp, OUTMAILBOX0) == MBOX_BUSY) {
515 		USEC_DELAY(100);
516 		if (--loops < 0) {
517 			isp_prt(isp, ISP_LOGERR,
518 			    "MBOX_BUSY never cleared on reset");
519 			return;
520 		}
521 	}
522 
523 	/*
524 	 * Up until this point we've done everything by just reading or
525 	 * setting registers. From this point on we rely on at least *some*
526 	 * kind of firmware running in the card.
527 	 */
528 
529 	/*
530 	 * Do some sanity checking.
531 	 */
532 	mbs.param[0] = MBOX_NO_OP;
533 	isp_mboxcmd(isp, &mbs, MBLOGALL);
534 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
535 		return;
536 	}
537 
538 	if (IS_SCSI(isp)) {
539 		mbs.param[0] = MBOX_MAILBOX_REG_TEST;
540 		mbs.param[1] = 0xdead;
541 		mbs.param[2] = 0xbeef;
542 		mbs.param[3] = 0xffff;
543 		mbs.param[4] = 0x1111;
544 		mbs.param[5] = 0xa5a5;
545 		isp_mboxcmd(isp, &mbs, MBLOGALL);
546 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
547 			return;
548 		}
549 		if (mbs.param[1] != 0xdead || mbs.param[2] != 0xbeef ||
550 		    mbs.param[3] != 0xffff || mbs.param[4] != 0x1111 ||
551 		    mbs.param[5] != 0xa5a5) {
552 			isp_prt(isp, ISP_LOGERR,
553 			    "Register Test Failed (0x%x 0x%x 0x%x 0x%x 0x%x)",
554 			    mbs.param[1], mbs.param[2], mbs.param[3],
555 			    mbs.param[4], mbs.param[5]);
556 			return;
557 		}
558 
559 	}
560 
561 	/*
562 	 * Download new Firmware, unless requested not to do so.
563 	 * This is made slightly trickier in some cases where the
564 	 * firmware of the ROM revision is newer than the revision
565 	 * compiled into the driver. So, where we used to compare
566 	 * versions of our f/w and the ROM f/w, now we just see
567 	 * whether we have f/w at all and whether a config flag
568 	 * has disabled our download.
569 	 */
570 	if ((isp->isp_mdvec->dv_ispfw == NULL) ||
571 	    (isp->isp_confopts & ISP_CFG_NORELOAD)) {
572 		dodnld = 0;
573 	}
574 
575 	if (dodnld) {
576 		u_int16_t fwlen  = isp->isp_mdvec->dv_ispfw[3];
577 		for (i = 0; i < fwlen; i++) {
578 			mbs.param[0] = MBOX_WRITE_RAM_WORD;
579 			mbs.param[1] = ISP_CODE_ORG + i;
580 			mbs.param[2] = isp->isp_mdvec->dv_ispfw[i];
581 			isp_mboxcmd(isp, &mbs, MBLOGNONE);
582 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
583 				isp_prt(isp, ISP_LOGERR,
584 				    "F/W download failed at word %d", i);
585 				dodnld = 0;
586 				goto again;
587 			}
588 		}
589 
590 		/*
591 		 * Verify that it downloaded correctly.
592 		 */
593 		mbs.param[0] = MBOX_VERIFY_CHECKSUM;
594 		mbs.param[1] = ISP_CODE_ORG;
595 		isp_mboxcmd(isp, &mbs, MBLOGNONE);
596 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
597 			isp_prt(isp, ISP_LOGERR, "Ram Checksum Failure");
598 			return;
599 		}
600 		isp->isp_loaded_fw = 1;
601 	} else {
602 		isp->isp_loaded_fw = 0;
603 		isp_prt(isp, ISP_LOGDEBUG2, "skipping f/w download");
604 	}
605 
606 	/*
607 	 * Now start it rolling.
608 	 *
609 	 * If we didn't actually download f/w,
610 	 * we still need to (re)start it.
611 	 */
612 
613 	mbs.param[0] = MBOX_EXEC_FIRMWARE;
614 	mbs.param[1] = ISP_CODE_ORG;
615 	isp_mboxcmd(isp, &mbs, MBLOGNONE);
616 	/* give it a chance to start */
617 	USEC_SLEEP(isp, 500);
618 
619 	if (IS_SCSI(isp)) {
620 		/*
621 		 * Set CLOCK RATE, but only if asked to.
622 		 */
623 		if (isp->isp_clock) {
624 			mbs.param[0] = MBOX_SET_CLOCK_RATE;
625 			mbs.param[1] = isp->isp_clock;
626 			isp_mboxcmd(isp, &mbs, MBLOGALL);
627 			/* we will try not to care if this fails */
628 		}
629 	}
630 
631 	mbs.param[0] = MBOX_ABOUT_FIRMWARE;
632 	isp_mboxcmd(isp, &mbs, MBLOGALL);
633 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
634 		return;
635 	}
636 	isp_prt(isp, ISP_LOGCONFIG,
637 	    "Board Revision %s, %s F/W Revision %d.%d.%d", revname,
638 	    dodnld? "loaded" : "resident", mbs.param[1], mbs.param[2],
639 	    mbs.param[3]);
640 	if (IS_FC(isp)) {
641 		if (ISP_READ(isp, BIU2100_CSR) & BIU2100_PCI64) {
642 			isp_prt(isp, ISP_LOGCONFIG,
643 			    "Installed in 64-Bit PCI slot");
644 		}
645 	}
646 
647 	isp->isp_fwrev[0] = mbs.param[1];
648 	isp->isp_fwrev[1] = mbs.param[2];
649 	isp->isp_fwrev[2] = mbs.param[3];
650 	if (isp->isp_romfw_rev[0] || isp->isp_romfw_rev[1] ||
651 	    isp->isp_romfw_rev[2]) {
652 		isp_prt(isp, ISP_LOGCONFIG, "Last F/W revision was %d.%d.%d",
653 		    isp->isp_romfw_rev[0], isp->isp_romfw_rev[1],
654 		    isp->isp_romfw_rev[2]);
655 	}
656 
657 	mbs.param[0] = MBOX_GET_FIRMWARE_STATUS;
658 	isp_mboxcmd(isp, &mbs, MBLOGALL);
659 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
660 		return;
661 	}
662 	isp->isp_maxcmds = mbs.param[2];
663 	isp_prt(isp, ISP_LOGINFO,
664 	    "%d max I/O commands supported", mbs.param[2]);
665 	isp_fw_state(isp);
666 
667 	/*
668 	 * Set up DMA for the request and result mailboxes.
669 	 */
670 	if (ISP_MBOXDMASETUP(isp) != 0) {
671 		isp_prt(isp, ISP_LOGERR, "Cannot setup DMA");
672 		return;
673 	}
674 	isp->isp_state = ISP_RESETSTATE;
675 
676 	/*
677 	 * Okay- now that we have new firmware running, we now (re)set our
678 	 * notion of how many luns we support. This is somewhat tricky because
679 	 * if we haven't loaded firmware, we don't have an easy way of telling
680 	 * how many luns we support.
681 	 *
682 	 * We'll make a simplifying assumption- if we loaded firmware, we
683 	 * are running with expanded lun firmware, otherwise not.
684 	 *
685 	 * Expanded lun firmware gives you 32 luns for SCSI cards and
686 	 * 65536 luns for Fibre Channel cards.
687 	 *
688 	 * Because the lun is in a a different position in the Request Queue
689 	 * Entry structure for Fibre Channel with expanded lun firmware, we
690 	 * can only support one lun (lun zero) when we don't know what kind
691 	 * of firmware we're running.
692 	 *
693 	 * Note that we only do this once (the first time thru isp_reset)
694 	 * because we may be called again after firmware has been loaded once
695 	 * and released.
696 	 */
697 	if (touched == 0) {
698 		if (dodnld) {
699 			if (IS_SCSI(isp)) {
700 				isp->isp_maxluns = 32;
701 			} else {
702 				isp->isp_maxluns = 65536;
703 			}
704 		} else {
705 			if (IS_SCSI(isp)) {
706 				isp->isp_maxluns = 8;
707 			} else {
708 				isp_prt(isp, ISP_LOGALL, warnlun);
709 				isp->isp_maxluns = 1;
710 			}
711 		}
712 	}
713 }
714 
715 /*
716  * Initialize Parameters of Hardware to a known state.
717  *
718  * Locks are held before coming here.
719  */
720 
721 void
722 isp_init(isp)
723 	struct ispsoftc *isp;
724 {
725 	/*
726 	 * Must do this first to get defaults established.
727 	 */
728 	isp_setdfltparm(isp, 0);
729 	if (IS_DUALBUS(isp)) {
730 		isp_setdfltparm(isp, 1);
731 	}
732 	if (IS_FC(isp)) {
733 		isp_fibre_init(isp);
734 	} else {
735 		isp_scsi_init(isp);
736 	}
737 }
738 
739 static void
740 isp_scsi_init(isp)
741 	struct ispsoftc *isp;
742 {
743 	sdparam *sdp_chan0, *sdp_chan1;
744 	mbreg_t mbs;
745 
746 	sdp_chan0 = isp->isp_param;
747 	sdp_chan1 = sdp_chan0;
748 	if (IS_DUALBUS(isp)) {
749 		sdp_chan1++;
750 	}
751 
752 	/* First do overall per-card settings. */
753 
754 	/*
755 	 * If we have fast memory timing enabled, turn it on.
756 	 */
757 	if (sdp_chan0->isp_fast_mttr) {
758 		ISP_WRITE(isp, RISC_MTR, 0x1313);
759 	}
760 
761 	/*
762 	 * Set Retry Delay and Count.
763 	 * You set both channels at the same time.
764 	 */
765 	mbs.param[0] = MBOX_SET_RETRY_COUNT;
766 	mbs.param[1] = sdp_chan0->isp_retry_count;
767 	mbs.param[2] = sdp_chan0->isp_retry_delay;
768 	mbs.param[6] = sdp_chan1->isp_retry_count;
769 	mbs.param[7] = sdp_chan1->isp_retry_delay;
770 
771 	isp_mboxcmd(isp, &mbs, MBLOGALL);
772 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
773 		return;
774 	}
775 
776 	/*
777 	 * Set ASYNC DATA SETUP time. This is very important.
778 	 */
779 	mbs.param[0] = MBOX_SET_ASYNC_DATA_SETUP_TIME;
780 	mbs.param[1] = sdp_chan0->isp_async_data_setup;
781 	mbs.param[2] = sdp_chan1->isp_async_data_setup;
782 	isp_mboxcmd(isp, &mbs, MBLOGALL);
783 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
784 		return;
785 	}
786 
787 	/*
788 	 * Set ACTIVE Negation State.
789 	 */
790 	mbs.param[0] = MBOX_SET_ACT_NEG_STATE;
791 	mbs.param[1] =
792 	    (sdp_chan0->isp_req_ack_active_neg << 4) |
793 	    (sdp_chan0->isp_data_line_active_neg << 5);
794 	mbs.param[2] =
795 	    (sdp_chan1->isp_req_ack_active_neg << 4) |
796 	    (sdp_chan1->isp_data_line_active_neg << 5);
797 
798 	isp_mboxcmd(isp, &mbs, MBLOGNONE);
799 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
800 		isp_prt(isp, ISP_LOGERR,
801 		    "failed to set active negation state (%d,%d), (%d,%d)",
802 		    sdp_chan0->isp_req_ack_active_neg,
803 		    sdp_chan0->isp_data_line_active_neg,
804 		    sdp_chan1->isp_req_ack_active_neg,
805 		    sdp_chan1->isp_data_line_active_neg);
806 		/*
807 		 * But don't return.
808 		 */
809 	}
810 
811 	/*
812 	 * Set the Tag Aging limit
813 	 */
814 	mbs.param[0] = MBOX_SET_TAG_AGE_LIMIT;
815 	mbs.param[1] = sdp_chan0->isp_tag_aging;
816 	mbs.param[2] = sdp_chan1->isp_tag_aging;
817 	isp_mboxcmd(isp, &mbs, MBLOGALL);
818 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
819 		isp_prt(isp, ISP_LOGERR, "failed to set tag age limit (%d,%d)",
820 		    sdp_chan0->isp_tag_aging, sdp_chan1->isp_tag_aging);
821 		return;
822 	}
823 
824 	/*
825 	 * Set selection timeout.
826 	 */
827 	mbs.param[0] = MBOX_SET_SELECT_TIMEOUT;
828 	mbs.param[1] = sdp_chan0->isp_selection_timeout;
829 	mbs.param[2] = sdp_chan1->isp_selection_timeout;
830 	isp_mboxcmd(isp, &mbs, MBLOGALL);
831 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
832 		return;
833 	}
834 
835 	/* now do per-channel settings */
836 	isp_scsi_channel_init(isp, 0);
837 	if (IS_DUALBUS(isp))
838 		isp_scsi_channel_init(isp, 1);
839 
840 	/*
841 	 * Now enable request/response queues
842 	 */
843 
844 	mbs.param[0] = MBOX_INIT_RES_QUEUE;
845 	mbs.param[1] = RESULT_QUEUE_LEN(isp);
846 	mbs.param[2] = DMA_MSW(isp->isp_result_dma);
847 	mbs.param[3] = DMA_LSW(isp->isp_result_dma);
848 	mbs.param[4] = 0;
849 	mbs.param[5] = 0;
850 	isp_mboxcmd(isp, &mbs, MBLOGALL);
851 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
852 		return;
853 	}
854 	isp->isp_residx = mbs.param[5];
855 
856 	mbs.param[0] = MBOX_INIT_REQ_QUEUE;
857 	mbs.param[1] = RQUEST_QUEUE_LEN(isp);
858 	mbs.param[2] = DMA_MSW(isp->isp_rquest_dma);
859 	mbs.param[3] = DMA_LSW(isp->isp_rquest_dma);
860 	mbs.param[4] = 0;
861 	isp_mboxcmd(isp, &mbs, MBLOGALL);
862 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
863 		return;
864 	}
865 	isp->isp_reqidx = isp->isp_reqodx = mbs.param[4];
866 
867 	/*
868 	 * Turn on Fast Posting, LVD transitions
869 	 *
870 	 * Ultra2 F/W always has had fast posting (and LVD transitions)
871 	 *
872 	 * Ultra and older (i.e., SBus) cards may not. It's just safer
873 	 * to assume not for them.
874 	 */
875 
876 	mbs.param[0] = MBOX_SET_FW_FEATURES;
877 	mbs.param[1] = 0;
878 	if (IS_ULTRA2(isp))
879 		mbs.param[1] |= FW_FEATURE_LVD_NOTIFY;
880 	if (IS_ULTRA2(isp) || IS_1240(isp))
881 		mbs.param[1] |= FW_FEATURE_FAST_POST;
882 	if (mbs.param[1] != 0) {
883 		u_int16_t sfeat = mbs.param[1];
884 		isp_mboxcmd(isp, &mbs, MBLOGALL);
885 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
886 			isp_prt(isp, ISP_LOGINFO,
887 			    "Enabled FW features (0x%x)", sfeat);
888 		}
889 	}
890 
891 	/*
892 	 * Let the outer layers decide whether to issue a SCSI bus reset.
893 	 */
894 	isp->isp_state = ISP_INITSTATE;
895 }
896 
897 static void
898 isp_scsi_channel_init(isp, channel)
899 	struct ispsoftc *isp;
900 	int channel;
901 {
902 	sdparam *sdp;
903 	mbreg_t mbs;
904 	int tgt;
905 
906 	sdp = isp->isp_param;
907 	sdp += channel;
908 
909 	/*
910 	 * Set (possibly new) Initiator ID.
911 	 */
912 	mbs.param[0] = MBOX_SET_INIT_SCSI_ID;
913 	mbs.param[1] = (channel << 7) | sdp->isp_initiator_id;
914 	isp_mboxcmd(isp, &mbs, MBLOGALL);
915 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
916 		return;
917 	}
918 	isp_prt(isp, ISP_LOGINFO, "Initiator ID is %d", sdp->isp_initiator_id);
919 
920 
921 	/*
922 	 * Set current per-target parameters to a safe minimum.
923 	 */
924 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
925 		int lun;
926 		u_int16_t sdf;
927 
928 		if (sdp->isp_devparam[tgt].dev_enable == 0) {
929 			continue;
930 		}
931 		sdf = DPARM_SAFE_DFLT;
932 		/*
933 		 * It is not quite clear when this changed over so that
934 		 * we could force narrow and async for 1000/1020 cards,
935 		 * but assume that this is only the case for loaded
936 		 * firmware.
937 		 */
938 		if (isp->isp_loaded_fw) {
939 			sdf |= DPARM_NARROW | DPARM_ASYNC;
940 		}
941 		mbs.param[0] = MBOX_SET_TARGET_PARAMS;
942 		mbs.param[1] = (tgt << 8) | (channel << 15);
943 		mbs.param[2] = sdf;
944 		if ((sdf & DPARM_SYNC) == 0) {
945 			mbs.param[3] = 0;
946 		} else {
947 			mbs.param[3] =
948 			    (sdp->isp_devparam[tgt].sync_offset << 8) |
949 			    (sdp->isp_devparam[tgt].sync_period);
950 		}
951 		isp_mboxcmd(isp, &mbs, MBLOGALL);
952 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
953 			sdf = DPARM_SAFE_DFLT;
954 			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
955 			mbs.param[1] = (tgt << 8) | (channel << 15);
956 			mbs.param[2] = sdf;
957 			mbs.param[3] = 0;
958 			isp_mboxcmd(isp, &mbs, MBLOGALL);
959 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
960 				continue;
961 			}
962 		}
963 
964 		/*
965 		 * We don't update any information directly from the f/w
966 		 * because we need to run at least one command to cause a
967 		 * new state to be latched up. So, we just assume that we
968 		 * converge to the values we just had set.
969 		 *
970 		 * Ensure that we don't believe tagged queuing is enabled yet.
971 		 * It turns out that sometimes the ISP just ignores our
972 		 * attempts to set parameters for devices that it hasn't
973 		 * seen yet.
974 		 */
975 		sdp->isp_devparam[tgt].cur_dflags = sdf & ~DPARM_TQING;
976 		for (lun = 0; lun < (int) isp->isp_maxluns; lun++) {
977 			mbs.param[0] = MBOX_SET_DEV_QUEUE_PARAMS;
978 			mbs.param[1] = (channel << 15) | (tgt << 8) | lun;
979 			mbs.param[2] = sdp->isp_max_queue_depth;
980 			mbs.param[3] = sdp->isp_devparam[tgt].exc_throttle;
981 			isp_mboxcmd(isp, &mbs, MBLOGALL);
982 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
983 				break;
984 			}
985 		}
986 	}
987 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
988 		if (sdp->isp_devparam[tgt].dev_refresh) {
989 			isp->isp_sendmarker |= (1 << channel);
990 			isp->isp_update |= (1 << channel);
991 			break;
992 		}
993 	}
994 }
995 
996 /*
997  * Fibre Channel specific initialization.
998  *
999  * Locks are held before coming here.
1000  */
1001 static void
1002 isp_fibre_init(isp)
1003 	struct ispsoftc *isp;
1004 {
1005 	fcparam *fcp;
1006 	isp_icb_t *icbp;
1007 	mbreg_t mbs;
1008 	int loopid;
1009 	u_int64_t nwwn, pwwn;
1010 
1011 	fcp = isp->isp_param;
1012 
1013 	loopid = DEFAULT_LOOPID(isp);
1014 	icbp = (isp_icb_t *) fcp->isp_scratch;
1015 	MEMZERO(icbp, sizeof (*icbp));
1016 
1017 	icbp->icb_version = ICB_VERSION1;
1018 
1019 	/*
1020 	 * Firmware Options are either retrieved from NVRAM or
1021 	 * are patched elsewhere. We check them for sanity here
1022 	 * and make changes based on board revision, but otherwise
1023 	 * let others decide policy.
1024 	 */
1025 
1026 	/*
1027 	 * If this is a 2100 < revision 5, we have to turn off FAIRNESS.
1028 	 */
1029 	if ((isp->isp_type == ISP_HA_FC_2100) && isp->isp_revision < 5) {
1030 		fcp->isp_fwoptions &= ~ICBOPT_FAIRNESS;
1031 	}
1032 
1033 	/*
1034 	 * We have to use FULL LOGIN even though it resets the loop too much
1035 	 * because otherwise port database entries don't get updated after
1036 	 * a LIP- this is a known f/w bug for 2100 f/w less than 1.17.0.
1037 	 */
1038 	if (ISP_FW_REVX(isp->isp_fwrev) < ISP_FW_REV(1, 17, 0)) {
1039 		fcp->isp_fwoptions |= ICBOPT_FULL_LOGIN;
1040 	}
1041 
1042 	/*
1043 	 * Insist on Port Database Update Async notifications
1044 	 */
1045 	fcp->isp_fwoptions |= ICBOPT_PDBCHANGE_AE;
1046 
1047 	/*
1048 	 * We don't set ICBOPT_PORTNAME because we want our
1049 	 * Node Name && Port Names to be distinct.
1050 	 */
1051 
1052 	icbp->icb_fwoptions = fcp->isp_fwoptions;
1053 	icbp->icb_maxfrmlen = fcp->isp_maxfrmlen;
1054 	if (icbp->icb_maxfrmlen < ICB_MIN_FRMLEN ||
1055 	    icbp->icb_maxfrmlen > ICB_MAX_FRMLEN) {
1056 		isp_prt(isp, ISP_LOGERR,
1057 		    "bad frame length (%d) from NVRAM- using %d",
1058 		    fcp->isp_maxfrmlen, ICB_DFLT_FRMLEN);
1059 		icbp->icb_maxfrmlen = ICB_DFLT_FRMLEN;
1060 	}
1061 	icbp->icb_maxalloc = fcp->isp_maxalloc;
1062 	if (icbp->icb_maxalloc < 1) {
1063 		isp_prt(isp, ISP_LOGERR,
1064 		    "bad maximum allocation (%d)- using 16", fcp->isp_maxalloc);
1065 		icbp->icb_maxalloc = 16;
1066 	}
1067 	icbp->icb_execthrottle = fcp->isp_execthrottle;
1068 	if (icbp->icb_execthrottle < 1) {
1069 		isp_prt(isp, ISP_LOGERR,
1070 		    "bad execution throttle of %d- using 16",
1071 		    fcp->isp_execthrottle);
1072 		icbp->icb_execthrottle = ICB_DFLT_THROTTLE;
1073 	}
1074 	icbp->icb_retry_delay = fcp->isp_retry_delay;
1075 	icbp->icb_retry_count = fcp->isp_retry_count;
1076 	icbp->icb_hardaddr = loopid;
1077 	/*
1078 	 * Right now we just set extended options to prefer point-to-point
1079 	 * over loop based upon some soft config options.
1080 	 */
1081 	if (IS_2200(isp)) {
1082 		icbp->icb_fwoptions |= ICBOPT_EXTENDED;
1083 		/*
1084 		 * Prefer or force Point-To-Point instead Loop?
1085 		 */
1086 		switch(isp->isp_confopts & ISP_CFG_PORT_PREF) {
1087 		case ISP_CFG_NPORT:
1088 			icbp->icb_xfwoptions = ICBXOPT_PTP_2_LOOP;
1089 			break;
1090 		case ISP_CFG_NPORT_ONLY:
1091 			icbp->icb_xfwoptions = ICBXOPT_PTP_ONLY;
1092 			break;
1093 		case ISP_CFG_LPORT_ONLY:
1094 			icbp->icb_xfwoptions = ICBXOPT_LOOP_ONLY;
1095 			break;
1096 		default:
1097 			icbp->icb_xfwoptions = ICBXOPT_LOOP_2_PTP;
1098 			break;
1099 		}
1100 	}
1101 	icbp->icb_logintime = 60;	/* 60 second login timeout */
1102 
1103 	nwwn = ISP_NODEWWN(isp);
1104 	pwwn = ISP_PORTWWN(isp);
1105 	if (nwwn && pwwn) {
1106 		MAKE_NODE_NAME_FROM_WWN(icbp->icb_nodename, nwwn);
1107 		MAKE_NODE_NAME_FROM_WWN(icbp->icb_portname, pwwn);
1108 		isp_prt(isp, ISP_LOGDEBUG1,
1109 		    "Setting ICB Node 0x%08x%08x Port 0x%08x%08x",
1110 		    ((u_int32_t) (nwwn >> 32)),
1111 		    ((u_int32_t) (nwwn & 0xffffffff)),
1112 		    ((u_int32_t) (pwwn >> 32)),
1113 		    ((u_int32_t) (pwwn & 0xffffffff)));
1114 	} else {
1115 		isp_prt(isp, ISP_LOGDEBUG1, "Not using any WWNs");
1116 		fcp->isp_fwoptions &= ~(ICBOPT_USE_PORTNAME|ICBOPT_FULL_LOGIN);
1117 	}
1118 	icbp->icb_rqstqlen = RQUEST_QUEUE_LEN(isp);
1119 	icbp->icb_rsltqlen = RESULT_QUEUE_LEN(isp);
1120 	icbp->icb_rqstaddr[RQRSP_ADDR0015] = DMA_LSW(isp->isp_rquest_dma);
1121 	icbp->icb_rqstaddr[RQRSP_ADDR1631] = DMA_MSW(isp->isp_rquest_dma);
1122 	icbp->icb_respaddr[RQRSP_ADDR0015] = DMA_LSW(isp->isp_result_dma);
1123 	icbp->icb_respaddr[RQRSP_ADDR1631] = DMA_MSW(isp->isp_result_dma);
1124 	isp_prt(isp, ISP_LOGDEBUG1,
1125 	    "isp_fibre_init: fwoptions 0x%x", fcp->isp_fwoptions);
1126 	ISP_SWIZZLE_ICB(isp, icbp);
1127 
1128 	/*
1129 	 * Do this *before* initializing the firmware.
1130 	 */
1131 	isp_mark_getpdb_all(isp);
1132 	fcp->isp_fwstate = FW_CONFIG_WAIT;
1133 	fcp->isp_loopstate = LOOP_NIL;
1134 
1135 	mbs.param[0] = MBOX_INIT_FIRMWARE;
1136 	mbs.param[1] = 0;
1137 	mbs.param[2] = DMA_MSW(fcp->isp_scdma);
1138 	mbs.param[3] = DMA_LSW(fcp->isp_scdma);
1139 	mbs.param[4] = 0;
1140 	mbs.param[5] = 0;
1141 	mbs.param[6] = 0;
1142 	mbs.param[7] = 0;
1143 	isp_mboxcmd(isp, &mbs, MBLOGALL);
1144 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1145 		return;
1146 	}
1147 	isp->isp_reqidx = isp->isp_reqodx = 0;
1148 	isp->isp_residx = 0;
1149 	isp->isp_sendmarker = 1;
1150 
1151 	/*
1152 	 * Whatever happens, we're now committed to being here.
1153 	 */
1154 	isp->isp_state = ISP_INITSTATE;
1155 }
1156 
1157 /*
1158  * Fibre Channel Support- get the port database for the id.
1159  *
1160  * Locks are held before coming here. Return 0 if success,
1161  * else failure.
1162  */
1163 
1164 static void
1165 isp_mark_getpdb_all(isp)
1166 	struct ispsoftc *isp;
1167 {
1168 	fcparam *fcp = (fcparam *) isp->isp_param;
1169 	int i;
1170 	for (i = 0; i < MAX_FC_TARG; i++) {
1171 		fcp->portdb[i].valid = 0;
1172 	}
1173 }
1174 
1175 static int
1176 isp_getpdb(isp, id, pdbp)
1177 	struct ispsoftc *isp;
1178 	int id;
1179 	isp_pdb_t *pdbp;
1180 {
1181 	fcparam *fcp = (fcparam *) isp->isp_param;
1182 	mbreg_t mbs;
1183 
1184 	mbs.param[0] = MBOX_GET_PORT_DB;
1185 	mbs.param[1] = id << 8;
1186 	mbs.param[2] = DMA_MSW(fcp->isp_scdma);
1187 	mbs.param[3] = DMA_LSW(fcp->isp_scdma);
1188 	/*
1189 	 * Unneeded. For the 2100, except for initializing f/w, registers
1190 	 * 4/5 have to not be written to.
1191 	 *	mbs.param[4] = 0;
1192 	 *	mbs.param[5] = 0;
1193 	 *
1194 	 */
1195 	mbs.param[6] = 0;
1196 	mbs.param[7] = 0;
1197 	isp_mboxcmd(isp, &mbs, MBLOGALL & ~MBOX_COMMAND_PARAM_ERROR);
1198 	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1199 		ISP_UNSWIZZLE_AND_COPY_PDBP(isp, pdbp, fcp->isp_scratch);
1200 		return (0);
1201 	}
1202 	return (-1);
1203 }
1204 
1205 static u_int64_t
1206 isp_get_portname(isp, loopid, nodename)
1207 	struct ispsoftc *isp;
1208 	int loopid;
1209 	int nodename;
1210 {
1211 	u_int64_t wwn = 0;
1212 	mbreg_t mbs;
1213 
1214 	mbs.param[0] = MBOX_GET_PORT_NAME;
1215 	mbs.param[1] = loopid << 8;
1216 	if (nodename)
1217 		mbs.param[1] |= 1;
1218 	isp_mboxcmd(isp, &mbs, MBLOGALL & ~MBOX_COMMAND_PARAM_ERROR);
1219 	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1220 		wwn =
1221 		    (((u_int64_t)(mbs.param[2] & 0xff)) << 56) |
1222 		    (((u_int64_t)(mbs.param[2] >> 8))	<< 48) |
1223 		    (((u_int64_t)(mbs.param[3] & 0xff))	<< 40) |
1224 		    (((u_int64_t)(mbs.param[3] >> 8))	<< 32) |
1225 		    (((u_int64_t)(mbs.param[6] & 0xff))	<< 24) |
1226 		    (((u_int64_t)(mbs.param[6] >> 8))	<< 16) |
1227 		    (((u_int64_t)(mbs.param[7] & 0xff))	<<  8) |
1228 		    (((u_int64_t)(mbs.param[7] >> 8)));
1229 	}
1230 	return (wwn);
1231 }
1232 
1233 /*
1234  * Make sure we have good FC link and know our Loop ID.
1235  */
1236 
1237 static int
1238 isp_fclink_test(isp, usdelay)
1239 	struct ispsoftc *isp;
1240 	int usdelay;
1241 {
1242 	static char *toponames[] = {
1243 		"Private Loop",
1244 		"FL Port",
1245 		"N-Port to N-Port",
1246 		"F Port",
1247 		"F Port (no FLOGI_ACC response)"
1248 	};
1249 	mbreg_t mbs;
1250 	int count;
1251 	u_int8_t lwfs;
1252 	fcparam *fcp;
1253 #if	defined(ISP2100_FABRIC)
1254 	isp_pdb_t pdb;
1255 #endif
1256 	fcp = isp->isp_param;
1257 
1258 	/*
1259 	 * XXX: Here is where we would start a 'loop dead' timeout
1260 	 */
1261 
1262 	/*
1263 	 * Wait up to N microseconds for F/W to go to a ready state.
1264 	 */
1265 	lwfs = FW_CONFIG_WAIT;
1266 	count = 0;
1267 	while (count < usdelay) {
1268 		u_int64_t enano;
1269 		u_int32_t wrk;
1270 		NANOTIME_T hra, hrb;
1271 
1272 		GET_NANOTIME(&hra);
1273 		isp_fw_state(isp);
1274 		if (lwfs != fcp->isp_fwstate) {
1275 			isp_prt(isp, ISP_LOGINFO, "Firmware State <%s->%s>",
1276 			    isp2100_fw_statename((int)lwfs),
1277 			    isp2100_fw_statename((int)fcp->isp_fwstate));
1278 			lwfs = fcp->isp_fwstate;
1279 		}
1280 		if (fcp->isp_fwstate == FW_READY) {
1281 			break;
1282 		}
1283 		GET_NANOTIME(&hrb);
1284 
1285 		/*
1286 		 * Get the elapsed time in nanoseconds.
1287 		 * Always guaranteed to be non-zero.
1288 		 */
1289 		enano = NANOTIME_SUB(&hrb, &hra);
1290 
1291 		/*
1292 		 * If the elapsed time is less than 1 millisecond,
1293 		 * delay a period of time up to that millisecond of
1294 		 * waiting.
1295 		 */
1296 		isp_prt(isp, ISP_LOGDEBUG3, "usec%d: 0x%lx->0x%lx enano %u",
1297 		    count, (long) GET_NANOSEC(&hra), (long) GET_NANOSEC(&hrb),
1298 		    enano);
1299 
1300 		/*
1301 		 * This peculiar code is an attempt to try and avoid
1302 		 * invoking u_int64_t math support functions for some
1303 		 * platforms where linkage is a problem.
1304 		 */
1305 		if (enano < (1000 * 1000)) {
1306 			count += 1000;
1307 			enano = (1000 * 1000) - enano;
1308 			while (enano > (u_int64_t) 4000000000U) {
1309 				USEC_SLEEP(isp, 4000000);
1310 				enano -= (u_int64_t) 4000000000U;
1311 			}
1312 			wrk = enano;
1313 			USEC_SLEEP(isp, wrk/1000);
1314 		} else {
1315 			while (enano > (u_int64_t) 4000000000U) {
1316 				count += 4000000;
1317 				enano -= (u_int64_t) 4000000000U;
1318 			}
1319 			wrk = enano;
1320 			count += (wrk / 1000);
1321 		}
1322 	}
1323 
1324 	/*
1325 	 * If we haven't gone to 'ready' state, return.
1326 	 */
1327 	if (fcp->isp_fwstate != FW_READY) {
1328 		return (-1);
1329 	}
1330 
1331 	/*
1332 	 * Get our Loop ID (if possible). We really need to have it.
1333 	 */
1334 	mbs.param[0] = MBOX_GET_LOOP_ID;
1335 	isp_mboxcmd(isp, &mbs, MBLOGALL);
1336 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1337 		return (-1);
1338 	}
1339 	fcp->isp_loopid = mbs.param[1];
1340 	if (IS_2200(isp)) {
1341 		int topo = (int) mbs.param[6];
1342 		if (topo < TOPO_NL_PORT || topo > TOPO_PTP_STUB)
1343 			topo = TOPO_PTP_STUB;
1344 		fcp->isp_topo = topo;
1345 	} else {
1346 		fcp->isp_topo = TOPO_NL_PORT;
1347 	}
1348 	fcp->isp_alpa = mbs.param[2];
1349 
1350 #if	defined(ISP2100_FABRIC)
1351 	fcp->isp_onfabric = 0;
1352 	if (fcp->isp_topo != TOPO_N_PORT &&
1353 	    isp_getpdb(isp, FL_PORT_ID, &pdb) == 0) {
1354 		struct lportdb *lp;
1355 		if (IS_2100(isp)) {
1356 			fcp->isp_topo = TOPO_FL_PORT;
1357 		}
1358 		fcp->isp_portid = mbs.param[2] | (((int)mbs.param[3]) << 16);
1359 		fcp->isp_onfabric = 1;
1360 
1361 		/*
1362 		 * Save the Fabric controller's port database entry.
1363 		 */
1364 		lp = &fcp->portdb[FL_PORT_ID];
1365 		lp->node_wwn =
1366 		    (((u_int64_t)pdb.pdb_nodename[0]) << 56) |
1367 		    (((u_int64_t)pdb.pdb_nodename[1]) << 48) |
1368 		    (((u_int64_t)pdb.pdb_nodename[2]) << 40) |
1369 		    (((u_int64_t)pdb.pdb_nodename[3]) << 32) |
1370 		    (((u_int64_t)pdb.pdb_nodename[4]) << 24) |
1371 		    (((u_int64_t)pdb.pdb_nodename[5]) << 16) |
1372 		    (((u_int64_t)pdb.pdb_nodename[6]) <<  8) |
1373 		    (((u_int64_t)pdb.pdb_nodename[7]));
1374 		lp->port_wwn =
1375 		    (((u_int64_t)pdb.pdb_portname[0]) << 56) |
1376 		    (((u_int64_t)pdb.pdb_portname[1]) << 48) |
1377 		    (((u_int64_t)pdb.pdb_portname[2]) << 40) |
1378 		    (((u_int64_t)pdb.pdb_portname[3]) << 32) |
1379 		    (((u_int64_t)pdb.pdb_portname[4]) << 24) |
1380 		    (((u_int64_t)pdb.pdb_portname[5]) << 16) |
1381 		    (((u_int64_t)pdb.pdb_portname[6]) <<  8) |
1382 		    (((u_int64_t)pdb.pdb_portname[7]));
1383 		lp->roles =
1384 		    (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
1385 		lp->portid = BITS2WORD(pdb.pdb_portid_bits);
1386 		lp->loopid = pdb.pdb_loopid;
1387 		lp->loggedin = lp->valid = 1;
1388 #if	0
1389 		if (isp->isp_rfabric == 0) {
1390 			isp_i_register_fc4_type(isp);
1391 		}
1392 #endif
1393 	} else
1394 #endif
1395 	{
1396 		fcp->isp_portid = mbs.param[2];
1397 		fcp->isp_onfabric = 0;
1398 #if	0
1399 		isp->isp_rfabric = 0;
1400 #endif
1401 		fcp->portdb[FL_PORT_ID].valid = 0;
1402 	}
1403 
1404 	isp_prt(isp, ISP_LOGINFO, topology, fcp->isp_loopid, fcp->isp_alpa,
1405 	    fcp->isp_portid, fcp->isp_loopstate, toponames[fcp->isp_topo]);
1406 
1407 	return (0);
1408 }
1409 
1410 static char *
1411 isp2100_fw_statename(state)
1412 	int state;
1413 {
1414 	switch(state) {
1415 	case FW_CONFIG_WAIT:	return "Config Wait";
1416 	case FW_WAIT_AL_PA:	return "Waiting for AL_PA";
1417 	case FW_WAIT_LOGIN:	return "Wait Login";
1418 	case FW_READY:		return "Ready";
1419 	case FW_LOSS_OF_SYNC:	return "Loss Of Sync";
1420 	case FW_ERROR:		return "Error";
1421 	case FW_REINIT:		return "Re-Init";
1422 	case FW_NON_PART:	return "Nonparticipating";
1423 	default:		return "?????";
1424 	}
1425 }
1426 
1427 static int
1428 isp_same_lportdb(a, b)
1429 	struct lportdb *a, *b;
1430 {
1431 	/*
1432 	 * We decide two lports are the same if they have non-zero and
1433 	 * identical port WWNs and identical loop IDs.
1434 	 */
1435 
1436 	if (a->port_wwn == 0 || a->port_wwn != b->port_wwn ||
1437 	    a->loopid != b->loopid || a->roles != b->roles) {
1438 		return (0);
1439 	} else {
1440 		return (1);
1441 	}
1442 }
1443 
1444 /*
1445  * Synchronize our soft copy of the port database with what the f/w thinks
1446  * (with a view toward possibly for a specific target....)
1447  */
1448 
1449 static int
1450 isp_pdb_sync(isp, target)
1451 	struct ispsoftc *isp;
1452 	int target;
1453 {
1454 	struct lportdb *lp, *tport;
1455 	fcparam *fcp = isp->isp_param;
1456 	isp_pdb_t pdb;
1457 	int loopid, prange, lim;
1458 
1459 #ifdef	ISP2100_FABRIC
1460 	/*
1461 	 * XXX: If we do this *after* building up our local port database,
1462 	 * XXX: the commands simply don't work.
1463 	 */
1464 	/*
1465 	 * (Re)discover all fabric devices
1466 	 */
1467 	if (fcp->isp_onfabric)
1468 		(void) isp_scan_fabric(isp);
1469 #endif
1470 
1471 
1472 	switch (fcp->isp_topo) {
1473 	case TOPO_F_PORT:
1474 	case TOPO_PTP_STUB:
1475 		prange = 0;
1476 		break;
1477 	case TOPO_N_PORT:
1478 		prange = 2;
1479 		break;
1480 	default:
1481 		prange = FL_PORT_ID;
1482 		break;
1483 	}
1484 
1485 	/*
1486 	 * Run through the local loop ports and get port database info
1487 	 * for each loop ID.
1488 	 *
1489 	 * There's a somewhat unexplained situation where the f/w passes back
1490 	 * the wrong database entity- if that happens, just restart (up to
1491 	 * FL_PORT_ID times).
1492 	 */
1493 	tport = fcp->tport;
1494 
1495 	/*
1496 	 * make sure the temp port database is clean...
1497 	 */
1498 	MEMZERO((void *) tport, sizeof (tport));
1499 
1500 	for (lim = loopid = 0; loopid < prange; loopid++) {
1501 		lp = &tport[loopid];
1502 		lp->node_wwn = isp_get_portname(isp, loopid, 1);
1503 		if (fcp->isp_loopstate < LOOP_PDB_RCVD)
1504 			return (-1);
1505 		if (lp->node_wwn == 0)
1506 			continue;
1507 		lp->port_wwn = isp_get_portname(isp, loopid, 0);
1508 		if (fcp->isp_loopstate < LOOP_PDB_RCVD)
1509 			return (-1);
1510 		if (lp->port_wwn == 0) {
1511 			lp->node_wwn = 0;
1512 			continue;
1513 		}
1514 
1515 		/*
1516 		 * Get an entry....
1517 		 */
1518 		if (isp_getpdb(isp, loopid, &pdb) != 0) {
1519 			if (fcp->isp_loopstate < LOOP_PDB_RCVD)
1520 				return (-1);
1521 			continue;
1522 		}
1523 
1524 		if (fcp->isp_loopstate < LOOP_PDB_RCVD)
1525 			return (-1);
1526 
1527 		/*
1528 		 * If the returned database element doesn't match what we
1529 		 * asked for, restart the process entirely (up to a point...).
1530 		 */
1531 		if (pdb.pdb_loopid != loopid) {
1532 			loopid = 0;
1533 			if (lim++ < FL_PORT_ID) {
1534 				continue;
1535 			}
1536 			isp_prt(isp, ISP_LOGWARN,
1537 			    "giving up on synchronizing the port database");
1538 			return (-1);
1539 		}
1540 
1541 		/*
1542 		 * Save the pertinent info locally.
1543 		 */
1544 		lp->node_wwn =
1545 		    (((u_int64_t)pdb.pdb_nodename[0]) << 56) |
1546 		    (((u_int64_t)pdb.pdb_nodename[1]) << 48) |
1547 		    (((u_int64_t)pdb.pdb_nodename[2]) << 40) |
1548 		    (((u_int64_t)pdb.pdb_nodename[3]) << 32) |
1549 		    (((u_int64_t)pdb.pdb_nodename[4]) << 24) |
1550 		    (((u_int64_t)pdb.pdb_nodename[5]) << 16) |
1551 		    (((u_int64_t)pdb.pdb_nodename[6]) <<  8) |
1552 		    (((u_int64_t)pdb.pdb_nodename[7]));
1553 		lp->port_wwn =
1554 		    (((u_int64_t)pdb.pdb_portname[0]) << 56) |
1555 		    (((u_int64_t)pdb.pdb_portname[1]) << 48) |
1556 		    (((u_int64_t)pdb.pdb_portname[2]) << 40) |
1557 		    (((u_int64_t)pdb.pdb_portname[3]) << 32) |
1558 		    (((u_int64_t)pdb.pdb_portname[4]) << 24) |
1559 		    (((u_int64_t)pdb.pdb_portname[5]) << 16) |
1560 		    (((u_int64_t)pdb.pdb_portname[6]) <<  8) |
1561 		    (((u_int64_t)pdb.pdb_portname[7]));
1562 		lp->roles =
1563 		    (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
1564 		lp->portid = BITS2WORD(pdb.pdb_portid_bits);
1565 		lp->loopid = pdb.pdb_loopid;
1566 		/*
1567 		 * Do a quick check to see whether this matches the saved port
1568 		 * database for the same loopid. We do this here to save
1569 		 * searching later (if possible). Note that this fails over
1570 		 * time as things shuffle on the loop- we get the current
1571 		 * loop state (where loop id as an index matches loop id in
1572 		 * use) and then compare it to our saved database which
1573 		 * never shifts.
1574 		 */
1575 		if (target >= 0 && isp_same_lportdb(lp, &fcp->portdb[target])) {
1576 			lp->valid = 1;
1577 		}
1578 	}
1579 
1580 	/*
1581 	 * If we get this far, we've settled our differences with the f/w
1582 	 * and we can say that the loop state is ready.
1583 	 */
1584 	fcp->isp_loopstate = LOOP_READY;
1585 
1586 	/*
1587 	 * Mark all of the permanent local loop database entries as invalid.
1588 	 */
1589 	for (loopid = 0; loopid < FL_PORT_ID; loopid++) {
1590 		fcp->portdb[loopid].valid = 0;
1591 	}
1592 
1593 	/*
1594 	 * Now merge our local copy of the port database into our saved copy.
1595 	 * Notify the outer layers of new devices arriving.
1596 	 */
1597 	for (loopid = 0; loopid < prange; loopid++) {
1598 		int i;
1599 
1600 		/*
1601 		 * If we don't have a non-zero Port WWN, we're not here.
1602 		 */
1603 		if (tport[loopid].port_wwn == 0) {
1604 			continue;
1605 		}
1606 
1607 		/*
1608 		 * If we've already marked our tmp copy as valid,
1609 		 * this means that we've decided that it's the
1610 		 * same as our saved data base. This didn't include
1611 		 * the 'valid' marking so we have set that here.
1612 		 */
1613 		if (tport[loopid].valid) {
1614 			fcp->portdb[loopid].valid = 1;
1615 			continue;
1616 		}
1617 
1618 		/*
1619 		 * For the purposes of deciding whether this is the
1620 		 * 'same' device or not, we only search for an identical
1621 		 * Port WWN. Node WWNs may or may not be the same as
1622 		 * the Port WWN, and there may be multiple different
1623 		 * Port WWNs with the same Node WWN. It would be chaos
1624 		 * to have multiple identical Port WWNs, so we don't
1625 		 * allow that.
1626 		 */
1627 
1628 		for (i = 0; i < FL_PORT_ID; i++) {
1629 			int j;
1630 			if (fcp->portdb[i].port_wwn == 0)
1631 				continue;
1632 			if (fcp->portdb[i].port_wwn != tport[loopid].port_wwn)
1633 				continue;
1634 			/*
1635 			 * We found this WWN elsewhere- it's changed
1636 			 * loopids then. We don't change it's actual
1637 			 * position in our cached port database- we
1638 			 * just change the actual loop ID we'd use.
1639 			 */
1640 			if (fcp->portdb[i].loopid != loopid) {
1641 				isp_prt(isp, ISP_LOGINFO, portshift, i,
1642 				    fcp->portdb[i].loopid,
1643 				    fcp->portdb[i].portid, loopid,
1644 				    tport[loopid].portid);
1645 			}
1646 			fcp->portdb[i].portid = tport[loopid].portid;
1647 			fcp->portdb[i].loopid = loopid;
1648 			fcp->portdb[i].valid = 1;
1649 			fcp->portdb[i].roles = tport[loopid].roles;
1650 
1651 			/*
1652 			 * Now make sure this Port WWN doesn't exist elsewhere
1653 			 * in the port database.
1654 			 */
1655 			for (j = i+1; j < FL_PORT_ID; j++) {
1656 				if (fcp->portdb[i].port_wwn !=
1657 				    fcp->portdb[j].port_wwn) {
1658 					continue;
1659 				}
1660 				isp_prt(isp, ISP_LOGWARN, portdup, j, i);
1661 				/*
1662 				 * Invalidate the 'old' *and* 'new' ones.
1663 				 * This is really harsh and not quite right,
1664 				 * but if this happens, we really don't know
1665 				 * who is what at this point.
1666 				 */
1667 				fcp->portdb[i].valid = 0;
1668 				fcp->portdb[j].valid = 0;
1669 			}
1670 			break;
1671 		}
1672 
1673 		/*
1674 		 * If we didn't traverse the entire port database,
1675 		 * then we found (and remapped) an existing entry.
1676 		 * No need to notify anyone- go for the next one.
1677 		 */
1678 		if (i < FL_PORT_ID) {
1679 			continue;
1680 		}
1681 
1682 		/*
1683 		 * We've not found this Port WWN anywhere. It's a new entry.
1684 		 * See if we can leave it where it is (with target == loopid).
1685 		 */
1686 		if (fcp->portdb[loopid].port_wwn != 0) {
1687 			for (lim = 0; lim < FL_PORT_ID; lim++) {
1688 				if (fcp->portdb[lim].port_wwn == 0)
1689 					break;
1690 			}
1691 			/* "Cannot Happen" */
1692 			if (lim == FL_PORT_ID) {
1693 				isp_prt(isp, ISP_LOGWARN, "Remap Overflow");
1694 				continue;
1695 			}
1696 			i = lim;
1697 		} else {
1698 			i = loopid;
1699 		}
1700 
1701 		/*
1702 		 * NB:	The actual loopid we use here is loopid- we may
1703 		 *	in fact be at a completely different index (target).
1704 		 */
1705 		fcp->portdb[i].loopid = loopid;
1706 		fcp->portdb[i].port_wwn = tport[loopid].port_wwn;
1707 		fcp->portdb[i].node_wwn = tport[loopid].node_wwn;
1708 		fcp->portdb[i].roles = tport[loopid].roles;
1709 		fcp->portdb[i].portid = tport[loopid].portid;
1710 		fcp->portdb[i].valid = 1;
1711 
1712 		/*
1713 		 * Tell the outside world we've arrived.
1714 		 */
1715 		(void) isp_async(isp, ISPASYNC_PDB_CHANGED, &i);
1716 	}
1717 
1718 	/*
1719 	 * Now find all previously used targets that are now invalid and
1720 	 * notify the outer layers that they're gone.
1721 	 */
1722 	for (lp = fcp->portdb; lp < &fcp->portdb[prange]; lp++) {
1723 		if (lp->valid || lp->port_wwn == 0)
1724 			continue;
1725 
1726 		/*
1727 		 * Tell the outside world we've gone away.
1728 		 */
1729 		loopid = lp - fcp->portdb;
1730 		(void) isp_async(isp, ISPASYNC_PDB_CHANGED, &loopid);
1731 		MEMZERO((void *) lp, sizeof (*lp));
1732 	}
1733 
1734 #ifdef	ISP2100_FABRIC
1735 	/*
1736 	 * Now log in any fabric devices
1737 	 */
1738 	for (lp = &fcp->portdb[FC_SNS_ID+1];
1739 	     lp < &fcp->portdb[MAX_FC_TARG]; lp++) {
1740 		u_int32_t portid;
1741 		mbreg_t mbs;
1742 
1743 		/*
1744 		 * Anything here?
1745 		 */
1746 		if (lp->port_wwn == 0)
1747 			continue;
1748 
1749 		/*
1750 		 * Don't try to log into yourself.
1751 		 */
1752 		if ((portid = lp->portid) == fcp->isp_portid)
1753 			continue;
1754 
1755 
1756 		/*
1757 		 * If we'd been logged in- see if we still are and we haven't
1758 		 * changed. If so, no need to log ourselves out, etc..
1759 		 */
1760 		if (lp->loggedin &&
1761 		    isp_getpdb(isp, lp->loopid, &pdb) == 0) {
1762 			int nrole;
1763 			u_int64_t nwwnn, nwwpn;
1764 			nwwnn =
1765 			    (((u_int64_t)pdb.pdb_nodename[0]) << 56) |
1766 			    (((u_int64_t)pdb.pdb_nodename[1]) << 48) |
1767 			    (((u_int64_t)pdb.pdb_nodename[2]) << 40) |
1768 			    (((u_int64_t)pdb.pdb_nodename[3]) << 32) |
1769 			    (((u_int64_t)pdb.pdb_nodename[4]) << 24) |
1770 			    (((u_int64_t)pdb.pdb_nodename[5]) << 16) |
1771 			    (((u_int64_t)pdb.pdb_nodename[6]) <<  8) |
1772 			    (((u_int64_t)pdb.pdb_nodename[7]));
1773 			nwwpn =
1774 			    (((u_int64_t)pdb.pdb_portname[0]) << 56) |
1775 			    (((u_int64_t)pdb.pdb_portname[1]) << 48) |
1776 			    (((u_int64_t)pdb.pdb_portname[2]) << 40) |
1777 			    (((u_int64_t)pdb.pdb_portname[3]) << 32) |
1778 			    (((u_int64_t)pdb.pdb_portname[4]) << 24) |
1779 			    (((u_int64_t)pdb.pdb_portname[5]) << 16) |
1780 			    (((u_int64_t)pdb.pdb_portname[6]) <<  8) |
1781 			    (((u_int64_t)pdb.pdb_portname[7]));
1782 			nrole = (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >>
1783 			    SVC3_ROLE_SHIFT;
1784 			if (pdb.pdb_loopid == lp->loopid && lp->portid ==
1785 			    (u_int32_t) BITS2WORD(pdb.pdb_portid_bits) &&
1786 			    nwwnn == lp->node_wwn && nwwpn == lp->port_wwn &&
1787 			    lp->roles == nrole) {
1788 				lp->loggedin = lp->valid = 1;
1789 				isp_prt(isp, ISP_LOGINFO, lretained,
1790 				    (int) (lp - fcp->portdb),
1791 				    (int) lp->loopid, lp->portid);
1792 				continue;
1793 			}
1794 		}
1795 
1796 		/*
1797 		 * Force a logout if we were logged in.
1798 		 */
1799 		if (lp->loggedin) {
1800 			mbs.param[0] = MBOX_FABRIC_LOGOUT;
1801 			mbs.param[1] = lp->loopid << 8;
1802 			mbs.param[2] = 0;
1803 			mbs.param[3] = 0;
1804 			isp_mboxcmd(isp, &mbs, MBLOGNONE);
1805 			lp->loggedin = 0;
1806 			isp_prt(isp, ISP_LOGINFO, plogout,
1807 			    (int) (lp - fcp->portdb), lp->loopid, lp->portid);
1808 		}
1809 
1810 		/*
1811 		 * And log in....
1812 		 */
1813 		loopid = lp - fcp->portdb;
1814 		lp->loopid = 0;
1815 		do {
1816 			mbs.param[0] = MBOX_FABRIC_LOGIN;
1817 			mbs.param[1] = loopid << 8;
1818 			mbs.param[2] = portid >> 16;
1819 			mbs.param[3] = portid & 0xffff;
1820 			if (IS_2200(isp)) {
1821 				/* only issue a PLOGI if not logged in */
1822 				mbs.param[1] |= 0x1;
1823 			}
1824 			isp_mboxcmd(isp, &mbs, MBLOGALL & ~(MBOX_LOOP_ID_USED |
1825 			    MBOX_PORT_ID_USED | MBOX_COMMAND_ERROR));
1826 			switch (mbs.param[0]) {
1827 			case MBOX_LOOP_ID_USED:
1828 				/*
1829 				 * Try the next available loop id.
1830 				 */
1831 				loopid++;
1832 				break;
1833 			case MBOX_PORT_ID_USED:
1834 				/*
1835 				 * This port is already logged in.
1836 				 * Snaffle the loop id it's using if it's
1837 				 * nonzero, otherwise we're hosed.
1838 				 */
1839 				if (mbs.param[1] != 0) {
1840 					loopid = mbs.param[1];
1841 					isp_prt(isp, ISP_LOGINFO, retained,
1842 					    loopid, (int) (lp - fcp->portdb),
1843 					    lp->portid);
1844 				} else {
1845 					loopid = MAX_FC_TARG;
1846 					break;
1847 				}
1848 				/* FALLTHROUGH */
1849 			case MBOX_COMMAND_COMPLETE:
1850 				lp->loggedin = 1;
1851 				lp->loopid = loopid;
1852 				break;
1853 			case MBOX_COMMAND_ERROR:
1854 				isp_prt(isp, ISP_LOGINFO, plogierr,
1855 				    portid, mbs.param[1]);
1856 				/* FALLTHROUGH */
1857 			case MBOX_ALL_IDS_USED: /* We're outta IDs */
1858 			default:
1859 				loopid = MAX_FC_TARG;
1860 				break;
1861 			}
1862 		} while (lp->loopid == 0 && loopid < MAX_FC_TARG);
1863 
1864 		/*
1865 		 * If we get here and we haven't set a Loop ID,
1866 		 * we failed to log into this device.
1867 		 */
1868 
1869 		if (lp->loopid == 0) {
1870 			continue;
1871 		}
1872 
1873 		/*
1874 		 * Make sure we can get the approriate port information.
1875 		 */
1876 		if (isp_getpdb(isp, lp->loopid, &pdb) != 0) {
1877 			isp_prt(isp, ISP_LOGWARN, nopdb, lp->portid);
1878 			goto dump_em;
1879 		}
1880 
1881 		if (pdb.pdb_loopid != lp->loopid) {
1882 			isp_prt(isp, ISP_LOGWARN, pdbmfail1,
1883 			    lp->portid, pdb.pdb_loopid);
1884 			goto dump_em;
1885 		}
1886 
1887 		if (lp->portid != (u_int32_t) BITS2WORD(pdb.pdb_portid_bits)) {
1888 			isp_prt(isp, ISP_LOGWARN, pdbmfail2,
1889 			    lp->portid, BITS2WORD(pdb.pdb_portid_bits));
1890 			goto dump_em;
1891 		}
1892 
1893 		lp->roles =
1894 		    (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
1895 		lp->node_wwn =
1896 		    (((u_int64_t)pdb.pdb_nodename[0]) << 56) |
1897 		    (((u_int64_t)pdb.pdb_nodename[1]) << 48) |
1898 		    (((u_int64_t)pdb.pdb_nodename[2]) << 40) |
1899 		    (((u_int64_t)pdb.pdb_nodename[3]) << 32) |
1900 		    (((u_int64_t)pdb.pdb_nodename[4]) << 24) |
1901 		    (((u_int64_t)pdb.pdb_nodename[5]) << 16) |
1902 		    (((u_int64_t)pdb.pdb_nodename[6]) <<  8) |
1903 		    (((u_int64_t)pdb.pdb_nodename[7]));
1904 		lp->port_wwn =
1905 		    (((u_int64_t)pdb.pdb_portname[0]) << 56) |
1906 		    (((u_int64_t)pdb.pdb_portname[1]) << 48) |
1907 		    (((u_int64_t)pdb.pdb_portname[2]) << 40) |
1908 		    (((u_int64_t)pdb.pdb_portname[3]) << 32) |
1909 		    (((u_int64_t)pdb.pdb_portname[4]) << 24) |
1910 		    (((u_int64_t)pdb.pdb_portname[5]) << 16) |
1911 		    (((u_int64_t)pdb.pdb_portname[6]) <<  8) |
1912 		    (((u_int64_t)pdb.pdb_portname[7]));
1913 		/*
1914 		 * Check to make sure this all makes sense.
1915 		 */
1916 		if (lp->node_wwn && lp->port_wwn) {
1917 			lp->valid = 1;
1918 			loopid = lp - fcp->portdb;
1919 			(void) isp_async(isp, ISPASYNC_PDB_CHANGED, &loopid);
1920 			continue;
1921 		}
1922 dump_em:
1923 		lp->valid = 0;
1924 		isp_prt(isp, ISP_LOGINFO,
1925 		    ldumped, loopid, lp->loopid, lp->portid);
1926 		mbs.param[0] = MBOX_FABRIC_LOGOUT;
1927 		mbs.param[1] = lp->loopid << 8;
1928 		mbs.param[2] = 0;
1929 		mbs.param[3] = 0;
1930 		isp_mboxcmd(isp, &mbs, MBLOGNONE);
1931 	}
1932 #endif
1933 	/*
1934 	 * If we get here, we've for sure seen not only a valid loop
1935 	 * but know what is or isn't on it, so mark this for usage
1936 	 * in isp_start.
1937 	 */
1938 	fcp->loop_seen_once = 1;
1939 	return (0);
1940 }
1941 
1942 #ifdef	ISP2100_FABRIC
1943 static int
1944 isp_scan_fabric(isp)
1945 	struct ispsoftc *isp;
1946 {
1947 	fcparam *fcp = isp->isp_param;
1948 	u_int32_t portid, first_nz_portid;
1949 	sns_screq_t *reqp;
1950 	sns_scrsp_t *resp;
1951 	mbreg_t mbs;
1952 	int hicap;
1953 
1954 	reqp = (sns_screq_t *) fcp->isp_scratch;
1955 	resp = (sns_scrsp_t *) (&((char *)fcp->isp_scratch)[0x100]);
1956 	first_nz_portid = portid = fcp->isp_portid;
1957 
1958 	for (hicap = 0; hicap < 1024; hicap++) {
1959 		MEMZERO((void *) reqp, SNS_GAN_REQ_SIZE);
1960 		reqp->snscb_rblen = SNS_GAN_RESP_SIZE >> 1;
1961 		reqp->snscb_addr[RQRSP_ADDR0015] =
1962 			DMA_LSW(fcp->isp_scdma + 0x100);
1963 		reqp->snscb_addr[RQRSP_ADDR1631] =
1964 			DMA_MSW(fcp->isp_scdma + 0x100);
1965 		reqp->snscb_sblen = 6;
1966 		reqp->snscb_data[0] = SNS_GAN;
1967 		reqp->snscb_data[4] = portid & 0xffff;
1968 		reqp->snscb_data[5] = (portid >> 16) & 0xff;
1969 		ISP_SWIZZLE_SNS_REQ(isp, reqp);
1970 		mbs.param[0] = MBOX_SEND_SNS;
1971 		mbs.param[1] = SNS_GAN_REQ_SIZE >> 1;
1972 		mbs.param[2] = DMA_MSW(fcp->isp_scdma);
1973 		mbs.param[3] = DMA_LSW(fcp->isp_scdma);
1974 		mbs.param[6] = 0;
1975 		mbs.param[7] = 0;
1976 		isp_mboxcmd(isp, &mbs, MBLOGALL);
1977 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1978 			return (-1);
1979 		}
1980 		ISP_UNSWIZZLE_SNS_RSP(isp, resp, SNS_GAN_RESP_SIZE >> 1);
1981 		portid = (((u_int32_t) resp->snscb_port_id[0]) << 16) |
1982 		    (((u_int32_t) resp->snscb_port_id[1]) << 8) |
1983 		    (((u_int32_t) resp->snscb_port_id[2]));
1984 		if (isp_async(isp, ISPASYNC_FABRIC_DEV, resp)) {
1985 			return (-1);
1986 		}
1987 		if (first_nz_portid == 0 && portid) {
1988 			first_nz_portid = portid;
1989 		}
1990 		if (first_nz_portid == portid) {
1991 			return (0);
1992 		}
1993 	}
1994 	/*
1995 	 * We either have a broken name server or a huge fabric if we get here.
1996 	 */
1997 	return (0);
1998 }
1999 #endif
2000 /*
2001  * Start a command. Locking is assumed done in the caller.
2002  */
2003 
2004 int
2005 isp_start(xs)
2006 	XS_T *xs;
2007 {
2008 	struct ispsoftc *isp;
2009 	u_int16_t iptr, optr;
2010 	union {
2011 		ispreq_t *_reqp;
2012 		ispreqt2_t *_t2reqp;
2013 	} _u;
2014 #define	reqp	_u._reqp
2015 #define	t2reqp	_u._t2reqp
2016 #define	UZSIZE	max(sizeof (ispreq_t), sizeof (ispreqt2_t))
2017 	int target, i;
2018 
2019 	XS_INITERR(xs);
2020 	isp = XS_ISP(xs);
2021 
2022 	if (isp->isp_state != ISP_RUNSTATE) {
2023 		isp_prt(isp, ISP_LOGERR, "Adapter not at RUNSTATE");
2024 		XS_SETERR(xs, HBA_BOTCH);
2025 		return (CMD_COMPLETE);
2026 	}
2027 
2028 	/*
2029 	 * Check command CDB length, etc.. We really are limited to 16 bytes
2030 	 * for Fibre Channel, but can do up to 44 bytes in parallel SCSI,
2031 	 * but probably only if we're running fairly new firmware (we'll
2032 	 * let the old f/w choke on an extended command queue entry).
2033 	 */
2034 
2035 	if (XS_CDBLEN(xs) > (IS_FC(isp)? 16 : 44) || XS_CDBLEN(xs) == 0) {
2036 		isp_prt(isp, ISP_LOGERR,
2037 		    "unsupported cdb length (%d, CDB[0]=0x%x)",
2038 		    XS_CDBLEN(xs), XS_CDBP(xs)[0] & 0xff);
2039 		XS_SETERR(xs, HBA_BOTCH);
2040 		return (CMD_COMPLETE);
2041 	}
2042 
2043 	/*
2044 	 * Check to see whether we have good firmware state still or
2045 	 * need to refresh our port database for this target.
2046 	 */
2047 	target = XS_TGT(xs);
2048 	if (IS_FC(isp)) {
2049 		fcparam *fcp = isp->isp_param;
2050 		struct lportdb *lp;
2051 #if	defined(ISP2100_FABRIC)
2052 		/*
2053 		 * If we're not on a Fabric, we can't have a target
2054 		 * above FL_PORT_ID-1. If we're on a fabric and
2055 		 * connected as an F-port, we can't have a target
2056 		 * less than FC_SNS_ID+1.
2057 		 */
2058 		if (fcp->isp_onfabric == 0) {
2059 			if (target >= FL_PORT_ID) {
2060 				XS_SETERR(xs, HBA_SELTIMEOUT);
2061 				return (CMD_COMPLETE);
2062 			}
2063 		} else {
2064 			if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
2065 				XS_SETERR(xs, HBA_SELTIMEOUT);
2066 				return (CMD_COMPLETE);
2067 			}
2068 			if (fcp->isp_topo == TOPO_F_PORT &&
2069 			    target < FL_PORT_ID) {
2070 				XS_SETERR(xs, HBA_SELTIMEOUT);
2071 				return (CMD_COMPLETE);
2072 			}
2073 		}
2074 #endif
2075 		/*
2076 		 * Check for f/w being in ready state. If the f/w
2077 		 * isn't in ready state, then we don't know our
2078 		 * loop ID and the f/w hasn't completed logging
2079 		 * into all targets on the loop. If this is the
2080 		 * case, then bounce the command. We pretend this is
2081 		 * a SELECTION TIMEOUT error if we've never gone to
2082 		 * FW_READY state at all- in this case we may not
2083 		 * be hooked to a loop at all and we shouldn't hang
2084 		 * the machine for this. Otherwise, defer this command
2085 		 * until later.
2086 		 */
2087 		if (fcp->isp_fwstate != FW_READY) {
2088 			/*
2089 			 * Give ourselves at most a 250ms delay.
2090 			 */
2091 			if (isp_fclink_test(isp, 250000)) {
2092 				XS_SETERR(xs, HBA_SELTIMEOUT);
2093 				if (fcp->loop_seen_once) {
2094 					return (CMD_RQLATER);
2095 				} else {
2096 					return (CMD_COMPLETE);
2097 				}
2098 			}
2099 		}
2100 
2101 		/*
2102 		 * If our loop state is such that we haven't yet received
2103 		 * a "Port Database Changed" notification (after a LIP or
2104 		 * a Loop Reset or firmware initialization), then defer
2105 		 * sending commands for a little while, but only if we've
2106 		 * seen a valid loop at one point (otherwise we can get
2107 		 * stuck at initialization time).
2108 		 */
2109 		if (fcp->isp_loopstate < LOOP_PDB_RCVD) {
2110 			XS_SETERR(xs, HBA_SELTIMEOUT);
2111 			if (fcp->loop_seen_once) {
2112 				return (CMD_RQLATER);
2113 			} else {
2114 				return (CMD_COMPLETE);
2115 			}
2116 		}
2117 
2118 		/*
2119 		 * If our loop state is now such that we've just now
2120 		 * received a Port Database Change notification, then
2121 		 * we have to go off and (re)synchronize our port
2122 		 * database.
2123 		 */
2124 		if (fcp->isp_loopstate == LOOP_PDB_RCVD) {
2125 			if (isp_pdb_sync(isp, target)) {
2126 				XS_SETERR(xs, HBA_SELTIMEOUT);
2127 				return (CMD_COMPLETE);
2128 			}
2129 		}
2130 
2131 		/*
2132 		 * XXX: Here's were we would cancel any loop_dead flag
2133 		 * XXX: also cancel in dead_loop timeout that's running
2134 		 */
2135 
2136 		/*
2137 		 * Now check whether we should even think about pursuing this.
2138 		 */
2139 		lp = &fcp->portdb[target];
2140 		if (lp->valid == 0) {
2141 			XS_SETERR(xs, HBA_SELTIMEOUT);
2142 			return (CMD_COMPLETE);
2143 		}
2144 		if ((lp->roles & (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT)) == 0) {
2145 			isp_prt(isp, ISP_LOGDEBUG2,
2146 			    "Target %d does not have target service", target);
2147 			XS_SETERR(xs, HBA_SELTIMEOUT);
2148 			return (CMD_COMPLETE);
2149 		}
2150 		/*
2151 		 * Now turn target into what the actual loop ID is.
2152 		 */
2153 		target = lp->loopid;
2154 	}
2155 
2156 	/*
2157 	 * Next check to see if any HBA or Device
2158 	 * parameters need to be updated.
2159 	 */
2160 	if (isp->isp_update != 0) {
2161 		isp_update(isp);
2162 	}
2163 
2164 	if (isp_getrqentry(isp, &iptr, &optr, (void **) &reqp)) {
2165 		isp_prt(isp, ISP_LOGDEBUG0, "Request Queue Overflow");
2166 		XS_SETERR(xs, HBA_BOTCH);
2167 		return (CMD_EAGAIN);
2168 	}
2169 
2170 	/*
2171 	 * Now see if we need to synchronize the ISP with respect to anything.
2172 	 * We do dual duty here (cough) for synchronizing for busses other
2173 	 * than which we got here to send a command to.
2174 	 */
2175 	if (isp->isp_sendmarker) {
2176 		u_int8_t n = (IS_DUALBUS(isp)? 2: 1);
2177 		/*
2178 		 * Check ports to send markers for...
2179 		 */
2180 		for (i = 0; i < n; i++) {
2181 			if ((isp->isp_sendmarker & (1 << i)) == 0) {
2182 				continue;
2183 			}
2184 			MEMZERO((void *) reqp, sizeof (*reqp));
2185 			reqp->req_header.rqs_entry_count = 1;
2186 			reqp->req_header.rqs_entry_type = RQSTYPE_MARKER;
2187 			reqp->req_modifier = SYNC_ALL;
2188 			reqp->req_target = i << 7;	/* insert bus number */
2189 			ISP_SWIZZLE_REQUEST(isp, reqp);
2190 			ISP_ADD_REQUEST(isp, iptr);
2191 
2192 			if (isp_getrqentry(isp, &iptr, &optr, (void **)&reqp)) {
2193 				isp_prt(isp, ISP_LOGDEBUG0,
2194 				    "Request Queue Overflow+");
2195 				XS_SETERR(xs, HBA_BOTCH);
2196 				return (CMD_EAGAIN);
2197 			}
2198 		}
2199 	}
2200 
2201 	MEMZERO((void *) reqp, UZSIZE);
2202 	reqp->req_header.rqs_entry_count = 1;
2203 	if (IS_FC(isp)) {
2204 		reqp->req_header.rqs_entry_type = RQSTYPE_T2RQS;
2205 	} else {
2206 		if (XS_CDBLEN(xs) > 12)
2207 			reqp->req_header.rqs_entry_type = RQSTYPE_CMDONLY;
2208 		else
2209 			reqp->req_header.rqs_entry_type = RQSTYPE_REQUEST;
2210 	}
2211 	reqp->req_header.rqs_flags = 0;
2212 	reqp->req_header.rqs_seqno = 0;
2213 	if (IS_FC(isp)) {
2214 		/*
2215 		 * See comment in isp_intr
2216 		 */
2217 		XS_RESID(xs) = 0;
2218 
2219 		/*
2220 		 * Fibre Channel always requires some kind of tag.
2221 		 * The Qlogic drivers seem be happy not to use a tag,
2222 		 * but this breaks for some devices (IBM drives).
2223 		 */
2224 		if (XS_TAG_P(xs)) {
2225 			t2reqp->req_flags = XS_TAG_TYPE(xs);
2226 		} else {
2227 			/*
2228 			 * If we don't know what tag to use, use HEAD OF QUEUE
2229 			 * for Request Sense or Ordered (for safety's sake).
2230 			 */
2231 			if (XS_CDBP(xs)[0] == 0x3)	/* REQUEST SENSE */
2232 				t2reqp->req_flags = REQFLAG_HTAG;
2233 			else
2234 				t2reqp->req_flags = REQFLAG_OTAG;
2235 		}
2236 	} else {
2237 		sdparam *sdp = (sdparam *)isp->isp_param;
2238 		if ((sdp->isp_devparam[target].cur_dflags & DPARM_TQING) &&
2239 		    XS_TAG_P(xs)) {
2240 			reqp->req_flags = XS_TAG_TYPE(xs);
2241 		}
2242 	}
2243 	reqp->req_target = target | (XS_CHANNEL(xs) << 7);
2244 	if (IS_SCSI(isp)) {
2245 		reqp->req_lun_trn = XS_LUN(xs);
2246 		reqp->req_cdblen = XS_CDBLEN(xs);
2247 	} else {
2248 		if (isp->isp_maxluns > 16)
2249 			t2reqp->req_scclun = XS_LUN(xs);
2250 		else
2251 			t2reqp->req_lun_trn = XS_LUN(xs);
2252 	}
2253 	MEMCPY(reqp->req_cdb, XS_CDBP(xs), XS_CDBLEN(xs));
2254 
2255 	reqp->req_time = XS_TIME(xs) / 1000;
2256 	if (reqp->req_time == 0 && XS_TIME(xs))
2257 		reqp->req_time = 1;
2258 
2259 	/*
2260 	 * Always give a bit more leeway to commands after a bus reset.
2261 	 * XXX: DOES NOT DISTINGUISH WHICH PORT MAY HAVE BEEN SYNCED
2262 	 */
2263 	if (isp->isp_sendmarker && reqp->req_time < 5) {
2264 		reqp->req_time = 5;
2265 	}
2266 	if (isp_save_xs(isp, xs, &reqp->req_handle)) {
2267 		isp_prt(isp, ISP_LOGDEBUG1, "out of xflist pointers");
2268 		XS_SETERR(xs, HBA_BOTCH);
2269 		return (CMD_EAGAIN);
2270 	}
2271 	/*
2272 	 * Set up DMA and/or do any bus swizzling of the request entry
2273 	 * so that the Qlogic F/W understands what is being asked of it.
2274  	*/
2275 	i = ISP_DMASETUP(isp, xs, reqp, &iptr, optr);
2276 	if (i != CMD_QUEUED) {
2277 		isp_destroy_handle(isp, reqp->req_handle);
2278 		/*
2279 		 * dmasetup sets actual error in packet, and
2280 		 * return what we were given to return.
2281 		 */
2282 		return (i);
2283 	}
2284 	XS_SETERR(xs, HBA_NOERROR);
2285 	isp_prt(isp, ISP_LOGDEBUG2,
2286 	    "START cmd for %d.%d.%d cmd 0x%x datalen %d",
2287 	    XS_CHANNEL(xs), target, XS_LUN(xs), XS_CDBP(xs)[0], XS_XFRLEN(xs));
2288 	ISP_ADD_REQUEST(isp, iptr);
2289 	isp->isp_nactive++;
2290 	if (isp->isp_sendmarker)
2291 		isp->isp_sendmarker = 0;
2292 	return (CMD_QUEUED);
2293 #undef	reqp
2294 #undef	t2reqp
2295 }
2296 
2297 /*
2298  * isp control
2299  * Locks (ints blocked) assumed held.
2300  */
2301 
2302 int
2303 isp_control(isp, ctl, arg)
2304 	struct ispsoftc *isp;
2305 	ispctl_t ctl;
2306 	void *arg;
2307 {
2308 	XS_T *xs;
2309 	mbreg_t mbs;
2310 	int bus, tgt;
2311 	u_int32_t handle;
2312 
2313 	switch (ctl) {
2314 	default:
2315 		isp_prt(isp, ISP_LOGERR, "Unknown Control Opcode 0x%x", ctl);
2316 		break;
2317 
2318 	case ISPCTL_RESET_BUS:
2319 		/*
2320 		 * Issue a bus reset.
2321 		 */
2322 		mbs.param[0] = MBOX_BUS_RESET;
2323 		mbs.param[2] = 0;
2324 		if (IS_SCSI(isp)) {
2325 			mbs.param[1] =
2326 			    ((sdparam *) isp->isp_param)->isp_bus_reset_delay;
2327 			if (mbs.param[1] < 2)
2328 				mbs.param[1] = 2;
2329 			bus = *((int *) arg);
2330 			if (IS_DUALBUS(isp))
2331 				mbs.param[2] = bus;
2332 		} else {
2333 			mbs.param[1] = 10;
2334 			bus = 0;
2335 		}
2336 		isp->isp_sendmarker |= (1 << bus);
2337 		isp_mboxcmd(isp, &mbs, MBLOGALL);
2338 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2339 			break;
2340 		}
2341 		isp_prt(isp, ISP_LOGINFO,
2342 		    "driver initiated bus reset of bus %d", bus);
2343 		return (0);
2344 
2345 	case ISPCTL_RESET_DEV:
2346 		tgt = (*((int *) arg)) & 0xffff;
2347 		bus = (*((int *) arg)) >> 16;
2348 		mbs.param[0] = MBOX_ABORT_TARGET;
2349 		mbs.param[1] = (tgt << 8) | (bus << 15);
2350 		mbs.param[2] = 3;	/* 'delay', in seconds */
2351 		isp_mboxcmd(isp, &mbs, MBLOGALL);
2352 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2353 			break;
2354 		}
2355 		isp_prt(isp, ISP_LOGINFO,
2356 		    "Target %d on Bus %d Reset Succeeded", tgt, bus);
2357 		isp->isp_sendmarker |= (1 << bus);
2358 		return (0);
2359 
2360 	case ISPCTL_ABORT_CMD:
2361 		xs = (XS_T *) arg;
2362 		tgt = XS_TGT(xs);
2363 		handle = isp_find_handle(isp, xs);
2364 		if (handle == 0) {
2365 			isp_prt(isp, ISP_LOGWARN,
2366 			    "cannot find handle for command to abort");
2367 			break;
2368 		}
2369 		bus = XS_CHANNEL(xs);
2370 		mbs.param[0] = MBOX_ABORT;
2371 		if (IS_FC(isp)) {
2372 			if (isp->isp_maxluns > 16) {
2373 				mbs.param[1] = tgt << 8;
2374 				mbs.param[4] = 0;
2375 				mbs.param[5] = 0;
2376 				mbs.param[6] = XS_LUN(xs);
2377 			} else {
2378 				mbs.param[1] = tgt << 8 | XS_LUN(xs);
2379 			}
2380 		} else {
2381 			mbs.param[1] =
2382 			    (bus << 15) | (XS_TGT(xs) << 8) | XS_LUN(xs);
2383 		}
2384 		mbs.param[3] = handle >> 16;
2385 		mbs.param[2] = handle & 0xffff;
2386 		isp_mboxcmd(isp, &mbs, MBLOGALL & ~MBOX_COMMAND_ERROR);
2387 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
2388 			return (0);
2389 		}
2390 		/*
2391 		 * XXX: Look for command in the REQUEST QUEUE. That is,
2392 		 * XXX: It hasen't been picked up by firmware yet.
2393 		 */
2394 		break;
2395 
2396 	case ISPCTL_UPDATE_PARAMS:
2397 		isp_update(isp);
2398 		return (0);
2399 
2400 	case ISPCTL_FCLINK_TEST:
2401 		if (IS_FC(isp)) {
2402 			int usdelay = (arg)? *((int *) arg) : 250000;
2403 			return (isp_fclink_test(isp, usdelay));
2404 		}
2405 		break;
2406 
2407 	case ISPCTL_PDB_SYNC:
2408 		if (IS_FC(isp)) {
2409 			return (isp_pdb_sync(isp, -1));
2410 		}
2411 		break;
2412 #ifdef	ISP_TARGET_MODE
2413 	case ISPCTL_TOGGLE_TMODE:
2414 	{
2415 		int ena = *(int *)arg;
2416 		if (IS_SCSI(isp)) {
2417 			mbs.param[0] = MBOX_ENABLE_TARGET_MODE;
2418 			mbs.param[1] = (ena)? ENABLE_TARGET_FLAG : 0;
2419 			isp_mboxcmd(isp, &mbs, MBLOGALL);
2420 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2421 				break;
2422 			}
2423 		} else {
2424 			fcparam *fcp = isp->isp_param;
2425 			/*
2426 			 * We assume somebody has quiesced this bus.
2427 			 */
2428 			if (ena) {
2429 				if (fcp->isp_fwoptions & ICBOPT_TGT_ENABLE) {
2430 					return (0);
2431 				}
2432 				fcp->isp_fwoptions |= ICBOPT_TGT_ENABLE;
2433 			} else {
2434 				if (!(fcp->isp_fwoptions & ICBOPT_TGT_ENABLE)) {
2435 					return (0);
2436 				}
2437 				fcp->isp_fwoptions &= ~ICBOPT_TGT_ENABLE;
2438 			}
2439 			isp->isp_state = ISP_NILSTATE;
2440 			isp_reset(isp);
2441 			if (isp->isp_state != ISP_RESETSTATE) {
2442 				break;
2443 			}
2444 			isp_init(isp);
2445 			if (isp->isp_state != ISP_INITSTATE) {
2446 				break;
2447 			}
2448 			isp->isp_state = ISP_RUNSTATE;
2449 		}
2450 		return (0);
2451 	}
2452 #endif
2453 	}
2454 	return (-1);
2455 }
2456 
2457 /*
2458  * Interrupt Service Routine(s).
2459  *
2460  * External (OS) framework has done the appropriate locking,
2461  * and the locking will be held throughout this function.
2462  */
2463 
2464 /*
2465  * Limit our stack depth by sticking with the max likely number
2466  * of completions on a request queue at any one time.
2467  */
2468 #define	MAX_REQUESTQ_COMPLETIONS	32
2469 
2470 int
2471 isp_intr(arg)
2472 	void *arg;
2473 {
2474 	struct ispsoftc *isp = arg;
2475 	XS_T *complist[MAX_REQUESTQ_COMPLETIONS], *xs;
2476 	u_int16_t iptr, optr, isr, sema, junk;
2477 	int i, nlooked = 0, ndone = 0;
2478 
2479 	if (IS_2100(isp)) {
2480 		i = 0;
2481 		do {
2482 			isr = ISP_READ(isp, BIU_ISR);
2483 			junk = ISP_READ(isp, BIU_ISR);
2484 		} while (isr != junk && ++i < 1000);
2485 		if (isr != junk) {
2486 			isp_prt(isp, ISP_LOGWARN,
2487 			    "isr unsteady (%x, %x)", isr, junk);
2488 		}
2489 		i = 0;
2490 		do {
2491 			sema = ISP_READ(isp, BIU_SEMA);
2492 			junk = ISP_READ(isp, BIU_SEMA);
2493 		} while (sema != junk && ++i < 1000);
2494 		if (sema != junk) {
2495 			isp_prt(isp, ISP_LOGWARN,
2496 			    "sema unsteady (%x, %x)", sema, junk);
2497 		}
2498 	} else {
2499 		isr = ISP_READ(isp, BIU_ISR);
2500 		sema = ISP_READ(isp, BIU_SEMA);
2501 	}
2502 	isp_prt(isp, ISP_LOGDEBUG3, "isp_intr isr %x sem %x", isr, sema);
2503 	isr &= INT_PENDING_MASK(isp);
2504 	sema &= BIU_SEMA_LOCK;
2505 	isp->isp_intcnt++;
2506 	if (isr == 0 && sema == 0) {
2507 		isp->isp_intbogus++;
2508 		return (0);
2509 	}
2510 
2511 	if (sema) {
2512 		u_int16_t mbox;
2513 
2514 		if (IS_2100(isp)) {
2515 			i = 0;
2516 			do {
2517 				mbox = ISP_READ(isp, OUTMAILBOX0);
2518 				junk = ISP_READ(isp, OUTMAILBOX0);;
2519 			} while (junk != mbox && ++i < 1000);
2520 			if (mbox != junk) {
2521 				isp_prt(isp, ISP_LOGWARN,
2522 				    "mailbox0 unsteady (%x, %x)", mbox, junk);
2523 				ISP_WRITE(isp, BIU_SEMA, 0);
2524 				ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
2525 				return (1);
2526 			}
2527 		} else {
2528 			mbox = ISP_READ(isp, OUTMAILBOX0);
2529 		}
2530 		if (mbox & 0x4000) {
2531 			int obits, i = 0;
2532 			if ((obits = isp->isp_mboxbsy) != 0) {
2533 				isp->isp_mboxtmp[i++] = mbox;
2534 				for (i = 1; i < 8; i++) {
2535 					if ((obits & (1 << i)) == 0) {
2536 						continue;
2537 					}
2538 					isp->isp_mboxtmp[i] =
2539 					    ISP_READ(isp, MBOX_OFF(i));
2540 				}
2541 				MBOX_NOTIFY_COMPLETE(isp);
2542 			} else {
2543 				isp_prt(isp, ISP_LOGWARN,
2544 				    "Mbox Command Async (0x%x) with no waiters",
2545 				    mbox);
2546 			}
2547 		} else {
2548 			u_int32_t fhandle = isp_parse_async(isp, (int) mbox);
2549 			isp_prt(isp, ISP_LOGDEBUG2, "Async Mbox 0x%x", mbox);
2550 			if (fhandle > 0) {
2551 				isp_fastpost_complete(isp, fhandle);
2552 			}
2553 		}
2554 		if (IS_FC(isp) || isp->isp_state != ISP_RUNSTATE) {
2555 			ISP_WRITE(isp, BIU_SEMA, 0);
2556 			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
2557 			return (1);
2558 		}
2559 	}
2560 
2561 	/*
2562 	 * We can't be getting this now.
2563 	 */
2564 	if (isp->isp_state != ISP_RUNSTATE) {
2565 		isp_prt(isp, ISP_LOGWARN,
2566 		    "interrupt (isr=%x, sema=%x) when not ready", isr, sema);
2567 		ISP_WRITE(isp, INMAILBOX5, ISP_READ(isp, OUTMAILBOX5));
2568 		ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
2569 		ISP_WRITE(isp, BIU_SEMA, 0);
2570 		return (1);
2571 	}
2572 
2573 	/*
2574 	 * You *must* read OUTMAILBOX5 prior to clearing the RISC interrupt.
2575 	 */
2576 	optr = isp->isp_residx;
2577 
2578 	if (IS_2100(isp)) {
2579 		i = 0;
2580 		do {
2581 			iptr = ISP_READ(isp, OUTMAILBOX5);
2582 			junk = ISP_READ(isp, OUTMAILBOX5);
2583 		} while (junk != iptr && ++i < 1000);
2584 
2585 		if (iptr != junk) {
2586 			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
2587 			isp_prt(isp, ISP_LOGWARN,
2588 			    "mailbox5 unsteady (%x, %x)", iptr, junk);
2589 			return (1);
2590 		}
2591 	} else {
2592 		iptr = ISP_READ(isp, OUTMAILBOX5);
2593 	}
2594 
2595 	if (sema) {
2596 		ISP_WRITE(isp, BIU_SEMA, 0);
2597 	}
2598 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
2599 
2600 	if (optr == iptr && sema == 0) {
2601 		/*
2602 		 * There are a lot of these- reasons unknown- mostly on
2603 		 * faster Alpha machines.
2604 		 *
2605 		 * I tried delaying after writing HCCR_CMD_CLEAR_RISC_INT to
2606 		 * make sure the old interrupt went away (to avoid 'ringing'
2607 		 * effects), but that didn't stop this from occurring.
2608 		 */
2609 		junk = ISP_READ(isp, BIU_ISR);
2610 		isp_prt(isp, ISP_LOGDEBUG2,
2611 		    "bogus intr- isr %x (%x) iptr %x optr %x",
2612 		    isr, junk, iptr, optr);
2613 	}
2614 
2615 	while (optr != iptr) {
2616 		ispstatusreq_t *sp;
2617 		u_int16_t oop;
2618 		int buddaboom = 0;
2619 
2620 		sp = (ispstatusreq_t *) ISP_QUEUE_ENTRY(isp->isp_result, optr);
2621 		oop = optr;
2622 		optr = ISP_NXT_QENTRY(optr, RESULT_QUEUE_LEN(isp));
2623 		nlooked++;
2624 		/*
2625 		 * Do any appropriate unswizzling of what the Qlogic f/w has
2626 		 * written into memory so it makes sense to us. This is a
2627 		 * per-platform thing. Also includes any memory barriers.
2628 		 */
2629 		ISP_UNSWIZZLE_RESPONSE(isp, sp, oop);
2630 		if (sp->req_header.rqs_entry_type != RQSTYPE_RESPONSE) {
2631 			if (isp_handle_other_response(isp, sp, &optr) == 0) {
2632 				MEMZERO(sp, sizeof (isphdr_t));
2633 				continue;
2634 			}
2635 			/*
2636 			 * It really has to be a bounced request just copied
2637 			 * from the request queue to the response queue. If
2638 			 * not, something bad has happened.
2639 			 */
2640 			if (sp->req_header.rqs_entry_type != RQSTYPE_REQUEST) {
2641 				isp_prt(isp, ISP_LOGERR, notresp,
2642 				    sp->req_header.rqs_entry_type, oop, optr,
2643 				    nlooked);
2644 				if (isp->isp_dblev & ISP_LOGDEBUG0) {
2645 					isp_print_bytes(isp, "Queue Entry",
2646 					    QENTRY_LEN, sp);
2647 				}
2648 				MEMZERO(sp, sizeof (isphdr_t));
2649 				continue;
2650 			}
2651 			buddaboom = 1;
2652 		}
2653 
2654 		if (sp->req_header.rqs_flags & 0xf) {
2655 #define	_RQS_OFLAGS	\
2656 	~(RQSFLAG_CONTINUATION|RQSFLAG_FULL|RQSFLAG_BADHEADER|RQSFLAG_BADPACKET)
2657 			if (sp->req_header.rqs_flags & RQSFLAG_CONTINUATION) {
2658 				isp_prt(isp, ISP_LOGWARN,
2659 				    "continuation segment");
2660 				ISP_WRITE(isp, INMAILBOX5, optr);
2661 				continue;
2662 			}
2663 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
2664 				isp_prt(isp, ISP_LOGDEBUG1,
2665 				    "internal queues full");
2666 				/*
2667 				 * We'll synthesize a QUEUE FULL message below.
2668 				 */
2669 			}
2670 			if (sp->req_header.rqs_flags & RQSFLAG_BADHEADER) {
2671 				isp_prt(isp, ISP_LOGERR,  "bad header flag");
2672 				buddaboom++;
2673 			}
2674 			if (sp->req_header.rqs_flags & RQSFLAG_BADPACKET) {
2675 				isp_prt(isp, ISP_LOGERR, "bad request packet");
2676 				buddaboom++;
2677 			}
2678 			if (sp->req_header.rqs_flags & _RQS_OFLAGS) {
2679 				isp_prt(isp, ISP_LOGERR,
2680 				    "unknown flags (0x%x) in response",
2681 				    sp->req_header.rqs_flags);
2682 				buddaboom++;
2683 			}
2684 #undef	_RQS_OFLAGS
2685 		}
2686 		if (sp->req_handle > isp->isp_maxcmds || sp->req_handle < 1) {
2687 			MEMZERO(sp, sizeof (isphdr_t));
2688 			isp_prt(isp, ISP_LOGERR,
2689 			    "bad request handle %d", sp->req_handle);
2690 			ISP_WRITE(isp, INMAILBOX5, optr);
2691 			continue;
2692 		}
2693 		xs = isp_find_xs(isp, sp->req_handle);
2694 		if (xs == NULL) {
2695 			MEMZERO(sp, sizeof (isphdr_t));
2696 			isp_prt(isp, ISP_LOGERR,
2697 			    "cannot find handle 0x%x in xflist",
2698 			    sp->req_handle);
2699 			ISP_WRITE(isp, INMAILBOX5, optr);
2700 			continue;
2701 		}
2702 		isp_destroy_handle(isp, sp->req_handle);
2703 		if (sp->req_status_flags & RQSTF_BUS_RESET) {
2704 			isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
2705 		}
2706 		if (buddaboom) {
2707 			XS_SETERR(xs, HBA_BOTCH);
2708 		}
2709 
2710 		if (IS_FC(isp) && (sp->req_scsi_status & RQCS_SV)) {
2711 			/*
2712 			 * Fibre Channel F/W doesn't say we got status
2713 			 * if there's Sense Data instead. I guess they
2714 			 * think it goes w/o saying.
2715 			 */
2716 			sp->req_state_flags |= RQSF_GOT_STATUS;
2717 		}
2718 		if (sp->req_state_flags & RQSF_GOT_STATUS) {
2719 			*XS_STSP(xs) = sp->req_scsi_status & 0xff;
2720 		}
2721 
2722 		switch (sp->req_header.rqs_entry_type) {
2723 		case RQSTYPE_RESPONSE:
2724 			XS_SET_STATE_STAT(isp, xs, sp);
2725 			isp_parse_status(isp, sp, xs);
2726 			if ((XS_NOERR(xs) || XS_ERR(xs) == HBA_NOERROR) &&
2727 			    (*XS_STSP(xs) == SCSI_BUSY)) {
2728 				XS_SETERR(xs, HBA_TGTBSY);
2729 			}
2730 			if (IS_SCSI(isp)) {
2731 				XS_RESID(xs) = sp->req_resid;
2732 				if ((sp->req_state_flags & RQSF_GOT_STATUS) &&
2733 				    (*XS_STSP(xs) == SCSI_CHECK) &&
2734 				    (sp->req_state_flags & RQSF_GOT_SENSE)) {
2735 					XS_SAVE_SENSE(xs, sp);
2736 				}
2737 				/*
2738 				 * A new synchronous rate was negotiated for
2739 				 * this target. Mark state such that we'll go
2740 				 * look up that which has changed later.
2741 				 */
2742 				if (sp->req_status_flags & RQSTF_NEGOTIATION) {
2743 					int t = XS_TGT(xs);
2744 					sdparam *sdp = isp->isp_param;
2745 					sdp += XS_CHANNEL(xs);
2746 					sdp->isp_devparam[t].dev_refresh = 1;
2747 					isp->isp_update |=
2748 					    (1 << XS_CHANNEL(xs));
2749 				}
2750 			} else {
2751 				if (sp->req_status_flags & RQSF_XFER_COMPLETE) {
2752 					XS_RESID(xs) = 0;
2753 				} else if (sp->req_scsi_status & RQCS_RESID) {
2754 					XS_RESID(xs) = sp->req_resid;
2755 				} else {
2756 					XS_RESID(xs) = 0;
2757 				}
2758 				if ((sp->req_state_flags & RQSF_GOT_STATUS) &&
2759 				    (*XS_STSP(xs) == SCSI_CHECK) &&
2760 				    (sp->req_scsi_status & RQCS_SV)) {
2761 					XS_SAVE_SENSE(xs, sp);
2762 				}
2763 			}
2764 			isp_prt(isp, ISP_LOGDEBUG2, "asked for %d got resid %d",
2765 				XS_XFRLEN(xs), sp->req_resid);
2766 			break;
2767 		case RQSTYPE_REQUEST:
2768 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
2769 				/*
2770 				 * Force Queue Full status.
2771 				 */
2772 				*XS_STSP(xs) = SCSI_QFULL;
2773 				XS_SETERR(xs, HBA_NOERROR);
2774 			} else if (XS_NOERR(xs)) {
2775 				XS_SETERR(xs, HBA_BOTCH);
2776 			}
2777 			XS_RESID(xs) = XS_XFRLEN(xs);
2778 			break;
2779 		default:
2780 			isp_prt(isp, ISP_LOGWARN,
2781 			    "unhandled respose queue type 0x%x",
2782 			    sp->req_header.rqs_entry_type);
2783 			if (XS_NOERR(xs)) {
2784 				XS_SETERR(xs, HBA_BOTCH);
2785 			}
2786 			break;
2787 		}
2788 
2789 		/*
2790 		 * Free any dma resources. As a side effect, this may
2791 		 * also do any cache flushing necessary for data coherence.			 */
2792 		if (XS_XFRLEN(xs)) {
2793 			ISP_DMAFREE(isp, xs, sp->req_handle);
2794 		}
2795 
2796 		if (((isp->isp_dblev & (ISP_LOGDEBUG2|ISP_LOGDEBUG3))) ||
2797 		    ((isp->isp_dblev & ISP_LOGDEBUG1) && !XS_NOERR(xs))) {
2798 			char skey;
2799 			if (sp->req_state_flags & RQSF_GOT_SENSE) {
2800 				skey = XS_SNSKEY(xs) & 0xf;
2801 				if (skey < 10)
2802 					skey += '0';
2803 				else
2804 					skey += 'a';
2805 			} else if (*XS_STSP(xs) == SCSI_CHECK) {
2806 				skey = '?';
2807 			} else {
2808 				skey = '.';
2809 			}
2810 			isp_prt(isp, ISP_LOGALL, finmsg, XS_CHANNEL(xs),
2811 			    XS_TGT(xs), XS_LUN(xs), XS_XFRLEN(xs), XS_RESID(xs),
2812 			    *XS_STSP(xs), skey, XS_ERR(xs));
2813 		}
2814 
2815 		if (isp->isp_nactive > 0)
2816 		    isp->isp_nactive--;
2817 		complist[ndone++] = xs;	/* defer completion call until later */
2818 		MEMZERO(sp, sizeof (isphdr_t));
2819 		if (ndone == MAX_REQUESTQ_COMPLETIONS) {
2820 			break;
2821 		}
2822 	}
2823 
2824 	/*
2825 	 * If we looked at any commands, then it's valid to find out
2826 	 * what the outpointer is. It also is a trigger to update the
2827 	 * ISP's notion of what we've seen so far.
2828 	 */
2829 	if (nlooked) {
2830 		ISP_WRITE(isp, INMAILBOX5, optr);
2831 		isp->isp_reqodx = ISP_READ(isp, OUTMAILBOX4);
2832 	}
2833 
2834 	isp->isp_residx = optr;
2835 	for (i = 0; i < ndone; i++) {
2836 		xs = complist[i];
2837 		if (xs) {
2838 			isp_done(xs);
2839 		}
2840 	}
2841 	return (1);
2842 }
2843 
2844 /*
2845  * Support routines.
2846  */
2847 
2848 static int
2849 isp_parse_async(isp, mbox)
2850 	struct ispsoftc *isp;
2851 	int mbox;
2852 {
2853 	int bus;
2854 	u_int32_t fast_post_handle = 0;
2855 
2856 	if (IS_DUALBUS(isp)) {
2857 		bus = ISP_READ(isp, OUTMAILBOX6);
2858 	} else {
2859 		bus = 0;
2860 	}
2861 
2862 	switch (mbox) {
2863 	case ASYNC_BUS_RESET:
2864 		isp->isp_sendmarker |= (1 << bus);
2865 #ifdef	ISP_TARGET_MODE
2866 		isp_target_async(isp, bus, mbox);
2867 #endif
2868 		isp_async(isp, ISPASYNC_BUS_RESET, &bus);
2869 		break;
2870 	case ASYNC_SYSTEM_ERROR:
2871 		mbox = ISP_READ(isp, OUTMAILBOX1);
2872 		isp_prt(isp, ISP_LOGERR,
2873 		    "Internal FW Error @ RISC Addr 0x%x", mbox);
2874 		isp_reinit(isp);
2875 #ifdef	ISP_TARGET_MODE
2876 		isp_target_async(isp, bus, mbox);
2877 #endif
2878 		/* no point continuing after this */
2879 		return (-1);
2880 
2881 	case ASYNC_RQS_XFER_ERR:
2882 		isp_prt(isp, ISP_LOGERR, "Request Queue Transfer Error");
2883 		break;
2884 
2885 	case ASYNC_RSP_XFER_ERR:
2886 		isp_prt(isp, ISP_LOGERR, "Response Queue Transfer Error");
2887 		break;
2888 
2889 	case ASYNC_QWAKEUP:
2890 		/*
2891 		 * We've just been notified that the Queue has woken up.
2892 		 * We don't need to be chatty about this- just unlatch things
2893 		 * and move on.
2894 		 */
2895 		mbox = ISP_READ(isp, OUTMAILBOX4);
2896 		break;
2897 
2898 	case ASYNC_TIMEOUT_RESET:
2899 		isp_prt(isp, ISP_LOGWARN,
2900 		    "timeout initiated SCSI bus reset of bus %d\n", bus);
2901 		isp->isp_sendmarker |= (1 << bus);
2902 #ifdef	ISP_TARGET_MODE
2903 		isp_target_async(isp, bus, mbox);
2904 #endif
2905 		break;
2906 
2907 	case ASYNC_DEVICE_RESET:
2908 		isp_prt(isp, ISP_LOGINFO, "device reset on bus %d", bus);
2909 		isp->isp_sendmarker |= (1 << bus);
2910 #ifdef	ISP_TARGET_MODE
2911 		isp_target_async(isp, bus, mbox);
2912 #endif
2913 		break;
2914 
2915 	case ASYNC_EXTMSG_UNDERRUN:
2916 		isp_prt(isp, ISP_LOGWARN, "extended message underrun");
2917 		break;
2918 
2919 	case ASYNC_SCAM_INT:
2920 		isp_prt(isp, ISP_LOGINFO, "SCAM interrupt");
2921 		break;
2922 
2923 	case ASYNC_HUNG_SCSI:
2924 		isp_prt(isp, ISP_LOGERR,
2925 		    "stalled SCSI Bus after DATA Overrun");
2926 		/* XXX: Need to issue SCSI reset at this point */
2927 		break;
2928 
2929 	case ASYNC_KILLED_BUS:
2930 		isp_prt(isp, ISP_LOGERR, "SCSI Bus reset after DATA Overrun");
2931 		break;
2932 
2933 	case ASYNC_BUS_TRANSIT:
2934 		mbox = ISP_READ(isp, OUTMAILBOX2);
2935 		switch (mbox & 0x1c00) {
2936 		case SXP_PINS_LVD_MODE:
2937 			isp_prt(isp, ISP_LOGINFO, "Transition to LVD mode");
2938 			SDPARAM(isp)->isp_diffmode = 0;
2939 			SDPARAM(isp)->isp_ultramode = 0;
2940 			SDPARAM(isp)->isp_lvdmode = 1;
2941 			break;
2942 		case SXP_PINS_HVD_MODE:
2943 			isp_prt(isp, ISP_LOGINFO,
2944 			    "Transition to Differential mode");
2945 			SDPARAM(isp)->isp_diffmode = 1;
2946 			SDPARAM(isp)->isp_ultramode = 0;
2947 			SDPARAM(isp)->isp_lvdmode = 0;
2948 			break;
2949 		case SXP_PINS_SE_MODE:
2950 			isp_prt(isp, ISP_LOGINFO,
2951 			    "Transition to Single Ended mode");
2952 			SDPARAM(isp)->isp_diffmode = 0;
2953 			SDPARAM(isp)->isp_ultramode = 1;
2954 			SDPARAM(isp)->isp_lvdmode = 0;
2955 			break;
2956 		default:
2957 			isp_prt(isp, ISP_LOGWARN,
2958 			    "Transition to Unknown Mode 0x%x", mbox);
2959 			break;
2960 		}
2961 		/*
2962 		 * XXX: Set up to renegotiate again!
2963 		 */
2964 		/* Can only be for a 1080... */
2965 		isp->isp_sendmarker |= (1 << bus);
2966 		break;
2967 
2968 	case ASYNC_CMD_CMPLT:
2969 		fast_post_handle = (ISP_READ(isp, OUTMAILBOX2) << 16) |
2970 		    ISP_READ(isp, OUTMAILBOX1);
2971 		isp_prt(isp, ISP_LOGDEBUG3, "fast post completion of %u",
2972 		    fast_post_handle);
2973 		break;
2974 
2975 	case ASYNC_CTIO_DONE:
2976 #ifdef	ISP_TARGET_MODE
2977 		/*
2978 		 * Bus gets overloaded with the handle. Dual bus
2979 		 * cards don't put bus# into the handle.
2980 		 */
2981 		bus = (ISP_READ(isp, OUTMAILBOX2) << 16) |
2982 		    ISP_READ(isp, OUTMAILBOX1);
2983 		isp_target_async(isp, bus, mbox);
2984 #else
2985 		isp_prt(isp, ISP_LOGINFO, "Fast Posting CTIO done");
2986 #endif
2987 		break;
2988 
2989 	case ASYNC_LIP_OCCURRED:
2990 		FCPARAM(isp)->isp_lipseq =
2991 		    ISP_READ(isp, OUTMAILBOX1);
2992 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
2993 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
2994 		isp->isp_sendmarker = 1;
2995 		isp_mark_getpdb_all(isp);
2996 		isp_prt(isp, ISP_LOGINFO, "LIP occurred");
2997 #ifdef	ISP_TARGET_MODE
2998 		isp_target_async(isp, bus, mbox);
2999 #endif
3000 		break;
3001 
3002 	case ASYNC_LOOP_UP:
3003 		isp->isp_sendmarker = 1;
3004 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
3005 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
3006 		isp_mark_getpdb_all(isp);
3007 		isp_async(isp, ISPASYNC_LOOP_UP, NULL);
3008 #ifdef	ISP_TARGET_MODE
3009 		isp_target_async(isp, bus, mbox);
3010 #endif
3011 		break;
3012 
3013 	case ASYNC_LOOP_DOWN:
3014 		isp->isp_sendmarker = 1;
3015 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
3016 		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
3017 		isp_mark_getpdb_all(isp);
3018 		isp_async(isp, ISPASYNC_LOOP_DOWN, NULL);
3019 #ifdef	ISP_TARGET_MODE
3020 		isp_target_async(isp, bus, mbox);
3021 #endif
3022 		break;
3023 
3024 	case ASYNC_LOOP_RESET:
3025 		isp->isp_sendmarker = 1;
3026 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
3027 		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
3028 		isp_mark_getpdb_all(isp);
3029 		isp_prt(isp, ISP_LOGINFO, "Loop RESET");
3030 #ifdef	ISP_TARGET_MODE
3031 		isp_target_async(isp, bus, mbox);
3032 #endif
3033 		break;
3034 
3035 	case ASYNC_PDB_CHANGED:
3036 		isp->isp_sendmarker = 1;
3037 		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
3038 		isp_mark_getpdb_all(isp);
3039 		isp_prt(isp, ISP_LOGINFO, "Port Database Changed");
3040 		break;
3041 
3042 	case ASYNC_CHANGE_NOTIFY:
3043 		isp_mark_getpdb_all(isp);
3044 		/*
3045 		 * Not correct, but it will force us to rescan the loop.
3046 		 */
3047 		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
3048 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, NULL);
3049 		break;
3050 
3051 	case ASYNC_PTPMODE:
3052 		if (FCPARAM(isp)->isp_onfabric)
3053 			FCPARAM(isp)->isp_topo = TOPO_N_PORT;
3054 		else
3055 			FCPARAM(isp)->isp_topo = TOPO_F_PORT;
3056 		isp_mark_getpdb_all(isp);
3057 		isp->isp_sendmarker = 1;
3058 		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
3059 		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
3060 #ifdef	ISP_TARGET_MODE
3061 		isp_target_async(isp, bus, mbox);
3062 #endif
3063 		isp_prt(isp, ISP_LOGINFO, "Point-to-Point mode");
3064 		break;
3065 
3066 	case ASYNC_CONNMODE:
3067 		mbox = ISP_READ(isp, OUTMAILBOX1);
3068 		switch (mbox) {
3069 		case ISP_CONN_LOOP:
3070 			isp_prt(isp, ISP_LOGINFO, "Point-to-Point->Loop mode");
3071 			break;
3072 		case ISP_CONN_PTP:
3073 			isp_prt(isp, ISP_LOGINFO, "Loop->Point-to-Point mode");
3074 			break;
3075 		case ISP_CONN_BADLIP:
3076 			isp_prt(isp, ISP_LOGWARN,
3077 			    "Point-to-Point->Loop mode (1)");
3078 			break;
3079 		case ISP_CONN_FATAL:
3080 			isp_prt(isp, ISP_LOGERR, "FATAL CONNECTION ERROR");
3081 			isp_reinit(isp);
3082 #ifdef	ISP_TARGET_MODE
3083 			isp_target_async(isp, bus, ASYNC_SYSTEM_ERROR);
3084 #endif
3085 			/* no point continuing after this */
3086 			return (-1);
3087 
3088 		case ISP_CONN_LOOPBACK:
3089 			isp_prt(isp, ISP_LOGWARN,
3090 			    "Looped Back in Point-to-Point mode");
3091 		}
3092 		break;
3093 
3094 	default:
3095 		isp_prt(isp, ISP_LOGWARN, "Unknown Async Code 0x%x", mbox);
3096 		break;
3097 	}
3098 	return (fast_post_handle);
3099 }
3100 
3101 /*
3102  * Handle other response entries. A pointer to the request queue output
3103  * index is here in case we want to eat several entries at once, although
3104  * this is not used currently.
3105  */
3106 
3107 static int
3108 isp_handle_other_response(isp, sp, optrp)
3109 	struct ispsoftc *isp;
3110 	ispstatusreq_t *sp;
3111 	u_int16_t *optrp;
3112 {
3113 	switch (sp->req_header.rqs_entry_type) {
3114 	case RQSTYPE_STATUS_CONT:
3115 		isp_prt(isp, ISP_LOGINFO, "Ignored Continuation Response");
3116 		return (0);
3117 	case RQSTYPE_ATIO:
3118 	case RQSTYPE_CTIO:
3119 	case RQSTYPE_ENABLE_LUN:
3120 	case RQSTYPE_MODIFY_LUN:
3121 	case RQSTYPE_NOTIFY:
3122 	case RQSTYPE_NOTIFY_ACK:
3123 	case RQSTYPE_CTIO1:
3124 	case RQSTYPE_ATIO2:
3125 	case RQSTYPE_CTIO2:
3126 	case RQSTYPE_CTIO3:
3127 #ifdef	ISP_TARGET_MODE
3128 		return (isp_target_notify(isp, sp, optrp));
3129 #else
3130 		optrp = optrp;
3131 		/* FALLTHROUGH */
3132 #endif
3133 	case RQSTYPE_REQUEST:
3134 	default:
3135 		isp_prt(isp, ISP_LOGWARN, "Unhandled Response Type 0x%x",
3136 		    sp->req_header.rqs_entry_type);
3137 		return (-1);
3138 	}
3139 }
3140 
3141 static void
3142 isp_parse_status(isp, sp, xs)
3143 	struct ispsoftc *isp;
3144 	ispstatusreq_t *sp;
3145 	XS_T *xs;
3146 {
3147 	switch (sp->req_completion_status & 0xff) {
3148 	case RQCS_COMPLETE:
3149 		if (XS_NOERR(xs)) {
3150 			XS_SETERR(xs, HBA_NOERROR);
3151 		}
3152 		return;
3153 
3154 	case RQCS_INCOMPLETE:
3155 		if ((sp->req_state_flags & RQSF_GOT_TARGET) == 0) {
3156 			isp_prt(isp, ISP_LOGDEBUG1,
3157 			    "Selection Timeout for %d.%d.%d",
3158 			    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3159 			if (XS_NOERR(xs)) {
3160 				XS_SETERR(xs, HBA_SELTIMEOUT);
3161 			}
3162 			return;
3163 		}
3164 		isp_prt(isp, ISP_LOGERR,
3165 		    "command incomplete for %d.%d.%d, state 0x%x",
3166 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs),
3167 		    sp->req_state_flags);
3168 		break;
3169 
3170 	case RQCS_DMA_ERROR:
3171 		isp_prt(isp, ISP_LOGERR, "DMA error for command on %d.%d.%d",
3172 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3173 		break;
3174 
3175 	case RQCS_TRANSPORT_ERROR:
3176 	{
3177 		char buf[172];
3178 		buf[0] = 0;
3179 		STRNCAT(buf, "states=>", sizeof buf);
3180 		if (sp->req_state_flags & RQSF_GOT_BUS) {
3181 			STRNCAT(buf, " GOT_BUS", sizeof buf);
3182 		}
3183 		if (sp->req_state_flags & RQSF_GOT_TARGET) {
3184 			STRNCAT(buf, " GOT_TGT", sizeof buf);
3185 		}
3186 		if (sp->req_state_flags & RQSF_SENT_CDB) {
3187 			STRNCAT(buf, " SENT_CDB", sizeof buf);
3188 		}
3189 		if (sp->req_state_flags & RQSF_XFRD_DATA) {
3190 			STRNCAT(buf, " XFRD_DATA", sizeof buf);
3191 		}
3192 		if (sp->req_state_flags & RQSF_GOT_STATUS) {
3193 			STRNCAT(buf, " GOT_STS", sizeof buf);
3194 		}
3195 		if (sp->req_state_flags & RQSF_GOT_SENSE) {
3196 			STRNCAT(buf, " GOT_SNS", sizeof buf);
3197 		}
3198 		if (sp->req_state_flags & RQSF_XFER_COMPLETE) {
3199 			STRNCAT(buf, " XFR_CMPLT", sizeof buf);
3200 		}
3201 		STRNCAT(buf, "\nstatus=>", sizeof buf);
3202 		if (sp->req_status_flags & RQSTF_DISCONNECT) {
3203 			STRNCAT(buf, " Disconnect", sizeof buf);
3204 		}
3205 		if (sp->req_status_flags & RQSTF_SYNCHRONOUS) {
3206 			STRNCAT(buf, " Sync_xfr", sizeof buf);
3207 		}
3208 		if (sp->req_status_flags & RQSTF_PARITY_ERROR) {
3209 			STRNCAT(buf, " Parity", sizeof buf);
3210 		}
3211 		if (sp->req_status_flags & RQSTF_BUS_RESET) {
3212 			STRNCAT(buf, " Bus_Reset", sizeof buf);
3213 		}
3214 		if (sp->req_status_flags & RQSTF_DEVICE_RESET) {
3215 			STRNCAT(buf, " Device_Reset", sizeof buf);
3216 		}
3217 		if (sp->req_status_flags & RQSTF_ABORTED) {
3218 			STRNCAT(buf, " Aborted", sizeof buf);
3219 		}
3220 		if (sp->req_status_flags & RQSTF_TIMEOUT) {
3221 			STRNCAT(buf, " Timeout", sizeof buf);
3222 		}
3223 		if (sp->req_status_flags & RQSTF_NEGOTIATION) {
3224 			STRNCAT(buf, " Negotiation", sizeof buf);
3225 		}
3226 		isp_prt(isp, ISP_LOGERR, "%s", buf);
3227 		isp_prt(isp, ISP_LOGERR, "transport error for %d.%d.%d:\n%s",
3228 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), buf);
3229 		break;
3230 	}
3231 	case RQCS_RESET_OCCURRED:
3232 		isp_prt(isp, ISP_LOGWARN,
3233 		    "bus reset destroyed command for %d.%d.%d",
3234 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3235 		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
3236 		if (XS_NOERR(xs)) {
3237 			XS_SETERR(xs, HBA_BUSRESET);
3238 		}
3239 		return;
3240 
3241 	case RQCS_ABORTED:
3242 		isp_prt(isp, ISP_LOGERR, "command aborted for %d.%d.%d",
3243 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3244 		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
3245 		if (XS_NOERR(xs)) {
3246 			XS_SETERR(xs, HBA_ABORTED);
3247 		}
3248 		return;
3249 
3250 	case RQCS_TIMEOUT:
3251 		isp_prt(isp, ISP_LOGWARN, "command timed out for %d.%d.%d",
3252 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3253 		if (XS_NOERR(xs)) {
3254 			XS_SETERR(xs, HBA_CMDTIMEOUT);
3255 		}
3256 		return;
3257 
3258 	case RQCS_DATA_OVERRUN:
3259 		XS_RESID(xs) = sp->req_resid;
3260 		isp_prt(isp, ISP_LOGERR, "data overrun for command on %d.%d.%d",
3261 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3262 		if (XS_NOERR(xs)) {
3263 			XS_SETERR(xs, HBA_DATAOVR);
3264 		}
3265 		return;
3266 
3267 	case RQCS_COMMAND_OVERRUN:
3268 		isp_prt(isp, ISP_LOGERR,
3269 		    "command overrun for command on %d.%d.%d",
3270 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3271 		break;
3272 
3273 	case RQCS_STATUS_OVERRUN:
3274 		isp_prt(isp, ISP_LOGERR,
3275 		    "status overrun for command on %d.%d.%d",
3276 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3277 		break;
3278 
3279 	case RQCS_BAD_MESSAGE:
3280 		isp_prt(isp, ISP_LOGERR,
3281 		    "msg not COMMAND COMPLETE after status %d.%d.%d",
3282 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3283 		break;
3284 
3285 	case RQCS_NO_MESSAGE_OUT:
3286 		isp_prt(isp, ISP_LOGERR,
3287 		    "No MESSAGE OUT phase after selection on %d.%d.%d",
3288 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3289 		break;
3290 
3291 	case RQCS_EXT_ID_FAILED:
3292 		isp_prt(isp, ISP_LOGERR, "EXTENDED IDENTIFY failed %d.%d.%d",
3293 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3294 		break;
3295 
3296 	case RQCS_IDE_MSG_FAILED:
3297 		isp_prt(isp, ISP_LOGERR,
3298 		    "INITIATOR DETECTED ERROR rejected by %d.%d.%d",
3299 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3300 		break;
3301 
3302 	case RQCS_ABORT_MSG_FAILED:
3303 		isp_prt(isp, ISP_LOGERR, "ABORT OPERATION rejected by %d.%d.%d",
3304 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3305 		break;
3306 
3307 	case RQCS_REJECT_MSG_FAILED:
3308 		isp_prt(isp, ISP_LOGERR, "MESSAGE REJECT rejected by %d.%d.%d",
3309 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3310 		break;
3311 
3312 	case RQCS_NOP_MSG_FAILED:
3313 		isp_prt(isp, ISP_LOGERR, "NOP rejected by %d.%d.%d",
3314 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3315 		break;
3316 
3317 	case RQCS_PARITY_ERROR_MSG_FAILED:
3318 		isp_prt(isp, ISP_LOGERR,
3319 		    "MESSAGE PARITY ERROR rejected by %d.%d.%d",
3320 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3321 		break;
3322 
3323 	case RQCS_DEVICE_RESET_MSG_FAILED:
3324 		isp_prt(isp, ISP_LOGWARN,
3325 		    "BUS DEVICE RESET rejected by %d.%d.%d",
3326 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3327 		break;
3328 
3329 	case RQCS_ID_MSG_FAILED:
3330 		isp_prt(isp, ISP_LOGERR, "IDENTIFY rejected by %d.%d.%d",
3331 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3332 		break;
3333 
3334 	case RQCS_UNEXP_BUS_FREE:
3335 		isp_prt(isp, ISP_LOGERR, "%d.%d.%d had an unexpected bus free",
3336 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3337 		break;
3338 
3339 	case RQCS_DATA_UNDERRUN:
3340 		XS_RESID(xs) = sp->req_resid;
3341 		if (XS_NOERR(xs)) {
3342 			XS_SETERR(xs, HBA_NOERROR);
3343 		}
3344 		return;
3345 
3346 	case RQCS_XACT_ERR1:
3347 		isp_prt(isp, ISP_LOGERR, xact1, XS_CHANNEL(xs),
3348 		    XS_TGT(xs), XS_LUN(xs));
3349 		break;
3350 
3351 	case RQCS_XACT_ERR2:
3352 		isp_prt(isp, ISP_LOGERR, xact2,
3353 		    XS_LUN(xs), XS_TGT(xs), XS_CHANNEL(xs));
3354 		break;
3355 
3356 	case RQCS_XACT_ERR3:
3357 		isp_prt(isp, ISP_LOGERR, xact3, XS_TGT(xs),
3358 		    XS_LUN(xs), XS_CHANNEL(xs));
3359 		break;
3360 
3361 	case RQCS_BAD_ENTRY:
3362 		isp_prt(isp, ISP_LOGERR, "Invalid IOCB entry type detected");
3363 		break;
3364 
3365 	case RQCS_QUEUE_FULL:
3366 		isp_prt(isp, ISP_LOGDEBUG1,
3367 		    "internal queues full for %d.%d.%d status 0x%x", XS_TGT(xs),
3368 		    XS_LUN(xs), XS_CHANNEL(xs), *XS_STSP(xs));
3369 		/*
3370 		 * If QFULL or some other status byte is set, then this
3371 		 * isn't an error, per se.
3372 		 */
3373 		if (*XS_STSP(xs) != SCSI_GOOD && XS_NOERR(xs)) {
3374 			XS_SETERR(xs, HBA_NOERROR);
3375 			return;
3376 		}
3377 		break;
3378 
3379 	case RQCS_PHASE_SKIPPED:
3380 		isp_prt(isp, ISP_LOGERR, pskip,
3381 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
3382 		break;
3383 
3384 	case RQCS_ARQS_FAILED:
3385 		isp_prt(isp, ISP_LOGERR,
3386 		    "Auto Request Sense failed for %d.%d.%d",
3387 		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3388 		if (XS_NOERR(xs)) {
3389 			XS_SETERR(xs, HBA_ARQFAIL);
3390 		}
3391 		return;
3392 
3393 	case RQCS_WIDE_FAILED:
3394 		isp_prt(isp, ISP_LOGERR,
3395 		    "Wide Negotiation failed for %d.%d.%d",
3396 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
3397 		if (IS_SCSI(isp)) {
3398 			sdparam *sdp = isp->isp_param;
3399 			sdp += XS_CHANNEL(xs);
3400 			sdp->isp_devparam[XS_TGT(xs)].dev_flags &= ~DPARM_WIDE;
3401 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
3402 			isp->isp_update |= (1 << XS_CHANNEL(xs));
3403 		}
3404 		if (XS_NOERR(xs)) {
3405 			XS_SETERR(xs, HBA_NOERROR);
3406 		}
3407 		return;
3408 
3409 	case RQCS_SYNCXFER_FAILED:
3410 		isp_prt(isp, ISP_LOGERR,
3411 		    "SDTR Message failed for target %d.%d.%d",
3412 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
3413 		if (IS_SCSI(isp)) {
3414 			sdparam *sdp = isp->isp_param;
3415 			sdp += XS_CHANNEL(xs);
3416 			sdp->isp_devparam[XS_TGT(xs)].dev_flags &= ~DPARM_SYNC;
3417 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
3418 			isp->isp_update |= (1 << XS_CHANNEL(xs));
3419 		}
3420 		break;
3421 
3422 	case RQCS_LVD_BUSERR:
3423 		isp_prt(isp, ISP_LOGERR,
3424 		    "Bad LVD condition while talking to %d.%d.%d",
3425 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
3426 		break;
3427 
3428 	case RQCS_PORT_UNAVAILABLE:
3429 		/*
3430 		 * No such port on the loop. Moral equivalent of SELTIMEO
3431 		 */
3432 		isp_prt(isp, ISP_LOGINFO,
3433 		    "Port Unavailable for target %d", XS_TGT(xs));
3434 		if (XS_NOERR(xs)) {
3435 			XS_SETERR(xs, HBA_SELTIMEOUT);
3436 		}
3437 		return;
3438 
3439 	case RQCS_PORT_LOGGED_OUT:
3440 		/*
3441 		 * It was there (maybe)- treat as a selection timeout.
3442 		 */
3443 		isp_prt(isp, ISP_LOGINFO,
3444 		    "port logout for target %d", XS_TGT(xs));
3445 		if (XS_NOERR(xs)) {
3446 			XS_SETERR(xs, HBA_SELTIMEOUT);
3447 		}
3448 		return;
3449 
3450 	case RQCS_PORT_CHANGED:
3451 		isp_prt(isp, ISP_LOGWARN,
3452 		    "port changed for target %d", XS_TGT(xs));
3453 		if (XS_NOERR(xs)) {
3454 			XS_SETERR(xs, HBA_SELTIMEOUT);
3455 		}
3456 		return;
3457 
3458 	case RQCS_PORT_BUSY:
3459 		isp_prt(isp, ISP_LOGWARN,
3460 		    "port busy for target %d", XS_TGT(xs));
3461 		if (XS_NOERR(xs)) {
3462 			XS_SETERR(xs, HBA_TGTBSY);
3463 		}
3464 		return;
3465 
3466 	default:
3467 		isp_prt(isp, ISP_LOGERR, "Unknown Completion Status 0x%x",
3468 		    sp->req_completion_status);
3469 		break;
3470 	}
3471 	if (XS_NOERR(xs)) {
3472 		XS_SETERR(xs, HBA_BOTCH);
3473 	}
3474 }
3475 
3476 static void
3477 isp_fastpost_complete(isp, fph)
3478 	struct ispsoftc *isp;
3479 	u_int32_t fph;
3480 {
3481 	XS_T *xs;
3482 
3483 	if (fph < 1) {
3484 		return;
3485 	}
3486 	xs = isp_find_xs(isp, fph);
3487 	if (xs == NULL) {
3488 		isp_prt(isp, ISP_LOGWARN,
3489 		    "Command for fast post handle 0x%x not found", fph);
3490 		return;
3491 	}
3492 	isp_destroy_handle(isp, fph);
3493 
3494 	/*
3495 	 * Since we don't have a result queue entry item,
3496 	 * we must believe that SCSI status is zero and
3497 	 * that all data transferred.
3498 	 */
3499 	XS_SET_STATE_STAT(isp, xs, NULL);
3500 	XS_RESID(xs) = 0;
3501 	*XS_STSP(xs) = SCSI_GOOD;
3502 	if (XS_XFRLEN(xs)) {
3503 		ISP_DMAFREE(isp, xs, fph);
3504 	}
3505 	if (isp->isp_nactive)
3506 		isp->isp_nactive--;
3507 	isp_done(xs);
3508 }
3509 
3510 #define	HIBYT(x)			((x) >> 0x8)
3511 #define	LOBYT(x)			((x)  & 0xff)
3512 #define	ISPOPMAP(a, b)			(((a) << 8) | (b))
3513 static u_int16_t mbpscsi[] = {
3514 	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
3515 	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
3516 	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
3517 	ISPOPMAP(0x1f, 0x01),	/* 0x03: MBOX_DUMP_RAM */
3518 	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
3519 	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
3520 	ISPOPMAP(0x3f, 0x3f),	/* 0x06: MBOX_MAILBOX_REG_TEST */
3521 	ISPOPMAP(0x03, 0x07),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
3522 	ISPOPMAP(0x01, 0x0f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
3523 	ISPOPMAP(0x00, 0x00),	/* 0x09: */
3524 	ISPOPMAP(0x00, 0x00),	/* 0x0a: */
3525 	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
3526 	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
3527 	ISPOPMAP(0x00, 0x00),	/* 0x0d: */
3528 	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
3529 	ISPOPMAP(0x00, 0x00),	/* 0x0f: */
3530 	ISPOPMAP(0x1f, 0x1f),	/* 0x10: MBOX_INIT_REQ_QUEUE */
3531 	ISPOPMAP(0x3f, 0x3f),	/* 0x11: MBOX_INIT_RES_QUEUE */
3532 	ISPOPMAP(0x0f, 0x0f),	/* 0x12: MBOX_EXECUTE_IOCB */
3533 	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
3534 	ISPOPMAP(0x01, 0x3f),	/* 0x14: MBOX_STOP_FIRMWARE */
3535 	ISPOPMAP(0x0f, 0x0f),	/* 0x15: MBOX_ABORT */
3536 	ISPOPMAP(0x03, 0x03),	/* 0x16: MBOX_ABORT_DEVICE */
3537 	ISPOPMAP(0x07, 0x07),	/* 0x17: MBOX_ABORT_TARGET */
3538 	ISPOPMAP(0x07, 0x07),	/* 0x18: MBOX_BUS_RESET */
3539 	ISPOPMAP(0x03, 0x07),	/* 0x19: MBOX_STOP_QUEUE */
3540 	ISPOPMAP(0x03, 0x07),	/* 0x1a: MBOX_START_QUEUE */
3541 	ISPOPMAP(0x03, 0x07),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
3542 	ISPOPMAP(0x03, 0x07),	/* 0x1c: MBOX_ABORT_QUEUE */
3543 	ISPOPMAP(0x03, 0x4f),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
3544 	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
3545 	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
3546 	ISPOPMAP(0x01, 0x07),	/* 0x20: MBOX_GET_INIT_SCSI_ID */
3547 	ISPOPMAP(0x01, 0x07),	/* 0x21: MBOX_GET_SELECT_TIMEOUT */
3548 	ISPOPMAP(0x01, 0xc7),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
3549 	ISPOPMAP(0x01, 0x07),	/* 0x23: MBOX_GET_TAG_AGE_LIMIT */
3550 	ISPOPMAP(0x01, 0x03),	/* 0x24: MBOX_GET_CLOCK_RATE */
3551 	ISPOPMAP(0x01, 0x07),	/* 0x25: MBOX_GET_ACT_NEG_STATE */
3552 	ISPOPMAP(0x01, 0x07),	/* 0x26: MBOX_GET_ASYNC_DATA_SETUP_TIME */
3553 	ISPOPMAP(0x01, 0x07),	/* 0x27: MBOX_GET_PCI_PARAMS */
3554 	ISPOPMAP(0x03, 0x4f),	/* 0x28: MBOX_GET_TARGET_PARAMS */
3555 	ISPOPMAP(0x03, 0x0f),	/* 0x29: MBOX_GET_DEV_QUEUE_PARAMS */
3556 	ISPOPMAP(0x01, 0x07),	/* 0x2a: MBOX_GET_RESET_DELAY_PARAMS */
3557 	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
3558 	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
3559 	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
3560 	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
3561 	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
3562 	ISPOPMAP(0x03, 0x03),	/* 0x30: MBOX_SET_INIT_SCSI_ID */
3563 	ISPOPMAP(0x07, 0x07),	/* 0x31: MBOX_SET_SELECT_TIMEOUT */
3564 	ISPOPMAP(0xc7, 0xc7),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
3565 	ISPOPMAP(0x07, 0x07),	/* 0x33: MBOX_SET_TAG_AGE_LIMIT */
3566 	ISPOPMAP(0x03, 0x03),	/* 0x34: MBOX_SET_CLOCK_RATE */
3567 	ISPOPMAP(0x07, 0x07),	/* 0x35: MBOX_SET_ACT_NEG_STATE */
3568 	ISPOPMAP(0x07, 0x07),	/* 0x36: MBOX_SET_ASYNC_DATA_SETUP_TIME */
3569 	ISPOPMAP(0x07, 0x07),	/* 0x37: MBOX_SET_PCI_CONTROL_PARAMS */
3570 	ISPOPMAP(0x4f, 0x4f),	/* 0x38: MBOX_SET_TARGET_PARAMS */
3571 	ISPOPMAP(0x0f, 0x0f),	/* 0x39: MBOX_SET_DEV_QUEUE_PARAMS */
3572 	ISPOPMAP(0x07, 0x07),	/* 0x3a: MBOX_SET_RESET_DELAY_PARAMS */
3573 	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
3574 	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
3575 	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
3576 	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
3577 	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
3578 	ISPOPMAP(0x01, 0x03),	/* 0x40: MBOX_RETURN_BIOS_BLOCK_ADDR */
3579 	ISPOPMAP(0x3f, 0x01),	/* 0x41: MBOX_WRITE_FOUR_RAM_WORDS */
3580 	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_EXEC_BIOS_IOCB */
3581 	ISPOPMAP(0x00, 0x00),	/* 0x43: */
3582 	ISPOPMAP(0x00, 0x00),	/* 0x44: */
3583 	ISPOPMAP(0x03, 0x03),	/* 0x45: SET SYSTEM PARAMETER */
3584 	ISPOPMAP(0x01, 0x03),	/* 0x46: GET SYSTEM PARAMETER */
3585 	ISPOPMAP(0x00, 0x00),	/* 0x47: */
3586 	ISPOPMAP(0x01, 0xcf),	/* 0x48: GET SCAM CONFIGURATION */
3587 	ISPOPMAP(0xcf, 0xcf),	/* 0x49: SET SCAM CONFIGURATION */
3588 	ISPOPMAP(0x03, 0x03),	/* 0x4a: MBOX_SET_FIRMWARE_FEATURES */
3589 	ISPOPMAP(0x01, 0x03),	/* 0x4b: MBOX_GET_FIRMWARE_FEATURES */
3590 	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
3591 	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
3592 	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
3593 	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
3594 	ISPOPMAP(0xdf, 0xdf),	/* 0x50: LOAD RAM A64 */
3595 	ISPOPMAP(0xdf, 0xdf),	/* 0x51: DUMP RAM A64 */
3596 	ISPOPMAP(0xdf, 0xdf),	/* 0x52: INITIALIZE REQUEST QUEUE A64 */
3597 	ISPOPMAP(0xff, 0xff),	/* 0x53: INITIALIZE RESPONSE QUEUE A64 */
3598 	ISPOPMAP(0xcf, 0xff),	/* 0x54: EXECUTE IOCB A64 */
3599 	ISPOPMAP(0x03, 0x01),	/* 0x55: ENABLE TARGET MODE */
3600 	ISPOPMAP(0x00, 0x00),	/* 0x56: */
3601 	ISPOPMAP(0x00, 0x00),	/* 0x57: */
3602 	ISPOPMAP(0x00, 0x00),	/* 0x58: */
3603 	ISPOPMAP(0x00, 0x00),	/* 0x59: */
3604 	ISPOPMAP(0x03, 0x03),	/* 0x5a: SET DATA OVERRUN RECOVERY MODE */
3605 	ISPOPMAP(0x01, 0x03),	/* 0x5b: GET DATA OVERRUN RECOVERY MODE */
3606 	ISPOPMAP(0x0f, 0x0f),	/* 0x5c: SET HOST DATA */
3607 	ISPOPMAP(0x01, 0x01)	/* 0x5d: GET NOST DATA */
3608 };
3609 
3610 #ifndef	ISP_STRIPEED
3611 static char *scsi_mbcmd_names[] = {
3612 	"NO-OP",
3613 	"LOAD RAM",
3614 	"EXEC FIRMWARE",
3615 	"DUMP RAM",
3616 	"WRITE RAM WORD",
3617 	"READ RAM WORD",
3618 	"MAILBOX REG TEST",
3619 	"VERIFY CHECKSUM",
3620 	"ABOUT FIRMWARE",
3621 	NULL,
3622 	NULL,
3623 	NULL,
3624 	NULL,
3625 	NULL,
3626 	"CHECK FIRMWARE",
3627 	NULL,
3628 	"INIT REQUEST QUEUE",
3629 	"INIT RESULT QUEUE",
3630 	"EXECUTE IOCB",
3631 	"WAKE UP",
3632 	"STOP FIRMWARE",
3633 	"ABORT",
3634 	"ABORT DEVICE",
3635 	"ABORT TARGET",
3636 	"BUS RESET",
3637 	"STOP QUEUE",
3638 	"START QUEUE",
3639 	"SINGLE STEP QUEUE",
3640 	"ABORT QUEUE",
3641 	"GET DEV QUEUE STATUS",
3642 	NULL,
3643 	"GET FIRMWARE STATUS",
3644 	"GET INIT SCSI ID",
3645 	"GET SELECT TIMEOUT",
3646 	"GET RETRY COUNT",
3647 	"GET TAG AGE LIMIT",
3648 	"GET CLOCK RATE",
3649 	"GET ACT NEG STATE",
3650 	"GET ASYNC DATA SETUP TIME",
3651 	"GET PCI PARAMS",
3652 	"GET TARGET PARAMS",
3653 	"GET DEV QUEUE PARAMS",
3654 	"GET RESET DELAY PARAMS",
3655 	NULL,
3656 	NULL,
3657 	NULL,
3658 	NULL,
3659 	NULL,
3660 	"SET INIT SCSI ID",
3661 	"SET SELECT TIMEOUT",
3662 	"SET RETRY COUNT",
3663 	"SET TAG AGE LIMIT",
3664 	"SET CLOCK RATE",
3665 	"SET ACT NEG STATE",
3666 	"SET ASYNC DATA SETUP TIME",
3667 	"SET PCI CONTROL PARAMS",
3668 	"SET TARGET PARAMS",
3669 	"SET DEV QUEUE PARAMS",
3670 	"SET RESET DELAY PARAMS",
3671 	NULL,
3672 	NULL,
3673 	NULL,
3674 	NULL,
3675 	NULL,
3676 	"RETURN BIOS BLOCK ADDR",
3677 	"WRITE FOUR RAM WORDS",
3678 	"EXEC BIOS IOCB",
3679 	NULL,
3680 	NULL,
3681 	"SET SYSTEM PARAMETER",
3682 	"GET SYSTEM PARAMETER",
3683 	NULL,
3684 	"GET SCAM CONFIGURATION",
3685 	"SET SCAM CONFIGURATION",
3686 	"SET FIRMWARE FEATURES",
3687 	"GET FIRMWARE FEATURES",
3688 	NULL,
3689 	NULL,
3690 	NULL,
3691 	NULL,
3692 	"LOAD RAM A64",
3693 	"DUMP RAM A64",
3694 	"INITIALIZE REQUEST QUEUE A64",
3695 	"INITIALIZE RESPONSE QUEUE A64",
3696 	"EXECUTE IOCB A64",
3697 	"ENABLE TARGET MODE",
3698 	NULL,
3699 	NULL,
3700 	NULL,
3701 	NULL,
3702 	"SET DATA OVERRUN RECOVERY MODE",
3703 	"GET DATA OVERRUN RECOVERY MODE",
3704 	"SET HOST DATA",
3705 	"GET NOST DATA",
3706 };
3707 #endif
3708 
3709 static u_int16_t mbpfc[] = {
3710 	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
3711 	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
3712 	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
3713 	ISPOPMAP(0x1f, 0x01),	/* 0x03: MBOX_DUMP_RAM */
3714 	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
3715 	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
3716 	ISPOPMAP(0xff, 0xff),	/* 0x06: MBOX_MAILBOX_REG_TEST */
3717 	ISPOPMAP(0x03, 0x05),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
3718 	ISPOPMAP(0x01, 0x0f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
3719 	ISPOPMAP(0xdf, 0x01),	/* 0x09: LOAD RAM */
3720 	ISPOPMAP(0xdf, 0x01),	/* 0x0a: DUMP RAM */
3721 	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
3722 	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
3723 	ISPOPMAP(0x00, 0x00),	/* 0x0d: */
3724 	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
3725 	ISPOPMAP(0x00, 0x00),	/* 0x0f: */
3726 	ISPOPMAP(0x1f, 0x11),	/* 0x10: MBOX_INIT_REQ_QUEUE */
3727 	ISPOPMAP(0x2f, 0x21),	/* 0x11: MBOX_INIT_RES_QUEUE */
3728 	ISPOPMAP(0x0f, 0x01),	/* 0x12: MBOX_EXECUTE_IOCB */
3729 	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
3730 	ISPOPMAP(0x01, 0xff),	/* 0x14: MBOX_STOP_FIRMWARE */
3731 	ISPOPMAP(0x4f, 0x01),	/* 0x15: MBOX_ABORT */
3732 	ISPOPMAP(0x07, 0x01),	/* 0x16: MBOX_ABORT_DEVICE */
3733 	ISPOPMAP(0x07, 0x01),	/* 0x17: MBOX_ABORT_TARGET */
3734 	ISPOPMAP(0x03, 0x03),	/* 0x18: MBOX_BUS_RESET */
3735 	ISPOPMAP(0x07, 0x05),	/* 0x19: MBOX_STOP_QUEUE */
3736 	ISPOPMAP(0x07, 0x05),	/* 0x1a: MBOX_START_QUEUE */
3737 	ISPOPMAP(0x07, 0x05),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
3738 	ISPOPMAP(0x07, 0x05),	/* 0x1c: MBOX_ABORT_QUEUE */
3739 	ISPOPMAP(0x07, 0x03),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
3740 	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
3741 	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
3742 	ISPOPMAP(0x01, 0x4f),	/* 0x20: MBOX_GET_LOOP_ID */
3743 	ISPOPMAP(0x00, 0x00),	/* 0x21: */
3744 	ISPOPMAP(0x01, 0x07),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
3745 	ISPOPMAP(0x00, 0x00),	/* 0x23: */
3746 	ISPOPMAP(0x00, 0x00),	/* 0x24: */
3747 	ISPOPMAP(0x00, 0x00),	/* 0x25: */
3748 	ISPOPMAP(0x00, 0x00),	/* 0x26: */
3749 	ISPOPMAP(0x00, 0x00),	/* 0x27: */
3750 	ISPOPMAP(0x0f, 0x1),	/* 0x28: MBOX_GET_FIRMWARE_OPTIONS */
3751 	ISPOPMAP(0x03, 0x07),	/* 0x29: MBOX_GET_PORT_QUEUE_PARAMS */
3752 	ISPOPMAP(0x00, 0x00),	/* 0x2a: */
3753 	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
3754 	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
3755 	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
3756 	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
3757 	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
3758 	ISPOPMAP(0x00, 0x00),	/* 0x30: */
3759 	ISPOPMAP(0x00, 0x00),	/* 0x31: */
3760 	ISPOPMAP(0x07, 0x07),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
3761 	ISPOPMAP(0x00, 0x00),	/* 0x33: */
3762 	ISPOPMAP(0x00, 0x00),	/* 0x34: */
3763 	ISPOPMAP(0x00, 0x00),	/* 0x35: */
3764 	ISPOPMAP(0x00, 0x00),	/* 0x36: */
3765 	ISPOPMAP(0x00, 0x00),	/* 0x37: */
3766 	ISPOPMAP(0x0f, 0x01),	/* 0x38: MBOX_SET_FIRMWARE_OPTIONS */
3767 	ISPOPMAP(0x0f, 0x07),	/* 0x39: MBOX_SET_PORT_QUEUE_PARAMS */
3768 	ISPOPMAP(0x00, 0x00),	/* 0x3a: */
3769 	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
3770 	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
3771 	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
3772 	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
3773 	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
3774 	ISPOPMAP(0x03, 0x01),	/* 0x40: MBOX_LOOP_PORT_BYPASS */
3775 	ISPOPMAP(0x03, 0x01),	/* 0x41: MBOX_LOOP_PORT_ENABLE */
3776 	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_GET_RESOURCE_COUNTS */
3777 	ISPOPMAP(0x01, 0x01),	/* 0x43: MBOX_REQUEST_NON_PARTICIPATING_MODE */
3778 	ISPOPMAP(0x00, 0x00),	/* 0x44: */
3779 	ISPOPMAP(0x00, 0x00),	/* 0x45: */
3780 	ISPOPMAP(0x00, 0x00),	/* 0x46: */
3781 	ISPOPMAP(0xcf, 0x03),	/* 0x47: GET PORT_DATABASE ENHANCED */
3782 	ISPOPMAP(0x00, 0x00),	/* 0x48: */
3783 	ISPOPMAP(0x00, 0x00),	/* 0x49: */
3784 	ISPOPMAP(0x00, 0x00),	/* 0x4a: */
3785 	ISPOPMAP(0x00, 0x00),	/* 0x4b: */
3786 	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
3787 	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
3788 	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
3789 	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
3790 	ISPOPMAP(0x00, 0x00),	/* 0x50: */
3791 	ISPOPMAP(0x00, 0x00),	/* 0x51: */
3792 	ISPOPMAP(0x00, 0x00),	/* 0x52: */
3793 	ISPOPMAP(0x00, 0x00),	/* 0x53: */
3794 	ISPOPMAP(0xcf, 0x01),	/* 0x54: EXECUTE IOCB A64 */
3795 	ISPOPMAP(0x00, 0x00),	/* 0x55: */
3796 	ISPOPMAP(0x00, 0x00),	/* 0x56: */
3797 	ISPOPMAP(0x00, 0x00),	/* 0x57: */
3798 	ISPOPMAP(0x00, 0x00),	/* 0x58: */
3799 	ISPOPMAP(0x00, 0x00),	/* 0x59: */
3800 	ISPOPMAP(0x00, 0x00),	/* 0x5a: */
3801 	ISPOPMAP(0x00, 0x00),	/* 0x5b: */
3802 	ISPOPMAP(0x00, 0x00),	/* 0x5c: */
3803 	ISPOPMAP(0x00, 0x00),	/* 0x5d: */
3804 	ISPOPMAP(0x00, 0x00),	/* 0x5e: */
3805 	ISPOPMAP(0x00, 0x00),	/* 0x5f: */
3806 	ISPOPMAP(0xfd, 0x31),	/* 0x60: MBOX_INIT_FIRMWARE */
3807 	ISPOPMAP(0x00, 0x00),	/* 0x61: */
3808 	ISPOPMAP(0x01, 0x01),	/* 0x62: MBOX_INIT_LIP */
3809 	ISPOPMAP(0xcd, 0x03),	/* 0x63: MBOX_GET_FC_AL_POSITION_MAP */
3810 	ISPOPMAP(0xcf, 0x01),	/* 0x64: MBOX_GET_PORT_DB */
3811 	ISPOPMAP(0x07, 0x01),	/* 0x65: MBOX_CLEAR_ACA */
3812 	ISPOPMAP(0x07, 0x01),	/* 0x66: MBOX_TARGET_RESET */
3813 	ISPOPMAP(0x07, 0x01),	/* 0x67: MBOX_CLEAR_TASK_SET */
3814 	ISPOPMAP(0x07, 0x01),	/* 0x68: MBOX_ABORT_TASK_SET */
3815 	ISPOPMAP(0x01, 0x07),	/* 0x69: MBOX_GET_FW_STATE */
3816 	ISPOPMAP(0x03, 0xcf),	/* 0x6a: MBOX_GET_PORT_NAME */
3817 	ISPOPMAP(0xcf, 0x01),	/* 0x6b: MBOX_GET_LINK_STATUS */
3818 	ISPOPMAP(0x0f, 0x01),	/* 0x6c: MBOX_INIT_LIP_RESET */
3819 	ISPOPMAP(0x00, 0x00),	/* 0x6d: */
3820 	ISPOPMAP(0xcf, 0x03),	/* 0x6e: MBOX_SEND_SNS */
3821 	ISPOPMAP(0x0f, 0x07),	/* 0x6f: MBOX_FABRIC_LOGIN */
3822 	ISPOPMAP(0x03, 0x01),	/* 0x70: MBOX_SEND_CHANGE_REQUEST */
3823 	ISPOPMAP(0x03, 0x03),	/* 0x71: MBOX_FABRIC_LOGOUT */
3824 	ISPOPMAP(0x0f, 0x0f),	/* 0x72: MBOX_INIT_LIP_LOGIN */
3825 	ISPOPMAP(0x00, 0x00),	/* 0x73: */
3826 	ISPOPMAP(0x07, 0x01),	/* 0x74: LOGIN LOOP PORT */
3827 	ISPOPMAP(0xcf, 0x03),	/* 0x75: GET PORT/NODE NAME LIST */
3828 	ISPOPMAP(0x4f, 0x01),	/* 0x76: SET VENDOR ID */
3829 	ISPOPMAP(0xcd, 0x01),	/* 0x77: INITIALIZE IP MAILBOX */
3830 	ISPOPMAP(0x00, 0x00),	/* 0x78: */
3831 	ISPOPMAP(0x00, 0x00),	/* 0x79: */
3832 	ISPOPMAP(0x00, 0x00),	/* 0x7a: */
3833 	ISPOPMAP(0x00, 0x00),	/* 0x7b: */
3834 	ISPOPMAP(0x4f, 0x03),	/* 0x7c: Get ID List */
3835 	ISPOPMAP(0xcf, 0x01),	/* 0x7d: SEND LFA */
3836 	ISPOPMAP(0x07, 0x01)	/* 0x7e: Lun RESET */
3837 };
3838 
3839 #ifndef	ISP_STRIPPED
3840 static char *fc_mbcmd_names[] = {
3841 	"NO-OP",
3842 	"LOAD RAM",
3843 	"EXEC FIRMWARE",
3844 	"DUMP RAM",
3845 	"WRITE RAM WORD",
3846 	"READ RAM WORD",
3847 	"MAILBOX REG TEST",
3848 	"VERIFY CHECKSUM",
3849 	"ABOUT FIRMWARE",
3850 	"LOAD RAM",
3851 	"DUMP RAM",
3852 	NULL,
3853 	NULL,
3854 	NULL,
3855 	"CHECK FIRMWARE",
3856 	NULL,
3857 	"INIT REQUEST QUEUE",
3858 	"INIT RESULT QUEUE",
3859 	"EXECUTE IOCB",
3860 	"WAKE UP",
3861 	"STOP FIRMWARE",
3862 	"ABORT",
3863 	"ABORT DEVICE",
3864 	"ABORT TARGET",
3865 	"BUS RESET",
3866 	"STOP QUEUE",
3867 	"START QUEUE",
3868 	"SINGLE STEP QUEUE",
3869 	"ABORT QUEUE",
3870 	"GET DEV QUEUE STATUS",
3871 	NULL,
3872 	"GET FIRMWARE STATUS",
3873 	"GET LOOP ID",
3874 	NULL,
3875 	"GET RETRY COUNT",
3876 	NULL,
3877 	NULL,
3878 	NULL,
3879 	NULL,
3880 	NULL,
3881 	"GET FIRMWARE OPTIONS",
3882 	"GET PORT QUEUE PARAMS",
3883 	NULL,
3884 	NULL,
3885 	NULL,
3886 	NULL,
3887 	NULL,
3888 	NULL,
3889 	NULL,
3890 	NULL,
3891 	"SET RETRY COUNT",
3892 	NULL,
3893 	NULL,
3894 	NULL,
3895 	NULL,
3896 	NULL,
3897 	"SET FIRMWARE OPTIONS",
3898 	"SET PORT QUEUE PARAMS",
3899 	NULL,
3900 	NULL,
3901 	NULL,
3902 	NULL,
3903 	NULL,
3904 	NULL,
3905 	"LOOP PORT BYPASS",
3906 	"LOOP PORT ENABLE",
3907 	"GET RESOURCE COUNTS",
3908 	"REQUEST NON PARTICIPATING MODE",
3909 	NULL,
3910 	NULL,
3911 	NULL,
3912 	"GET PORT DATABASE,, ENHANCED",
3913 	NULL,
3914 	NULL,
3915 	NULL,
3916 	NULL,
3917 	NULL,
3918 	NULL,
3919 	NULL,
3920 	NULL,
3921 	NULL,
3922 	NULL,
3923 	NULL,
3924 	NULL,
3925 	"EXECUTE IOCB A64",
3926 	NULL,
3927 	NULL,
3928 	NULL,
3929 	NULL,
3930 	NULL,
3931 	NULL,
3932 	NULL,
3933 	NULL,
3934 	NULL,
3935 	NULL,
3936 	NULL,
3937 	"INIT FIRMWARE",
3938 	NULL,
3939 	"INIT LIP",
3940 	"GET FC-AL POSITION MAP",
3941 	"GET PORT DATABASE",
3942 	"CLEAR ACA",
3943 	"TARGET RESET",
3944 	"CLEAR TASK SET",
3945 	"ABORT TASK SET",
3946 	"GET FW STATE",
3947 	"GET PORT NAME",
3948 	"GET LINK STATUS",
3949 	"INIT LIP RESET",
3950 	NULL,
3951 	"SEND SNS",
3952 	"FABRIC LOGIN",
3953 	"SEND CHANGE REQUEST",
3954 	"FABRIC LOGOUT",
3955 	"INIT LIP LOGIN",
3956 	NULL,
3957 	"LOGIN LOOP PORT",
3958 	"GET PORT/NODE NAME LIST",
3959 	"SET VENDOR ID",
3960 	"INITIALIZE IP MAILBOX",
3961 	NULL,
3962 	NULL,
3963 	NULL,
3964 	NULL,
3965 	"Get ID List",
3966 	"SEND LFA",
3967 	"Lun RESET"
3968 };
3969 #endif
3970 
3971 static void
3972 isp_mboxcmd(isp, mbp, logmask)
3973 	struct ispsoftc *isp;
3974 	mbreg_t *mbp;
3975 	int logmask;
3976 {
3977 	char *cname, *xname, tname[16], mname[16];
3978 	unsigned int lim, ibits, obits, box, opcode;
3979 	u_int16_t *mcp;
3980 
3981 	if (IS_FC(isp)) {
3982 		mcp = mbpfc;
3983 		lim = (sizeof (mbpfc) / sizeof (mbpfc[0]));
3984 	} else {
3985 		mcp = mbpscsi;
3986 		lim = (sizeof (mbpscsi) / sizeof (mbpscsi[0]));
3987 	}
3988 
3989 	if ((opcode = mbp->param[0]) >= lim) {
3990 		mbp->param[0] = MBOX_INVALID_COMMAND;
3991 		isp_prt(isp, ISP_LOGERR, "Unknown Command 0x%x", opcode);
3992 		return;
3993 	}
3994 
3995 	ibits = HIBYT(mcp[opcode]) & NMBOX_BMASK(isp);
3996 	obits = LOBYT(mcp[opcode]) & NMBOX_BMASK(isp);
3997 
3998 	if (ibits == 0 && obits == 0) {
3999 		mbp->param[0] = MBOX_COMMAND_PARAM_ERROR;
4000 		isp_prt(isp, ISP_LOGERR, "no parameters for 0x%x", opcode);
4001 		return;
4002 	}
4003 
4004 	/*
4005 	 * Get exclusive usage of mailbox registers.
4006 	 */
4007 	MBOX_ACQUIRE(isp);
4008 
4009 	for (box = 0; box < MAX_MAILBOX; box++) {
4010 		if (ibits & (1 << box)) {
4011 			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
4012 		}
4013 		isp->isp_mboxtmp[box] = mbp->param[box] = 0;
4014 	}
4015 
4016 	/*
4017 	 * We assume that we can't overwrite a previous command.
4018 	 */
4019 	isp->isp_mboxbsy = obits;
4020 
4021 	/*
4022 	 * Set Host Interrupt condition so that RISC will pick up mailbox regs.
4023 	 */
4024 	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
4025 
4026 	/*
4027 	 * While we haven't finished the command, spin our wheels here.
4028 	 */
4029 	MBOX_WAIT_COMPLETE(isp);
4030 
4031 	/*
4032 	 * Copy back output registers.
4033 	 */
4034 	for (box = 0; box < MAX_MAILBOX; box++) {
4035 		if (obits & (1 << box)) {
4036 			mbp->param[box] = isp->isp_mboxtmp[box];
4037 		}
4038 	}
4039 
4040 	MBOX_RELEASE(isp);
4041 
4042 	if (logmask == 0 || opcode == MBOX_EXEC_FIRMWARE) {
4043 		return;
4044 	}
4045 #ifdef	ISP_STRIPPED
4046 	cname = NULL;
4047 #else
4048 	cname = (IS_FC(isp))? fc_mbcmd_names[opcode] : scsi_mbcmd_names[opcode];
4049 #endif
4050 	if (cname == NULL) {
4051 		SNPRINTF(cname, sizeof tname, "opcode %x", opcode);
4052 	}
4053 
4054 	/*
4055 	 * Just to be chatty here...
4056 	 */
4057 	xname = NULL;
4058 	switch (mbp->param[0]) {
4059 	case MBOX_COMMAND_COMPLETE:
4060 		break;
4061 	case MBOX_INVALID_COMMAND:
4062 		if (logmask & MBLOGMASK(MBOX_COMMAND_COMPLETE))
4063 			xname = "INVALID COMMAND";
4064 		break;
4065 	case MBOX_HOST_INTERFACE_ERROR:
4066 		if (logmask & MBLOGMASK(MBOX_HOST_INTERFACE_ERROR))
4067 			xname = "HOST INTERFACE ERROR";
4068 		break;
4069 	case MBOX_TEST_FAILED:
4070 		if (logmask & MBLOGMASK(MBOX_TEST_FAILED))
4071 			xname = "TEST FAILED";
4072 		break;
4073 	case MBOX_COMMAND_ERROR:
4074 		if (logmask & MBLOGMASK(MBOX_COMMAND_ERROR))
4075 			xname = "COMMAND ERROR";
4076 		break;
4077 	case MBOX_COMMAND_PARAM_ERROR:
4078 		if (logmask & MBLOGMASK(MBOX_COMMAND_PARAM_ERROR))
4079 			xname = "COMMAND PARAMETER ERROR";
4080 		break;
4081 	case MBOX_LOOP_ID_USED:
4082 		if (logmask & MBLOGMASK(MBOX_LOOP_ID_USED))
4083 			xname = "LOOP ID ALREADY IN USE";
4084 		break;
4085 	case MBOX_PORT_ID_USED:
4086 		if (logmask & MBLOGMASK(MBOX_PORT_ID_USED))
4087 			xname = "PORT ID ALREADY IN USE";
4088 		break;
4089 	case MBOX_ALL_IDS_USED:
4090 		if (logmask & MBLOGMASK(MBOX_ALL_IDS_USED))
4091 			xname = "ALL LOOP IDS IN USE";
4092 		break;
4093 	case 0:		/* special case */
4094 		xname = "TIMEOUT";
4095 		break;
4096 	default:
4097 		SNPRINTF(mname, sizeof mname, "error 0x%x", mbp->param[0]);
4098 		xname = mname;
4099 		break;
4100 	}
4101 	if (xname)
4102 		isp_prt(isp, ISP_LOGALL, "Mailbox Command '%s' failed (%s)",
4103 		    cname, xname);
4104 }
4105 
4106 static void
4107 isp_fw_state(isp)
4108 	struct ispsoftc *isp;
4109 {
4110 	if (IS_FC(isp)) {
4111 		mbreg_t mbs;
4112 		fcparam *fcp = isp->isp_param;
4113 
4114 		mbs.param[0] = MBOX_GET_FW_STATE;
4115 		isp_mboxcmd(isp, &mbs, MBLOGALL);
4116 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE)
4117 			fcp->isp_fwstate = mbs.param[1];
4118 	}
4119 }
4120 
4121 static void
4122 isp_update(isp)
4123 	struct ispsoftc *isp;
4124 {
4125 	int bus, upmask;
4126 
4127 	for (bus = 0, upmask = isp->isp_update; upmask != 0; bus++) {
4128 		if (upmask & (1 << bus)) {
4129 			isp_update_bus(isp, bus);
4130 		}
4131 		upmask &= ~(1 << bus);
4132 	}
4133 }
4134 
4135 static void
4136 isp_update_bus(isp, bus)
4137 	struct ispsoftc *isp;
4138 	int bus;
4139 {
4140 	int tgt;
4141 	mbreg_t mbs;
4142 	sdparam *sdp;
4143 
4144 	isp->isp_update &= ~(1 << bus);
4145 	if (IS_FC(isp)) {
4146 		/*
4147 		 * There are no 'per-bus' settings for Fibre Channel.
4148 		 */
4149 		return;
4150 	}
4151 	sdp = isp->isp_param;
4152 	sdp += bus;
4153 
4154 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
4155 		u_int16_t flags, period, offset;
4156 		int get;
4157 
4158 		if (sdp->isp_devparam[tgt].dev_enable == 0) {
4159 			sdp->isp_devparam[tgt].dev_update = 0;
4160 			sdp->isp_devparam[tgt].dev_refresh = 0;
4161 			isp_prt(isp, ISP_LOGDEBUG1,
4162 	 		    "skipping target %d bus %d update", tgt, bus);
4163 			continue;
4164 		}
4165 		/*
4166 		 * If the goal is to update the status of the device,
4167 		 * take what's in dev_flags and try and set the device
4168 		 * toward that. Otherwise, if we're just refreshing the
4169 		 * current device state, get the current parameters.
4170 		 */
4171 
4172 		/*
4173 		 * Refresh overrides set
4174 		 */
4175 		if (sdp->isp_devparam[tgt].dev_refresh) {
4176 			mbs.param[0] = MBOX_GET_TARGET_PARAMS;
4177 			sdp->isp_devparam[tgt].dev_refresh = 0;
4178 			get = 1;
4179 		} else if (sdp->isp_devparam[tgt].dev_update) {
4180 			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
4181 			/*
4182 			 * Make sure dev_flags has "Renegotiate on Error"
4183 			 * on and "Freeze Queue on Error" off.
4184 			 */
4185 			sdp->isp_devparam[tgt].dev_flags |= DPARM_RENEG;
4186 			sdp->isp_devparam[tgt].dev_flags &= ~DPARM_QFRZ;
4187 
4188 			mbs.param[2] = sdp->isp_devparam[tgt].dev_flags;
4189 
4190 			/*
4191 			 * Insist that PARITY must be enabled
4192 			 * if SYNC or WIDE is enabled.
4193 			 */
4194 			if ((mbs.param[2] & (DPARM_SYNC|DPARM_WIDE)) != 0) {
4195 				mbs.param[2] |= DPARM_PARITY;
4196 			}
4197 
4198 			if ((mbs.param[2] & DPARM_SYNC) == 0) {
4199 				mbs.param[3] = 0;
4200 			} else {
4201 				mbs.param[3] =
4202 				    (sdp->isp_devparam[tgt].sync_offset << 8) |
4203 				    (sdp->isp_devparam[tgt].sync_period);
4204 			}
4205 			/*
4206 			 * A command completion later that has
4207 			 * RQSTF_NEGOTIATION set canl cause
4208 			 * the dev_refresh/announce cycle also.
4209 			 &
4210 			 *
4211 			 * Note: It is really important to update our current
4212 			 * flags with at least the state of TAG capabilities-
4213 			 * otherwise we might try and send a tagged command
4214 			 * when we have it all turned off. So change it here
4215 			 * to say that current already matches goal.
4216 			 */
4217 			sdp->isp_devparam[tgt].cur_dflags &= ~DPARM_TQING;
4218 			sdp->isp_devparam[tgt].cur_dflags |=
4219 			    (sdp->isp_devparam[tgt].dev_flags & DPARM_TQING);
4220 			isp_prt(isp, ISP_LOGDEBUG2,
4221 			    "bus %d set tgt %d flags 0x%x off 0x%x period 0x%x",
4222 			    bus, tgt, mbs.param[2], mbs.param[3] >> 8,
4223 			    mbs.param[3] & 0xff);
4224 			sdp->isp_devparam[tgt].dev_update = 0;
4225 			sdp->isp_devparam[tgt].dev_refresh = 1;
4226 			get = 0;
4227 		} else {
4228 			continue;
4229 		}
4230 		mbs.param[1] = (bus << 15) | (tgt << 8) ;
4231 		isp_mboxcmd(isp, &mbs, MBLOGALL);
4232 		if (get == 0) {
4233 			isp->isp_sendmarker |= (1 << bus);
4234 			continue;
4235 		}
4236 		flags = mbs.param[2];
4237 		period = mbs.param[3] & 0xff;
4238 		offset = mbs.param[3] >> 8;
4239 		sdp->isp_devparam[tgt].cur_dflags = flags;
4240 		sdp->isp_devparam[tgt].cur_period = period;
4241 		sdp->isp_devparam[tgt].cur_offset = offset;
4242 		get = (bus << 16) | tgt;
4243 		(void) isp_async(isp, ISPASYNC_NEW_TGT_PARAMS, &get);
4244 	}
4245 
4246 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
4247 		if (sdp->isp_devparam[tgt].dev_update ||
4248 		    sdp->isp_devparam[tgt].dev_refresh) {
4249 			isp->isp_update |= (1 << bus);
4250 			break;
4251 		}
4252 	}
4253 }
4254 
4255 static void
4256 isp_setdfltparm(isp, channel)
4257 	struct ispsoftc *isp;
4258 	int channel;
4259 {
4260 	int tgt;
4261 	mbreg_t mbs;
4262 	sdparam *sdp;
4263 
4264 	if (IS_FC(isp)) {
4265 		fcparam *fcp = (fcparam *) isp->isp_param;
4266 		fcp += channel;
4267 		if (fcp->isp_gotdparms) {
4268 			return;
4269 		}
4270 		fcp->isp_gotdparms = 1;
4271 		fcp->isp_maxfrmlen = ICB_DFLT_FRMLEN;
4272 		fcp->isp_maxalloc = ICB_DFLT_ALLOC;
4273 		fcp->isp_execthrottle = ISP_EXEC_THROTTLE;
4274 		fcp->isp_retry_delay = ICB_DFLT_RDELAY;
4275 		fcp->isp_retry_count = ICB_DFLT_RCOUNT;
4276 		/* Platform specific.... */
4277 		fcp->isp_loopid = DEFAULT_LOOPID(isp);
4278 		fcp->isp_nodewwn = DEFAULT_NODEWWN(isp);
4279 		fcp->isp_portwwn = DEFAULT_PORTWWN(isp);
4280 		fcp->isp_fwoptions = 0;
4281 		fcp->isp_fwoptions |= ICBOPT_FAIRNESS;
4282 		fcp->isp_fwoptions |= ICBOPT_PDBCHANGE_AE;
4283 		fcp->isp_fwoptions |= ICBOPT_HARD_ADDRESS;
4284 #ifndef	ISP_NO_FASTPOST_FC
4285 		fcp->isp_fwoptions |= ICBOPT_FAST_POST;
4286 #endif
4287 		if (isp->isp_confopts & ISP_CFG_FULL_DUPLEX)
4288 			fcp->isp_fwoptions |= ICBOPT_FULL_DUPLEX;
4289 
4290 		/*
4291 		 * Make sure this is turned off now until we get
4292 		 * extended options from NVRAM
4293 		 */
4294 		fcp->isp_fwoptions &= ~ICBOPT_EXTENDED;
4295 
4296 		/*
4297 		 * Now try and read NVRAM
4298 		 */
4299 		if ((isp->isp_confopts & (ISP_CFG_NONVRAM|ISP_CFG_OWNWWN)) ||
4300 		    (isp_read_nvram(isp))) {
4301 			isp_prt(isp, ISP_LOGINFO,
4302 			    "Node WWN 0x%08x%08x, Port WWN 0x%08x%08x",
4303 			    (u_int32_t) (fcp->isp_nodewwn >> 32),
4304 			    (u_int32_t) (fcp->isp_nodewwn & 0xffffffff),
4305 			    (u_int32_t) (fcp->isp_portwwn >> 32),
4306 			    (u_int32_t) (fcp->isp_portwwn & 0xffffffff));
4307 		}
4308 		return;
4309 	}
4310 
4311 	sdp = (sdparam *) isp->isp_param;
4312 	sdp += channel;
4313 
4314 	/*
4315 	 * Been there, done that, got the T-shirt...
4316 	 */
4317 	if (sdp->isp_gotdparms) {
4318 		return;
4319 	}
4320 	sdp->isp_gotdparms = 1;
4321 
4322 	/*
4323 	 * If we've not been told to avoid reading NVRAM, try and read it.
4324 	 * If we're successful reading it, we can return since NVRAM will
4325 	 * tell us the right thing to do. Otherwise, establish some reasonable
4326 	 * defaults.
4327 	 */
4328 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
4329 		if (isp_read_nvram(isp) == 0) {
4330 			return;
4331 		}
4332 	}
4333 
4334 	/*
4335 	 * Now try and see whether we have specific values for them.
4336 	 */
4337 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
4338 		mbs.param[0] = MBOX_GET_ACT_NEG_STATE;
4339 		isp_mboxcmd(isp, &mbs, MBLOGALL);
4340 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
4341 			sdp->isp_req_ack_active_neg = 1;
4342 			sdp->isp_data_line_active_neg = 1;
4343 		} else {
4344 			sdp->isp_req_ack_active_neg =
4345 			    (mbs.param[1+channel] >> 4) & 0x1;
4346 			sdp->isp_data_line_active_neg =
4347 			    (mbs.param[1+channel] >> 5) & 0x1;
4348 		}
4349 	} else {
4350 		sdp->isp_req_ack_active_neg = 1;
4351 		sdp->isp_data_line_active_neg = 1;
4352 	}
4353 
4354 	isp_prt(isp, ISP_LOGDEBUG1,
4355 	    "defaulting bus %d REQ/ACK Active Negation is %d",
4356 	    channel, sdp->isp_req_ack_active_neg);
4357 	isp_prt(isp, ISP_LOGDEBUG1,
4358 	    "defaulting bus %d DATA Active Negation is %d",
4359 	    channel, sdp->isp_data_line_active_neg);
4360 
4361 	/*
4362 	 * The trick here is to establish a default for the default (honk!)
4363 	 * state (dev_flags). Then try and get the current status from
4364 	 * the card to fill in the current state. We don't, in fact, set
4365 	 * the default to the SAFE default state- that's not the goal state.
4366 	 */
4367 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
4368 		sdp->isp_devparam[tgt].cur_offset = 0;
4369 		sdp->isp_devparam[tgt].cur_period = 0;
4370 		sdp->isp_devparam[tgt].dev_flags = DPARM_DEFAULT;
4371 		sdp->isp_devparam[tgt].cur_dflags = 0;
4372 		/*
4373 		 * We default to Wide/Fast for versions less than a 1040
4374 		 * (unless it's SBus).
4375 		 */
4376 		if ((isp->isp_bustype == ISP_BT_SBUS &&
4377 		    isp->isp_type < ISP_HA_SCSI_1020A) ||
4378 		    (isp->isp_bustype == ISP_BT_PCI &&
4379 		    isp->isp_type < ISP_HA_SCSI_1040) ||
4380 		    (isp->isp_clock && isp->isp_clock < 60) ||
4381 		    (sdp->isp_ultramode == 0)) {
4382 			sdp->isp_devparam[tgt].sync_offset =
4383 			    ISP_10M_SYNCPARMS >> 8;
4384 			sdp->isp_devparam[tgt].sync_period =
4385 			    ISP_10M_SYNCPARMS & 0xff;
4386 		} else if (IS_ULTRA3(isp)) {
4387 			sdp->isp_devparam[tgt].sync_offset =
4388 			    ISP_80M_SYNCPARMS >> 8;
4389 			sdp->isp_devparam[tgt].sync_period =
4390 			    ISP_80M_SYNCPARMS & 0xff;
4391 		} else if (IS_ULTRA2(isp)) {
4392 			sdp->isp_devparam[tgt].sync_offset =
4393 			    ISP_40M_SYNCPARMS >> 8;
4394 			sdp->isp_devparam[tgt].sync_period =
4395 			    ISP_40M_SYNCPARMS & 0xff;
4396 		} else if (IS_1240(isp)) {
4397 			sdp->isp_devparam[tgt].sync_offset =
4398 			    ISP_20M_SYNCPARMS >> 8;
4399 			sdp->isp_devparam[tgt].sync_period =
4400 			    ISP_20M_SYNCPARMS & 0xff;
4401 		} else {
4402 			sdp->isp_devparam[tgt].sync_offset =
4403 			    ISP_20M_SYNCPARMS_1040 >> 8;
4404 			sdp->isp_devparam[tgt].sync_period =
4405 			    ISP_20M_SYNCPARMS_1040 & 0xff;
4406 		}
4407 
4408 		/*
4409 		 * Don't get current target parameters if we've been
4410 		 * told not to use NVRAM- it's really the same thing.
4411 		 */
4412 		if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
4413 
4414 			mbs.param[0] = MBOX_GET_TARGET_PARAMS;
4415 			mbs.param[1] = tgt << 8;
4416 			isp_mboxcmd(isp, &mbs, MBLOGALL);
4417 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
4418 				continue;
4419 			}
4420 			sdp->isp_devparam[tgt].cur_dflags = mbs.param[2];
4421 			sdp->isp_devparam[tgt].dev_flags = mbs.param[2];
4422 			sdp->isp_devparam[tgt].cur_period = mbs.param[3] & 0xff;
4423 			sdp->isp_devparam[tgt].cur_offset = mbs.param[3] >> 8;
4424 
4425 			/*
4426 			 * The maximum period we can really see
4427 			 * here is 100 (decimal), or 400 ns.
4428 			 * For some unknown reason we sometimes
4429 			 * get back wildass numbers from the
4430 			 * boot device's parameters (alpha only).
4431 			 */
4432 			if ((mbs.param[3] & 0xff) <= 0x64) {
4433 				sdp->isp_devparam[tgt].sync_period =
4434 				    mbs.param[3] & 0xff;
4435 				sdp->isp_devparam[tgt].sync_offset =
4436 				    mbs.param[3] >> 8;
4437 			}
4438 
4439 			/*
4440 			 * It is not safe to run Ultra Mode with a clock < 60.
4441 			 */
4442 			if (((isp->isp_clock && isp->isp_clock < 60) ||
4443 			    (isp->isp_type < ISP_HA_SCSI_1020A)) &&
4444 			    (sdp->isp_devparam[tgt].sync_period <=
4445 			    (ISP_20M_SYNCPARMS & 0xff))) {
4446 				sdp->isp_devparam[tgt].sync_offset =
4447 				    ISP_10M_SYNCPARMS >> 8;
4448 				sdp->isp_devparam[tgt].sync_period =
4449 				    ISP_10M_SYNCPARMS & 0xff;
4450 			}
4451 		}
4452 		isp_prt(isp, ISP_LOGDEBUG1,
4453 		    "Initial bus %d tgt %d flags %x offset %x period %x",
4454 		    channel, tgt, sdp->isp_devparam[tgt].dev_flags,
4455 		    sdp->isp_devparam[tgt].sync_offset,
4456 		    sdp->isp_devparam[tgt].sync_period);
4457 	}
4458 
4459 	/*
4460 	 * Establish default some more default parameters.
4461 	 */
4462 	sdp->isp_cmd_dma_burst_enable = 1;
4463 	sdp->isp_data_dma_burst_enabl = 1;
4464 	sdp->isp_fifo_threshold = 0;
4465 	sdp->isp_initiator_id = DEFAULT_IID(isp);
4466 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
4467 		sdp->isp_async_data_setup = 9;
4468 	} else {
4469 		sdp->isp_async_data_setup = 6;
4470 	}
4471 	sdp->isp_selection_timeout = 250;
4472 	sdp->isp_max_queue_depth = MAXISPREQUEST(isp);
4473 	sdp->isp_tag_aging = 8;
4474 	sdp->isp_bus_reset_delay = 3;
4475 	sdp->isp_retry_count = 2;
4476 	sdp->isp_retry_delay = 2;
4477 
4478 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
4479 		sdp->isp_devparam[tgt].exc_throttle = ISP_EXEC_THROTTLE;
4480 		sdp->isp_devparam[tgt].dev_enable = 1;
4481 	}
4482 }
4483 
4484 /*
4485  * Re-initialize the ISP and complete all orphaned commands
4486  * with a 'botched' notice. The reset/init routines should
4487  * not disturb an already active list of commands.
4488  *
4489  * Locks held prior to coming here.
4490  */
4491 
4492 void
4493 isp_reinit(isp)
4494 	struct ispsoftc *isp;
4495 {
4496 	XS_T *xs;
4497 	u_int32_t handle;
4498 
4499 	isp_reset(isp);
4500 	if (isp->isp_state == ISP_RESETSTATE) {
4501 		isp_init(isp);
4502 		if (isp->isp_state == ISP_INITSTATE) {
4503 			isp->isp_state = ISP_RUNSTATE;
4504 		}
4505 	}
4506 	if (isp->isp_state != ISP_RUNSTATE) {
4507 		isp_prt(isp, ISP_LOGERR, "isp_reinit cannot restart ISP");
4508 	}
4509 	isp->isp_nactive = 0;
4510 
4511 	for (handle = 1; (int) handle <= isp->isp_maxcmds; handle++) {
4512 		xs = isp_find_xs(isp, handle);
4513 		if (xs == NULL) {
4514 			continue;
4515 		}
4516 		isp_destroy_handle(isp, handle);
4517 		if (XS_XFRLEN(xs)) {
4518 			ISP_DMAFREE(isp, xs, handle);
4519 			XS_RESID(xs) = XS_XFRLEN(xs);
4520 		} else {
4521 			XS_RESID(xs) = 0;
4522 		}
4523 		XS_SETERR(xs, HBA_BUSRESET);
4524 		isp_done(xs);
4525 	}
4526 }
4527 
4528 /*
4529  * NVRAM Routines
4530  */
4531 static int
4532 isp_read_nvram(isp)
4533 	struct ispsoftc *isp;
4534 {
4535 	int i, amt;
4536 	u_int8_t csum, minversion;
4537 	union {
4538 		u_int8_t _x[ISP2100_NVRAM_SIZE];
4539 		u_int16_t _s[ISP2100_NVRAM_SIZE>>1];
4540 	} _n;
4541 #define	nvram_data	_n._x
4542 #define	nvram_words	_n._s
4543 
4544 	if (IS_FC(isp)) {
4545 		amt = ISP2100_NVRAM_SIZE;
4546 		minversion = 1;
4547 	} else if (IS_ULTRA2(isp)) {
4548 		amt = ISP1080_NVRAM_SIZE;
4549 		minversion = 0;
4550 	} else {
4551 		amt = ISP_NVRAM_SIZE;
4552 		minversion = 2;
4553 	}
4554 
4555 	/*
4556 	 * Just read the first two words first to see if we have a valid
4557 	 * NVRAM to continue reading the rest with.
4558 	 */
4559 	for (i = 0; i < 2; i++) {
4560 		isp_rdnvram_word(isp, i, &nvram_words[i]);
4561 	}
4562 	if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
4563 	    nvram_data[2] != 'P') {
4564 		if (isp->isp_bustype != ISP_BT_SBUS) {
4565 			isp_prt(isp, ISP_LOGWARN, "invalid NVRAM header");
4566 			isp_prt(isp, ISP_LOGDEBUG0, "%x %x %x",
4567 			    nvram_data[0], nvram_data[1], nvram_data[2]);
4568 		}
4569 		return (-1);
4570 	}
4571 	for (i = 2; i < amt>>1; i++) {
4572 		isp_rdnvram_word(isp, i, &nvram_words[i]);
4573 	}
4574 	for (csum = 0, i = 0; i < amt; i++) {
4575 		csum += nvram_data[i];
4576 	}
4577 	if (csum != 0) {
4578 		isp_prt(isp, ISP_LOGWARN, "invalid NVRAM checksum");
4579 		return (-1);
4580 	}
4581 	if (ISP_NVRAM_VERSION(nvram_data) < minversion) {
4582 		isp_prt(isp, ISP_LOGWARN, "version %d NVRAM not understood",
4583 		    ISP_NVRAM_VERSION(nvram_data));
4584 		return (-1);
4585 	}
4586 
4587 	if (IS_ULTRA3(isp)) {
4588 		isp_parse_nvram_12160(isp, 0, nvram_data);
4589 		isp_parse_nvram_12160(isp, 1, nvram_data);
4590 	} else if (IS_1080(isp)) {
4591 		isp_parse_nvram_1080(isp, 0, nvram_data);
4592 	} else if (IS_1280(isp) || IS_1240(isp)) {
4593 		isp_parse_nvram_1080(isp, 0, nvram_data);
4594 		isp_parse_nvram_1080(isp, 1, nvram_data);
4595 	} else if (IS_SCSI(isp)) {
4596 		isp_parse_nvram_1020(isp, nvram_data);
4597 	} else {
4598 		isp_parse_nvram_2100(isp, nvram_data);
4599 	}
4600 	return (0);
4601 #undef	nvram_data
4602 #undef	nvram_words
4603 }
4604 
4605 static void
4606 isp_rdnvram_word(isp, wo, rp)
4607 	struct ispsoftc *isp;
4608 	int wo;
4609 	u_int16_t *rp;
4610 {
4611 	int i, cbits;
4612 	u_int16_t bit, rqst;
4613 
4614 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
4615 	USEC_DELAY(2);
4616 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
4617 	USEC_DELAY(2);
4618 
4619 	if (IS_FC(isp)) {
4620 		wo &= ((ISP2100_NVRAM_SIZE >> 1) - 1);
4621 		rqst = (ISP_NVRAM_READ << 8) | wo;
4622 		cbits = 10;
4623 	} else if (IS_ULTRA2(isp)) {
4624 		wo &= ((ISP1080_NVRAM_SIZE >> 1) - 1);
4625 		rqst = (ISP_NVRAM_READ << 8) | wo;
4626 		cbits = 10;
4627 	} else {
4628 		wo &= ((ISP_NVRAM_SIZE >> 1) - 1);
4629 		rqst = (ISP_NVRAM_READ << 6) | wo;
4630 		cbits = 8;
4631 	}
4632 
4633 	/*
4634 	 * Clock the word select request out...
4635 	 */
4636 	for (i = cbits; i >= 0; i--) {
4637 		if ((rqst >> i) & 1) {
4638 			bit = BIU_NVRAM_SELECT | BIU_NVRAM_DATAOUT;
4639 		} else {
4640 			bit = BIU_NVRAM_SELECT;
4641 		}
4642 		ISP_WRITE(isp, BIU_NVRAM, bit);
4643 		USEC_DELAY(2);
4644 		ISP_WRITE(isp, BIU_NVRAM, bit | BIU_NVRAM_CLOCK);
4645 		USEC_DELAY(2);
4646 		ISP_WRITE(isp, BIU_NVRAM, bit);
4647 		USEC_DELAY(2);
4648 	}
4649 	/*
4650 	 * Now read the result back in (bits come back in MSB format).
4651 	 */
4652 	*rp = 0;
4653 	for (i = 0; i < 16; i++) {
4654 		u_int16_t rv;
4655 		*rp <<= 1;
4656 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
4657 		USEC_DELAY(2);
4658 		rv = ISP_READ(isp, BIU_NVRAM);
4659 		if (rv & BIU_NVRAM_DATAIN) {
4660 			*rp |= 1;
4661 		}
4662 		USEC_DELAY(2);
4663 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
4664 		USEC_DELAY(2);
4665 	}
4666 	ISP_WRITE(isp, BIU_NVRAM, 0);
4667 	USEC_DELAY(2);
4668 	ISP_SWIZZLE_NVRAM_WORD(isp, rp);
4669 }
4670 
4671 static void
4672 isp_parse_nvram_1020(isp, nvram_data)
4673 	struct ispsoftc *isp;
4674 	u_int8_t *nvram_data;
4675 {
4676 	int i;
4677 	sdparam *sdp = (sdparam *) isp->isp_param;
4678 
4679 	sdp->isp_fifo_threshold =
4680 		ISP_NVRAM_FIFO_THRESHOLD(nvram_data) |
4681 		(ISP_NVRAM_FIFO_THRESHOLD_128(nvram_data) << 2);
4682 
4683 	sdp->isp_initiator_id =
4684 		ISP_NVRAM_INITIATOR_ID(nvram_data);
4685 
4686 	sdp->isp_bus_reset_delay =
4687 		ISP_NVRAM_BUS_RESET_DELAY(nvram_data);
4688 
4689 	sdp->isp_retry_count =
4690 		ISP_NVRAM_BUS_RETRY_COUNT(nvram_data);
4691 
4692 	sdp->isp_retry_delay =
4693 		ISP_NVRAM_BUS_RETRY_DELAY(nvram_data);
4694 
4695 	sdp->isp_async_data_setup =
4696 		ISP_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data);
4697 
4698 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
4699 		if (sdp->isp_async_data_setup < 9) {
4700 			sdp->isp_async_data_setup = 9;
4701 		}
4702 	} else {
4703 		if (sdp->isp_async_data_setup != 6) {
4704 			sdp->isp_async_data_setup = 6;
4705 		}
4706 	}
4707 
4708 	sdp->isp_req_ack_active_neg =
4709 		ISP_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data);
4710 
4711 	sdp->isp_data_line_active_neg =
4712 		ISP_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data);
4713 
4714 	sdp->isp_data_dma_burst_enabl =
4715 		ISP_NVRAM_DATA_DMA_BURST_ENABLE(nvram_data);
4716 
4717 	sdp->isp_cmd_dma_burst_enable =
4718 		ISP_NVRAM_CMD_DMA_BURST_ENABLE(nvram_data);
4719 
4720 	sdp->isp_tag_aging =
4721 		ISP_NVRAM_TAG_AGE_LIMIT(nvram_data);
4722 
4723 	sdp->isp_selection_timeout =
4724 		ISP_NVRAM_SELECTION_TIMEOUT(nvram_data);
4725 
4726 	sdp->isp_max_queue_depth =
4727 		ISP_NVRAM_MAX_QUEUE_DEPTH(nvram_data);
4728 
4729 	sdp->isp_fast_mttr = ISP_NVRAM_FAST_MTTR_ENABLE(nvram_data);
4730 	for (i = 0; i < MAX_TARGETS; i++) {
4731 		sdp->isp_devparam[i].dev_enable =
4732 			ISP_NVRAM_TGT_DEVICE_ENABLE(nvram_data, i);
4733 		sdp->isp_devparam[i].exc_throttle =
4734 			ISP_NVRAM_TGT_EXEC_THROTTLE(nvram_data, i);
4735 		sdp->isp_devparam[i].sync_offset =
4736 			ISP_NVRAM_TGT_SYNC_OFFSET(nvram_data, i);
4737 		sdp->isp_devparam[i].sync_period =
4738 			ISP_NVRAM_TGT_SYNC_PERIOD(nvram_data, i);
4739 
4740 		if (isp->isp_type < ISP_HA_SCSI_1040) {
4741 			/*
4742 			 * If we're not ultra, we can't possibly
4743 			 * be a shorter period than this.
4744 			 */
4745 			if (sdp->isp_devparam[i].sync_period < 0x19) {
4746 				sdp->isp_devparam[i].sync_period = 0x19;
4747 			}
4748 			if (sdp->isp_devparam[i].sync_offset > 0xc) {
4749 				sdp->isp_devparam[i].sync_offset = 0x0c;
4750 			}
4751 		} else {
4752 			if (sdp->isp_devparam[i].sync_offset > 0x8) {
4753 				sdp->isp_devparam[i].sync_offset = 0x8;
4754 			}
4755 		}
4756 		sdp->isp_devparam[i].dev_flags = 0;
4757 		if (ISP_NVRAM_TGT_RENEG(nvram_data, i))
4758 			sdp->isp_devparam[i].dev_flags |= DPARM_RENEG;
4759 		sdp->isp_devparam[i].dev_flags |= DPARM_ARQ;
4760 		if (ISP_NVRAM_TGT_TQING(nvram_data, i))
4761 			sdp->isp_devparam[i].dev_flags |= DPARM_TQING;
4762 		if (ISP_NVRAM_TGT_SYNC(nvram_data, i))
4763 			sdp->isp_devparam[i].dev_flags |= DPARM_SYNC;
4764 		if (ISP_NVRAM_TGT_WIDE(nvram_data, i))
4765 			sdp->isp_devparam[i].dev_flags |= DPARM_WIDE;
4766 		if (ISP_NVRAM_TGT_PARITY(nvram_data, i))
4767 			sdp->isp_devparam[i].dev_flags |= DPARM_PARITY;
4768 		if (ISP_NVRAM_TGT_DISC(nvram_data, i))
4769 			sdp->isp_devparam[i].dev_flags |= DPARM_DISC;
4770 		sdp->isp_devparam[i].cur_dflags = 0; /* we don't know */
4771 	}
4772 }
4773 
4774 static void
4775 isp_parse_nvram_1080(isp, bus, nvram_data)
4776 	struct ispsoftc *isp;
4777 	int bus;
4778 	u_int8_t *nvram_data;
4779 {
4780 	int i;
4781 	sdparam *sdp = (sdparam *) isp->isp_param;
4782 	sdp += bus;
4783 
4784 	sdp->isp_fifo_threshold =
4785 	    ISP1080_NVRAM_FIFO_THRESHOLD(nvram_data);
4786 
4787 	sdp->isp_initiator_id =
4788 	    ISP1080_NVRAM_INITIATOR_ID(nvram_data, bus);
4789 
4790 	sdp->isp_bus_reset_delay =
4791 	    ISP1080_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
4792 
4793 	sdp->isp_retry_count =
4794 	    ISP1080_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
4795 
4796 	sdp->isp_retry_delay =
4797 	    ISP1080_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
4798 
4799 	sdp->isp_async_data_setup =
4800 	    ISP1080_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data,
4801 	    bus);
4802 
4803 	sdp->isp_req_ack_active_neg =
4804 	    ISP1080_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data,
4805 	    bus);
4806 
4807 	sdp->isp_data_line_active_neg =
4808 	    ISP1080_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data,
4809 	    bus);
4810 
4811 	sdp->isp_data_dma_burst_enabl =
4812 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
4813 
4814 	sdp->isp_cmd_dma_burst_enable =
4815 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
4816 
4817 	sdp->isp_selection_timeout =
4818 	    ISP1080_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
4819 
4820 	sdp->isp_max_queue_depth =
4821 	     ISP1080_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
4822 
4823 	for (i = 0; i < MAX_TARGETS; i++) {
4824 		sdp->isp_devparam[i].dev_enable =
4825 		    ISP1080_NVRAM_TGT_DEVICE_ENABLE(nvram_data, i, bus);
4826 		sdp->isp_devparam[i].exc_throttle =
4827 			ISP1080_NVRAM_TGT_EXEC_THROTTLE(nvram_data, i, bus);
4828 		sdp->isp_devparam[i].sync_offset =
4829 			ISP1080_NVRAM_TGT_SYNC_OFFSET(nvram_data, i, bus);
4830 		sdp->isp_devparam[i].sync_period =
4831 			ISP1080_NVRAM_TGT_SYNC_PERIOD(nvram_data, i, bus);
4832 		sdp->isp_devparam[i].dev_flags = 0;
4833 		if (ISP1080_NVRAM_TGT_RENEG(nvram_data, i, bus))
4834 			sdp->isp_devparam[i].dev_flags |= DPARM_RENEG;
4835 		sdp->isp_devparam[i].dev_flags |= DPARM_ARQ;
4836 		if (ISP1080_NVRAM_TGT_TQING(nvram_data, i, bus))
4837 			sdp->isp_devparam[i].dev_flags |= DPARM_TQING;
4838 		if (ISP1080_NVRAM_TGT_SYNC(nvram_data, i, bus))
4839 			sdp->isp_devparam[i].dev_flags |= DPARM_SYNC;
4840 		if (ISP1080_NVRAM_TGT_WIDE(nvram_data, i, bus))
4841 			sdp->isp_devparam[i].dev_flags |= DPARM_WIDE;
4842 		if (ISP1080_NVRAM_TGT_PARITY(nvram_data, i, bus))
4843 			sdp->isp_devparam[i].dev_flags |= DPARM_PARITY;
4844 		if (ISP1080_NVRAM_TGT_DISC(nvram_data, i, bus))
4845 			sdp->isp_devparam[i].dev_flags |= DPARM_DISC;
4846 		sdp->isp_devparam[i].cur_dflags = 0;
4847 	}
4848 }
4849 
4850 static void
4851 isp_parse_nvram_12160(isp, bus, nvram_data)
4852 	struct ispsoftc *isp;
4853 	int bus;
4854 	u_int8_t *nvram_data;
4855 {
4856 	sdparam *sdp = (sdparam *) isp->isp_param;
4857 	int i;
4858 
4859 	sdp += bus;
4860 
4861 	sdp->isp_fifo_threshold =
4862 	    ISP12160_NVRAM_FIFO_THRESHOLD(nvram_data);
4863 
4864 	sdp->isp_initiator_id =
4865 	    ISP12160_NVRAM_INITIATOR_ID(nvram_data, bus);
4866 
4867 	sdp->isp_bus_reset_delay =
4868 	    ISP12160_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
4869 
4870 	sdp->isp_retry_count =
4871 	    ISP12160_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
4872 
4873 	sdp->isp_retry_delay =
4874 	    ISP12160_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
4875 
4876 	sdp->isp_async_data_setup =
4877 	    ISP12160_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data,
4878 	    bus);
4879 
4880 	sdp->isp_req_ack_active_neg =
4881 	    ISP12160_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data,
4882 	    bus);
4883 
4884 	sdp->isp_data_line_active_neg =
4885 	    ISP12160_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data,
4886 	    bus);
4887 
4888 	sdp->isp_data_dma_burst_enabl =
4889 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
4890 
4891 	sdp->isp_cmd_dma_burst_enable =
4892 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
4893 
4894 	sdp->isp_selection_timeout =
4895 	    ISP12160_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
4896 
4897 	sdp->isp_max_queue_depth =
4898 	     ISP12160_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
4899 
4900 	for (i = 0; i < MAX_TARGETS; i++) {
4901 		sdp->isp_devparam[i].dev_enable =
4902 		    ISP12160_NVRAM_TGT_DEVICE_ENABLE(nvram_data, i, bus);
4903 		sdp->isp_devparam[i].exc_throttle =
4904 			ISP12160_NVRAM_TGT_EXEC_THROTTLE(nvram_data, i, bus);
4905 		sdp->isp_devparam[i].sync_offset =
4906 			ISP12160_NVRAM_TGT_SYNC_OFFSET(nvram_data, i, bus);
4907 		sdp->isp_devparam[i].sync_period =
4908 			ISP12160_NVRAM_TGT_SYNC_PERIOD(nvram_data, i, bus);
4909 		sdp->isp_devparam[i].dev_flags = 0;
4910 		if (ISP12160_NVRAM_TGT_RENEG(nvram_data, i, bus))
4911 			sdp->isp_devparam[i].dev_flags |= DPARM_RENEG;
4912 		sdp->isp_devparam[i].dev_flags |= DPARM_ARQ;
4913 		if (ISP12160_NVRAM_TGT_TQING(nvram_data, i, bus))
4914 			sdp->isp_devparam[i].dev_flags |= DPARM_TQING;
4915 		if (ISP12160_NVRAM_TGT_SYNC(nvram_data, i, bus))
4916 			sdp->isp_devparam[i].dev_flags |= DPARM_SYNC;
4917 		if (ISP12160_NVRAM_TGT_WIDE(nvram_data, i, bus))
4918 			sdp->isp_devparam[i].dev_flags |= DPARM_WIDE;
4919 		if (ISP12160_NVRAM_TGT_PARITY(nvram_data, i, bus))
4920 			sdp->isp_devparam[i].dev_flags |= DPARM_PARITY;
4921 		if (ISP12160_NVRAM_TGT_DISC(nvram_data, i, bus))
4922 			sdp->isp_devparam[i].dev_flags |= DPARM_DISC;
4923 		sdp->isp_devparam[i].cur_dflags = 0;
4924 	}
4925 }
4926 
4927 static void
4928 isp_parse_nvram_2100(isp, nvram_data)
4929 	struct ispsoftc *isp;
4930 	u_int8_t *nvram_data;
4931 {
4932 	fcparam *fcp = (fcparam *) isp->isp_param;
4933 	u_int64_t wwn;
4934 
4935 	/*
4936 	 * There is NVRAM storage for both Port and Node entities-
4937 	 * but the Node entity appears to be unused on all the cards
4938 	 * I can find. However, we should account for this being set
4939 	 * at some point in the future.
4940 	 *
4941 	 * Qlogic WWNs have an NAA of 2, but usually nothing shows up in
4942 	 * bits 48..60. In the case of the 2202, it appears that they do
4943 	 * use bit 48 to distinguish between the two instances on the card.
4944 	 * The 2204, which I've never seen, *probably* extends this method.
4945 	 */
4946 	wwn = ISP2100_NVRAM_PORT_NAME(nvram_data);
4947 	if (wwn) {
4948 		isp_prt(isp, ISP_LOGCONFIG, "NVRAM Port WWN 0x%08x%08x",
4949 		    (u_int32_t) (wwn >> 32), (u_int32_t) (wwn & 0xffffffff));
4950 		if ((wwn >> 60) == 0) {
4951 			wwn |= (((u_int64_t) 2)<< 60);
4952 		}
4953 	}
4954 	fcp->isp_portwwn = wwn;
4955 	wwn = ISP2100_NVRAM_NODE_NAME(nvram_data);
4956 	if (wwn) {
4957 		isp_prt(isp, ISP_LOGCONFIG, "NVRAM Node WWN 0x%08x%08x",
4958 		    (u_int32_t) (wwn >> 32), (u_int32_t) (wwn & 0xffffffff));
4959 		if ((wwn >> 60) == 0) {
4960 			wwn |= (((u_int64_t) 2)<< 60);
4961 		}
4962 	}
4963 	fcp->isp_nodewwn = wwn;
4964 
4965 	/*
4966 	 * Make sure we have both Node and Port as non-zero values.
4967 	 */
4968 	if (fcp->isp_nodewwn != 0 && fcp->isp_portwwn == 0) {
4969 		fcp->isp_portwwn = fcp->isp_nodewwn;
4970 	} else if (fcp->isp_nodewwn == 0 && fcp->isp_portwwn != 0) {
4971 		fcp->isp_nodewwn = fcp->isp_portwwn;
4972 	}
4973 
4974 	/*
4975 	 * Make the Node and Port values sane if they're NAA == 2.
4976 	 * This means to clear bits 48..56 for the Node WWN and
4977 	 * make sure that there's some non-zero value in 48..56
4978 	 * for the Port WWN.
4979 	 */
4980 	if (fcp->isp_nodewwn && fcp->isp_portwwn) {
4981 		if ((fcp->isp_nodewwn & (((u_int64_t) 0xfff) << 48)) != 0 &&
4982 		    (fcp->isp_nodewwn >> 60) == 2) {
4983 			fcp->isp_nodewwn &= ~((u_int64_t) 0xfff << 48);
4984 		}
4985 		if ((fcp->isp_portwwn & (((u_int64_t) 0xfff) << 48)) == 0 &&
4986 		    (fcp->isp_portwwn >> 60) == 2) {
4987 			fcp->isp_portwwn |= ((u_int64_t) 1 << 56);
4988 		}
4989 	}
4990 
4991 	fcp->isp_maxalloc =
4992 		ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data);
4993 	fcp->isp_maxfrmlen =
4994 		ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data);
4995 	fcp->isp_retry_delay =
4996 		ISP2100_NVRAM_RETRY_DELAY(nvram_data);
4997 	fcp->isp_retry_count =
4998 		ISP2100_NVRAM_RETRY_COUNT(nvram_data);
4999 	fcp->isp_loopid =
5000 		ISP2100_NVRAM_HARDLOOPID(nvram_data);
5001 	fcp->isp_execthrottle =
5002 		ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data);
5003 	fcp->isp_fwoptions = ISP2100_NVRAM_OPTIONS(nvram_data);
5004 	isp_prt(isp, ISP_LOGDEBUG0,
5005 	    "fwoptions from nvram are 0x%x", fcp->isp_fwoptions);
5006 }
5007