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