isp.c revision 157945
1/*-
2 * Machine and OS Independent (well, as best as possible)
3 * code for the Qlogic ISP SCSI adapters.
4 *
5 * Copyright (c) 1997-2006 by Matthew Jacob
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice immediately at the beginning of the file, without modification,
13 *    this list of conditions, and the following disclaimer.
14 * 2. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30/*
31 * Inspiration and ideas about this driver are from Erik Moe's Linux driver
32 * (qlogicisp.c) and Dave Miller's SBus version of same (qlogicisp.c). Some
33 * ideas dredged from the Solaris driver.
34 */
35#ifdef	__FreeBSD__
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD: head/sys/dev/isp/isp.c 157945 2006-04-21 18:46:35Z mjacob $");
38#endif
39
40/*
41 * Include header file appropriate for platform we're building on.
42 */
43#ifdef	__NetBSD__
44#include <dev/ic/isp_netbsd.h>
45#endif
46#ifdef	__FreeBSD__
47#include <dev/isp/isp_freebsd.h>
48#endif
49#ifdef	__OpenBSD__
50#include <dev/ic/isp_openbsd.h>
51#endif
52#ifdef	__linux__
53#include "isp_linux.h"
54#endif
55#ifdef	__svr4__
56#include "isp_solaris.h"
57#endif
58
59/*
60 * General defines
61 */
62
63#define	MBOX_DELAY_COUNT	1000000 / 100
64
65/*
66 * Local static data
67 */
68static const char portshift[] =
69    "Target %d Loop ID 0x%x (Port 0x%x) => Loop 0x%x (Port 0x%x)";
70static const char portdup[] =
71    "Target %d duplicates Target %d- killing off both";
72static const char retained[] =
73    "Retaining Loop ID 0x%x for Target %d (Port 0x%x)";
74static const char lretained[] =
75    "Retained login of Target %d (Loop ID 0x%x) Port 0x%x";
76static const char plogout[] =
77    "Logging out Target %d at Loop ID 0x%x (Port 0x%x)";
78static const char plogierr[] =
79    "Command Error in PLOGI for Port 0x%x (0x%x)";
80static const char nopdb[] =
81    "Could not get PDB for Device @ Port 0x%x";
82static const char pdbmfail1[] =
83    "PDB Loop ID info for Device @ Port 0x%x does not match up (0x%x)";
84static const char pdbmfail2[] =
85    "PDB Port info for Device @ Port 0x%x does not match up (0x%x)";
86static const char ldumped[] =
87    "Target %d (Loop ID 0x%x) Port 0x%x dumped after login info mismatch";
88static const char notresp[] =
89  "Not RESPONSE in RESPONSE Queue (type 0x%x) @ idx %d (next %d) nlooked %d";
90static const char xact1[] =
91    "HBA attempted queued transaction with disconnect not set for %d.%d.%d";
92static const char xact2[] =
93    "HBA attempted queued transaction to target routine %d on target %d bus %d";
94static const char xact3[] =
95    "HBA attempted queued cmd for %d.%d.%d when queueing disabled";
96static const char pskip[] =
97    "SCSI phase skipped for target %d.%d.%d";
98static const char topology[] =
99    "Loop ID %d, Port ID 0x%x, Loop State 0x%x, Topology '%s'";
100static const char swrej[] =
101    "Fabric Nameserver rejected %s (Reason=0x%x Expl=0x%x) for Port ID 0x%x";
102static const char finmsg[] =
103    "(%d.%d.%d): FIN dl%d resid %d STS 0x%x SKEY %c XS_ERR=0x%x";
104static const char sc0[] =
105    "%s CHAN %d FTHRSH %d IID %d RESETD %d RETRYC %d RETRYD %d ASD 0x%x";
106static const char sc1[] =
107    "%s RAAN 0x%x DLAN 0x%x DDMAB 0x%x CDMAB 0x%x SELTIME %d MQD %d";
108static const char sc2[] = "%s CHAN %d TGT %d FLAGS 0x%x 0x%x/0x%x";
109static const char sc3[] = "Generated";
110static const char sc4[] = "NVRAM";
111static const char bun[] =
112    "bad underrun for %d.%d (count %d, resid %d, status %s)";
113
114/*
115 * Local function prototypes.
116 */
117static int isp_parse_async(ispsoftc_t *, uint16_t);
118static int isp_handle_other_response(ispsoftc_t *, int, isphdr_t *,
119    uint16_t *);
120static void
121isp_parse_status(ispsoftc_t *, ispstatusreq_t *, XS_T *);
122static void isp_fastpost_complete(ispsoftc_t *, uint16_t);
123static int isp_mbox_continue(ispsoftc_t *);
124static void isp_scsi_init(ispsoftc_t *);
125static void isp_scsi_channel_init(ispsoftc_t *, int);
126static void isp_fibre_init(ispsoftc_t *);
127static void isp_mark_getpdb_all(ispsoftc_t *);
128static int isp_getmap(ispsoftc_t *, fcpos_map_t *);
129static int isp_getpdb(ispsoftc_t *, int, isp_pdb_t *);
130static uint64_t isp_get_portname(ispsoftc_t *, int, int);
131static int isp_fclink_test(ispsoftc_t *, int);
132static char *isp2100_fw_statename(int);
133static int isp_pdb_sync(ispsoftc_t *);
134static int isp_scan_loop(ispsoftc_t *);
135static int isp_fabric_mbox_cmd(ispsoftc_t *, mbreg_t *);
136static int isp_scan_fabric(ispsoftc_t *, int);
137static void isp_register_fc4_type(ispsoftc_t *);
138static void isp_fw_state(ispsoftc_t *);
139static void isp_mboxcmd_qnw(ispsoftc_t *, mbreg_t *, int);
140static void isp_mboxcmd(ispsoftc_t *, mbreg_t *, int);
141
142static void isp_update(ispsoftc_t *);
143static void isp_update_bus(ispsoftc_t *, int);
144static void isp_setdfltparm(ispsoftc_t *, int);
145static int isp_read_nvram(ispsoftc_t *);
146static void isp_rdnvram_word(ispsoftc_t *, int, uint16_t *);
147static void isp_parse_nvram_1020(ispsoftc_t *, uint8_t *);
148static void isp_parse_nvram_1080(ispsoftc_t *, int, uint8_t *);
149static void isp_parse_nvram_12160(ispsoftc_t *, int, uint8_t *);
150static void isp_parse_nvram_2100(ispsoftc_t *, uint8_t *);
151
152/*
153 * Reset Hardware.
154 *
155 * Hit the chip over the head, download new f/w if available and set it running.
156 *
157 * Locking done elsewhere.
158 */
159
160void
161isp_reset(ispsoftc_t *isp)
162{
163	mbreg_t mbs;
164	uint16_t code_org;
165	int loops, i, dodnld = 1;
166	char *btype = "????";
167
168	isp->isp_state = ISP_NILSTATE;
169	MEMZERO(&mbs, sizeof (mbs));
170
171	/*
172	 * Basic types (SCSI, FibreChannel and PCI or SBus)
173	 * have been set in the MD code. We figure out more
174	 * here. Possibly more refined types based upon PCI
175	 * identification. Chip revision has been gathered.
176	 *
177	 * After we've fired this chip up, zero out the conf1 register
178	 * for SCSI adapters and do other settings for the 2100.
179	 */
180
181	/*
182	 * Get the current running firmware revision out of the
183	 * chip before we hit it over the head (if this is our
184	 * first time through). Note that we store this as the
185	 * 'ROM' firmware revision- which it may not be. In any
186	 * case, we don't really use this yet, but we may in
187	 * the future.
188	 */
189	if (isp->isp_touched == 0) {
190		/*
191		 * First see whether or not we're sitting in the ISP PROM.
192		 * If we've just been reset, we'll have the string "ISP   "
193		 * spread through outgoing mailbox registers 1-3. We do
194		 * this for PCI cards because otherwise we really don't
195		 * know what state the card is in and we could hang if
196		 * we try this command otherwise.
197		 *
198		 * For SBus cards, we just do this because they almost
199		 * certainly will be running firmware by now.
200		 */
201		if (ISP_READ(isp, OUTMAILBOX1) != 0x4953 ||
202		    ISP_READ(isp, OUTMAILBOX2) != 0x5020 ||
203		    ISP_READ(isp, OUTMAILBOX3) != 0x2020) {
204			/*
205			 * Just in case it was paused...
206			 */
207			ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
208			mbs.param[0] = MBOX_ABOUT_FIRMWARE;
209			isp_mboxcmd(isp, &mbs, MBLOGNONE);
210			if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
211				isp->isp_romfw_rev[0] = mbs.param[1];
212				isp->isp_romfw_rev[1] = mbs.param[2];
213				isp->isp_romfw_rev[2] = mbs.param[3];
214			}
215		}
216		isp->isp_touched = 1;
217	}
218
219	DISABLE_INTS(isp);
220
221	/*
222	 * Set up default request/response queue in-pointer/out-pointer
223	 * register indices.
224	 */
225	if (IS_23XX(isp)) {
226		isp->isp_rqstinrp = BIU_REQINP;
227		isp->isp_rqstoutrp = BIU_REQOUTP;
228		isp->isp_respinrp = BIU_RSPINP;
229		isp->isp_respoutrp = BIU_RSPOUTP;
230	} else {
231		isp->isp_rqstinrp = INMAILBOX4;
232		isp->isp_rqstoutrp = OUTMAILBOX4;
233		isp->isp_respinrp = OUTMAILBOX5;
234		isp->isp_respoutrp = INMAILBOX5;
235	}
236
237	/*
238	 * Put the board into PAUSE mode (so we can read the SXP registers
239	 * or write FPM/FBM registers).
240	 */
241	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
242
243	if (IS_FC(isp)) {
244		switch (isp->isp_type) {
245		case ISP_HA_FC_2100:
246			btype = "2100";
247			break;
248		case ISP_HA_FC_2200:
249			btype = "2200";
250			break;
251		case ISP_HA_FC_2300:
252			btype = "2300";
253			break;
254		case ISP_HA_FC_2312:
255			btype = "2312";
256			break;
257		case ISP_HA_FC_2322:
258			btype = "2322";
259			break;
260		case ISP_HA_FC_2422:
261			btype = "2422";
262			break;
263		default:
264			break;
265		}
266		/*
267		 * While we're paused, reset the FPM module and FBM fifos.
268		 */
269		ISP_WRITE(isp, BIU2100_CSR, BIU2100_FPM0_REGS);
270		ISP_WRITE(isp, FPM_DIAG_CONFIG, FPM_SOFT_RESET);
271		ISP_WRITE(isp, BIU2100_CSR, BIU2100_FB_REGS);
272		ISP_WRITE(isp, FBM_CMD, FBMCMD_FIFO_RESET_ALL);
273		ISP_WRITE(isp, BIU2100_CSR, BIU2100_RISC_REGS);
274	} else if (IS_1240(isp)) {
275		sdparam *sdp = isp->isp_param;
276		btype = "1240";
277		isp->isp_clock = 60;
278		sdp->isp_ultramode = 1;
279		sdp++;
280		sdp->isp_ultramode = 1;
281		/*
282		 * XXX: Should probably do some bus sensing.
283		 */
284	} else if (IS_ULTRA2(isp)) {
285		static const char m[] = "bus %d is in %s Mode";
286		uint16_t l;
287		sdparam *sdp = isp->isp_param;
288
289		isp->isp_clock = 100;
290
291		if (IS_1280(isp))
292			btype = "1280";
293		else if (IS_1080(isp))
294			btype = "1080";
295		else if (IS_10160(isp))
296			btype = "10160";
297		else if (IS_12160(isp))
298			btype = "12160";
299		else
300			btype = "<UNKLVD>";
301
302		l = ISP_READ(isp, SXP_PINS_DIFF) & ISP1080_MODE_MASK;
303		switch (l) {
304		case ISP1080_LVD_MODE:
305			sdp->isp_lvdmode = 1;
306			isp_prt(isp, ISP_LOGCONFIG, m, 0, "LVD");
307			break;
308		case ISP1080_HVD_MODE:
309			sdp->isp_diffmode = 1;
310			isp_prt(isp, ISP_LOGCONFIG, m, 0, "Differential");
311			break;
312		case ISP1080_SE_MODE:
313			sdp->isp_ultramode = 1;
314			isp_prt(isp, ISP_LOGCONFIG, m, 0, "Single-Ended");
315			break;
316		default:
317			isp_prt(isp, ISP_LOGERR,
318			    "unknown mode on bus %d (0x%x)", 0, l);
319			break;
320		}
321
322		if (IS_DUALBUS(isp)) {
323			sdp++;
324			l = ISP_READ(isp, SXP_PINS_DIFF|SXP_BANK1_SELECT);
325			l &= ISP1080_MODE_MASK;
326			switch(l) {
327			case ISP1080_LVD_MODE:
328				sdp->isp_lvdmode = 1;
329				isp_prt(isp, ISP_LOGCONFIG, m, 1, "LVD");
330				break;
331			case ISP1080_HVD_MODE:
332				sdp->isp_diffmode = 1;
333				isp_prt(isp, ISP_LOGCONFIG,
334				    m, 1, "Differential");
335				break;
336			case ISP1080_SE_MODE:
337				sdp->isp_ultramode = 1;
338				isp_prt(isp, ISP_LOGCONFIG,
339				    m, 1, "Single-Ended");
340				break;
341			default:
342				isp_prt(isp, ISP_LOGERR,
343				    "unknown mode on bus %d (0x%x)", 1, l);
344				break;
345			}
346		}
347	} else {
348		sdparam *sdp = isp->isp_param;
349		i = ISP_READ(isp, BIU_CONF0) & BIU_CONF0_HW_MASK;
350		switch (i) {
351		default:
352			isp_prt(isp, ISP_LOGALL, "Unknown Chip Type 0x%x", i);
353			/* FALLTHROUGH */
354		case 1:
355			btype = "1020";
356			isp->isp_type = ISP_HA_SCSI_1020;
357			isp->isp_clock = 40;
358			break;
359		case 2:
360			/*
361			 * Some 1020A chips are Ultra Capable, but don't
362			 * run the clock rate up for that unless told to
363			 * do so by the Ultra Capable bits being set.
364			 */
365			btype = "1020A";
366			isp->isp_type = ISP_HA_SCSI_1020A;
367			isp->isp_clock = 40;
368			break;
369		case 3:
370			btype = "1040";
371			isp->isp_type = ISP_HA_SCSI_1040;
372			isp->isp_clock = 60;
373			break;
374		case 4:
375			btype = "1040A";
376			isp->isp_type = ISP_HA_SCSI_1040A;
377			isp->isp_clock = 60;
378			break;
379		case 5:
380			btype = "1040B";
381			isp->isp_type = ISP_HA_SCSI_1040B;
382			isp->isp_clock = 60;
383			break;
384		case 6:
385			btype = "1040C";
386			isp->isp_type = ISP_HA_SCSI_1040C;
387			isp->isp_clock = 60;
388                        break;
389		}
390		/*
391		 * Now, while we're at it, gather info about ultra
392		 * and/or differential mode.
393		 */
394		if (ISP_READ(isp, SXP_PINS_DIFF) & SXP_PINS_DIFF_MODE) {
395			isp_prt(isp, ISP_LOGCONFIG, "Differential Mode");
396			sdp->isp_diffmode = 1;
397		} else {
398			sdp->isp_diffmode = 0;
399		}
400		i = ISP_READ(isp, RISC_PSR);
401		if (isp->isp_bustype == ISP_BT_SBUS) {
402			i &= RISC_PSR_SBUS_ULTRA;
403		} else {
404			i &= RISC_PSR_PCI_ULTRA;
405		}
406		if (i != 0) {
407			isp_prt(isp, ISP_LOGCONFIG, "Ultra Mode Capable");
408			sdp->isp_ultramode = 1;
409			/*
410			 * If we're in Ultra Mode, we have to be 60MHz clock-
411			 * even for the SBus version.
412			 */
413			isp->isp_clock = 60;
414		} else {
415			sdp->isp_ultramode = 0;
416			/*
417			 * Clock is known. Gronk.
418			 */
419		}
420
421		/*
422		 * Machine dependent clock (if set) overrides
423		 * our generic determinations.
424		 */
425		if (isp->isp_mdvec->dv_clock) {
426			if (isp->isp_mdvec->dv_clock < isp->isp_clock) {
427				isp->isp_clock = isp->isp_mdvec->dv_clock;
428			}
429		}
430
431	}
432
433	/*
434	 * Clear instrumentation
435	 */
436	isp->isp_intcnt = isp->isp_intbogus = 0;
437
438	/*
439	 * Do MD specific pre initialization
440	 */
441	ISP_RESET0(isp);
442
443again:
444
445	/*
446	 * Hit the chip over the head with hammer,
447	 * and give the ISP a chance to recover.
448	 */
449
450	if (IS_SCSI(isp)) {
451		ISP_WRITE(isp, BIU_ICR, BIU_ICR_SOFT_RESET);
452		/*
453		 * A slight delay...
454		 */
455		USEC_DELAY(100);
456
457		/*
458		 * Clear data && control DMA engines.
459		 */
460		ISP_WRITE(isp, CDMA_CONTROL,
461		    DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
462		ISP_WRITE(isp, DDMA_CONTROL,
463		    DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
464
465
466	} else {
467		ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
468		/*
469		 * A slight delay...
470		 */
471		USEC_DELAY(100);
472
473		/*
474		 * Clear data && control DMA engines.
475		 */
476		ISP_WRITE(isp, CDMA2100_CONTROL,
477			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
478		ISP_WRITE(isp, TDMA2100_CONTROL,
479			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
480		ISP_WRITE(isp, RDMA2100_CONTROL,
481			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
482	}
483
484	/*
485	 * Wait for ISP to be ready to go...
486	 */
487	loops = MBOX_DELAY_COUNT;
488	for (;;) {
489		if (IS_SCSI(isp)) {
490			if (!(ISP_READ(isp, BIU_ICR) & BIU_ICR_SOFT_RESET))
491				break;
492		} else {
493			if (!(ISP_READ(isp, BIU2100_CSR) & BIU2100_SOFT_RESET))
494				break;
495		}
496		USEC_DELAY(100);
497		if (--loops < 0) {
498			ISP_DUMPREGS(isp, "chip reset timed out");
499			return;
500		}
501	}
502
503	/*
504	 * After we've fired this chip up, zero out the conf1 register
505	 * for SCSI adapters and other settings for the 2100.
506	 */
507
508	if (IS_SCSI(isp)) {
509		ISP_WRITE(isp, BIU_CONF1, 0);
510	} else {
511		ISP_WRITE(isp, BIU2100_CSR, 0);
512	}
513
514	/*
515	 * Reset RISC Processor
516	 */
517	ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
518	USEC_DELAY(100);
519	/* Clear semaphore register (just to be sure) */
520	ISP_WRITE(isp, BIU_SEMA, 0);
521
522	/*
523	 * Establish some initial burst rate stuff.
524	 * (only for the 1XX0 boards). This really should
525	 * be done later after fetching from NVRAM.
526	 */
527	if (IS_SCSI(isp)) {
528		uint16_t tmp = isp->isp_mdvec->dv_conf1;
529		/*
530		 * Busted FIFO. Turn off all but burst enables.
531		 */
532		if (isp->isp_type == ISP_HA_SCSI_1040A) {
533			tmp &= BIU_BURST_ENABLE;
534		}
535		ISP_SETBITS(isp, BIU_CONF1, tmp);
536		if (tmp & BIU_BURST_ENABLE) {
537			ISP_SETBITS(isp, CDMA_CONF, DMA_ENABLE_BURST);
538			ISP_SETBITS(isp, DDMA_CONF, DMA_ENABLE_BURST);
539		}
540#ifdef	PTI_CARDS
541		if (((sdparam *) isp->isp_param)->isp_ultramode) {
542			while (ISP_READ(isp, RISC_MTR) != 0x1313) {
543				ISP_WRITE(isp, RISC_MTR, 0x1313);
544				ISP_WRITE(isp, HCCR, HCCR_CMD_STEP);
545			}
546		} else {
547			ISP_WRITE(isp, RISC_MTR, 0x1212);
548		}
549		/*
550		 * PTI specific register
551		 */
552		ISP_WRITE(isp, RISC_EMB, DUAL_BANK)
553#else
554		ISP_WRITE(isp, RISC_MTR, 0x1212);
555#endif
556	} else {
557		ISP_WRITE(isp, RISC_MTR2100, 0x1212);
558		if (IS_2200(isp) || IS_23XX(isp)) {
559			ISP_WRITE(isp, HCCR, HCCR_2X00_DISABLE_PARITY_PAUSE);
560		}
561	}
562
563	ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE); /* release paused processor */
564
565	/*
566	 * Do MD specific post initialization
567	 */
568	ISP_RESET1(isp);
569
570	/*
571	 * Wait for everything to finish firing up.
572	 *
573	 * Avoid doing this on the 2312 because you can generate a PCI
574	 * parity error (chip breakage).
575	 */
576	if (IS_23XX(isp)) {
577		USEC_DELAY(5);
578	} else {
579		loops = MBOX_DELAY_COUNT;
580		while (ISP_READ(isp, OUTMAILBOX0) == MBOX_BUSY) {
581			USEC_DELAY(100);
582			if (--loops < 0) {
583				isp_prt(isp, ISP_LOGERR,
584				    "MBOX_BUSY never cleared on reset");
585				return;
586			}
587		}
588	}
589
590	/*
591	 * Up until this point we've done everything by just reading or
592	 * setting registers. From this point on we rely on at least *some*
593	 * kind of firmware running in the card.
594	 */
595
596	/*
597	 * Do some sanity checking.
598	 */
599	mbs.param[0] = MBOX_NO_OP;
600	isp_mboxcmd(isp, &mbs, MBLOGALL);
601	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
602		return;
603	}
604
605	if (IS_SCSI(isp)) {
606		mbs.param[0] = MBOX_MAILBOX_REG_TEST;
607		mbs.param[1] = 0xdead;
608		mbs.param[2] = 0xbeef;
609		mbs.param[3] = 0xffff;
610		mbs.param[4] = 0x1111;
611		mbs.param[5] = 0xa5a5;
612		isp_mboxcmd(isp, &mbs, MBLOGALL);
613		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
614			return;
615		}
616		if (mbs.param[1] != 0xdead || mbs.param[2] != 0xbeef ||
617		    mbs.param[3] != 0xffff || mbs.param[4] != 0x1111 ||
618		    mbs.param[5] != 0xa5a5) {
619			isp_prt(isp, ISP_LOGERR,
620			    "Register Test Failed (0x%x 0x%x 0x%x 0x%x 0x%x)",
621			    mbs.param[1], mbs.param[2], mbs.param[3],
622			    mbs.param[4], mbs.param[5]);
623			return;
624		}
625
626	}
627
628	/*
629	 * Download new Firmware, unless requested not to do so.
630	 * This is made slightly trickier in some cases where the
631	 * firmware of the ROM revision is newer than the revision
632	 * compiled into the driver. So, where we used to compare
633	 * versions of our f/w and the ROM f/w, now we just see
634	 * whether we have f/w at all and whether a config flag
635	 * has disabled our download.
636	 */
637	if ((isp->isp_mdvec->dv_ispfw == NULL) ||
638	    (isp->isp_confopts & ISP_CFG_NORELOAD)) {
639		dodnld = 0;
640	}
641
642	if (IS_23XX(isp))
643		code_org = ISP_CODE_ORG_2300;
644	else
645		code_org = ISP_CODE_ORG;
646
647	if (dodnld) {
648		isp->isp_mbxworkp = (void *) &isp->isp_mdvec->dv_ispfw[1];
649		isp->isp_mbxwrk0 = isp->isp_mdvec->dv_ispfw[3] - 1;
650		isp->isp_mbxwrk1 = code_org + 1;
651		mbs.param[0] = MBOX_WRITE_RAM_WORD;
652		mbs.param[1] = code_org;
653		mbs.param[2] = isp->isp_mdvec->dv_ispfw[0];
654		isp_mboxcmd(isp, &mbs, MBLOGNONE);
655		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
656			isp_prt(isp, ISP_LOGERR,
657			    "F/W download failed at word %d",
658			    isp->isp_mbxwrk1 - code_org);
659			dodnld = 0;
660			goto again;
661		}
662		/*
663		 * Verify that it downloaded correctly.
664		 */
665		mbs.param[0] = MBOX_VERIFY_CHECKSUM;
666		mbs.param[1] = code_org;
667		isp_mboxcmd(isp, &mbs, MBLOGNONE);
668		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
669			isp_prt(isp, ISP_LOGERR, "Ram Checksum Failure");
670			return;
671		}
672		isp->isp_loaded_fw = 1;
673	} else {
674		isp->isp_loaded_fw = 0;
675		isp_prt(isp, ISP_LOGDEBUG2, "skipping f/w download");
676	}
677
678	/*
679	 * Now start it rolling.
680	 *
681	 * If we didn't actually download f/w,
682	 * we still need to (re)start it.
683	 */
684
685
686	mbs.param[0] = MBOX_EXEC_FIRMWARE;
687	mbs.param[1] = code_org;
688	if (IS_2322(isp) || IS_24XX(isp)) {
689		if (isp->isp_loaded_fw) {
690			mbs.param[2] = 1;
691		} else {
692			mbs.param[2] = 0;
693		}
694		mbs.obits |= 2;
695	}
696
697	isp_mboxcmd(isp, &mbs, MBLOGNONE);
698	/*
699	 * Give it a chance to start.
700	 */
701	USEC_DELAY(500);
702
703	if (IS_SCSI(isp)) {
704		/*
705		 * Set CLOCK RATE, but only if asked to.
706		 */
707		if (isp->isp_clock) {
708			mbs.param[0] = MBOX_SET_CLOCK_RATE;
709			mbs.param[1] = isp->isp_clock;
710			isp_mboxcmd(isp, &mbs, MBLOGALL);
711			/* we will try not to care if this fails */
712		}
713	}
714
715	mbs.param[0] = MBOX_ABOUT_FIRMWARE;
716	isp_mboxcmd(isp, &mbs, MBLOGALL);
717	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
718		return;
719	}
720
721	/*
722	 * The SBus firmware that we are using apparently does not return
723	 * major, minor, micro revisions in the mailbox registers, which
724	 * is really, really, annoying.
725	 */
726	if (ISP_SBUS_SUPPORTED && isp->isp_bustype == ISP_BT_SBUS) {
727		if (dodnld) {
728#ifdef	ISP_TARGET_MODE
729			isp->isp_fwrev[0] = 7;
730			isp->isp_fwrev[1] = 55;
731#else
732			isp->isp_fwrev[0] = 1;
733			isp->isp_fwrev[1] = 37;
734#endif
735			isp->isp_fwrev[2] = 0;
736		}
737	} else {
738		isp->isp_fwrev[0] = mbs.param[1];
739		isp->isp_fwrev[1] = mbs.param[2];
740		isp->isp_fwrev[2] = mbs.param[3];
741	}
742	isp_prt(isp, ISP_LOGCONFIG,
743	    "Board Type %s, Chip Revision 0x%x, %s F/W Revision %d.%d.%d",
744	    btype, isp->isp_revision, dodnld? "loaded" : "resident",
745	    isp->isp_fwrev[0], isp->isp_fwrev[1], isp->isp_fwrev[2]);
746
747	if (IS_FC(isp)) {
748		/*
749		 * We do not believe firmware attributes for 2100 code less
750		 * than 1.17.0, unless it's the firmware we specifically
751		 * are loading.
752		 *
753		 * Note that all 22XX and 23XX f/w is greater than 1.X.0.
754		 */
755		if (!(ISP_FW_NEWER_THAN(isp, 1, 17, 0))) {
756#ifdef	USE_SMALLER_2100_FIRMWARE
757			FCPARAM(isp)->isp_fwattr = ISP_FW_ATTR_SCCLUN;
758#else
759			FCPARAM(isp)->isp_fwattr = 0;
760#endif
761		} else {
762			FCPARAM(isp)->isp_fwattr = mbs.param[6];
763			isp_prt(isp, ISP_LOGDEBUG0,
764			    "Firmware Attributes = 0x%x", mbs.param[6]);
765		}
766		if (IS_2KLOGIN(isp)) {
767			isp_prt(isp, ISP_LOGCONFIG, "2K Logins Supported");
768		}
769	}
770
771	if (isp->isp_romfw_rev[0] || isp->isp_romfw_rev[1] ||
772	    isp->isp_romfw_rev[2]) {
773		isp_prt(isp, ISP_LOGCONFIG, "Last F/W revision was %d.%d.%d",
774		    isp->isp_romfw_rev[0], isp->isp_romfw_rev[1],
775		    isp->isp_romfw_rev[2]);
776	}
777
778	mbs.param[0] = MBOX_GET_FIRMWARE_STATUS;
779	isp_mboxcmd(isp, &mbs, MBLOGALL);
780	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
781		return;
782	}
783	isp->isp_maxcmds = mbs.param[2];
784	isp_prt(isp, ISP_LOGINFO,
785	    "%d max I/O commands supported", mbs.param[2]);
786	isp_fw_state(isp);
787
788	/*
789	 * Set up DMA for the request and result mailboxes.
790	 */
791	if (ISP_MBOXDMASETUP(isp) != 0) {
792		isp_prt(isp, ISP_LOGERR, "Cannot setup DMA");
793		return;
794	}
795	isp->isp_state = ISP_RESETSTATE;
796
797	/*
798	 * Okay- now that we have new firmware running, we now (re)set our
799	 * notion of how many luns we support. This is somewhat tricky because
800	 * if we haven't loaded firmware, we sometimes do not have an easy way
801	 * of knowing how many luns we support.
802	 *
803	 * Expanded lun firmware gives you 32 luns for SCSI cards and
804	 * 16384 luns for Fibre Channel cards.
805	 *
806	 * It turns out that even for QLogic 2100s with ROM 1.10 and above
807	 * we do get a firmware attributes word returned in mailbox register 6.
808	 *
809	 * Because the lun is in a different position in the Request Queue
810	 * Entry structure for Fibre Channel with expanded lun firmware, we
811	 * can only support one lun (lun zero) when we don't know what kind
812	 * of firmware we're running.
813	 */
814	if (IS_SCSI(isp)) {
815		if (dodnld) {
816			if (IS_ULTRA2(isp) || IS_ULTRA3(isp)) {
817				isp->isp_maxluns = 32;
818			} else {
819				isp->isp_maxluns = 8;
820			}
821		} else {
822			isp->isp_maxluns = 8;
823		}
824	} else {
825		if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) {
826			isp->isp_maxluns = 16384;
827		} else {
828			isp->isp_maxluns = 16;
829		}
830	}
831}
832
833/*
834 * Initialize Parameters of Hardware to a known state.
835 *
836 * Locks are held before coming here.
837 */
838
839void
840isp_init(ispsoftc_t *isp)
841{
842	/*
843	 * Must do this first to get defaults established.
844	 */
845	isp_setdfltparm(isp, 0);
846	if (IS_DUALBUS(isp)) {
847		isp_setdfltparm(isp, 1);
848	}
849	if (IS_FC(isp)) {
850		isp_fibre_init(isp);
851	} else {
852		isp_scsi_init(isp);
853	}
854}
855
856static void
857isp_scsi_init(ispsoftc_t *isp)
858{
859	sdparam *sdp_chan0, *sdp_chan1;
860	mbreg_t mbs;
861
862	sdp_chan0 = isp->isp_param;
863	sdp_chan1 = sdp_chan0;
864	if (IS_DUALBUS(isp)) {
865		sdp_chan1++;
866	}
867
868	/*
869	 * If we have no role (neither target nor initiator), return.
870	 */
871	if (isp->isp_role == ISP_ROLE_NONE) {
872		return;
873	}
874
875	/* First do overall per-card settings. */
876
877	/*
878	 * If we have fast memory timing enabled, turn it on.
879	 */
880	if (sdp_chan0->isp_fast_mttr) {
881		ISP_WRITE(isp, RISC_MTR, 0x1313);
882	}
883
884	/*
885	 * Set Retry Delay and Count.
886	 * You set both channels at the same time.
887	 */
888	MEMZERO(&mbs, sizeof (mbs));
889	mbs.param[0] = MBOX_SET_RETRY_COUNT;
890	mbs.param[1] = sdp_chan0->isp_retry_count;
891	mbs.param[2] = sdp_chan0->isp_retry_delay;
892	mbs.param[6] = sdp_chan1->isp_retry_count;
893	mbs.param[7] = sdp_chan1->isp_retry_delay;
894
895	isp_mboxcmd(isp, &mbs, MBLOGALL);
896	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
897		return;
898	}
899
900	/*
901	 * Set ASYNC DATA SETUP time. This is very important.
902	 */
903	mbs.param[0] = MBOX_SET_ASYNC_DATA_SETUP_TIME;
904	mbs.param[1] = sdp_chan0->isp_async_data_setup;
905	mbs.param[2] = sdp_chan1->isp_async_data_setup;
906	isp_mboxcmd(isp, &mbs, MBLOGALL);
907	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
908		return;
909	}
910
911	/*
912	 * Set ACTIVE Negation State.
913	 */
914	mbs.param[0] = MBOX_SET_ACT_NEG_STATE;
915	mbs.param[1] =
916	    (sdp_chan0->isp_req_ack_active_neg << 4) |
917	    (sdp_chan0->isp_data_line_active_neg << 5);
918	mbs.param[2] =
919	    (sdp_chan1->isp_req_ack_active_neg << 4) |
920	    (sdp_chan1->isp_data_line_active_neg << 5);
921
922	isp_mboxcmd(isp, &mbs, MBLOGNONE);
923	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
924		isp_prt(isp, ISP_LOGERR,
925		    "failed to set active negation state (%d,%d), (%d,%d)",
926		    sdp_chan0->isp_req_ack_active_neg,
927		    sdp_chan0->isp_data_line_active_neg,
928		    sdp_chan1->isp_req_ack_active_neg,
929		    sdp_chan1->isp_data_line_active_neg);
930		/*
931		 * But don't return.
932		 */
933	}
934
935	/*
936	 * Set the Tag Aging limit
937	 */
938	mbs.param[0] = MBOX_SET_TAG_AGE_LIMIT;
939	mbs.param[1] = sdp_chan0->isp_tag_aging;
940	mbs.param[2] = sdp_chan1->isp_tag_aging;
941	isp_mboxcmd(isp, &mbs, MBLOGALL);
942	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
943		isp_prt(isp, ISP_LOGERR, "failed to set tag age limit (%d,%d)",
944		    sdp_chan0->isp_tag_aging, sdp_chan1->isp_tag_aging);
945		return;
946	}
947
948	/*
949	 * Set selection timeout.
950	 */
951	mbs.param[0] = MBOX_SET_SELECT_TIMEOUT;
952	mbs.param[1] = sdp_chan0->isp_selection_timeout;
953	mbs.param[2] = sdp_chan1->isp_selection_timeout;
954	isp_mboxcmd(isp, &mbs, MBLOGALL);
955	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
956		return;
957	}
958
959	/* now do per-channel settings */
960	isp_scsi_channel_init(isp, 0);
961	if (IS_DUALBUS(isp))
962		isp_scsi_channel_init(isp, 1);
963
964	/*
965	 * Now enable request/response queues
966	 */
967
968	if (IS_ULTRA2(isp) || IS_1240(isp)) {
969		mbs.param[0] = MBOX_INIT_RES_QUEUE_A64;
970		mbs.param[1] = RESULT_QUEUE_LEN(isp);
971		mbs.param[2] = DMA_WD1(isp->isp_result_dma);
972		mbs.param[3] = DMA_WD0(isp->isp_result_dma);
973		mbs.param[4] = 0;
974		mbs.param[6] = DMA_WD3(isp->isp_result_dma);
975		mbs.param[7] = DMA_WD2(isp->isp_result_dma);
976		isp_mboxcmd(isp, &mbs, MBLOGALL);
977		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
978			return;
979		}
980		isp->isp_residx = mbs.param[5];
981
982		mbs.param[0] = MBOX_INIT_REQ_QUEUE_A64;
983		mbs.param[1] = RQUEST_QUEUE_LEN(isp);
984		mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
985		mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
986		mbs.param[5] = 0;
987		mbs.param[6] = DMA_WD3(isp->isp_result_dma);
988		mbs.param[7] = DMA_WD2(isp->isp_result_dma);
989		isp_mboxcmd(isp, &mbs, MBLOGALL);
990		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
991			return;
992		}
993		isp->isp_reqidx = isp->isp_reqodx = mbs.param[4];
994	} else {
995		mbs.param[0] = MBOX_INIT_RES_QUEUE;
996		mbs.param[1] = RESULT_QUEUE_LEN(isp);
997		mbs.param[2] = DMA_WD1(isp->isp_result_dma);
998		mbs.param[3] = DMA_WD0(isp->isp_result_dma);
999		mbs.param[4] = 0;
1000		isp_mboxcmd(isp, &mbs, MBLOGALL);
1001		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1002			return;
1003		}
1004		isp->isp_residx = mbs.param[5];
1005
1006		mbs.param[0] = MBOX_INIT_REQ_QUEUE;
1007		mbs.param[1] = RQUEST_QUEUE_LEN(isp);
1008		mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
1009		mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
1010		mbs.param[5] = 0;
1011		isp_mboxcmd(isp, &mbs, MBLOGALL);
1012		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1013			return;
1014		}
1015		isp->isp_reqidx = isp->isp_reqodx = mbs.param[4];
1016	}
1017
1018	/*
1019	 * Turn on Fast Posting, LVD transitions
1020	 *
1021	 * Ultra2 F/W always has had fast posting (and LVD transitions)
1022	 *
1023	 * Ultra and older (i.e., SBus) cards may not. It's just safer
1024	 * to assume not for them.
1025	 */
1026
1027	mbs.param[0] = MBOX_SET_FW_FEATURES;
1028	mbs.param[1] = 0;
1029	if (IS_ULTRA2(isp))
1030		mbs.param[1] |= FW_FEATURE_LVD_NOTIFY;
1031#ifndef	ISP_NO_RIO
1032	if (IS_ULTRA2(isp) || IS_1240(isp))
1033		mbs.param[1] |= FW_FEATURE_RIO_16BIT;
1034#else
1035#ifndef	ISP_NO_FASTPOST
1036	if (IS_ULTRA2(isp) || IS_1240(isp))
1037		mbs.param[1] |= FW_FEATURE_FAST_POST;
1038#endif
1039#endif
1040	if (mbs.param[1] != 0) {
1041		uint16_t sfeat = mbs.param[1];
1042		isp_mboxcmd(isp, &mbs, MBLOGALL);
1043		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1044			isp_prt(isp, ISP_LOGINFO,
1045			    "Enabled FW features (0x%x)", sfeat);
1046		}
1047	}
1048
1049	/*
1050	 * Let the outer layers decide whether to issue a SCSI bus reset.
1051	 */
1052	isp->isp_state = ISP_INITSTATE;
1053}
1054
1055static void
1056isp_scsi_channel_init(ispsoftc_t *isp, int channel)
1057{
1058	sdparam *sdp;
1059	mbreg_t mbs;
1060	int tgt;
1061
1062	sdp = isp->isp_param;
1063	sdp += channel;
1064
1065	/*
1066	 * Set (possibly new) Initiator ID.
1067	 */
1068	MEMZERO(&mbs, sizeof (mbs));
1069	mbs.param[0] = MBOX_SET_INIT_SCSI_ID;
1070	mbs.param[1] = (channel << 7) | sdp->isp_initiator_id;
1071	isp_mboxcmd(isp, &mbs, MBLOGALL);
1072	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1073		return;
1074	}
1075	isp_prt(isp, ISP_LOGINFO, "Initiator ID is %d on Channel %d",
1076	    sdp->isp_initiator_id, channel);
1077
1078
1079	/*
1080	 * Set current per-target parameters to an initial safe minimum.
1081	 */
1082	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
1083		int lun;
1084		uint16_t sdf;
1085
1086		if (sdp->isp_devparam[tgt].dev_enable == 0) {
1087			continue;
1088		}
1089#ifndef	ISP_TARGET_MODE
1090		sdf = sdp->isp_devparam[tgt].goal_flags;
1091		sdf &= DPARM_SAFE_DFLT;
1092		/*
1093		 * It is not quite clear when this changed over so that
1094		 * we could force narrow and async for 1000/1020 cards,
1095		 * but assume that this is only the case for loaded
1096		 * firmware.
1097		 */
1098		if (isp->isp_loaded_fw) {
1099			sdf |= DPARM_NARROW | DPARM_ASYNC;
1100		}
1101#else
1102		/*
1103		 * The !$*!)$!$)* f/w uses the same index into some
1104		 * internal table to decide how to respond to negotiations,
1105		 * so if we've said "let's be safe" for ID X, and ID X
1106		 * selects *us*, the negotiations will back to 'safe'
1107		 * (as in narrow/async). What the f/w *should* do is
1108		 * use the initiator id settings to decide how to respond.
1109		 */
1110		sdp->isp_devparam[tgt].goal_flags = sdf = DPARM_DEFAULT;
1111#endif
1112		mbs.param[0] = MBOX_SET_TARGET_PARAMS;
1113		mbs.param[1] = (channel << 15) | (tgt << 8);
1114		mbs.param[2] = sdf;
1115		if ((sdf & DPARM_SYNC) == 0) {
1116			mbs.param[3] = 0;
1117		} else {
1118			mbs.param[3] =
1119			    (sdp->isp_devparam[tgt].goal_offset << 8) |
1120			    (sdp->isp_devparam[tgt].goal_period);
1121		}
1122		isp_prt(isp, ISP_LOGDEBUG0,
1123		    "Initial Settings bus%d tgt%d flags 0x%x off 0x%x per 0x%x",
1124		    channel, tgt, mbs.param[2], mbs.param[3] >> 8,
1125		    mbs.param[3] & 0xff);
1126		isp_mboxcmd(isp, &mbs, MBLOGNONE);
1127		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1128			sdf = DPARM_SAFE_DFLT;
1129			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
1130			mbs.param[1] = (tgt << 8) | (channel << 15);
1131			mbs.param[2] = sdf;
1132			mbs.param[3] = 0;
1133			isp_mboxcmd(isp, &mbs, MBLOGALL);
1134			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1135				continue;
1136			}
1137		}
1138
1139		/*
1140		 * We don't update any information directly from the f/w
1141		 * because we need to run at least one command to cause a
1142		 * new state to be latched up. So, we just assume that we
1143		 * converge to the values we just had set.
1144		 *
1145		 * Ensure that we don't believe tagged queuing is enabled yet.
1146		 * It turns out that sometimes the ISP just ignores our
1147		 * attempts to set parameters for devices that it hasn't
1148		 * seen yet.
1149		 */
1150		sdp->isp_devparam[tgt].actv_flags = sdf & ~DPARM_TQING;
1151		for (lun = 0; lun < (int) isp->isp_maxluns; lun++) {
1152			mbs.param[0] = MBOX_SET_DEV_QUEUE_PARAMS;
1153			mbs.param[1] = (channel << 15) | (tgt << 8) | lun;
1154			mbs.param[2] = sdp->isp_max_queue_depth;
1155			mbs.param[3] = sdp->isp_devparam[tgt].exc_throttle;
1156			isp_mboxcmd(isp, &mbs, MBLOGALL);
1157			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1158				break;
1159			}
1160		}
1161	}
1162	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
1163		if (sdp->isp_devparam[tgt].dev_refresh) {
1164			isp->isp_sendmarker |= (1 << channel);
1165			isp->isp_update |= (1 << channel);
1166			break;
1167		}
1168	}
1169}
1170
1171/*
1172 * Fibre Channel specific initialization.
1173 *
1174 * Locks are held before coming here.
1175 */
1176static void
1177isp_fibre_init(ispsoftc_t *isp)
1178{
1179	fcparam *fcp;
1180	isp_icb_t local, *icbp = &local;
1181	mbreg_t mbs;
1182	int loopid;
1183	uint64_t nwwn, pwwn;
1184
1185	fcp = isp->isp_param;
1186
1187	/*
1188	 * Do this *before* initializing the firmware.
1189	 */
1190	isp_mark_getpdb_all(isp);
1191	fcp->isp_fwstate = FW_CONFIG_WAIT;
1192	fcp->isp_loopstate = LOOP_NIL;
1193
1194	/*
1195	 * If we have no role (neither target nor initiator), return.
1196	 */
1197	if (isp->isp_role == ISP_ROLE_NONE) {
1198		return;
1199	}
1200
1201	loopid = fcp->isp_loopid;
1202	MEMZERO(icbp, sizeof (*icbp));
1203	icbp->icb_version = ICB_VERSION1;
1204
1205	/*
1206	 * Firmware Options are either retrieved from NVRAM or
1207	 * are patched elsewhere. We check them for sanity here
1208	 * and make changes based on board revision, but otherwise
1209	 * let others decide policy.
1210	 */
1211
1212	/*
1213	 * If this is a 2100 < revision 5, we have to turn off FAIRNESS.
1214	 */
1215	if ((isp->isp_type == ISP_HA_FC_2100) && isp->isp_revision < 5) {
1216		fcp->isp_fwoptions &= ~ICBOPT_FAIRNESS;
1217	}
1218
1219	/*
1220	 * We have to use FULL LOGIN even though it resets the loop too much
1221	 * because otherwise port database entries don't get updated after
1222	 * a LIP- this is a known f/w bug for 2100 f/w less than 1.17.0.
1223	 */
1224	if (!ISP_FW_NEWER_THAN(isp, 1, 17, 0)) {
1225		fcp->isp_fwoptions |= ICBOPT_FULL_LOGIN;
1226	}
1227
1228	/*
1229	 * Insist on Port Database Update Async notifications
1230	 */
1231	fcp->isp_fwoptions |= ICBOPT_PDBCHANGE_AE;
1232
1233	/*
1234	 * Make sure that target role reflects into fwoptions.
1235	 */
1236	if (isp->isp_role & ISP_ROLE_TARGET) {
1237		fcp->isp_fwoptions |= ICBOPT_TGT_ENABLE;
1238	} else {
1239		fcp->isp_fwoptions &= ~ICBOPT_TGT_ENABLE;
1240	}
1241
1242	if (isp->isp_role & ISP_ROLE_INITIATOR) {
1243		fcp->isp_fwoptions &= ~ICBOPT_INI_DISABLE;
1244	} else {
1245		fcp->isp_fwoptions |= ICBOPT_INI_DISABLE;
1246	}
1247
1248	/*
1249	 * Propagate all of this into the ICB structure.
1250	 */
1251	icbp->icb_fwoptions = fcp->isp_fwoptions;
1252	icbp->icb_maxfrmlen = fcp->isp_maxfrmlen;
1253	if (icbp->icb_maxfrmlen < ICB_MIN_FRMLEN ||
1254	    icbp->icb_maxfrmlen > ICB_MAX_FRMLEN) {
1255		isp_prt(isp, ISP_LOGERR,
1256		    "bad frame length (%d) from NVRAM- using %d",
1257		    fcp->isp_maxfrmlen, ICB_DFLT_FRMLEN);
1258		icbp->icb_maxfrmlen = ICB_DFLT_FRMLEN;
1259	}
1260	icbp->icb_maxalloc = fcp->isp_maxalloc;
1261	if (icbp->icb_maxalloc < 1) {
1262		isp_prt(isp, ISP_LOGERR,
1263		    "bad maximum allocation (%d)- using 16", fcp->isp_maxalloc);
1264		icbp->icb_maxalloc = 16;
1265	}
1266	icbp->icb_execthrottle = fcp->isp_execthrottle;
1267	if (icbp->icb_execthrottle < 1) {
1268		isp_prt(isp, ISP_LOGERR,
1269		    "bad execution throttle of %d- using 16",
1270		    fcp->isp_execthrottle);
1271		icbp->icb_execthrottle = ICB_DFLT_THROTTLE;
1272	}
1273	icbp->icb_retry_delay = fcp->isp_retry_delay;
1274	icbp->icb_retry_count = fcp->isp_retry_count;
1275	icbp->icb_hardaddr = loopid;
1276	if (icbp->icb_hardaddr >= 125) {
1277		/*
1278		 * We end up with these Loop IDs for F-Port topologies
1279		 */
1280		if (icbp->icb_hardaddr != 0xff && icbp->icb_hardaddr != 0x800) {
1281		    isp_prt(isp, ISP_LOGERR,
1282			"bad hard address %u- resetting to zero",
1283			icbp->icb_hardaddr);
1284		}
1285		icbp->icb_hardaddr = 0;
1286	}
1287	/*
1288	 * Right now we just set extended options to prefer point-to-point
1289	 * over loop based upon some soft config options.
1290	 *
1291	 * NB: for the 2300, ICBOPT_EXTENDED is required.
1292	 */
1293	if (IS_2200(isp) || IS_23XX(isp)) {
1294		icbp->icb_fwoptions |= ICBOPT_EXTENDED;
1295		/*
1296		 * Prefer or force Point-To-Point instead Loop?
1297		 */
1298		switch(isp->isp_confopts & ISP_CFG_PORT_PREF) {
1299		case ISP_CFG_NPORT:
1300			icbp->icb_xfwoptions |= ICBXOPT_PTP_2_LOOP;
1301			break;
1302		case ISP_CFG_NPORT_ONLY:
1303			icbp->icb_xfwoptions |= ICBXOPT_PTP_ONLY;
1304			break;
1305		case ISP_CFG_LPORT_ONLY:
1306			icbp->icb_xfwoptions |= ICBXOPT_LOOP_ONLY;
1307			break;
1308		default:
1309			icbp->icb_xfwoptions |= ICBXOPT_LOOP_2_PTP;
1310			break;
1311		}
1312		if (IS_23XX(isp)) {
1313			/*
1314			 * QLogic recommends that FAST Posting be turned
1315			 * off for 23XX cards and instead allow the HBA
1316			 * to write response queue entries and interrupt
1317			 * after a delay (ZIO).
1318			 *
1319			 * If we set ZIO, it will disable fast posting,
1320			 * so we don't need to clear it in fwoptions.
1321			 *
1322			 * Depending on the role we're selecting, we
1323			 * chose fast posting or not as it still is
1324			 * a win for target mode.
1325			 */
1326#ifndef	ISP_NO_ZIO
1327			if (isp->isp_role == ISP_ROLE_TARGET) {
1328				icbp->icb_fwoptions |= ICBOPT_FAST_POST;
1329			} else {
1330				icbp->icb_xfwoptions |= ICBXOPT_ZIO;
1331			}
1332#else
1333			icbp->icb_fwoptions |= ICBOPT_FAST_POST;
1334#endif
1335#if	0
1336			/*
1337			 * Values, in 100us increments. The default
1338			 * is 2 (200us) if a value 0 (default) is
1339			 * selected.
1340			 */
1341			icbp->icb_idelaytimer = 2;
1342#endif
1343
1344			if (isp->isp_confopts & ISP_CFG_ONEGB) {
1345				icbp->icb_zfwoptions |= ICBZOPT_RATE_ONEGB;
1346			} else if (isp->isp_confopts & ISP_CFG_TWOGB) {
1347				icbp->icb_zfwoptions |= ICBZOPT_RATE_TWOGB;
1348			} else {
1349				icbp->icb_zfwoptions |= ICBZOPT_RATE_AUTO;
1350			}
1351		}
1352	}
1353
1354#ifndef	ISP_NO_RIO_FC
1355	/*
1356	 * RIO seems to be enabled in 2100s for fw >= 1.17.0.
1357	 *
1358	 * I've had some questionable problems with RIO on 2200.
1359	 * More specifically, on a 2204 I had problems with RIO
1360	 * on a Linux system where I was dropping commands right
1361	 * and left. It's not clear to me what the actual problem
1362	 * was.
1363	 *
1364	 * 23XX Cards do not support RIO. Instead they support ZIO.
1365	 */
1366#if	0
1367	if (!IS_23XX(isp) && ISP_FW_NEWER_THAN(isp, 1, 17, 0)) {
1368		icbp->icb_xfwoptions |= ICBXOPT_RIO_16BIT;
1369		icbp->icb_racctimer = 4;
1370		icbp->icb_idelaytimer = 8;
1371	}
1372#endif
1373#endif
1374
1375	MEMZERO(&mbs, sizeof (mbs));
1376
1377	/*
1378	 * For 22XX > 2.1.26 && 23XX, set some options.
1379	 * XXX: Probably okay for newer 2100 f/w too.
1380	 */
1381	if (ISP_FW_NEWER_THAN(isp, 2, 26, 0)) {
1382		/*
1383		 * Turn on LIP F8 async event (1)
1384		 * Turn on generate AE 8013 on all LIP Resets (2)
1385		 * Disable LIP F7 switching (8)
1386		 */
1387		mbs.param[0] = MBOX_SET_FIRMWARE_OPTIONS;
1388		mbs.param[1] = 0xb;
1389		mbs.param[2] = 0;
1390		mbs.param[3] = 0;
1391		isp_mboxcmd(isp, &mbs, MBLOGALL);
1392	}
1393	icbp->icb_logintime = ICB_LOGIN_TOV;
1394	icbp->icb_lunetimeout = ICB_LUN_ENABLE_TOV;
1395
1396	if (IS_23XX(isp)) {
1397		ISP_WRITE(isp, isp->isp_rqstinrp, 0);
1398        	ISP_WRITE(isp, isp->isp_rqstoutrp, 0);
1399        	ISP_WRITE(isp, isp->isp_respinrp, 0);
1400		ISP_WRITE(isp, isp->isp_respoutrp, 0);
1401	}
1402
1403	nwwn = ISP_NODEWWN(isp);
1404	pwwn = ISP_PORTWWN(isp);
1405	if (nwwn && pwwn) {
1406		icbp->icb_fwoptions |= ICBOPT_BOTH_WWNS;
1407		MAKE_NODE_NAME_FROM_WWN(icbp->icb_nodename, nwwn);
1408		MAKE_NODE_NAME_FROM_WWN(icbp->icb_portname, pwwn);
1409		isp_prt(isp, ISP_LOGDEBUG1,
1410		    "Setting ICB Node 0x%08x%08x Port 0x%08x%08x",
1411		    ((uint32_t) (nwwn >> 32)),
1412		    ((uint32_t) (nwwn & 0xffffffff)),
1413		    ((uint32_t) (pwwn >> 32)),
1414		    ((uint32_t) (pwwn & 0xffffffff)));
1415	} else {
1416		isp_prt(isp, ISP_LOGDEBUG1, "Not using any WWNs");
1417		icbp->icb_fwoptions &= ~(ICBOPT_BOTH_WWNS|ICBOPT_FULL_LOGIN);
1418	}
1419	icbp->icb_rqstqlen = RQUEST_QUEUE_LEN(isp);
1420	if (icbp->icb_rqstqlen < 1) {
1421		isp_prt(isp, ISP_LOGERR, "bad request queue length");
1422	}
1423	icbp->icb_rsltqlen = RESULT_QUEUE_LEN(isp);
1424	if (icbp->icb_rsltqlen < 1) {
1425		isp_prt(isp, ISP_LOGERR, "bad result queue length");
1426	}
1427	icbp->icb_rqstaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_rquest_dma);
1428	icbp->icb_rqstaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_rquest_dma);
1429	icbp->icb_rqstaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_rquest_dma);
1430	icbp->icb_rqstaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_rquest_dma);
1431	icbp->icb_respaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_result_dma);
1432	icbp->icb_respaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_result_dma);
1433	icbp->icb_respaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_result_dma);
1434	icbp->icb_respaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_result_dma);
1435	isp_prt(isp, ISP_LOGDEBUG0,
1436	    "isp_fibre_init: fwopt 0x%x xfwopt 0x%x zfwopt 0x%x",
1437	    icbp->icb_fwoptions, icbp->icb_xfwoptions, icbp->icb_zfwoptions);
1438
1439	FC_SCRATCH_ACQUIRE(isp);
1440	isp_put_icb(isp, icbp, (isp_icb_t *)fcp->isp_scratch);
1441
1442	/*
1443	 * Init the firmware
1444	 */
1445	mbs.param[0] = MBOX_INIT_FIRMWARE;
1446	mbs.param[1] = 0;
1447	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
1448	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
1449	mbs.param[4] = 0;
1450	mbs.param[5] = 0;
1451	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
1452	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
1453	isp_mboxcmd(isp, &mbs, MBLOGALL);
1454	FC_SCRATCH_RELEASE(isp);
1455	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1456		return;
1457	}
1458	isp->isp_reqidx = isp->isp_reqodx = 0;
1459	isp->isp_residx = 0;
1460	isp->isp_sendmarker = 1;
1461
1462	/*
1463	 * Whatever happens, we're now committed to being here.
1464	 */
1465	isp->isp_state = ISP_INITSTATE;
1466}
1467
1468/*
1469 * Fibre Channel Support- get the port database for the id.
1470 *
1471 * Locks are held before coming here. Return 0 if success,
1472 * else failure.
1473 */
1474
1475static int
1476isp_getmap(ispsoftc_t *isp, fcpos_map_t *map)
1477{
1478	fcparam *fcp = (fcparam *) isp->isp_param;
1479	mbreg_t mbs;
1480
1481	MEMZERO(&mbs, sizeof (mbs));
1482	mbs.param[0] = MBOX_GET_FC_AL_POSITION_MAP;
1483	mbs.param[1] = 0;
1484	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
1485	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
1486	/*
1487	 * Unneeded. For the 2100, except for initializing f/w, registers
1488	 * 4/5 have to not be written to.
1489	 *	mbs.param[4] = 0;
1490	 *	mbs.param[5] = 0;
1491	 *
1492	 */
1493	mbs.param[6] = 0;
1494	mbs.param[7] = 0;
1495	FC_SCRATCH_ACQUIRE(isp);
1496	isp_mboxcmd(isp, &mbs, MBLOGALL & ~MBOX_COMMAND_PARAM_ERROR);
1497	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1498		MEMCPY(map, fcp->isp_scratch, sizeof (fcpos_map_t));
1499		map->fwmap = mbs.param[1] != 0;
1500		FC_SCRATCH_RELEASE(isp);
1501		return (0);
1502	}
1503	FC_SCRATCH_RELEASE(isp);
1504	return (-1);
1505}
1506
1507static void
1508isp_mark_getpdb_all(ispsoftc_t *isp)
1509{
1510	fcparam *fcp = (fcparam *) isp->isp_param;
1511	int i;
1512	for (i = 0; i < MAX_FC_TARG; i++) {
1513		fcp->portdb[i].valid = fcp->portdb[i].fabric_dev = 0;
1514	}
1515}
1516
1517static int
1518isp_getpdb(ispsoftc_t *isp, int id, isp_pdb_t *pdbp)
1519{
1520	fcparam *fcp = (fcparam *) isp->isp_param;
1521	mbreg_t mbs;
1522
1523	MEMZERO(&mbs, sizeof (mbs));
1524	mbs.param[0] = MBOX_GET_PORT_DB;
1525	if (IS_2KLOGIN(isp)) {
1526		mbs.param[1] = id;
1527		mbs.obits |= (1 << 10);
1528	} else {
1529		mbs.param[1] = id << 8;
1530	}
1531	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
1532	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
1533	/*
1534	 * Unneeded. For the 2100, except for initializing f/w, registers
1535	 * 4/5 have to not be written to.
1536	 *	mbs.param[4] = 0;
1537	 *	mbs.param[5] = 0;
1538	 *
1539	 */
1540	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
1541	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
1542	FC_SCRATCH_ACQUIRE(isp);
1543	isp_mboxcmd(isp, &mbs, MBLOGALL & ~MBOX_COMMAND_PARAM_ERROR);
1544	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1545		isp_get_pdb(isp, (isp_pdb_t *)fcp->isp_scratch, pdbp);
1546		FC_SCRATCH_RELEASE(isp);
1547		return (0);
1548	}
1549	FC_SCRATCH_RELEASE(isp);
1550	return (-1);
1551}
1552
1553static uint64_t
1554isp_get_portname(ispsoftc_t *isp, int loopid, int nodename)
1555{
1556	uint64_t wwn = 0;
1557	mbreg_t mbs;
1558
1559	MEMZERO(&mbs, sizeof (mbs));
1560	mbs.param[0] = MBOX_GET_PORT_NAME;
1561	if (IS_2KLOGIN(isp)) {
1562		mbs.param[1] = loopid;
1563		if (nodename)
1564			mbs.param[10] = 1;
1565		mbs.obits |= (1 << 10);
1566	} else {
1567		mbs.param[1] = loopid << 8;
1568		if (nodename)
1569			mbs.param[1] |= 1;
1570	}
1571	isp_mboxcmd(isp, &mbs, MBLOGALL & ~MBOX_COMMAND_PARAM_ERROR);
1572	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1573		wwn =
1574		    (((uint64_t)(mbs.param[2] & 0xff)) << 56) |
1575		    (((uint64_t)(mbs.param[2] >> 8))	<< 48) |
1576		    (((uint64_t)(mbs.param[3] & 0xff))	<< 40) |
1577		    (((uint64_t)(mbs.param[3] >> 8))	<< 32) |
1578		    (((uint64_t)(mbs.param[6] & 0xff))	<< 24) |
1579		    (((uint64_t)(mbs.param[6] >> 8))	<< 16) |
1580		    (((uint64_t)(mbs.param[7] & 0xff))	<<  8) |
1581		    (((uint64_t)(mbs.param[7] >> 8)));
1582	}
1583	return (wwn);
1584}
1585
1586/*
1587 * Make sure we have good FC link and know our Loop ID.
1588 */
1589
1590static int
1591isp_fclink_test(ispsoftc_t *isp, int usdelay)
1592{
1593	static char *toponames[] = {
1594		"Private Loop",
1595		"FL Port",
1596		"N-Port to N-Port",
1597		"F Port",
1598		"F Port (no FLOGI_ACC response)"
1599	};
1600	mbreg_t mbs;
1601	int count, check_for_fabric;
1602	uint8_t lwfs;
1603	fcparam *fcp;
1604	struct lportdb *lp;
1605	isp_pdb_t pdb;
1606
1607	fcp = isp->isp_param;
1608
1609	/*
1610	 * XXX: Here is where we would start a 'loop dead' timeout
1611	 */
1612
1613	/*
1614	 * Wait up to N microseconds for F/W to go to a ready state.
1615	 */
1616	lwfs = FW_CONFIG_WAIT;
1617	count = 0;
1618	while (count < usdelay) {
1619		uint64_t enano;
1620		uint32_t wrk;
1621		NANOTIME_T hra, hrb;
1622
1623		GET_NANOTIME(&hra);
1624		isp_fw_state(isp);
1625		if (lwfs != fcp->isp_fwstate) {
1626			isp_prt(isp, ISP_LOGINFO, "Firmware State <%s->%s>",
1627			    isp2100_fw_statename((int)lwfs),
1628			    isp2100_fw_statename((int)fcp->isp_fwstate));
1629			lwfs = fcp->isp_fwstate;
1630		}
1631		if (fcp->isp_fwstate == FW_READY) {
1632			break;
1633		}
1634		GET_NANOTIME(&hrb);
1635
1636		/*
1637		 * Get the elapsed time in nanoseconds.
1638		 * Always guaranteed to be non-zero.
1639		 */
1640		enano = NANOTIME_SUB(&hrb, &hra);
1641
1642		isp_prt(isp, ISP_LOGDEBUG1,
1643		    "usec%d: 0x%lx->0x%lx enano 0x%x%08x",
1644		    count, (long) GET_NANOSEC(&hra), (long) GET_NANOSEC(&hrb),
1645		    (uint32_t)(enano >> 32), (uint32_t)(enano & 0xffffffff));
1646
1647		/*
1648		 * If the elapsed time is less than 1 millisecond,
1649		 * delay a period of time up to that millisecond of
1650		 * waiting.
1651		 *
1652		 * This peculiar code is an attempt to try and avoid
1653		 * invoking uint64_t math support functions for some
1654		 * platforms where linkage is a problem.
1655		 */
1656		if (enano < (1000 * 1000)) {
1657			count += 1000;
1658			enano = (1000 * 1000) - enano;
1659			while (enano > (uint64_t) 4000000000U) {
1660				USEC_SLEEP(isp, 4000000);
1661				enano -= (uint64_t) 4000000000U;
1662			}
1663			wrk = enano;
1664			wrk /= 1000;
1665			USEC_SLEEP(isp, wrk);
1666		} else {
1667			while (enano > (uint64_t) 4000000000U) {
1668				count += 4000000;
1669				enano -= (uint64_t) 4000000000U;
1670			}
1671			wrk = enano;
1672			count += (wrk / 1000);
1673		}
1674	}
1675
1676	/*
1677	 * If we haven't gone to 'ready' state, return.
1678	 */
1679	if (fcp->isp_fwstate != FW_READY) {
1680		return (-1);
1681	}
1682
1683	/*
1684	 * Get our Loop ID (if possible). We really need to have it.
1685	 */
1686	MEMZERO(&mbs, sizeof (mbs));
1687	mbs.param[0] = MBOX_GET_LOOP_ID;
1688	isp_mboxcmd(isp, &mbs, MBLOGALL);
1689	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1690		return (-1);
1691	}
1692	fcp->isp_loopid = mbs.param[1];
1693	if (fcp->isp_loopid == 0xffff) {	/* happens with 2k login f/w */
1694		fcp->isp_loopid = MAX_FC_TARG-1;
1695	} else if (fcp->isp_loopid >= MAX_FC_TARG) {
1696		isp_prt(isp, ISP_LOGWARN, "bad initiator loopid (0x%x)", fcp->isp_loopid);
1697		fcp->isp_loopid = MAX_FC_TARG-1;
1698	}
1699	if (IS_2200(isp) || IS_23XX(isp)) {
1700		int topo = (int) mbs.param[6];
1701		if (topo < TOPO_NL_PORT || topo > TOPO_PTP_STUB)
1702			topo = TOPO_PTP_STUB;
1703		fcp->isp_topo = topo;
1704	} else {
1705		fcp->isp_topo = TOPO_NL_PORT;
1706	}
1707	/*
1708	 * Get the port id.
1709	 */
1710	fcp->isp_portid = mbs.param[2] | (mbs.param[3] << 16);
1711
1712	/*
1713	 * Check to see if we're on a fabric by trying to see if we
1714	 * can talk to the fabric name server. This can be a bit
1715	 * tricky because if we're a 2100, we should check always
1716	 * (in case we're connected to a server doing aliasing).
1717	 */
1718	fcp->isp_onfabric = 0;
1719
1720	if (IS_2100(isp)) {
1721		/*
1722		 * Don't bother with fabric if we are using really old
1723		 * 2100 firmware. It's just not worth it.
1724		 */
1725		if (ISP_FW_NEWER_THAN(isp, 1, 15, 37)) {
1726			check_for_fabric = 1;
1727		} else {
1728			check_for_fabric = 0;
1729		}
1730	} else if (fcp->isp_topo == TOPO_FL_PORT ||
1731	    fcp->isp_topo == TOPO_F_PORT) {
1732		check_for_fabric = 1;
1733	} else
1734		check_for_fabric = 0;
1735
1736	if (check_for_fabric && isp_getpdb(isp, FL_PORT_ID, &pdb) == 0) {
1737		int loopid = FL_PORT_ID;
1738		if (IS_2100(isp)) {
1739			fcp->isp_topo = TOPO_FL_PORT;
1740		}
1741
1742		if (BITS2WORD(pdb.pdb_portid_bits) == 0) {
1743			/*
1744			 * Crock.
1745			 */
1746			fcp->isp_topo = TOPO_NL_PORT;
1747			goto not_on_fabric;
1748		}
1749		fcp->isp_portid = mbs.param[2] | ((int) mbs.param[3] << 16);
1750
1751		/*
1752		 * Save the Fabric controller's port database entry.
1753		 */
1754		lp = &fcp->portdb[loopid];
1755		lp->node_wwn =
1756		    (((uint64_t)pdb.pdb_nodename[0]) << 56) |
1757		    (((uint64_t)pdb.pdb_nodename[1]) << 48) |
1758		    (((uint64_t)pdb.pdb_nodename[2]) << 40) |
1759		    (((uint64_t)pdb.pdb_nodename[3]) << 32) |
1760		    (((uint64_t)pdb.pdb_nodename[4]) << 24) |
1761		    (((uint64_t)pdb.pdb_nodename[5]) << 16) |
1762		    (((uint64_t)pdb.pdb_nodename[6]) <<  8) |
1763		    (((uint64_t)pdb.pdb_nodename[7]));
1764		lp->port_wwn =
1765		    (((uint64_t)pdb.pdb_portname[0]) << 56) |
1766		    (((uint64_t)pdb.pdb_portname[1]) << 48) |
1767		    (((uint64_t)pdb.pdb_portname[2]) << 40) |
1768		    (((uint64_t)pdb.pdb_portname[3]) << 32) |
1769		    (((uint64_t)pdb.pdb_portname[4]) << 24) |
1770		    (((uint64_t)pdb.pdb_portname[5]) << 16) |
1771		    (((uint64_t)pdb.pdb_portname[6]) <<  8) |
1772		    (((uint64_t)pdb.pdb_portname[7]));
1773		lp->roles =
1774		    (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
1775		lp->portid = BITS2WORD(pdb.pdb_portid_bits);
1776		lp->loopid = pdb.pdb_loopid;
1777		lp->loggedin = lp->valid = 1;
1778		fcp->isp_onfabric = 1;
1779		(void) isp_async(isp, ISPASYNC_PROMENADE, &loopid);
1780		isp_register_fc4_type(isp);
1781	} else {
1782not_on_fabric:
1783		fcp->isp_onfabric = 0;
1784		fcp->portdb[FL_PORT_ID].valid = 0;
1785	}
1786
1787	fcp->isp_gbspeed = 1;
1788	if (IS_23XX(isp)) {
1789		mbs.param[0] = MBOX_GET_SET_DATA_RATE;
1790		mbs.param[1] = MBGSD_GET_RATE;
1791		/* mbs.param[2] undefined if we're just getting rate */
1792		isp_mboxcmd(isp, &mbs, MBLOGALL);
1793		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1794			if (mbs.param[1] == MBGSD_TWOGB) {
1795				isp_prt(isp, ISP_LOGINFO, "2Gb link speed/s");
1796				fcp->isp_gbspeed = 2;
1797			}
1798		}
1799	}
1800
1801	isp_prt(isp, ISP_LOGCONFIG, topology, fcp->isp_loopid,
1802	    fcp->isp_portid, fcp->isp_loopstate, toponames[fcp->isp_topo]);
1803
1804	/*
1805	 * Announce ourselves, too. This involves synthesizing an entry.
1806	 */
1807	if (fcp->isp_iid_set == 0) {
1808		fcp->isp_iid_set = 1;
1809		fcp->isp_iid = fcp->isp_loopid;
1810		lp = &fcp->portdb[fcp->isp_iid];
1811	} else {
1812		lp = &fcp->portdb[fcp->isp_iid];
1813		if (fcp->isp_portid != lp->portid ||
1814		    fcp->isp_loopid != lp->loopid ||
1815		    fcp->isp_nodewwn != ISP_NODEWWN(isp) ||
1816		    fcp->isp_portwwn != ISP_PORTWWN(isp)) {
1817			lp->valid = 0;
1818			count = fcp->isp_iid;
1819			(void) isp_async(isp, ISPASYNC_PROMENADE, &count);
1820		}
1821	}
1822	lp->loopid = fcp->isp_loopid;
1823	lp->portid = fcp->isp_portid;
1824	lp->node_wwn = ISP_NODEWWN(isp);
1825	lp->port_wwn = ISP_PORTWWN(isp);
1826	switch (isp->isp_role) {
1827	case ISP_ROLE_NONE:
1828		lp->roles = 0;
1829		break;
1830	case ISP_ROLE_TARGET:
1831		lp->roles = SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT;
1832		break;
1833	case ISP_ROLE_INITIATOR:
1834		lp->roles = SVC3_INI_ROLE >> SVC3_ROLE_SHIFT;
1835		break;
1836	case ISP_ROLE_BOTH:
1837		lp->roles = (SVC3_INI_ROLE|SVC3_TGT_ROLE) >> SVC3_ROLE_SHIFT;
1838		break;
1839	}
1840	lp->loggedin = lp->valid = 1;
1841	count = fcp->isp_iid;
1842	(void) isp_async(isp, ISPASYNC_PROMENADE, &count);
1843	return (0);
1844}
1845
1846static char *
1847isp2100_fw_statename(int state)
1848{
1849	switch(state) {
1850	case FW_CONFIG_WAIT:	return "Config Wait";
1851	case FW_WAIT_AL_PA:	return "Waiting for AL_PA";
1852	case FW_WAIT_LOGIN:	return "Wait Login";
1853	case FW_READY:		return "Ready";
1854	case FW_LOSS_OF_SYNC:	return "Loss Of Sync";
1855	case FW_ERROR:		return "Error";
1856	case FW_REINIT:		return "Re-Init";
1857	case FW_NON_PART:	return "Nonparticipating";
1858	default:		return "?????";
1859	}
1860}
1861
1862/*
1863 * Synchronize our soft copy of the port database with what the f/w thinks
1864 * (with a view toward possibly for a specific target....)
1865 */
1866
1867static int
1868isp_pdb_sync(ispsoftc_t *isp)
1869{
1870	struct lportdb *lp;
1871	fcparam *fcp = isp->isp_param;
1872	isp_pdb_t pdb;
1873	int loopid, base, lim;
1874
1875	/*
1876	 * Make sure we're okay for doing this right now.
1877	 */
1878	if (fcp->isp_loopstate != LOOP_PDB_RCVD &&
1879	    fcp->isp_loopstate != LOOP_FSCAN_DONE &&
1880	    fcp->isp_loopstate != LOOP_LSCAN_DONE) {
1881		return (-1);
1882	}
1883
1884	if (fcp->isp_topo == TOPO_FL_PORT || fcp->isp_topo == TOPO_NL_PORT ||
1885	    fcp->isp_topo == TOPO_N_PORT) {
1886		if (fcp->isp_loopstate < LOOP_LSCAN_DONE) {
1887			if (isp_scan_loop(isp) != 0) {
1888				return (-1);
1889			}
1890		}
1891	}
1892	fcp->isp_loopstate = LOOP_SYNCING_PDB;
1893
1894	/*
1895	 * If we get this far, we've settled our differences with the f/w
1896	 * (for local loop device) and we can say that the loop state is ready.
1897	 */
1898
1899	if (fcp->isp_topo == TOPO_NL_PORT) {
1900		fcp->loop_seen_once = 1;
1901		fcp->isp_loopstate = LOOP_READY;
1902		return (0);
1903	}
1904
1905	/*
1906	 * Find all Fabric Entities that didn't make it from one scan to the
1907	 * next and let the world know they went away. Scan the whole database.
1908	 */
1909	for (lp = &fcp->portdb[0]; lp < &fcp->portdb[MAX_FC_TARG]; lp++) {
1910		if (lp->was_fabric_dev && lp->fabric_dev == 0) {
1911			loopid = lp - fcp->portdb;
1912			lp->valid = 0;	/* should already be set */
1913			(void) isp_async(isp, ISPASYNC_PROMENADE, &loopid);
1914			MEMZERO((void *) lp, sizeof (*lp));
1915			continue;
1916		}
1917		lp->was_fabric_dev = lp->fabric_dev;
1918	}
1919
1920	if (fcp->isp_topo == TOPO_FL_PORT)
1921		base = FC_SNS_ID+1;
1922	else
1923		base = 0;
1924
1925	if (fcp->isp_topo == TOPO_N_PORT)
1926		lim = 1;
1927	else
1928		lim = MAX_FC_TARG;
1929
1930	/*
1931	 * Now log in any fabric devices that the outer layer has
1932	 * left for us to see. This seems the most sane policy
1933	 * for the moment.
1934	 */
1935	for (lp = &fcp->portdb[base]; lp < &fcp->portdb[lim]; lp++) {
1936		uint32_t portid;
1937		mbreg_t mbs;
1938
1939		loopid = lp - fcp->portdb;
1940		if (loopid >= FL_PORT_ID && loopid <= FC_SNS_ID) {
1941			continue;
1942		}
1943
1944		/*
1945		 * Anything here?
1946		 */
1947		if (lp->port_wwn == 0) {
1948			continue;
1949		}
1950
1951		/*
1952		 * Don't try to log into yourself.
1953		 */
1954		if ((portid = lp->portid) == fcp->isp_portid) {
1955			continue;
1956		}
1957
1958
1959		/*
1960		 * If we'd been logged in- see if we still are and we haven't
1961		 * changed. If so, no need to log ourselves out, etc..
1962		 *
1963		 * Unfortunately, our charming Qlogic f/w has decided to
1964		 * return a valid port database entry for a fabric device
1965		 * that has, in fact, gone away. And it hangs trying to
1966		 * log it out.
1967		 */
1968		if (lp->loggedin && lp->force_logout == 0 &&
1969		    isp_getpdb(isp, lp->loopid, &pdb) == 0) {
1970			int nrole;
1971			uint64_t nwwnn, nwwpn;
1972			nwwnn =
1973			    (((uint64_t)pdb.pdb_nodename[0]) << 56) |
1974			    (((uint64_t)pdb.pdb_nodename[1]) << 48) |
1975			    (((uint64_t)pdb.pdb_nodename[2]) << 40) |
1976			    (((uint64_t)pdb.pdb_nodename[3]) << 32) |
1977			    (((uint64_t)pdb.pdb_nodename[4]) << 24) |
1978			    (((uint64_t)pdb.pdb_nodename[5]) << 16) |
1979			    (((uint64_t)pdb.pdb_nodename[6]) <<  8) |
1980			    (((uint64_t)pdb.pdb_nodename[7]));
1981			nwwpn =
1982			    (((uint64_t)pdb.pdb_portname[0]) << 56) |
1983			    (((uint64_t)pdb.pdb_portname[1]) << 48) |
1984			    (((uint64_t)pdb.pdb_portname[2]) << 40) |
1985			    (((uint64_t)pdb.pdb_portname[3]) << 32) |
1986			    (((uint64_t)pdb.pdb_portname[4]) << 24) |
1987			    (((uint64_t)pdb.pdb_portname[5]) << 16) |
1988			    (((uint64_t)pdb.pdb_portname[6]) <<  8) |
1989			    (((uint64_t)pdb.pdb_portname[7]));
1990			nrole = (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >>
1991			    SVC3_ROLE_SHIFT;
1992			if (pdb.pdb_loopid == lp->loopid && lp->portid ==
1993			    (uint32_t) BITS2WORD(pdb.pdb_portid_bits) &&
1994			    nwwnn == lp->node_wwn && nwwpn == lp->port_wwn &&
1995			    lp->roles == nrole && lp->force_logout == 0) {
1996				lp->loggedin = lp->valid = 1;
1997				isp_prt(isp, ISP_LOGCONFIG, lretained,
1998				    (int) (lp - fcp->portdb),
1999				    (int) lp->loopid, lp->portid);
2000				continue;
2001			}
2002		}
2003
2004		if (fcp->isp_fwstate != FW_READY ||
2005		    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
2006			return (-1);
2007		}
2008
2009		/*
2010		 * Force a logout if we were logged in.
2011		 */
2012		if (lp->loggedin) {
2013			if (lp->force_logout ||
2014			    isp_getpdb(isp, lp->loopid, &pdb) == 0) {
2015				MEMZERO(&mbs, sizeof (mbs));
2016				mbs.param[0] = MBOX_FABRIC_LOGOUT;
2017				if (IS_2KLOGIN(isp)) {
2018					mbs.param[1] = lp->loopid;
2019					mbs.obits |= (1 << 10);
2020				} else {
2021					mbs.param[1] = lp->loopid << 8;
2022				}
2023				mbs.param[2] = 0;
2024				mbs.param[3] = 0;
2025				isp_mboxcmd(isp, &mbs, MBLOGNONE);
2026				isp_prt(isp, ISP_LOGINFO, plogout,
2027				    (int) (lp - fcp->portdb), lp->loopid,
2028				    lp->portid);
2029			}
2030			lp->force_logout = lp->loggedin = 0;
2031			if (fcp->isp_fwstate != FW_READY ||
2032			    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
2033				return (-1);
2034			}
2035		}
2036
2037		/*
2038		 * And log in....
2039		 */
2040		loopid = lp - fcp->portdb;
2041		lp->loopid = FL_PORT_ID;
2042		do {
2043			MEMZERO(&mbs, sizeof (mbs));
2044			mbs.param[0] = MBOX_FABRIC_LOGIN;
2045			if (IS_2KLOGIN(isp)) {
2046				mbs.param[1] = loopid;
2047				mbs.obits |= (1 << 10);
2048			} else {
2049				mbs.param[1] = loopid << 8;
2050			}
2051			mbs.param[2] = portid >> 16;
2052			mbs.param[3] = portid & 0xffff;
2053			isp_mboxcmd(isp, &mbs, MBLOGALL & ~(MBOX_LOOP_ID_USED |
2054			    MBOX_PORT_ID_USED | MBOX_COMMAND_ERROR));
2055			if (fcp->isp_fwstate != FW_READY ||
2056			    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
2057				return (-1);
2058			}
2059			switch (mbs.param[0]) {
2060			case MBOX_LOOP_ID_USED:
2061				/*
2062				 * Try the next available loop id.
2063				 */
2064				loopid++;
2065				break;
2066			case MBOX_PORT_ID_USED:
2067				/*
2068				 * This port is already logged in.
2069				 * Snaffle the loop id it's using if it's
2070				 * nonzero, otherwise we're hosed.
2071				 */
2072				if (mbs.param[1] != 0) {
2073					loopid = mbs.param[1];
2074					if (loopid >= MAX_FC_TARG) {
2075						loopid = MAX_FC_TARG;
2076						break;
2077					}
2078					isp_prt(isp, ISP_LOGINFO, retained,
2079					    loopid, (int) (lp - fcp->portdb),
2080					    lp->portid);
2081				} else {
2082					loopid = MAX_FC_TARG;
2083					break;
2084				}
2085				/* FALLTHROUGH */
2086			case MBOX_COMMAND_COMPLETE:
2087				lp->loggedin = 1;
2088				lp->loopid = loopid;
2089				break;
2090			case MBOX_COMMAND_ERROR:
2091				isp_prt(isp, ISP_LOGINFO, plogierr,
2092				    portid, mbs.param[1]);
2093				/* FALLTHROUGH */
2094			case MBOX_ALL_IDS_USED: /* We're outta IDs */
2095			default:
2096				loopid = MAX_FC_TARG;
2097				break;
2098			}
2099		} while (lp->loopid == FL_PORT_ID && loopid < MAX_FC_TARG);
2100
2101		/*
2102		 * If we get here and we haven't set a Loop ID,
2103		 * we failed to log into this device.
2104		 */
2105
2106		if (lp->loopid == FL_PORT_ID) {
2107			lp->loopid = 0;
2108			continue;
2109		}
2110
2111		/*
2112		 * Make sure we can get the approriate port information.
2113		 */
2114		if (isp_getpdb(isp, lp->loopid, &pdb) != 0) {
2115			isp_prt(isp, ISP_LOGWARN, nopdb, lp->portid);
2116			goto dump_em;
2117		}
2118
2119		if (fcp->isp_fwstate != FW_READY ||
2120		    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
2121			return (-1);
2122		}
2123
2124		if (pdb.pdb_loopid != lp->loopid) {
2125			isp_prt(isp, ISP_LOGWARN, pdbmfail1,
2126			    lp->portid, pdb.pdb_loopid);
2127			goto dump_em;
2128		}
2129
2130		if (lp->portid != (uint32_t) BITS2WORD(pdb.pdb_portid_bits)) {
2131			isp_prt(isp, ISP_LOGWARN, pdbmfail2,
2132			    lp->portid, BITS2WORD(pdb.pdb_portid_bits));
2133			goto dump_em;
2134		}
2135
2136		lp->roles =
2137		    (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
2138		lp->node_wwn =
2139		    (((uint64_t)pdb.pdb_nodename[0]) << 56) |
2140		    (((uint64_t)pdb.pdb_nodename[1]) << 48) |
2141		    (((uint64_t)pdb.pdb_nodename[2]) << 40) |
2142		    (((uint64_t)pdb.pdb_nodename[3]) << 32) |
2143		    (((uint64_t)pdb.pdb_nodename[4]) << 24) |
2144		    (((uint64_t)pdb.pdb_nodename[5]) << 16) |
2145		    (((uint64_t)pdb.pdb_nodename[6]) <<  8) |
2146		    (((uint64_t)pdb.pdb_nodename[7]));
2147		lp->port_wwn =
2148		    (((uint64_t)pdb.pdb_portname[0]) << 56) |
2149		    (((uint64_t)pdb.pdb_portname[1]) << 48) |
2150		    (((uint64_t)pdb.pdb_portname[2]) << 40) |
2151		    (((uint64_t)pdb.pdb_portname[3]) << 32) |
2152		    (((uint64_t)pdb.pdb_portname[4]) << 24) |
2153		    (((uint64_t)pdb.pdb_portname[5]) << 16) |
2154		    (((uint64_t)pdb.pdb_portname[6]) <<  8) |
2155		    (((uint64_t)pdb.pdb_portname[7]));
2156		/*
2157		 * Check to make sure this all makes sense.
2158		 */
2159		if (lp->node_wwn && lp->port_wwn) {
2160			lp->valid = 1;
2161			loopid = lp - fcp->portdb;
2162			(void) isp_async(isp, ISPASYNC_PROMENADE, &loopid);
2163			continue;
2164		}
2165dump_em:
2166		lp->valid = 0;
2167		isp_prt(isp, ISP_LOGINFO,
2168		    ldumped, loopid, lp->loopid, lp->portid);
2169		MEMZERO(&mbs, sizeof (mbs));
2170		mbs.param[0] = MBOX_FABRIC_LOGOUT;
2171		if (IS_2KLOGIN(isp)) {
2172			mbs.param[1] = lp->loopid;
2173			mbs.obits |= (1 << 10);
2174		} else {
2175			mbs.param[1] = lp->loopid << 8;
2176		}
2177		isp_mboxcmd(isp, &mbs, MBLOGNONE);
2178		if (fcp->isp_fwstate != FW_READY ||
2179		    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
2180			return (-1);
2181		}
2182	}
2183	/*
2184	 * If we get here, we've for sure seen not only a valid loop
2185	 * but know what is or isn't on it, so mark this for usage
2186	 * in isp_start.
2187	 */
2188	fcp->loop_seen_once = 1;
2189	fcp->isp_loopstate = LOOP_READY;
2190	return (0);
2191}
2192
2193static int
2194isp_scan_loop(ispsoftc_t *isp)
2195{
2196	struct lportdb *lp;
2197	fcparam *fcp = isp->isp_param;
2198	isp_pdb_t pdb;
2199	int loopid, lim, hival;
2200
2201	switch (fcp->isp_topo) {
2202	case TOPO_NL_PORT:
2203		hival = FL_PORT_ID;
2204		break;
2205	case TOPO_N_PORT:
2206		hival = 2;
2207		break;
2208	case TOPO_FL_PORT:
2209		hival = FC_PORT_ID;
2210		break;
2211	default:
2212		fcp->isp_loopstate = LOOP_LSCAN_DONE;
2213		return (0);
2214	}
2215	fcp->isp_loopstate = LOOP_SCANNING_LOOP;
2216
2217	/*
2218	 * make sure the temp port database is clean...
2219	 */
2220	MEMZERO((void *)fcp->tport, sizeof (fcp->tport));
2221
2222	/*
2223	 * Run through the local loop ports and get port database info
2224	 * for each loop ID.
2225	 *
2226	 * There's a somewhat unexplained situation where the f/w passes back
2227	 * the wrong database entity- if that happens, just restart (up to
2228	 * FL_PORT_ID times).
2229	 */
2230	for (lim = loopid = 0; loopid < hival; loopid++) {
2231		lp = &fcp->tport[loopid];
2232
2233		/*
2234		 * Don't even try for ourselves...
2235	 	 */
2236		if (loopid == fcp->isp_loopid)
2237			continue;
2238
2239		lp->node_wwn = isp_get_portname(isp, loopid, 1);
2240		if (fcp->isp_loopstate < LOOP_SCANNING_LOOP)
2241			return (-1);
2242		if (lp->node_wwn == 0)
2243			continue;
2244		lp->port_wwn = isp_get_portname(isp, loopid, 0);
2245		if (fcp->isp_loopstate < LOOP_SCANNING_LOOP)
2246			return (-1);
2247		if (lp->port_wwn == 0) {
2248			lp->node_wwn = 0;
2249			continue;
2250		}
2251
2252		/*
2253		 * Get an entry....
2254		 */
2255		if (isp_getpdb(isp, loopid, &pdb) != 0) {
2256			if (fcp->isp_loopstate < LOOP_SCANNING_LOOP)
2257				return (-1);
2258			continue;
2259		}
2260		if (fcp->isp_loopstate < LOOP_SCANNING_LOOP) {
2261			return (-1);
2262		}
2263
2264		/*
2265		 * If the returned database element doesn't match what we
2266		 * asked for, restart the process entirely (up to a point...).
2267		 */
2268		if (pdb.pdb_loopid != loopid) {
2269			loopid = 0;
2270			if (lim++ < hival) {
2271				continue;
2272			}
2273			isp_prt(isp, ISP_LOGWARN,
2274			    "giving up on synchronizing the port database");
2275			return (-1);
2276		}
2277
2278		/*
2279		 * Save the pertinent info locally.
2280		 */
2281		lp->node_wwn =
2282		    (((uint64_t)pdb.pdb_nodename[0]) << 56) |
2283		    (((uint64_t)pdb.pdb_nodename[1]) << 48) |
2284		    (((uint64_t)pdb.pdb_nodename[2]) << 40) |
2285		    (((uint64_t)pdb.pdb_nodename[3]) << 32) |
2286		    (((uint64_t)pdb.pdb_nodename[4]) << 24) |
2287		    (((uint64_t)pdb.pdb_nodename[5]) << 16) |
2288		    (((uint64_t)pdb.pdb_nodename[6]) <<  8) |
2289		    (((uint64_t)pdb.pdb_nodename[7]));
2290		lp->port_wwn =
2291		    (((uint64_t)pdb.pdb_portname[0]) << 56) |
2292		    (((uint64_t)pdb.pdb_portname[1]) << 48) |
2293		    (((uint64_t)pdb.pdb_portname[2]) << 40) |
2294		    (((uint64_t)pdb.pdb_portname[3]) << 32) |
2295		    (((uint64_t)pdb.pdb_portname[4]) << 24) |
2296		    (((uint64_t)pdb.pdb_portname[5]) << 16) |
2297		    (((uint64_t)pdb.pdb_portname[6]) <<  8) |
2298		    (((uint64_t)pdb.pdb_portname[7]));
2299		lp->roles =
2300		    (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
2301		lp->portid = BITS2WORD(pdb.pdb_portid_bits);
2302		lp->loopid = pdb.pdb_loopid;
2303	}
2304
2305	/*
2306	 * Mark all of the permanent local loop database entries as invalid
2307	 * (except our own entry).
2308	 */
2309	for (loopid = 0; loopid < hival; loopid++) {
2310		if (loopid == fcp->isp_iid) {
2311			fcp->portdb[loopid].valid = 1;
2312			fcp->portdb[loopid].loopid = fcp->isp_loopid;
2313			continue;
2314		}
2315		fcp->portdb[loopid].valid = 0;
2316	}
2317
2318	/*
2319	 * Now merge our local copy of the port database into our saved copy.
2320	 * Notify the outer layers of new devices arriving.
2321	 */
2322	for (loopid = 0; loopid < hival; loopid++) {
2323		int i;
2324
2325		/*
2326		 * If we don't have a non-zero Port WWN, we're not here.
2327		 */
2328		if (fcp->tport[loopid].port_wwn == 0) {
2329			continue;
2330		}
2331
2332		/*
2333		 * Skip ourselves.
2334		 */
2335		if (loopid == fcp->isp_iid) {
2336			continue;
2337		}
2338
2339		/*
2340		 * For the purposes of deciding whether this is the
2341		 * 'same' device or not, we only search for an identical
2342		 * Port WWN. Node WWNs may or may not be the same as
2343		 * the Port WWN, and there may be multiple different
2344		 * Port WWNs with the same Node WWN. It would be chaos
2345		 * to have multiple identical Port WWNs, so we don't
2346		 * allow that.
2347		 */
2348
2349		for (i = 0; i < hival; i++) {
2350			int j;
2351			if (fcp->portdb[i].port_wwn == 0)
2352				continue;
2353			if (fcp->portdb[i].port_wwn !=
2354			    fcp->tport[loopid].port_wwn)
2355				continue;
2356			/*
2357			 * We found this WWN elsewhere- it's changed
2358			 * loopids then. We don't change it's actual
2359			 * position in our cached port database- we
2360			 * just change the actual loop ID we'd use.
2361			 */
2362			if (fcp->portdb[i].loopid != loopid) {
2363				isp_prt(isp, ISP_LOGINFO, portshift, i,
2364				    fcp->portdb[i].loopid,
2365				    fcp->portdb[i].portid, loopid,
2366				    fcp->tport[loopid].portid);
2367			}
2368			fcp->portdb[i].portid = fcp->tport[loopid].portid;
2369			fcp->portdb[i].loopid = loopid;
2370			fcp->portdb[i].valid = 1;
2371			fcp->portdb[i].roles = fcp->tport[loopid].roles;
2372
2373			/*
2374			 * Now make sure this Port WWN doesn't exist elsewhere
2375			 * in the port database.
2376			 */
2377			for (j = i+1; j < hival; j++) {
2378				if (fcp->portdb[i].port_wwn !=
2379				    fcp->portdb[j].port_wwn) {
2380					continue;
2381				}
2382				isp_prt(isp, ISP_LOGWARN, portdup, j, i);
2383				/*
2384				 * Invalidate the 'old' *and* 'new' ones.
2385				 * This is really harsh and not quite right,
2386				 * but if this happens, we really don't know
2387				 * who is what at this point.
2388				 */
2389				fcp->portdb[i].valid = 0;
2390				fcp->portdb[j].valid = 0;
2391			}
2392			break;
2393		}
2394
2395		/*
2396		 * If we didn't traverse the entire port database,
2397		 * then we found (and remapped) an existing entry.
2398		 * No need to notify anyone- go for the next one.
2399		 */
2400		if (i < hival) {
2401			isp_prt(isp, ISP_LOGINFO, retained,
2402			    fcp->portdb[i].loopid, i, fcp->portdb[i].portid);
2403			continue;
2404		}
2405
2406		/*
2407		 * We've not found this Port WWN anywhere. It's a new entry.
2408		 * See if we can leave it where it is (with target == loopid).
2409		 */
2410		if (fcp->portdb[loopid].port_wwn != 0) {
2411			for (lim = 0; lim < hival; lim++) {
2412				if (fcp->portdb[lim].port_wwn == 0)
2413					break;
2414			}
2415			/* "Cannot Happen" */
2416			if (lim == hival) {
2417				isp_prt(isp, ISP_LOGWARN, "Remap Overflow");
2418				continue;
2419			}
2420			i = lim;
2421		} else {
2422			i = loopid;
2423		}
2424
2425		/*
2426		 * NB:	The actual loopid we use here is loopid- we may
2427		 *	in fact be at a completely different index (target).
2428		 */
2429		fcp->portdb[i].loopid = loopid;
2430		fcp->portdb[i].port_wwn = fcp->tport[loopid].port_wwn;
2431		fcp->portdb[i].node_wwn = fcp->tport[loopid].node_wwn;
2432		fcp->portdb[i].roles = fcp->tport[loopid].roles;
2433		fcp->portdb[i].portid = fcp->tport[loopid].portid;
2434		fcp->portdb[i].valid = 1;
2435
2436		/*
2437		 * Tell the outside world we've arrived.
2438		 */
2439		(void) isp_async(isp, ISPASYNC_PROMENADE, &i);
2440	}
2441
2442	/*
2443	 * Now find all previously used targets that are now invalid and
2444	 * notify the outer layers that they're gone.
2445	 */
2446	for (lp = &fcp->portdb[0]; lp < &fcp->portdb[hival]; lp++) {
2447		if (lp->valid || lp->port_wwn == 0) {
2448			continue;
2449		}
2450
2451		/*
2452		 * Tell the outside world we've gone
2453		 * away and erase our pdb entry.
2454		 *
2455		 */
2456		loopid = lp - fcp->portdb;
2457		(void) isp_async(isp, ISPASYNC_PROMENADE, &loopid);
2458		MEMZERO((void *) lp, sizeof (*lp));
2459	}
2460	fcp->isp_loopstate = LOOP_LSCAN_DONE;
2461	return (0);
2462}
2463
2464
2465static int
2466isp_fabric_mbox_cmd(ispsoftc_t *isp, mbreg_t *mbp)
2467{
2468	isp_mboxcmd(isp, mbp, MBLOGNONE);
2469	if (mbp->param[0] != MBOX_COMMAND_COMPLETE) {
2470		if (FCPARAM(isp)->isp_loopstate == LOOP_SCANNING_FABRIC) {
2471			FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
2472		}
2473		if (mbp->param[0] == MBOX_COMMAND_ERROR) {
2474			char tbuf[16];
2475			char *m;
2476			switch (mbp->param[1]) {
2477			case 1:
2478				m = "No Loop";
2479				break;
2480			case 2:
2481				m = "Failed to allocate IOCB buffer";
2482				break;
2483			case 3:
2484				m = "Failed to allocate XCB buffer";
2485				break;
2486			case 4:
2487				m = "timeout or transmit failed";
2488				break;
2489			case 5:
2490				m = "no fabric loop";
2491				break;
2492			case 6:
2493				m = "remote device not a target";
2494				break;
2495			default:
2496				SNPRINTF(tbuf, sizeof tbuf, "%x",
2497				    mbp->param[1]);
2498				m = tbuf;
2499				break;
2500			}
2501			isp_prt(isp, ISP_LOGERR, "SNS Failed- %s", m);
2502		}
2503		return (-1);
2504	}
2505
2506	if (FCPARAM(isp)->isp_fwstate != FW_READY ||
2507	    FCPARAM(isp)->isp_loopstate < LOOP_SCANNING_FABRIC) {
2508		return (-1);
2509	}
2510	return(0);
2511}
2512
2513#ifdef	ISP_USE_GA_NXT
2514static int
2515isp_scan_fabric(ispsoftc_t *isp, int ftype)
2516{
2517	fcparam *fcp = isp->isp_param;
2518	uint32_t portid, first_portid, last_portid;
2519	int hicap, last_port_same;
2520
2521	if (fcp->isp_onfabric == 0) {
2522		fcp->isp_loopstate = LOOP_FSCAN_DONE;
2523		return (0);
2524	}
2525
2526	FC_SCRATCH_ACQUIRE(isp);
2527
2528	/*
2529	 * Since Port IDs are 24 bits, we can check against having seen
2530	 * anything yet with this value.
2531	 */
2532	last_port_same = 0;
2533	last_portid = 0xffffffff;	/* not a port */
2534	first_portid = portid = fcp->isp_portid;
2535	fcp->isp_loopstate = LOOP_SCANNING_FABRIC;
2536
2537	for (hicap = 0; hicap < GA_NXT_MAX; hicap++) {
2538		mbreg_t mbs;
2539		sns_screq_t *rq;
2540		sns_ga_nxt_rsp_t *rs0, *rs1;
2541		struct lportdb lcl;
2542		uint8_t sc[SNS_GA_NXT_RESP_SIZE];
2543
2544		rq = (sns_screq_t *)sc;
2545		MEMZERO((void *) rq, SNS_GA_NXT_REQ_SIZE);
2546		rq->snscb_rblen = SNS_GA_NXT_RESP_SIZE >> 1;
2547		rq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+0x100);
2548		rq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+0x100);
2549		rq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+0x100);
2550		rq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+0x100);
2551		rq->snscb_sblen = 6;
2552		rq->snscb_data[0] = SNS_GA_NXT;
2553		rq->snscb_data[4] = portid & 0xffff;
2554		rq->snscb_data[5] = (portid >> 16) & 0xff;
2555		isp_put_sns_request(isp, rq, (sns_screq_t *) fcp->isp_scratch);
2556		MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GA_NXT_REQ_SIZE);
2557		MEMZERO(&mbs, sizeof (mbs));
2558		mbs.param[0] = MBOX_SEND_SNS;
2559		mbs.param[1] = SNS_GA_NXT_REQ_SIZE >> 1;
2560		mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2561		mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2562		/*
2563		 * Leave 4 and 5 alone
2564		 */
2565		mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2566		mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2567		if (isp_fabric_mbox_cmd(isp, &mbs)) {
2568			if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
2569				fcp->isp_loopstate = LOOP_PDB_RCVD;
2570			}
2571			FC_SCRATCH_RELEASE(isp);
2572			return (-1);
2573		}
2574		MEMORYBARRIER(isp, SYNC_SFORCPU, 0x100, SNS_GA_NXT_RESP_SIZE);
2575		rs1 = (sns_ga_nxt_rsp_t *) sc;
2576		rs0 = (sns_ga_nxt_rsp_t *) ((uint8_t *)fcp->isp_scratch+0x100);
2577		isp_get_ga_nxt_response(isp, rs0, rs1);
2578		if (rs1->snscb_cthdr.ct_response != FS_ACC) {
2579			int level;
2580			if (rs1->snscb_cthdr.ct_reason == 9 &&
2581			    rs1->snscb_cthdr.ct_explanation == 7)
2582				level = ISP_LOGDEBUG0;
2583			else
2584				level = ISP_LOGWARN;
2585			isp_prt(isp, level, swrej, "GA_NXT",
2586			    rs1->snscb_cthdr.ct_reason,
2587			    rs1->snscb_cthdr.ct_explanation, portid);
2588			FC_SCRATCH_RELEASE(isp);
2589			fcp->isp_loopstate = LOOP_FSCAN_DONE;
2590			return (0);
2591		}
2592		portid =
2593		    (((uint32_t) rs1->snscb_port_id[0]) << 16) |
2594		    (((uint32_t) rs1->snscb_port_id[1]) << 8) |
2595		    (((uint32_t) rs1->snscb_port_id[2]));
2596
2597		/*
2598		 * XXX: We should check to make sure that this entry
2599		 * XXX: supports the type(s) we are interested in.
2600		 */
2601		/*
2602		 * Okay, we now have information about a fabric object.
2603		 * If it is the type we're interested in, tell the outer layers
2604		 * about it. The outer layer needs to  know: Port ID, WWNN,
2605		 * WWPN, FC4 type, and port type.
2606		 *
2607		 * The lportdb structure is adequate for this.
2608		 */
2609		MEMZERO(&lcl, sizeof (lcl));
2610		lcl.port_type = rs1->snscb_port_type;
2611		lcl.fc4_type = ftype;
2612		lcl.portid = portid;
2613		lcl.node_wwn =
2614		    (((uint64_t)rs1->snscb_nodename[0]) << 56) |
2615		    (((uint64_t)rs1->snscb_nodename[1]) << 48) |
2616		    (((uint64_t)rs1->snscb_nodename[2]) << 40) |
2617		    (((uint64_t)rs1->snscb_nodename[3]) << 32) |
2618		    (((uint64_t)rs1->snscb_nodename[4]) << 24) |
2619		    (((uint64_t)rs1->snscb_nodename[5]) << 16) |
2620		    (((uint64_t)rs1->snscb_nodename[6]) <<  8) |
2621		    (((uint64_t)rs1->snscb_nodename[7]));
2622		lcl.port_wwn =
2623		    (((uint64_t)rs1->snscb_portname[0]) << 56) |
2624		    (((uint64_t)rs1->snscb_portname[1]) << 48) |
2625		    (((uint64_t)rs1->snscb_portname[2]) << 40) |
2626		    (((uint64_t)rs1->snscb_portname[3]) << 32) |
2627		    (((uint64_t)rs1->snscb_portname[4]) << 24) |
2628		    (((uint64_t)rs1->snscb_portname[5]) << 16) |
2629		    (((uint64_t)rs1->snscb_portname[6]) <<  8) |
2630		    (((uint64_t)rs1->snscb_portname[7]));
2631
2632		/*
2633		 * Does this fabric object support the type we want?
2634		 * If not, skip it.
2635		 */
2636		if (rs1->snscb_fc4_types[ftype >> 5] & (1 << (ftype & 0x1f))) {
2637			if (first_portid == portid) {
2638				lcl.last_fabric_dev = 1;
2639			} else {
2640				lcl.last_fabric_dev = 0;
2641			}
2642			(void) isp_async(isp, ISPASYNC_FABRIC_DEV, &lcl);
2643		} else {
2644			isp_prt(isp, ISP_LOGDEBUG0,
2645			    "PortID 0x%x doesn't support FC4 type 0x%x",
2646			    portid, ftype);
2647		}
2648		if (first_portid == portid) {
2649			fcp->isp_loopstate = LOOP_FSCAN_DONE;
2650			FC_SCRATCH_RELEASE(isp);
2651			return (0);
2652		}
2653		if (portid == last_portid) {
2654			if (last_port_same++ > 20) {
2655				isp_prt(isp, ISP_LOGWARN,
2656				    "tangled fabric database detected");
2657				break;
2658			}
2659		} else {
2660			last_port_same = 0 ;
2661			last_portid = portid;
2662		}
2663	}
2664	FC_SCRATCH_RELEASE(isp);
2665	if (hicap >= GA_NXT_MAX) {
2666		isp_prt(isp, ISP_LOGWARN, "fabric too big (> %d)", GA_NXT_MAX);
2667	}
2668	fcp->isp_loopstate = LOOP_FSCAN_DONE;
2669	return (0);
2670}
2671#else
2672#define	GIDLEN	((ISP2100_SCRLEN >> 1) + 16)
2673#define	NGENT	((GIDLEN - 16) >> 2)
2674
2675#define	IGPOFF	(ISP2100_SCRLEN - GIDLEN)
2676#define	GXOFF	(256)
2677
2678static int
2679isp_scan_fabric(ispsoftc_t *isp, int ftype)
2680{
2681	fcparam *fcp = FCPARAM(isp);
2682	mbreg_t mbs;
2683	int i;
2684	sns_gid_ft_req_t *rq;
2685	sns_gid_ft_rsp_t *rs0, *rs1;
2686
2687	if (fcp->isp_onfabric == 0) {
2688		fcp->isp_loopstate = LOOP_FSCAN_DONE;
2689		return (0);
2690	}
2691
2692	FC_SCRATCH_ACQUIRE(isp);
2693	fcp->isp_loopstate = LOOP_SCANNING_FABRIC;
2694
2695	rq = (sns_gid_ft_req_t *)fcp->tport;
2696	MEMZERO((void *) rq, SNS_GID_FT_REQ_SIZE);
2697	rq->snscb_rblen = GIDLEN >> 1;
2698	rq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+IGPOFF);
2699	rq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+IGPOFF);
2700	rq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+IGPOFF);
2701	rq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+IGPOFF);
2702	rq->snscb_sblen = 6;
2703	rq->snscb_cmd = SNS_GID_FT;
2704	rq->snscb_mword_div_2 = NGENT;
2705	rq->snscb_fc4_type = ftype;
2706	isp_put_gid_ft_request(isp, rq, (sns_gid_ft_req_t *) fcp->isp_scratch);
2707	MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GID_FT_REQ_SIZE);
2708	MEMZERO(&mbs, sizeof (mbs));
2709	mbs.param[0] = MBOX_SEND_SNS;
2710	mbs.param[1] = SNS_GID_FT_REQ_SIZE >> 1;
2711	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2712	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2713
2714	/*
2715	 * Leave 4 and 5 alone
2716	 */
2717	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2718	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2719	if (isp_fabric_mbox_cmd(isp, &mbs)) {
2720		if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
2721			fcp->isp_loopstate = LOOP_PDB_RCVD;
2722		}
2723		FC_SCRATCH_RELEASE(isp);
2724		return (-1);
2725	}
2726	if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2727		FC_SCRATCH_RELEASE(isp);
2728		return (-1);
2729	}
2730	MEMORYBARRIER(isp, SYNC_SFORCPU, IGPOFF, GIDLEN);
2731	rs1 = (sns_gid_ft_rsp_t *) fcp->tport;
2732	rs0 = (sns_gid_ft_rsp_t *) ((uint8_t *)fcp->isp_scratch+IGPOFF);
2733	isp_get_gid_ft_response(isp, rs0, rs1, NGENT);
2734	if (rs1->snscb_cthdr.ct_response != FS_ACC) {
2735		int level;
2736		if (rs1->snscb_cthdr.ct_reason == 9 &&
2737		    rs1->snscb_cthdr.ct_explanation == 7)
2738			level = ISP_LOGDEBUG0;
2739		else
2740			level = ISP_LOGWARN;
2741		isp_prt(isp, level, swrej, "GID_FT",
2742		    rs1->snscb_cthdr.ct_reason,
2743		    rs1->snscb_cthdr.ct_explanation, 0);
2744		FC_SCRATCH_RELEASE(isp);
2745		fcp->isp_loopstate = LOOP_FSCAN_DONE;
2746		return (0);
2747	}
2748
2749	/*
2750	 * Okay, we now have a list of Port IDs for this class of device.
2751	 * Go through the list and for each one get the WWPN/WWNN for it
2752	 * and tell the outer layers about it. The outer layer needs to
2753	 * know: Port ID, WWNN, WWPN, FC4 type, and (possibly) port type.
2754	 *
2755	 * The lportdb structure is adequate for this.
2756	 */
2757	i = -1;
2758	do {
2759		sns_gxn_id_req_t grqbuf, *gq = &grqbuf;
2760		sns_gxn_id_rsp_t *gs0, grsbuf, *gs1 = &grsbuf;
2761		struct lportdb lcl;
2762#if	0
2763		sns_gff_id_rsp_t *fs0, ffsbuf, *fs1 = &ffsbuf;
2764#endif
2765
2766		i++;
2767		MEMZERO(&lcl, sizeof (lcl));
2768		lcl.fc4_type = ftype;
2769		lcl.portid =
2770		    (((uint32_t) rs1->snscb_ports[i].portid[0]) << 16) |
2771		    (((uint32_t) rs1->snscb_ports[i].portid[1]) << 8) |
2772		    (((uint32_t) rs1->snscb_ports[i].portid[2]));
2773
2774		MEMZERO((void *) gq, sizeof (sns_gxn_id_req_t));
2775		gq->snscb_rblen = SNS_GXN_ID_RESP_SIZE >> 1;
2776		gq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+GXOFF);
2777		gq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+GXOFF);
2778		gq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+GXOFF);
2779		gq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+GXOFF);
2780		gq->snscb_sblen = 6;
2781		gq->snscb_cmd = SNS_GPN_ID;
2782		gq->snscb_portid = lcl.portid;
2783		isp_put_gxn_id_request(isp, gq,
2784		    (sns_gxn_id_req_t *) fcp->isp_scratch);
2785		MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GXN_ID_REQ_SIZE);
2786		MEMZERO(&mbs, sizeof (mbs));
2787		mbs.param[0] = MBOX_SEND_SNS;
2788		mbs.param[1] = SNS_GXN_ID_REQ_SIZE >> 1;
2789		mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2790		mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2791		/*
2792		 * Leave 4 and 5 alone
2793		 */
2794		mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2795		mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2796		if (isp_fabric_mbox_cmd(isp, &mbs)) {
2797			if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
2798				fcp->isp_loopstate = LOOP_PDB_RCVD;
2799			}
2800			FC_SCRATCH_RELEASE(isp);
2801			return (-1);
2802		}
2803		if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2804			FC_SCRATCH_RELEASE(isp);
2805			return (-1);
2806		}
2807		MEMORYBARRIER(isp, SYNC_SFORCPU, GXOFF, SNS_GXN_ID_RESP_SIZE);
2808		gs0 = (sns_gxn_id_rsp_t *) ((uint8_t *)fcp->isp_scratch+GXOFF);
2809		isp_get_gxn_id_response(isp, gs0, gs1);
2810		if (gs1->snscb_cthdr.ct_response != FS_ACC) {
2811			isp_prt(isp, ISP_LOGWARN, swrej, "GPN_ID",
2812			    gs1->snscb_cthdr.ct_reason,
2813			    gs1->snscb_cthdr.ct_explanation, lcl.portid);
2814			if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2815				FC_SCRATCH_RELEASE(isp);
2816				return (-1);
2817			}
2818			continue;
2819		}
2820		lcl.port_wwn =
2821		    (((uint64_t)gs1->snscb_wwn[0]) << 56) |
2822		    (((uint64_t)gs1->snscb_wwn[1]) << 48) |
2823		    (((uint64_t)gs1->snscb_wwn[2]) << 40) |
2824		    (((uint64_t)gs1->snscb_wwn[3]) << 32) |
2825		    (((uint64_t)gs1->snscb_wwn[4]) << 24) |
2826		    (((uint64_t)gs1->snscb_wwn[5]) << 16) |
2827		    (((uint64_t)gs1->snscb_wwn[6]) <<  8) |
2828		    (((uint64_t)gs1->snscb_wwn[7]));
2829
2830		MEMZERO((void *) gq, sizeof (sns_gxn_id_req_t));
2831		gq->snscb_rblen = SNS_GXN_ID_RESP_SIZE >> 1;
2832		gq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+GXOFF);
2833		gq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+GXOFF);
2834		gq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+GXOFF);
2835		gq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+GXOFF);
2836		gq->snscb_sblen = 6;
2837		gq->snscb_cmd = SNS_GNN_ID;
2838		gq->snscb_portid = lcl.portid;
2839		isp_put_gxn_id_request(isp, gq,
2840		    (sns_gxn_id_req_t *) fcp->isp_scratch);
2841		MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GXN_ID_REQ_SIZE);
2842		MEMZERO(&mbs, sizeof (mbs));
2843		mbs.param[0] = MBOX_SEND_SNS;
2844		mbs.param[1] = SNS_GXN_ID_REQ_SIZE >> 1;
2845		mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2846		mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2847		/*
2848		 * Leave 4 and 5 alone
2849		 */
2850		mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2851		mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2852		if (isp_fabric_mbox_cmd(isp, &mbs)) {
2853			if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
2854				fcp->isp_loopstate = LOOP_PDB_RCVD;
2855			}
2856			FC_SCRATCH_RELEASE(isp);
2857			return (-1);
2858		}
2859		if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2860			FC_SCRATCH_RELEASE(isp);
2861			return (-1);
2862		}
2863		MEMORYBARRIER(isp, SYNC_SFORCPU, GXOFF, SNS_GXN_ID_RESP_SIZE);
2864		gs0 = (sns_gxn_id_rsp_t *) ((uint8_t *)fcp->isp_scratch+GXOFF);
2865		isp_get_gxn_id_response(isp, gs0, gs1);
2866		if (gs1->snscb_cthdr.ct_response != FS_ACC) {
2867			isp_prt(isp, ISP_LOGWARN, swrej, "GNN_ID",
2868			    gs1->snscb_cthdr.ct_reason,
2869			    gs1->snscb_cthdr.ct_explanation, lcl.portid);
2870			if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2871				FC_SCRATCH_RELEASE(isp);
2872				return (-1);
2873			}
2874			continue;
2875		}
2876		lcl.node_wwn =
2877		    (((uint64_t)gs1->snscb_wwn[0]) << 56) |
2878		    (((uint64_t)gs1->snscb_wwn[1]) << 48) |
2879		    (((uint64_t)gs1->snscb_wwn[2]) << 40) |
2880		    (((uint64_t)gs1->snscb_wwn[3]) << 32) |
2881		    (((uint64_t)gs1->snscb_wwn[4]) << 24) |
2882		    (((uint64_t)gs1->snscb_wwn[5]) << 16) |
2883		    (((uint64_t)gs1->snscb_wwn[6]) <<  8) |
2884		    (((uint64_t)gs1->snscb_wwn[7]));
2885
2886		/*
2887		 * The QLogic f/w is bouncing this with a parameter error.
2888		 */
2889#if	0
2890		/*
2891		 * Try and get FC4 Features (FC-GS-3 only).
2892		 * We can use the sns_gxn_id_req_t for this request.
2893		 */
2894		MEMZERO((void *) gq, sizeof (sns_gxn_id_req_t));
2895		gq->snscb_rblen = SNS_GFF_ID_RESP_SIZE >> 1;
2896		gq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+GXOFF);
2897		gq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+GXOFF);
2898		gq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+GXOFF);
2899		gq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+GXOFF);
2900		gq->snscb_sblen = 6;
2901		gq->snscb_cmd = SNS_GFF_ID;
2902		gq->snscb_portid = lcl.portid;
2903		isp_put_gxn_id_request(isp, gq,
2904		    (sns_gxn_id_req_t *) fcp->isp_scratch);
2905		MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GXN_ID_REQ_SIZE);
2906		MEMZERO(&mbs, sizeof (mbs));
2907		mbs.param[0] = MBOX_SEND_SNS;
2908		mbs.param[1] = SNS_GXN_ID_REQ_SIZE >> 1;
2909		mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2910		mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2911		/*
2912		 * Leave 4 and 5 alone
2913		 */
2914		mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2915		mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2916		if (isp_fabric_mbox_cmd(isp, &mbs)) {
2917			if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
2918				fcp->isp_loopstate = LOOP_PDB_RCVD;
2919			}
2920			FC_SCRATCH_RELEASE(isp);
2921			return (-1);
2922		}
2923		if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2924			FC_SCRATCH_RELEASE(isp);
2925			return (-1);
2926		}
2927		MEMORYBARRIER(isp, SYNC_SFORCPU, GXOFF, SNS_GFF_ID_RESP_SIZE);
2928		fs0 = (sns_gff_id_rsp_t *) ((uint8_t *)fcp->isp_scratch+GXOFF);
2929		isp_get_gff_id_response(isp, fs0, fs1);
2930		if (fs1->snscb_cthdr.ct_response != FS_ACC) {
2931			isp_prt(isp, /* ISP_LOGDEBUG0 */ ISP_LOGWARN,
2932			    swrej, "GFF_ID",
2933			    fs1->snscb_cthdr.ct_reason,
2934			    fs1->snscb_cthdr.ct_explanation, lcl.portid);
2935			if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2936				FC_SCRATCH_RELEASE(isp);
2937				return (-1);
2938			}
2939		} else {
2940			int index = (ftype >> 3);
2941			int bshft = (ftype & 0x7) * 4;
2942			int fc4_fval =
2943			    (fs1->snscb_fc4_features[index] >> bshft) & 0xf;
2944			if (fc4_fval & 0x1) {
2945				lcl.roles |=
2946				    (SVC3_INI_ROLE >> SVC3_ROLE_SHIFT);
2947			}
2948			if (fc4_fval & 0x2) {
2949				lcl.roles |=
2950				    (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT);
2951			}
2952		}
2953#endif
2954
2955		/*
2956		 * If we really want to know what kind of port type this is,
2957		 * we have to run another CT command. Otherwise, we'll leave
2958		 * it as undefined.
2959		 *
2960		lcl.port_type = 0;
2961		 */
2962		if (rs1->snscb_ports[i].control & 0x80) {
2963			lcl.last_fabric_dev = 1;
2964		} else {
2965			lcl.last_fabric_dev = 0;
2966		}
2967		(void) isp_async(isp, ISPASYNC_FABRIC_DEV, &lcl);
2968
2969	} while ((rs1->snscb_ports[i].control & 0x80) == 0 && i < NGENT-1);
2970
2971	/*
2972	 * If we're not at the last entry, our list isn't big enough.
2973	 */
2974	if ((rs1->snscb_ports[i].control & 0x80) == 0) {
2975		isp_prt(isp, ISP_LOGWARN, "fabric too big for scratch area");
2976	}
2977
2978	FC_SCRATCH_RELEASE(isp);
2979	fcp->isp_loopstate = LOOP_FSCAN_DONE;
2980	return (0);
2981}
2982#endif
2983
2984static void
2985isp_register_fc4_type(ispsoftc_t *isp)
2986{
2987	fcparam *fcp = isp->isp_param;
2988	uint8_t local[SNS_RFT_ID_REQ_SIZE];
2989	sns_screq_t *reqp = (sns_screq_t *) local;
2990	mbreg_t mbs;
2991
2992	MEMZERO((void *) reqp, SNS_RFT_ID_REQ_SIZE);
2993	reqp->snscb_rblen = SNS_RFT_ID_RESP_SIZE >> 1;
2994	reqp->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma + 0x100);
2995	reqp->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma + 0x100);
2996	reqp->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma + 0x100);
2997	reqp->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma + 0x100);
2998	reqp->snscb_sblen = 22;
2999	reqp->snscb_data[0] = SNS_RFT_ID;
3000	reqp->snscb_data[4] = fcp->isp_portid & 0xffff;
3001	reqp->snscb_data[5] = (fcp->isp_portid >> 16) & 0xff;
3002	reqp->snscb_data[6] = (1 << FC4_SCSI);
3003#if	0
3004	reqp->snscb_data[6] |= (1 << FC4_IP);	/* ISO/IEC 8802-2 LLC/SNAP */
3005#endif
3006	FC_SCRATCH_ACQUIRE(isp);
3007	isp_put_sns_request(isp, reqp, (sns_screq_t *) fcp->isp_scratch);
3008	MEMZERO(&mbs, sizeof (mbs));
3009	mbs.param[0] = MBOX_SEND_SNS;
3010	mbs.param[1] = SNS_RFT_ID_REQ_SIZE >> 1;
3011	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
3012	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
3013	/*
3014	 * Leave 4 and 5 alone
3015	 */
3016	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
3017	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
3018	isp_mboxcmd(isp, &mbs, MBLOGALL);
3019	FC_SCRATCH_RELEASE(isp);
3020	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
3021		isp_prt(isp, ISP_LOGDEBUG0, "Register FC4 types succeeded");
3022	}
3023}
3024
3025/*
3026 * Start a command. Locking is assumed done in the caller.
3027 */
3028
3029int
3030isp_start(XS_T *xs)
3031{
3032	ispsoftc_t *isp;
3033	uint16_t nxti, optr, handle;
3034	uint8_t local[QENTRY_LEN];
3035	ispreq_t *reqp, *qep;
3036	int target, i;
3037
3038	XS_INITERR(xs);
3039	isp = XS_ISP(xs);
3040
3041	/*
3042	 * Check to make sure we're supporting initiator role.
3043	 */
3044	if ((isp->isp_role & ISP_ROLE_INITIATOR) == 0) {
3045		XS_SETERR(xs, HBA_SELTIMEOUT);
3046		return (CMD_COMPLETE);
3047	}
3048
3049	/*
3050	 * Now make sure we're running.
3051	 */
3052
3053	if (isp->isp_state != ISP_RUNSTATE) {
3054		isp_prt(isp, ISP_LOGERR, "Adapter not at RUNSTATE");
3055		XS_SETERR(xs, HBA_BOTCH);
3056		return (CMD_COMPLETE);
3057	}
3058
3059	/*
3060	 * Check command CDB length, etc.. We really are limited to 16 bytes
3061	 * for Fibre Channel, but can do up to 44 bytes in parallel SCSI,
3062	 * but probably only if we're running fairly new firmware (we'll
3063	 * let the old f/w choke on an extended command queue entry).
3064	 */
3065
3066	if (XS_CDBLEN(xs) > (IS_FC(isp)? 16 : 44) || XS_CDBLEN(xs) == 0) {
3067		isp_prt(isp, ISP_LOGERR,
3068		    "unsupported cdb length (%d, CDB[0]=0x%x)",
3069		    XS_CDBLEN(xs), XS_CDBP(xs)[0] & 0xff);
3070		XS_SETERR(xs, HBA_BOTCH);
3071		return (CMD_COMPLETE);
3072	}
3073
3074	/*
3075	 * Check to see whether we have good firmware state still or
3076	 * need to refresh our port database for this target.
3077	 */
3078	target = XS_TGT(xs);
3079	if (IS_FC(isp)) {
3080		fcparam *fcp = isp->isp_param;
3081		struct lportdb *lp;
3082#ifdef	HANDLE_LOOPSTATE_IN_OUTER_LAYERS
3083		if (fcp->isp_fwstate != FW_READY ||
3084		    fcp->isp_loopstate != LOOP_READY) {
3085			return (CMD_RQLATER);
3086		}
3087
3088		/*
3089		 * If we're not on a Fabric, we can't have a target
3090		 * above FL_PORT_ID-1.
3091		 *
3092		 * If we're on a fabric and *not* connected as an F-port,
3093		 * we can't have a target less than FC_SNS_ID+1. This
3094		 * keeps us from having to sort out the difference between
3095		 * local public loop devices and those which we might get
3096		 * from a switch's database.
3097		 */
3098		if (fcp->isp_onfabric == 0) {
3099			if (target >= FL_PORT_ID) {
3100				XS_SETERR(xs, HBA_SELTIMEOUT);
3101				return (CMD_COMPLETE);
3102			}
3103		} else {
3104			if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
3105				XS_SETERR(xs, HBA_SELTIMEOUT);
3106				return (CMD_COMPLETE);
3107			}
3108			/*
3109			 * We used to exclude having local loop ports
3110			 * at the same time that we have fabric ports.
3111			 * That is, we used to exclude having ports
3112			 * at < FL_PORT_ID if we're FL-port.
3113			 *
3114			 * That's wrong. The only thing that could be
3115			 * dicey is if the switch you're connected to
3116			 * has these local loop ports appear on the
3117			 * fabric and we somehow attach them twice.
3118			 */
3119		}
3120#else
3121		/*
3122		 * Check for f/w being in ready state. If the f/w
3123		 * isn't in ready state, then we don't know our
3124		 * loop ID and the f/w hasn't completed logging
3125		 * into all targets on the loop. If this is the
3126		 * case, then bounce the command. We pretend this is
3127		 * a SELECTION TIMEOUT error if we've never gone to
3128		 * FW_READY state at all- in this case we may not
3129		 * be hooked to a loop at all and we shouldn't hang
3130		 * the machine for this. Otherwise, defer this command
3131		 * until later.
3132		 */
3133		if (fcp->isp_fwstate != FW_READY) {
3134			/*
3135			 * Give ourselves at most a 250ms delay.
3136			 */
3137			if (isp_fclink_test(isp, 250000)) {
3138				XS_SETERR(xs, HBA_SELTIMEOUT);
3139				if (fcp->loop_seen_once) {
3140					return (CMD_RQLATER);
3141				} else {
3142					return (CMD_COMPLETE);
3143				}
3144			}
3145		}
3146
3147		/*
3148		 * If we're not on a Fabric, we can't have a target
3149		 * above FL_PORT_ID-1.
3150		 *
3151		 * If we're on a fabric and *not* connected as an F-port,
3152		 * we can't have a target less than FC_SNS_ID+1. This
3153		 * keeps us from having to sort out the difference between
3154		 * local public loop devices and those which we might get
3155		 * from a switch's database.
3156		 */
3157		if (fcp->isp_onfabric == 0) {
3158			if (target >= FL_PORT_ID) {
3159				XS_SETERR(xs, HBA_SELTIMEOUT);
3160				return (CMD_COMPLETE);
3161			}
3162		} else {
3163			if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
3164				XS_SETERR(xs, HBA_SELTIMEOUT);
3165				return (CMD_COMPLETE);
3166			}
3167			if (fcp->isp_topo != TOPO_F_PORT &&
3168			    target < FL_PORT_ID) {
3169				XS_SETERR(xs, HBA_SELTIMEOUT);
3170				return (CMD_COMPLETE);
3171			}
3172		}
3173
3174		/*
3175		 * If our loop state is such that we haven't yet received
3176		 * a "Port Database Changed" notification (after a LIP or
3177		 * a Loop Reset or firmware initialization), then defer
3178		 * sending commands for a little while, but only if we've
3179		 * seen a valid loop at one point (otherwise we can get
3180		 * stuck at initialization time).
3181		 */
3182		if (fcp->isp_loopstate < LOOP_PDB_RCVD) {
3183			XS_SETERR(xs, HBA_SELTIMEOUT);
3184			if (fcp->loop_seen_once) {
3185				return (CMD_RQLATER);
3186			} else {
3187				return (CMD_COMPLETE);
3188			}
3189		}
3190
3191		/*
3192		 * If we're in the middle of loop or fabric scanning
3193		 * or merging the port databases, retry this command later.
3194		 */
3195		if (fcp->isp_loopstate == LOOP_SCANNING_FABRIC ||
3196		    fcp->isp_loopstate == LOOP_SCANNING_LOOP ||
3197		    fcp->isp_loopstate == LOOP_SYNCING_PDB) {
3198			return (CMD_RQLATER);
3199		}
3200
3201		/*
3202		 * If our loop state is now such that we've just now
3203		 * received a Port Database Change notification, then
3204		 * we have to go off and (re)scan the fabric. We back
3205		 * out and try again later if this doesn't work.
3206		 */
3207		if (fcp->isp_loopstate == LOOP_PDB_RCVD && fcp->isp_onfabric) {
3208			if (isp_scan_fabric(isp, FC4_SCSI)) {
3209				return (CMD_RQLATER);
3210			}
3211			if (fcp->isp_fwstate != FW_READY ||
3212			    fcp->isp_loopstate < LOOP_FSCAN_DONE) {
3213				return (CMD_RQLATER);
3214			}
3215		}
3216
3217		/*
3218		 * If our loop state is now such that we've just now
3219		 * received a Port Database Change notification, then
3220		 * we have to go off and (re)synchronize our port
3221		 * database.
3222		 */
3223		if (fcp->isp_loopstate < LOOP_READY) {
3224			if (isp_pdb_sync(isp)) {
3225				return (CMD_RQLATER);
3226			}
3227			if (fcp->isp_fwstate != FW_READY ||
3228			    fcp->isp_loopstate != LOOP_READY) {
3229				return (CMD_RQLATER);
3230			}
3231		}
3232
3233		/*
3234		 * XXX: Here's were we would cancel any loop_dead flag
3235		 * XXX: also cancel in dead_loop timeout that's running
3236		 */
3237#endif
3238
3239		/*
3240		 * Now check whether we should even think about pursuing this.
3241		 */
3242		lp = &fcp->portdb[target];
3243		if (lp->valid == 0) {
3244			XS_SETERR(xs, HBA_SELTIMEOUT);
3245			return (CMD_COMPLETE);
3246		}
3247		if ((lp->roles & (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT)) == 0) {
3248			isp_prt(isp, ISP_LOGDEBUG2,
3249			    "Target %d does not have target service", target);
3250			XS_SETERR(xs, HBA_SELTIMEOUT);
3251			return (CMD_COMPLETE);
3252		}
3253		/*
3254		 * Now turn target into what the actual Loop ID is.
3255		 */
3256		target = lp->loopid;
3257	}
3258
3259	/*
3260	 * Next check to see if any HBA or Device
3261	 * parameters need to be updated.
3262	 */
3263	if (isp->isp_update != 0) {
3264		isp_update(isp);
3265	}
3266
3267	if (isp_getrqentry(isp, &nxti, &optr, (void *)&qep)) {
3268		isp_prt(isp, ISP_LOGDEBUG0, "Request Queue Overflow");
3269		XS_SETERR(xs, HBA_BOTCH);
3270		return (CMD_EAGAIN);
3271	}
3272
3273	/*
3274	 * Now see if we need to synchronize the ISP with respect to anything.
3275	 * We do dual duty here (cough) for synchronizing for busses other
3276	 * than which we got here to send a command to.
3277	 */
3278	reqp = (ispreq_t *) local;
3279	if (isp->isp_sendmarker) {
3280		uint8_t n = (IS_DUALBUS(isp)? 2: 1);
3281		/*
3282		 * Check ports to send markers for...
3283		 */
3284		for (i = 0; i < n; i++) {
3285			if ((isp->isp_sendmarker & (1 << i)) == 0) {
3286				continue;
3287			}
3288			MEMZERO((void *) reqp, QENTRY_LEN);
3289			reqp->req_header.rqs_entry_count = 1;
3290			reqp->req_header.rqs_entry_type = RQSTYPE_MARKER;
3291			reqp->req_modifier = SYNC_ALL;
3292			reqp->req_target = i << 7;	/* insert bus number */
3293			isp_put_request(isp, reqp, qep);
3294			ISP_ADD_REQUEST(isp, nxti);
3295			isp->isp_sendmarker &= ~(1 << i);
3296			if (isp_getrqentry(isp, &nxti, &optr, (void *) &qep)) {
3297				isp_prt(isp, ISP_LOGDEBUG0,
3298				    "Request Queue Overflow+");
3299				XS_SETERR(xs, HBA_BOTCH);
3300				return (CMD_EAGAIN);
3301			}
3302		}
3303	}
3304
3305	MEMZERO((void *)reqp, QENTRY_LEN);
3306	reqp->req_header.rqs_entry_count = 1;
3307	if (IS_FC(isp)) {
3308		reqp->req_header.rqs_entry_type = RQSTYPE_T2RQS;
3309	} else {
3310		if (XS_CDBLEN(xs) > 12)
3311			reqp->req_header.rqs_entry_type = RQSTYPE_CMDONLY;
3312		else
3313			reqp->req_header.rqs_entry_type = RQSTYPE_REQUEST;
3314	}
3315	/* reqp->req_header.rqs_flags = 0; */
3316	/* reqp->req_header.rqs_seqno = 0; */
3317	if (IS_FC(isp)) {
3318		/*
3319		 * See comment in isp_intr
3320		 */
3321		/* XS_RESID(xs) = 0; */
3322
3323		/*
3324		 * Fibre Channel always requires some kind of tag.
3325		 * The Qlogic drivers seem be happy not to use a tag,
3326		 * but this breaks for some devices (IBM drives).
3327		 */
3328		if (XS_TAG_P(xs)) {
3329			((ispreqt2_t *)reqp)->req_flags = XS_TAG_TYPE(xs);
3330		} else {
3331			/*
3332			 * If we don't know what tag to use, use HEAD OF QUEUE
3333			 * for Request Sense or Simple.
3334			 */
3335			if (XS_CDBP(xs)[0] == 0x3)	/* REQUEST SENSE */
3336				((ispreqt2_t *)reqp)->req_flags = REQFLAG_HTAG;
3337			else
3338				((ispreqt2_t *)reqp)->req_flags = REQFLAG_STAG;
3339		}
3340	} else {
3341		sdparam *sdp = (sdparam *)isp->isp_param;
3342		sdp += XS_CHANNEL(xs);
3343		if ((sdp->isp_devparam[target].actv_flags & DPARM_TQING) &&
3344		    XS_TAG_P(xs)) {
3345			reqp->req_flags = XS_TAG_TYPE(xs);
3346		}
3347	}
3348	if (IS_SCSI(isp)) {
3349		reqp->req_target = target | (XS_CHANNEL(xs) << 7);
3350		reqp->req_lun_trn = XS_LUN(xs);
3351		reqp->req_cdblen = XS_CDBLEN(xs);
3352	} else if (IS_2KLOGIN(isp)) {
3353		((ispreqt2e_t *)reqp)->req_target = target;
3354		((ispreqt2e_t *)reqp)->req_scclun = XS_LUN(xs);
3355	} else if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) {
3356		((ispreqt2_t *)reqp)->req_target = target;
3357		((ispreqt2_t *)reqp)->req_scclun = XS_LUN(xs);
3358	} else {
3359		((ispreqt2_t *)reqp)->req_target = target;
3360		((ispreqt2_t *)reqp)->req_lun_trn = XS_LUN(xs);
3361	}
3362	MEMCPY(reqp->req_cdb, XS_CDBP(xs), XS_CDBLEN(xs));
3363
3364	reqp->req_time = XS_TIME(xs) / 1000;
3365	if (reqp->req_time == 0 && XS_TIME(xs)) {
3366		reqp->req_time = 1;
3367	}
3368
3369	if (isp_save_xs(isp, xs, &handle)) {
3370		isp_prt(isp, ISP_LOGDEBUG0, "out of xflist pointers");
3371		XS_SETERR(xs, HBA_BOTCH);
3372		return (CMD_EAGAIN);
3373	}
3374	reqp->req_handle = handle;
3375
3376	/*
3377	 * Set up DMA and/or do any bus swizzling of the request entry
3378	 * so that the Qlogic F/W understands what is being asked of it.
3379	 */
3380	i = ISP_DMASETUP(isp, xs, reqp, &nxti, optr);
3381	if (i != CMD_QUEUED) {
3382		isp_destroy_handle(isp, handle);
3383		/*
3384		 * dmasetup sets actual error in packet, and
3385		 * return what we were given to return.
3386		 */
3387		return (i);
3388	}
3389	XS_SETERR(xs, HBA_NOERROR);
3390	isp_prt(isp, ISP_LOGDEBUG2,
3391	    "START cmd for %d.%d.%d cmd 0x%x datalen %ld",
3392	    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), XS_CDBP(xs)[0],
3393	    (long) XS_XFRLEN(xs));
3394	ISP_ADD_REQUEST(isp, nxti);
3395	isp->isp_nactive++;
3396	return (CMD_QUEUED);
3397}
3398
3399/*
3400 * isp control
3401 * Locks (ints blocked) assumed held.
3402 */
3403
3404int
3405isp_control(ispsoftc_t *isp, ispctl_t ctl, void *arg)
3406{
3407	XS_T *xs;
3408	mbreg_t mbs;
3409	int bus, tgt;
3410	uint16_t handle;
3411
3412	MEMZERO(&mbs, sizeof (mbs));
3413
3414	switch (ctl) {
3415	default:
3416		isp_prt(isp, ISP_LOGERR, "Unknown Control Opcode 0x%x", ctl);
3417		break;
3418
3419	case ISPCTL_RESET_BUS:
3420		/*
3421		 * Issue a bus reset.
3422		 */
3423		mbs.param[0] = MBOX_BUS_RESET;
3424		mbs.param[2] = 0;
3425		if (IS_SCSI(isp)) {
3426			mbs.param[1] =
3427			    ((sdparam *) isp->isp_param)->isp_bus_reset_delay;
3428			if (mbs.param[1] < 2)
3429				mbs.param[1] = 2;
3430			bus = *((int *) arg);
3431			if (IS_DUALBUS(isp))
3432				mbs.param[2] = bus;
3433		} else {
3434			mbs.param[1] = 10;
3435			bus = 0;
3436		}
3437		isp->isp_sendmarker |= (1 << bus);
3438		isp_mboxcmd(isp, &mbs, MBLOGALL);
3439		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3440			break;
3441		}
3442		isp_prt(isp, ISP_LOGINFO,
3443		    "driver initiated bus reset of bus %d", bus);
3444		return (0);
3445
3446	case ISPCTL_RESET_DEV:
3447		tgt = (*((int *) arg)) & 0xffff;
3448		bus = (*((int *) arg)) >> 16;
3449		mbs.param[0] = MBOX_ABORT_TARGET;
3450		if (IS_SCSI(isp)) {
3451			mbs.param[1] = (tgt << 8) | (bus << 15);
3452		} else {
3453			if (IS_2KLOGIN(isp)) {
3454				mbs.param[1] = tgt;
3455				mbs.obits |= (1 << 10);
3456			} else {
3457				mbs.param[1] = (tgt << 8);
3458			}
3459		}
3460		mbs.param[2] = 3;	/* 'delay', in seconds */
3461		isp_mboxcmd(isp, &mbs, MBLOGALL);
3462		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3463			break;
3464		}
3465		isp_prt(isp, ISP_LOGINFO,
3466		    "Target %d on Bus %d Reset Succeeded", tgt, bus);
3467		isp->isp_sendmarker |= (1 << bus);
3468		return (0);
3469
3470	case ISPCTL_ABORT_CMD:
3471		xs = (XS_T *) arg;
3472		tgt = XS_TGT(xs);
3473		handle = isp_find_handle(isp, xs);
3474		if (handle == 0) {
3475			isp_prt(isp, ISP_LOGWARN,
3476			    "cannot find handle for command to abort");
3477			break;
3478		}
3479		bus = XS_CHANNEL(xs);
3480		mbs.param[0] = MBOX_ABORT;
3481		if (IS_FC(isp)) {
3482			if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN)  {
3483				if (IS_2KLOGIN(isp)) {
3484					mbs.param[1] = tgt;
3485				} else {
3486					mbs.param[1] = tgt << 8;
3487				}
3488				mbs.param[4] = 0;
3489				mbs.param[5] = 0;
3490				mbs.param[6] = XS_LUN(xs);
3491			} else {
3492				mbs.param[1] = tgt << 8 | XS_LUN(xs);
3493			}
3494		} else {
3495			mbs.param[1] =
3496			    (bus << 15) | (XS_TGT(xs) << 8) | XS_LUN(xs);
3497		}
3498		mbs.param[3] = 0;
3499		mbs.param[2] = handle;
3500		isp_mboxcmd(isp, &mbs, MBLOGALL & ~MBOX_COMMAND_ERROR);
3501		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
3502			return (0);
3503		}
3504		/*
3505		 * XXX: Look for command in the REQUEST QUEUE. That is,
3506		 * XXX: It hasen't been picked up by firmware yet.
3507		 */
3508		break;
3509
3510	case ISPCTL_UPDATE_PARAMS:
3511
3512		isp_update(isp);
3513		return (0);
3514
3515	case ISPCTL_FCLINK_TEST:
3516
3517		if (IS_FC(isp)) {
3518			int usdelay = *((int *) arg);
3519			if (usdelay == 0) {
3520				usdelay =  250000;
3521			}
3522			return (isp_fclink_test(isp, usdelay));
3523		}
3524		break;
3525
3526	case ISPCTL_SCAN_FABRIC:
3527
3528		if (IS_FC(isp)) {
3529			int ftype = (arg)? *((int *) arg) : FC4_SCSI;
3530			return (isp_scan_fabric(isp, ftype));
3531		}
3532		break;
3533
3534	case ISPCTL_SCAN_LOOP:
3535
3536		if (IS_FC(isp)) {
3537			return (isp_scan_loop(isp));
3538		}
3539		break;
3540
3541	case ISPCTL_PDB_SYNC:
3542
3543		if (IS_FC(isp)) {
3544			return (isp_pdb_sync(isp));
3545		}
3546		break;
3547
3548	case ISPCTL_SEND_LIP:
3549
3550		if (IS_FC(isp)) {
3551			mbs.param[0] = MBOX_INIT_LIP;
3552			isp_mboxcmd(isp, &mbs, MBLOGALL);
3553			if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
3554				return (0);
3555			}
3556		}
3557		break;
3558
3559	case ISPCTL_GET_POSMAP:
3560
3561		if (IS_FC(isp) && arg) {
3562			return (isp_getmap(isp, arg));
3563		}
3564		break;
3565
3566
3567	case ISPCTL_GET_PDB:
3568		if (IS_FC(isp) && arg) {
3569			int id = *((int *)arg);
3570			isp_pdb_t *pdb = arg;
3571			return (isp_getpdb(isp, id, pdb));
3572		}
3573		break;
3574
3575	case ISPCTL_RUN_MBOXCMD:
3576
3577		isp_mboxcmd(isp, arg, MBLOGALL);
3578		return(0);
3579
3580#ifdef	ISP_TARGET_MODE
3581	case ISPCTL_TOGGLE_TMODE:
3582	{
3583
3584		/*
3585		 * We don't check/set against role here- that's the
3586		 * responsibility for the outer layer to coordinate.
3587		 */
3588		if (IS_SCSI(isp)) {
3589			int param = *(int *)arg;
3590			mbs.param[0] = MBOX_ENABLE_TARGET_MODE;
3591			mbs.param[1] = param & 0xffff;
3592			mbs.param[2] = param >> 16;
3593			isp_mboxcmd(isp, &mbs, MBLOGALL);
3594			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3595				break;
3596			}
3597		}
3598		return (0);
3599	}
3600#endif
3601	}
3602	return (-1);
3603}
3604
3605/*
3606 * Interrupt Service Routine(s).
3607 *
3608 * External (OS) framework has done the appropriate locking,
3609 * and the locking will be held throughout this function.
3610 */
3611
3612/*
3613 * Limit our stack depth by sticking with the max likely number
3614 * of completions on a request queue at any one time.
3615 */
3616#ifndef	MAX_REQUESTQ_COMPLETIONS
3617#define	MAX_REQUESTQ_COMPLETIONS	64
3618#endif
3619
3620void
3621isp_intr(ispsoftc_t *isp, uint16_t isr, uint16_t sema, uint16_t mbox)
3622{
3623	XS_T *complist[MAX_REQUESTQ_COMPLETIONS], *xs;
3624	uint16_t iptr, optr, junk;
3625	int i, nlooked = 0, ndone = 0;
3626
3627again:
3628	/*
3629	 * Is this a mailbox related interrupt?
3630	 * The mailbox semaphore will be nonzero if so.
3631	 */
3632	if (sema) {
3633		if (mbox & 0x4000) {
3634			isp->isp_intmboxc++;
3635			if (isp->isp_mboxbsy) {
3636				int i = 0, obits = isp->isp_obits;
3637				isp->isp_mboxtmp[i++] = mbox;
3638				for (i = 1; i < MAX_MAILBOX(isp); i++) {
3639					if ((obits & (1 << i)) == 0) {
3640						continue;
3641					}
3642					isp->isp_mboxtmp[i] =
3643					    ISP_READ(isp, MBOX_OFF(i));
3644				}
3645				if (isp->isp_mbxwrk0) {
3646					if (isp_mbox_continue(isp) == 0) {
3647						return;
3648					}
3649				}
3650				MBOX_NOTIFY_COMPLETE(isp);
3651			} else {
3652				isp_prt(isp, ISP_LOGWARN,
3653				    "Mbox Command Async (0x%x) with no waiters",
3654				    mbox);
3655			}
3656		} else if (isp_parse_async(isp, mbox) < 0) {
3657			return;
3658		}
3659		if ((IS_FC(isp) && mbox != ASYNC_RIO_RESP) ||
3660		    isp->isp_state != ISP_RUNSTATE) {
3661			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3662			ISP_WRITE(isp, BIU_SEMA, 0);
3663			return;
3664		}
3665	}
3666
3667	/*
3668	 * We can't be getting this now.
3669	 */
3670	if (isp->isp_state != ISP_RUNSTATE) {
3671		isp_prt(isp, ISP_LOGWARN,
3672		    "interrupt (ISR=%x SEMA=%x) when not ready", isr, sema);
3673		/*
3674		 * Thank you very much!  *Burrrp*!
3675		 */
3676		WRITE_RESPONSE_QUEUE_OUT_POINTER(isp,
3677		    READ_RESPONSE_QUEUE_IN_POINTER(isp));
3678
3679		ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3680		ISP_WRITE(isp, BIU_SEMA, 0);
3681		return;
3682	}
3683
3684	/*
3685	 * Get the current Response Queue Out Pointer.
3686	 *
3687	 * If we're a 2300, we can ask what hardware what it thinks.
3688	 */
3689	if (IS_23XX(isp)) {
3690		optr = ISP_READ(isp, isp->isp_respoutrp);
3691		/*
3692		 * Debug: to be taken out eventually
3693		 */
3694		if (isp->isp_residx != optr) {
3695			isp_prt(isp, ISP_LOGWARN, "optr %x soft optr %x",
3696			    optr, isp->isp_residx);
3697		}
3698	} else {
3699		optr = isp->isp_residx;
3700	}
3701
3702	/*
3703	 * You *must* read the Response Queue In Pointer
3704	 * prior to clearing the RISC interrupt.
3705	 *
3706	 * Debounce the 2300 if revision less than 2.
3707	 */
3708	if (IS_2100(isp) || (IS_2300(isp) && isp->isp_revision < 2)) {
3709		i = 0;
3710		do {
3711			iptr = READ_RESPONSE_QUEUE_IN_POINTER(isp);
3712			junk = READ_RESPONSE_QUEUE_IN_POINTER(isp);
3713		} while (junk != iptr && ++i < 1000);
3714
3715		if (iptr != junk) {
3716			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3717			isp_prt(isp, ISP_LOGWARN,
3718			    "Response Queue Out Pointer Unstable (%x, %x)",
3719			    iptr, junk);
3720			return;
3721		}
3722	} else {
3723		iptr = READ_RESPONSE_QUEUE_IN_POINTER(isp);
3724	}
3725	isp->isp_resodx = iptr;
3726
3727
3728	if (optr == iptr && sema == 0) {
3729		/*
3730		 * There are a lot of these- reasons unknown- mostly on
3731		 * faster Alpha machines.
3732		 *
3733		 * I tried delaying after writing HCCR_CMD_CLEAR_RISC_INT to
3734		 * make sure the old interrupt went away (to avoid 'ringing'
3735		 * effects), but that didn't stop this from occurring.
3736		 */
3737		if (IS_23XX(isp)) {
3738			USEC_DELAY(100);
3739			iptr = READ_RESPONSE_QUEUE_IN_POINTER(isp);
3740			junk = ISP_READ(isp, BIU_R2HSTSLO);
3741		} else {
3742			junk = ISP_READ(isp, BIU_ISR);
3743		}
3744		if (optr == iptr) {
3745			if (IS_23XX(isp)) {
3746				;
3747			} else {
3748				sema = ISP_READ(isp, BIU_SEMA);
3749				mbox = ISP_READ(isp, OUTMAILBOX0);
3750				if ((sema & 0x3) && (mbox & 0x8000)) {
3751					goto again;
3752				}
3753			}
3754			isp->isp_intbogus++;
3755			isp_prt(isp, ISP_LOGDEBUG1,
3756			    "bogus intr- isr %x (%x) iptr %x optr %x",
3757			    isr, junk, iptr, optr);
3758		}
3759	}
3760	isp->isp_resodx = iptr;
3761	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3762	ISP_WRITE(isp, BIU_SEMA, 0);
3763
3764	if (isp->isp_rspbsy) {
3765		return;
3766	}
3767	isp->isp_rspbsy = 1;
3768
3769	while (optr != iptr) {
3770		ispstatusreq_t local, *sp = &local;
3771		isphdr_t *hp;
3772		int type;
3773		uint16_t oop;
3774		int buddaboom = 0;
3775
3776		hp = (isphdr_t *) ISP_QUEUE_ENTRY(isp->isp_result, optr);
3777		oop = optr;
3778		optr = ISP_NXT_QENTRY(optr, RESULT_QUEUE_LEN(isp));
3779		nlooked++;
3780 read_again:
3781		/*
3782		 * Synchronize our view of this response queue entry.
3783		 */
3784		MEMORYBARRIER(isp, SYNC_RESULT, oop, QENTRY_LEN);
3785
3786		type = isp_get_response_type(isp, hp);
3787
3788		if (type == RQSTYPE_RESPONSE) {
3789			isp_get_response(isp, (ispstatusreq_t *) hp, sp);
3790		} else if (type == RQSTYPE_RIO2) {
3791			isp_rio2_t rio;
3792			isp_get_rio2(isp, (isp_rio2_t *) hp, &rio);
3793			for (i = 0; i < rio.req_header.rqs_seqno; i++) {
3794				isp_fastpost_complete(isp, rio.req_handles[i]);
3795			}
3796			if (isp->isp_fpcchiwater < rio.req_header.rqs_seqno)
3797				isp->isp_fpcchiwater = rio.req_header.rqs_seqno;
3798			MEMZERO(hp, QENTRY_LEN);	/* PERF */
3799			continue;
3800		} else {
3801			/*
3802			 * Somebody reachable via isp_handle_other_response
3803			 * may have updated the response queue pointers for
3804			 * us, so we reload our goal index.
3805			 */
3806			int i = isp_handle_other_response(isp, type, hp, &optr);
3807			if (i < 0) {
3808				goto read_again;
3809			} else if (i > 0) {
3810				iptr = isp->isp_resodx;
3811				MEMZERO(hp, QENTRY_LEN);	/* PERF */
3812				continue;
3813			}
3814
3815			/*
3816			 * After this point, we'll just look at the header as
3817			 * we don't know how to deal with the rest of the
3818			 * response.
3819			 */
3820			isp_get_response(isp, (ispstatusreq_t *) hp, sp);
3821
3822			/*
3823			 * It really has to be a bounced request just copied
3824			 * from the request queue to the response queue. If
3825			 * not, something bad has happened.
3826			 */
3827			if (sp->req_header.rqs_entry_type != RQSTYPE_REQUEST) {
3828				isp_prt(isp, ISP_LOGERR, notresp,
3829				    sp->req_header.rqs_entry_type, oop, optr,
3830				    nlooked);
3831				if (isp->isp_dblev & ISP_LOGDEBUG0) {
3832					isp_print_bytes(isp, "Queue Entry",
3833					    QENTRY_LEN, sp);
3834				}
3835				MEMZERO(hp, QENTRY_LEN);	/* PERF */
3836				continue;
3837			}
3838			buddaboom = 1;
3839		}
3840
3841		if (sp->req_header.rqs_flags & 0xf) {
3842#define	_RQS_OFLAGS	\
3843	~(RQSFLAG_CONTINUATION|RQSFLAG_FULL|RQSFLAG_BADHEADER|RQSFLAG_BADPACKET)
3844			if (sp->req_header.rqs_flags & RQSFLAG_CONTINUATION) {
3845				isp_prt(isp, ISP_LOGWARN,
3846				    "continuation segment");
3847				WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
3848				continue;
3849			}
3850			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
3851				isp_prt(isp, ISP_LOGDEBUG1,
3852				    "internal queues full");
3853				/*
3854				 * We'll synthesize a QUEUE FULL message below.
3855				 */
3856			}
3857			if (sp->req_header.rqs_flags & RQSFLAG_BADHEADER) {
3858				isp_prt(isp, ISP_LOGERR,  "bad header flag");
3859				buddaboom++;
3860			}
3861			if (sp->req_header.rqs_flags & RQSFLAG_BADPACKET) {
3862				isp_prt(isp, ISP_LOGERR, "bad request packet");
3863				buddaboom++;
3864			}
3865			if (sp->req_header.rqs_flags & _RQS_OFLAGS) {
3866				isp_prt(isp, ISP_LOGERR,
3867				    "unknown flags (0x%x) in response",
3868				    sp->req_header.rqs_flags);
3869				buddaboom++;
3870			}
3871#undef	_RQS_OFLAGS
3872		}
3873		if (sp->req_handle > isp->isp_maxcmds || sp->req_handle < 1) {
3874			MEMZERO(hp, QENTRY_LEN);	/* PERF */
3875			isp_prt(isp, ISP_LOGERR,
3876			    "bad request handle %d (type 0x%x, flags 0x%x)",
3877			    sp->req_handle, sp->req_header.rqs_entry_type,
3878			    sp->req_header.rqs_flags);
3879			WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
3880			continue;
3881		}
3882		xs = isp_find_xs(isp, sp->req_handle);
3883		if (xs == NULL) {
3884			uint8_t ts = sp->req_completion_status & 0xff;
3885			MEMZERO(hp, QENTRY_LEN);	/* PERF */
3886			/*
3887			 * Only whine if this isn't the expected fallout of
3888			 * aborting the command.
3889			 */
3890			if (sp->req_header.rqs_entry_type != RQSTYPE_RESPONSE) {
3891				isp_prt(isp, ISP_LOGERR,
3892				    "cannot find handle 0x%x (type 0x%x)",
3893				    sp->req_handle,
3894				    sp->req_header.rqs_entry_type);
3895			} else if (ts != RQCS_ABORTED) {
3896				isp_prt(isp, ISP_LOGERR,
3897				    "cannot find handle 0x%x (status 0x%x)",
3898				    sp->req_handle, ts);
3899			}
3900			WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
3901			continue;
3902		}
3903		isp_destroy_handle(isp, sp->req_handle);
3904		if (sp->req_status_flags & RQSTF_BUS_RESET) {
3905			XS_SETERR(xs, HBA_BUSRESET);
3906			isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
3907		}
3908		if (buddaboom) {
3909			XS_SETERR(xs, HBA_BOTCH);
3910		}
3911
3912		if (IS_FC(isp) && (sp->req_scsi_status & RQCS_SV)) {
3913			/*
3914			 * Fibre Channel F/W doesn't say we got status
3915			 * if there's Sense Data instead. I guess they
3916			 * think it goes w/o saying.
3917			 */
3918			sp->req_state_flags |= RQSF_GOT_STATUS;
3919		}
3920		if (sp->req_state_flags & RQSF_GOT_STATUS) {
3921			*XS_STSP(xs) = sp->req_scsi_status & 0xff;
3922		}
3923
3924		switch (sp->req_header.rqs_entry_type) {
3925		case RQSTYPE_RESPONSE:
3926			XS_SET_STATE_STAT(isp, xs, sp);
3927			isp_parse_status(isp, sp, xs);
3928			if ((XS_NOERR(xs) || XS_ERR(xs) == HBA_NOERROR) &&
3929			    (*XS_STSP(xs) == SCSI_BUSY)) {
3930				XS_SETERR(xs, HBA_TGTBSY);
3931			}
3932			if (IS_SCSI(isp)) {
3933				XS_RESID(xs) = sp->req_resid;
3934				if ((sp->req_state_flags & RQSF_GOT_STATUS) &&
3935				    (*XS_STSP(xs) == SCSI_CHECK) &&
3936				    (sp->req_state_flags & RQSF_GOT_SENSE)) {
3937					XS_SAVE_SENSE(xs, sp);
3938				}
3939				/*
3940				 * A new synchronous rate was negotiated for
3941				 * this target. Mark state such that we'll go
3942				 * look up that which has changed later.
3943				 */
3944				if (sp->req_status_flags & RQSTF_NEGOTIATION) {
3945					int t = XS_TGT(xs);
3946					sdparam *sdp = isp->isp_param;
3947					sdp += XS_CHANNEL(xs);
3948					sdp->isp_devparam[t].dev_refresh = 1;
3949					isp->isp_update |=
3950					    (1 << XS_CHANNEL(xs));
3951				}
3952			} else {
3953				if (sp->req_status_flags & RQSF_XFER_COMPLETE) {
3954					XS_RESID(xs) = 0;
3955				} else if (sp->req_scsi_status & RQCS_RESID) {
3956					XS_RESID(xs) = sp->req_resid;
3957				} else {
3958					XS_RESID(xs) = 0;
3959				}
3960				if ((sp->req_state_flags & RQSF_GOT_STATUS) &&
3961				    (*XS_STSP(xs) == SCSI_CHECK) &&
3962				    (sp->req_scsi_status & RQCS_SV)) {
3963					XS_SAVE_SENSE(xs, sp);
3964					/* solely for the benefit of debug */
3965					sp->req_state_flags |= RQSF_GOT_SENSE;
3966				}
3967			}
3968			isp_prt(isp, ISP_LOGDEBUG2,
3969			   "asked for %ld got resid %ld", (long) XS_XFRLEN(xs),
3970			   (long) sp->req_resid);
3971			break;
3972		case RQSTYPE_REQUEST:
3973			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
3974				/*
3975				 * Force Queue Full status.
3976				 */
3977				*XS_STSP(xs) = SCSI_QFULL;
3978				XS_SETERR(xs, HBA_NOERROR);
3979			} else if (XS_NOERR(xs)) {
3980				/*
3981				 * ????
3982				 */
3983				isp_prt(isp, ISP_LOGDEBUG0,
3984				    "Request Queue Entry bounced back");
3985				XS_SETERR(xs, HBA_BOTCH);
3986			}
3987			XS_RESID(xs) = XS_XFRLEN(xs);
3988			break;
3989		default:
3990			isp_prt(isp, ISP_LOGWARN,
3991			    "unhandled response queue type 0x%x",
3992			    sp->req_header.rqs_entry_type);
3993			if (XS_NOERR(xs)) {
3994				XS_SETERR(xs, HBA_BOTCH);
3995			}
3996			break;
3997		}
3998
3999		/*
4000		 * Free any DMA resources. As a side effect, this may
4001		 * also do any cache flushing necessary for data coherence.			 */
4002		if (XS_XFRLEN(xs)) {
4003			ISP_DMAFREE(isp, xs, sp->req_handle);
4004		}
4005
4006		if (((isp->isp_dblev & (ISP_LOGDEBUG2|ISP_LOGDEBUG3))) ||
4007		    ((isp->isp_dblev & ISP_LOGDEBUG1) && ((!XS_NOERR(xs)) ||
4008		    (*XS_STSP(xs) != SCSI_GOOD)))) {
4009			char skey;
4010			if (sp->req_state_flags & RQSF_GOT_SENSE) {
4011				skey = XS_SNSKEY(xs) & 0xf;
4012				if (skey < 10)
4013					skey += '0';
4014				else
4015					skey += 'a' - 10;
4016			} else if (*XS_STSP(xs) == SCSI_CHECK) {
4017				skey = '?';
4018			} else {
4019				skey = '.';
4020			}
4021			isp_prt(isp, ISP_LOGALL, finmsg, XS_CHANNEL(xs),
4022			    XS_TGT(xs), XS_LUN(xs), XS_XFRLEN(xs), XS_RESID(xs),
4023			    *XS_STSP(xs), skey, XS_ERR(xs));
4024		}
4025
4026		if (isp->isp_nactive > 0)
4027		    isp->isp_nactive--;
4028		complist[ndone++] = xs;	/* defer completion call until later */
4029		MEMZERO(hp, QENTRY_LEN);	/* PERF */
4030		if (ndone == MAX_REQUESTQ_COMPLETIONS) {
4031			break;
4032		}
4033	}
4034
4035	/*
4036	 * If we looked at any commands, then it's valid to find out
4037	 * what the outpointer is. It also is a trigger to update the
4038	 * ISP's notion of what we've seen so far.
4039	 */
4040	if (nlooked) {
4041		WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
4042		/*
4043		 * While we're at it, read the requst queue out pointer.
4044		 */
4045		isp->isp_reqodx = READ_REQUEST_QUEUE_OUT_POINTER(isp);
4046		if (isp->isp_rscchiwater < ndone)
4047			isp->isp_rscchiwater = ndone;
4048	}
4049
4050	isp->isp_residx = optr;
4051	isp->isp_rspbsy = 0;
4052	for (i = 0; i < ndone; i++) {
4053		xs = complist[i];
4054		if (xs) {
4055			isp->isp_rsltccmplt++;
4056			isp_done(xs);
4057		}
4058	}
4059}
4060
4061/*
4062 * Support routines.
4063 */
4064
4065static int
4066isp_parse_async(ispsoftc_t *isp, uint16_t mbox)
4067{
4068	int rval = 0;
4069	int bus;
4070
4071	if (IS_DUALBUS(isp)) {
4072		bus = ISP_READ(isp, OUTMAILBOX6);
4073	} else {
4074		bus = 0;
4075	}
4076	isp_prt(isp, ISP_LOGDEBUG2, "Async Mbox 0x%x", mbox);
4077
4078	switch (mbox) {
4079	case ASYNC_BUS_RESET:
4080		isp->isp_sendmarker |= (1 << bus);
4081#ifdef	ISP_TARGET_MODE
4082		if (isp_target_async(isp, bus, mbox))
4083			rval = -1;
4084#endif
4085		isp_async(isp, ISPASYNC_BUS_RESET, &bus);
4086		break;
4087	case ASYNC_SYSTEM_ERROR:
4088#ifdef	ISP_FW_CRASH_DUMP
4089		/*
4090		 * If we have crash dumps enabled, it's up to the handler
4091		 * for isp_async to reinit stuff and restart the firmware
4092		 * after performing the crash dump. The reason we do things
4093		 * this way is that we may need to activate a kernel thread
4094		 * to do all the crash dump goop.
4095		 */
4096		isp_async(isp, ISPASYNC_FW_CRASH, NULL);
4097#else
4098		isp_async(isp, ISPASYNC_FW_CRASH, NULL);
4099		isp_reinit(isp);
4100		isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
4101#endif
4102		rval = -1;
4103		break;
4104
4105	case ASYNC_RQS_XFER_ERR:
4106		isp_prt(isp, ISP_LOGERR, "Request Queue Transfer Error");
4107		break;
4108
4109	case ASYNC_RSP_XFER_ERR:
4110		isp_prt(isp, ISP_LOGERR, "Response Queue Transfer Error");
4111		break;
4112
4113	case ASYNC_QWAKEUP:
4114		/*
4115		 * We've just been notified that the Queue has woken up.
4116		 * We don't need to be chatty about this- just unlatch things
4117		 * and move on.
4118		 */
4119		mbox = READ_REQUEST_QUEUE_OUT_POINTER(isp);
4120		break;
4121
4122	case ASYNC_TIMEOUT_RESET:
4123		isp_prt(isp, ISP_LOGWARN,
4124		    "timeout initiated SCSI bus reset of bus %d", bus);
4125		isp->isp_sendmarker |= (1 << bus);
4126#ifdef	ISP_TARGET_MODE
4127		if (isp_target_async(isp, bus, mbox))
4128			rval = -1;
4129#endif
4130		break;
4131
4132	case ASYNC_DEVICE_RESET:
4133		isp_prt(isp, ISP_LOGINFO, "device reset on bus %d", bus);
4134		isp->isp_sendmarker |= (1 << bus);
4135#ifdef	ISP_TARGET_MODE
4136		if (isp_target_async(isp, bus, mbox))
4137			rval = -1;
4138#endif
4139		break;
4140
4141	case ASYNC_EXTMSG_UNDERRUN:
4142		isp_prt(isp, ISP_LOGWARN, "extended message underrun");
4143		break;
4144
4145	case ASYNC_SCAM_INT:
4146		isp_prt(isp, ISP_LOGINFO, "SCAM interrupt");
4147		break;
4148
4149	case ASYNC_HUNG_SCSI:
4150		isp_prt(isp, ISP_LOGERR,
4151		    "stalled SCSI Bus after DATA Overrun");
4152		/* XXX: Need to issue SCSI reset at this point */
4153		break;
4154
4155	case ASYNC_KILLED_BUS:
4156		isp_prt(isp, ISP_LOGERR, "SCSI Bus reset after DATA Overrun");
4157		break;
4158
4159	case ASYNC_BUS_TRANSIT:
4160		mbox = ISP_READ(isp, OUTMAILBOX2);
4161		switch (mbox & 0x1c00) {
4162		case SXP_PINS_LVD_MODE:
4163			isp_prt(isp, ISP_LOGINFO, "Transition to LVD mode");
4164			SDPARAM(isp)->isp_diffmode = 0;
4165			SDPARAM(isp)->isp_ultramode = 0;
4166			SDPARAM(isp)->isp_lvdmode = 1;
4167			break;
4168		case SXP_PINS_HVD_MODE:
4169			isp_prt(isp, ISP_LOGINFO,
4170			    "Transition to Differential mode");
4171			SDPARAM(isp)->isp_diffmode = 1;
4172			SDPARAM(isp)->isp_ultramode = 0;
4173			SDPARAM(isp)->isp_lvdmode = 0;
4174			break;
4175		case SXP_PINS_SE_MODE:
4176			isp_prt(isp, ISP_LOGINFO,
4177			    "Transition to Single Ended mode");
4178			SDPARAM(isp)->isp_diffmode = 0;
4179			SDPARAM(isp)->isp_ultramode = 1;
4180			SDPARAM(isp)->isp_lvdmode = 0;
4181			break;
4182		default:
4183			isp_prt(isp, ISP_LOGWARN,
4184			    "Transition to Unknown Mode 0x%x", mbox);
4185			break;
4186		}
4187		/*
4188		 * XXX: Set up to renegotiate again!
4189		 */
4190		/* Can only be for a 1080... */
4191		isp->isp_sendmarker |= (1 << bus);
4192		break;
4193
4194	/*
4195	 * We can use bus, which will always be zero for FC cards,
4196	 * as a mailbox pattern accumulator to be checked below.
4197	 */
4198	case ASYNC_RIO5:
4199		bus = 0x1ce;	/* outgoing mailbox regs 1-3, 6-7 */
4200		break;
4201
4202	case ASYNC_RIO4:
4203		bus = 0x14e;	/* outgoing mailbox regs 1-3, 6 */
4204		break;
4205
4206	case ASYNC_RIO3:
4207		bus = 0x10e;	/* outgoing mailbox regs 1-3 */
4208		break;
4209
4210	case ASYNC_RIO2:
4211		bus = 0x106;	/* outgoing mailbox regs 1-2 */
4212		break;
4213
4214	case ASYNC_RIO1:
4215	case ASYNC_CMD_CMPLT:
4216		bus = 0x102;	/* outgoing mailbox regs 1 */
4217		break;
4218
4219	case ASYNC_RIO_RESP:
4220		return (rval);
4221
4222	case ASYNC_CTIO_DONE:
4223	{
4224#ifdef	ISP_TARGET_MODE
4225		int handle =
4226		    (ISP_READ(isp, OUTMAILBOX2) << 16) |
4227		    (ISP_READ(isp, OUTMAILBOX1));
4228		if (isp_target_async(isp, handle, mbox)) {
4229			rval = -1;
4230		} else {
4231			/* count it as a fast posting intr */
4232			isp->isp_fphccmplt++;
4233		}
4234#else
4235		isp_prt(isp, ISP_LOGINFO, "Fast Posting CTIO done");
4236		isp->isp_fphccmplt++;	/* count it as a fast posting intr */
4237#endif
4238		break;
4239	}
4240	case ASYNC_LIP_F8:
4241	case ASYNC_LIP_OCCURRED:
4242		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4243		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
4244		isp->isp_sendmarker = 1;
4245		isp_mark_getpdb_all(isp);
4246		isp_async(isp, ISPASYNC_LIP, NULL);
4247#ifdef	ISP_TARGET_MODE
4248		if (isp_target_async(isp, bus, mbox))
4249			rval = -1;
4250#endif
4251		/*
4252		 * We've had problems with data corruption occuring on
4253		 * commands that complete (with no apparent error) after
4254		 * we receive a LIP. This has been observed mostly on
4255		 * Local Loop topologies. To be safe, let's just mark
4256		 * all active commands as dead.
4257		 */
4258		if (FCPARAM(isp)->isp_topo == TOPO_NL_PORT ||
4259		    FCPARAM(isp)->isp_topo == TOPO_FL_PORT) {
4260			int i, j;
4261			for (i = j = 0; i < isp->isp_maxcmds; i++) {
4262				XS_T *xs;
4263				xs = isp->isp_xflist[i];
4264				if (xs != NULL) {
4265					j++;
4266					XS_SETERR(xs, HBA_BUSRESET);
4267				}
4268			}
4269			if (j) {
4270				isp_prt(isp, ISP_LOGERR,
4271				    "LIP destroyed %d active commands", j);
4272			}
4273		}
4274		break;
4275
4276	case ASYNC_LOOP_UP:
4277		isp->isp_sendmarker = 1;
4278		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4279		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
4280		isp_mark_getpdb_all(isp);
4281		isp_async(isp, ISPASYNC_LOOP_UP, NULL);
4282#ifdef	ISP_TARGET_MODE
4283		if (isp_target_async(isp, bus, mbox))
4284			rval = -1;
4285#endif
4286		break;
4287
4288	case ASYNC_LOOP_DOWN:
4289		isp->isp_sendmarker = 1;
4290		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4291		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
4292		isp_mark_getpdb_all(isp);
4293		isp_async(isp, ISPASYNC_LOOP_DOWN, NULL);
4294#ifdef	ISP_TARGET_MODE
4295		if (isp_target_async(isp, bus, mbox))
4296			rval = -1;
4297#endif
4298		break;
4299
4300	case ASYNC_LOOP_RESET:
4301		isp->isp_sendmarker = 1;
4302		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4303		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
4304		isp_mark_getpdb_all(isp);
4305		isp_async(isp, ISPASYNC_LOOP_RESET, NULL);
4306#ifdef	ISP_TARGET_MODE
4307		if (isp_target_async(isp, bus, mbox))
4308			rval = -1;
4309#endif
4310		break;
4311
4312	case ASYNC_PDB_CHANGED:
4313		isp->isp_sendmarker = 1;
4314		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
4315		isp_mark_getpdb_all(isp);
4316		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_PDB);
4317		break;
4318
4319	case ASYNC_CHANGE_NOTIFY:
4320		/*
4321		 * Not correct, but it will force us to rescan the loop.
4322		 */
4323		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
4324		isp_mark_getpdb_all(isp);
4325		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_SNS);
4326		break;
4327
4328	case ASYNC_PTPMODE:
4329		if (FCPARAM(isp)->isp_onfabric)
4330			FCPARAM(isp)->isp_topo = TOPO_F_PORT;
4331		else
4332			FCPARAM(isp)->isp_topo = TOPO_N_PORT;
4333		isp_mark_getpdb_all(isp);
4334		isp->isp_sendmarker = 1;
4335		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4336		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
4337		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
4338#ifdef	ISP_TARGET_MODE
4339		if (isp_target_async(isp, bus, mbox))
4340			rval = -1;
4341#endif
4342		isp_prt(isp, ISP_LOGINFO, "Point-to-Point mode");
4343		break;
4344
4345	case ASYNC_CONNMODE:
4346		mbox = ISP_READ(isp, OUTMAILBOX1);
4347		isp_mark_getpdb_all(isp);
4348		switch (mbox) {
4349		case ISP_CONN_LOOP:
4350			isp_prt(isp, ISP_LOGINFO,
4351			    "Point-to-Point -> Loop mode");
4352			break;
4353		case ISP_CONN_PTP:
4354			isp_prt(isp, ISP_LOGINFO,
4355			    "Loop -> Point-to-Point mode");
4356			break;
4357		case ISP_CONN_BADLIP:
4358			isp_prt(isp, ISP_LOGWARN,
4359			    "Point-to-Point -> Loop mode (BAD LIP)");
4360			break;
4361		case ISP_CONN_FATAL:
4362			isp_prt(isp, ISP_LOGERR, "FATAL CONNECTION ERROR");
4363#ifdef	ISP_FW_CRASH_DUMP
4364			isp_async(isp, ISPASYNC_FW_CRASH, NULL);
4365#else
4366			isp_async(isp, ISPASYNC_FW_CRASH, NULL);
4367			isp_reinit(isp);
4368			isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
4369#endif
4370			return (-1);
4371		case ISP_CONN_LOOPBACK:
4372			isp_prt(isp, ISP_LOGWARN,
4373			    "Looped Back in Point-to-Point mode");
4374			break;
4375		default:
4376			isp_prt(isp, ISP_LOGWARN,
4377			    "Unknown connection mode (0x%x)", mbox);
4378			break;
4379		}
4380		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
4381		isp->isp_sendmarker = 1;
4382		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4383		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
4384		break;
4385
4386	default:
4387		isp_prt(isp, ISP_LOGWARN, "Unknown Async Code 0x%x", mbox);
4388		break;
4389	}
4390
4391	if (bus & 0x100) {
4392		int i, nh;
4393		uint16_t handles[16];
4394
4395		for (nh = 0, i = 1; i < MAX_MAILBOX(isp); i++) {
4396			if ((bus & (1 << i)) == 0) {
4397				continue;
4398			}
4399			handles[nh++] = ISP_READ(isp, MBOX_OFF(i));
4400		}
4401		for (i = 0; i < nh; i++) {
4402			isp_fastpost_complete(isp, handles[i]);
4403			isp_prt(isp,  ISP_LOGDEBUG3,
4404			    "fast post completion of %u", handles[i]);
4405		}
4406		if (isp->isp_fpcchiwater < nh)
4407			isp->isp_fpcchiwater = nh;
4408	} else {
4409		isp->isp_intoasync++;
4410	}
4411	return (rval);
4412}
4413
4414/*
4415 * Handle other response entries. A pointer to the request queue output
4416 * index is here in case we want to eat several entries at once, although
4417 * this is not used currently.
4418 */
4419
4420static int
4421isp_handle_other_response(ispsoftc_t *isp, int type,
4422    isphdr_t *hp, uint16_t *optrp)
4423{
4424	switch (type) {
4425	case RQSTYPE_STATUS_CONT:
4426		isp_prt(isp, ISP_LOGINFO, "Ignored Continuation Response");
4427		return (1);
4428	case RQSTYPE_ATIO:
4429	case RQSTYPE_CTIO:
4430	case RQSTYPE_ENABLE_LUN:
4431	case RQSTYPE_MODIFY_LUN:
4432	case RQSTYPE_NOTIFY:
4433	case RQSTYPE_NOTIFY_ACK:
4434	case RQSTYPE_CTIO1:
4435	case RQSTYPE_ATIO2:
4436	case RQSTYPE_CTIO2:
4437	case RQSTYPE_CTIO3:
4438		isp->isp_rsltccmplt++;	/* count as a response completion */
4439#ifdef	ISP_TARGET_MODE
4440		if (isp_target_notify(isp, (ispstatusreq_t *) hp, optrp)) {
4441			return (1);
4442		}
4443#endif
4444		/* FALLTHROUGH */
4445	case RQSTYPE_REQUEST:
4446	default:
4447		USEC_DELAY(100);
4448		if (type != isp_get_response_type(isp, hp)) {
4449			/*
4450			 * This is questionable- we're just papering over
4451			 * something we've seen on SMP linux in target
4452			 * mode- we don't really know what's happening
4453			 * here that causes us to think we've gotten
4454			 * an entry, but that either the entry isn't
4455			 * filled out yet or our CPU read data is stale.
4456			 */
4457			isp_prt(isp, ISP_LOGINFO,
4458				"unstable type in response queue");
4459			return (-1);
4460		}
4461		isp_prt(isp, ISP_LOGWARN, "Unhandled Response Type 0x%x",
4462		    isp_get_response_type(isp, hp));
4463		if (isp_async(isp, ISPASYNC_UNHANDLED_RESPONSE, hp)) {
4464			return (1);
4465		}
4466		return (0);
4467	}
4468}
4469
4470static void
4471isp_parse_status(ispsoftc_t *isp, ispstatusreq_t *sp, XS_T *xs)
4472{
4473	switch (sp->req_completion_status & 0xff) {
4474	case RQCS_COMPLETE:
4475		if (XS_NOERR(xs)) {
4476			XS_SETERR(xs, HBA_NOERROR);
4477		}
4478		return;
4479
4480	case RQCS_INCOMPLETE:
4481		if ((sp->req_state_flags & RQSF_GOT_TARGET) == 0) {
4482			isp_prt(isp, ISP_LOGDEBUG1,
4483			    "Selection Timeout for %d.%d.%d",
4484			    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4485			if (XS_NOERR(xs)) {
4486				XS_SETERR(xs, HBA_SELTIMEOUT);
4487			}
4488			return;
4489		}
4490		isp_prt(isp, ISP_LOGERR,
4491		    "command incomplete for %d.%d.%d, state 0x%x",
4492		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs),
4493		    sp->req_state_flags);
4494		break;
4495
4496	case RQCS_DMA_ERROR:
4497		isp_prt(isp, ISP_LOGERR, "DMA error for command on %d.%d.%d",
4498		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4499		break;
4500
4501	case RQCS_TRANSPORT_ERROR:
4502	{
4503		char buf[172];
4504		SNPRINTF(buf, sizeof (buf), "states=>");
4505		if (sp->req_state_flags & RQSF_GOT_BUS) {
4506			SNPRINTF(buf, sizeof (buf), "%s GOT_BUS", buf);
4507		}
4508		if (sp->req_state_flags & RQSF_GOT_TARGET) {
4509			SNPRINTF(buf, sizeof (buf), "%s GOT_TGT", buf);
4510		}
4511		if (sp->req_state_flags & RQSF_SENT_CDB) {
4512			SNPRINTF(buf, sizeof (buf), "%s SENT_CDB", buf);
4513		}
4514		if (sp->req_state_flags & RQSF_XFRD_DATA) {
4515			SNPRINTF(buf, sizeof (buf), "%s XFRD_DATA", buf);
4516		}
4517		if (sp->req_state_flags & RQSF_GOT_STATUS) {
4518			SNPRINTF(buf, sizeof (buf), "%s GOT_STS", buf);
4519		}
4520		if (sp->req_state_flags & RQSF_GOT_SENSE) {
4521			SNPRINTF(buf, sizeof (buf), "%s GOT_SNS", buf);
4522		}
4523		if (sp->req_state_flags & RQSF_XFER_COMPLETE) {
4524			SNPRINTF(buf, sizeof (buf), "%s XFR_CMPLT", buf);
4525		}
4526		SNPRINTF(buf, sizeof (buf), "%s\nstatus=>", buf);
4527		if (sp->req_status_flags & RQSTF_DISCONNECT) {
4528			SNPRINTF(buf, sizeof (buf), "%s Disconnect", buf);
4529		}
4530		if (sp->req_status_flags & RQSTF_SYNCHRONOUS) {
4531			SNPRINTF(buf, sizeof (buf), "%s Sync_xfr", buf);
4532		}
4533		if (sp->req_status_flags & RQSTF_PARITY_ERROR) {
4534			SNPRINTF(buf, sizeof (buf), "%s Parity", buf);
4535		}
4536		if (sp->req_status_flags & RQSTF_BUS_RESET) {
4537			SNPRINTF(buf, sizeof (buf), "%s Bus_Reset", buf);
4538		}
4539		if (sp->req_status_flags & RQSTF_DEVICE_RESET) {
4540			SNPRINTF(buf, sizeof (buf), "%s Device_Reset", buf);
4541		}
4542		if (sp->req_status_flags & RQSTF_ABORTED) {
4543			SNPRINTF(buf, sizeof (buf), "%s Aborted", buf);
4544		}
4545		if (sp->req_status_flags & RQSTF_TIMEOUT) {
4546			SNPRINTF(buf, sizeof (buf), "%s Timeout", buf);
4547		}
4548		if (sp->req_status_flags & RQSTF_NEGOTIATION) {
4549			SNPRINTF(buf, sizeof (buf), "%s Negotiation", buf);
4550		}
4551		isp_prt(isp, ISP_LOGERR, "%s", buf);
4552		isp_prt(isp, ISP_LOGERR, "transport error for %d.%d.%d:\n%s",
4553		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), buf);
4554		break;
4555	}
4556	case RQCS_RESET_OCCURRED:
4557		isp_prt(isp, ISP_LOGWARN,
4558		    "bus reset destroyed command for %d.%d.%d",
4559		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4560		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
4561		if (XS_NOERR(xs)) {
4562			XS_SETERR(xs, HBA_BUSRESET);
4563		}
4564		return;
4565
4566	case RQCS_ABORTED:
4567		isp_prt(isp, ISP_LOGERR, "command aborted for %d.%d.%d",
4568		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4569		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
4570		if (XS_NOERR(xs)) {
4571			XS_SETERR(xs, HBA_ABORTED);
4572		}
4573		return;
4574
4575	case RQCS_TIMEOUT:
4576		isp_prt(isp, ISP_LOGWARN, "command timed out for %d.%d.%d",
4577		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4578		/*
4579	 	 * Check to see if we logged out the device.
4580		 */
4581		if (IS_FC(isp)) {
4582			if ((sp->req_completion_status & RQSTF_LOGOUT) &&
4583			    FCPARAM(isp)->portdb[XS_TGT(xs)].valid &&
4584			    FCPARAM(isp)->portdb[XS_TGT(xs)].fabric_dev) {
4585				FCPARAM(isp)->portdb[XS_TGT(xs)].relogin = 1;
4586			}
4587		}
4588		if (XS_NOERR(xs)) {
4589			XS_SETERR(xs, HBA_CMDTIMEOUT);
4590		}
4591		return;
4592
4593	case RQCS_DATA_OVERRUN:
4594		XS_RESID(xs) = sp->req_resid;
4595		isp_prt(isp, ISP_LOGERR, "data overrun for command on %d.%d.%d",
4596		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4597		if (XS_NOERR(xs)) {
4598			XS_SETERR(xs, HBA_DATAOVR);
4599		}
4600		return;
4601
4602	case RQCS_COMMAND_OVERRUN:
4603		isp_prt(isp, ISP_LOGERR,
4604		    "command overrun for command on %d.%d.%d",
4605		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4606		break;
4607
4608	case RQCS_STATUS_OVERRUN:
4609		isp_prt(isp, ISP_LOGERR,
4610		    "status overrun for command on %d.%d.%d",
4611		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4612		break;
4613
4614	case RQCS_BAD_MESSAGE:
4615		isp_prt(isp, ISP_LOGERR,
4616		    "msg not COMMAND COMPLETE after status %d.%d.%d",
4617		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4618		break;
4619
4620	case RQCS_NO_MESSAGE_OUT:
4621		isp_prt(isp, ISP_LOGERR,
4622		    "No MESSAGE OUT phase after selection on %d.%d.%d",
4623		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4624		break;
4625
4626	case RQCS_EXT_ID_FAILED:
4627		isp_prt(isp, ISP_LOGERR, "EXTENDED IDENTIFY failed %d.%d.%d",
4628		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4629		break;
4630
4631	case RQCS_IDE_MSG_FAILED:
4632		isp_prt(isp, ISP_LOGERR,
4633		    "INITIATOR DETECTED ERROR rejected by %d.%d.%d",
4634		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4635		break;
4636
4637	case RQCS_ABORT_MSG_FAILED:
4638		isp_prt(isp, ISP_LOGERR, "ABORT OPERATION rejected by %d.%d.%d",
4639		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4640		break;
4641
4642	case RQCS_REJECT_MSG_FAILED:
4643		isp_prt(isp, ISP_LOGERR, "MESSAGE REJECT rejected by %d.%d.%d",
4644		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4645		break;
4646
4647	case RQCS_NOP_MSG_FAILED:
4648		isp_prt(isp, ISP_LOGERR, "NOP rejected by %d.%d.%d",
4649		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4650		break;
4651
4652	case RQCS_PARITY_ERROR_MSG_FAILED:
4653		isp_prt(isp, ISP_LOGERR,
4654		    "MESSAGE PARITY ERROR rejected by %d.%d.%d",
4655		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4656		break;
4657
4658	case RQCS_DEVICE_RESET_MSG_FAILED:
4659		isp_prt(isp, ISP_LOGWARN,
4660		    "BUS DEVICE RESET rejected by %d.%d.%d",
4661		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4662		break;
4663
4664	case RQCS_ID_MSG_FAILED:
4665		isp_prt(isp, ISP_LOGERR, "IDENTIFY rejected by %d.%d.%d",
4666		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4667		break;
4668
4669	case RQCS_UNEXP_BUS_FREE:
4670		isp_prt(isp, ISP_LOGERR, "%d.%d.%d had an unexpected bus free",
4671		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4672		break;
4673
4674	case RQCS_DATA_UNDERRUN:
4675	{
4676		if (IS_FC(isp)) {
4677			int ru_marked = (sp->req_scsi_status & RQCS_RU) != 0;
4678			if (!ru_marked || sp->req_resid > XS_XFRLEN(xs)) {
4679				isp_prt(isp, ISP_LOGWARN, bun, XS_TGT(xs),
4680				    XS_LUN(xs), XS_XFRLEN(xs), sp->req_resid,
4681				    (ru_marked)? "marked" : "not marked");
4682				if (XS_NOERR(xs)) {
4683					XS_SETERR(xs, HBA_BOTCH);
4684				}
4685				return;
4686			}
4687		}
4688		XS_RESID(xs) = sp->req_resid;
4689		if (XS_NOERR(xs)) {
4690			XS_SETERR(xs, HBA_NOERROR);
4691		}
4692		return;
4693	}
4694
4695	case RQCS_XACT_ERR1:
4696		isp_prt(isp, ISP_LOGERR, xact1, XS_CHANNEL(xs),
4697		    XS_TGT(xs), XS_LUN(xs));
4698		break;
4699
4700	case RQCS_XACT_ERR2:
4701		isp_prt(isp, ISP_LOGERR, xact2,
4702		    XS_LUN(xs), XS_TGT(xs), XS_CHANNEL(xs));
4703		break;
4704
4705	case RQCS_XACT_ERR3:
4706		isp_prt(isp, ISP_LOGERR, xact3,
4707		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4708		break;
4709
4710	case RQCS_BAD_ENTRY:
4711		isp_prt(isp, ISP_LOGERR, "Invalid IOCB entry type detected");
4712		break;
4713
4714	case RQCS_QUEUE_FULL:
4715		isp_prt(isp, ISP_LOGDEBUG0,
4716		    "internal queues full for %d.%d.%d status 0x%x",
4717		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), *XS_STSP(xs));
4718
4719		/*
4720		 * If QFULL or some other status byte is set, then this
4721		 * isn't an error, per se.
4722		 *
4723		 * Unfortunately, some QLogic f/w writers have, in
4724		 * some cases, ommitted to *set* status to QFULL.
4725		 *
4726
4727		if (*XS_STSP(xs) != SCSI_GOOD && XS_NOERR(xs)) {
4728			XS_SETERR(xs, HBA_NOERROR);
4729			return;
4730		}
4731
4732		 *
4733		 *
4734		 */
4735
4736		*XS_STSP(xs) = SCSI_QFULL;
4737		XS_SETERR(xs, HBA_NOERROR);
4738		return;
4739
4740	case RQCS_PHASE_SKIPPED:
4741		isp_prt(isp, ISP_LOGERR, pskip, XS_CHANNEL(xs),
4742		    XS_TGT(xs), XS_LUN(xs));
4743		break;
4744
4745	case RQCS_ARQS_FAILED:
4746		isp_prt(isp, ISP_LOGERR,
4747		    "Auto Request Sense failed for %d.%d.%d",
4748		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4749		if (XS_NOERR(xs)) {
4750			XS_SETERR(xs, HBA_ARQFAIL);
4751		}
4752		return;
4753
4754	case RQCS_WIDE_FAILED:
4755		isp_prt(isp, ISP_LOGERR,
4756		    "Wide Negotiation failed for %d.%d.%d",
4757		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
4758		if (IS_SCSI(isp)) {
4759			sdparam *sdp = isp->isp_param;
4760			sdp += XS_CHANNEL(xs);
4761			sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_WIDE;
4762			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
4763			isp->isp_update |= (1 << XS_CHANNEL(xs));
4764		}
4765		if (XS_NOERR(xs)) {
4766			XS_SETERR(xs, HBA_NOERROR);
4767		}
4768		return;
4769
4770	case RQCS_SYNCXFER_FAILED:
4771		isp_prt(isp, ISP_LOGERR,
4772		    "SDTR Message failed for target %d.%d.%d",
4773		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
4774		if (IS_SCSI(isp)) {
4775			sdparam *sdp = isp->isp_param;
4776			sdp += XS_CHANNEL(xs);
4777			sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_SYNC;
4778			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
4779			isp->isp_update |= (1 << XS_CHANNEL(xs));
4780		}
4781		break;
4782
4783	case RQCS_LVD_BUSERR:
4784		isp_prt(isp, ISP_LOGERR,
4785		    "Bad LVD condition while talking to %d.%d.%d",
4786		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
4787		break;
4788
4789	case RQCS_PORT_UNAVAILABLE:
4790		/*
4791		 * No such port on the loop. Moral equivalent of SELTIMEO
4792		 */
4793	case RQCS_PORT_LOGGED_OUT:
4794		/*
4795		 * It was there (maybe)- treat as a selection timeout.
4796		 */
4797		if ((sp->req_completion_status & 0xff) == RQCS_PORT_UNAVAILABLE)
4798			isp_prt(isp, ISP_LOGINFO,
4799			    "port unavailable for target %d", XS_TGT(xs));
4800		else
4801			isp_prt(isp, ISP_LOGINFO,
4802			    "port logout for target %d", XS_TGT(xs));
4803		/*
4804		 * If we're on a local loop, force a LIP (which is overkill)
4805		 * to force a re-login of this unit. If we're on fabric,
4806		 * then we'll have to relogin as a matter of course.
4807		 */
4808		if (FCPARAM(isp)->isp_topo == TOPO_NL_PORT ||
4809		    FCPARAM(isp)->isp_topo == TOPO_FL_PORT) {
4810			mbreg_t mbs;
4811			MEMZERO(&mbs, sizeof (mbs));
4812			mbs.param[0] = MBOX_INIT_LIP;
4813			isp_mboxcmd_qnw(isp, &mbs, 1);
4814		}
4815
4816		/*
4817		 * Probably overkill.
4818		 */
4819		isp->isp_sendmarker = 1;
4820		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
4821		isp_mark_getpdb_all(isp);
4822		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
4823		if (XS_NOERR(xs)) {
4824			XS_SETERR(xs, HBA_SELTIMEOUT);
4825		}
4826		return;
4827
4828	case RQCS_PORT_CHANGED:
4829		isp_prt(isp, ISP_LOGWARN,
4830		    "port changed for target %d", XS_TGT(xs));
4831		if (XS_NOERR(xs)) {
4832			XS_SETERR(xs, HBA_SELTIMEOUT);
4833		}
4834		return;
4835
4836	case RQCS_PORT_BUSY:
4837		isp_prt(isp, ISP_LOGWARN,
4838		    "port busy for target %d", XS_TGT(xs));
4839		if (XS_NOERR(xs)) {
4840			XS_SETERR(xs, HBA_TGTBSY);
4841		}
4842		return;
4843
4844	default:
4845		isp_prt(isp, ISP_LOGERR, "Unknown Completion Status 0x%x",
4846		    sp->req_completion_status);
4847		break;
4848	}
4849	if (XS_NOERR(xs)) {
4850		XS_SETERR(xs, HBA_BOTCH);
4851	}
4852}
4853
4854static void
4855isp_fastpost_complete(ispsoftc_t *isp, uint16_t fph)
4856{
4857	XS_T *xs;
4858
4859	if (fph == 0) {
4860		return;
4861	}
4862	xs = isp_find_xs(isp, fph);
4863	if (xs == NULL) {
4864		isp_prt(isp, ISP_LOGWARN,
4865		    "Command for fast post handle 0x%x not found", fph);
4866		return;
4867	}
4868	isp_destroy_handle(isp, fph);
4869
4870	/*
4871	 * Since we don't have a result queue entry item,
4872	 * we must believe that SCSI status is zero and
4873	 * that all data transferred.
4874	 */
4875	XS_SET_STATE_STAT(isp, xs, NULL);
4876	XS_RESID(xs) = 0;
4877	*XS_STSP(xs) = SCSI_GOOD;
4878	if (XS_XFRLEN(xs)) {
4879		ISP_DMAFREE(isp, xs, fph);
4880	}
4881	if (isp->isp_nactive)
4882		isp->isp_nactive--;
4883	isp->isp_fphccmplt++;
4884	isp_done(xs);
4885}
4886
4887static int
4888isp_mbox_continue(ispsoftc_t *isp)
4889{
4890	mbreg_t mbs;
4891	uint16_t *ptr;
4892
4893	switch (isp->isp_lastmbxcmd) {
4894	case MBOX_WRITE_RAM_WORD:
4895	case MBOX_READ_RAM_WORD:
4896	case MBOX_READ_RAM_WORD_EXTENDED:
4897		break;
4898	default:
4899		return (1);
4900	}
4901	if (isp->isp_mboxtmp[0] != MBOX_COMMAND_COMPLETE) {
4902		isp->isp_mbxwrk0 = 0;
4903		return (-1);
4904	}
4905
4906	/*
4907	 * Clear the previous interrupt.
4908	 */
4909	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
4910	ISP_WRITE(isp, BIU_SEMA, 0);
4911
4912	/*
4913	 * Continue with next word.
4914	 */
4915	MEMZERO(&mbs, sizeof (mbs));
4916	ptr = isp->isp_mbxworkp;
4917	switch (isp->isp_lastmbxcmd) {
4918	case MBOX_WRITE_RAM_WORD:
4919		mbs.param[2] = *ptr++;
4920		mbs.param[1] = isp->isp_mbxwrk1++;
4921		break;
4922	case MBOX_READ_RAM_WORD:
4923	case MBOX_READ_RAM_WORD_EXTENDED:
4924		*ptr++ = isp->isp_mboxtmp[2];
4925		mbs.param[1] = isp->isp_mbxwrk1++;
4926		break;
4927	}
4928	isp->isp_mbxworkp = ptr;
4929	mbs.param[0] = isp->isp_lastmbxcmd;
4930	isp->isp_mbxwrk0 -= 1;
4931	isp_mboxcmd_qnw(isp, &mbs, 0);
4932	return (0);
4933}
4934
4935
4936#define	HIWRD(x)			((x) >> 16)
4937#define	LOWRD(x)			((x)  & 0xffff)
4938#define	ISPOPMAP(a, b)			(((a) << 16) | (b))
4939static const uint32_t mbpscsi[] = {
4940	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
4941	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
4942	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
4943	ISPOPMAP(0x1f, 0x01),	/* 0x03: MBOX_DUMP_RAM */
4944	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
4945	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
4946	ISPOPMAP(0x3f, 0x3f),	/* 0x06: MBOX_MAILBOX_REG_TEST */
4947	ISPOPMAP(0x03, 0x07),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
4948	ISPOPMAP(0x01, 0x0f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
4949	ISPOPMAP(0x00, 0x00),	/* 0x09: */
4950	ISPOPMAP(0x00, 0x00),	/* 0x0a: */
4951	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
4952	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
4953	ISPOPMAP(0x00, 0x00),	/* 0x0d: */
4954	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
4955	ISPOPMAP(0x00, 0x00),	/* 0x0f: */
4956	ISPOPMAP(0x1f, 0x1f),	/* 0x10: MBOX_INIT_REQ_QUEUE */
4957	ISPOPMAP(0x3f, 0x3f),	/* 0x11: MBOX_INIT_RES_QUEUE */
4958	ISPOPMAP(0x0f, 0x0f),	/* 0x12: MBOX_EXECUTE_IOCB */
4959	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
4960	ISPOPMAP(0x01, 0x3f),	/* 0x14: MBOX_STOP_FIRMWARE */
4961	ISPOPMAP(0x0f, 0x0f),	/* 0x15: MBOX_ABORT */
4962	ISPOPMAP(0x03, 0x03),	/* 0x16: MBOX_ABORT_DEVICE */
4963	ISPOPMAP(0x07, 0x07),	/* 0x17: MBOX_ABORT_TARGET */
4964	ISPOPMAP(0x07, 0x07),	/* 0x18: MBOX_BUS_RESET */
4965	ISPOPMAP(0x03, 0x07),	/* 0x19: MBOX_STOP_QUEUE */
4966	ISPOPMAP(0x03, 0x07),	/* 0x1a: MBOX_START_QUEUE */
4967	ISPOPMAP(0x03, 0x07),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
4968	ISPOPMAP(0x03, 0x07),	/* 0x1c: MBOX_ABORT_QUEUE */
4969	ISPOPMAP(0x03, 0x4f),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
4970	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
4971	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
4972	ISPOPMAP(0x01, 0x07),	/* 0x20: MBOX_GET_INIT_SCSI_ID */
4973	ISPOPMAP(0x01, 0x07),	/* 0x21: MBOX_GET_SELECT_TIMEOUT */
4974	ISPOPMAP(0x01, 0xc7),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
4975	ISPOPMAP(0x01, 0x07),	/* 0x23: MBOX_GET_TAG_AGE_LIMIT */
4976	ISPOPMAP(0x01, 0x03),	/* 0x24: MBOX_GET_CLOCK_RATE */
4977	ISPOPMAP(0x01, 0x07),	/* 0x25: MBOX_GET_ACT_NEG_STATE */
4978	ISPOPMAP(0x01, 0x07),	/* 0x26: MBOX_GET_ASYNC_DATA_SETUP_TIME */
4979	ISPOPMAP(0x01, 0x07),	/* 0x27: MBOX_GET_PCI_PARAMS */
4980	ISPOPMAP(0x03, 0x4f),	/* 0x28: MBOX_GET_TARGET_PARAMS */
4981	ISPOPMAP(0x03, 0x0f),	/* 0x29: MBOX_GET_DEV_QUEUE_PARAMS */
4982	ISPOPMAP(0x01, 0x07),	/* 0x2a: MBOX_GET_RESET_DELAY_PARAMS */
4983	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
4984	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
4985	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
4986	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
4987	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
4988	ISPOPMAP(0x03, 0x03),	/* 0x30: MBOX_SET_INIT_SCSI_ID */
4989	ISPOPMAP(0x07, 0x07),	/* 0x31: MBOX_SET_SELECT_TIMEOUT */
4990	ISPOPMAP(0xc7, 0xc7),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
4991	ISPOPMAP(0x07, 0x07),	/* 0x33: MBOX_SET_TAG_AGE_LIMIT */
4992	ISPOPMAP(0x03, 0x03),	/* 0x34: MBOX_SET_CLOCK_RATE */
4993	ISPOPMAP(0x07, 0x07),	/* 0x35: MBOX_SET_ACT_NEG_STATE */
4994	ISPOPMAP(0x07, 0x07),	/* 0x36: MBOX_SET_ASYNC_DATA_SETUP_TIME */
4995	ISPOPMAP(0x07, 0x07),	/* 0x37: MBOX_SET_PCI_CONTROL_PARAMS */
4996	ISPOPMAP(0x4f, 0x4f),	/* 0x38: MBOX_SET_TARGET_PARAMS */
4997	ISPOPMAP(0x0f, 0x0f),	/* 0x39: MBOX_SET_DEV_QUEUE_PARAMS */
4998	ISPOPMAP(0x07, 0x07),	/* 0x3a: MBOX_SET_RESET_DELAY_PARAMS */
4999	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
5000	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
5001	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
5002	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
5003	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
5004	ISPOPMAP(0x01, 0x03),	/* 0x40: MBOX_RETURN_BIOS_BLOCK_ADDR */
5005	ISPOPMAP(0x3f, 0x01),	/* 0x41: MBOX_WRITE_FOUR_RAM_WORDS */
5006	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_EXEC_BIOS_IOCB */
5007	ISPOPMAP(0x00, 0x00),	/* 0x43: */
5008	ISPOPMAP(0x00, 0x00),	/* 0x44: */
5009	ISPOPMAP(0x03, 0x03),	/* 0x45: SET SYSTEM PARAMETER */
5010	ISPOPMAP(0x01, 0x03),	/* 0x46: GET SYSTEM PARAMETER */
5011	ISPOPMAP(0x00, 0x00),	/* 0x47: */
5012	ISPOPMAP(0x01, 0xcf),	/* 0x48: GET SCAM CONFIGURATION */
5013	ISPOPMAP(0xcf, 0xcf),	/* 0x49: SET SCAM CONFIGURATION */
5014	ISPOPMAP(0x03, 0x03),	/* 0x4a: MBOX_SET_FIRMWARE_FEATURES */
5015	ISPOPMAP(0x01, 0x03),	/* 0x4b: MBOX_GET_FIRMWARE_FEATURES */
5016	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
5017	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
5018	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
5019	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
5020	ISPOPMAP(0xdf, 0xdf),	/* 0x50: LOAD RAM A64 */
5021	ISPOPMAP(0xdf, 0xdf),	/* 0x51: DUMP RAM A64 */
5022	ISPOPMAP(0xdf, 0xff),	/* 0x52: INITIALIZE REQUEST QUEUE A64 */
5023	ISPOPMAP(0xef, 0xff),	/* 0x53: INITIALIZE RESPONSE QUEUE A64 */
5024	ISPOPMAP(0xcf, 0x01),	/* 0x54: EXECUTE IOCB A64 */
5025	ISPOPMAP(0x07, 0x01),	/* 0x55: ENABLE TARGET MODE */
5026	ISPOPMAP(0x03, 0x0f),	/* 0x56: GET TARGET STATUS */
5027	ISPOPMAP(0x00, 0x00),	/* 0x57: */
5028	ISPOPMAP(0x00, 0x00),	/* 0x58: */
5029	ISPOPMAP(0x00, 0x00),	/* 0x59: */
5030	ISPOPMAP(0x03, 0x03),	/* 0x5a: SET DATA OVERRUN RECOVERY MODE */
5031	ISPOPMAP(0x01, 0x03),	/* 0x5b: GET DATA OVERRUN RECOVERY MODE */
5032	ISPOPMAP(0x0f, 0x0f),	/* 0x5c: SET HOST DATA */
5033	ISPOPMAP(0x01, 0x01)	/* 0x5d: GET NOST DATA */
5034};
5035
5036#ifndef	ISP_STRIPPED
5037static char *scsi_mbcmd_names[] = {
5038	"NO-OP",
5039	"LOAD RAM",
5040	"EXEC FIRMWARE",
5041	"DUMP RAM",
5042	"WRITE RAM WORD",
5043	"READ RAM WORD",
5044	"MAILBOX REG TEST",
5045	"VERIFY CHECKSUM",
5046	"ABOUT FIRMWARE",
5047	NULL,
5048	NULL,
5049	NULL,
5050	NULL,
5051	NULL,
5052	"CHECK FIRMWARE",
5053	NULL,
5054	"INIT REQUEST QUEUE",
5055	"INIT RESULT QUEUE",
5056	"EXECUTE IOCB",
5057	"WAKE UP",
5058	"STOP FIRMWARE",
5059	"ABORT",
5060	"ABORT DEVICE",
5061	"ABORT TARGET",
5062	"BUS RESET",
5063	"STOP QUEUE",
5064	"START QUEUE",
5065	"SINGLE STEP QUEUE",
5066	"ABORT QUEUE",
5067	"GET DEV QUEUE STATUS",
5068	NULL,
5069	"GET FIRMWARE STATUS",
5070	"GET INIT SCSI ID",
5071	"GET SELECT TIMEOUT",
5072	"GET RETRY COUNT",
5073	"GET TAG AGE LIMIT",
5074	"GET CLOCK RATE",
5075	"GET ACT NEG STATE",
5076	"GET ASYNC DATA SETUP TIME",
5077	"GET PCI PARAMS",
5078	"GET TARGET PARAMS",
5079	"GET DEV QUEUE PARAMS",
5080	"GET RESET DELAY PARAMS",
5081	NULL,
5082	NULL,
5083	NULL,
5084	NULL,
5085	NULL,
5086	"SET INIT SCSI ID",
5087	"SET SELECT TIMEOUT",
5088	"SET RETRY COUNT",
5089	"SET TAG AGE LIMIT",
5090	"SET CLOCK RATE",
5091	"SET ACT NEG STATE",
5092	"SET ASYNC DATA SETUP TIME",
5093	"SET PCI CONTROL PARAMS",
5094	"SET TARGET PARAMS",
5095	"SET DEV QUEUE PARAMS",
5096	"SET RESET DELAY PARAMS",
5097	NULL,
5098	NULL,
5099	NULL,
5100	NULL,
5101	NULL,
5102	"RETURN BIOS BLOCK ADDR",
5103	"WRITE FOUR RAM WORDS",
5104	"EXEC BIOS IOCB",
5105	NULL,
5106	NULL,
5107	"SET SYSTEM PARAMETER",
5108	"GET SYSTEM PARAMETER",
5109	NULL,
5110	"GET SCAM CONFIGURATION",
5111	"SET SCAM CONFIGURATION",
5112	"SET FIRMWARE FEATURES",
5113	"GET FIRMWARE FEATURES",
5114	NULL,
5115	NULL,
5116	NULL,
5117	NULL,
5118	"LOAD RAM A64",
5119	"DUMP RAM A64",
5120	"INITIALIZE REQUEST QUEUE A64",
5121	"INITIALIZE RESPONSE QUEUE A64",
5122	"EXECUTE IOCB A64",
5123	"ENABLE TARGET MODE",
5124	"GET TARGET MODE STATE",
5125	NULL,
5126	NULL,
5127	NULL,
5128	"SET DATA OVERRUN RECOVERY MODE",
5129	"GET DATA OVERRUN RECOVERY MODE",
5130	"SET HOST DATA",
5131	"GET NOST DATA",
5132};
5133#endif
5134
5135static const uint32_t mbpfc[] = {
5136	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
5137	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
5138	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
5139	ISPOPMAP(0xdf, 0x01),	/* 0x03: MBOX_DUMP_RAM */
5140	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
5141	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
5142	ISPOPMAP(0xff, 0xff),	/* 0x06: MBOX_MAILBOX_REG_TEST */
5143	ISPOPMAP(0x03, 0x05),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
5144	ISPOPMAP(0x01, 0x4f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
5145	ISPOPMAP(0xdf, 0x01),	/* 0x09: LOAD RAM */
5146	ISPOPMAP(0xdf, 0x01),	/* 0x0a: DUMP RAM */
5147	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
5148	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
5149	ISPOPMAP(0x00, 0x00),	/* 0x0d: */
5150	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
5151	ISPOPMAP(0x03, 0x07),	/* 0x0f: MBOX_READ_RAM_WORD_EXTENDED(1) */
5152	ISPOPMAP(0x1f, 0x11),	/* 0x10: MBOX_INIT_REQ_QUEUE */
5153	ISPOPMAP(0x2f, 0x21),	/* 0x11: MBOX_INIT_RES_QUEUE */
5154	ISPOPMAP(0x0f, 0x01),	/* 0x12: MBOX_EXECUTE_IOCB */
5155	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
5156	ISPOPMAP(0x01, 0xff),	/* 0x14: MBOX_STOP_FIRMWARE */
5157	ISPOPMAP(0x4f, 0x01),	/* 0x15: MBOX_ABORT */
5158	ISPOPMAP(0x07, 0x01),	/* 0x16: MBOX_ABORT_DEVICE */
5159	ISPOPMAP(0x07, 0x01),	/* 0x17: MBOX_ABORT_TARGET */
5160	ISPOPMAP(0x03, 0x03),	/* 0x18: MBOX_BUS_RESET */
5161	ISPOPMAP(0x07, 0x05),	/* 0x19: MBOX_STOP_QUEUE */
5162	ISPOPMAP(0x07, 0x05),	/* 0x1a: MBOX_START_QUEUE */
5163	ISPOPMAP(0x07, 0x05),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
5164	ISPOPMAP(0x07, 0x05),	/* 0x1c: MBOX_ABORT_QUEUE */
5165	ISPOPMAP(0x07, 0x03),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
5166	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
5167	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
5168	ISPOPMAP(0x01, 0x4f),	/* 0x20: MBOX_GET_LOOP_ID */
5169	ISPOPMAP(0x00, 0x00),	/* 0x21: */
5170	ISPOPMAP(0x01, 0x07),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
5171	ISPOPMAP(0x00, 0x00),	/* 0x23: */
5172	ISPOPMAP(0x00, 0x00),	/* 0x24: */
5173	ISPOPMAP(0x00, 0x00),	/* 0x25: */
5174	ISPOPMAP(0x00, 0x00),	/* 0x26: */
5175	ISPOPMAP(0x00, 0x00),	/* 0x27: */
5176	ISPOPMAP(0x01, 0x03),	/* 0x28: MBOX_GET_FIRMWARE_OPTIONS */
5177	ISPOPMAP(0x03, 0x07),	/* 0x29: MBOX_GET_PORT_QUEUE_PARAMS */
5178	ISPOPMAP(0x00, 0x00),	/* 0x2a: */
5179	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
5180	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
5181	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
5182	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
5183	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
5184	ISPOPMAP(0x00, 0x00),	/* 0x30: */
5185	ISPOPMAP(0x00, 0x00),	/* 0x31: */
5186	ISPOPMAP(0x07, 0x07),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
5187	ISPOPMAP(0x00, 0x00),	/* 0x33: */
5188	ISPOPMAP(0x00, 0x00),	/* 0x34: */
5189	ISPOPMAP(0x00, 0x00),	/* 0x35: */
5190	ISPOPMAP(0x00, 0x00),	/* 0x36: */
5191	ISPOPMAP(0x00, 0x00),	/* 0x37: */
5192	ISPOPMAP(0x0f, 0x01),	/* 0x38: MBOX_SET_FIRMWARE_OPTIONS */
5193	ISPOPMAP(0x0f, 0x07),	/* 0x39: MBOX_SET_PORT_QUEUE_PARAMS */
5194	ISPOPMAP(0x00, 0x00),	/* 0x3a: */
5195	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
5196	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
5197	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
5198	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
5199	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
5200	ISPOPMAP(0x03, 0x01),	/* 0x40: MBOX_LOOP_PORT_BYPASS */
5201	ISPOPMAP(0x03, 0x01),	/* 0x41: MBOX_LOOP_PORT_ENABLE */
5202	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_GET_RESOURCE_COUNTS */
5203	ISPOPMAP(0x01, 0x01),	/* 0x43: MBOX_REQUEST_NON_PARTICIPATING_MODE */
5204	ISPOPMAP(0x00, 0x00),	/* 0x44: */
5205	ISPOPMAP(0x00, 0x00),	/* 0x45: */
5206	ISPOPMAP(0x00, 0x00),	/* 0x46: */
5207	ISPOPMAP(0xcf, 0x03),	/* 0x47: GET PORT_DATABASE ENHANCED */
5208	ISPOPMAP(0x00, 0x00),	/* 0x48: */
5209	ISPOPMAP(0x00, 0x00),	/* 0x49: */
5210	ISPOPMAP(0x00, 0x00),	/* 0x4a: */
5211	ISPOPMAP(0x00, 0x00),	/* 0x4b: */
5212	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
5213	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
5214	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
5215	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
5216	ISPOPMAP(0x00, 0x00),	/* 0x50: */
5217	ISPOPMAP(0x00, 0x00),	/* 0x51: */
5218	ISPOPMAP(0x00, 0x00),	/* 0x52: */
5219	ISPOPMAP(0x00, 0x00),	/* 0x53: */
5220	ISPOPMAP(0xcf, 0x01),	/* 0x54: EXECUTE IOCB A64 */
5221	ISPOPMAP(0x00, 0x00),	/* 0x55: */
5222	ISPOPMAP(0x00, 0x00),	/* 0x56: */
5223	ISPOPMAP(0x00, 0x00),	/* 0x57: */
5224	ISPOPMAP(0x00, 0x00),	/* 0x58: */
5225	ISPOPMAP(0x00, 0x00),	/* 0x59: */
5226	ISPOPMAP(0x00, 0x00),	/* 0x5a: */
5227	ISPOPMAP(0x03, 0x01),	/* 0x5b: MBOX_DRIVER_HEARTBEAT */
5228	ISPOPMAP(0xcf, 0x01),	/* 0x5c: MBOX_FW_HEARTBEAT */
5229	ISPOPMAP(0x07, 0x03),	/* 0x5d: MBOX_GET_SET_DATA_RATE */
5230	ISPOPMAP(0x00, 0x00),	/* 0x5e: */
5231	ISPOPMAP(0x00, 0x00),	/* 0x5f: */
5232	ISPOPMAP(0xfd, 0x31),	/* 0x60: MBOX_INIT_FIRMWARE */
5233	ISPOPMAP(0x00, 0x00),	/* 0x61: */
5234	ISPOPMAP(0x01, 0x01),	/* 0x62: MBOX_INIT_LIP */
5235	ISPOPMAP(0xcd, 0x03),	/* 0x63: MBOX_GET_FC_AL_POSITION_MAP */
5236	ISPOPMAP(0xcf, 0x01),	/* 0x64: MBOX_GET_PORT_DB */
5237	ISPOPMAP(0x07, 0x01),	/* 0x65: MBOX_CLEAR_ACA */
5238	ISPOPMAP(0x07, 0x01),	/* 0x66: MBOX_TARGET_RESET */
5239	ISPOPMAP(0x07, 0x01),	/* 0x67: MBOX_CLEAR_TASK_SET */
5240	ISPOPMAP(0x07, 0x01),	/* 0x68: MBOX_ABORT_TASK_SET */
5241	ISPOPMAP(0x01, 0x07),	/* 0x69: MBOX_GET_FW_STATE */
5242	ISPOPMAP(0x03, 0xcf),	/* 0x6a: MBOX_GET_PORT_NAME */
5243	ISPOPMAP(0xcf, 0x01),	/* 0x6b: MBOX_GET_LINK_STATUS */
5244	ISPOPMAP(0x0f, 0x01),	/* 0x6c: MBOX_INIT_LIP_RESET */
5245	ISPOPMAP(0x00, 0x00),	/* 0x6d: */
5246	ISPOPMAP(0xcf, 0x03),	/* 0x6e: MBOX_SEND_SNS */
5247	ISPOPMAP(0x0f, 0x07),	/* 0x6f: MBOX_FABRIC_LOGIN */
5248	ISPOPMAP(0x03, 0x01),	/* 0x70: MBOX_SEND_CHANGE_REQUEST */
5249	ISPOPMAP(0x03, 0x03),	/* 0x71: MBOX_FABRIC_LOGOUT */
5250	ISPOPMAP(0x0f, 0x0f),	/* 0x72: MBOX_INIT_LIP_LOGIN */
5251	ISPOPMAP(0x00, 0x00),	/* 0x73: */
5252	ISPOPMAP(0x07, 0x01),	/* 0x74: LOGIN LOOP PORT */
5253	ISPOPMAP(0xcf, 0x03),	/* 0x75: GET PORT/NODE NAME LIST */
5254	ISPOPMAP(0x4f, 0x01),	/* 0x76: SET VENDOR ID */
5255	ISPOPMAP(0xcd, 0x01),	/* 0x77: INITIALIZE IP MAILBOX */
5256	ISPOPMAP(0x00, 0x00),	/* 0x78: */
5257	ISPOPMAP(0x00, 0x00),	/* 0x79: */
5258	ISPOPMAP(0x00, 0x00),	/* 0x7a: */
5259	ISPOPMAP(0x00, 0x00),	/* 0x7b: */
5260	ISPOPMAP(0x4f, 0x03),	/* 0x7c: Get ID List */
5261	ISPOPMAP(0xcf, 0x01),	/* 0x7d: SEND LFA */
5262	ISPOPMAP(0x0f, 0x01)	/* 0x7e: LUN RESET */
5263};
5264/*
5265 * Footnotes
5266 *
5267 * (1): this sets bits 21..16 in mailbox register #8, which we nominally
5268 *	do not access at this time in the core driver. The caller is
5269 *	responsible for setting this register first (Gross!).
5270 */
5271
5272#ifndef	ISP_STRIPPED
5273static char *fc_mbcmd_names[] = {
5274	"NO-OP",
5275	"LOAD RAM",
5276	"EXEC FIRMWARE",
5277	"DUMP RAM",
5278	"WRITE RAM WORD",
5279	"READ RAM WORD",
5280	"MAILBOX REG TEST",
5281	"VERIFY CHECKSUM",
5282	"ABOUT FIRMWARE",
5283	"LOAD RAM",
5284	"DUMP RAM",
5285	NULL,
5286	NULL,
5287	"READ RAM WORD EXTENDED",
5288	"CHECK FIRMWARE",
5289	NULL,
5290	"INIT REQUEST QUEUE",
5291	"INIT RESULT QUEUE",
5292	"EXECUTE IOCB",
5293	"WAKE UP",
5294	"STOP FIRMWARE",
5295	"ABORT",
5296	"ABORT DEVICE",
5297	"ABORT TARGET",
5298	"BUS RESET",
5299	"STOP QUEUE",
5300	"START QUEUE",
5301	"SINGLE STEP QUEUE",
5302	"ABORT QUEUE",
5303	"GET DEV QUEUE STATUS",
5304	NULL,
5305	"GET FIRMWARE STATUS",
5306	"GET LOOP ID",
5307	NULL,
5308	"GET RETRY COUNT",
5309	NULL,
5310	NULL,
5311	NULL,
5312	NULL,
5313	NULL,
5314	"GET FIRMWARE OPTIONS",
5315	"GET PORT QUEUE PARAMS",
5316	NULL,
5317	NULL,
5318	NULL,
5319	NULL,
5320	NULL,
5321	NULL,
5322	NULL,
5323	NULL,
5324	"SET RETRY COUNT",
5325	NULL,
5326	NULL,
5327	NULL,
5328	NULL,
5329	NULL,
5330	"SET FIRMWARE OPTIONS",
5331	"SET PORT QUEUE PARAMS",
5332	NULL,
5333	NULL,
5334	NULL,
5335	NULL,
5336	NULL,
5337	NULL,
5338	"LOOP PORT BYPASS",
5339	"LOOP PORT ENABLE",
5340	"GET RESOURCE COUNTS",
5341	"REQUEST NON PARTICIPATING MODE",
5342	NULL,
5343	NULL,
5344	NULL,
5345	"GET PORT DATABASE,, ENHANCED",
5346	NULL,
5347	NULL,
5348	NULL,
5349	NULL,
5350	NULL,
5351	NULL,
5352	NULL,
5353	NULL,
5354	NULL,
5355	NULL,
5356	NULL,
5357	NULL,
5358	"EXECUTE IOCB A64",
5359	NULL,
5360	NULL,
5361	NULL,
5362	NULL,
5363	NULL,
5364	NULL,
5365	"DRIVER HEARTBEAT",
5366	NULL,
5367	"GET/SET DATA RATE",
5368	NULL,
5369	NULL,
5370	"INIT FIRMWARE",
5371	NULL,
5372	"INIT LIP",
5373	"GET FC-AL POSITION MAP",
5374	"GET PORT DATABASE",
5375	"CLEAR ACA",
5376	"TARGET RESET",
5377	"CLEAR TASK SET",
5378	"ABORT TASK SET",
5379	"GET FW STATE",
5380	"GET PORT NAME",
5381	"GET LINK STATUS",
5382	"INIT LIP RESET",
5383	NULL,
5384	"SEND SNS",
5385	"FABRIC LOGIN",
5386	"SEND CHANGE REQUEST",
5387	"FABRIC LOGOUT",
5388	"INIT LIP LOGIN",
5389	NULL,
5390	"LOGIN LOOP PORT",
5391	"GET PORT/NODE NAME LIST",
5392	"SET VENDOR ID",
5393	"INITIALIZE IP MAILBOX",
5394	NULL,
5395	NULL,
5396	NULL,
5397	NULL,
5398	"Get ID List",
5399	"SEND LFA",
5400	"Lun RESET"
5401};
5402#endif
5403
5404static void
5405isp_mboxcmd_qnw(ispsoftc_t *isp, mbreg_t *mbp, int nodelay)
5406{
5407	unsigned int ibits, obits, box, opcode;
5408	const uint32_t *mcp;
5409
5410	if (IS_FC(isp)) {
5411		mcp = mbpfc;
5412	} else {
5413		mcp = mbpscsi;
5414	}
5415	opcode = mbp->param[0];
5416	ibits = HIWRD(mcp[opcode]) & NMBOX_BMASK(isp);
5417	obits = LOWRD(mcp[opcode]) & NMBOX_BMASK(isp);
5418	ibits |= mbp->ibits;
5419	obits |= mbp->obits;
5420	for (box = 0; box < MAX_MAILBOX(isp); box++) {
5421		if (ibits & (1 << box)) {
5422			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
5423		}
5424		if (nodelay == 0) {
5425			isp->isp_mboxtmp[box] = mbp->param[box] = 0;
5426		}
5427	}
5428	if (nodelay == 0) {
5429		isp->isp_lastmbxcmd = opcode;
5430		isp->isp_obits = obits;
5431		isp->isp_mboxbsy = 1;
5432	}
5433	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
5434	/*
5435	 * Oddly enough, if we're not delaying for an answer,
5436	 * delay a bit to give the f/w a chance to pick up the
5437	 * command.
5438	 */
5439	if (nodelay) {
5440		USEC_DELAY(1000);
5441	}
5442}
5443
5444static void
5445isp_mboxcmd(ispsoftc_t *isp, mbreg_t *mbp, int logmask)
5446{
5447	char *cname, *xname, tname[16], mname[16];
5448	unsigned int lim, ibits, obits, box, opcode;
5449	const uint32_t *mcp;
5450
5451	if (IS_FC(isp)) {
5452		mcp = mbpfc;
5453		lim = (sizeof (mbpfc) / sizeof (mbpfc[0]));
5454	} else {
5455		mcp = mbpscsi;
5456		lim = (sizeof (mbpscsi) / sizeof (mbpscsi[0]));
5457	}
5458
5459	if ((opcode = mbp->param[0]) >= lim) {
5460		mbp->param[0] = MBOX_INVALID_COMMAND;
5461		isp_prt(isp, ISP_LOGERR, "Unknown Command 0x%x", opcode);
5462		return;
5463	}
5464
5465	ibits = HIWRD(mcp[opcode]) & NMBOX_BMASK(isp);
5466	obits = LOWRD(mcp[opcode]) & NMBOX_BMASK(isp);
5467
5468	ibits |= mbp->ibits;
5469	obits |= mbp->obits;
5470
5471	if (ibits == 0 && obits == 0) {
5472		mbp->param[0] = MBOX_COMMAND_PARAM_ERROR;
5473		isp_prt(isp, ISP_LOGERR, "no parameters for 0x%x", opcode);
5474		return;
5475	}
5476
5477	/*
5478	 * Get exclusive usage of mailbox registers.
5479	 */
5480	MBOX_ACQUIRE(isp);
5481
5482	for (box = 0; box < MAX_MAILBOX(isp); box++) {
5483		if (ibits & (1 << box)) {
5484			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
5485		}
5486		isp->isp_mboxtmp[box] = mbp->param[box] = 0;
5487	}
5488
5489	isp->isp_lastmbxcmd = opcode;
5490
5491	/*
5492	 * We assume that we can't overwrite a previous command.
5493	 */
5494	isp->isp_obits = obits;
5495	isp->isp_mboxbsy = 1;
5496
5497	/*
5498	 * Set Host Interrupt condition so that RISC will pick up mailbox regs.
5499	 */
5500	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
5501
5502	/*
5503	 * While we haven't finished the command, spin our wheels here.
5504	 */
5505	MBOX_WAIT_COMPLETE(isp);
5506
5507	if (isp->isp_mboxbsy) {
5508		/*
5509		 * Command timed out.
5510		 */
5511		isp->isp_mboxbsy = 0;
5512		MBOX_RELEASE(isp);
5513		return;
5514	}
5515
5516	/*
5517	 * Copy back output registers.
5518	 */
5519	for (box = 0; box < MAX_MAILBOX(isp); box++) {
5520		if (obits & (1 << box)) {
5521			mbp->param[box] = isp->isp_mboxtmp[box];
5522		}
5523	}
5524
5525	MBOX_RELEASE(isp);
5526
5527	if (logmask == 0 || opcode == MBOX_EXEC_FIRMWARE) {
5528		return;
5529	}
5530#ifdef	ISP_STRIPPED
5531	cname = NULL;
5532#else
5533	cname = (IS_FC(isp))? fc_mbcmd_names[opcode] : scsi_mbcmd_names[opcode];
5534#endif
5535	if (cname == NULL) {
5536		cname = tname;
5537		SNPRINTF(tname, sizeof tname, "opcode %x", opcode);
5538	}
5539
5540	/*
5541	 * Just to be chatty here...
5542	 */
5543	xname = NULL;
5544	switch (mbp->param[0]) {
5545	case MBOX_COMMAND_COMPLETE:
5546		break;
5547	case MBOX_INVALID_COMMAND:
5548		if (logmask & MBLOGMASK(MBOX_COMMAND_COMPLETE))
5549			xname = "INVALID COMMAND";
5550		break;
5551	case MBOX_HOST_INTERFACE_ERROR:
5552		if (logmask & MBLOGMASK(MBOX_HOST_INTERFACE_ERROR))
5553			xname = "HOST INTERFACE ERROR";
5554		break;
5555	case MBOX_TEST_FAILED:
5556		if (logmask & MBLOGMASK(MBOX_TEST_FAILED))
5557			xname = "TEST FAILED";
5558		break;
5559	case MBOX_COMMAND_ERROR:
5560		if (logmask & MBLOGMASK(MBOX_COMMAND_ERROR))
5561			xname = "COMMAND ERROR";
5562		break;
5563	case MBOX_COMMAND_PARAM_ERROR:
5564		if (logmask & MBLOGMASK(MBOX_COMMAND_PARAM_ERROR))
5565			xname = "COMMAND PARAMETER ERROR";
5566		break;
5567	case MBOX_LOOP_ID_USED:
5568		if (logmask & MBLOGMASK(MBOX_LOOP_ID_USED))
5569			xname = "LOOP ID ALREADY IN USE";
5570		break;
5571	case MBOX_PORT_ID_USED:
5572		if (logmask & MBLOGMASK(MBOX_PORT_ID_USED))
5573			xname = "PORT ID ALREADY IN USE";
5574		break;
5575	case MBOX_ALL_IDS_USED:
5576		if (logmask & MBLOGMASK(MBOX_ALL_IDS_USED))
5577			xname = "ALL LOOP IDS IN USE";
5578		break;
5579	case 0:		/* special case */
5580		xname = "TIMEOUT";
5581		break;
5582	default:
5583		SNPRINTF(mname, sizeof mname, "error 0x%x", mbp->param[0]);
5584		xname = mname;
5585		break;
5586	}
5587	if (xname)
5588		isp_prt(isp, ISP_LOGALL, "Mailbox Command '%s' failed (%s)",
5589		    cname, xname);
5590}
5591
5592static void
5593isp_fw_state(ispsoftc_t *isp)
5594{
5595	if (IS_FC(isp)) {
5596		mbreg_t mbs;
5597		fcparam *fcp = isp->isp_param;
5598
5599		MEMZERO(&mbs, sizeof (mbs));
5600		mbs.param[0] = MBOX_GET_FW_STATE;
5601		isp_mboxcmd(isp, &mbs, MBLOGALL);
5602		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
5603			fcp->isp_fwstate = mbs.param[1];
5604		}
5605	}
5606}
5607
5608static void
5609isp_update(ispsoftc_t *isp)
5610{
5611	int bus, upmask;
5612
5613	for (bus = 0, upmask = isp->isp_update; upmask != 0; bus++) {
5614		if (upmask & (1 << bus)) {
5615			isp_update_bus(isp, bus);
5616		}
5617		upmask &= ~(1 << bus);
5618	}
5619}
5620
5621static void
5622isp_update_bus(ispsoftc_t *isp, int bus)
5623{
5624	int tgt;
5625	mbreg_t mbs;
5626	sdparam *sdp;
5627
5628	isp->isp_update &= ~(1 << bus);
5629	if (IS_FC(isp)) {
5630		/*
5631		 * There are no 'per-bus' settings for Fibre Channel.
5632		 */
5633		return;
5634	}
5635	sdp = isp->isp_param;
5636	sdp += bus;
5637	MEMZERO(&mbs, sizeof (mbs));
5638
5639	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
5640		uint16_t flags, period, offset;
5641		int get;
5642
5643		if (sdp->isp_devparam[tgt].dev_enable == 0) {
5644			sdp->isp_devparam[tgt].dev_update = 0;
5645			sdp->isp_devparam[tgt].dev_refresh = 0;
5646			isp_prt(isp, ISP_LOGDEBUG0,
5647	 		    "skipping target %d bus %d update", tgt, bus);
5648			continue;
5649		}
5650		/*
5651		 * If the goal is to update the status of the device,
5652		 * take what's in goal_flags and try and set the device
5653		 * toward that. Otherwise, if we're just refreshing the
5654		 * current device state, get the current parameters.
5655		 */
5656
5657		/*
5658		 * Refresh overrides set
5659		 */
5660		if (sdp->isp_devparam[tgt].dev_refresh) {
5661			mbs.param[0] = MBOX_GET_TARGET_PARAMS;
5662			sdp->isp_devparam[tgt].dev_refresh = 0;
5663			get = 1;
5664		} else if (sdp->isp_devparam[tgt].dev_update) {
5665			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
5666			/*
5667			 * Make sure goal_flags has "Renegotiate on Error"
5668			 * on and "Freeze Queue on Error" off.
5669			 */
5670			sdp->isp_devparam[tgt].goal_flags |= DPARM_RENEG;
5671			sdp->isp_devparam[tgt].goal_flags &= ~DPARM_QFRZ;
5672
5673			mbs.param[2] = sdp->isp_devparam[tgt].goal_flags;
5674
5675			/*
5676			 * Insist that PARITY must be enabled
5677			 * if SYNC or WIDE is enabled.
5678			 */
5679			if ((mbs.param[2] & (DPARM_SYNC|DPARM_WIDE)) != 0) {
5680				mbs.param[2] |= DPARM_PARITY;
5681			}
5682
5683			if ((mbs.param[2] & DPARM_SYNC) == 0) {
5684				mbs.param[3] = 0;
5685			} else {
5686				mbs.param[3] =
5687				    (sdp->isp_devparam[tgt].goal_offset << 8) |
5688				    (sdp->isp_devparam[tgt].goal_period);
5689			}
5690			/*
5691			 * A command completion later that has
5692			 * RQSTF_NEGOTIATION set can cause
5693			 * the dev_refresh/announce cycle also.
5694			 *
5695			 * Note: It is really important to update our current
5696			 * flags with at least the state of TAG capabilities-
5697			 * otherwise we might try and send a tagged command
5698			 * when we have it all turned off. So change it here
5699			 * to say that current already matches goal.
5700			 */
5701			sdp->isp_devparam[tgt].actv_flags &= ~DPARM_TQING;
5702			sdp->isp_devparam[tgt].actv_flags |=
5703			    (sdp->isp_devparam[tgt].goal_flags & DPARM_TQING);
5704			isp_prt(isp, ISP_LOGDEBUG0,
5705			    "bus %d set tgt %d flags 0x%x off 0x%x period 0x%x",
5706			    bus, tgt, mbs.param[2], mbs.param[3] >> 8,
5707			    mbs.param[3] & 0xff);
5708			sdp->isp_devparam[tgt].dev_update = 0;
5709			sdp->isp_devparam[tgt].dev_refresh = 1;
5710			get = 0;
5711		} else {
5712			continue;
5713		}
5714		mbs.param[1] = (bus << 15) | (tgt << 8);
5715		isp_mboxcmd(isp, &mbs, MBLOGALL);
5716		if (get == 0) {
5717			isp->isp_sendmarker |= (1 << bus);
5718			continue;
5719		}
5720		flags = mbs.param[2];
5721		period = mbs.param[3] & 0xff;
5722		offset = mbs.param[3] >> 8;
5723		sdp->isp_devparam[tgt].actv_flags = flags;
5724		sdp->isp_devparam[tgt].actv_period = period;
5725		sdp->isp_devparam[tgt].actv_offset = offset;
5726		get = (bus << 16) | tgt;
5727		(void) isp_async(isp, ISPASYNC_NEW_TGT_PARAMS, &get);
5728	}
5729
5730	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
5731		if (sdp->isp_devparam[tgt].dev_update ||
5732		    sdp->isp_devparam[tgt].dev_refresh) {
5733			isp->isp_update |= (1 << bus);
5734			break;
5735		}
5736	}
5737}
5738
5739#ifndef	DEFAULT_FRAMESIZE
5740#define	DEFAULT_FRAMESIZE(isp)		ICB_DFLT_FRMLEN
5741#endif
5742#ifndef	DEFAULT_EXEC_THROTTLE
5743#define	DEFAULT_EXEC_THROTTLE(isp)	ISP_EXEC_THROTTLE
5744#endif
5745
5746static void
5747isp_setdfltparm(ispsoftc_t *isp, int channel)
5748{
5749	int tgt;
5750	mbreg_t mbs;
5751	sdparam *sdp;
5752
5753	MEMZERO(&mbs, sizeof (mbs));
5754	if (IS_FC(isp)) {
5755		fcparam *fcp = (fcparam *) isp->isp_param;
5756		int nvfail;
5757
5758		fcp += channel;
5759		if (fcp->isp_gotdparms) {
5760			return;
5761		}
5762		fcp->isp_gotdparms = 1;
5763		fcp->isp_maxfrmlen = DEFAULT_FRAMESIZE(isp);
5764		fcp->isp_maxalloc = ICB_DFLT_ALLOC;
5765		fcp->isp_execthrottle = DEFAULT_EXEC_THROTTLE(isp);
5766		fcp->isp_retry_delay = ICB_DFLT_RDELAY;
5767		fcp->isp_retry_count = ICB_DFLT_RCOUNT;
5768		/* Platform specific.... */
5769		fcp->isp_loopid = DEFAULT_LOOPID(isp);
5770		fcp->isp_nodewwn = DEFAULT_NODEWWN(isp);
5771		fcp->isp_portwwn = DEFAULT_PORTWWN(isp);
5772		fcp->isp_fwoptions = 0;
5773		fcp->isp_fwoptions |= ICBOPT_FAIRNESS;
5774		fcp->isp_fwoptions |= ICBOPT_PDBCHANGE_AE;
5775		fcp->isp_fwoptions |= ICBOPT_HARD_ADDRESS;
5776#ifndef	ISP_NO_FASTPOST_FC
5777		fcp->isp_fwoptions |= ICBOPT_FAST_POST;
5778#endif
5779		if (isp->isp_confopts & ISP_CFG_FULL_DUPLEX)
5780			fcp->isp_fwoptions |= ICBOPT_FULL_DUPLEX;
5781
5782		/*
5783		 * Make sure this is turned off now until we get
5784		 * extended options from NVRAM
5785		 */
5786		fcp->isp_fwoptions &= ~ICBOPT_EXTENDED;
5787
5788		/*
5789		 * Now try and read NVRAM unless told to not do so.
5790		 * This will set fcparam's isp_nodewwn && isp_portwwn.
5791		 */
5792		if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
5793		    	nvfail = isp_read_nvram(isp);
5794			if (nvfail)
5795				isp->isp_confopts |= ISP_CFG_NONVRAM;
5796		} else {
5797			nvfail = 1;
5798		}
5799		/*
5800		 * Set node && port to override platform set defaults
5801		 * unless the nvram read failed (or none was done),
5802		 * or the platform code wants to use what had been
5803		 * set in the defaults.
5804		 */
5805		if (nvfail) {
5806			isp->isp_confopts |= ISP_CFG_OWNWWPN|ISP_CFG_OWNWWNN;
5807		}
5808		if (isp->isp_confopts & ISP_CFG_OWNWWNN) {
5809			isp_prt(isp, ISP_LOGCONFIG, "Using Node WWN 0x%08x%08x",
5810			    (uint32_t) (DEFAULT_NODEWWN(isp) >> 32),
5811			    (uint32_t) (DEFAULT_NODEWWN(isp) & 0xffffffff));
5812			ISP_NODEWWN(isp) = DEFAULT_NODEWWN(isp);
5813		} else {
5814			/*
5815			 * We always start out with values derived
5816			 * from NVRAM or our platform default.
5817			 */
5818			ISP_NODEWWN(isp) = fcp->isp_nodewwn;
5819		}
5820		if (isp->isp_confopts & ISP_CFG_OWNWWPN) {
5821			isp_prt(isp, ISP_LOGCONFIG, "Using Port WWN 0x%08x%08x",
5822			    (uint32_t) (DEFAULT_PORTWWN(isp) >> 32),
5823			    (uint32_t) (DEFAULT_PORTWWN(isp) & 0xffffffff));
5824			ISP_PORTWWN(isp) = DEFAULT_PORTWWN(isp);
5825		} else {
5826			/*
5827			 * We always start out with values derived
5828			 * from NVRAM or our platform default.
5829			 */
5830			ISP_PORTWWN(isp) = fcp->isp_portwwn;
5831		}
5832		return;
5833	}
5834
5835	sdp = (sdparam *) isp->isp_param;
5836	sdp += channel;
5837
5838	/*
5839	 * Been there, done that, got the T-shirt...
5840	 */
5841	if (sdp->isp_gotdparms) {
5842		return;
5843	}
5844	sdp->isp_gotdparms = 1;
5845
5846	/*
5847	 * Establish some default parameters.
5848	 */
5849	sdp->isp_cmd_dma_burst_enable = 0;
5850	sdp->isp_data_dma_burst_enabl = 1;
5851	sdp->isp_fifo_threshold = 0;
5852	sdp->isp_initiator_id = DEFAULT_IID(isp);
5853	if (isp->isp_type >= ISP_HA_SCSI_1040) {
5854		sdp->isp_async_data_setup = 9;
5855	} else {
5856		sdp->isp_async_data_setup = 6;
5857	}
5858	sdp->isp_selection_timeout = 250;
5859	sdp->isp_max_queue_depth = MAXISPREQUEST(isp);
5860	sdp->isp_tag_aging = 8;
5861	sdp->isp_bus_reset_delay = 5;
5862	/*
5863	 * Don't retry selection, busy or queue full automatically- reflect
5864	 * these back to us.
5865	 */
5866	sdp->isp_retry_count = 0;
5867	sdp->isp_retry_delay = 0;
5868
5869	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
5870		sdp->isp_devparam[tgt].exc_throttle = ISP_EXEC_THROTTLE;
5871		sdp->isp_devparam[tgt].dev_enable = 1;
5872	}
5873
5874	/*
5875	 * If we've not been told to avoid reading NVRAM, try and read it.
5876	 * If we're successful reading it, we can then return because NVRAM
5877	 * will tell us what the desired settings are. Otherwise, we establish
5878	 * some reasonable 'fake' nvram and goal defaults.
5879	 */
5880
5881	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
5882		if (isp_read_nvram(isp) == 0) {
5883			return;
5884		}
5885	}
5886
5887	/*
5888	 * Now try and see whether we have specific values for them.
5889	 */
5890	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
5891		mbs.param[0] = MBOX_GET_ACT_NEG_STATE;
5892		isp_mboxcmd(isp, &mbs, MBLOGNONE);
5893		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
5894			sdp->isp_req_ack_active_neg = 1;
5895			sdp->isp_data_line_active_neg = 1;
5896		} else {
5897			sdp->isp_req_ack_active_neg =
5898			    (mbs.param[1+channel] >> 4) & 0x1;
5899			sdp->isp_data_line_active_neg =
5900			    (mbs.param[1+channel] >> 5) & 0x1;
5901		}
5902	}
5903
5904	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc3,
5905	    0, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
5906	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
5907	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
5908	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc3,
5909	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
5910	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
5911	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
5912
5913	/*
5914	 * The trick here is to establish a default for the default (honk!)
5915	 * state (goal_flags). Then try and get the current status from
5916	 * the card to fill in the current state. We don't, in fact, set
5917	 * the default to the SAFE default state- that's not the goal state.
5918	 */
5919	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
5920		uint8_t off, per;
5921		sdp->isp_devparam[tgt].actv_offset = 0;
5922		sdp->isp_devparam[tgt].actv_period = 0;
5923		sdp->isp_devparam[tgt].actv_flags = 0;
5924
5925		sdp->isp_devparam[tgt].goal_flags =
5926		    sdp->isp_devparam[tgt].nvrm_flags = DPARM_DEFAULT;
5927
5928		/*
5929		 * We default to Wide/Fast for versions less than a 1040
5930		 * (unless it's SBus).
5931		 */
5932		if (IS_ULTRA3(isp)) {
5933			off = ISP_80M_SYNCPARMS >> 8;
5934			per = ISP_80M_SYNCPARMS & 0xff;
5935		} else if (IS_ULTRA2(isp)) {
5936			off = ISP_40M_SYNCPARMS >> 8;
5937			per = ISP_40M_SYNCPARMS & 0xff;
5938		} else if (IS_1240(isp)) {
5939			off = ISP_20M_SYNCPARMS >> 8;
5940			per = ISP_20M_SYNCPARMS & 0xff;
5941		} else if ((isp->isp_bustype == ISP_BT_SBUS &&
5942		    isp->isp_type < ISP_HA_SCSI_1020A) ||
5943		    (isp->isp_bustype == ISP_BT_PCI &&
5944		    isp->isp_type < ISP_HA_SCSI_1040) ||
5945		    (isp->isp_clock && isp->isp_clock < 60) ||
5946		    (sdp->isp_ultramode == 0)) {
5947			off = ISP_10M_SYNCPARMS >> 8;
5948			per = ISP_10M_SYNCPARMS & 0xff;
5949		} else {
5950			off = ISP_20M_SYNCPARMS_1040 >> 8;
5951			per = ISP_20M_SYNCPARMS_1040 & 0xff;
5952		}
5953		sdp->isp_devparam[tgt].goal_offset =
5954		    sdp->isp_devparam[tgt].nvrm_offset = off;
5955		sdp->isp_devparam[tgt].goal_period =
5956		    sdp->isp_devparam[tgt].nvrm_period = per;
5957
5958		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc3,
5959		    channel, tgt, sdp->isp_devparam[tgt].nvrm_flags,
5960		    sdp->isp_devparam[tgt].nvrm_offset,
5961		    sdp->isp_devparam[tgt].nvrm_period);
5962	}
5963}
5964
5965/*
5966 * Re-initialize the ISP and complete all orphaned commands
5967 * with a 'botched' notice. The reset/init routines should
5968 * not disturb an already active list of commands.
5969 *
5970 * Locks held prior to coming here.
5971 */
5972
5973void
5974isp_reinit(ispsoftc_t *isp)
5975{
5976	XS_T *xs;
5977	int i;
5978
5979	if (IS_FC(isp)) {
5980		isp_mark_getpdb_all(isp);
5981	}
5982	isp_reset(isp);
5983	if (isp->isp_state != ISP_RESETSTATE) {
5984		isp_prt(isp, ISP_LOGERR, "isp_reinit cannot reset card");
5985	} else if (isp->isp_role != ISP_ROLE_NONE) {
5986		isp_init(isp);
5987		if (isp->isp_state == ISP_INITSTATE) {
5988			isp->isp_state = ISP_RUNSTATE;
5989		}
5990		if (isp->isp_state != ISP_RUNSTATE) {
5991			isp_prt(isp, ISP_LOGERR,
5992			    "isp_reinit cannot restart card");
5993		}
5994	}
5995	isp->isp_nactive = 0;
5996
5997	for (i = 0; i < isp->isp_maxcmds; i++) {
5998		uint16_t handle;
5999		xs = isp->isp_xflist[i];
6000		if (xs == NULL) {
6001			continue;
6002		}
6003		handle = isp_index_handle(i);
6004		isp_destroy_handle(isp, handle);
6005		if (XS_XFRLEN(xs)) {
6006			ISP_DMAFREE(isp, xs, handle);
6007			XS_RESID(xs) = XS_XFRLEN(xs);
6008		} else {
6009			XS_RESID(xs) = 0;
6010		}
6011		XS_SETERR(xs, HBA_BUSRESET);
6012		isp_done(xs);
6013	}
6014}
6015
6016/*
6017 * NVRAM Routines
6018 */
6019static int
6020isp_read_nvram(ispsoftc_t *isp)
6021{
6022	int i, amt;
6023	uint8_t csum, minversion;
6024	union {
6025		uint8_t _x[ISP2100_NVRAM_SIZE];
6026		uint16_t _s[ISP2100_NVRAM_SIZE>>1];
6027	} _n;
6028#define	nvram_data	_n._x
6029#define	nvram_words	_n._s
6030
6031	if (IS_FC(isp)) {
6032		amt = ISP2100_NVRAM_SIZE;
6033		minversion = 1;
6034	} else if (IS_ULTRA2(isp)) {
6035		amt = ISP1080_NVRAM_SIZE;
6036		minversion = 0;
6037	} else {
6038		amt = ISP_NVRAM_SIZE;
6039		minversion = 2;
6040	}
6041
6042	/*
6043	 * Just read the first two words first to see if we have a valid
6044	 * NVRAM to continue reading the rest with.
6045	 */
6046	for (i = 0; i < 2; i++) {
6047		isp_rdnvram_word(isp, i, &nvram_words[i]);
6048	}
6049	if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
6050	    nvram_data[2] != 'P') {
6051		if (isp->isp_bustype != ISP_BT_SBUS) {
6052			isp_prt(isp, ISP_LOGWARN, "invalid NVRAM header");
6053			isp_prt(isp, ISP_LOGDEBUG0, "%x %x %x",
6054			    nvram_data[0], nvram_data[1], nvram_data[2]);
6055		}
6056		return (-1);
6057	}
6058	for (i = 2; i < amt>>1; i++) {
6059		isp_rdnvram_word(isp, i, &nvram_words[i]);
6060	}
6061	for (csum = 0, i = 0; i < amt; i++) {
6062		csum += nvram_data[i];
6063	}
6064	if (csum != 0) {
6065		isp_prt(isp, ISP_LOGWARN, "invalid NVRAM checksum");
6066		return (-1);
6067	}
6068	if (ISP_NVRAM_VERSION(nvram_data) < minversion) {
6069		isp_prt(isp, ISP_LOGWARN, "version %d NVRAM not understood",
6070		    ISP_NVRAM_VERSION(nvram_data));
6071		return (-1);
6072	}
6073
6074	if (IS_ULTRA3(isp)) {
6075		isp_parse_nvram_12160(isp, 0, nvram_data);
6076		if (IS_12160(isp))
6077			isp_parse_nvram_12160(isp, 1, nvram_data);
6078	} else if (IS_1080(isp)) {
6079		isp_parse_nvram_1080(isp, 0, nvram_data);
6080	} else if (IS_1280(isp) || IS_1240(isp)) {
6081		isp_parse_nvram_1080(isp, 0, nvram_data);
6082		isp_parse_nvram_1080(isp, 1, nvram_data);
6083	} else if (IS_SCSI(isp)) {
6084		isp_parse_nvram_1020(isp, nvram_data);
6085	} else {
6086		isp_parse_nvram_2100(isp, nvram_data);
6087	}
6088	return (0);
6089#undef	nvram_data
6090#undef	nvram_words
6091}
6092
6093static void
6094isp_rdnvram_word(ispsoftc_t *isp, int wo, uint16_t *rp)
6095{
6096	int i, cbits;
6097	uint16_t bit, rqst;
6098
6099	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
6100	USEC_DELAY(2);
6101	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
6102	USEC_DELAY(2);
6103
6104	if (IS_FC(isp)) {
6105		wo &= ((ISP2100_NVRAM_SIZE >> 1) - 1);
6106		if (IS_2312(isp) && isp->isp_port) {
6107			wo += 128;
6108		}
6109		rqst = (ISP_NVRAM_READ << 8) | wo;
6110		cbits = 10;
6111	} else if (IS_ULTRA2(isp)) {
6112		wo &= ((ISP1080_NVRAM_SIZE >> 1) - 1);
6113		rqst = (ISP_NVRAM_READ << 8) | wo;
6114		cbits = 10;
6115	} else {
6116		wo &= ((ISP_NVRAM_SIZE >> 1) - 1);
6117		rqst = (ISP_NVRAM_READ << 6) | wo;
6118		cbits = 8;
6119	}
6120
6121	/*
6122	 * Clock the word select request out...
6123	 */
6124	for (i = cbits; i >= 0; i--) {
6125		if ((rqst >> i) & 1) {
6126			bit = BIU_NVRAM_SELECT | BIU_NVRAM_DATAOUT;
6127		} else {
6128			bit = BIU_NVRAM_SELECT;
6129		}
6130		ISP_WRITE(isp, BIU_NVRAM, bit);
6131		USEC_DELAY(2);
6132		ISP_WRITE(isp, BIU_NVRAM, bit | BIU_NVRAM_CLOCK);
6133		USEC_DELAY(2);
6134		ISP_WRITE(isp, BIU_NVRAM, bit);
6135		USEC_DELAY(2);
6136	}
6137	/*
6138	 * Now read the result back in (bits come back in MSB format).
6139	 */
6140	*rp = 0;
6141	for (i = 0; i < 16; i++) {
6142		uint16_t rv;
6143		*rp <<= 1;
6144		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
6145		USEC_DELAY(2);
6146		rv = ISP_READ(isp, BIU_NVRAM);
6147		if (rv & BIU_NVRAM_DATAIN) {
6148			*rp |= 1;
6149		}
6150		USEC_DELAY(2);
6151		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
6152		USEC_DELAY(2);
6153	}
6154	ISP_WRITE(isp, BIU_NVRAM, 0);
6155	USEC_DELAY(2);
6156	ISP_SWIZZLE_NVRAM_WORD(isp, rp);
6157}
6158
6159static void
6160isp_parse_nvram_1020(ispsoftc_t *isp, uint8_t *nvram_data)
6161{
6162	sdparam *sdp = (sdparam *) isp->isp_param;
6163	int tgt;
6164
6165	sdp->isp_fifo_threshold =
6166		ISP_NVRAM_FIFO_THRESHOLD(nvram_data) |
6167		(ISP_NVRAM_FIFO_THRESHOLD_128(nvram_data) << 2);
6168
6169	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
6170		sdp->isp_initiator_id =
6171			ISP_NVRAM_INITIATOR_ID(nvram_data);
6172
6173	sdp->isp_bus_reset_delay =
6174		ISP_NVRAM_BUS_RESET_DELAY(nvram_data);
6175
6176	sdp->isp_retry_count =
6177		ISP_NVRAM_BUS_RETRY_COUNT(nvram_data);
6178
6179	sdp->isp_retry_delay =
6180		ISP_NVRAM_BUS_RETRY_DELAY(nvram_data);
6181
6182	sdp->isp_async_data_setup =
6183		ISP_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data);
6184
6185	if (isp->isp_type >= ISP_HA_SCSI_1040) {
6186		if (sdp->isp_async_data_setup < 9) {
6187			sdp->isp_async_data_setup = 9;
6188		}
6189	} else {
6190		if (sdp->isp_async_data_setup != 6) {
6191			sdp->isp_async_data_setup = 6;
6192		}
6193	}
6194
6195	sdp->isp_req_ack_active_neg =
6196		ISP_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data);
6197
6198	sdp->isp_data_line_active_neg =
6199		ISP_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data);
6200
6201	sdp->isp_data_dma_burst_enabl =
6202		ISP_NVRAM_DATA_DMA_BURST_ENABLE(nvram_data);
6203
6204	sdp->isp_cmd_dma_burst_enable =
6205		ISP_NVRAM_CMD_DMA_BURST_ENABLE(nvram_data);
6206
6207	sdp->isp_tag_aging =
6208		ISP_NVRAM_TAG_AGE_LIMIT(nvram_data);
6209
6210	sdp->isp_selection_timeout =
6211		ISP_NVRAM_SELECTION_TIMEOUT(nvram_data);
6212
6213	sdp->isp_max_queue_depth =
6214		ISP_NVRAM_MAX_QUEUE_DEPTH(nvram_data);
6215
6216	sdp->isp_fast_mttr = ISP_NVRAM_FAST_MTTR_ENABLE(nvram_data);
6217
6218	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
6219	    0, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
6220	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
6221	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
6222	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
6223	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
6224	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
6225	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
6226
6227	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
6228		sdp->isp_devparam[tgt].dev_enable =
6229			ISP_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt);
6230		sdp->isp_devparam[tgt].exc_throttle =
6231			ISP_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt);
6232		sdp->isp_devparam[tgt].nvrm_offset =
6233			ISP_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt);
6234		sdp->isp_devparam[tgt].nvrm_period =
6235			ISP_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt);
6236		/*
6237		 * We probably shouldn't lie about this, but it
6238		 * it makes it much safer if we limit NVRAM values
6239		 * to sanity.
6240		 */
6241		if (isp->isp_type < ISP_HA_SCSI_1040) {
6242			/*
6243			 * If we're not ultra, we can't possibly
6244			 * be a shorter period than this.
6245			 */
6246			if (sdp->isp_devparam[tgt].nvrm_period < 0x19) {
6247				sdp->isp_devparam[tgt].nvrm_period = 0x19;
6248			}
6249			if (sdp->isp_devparam[tgt].nvrm_offset > 0xc) {
6250				sdp->isp_devparam[tgt].nvrm_offset = 0x0c;
6251			}
6252		} else {
6253			if (sdp->isp_devparam[tgt].nvrm_offset > 0x8) {
6254				sdp->isp_devparam[tgt].nvrm_offset = 0x8;
6255			}
6256		}
6257		sdp->isp_devparam[tgt].nvrm_flags = 0;
6258		if (ISP_NVRAM_TGT_RENEG(nvram_data, tgt))
6259			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
6260		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
6261		if (ISP_NVRAM_TGT_TQING(nvram_data, tgt))
6262			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
6263		if (ISP_NVRAM_TGT_SYNC(nvram_data, tgt))
6264			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
6265		if (ISP_NVRAM_TGT_WIDE(nvram_data, tgt))
6266			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
6267		if (ISP_NVRAM_TGT_PARITY(nvram_data, tgt))
6268			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
6269		if (ISP_NVRAM_TGT_DISC(nvram_data, tgt))
6270			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
6271		sdp->isp_devparam[tgt].actv_flags = 0; /* we don't know */
6272		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
6273		    0, tgt, sdp->isp_devparam[tgt].nvrm_flags,
6274		    sdp->isp_devparam[tgt].nvrm_offset,
6275		    sdp->isp_devparam[tgt].nvrm_period);
6276		sdp->isp_devparam[tgt].goal_offset =
6277		    sdp->isp_devparam[tgt].nvrm_offset;
6278		sdp->isp_devparam[tgt].goal_period =
6279		    sdp->isp_devparam[tgt].nvrm_period;
6280		sdp->isp_devparam[tgt].goal_flags =
6281		    sdp->isp_devparam[tgt].nvrm_flags;
6282	}
6283}
6284
6285static void
6286isp_parse_nvram_1080(ispsoftc_t *isp, int bus, uint8_t *nvram_data)
6287{
6288	sdparam *sdp = (sdparam *) isp->isp_param;
6289	int tgt;
6290
6291	sdp += bus;
6292
6293	sdp->isp_fifo_threshold =
6294	    ISP1080_NVRAM_FIFO_THRESHOLD(nvram_data);
6295
6296	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
6297		sdp->isp_initiator_id =
6298		    ISP1080_NVRAM_INITIATOR_ID(nvram_data, bus);
6299
6300	sdp->isp_bus_reset_delay =
6301	    ISP1080_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
6302
6303	sdp->isp_retry_count =
6304	    ISP1080_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
6305
6306	sdp->isp_retry_delay =
6307	    ISP1080_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
6308
6309	sdp->isp_async_data_setup =
6310	    ISP1080_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
6311
6312	sdp->isp_req_ack_active_neg =
6313	    ISP1080_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
6314
6315	sdp->isp_data_line_active_neg =
6316	    ISP1080_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
6317
6318	sdp->isp_data_dma_burst_enabl =
6319	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
6320
6321	sdp->isp_cmd_dma_burst_enable =
6322	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
6323
6324	sdp->isp_selection_timeout =
6325	    ISP1080_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
6326
6327	sdp->isp_max_queue_depth =
6328	     ISP1080_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
6329
6330	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
6331	    bus, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
6332	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
6333	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
6334	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
6335	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
6336	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
6337	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
6338
6339
6340	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
6341		sdp->isp_devparam[tgt].dev_enable =
6342		    ISP1080_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
6343		sdp->isp_devparam[tgt].exc_throttle =
6344			ISP1080_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
6345		sdp->isp_devparam[tgt].nvrm_offset =
6346			ISP1080_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
6347		sdp->isp_devparam[tgt].nvrm_period =
6348			ISP1080_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
6349		sdp->isp_devparam[tgt].nvrm_flags = 0;
6350		if (ISP1080_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
6351			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
6352		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
6353		if (ISP1080_NVRAM_TGT_TQING(nvram_data, tgt, bus))
6354			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
6355		if (ISP1080_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
6356			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
6357		if (ISP1080_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
6358			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
6359		if (ISP1080_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
6360			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
6361		if (ISP1080_NVRAM_TGT_DISC(nvram_data, tgt, bus))
6362			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
6363		sdp->isp_devparam[tgt].actv_flags = 0;
6364		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
6365		    bus, tgt, sdp->isp_devparam[tgt].nvrm_flags,
6366		    sdp->isp_devparam[tgt].nvrm_offset,
6367		    sdp->isp_devparam[tgt].nvrm_period);
6368		sdp->isp_devparam[tgt].goal_offset =
6369		    sdp->isp_devparam[tgt].nvrm_offset;
6370		sdp->isp_devparam[tgt].goal_period =
6371		    sdp->isp_devparam[tgt].nvrm_period;
6372		sdp->isp_devparam[tgt].goal_flags =
6373		    sdp->isp_devparam[tgt].nvrm_flags;
6374	}
6375}
6376
6377static void
6378isp_parse_nvram_12160(ispsoftc_t *isp, int bus, uint8_t *nvram_data)
6379{
6380	sdparam *sdp = (sdparam *) isp->isp_param;
6381	int tgt;
6382
6383	sdp += bus;
6384
6385	sdp->isp_fifo_threshold =
6386	    ISP12160_NVRAM_FIFO_THRESHOLD(nvram_data);
6387
6388	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
6389		sdp->isp_initiator_id =
6390		    ISP12160_NVRAM_INITIATOR_ID(nvram_data, bus);
6391
6392	sdp->isp_bus_reset_delay =
6393	    ISP12160_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
6394
6395	sdp->isp_retry_count =
6396	    ISP12160_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
6397
6398	sdp->isp_retry_delay =
6399	    ISP12160_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
6400
6401	sdp->isp_async_data_setup =
6402	    ISP12160_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
6403
6404	sdp->isp_req_ack_active_neg =
6405	    ISP12160_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
6406
6407	sdp->isp_data_line_active_neg =
6408	    ISP12160_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
6409
6410	sdp->isp_data_dma_burst_enabl =
6411	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
6412
6413	sdp->isp_cmd_dma_burst_enable =
6414	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
6415
6416	sdp->isp_selection_timeout =
6417	    ISP12160_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
6418
6419	sdp->isp_max_queue_depth =
6420	     ISP12160_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
6421
6422	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
6423	    bus, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
6424	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
6425	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
6426	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
6427	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
6428	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
6429	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
6430
6431	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
6432		sdp->isp_devparam[tgt].dev_enable =
6433		    ISP12160_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
6434		sdp->isp_devparam[tgt].exc_throttle =
6435			ISP12160_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
6436		sdp->isp_devparam[tgt].nvrm_offset =
6437			ISP12160_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
6438		sdp->isp_devparam[tgt].nvrm_period =
6439			ISP12160_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
6440		sdp->isp_devparam[tgt].nvrm_flags = 0;
6441		if (ISP12160_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
6442			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
6443		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
6444		if (ISP12160_NVRAM_TGT_TQING(nvram_data, tgt, bus))
6445			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
6446		if (ISP12160_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
6447			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
6448		if (ISP12160_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
6449			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
6450		if (ISP12160_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
6451			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
6452		if (ISP12160_NVRAM_TGT_DISC(nvram_data, tgt, bus))
6453			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
6454		sdp->isp_devparam[tgt].actv_flags = 0;
6455		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
6456		    bus, tgt, sdp->isp_devparam[tgt].nvrm_flags,
6457		    sdp->isp_devparam[tgt].nvrm_offset,
6458		    sdp->isp_devparam[tgt].nvrm_period);
6459		sdp->isp_devparam[tgt].goal_offset =
6460		    sdp->isp_devparam[tgt].nvrm_offset;
6461		sdp->isp_devparam[tgt].goal_period =
6462		    sdp->isp_devparam[tgt].nvrm_period;
6463		sdp->isp_devparam[tgt].goal_flags =
6464		    sdp->isp_devparam[tgt].nvrm_flags;
6465	}
6466}
6467
6468static void
6469isp_parse_nvram_2100(ispsoftc_t *isp, uint8_t *nvram_data)
6470{
6471	fcparam *fcp = (fcparam *) isp->isp_param;
6472	uint64_t wwn;
6473
6474	/*
6475	 * There is NVRAM storage for both Port and Node entities-
6476	 * but the Node entity appears to be unused on all the cards
6477	 * I can find. However, we should account for this being set
6478	 * at some point in the future.
6479	 *
6480	 * Qlogic WWNs have an NAA of 2, but usually nothing shows up in
6481	 * bits 48..60. In the case of the 2202, it appears that they do
6482	 * use bit 48 to distinguish between the two instances on the card.
6483	 * The 2204, which I've never seen, *probably* extends this method.
6484	 */
6485	wwn = ISP2100_NVRAM_PORT_NAME(nvram_data);
6486	if (wwn) {
6487		isp_prt(isp, ISP_LOGCONFIG, "NVRAM Port WWN 0x%08x%08x",
6488		    (uint32_t) (wwn >> 32), (uint32_t) (wwn & 0xffffffff));
6489		if ((wwn >> 60) == 0) {
6490			wwn |= (((uint64_t) 2)<< 60);
6491		}
6492	}
6493	fcp->isp_portwwn = wwn;
6494	if (IS_2200(isp) || IS_23XX(isp)) {
6495		wwn = ISP2200_NVRAM_NODE_NAME(nvram_data);
6496		if (wwn) {
6497			isp_prt(isp, ISP_LOGCONFIG, "NVRAM Node WWN 0x%08x%08x",
6498			    (uint32_t) (wwn >> 32),
6499			    (uint32_t) (wwn & 0xffffffff));
6500			if ((wwn >> 60) == 0) {
6501				wwn |= (((uint64_t) 2)<< 60);
6502			}
6503		}
6504	} else {
6505		wwn &= ~((uint64_t) 0xfff << 48);
6506	}
6507	fcp->isp_nodewwn = wwn;
6508
6509	/*
6510	 * Make sure we have both Node and Port as non-zero values.
6511	 */
6512	if (fcp->isp_nodewwn != 0 && fcp->isp_portwwn == 0) {
6513		fcp->isp_portwwn = fcp->isp_nodewwn;
6514	} else if (fcp->isp_nodewwn == 0 && fcp->isp_portwwn != 0) {
6515		fcp->isp_nodewwn = fcp->isp_portwwn;
6516	}
6517
6518	/*
6519	 * Make the Node and Port values sane if they're NAA == 2.
6520	 * This means to clear bits 48..56 for the Node WWN and
6521	 * make sure that there's some non-zero value in 48..56
6522	 * for the Port WWN.
6523	 */
6524	if (fcp->isp_nodewwn && fcp->isp_portwwn) {
6525		if ((fcp->isp_nodewwn & (((uint64_t) 0xfff) << 48)) != 0 &&
6526		    (fcp->isp_nodewwn >> 60) == 2) {
6527			fcp->isp_nodewwn &= ~((uint64_t) 0xfff << 48);
6528		}
6529		if ((fcp->isp_portwwn & (((uint64_t) 0xfff) << 48)) == 0 &&
6530		    (fcp->isp_portwwn >> 60) == 2) {
6531			fcp->isp_portwwn |= ((uint64_t) 1 << 56);
6532		}
6533	}
6534
6535	isp_prt(isp, ISP_LOGDEBUG0,
6536	    "NVRAM: maxfrmlen %d execthrottle %d fwoptions 0x%x loopid %x",
6537	    ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data),
6538	    ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data),
6539	    ISP2100_NVRAM_OPTIONS(nvram_data),
6540	    ISP2100_NVRAM_HARDLOOPID(nvram_data));
6541
6542	fcp->isp_maxalloc =
6543		ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data);
6544	if ((isp->isp_confopts & ISP_CFG_OWNFSZ) == 0)
6545		fcp->isp_maxfrmlen =
6546			ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data);
6547	fcp->isp_retry_delay =
6548		ISP2100_NVRAM_RETRY_DELAY(nvram_data);
6549	fcp->isp_retry_count =
6550		ISP2100_NVRAM_RETRY_COUNT(nvram_data);
6551	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
6552		fcp->isp_loopid =
6553			ISP2100_NVRAM_HARDLOOPID(nvram_data);
6554	if ((isp->isp_confopts & ISP_CFG_OWNEXCTHROTTLE) == 0)
6555		fcp->isp_execthrottle =
6556			ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data);
6557	fcp->isp_fwoptions = ISP2100_NVRAM_OPTIONS(nvram_data);
6558}
6559
6560#ifdef	ISP_FW_CRASH_DUMP
6561static void isp2200_fw_dump(ispsoftc_t *);
6562static void isp2300_fw_dump(ispsoftc_t *);
6563
6564static void
6565isp2200_fw_dump(ispsoftc_t *isp)
6566{
6567	int i, j;
6568	mbreg_t mbs;
6569	uint16_t *ptr;
6570
6571	MEMZERO(&mbs, sizeof (mbs));
6572	ptr = FCPARAM(isp)->isp_dump_data;
6573	if (ptr == NULL) {
6574		isp_prt(isp, ISP_LOGERR,
6575		   "No place to dump RISC registers and SRAM");
6576		return;
6577	}
6578	if (*ptr++) {
6579		isp_prt(isp, ISP_LOGERR,
6580		   "dump area for RISC registers and SRAM already used");
6581		return;
6582	}
6583	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
6584	for (i = 0; i < 100; i++) {
6585		USEC_DELAY(100);
6586		if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6587			break;
6588		}
6589	}
6590	if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6591		/*
6592		 * PBIU Registers
6593		 */
6594		for (i = 0; i < 8; i++) {
6595			*ptr++ = ISP_READ(isp, BIU_BLOCK + (i << 1));
6596		}
6597
6598		/*
6599		 * Mailbox Registers
6600		 */
6601		for (i = 0; i < 8; i++) {
6602			*ptr++ = ISP_READ(isp, MBOX_BLOCK + (i << 1));
6603		}
6604
6605		/*
6606		 * DMA Registers
6607		 */
6608		for (i = 0; i < 48; i++) {
6609			*ptr++ = ISP_READ(isp, DMA_BLOCK + 0x20 + (i << 1));
6610		}
6611
6612		/*
6613		 * RISC H/W Registers
6614		 */
6615		ISP_WRITE(isp, BIU2100_CSR, 0);
6616		for (i = 0; i < 16; i++) {
6617			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0xA0 + (i << 1));
6618		}
6619
6620		/*
6621		 * RISC GP Registers
6622		 */
6623		for (j = 0; j < 8; j++) {
6624			ISP_WRITE(isp, BIU_BLOCK + 0xA4, 0x2000 + (j << 8));
6625			for (i = 0; i < 16; i++) {
6626				*ptr++ =
6627				    ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6628			}
6629		}
6630
6631		/*
6632		 * Frame Buffer Hardware Registers
6633		 */
6634		ISP_WRITE(isp, BIU2100_CSR, 0x10);
6635		for (i = 0; i < 16; i++) {
6636			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6637		}
6638
6639		/*
6640		 * Fibre Protocol Module 0 Hardware Registers
6641		 */
6642		ISP_WRITE(isp, BIU2100_CSR, 0x20);
6643		for (i = 0; i < 64; i++) {
6644			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6645		}
6646
6647		/*
6648		 * Fibre Protocol Module 1 Hardware Registers
6649		 */
6650		ISP_WRITE(isp, BIU2100_CSR, 0x30);
6651		for (i = 0; i < 64; i++) {
6652			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6653		}
6654	} else {
6655		isp_prt(isp, ISP_LOGERR, "RISC Would Not Pause");
6656		return;
6657	}
6658	isp_prt(isp, ISP_LOGALL,
6659	   "isp_fw_dump: RISC registers dumped successfully");
6660	ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
6661	for (i = 0; i < 100; i++) {
6662		USEC_DELAY(100);
6663		if (ISP_READ(isp, OUTMAILBOX0) == 0) {
6664			break;
6665		}
6666	}
6667	if (ISP_READ(isp, OUTMAILBOX0) != 0) {
6668		isp_prt(isp, ISP_LOGERR, "Board Would Not Reset");
6669		return;
6670	}
6671	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
6672	for (i = 0; i < 100; i++) {
6673		USEC_DELAY(100);
6674		if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6675			break;
6676		}
6677	}
6678	if ((ISP_READ(isp, HCCR) & HCCR_PAUSE) == 0) {
6679		isp_prt(isp, ISP_LOGERR, "RISC Would Not Pause After Reset");
6680		return;
6681	}
6682	ISP_WRITE(isp, RISC_EMB, 0xf2);
6683	ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
6684	for (i = 0; i < 100; i++) {
6685		USEC_DELAY(100);
6686		if ((ISP_READ(isp, HCCR) & HCCR_PAUSE) == 0) {
6687			break;
6688		}
6689	}
6690	ENABLE_INTS(isp);
6691	mbs.param[0] = MBOX_READ_RAM_WORD;
6692	mbs.param[1] = 0x1000;
6693	isp->isp_mbxworkp = (void *) ptr;
6694	isp->isp_mbxwrk0 = 0xefff;	/* continuation count */
6695	isp->isp_mbxwrk1 = 0x1001;	/* next SRAM address */
6696	isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
6697	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
6698		isp_prt(isp, ISP_LOGWARN,
6699		    "RAM DUMP FAILED @ WORD %x", isp->isp_mbxwrk1);
6700		return;
6701	}
6702	ptr = isp->isp_mbxworkp;	/* finish fetch of final word */
6703	*ptr++ = isp->isp_mboxtmp[2];
6704	isp_prt(isp, ISP_LOGALL, "isp_fw_dump: SRAM dumped successfully");
6705	FCPARAM(isp)->isp_dump_data[0] = isp->isp_type; /* now used */
6706	(void) isp_async(isp, ISPASYNC_FW_DUMPED, 0);
6707}
6708
6709static void
6710isp2300_fw_dump(ispsoftc_t *isp)
6711{
6712	int i, j;
6713	mbreg_t mbs;
6714	uint16_t *ptr;
6715
6716	MEMZERO(&mbs, sizeof (mbs));
6717	ptr = FCPARAM(isp)->isp_dump_data;
6718	if (ptr == NULL) {
6719		isp_prt(isp, ISP_LOGERR,
6720		   "No place to dump RISC registers and SRAM");
6721		return;
6722	}
6723	if (*ptr++) {
6724		isp_prt(isp, ISP_LOGERR,
6725		   "dump area for RISC registers and SRAM already used");
6726		return;
6727	}
6728	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
6729	for (i = 0; i < 100; i++) {
6730		USEC_DELAY(100);
6731		if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6732			break;
6733		}
6734	}
6735	if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6736		/*
6737		 * PBIU registers
6738		 */
6739		for (i = 0; i < 8; i++) {
6740			*ptr++ = ISP_READ(isp, BIU_BLOCK + (i << 1));
6741		}
6742
6743		/*
6744		 * ReqQ-RspQ-Risc2Host Status registers
6745		 */
6746		for (i = 0; i < 8; i++) {
6747			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x10 + (i << 1));
6748		}
6749
6750		/*
6751		 * Mailbox Registers
6752		 */
6753		for (i = 0; i < 32; i++) {
6754			*ptr++ =
6755			    ISP_READ(isp, PCI_MBOX_REGS2300_OFF + (i << 1));
6756		}
6757
6758		/*
6759		 * Auto Request Response DMA registers
6760		 */
6761		ISP_WRITE(isp, BIU2100_CSR, 0x40);
6762		for (i = 0; i < 32; i++) {
6763			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6764		}
6765
6766		/*
6767		 * DMA registers
6768		 */
6769		ISP_WRITE(isp, BIU2100_CSR, 0x50);
6770		for (i = 0; i < 48; i++) {
6771			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6772		}
6773
6774		/*
6775		 * RISC hardware registers
6776		 */
6777		ISP_WRITE(isp, BIU2100_CSR, 0);
6778		for (i = 0; i < 16; i++) {
6779			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0xA0 + (i << 1));
6780		}
6781
6782		/*
6783		 * RISC GP? registers
6784		 */
6785		for (j = 0; j < 8; j++) {
6786			ISP_WRITE(isp, BIU_BLOCK + 0xA4, 0x2000 + (j << 9));
6787			for (i = 0; i < 16; i++) {
6788				*ptr++ =
6789				    ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6790			}
6791		}
6792
6793		/*
6794		 * frame buffer hardware registers
6795		 */
6796		ISP_WRITE(isp, BIU2100_CSR, 0x10);
6797		for (i = 0; i < 64; i++) {
6798			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6799		}
6800
6801		/*
6802		 * FPM B0 hardware registers
6803		 */
6804		ISP_WRITE(isp, BIU2100_CSR, 0x20);
6805		for (i = 0; i < 64; i++) {
6806			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6807		}
6808
6809		/*
6810		 * FPM B1 hardware registers
6811		 */
6812		ISP_WRITE(isp, BIU2100_CSR, 0x30);
6813		for (i = 0; i < 64; i++) {
6814			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6815		}
6816	} else {
6817		isp_prt(isp, ISP_LOGERR, "RISC Would Not Pause");
6818		return;
6819	}
6820	isp_prt(isp, ISP_LOGALL,
6821	   "isp_fw_dump: RISC registers dumped successfully");
6822	ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
6823	for (i = 0; i < 100; i++) {
6824		USEC_DELAY(100);
6825		if (ISP_READ(isp, OUTMAILBOX0) == 0) {
6826			break;
6827		}
6828	}
6829	if (ISP_READ(isp, OUTMAILBOX0) != 0) {
6830		isp_prt(isp, ISP_LOGERR, "Board Would Not Reset");
6831		return;
6832	}
6833	ENABLE_INTS(isp);
6834	mbs.param[0] = MBOX_READ_RAM_WORD;
6835	mbs.param[1] = 0x800;
6836	isp->isp_mbxworkp = (void *) ptr;
6837	isp->isp_mbxwrk0 = 0xf7ff;	/* continuation count */
6838	isp->isp_mbxwrk1 = 0x801;	/* next SRAM address */
6839	isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
6840	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
6841		isp_prt(isp, ISP_LOGWARN,
6842		    "RAM DUMP FAILED @ WORD %x", isp->isp_mbxwrk1);
6843		return;
6844	}
6845	ptr = isp->isp_mbxworkp;	/* finish fetch of final word */
6846	*ptr++ = isp->isp_mboxtmp[2];
6847
6848	/*
6849	 * We don't have access to mailbox registers 8.. onward
6850	 * in our 'common' device model- so we have to set it
6851	 * here and hope it stays the same!
6852	 */
6853	ISP_WRITE(isp, PCI_MBOX_REGS2300_OFF + (8 << 1), 0x1);
6854
6855	mbs.param[0] = MBOX_READ_RAM_WORD_EXTENDED;
6856	mbs.param[1] = 0;
6857	isp->isp_mbxworkp = (void *) ptr;
6858	isp->isp_mbxwrk0 = 0xffff;	/* continuation count */
6859	isp->isp_mbxwrk1 = 0x1;		/* next SRAM address */
6860	isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
6861	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
6862		isp_prt(isp, ISP_LOGWARN,
6863		    "RAM DUMP FAILED @ WORD %x", 0x10000 + isp->isp_mbxwrk1);
6864		return;
6865	}
6866	ptr = isp->isp_mbxworkp;	/* finish final word */
6867	*ptr++ = mbs.param[2];
6868	isp_prt(isp, ISP_LOGALL, "isp_fw_dump: SRAM dumped successfully");
6869	FCPARAM(isp)->isp_dump_data[0] = isp->isp_type; /* now used */
6870	(void) isp_async(isp, ISPASYNC_FW_DUMPED, 0);
6871}
6872
6873void
6874isp_fw_dump(ispsoftc_t *isp)
6875{
6876	if (IS_2200(isp))
6877		isp2200_fw_dump(isp);
6878	else if (IS_23XX(isp))
6879		isp2300_fw_dump(isp);
6880}
6881#endif
6882