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