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