isp.c revision 70489
1/* $FreeBSD: head/sys/dev/isp/isp.c 70489 2000-12-29 19:12:44Z 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 *));
126#endif
127static void isp_fw_state __P((struct ispsoftc *));
128static void isp_mboxcmd __P((struct ispsoftc *, mbreg_t *, int));
129
130static void isp_update __P((struct ispsoftc *));
131static void isp_update_bus __P((struct ispsoftc *, int));
132static void isp_setdfltparm __P((struct ispsoftc *, int));
133static int isp_read_nvram __P((struct ispsoftc *));
134static void isp_rdnvram_word __P((struct ispsoftc *, int, u_int16_t *));
135static void isp_parse_nvram_1020 __P((struct ispsoftc *, u_int8_t *));
136static void isp_parse_nvram_1080 __P((struct ispsoftc *, int, u_int8_t *));
137static void isp_parse_nvram_12160 __P((struct ispsoftc *, int, u_int8_t *));
138static void isp_parse_nvram_2100 __P((struct ispsoftc *, u_int8_t *));
139
140/*
141 * Reset Hardware.
142 *
143 * Hit the chip over the head, download new f/w if available and set it running.
144 *
145 * Locking done elsewhere.
146 */
147void
148isp_reset(isp)
149	struct ispsoftc *isp;
150{
151	mbreg_t mbs;
152	int loops, i, touched, dodnld = 1;
153	char *revname;
154
155	isp->isp_state = ISP_NILSTATE;
156
157
158	/*
159	 * Basic types (SCSI, FibreChannel and PCI or SBus)
160	 * have been set in the MD code. We figure out more
161	 * here.
162	 *
163	 * After we've fired this chip up, zero out the conf1 register
164	 * for SCSI adapters and do other settings for the 2100.
165	 */
166
167	/*
168	 * Get the current running firmware revision out of the
169	 * chip before we hit it over the head (if this is our
170	 * first time through). Note that we store this as the
171	 * 'ROM' firmware revision- which it may not be. In any
172	 * case, we don't really use this yet, but we may in
173	 * the future.
174	 */
175	if ((touched = isp->isp_touched) == 0) {
176		/*
177		 * First see whether or not we're sitting in the ISP PROM.
178		 * If we've just been reset, we'll have the string "ISP   "
179		 * spread through outgoing mailbox registers 1-3.
180		 */
181		if (ISP_READ(isp, OUTMAILBOX1) != 0x4953 ||
182		    ISP_READ(isp, OUTMAILBOX2) != 0x5020 ||
183		    ISP_READ(isp, OUTMAILBOX3) != 0x2020) {
184			/*
185			 * Just in case it was paused...
186			 */
187			ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
188			mbs.param[0] = MBOX_ABOUT_FIRMWARE;
189			isp_mboxcmd(isp, &mbs, MBOX_COMMAND_ERROR);
190			/*
191			 * This *shouldn't* fail.....
192			 */
193			if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
194				isp->isp_romfw_rev[0] = mbs.param[1];
195				isp->isp_romfw_rev[1] = mbs.param[2];
196				isp->isp_romfw_rev[2] = mbs.param[3];
197			}
198		}
199		isp->isp_touched = 1;
200	}
201
202	DISABLE_INTS(isp);
203
204	/*
205	 * Put the board into PAUSE mode (so we can read the SXP registers).
206	 */
207	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
208
209	if (IS_FC(isp)) {
210		revname = "2X00";
211		switch (isp->isp_type) {
212		case ISP_HA_FC_2100:
213			revname[1] = '1';
214			break;
215		case ISP_HA_FC_2200:
216			revname[1] = '2';
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#if	0
1391		if (isp->isp_rfabric == 0) {
1392			isp_i_register_fc4_type(isp);
1393		}
1394#endif
1395	} else
1396#endif
1397	{
1398		fcp->isp_portid = mbs.param[2];
1399		fcp->isp_onfabric = 0;
1400#if	0
1401		isp->isp_rfabric = 0;
1402#endif
1403		fcp->portdb[FL_PORT_ID].valid = 0;
1404	}
1405
1406	isp_prt(isp, ISP_LOGINFO, topology, fcp->isp_loopid, fcp->isp_alpa,
1407	    fcp->isp_portid, fcp->isp_loopstate, toponames[fcp->isp_topo]);
1408
1409	return (0);
1410}
1411
1412static char *
1413isp2100_fw_statename(state)
1414	int state;
1415{
1416	switch(state) {
1417	case FW_CONFIG_WAIT:	return "Config Wait";
1418	case FW_WAIT_AL_PA:	return "Waiting for AL_PA";
1419	case FW_WAIT_LOGIN:	return "Wait Login";
1420	case FW_READY:		return "Ready";
1421	case FW_LOSS_OF_SYNC:	return "Loss Of Sync";
1422	case FW_ERROR:		return "Error";
1423	case FW_REINIT:		return "Re-Init";
1424	case FW_NON_PART:	return "Nonparticipating";
1425	default:		return "?????";
1426	}
1427}
1428
1429static int
1430isp_same_lportdb(a, b)
1431	struct lportdb *a, *b;
1432{
1433	/*
1434	 * We decide two lports are the same if they have non-zero and
1435	 * identical port WWNs and identical loop IDs.
1436	 */
1437
1438	if (a->port_wwn == 0 || a->port_wwn != b->port_wwn ||
1439	    a->loopid != b->loopid || a->roles != b->roles) {
1440		return (0);
1441	} else {
1442		return (1);
1443	}
1444}
1445
1446/*
1447 * Synchronize our soft copy of the port database with what the f/w thinks
1448 * (with a view toward possibly for a specific target....)
1449 */
1450
1451static int
1452isp_pdb_sync(isp, target)
1453	struct ispsoftc *isp;
1454	int target;
1455{
1456	struct lportdb *lp, *tport;
1457	fcparam *fcp = isp->isp_param;
1458	isp_pdb_t pdb;
1459	int loopid, prange, lim;
1460
1461#ifdef	ISP2100_FABRIC
1462	/*
1463	 * XXX: If we do this *after* building up our local port database,
1464	 * XXX: the commands simply don't work.
1465	 */
1466	/*
1467	 * (Re)discover all fabric devices
1468	 */
1469	if (fcp->isp_onfabric)
1470		(void) isp_scan_fabric(isp);
1471#endif
1472
1473
1474	switch (fcp->isp_topo) {
1475	case TOPO_F_PORT:
1476	case TOPO_PTP_STUB:
1477		prange = 0;
1478		break;
1479	case TOPO_N_PORT:
1480		prange = 2;
1481		break;
1482	default:
1483		prange = FL_PORT_ID;
1484		break;
1485	}
1486
1487	/*
1488	 * Run through the local loop ports and get port database info
1489	 * for each loop ID.
1490	 *
1491	 * There's a somewhat unexplained situation where the f/w passes back
1492	 * the wrong database entity- if that happens, just restart (up to
1493	 * FL_PORT_ID times).
1494	 */
1495	tport = fcp->tport;
1496
1497	/*
1498	 * make sure the temp port database is clean...
1499	 */
1500	MEMZERO((void *) tport, sizeof (tport));
1501
1502	for (lim = loopid = 0; loopid < prange; loopid++) {
1503		lp = &tport[loopid];
1504		lp->node_wwn = isp_get_portname(isp, loopid, 1);
1505		if (fcp->isp_loopstate < LOOP_PDB_RCVD)
1506			return (-1);
1507		if (lp->node_wwn == 0)
1508			continue;
1509		lp->port_wwn = isp_get_portname(isp, loopid, 0);
1510		if (fcp->isp_loopstate < LOOP_PDB_RCVD)
1511			return (-1);
1512		if (lp->port_wwn == 0) {
1513			lp->node_wwn = 0;
1514			continue;
1515		}
1516
1517		/*
1518		 * Get an entry....
1519		 */
1520		if (isp_getpdb(isp, loopid, &pdb) != 0) {
1521			if (fcp->isp_loopstate < LOOP_PDB_RCVD)
1522				return (-1);
1523			continue;
1524		}
1525
1526		if (fcp->isp_loopstate < LOOP_PDB_RCVD)
1527			return (-1);
1528
1529		/*
1530		 * If the returned database element doesn't match what we
1531		 * asked for, restart the process entirely (up to a point...).
1532		 */
1533		if (pdb.pdb_loopid != loopid) {
1534			loopid = 0;
1535			if (lim++ < FL_PORT_ID) {
1536				continue;
1537			}
1538			isp_prt(isp, ISP_LOGWARN,
1539			    "giving up on synchronizing the port database");
1540			return (-1);
1541		}
1542
1543		/*
1544		 * Save the pertinent info locally.
1545		 */
1546		lp->node_wwn =
1547		    (((u_int64_t)pdb.pdb_nodename[0]) << 56) |
1548		    (((u_int64_t)pdb.pdb_nodename[1]) << 48) |
1549		    (((u_int64_t)pdb.pdb_nodename[2]) << 40) |
1550		    (((u_int64_t)pdb.pdb_nodename[3]) << 32) |
1551		    (((u_int64_t)pdb.pdb_nodename[4]) << 24) |
1552		    (((u_int64_t)pdb.pdb_nodename[5]) << 16) |
1553		    (((u_int64_t)pdb.pdb_nodename[6]) <<  8) |
1554		    (((u_int64_t)pdb.pdb_nodename[7]));
1555		lp->port_wwn =
1556		    (((u_int64_t)pdb.pdb_portname[0]) << 56) |
1557		    (((u_int64_t)pdb.pdb_portname[1]) << 48) |
1558		    (((u_int64_t)pdb.pdb_portname[2]) << 40) |
1559		    (((u_int64_t)pdb.pdb_portname[3]) << 32) |
1560		    (((u_int64_t)pdb.pdb_portname[4]) << 24) |
1561		    (((u_int64_t)pdb.pdb_portname[5]) << 16) |
1562		    (((u_int64_t)pdb.pdb_portname[6]) <<  8) |
1563		    (((u_int64_t)pdb.pdb_portname[7]));
1564		lp->roles =
1565		    (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
1566		lp->portid = BITS2WORD(pdb.pdb_portid_bits);
1567		lp->loopid = pdb.pdb_loopid;
1568		/*
1569		 * Do a quick check to see whether this matches the saved port
1570		 * database for the same loopid. We do this here to save
1571		 * searching later (if possible). Note that this fails over
1572		 * time as things shuffle on the loop- we get the current
1573		 * loop state (where loop id as an index matches loop id in
1574		 * use) and then compare it to our saved database which
1575		 * never shifts.
1576		 */
1577		if (target >= 0 && isp_same_lportdb(lp, &fcp->portdb[target])) {
1578			lp->valid = 1;
1579		}
1580	}
1581
1582	/*
1583	 * If we get this far, we've settled our differences with the f/w
1584	 * and we can say that the loop state is ready.
1585	 */
1586	fcp->isp_loopstate = LOOP_READY;
1587
1588	/*
1589	 * Mark all of the permanent local loop database entries as invalid.
1590	 */
1591	for (loopid = 0; loopid < FL_PORT_ID; loopid++) {
1592		fcp->portdb[loopid].valid = 0;
1593	}
1594
1595	/*
1596	 * Now merge our local copy of the port database into our saved copy.
1597	 * Notify the outer layers of new devices arriving.
1598	 */
1599	for (loopid = 0; loopid < prange; loopid++) {
1600		int i;
1601
1602		/*
1603		 * If we don't have a non-zero Port WWN, we're not here.
1604		 */
1605		if (tport[loopid].port_wwn == 0) {
1606			continue;
1607		}
1608
1609		/*
1610		 * If we've already marked our tmp copy as valid,
1611		 * this means that we've decided that it's the
1612		 * same as our saved data base. This didn't include
1613		 * the 'valid' marking so we have set that here.
1614		 */
1615		if (tport[loopid].valid) {
1616			fcp->portdb[loopid].valid = 1;
1617			continue;
1618		}
1619
1620		/*
1621		 * For the purposes of deciding whether this is the
1622		 * 'same' device or not, we only search for an identical
1623		 * Port WWN. Node WWNs may or may not be the same as
1624		 * the Port WWN, and there may be multiple different
1625		 * Port WWNs with the same Node WWN. It would be chaos
1626		 * to have multiple identical Port WWNs, so we don't
1627		 * allow that.
1628		 */
1629
1630		for (i = 0; i < FL_PORT_ID; i++) {
1631			int j;
1632			if (fcp->portdb[i].port_wwn == 0)
1633				continue;
1634			if (fcp->portdb[i].port_wwn != tport[loopid].port_wwn)
1635				continue;
1636			/*
1637			 * We found this WWN elsewhere- it's changed
1638			 * loopids then. We don't change it's actual
1639			 * position in our cached port database- we
1640			 * just change the actual loop ID we'd use.
1641			 */
1642			if (fcp->portdb[i].loopid != loopid) {
1643				isp_prt(isp, ISP_LOGINFO, portshift, i,
1644				    fcp->portdb[i].loopid,
1645				    fcp->portdb[i].portid, loopid,
1646				    tport[loopid].portid);
1647			}
1648			fcp->portdb[i].portid = tport[loopid].portid;
1649			fcp->portdb[i].loopid = loopid;
1650			fcp->portdb[i].valid = 1;
1651			fcp->portdb[i].roles = tport[loopid].roles;
1652
1653			/*
1654			 * Now make sure this Port WWN doesn't exist elsewhere
1655			 * in the port database.
1656			 */
1657			for (j = i+1; j < FL_PORT_ID; j++) {
1658				if (fcp->portdb[i].port_wwn !=
1659				    fcp->portdb[j].port_wwn) {
1660					continue;
1661				}
1662				isp_prt(isp, ISP_LOGWARN, portdup, j, i);
1663				/*
1664				 * Invalidate the 'old' *and* 'new' ones.
1665				 * This is really harsh and not quite right,
1666				 * but if this happens, we really don't know
1667				 * who is what at this point.
1668				 */
1669				fcp->portdb[i].valid = 0;
1670				fcp->portdb[j].valid = 0;
1671			}
1672			break;
1673		}
1674
1675		/*
1676		 * If we didn't traverse the entire port database,
1677		 * then we found (and remapped) an existing entry.
1678		 * No need to notify anyone- go for the next one.
1679		 */
1680		if (i < FL_PORT_ID) {
1681			continue;
1682		}
1683
1684		/*
1685		 * We've not found this Port WWN anywhere. It's a new entry.
1686		 * See if we can leave it where it is (with target == loopid).
1687		 */
1688		if (fcp->portdb[loopid].port_wwn != 0) {
1689			for (lim = 0; lim < FL_PORT_ID; lim++) {
1690				if (fcp->portdb[lim].port_wwn == 0)
1691					break;
1692			}
1693			/* "Cannot Happen" */
1694			if (lim == FL_PORT_ID) {
1695				isp_prt(isp, ISP_LOGWARN, "Remap Overflow");
1696				continue;
1697			}
1698			i = lim;
1699		} else {
1700			i = loopid;
1701		}
1702
1703		/*
1704		 * NB:	The actual loopid we use here is loopid- we may
1705		 *	in fact be at a completely different index (target).
1706		 */
1707		fcp->portdb[i].loopid = loopid;
1708		fcp->portdb[i].port_wwn = tport[loopid].port_wwn;
1709		fcp->portdb[i].node_wwn = tport[loopid].node_wwn;
1710		fcp->portdb[i].roles = tport[loopid].roles;
1711		fcp->portdb[i].portid = tport[loopid].portid;
1712		fcp->portdb[i].valid = 1;
1713
1714		/*
1715		 * Tell the outside world we've arrived.
1716		 */
1717		(void) isp_async(isp, ISPASYNC_PDB_CHANGED, &i);
1718	}
1719
1720	/*
1721	 * Now find all previously used targets that are now invalid and
1722	 * notify the outer layers that they're gone.
1723	 */
1724	for (lp = fcp->portdb; lp < &fcp->portdb[prange]; lp++) {
1725		if (lp->valid || lp->port_wwn == 0)
1726			continue;
1727
1728		/*
1729		 * Tell the outside world we've gone away.
1730		 */
1731		loopid = lp - fcp->portdb;
1732		(void) isp_async(isp, ISPASYNC_PDB_CHANGED, &loopid);
1733		MEMZERO((void *) lp, sizeof (*lp));
1734	}
1735
1736#ifdef	ISP2100_FABRIC
1737	/*
1738	 * Now log in any fabric devices
1739	 */
1740	for (lp = &fcp->portdb[FC_SNS_ID+1];
1741	     lp < &fcp->portdb[MAX_FC_TARG]; lp++) {
1742		u_int32_t portid;
1743		mbreg_t mbs;
1744
1745		/*
1746		 * Anything here?
1747		 */
1748		if (lp->port_wwn == 0)
1749			continue;
1750
1751		/*
1752		 * Don't try to log into yourself.
1753		 */
1754		if ((portid = lp->portid) == fcp->isp_portid)
1755			continue;
1756
1757
1758		/*
1759		 * If we'd been logged in- see if we still are and we haven't
1760		 * changed. If so, no need to log ourselves out, etc..
1761		 */
1762		if (lp->loggedin &&
1763		    isp_getpdb(isp, lp->loopid, &pdb) == 0) {
1764			int nrole;
1765			u_int64_t nwwnn, nwwpn;
1766			nwwnn =
1767			    (((u_int64_t)pdb.pdb_nodename[0]) << 56) |
1768			    (((u_int64_t)pdb.pdb_nodename[1]) << 48) |
1769			    (((u_int64_t)pdb.pdb_nodename[2]) << 40) |
1770			    (((u_int64_t)pdb.pdb_nodename[3]) << 32) |
1771			    (((u_int64_t)pdb.pdb_nodename[4]) << 24) |
1772			    (((u_int64_t)pdb.pdb_nodename[5]) << 16) |
1773			    (((u_int64_t)pdb.pdb_nodename[6]) <<  8) |
1774			    (((u_int64_t)pdb.pdb_nodename[7]));
1775			nwwpn =
1776			    (((u_int64_t)pdb.pdb_portname[0]) << 56) |
1777			    (((u_int64_t)pdb.pdb_portname[1]) << 48) |
1778			    (((u_int64_t)pdb.pdb_portname[2]) << 40) |
1779			    (((u_int64_t)pdb.pdb_portname[3]) << 32) |
1780			    (((u_int64_t)pdb.pdb_portname[4]) << 24) |
1781			    (((u_int64_t)pdb.pdb_portname[5]) << 16) |
1782			    (((u_int64_t)pdb.pdb_portname[6]) <<  8) |
1783			    (((u_int64_t)pdb.pdb_portname[7]));
1784			nrole = (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >>
1785			    SVC3_ROLE_SHIFT;
1786			if (pdb.pdb_loopid == lp->loopid && lp->portid ==
1787			    (u_int32_t) BITS2WORD(pdb.pdb_portid_bits) &&
1788			    nwwnn == lp->node_wwn && nwwpn == lp->port_wwn &&
1789			    lp->roles == nrole) {
1790				lp->loggedin = lp->valid = 1;
1791				isp_prt(isp, ISP_LOGINFO, lretained,
1792				    (int) (lp - fcp->portdb),
1793				    (int) lp->loopid, lp->portid);
1794				continue;
1795			}
1796		}
1797
1798		/*
1799		 * Force a logout if we were logged in.
1800		 */
1801		if (lp->loggedin) {
1802			mbs.param[0] = MBOX_FABRIC_LOGOUT;
1803			mbs.param[1] = lp->loopid << 8;
1804			mbs.param[2] = 0;
1805			mbs.param[3] = 0;
1806			isp_mboxcmd(isp, &mbs, MBLOGNONE);
1807			lp->loggedin = 0;
1808			isp_prt(isp, ISP_LOGINFO, plogout,
1809			    (int) (lp - fcp->portdb), lp->loopid, lp->portid);
1810		}
1811
1812		/*
1813		 * And log in....
1814		 */
1815		loopid = lp - fcp->portdb;
1816		lp->loopid = 0;
1817		do {
1818			mbs.param[0] = MBOX_FABRIC_LOGIN;
1819			mbs.param[1] = loopid << 8;
1820			mbs.param[2] = portid >> 16;
1821			mbs.param[3] = portid & 0xffff;
1822			if (IS_2200(isp)) {
1823				/* only issue a PLOGI if not logged in */
1824				mbs.param[1] |= 0x1;
1825			}
1826			isp_mboxcmd(isp, &mbs, MBLOGALL & ~(MBOX_LOOP_ID_USED |
1827			    MBOX_PORT_ID_USED | MBOX_COMMAND_ERROR));
1828			switch (mbs.param[0]) {
1829			case MBOX_LOOP_ID_USED:
1830				/*
1831				 * Try the next available loop id.
1832				 */
1833				loopid++;
1834				break;
1835			case MBOX_PORT_ID_USED:
1836				/*
1837				 * This port is already logged in.
1838				 * Snaffle the loop id it's using if it's
1839				 * nonzero, otherwise we're hosed.
1840				 */
1841				if (mbs.param[1] != 0) {
1842					loopid = mbs.param[1];
1843					isp_prt(isp, ISP_LOGINFO, retained,
1844					    loopid, (int) (lp - fcp->portdb),
1845					    lp->portid);
1846				} else {
1847					loopid = MAX_FC_TARG;
1848					break;
1849				}
1850				/* FALLTHROUGH */
1851			case MBOX_COMMAND_COMPLETE:
1852				lp->loggedin = 1;
1853				lp->loopid = loopid;
1854				break;
1855			case MBOX_COMMAND_ERROR:
1856				isp_prt(isp, ISP_LOGINFO, plogierr,
1857				    portid, mbs.param[1]);
1858				/* FALLTHROUGH */
1859			case MBOX_ALL_IDS_USED: /* We're outta IDs */
1860			default:
1861				loopid = MAX_FC_TARG;
1862				break;
1863			}
1864		} while (lp->loopid == 0 && loopid < MAX_FC_TARG);
1865
1866		/*
1867		 * If we get here and we haven't set a Loop ID,
1868		 * we failed to log into this device.
1869		 */
1870
1871		if (lp->loopid == 0) {
1872			continue;
1873		}
1874
1875		/*
1876		 * Make sure we can get the approriate port information.
1877		 */
1878		if (isp_getpdb(isp, lp->loopid, &pdb) != 0) {
1879			isp_prt(isp, ISP_LOGWARN, nopdb, lp->portid);
1880			goto dump_em;
1881		}
1882
1883		if (pdb.pdb_loopid != lp->loopid) {
1884			isp_prt(isp, ISP_LOGWARN, pdbmfail1,
1885			    lp->portid, pdb.pdb_loopid);
1886			goto dump_em;
1887		}
1888
1889		if (lp->portid != (u_int32_t) BITS2WORD(pdb.pdb_portid_bits)) {
1890			isp_prt(isp, ISP_LOGWARN, pdbmfail2,
1891			    lp->portid, BITS2WORD(pdb.pdb_portid_bits));
1892			goto dump_em;
1893		}
1894
1895		lp->roles =
1896		    (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
1897		lp->node_wwn =
1898		    (((u_int64_t)pdb.pdb_nodename[0]) << 56) |
1899		    (((u_int64_t)pdb.pdb_nodename[1]) << 48) |
1900		    (((u_int64_t)pdb.pdb_nodename[2]) << 40) |
1901		    (((u_int64_t)pdb.pdb_nodename[3]) << 32) |
1902		    (((u_int64_t)pdb.pdb_nodename[4]) << 24) |
1903		    (((u_int64_t)pdb.pdb_nodename[5]) << 16) |
1904		    (((u_int64_t)pdb.pdb_nodename[6]) <<  8) |
1905		    (((u_int64_t)pdb.pdb_nodename[7]));
1906		lp->port_wwn =
1907		    (((u_int64_t)pdb.pdb_portname[0]) << 56) |
1908		    (((u_int64_t)pdb.pdb_portname[1]) << 48) |
1909		    (((u_int64_t)pdb.pdb_portname[2]) << 40) |
1910		    (((u_int64_t)pdb.pdb_portname[3]) << 32) |
1911		    (((u_int64_t)pdb.pdb_portname[4]) << 24) |
1912		    (((u_int64_t)pdb.pdb_portname[5]) << 16) |
1913		    (((u_int64_t)pdb.pdb_portname[6]) <<  8) |
1914		    (((u_int64_t)pdb.pdb_portname[7]));
1915		/*
1916		 * Check to make sure this all makes sense.
1917		 */
1918		if (lp->node_wwn && lp->port_wwn) {
1919			lp->valid = 1;
1920			loopid = lp - fcp->portdb;
1921			(void) isp_async(isp, ISPASYNC_PDB_CHANGED, &loopid);
1922			continue;
1923		}
1924dump_em:
1925		lp->valid = 0;
1926		isp_prt(isp, ISP_LOGINFO,
1927		    ldumped, loopid, lp->loopid, lp->portid);
1928		mbs.param[0] = MBOX_FABRIC_LOGOUT;
1929		mbs.param[1] = lp->loopid << 8;
1930		mbs.param[2] = 0;
1931		mbs.param[3] = 0;
1932		isp_mboxcmd(isp, &mbs, MBLOGNONE);
1933	}
1934#endif
1935	/*
1936	 * If we get here, we've for sure seen not only a valid loop
1937	 * but know what is or isn't on it, so mark this for usage
1938	 * in isp_start.
1939	 */
1940	fcp->loop_seen_once = 1;
1941	return (0);
1942}
1943
1944#ifdef	ISP2100_FABRIC
1945static int
1946isp_scan_fabric(isp)
1947	struct ispsoftc *isp;
1948{
1949	fcparam *fcp = isp->isp_param;
1950	u_int32_t portid, first_nz_portid;
1951	sns_screq_t *reqp;
1952	sns_scrsp_t *resp;
1953	mbreg_t mbs;
1954	int hicap;
1955
1956	reqp = (sns_screq_t *) fcp->isp_scratch;
1957	resp = (sns_scrsp_t *) (&((char *)fcp->isp_scratch)[0x100]);
1958	first_nz_portid = portid = fcp->isp_portid;
1959
1960	for (hicap = 0; hicap < 1024; hicap++) {
1961		MEMZERO((void *) reqp, SNS_GAN_REQ_SIZE);
1962		reqp->snscb_rblen = SNS_GAN_RESP_SIZE >> 1;
1963		reqp->snscb_addr[RQRSP_ADDR0015] =
1964			DMA_LSW(fcp->isp_scdma + 0x100);
1965		reqp->snscb_addr[RQRSP_ADDR1631] =
1966			DMA_MSW(fcp->isp_scdma + 0x100);
1967		reqp->snscb_sblen = 6;
1968		reqp->snscb_data[0] = SNS_GAN;
1969		reqp->snscb_data[4] = portid & 0xffff;
1970		reqp->snscb_data[5] = (portid >> 16) & 0xff;
1971		ISP_SWIZZLE_SNS_REQ(isp, reqp);
1972		mbs.param[0] = MBOX_SEND_SNS;
1973		mbs.param[1] = SNS_GAN_REQ_SIZE >> 1;
1974		mbs.param[2] = DMA_MSW(fcp->isp_scdma);
1975		mbs.param[3] = DMA_LSW(fcp->isp_scdma);
1976		mbs.param[6] = 0;
1977		mbs.param[7] = 0;
1978		isp_mboxcmd(isp, &mbs, MBLOGALL);
1979		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1980			return (-1);
1981		}
1982		ISP_UNSWIZZLE_SNS_RSP(isp, resp, SNS_GAN_RESP_SIZE >> 1);
1983		portid = (((u_int32_t) resp->snscb_port_id[0]) << 16) |
1984		    (((u_int32_t) resp->snscb_port_id[1]) << 8) |
1985		    (((u_int32_t) resp->snscb_port_id[2]));
1986		if (isp_async(isp, ISPASYNC_FABRIC_DEV, resp)) {
1987			return (-1);
1988		}
1989		if (first_nz_portid == 0 && portid) {
1990			first_nz_portid = portid;
1991		}
1992		if (first_nz_portid == portid) {
1993			return (0);
1994		}
1995	}
1996	/*
1997	 * We either have a broken name server or a huge fabric if we get here.
1998	 */
1999	return (0);
2000}
2001#endif
2002/*
2003 * Start a command. Locking is assumed done in the caller.
2004 */
2005
2006int
2007isp_start(xs)
2008	XS_T *xs;
2009{
2010	struct ispsoftc *isp;
2011	u_int16_t iptr, optr;
2012	union {
2013		ispreq_t *_reqp;
2014		ispreqt2_t *_t2reqp;
2015	} _u;
2016#define	reqp	_u._reqp
2017#define	t2reqp	_u._t2reqp
2018#define	UZSIZE	max(sizeof (ispreq_t), sizeof (ispreqt2_t))
2019	int target, i;
2020
2021	XS_INITERR(xs);
2022	isp = XS_ISP(xs);
2023
2024	if (isp->isp_state != ISP_RUNSTATE) {
2025		isp_prt(isp, ISP_LOGERR, "Adapter not at RUNSTATE");
2026		XS_SETERR(xs, HBA_BOTCH);
2027		return (CMD_COMPLETE);
2028	}
2029
2030	/*
2031	 * Check command CDB length, etc.. We really are limited to 16 bytes
2032	 * for Fibre Channel, but can do up to 44 bytes in parallel SCSI,
2033	 * but probably only if we're running fairly new firmware (we'll
2034	 * let the old f/w choke on an extended command queue entry).
2035	 */
2036
2037	if (XS_CDBLEN(xs) > (IS_FC(isp)? 16 : 44) || XS_CDBLEN(xs) == 0) {
2038		isp_prt(isp, ISP_LOGERR,
2039		    "unsupported cdb length (%d, CDB[0]=0x%x)",
2040		    XS_CDBLEN(xs), XS_CDBP(xs)[0] & 0xff);
2041		XS_SETERR(xs, HBA_BOTCH);
2042		return (CMD_COMPLETE);
2043	}
2044
2045	/*
2046	 * Check to see whether we have good firmware state still or
2047	 * need to refresh our port database for this target.
2048	 */
2049	target = XS_TGT(xs);
2050	if (IS_FC(isp)) {
2051		fcparam *fcp = isp->isp_param;
2052		struct lportdb *lp;
2053#if	defined(ISP2100_FABRIC)
2054		/*
2055		 * If we're not on a Fabric, we can't have a target
2056		 * above FL_PORT_ID-1. If we're on a fabric and
2057		 * connected as an F-port, we can't have a target
2058		 * less than FC_SNS_ID+1.
2059		 */
2060		if (fcp->isp_onfabric == 0) {
2061			if (target >= FL_PORT_ID) {
2062				XS_SETERR(xs, HBA_SELTIMEOUT);
2063				return (CMD_COMPLETE);
2064			}
2065		} else {
2066			if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
2067				XS_SETERR(xs, HBA_SELTIMEOUT);
2068				return (CMD_COMPLETE);
2069			}
2070			if (fcp->isp_topo == TOPO_F_PORT &&
2071			    target < FL_PORT_ID) {
2072				XS_SETERR(xs, HBA_SELTIMEOUT);
2073				return (CMD_COMPLETE);
2074			}
2075		}
2076#endif
2077		/*
2078		 * Check for f/w being in ready state. If the f/w
2079		 * isn't in ready state, then we don't know our
2080		 * loop ID and the f/w hasn't completed logging
2081		 * into all targets on the loop. If this is the
2082		 * case, then bounce the command. We pretend this is
2083		 * a SELECTION TIMEOUT error if we've never gone to
2084		 * FW_READY state at all- in this case we may not
2085		 * be hooked to a loop at all and we shouldn't hang
2086		 * the machine for this. Otherwise, defer this command
2087		 * until later.
2088		 */
2089		if (fcp->isp_fwstate != FW_READY) {
2090			/*
2091			 * Give ourselves at most a 250ms delay.
2092			 */
2093			if (isp_fclink_test(isp, 250000)) {
2094				XS_SETERR(xs, HBA_SELTIMEOUT);
2095				if (fcp->loop_seen_once) {
2096					return (CMD_RQLATER);
2097				} else {
2098					return (CMD_COMPLETE);
2099				}
2100			}
2101		}
2102
2103		/*
2104		 * If our loop state is such that we haven't yet received
2105		 * a "Port Database Changed" notification (after a LIP or
2106		 * a Loop Reset or firmware initialization), then defer
2107		 * sending commands for a little while, but only if we've
2108		 * seen a valid loop at one point (otherwise we can get
2109		 * stuck at initialization time).
2110		 */
2111		if (fcp->isp_loopstate < LOOP_PDB_RCVD) {
2112			XS_SETERR(xs, HBA_SELTIMEOUT);
2113			if (fcp->loop_seen_once) {
2114				return (CMD_RQLATER);
2115			} else {
2116				return (CMD_COMPLETE);
2117			}
2118		}
2119
2120		/*
2121		 * If our loop state is now such that we've just now
2122		 * received a Port Database Change notification, then
2123		 * we have to go off and (re)synchronize our port
2124		 * database.
2125		 */
2126		if (fcp->isp_loopstate == LOOP_PDB_RCVD) {
2127			if (isp_pdb_sync(isp, target)) {
2128				XS_SETERR(xs, HBA_SELTIMEOUT);
2129				return (CMD_COMPLETE);
2130			}
2131		}
2132
2133		/*
2134		 * XXX: Here's were we would cancel any loop_dead flag
2135		 * XXX: also cancel in dead_loop timeout that's running
2136		 */
2137
2138		/*
2139		 * Now check whether we should even think about pursuing this.
2140		 */
2141		lp = &fcp->portdb[target];
2142		if (lp->valid == 0) {
2143			XS_SETERR(xs, HBA_SELTIMEOUT);
2144			return (CMD_COMPLETE);
2145		}
2146		if ((lp->roles & (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT)) == 0) {
2147			isp_prt(isp, ISP_LOGDEBUG2,
2148			    "Target %d does not have target service", target);
2149			XS_SETERR(xs, HBA_SELTIMEOUT);
2150			return (CMD_COMPLETE);
2151		}
2152		/*
2153		 * Now turn target into what the actual loop ID is.
2154		 */
2155		target = lp->loopid;
2156	}
2157
2158	/*
2159	 * Next check to see if any HBA or Device
2160	 * parameters need to be updated.
2161	 */
2162	if (isp->isp_update != 0) {
2163		isp_update(isp);
2164	}
2165
2166	if (isp_getrqentry(isp, &iptr, &optr, (void **) &reqp)) {
2167		isp_prt(isp, ISP_LOGDEBUG0, "Request Queue Overflow");
2168		XS_SETERR(xs, HBA_BOTCH);
2169		return (CMD_EAGAIN);
2170	}
2171
2172	/*
2173	 * Now see if we need to synchronize the ISP with respect to anything.
2174	 * We do dual duty here (cough) for synchronizing for busses other
2175	 * than which we got here to send a command to.
2176	 */
2177	if (isp->isp_sendmarker) {
2178		u_int8_t n = (IS_DUALBUS(isp)? 2: 1);
2179		/*
2180		 * Check ports to send markers for...
2181		 */
2182		for (i = 0; i < n; i++) {
2183			if ((isp->isp_sendmarker & (1 << i)) == 0) {
2184				continue;
2185			}
2186			MEMZERO((void *) reqp, sizeof (*reqp));
2187			reqp->req_header.rqs_entry_count = 1;
2188			reqp->req_header.rqs_entry_type = RQSTYPE_MARKER;
2189			reqp->req_modifier = SYNC_ALL;
2190			reqp->req_target = i << 7;	/* insert bus number */
2191			ISP_SWIZZLE_REQUEST(isp, reqp);
2192			ISP_ADD_REQUEST(isp, iptr);
2193
2194			if (isp_getrqentry(isp, &iptr, &optr, (void **)&reqp)) {
2195				isp_prt(isp, ISP_LOGDEBUG0,
2196				    "Request Queue Overflow+");
2197				XS_SETERR(xs, HBA_BOTCH);
2198				return (CMD_EAGAIN);
2199			}
2200		}
2201	}
2202
2203	MEMZERO((void *) reqp, UZSIZE);
2204	reqp->req_header.rqs_entry_count = 1;
2205	if (IS_FC(isp)) {
2206		reqp->req_header.rqs_entry_type = RQSTYPE_T2RQS;
2207	} else {
2208		if (XS_CDBLEN(xs) > 12)
2209			reqp->req_header.rqs_entry_type = RQSTYPE_CMDONLY;
2210		else
2211			reqp->req_header.rqs_entry_type = RQSTYPE_REQUEST;
2212	}
2213	reqp->req_header.rqs_flags = 0;
2214	reqp->req_header.rqs_seqno = 0;
2215	if (IS_FC(isp)) {
2216		/*
2217		 * See comment in isp_intr
2218		 */
2219		XS_RESID(xs) = 0;
2220
2221		/*
2222		 * Fibre Channel always requires some kind of tag.
2223		 * The Qlogic drivers seem be happy not to use a tag,
2224		 * but this breaks for some devices (IBM drives).
2225		 */
2226		if (XS_TAG_P(xs)) {
2227			t2reqp->req_flags = XS_TAG_TYPE(xs);
2228		} else {
2229			/*
2230			 * If we don't know what tag to use, use HEAD OF QUEUE
2231			 * for Request Sense or Ordered (for safety's sake).
2232			 */
2233			if (XS_CDBP(xs)[0] == 0x3)	/* REQUEST SENSE */
2234				t2reqp->req_flags = REQFLAG_HTAG;
2235			else
2236				t2reqp->req_flags = REQFLAG_OTAG;
2237		}
2238	} else {
2239		sdparam *sdp = (sdparam *)isp->isp_param;
2240		if ((sdp->isp_devparam[target].cur_dflags & DPARM_TQING) &&
2241		    XS_TAG_P(xs)) {
2242			reqp->req_flags = XS_TAG_TYPE(xs);
2243		}
2244	}
2245	reqp->req_target = target | (XS_CHANNEL(xs) << 7);
2246	if (IS_SCSI(isp)) {
2247		reqp->req_lun_trn = XS_LUN(xs);
2248		reqp->req_cdblen = XS_CDBLEN(xs);
2249	} else {
2250		if (isp->isp_maxluns > 16)
2251			t2reqp->req_scclun = XS_LUN(xs);
2252		else
2253			t2reqp->req_lun_trn = XS_LUN(xs);
2254	}
2255	MEMCPY(reqp->req_cdb, XS_CDBP(xs), XS_CDBLEN(xs));
2256
2257	reqp->req_time = XS_TIME(xs) / 1000;
2258	if (reqp->req_time == 0 && XS_TIME(xs))
2259		reqp->req_time = 1;
2260
2261	/*
2262	 * Always give a bit more leeway to commands after a bus reset.
2263	 * XXX: DOES NOT DISTINGUISH WHICH PORT MAY HAVE BEEN SYNCED
2264	 */
2265	if (isp->isp_sendmarker && reqp->req_time < 5) {
2266		reqp->req_time = 5;
2267	}
2268	if (isp_save_xs(isp, xs, &reqp->req_handle)) {
2269		isp_prt(isp, ISP_LOGDEBUG1, "out of xflist pointers");
2270		XS_SETERR(xs, HBA_BOTCH);
2271		return (CMD_EAGAIN);
2272	}
2273	/*
2274	 * Set up DMA and/or do any bus swizzling of the request entry
2275	 * so that the Qlogic F/W understands what is being asked of it.
2276 	*/
2277	i = ISP_DMASETUP(isp, xs, reqp, &iptr, optr);
2278	if (i != CMD_QUEUED) {
2279		isp_destroy_handle(isp, reqp->req_handle);
2280		/*
2281		 * dmasetup sets actual error in packet, and
2282		 * return what we were given to return.
2283		 */
2284		return (i);
2285	}
2286	XS_SETERR(xs, HBA_NOERROR);
2287	isp_prt(isp, ISP_LOGDEBUG2,
2288	    "START cmd for %d.%d.%d cmd 0x%x datalen %d",
2289	    XS_CHANNEL(xs), target, XS_LUN(xs), XS_CDBP(xs)[0], XS_XFRLEN(xs));
2290	ISP_ADD_REQUEST(isp, iptr);
2291	isp->isp_nactive++;
2292	if (isp->isp_sendmarker)
2293		isp->isp_sendmarker = 0;
2294	return (CMD_QUEUED);
2295#undef	reqp
2296#undef	t2reqp
2297}
2298
2299/*
2300 * isp control
2301 * Locks (ints blocked) assumed held.
2302 */
2303
2304int
2305isp_control(isp, ctl, arg)
2306	struct ispsoftc *isp;
2307	ispctl_t ctl;
2308	void *arg;
2309{
2310	XS_T *xs;
2311	mbreg_t mbs;
2312	int bus, tgt;
2313	u_int32_t handle;
2314
2315	switch (ctl) {
2316	default:
2317		isp_prt(isp, ISP_LOGERR, "Unknown Control Opcode 0x%x", ctl);
2318		break;
2319
2320	case ISPCTL_RESET_BUS:
2321		/*
2322		 * Issue a bus reset.
2323		 */
2324		mbs.param[0] = MBOX_BUS_RESET;
2325		mbs.param[2] = 0;
2326		if (IS_SCSI(isp)) {
2327			mbs.param[1] =
2328			    ((sdparam *) isp->isp_param)->isp_bus_reset_delay;
2329			if (mbs.param[1] < 2)
2330				mbs.param[1] = 2;
2331			bus = *((int *) arg);
2332			if (IS_DUALBUS(isp))
2333				mbs.param[2] = bus;
2334		} else {
2335			mbs.param[1] = 10;
2336			bus = 0;
2337		}
2338		isp->isp_sendmarker |= (1 << bus);
2339		isp_mboxcmd(isp, &mbs, MBLOGALL);
2340		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2341			break;
2342		}
2343		isp_prt(isp, ISP_LOGINFO,
2344		    "driver initiated bus reset of bus %d", bus);
2345		return (0);
2346
2347	case ISPCTL_RESET_DEV:
2348		tgt = (*((int *) arg)) & 0xffff;
2349		bus = (*((int *) arg)) >> 16;
2350		mbs.param[0] = MBOX_ABORT_TARGET;
2351		mbs.param[1] = (tgt << 8) | (bus << 15);
2352		mbs.param[2] = 3;	/* 'delay', in seconds */
2353		isp_mboxcmd(isp, &mbs, MBLOGALL);
2354		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2355			break;
2356		}
2357		isp_prt(isp, ISP_LOGINFO,
2358		    "Target %d on Bus %d Reset Succeeded", tgt, bus);
2359		isp->isp_sendmarker |= (1 << bus);
2360		return (0);
2361
2362	case ISPCTL_ABORT_CMD:
2363		xs = (XS_T *) arg;
2364		tgt = XS_TGT(xs);
2365		handle = isp_find_handle(isp, xs);
2366		if (handle == 0) {
2367			isp_prt(isp, ISP_LOGWARN,
2368			    "cannot find handle for command to abort");
2369			break;
2370		}
2371		bus = XS_CHANNEL(xs);
2372		mbs.param[0] = MBOX_ABORT;
2373		if (IS_FC(isp)) {
2374			if (isp->isp_maxluns > 16) {
2375				mbs.param[1] = tgt << 8;
2376				mbs.param[4] = 0;
2377				mbs.param[5] = 0;
2378				mbs.param[6] = XS_LUN(xs);
2379			} else {
2380				mbs.param[1] = tgt << 8 | XS_LUN(xs);
2381			}
2382		} else {
2383			mbs.param[1] =
2384			    (bus << 15) | (XS_TGT(xs) << 8) | XS_LUN(xs);
2385		}
2386		mbs.param[3] = handle >> 16;
2387		mbs.param[2] = handle & 0xffff;
2388		isp_mboxcmd(isp, &mbs, MBLOGALL & ~MBOX_COMMAND_ERROR);
2389		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
2390			return (0);
2391		}
2392		/*
2393		 * XXX: Look for command in the REQUEST QUEUE. That is,
2394		 * XXX: It hasen't been picked up by firmware yet.
2395		 */
2396		break;
2397
2398	case ISPCTL_UPDATE_PARAMS:
2399		isp_update(isp);
2400		return (0);
2401
2402	case ISPCTL_FCLINK_TEST:
2403		if (IS_FC(isp)) {
2404			int usdelay = (arg)? *((int *) arg) : 250000;
2405			return (isp_fclink_test(isp, usdelay));
2406		}
2407		break;
2408
2409	case ISPCTL_PDB_SYNC:
2410		if (IS_FC(isp)) {
2411			return (isp_pdb_sync(isp, -1));
2412		}
2413		break;
2414#ifdef	ISP_TARGET_MODE
2415	case ISPCTL_TOGGLE_TMODE:
2416	{
2417		int ena = *(int *)arg;
2418		if (IS_SCSI(isp)) {
2419			mbs.param[0] = MBOX_ENABLE_TARGET_MODE;
2420			mbs.param[1] = (ena)? ENABLE_TARGET_FLAG : 0;
2421			isp_mboxcmd(isp, &mbs, MBLOGALL);
2422			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2423				break;
2424			}
2425		} else {
2426			fcparam *fcp = isp->isp_param;
2427			/*
2428			 * We assume somebody has quiesced this bus.
2429			 */
2430			if (ena) {
2431				if (fcp->isp_fwoptions & ICBOPT_TGT_ENABLE) {
2432					return (0);
2433				}
2434				fcp->isp_fwoptions |= ICBOPT_TGT_ENABLE;
2435			} else {
2436				if (!(fcp->isp_fwoptions & ICBOPT_TGT_ENABLE)) {
2437					return (0);
2438				}
2439				fcp->isp_fwoptions &= ~ICBOPT_TGT_ENABLE;
2440			}
2441			isp->isp_state = ISP_NILSTATE;
2442			isp_reset(isp);
2443			if (isp->isp_state != ISP_RESETSTATE) {
2444				break;
2445			}
2446			isp_init(isp);
2447			if (isp->isp_state != ISP_INITSTATE) {
2448				break;
2449			}
2450			isp->isp_state = ISP_RUNSTATE;
2451		}
2452		return (0);
2453	}
2454#endif
2455	}
2456	return (-1);
2457}
2458
2459/*
2460 * Interrupt Service Routine(s).
2461 *
2462 * External (OS) framework has done the appropriate locking,
2463 * and the locking will be held throughout this function.
2464 */
2465
2466/*
2467 * Limit our stack depth by sticking with the max likely number
2468 * of completions on a request queue at any one time.
2469 */
2470#define	MAX_REQUESTQ_COMPLETIONS	32
2471
2472int
2473isp_intr(arg)
2474	void *arg;
2475{
2476	struct ispsoftc *isp = arg;
2477	XS_T *complist[MAX_REQUESTQ_COMPLETIONS], *xs;
2478	u_int16_t iptr, optr, isr, sema, junk;
2479	int i, nlooked = 0, ndone = 0;
2480
2481	if (IS_2100(isp)) {
2482		i = 0;
2483		do {
2484			isr = ISP_READ(isp, BIU_ISR);
2485			junk = ISP_READ(isp, BIU_ISR);
2486		} while (isr != junk && ++i < 1000);
2487		if (isr != junk) {
2488			isp_prt(isp, ISP_LOGWARN,
2489			    "isr unsteady (%x, %x)", isr, junk);
2490		}
2491		i = 0;
2492		do {
2493			sema = ISP_READ(isp, BIU_SEMA);
2494			junk = ISP_READ(isp, BIU_SEMA);
2495		} while (sema != junk && ++i < 1000);
2496		if (sema != junk) {
2497			isp_prt(isp, ISP_LOGWARN,
2498			    "sema unsteady (%x, %x)", sema, junk);
2499		}
2500	} else {
2501		isr = ISP_READ(isp, BIU_ISR);
2502		sema = ISP_READ(isp, BIU_SEMA);
2503	}
2504	isp_prt(isp, ISP_LOGDEBUG3, "isp_intr isr %x sem %x", isr, sema);
2505	isr &= INT_PENDING_MASK(isp);
2506	sema &= BIU_SEMA_LOCK;
2507	isp->isp_intcnt++;
2508	if (isr == 0 && sema == 0) {
2509		isp->isp_intbogus++;
2510		return (0);
2511	}
2512
2513	if (sema) {
2514		u_int16_t mbox;
2515
2516		if (IS_2100(isp)) {
2517			i = 0;
2518			do {
2519				mbox = ISP_READ(isp, OUTMAILBOX0);
2520				junk = ISP_READ(isp, OUTMAILBOX0);;
2521			} while (junk != mbox && ++i < 1000);
2522			if (mbox != junk) {
2523				isp_prt(isp, ISP_LOGWARN,
2524				    "mailbox0 unsteady (%x, %x)", mbox, junk);
2525				ISP_WRITE(isp, BIU_SEMA, 0);
2526				ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
2527				return (1);
2528			}
2529		} else {
2530			mbox = ISP_READ(isp, OUTMAILBOX0);
2531		}
2532		if (mbox & 0x4000) {
2533			int obits, i = 0;
2534			if ((obits = isp->isp_mboxbsy) != 0) {
2535				isp->isp_mboxtmp[i++] = mbox;
2536				for (i = 1; i < MAX_MAILBOX; i++) {
2537					if ((obits & (1 << i)) == 0) {
2538						continue;
2539					}
2540					isp->isp_mboxtmp[i] =
2541					    ISP_READ(isp, MBOX_OFF(i));
2542				}
2543				MBOX_NOTIFY_COMPLETE(isp);
2544			} else {
2545				isp_prt(isp, ISP_LOGWARN,
2546				    "Mbox Command Async (0x%x) with no waiters",
2547				    mbox);
2548			}
2549		} else {
2550			u_int32_t fhandle = isp_parse_async(isp, (int) mbox);
2551			isp_prt(isp, ISP_LOGDEBUG2, "Async Mbox 0x%x", mbox);
2552			if (fhandle > 0) {
2553				isp_fastpost_complete(isp, fhandle);
2554			}
2555		}
2556		if (IS_FC(isp) || isp->isp_state != ISP_RUNSTATE) {
2557			ISP_WRITE(isp, BIU_SEMA, 0);
2558			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
2559			return (1);
2560		}
2561	}
2562
2563	/*
2564	 * We can't be getting this now.
2565	 */
2566	if (isp->isp_state != ISP_RUNSTATE) {
2567		isp_prt(isp, ISP_LOGWARN,
2568		    "interrupt (isr=%x, sema=%x) when not ready", isr, sema);
2569		ISP_WRITE(isp, INMAILBOX5, ISP_READ(isp, OUTMAILBOX5));
2570		ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
2571		ISP_WRITE(isp, BIU_SEMA, 0);
2572		return (1);
2573	}
2574
2575	/*
2576	 * You *must* read OUTMAILBOX5 prior to clearing the RISC interrupt.
2577	 */
2578	optr = isp->isp_residx;
2579
2580	if (IS_2100(isp)) {
2581		i = 0;
2582		do {
2583			iptr = ISP_READ(isp, OUTMAILBOX5);
2584			junk = ISP_READ(isp, OUTMAILBOX5);
2585		} while (junk != iptr && ++i < 1000);
2586
2587		if (iptr != junk) {
2588			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
2589			isp_prt(isp, ISP_LOGWARN,
2590			    "mailbox5 unsteady (%x, %x)", iptr, junk);
2591			return (1);
2592		}
2593	} else {
2594		iptr = ISP_READ(isp, OUTMAILBOX5);
2595	}
2596
2597	if (sema) {
2598		ISP_WRITE(isp, BIU_SEMA, 0);
2599	}
2600	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
2601
2602	if (optr == iptr && sema == 0) {
2603		/*
2604		 * There are a lot of these- reasons unknown- mostly on
2605		 * faster Alpha machines.
2606		 *
2607		 * I tried delaying after writing HCCR_CMD_CLEAR_RISC_INT to
2608		 * make sure the old interrupt went away (to avoid 'ringing'
2609		 * effects), but that didn't stop this from occurring.
2610		 */
2611		junk = ISP_READ(isp, BIU_ISR);
2612		isp_prt(isp, ISP_LOGDEBUG2,
2613		    "bogus intr- isr %x (%x) iptr %x optr %x",
2614		    isr, junk, iptr, optr);
2615	}
2616
2617	while (optr != iptr) {
2618		ispstatusreq_t *sp;
2619		u_int16_t oop;
2620		int buddaboom = 0;
2621
2622		sp = (ispstatusreq_t *) ISP_QUEUE_ENTRY(isp->isp_result, optr);
2623		oop = optr;
2624		optr = ISP_NXT_QENTRY(optr, RESULT_QUEUE_LEN(isp));
2625		nlooked++;
2626		/*
2627		 * Do any appropriate unswizzling of what the Qlogic f/w has
2628		 * written into memory so it makes sense to us. This is a
2629		 * per-platform thing. Also includes any memory barriers.
2630		 */
2631		ISP_UNSWIZZLE_RESPONSE(isp, sp, oop);
2632		if (sp->req_header.rqs_entry_type != RQSTYPE_RESPONSE) {
2633			if (isp_handle_other_response(isp, sp, &optr) == 0) {
2634				MEMZERO(sp, sizeof (isphdr_t));
2635				continue;
2636			}
2637			/*
2638			 * It really has to be a bounced request just copied
2639			 * from the request queue to the response queue. If
2640			 * not, something bad has happened.
2641			 */
2642			if (sp->req_header.rqs_entry_type != RQSTYPE_REQUEST) {
2643				isp_prt(isp, ISP_LOGERR, notresp,
2644				    sp->req_header.rqs_entry_type, oop, optr,
2645				    nlooked);
2646				if (isp->isp_dblev & ISP_LOGDEBUG0) {
2647					isp_print_bytes(isp, "Queue Entry",
2648					    QENTRY_LEN, sp);
2649				}
2650				MEMZERO(sp, sizeof (isphdr_t));
2651				continue;
2652			}
2653			buddaboom = 1;
2654		}
2655
2656		if (sp->req_header.rqs_flags & 0xf) {
2657#define	_RQS_OFLAGS	\
2658	~(RQSFLAG_CONTINUATION|RQSFLAG_FULL|RQSFLAG_BADHEADER|RQSFLAG_BADPACKET)
2659			if (sp->req_header.rqs_flags & RQSFLAG_CONTINUATION) {
2660				isp_prt(isp, ISP_LOGWARN,
2661				    "continuation segment");
2662				ISP_WRITE(isp, INMAILBOX5, optr);
2663				continue;
2664			}
2665			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
2666				isp_prt(isp, ISP_LOGDEBUG1,
2667				    "internal queues full");
2668				/*
2669				 * We'll synthesize a QUEUE FULL message below.
2670				 */
2671			}
2672			if (sp->req_header.rqs_flags & RQSFLAG_BADHEADER) {
2673				isp_prt(isp, ISP_LOGERR,  "bad header flag");
2674				buddaboom++;
2675			}
2676			if (sp->req_header.rqs_flags & RQSFLAG_BADPACKET) {
2677				isp_prt(isp, ISP_LOGERR, "bad request packet");
2678				buddaboom++;
2679			}
2680			if (sp->req_header.rqs_flags & _RQS_OFLAGS) {
2681				isp_prt(isp, ISP_LOGERR,
2682				    "unknown flags (0x%x) in response",
2683				    sp->req_header.rqs_flags);
2684				buddaboom++;
2685			}
2686#undef	_RQS_OFLAGS
2687		}
2688		if (sp->req_handle > isp->isp_maxcmds || sp->req_handle < 1) {
2689			MEMZERO(sp, sizeof (isphdr_t));
2690			isp_prt(isp, ISP_LOGERR,
2691			    "bad request handle %d", sp->req_handle);
2692			ISP_WRITE(isp, INMAILBOX5, optr);
2693			continue;
2694		}
2695		xs = isp_find_xs(isp, sp->req_handle);
2696		if (xs == NULL) {
2697			MEMZERO(sp, sizeof (isphdr_t));
2698			isp_prt(isp, ISP_LOGERR,
2699			    "cannot find handle 0x%x in xflist",
2700			    sp->req_handle);
2701			ISP_WRITE(isp, INMAILBOX5, optr);
2702			continue;
2703		}
2704		isp_destroy_handle(isp, sp->req_handle);
2705		if (sp->req_status_flags & RQSTF_BUS_RESET) {
2706			isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
2707		}
2708		if (buddaboom) {
2709			XS_SETERR(xs, HBA_BOTCH);
2710		}
2711
2712		if (IS_FC(isp) && (sp->req_scsi_status & RQCS_SV)) {
2713			/*
2714			 * Fibre Channel F/W doesn't say we got status
2715			 * if there's Sense Data instead. I guess they
2716			 * think it goes w/o saying.
2717			 */
2718			sp->req_state_flags |= RQSF_GOT_STATUS;
2719		}
2720		if (sp->req_state_flags & RQSF_GOT_STATUS) {
2721			*XS_STSP(xs) = sp->req_scsi_status & 0xff;
2722		}
2723
2724		switch (sp->req_header.rqs_entry_type) {
2725		case RQSTYPE_RESPONSE:
2726			XS_SET_STATE_STAT(isp, xs, sp);
2727			isp_parse_status(isp, sp, xs);
2728			if ((XS_NOERR(xs) || XS_ERR(xs) == HBA_NOERROR) &&
2729			    (*XS_STSP(xs) == SCSI_BUSY)) {
2730				XS_SETERR(xs, HBA_TGTBSY);
2731			}
2732			if (IS_SCSI(isp)) {
2733				XS_RESID(xs) = sp->req_resid;
2734				if ((sp->req_state_flags & RQSF_GOT_STATUS) &&
2735				    (*XS_STSP(xs) == SCSI_CHECK) &&
2736				    (sp->req_state_flags & RQSF_GOT_SENSE)) {
2737					XS_SAVE_SENSE(xs, sp);
2738				}
2739				/*
2740				 * A new synchronous rate was negotiated for
2741				 * this target. Mark state such that we'll go
2742				 * look up that which has changed later.
2743				 */
2744				if (sp->req_status_flags & RQSTF_NEGOTIATION) {
2745					int t = XS_TGT(xs);
2746					sdparam *sdp = isp->isp_param;
2747					sdp += XS_CHANNEL(xs);
2748					sdp->isp_devparam[t].dev_refresh = 1;
2749					isp->isp_update |=
2750					    (1 << XS_CHANNEL(xs));
2751				}
2752			} else {
2753				if (sp->req_status_flags & RQSF_XFER_COMPLETE) {
2754					XS_RESID(xs) = 0;
2755				} else if (sp->req_scsi_status & RQCS_RESID) {
2756					XS_RESID(xs) = sp->req_resid;
2757				} else {
2758					XS_RESID(xs) = 0;
2759				}
2760				if ((sp->req_state_flags & RQSF_GOT_STATUS) &&
2761				    (*XS_STSP(xs) == SCSI_CHECK) &&
2762				    (sp->req_scsi_status & RQCS_SV)) {
2763					XS_SAVE_SENSE(xs, sp);
2764				}
2765			}
2766			isp_prt(isp, ISP_LOGDEBUG2, "asked for %d got resid %d",
2767				XS_XFRLEN(xs), sp->req_resid);
2768			break;
2769		case RQSTYPE_REQUEST:
2770			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
2771				/*
2772				 * Force Queue Full status.
2773				 */
2774				*XS_STSP(xs) = SCSI_QFULL;
2775				XS_SETERR(xs, HBA_NOERROR);
2776			} else if (XS_NOERR(xs)) {
2777				XS_SETERR(xs, HBA_BOTCH);
2778			}
2779			XS_RESID(xs) = XS_XFRLEN(xs);
2780			break;
2781		default:
2782			isp_prt(isp, ISP_LOGWARN,
2783			    "unhandled respose queue type 0x%x",
2784			    sp->req_header.rqs_entry_type);
2785			if (XS_NOERR(xs)) {
2786				XS_SETERR(xs, HBA_BOTCH);
2787			}
2788			break;
2789		}
2790
2791		/*
2792		 * Free any dma resources. As a side effect, this may
2793		 * also do any cache flushing necessary for data coherence.			 */
2794		if (XS_XFRLEN(xs)) {
2795			ISP_DMAFREE(isp, xs, sp->req_handle);
2796		}
2797
2798		if (((isp->isp_dblev & (ISP_LOGDEBUG2|ISP_LOGDEBUG3))) ||
2799		    ((isp->isp_dblev & ISP_LOGDEBUG1) && !XS_NOERR(xs))) {
2800			char skey;
2801			if (sp->req_state_flags & RQSF_GOT_SENSE) {
2802				skey = XS_SNSKEY(xs) & 0xf;
2803				if (skey < 10)
2804					skey += '0';
2805				else
2806					skey += 'a';
2807			} else if (*XS_STSP(xs) == SCSI_CHECK) {
2808				skey = '?';
2809			} else {
2810				skey = '.';
2811			}
2812			isp_prt(isp, ISP_LOGALL, finmsg, XS_CHANNEL(xs),
2813			    XS_TGT(xs), XS_LUN(xs), XS_XFRLEN(xs), XS_RESID(xs),
2814			    *XS_STSP(xs), skey, XS_ERR(xs));
2815		}
2816
2817		if (isp->isp_nactive > 0)
2818		    isp->isp_nactive--;
2819		complist[ndone++] = xs;	/* defer completion call until later */
2820		MEMZERO(sp, sizeof (isphdr_t));
2821		if (ndone == MAX_REQUESTQ_COMPLETIONS) {
2822			break;
2823		}
2824	}
2825
2826	/*
2827	 * If we looked at any commands, then it's valid to find out
2828	 * what the outpointer is. It also is a trigger to update the
2829	 * ISP's notion of what we've seen so far.
2830	 */
2831	if (nlooked) {
2832		ISP_WRITE(isp, INMAILBOX5, optr);
2833		isp->isp_reqodx = ISP_READ(isp, OUTMAILBOX4);
2834	}
2835
2836	isp->isp_residx = optr;
2837	for (i = 0; i < ndone; i++) {
2838		xs = complist[i];
2839		if (xs) {
2840			isp_done(xs);
2841		}
2842	}
2843	return (1);
2844}
2845
2846/*
2847 * Support routines.
2848 */
2849
2850static int
2851isp_parse_async(isp, mbox)
2852	struct ispsoftc *isp;
2853	int mbox;
2854{
2855	int bus;
2856	u_int32_t fast_post_handle = 0;
2857
2858	if (IS_DUALBUS(isp)) {
2859		bus = ISP_READ(isp, OUTMAILBOX6);
2860	} else {
2861		bus = 0;
2862	}
2863
2864	switch (mbox) {
2865	case ASYNC_BUS_RESET:
2866		isp->isp_sendmarker |= (1 << bus);
2867#ifdef	ISP_TARGET_MODE
2868		isp_target_async(isp, bus, mbox);
2869#endif
2870		isp_async(isp, ISPASYNC_BUS_RESET, &bus);
2871		break;
2872	case ASYNC_SYSTEM_ERROR:
2873		mbox = ISP_READ(isp, OUTMAILBOX1);
2874		isp_prt(isp, ISP_LOGERR,
2875		    "Internal FW Error @ RISC Addr 0x%x", mbox);
2876		isp_reinit(isp);
2877#ifdef	ISP_TARGET_MODE
2878		isp_target_async(isp, bus, mbox);
2879#endif
2880		/* no point continuing after this */
2881		return (-1);
2882
2883	case ASYNC_RQS_XFER_ERR:
2884		isp_prt(isp, ISP_LOGERR, "Request Queue Transfer Error");
2885		break;
2886
2887	case ASYNC_RSP_XFER_ERR:
2888		isp_prt(isp, ISP_LOGERR, "Response Queue Transfer Error");
2889		break;
2890
2891	case ASYNC_QWAKEUP:
2892		/*
2893		 * We've just been notified that the Queue has woken up.
2894		 * We don't need to be chatty about this- just unlatch things
2895		 * and move on.
2896		 */
2897		mbox = ISP_READ(isp, OUTMAILBOX4);
2898		break;
2899
2900	case ASYNC_TIMEOUT_RESET:
2901		isp_prt(isp, ISP_LOGWARN,
2902		    "timeout initiated SCSI bus reset of bus %d\n", bus);
2903		isp->isp_sendmarker |= (1 << bus);
2904#ifdef	ISP_TARGET_MODE
2905		isp_target_async(isp, bus, mbox);
2906#endif
2907		break;
2908
2909	case ASYNC_DEVICE_RESET:
2910		isp_prt(isp, ISP_LOGINFO, "device reset on bus %d", bus);
2911		isp->isp_sendmarker |= (1 << bus);
2912#ifdef	ISP_TARGET_MODE
2913		isp_target_async(isp, bus, mbox);
2914#endif
2915		break;
2916
2917	case ASYNC_EXTMSG_UNDERRUN:
2918		isp_prt(isp, ISP_LOGWARN, "extended message underrun");
2919		break;
2920
2921	case ASYNC_SCAM_INT:
2922		isp_prt(isp, ISP_LOGINFO, "SCAM interrupt");
2923		break;
2924
2925	case ASYNC_HUNG_SCSI:
2926		isp_prt(isp, ISP_LOGERR,
2927		    "stalled SCSI Bus after DATA Overrun");
2928		/* XXX: Need to issue SCSI reset at this point */
2929		break;
2930
2931	case ASYNC_KILLED_BUS:
2932		isp_prt(isp, ISP_LOGERR, "SCSI Bus reset after DATA Overrun");
2933		break;
2934
2935	case ASYNC_BUS_TRANSIT:
2936		mbox = ISP_READ(isp, OUTMAILBOX2);
2937		switch (mbox & 0x1c00) {
2938		case SXP_PINS_LVD_MODE:
2939			isp_prt(isp, ISP_LOGINFO, "Transition to LVD mode");
2940			SDPARAM(isp)->isp_diffmode = 0;
2941			SDPARAM(isp)->isp_ultramode = 0;
2942			SDPARAM(isp)->isp_lvdmode = 1;
2943			break;
2944		case SXP_PINS_HVD_MODE:
2945			isp_prt(isp, ISP_LOGINFO,
2946			    "Transition to Differential mode");
2947			SDPARAM(isp)->isp_diffmode = 1;
2948			SDPARAM(isp)->isp_ultramode = 0;
2949			SDPARAM(isp)->isp_lvdmode = 0;
2950			break;
2951		case SXP_PINS_SE_MODE:
2952			isp_prt(isp, ISP_LOGINFO,
2953			    "Transition to Single Ended mode");
2954			SDPARAM(isp)->isp_diffmode = 0;
2955			SDPARAM(isp)->isp_ultramode = 1;
2956			SDPARAM(isp)->isp_lvdmode = 0;
2957			break;
2958		default:
2959			isp_prt(isp, ISP_LOGWARN,
2960			    "Transition to Unknown Mode 0x%x", mbox);
2961			break;
2962		}
2963		/*
2964		 * XXX: Set up to renegotiate again!
2965		 */
2966		/* Can only be for a 1080... */
2967		isp->isp_sendmarker |= (1 << bus);
2968		break;
2969
2970	case ASYNC_CMD_CMPLT:
2971		fast_post_handle = (ISP_READ(isp, OUTMAILBOX2) << 16) |
2972		    ISP_READ(isp, OUTMAILBOX1);
2973		isp_prt(isp, ISP_LOGDEBUG3, "fast post completion of %u",
2974		    fast_post_handle);
2975		break;
2976
2977	case ASYNC_CTIO_DONE:
2978#ifdef	ISP_TARGET_MODE
2979		/*
2980		 * Bus gets overloaded with the handle. Dual bus
2981		 * cards don't put bus# into the handle.
2982		 */
2983		bus = (ISP_READ(isp, OUTMAILBOX2) << 16) |
2984		    ISP_READ(isp, OUTMAILBOX1);
2985		isp_target_async(isp, bus, mbox);
2986#else
2987		isp_prt(isp, ISP_LOGINFO, "Fast Posting CTIO done");
2988#endif
2989		break;
2990
2991	case ASYNC_LIP_OCCURRED:
2992		FCPARAM(isp)->isp_lipseq =
2993		    ISP_READ(isp, OUTMAILBOX1);
2994		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
2995		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
2996		isp->isp_sendmarker = 1;
2997		isp_mark_getpdb_all(isp);
2998		isp_prt(isp, ISP_LOGINFO, "LIP occurred");
2999#ifdef	ISP_TARGET_MODE
3000		isp_target_async(isp, bus, mbox);
3001#endif
3002		break;
3003
3004	case ASYNC_LOOP_UP:
3005		isp->isp_sendmarker = 1;
3006		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
3007		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
3008		isp_mark_getpdb_all(isp);
3009		isp_async(isp, ISPASYNC_LOOP_UP, NULL);
3010#ifdef	ISP_TARGET_MODE
3011		isp_target_async(isp, bus, mbox);
3012#endif
3013		break;
3014
3015	case ASYNC_LOOP_DOWN:
3016		isp->isp_sendmarker = 1;
3017		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
3018		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
3019		isp_mark_getpdb_all(isp);
3020		isp_async(isp, ISPASYNC_LOOP_DOWN, NULL);
3021#ifdef	ISP_TARGET_MODE
3022		isp_target_async(isp, bus, mbox);
3023#endif
3024		break;
3025
3026	case ASYNC_LOOP_RESET:
3027		isp->isp_sendmarker = 1;
3028		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
3029		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
3030		isp_mark_getpdb_all(isp);
3031		isp_prt(isp, ISP_LOGINFO, "Loop RESET");
3032#ifdef	ISP_TARGET_MODE
3033		isp_target_async(isp, bus, mbox);
3034#endif
3035		break;
3036
3037	case ASYNC_PDB_CHANGED:
3038		isp->isp_sendmarker = 1;
3039		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
3040		isp_mark_getpdb_all(isp);
3041		isp_prt(isp, ISP_LOGINFO, "Port Database Changed");
3042		break;
3043
3044	case ASYNC_CHANGE_NOTIFY:
3045		isp_mark_getpdb_all(isp);
3046		/*
3047		 * Not correct, but it will force us to rescan the loop.
3048		 */
3049		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
3050		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, NULL);
3051		break;
3052
3053	case ASYNC_PTPMODE:
3054		if (FCPARAM(isp)->isp_onfabric)
3055			FCPARAM(isp)->isp_topo = TOPO_N_PORT;
3056		else
3057			FCPARAM(isp)->isp_topo = TOPO_F_PORT;
3058		isp_mark_getpdb_all(isp);
3059		isp->isp_sendmarker = 1;
3060		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
3061		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
3062#ifdef	ISP_TARGET_MODE
3063		isp_target_async(isp, bus, mbox);
3064#endif
3065		isp_prt(isp, ISP_LOGINFO, "Point-to-Point mode");
3066		break;
3067
3068	case ASYNC_CONNMODE:
3069		mbox = ISP_READ(isp, OUTMAILBOX1);
3070		switch (mbox) {
3071		case ISP_CONN_LOOP:
3072			isp_prt(isp, ISP_LOGINFO, "Point-to-Point->Loop mode");
3073			break;
3074		case ISP_CONN_PTP:
3075			isp_prt(isp, ISP_LOGINFO, "Loop->Point-to-Point mode");
3076			break;
3077		case ISP_CONN_BADLIP:
3078			isp_prt(isp, ISP_LOGWARN,
3079			    "Point-to-Point->Loop mode (1)");
3080			break;
3081		case ISP_CONN_FATAL:
3082			isp_prt(isp, ISP_LOGERR, "FATAL CONNECTION ERROR");
3083			isp_reinit(isp);
3084#ifdef	ISP_TARGET_MODE
3085			isp_target_async(isp, bus, ASYNC_SYSTEM_ERROR);
3086#endif
3087			/* no point continuing after this */
3088			return (-1);
3089
3090		case ISP_CONN_LOOPBACK:
3091			isp_prt(isp, ISP_LOGWARN,
3092			    "Looped Back in Point-to-Point mode");
3093		}
3094		break;
3095
3096	default:
3097		isp_prt(isp, ISP_LOGWARN, "Unknown Async Code 0x%x", mbox);
3098		break;
3099	}
3100	return (fast_post_handle);
3101}
3102
3103/*
3104 * Handle other response entries. A pointer to the request queue output
3105 * index is here in case we want to eat several entries at once, although
3106 * this is not used currently.
3107 */
3108
3109static int
3110isp_handle_other_response(isp, sp, optrp)
3111	struct ispsoftc *isp;
3112	ispstatusreq_t *sp;
3113	u_int16_t *optrp;
3114{
3115	switch (sp->req_header.rqs_entry_type) {
3116	case RQSTYPE_STATUS_CONT:
3117		isp_prt(isp, ISP_LOGINFO, "Ignored Continuation Response");
3118		return (0);
3119	case RQSTYPE_ATIO:
3120	case RQSTYPE_CTIO:
3121	case RQSTYPE_ENABLE_LUN:
3122	case RQSTYPE_MODIFY_LUN:
3123	case RQSTYPE_NOTIFY:
3124	case RQSTYPE_NOTIFY_ACK:
3125	case RQSTYPE_CTIO1:
3126	case RQSTYPE_ATIO2:
3127	case RQSTYPE_CTIO2:
3128	case RQSTYPE_CTIO3:
3129#ifdef	ISP_TARGET_MODE
3130		return (isp_target_notify(isp, sp, optrp));
3131#else
3132		optrp = optrp;
3133		/* FALLTHROUGH */
3134#endif
3135	case RQSTYPE_REQUEST:
3136	default:
3137		isp_prt(isp, ISP_LOGWARN, "Unhandled Response Type 0x%x",
3138		    sp->req_header.rqs_entry_type);
3139		return (-1);
3140	}
3141}
3142
3143static void
3144isp_parse_status(isp, sp, xs)
3145	struct ispsoftc *isp;
3146	ispstatusreq_t *sp;
3147	XS_T *xs;
3148{
3149	switch (sp->req_completion_status & 0xff) {
3150	case RQCS_COMPLETE:
3151		if (XS_NOERR(xs)) {
3152			XS_SETERR(xs, HBA_NOERROR);
3153		}
3154		return;
3155
3156	case RQCS_INCOMPLETE:
3157		if ((sp->req_state_flags & RQSF_GOT_TARGET) == 0) {
3158			isp_prt(isp, ISP_LOGDEBUG1,
3159			    "Selection Timeout for %d.%d.%d",
3160			    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3161			if (XS_NOERR(xs)) {
3162				XS_SETERR(xs, HBA_SELTIMEOUT);
3163			}
3164			return;
3165		}
3166		isp_prt(isp, ISP_LOGERR,
3167		    "command incomplete for %d.%d.%d, state 0x%x",
3168		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs),
3169		    sp->req_state_flags);
3170		break;
3171
3172	case RQCS_DMA_ERROR:
3173		isp_prt(isp, ISP_LOGERR, "DMA error for command on %d.%d.%d",
3174		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3175		break;
3176
3177	case RQCS_TRANSPORT_ERROR:
3178	{
3179		char buf[172];
3180		buf[0] = 0;
3181		STRNCAT(buf, "states=>", sizeof buf);
3182		if (sp->req_state_flags & RQSF_GOT_BUS) {
3183			STRNCAT(buf, " GOT_BUS", sizeof buf);
3184		}
3185		if (sp->req_state_flags & RQSF_GOT_TARGET) {
3186			STRNCAT(buf, " GOT_TGT", sizeof buf);
3187		}
3188		if (sp->req_state_flags & RQSF_SENT_CDB) {
3189			STRNCAT(buf, " SENT_CDB", sizeof buf);
3190		}
3191		if (sp->req_state_flags & RQSF_XFRD_DATA) {
3192			STRNCAT(buf, " XFRD_DATA", sizeof buf);
3193		}
3194		if (sp->req_state_flags & RQSF_GOT_STATUS) {
3195			STRNCAT(buf, " GOT_STS", sizeof buf);
3196		}
3197		if (sp->req_state_flags & RQSF_GOT_SENSE) {
3198			STRNCAT(buf, " GOT_SNS", sizeof buf);
3199		}
3200		if (sp->req_state_flags & RQSF_XFER_COMPLETE) {
3201			STRNCAT(buf, " XFR_CMPLT", sizeof buf);
3202		}
3203		STRNCAT(buf, "\nstatus=>", sizeof buf);
3204		if (sp->req_status_flags & RQSTF_DISCONNECT) {
3205			STRNCAT(buf, " Disconnect", sizeof buf);
3206		}
3207		if (sp->req_status_flags & RQSTF_SYNCHRONOUS) {
3208			STRNCAT(buf, " Sync_xfr", sizeof buf);
3209		}
3210		if (sp->req_status_flags & RQSTF_PARITY_ERROR) {
3211			STRNCAT(buf, " Parity", sizeof buf);
3212		}
3213		if (sp->req_status_flags & RQSTF_BUS_RESET) {
3214			STRNCAT(buf, " Bus_Reset", sizeof buf);
3215		}
3216		if (sp->req_status_flags & RQSTF_DEVICE_RESET) {
3217			STRNCAT(buf, " Device_Reset", sizeof buf);
3218		}
3219		if (sp->req_status_flags & RQSTF_ABORTED) {
3220			STRNCAT(buf, " Aborted", sizeof buf);
3221		}
3222		if (sp->req_status_flags & RQSTF_TIMEOUT) {
3223			STRNCAT(buf, " Timeout", sizeof buf);
3224		}
3225		if (sp->req_status_flags & RQSTF_NEGOTIATION) {
3226			STRNCAT(buf, " Negotiation", sizeof buf);
3227		}
3228		isp_prt(isp, ISP_LOGERR, "%s", buf);
3229		isp_prt(isp, ISP_LOGERR, "transport error for %d.%d.%d:\n%s",
3230		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), buf);
3231		break;
3232	}
3233	case RQCS_RESET_OCCURRED:
3234		isp_prt(isp, ISP_LOGWARN,
3235		    "bus reset destroyed command for %d.%d.%d",
3236		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3237		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
3238		if (XS_NOERR(xs)) {
3239			XS_SETERR(xs, HBA_BUSRESET);
3240		}
3241		return;
3242
3243	case RQCS_ABORTED:
3244		isp_prt(isp, ISP_LOGERR, "command aborted for %d.%d.%d",
3245		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3246		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
3247		if (XS_NOERR(xs)) {
3248			XS_SETERR(xs, HBA_ABORTED);
3249		}
3250		return;
3251
3252	case RQCS_TIMEOUT:
3253		isp_prt(isp, ISP_LOGWARN, "command timed out for %d.%d.%d",
3254		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3255		if (XS_NOERR(xs)) {
3256			XS_SETERR(xs, HBA_CMDTIMEOUT);
3257		}
3258		return;
3259
3260	case RQCS_DATA_OVERRUN:
3261		XS_RESID(xs) = sp->req_resid;
3262		isp_prt(isp, ISP_LOGERR, "data overrun for command on %d.%d.%d",
3263		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3264		if (XS_NOERR(xs)) {
3265			XS_SETERR(xs, HBA_DATAOVR);
3266		}
3267		return;
3268
3269	case RQCS_COMMAND_OVERRUN:
3270		isp_prt(isp, ISP_LOGERR,
3271		    "command overrun for command on %d.%d.%d",
3272		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3273		break;
3274
3275	case RQCS_STATUS_OVERRUN:
3276		isp_prt(isp, ISP_LOGERR,
3277		    "status overrun for command on %d.%d.%d",
3278		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3279		break;
3280
3281	case RQCS_BAD_MESSAGE:
3282		isp_prt(isp, ISP_LOGERR,
3283		    "msg not COMMAND COMPLETE after status %d.%d.%d",
3284		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3285		break;
3286
3287	case RQCS_NO_MESSAGE_OUT:
3288		isp_prt(isp, ISP_LOGERR,
3289		    "No MESSAGE OUT phase after selection on %d.%d.%d",
3290		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3291		break;
3292
3293	case RQCS_EXT_ID_FAILED:
3294		isp_prt(isp, ISP_LOGERR, "EXTENDED IDENTIFY failed %d.%d.%d",
3295		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3296		break;
3297
3298	case RQCS_IDE_MSG_FAILED:
3299		isp_prt(isp, ISP_LOGERR,
3300		    "INITIATOR DETECTED ERROR rejected by %d.%d.%d",
3301		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3302		break;
3303
3304	case RQCS_ABORT_MSG_FAILED:
3305		isp_prt(isp, ISP_LOGERR, "ABORT OPERATION rejected by %d.%d.%d",
3306		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3307		break;
3308
3309	case RQCS_REJECT_MSG_FAILED:
3310		isp_prt(isp, ISP_LOGERR, "MESSAGE REJECT rejected by %d.%d.%d",
3311		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3312		break;
3313
3314	case RQCS_NOP_MSG_FAILED:
3315		isp_prt(isp, ISP_LOGERR, "NOP rejected by %d.%d.%d",
3316		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3317		break;
3318
3319	case RQCS_PARITY_ERROR_MSG_FAILED:
3320		isp_prt(isp, ISP_LOGERR,
3321		    "MESSAGE PARITY ERROR rejected by %d.%d.%d",
3322		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3323		break;
3324
3325	case RQCS_DEVICE_RESET_MSG_FAILED:
3326		isp_prt(isp, ISP_LOGWARN,
3327		    "BUS DEVICE RESET rejected by %d.%d.%d",
3328		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3329		break;
3330
3331	case RQCS_ID_MSG_FAILED:
3332		isp_prt(isp, ISP_LOGERR, "IDENTIFY rejected by %d.%d.%d",
3333		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3334		break;
3335
3336	case RQCS_UNEXP_BUS_FREE:
3337		isp_prt(isp, ISP_LOGERR, "%d.%d.%d had an unexpected bus free",
3338		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3339		break;
3340
3341	case RQCS_DATA_UNDERRUN:
3342		XS_RESID(xs) = sp->req_resid;
3343		if (XS_NOERR(xs)) {
3344			XS_SETERR(xs, HBA_NOERROR);
3345		}
3346		return;
3347
3348	case RQCS_XACT_ERR1:
3349		isp_prt(isp, ISP_LOGERR, xact1, XS_CHANNEL(xs),
3350		    XS_TGT(xs), XS_LUN(xs));
3351		break;
3352
3353	case RQCS_XACT_ERR2:
3354		isp_prt(isp, ISP_LOGERR, xact2,
3355		    XS_LUN(xs), XS_TGT(xs), XS_CHANNEL(xs));
3356		break;
3357
3358	case RQCS_XACT_ERR3:
3359		isp_prt(isp, ISP_LOGERR, xact3, XS_TGT(xs),
3360		    XS_LUN(xs), XS_CHANNEL(xs));
3361		break;
3362
3363	case RQCS_BAD_ENTRY:
3364		isp_prt(isp, ISP_LOGERR, "Invalid IOCB entry type detected");
3365		break;
3366
3367	case RQCS_QUEUE_FULL:
3368		isp_prt(isp, ISP_LOGDEBUG1,
3369		    "internal queues full for %d.%d.%d status 0x%x", XS_TGT(xs),
3370		    XS_LUN(xs), XS_CHANNEL(xs), *XS_STSP(xs));
3371		/*
3372		 * If QFULL or some other status byte is set, then this
3373		 * isn't an error, per se.
3374		 */
3375		if (*XS_STSP(xs) != SCSI_GOOD && XS_NOERR(xs)) {
3376			XS_SETERR(xs, HBA_NOERROR);
3377			return;
3378		}
3379		break;
3380
3381	case RQCS_PHASE_SKIPPED:
3382		isp_prt(isp, ISP_LOGERR, pskip,
3383		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
3384		break;
3385
3386	case RQCS_ARQS_FAILED:
3387		isp_prt(isp, ISP_LOGERR,
3388		    "Auto Request Sense failed for %d.%d.%d",
3389		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
3390		if (XS_NOERR(xs)) {
3391			XS_SETERR(xs, HBA_ARQFAIL);
3392		}
3393		return;
3394
3395	case RQCS_WIDE_FAILED:
3396		isp_prt(isp, ISP_LOGERR,
3397		    "Wide Negotiation failed for %d.%d.%d",
3398		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
3399		if (IS_SCSI(isp)) {
3400			sdparam *sdp = isp->isp_param;
3401			sdp += XS_CHANNEL(xs);
3402			sdp->isp_devparam[XS_TGT(xs)].dev_flags &= ~DPARM_WIDE;
3403			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
3404			isp->isp_update |= (1 << XS_CHANNEL(xs));
3405		}
3406		if (XS_NOERR(xs)) {
3407			XS_SETERR(xs, HBA_NOERROR);
3408		}
3409		return;
3410
3411	case RQCS_SYNCXFER_FAILED:
3412		isp_prt(isp, ISP_LOGERR,
3413		    "SDTR Message failed for target %d.%d.%d",
3414		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
3415		if (IS_SCSI(isp)) {
3416			sdparam *sdp = isp->isp_param;
3417			sdp += XS_CHANNEL(xs);
3418			sdp->isp_devparam[XS_TGT(xs)].dev_flags &= ~DPARM_SYNC;
3419			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
3420			isp->isp_update |= (1 << XS_CHANNEL(xs));
3421		}
3422		break;
3423
3424	case RQCS_LVD_BUSERR:
3425		isp_prt(isp, ISP_LOGERR,
3426		    "Bad LVD condition while talking to %d.%d.%d",
3427		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
3428		break;
3429
3430	case RQCS_PORT_UNAVAILABLE:
3431		/*
3432		 * No such port on the loop. Moral equivalent of SELTIMEO
3433		 */
3434		isp_prt(isp, ISP_LOGINFO,
3435		    "Port Unavailable for target %d", XS_TGT(xs));
3436		if (XS_NOERR(xs)) {
3437			XS_SETERR(xs, HBA_SELTIMEOUT);
3438		}
3439		return;
3440
3441	case RQCS_PORT_LOGGED_OUT:
3442		/*
3443		 * It was there (maybe)- treat as a selection timeout.
3444		 */
3445		isp_prt(isp, ISP_LOGINFO,
3446		    "port logout for target %d", XS_TGT(xs));
3447		if (XS_NOERR(xs)) {
3448			XS_SETERR(xs, HBA_SELTIMEOUT);
3449		}
3450		return;
3451
3452	case RQCS_PORT_CHANGED:
3453		isp_prt(isp, ISP_LOGWARN,
3454		    "port changed for target %d", XS_TGT(xs));
3455		if (XS_NOERR(xs)) {
3456			XS_SETERR(xs, HBA_SELTIMEOUT);
3457		}
3458		return;
3459
3460	case RQCS_PORT_BUSY:
3461		isp_prt(isp, ISP_LOGWARN,
3462		    "port busy for target %d", XS_TGT(xs));
3463		if (XS_NOERR(xs)) {
3464			XS_SETERR(xs, HBA_TGTBSY);
3465		}
3466		return;
3467
3468	default:
3469		isp_prt(isp, ISP_LOGERR, "Unknown Completion Status 0x%x",
3470		    sp->req_completion_status);
3471		break;
3472	}
3473	if (XS_NOERR(xs)) {
3474		XS_SETERR(xs, HBA_BOTCH);
3475	}
3476}
3477
3478static void
3479isp_fastpost_complete(isp, fph)
3480	struct ispsoftc *isp;
3481	u_int32_t fph;
3482{
3483	XS_T *xs;
3484
3485	if (fph < 1) {
3486		return;
3487	}
3488	xs = isp_find_xs(isp, fph);
3489	if (xs == NULL) {
3490		isp_prt(isp, ISP_LOGWARN,
3491		    "Command for fast post handle 0x%x not found", fph);
3492		return;
3493	}
3494	isp_destroy_handle(isp, fph);
3495
3496	/*
3497	 * Since we don't have a result queue entry item,
3498	 * we must believe that SCSI status is zero and
3499	 * that all data transferred.
3500	 */
3501	XS_SET_STATE_STAT(isp, xs, NULL);
3502	XS_RESID(xs) = 0;
3503	*XS_STSP(xs) = SCSI_GOOD;
3504	if (XS_XFRLEN(xs)) {
3505		ISP_DMAFREE(isp, xs, fph);
3506	}
3507	if (isp->isp_nactive)
3508		isp->isp_nactive--;
3509	isp_done(xs);
3510}
3511
3512#define	HIBYT(x)			((x) >> 0x8)
3513#define	LOBYT(x)			((x)  & 0xff)
3514#define	ISPOPMAP(a, b)			(((a) << 8) | (b))
3515static u_int16_t mbpscsi[] = {
3516	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
3517	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
3518	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
3519	ISPOPMAP(0x1f, 0x01),	/* 0x03: MBOX_DUMP_RAM */
3520	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
3521	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
3522	ISPOPMAP(0x3f, 0x3f),	/* 0x06: MBOX_MAILBOX_REG_TEST */
3523	ISPOPMAP(0x03, 0x07),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
3524	ISPOPMAP(0x01, 0x0f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
3525	ISPOPMAP(0x00, 0x00),	/* 0x09: */
3526	ISPOPMAP(0x00, 0x00),	/* 0x0a: */
3527	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
3528	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
3529	ISPOPMAP(0x00, 0x00),	/* 0x0d: */
3530	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
3531	ISPOPMAP(0x00, 0x00),	/* 0x0f: */
3532	ISPOPMAP(0x1f, 0x1f),	/* 0x10: MBOX_INIT_REQ_QUEUE */
3533	ISPOPMAP(0x3f, 0x3f),	/* 0x11: MBOX_INIT_RES_QUEUE */
3534	ISPOPMAP(0x0f, 0x0f),	/* 0x12: MBOX_EXECUTE_IOCB */
3535	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
3536	ISPOPMAP(0x01, 0x3f),	/* 0x14: MBOX_STOP_FIRMWARE */
3537	ISPOPMAP(0x0f, 0x0f),	/* 0x15: MBOX_ABORT */
3538	ISPOPMAP(0x03, 0x03),	/* 0x16: MBOX_ABORT_DEVICE */
3539	ISPOPMAP(0x07, 0x07),	/* 0x17: MBOX_ABORT_TARGET */
3540	ISPOPMAP(0x07, 0x07),	/* 0x18: MBOX_BUS_RESET */
3541	ISPOPMAP(0x03, 0x07),	/* 0x19: MBOX_STOP_QUEUE */
3542	ISPOPMAP(0x03, 0x07),	/* 0x1a: MBOX_START_QUEUE */
3543	ISPOPMAP(0x03, 0x07),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
3544	ISPOPMAP(0x03, 0x07),	/* 0x1c: MBOX_ABORT_QUEUE */
3545	ISPOPMAP(0x03, 0x4f),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
3546	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
3547	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
3548	ISPOPMAP(0x01, 0x07),	/* 0x20: MBOX_GET_INIT_SCSI_ID */
3549	ISPOPMAP(0x01, 0x07),	/* 0x21: MBOX_GET_SELECT_TIMEOUT */
3550	ISPOPMAP(0x01, 0xc7),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
3551	ISPOPMAP(0x01, 0x07),	/* 0x23: MBOX_GET_TAG_AGE_LIMIT */
3552	ISPOPMAP(0x01, 0x03),	/* 0x24: MBOX_GET_CLOCK_RATE */
3553	ISPOPMAP(0x01, 0x07),	/* 0x25: MBOX_GET_ACT_NEG_STATE */
3554	ISPOPMAP(0x01, 0x07),	/* 0x26: MBOX_GET_ASYNC_DATA_SETUP_TIME */
3555	ISPOPMAP(0x01, 0x07),	/* 0x27: MBOX_GET_PCI_PARAMS */
3556	ISPOPMAP(0x03, 0x4f),	/* 0x28: MBOX_GET_TARGET_PARAMS */
3557	ISPOPMAP(0x03, 0x0f),	/* 0x29: MBOX_GET_DEV_QUEUE_PARAMS */
3558	ISPOPMAP(0x01, 0x07),	/* 0x2a: MBOX_GET_RESET_DELAY_PARAMS */
3559	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
3560	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
3561	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
3562	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
3563	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
3564	ISPOPMAP(0x03, 0x03),	/* 0x30: MBOX_SET_INIT_SCSI_ID */
3565	ISPOPMAP(0x07, 0x07),	/* 0x31: MBOX_SET_SELECT_TIMEOUT */
3566	ISPOPMAP(0xc7, 0xc7),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
3567	ISPOPMAP(0x07, 0x07),	/* 0x33: MBOX_SET_TAG_AGE_LIMIT */
3568	ISPOPMAP(0x03, 0x03),	/* 0x34: MBOX_SET_CLOCK_RATE */
3569	ISPOPMAP(0x07, 0x07),	/* 0x35: MBOX_SET_ACT_NEG_STATE */
3570	ISPOPMAP(0x07, 0x07),	/* 0x36: MBOX_SET_ASYNC_DATA_SETUP_TIME */
3571	ISPOPMAP(0x07, 0x07),	/* 0x37: MBOX_SET_PCI_CONTROL_PARAMS */
3572	ISPOPMAP(0x4f, 0x4f),	/* 0x38: MBOX_SET_TARGET_PARAMS */
3573	ISPOPMAP(0x0f, 0x0f),	/* 0x39: MBOX_SET_DEV_QUEUE_PARAMS */
3574	ISPOPMAP(0x07, 0x07),	/* 0x3a: MBOX_SET_RESET_DELAY_PARAMS */
3575	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
3576	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
3577	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
3578	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
3579	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
3580	ISPOPMAP(0x01, 0x03),	/* 0x40: MBOX_RETURN_BIOS_BLOCK_ADDR */
3581	ISPOPMAP(0x3f, 0x01),	/* 0x41: MBOX_WRITE_FOUR_RAM_WORDS */
3582	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_EXEC_BIOS_IOCB */
3583	ISPOPMAP(0x00, 0x00),	/* 0x43: */
3584	ISPOPMAP(0x00, 0x00),	/* 0x44: */
3585	ISPOPMAP(0x03, 0x03),	/* 0x45: SET SYSTEM PARAMETER */
3586	ISPOPMAP(0x01, 0x03),	/* 0x46: GET SYSTEM PARAMETER */
3587	ISPOPMAP(0x00, 0x00),	/* 0x47: */
3588	ISPOPMAP(0x01, 0xcf),	/* 0x48: GET SCAM CONFIGURATION */
3589	ISPOPMAP(0xcf, 0xcf),	/* 0x49: SET SCAM CONFIGURATION */
3590	ISPOPMAP(0x03, 0x03),	/* 0x4a: MBOX_SET_FIRMWARE_FEATURES */
3591	ISPOPMAP(0x01, 0x03),	/* 0x4b: MBOX_GET_FIRMWARE_FEATURES */
3592	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
3593	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
3594	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
3595	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
3596	ISPOPMAP(0xdf, 0xdf),	/* 0x50: LOAD RAM A64 */
3597	ISPOPMAP(0xdf, 0xdf),	/* 0x51: DUMP RAM A64 */
3598	ISPOPMAP(0xdf, 0xdf),	/* 0x52: INITIALIZE REQUEST QUEUE A64 */
3599	ISPOPMAP(0xff, 0xff),	/* 0x53: INITIALIZE RESPONSE QUEUE A64 */
3600	ISPOPMAP(0xcf, 0xff),	/* 0x54: EXECUTE IOCB A64 */
3601	ISPOPMAP(0x03, 0x01),	/* 0x55: ENABLE TARGET MODE */
3602	ISPOPMAP(0x00, 0x00),	/* 0x56: */
3603	ISPOPMAP(0x00, 0x00),	/* 0x57: */
3604	ISPOPMAP(0x00, 0x00),	/* 0x58: */
3605	ISPOPMAP(0x00, 0x00),	/* 0x59: */
3606	ISPOPMAP(0x03, 0x03),	/* 0x5a: SET DATA OVERRUN RECOVERY MODE */
3607	ISPOPMAP(0x01, 0x03),	/* 0x5b: GET DATA OVERRUN RECOVERY MODE */
3608	ISPOPMAP(0x0f, 0x0f),	/* 0x5c: SET HOST DATA */
3609	ISPOPMAP(0x01, 0x01)	/* 0x5d: GET NOST DATA */
3610};
3611
3612#ifndef	ISP_STRIPEED
3613static char *scsi_mbcmd_names[] = {
3614	"NO-OP",
3615	"LOAD RAM",
3616	"EXEC FIRMWARE",
3617	"DUMP RAM",
3618	"WRITE RAM WORD",
3619	"READ RAM WORD",
3620	"MAILBOX REG TEST",
3621	"VERIFY CHECKSUM",
3622	"ABOUT FIRMWARE",
3623	NULL,
3624	NULL,
3625	NULL,
3626	NULL,
3627	NULL,
3628	"CHECK FIRMWARE",
3629	NULL,
3630	"INIT REQUEST QUEUE",
3631	"INIT RESULT QUEUE",
3632	"EXECUTE IOCB",
3633	"WAKE UP",
3634	"STOP FIRMWARE",
3635	"ABORT",
3636	"ABORT DEVICE",
3637	"ABORT TARGET",
3638	"BUS RESET",
3639	"STOP QUEUE",
3640	"START QUEUE",
3641	"SINGLE STEP QUEUE",
3642	"ABORT QUEUE",
3643	"GET DEV QUEUE STATUS",
3644	NULL,
3645	"GET FIRMWARE STATUS",
3646	"GET INIT SCSI ID",
3647	"GET SELECT TIMEOUT",
3648	"GET RETRY COUNT",
3649	"GET TAG AGE LIMIT",
3650	"GET CLOCK RATE",
3651	"GET ACT NEG STATE",
3652	"GET ASYNC DATA SETUP TIME",
3653	"GET PCI PARAMS",
3654	"GET TARGET PARAMS",
3655	"GET DEV QUEUE PARAMS",
3656	"GET RESET DELAY PARAMS",
3657	NULL,
3658	NULL,
3659	NULL,
3660	NULL,
3661	NULL,
3662	"SET INIT SCSI ID",
3663	"SET SELECT TIMEOUT",
3664	"SET RETRY COUNT",
3665	"SET TAG AGE LIMIT",
3666	"SET CLOCK RATE",
3667	"SET ACT NEG STATE",
3668	"SET ASYNC DATA SETUP TIME",
3669	"SET PCI CONTROL PARAMS",
3670	"SET TARGET PARAMS",
3671	"SET DEV QUEUE PARAMS",
3672	"SET RESET DELAY PARAMS",
3673	NULL,
3674	NULL,
3675	NULL,
3676	NULL,
3677	NULL,
3678	"RETURN BIOS BLOCK ADDR",
3679	"WRITE FOUR RAM WORDS",
3680	"EXEC BIOS IOCB",
3681	NULL,
3682	NULL,
3683	"SET SYSTEM PARAMETER",
3684	"GET SYSTEM PARAMETER",
3685	NULL,
3686	"GET SCAM CONFIGURATION",
3687	"SET SCAM CONFIGURATION",
3688	"SET FIRMWARE FEATURES",
3689	"GET FIRMWARE FEATURES",
3690	NULL,
3691	NULL,
3692	NULL,
3693	NULL,
3694	"LOAD RAM A64",
3695	"DUMP RAM A64",
3696	"INITIALIZE REQUEST QUEUE A64",
3697	"INITIALIZE RESPONSE QUEUE A64",
3698	"EXECUTE IOCB A64",
3699	"ENABLE TARGET MODE",
3700	NULL,
3701	NULL,
3702	NULL,
3703	NULL,
3704	"SET DATA OVERRUN RECOVERY MODE",
3705	"GET DATA OVERRUN RECOVERY MODE",
3706	"SET HOST DATA",
3707	"GET NOST DATA",
3708};
3709#endif
3710
3711static u_int16_t mbpfc[] = {
3712	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
3713	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
3714	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
3715	ISPOPMAP(0x1f, 0x01),	/* 0x03: MBOX_DUMP_RAM */
3716	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
3717	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
3718	ISPOPMAP(0xff, 0xff),	/* 0x06: MBOX_MAILBOX_REG_TEST */
3719	ISPOPMAP(0x03, 0x05),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
3720	ISPOPMAP(0x01, 0x0f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
3721	ISPOPMAP(0xdf, 0x01),	/* 0x09: LOAD RAM */
3722	ISPOPMAP(0xdf, 0x01),	/* 0x0a: DUMP RAM */
3723	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
3724	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
3725	ISPOPMAP(0x00, 0x00),	/* 0x0d: */
3726	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
3727	ISPOPMAP(0x00, 0x00),	/* 0x0f: */
3728	ISPOPMAP(0x1f, 0x11),	/* 0x10: MBOX_INIT_REQ_QUEUE */
3729	ISPOPMAP(0x2f, 0x21),	/* 0x11: MBOX_INIT_RES_QUEUE */
3730	ISPOPMAP(0x0f, 0x01),	/* 0x12: MBOX_EXECUTE_IOCB */
3731	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
3732	ISPOPMAP(0x01, 0xff),	/* 0x14: MBOX_STOP_FIRMWARE */
3733	ISPOPMAP(0x4f, 0x01),	/* 0x15: MBOX_ABORT */
3734	ISPOPMAP(0x07, 0x01),	/* 0x16: MBOX_ABORT_DEVICE */
3735	ISPOPMAP(0x07, 0x01),	/* 0x17: MBOX_ABORT_TARGET */
3736	ISPOPMAP(0x03, 0x03),	/* 0x18: MBOX_BUS_RESET */
3737	ISPOPMAP(0x07, 0x05),	/* 0x19: MBOX_STOP_QUEUE */
3738	ISPOPMAP(0x07, 0x05),	/* 0x1a: MBOX_START_QUEUE */
3739	ISPOPMAP(0x07, 0x05),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
3740	ISPOPMAP(0x07, 0x05),	/* 0x1c: MBOX_ABORT_QUEUE */
3741	ISPOPMAP(0x07, 0x03),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
3742	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
3743	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
3744	ISPOPMAP(0x01, 0x4f),	/* 0x20: MBOX_GET_LOOP_ID */
3745	ISPOPMAP(0x00, 0x00),	/* 0x21: */
3746	ISPOPMAP(0x01, 0x07),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
3747	ISPOPMAP(0x00, 0x00),	/* 0x23: */
3748	ISPOPMAP(0x00, 0x00),	/* 0x24: */
3749	ISPOPMAP(0x00, 0x00),	/* 0x25: */
3750	ISPOPMAP(0x00, 0x00),	/* 0x26: */
3751	ISPOPMAP(0x00, 0x00),	/* 0x27: */
3752	ISPOPMAP(0x0f, 0x1),	/* 0x28: MBOX_GET_FIRMWARE_OPTIONS */
3753	ISPOPMAP(0x03, 0x07),	/* 0x29: MBOX_GET_PORT_QUEUE_PARAMS */
3754	ISPOPMAP(0x00, 0x00),	/* 0x2a: */
3755	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
3756	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
3757	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
3758	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
3759	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
3760	ISPOPMAP(0x00, 0x00),	/* 0x30: */
3761	ISPOPMAP(0x00, 0x00),	/* 0x31: */
3762	ISPOPMAP(0x07, 0x07),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
3763	ISPOPMAP(0x00, 0x00),	/* 0x33: */
3764	ISPOPMAP(0x00, 0x00),	/* 0x34: */
3765	ISPOPMAP(0x00, 0x00),	/* 0x35: */
3766	ISPOPMAP(0x00, 0x00),	/* 0x36: */
3767	ISPOPMAP(0x00, 0x00),	/* 0x37: */
3768	ISPOPMAP(0x0f, 0x01),	/* 0x38: MBOX_SET_FIRMWARE_OPTIONS */
3769	ISPOPMAP(0x0f, 0x07),	/* 0x39: MBOX_SET_PORT_QUEUE_PARAMS */
3770	ISPOPMAP(0x00, 0x00),	/* 0x3a: */
3771	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
3772	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
3773	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
3774	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
3775	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
3776	ISPOPMAP(0x03, 0x01),	/* 0x40: MBOX_LOOP_PORT_BYPASS */
3777	ISPOPMAP(0x03, 0x01),	/* 0x41: MBOX_LOOP_PORT_ENABLE */
3778	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_GET_RESOURCE_COUNTS */
3779	ISPOPMAP(0x01, 0x01),	/* 0x43: MBOX_REQUEST_NON_PARTICIPATING_MODE */
3780	ISPOPMAP(0x00, 0x00),	/* 0x44: */
3781	ISPOPMAP(0x00, 0x00),	/* 0x45: */
3782	ISPOPMAP(0x00, 0x00),	/* 0x46: */
3783	ISPOPMAP(0xcf, 0x03),	/* 0x47: GET PORT_DATABASE ENHANCED */
3784	ISPOPMAP(0x00, 0x00),	/* 0x48: */
3785	ISPOPMAP(0x00, 0x00),	/* 0x49: */
3786	ISPOPMAP(0x00, 0x00),	/* 0x4a: */
3787	ISPOPMAP(0x00, 0x00),	/* 0x4b: */
3788	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
3789	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
3790	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
3791	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
3792	ISPOPMAP(0x00, 0x00),	/* 0x50: */
3793	ISPOPMAP(0x00, 0x00),	/* 0x51: */
3794	ISPOPMAP(0x00, 0x00),	/* 0x52: */
3795	ISPOPMAP(0x00, 0x00),	/* 0x53: */
3796	ISPOPMAP(0xcf, 0x01),	/* 0x54: EXECUTE IOCB A64 */
3797	ISPOPMAP(0x00, 0x00),	/* 0x55: */
3798	ISPOPMAP(0x00, 0x00),	/* 0x56: */
3799	ISPOPMAP(0x00, 0x00),	/* 0x57: */
3800	ISPOPMAP(0x00, 0x00),	/* 0x58: */
3801	ISPOPMAP(0x00, 0x00),	/* 0x59: */
3802	ISPOPMAP(0x00, 0x00),	/* 0x5a: */
3803	ISPOPMAP(0x00, 0x00),	/* 0x5b: */
3804	ISPOPMAP(0x00, 0x00),	/* 0x5c: */
3805	ISPOPMAP(0x00, 0x00),	/* 0x5d: */
3806	ISPOPMAP(0x00, 0x00),	/* 0x5e: */
3807	ISPOPMAP(0x00, 0x00),	/* 0x5f: */
3808	ISPOPMAP(0xfd, 0x31),	/* 0x60: MBOX_INIT_FIRMWARE */
3809	ISPOPMAP(0x00, 0x00),	/* 0x61: */
3810	ISPOPMAP(0x01, 0x01),	/* 0x62: MBOX_INIT_LIP */
3811	ISPOPMAP(0xcd, 0x03),	/* 0x63: MBOX_GET_FC_AL_POSITION_MAP */
3812	ISPOPMAP(0xcf, 0x01),	/* 0x64: MBOX_GET_PORT_DB */
3813	ISPOPMAP(0x07, 0x01),	/* 0x65: MBOX_CLEAR_ACA */
3814	ISPOPMAP(0x07, 0x01),	/* 0x66: MBOX_TARGET_RESET */
3815	ISPOPMAP(0x07, 0x01),	/* 0x67: MBOX_CLEAR_TASK_SET */
3816	ISPOPMAP(0x07, 0x01),	/* 0x68: MBOX_ABORT_TASK_SET */
3817	ISPOPMAP(0x01, 0x07),	/* 0x69: MBOX_GET_FW_STATE */
3818	ISPOPMAP(0x03, 0xcf),	/* 0x6a: MBOX_GET_PORT_NAME */
3819	ISPOPMAP(0xcf, 0x01),	/* 0x6b: MBOX_GET_LINK_STATUS */
3820	ISPOPMAP(0x0f, 0x01),	/* 0x6c: MBOX_INIT_LIP_RESET */
3821	ISPOPMAP(0x00, 0x00),	/* 0x6d: */
3822	ISPOPMAP(0xcf, 0x03),	/* 0x6e: MBOX_SEND_SNS */
3823	ISPOPMAP(0x0f, 0x07),	/* 0x6f: MBOX_FABRIC_LOGIN */
3824	ISPOPMAP(0x03, 0x01),	/* 0x70: MBOX_SEND_CHANGE_REQUEST */
3825	ISPOPMAP(0x03, 0x03),	/* 0x71: MBOX_FABRIC_LOGOUT */
3826	ISPOPMAP(0x0f, 0x0f),	/* 0x72: MBOX_INIT_LIP_LOGIN */
3827	ISPOPMAP(0x00, 0x00),	/* 0x73: */
3828	ISPOPMAP(0x07, 0x01),	/* 0x74: LOGIN LOOP PORT */
3829	ISPOPMAP(0xcf, 0x03),	/* 0x75: GET PORT/NODE NAME LIST */
3830	ISPOPMAP(0x4f, 0x01),	/* 0x76: SET VENDOR ID */
3831	ISPOPMAP(0xcd, 0x01),	/* 0x77: INITIALIZE IP MAILBOX */
3832	ISPOPMAP(0x00, 0x00),	/* 0x78: */
3833	ISPOPMAP(0x00, 0x00),	/* 0x79: */
3834	ISPOPMAP(0x00, 0x00),	/* 0x7a: */
3835	ISPOPMAP(0x00, 0x00),	/* 0x7b: */
3836	ISPOPMAP(0x4f, 0x03),	/* 0x7c: Get ID List */
3837	ISPOPMAP(0xcf, 0x01),	/* 0x7d: SEND LFA */
3838	ISPOPMAP(0x07, 0x01)	/* 0x7e: Lun RESET */
3839};
3840
3841#ifndef	ISP_STRIPPED
3842static char *fc_mbcmd_names[] = {
3843	"NO-OP",
3844	"LOAD RAM",
3845	"EXEC FIRMWARE",
3846	"DUMP RAM",
3847	"WRITE RAM WORD",
3848	"READ RAM WORD",
3849	"MAILBOX REG TEST",
3850	"VERIFY CHECKSUM",
3851	"ABOUT FIRMWARE",
3852	"LOAD RAM",
3853	"DUMP RAM",
3854	NULL,
3855	NULL,
3856	NULL,
3857	"CHECK FIRMWARE",
3858	NULL,
3859	"INIT REQUEST QUEUE",
3860	"INIT RESULT QUEUE",
3861	"EXECUTE IOCB",
3862	"WAKE UP",
3863	"STOP FIRMWARE",
3864	"ABORT",
3865	"ABORT DEVICE",
3866	"ABORT TARGET",
3867	"BUS RESET",
3868	"STOP QUEUE",
3869	"START QUEUE",
3870	"SINGLE STEP QUEUE",
3871	"ABORT QUEUE",
3872	"GET DEV QUEUE STATUS",
3873	NULL,
3874	"GET FIRMWARE STATUS",
3875	"GET LOOP ID",
3876	NULL,
3877	"GET RETRY COUNT",
3878	NULL,
3879	NULL,
3880	NULL,
3881	NULL,
3882	NULL,
3883	"GET FIRMWARE OPTIONS",
3884	"GET PORT QUEUE PARAMS",
3885	NULL,
3886	NULL,
3887	NULL,
3888	NULL,
3889	NULL,
3890	NULL,
3891	NULL,
3892	NULL,
3893	"SET RETRY COUNT",
3894	NULL,
3895	NULL,
3896	NULL,
3897	NULL,
3898	NULL,
3899	"SET FIRMWARE OPTIONS",
3900	"SET PORT QUEUE PARAMS",
3901	NULL,
3902	NULL,
3903	NULL,
3904	NULL,
3905	NULL,
3906	NULL,
3907	"LOOP PORT BYPASS",
3908	"LOOP PORT ENABLE",
3909	"GET RESOURCE COUNTS",
3910	"REQUEST NON PARTICIPATING MODE",
3911	NULL,
3912	NULL,
3913	NULL,
3914	"GET PORT DATABASE,, ENHANCED",
3915	NULL,
3916	NULL,
3917	NULL,
3918	NULL,
3919	NULL,
3920	NULL,
3921	NULL,
3922	NULL,
3923	NULL,
3924	NULL,
3925	NULL,
3926	NULL,
3927	"EXECUTE IOCB A64",
3928	NULL,
3929	NULL,
3930	NULL,
3931	NULL,
3932	NULL,
3933	NULL,
3934	NULL,
3935	NULL,
3936	NULL,
3937	NULL,
3938	NULL,
3939	"INIT FIRMWARE",
3940	NULL,
3941	"INIT LIP",
3942	"GET FC-AL POSITION MAP",
3943	"GET PORT DATABASE",
3944	"CLEAR ACA",
3945	"TARGET RESET",
3946	"CLEAR TASK SET",
3947	"ABORT TASK SET",
3948	"GET FW STATE",
3949	"GET PORT NAME",
3950	"GET LINK STATUS",
3951	"INIT LIP RESET",
3952	NULL,
3953	"SEND SNS",
3954	"FABRIC LOGIN",
3955	"SEND CHANGE REQUEST",
3956	"FABRIC LOGOUT",
3957	"INIT LIP LOGIN",
3958	NULL,
3959	"LOGIN LOOP PORT",
3960	"GET PORT/NODE NAME LIST",
3961	"SET VENDOR ID",
3962	"INITIALIZE IP MAILBOX",
3963	NULL,
3964	NULL,
3965	NULL,
3966	NULL,
3967	"Get ID List",
3968	"SEND LFA",
3969	"Lun RESET"
3970};
3971#endif
3972
3973static void
3974isp_mboxcmd(isp, mbp, logmask)
3975	struct ispsoftc *isp;
3976	mbreg_t *mbp;
3977	int logmask;
3978{
3979	char *cname, *xname, tname[16], mname[16];
3980	unsigned int lim, ibits, obits, box, opcode;
3981	u_int16_t *mcp;
3982
3983	if (IS_FC(isp)) {
3984		mcp = mbpfc;
3985		lim = (sizeof (mbpfc) / sizeof (mbpfc[0]));
3986	} else {
3987		mcp = mbpscsi;
3988		lim = (sizeof (mbpscsi) / sizeof (mbpscsi[0]));
3989	}
3990
3991	if ((opcode = mbp->param[0]) >= lim) {
3992		mbp->param[0] = MBOX_INVALID_COMMAND;
3993		isp_prt(isp, ISP_LOGERR, "Unknown Command 0x%x", opcode);
3994		return;
3995	}
3996
3997	ibits = HIBYT(mcp[opcode]) & NMBOX_BMASK(isp);
3998	obits = LOBYT(mcp[opcode]) & NMBOX_BMASK(isp);
3999
4000	if (ibits == 0 && obits == 0) {
4001		mbp->param[0] = MBOX_COMMAND_PARAM_ERROR;
4002		isp_prt(isp, ISP_LOGERR, "no parameters for 0x%x", opcode);
4003		return;
4004	}
4005
4006	/*
4007	 * Get exclusive usage of mailbox registers.
4008	 */
4009	MBOX_ACQUIRE(isp);
4010
4011	for (box = 0; box < MAX_MAILBOX; box++) {
4012		if (ibits & (1 << box)) {
4013			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
4014		}
4015		isp->isp_mboxtmp[box] = mbp->param[box] = 0;
4016	}
4017
4018	/*
4019	 * We assume that we can't overwrite a previous command.
4020	 */
4021	isp->isp_mboxbsy = obits;
4022
4023	/*
4024	 * Set Host Interrupt condition so that RISC will pick up mailbox regs.
4025	 */
4026	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
4027
4028	/*
4029	 * While we haven't finished the command, spin our wheels here.
4030	 */
4031	MBOX_WAIT_COMPLETE(isp);
4032
4033	/*
4034	 * Copy back output registers.
4035	 */
4036	for (box = 0; box < MAX_MAILBOX; box++) {
4037		if (obits & (1 << box)) {
4038			mbp->param[box] = isp->isp_mboxtmp[box];
4039		}
4040	}
4041
4042	MBOX_RELEASE(isp);
4043
4044	if (logmask == 0 || opcode == MBOX_EXEC_FIRMWARE) {
4045		return;
4046	}
4047#ifdef	ISP_STRIPPED
4048	cname = NULL;
4049#else
4050	cname = (IS_FC(isp))? fc_mbcmd_names[opcode] : scsi_mbcmd_names[opcode];
4051#endif
4052	if (cname == NULL) {
4053		SNPRINTF(cname, sizeof tname, "opcode %x", opcode);
4054	}
4055
4056	/*
4057	 * Just to be chatty here...
4058	 */
4059	xname = NULL;
4060	switch (mbp->param[0]) {
4061	case MBOX_COMMAND_COMPLETE:
4062		break;
4063	case MBOX_INVALID_COMMAND:
4064		if (logmask & MBLOGMASK(MBOX_COMMAND_COMPLETE))
4065			xname = "INVALID COMMAND";
4066		break;
4067	case MBOX_HOST_INTERFACE_ERROR:
4068		if (logmask & MBLOGMASK(MBOX_HOST_INTERFACE_ERROR))
4069			xname = "HOST INTERFACE ERROR";
4070		break;
4071	case MBOX_TEST_FAILED:
4072		if (logmask & MBLOGMASK(MBOX_TEST_FAILED))
4073			xname = "TEST FAILED";
4074		break;
4075	case MBOX_COMMAND_ERROR:
4076		if (logmask & MBLOGMASK(MBOX_COMMAND_ERROR))
4077			xname = "COMMAND ERROR";
4078		break;
4079	case MBOX_COMMAND_PARAM_ERROR:
4080		if (logmask & MBLOGMASK(MBOX_COMMAND_PARAM_ERROR))
4081			xname = "COMMAND PARAMETER ERROR";
4082		break;
4083	case MBOX_LOOP_ID_USED:
4084		if (logmask & MBLOGMASK(MBOX_LOOP_ID_USED))
4085			xname = "LOOP ID ALREADY IN USE";
4086		break;
4087	case MBOX_PORT_ID_USED:
4088		if (logmask & MBLOGMASK(MBOX_PORT_ID_USED))
4089			xname = "PORT ID ALREADY IN USE";
4090		break;
4091	case MBOX_ALL_IDS_USED:
4092		if (logmask & MBLOGMASK(MBOX_ALL_IDS_USED))
4093			xname = "ALL LOOP IDS IN USE";
4094		break;
4095	case 0:		/* special case */
4096		xname = "TIMEOUT";
4097		break;
4098	default:
4099		SNPRINTF(mname, sizeof mname, "error 0x%x", mbp->param[0]);
4100		xname = mname;
4101		break;
4102	}
4103	if (xname)
4104		isp_prt(isp, ISP_LOGALL, "Mailbox Command '%s' failed (%s)",
4105		    cname, xname);
4106}
4107
4108static void
4109isp_fw_state(isp)
4110	struct ispsoftc *isp;
4111{
4112	if (IS_FC(isp)) {
4113		mbreg_t mbs;
4114		fcparam *fcp = isp->isp_param;
4115
4116		mbs.param[0] = MBOX_GET_FW_STATE;
4117		isp_mboxcmd(isp, &mbs, MBLOGALL);
4118		if (mbs.param[0] == MBOX_COMMAND_COMPLETE)
4119			fcp->isp_fwstate = mbs.param[1];
4120	}
4121}
4122
4123static void
4124isp_update(isp)
4125	struct ispsoftc *isp;
4126{
4127	int bus, upmask;
4128
4129	for (bus = 0, upmask = isp->isp_update; upmask != 0; bus++) {
4130		if (upmask & (1 << bus)) {
4131			isp_update_bus(isp, bus);
4132		}
4133		upmask &= ~(1 << bus);
4134	}
4135}
4136
4137static void
4138isp_update_bus(isp, bus)
4139	struct ispsoftc *isp;
4140	int bus;
4141{
4142	int tgt;
4143	mbreg_t mbs;
4144	sdparam *sdp;
4145
4146	isp->isp_update &= ~(1 << bus);
4147	if (IS_FC(isp)) {
4148		/*
4149		 * There are no 'per-bus' settings for Fibre Channel.
4150		 */
4151		return;
4152	}
4153	sdp = isp->isp_param;
4154	sdp += bus;
4155
4156	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
4157		u_int16_t flags, period, offset;
4158		int get;
4159
4160		if (sdp->isp_devparam[tgt].dev_enable == 0) {
4161			sdp->isp_devparam[tgt].dev_update = 0;
4162			sdp->isp_devparam[tgt].dev_refresh = 0;
4163			isp_prt(isp, ISP_LOGDEBUG1,
4164	 		    "skipping target %d bus %d update", tgt, bus);
4165			continue;
4166		}
4167		/*
4168		 * If the goal is to update the status of the device,
4169		 * take what's in dev_flags and try and set the device
4170		 * toward that. Otherwise, if we're just refreshing the
4171		 * current device state, get the current parameters.
4172		 */
4173
4174		/*
4175		 * Refresh overrides set
4176		 */
4177		if (sdp->isp_devparam[tgt].dev_refresh) {
4178			mbs.param[0] = MBOX_GET_TARGET_PARAMS;
4179			sdp->isp_devparam[tgt].dev_refresh = 0;
4180			get = 1;
4181		} else if (sdp->isp_devparam[tgt].dev_update) {
4182			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
4183			/*
4184			 * Make sure dev_flags has "Renegotiate on Error"
4185			 * on and "Freeze Queue on Error" off.
4186			 */
4187			sdp->isp_devparam[tgt].dev_flags |= DPARM_RENEG;
4188			sdp->isp_devparam[tgt].dev_flags &= ~DPARM_QFRZ;
4189
4190			mbs.param[2] = sdp->isp_devparam[tgt].dev_flags;
4191
4192			/*
4193			 * Insist that PARITY must be enabled
4194			 * if SYNC or WIDE is enabled.
4195			 */
4196			if ((mbs.param[2] & (DPARM_SYNC|DPARM_WIDE)) != 0) {
4197				mbs.param[2] |= DPARM_PARITY;
4198			}
4199
4200			if ((mbs.param[2] & DPARM_SYNC) == 0) {
4201				mbs.param[3] = 0;
4202			} else {
4203				mbs.param[3] =
4204				    (sdp->isp_devparam[tgt].sync_offset << 8) |
4205				    (sdp->isp_devparam[tgt].sync_period);
4206			}
4207			/*
4208			 * A command completion later that has
4209			 * RQSTF_NEGOTIATION set canl cause
4210			 * the dev_refresh/announce cycle also.
4211			 &
4212			 *
4213			 * Note: It is really important to update our current
4214			 * flags with at least the state of TAG capabilities-
4215			 * otherwise we might try and send a tagged command
4216			 * when we have it all turned off. So change it here
4217			 * to say that current already matches goal.
4218			 */
4219			sdp->isp_devparam[tgt].cur_dflags &= ~DPARM_TQING;
4220			sdp->isp_devparam[tgt].cur_dflags |=
4221			    (sdp->isp_devparam[tgt].dev_flags & DPARM_TQING);
4222			isp_prt(isp, ISP_LOGDEBUG2,
4223			    "bus %d set tgt %d flags 0x%x off 0x%x period 0x%x",
4224			    bus, tgt, mbs.param[2], mbs.param[3] >> 8,
4225			    mbs.param[3] & 0xff);
4226			sdp->isp_devparam[tgt].dev_update = 0;
4227			sdp->isp_devparam[tgt].dev_refresh = 1;
4228			get = 0;
4229		} else {
4230			continue;
4231		}
4232		mbs.param[1] = (bus << 15) | (tgt << 8) ;
4233		isp_mboxcmd(isp, &mbs, MBLOGALL);
4234		if (get == 0) {
4235			isp->isp_sendmarker |= (1 << bus);
4236			continue;
4237		}
4238		flags = mbs.param[2];
4239		period = mbs.param[3] & 0xff;
4240		offset = mbs.param[3] >> 8;
4241		sdp->isp_devparam[tgt].cur_dflags = flags;
4242		sdp->isp_devparam[tgt].cur_period = period;
4243		sdp->isp_devparam[tgt].cur_offset = offset;
4244		get = (bus << 16) | tgt;
4245		(void) isp_async(isp, ISPASYNC_NEW_TGT_PARAMS, &get);
4246	}
4247
4248	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
4249		if (sdp->isp_devparam[tgt].dev_update ||
4250		    sdp->isp_devparam[tgt].dev_refresh) {
4251			isp->isp_update |= (1 << bus);
4252			break;
4253		}
4254	}
4255}
4256
4257static void
4258isp_setdfltparm(isp, channel)
4259	struct ispsoftc *isp;
4260	int channel;
4261{
4262	int tgt;
4263	mbreg_t mbs;
4264	sdparam *sdp;
4265
4266	if (IS_FC(isp)) {
4267		fcparam *fcp = (fcparam *) isp->isp_param;
4268		fcp += channel;
4269		if (fcp->isp_gotdparms) {
4270			return;
4271		}
4272		fcp->isp_gotdparms = 1;
4273		fcp->isp_maxfrmlen = ICB_DFLT_FRMLEN;
4274		fcp->isp_maxalloc = ICB_DFLT_ALLOC;
4275		fcp->isp_execthrottle = ISP_EXEC_THROTTLE;
4276		fcp->isp_retry_delay = ICB_DFLT_RDELAY;
4277		fcp->isp_retry_count = ICB_DFLT_RCOUNT;
4278		/* Platform specific.... */
4279		fcp->isp_loopid = DEFAULT_LOOPID(isp);
4280		fcp->isp_nodewwn = DEFAULT_NODEWWN(isp);
4281		fcp->isp_portwwn = DEFAULT_PORTWWN(isp);
4282		fcp->isp_fwoptions = 0;
4283		fcp->isp_fwoptions |= ICBOPT_FAIRNESS;
4284		fcp->isp_fwoptions |= ICBOPT_PDBCHANGE_AE;
4285		fcp->isp_fwoptions |= ICBOPT_HARD_ADDRESS;
4286#ifndef	ISP_NO_FASTPOST_FC
4287		fcp->isp_fwoptions |= ICBOPT_FAST_POST;
4288#endif
4289		if (isp->isp_confopts & ISP_CFG_FULL_DUPLEX)
4290			fcp->isp_fwoptions |= ICBOPT_FULL_DUPLEX;
4291
4292		/*
4293		 * Make sure this is turned off now until we get
4294		 * extended options from NVRAM
4295		 */
4296		fcp->isp_fwoptions &= ~ICBOPT_EXTENDED;
4297
4298		/*
4299		 * Now try and read NVRAM
4300		 */
4301		if ((isp->isp_confopts & (ISP_CFG_NONVRAM|ISP_CFG_OWNWWN)) ||
4302		    (isp_read_nvram(isp))) {
4303			isp_prt(isp, ISP_LOGINFO,
4304			    "Node WWN 0x%08x%08x, Port WWN 0x%08x%08x",
4305			    (u_int32_t) (fcp->isp_nodewwn >> 32),
4306			    (u_int32_t) (fcp->isp_nodewwn & 0xffffffff),
4307			    (u_int32_t) (fcp->isp_portwwn >> 32),
4308			    (u_int32_t) (fcp->isp_portwwn & 0xffffffff));
4309		}
4310		fcp->isp_nodewwn = ISP_NODEWWN(isp);
4311		fcp->isp_portwwn = ISP_PORTWWN(isp);
4312		return;
4313	}
4314
4315	sdp = (sdparam *) isp->isp_param;
4316	sdp += channel;
4317
4318	/*
4319	 * Been there, done that, got the T-shirt...
4320	 */
4321	if (sdp->isp_gotdparms) {
4322		return;
4323	}
4324	sdp->isp_gotdparms = 1;
4325
4326	/*
4327	 * If we've not been told to avoid reading NVRAM, try and read it.
4328	 * If we're successful reading it, we can return since NVRAM will
4329	 * tell us the right thing to do. Otherwise, establish some reasonable
4330	 * defaults.
4331	 */
4332	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
4333		if (isp_read_nvram(isp) == 0) {
4334			return;
4335		}
4336	}
4337
4338	/*
4339	 * Now try and see whether we have specific values for them.
4340	 */
4341	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
4342		mbs.param[0] = MBOX_GET_ACT_NEG_STATE;
4343		isp_mboxcmd(isp, &mbs, MBLOGALL);
4344		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
4345			sdp->isp_req_ack_active_neg = 1;
4346			sdp->isp_data_line_active_neg = 1;
4347		} else {
4348			sdp->isp_req_ack_active_neg =
4349			    (mbs.param[1+channel] >> 4) & 0x1;
4350			sdp->isp_data_line_active_neg =
4351			    (mbs.param[1+channel] >> 5) & 0x1;
4352		}
4353	} else {
4354		sdp->isp_req_ack_active_neg = 1;
4355		sdp->isp_data_line_active_neg = 1;
4356	}
4357
4358	isp_prt(isp, ISP_LOGDEBUG1,
4359	    "defaulting bus %d REQ/ACK Active Negation is %d",
4360	    channel, sdp->isp_req_ack_active_neg);
4361	isp_prt(isp, ISP_LOGDEBUG1,
4362	    "defaulting bus %d DATA Active Negation is %d",
4363	    channel, sdp->isp_data_line_active_neg);
4364
4365	/*
4366	 * The trick here is to establish a default for the default (honk!)
4367	 * state (dev_flags). Then try and get the current status from
4368	 * the card to fill in the current state. We don't, in fact, set
4369	 * the default to the SAFE default state- that's not the goal state.
4370	 */
4371	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
4372		sdp->isp_devparam[tgt].cur_offset = 0;
4373		sdp->isp_devparam[tgt].cur_period = 0;
4374		sdp->isp_devparam[tgt].dev_flags = DPARM_DEFAULT;
4375		sdp->isp_devparam[tgt].cur_dflags = 0;
4376		/*
4377		 * We default to Wide/Fast for versions less than a 1040
4378		 * (unless it's SBus).
4379		 */
4380		if ((isp->isp_bustype == ISP_BT_SBUS &&
4381		    isp->isp_type < ISP_HA_SCSI_1020A) ||
4382		    (isp->isp_bustype == ISP_BT_PCI &&
4383		    isp->isp_type < ISP_HA_SCSI_1040) ||
4384		    (isp->isp_clock && isp->isp_clock < 60) ||
4385		    (sdp->isp_ultramode == 0)) {
4386			sdp->isp_devparam[tgt].sync_offset =
4387			    ISP_10M_SYNCPARMS >> 8;
4388			sdp->isp_devparam[tgt].sync_period =
4389			    ISP_10M_SYNCPARMS & 0xff;
4390		} else if (IS_ULTRA3(isp)) {
4391			sdp->isp_devparam[tgt].sync_offset =
4392			    ISP_80M_SYNCPARMS >> 8;
4393			sdp->isp_devparam[tgt].sync_period =
4394			    ISP_80M_SYNCPARMS & 0xff;
4395		} else if (IS_ULTRA2(isp)) {
4396			sdp->isp_devparam[tgt].sync_offset =
4397			    ISP_40M_SYNCPARMS >> 8;
4398			sdp->isp_devparam[tgt].sync_period =
4399			    ISP_40M_SYNCPARMS & 0xff;
4400		} else if (IS_1240(isp)) {
4401			sdp->isp_devparam[tgt].sync_offset =
4402			    ISP_20M_SYNCPARMS >> 8;
4403			sdp->isp_devparam[tgt].sync_period =
4404			    ISP_20M_SYNCPARMS & 0xff;
4405		} else {
4406			sdp->isp_devparam[tgt].sync_offset =
4407			    ISP_20M_SYNCPARMS_1040 >> 8;
4408			sdp->isp_devparam[tgt].sync_period =
4409			    ISP_20M_SYNCPARMS_1040 & 0xff;
4410		}
4411
4412		/*
4413		 * Don't get current target parameters if we've been
4414		 * told not to use NVRAM- it's really the same thing.
4415		 */
4416		if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
4417
4418			mbs.param[0] = MBOX_GET_TARGET_PARAMS;
4419			mbs.param[1] = tgt << 8;
4420			isp_mboxcmd(isp, &mbs, MBLOGALL);
4421			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
4422				continue;
4423			}
4424			sdp->isp_devparam[tgt].cur_dflags = mbs.param[2];
4425			sdp->isp_devparam[tgt].dev_flags = mbs.param[2];
4426			sdp->isp_devparam[tgt].cur_period = mbs.param[3] & 0xff;
4427			sdp->isp_devparam[tgt].cur_offset = mbs.param[3] >> 8;
4428
4429			/*
4430			 * The maximum period we can really see
4431			 * here is 100 (decimal), or 400 ns.
4432			 * For some unknown reason we sometimes
4433			 * get back wildass numbers from the
4434			 * boot device's parameters (alpha only).
4435			 */
4436			if ((mbs.param[3] & 0xff) <= 0x64) {
4437				sdp->isp_devparam[tgt].sync_period =
4438				    mbs.param[3] & 0xff;
4439				sdp->isp_devparam[tgt].sync_offset =
4440				    mbs.param[3] >> 8;
4441			}
4442
4443			/*
4444			 * It is not safe to run Ultra Mode with a clock < 60.
4445			 */
4446			if (((isp->isp_clock && isp->isp_clock < 60) ||
4447			    (isp->isp_type < ISP_HA_SCSI_1020A)) &&
4448			    (sdp->isp_devparam[tgt].sync_period <=
4449			    (ISP_20M_SYNCPARMS & 0xff))) {
4450				sdp->isp_devparam[tgt].sync_offset =
4451				    ISP_10M_SYNCPARMS >> 8;
4452				sdp->isp_devparam[tgt].sync_period =
4453				    ISP_10M_SYNCPARMS & 0xff;
4454			}
4455		}
4456		isp_prt(isp, ISP_LOGDEBUG1,
4457		    "Initial bus %d tgt %d flags %x offset %x period %x",
4458		    channel, tgt, sdp->isp_devparam[tgt].dev_flags,
4459		    sdp->isp_devparam[tgt].sync_offset,
4460		    sdp->isp_devparam[tgt].sync_period);
4461	}
4462
4463	/*
4464	 * Establish default some more default parameters.
4465	 */
4466	sdp->isp_cmd_dma_burst_enable = 1;
4467	sdp->isp_data_dma_burst_enabl = 1;
4468	sdp->isp_fifo_threshold = 0;
4469	sdp->isp_initiator_id = DEFAULT_IID(isp);
4470	if (isp->isp_type >= ISP_HA_SCSI_1040) {
4471		sdp->isp_async_data_setup = 9;
4472	} else {
4473		sdp->isp_async_data_setup = 6;
4474	}
4475	sdp->isp_selection_timeout = 250;
4476	sdp->isp_max_queue_depth = MAXISPREQUEST(isp);
4477	sdp->isp_tag_aging = 8;
4478	sdp->isp_bus_reset_delay = 3;
4479	sdp->isp_retry_count = 2;
4480	sdp->isp_retry_delay = 2;
4481
4482	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
4483		sdp->isp_devparam[tgt].exc_throttle = ISP_EXEC_THROTTLE;
4484		sdp->isp_devparam[tgt].dev_enable = 1;
4485	}
4486}
4487
4488/*
4489 * Re-initialize the ISP and complete all orphaned commands
4490 * with a 'botched' notice. The reset/init routines should
4491 * not disturb an already active list of commands.
4492 *
4493 * Locks held prior to coming here.
4494 */
4495
4496void
4497isp_reinit(isp)
4498	struct ispsoftc *isp;
4499{
4500	XS_T *xs;
4501	u_int32_t handle;
4502
4503	isp_reset(isp);
4504	if (isp->isp_state == ISP_RESETSTATE) {
4505		isp_init(isp);
4506		if (isp->isp_state == ISP_INITSTATE) {
4507			isp->isp_state = ISP_RUNSTATE;
4508		}
4509	}
4510	if (isp->isp_state != ISP_RUNSTATE) {
4511		isp_prt(isp, ISP_LOGERR, "isp_reinit cannot restart ISP");
4512	}
4513	isp->isp_nactive = 0;
4514
4515	for (handle = 1; (int) handle <= isp->isp_maxcmds; handle++) {
4516		xs = isp_find_xs(isp, handle);
4517		if (xs == NULL) {
4518			continue;
4519		}
4520		isp_destroy_handle(isp, handle);
4521		if (XS_XFRLEN(xs)) {
4522			ISP_DMAFREE(isp, xs, handle);
4523			XS_RESID(xs) = XS_XFRLEN(xs);
4524		} else {
4525			XS_RESID(xs) = 0;
4526		}
4527		XS_SETERR(xs, HBA_BUSRESET);
4528		isp_done(xs);
4529	}
4530}
4531
4532/*
4533 * NVRAM Routines
4534 */
4535static int
4536isp_read_nvram(isp)
4537	struct ispsoftc *isp;
4538{
4539	int i, amt;
4540	u_int8_t csum, minversion;
4541	union {
4542		u_int8_t _x[ISP2100_NVRAM_SIZE];
4543		u_int16_t _s[ISP2100_NVRAM_SIZE>>1];
4544	} _n;
4545#define	nvram_data	_n._x
4546#define	nvram_words	_n._s
4547
4548	if (IS_FC(isp)) {
4549		amt = ISP2100_NVRAM_SIZE;
4550		minversion = 1;
4551	} else if (IS_ULTRA2(isp)) {
4552		amt = ISP1080_NVRAM_SIZE;
4553		minversion = 0;
4554	} else {
4555		amt = ISP_NVRAM_SIZE;
4556		minversion = 2;
4557	}
4558
4559	/*
4560	 * Just read the first two words first to see if we have a valid
4561	 * NVRAM to continue reading the rest with.
4562	 */
4563	for (i = 0; i < 2; i++) {
4564		isp_rdnvram_word(isp, i, &nvram_words[i]);
4565	}
4566	if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
4567	    nvram_data[2] != 'P') {
4568		if (isp->isp_bustype != ISP_BT_SBUS) {
4569			isp_prt(isp, ISP_LOGWARN, "invalid NVRAM header");
4570			isp_prt(isp, ISP_LOGDEBUG0, "%x %x %x",
4571			    nvram_data[0], nvram_data[1], nvram_data[2]);
4572		}
4573		return (-1);
4574	}
4575	for (i = 2; i < amt>>1; i++) {
4576		isp_rdnvram_word(isp, i, &nvram_words[i]);
4577	}
4578	for (csum = 0, i = 0; i < amt; i++) {
4579		csum += nvram_data[i];
4580	}
4581	if (csum != 0) {
4582		isp_prt(isp, ISP_LOGWARN, "invalid NVRAM checksum");
4583		return (-1);
4584	}
4585	if (ISP_NVRAM_VERSION(nvram_data) < minversion) {
4586		isp_prt(isp, ISP_LOGWARN, "version %d NVRAM not understood",
4587		    ISP_NVRAM_VERSION(nvram_data));
4588		return (-1);
4589	}
4590
4591	if (IS_ULTRA3(isp)) {
4592		isp_parse_nvram_12160(isp, 0, nvram_data);
4593		isp_parse_nvram_12160(isp, 1, nvram_data);
4594	} else if (IS_1080(isp)) {
4595		isp_parse_nvram_1080(isp, 0, nvram_data);
4596	} else if (IS_1280(isp) || IS_1240(isp)) {
4597		isp_parse_nvram_1080(isp, 0, nvram_data);
4598		isp_parse_nvram_1080(isp, 1, nvram_data);
4599	} else if (IS_SCSI(isp)) {
4600		isp_parse_nvram_1020(isp, nvram_data);
4601	} else {
4602		isp_parse_nvram_2100(isp, nvram_data);
4603	}
4604	return (0);
4605#undef	nvram_data
4606#undef	nvram_words
4607}
4608
4609static void
4610isp_rdnvram_word(isp, wo, rp)
4611	struct ispsoftc *isp;
4612	int wo;
4613	u_int16_t *rp;
4614{
4615	int i, cbits;
4616	u_int16_t bit, rqst;
4617
4618	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
4619	USEC_DELAY(2);
4620	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
4621	USEC_DELAY(2);
4622
4623	if (IS_FC(isp)) {
4624		wo &= ((ISP2100_NVRAM_SIZE >> 1) - 1);
4625		rqst = (ISP_NVRAM_READ << 8) | wo;
4626		cbits = 10;
4627	} else if (IS_ULTRA2(isp)) {
4628		wo &= ((ISP1080_NVRAM_SIZE >> 1) - 1);
4629		rqst = (ISP_NVRAM_READ << 8) | wo;
4630		cbits = 10;
4631	} else {
4632		wo &= ((ISP_NVRAM_SIZE >> 1) - 1);
4633		rqst = (ISP_NVRAM_READ << 6) | wo;
4634		cbits = 8;
4635	}
4636
4637	/*
4638	 * Clock the word select request out...
4639	 */
4640	for (i = cbits; i >= 0; i--) {
4641		if ((rqst >> i) & 1) {
4642			bit = BIU_NVRAM_SELECT | BIU_NVRAM_DATAOUT;
4643		} else {
4644			bit = BIU_NVRAM_SELECT;
4645		}
4646		ISP_WRITE(isp, BIU_NVRAM, bit);
4647		USEC_DELAY(2);
4648		ISP_WRITE(isp, BIU_NVRAM, bit | BIU_NVRAM_CLOCK);
4649		USEC_DELAY(2);
4650		ISP_WRITE(isp, BIU_NVRAM, bit);
4651		USEC_DELAY(2);
4652	}
4653	/*
4654	 * Now read the result back in (bits come back in MSB format).
4655	 */
4656	*rp = 0;
4657	for (i = 0; i < 16; i++) {
4658		u_int16_t rv;
4659		*rp <<= 1;
4660		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
4661		USEC_DELAY(2);
4662		rv = ISP_READ(isp, BIU_NVRAM);
4663		if (rv & BIU_NVRAM_DATAIN) {
4664			*rp |= 1;
4665		}
4666		USEC_DELAY(2);
4667		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
4668		USEC_DELAY(2);
4669	}
4670	ISP_WRITE(isp, BIU_NVRAM, 0);
4671	USEC_DELAY(2);
4672	ISP_SWIZZLE_NVRAM_WORD(isp, rp);
4673}
4674
4675static void
4676isp_parse_nvram_1020(isp, nvram_data)
4677	struct ispsoftc *isp;
4678	u_int8_t *nvram_data;
4679{
4680	int i;
4681	sdparam *sdp = (sdparam *) isp->isp_param;
4682
4683	sdp->isp_fifo_threshold =
4684		ISP_NVRAM_FIFO_THRESHOLD(nvram_data) |
4685		(ISP_NVRAM_FIFO_THRESHOLD_128(nvram_data) << 2);
4686
4687	sdp->isp_initiator_id =
4688		ISP_NVRAM_INITIATOR_ID(nvram_data);
4689
4690	sdp->isp_bus_reset_delay =
4691		ISP_NVRAM_BUS_RESET_DELAY(nvram_data);
4692
4693	sdp->isp_retry_count =
4694		ISP_NVRAM_BUS_RETRY_COUNT(nvram_data);
4695
4696	sdp->isp_retry_delay =
4697		ISP_NVRAM_BUS_RETRY_DELAY(nvram_data);
4698
4699	sdp->isp_async_data_setup =
4700		ISP_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data);
4701
4702	if (isp->isp_type >= ISP_HA_SCSI_1040) {
4703		if (sdp->isp_async_data_setup < 9) {
4704			sdp->isp_async_data_setup = 9;
4705		}
4706	} else {
4707		if (sdp->isp_async_data_setup != 6) {
4708			sdp->isp_async_data_setup = 6;
4709		}
4710	}
4711
4712	sdp->isp_req_ack_active_neg =
4713		ISP_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data);
4714
4715	sdp->isp_data_line_active_neg =
4716		ISP_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data);
4717
4718	sdp->isp_data_dma_burst_enabl =
4719		ISP_NVRAM_DATA_DMA_BURST_ENABLE(nvram_data);
4720
4721	sdp->isp_cmd_dma_burst_enable =
4722		ISP_NVRAM_CMD_DMA_BURST_ENABLE(nvram_data);
4723
4724	sdp->isp_tag_aging =
4725		ISP_NVRAM_TAG_AGE_LIMIT(nvram_data);
4726
4727	sdp->isp_selection_timeout =
4728		ISP_NVRAM_SELECTION_TIMEOUT(nvram_data);
4729
4730	sdp->isp_max_queue_depth =
4731		ISP_NVRAM_MAX_QUEUE_DEPTH(nvram_data);
4732
4733	sdp->isp_fast_mttr = ISP_NVRAM_FAST_MTTR_ENABLE(nvram_data);
4734	for (i = 0; i < MAX_TARGETS; i++) {
4735		sdp->isp_devparam[i].dev_enable =
4736			ISP_NVRAM_TGT_DEVICE_ENABLE(nvram_data, i);
4737		sdp->isp_devparam[i].exc_throttle =
4738			ISP_NVRAM_TGT_EXEC_THROTTLE(nvram_data, i);
4739		sdp->isp_devparam[i].sync_offset =
4740			ISP_NVRAM_TGT_SYNC_OFFSET(nvram_data, i);
4741		sdp->isp_devparam[i].sync_period =
4742			ISP_NVRAM_TGT_SYNC_PERIOD(nvram_data, i);
4743
4744		if (isp->isp_type < ISP_HA_SCSI_1040) {
4745			/*
4746			 * If we're not ultra, we can't possibly
4747			 * be a shorter period than this.
4748			 */
4749			if (sdp->isp_devparam[i].sync_period < 0x19) {
4750				sdp->isp_devparam[i].sync_period = 0x19;
4751			}
4752			if (sdp->isp_devparam[i].sync_offset > 0xc) {
4753				sdp->isp_devparam[i].sync_offset = 0x0c;
4754			}
4755		} else {
4756			if (sdp->isp_devparam[i].sync_offset > 0x8) {
4757				sdp->isp_devparam[i].sync_offset = 0x8;
4758			}
4759		}
4760		sdp->isp_devparam[i].dev_flags = 0;
4761		if (ISP_NVRAM_TGT_RENEG(nvram_data, i))
4762			sdp->isp_devparam[i].dev_flags |= DPARM_RENEG;
4763		sdp->isp_devparam[i].dev_flags |= DPARM_ARQ;
4764		if (ISP_NVRAM_TGT_TQING(nvram_data, i))
4765			sdp->isp_devparam[i].dev_flags |= DPARM_TQING;
4766		if (ISP_NVRAM_TGT_SYNC(nvram_data, i))
4767			sdp->isp_devparam[i].dev_flags |= DPARM_SYNC;
4768		if (ISP_NVRAM_TGT_WIDE(nvram_data, i))
4769			sdp->isp_devparam[i].dev_flags |= DPARM_WIDE;
4770		if (ISP_NVRAM_TGT_PARITY(nvram_data, i))
4771			sdp->isp_devparam[i].dev_flags |= DPARM_PARITY;
4772		if (ISP_NVRAM_TGT_DISC(nvram_data, i))
4773			sdp->isp_devparam[i].dev_flags |= DPARM_DISC;
4774		sdp->isp_devparam[i].cur_dflags = 0; /* we don't know */
4775	}
4776}
4777
4778static void
4779isp_parse_nvram_1080(isp, bus, nvram_data)
4780	struct ispsoftc *isp;
4781	int bus;
4782	u_int8_t *nvram_data;
4783{
4784	int i;
4785	sdparam *sdp = (sdparam *) isp->isp_param;
4786	sdp += bus;
4787
4788	sdp->isp_fifo_threshold =
4789	    ISP1080_NVRAM_FIFO_THRESHOLD(nvram_data);
4790
4791	sdp->isp_initiator_id =
4792	    ISP1080_NVRAM_INITIATOR_ID(nvram_data, bus);
4793
4794	sdp->isp_bus_reset_delay =
4795	    ISP1080_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
4796
4797	sdp->isp_retry_count =
4798	    ISP1080_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
4799
4800	sdp->isp_retry_delay =
4801	    ISP1080_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
4802
4803	sdp->isp_async_data_setup =
4804	    ISP1080_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data,
4805	    bus);
4806
4807	sdp->isp_req_ack_active_neg =
4808	    ISP1080_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data,
4809	    bus);
4810
4811	sdp->isp_data_line_active_neg =
4812	    ISP1080_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data,
4813	    bus);
4814
4815	sdp->isp_data_dma_burst_enabl =
4816	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
4817
4818	sdp->isp_cmd_dma_burst_enable =
4819	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
4820
4821	sdp->isp_selection_timeout =
4822	    ISP1080_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
4823
4824	sdp->isp_max_queue_depth =
4825	     ISP1080_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
4826
4827	for (i = 0; i < MAX_TARGETS; i++) {
4828		sdp->isp_devparam[i].dev_enable =
4829		    ISP1080_NVRAM_TGT_DEVICE_ENABLE(nvram_data, i, bus);
4830		sdp->isp_devparam[i].exc_throttle =
4831			ISP1080_NVRAM_TGT_EXEC_THROTTLE(nvram_data, i, bus);
4832		sdp->isp_devparam[i].sync_offset =
4833			ISP1080_NVRAM_TGT_SYNC_OFFSET(nvram_data, i, bus);
4834		sdp->isp_devparam[i].sync_period =
4835			ISP1080_NVRAM_TGT_SYNC_PERIOD(nvram_data, i, bus);
4836		sdp->isp_devparam[i].dev_flags = 0;
4837		if (ISP1080_NVRAM_TGT_RENEG(nvram_data, i, bus))
4838			sdp->isp_devparam[i].dev_flags |= DPARM_RENEG;
4839		sdp->isp_devparam[i].dev_flags |= DPARM_ARQ;
4840		if (ISP1080_NVRAM_TGT_TQING(nvram_data, i, bus))
4841			sdp->isp_devparam[i].dev_flags |= DPARM_TQING;
4842		if (ISP1080_NVRAM_TGT_SYNC(nvram_data, i, bus))
4843			sdp->isp_devparam[i].dev_flags |= DPARM_SYNC;
4844		if (ISP1080_NVRAM_TGT_WIDE(nvram_data, i, bus))
4845			sdp->isp_devparam[i].dev_flags |= DPARM_WIDE;
4846		if (ISP1080_NVRAM_TGT_PARITY(nvram_data, i, bus))
4847			sdp->isp_devparam[i].dev_flags |= DPARM_PARITY;
4848		if (ISP1080_NVRAM_TGT_DISC(nvram_data, i, bus))
4849			sdp->isp_devparam[i].dev_flags |= DPARM_DISC;
4850		sdp->isp_devparam[i].cur_dflags = 0;
4851	}
4852}
4853
4854static void
4855isp_parse_nvram_12160(isp, bus, nvram_data)
4856	struct ispsoftc *isp;
4857	int bus;
4858	u_int8_t *nvram_data;
4859{
4860	sdparam *sdp = (sdparam *) isp->isp_param;
4861	int i;
4862
4863	sdp += bus;
4864
4865	sdp->isp_fifo_threshold =
4866	    ISP12160_NVRAM_FIFO_THRESHOLD(nvram_data);
4867
4868	sdp->isp_initiator_id =
4869	    ISP12160_NVRAM_INITIATOR_ID(nvram_data, bus);
4870
4871	sdp->isp_bus_reset_delay =
4872	    ISP12160_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
4873
4874	sdp->isp_retry_count =
4875	    ISP12160_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
4876
4877	sdp->isp_retry_delay =
4878	    ISP12160_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
4879
4880	sdp->isp_async_data_setup =
4881	    ISP12160_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data,
4882	    bus);
4883
4884	sdp->isp_req_ack_active_neg =
4885	    ISP12160_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data,
4886	    bus);
4887
4888	sdp->isp_data_line_active_neg =
4889	    ISP12160_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data,
4890	    bus);
4891
4892	sdp->isp_data_dma_burst_enabl =
4893	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
4894
4895	sdp->isp_cmd_dma_burst_enable =
4896	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
4897
4898	sdp->isp_selection_timeout =
4899	    ISP12160_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
4900
4901	sdp->isp_max_queue_depth =
4902	     ISP12160_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
4903
4904	for (i = 0; i < MAX_TARGETS; i++) {
4905		sdp->isp_devparam[i].dev_enable =
4906		    ISP12160_NVRAM_TGT_DEVICE_ENABLE(nvram_data, i, bus);
4907		sdp->isp_devparam[i].exc_throttle =
4908			ISP12160_NVRAM_TGT_EXEC_THROTTLE(nvram_data, i, bus);
4909		sdp->isp_devparam[i].sync_offset =
4910			ISP12160_NVRAM_TGT_SYNC_OFFSET(nvram_data, i, bus);
4911		sdp->isp_devparam[i].sync_period =
4912			ISP12160_NVRAM_TGT_SYNC_PERIOD(nvram_data, i, bus);
4913		sdp->isp_devparam[i].dev_flags = 0;
4914		if (ISP12160_NVRAM_TGT_RENEG(nvram_data, i, bus))
4915			sdp->isp_devparam[i].dev_flags |= DPARM_RENEG;
4916		sdp->isp_devparam[i].dev_flags |= DPARM_ARQ;
4917		if (ISP12160_NVRAM_TGT_TQING(nvram_data, i, bus))
4918			sdp->isp_devparam[i].dev_flags |= DPARM_TQING;
4919		if (ISP12160_NVRAM_TGT_SYNC(nvram_data, i, bus))
4920			sdp->isp_devparam[i].dev_flags |= DPARM_SYNC;
4921		if (ISP12160_NVRAM_TGT_WIDE(nvram_data, i, bus))
4922			sdp->isp_devparam[i].dev_flags |= DPARM_WIDE;
4923		if (ISP12160_NVRAM_TGT_PARITY(nvram_data, i, bus))
4924			sdp->isp_devparam[i].dev_flags |= DPARM_PARITY;
4925		if (ISP12160_NVRAM_TGT_DISC(nvram_data, i, bus))
4926			sdp->isp_devparam[i].dev_flags |= DPARM_DISC;
4927		sdp->isp_devparam[i].cur_dflags = 0;
4928	}
4929}
4930
4931static void
4932isp_parse_nvram_2100(isp, nvram_data)
4933	struct ispsoftc *isp;
4934	u_int8_t *nvram_data;
4935{
4936	fcparam *fcp = (fcparam *) isp->isp_param;
4937	u_int64_t wwn;
4938
4939	/*
4940	 * There is NVRAM storage for both Port and Node entities-
4941	 * but the Node entity appears to be unused on all the cards
4942	 * I can find. However, we should account for this being set
4943	 * at some point in the future.
4944	 *
4945	 * Qlogic WWNs have an NAA of 2, but usually nothing shows up in
4946	 * bits 48..60. In the case of the 2202, it appears that they do
4947	 * use bit 48 to distinguish between the two instances on the card.
4948	 * The 2204, which I've never seen, *probably* extends this method.
4949	 */
4950	wwn = ISP2100_NVRAM_PORT_NAME(nvram_data);
4951	if (wwn) {
4952		isp_prt(isp, ISP_LOGCONFIG, "NVRAM Port WWN 0x%08x%08x",
4953		    (u_int32_t) (wwn >> 32), (u_int32_t) (wwn & 0xffffffff));
4954		if ((wwn >> 60) == 0) {
4955			wwn |= (((u_int64_t) 2)<< 60);
4956		}
4957	}
4958	fcp->isp_portwwn = wwn;
4959	wwn = ISP2100_NVRAM_NODE_NAME(nvram_data);
4960	if (wwn) {
4961		isp_prt(isp, ISP_LOGCONFIG, "NVRAM Node WWN 0x%08x%08x",
4962		    (u_int32_t) (wwn >> 32), (u_int32_t) (wwn & 0xffffffff));
4963		if ((wwn >> 60) == 0) {
4964			wwn |= (((u_int64_t) 2)<< 60);
4965		}
4966	}
4967	fcp->isp_nodewwn = wwn;
4968
4969	/*
4970	 * Make sure we have both Node and Port as non-zero values.
4971	 */
4972	if (fcp->isp_nodewwn != 0 && fcp->isp_portwwn == 0) {
4973		fcp->isp_portwwn = fcp->isp_nodewwn;
4974	} else if (fcp->isp_nodewwn == 0 && fcp->isp_portwwn != 0) {
4975		fcp->isp_nodewwn = fcp->isp_portwwn;
4976	}
4977
4978	/*
4979	 * Make the Node and Port values sane if they're NAA == 2.
4980	 * This means to clear bits 48..56 for the Node WWN and
4981	 * make sure that there's some non-zero value in 48..56
4982	 * for the Port WWN.
4983	 */
4984	if (fcp->isp_nodewwn && fcp->isp_portwwn) {
4985		if ((fcp->isp_nodewwn & (((u_int64_t) 0xfff) << 48)) != 0 &&
4986		    (fcp->isp_nodewwn >> 60) == 2) {
4987			fcp->isp_nodewwn &= ~((u_int64_t) 0xfff << 48);
4988		}
4989		if ((fcp->isp_portwwn & (((u_int64_t) 0xfff) << 48)) == 0 &&
4990		    (fcp->isp_portwwn >> 60) == 2) {
4991			fcp->isp_portwwn |= ((u_int64_t) 1 << 56);
4992		}
4993	}
4994
4995	fcp->isp_maxalloc =
4996		ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data);
4997	fcp->isp_maxfrmlen =
4998		ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data);
4999	fcp->isp_retry_delay =
5000		ISP2100_NVRAM_RETRY_DELAY(nvram_data);
5001	fcp->isp_retry_count =
5002		ISP2100_NVRAM_RETRY_COUNT(nvram_data);
5003	fcp->isp_loopid =
5004		ISP2100_NVRAM_HARDLOOPID(nvram_data);
5005	fcp->isp_execthrottle =
5006		ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data);
5007	fcp->isp_fwoptions = ISP2100_NVRAM_OPTIONS(nvram_data);
5008	isp_prt(isp, ISP_LOGDEBUG0,
5009	    "fwoptions from nvram are 0x%x", fcp->isp_fwoptions);
5010}
5011