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