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