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