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