xref: /freebsd/sys/dev/isp/isp.c (revision a654d072959982a88cfbd609921641d85a8eb50c)
1 /* $FreeBSD$ */
2 /*
3  * Machine and OS Independent (well, as best as possible)
4  * code for the Qlogic ISP SCSI adapters.
5  *
6  *---------------------------------------
7  * Copyright (c) 1997, 1998 by Matthew Jacob
8  * NASA/Ames Research Center
9  * All rights reserved.
10  *---------------------------------------
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice immediately at the beginning of the file, without modification,
17  *    this list of conditions, and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. The name of the author may not be used to endorse or promote products
22  *    derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
28  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 /*
38  * Inspiration and ideas about this driver are from Erik Moe's Linux driver
39  * (qlogicisp.c) and Dave Miller's SBus version of same (qlogicisp.c). Some
40  * ideas dredged from the Solaris driver.
41  */
42 
43 /*
44  * Include header file appropriate for platform we're building on.
45  */
46 
47 #ifdef	__NetBSD__
48 #include <dev/ic/isp_netbsd.h>
49 #endif
50 #ifdef	__FreeBSD__
51 #include <dev/isp/isp_freebsd.h>
52 #endif
53 #ifdef	__linux__
54 #include <isp_linux.h>
55 #endif
56 
57 /*
58  * General defines
59  */
60 
61 #define	MBOX_DELAY_COUNT	1000000 / 100
62 
63 /*
64  * Local static data
65  */
66 #if	defined(ISP2100_TARGET_MODE) || defined(ISP_TARGET_MODE)
67 static const char tgtiqd[36] = {
68 	0x03, 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00,
69 	0x51, 0x4C, 0x4F, 0x47, 0x49, 0x43, 0x20, 0x20,
70 #ifdef	__NetBSD__
71 	0x4E, 0x45, 0x54, 0x42, 0x53, 0x44, 0x20, 0x20,
72 #else
73 # ifdef	__FreeBSD__
74 	0x46, 0x52, 0x45, 0x45, 0x42, 0x52, 0x44, 0x20,
75 # else
76 #  ifdef linux
77 	0x4C, 0x49, 0x4E, 0x55, 0x58, 0x20, 0x20, 0x20,
78 #  else
79 #  endif
80 # endif
81 #endif
82 	0x54, 0x41, 0x52, 0x47, 0x45, 0x54, 0x20, 0x20,
83 	0x20, 0x20, 0x20, 0x31
84 };
85 #endif
86 
87 
88 /*
89  * Local function prototypes.
90  */
91 static int isp_parse_async __P((struct ispsoftc *, u_int16_t));
92 static int isp_handle_other_response
93 __P((struct ispsoftc *, ispstatusreq_t *, u_int8_t *));
94 #if	defined(ISP2100_TARGET_MODE) || defined(ISP_TARGET_MODE)
95 static int isp_modify_lun __P((struct ispsoftc *, int, int, int));
96 #endif
97 static void isp_parse_status
98 __P((struct ispsoftc *, ispstatusreq_t *, ISP_SCSI_XFER_T *));
99 static void isp_fibre_init __P((struct ispsoftc *));
100 static void isp_fw_state __P((struct ispsoftc *));
101 static void isp_dumpregs __P((struct ispsoftc *, const char *));
102 static void isp_dumpxflist __P((struct ispsoftc *));
103 static void isp_prtstst __P((ispstatusreq_t *));
104 static char *isp2100_fw_statename __P((u_int8_t));
105 static void isp_mboxcmd __P((struct ispsoftc *, mbreg_t *));
106 
107 static void isp_update  __P((struct ispsoftc *));
108 static void isp_setdfltparm __P((struct ispsoftc *));
109 static int isp_read_nvram __P((struct ispsoftc *));
110 static void isp_rdnvram_word __P((struct ispsoftc *, int, u_int16_t *));
111 
112 /*
113  * Reset Hardware.
114  *
115  * Hit the chip over the head, download new f/w.
116  *
117  * Locking done elsewhere.
118  */
119 void
120 isp_reset(isp)
121 	struct ispsoftc *isp;
122 {
123 	static char once = 1;
124 	mbreg_t mbs;
125 	int loops, i, dodnld = 1;
126 	char *revname;
127 
128 	isp->isp_state = ISP_NILSTATE;
129 
130 	/*
131 	 * Basic types (SCSI, FibreChannel and PCI or SBus)
132 	 * have been set in the MD code. We figure out more
133 	 * here.
134 	 */
135 	isp->isp_dblev = DFLT_DBLEVEL;
136 	if (isp->isp_type & ISP_HA_FC) {
137 		revname = "2100";
138 	} else {
139 		sdparam *sdp = isp->isp_param;
140 
141 		int rev = ISP_READ(isp, BIU_CONF0) & BIU_CONF0_HW_MASK;
142 		switch (rev) {
143 		default:
144 			PRINTF("%s: unknown chip rev. 0x%x- assuming a 1020\n",
145 			    isp->isp_name, rev);
146 			/* FALLTHROUGH */
147 		case 1:
148 			revname = "1020";
149 			isp->isp_type = ISP_HA_SCSI_1020;
150 			sdp->isp_clock = 40;
151 			break;
152 		case 2:
153 			/*
154 			 * Some 1020A chips are Ultra Capable, but don't
155 			 * run the clock rate up for that unless told to
156 			 * do so by the Ultra Capable bits being set.
157 			 */
158 			revname = "1020A";
159 			isp->isp_type = ISP_HA_SCSI_1020A;
160 			sdp->isp_clock = 40;
161 			break;
162 		case 3:
163 			revname = "1040";
164 			isp->isp_type = ISP_HA_SCSI_1040;
165 			sdp->isp_clock = 60;
166 			break;
167 		case 4:
168 			revname = "1040A";
169 			isp->isp_type = ISP_HA_SCSI_1040A;
170 			sdp->isp_clock = 60;
171 			break;
172 		case 5:
173 			revname = "1040B";
174 			isp->isp_type = ISP_HA_SCSI_1040B;
175 			sdp->isp_clock = 60;
176 			break;
177 		}
178 		/*
179 		 * Try and figure out if we're connected to a differential bus.
180 		 * You have to pause the RISC processor to read SXP registers.
181 		 */
182 		ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
183 		i = 100;
184 		while ((ISP_READ(isp, HCCR) & HCCR_PAUSE) == 0) {
185 			SYS_DELAY(20);
186 			if (--i == 0) {
187 				PRINTF("%s: unable to pause RISC processor\n",
188 				    isp->isp_name);
189 				i = -1;
190 				break;
191 			}
192 		}
193 		if (i > 0) {
194 			if (isp->isp_bustype != ISP_BT_SBUS) {
195 				ISP_SETBITS(isp, BIU_CONF1, BIU_PCI_CONF1_SXP);
196 			}
197 			if (ISP_READ(isp, SXP_PINS_DIFF) & SXP_PINS_DIFF_MODE) {
198 				IDPRINTF(2, ("%s: Differential Mode Set\n",
199 				    isp->isp_name));
200 				sdp->isp_diffmode = 1;
201 			} else {
202 				sdp->isp_diffmode = 0;
203 			}
204 
205 			if (isp->isp_bustype != ISP_BT_SBUS) {
206 				ISP_CLRBITS(isp, BIU_CONF1, BIU_PCI_CONF1_SXP);
207 			}
208 
209 			/*
210 			 * Figure out whether we're ultra capable.
211 			 */
212 			i = ISP_READ(isp, RISC_PSR);
213 			if (isp->isp_bustype != ISP_BT_SBUS) {
214 				i &= RISC_PSR_PCI_ULTRA;
215 			} else {
216 				i &= RISC_PSR_SBUS_ULTRA;
217 			}
218 			if (i) {
219 				IDPRINTF(2, ("%s: Ultra Mode Capable\n",
220 				    isp->isp_name));
221 				sdp->isp_clock = 60;
222 			} else {
223 				sdp->isp_clock = 40;
224 			}
225 			/*
226 			 * Restart processor
227 			 */
228 			ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
229 		}
230 		/*
231 		 * Machine dependent clock (if set) overrides
232 		 * our generic determinations.
233 		 */
234 		if (isp->isp_mdvec->dv_clock) {
235 			if (isp->isp_mdvec->dv_clock < sdp->isp_clock) {
236 				sdp->isp_clock = isp->isp_mdvec->dv_clock;
237 			}
238 		}
239 	}
240 
241 	/*
242 	 * Do MD specific pre initialization
243 	 */
244 	ISP_RESET0(isp);
245 
246 	if (once == 1) {
247 		once = 0;
248 		/*
249 		 * Get the current running firmware revision out of the
250 		 * chip before we hit it over the head (if this is our
251 		 * first time through). Note that we store this as the
252 		 * 'ROM' firmware revision- which it may not be. In any
253 		 * case, we don't really use this yet, but we may in
254 		 * the future.
255 		 */
256 		mbs.param[0] = MBOX_ABOUT_FIRMWARE;
257 		isp_mboxcmd(isp, &mbs);
258 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
259 			IDPRINTF(3, ("%s: initial ABOUT FIRMWARE command "
260 			    "failed\n", isp->isp_name));
261 		} else {
262 			isp->isp_romfw_rev =
263 			    (((u_int16_t) mbs.param[1]) << 10) + mbs.param[2];
264 		}
265 	}
266 
267 	/*
268 	 * Hit the chip over the head with hammer,
269 	 * and give the ISP a chance to recover.
270 	 */
271 
272 	if (isp->isp_type & ISP_HA_SCSI) {
273 		ISP_WRITE(isp, BIU_ICR, BIU_ICR_SOFT_RESET);
274 		/*
275 		 * A slight delay...
276 		 */
277 		SYS_DELAY(100);
278 
279 		/*
280 		 * Clear data && control DMA engines.
281 		 */
282 		ISP_WRITE(isp, CDMA_CONTROL,
283 		      DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
284 		ISP_WRITE(isp, DDMA_CONTROL,
285 		      DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
286 	} else {
287 		ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
288 		/*
289 		 * A slight delay...
290 		 */
291 		SYS_DELAY(100);
292 		ISP_WRITE(isp, CDMA2100_CONTROL,
293 			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
294 		ISP_WRITE(isp, TDMA2100_CONTROL,
295 			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
296 		ISP_WRITE(isp, RDMA2100_CONTROL,
297 			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
298 	}
299 
300 	/*
301 	 * Wait for ISP to be ready to go...
302 	 */
303 	loops = MBOX_DELAY_COUNT;
304 	for (;;) {
305 		if (isp->isp_type & ISP_HA_SCSI) {
306 			if (!(ISP_READ(isp, BIU_ICR) & BIU_ICR_SOFT_RESET))
307 				break;
308 		} else {
309 			if (!(ISP_READ(isp, BIU2100_CSR) & BIU2100_SOFT_RESET))
310 				break;
311 		}
312 		SYS_DELAY(100);
313 		if (--loops < 0) {
314 			isp_dumpregs(isp, "chip reset timed out");
315 			return;
316 		}
317 	}
318 	/*
319 	 * More initialization
320 	 */
321 	if (isp->isp_type & ISP_HA_SCSI) {
322 		ISP_WRITE(isp, BIU_CONF1, 0);
323 	} else {
324 		ISP_WRITE(isp, BIU2100_CSR, 0);
325 		/*
326 		 * All 2100's are 60Mhz with fast rams onboard.
327 		 */
328 		ISP_WRITE(isp, RISC_MTR2100, 0x1212);
329 	}
330 
331 	ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
332 	SYS_DELAY(100);
333 
334 	if (isp->isp_type & ISP_HA_SCSI) {
335 		ISP_SETBITS(isp, BIU_CONF1, isp->isp_mdvec->dv_conf1);
336 		if (isp->isp_mdvec->dv_conf1 & BIU_BURST_ENABLE) {
337 			ISP_SETBITS(isp, CDMA_CONF, DMA_ENABLE_BURST);
338 			ISP_SETBITS(isp, DDMA_CONF, DMA_ENABLE_BURST);
339 		}
340 	}
341 	ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE); /* release paused processor */
342 
343 	/*
344 	 * Do MD specific post initialization
345 	 */
346 	ISP_RESET1(isp);
347 
348 	/*
349 	 * Enable interrupts
350 	 */
351 	ENABLE_INTS(isp);
352 
353 	/*
354 	 * Do some sanity checking.
355 	 */
356 	mbs.param[0] = MBOX_NO_OP;
357 	isp_mboxcmd(isp, &mbs);
358 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
359 		isp_dumpregs(isp, "NOP test failed");
360 		return;
361 	}
362 
363 	if (isp->isp_type & ISP_HA_SCSI) {
364 		mbs.param[0] = MBOX_MAILBOX_REG_TEST;
365 		mbs.param[1] = 0xdead;
366 		mbs.param[2] = 0xbeef;
367 		mbs.param[3] = 0xffff;
368 		mbs.param[4] = 0x1111;
369 		mbs.param[5] = 0xa5a5;
370 		isp_mboxcmd(isp, &mbs);
371 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
372 			isp_dumpregs(isp,
373 				"Mailbox Register test didn't complete");
374 			return;
375 		}
376 		if (mbs.param[1] != 0xdead || mbs.param[2] != 0xbeef ||
377 		    mbs.param[3] != 0xffff || mbs.param[4] != 0x1111 ||
378 		    mbs.param[5] != 0xa5a5) {
379 			isp_dumpregs(isp, "Register Test Failed");
380 			return;
381 		}
382 
383 	}
384 
385 	/*
386 	 * Download new Firmware, unless requested not to do so.
387 	 * This is made slightly trickier in some cases where the
388 	 * firmware of the ROM revision is newer than the revision
389 	 * compiled into the driver. So, where we used to compare
390 	 * versions of our f/w and the ROM f/w, now we just see
391 	 * whether we have f/w at all and whether a config flag
392 	 * has disabled our download.
393 	 */
394 	if ((isp->isp_mdvec->dv_fwlen == 0) ||
395 	    (isp->isp_confopts & ISP_CFG_NORELOAD)) {
396 		dodnld = 0;
397 	}
398 
399 	if (dodnld) {
400 		for (i = 0; i < isp->isp_mdvec->dv_fwlen; i++) {
401 			mbs.param[0] = MBOX_WRITE_RAM_WORD;
402 			mbs.param[1] = isp->isp_mdvec->dv_codeorg + i;
403 			mbs.param[2] = isp->isp_mdvec->dv_ispfw[i];
404 			isp_mboxcmd(isp, &mbs);
405 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
406 				isp_dumpregs(isp, "f/w download failed");
407 				return;
408 			}
409 		}
410 
411 		if (isp->isp_mdvec->dv_fwlen) {
412 			/*
413 			 * Verify that it downloaded correctly.
414 			 */
415 			mbs.param[0] = MBOX_VERIFY_CHECKSUM;
416 			mbs.param[1] = isp->isp_mdvec->dv_codeorg;
417 			isp_mboxcmd(isp, &mbs);
418 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
419 				isp_dumpregs(isp, "ram checksum failure");
420 				return;
421 			}
422 		}
423 	} else {
424 		IDPRINTF(3, ("%s: skipping f/w download\n", isp->isp_name));
425 	}
426 
427 	/*
428 	 * Now start it rolling.
429 	 *
430 	 * If we didn't actually download f/w,
431 	 * we still need to (re)start it.
432 	 */
433 
434 	mbs.param[0] = MBOX_EXEC_FIRMWARE;
435 	mbs.param[1] = isp->isp_mdvec->dv_codeorg;
436 	isp_mboxcmd(isp, &mbs);
437 
438 	if (isp->isp_type & ISP_HA_SCSI) {
439 		sdparam *sdp = isp->isp_param;
440 		/*
441 		 * Set CLOCK RATE, but only if asked to.
442 		 */
443 		if (sdp->isp_clock) {
444 			mbs.param[0] = MBOX_SET_CLOCK_RATE;
445 			mbs.param[1] = sdp->isp_clock;
446 			isp_mboxcmd(isp, &mbs);
447 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
448 				isp_dumpregs(isp, "failed to set CLOCKRATE");
449 				/* but continue */
450 			} else {
451 				IDPRINTF(3, ("%s: setting input clock to %d\n",
452 				    isp->isp_name, sdp->isp_clock));
453 			}
454 		}
455 	}
456 	mbs.param[0] = MBOX_ABOUT_FIRMWARE;
457 	isp_mboxcmd(isp, &mbs);
458 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
459 		isp_dumpregs(isp, "ABOUT FIRMWARE command failed");
460 		return;
461 	}
462 	PRINTF("%s: Board Revision %s, %s F/W Revision %d.%d\n",
463 		isp->isp_name, revname, dodnld? "loaded" : "resident",
464 		mbs.param[1], mbs.param[2]);
465 	isp->isp_fwrev = (((u_int16_t) mbs.param[1]) << 10) + mbs.param[2];
466 	if (isp->isp_romfw_rev && dodnld) {
467 		PRINTF("%s: Last F/W revision was %d.%d\n", isp->isp_name,
468 		    isp->isp_romfw_rev >> 10, isp->isp_romfw_rev & 0x3ff);
469 	}
470 	isp_fw_state(isp);
471 	isp->isp_state = ISP_RESETSTATE;
472 }
473 
474 /*
475  * Initialize Hardware to known state
476  *
477  * Locks are held before coming here.
478  */
479 
480 void
481 isp_init(isp)
482 	struct ispsoftc *isp;
483 {
484 	sdparam *sdp;
485 	mbreg_t mbs;
486 	int tgt;
487 
488 	/*
489 	 * Must do first.
490 	 */
491 	isp_setdfltparm(isp);
492 
493 	/*
494 	 * If we're fibre, we have a completely different
495 	 * initialization method.
496 	 */
497 
498 	if (isp->isp_type & ISP_HA_FC) {
499 		isp_fibre_init(isp);
500 		return;
501 	}
502 	sdp = isp->isp_param;
503 
504 	/*
505 	 * Set (possibly new) Initiator ID.
506 	 */
507 	mbs.param[0] = MBOX_SET_INIT_SCSI_ID;
508 	mbs.param[1] = sdp->isp_initiator_id;
509 	isp_mboxcmd(isp, &mbs);
510 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
511 		isp_dumpregs(isp, "failed to set initiator id");
512 		return;
513 	}
514 
515 	/*
516 	 * Set Retry Delay and Count
517 	 */
518 	mbs.param[0] = MBOX_SET_RETRY_COUNT;
519 	mbs.param[1] = sdp->isp_retry_count;
520 	mbs.param[2] = sdp->isp_retry_delay;
521 	isp_mboxcmd(isp, &mbs);
522 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
523 		isp_dumpregs(isp, "failed to set retry count and delay");
524 		return;
525 	}
526 
527 	/*
528 	 * Set ASYNC DATA SETUP time. This is very important.
529 	 */
530 	mbs.param[0] = MBOX_SET_ASYNC_DATA_SETUP_TIME;
531 	mbs.param[1] = sdp->isp_async_data_setup;
532 	isp_mboxcmd(isp, &mbs);
533 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
534 		isp_dumpregs(isp, "failed to set async data setup time");
535 		return;
536 	}
537 
538 	/*
539 	 * Set ACTIVE Negation State.
540 	 */
541 	mbs.param[0] = MBOX_SET_ACTIVE_NEG_STATE;
542 	mbs.param[1] =
543 	    (sdp->isp_req_ack_active_neg << 4) |
544 	    (sdp->isp_data_line_active_neg << 5);
545 	isp_mboxcmd(isp, &mbs);
546 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
547 		isp_dumpregs(isp, "failed to set active neg state");
548 		return;
549 	}
550 
551 	/*
552 	 * Set the Tag Aging limit
553 	 */
554 
555 	mbs.param[0] = MBOX_SET_TAG_AGE_LIMIT;
556 	mbs.param[1] = sdp->isp_tag_aging;
557 	isp_mboxcmd(isp, &mbs);
558 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
559 		isp_dumpregs(isp, "failed to set tag age limit");
560 		return;
561 	}
562 
563 	/*
564 	 * Set selection timeout.
565 	 */
566 
567 	mbs.param[0] = MBOX_SET_SELECT_TIMEOUT;
568 	mbs.param[1] = sdp->isp_selection_timeout;
569 	isp_mboxcmd(isp, &mbs);
570 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
571 		isp_dumpregs(isp, "failed to set selection timeout");
572 		return;
573 	}
574 
575 	/*
576 	 * Set per-target parameters to a safe minimum.
577 	 */
578 
579 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
580 		int maxlun, lun;
581 
582 		if (sdp->isp_devparam[tgt].dev_enable == 0)
583 			continue;
584 
585 		mbs.param[0] = MBOX_SET_TARGET_PARAMS;
586 		mbs.param[1] = tgt << 8;
587 		mbs.param[2] = DPARM_SAFE_DFLT;
588 		mbs.param[3] = 0;
589 		/*
590 		 * It is not quite clear when this changed over so that
591 		 * we could force narrow and async, so assume >= 7.55.
592 		 *
593 		 * Otherwise, a SCSI bus reset issued below will force
594 		 * the back to the narrow, async state (but see note
595 		 * below also). Technically we should also do without
596 		 * Parity.
597 		 */
598 		if (isp->isp_fwrev >= ISP_FW_REV(7, 55)) {
599 			mbs.param[2] |= DPARM_NARROW | DPARM_ASYNC;
600 		}
601 		sdp->isp_devparam[tgt].cur_dflags = mbs.param[2] >> 8;
602 
603 		IDPRINTF(3, ("\n%s: tgt %d cflags %x offset %x period %x\n",
604 		    isp->isp_name, tgt, mbs.param[2], mbs.param[3] >> 8,
605 		    mbs.param[3] & 0xff));
606 		isp_mboxcmd(isp, &mbs);
607 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
608 
609 			PRINTF("%s: failed to set parameters for tgt %d\n",
610 				isp->isp_name, tgt);
611 
612 			PRINTF("%s: flags %x offset %x period %x\n",
613 				isp->isp_name, sdp->isp_devparam[tgt].dev_flags,
614 				sdp->isp_devparam[tgt].sync_offset,
615 				sdp->isp_devparam[tgt].sync_period);
616 
617 			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
618 			mbs.param[1] = tgt << 8;
619 			mbs.param[2] = DPARM_SAFE_DFLT;
620 			mbs.param[3] = 0;
621 			isp_mboxcmd(isp, &mbs);
622 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
623 				PRINTF("%s: failed even to set defaults for "
624 				    "target %d\n", isp->isp_name, tgt);
625 				continue;
626 			}
627 		}
628 
629 		maxlun = (isp->isp_fwrev >= ISP_FW_REV(7, 55))? 32 : 8;
630 		for (lun = 0; lun < maxlun; lun++) {
631 			mbs.param[0] = MBOX_SET_DEV_QUEUE_PARAMS;
632 			mbs.param[1] = (tgt << 8) | lun;
633 			mbs.param[2] = sdp->isp_max_queue_depth;
634 			mbs.param[3] = sdp->isp_devparam[tgt].exc_throttle;
635 			isp_mboxcmd(isp, &mbs);
636 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
637 				PRINTF("%s: failed to set device queue "
638 				    "parameters for target %d, lun %d\n",
639 				    isp->isp_name, tgt, lun);
640 				break;
641 			}
642 		}
643 	}
644 
645 	/*
646 	 * Set up DMA for the request and result mailboxes.
647 	 */
648 	if (ISP_MBOXDMASETUP(isp) != 0) {
649 		PRINTF("%s: can't setup dma mailboxes\n", isp->isp_name);
650 		return;
651 	}
652 
653 	mbs.param[0] = MBOX_INIT_RES_QUEUE;
654 	mbs.param[1] = RESULT_QUEUE_LEN;
655 	mbs.param[2] = (u_int16_t) (isp->isp_result_dma >> 16);
656 	mbs.param[3] = (u_int16_t) (isp->isp_result_dma & 0xffff);
657 	mbs.param[4] = 0;
658 	mbs.param[5] = 0;
659 	isp_mboxcmd(isp, &mbs);
660 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
661 		isp_dumpregs(isp, "set of response queue failed");
662 		return;
663 	}
664 	isp->isp_residx = 0;
665 
666 	mbs.param[0] = MBOX_INIT_REQ_QUEUE;
667 	mbs.param[1] = RQUEST_QUEUE_LEN;
668 	mbs.param[2] = (u_int16_t) (isp->isp_rquest_dma >> 16);
669 	mbs.param[3] = (u_int16_t) (isp->isp_rquest_dma & 0xffff);
670 	mbs.param[4] = 0;
671 	mbs.param[5] = 0;
672 	isp_mboxcmd(isp, &mbs);
673 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
674 		isp_dumpregs(isp, "set of request queue failed");
675 		return;
676 	}
677 	isp->isp_reqidx = isp->isp_reqodx = 0;
678 
679 	/*
680 	 * XXX: See whether or not for 7.55 F/W or later we
681 	 * XXX: can do without this, and see whether we should
682 	 * XXX: honor the NVRAM SCSI_RESET_DISABLE token.
683 	 */
684 	mbs.param[0] = MBOX_BUS_RESET;
685 	mbs.param[1] = 3;
686 	isp_mboxcmd(isp, &mbs);
687 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
688 		isp_dumpregs(isp, "SCSI bus reset failed");
689 	}
690 	/*
691 	 * This is really important to have set after a bus reset.
692 	 */
693 	isp->isp_sendmarker = 1;
694 	isp->isp_state = ISP_INITSTATE;
695 }
696 
697 /*
698  * Fibre Channel specific initialization.
699  *
700  * Locks are held before coming here.
701  */
702 static void
703 isp_fibre_init(isp)
704 	struct ispsoftc *isp;
705 {
706 	fcparam *fcp;
707 	isp_icb_t *icbp;
708 	mbreg_t mbs;
709 	int count;
710 	u_int8_t lwfs;
711 
712 	fcp = isp->isp_param;
713 
714 	if (ISP_MBOXDMASETUP(isp) != 0) {
715 		PRINTF("%s: can't setup DMA for mailboxes\n", isp->isp_name);
716 		return;
717 	}
718 
719 	icbp = (isp_icb_t *) fcp->isp_scratch;
720 	bzero(icbp, sizeof (*icbp));
721 
722 	icbp->icb_version = ICB_VERSION1;
723 
724 	fcp->isp_fwoptions = 0;
725 #ifdef	ISP2100_TARGET_MODE
726 	fcp->isp_fwoptions |= ICBOPT_TGT_ENABLE	| ICBOPT_INI_TGTTYPE;
727 	icbp->icb_iqdevtype = 0x23;	/* DPQ_SUPPORTED/PROCESSOR */
728 #endif
729 	icbp->icb_fwoptions = fcp->isp_fwoptions;
730 	icbp->icb_maxfrmlen = fcp->isp_maxfrmlen;
731 	if (icbp->icb_maxfrmlen < ICB_MIN_FRMLEN ||
732 	    icbp->icb_maxfrmlen > ICB_MAX_FRMLEN) {
733 		PRINTF("%s: bad frame length (%d) from NVRAM- using %d\n",
734 		    isp->isp_name, fcp->isp_maxfrmlen, ICB_DFLT_FRMLEN);
735 	}
736 	icbp->icb_maxalloc = fcp->isp_maxalloc;
737 	icbp->icb_execthrottle = fcp->isp_execthrottle;
738 	icbp->icb_retry_delay = fcp->isp_retry_delay;
739 	icbp->icb_retry_count = fcp->isp_retry_count;
740 
741 	MAKE_NODE_NAME_FROM_WWN(icbp->icb_nodename, fcp->isp_wwn);
742 
743 	icbp->icb_rqstqlen = RQUEST_QUEUE_LEN;
744 	icbp->icb_rsltqlen = RESULT_QUEUE_LEN;
745 	icbp->icb_rqstaddr[RQRSP_ADDR0015] =
746 	    (u_int16_t) (isp->isp_rquest_dma & 0xffff);
747 	icbp->icb_rqstaddr[RQRSP_ADDR1631] =
748 	    (u_int16_t) (isp->isp_rquest_dma >> 16);
749 	icbp->icb_respaddr[RQRSP_ADDR0015] =
750 	    (u_int16_t) (isp->isp_result_dma & 0xffff);
751 	icbp->icb_respaddr[RQRSP_ADDR1631] =
752 	    (u_int16_t) (isp->isp_result_dma >> 16);
753 
754 	for (count = 0; count < 10; count++) {
755 		mbs.param[0] = MBOX_INIT_FIRMWARE;
756 		mbs.param[1] = 0;
757 		mbs.param[2] = (u_int16_t) (fcp->isp_scdma >> 16);
758 		mbs.param[3] = (u_int16_t) (fcp->isp_scdma & 0xffff);
759 		mbs.param[4] = 0;
760 		mbs.param[5] = 0;
761 		mbs.param[6] = 0;
762 		mbs.param[7] = 0;
763 
764 		isp_mboxcmd(isp, &mbs);
765 
766 		switch (mbs.param[0]) {
767 		case MBOX_COMMAND_COMPLETE:
768 			count = 10;
769 			break;
770 		case ASYNC_LIP_OCCURRED:
771 		case ASYNC_LOOP_UP:
772 		case ASYNC_LOOP_DOWN:
773 		case ASYNC_LOOP_RESET:
774 		case ASYNC_PDB_CHANGED:
775 		case ASYNC_CHANGE_NOTIFY:
776 			if (count > 9) {
777 				PRINTF("%s: too many retries to get going- "
778 				    "giving up\n", isp->isp_name);
779 				return;
780 			}
781 			break;
782 		default:
783 			isp_dumpregs(isp, "INIT FIRMWARE failed");
784 			return;
785 		}
786 	}
787 	isp->isp_reqidx = isp->isp_reqodx = 0;
788 	isp->isp_residx = 0;
789 
790 	/*
791 	 * Wait up to 12 seconds for FW to go to READY state.
792 	 * This used to be 3 seconds, but that lost.
793 	 *
794 	 * This is all very much not right. The problem here
795 	 * is that the cable may not be plugged in, or there
796 	 * may be many many members of the loop that haven't
797 	 * been logged into.
798 	 *
799 	 * This model of doing things doesn't support dynamic
800 	 * attachment, so we just plain lose (for now).
801 	 */
802 	lwfs = FW_CONFIG_WAIT;
803 	for (count = 0; count < 12000; count++) {
804 		isp_fw_state(isp);
805 		if (lwfs != fcp->isp_fwstate) {
806 			PRINTF("%s: Firmware State %s -> %s\n",
807 			    isp->isp_name, isp2100_fw_statename(lwfs),
808 			    isp2100_fw_statename(fcp->isp_fwstate));
809 			lwfs = fcp->isp_fwstate;
810 		}
811 		if (fcp->isp_fwstate == FW_READY) {
812 			break;
813 		}
814 		SYS_DELAY(1000);	/* wait one millisecond */
815 	}
816 	isp->isp_sendmarker = 1;
817 
818 	/*
819 	 * Get our Loop ID
820 	 * (if possible)
821 	 */
822 	if (fcp->isp_fwstate == FW_READY) {
823 		mbs.param[0] = MBOX_GET_LOOP_ID;
824 		isp_mboxcmd(isp, &mbs);
825 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
826 			isp_dumpregs(isp, "GET LOOP ID failed");
827 			return;
828 		}
829 		fcp->isp_loopid = mbs.param[1];
830 		fcp->isp_alpa = mbs.param[2];
831 		PRINTF("%s: Loop ID 0x%x, ALPA 0x%x\n", isp->isp_name,
832 		    fcp->isp_loopid, fcp->isp_alpa);
833 		isp->isp_state = ISP_INITSTATE;
834 #if	defined(ISP2100_TARGET_MODE) || defined(ISP_TARGET_MODE)
835 		DISABLE_INTS(isp);
836 		if (isp->isp_fwrev >= ISP_FW_REV(1, 13)) {
837 			if (isp_modify_lun(isp, 0, 1, 1)) {
838 				PRINTF("%s: failed to establish target mode\n",
839 				    isp->isp_name);
840 			}
841 		}
842 		ENABLE_INTS(isp);
843 #endif
844 	} else {
845 		PRINTF("%s: failed to go to FW READY state- will not attach\n",
846 		    isp->isp_name);
847 	}
848 }
849 
850 /*
851  * Free any associated resources prior to decommissioning and
852  * set the card to a known state (so it doesn't wake up and kick
853  * us when we aren't expecting it to).
854  *
855  * Locks are held before coming here.
856  */
857 void
858 isp_uninit(isp)
859 	struct ispsoftc *isp;
860 {
861 	/*
862 	 * Leave with interrupts disabled.
863 	 */
864 	DISABLE_INTS(isp);
865 
866 	/*
867 	 * Stop the watchdog timer (if started).
868 	 */
869 	STOP_WATCHDOG(isp_watch, isp);
870 }
871 
872 
873 /*
874  * Start a command. Locking is assumed done in the caller.
875  */
876 
877 int32_t
878 ispscsicmd(xs)
879 	ISP_SCSI_XFER_T *xs;
880 {
881 	struct ispsoftc *isp;
882 	u_int8_t iptr, optr;
883 	union {
884 		ispreq_t *_reqp;
885 		ispreqt2_t *_t2reqp;
886 	} _u;
887 #define	reqp	_u._reqp
888 #define	t2reqp	_u._t2reqp
889 #define	UZSIZE	max(sizeof (ispreq_t), sizeof (ispreqt2_t))
890 	int i;
891 
892 	XS_INITERR(xs);
893 	isp = XS_ISP(xs);
894 
895 	if (isp->isp_state != ISP_RUNSTATE) {
896 		PRINTF("%s: adapter not ready\n", isp->isp_name);
897 		XS_SETERR(xs, HBA_BOTCH);
898 		return (CMD_COMPLETE);
899 	}
900 
901 	/*
902 	 * We *could* do the different sequence type that has clos
903 	 * to the whole Queue Entry for the command,.
904 	 */
905 	if (XS_CDBLEN(xs) > ((isp->isp_type & ISP_HA_FC)? 16 : 12)) {
906 		PRINTF("%s: unsupported cdb length (%d)\n",
907 		    isp->isp_name, XS_CDBLEN(xs));
908 		XS_SETERR(xs, HBA_BOTCH);
909 		return (CMD_COMPLETE);
910 	}
911 
912 	/*
913 	 * First check to see if any HBA or Device
914 	 * parameters need to be updated.
915 	 */
916 	if (isp->isp_update) {
917 		isp_update(isp);
918 	}
919 
920 	optr = isp->isp_reqodx = ISP_READ(isp, OUTMAILBOX4);
921 	iptr = isp->isp_reqidx;
922 
923 	reqp = (ispreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, iptr);
924 	iptr = ISP_NXT_QENTRY(iptr, RQUEST_QUEUE_LEN);
925 	if (iptr == optr) {
926 		IDPRINTF(2, ("%s: Request Queue Overflow\n", isp->isp_name));
927 		XS_SETERR(xs, HBA_BOTCH);
928 		return (CMD_EAGAIN);
929 	}
930 	if (isp->isp_type & ISP_HA_FC) {
931 		DISABLE_INTS(isp);
932 	}
933 
934 	if (isp->isp_sendmarker) {
935 		u_int8_t niptr;
936 		ispmarkreq_t *marker = (ispmarkreq_t *) reqp;
937 
938 		bzero((void *) marker, sizeof (*marker));
939 		marker->req_header.rqs_entry_count = 1;
940 		marker->req_header.rqs_entry_type = RQSTYPE_MARKER;
941 		marker->req_modifier = SYNC_ALL;
942 
943 		isp->isp_sendmarker = 0;
944 
945 		/*
946 		 * Unconditionally update the input pointer anyway.
947 		 */
948 		ISP_WRITE(isp, INMAILBOX4, iptr);
949 		isp->isp_reqidx = iptr;
950 
951 		niptr = ISP_NXT_QENTRY(iptr, RQUEST_QUEUE_LEN);
952 		if (niptr == optr) {
953 			if (isp->isp_type & ISP_HA_FC) {
954 				ENABLE_INTS(isp);
955 			}
956 			IDPRINTF(2, ("%s: Request Queue Overflow+\n",
957 			    isp->isp_name));
958 			XS_SETERR(xs, HBA_BOTCH);
959 			return (CMD_EAGAIN);
960 		}
961 		reqp = (ispreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, iptr);
962 		iptr = niptr;
963 	}
964 
965 	bzero((void *) reqp, UZSIZE);
966 	reqp->req_header.rqs_entry_count = 1;
967 	if (isp->isp_type & ISP_HA_FC) {
968 		reqp->req_header.rqs_entry_type = RQSTYPE_T2RQS;
969 	} else {
970 		reqp->req_header.rqs_entry_type = RQSTYPE_REQUEST;
971 	}
972 	reqp->req_header.rqs_flags = 0;
973 	reqp->req_header.rqs_seqno = isp->isp_seqno++;
974 
975 	for (i = 0; i < RQUEST_QUEUE_LEN; i++) {
976 		if (isp->isp_xflist[i] == NULL)
977 			break;
978 	}
979 	if (i == RQUEST_QUEUE_LEN) {
980 		if (isp->isp_type & ISP_HA_FC)
981 			ENABLE_INTS(isp);
982 		IDPRINTF(2, ("%s: out of xflist pointers\n", isp->isp_name));
983 		XS_SETERR(xs, HBA_BOTCH);
984 		return (CMD_EAGAIN);
985 	} else {
986 		/*
987 		 * Never have a handle that is zero, so
988 		 * set req_handle off by one.
989 		 */
990 		isp->isp_xflist[i] = xs;
991 		reqp->req_handle = i+1;
992 	}
993 
994 	if (isp->isp_type & ISP_HA_FC) {
995 		/*
996 		 * See comment in isp_intr
997 		 */
998 		XS_RESID(xs) = 0;
999 		/*
1000 		 * Fibre Channel always requires some kind of tag.
1001 		 * If we're marked as "Can't Tag", just do simple
1002 		 * instead of ordered tags. It's pretty clear to me
1003 		 * that we shouldn't do head of queue tagging in
1004 		 * this case.
1005 		 */
1006 		if (XS_CANTAG(xs)) {
1007 			t2reqp->req_flags = XS_KINDOF_TAG(xs);
1008 		} else {
1009  			t2reqp->req_flags = REQFLAG_STAG;
1010 		}
1011 	} else {
1012 		sdparam *sdp = (sdparam *)isp->isp_param;
1013 		if ((sdp->isp_devparam[XS_TGT(xs)].cur_dflags & DPARM_TQING) &&
1014 		    XS_CANTAG(xs)) {
1015 			reqp->req_flags = XS_KINDOF_TAG(xs);
1016 		} else {
1017 			reqp->req_flags = 0;
1018 		}
1019 	}
1020 	reqp->req_lun_trn = XS_LUN(xs);
1021 	reqp->req_target = XS_TGT(xs);
1022 	if (isp->isp_type & ISP_HA_SCSI) {
1023 		reqp->req_cdblen = XS_CDBLEN(xs);
1024 	}
1025 	bcopy((void *)XS_CDBP(xs), reqp->req_cdb, XS_CDBLEN(xs));
1026 
1027 	IDPRINTF(5, ("%s(%d.%d): START%d cmd 0x%x datalen %d\n", isp->isp_name,
1028 	    XS_TGT(xs), XS_LUN(xs), reqp->req_header.rqs_seqno,
1029 	    reqp->req_cdb[0], XS_XFRLEN(xs)));
1030 
1031 	reqp->req_time = XS_TIME(xs) / 1000;
1032 	if (reqp->req_time == 0 && XS_TIME(xs))
1033 		reqp->req_time = 1;
1034 	i = ISP_DMASETUP(isp, xs, reqp, &iptr, optr);
1035 	if (i != CMD_QUEUED) {
1036 		if (isp->isp_type & ISP_HA_FC)
1037 			ENABLE_INTS(isp);
1038 		/*
1039 		 * dmasetup sets actual error in packet, and
1040 		 * return what we were given to return.
1041 		 */
1042 		return (i);
1043 	}
1044 	XS_SETERR(xs, HBA_NOERROR);
1045 	ISP_WRITE(isp, INMAILBOX4, iptr);
1046 	isp->isp_reqidx = iptr;
1047 	if (isp->isp_type & ISP_HA_FC) {
1048 		ENABLE_INTS(isp);
1049 	}
1050 	isp->isp_nactive++;
1051 	return (CMD_QUEUED);
1052 #undef	reqp
1053 #undef	t2reqp
1054 }
1055 
1056 /*
1057  * isp control
1058  * Locks (ints blocked) assumed held.
1059  */
1060 
1061 int
1062 isp_control(isp, ctl, arg)
1063 	struct ispsoftc *isp;
1064 	ispctl_t ctl;
1065 	void *arg;
1066 {
1067 	ISP_SCSI_XFER_T *xs;
1068 	mbreg_t mbs;
1069 	int i;
1070 
1071 	switch (ctl) {
1072 	default:
1073 		PRINTF("%s: isp_control unknown control op %x\n",
1074 		    isp->isp_name, ctl);
1075 		break;
1076 
1077 	case ISPCTL_RESET_BUS:
1078 		mbs.param[0] = MBOX_BUS_RESET;
1079 		mbs.param[1] = (isp->isp_type & ISP_HA_FC)? 5: 2;
1080 		isp_mboxcmd(isp, &mbs);
1081 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1082 			isp_dumpregs(isp, "isp_control SCSI bus reset failed");
1083 			break;
1084 		}
1085 		/*
1086 		 * This is really important to have set after a bus reset.
1087 		 */
1088 		isp->isp_sendmarker = 1;
1089 		PRINTF("%s: driver initiated bus reset\n", isp->isp_name);
1090 		return (0);
1091 
1092         case ISPCTL_RESET_DEV:
1093 		/*
1094 		 * Note that under parallel SCSI, this issues a BDR message.
1095 		 * Under FC, we could probably be using ABORT TASK SET
1096 		 * command.
1097 		 */
1098 
1099 		mbs.param[0] = MBOX_ABORT_TARGET;
1100 		mbs.param[1] = ((long)arg) << 8;
1101 		mbs.param[2] = 2;	/* 'delay', in seconds */
1102 		isp_mboxcmd(isp, &mbs);
1103 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1104 			isp_dumpregs(isp, "SCSI Target  reset failed");
1105 			break;
1106 		}
1107 		PRINTF("%s: Target %d Reset Succeeded\n", isp->isp_name,
1108 		    (int) ((long) arg));
1109 		isp->isp_sendmarker = 1;
1110 		return (0);
1111 
1112         case ISPCTL_ABORT_CMD:
1113 		xs = (ISP_SCSI_XFER_T *) arg;
1114 		for (i = 0; i < RQUEST_QUEUE_LEN; i++) {
1115 			if (xs == isp->isp_xflist[i]) {
1116 				break;
1117 			}
1118 		}
1119 		if (i == RQUEST_QUEUE_LEN) {
1120 			PRINTF("%s: isp_control- cannot find command to abort "
1121 			    "in active list\n", isp->isp_name);
1122 			break;
1123 		}
1124 		mbs.param[0] = MBOX_ABORT;
1125 		mbs.param[1] = XS_TGT(xs) | XS_LUN(xs);
1126 		mbs.param[2] = (i+1) >> 16;
1127 		mbs.param[3] = (i+1) & 0xffff;
1128 		isp_mboxcmd(isp, &mbs);
1129 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1130 			PRINTF("%s: isp_control MBOX_ABORT failure (code %x)\n",
1131 			    isp->isp_name, mbs.param[0]);
1132 			break;
1133 		}
1134 		PRINTF("%s: command for target %d lun %d was aborted\n",
1135 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1136 		return (0);
1137 
1138 	case ISPCTL_UPDATE_PARAMS:
1139 		isp_update(isp);
1140 		return(0);
1141 	}
1142 	return (-1);
1143 }
1144 
1145 /*
1146  * Interrupt Service Routine(s).
1147  *
1148  * External (OS) framework has done the appropriate locking,
1149  * and the locking will be held throughout this function.
1150  */
1151 
1152 int
1153 isp_intr(arg)
1154 	void *arg;
1155 {
1156 	ISP_SCSI_XFER_T *complist[RESULT_QUEUE_LEN], *xs;
1157 	struct ispsoftc *isp = arg;
1158 	u_int8_t iptr, optr;
1159 	u_int16_t isr;
1160 	int i, ndone = 0;
1161 
1162 	isr = ISP_READ(isp, BIU_ISR);
1163 	if (isp->isp_type & ISP_HA_FC) {
1164 		if (isr == 0 || (isr & BIU2100_ISR_RISC_INT) == 0) {
1165 			if (isr) {
1166 				IDPRINTF(4, ("%s: isp_intr isr=%x\n",
1167 					     isp->isp_name, isr));
1168 			}
1169 			return (0);
1170 		}
1171 	} else {
1172 		if (isr == 0 || (isr & BIU_ISR_RISC_INT) == 0) {
1173 			if (isr) {
1174 				IDPRINTF(4, ("%s: isp_intr isr=%x\n",
1175 					     isp->isp_name, isr));
1176 			}
1177 			return (0);
1178 		}
1179 	}
1180 
1181 	if (ISP_READ(isp, BIU_SEMA) & 1) {
1182 		u_int16_t mbox = ISP_READ(isp, OUTMAILBOX0);
1183 		if (isp_parse_async(isp, mbox))
1184 			return (1);
1185 		ISP_WRITE(isp, BIU_SEMA, 0);
1186 	}
1187 
1188 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
1189 
1190 	optr = isp->isp_residx;
1191 	iptr = ISP_READ(isp, OUTMAILBOX5);
1192 
1193 	if (optr == iptr) {
1194 		IDPRINTF(4, ("why intr? isr %x iptr %x optr %x\n",
1195 		    isr, optr, iptr));
1196 	}
1197 	ENABLE_INTS(isp);
1198 
1199 	while (optr != iptr) {
1200 		ispstatusreq_t *sp;
1201 		u_int8_t oop;
1202 		int buddaboom = 0;
1203 
1204 		sp = (ispstatusreq_t *) ISP_QUEUE_ENTRY(isp->isp_result, optr);
1205 		oop = optr;
1206 		optr = ISP_NXT_QENTRY(optr, RESULT_QUEUE_LEN);
1207 
1208 		if (sp->req_header.rqs_entry_type != RQSTYPE_RESPONSE) {
1209 			if (isp_handle_other_response(isp, sp, &optr) == 0) {
1210 				ISP_WRITE(isp, INMAILBOX5, optr);
1211 				continue;
1212 			}
1213 			/*
1214 			 * It really has to be a bounced request just copied
1215 			 * from the request queue to the response queue.
1216 			 */
1217 
1218 			if (sp->req_header.rqs_entry_type != RQSTYPE_REQUEST) {
1219 				ISP_WRITE(isp, INMAILBOX5, optr);
1220 				continue;
1221 			}
1222 			PRINTF("%s: not RESPONSE in RESPONSE Queue "
1223 			    "(type 0x%x) @ idx %d (next %d)\n", isp->isp_name,
1224 			    sp->req_header.rqs_entry_type, oop, optr);
1225 			buddaboom = 1;
1226 		}
1227 
1228 		if (sp->req_header.rqs_flags & 0xf) {
1229 			if (sp->req_header.rqs_flags & RQSFLAG_CONTINUATION) {
1230 				ISP_WRITE(isp, INMAILBOX5, optr);
1231 				continue;
1232 			}
1233 			PRINTF("%s: rqs_flags=%x", isp->isp_name,
1234 				sp->req_header.rqs_flags & 0xf);
1235 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
1236 				PRINTF("%s: internal queues full\n",
1237 				    isp->isp_name);
1238 				/* XXXX: this command *could* get restarted */
1239 				buddaboom++;
1240 			}
1241 			if (sp->req_header.rqs_flags & RQSFLAG_BADHEADER) {
1242 				PRINTF("%s: bad header\n", isp->isp_name);
1243 				buddaboom++;
1244 			}
1245 			if (sp->req_header.rqs_flags & RQSFLAG_BADPACKET) {
1246 				PRINTF("%s: bad request packet\n",
1247 				    isp->isp_name);
1248 				buddaboom++;
1249 			}
1250 		}
1251 		if (sp->req_handle > RQUEST_QUEUE_LEN || sp->req_handle < 1) {
1252 			PRINTF("%s: bad request handle %d\n", isp->isp_name,
1253 				sp->req_handle);
1254 			ISP_WRITE(isp, INMAILBOX5, optr);
1255 			continue;
1256 		}
1257 		xs = (ISP_SCSI_XFER_T *) isp->isp_xflist[sp->req_handle - 1];
1258 		if (xs == NULL) {
1259 			PRINTF("%s: NULL xs in xflist (handle %x)\n",
1260 			    isp->isp_name, sp->req_handle);
1261 			isp_dumpxflist(isp);
1262 			ISP_WRITE(isp, INMAILBOX5, optr);
1263 			continue;
1264 		}
1265 		isp->isp_xflist[sp->req_handle - 1] = NULL;
1266 		if (sp->req_status_flags & RQSTF_BUS_RESET) {
1267 			isp->isp_sendmarker = 1;
1268 		}
1269 		if (buddaboom) {
1270 			XS_SETERR(xs, HBA_BOTCH);
1271 		}
1272 		XS_STS(xs) = sp->req_scsi_status & 0xff;
1273 		if (isp->isp_type & ISP_HA_SCSI) {
1274 			if (sp->req_state_flags & RQSF_GOT_SENSE) {
1275 				bcopy(sp->req_sense_data, XS_SNSP(xs),
1276 					XS_SNSLEN(xs));
1277 				XS_SNS_IS_VALID(xs);
1278 			}
1279 		} else {
1280 			if (XS_STS(xs) == SCSI_CHECK) {
1281 				XS_SNS_IS_VALID(xs);
1282 				bcopy(sp->req_sense_data, XS_SNSP(xs),
1283 					XS_SNSLEN(xs));
1284 				sp->req_state_flags |= RQSF_GOT_SENSE;
1285 			}
1286 		}
1287 		if (XS_NOERR(xs) && XS_STS(xs) == SCSI_BUSY) {
1288 			XS_SETERR(xs, HBA_TGTBSY);
1289 		}
1290 
1291 		if (sp->req_header.rqs_entry_type == RQSTYPE_RESPONSE) {
1292 			if (XS_NOERR(xs)) {
1293 			    if (sp->req_completion_status != RQCS_COMPLETE) {
1294 				isp_parse_status(isp, sp, xs);
1295 			    } else {
1296 				XS_SETERR(xs, HBA_NOERROR);
1297 			    }
1298 			}
1299 		} else {
1300 			PRINTF("%s: unknown return %x\n", isp->isp_name,
1301 				sp->req_header.rqs_entry_type);
1302 			if (XS_NOERR(xs))
1303 				XS_SETERR(xs, HBA_BOTCH);
1304 		}
1305 		if (isp->isp_type & ISP_HA_SCSI) {
1306 			XS_RESID(xs) = sp->req_resid;
1307 		} else if (sp->req_scsi_status & RQCS_RU) {
1308 			XS_RESID(xs) = sp->req_resid;
1309 			IDPRINTF(4, ("%s: cnt %d rsd %d\n", isp->isp_name,
1310 				XS_XFRLEN(xs), sp->req_resid));
1311 		}
1312 		if (XS_XFRLEN(xs)) {
1313 			ISP_DMAFREE(isp, xs, sp->req_handle - 1);
1314 		}
1315 		/*
1316 		 * XXX: If we have a check condition, but no Sense Data,
1317 		 * XXX: mark it as an error (ARQ failed). We need to
1318 		 * XXX: to do a more distinct job because there may
1319 		 * XXX: cases where ARQ is disabled.
1320 		 */
1321 		if (XS_STS(xs) == SCSI_CHECK && !(XS_IS_SNS_VALID(xs))) {
1322 			if (XS_NOERR(xs)) {
1323 				PRINTF("%s: ARQ Failure\n", isp->isp_name);
1324 				XS_SETERR(xs, HBA_ARQFAIL);
1325 			}
1326 		}
1327 		if ((isp->isp_dblev >= 5) ||
1328 		    (isp->isp_dblev > 2 && !XS_NOERR(xs))) {
1329 			PRINTF("%s(%d.%d): FIN%d dl%d resid%d STS %x",
1330 			    isp->isp_name, XS_TGT(xs), XS_LUN(xs),
1331 			    sp->req_header.rqs_seqno, XS_XFRLEN(xs),
1332 			    XS_RESID(xs), XS_STS(xs));
1333 			if (sp->req_state_flags & RQSF_GOT_SENSE) {
1334 				PRINTF(" Skey: %x", XS_SNSKEY(xs));
1335 				if (!(XS_IS_SNS_VALID(xs))) {
1336 					PRINTF(" BUT NOT SET");
1337 				}
1338 			}
1339 			PRINTF(" XS_ERR=0x%x\n", (unsigned int) XS_ERR(xs));
1340 		}
1341 
1342 		ISP_WRITE(isp, INMAILBOX5, optr);
1343 		isp->isp_nactive--;
1344 		if (isp->isp_nactive < 0)
1345 			isp->isp_nactive = 0;
1346 		complist[ndone++] = xs;	/* defer completion call until later */
1347 	}
1348 	/*
1349 	 * If we completed any commands, then it's valid to find out
1350 	 * what the outpointer is.
1351 	 */
1352 	if (ndone) {
1353 	 	isp->isp_reqodx = ISP_READ(isp, OUTMAILBOX4);
1354 	}
1355 	isp->isp_residx = optr;
1356 	for (i = 0; i < ndone; i++) {
1357 		xs = complist[i];
1358 		if (xs) {
1359 			XS_CMD_DONE(xs);
1360 		}
1361 	}
1362 	return (1);
1363 }
1364 
1365 /*
1366  * Support routines.
1367  */
1368 
1369 static int
1370 isp_parse_async(isp, mbox)
1371 	struct ispsoftc *isp;
1372 	u_int16_t mbox;
1373 {
1374 	switch (mbox) {
1375 	case ASYNC_BUS_RESET:
1376 		PRINTF("%s: SCSI bus reset detected\n", isp->isp_name);
1377 		isp->isp_sendmarker = 1;
1378 		break;
1379 
1380 	case ASYNC_SYSTEM_ERROR:
1381 		mbox = ISP_READ(isp, OUTMAILBOX1);
1382 		PRINTF("%s: Internal FW Error @ RISC Addr 0x%x\n",
1383 		    isp->isp_name, mbox);
1384 		isp_restart(isp);
1385 		/* no point continuing after this */
1386 		return (1);
1387 
1388 	case ASYNC_RQS_XFER_ERR:
1389 		PRINTF("%s: Request Queue Transfer Error\n", isp->isp_name);
1390 		break;
1391 
1392 	case ASYNC_RSP_XFER_ERR:
1393 		PRINTF("%s: Response Queue Transfer Error\n", isp->isp_name);
1394 		break;
1395 
1396 	case ASYNC_QWAKEUP:
1397 		/* don't need to be chatty */
1398 		mbox = ISP_READ(isp, OUTMAILBOX4);
1399 		break;
1400 
1401 	case ASYNC_TIMEOUT_RESET:
1402 		PRINTF("%s: timeout initiated SCSI bus reset\n", isp->isp_name);
1403 		isp->isp_sendmarker = 1;
1404 		break;
1405 
1406 	case ASYNC_UNSPEC_TMODE:
1407 		PRINTF("%s: mystery async target completion\n", isp->isp_name);
1408 		break;
1409 
1410 	case ASYNC_EXTMSG_UNDERRUN:
1411 		PRINTF("%s: extended message underrun\n", isp->isp_name);
1412 		break;
1413 
1414 	case ASYNC_SCAM_INT:
1415 		PRINTF("%s: SCAM interrupt\n", isp->isp_name);
1416 		break;
1417 
1418 	case ASYNC_HUNG_SCSI:
1419 		PRINTF("%s: stalled SCSI Bus after DATA Overrun\n",
1420 		    isp->isp_name);
1421 		/* XXX: Need to issue SCSI reset at this point */
1422 		break;
1423 
1424 	case ASYNC_KILLED_BUS:
1425 		PRINTF("%s: SCSI Bus reset after DATA Overrun\n",
1426 		    isp->isp_name);
1427 		break;
1428 
1429 	case ASYNC_BUS_TRANSIT:
1430 		PRINTF("%s: LBD->HVD Transition 0x%x\n",
1431 		    isp->isp_name, ISP_READ(isp, OUTMAILBOX1));
1432 		break;
1433 
1434 	case ASYNC_CMD_CMPLT:
1435 		PRINTF("%s: fast post completion\n", isp->isp_name);
1436 #if	0
1437 		fast_post_handle = (ISP_READ(isp, OUTMAILBOX1) << 16) |
1438 		    ISP_READ(isp, OUTMAILBOX2);
1439 #endif
1440 		break;
1441 
1442 	case ASYNC_CTIO_DONE:
1443 		PRINTF("%s: CTIO done\n", isp->isp_name);
1444 		break;
1445 
1446 	case ASYNC_LIP_OCCURRED:
1447 		PRINTF("%s: LIP occurred\n", isp->isp_name);
1448 		break;
1449 
1450 	case ASYNC_LOOP_UP:
1451 		PRINTF("%s: Loop UP\n", isp->isp_name);
1452 		break;
1453 
1454 	case ASYNC_LOOP_DOWN:
1455 		PRINTF("%s: Loop DOWN\n", isp->isp_name);
1456 		break;
1457 
1458 	case ASYNC_LOOP_RESET:
1459 		PRINTF("%s: Loop RESET\n", isp->isp_name);
1460 		break;
1461 
1462 	case ASYNC_PDB_CHANGED:
1463 		PRINTF("%s: Port Database Changed\n", isp->isp_name);
1464 		break;
1465 
1466 	case ASYNC_CHANGE_NOTIFY:
1467 		PRINTF("%s: Name Server Database Changed\n", isp->isp_name);
1468 		break;
1469 
1470 	default:
1471 		PRINTF("%s: async %x\n", isp->isp_name, mbox);
1472 		break;
1473 	}
1474 	return (0);
1475 }
1476 
1477 static int
1478 isp_handle_other_response(isp, sp, optrp)
1479 	struct ispsoftc *isp;
1480 	ispstatusreq_t *sp;
1481 	u_int8_t *optrp;
1482 {
1483 	u_int8_t iptr, optr;
1484 	int reqsize = 0;
1485 	void *ireqp = NULL;
1486 
1487 	switch (sp->req_header.rqs_entry_type) {
1488 	case RQSTYPE_REQUEST:
1489 		return (-1);
1490 #if	defined(ISP2100_TARGET_MODE) || defined(ISP_TARGET_MODE)
1491 	case RQSTYPE_NOTIFY_ACK:
1492 	{
1493 		ispnotify_t *spx = (ispnotify_t *) sp;
1494 		PRINTF("%s: Immediate Notify Ack %d.%d Status 0x%x Sequence "
1495 		    "0x%x\n", isp->isp_name, spx->req_initiator, spx->req_lun,
1496 		    spx->req_status, spx->req_sequence);
1497 		break;
1498 	}
1499 	case RQSTYPE_NOTIFY:
1500 	{
1501 		ispnotify_t *spx = (ispnotify_t *) sp;
1502 
1503 		PRINTF("%s: Notify loopid %d to lun %d req_status 0x%x "
1504 		    "req_task_flags 0x%x seq 0x%x\n", isp->isp_name, 				    spx->req_initiator, spx->req_lun, spx->req_status,
1505 		    spx->req_task_flags, spx->req_sequence);
1506 		reqsize = sizeof (*spx);
1507 		spx->req_header.rqs_entry_type = RQSTYPE_NOTIFY_ACK;
1508 		spx->req_header.rqs_entry_count = 1;
1509 		spx->req_header.rqs_flags = 0;
1510 		spx->req_header.rqs_seqno = isp->isp_seqno++;
1511 		spx->req_handle = (spx->req_initiator<<16) | RQSTYPE_NOTIFY_ACK;
1512 		if (spx->req_status == IN_RSRC_UNAVAIL)
1513 			spx->req_flags = LUN_INCR_CMD;
1514 		else if (spx->req_status == IN_NOCAP)
1515 			spx->req_flags = LUN_INCR_IMMED;
1516 		else {
1517 			reqsize = 0;
1518 		}
1519 		ireqp = spx;
1520 		break;
1521 	}
1522 	case RQSTYPE_ENABLE_LUN:
1523 	{
1524 		isplun_t *ip = (isplun_t *) sp;
1525 		if (ip->req_status != 1) {
1526 		    PRINTF("%s: ENABLE LUN returned status 0x%x\n",
1527 			isp->isp_name, ip->req_status);
1528 		}
1529 		break;
1530 	}
1531 	case RQSTYPE_ATIO2:
1532 	{
1533 		fcparam *fcp = isp->isp_param;
1534 		ispctiot2_t local, *ct2 = NULL;
1535 		ispatiot2_t *at2 = (ispatiot2_t *) sp;
1536 		int s;
1537 
1538 		PRINTF("%s: atio2 loopid %d for lun %d rxid 0x%x flags 0x%x "
1539 		    "task flags 0x%x exec codes 0x%x\n", isp->isp_name,
1540 		    at2->req_initiator, at2->req_lun, at2->req_rxid,
1541 		    at2->req_flags, at2->req_taskflags, at2->req_execodes);
1542 
1543 		switch (at2->req_status & ~ATIO_SENSEVALID) {
1544 		case ATIO_PATH_INVALID:
1545 			PRINTF("%s: ATIO2 Path Invalid\n", isp->isp_name);
1546 			break;
1547 		case ATIO_NOCAP:
1548 			PRINTF("%s: ATIO2 No Cap\n", isp->isp_name);
1549 			break;
1550 		case ATIO_BDR_MSG:
1551 			PRINTF("%s: ATIO2 BDR Received\n", isp->isp_name);
1552 			break;
1553 		case ATIO_CDB_RECEIVED:
1554 			ct2 = &local;
1555 			break;
1556 		default:
1557 			PRINTF("%s: unknown req_status 0x%x\n", isp->isp_name,
1558 			    at2->req_status);
1559 			break;
1560 		}
1561 		if (ct2 == NULL) {
1562 			/*
1563 			 * Just do an ACCEPT on this fellow.
1564 			 */
1565 			at2->req_header.rqs_entry_type = RQSTYPE_ATIO2;
1566 			at2->req_header.rqs_flags = 0;
1567 			at2->req_flags = 1;
1568 			ireqp = at2;
1569 			reqsize = sizeof (*at2);
1570 			break;
1571 		}
1572 		PRINTF("%s: datalen %d cdb0=0x%x\n", isp->isp_name,
1573 		    at2->req_datalen, at2->req_cdb[0]);
1574 		bzero ((void *) ct2, sizeof (*ct2));
1575 		ct2->req_header.rqs_entry_type = RQSTYPE_CTIO2;
1576 		ct2->req_header.rqs_entry_count = 1;
1577 		ct2->req_header.rqs_flags = 0;
1578 		ct2->req_header.rqs_seqno = isp->isp_seqno++;
1579 		ct2->req_handle = (at2->req_initiator << 16) | at2->req_lun;
1580 		ct2->req_lun = at2->req_lun;
1581 		ct2->req_initiator = at2->req_initiator;
1582 		ct2->req_rxid = at2->req_rxid;
1583 
1584 		ct2->req_flags = CTIO_SEND_STATUS;
1585 		switch (at2->req_cdb[0]) {
1586 		case 0x0:		/* TUR */
1587 			ct2->req_flags |= CTIO_NODATA | CTIO2_SMODE0;
1588 			ct2->req_m.mode0.req_scsi_status = CTIO2_STATUS_VALID;
1589 			break;
1590 
1591 		case 0x3:		/* REQUEST SENSE */
1592 		case 0x12:		/* INQUIRE */
1593 			ct2->req_flags |= CTIO_SEND_DATA | CTIO2_SMODE0;
1594 			ct2->req_m.mode0.req_scsi_status = CTIO2_STATUS_VALID;
1595 			ct2->req_seg_count = 1;
1596 			if (at2->req_cdb[0] == 0x12) {
1597 				s = sizeof(tgtiqd);
1598 				bcopy((void *)tgtiqd, fcp->isp_scratch, s);
1599 			} else {
1600 				s = at2->req_datalen;
1601 				bzero(fcp->isp_scratch, s);
1602 			}
1603 			ct2->req_m.mode0.req_dataseg[0].ds_base =
1604 			    fcp->isp_scdma;
1605 			ct2->req_m.mode0.req_dataseg[0].ds_count = s;
1606 			ct2->req_m.mode0.req_datalen = s;
1607 #if	0
1608 			if (at2->req_datalen < s) {
1609 				ct2->req_m.mode1.req_scsi_status |=
1610 				    CTIO2_RESP_VALID|CTIO2_RSPOVERUN;
1611 			} else if (at2->req_datalen > s) {
1612 				ct2->req_m.mode1.req_scsi_status |=
1613 				    CTIO2_RESP_VALID|CTIO2_RSPUNDERUN;
1614 			}
1615 #endif
1616 			break;
1617 
1618 		default:		/* ALL OTHERS */
1619 			ct2->req_flags |= CTIO_NODATA | CTIO2_SMODE1;
1620 			ct2->req_m.mode1.req_scsi_status = 0;
1621 #if	0
1622 			if (at2->req_datalen) {
1623 				ct2->req_m.mode1.req_scsi_status |=
1624 				    CTIO2_RSPUNDERUN;
1625 #if	BYTE_ORDER == BIG_ENDIAN
1626 				ct2->req_resid[1] = at2->req_datalen & 0xff;
1627 				ct2->req_resid[0] =
1628 					(at2->req_datalen >> 8) & 0xff;
1629 				ct2->req_resid[3] =
1630 					(at2->req_datalen >> 16) & 0xff;
1631 				ct2->req_resid[2] =
1632 					(at2->req_datalen >> 24) & 0xff;
1633 #else
1634 				ct2->req_resid[0] = at2->req_datalen & 0xff;
1635 				ct2->req_resid[1] =
1636 					(at2->req_datalen >> 8) & 0xff;
1637 				ct2->req_resid[2] =
1638 					(at2->req_datalen >> 16) & 0xff;
1639 				ct2->req_resid[3] =
1640 					(at2->req_datalen >> 24) & 0xff;
1641 #endif
1642 			}
1643 #endif
1644 			if ((at2->req_status & ATIO_SENSEVALID) == 0) {
1645 				ct2->req_m.mode1.req_sense_len = 18;
1646 				ct2->req_m.mode1.req_scsi_status |= 2;
1647 				ct2->req_m.mode1.req_response[0] = 0x70;
1648 				ct2->req_m.mode1.req_response[2] = 0x2;
1649 			} else {
1650 				ct2->req_m.mode1.req_sense_len = 18;
1651 				ct2->req_m.mode1.req_scsi_status |=
1652 				    at2->req_scsi_status;
1653 				bcopy((void *)at2->req_sense,
1654 				    (void *)ct2->req_m.mode1.req_response,
1655 				    sizeof (at2->req_sense));
1656 			}
1657 			break;
1658 		}
1659 		reqsize = sizeof (*ct2);
1660 		ireqp = ct2;
1661 		break;
1662 	}
1663 	case RQSTYPE_CTIO2:
1664 	{
1665 		ispatiot2_t *at2;
1666 		ispctiot2_t *ct2 = (ispctiot2_t *) sp;
1667 		PRINTF("%s: CTIO2 returned status 0x%x\n", isp->isp_name,
1668 		    ct2->req_status);
1669 		/*
1670 	 	 * Return the ATIO to the board.
1671 		 */
1672 		at2 = (ispatiot2_t *) sp;
1673 		at2->req_header.rqs_entry_type = RQSTYPE_ATIO2;
1674 		at2->req_header.rqs_entry_count = 1;
1675 		at2->req_header.rqs_flags = 0;
1676 		at2->req_header.rqs_seqno = isp->isp_seqno++;
1677 		at2->req_status = 1;
1678 		reqsize = sizeof (*at2);
1679 		ireqp = at2;
1680 		break;
1681 	}
1682 #endif
1683 	default:
1684 		PRINTF("%s: other response type %x\n", isp->isp_name,
1685 		    sp->req_header.rqs_entry_type);
1686 		break;
1687 	}
1688 	if (reqsize) {
1689 		void *reqp;
1690 		optr = isp->isp_reqodx = ISP_READ(isp, OUTMAILBOX4);
1691 		iptr = isp->isp_reqidx;
1692 		reqp = (void *) ISP_QUEUE_ENTRY(isp->isp_rquest, iptr);
1693 		iptr = ISP_NXT_QENTRY(iptr, RQUEST_QUEUE_LEN);
1694 		if (iptr == optr) {
1695 			PRINTF("%s: Request Queue Overflow other response\n",
1696 			    isp->isp_name);
1697 		} else {
1698 			bcopy(ireqp, reqp, reqsize);
1699 			ISP_WRITE(isp, INMAILBOX4, iptr);
1700 			isp->isp_reqidx = iptr;
1701 		}
1702 	}
1703 	return (0);
1704 }
1705 
1706 #if	defined(ISP2100_TARGET_MODE) || defined(ISP_TARGET_MODE)
1707 /*
1708  * Locks held, and ints disabled (if FC).
1709  *
1710  * XXX: SETUP ONLY FOR INITIAL ENABLING RIGHT NOW
1711  */
1712 static int
1713 isp_modify_lun(isp, lun, icnt, ccnt)
1714 	struct ispsoftc *isp;
1715 	int lun;	/* logical unit to enable, modify, or disable */
1716 	int icnt;	/* immediate notify count */
1717 	int ccnt;	/* command count */
1718 {
1719 	isplun_t *ip = NULL;
1720 	u_int8_t iptr, optr;
1721 
1722 	optr = isp->isp_reqodx = ISP_READ(isp, OUTMAILBOX4);
1723 	iptr = isp->isp_reqidx;
1724 	ip = (isplun_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, iptr);
1725 	iptr = ISP_NXT_QENTRY(iptr, RQUEST_QUEUE_LEN);
1726 	if (iptr == optr) {
1727 		PRINTF("%s: Request Queue Overflow in isp_modify_lun\n",
1728 		    isp->isp_name);
1729 		return (-1);
1730 	}
1731 
1732 	bzero((void *) ip, sizeof (*ip));
1733 	ip->req_header.rqs_entry_type = RQSTYPE_ENABLE_LUN;
1734 	ip->req_header.rqs_entry_count = 1;
1735 	ip->req_header.rqs_flags = 0;
1736 	ip->req_header.rqs_seqno = isp->isp_seqno++;
1737 	ip->req_handle = RQSTYPE_ENABLE_LUN;
1738 	ip->req_lun = lun;
1739 	ip->req_cmdcount = ccnt;
1740 	ip->req_imcount = icnt;
1741 	ip->req_timeout = 0;	/* default 30 seconds */
1742 	ISP_WRITE(isp, INMAILBOX4, iptr);
1743 	isp->isp_reqidx = iptr;
1744 	return (0);
1745 }
1746 #endif
1747 
1748 static void
1749 isp_parse_status(isp, sp, xs)
1750 	struct ispsoftc *isp;
1751 	ispstatusreq_t *sp;
1752 	ISP_SCSI_XFER_T *xs;
1753 {
1754 	switch (sp->req_completion_status) {
1755 	case RQCS_COMPLETE:
1756 		XS_SETERR(xs, HBA_NOERROR);
1757 		return;
1758 
1759 	case RQCS_INCOMPLETE:
1760 		if ((sp->req_state_flags & RQSF_GOT_TARGET) == 0) {
1761 			IDPRINTF(3, ("%s: Selection Timeout for target %d\n",
1762 			    isp->isp_name, XS_TGT(xs)));
1763 			XS_SETERR(xs, HBA_SELTIMEOUT);
1764 			return;
1765 		}
1766 		PRINTF("%s: command incomplete for target %d lun %d, state "
1767 		    "0x%x\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs),
1768 		    sp->req_state_flags);
1769 		break;
1770 
1771 	case RQCS_DMA_ERROR:
1772 		PRINTF("%s: DMA error for command on target %d, lun %d\n",
1773 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1774 		break;
1775 
1776 	case RQCS_TRANSPORT_ERROR:
1777 		PRINTF("%s: transport error\n", isp->isp_name);
1778 		isp_prtstst(sp);
1779 		break;
1780 
1781 	case RQCS_RESET_OCCURRED:
1782 		IDPRINTF(2, ("%s: bus reset destroyed command for target %d "
1783 		    "lun %d\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs)));
1784 		isp->isp_sendmarker = 1;
1785 		XS_SETERR(xs, HBA_BUSRESET);
1786 		return;
1787 
1788 	case RQCS_ABORTED:
1789 		PRINTF("%s: command aborted for target %d lun %d\n",
1790 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1791 		isp->isp_sendmarker = 1;
1792 		XS_SETERR(xs, HBA_ABORTED);
1793 		return;
1794 
1795 	case RQCS_TIMEOUT:
1796 		IDPRINTF(2, ("%s: command timed out for target %d lun %d\n",
1797 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs)));
1798 		XS_SETERR(xs, HBA_CMDTIMEOUT);
1799 		return;
1800 
1801 	case RQCS_DATA_OVERRUN:
1802 		if (isp->isp_type & ISP_HA_FC) {
1803 			XS_RESID(xs) = sp->req_resid;
1804 			break;
1805 		}
1806 		XS_SETERR(xs, HBA_DATAOVR);
1807 		return;
1808 
1809 	case RQCS_COMMAND_OVERRUN:
1810 		PRINTF("%s: command overrun for command on target %d, lun %d\n",
1811 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1812 		break;
1813 
1814 	case RQCS_STATUS_OVERRUN:
1815 		PRINTF("%s: status overrun for command on target %d, lun %d\n",
1816 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1817 		break;
1818 
1819 	case RQCS_BAD_MESSAGE:
1820 		PRINTF("%s: message not COMMAND COMPLETE after status on "
1821 		    "target %d, lun %d\n", isp->isp_name, XS_TGT(xs),
1822 		    XS_LUN(xs));
1823 		break;
1824 
1825 	case RQCS_NO_MESSAGE_OUT:
1826 		PRINTF("%s: No MESSAGE OUT phase after selection on "
1827 		    "target %d, lun %d\n", isp->isp_name, XS_TGT(xs),
1828 		    XS_LUN(xs));
1829 		break;
1830 
1831 	case RQCS_EXT_ID_FAILED:
1832 		PRINTF("%s: EXTENDED IDENTIFY failed on target %d, lun %d\n",
1833 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1834 		break;
1835 
1836 	case RQCS_IDE_MSG_FAILED:
1837 		PRINTF("%s: target %d lun %d rejected INITIATOR DETECTED "
1838 		    "ERROR message\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1839 		break;
1840 
1841 	case RQCS_ABORT_MSG_FAILED:
1842 		PRINTF("%s: target %d lun %d rejected ABORT message\n",
1843 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1844 		break;
1845 
1846 	case RQCS_REJECT_MSG_FAILED:
1847 		PRINTF("%s: target %d lun %d rejected MESSAGE REJECT message\n",
1848 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1849 		break;
1850 
1851 	case RQCS_NOP_MSG_FAILED:
1852 		PRINTF("%s: target %d lun %d rejected NOP message\n",
1853 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1854 		break;
1855 
1856 	case RQCS_PARITY_ERROR_MSG_FAILED:
1857 		PRINTF("%s: target %d lun %d rejected MESSAGE PARITY ERROR "
1858 		    "message\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1859 		break;
1860 
1861 	case RQCS_DEVICE_RESET_MSG_FAILED:
1862 		PRINTF("%s: target %d lun %d rejected BUS DEVICE RESET "
1863 		    "message\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1864 		break;
1865 
1866 	case RQCS_ID_MSG_FAILED:
1867 		PRINTF("%s: target %d lun %d rejected IDENTIFY "
1868 		    "message\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1869 		break;
1870 
1871 	case RQCS_UNEXP_BUS_FREE:
1872 		PRINTF("%s: target %d lun %d had unexeptected bus free\n",
1873 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1874 		break;
1875 
1876 	case RQCS_DATA_UNDERRUN:
1877 		if (isp->isp_type & ISP_HA_FC) {
1878 			XS_RESID(xs) = sp->req_resid;
1879 			/* an UNDERRUN is not a botch ??? */
1880 		}
1881 		XS_SETERR(xs, HBA_NOERROR);
1882 		return;
1883 
1884 	case RQCS_XACT_ERR1:
1885 		PRINTF("%s: HBA attempted queued transaction with disconnect "
1886 		    "not set for target %d lun %d\n", isp->isp_name, XS_TGT(xs),
1887 		    XS_LUN(xs));
1888 		break;
1889 
1890 	case RQCS_XACT_ERR2:
1891 		PRINTF("%s: HBA attempted queued transaction to target "
1892 		    "routine %d on target %d\n", isp->isp_name, XS_LUN(xs),
1893 		    XS_TGT(xs));
1894 		break;
1895 
1896 	case RQCS_XACT_ERR3:
1897 		PRINTF("%s: HBA attempted queued transaction for target %d lun "
1898 		    "%d when queueing disabled\n", isp->isp_name, XS_TGT(xs),
1899 		    XS_LUN(xs));
1900 		break;
1901 
1902 	case RQCS_BAD_ENTRY:
1903 		PRINTF("%s: invalid IOCB entry type detected\n", isp->isp_name);
1904 		break;
1905 
1906 	case RQCS_QUEUE_FULL:
1907 		PRINTF("%s: internal queues full for target %d lun %d\n",
1908 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1909 		break;
1910 
1911 	case RQCS_PHASE_SKIPPED:
1912 		PRINTF("%s: SCSI phase skipped (e.g., COMMAND COMPLETE w/o "
1913 		    "STATUS phase) for target %d lun %d\n", isp->isp_name,
1914 		    XS_TGT(xs), XS_LUN(xs));
1915 		break;
1916 
1917 	case RQCS_ARQS_FAILED:
1918 		PRINTF("%s: Auto Request Sense failed for target %d lun %d\n",
1919 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1920 		XS_SETERR(xs, HBA_ARQFAIL);
1921 		return;
1922 
1923 	case RQCS_WIDE_FAILED:
1924 		PRINTF("%s: Wide Negotiation failed for target %d lun %d\n",
1925 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1926 		if (isp->isp_type & ISP_HA_SCSI) {
1927 			sdparam *sdp = isp->isp_param;
1928 			isp->isp_update = 1;
1929 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
1930 			sdp->isp_devparam[XS_TGT(xs)].dev_flags &= ~DPARM_WIDE;
1931 		}
1932 		XS_SETERR(xs, HBA_NOERROR);
1933 		return;
1934 
1935 	case RQCS_SYNCXFER_FAILED:
1936 		PRINTF("%s: SDTR Message failed for target %d lun %d\n",
1937 		    isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1938 		if (isp->isp_type & ISP_HA_SCSI) {
1939 			sdparam *sdp = isp->isp_param;
1940 			isp->isp_update = 1;
1941 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
1942 			sdp->isp_devparam[XS_TGT(xs)].dev_flags &= ~DPARM_SYNC;
1943 		}
1944 		break;
1945 
1946 	case RQCS_LVD_BUSERR:
1947 		PRINTF("%s: Bad LVD Bus condition while talking to target %d "
1948 		    "lun %d\n", isp->isp_name, XS_TGT(xs), XS_LUN(xs));
1949 		break;
1950 
1951 	case RQCS_PORT_UNAVAILABLE:
1952 		/*
1953 		 * No such port on the loop. Moral equivalent of SELTIMEO
1954 		 */
1955 		IDPRINTF(3, ("%s: Port Unavailable for target %d\n",
1956 		    isp->isp_name, XS_TGT(xs)));
1957 		XS_SETERR(xs, HBA_SELTIMEOUT);
1958 		return;
1959 
1960 	case RQCS_PORT_LOGGED_OUT:
1961 		/*
1962 		 * It was there (maybe)- treat as a selection timeout.
1963 		 */
1964 		PRINTF("%s: port logout for target %d\n",
1965 			isp->isp_name, XS_TGT(xs));
1966 		XS_SETERR(xs, HBA_SELTIMEOUT);
1967 		return;
1968 
1969 	case RQCS_PORT_CHANGED:
1970 		PRINTF("%s: port changed for target %d\n",
1971 			isp->isp_name, XS_TGT(xs));
1972 		break;
1973 
1974 	case RQCS_PORT_BUSY:
1975 		PRINTF("%s: port busy for target %d\n",
1976 			isp->isp_name, XS_TGT(xs));
1977 		XS_SETERR(xs, HBA_TGTBSY);
1978 		return;
1979 
1980 	default:
1981 		PRINTF("%s: comp status %x\n", isp->isp_name,
1982 		       sp->req_completion_status);
1983 		break;
1984 	}
1985 	XS_SETERR(xs, HBA_BOTCH);
1986 }
1987 
1988 #define	HINIB(x)			((x) >> 0x4)
1989 #define	LONIB(x)			((x)  & 0xf)
1990 #define MAKNIB(a, b)			(((a) << 4) | (b))
1991 static u_int8_t mbpcnt[] = {
1992 	MAKNIB(1, 1),	/* 0x00: MBOX_NO_OP */
1993 	MAKNIB(5, 5),	/* 0x01: MBOX_LOAD_RAM */
1994 	MAKNIB(2, 0),	/* 0x02: MBOX_EXEC_FIRMWARE */
1995 	MAKNIB(5, 5),	/* 0x03: MBOX_DUMP_RAM */
1996 	MAKNIB(3, 3),	/* 0x04: MBOX_WRITE_RAM_WORD */
1997 	MAKNIB(2, 3),	/* 0x05: MBOX_READ_RAM_WORD */
1998 	MAKNIB(6, 6),	/* 0x06: MBOX_MAILBOX_REG_TEST */
1999 	MAKNIB(2, 3),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
2000 	MAKNIB(1, 3),	/* 0x08: MBOX_ABOUT_FIRMWARE */
2001 	MAKNIB(0, 0),	/* 0x09: */
2002 	MAKNIB(0, 0),	/* 0x0a: */
2003 	MAKNIB(0, 0),	/* 0x0b: */
2004 	MAKNIB(0, 0),	/* 0x0c: */
2005 	MAKNIB(0, 0),	/* 0x0d: */
2006 	MAKNIB(1, 2),	/* 0x0e: MBOX_CHECK_FIRMWARE */
2007 	MAKNIB(0, 0),	/* 0x0f: */
2008 	MAKNIB(5, 5),	/* 0x10: MBOX_INIT_REQ_QUEUE */
2009 	MAKNIB(6, 6),	/* 0x11: MBOX_INIT_RES_QUEUE */
2010 	MAKNIB(4, 4),	/* 0x12: MBOX_EXECUTE_IOCB */
2011 	MAKNIB(2, 2),	/* 0x13: MBOX_WAKE_UP	*/
2012 	MAKNIB(1, 6),	/* 0x14: MBOX_STOP_FIRMWARE */
2013 	MAKNIB(4, 4),	/* 0x15: MBOX_ABORT */
2014 	MAKNIB(2, 2),	/* 0x16: MBOX_ABORT_DEVICE */
2015 	MAKNIB(3, 3),	/* 0x17: MBOX_ABORT_TARGET */
2016 	MAKNIB(2, 2),	/* 0x18: MBOX_BUS_RESET */
2017 	MAKNIB(2, 3),	/* 0x19: MBOX_STOP_QUEUE */
2018 	MAKNIB(2, 3),	/* 0x1a: MBOX_START_QUEUE */
2019 	MAKNIB(2, 3),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
2020 	MAKNIB(2, 3),	/* 0x1c: MBOX_ABORT_QUEUE */
2021 	MAKNIB(2, 4),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
2022 	MAKNIB(0, 0),	/* 0x1e: */
2023 	MAKNIB(1, 3),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
2024 	MAKNIB(1, 3),	/* 0x20: MBOX_GET_INIT_SCSI_ID, MBOX_GET_LOOP_ID */
2025 	MAKNIB(1, 2),	/* 0x21: MBOX_GET_SELECT_TIMEOUT */
2026 	MAKNIB(1, 3),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
2027 	MAKNIB(1, 2),	/* 0x23: MBOX_GET_TAG_AGE_LIMIT */
2028 	MAKNIB(1, 2),	/* 0x24: MBOX_GET_CLOCK_RATE */
2029 	MAKNIB(1, 2),	/* 0x25: MBOX_GET_ACT_NEG_STATE */
2030 	MAKNIB(1, 2),	/* 0x26: MBOX_GET_ASYNC_DATA_SETUP_TIME */
2031 	MAKNIB(1, 3),	/* 0x27: MBOX_GET_PCI_PARAMS */
2032 	MAKNIB(2, 4),	/* 0x28: MBOX_GET_TARGET_PARAMS */
2033 	MAKNIB(2, 4),	/* 0x29: MBOX_GET_DEV_QUEUE_PARAMS */
2034 	MAKNIB(0, 0),	/* 0x2a: */
2035 	MAKNIB(0, 0),	/* 0x2b: */
2036 	MAKNIB(0, 0),	/* 0x2c: */
2037 	MAKNIB(0, 0),	/* 0x2d: */
2038 	MAKNIB(0, 0),	/* 0x2e: */
2039 	MAKNIB(0, 0),	/* 0x2f: */
2040 	MAKNIB(2, 2),	/* 0x30: MBOX_SET_INIT_SCSI_ID */
2041 	MAKNIB(2, 2),	/* 0x31: MBOX_SET_SELECT_TIMEOUT */
2042 	MAKNIB(3, 3),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
2043 	MAKNIB(2, 2),	/* 0x33: MBOX_SET_TAG_AGE_LIMIT */
2044 	MAKNIB(2, 2),	/* 0x34: MBOX_SET_CLOCK_RATE */
2045 	MAKNIB(2, 2),	/* 0x35: MBOX_SET_ACTIVE_NEG_STATE */
2046 	MAKNIB(2, 2),	/* 0x36: MBOX_SET_ASYNC_DATA_SETUP_TIME */
2047 	MAKNIB(3, 3),	/* 0x37: MBOX_SET_PCI_CONTROL_PARAMS */
2048 	MAKNIB(4, 4),	/* 0x38: MBOX_SET_TARGET_PARAMS */
2049 	MAKNIB(4, 4),	/* 0x39: MBOX_SET_DEV_QUEUE_PARAMS */
2050 	MAKNIB(0, 0),	/* 0x3a: */
2051 	MAKNIB(0, 0),	/* 0x3b: */
2052 	MAKNIB(0, 0),	/* 0x3c: */
2053 	MAKNIB(0, 0),	/* 0x3d: */
2054 	MAKNIB(0, 0),	/* 0x3e: */
2055 	MAKNIB(0, 0),	/* 0x3f: */
2056 	MAKNIB(1, 2),	/* 0x40: MBOX_RETURN_BIOS_BLOCK_ADDR */
2057 	MAKNIB(6, 1),	/* 0x41: MBOX_WRITE_FOUR_RAM_WORDS */
2058 	MAKNIB(2, 3),	/* 0x42: MBOX_EXEC_BIOS_IOCB */
2059 	MAKNIB(0, 0),	/* 0x43: */
2060 	MAKNIB(0, 0),	/* 0x44: */
2061 	MAKNIB(0, 0),	/* 0x45: */
2062 	MAKNIB(0, 0),	/* 0x46: */
2063 	MAKNIB(0, 0),	/* 0x47: */
2064 	MAKNIB(0, 0),	/* 0x48: */
2065 	MAKNIB(0, 0),	/* 0x49: */
2066 	MAKNIB(0, 0),	/* 0x4a: */
2067 	MAKNIB(0, 0),	/* 0x4b: */
2068 	MAKNIB(0, 0),	/* 0x4c: */
2069 	MAKNIB(0, 0),	/* 0x4d: */
2070 	MAKNIB(0, 0),	/* 0x4e: */
2071 	MAKNIB(0, 0),	/* 0x4f: */
2072 	MAKNIB(0, 0),	/* 0x50: */
2073 	MAKNIB(0, 0),	/* 0x51: */
2074 	MAKNIB(0, 0),	/* 0x52: */
2075 	MAKNIB(0, 0),	/* 0x53: */
2076 	MAKNIB(8, 0),	/* 0x54: MBOX_EXEC_COMMAND_IOCB_A64 */
2077 	MAKNIB(0, 0),	/* 0x55: */
2078 	MAKNIB(0, 0),	/* 0x56: */
2079 	MAKNIB(0, 0),	/* 0x57: */
2080 	MAKNIB(0, 0),	/* 0x58: */
2081 	MAKNIB(0, 0),	/* 0x59: */
2082 	MAKNIB(0, 0),	/* 0x5a: */
2083 	MAKNIB(0, 0),	/* 0x5b: */
2084 	MAKNIB(0, 0),	/* 0x5c: */
2085 	MAKNIB(0, 0),	/* 0x5d: */
2086 	MAKNIB(0, 0),	/* 0x5e: */
2087 	MAKNIB(0, 0),	/* 0x5f: */
2088 	MAKNIB(8, 6),	/* 0x60: MBOX_INIT_FIRMWARE */
2089 	MAKNIB(0, 0),	/* 0x60: MBOX_GET_INIT_CONTROL_BLOCK  (FORMAT?) */
2090 	MAKNIB(2, 1),	/* 0x62: MBOX_INIT_LIP */
2091 	MAKNIB(8, 1),	/* 0x63: MBOX_GET_FC_AL_POSITION_MAP */
2092 	MAKNIB(8, 1),	/* 0x64: MBOX_GET_PORT_DB */
2093 	MAKNIB(3, 1),	/* 0x65: MBOX_CLEAR_ACA */
2094 	MAKNIB(3, 1),	/* 0x66: MBOX_TARGET_RESET */
2095 	MAKNIB(3, 1),	/* 0x67: MBOX_CLEAR_TASK_SET */
2096 	MAKNIB(3, 1),	/* 0x69: MBOX_ABORT_TASK_SET */
2097 	MAKNIB(1, 2)	/* 0x69: MBOX_GET_FW_STATE */
2098 };
2099 #define	NMBCOM	(sizeof (mbpcnt) / sizeof (mbpcnt[0]))
2100 
2101 static void
2102 isp_mboxcmd(isp, mbp)
2103 	struct ispsoftc *isp;
2104 	mbreg_t *mbp;
2105 {
2106 	int outparam, inparam;
2107 	int loops, dld = 0;
2108 	u_int8_t opcode;
2109 
2110 	if (mbp->param[0] == ISP2100_SET_PCI_PARAM) {
2111 		opcode = mbp->param[0] = MBOX_SET_PCI_PARAMETERS;
2112 		inparam = 4;
2113 		outparam = 4;
2114 		goto command_known;
2115 	} else if (mbp->param[0] > NMBCOM) {
2116 		PRINTF("%s: bad command %x\n", isp->isp_name, mbp->param[0]);
2117 		return;
2118 	}
2119 
2120 	opcode = mbp->param[0];
2121 	inparam = HINIB(mbpcnt[mbp->param[0]]);
2122 	outparam =  LONIB(mbpcnt[mbp->param[0]]);
2123 
2124 	if (inparam == 0 && outparam == 0) {
2125 		PRINTF("%s: no parameters for %x\n", isp->isp_name,
2126 			mbp->param[0]);
2127 		return;
2128 	}
2129 
2130 
2131 command_known:
2132 
2133 	/*
2134 	 * Make sure we can send some words..
2135 	 */
2136 
2137 	loops = MBOX_DELAY_COUNT;
2138 	while ((ISP_READ(isp, HCCR) & HCCR_HOST_INT) != 0) {
2139 		SYS_DELAY(100);
2140 		if (--loops < 0) {
2141 			PRINTF("%s: isp_mboxcmd timeout #1\n", isp->isp_name);
2142 			if (dld++) {
2143 				return;
2144 			}
2145 			PRINTF("%s: but we'll try again, isr=%x\n",
2146 			    isp->isp_name, ISP_READ(isp, BIU_ISR));
2147 			if (ISP_READ(isp, BIU_SEMA) & 1) {
2148 				u_int16_t mbox = ISP_READ(isp, OUTMAILBOX0);
2149 				if (isp_parse_async(isp, mbox))
2150 					return;
2151 				ISP_WRITE(isp, BIU_SEMA, 0);
2152 			}
2153 			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
2154 			goto command_known;
2155 		}
2156 	}
2157 
2158 	/*
2159 	 * Write input parameters
2160 	 */
2161 	switch (inparam) {
2162 	case 8: ISP_WRITE(isp, INMAILBOX7, mbp->param[7]); mbp->param[7] = 0;
2163 	case 7: ISP_WRITE(isp, INMAILBOX6, mbp->param[6]); mbp->param[6] = 0;
2164 	case 6: ISP_WRITE(isp, INMAILBOX5, mbp->param[5]); mbp->param[5] = 0;
2165 	case 5: ISP_WRITE(isp, INMAILBOX4, mbp->param[4]); mbp->param[4] = 0;
2166 	case 4: ISP_WRITE(isp, INMAILBOX3, mbp->param[3]); mbp->param[3] = 0;
2167 	case 3: ISP_WRITE(isp, INMAILBOX2, mbp->param[2]); mbp->param[2] = 0;
2168 	case 2: ISP_WRITE(isp, INMAILBOX1, mbp->param[1]); mbp->param[1] = 0;
2169 	case 1: ISP_WRITE(isp, INMAILBOX0, mbp->param[0]); mbp->param[0] = 0;
2170 	}
2171 
2172 	/*
2173 	 * Clear semaphore on mailbox registers
2174 	 */
2175 	ISP_WRITE(isp, BIU_SEMA, 0);
2176 
2177 	/*
2178 	 * Clear RISC int condition.
2179 	 */
2180 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
2181 
2182 	/*
2183 	 * Set Host Interrupt condition so that RISC will pick up mailbox regs.
2184 	 */
2185 	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
2186 
2187 	/*
2188 	 * Wait until RISC int is set, except 2100
2189 	 */
2190 	if ((isp->isp_type & ISP_HA_FC) == 0) {
2191 		loops = MBOX_DELAY_COUNT;
2192 		while ((ISP_READ(isp, BIU_ISR) & BIU_ISR_RISC_INT) == 0) {
2193 			SYS_DELAY(100);
2194 			if (--loops < 0) {
2195 				PRINTF("%s: isp_mboxcmd timeout #2\n",
2196 				    isp->isp_name);
2197 				return;
2198 			}
2199 		}
2200 	}
2201 
2202 	/*
2203 	 * Check to make sure that the semaphore has been set.
2204 	 */
2205 	loops = MBOX_DELAY_COUNT;
2206 	while ((ISP_READ(isp, BIU_SEMA) & 1) == 0) {
2207 		SYS_DELAY(100);
2208 		if (--loops < 0) {
2209 			PRINTF("%s: isp_mboxcmd timeout #3\n", isp->isp_name);
2210 			return;
2211 		}
2212 	}
2213 
2214 	/*
2215 	 * Make sure that the MBOX_BUSY has gone away
2216 	 */
2217 	loops = MBOX_DELAY_COUNT;
2218 	while (ISP_READ(isp, OUTMAILBOX0) == MBOX_BUSY) {
2219 		SYS_DELAY(100);
2220 		if (--loops < 0) {
2221 			PRINTF("%s: isp_mboxcmd timeout #4\n", isp->isp_name);
2222 			return;
2223 		}
2224 	}
2225 
2226 
2227 	/*
2228 	 * Pick up output parameters.
2229 	 */
2230 	switch (outparam) {
2231 	case 8: mbp->param[7] = ISP_READ(isp, OUTMAILBOX7);
2232 	case 7: mbp->param[6] = ISP_READ(isp, OUTMAILBOX6);
2233 	case 6: mbp->param[5] = ISP_READ(isp, OUTMAILBOX5);
2234 	case 5: mbp->param[4] = ISP_READ(isp, OUTMAILBOX4);
2235 	case 4: mbp->param[3] = ISP_READ(isp, OUTMAILBOX3);
2236 	case 3: mbp->param[2] = ISP_READ(isp, OUTMAILBOX2);
2237 	case 2: mbp->param[1] = ISP_READ(isp, OUTMAILBOX1);
2238 	case 1: mbp->param[0] = ISP_READ(isp, OUTMAILBOX0);
2239 	}
2240 
2241 	/*
2242 	 * Clear RISC int.
2243 	 */
2244 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
2245 
2246 	/*
2247 	 * Release semaphore on mailbox registers
2248 	 */
2249 	ISP_WRITE(isp, BIU_SEMA, 0);
2250 
2251 	/*
2252 	 * Just to be chatty here...
2253 	 */
2254 	switch(mbp->param[0]) {
2255 	case MBOX_COMMAND_COMPLETE:
2256 		break;
2257 	case MBOX_INVALID_COMMAND:
2258 		IDPRINTF(2, ("%s: mbox cmd %x failed with INVALID_COMMAND\n",
2259 		    isp->isp_name, opcode));
2260 		break;
2261 	case MBOX_HOST_INTERFACE_ERROR:
2262 		PRINTF("%s: mbox cmd %x failed with HOST_INTERFACE_ERROR\n",
2263 		    isp->isp_name, opcode);
2264 		break;
2265 	case MBOX_TEST_FAILED:
2266 		PRINTF("%s: mbox cmd %x failed with TEST_FAILED\n",
2267 		    isp->isp_name, opcode);
2268 		break;
2269 	case MBOX_COMMAND_ERROR:
2270 		PRINTF("%s: mbox cmd %x failed with COMMAND_ERROR\n",
2271 		    isp->isp_name, opcode);
2272 		break;
2273 	case MBOX_COMMAND_PARAM_ERROR:
2274 		PRINTF("%s: mbox cmd %x failed with COMMAND_PARAM_ERROR\n",
2275 		    isp->isp_name, opcode);
2276 		break;
2277 
2278 	case ASYNC_LOOP_UP:
2279 	case ASYNC_LIP_OCCURRED:
2280 		break;
2281 
2282 	default:
2283 		/*
2284 		 * The expected return of EXEC_FIRMWARE is zero.
2285 		 */
2286 		if ((opcode == MBOX_EXEC_FIRMWARE && mbp->param[0] != 0) ||
2287 		    (opcode != MBOX_EXEC_FIRMWARE)) {
2288 			PRINTF("%s: mbox cmd %x failed with error %x\n",
2289 				isp->isp_name, opcode, mbp->param[0]);
2290 		}
2291 		break;
2292 	}
2293 }
2294 
2295 void
2296 isp_lostcmd(isp, xs)
2297 	struct ispsoftc *isp;
2298 	ISP_SCSI_XFER_T *xs;
2299 {
2300 	mbreg_t mbs;
2301 
2302 	mbs.param[0] = MBOX_GET_FIRMWARE_STATUS;
2303 	isp_mboxcmd(isp, &mbs);
2304 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2305 		isp_dumpregs(isp, "couldn't GET FIRMWARE STATUS");
2306 		return;
2307 	}
2308 	if (mbs.param[1]) {
2309 		PRINTF("%s: %d commands on completion queue\n",
2310 		       isp->isp_name, mbs.param[1]);
2311 	}
2312 	if (XS_NULL(xs))
2313 		return;
2314 
2315 	mbs.param[0] = MBOX_GET_DEV_QUEUE_STATUS;
2316 	mbs.param[1] = (XS_TGT(xs) << 8) | XS_LUN(xs);
2317 	isp_mboxcmd(isp, &mbs);
2318 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2319 		isp_dumpregs(isp, "couldn't GET DEVICE QUEUE STATUS");
2320 		return;
2321 	}
2322 	PRINTF("%s: lost command for target %d lun %d, %d active of %d, "
2323 		"Queue State: %x\n", isp->isp_name, XS_TGT(xs),
2324 		XS_LUN(xs), mbs.param[2], mbs.param[3], mbs.param[1]);
2325 
2326 	isp_dumpregs(isp, "lost command");
2327 	/*
2328 	 * XXX: Need to try and do something to recover.
2329 	 */
2330 }
2331 
2332 static void
2333 isp_dumpregs(isp, msg)
2334 	struct ispsoftc *isp;
2335 	const char *msg;
2336 {
2337 	PRINTF("%s: %s\n", isp->isp_name, msg);
2338 	if (isp->isp_type & ISP_HA_SCSI)
2339 		PRINTF("    biu_conf1=%x", ISP_READ(isp, BIU_CONF1));
2340 	else
2341 		PRINTF("    biu_csr=%x", ISP_READ(isp, BIU2100_CSR));
2342 	PRINTF(" biu_icr=%x biu_isr=%x biu_sema=%x ", ISP_READ(isp, BIU_ICR),
2343 	       ISP_READ(isp, BIU_ISR), ISP_READ(isp, BIU_SEMA));
2344 	PRINTF("risc_hccr=%x\n", ISP_READ(isp, HCCR));
2345 
2346 	if (isp->isp_type & ISP_HA_SCSI) {
2347 		ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
2348 		PRINTF("    cdma_conf=%x cdma_sts=%x cdma_fifostat=%x\n",
2349 			ISP_READ(isp, CDMA_CONF), ISP_READ(isp, CDMA_STATUS),
2350 			ISP_READ(isp, CDMA_FIFO_STS));
2351 		PRINTF("    ddma_conf=%x ddma_sts=%x ddma_fifostat=%x\n",
2352 			ISP_READ(isp, DDMA_CONF), ISP_READ(isp, DDMA_STATUS),
2353 			ISP_READ(isp, DDMA_FIFO_STS));
2354 		PRINTF("    sxp_int=%x sxp_gross=%x sxp(scsi_ctrl)=%x\n",
2355 			ISP_READ(isp, SXP_INTERRUPT),
2356 			ISP_READ(isp, SXP_GROSS_ERR),
2357 			ISP_READ(isp, SXP_PINS_CONTROL));
2358 		ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
2359 	}
2360 	ISP_DUMPREGS(isp);
2361 }
2362 
2363 static void
2364 isp_dumpxflist(isp)
2365 	struct ispsoftc *isp;
2366 {
2367 	volatile ISP_SCSI_XFER_T *xs;
2368 	int i, hdp;
2369 
2370 	for (hdp = i = 0; i < RQUEST_QUEUE_LEN; i++) {
2371 		xs = isp->isp_xflist[i];
2372 		if (xs == NULL) {
2373 			continue;
2374 		}
2375 		if (hdp == 0) {
2376 			PRINTF("%s: active requests\n", isp->isp_name);
2377 			hdp++;
2378 		}
2379 		PRINTF(" Active Handle %d: tgt %d lun %d dlen %d\n",
2380 		    i+1, XS_TGT(xs), XS_LUN(xs), XS_XFRLEN(xs));
2381 	}
2382 }
2383 
2384 static void
2385 isp_fw_state(isp)
2386 	struct ispsoftc *isp;
2387 {
2388 	mbreg_t mbs;
2389 	if (isp->isp_type & ISP_HA_FC) {
2390 		int once = 0;
2391 		fcparam *fcp = isp->isp_param;
2392 again:
2393 		mbs.param[0] = MBOX_GET_FW_STATE;
2394 		isp_mboxcmd(isp, &mbs);
2395 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2396 			if (mbs.param[0] == ASYNC_LIP_OCCURRED ||
2397 			    mbs.param[0] == ASYNC_LOOP_UP) {
2398 				if (once++ < 2) {
2399 					goto again;
2400 				}
2401 			}
2402 			isp_dumpregs(isp, "GET FIRMWARE STATE failed");
2403 			return;
2404 		}
2405 		fcp->isp_fwstate = mbs.param[1];
2406 	}
2407 }
2408 
2409 static void
2410 isp_update(isp)
2411 	struct ispsoftc *isp;
2412 {
2413 	int tgt;
2414 	mbreg_t mbs;
2415 	sdparam *sdp;
2416 
2417 	isp->isp_update = 0;
2418 
2419 	if (isp->isp_type & ISP_HA_FC) {
2420 		return;
2421 	}
2422 
2423 	sdp = isp->isp_param;
2424 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
2425 		if (sdp->isp_devparam[tgt].dev_enable == 0) {
2426 			continue;
2427 		}
2428 		if (sdp->isp_devparam[tgt].dev_update == 0) {
2429 			continue;
2430 		}
2431 
2432 		mbs.param[0] = MBOX_SET_TARGET_PARAMS;
2433 		mbs.param[1] = tgt << 8;
2434 		mbs.param[2] = sdp->isp_devparam[tgt].dev_flags;
2435 		mbs.param[3] =
2436 			(sdp->isp_devparam[tgt].sync_offset << 8) |
2437 			(sdp->isp_devparam[tgt].sync_period);
2438 
2439 		IDPRINTF(3, ("\n%s: tgt %d cflags %x offset %x period %x\n",
2440 		    isp->isp_name, tgt, mbs.param[2], mbs.param[3] >> 8,
2441 		    mbs.param[3] & 0xff));
2442 
2443 		isp_mboxcmd(isp, &mbs);
2444 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2445 			PRINTF("%s: failed to change SCSI parameters for "
2446 			    "target %d\n", isp->isp_name, tgt);
2447 		} else {
2448 			char *wt;
2449 			int x, flags;
2450 
2451 			flags = sdp->isp_devparam[tgt].cur_dflags =
2452 			    sdp->isp_devparam[tgt].dev_flags;
2453 
2454 			x = sdp->isp_devparam[tgt].sync_period & 0xff;
2455 			if (flags & DPARM_SYNC) {
2456 				if (x == (ISP_20M_SYNCPARMS & 0xff)) {
2457 					x = 20;
2458 				} else if (x == (ISP_10M_SYNCPARMS & 0xff)) {
2459 					x = 10;
2460 				} else if (x == (ISP_08M_SYNCPARMS & 0xff)) {
2461 					x = 8;
2462 				} else if (x == (ISP_05M_SYNCPARMS & 0xff)) {
2463 					x = 5;
2464 				} else if (x == (ISP_04M_SYNCPARMS & 0xff)) {
2465 					x = 4;
2466 				} else {
2467 					x = 0;
2468 				}
2469 			} else {
2470 				x = 0;
2471 			}
2472 			switch (flags & (DPARM_WIDE|DPARM_TQING)) {
2473 			case DPARM_WIDE:
2474 				wt = ", 16 bit wide\n";
2475 				break;
2476 			case DPARM_TQING:
2477 				wt = ", Tagged Queueing Enabled\n";
2478 				break;
2479 			case DPARM_WIDE|DPARM_TQING:
2480 				wt = ", 16 bit wide, Tagged Queueing Enabled\n";
2481 				break;
2482 
2483 			default:
2484 				wt = "\n";
2485 				break;
2486 			}
2487 			if (x) {
2488 				IDPRINTF(3, ("%s: Target %d maximum Sync Mode "
2489 				    "at %dMHz%s", isp->isp_name, tgt, x, wt));
2490 			} else {
2491 				IDPRINTF(3, ("%s: Target %d Async Mode%s",
2492 				    isp->isp_name, tgt, wt));
2493 			}
2494 		}
2495 		sdp->isp_devparam[tgt].dev_update = 0;
2496 	}
2497 }
2498 
2499 static void
2500 isp_setdfltparm(isp)
2501 	struct ispsoftc *isp;
2502 {
2503 	int i, use_nvram;
2504 	mbreg_t mbs;
2505 	sdparam *sdp;
2506 
2507 	/*
2508 	 * Been there, done that, got the T-shirt...
2509 	 */
2510 	if (isp->isp_gotdparms) {
2511 		IDPRINTF(3, ("%s: already have dparms\n", isp->isp_name));
2512 		return;
2513 	}
2514 	isp->isp_gotdparms = 1;
2515 
2516 	use_nvram = (isp_read_nvram(isp) == 0);
2517 	if (use_nvram) {
2518 		return;
2519 	}
2520 	if (isp->isp_type & ISP_HA_FC) {
2521 		fcparam *fcp = (fcparam *) isp->isp_param;
2522 		fcp->isp_maxfrmlen = ICB_DFLT_FRMLEN;
2523 		fcp->isp_maxalloc = 256;
2524 		fcp->isp_execthrottle = 16;
2525 		fcp->isp_retry_delay = 5;
2526 		fcp->isp_retry_count = 0;
2527 		/*
2528 		 * It would be nice to fake up a WWN in case we don't
2529 		 * get one out of NVRAM. Solaris does this for SOCAL
2530 		 * cards that don't have SBus properties- it sets up
2531 		 * a WWN based upon the system MAC Address.
2532 		 */
2533 		fcp->isp_wwn = 0;
2534 		return;
2535 	}
2536 
2537 	sdp = (sdparam *) isp->isp_param;
2538 	mbs.param[0] = MBOX_GET_ACT_NEG_STATE;
2539 	isp_mboxcmd(isp, &mbs);
2540 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2541 		IDPRINTF(2, ("could not GET ACT NEG STATE\n"));
2542 		sdp->isp_req_ack_active_neg = 1;
2543 		sdp->isp_data_line_active_neg = 1;
2544 	} else {
2545 		sdp->isp_req_ack_active_neg = (mbs.param[1] >> 4) & 0x1;
2546 		sdp->isp_data_line_active_neg = (mbs.param[1] >> 5) & 0x1;
2547 	}
2548 	for (i = 0; i < MAX_TARGETS; i++) {
2549 
2550 		mbs.param[0] = MBOX_GET_TARGET_PARAMS;
2551 		mbs.param[1] = i << 8;
2552 		isp_mboxcmd(isp, &mbs);
2553 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2554 			PRINTF("%s: can't get SCSI parameters for target %d\n",
2555 			    isp->isp_name, i);
2556 			sdp->isp_devparam[i].sync_period = 0;
2557 			sdp->isp_devparam[i].sync_offset = 0;
2558 			sdp->isp_devparam[i].dev_flags = DPARM_SAFE_DFLT;
2559 			continue;
2560 		}
2561 		sdp->isp_devparam[i].dev_flags = mbs.param[2];
2562 
2563 		/*
2564 		 * The maximum period we can really see
2565 		 * here is 100 (decimal), or 400 ns.
2566 		 * For some unknown reason we sometimes
2567 		 * get back wildass numbers from the
2568 		 * boot device's parameters.
2569 		 *
2570 		 * XXX: Hmm- this may be based on a different
2571 		 * XXX: clock rate.
2572 		 */
2573 		if ((mbs.param[3] & 0xff) <= 0x64) {
2574 			sdp->isp_devparam[i].sync_period = mbs.param[3] & 0xff;
2575 			sdp->isp_devparam[i].sync_offset = mbs.param[3] >> 8;
2576 		}
2577 
2578 		/*
2579 		 * It is not safe to run Ultra Mode with a clock < 60.
2580 		 */
2581 		if (((sdp->isp_clock && sdp->isp_clock < 60) ||
2582 		    (isp->isp_type < ISP_HA_SCSI_1020A)) &&
2583 		    (sdp->isp_devparam[i].sync_period ==
2584 		    (ISP_20M_SYNCPARMS & 0xff))) {
2585 			sdp->isp_devparam[i].sync_offset =
2586 				ISP_10M_SYNCPARMS >> 8;
2587 			sdp->isp_devparam[i].sync_period =
2588 				ISP_10M_SYNCPARMS & 0xff;
2589 		}
2590 
2591 	}
2592 
2593 	/*
2594 	 * Set Default Host Adapter Parameters
2595 	 */
2596 	sdp->isp_cmd_dma_burst_enable = 1;
2597 	sdp->isp_data_dma_burst_enabl = 1;
2598 	sdp->isp_fifo_threshold = 0;
2599 	sdp->isp_initiator_id = 7;
2600 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
2601 		sdp->isp_async_data_setup = 9;
2602 	} else {
2603 		sdp->isp_async_data_setup = 6;
2604 	}
2605 	sdp->isp_selection_timeout = 250;
2606 	sdp->isp_max_queue_depth = 128;
2607 	sdp->isp_tag_aging = 8;
2608 	sdp->isp_bus_reset_delay = 3;
2609 	sdp->isp_retry_count = 0;
2610 	sdp->isp_retry_delay = 1;
2611 
2612 	for (i = 0; i < MAX_TARGETS; i++) {
2613 		sdp->isp_devparam[i].exc_throttle = 16;
2614 		sdp->isp_devparam[i].dev_enable = 1;
2615 	}
2616 }
2617 
2618 /*
2619  * Re-initialize the ISP and complete all orphaned commands
2620  * with a 'botched' notice.
2621  *
2622  * Locks held prior to coming here.
2623  */
2624 
2625 void
2626 isp_restart(isp)
2627 	struct ispsoftc *isp;
2628 {
2629 	ISP_SCSI_XFER_T *tlist[RQUEST_QUEUE_LEN], *xs;
2630 	int i;
2631 
2632 	for (i = 0; i < RQUEST_QUEUE_LEN; i++) {
2633 		tlist[i] = (ISP_SCSI_XFER_T *) isp->isp_xflist[i];
2634 		isp->isp_xflist[i] = NULL;
2635 	}
2636 	isp_reset(isp);
2637 	if (isp->isp_state == ISP_RESETSTATE) {
2638 		isp_init(isp);
2639 		if (isp->isp_state == ISP_INITSTATE) {
2640 			isp->isp_state = ISP_RUNSTATE;
2641 		}
2642 	}
2643 	if (isp->isp_state != ISP_RUNSTATE) {
2644 		PRINTF("%s: isp_restart cannot restart ISP\n", isp->isp_name);
2645 	}
2646 
2647 	for (i = 0; i < RQUEST_QUEUE_LEN; i++) {
2648 		xs = tlist[i];
2649 		if (XS_NULL(xs)) {
2650 			continue;
2651 		}
2652 		isp->isp_nactive--;
2653 		if (isp->isp_nactive < 0) {
2654 			isp->isp_nactive = 0;
2655 		}
2656 		XS_RESID(xs) = XS_XFRLEN(xs);
2657 		XS_SETERR(xs, HBA_BUSRESET);
2658 		XS_CMD_DONE(xs);
2659 	}
2660 }
2661 
2662 void
2663 isp_watch(arg)
2664 	void *arg;
2665 {
2666 	int i;
2667 	struct ispsoftc *isp = arg;
2668 	ISP_SCSI_XFER_T *xs;
2669 	ISP_LOCKVAL_DECL;
2670 
2671 	/*
2672 	 * Look for completely dead commands (but not polled ones).
2673 	 */
2674 	ISP_ILOCK(isp);
2675 	for (i = 0; i < RQUEST_QUEUE_LEN; i++) {
2676 		if ((xs = (ISP_SCSI_XFER_T *) isp->isp_xflist[i]) == NULL) {
2677 			continue;
2678 		}
2679 		if (XS_TIME(xs) == 0) {
2680 			continue;
2681 		}
2682 		XS_TIME(xs) -= (WATCH_INTERVAL * 1000);
2683 		/*
2684 		 * Avoid later thinking that this
2685 		 * transaction is not being timed.
2686 		 * Then give ourselves to watchdog
2687 		 * periods of grace.
2688 		 */
2689 		if (XS_TIME(xs) == 0)
2690 			XS_TIME(xs) = 1;
2691 		else if (XS_TIME(xs) > -(2 * WATCH_INTERVAL * 1000)) {
2692 			continue;
2693 		}
2694 		if (isp_control(isp, ISPCTL_ABORT_CMD, xs)) {
2695 			PRINTF("%s: isp_watch failed to abort command\n",
2696 			    isp->isp_name);
2697 			isp_restart(isp);
2698 			break;
2699 		}
2700 	}
2701 	ISP_IUNLOCK(isp);
2702 	RESTART_WATCHDOG(isp_watch, isp);
2703 }
2704 
2705 /*
2706  * Miscellaneous debug statements.
2707  */
2708 static void
2709 isp_prtstst(sp)
2710 	ispstatusreq_t *sp;
2711 {
2712 	PRINTF("states->");
2713 	if (sp->req_state_flags & RQSF_GOT_BUS)
2714 		PRINTF("GOT_BUS ");
2715 	if (sp->req_state_flags & RQSF_GOT_TARGET)
2716 		PRINTF("GOT_TGT ");
2717 	if (sp->req_state_flags & RQSF_SENT_CDB)
2718 		PRINTF("SENT_CDB ");
2719 	if (sp->req_state_flags & RQSF_XFRD_DATA)
2720 		PRINTF("XFRD_DATA ");
2721 	if (sp->req_state_flags & RQSF_GOT_STATUS)
2722 		PRINTF("GOT_STS ");
2723 	if (sp->req_state_flags & RQSF_GOT_SENSE)
2724 		PRINTF("GOT_SNS ");
2725 	if (sp->req_state_flags & RQSF_XFER_COMPLETE)
2726 		PRINTF("XFR_CMPLT ");
2727 	PRINTF("\n");
2728 	PRINTF("status->");
2729 	if (sp->req_status_flags & RQSTF_DISCONNECT)
2730 		PRINTF("Disconnect ");
2731 	if (sp->req_status_flags & RQSTF_SYNCHRONOUS)
2732 		PRINTF("Sync_xfr ");
2733 	if (sp->req_status_flags & RQSTF_PARITY_ERROR)
2734 		PRINTF("Parity ");
2735 	if (sp->req_status_flags & RQSTF_BUS_RESET)
2736 		PRINTF("Bus_Reset ");
2737 	if (sp->req_status_flags & RQSTF_DEVICE_RESET)
2738 		PRINTF("Device_Reset ");
2739 	if (sp->req_status_flags & RQSTF_ABORTED)
2740 		PRINTF("Aborted ");
2741 	if (sp->req_status_flags & RQSTF_TIMEOUT)
2742 		PRINTF("Timeout ");
2743 	if (sp->req_status_flags & RQSTF_NEGOTIATION)
2744 		PRINTF("Negotiation ");
2745 	PRINTF("\n");
2746 }
2747 
2748 static char *
2749 isp2100_fw_statename(state)
2750 	u_int8_t state;
2751 {
2752 	switch(state) {
2753 	case FW_CONFIG_WAIT:	return "Config Wait";
2754 	case FW_WAIT_AL_PA:	return "Waiting for AL/PA";
2755 	case FW_WAIT_LOGIN:	return "Wait Login";
2756 	case FW_READY:		return "Ready";
2757 	case FW_LOSS_OF_SYNC:	return "Loss Of Sync";
2758 	case FW_ERROR:		return "Error";
2759 	case FW_REINIT:		return "Re-Init";
2760 	case FW_NON_PART:	return "Nonparticipating";
2761 	default:		return "eh?";
2762 	}
2763 }
2764 
2765 /*
2766  * NVRAM Routines
2767  */
2768 
2769 static int
2770 isp_read_nvram(isp)
2771 	struct ispsoftc *isp;
2772 {
2773 	int i, amt;
2774 	u_int8_t csum, minversion;
2775 	union {
2776 		u_int8_t _x[ISP2100_NVRAM_SIZE];
2777 		u_int16_t _s[ISP2100_NVRAM_SIZE>>1];
2778 	} _n;
2779 #define	nvram_data	_n._x
2780 #define	nvram_words	_n._s
2781 
2782 	if (isp->isp_type & ISP_HA_FC) {
2783 		amt = ISP2100_NVRAM_SIZE;
2784 		minversion = 1;
2785 	} else {
2786 		amt = ISP_NVRAM_SIZE;
2787 		minversion = 2;
2788 	}
2789 
2790 	/*
2791 	 * Just read the first two words first to see if we have a valid
2792 	 * NVRAM to continue reading the rest with.
2793 	 */
2794 	for (i = 0; i < 2; i++) {
2795 		isp_rdnvram_word(isp, i, &nvram_words[i]);
2796 	}
2797 	if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
2798 	    nvram_data[2] != 'P') {
2799 		if (isp->isp_bustype != ISP_BT_SBUS) {
2800 			PRINTF("%s: invalid NVRAM header\n", isp->isp_name);
2801 		}
2802 		return (-1);
2803 	}
2804 	for (i = 2; i < amt>>1; i++) {
2805 		isp_rdnvram_word(isp, i, &nvram_words[i]);
2806 	}
2807 	for (csum = 0, i = 0; i < amt; i++) {
2808 		csum += nvram_data[i];
2809 	}
2810 	if (csum != 0) {
2811 		PRINTF("%s: invalid NVRAM checksum\n", isp->isp_name);
2812 		return (-1);
2813 	}
2814 	if (ISP_NVRAM_VERSION(nvram_data) < minversion) {
2815 		PRINTF("%s: version %d NVRAM not understood\n", isp->isp_name,
2816 		    ISP_NVRAM_VERSION(nvram_data));
2817 		return (-1);
2818 	}
2819 
2820 	if (isp->isp_type & ISP_HA_SCSI) {
2821 		sdparam *sdp = (sdparam *) isp->isp_param;
2822 
2823 		/* XXX CHECK THIS FOR SANITY XXX */
2824 		sdp->isp_fifo_threshold =
2825 			ISP_NVRAM_FIFO_THRESHOLD(nvram_data);
2826 
2827 		sdp->isp_initiator_id =
2828 			ISP_NVRAM_INITIATOR_ID(nvram_data);
2829 
2830 		sdp->isp_bus_reset_delay =
2831 			ISP_NVRAM_BUS_RESET_DELAY(nvram_data);
2832 
2833 		sdp->isp_retry_count =
2834 			ISP_NVRAM_BUS_RETRY_COUNT(nvram_data);
2835 
2836 		sdp->isp_retry_delay =
2837 			ISP_NVRAM_BUS_RETRY_DELAY(nvram_data);
2838 
2839 		sdp->isp_async_data_setup =
2840 			ISP_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data);
2841 
2842 		if (isp->isp_type >= ISP_HA_SCSI_1040) {
2843 			if (sdp->isp_async_data_setup < 9) {
2844 				sdp->isp_async_data_setup = 9;
2845 			}
2846 		} else {
2847 			if (sdp->isp_async_data_setup != 6) {
2848 				sdp->isp_async_data_setup = 6;
2849 			}
2850 		}
2851 
2852 		sdp->isp_req_ack_active_neg =
2853 			ISP_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data);
2854 
2855 		sdp->isp_data_line_active_neg =
2856 			ISP_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data);
2857 
2858 		sdp->isp_data_dma_burst_enabl =
2859 			ISP_NVRAM_DATA_DMA_BURST_ENABLE(nvram_data);
2860 
2861 		sdp->isp_cmd_dma_burst_enable =
2862 			ISP_NVRAM_CMD_DMA_BURST_ENABLE(nvram_data);
2863 
2864 		sdp->isp_tag_aging =
2865 			ISP_NVRAM_TAG_AGE_LIMIT(nvram_data);
2866 
2867 		/* XXX ISP_NVRAM_FIFO_THRESHOLD_128 XXX */
2868 
2869 		sdp->isp_selection_timeout =
2870 			ISP_NVRAM_SELECTION_TIMEOUT(nvram_data);
2871 
2872 		sdp->isp_max_queue_depth =
2873 			ISP_NVRAM_MAX_QUEUE_DEPTH(nvram_data);
2874 
2875 		sdp->isp_fast_mttr = ISP_NVRAM_FAST_MTTR_ENABLE(nvram_data);
2876 
2877 		for (i = 0; i < 16; i++) {
2878 			sdp->isp_devparam[i].dev_enable =
2879 				ISP_NVRAM_TGT_DEVICE_ENABLE(nvram_data, i);
2880 			sdp->isp_devparam[i].exc_throttle =
2881 				ISP_NVRAM_TGT_EXEC_THROTTLE(nvram_data, i);
2882 			sdp->isp_devparam[i].sync_offset =
2883 				ISP_NVRAM_TGT_SYNC_OFFSET(nvram_data, i);
2884 			sdp->isp_devparam[i].sync_period =
2885 				ISP_NVRAM_TGT_SYNC_PERIOD(nvram_data, i);
2886 
2887 			if (isp->isp_type < ISP_HA_SCSI_1040) {
2888 				/*
2889 				 * If we're not ultra, we can't possibly
2890 				 * be a shorter period than this.
2891 				 */
2892 				if (sdp->isp_devparam[i].sync_period < 0x19) {
2893 					sdp->isp_devparam[i].sync_period =
2894 					    0x19;
2895 				}
2896 				if (sdp->isp_devparam[i].sync_offset > 0xc) {
2897 					sdp->isp_devparam[i].sync_offset =
2898 					    0x0c;
2899 				}
2900 			} else {
2901 				if (sdp->isp_devparam[i].sync_offset > 0x8) {
2902 					sdp->isp_devparam[i].sync_offset = 0x8;
2903 				}
2904 			}
2905 
2906 			sdp->isp_devparam[i].dev_flags = 0;
2907 
2908 			if (ISP_NVRAM_TGT_RENEG(nvram_data, i))
2909 				sdp->isp_devparam[i].dev_flags |= DPARM_RENEG;
2910 			if (ISP_NVRAM_TGT_QFRZ(nvram_data, i)) {
2911 				PRINTF("%s: not supporting QFRZ option for "
2912 				    "target %d\n", isp->isp_name, i);
2913 			}
2914 			sdp->isp_devparam[i].dev_flags |= DPARM_ARQ;
2915 			if (ISP_NVRAM_TGT_ARQ(nvram_data, i) == 0) {
2916 				PRINTF("%s: not disabling ARQ option for "
2917 				    "target %d\n", isp->isp_name, i);
2918 			}
2919 			if (ISP_NVRAM_TGT_TQING(nvram_data, i))
2920 				sdp->isp_devparam[i].dev_flags |= DPARM_TQING;
2921 			if (ISP_NVRAM_TGT_SYNC(nvram_data, i))
2922 				sdp->isp_devparam[i].dev_flags |= DPARM_SYNC;
2923 			if (ISP_NVRAM_TGT_WIDE(nvram_data, i))
2924 				sdp->isp_devparam[i].dev_flags |= DPARM_WIDE;
2925 			if (ISP_NVRAM_TGT_PARITY(nvram_data, i))
2926 				sdp->isp_devparam[i].dev_flags |= DPARM_PARITY;
2927 			if (ISP_NVRAM_TGT_DISC(nvram_data, i))
2928 				sdp->isp_devparam[i].dev_flags |= DPARM_DISC;
2929 		}
2930 	} else {
2931 		fcparam *fcp = (fcparam *) isp->isp_param;
2932 		union {
2933 			struct {
2934 #if	BYTE_ORDER == BIG_ENDIAN
2935 				u_int32_t hi32;
2936 				u_int32_t lo32;
2937 #else
2938 				u_int32_t lo32;
2939 				u_int32_t hi32;
2940 #endif
2941 			} wds;
2942 			u_int64_t full64;
2943 		} wwnstore;
2944 
2945 		wwnstore.full64 = ISP2100_NVRAM_NODE_NAME(nvram_data);
2946 		PRINTF("%s: Adapter WWN 0x%08x%08x\n", isp->isp_name,
2947 		    wwnstore.wds.hi32, wwnstore.wds.lo32);
2948 		fcp->isp_wwn = wwnstore.full64;
2949 		wwnstore.full64 = ISP2100_NVRAM_BOOT_NODE_NAME(nvram_data);
2950 		if (wwnstore.full64 != 0) {
2951 			PRINTF("%s: BOOT DEVICE WWN 0x%08x%08x\n", isp->isp_name,
2952 			    wwnstore.wds.hi32, wwnstore.wds.lo32);
2953 		}
2954 		fcp->isp_maxalloc =
2955 			ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data);
2956 		fcp->isp_maxfrmlen =
2957 			ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data);
2958 		fcp->isp_retry_delay =
2959 			ISP2100_NVRAM_RETRY_DELAY(nvram_data);
2960 		fcp->isp_retry_count =
2961 			ISP2100_NVRAM_RETRY_COUNT(nvram_data);
2962 		fcp->isp_loopid =
2963 			ISP2100_NVRAM_HARDLOOPID(nvram_data);
2964 		fcp->isp_execthrottle =
2965 			ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data);
2966 	}
2967 	return (0);
2968 }
2969 
2970 static void
2971 isp_rdnvram_word(isp, wo, rp)
2972 	struct ispsoftc *isp;
2973 	int wo;
2974 	u_int16_t *rp;
2975 {
2976 	int i, cbits;
2977 	u_int16_t bit, rqst;
2978 
2979 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
2980 	SYS_DELAY(2);
2981 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
2982 	SYS_DELAY(2);
2983 
2984 	if (isp->isp_type & ISP_HA_FC) {
2985 		wo &= ((ISP2100_NVRAM_SIZE >> 1) - 1);
2986 		rqst = (ISP_NVRAM_READ << 8) | wo;
2987 		cbits = 10;
2988 	} else {
2989 		wo &= ((ISP_NVRAM_SIZE >> 1) - 1);
2990 		rqst = (ISP_NVRAM_READ << 6) | wo;
2991 		cbits = 8;
2992 	}
2993 
2994 	/*
2995 	 * Clock the word select request out...
2996 	 */
2997 	for (i = cbits; i >= 0; i--) {
2998 		if ((rqst >> i) & 1) {
2999 			bit = BIU_NVRAM_SELECT | BIU_NVRAM_DATAOUT;
3000 		} else {
3001 			bit = BIU_NVRAM_SELECT;
3002 		}
3003 		ISP_WRITE(isp, BIU_NVRAM, bit);
3004 		SYS_DELAY(2);
3005 		ISP_WRITE(isp, BIU_NVRAM, bit | BIU_NVRAM_CLOCK);
3006 		SYS_DELAY(2);
3007 		ISP_WRITE(isp, BIU_NVRAM, bit);
3008 		SYS_DELAY(2);
3009 	}
3010 	/*
3011 	 * Now read the result back in (bits come back in MSB format).
3012 	 */
3013 	*rp = 0;
3014 	for (i = 0; i < 16; i++) {
3015 		u_int16_t rv;
3016 		*rp <<= 1;
3017 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
3018 		SYS_DELAY(2);
3019 		rv = ISP_READ(isp, BIU_NVRAM);
3020 		if (rv & BIU_NVRAM_DATAIN) {
3021 			*rp |= 1;
3022 		}
3023 		SYS_DELAY(2);
3024 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
3025 		SYS_DELAY(2);
3026 	}
3027 	ISP_WRITE(isp, BIU_NVRAM, 0);
3028 	SYS_DELAY(2);
3029 #if	BYTE_ORDER == BIG_ENDIAN
3030 	*rp = ((*rp >> 8) | ((*rp & 0xff) << 8));
3031 #endif
3032 }
3033