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