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