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