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