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