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