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