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