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