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