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