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