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