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