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