xref: /freebsd/sys/dev/isp/isp.c (revision 11afcc8f9f96d657b8e6f7547c02c1957331fc96)
1 /* $Id: isp.c,v 1.6 1998/04/14 17:43:45 mjacob Exp $ */
2 /*
3  * Machine Independent (well, as best as possible)
4  * code for the Qlogic ISP SCSI adapters.
5  *
6  *---------------------------------------
7  * Copyright (c) 1997, 1998 by Matthew Jacob
8  * NASA/Ames Research Center
9  * All rights reserved.
10  *---------------------------------------
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice immediately at the beginning of the file, without modification,
17  *    this list of conditions, and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. The name of the author may not be used to endorse or promote products
22  *    derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
28  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 /*
38  * Inspiration and ideas about this driver are from Erik Moe's Linux driver
39  * (qlogicisp.c) and Dave Miller's SBus version of same (qlogicisp.c). Some
40  * ideas dredged from the Solaris driver.
41  */
42 
43 /*
44  * Include header file appropriate for platform we're building on.
45  */
46 
47 #ifdef	__NetBSD__
48 #include <dev/ic/isp_netbsd.h>
49 #endif
50 #ifdef	__FreeBSD__
51 #include <dev/isp/isp_freebsd.h>
52 #endif
53 #ifdef	__linux__
54 #include <isp_linux.h>
55 #endif
56 
57 /*
58  * General defines
59  */
60 
61 #define	MBOX_DELAY_COUNT	1000000 / 100
62 
63 /*
64  * Function prototypes.
65  */
66 static int isp_poll __P((struct ispsoftc *, ISP_SCSI_XFER_T *, int));
67 static void isp_parse_status
68 __P((struct ispsoftc *, ispstatusreq_t *, ISP_SCSI_XFER_T *));
69 static void isp_lostcmd
70 __P((struct ispsoftc *, ISP_SCSI_XFER_T *, ispreq_t *));
71 static void isp_fibre_init __P((struct ispsoftc *));
72 static void isp_fw_state __P((struct ispsoftc *));
73 static void isp_dumpregs __P((struct ispsoftc *, const char *));
74 static void isp_setdparm __P((struct ispsoftc *));
75 static void isp_prtstst __P((ispstatusreq_t *));
76 static void isp_mboxcmd __P((struct ispsoftc *, mbreg_t *));
77 
78 /*
79  * Reset Hardware.
80  */
81 void
82 isp_reset(isp)
83 	struct ispsoftc *isp;
84 {
85 	mbreg_t mbs;
86 	int loops, i, dodnld = 1;
87 	char *revname;
88 	ISP_LOCKVAL_DECL;
89 
90 	revname = "(unknown)";
91 
92 	isp->isp_state = ISP_NILSTATE;
93 
94 	/*
95 	 * Basic types have been set in the MD code.
96 	 * See if we can't figure out more here.
97 	 */
98 	isp->isp_dblev = DFLT_DBLEVEL;
99 	if (isp->isp_type & ISP_HA_FC) {
100 		revname = "2100";
101 	} else {
102 		i = ISP_READ(isp, BIU_CONF0) & BIU_CONF0_HW_MASK;
103 		switch (i) {
104 		default:
105 			PRINTF("%s: unknown ISP type %x- assuming 1020\n",
106 			    isp->isp_name, i);
107 			isp->isp_type = ISP_HA_SCSI_1020;
108 			break;
109 		case 1:
110 		case 2:
111 			revname = "1020";
112 			isp->isp_type = ISP_HA_SCSI_1020;
113 			break;
114 		case 3:
115 			revname = "1040A";
116 			isp->isp_type = ISP_HA_SCSI_1040A;
117 			break;
118 		case 5:
119 			revname = "1040B";
120 			isp->isp_type = ISP_HA_SCSI_1040B;
121 			break;
122 		}
123 	}
124 
125 	/*
126 	 * Do MD specific pre initialization
127 	 */
128 	ISP_LOCK;
129 	ISP_RESET0(isp);
130 	isp_setdparm(isp);
131 
132 	/*
133 	 * Hit the chip over the head with hammer,
134 	 * and give the ISP a chance to recover.
135 	 */
136 
137 	if (isp->isp_type & ISP_HA_SCSI) {
138 		ISP_WRITE(isp, BIU_ICR, BIU_ICR_SOFT_RESET);
139 		/*
140 		 * A slight delay...
141 		 */
142 		SYS_DELAY(100);
143 
144 		/*
145 		 * Clear data && control DMA engines.
146 		 */
147 		ISP_WRITE(isp, CDMA_CONTROL,
148 		      DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
149 		ISP_WRITE(isp, DDMA_CONTROL,
150 		      DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
151 	} else {
152 		ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
153 		/*
154 		 * A slight delay...
155 		 */
156 		SYS_DELAY(100);
157 		ISP_WRITE(isp, CDMA2100_CONTROL,
158 			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
159 		ISP_WRITE(isp, TDMA2100_CONTROL,
160 			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
161 		ISP_WRITE(isp, RDMA2100_CONTROL,
162 			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
163 	}
164 
165 	/*
166 	 * Wait for ISP to be ready to go...
167 	 */
168 	loops = MBOX_DELAY_COUNT;
169 	for (;;) {
170 		if (isp->isp_type & ISP_HA_SCSI) {
171 			if (!(ISP_READ(isp, BIU_ICR) & BIU_ICR_SOFT_RESET))
172 				break;
173 		} else {
174 			if (!(ISP_READ(isp, BIU2100_CSR) & BIU2100_SOFT_RESET))
175 				break;
176 		}
177 		SYS_DELAY(100);
178 		if (--loops < 0) {
179 			isp_dumpregs(isp, "chip reset timed out");
180 			ISP_UNLOCK;
181 			return;
182 		}
183 	}
184 	/*
185 	 * More initialization
186 	 */
187 	if (isp->isp_type & ISP_HA_SCSI) {
188 		ISP_WRITE(isp, BIU_CONF1, 0);
189 	} else {
190 		ISP_WRITE(isp, BIU2100_CSR, 0);
191 		ISP_WRITE(isp, RISC_MTR2100, 0x1212);	/* FM */
192 	}
193 
194 	ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
195 	SYS_DELAY(100);
196 
197 	if (isp->isp_type & ISP_HA_SCSI) {
198 		ISP_SETBITS(isp, BIU_CONF1, isp->isp_mdvec->dv_conf1);
199 		if (isp->isp_mdvec->dv_conf1 & BIU_BURST_ENABLE) {
200 			ISP_SETBITS(isp, CDMA_CONF, DMA_ENABLE_BURST);
201 			ISP_SETBITS(isp, DDMA_CONF, DMA_ENABLE_BURST);
202 		}
203 	}
204 	ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE); /* release paused processor */
205 
206 	/*
207 	 * Do MD specific post initialization
208 	 */
209 	ISP_RESET1(isp);
210 
211 	/*
212 	 * Enable interrupts
213 	 */
214 	ENABLE_INTS(isp);
215 
216 	/*
217 	 * Do some sanity checking.
218 	 */
219 	mbs.param[0] = MBOX_NO_OP;
220 	isp_mboxcmd(isp, &mbs);
221 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
222 		isp_dumpregs(isp, "NOP test failed");
223 		ISP_UNLOCK;
224 		return;
225 	}
226 
227 	if (isp->isp_type & ISP_HA_SCSI) {
228 		mbs.param[0] = MBOX_MAILBOX_REG_TEST;
229 		mbs.param[1] = 0xdead;
230 		mbs.param[2] = 0xbeef;
231 		mbs.param[3] = 0xffff;
232 		mbs.param[4] = 0x1111;
233 		mbs.param[5] = 0xa5a5;
234 		isp_mboxcmd(isp, &mbs);
235 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
236 			isp_dumpregs(isp,
237 				"Mailbox Register test didn't complete");
238 			ISP_UNLOCK;
239 			return;
240 		}
241 		if (mbs.param[1] != 0xdead || mbs.param[2] != 0xbeef ||
242 		    mbs.param[3] != 0xffff || mbs.param[4] != 0x1111 ||
243 		    mbs.param[5] != 0xa5a5) {
244 			isp_dumpregs(isp, "Register Test Failed");
245 			ISP_UNLOCK;
246 			return;
247 		}
248 
249 	}
250 
251 	/*
252 	 * Download new Firmware, unless requested not to do so.
253 	 * This is made slightly trickier in some cases where the
254 	 * firmware of the ROM revision is newer than the revision
255 	 * compiled into the driver. So, where we used to compare
256 	 * versions of our f/w and the ROM f/w, now we just see
257 	 * whether we have f/w at all and whether a config flag
258 	 * has disabled our download.
259 	 */
260 	if ((isp->isp_mdvec->dv_fwlen == 0) ||
261 	    (isp->isp_confopts & ISP_CFG_NORELOAD)) {
262 		dodnld = 0;
263 	}
264 
265 	if (dodnld) {
266 		for (i = 0; i < isp->isp_mdvec->dv_fwlen; i++) {
267 			mbs.param[0] = MBOX_WRITE_RAM_WORD;
268 			mbs.param[1] = isp->isp_mdvec->dv_codeorg + i;
269 			mbs.param[2] = isp->isp_mdvec->dv_ispfw[i];
270 			isp_mboxcmd(isp, &mbs);
271 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
272 				isp_dumpregs(isp, "f/w download failed");
273 
274 				ISP_UNLOCK;
275 				return;
276 			}
277 		}
278 
279 		if (isp->isp_mdvec->dv_fwlen) {
280 			/*
281 			 * Verify that it downloaded correctly.
282 			 */
283 			mbs.param[0] = MBOX_VERIFY_CHECKSUM;
284 			mbs.param[1] = isp->isp_mdvec->dv_codeorg;
285 			isp_mboxcmd(isp, &mbs);
286 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
287 				isp_dumpregs(isp, "ram checksum failure");
288 				ISP_UNLOCK;
289 				return;
290 			}
291 		}
292 	} else {
293 		IDPRINTF(3, ("%s: skipping f/w download\n", isp->isp_name));
294 	}
295 
296 	/*
297 	 * Now start it rolling.
298 	 *
299 	 * If we didn't actually download f/w,
300 	 * we still need to (re)start it.
301 	 */
302 
303 	mbs.param[0] = MBOX_EXEC_FIRMWARE;
304 	mbs.param[1] = isp->isp_mdvec->dv_codeorg;
305 	isp_mboxcmd(isp, &mbs);
306 
307 	if (isp->isp_type & ISP_HA_SCSI) {
308 		/*
309 		 * Set CLOCK RATE
310 		 */
311 		if (((sdparam *)isp->isp_param)->isp_clock) {
312 			mbs.param[0] = MBOX_SET_CLOCK_RATE;
313 			mbs.param[1] = ((sdparam *)isp->isp_param)->isp_clock;
314 			isp_mboxcmd(isp, &mbs);
315 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
316 				isp_dumpregs(isp, "failed to set CLOCKRATE");
317 				ISP_UNLOCK;
318 				return;
319 			}
320 		}
321 	}
322 	mbs.param[0] = MBOX_ABOUT_FIRMWARE;
323 	isp_mboxcmd(isp, &mbs);
324 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
325 		isp_dumpregs(isp, "ABOUT FIRMWARE command failed");
326 		ISP_UNLOCK;
327 		return;
328 	}
329 	PRINTF("%s: Board Revision %s, %s F/W Revision %d.%d\n",
330 		isp->isp_name, revname, dodnld? "loaded" : "ROM",
331 		mbs.param[1], mbs.param[2]);
332 	isp_fw_state(isp);
333 	isp->isp_state = ISP_RESETSTATE;
334 	ISP_UNLOCK;
335 }
336 
337 /*
338  * Abort an executing command.
339  * Locks (ints blocked) assumed held.
340  */
341 
342 int
343 isp_abortcmd(isp, xidx)
344 	struct ispsoftc *isp;
345 	int xidx;
346 {
347 	mbreg_t mbs;
348 	ISP_SCSI_XFER_T *xs;
349 
350 	xs = (ISP_SCSI_XFER_T *) isp->isp_xflist[xidx];
351 	if (xs == NULL) {
352 		PRINTF("%s: isp_abortcmd - NULL xs\n", isp->isp_name);
353 		return (0);
354 	}
355 	mbs.param[0] = MBOX_ABORT;
356 	mbs.param[1] = XS_TGT(xs) | XS_LUN(xs);
357 	mbs.param[2] = (xidx+1) >> 16;
358 	mbs.param[3] = (xidx+1) & 0xffff;
359 	isp_mboxcmd(isp, &mbs);
360 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
361 		PRINTF("%s: isp_abort failure (code %x)\n",
362 		       isp->isp_name, mbs.param[0]);
363 		return (0);
364 	} else {
365 		return (1);
366 	}
367 }
368 
369 /*
370  * Initialize Hardware to known state
371  */
372 void
373 isp_init(isp)
374 	struct ispsoftc *isp;
375 {
376 	sdparam *sdp;
377 	mbreg_t mbs;
378 	int i, l;
379 	ISP_LOCKVAL_DECL;
380 
381 	if (isp->isp_type & ISP_HA_FC) {
382 		isp_fibre_init(isp);
383 		return;
384 	}
385 
386 	sdp = isp->isp_param;
387 
388 	/*
389 	 * Try and figure out if we're connected to a differential bus.
390 	 * You have to pause the RISC processor to read SXP registers.
391 	 *
392 	 * This, by the way, is likely broken in that it should be
393 	 * getting this info from NVRAM settings too.
394 	 */
395 	ISP_LOCK;
396 	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
397 	if (ISP_READ(isp, SXP_PINS_DIFF) & SXP_PINS_DIFF_SENSE) {
398 		sdp->isp_diffmode = 1;
399 		PRINTF("%s: Differential Mode\n", isp->isp_name);
400 	} else {
401 		/*
402 		 * Force pullups on.
403 		 */
404 		sdp->isp_req_ack_active_neg = 1;
405 		sdp->isp_data_line_active_neg = 1;
406 		sdp->isp_diffmode = 0;
407 	}
408 	ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE); /* release paused processor */
409 
410 	mbs.param[0] = MBOX_GET_INIT_SCSI_ID;
411 	isp_mboxcmd(isp, &mbs);
412 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
413 		ISP_UNLOCK;
414 		isp_dumpregs(isp, "failed to get initiator id");
415 		return;
416 	}
417 	if (mbs.param[1] != sdp->isp_initiator_id) {
418 		PRINTF("%s: setting Initiator ID to %d\n", isp->isp_name,
419 			sdp->isp_initiator_id);
420 		mbs.param[0] = MBOX_SET_INIT_SCSI_ID;
421 		mbs.param[1] = sdp->isp_initiator_id;
422 		isp_mboxcmd(isp, &mbs);
423 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
424 			ISP_UNLOCK;
425 			isp_dumpregs(isp, "failed to set initiator id");
426 			return;
427 		}
428 	} else {
429 		IDPRINTF(3, ("%s: leaving Initiator ID at %d\n", isp->isp_name,
430 			sdp->isp_initiator_id));
431 	}
432 
433 	mbs.param[0] = MBOX_SET_RETRY_COUNT;
434 	mbs.param[1] = sdp->isp_retry_count;
435 	mbs.param[2] = sdp->isp_retry_delay;
436 	isp_mboxcmd(isp, &mbs);
437 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
438 		ISP_UNLOCK;
439 		isp_dumpregs(isp, "failed to set retry count and delay");
440 		return;
441 	}
442 
443 	mbs.param[0] = MBOX_SET_ASYNC_DATA_SETUP_TIME;
444 	mbs.param[1] = sdp->isp_async_data_setup;
445 	isp_mboxcmd(isp, &mbs);
446 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
447 		ISP_UNLOCK;
448 		isp_dumpregs(isp, "failed to set async data setup time");
449 		return;
450 	}
451 
452 	mbs.param[0] = MBOX_SET_ACTIVE_NEG_STATE;
453 	mbs.param[1] =	(sdp->isp_req_ack_active_neg << 4) |
454 			(sdp->isp_data_line_active_neg << 5);
455 	isp_mboxcmd(isp, &mbs);
456 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
457 		ISP_UNLOCK;
458 		isp_dumpregs(isp, "failed to set active neg state");
459 		return;
460 	}
461 
462 	mbs.param[0] = MBOX_SET_TAG_AGE_LIMIT;
463 	mbs.param[1] = sdp->isp_tag_aging;
464 	isp_mboxcmd(isp, &mbs);
465 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
466 		ISP_UNLOCK;
467 		isp_dumpregs(isp, "failed to set tag age limit");
468 		return;
469 	}
470 
471 	mbs.param[0] = MBOX_SET_SELECT_TIMEOUT;
472 	mbs.param[1] = sdp->isp_selection_timeout;
473 	isp_mboxcmd(isp, &mbs);
474 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
475 		ISP_UNLOCK;
476 		isp_dumpregs(isp, "failed to set selection timeout");
477 		return;
478 	}
479 
480 	IDPRINTF(2, ("%s: devparm, W=wide, S=sync, T=Tag\n", isp->isp_name));
481 	for (i = 0; i < MAX_TARGETS; i++) {
482 		char bz[9];
483 		u_int16_t cj = sdp->isp_devparam[i].sync_period;
484 
485 		if (sdp->isp_devparam[i].dev_flags & DPARM_SYNC) {
486 			u_int16_t x;
487 			if (cj == (ISP_20M_SYNCPARMS & 0xff)) {
488 				x = 20;
489 			} else if (cj == (ISP_10M_SYNCPARMS & 0xff)) {
490 				x = 10;
491 			} else if (cj == (ISP_08M_SYNCPARMS & 0xff)) {
492 				x = 8;
493 			} else if (cj == (ISP_05M_SYNCPARMS & 0xff)) {
494 				x = 5;
495 			} else if (cj == (ISP_04M_SYNCPARMS & 0xff)) {
496 				x = 4;
497 			} else {
498 				x = 0;
499 			}
500 			if (x)
501 				sprintf(bz, "%02dMHz:", x);
502 			else
503 				sprintf(bz, "?%04x:", cj);
504 		} else {
505 			sprintf(bz, "Async:");
506 		}
507 		if (sdp->isp_devparam[i].dev_flags & DPARM_WIDE)
508 			bz[6] = 'W';
509 		else
510 			bz[6] = ' ';
511 		if (sdp->isp_devparam[i].dev_flags & DPARM_TQING)
512 			bz[7] = 'T';
513 		else
514 			bz[7] = ' ';
515 		bz[8] = 0;
516 		IDPRINTF(2, (" id%x:%s", i, bz));
517 		if (((i+1) & 0x3) == 0)
518 			IDPRINTF(2, ("\n"));
519 		if (sdp->isp_devparam[i].dev_enable == 0)
520 			continue;
521 
522 		/*
523 		 * It is not safe to run the 1020 in ultra mode.
524 		 */
525 		if (isp->isp_type == ISP_HA_SCSI_1020 &&
526 		    cj == (ISP_20M_SYNCPARMS & 0xff)) {
527 			PRINTF("%s: an ISP1020 set to Ultra Speed- derating.\n",
528 				isp->isp_name);
529 			sdp->isp_devparam[i].sync_offset =
530 				ISP_10M_SYNCPARMS >> 8;
531 			sdp->isp_devparam[i].sync_period =
532 				ISP_10M_SYNCPARMS & 0xff;
533 		}
534 		mbs.param[0] = MBOX_SET_TARGET_PARAMS;
535 		mbs.param[1] = i << 8;
536 		mbs.param[2] = sdp->isp_devparam[i].dev_flags << 8;
537 		mbs.param[3] =
538 			(sdp->isp_devparam[i].sync_offset << 8) |
539 			(sdp->isp_devparam[i].sync_period);
540 
541 		IDPRINTF(3, ("\n%s: target %d flags %x offset %x period %x\n",
542 			     isp->isp_name, i, sdp->isp_devparam[i].dev_flags,
543 			     sdp->isp_devparam[i].sync_offset,
544 			     sdp->isp_devparam[i].sync_period));
545 		isp_mboxcmd(isp, &mbs);
546 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
547 			PRINTF("%s: failed to set parameters for target %d\n",
548 				isp->isp_name, i);
549 			PRINTF("%s: flags %x offset %x period %x\n",
550 				isp->isp_name, sdp->isp_devparam[i].dev_flags,
551 				sdp->isp_devparam[i].sync_offset,
552 				sdp->isp_devparam[i].sync_period);
553 			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
554 			mbs.param[1] = i << 8;
555 			mbs.param[2] = DPARM_DEFAULT << 8;
556 			mbs.param[3] = ISP_10M_SYNCPARMS;
557 			isp_mboxcmd(isp, &mbs);
558 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
559 				ISP_UNLOCK;
560 				PRINTF("%s: failed even to set defaults\n",
561 					isp->isp_name);
562 				return;
563 			}
564 		}
565 		for (l = 0; l < MAX_LUNS; l++) {
566 			mbs.param[0] = MBOX_SET_DEV_QUEUE_PARAMS;
567 			mbs.param[1] = (i << 8) | l;
568 			mbs.param[2] = sdp->isp_max_queue_depth;
569 			mbs.param[3] = sdp->isp_devparam[i].exc_throttle;
570 			isp_mboxcmd(isp, &mbs);
571 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
572 				ISP_UNLOCK;
573 				isp_dumpregs(isp, "failed to set device queue "
574 				       "parameters");
575 				return;
576 			}
577 		}
578 	}
579 
580 	/*
581 	 * Set up DMA for the request and result mailboxes.
582 	 */
583 	if (ISP_MBOXDMASETUP(isp)) {
584 		ISP_UNLOCK;
585 		PRINTF("%s: can't setup dma mailboxes\n", isp->isp_name);
586 		return;
587 	}
588 
589 	mbs.param[0] = MBOX_INIT_RES_QUEUE;
590 	mbs.param[1] = RESULT_QUEUE_LEN(isp);
591 	mbs.param[2] = (u_int16_t) (isp->isp_result_dma >> 16);
592 	mbs.param[3] = (u_int16_t) (isp->isp_result_dma & 0xffff);
593 	mbs.param[4] = 0;
594 	mbs.param[5] = 0;
595 	isp_mboxcmd(isp, &mbs);
596 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
597 		ISP_UNLOCK;
598 		isp_dumpregs(isp, "set of response queue failed");
599 		return;
600 	}
601 	isp->isp_residx = 0;
602 
603 	mbs.param[0] = MBOX_INIT_REQ_QUEUE;
604 	mbs.param[1] = RQUEST_QUEUE_LEN(isp);
605 	mbs.param[2] = (u_int16_t) (isp->isp_rquest_dma >> 16);
606 	mbs.param[3] = (u_int16_t) (isp->isp_rquest_dma & 0xffff);
607 	mbs.param[4] = 0;
608 	mbs.param[5] = 0;
609 	isp_mboxcmd(isp, &mbs);
610 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
611 		ISP_UNLOCK;
612 		isp_dumpregs(isp, "set of request queue failed");
613 		return;
614 	}
615 	isp->isp_reqidx = 0;
616 
617 	/*
618 	 * Unfortunately, this is the only way right now for
619 	 * forcing a sync renegotiation. If we boot off of
620 	 * an Alpha, it's put the chip in SYNC mode, but we
621 	 * haven't necessarily set up the parameters the
622 	 * same, so we'll have to yank the reset line to
623 	 * get everyone to renegotiate.
624 	 */
625 
626 	mbs.param[0] = MBOX_BUS_RESET;
627 	mbs.param[1] = 2;
628 	isp_mboxcmd(isp, &mbs);
629 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
630 		ISP_UNLOCK;
631 		isp_dumpregs(isp, "SCSI bus reset failed");
632 	}
633 	/*
634 	 * This is really important to have set after a bus reset.
635 	 */
636 	isp->isp_sendmarker = 1;
637 	ISP_UNLOCK;
638 	isp->isp_state = ISP_INITSTATE;
639 }
640 
641 static void
642 isp_fibre_init(isp)
643 	struct ispsoftc *isp;
644 {
645 	fcparam *fcp;
646 	isp_icb_t *icbp;
647 	mbreg_t mbs;
648 	int count;
649 	u_int8_t lwfs;
650 	ISP_LOCKVAL_DECL;
651 
652 	fcp = isp->isp_param;
653 
654 	fcp->isp_retry_count = 0;
655 	fcp->isp_retry_delay = 1;
656 
657 	ISP_LOCK;
658 	mbs.param[0] = MBOX_SET_RETRY_COUNT;
659 	mbs.param[1] = fcp->isp_retry_count;
660 	mbs.param[2] = fcp->isp_retry_delay;
661 	isp_mboxcmd(isp, &mbs);
662 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
663 		ISP_UNLOCK;
664 		isp_dumpregs(isp, "failed to set retry count and delay");
665 		return;
666 	}
667 
668 	if (ISP_MBOXDMASETUP(isp)) {
669 		ISP_UNLOCK;
670 		PRINTF("%s: can't setup DMA for mailboxes\n", isp->isp_name);
671 		return;
672 	}
673 
674 	icbp = (isp_icb_t *) fcp->isp_scratch;
675 	bzero(icbp, sizeof (*icbp));
676 #if 0
677 	icbp->icb_maxfrmlen = ICB_DFLT_FRMLEN;
678 	MAKE_NODE_NAME(isp, icbp);
679 	icbp->icb_rqstqlen = RQUEST_QUEUE_LEN(isp);
680 	icbp->icb_rsltqlen = RESULT_QUEUE_LEN(isp);
681 	icbp->icb_rqstaddr[0] = (u_int16_t) (isp->isp_rquest_dma & 0xffff);
682 	icbp->icb_rqstaddr[1] = (u_int16_t) (isp->isp_rquest_dma >> 16);
683 	icbp->icb_respaddr[0] = (u_int16_t) (isp->isp_result_dma & 0xffff);
684 	icbp->icb_respaddr[1] = (u_int16_t) (isp->isp_result_dma >> 16);
685 #endif
686 	icbp->icb_version = 1;
687 	icbp->icb_maxfrmlen = ICB_DFLT_FRMLEN;
688 	icbp->icb_maxalloc = 256;
689 	icbp->icb_execthrottle = 16;
690 	icbp->icb_retry_delay = 5;
691 	icbp->icb_retry_count = 0;
692 	MAKE_NODE_NAME(isp, icbp);
693 	icbp->icb_rqstqlen = RQUEST_QUEUE_LEN(isp);
694 	icbp->icb_rsltqlen = RESULT_QUEUE_LEN(isp);
695 	icbp->icb_rqstaddr[0] = (u_int16_t) (isp->isp_rquest_dma & 0xffff);
696 	icbp->icb_rqstaddr[1] = (u_int16_t) (isp->isp_rquest_dma >> 16);
697 	icbp->icb_respaddr[0] = (u_int16_t) (isp->isp_result_dma & 0xffff);
698 	icbp->icb_respaddr[1] = (u_int16_t) (isp->isp_result_dma >> 16);
699 
700 	mbs.param[0] = MBOX_INIT_FIRMWARE;
701 	mbs.param[1] = 0;
702 	mbs.param[2] = (u_int16_t) (fcp->isp_scdma >> 16);
703 	mbs.param[3] = (u_int16_t) (fcp->isp_scdma & 0xffff);
704 	mbs.param[4] = 0;
705 	mbs.param[5] = 0;
706 	mbs.param[6] = 0;
707 	mbs.param[7] = 0;
708 	isp_mboxcmd(isp, &mbs);
709 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
710 		ISP_UNLOCK;
711 		isp_dumpregs(isp, "INIT FIRMWARE failed");
712 		return;
713 	}
714 	isp->isp_reqidx = 0;
715 	isp->isp_residx = 0;
716 
717 	/*
718 	 * Wait up to 3 seconds for FW to go to READY state.
719 	 *
720 	 * This is all very much not right. The problem here
721 	 * is that the cable may not be plugged in, or there
722 	 * may be many many members of the loop that haven't
723 	 * been logged into.
724 	 *
725 	 * This model of doing things doesn't support dynamic
726 	 * attachment, so we just plain lose (for now).
727 	 */
728 	lwfs = FW_CONFIG_WAIT;
729 	for (count = 0; count < 3000; count++) {
730 		isp_fw_state(isp);
731 		if (lwfs != fcp->isp_fwstate) {
732 			PRINTF("%s: Firmware State %s -> %s\n", isp->isp_name,
733 			    fw_statename(lwfs), fw_statename(fcp->isp_fwstate));
734 			lwfs = fcp->isp_fwstate;
735 		}
736 		if (fcp->isp_fwstate == FW_READY) {
737 			break;
738 		}
739 		SYS_DELAY(1000);	/* wait one millisecond */
740 	}
741 	isp->isp_sendmarker = 1;
742 
743 	/*
744 	 * Get our Loop ID
745 	 * (if possible)
746 	 */
747 	if (fcp->isp_fwstate == FW_READY) {
748 		mbs.param[0] = MBOX_GET_LOOP_ID;
749 		isp_mboxcmd(isp, &mbs);
750 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
751 			isp_dumpregs(isp, "GET LOOP ID failed");
752 			ISP_UNLOCK;
753 			return;
754 		}
755 		fcp->isp_loopid = mbs.param[1];
756 		if (fcp->isp_loopid) {
757 			PRINTF("%s: Loop ID 0x%x\n", isp->isp_name,
758 				fcp->isp_loopid);
759 		}
760 		isp->isp_state = ISP_INITSTATE;
761 	}
762 	ISP_UNLOCK;
763 }
764 
765 /*
766  * Free any associated resources prior to decommissioning.
767  */
768 void
769 isp_uninit(isp)
770 	struct ispsoftc *isp;
771 {
772 	STOP_WATCHDOG(isp_watch, isp);
773 }
774 
775 
776 /*
777  * start an xfer
778  */
779 int32_t
780 ispscsicmd(xs)
781 	ISP_SCSI_XFER_T *xs;
782 {
783 	struct ispsoftc *isp;
784 	u_int8_t iptr, optr;
785 	union {
786 		ispreq_t *_reqp;
787 		ispreqt2_t *_t2reqp;
788 	} _u;
789 #define	reqp	_u._reqp
790 #define	t2reqp	_u._t2reqp
791 #define	UZSIZE	max(sizeof (ispreq_t), sizeof (ispreqt2_t))
792 	int i;
793 	ISP_LOCKVAL_DECL;
794 
795 	XS_INITERR(xs);
796 	isp = XS_ISP(xs);
797 
798 	if (isp->isp_type & ISP_HA_FC) {
799 		if (XS_CDBLEN(xs) > 12) {
800 			PRINTF("%s: unsupported cdb length for fibre (%d)\n",
801 				isp->isp_name, XS_CDBLEN(xs));
802 			XS_SETERR(xs, HBA_BOTCH);
803 			return (CMD_COMPLETE);
804 		}
805 	}
806 	optr = ISP_READ(isp, OUTMAILBOX4);
807 	iptr = isp->isp_reqidx;
808 
809 	reqp = (ispreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, iptr);
810 	iptr = (iptr + 1) & (RQUEST_QUEUE_LEN(isp) - 1);
811 	if (iptr == optr) {
812 		PRINTF("%s: Request Queue Overflow\n", isp->isp_name);
813 		XS_SETERR(xs, HBA_BOTCH);
814 		return (CMD_EAGAIN);
815 	}
816 
817 	ISP_LOCK;
818 	if (isp->isp_type & ISP_HA_FC)
819 		DISABLE_INTS(isp);
820 
821 	if (isp->isp_sendmarker) {
822 		ispmarkreq_t *marker = (ispmarkreq_t *) reqp;
823 
824 		bzero((void *) marker, sizeof (*marker));
825 		marker->req_header.rqs_entry_count = 1;
826 		marker->req_header.rqs_entry_type = RQSTYPE_MARKER;
827 		marker->req_modifier = SYNC_ALL;
828 
829 		isp->isp_sendmarker = 0;
830 
831 		if (((iptr + 1) & (RQUEST_QUEUE_LEN(isp) - 1)) == optr) {
832 			ISP_WRITE(isp, INMAILBOX4, iptr);
833 			isp->isp_reqidx = iptr;
834 
835 			if (isp->isp_type & ISP_HA_FC)
836 				ENABLE_INTS(isp);
837 			ISP_UNLOCK;
838 			PRINTF("%s: Request Queue Overflow+\n", isp->isp_name);
839 			XS_SETERR(xs, HBA_BOTCH);
840 			return (CMD_EAGAIN);
841 		}
842 		reqp = (ispreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, iptr);
843 		iptr = (iptr + 1) & (RQUEST_QUEUE_LEN(isp) - 1);
844 	}
845 
846 	bzero((void *) reqp, UZSIZE);
847 	reqp->req_header.rqs_entry_count = 1;
848 	if (isp->isp_type & ISP_HA_FC) {
849 		reqp->req_header.rqs_entry_type = RQSTYPE_T2RQS;
850 	} else {
851 		reqp->req_header.rqs_entry_type = RQSTYPE_REQUEST;
852 	}
853 	reqp->req_header.rqs_flags = 0;
854 	reqp->req_header.rqs_seqno = isp->isp_seqno++;
855 
856 	for (i = 0; i < RQUEST_QUEUE_LEN(isp); i++) {
857 		if (isp->isp_xflist[i] == NULL)
858 			break;
859 	}
860 	if (i == RQUEST_QUEUE_LEN(isp)) {
861 		if (isp->isp_type & ISP_HA_FC)
862 			ENABLE_INTS(isp);
863 		ISP_UNLOCK;
864 		PRINTF("%s: ran out of xflist pointers?????\n", isp->isp_name);
865 		XS_SETERR(xs, HBA_BOTCH);
866 		return (CMD_EAGAIN);
867 	} else {
868 		/*
869 		 * Never have a handle that is zero, so
870 		 * set req_handle off by one.
871 		 */
872 		isp->isp_xflist[i] = xs;
873 		reqp->req_handle = i+1;
874 	}
875 
876 	if (isp->isp_type & ISP_HA_FC) {
877 		/*
878 		 * See comment in isp_intr
879 		 */
880 		XS_RESID(xs) = 0;
881 		/*
882 		 * Fibre Channel always requires some kind of tag.
883 		 */
884 		if (XS_POLLDCMD(xs)) {
885 			t2reqp->req_flags = REQFLAG_STAG;
886 		} else {
887 			t2reqp->req_flags = REQFLAG_OTAG;
888 		}
889 	} else {
890 		sdparam *sdp = (sdparam *)isp->isp_param;
891 		if ((sdp->isp_devparam[XS_TGT(xs)].dev_flags & DPARM_TQING) &&
892 		    (XS_POLLDCMD(xs) == 0)) {
893 			reqp->req_flags = REQFLAG_OTAG;
894 		} else {
895 			reqp->req_flags = 0;
896 		}
897 	}
898 	reqp->req_lun_trn = XS_LUN(xs);
899 	reqp->req_target = XS_TGT(xs);
900 	if (isp->isp_type & ISP_HA_SCSI) {
901 		reqp->req_cdblen = XS_CDBLEN(xs);
902 	}
903 	bcopy((void *)XS_CDBP(xs), reqp->req_cdb, XS_CDBLEN(xs));
904 
905 	IDPRINTF(6, ("%s(%d.%d): START%d cmd 0x%x datalen %d\n", isp->isp_name,
906 	    XS_TGT(xs), XS_LUN(xs), reqp->req_header.rqs_seqno,
907 	    *(u_char *) XS_CDBP(xs), XS_XFRLEN(xs)));
908 
909 	reqp->req_time = XS_TIME(xs) / 1000;
910 	if (reqp->req_time == 0 && XS_TIME(xs))
911 		reqp->req_time = 1;
912 	if (ISP_DMASETUP(isp, xs, reqp, &iptr, optr)) {
913 		if (isp->isp_type & ISP_HA_FC)
914 			ENABLE_INTS(isp);
915 		ISP_UNLOCK;
916 		XS_SETERR(xs, HBA_BOTCH);
917 		return (CMD_COMPLETE);
918 	}
919 	XS_SETERR(xs, HBA_NOERROR);
920 	ISP_WRITE(isp, INMAILBOX4, iptr);
921 	isp->isp_reqidx = iptr;
922 	if (isp->isp_type & ISP_HA_FC)
923 		ENABLE_INTS(isp);
924 	isp->isp_nactive++;
925 	if (XS_POLLDCMD(xs) == 0) {
926 		ISP_UNLOCK;
927 		return (CMD_QUEUED);
928 	}
929 
930 	/*
931 	 * If we can't use interrupts, poll on completion.
932 	 */
933 	if (isp_poll(isp, xs, XS_TIME(xs))) {
934 		/*
935 		 * If no other error occurred but we didn't finish,
936 		 * something bad happened.
937 		 */
938 		if (XS_IS_CMD_DONE(xs) == 0) {
939 			isp->isp_nactive--;
940 			if (isp->isp_nactive < 0)
941 				isp->isp_nactive = 0;
942 			if (XS_NOERR(xs)) {
943 				isp_lostcmd(isp, xs, reqp);
944 				XS_SETERR(xs, HBA_BOTCH);
945 			}
946 		}
947 	}
948 	ISP_UNLOCK;
949 	return (CMD_COMPLETE);
950 #undef	reqp
951 #undef	t2reqp
952 }
953 
954 /*
955  * Interrupt Service Routine(s)
956  */
957 
958 int
959 isp_poll(isp, xs, mswait)
960 	struct ispsoftc *isp;
961 	ISP_SCSI_XFER_T *xs;
962 	int mswait;
963 {
964 
965 	while (mswait) {
966 		/* Try the interrupt handling routine */
967 		(void)isp_intr((void *)isp);
968 
969 		/* See if the xs is now done */
970 		if (XS_IS_CMD_DONE(xs))
971 			return (0);
972 		SYS_DELAY(1000);	/* wait one millisecond */
973 		mswait--;
974 	}
975 	return (1);
976 }
977 
978 int
979 isp_intr(arg)
980 	void *arg;
981 {
982 	ISP_SCSI_XFER_T *xs;
983 	struct ispsoftc *isp = arg;
984 	u_int16_t iptr, optr, isr;
985 
986 	isr = ISP_READ(isp, BIU_ISR);
987 	if (isp->isp_type & ISP_HA_FC) {
988 		if (isr == 0 || (isr & BIU2100_ISR_RISC_INT) == 0) {
989 			if (isr) {
990 				IDPRINTF(4, ("%s: isp_intr isr=%x\n",
991 					     isp->isp_name, isr));
992 			}
993 			return (0);
994 		}
995 	} else {
996 		if (isr == 0 || (isr & BIU_ISR_RISC_INT) == 0) {
997 			if (isr) {
998 				IDPRINTF(4, ("%s: isp_intr isr=%x\n",
999 					     isp->isp_name, isr));
1000 			}
1001 			return (0);
1002 		}
1003 	}
1004 
1005 	optr = isp->isp_residx;
1006 	if (ISP_READ(isp, BIU_SEMA) & 1) {
1007 		u_int16_t mbox0 = ISP_READ(isp, OUTMAILBOX0);
1008 		switch (mbox0) {
1009 		case ASYNC_BUS_RESET:
1010 		case ASYNC_TIMEOUT_RESET:
1011 			PRINTF("%s: bus or timeout reset\n", isp->isp_name);
1012 			isp->isp_sendmarker = 1;
1013 			break;
1014 		case ASYNC_LIP_OCCURRED:
1015 			PRINTF("%s: LIP occurred\n", isp->isp_name);
1016 			break;
1017 		case ASYNC_LOOP_UP:
1018 			PRINTF("%s: Loop UP\n", isp->isp_name);
1019 			break;
1020 		case ASYNC_LOOP_DOWN:
1021 			PRINTF("%s: Loop DOWN\n", isp->isp_name);
1022 			break;
1023 		case ASYNC_LOOP_RESET:
1024 			PRINTF("%s: Loop RESET\n", isp->isp_name);
1025 			break;
1026 		default:
1027 			PRINTF("%s: async %x\n", isp->isp_name, mbox0);
1028 			break;
1029 		}
1030 		ISP_WRITE(isp, BIU_SEMA, 0);
1031 	}
1032 
1033 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
1034 	iptr = ISP_READ(isp, OUTMAILBOX5);
1035 	if (optr == iptr) {
1036 		IDPRINTF(4, ("why intr? isr %x iptr %x optr %x\n",
1037 			isr, optr, iptr));
1038 	}
1039 	ENABLE_INTS(isp);
1040 
1041 	while (optr != iptr) {
1042 		ispstatusreq_t *sp;
1043 		int buddaboom = 0;
1044 
1045 		sp = (ispstatusreq_t *) ISP_QUEUE_ENTRY(isp->isp_result, optr);
1046 
1047 		optr = (optr + 1) & (RESULT_QUEUE_LEN(isp)-1);
1048 		if (sp->req_header.rqs_entry_type != RQSTYPE_RESPONSE) {
1049 			PRINTF("%s: not RESPONSE in RESPONSE Queue (0x%x)\n",
1050 				isp->isp_name, sp->req_header.rqs_entry_type);
1051 			if (sp->req_header.rqs_entry_type != RQSTYPE_REQUEST) {
1052 				ISP_WRITE(isp, INMAILBOX5, optr);
1053 				continue;
1054 			}
1055 			buddaboom = 1;
1056 		}
1057 
1058 		if (sp->req_header.rqs_flags & 0xf) {
1059 			if (sp->req_header.rqs_flags & RQSFLAG_CONTINUATION) {
1060 				ISP_WRITE(isp, INMAILBOX5, optr);
1061 				continue;
1062 			}
1063 			PRINTF("%s: rqs_flags=%x\n", isp->isp_name,
1064 				sp->req_header.rqs_flags & 0xf);
1065 		}
1066 		if (sp->req_handle > RQUEST_QUEUE_LEN(isp) ||
1067 		    sp->req_handle < 1) {
1068 			PRINTF("%s: bad request handle %d\n", isp->isp_name,
1069 				sp->req_handle);
1070 			ISP_WRITE(isp, INMAILBOX5, optr);
1071 			continue;
1072 		}
1073 		xs = (ISP_SCSI_XFER_T *) isp->isp_xflist[sp->req_handle - 1];
1074 		if (xs == NULL) {
1075 			PRINTF("%s: NULL xs in xflist\n", isp->isp_name);
1076 			ISP_WRITE(isp, INMAILBOX5, optr);
1077 			continue;
1078 		}
1079 		isp->isp_xflist[sp->req_handle - 1] = NULL;
1080 		if (sp->req_status_flags & RQSTF_BUS_RESET) {
1081 			isp->isp_sendmarker = 1;
1082 		}
1083 		if (buddaboom) {
1084 			XS_SETERR(xs, HBA_BOTCH);
1085 		}
1086 		XS_STS(xs) = sp->req_scsi_status & 0xff;
1087 		if (isp->isp_type & ISP_HA_SCSI) {
1088 			if (sp->req_state_flags & RQSF_GOT_SENSE) {
1089 				bcopy(sp->req_sense_data, XS_SNSP(xs),
1090 					XS_SNSLEN(xs));
1091 				XS_SNS_IS_VALID(xs);
1092 			}
1093 		} else {
1094 			if (XS_STS(xs) == SCSI_CHECK) {
1095 				XS_SNS_IS_VALID(xs);
1096 				bcopy(sp->req_sense_data, XS_SNSP(xs),
1097 					XS_SNSLEN(xs));
1098 				sp->req_state_flags |= RQSF_GOT_SENSE;
1099 			}
1100 		}
1101 		if (XS_NOERR(xs) && XS_STS(xs) == SCSI_BUSY) {
1102 			XS_SETERR(xs, HBA_TGTBSY);
1103 		}
1104 
1105 		if (sp->req_header.rqs_entry_type == RQSTYPE_RESPONSE) {
1106 			if (XS_NOERR(xs) && sp->req_completion_status)
1107 				isp_parse_status(isp, sp, xs);
1108 		} else {
1109 			PRINTF("%s: unknown return %x\n", isp->isp_name,
1110 				sp->req_header.rqs_entry_type);
1111 			if (XS_NOERR(xs))
1112 				XS_SETERR(xs, HBA_BOTCH);
1113 		}
1114 		if (isp->isp_type & ISP_HA_SCSI) {
1115 			XS_RESID(xs) = sp->req_resid;
1116 		} else if (sp->req_scsi_status & RQCS_RU) {
1117 			XS_RESID(xs) = sp->req_resid;
1118 			IDPRINTF(4, ("%s: cnt %d rsd %d\n", isp->isp_name,
1119 				XS_XFRLEN(xs), sp->req_resid));
1120 		}
1121 		if (XS_XFRLEN(xs)) {
1122 			ISP_DMAFREE(isp, xs, sp->req_handle - 1);
1123 		}
1124 		if ((isp->isp_dblev >= 5) ||
1125 		    (isp->isp_dblev > 2 && !XS_NOERR(xs))) {
1126 			PRINTF("%s(%d.%d): FIN%d cmd0x%x len%d resid%d STS %x",
1127 			    isp->isp_name, XS_TGT(xs), XS_LUN(xs),
1128 			    sp->req_header.rqs_seqno, *(u_char *) XS_CDBP(xs),
1129 			    XS_XFRLEN(xs), XS_RESID(xs), XS_STS(xs));
1130 			if (sp->req_state_flags & RQSF_GOT_SENSE) {
1131 				PRINTF(" Skey: %x", XS_SNSKEY(xs));
1132 				if (!(XS_IS_SNS_VALID(xs))) {
1133 					PRINTF(" BUT NOT SET");
1134 				}
1135 			}
1136 			PRINTF(" XS_ERR(xs) %d\n", XS_ERR(xs));
1137 		}
1138 		ISP_WRITE(isp, INMAILBOX5, optr);
1139 		isp->isp_nactive--;
1140 		if (isp->isp_nactive < 0)
1141 			isp->isp_nactive = 0;
1142 		XS_CMD_DONE(xs);
1143 	}
1144 	isp->isp_residx = optr;
1145 	return (1);
1146 }
1147 
1148 /*
1149  * Support routines.
1150  */
1151 
1152 static void
1153 isp_parse_status(isp, sp, xs)
1154 	struct ispsoftc *isp;
1155 	ispstatusreq_t *sp;
1156 	ISP_SCSI_XFER_T *xs;
1157 {
1158 	switch (sp->req_completion_status) {
1159 	case RQCS_COMPLETE:
1160 		XS_SETERR(xs, HBA_NOERROR);
1161 		return;
1162 
1163 	case RQCS_INCOMPLETE:
1164 		if ((sp->req_state_flags & RQSF_GOT_TARGET) == 0) {
1165 			XS_SETERR(xs, HBA_SELTIMEOUT);
1166 			return;
1167 		}
1168 		PRINTF("%s: incomplete, state %x\n",
1169 			isp->isp_name, sp->req_state_flags);
1170 		break;
1171 
1172 	case RQCS_TRANSPORT_ERROR:
1173 		PRINTF("%s: transport error\n", isp->isp_name);
1174 		isp_prtstst(sp);
1175 		break;
1176 
1177 	case RQCS_DATA_OVERRUN:
1178 		if (isp->isp_type & ISP_HA_FC) {
1179 			XS_RESID(xs) = sp->req_resid;
1180 			break;
1181 		}
1182 		XS_SETERR(xs, HBA_NOERROR);
1183 		return;
1184 
1185 	case RQCS_DATA_UNDERRUN:
1186 		if (isp->isp_type & ISP_HA_FC) {
1187 			XS_RESID(xs) = sp->req_resid;
1188 			/* an UNDERRUN is not a botch ??? */
1189 		}
1190 		XS_SETERR(xs, HBA_NOERROR);
1191 		return;
1192 
1193 	case RQCS_TIMEOUT:
1194 		XS_SETERR(xs, HBA_CMDTIMEOUT);
1195 		return;
1196 
1197 	case RQCS_RESET_OCCURRED:
1198 		PRINTF("%s: reset occurred, %d active\n", isp->isp_name,
1199 			isp->isp_nactive);
1200 		isp->isp_sendmarker = 1;
1201 		XS_SETERR(xs, HBA_BUSRESET);
1202 		return;
1203 
1204 	case RQCS_ABORTED:
1205 		PRINTF("%s: command aborted\n", isp->isp_name);
1206 		isp->isp_sendmarker = 1;
1207 		XS_SETERR(xs, HBA_ABORTED);
1208 		return;
1209 
1210 	case RQCS_PORT_UNAVAILABLE:
1211 		/*
1212 		 * No such port on the loop. Moral equivalent of SELTIMEO
1213 		 */
1214 		XS_SETERR(xs, HBA_SELTIMEOUT);
1215 		return;
1216 
1217 	case RQCS_PORT_LOGGED_OUT:
1218 		PRINTF("%s: port logout for target %d\n",
1219 			isp->isp_name, XS_TGT(xs));
1220 		break;
1221 
1222 	case RQCS_PORT_CHANGED:
1223 		PRINTF("%s: port changed for target %d\n",
1224 			isp->isp_name, XS_TGT(xs));
1225 		break;
1226 
1227 	case RQCS_PORT_BUSY:
1228 		PRINTF("%s: port busy for target %d\n",
1229 			isp->isp_name, XS_TGT(xs));
1230 		XS_SETERR(xs, HBA_TGTBSY);
1231 		return;
1232 
1233 	default:
1234 		PRINTF("%s: comp status %x\n", isp->isp_name,
1235 		       sp->req_completion_status);
1236 		break;
1237 	}
1238 	XS_SETERR(xs, HBA_BOTCH);
1239 }
1240 
1241 #define	HINIB(x)			((x) >> 0x4)
1242 #define	LONIB(x)			((x)  & 0xf)
1243 #define MAKNIB(a, b)			(((a) << 4) | (b))
1244 static u_int8_t mbpcnt[] = {
1245 	MAKNIB(1, 1),	/* 0x00: MBOX_NO_OP */
1246 	MAKNIB(5, 5),	/* 0x01: MBOX_LOAD_RAM */
1247 	MAKNIB(2, 0),	/* 0x02: MBOX_EXEC_FIRMWARE */
1248 	MAKNIB(5, 5),	/* 0x03: MBOX_DUMP_RAM */
1249 	MAKNIB(3, 3),	/* 0x04: MBOX_WRITE_RAM_WORD */
1250 	MAKNIB(2, 3),	/* 0x05: MBOX_READ_RAM_WORD */
1251 	MAKNIB(6, 6),	/* 0x06: MBOX_MAILBOX_REG_TEST */
1252 	MAKNIB(2, 3),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
1253 	MAKNIB(1, 3),	/* 0x08: MBOX_ABOUT_FIRMWARE */
1254 	MAKNIB(0, 0),	/* 0x09: */
1255 	MAKNIB(0, 0),	/* 0x0a: */
1256 	MAKNIB(0, 0),	/* 0x0b: */
1257 	MAKNIB(0, 0),	/* 0x0c: */
1258 	MAKNIB(0, 0),	/* 0x0d: */
1259 	MAKNIB(1, 2),	/* 0x0e: MBOX_CHECK_FIRMWARE */
1260 	MAKNIB(0, 0),	/* 0x0f: */
1261 	MAKNIB(5, 5),	/* 0x10: MBOX_INIT_REQ_QUEUE */
1262 	MAKNIB(6, 6),	/* 0x11: MBOX_INIT_RES_QUEUE */
1263 	MAKNIB(4, 4),	/* 0x12: MBOX_EXECUTE_IOCB */
1264 	MAKNIB(2, 2),	/* 0x13: MBOX_WAKE_UP	*/
1265 	MAKNIB(1, 6),	/* 0x14: MBOX_STOP_FIRMWARE */
1266 	MAKNIB(4, 4),	/* 0x15: MBOX_ABORT */
1267 	MAKNIB(2, 2),	/* 0x16: MBOX_ABORT_DEVICE */
1268 	MAKNIB(3, 3),	/* 0x17: MBOX_ABORT_TARGET */
1269 	MAKNIB(2, 2),	/* 0x18: MBOX_BUS_RESET */
1270 	MAKNIB(2, 3),	/* 0x19: MBOX_STOP_QUEUE */
1271 	MAKNIB(2, 3),	/* 0x1a: MBOX_START_QUEUE */
1272 	MAKNIB(2, 3),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
1273 	MAKNIB(2, 3),	/* 0x1c: MBOX_ABORT_QUEUE */
1274 	MAKNIB(2, 4),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
1275 	MAKNIB(0, 0),	/* 0x1e: */
1276 	MAKNIB(1, 3),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
1277 	MAKNIB(1, 2),	/* 0x20: MBOX_GET_INIT_SCSI_ID */
1278 	MAKNIB(1, 2),	/* 0x21: MBOX_GET_SELECT_TIMEOUT */
1279 	MAKNIB(1, 3),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
1280 	MAKNIB(1, 2),	/* 0x23: MBOX_GET_TAG_AGE_LIMIT */
1281 	MAKNIB(1, 2),	/* 0x24: MBOX_GET_CLOCK_RATE */
1282 	MAKNIB(1, 2),	/* 0x25: MBOX_GET_ACT_NEG_STATE */
1283 	MAKNIB(1, 2),	/* 0x26: MBOX_GET_ASYNC_DATA_SETUP_TIME */
1284 	MAKNIB(1, 3),	/* 0x27: MBOX_GET_PCI_PARAMS */
1285 	MAKNIB(2, 4),	/* 0x28: MBOX_GET_TARGET_PARAMS */
1286 	MAKNIB(2, 4),	/* 0x29: MBOX_GET_DEV_QUEUE_PARAMS */
1287 	MAKNIB(0, 0),	/* 0x2a: */
1288 	MAKNIB(0, 0),	/* 0x2b: */
1289 	MAKNIB(0, 0),	/* 0x2c: */
1290 	MAKNIB(0, 0),	/* 0x2d: */
1291 	MAKNIB(0, 0),	/* 0x2e: */
1292 	MAKNIB(0, 0),	/* 0x2f: */
1293 	MAKNIB(2, 2),	/* 0x30: MBOX_SET_INIT_SCSI_ID */
1294 	MAKNIB(2, 2),	/* 0x31: MBOX_SET_SELECT_TIMEOUT */
1295 	MAKNIB(3, 3),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
1296 	MAKNIB(2, 2),	/* 0x33: MBOX_SET_TAG_AGE_LIMIT */
1297 	MAKNIB(2, 2),	/* 0x34: MBOX_SET_CLOCK_RATE */
1298 	MAKNIB(2, 2),	/* 0x35: MBOX_SET_ACTIVE_NEG_STATE */
1299 	MAKNIB(2, 2),	/* 0x36: MBOX_SET_ASYNC_DATA_SETUP_TIME */
1300 	MAKNIB(3, 3),	/* 0x37: MBOX_SET_PCI_CONTROL_PARAMS */
1301 	MAKNIB(4, 4),	/* 0x38: MBOX_SET_TARGET_PARAMS */
1302 	MAKNIB(4, 4),	/* 0x39: MBOX_SET_DEV_QUEUE_PARAMS */
1303 	MAKNIB(0, 0),	/* 0x3a: */
1304 	MAKNIB(0, 0),	/* 0x3b: */
1305 	MAKNIB(0, 0),	/* 0x3c: */
1306 	MAKNIB(0, 0),	/* 0x3d: */
1307 	MAKNIB(0, 0),	/* 0x3e: */
1308 	MAKNIB(0, 0),	/* 0x3f: */
1309 	MAKNIB(1, 2),	/* 0x40: MBOX_RETURN_BIOS_BLOCK_ADDR */
1310 	MAKNIB(6, 1),	/* 0x41: MBOX_WRITE_FOUR_RAM_WORDS */
1311 	MAKNIB(2, 3),	/* 0x42: MBOX_EXEC_BIOS_IOCB */
1312 	MAKNIB(0, 0),	/* 0x43: */
1313 	MAKNIB(0, 0),	/* 0x44: */
1314 	MAKNIB(0, 0),	/* 0x45: */
1315 	MAKNIB(0, 0),	/* 0x46: */
1316 	MAKNIB(0, 0),	/* 0x47: */
1317 	MAKNIB(0, 0),	/* 0x48: */
1318 	MAKNIB(0, 0),	/* 0x49: */
1319 	MAKNIB(0, 0),	/* 0x4a: */
1320 	MAKNIB(0, 0),	/* 0x4b: */
1321 	MAKNIB(0, 0),	/* 0x4c: */
1322 	MAKNIB(0, 0),	/* 0x4d: */
1323 	MAKNIB(0, 0),	/* 0x4e: */
1324 	MAKNIB(0, 0),	/* 0x4f: */
1325 	MAKNIB(0, 0),	/* 0x50: */
1326 	MAKNIB(0, 0),	/* 0x51: */
1327 	MAKNIB(0, 0),	/* 0x52: */
1328 	MAKNIB(0, 0),	/* 0x53: */
1329 	MAKNIB(8, 0),	/* 0x54: MBOX_EXEC_COMMAND_IOCB_A64 */
1330 	MAKNIB(0, 0),	/* 0x55: */
1331 	MAKNIB(0, 0),	/* 0x56: */
1332 	MAKNIB(0, 0),	/* 0x57: */
1333 	MAKNIB(0, 0),	/* 0x58: */
1334 	MAKNIB(0, 0),	/* 0x59: */
1335 	MAKNIB(0, 0),	/* 0x5a: */
1336 	MAKNIB(0, 0),	/* 0x5b: */
1337 	MAKNIB(0, 0),	/* 0x5c: */
1338 	MAKNIB(0, 0),	/* 0x5d: */
1339 	MAKNIB(0, 0),	/* 0x5e: */
1340 	MAKNIB(0, 0),	/* 0x5f: */
1341 	MAKNIB(8, 6),	/* 0x60: MBOX_INIT_FIRMWARE */
1342 	MAKNIB(0, 0),	/* 0x60: MBOX_GET_INIT_CONTROL_BLOCK  (FORMAT?) */
1343 	MAKNIB(2, 1),	/* 0x62: MBOX_INIT_LIP */
1344 	MAKNIB(8, 1),	/* 0x63: MBOX_GET_FC_AL_POSITION_MAP */
1345 	MAKNIB(8, 1),	/* 0x64: MBOX_GET_PORT_DB */
1346 	MAKNIB(3, 1),	/* 0x65: MBOX_CLEAR_ACA */
1347 	MAKNIB(3, 1),	/* 0x66: MBOX_TARGET_RESET */
1348 	MAKNIB(3, 1),	/* 0x67: MBOX_CLEAR_TASK_SET */
1349 	MAKNIB(3, 1),	/* 0x69: MBOX_ABORT_TASK_SET */
1350 	MAKNIB(1, 2)	/* 0x69: MBOX_GET_FW_STATE */
1351 };
1352 #define	NMBCOM	(sizeof (mbpcnt) / sizeof (mbpcnt[0]))
1353 
1354 static void
1355 isp_mboxcmd(isp, mbp)
1356 	struct ispsoftc *isp;
1357 	mbreg_t *mbp;
1358 {
1359 	int outparam, inparam;
1360 	int loops;
1361 	u_int8_t opcode;
1362 
1363 	if (mbp->param[0] == ISP2100_SET_PCI_PARAM) {
1364 		opcode = mbp->param[0] = MBOX_SET_PCI_PARAMETERS;
1365 		inparam = 4;
1366 		outparam = 4;
1367 		goto command_known;
1368 	} else if (mbp->param[0] > NMBCOM) {
1369 		PRINTF("%s: bad command %x\n", isp->isp_name, mbp->param[0]);
1370 		return;
1371 	}
1372 
1373 	opcode = mbp->param[0];
1374 	inparam = HINIB(mbpcnt[mbp->param[0]]);
1375 	outparam =  LONIB(mbpcnt[mbp->param[0]]);
1376 
1377 	if (inparam == 0 && outparam == 0) {
1378 		PRINTF("%s: no parameters for %x\n", isp->isp_name,
1379 			mbp->param[0]);
1380 		return;
1381 	}
1382 
1383 
1384 command_known:
1385 	/*
1386 	 * Make sure we can send some words..
1387 	 */
1388 
1389 	loops = MBOX_DELAY_COUNT;
1390 	while ((ISP_READ(isp, HCCR) & HCCR_HOST_INT) != 0) {
1391 		SYS_DELAY(100);
1392 		if (--loops < 0) {
1393 			PRINTF("%s: isp_mboxcmd timeout #1\n", isp->isp_name);
1394 			return;
1395 		}
1396 	}
1397 
1398 	/*
1399 	 * Write input parameters
1400 	 */
1401 	switch (inparam) {
1402 	case 8: ISP_WRITE(isp, INMAILBOX7, mbp->param[7]); mbp->param[7] = 0;
1403 	case 7: ISP_WRITE(isp, INMAILBOX6, mbp->param[6]); mbp->param[6] = 0;
1404 	case 6: ISP_WRITE(isp, INMAILBOX5, mbp->param[5]); mbp->param[5] = 0;
1405 	case 5: ISP_WRITE(isp, INMAILBOX4, mbp->param[4]); mbp->param[4] = 0;
1406 	case 4: ISP_WRITE(isp, INMAILBOX3, mbp->param[3]); mbp->param[3] = 0;
1407 	case 3: ISP_WRITE(isp, INMAILBOX2, mbp->param[2]); mbp->param[2] = 0;
1408 	case 2: ISP_WRITE(isp, INMAILBOX1, mbp->param[1]); mbp->param[1] = 0;
1409 	case 1: ISP_WRITE(isp, INMAILBOX0, mbp->param[0]); mbp->param[0] = 0;
1410 	}
1411 
1412 	/*
1413 	 * Clear semaphore on mailbox registers
1414 	 */
1415 	ISP_WRITE(isp, BIU_SEMA, 0);
1416 
1417 	/*
1418 	 * Clear RISC int condition.
1419 	 */
1420 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
1421 
1422 	/*
1423 	 * Set Host Interrupt condition so that RISC will pick up mailbox regs.
1424 	 */
1425 	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
1426 
1427 	/*
1428 	 * Wait until RISC int is set, except 2100
1429 	 */
1430 	if ((isp->isp_type & ISP_HA_FC) == 0) {
1431 		loops = MBOX_DELAY_COUNT;
1432 		while ((ISP_READ(isp, BIU_ISR) & BIU_ISR_RISC_INT) == 0) {
1433 			SYS_DELAY(100);
1434 			if (--loops < 0) {
1435 				PRINTF("%s: isp_mboxcmd timeout #2\n",
1436 				    isp->isp_name);
1437 				return;
1438 			}
1439 		}
1440 	}
1441 
1442 	/*
1443 	 * Check to make sure that the semaphore has been set.
1444 	 */
1445 	loops = MBOX_DELAY_COUNT;
1446 	while ((ISP_READ(isp, BIU_SEMA) & 1) == 0) {
1447 		SYS_DELAY(100);
1448 		if (--loops < 0) {
1449 			PRINTF("%s: isp_mboxcmd timeout #3\n", isp->isp_name);
1450 			return;
1451 		}
1452 	}
1453 
1454 	/*
1455 	 * Make sure that the MBOX_BUSY has gone away
1456 	 */
1457 	loops = MBOX_DELAY_COUNT;
1458 	while (ISP_READ(isp, OUTMAILBOX0) == MBOX_BUSY) {
1459 		SYS_DELAY(100);
1460 		if (--loops < 0) {
1461 			PRINTF("%s: isp_mboxcmd timeout #4\n", isp->isp_name);
1462 			return;
1463 		}
1464 	}
1465 
1466 
1467 	/*
1468 	 * Pick up output parameters.
1469 	 */
1470 	switch (outparam) {
1471 	case 8: mbp->param[7] = ISP_READ(isp, OUTMAILBOX7);
1472 	case 7: mbp->param[6] = ISP_READ(isp, OUTMAILBOX6);
1473 	case 6: mbp->param[5] = ISP_READ(isp, OUTMAILBOX5);
1474 	case 5: mbp->param[4] = ISP_READ(isp, OUTMAILBOX4);
1475 	case 4: mbp->param[3] = ISP_READ(isp, OUTMAILBOX3);
1476 	case 3: mbp->param[2] = ISP_READ(isp, OUTMAILBOX2);
1477 	case 2: mbp->param[1] = ISP_READ(isp, OUTMAILBOX1);
1478 	case 1: mbp->param[0] = ISP_READ(isp, OUTMAILBOX0);
1479 	}
1480 
1481 	/*
1482 	 * Clear RISC int.
1483 	 */
1484 	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
1485 
1486 	/*
1487 	 * Release semaphore on mailbox registers
1488 	 */
1489 	ISP_WRITE(isp, BIU_SEMA, 0);
1490 
1491 	/*
1492 	 * Just to be chatty here...
1493 	 */
1494 	switch(mbp->param[0]) {
1495 	case MBOX_COMMAND_COMPLETE:
1496 		break;
1497 	case MBOX_INVALID_COMMAND:
1498 		/*
1499 		 * GET_CLOCK_RATE can fail a lot
1500 		 * So can a couple of other commands.
1501 		 */
1502 		if (isp->isp_dblev > 2  && opcode != MBOX_GET_CLOCK_RATE) {
1503 			PRINTF("%s: mbox cmd %x failed with INVALID_COMMAND\n",
1504 				isp->isp_name, opcode);
1505 		}
1506 		break;
1507 	case MBOX_HOST_INTERFACE_ERROR:
1508 		PRINTF("%s: mbox cmd %x failed with HOST_INTERFACE_ERROR\n",
1509 			isp->isp_name, opcode);
1510 		break;
1511 	case MBOX_TEST_FAILED:
1512 		PRINTF("%s: mbox cmd %x failed with TEST_FAILED\n",
1513 			isp->isp_name, opcode);
1514 		break;
1515 	case MBOX_COMMAND_ERROR:
1516 		PRINTF("%s: mbox cmd %x failed with COMMAND_ERROR\n",
1517 			isp->isp_name, opcode);
1518 		break;
1519 	case MBOX_COMMAND_PARAM_ERROR:
1520 		PRINTF("%s: mbox cmd %x failed with COMMAND_PARAM_ERROR\n",
1521 			isp->isp_name, opcode);
1522 		break;
1523 
1524 	case ASYNC_LIP_OCCURRED:
1525 		break;
1526 
1527 	default:
1528 		/*
1529 		 * The expected return of EXEC_FIRMWARE is zero.
1530 		 */
1531 		if ((opcode == MBOX_EXEC_FIRMWARE && mbp->param[0] != 0) ||
1532 		    (opcode != MBOX_EXEC_FIRMWARE)) {
1533 			PRINTF("%s: mbox cmd %x failed with error %x\n",
1534 				isp->isp_name, opcode, mbp->param[0]);
1535 		}
1536 		break;
1537 	}
1538 }
1539 
1540 static void
1541 isp_lostcmd(struct ispsoftc *isp, ISP_SCSI_XFER_T *xs, ispreq_t *req)
1542 {
1543 	mbreg_t mbs;
1544 
1545 	mbs.param[0] = MBOX_GET_FIRMWARE_STATUS;
1546 	isp_mboxcmd(isp, &mbs);
1547 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1548 		isp_dumpregs(isp, "couldn't GET FIRMWARE STATUS");
1549 		return;
1550 	}
1551 	if (mbs.param[1]) {
1552 		PRINTF("%s: %d commands on completion queue\n",
1553 		       isp->isp_name, mbs.param[1]);
1554 	}
1555 	if (XS_NULL(xs))
1556 		return;
1557 
1558 	mbs.param[0] = MBOX_GET_DEV_QUEUE_STATUS;
1559 	mbs.param[1] = XS_TGT(xs) << 8 | XS_LUN(xs);
1560 	isp_mboxcmd(isp, &mbs);
1561 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1562 		isp_dumpregs(isp, "couldn't GET DEVICE QUEUE STATUS");
1563 		return;
1564 	}
1565 	PRINTF("%s: lost command for target %d lun %d, %d active of %d, "
1566 		"Queue State: %x\n", isp->isp_name, XS_TGT(xs),
1567 		XS_LUN(xs), mbs.param[2], mbs.param[3], mbs.param[1]);
1568 
1569 	isp_dumpregs(isp, "lost command");
1570 	/*
1571 	 * XXX: Need to try and do something to recover.
1572 	 */
1573 }
1574 
1575 static void
1576 isp_dumpregs(struct ispsoftc *isp, const char *msg)
1577 {
1578 	PRINTF("%s: %s\n", isp->isp_name, msg);
1579 	if (isp->isp_type & ISP_HA_SCSI)
1580 		PRINTF("    biu_conf1=%x", ISP_READ(isp, BIU_CONF1));
1581 	else
1582 		PRINTF("    biu_csr=%x", ISP_READ(isp, BIU2100_CSR));
1583 	PRINTF(" biu_icr=%x biu_isr=%x biu_sema=%x ", ISP_READ(isp, BIU_ICR),
1584 	       ISP_READ(isp, BIU_ISR), ISP_READ(isp, BIU_SEMA));
1585 	PRINTF("risc_hccr=%x\n", ISP_READ(isp, HCCR));
1586 
1587 	if (isp->isp_type & ISP_HA_SCSI) {
1588 		ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
1589 		PRINTF("    cdma_conf=%x cdma_sts=%x cdma_fifostat=%x\n",
1590 			ISP_READ(isp, CDMA_CONF), ISP_READ(isp, CDMA_STATUS),
1591 			ISP_READ(isp, CDMA_FIFO_STS));
1592 		PRINTF("    ddma_conf=%x ddma_sts=%x ddma_fifostat=%x\n",
1593 			ISP_READ(isp, DDMA_CONF), ISP_READ(isp, DDMA_STATUS),
1594 			ISP_READ(isp, DDMA_FIFO_STS));
1595 		PRINTF("    sxp_int=%x sxp_gross=%x sxp(scsi_ctrl)=%x\n",
1596 			ISP_READ(isp, SXP_INTERRUPT),
1597 			ISP_READ(isp, SXP_GROSS_ERR),
1598 			ISP_READ(isp, SXP_PINS_CONTROL));
1599 		ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
1600 	}
1601 	ISP_DUMPREGS(isp);
1602 }
1603 
1604 static void
1605 isp_fw_state(struct ispsoftc *isp)
1606 {
1607 	mbreg_t mbs;
1608 	if (isp->isp_type & ISP_HA_FC) {
1609 		int once = 0;
1610 		fcparam *fcp = isp->isp_param;
1611 again:
1612 		mbs.param[0] = MBOX_GET_FW_STATE;
1613 		isp_mboxcmd(isp, &mbs);
1614 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1615 			if (mbs.param[0] == ASYNC_LIP_OCCURRED) {
1616 				if (!once++) {
1617 					goto again;
1618 				}
1619 			}
1620 			isp_dumpregs(isp, "GET FIRMWARE STATE failed");
1621 			return;
1622 		}
1623 		fcp->isp_fwstate = mbs.param[1];
1624 	}
1625 }
1626 
1627 static void
1628 isp_setdparm(struct ispsoftc *isp)
1629 {
1630 	int i;
1631 	mbreg_t mbs;
1632 	sdparam *sdp;
1633 
1634 	isp->isp_fwrev = 0;
1635 	if (isp->isp_type & ISP_HA_FC) {
1636 		/*
1637 		 * ROM in 2100 doesn't appear to support ABOUT_FIRMWARE
1638 		 */
1639 		return;
1640 	}
1641 
1642 	mbs.param[0] = MBOX_ABOUT_FIRMWARE;
1643 	isp_mboxcmd(isp, &mbs);
1644 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1645 		IDPRINTF(3, ("1st ABOUT FIRMWARE command failed"));
1646 	} else {
1647 		isp->isp_fwrev =
1648 			(((u_int16_t) mbs.param[1]) << 10) + mbs.param[2];
1649 	}
1650 
1651 
1652 	sdp = (sdparam *) isp->isp_param;
1653 	/*
1654 	 * Try and get old clock rate out before we hit the
1655 	 * chip over the head- but if and only if we don't
1656 	 * know our desired clock rate.
1657 	 */
1658 	if (isp->isp_mdvec->dv_clock == 0) {
1659 		mbs.param[0] = MBOX_GET_CLOCK_RATE;
1660 		isp_mboxcmd(isp, &mbs);
1661 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1662 			sdp->isp_clock = mbs.param[1];
1663 			PRINTF("%s: using board clock 0x%x\n",
1664 				isp->isp_name, sdp->isp_clock);
1665 		}
1666 	} else {
1667 		sdp->isp_clock = isp->isp_mdvec->dv_clock;
1668 	}
1669 
1670 	mbs.param[0] = MBOX_GET_ACT_NEG_STATE;
1671 	isp_mboxcmd(isp, &mbs);
1672 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1673 		IDPRINTF(2, ("could not GET ACT NEG STATE\n"));
1674 		sdp->isp_req_ack_active_neg = 1;
1675 		sdp->isp_data_line_active_neg = 1;
1676 	} else {
1677 		sdp->isp_req_ack_active_neg = (mbs.param[1] >> 4) & 0x1;
1678 		sdp->isp_data_line_active_neg = (mbs.param[1] >> 5) & 0x1;
1679 	}
1680 	for (i = 0; i < MAX_TARGETS; i++) {
1681 		mbs.param[0] = MBOX_GET_TARGET_PARAMS;
1682 		mbs.param[1] = i << 8;
1683 		isp_mboxcmd(isp, &mbs);
1684 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1685 			IDPRINTF(2, ("cannot get params for target %d\n", i));
1686 			sdp->isp_devparam[i].sync_period =
1687 				ISP_10M_SYNCPARMS & 0xff;
1688 			sdp->isp_devparam[i].sync_offset =
1689 				ISP_10M_SYNCPARMS >> 8;
1690 			sdp->isp_devparam[i].dev_flags = DPARM_DEFAULT;
1691 		} else {
1692 			IDPRINTF(3, ("\%s: target %d - flags 0x%x, sync %x\n",
1693 			       isp->isp_name, i, mbs.param[2], mbs.param[3]));
1694 			sdp->isp_devparam[i].dev_flags = mbs.param[2] >> 8;
1695 			/*
1696 			 * The maximum period we can really see
1697 			 * here is 100 (decimal), or 400 ns.
1698 			 * For some unknown reason we sometimes
1699 			 * get back wildass numbers from the
1700 			 * boot device's paramaters.
1701 			 */
1702 			if ((mbs.param[3] & 0xff) <= 0x64) {
1703 				sdp->isp_devparam[i].sync_period =
1704 					mbs.param[3] & 0xff;
1705 				sdp->isp_devparam[i].sync_offset =
1706 					mbs.param[3] >> 8;
1707 			}
1708 		}
1709 	}
1710 
1711 	/*
1712 	 * Set Default Host Adapter Parameters
1713 	 * XXX: Should try and get them out of NVRAM
1714 	 */
1715 	sdp->isp_adapter_enabled = 1;
1716 	sdp->isp_cmd_dma_burst_enable = 1;
1717 	sdp->isp_data_dma_burst_enabl = 1;
1718 	sdp->isp_fifo_threshold = 2;
1719 	sdp->isp_initiator_id = 7;
1720 	sdp->isp_async_data_setup = 6;
1721 	sdp->isp_selection_timeout = 250;
1722 	sdp->isp_max_queue_depth = 128;
1723 	sdp->isp_tag_aging = 8;
1724 	sdp->isp_bus_reset_delay = 3;
1725 	sdp->isp_retry_count = 0;
1726 	sdp->isp_retry_delay = 1;
1727 
1728 	for (i = 0; i < MAX_TARGETS; i++) {
1729 		sdp->isp_devparam[i].exc_throttle = 16;
1730 		sdp->isp_devparam[i].dev_enable = 1;
1731 	}
1732 }
1733 
1734 static void
1735 isp_phoenix(struct ispsoftc *isp)
1736 {
1737 	ISP_SCSI_XFER_T *tlist[MAXISPREQUEST], *xs;
1738 	int i;
1739 
1740 	for (i = 0; i < RQUEST_QUEUE_LEN(isp); i++) {
1741 		tlist[i] = (ISP_SCSI_XFER_T *) isp->isp_xflist[i];
1742 	}
1743 	isp_reset(isp);
1744 	isp_init(isp);
1745 	isp->isp_state = ISP_RUNSTATE;
1746 
1747 	for (i = 0; i < RQUEST_QUEUE_LEN(isp); i++) {
1748 		xs = tlist[i];
1749 		if (XS_NULL(xs))
1750 			continue;
1751 		isp->isp_nactive--;
1752 		if (isp->isp_nactive < 0)
1753 			isp->isp_nactive = 0;
1754 		XS_RESID(xs) = XS_XFRLEN(xs);
1755 		XS_SETERR(xs, HBA_BOTCH);
1756 		XS_CMD_DONE(xs);
1757 	}
1758 }
1759 
1760 void
1761 isp_watch(void *arg)
1762 {
1763 	int i;
1764 	struct ispsoftc *isp = arg;
1765 	ISP_SCSI_XFER_T *xs;
1766 	ISP_LOCKVAL_DECL;
1767 
1768 	/*
1769 	 * Look for completely dead commands (but not polled ones).
1770 	 */
1771 	ISP_LOCK;
1772 	for (i = 0; i < RQUEST_QUEUE_LEN(isp); i++) {
1773 		if ((xs = (ISP_SCSI_XFER_T *) isp->isp_xflist[i]) == NULL) {
1774 			continue;
1775 		}
1776 		if (XS_POLLDCMD(xs))
1777 			continue;
1778 		if (XS_TIME(xs) == 0) {
1779 			continue;
1780 		}
1781 		XS_TIME(xs) -= (WATCH_INTERVAL * 1000);
1782 		/*
1783 		 * Avoid later thinking that this
1784 		 * transaction is not being timed.
1785 		 * Then give ourselves to watchdog
1786 		 * periods of grace.
1787 		 */
1788 		if (XS_TIME(xs) == 0)
1789 			XS_TIME(xs) = 1;
1790 		else if (XS_TIME(xs) > -(2 * WATCH_INTERVAL * 1000)) {
1791 			continue;
1792 		}
1793 		if (isp_abortcmd(isp, i)) {
1794 			PRINTF("%s: isp_watch failed to abort command\n",
1795 			       isp->isp_name);
1796 			isp_phoenix(isp);
1797 			break;
1798 		}
1799 	}
1800 	ISP_UNLOCK;
1801 	RESTART_WATCHDOG(isp_watch, isp);
1802 }
1803 
1804 static void
1805 isp_prtstst(ispstatusreq_t *sp)
1806 {
1807 	PRINTF("states->");
1808 	if (sp->req_state_flags & RQSF_GOT_BUS)
1809 		PRINTF("GOT_BUS ");
1810 	if (sp->req_state_flags & RQSF_GOT_TARGET)
1811 		PRINTF("GOT_TGT ");
1812 	if (sp->req_state_flags & RQSF_SENT_CDB)
1813 		PRINTF("SENT_CDB ");
1814 	if (sp->req_state_flags & RQSF_XFRD_DATA)
1815 		PRINTF("XFRD_DATA ");
1816 	if (sp->req_state_flags & RQSF_GOT_STATUS)
1817 		PRINTF("GOT_STS ");
1818 	if (sp->req_state_flags & RQSF_GOT_SENSE)
1819 		PRINTF("GOT_SNS ");
1820 	if (sp->req_state_flags & RQSF_XFER_COMPLETE)
1821 		PRINTF("XFR_CMPLT ");
1822 	PRINTF("\n");
1823 	PRINTF("status->");
1824 	if (sp->req_status_flags & RQSTF_DISCONNECT)
1825 		PRINTF("Disconnect ");
1826 	if (sp->req_status_flags & RQSTF_SYNCHRONOUS)
1827 		PRINTF("Sync_xfr ");
1828 	if (sp->req_status_flags & RQSTF_PARITY_ERROR)
1829 		PRINTF("Parity ");
1830 	if (sp->req_status_flags & RQSTF_BUS_RESET)
1831 		PRINTF("Bus_Reset ");
1832 	if (sp->req_status_flags & RQSTF_DEVICE_RESET)
1833 		PRINTF("Device_Reset ");
1834 	if (sp->req_status_flags & RQSTF_ABORTED)
1835 		PRINTF("Aborted ");
1836 	if (sp->req_status_flags & RQSTF_TIMEOUT)
1837 		PRINTF("Timeout ");
1838 	if (sp->req_status_flags & RQSTF_NEGOTIATION)
1839 		PRINTF("Negotiation ");
1840 	PRINTF("\n");
1841 }
1842