xref: /freebsd/sys/dev/isp/isp.c (revision cb166ce422ac2bc81f42c2a2e2cd68625c11478d)
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 (lim = FC_SNS_ID+1, lp = &fcp->portdb[FC_SNS_ID+1];
1654 	     lp < &fcp->portdb[MAX_FC_TARG]; lp++) {
1655 		u_int32_t portid;
1656 		mbreg_t mbs;
1657 
1658 		/*
1659 		 * Nothing here?
1660 		 */
1661 		if (lp->port_wwn == 0)
1662 			continue;
1663 
1664 		/*
1665 		 * Don't try to log into yourself.
1666 		 */
1667 		if ((portid = lp->portid) == fcp->isp_portid)
1668 			continue;
1669 
1670 		/*
1671 		 * Force a logout if we were logged in.
1672 		 */
1673 		if (lp->valid) {
1674 			mbs.param[0] = MBOX_FABRIC_LOGOUT;
1675 			mbs.param[1] = lp->loopid << 8;
1676 			mbs.param[2] = 0;
1677 			mbs.param[3] = 0;
1678 			isp_mboxcmd(isp, &mbs);
1679 			lp->valid = 0;
1680 		}
1681 
1682 		/*
1683 		 * And log in....
1684 		 */
1685 		loopid = lp - fcp->portdb;
1686 		lp->loopid = 0;
1687 		lim = 0;
1688 		do {
1689 			mbs.param[0] = MBOX_FABRIC_LOGIN;
1690 			mbs.param[1] = loopid << 8;
1691 			if (IS_2200(isp)) {
1692 				/* only issue a PLOGI if not logged in */
1693 				mbs.param[1] |= 0x1;
1694 			}
1695 			mbs.param[2] = portid >> 16;
1696 			mbs.param[3] = portid & 0xffff;
1697 			isp_mboxcmd(isp, &mbs);
1698 			switch (mbs.param[0]) {
1699 			case MBOX_LOOP_ID_USED:
1700 				/*
1701 				 * Try the next available loop id.
1702 				 */
1703 				loopid++;
1704 				break;
1705 			case MBOX_PORT_ID_USED:
1706 				/*
1707 				 * This port is already logged in.
1708 				 * Snaffle the loop id it's using.
1709 				 */
1710 				if ((loopid = mbs.param[1]) == 0) {
1711 					lim = -1;
1712 				}
1713 				/* FALLTHROUGH */
1714 			case MBOX_COMMAND_COMPLETE:
1715 				lp->loopid = loopid;
1716 				lim = 1;
1717 				break;
1718 			case MBOX_COMMAND_ERROR:
1719 				PRINTF("%s: command error in PLOGI (0x%x)\n",
1720 				    isp->isp_name, mbs.param[1]);
1721 				/* FALLTHROUGH */
1722 			case MBOX_ALL_IDS_USED: /* We're outta IDs */
1723 			default:
1724 				lim = -1;
1725 				break;
1726 			}
1727 		} while (lim == 0 && loopid < MAX_FC_TARG);
1728 		if (lim < 0)
1729 			continue;
1730 
1731 		lp->valid = 1;
1732 		lp->fabdev = 1;
1733 		if (isp_getpdb(isp, lp->loopid, &pdb) != 0) {
1734 			/*
1735 			 * Be kind...
1736 			 */
1737 			lp->roles = (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT);
1738 			PRINTF("%s: Faked PortID 0x%x into LoopID %d\n",
1739 			    isp->isp_name, lp->portid, lp->loopid);
1740 			continue;
1741 		}
1742 		if (pdb.pdb_loopid != lp->loopid) {
1743 			lp->roles = (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT);
1744 			PRINTF("%s: Wanked PortID 0x%x to LoopID %d\n",
1745 			    isp->isp_name, lp->portid, lp->loopid);
1746 			continue;
1747 		}
1748 		lp->roles =
1749 		    (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
1750 		lp->portid = BITS2WORD(pdb.pdb_portid_bits);
1751 		lp->node_wwn =
1752 		    (((u_int64_t)pdb.pdb_nodename[0]) << 56) |
1753 		    (((u_int64_t)pdb.pdb_nodename[1]) << 48) |
1754 		    (((u_int64_t)pdb.pdb_nodename[2]) << 40) |
1755 		    (((u_int64_t)pdb.pdb_nodename[3]) << 32) |
1756 		    (((u_int64_t)pdb.pdb_nodename[4]) << 24) |
1757 		    (((u_int64_t)pdb.pdb_nodename[5]) << 16) |
1758 		    (((u_int64_t)pdb.pdb_nodename[6]) <<  8) |
1759 		    (((u_int64_t)pdb.pdb_nodename[7]));
1760 		lp->port_wwn =
1761 		    (((u_int64_t)pdb.pdb_portname[0]) << 56) |
1762 		    (((u_int64_t)pdb.pdb_portname[1]) << 48) |
1763 		    (((u_int64_t)pdb.pdb_portname[2]) << 40) |
1764 		    (((u_int64_t)pdb.pdb_portname[3]) << 32) |
1765 		    (((u_int64_t)pdb.pdb_portname[4]) << 24) |
1766 		    (((u_int64_t)pdb.pdb_portname[5]) << 16) |
1767 		    (((u_int64_t)pdb.pdb_portname[6]) <<  8) |
1768 		    (((u_int64_t)pdb.pdb_portname[7]));
1769 		/*
1770 		 * Check to make sure this all makes sense.
1771 		 */
1772 		if (lp->node_wwn && lp->port_wwn) {
1773 			loopid = lp - fcp->portdb;
1774 			(void) isp_async(isp, ISPASYNC_PDB_CHANGED, &loopid);
1775 			continue;
1776 		}
1777 		lp->fabdev = lp->valid = 0;
1778 		PRINTF("%s: Target %d (Loop 0x%x) Port ID 0x%x lost its WWN\n",
1779 		    isp->isp_name, loopid, lp->loopid, lp->portid);
1780 		mbs.param[0] = MBOX_FABRIC_LOGOUT;
1781 		mbs.param[1] = lp->loopid << 8;
1782 		mbs.param[2] = 0;
1783 		mbs.param[3] = 0;
1784 		isp_mboxcmd(isp, &mbs);
1785 	}
1786 #endif
1787 	/*
1788 	 * If we get here, we've for sure seen not only a valid loop
1789 	 * but know what is or isn't on it, so mark this for usage
1790 	 * in ispscsicmd.
1791 	 */
1792 	fcp->loop_seen_once = 1;
1793 	return (0);
1794 }
1795 
1796 #ifdef	ISP2100_FABRIC
1797 static int
1798 isp_scan_fabric(isp)
1799 	struct ispsoftc *isp;
1800 {
1801 	fcparam *fcp = isp->isp_param;
1802 	u_int32_t portid, first_nz_portid;
1803 	sns_screq_t *reqp;
1804 	sns_scrsp_t *resp;
1805 	mbreg_t mbs;
1806 	int hicap;
1807 
1808 	reqp = (sns_screq_t *) fcp->isp_scratch;
1809 	resp = (sns_scrsp_t *) (&((char *)fcp->isp_scratch)[0x100]);
1810 	first_nz_portid = portid = fcp->isp_portid;
1811 
1812 	for (hicap = 0; hicap < 1024; hicap++) {
1813 		MEMZERO((void *) reqp, SNS_GAN_REQ_SIZE);
1814 		reqp->snscb_rblen = SNS_GAN_RESP_SIZE >> 1;
1815 		reqp->snscb_addr[RQRSP_ADDR0015] =
1816 			DMA_LSW(fcp->isp_scdma + 0x100);
1817 		reqp->snscb_addr[RQRSP_ADDR1631] =
1818 			DMA_MSW(fcp->isp_scdma + 0x100);
1819 		reqp->snscb_sblen = 6;
1820 		reqp->snscb_data[0] = SNS_GAN;
1821 		reqp->snscb_data[4] = portid & 0xffff;
1822 		reqp->snscb_data[5] = (portid >> 16) & 0xff;
1823 		ISP_SWIZZLE_SNS_REQ(isp, reqp);
1824 		mbs.param[0] = MBOX_SEND_SNS;
1825 		mbs.param[1] = SNS_GAN_REQ_SIZE >> 1;
1826 		mbs.param[2] = DMA_MSW(fcp->isp_scdma);
1827 		mbs.param[3] = DMA_LSW(fcp->isp_scdma);
1828 		mbs.param[6] = 0;
1829 		mbs.param[7] = 0;
1830 		MemoryBarrier();
1831 		isp_mboxcmd(isp, &mbs);
1832 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1833 			IDPRINTF(1, ("%s: SNS failed (0x%x)\n", isp->isp_name,
1834 			    mbs.param[0]));
1835 			return (-1);
1836 		}
1837 		ISP_UNSWIZZLE_SNS_RSP(isp, resp, SNS_GAN_RESP_SIZE >> 1);
1838 		portid = (((u_int32_t) resp->snscb_port_id[0]) << 16) |
1839 		    (((u_int32_t) resp->snscb_port_id[1]) << 8) |
1840 		    (((u_int32_t) resp->snscb_port_id[2]));
1841 		if (isp_async(isp, ISPASYNC_FABRIC_DEV, resp)) {
1842 			return (-1);
1843 		}
1844 		if (first_nz_portid == 0 && portid) {
1845 			first_nz_portid = portid;
1846 		}
1847 		if (first_nz_portid == portid) {
1848 			return (0);
1849 		}
1850 	}
1851 	/*
1852 	 * We either have a broken name server or a huge fabric if we get here.
1853 	 */
1854 	return (0);
1855 }
1856 #endif
1857 /*
1858  * Start a command. Locking is assumed done in the caller.
1859  */
1860 
1861 int32_t
1862 ispscsicmd(xs)
1863 	ISP_SCSI_XFER_T *xs;
1864 {
1865 	struct ispsoftc *isp;
1866 	u_int16_t iptr, optr;
1867 	union {
1868 		ispreq_t *_reqp;
1869 		ispreqt2_t *_t2reqp;
1870 	} _u;
1871 #define	reqp	_u._reqp
1872 #define	t2reqp	_u._t2reqp
1873 #define	UZSIZE	max(sizeof (ispreq_t), sizeof (ispreqt2_t))
1874 	int target, i;
1875 
1876 	XS_INITERR(xs);
1877 	isp = XS_ISP(xs);
1878 
1879 	if (isp->isp_state != ISP_RUNSTATE) {
1880 		PRINTF("%s: adapter not ready\n", isp->isp_name);
1881 		XS_SETERR(xs, HBA_BOTCH);
1882 		return (CMD_COMPLETE);
1883 	}
1884 
1885 	/*
1886 	 * Check command CDB length, etc.. We really are limited to 16 bytes
1887 	 * for Fibre Channel, but can do up to 44 bytes in parallel SCSI,
1888 	 * but probably only if we're running fairly new firmware (we'll
1889 	 * let the old f/w choke on an extended command queue entry).
1890 	 */
1891 
1892 	if (XS_CDBLEN(xs) > (IS_FC(isp)? 16 : 44) || XS_CDBLEN(xs) == 0) {
1893 		PRINTF("%s: unsupported cdb length (%d, CDB[0]=0x%x)\n",
1894 		    isp->isp_name, XS_CDBLEN(xs), XS_CDBP(xs)[0] & 0xff);
1895 		XS_SETERR(xs, HBA_BOTCH);
1896 		return (CMD_COMPLETE);
1897 	}
1898 
1899 	/*
1900 	 * Check to see whether we have good firmware state still or
1901 	 * need to refresh our port database for this target.
1902 	 */
1903 	target = XS_TGT(xs);
1904 	if (IS_FC(isp)) {
1905 		fcparam *fcp = isp->isp_param;
1906 		struct lportdb *lp;
1907 #if	defined(ISP2100_FABRIC)
1908 		if (target >= FL_PORT_ID) {
1909 			/*
1910 			 * If we're not on a Fabric, we can't have a target
1911 			 * above FL_PORT_ID-1. If we're on a fabric, we
1912 			 * can't have a target less than FC_SNS_ID+1.
1913 			 */
1914 			if (fcp->isp_onfabric == 0 || target <= FC_SNS_ID) {
1915 				XS_SETERR(xs, HBA_SELTIMEOUT);
1916 				return (CMD_COMPLETE);
1917 			}
1918 		}
1919 #endif
1920 		/*
1921 		 * Check for f/w being in ready state. If the f/w
1922 		 * isn't in ready state, then we don't know our
1923 		 * loop ID and the f/w hasn't completed logging
1924 		 * into all targets on the loop. If this is the
1925 		 * case, then bounce the command. We pretend this is
1926 		 * a SELECTION TIMEOUT error if we've never gone to
1927 		 * FW_READY state at all- in this case we may not
1928 		 * be hooked to a loop at all and we shouldn't hang
1929 		 * the machine for this. Otherwise, defer this command
1930 		 * until later.
1931 		 */
1932 		if (fcp->isp_fwstate != FW_READY) {
1933 			if (isp_fclink_test(isp, FC_FW_READY_DELAY)) {
1934 				XS_SETERR(xs, HBA_SELTIMEOUT);
1935 				if (fcp->loop_seen_once) {
1936 					return (CMD_RQLATER);
1937 				} else {
1938 					return (CMD_COMPLETE);
1939 				}
1940 			}
1941 		}
1942 
1943 		/*
1944 		 * If our loop state is such that we haven't yet received
1945 		 * a "Port Database Changed" notification (after a LIP or
1946 		 * a Loop Reset or firmware initialization), then defer
1947 		 * sending commands for a little while, but only if we've
1948 		 * seen a valid loop at one point (otherwise we can get
1949 		 * stuck at initialization time).
1950 		 */
1951 		if (fcp->isp_loopstate < LOOP_PDB_RCVD) {
1952 			XS_SETERR(xs, HBA_SELTIMEOUT);
1953 			if (fcp->loop_seen_once) {
1954 				return (CMD_RQLATER);
1955 			} else {
1956 				return (CMD_COMPLETE);
1957 			}
1958 		}
1959 
1960 		/*
1961 		 * If our loop state is now such that we've just now
1962 		 * received a Port Database Change notification, then
1963 		 * we have to go off and (re)synchronize our port
1964 		 * database.
1965 		 */
1966 		if (fcp->isp_loopstate == LOOP_PDB_RCVD) {
1967 			if (isp_pdb_sync(isp, target)) {
1968 				XS_SETERR(xs, HBA_SELTIMEOUT);
1969 				return (CMD_COMPLETE);
1970 			}
1971 		}
1972 
1973 		/*
1974 		 * Now check whether we should even think about pursuing this.
1975 		 */
1976 		lp = &fcp->portdb[target];
1977 		if (lp->valid == 0) {
1978 			XS_SETERR(xs, HBA_SELTIMEOUT);
1979 			return (CMD_COMPLETE);
1980 		}
1981 		if ((lp->roles & (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT)) == 0) {
1982 			IDPRINTF(3, ("%s: target %d is not a target\n",
1983 			    isp->isp_name, target));
1984 			XS_SETERR(xs, HBA_SELTIMEOUT);
1985 			return (CMD_COMPLETE);
1986 		}
1987 		/*
1988 		 * Now turn target into what the actual loop ID is.
1989 		 */
1990 		target = lp->loopid;
1991 	}
1992 
1993 	/*
1994 	 * Next check to see if any HBA or Device
1995 	 * parameters need to be updated.
1996 	 */
1997 	if (isp->isp_update != 0) {
1998 		isp_update(isp);
1999 	}
2000 
2001 	if (isp_getrqentry(isp, &iptr, &optr, (void **) &reqp)) {
2002 		IDPRINTF(1, ("%s: Request Queue Overflow\n", isp->isp_name));
2003 		XS_SETERR(xs, HBA_BOTCH);
2004 		return (CMD_EAGAIN);
2005 	}
2006 
2007 	/*
2008 	 * Now see if we need to synchronize the ISP with respect to anything.
2009 	 * We do dual duty here (cough) for synchronizing for busses other
2010 	 * than which we got here to send a command to.
2011 	 */
2012 	if (isp->isp_sendmarker) {
2013 		u_int8_t n = (IS_DUALBUS(isp)? 2: 1);
2014 		/*
2015 		 * Check ports to send markers for...
2016 		 */
2017 		for (i = 0; i < n; i++) {
2018 			if ((isp->isp_sendmarker & (1 << i)) == 0) {
2019 				continue;
2020 			}
2021 			MEMZERO((void *) reqp, sizeof (*reqp));
2022 			reqp->req_header.rqs_entry_count = 1;
2023 			reqp->req_header.rqs_entry_type = RQSTYPE_MARKER;
2024 			reqp->req_modifier = SYNC_ALL;
2025 			reqp->req_target = i << 7;	/* insert bus number */
2026 			ISP_SWIZZLE_REQUEST(isp, reqp);
2027 			MemoryBarrier();
2028 			ISP_ADD_REQUEST(isp, iptr);
2029 
2030 			if (isp_getrqentry(isp, &iptr, &optr, (void **)&reqp)) {
2031 				IDPRINTF(1, ("%s: Request Queue Overflow+\n",
2032 				    isp->isp_name));
2033 				XS_SETERR(xs, HBA_BOTCH);
2034 				return (CMD_EAGAIN);
2035 			}
2036 		}
2037 	}
2038 
2039 	MEMZERO((void *) reqp, UZSIZE);
2040 	reqp->req_header.rqs_entry_count = 1;
2041 	if (IS_FC(isp)) {
2042 		reqp->req_header.rqs_entry_type = RQSTYPE_T2RQS;
2043 	} else {
2044 		if (XS_CDBLEN(xs) > 12)
2045 			reqp->req_header.rqs_entry_type = RQSTYPE_CMDONLY;
2046 		else
2047 			reqp->req_header.rqs_entry_type = RQSTYPE_REQUEST;
2048 	}
2049 	reqp->req_header.rqs_flags = 0;
2050 	reqp->req_header.rqs_seqno = 0;
2051 	if (IS_FC(isp)) {
2052 		/*
2053 		 * See comment in isp_intr
2054 		 */
2055 		XS_RESID(xs) = 0;
2056 
2057 		/*
2058 		 * Fibre Channel always requires some kind of tag.
2059 		 * The Qlogic drivers seem be happy not to use a tag,
2060 		 * but this breaks for some devices (IBM drives).
2061 		 */
2062 		if (XS_CANTAG(xs)) {
2063 			t2reqp->req_flags = XS_KINDOF_TAG(xs);
2064 		} else {
2065 			if (XS_CDBP(xs)[0] == 0x3)	/* REQUEST SENSE */
2066 				t2reqp->req_flags = REQFLAG_HTAG;
2067 			else
2068 				t2reqp->req_flags = REQFLAG_OTAG;
2069 		}
2070 	} else {
2071 		sdparam *sdp = (sdparam *)isp->isp_param;
2072 		if ((sdp->isp_devparam[target].cur_dflags & DPARM_TQING) &&
2073 		    XS_CANTAG(xs)) {
2074 			reqp->req_flags = XS_KINDOF_TAG(xs);
2075 		}
2076 	}
2077 	reqp->req_target = target | (XS_CHANNEL(xs) << 7);
2078 	if (IS_SCSI(isp)) {
2079 		reqp->req_lun_trn = XS_LUN(xs);
2080 		reqp->req_cdblen = XS_CDBLEN(xs);
2081 	} else {
2082 #ifdef	ISP2100_SCCLUN
2083 		t2reqp->req_scclun = XS_LUN(xs);
2084 #else
2085 		t2reqp->req_lun_trn = XS_LUN(xs);
2086 #endif
2087 	}
2088 	MEMCPY(reqp->req_cdb, XS_CDBP(xs), XS_CDBLEN(xs));
2089 
2090 	reqp->req_time = XS_TIME(xs) / 1000;
2091 	if (reqp->req_time == 0 && XS_TIME(xs))
2092 		reqp->req_time = 1;
2093 
2094 	/*
2095 	 * Always give a bit more leeway to commands after a bus reset.
2096 	 * XXX: DOES NOT DISTINGUISH WHICH PORT MAY HAVE BEEN SYNCED
2097 	 */
2098 	if (isp->isp_sendmarker && reqp->req_time < 5) {
2099 		reqp->req_time = 5;
2100 	}
2101 	if (isp_save_xs(isp, xs, &reqp->req_handle)) {
2102 		IDPRINTF(2, ("%s: out of xflist pointers\n", isp->isp_name));
2103 		XS_SETERR(xs, HBA_BOTCH);
2104 		return (CMD_EAGAIN);
2105 	}
2106 	/*
2107 	 * Set up DMA and/or do any bus swizzling of the request entry
2108 	 * so that the Qlogic F/W understands what is being asked of it.
2109  	*/
2110 	i = ISP_DMASETUP(isp, xs, reqp, &iptr, optr);
2111 	if (i != CMD_QUEUED) {
2112 		isp_destroy_handle(isp, reqp->req_handle);
2113 		/*
2114 		 * dmasetup sets actual error in packet, and
2115 		 * return what we were given to return.
2116 		 */
2117 		return (i);
2118 	}
2119 	XS_SETERR(xs, HBA_NOERROR);
2120 	IDPRINTF(5, ("%s(%d.%d.%d): START cmd 0x%x datalen %d\n",
2121 	    isp->isp_name, XS_CHANNEL(xs), target, XS_LUN(xs),
2122 	    reqp->req_cdb[0], XS_XFRLEN(xs)));
2123 	MemoryBarrier();
2124 	ISP_ADD_REQUEST(isp, iptr);
2125 	isp->isp_nactive++;
2126 	if (isp->isp_sendmarker)
2127 		isp->isp_sendmarker = 0;
2128 	return (CMD_QUEUED);
2129 #undef	reqp
2130 #undef	t2reqp
2131 }
2132 
2133 /*
2134  * isp control
2135  * Locks (ints blocked) assumed held.
2136  */
2137 
2138 int
2139 isp_control(isp, ctl, arg)
2140 	struct ispsoftc *isp;
2141 	ispctl_t ctl;
2142 	void *arg;
2143 {
2144 	ISP_SCSI_XFER_T *xs;
2145 	mbreg_t mbs;
2146 	int bus, tgt;
2147 	u_int32_t handle;
2148 
2149 	switch (ctl) {
2150 	default:
2151 		PRINTF("%s: isp_control unknown control op %x\n",
2152 		    isp->isp_name, ctl);
2153 		break;
2154 
2155 	case ISPCTL_RESET_BUS:
2156 		/*
2157 		 * Issue a bus reset.
2158 		 */
2159 		mbs.param[0] = MBOX_BUS_RESET;
2160 		mbs.param[2] = 0;
2161 		if (IS_SCSI(isp)) {
2162 			mbs.param[1] =
2163 			    ((sdparam *) isp->isp_param)->isp_bus_reset_delay;
2164 			if (mbs.param[1] < 2)
2165 				mbs.param[1] = 2;
2166 			bus = *((int *) arg);
2167 			if (IS_DUALBUS(isp))
2168 				mbs.param[2] = bus;
2169 		} else {
2170 			mbs.param[1] = 10;
2171 			bus = 0;
2172 		}
2173 		isp->isp_sendmarker = 1 << bus;
2174 		isp_mboxcmd(isp, &mbs);
2175 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2176 			isp_dumpregs(isp, "isp_control SCSI bus reset failed");
2177 			break;
2178 		}
2179 		CFGPRINTF("%s: driver initiated bus reset of bus %d\n",
2180 		    isp->isp_name, bus);
2181 		return (0);
2182 
2183 	case ISPCTL_RESET_DEV:
2184 		tgt = (*((int *) arg)) & 0xffff;
2185 		bus = (*((int *) arg)) >> 16;
2186 		mbs.param[0] = MBOX_ABORT_TARGET;
2187 		mbs.param[1] = (tgt << 8) | (bus << 15);
2188 		mbs.param[2] = 3;	/* 'delay', in seconds */
2189 		isp_mboxcmd(isp, &mbs);
2190 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2191 			PRINTF("%s: isp_control MBOX_RESET_DEV failure (code "
2192 			    "%x)\n", isp->isp_name, mbs.param[0]);
2193 			break;
2194 		}
2195 		PRINTF("%s: Target %d on Bus %d Reset Succeeded\n",
2196 		    isp->isp_name, tgt, bus);
2197 		isp->isp_sendmarker = 1 << bus;
2198 		return (0);
2199 
2200 	case ISPCTL_ABORT_CMD:
2201 		xs = (ISP_SCSI_XFER_T *) arg;
2202 		handle = isp_find_handle(isp, xs);
2203 		if (handle == 0) {
2204 			PRINTF("%s: isp_control- cannot find command to abort "
2205 			    "in active list\n", isp->isp_name);
2206 			break;
2207 		}
2208 		bus = XS_CHANNEL(xs);
2209 		mbs.param[0] = MBOX_ABORT;
2210 		if (IS_FC(isp)) {
2211 #ifdef	ISP2100_SCCLUN
2212 			mbs.param[1] = XS_TGT(xs) << 8;
2213 			mbs.param[4] = 0;
2214 			mbs.param[5] = 0;
2215 			mbs.param[6] = XS_LUN(xs);
2216 #else
2217 			mbs.param[1] = XS_TGT(xs) << 8 | XS_LUN(xs);
2218 #endif
2219 		} else {
2220 			mbs.param[1] =
2221 			    (bus << 15) | (XS_TGT(xs) << 8) | XS_LUN(xs);
2222 		}
2223 		mbs.param[3] = handle >> 16;
2224 		mbs.param[2] = handle & 0xffff;
2225 		isp_mboxcmd(isp, &mbs);
2226 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2227 			PRINTF("%s: isp_control MBOX_ABORT failure (code %x)\n",
2228 			    isp->isp_name, mbs.param[0]);
2229 			break;
2230 		}
2231 		PRINTF("%s: command for target %d lun %d was aborted\n",
2232 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
2233 		return (0);
2234 
2235 	case ISPCTL_UPDATE_PARAMS:
2236 		isp_update(isp);
2237 		return (0);
2238 
2239 	case ISPCTL_FCLINK_TEST:
2240 		return (isp_fclink_test(isp, FC_FW_READY_DELAY));
2241 
2242 	case ISPCTL_PDB_SYNC:
2243 		return (isp_pdb_sync(isp, -1));
2244 
2245 #ifdef	ISP_TARGET_MODE
2246 	case ISPCTL_TOGGLE_TMODE:
2247 		if (IS_SCSI(isp)) {
2248 			int ena = *(int *)arg;
2249 			mbs.param[0] = MBOX_ENABLE_TARGET_MODE;
2250 			mbs.param[1] = (ena)? ENABLE_TARGET_FLAG : 0;
2251 			isp_mboxcmd(isp, &mbs);
2252 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2253 				PRINTF("%s: cannot %sable target mode (0x%x)\n",
2254 				    isp->isp_name, ena? "en" : "dis",
2255 				    mbs.param[0]);
2256 				break;
2257 			}
2258 		}
2259 		return (0);
2260 #endif
2261 	}
2262 	return (-1);
2263 }
2264 
2265 /*
2266  * Interrupt Service Routine(s).
2267  *
2268  * External (OS) framework has done the appropriate locking,
2269  * and the locking will be held throughout this function.
2270  */
2271 
2272 int
2273 isp_intr(arg)
2274 	void *arg;
2275 {
2276 	ISP_SCSI_XFER_T *complist[RESULT_QUEUE_LEN], *xs;
2277 	struct ispsoftc *isp = arg;
2278 	u_int16_t iptr, optr;
2279 	u_int16_t isr, isrb, sema;
2280 	int i, nlooked = 0, ndone = 0;
2281 
2282 	/*
2283 	 * Well, if we've disabled interrupts, we may get a case where
2284 	 * isr isn't set, but sema is. In any case, debounce isr reads.
2285 	 */
2286 	do {
2287 		isr = ISP_READ(isp, BIU_ISR);
2288 		isrb = ISP_READ(isp, BIU_ISR);
2289 	} while (isr != isrb);
2290 	sema = ISP_READ(isp, BIU_SEMA) & 0x1;
2291 	IDPRINTF(5, ("%s: isp_intr isr %x sem %x\n", isp->isp_name, isr, sema));
2292 	if (isr == 0) {
2293 		return (0);
2294 	}
2295 	if (!INT_PENDING(isp, isr)) {
2296 		IDPRINTF(4, ("%s: isp_intr isr=%x\n", isp->isp_name, isr));
2297 		return (0);
2298 	}
2299 	if (isp->isp_state != ISP_RUNSTATE) {
2300 		IDPRINTF(3, ("%s: interrupt (isr=%x,sema=%x) when not ready\n",
2301 		    isp->isp_name, isr, sema));
2302 		ISP_WRITE(isp, INMAILBOX5, ISP_READ(isp, OUTMAILBOX5));
2303 		ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
2304 		ISP_WRITE(isp, BIU_SEMA, 0);
2305 		ENABLE_INTS(isp);
2306 		return (1);
2307 	}
2308 
2309 	if (sema) {
2310 		u_int16_t mbox = ISP_READ(isp, OUTMAILBOX0);
2311 		if (mbox & 0x4000) {
2312 			IDPRINTF(4, ("%s: Command Mbox 0x%x\n",
2313 			    isp->isp_name, mbox));
2314 		} else {
2315 			u_int32_t fhandle = isp_parse_async(isp, (int) mbox);
2316 			IDPRINTF(4, ("%s: Async Mbox 0x%x\n",
2317 			    isp->isp_name, mbox));
2318 			if (fhandle > 0) {
2319 				isp_fastpost_complete(isp, fhandle);
2320 			}
2321 		}
2322 		ISP_WRITE(isp, BIU_SEMA, 0);
2323 		ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
2324 		ENABLE_INTS(isp);
2325 		return (1);
2326 	}
2327 
2328 	/*
2329 	 * You *must* read OUTMAILBOX5 prior to clearing the RISC interrupt.
2330 	 */
2331 	optr = isp->isp_residx;
2332 	iptr = ISP_READ(isp, OUTMAILBOX5);
2333 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
2334 	if (optr == iptr) {
2335 		IDPRINTF(4, ("why intr? isr %x iptr %x optr %x\n",
2336 		    isr, optr, iptr));
2337 	}
2338 
2339 	while (optr != iptr) {
2340 		ispstatusreq_t *sp;
2341 		u_int16_t oop;
2342 		int buddaboom = 0;
2343 
2344 		sp = (ispstatusreq_t *) ISP_QUEUE_ENTRY(isp->isp_result, optr);
2345 		oop = optr;
2346 		optr = ISP_NXT_QENTRY(optr, RESULT_QUEUE_LEN);
2347 		nlooked++;
2348 		MemoryBarrier();
2349 		/*
2350 		 * Do any appropriate unswizzling of what the Qlogic f/w has
2351 		 * written into memory so it makes sense to us. This is a
2352 		 * per-platform thing.
2353 		 */
2354 		ISP_UNSWIZZLE_RESPONSE(isp, sp);
2355 		if (sp->req_header.rqs_entry_type != RQSTYPE_RESPONSE) {
2356 			if (isp_handle_other_response(isp, sp, &optr) == 0) {
2357 				continue;
2358 			}
2359 			/*
2360 			 * It really has to be a bounced request just copied
2361 			 * from the request queue to the response queue. If
2362 			 * not, something bad has happened.
2363 			 */
2364 			if (sp->req_header.rqs_entry_type != RQSTYPE_REQUEST) {
2365 				PRINTF("%s: not RESPONSE in RESPONSE Queue "
2366 				    "(type 0x%x) @ idx %d (next %d)\n",
2367 				    isp->isp_name,
2368 				    sp->req_header.rqs_entry_type, oop, optr);
2369 				continue;
2370 			}
2371 			buddaboom = 1;
2372 		}
2373 
2374 		if (sp->req_header.rqs_flags & 0xf) {
2375 #define	_RQS_OFLAGS	\
2376 	~(RQSFLAG_CONTINUATION|RQSFLAG_FULL|RQSFLAG_BADHEADER|RQSFLAG_BADPACKET)
2377 			if (sp->req_header.rqs_flags & RQSFLAG_CONTINUATION) {
2378 				IDPRINTF(4, ("%s: continuation segment\n",
2379 				    isp->isp_name));
2380 				ISP_WRITE(isp, INMAILBOX5, optr);
2381 				continue;
2382 			}
2383 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
2384 				IDPRINTF(2, ("%s: internal queues full\n",
2385 				    isp->isp_name));
2386 				/*
2387 				 * We'll synthesize a QUEUE FULL message below.
2388 				 */
2389 			}
2390 			if (sp->req_header.rqs_flags & RQSFLAG_BADHEADER) {
2391 				PRINTF("%s: bad header\n", isp->isp_name);
2392 				buddaboom++;
2393 			}
2394 			if (sp->req_header.rqs_flags & RQSFLAG_BADPACKET) {
2395 				PRINTF("%s: bad request packet\n",
2396 				    isp->isp_name);
2397 				buddaboom++;
2398 			}
2399 			if (sp->req_header.rqs_flags & _RQS_OFLAGS) {
2400 				PRINTF("%s: unknown flags in response (0x%x)\n",
2401 				    isp->isp_name, sp->req_header.rqs_flags);
2402 				buddaboom++;
2403 			}
2404 #undef	_RQS_OFLAGS
2405 		}
2406 		if (sp->req_handle > isp->isp_maxcmds || sp->req_handle < 1) {
2407 			PRINTF("%s: bad request handle %d\n", isp->isp_name,
2408 			    sp->req_handle);
2409 			ISP_WRITE(isp, INMAILBOX5, optr);
2410 			continue;
2411 		}
2412 		xs = isp_find_xs(isp, sp->req_handle);
2413 		if (xs == NULL) {
2414 			PRINTF("%s: NULL xs in xflist (handle 0x%x)\n",
2415 			    isp->isp_name, sp->req_handle);
2416 			ISP_WRITE(isp, INMAILBOX5, optr);
2417 			continue;
2418 		}
2419 		isp_destroy_handle(isp, sp->req_handle);
2420 		if (sp->req_status_flags & RQSTF_BUS_RESET) {
2421 			isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
2422 		}
2423 		if (buddaboom) {
2424 			XS_SETERR(xs, HBA_BOTCH);
2425 		}
2426 		XS_STS(xs) = sp->req_scsi_status & 0xff;
2427 		if (IS_SCSI(isp)) {
2428 			if (sp->req_state_flags & RQSF_GOT_SENSE) {
2429 				MEMCPY(XS_SNSP(xs), sp->req_sense_data,
2430 					XS_SNSLEN(xs));
2431 				XS_SNS_IS_VALID(xs);
2432 			}
2433 			/*
2434 			 * A new synchronous rate was negotiated for this
2435 			 * target. Mark state such that we'll go look up
2436 			 * that which has changed later.
2437 			 */
2438 			if (sp->req_status_flags & RQSTF_NEGOTIATION) {
2439 				sdparam *sdp = isp->isp_param;
2440 				sdp += XS_CHANNEL(xs);
2441 				sdp->isp_devparam[XS_TGT(xs)].dev_refresh = 1;
2442 				isp->isp_update |= (1 << XS_CHANNEL(xs));
2443 			}
2444 		} else {
2445 			if (sp->req_scsi_status & RQCS_SV) {
2446 				int amt = min(XS_SNSLEN(xs), sp->req_sense_len);
2447 				MEMCPY(XS_SNSP(xs), sp->req_sense_data, amt);
2448 				XS_SNS_IS_VALID(xs);
2449 				sp->req_state_flags |= RQSF_GOT_SENSE;
2450 			} else if (XS_STS(xs) == SCSI_CHECK) {
2451 				IDPRINTF(1, ("%s: check condition with no sense"
2452 				    " data\n", isp->isp_name));
2453 			}
2454 		}
2455 		if (XS_NOERR(xs) && XS_STS(xs) == SCSI_BUSY) {
2456 			XS_SETERR(xs, HBA_TGTBSY);
2457 		}
2458 
2459 		if (sp->req_header.rqs_entry_type == RQSTYPE_RESPONSE) {
2460 			if (XS_NOERR(xs)) {
2461 			    if (sp->req_completion_status != RQCS_COMPLETE) {
2462 				isp_parse_status(isp, sp, xs);
2463 			    } else {
2464 				XS_SETERR(xs, HBA_NOERROR);
2465 			    }
2466 			}
2467 		} else if (sp->req_header.rqs_entry_type == RQSTYPE_REQUEST) {
2468 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
2469 				/*
2470 				 * Force Queue Full status.
2471 				 */
2472 				XS_STS(xs) = SCSI_QFULL;
2473 				XS_SETERR(xs, HBA_NOERROR);
2474 			} else if (XS_NOERR(xs)) {
2475 				XS_SETERR(xs, HBA_BOTCH);
2476 			}
2477 		} else {
2478 			PRINTF("%s: unhandled respose queue type 0x%x\n",
2479 			    isp->isp_name, sp->req_header.rqs_entry_type);
2480 			if (XS_NOERR(xs)) {
2481 				XS_SETERR(xs, HBA_BOTCH);
2482 			}
2483 		}
2484 		if (IS_SCSI(isp)) {
2485 			XS_RESID(xs) = sp->req_resid;
2486 		} else if (sp->req_scsi_status & RQCS_RU) {
2487 			XS_RESID(xs) = sp->req_resid;
2488 			IDPRINTF(4, ("%s: cnt %d rsd %d\n", isp->isp_name,
2489 				XS_XFRLEN(xs), sp->req_resid));
2490 		}
2491 		if (XS_XFRLEN(xs)) {
2492 			ISP_DMAFREE(isp, xs, sp->req_handle);
2493 		}
2494 		/*
2495 		 * Let the platforms cope.
2496 		 */
2497 #if	0
2498 		/*
2499 		 * XXX: If we have a check condition, but no Sense Data,
2500 		 * XXX: mark it as an error (ARQ failed). We need to
2501 		 * XXX: to do a more distinct job because there may
2502 		 * XXX: cases where ARQ is disabled.
2503 		 */
2504 		if (XS_STS(xs) == SCSI_CHECK && !(XS_IS_SNS_VALID(xs))) {
2505 			if (XS_NOERR(xs)) {
2506 				PRINTF("%s: ARQ failure for target %d lun %d\n",
2507 				    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
2508 				XS_SETERR(xs, HBA_ARQFAIL);
2509 			}
2510 		}
2511 #endif
2512 		if ((isp->isp_dblev >= 5) ||
2513 		    (isp->isp_dblev > 2 && !XS_NOERR(xs))) {
2514 			PRINTF("%s(%d.%d): FIN dl%d resid%d STS %x",
2515 			    isp->isp_name, XS_TGT(xs), XS_LUN(xs),
2516 			    XS_XFRLEN(xs), XS_RESID(xs), XS_STS(xs));
2517 			if (sp->req_state_flags & RQSF_GOT_SENSE) {
2518 				PRINTF(" Skey: %x", XS_SNSKEY(xs));
2519 				if (!(XS_IS_SNS_VALID(xs))) {
2520 					PRINTF(" BUT NOT SET");
2521 				}
2522 			}
2523 			PRINTF(" XS_ERR=0x%x\n", (unsigned int) XS_ERR(xs));
2524 		}
2525 
2526 		if (isp->isp_nactive > 0)
2527 		    isp->isp_nactive--;
2528 		complist[ndone++] = xs;	/* defer completion call until later */
2529 	}
2530 
2531 	/*
2532 	 * If we looked at any commands, then it's valid to find out
2533 	 * what the outpointer is. It also is a trigger to update the
2534 	 * ISP's notion of what we've seen so far.
2535 	 */
2536 	if (nlooked) {
2537 		ISP_WRITE(isp, INMAILBOX5, optr);
2538 		isp->isp_reqodx = ISP_READ(isp, OUTMAILBOX4);
2539 	}
2540 	isp->isp_residx = optr;
2541 	for (i = 0; i < ndone; i++) {
2542 		xs = complist[i];
2543 		if (xs) {
2544 			XS_CMD_DONE(xs);
2545 		}
2546 	}
2547 	ENABLE_INTS(isp);
2548 	return (1);
2549 }
2550 
2551 /*
2552  * Support routines.
2553  */
2554 
2555 static int
2556 isp_parse_async(isp, mbox)
2557 	struct ispsoftc *isp;
2558 	int mbox;
2559 {
2560 	int bus;
2561 	u_int32_t fast_post_handle = 0;
2562 
2563 	if (IS_DUALBUS(isp)) {
2564 		bus = ISP_READ(isp, OUTMAILBOX6);
2565 	} else {
2566 		bus = 0;
2567 	}
2568 
2569 	switch (mbox) {
2570 	case MBOX_COMMAND_COMPLETE:	/* sometimes these show up */
2571 		break;
2572 	case ASYNC_BUS_RESET:
2573 		isp->isp_sendmarker = (1 << bus);
2574 #ifdef	ISP_TARGET_MODE
2575 		isp_target_async(isp, bus, mbox);
2576 #endif
2577 		isp_async(isp, ISPASYNC_BUS_RESET, &bus);
2578 		break;
2579 	case ASYNC_SYSTEM_ERROR:
2580 		mbox = ISP_READ(isp, OUTMAILBOX1);
2581 		PRINTF("%s: Internal FW Error @ RISC Addr 0x%x\n",
2582 		    isp->isp_name, mbox);
2583 		isp_restart(isp);
2584 		/* no point continuing after this */
2585 		return (-1);
2586 
2587 	case ASYNC_RQS_XFER_ERR:
2588 		PRINTF("%s: Request Queue Transfer Error\n", isp->isp_name);
2589 		break;
2590 
2591 	case ASYNC_RSP_XFER_ERR:
2592 		PRINTF("%s: Response Queue Transfer Error\n", isp->isp_name);
2593 		break;
2594 
2595 	case ASYNC_QWAKEUP:
2596 		/*
2597 		 * We've just been notified that the Queue has woken up.
2598 		 * We don't need to be chatty about this- just unlatch things
2599 		 * and move on.
2600 		 */
2601 		mbox = ISP_READ(isp, OUTMAILBOX4);
2602 		break;
2603 
2604 	case ASYNC_TIMEOUT_RESET:
2605 		PRINTF("%s: timeout initiated SCSI bus reset of bus %d\n",
2606 		    isp->isp_name, bus);
2607 		isp->isp_sendmarker = (1 << bus);
2608 #ifdef	ISP_TARGET_MODE
2609 		isp_target_async(isp, bus, mbox);
2610 #endif
2611 		break;
2612 
2613 	case ASYNC_DEVICE_RESET:
2614 		PRINTF("%s: device reset on bus %d\n", isp->isp_name, bus);
2615 		isp->isp_sendmarker = 1 << bus;
2616 #ifdef	ISP_TARGET_MODE
2617 		isp_target_async(isp, bus, mbox);
2618 #endif
2619 		break;
2620 
2621 	case ASYNC_EXTMSG_UNDERRUN:
2622 		PRINTF("%s: extended message underrun\n", isp->isp_name);
2623 		break;
2624 
2625 	case ASYNC_SCAM_INT:
2626 		PRINTF("%s: SCAM interrupt\n", isp->isp_name);
2627 		break;
2628 
2629 	case ASYNC_HUNG_SCSI:
2630 		PRINTF("%s: stalled SCSI Bus after DATA Overrun\n",
2631 		    isp->isp_name);
2632 		/* XXX: Need to issue SCSI reset at this point */
2633 		break;
2634 
2635 	case ASYNC_KILLED_BUS:
2636 		PRINTF("%s: SCSI Bus reset after DATA Overrun\n",
2637 		    isp->isp_name);
2638 		break;
2639 
2640 	case ASYNC_BUS_TRANSIT:
2641 		mbox = ISP_READ(isp, OUTMAILBOX2);
2642 		switch (mbox & 0x1c00) {
2643 		case SXP_PINS_LVD_MODE:
2644 			PRINTF("%s: Transition to LVD mode\n", isp->isp_name);
2645 			((sdparam *)isp->isp_param)->isp_diffmode = 0;
2646 			((sdparam *)isp->isp_param)->isp_ultramode = 0;
2647 			((sdparam *)isp->isp_param)->isp_lvdmode = 1;
2648 			break;
2649 		case SXP_PINS_HVD_MODE:
2650 			PRINTF("%s: Transition to Differential mode\n",
2651 			    isp->isp_name);
2652 			((sdparam *)isp->isp_param)->isp_diffmode = 1;
2653 			((sdparam *)isp->isp_param)->isp_ultramode = 0;
2654 			((sdparam *)isp->isp_param)->isp_lvdmode = 0;
2655 			break;
2656 		case SXP_PINS_SE_MODE:
2657 			PRINTF("%s: Transition to Single Ended mode\n",
2658 			    isp->isp_name);
2659 			((sdparam *)isp->isp_param)->isp_diffmode = 0;
2660 			((sdparam *)isp->isp_param)->isp_ultramode = 1;
2661 			((sdparam *)isp->isp_param)->isp_lvdmode = 0;
2662 			break;
2663 		default:
2664 			PRINTF("%s: Transition to unknown mode 0x%x\n",
2665 			    isp->isp_name, mbox);
2666 			break;
2667 		}
2668 		/*
2669 		 * XXX: Set up to renegotiate again!
2670 		 */
2671 		/* Can only be for a 1080... */
2672 		isp->isp_sendmarker = (1 << bus);
2673 		break;
2674 
2675 	case ASYNC_CMD_CMPLT:
2676 		fast_post_handle = (ISP_READ(isp, OUTMAILBOX2) << 16) |
2677 		    ISP_READ(isp, OUTMAILBOX1);
2678 		IDPRINTF(4, ("%s: fast post completion of %u\n", isp->isp_name,
2679 		    fast_post_handle));
2680 		break;
2681 
2682 	case ASYNC_CTIO_DONE:
2683 		/* Should only occur when Fast Posting Set for 2100s */
2684 		PRINTF("%s: CTIO done\n", isp->isp_name);
2685 		break;
2686 
2687 	case ASYNC_LIP_OCCURRED:
2688 		((fcparam *) isp->isp_param)->isp_lipseq =
2689 		    ISP_READ(isp, OUTMAILBOX1);
2690 		((fcparam *) isp->isp_param)->isp_fwstate = FW_CONFIG_WAIT;
2691 		((fcparam *) isp->isp_param)->isp_loopstate = LOOP_LIP_RCVD;
2692 		isp->isp_sendmarker = 1;
2693 		isp_mark_getpdb_all(isp);
2694 		IDPRINTF(1, ("%s: LIP occurred\n", isp->isp_name));
2695 #ifdef	ISP_TARGET_MODE
2696 		isp_target_async(isp, bus, mbox);
2697 #endif
2698 		break;
2699 
2700 	case ASYNC_LOOP_UP:
2701 		isp->isp_sendmarker = 1;
2702 		((fcparam *) isp->isp_param)->isp_fwstate = FW_CONFIG_WAIT;
2703 		((fcparam *) isp->isp_param)->isp_loopstate = LOOP_LIP_RCVD;
2704 		isp_mark_getpdb_all(isp);
2705 		isp_async(isp, ISPASYNC_LOOP_UP, NULL);
2706 #ifdef	ISP_TARGET_MODE
2707 		isp_target_async(isp, bus, mbox);
2708 #endif
2709 		break;
2710 
2711 	case ASYNC_LOOP_DOWN:
2712 		isp->isp_sendmarker = 1;
2713 		((fcparam *) isp->isp_param)->isp_fwstate = FW_CONFIG_WAIT;
2714 		((fcparam *) isp->isp_param)->isp_loopstate = LOOP_NIL;
2715 		isp_mark_getpdb_all(isp);
2716 		isp_async(isp, ISPASYNC_LOOP_DOWN, NULL);
2717 #ifdef	ISP_TARGET_MODE
2718 		isp_target_async(isp, bus, mbox);
2719 #endif
2720 		break;
2721 
2722 	case ASYNC_LOOP_RESET:
2723 		isp->isp_sendmarker = 1 << bus;
2724 		((fcparam *) isp->isp_param)->isp_fwstate = FW_CONFIG_WAIT;
2725 		((fcparam *) isp->isp_param)->isp_loopstate = LOOP_NIL;
2726 		isp_mark_getpdb_all(isp);
2727 		PRINTF("%s: Loop RESET\n", isp->isp_name);
2728 #ifdef	ISP_TARGET_MODE
2729 		isp_target_async(isp, bus, mbox);
2730 #endif
2731 		break;
2732 
2733 	case ASYNC_PDB_CHANGED:
2734 		isp->isp_sendmarker = 1;
2735 		((fcparam *) isp->isp_param)->isp_loopstate = LOOP_PDB_RCVD;
2736 		isp_mark_getpdb_all(isp);
2737 		IDPRINTF(2, ("%s: Port Database Changed\n", isp->isp_name));
2738 		break;
2739 
2740 	case ASYNC_CHANGE_NOTIFY:
2741 		isp_mark_getpdb_all(isp);
2742 		/*
2743 		 * Not correct, but it will force us to rescan the loop.
2744 		 */
2745 		((fcparam *) isp->isp_param)->isp_loopstate = LOOP_PDB_RCVD;
2746 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, NULL);
2747 		break;
2748 
2749 	case ASYNC_PTPMODE:
2750 		PRINTF("%s: Point-to-Point mode\n", isp->isp_name);
2751 		break;
2752 
2753 	case ASYNC_CONNMODE:
2754 		mbox = ISP_READ(isp, OUTMAILBOX1);
2755 		switch (mbox) {
2756 		case ISP_CONN_LOOP:
2757 			PRINTF("%s: Point-to-Point -> Loop mode\n",
2758 			    isp->isp_name);
2759 			break;
2760 		case ISP_CONN_PTP:
2761 			PRINTF("%s: Loop -> Point-to-Point mode\n",
2762 			    isp->isp_name);
2763 			break;
2764 		case ISP_CONN_BADLIP:
2765 			PRINTF("%s: Point-to-Point -> Loop mode (1)\n",
2766 			    isp->isp_name);
2767 			break;
2768 		case ISP_CONN_FATAL:
2769 			PRINTF("%s: FATAL CONNECTION ERROR\n", isp->isp_name);
2770 			isp_restart(isp);
2771 			/* no point continuing after this */
2772 			return (-1);
2773 
2774 		case ISP_CONN_LOOPBACK:
2775 			PRINTF("%s: Looped Back in Point-to-Point mode\n",
2776 			     isp->isp_name);
2777 		}
2778 		break;
2779 
2780 	default:
2781 		PRINTF("%s: unknown async code 0x%x\n", isp->isp_name, mbox);
2782 		break;
2783 	}
2784 	return (fast_post_handle);
2785 }
2786 
2787 /*
2788  * Handle other response entries. A pointer to the request queue output
2789  * index is here in case we want to eat several entries at once, although
2790  * this is not used currently.
2791  */
2792 
2793 static int
2794 isp_handle_other_response(isp, sp, optrp)
2795 	struct ispsoftc *isp;
2796 	ispstatusreq_t *sp;
2797 	u_int16_t *optrp;
2798 {
2799 	switch (sp->req_header.rqs_entry_type) {
2800 	case RQSTYPE_ATIO:
2801 	case RQSTYPE_CTIO:
2802 	case RQSTYPE_ENABLE_LUN:
2803 	case RQSTYPE_MODIFY_LUN:
2804 	case RQSTYPE_NOTIFY:
2805 	case RQSTYPE_NOTIFY_ACK:
2806 	case RQSTYPE_CTIO1:
2807 	case RQSTYPE_ATIO2:
2808 	case RQSTYPE_CTIO2:
2809 	case RQSTYPE_CTIO3:
2810 #ifdef	ISP_TARGET_MODE
2811 		return (isp_target_notify(isp, sp, optrp));
2812 #else
2813 		/* FALLTHROUGH */
2814 #endif
2815 	case RQSTYPE_REQUEST:
2816 	default:
2817 		PRINTF("%s: unhandled response type 0x%x\n", isp->isp_name,
2818 		    sp->req_header.rqs_entry_type);
2819 		return (-1);
2820 	}
2821 }
2822 
2823 static void
2824 isp_parse_status(isp, sp, xs)
2825 	struct ispsoftc *isp;
2826 	ispstatusreq_t *sp;
2827 	ISP_SCSI_XFER_T *xs;
2828 {
2829 	switch (sp->req_completion_status) {
2830 	case RQCS_COMPLETE:
2831 		XS_SETERR(xs, HBA_NOERROR);
2832 		return;
2833 
2834 	case RQCS_INCOMPLETE:
2835 		if ((sp->req_state_flags & RQSF_GOT_TARGET) == 0) {
2836 			IDPRINTF(3, ("%s: Selection Timeout for %d.%d.%d\n",
2837 			    isp->isp_name, XS_TGT(xs), XS_LUN(xs),
2838 			    XS_CHANNEL(xs)));
2839 			XS_SETERR(xs, HBA_SELTIMEOUT);
2840 			return;
2841 		}
2842 		PRINTF("%s: command incomplete for %d.%d.%d, state 0x%x\n",
2843 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs),
2844 		    sp->req_state_flags);
2845 		break;
2846 
2847 	case RQCS_DMA_ERROR:
2848 		PRINTF("%s: DMA error for command on %d.%d.%d\n",
2849 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
2850 		break;
2851 
2852 	case RQCS_TRANSPORT_ERROR:
2853 		PRINTF("%s: transport error for %d.%d.%d\n",
2854 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
2855 		isp_prtstst(sp);
2856 		break;
2857 
2858 	case RQCS_RESET_OCCURRED:
2859 		IDPRINTF(1, ("%s: bus reset destroyed command for %d.%d.%d\n",
2860 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs)));
2861 		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
2862 		XS_SETERR(xs, HBA_BUSRESET);
2863 		return;
2864 
2865 	case RQCS_ABORTED:
2866 		PRINTF("%s: command aborted for %d.%d.%d\n",
2867 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
2868 		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
2869 		XS_SETERR(xs, HBA_ABORTED);
2870 		return;
2871 
2872 	case RQCS_TIMEOUT:
2873 		IDPRINTF(2, ("%s: command timed out for %d.%d.%d\n",
2874 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs)));
2875 		XS_SETERR(xs, HBA_CMDTIMEOUT);
2876 		return;
2877 
2878 	case RQCS_DATA_OVERRUN:
2879 		if (IS_FC(isp)) {
2880 			XS_RESID(xs) = sp->req_resid;
2881 			break;
2882 		}
2883 		PRINTF("%s: data overrun for command on %d.%d.%d\n",
2884 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
2885 		XS_SETERR(xs, HBA_DATAOVR);
2886 		return;
2887 
2888 	case RQCS_COMMAND_OVERRUN:
2889 		PRINTF("%s: command overrun for command on %d.%d.%d\n",
2890 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
2891 		break;
2892 
2893 	case RQCS_STATUS_OVERRUN:
2894 		PRINTF("%s: status overrun for command on %d.%d.%d\n",
2895 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
2896 		break;
2897 
2898 	case RQCS_BAD_MESSAGE:
2899 		PRINTF("%s: msg not COMMAND COMPLETE after status %d.%d.%d\n",
2900 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
2901 		break;
2902 
2903 	case RQCS_NO_MESSAGE_OUT:
2904 		PRINTF("%s: No MESSAGE OUT phase after selection on %d.%d.%d\n",
2905 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
2906 		break;
2907 
2908 	case RQCS_EXT_ID_FAILED:
2909 		PRINTF("%s: EXTENDED IDENTIFY failed %d.%d.%d\n",
2910 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
2911 		break;
2912 
2913 	case RQCS_IDE_MSG_FAILED:
2914 		PRINTF("%s: INITIATOR DETECTED ERROR rejected by %d.%d.%d\n",
2915 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
2916 		break;
2917 
2918 	case RQCS_ABORT_MSG_FAILED:
2919 		PRINTF("%s: ABORT OPERATION rejected by %d.%d.%d\n",
2920 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
2921 		break;
2922 
2923 	case RQCS_REJECT_MSG_FAILED:
2924 		PRINTF("%s: MESSAGE REJECT rejected by %d.%d.%d\n",
2925 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
2926 		break;
2927 
2928 	case RQCS_NOP_MSG_FAILED:
2929 		PRINTF("%s: NOP rejected by %d.%d.%d\n",
2930 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
2931 		break;
2932 
2933 	case RQCS_PARITY_ERROR_MSG_FAILED:
2934 		PRINTF("%s: MESSAGE PARITY ERROR rejected by %d.%d.%d\n",
2935 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
2936 		break;
2937 
2938 	case RQCS_DEVICE_RESET_MSG_FAILED:
2939 		PRINTF("%s: BUS DEVICE RESET rejected by %d.%d.%d\n",
2940 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
2941 		break;
2942 
2943 	case RQCS_ID_MSG_FAILED:
2944 		PRINTF("%s: IDENTIFY rejected by %d.%d.%d\n",
2945 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
2946 		break;
2947 
2948 	case RQCS_UNEXP_BUS_FREE:
2949 		PRINTF("%s: %d.%d.%d had an unexpected bus free\n",
2950 		    isp->isp_name, XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
2951 		break;
2952 
2953 	case RQCS_DATA_UNDERRUN:
2954 		if (IS_FC(isp)) {
2955 			XS_RESID(xs) = sp->req_resid;
2956 		}
2957 		XS_SETERR(xs, HBA_NOERROR);
2958 		return;
2959 
2960 	case RQCS_XACT_ERR1:
2961 		PRINTF("%s: HBA attempted queued transaction with disconnect "
2962 		    "not set for %d.%d.%d\n", isp->isp_name, XS_CHANNEL(xs),
2963 		    XS_TGT(xs), XS_LUN(xs));
2964 		break;
2965 
2966 	case RQCS_XACT_ERR2:
2967 		PRINTF("%s: HBA attempted queued transaction to target "
2968 		    "routine %d on target %d, bus %d\n", isp->isp_name,
2969 		    XS_LUN(xs), XS_TGT(xs), XS_CHANNEL(xs));
2970 		break;
2971 
2972 	case RQCS_XACT_ERR3:
2973 		PRINTF("%s: HBA attempted queued transaction for target %d lun "
2974 		    "%d on bus %d when queueing disabled\n", isp->isp_name,
2975 		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
2976 		break;
2977 
2978 	case RQCS_BAD_ENTRY:
2979 		PRINTF("%s: invalid IOCB entry type detected\n", isp->isp_name);
2980 		break;
2981 
2982 	case RQCS_QUEUE_FULL:
2983 		IDPRINTF(3, ("%s: internal queues full for target %d lun %d "
2984 		    "bus %d, status 0x%x\n", isp->isp_name, XS_TGT(xs),
2985 		    XS_LUN(xs), XS_CHANNEL(xs), XS_STS(xs)));
2986 		/*
2987 		 * If QFULL or some other status byte is set, then this
2988 		 * isn't an error, per se.
2989 		 */
2990 		if (XS_STS(xs) != 0) {
2991 			XS_SETERR(xs, HBA_NOERROR);
2992 			return;
2993 		}
2994 		break;
2995 
2996 	case RQCS_PHASE_SKIPPED:
2997 		PRINTF("%s: SCSI phase skipped (e.g., COMMAND COMPLETE w/o "
2998 		    "STATUS phase) for target %d lun %d bus %d\n",
2999 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
3000 		break;
3001 
3002 	case RQCS_ARQS_FAILED:
3003 		PRINTF("%s: Auto Request Sense failed for %d.%d.%d\n",
3004 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
3005 		return;
3006 
3007 	case RQCS_WIDE_FAILED:
3008 		PRINTF("%s: Wide Negotiation failed for %d.%d.%d\n",
3009 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
3010 		if (IS_SCSI(isp)) {
3011 			sdparam *sdp = isp->isp_param;
3012 			sdp += XS_CHANNEL(xs);
3013 			sdp->isp_devparam[XS_TGT(xs)].dev_flags &= ~DPARM_WIDE;
3014 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
3015 			isp->isp_update |= (1 << XS_CHANNEL(xs));
3016 		}
3017 		XS_SETERR(xs, HBA_NOERROR);
3018 		return;
3019 
3020 	case RQCS_SYNCXFER_FAILED:
3021 		PRINTF("%s: SDTR Message failed for target %d.%d.%d\n",
3022 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
3023 		if (IS_SCSI(isp)) {
3024 			sdparam *sdp = isp->isp_param;
3025 			sdp += XS_CHANNEL(xs);
3026 			sdp->isp_devparam[XS_TGT(xs)].dev_flags &= ~DPARM_SYNC;
3027 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
3028 			isp->isp_update |= (1 << XS_CHANNEL(xs));
3029 		}
3030 		break;
3031 
3032 	case RQCS_LVD_BUSERR:
3033 		PRINTF("%s: Bad LVD condition while talking to %d.%d.%d\n",
3034 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
3035 		break;
3036 
3037 	case RQCS_PORT_UNAVAILABLE:
3038 		/*
3039 		 * No such port on the loop. Moral equivalent of SELTIMEO
3040 		 */
3041 		IDPRINTF(3, ("%s: Port Unavailable for target %d\n",
3042 		    isp->isp_name, XS_TGT(xs)));
3043 		XS_SETERR(xs, HBA_SELTIMEOUT);
3044 		return;
3045 
3046 	case RQCS_PORT_LOGGED_OUT:
3047 		/*
3048 		 * It was there (maybe)- treat as a selection timeout.
3049 		 */
3050 		IDPRINTF(2, ("%s: port logout for target %d\n",
3051 			isp->isp_name, XS_TGT(xs)));
3052 		XS_SETERR(xs, HBA_SELTIMEOUT);
3053 		return;
3054 
3055 	case RQCS_PORT_CHANGED:
3056 		PRINTF("%s: port changed for target %d\n",
3057 		    isp->isp_name, XS_TGT(xs));
3058 		XS_SETERR(xs, HBA_SELTIMEOUT);
3059 		return;
3060 
3061 	case RQCS_PORT_BUSY:
3062 		PRINTF("%s: port busy for target %d\n",
3063 		    isp->isp_name, XS_TGT(xs));
3064 		XS_SETERR(xs, HBA_TGTBSY);
3065 		return;
3066 
3067 	default:
3068 		PRINTF("%s: completion status 0x%x\n",
3069 		    isp->isp_name, sp->req_completion_status);
3070 		break;
3071 	}
3072 	XS_SETERR(xs, HBA_BOTCH);
3073 }
3074 
3075 static void
3076 isp_fastpost_complete(isp, fph)
3077 	struct ispsoftc *isp;
3078 	u_int32_t fph;
3079 {
3080 	ISP_SCSI_XFER_T *xs;
3081 
3082 	if (fph < 1) {
3083 		return;
3084 	}
3085 	xs = isp_find_xs(isp, fph);
3086 	if (xs == NULL) {
3087 		PRINTF("%s: command for fast posting handle 0x%x not found\n",
3088 		    isp->isp_name, fph);
3089 		return;
3090 	}
3091 	isp_destroy_handle(isp, fph);
3092 
3093 	/*
3094 	 * Since we don't have a result queue entry item,
3095 	 * we must believe that SCSI status is zero and
3096 	 * that all data transferred.
3097 	 */
3098 	XS_RESID(xs) = 0;
3099 	XS_STS(xs) = 0;
3100 	if (XS_XFRLEN(xs)) {
3101 		ISP_DMAFREE(isp, xs, fph);
3102 	}
3103 	XS_CMD_DONE(xs);
3104 	if (isp->isp_nactive)
3105 		isp->isp_nactive--;
3106 }
3107 
3108 #define	HINIB(x)			((x) >> 0x4)
3109 #define	LONIB(x)			((x)  & 0xf)
3110 #define	MAKNIB(a, b)			(((a) << 4) | (b))
3111 static u_int8_t mbpcnt[] = {
3112 	MAKNIB(1, 1),	/* 0x00: MBOX_NO_OP */
3113 	MAKNIB(5, 5),	/* 0x01: MBOX_LOAD_RAM */
3114 	MAKNIB(2, 0),	/* 0x02: MBOX_EXEC_FIRMWARE */
3115 	MAKNIB(5, 5),	/* 0x03: MBOX_DUMP_RAM */
3116 	MAKNIB(3, 3),	/* 0x04: MBOX_WRITE_RAM_WORD */
3117 	MAKNIB(2, 3),	/* 0x05: MBOX_READ_RAM_WORD */
3118 	MAKNIB(6, 6),	/* 0x06: MBOX_MAILBOX_REG_TEST */
3119 	MAKNIB(2, 3),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
3120 	MAKNIB(1, 4),	/* 0x08: MBOX_ABOUT_FIRMWARE */
3121 	MAKNIB(0, 0),	/* 0x09: */
3122 	MAKNIB(0, 0),	/* 0x0a: */
3123 	MAKNIB(0, 0),	/* 0x0b: */
3124 	MAKNIB(0, 0),	/* 0x0c: */
3125 	MAKNIB(0, 0),	/* 0x0d: */
3126 	MAKNIB(1, 2),	/* 0x0e: MBOX_CHECK_FIRMWARE */
3127 	MAKNIB(0, 0),	/* 0x0f: */
3128 	MAKNIB(5, 5),	/* 0x10: MBOX_INIT_REQ_QUEUE */
3129 	MAKNIB(6, 6),	/* 0x11: MBOX_INIT_RES_QUEUE */
3130 	MAKNIB(4, 4),	/* 0x12: MBOX_EXECUTE_IOCB */
3131 	MAKNIB(2, 2),	/* 0x13: MBOX_WAKE_UP	*/
3132 	MAKNIB(1, 6),	/* 0x14: MBOX_STOP_FIRMWARE */
3133 	MAKNIB(4, 4),	/* 0x15: MBOX_ABORT */
3134 	MAKNIB(2, 2),	/* 0x16: MBOX_ABORT_DEVICE */
3135 	MAKNIB(3, 3),	/* 0x17: MBOX_ABORT_TARGET */
3136 	MAKNIB(3, 1),	/* 0x18: MBOX_BUS_RESET */
3137 	MAKNIB(2, 3),	/* 0x19: MBOX_STOP_QUEUE */
3138 	MAKNIB(2, 3),	/* 0x1a: MBOX_START_QUEUE */
3139 	MAKNIB(2, 3),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
3140 	MAKNIB(2, 3),	/* 0x1c: MBOX_ABORT_QUEUE */
3141 	MAKNIB(2, 4),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
3142 	MAKNIB(0, 0),	/* 0x1e: */
3143 	MAKNIB(1, 3),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
3144 	MAKNIB(1, 4),	/* 0x20: MBOX_GET_INIT_SCSI_ID, MBOX_GET_LOOP_ID */
3145 	MAKNIB(1, 3),	/* 0x21: MBOX_GET_SELECT_TIMEOUT */
3146 	MAKNIB(1, 3),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
3147 	MAKNIB(1, 2),	/* 0x23: MBOX_GET_TAG_AGE_LIMIT */
3148 	MAKNIB(1, 2),	/* 0x24: MBOX_GET_CLOCK_RATE */
3149 	MAKNIB(1, 2),	/* 0x25: MBOX_GET_ACT_NEG_STATE */
3150 	MAKNIB(1, 2),	/* 0x26: MBOX_GET_ASYNC_DATA_SETUP_TIME */
3151 	MAKNIB(1, 3),	/* 0x27: MBOX_GET_PCI_PARAMS */
3152 	MAKNIB(2, 4),	/* 0x28: MBOX_GET_TARGET_PARAMS */
3153 	MAKNIB(2, 4),	/* 0x29: MBOX_GET_DEV_QUEUE_PARAMS */
3154 	MAKNIB(1, 2),	/* 0x2a: MBOX_GET_RESET_DELAY_PARAMS */
3155 	MAKNIB(0, 0),	/* 0x2b: */
3156 	MAKNIB(0, 0),	/* 0x2c: */
3157 	MAKNIB(0, 0),	/* 0x2d: */
3158 	MAKNIB(0, 0),	/* 0x2e: */
3159 	MAKNIB(0, 0),	/* 0x2f: */
3160 	MAKNIB(2, 2),	/* 0x30: MBOX_SET_INIT_SCSI_ID */
3161 	MAKNIB(2, 3),	/* 0x31: MBOX_SET_SELECT_TIMEOUT */
3162 	MAKNIB(3, 3),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
3163 	MAKNIB(2, 2),	/* 0x33: MBOX_SET_TAG_AGE_LIMIT */
3164 	MAKNIB(2, 2),	/* 0x34: MBOX_SET_CLOCK_RATE */
3165 	MAKNIB(2, 2),	/* 0x35: MBOX_SET_ACT_NEG_STATE */
3166 	MAKNIB(2, 2),	/* 0x36: MBOX_SET_ASYNC_DATA_SETUP_TIME */
3167 	MAKNIB(3, 3),	/* 0x37: MBOX_SET_PCI_CONTROL_PARAMS */
3168 	MAKNIB(4, 4),	/* 0x38: MBOX_SET_TARGET_PARAMS */
3169 	MAKNIB(4, 4),	/* 0x39: MBOX_SET_DEV_QUEUE_PARAMS */
3170 	MAKNIB(1, 2),	/* 0x3a: MBOX_SET_RESET_DELAY_PARAMS */
3171 	MAKNIB(0, 0),	/* 0x3b: */
3172 	MAKNIB(0, 0),	/* 0x3c: */
3173 	MAKNIB(0, 0),	/* 0x3d: */
3174 	MAKNIB(0, 0),	/* 0x3e: */
3175 	MAKNIB(0, 0),	/* 0x3f: */
3176 	MAKNIB(1, 2),	/* 0x40: MBOX_RETURN_BIOS_BLOCK_ADDR */
3177 	MAKNIB(6, 1),	/* 0x41: MBOX_WRITE_FOUR_RAM_WORDS */
3178 	MAKNIB(2, 3),	/* 0x42: MBOX_EXEC_BIOS_IOCB */
3179 	MAKNIB(0, 0),	/* 0x43: */
3180 	MAKNIB(0, 0),	/* 0x44: */
3181 	MAKNIB(0, 0),	/* 0x45: */
3182 	MAKNIB(0, 0),	/* 0x46: */
3183 	MAKNIB(0, 0),	/* 0x47: */
3184 	MAKNIB(0, 0),	/* 0x48: */
3185 	MAKNIB(0, 0),	/* 0x49: */
3186 	MAKNIB(2, 1),	/* 0x4a: MBOX_SET_FIRMWARE_FEATURES */
3187 	MAKNIB(1, 2),	/* 0x4b: MBOX_GET_FIRMWARE_FEATURES */
3188 	MAKNIB(0, 0),	/* 0x4c: */
3189 	MAKNIB(0, 0),	/* 0x4d: */
3190 	MAKNIB(0, 0),	/* 0x4e: */
3191 	MAKNIB(0, 0),	/* 0x4f: */
3192 	MAKNIB(0, 0),	/* 0x50: */
3193 	MAKNIB(0, 0),	/* 0x51: */
3194 	MAKNIB(0, 0),	/* 0x52: */
3195 	MAKNIB(0, 0),	/* 0x53: */
3196 	MAKNIB(8, 0),	/* 0x54: MBOX_EXEC_COMMAND_IOCB_A64 */
3197 	MAKNIB(2, 1),	/* 0x55: MBOX_ENABLE_TARGET_MODE */
3198 	MAKNIB(0, 0),	/* 0x56: */
3199 	MAKNIB(0, 0),	/* 0x57: */
3200 	MAKNIB(0, 0),	/* 0x58: */
3201 	MAKNIB(0, 0),	/* 0x59: */
3202 	MAKNIB(0, 0),	/* 0x5a: */
3203 	MAKNIB(0, 0),	/* 0x5b: */
3204 	MAKNIB(0, 0),	/* 0x5c: */
3205 	MAKNIB(0, 0),	/* 0x5d: */
3206 	MAKNIB(0, 0),	/* 0x5e: */
3207 	MAKNIB(0, 0),	/* 0x5f: */
3208 	MAKNIB(8, 6),	/* 0x60: MBOX_INIT_FIRMWARE */
3209 	MAKNIB(0, 0),	/* 0x61: */
3210 	MAKNIB(2, 1),	/* 0x62: MBOX_INIT_LIP */
3211 	MAKNIB(8, 1),	/* 0x63: MBOX_GET_FC_AL_POSITION_MAP */
3212 	MAKNIB(8, 1),	/* 0x64: MBOX_GET_PORT_DB */
3213 	MAKNIB(3, 1),	/* 0x65: MBOX_CLEAR_ACA */
3214 	MAKNIB(3, 1),	/* 0x66: MBOX_TARGET_RESET */
3215 	MAKNIB(3, 1),	/* 0x67: MBOX_CLEAR_TASK_SET */
3216 	MAKNIB(3, 1),	/* 0x68: MBOX_ABORT_TASK_SET */
3217 	MAKNIB(1, 2),	/* 0x69: MBOX_GET_FW_STATE */
3218 	MAKNIB(2, 8),	/* 0x6a: MBOX_GET_PORT_NAME */
3219 	MAKNIB(8, 1),	/* 0x6b: MBOX_GET_LINK_STATUS */
3220 	MAKNIB(4, 4),	/* 0x6c: MBOX_INIT_LIP_RESET */
3221 	MAKNIB(0, 0),	/* 0x6d: */
3222 	MAKNIB(8, 2),	/* 0x6e: MBOX_SEND_SNS */
3223 	MAKNIB(4, 3),	/* 0x6f: MBOX_FABRIC_LOGIN */
3224 	MAKNIB(2, 1),	/* 0x70: MBOX_SEND_CHANGE_REQUEST */
3225 	MAKNIB(2, 1),	/* 0x71: MBOX_FABRIC_LOGOUT */
3226 	MAKNIB(4, 1)	/* 0x72: MBOX_INIT_LIP_LOGIN */
3227 };
3228 #define	NMBCOM	(sizeof (mbpcnt) / sizeof (mbpcnt[0]))
3229 
3230 static void
3231 isp_mboxcmd(isp, mbp)
3232 	struct ispsoftc *isp;
3233 	mbreg_t *mbp;
3234 {
3235 	int outparam, inparam;
3236 	int loops, dld = 0;
3237 	u_int8_t opcode;
3238 
3239 	if (mbp->param[0] == ISP2100_SET_PCI_PARAM) {
3240 		opcode = mbp->param[0] = MBOX_SET_PCI_PARAMETERS;
3241 		inparam = 4;
3242 		outparam = 4;
3243 		goto command_known;
3244 	} else if (mbp->param[0] > NMBCOM) {
3245 		PRINTF("%s: bad command %x\n", isp->isp_name, mbp->param[0]);
3246 		return;
3247 	}
3248 
3249 	opcode = mbp->param[0];
3250 	inparam = HINIB(mbpcnt[mbp->param[0]]);
3251 	outparam =  LONIB(mbpcnt[mbp->param[0]]);
3252 
3253 	if (inparam == 0 && outparam == 0) {
3254 		PRINTF("%s: no parameters for %x\n", isp->isp_name,
3255 			mbp->param[0]);
3256 		return;
3257 	}
3258 
3259 
3260 	/*
3261 	 * Check for variants
3262 	 */
3263 #ifdef	ISP2100_SCCLUN
3264 	if (IS_FC(isp)) {
3265 		switch (mbp->param[0]) {
3266 		case MBOX_ABORT:
3267 			inparam = 7;
3268 			break;
3269 		case MBOX_ABORT_DEVICE:
3270 		case MBOX_START_QUEUE:
3271 		case MBOX_STOP_QUEUE:
3272 		case MBOX_SINGLE_STEP_QUEUE:
3273 		case MBOX_ABORT_QUEUE:
3274 		case MBOX_GET_DEV_QUEUE_STATUS:
3275 			inparam = 3;
3276 			break;
3277 		case MBOX_BUS_RESET:
3278 			inparam = 2;
3279 			break;
3280 		default:
3281 			break;
3282 		}
3283 	}
3284 #endif
3285 
3286 command_known:
3287 
3288 	/*
3289 	 * Set semaphore on mailbox registers to win any races to acquire them.
3290 	 */
3291 	ISP_WRITE(isp, BIU_SEMA, 1);
3292 
3293 	/*
3294 	 * Qlogic Errata for the ISP2100 says that there is a necessary
3295 	 * debounce between between writing the semaphore register
3296 	 * and reading a mailbox register. I believe we're okay here.
3297 	 */
3298 
3299 	/*
3300 	 * Make sure we can send some words.
3301 	 * Check to see if there's an async mbox event pending.
3302 	 */
3303 
3304 	loops = MBOX_DELAY_COUNT;
3305 	while ((ISP_READ(isp, HCCR) & HCCR_HOST_INT) != 0) {
3306 		if (ISP_READ(isp, BIU_SEMA) & 1) {
3307 			int fph;
3308 			u_int16_t mbox = ISP_READ(isp, OUTMAILBOX0);
3309 			/*
3310 			 * We have a pending MBOX async event.
3311 			 */
3312 			if (mbox & 0x8000) {
3313 				fph = isp_parse_async(isp, (int) mbox);
3314 				IDPRINTF(5, ("%s: line %d, fph %d\n",
3315 				    isp->isp_name, __LINE__, fph));
3316 				ISP_WRITE(isp, BIU_SEMA, 0);
3317 				ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3318 				if (fph < 0) {
3319 					return;
3320 				} else if (fph > 0) {
3321 					isp_fastpost_complete(isp, fph);
3322 				}
3323 				SYS_DELAY(100);
3324 				goto command_known;
3325 			}
3326 			/*
3327 			 * We have a pending MBOX completion? Might be
3328 			 * from a previous command. We can't (sometimes)
3329 			 * just clear HOST INTERRUPT, so we'll just silently
3330 			 * eat this here.
3331 			 */
3332 			if (mbox & 0x4000) {
3333 				IDPRINTF(5, ("%s: line %d, mbox 0x%x\n",
3334 				    isp->isp_name, __LINE__, mbox));
3335 				ISP_WRITE(isp, BIU_SEMA, 0);
3336 				ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3337 				SYS_DELAY(100);
3338 				goto command_known;
3339 			}
3340 		}
3341 		SYS_DELAY(100);
3342 		if (--loops < 0) {
3343 			if (dld++ > 10) {
3344 				PRINTF("%s: isp_mboxcmd could not get command "
3345 				    "started\n", isp->isp_name);
3346 				return;
3347 			}
3348 			ISP_WRITE(isp, BIU_SEMA, 0);
3349 			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3350 			goto command_known;
3351 		}
3352 	}
3353 
3354 	/*
3355 	 * Write input parameters.
3356 	 *
3357 	 * Special case some of the setups for the dual port SCSI cards.
3358 	 * XXX Eventually will be fixed by converting register write/read
3359 	 * XXX counts to bitmasks.
3360 	 */
3361 	if (IS_DUALBUS(isp)) {
3362 		switch (opcode) {
3363 		case MBOX_GET_RETRY_COUNT:
3364 		case MBOX_SET_RETRY_COUNT:
3365 			ISP_WRITE(isp, INMAILBOX7, mbp->param[7]);
3366 			mbp->param[7] = 0;
3367 			ISP_WRITE(isp, INMAILBOX6, mbp->param[6]);
3368 			mbp->param[6] = 0;
3369 			break;
3370 		case MBOX_SET_ASYNC_DATA_SETUP_TIME:
3371 		case MBOX_SET_ACT_NEG_STATE:
3372 		case MBOX_SET_TAG_AGE_LIMIT:
3373 		case MBOX_SET_SELECT_TIMEOUT:
3374 			ISP_WRITE(isp, INMAILBOX2, mbp->param[2]);
3375 			mbp->param[2] = 0;
3376 			break;
3377 		}
3378 	}
3379 
3380 	switch (inparam) {
3381 	case 8: ISP_WRITE(isp, INMAILBOX7, mbp->param[7]); mbp->param[7] = 0;
3382 	case 7: ISP_WRITE(isp, INMAILBOX6, mbp->param[6]); mbp->param[6] = 0;
3383 	case 6:
3384 		/*
3385 		 * The Qlogic 2100 cannot have registers 4 and 5 written to
3386 		 * after initialization or BAD THINGS HAPPEN (tm).
3387 		 */
3388 		if (IS_SCSI(isp) || mbp->param[0] == MBOX_INIT_FIRMWARE)
3389 			ISP_WRITE(isp, INMAILBOX5, mbp->param[5]);
3390 		mbp->param[5] = 0;
3391 	case 5:
3392 		if (IS_SCSI(isp) || mbp->param[0] == MBOX_INIT_FIRMWARE)
3393 			ISP_WRITE(isp, INMAILBOX4, mbp->param[4]);
3394 		mbp->param[4] = 0;
3395 	case 4: ISP_WRITE(isp, INMAILBOX3, mbp->param[3]); mbp->param[3] = 0;
3396 	case 3: ISP_WRITE(isp, INMAILBOX2, mbp->param[2]); mbp->param[2] = 0;
3397 	case 2: ISP_WRITE(isp, INMAILBOX1, mbp->param[1]); mbp->param[1] = 0;
3398 	case 1: ISP_WRITE(isp, INMAILBOX0, mbp->param[0]); mbp->param[0] = 0;
3399 	}
3400 
3401 	/*
3402 	 * Clear RISC int condition.
3403 	 */
3404 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3405 
3406 	/*
3407 	 * Clear semaphore on mailbox registers so that the Qlogic
3408 	 * may update outgoing registers.
3409 	 */
3410 	ISP_WRITE(isp, BIU_SEMA, 0);
3411 
3412 	/*
3413 	 * Set Host Interrupt condition so that RISC will pick up mailbox regs.
3414 	 */
3415 	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
3416 
3417 	/*
3418 	 * Wait until HOST INT has gone away (meaning that the Qlogic
3419 	 * has picked up the mailbox command. Wait a long time.
3420 	 */
3421 	loops = MBOX_DELAY_COUNT * 5;
3422 	while ((ISP_READ(isp, HCCR) & HCCR_CMD_CLEAR_RISC_INT) != 0) {
3423 		SYS_DELAY(100);
3424 		if (--loops < 0) {
3425 			PRINTF("%s: isp_mboxcmd timeout #2\n", isp->isp_name);
3426 			return;
3427 		}
3428 	}
3429 
3430 	/*
3431 	 * While the Semaphore registers isn't set, wait for the Qlogic
3432 	 * to process the mailbox command. Again- wait a long time.
3433 	 */
3434 	loops = MBOX_DELAY_COUNT * 5;
3435 	while ((ISP_READ(isp, BIU_SEMA) & 1) == 0) {
3436 		SYS_DELAY(100);
3437 		/*
3438 		 * Wierd- I've seen the case where the semaphore register
3439 		 * isn't getting set- sort of a violation of the protocol..
3440 		 */
3441 		if (ISP_READ(isp, OUTMAILBOX0) & 0x4000)
3442 			break;
3443 		if (--loops < 0) {
3444 			PRINTF("%s: isp_mboxcmd timeout #3\n", isp->isp_name);
3445 			return;
3446 		}
3447 	}
3448 
3449 	/*
3450 	 * Make sure that the MBOX_BUSY has gone away
3451 	 */
3452 	loops = MBOX_DELAY_COUNT;
3453 	for (;;) {
3454 		u_int16_t mbox = ISP_READ(isp, OUTMAILBOX0);
3455 		if (mbox == MBOX_BUSY) {
3456 			if (--loops < 0) {
3457 				PRINTF("%s: isp_mboxcmd timeout #4\n",
3458 				    isp->isp_name);
3459 				return;
3460 			}
3461 			SYS_DELAY(100);
3462 			continue;
3463 		}
3464 		/*
3465 		 * We have a pending MBOX async event.
3466 		 */
3467 		if (mbox & 0x8000) {
3468 			int fph = isp_parse_async(isp, (int) mbox);
3469 			ISP_WRITE(isp, BIU_SEMA, 0);
3470 			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3471 			if (fph < 0) {
3472 				return;
3473 			} else if (fph > 0) {
3474 				isp_fastpost_complete(isp, fph);
3475 			}
3476 			SYS_DELAY(100);
3477 			continue;
3478 		}
3479 		break;
3480 	}
3481 
3482 	/*
3483 	 * Pick up output parameters. Special case some of the readbacks
3484 	 * for the dual port SCSI cards.
3485 	 */
3486 	if (IS_DUALBUS(isp)) {
3487 		switch (opcode) {
3488 		case MBOX_GET_RETRY_COUNT:
3489 		case MBOX_SET_RETRY_COUNT:
3490 			mbp->param[7] = ISP_READ(isp, OUTMAILBOX7);
3491 			mbp->param[6] = ISP_READ(isp, OUTMAILBOX6);
3492 			break;
3493 		case MBOX_GET_TAG_AGE_LIMIT:
3494 		case MBOX_SET_TAG_AGE_LIMIT:
3495 		case MBOX_GET_ACT_NEG_STATE:
3496 		case MBOX_SET_ACT_NEG_STATE:
3497 		case MBOX_SET_ASYNC_DATA_SETUP_TIME:
3498 		case MBOX_GET_ASYNC_DATA_SETUP_TIME:
3499 		case MBOX_GET_RESET_DELAY_PARAMS:
3500 		case MBOX_SET_RESET_DELAY_PARAMS:
3501 			mbp->param[2] = ISP_READ(isp, OUTMAILBOX2);
3502 			break;
3503 		}
3504 	}
3505 
3506 	if (IS_2200(isp)) {
3507 		if (opcode == MBOX_GET_LOOP_ID) {
3508 			mbp->param[6] = ISP_READ(isp, OUTMAILBOX6);
3509 		}
3510 	}
3511 
3512 	switch (outparam) {
3513 	case 8: mbp->param[7] = ISP_READ(isp, OUTMAILBOX7);
3514 	case 7: mbp->param[6] = ISP_READ(isp, OUTMAILBOX6);
3515 	case 6: mbp->param[5] = ISP_READ(isp, OUTMAILBOX5);
3516 	case 5: mbp->param[4] = ISP_READ(isp, OUTMAILBOX4);
3517 	case 4: mbp->param[3] = ISP_READ(isp, OUTMAILBOX3);
3518 	case 3: mbp->param[2] = ISP_READ(isp, OUTMAILBOX2);
3519 	case 2: mbp->param[1] = ISP_READ(isp, OUTMAILBOX1);
3520 	case 1: mbp->param[0] = ISP_READ(isp, OUTMAILBOX0);
3521 	}
3522 
3523 	/*
3524 	 * Clear RISC int.
3525 	 */
3526 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3527 
3528 	/*
3529 	 * Release semaphore on mailbox registers
3530 	 */
3531 	ISP_WRITE(isp, BIU_SEMA, 0);
3532 
3533 	/*
3534 	 * Just to be chatty here...
3535 	 */
3536 	switch (mbp->param[0]) {
3537 	case MBOX_COMMAND_COMPLETE:
3538 		break;
3539 	case MBOX_INVALID_COMMAND:
3540 		IDPRINTF(2, ("%s: mbox cmd %x failed with INVALID_COMMAND\n",
3541 		    isp->isp_name, opcode));
3542 		break;
3543 	case MBOX_HOST_INTERFACE_ERROR:
3544 		PRINTF("%s: mbox cmd %x failed with HOST_INTERFACE_ERROR\n",
3545 		    isp->isp_name, opcode);
3546 		break;
3547 	case MBOX_TEST_FAILED:
3548 		PRINTF("%s: mbox cmd %x failed with TEST_FAILED\n",
3549 		    isp->isp_name, opcode);
3550 		break;
3551 	case MBOX_COMMAND_ERROR:
3552 		if (opcode != MBOX_ABOUT_FIRMWARE)
3553 		    PRINTF("%s: mbox cmd %x failed with COMMAND_ERROR\n",
3554 			isp->isp_name, opcode);
3555 		break;
3556 	case MBOX_COMMAND_PARAM_ERROR:
3557 		switch (opcode) {
3558 		case MBOX_GET_PORT_DB:
3559 		case MBOX_GET_PORT_NAME:
3560 		case MBOX_GET_DEV_QUEUE_PARAMS:
3561 			break;
3562 		default:
3563 			PRINTF("%s: mbox cmd %x failed with "
3564 			    "COMMAND_PARAM_ERROR\n", isp->isp_name, opcode);
3565 		}
3566 		break;
3567 
3568 	case MBOX_LOOP_ID_USED:
3569 	case MBOX_PORT_ID_USED:
3570 	case MBOX_ALL_IDS_USED:
3571 		break;
3572 
3573 
3574 	/*
3575 	 * Be silent about these...
3576 	 */
3577 	case ASYNC_PDB_CHANGED:
3578 		((fcparam *) isp->isp_param)->isp_loopstate = LOOP_PDB_RCVD;
3579 		break;
3580 
3581 	case ASYNC_LIP_OCCURRED:
3582 		((fcparam *) isp->isp_param)->isp_lipseq = mbp->param[1];
3583 		/* FALLTHROUGH */
3584 	case ASYNC_LOOP_UP:
3585 		((fcparam *) isp->isp_param)->isp_fwstate = FW_CONFIG_WAIT;
3586 		((fcparam *) isp->isp_param)->isp_loopstate = LOOP_LIP_RCVD;
3587 		break;
3588 
3589 	case ASYNC_LOOP_DOWN:
3590 	case ASYNC_LOOP_RESET:
3591 		((fcparam *) isp->isp_param)->isp_fwstate = FW_CONFIG_WAIT;
3592 		((fcparam *) isp->isp_param)->isp_loopstate = LOOP_NIL;
3593 		/* FALLTHROUGH */
3594 	case ASYNC_CHANGE_NOTIFY:
3595 		break;
3596 
3597 	default:
3598 		/*
3599 		 * The expected return of EXEC_FIRMWARE is zero.
3600 		 */
3601 		if ((opcode == MBOX_EXEC_FIRMWARE && mbp->param[0] != 0) ||
3602 		    (opcode != MBOX_EXEC_FIRMWARE)) {
3603 			PRINTF("%s: mbox cmd %x failed with error %x\n",
3604 				isp->isp_name, opcode, mbp->param[0]);
3605 		}
3606 		break;
3607 	}
3608 }
3609 
3610 void
3611 isp_lostcmd(isp, xs)
3612 	struct ispsoftc *isp;
3613 	ISP_SCSI_XFER_T *xs;
3614 {
3615 	mbreg_t mbs;
3616 
3617 	mbs.param[0] = MBOX_GET_FIRMWARE_STATUS;
3618 	isp_mboxcmd(isp, &mbs);
3619 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3620 		isp_dumpregs(isp, "couldn't GET FIRMWARE STATUS");
3621 		return;
3622 	}
3623 	if (mbs.param[1]) {
3624 		PRINTF("%s: %d commands on completion queue\n",
3625 		    isp->isp_name, mbs.param[1]);
3626 	}
3627 	if (XS_NULL(xs))
3628 		return;
3629 
3630 	mbs.param[0] = MBOX_GET_DEV_QUEUE_STATUS;
3631 	mbs.param[1] = (XS_TGT(xs) << 8) | XS_LUN(xs); /* XXX: WHICH BUS? */
3632 	isp_mboxcmd(isp, &mbs);
3633 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3634 		isp_dumpregs(isp, "couldn't GET DEVICE QUEUE STATUS");
3635 		return;
3636 	}
3637 	PRINTF("%s: lost command for target %d lun %d, %d active of %d, "
3638 		"Queue State: %x\n", isp->isp_name, XS_TGT(xs),
3639 		XS_LUN(xs), mbs.param[2], mbs.param[3], mbs.param[1]);
3640 
3641 	isp_dumpregs(isp, "lost command");
3642 	/*
3643 	 * XXX: Need to try and do something to recover.
3644 	 */
3645 }
3646 
3647 static void
3648 isp_dumpregs(isp, msg)
3649 	struct ispsoftc *isp;
3650 	const char *msg;
3651 {
3652 	PRINTF("%s: %s\n", isp->isp_name, msg);
3653 	if (IS_SCSI(isp))
3654 		PRINTF("    biu_conf1=%x", ISP_READ(isp, BIU_CONF1));
3655 	else
3656 		PRINTF("    biu_csr=%x", ISP_READ(isp, BIU2100_CSR));
3657 	PRINTF(" biu_icr=%x biu_isr=%x biu_sema=%x ", ISP_READ(isp, BIU_ICR),
3658 	    ISP_READ(isp, BIU_ISR), ISP_READ(isp, BIU_SEMA));
3659 	PRINTF("risc_hccr=%x\n", ISP_READ(isp, HCCR));
3660 
3661 
3662 	if (IS_SCSI(isp)) {
3663 		ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
3664 		PRINTF("    cdma_conf=%x cdma_sts=%x cdma_fifostat=%x\n",
3665 			ISP_READ(isp, CDMA_CONF), ISP_READ(isp, CDMA_STATUS),
3666 			ISP_READ(isp, CDMA_FIFO_STS));
3667 		PRINTF("    ddma_conf=%x ddma_sts=%x ddma_fifostat=%x\n",
3668 			ISP_READ(isp, DDMA_CONF), ISP_READ(isp, DDMA_STATUS),
3669 			ISP_READ(isp, DDMA_FIFO_STS));
3670 		PRINTF("    sxp_int=%x sxp_gross=%x sxp(scsi_ctrl)=%x\n",
3671 			ISP_READ(isp, SXP_INTERRUPT),
3672 			ISP_READ(isp, SXP_GROSS_ERR),
3673 			ISP_READ(isp, SXP_PINS_CTRL));
3674 		ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
3675 	}
3676 	PRINTF("    mbox regs: %x %x %x %x %x\n",
3677 	    ISP_READ(isp, OUTMAILBOX0), ISP_READ(isp, OUTMAILBOX1),
3678 	    ISP_READ(isp, OUTMAILBOX2), ISP_READ(isp, OUTMAILBOX3),
3679 	    ISP_READ(isp, OUTMAILBOX4));
3680 	ISP_DUMPREGS(isp);
3681 }
3682 
3683 static void
3684 isp_fw_state(isp)
3685 	struct ispsoftc *isp;
3686 {
3687 	mbreg_t mbs;
3688 	if (IS_FC(isp)) {
3689 		int once = 0;
3690 		fcparam *fcp = isp->isp_param;
3691 again:
3692 		mbs.param[0] = MBOX_GET_FW_STATE;
3693 		isp_mboxcmd(isp, &mbs);
3694 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3695 			IDPRINTF(1, ("%s: isp_fw_state 0x%x\n", isp->isp_name,
3696 			    mbs.param[0]));
3697 			switch (mbs.param[0]) {
3698 			case ASYNC_PDB_CHANGED:
3699 				if (once++ < 10) {
3700 					goto again;
3701 				}
3702 				fcp->isp_fwstate = FW_CONFIG_WAIT;
3703 				fcp->isp_loopstate = LOOP_PDB_RCVD;
3704 				goto again;
3705 			case ASYNC_LIP_OCCURRED:
3706 				fcp->isp_lipseq = mbs.param[1];
3707 				/* FALLTHROUGH */
3708 			case ASYNC_LOOP_UP:
3709 				fcp->isp_fwstate = FW_CONFIG_WAIT;
3710 				fcp->isp_loopstate = LOOP_LIP_RCVD;
3711 				if (once++ < 10) {
3712 					goto again;
3713 				}
3714 				break;
3715 			case ASYNC_LOOP_RESET:
3716 			case ASYNC_LOOP_DOWN:
3717 				fcp->isp_fwstate = FW_CONFIG_WAIT;
3718 				fcp->isp_loopstate = LOOP_NIL;
3719 				/* FALLTHROUGH */
3720 			case ASYNC_CHANGE_NOTIFY:
3721 				if (once++ < 10) {
3722 					goto again;
3723 				}
3724 				break;
3725 			}
3726 			PRINTF("%s: GET FIRMWARE STATE failed (0x%x)\n",
3727 			    isp->isp_name, mbs.param[0]);
3728 			return;
3729 		}
3730 		fcp->isp_fwstate = mbs.param[1];
3731 	}
3732 }
3733 
3734 static void
3735 isp_update(isp)
3736 	struct ispsoftc *isp;
3737 {
3738 	int bus;
3739 
3740 	for (bus = 0; isp->isp_update != 0; bus++) {
3741 		if (isp->isp_update & (1 << bus)) {
3742 			isp_update_bus(isp, bus);
3743 			isp->isp_update ^= (1 << bus);
3744 		}
3745 	}
3746 }
3747 
3748 static void
3749 isp_update_bus(isp, bus)
3750 	struct ispsoftc *isp;
3751 	int bus;
3752 {
3753 	int tgt;
3754 	mbreg_t mbs;
3755 	sdparam *sdp;
3756 
3757 	if (IS_FC(isp)) {
3758 		return;
3759 	}
3760 
3761 	sdp = isp->isp_param;
3762 	sdp += bus;
3763 
3764 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
3765 		u_int16_t flags, period, offset;
3766 		int get;
3767 
3768 		if (sdp->isp_devparam[tgt].dev_enable == 0) {
3769 			IDPRINTF(1, ("%s: skipping target %d bus %d update\n",
3770 			    isp->isp_name, tgt, bus));
3771 			continue;
3772 		}
3773 
3774 		/*
3775 		 * If the goal is to update the status of the device,
3776 		 * take what's in dev_flags and try and set the device
3777 		 * toward that. Otherwise, if we're just refreshing the
3778 		 * current device state, get the current parameters.
3779 		 */
3780 		if (sdp->isp_devparam[tgt].dev_update) {
3781 			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
3782 			mbs.param[2] = sdp->isp_devparam[tgt].dev_flags;
3783 			/*
3784 			 * Insist that PARITY must be enabled if SYNC
3785 			 * is enabled.
3786 			 */
3787 			if (mbs.param[2] & DPARM_SYNC) {
3788 				mbs.param[2] |= DPARM_PARITY;
3789 			}
3790 			mbs.param[3] =
3791 				(sdp->isp_devparam[tgt].sync_offset << 8) |
3792 				(sdp->isp_devparam[tgt].sync_period);
3793 			sdp->isp_devparam[tgt].dev_update = 0;
3794 			/*
3795 			 * A command completion later that has
3796 			 * RQSTF_NEGOTIATION set will cause
3797 			 * the dev_refresh/announce cycle.
3798 			 *
3799 			 * Note: It is really important to update our current
3800 			 * flags with at least the state of TAG capabilities-
3801 			 * otherwise we might try and send a tagged command
3802 			 * when we have it all turned off. So change it here
3803 			 * to say that current already matches goal.
3804 			 */
3805 			sdp->isp_devparam[tgt].cur_dflags &= ~DPARM_TQING;
3806 			sdp->isp_devparam[tgt].cur_dflags |=
3807 			    (sdp->isp_devparam[tgt].dev_flags & DPARM_TQING);
3808 			sdp->isp_devparam[tgt].dev_refresh = 1;
3809 			IDPRINTF(3, ("%s: bus %d set tgt %d flags 0x%x off 0x%x"
3810 			    " period 0x%x\n", isp->isp_name, bus, tgt,
3811 			    mbs.param[2], mbs.param[3] >> 8,
3812 			    mbs.param[3] & 0xff));
3813 			get = 0;
3814 		} else if (sdp->isp_devparam[tgt].dev_refresh) {
3815 			mbs.param[0] = MBOX_GET_TARGET_PARAMS;
3816 			sdp->isp_devparam[tgt].dev_refresh = 0;
3817 			get = 1;
3818 		} else {
3819 			continue;
3820 		}
3821 		mbs.param[1] = (bus << 15) | (tgt << 8) ;
3822 		isp_mboxcmd(isp, &mbs);
3823 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3824 			PRINTF("%s: failed to %cet SCSI parameters for "
3825 			    "target %d\n", isp->isp_name, (get)? 'g' : 's',
3826 			    tgt);
3827 			continue;
3828 		}
3829 		if (get == 0) {
3830 			isp->isp_sendmarker |= (1 << bus);
3831 			continue;
3832 		}
3833 		flags = mbs.param[2];
3834 		period = mbs.param[3] & 0xff;
3835 		offset = mbs.param[3] >> 8;
3836 		sdp->isp_devparam[tgt].cur_dflags = flags;
3837 		sdp->isp_devparam[tgt].cur_period = period;
3838 		sdp->isp_devparam[tgt].cur_offset = offset;
3839 		get = (bus << 16) | tgt;
3840 		(void) isp_async(isp, ISPASYNC_NEW_TGT_PARAMS, &get);
3841 	}
3842 }
3843 
3844 static void
3845 isp_setdfltparm(isp, channel)
3846 	struct ispsoftc *isp;
3847 	int channel;
3848 {
3849 	int tgt;
3850 	mbreg_t mbs;
3851 	sdparam *sdp;
3852 
3853 	if (IS_FC(isp)) {
3854 		fcparam *fcp = (fcparam *) isp->isp_param;
3855 		fcp += channel;
3856 		if (fcp->isp_gotdparms) {
3857 			return;
3858 		}
3859 		fcp->isp_gotdparms = 1;
3860 		fcp->isp_maxfrmlen = ICB_DFLT_FRMLEN;
3861 		fcp->isp_maxalloc = ICB_DFLT_ALLOC;
3862 		fcp->isp_execthrottle = ICB_DFLT_THROTTLE;
3863 		fcp->isp_retry_delay = ICB_DFLT_RDELAY;
3864 		fcp->isp_retry_count = ICB_DFLT_RCOUNT;
3865 		/* Platform specific.... */
3866 		fcp->isp_loopid = DEFAULT_LOOPID(isp);
3867 		fcp->isp_nodewwn = DEFAULT_WWN(isp);
3868 		fcp->isp_portwwn = 0;
3869 		/*
3870 		 * Now try and read NVRAM
3871 		 */
3872 		if ((isp->isp_confopts & (ISP_CFG_NONVRAM|ISP_CFG_OWNWWN)) ||
3873 		    (isp_read_nvram(isp))) {
3874 			PRINTF("%s: using Node WWN 0x%08x%08x\n",
3875 			    isp->isp_name, (u_int32_t)(fcp->isp_nodewwn >> 32),
3876 			    (u_int32_t)(fcp->isp_nodewwn & 0xffffffff));
3877 		}
3878 		return;
3879 	}
3880 
3881 	sdp = (sdparam *) isp->isp_param;
3882 	sdp += channel;
3883 
3884 	/*
3885 	 * Been there, done that, got the T-shirt...
3886 	 */
3887 	if (sdp->isp_gotdparms) {
3888 		return;
3889 	}
3890 	sdp->isp_gotdparms = 1;
3891 
3892 	/*
3893 	 * If we've not been told to avoid reading NVRAM, try and read it.
3894 	 * If we're successful reading it, we can return since NVRAM will
3895 	 * tell us the right thing to do. Otherwise, establish some reasonable
3896 	 * defaults.
3897 	 */
3898 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
3899 		if (isp_read_nvram(isp) == 0) {
3900 			return;
3901 		}
3902 	}
3903 
3904 	/*
3905 	 * Now try and see whether we have specific values for them.
3906 	 */
3907 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
3908 		mbs.param[0] = MBOX_GET_ACT_NEG_STATE;
3909 		isp_mboxcmd(isp, &mbs);
3910 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3911 			IDPRINTF(2, ("could not GET ACT NEG STATE\n"));
3912 			sdp->isp_req_ack_active_neg = 1;
3913 			sdp->isp_data_line_active_neg = 1;
3914 		} else {
3915 			sdp->isp_req_ack_active_neg =
3916 			    (mbs.param[1+channel] >> 4) & 0x1;
3917 			sdp->isp_data_line_active_neg =
3918 			    (mbs.param[1+channel] >> 5) & 0x1;
3919 		}
3920 	} else {
3921 		sdp->isp_req_ack_active_neg = 1;
3922 		sdp->isp_data_line_active_neg = 1;
3923 	}
3924 
3925 	IDPRINTF(3, ("%s: defaulting bus %d REQ/ACK Active Negation is %d\n",
3926 	    isp->isp_name, channel, sdp->isp_req_ack_active_neg));
3927 	IDPRINTF(3, ("%s: defaulting bus %d DATA Active Negation is %d\n",
3928 	    isp->isp_name, channel, sdp->isp_data_line_active_neg));
3929 
3930 	/*
3931 	 * The trick here is to establish a default for the default (honk!)
3932 	 * state (dev_flags). Then try and get the current status from
3933 	 * the card to fill in the current state. We don't, in fact, set
3934 	 * the default to the SAFE default state- that's not the goal state.
3935 	 */
3936 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
3937 		sdp->isp_devparam[tgt].cur_offset = 0;
3938 		sdp->isp_devparam[tgt].cur_period = 0;
3939 		sdp->isp_devparam[tgt].dev_flags = DPARM_DEFAULT;
3940 		sdp->isp_devparam[tgt].cur_dflags = 0;
3941 		/*
3942 		 * We default to Wide/Fast for versions less than a 1040
3943 		 * (unless it's SBus).
3944 		 */
3945 		if ((isp->isp_bustype == ISP_BT_SBUS &&
3946 		    isp->isp_type < ISP_HA_SCSI_1020A) ||
3947 		    (isp->isp_bustype == ISP_BT_PCI &&
3948 		    isp->isp_type < ISP_HA_SCSI_1040) ||
3949 		    (isp->isp_clock && isp->isp_clock < 60)) {
3950 			sdp->isp_devparam[tgt].sync_offset =
3951 			    ISP_10M_SYNCPARMS >> 8;
3952 			sdp->isp_devparam[tgt].sync_period =
3953 			    ISP_10M_SYNCPARMS & 0xff;
3954 		} else if (IS_ULTRA2(isp)) {
3955 			sdp->isp_devparam[tgt].sync_offset =
3956 			    ISP_40M_SYNCPARMS >> 8;
3957 			sdp->isp_devparam[tgt].sync_period =
3958 			    ISP_40M_SYNCPARMS & 0xff;
3959 		} else {
3960 			sdp->isp_devparam[tgt].sync_offset =
3961 			    ISP_20M_SYNCPARMS >> 8;
3962 			sdp->isp_devparam[tgt].sync_period =
3963 			    ISP_20M_SYNCPARMS & 0xff;
3964 		}
3965 
3966 		/*
3967 		 * Don't get current target parameters if we've been
3968 		 * told not to use NVRAM- it's really the same thing.
3969 		 */
3970 		if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
3971 
3972 			mbs.param[0] = MBOX_GET_TARGET_PARAMS;
3973 			mbs.param[1] = tgt << 8;
3974 			isp_mboxcmd(isp, &mbs);
3975 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3976 				continue;
3977 			}
3978 			sdp->isp_devparam[tgt].cur_dflags = mbs.param[2];
3979 			sdp->isp_devparam[tgt].dev_flags = mbs.param[2];
3980 			sdp->isp_devparam[tgt].cur_period = mbs.param[3] & 0xff;
3981 			sdp->isp_devparam[tgt].cur_offset = mbs.param[3] >> 8;
3982 
3983 			/*
3984 			 * The maximum period we can really see
3985 			 * here is 100 (decimal), or 400 ns.
3986 			 * For some unknown reason we sometimes
3987 			 * get back wildass numbers from the
3988 			 * boot device's parameters (alpha only).
3989 			 */
3990 			if ((mbs.param[3] & 0xff) <= 0x64) {
3991 				sdp->isp_devparam[tgt].sync_period =
3992 				    mbs.param[3] & 0xff;
3993 				sdp->isp_devparam[tgt].sync_offset =
3994 				    mbs.param[3] >> 8;
3995 			}
3996 
3997 			/*
3998 			 * It is not safe to run Ultra Mode with a clock < 60.
3999 			 */
4000 			if (((isp->isp_clock && isp->isp_clock < 60) ||
4001 			    (isp->isp_type < ISP_HA_SCSI_1020A)) &&
4002 			    (sdp->isp_devparam[tgt].sync_period <=
4003 			    (ISP_20M_SYNCPARMS & 0xff))) {
4004 				sdp->isp_devparam[tgt].sync_offset =
4005 				    ISP_10M_SYNCPARMS >> 8;
4006 				sdp->isp_devparam[tgt].sync_period =
4007 				    ISP_10M_SYNCPARMS & 0xff;
4008 			}
4009 		}
4010 		IDPRINTF(3, ("%s: bus %d tgt %d flags %x offset %x period %x\n",
4011 		    isp->isp_name, channel, tgt,
4012 		    sdp->isp_devparam[tgt].dev_flags,
4013 		    sdp->isp_devparam[tgt].sync_offset,
4014 		    sdp->isp_devparam[tgt].sync_period));
4015 	}
4016 
4017 	/*
4018 	 * Establish default some more default parameters.
4019 	 */
4020 	sdp->isp_cmd_dma_burst_enable = 1;
4021 	sdp->isp_data_dma_burst_enabl = 1;
4022 	sdp->isp_fifo_threshold = 0;
4023 	sdp->isp_initiator_id = 7;
4024 	/* XXXX This is probably based upon clock XXXX */
4025 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
4026 		sdp->isp_async_data_setup = 9;
4027 	} else {
4028 		sdp->isp_async_data_setup = 6;
4029 	}
4030 	sdp->isp_selection_timeout = 250;
4031 	sdp->isp_max_queue_depth = MAXISPREQUEST;
4032 	sdp->isp_tag_aging = 8;
4033 	sdp->isp_bus_reset_delay = 3;
4034 	sdp->isp_retry_count = 2;
4035 	sdp->isp_retry_delay = 2;
4036 
4037 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
4038 		sdp->isp_devparam[tgt].exc_throttle = 16;
4039 		sdp->isp_devparam[tgt].dev_enable = 1;
4040 	}
4041 }
4042 
4043 /*
4044  * Re-initialize the ISP and complete all orphaned commands
4045  * with a 'botched' notice. The reset/init routines should
4046  * not disturb an already active list of commands.
4047  *
4048  * Locks held prior to coming here.
4049  */
4050 
4051 void
4052 isp_restart(isp)
4053 	struct ispsoftc *isp;
4054 {
4055 	ISP_SCSI_XFER_T *xs;
4056 	u_int32_t handle;
4057 
4058 #if	0
4059 	isp->isp_gotdparms = 0;
4060 #endif
4061 	isp_reset(isp);
4062 	if (isp->isp_state == ISP_RESETSTATE) {
4063 		isp_init(isp);
4064 		if (isp->isp_state == ISP_INITSTATE) {
4065 			isp->isp_state = ISP_RUNSTATE;
4066 		}
4067 	}
4068 	if (isp->isp_state != ISP_RUNSTATE) {
4069 		PRINTF("%s: isp_restart cannot restart ISP\n", isp->isp_name);
4070 	}
4071 	isp->isp_nactive = 0;
4072 
4073 	for (handle = 1; handle <= (int) isp->isp_maxcmds; handle++) {
4074 		xs = isp_find_xs(isp, handle);
4075 		if (xs == NULL) {
4076 			continue;
4077 		}
4078 		isp_destroy_handle(isp, handle);
4079 		if (XS_XFRLEN(xs)) {
4080 			ISP_DMAFREE(isp, xs, handle);
4081 			XS_RESID(xs) = XS_XFRLEN(xs);
4082 		} else {
4083 			XS_RESID(xs) = 0;
4084 		}
4085 		XS_SETERR(xs, HBA_BUSRESET);
4086 		XS_CMD_DONE(xs);
4087 	}
4088 }
4089 
4090 /*
4091  * NVRAM Routines
4092  */
4093 static int
4094 isp_read_nvram(isp)
4095 	struct ispsoftc *isp;
4096 {
4097 	int i, amt;
4098 	u_int8_t csum, minversion;
4099 	union {
4100 		u_int8_t _x[ISP2100_NVRAM_SIZE];
4101 		u_int16_t _s[ISP2100_NVRAM_SIZE>>1];
4102 	} _n;
4103 #define	nvram_data	_n._x
4104 #define	nvram_words	_n._s
4105 
4106 	if (IS_FC(isp)) {
4107 		amt = ISP2100_NVRAM_SIZE;
4108 		minversion = 1;
4109 	} else if (IS_ULTRA2(isp)) {
4110 		amt = ISP1080_NVRAM_SIZE;
4111 		minversion = 0;
4112 	} else {
4113 		amt = ISP_NVRAM_SIZE;
4114 		minversion = 2;
4115 	}
4116 
4117 	/*
4118 	 * Just read the first two words first to see if we have a valid
4119 	 * NVRAM to continue reading the rest with.
4120 	 */
4121 	for (i = 0; i < 2; i++) {
4122 		isp_rdnvram_word(isp, i, &nvram_words[i]);
4123 	}
4124 	if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
4125 	    nvram_data[2] != 'P') {
4126 		if (isp->isp_bustype != ISP_BT_SBUS) {
4127 			PRINTF("%s: invalid NVRAM header (%x,%x,%x,%x)\n",
4128 			    isp->isp_name, nvram_data[0], nvram_data[1],
4129 			    nvram_data[2], nvram_data[3]);
4130 		}
4131 		return (-1);
4132 	}
4133 	for (i = 2; i < amt>>1; i++) {
4134 		isp_rdnvram_word(isp, i, &nvram_words[i]);
4135 	}
4136 	for (csum = 0, i = 0; i < amt; i++) {
4137 		csum += nvram_data[i];
4138 	}
4139 	if (csum != 0) {
4140 		PRINTF("%s: invalid NVRAM checksum\n", isp->isp_name);
4141 		return (-1);
4142 	}
4143 	if (ISP_NVRAM_VERSION(nvram_data) < minversion) {
4144 		PRINTF("%s: version %d NVRAM not understood\n", isp->isp_name,
4145 		    ISP_NVRAM_VERSION(nvram_data));
4146 		return (-1);
4147 	}
4148 
4149 	if (IS_ULTRA3(isp)) {
4150 		isp_parse_nvram_12160(isp, 0, nvram_data);
4151 		isp_parse_nvram_12160(isp, 1, nvram_data);
4152 	} else if (IS_1080(isp)) {
4153 		isp_parse_nvram_1080(isp, 0, nvram_data);
4154 	} else if (IS_1280(isp) || IS_1240(isp)) {
4155 		isp_parse_nvram_1080(isp, 0, nvram_data);
4156 		isp_parse_nvram_1080(isp, 1, nvram_data);
4157 	} else if (IS_SCSI(isp)) {
4158 		isp_parse_nvram_1020(isp, nvram_data);
4159 	} else {
4160 		isp_parse_nvram_2100(isp, nvram_data);
4161 	}
4162 	IDPRINTF(3, ("%s: NVRAM is valid\n", isp->isp_name));
4163 	return (0);
4164 #undef	nvram_data
4165 #undef	nvram_words
4166 }
4167 
4168 static void
4169 isp_rdnvram_word(isp, wo, rp)
4170 	struct ispsoftc *isp;
4171 	int wo;
4172 	u_int16_t *rp;
4173 {
4174 	int i, cbits;
4175 	u_int16_t bit, rqst;
4176 
4177 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
4178 	SYS_DELAY(2);
4179 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
4180 	SYS_DELAY(2);
4181 
4182 	if (IS_FC(isp)) {
4183 		wo &= ((ISP2100_NVRAM_SIZE >> 1) - 1);
4184 		rqst = (ISP_NVRAM_READ << 8) | wo;
4185 		cbits = 10;
4186 	} else if (IS_ULTRA2(isp)) {
4187 		wo &= ((ISP1080_NVRAM_SIZE >> 1) - 1);
4188 		rqst = (ISP_NVRAM_READ << 8) | wo;
4189 		cbits = 10;
4190 	} else {
4191 		wo &= ((ISP_NVRAM_SIZE >> 1) - 1);
4192 		rqst = (ISP_NVRAM_READ << 6) | wo;
4193 		cbits = 8;
4194 	}
4195 
4196 	/*
4197 	 * Clock the word select request out...
4198 	 */
4199 	for (i = cbits; i >= 0; i--) {
4200 		if ((rqst >> i) & 1) {
4201 			bit = BIU_NVRAM_SELECT | BIU_NVRAM_DATAOUT;
4202 		} else {
4203 			bit = BIU_NVRAM_SELECT;
4204 		}
4205 		ISP_WRITE(isp, BIU_NVRAM, bit);
4206 		SYS_DELAY(2);
4207 		ISP_WRITE(isp, BIU_NVRAM, bit | BIU_NVRAM_CLOCK);
4208 		SYS_DELAY(2);
4209 		ISP_WRITE(isp, BIU_NVRAM, bit);
4210 		SYS_DELAY(2);
4211 	}
4212 	/*
4213 	 * Now read the result back in (bits come back in MSB format).
4214 	 */
4215 	*rp = 0;
4216 	for (i = 0; i < 16; i++) {
4217 		u_int16_t rv;
4218 		*rp <<= 1;
4219 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
4220 		SYS_DELAY(2);
4221 		rv = ISP_READ(isp, BIU_NVRAM);
4222 		if (rv & BIU_NVRAM_DATAIN) {
4223 			*rp |= 1;
4224 		}
4225 		SYS_DELAY(2);
4226 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
4227 		SYS_DELAY(2);
4228 	}
4229 	ISP_WRITE(isp, BIU_NVRAM, 0);
4230 	SYS_DELAY(2);
4231 #if	BYTE_ORDER == BIG_ENDIAN
4232 	*rp = ((*rp >> 8) | ((*rp & 0xff) << 8));
4233 #endif
4234 }
4235 
4236 static void
4237 isp_parse_nvram_1020(isp, nvram_data)
4238 	struct ispsoftc *isp;
4239 	u_int8_t *nvram_data;
4240 {
4241 	int i;
4242 	static char *tru = "true";
4243 	static char *not = "false";
4244 	sdparam *sdp = (sdparam *) isp->isp_param;
4245 
4246 	sdp->isp_fifo_threshold =
4247 		ISP_NVRAM_FIFO_THRESHOLD(nvram_data) |
4248 		(ISP_NVRAM_FIFO_THRESHOLD_128(nvram_data) << 2);
4249 
4250 	sdp->isp_initiator_id =
4251 		ISP_NVRAM_INITIATOR_ID(nvram_data);
4252 
4253 	sdp->isp_bus_reset_delay =
4254 		ISP_NVRAM_BUS_RESET_DELAY(nvram_data);
4255 
4256 	sdp->isp_retry_count =
4257 		ISP_NVRAM_BUS_RETRY_COUNT(nvram_data);
4258 
4259 	sdp->isp_retry_delay =
4260 		ISP_NVRAM_BUS_RETRY_DELAY(nvram_data);
4261 
4262 	sdp->isp_async_data_setup =
4263 		ISP_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data);
4264 
4265 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
4266 		if (sdp->isp_async_data_setup < 9) {
4267 			sdp->isp_async_data_setup = 9;
4268 		}
4269 	} else {
4270 		if (sdp->isp_async_data_setup != 6) {
4271 			sdp->isp_async_data_setup = 6;
4272 		}
4273 	}
4274 
4275 	sdp->isp_req_ack_active_neg =
4276 		ISP_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data);
4277 
4278 	sdp->isp_data_line_active_neg =
4279 		ISP_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data);
4280 
4281 	sdp->isp_data_dma_burst_enabl =
4282 		ISP_NVRAM_DATA_DMA_BURST_ENABLE(nvram_data);
4283 
4284 	sdp->isp_cmd_dma_burst_enable =
4285 		ISP_NVRAM_CMD_DMA_BURST_ENABLE(nvram_data);
4286 
4287 	sdp->isp_tag_aging =
4288 		ISP_NVRAM_TAG_AGE_LIMIT(nvram_data);
4289 
4290 	sdp->isp_selection_timeout =
4291 		ISP_NVRAM_SELECTION_TIMEOUT(nvram_data);
4292 
4293 	sdp->isp_max_queue_depth =
4294 		ISP_NVRAM_MAX_QUEUE_DEPTH(nvram_data);
4295 
4296 	isp->isp_fast_mttr = ISP_NVRAM_FAST_MTTR_ENABLE(nvram_data);
4297 	if (isp->isp_dblev > 2) {
4298 		PRINTF("%s: NVRAM values:\n", isp->isp_name);
4299 		PRINTF("             Fifo Threshold = 0x%x\n",
4300 		    sdp->isp_fifo_threshold);
4301 		PRINTF("            Bus Reset Delay = %d\n",
4302 		    sdp->isp_bus_reset_delay);
4303 		PRINTF("                Retry Count = %d\n",
4304 		    sdp->isp_retry_count);
4305 		PRINTF("                Retry Delay = %d\n",
4306 		    sdp->isp_retry_delay);
4307 		PRINTF("              Tag Age Limit = %d\n",
4308 		    sdp->isp_tag_aging);
4309 		PRINTF("          Selection Timeout = %d\n",
4310 		    sdp->isp_selection_timeout);
4311 		PRINTF("            Max Queue Depth = %d\n",
4312 		    sdp->isp_max_queue_depth);
4313 		PRINTF("           Async Data Setup = 0x%x\n",
4314 		    sdp->isp_async_data_setup);
4315 		PRINTF("    REQ/ACK Active Negation = %s\n",
4316 		    sdp->isp_req_ack_active_neg? tru : not);
4317 		PRINTF("  Data Line Active Negation = %s\n",
4318 		    sdp->isp_data_line_active_neg? tru : not);
4319 		PRINTF("      Data DMA Burst Enable = %s\n",
4320 		    sdp->isp_data_dma_burst_enabl? tru : not);
4321 		PRINTF("       Cmd DMA Burst Enable = %s\n",
4322 		    sdp->isp_cmd_dma_burst_enable? tru : not);
4323 		PRINTF("                  Fast MTTR = %s\n",
4324 		    isp->isp_fast_mttr? tru : not);
4325 	}
4326 	for (i = 0; i < MAX_TARGETS; i++) {
4327 		sdp->isp_devparam[i].dev_enable =
4328 			ISP_NVRAM_TGT_DEVICE_ENABLE(nvram_data, i);
4329 		sdp->isp_devparam[i].exc_throttle =
4330 			ISP_NVRAM_TGT_EXEC_THROTTLE(nvram_data, i);
4331 		sdp->isp_devparam[i].sync_offset =
4332 			ISP_NVRAM_TGT_SYNC_OFFSET(nvram_data, i);
4333 		sdp->isp_devparam[i].sync_period =
4334 			ISP_NVRAM_TGT_SYNC_PERIOD(nvram_data, i);
4335 
4336 		if (isp->isp_type < ISP_HA_SCSI_1040) {
4337 			/*
4338 			 * If we're not ultra, we can't possibly
4339 			 * be a shorter period than this.
4340 			 */
4341 			if (sdp->isp_devparam[i].sync_period < 0x19) {
4342 				sdp->isp_devparam[i].sync_period =
4343 				    0x19;
4344 			}
4345 			if (sdp->isp_devparam[i].sync_offset > 0xc) {
4346 				sdp->isp_devparam[i].sync_offset =
4347 				    0x0c;
4348 			}
4349 		} else {
4350 			if (sdp->isp_devparam[i].sync_offset > 0x8) {
4351 				sdp->isp_devparam[i].sync_offset = 0x8;
4352 			}
4353 		}
4354 		sdp->isp_devparam[i].dev_flags = 0;
4355 		if (ISP_NVRAM_TGT_RENEG(nvram_data, i))
4356 			sdp->isp_devparam[i].dev_flags |= DPARM_RENEG;
4357 		if (ISP_NVRAM_TGT_QFRZ(nvram_data, i)) {
4358 			PRINTF("%s: not supporting QFRZ option for "
4359 			    "target %d\n", isp->isp_name, i);
4360 		}
4361 		sdp->isp_devparam[i].dev_flags |= DPARM_ARQ;
4362 		if (ISP_NVRAM_TGT_ARQ(nvram_data, i) == 0) {
4363 			PRINTF("%s: not disabling ARQ option for "
4364 			    "target %d\n", isp->isp_name, i);
4365 		}
4366 		if (ISP_NVRAM_TGT_TQING(nvram_data, i))
4367 			sdp->isp_devparam[i].dev_flags |= DPARM_TQING;
4368 		if (ISP_NVRAM_TGT_SYNC(nvram_data, i))
4369 			sdp->isp_devparam[i].dev_flags |= DPARM_SYNC;
4370 		if (ISP_NVRAM_TGT_WIDE(nvram_data, i))
4371 			sdp->isp_devparam[i].dev_flags |= DPARM_WIDE;
4372 		if (ISP_NVRAM_TGT_PARITY(nvram_data, i))
4373 			sdp->isp_devparam[i].dev_flags |= DPARM_PARITY;
4374 		if (ISP_NVRAM_TGT_DISC(nvram_data, i))
4375 			sdp->isp_devparam[i].dev_flags |= DPARM_DISC;
4376 		sdp->isp_devparam[i].cur_dflags = 0; /* we don't know */
4377 		if (isp->isp_dblev > 2) {
4378 			PRINTF("   Target %d: Enabled %d Throttle %d "
4379 			    "Offset %d Period %d Flags 0x%x\n", i,
4380 			    sdp->isp_devparam[i].dev_enable,
4381 			    sdp->isp_devparam[i].exc_throttle,
4382 			    sdp->isp_devparam[i].sync_offset,
4383 			    sdp->isp_devparam[i].sync_period,
4384 			    sdp->isp_devparam[i].dev_flags);
4385 		}
4386 	}
4387 }
4388 
4389 static void
4390 isp_parse_nvram_1080(isp, bus, nvram_data)
4391 	struct ispsoftc *isp;
4392 	int bus;
4393 	u_int8_t *nvram_data;
4394 {
4395 	static char *tru = "true";
4396 	static char *not = "false";
4397 	int i;
4398 	sdparam *sdp = (sdparam *) isp->isp_param;
4399 	sdp += bus;
4400 
4401 	sdp->isp_fifo_threshold =
4402 	    ISP1080_NVRAM_FIFO_THRESHOLD(nvram_data);
4403 
4404 	sdp->isp_initiator_id =
4405 	    ISP1080_NVRAM_INITIATOR_ID(nvram_data, bus);
4406 
4407 	sdp->isp_bus_reset_delay =
4408 	    ISP1080_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
4409 
4410 	sdp->isp_retry_count =
4411 	    ISP1080_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
4412 
4413 	sdp->isp_retry_delay =
4414 	    ISP1080_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
4415 
4416 	sdp->isp_async_data_setup =
4417 	    ISP1080_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data,
4418 	    bus);
4419 
4420 	sdp->isp_req_ack_active_neg =
4421 	    ISP1080_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data,
4422 	    bus);
4423 
4424 	sdp->isp_data_line_active_neg =
4425 	    ISP1080_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data,
4426 	    bus);
4427 
4428 	sdp->isp_data_dma_burst_enabl =
4429 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
4430 
4431 	sdp->isp_cmd_dma_burst_enable =
4432 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
4433 
4434 	sdp->isp_selection_timeout =
4435 	    ISP1080_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
4436 
4437 	sdp->isp_max_queue_depth =
4438 	     ISP1080_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
4439 
4440 	if (isp->isp_dblev >= 3) {
4441 		PRINTF("%s: ISP1080 bus %d NVRAM values:\n",
4442 		    isp->isp_name, bus);
4443 		PRINTF("               Initiator ID = %d\n",
4444 		    sdp->isp_initiator_id);
4445 		PRINTF("             Fifo Threshold = 0x%x\n",
4446 		    sdp->isp_fifo_threshold);
4447 		PRINTF("            Bus Reset Delay = %d\n",
4448 		    sdp->isp_bus_reset_delay);
4449 		PRINTF("                Retry Count = %d\n",
4450 		    sdp->isp_retry_count);
4451 		PRINTF("                Retry Delay = %d\n",
4452 		    sdp->isp_retry_delay);
4453 		PRINTF("              Tag Age Limit = %d\n",
4454 		    sdp->isp_tag_aging);
4455 		PRINTF("          Selection Timeout = %d\n",
4456 		    sdp->isp_selection_timeout);
4457 		PRINTF("            Max Queue Depth = %d\n",
4458 		    sdp->isp_max_queue_depth);
4459 		PRINTF("           Async Data Setup = 0x%x\n",
4460 		    sdp->isp_async_data_setup);
4461 		PRINTF("    REQ/ACK Active Negation = %s\n",
4462 		    sdp->isp_req_ack_active_neg? tru : not);
4463 		PRINTF("  Data Line Active Negation = %s\n",
4464 		    sdp->isp_data_line_active_neg? tru : not);
4465 		PRINTF("       Cmd DMA Burst Enable = %s\n",
4466 		    sdp->isp_cmd_dma_burst_enable? tru : not);
4467 	}
4468 	for (i = 0; i < MAX_TARGETS; i++) {
4469 		sdp->isp_devparam[i].dev_enable =
4470 		    ISP1080_NVRAM_TGT_DEVICE_ENABLE(nvram_data, i, bus);
4471 		sdp->isp_devparam[i].exc_throttle =
4472 			ISP1080_NVRAM_TGT_EXEC_THROTTLE(nvram_data, i, bus);
4473 		sdp->isp_devparam[i].sync_offset =
4474 			ISP1080_NVRAM_TGT_SYNC_OFFSET(nvram_data, i, bus);
4475 		sdp->isp_devparam[i].sync_period =
4476 			ISP1080_NVRAM_TGT_SYNC_PERIOD(nvram_data, i, bus);
4477 		sdp->isp_devparam[i].dev_flags = 0;
4478 		if (ISP1080_NVRAM_TGT_RENEG(nvram_data, i, bus))
4479 			sdp->isp_devparam[i].dev_flags |= DPARM_RENEG;
4480 		if (ISP1080_NVRAM_TGT_QFRZ(nvram_data, i, bus)) {
4481 			PRINTF("%s: not supporting QFRZ option "
4482 			    "for target %d bus %d\n",
4483 			    isp->isp_name, i, bus);
4484 		}
4485 		sdp->isp_devparam[i].dev_flags |= DPARM_ARQ;
4486 		if (ISP1080_NVRAM_TGT_ARQ(nvram_data, i, bus) == 0) {
4487 			PRINTF("%s: not disabling ARQ option "
4488 			    "for target %d bus %d\n",
4489 			    isp->isp_name, i, bus);
4490 		}
4491 		if (ISP1080_NVRAM_TGT_TQING(nvram_data, i, bus))
4492 			sdp->isp_devparam[i].dev_flags |= DPARM_TQING;
4493 		if (ISP1080_NVRAM_TGT_SYNC(nvram_data, i, bus))
4494 			sdp->isp_devparam[i].dev_flags |= DPARM_SYNC;
4495 		if (ISP1080_NVRAM_TGT_WIDE(nvram_data, i, bus))
4496 			sdp->isp_devparam[i].dev_flags |= DPARM_WIDE;
4497 		if (ISP1080_NVRAM_TGT_PARITY(nvram_data, i, bus))
4498 			sdp->isp_devparam[i].dev_flags |= DPARM_PARITY;
4499 		if (ISP1080_NVRAM_TGT_DISC(nvram_data, i, bus))
4500 			sdp->isp_devparam[i].dev_flags |= DPARM_DISC;
4501 		sdp->isp_devparam[i].cur_dflags = 0;
4502 		if (isp->isp_dblev >= 3) {
4503 			PRINTF("   Target %d: Ena %d Throttle "
4504 			    "%d Offset %d Period %d Flags "
4505 			    "0x%x\n", i,
4506 			    sdp->isp_devparam[i].dev_enable,
4507 			    sdp->isp_devparam[i].exc_throttle,
4508 			    sdp->isp_devparam[i].sync_offset,
4509 			    sdp->isp_devparam[i].sync_period,
4510 			    sdp->isp_devparam[i].dev_flags);
4511 		}
4512 	}
4513 }
4514 
4515 static void
4516 isp_parse_nvram_12160(isp, bus, nvram_data)
4517 	struct ispsoftc *isp;
4518 	int bus;
4519 	u_int8_t *nvram_data;
4520 {
4521 	static char *tru = "true";
4522 	static char *not = "false";
4523 	sdparam *sdp = (sdparam *) isp->isp_param;
4524 	int i;
4525 
4526 	sdp += bus;
4527 
4528 	sdp->isp_fifo_threshold =
4529 	    ISP12160_NVRAM_FIFO_THRESHOLD(nvram_data);
4530 
4531 	sdp->isp_initiator_id =
4532 	    ISP12160_NVRAM_INITIATOR_ID(nvram_data, bus);
4533 
4534 	sdp->isp_bus_reset_delay =
4535 	    ISP12160_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
4536 
4537 	sdp->isp_retry_count =
4538 	    ISP12160_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
4539 
4540 	sdp->isp_retry_delay =
4541 	    ISP12160_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
4542 
4543 	sdp->isp_async_data_setup =
4544 	    ISP12160_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data,
4545 	    bus);
4546 
4547 	sdp->isp_req_ack_active_neg =
4548 	    ISP12160_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data,
4549 	    bus);
4550 
4551 	sdp->isp_data_line_active_neg =
4552 	    ISP12160_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data,
4553 	    bus);
4554 
4555 	sdp->isp_data_dma_burst_enabl =
4556 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
4557 
4558 	sdp->isp_cmd_dma_burst_enable =
4559 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
4560 
4561 	sdp->isp_selection_timeout =
4562 	    ISP12160_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
4563 
4564 	sdp->isp_max_queue_depth =
4565 	     ISP12160_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
4566 
4567 	if (isp->isp_dblev >= 3) {
4568 		PRINTF("%s: ISP12160 bus %d NVRAM values:\n",
4569 		    isp->isp_name, bus);
4570 		PRINTF("               Initiator ID = %d\n",
4571 		    sdp->isp_initiator_id);
4572 		PRINTF("             Fifo Threshold = 0x%x\n",
4573 		    sdp->isp_fifo_threshold);
4574 		PRINTF("            Bus Reset Delay = %d\n",
4575 		    sdp->isp_bus_reset_delay);
4576 		PRINTF("                Retry Count = %d\n",
4577 		    sdp->isp_retry_count);
4578 		PRINTF("                Retry Delay = %d\n",
4579 		    sdp->isp_retry_delay);
4580 		PRINTF("              Tag Age Limit = %d\n",
4581 		    sdp->isp_tag_aging);
4582 		PRINTF("          Selection Timeout = %d\n",
4583 		    sdp->isp_selection_timeout);
4584 		PRINTF("            Max Queue Depth = %d\n",
4585 		    sdp->isp_max_queue_depth);
4586 		PRINTF("           Async Data Setup = 0x%x\n",
4587 		    sdp->isp_async_data_setup);
4588 		PRINTF("    REQ/ACK Active Negation = %s\n",
4589 		    sdp->isp_req_ack_active_neg? tru : not);
4590 		PRINTF("  Data Line Active Negation = %s\n",
4591 		    sdp->isp_data_line_active_neg? tru : not);
4592 		PRINTF("       Cmd DMA Burst Enable = %s\n",
4593 		    sdp->isp_cmd_dma_burst_enable? tru : not);
4594 	}
4595 
4596 	for (i = 0; i < MAX_TARGETS; i++) {
4597 		sdp->isp_devparam[i].dev_enable =
4598 		    ISP12160_NVRAM_TGT_DEVICE_ENABLE(nvram_data, i, bus);
4599 		sdp->isp_devparam[i].exc_throttle =
4600 			ISP12160_NVRAM_TGT_EXEC_THROTTLE(nvram_data, i, bus);
4601 		sdp->isp_devparam[i].sync_offset =
4602 			ISP12160_NVRAM_TGT_SYNC_OFFSET(nvram_data, i, bus);
4603 		sdp->isp_devparam[i].sync_period =
4604 			ISP12160_NVRAM_TGT_SYNC_PERIOD(nvram_data, i, bus);
4605 		sdp->isp_devparam[i].dev_flags = 0;
4606 		if (ISP12160_NVRAM_TGT_RENEG(nvram_data, i, bus))
4607 			sdp->isp_devparam[i].dev_flags |= DPARM_RENEG;
4608 		if (ISP12160_NVRAM_TGT_QFRZ(nvram_data, i, bus)) {
4609 			PRINTF("%s: not supporting QFRZ option "
4610 			    "for target %d bus %d\n", isp->isp_name, i, bus);
4611 		}
4612 		sdp->isp_devparam[i].dev_flags |= DPARM_ARQ;
4613 		if (ISP12160_NVRAM_TGT_ARQ(nvram_data, i, bus) == 0) {
4614 			PRINTF("%s: not disabling ARQ option "
4615 			    "for target %d bus %d\n", isp->isp_name, i, bus);
4616 		}
4617 		if (ISP12160_NVRAM_TGT_TQING(nvram_data, i, bus))
4618 			sdp->isp_devparam[i].dev_flags |= DPARM_TQING;
4619 		if (ISP12160_NVRAM_TGT_SYNC(nvram_data, i, bus))
4620 			sdp->isp_devparam[i].dev_flags |= DPARM_SYNC;
4621 		if (ISP12160_NVRAM_TGT_WIDE(nvram_data, i, bus))
4622 			sdp->isp_devparam[i].dev_flags |= DPARM_WIDE;
4623 		if (ISP12160_NVRAM_TGT_PARITY(nvram_data, i, bus))
4624 			sdp->isp_devparam[i].dev_flags |= DPARM_PARITY;
4625 		if (ISP12160_NVRAM_TGT_DISC(nvram_data, i, bus))
4626 			sdp->isp_devparam[i].dev_flags |= DPARM_DISC;
4627 		sdp->isp_devparam[i].cur_dflags = 0;
4628 		if (isp->isp_dblev >= 3) {
4629 			PRINTF("   Target %d: Ena %d Throttle %d Offset %d "
4630 			    "Period %d Flags 0x%x\n", i,
4631 			    sdp->isp_devparam[i].dev_enable,
4632 			    sdp->isp_devparam[i].exc_throttle,
4633 			    sdp->isp_devparam[i].sync_offset,
4634 			    sdp->isp_devparam[i].sync_period,
4635 			    sdp->isp_devparam[i].dev_flags);
4636 		}
4637 	}
4638 }
4639 
4640 static void
4641 isp_parse_nvram_2100(isp, nvram_data)
4642 	struct ispsoftc *isp;
4643 	u_int8_t *nvram_data;
4644 {
4645 	fcparam *fcp = (fcparam *) isp->isp_param;
4646 	union {
4647 		struct {
4648 #if	BYTE_ORDER == BIG_ENDIAN
4649 			u_int32_t hi32;
4650 			u_int32_t lo32;
4651 #else
4652 			u_int32_t lo32;
4653 			u_int32_t hi32;
4654 #endif
4655 		} wd;
4656 		u_int64_t full64;
4657 	} wwnstore;
4658 
4659 	wwnstore.full64 = ISP2100_NVRAM_NODE_NAME(nvram_data);
4660 
4661 	/*
4662 	 * Broken PTI cards with nothing in the top nibble. Pah.
4663 	 */
4664 	if ((wwnstore.wd.hi32 >> 28) == 0) {
4665 		wwnstore.wd.hi32 |= (2 << 28);
4666 		CFGPRINTF("%s: (corrected) Adapter WWN 0x%08x%08x\n",
4667 		    isp->isp_name, wwnstore.wd.hi32, wwnstore.wd.lo32);
4668 	} else {
4669 		CFGPRINTF("%s: Adapter WWN 0x%08x%08x\n", isp->isp_name,
4670 		    wwnstore.wd.hi32, wwnstore.wd.lo32);
4671 	}
4672 	fcp->isp_nodewwn = wwnstore.full64;
4673 
4674 	/*
4675 	 * If the Node WWN has 2 in the top nibble, we can
4676 	 * authoritatively construct a Port WWN by adding
4677 	 * our unit number (plus one to make it nonzero) and
4678 	 * putting it into bits 59..56. If the top nibble isn't
4679 	 * 2, then we just set them identically.
4680 	 */
4681 	if ((fcp->isp_nodewwn >> 60) == 2) {
4682 		fcp->isp_portwwn = fcp->isp_nodewwn |
4683 		    (((u_int64_t)(isp->isp_unit+1)) << 56);
4684 	} else {
4685 		fcp->isp_portwwn = fcp->isp_nodewwn;
4686 	}
4687 	wwnstore.full64 = ISP2100_NVRAM_BOOT_NODE_NAME(nvram_data);
4688 	if (wwnstore.full64 != 0) {
4689 		PRINTF("%s: BOOT DEVICE WWN 0x%08x%08x\n",
4690 		    isp->isp_name, wwnstore.wd.hi32, wwnstore.wd.lo32);
4691 	}
4692 	fcp->isp_maxalloc =
4693 		ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data);
4694 	fcp->isp_maxfrmlen =
4695 		ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data);
4696 	fcp->isp_retry_delay =
4697 		ISP2100_NVRAM_RETRY_DELAY(nvram_data);
4698 	fcp->isp_retry_count =
4699 		ISP2100_NVRAM_RETRY_COUNT(nvram_data);
4700 	fcp->isp_loopid =
4701 		ISP2100_NVRAM_HARDLOOPID(nvram_data);
4702 	fcp->isp_execthrottle =
4703 		ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data);
4704 	fcp->isp_fwoptions = ISP2100_NVRAM_OPTIONS(nvram_data);
4705 	if (isp->isp_dblev > 2) {
4706 		PRINTF("%s: NVRAM values:\n", isp->isp_name);
4707 		PRINTF("  Max IOCB Allocation = %d\n",
4708 		    fcp->isp_maxalloc);
4709 		PRINTF("     Max Frame Length = %d\n",
4710 		    fcp->isp_maxfrmlen);
4711 		PRINTF("   Execution Throttle = %d\n",
4712 		    fcp->isp_execthrottle);
4713 		PRINTF("          Retry Count = %d\n",
4714 		    fcp->isp_retry_count);
4715 		PRINTF("          Retry Delay = %d\n",
4716 		    fcp->isp_retry_delay);
4717 		PRINTF("         Hard Loop ID = %d\n",
4718 		    fcp->isp_loopid);
4719 		PRINTF("              Options = 0x%x\n",
4720 		    fcp->isp_fwoptions);
4721 		PRINTF("          HBA Options = 0x%x\n",
4722 		    ISP2100_NVRAM_HBA_OPTIONS(nvram_data));
4723 	}
4724 }
4725