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