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