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