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