isp.c revision 157943
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 157943 2006-04-21 18:30:01Z 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 = (arg)? *((int *) arg) : 250000;
3519			return (isp_fclink_test(isp, usdelay));
3520		}
3521		break;
3522
3523	case ISPCTL_SCAN_FABRIC:
3524
3525		if (IS_FC(isp)) {
3526			int ftype = (arg)? *((int *) arg) : FC4_SCSI;
3527			return (isp_scan_fabric(isp, ftype));
3528		}
3529		break;
3530
3531	case ISPCTL_SCAN_LOOP:
3532
3533		if (IS_FC(isp)) {
3534			return (isp_scan_loop(isp));
3535		}
3536		break;
3537
3538	case ISPCTL_PDB_SYNC:
3539
3540		if (IS_FC(isp)) {
3541			return (isp_pdb_sync(isp));
3542		}
3543		break;
3544
3545	case ISPCTL_SEND_LIP:
3546
3547		if (IS_FC(isp)) {
3548			mbs.param[0] = MBOX_INIT_LIP;
3549			isp_mboxcmd(isp, &mbs, MBLOGALL);
3550			if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
3551				return (0);
3552			}
3553		}
3554		break;
3555
3556	case ISPCTL_GET_POSMAP:
3557
3558		if (IS_FC(isp) && arg) {
3559			return (isp_getmap(isp, arg));
3560		}
3561		break;
3562
3563
3564	case ISPCTL_GET_PDB:
3565		if (IS_FC(isp) && arg) {
3566			int id = *((int *)arg);
3567			isp_pdb_t *pdb = arg;
3568			return (isp_getpdb(isp, id, pdb));
3569		}
3570		break;
3571
3572	case ISPCTL_RUN_MBOXCMD:
3573
3574		isp_mboxcmd(isp, arg, MBLOGALL);
3575		return(0);
3576
3577#ifdef	ISP_TARGET_MODE
3578	case ISPCTL_TOGGLE_TMODE:
3579	{
3580
3581		/*
3582		 * We don't check/set against role here- that's the
3583		 * responsibility for the outer layer to coordinate.
3584		 */
3585		if (IS_SCSI(isp)) {
3586			int param = *(int *)arg;
3587			mbs.param[0] = MBOX_ENABLE_TARGET_MODE;
3588			mbs.param[1] = param & 0xffff;
3589			mbs.param[2] = param >> 16;
3590			isp_mboxcmd(isp, &mbs, MBLOGALL);
3591			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3592				break;
3593			}
3594		}
3595		return (0);
3596	}
3597#endif
3598	}
3599	return (-1);
3600}
3601
3602/*
3603 * Interrupt Service Routine(s).
3604 *
3605 * External (OS) framework has done the appropriate locking,
3606 * and the locking will be held throughout this function.
3607 */
3608
3609/*
3610 * Limit our stack depth by sticking with the max likely number
3611 * of completions on a request queue at any one time.
3612 */
3613#ifndef	MAX_REQUESTQ_COMPLETIONS
3614#define	MAX_REQUESTQ_COMPLETIONS	64
3615#endif
3616
3617void
3618isp_intr(ispsoftc_t *isp, uint16_t isr, uint16_t sema, uint16_t mbox)
3619{
3620	XS_T *complist[MAX_REQUESTQ_COMPLETIONS], *xs;
3621	uint16_t iptr, optr, junk;
3622	int i, nlooked = 0, ndone = 0;
3623
3624again:
3625	/*
3626	 * Is this a mailbox related interrupt?
3627	 * The mailbox semaphore will be nonzero if so.
3628	 */
3629	if (sema) {
3630		if (mbox & 0x4000) {
3631			isp->isp_intmboxc++;
3632			if (isp->isp_mboxbsy) {
3633				int i = 0, obits = isp->isp_obits;
3634				isp->isp_mboxtmp[i++] = mbox;
3635				for (i = 1; i < MAX_MAILBOX(isp); i++) {
3636					if ((obits & (1 << i)) == 0) {
3637						continue;
3638					}
3639					isp->isp_mboxtmp[i] =
3640					    ISP_READ(isp, MBOX_OFF(i));
3641				}
3642				if (isp->isp_mbxwrk0) {
3643					if (isp_mbox_continue(isp) == 0) {
3644						return;
3645					}
3646				}
3647				MBOX_NOTIFY_COMPLETE(isp);
3648			} else {
3649				isp_prt(isp, ISP_LOGWARN,
3650				    "Mbox Command Async (0x%x) with no waiters",
3651				    mbox);
3652			}
3653		} else if (isp_parse_async(isp, mbox) < 0) {
3654			return;
3655		}
3656		if ((IS_FC(isp) && mbox != ASYNC_RIO_RESP) ||
3657		    isp->isp_state != ISP_RUNSTATE) {
3658			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3659			ISP_WRITE(isp, BIU_SEMA, 0);
3660			return;
3661		}
3662	}
3663
3664	/*
3665	 * We can't be getting this now.
3666	 */
3667	if (isp->isp_state != ISP_RUNSTATE) {
3668		isp_prt(isp, ISP_LOGWARN,
3669		    "interrupt (ISR=%x SEMA=%x) when not ready", isr, sema);
3670		/*
3671		 * Thank you very much!  *Burrrp*!
3672		 */
3673		WRITE_RESPONSE_QUEUE_OUT_POINTER(isp,
3674		    READ_RESPONSE_QUEUE_IN_POINTER(isp));
3675
3676		ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3677		ISP_WRITE(isp, BIU_SEMA, 0);
3678		return;
3679	}
3680
3681	/*
3682	 * Get the current Response Queue Out Pointer.
3683	 *
3684	 * If we're a 2300, we can ask what hardware what it thinks.
3685	 */
3686	if (IS_23XX(isp)) {
3687		optr = ISP_READ(isp, isp->isp_respoutrp);
3688		/*
3689		 * Debug: to be taken out eventually
3690		 */
3691		if (isp->isp_residx != optr) {
3692			isp_prt(isp, ISP_LOGWARN, "optr %x soft optr %x",
3693			    optr, isp->isp_residx);
3694		}
3695	} else {
3696		optr = isp->isp_residx;
3697	}
3698
3699	/*
3700	 * You *must* read the Response Queue In Pointer
3701	 * prior to clearing the RISC interrupt.
3702	 *
3703	 * Debounce the 2300 if revision less than 2.
3704	 */
3705	if (IS_2100(isp) || (IS_2300(isp) && isp->isp_revision < 2)) {
3706		i = 0;
3707		do {
3708			iptr = READ_RESPONSE_QUEUE_IN_POINTER(isp);
3709			junk = READ_RESPONSE_QUEUE_IN_POINTER(isp);
3710		} while (junk != iptr && ++i < 1000);
3711
3712		if (iptr != junk) {
3713			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3714			isp_prt(isp, ISP_LOGWARN,
3715			    "Response Queue Out Pointer Unstable (%x, %x)",
3716			    iptr, junk);
3717			return;
3718		}
3719	} else {
3720		iptr = READ_RESPONSE_QUEUE_IN_POINTER(isp);
3721	}
3722	isp->isp_resodx = iptr;
3723
3724
3725	if (optr == iptr && sema == 0) {
3726		/*
3727		 * There are a lot of these- reasons unknown- mostly on
3728		 * faster Alpha machines.
3729		 *
3730		 * I tried delaying after writing HCCR_CMD_CLEAR_RISC_INT to
3731		 * make sure the old interrupt went away (to avoid 'ringing'
3732		 * effects), but that didn't stop this from occurring.
3733		 */
3734		if (IS_23XX(isp)) {
3735			USEC_DELAY(100);
3736			iptr = READ_RESPONSE_QUEUE_IN_POINTER(isp);
3737			junk = ISP_READ(isp, BIU_R2HSTSLO);
3738		} else {
3739			junk = ISP_READ(isp, BIU_ISR);
3740		}
3741		if (optr == iptr) {
3742			if (IS_23XX(isp)) {
3743				;
3744			} else {
3745				sema = ISP_READ(isp, BIU_SEMA);
3746				mbox = ISP_READ(isp, OUTMAILBOX0);
3747				if ((sema & 0x3) && (mbox & 0x8000)) {
3748					goto again;
3749				}
3750			}
3751			isp->isp_intbogus++;
3752			isp_prt(isp, ISP_LOGDEBUG1,
3753			    "bogus intr- isr %x (%x) iptr %x optr %x",
3754			    isr, junk, iptr, optr);
3755		}
3756	}
3757	isp->isp_resodx = iptr;
3758	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3759	ISP_WRITE(isp, BIU_SEMA, 0);
3760
3761	if (isp->isp_rspbsy) {
3762		return;
3763	}
3764	isp->isp_rspbsy = 1;
3765
3766	while (optr != iptr) {
3767		ispstatusreq_t local, *sp = &local;
3768		isphdr_t *hp;
3769		int type;
3770		uint16_t oop;
3771		int buddaboom = 0;
3772
3773		hp = (isphdr_t *) ISP_QUEUE_ENTRY(isp->isp_result, optr);
3774		oop = optr;
3775		optr = ISP_NXT_QENTRY(optr, RESULT_QUEUE_LEN(isp));
3776		nlooked++;
3777 read_again:
3778		/*
3779		 * Synchronize our view of this response queue entry.
3780		 */
3781		MEMORYBARRIER(isp, SYNC_RESULT, oop, QENTRY_LEN);
3782
3783		type = isp_get_response_type(isp, hp);
3784
3785		if (type == RQSTYPE_RESPONSE) {
3786			isp_get_response(isp, (ispstatusreq_t *) hp, sp);
3787		} else if (type == RQSTYPE_RIO2) {
3788			isp_rio2_t rio;
3789			isp_get_rio2(isp, (isp_rio2_t *) hp, &rio);
3790			for (i = 0; i < rio.req_header.rqs_seqno; i++) {
3791				isp_fastpost_complete(isp, rio.req_handles[i]);
3792			}
3793			if (isp->isp_fpcchiwater < rio.req_header.rqs_seqno)
3794				isp->isp_fpcchiwater = rio.req_header.rqs_seqno;
3795			MEMZERO(hp, QENTRY_LEN);	/* PERF */
3796			continue;
3797		} else {
3798			/*
3799			 * Somebody reachable via isp_handle_other_response
3800			 * may have updated the response queue pointers for
3801			 * us, so we reload our goal index.
3802			 */
3803			int i = isp_handle_other_response(isp, type, hp, &optr);
3804			if (i < 0) {
3805				goto read_again;
3806			} else if (i > 0) {
3807				iptr = isp->isp_resodx;
3808				MEMZERO(hp, QENTRY_LEN);	/* PERF */
3809				continue;
3810			}
3811
3812			/*
3813			 * After this point, we'll just look at the header as
3814			 * we don't know how to deal with the rest of the
3815			 * response.
3816			 */
3817			isp_get_response(isp, (ispstatusreq_t *) hp, sp);
3818
3819			/*
3820			 * It really has to be a bounced request just copied
3821			 * from the request queue to the response queue. If
3822			 * not, something bad has happened.
3823			 */
3824			if (sp->req_header.rqs_entry_type != RQSTYPE_REQUEST) {
3825				isp_prt(isp, ISP_LOGERR, notresp,
3826				    sp->req_header.rqs_entry_type, oop, optr,
3827				    nlooked);
3828				if (isp->isp_dblev & ISP_LOGDEBUG0) {
3829					isp_print_bytes(isp, "Queue Entry",
3830					    QENTRY_LEN, sp);
3831				}
3832				MEMZERO(hp, QENTRY_LEN);	/* PERF */
3833				continue;
3834			}
3835			buddaboom = 1;
3836		}
3837
3838		if (sp->req_header.rqs_flags & 0xf) {
3839#define	_RQS_OFLAGS	\
3840	~(RQSFLAG_CONTINUATION|RQSFLAG_FULL|RQSFLAG_BADHEADER|RQSFLAG_BADPACKET)
3841			if (sp->req_header.rqs_flags & RQSFLAG_CONTINUATION) {
3842				isp_prt(isp, ISP_LOGWARN,
3843				    "continuation segment");
3844				WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
3845				continue;
3846			}
3847			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
3848				isp_prt(isp, ISP_LOGDEBUG1,
3849				    "internal queues full");
3850				/*
3851				 * We'll synthesize a QUEUE FULL message below.
3852				 */
3853			}
3854			if (sp->req_header.rqs_flags & RQSFLAG_BADHEADER) {
3855				isp_prt(isp, ISP_LOGERR,  "bad header flag");
3856				buddaboom++;
3857			}
3858			if (sp->req_header.rqs_flags & RQSFLAG_BADPACKET) {
3859				isp_prt(isp, ISP_LOGERR, "bad request packet");
3860				buddaboom++;
3861			}
3862			if (sp->req_header.rqs_flags & _RQS_OFLAGS) {
3863				isp_prt(isp, ISP_LOGERR,
3864				    "unknown flags (0x%x) in response",
3865				    sp->req_header.rqs_flags);
3866				buddaboom++;
3867			}
3868#undef	_RQS_OFLAGS
3869		}
3870		if (sp->req_handle > isp->isp_maxcmds || sp->req_handle < 1) {
3871			MEMZERO(hp, QENTRY_LEN);	/* PERF */
3872			isp_prt(isp, ISP_LOGERR,
3873			    "bad request handle %d (type 0x%x, flags 0x%x)",
3874			    sp->req_handle, sp->req_header.rqs_entry_type,
3875			    sp->req_header.rqs_flags);
3876			WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
3877			continue;
3878		}
3879		xs = isp_find_xs(isp, sp->req_handle);
3880		if (xs == NULL) {
3881			uint8_t ts = sp->req_completion_status & 0xff;
3882			MEMZERO(hp, QENTRY_LEN);	/* PERF */
3883			/*
3884			 * Only whine if this isn't the expected fallout of
3885			 * aborting the command.
3886			 */
3887			if (sp->req_header.rqs_entry_type != RQSTYPE_RESPONSE) {
3888				isp_prt(isp, ISP_LOGERR,
3889				    "cannot find handle 0x%x (type 0x%x)",
3890				    sp->req_handle,
3891				    sp->req_header.rqs_entry_type);
3892			} else if (ts != RQCS_ABORTED) {
3893				isp_prt(isp, ISP_LOGERR,
3894				    "cannot find handle 0x%x (status 0x%x)",
3895				    sp->req_handle, ts);
3896			}
3897			WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
3898			continue;
3899		}
3900		isp_destroy_handle(isp, sp->req_handle);
3901		if (sp->req_status_flags & RQSTF_BUS_RESET) {
3902			XS_SETERR(xs, HBA_BUSRESET);
3903			isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
3904		}
3905		if (buddaboom) {
3906			XS_SETERR(xs, HBA_BOTCH);
3907		}
3908
3909		if (IS_FC(isp) && (sp->req_scsi_status & RQCS_SV)) {
3910			/*
3911			 * Fibre Channel F/W doesn't say we got status
3912			 * if there's Sense Data instead. I guess they
3913			 * think it goes w/o saying.
3914			 */
3915			sp->req_state_flags |= RQSF_GOT_STATUS;
3916		}
3917		if (sp->req_state_flags & RQSF_GOT_STATUS) {
3918			*XS_STSP(xs) = sp->req_scsi_status & 0xff;
3919		}
3920
3921		switch (sp->req_header.rqs_entry_type) {
3922		case RQSTYPE_RESPONSE:
3923			XS_SET_STATE_STAT(isp, xs, sp);
3924			isp_parse_status(isp, sp, xs);
3925			if ((XS_NOERR(xs) || XS_ERR(xs) == HBA_NOERROR) &&
3926			    (*XS_STSP(xs) == SCSI_BUSY)) {
3927				XS_SETERR(xs, HBA_TGTBSY);
3928			}
3929			if (IS_SCSI(isp)) {
3930				XS_RESID(xs) = sp->req_resid;
3931				if ((sp->req_state_flags & RQSF_GOT_STATUS) &&
3932				    (*XS_STSP(xs) == SCSI_CHECK) &&
3933				    (sp->req_state_flags & RQSF_GOT_SENSE)) {
3934					XS_SAVE_SENSE(xs, sp);
3935				}
3936				/*
3937				 * A new synchronous rate was negotiated for
3938				 * this target. Mark state such that we'll go
3939				 * look up that which has changed later.
3940				 */
3941				if (sp->req_status_flags & RQSTF_NEGOTIATION) {
3942					int t = XS_TGT(xs);
3943					sdparam *sdp = isp->isp_param;
3944					sdp += XS_CHANNEL(xs);
3945					sdp->isp_devparam[t].dev_refresh = 1;
3946					isp->isp_update |=
3947					    (1 << XS_CHANNEL(xs));
3948				}
3949			} else {
3950				if (sp->req_status_flags & RQSF_XFER_COMPLETE) {
3951					XS_RESID(xs) = 0;
3952				} else if (sp->req_scsi_status & RQCS_RESID) {
3953					XS_RESID(xs) = sp->req_resid;
3954				} else {
3955					XS_RESID(xs) = 0;
3956				}
3957				if ((sp->req_state_flags & RQSF_GOT_STATUS) &&
3958				    (*XS_STSP(xs) == SCSI_CHECK) &&
3959				    (sp->req_scsi_status & RQCS_SV)) {
3960					XS_SAVE_SENSE(xs, sp);
3961					/* solely for the benefit of debug */
3962					sp->req_state_flags |= RQSF_GOT_SENSE;
3963				}
3964			}
3965			isp_prt(isp, ISP_LOGDEBUG2,
3966			   "asked for %ld got resid %ld", (long) XS_XFRLEN(xs),
3967			   (long) sp->req_resid);
3968			break;
3969		case RQSTYPE_REQUEST:
3970			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
3971				/*
3972				 * Force Queue Full status.
3973				 */
3974				*XS_STSP(xs) = SCSI_QFULL;
3975				XS_SETERR(xs, HBA_NOERROR);
3976			} else if (XS_NOERR(xs)) {
3977				/*
3978				 * ????
3979				 */
3980				isp_prt(isp, ISP_LOGDEBUG0,
3981				    "Request Queue Entry bounced back");
3982				XS_SETERR(xs, HBA_BOTCH);
3983			}
3984			XS_RESID(xs) = XS_XFRLEN(xs);
3985			break;
3986		default:
3987			isp_prt(isp, ISP_LOGWARN,
3988			    "unhandled response queue type 0x%x",
3989			    sp->req_header.rqs_entry_type);
3990			if (XS_NOERR(xs)) {
3991				XS_SETERR(xs, HBA_BOTCH);
3992			}
3993			break;
3994		}
3995
3996		/*
3997		 * Free any DMA resources. As a side effect, this may
3998		 * also do any cache flushing necessary for data coherence.			 */
3999		if (XS_XFRLEN(xs)) {
4000			ISP_DMAFREE(isp, xs, sp->req_handle);
4001		}
4002
4003		if (((isp->isp_dblev & (ISP_LOGDEBUG2|ISP_LOGDEBUG3))) ||
4004		    ((isp->isp_dblev & ISP_LOGDEBUG1) && ((!XS_NOERR(xs)) ||
4005		    (*XS_STSP(xs) != SCSI_GOOD)))) {
4006			char skey;
4007			if (sp->req_state_flags & RQSF_GOT_SENSE) {
4008				skey = XS_SNSKEY(xs) & 0xf;
4009				if (skey < 10)
4010					skey += '0';
4011				else
4012					skey += 'a' - 10;
4013			} else if (*XS_STSP(xs) == SCSI_CHECK) {
4014				skey = '?';
4015			} else {
4016				skey = '.';
4017			}
4018			isp_prt(isp, ISP_LOGALL, finmsg, XS_CHANNEL(xs),
4019			    XS_TGT(xs), XS_LUN(xs), XS_XFRLEN(xs), XS_RESID(xs),
4020			    *XS_STSP(xs), skey, XS_ERR(xs));
4021		}
4022
4023		if (isp->isp_nactive > 0)
4024		    isp->isp_nactive--;
4025		complist[ndone++] = xs;	/* defer completion call until later */
4026		MEMZERO(hp, QENTRY_LEN);	/* PERF */
4027		if (ndone == MAX_REQUESTQ_COMPLETIONS) {
4028			break;
4029		}
4030	}
4031
4032	/*
4033	 * If we looked at any commands, then it's valid to find out
4034	 * what the outpointer is. It also is a trigger to update the
4035	 * ISP's notion of what we've seen so far.
4036	 */
4037	if (nlooked) {
4038		WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
4039		/*
4040		 * While we're at it, read the requst queue out pointer.
4041		 */
4042		isp->isp_reqodx = READ_REQUEST_QUEUE_OUT_POINTER(isp);
4043		if (isp->isp_rscchiwater < ndone)
4044			isp->isp_rscchiwater = ndone;
4045	}
4046
4047	isp->isp_residx = optr;
4048	isp->isp_rspbsy = 0;
4049	for (i = 0; i < ndone; i++) {
4050		xs = complist[i];
4051		if (xs) {
4052			isp->isp_rsltccmplt++;
4053			isp_done(xs);
4054		}
4055	}
4056}
4057
4058/*
4059 * Support routines.
4060 */
4061
4062static int
4063isp_parse_async(ispsoftc_t *isp, uint16_t mbox)
4064{
4065	int rval = 0;
4066	int bus;
4067
4068	if (IS_DUALBUS(isp)) {
4069		bus = ISP_READ(isp, OUTMAILBOX6);
4070	} else {
4071		bus = 0;
4072	}
4073	isp_prt(isp, ISP_LOGDEBUG2, "Async Mbox 0x%x", mbox);
4074
4075	switch (mbox) {
4076	case ASYNC_BUS_RESET:
4077		isp->isp_sendmarker |= (1 << bus);
4078#ifdef	ISP_TARGET_MODE
4079		if (isp_target_async(isp, bus, mbox))
4080			rval = -1;
4081#endif
4082		isp_async(isp, ISPASYNC_BUS_RESET, &bus);
4083		break;
4084	case ASYNC_SYSTEM_ERROR:
4085#ifdef	ISP_FW_CRASH_DUMP
4086		/*
4087		 * If we have crash dumps enabled, it's up to the handler
4088		 * for isp_async to reinit stuff and restart the firmware
4089		 * after performing the crash dump. The reason we do things
4090		 * this way is that we may need to activate a kernel thread
4091		 * to do all the crash dump goop.
4092		 */
4093		isp_async(isp, ISPASYNC_FW_CRASH, NULL);
4094#else
4095		isp_async(isp, ISPASYNC_FW_CRASH, NULL);
4096		isp_reinit(isp);
4097		isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
4098#endif
4099		rval = -1;
4100		break;
4101
4102	case ASYNC_RQS_XFER_ERR:
4103		isp_prt(isp, ISP_LOGERR, "Request Queue Transfer Error");
4104		break;
4105
4106	case ASYNC_RSP_XFER_ERR:
4107		isp_prt(isp, ISP_LOGERR, "Response Queue Transfer Error");
4108		break;
4109
4110	case ASYNC_QWAKEUP:
4111		/*
4112		 * We've just been notified that the Queue has woken up.
4113		 * We don't need to be chatty about this- just unlatch things
4114		 * and move on.
4115		 */
4116		mbox = READ_REQUEST_QUEUE_OUT_POINTER(isp);
4117		break;
4118
4119	case ASYNC_TIMEOUT_RESET:
4120		isp_prt(isp, ISP_LOGWARN,
4121		    "timeout initiated SCSI bus reset of bus %d", bus);
4122		isp->isp_sendmarker |= (1 << bus);
4123#ifdef	ISP_TARGET_MODE
4124		if (isp_target_async(isp, bus, mbox))
4125			rval = -1;
4126#endif
4127		break;
4128
4129	case ASYNC_DEVICE_RESET:
4130		isp_prt(isp, ISP_LOGINFO, "device reset on bus %d", bus);
4131		isp->isp_sendmarker |= (1 << bus);
4132#ifdef	ISP_TARGET_MODE
4133		if (isp_target_async(isp, bus, mbox))
4134			rval = -1;
4135#endif
4136		break;
4137
4138	case ASYNC_EXTMSG_UNDERRUN:
4139		isp_prt(isp, ISP_LOGWARN, "extended message underrun");
4140		break;
4141
4142	case ASYNC_SCAM_INT:
4143		isp_prt(isp, ISP_LOGINFO, "SCAM interrupt");
4144		break;
4145
4146	case ASYNC_HUNG_SCSI:
4147		isp_prt(isp, ISP_LOGERR,
4148		    "stalled SCSI Bus after DATA Overrun");
4149		/* XXX: Need to issue SCSI reset at this point */
4150		break;
4151
4152	case ASYNC_KILLED_BUS:
4153		isp_prt(isp, ISP_LOGERR, "SCSI Bus reset after DATA Overrun");
4154		break;
4155
4156	case ASYNC_BUS_TRANSIT:
4157		mbox = ISP_READ(isp, OUTMAILBOX2);
4158		switch (mbox & 0x1c00) {
4159		case SXP_PINS_LVD_MODE:
4160			isp_prt(isp, ISP_LOGINFO, "Transition to LVD mode");
4161			SDPARAM(isp)->isp_diffmode = 0;
4162			SDPARAM(isp)->isp_ultramode = 0;
4163			SDPARAM(isp)->isp_lvdmode = 1;
4164			break;
4165		case SXP_PINS_HVD_MODE:
4166			isp_prt(isp, ISP_LOGINFO,
4167			    "Transition to Differential mode");
4168			SDPARAM(isp)->isp_diffmode = 1;
4169			SDPARAM(isp)->isp_ultramode = 0;
4170			SDPARAM(isp)->isp_lvdmode = 0;
4171			break;
4172		case SXP_PINS_SE_MODE:
4173			isp_prt(isp, ISP_LOGINFO,
4174			    "Transition to Single Ended mode");
4175			SDPARAM(isp)->isp_diffmode = 0;
4176			SDPARAM(isp)->isp_ultramode = 1;
4177			SDPARAM(isp)->isp_lvdmode = 0;
4178			break;
4179		default:
4180			isp_prt(isp, ISP_LOGWARN,
4181			    "Transition to Unknown Mode 0x%x", mbox);
4182			break;
4183		}
4184		/*
4185		 * XXX: Set up to renegotiate again!
4186		 */
4187		/* Can only be for a 1080... */
4188		isp->isp_sendmarker |= (1 << bus);
4189		break;
4190
4191	/*
4192	 * We can use bus, which will always be zero for FC cards,
4193	 * as a mailbox pattern accumulator to be checked below.
4194	 */
4195	case ASYNC_RIO5:
4196		bus = 0x1ce;	/* outgoing mailbox regs 1-3, 6-7 */
4197		break;
4198
4199	case ASYNC_RIO4:
4200		bus = 0x14e;	/* outgoing mailbox regs 1-3, 6 */
4201		break;
4202
4203	case ASYNC_RIO3:
4204		bus = 0x10e;	/* outgoing mailbox regs 1-3 */
4205		break;
4206
4207	case ASYNC_RIO2:
4208		bus = 0x106;	/* outgoing mailbox regs 1-2 */
4209		break;
4210
4211	case ASYNC_RIO1:
4212	case ASYNC_CMD_CMPLT:
4213		bus = 0x102;	/* outgoing mailbox regs 1 */
4214		break;
4215
4216	case ASYNC_RIO_RESP:
4217		return (rval);
4218
4219	case ASYNC_CTIO_DONE:
4220	{
4221#ifdef	ISP_TARGET_MODE
4222		int handle =
4223		    (ISP_READ(isp, OUTMAILBOX2) << 16) |
4224		    (ISP_READ(isp, OUTMAILBOX1));
4225		if (isp_target_async(isp, handle, mbox)) {
4226			rval = -1;
4227		} else {
4228			/* count it as a fast posting intr */
4229			isp->isp_fphccmplt++;
4230		}
4231#else
4232		isp_prt(isp, ISP_LOGINFO, "Fast Posting CTIO done");
4233		isp->isp_fphccmplt++;	/* count it as a fast posting intr */
4234#endif
4235		break;
4236	}
4237	case ASYNC_LIP_F8:
4238	case ASYNC_LIP_OCCURRED:
4239		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4240		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
4241		isp->isp_sendmarker = 1;
4242		isp_mark_getpdb_all(isp);
4243		isp_async(isp, ISPASYNC_LIP, NULL);
4244#ifdef	ISP_TARGET_MODE
4245		if (isp_target_async(isp, bus, mbox))
4246			rval = -1;
4247#endif
4248		/*
4249		 * We've had problems with data corruption occuring on
4250		 * commands that complete (with no apparent error) after
4251		 * we receive a LIP. This has been observed mostly on
4252		 * Local Loop topologies. To be safe, let's just mark
4253		 * all active commands as dead.
4254		 */
4255		if (FCPARAM(isp)->isp_topo == TOPO_NL_PORT ||
4256		    FCPARAM(isp)->isp_topo == TOPO_FL_PORT) {
4257			int i, j;
4258			for (i = j = 0; i < isp->isp_maxcmds; i++) {
4259				XS_T *xs;
4260				xs = isp->isp_xflist[i];
4261				if (xs != NULL) {
4262					j++;
4263					XS_SETERR(xs, HBA_BUSRESET);
4264				}
4265			}
4266			if (j) {
4267				isp_prt(isp, ISP_LOGERR,
4268				    "LIP destroyed %d active commands", j);
4269			}
4270		}
4271		break;
4272
4273	case ASYNC_LOOP_UP:
4274		isp->isp_sendmarker = 1;
4275		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4276		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
4277		isp_mark_getpdb_all(isp);
4278		isp_async(isp, ISPASYNC_LOOP_UP, NULL);
4279#ifdef	ISP_TARGET_MODE
4280		if (isp_target_async(isp, bus, mbox))
4281			rval = -1;
4282#endif
4283		break;
4284
4285	case ASYNC_LOOP_DOWN:
4286		isp->isp_sendmarker = 1;
4287		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4288		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
4289		isp_mark_getpdb_all(isp);
4290		isp_async(isp, ISPASYNC_LOOP_DOWN, NULL);
4291#ifdef	ISP_TARGET_MODE
4292		if (isp_target_async(isp, bus, mbox))
4293			rval = -1;
4294#endif
4295		break;
4296
4297	case ASYNC_LOOP_RESET:
4298		isp->isp_sendmarker = 1;
4299		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4300		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
4301		isp_mark_getpdb_all(isp);
4302		isp_async(isp, ISPASYNC_LOOP_RESET, NULL);
4303#ifdef	ISP_TARGET_MODE
4304		if (isp_target_async(isp, bus, mbox))
4305			rval = -1;
4306#endif
4307		break;
4308
4309	case ASYNC_PDB_CHANGED:
4310		isp->isp_sendmarker = 1;
4311		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
4312		isp_mark_getpdb_all(isp);
4313		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_PDB);
4314		break;
4315
4316	case ASYNC_CHANGE_NOTIFY:
4317		/*
4318		 * Not correct, but it will force us to rescan the loop.
4319		 */
4320		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
4321		isp_mark_getpdb_all(isp);
4322		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_SNS);
4323		break;
4324
4325	case ASYNC_PTPMODE:
4326		if (FCPARAM(isp)->isp_onfabric)
4327			FCPARAM(isp)->isp_topo = TOPO_F_PORT;
4328		else
4329			FCPARAM(isp)->isp_topo = TOPO_N_PORT;
4330		isp_mark_getpdb_all(isp);
4331		isp->isp_sendmarker = 1;
4332		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4333		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
4334		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
4335#ifdef	ISP_TARGET_MODE
4336		if (isp_target_async(isp, bus, mbox))
4337			rval = -1;
4338#endif
4339		isp_prt(isp, ISP_LOGINFO, "Point-to-Point mode");
4340		break;
4341
4342	case ASYNC_CONNMODE:
4343		mbox = ISP_READ(isp, OUTMAILBOX1);
4344		isp_mark_getpdb_all(isp);
4345		switch (mbox) {
4346		case ISP_CONN_LOOP:
4347			isp_prt(isp, ISP_LOGINFO,
4348			    "Point-to-Point -> Loop mode");
4349			break;
4350		case ISP_CONN_PTP:
4351			isp_prt(isp, ISP_LOGINFO,
4352			    "Loop -> Point-to-Point mode");
4353			break;
4354		case ISP_CONN_BADLIP:
4355			isp_prt(isp, ISP_LOGWARN,
4356			    "Point-to-Point -> Loop mode (BAD LIP)");
4357			break;
4358		case ISP_CONN_FATAL:
4359			isp_prt(isp, ISP_LOGERR, "FATAL CONNECTION ERROR");
4360#ifdef	ISP_FW_CRASH_DUMP
4361			isp_async(isp, ISPASYNC_FW_CRASH, NULL);
4362#else
4363			isp_async(isp, ISPASYNC_FW_CRASH, NULL);
4364			isp_reinit(isp);
4365			isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
4366#endif
4367			return (-1);
4368		case ISP_CONN_LOOPBACK:
4369			isp_prt(isp, ISP_LOGWARN,
4370			    "Looped Back in Point-to-Point mode");
4371			break;
4372		default:
4373			isp_prt(isp, ISP_LOGWARN,
4374			    "Unknown connection mode (0x%x)", mbox);
4375			break;
4376		}
4377		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
4378		isp->isp_sendmarker = 1;
4379		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4380		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
4381		break;
4382
4383	default:
4384		isp_prt(isp, ISP_LOGWARN, "Unknown Async Code 0x%x", mbox);
4385		break;
4386	}
4387
4388	if (bus & 0x100) {
4389		int i, nh;
4390		uint16_t handles[16];
4391
4392		for (nh = 0, i = 1; i < MAX_MAILBOX(isp); i++) {
4393			if ((bus & (1 << i)) == 0) {
4394				continue;
4395			}
4396			handles[nh++] = ISP_READ(isp, MBOX_OFF(i));
4397		}
4398		for (i = 0; i < nh; i++) {
4399			isp_fastpost_complete(isp, handles[i]);
4400			isp_prt(isp,  ISP_LOGDEBUG3,
4401			    "fast post completion of %u", handles[i]);
4402		}
4403		if (isp->isp_fpcchiwater < nh)
4404			isp->isp_fpcchiwater = nh;
4405	} else {
4406		isp->isp_intoasync++;
4407	}
4408	return (rval);
4409}
4410
4411/*
4412 * Handle other response entries. A pointer to the request queue output
4413 * index is here in case we want to eat several entries at once, although
4414 * this is not used currently.
4415 */
4416
4417static int
4418isp_handle_other_response(ispsoftc_t *isp, int type,
4419    isphdr_t *hp, uint16_t *optrp)
4420{
4421	switch (type) {
4422	case RQSTYPE_STATUS_CONT:
4423		isp_prt(isp, ISP_LOGINFO, "Ignored Continuation Response");
4424		return (1);
4425	case RQSTYPE_ATIO:
4426	case RQSTYPE_CTIO:
4427	case RQSTYPE_ENABLE_LUN:
4428	case RQSTYPE_MODIFY_LUN:
4429	case RQSTYPE_NOTIFY:
4430	case RQSTYPE_NOTIFY_ACK:
4431	case RQSTYPE_CTIO1:
4432	case RQSTYPE_ATIO2:
4433	case RQSTYPE_CTIO2:
4434	case RQSTYPE_CTIO3:
4435		isp->isp_rsltccmplt++;	/* count as a response completion */
4436#ifdef	ISP_TARGET_MODE
4437		if (isp_target_notify(isp, (ispstatusreq_t *) hp, optrp)) {
4438			return (1);
4439		}
4440#endif
4441		/* FALLTHROUGH */
4442	case RQSTYPE_REQUEST:
4443	default:
4444		USEC_DELAY(100);
4445		if (type != isp_get_response_type(isp, hp)) {
4446			/*
4447			 * This is questionable- we're just papering over
4448			 * something we've seen on SMP linux in target
4449			 * mode- we don't really know what's happening
4450			 * here that causes us to think we've gotten
4451			 * an entry, but that either the entry isn't
4452			 * filled out yet or our CPU read data is stale.
4453			 */
4454			isp_prt(isp, ISP_LOGINFO,
4455				"unstable type in response queue");
4456			return (-1);
4457		}
4458		isp_prt(isp, ISP_LOGWARN, "Unhandled Response Type 0x%x",
4459		    isp_get_response_type(isp, hp));
4460		if (isp_async(isp, ISPASYNC_UNHANDLED_RESPONSE, hp)) {
4461			return (1);
4462		}
4463		return (0);
4464	}
4465}
4466
4467static void
4468isp_parse_status(ispsoftc_t *isp, ispstatusreq_t *sp, XS_T *xs)
4469{
4470	switch (sp->req_completion_status & 0xff) {
4471	case RQCS_COMPLETE:
4472		if (XS_NOERR(xs)) {
4473			XS_SETERR(xs, HBA_NOERROR);
4474		}
4475		return;
4476
4477	case RQCS_INCOMPLETE:
4478		if ((sp->req_state_flags & RQSF_GOT_TARGET) == 0) {
4479			isp_prt(isp, ISP_LOGDEBUG1,
4480			    "Selection Timeout for %d.%d.%d",
4481			    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4482			if (XS_NOERR(xs)) {
4483				XS_SETERR(xs, HBA_SELTIMEOUT);
4484			}
4485			return;
4486		}
4487		isp_prt(isp, ISP_LOGERR,
4488		    "command incomplete for %d.%d.%d, state 0x%x",
4489		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs),
4490		    sp->req_state_flags);
4491		break;
4492
4493	case RQCS_DMA_ERROR:
4494		isp_prt(isp, ISP_LOGERR, "DMA error for command on %d.%d.%d",
4495		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4496		break;
4497
4498	case RQCS_TRANSPORT_ERROR:
4499	{
4500		char buf[172];
4501		SNPRINTF(buf, sizeof (buf), "states=>");
4502		if (sp->req_state_flags & RQSF_GOT_BUS) {
4503			SNPRINTF(buf, sizeof (buf), "%s GOT_BUS", buf);
4504		}
4505		if (sp->req_state_flags & RQSF_GOT_TARGET) {
4506			SNPRINTF(buf, sizeof (buf), "%s GOT_TGT", buf);
4507		}
4508		if (sp->req_state_flags & RQSF_SENT_CDB) {
4509			SNPRINTF(buf, sizeof (buf), "%s SENT_CDB", buf);
4510		}
4511		if (sp->req_state_flags & RQSF_XFRD_DATA) {
4512			SNPRINTF(buf, sizeof (buf), "%s XFRD_DATA", buf);
4513		}
4514		if (sp->req_state_flags & RQSF_GOT_STATUS) {
4515			SNPRINTF(buf, sizeof (buf), "%s GOT_STS", buf);
4516		}
4517		if (sp->req_state_flags & RQSF_GOT_SENSE) {
4518			SNPRINTF(buf, sizeof (buf), "%s GOT_SNS", buf);
4519		}
4520		if (sp->req_state_flags & RQSF_XFER_COMPLETE) {
4521			SNPRINTF(buf, sizeof (buf), "%s XFR_CMPLT", buf);
4522		}
4523		SNPRINTF(buf, sizeof (buf), "%s\nstatus=>", buf);
4524		if (sp->req_status_flags & RQSTF_DISCONNECT) {
4525			SNPRINTF(buf, sizeof (buf), "%s Disconnect", buf);
4526		}
4527		if (sp->req_status_flags & RQSTF_SYNCHRONOUS) {
4528			SNPRINTF(buf, sizeof (buf), "%s Sync_xfr", buf);
4529		}
4530		if (sp->req_status_flags & RQSTF_PARITY_ERROR) {
4531			SNPRINTF(buf, sizeof (buf), "%s Parity", buf);
4532		}
4533		if (sp->req_status_flags & RQSTF_BUS_RESET) {
4534			SNPRINTF(buf, sizeof (buf), "%s Bus_Reset", buf);
4535		}
4536		if (sp->req_status_flags & RQSTF_DEVICE_RESET) {
4537			SNPRINTF(buf, sizeof (buf), "%s Device_Reset", buf);
4538		}
4539		if (sp->req_status_flags & RQSTF_ABORTED) {
4540			SNPRINTF(buf, sizeof (buf), "%s Aborted", buf);
4541		}
4542		if (sp->req_status_flags & RQSTF_TIMEOUT) {
4543			SNPRINTF(buf, sizeof (buf), "%s Timeout", buf);
4544		}
4545		if (sp->req_status_flags & RQSTF_NEGOTIATION) {
4546			SNPRINTF(buf, sizeof (buf), "%s Negotiation", buf);
4547		}
4548		isp_prt(isp, ISP_LOGERR, "%s", buf);
4549		isp_prt(isp, ISP_LOGERR, "transport error for %d.%d.%d:\n%s",
4550		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), buf);
4551		break;
4552	}
4553	case RQCS_RESET_OCCURRED:
4554		isp_prt(isp, ISP_LOGWARN,
4555		    "bus reset destroyed command for %d.%d.%d",
4556		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4557		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
4558		if (XS_NOERR(xs)) {
4559			XS_SETERR(xs, HBA_BUSRESET);
4560		}
4561		return;
4562
4563	case RQCS_ABORTED:
4564		isp_prt(isp, ISP_LOGERR, "command aborted for %d.%d.%d",
4565		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4566		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
4567		if (XS_NOERR(xs)) {
4568			XS_SETERR(xs, HBA_ABORTED);
4569		}
4570		return;
4571
4572	case RQCS_TIMEOUT:
4573		isp_prt(isp, ISP_LOGWARN, "command timed out for %d.%d.%d",
4574		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4575		/*
4576	 	 * Check to see if we logged out the device.
4577		 */
4578		if (IS_FC(isp)) {
4579			if ((sp->req_completion_status & RQSTF_LOGOUT) &&
4580			    FCPARAM(isp)->portdb[XS_TGT(xs)].valid &&
4581			    FCPARAM(isp)->portdb[XS_TGT(xs)].fabric_dev) {
4582				FCPARAM(isp)->portdb[XS_TGT(xs)].relogin = 1;
4583			}
4584		}
4585		if (XS_NOERR(xs)) {
4586			XS_SETERR(xs, HBA_CMDTIMEOUT);
4587		}
4588		return;
4589
4590	case RQCS_DATA_OVERRUN:
4591		XS_RESID(xs) = sp->req_resid;
4592		isp_prt(isp, ISP_LOGERR, "data overrun for command on %d.%d.%d",
4593		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4594		if (XS_NOERR(xs)) {
4595			XS_SETERR(xs, HBA_DATAOVR);
4596		}
4597		return;
4598
4599	case RQCS_COMMAND_OVERRUN:
4600		isp_prt(isp, ISP_LOGERR,
4601		    "command overrun for command on %d.%d.%d",
4602		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4603		break;
4604
4605	case RQCS_STATUS_OVERRUN:
4606		isp_prt(isp, ISP_LOGERR,
4607		    "status overrun for command on %d.%d.%d",
4608		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4609		break;
4610
4611	case RQCS_BAD_MESSAGE:
4612		isp_prt(isp, ISP_LOGERR,
4613		    "msg not COMMAND COMPLETE after status %d.%d.%d",
4614		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4615		break;
4616
4617	case RQCS_NO_MESSAGE_OUT:
4618		isp_prt(isp, ISP_LOGERR,
4619		    "No MESSAGE OUT phase after selection on %d.%d.%d",
4620		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4621		break;
4622
4623	case RQCS_EXT_ID_FAILED:
4624		isp_prt(isp, ISP_LOGERR, "EXTENDED IDENTIFY failed %d.%d.%d",
4625		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4626		break;
4627
4628	case RQCS_IDE_MSG_FAILED:
4629		isp_prt(isp, ISP_LOGERR,
4630		    "INITIATOR DETECTED ERROR rejected by %d.%d.%d",
4631		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4632		break;
4633
4634	case RQCS_ABORT_MSG_FAILED:
4635		isp_prt(isp, ISP_LOGERR, "ABORT OPERATION rejected by %d.%d.%d",
4636		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4637		break;
4638
4639	case RQCS_REJECT_MSG_FAILED:
4640		isp_prt(isp, ISP_LOGERR, "MESSAGE REJECT rejected by %d.%d.%d",
4641		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4642		break;
4643
4644	case RQCS_NOP_MSG_FAILED:
4645		isp_prt(isp, ISP_LOGERR, "NOP rejected by %d.%d.%d",
4646		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4647		break;
4648
4649	case RQCS_PARITY_ERROR_MSG_FAILED:
4650		isp_prt(isp, ISP_LOGERR,
4651		    "MESSAGE PARITY ERROR rejected by %d.%d.%d",
4652		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4653		break;
4654
4655	case RQCS_DEVICE_RESET_MSG_FAILED:
4656		isp_prt(isp, ISP_LOGWARN,
4657		    "BUS DEVICE RESET rejected by %d.%d.%d",
4658		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4659		break;
4660
4661	case RQCS_ID_MSG_FAILED:
4662		isp_prt(isp, ISP_LOGERR, "IDENTIFY rejected by %d.%d.%d",
4663		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4664		break;
4665
4666	case RQCS_UNEXP_BUS_FREE:
4667		isp_prt(isp, ISP_LOGERR, "%d.%d.%d had an unexpected bus free",
4668		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4669		break;
4670
4671	case RQCS_DATA_UNDERRUN:
4672	{
4673		if (IS_FC(isp)) {
4674			int ru_marked = (sp->req_scsi_status & RQCS_RU) != 0;
4675			if (!ru_marked || sp->req_resid > XS_XFRLEN(xs)) {
4676				isp_prt(isp, ISP_LOGWARN, bun, XS_TGT(xs),
4677				    XS_LUN(xs), XS_XFRLEN(xs), sp->req_resid,
4678				    (ru_marked)? "marked" : "not marked");
4679				if (XS_NOERR(xs)) {
4680					XS_SETERR(xs, HBA_BOTCH);
4681				}
4682				return;
4683			}
4684		}
4685		XS_RESID(xs) = sp->req_resid;
4686		if (XS_NOERR(xs)) {
4687			XS_SETERR(xs, HBA_NOERROR);
4688		}
4689		return;
4690	}
4691
4692	case RQCS_XACT_ERR1:
4693		isp_prt(isp, ISP_LOGERR, xact1, XS_CHANNEL(xs),
4694		    XS_TGT(xs), XS_LUN(xs));
4695		break;
4696
4697	case RQCS_XACT_ERR2:
4698		isp_prt(isp, ISP_LOGERR, xact2,
4699		    XS_LUN(xs), XS_TGT(xs), XS_CHANNEL(xs));
4700		break;
4701
4702	case RQCS_XACT_ERR3:
4703		isp_prt(isp, ISP_LOGERR, xact3,
4704		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4705		break;
4706
4707	case RQCS_BAD_ENTRY:
4708		isp_prt(isp, ISP_LOGERR, "Invalid IOCB entry type detected");
4709		break;
4710
4711	case RQCS_QUEUE_FULL:
4712		isp_prt(isp, ISP_LOGDEBUG0,
4713		    "internal queues full for %d.%d.%d status 0x%x",
4714		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), *XS_STSP(xs));
4715
4716		/*
4717		 * If QFULL or some other status byte is set, then this
4718		 * isn't an error, per se.
4719		 *
4720		 * Unfortunately, some QLogic f/w writers have, in
4721		 * some cases, ommitted to *set* status to QFULL.
4722		 *
4723
4724		if (*XS_STSP(xs) != SCSI_GOOD && XS_NOERR(xs)) {
4725			XS_SETERR(xs, HBA_NOERROR);
4726			return;
4727		}
4728
4729		 *
4730		 *
4731		 */
4732
4733		*XS_STSP(xs) = SCSI_QFULL;
4734		XS_SETERR(xs, HBA_NOERROR);
4735		return;
4736
4737	case RQCS_PHASE_SKIPPED:
4738		isp_prt(isp, ISP_LOGERR, pskip, XS_CHANNEL(xs),
4739		    XS_TGT(xs), XS_LUN(xs));
4740		break;
4741
4742	case RQCS_ARQS_FAILED:
4743		isp_prt(isp, ISP_LOGERR,
4744		    "Auto Request Sense failed for %d.%d.%d",
4745		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4746		if (XS_NOERR(xs)) {
4747			XS_SETERR(xs, HBA_ARQFAIL);
4748		}
4749		return;
4750
4751	case RQCS_WIDE_FAILED:
4752		isp_prt(isp, ISP_LOGERR,
4753		    "Wide Negotiation failed for %d.%d.%d",
4754		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
4755		if (IS_SCSI(isp)) {
4756			sdparam *sdp = isp->isp_param;
4757			sdp += XS_CHANNEL(xs);
4758			sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_WIDE;
4759			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
4760			isp->isp_update |= (1 << XS_CHANNEL(xs));
4761		}
4762		if (XS_NOERR(xs)) {
4763			XS_SETERR(xs, HBA_NOERROR);
4764		}
4765		return;
4766
4767	case RQCS_SYNCXFER_FAILED:
4768		isp_prt(isp, ISP_LOGERR,
4769		    "SDTR Message failed for target %d.%d.%d",
4770		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
4771		if (IS_SCSI(isp)) {
4772			sdparam *sdp = isp->isp_param;
4773			sdp += XS_CHANNEL(xs);
4774			sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_SYNC;
4775			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
4776			isp->isp_update |= (1 << XS_CHANNEL(xs));
4777		}
4778		break;
4779
4780	case RQCS_LVD_BUSERR:
4781		isp_prt(isp, ISP_LOGERR,
4782		    "Bad LVD condition while talking to %d.%d.%d",
4783		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
4784		break;
4785
4786	case RQCS_PORT_UNAVAILABLE:
4787		/*
4788		 * No such port on the loop. Moral equivalent of SELTIMEO
4789		 */
4790	case RQCS_PORT_LOGGED_OUT:
4791		/*
4792		 * It was there (maybe)- treat as a selection timeout.
4793		 */
4794		if ((sp->req_completion_status & 0xff) == RQCS_PORT_UNAVAILABLE)
4795			isp_prt(isp, ISP_LOGINFO,
4796			    "port unavailable for target %d", XS_TGT(xs));
4797		else
4798			isp_prt(isp, ISP_LOGINFO,
4799			    "port logout for target %d", XS_TGT(xs));
4800		/*
4801		 * If we're on a local loop, force a LIP (which is overkill)
4802		 * to force a re-login of this unit. If we're on fabric,
4803		 * then we'll have to relogin as a matter of course.
4804		 */
4805		if (FCPARAM(isp)->isp_topo == TOPO_NL_PORT ||
4806		    FCPARAM(isp)->isp_topo == TOPO_FL_PORT) {
4807			mbreg_t mbs;
4808			MEMZERO(&mbs, sizeof (mbs));
4809			mbs.param[0] = MBOX_INIT_LIP;
4810			isp_mboxcmd_qnw(isp, &mbs, 1);
4811		}
4812
4813		/*
4814		 * Probably overkill.
4815		 */
4816		isp->isp_sendmarker = 1;
4817		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
4818		isp_mark_getpdb_all(isp);
4819		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
4820		if (XS_NOERR(xs)) {
4821			XS_SETERR(xs, HBA_SELTIMEOUT);
4822		}
4823		return;
4824
4825	case RQCS_PORT_CHANGED:
4826		isp_prt(isp, ISP_LOGWARN,
4827		    "port changed for target %d", XS_TGT(xs));
4828		if (XS_NOERR(xs)) {
4829			XS_SETERR(xs, HBA_SELTIMEOUT);
4830		}
4831		return;
4832
4833	case RQCS_PORT_BUSY:
4834		isp_prt(isp, ISP_LOGWARN,
4835		    "port busy for target %d", XS_TGT(xs));
4836		if (XS_NOERR(xs)) {
4837			XS_SETERR(xs, HBA_TGTBSY);
4838		}
4839		return;
4840
4841	default:
4842		isp_prt(isp, ISP_LOGERR, "Unknown Completion Status 0x%x",
4843		    sp->req_completion_status);
4844		break;
4845	}
4846	if (XS_NOERR(xs)) {
4847		XS_SETERR(xs, HBA_BOTCH);
4848	}
4849}
4850
4851static void
4852isp_fastpost_complete(ispsoftc_t *isp, uint16_t fph)
4853{
4854	XS_T *xs;
4855
4856	if (fph == 0) {
4857		return;
4858	}
4859	xs = isp_find_xs(isp, fph);
4860	if (xs == NULL) {
4861		isp_prt(isp, ISP_LOGWARN,
4862		    "Command for fast post handle 0x%x not found", fph);
4863		return;
4864	}
4865	isp_destroy_handle(isp, fph);
4866
4867	/*
4868	 * Since we don't have a result queue entry item,
4869	 * we must believe that SCSI status is zero and
4870	 * that all data transferred.
4871	 */
4872	XS_SET_STATE_STAT(isp, xs, NULL);
4873	XS_RESID(xs) = 0;
4874	*XS_STSP(xs) = SCSI_GOOD;
4875	if (XS_XFRLEN(xs)) {
4876		ISP_DMAFREE(isp, xs, fph);
4877	}
4878	if (isp->isp_nactive)
4879		isp->isp_nactive--;
4880	isp->isp_fphccmplt++;
4881	isp_done(xs);
4882}
4883
4884static int
4885isp_mbox_continue(ispsoftc_t *isp)
4886{
4887	mbreg_t mbs;
4888	uint16_t *ptr;
4889
4890	switch (isp->isp_lastmbxcmd) {
4891	case MBOX_WRITE_RAM_WORD:
4892	case MBOX_READ_RAM_WORD:
4893	case MBOX_READ_RAM_WORD_EXTENDED:
4894		break;
4895	default:
4896		return (1);
4897	}
4898	if (isp->isp_mboxtmp[0] != MBOX_COMMAND_COMPLETE) {
4899		isp->isp_mbxwrk0 = 0;
4900		return (-1);
4901	}
4902
4903	/*
4904	 * Clear the previous interrupt.
4905	 */
4906	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
4907	ISP_WRITE(isp, BIU_SEMA, 0);
4908
4909	/*
4910	 * Continue with next word.
4911	 */
4912	MEMZERO(&mbs, sizeof (mbs));
4913	ptr = isp->isp_mbxworkp;
4914	switch (isp->isp_lastmbxcmd) {
4915	case MBOX_WRITE_RAM_WORD:
4916		mbs.param[2] = *ptr++;
4917		mbs.param[1] = isp->isp_mbxwrk1++;
4918		break;
4919	case MBOX_READ_RAM_WORD:
4920	case MBOX_READ_RAM_WORD_EXTENDED:
4921		*ptr++ = isp->isp_mboxtmp[2];
4922		mbs.param[1] = isp->isp_mbxwrk1++;
4923		break;
4924	}
4925	isp->isp_mbxworkp = ptr;
4926	mbs.param[0] = isp->isp_lastmbxcmd;
4927	isp->isp_mbxwrk0 -= 1;
4928	isp_mboxcmd_qnw(isp, &mbs, 0);
4929	return (0);
4930}
4931
4932
4933#define	HIWRD(x)			((x) >> 16)
4934#define	LOWRD(x)			((x)  & 0xffff)
4935#define	ISPOPMAP(a, b)			(((a) << 16) | (b))
4936static const uint32_t mbpscsi[] = {
4937	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
4938	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
4939	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
4940	ISPOPMAP(0x1f, 0x01),	/* 0x03: MBOX_DUMP_RAM */
4941	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
4942	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
4943	ISPOPMAP(0x3f, 0x3f),	/* 0x06: MBOX_MAILBOX_REG_TEST */
4944	ISPOPMAP(0x03, 0x07),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
4945	ISPOPMAP(0x01, 0x0f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
4946	ISPOPMAP(0x00, 0x00),	/* 0x09: */
4947	ISPOPMAP(0x00, 0x00),	/* 0x0a: */
4948	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
4949	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
4950	ISPOPMAP(0x00, 0x00),	/* 0x0d: */
4951	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
4952	ISPOPMAP(0x00, 0x00),	/* 0x0f: */
4953	ISPOPMAP(0x1f, 0x1f),	/* 0x10: MBOX_INIT_REQ_QUEUE */
4954	ISPOPMAP(0x3f, 0x3f),	/* 0x11: MBOX_INIT_RES_QUEUE */
4955	ISPOPMAP(0x0f, 0x0f),	/* 0x12: MBOX_EXECUTE_IOCB */
4956	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
4957	ISPOPMAP(0x01, 0x3f),	/* 0x14: MBOX_STOP_FIRMWARE */
4958	ISPOPMAP(0x0f, 0x0f),	/* 0x15: MBOX_ABORT */
4959	ISPOPMAP(0x03, 0x03),	/* 0x16: MBOX_ABORT_DEVICE */
4960	ISPOPMAP(0x07, 0x07),	/* 0x17: MBOX_ABORT_TARGET */
4961	ISPOPMAP(0x07, 0x07),	/* 0x18: MBOX_BUS_RESET */
4962	ISPOPMAP(0x03, 0x07),	/* 0x19: MBOX_STOP_QUEUE */
4963	ISPOPMAP(0x03, 0x07),	/* 0x1a: MBOX_START_QUEUE */
4964	ISPOPMAP(0x03, 0x07),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
4965	ISPOPMAP(0x03, 0x07),	/* 0x1c: MBOX_ABORT_QUEUE */
4966	ISPOPMAP(0x03, 0x4f),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
4967	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
4968	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
4969	ISPOPMAP(0x01, 0x07),	/* 0x20: MBOX_GET_INIT_SCSI_ID */
4970	ISPOPMAP(0x01, 0x07),	/* 0x21: MBOX_GET_SELECT_TIMEOUT */
4971	ISPOPMAP(0x01, 0xc7),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
4972	ISPOPMAP(0x01, 0x07),	/* 0x23: MBOX_GET_TAG_AGE_LIMIT */
4973	ISPOPMAP(0x01, 0x03),	/* 0x24: MBOX_GET_CLOCK_RATE */
4974	ISPOPMAP(0x01, 0x07),	/* 0x25: MBOX_GET_ACT_NEG_STATE */
4975	ISPOPMAP(0x01, 0x07),	/* 0x26: MBOX_GET_ASYNC_DATA_SETUP_TIME */
4976	ISPOPMAP(0x01, 0x07),	/* 0x27: MBOX_GET_PCI_PARAMS */
4977	ISPOPMAP(0x03, 0x4f),	/* 0x28: MBOX_GET_TARGET_PARAMS */
4978	ISPOPMAP(0x03, 0x0f),	/* 0x29: MBOX_GET_DEV_QUEUE_PARAMS */
4979	ISPOPMAP(0x01, 0x07),	/* 0x2a: MBOX_GET_RESET_DELAY_PARAMS */
4980	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
4981	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
4982	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
4983	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
4984	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
4985	ISPOPMAP(0x03, 0x03),	/* 0x30: MBOX_SET_INIT_SCSI_ID */
4986	ISPOPMAP(0x07, 0x07),	/* 0x31: MBOX_SET_SELECT_TIMEOUT */
4987	ISPOPMAP(0xc7, 0xc7),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
4988	ISPOPMAP(0x07, 0x07),	/* 0x33: MBOX_SET_TAG_AGE_LIMIT */
4989	ISPOPMAP(0x03, 0x03),	/* 0x34: MBOX_SET_CLOCK_RATE */
4990	ISPOPMAP(0x07, 0x07),	/* 0x35: MBOX_SET_ACT_NEG_STATE */
4991	ISPOPMAP(0x07, 0x07),	/* 0x36: MBOX_SET_ASYNC_DATA_SETUP_TIME */
4992	ISPOPMAP(0x07, 0x07),	/* 0x37: MBOX_SET_PCI_CONTROL_PARAMS */
4993	ISPOPMAP(0x4f, 0x4f),	/* 0x38: MBOX_SET_TARGET_PARAMS */
4994	ISPOPMAP(0x0f, 0x0f),	/* 0x39: MBOX_SET_DEV_QUEUE_PARAMS */
4995	ISPOPMAP(0x07, 0x07),	/* 0x3a: MBOX_SET_RESET_DELAY_PARAMS */
4996	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
4997	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
4998	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
4999	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
5000	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
5001	ISPOPMAP(0x01, 0x03),	/* 0x40: MBOX_RETURN_BIOS_BLOCK_ADDR */
5002	ISPOPMAP(0x3f, 0x01),	/* 0x41: MBOX_WRITE_FOUR_RAM_WORDS */
5003	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_EXEC_BIOS_IOCB */
5004	ISPOPMAP(0x00, 0x00),	/* 0x43: */
5005	ISPOPMAP(0x00, 0x00),	/* 0x44: */
5006	ISPOPMAP(0x03, 0x03),	/* 0x45: SET SYSTEM PARAMETER */
5007	ISPOPMAP(0x01, 0x03),	/* 0x46: GET SYSTEM PARAMETER */
5008	ISPOPMAP(0x00, 0x00),	/* 0x47: */
5009	ISPOPMAP(0x01, 0xcf),	/* 0x48: GET SCAM CONFIGURATION */
5010	ISPOPMAP(0xcf, 0xcf),	/* 0x49: SET SCAM CONFIGURATION */
5011	ISPOPMAP(0x03, 0x03),	/* 0x4a: MBOX_SET_FIRMWARE_FEATURES */
5012	ISPOPMAP(0x01, 0x03),	/* 0x4b: MBOX_GET_FIRMWARE_FEATURES */
5013	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
5014	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
5015	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
5016	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
5017	ISPOPMAP(0xdf, 0xdf),	/* 0x50: LOAD RAM A64 */
5018	ISPOPMAP(0xdf, 0xdf),	/* 0x51: DUMP RAM A64 */
5019	ISPOPMAP(0xdf, 0xff),	/* 0x52: INITIALIZE REQUEST QUEUE A64 */
5020	ISPOPMAP(0xef, 0xff),	/* 0x53: INITIALIZE RESPONSE QUEUE A64 */
5021	ISPOPMAP(0xcf, 0x01),	/* 0x54: EXECUTE IOCB A64 */
5022	ISPOPMAP(0x07, 0x01),	/* 0x55: ENABLE TARGET MODE */
5023	ISPOPMAP(0x03, 0x0f),	/* 0x56: GET TARGET STATUS */
5024	ISPOPMAP(0x00, 0x00),	/* 0x57: */
5025	ISPOPMAP(0x00, 0x00),	/* 0x58: */
5026	ISPOPMAP(0x00, 0x00),	/* 0x59: */
5027	ISPOPMAP(0x03, 0x03),	/* 0x5a: SET DATA OVERRUN RECOVERY MODE */
5028	ISPOPMAP(0x01, 0x03),	/* 0x5b: GET DATA OVERRUN RECOVERY MODE */
5029	ISPOPMAP(0x0f, 0x0f),	/* 0x5c: SET HOST DATA */
5030	ISPOPMAP(0x01, 0x01)	/* 0x5d: GET NOST DATA */
5031};
5032
5033#ifndef	ISP_STRIPPED
5034static char *scsi_mbcmd_names[] = {
5035	"NO-OP",
5036	"LOAD RAM",
5037	"EXEC FIRMWARE",
5038	"DUMP RAM",
5039	"WRITE RAM WORD",
5040	"READ RAM WORD",
5041	"MAILBOX REG TEST",
5042	"VERIFY CHECKSUM",
5043	"ABOUT FIRMWARE",
5044	NULL,
5045	NULL,
5046	NULL,
5047	NULL,
5048	NULL,
5049	"CHECK FIRMWARE",
5050	NULL,
5051	"INIT REQUEST QUEUE",
5052	"INIT RESULT QUEUE",
5053	"EXECUTE IOCB",
5054	"WAKE UP",
5055	"STOP FIRMWARE",
5056	"ABORT",
5057	"ABORT DEVICE",
5058	"ABORT TARGET",
5059	"BUS RESET",
5060	"STOP QUEUE",
5061	"START QUEUE",
5062	"SINGLE STEP QUEUE",
5063	"ABORT QUEUE",
5064	"GET DEV QUEUE STATUS",
5065	NULL,
5066	"GET FIRMWARE STATUS",
5067	"GET INIT SCSI ID",
5068	"GET SELECT TIMEOUT",
5069	"GET RETRY COUNT",
5070	"GET TAG AGE LIMIT",
5071	"GET CLOCK RATE",
5072	"GET ACT NEG STATE",
5073	"GET ASYNC DATA SETUP TIME",
5074	"GET PCI PARAMS",
5075	"GET TARGET PARAMS",
5076	"GET DEV QUEUE PARAMS",
5077	"GET RESET DELAY PARAMS",
5078	NULL,
5079	NULL,
5080	NULL,
5081	NULL,
5082	NULL,
5083	"SET INIT SCSI ID",
5084	"SET SELECT TIMEOUT",
5085	"SET RETRY COUNT",
5086	"SET TAG AGE LIMIT",
5087	"SET CLOCK RATE",
5088	"SET ACT NEG STATE",
5089	"SET ASYNC DATA SETUP TIME",
5090	"SET PCI CONTROL PARAMS",
5091	"SET TARGET PARAMS",
5092	"SET DEV QUEUE PARAMS",
5093	"SET RESET DELAY PARAMS",
5094	NULL,
5095	NULL,
5096	NULL,
5097	NULL,
5098	NULL,
5099	"RETURN BIOS BLOCK ADDR",
5100	"WRITE FOUR RAM WORDS",
5101	"EXEC BIOS IOCB",
5102	NULL,
5103	NULL,
5104	"SET SYSTEM PARAMETER",
5105	"GET SYSTEM PARAMETER",
5106	NULL,
5107	"GET SCAM CONFIGURATION",
5108	"SET SCAM CONFIGURATION",
5109	"SET FIRMWARE FEATURES",
5110	"GET FIRMWARE FEATURES",
5111	NULL,
5112	NULL,
5113	NULL,
5114	NULL,
5115	"LOAD RAM A64",
5116	"DUMP RAM A64",
5117	"INITIALIZE REQUEST QUEUE A64",
5118	"INITIALIZE RESPONSE QUEUE A64",
5119	"EXECUTE IOCB A64",
5120	"ENABLE TARGET MODE",
5121	"GET TARGET MODE STATE",
5122	NULL,
5123	NULL,
5124	NULL,
5125	"SET DATA OVERRUN RECOVERY MODE",
5126	"GET DATA OVERRUN RECOVERY MODE",
5127	"SET HOST DATA",
5128	"GET NOST DATA",
5129};
5130#endif
5131
5132static const uint32_t mbpfc[] = {
5133	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
5134	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
5135	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
5136	ISPOPMAP(0xdf, 0x01),	/* 0x03: MBOX_DUMP_RAM */
5137	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
5138	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
5139	ISPOPMAP(0xff, 0xff),	/* 0x06: MBOX_MAILBOX_REG_TEST */
5140	ISPOPMAP(0x03, 0x05),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
5141	ISPOPMAP(0x01, 0x4f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
5142	ISPOPMAP(0xdf, 0x01),	/* 0x09: LOAD RAM */
5143	ISPOPMAP(0xdf, 0x01),	/* 0x0a: DUMP RAM */
5144	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
5145	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
5146	ISPOPMAP(0x00, 0x00),	/* 0x0d: */
5147	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
5148	ISPOPMAP(0x03, 0x07),	/* 0x0f: MBOX_READ_RAM_WORD_EXTENDED(1) */
5149	ISPOPMAP(0x1f, 0x11),	/* 0x10: MBOX_INIT_REQ_QUEUE */
5150	ISPOPMAP(0x2f, 0x21),	/* 0x11: MBOX_INIT_RES_QUEUE */
5151	ISPOPMAP(0x0f, 0x01),	/* 0x12: MBOX_EXECUTE_IOCB */
5152	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
5153	ISPOPMAP(0x01, 0xff),	/* 0x14: MBOX_STOP_FIRMWARE */
5154	ISPOPMAP(0x4f, 0x01),	/* 0x15: MBOX_ABORT */
5155	ISPOPMAP(0x07, 0x01),	/* 0x16: MBOX_ABORT_DEVICE */
5156	ISPOPMAP(0x07, 0x01),	/* 0x17: MBOX_ABORT_TARGET */
5157	ISPOPMAP(0x03, 0x03),	/* 0x18: MBOX_BUS_RESET */
5158	ISPOPMAP(0x07, 0x05),	/* 0x19: MBOX_STOP_QUEUE */
5159	ISPOPMAP(0x07, 0x05),	/* 0x1a: MBOX_START_QUEUE */
5160	ISPOPMAP(0x07, 0x05),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
5161	ISPOPMAP(0x07, 0x05),	/* 0x1c: MBOX_ABORT_QUEUE */
5162	ISPOPMAP(0x07, 0x03),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
5163	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
5164	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
5165	ISPOPMAP(0x01, 0x4f),	/* 0x20: MBOX_GET_LOOP_ID */
5166	ISPOPMAP(0x00, 0x00),	/* 0x21: */
5167	ISPOPMAP(0x01, 0x07),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
5168	ISPOPMAP(0x00, 0x00),	/* 0x23: */
5169	ISPOPMAP(0x00, 0x00),	/* 0x24: */
5170	ISPOPMAP(0x00, 0x00),	/* 0x25: */
5171	ISPOPMAP(0x00, 0x00),	/* 0x26: */
5172	ISPOPMAP(0x00, 0x00),	/* 0x27: */
5173	ISPOPMAP(0x01, 0x03),	/* 0x28: MBOX_GET_FIRMWARE_OPTIONS */
5174	ISPOPMAP(0x03, 0x07),	/* 0x29: MBOX_GET_PORT_QUEUE_PARAMS */
5175	ISPOPMAP(0x00, 0x00),	/* 0x2a: */
5176	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
5177	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
5178	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
5179	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
5180	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
5181	ISPOPMAP(0x00, 0x00),	/* 0x30: */
5182	ISPOPMAP(0x00, 0x00),	/* 0x31: */
5183	ISPOPMAP(0x07, 0x07),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
5184	ISPOPMAP(0x00, 0x00),	/* 0x33: */
5185	ISPOPMAP(0x00, 0x00),	/* 0x34: */
5186	ISPOPMAP(0x00, 0x00),	/* 0x35: */
5187	ISPOPMAP(0x00, 0x00),	/* 0x36: */
5188	ISPOPMAP(0x00, 0x00),	/* 0x37: */
5189	ISPOPMAP(0x0f, 0x01),	/* 0x38: MBOX_SET_FIRMWARE_OPTIONS */
5190	ISPOPMAP(0x0f, 0x07),	/* 0x39: MBOX_SET_PORT_QUEUE_PARAMS */
5191	ISPOPMAP(0x00, 0x00),	/* 0x3a: */
5192	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
5193	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
5194	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
5195	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
5196	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
5197	ISPOPMAP(0x03, 0x01),	/* 0x40: MBOX_LOOP_PORT_BYPASS */
5198	ISPOPMAP(0x03, 0x01),	/* 0x41: MBOX_LOOP_PORT_ENABLE */
5199	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_GET_RESOURCE_COUNTS */
5200	ISPOPMAP(0x01, 0x01),	/* 0x43: MBOX_REQUEST_NON_PARTICIPATING_MODE */
5201	ISPOPMAP(0x00, 0x00),	/* 0x44: */
5202	ISPOPMAP(0x00, 0x00),	/* 0x45: */
5203	ISPOPMAP(0x00, 0x00),	/* 0x46: */
5204	ISPOPMAP(0xcf, 0x03),	/* 0x47: GET PORT_DATABASE ENHANCED */
5205	ISPOPMAP(0x00, 0x00),	/* 0x48: */
5206	ISPOPMAP(0x00, 0x00),	/* 0x49: */
5207	ISPOPMAP(0x00, 0x00),	/* 0x4a: */
5208	ISPOPMAP(0x00, 0x00),	/* 0x4b: */
5209	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
5210	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
5211	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
5212	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
5213	ISPOPMAP(0x00, 0x00),	/* 0x50: */
5214	ISPOPMAP(0x00, 0x00),	/* 0x51: */
5215	ISPOPMAP(0x00, 0x00),	/* 0x52: */
5216	ISPOPMAP(0x00, 0x00),	/* 0x53: */
5217	ISPOPMAP(0xcf, 0x01),	/* 0x54: EXECUTE IOCB A64 */
5218	ISPOPMAP(0x00, 0x00),	/* 0x55: */
5219	ISPOPMAP(0x00, 0x00),	/* 0x56: */
5220	ISPOPMAP(0x00, 0x00),	/* 0x57: */
5221	ISPOPMAP(0x00, 0x00),	/* 0x58: */
5222	ISPOPMAP(0x00, 0x00),	/* 0x59: */
5223	ISPOPMAP(0x00, 0x00),	/* 0x5a: */
5224	ISPOPMAP(0x03, 0x01),	/* 0x5b: MBOX_DRIVER_HEARTBEAT */
5225	ISPOPMAP(0xcf, 0x01),	/* 0x5c: MBOX_FW_HEARTBEAT */
5226	ISPOPMAP(0x07, 0x03),	/* 0x5d: MBOX_GET_SET_DATA_RATE */
5227	ISPOPMAP(0x00, 0x00),	/* 0x5e: */
5228	ISPOPMAP(0x00, 0x00),	/* 0x5f: */
5229	ISPOPMAP(0xfd, 0x31),	/* 0x60: MBOX_INIT_FIRMWARE */
5230	ISPOPMAP(0x00, 0x00),	/* 0x61: */
5231	ISPOPMAP(0x01, 0x01),	/* 0x62: MBOX_INIT_LIP */
5232	ISPOPMAP(0xcd, 0x03),	/* 0x63: MBOX_GET_FC_AL_POSITION_MAP */
5233	ISPOPMAP(0xcf, 0x01),	/* 0x64: MBOX_GET_PORT_DB */
5234	ISPOPMAP(0x07, 0x01),	/* 0x65: MBOX_CLEAR_ACA */
5235	ISPOPMAP(0x07, 0x01),	/* 0x66: MBOX_TARGET_RESET */
5236	ISPOPMAP(0x07, 0x01),	/* 0x67: MBOX_CLEAR_TASK_SET */
5237	ISPOPMAP(0x07, 0x01),	/* 0x68: MBOX_ABORT_TASK_SET */
5238	ISPOPMAP(0x01, 0x07),	/* 0x69: MBOX_GET_FW_STATE */
5239	ISPOPMAP(0x03, 0xcf),	/* 0x6a: MBOX_GET_PORT_NAME */
5240	ISPOPMAP(0xcf, 0x01),	/* 0x6b: MBOX_GET_LINK_STATUS */
5241	ISPOPMAP(0x0f, 0x01),	/* 0x6c: MBOX_INIT_LIP_RESET */
5242	ISPOPMAP(0x00, 0x00),	/* 0x6d: */
5243	ISPOPMAP(0xcf, 0x03),	/* 0x6e: MBOX_SEND_SNS */
5244	ISPOPMAP(0x0f, 0x07),	/* 0x6f: MBOX_FABRIC_LOGIN */
5245	ISPOPMAP(0x03, 0x01),	/* 0x70: MBOX_SEND_CHANGE_REQUEST */
5246	ISPOPMAP(0x03, 0x03),	/* 0x71: MBOX_FABRIC_LOGOUT */
5247	ISPOPMAP(0x0f, 0x0f),	/* 0x72: MBOX_INIT_LIP_LOGIN */
5248	ISPOPMAP(0x00, 0x00),	/* 0x73: */
5249	ISPOPMAP(0x07, 0x01),	/* 0x74: LOGIN LOOP PORT */
5250	ISPOPMAP(0xcf, 0x03),	/* 0x75: GET PORT/NODE NAME LIST */
5251	ISPOPMAP(0x4f, 0x01),	/* 0x76: SET VENDOR ID */
5252	ISPOPMAP(0xcd, 0x01),	/* 0x77: INITIALIZE IP MAILBOX */
5253	ISPOPMAP(0x00, 0x00),	/* 0x78: */
5254	ISPOPMAP(0x00, 0x00),	/* 0x79: */
5255	ISPOPMAP(0x00, 0x00),	/* 0x7a: */
5256	ISPOPMAP(0x00, 0x00),	/* 0x7b: */
5257	ISPOPMAP(0x4f, 0x03),	/* 0x7c: Get ID List */
5258	ISPOPMAP(0xcf, 0x01),	/* 0x7d: SEND LFA */
5259	ISPOPMAP(0x0f, 0x01)	/* 0x7e: LUN RESET */
5260};
5261/*
5262 * Footnotes
5263 *
5264 * (1): this sets bits 21..16 in mailbox register #8, which we nominally
5265 *	do not access at this time in the core driver. The caller is
5266 *	responsible for setting this register first (Gross!).
5267 */
5268
5269#ifndef	ISP_STRIPPED
5270static char *fc_mbcmd_names[] = {
5271	"NO-OP",
5272	"LOAD RAM",
5273	"EXEC FIRMWARE",
5274	"DUMP RAM",
5275	"WRITE RAM WORD",
5276	"READ RAM WORD",
5277	"MAILBOX REG TEST",
5278	"VERIFY CHECKSUM",
5279	"ABOUT FIRMWARE",
5280	"LOAD RAM",
5281	"DUMP RAM",
5282	NULL,
5283	NULL,
5284	"READ RAM WORD EXTENDED",
5285	"CHECK FIRMWARE",
5286	NULL,
5287	"INIT REQUEST QUEUE",
5288	"INIT RESULT QUEUE",
5289	"EXECUTE IOCB",
5290	"WAKE UP",
5291	"STOP FIRMWARE",
5292	"ABORT",
5293	"ABORT DEVICE",
5294	"ABORT TARGET",
5295	"BUS RESET",
5296	"STOP QUEUE",
5297	"START QUEUE",
5298	"SINGLE STEP QUEUE",
5299	"ABORT QUEUE",
5300	"GET DEV QUEUE STATUS",
5301	NULL,
5302	"GET FIRMWARE STATUS",
5303	"GET LOOP ID",
5304	NULL,
5305	"GET RETRY COUNT",
5306	NULL,
5307	NULL,
5308	NULL,
5309	NULL,
5310	NULL,
5311	"GET FIRMWARE OPTIONS",
5312	"GET PORT QUEUE PARAMS",
5313	NULL,
5314	NULL,
5315	NULL,
5316	NULL,
5317	NULL,
5318	NULL,
5319	NULL,
5320	NULL,
5321	"SET RETRY COUNT",
5322	NULL,
5323	NULL,
5324	NULL,
5325	NULL,
5326	NULL,
5327	"SET FIRMWARE OPTIONS",
5328	"SET PORT QUEUE PARAMS",
5329	NULL,
5330	NULL,
5331	NULL,
5332	NULL,
5333	NULL,
5334	NULL,
5335	"LOOP PORT BYPASS",
5336	"LOOP PORT ENABLE",
5337	"GET RESOURCE COUNTS",
5338	"REQUEST NON PARTICIPATING MODE",
5339	NULL,
5340	NULL,
5341	NULL,
5342	"GET PORT DATABASE,, ENHANCED",
5343	NULL,
5344	NULL,
5345	NULL,
5346	NULL,
5347	NULL,
5348	NULL,
5349	NULL,
5350	NULL,
5351	NULL,
5352	NULL,
5353	NULL,
5354	NULL,
5355	"EXECUTE IOCB A64",
5356	NULL,
5357	NULL,
5358	NULL,
5359	NULL,
5360	NULL,
5361	NULL,
5362	"DRIVER HEARTBEAT",
5363	NULL,
5364	"GET/SET DATA RATE",
5365	NULL,
5366	NULL,
5367	"INIT FIRMWARE",
5368	NULL,
5369	"INIT LIP",
5370	"GET FC-AL POSITION MAP",
5371	"GET PORT DATABASE",
5372	"CLEAR ACA",
5373	"TARGET RESET",
5374	"CLEAR TASK SET",
5375	"ABORT TASK SET",
5376	"GET FW STATE",
5377	"GET PORT NAME",
5378	"GET LINK STATUS",
5379	"INIT LIP RESET",
5380	NULL,
5381	"SEND SNS",
5382	"FABRIC LOGIN",
5383	"SEND CHANGE REQUEST",
5384	"FABRIC LOGOUT",
5385	"INIT LIP LOGIN",
5386	NULL,
5387	"LOGIN LOOP PORT",
5388	"GET PORT/NODE NAME LIST",
5389	"SET VENDOR ID",
5390	"INITIALIZE IP MAILBOX",
5391	NULL,
5392	NULL,
5393	NULL,
5394	NULL,
5395	"Get ID List",
5396	"SEND LFA",
5397	"Lun RESET"
5398};
5399#endif
5400
5401static void
5402isp_mboxcmd_qnw(ispsoftc_t *isp, mbreg_t *mbp, int nodelay)
5403{
5404	unsigned int ibits, obits, box, opcode;
5405	const uint32_t *mcp;
5406
5407	if (IS_FC(isp)) {
5408		mcp = mbpfc;
5409	} else {
5410		mcp = mbpscsi;
5411	}
5412	opcode = mbp->param[0];
5413	ibits = HIWRD(mcp[opcode]) & NMBOX_BMASK(isp);
5414	obits = LOWRD(mcp[opcode]) & NMBOX_BMASK(isp);
5415	ibits |= mbp->ibits;
5416	obits |= mbp->obits;
5417	for (box = 0; box < MAX_MAILBOX(isp); box++) {
5418		if (ibits & (1 << box)) {
5419			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
5420		}
5421		if (nodelay == 0) {
5422			isp->isp_mboxtmp[box] = mbp->param[box] = 0;
5423		}
5424	}
5425	if (nodelay == 0) {
5426		isp->isp_lastmbxcmd = opcode;
5427		isp->isp_obits = obits;
5428		isp->isp_mboxbsy = 1;
5429	}
5430	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
5431	/*
5432	 * Oddly enough, if we're not delaying for an answer,
5433	 * delay a bit to give the f/w a chance to pick up the
5434	 * command.
5435	 */
5436	if (nodelay) {
5437		USEC_DELAY(1000);
5438	}
5439}
5440
5441static void
5442isp_mboxcmd(ispsoftc_t *isp, mbreg_t *mbp, int logmask)
5443{
5444	char *cname, *xname, tname[16], mname[16];
5445	unsigned int lim, ibits, obits, box, opcode;
5446	const uint32_t *mcp;
5447
5448	if (IS_FC(isp)) {
5449		mcp = mbpfc;
5450		lim = (sizeof (mbpfc) / sizeof (mbpfc[0]));
5451	} else {
5452		mcp = mbpscsi;
5453		lim = (sizeof (mbpscsi) / sizeof (mbpscsi[0]));
5454	}
5455
5456	if ((opcode = mbp->param[0]) >= lim) {
5457		mbp->param[0] = MBOX_INVALID_COMMAND;
5458		isp_prt(isp, ISP_LOGERR, "Unknown Command 0x%x", opcode);
5459		return;
5460	}
5461
5462	ibits = HIWRD(mcp[opcode]) & NMBOX_BMASK(isp);
5463	obits = LOWRD(mcp[opcode]) & NMBOX_BMASK(isp);
5464
5465	ibits |= mbp->ibits;
5466	obits |= mbp->obits;
5467
5468	if (ibits == 0 && obits == 0) {
5469		mbp->param[0] = MBOX_COMMAND_PARAM_ERROR;
5470		isp_prt(isp, ISP_LOGERR, "no parameters for 0x%x", opcode);
5471		return;
5472	}
5473
5474	/*
5475	 * Get exclusive usage of mailbox registers.
5476	 */
5477	MBOX_ACQUIRE(isp);
5478
5479	for (box = 0; box < MAX_MAILBOX(isp); box++) {
5480		if (ibits & (1 << box)) {
5481			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
5482		}
5483		isp->isp_mboxtmp[box] = mbp->param[box] = 0;
5484	}
5485
5486	isp->isp_lastmbxcmd = opcode;
5487
5488	/*
5489	 * We assume that we can't overwrite a previous command.
5490	 */
5491	isp->isp_obits = obits;
5492	isp->isp_mboxbsy = 1;
5493
5494	/*
5495	 * Set Host Interrupt condition so that RISC will pick up mailbox regs.
5496	 */
5497	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
5498
5499	/*
5500	 * While we haven't finished the command, spin our wheels here.
5501	 */
5502	MBOX_WAIT_COMPLETE(isp);
5503
5504	if (isp->isp_mboxbsy) {
5505		/*
5506		 * Command timed out.
5507		 */
5508		isp->isp_mboxbsy = 0;
5509		MBOX_RELEASE(isp);
5510		return;
5511	}
5512
5513	/*
5514	 * Copy back output registers.
5515	 */
5516	for (box = 0; box < MAX_MAILBOX(isp); box++) {
5517		if (obits & (1 << box)) {
5518			mbp->param[box] = isp->isp_mboxtmp[box];
5519		}
5520	}
5521
5522	MBOX_RELEASE(isp);
5523
5524	if (logmask == 0 || opcode == MBOX_EXEC_FIRMWARE) {
5525		return;
5526	}
5527#ifdef	ISP_STRIPPED
5528	cname = NULL;
5529#else
5530	cname = (IS_FC(isp))? fc_mbcmd_names[opcode] : scsi_mbcmd_names[opcode];
5531#endif
5532	if (cname == NULL) {
5533		cname = tname;
5534		SNPRINTF(tname, sizeof tname, "opcode %x", opcode);
5535	}
5536
5537	/*
5538	 * Just to be chatty here...
5539	 */
5540	xname = NULL;
5541	switch (mbp->param[0]) {
5542	case MBOX_COMMAND_COMPLETE:
5543		break;
5544	case MBOX_INVALID_COMMAND:
5545		if (logmask & MBLOGMASK(MBOX_COMMAND_COMPLETE))
5546			xname = "INVALID COMMAND";
5547		break;
5548	case MBOX_HOST_INTERFACE_ERROR:
5549		if (logmask & MBLOGMASK(MBOX_HOST_INTERFACE_ERROR))
5550			xname = "HOST INTERFACE ERROR";
5551		break;
5552	case MBOX_TEST_FAILED:
5553		if (logmask & MBLOGMASK(MBOX_TEST_FAILED))
5554			xname = "TEST FAILED";
5555		break;
5556	case MBOX_COMMAND_ERROR:
5557		if (logmask & MBLOGMASK(MBOX_COMMAND_ERROR))
5558			xname = "COMMAND ERROR";
5559		break;
5560	case MBOX_COMMAND_PARAM_ERROR:
5561		if (logmask & MBLOGMASK(MBOX_COMMAND_PARAM_ERROR))
5562			xname = "COMMAND PARAMETER ERROR";
5563		break;
5564	case MBOX_LOOP_ID_USED:
5565		if (logmask & MBLOGMASK(MBOX_LOOP_ID_USED))
5566			xname = "LOOP ID ALREADY IN USE";
5567		break;
5568	case MBOX_PORT_ID_USED:
5569		if (logmask & MBLOGMASK(MBOX_PORT_ID_USED))
5570			xname = "PORT ID ALREADY IN USE";
5571		break;
5572	case MBOX_ALL_IDS_USED:
5573		if (logmask & MBLOGMASK(MBOX_ALL_IDS_USED))
5574			xname = "ALL LOOP IDS IN USE";
5575		break;
5576	case 0:		/* special case */
5577		xname = "TIMEOUT";
5578		break;
5579	default:
5580		SNPRINTF(mname, sizeof mname, "error 0x%x", mbp->param[0]);
5581		xname = mname;
5582		break;
5583	}
5584	if (xname)
5585		isp_prt(isp, ISP_LOGALL, "Mailbox Command '%s' failed (%s)",
5586		    cname, xname);
5587}
5588
5589static void
5590isp_fw_state(ispsoftc_t *isp)
5591{
5592	if (IS_FC(isp)) {
5593		mbreg_t mbs;
5594		fcparam *fcp = isp->isp_param;
5595
5596		MEMZERO(&mbs, sizeof (mbs));
5597		mbs.param[0] = MBOX_GET_FW_STATE;
5598		isp_mboxcmd(isp, &mbs, MBLOGALL);
5599		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
5600			fcp->isp_fwstate = mbs.param[1];
5601		}
5602	}
5603}
5604
5605static void
5606isp_update(ispsoftc_t *isp)
5607{
5608	int bus, upmask;
5609
5610	for (bus = 0, upmask = isp->isp_update; upmask != 0; bus++) {
5611		if (upmask & (1 << bus)) {
5612			isp_update_bus(isp, bus);
5613		}
5614		upmask &= ~(1 << bus);
5615	}
5616}
5617
5618static void
5619isp_update_bus(ispsoftc_t *isp, int bus)
5620{
5621	int tgt;
5622	mbreg_t mbs;
5623	sdparam *sdp;
5624
5625	isp->isp_update &= ~(1 << bus);
5626	if (IS_FC(isp)) {
5627		/*
5628		 * There are no 'per-bus' settings for Fibre Channel.
5629		 */
5630		return;
5631	}
5632	sdp = isp->isp_param;
5633	sdp += bus;
5634	MEMZERO(&mbs, sizeof (mbs));
5635
5636	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
5637		uint16_t flags, period, offset;
5638		int get;
5639
5640		if (sdp->isp_devparam[tgt].dev_enable == 0) {
5641			sdp->isp_devparam[tgt].dev_update = 0;
5642			sdp->isp_devparam[tgt].dev_refresh = 0;
5643			isp_prt(isp, ISP_LOGDEBUG0,
5644	 		    "skipping target %d bus %d update", tgt, bus);
5645			continue;
5646		}
5647		/*
5648		 * If the goal is to update the status of the device,
5649		 * take what's in goal_flags and try and set the device
5650		 * toward that. Otherwise, if we're just refreshing the
5651		 * current device state, get the current parameters.
5652		 */
5653
5654		/*
5655		 * Refresh overrides set
5656		 */
5657		if (sdp->isp_devparam[tgt].dev_refresh) {
5658			mbs.param[0] = MBOX_GET_TARGET_PARAMS;
5659			sdp->isp_devparam[tgt].dev_refresh = 0;
5660			get = 1;
5661		} else if (sdp->isp_devparam[tgt].dev_update) {
5662			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
5663			/*
5664			 * Make sure goal_flags has "Renegotiate on Error"
5665			 * on and "Freeze Queue on Error" off.
5666			 */
5667			sdp->isp_devparam[tgt].goal_flags |= DPARM_RENEG;
5668			sdp->isp_devparam[tgt].goal_flags &= ~DPARM_QFRZ;
5669
5670			mbs.param[2] = sdp->isp_devparam[tgt].goal_flags;
5671
5672			/*
5673			 * Insist that PARITY must be enabled
5674			 * if SYNC or WIDE is enabled.
5675			 */
5676			if ((mbs.param[2] & (DPARM_SYNC|DPARM_WIDE)) != 0) {
5677				mbs.param[2] |= DPARM_PARITY;
5678			}
5679
5680			if ((mbs.param[2] & DPARM_SYNC) == 0) {
5681				mbs.param[3] = 0;
5682			} else {
5683				mbs.param[3] =
5684				    (sdp->isp_devparam[tgt].goal_offset << 8) |
5685				    (sdp->isp_devparam[tgt].goal_period);
5686			}
5687			/*
5688			 * A command completion later that has
5689			 * RQSTF_NEGOTIATION set can cause
5690			 * the dev_refresh/announce cycle also.
5691			 *
5692			 * Note: It is really important to update our current
5693			 * flags with at least the state of TAG capabilities-
5694			 * otherwise we might try and send a tagged command
5695			 * when we have it all turned off. So change it here
5696			 * to say that current already matches goal.
5697			 */
5698			sdp->isp_devparam[tgt].actv_flags &= ~DPARM_TQING;
5699			sdp->isp_devparam[tgt].actv_flags |=
5700			    (sdp->isp_devparam[tgt].goal_flags & DPARM_TQING);
5701			isp_prt(isp, ISP_LOGDEBUG0,
5702			    "bus %d set tgt %d flags 0x%x off 0x%x period 0x%x",
5703			    bus, tgt, mbs.param[2], mbs.param[3] >> 8,
5704			    mbs.param[3] & 0xff);
5705			sdp->isp_devparam[tgt].dev_update = 0;
5706			sdp->isp_devparam[tgt].dev_refresh = 1;
5707			get = 0;
5708		} else {
5709			continue;
5710		}
5711		mbs.param[1] = (bus << 15) | (tgt << 8);
5712		isp_mboxcmd(isp, &mbs, MBLOGALL);
5713		if (get == 0) {
5714			isp->isp_sendmarker |= (1 << bus);
5715			continue;
5716		}
5717		flags = mbs.param[2];
5718		period = mbs.param[3] & 0xff;
5719		offset = mbs.param[3] >> 8;
5720		sdp->isp_devparam[tgt].actv_flags = flags;
5721		sdp->isp_devparam[tgt].actv_period = period;
5722		sdp->isp_devparam[tgt].actv_offset = offset;
5723		get = (bus << 16) | tgt;
5724		(void) isp_async(isp, ISPASYNC_NEW_TGT_PARAMS, &get);
5725	}
5726
5727	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
5728		if (sdp->isp_devparam[tgt].dev_update ||
5729		    sdp->isp_devparam[tgt].dev_refresh) {
5730			isp->isp_update |= (1 << bus);
5731			break;
5732		}
5733	}
5734}
5735
5736#ifndef	DEFAULT_FRAMESIZE
5737#define	DEFAULT_FRAMESIZE(isp)		ICB_DFLT_FRMLEN
5738#endif
5739#ifndef	DEFAULT_EXEC_THROTTLE
5740#define	DEFAULT_EXEC_THROTTLE(isp)	ISP_EXEC_THROTTLE
5741#endif
5742
5743static void
5744isp_setdfltparm(ispsoftc_t *isp, int channel)
5745{
5746	int tgt;
5747	mbreg_t mbs;
5748	sdparam *sdp;
5749
5750	MEMZERO(&mbs, sizeof (mbs));
5751	if (IS_FC(isp)) {
5752		fcparam *fcp = (fcparam *) isp->isp_param;
5753		int nvfail;
5754
5755		fcp += channel;
5756		if (fcp->isp_gotdparms) {
5757			return;
5758		}
5759		fcp->isp_gotdparms = 1;
5760		fcp->isp_maxfrmlen = DEFAULT_FRAMESIZE(isp);
5761		fcp->isp_maxalloc = ICB_DFLT_ALLOC;
5762		fcp->isp_execthrottle = DEFAULT_EXEC_THROTTLE(isp);
5763		fcp->isp_retry_delay = ICB_DFLT_RDELAY;
5764		fcp->isp_retry_count = ICB_DFLT_RCOUNT;
5765		/* Platform specific.... */
5766		fcp->isp_loopid = DEFAULT_LOOPID(isp);
5767		fcp->isp_nodewwn = DEFAULT_NODEWWN(isp);
5768		fcp->isp_portwwn = DEFAULT_PORTWWN(isp);
5769		fcp->isp_fwoptions = 0;
5770		fcp->isp_fwoptions |= ICBOPT_FAIRNESS;
5771		fcp->isp_fwoptions |= ICBOPT_PDBCHANGE_AE;
5772		fcp->isp_fwoptions |= ICBOPT_HARD_ADDRESS;
5773#ifndef	ISP_NO_FASTPOST_FC
5774		fcp->isp_fwoptions |= ICBOPT_FAST_POST;
5775#endif
5776		if (isp->isp_confopts & ISP_CFG_FULL_DUPLEX)
5777			fcp->isp_fwoptions |= ICBOPT_FULL_DUPLEX;
5778
5779		/*
5780		 * Make sure this is turned off now until we get
5781		 * extended options from NVRAM
5782		 */
5783		fcp->isp_fwoptions &= ~ICBOPT_EXTENDED;
5784
5785		/*
5786		 * Now try and read NVRAM unless told to not do so.
5787		 * This will set fcparam's isp_nodewwn && isp_portwwn.
5788		 */
5789		if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
5790		    	nvfail = isp_read_nvram(isp);
5791			if (nvfail)
5792				isp->isp_confopts |= ISP_CFG_NONVRAM;
5793		} else {
5794			nvfail = 1;
5795		}
5796		/*
5797		 * Set node && port to override platform set defaults
5798		 * unless the nvram read failed (or none was done),
5799		 * or the platform code wants to use what had been
5800		 * set in the defaults.
5801		 */
5802		if (nvfail) {
5803			isp->isp_confopts |= ISP_CFG_OWNWWPN|ISP_CFG_OWNWWNN;
5804		}
5805		if (isp->isp_confopts & ISP_CFG_OWNWWNN) {
5806			isp_prt(isp, ISP_LOGCONFIG, "Using Node WWN 0x%08x%08x",
5807			    (uint32_t) (DEFAULT_NODEWWN(isp) >> 32),
5808			    (uint32_t) (DEFAULT_NODEWWN(isp) & 0xffffffff));
5809			ISP_NODEWWN(isp) = DEFAULT_NODEWWN(isp);
5810		} else {
5811			/*
5812			 * We always start out with values derived
5813			 * from NVRAM or our platform default.
5814			 */
5815			ISP_NODEWWN(isp) = fcp->isp_nodewwn;
5816		}
5817		if (isp->isp_confopts & ISP_CFG_OWNWWPN) {
5818			isp_prt(isp, ISP_LOGCONFIG, "Using Port WWN 0x%08x%08x",
5819			    (uint32_t) (DEFAULT_PORTWWN(isp) >> 32),
5820			    (uint32_t) (DEFAULT_PORTWWN(isp) & 0xffffffff));
5821			ISP_PORTWWN(isp) = DEFAULT_PORTWWN(isp);
5822		} else {
5823			/*
5824			 * We always start out with values derived
5825			 * from NVRAM or our platform default.
5826			 */
5827			ISP_PORTWWN(isp) = fcp->isp_portwwn;
5828		}
5829		return;
5830	}
5831
5832	sdp = (sdparam *) isp->isp_param;
5833	sdp += channel;
5834
5835	/*
5836	 * Been there, done that, got the T-shirt...
5837	 */
5838	if (sdp->isp_gotdparms) {
5839		return;
5840	}
5841	sdp->isp_gotdparms = 1;
5842
5843	/*
5844	 * Establish some default parameters.
5845	 */
5846	sdp->isp_cmd_dma_burst_enable = 0;
5847	sdp->isp_data_dma_burst_enabl = 1;
5848	sdp->isp_fifo_threshold = 0;
5849	sdp->isp_initiator_id = DEFAULT_IID(isp);
5850	if (isp->isp_type >= ISP_HA_SCSI_1040) {
5851		sdp->isp_async_data_setup = 9;
5852	} else {
5853		sdp->isp_async_data_setup = 6;
5854	}
5855	sdp->isp_selection_timeout = 250;
5856	sdp->isp_max_queue_depth = MAXISPREQUEST(isp);
5857	sdp->isp_tag_aging = 8;
5858	sdp->isp_bus_reset_delay = 5;
5859	/*
5860	 * Don't retry selection, busy or queue full automatically- reflect
5861	 * these back to us.
5862	 */
5863	sdp->isp_retry_count = 0;
5864	sdp->isp_retry_delay = 0;
5865
5866	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
5867		sdp->isp_devparam[tgt].exc_throttle = ISP_EXEC_THROTTLE;
5868		sdp->isp_devparam[tgt].dev_enable = 1;
5869	}
5870
5871	/*
5872	 * If we've not been told to avoid reading NVRAM, try and read it.
5873	 * If we're successful reading it, we can then return because NVRAM
5874	 * will tell us what the desired settings are. Otherwise, we establish
5875	 * some reasonable 'fake' nvram and goal defaults.
5876	 */
5877
5878	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
5879		if (isp_read_nvram(isp) == 0) {
5880			return;
5881		}
5882	}
5883
5884	/*
5885	 * Now try and see whether we have specific values for them.
5886	 */
5887	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
5888		mbs.param[0] = MBOX_GET_ACT_NEG_STATE;
5889		isp_mboxcmd(isp, &mbs, MBLOGNONE);
5890		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
5891			sdp->isp_req_ack_active_neg = 1;
5892			sdp->isp_data_line_active_neg = 1;
5893		} else {
5894			sdp->isp_req_ack_active_neg =
5895			    (mbs.param[1+channel] >> 4) & 0x1;
5896			sdp->isp_data_line_active_neg =
5897			    (mbs.param[1+channel] >> 5) & 0x1;
5898		}
5899	}
5900
5901	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc3,
5902	    0, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
5903	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
5904	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
5905	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc3,
5906	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
5907	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
5908	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
5909
5910	/*
5911	 * The trick here is to establish a default for the default (honk!)
5912	 * state (goal_flags). Then try and get the current status from
5913	 * the card to fill in the current state. We don't, in fact, set
5914	 * the default to the SAFE default state- that's not the goal state.
5915	 */
5916	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
5917		uint8_t off, per;
5918		sdp->isp_devparam[tgt].actv_offset = 0;
5919		sdp->isp_devparam[tgt].actv_period = 0;
5920		sdp->isp_devparam[tgt].actv_flags = 0;
5921
5922		sdp->isp_devparam[tgt].goal_flags =
5923		    sdp->isp_devparam[tgt].nvrm_flags = DPARM_DEFAULT;
5924
5925		/*
5926		 * We default to Wide/Fast for versions less than a 1040
5927		 * (unless it's SBus).
5928		 */
5929		if (IS_ULTRA3(isp)) {
5930			off = ISP_80M_SYNCPARMS >> 8;
5931			per = ISP_80M_SYNCPARMS & 0xff;
5932		} else if (IS_ULTRA2(isp)) {
5933			off = ISP_40M_SYNCPARMS >> 8;
5934			per = ISP_40M_SYNCPARMS & 0xff;
5935		} else if (IS_1240(isp)) {
5936			off = ISP_20M_SYNCPARMS >> 8;
5937			per = ISP_20M_SYNCPARMS & 0xff;
5938		} else if ((isp->isp_bustype == ISP_BT_SBUS &&
5939		    isp->isp_type < ISP_HA_SCSI_1020A) ||
5940		    (isp->isp_bustype == ISP_BT_PCI &&
5941		    isp->isp_type < ISP_HA_SCSI_1040) ||
5942		    (isp->isp_clock && isp->isp_clock < 60) ||
5943		    (sdp->isp_ultramode == 0)) {
5944			off = ISP_10M_SYNCPARMS >> 8;
5945			per = ISP_10M_SYNCPARMS & 0xff;
5946		} else {
5947			off = ISP_20M_SYNCPARMS_1040 >> 8;
5948			per = ISP_20M_SYNCPARMS_1040 & 0xff;
5949		}
5950		sdp->isp_devparam[tgt].goal_offset =
5951		    sdp->isp_devparam[tgt].nvrm_offset = off;
5952		sdp->isp_devparam[tgt].goal_period =
5953		    sdp->isp_devparam[tgt].nvrm_period = per;
5954
5955		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc3,
5956		    channel, tgt, sdp->isp_devparam[tgt].nvrm_flags,
5957		    sdp->isp_devparam[tgt].nvrm_offset,
5958		    sdp->isp_devparam[tgt].nvrm_period);
5959	}
5960}
5961
5962/*
5963 * Re-initialize the ISP and complete all orphaned commands
5964 * with a 'botched' notice. The reset/init routines should
5965 * not disturb an already active list of commands.
5966 *
5967 * Locks held prior to coming here.
5968 */
5969
5970void
5971isp_reinit(ispsoftc_t *isp)
5972{
5973	XS_T *xs;
5974	int i;
5975
5976	if (IS_FC(isp)) {
5977		isp_mark_getpdb_all(isp);
5978	}
5979	isp_reset(isp);
5980	if (isp->isp_state != ISP_RESETSTATE) {
5981		isp_prt(isp, ISP_LOGERR, "isp_reinit cannot reset card");
5982	} else if (isp->isp_role != ISP_ROLE_NONE) {
5983		isp_init(isp);
5984		if (isp->isp_state == ISP_INITSTATE) {
5985			isp->isp_state = ISP_RUNSTATE;
5986		}
5987		if (isp->isp_state != ISP_RUNSTATE) {
5988			isp_prt(isp, ISP_LOGERR,
5989			    "isp_reinit cannot restart card");
5990		}
5991	}
5992	isp->isp_nactive = 0;
5993
5994	for (i = 0; i < isp->isp_maxcmds; i++) {
5995		uint16_t handle;
5996		xs = isp->isp_xflist[i];
5997		if (xs == NULL) {
5998			continue;
5999		}
6000		handle = isp_index_handle(i);
6001		isp_destroy_handle(isp, handle);
6002		if (XS_XFRLEN(xs)) {
6003			ISP_DMAFREE(isp, xs, handle);
6004			XS_RESID(xs) = XS_XFRLEN(xs);
6005		} else {
6006			XS_RESID(xs) = 0;
6007		}
6008		XS_SETERR(xs, HBA_BUSRESET);
6009		isp_done(xs);
6010	}
6011}
6012
6013/*
6014 * NVRAM Routines
6015 */
6016static int
6017isp_read_nvram(ispsoftc_t *isp)
6018{
6019	int i, amt;
6020	uint8_t csum, minversion;
6021	union {
6022		uint8_t _x[ISP2100_NVRAM_SIZE];
6023		uint16_t _s[ISP2100_NVRAM_SIZE>>1];
6024	} _n;
6025#define	nvram_data	_n._x
6026#define	nvram_words	_n._s
6027
6028	if (IS_FC(isp)) {
6029		amt = ISP2100_NVRAM_SIZE;
6030		minversion = 1;
6031	} else if (IS_ULTRA2(isp)) {
6032		amt = ISP1080_NVRAM_SIZE;
6033		minversion = 0;
6034	} else {
6035		amt = ISP_NVRAM_SIZE;
6036		minversion = 2;
6037	}
6038
6039	/*
6040	 * Just read the first two words first to see if we have a valid
6041	 * NVRAM to continue reading the rest with.
6042	 */
6043	for (i = 0; i < 2; i++) {
6044		isp_rdnvram_word(isp, i, &nvram_words[i]);
6045	}
6046	if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
6047	    nvram_data[2] != 'P') {
6048		if (isp->isp_bustype != ISP_BT_SBUS) {
6049			isp_prt(isp, ISP_LOGWARN, "invalid NVRAM header");
6050			isp_prt(isp, ISP_LOGDEBUG0, "%x %x %x",
6051			    nvram_data[0], nvram_data[1], nvram_data[2]);
6052		}
6053		return (-1);
6054	}
6055	for (i = 2; i < amt>>1; i++) {
6056		isp_rdnvram_word(isp, i, &nvram_words[i]);
6057	}
6058	for (csum = 0, i = 0; i < amt; i++) {
6059		csum += nvram_data[i];
6060	}
6061	if (csum != 0) {
6062		isp_prt(isp, ISP_LOGWARN, "invalid NVRAM checksum");
6063		return (-1);
6064	}
6065	if (ISP_NVRAM_VERSION(nvram_data) < minversion) {
6066		isp_prt(isp, ISP_LOGWARN, "version %d NVRAM not understood",
6067		    ISP_NVRAM_VERSION(nvram_data));
6068		return (-1);
6069	}
6070
6071	if (IS_ULTRA3(isp)) {
6072		isp_parse_nvram_12160(isp, 0, nvram_data);
6073		if (IS_12160(isp))
6074			isp_parse_nvram_12160(isp, 1, nvram_data);
6075	} else if (IS_1080(isp)) {
6076		isp_parse_nvram_1080(isp, 0, nvram_data);
6077	} else if (IS_1280(isp) || IS_1240(isp)) {
6078		isp_parse_nvram_1080(isp, 0, nvram_data);
6079		isp_parse_nvram_1080(isp, 1, nvram_data);
6080	} else if (IS_SCSI(isp)) {
6081		isp_parse_nvram_1020(isp, nvram_data);
6082	} else {
6083		isp_parse_nvram_2100(isp, nvram_data);
6084	}
6085	return (0);
6086#undef	nvram_data
6087#undef	nvram_words
6088}
6089
6090static void
6091isp_rdnvram_word(ispsoftc_t *isp, int wo, uint16_t *rp)
6092{
6093	int i, cbits;
6094	uint16_t bit, rqst;
6095
6096	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
6097	USEC_DELAY(2);
6098	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
6099	USEC_DELAY(2);
6100
6101	if (IS_FC(isp)) {
6102		wo &= ((ISP2100_NVRAM_SIZE >> 1) - 1);
6103		if (IS_2312(isp) && isp->isp_port) {
6104			wo += 128;
6105		}
6106		rqst = (ISP_NVRAM_READ << 8) | wo;
6107		cbits = 10;
6108	} else if (IS_ULTRA2(isp)) {
6109		wo &= ((ISP1080_NVRAM_SIZE >> 1) - 1);
6110		rqst = (ISP_NVRAM_READ << 8) | wo;
6111		cbits = 10;
6112	} else {
6113		wo &= ((ISP_NVRAM_SIZE >> 1) - 1);
6114		rqst = (ISP_NVRAM_READ << 6) | wo;
6115		cbits = 8;
6116	}
6117
6118	/*
6119	 * Clock the word select request out...
6120	 */
6121	for (i = cbits; i >= 0; i--) {
6122		if ((rqst >> i) & 1) {
6123			bit = BIU_NVRAM_SELECT | BIU_NVRAM_DATAOUT;
6124		} else {
6125			bit = BIU_NVRAM_SELECT;
6126		}
6127		ISP_WRITE(isp, BIU_NVRAM, bit);
6128		USEC_DELAY(2);
6129		ISP_WRITE(isp, BIU_NVRAM, bit | BIU_NVRAM_CLOCK);
6130		USEC_DELAY(2);
6131		ISP_WRITE(isp, BIU_NVRAM, bit);
6132		USEC_DELAY(2);
6133	}
6134	/*
6135	 * Now read the result back in (bits come back in MSB format).
6136	 */
6137	*rp = 0;
6138	for (i = 0; i < 16; i++) {
6139		uint16_t rv;
6140		*rp <<= 1;
6141		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
6142		USEC_DELAY(2);
6143		rv = ISP_READ(isp, BIU_NVRAM);
6144		if (rv & BIU_NVRAM_DATAIN) {
6145			*rp |= 1;
6146		}
6147		USEC_DELAY(2);
6148		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
6149		USEC_DELAY(2);
6150	}
6151	ISP_WRITE(isp, BIU_NVRAM, 0);
6152	USEC_DELAY(2);
6153	ISP_SWIZZLE_NVRAM_WORD(isp, rp);
6154}
6155
6156static void
6157isp_parse_nvram_1020(ispsoftc_t *isp, uint8_t *nvram_data)
6158{
6159	sdparam *sdp = (sdparam *) isp->isp_param;
6160	int tgt;
6161
6162	sdp->isp_fifo_threshold =
6163		ISP_NVRAM_FIFO_THRESHOLD(nvram_data) |
6164		(ISP_NVRAM_FIFO_THRESHOLD_128(nvram_data) << 2);
6165
6166	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
6167		sdp->isp_initiator_id =
6168			ISP_NVRAM_INITIATOR_ID(nvram_data);
6169
6170	sdp->isp_bus_reset_delay =
6171		ISP_NVRAM_BUS_RESET_DELAY(nvram_data);
6172
6173	sdp->isp_retry_count =
6174		ISP_NVRAM_BUS_RETRY_COUNT(nvram_data);
6175
6176	sdp->isp_retry_delay =
6177		ISP_NVRAM_BUS_RETRY_DELAY(nvram_data);
6178
6179	sdp->isp_async_data_setup =
6180		ISP_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data);
6181
6182	if (isp->isp_type >= ISP_HA_SCSI_1040) {
6183		if (sdp->isp_async_data_setup < 9) {
6184			sdp->isp_async_data_setup = 9;
6185		}
6186	} else {
6187		if (sdp->isp_async_data_setup != 6) {
6188			sdp->isp_async_data_setup = 6;
6189		}
6190	}
6191
6192	sdp->isp_req_ack_active_neg =
6193		ISP_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data);
6194
6195	sdp->isp_data_line_active_neg =
6196		ISP_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data);
6197
6198	sdp->isp_data_dma_burst_enabl =
6199		ISP_NVRAM_DATA_DMA_BURST_ENABLE(nvram_data);
6200
6201	sdp->isp_cmd_dma_burst_enable =
6202		ISP_NVRAM_CMD_DMA_BURST_ENABLE(nvram_data);
6203
6204	sdp->isp_tag_aging =
6205		ISP_NVRAM_TAG_AGE_LIMIT(nvram_data);
6206
6207	sdp->isp_selection_timeout =
6208		ISP_NVRAM_SELECTION_TIMEOUT(nvram_data);
6209
6210	sdp->isp_max_queue_depth =
6211		ISP_NVRAM_MAX_QUEUE_DEPTH(nvram_data);
6212
6213	sdp->isp_fast_mttr = ISP_NVRAM_FAST_MTTR_ENABLE(nvram_data);
6214
6215	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
6216	    0, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
6217	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
6218	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
6219	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
6220	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
6221	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
6222	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
6223
6224	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
6225		sdp->isp_devparam[tgt].dev_enable =
6226			ISP_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt);
6227		sdp->isp_devparam[tgt].exc_throttle =
6228			ISP_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt);
6229		sdp->isp_devparam[tgt].nvrm_offset =
6230			ISP_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt);
6231		sdp->isp_devparam[tgt].nvrm_period =
6232			ISP_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt);
6233		/*
6234		 * We probably shouldn't lie about this, but it
6235		 * it makes it much safer if we limit NVRAM values
6236		 * to sanity.
6237		 */
6238		if (isp->isp_type < ISP_HA_SCSI_1040) {
6239			/*
6240			 * If we're not ultra, we can't possibly
6241			 * be a shorter period than this.
6242			 */
6243			if (sdp->isp_devparam[tgt].nvrm_period < 0x19) {
6244				sdp->isp_devparam[tgt].nvrm_period = 0x19;
6245			}
6246			if (sdp->isp_devparam[tgt].nvrm_offset > 0xc) {
6247				sdp->isp_devparam[tgt].nvrm_offset = 0x0c;
6248			}
6249		} else {
6250			if (sdp->isp_devparam[tgt].nvrm_offset > 0x8) {
6251				sdp->isp_devparam[tgt].nvrm_offset = 0x8;
6252			}
6253		}
6254		sdp->isp_devparam[tgt].nvrm_flags = 0;
6255		if (ISP_NVRAM_TGT_RENEG(nvram_data, tgt))
6256			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
6257		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
6258		if (ISP_NVRAM_TGT_TQING(nvram_data, tgt))
6259			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
6260		if (ISP_NVRAM_TGT_SYNC(nvram_data, tgt))
6261			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
6262		if (ISP_NVRAM_TGT_WIDE(nvram_data, tgt))
6263			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
6264		if (ISP_NVRAM_TGT_PARITY(nvram_data, tgt))
6265			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
6266		if (ISP_NVRAM_TGT_DISC(nvram_data, tgt))
6267			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
6268		sdp->isp_devparam[tgt].actv_flags = 0; /* we don't know */
6269		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
6270		    0, tgt, sdp->isp_devparam[tgt].nvrm_flags,
6271		    sdp->isp_devparam[tgt].nvrm_offset,
6272		    sdp->isp_devparam[tgt].nvrm_period);
6273		sdp->isp_devparam[tgt].goal_offset =
6274		    sdp->isp_devparam[tgt].nvrm_offset;
6275		sdp->isp_devparam[tgt].goal_period =
6276		    sdp->isp_devparam[tgt].nvrm_period;
6277		sdp->isp_devparam[tgt].goal_flags =
6278		    sdp->isp_devparam[tgt].nvrm_flags;
6279	}
6280}
6281
6282static void
6283isp_parse_nvram_1080(ispsoftc_t *isp, int bus, uint8_t *nvram_data)
6284{
6285	sdparam *sdp = (sdparam *) isp->isp_param;
6286	int tgt;
6287
6288	sdp += bus;
6289
6290	sdp->isp_fifo_threshold =
6291	    ISP1080_NVRAM_FIFO_THRESHOLD(nvram_data);
6292
6293	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
6294		sdp->isp_initiator_id =
6295		    ISP1080_NVRAM_INITIATOR_ID(nvram_data, bus);
6296
6297	sdp->isp_bus_reset_delay =
6298	    ISP1080_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
6299
6300	sdp->isp_retry_count =
6301	    ISP1080_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
6302
6303	sdp->isp_retry_delay =
6304	    ISP1080_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
6305
6306	sdp->isp_async_data_setup =
6307	    ISP1080_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
6308
6309	sdp->isp_req_ack_active_neg =
6310	    ISP1080_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
6311
6312	sdp->isp_data_line_active_neg =
6313	    ISP1080_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
6314
6315	sdp->isp_data_dma_burst_enabl =
6316	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
6317
6318	sdp->isp_cmd_dma_burst_enable =
6319	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
6320
6321	sdp->isp_selection_timeout =
6322	    ISP1080_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
6323
6324	sdp->isp_max_queue_depth =
6325	     ISP1080_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
6326
6327	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
6328	    bus, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
6329	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
6330	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
6331	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
6332	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
6333	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
6334	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
6335
6336
6337	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
6338		sdp->isp_devparam[tgt].dev_enable =
6339		    ISP1080_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
6340		sdp->isp_devparam[tgt].exc_throttle =
6341			ISP1080_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
6342		sdp->isp_devparam[tgt].nvrm_offset =
6343			ISP1080_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
6344		sdp->isp_devparam[tgt].nvrm_period =
6345			ISP1080_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
6346		sdp->isp_devparam[tgt].nvrm_flags = 0;
6347		if (ISP1080_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
6348			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
6349		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
6350		if (ISP1080_NVRAM_TGT_TQING(nvram_data, tgt, bus))
6351			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
6352		if (ISP1080_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
6353			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
6354		if (ISP1080_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
6355			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
6356		if (ISP1080_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
6357			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
6358		if (ISP1080_NVRAM_TGT_DISC(nvram_data, tgt, bus))
6359			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
6360		sdp->isp_devparam[tgt].actv_flags = 0;
6361		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
6362		    bus, tgt, sdp->isp_devparam[tgt].nvrm_flags,
6363		    sdp->isp_devparam[tgt].nvrm_offset,
6364		    sdp->isp_devparam[tgt].nvrm_period);
6365		sdp->isp_devparam[tgt].goal_offset =
6366		    sdp->isp_devparam[tgt].nvrm_offset;
6367		sdp->isp_devparam[tgt].goal_period =
6368		    sdp->isp_devparam[tgt].nvrm_period;
6369		sdp->isp_devparam[tgt].goal_flags =
6370		    sdp->isp_devparam[tgt].nvrm_flags;
6371	}
6372}
6373
6374static void
6375isp_parse_nvram_12160(ispsoftc_t *isp, int bus, uint8_t *nvram_data)
6376{
6377	sdparam *sdp = (sdparam *) isp->isp_param;
6378	int tgt;
6379
6380	sdp += bus;
6381
6382	sdp->isp_fifo_threshold =
6383	    ISP12160_NVRAM_FIFO_THRESHOLD(nvram_data);
6384
6385	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
6386		sdp->isp_initiator_id =
6387		    ISP12160_NVRAM_INITIATOR_ID(nvram_data, bus);
6388
6389	sdp->isp_bus_reset_delay =
6390	    ISP12160_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
6391
6392	sdp->isp_retry_count =
6393	    ISP12160_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
6394
6395	sdp->isp_retry_delay =
6396	    ISP12160_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
6397
6398	sdp->isp_async_data_setup =
6399	    ISP12160_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
6400
6401	sdp->isp_req_ack_active_neg =
6402	    ISP12160_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
6403
6404	sdp->isp_data_line_active_neg =
6405	    ISP12160_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
6406
6407	sdp->isp_data_dma_burst_enabl =
6408	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
6409
6410	sdp->isp_cmd_dma_burst_enable =
6411	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
6412
6413	sdp->isp_selection_timeout =
6414	    ISP12160_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
6415
6416	sdp->isp_max_queue_depth =
6417	     ISP12160_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
6418
6419	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
6420	    bus, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
6421	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
6422	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
6423	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
6424	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
6425	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
6426	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
6427
6428	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
6429		sdp->isp_devparam[tgt].dev_enable =
6430		    ISP12160_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
6431		sdp->isp_devparam[tgt].exc_throttle =
6432			ISP12160_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
6433		sdp->isp_devparam[tgt].nvrm_offset =
6434			ISP12160_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
6435		sdp->isp_devparam[tgt].nvrm_period =
6436			ISP12160_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
6437		sdp->isp_devparam[tgt].nvrm_flags = 0;
6438		if (ISP12160_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
6439			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
6440		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
6441		if (ISP12160_NVRAM_TGT_TQING(nvram_data, tgt, bus))
6442			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
6443		if (ISP12160_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
6444			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
6445		if (ISP12160_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
6446			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
6447		if (ISP12160_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
6448			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
6449		if (ISP12160_NVRAM_TGT_DISC(nvram_data, tgt, bus))
6450			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
6451		sdp->isp_devparam[tgt].actv_flags = 0;
6452		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
6453		    bus, tgt, sdp->isp_devparam[tgt].nvrm_flags,
6454		    sdp->isp_devparam[tgt].nvrm_offset,
6455		    sdp->isp_devparam[tgt].nvrm_period);
6456		sdp->isp_devparam[tgt].goal_offset =
6457		    sdp->isp_devparam[tgt].nvrm_offset;
6458		sdp->isp_devparam[tgt].goal_period =
6459		    sdp->isp_devparam[tgt].nvrm_period;
6460		sdp->isp_devparam[tgt].goal_flags =
6461		    sdp->isp_devparam[tgt].nvrm_flags;
6462	}
6463}
6464
6465static void
6466isp_parse_nvram_2100(ispsoftc_t *isp, uint8_t *nvram_data)
6467{
6468	fcparam *fcp = (fcparam *) isp->isp_param;
6469	uint64_t wwn;
6470
6471	/*
6472	 * There is NVRAM storage for both Port and Node entities-
6473	 * but the Node entity appears to be unused on all the cards
6474	 * I can find. However, we should account for this being set
6475	 * at some point in the future.
6476	 *
6477	 * Qlogic WWNs have an NAA of 2, but usually nothing shows up in
6478	 * bits 48..60. In the case of the 2202, it appears that they do
6479	 * use bit 48 to distinguish between the two instances on the card.
6480	 * The 2204, which I've never seen, *probably* extends this method.
6481	 */
6482	wwn = ISP2100_NVRAM_PORT_NAME(nvram_data);
6483	if (wwn) {
6484		isp_prt(isp, ISP_LOGCONFIG, "NVRAM Port WWN 0x%08x%08x",
6485		    (uint32_t) (wwn >> 32), (uint32_t) (wwn & 0xffffffff));
6486		if ((wwn >> 60) == 0) {
6487			wwn |= (((uint64_t) 2)<< 60);
6488		}
6489	}
6490	fcp->isp_portwwn = wwn;
6491	if (IS_2200(isp) || IS_23XX(isp)) {
6492		wwn = ISP2200_NVRAM_NODE_NAME(nvram_data);
6493		if (wwn) {
6494			isp_prt(isp, ISP_LOGCONFIG, "NVRAM Node WWN 0x%08x%08x",
6495			    (uint32_t) (wwn >> 32),
6496			    (uint32_t) (wwn & 0xffffffff));
6497			if ((wwn >> 60) == 0) {
6498				wwn |= (((uint64_t) 2)<< 60);
6499			}
6500		}
6501	} else {
6502		wwn &= ~((uint64_t) 0xfff << 48);
6503	}
6504	fcp->isp_nodewwn = wwn;
6505
6506	/*
6507	 * Make sure we have both Node and Port as non-zero values.
6508	 */
6509	if (fcp->isp_nodewwn != 0 && fcp->isp_portwwn == 0) {
6510		fcp->isp_portwwn = fcp->isp_nodewwn;
6511	} else if (fcp->isp_nodewwn == 0 && fcp->isp_portwwn != 0) {
6512		fcp->isp_nodewwn = fcp->isp_portwwn;
6513	}
6514
6515	/*
6516	 * Make the Node and Port values sane if they're NAA == 2.
6517	 * This means to clear bits 48..56 for the Node WWN and
6518	 * make sure that there's some non-zero value in 48..56
6519	 * for the Port WWN.
6520	 */
6521	if (fcp->isp_nodewwn && fcp->isp_portwwn) {
6522		if ((fcp->isp_nodewwn & (((uint64_t) 0xfff) << 48)) != 0 &&
6523		    (fcp->isp_nodewwn >> 60) == 2) {
6524			fcp->isp_nodewwn &= ~((uint64_t) 0xfff << 48);
6525		}
6526		if ((fcp->isp_portwwn & (((uint64_t) 0xfff) << 48)) == 0 &&
6527		    (fcp->isp_portwwn >> 60) == 2) {
6528			fcp->isp_portwwn |= ((uint64_t) 1 << 56);
6529		}
6530	}
6531
6532	isp_prt(isp, ISP_LOGDEBUG0,
6533	    "NVRAM: maxfrmlen %d execthrottle %d fwoptions 0x%x loopid %x",
6534	    ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data),
6535	    ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data),
6536	    ISP2100_NVRAM_OPTIONS(nvram_data),
6537	    ISP2100_NVRAM_HARDLOOPID(nvram_data));
6538
6539	fcp->isp_maxalloc =
6540		ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data);
6541	if ((isp->isp_confopts & ISP_CFG_OWNFSZ) == 0)
6542		fcp->isp_maxfrmlen =
6543			ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data);
6544	fcp->isp_retry_delay =
6545		ISP2100_NVRAM_RETRY_DELAY(nvram_data);
6546	fcp->isp_retry_count =
6547		ISP2100_NVRAM_RETRY_COUNT(nvram_data);
6548	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
6549		fcp->isp_loopid =
6550			ISP2100_NVRAM_HARDLOOPID(nvram_data);
6551	if ((isp->isp_confopts & ISP_CFG_OWNEXCTHROTTLE) == 0)
6552		fcp->isp_execthrottle =
6553			ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data);
6554	fcp->isp_fwoptions = ISP2100_NVRAM_OPTIONS(nvram_data);
6555}
6556
6557#ifdef	ISP_FW_CRASH_DUMP
6558static void isp2200_fw_dump(ispsoftc_t *);
6559static void isp2300_fw_dump(ispsoftc_t *);
6560
6561static void
6562isp2200_fw_dump(ispsoftc_t *isp)
6563{
6564	int i, j;
6565	mbreg_t mbs;
6566	uint16_t *ptr;
6567
6568	MEMZERO(&mbs, sizeof (mbs));
6569	ptr = FCPARAM(isp)->isp_dump_data;
6570	if (ptr == NULL) {
6571		isp_prt(isp, ISP_LOGERR,
6572		   "No place to dump RISC registers and SRAM");
6573		return;
6574	}
6575	if (*ptr++) {
6576		isp_prt(isp, ISP_LOGERR,
6577		   "dump area for RISC registers and SRAM already used");
6578		return;
6579	}
6580	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
6581	for (i = 0; i < 100; i++) {
6582		USEC_DELAY(100);
6583		if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6584			break;
6585		}
6586	}
6587	if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6588		/*
6589		 * PBIU Registers
6590		 */
6591		for (i = 0; i < 8; i++) {
6592			*ptr++ = ISP_READ(isp, BIU_BLOCK + (i << 1));
6593		}
6594
6595		/*
6596		 * Mailbox Registers
6597		 */
6598		for (i = 0; i < 8; i++) {
6599			*ptr++ = ISP_READ(isp, MBOX_BLOCK + (i << 1));
6600		}
6601
6602		/*
6603		 * DMA Registers
6604		 */
6605		for (i = 0; i < 48; i++) {
6606			*ptr++ = ISP_READ(isp, DMA_BLOCK + 0x20 + (i << 1));
6607		}
6608
6609		/*
6610		 * RISC H/W Registers
6611		 */
6612		ISP_WRITE(isp, BIU2100_CSR, 0);
6613		for (i = 0; i < 16; i++) {
6614			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0xA0 + (i << 1));
6615		}
6616
6617		/*
6618		 * RISC GP Registers
6619		 */
6620		for (j = 0; j < 8; j++) {
6621			ISP_WRITE(isp, BIU_BLOCK + 0xA4, 0x2000 + (j << 8));
6622			for (i = 0; i < 16; i++) {
6623				*ptr++ =
6624				    ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6625			}
6626		}
6627
6628		/*
6629		 * Frame Buffer Hardware Registers
6630		 */
6631		ISP_WRITE(isp, BIU2100_CSR, 0x10);
6632		for (i = 0; i < 16; i++) {
6633			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6634		}
6635
6636		/*
6637		 * Fibre Protocol Module 0 Hardware Registers
6638		 */
6639		ISP_WRITE(isp, BIU2100_CSR, 0x20);
6640		for (i = 0; i < 64; i++) {
6641			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6642		}
6643
6644		/*
6645		 * Fibre Protocol Module 1 Hardware Registers
6646		 */
6647		ISP_WRITE(isp, BIU2100_CSR, 0x30);
6648		for (i = 0; i < 64; i++) {
6649			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6650		}
6651	} else {
6652		isp_prt(isp, ISP_LOGERR, "RISC Would Not Pause");
6653		return;
6654	}
6655	isp_prt(isp, ISP_LOGALL,
6656	   "isp_fw_dump: RISC registers dumped successfully");
6657	ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
6658	for (i = 0; i < 100; i++) {
6659		USEC_DELAY(100);
6660		if (ISP_READ(isp, OUTMAILBOX0) == 0) {
6661			break;
6662		}
6663	}
6664	if (ISP_READ(isp, OUTMAILBOX0) != 0) {
6665		isp_prt(isp, ISP_LOGERR, "Board Would Not Reset");
6666		return;
6667	}
6668	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
6669	for (i = 0; i < 100; i++) {
6670		USEC_DELAY(100);
6671		if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6672			break;
6673		}
6674	}
6675	if ((ISP_READ(isp, HCCR) & HCCR_PAUSE) == 0) {
6676		isp_prt(isp, ISP_LOGERR, "RISC Would Not Pause After Reset");
6677		return;
6678	}
6679	ISP_WRITE(isp, RISC_EMB, 0xf2);
6680	ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
6681	for (i = 0; i < 100; i++) {
6682		USEC_DELAY(100);
6683		if ((ISP_READ(isp, HCCR) & HCCR_PAUSE) == 0) {
6684			break;
6685		}
6686	}
6687	ENABLE_INTS(isp);
6688	mbs.param[0] = MBOX_READ_RAM_WORD;
6689	mbs.param[1] = 0x1000;
6690	isp->isp_mbxworkp = (void *) ptr;
6691	isp->isp_mbxwrk0 = 0xefff;	/* continuation count */
6692	isp->isp_mbxwrk1 = 0x1001;	/* next SRAM address */
6693	isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
6694	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
6695		isp_prt(isp, ISP_LOGWARN,
6696		    "RAM DUMP FAILED @ WORD %x", isp->isp_mbxwrk1);
6697		return;
6698	}
6699	ptr = isp->isp_mbxworkp;	/* finish fetch of final word */
6700	*ptr++ = isp->isp_mboxtmp[2];
6701	isp_prt(isp, ISP_LOGALL, "isp_fw_dump: SRAM dumped successfully");
6702	FCPARAM(isp)->isp_dump_data[0] = isp->isp_type; /* now used */
6703	(void) isp_async(isp, ISPASYNC_FW_DUMPED, 0);
6704}
6705
6706static void
6707isp2300_fw_dump(ispsoftc_t *isp)
6708{
6709	int i, j;
6710	mbreg_t mbs;
6711	uint16_t *ptr;
6712
6713	MEMZERO(&mbs, sizeof (mbs));
6714	ptr = FCPARAM(isp)->isp_dump_data;
6715	if (ptr == NULL) {
6716		isp_prt(isp, ISP_LOGERR,
6717		   "No place to dump RISC registers and SRAM");
6718		return;
6719	}
6720	if (*ptr++) {
6721		isp_prt(isp, ISP_LOGERR,
6722		   "dump area for RISC registers and SRAM already used");
6723		return;
6724	}
6725	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
6726	for (i = 0; i < 100; i++) {
6727		USEC_DELAY(100);
6728		if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6729			break;
6730		}
6731	}
6732	if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6733		/*
6734		 * PBIU registers
6735		 */
6736		for (i = 0; i < 8; i++) {
6737			*ptr++ = ISP_READ(isp, BIU_BLOCK + (i << 1));
6738		}
6739
6740		/*
6741		 * ReqQ-RspQ-Risc2Host Status registers
6742		 */
6743		for (i = 0; i < 8; i++) {
6744			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x10 + (i << 1));
6745		}
6746
6747		/*
6748		 * Mailbox Registers
6749		 */
6750		for (i = 0; i < 32; i++) {
6751			*ptr++ =
6752			    ISP_READ(isp, PCI_MBOX_REGS2300_OFF + (i << 1));
6753		}
6754
6755		/*
6756		 * Auto Request Response DMA registers
6757		 */
6758		ISP_WRITE(isp, BIU2100_CSR, 0x40);
6759		for (i = 0; i < 32; i++) {
6760			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6761		}
6762
6763		/*
6764		 * DMA registers
6765		 */
6766		ISP_WRITE(isp, BIU2100_CSR, 0x50);
6767		for (i = 0; i < 48; i++) {
6768			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6769		}
6770
6771		/*
6772		 * RISC hardware registers
6773		 */
6774		ISP_WRITE(isp, BIU2100_CSR, 0);
6775		for (i = 0; i < 16; i++) {
6776			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0xA0 + (i << 1));
6777		}
6778
6779		/*
6780		 * RISC GP? registers
6781		 */
6782		for (j = 0; j < 8; j++) {
6783			ISP_WRITE(isp, BIU_BLOCK + 0xA4, 0x2000 + (j << 9));
6784			for (i = 0; i < 16; i++) {
6785				*ptr++ =
6786				    ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6787			}
6788		}
6789
6790		/*
6791		 * frame buffer hardware registers
6792		 */
6793		ISP_WRITE(isp, BIU2100_CSR, 0x10);
6794		for (i = 0; i < 64; i++) {
6795			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6796		}
6797
6798		/*
6799		 * FPM B0 hardware registers
6800		 */
6801		ISP_WRITE(isp, BIU2100_CSR, 0x20);
6802		for (i = 0; i < 64; i++) {
6803			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6804		}
6805
6806		/*
6807		 * FPM B1 hardware registers
6808		 */
6809		ISP_WRITE(isp, BIU2100_CSR, 0x30);
6810		for (i = 0; i < 64; i++) {
6811			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6812		}
6813	} else {
6814		isp_prt(isp, ISP_LOGERR, "RISC Would Not Pause");
6815		return;
6816	}
6817	isp_prt(isp, ISP_LOGALL,
6818	   "isp_fw_dump: RISC registers dumped successfully");
6819	ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
6820	for (i = 0; i < 100; i++) {
6821		USEC_DELAY(100);
6822		if (ISP_READ(isp, OUTMAILBOX0) == 0) {
6823			break;
6824		}
6825	}
6826	if (ISP_READ(isp, OUTMAILBOX0) != 0) {
6827		isp_prt(isp, ISP_LOGERR, "Board Would Not Reset");
6828		return;
6829	}
6830	ENABLE_INTS(isp);
6831	mbs.param[0] = MBOX_READ_RAM_WORD;
6832	mbs.param[1] = 0x800;
6833	isp->isp_mbxworkp = (void *) ptr;
6834	isp->isp_mbxwrk0 = 0xf7ff;	/* continuation count */
6835	isp->isp_mbxwrk1 = 0x801;	/* next SRAM address */
6836	isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
6837	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
6838		isp_prt(isp, ISP_LOGWARN,
6839		    "RAM DUMP FAILED @ WORD %x", isp->isp_mbxwrk1);
6840		return;
6841	}
6842	ptr = isp->isp_mbxworkp;	/* finish fetch of final word */
6843	*ptr++ = isp->isp_mboxtmp[2];
6844
6845	/*
6846	 * We don't have access to mailbox registers 8.. onward
6847	 * in our 'common' device model- so we have to set it
6848	 * here and hope it stays the same!
6849	 */
6850	ISP_WRITE(isp, PCI_MBOX_REGS2300_OFF + (8 << 1), 0x1);
6851
6852	mbs.param[0] = MBOX_READ_RAM_WORD_EXTENDED;
6853	mbs.param[1] = 0;
6854	isp->isp_mbxworkp = (void *) ptr;
6855	isp->isp_mbxwrk0 = 0xffff;	/* continuation count */
6856	isp->isp_mbxwrk1 = 0x1;		/* next SRAM address */
6857	isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
6858	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
6859		isp_prt(isp, ISP_LOGWARN,
6860		    "RAM DUMP FAILED @ WORD %x", 0x10000 + isp->isp_mbxwrk1);
6861		return;
6862	}
6863	ptr = isp->isp_mbxworkp;	/* finish final word */
6864	*ptr++ = mbs.param[2];
6865	isp_prt(isp, ISP_LOGALL, "isp_fw_dump: SRAM dumped successfully");
6866	FCPARAM(isp)->isp_dump_data[0] = isp->isp_type; /* now used */
6867	(void) isp_async(isp, ISPASYNC_FW_DUMPED, 0);
6868}
6869
6870void
6871isp_fw_dump(ispsoftc_t *isp)
6872{
6873	if (IS_2200(isp))
6874		isp2200_fw_dump(isp);
6875	else if (IS_23XX(isp))
6876		isp2300_fw_dump(isp);
6877}
6878#endif
6879