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