isp.c revision 119459
1/* $FreeBSD: head/sys/dev/isp/isp.c 119459 2003-08-25 17:58:23Z mjacob $ */
2/*
3 * Machine and OS Independent (well, as best as possible)
4 * code for the Qlogic ISP SCSI adapters.
5 *
6 * Copyright (c) 1997, 1998, 1999, 2000, 2001 by Matthew Jacob
7 * Feral Software
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice immediately at the beginning of the file, without modification,
15 *    this list of conditions, and the following disclaimer.
16 * 2. The name of the author may not be used to endorse or promote products
17 *    derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32/*
33 * Inspiration and ideas about this driver are from Erik Moe's Linux driver
34 * (qlogicisp.c) and Dave Miller's SBus version of same (qlogicisp.c). Some
35 * ideas dredged from the Solaris driver.
36 */
37
38/*
39 * Include header file appropriate for platform we're building on.
40 */
41
42#ifdef	__NetBSD__
43#include <dev/ic/isp_netbsd.h>
44#endif
45#ifdef	__FreeBSD__
46#include <dev/isp/isp_freebsd.h>
47#endif
48#ifdef	__OpenBSD__
49#include <dev/ic/isp_openbsd.h>
50#endif
51#ifdef	__linux__
52#include "isp_linux.h"
53#endif
54#ifdef	__svr4__
55#include "isp_solaris.h"
56#endif
57
58/*
59 * General defines
60 */
61
62#define	MBOX_DELAY_COUNT	1000000 / 100
63
64/*
65 * Local static data
66 */
67static const char portshift[] =
68    "Target %d Loop ID 0x%x (Port 0x%x) => Loop 0x%x (Port 0x%x)";
69static const char portdup[] =
70    "Target %d duplicates Target %d- killing off both";
71static const char retained[] =
72    "Retaining Loop ID 0x%x for Target %d (Port 0x%x)";
73static const char lretained[] =
74    "Retained login of Target %d (Loop ID 0x%x) Port 0x%x";
75static const char plogout[] =
76    "Logging out Target %d at Loop ID 0x%x (Port 0x%x)";
77static const char plogierr[] =
78    "Command Error in PLOGI for Port 0x%x (0x%x)";
79static const char nopdb[] =
80    "Could not get PDB for Device @ Port 0x%x";
81static const char pdbmfail1[] =
82    "PDB Loop ID info for Device @ Port 0x%x does not match up (0x%x)";
83static const char pdbmfail2[] =
84    "PDB Port info for Device @ Port 0x%x does not match up (0x%x)";
85static const char ldumped[] =
86    "Target %d (Loop ID 0x%x) Port 0x%x dumped after login info mismatch";
87static const char notresp[] =
88  "Not RESPONSE in RESPONSE Queue (type 0x%x) @ idx %d (next %d) nlooked %d";
89static const char xact1[] =
90    "HBA attempted queued transaction with disconnect not set for %d.%d.%d";
91static const char xact2[] =
92    "HBA attempted queued transaction to target routine %d on target %d bus %d";
93static const char xact3[] =
94    "HBA attempted queued cmd for %d.%d.%d when queueing disabled";
95static const char pskip[] =
96    "SCSI phase skipped for target %d.%d.%d";
97static const char topology[] =
98    "Loop ID %d, AL_PA 0x%x, Port ID 0x%x, Loop State 0x%x, Topology '%s'";
99static const char swrej[] =
100    "Fabric Nameserver rejected %s (Reason=0x%x Expl=0x%x) for Port ID 0x%x";
101static const char finmsg[] =
102    "(%d.%d.%d): FIN dl%d resid %d STS 0x%x SKEY %c XS_ERR=0x%x";
103static const char sc0[] =
104    "%s CHAN %d FTHRSH %d IID %d RESETD %d RETRYC %d RETRYD %d ASD 0x%x";
105static const char sc1[] =
106    "%s RAAN 0x%x DLAN 0x%x DDMAB 0x%x CDMAB 0x%x SELTIME %d MQD %d";
107static const char sc2[] = "%s CHAN %d TGT %d FLAGS 0x%x 0x%x/0x%x";
108static const char sc3[] = "Generated";
109static const char sc4[] = "NVRAM";
110static const char bun[] =
111    "bad underrun for %d.%d (count %d, resid %d, status %s)";
112
113/*
114 * Local function prototypes.
115 */
116static int isp_parse_async(struct ispsoftc *, u_int16_t);
117static int isp_handle_other_response(struct ispsoftc *, int, isphdr_t *,
118    u_int16_t *);
119static void
120isp_parse_status(struct ispsoftc *, ispstatusreq_t *, XS_T *);
121static void isp_fastpost_complete(struct ispsoftc *, u_int16_t);
122static int isp_mbox_continue(struct ispsoftc *);
123static void isp_scsi_init(struct ispsoftc *);
124static void isp_scsi_channel_init(struct ispsoftc *, int);
125static void isp_fibre_init(struct ispsoftc *);
126static void isp_mark_getpdb_all(struct ispsoftc *);
127static int isp_getmap(struct ispsoftc *, fcpos_map_t *);
128static int isp_getpdb(struct ispsoftc *, int, isp_pdb_t *);
129static u_int64_t isp_get_portname(struct ispsoftc *, int, int);
130static int isp_fclink_test(struct ispsoftc *, int);
131static char *isp2100_fw_statename(int);
132static int isp_pdb_sync(struct ispsoftc *);
133static int isp_scan_loop(struct ispsoftc *);
134static int isp_fabric_mbox_cmd(struct ispsoftc *, mbreg_t *);
135static int isp_scan_fabric(struct ispsoftc *, int);
136static void isp_register_fc4_type(struct ispsoftc *);
137static void isp_fw_state(struct ispsoftc *);
138static void isp_mboxcmd_qnw(struct ispsoftc *, mbreg_t *, int);
139static void isp_mboxcmd(struct ispsoftc *, mbreg_t *, int);
140
141static void isp_update(struct ispsoftc *);
142static void isp_update_bus(struct ispsoftc *, int);
143static void isp_setdfltparm(struct ispsoftc *, int);
144static int isp_read_nvram(struct ispsoftc *);
145static void isp_rdnvram_word(struct ispsoftc *, int, u_int16_t *);
146static void isp_parse_nvram_1020(struct ispsoftc *, u_int8_t *);
147static void isp_parse_nvram_1080(struct ispsoftc *, int, u_int8_t *);
148static void isp_parse_nvram_12160(struct ispsoftc *, int, u_int8_t *);
149static void isp_parse_nvram_2100(struct ispsoftc *, u_int8_t *);
150
151/*
152 * Reset Hardware.
153 *
154 * Hit the chip over the head, download new f/w if available and set it running.
155 *
156 * Locking done elsewhere.
157 */
158
159void
160isp_reset(struct ispsoftc *isp)
161{
162	mbreg_t mbs;
163	u_int16_t code_org;
164	int loops, i, dodnld = 1;
165	char *btype = "????";
166
167	isp->isp_state = ISP_NILSTATE;
168
169	/*
170	 * Basic types (SCSI, FibreChannel and PCI or SBus)
171	 * have been set in the MD code. We figure out more
172	 * here. Possibly more refined types based upon PCI
173	 * identification. Chip revision has been gathered.
174	 *
175	 * After we've fired this chip up, zero out the conf1 register
176	 * for SCSI adapters and do other settings for the 2100.
177	 */
178
179	/*
180	 * Get the current running firmware revision out of the
181	 * chip before we hit it over the head (if this is our
182	 * first time through). Note that we store this as the
183	 * 'ROM' firmware revision- which it may not be. In any
184	 * case, we don't really use this yet, but we may in
185	 * the future.
186	 */
187	if (isp->isp_touched == 0) {
188		/*
189		 * First see whether or not we're sitting in the ISP PROM.
190		 * If we've just been reset, we'll have the string "ISP   "
191		 * spread through outgoing mailbox registers 1-3. We do
192		 * this for PCI cards because otherwise we really don't
193		 * know what state the card is in and we could hang if
194		 * we try this command otherwise.
195		 *
196		 * For SBus cards, we just do this because they almost
197		 * certainly will be running firmware by now.
198		 */
199		if (ISP_READ(isp, OUTMAILBOX1) != 0x4953 ||
200		    ISP_READ(isp, OUTMAILBOX2) != 0x5020 ||
201		    ISP_READ(isp, OUTMAILBOX3) != 0x2020) {
202			/*
203			 * Just in case it was paused...
204			 */
205			ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
206			mbs.param[0] = MBOX_ABOUT_FIRMWARE;
207			isp_mboxcmd(isp, &mbs, MBLOGNONE);
208			if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
209				isp->isp_romfw_rev[0] = mbs.param[1];
210				isp->isp_romfw_rev[1] = mbs.param[2];
211				isp->isp_romfw_rev[2] = mbs.param[3];
212			}
213		}
214		isp->isp_touched = 1;
215	}
216
217	DISABLE_INTS(isp);
218
219	/*
220	 * Set up default request/response queue in-pointer/out-pointer
221	 * register indices.
222	 */
223	if (IS_23XX(isp)) {
224		isp->isp_rqstinrp = BIU_REQINP;
225		isp->isp_rqstoutrp = BIU_REQOUTP;
226		isp->isp_respinrp = BIU_RSPINP;
227		isp->isp_respoutrp = BIU_RSPOUTP;
228	} else {
229		isp->isp_rqstinrp = INMAILBOX4;
230		isp->isp_rqstoutrp = OUTMAILBOX4;
231		isp->isp_respinrp = OUTMAILBOX5;
232		isp->isp_respoutrp = INMAILBOX5;
233	}
234
235	/*
236	 * Put the board into PAUSE mode (so we can read the SXP registers
237	 * or write FPM/FBM registers).
238	 */
239	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
240
241	if (IS_FC(isp)) {
242		switch (isp->isp_type) {
243		case ISP_HA_FC_2100:
244			btype = "2100";
245			break;
246		case ISP_HA_FC_2200:
247			btype = "2200";
248			break;
249		case ISP_HA_FC_2300:
250			btype = "2300";
251			break;
252		case ISP_HA_FC_2312:
253			btype = "2312";
254			break;
255		default:
256			break;
257		}
258		/*
259		 * While we're paused, reset the FPM module and FBM fifos.
260		 */
261		ISP_WRITE(isp, BIU2100_CSR, BIU2100_FPM0_REGS);
262		ISP_WRITE(isp, FPM_DIAG_CONFIG, FPM_SOFT_RESET);
263		ISP_WRITE(isp, BIU2100_CSR, BIU2100_FB_REGS);
264		ISP_WRITE(isp, FBM_CMD, FBMCMD_FIFO_RESET_ALL);
265		ISP_WRITE(isp, BIU2100_CSR, BIU2100_RISC_REGS);
266	} else if (IS_1240(isp)) {
267		sdparam *sdp = isp->isp_param;
268		btype = "1240";
269		isp->isp_clock = 60;
270		sdp->isp_ultramode = 1;
271		sdp++;
272		sdp->isp_ultramode = 1;
273		/*
274		 * XXX: Should probably do some bus sensing.
275		 */
276	} else if (IS_ULTRA2(isp)) {
277		static const char m[] = "bus %d is in %s Mode";
278		u_int16_t l;
279		sdparam *sdp = isp->isp_param;
280
281		isp->isp_clock = 100;
282
283		if (IS_1280(isp))
284			btype = "1280";
285		else if (IS_1080(isp))
286			btype = "1080";
287		else if (IS_10160(isp))
288			btype = "10160";
289		else if (IS_12160(isp))
290			btype = "12160";
291		else
292			btype = "<UNKLVD>";
293
294		l = ISP_READ(isp, SXP_PINS_DIFF) & ISP1080_MODE_MASK;
295		switch (l) {
296		case ISP1080_LVD_MODE:
297			sdp->isp_lvdmode = 1;
298			isp_prt(isp, ISP_LOGCONFIG, m, 0, "LVD");
299			break;
300		case ISP1080_HVD_MODE:
301			sdp->isp_diffmode = 1;
302			isp_prt(isp, ISP_LOGCONFIG, m, 0, "Differential");
303			break;
304		case ISP1080_SE_MODE:
305			sdp->isp_ultramode = 1;
306			isp_prt(isp, ISP_LOGCONFIG, m, 0, "Single-Ended");
307			break;
308		default:
309			isp_prt(isp, ISP_LOGERR,
310			    "unknown mode on bus %d (0x%x)", 0, l);
311			break;
312		}
313
314		if (IS_DUALBUS(isp)) {
315			sdp++;
316			l = ISP_READ(isp, SXP_PINS_DIFF|SXP_BANK1_SELECT);
317			l &= ISP1080_MODE_MASK;
318			switch(l) {
319			case ISP1080_LVD_MODE:
320				sdp->isp_lvdmode = 1;
321				isp_prt(isp, ISP_LOGCONFIG, m, 1, "LVD");
322				break;
323			case ISP1080_HVD_MODE:
324				sdp->isp_diffmode = 1;
325				isp_prt(isp, ISP_LOGCONFIG,
326				    m, 1, "Differential");
327				break;
328			case ISP1080_SE_MODE:
329				sdp->isp_ultramode = 1;
330				isp_prt(isp, ISP_LOGCONFIG,
331				    m, 1, "Single-Ended");
332				break;
333			default:
334				isp_prt(isp, ISP_LOGERR,
335				    "unknown mode on bus %d (0x%x)", 1, l);
336				break;
337			}
338		}
339	} else {
340		sdparam *sdp = isp->isp_param;
341		i = ISP_READ(isp, BIU_CONF0) & BIU_CONF0_HW_MASK;
342		switch (i) {
343		default:
344			isp_prt(isp, ISP_LOGALL, "Unknown Chip Type 0x%x", i);
345			/* FALLTHROUGH */
346		case 1:
347			btype = "1020";
348			isp->isp_type = ISP_HA_SCSI_1020;
349			isp->isp_clock = 40;
350			break;
351		case 2:
352			/*
353			 * Some 1020A chips are Ultra Capable, but don't
354			 * run the clock rate up for that unless told to
355			 * do so by the Ultra Capable bits being set.
356			 */
357			btype = "1020A";
358			isp->isp_type = ISP_HA_SCSI_1020A;
359			isp->isp_clock = 40;
360			break;
361		case 3:
362			btype = "1040";
363			isp->isp_type = ISP_HA_SCSI_1040;
364			isp->isp_clock = 60;
365			break;
366		case 4:
367			btype = "1040A";
368			isp->isp_type = ISP_HA_SCSI_1040A;
369			isp->isp_clock = 60;
370			break;
371		case 5:
372			btype = "1040B";
373			isp->isp_type = ISP_HA_SCSI_1040B;
374			isp->isp_clock = 60;
375			break;
376		case 6:
377			btype = "1040C";
378			isp->isp_type = ISP_HA_SCSI_1040C;
379			isp->isp_clock = 60;
380                        break;
381		}
382		/*
383		 * Now, while we're at it, gather info about ultra
384		 * and/or differential mode.
385		 */
386		if (ISP_READ(isp, SXP_PINS_DIFF) & SXP_PINS_DIFF_MODE) {
387			isp_prt(isp, ISP_LOGCONFIG, "Differential Mode");
388			sdp->isp_diffmode = 1;
389		} else {
390			sdp->isp_diffmode = 0;
391		}
392		i = ISP_READ(isp, RISC_PSR);
393		if (isp->isp_bustype == ISP_BT_SBUS) {
394			i &= RISC_PSR_SBUS_ULTRA;
395		} else {
396			i &= RISC_PSR_PCI_ULTRA;
397		}
398		if (i != 0) {
399			isp_prt(isp, ISP_LOGCONFIG, "Ultra Mode Capable");
400			sdp->isp_ultramode = 1;
401			/*
402			 * If we're in Ultra Mode, we have to be 60MHz clock-
403			 * even for the SBus version.
404			 */
405			isp->isp_clock = 60;
406		} else {
407			sdp->isp_ultramode = 0;
408			/*
409			 * Clock is known. Gronk.
410			 */
411		}
412
413		/*
414		 * Machine dependent clock (if set) overrides
415		 * our generic determinations.
416		 */
417		if (isp->isp_mdvec->dv_clock) {
418			if (isp->isp_mdvec->dv_clock < isp->isp_clock) {
419				isp->isp_clock = isp->isp_mdvec->dv_clock;
420			}
421		}
422
423	}
424
425	/*
426	 * Clear instrumentation
427	 */
428	isp->isp_intcnt = isp->isp_intbogus = 0;
429
430	/*
431	 * Do MD specific pre initialization
432	 */
433	ISP_RESET0(isp);
434
435again:
436
437	/*
438	 * Hit the chip over the head with hammer,
439	 * and give the ISP a chance to recover.
440	 */
441
442	if (IS_SCSI(isp)) {
443		ISP_WRITE(isp, BIU_ICR, BIU_ICR_SOFT_RESET);
444		/*
445		 * A slight delay...
446		 */
447		USEC_DELAY(100);
448
449		/*
450		 * Clear data && control DMA engines.
451		 */
452		ISP_WRITE(isp, CDMA_CONTROL,
453		    DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
454		ISP_WRITE(isp, DDMA_CONTROL,
455		    DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
456
457
458	} else {
459		ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
460		/*
461		 * A slight delay...
462		 */
463		USEC_DELAY(100);
464
465		/*
466		 * Clear data && control DMA engines.
467		 */
468		ISP_WRITE(isp, CDMA2100_CONTROL,
469			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
470		ISP_WRITE(isp, TDMA2100_CONTROL,
471			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
472		ISP_WRITE(isp, RDMA2100_CONTROL,
473			DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
474	}
475
476	/*
477	 * Wait for ISP to be ready to go...
478	 */
479	loops = MBOX_DELAY_COUNT;
480	for (;;) {
481		if (IS_SCSI(isp)) {
482			if (!(ISP_READ(isp, BIU_ICR) & BIU_ICR_SOFT_RESET))
483				break;
484		} else {
485			if (!(ISP_READ(isp, BIU2100_CSR) & BIU2100_SOFT_RESET))
486				break;
487		}
488		USEC_DELAY(100);
489		if (--loops < 0) {
490			ISP_DUMPREGS(isp, "chip reset timed out");
491			return;
492		}
493	}
494
495	/*
496	 * After we've fired this chip up, zero out the conf1 register
497	 * for SCSI adapters and other settings for the 2100.
498	 */
499
500	if (IS_SCSI(isp)) {
501		ISP_WRITE(isp, BIU_CONF1, 0);
502	} else {
503		ISP_WRITE(isp, BIU2100_CSR, 0);
504	}
505
506	/*
507	 * Reset RISC Processor
508	 */
509	ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
510	USEC_DELAY(100);
511	/* Clear semaphore register (just to be sure) */
512	ISP_WRITE(isp, BIU_SEMA, 0);
513
514	/*
515	 * Establish some initial burst rate stuff.
516	 * (only for the 1XX0 boards). This really should
517	 * be done later after fetching from NVRAM.
518	 */
519	if (IS_SCSI(isp)) {
520		u_int16_t tmp = isp->isp_mdvec->dv_conf1;
521		/*
522		 * Busted FIFO. Turn off all but burst enables.
523		 */
524		if (isp->isp_type == ISP_HA_SCSI_1040A) {
525			tmp &= BIU_BURST_ENABLE;
526		}
527		ISP_SETBITS(isp, BIU_CONF1, tmp);
528		if (tmp & BIU_BURST_ENABLE) {
529			ISP_SETBITS(isp, CDMA_CONF, DMA_ENABLE_BURST);
530			ISP_SETBITS(isp, DDMA_CONF, DMA_ENABLE_BURST);
531		}
532#ifdef	PTI_CARDS
533		if (((sdparam *) isp->isp_param)->isp_ultramode) {
534			while (ISP_READ(isp, RISC_MTR) != 0x1313) {
535				ISP_WRITE(isp, RISC_MTR, 0x1313);
536				ISP_WRITE(isp, HCCR, HCCR_CMD_STEP);
537			}
538		} else {
539			ISP_WRITE(isp, RISC_MTR, 0x1212);
540		}
541		/*
542		 * PTI specific register
543		 */
544		ISP_WRITE(isp, RISC_EMB, DUAL_BANK)
545#else
546		ISP_WRITE(isp, RISC_MTR, 0x1212);
547#endif
548	} else {
549		ISP_WRITE(isp, RISC_MTR2100, 0x1212);
550		if (IS_2200(isp) || IS_23XX(isp)) {
551			ISP_WRITE(isp, HCCR, HCCR_2X00_DISABLE_PARITY_PAUSE);
552		}
553	}
554
555	ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE); /* release paused processor */
556
557	/*
558	 * Do MD specific post initialization
559	 */
560	ISP_RESET1(isp);
561
562	/*
563	 * Wait for everything to finish firing up.
564	 *
565	 * Avoid doing this on the 2312 because you can generate a PCI
566	 * parity error (chip breakage).
567	 */
568	if (IS_23XX(isp)) {
569		USEC_DELAY(5);
570	} else {
571		loops = MBOX_DELAY_COUNT;
572		while (ISP_READ(isp, OUTMAILBOX0) == MBOX_BUSY) {
573			USEC_DELAY(100);
574			if (--loops < 0) {
575				isp_prt(isp, ISP_LOGERR,
576				    "MBOX_BUSY never cleared on reset");
577				return;
578			}
579		}
580	}
581
582	/*
583	 * Up until this point we've done everything by just reading or
584	 * setting registers. From this point on we rely on at least *some*
585	 * kind of firmware running in the card.
586	 */
587
588	/*
589	 * Do some sanity checking.
590	 */
591	mbs.param[0] = MBOX_NO_OP;
592	isp_mboxcmd(isp, &mbs, MBLOGALL);
593	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
594		return;
595	}
596
597	if (IS_SCSI(isp)) {
598		mbs.param[0] = MBOX_MAILBOX_REG_TEST;
599		mbs.param[1] = 0xdead;
600		mbs.param[2] = 0xbeef;
601		mbs.param[3] = 0xffff;
602		mbs.param[4] = 0x1111;
603		mbs.param[5] = 0xa5a5;
604		isp_mboxcmd(isp, &mbs, MBLOGALL);
605		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
606			return;
607		}
608		if (mbs.param[1] != 0xdead || mbs.param[2] != 0xbeef ||
609		    mbs.param[3] != 0xffff || mbs.param[4] != 0x1111 ||
610		    mbs.param[5] != 0xa5a5) {
611			isp_prt(isp, ISP_LOGERR,
612			    "Register Test Failed (0x%x 0x%x 0x%x 0x%x 0x%x)",
613			    mbs.param[1], mbs.param[2], mbs.param[3],
614			    mbs.param[4], mbs.param[5]);
615			return;
616		}
617
618	}
619
620	/*
621	 * Download new Firmware, unless requested not to do so.
622	 * This is made slightly trickier in some cases where the
623	 * firmware of the ROM revision is newer than the revision
624	 * compiled into the driver. So, where we used to compare
625	 * versions of our f/w and the ROM f/w, now we just see
626	 * whether we have f/w at all and whether a config flag
627	 * has disabled our download.
628	 */
629	if ((isp->isp_mdvec->dv_ispfw == NULL) ||
630	    (isp->isp_confopts & ISP_CFG_NORELOAD)) {
631		dodnld = 0;
632	}
633
634	if (IS_23XX(isp))
635		code_org = ISP_CODE_ORG_2300;
636	else
637		code_org = ISP_CODE_ORG;
638
639	if (dodnld) {
640		isp->isp_mbxworkp = (void *) &isp->isp_mdvec->dv_ispfw[1];
641		isp->isp_mbxwrk0 = isp->isp_mdvec->dv_ispfw[3] - 1;
642		isp->isp_mbxwrk1 = code_org + 1;
643		mbs.param[0] = MBOX_WRITE_RAM_WORD;
644		mbs.param[1] = code_org;
645		mbs.param[2] = isp->isp_mdvec->dv_ispfw[0];
646		isp_mboxcmd(isp, &mbs, MBLOGNONE);
647		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
648			isp_prt(isp, ISP_LOGERR,
649			    "F/W download failed at word %d",
650			    isp->isp_mbxwrk1 - code_org);
651			dodnld = 0;
652			goto again;
653		}
654		/*
655		 * Verify that it downloaded correctly.
656		 */
657		mbs.param[0] = MBOX_VERIFY_CHECKSUM;
658		mbs.param[1] = code_org;
659		isp_mboxcmd(isp, &mbs, MBLOGNONE);
660		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
661			isp_prt(isp, ISP_LOGERR, "Ram Checksum Failure");
662			return;
663		}
664		isp->isp_loaded_fw = 1;
665	} else {
666		isp->isp_loaded_fw = 0;
667		isp_prt(isp, ISP_LOGDEBUG2, "skipping f/w download");
668	}
669
670	/*
671	 * Now start it rolling.
672	 *
673	 * If we didn't actually download f/w,
674	 * we still need to (re)start it.
675	 */
676
677
678	mbs.param[0] = MBOX_EXEC_FIRMWARE;
679	mbs.param[1] = code_org;
680	isp_mboxcmd(isp, &mbs, MBLOGNONE);
681	/*
682	 * Give it a chance to start.
683	 */
684	USEC_DELAY(500);
685
686	if (IS_SCSI(isp)) {
687		/*
688		 * Set CLOCK RATE, but only if asked to.
689		 */
690		if (isp->isp_clock) {
691			mbs.param[0] = MBOX_SET_CLOCK_RATE;
692			mbs.param[1] = isp->isp_clock;
693			isp_mboxcmd(isp, &mbs, MBLOGALL);
694			/* we will try not to care if this fails */
695		}
696	}
697
698	mbs.param[0] = MBOX_ABOUT_FIRMWARE;
699	isp_mboxcmd(isp, &mbs, MBLOGALL);
700	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
701		return;
702	}
703
704	/*
705	 * The SBus firmware that we are using apparently does not return
706	 * major, minor, micro revisions in the mailbox registers, which
707	 * is really, really, annoying.
708	 */
709	if (ISP_SBUS_SUPPORTED && isp->isp_bustype == ISP_BT_SBUS) {
710		if (dodnld) {
711#ifdef	ISP_TARGET_MODE
712			isp->isp_fwrev[0] = 7;
713			isp->isp_fwrev[1] = 55;
714#else
715			isp->isp_fwrev[0] = 1;
716			isp->isp_fwrev[1] = 37;
717#endif
718			isp->isp_fwrev[2] = 0;
719		}
720	} else {
721		isp->isp_fwrev[0] = mbs.param[1];
722		isp->isp_fwrev[1] = mbs.param[2];
723		isp->isp_fwrev[2] = mbs.param[3];
724	}
725	isp_prt(isp, ISP_LOGCONFIG,
726	    "Board Type %s, Chip Revision 0x%x, %s F/W Revision %d.%d.%d",
727	    btype, isp->isp_revision, dodnld? "loaded" : "resident",
728	    isp->isp_fwrev[0], isp->isp_fwrev[1], isp->isp_fwrev[2]);
729
730	if (IS_FC(isp)) {
731		/*
732		 * We do not believe firmware attributes for 2100 code less
733		 * than 1.17.0, unless it's the firmware we specifically
734		 * are loading.
735		 *
736		 * Note that all 22XX and 23XX f/w is greater than 1.X.0.
737		 */
738		if (!(ISP_FW_NEWER_THAN(isp, 1, 17, 0))) {
739#ifdef	USE_SMALLER_2100_FIRMWARE
740			FCPARAM(isp)->isp_fwattr = ISP_FW_ATTR_SCCLUN;
741#else
742			FCPARAM(isp)->isp_fwattr = 0;
743#endif
744		} else {
745			FCPARAM(isp)->isp_fwattr = mbs.param[6];
746			isp_prt(isp, ISP_LOGDEBUG0,
747			    "Firmware Attributes = 0x%x", mbs.param[6]);
748		}
749		if (ISP_READ(isp, BIU2100_CSR) & BIU2100_PCI64) {
750			isp_prt(isp, ISP_LOGCONFIG,
751			    "Installed in 64-Bit PCI slot");
752		}
753	}
754
755	if (isp->isp_romfw_rev[0] || isp->isp_romfw_rev[1] ||
756	    isp->isp_romfw_rev[2]) {
757		isp_prt(isp, ISP_LOGCONFIG, "Last F/W revision was %d.%d.%d",
758		    isp->isp_romfw_rev[0], isp->isp_romfw_rev[1],
759		    isp->isp_romfw_rev[2]);
760	}
761
762	mbs.param[0] = MBOX_GET_FIRMWARE_STATUS;
763	isp_mboxcmd(isp, &mbs, MBLOGALL);
764	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
765		return;
766	}
767	isp->isp_maxcmds = mbs.param[2];
768	isp_prt(isp, ISP_LOGINFO,
769	    "%d max I/O commands supported", mbs.param[2]);
770	isp_fw_state(isp);
771
772	/*
773	 * Set up DMA for the request and result mailboxes.
774	 */
775	if (ISP_MBOXDMASETUP(isp) != 0) {
776		isp_prt(isp, ISP_LOGERR, "Cannot setup DMA");
777		return;
778	}
779	isp->isp_state = ISP_RESETSTATE;
780
781	/*
782	 * Okay- now that we have new firmware running, we now (re)set our
783	 * notion of how many luns we support. This is somewhat tricky because
784	 * if we haven't loaded firmware, we sometimes do not have an easy way
785	 * of knowing how many luns we support.
786	 *
787	 * Expanded lun firmware gives you 32 luns for SCSI cards and
788	 * 16384 luns for Fibre Channel cards.
789	 *
790	 * It turns out that even for QLogic 2100s with ROM 1.10 and above
791	 * we do get a firmware attributes word returned in mailbox register 6.
792	 *
793	 * Because the lun is in a different position in the Request Queue
794	 * Entry structure for Fibre Channel with expanded lun firmware, we
795	 * can only support one lun (lun zero) when we don't know what kind
796	 * of firmware we're running.
797	 */
798	if (IS_SCSI(isp)) {
799		if (dodnld) {
800			if (IS_ULTRA2(isp) || IS_ULTRA3(isp)) {
801				isp->isp_maxluns = 32;
802			} else {
803				isp->isp_maxluns = 8;
804			}
805		} else {
806			isp->isp_maxluns = 8;
807		}
808	} else {
809		if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN) {
810			isp->isp_maxluns = 16384;
811		} else {
812			isp->isp_maxluns = 16;
813		}
814	}
815}
816
817/*
818 * Initialize Parameters of Hardware to a known state.
819 *
820 * Locks are held before coming here.
821 */
822
823void
824isp_init(struct ispsoftc *isp)
825{
826	/*
827	 * Must do this first to get defaults established.
828	 */
829	isp_setdfltparm(isp, 0);
830	if (IS_DUALBUS(isp)) {
831		isp_setdfltparm(isp, 1);
832	}
833	if (IS_FC(isp)) {
834		isp_fibre_init(isp);
835	} else {
836		isp_scsi_init(isp);
837	}
838}
839
840static void
841isp_scsi_init(struct ispsoftc *isp)
842{
843	sdparam *sdp_chan0, *sdp_chan1;
844	mbreg_t mbs;
845
846	sdp_chan0 = isp->isp_param;
847	sdp_chan1 = sdp_chan0;
848	if (IS_DUALBUS(isp)) {
849		sdp_chan1++;
850	}
851
852	/*
853	 * If we have no role (neither target nor initiator), return.
854	 */
855	if (isp->isp_role == ISP_ROLE_NONE) {
856		return;
857	}
858
859	/* First do overall per-card settings. */
860
861	/*
862	 * If we have fast memory timing enabled, turn it on.
863	 */
864	if (sdp_chan0->isp_fast_mttr) {
865		ISP_WRITE(isp, RISC_MTR, 0x1313);
866	}
867
868	/*
869	 * Set Retry Delay and Count.
870	 * You set both channels at the same time.
871	 */
872	mbs.param[0] = MBOX_SET_RETRY_COUNT;
873	mbs.param[1] = sdp_chan0->isp_retry_count;
874	mbs.param[2] = sdp_chan0->isp_retry_delay;
875	mbs.param[6] = sdp_chan1->isp_retry_count;
876	mbs.param[7] = sdp_chan1->isp_retry_delay;
877
878	isp_mboxcmd(isp, &mbs, MBLOGALL);
879	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
880		return;
881	}
882
883	/*
884	 * Set ASYNC DATA SETUP time. This is very important.
885	 */
886	mbs.param[0] = MBOX_SET_ASYNC_DATA_SETUP_TIME;
887	mbs.param[1] = sdp_chan0->isp_async_data_setup;
888	mbs.param[2] = sdp_chan1->isp_async_data_setup;
889	isp_mboxcmd(isp, &mbs, MBLOGALL);
890	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
891		return;
892	}
893
894	/*
895	 * Set ACTIVE Negation State.
896	 */
897	mbs.param[0] = MBOX_SET_ACT_NEG_STATE;
898	mbs.param[1] =
899	    (sdp_chan0->isp_req_ack_active_neg << 4) |
900	    (sdp_chan0->isp_data_line_active_neg << 5);
901	mbs.param[2] =
902	    (sdp_chan1->isp_req_ack_active_neg << 4) |
903	    (sdp_chan1->isp_data_line_active_neg << 5);
904
905	isp_mboxcmd(isp, &mbs, MBLOGNONE);
906	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
907		isp_prt(isp, ISP_LOGERR,
908		    "failed to set active negation state (%d,%d), (%d,%d)",
909		    sdp_chan0->isp_req_ack_active_neg,
910		    sdp_chan0->isp_data_line_active_neg,
911		    sdp_chan1->isp_req_ack_active_neg,
912		    sdp_chan1->isp_data_line_active_neg);
913		/*
914		 * But don't return.
915		 */
916	}
917
918	/*
919	 * Set the Tag Aging limit
920	 */
921	mbs.param[0] = MBOX_SET_TAG_AGE_LIMIT;
922	mbs.param[1] = sdp_chan0->isp_tag_aging;
923	mbs.param[2] = sdp_chan1->isp_tag_aging;
924	isp_mboxcmd(isp, &mbs, MBLOGALL);
925	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
926		isp_prt(isp, ISP_LOGERR, "failed to set tag age limit (%d,%d)",
927		    sdp_chan0->isp_tag_aging, sdp_chan1->isp_tag_aging);
928		return;
929	}
930
931	/*
932	 * Set selection timeout.
933	 */
934	mbs.param[0] = MBOX_SET_SELECT_TIMEOUT;
935	mbs.param[1] = sdp_chan0->isp_selection_timeout;
936	mbs.param[2] = sdp_chan1->isp_selection_timeout;
937	isp_mboxcmd(isp, &mbs, MBLOGALL);
938	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
939		return;
940	}
941
942	/* now do per-channel settings */
943	isp_scsi_channel_init(isp, 0);
944	if (IS_DUALBUS(isp))
945		isp_scsi_channel_init(isp, 1);
946
947	/*
948	 * Now enable request/response queues
949	 */
950
951	if (IS_ULTRA2(isp) || IS_1240(isp)) {
952		mbs.param[0] = MBOX_INIT_RES_QUEUE_A64;
953		mbs.param[1] = RESULT_QUEUE_LEN(isp);
954		mbs.param[2] = DMA_WD1(isp->isp_result_dma);
955		mbs.param[3] = DMA_WD0(isp->isp_result_dma);
956		mbs.param[4] = 0;
957		mbs.param[6] = DMA_WD3(isp->isp_result_dma);
958		mbs.param[7] = DMA_WD2(isp->isp_result_dma);
959		isp_mboxcmd(isp, &mbs, MBLOGALL);
960		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
961			return;
962		}
963		isp->isp_residx = mbs.param[5];
964
965		mbs.param[0] = MBOX_INIT_REQ_QUEUE_A64;
966		mbs.param[1] = RQUEST_QUEUE_LEN(isp);
967		mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
968		mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
969		mbs.param[5] = 0;
970		mbs.param[6] = DMA_WD3(isp->isp_result_dma);
971		mbs.param[7] = DMA_WD2(isp->isp_result_dma);
972		isp_mboxcmd(isp, &mbs, MBLOGALL);
973		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
974			return;
975		}
976		isp->isp_reqidx = isp->isp_reqodx = mbs.param[4];
977	} else {
978		mbs.param[0] = MBOX_INIT_RES_QUEUE;
979		mbs.param[1] = RESULT_QUEUE_LEN(isp);
980		mbs.param[2] = DMA_WD1(isp->isp_result_dma);
981		mbs.param[3] = DMA_WD0(isp->isp_result_dma);
982		mbs.param[4] = 0;
983		isp_mboxcmd(isp, &mbs, MBLOGALL);
984		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
985			return;
986		}
987		isp->isp_residx = mbs.param[5];
988
989		mbs.param[0] = MBOX_INIT_REQ_QUEUE;
990		mbs.param[1] = RQUEST_QUEUE_LEN(isp);
991		mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
992		mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
993		mbs.param[5] = 0;
994		isp_mboxcmd(isp, &mbs, MBLOGALL);
995		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
996			return;
997		}
998		isp->isp_reqidx = isp->isp_reqodx = mbs.param[4];
999	}
1000
1001	/*
1002	 * Turn on Fast Posting, LVD transitions
1003	 *
1004	 * Ultra2 F/W always has had fast posting (and LVD transitions)
1005	 *
1006	 * Ultra and older (i.e., SBus) cards may not. It's just safer
1007	 * to assume not for them.
1008	 */
1009
1010	mbs.param[0] = MBOX_SET_FW_FEATURES;
1011	mbs.param[1] = 0;
1012	if (IS_ULTRA2(isp))
1013		mbs.param[1] |= FW_FEATURE_LVD_NOTIFY;
1014#ifndef	ISP_NO_RIO
1015	if (IS_ULTRA2(isp) || IS_1240(isp))
1016		mbs.param[1] |= FW_FEATURE_RIO_16BIT;
1017#else
1018#ifndef	ISP_NO_FASTPOST
1019	if (IS_ULTRA2(isp) || IS_1240(isp))
1020		mbs.param[1] |= FW_FEATURE_FAST_POST;
1021#endif
1022#endif
1023	if (mbs.param[1] != 0) {
1024		u_int16_t sfeat = mbs.param[1];
1025		isp_mboxcmd(isp, &mbs, MBLOGALL);
1026		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1027			isp_prt(isp, ISP_LOGINFO,
1028			    "Enabled FW features (0x%x)", sfeat);
1029		}
1030	}
1031
1032	/*
1033	 * Let the outer layers decide whether to issue a SCSI bus reset.
1034	 */
1035	isp->isp_state = ISP_INITSTATE;
1036}
1037
1038static void
1039isp_scsi_channel_init(struct ispsoftc *isp, int channel)
1040{
1041	sdparam *sdp;
1042	mbreg_t mbs;
1043	int tgt;
1044
1045	sdp = isp->isp_param;
1046	sdp += channel;
1047
1048	/*
1049	 * Set (possibly new) Initiator ID.
1050	 */
1051	mbs.param[0] = MBOX_SET_INIT_SCSI_ID;
1052	mbs.param[1] = (channel << 7) | sdp->isp_initiator_id;
1053	isp_mboxcmd(isp, &mbs, MBLOGALL);
1054	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1055		return;
1056	}
1057	isp_prt(isp, ISP_LOGINFO, "Initiator ID is %d on Channel %d",
1058	    sdp->isp_initiator_id, channel);
1059
1060
1061	/*
1062	 * Set current per-target parameters to an initial safe minimum.
1063	 */
1064	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
1065		int lun;
1066		u_int16_t sdf;
1067
1068		if (sdp->isp_devparam[tgt].dev_enable == 0) {
1069			continue;
1070		}
1071#ifndef	ISP_TARGET_MODE
1072		sdf = sdp->isp_devparam[tgt].goal_flags;
1073		sdf &= DPARM_SAFE_DFLT;
1074		/*
1075		 * It is not quite clear when this changed over so that
1076		 * we could force narrow and async for 1000/1020 cards,
1077		 * but assume that this is only the case for loaded
1078		 * firmware.
1079		 */
1080		if (isp->isp_loaded_fw) {
1081			sdf |= DPARM_NARROW | DPARM_ASYNC;
1082		}
1083#else
1084		/*
1085		 * The !$*!)$!$)* f/w uses the same index into some
1086		 * internal table to decide how to respond to negotiations,
1087		 * so if we've said "let's be safe" for ID X, and ID X
1088		 * selects *us*, the negotiations will back to 'safe'
1089		 * (as in narrow/async). What the f/w *should* do is
1090		 * use the initiator id settings to decide how to respond.
1091		 */
1092		sdp->isp_devparam[tgt].goal_flags = sdf = DPARM_DEFAULT;
1093#endif
1094		mbs.param[0] = MBOX_SET_TARGET_PARAMS;
1095		mbs.param[1] = (channel << 15) | (tgt << 8);
1096		mbs.param[2] = sdf;
1097		if ((sdf & DPARM_SYNC) == 0) {
1098			mbs.param[3] = 0;
1099		} else {
1100			mbs.param[3] =
1101			    (sdp->isp_devparam[tgt].goal_offset << 8) |
1102			    (sdp->isp_devparam[tgt].goal_period);
1103		}
1104		isp_prt(isp, ISP_LOGDEBUG0,
1105		    "Initial Settings bus%d tgt%d flags 0x%x off 0x%x per 0x%x",
1106		    channel, tgt, mbs.param[2], mbs.param[3] >> 8,
1107		    mbs.param[3] & 0xff);
1108		isp_mboxcmd(isp, &mbs, MBLOGNONE);
1109		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1110			sdf = DPARM_SAFE_DFLT;
1111			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
1112			mbs.param[1] = (tgt << 8) | (channel << 15);
1113			mbs.param[2] = sdf;
1114			mbs.param[3] = 0;
1115			isp_mboxcmd(isp, &mbs, MBLOGALL);
1116			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1117				continue;
1118			}
1119		}
1120
1121		/*
1122		 * We don't update any information directly from the f/w
1123		 * because we need to run at least one command to cause a
1124		 * new state to be latched up. So, we just assume that we
1125		 * converge to the values we just had set.
1126		 *
1127		 * Ensure that we don't believe tagged queuing is enabled yet.
1128		 * It turns out that sometimes the ISP just ignores our
1129		 * attempts to set parameters for devices that it hasn't
1130		 * seen yet.
1131		 */
1132		sdp->isp_devparam[tgt].actv_flags = sdf & ~DPARM_TQING;
1133		for (lun = 0; lun < (int) isp->isp_maxluns; lun++) {
1134			mbs.param[0] = MBOX_SET_DEV_QUEUE_PARAMS;
1135			mbs.param[1] = (channel << 15) | (tgt << 8) | lun;
1136			mbs.param[2] = sdp->isp_max_queue_depth;
1137			mbs.param[3] = sdp->isp_devparam[tgt].exc_throttle;
1138			isp_mboxcmd(isp, &mbs, MBLOGALL);
1139			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1140				break;
1141			}
1142		}
1143	}
1144	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
1145		if (sdp->isp_devparam[tgt].dev_refresh) {
1146			isp->isp_sendmarker |= (1 << channel);
1147			isp->isp_update |= (1 << channel);
1148			break;
1149		}
1150	}
1151}
1152
1153/*
1154 * Fibre Channel specific initialization.
1155 *
1156 * Locks are held before coming here.
1157 */
1158static void
1159isp_fibre_init(struct ispsoftc *isp)
1160{
1161	fcparam *fcp;
1162	isp_icb_t local, *icbp = &local;
1163	mbreg_t mbs;
1164	int loopid;
1165	u_int64_t nwwn, pwwn;
1166
1167	fcp = isp->isp_param;
1168
1169	/*
1170	 * Do this *before* initializing the firmware.
1171	 */
1172	isp_mark_getpdb_all(isp);
1173	fcp->isp_fwstate = FW_CONFIG_WAIT;
1174	fcp->isp_loopstate = LOOP_NIL;
1175
1176	/*
1177	 * If we have no role (neither target nor initiator), return.
1178	 */
1179	if (isp->isp_role == ISP_ROLE_NONE) {
1180		return;
1181	}
1182
1183	loopid = fcp->isp_loopid;
1184	MEMZERO(icbp, sizeof (*icbp));
1185	icbp->icb_version = ICB_VERSION1;
1186
1187	/*
1188	 * Firmware Options are either retrieved from NVRAM or
1189	 * are patched elsewhere. We check them for sanity here
1190	 * and make changes based on board revision, but otherwise
1191	 * let others decide policy.
1192	 */
1193
1194	/*
1195	 * If this is a 2100 < revision 5, we have to turn off FAIRNESS.
1196	 */
1197	if ((isp->isp_type == ISP_HA_FC_2100) && isp->isp_revision < 5) {
1198		fcp->isp_fwoptions &= ~ICBOPT_FAIRNESS;
1199	}
1200
1201	/*
1202	 * We have to use FULL LOGIN even though it resets the loop too much
1203	 * because otherwise port database entries don't get updated after
1204	 * a LIP- this is a known f/w bug for 2100 f/w less than 1.17.0.
1205	 */
1206	if (!ISP_FW_NEWER_THAN(isp, 1, 17, 0)) {
1207		fcp->isp_fwoptions |= ICBOPT_FULL_LOGIN;
1208	}
1209
1210	/*
1211	 * Insist on Port Database Update Async notifications
1212	 */
1213	fcp->isp_fwoptions |= ICBOPT_PDBCHANGE_AE;
1214
1215	/*
1216	 * Make sure that target role reflects into fwoptions.
1217	 */
1218	if (isp->isp_role & ISP_ROLE_TARGET) {
1219		fcp->isp_fwoptions |= ICBOPT_TGT_ENABLE;
1220	} else {
1221		fcp->isp_fwoptions &= ~ICBOPT_TGT_ENABLE;
1222	}
1223
1224	/*
1225	 * Propagate all of this into the ICB structure.
1226	 */
1227	icbp->icb_fwoptions = fcp->isp_fwoptions;
1228	icbp->icb_maxfrmlen = fcp->isp_maxfrmlen;
1229	if (icbp->icb_maxfrmlen < ICB_MIN_FRMLEN ||
1230	    icbp->icb_maxfrmlen > ICB_MAX_FRMLEN) {
1231		isp_prt(isp, ISP_LOGERR,
1232		    "bad frame length (%d) from NVRAM- using %d",
1233		    fcp->isp_maxfrmlen, ICB_DFLT_FRMLEN);
1234		icbp->icb_maxfrmlen = ICB_DFLT_FRMLEN;
1235	}
1236	icbp->icb_maxalloc = fcp->isp_maxalloc;
1237	if (icbp->icb_maxalloc < 1) {
1238		isp_prt(isp, ISP_LOGERR,
1239		    "bad maximum allocation (%d)- using 16", fcp->isp_maxalloc);
1240		icbp->icb_maxalloc = 16;
1241	}
1242	icbp->icb_execthrottle = fcp->isp_execthrottle;
1243	if (icbp->icb_execthrottle < 1) {
1244		isp_prt(isp, ISP_LOGERR,
1245		    "bad execution throttle of %d- using 16",
1246		    fcp->isp_execthrottle);
1247		icbp->icb_execthrottle = ICB_DFLT_THROTTLE;
1248	}
1249	icbp->icb_retry_delay = fcp->isp_retry_delay;
1250	icbp->icb_retry_count = fcp->isp_retry_count;
1251	icbp->icb_hardaddr = loopid;
1252	/*
1253	 * Right now we just set extended options to prefer point-to-point
1254	 * over loop based upon some soft config options.
1255	 *
1256	 * NB: for the 2300, ICBOPT_EXTENDED is required.
1257	 */
1258	if (IS_2200(isp) || IS_23XX(isp)) {
1259		icbp->icb_fwoptions |= ICBOPT_EXTENDED;
1260		/*
1261		 * Prefer or force Point-To-Point instead Loop?
1262		 */
1263		switch(isp->isp_confopts & ISP_CFG_PORT_PREF) {
1264		case ISP_CFG_NPORT:
1265			icbp->icb_xfwoptions |= ICBXOPT_PTP_2_LOOP;
1266			break;
1267		case ISP_CFG_NPORT_ONLY:
1268			icbp->icb_xfwoptions |= ICBXOPT_PTP_ONLY;
1269			break;
1270		case ISP_CFG_LPORT_ONLY:
1271			icbp->icb_xfwoptions |= ICBXOPT_LOOP_ONLY;
1272			break;
1273		default:
1274			icbp->icb_xfwoptions |= ICBXOPT_LOOP_2_PTP;
1275			break;
1276		}
1277		if (IS_23XX(isp)) {
1278			/*
1279			 * QLogic recommends that FAST Posting be turned
1280			 * off for 23XX cards and instead allow the HBA
1281			 * to write response queue entries and interrupt
1282			 * after a delay (ZIO).
1283			 *
1284			 * If we set ZIO, it will disable fast posting,
1285			 * so we don't need to clear it in fwoptions.
1286			 */
1287#ifndef	ISP_NO_ZIO
1288			icbp->icb_xfwoptions |= ICBXOPT_ZIO;
1289#else
1290			icbp->icb_fwoptions |= ICBOPT_FAST_POST;
1291#endif
1292#if	0
1293			/*
1294			 * Values, in 100us increments. The default
1295			 * is 2 (200us) if a value 0 (default) is
1296			 * selected.
1297			 */
1298			icbp->icb_idelaytimer = 2;
1299#endif
1300
1301			if (isp->isp_confopts & ISP_CFG_ONEGB) {
1302				icbp->icb_zfwoptions |= ICBZOPT_RATE_ONEGB;
1303			} else if (isp->isp_confopts & ISP_CFG_TWOGB) {
1304				icbp->icb_zfwoptions |= ICBZOPT_RATE_TWOGB;
1305			} else {
1306				icbp->icb_zfwoptions |= ICBZOPT_RATE_AUTO;
1307			}
1308		}
1309	}
1310
1311#ifndef	ISP_NO_RIO_FC
1312	/*
1313	 * RIO seems to be enabled in 2100s for fw >= 1.17.0.
1314	 *
1315	 * I've had some questionable problems with RIO on 2200.
1316	 * More specifically, on a 2204 I had problems with RIO
1317	 * on a Linux system where I was dropping commands right
1318	 * and left. It's not clear to me what the actual problem
1319	 * was.
1320	 *
1321	 * 23XX Cards do not support RIO. Instead they support ZIO.
1322	 */
1323#if	0
1324	if (!IS_23XX(isp) && ISP_FW_NEWER_THAN(isp, 1, 17, 0)) {
1325		icbp->icb_xfwoptions |= ICBXOPT_RIO_16BIT;
1326		icbp->icb_racctimer = 4;
1327		icbp->icb_idelaytimer = 8;
1328	}
1329#endif
1330#endif
1331
1332	/*
1333	 * For 22XX > 2.1.26 && 23XX, set someoptions.
1334	 * XXX: Probably okay for newer 2100 f/w too.
1335	 */
1336	if (ISP_FW_NEWER_THAN(isp, 2, 26, 0)) {
1337		/*
1338		 * Turn on LIP F8 async event (1)
1339		 * Turn on generate AE 8013 on all LIP Resets (2)
1340		 * Disable LIP F7 switching (8)
1341		 */
1342		mbs.param[0] = MBOX_SET_FIRMWARE_OPTIONS;
1343		mbs.param[1] = 0xb;
1344		mbs.param[2] = 0;
1345		mbs.param[3] = 0;
1346		isp_mboxcmd(isp, &mbs, MBLOGALL);
1347	}
1348	icbp->icb_logintime = 30;	/* 30 second login timeout */
1349
1350	if (IS_23XX(isp)) {
1351		ISP_WRITE(isp, isp->isp_rqstinrp, 0);
1352        	ISP_WRITE(isp, isp->isp_rqstoutrp, 0);
1353        	ISP_WRITE(isp, isp->isp_respinrp, 0);
1354		ISP_WRITE(isp, isp->isp_respoutrp, 0);
1355	}
1356
1357	nwwn = ISP_NODEWWN(isp);
1358	pwwn = ISP_PORTWWN(isp);
1359	if (nwwn && pwwn) {
1360		icbp->icb_fwoptions |= ICBOPT_BOTH_WWNS;
1361		MAKE_NODE_NAME_FROM_WWN(icbp->icb_nodename, nwwn);
1362		MAKE_NODE_NAME_FROM_WWN(icbp->icb_portname, pwwn);
1363		isp_prt(isp, ISP_LOGDEBUG1,
1364		    "Setting ICB Node 0x%08x%08x Port 0x%08x%08x",
1365		    ((u_int32_t) (nwwn >> 32)),
1366		    ((u_int32_t) (nwwn & 0xffffffff)),
1367		    ((u_int32_t) (pwwn >> 32)),
1368		    ((u_int32_t) (pwwn & 0xffffffff)));
1369	} else {
1370		isp_prt(isp, ISP_LOGDEBUG1, "Not using any WWNs");
1371		icbp->icb_fwoptions &= ~(ICBOPT_BOTH_WWNS|ICBOPT_FULL_LOGIN);
1372	}
1373	icbp->icb_rqstqlen = RQUEST_QUEUE_LEN(isp);
1374	icbp->icb_rsltqlen = RESULT_QUEUE_LEN(isp);
1375	icbp->icb_rqstaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_rquest_dma);
1376	icbp->icb_rqstaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_rquest_dma);
1377	icbp->icb_rqstaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_rquest_dma);
1378	icbp->icb_rqstaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_rquest_dma);
1379	icbp->icb_respaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_result_dma);
1380	icbp->icb_respaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_result_dma);
1381	icbp->icb_respaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_result_dma);
1382	icbp->icb_respaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_result_dma);
1383	isp_prt(isp, ISP_LOGDEBUG0,
1384	    "isp_fibre_init: fwopt 0x%x xfwopt 0x%x zfwopt 0x%x",
1385	    icbp->icb_fwoptions, icbp->icb_xfwoptions, icbp->icb_zfwoptions);
1386
1387	FC_SCRATCH_ACQUIRE(isp);
1388	isp_put_icb(isp, icbp, (isp_icb_t *)fcp->isp_scratch);
1389
1390	/*
1391	 * Init the firmware
1392	 */
1393	mbs.param[0] = MBOX_INIT_FIRMWARE;
1394	mbs.param[1] = 0;
1395	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
1396	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
1397	mbs.param[4] = 0;
1398	mbs.param[5] = 0;
1399	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
1400	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
1401	isp_mboxcmd(isp, &mbs, MBLOGALL);
1402	FC_SCRATCH_RELEASE(isp);
1403	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1404		return;
1405	}
1406	isp->isp_reqidx = isp->isp_reqodx = 0;
1407	isp->isp_residx = 0;
1408	isp->isp_sendmarker = 1;
1409
1410	/*
1411	 * Whatever happens, we're now committed to being here.
1412	 */
1413	isp->isp_state = ISP_INITSTATE;
1414}
1415
1416/*
1417 * Fibre Channel Support- get the port database for the id.
1418 *
1419 * Locks are held before coming here. Return 0 if success,
1420 * else failure.
1421 */
1422
1423static int
1424isp_getmap(struct ispsoftc *isp, fcpos_map_t *map)
1425{
1426	fcparam *fcp = (fcparam *) isp->isp_param;
1427	mbreg_t mbs;
1428
1429	mbs.param[0] = MBOX_GET_FC_AL_POSITION_MAP;
1430	mbs.param[1] = 0;
1431	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
1432	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
1433	/*
1434	 * Unneeded. For the 2100, except for initializing f/w, registers
1435	 * 4/5 have to not be written to.
1436	 *	mbs.param[4] = 0;
1437	 *	mbs.param[5] = 0;
1438	 *
1439	 */
1440	mbs.param[6] = 0;
1441	mbs.param[7] = 0;
1442	FC_SCRATCH_ACQUIRE(isp);
1443	isp_mboxcmd(isp, &mbs, MBLOGALL & ~MBOX_COMMAND_PARAM_ERROR);
1444	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1445		MEMCPY(map, fcp->isp_scratch, sizeof (fcpos_map_t));
1446		map->fwmap = mbs.param[1] != 0;
1447		FC_SCRATCH_RELEASE(isp);
1448		return (0);
1449	}
1450	FC_SCRATCH_RELEASE(isp);
1451	return (-1);
1452}
1453
1454static void
1455isp_mark_getpdb_all(struct ispsoftc *isp)
1456{
1457	fcparam *fcp = (fcparam *) isp->isp_param;
1458	int i;
1459	for (i = 0; i < MAX_FC_TARG; i++) {
1460		fcp->portdb[i].valid = fcp->portdb[i].fabric_dev = 0;
1461	}
1462}
1463
1464static int
1465isp_getpdb(struct ispsoftc *isp, int id, isp_pdb_t *pdbp)
1466{
1467	fcparam *fcp = (fcparam *) isp->isp_param;
1468	mbreg_t mbs;
1469
1470	mbs.param[0] = MBOX_GET_PORT_DB;
1471	mbs.param[1] = id << 8;
1472	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
1473	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
1474	/*
1475	 * Unneeded. For the 2100, except for initializing f/w, registers
1476	 * 4/5 have to not be written to.
1477	 *	mbs.param[4] = 0;
1478	 *	mbs.param[5] = 0;
1479	 *
1480	 */
1481	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
1482	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
1483	FC_SCRATCH_ACQUIRE(isp);
1484	isp_mboxcmd(isp, &mbs, MBLOGALL & ~MBOX_COMMAND_PARAM_ERROR);
1485	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1486		isp_get_pdb(isp, (isp_pdb_t *)fcp->isp_scratch, pdbp);
1487		FC_SCRATCH_RELEASE(isp);
1488		return (0);
1489	}
1490	FC_SCRATCH_RELEASE(isp);
1491	return (-1);
1492}
1493
1494static u_int64_t
1495isp_get_portname(struct ispsoftc *isp, int loopid, int nodename)
1496{
1497	u_int64_t wwn = 0;
1498	mbreg_t mbs;
1499
1500	mbs.param[0] = MBOX_GET_PORT_NAME;
1501	mbs.param[1] = loopid << 8;
1502	if (nodename)
1503		mbs.param[1] |= 1;
1504	isp_mboxcmd(isp, &mbs, MBLOGALL & ~MBOX_COMMAND_PARAM_ERROR);
1505	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1506		wwn =
1507		    (((u_int64_t)(mbs.param[2] & 0xff)) << 56) |
1508		    (((u_int64_t)(mbs.param[2] >> 8))	<< 48) |
1509		    (((u_int64_t)(mbs.param[3] & 0xff))	<< 40) |
1510		    (((u_int64_t)(mbs.param[3] >> 8))	<< 32) |
1511		    (((u_int64_t)(mbs.param[6] & 0xff))	<< 24) |
1512		    (((u_int64_t)(mbs.param[6] >> 8))	<< 16) |
1513		    (((u_int64_t)(mbs.param[7] & 0xff))	<<  8) |
1514		    (((u_int64_t)(mbs.param[7] >> 8)));
1515	}
1516	return (wwn);
1517}
1518
1519/*
1520 * Make sure we have good FC link and know our Loop ID.
1521 */
1522
1523static int
1524isp_fclink_test(struct ispsoftc *isp, int usdelay)
1525{
1526	static char *toponames[] = {
1527		"Private Loop",
1528		"FL Port",
1529		"N-Port to N-Port",
1530		"F Port",
1531		"F Port (no FLOGI_ACC response)"
1532	};
1533	mbreg_t mbs;
1534	int count, check_for_fabric;
1535	u_int8_t lwfs;
1536	fcparam *fcp;
1537	struct lportdb *lp;
1538	isp_pdb_t pdb;
1539
1540	fcp = isp->isp_param;
1541
1542	/*
1543	 * XXX: Here is where we would start a 'loop dead' timeout
1544	 */
1545
1546	/*
1547	 * Wait up to N microseconds for F/W to go to a ready state.
1548	 */
1549	lwfs = FW_CONFIG_WAIT;
1550	count = 0;
1551	while (count < usdelay) {
1552		u_int64_t enano;
1553		u_int32_t wrk;
1554		NANOTIME_T hra, hrb;
1555
1556		GET_NANOTIME(&hra);
1557		isp_fw_state(isp);
1558		if (lwfs != fcp->isp_fwstate) {
1559			isp_prt(isp, ISP_LOGINFO, "Firmware State <%s->%s>",
1560			    isp2100_fw_statename((int)lwfs),
1561			    isp2100_fw_statename((int)fcp->isp_fwstate));
1562			lwfs = fcp->isp_fwstate;
1563		}
1564		if (fcp->isp_fwstate == FW_READY) {
1565			break;
1566		}
1567		GET_NANOTIME(&hrb);
1568
1569		/*
1570		 * Get the elapsed time in nanoseconds.
1571		 * Always guaranteed to be non-zero.
1572		 */
1573		enano = NANOTIME_SUB(&hrb, &hra);
1574
1575		isp_prt(isp, ISP_LOGDEBUG1,
1576		    "usec%d: 0x%lx->0x%lx enano 0x%x%08x",
1577		    count, (long) GET_NANOSEC(&hra), (long) GET_NANOSEC(&hrb),
1578		    (u_int32_t)(enano >> 32), (u_int32_t)(enano & 0xffffffff));
1579
1580		/*
1581		 * If the elapsed time is less than 1 millisecond,
1582		 * delay a period of time up to that millisecond of
1583		 * waiting.
1584		 *
1585		 * This peculiar code is an attempt to try and avoid
1586		 * invoking u_int64_t math support functions for some
1587		 * platforms where linkage is a problem.
1588		 */
1589		if (enano < (1000 * 1000)) {
1590			count += 1000;
1591			enano = (1000 * 1000) - enano;
1592			while (enano > (u_int64_t) 4000000000U) {
1593				USEC_SLEEP(isp, 4000000);
1594				enano -= (u_int64_t) 4000000000U;
1595			}
1596			wrk = enano;
1597			wrk /= 1000;
1598			USEC_SLEEP(isp, wrk);
1599		} else {
1600			while (enano > (u_int64_t) 4000000000U) {
1601				count += 4000000;
1602				enano -= (u_int64_t) 4000000000U;
1603			}
1604			wrk = enano;
1605			count += (wrk / 1000);
1606		}
1607	}
1608
1609	/*
1610	 * If we haven't gone to 'ready' state, return.
1611	 */
1612	if (fcp->isp_fwstate != FW_READY) {
1613		return (-1);
1614	}
1615
1616	/*
1617	 * Get our Loop ID (if possible). We really need to have it.
1618	 */
1619	mbs.param[0] = MBOX_GET_LOOP_ID;
1620	isp_mboxcmd(isp, &mbs, MBLOGALL);
1621	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1622		return (-1);
1623	}
1624	fcp->isp_loopid = mbs.param[1];
1625	if (IS_2200(isp) || IS_23XX(isp)) {
1626		int topo = (int) mbs.param[6];
1627		if (topo < TOPO_NL_PORT || topo > TOPO_PTP_STUB)
1628			topo = TOPO_PTP_STUB;
1629		fcp->isp_topo = topo;
1630	} else {
1631		fcp->isp_topo = TOPO_NL_PORT;
1632	}
1633	fcp->isp_portid = fcp->isp_alpa = mbs.param[2] & 0xff;
1634
1635	/*
1636	 * Check to see if we're on a fabric by trying to see if we
1637	 * can talk to the fabric name server. This can be a bit
1638	 * tricky because if we're a 2100, we should check always
1639	 * (in case we're connected to a server doing aliasing).
1640	 */
1641	fcp->isp_onfabric = 0;
1642
1643	if (IS_2100(isp)) {
1644		/*
1645		 * Don't bother with fabric if we are using really old
1646		 * 2100 firmware. It's just not worth it.
1647		 */
1648		if (ISP_FW_NEWER_THAN(isp, 1, 15, 37)) {
1649			check_for_fabric = 1;
1650		} else {
1651			check_for_fabric = 0;
1652		}
1653	} else if (fcp->isp_topo == TOPO_FL_PORT ||
1654	    fcp->isp_topo == TOPO_F_PORT) {
1655		check_for_fabric = 1;
1656	} else
1657		check_for_fabric = 0;
1658
1659	if (check_for_fabric && isp_getpdb(isp, FL_PORT_ID, &pdb) == 0) {
1660		int loopid = FL_PORT_ID;
1661		if (IS_2100(isp)) {
1662			fcp->isp_topo = TOPO_FL_PORT;
1663		}
1664
1665		if (BITS2WORD(pdb.pdb_portid_bits) == 0) {
1666			/*
1667			 * Crock.
1668			 */
1669			fcp->isp_topo = TOPO_NL_PORT;
1670			goto not_on_fabric;
1671		}
1672		fcp->isp_portid = mbs.param[2] | ((int) mbs.param[3] << 16);
1673
1674		/*
1675		 * Save the Fabric controller's port database entry.
1676		 */
1677		lp = &fcp->portdb[loopid];
1678		lp->node_wwn =
1679		    (((u_int64_t)pdb.pdb_nodename[0]) << 56) |
1680		    (((u_int64_t)pdb.pdb_nodename[1]) << 48) |
1681		    (((u_int64_t)pdb.pdb_nodename[2]) << 40) |
1682		    (((u_int64_t)pdb.pdb_nodename[3]) << 32) |
1683		    (((u_int64_t)pdb.pdb_nodename[4]) << 24) |
1684		    (((u_int64_t)pdb.pdb_nodename[5]) << 16) |
1685		    (((u_int64_t)pdb.pdb_nodename[6]) <<  8) |
1686		    (((u_int64_t)pdb.pdb_nodename[7]));
1687		lp->port_wwn =
1688		    (((u_int64_t)pdb.pdb_portname[0]) << 56) |
1689		    (((u_int64_t)pdb.pdb_portname[1]) << 48) |
1690		    (((u_int64_t)pdb.pdb_portname[2]) << 40) |
1691		    (((u_int64_t)pdb.pdb_portname[3]) << 32) |
1692		    (((u_int64_t)pdb.pdb_portname[4]) << 24) |
1693		    (((u_int64_t)pdb.pdb_portname[5]) << 16) |
1694		    (((u_int64_t)pdb.pdb_portname[6]) <<  8) |
1695		    (((u_int64_t)pdb.pdb_portname[7]));
1696		lp->roles =
1697		    (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
1698		lp->portid = BITS2WORD(pdb.pdb_portid_bits);
1699		lp->loopid = pdb.pdb_loopid;
1700		lp->loggedin = lp->valid = 1;
1701		fcp->isp_onfabric = 1;
1702		(void) isp_async(isp, ISPASYNC_PROMENADE, &loopid);
1703		isp_register_fc4_type(isp);
1704	} else {
1705not_on_fabric:
1706		fcp->isp_onfabric = 0;
1707		fcp->portdb[FL_PORT_ID].valid = 0;
1708	}
1709
1710	fcp->isp_gbspeed = 1;
1711	if (IS_23XX(isp)) {
1712		mbs.param[0] = MBOX_GET_SET_DATA_RATE;
1713		mbs.param[1] = MBGSD_GET_RATE;
1714		/* mbs.param[2] undefined if we're just getting rate */
1715		isp_mboxcmd(isp, &mbs, MBLOGALL);
1716		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1717			if (mbs.param[1] == MBGSD_TWOGB) {
1718				isp_prt(isp, ISP_LOGINFO, "2Gb link speed/s");
1719				fcp->isp_gbspeed = 2;
1720			}
1721		}
1722	}
1723
1724	isp_prt(isp, ISP_LOGCONFIG, topology, fcp->isp_loopid, fcp->isp_alpa,
1725	    fcp->isp_portid, fcp->isp_loopstate, toponames[fcp->isp_topo]);
1726
1727	/*
1728	 * Announce ourselves, too. This involves synthesizing an entry.
1729	 */
1730	if (fcp->isp_iid_set == 0) {
1731		fcp->isp_iid_set = 1;
1732		fcp->isp_iid = fcp->isp_loopid;
1733		lp = &fcp->portdb[fcp->isp_iid];
1734	} else {
1735		lp = &fcp->portdb[fcp->isp_iid];
1736		if (fcp->isp_portid != lp->portid ||
1737		    fcp->isp_loopid != lp->loopid ||
1738		    fcp->isp_nodewwn != ISP_NODEWWN(isp) ||
1739		    fcp->isp_portwwn != ISP_PORTWWN(isp)) {
1740			lp->valid = 0;
1741			count = fcp->isp_iid;
1742			(void) isp_async(isp, ISPASYNC_PROMENADE, &count);
1743		}
1744	}
1745	lp->loopid = fcp->isp_loopid;
1746	lp->portid = fcp->isp_portid;
1747	lp->node_wwn = ISP_NODEWWN(isp);
1748	lp->port_wwn = ISP_PORTWWN(isp);
1749	switch (isp->isp_role) {
1750	case ISP_ROLE_NONE:
1751		lp->roles = 0;
1752		break;
1753	case ISP_ROLE_TARGET:
1754		lp->roles = SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT;
1755		break;
1756	case ISP_ROLE_INITIATOR:
1757		lp->roles = SVC3_INI_ROLE >> SVC3_ROLE_SHIFT;
1758		break;
1759	case ISP_ROLE_BOTH:
1760		lp->roles = (SVC3_INI_ROLE|SVC3_TGT_ROLE) >> SVC3_ROLE_SHIFT;
1761		break;
1762	}
1763	lp->loggedin = lp->valid = 1;
1764	count = fcp->isp_iid;
1765	(void) isp_async(isp, ISPASYNC_PROMENADE, &count);
1766	return (0);
1767}
1768
1769static char *
1770isp2100_fw_statename(int state)
1771{
1772	switch(state) {
1773	case FW_CONFIG_WAIT:	return "Config Wait";
1774	case FW_WAIT_AL_PA:	return "Waiting for AL_PA";
1775	case FW_WAIT_LOGIN:	return "Wait Login";
1776	case FW_READY:		return "Ready";
1777	case FW_LOSS_OF_SYNC:	return "Loss Of Sync";
1778	case FW_ERROR:		return "Error";
1779	case FW_REINIT:		return "Re-Init";
1780	case FW_NON_PART:	return "Nonparticipating";
1781	default:		return "?????";
1782	}
1783}
1784
1785/*
1786 * Synchronize our soft copy of the port database with what the f/w thinks
1787 * (with a view toward possibly for a specific target....)
1788 */
1789
1790static int
1791isp_pdb_sync(struct ispsoftc *isp)
1792{
1793	struct lportdb *lp;
1794	fcparam *fcp = isp->isp_param;
1795	isp_pdb_t pdb;
1796	int loopid, base, lim;
1797
1798	/*
1799	 * Make sure we're okay for doing this right now.
1800	 */
1801	if (fcp->isp_loopstate != LOOP_PDB_RCVD &&
1802	    fcp->isp_loopstate != LOOP_FSCAN_DONE &&
1803	    fcp->isp_loopstate != LOOP_LSCAN_DONE) {
1804		return (-1);
1805	}
1806
1807	if (fcp->isp_topo == TOPO_FL_PORT || fcp->isp_topo == TOPO_NL_PORT ||
1808	    fcp->isp_topo == TOPO_N_PORT) {
1809		if (fcp->isp_loopstate < LOOP_LSCAN_DONE) {
1810			if (isp_scan_loop(isp) != 0) {
1811				return (-1);
1812			}
1813		}
1814	}
1815	fcp->isp_loopstate = LOOP_SYNCING_PDB;
1816
1817	/*
1818	 * If we get this far, we've settled our differences with the f/w
1819	 * (for local loop device) and we can say that the loop state is ready.
1820	 */
1821
1822	if (fcp->isp_topo == TOPO_NL_PORT) {
1823		fcp->loop_seen_once = 1;
1824		fcp->isp_loopstate = LOOP_READY;
1825		return (0);
1826	}
1827
1828	/*
1829	 * Find all Fabric Entities that didn't make it from one scan to the
1830	 * next and let the world know they went away. Scan the whole database.
1831	 */
1832	for (lp = &fcp->portdb[0]; lp < &fcp->portdb[MAX_FC_TARG]; lp++) {
1833		if (lp->was_fabric_dev && lp->fabric_dev == 0) {
1834			loopid = lp - fcp->portdb;
1835			lp->valid = 0;	/* should already be set */
1836			(void) isp_async(isp, ISPASYNC_PROMENADE, &loopid);
1837			MEMZERO((void *) lp, sizeof (*lp));
1838			continue;
1839		}
1840		lp->was_fabric_dev = lp->fabric_dev;
1841	}
1842
1843	if (fcp->isp_topo == TOPO_FL_PORT)
1844		base = FC_SNS_ID+1;
1845	else
1846		base = 0;
1847
1848	if (fcp->isp_topo == TOPO_N_PORT)
1849		lim = 1;
1850	else
1851		lim = MAX_FC_TARG;
1852
1853	/*
1854	 * Now log in any fabric devices that the outer layer has
1855	 * left for us to see. This seems the most sane policy
1856	 * for the moment.
1857	 */
1858	for (lp = &fcp->portdb[base]; lp < &fcp->portdb[lim]; lp++) {
1859		u_int32_t portid;
1860		mbreg_t mbs;
1861
1862		loopid = lp - fcp->portdb;
1863		if (loopid >= FL_PORT_ID && loopid <= FC_SNS_ID) {
1864			continue;
1865		}
1866
1867		/*
1868		 * Anything here?
1869		 */
1870		if (lp->port_wwn == 0) {
1871			continue;
1872		}
1873
1874		/*
1875		 * Don't try to log into yourself.
1876		 */
1877		if ((portid = lp->portid) == fcp->isp_portid) {
1878			continue;
1879		}
1880
1881
1882		/*
1883		 * If we'd been logged in- see if we still are and we haven't
1884		 * changed. If so, no need to log ourselves out, etc..
1885		 *
1886		 * Unfortunately, our charming Qlogic f/w has decided to
1887		 * return a valid port database entry for a fabric device
1888		 * that has, in fact, gone away. And it hangs trying to
1889		 * log it out.
1890		 */
1891		if (lp->loggedin && lp->force_logout == 0 &&
1892		    isp_getpdb(isp, lp->loopid, &pdb) == 0) {
1893			int nrole;
1894			u_int64_t nwwnn, nwwpn;
1895			nwwnn =
1896			    (((u_int64_t)pdb.pdb_nodename[0]) << 56) |
1897			    (((u_int64_t)pdb.pdb_nodename[1]) << 48) |
1898			    (((u_int64_t)pdb.pdb_nodename[2]) << 40) |
1899			    (((u_int64_t)pdb.pdb_nodename[3]) << 32) |
1900			    (((u_int64_t)pdb.pdb_nodename[4]) << 24) |
1901			    (((u_int64_t)pdb.pdb_nodename[5]) << 16) |
1902			    (((u_int64_t)pdb.pdb_nodename[6]) <<  8) |
1903			    (((u_int64_t)pdb.pdb_nodename[7]));
1904			nwwpn =
1905			    (((u_int64_t)pdb.pdb_portname[0]) << 56) |
1906			    (((u_int64_t)pdb.pdb_portname[1]) << 48) |
1907			    (((u_int64_t)pdb.pdb_portname[2]) << 40) |
1908			    (((u_int64_t)pdb.pdb_portname[3]) << 32) |
1909			    (((u_int64_t)pdb.pdb_portname[4]) << 24) |
1910			    (((u_int64_t)pdb.pdb_portname[5]) << 16) |
1911			    (((u_int64_t)pdb.pdb_portname[6]) <<  8) |
1912			    (((u_int64_t)pdb.pdb_portname[7]));
1913			nrole = (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >>
1914			    SVC3_ROLE_SHIFT;
1915			if (pdb.pdb_loopid == lp->loopid && lp->portid ==
1916			    (u_int32_t) BITS2WORD(pdb.pdb_portid_bits) &&
1917			    nwwnn == lp->node_wwn && nwwpn == lp->port_wwn &&
1918			    lp->roles == nrole && lp->force_logout == 0) {
1919				lp->loggedin = lp->valid = 1;
1920				isp_prt(isp, ISP_LOGCONFIG, lretained,
1921				    (int) (lp - fcp->portdb),
1922				    (int) lp->loopid, lp->portid);
1923				continue;
1924			}
1925		}
1926
1927		if (fcp->isp_fwstate != FW_READY ||
1928		    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
1929			return (-1);
1930		}
1931
1932		/*
1933		 * Force a logout if we were logged in.
1934		 */
1935		if (lp->loggedin) {
1936			if (lp->force_logout ||
1937			    isp_getpdb(isp, lp->loopid, &pdb) == 0) {
1938				mbs.param[0] = MBOX_FABRIC_LOGOUT;
1939				mbs.param[1] = lp->loopid << 8;
1940				mbs.param[2] = 0;
1941				mbs.param[3] = 0;
1942				isp_mboxcmd(isp, &mbs, MBLOGNONE);
1943				isp_prt(isp, ISP_LOGINFO, plogout,
1944				    (int) (lp - fcp->portdb), lp->loopid,
1945				    lp->portid);
1946			}
1947			lp->force_logout = lp->loggedin = 0;
1948			if (fcp->isp_fwstate != FW_READY ||
1949			    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
1950				return (-1);
1951			}
1952		}
1953
1954		/*
1955		 * And log in....
1956		 */
1957		loopid = lp - fcp->portdb;
1958		lp->loopid = FL_PORT_ID;
1959		do {
1960			mbs.param[0] = MBOX_FABRIC_LOGIN;
1961			mbs.param[1] = loopid << 8;
1962			mbs.param[2] = portid >> 16;
1963			mbs.param[3] = portid & 0xffff;
1964			isp_mboxcmd(isp, &mbs, MBLOGALL & ~(MBOX_LOOP_ID_USED |
1965			    MBOX_PORT_ID_USED | MBOX_COMMAND_ERROR));
1966			if (fcp->isp_fwstate != FW_READY ||
1967			    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
1968				return (-1);
1969			}
1970			switch (mbs.param[0]) {
1971			case MBOX_LOOP_ID_USED:
1972				/*
1973				 * Try the next available loop id.
1974				 */
1975				loopid++;
1976				break;
1977			case MBOX_PORT_ID_USED:
1978				/*
1979				 * This port is already logged in.
1980				 * Snaffle the loop id it's using if it's
1981				 * nonzero, otherwise we're hosed.
1982				 */
1983				if (mbs.param[1] != 0) {
1984					loopid = mbs.param[1];
1985					isp_prt(isp, ISP_LOGINFO, retained,
1986					    loopid, (int) (lp - fcp->portdb),
1987					    lp->portid);
1988				} else {
1989					loopid = MAX_FC_TARG;
1990					break;
1991				}
1992				/* FALLTHROUGH */
1993			case MBOX_COMMAND_COMPLETE:
1994				lp->loggedin = 1;
1995				lp->loopid = loopid;
1996				break;
1997			case MBOX_COMMAND_ERROR:
1998				isp_prt(isp, ISP_LOGINFO, plogierr,
1999				    portid, mbs.param[1]);
2000				/* FALLTHROUGH */
2001			case MBOX_ALL_IDS_USED: /* We're outta IDs */
2002			default:
2003				loopid = MAX_FC_TARG;
2004				break;
2005			}
2006		} while (lp->loopid == FL_PORT_ID && loopid < MAX_FC_TARG);
2007
2008		/*
2009		 * If we get here and we haven't set a Loop ID,
2010		 * we failed to log into this device.
2011		 */
2012
2013		if (lp->loopid == FL_PORT_ID) {
2014			lp->loopid = 0;
2015			continue;
2016		}
2017
2018		/*
2019		 * Make sure we can get the approriate port information.
2020		 */
2021		if (isp_getpdb(isp, lp->loopid, &pdb) != 0) {
2022			isp_prt(isp, ISP_LOGWARN, nopdb, lp->portid);
2023			goto dump_em;
2024		}
2025
2026		if (fcp->isp_fwstate != FW_READY ||
2027		    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
2028			return (-1);
2029		}
2030
2031		if (pdb.pdb_loopid != lp->loopid) {
2032			isp_prt(isp, ISP_LOGWARN, pdbmfail1,
2033			    lp->portid, pdb.pdb_loopid);
2034			goto dump_em;
2035		}
2036
2037		if (lp->portid != (u_int32_t) BITS2WORD(pdb.pdb_portid_bits)) {
2038			isp_prt(isp, ISP_LOGWARN, pdbmfail2,
2039			    lp->portid, BITS2WORD(pdb.pdb_portid_bits));
2040			goto dump_em;
2041		}
2042
2043		lp->roles =
2044		    (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
2045		lp->node_wwn =
2046		    (((u_int64_t)pdb.pdb_nodename[0]) << 56) |
2047		    (((u_int64_t)pdb.pdb_nodename[1]) << 48) |
2048		    (((u_int64_t)pdb.pdb_nodename[2]) << 40) |
2049		    (((u_int64_t)pdb.pdb_nodename[3]) << 32) |
2050		    (((u_int64_t)pdb.pdb_nodename[4]) << 24) |
2051		    (((u_int64_t)pdb.pdb_nodename[5]) << 16) |
2052		    (((u_int64_t)pdb.pdb_nodename[6]) <<  8) |
2053		    (((u_int64_t)pdb.pdb_nodename[7]));
2054		lp->port_wwn =
2055		    (((u_int64_t)pdb.pdb_portname[0]) << 56) |
2056		    (((u_int64_t)pdb.pdb_portname[1]) << 48) |
2057		    (((u_int64_t)pdb.pdb_portname[2]) << 40) |
2058		    (((u_int64_t)pdb.pdb_portname[3]) << 32) |
2059		    (((u_int64_t)pdb.pdb_portname[4]) << 24) |
2060		    (((u_int64_t)pdb.pdb_portname[5]) << 16) |
2061		    (((u_int64_t)pdb.pdb_portname[6]) <<  8) |
2062		    (((u_int64_t)pdb.pdb_portname[7]));
2063		/*
2064		 * Check to make sure this all makes sense.
2065		 */
2066		if (lp->node_wwn && lp->port_wwn) {
2067			lp->valid = 1;
2068			loopid = lp - fcp->portdb;
2069			(void) isp_async(isp, ISPASYNC_PROMENADE, &loopid);
2070			continue;
2071		}
2072dump_em:
2073		lp->valid = 0;
2074		isp_prt(isp, ISP_LOGINFO,
2075		    ldumped, loopid, lp->loopid, lp->portid);
2076		mbs.param[0] = MBOX_FABRIC_LOGOUT;
2077		mbs.param[1] = lp->loopid << 8;
2078		mbs.param[2] = 0;
2079		mbs.param[3] = 0;
2080		isp_mboxcmd(isp, &mbs, MBLOGNONE);
2081		if (fcp->isp_fwstate != FW_READY ||
2082		    fcp->isp_loopstate != LOOP_SYNCING_PDB) {
2083			return (-1);
2084		}
2085	}
2086	/*
2087	 * If we get here, we've for sure seen not only a valid loop
2088	 * but know what is or isn't on it, so mark this for usage
2089	 * in isp_start.
2090	 */
2091	fcp->loop_seen_once = 1;
2092	fcp->isp_loopstate = LOOP_READY;
2093	return (0);
2094}
2095
2096static int
2097isp_scan_loop(struct ispsoftc *isp)
2098{
2099	struct lportdb *lp;
2100	fcparam *fcp = isp->isp_param;
2101	isp_pdb_t pdb;
2102	int loopid, lim, hival;
2103
2104	switch (fcp->isp_topo) {
2105	case TOPO_NL_PORT:
2106		hival = FL_PORT_ID;
2107		break;
2108	case TOPO_N_PORT:
2109		hival = 2;
2110		break;
2111	case TOPO_FL_PORT:
2112		hival = FC_PORT_ID;
2113		break;
2114	default:
2115		fcp->isp_loopstate = LOOP_LSCAN_DONE;
2116		return (0);
2117	}
2118	fcp->isp_loopstate = LOOP_SCANNING_LOOP;
2119
2120	/*
2121	 * make sure the temp port database is clean...
2122	 */
2123	MEMZERO((void *)fcp->tport, sizeof (fcp->tport));
2124
2125	/*
2126	 * Run through the local loop ports and get port database info
2127	 * for each loop ID.
2128	 *
2129	 * There's a somewhat unexplained situation where the f/w passes back
2130	 * the wrong database entity- if that happens, just restart (up to
2131	 * FL_PORT_ID times).
2132	 */
2133	for (lim = loopid = 0; loopid < hival; loopid++) {
2134		lp = &fcp->tport[loopid];
2135
2136		/*
2137		 * Don't even try for ourselves...
2138	 	 */
2139		if (loopid == fcp->isp_loopid)
2140			continue;
2141
2142		lp->node_wwn = isp_get_portname(isp, loopid, 1);
2143		if (fcp->isp_loopstate < LOOP_SCANNING_LOOP)
2144			return (-1);
2145		if (lp->node_wwn == 0)
2146			continue;
2147		lp->port_wwn = isp_get_portname(isp, loopid, 0);
2148		if (fcp->isp_loopstate < LOOP_SCANNING_LOOP)
2149			return (-1);
2150		if (lp->port_wwn == 0) {
2151			lp->node_wwn = 0;
2152			continue;
2153		}
2154
2155		/*
2156		 * Get an entry....
2157		 */
2158		if (isp_getpdb(isp, loopid, &pdb) != 0) {
2159			if (fcp->isp_loopstate < LOOP_SCANNING_LOOP)
2160				return (-1);
2161			continue;
2162		}
2163		if (fcp->isp_loopstate < LOOP_SCANNING_LOOP) {
2164			return (-1);
2165		}
2166
2167		/*
2168		 * If the returned database element doesn't match what we
2169		 * asked for, restart the process entirely (up to a point...).
2170		 */
2171		if (pdb.pdb_loopid != loopid) {
2172			loopid = 0;
2173			if (lim++ < hival) {
2174				continue;
2175			}
2176			isp_prt(isp, ISP_LOGWARN,
2177			    "giving up on synchronizing the port database");
2178			return (-1);
2179		}
2180
2181		/*
2182		 * Save the pertinent info locally.
2183		 */
2184		lp->node_wwn =
2185		    (((u_int64_t)pdb.pdb_nodename[0]) << 56) |
2186		    (((u_int64_t)pdb.pdb_nodename[1]) << 48) |
2187		    (((u_int64_t)pdb.pdb_nodename[2]) << 40) |
2188		    (((u_int64_t)pdb.pdb_nodename[3]) << 32) |
2189		    (((u_int64_t)pdb.pdb_nodename[4]) << 24) |
2190		    (((u_int64_t)pdb.pdb_nodename[5]) << 16) |
2191		    (((u_int64_t)pdb.pdb_nodename[6]) <<  8) |
2192		    (((u_int64_t)pdb.pdb_nodename[7]));
2193		lp->port_wwn =
2194		    (((u_int64_t)pdb.pdb_portname[0]) << 56) |
2195		    (((u_int64_t)pdb.pdb_portname[1]) << 48) |
2196		    (((u_int64_t)pdb.pdb_portname[2]) << 40) |
2197		    (((u_int64_t)pdb.pdb_portname[3]) << 32) |
2198		    (((u_int64_t)pdb.pdb_portname[4]) << 24) |
2199		    (((u_int64_t)pdb.pdb_portname[5]) << 16) |
2200		    (((u_int64_t)pdb.pdb_portname[6]) <<  8) |
2201		    (((u_int64_t)pdb.pdb_portname[7]));
2202		lp->roles =
2203		    (pdb.pdb_prli_svc3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
2204		lp->portid = BITS2WORD(pdb.pdb_portid_bits);
2205		lp->loopid = pdb.pdb_loopid;
2206	}
2207
2208	/*
2209	 * Mark all of the permanent local loop database entries as invalid
2210	 * (except our own entry).
2211	 */
2212	for (loopid = 0; loopid < hival; loopid++) {
2213		if (loopid == fcp->isp_iid) {
2214			fcp->portdb[loopid].valid = 1;
2215			fcp->portdb[loopid].loopid = fcp->isp_loopid;
2216			continue;
2217		}
2218		fcp->portdb[loopid].valid = 0;
2219	}
2220
2221	/*
2222	 * Now merge our local copy of the port database into our saved copy.
2223	 * Notify the outer layers of new devices arriving.
2224	 */
2225	for (loopid = 0; loopid < hival; loopid++) {
2226		int i;
2227
2228		/*
2229		 * If we don't have a non-zero Port WWN, we're not here.
2230		 */
2231		if (fcp->tport[loopid].port_wwn == 0) {
2232			continue;
2233		}
2234
2235		/*
2236		 * Skip ourselves.
2237		 */
2238		if (loopid == fcp->isp_iid) {
2239			continue;
2240		}
2241
2242		/*
2243		 * For the purposes of deciding whether this is the
2244		 * 'same' device or not, we only search for an identical
2245		 * Port WWN. Node WWNs may or may not be the same as
2246		 * the Port WWN, and there may be multiple different
2247		 * Port WWNs with the same Node WWN. It would be chaos
2248		 * to have multiple identical Port WWNs, so we don't
2249		 * allow that.
2250		 */
2251
2252		for (i = 0; i < hival; i++) {
2253			int j;
2254			if (fcp->portdb[i].port_wwn == 0)
2255				continue;
2256			if (fcp->portdb[i].port_wwn !=
2257			    fcp->tport[loopid].port_wwn)
2258				continue;
2259			/*
2260			 * We found this WWN elsewhere- it's changed
2261			 * loopids then. We don't change it's actual
2262			 * position in our cached port database- we
2263			 * just change the actual loop ID we'd use.
2264			 */
2265			if (fcp->portdb[i].loopid != loopid) {
2266				isp_prt(isp, ISP_LOGINFO, portshift, i,
2267				    fcp->portdb[i].loopid,
2268				    fcp->portdb[i].portid, loopid,
2269				    fcp->tport[loopid].portid);
2270			}
2271			fcp->portdb[i].portid = fcp->tport[loopid].portid;
2272			fcp->portdb[i].loopid = loopid;
2273			fcp->portdb[i].valid = 1;
2274			fcp->portdb[i].roles = fcp->tport[loopid].roles;
2275
2276			/*
2277			 * Now make sure this Port WWN doesn't exist elsewhere
2278			 * in the port database.
2279			 */
2280			for (j = i+1; j < hival; j++) {
2281				if (fcp->portdb[i].port_wwn !=
2282				    fcp->portdb[j].port_wwn) {
2283					continue;
2284				}
2285				isp_prt(isp, ISP_LOGWARN, portdup, j, i);
2286				/*
2287				 * Invalidate the 'old' *and* 'new' ones.
2288				 * This is really harsh and not quite right,
2289				 * but if this happens, we really don't know
2290				 * who is what at this point.
2291				 */
2292				fcp->portdb[i].valid = 0;
2293				fcp->portdb[j].valid = 0;
2294			}
2295			break;
2296		}
2297
2298		/*
2299		 * If we didn't traverse the entire port database,
2300		 * then we found (and remapped) an existing entry.
2301		 * No need to notify anyone- go for the next one.
2302		 */
2303		if (i < hival) {
2304			isp_prt(isp, ISP_LOGINFO, retained,
2305			    fcp->portdb[i].loopid, i, fcp->portdb[i].portid);
2306			continue;
2307		}
2308
2309		/*
2310		 * We've not found this Port WWN anywhere. It's a new entry.
2311		 * See if we can leave it where it is (with target == loopid).
2312		 */
2313		if (fcp->portdb[loopid].port_wwn != 0) {
2314			for (lim = 0; lim < hival; lim++) {
2315				if (fcp->portdb[lim].port_wwn == 0)
2316					break;
2317			}
2318			/* "Cannot Happen" */
2319			if (lim == hival) {
2320				isp_prt(isp, ISP_LOGWARN, "Remap Overflow");
2321				continue;
2322			}
2323			i = lim;
2324		} else {
2325			i = loopid;
2326		}
2327
2328		/*
2329		 * NB:	The actual loopid we use here is loopid- we may
2330		 *	in fact be at a completely different index (target).
2331		 */
2332		fcp->portdb[i].loopid = loopid;
2333		fcp->portdb[i].port_wwn = fcp->tport[loopid].port_wwn;
2334		fcp->portdb[i].node_wwn = fcp->tport[loopid].node_wwn;
2335		fcp->portdb[i].roles = fcp->tport[loopid].roles;
2336		fcp->portdb[i].portid = fcp->tport[loopid].portid;
2337		fcp->portdb[i].valid = 1;
2338
2339		/*
2340		 * Tell the outside world we've arrived.
2341		 */
2342		(void) isp_async(isp, ISPASYNC_PROMENADE, &i);
2343	}
2344
2345	/*
2346	 * Now find all previously used targets that are now invalid and
2347	 * notify the outer layers that they're gone.
2348	 */
2349	for (lp = &fcp->portdb[0]; lp < &fcp->portdb[hival]; lp++) {
2350		if (lp->valid || lp->port_wwn == 0) {
2351			continue;
2352		}
2353
2354		/*
2355		 * Tell the outside world we've gone
2356		 * away and erase our pdb entry.
2357		 *
2358		 */
2359		loopid = lp - fcp->portdb;
2360		(void) isp_async(isp, ISPASYNC_PROMENADE, &loopid);
2361		MEMZERO((void *) lp, sizeof (*lp));
2362	}
2363	fcp->isp_loopstate = LOOP_LSCAN_DONE;
2364	return (0);
2365}
2366
2367
2368static int
2369isp_fabric_mbox_cmd(struct ispsoftc *isp, mbreg_t *mbp)
2370{
2371	isp_mboxcmd(isp, mbp, MBLOGNONE);
2372	if (mbp->param[0] != MBOX_COMMAND_COMPLETE) {
2373		if (FCPARAM(isp)->isp_loopstate == LOOP_SCANNING_FABRIC) {
2374			FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
2375		}
2376		if (mbp->param[0] == MBOX_COMMAND_ERROR) {
2377			char tbuf[16];
2378			char *m;
2379			switch (mbp->param[1]) {
2380			case 1:
2381				m = "No Loop";
2382				break;
2383			case 2:
2384				m = "Failed to allocate IOCB buffer";
2385				break;
2386			case 3:
2387				m = "Failed to allocate XCB buffer";
2388				break;
2389			case 4:
2390				m = "timeout or transmit failed";
2391				break;
2392			case 5:
2393				m = "no fabric loop";
2394				break;
2395			case 6:
2396				m = "remote device not a target";
2397				break;
2398			default:
2399				SNPRINTF(tbuf, sizeof tbuf, "%x",
2400				    mbp->param[1]);
2401				m = tbuf;
2402				break;
2403			}
2404			isp_prt(isp, ISP_LOGERR, "SNS Failed- %s", m);
2405		}
2406		return (-1);
2407	}
2408
2409	if (FCPARAM(isp)->isp_fwstate != FW_READY ||
2410	    FCPARAM(isp)->isp_loopstate < LOOP_SCANNING_FABRIC) {
2411		return (-1);
2412	}
2413	return(0);
2414}
2415
2416#ifdef	ISP_USE_GA_NXT
2417static int
2418isp_scan_fabric(struct ispsoftc *isp, int ftype)
2419{
2420	fcparam *fcp = isp->isp_param;
2421	u_int32_t portid, first_portid, last_portid;
2422	int hicap, last_port_same;
2423
2424	if (fcp->isp_onfabric == 0) {
2425		fcp->isp_loopstate = LOOP_FSCAN_DONE;
2426		return (0);
2427	}
2428
2429	FC_SCRATCH_ACQUIRE(isp);
2430
2431	/*
2432	 * Since Port IDs are 24 bits, we can check against having seen
2433	 * anything yet with this value.
2434	 */
2435	last_port_same = 0;
2436	last_portid = 0xffffffff;	/* not a port */
2437	first_portid = portid = fcp->isp_portid;
2438	fcp->isp_loopstate = LOOP_SCANNING_FABRIC;
2439
2440	for (hicap = 0; hicap < GA_NXT_MAX; hicap++) {
2441		mbreg_t mbs;
2442		sns_screq_t *rq;
2443		sns_ga_nxt_rsp_t *rs0, *rs1;
2444		struct lportdb lcl;
2445		u_int8_t sc[SNS_GA_NXT_RESP_SIZE];
2446
2447		rq = (sns_screq_t *)sc;
2448		MEMZERO((void *) rq, SNS_GA_NXT_REQ_SIZE);
2449		rq->snscb_rblen = SNS_GA_NXT_RESP_SIZE >> 1;
2450		rq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+0x100);
2451		rq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+0x100);
2452		rq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+0x100);
2453		rq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+0x100);
2454		rq->snscb_sblen = 6;
2455		rq->snscb_data[0] = SNS_GA_NXT;
2456		rq->snscb_data[4] = portid & 0xffff;
2457		rq->snscb_data[5] = (portid >> 16) & 0xff;
2458		isp_put_sns_request(isp, rq, (sns_screq_t *) fcp->isp_scratch);
2459		MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GA_NXT_REQ_SIZE);
2460		mbs.param[0] = MBOX_SEND_SNS;
2461		mbs.param[1] = SNS_GA_NXT_REQ_SIZE >> 1;
2462		mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2463		mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2464		/*
2465		 * Leave 4 and 5 alone
2466		 */
2467		mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2468		mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2469		if (isp_fabric_mbox_cmd(isp, &mbs)) {
2470			if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
2471				fcp->isp_loopstate = LOOP_PDB_RCVD;
2472			}
2473			FC_SCRATCH_RELEASE(isp);
2474			return (-1);
2475		}
2476		MEMORYBARRIER(isp, SYNC_SFORCPU, 0x100, SNS_GA_NXT_RESP_SIZE);
2477		rs1 = (sns_ga_nxt_rsp_t *) sc;
2478		rs0 = (sns_ga_nxt_rsp_t *) ((u_int8_t *)fcp->isp_scratch+0x100);
2479		isp_get_ga_nxt_response(isp, rs0, rs1);
2480		if (rs1->snscb_cthdr.ct_response != FS_ACC) {
2481			int level;
2482			if (rs1->snscb_cthdr.ct_reason == 9 &&
2483			    rs1->snscb_cthdr.ct_explanation == 7)
2484				level = ISP_LOGDEBUG0;
2485			else
2486				level = ISP_LOGWARN;
2487			isp_prt(isp, level, swrej, "GA_NXT",
2488			    rs1->snscb_cthdr.ct_reason,
2489			    rs1->snscb_cthdr.ct_explanation, portid);
2490			FC_SCRATCH_RELEASE(isp);
2491			fcp->isp_loopstate = LOOP_FSCAN_DONE;
2492			return (0);
2493		}
2494		portid =
2495		    (((u_int32_t) rs1->snscb_port_id[0]) << 16) |
2496		    (((u_int32_t) rs1->snscb_port_id[1]) << 8) |
2497		    (((u_int32_t) rs1->snscb_port_id[2]));
2498
2499		/*
2500		 * XXX: We should check to make sure that this entry
2501		 * XXX: supports the type(s) we are interested in.
2502		 */
2503		/*
2504		 * Okay, we now have information about a fabric object.
2505		 * If it is the type we're interested in, tell the outer layers
2506		 * about it. The outer layer needs to  know: Port ID, WWNN,
2507		 * WWPN, FC4 type, and port type.
2508		 *
2509		 * The lportdb structure is adequate for this.
2510		 */
2511		MEMZERO(&lcl, sizeof (lcl));
2512		lcl.port_type = rs1->snscb_port_type;
2513		lcl.fc4_type = ftype;
2514		lcl.portid = portid;
2515		lcl.node_wwn =
2516		    (((u_int64_t)rs1->snscb_nodename[0]) << 56) |
2517		    (((u_int64_t)rs1->snscb_nodename[1]) << 48) |
2518		    (((u_int64_t)rs1->snscb_nodename[2]) << 40) |
2519		    (((u_int64_t)rs1->snscb_nodename[3]) << 32) |
2520		    (((u_int64_t)rs1->snscb_nodename[4]) << 24) |
2521		    (((u_int64_t)rs1->snscb_nodename[5]) << 16) |
2522		    (((u_int64_t)rs1->snscb_nodename[6]) <<  8) |
2523		    (((u_int64_t)rs1->snscb_nodename[7]));
2524		lcl.port_wwn =
2525		    (((u_int64_t)rs1->snscb_portname[0]) << 56) |
2526		    (((u_int64_t)rs1->snscb_portname[1]) << 48) |
2527		    (((u_int64_t)rs1->snscb_portname[2]) << 40) |
2528		    (((u_int64_t)rs1->snscb_portname[3]) << 32) |
2529		    (((u_int64_t)rs1->snscb_portname[4]) << 24) |
2530		    (((u_int64_t)rs1->snscb_portname[5]) << 16) |
2531		    (((u_int64_t)rs1->snscb_portname[6]) <<  8) |
2532		    (((u_int64_t)rs1->snscb_portname[7]));
2533
2534		/*
2535		 * Does this fabric object support the type we want?
2536		 * If not, skip it.
2537		 */
2538		if (rs1->snscb_fc4_types[ftype >> 5] & (1 << (ftype & 0x1f))) {
2539			if (first_portid == portid) {
2540				lcl.last_fabric_dev = 1;
2541			} else {
2542				lcl.last_fabric_dev = 0;
2543			}
2544			(void) isp_async(isp, ISPASYNC_FABRIC_DEV, &lcl);
2545		} else {
2546			isp_prt(isp, ISP_LOGDEBUG0,
2547			    "PortID 0x%x doesn't support FC4 type 0x%x",
2548			    portid, ftype);
2549		}
2550		if (first_portid == portid) {
2551			fcp->isp_loopstate = LOOP_FSCAN_DONE;
2552			FC_SCRATCH_RELEASE(isp);
2553			return (0);
2554		}
2555		if (portid == last_portid) {
2556			if (last_port_same++ > 20) {
2557				isp_prt(isp, ISP_LOGWARN,
2558				    "tangled fabric database detected");
2559				break;
2560			}
2561		} else {
2562			last_port_same = 0 ;
2563			last_portid = portid;
2564		}
2565	}
2566	FC_SCRATCH_RELEASE(isp);
2567	if (hicap >= GA_NXT_MAX) {
2568		isp_prt(isp, ISP_LOGWARN, "fabric too big (> %d)", GA_NXT_MAX);
2569	}
2570	fcp->isp_loopstate = LOOP_FSCAN_DONE;
2571	return (0);
2572}
2573#else
2574#define	GIDLEN	((ISP2100_SCRLEN >> 1) + 16)
2575#define	NGENT	((GIDLEN - 16) >> 2)
2576
2577#define	IGPOFF	(ISP2100_SCRLEN - GIDLEN)
2578#define	GXOFF	(256)
2579
2580static int
2581isp_scan_fabric(struct ispsoftc *isp, int ftype)
2582{
2583	fcparam *fcp = FCPARAM(isp);
2584	mbreg_t mbs;
2585	int i;
2586	sns_gid_ft_req_t *rq;
2587	sns_gid_ft_rsp_t *rs0, *rs1;
2588
2589	if (fcp->isp_onfabric == 0) {
2590		fcp->isp_loopstate = LOOP_FSCAN_DONE;
2591		return (0);
2592	}
2593
2594	FC_SCRATCH_ACQUIRE(isp);
2595	fcp->isp_loopstate = LOOP_SCANNING_FABRIC;
2596
2597	rq = (sns_gid_ft_req_t *)fcp->tport;
2598	MEMZERO((void *) rq, SNS_GID_FT_REQ_SIZE);
2599	rq->snscb_rblen = GIDLEN >> 1;
2600	rq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+IGPOFF);
2601	rq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+IGPOFF);
2602	rq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+IGPOFF);
2603	rq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+IGPOFF);
2604	rq->snscb_sblen = 6;
2605	rq->snscb_cmd = SNS_GID_FT;
2606	rq->snscb_mword_div_2 = NGENT;
2607	rq->snscb_fc4_type = ftype;
2608	isp_put_gid_ft_request(isp, rq, (sns_gid_ft_req_t *) fcp->isp_scratch);
2609	MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GID_FT_REQ_SIZE);
2610	mbs.param[0] = MBOX_SEND_SNS;
2611	mbs.param[1] = SNS_GID_FT_REQ_SIZE >> 1;
2612	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2613	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2614
2615	/*
2616	 * Leave 4 and 5 alone
2617	 */
2618	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2619	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2620	if (isp_fabric_mbox_cmd(isp, &mbs)) {
2621		if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
2622			fcp->isp_loopstate = LOOP_PDB_RCVD;
2623		}
2624		FC_SCRATCH_RELEASE(isp);
2625		return (-1);
2626	}
2627	if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2628		FC_SCRATCH_RELEASE(isp);
2629		return (-1);
2630	}
2631	MEMORYBARRIER(isp, SYNC_SFORCPU, IGPOFF, GIDLEN);
2632	rs1 = (sns_gid_ft_rsp_t *) fcp->tport;
2633	rs0 = (sns_gid_ft_rsp_t *) ((u_int8_t *)fcp->isp_scratch+IGPOFF);
2634	isp_get_gid_ft_response(isp, rs0, rs1, NGENT);
2635	if (rs1->snscb_cthdr.ct_response != FS_ACC) {
2636		int level;
2637		if (rs1->snscb_cthdr.ct_reason == 9 &&
2638		    rs1->snscb_cthdr.ct_explanation == 7)
2639			level = ISP_LOGDEBUG0;
2640		else
2641			level = ISP_LOGWARN;
2642		isp_prt(isp, level, swrej, "GID_FT",
2643		    rs1->snscb_cthdr.ct_reason,
2644		    rs1->snscb_cthdr.ct_explanation, 0);
2645		FC_SCRATCH_RELEASE(isp);
2646		fcp->isp_loopstate = LOOP_FSCAN_DONE;
2647		return (0);
2648	}
2649
2650	/*
2651	 * Okay, we now have a list of Port IDs for this class of device.
2652	 * Go through the list and for each one get the WWPN/WWNN for it
2653	 * and tell the outer layers about it. The outer layer needs to
2654	 * know: Port ID, WWNN, WWPN, FC4 type, and (possibly) port type.
2655	 *
2656	 * The lportdb structure is adequate for this.
2657	 */
2658	i = -1;
2659	do {
2660		sns_gxn_id_req_t grqbuf, *gq = &grqbuf;
2661		sns_gxn_id_rsp_t *gs0, grsbuf, *gs1 = &grsbuf;
2662		struct lportdb lcl;
2663#if	0
2664		sns_gff_id_rsp_t *fs0, ffsbuf, *fs1 = &ffsbuf;
2665#endif
2666
2667		i++;
2668		MEMZERO(&lcl, sizeof (lcl));
2669		lcl.fc4_type = ftype;
2670		lcl.portid =
2671		    (((u_int32_t) rs1->snscb_ports[i].portid[0]) << 16) |
2672		    (((u_int32_t) rs1->snscb_ports[i].portid[1]) << 8) |
2673		    (((u_int32_t) rs1->snscb_ports[i].portid[2]));
2674
2675		MEMZERO((void *) gq, sizeof (sns_gxn_id_req_t));
2676		gq->snscb_rblen = SNS_GXN_ID_RESP_SIZE >> 1;
2677		gq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+GXOFF);
2678		gq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+GXOFF);
2679		gq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+GXOFF);
2680		gq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+GXOFF);
2681		gq->snscb_sblen = 6;
2682		gq->snscb_cmd = SNS_GPN_ID;
2683		gq->snscb_portid = lcl.portid;
2684		isp_put_gxn_id_request(isp, gq,
2685		    (sns_gxn_id_req_t *) fcp->isp_scratch);
2686		MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GXN_ID_REQ_SIZE);
2687		mbs.param[0] = MBOX_SEND_SNS;
2688		mbs.param[1] = SNS_GXN_ID_REQ_SIZE >> 1;
2689		mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2690		mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2691		/*
2692		 * Leave 4 and 5 alone
2693		 */
2694		mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2695		mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2696		if (isp_fabric_mbox_cmd(isp, &mbs)) {
2697			if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
2698				fcp->isp_loopstate = LOOP_PDB_RCVD;
2699			}
2700			FC_SCRATCH_RELEASE(isp);
2701			return (-1);
2702		}
2703		if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2704			FC_SCRATCH_RELEASE(isp);
2705			return (-1);
2706		}
2707		MEMORYBARRIER(isp, SYNC_SFORCPU, GXOFF, SNS_GXN_ID_RESP_SIZE);
2708		gs0 = (sns_gxn_id_rsp_t *) ((u_int8_t *)fcp->isp_scratch+GXOFF);
2709		isp_get_gxn_id_response(isp, gs0, gs1);
2710		if (gs1->snscb_cthdr.ct_response != FS_ACC) {
2711			isp_prt(isp, ISP_LOGWARN, swrej, "GPN_ID",
2712			    gs1->snscb_cthdr.ct_reason,
2713			    gs1->snscb_cthdr.ct_explanation, lcl.portid);
2714			if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2715				FC_SCRATCH_RELEASE(isp);
2716				return (-1);
2717			}
2718			continue;
2719		}
2720		lcl.port_wwn =
2721		    (((u_int64_t)gs1->snscb_wwn[0]) << 56) |
2722		    (((u_int64_t)gs1->snscb_wwn[1]) << 48) |
2723		    (((u_int64_t)gs1->snscb_wwn[2]) << 40) |
2724		    (((u_int64_t)gs1->snscb_wwn[3]) << 32) |
2725		    (((u_int64_t)gs1->snscb_wwn[4]) << 24) |
2726		    (((u_int64_t)gs1->snscb_wwn[5]) << 16) |
2727		    (((u_int64_t)gs1->snscb_wwn[6]) <<  8) |
2728		    (((u_int64_t)gs1->snscb_wwn[7]));
2729
2730		MEMZERO((void *) gq, sizeof (sns_gxn_id_req_t));
2731		gq->snscb_rblen = SNS_GXN_ID_RESP_SIZE >> 1;
2732		gq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+GXOFF);
2733		gq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+GXOFF);
2734		gq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+GXOFF);
2735		gq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+GXOFF);
2736		gq->snscb_sblen = 6;
2737		gq->snscb_cmd = SNS_GNN_ID;
2738		gq->snscb_portid = lcl.portid;
2739		isp_put_gxn_id_request(isp, gq,
2740		    (sns_gxn_id_req_t *) fcp->isp_scratch);
2741		MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GXN_ID_REQ_SIZE);
2742		mbs.param[0] = MBOX_SEND_SNS;
2743		mbs.param[1] = SNS_GXN_ID_REQ_SIZE >> 1;
2744		mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2745		mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2746		/*
2747		 * Leave 4 and 5 alone
2748		 */
2749		mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2750		mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2751		if (isp_fabric_mbox_cmd(isp, &mbs)) {
2752			if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
2753				fcp->isp_loopstate = LOOP_PDB_RCVD;
2754			}
2755			FC_SCRATCH_RELEASE(isp);
2756			return (-1);
2757		}
2758		if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2759			FC_SCRATCH_RELEASE(isp);
2760			return (-1);
2761		}
2762		MEMORYBARRIER(isp, SYNC_SFORCPU, GXOFF, SNS_GXN_ID_RESP_SIZE);
2763		gs0 = (sns_gxn_id_rsp_t *) ((u_int8_t *)fcp->isp_scratch+GXOFF);
2764		isp_get_gxn_id_response(isp, gs0, gs1);
2765		if (gs1->snscb_cthdr.ct_response != FS_ACC) {
2766			isp_prt(isp, ISP_LOGWARN, swrej, "GNN_ID",
2767			    gs1->snscb_cthdr.ct_reason,
2768			    gs1->snscb_cthdr.ct_explanation, lcl.portid);
2769			if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2770				FC_SCRATCH_RELEASE(isp);
2771				return (-1);
2772			}
2773			continue;
2774		}
2775		lcl.node_wwn =
2776		    (((u_int64_t)gs1->snscb_wwn[0]) << 56) |
2777		    (((u_int64_t)gs1->snscb_wwn[1]) << 48) |
2778		    (((u_int64_t)gs1->snscb_wwn[2]) << 40) |
2779		    (((u_int64_t)gs1->snscb_wwn[3]) << 32) |
2780		    (((u_int64_t)gs1->snscb_wwn[4]) << 24) |
2781		    (((u_int64_t)gs1->snscb_wwn[5]) << 16) |
2782		    (((u_int64_t)gs1->snscb_wwn[6]) <<  8) |
2783		    (((u_int64_t)gs1->snscb_wwn[7]));
2784
2785		/*
2786		 * The QLogic f/w is bouncing this with a parameter error.
2787		 */
2788#if	0
2789		/*
2790		 * Try and get FC4 Features (FC-GS-3 only).
2791		 * We can use the sns_gxn_id_req_t for this request.
2792		 */
2793		MEMZERO((void *) gq, sizeof (sns_gxn_id_req_t));
2794		gq->snscb_rblen = SNS_GFF_ID_RESP_SIZE >> 1;
2795		gq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma+GXOFF);
2796		gq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma+GXOFF);
2797		gq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma+GXOFF);
2798		gq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma+GXOFF);
2799		gq->snscb_sblen = 6;
2800		gq->snscb_cmd = SNS_GFF_ID;
2801		gq->snscb_portid = lcl.portid;
2802		isp_put_gxn_id_request(isp, gq,
2803		    (sns_gxn_id_req_t *) fcp->isp_scratch);
2804		MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GXN_ID_REQ_SIZE);
2805		mbs.param[0] = MBOX_SEND_SNS;
2806		mbs.param[1] = SNS_GXN_ID_REQ_SIZE >> 1;
2807		mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2808		mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2809		/*
2810		 * Leave 4 and 5 alone
2811		 */
2812		mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2813		mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2814		if (isp_fabric_mbox_cmd(isp, &mbs)) {
2815			if (fcp->isp_loopstate >= LOOP_SCANNING_FABRIC) {
2816				fcp->isp_loopstate = LOOP_PDB_RCVD;
2817			}
2818			FC_SCRATCH_RELEASE(isp);
2819			return (-1);
2820		}
2821		if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2822			FC_SCRATCH_RELEASE(isp);
2823			return (-1);
2824		}
2825		MEMORYBARRIER(isp, SYNC_SFORCPU, GXOFF, SNS_GFF_ID_RESP_SIZE);
2826		fs0 = (sns_gff_id_rsp_t *) ((u_int8_t *)fcp->isp_scratch+GXOFF);
2827		isp_get_gff_id_response(isp, fs0, fs1);
2828		if (fs1->snscb_cthdr.ct_response != FS_ACC) {
2829			isp_prt(isp, /* ISP_LOGDEBUG0 */ ISP_LOGWARN,
2830			    swrej, "GFF_ID",
2831			    fs1->snscb_cthdr.ct_reason,
2832			    fs1->snscb_cthdr.ct_explanation, lcl.portid);
2833			if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
2834				FC_SCRATCH_RELEASE(isp);
2835				return (-1);
2836			}
2837		} else {
2838			int index = (ftype >> 3);
2839			int bshft = (ftype & 0x7) * 4;
2840			int fc4_fval =
2841			    (fs1->snscb_fc4_features[index] >> bshft) & 0xf;
2842			if (fc4_fval & 0x1) {
2843				lcl.roles |=
2844				    (SVC3_INI_ROLE >> SVC3_ROLE_SHIFT);
2845			}
2846			if (fc4_fval & 0x2) {
2847				lcl.roles |=
2848				    (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT);
2849			}
2850		}
2851#endif
2852
2853		/*
2854		 * If we really want to know what kind of port type this is,
2855		 * we have to run another CT command. Otherwise, we'll leave
2856		 * it as undefined.
2857		 *
2858		lcl.port_type = 0;
2859		 */
2860		if (rs1->snscb_ports[i].control & 0x80) {
2861			lcl.last_fabric_dev = 1;
2862		} else {
2863			lcl.last_fabric_dev = 0;
2864		}
2865		(void) isp_async(isp, ISPASYNC_FABRIC_DEV, &lcl);
2866
2867	} while ((rs1->snscb_ports[i].control & 0x80) == 0 && i < NGENT-1);
2868
2869	/*
2870	 * If we're not at the last entry, our list isn't big enough.
2871	 */
2872	if ((rs1->snscb_ports[i].control & 0x80) == 0) {
2873		isp_prt(isp, ISP_LOGWARN, "fabric too big for scratch area");
2874	}
2875
2876	FC_SCRATCH_RELEASE(isp);
2877	fcp->isp_loopstate = LOOP_FSCAN_DONE;
2878	return (0);
2879}
2880#endif
2881
2882static void
2883isp_register_fc4_type(struct ispsoftc *isp)
2884{
2885	fcparam *fcp = isp->isp_param;
2886	u_int8_t local[SNS_RFT_ID_REQ_SIZE];
2887	sns_screq_t *reqp = (sns_screq_t *) local;
2888	mbreg_t mbs;
2889
2890	MEMZERO((void *) reqp, SNS_RFT_ID_REQ_SIZE);
2891	reqp->snscb_rblen = SNS_RFT_ID_RESP_SIZE >> 1;
2892	reqp->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma + 0x100);
2893	reqp->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma + 0x100);
2894	reqp->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma + 0x100);
2895	reqp->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma + 0x100);
2896	reqp->snscb_sblen = 22;
2897	reqp->snscb_data[0] = SNS_RFT_ID;
2898	reqp->snscb_data[4] = fcp->isp_portid & 0xffff;
2899	reqp->snscb_data[5] = (fcp->isp_portid >> 16) & 0xff;
2900	reqp->snscb_data[6] = (1 << FC4_SCSI);
2901#if	0
2902	reqp->snscb_data[6] |= (1 << FC4_IP);	/* ISO/IEC 8802-2 LLC/SNAP */
2903#endif
2904	FC_SCRATCH_ACQUIRE(isp);
2905	isp_put_sns_request(isp, reqp, (sns_screq_t *) fcp->isp_scratch);
2906	mbs.param[0] = MBOX_SEND_SNS;
2907	mbs.param[1] = SNS_RFT_ID_REQ_SIZE >> 1;
2908	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2909	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2910	/*
2911	 * Leave 4 and 5 alone
2912	 */
2913	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2914	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2915	isp_mboxcmd(isp, &mbs, MBLOGALL);
2916	FC_SCRATCH_RELEASE(isp);
2917	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
2918		isp_prt(isp, ISP_LOGDEBUG0, "Register FC4 types succeeded");
2919	}
2920}
2921
2922/*
2923 * Start a command. Locking is assumed done in the caller.
2924 */
2925
2926int
2927isp_start(XS_T *xs)
2928{
2929	struct ispsoftc *isp;
2930	u_int16_t nxti, optr, handle;
2931	u_int8_t local[QENTRY_LEN];
2932	ispreq_t *reqp, *qep;
2933	int target, i;
2934
2935	XS_INITERR(xs);
2936	isp = XS_ISP(xs);
2937
2938	/*
2939	 * Check to make sure we're supporting initiator role.
2940	 */
2941	if ((isp->isp_role & ISP_ROLE_INITIATOR) == 0) {
2942		XS_SETERR(xs, HBA_SELTIMEOUT);
2943		return (CMD_COMPLETE);
2944	}
2945
2946	/*
2947	 * Now make sure we're running.
2948	 */
2949
2950	if (isp->isp_state != ISP_RUNSTATE) {
2951		isp_prt(isp, ISP_LOGERR, "Adapter not at RUNSTATE");
2952		XS_SETERR(xs, HBA_BOTCH);
2953		return (CMD_COMPLETE);
2954	}
2955
2956	/*
2957	 * Check command CDB length, etc.. We really are limited to 16 bytes
2958	 * for Fibre Channel, but can do up to 44 bytes in parallel SCSI,
2959	 * but probably only if we're running fairly new firmware (we'll
2960	 * let the old f/w choke on an extended command queue entry).
2961	 */
2962
2963	if (XS_CDBLEN(xs) > (IS_FC(isp)? 16 : 44) || XS_CDBLEN(xs) == 0) {
2964		isp_prt(isp, ISP_LOGERR,
2965		    "unsupported cdb length (%d, CDB[0]=0x%x)",
2966		    XS_CDBLEN(xs), XS_CDBP(xs)[0] & 0xff);
2967		XS_SETERR(xs, HBA_BOTCH);
2968		return (CMD_COMPLETE);
2969	}
2970
2971	/*
2972	 * Check to see whether we have good firmware state still or
2973	 * need to refresh our port database for this target.
2974	 */
2975	target = XS_TGT(xs);
2976	if (IS_FC(isp)) {
2977		fcparam *fcp = isp->isp_param;
2978		struct lportdb *lp;
2979#ifdef	HANDLE_LOOPSTATE_IN_OUTER_LAYERS
2980		if (fcp->isp_fwstate != FW_READY ||
2981		    fcp->isp_loopstate != LOOP_READY) {
2982			return (CMD_RQLATER);
2983		}
2984
2985		/*
2986		 * If we're not on a Fabric, we can't have a target
2987		 * above FL_PORT_ID-1.
2988		 *
2989		 * If we're on a fabric and *not* connected as an F-port,
2990		 * we can't have a target less than FC_SNS_ID+1. This
2991		 * keeps us from having to sort out the difference between
2992		 * local public loop devices and those which we might get
2993		 * from a switch's database.
2994		 */
2995		if (fcp->isp_onfabric == 0) {
2996			if (target >= FL_PORT_ID) {
2997				XS_SETERR(xs, HBA_SELTIMEOUT);
2998				return (CMD_COMPLETE);
2999			}
3000		} else {
3001			if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
3002				XS_SETERR(xs, HBA_SELTIMEOUT);
3003				return (CMD_COMPLETE);
3004			}
3005			/*
3006			 * We used to exclude having local loop ports
3007			 * at the same time that we have fabric ports.
3008			 * That is, we used to exclude having ports
3009			 * at < FL_PORT_ID if we're FL-port.
3010			 *
3011			 * That's wrong. The only thing that could be
3012			 * dicey is if the switch you're connected to
3013			 * has these local loop ports appear on the
3014			 * fabric and we somehow attach them twice.
3015			 */
3016		}
3017#else
3018		/*
3019		 * Check for f/w being in ready state. If the f/w
3020		 * isn't in ready state, then we don't know our
3021		 * loop ID and the f/w hasn't completed logging
3022		 * into all targets on the loop. If this is the
3023		 * case, then bounce the command. We pretend this is
3024		 * a SELECTION TIMEOUT error if we've never gone to
3025		 * FW_READY state at all- in this case we may not
3026		 * be hooked to a loop at all and we shouldn't hang
3027		 * the machine for this. Otherwise, defer this command
3028		 * until later.
3029		 */
3030		if (fcp->isp_fwstate != FW_READY) {
3031			/*
3032			 * Give ourselves at most a 250ms delay.
3033			 */
3034			if (isp_fclink_test(isp, 250000)) {
3035				XS_SETERR(xs, HBA_SELTIMEOUT);
3036				if (fcp->loop_seen_once) {
3037					return (CMD_RQLATER);
3038				} else {
3039					return (CMD_COMPLETE);
3040				}
3041			}
3042		}
3043
3044		/*
3045		 * If we're not on a Fabric, we can't have a target
3046		 * above FL_PORT_ID-1.
3047		 *
3048		 * If we're on a fabric and *not* connected as an F-port,
3049		 * we can't have a target less than FC_SNS_ID+1. This
3050		 * keeps us from having to sort out the difference between
3051		 * local public loop devices and those which we might get
3052		 * from a switch's database.
3053		 */
3054		if (fcp->isp_onfabric == 0) {
3055			if (target >= FL_PORT_ID) {
3056				XS_SETERR(xs, HBA_SELTIMEOUT);
3057				return (CMD_COMPLETE);
3058			}
3059		} else {
3060			if (target >= FL_PORT_ID && target <= FC_SNS_ID) {
3061				XS_SETERR(xs, HBA_SELTIMEOUT);
3062				return (CMD_COMPLETE);
3063			}
3064			if (fcp->isp_topo != TOPO_F_PORT &&
3065			    target < FL_PORT_ID) {
3066				XS_SETERR(xs, HBA_SELTIMEOUT);
3067				return (CMD_COMPLETE);
3068			}
3069		}
3070
3071		/*
3072		 * If our loop state is such that we haven't yet received
3073		 * a "Port Database Changed" notification (after a LIP or
3074		 * a Loop Reset or firmware initialization), then defer
3075		 * sending commands for a little while, but only if we've
3076		 * seen a valid loop at one point (otherwise we can get
3077		 * stuck at initialization time).
3078		 */
3079		if (fcp->isp_loopstate < LOOP_PDB_RCVD) {
3080			XS_SETERR(xs, HBA_SELTIMEOUT);
3081			if (fcp->loop_seen_once) {
3082				return (CMD_RQLATER);
3083			} else {
3084				return (CMD_COMPLETE);
3085			}
3086		}
3087
3088		/*
3089		 * If we're in the middle of loop or fabric scanning
3090		 * or merging the port databases, retry this command later.
3091		 */
3092		if (fcp->isp_loopstate == LOOP_SCANNING_FABRIC ||
3093		    fcp->isp_loopstate == LOOP_SCANNING_LOOP ||
3094		    fcp->isp_loopstate == LOOP_SYNCING_PDB) {
3095			return (CMD_RQLATER);
3096		}
3097
3098		/*
3099		 * If our loop state is now such that we've just now
3100		 * received a Port Database Change notification, then
3101		 * we have to go off and (re)scan the fabric. We back
3102		 * out and try again later if this doesn't work.
3103		 */
3104		if (fcp->isp_loopstate == LOOP_PDB_RCVD && fcp->isp_onfabric) {
3105			if (isp_scan_fabric(isp, FC4_SCSI)) {
3106				return (CMD_RQLATER);
3107			}
3108			if (fcp->isp_fwstate != FW_READY ||
3109			    fcp->isp_loopstate < LOOP_FSCAN_DONE) {
3110				return (CMD_RQLATER);
3111			}
3112		}
3113
3114		/*
3115		 * If our loop state is now such that we've just now
3116		 * received a Port Database Change notification, then
3117		 * we have to go off and (re)synchronize our port
3118		 * database.
3119		 */
3120		if (fcp->isp_loopstate < LOOP_READY) {
3121			if (isp_pdb_sync(isp)) {
3122				return (CMD_RQLATER);
3123			}
3124			if (fcp->isp_fwstate != FW_READY ||
3125			    fcp->isp_loopstate != LOOP_READY) {
3126				return (CMD_RQLATER);
3127			}
3128		}
3129
3130		/*
3131		 * XXX: Here's were we would cancel any loop_dead flag
3132		 * XXX: also cancel in dead_loop timeout that's running
3133		 */
3134#endif
3135
3136		/*
3137		 * Now check whether we should even think about pursuing this.
3138		 */
3139		lp = &fcp->portdb[target];
3140		if (lp->valid == 0) {
3141			XS_SETERR(xs, HBA_SELTIMEOUT);
3142			return (CMD_COMPLETE);
3143		}
3144		if ((lp->roles & (SVC3_TGT_ROLE >> SVC3_ROLE_SHIFT)) == 0) {
3145			isp_prt(isp, ISP_LOGDEBUG2,
3146			    "Target %d does not have target service", target);
3147			XS_SETERR(xs, HBA_SELTIMEOUT);
3148			return (CMD_COMPLETE);
3149		}
3150		/*
3151		 * Now turn target into what the actual Loop ID is.
3152		 */
3153		target = lp->loopid;
3154	}
3155
3156	/*
3157	 * Next check to see if any HBA or Device
3158	 * parameters need to be updated.
3159	 */
3160	if (isp->isp_update != 0) {
3161		isp_update(isp);
3162	}
3163
3164	if (isp_getrqentry(isp, &nxti, &optr, (void *)&qep)) {
3165		isp_prt(isp, ISP_LOGDEBUG0, "Request Queue Overflow");
3166		XS_SETERR(xs, HBA_BOTCH);
3167		return (CMD_EAGAIN);
3168	}
3169
3170	/*
3171	 * Now see if we need to synchronize the ISP with respect to anything.
3172	 * We do dual duty here (cough) for synchronizing for busses other
3173	 * than which we got here to send a command to.
3174	 */
3175	reqp = (ispreq_t *) local;
3176	if (isp->isp_sendmarker) {
3177		u_int8_t n = (IS_DUALBUS(isp)? 2: 1);
3178		/*
3179		 * Check ports to send markers for...
3180		 */
3181		for (i = 0; i < n; i++) {
3182			if ((isp->isp_sendmarker & (1 << i)) == 0) {
3183				continue;
3184			}
3185			MEMZERO((void *) reqp, QENTRY_LEN);
3186			reqp->req_header.rqs_entry_count = 1;
3187			reqp->req_header.rqs_entry_type = RQSTYPE_MARKER;
3188			reqp->req_modifier = SYNC_ALL;
3189			reqp->req_target = i << 7;	/* insert bus number */
3190			isp_put_request(isp, reqp, qep);
3191			ISP_ADD_REQUEST(isp, nxti);
3192			isp->isp_sendmarker &= ~(1 << i);
3193			if (isp_getrqentry(isp, &nxti, &optr, (void *) &qep)) {
3194				isp_prt(isp, ISP_LOGDEBUG0,
3195				    "Request Queue Overflow+");
3196				XS_SETERR(xs, HBA_BOTCH);
3197				return (CMD_EAGAIN);
3198			}
3199		}
3200	}
3201
3202	MEMZERO((void *)reqp, QENTRY_LEN);
3203	reqp->req_header.rqs_entry_count = 1;
3204	if (IS_FC(isp)) {
3205		reqp->req_header.rqs_entry_type = RQSTYPE_T2RQS;
3206	} else {
3207		if (XS_CDBLEN(xs) > 12)
3208			reqp->req_header.rqs_entry_type = RQSTYPE_CMDONLY;
3209		else
3210			reqp->req_header.rqs_entry_type = RQSTYPE_REQUEST;
3211	}
3212	/* reqp->req_header.rqs_flags = 0; */
3213	/* reqp->req_header.rqs_seqno = 0; */
3214	if (IS_FC(isp)) {
3215		/*
3216		 * See comment in isp_intr
3217		 */
3218		/* XS_RESID(xs) = 0; */
3219
3220		/*
3221		 * Fibre Channel always requires some kind of tag.
3222		 * The Qlogic drivers seem be happy not to use a tag,
3223		 * but this breaks for some devices (IBM drives).
3224		 */
3225		if (XS_TAG_P(xs)) {
3226			((ispreqt2_t *)reqp)->req_flags = XS_TAG_TYPE(xs);
3227		} else {
3228			/*
3229			 * If we don't know what tag to use, use HEAD OF QUEUE
3230			 * for Request Sense or Simple.
3231			 */
3232			if (XS_CDBP(xs)[0] == 0x3)	/* REQUEST SENSE */
3233				((ispreqt2_t *)reqp)->req_flags = REQFLAG_HTAG;
3234			else
3235				((ispreqt2_t *)reqp)->req_flags = REQFLAG_STAG;
3236		}
3237	} else {
3238		sdparam *sdp = (sdparam *)isp->isp_param;
3239		sdp += XS_CHANNEL(xs);
3240		if ((sdp->isp_devparam[target].actv_flags & DPARM_TQING) &&
3241		    XS_TAG_P(xs)) {
3242			reqp->req_flags = XS_TAG_TYPE(xs);
3243		}
3244	}
3245	reqp->req_target = target | (XS_CHANNEL(xs) << 7);
3246	if (IS_SCSI(isp)) {
3247		reqp->req_lun_trn = XS_LUN(xs);
3248		reqp->req_cdblen = XS_CDBLEN(xs);
3249	} else {
3250		if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN)
3251			((ispreqt2_t *)reqp)->req_scclun = XS_LUN(xs);
3252		else
3253			((ispreqt2_t *)reqp)->req_lun_trn = XS_LUN(xs);
3254	}
3255	MEMCPY(reqp->req_cdb, XS_CDBP(xs), XS_CDBLEN(xs));
3256
3257	reqp->req_time = XS_TIME(xs) / 1000;
3258	if (reqp->req_time == 0 && XS_TIME(xs)) {
3259		reqp->req_time = 1;
3260	}
3261
3262	if (isp_save_xs(isp, xs, &handle)) {
3263		isp_prt(isp, ISP_LOGDEBUG0, "out of xflist pointers");
3264		XS_SETERR(xs, HBA_BOTCH);
3265		return (CMD_EAGAIN);
3266	}
3267	reqp->req_handle = handle;
3268
3269	/*
3270	 * Set up DMA and/or do any bus swizzling of the request entry
3271	 * so that the Qlogic F/W understands what is being asked of it.
3272	 */
3273	i = ISP_DMASETUP(isp, xs, reqp, &nxti, optr);
3274	if (i != CMD_QUEUED) {
3275		isp_destroy_handle(isp, handle);
3276		/*
3277		 * dmasetup sets actual error in packet, and
3278		 * return what we were given to return.
3279		 */
3280		return (i);
3281	}
3282	XS_SETERR(xs, HBA_NOERROR);
3283	isp_prt(isp, ISP_LOGDEBUG2,
3284	    "START cmd for %d.%d.%d cmd 0x%x datalen %ld",
3285	    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), XS_CDBP(xs)[0],
3286	    (long) XS_XFRLEN(xs));
3287	ISP_ADD_REQUEST(isp, nxti);
3288	isp->isp_nactive++;
3289	return (CMD_QUEUED);
3290}
3291
3292/*
3293 * isp control
3294 * Locks (ints blocked) assumed held.
3295 */
3296
3297int
3298isp_control(struct ispsoftc *isp, ispctl_t ctl, void *arg)
3299{
3300	XS_T *xs;
3301	mbreg_t mbs;
3302	int bus, tgt;
3303	u_int16_t handle;
3304
3305	switch (ctl) {
3306	default:
3307		isp_prt(isp, ISP_LOGERR, "Unknown Control Opcode 0x%x", ctl);
3308		break;
3309
3310	case ISPCTL_RESET_BUS:
3311		/*
3312		 * Issue a bus reset.
3313		 */
3314		mbs.param[0] = MBOX_BUS_RESET;
3315		mbs.param[2] = 0;
3316		if (IS_SCSI(isp)) {
3317			mbs.param[1] =
3318			    ((sdparam *) isp->isp_param)->isp_bus_reset_delay;
3319			if (mbs.param[1] < 2)
3320				mbs.param[1] = 2;
3321			bus = *((int *) arg);
3322			if (IS_DUALBUS(isp))
3323				mbs.param[2] = bus;
3324		} else {
3325			mbs.param[1] = 10;
3326			bus = 0;
3327		}
3328		isp->isp_sendmarker |= (1 << bus);
3329		isp_mboxcmd(isp, &mbs, MBLOGALL);
3330		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3331			break;
3332		}
3333		isp_prt(isp, ISP_LOGINFO,
3334		    "driver initiated bus reset of bus %d", bus);
3335		return (0);
3336
3337	case ISPCTL_RESET_DEV:
3338		tgt = (*((int *) arg)) & 0xffff;
3339		bus = (*((int *) arg)) >> 16;
3340		mbs.param[0] = MBOX_ABORT_TARGET;
3341		mbs.param[1] = (tgt << 8) | (bus << 15);
3342		mbs.param[2] = 3;	/* 'delay', in seconds */
3343		isp_mboxcmd(isp, &mbs, MBLOGALL);
3344		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3345			break;
3346		}
3347		isp_prt(isp, ISP_LOGINFO,
3348		    "Target %d on Bus %d Reset Succeeded", tgt, bus);
3349		isp->isp_sendmarker |= (1 << bus);
3350		return (0);
3351
3352	case ISPCTL_ABORT_CMD:
3353		xs = (XS_T *) arg;
3354		tgt = XS_TGT(xs);
3355		handle = isp_find_handle(isp, xs);
3356		if (handle == 0) {
3357			isp_prt(isp, ISP_LOGWARN,
3358			    "cannot find handle for command to abort");
3359			break;
3360		}
3361		bus = XS_CHANNEL(xs);
3362		mbs.param[0] = MBOX_ABORT;
3363		if (IS_FC(isp)) {
3364			if (FCPARAM(isp)->isp_fwattr & ISP_FW_ATTR_SCCLUN)  {
3365				mbs.param[1] = tgt << 8;
3366				mbs.param[4] = 0;
3367				mbs.param[5] = 0;
3368				mbs.param[6] = XS_LUN(xs);
3369			} else {
3370				mbs.param[1] = tgt << 8 | XS_LUN(xs);
3371			}
3372		} else {
3373			mbs.param[1] =
3374			    (bus << 15) | (XS_TGT(xs) << 8) | XS_LUN(xs);
3375		}
3376		mbs.param[3] = 0;
3377		mbs.param[2] = handle;
3378		isp_mboxcmd(isp, &mbs, MBLOGALL & ~MBOX_COMMAND_ERROR);
3379		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
3380			return (0);
3381		}
3382		/*
3383		 * XXX: Look for command in the REQUEST QUEUE. That is,
3384		 * XXX: It hasen't been picked up by firmware yet.
3385		 */
3386		break;
3387
3388	case ISPCTL_UPDATE_PARAMS:
3389
3390		isp_update(isp);
3391		return (0);
3392
3393	case ISPCTL_FCLINK_TEST:
3394
3395		if (IS_FC(isp)) {
3396			int usdelay = (arg)? *((int *) arg) : 250000;
3397			return (isp_fclink_test(isp, usdelay));
3398		}
3399		break;
3400
3401	case ISPCTL_SCAN_FABRIC:
3402
3403		if (IS_FC(isp)) {
3404			int ftype = (arg)? *((int *) arg) : FC4_SCSI;
3405			return (isp_scan_fabric(isp, ftype));
3406		}
3407		break;
3408
3409	case ISPCTL_SCAN_LOOP:
3410
3411		if (IS_FC(isp)) {
3412			return (isp_scan_loop(isp));
3413		}
3414		break;
3415
3416	case ISPCTL_PDB_SYNC:
3417
3418		if (IS_FC(isp)) {
3419			return (isp_pdb_sync(isp));
3420		}
3421		break;
3422
3423	case ISPCTL_SEND_LIP:
3424
3425		if (IS_FC(isp)) {
3426			mbs.param[0] = MBOX_INIT_LIP;
3427			isp_mboxcmd(isp, &mbs, MBLOGALL);
3428			if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
3429				return (0);
3430			}
3431		}
3432		break;
3433
3434	case ISPCTL_GET_POSMAP:
3435
3436		if (IS_FC(isp) && arg) {
3437			return (isp_getmap(isp, arg));
3438		}
3439		break;
3440
3441	case ISPCTL_RUN_MBOXCMD:
3442
3443		isp_mboxcmd(isp, arg, MBLOGALL);
3444		return(0);
3445
3446#ifdef	ISP_TARGET_MODE
3447	case ISPCTL_TOGGLE_TMODE:
3448	{
3449
3450		/*
3451		 * We don't check/set against role here- that's the
3452		 * responsibility for the outer layer to coordinate.
3453		 */
3454		if (IS_SCSI(isp)) {
3455			int param = *(int *)arg;
3456			mbs.param[0] = MBOX_ENABLE_TARGET_MODE;
3457			mbs.param[1] = param & 0xffff;
3458			mbs.param[2] = param >> 16;
3459			isp_mboxcmd(isp, &mbs, MBLOGALL);
3460			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3461				break;
3462			}
3463		}
3464		return (0);
3465	}
3466#endif
3467	}
3468	return (-1);
3469}
3470
3471/*
3472 * Interrupt Service Routine(s).
3473 *
3474 * External (OS) framework has done the appropriate locking,
3475 * and the locking will be held throughout this function.
3476 */
3477
3478/*
3479 * Limit our stack depth by sticking with the max likely number
3480 * of completions on a request queue at any one time.
3481 */
3482#ifndef	MAX_REQUESTQ_COMPLETIONS
3483#define	MAX_REQUESTQ_COMPLETIONS	64
3484#endif
3485
3486void
3487isp_intr(struct ispsoftc *isp, u_int16_t isr, u_int16_t sema, u_int16_t mbox)
3488{
3489	XS_T *complist[MAX_REQUESTQ_COMPLETIONS], *xs;
3490	u_int16_t iptr, optr, junk;
3491	int i, nlooked = 0, ndone = 0;
3492
3493again:
3494	/*
3495	 * Is this a mailbox related interrupt?
3496	 * The mailbox semaphore will be nonzero if so.
3497	 */
3498	if (sema) {
3499		if (mbox & 0x4000) {
3500			isp->isp_intmboxc++;
3501			if (isp->isp_mboxbsy) {
3502				int i = 0, obits = isp->isp_obits;
3503				isp->isp_mboxtmp[i++] = mbox;
3504				for (i = 1; i < MAX_MAILBOX; i++) {
3505					if ((obits & (1 << i)) == 0) {
3506						continue;
3507					}
3508					isp->isp_mboxtmp[i] =
3509					    ISP_READ(isp, MBOX_OFF(i));
3510				}
3511				if (isp->isp_mbxwrk0) {
3512					if (isp_mbox_continue(isp) == 0) {
3513						return;
3514					}
3515				}
3516				MBOX_NOTIFY_COMPLETE(isp);
3517			} else {
3518				isp_prt(isp, ISP_LOGWARN,
3519				    "Mbox Command Async (0x%x) with no waiters",
3520				    mbox);
3521			}
3522		} else if (isp_parse_async(isp, mbox) < 0) {
3523			return;
3524		}
3525		if ((IS_FC(isp) && mbox != ASYNC_RIO_RESP) ||
3526		    isp->isp_state != ISP_RUNSTATE) {
3527			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3528			ISP_WRITE(isp, BIU_SEMA, 0);
3529			return;
3530		}
3531	}
3532
3533	/*
3534	 * We can't be getting this now.
3535	 */
3536	if (isp->isp_state != ISP_RUNSTATE) {
3537		isp_prt(isp, ISP_LOGWARN,
3538		    "interrupt (ISR=%x SEMA=%x) when not ready", isr, sema);
3539		/*
3540		 * Thank you very much!  *Burrrp*!
3541		 */
3542		WRITE_RESPONSE_QUEUE_OUT_POINTER(isp,
3543		    READ_RESPONSE_QUEUE_IN_POINTER(isp));
3544
3545		ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3546		ISP_WRITE(isp, BIU_SEMA, 0);
3547		return;
3548	}
3549
3550	/*
3551	 * Get the current Response Queue Out Pointer.
3552	 *
3553	 * If we're a 2300, we can ask what hardware what it thinks.
3554	 */
3555	if (IS_23XX(isp)) {
3556		optr = ISP_READ(isp, isp->isp_respoutrp);
3557		/*
3558		 * Debug: to be taken out eventually
3559		 */
3560		if (isp->isp_residx != optr) {
3561			isp_prt(isp, ISP_LOGWARN, "optr %x soft optr %x",
3562			    optr, isp->isp_residx);
3563		}
3564	} else {
3565		optr = isp->isp_residx;
3566	}
3567
3568	/*
3569	 * You *must* read the Response Queue In Pointer
3570	 * prior to clearing the RISC interrupt.
3571	 *
3572	 * Debounce the 2300 if revision less than 2.
3573	 */
3574	if (IS_2100(isp) || (IS_2300(isp) && isp->isp_revision < 2)) {
3575		i = 0;
3576		do {
3577			iptr = READ_RESPONSE_QUEUE_IN_POINTER(isp);
3578			junk = READ_RESPONSE_QUEUE_IN_POINTER(isp);
3579		} while (junk != iptr && ++i < 1000);
3580
3581		if (iptr != junk) {
3582			ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3583			isp_prt(isp, ISP_LOGWARN,
3584			    "Response Queue Out Pointer Unstable (%x, %x)",
3585			    iptr, junk);
3586			return;
3587		}
3588	} else {
3589		iptr = READ_RESPONSE_QUEUE_IN_POINTER(isp);
3590	}
3591	isp->isp_resodx = iptr;
3592
3593
3594	if (optr == iptr && sema == 0) {
3595		/*
3596		 * There are a lot of these- reasons unknown- mostly on
3597		 * faster Alpha machines.
3598		 *
3599		 * I tried delaying after writing HCCR_CMD_CLEAR_RISC_INT to
3600		 * make sure the old interrupt went away (to avoid 'ringing'
3601		 * effects), but that didn't stop this from occurring.
3602		 */
3603		if (IS_23XX(isp)) {
3604			USEC_DELAY(100);
3605			iptr = READ_RESPONSE_QUEUE_IN_POINTER(isp);
3606			junk = ISP_READ(isp, BIU_R2HSTSLO);
3607		} else {
3608			junk = ISP_READ(isp, BIU_ISR);
3609		}
3610		if (optr == iptr) {
3611			if (IS_23XX(isp)) {
3612				;
3613			} else {
3614				sema = ISP_READ(isp, BIU_SEMA);
3615				mbox = ISP_READ(isp, OUTMAILBOX0);
3616				if ((sema & 0x3) && (mbox & 0x8000)) {
3617					goto again;
3618				}
3619			}
3620			isp->isp_intbogus++;
3621			isp_prt(isp, ISP_LOGDEBUG1,
3622			    "bogus intr- isr %x (%x) iptr %x optr %x",
3623			    isr, junk, iptr, optr);
3624		}
3625	}
3626	isp->isp_resodx = iptr;
3627	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
3628	ISP_WRITE(isp, BIU_SEMA, 0);
3629
3630	if (isp->isp_rspbsy) {
3631		return;
3632	}
3633	isp->isp_rspbsy = 1;
3634
3635	while (optr != iptr) {
3636		ispstatusreq_t local, *sp = &local;
3637		isphdr_t *hp;
3638		int type;
3639		u_int16_t oop;
3640		int buddaboom = 0;
3641
3642		hp = (isphdr_t *) ISP_QUEUE_ENTRY(isp->isp_result, optr);
3643		oop = optr;
3644		optr = ISP_NXT_QENTRY(optr, RESULT_QUEUE_LEN(isp));
3645		nlooked++;
3646		/*
3647		 * Synchronize our view of this response queue entry.
3648		 */
3649		MEMORYBARRIER(isp, SYNC_RESULT, oop, QENTRY_LEN);
3650
3651		type = isp_get_response_type(isp, hp);
3652
3653		if (type == RQSTYPE_RESPONSE) {
3654			isp_get_response(isp, (ispstatusreq_t *) hp, sp);
3655		} else if (type == RQSTYPE_RIO2) {
3656			isp_rio2_t rio;
3657			isp_get_rio2(isp, (isp_rio2_t *) hp, &rio);
3658			for (i = 0; i < rio.req_header.rqs_seqno; i++) {
3659				isp_fastpost_complete(isp, rio.req_handles[i]);
3660			}
3661			if (isp->isp_fpcchiwater < rio.req_header.rqs_seqno)
3662				isp->isp_fpcchiwater = rio.req_header.rqs_seqno;
3663			MEMZERO(hp, QENTRY_LEN);	/* PERF */
3664			continue;
3665		} else {
3666			/*
3667			 * Somebody reachable via isp_handle_other_response
3668			 * may have updated the response queue pointers for
3669			 * us, so we reload our goal index.
3670			 */
3671			if (isp_handle_other_response(isp, type, hp, &optr)) {
3672				iptr = isp->isp_resodx;
3673				MEMZERO(hp, QENTRY_LEN);	/* PERF */
3674				continue;
3675			}
3676
3677			/*
3678			 * After this point, we'll just look at the header as
3679			 * we don't know how to deal with the rest of the
3680			 * response.
3681			 */
3682			isp_get_response(isp, (ispstatusreq_t *) hp, sp);
3683
3684			/*
3685			 * It really has to be a bounced request just copied
3686			 * from the request queue to the response queue. If
3687			 * not, something bad has happened.
3688			 */
3689			if (sp->req_header.rqs_entry_type != RQSTYPE_REQUEST) {
3690				isp_prt(isp, ISP_LOGERR, notresp,
3691				    sp->req_header.rqs_entry_type, oop, optr,
3692				    nlooked);
3693				if (isp->isp_dblev & ISP_LOGDEBUG0) {
3694					isp_print_bytes(isp, "Queue Entry",
3695					    QENTRY_LEN, sp);
3696				}
3697				MEMZERO(hp, QENTRY_LEN);	/* PERF */
3698				continue;
3699			}
3700			buddaboom = 1;
3701		}
3702
3703		if (sp->req_header.rqs_flags & 0xf) {
3704#define	_RQS_OFLAGS	\
3705	~(RQSFLAG_CONTINUATION|RQSFLAG_FULL|RQSFLAG_BADHEADER|RQSFLAG_BADPACKET)
3706			if (sp->req_header.rqs_flags & RQSFLAG_CONTINUATION) {
3707				isp_prt(isp, ISP_LOGWARN,
3708				    "continuation segment");
3709				WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
3710				continue;
3711			}
3712			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
3713				isp_prt(isp, ISP_LOGDEBUG1,
3714				    "internal queues full");
3715				/*
3716				 * We'll synthesize a QUEUE FULL message below.
3717				 */
3718			}
3719			if (sp->req_header.rqs_flags & RQSFLAG_BADHEADER) {
3720				isp_prt(isp, ISP_LOGERR,  "bad header flag");
3721				buddaboom++;
3722			}
3723			if (sp->req_header.rqs_flags & RQSFLAG_BADPACKET) {
3724				isp_prt(isp, ISP_LOGERR, "bad request packet");
3725				buddaboom++;
3726			}
3727			if (sp->req_header.rqs_flags & _RQS_OFLAGS) {
3728				isp_prt(isp, ISP_LOGERR,
3729				    "unknown flags (0x%x) in response",
3730				    sp->req_header.rqs_flags);
3731				buddaboom++;
3732			}
3733#undef	_RQS_OFLAGS
3734		}
3735		if (sp->req_handle > isp->isp_maxcmds || sp->req_handle < 1) {
3736			MEMZERO(hp, QENTRY_LEN);	/* PERF */
3737			isp_prt(isp, ISP_LOGERR,
3738			    "bad request handle %d (type 0x%x, flags 0x%x)",
3739			    sp->req_handle, sp->req_header.rqs_entry_type,
3740			    sp->req_header.rqs_flags);
3741			WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
3742			continue;
3743		}
3744		xs = isp_find_xs(isp, sp->req_handle);
3745		if (xs == NULL) {
3746			u_int8_t ts = sp->req_completion_status & 0xff;
3747			MEMZERO(hp, QENTRY_LEN);	/* PERF */
3748			/*
3749			 * Only whine if this isn't the expected fallout of
3750			 * aborting the command.
3751			 */
3752			if (sp->req_header.rqs_entry_type != RQSTYPE_RESPONSE) {
3753				isp_prt(isp, ISP_LOGERR,
3754				    "cannot find handle 0x%x (type 0x%x)",
3755				    sp->req_handle,
3756				    sp->req_header.rqs_entry_type);
3757			} else if (ts != RQCS_ABORTED) {
3758				isp_prt(isp, ISP_LOGERR,
3759				    "cannot find handle 0x%x (status 0x%x)",
3760				    sp->req_handle, ts);
3761			}
3762			WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
3763			continue;
3764		}
3765		isp_destroy_handle(isp, sp->req_handle);
3766		if (sp->req_status_flags & RQSTF_BUS_RESET) {
3767			XS_SETERR(xs, HBA_BUSRESET);
3768			isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
3769		}
3770		if (buddaboom) {
3771			XS_SETERR(xs, HBA_BOTCH);
3772		}
3773
3774		if (IS_FC(isp) && (sp->req_scsi_status & RQCS_SV)) {
3775			/*
3776			 * Fibre Channel F/W doesn't say we got status
3777			 * if there's Sense Data instead. I guess they
3778			 * think it goes w/o saying.
3779			 */
3780			sp->req_state_flags |= RQSF_GOT_STATUS;
3781		}
3782		if (sp->req_state_flags & RQSF_GOT_STATUS) {
3783			*XS_STSP(xs) = sp->req_scsi_status & 0xff;
3784		}
3785
3786		switch (sp->req_header.rqs_entry_type) {
3787		case RQSTYPE_RESPONSE:
3788			XS_SET_STATE_STAT(isp, xs, sp);
3789			isp_parse_status(isp, sp, xs);
3790			if ((XS_NOERR(xs) || XS_ERR(xs) == HBA_NOERROR) &&
3791			    (*XS_STSP(xs) == SCSI_BUSY)) {
3792				XS_SETERR(xs, HBA_TGTBSY);
3793			}
3794			if (IS_SCSI(isp)) {
3795				XS_RESID(xs) = sp->req_resid;
3796				if ((sp->req_state_flags & RQSF_GOT_STATUS) &&
3797				    (*XS_STSP(xs) == SCSI_CHECK) &&
3798				    (sp->req_state_flags & RQSF_GOT_SENSE)) {
3799					XS_SAVE_SENSE(xs, sp);
3800				}
3801				/*
3802				 * A new synchronous rate was negotiated for
3803				 * this target. Mark state such that we'll go
3804				 * look up that which has changed later.
3805				 */
3806				if (sp->req_status_flags & RQSTF_NEGOTIATION) {
3807					int t = XS_TGT(xs);
3808					sdparam *sdp = isp->isp_param;
3809					sdp += XS_CHANNEL(xs);
3810					sdp->isp_devparam[t].dev_refresh = 1;
3811					isp->isp_update |=
3812					    (1 << XS_CHANNEL(xs));
3813				}
3814			} else {
3815				if (sp->req_status_flags & RQSF_XFER_COMPLETE) {
3816					XS_RESID(xs) = 0;
3817				} else if (sp->req_scsi_status & RQCS_RESID) {
3818					XS_RESID(xs) = sp->req_resid;
3819				} else {
3820					XS_RESID(xs) = 0;
3821				}
3822				if ((sp->req_state_flags & RQSF_GOT_STATUS) &&
3823				    (*XS_STSP(xs) == SCSI_CHECK) &&
3824				    (sp->req_scsi_status & RQCS_SV)) {
3825					XS_SAVE_SENSE(xs, sp);
3826					/* solely for the benefit of debug */
3827					sp->req_state_flags |= RQSF_GOT_SENSE;
3828				}
3829			}
3830			isp_prt(isp, ISP_LOGDEBUG2,
3831			   "asked for %ld got resid %ld", (long) XS_XFRLEN(xs),
3832			   (long) sp->req_resid);
3833			break;
3834		case RQSTYPE_REQUEST:
3835			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
3836				/*
3837				 * Force Queue Full status.
3838				 */
3839				*XS_STSP(xs) = SCSI_QFULL;
3840				XS_SETERR(xs, HBA_NOERROR);
3841			} else if (XS_NOERR(xs)) {
3842				/*
3843				 * ????
3844				 */
3845				isp_prt(isp, ISP_LOGDEBUG0,
3846				    "Request Queue Entry bounced back");
3847				XS_SETERR(xs, HBA_BOTCH);
3848			}
3849			XS_RESID(xs) = XS_XFRLEN(xs);
3850			break;
3851		default:
3852			isp_prt(isp, ISP_LOGWARN,
3853			    "unhandled response queue type 0x%x",
3854			    sp->req_header.rqs_entry_type);
3855			if (XS_NOERR(xs)) {
3856				XS_SETERR(xs, HBA_BOTCH);
3857			}
3858			break;
3859		}
3860
3861		/*
3862		 * Free any dma resources. As a side effect, this may
3863		 * also do any cache flushing necessary for data coherence.			 */
3864		if (XS_XFRLEN(xs)) {
3865			ISP_DMAFREE(isp, xs, sp->req_handle);
3866		}
3867
3868		if (((isp->isp_dblev & (ISP_LOGDEBUG2|ISP_LOGDEBUG3))) ||
3869		    ((isp->isp_dblev & ISP_LOGDEBUG1) && ((!XS_NOERR(xs)) ||
3870		    (*XS_STSP(xs) != SCSI_GOOD)))) {
3871			char skey;
3872			if (sp->req_state_flags & RQSF_GOT_SENSE) {
3873				skey = XS_SNSKEY(xs) & 0xf;
3874				if (skey < 10)
3875					skey += '0';
3876				else
3877					skey += 'a' - 10;
3878			} else if (*XS_STSP(xs) == SCSI_CHECK) {
3879				skey = '?';
3880			} else {
3881				skey = '.';
3882			}
3883			isp_prt(isp, ISP_LOGALL, finmsg, XS_CHANNEL(xs),
3884			    XS_TGT(xs), XS_LUN(xs), XS_XFRLEN(xs), XS_RESID(xs),
3885			    *XS_STSP(xs), skey, XS_ERR(xs));
3886		}
3887
3888		if (isp->isp_nactive > 0)
3889		    isp->isp_nactive--;
3890		complist[ndone++] = xs;	/* defer completion call until later */
3891		MEMZERO(hp, QENTRY_LEN);	/* PERF */
3892		if (ndone == MAX_REQUESTQ_COMPLETIONS) {
3893			break;
3894		}
3895	}
3896
3897	/*
3898	 * If we looked at any commands, then it's valid to find out
3899	 * what the outpointer is. It also is a trigger to update the
3900	 * ISP's notion of what we've seen so far.
3901	 */
3902	if (nlooked) {
3903		WRITE_RESPONSE_QUEUE_OUT_POINTER(isp, optr);
3904		/*
3905		 * While we're at it, read the requst queue out pointer.
3906		 */
3907		isp->isp_reqodx = READ_REQUEST_QUEUE_OUT_POINTER(isp);
3908		if (isp->isp_rscchiwater < ndone)
3909			isp->isp_rscchiwater = ndone;
3910	}
3911
3912	isp->isp_residx = optr;
3913	isp->isp_rspbsy = 0;
3914	for (i = 0; i < ndone; i++) {
3915		xs = complist[i];
3916		if (xs) {
3917			isp->isp_rsltccmplt++;
3918			isp_done(xs);
3919		}
3920	}
3921}
3922
3923/*
3924 * Support routines.
3925 */
3926
3927static int
3928isp_parse_async(struct ispsoftc *isp, u_int16_t mbox)
3929{
3930	int rval = 0;
3931	int bus;
3932
3933	if (IS_DUALBUS(isp)) {
3934		bus = ISP_READ(isp, OUTMAILBOX6);
3935	} else {
3936		bus = 0;
3937	}
3938	isp_prt(isp, ISP_LOGDEBUG2, "Async Mbox 0x%x", mbox);
3939
3940	switch (mbox) {
3941	case ASYNC_BUS_RESET:
3942		isp->isp_sendmarker |= (1 << bus);
3943#ifdef	ISP_TARGET_MODE
3944		if (isp_target_async(isp, bus, mbox))
3945			rval = -1;
3946#endif
3947		isp_async(isp, ISPASYNC_BUS_RESET, &bus);
3948		break;
3949	case ASYNC_SYSTEM_ERROR:
3950#ifdef	ISP_FW_CRASH_DUMP
3951		/*
3952		 * If we have crash dumps enabled, it's up to the handler
3953		 * for isp_async to reinit stuff and restart the firmware
3954		 * after performing the crash dump. The reason we do things
3955		 * this way is that we may need to activate a kernel thread
3956		 * to do all the crash dump goop.
3957		 */
3958		isp_async(isp, ISPASYNC_FW_CRASH, NULL);
3959#else
3960		isp_async(isp, ISPASYNC_FW_CRASH, NULL);
3961		isp_reinit(isp);
3962		isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
3963#endif
3964		rval = -1;
3965		break;
3966
3967	case ASYNC_RQS_XFER_ERR:
3968		isp_prt(isp, ISP_LOGERR, "Request Queue Transfer Error");
3969		break;
3970
3971	case ASYNC_RSP_XFER_ERR:
3972		isp_prt(isp, ISP_LOGERR, "Response Queue Transfer Error");
3973		break;
3974
3975	case ASYNC_QWAKEUP:
3976		/*
3977		 * We've just been notified that the Queue has woken up.
3978		 * We don't need to be chatty about this- just unlatch things
3979		 * and move on.
3980		 */
3981		mbox = READ_REQUEST_QUEUE_OUT_POINTER(isp);
3982		break;
3983
3984	case ASYNC_TIMEOUT_RESET:
3985		isp_prt(isp, ISP_LOGWARN,
3986		    "timeout initiated SCSI bus reset of bus %d", bus);
3987		isp->isp_sendmarker |= (1 << bus);
3988#ifdef	ISP_TARGET_MODE
3989		if (isp_target_async(isp, bus, mbox))
3990			rval = -1;
3991#endif
3992		break;
3993
3994	case ASYNC_DEVICE_RESET:
3995		isp_prt(isp, ISP_LOGINFO, "device reset on bus %d", bus);
3996		isp->isp_sendmarker |= (1 << bus);
3997#ifdef	ISP_TARGET_MODE
3998		if (isp_target_async(isp, bus, mbox))
3999			rval = -1;
4000#endif
4001		break;
4002
4003	case ASYNC_EXTMSG_UNDERRUN:
4004		isp_prt(isp, ISP_LOGWARN, "extended message underrun");
4005		break;
4006
4007	case ASYNC_SCAM_INT:
4008		isp_prt(isp, ISP_LOGINFO, "SCAM interrupt");
4009		break;
4010
4011	case ASYNC_HUNG_SCSI:
4012		isp_prt(isp, ISP_LOGERR,
4013		    "stalled SCSI Bus after DATA Overrun");
4014		/* XXX: Need to issue SCSI reset at this point */
4015		break;
4016
4017	case ASYNC_KILLED_BUS:
4018		isp_prt(isp, ISP_LOGERR, "SCSI Bus reset after DATA Overrun");
4019		break;
4020
4021	case ASYNC_BUS_TRANSIT:
4022		mbox = ISP_READ(isp, OUTMAILBOX2);
4023		switch (mbox & 0x1c00) {
4024		case SXP_PINS_LVD_MODE:
4025			isp_prt(isp, ISP_LOGINFO, "Transition to LVD mode");
4026			SDPARAM(isp)->isp_diffmode = 0;
4027			SDPARAM(isp)->isp_ultramode = 0;
4028			SDPARAM(isp)->isp_lvdmode = 1;
4029			break;
4030		case SXP_PINS_HVD_MODE:
4031			isp_prt(isp, ISP_LOGINFO,
4032			    "Transition to Differential mode");
4033			SDPARAM(isp)->isp_diffmode = 1;
4034			SDPARAM(isp)->isp_ultramode = 0;
4035			SDPARAM(isp)->isp_lvdmode = 0;
4036			break;
4037		case SXP_PINS_SE_MODE:
4038			isp_prt(isp, ISP_LOGINFO,
4039			    "Transition to Single Ended mode");
4040			SDPARAM(isp)->isp_diffmode = 0;
4041			SDPARAM(isp)->isp_ultramode = 1;
4042			SDPARAM(isp)->isp_lvdmode = 0;
4043			break;
4044		default:
4045			isp_prt(isp, ISP_LOGWARN,
4046			    "Transition to Unknown Mode 0x%x", mbox);
4047			break;
4048		}
4049		/*
4050		 * XXX: Set up to renegotiate again!
4051		 */
4052		/* Can only be for a 1080... */
4053		isp->isp_sendmarker |= (1 << bus);
4054		break;
4055
4056	/*
4057	 * We can use bus, which will always be zero for FC cards,
4058	 * as a mailbox pattern accumulator to be checked below.
4059	 */
4060	case ASYNC_RIO5:
4061		bus = 0x1ce;	/* outgoing mailbox regs 1-3, 6-7 */
4062		break;
4063
4064	case ASYNC_RIO4:
4065		bus = 0x14e;	/* outgoing mailbox regs 1-3, 6 */
4066		break;
4067
4068	case ASYNC_RIO3:
4069		bus = 0x10e;	/* outgoing mailbox regs 1-3 */
4070		break;
4071
4072	case ASYNC_RIO2:
4073		bus = 0x106;	/* outgoing mailbox regs 1-2 */
4074		break;
4075
4076	case ASYNC_RIO1:
4077	case ASYNC_CMD_CMPLT:
4078		bus = 0x102;	/* outgoing mailbox regs 1 */
4079		break;
4080
4081	case ASYNC_RIO_RESP:
4082		return (rval);
4083
4084	case ASYNC_CTIO_DONE:
4085	{
4086#ifdef	ISP_TARGET_MODE
4087		int handle =
4088		    (ISP_READ(isp, OUTMAILBOX2) << 16) |
4089		    (ISP_READ(isp, OUTMAILBOX1));
4090		if (isp_target_async(isp, handle, mbox))
4091			rval = -1;
4092#else
4093		isp_prt(isp, ISP_LOGINFO, "Fast Posting CTIO done");
4094#endif
4095		isp->isp_fphccmplt++;	/* count it as a fast posting intr */
4096		break;
4097	}
4098	case ASYNC_LIP_F8:
4099	case ASYNC_LIP_OCCURRED:
4100		FCPARAM(isp)->isp_lipseq =
4101		    ISP_READ(isp, OUTMAILBOX1);
4102		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4103		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
4104		isp->isp_sendmarker = 1;
4105		isp_mark_getpdb_all(isp);
4106		isp_async(isp, ISPASYNC_LIP, NULL);
4107#ifdef	ISP_TARGET_MODE
4108		if (isp_target_async(isp, bus, mbox))
4109			rval = -1;
4110#endif
4111		/*
4112		 * We've had problems with data corruption occuring on
4113		 * commands that complete (with no apparent error) after
4114		 * we receive a LIP. This has been observed mostly on
4115		 * Local Loop topologies. To be safe, let's just mark
4116		 * all active commands as dead.
4117		 */
4118		if (FCPARAM(isp)->isp_topo == TOPO_NL_PORT ||
4119		    FCPARAM(isp)->isp_topo == TOPO_FL_PORT) {
4120			int i, j;
4121			for (i = j = 0; i < isp->isp_maxcmds; i++) {
4122				XS_T *xs;
4123				xs = isp->isp_xflist[i];
4124				if (xs != NULL) {
4125					j++;
4126					XS_SETERR(xs, HBA_BUSRESET);
4127				}
4128			}
4129			if (j) {
4130				isp_prt(isp, ISP_LOGERR,
4131				    "LIP destroyed %d active commands", j);
4132			}
4133		}
4134		break;
4135
4136	case ASYNC_LOOP_UP:
4137		isp->isp_sendmarker = 1;
4138		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4139		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
4140		isp_mark_getpdb_all(isp);
4141		isp_async(isp, ISPASYNC_LOOP_UP, NULL);
4142#ifdef	ISP_TARGET_MODE
4143		if (isp_target_async(isp, bus, mbox))
4144			rval = -1;
4145#endif
4146		break;
4147
4148	case ASYNC_LOOP_DOWN:
4149		isp->isp_sendmarker = 1;
4150		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4151		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
4152		isp_mark_getpdb_all(isp);
4153		isp_async(isp, ISPASYNC_LOOP_DOWN, NULL);
4154#ifdef	ISP_TARGET_MODE
4155		if (isp_target_async(isp, bus, mbox))
4156			rval = -1;
4157#endif
4158		break;
4159
4160	case ASYNC_LOOP_RESET:
4161		isp->isp_sendmarker = 1;
4162		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4163		FCPARAM(isp)->isp_loopstate = LOOP_NIL;
4164		isp_mark_getpdb_all(isp);
4165		isp_async(isp, ISPASYNC_LOOP_RESET, NULL);
4166#ifdef	ISP_TARGET_MODE
4167		if (isp_target_async(isp, bus, mbox))
4168			rval = -1;
4169#endif
4170		break;
4171
4172	case ASYNC_PDB_CHANGED:
4173		isp->isp_sendmarker = 1;
4174		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
4175		isp_mark_getpdb_all(isp);
4176		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_PDB);
4177		break;
4178
4179	case ASYNC_CHANGE_NOTIFY:
4180		/*
4181		 * Not correct, but it will force us to rescan the loop.
4182		 */
4183		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
4184		isp_mark_getpdb_all(isp);
4185		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_SNS);
4186		break;
4187
4188	case ASYNC_PTPMODE:
4189		if (FCPARAM(isp)->isp_onfabric)
4190			FCPARAM(isp)->isp_topo = TOPO_F_PORT;
4191		else
4192			FCPARAM(isp)->isp_topo = TOPO_N_PORT;
4193		isp_mark_getpdb_all(isp);
4194		isp->isp_sendmarker = 1;
4195		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4196		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
4197		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
4198#ifdef	ISP_TARGET_MODE
4199		if (isp_target_async(isp, bus, mbox))
4200			rval = -1;
4201#endif
4202		isp_prt(isp, ISP_LOGINFO, "Point-to-Point mode");
4203		break;
4204
4205	case ASYNC_CONNMODE:
4206		mbox = ISP_READ(isp, OUTMAILBOX1);
4207		isp_mark_getpdb_all(isp);
4208		switch (mbox) {
4209		case ISP_CONN_LOOP:
4210			isp_prt(isp, ISP_LOGINFO,
4211			    "Point-to-Point -> Loop mode");
4212			break;
4213		case ISP_CONN_PTP:
4214			isp_prt(isp, ISP_LOGINFO,
4215			    "Loop -> Point-to-Point mode");
4216			break;
4217		case ISP_CONN_BADLIP:
4218			isp_prt(isp, ISP_LOGWARN,
4219			    "Point-to-Point -> Loop mode (BAD LIP)");
4220			break;
4221		case ISP_CONN_FATAL:
4222			isp_prt(isp, ISP_LOGERR, "FATAL CONNECTION ERROR");
4223#ifdef	ISP_FW_CRASH_DUMP
4224			isp_async(isp, ISPASYNC_FW_CRASH, NULL);
4225#else
4226			isp_async(isp, ISPASYNC_FW_CRASH, NULL);
4227			isp_reinit(isp);
4228			isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
4229#endif
4230			return (-1);
4231		case ISP_CONN_LOOPBACK:
4232			isp_prt(isp, ISP_LOGWARN,
4233			    "Looped Back in Point-to-Point mode");
4234			break;
4235		default:
4236			isp_prt(isp, ISP_LOGWARN,
4237			    "Unknown connection mode (0x%x)", mbox);
4238			break;
4239		}
4240		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
4241		isp->isp_sendmarker = 1;
4242		FCPARAM(isp)->isp_fwstate = FW_CONFIG_WAIT;
4243		FCPARAM(isp)->isp_loopstate = LOOP_LIP_RCVD;
4244		break;
4245
4246	default:
4247		isp_prt(isp, ISP_LOGWARN, "Unknown Async Code 0x%x", mbox);
4248		break;
4249	}
4250
4251	if (bus & 0x100) {
4252		int i, nh;
4253		u_int16_t handles[5];
4254
4255		for (nh = 0, i = 1; i < MAX_MAILBOX; i++) {
4256			if ((bus & (1 << i)) == 0) {
4257				continue;
4258			}
4259			handles[nh++] = ISP_READ(isp, MBOX_OFF(i));
4260		}
4261		for (i = 0; i < nh; i++) {
4262			isp_fastpost_complete(isp, handles[i]);
4263			isp_prt(isp,  ISP_LOGDEBUG3,
4264			    "fast post completion of %u", handles[i]);
4265		}
4266		if (isp->isp_fpcchiwater < nh)
4267			isp->isp_fpcchiwater = nh;
4268	} else {
4269		isp->isp_intoasync++;
4270	}
4271	return (rval);
4272}
4273
4274/*
4275 * Handle other response entries. A pointer to the request queue output
4276 * index is here in case we want to eat several entries at once, although
4277 * this is not used currently.
4278 */
4279
4280static int
4281isp_handle_other_response(struct ispsoftc *isp, int type,
4282    isphdr_t *hp, u_int16_t *optrp)
4283{
4284	switch (type) {
4285	case RQSTYPE_STATUS_CONT:
4286		isp_prt(isp, ISP_LOGINFO, "Ignored Continuation Response");
4287		return (1);
4288	case RQSTYPE_ATIO:
4289	case RQSTYPE_CTIO:
4290	case RQSTYPE_ENABLE_LUN:
4291	case RQSTYPE_MODIFY_LUN:
4292	case RQSTYPE_NOTIFY:
4293	case RQSTYPE_NOTIFY_ACK:
4294	case RQSTYPE_CTIO1:
4295	case RQSTYPE_ATIO2:
4296	case RQSTYPE_CTIO2:
4297	case RQSTYPE_CTIO3:
4298		isp->isp_rsltccmplt++;	/* count as a response completion */
4299#ifdef	ISP_TARGET_MODE
4300		if (isp_target_notify(isp, (ispstatusreq_t *) hp, optrp)) {
4301			return (1);
4302		}
4303#endif
4304		/* FALLTHROUGH */
4305	case RQSTYPE_REQUEST:
4306	default:
4307		if (isp_async(isp, ISPASYNC_UNHANDLED_RESPONSE, hp)) {
4308			return (1);
4309		}
4310		isp_prt(isp, ISP_LOGWARN, "Unhandled Response Type 0x%x",
4311		    isp_get_response_type(isp, hp));
4312		return (0);
4313	}
4314}
4315
4316static void
4317isp_parse_status(struct ispsoftc *isp, ispstatusreq_t *sp, XS_T *xs)
4318{
4319	switch (sp->req_completion_status & 0xff) {
4320	case RQCS_COMPLETE:
4321		if (XS_NOERR(xs)) {
4322			XS_SETERR(xs, HBA_NOERROR);
4323		}
4324		return;
4325
4326	case RQCS_INCOMPLETE:
4327		if ((sp->req_state_flags & RQSF_GOT_TARGET) == 0) {
4328			isp_prt(isp, ISP_LOGDEBUG1,
4329			    "Selection Timeout for %d.%d.%d",
4330			    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4331			if (XS_NOERR(xs)) {
4332				XS_SETERR(xs, HBA_SELTIMEOUT);
4333			}
4334			return;
4335		}
4336		isp_prt(isp, ISP_LOGERR,
4337		    "command incomplete for %d.%d.%d, state 0x%x",
4338		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs),
4339		    sp->req_state_flags);
4340		break;
4341
4342	case RQCS_DMA_ERROR:
4343		isp_prt(isp, ISP_LOGERR, "DMA error for command on %d.%d.%d",
4344		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4345		break;
4346
4347	case RQCS_TRANSPORT_ERROR:
4348	{
4349		char buf[172];
4350		SNPRINTF(buf, sizeof (buf), "states=>");
4351		if (sp->req_state_flags & RQSF_GOT_BUS) {
4352			SNPRINTF(buf, sizeof (buf), "%s GOT_BUS", buf);
4353		}
4354		if (sp->req_state_flags & RQSF_GOT_TARGET) {
4355			SNPRINTF(buf, sizeof (buf), "%s GOT_TGT", buf);
4356		}
4357		if (sp->req_state_flags & RQSF_SENT_CDB) {
4358			SNPRINTF(buf, sizeof (buf), "%s SENT_CDB", buf);
4359		}
4360		if (sp->req_state_flags & RQSF_XFRD_DATA) {
4361			SNPRINTF(buf, sizeof (buf), "%s XFRD_DATA", buf);
4362		}
4363		if (sp->req_state_flags & RQSF_GOT_STATUS) {
4364			SNPRINTF(buf, sizeof (buf), "%s GOT_STS", buf);
4365		}
4366		if (sp->req_state_flags & RQSF_GOT_SENSE) {
4367			SNPRINTF(buf, sizeof (buf), "%s GOT_SNS", buf);
4368		}
4369		if (sp->req_state_flags & RQSF_XFER_COMPLETE) {
4370			SNPRINTF(buf, sizeof (buf), "%s XFR_CMPLT", buf);
4371		}
4372		SNPRINTF(buf, sizeof (buf), "%s\nstatus=>", buf);
4373		if (sp->req_status_flags & RQSTF_DISCONNECT) {
4374			SNPRINTF(buf, sizeof (buf), "%s Disconnect", buf);
4375		}
4376		if (sp->req_status_flags & RQSTF_SYNCHRONOUS) {
4377			SNPRINTF(buf, sizeof (buf), "%s Sync_xfr", buf);
4378		}
4379		if (sp->req_status_flags & RQSTF_PARITY_ERROR) {
4380			SNPRINTF(buf, sizeof (buf), "%s Parity", buf);
4381		}
4382		if (sp->req_status_flags & RQSTF_BUS_RESET) {
4383			SNPRINTF(buf, sizeof (buf), "%s Bus_Reset", buf);
4384		}
4385		if (sp->req_status_flags & RQSTF_DEVICE_RESET) {
4386			SNPRINTF(buf, sizeof (buf), "%s Device_Reset", buf);
4387		}
4388		if (sp->req_status_flags & RQSTF_ABORTED) {
4389			SNPRINTF(buf, sizeof (buf), "%s Aborted", buf);
4390		}
4391		if (sp->req_status_flags & RQSTF_TIMEOUT) {
4392			SNPRINTF(buf, sizeof (buf), "%s Timeout", buf);
4393		}
4394		if (sp->req_status_flags & RQSTF_NEGOTIATION) {
4395			SNPRINTF(buf, sizeof (buf), "%s Negotiation", buf);
4396		}
4397		isp_prt(isp, ISP_LOGERR, "%s", buf);
4398		isp_prt(isp, ISP_LOGERR, "transport error for %d.%d.%d:\n%s",
4399		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), buf);
4400		break;
4401	}
4402	case RQCS_RESET_OCCURRED:
4403		isp_prt(isp, ISP_LOGWARN,
4404		    "bus reset destroyed command for %d.%d.%d",
4405		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4406		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
4407		if (XS_NOERR(xs)) {
4408			XS_SETERR(xs, HBA_BUSRESET);
4409		}
4410		return;
4411
4412	case RQCS_ABORTED:
4413		isp_prt(isp, ISP_LOGERR, "command aborted for %d.%d.%d",
4414		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4415		isp->isp_sendmarker |= (1 << XS_CHANNEL(xs));
4416		if (XS_NOERR(xs)) {
4417			XS_SETERR(xs, HBA_ABORTED);
4418		}
4419		return;
4420
4421	case RQCS_TIMEOUT:
4422		isp_prt(isp, ISP_LOGWARN, "command timed out for %d.%d.%d",
4423		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4424		/*
4425	 	 * Check to see if we logged out the device.
4426		 */
4427		if (IS_FC(isp)) {
4428			if ((sp->req_completion_status & RQSTF_LOGOUT) &&
4429			    FCPARAM(isp)->portdb[XS_TGT(xs)].valid &&
4430			    FCPARAM(isp)->portdb[XS_TGT(xs)].fabric_dev) {
4431				FCPARAM(isp)->portdb[XS_TGT(xs)].relogin = 1;
4432			}
4433		}
4434		if (XS_NOERR(xs)) {
4435			XS_SETERR(xs, HBA_CMDTIMEOUT);
4436		}
4437		return;
4438
4439	case RQCS_DATA_OVERRUN:
4440		XS_RESID(xs) = sp->req_resid;
4441		isp_prt(isp, ISP_LOGERR, "data overrun for command on %d.%d.%d",
4442		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4443		if (XS_NOERR(xs)) {
4444			XS_SETERR(xs, HBA_DATAOVR);
4445		}
4446		return;
4447
4448	case RQCS_COMMAND_OVERRUN:
4449		isp_prt(isp, ISP_LOGERR,
4450		    "command overrun for command on %d.%d.%d",
4451		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4452		break;
4453
4454	case RQCS_STATUS_OVERRUN:
4455		isp_prt(isp, ISP_LOGERR,
4456		    "status overrun for command on %d.%d.%d",
4457		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4458		break;
4459
4460	case RQCS_BAD_MESSAGE:
4461		isp_prt(isp, ISP_LOGERR,
4462		    "msg not COMMAND COMPLETE after status %d.%d.%d",
4463		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4464		break;
4465
4466	case RQCS_NO_MESSAGE_OUT:
4467		isp_prt(isp, ISP_LOGERR,
4468		    "No MESSAGE OUT phase after selection on %d.%d.%d",
4469		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4470		break;
4471
4472	case RQCS_EXT_ID_FAILED:
4473		isp_prt(isp, ISP_LOGERR, "EXTENDED IDENTIFY failed %d.%d.%d",
4474		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4475		break;
4476
4477	case RQCS_IDE_MSG_FAILED:
4478		isp_prt(isp, ISP_LOGERR,
4479		    "INITIATOR DETECTED ERROR rejected by %d.%d.%d",
4480		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4481		break;
4482
4483	case RQCS_ABORT_MSG_FAILED:
4484		isp_prt(isp, ISP_LOGERR, "ABORT OPERATION rejected by %d.%d.%d",
4485		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4486		break;
4487
4488	case RQCS_REJECT_MSG_FAILED:
4489		isp_prt(isp, ISP_LOGERR, "MESSAGE REJECT rejected by %d.%d.%d",
4490		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4491		break;
4492
4493	case RQCS_NOP_MSG_FAILED:
4494		isp_prt(isp, ISP_LOGERR, "NOP rejected by %d.%d.%d",
4495		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4496		break;
4497
4498	case RQCS_PARITY_ERROR_MSG_FAILED:
4499		isp_prt(isp, ISP_LOGERR,
4500		    "MESSAGE PARITY ERROR rejected by %d.%d.%d",
4501		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4502		break;
4503
4504	case RQCS_DEVICE_RESET_MSG_FAILED:
4505		isp_prt(isp, ISP_LOGWARN,
4506		    "BUS DEVICE RESET rejected by %d.%d.%d",
4507		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4508		break;
4509
4510	case RQCS_ID_MSG_FAILED:
4511		isp_prt(isp, ISP_LOGERR, "IDENTIFY rejected by %d.%d.%d",
4512		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4513		break;
4514
4515	case RQCS_UNEXP_BUS_FREE:
4516		isp_prt(isp, ISP_LOGERR, "%d.%d.%d had an unexpected bus free",
4517		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4518		break;
4519
4520	case RQCS_DATA_UNDERRUN:
4521	{
4522		if (IS_FC(isp)) {
4523			int ru_marked = (sp->req_scsi_status & RQCS_RU) != 0;
4524			if (!ru_marked || sp->req_resid > XS_XFRLEN(xs)) {
4525				isp_prt(isp, ISP_LOGWARN, bun, XS_TGT(xs),
4526				    XS_LUN(xs), XS_XFRLEN(xs), sp->req_resid,
4527				    (ru_marked)? "marked" : "not marked");
4528				if (XS_NOERR(xs)) {
4529					XS_SETERR(xs, HBA_BOTCH);
4530				}
4531				return;
4532			}
4533		}
4534		XS_RESID(xs) = sp->req_resid;
4535		if (XS_NOERR(xs)) {
4536			XS_SETERR(xs, HBA_NOERROR);
4537		}
4538		return;
4539	}
4540
4541	case RQCS_XACT_ERR1:
4542		isp_prt(isp, ISP_LOGERR, xact1, XS_CHANNEL(xs),
4543		    XS_TGT(xs), XS_LUN(xs));
4544		break;
4545
4546	case RQCS_XACT_ERR2:
4547		isp_prt(isp, ISP_LOGERR, xact2,
4548		    XS_LUN(xs), XS_TGT(xs), XS_CHANNEL(xs));
4549		break;
4550
4551	case RQCS_XACT_ERR3:
4552		isp_prt(isp, ISP_LOGERR, xact3,
4553		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4554		break;
4555
4556	case RQCS_BAD_ENTRY:
4557		isp_prt(isp, ISP_LOGERR, "Invalid IOCB entry type detected");
4558		break;
4559
4560	case RQCS_QUEUE_FULL:
4561		isp_prt(isp, ISP_LOGDEBUG0,
4562		    "internal queues full for %d.%d.%d status 0x%x",
4563		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), *XS_STSP(xs));
4564
4565		/*
4566		 * If QFULL or some other status byte is set, then this
4567		 * isn't an error, per se.
4568		 *
4569		 * Unfortunately, some QLogic f/w writers have, in
4570		 * some cases, ommitted to *set* status to QFULL.
4571		 *
4572
4573		if (*XS_STSP(xs) != SCSI_GOOD && XS_NOERR(xs)) {
4574			XS_SETERR(xs, HBA_NOERROR);
4575			return;
4576		}
4577
4578		 *
4579		 *
4580		 */
4581
4582		*XS_STSP(xs) = SCSI_QFULL;
4583		XS_SETERR(xs, HBA_NOERROR);
4584		return;
4585
4586	case RQCS_PHASE_SKIPPED:
4587		isp_prt(isp, ISP_LOGERR, pskip, XS_CHANNEL(xs),
4588		    XS_TGT(xs), XS_LUN(xs));
4589		break;
4590
4591	case RQCS_ARQS_FAILED:
4592		isp_prt(isp, ISP_LOGERR,
4593		    "Auto Request Sense failed for %d.%d.%d",
4594		    XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
4595		if (XS_NOERR(xs)) {
4596			XS_SETERR(xs, HBA_ARQFAIL);
4597		}
4598		return;
4599
4600	case RQCS_WIDE_FAILED:
4601		isp_prt(isp, ISP_LOGERR,
4602		    "Wide Negotiation failed for %d.%d.%d",
4603		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
4604		if (IS_SCSI(isp)) {
4605			sdparam *sdp = isp->isp_param;
4606			sdp += XS_CHANNEL(xs);
4607			sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_WIDE;
4608			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
4609			isp->isp_update |= (1 << XS_CHANNEL(xs));
4610		}
4611		if (XS_NOERR(xs)) {
4612			XS_SETERR(xs, HBA_NOERROR);
4613		}
4614		return;
4615
4616	case RQCS_SYNCXFER_FAILED:
4617		isp_prt(isp, ISP_LOGERR,
4618		    "SDTR Message failed for target %d.%d.%d",
4619		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
4620		if (IS_SCSI(isp)) {
4621			sdparam *sdp = isp->isp_param;
4622			sdp += XS_CHANNEL(xs);
4623			sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_SYNC;
4624			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
4625			isp->isp_update |= (1 << XS_CHANNEL(xs));
4626		}
4627		break;
4628
4629	case RQCS_LVD_BUSERR:
4630		isp_prt(isp, ISP_LOGERR,
4631		    "Bad LVD condition while talking to %d.%d.%d",
4632		    XS_TGT(xs), XS_LUN(xs), XS_CHANNEL(xs));
4633		break;
4634
4635	case RQCS_PORT_UNAVAILABLE:
4636		/*
4637		 * No such port on the loop. Moral equivalent of SELTIMEO
4638		 */
4639	case RQCS_PORT_LOGGED_OUT:
4640		/*
4641		 * It was there (maybe)- treat as a selection timeout.
4642		 */
4643		if ((sp->req_completion_status & 0xff) == RQCS_PORT_UNAVAILABLE)
4644			isp_prt(isp, ISP_LOGINFO,
4645			    "port unavailable for target %d", XS_TGT(xs));
4646		else
4647			isp_prt(isp, ISP_LOGINFO,
4648			    "port logout for target %d", XS_TGT(xs));
4649		/*
4650		 * If we're on a local loop, force a LIP (which is overkill)
4651		 * to force a re-login of this unit. If we're on fabric,
4652		 * then we'll have to relogin as a matter of course.
4653		 */
4654		if (FCPARAM(isp)->isp_topo == TOPO_NL_PORT ||
4655		    FCPARAM(isp)->isp_topo == TOPO_FL_PORT) {
4656			mbreg_t mbs;
4657			mbs.param[0] = MBOX_INIT_LIP;
4658			isp_mboxcmd_qnw(isp, &mbs, 1);
4659		}
4660
4661		/*
4662		 * Probably overkill.
4663		 */
4664		isp->isp_sendmarker = 1;
4665		FCPARAM(isp)->isp_loopstate = LOOP_PDB_RCVD;
4666		isp_mark_getpdb_all(isp);
4667		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, ISPASYNC_CHANGE_OTHER);
4668		if (XS_NOERR(xs)) {
4669			XS_SETERR(xs, HBA_SELTIMEOUT);
4670		}
4671		return;
4672
4673	case RQCS_PORT_CHANGED:
4674		isp_prt(isp, ISP_LOGWARN,
4675		    "port changed for target %d", XS_TGT(xs));
4676		if (XS_NOERR(xs)) {
4677			XS_SETERR(xs, HBA_SELTIMEOUT);
4678		}
4679		return;
4680
4681	case RQCS_PORT_BUSY:
4682		isp_prt(isp, ISP_LOGWARN,
4683		    "port busy for target %d", XS_TGT(xs));
4684		if (XS_NOERR(xs)) {
4685			XS_SETERR(xs, HBA_TGTBSY);
4686		}
4687		return;
4688
4689	default:
4690		isp_prt(isp, ISP_LOGERR, "Unknown Completion Status 0x%x",
4691		    sp->req_completion_status);
4692		break;
4693	}
4694	if (XS_NOERR(xs)) {
4695		XS_SETERR(xs, HBA_BOTCH);
4696	}
4697}
4698
4699static void
4700isp_fastpost_complete(struct ispsoftc *isp, u_int16_t fph)
4701{
4702	XS_T *xs;
4703
4704	if (fph == 0) {
4705		return;
4706	}
4707	xs = isp_find_xs(isp, fph);
4708	if (xs == NULL) {
4709		isp_prt(isp, ISP_LOGWARN,
4710		    "Command for fast post handle 0x%x not found", fph);
4711		return;
4712	}
4713	isp_destroy_handle(isp, fph);
4714
4715	/*
4716	 * Since we don't have a result queue entry item,
4717	 * we must believe that SCSI status is zero and
4718	 * that all data transferred.
4719	 */
4720	XS_SET_STATE_STAT(isp, xs, NULL);
4721	XS_RESID(xs) = 0;
4722	*XS_STSP(xs) = SCSI_GOOD;
4723	if (XS_XFRLEN(xs)) {
4724		ISP_DMAFREE(isp, xs, fph);
4725	}
4726	if (isp->isp_nactive)
4727		isp->isp_nactive--;
4728	isp->isp_fphccmplt++;
4729	isp_done(xs);
4730}
4731
4732static int
4733isp_mbox_continue(struct ispsoftc *isp)
4734{
4735	mbreg_t mbs;
4736	u_int16_t *ptr;
4737
4738	switch (isp->isp_lastmbxcmd) {
4739	case MBOX_WRITE_RAM_WORD:
4740	case MBOX_READ_RAM_WORD:
4741	case MBOX_READ_RAM_WORD_EXTENDED:
4742		break;
4743	default:
4744		return (1);
4745	}
4746	if (isp->isp_mboxtmp[0] != MBOX_COMMAND_COMPLETE) {
4747		isp->isp_mbxwrk0 = 0;
4748		return (-1);
4749	}
4750
4751
4752	/*
4753	 * Clear the previous interrupt.
4754	 */
4755	ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
4756	ISP_WRITE(isp, BIU_SEMA, 0);
4757
4758	/*
4759	 * Continue with next word.
4760	 */
4761	ptr = isp->isp_mbxworkp;
4762	switch (isp->isp_lastmbxcmd) {
4763	case MBOX_WRITE_RAM_WORD:
4764		mbs.param[2] = *ptr++;
4765		mbs.param[1] = isp->isp_mbxwrk1++;
4766		break;
4767	case MBOX_READ_RAM_WORD:
4768	case MBOX_READ_RAM_WORD_EXTENDED:
4769		*ptr++ = isp->isp_mboxtmp[2];
4770		mbs.param[1] = isp->isp_mbxwrk1++;
4771		break;
4772	}
4773	isp->isp_mbxworkp = ptr;
4774	mbs.param[0] = isp->isp_lastmbxcmd;
4775	isp->isp_mbxwrk0 -= 1;
4776	isp_mboxcmd_qnw(isp, &mbs, 0);
4777	return (0);
4778}
4779
4780
4781#define	HIBYT(x)			((x) >> 0x8)
4782#define	LOBYT(x)			((x)  & 0xff)
4783#define	ISPOPMAP(a, b)			(((a) << 8) | (b))
4784static const u_int16_t mbpscsi[] = {
4785	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
4786	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
4787	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
4788	ISPOPMAP(0x1f, 0x01),	/* 0x03: MBOX_DUMP_RAM */
4789	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
4790	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
4791	ISPOPMAP(0x3f, 0x3f),	/* 0x06: MBOX_MAILBOX_REG_TEST */
4792	ISPOPMAP(0x03, 0x07),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
4793	ISPOPMAP(0x01, 0x0f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
4794	ISPOPMAP(0x00, 0x00),	/* 0x09: */
4795	ISPOPMAP(0x00, 0x00),	/* 0x0a: */
4796	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
4797	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
4798	ISPOPMAP(0x00, 0x00),	/* 0x0d: */
4799	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
4800	ISPOPMAP(0x00, 0x00),	/* 0x0f: */
4801	ISPOPMAP(0x1f, 0x1f),	/* 0x10: MBOX_INIT_REQ_QUEUE */
4802	ISPOPMAP(0x3f, 0x3f),	/* 0x11: MBOX_INIT_RES_QUEUE */
4803	ISPOPMAP(0x0f, 0x0f),	/* 0x12: MBOX_EXECUTE_IOCB */
4804	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
4805	ISPOPMAP(0x01, 0x3f),	/* 0x14: MBOX_STOP_FIRMWARE */
4806	ISPOPMAP(0x0f, 0x0f),	/* 0x15: MBOX_ABORT */
4807	ISPOPMAP(0x03, 0x03),	/* 0x16: MBOX_ABORT_DEVICE */
4808	ISPOPMAP(0x07, 0x07),	/* 0x17: MBOX_ABORT_TARGET */
4809	ISPOPMAP(0x07, 0x07),	/* 0x18: MBOX_BUS_RESET */
4810	ISPOPMAP(0x03, 0x07),	/* 0x19: MBOX_STOP_QUEUE */
4811	ISPOPMAP(0x03, 0x07),	/* 0x1a: MBOX_START_QUEUE */
4812	ISPOPMAP(0x03, 0x07),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
4813	ISPOPMAP(0x03, 0x07),	/* 0x1c: MBOX_ABORT_QUEUE */
4814	ISPOPMAP(0x03, 0x4f),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
4815	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
4816	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
4817	ISPOPMAP(0x01, 0x07),	/* 0x20: MBOX_GET_INIT_SCSI_ID */
4818	ISPOPMAP(0x01, 0x07),	/* 0x21: MBOX_GET_SELECT_TIMEOUT */
4819	ISPOPMAP(0x01, 0xc7),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
4820	ISPOPMAP(0x01, 0x07),	/* 0x23: MBOX_GET_TAG_AGE_LIMIT */
4821	ISPOPMAP(0x01, 0x03),	/* 0x24: MBOX_GET_CLOCK_RATE */
4822	ISPOPMAP(0x01, 0x07),	/* 0x25: MBOX_GET_ACT_NEG_STATE */
4823	ISPOPMAP(0x01, 0x07),	/* 0x26: MBOX_GET_ASYNC_DATA_SETUP_TIME */
4824	ISPOPMAP(0x01, 0x07),	/* 0x27: MBOX_GET_PCI_PARAMS */
4825	ISPOPMAP(0x03, 0x4f),	/* 0x28: MBOX_GET_TARGET_PARAMS */
4826	ISPOPMAP(0x03, 0x0f),	/* 0x29: MBOX_GET_DEV_QUEUE_PARAMS */
4827	ISPOPMAP(0x01, 0x07),	/* 0x2a: MBOX_GET_RESET_DELAY_PARAMS */
4828	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
4829	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
4830	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
4831	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
4832	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
4833	ISPOPMAP(0x03, 0x03),	/* 0x30: MBOX_SET_INIT_SCSI_ID */
4834	ISPOPMAP(0x07, 0x07),	/* 0x31: MBOX_SET_SELECT_TIMEOUT */
4835	ISPOPMAP(0xc7, 0xc7),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
4836	ISPOPMAP(0x07, 0x07),	/* 0x33: MBOX_SET_TAG_AGE_LIMIT */
4837	ISPOPMAP(0x03, 0x03),	/* 0x34: MBOX_SET_CLOCK_RATE */
4838	ISPOPMAP(0x07, 0x07),	/* 0x35: MBOX_SET_ACT_NEG_STATE */
4839	ISPOPMAP(0x07, 0x07),	/* 0x36: MBOX_SET_ASYNC_DATA_SETUP_TIME */
4840	ISPOPMAP(0x07, 0x07),	/* 0x37: MBOX_SET_PCI_CONTROL_PARAMS */
4841	ISPOPMAP(0x4f, 0x4f),	/* 0x38: MBOX_SET_TARGET_PARAMS */
4842	ISPOPMAP(0x0f, 0x0f),	/* 0x39: MBOX_SET_DEV_QUEUE_PARAMS */
4843	ISPOPMAP(0x07, 0x07),	/* 0x3a: MBOX_SET_RESET_DELAY_PARAMS */
4844	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
4845	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
4846	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
4847	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
4848	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
4849	ISPOPMAP(0x01, 0x03),	/* 0x40: MBOX_RETURN_BIOS_BLOCK_ADDR */
4850	ISPOPMAP(0x3f, 0x01),	/* 0x41: MBOX_WRITE_FOUR_RAM_WORDS */
4851	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_EXEC_BIOS_IOCB */
4852	ISPOPMAP(0x00, 0x00),	/* 0x43: */
4853	ISPOPMAP(0x00, 0x00),	/* 0x44: */
4854	ISPOPMAP(0x03, 0x03),	/* 0x45: SET SYSTEM PARAMETER */
4855	ISPOPMAP(0x01, 0x03),	/* 0x46: GET SYSTEM PARAMETER */
4856	ISPOPMAP(0x00, 0x00),	/* 0x47: */
4857	ISPOPMAP(0x01, 0xcf),	/* 0x48: GET SCAM CONFIGURATION */
4858	ISPOPMAP(0xcf, 0xcf),	/* 0x49: SET SCAM CONFIGURATION */
4859	ISPOPMAP(0x03, 0x03),	/* 0x4a: MBOX_SET_FIRMWARE_FEATURES */
4860	ISPOPMAP(0x01, 0x03),	/* 0x4b: MBOX_GET_FIRMWARE_FEATURES */
4861	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
4862	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
4863	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
4864	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
4865	ISPOPMAP(0xdf, 0xdf),	/* 0x50: LOAD RAM A64 */
4866	ISPOPMAP(0xdf, 0xdf),	/* 0x51: DUMP RAM A64 */
4867	ISPOPMAP(0xdf, 0xff),	/* 0x52: INITIALIZE REQUEST QUEUE A64 */
4868	ISPOPMAP(0xef, 0xff),	/* 0x53: INITIALIZE RESPONSE QUEUE A64 */
4869	ISPOPMAP(0xcf, 0x01),	/* 0x54: EXECUTE IOCB A64 */
4870	ISPOPMAP(0x07, 0x01),	/* 0x55: ENABLE TARGET MODE */
4871	ISPOPMAP(0x03, 0x0f),	/* 0x56: GET TARGET STATUS */
4872	ISPOPMAP(0x00, 0x00),	/* 0x57: */
4873	ISPOPMAP(0x00, 0x00),	/* 0x58: */
4874	ISPOPMAP(0x00, 0x00),	/* 0x59: */
4875	ISPOPMAP(0x03, 0x03),	/* 0x5a: SET DATA OVERRUN RECOVERY MODE */
4876	ISPOPMAP(0x01, 0x03),	/* 0x5b: GET DATA OVERRUN RECOVERY MODE */
4877	ISPOPMAP(0x0f, 0x0f),	/* 0x5c: SET HOST DATA */
4878	ISPOPMAP(0x01, 0x01)	/* 0x5d: GET NOST DATA */
4879};
4880
4881#ifndef	ISP_STRIPPED
4882static char *scsi_mbcmd_names[] = {
4883	"NO-OP",
4884	"LOAD RAM",
4885	"EXEC FIRMWARE",
4886	"DUMP RAM",
4887	"WRITE RAM WORD",
4888	"READ RAM WORD",
4889	"MAILBOX REG TEST",
4890	"VERIFY CHECKSUM",
4891	"ABOUT FIRMWARE",
4892	NULL,
4893	NULL,
4894	NULL,
4895	NULL,
4896	NULL,
4897	"CHECK FIRMWARE",
4898	NULL,
4899	"INIT REQUEST QUEUE",
4900	"INIT RESULT QUEUE",
4901	"EXECUTE IOCB",
4902	"WAKE UP",
4903	"STOP FIRMWARE",
4904	"ABORT",
4905	"ABORT DEVICE",
4906	"ABORT TARGET",
4907	"BUS RESET",
4908	"STOP QUEUE",
4909	"START QUEUE",
4910	"SINGLE STEP QUEUE",
4911	"ABORT QUEUE",
4912	"GET DEV QUEUE STATUS",
4913	NULL,
4914	"GET FIRMWARE STATUS",
4915	"GET INIT SCSI ID",
4916	"GET SELECT TIMEOUT",
4917	"GET RETRY COUNT",
4918	"GET TAG AGE LIMIT",
4919	"GET CLOCK RATE",
4920	"GET ACT NEG STATE",
4921	"GET ASYNC DATA SETUP TIME",
4922	"GET PCI PARAMS",
4923	"GET TARGET PARAMS",
4924	"GET DEV QUEUE PARAMS",
4925	"GET RESET DELAY PARAMS",
4926	NULL,
4927	NULL,
4928	NULL,
4929	NULL,
4930	NULL,
4931	"SET INIT SCSI ID",
4932	"SET SELECT TIMEOUT",
4933	"SET RETRY COUNT",
4934	"SET TAG AGE LIMIT",
4935	"SET CLOCK RATE",
4936	"SET ACT NEG STATE",
4937	"SET ASYNC DATA SETUP TIME",
4938	"SET PCI CONTROL PARAMS",
4939	"SET TARGET PARAMS",
4940	"SET DEV QUEUE PARAMS",
4941	"SET RESET DELAY PARAMS",
4942	NULL,
4943	NULL,
4944	NULL,
4945	NULL,
4946	NULL,
4947	"RETURN BIOS BLOCK ADDR",
4948	"WRITE FOUR RAM WORDS",
4949	"EXEC BIOS IOCB",
4950	NULL,
4951	NULL,
4952	"SET SYSTEM PARAMETER",
4953	"GET SYSTEM PARAMETER",
4954	NULL,
4955	"GET SCAM CONFIGURATION",
4956	"SET SCAM CONFIGURATION",
4957	"SET FIRMWARE FEATURES",
4958	"GET FIRMWARE FEATURES",
4959	NULL,
4960	NULL,
4961	NULL,
4962	NULL,
4963	"LOAD RAM A64",
4964	"DUMP RAM A64",
4965	"INITIALIZE REQUEST QUEUE A64",
4966	"INITIALIZE RESPONSE QUEUE A64",
4967	"EXECUTE IOCB A64",
4968	"ENABLE TARGET MODE",
4969	"GET TARGET MODE STATE",
4970	NULL,
4971	NULL,
4972	NULL,
4973	"SET DATA OVERRUN RECOVERY MODE",
4974	"GET DATA OVERRUN RECOVERY MODE",
4975	"SET HOST DATA",
4976	"GET NOST DATA",
4977};
4978#endif
4979
4980static const u_int16_t mbpfc[] = {
4981	ISPOPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
4982	ISPOPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
4983	ISPOPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
4984	ISPOPMAP(0xdf, 0x01),	/* 0x03: MBOX_DUMP_RAM */
4985	ISPOPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
4986	ISPOPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
4987	ISPOPMAP(0xff, 0xff),	/* 0x06: MBOX_MAILBOX_REG_TEST */
4988	ISPOPMAP(0x03, 0x05),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
4989	ISPOPMAP(0x01, 0x4f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
4990	ISPOPMAP(0xdf, 0x01),	/* 0x09: LOAD RAM */
4991	ISPOPMAP(0xdf, 0x01),	/* 0x0a: DUMP RAM */
4992	ISPOPMAP(0x00, 0x00),	/* 0x0b: */
4993	ISPOPMAP(0x00, 0x00),	/* 0x0c: */
4994	ISPOPMAP(0x00, 0x00),	/* 0x0d: */
4995	ISPOPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
4996	ISPOPMAP(0x03, 0x07),	/* 0x0f: MBOX_READ_RAM_WORD_EXTENDED(1) */
4997	ISPOPMAP(0x1f, 0x11),	/* 0x10: MBOX_INIT_REQ_QUEUE */
4998	ISPOPMAP(0x2f, 0x21),	/* 0x11: MBOX_INIT_RES_QUEUE */
4999	ISPOPMAP(0x0f, 0x01),	/* 0x12: MBOX_EXECUTE_IOCB */
5000	ISPOPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
5001	ISPOPMAP(0x01, 0xff),	/* 0x14: MBOX_STOP_FIRMWARE */
5002	ISPOPMAP(0x4f, 0x01),	/* 0x15: MBOX_ABORT */
5003	ISPOPMAP(0x07, 0x01),	/* 0x16: MBOX_ABORT_DEVICE */
5004	ISPOPMAP(0x07, 0x01),	/* 0x17: MBOX_ABORT_TARGET */
5005	ISPOPMAP(0x03, 0x03),	/* 0x18: MBOX_BUS_RESET */
5006	ISPOPMAP(0x07, 0x05),	/* 0x19: MBOX_STOP_QUEUE */
5007	ISPOPMAP(0x07, 0x05),	/* 0x1a: MBOX_START_QUEUE */
5008	ISPOPMAP(0x07, 0x05),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
5009	ISPOPMAP(0x07, 0x05),	/* 0x1c: MBOX_ABORT_QUEUE */
5010	ISPOPMAP(0x07, 0x03),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
5011	ISPOPMAP(0x00, 0x00),	/* 0x1e: */
5012	ISPOPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
5013	ISPOPMAP(0x01, 0x4f),	/* 0x20: MBOX_GET_LOOP_ID */
5014	ISPOPMAP(0x00, 0x00),	/* 0x21: */
5015	ISPOPMAP(0x01, 0x07),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
5016	ISPOPMAP(0x00, 0x00),	/* 0x23: */
5017	ISPOPMAP(0x00, 0x00),	/* 0x24: */
5018	ISPOPMAP(0x00, 0x00),	/* 0x25: */
5019	ISPOPMAP(0x00, 0x00),	/* 0x26: */
5020	ISPOPMAP(0x00, 0x00),	/* 0x27: */
5021	ISPOPMAP(0x01, 0x03),	/* 0x28: MBOX_GET_FIRMWARE_OPTIONS */
5022	ISPOPMAP(0x03, 0x07),	/* 0x29: MBOX_GET_PORT_QUEUE_PARAMS */
5023	ISPOPMAP(0x00, 0x00),	/* 0x2a: */
5024	ISPOPMAP(0x00, 0x00),	/* 0x2b: */
5025	ISPOPMAP(0x00, 0x00),	/* 0x2c: */
5026	ISPOPMAP(0x00, 0x00),	/* 0x2d: */
5027	ISPOPMAP(0x00, 0x00),	/* 0x2e: */
5028	ISPOPMAP(0x00, 0x00),	/* 0x2f: */
5029	ISPOPMAP(0x00, 0x00),	/* 0x30: */
5030	ISPOPMAP(0x00, 0x00),	/* 0x31: */
5031	ISPOPMAP(0x07, 0x07),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
5032	ISPOPMAP(0x00, 0x00),	/* 0x33: */
5033	ISPOPMAP(0x00, 0x00),	/* 0x34: */
5034	ISPOPMAP(0x00, 0x00),	/* 0x35: */
5035	ISPOPMAP(0x00, 0x00),	/* 0x36: */
5036	ISPOPMAP(0x00, 0x00),	/* 0x37: */
5037	ISPOPMAP(0x0f, 0x01),	/* 0x38: MBOX_SET_FIRMWARE_OPTIONS */
5038	ISPOPMAP(0x0f, 0x07),	/* 0x39: MBOX_SET_PORT_QUEUE_PARAMS */
5039	ISPOPMAP(0x00, 0x00),	/* 0x3a: */
5040	ISPOPMAP(0x00, 0x00),	/* 0x3b: */
5041	ISPOPMAP(0x00, 0x00),	/* 0x3c: */
5042	ISPOPMAP(0x00, 0x00),	/* 0x3d: */
5043	ISPOPMAP(0x00, 0x00),	/* 0x3e: */
5044	ISPOPMAP(0x00, 0x00),	/* 0x3f: */
5045	ISPOPMAP(0x03, 0x01),	/* 0x40: MBOX_LOOP_PORT_BYPASS */
5046	ISPOPMAP(0x03, 0x01),	/* 0x41: MBOX_LOOP_PORT_ENABLE */
5047	ISPOPMAP(0x03, 0x07),	/* 0x42: MBOX_GET_RESOURCE_COUNTS */
5048	ISPOPMAP(0x01, 0x01),	/* 0x43: MBOX_REQUEST_NON_PARTICIPATING_MODE */
5049	ISPOPMAP(0x00, 0x00),	/* 0x44: */
5050	ISPOPMAP(0x00, 0x00),	/* 0x45: */
5051	ISPOPMAP(0x00, 0x00),	/* 0x46: */
5052	ISPOPMAP(0xcf, 0x03),	/* 0x47: GET PORT_DATABASE ENHANCED */
5053	ISPOPMAP(0x00, 0x00),	/* 0x48: */
5054	ISPOPMAP(0x00, 0x00),	/* 0x49: */
5055	ISPOPMAP(0x00, 0x00),	/* 0x4a: */
5056	ISPOPMAP(0x00, 0x00),	/* 0x4b: */
5057	ISPOPMAP(0x00, 0x00),	/* 0x4c: */
5058	ISPOPMAP(0x00, 0x00),	/* 0x4d: */
5059	ISPOPMAP(0x00, 0x00),	/* 0x4e: */
5060	ISPOPMAP(0x00, 0x00),	/* 0x4f: */
5061	ISPOPMAP(0x00, 0x00),	/* 0x50: */
5062	ISPOPMAP(0x00, 0x00),	/* 0x51: */
5063	ISPOPMAP(0x00, 0x00),	/* 0x52: */
5064	ISPOPMAP(0x00, 0x00),	/* 0x53: */
5065	ISPOPMAP(0xcf, 0x01),	/* 0x54: EXECUTE IOCB A64 */
5066	ISPOPMAP(0x00, 0x00),	/* 0x55: */
5067	ISPOPMAP(0x00, 0x00),	/* 0x56: */
5068	ISPOPMAP(0x00, 0x00),	/* 0x57: */
5069	ISPOPMAP(0x00, 0x00),	/* 0x58: */
5070	ISPOPMAP(0x00, 0x00),	/* 0x59: */
5071	ISPOPMAP(0x00, 0x00),	/* 0x5a: */
5072	ISPOPMAP(0x03, 0x01),	/* 0x5b: MBOX_DRIVER_HEARTBEAT */
5073	ISPOPMAP(0xcf, 0x01),	/* 0x5c: MBOX_FW_HEARTBEAT */
5074	ISPOPMAP(0x07, 0x03),	/* 0x5d: MBOX_GET_SET_DATA_RATE */
5075	ISPOPMAP(0x00, 0x00),	/* 0x5e: */
5076	ISPOPMAP(0x00, 0x00),	/* 0x5f: */
5077	ISPOPMAP(0xfd, 0x31),	/* 0x60: MBOX_INIT_FIRMWARE */
5078	ISPOPMAP(0x00, 0x00),	/* 0x61: */
5079	ISPOPMAP(0x01, 0x01),	/* 0x62: MBOX_INIT_LIP */
5080	ISPOPMAP(0xcd, 0x03),	/* 0x63: MBOX_GET_FC_AL_POSITION_MAP */
5081	ISPOPMAP(0xcf, 0x01),	/* 0x64: MBOX_GET_PORT_DB */
5082	ISPOPMAP(0x07, 0x01),	/* 0x65: MBOX_CLEAR_ACA */
5083	ISPOPMAP(0x07, 0x01),	/* 0x66: MBOX_TARGET_RESET */
5084	ISPOPMAP(0x07, 0x01),	/* 0x67: MBOX_CLEAR_TASK_SET */
5085	ISPOPMAP(0x07, 0x01),	/* 0x68: MBOX_ABORT_TASK_SET */
5086	ISPOPMAP(0x01, 0x07),	/* 0x69: MBOX_GET_FW_STATE */
5087	ISPOPMAP(0x03, 0xcf),	/* 0x6a: MBOX_GET_PORT_NAME */
5088	ISPOPMAP(0xcf, 0x01),	/* 0x6b: MBOX_GET_LINK_STATUS */
5089	ISPOPMAP(0x0f, 0x01),	/* 0x6c: MBOX_INIT_LIP_RESET */
5090	ISPOPMAP(0x00, 0x00),	/* 0x6d: */
5091	ISPOPMAP(0xcf, 0x03),	/* 0x6e: MBOX_SEND_SNS */
5092	ISPOPMAP(0x0f, 0x07),	/* 0x6f: MBOX_FABRIC_LOGIN */
5093	ISPOPMAP(0x03, 0x01),	/* 0x70: MBOX_SEND_CHANGE_REQUEST */
5094	ISPOPMAP(0x03, 0x03),	/* 0x71: MBOX_FABRIC_LOGOUT */
5095	ISPOPMAP(0x0f, 0x0f),	/* 0x72: MBOX_INIT_LIP_LOGIN */
5096	ISPOPMAP(0x00, 0x00),	/* 0x73: */
5097	ISPOPMAP(0x07, 0x01),	/* 0x74: LOGIN LOOP PORT */
5098	ISPOPMAP(0xcf, 0x03),	/* 0x75: GET PORT/NODE NAME LIST */
5099	ISPOPMAP(0x4f, 0x01),	/* 0x76: SET VENDOR ID */
5100	ISPOPMAP(0xcd, 0x01),	/* 0x77: INITIALIZE IP MAILBOX */
5101	ISPOPMAP(0x00, 0x00),	/* 0x78: */
5102	ISPOPMAP(0x00, 0x00),	/* 0x79: */
5103	ISPOPMAP(0x00, 0x00),	/* 0x7a: */
5104	ISPOPMAP(0x00, 0x00),	/* 0x7b: */
5105	ISPOPMAP(0x4f, 0x03),	/* 0x7c: Get ID List */
5106	ISPOPMAP(0xcf, 0x01),	/* 0x7d: SEND LFA */
5107	ISPOPMAP(0x07, 0x01)	/* 0x7e: Lun RESET */
5108};
5109/*
5110 * Footnotes
5111 *
5112 * (1): this sets bits 21..16 in mailbox register #8, which we nominally
5113 *	do not access at this time in the core driver. The caller is
5114 *	responsible for setting this register first (Gross!).
5115 */
5116
5117#ifndef	ISP_STRIPPED
5118static char *fc_mbcmd_names[] = {
5119	"NO-OP",
5120	"LOAD RAM",
5121	"EXEC FIRMWARE",
5122	"DUMP RAM",
5123	"WRITE RAM WORD",
5124	"READ RAM WORD",
5125	"MAILBOX REG TEST",
5126	"VERIFY CHECKSUM",
5127	"ABOUT FIRMWARE",
5128	"LOAD RAM",
5129	"DUMP RAM",
5130	NULL,
5131	NULL,
5132	"READ RAM WORD EXTENDED",
5133	"CHECK FIRMWARE",
5134	NULL,
5135	"INIT REQUEST QUEUE",
5136	"INIT RESULT QUEUE",
5137	"EXECUTE IOCB",
5138	"WAKE UP",
5139	"STOP FIRMWARE",
5140	"ABORT",
5141	"ABORT DEVICE",
5142	"ABORT TARGET",
5143	"BUS RESET",
5144	"STOP QUEUE",
5145	"START QUEUE",
5146	"SINGLE STEP QUEUE",
5147	"ABORT QUEUE",
5148	"GET DEV QUEUE STATUS",
5149	NULL,
5150	"GET FIRMWARE STATUS",
5151	"GET LOOP ID",
5152	NULL,
5153	"GET RETRY COUNT",
5154	NULL,
5155	NULL,
5156	NULL,
5157	NULL,
5158	NULL,
5159	"GET FIRMWARE OPTIONS",
5160	"GET PORT QUEUE PARAMS",
5161	NULL,
5162	NULL,
5163	NULL,
5164	NULL,
5165	NULL,
5166	NULL,
5167	NULL,
5168	NULL,
5169	"SET RETRY COUNT",
5170	NULL,
5171	NULL,
5172	NULL,
5173	NULL,
5174	NULL,
5175	"SET FIRMWARE OPTIONS",
5176	"SET PORT QUEUE PARAMS",
5177	NULL,
5178	NULL,
5179	NULL,
5180	NULL,
5181	NULL,
5182	NULL,
5183	"LOOP PORT BYPASS",
5184	"LOOP PORT ENABLE",
5185	"GET RESOURCE COUNTS",
5186	"REQUEST NON PARTICIPATING MODE",
5187	NULL,
5188	NULL,
5189	NULL,
5190	"GET PORT DATABASE,, ENHANCED",
5191	NULL,
5192	NULL,
5193	NULL,
5194	NULL,
5195	NULL,
5196	NULL,
5197	NULL,
5198	NULL,
5199	NULL,
5200	NULL,
5201	NULL,
5202	NULL,
5203	"EXECUTE IOCB A64",
5204	NULL,
5205	NULL,
5206	NULL,
5207	NULL,
5208	NULL,
5209	NULL,
5210	"DRIVER HEARTBEAT",
5211	NULL,
5212	"GET/SET DATA RATE",
5213	NULL,
5214	NULL,
5215	"INIT FIRMWARE",
5216	NULL,
5217	"INIT LIP",
5218	"GET FC-AL POSITION MAP",
5219	"GET PORT DATABASE",
5220	"CLEAR ACA",
5221	"TARGET RESET",
5222	"CLEAR TASK SET",
5223	"ABORT TASK SET",
5224	"GET FW STATE",
5225	"GET PORT NAME",
5226	"GET LINK STATUS",
5227	"INIT LIP RESET",
5228	NULL,
5229	"SEND SNS",
5230	"FABRIC LOGIN",
5231	"SEND CHANGE REQUEST",
5232	"FABRIC LOGOUT",
5233	"INIT LIP LOGIN",
5234	NULL,
5235	"LOGIN LOOP PORT",
5236	"GET PORT/NODE NAME LIST",
5237	"SET VENDOR ID",
5238	"INITIALIZE IP MAILBOX",
5239	NULL,
5240	NULL,
5241	NULL,
5242	NULL,
5243	"Get ID List",
5244	"SEND LFA",
5245	"Lun RESET"
5246};
5247#endif
5248
5249static void
5250isp_mboxcmd_qnw(struct ispsoftc *isp, mbreg_t *mbp, int nodelay)
5251{
5252	unsigned int ibits, obits, box, opcode;
5253	const u_int16_t *mcp;
5254
5255	if (IS_FC(isp)) {
5256		mcp = mbpfc;
5257	} else {
5258		mcp = mbpscsi;
5259	}
5260	opcode = mbp->param[0];
5261	ibits = HIBYT(mcp[opcode]) & NMBOX_BMASK(isp);
5262	obits = LOBYT(mcp[opcode]) & NMBOX_BMASK(isp);
5263	for (box = 0; box < MAX_MAILBOX; box++) {
5264		if (ibits & (1 << box)) {
5265			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
5266		}
5267		if (nodelay == 0) {
5268			isp->isp_mboxtmp[box] = mbp->param[box] = 0;
5269		}
5270	}
5271	if (nodelay == 0) {
5272		isp->isp_lastmbxcmd = opcode;
5273		isp->isp_obits = obits;
5274		isp->isp_mboxbsy = 1;
5275	}
5276	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
5277	/*
5278	 * Oddly enough, if we're not delaying for an answer,
5279	 * delay a bit to give the f/w a chance to pick up the
5280	 * command.
5281	 */
5282	if (nodelay) {
5283		USEC_DELAY(1000);
5284	}
5285}
5286
5287static void
5288isp_mboxcmd(struct ispsoftc *isp, mbreg_t *mbp, int logmask)
5289{
5290	char *cname, *xname, tname[16], mname[16];
5291	unsigned int lim, ibits, obits, box, opcode;
5292	const u_int16_t *mcp;
5293
5294	if (IS_FC(isp)) {
5295		mcp = mbpfc;
5296		lim = (sizeof (mbpfc) / sizeof (mbpfc[0]));
5297	} else {
5298		mcp = mbpscsi;
5299		lim = (sizeof (mbpscsi) / sizeof (mbpscsi[0]));
5300	}
5301
5302	if ((opcode = mbp->param[0]) >= lim) {
5303		mbp->param[0] = MBOX_INVALID_COMMAND;
5304		isp_prt(isp, ISP_LOGERR, "Unknown Command 0x%x", opcode);
5305		return;
5306	}
5307
5308	ibits = HIBYT(mcp[opcode]) & NMBOX_BMASK(isp);
5309	obits = LOBYT(mcp[opcode]) & NMBOX_BMASK(isp);
5310
5311	if (ibits == 0 && obits == 0) {
5312		mbp->param[0] = MBOX_COMMAND_PARAM_ERROR;
5313		isp_prt(isp, ISP_LOGERR, "no parameters for 0x%x", opcode);
5314		return;
5315	}
5316
5317	/*
5318	 * Get exclusive usage of mailbox registers.
5319	 */
5320	MBOX_ACQUIRE(isp);
5321
5322	for (box = 0; box < MAX_MAILBOX; box++) {
5323		if (ibits & (1 << box)) {
5324			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
5325		}
5326		isp->isp_mboxtmp[box] = mbp->param[box] = 0;
5327	}
5328
5329	isp->isp_lastmbxcmd = opcode;
5330
5331	/*
5332	 * We assume that we can't overwrite a previous command.
5333	 */
5334	isp->isp_obits = obits;
5335	isp->isp_mboxbsy = 1;
5336
5337	/*
5338	 * Set Host Interrupt condition so that RISC will pick up mailbox regs.
5339	 */
5340	ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
5341
5342	/*
5343	 * While we haven't finished the command, spin our wheels here.
5344	 */
5345	MBOX_WAIT_COMPLETE(isp);
5346
5347	if (isp->isp_mboxbsy) {
5348		/*
5349		 * Command timed out.
5350		 */
5351		isp->isp_mboxbsy = 0;
5352		MBOX_RELEASE(isp);
5353		return;
5354	}
5355
5356	/*
5357	 * Copy back output registers.
5358	 */
5359	for (box = 0; box < MAX_MAILBOX; box++) {
5360		if (obits & (1 << box)) {
5361			mbp->param[box] = isp->isp_mboxtmp[box];
5362		}
5363	}
5364
5365	MBOX_RELEASE(isp);
5366
5367	if (logmask == 0 || opcode == MBOX_EXEC_FIRMWARE) {
5368		return;
5369	}
5370#ifdef	ISP_STRIPPED
5371	cname = NULL;
5372#else
5373	cname = (IS_FC(isp))? fc_mbcmd_names[opcode] : scsi_mbcmd_names[opcode];
5374#endif
5375	if (cname == NULL) {
5376		cname = tname;
5377		SNPRINTF(tname, sizeof tname, "opcode %x", opcode);
5378	}
5379
5380	/*
5381	 * Just to be chatty here...
5382	 */
5383	xname = NULL;
5384	switch (mbp->param[0]) {
5385	case MBOX_COMMAND_COMPLETE:
5386		break;
5387	case MBOX_INVALID_COMMAND:
5388		if (logmask & MBLOGMASK(MBOX_COMMAND_COMPLETE))
5389			xname = "INVALID COMMAND";
5390		break;
5391	case MBOX_HOST_INTERFACE_ERROR:
5392		if (logmask & MBLOGMASK(MBOX_HOST_INTERFACE_ERROR))
5393			xname = "HOST INTERFACE ERROR";
5394		break;
5395	case MBOX_TEST_FAILED:
5396		if (logmask & MBLOGMASK(MBOX_TEST_FAILED))
5397			xname = "TEST FAILED";
5398		break;
5399	case MBOX_COMMAND_ERROR:
5400		if (logmask & MBLOGMASK(MBOX_COMMAND_ERROR))
5401			xname = "COMMAND ERROR";
5402		break;
5403	case MBOX_COMMAND_PARAM_ERROR:
5404		if (logmask & MBLOGMASK(MBOX_COMMAND_PARAM_ERROR))
5405			xname = "COMMAND PARAMETER ERROR";
5406		break;
5407	case MBOX_LOOP_ID_USED:
5408		if (logmask & MBLOGMASK(MBOX_LOOP_ID_USED))
5409			xname = "LOOP ID ALREADY IN USE";
5410		break;
5411	case MBOX_PORT_ID_USED:
5412		if (logmask & MBLOGMASK(MBOX_PORT_ID_USED))
5413			xname = "PORT ID ALREADY IN USE";
5414		break;
5415	case MBOX_ALL_IDS_USED:
5416		if (logmask & MBLOGMASK(MBOX_ALL_IDS_USED))
5417			xname = "ALL LOOP IDS IN USE";
5418		break;
5419	case 0:		/* special case */
5420		xname = "TIMEOUT";
5421		break;
5422	default:
5423		SNPRINTF(mname, sizeof mname, "error 0x%x", mbp->param[0]);
5424		xname = mname;
5425		break;
5426	}
5427	if (xname)
5428		isp_prt(isp, ISP_LOGALL, "Mailbox Command '%s' failed (%s)",
5429		    cname, xname);
5430}
5431
5432static void
5433isp_fw_state(struct ispsoftc *isp)
5434{
5435	if (IS_FC(isp)) {
5436		mbreg_t mbs;
5437		fcparam *fcp = isp->isp_param;
5438
5439		mbs.param[0] = MBOX_GET_FW_STATE;
5440		isp_mboxcmd(isp, &mbs, MBLOGALL);
5441		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
5442			fcp->isp_fwstate = mbs.param[1];
5443		}
5444	}
5445}
5446
5447static void
5448isp_update(struct ispsoftc *isp)
5449{
5450	int bus, upmask;
5451
5452	for (bus = 0, upmask = isp->isp_update; upmask != 0; bus++) {
5453		if (upmask & (1 << bus)) {
5454			isp_update_bus(isp, bus);
5455		}
5456		upmask &= ~(1 << bus);
5457	}
5458}
5459
5460static void
5461isp_update_bus(struct ispsoftc *isp, int bus)
5462{
5463	int tgt;
5464	mbreg_t mbs;
5465	sdparam *sdp;
5466
5467	isp->isp_update &= ~(1 << bus);
5468	if (IS_FC(isp)) {
5469		/*
5470		 * There are no 'per-bus' settings for Fibre Channel.
5471		 */
5472		return;
5473	}
5474	sdp = isp->isp_param;
5475	sdp += bus;
5476
5477	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
5478		u_int16_t flags, period, offset;
5479		int get;
5480
5481		if (sdp->isp_devparam[tgt].dev_enable == 0) {
5482			sdp->isp_devparam[tgt].dev_update = 0;
5483			sdp->isp_devparam[tgt].dev_refresh = 0;
5484			isp_prt(isp, ISP_LOGDEBUG0,
5485	 		    "skipping target %d bus %d update", tgt, bus);
5486			continue;
5487		}
5488		/*
5489		 * If the goal is to update the status of the device,
5490		 * take what's in goal_flags and try and set the device
5491		 * toward that. Otherwise, if we're just refreshing the
5492		 * current device state, get the current parameters.
5493		 */
5494
5495		/*
5496		 * Refresh overrides set
5497		 */
5498		if (sdp->isp_devparam[tgt].dev_refresh) {
5499			mbs.param[0] = MBOX_GET_TARGET_PARAMS;
5500			sdp->isp_devparam[tgt].dev_refresh = 0;
5501			get = 1;
5502		} else if (sdp->isp_devparam[tgt].dev_update) {
5503			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
5504			/*
5505			 * Make sure goal_flags has "Renegotiate on Error"
5506			 * on and "Freeze Queue on Error" off.
5507			 */
5508			sdp->isp_devparam[tgt].goal_flags |= DPARM_RENEG;
5509			sdp->isp_devparam[tgt].goal_flags &= ~DPARM_QFRZ;
5510
5511			mbs.param[2] = sdp->isp_devparam[tgt].goal_flags;
5512
5513			/*
5514			 * Insist that PARITY must be enabled
5515			 * if SYNC or WIDE is enabled.
5516			 */
5517			if ((mbs.param[2] & (DPARM_SYNC|DPARM_WIDE)) != 0) {
5518				mbs.param[2] |= DPARM_PARITY;
5519			}
5520
5521			if ((mbs.param[2] & DPARM_SYNC) == 0) {
5522				mbs.param[3] = 0;
5523			} else {
5524				mbs.param[3] =
5525				    (sdp->isp_devparam[tgt].goal_offset << 8) |
5526				    (sdp->isp_devparam[tgt].goal_period);
5527			}
5528			/*
5529			 * A command completion later that has
5530			 * RQSTF_NEGOTIATION set can cause
5531			 * the dev_refresh/announce cycle also.
5532			 *
5533			 * Note: It is really important to update our current
5534			 * flags with at least the state of TAG capabilities-
5535			 * otherwise we might try and send a tagged command
5536			 * when we have it all turned off. So change it here
5537			 * to say that current already matches goal.
5538			 */
5539			sdp->isp_devparam[tgt].actv_flags &= ~DPARM_TQING;
5540			sdp->isp_devparam[tgt].actv_flags |=
5541			    (sdp->isp_devparam[tgt].goal_flags & DPARM_TQING);
5542			isp_prt(isp, ISP_LOGDEBUG0,
5543			    "bus %d set tgt %d flags 0x%x off 0x%x period 0x%x",
5544			    bus, tgt, mbs.param[2], mbs.param[3] >> 8,
5545			    mbs.param[3] & 0xff);
5546			sdp->isp_devparam[tgt].dev_update = 0;
5547			sdp->isp_devparam[tgt].dev_refresh = 1;
5548			get = 0;
5549		} else {
5550			continue;
5551		}
5552		mbs.param[1] = (bus << 15) | (tgt << 8);
5553		isp_mboxcmd(isp, &mbs, MBLOGALL);
5554		if (get == 0) {
5555			isp->isp_sendmarker |= (1 << bus);
5556			continue;
5557		}
5558		flags = mbs.param[2];
5559		period = mbs.param[3] & 0xff;
5560		offset = mbs.param[3] >> 8;
5561		sdp->isp_devparam[tgt].actv_flags = flags;
5562		sdp->isp_devparam[tgt].actv_period = period;
5563		sdp->isp_devparam[tgt].actv_offset = offset;
5564		get = (bus << 16) | tgt;
5565		(void) isp_async(isp, ISPASYNC_NEW_TGT_PARAMS, &get);
5566	}
5567
5568	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
5569		if (sdp->isp_devparam[tgt].dev_update ||
5570		    sdp->isp_devparam[tgt].dev_refresh) {
5571			isp->isp_update |= (1 << bus);
5572			break;
5573		}
5574	}
5575}
5576
5577#ifndef	DEFAULT_FRAMESIZE
5578#define	DEFAULT_FRAMESIZE(isp)		ICB_DFLT_FRMLEN
5579#endif
5580#ifndef	DEFAULT_EXEC_THROTTLE
5581#define	DEFAULT_EXEC_THROTTLE(isp)	ISP_EXEC_THROTTLE
5582#endif
5583
5584static void
5585isp_setdfltparm(struct ispsoftc *isp, int channel)
5586{
5587	int tgt;
5588	mbreg_t mbs;
5589	sdparam *sdp;
5590
5591	if (IS_FC(isp)) {
5592		fcparam *fcp = (fcparam *) isp->isp_param;
5593		int nvfail;
5594
5595		fcp += channel;
5596		if (fcp->isp_gotdparms) {
5597			return;
5598		}
5599		fcp->isp_gotdparms = 1;
5600		fcp->isp_maxfrmlen = DEFAULT_FRAMESIZE(isp);
5601		fcp->isp_maxalloc = ICB_DFLT_ALLOC;
5602		fcp->isp_execthrottle = DEFAULT_EXEC_THROTTLE(isp);
5603		fcp->isp_retry_delay = ICB_DFLT_RDELAY;
5604		fcp->isp_retry_count = ICB_DFLT_RCOUNT;
5605		/* Platform specific.... */
5606		fcp->isp_loopid = DEFAULT_LOOPID(isp);
5607		fcp->isp_nodewwn = DEFAULT_NODEWWN(isp);
5608		fcp->isp_portwwn = DEFAULT_PORTWWN(isp);
5609		fcp->isp_fwoptions = 0;
5610		fcp->isp_fwoptions |= ICBOPT_FAIRNESS;
5611		fcp->isp_fwoptions |= ICBOPT_PDBCHANGE_AE;
5612		fcp->isp_fwoptions |= ICBOPT_HARD_ADDRESS;
5613#ifndef	ISP_NO_FASTPOST_FC
5614		fcp->isp_fwoptions |= ICBOPT_FAST_POST;
5615#endif
5616		if (isp->isp_confopts & ISP_CFG_FULL_DUPLEX)
5617			fcp->isp_fwoptions |= ICBOPT_FULL_DUPLEX;
5618
5619		/*
5620		 * Make sure this is turned off now until we get
5621		 * extended options from NVRAM
5622		 */
5623		fcp->isp_fwoptions &= ~ICBOPT_EXTENDED;
5624
5625		/*
5626		 * Now try and read NVRAM unless told to not do so.
5627		 * This will set fcparam's isp_nodewwn && isp_portwwn.
5628		 */
5629		if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
5630		    	nvfail = isp_read_nvram(isp);
5631			if (nvfail)
5632				isp->isp_confopts |= ISP_CFG_NONVRAM;
5633		} else {
5634			nvfail = 1;
5635		}
5636		/*
5637		 * Set node && port to override platform set defaults
5638		 * unless the nvram read failed (or none was done),
5639		 * or the platform code wants to use what had been
5640		 * set in the defaults.
5641		 */
5642		if (nvfail) {
5643			isp->isp_confopts |= ISP_CFG_OWNWWPN|ISP_CFG_OWNWWNN;
5644		}
5645		if (isp->isp_confopts & ISP_CFG_OWNWWNN) {
5646			isp_prt(isp, ISP_LOGCONFIG, "Using Node WWN 0x%08x%08x",
5647			    (u_int32_t) (DEFAULT_NODEWWN(isp) >> 32),
5648			    (u_int32_t) (DEFAULT_NODEWWN(isp) & 0xffffffff));
5649			ISP_NODEWWN(isp) = DEFAULT_NODEWWN(isp);
5650		} else {
5651			/*
5652			 * We always start out with values derived
5653			 * from NVRAM or our platform default.
5654			 */
5655			ISP_NODEWWN(isp) = fcp->isp_nodewwn;
5656		}
5657		if (isp->isp_confopts & ISP_CFG_OWNWWPN) {
5658			isp_prt(isp, ISP_LOGCONFIG, "Using Port WWN 0x%08x%08x",
5659			    (u_int32_t) (DEFAULT_PORTWWN(isp) >> 32),
5660			    (u_int32_t) (DEFAULT_PORTWWN(isp) & 0xffffffff));
5661			ISP_PORTWWN(isp) = DEFAULT_PORTWWN(isp);
5662		} else {
5663			/*
5664			 * We always start out with values derived
5665			 * from NVRAM or our platform default.
5666			 */
5667			ISP_PORTWWN(isp) = fcp->isp_portwwn;
5668		}
5669		return;
5670	}
5671
5672	sdp = (sdparam *) isp->isp_param;
5673	sdp += channel;
5674
5675	/*
5676	 * Been there, done that, got the T-shirt...
5677	 */
5678	if (sdp->isp_gotdparms) {
5679		return;
5680	}
5681	sdp->isp_gotdparms = 1;
5682
5683	/*
5684	 * Establish some default parameters.
5685	 */
5686	sdp->isp_cmd_dma_burst_enable = 0;
5687	sdp->isp_data_dma_burst_enabl = 1;
5688	sdp->isp_fifo_threshold = 0;
5689	sdp->isp_initiator_id = DEFAULT_IID(isp);
5690	if (isp->isp_type >= ISP_HA_SCSI_1040) {
5691		sdp->isp_async_data_setup = 9;
5692	} else {
5693		sdp->isp_async_data_setup = 6;
5694	}
5695	sdp->isp_selection_timeout = 250;
5696	sdp->isp_max_queue_depth = MAXISPREQUEST(isp);
5697	sdp->isp_tag_aging = 8;
5698	sdp->isp_bus_reset_delay = 5;
5699	/*
5700	 * Don't retry selection, busy or queue full automatically- reflect
5701	 * these back to us.
5702	 */
5703	sdp->isp_retry_count = 0;
5704	sdp->isp_retry_delay = 0;
5705
5706	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
5707		sdp->isp_devparam[tgt].exc_throttle = ISP_EXEC_THROTTLE;
5708		sdp->isp_devparam[tgt].dev_enable = 1;
5709	}
5710
5711	/*
5712	 * If we've not been told to avoid reading NVRAM, try and read it.
5713	 * If we're successful reading it, we can then return because NVRAM
5714	 * will tell us what the desired settings are. Otherwise, we establish
5715	 * some reasonable 'fake' nvram and goal defaults.
5716	 */
5717
5718	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
5719		if (isp_read_nvram(isp) == 0) {
5720			return;
5721		}
5722	}
5723
5724	/*
5725	 * Now try and see whether we have specific values for them.
5726	 */
5727	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
5728		mbs.param[0] = MBOX_GET_ACT_NEG_STATE;
5729		isp_mboxcmd(isp, &mbs, MBLOGNONE);
5730		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
5731			sdp->isp_req_ack_active_neg = 1;
5732			sdp->isp_data_line_active_neg = 1;
5733		} else {
5734			sdp->isp_req_ack_active_neg =
5735			    (mbs.param[1+channel] >> 4) & 0x1;
5736			sdp->isp_data_line_active_neg =
5737			    (mbs.param[1+channel] >> 5) & 0x1;
5738		}
5739	}
5740
5741	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc3,
5742	    0, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
5743	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
5744	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
5745	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc3,
5746	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
5747	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
5748	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
5749
5750	/*
5751	 * The trick here is to establish a default for the default (honk!)
5752	 * state (goal_flags). Then try and get the current status from
5753	 * the card to fill in the current state. We don't, in fact, set
5754	 * the default to the SAFE default state- that's not the goal state.
5755	 */
5756	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
5757		u_int8_t off, per;
5758		sdp->isp_devparam[tgt].actv_offset = 0;
5759		sdp->isp_devparam[tgt].actv_period = 0;
5760		sdp->isp_devparam[tgt].actv_flags = 0;
5761
5762		sdp->isp_devparam[tgt].goal_flags =
5763		    sdp->isp_devparam[tgt].nvrm_flags = DPARM_DEFAULT;
5764
5765		/*
5766		 * We default to Wide/Fast for versions less than a 1040
5767		 * (unless it's SBus).
5768		 */
5769		if (IS_ULTRA3(isp)) {
5770			off = ISP_80M_SYNCPARMS >> 8;
5771			per = ISP_80M_SYNCPARMS & 0xff;
5772		} else if (IS_ULTRA2(isp)) {
5773			off = ISP_40M_SYNCPARMS >> 8;
5774			per = ISP_40M_SYNCPARMS & 0xff;
5775		} else if (IS_1240(isp)) {
5776			off = ISP_20M_SYNCPARMS >> 8;
5777			per = ISP_20M_SYNCPARMS & 0xff;
5778		} else if ((isp->isp_bustype == ISP_BT_SBUS &&
5779		    isp->isp_type < ISP_HA_SCSI_1020A) ||
5780		    (isp->isp_bustype == ISP_BT_PCI &&
5781		    isp->isp_type < ISP_HA_SCSI_1040) ||
5782		    (isp->isp_clock && isp->isp_clock < 60) ||
5783		    (sdp->isp_ultramode == 0)) {
5784			off = ISP_10M_SYNCPARMS >> 8;
5785			per = ISP_10M_SYNCPARMS & 0xff;
5786		} else {
5787			off = ISP_20M_SYNCPARMS_1040 >> 8;
5788			per = ISP_20M_SYNCPARMS_1040 & 0xff;
5789		}
5790		sdp->isp_devparam[tgt].goal_offset =
5791		    sdp->isp_devparam[tgt].nvrm_offset = off;
5792		sdp->isp_devparam[tgt].goal_period =
5793		    sdp->isp_devparam[tgt].nvrm_period = per;
5794
5795		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc3,
5796		    channel, tgt, sdp->isp_devparam[tgt].nvrm_flags,
5797		    sdp->isp_devparam[tgt].nvrm_offset,
5798		    sdp->isp_devparam[tgt].nvrm_period);
5799	}
5800}
5801
5802/*
5803 * Re-initialize the ISP and complete all orphaned commands
5804 * with a 'botched' notice. The reset/init routines should
5805 * not disturb an already active list of commands.
5806 *
5807 * Locks held prior to coming here.
5808 */
5809
5810void
5811isp_reinit(struct ispsoftc *isp)
5812{
5813	XS_T *xs;
5814	u_int16_t handle;
5815
5816	if (IS_FC(isp)) {
5817		isp_mark_getpdb_all(isp);
5818	}
5819	isp_reset(isp);
5820	if (isp->isp_state != ISP_RESETSTATE) {
5821		isp_prt(isp, ISP_LOGERR, "isp_reinit cannot reset card");
5822	} else if (isp->isp_role != ISP_ROLE_NONE) {
5823		isp_init(isp);
5824		if (isp->isp_state == ISP_INITSTATE) {
5825			isp->isp_state = ISP_RUNSTATE;
5826		}
5827		if (isp->isp_state != ISP_RUNSTATE) {
5828			isp_prt(isp, ISP_LOGERR,
5829			    "isp_reinit cannot restart card");
5830		}
5831	}
5832	isp->isp_nactive = 0;
5833
5834	for (handle = 1; (int) handle <= isp->isp_maxcmds; handle++) {
5835		xs = isp_find_xs(isp, handle);
5836		if (xs == NULL) {
5837			continue;
5838		}
5839		isp_destroy_handle(isp, handle);
5840		if (XS_XFRLEN(xs)) {
5841			ISP_DMAFREE(isp, xs, handle);
5842			XS_RESID(xs) = XS_XFRLEN(xs);
5843		} else {
5844			XS_RESID(xs) = 0;
5845		}
5846		XS_SETERR(xs, HBA_BUSRESET);
5847		isp_done(xs);
5848	}
5849}
5850
5851/*
5852 * NVRAM Routines
5853 */
5854static int
5855isp_read_nvram(struct ispsoftc *isp)
5856{
5857	int i, amt;
5858	u_int8_t csum, minversion;
5859	union {
5860		u_int8_t _x[ISP2100_NVRAM_SIZE];
5861		u_int16_t _s[ISP2100_NVRAM_SIZE>>1];
5862	} _n;
5863#define	nvram_data	_n._x
5864#define	nvram_words	_n._s
5865
5866	if (IS_FC(isp)) {
5867		amt = ISP2100_NVRAM_SIZE;
5868		minversion = 1;
5869	} else if (IS_ULTRA2(isp)) {
5870		amt = ISP1080_NVRAM_SIZE;
5871		minversion = 0;
5872	} else {
5873		amt = ISP_NVRAM_SIZE;
5874		minversion = 2;
5875	}
5876
5877	/*
5878	 * Just read the first two words first to see if we have a valid
5879	 * NVRAM to continue reading the rest with.
5880	 */
5881	for (i = 0; i < 2; i++) {
5882		isp_rdnvram_word(isp, i, &nvram_words[i]);
5883	}
5884	if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
5885	    nvram_data[2] != 'P') {
5886		if (isp->isp_bustype != ISP_BT_SBUS) {
5887			isp_prt(isp, ISP_LOGWARN, "invalid NVRAM header");
5888			isp_prt(isp, ISP_LOGDEBUG0, "%x %x %x",
5889			    nvram_data[0], nvram_data[1], nvram_data[2]);
5890		}
5891		return (-1);
5892	}
5893	for (i = 2; i < amt>>1; i++) {
5894		isp_rdnvram_word(isp, i, &nvram_words[i]);
5895	}
5896	for (csum = 0, i = 0; i < amt; i++) {
5897		csum += nvram_data[i];
5898	}
5899	if (csum != 0) {
5900		isp_prt(isp, ISP_LOGWARN, "invalid NVRAM checksum");
5901		return (-1);
5902	}
5903	if (ISP_NVRAM_VERSION(nvram_data) < minversion) {
5904		isp_prt(isp, ISP_LOGWARN, "version %d NVRAM not understood",
5905		    ISP_NVRAM_VERSION(nvram_data));
5906		return (-1);
5907	}
5908
5909	if (IS_ULTRA3(isp)) {
5910		isp_parse_nvram_12160(isp, 0, nvram_data);
5911		if (IS_12160(isp))
5912			isp_parse_nvram_12160(isp, 1, nvram_data);
5913	} else if (IS_1080(isp)) {
5914		isp_parse_nvram_1080(isp, 0, nvram_data);
5915	} else if (IS_1280(isp) || IS_1240(isp)) {
5916		isp_parse_nvram_1080(isp, 0, nvram_data);
5917		isp_parse_nvram_1080(isp, 1, nvram_data);
5918	} else if (IS_SCSI(isp)) {
5919		isp_parse_nvram_1020(isp, nvram_data);
5920	} else {
5921		isp_parse_nvram_2100(isp, nvram_data);
5922	}
5923	return (0);
5924#undef	nvram_data
5925#undef	nvram_words
5926}
5927
5928static void
5929isp_rdnvram_word(struct ispsoftc *isp, int wo, u_int16_t *rp)
5930{
5931	int i, cbits;
5932	u_int16_t bit, rqst;
5933
5934	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
5935	USEC_DELAY(2);
5936	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
5937	USEC_DELAY(2);
5938
5939	if (IS_FC(isp)) {
5940		wo &= ((ISP2100_NVRAM_SIZE >> 1) - 1);
5941		if (IS_2312(isp) && isp->isp_port) {
5942			wo += 128;
5943		}
5944		rqst = (ISP_NVRAM_READ << 8) | wo;
5945		cbits = 10;
5946	} else if (IS_ULTRA2(isp)) {
5947		wo &= ((ISP1080_NVRAM_SIZE >> 1) - 1);
5948		rqst = (ISP_NVRAM_READ << 8) | wo;
5949		cbits = 10;
5950	} else {
5951		wo &= ((ISP_NVRAM_SIZE >> 1) - 1);
5952		rqst = (ISP_NVRAM_READ << 6) | wo;
5953		cbits = 8;
5954	}
5955
5956	/*
5957	 * Clock the word select request out...
5958	 */
5959	for (i = cbits; i >= 0; i--) {
5960		if ((rqst >> i) & 1) {
5961			bit = BIU_NVRAM_SELECT | BIU_NVRAM_DATAOUT;
5962		} else {
5963			bit = BIU_NVRAM_SELECT;
5964		}
5965		ISP_WRITE(isp, BIU_NVRAM, bit);
5966		USEC_DELAY(2);
5967		ISP_WRITE(isp, BIU_NVRAM, bit | BIU_NVRAM_CLOCK);
5968		USEC_DELAY(2);
5969		ISP_WRITE(isp, BIU_NVRAM, bit);
5970		USEC_DELAY(2);
5971	}
5972	/*
5973	 * Now read the result back in (bits come back in MSB format).
5974	 */
5975	*rp = 0;
5976	for (i = 0; i < 16; i++) {
5977		u_int16_t rv;
5978		*rp <<= 1;
5979		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
5980		USEC_DELAY(2);
5981		rv = ISP_READ(isp, BIU_NVRAM);
5982		if (rv & BIU_NVRAM_DATAIN) {
5983			*rp |= 1;
5984		}
5985		USEC_DELAY(2);
5986		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
5987		USEC_DELAY(2);
5988	}
5989	ISP_WRITE(isp, BIU_NVRAM, 0);
5990	USEC_DELAY(2);
5991	ISP_SWIZZLE_NVRAM_WORD(isp, rp);
5992}
5993
5994static void
5995isp_parse_nvram_1020(struct ispsoftc *isp, u_int8_t *nvram_data)
5996{
5997	sdparam *sdp = (sdparam *) isp->isp_param;
5998	int tgt;
5999
6000	sdp->isp_fifo_threshold =
6001		ISP_NVRAM_FIFO_THRESHOLD(nvram_data) |
6002		(ISP_NVRAM_FIFO_THRESHOLD_128(nvram_data) << 2);
6003
6004	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
6005		sdp->isp_initiator_id =
6006			ISP_NVRAM_INITIATOR_ID(nvram_data);
6007
6008	sdp->isp_bus_reset_delay =
6009		ISP_NVRAM_BUS_RESET_DELAY(nvram_data);
6010
6011	sdp->isp_retry_count =
6012		ISP_NVRAM_BUS_RETRY_COUNT(nvram_data);
6013
6014	sdp->isp_retry_delay =
6015		ISP_NVRAM_BUS_RETRY_DELAY(nvram_data);
6016
6017	sdp->isp_async_data_setup =
6018		ISP_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data);
6019
6020	if (isp->isp_type >= ISP_HA_SCSI_1040) {
6021		if (sdp->isp_async_data_setup < 9) {
6022			sdp->isp_async_data_setup = 9;
6023		}
6024	} else {
6025		if (sdp->isp_async_data_setup != 6) {
6026			sdp->isp_async_data_setup = 6;
6027		}
6028	}
6029
6030	sdp->isp_req_ack_active_neg =
6031		ISP_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data);
6032
6033	sdp->isp_data_line_active_neg =
6034		ISP_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data);
6035
6036	sdp->isp_data_dma_burst_enabl =
6037		ISP_NVRAM_DATA_DMA_BURST_ENABLE(nvram_data);
6038
6039	sdp->isp_cmd_dma_burst_enable =
6040		ISP_NVRAM_CMD_DMA_BURST_ENABLE(nvram_data);
6041
6042	sdp->isp_tag_aging =
6043		ISP_NVRAM_TAG_AGE_LIMIT(nvram_data);
6044
6045	sdp->isp_selection_timeout =
6046		ISP_NVRAM_SELECTION_TIMEOUT(nvram_data);
6047
6048	sdp->isp_max_queue_depth =
6049		ISP_NVRAM_MAX_QUEUE_DEPTH(nvram_data);
6050
6051	sdp->isp_fast_mttr = ISP_NVRAM_FAST_MTTR_ENABLE(nvram_data);
6052
6053	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
6054	    0, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
6055	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
6056	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
6057	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
6058	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
6059	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
6060	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
6061
6062	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
6063		sdp->isp_devparam[tgt].dev_enable =
6064			ISP_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt);
6065		sdp->isp_devparam[tgt].exc_throttle =
6066			ISP_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt);
6067		sdp->isp_devparam[tgt].nvrm_offset =
6068			ISP_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt);
6069		sdp->isp_devparam[tgt].nvrm_period =
6070			ISP_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt);
6071		/*
6072		 * We probably shouldn't lie about this, but it
6073		 * it makes it much safer if we limit NVRAM values
6074		 * to sanity.
6075		 */
6076		if (isp->isp_type < ISP_HA_SCSI_1040) {
6077			/*
6078			 * If we're not ultra, we can't possibly
6079			 * be a shorter period than this.
6080			 */
6081			if (sdp->isp_devparam[tgt].nvrm_period < 0x19) {
6082				sdp->isp_devparam[tgt].nvrm_period = 0x19;
6083			}
6084			if (sdp->isp_devparam[tgt].nvrm_offset > 0xc) {
6085				sdp->isp_devparam[tgt].nvrm_offset = 0x0c;
6086			}
6087		} else {
6088			if (sdp->isp_devparam[tgt].nvrm_offset > 0x8) {
6089				sdp->isp_devparam[tgt].nvrm_offset = 0x8;
6090			}
6091		}
6092		sdp->isp_devparam[tgt].nvrm_flags = 0;
6093		if (ISP_NVRAM_TGT_RENEG(nvram_data, tgt))
6094			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
6095		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
6096		if (ISP_NVRAM_TGT_TQING(nvram_data, tgt))
6097			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
6098		if (ISP_NVRAM_TGT_SYNC(nvram_data, tgt))
6099			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
6100		if (ISP_NVRAM_TGT_WIDE(nvram_data, tgt))
6101			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
6102		if (ISP_NVRAM_TGT_PARITY(nvram_data, tgt))
6103			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
6104		if (ISP_NVRAM_TGT_DISC(nvram_data, tgt))
6105			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
6106		sdp->isp_devparam[tgt].actv_flags = 0; /* we don't know */
6107		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
6108		    0, tgt, sdp->isp_devparam[tgt].nvrm_flags,
6109		    sdp->isp_devparam[tgt].nvrm_offset,
6110		    sdp->isp_devparam[tgt].nvrm_period);
6111		sdp->isp_devparam[tgt].goal_offset =
6112		    sdp->isp_devparam[tgt].nvrm_offset;
6113		sdp->isp_devparam[tgt].goal_period =
6114		    sdp->isp_devparam[tgt].nvrm_period;
6115		sdp->isp_devparam[tgt].goal_flags =
6116		    sdp->isp_devparam[tgt].nvrm_flags;
6117	}
6118}
6119
6120static void
6121isp_parse_nvram_1080(struct ispsoftc *isp, int bus, u_int8_t *nvram_data)
6122{
6123	sdparam *sdp = (sdparam *) isp->isp_param;
6124	int tgt;
6125
6126	sdp += bus;
6127
6128	sdp->isp_fifo_threshold =
6129	    ISP1080_NVRAM_FIFO_THRESHOLD(nvram_data);
6130
6131	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
6132		sdp->isp_initiator_id =
6133		    ISP1080_NVRAM_INITIATOR_ID(nvram_data, bus);
6134
6135	sdp->isp_bus_reset_delay =
6136	    ISP1080_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
6137
6138	sdp->isp_retry_count =
6139	    ISP1080_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
6140
6141	sdp->isp_retry_delay =
6142	    ISP1080_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
6143
6144	sdp->isp_async_data_setup =
6145	    ISP1080_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
6146
6147	sdp->isp_req_ack_active_neg =
6148	    ISP1080_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
6149
6150	sdp->isp_data_line_active_neg =
6151	    ISP1080_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
6152
6153	sdp->isp_data_dma_burst_enabl =
6154	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
6155
6156	sdp->isp_cmd_dma_burst_enable =
6157	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
6158
6159	sdp->isp_selection_timeout =
6160	    ISP1080_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
6161
6162	sdp->isp_max_queue_depth =
6163	     ISP1080_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
6164
6165	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
6166	    bus, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
6167	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
6168	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
6169	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
6170	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
6171	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
6172	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
6173
6174
6175	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
6176		sdp->isp_devparam[tgt].dev_enable =
6177		    ISP1080_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
6178		sdp->isp_devparam[tgt].exc_throttle =
6179			ISP1080_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
6180		sdp->isp_devparam[tgt].nvrm_offset =
6181			ISP1080_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
6182		sdp->isp_devparam[tgt].nvrm_period =
6183			ISP1080_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
6184		sdp->isp_devparam[tgt].nvrm_flags = 0;
6185		if (ISP1080_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
6186			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
6187		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
6188		if (ISP1080_NVRAM_TGT_TQING(nvram_data, tgt, bus))
6189			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
6190		if (ISP1080_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
6191			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
6192		if (ISP1080_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
6193			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
6194		if (ISP1080_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
6195			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
6196		if (ISP1080_NVRAM_TGT_DISC(nvram_data, tgt, bus))
6197			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
6198		sdp->isp_devparam[tgt].actv_flags = 0;
6199		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
6200		    bus, tgt, sdp->isp_devparam[tgt].nvrm_flags,
6201		    sdp->isp_devparam[tgt].nvrm_offset,
6202		    sdp->isp_devparam[tgt].nvrm_period);
6203		sdp->isp_devparam[tgt].goal_offset =
6204		    sdp->isp_devparam[tgt].nvrm_offset;
6205		sdp->isp_devparam[tgt].goal_period =
6206		    sdp->isp_devparam[tgt].nvrm_period;
6207		sdp->isp_devparam[tgt].goal_flags =
6208		    sdp->isp_devparam[tgt].nvrm_flags;
6209	}
6210}
6211
6212static void
6213isp_parse_nvram_12160(struct ispsoftc *isp, int bus, u_int8_t *nvram_data)
6214{
6215	sdparam *sdp = (sdparam *) isp->isp_param;
6216	int tgt;
6217
6218	sdp += bus;
6219
6220	sdp->isp_fifo_threshold =
6221	    ISP12160_NVRAM_FIFO_THRESHOLD(nvram_data);
6222
6223	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
6224		sdp->isp_initiator_id =
6225		    ISP12160_NVRAM_INITIATOR_ID(nvram_data, bus);
6226
6227	sdp->isp_bus_reset_delay =
6228	    ISP12160_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
6229
6230	sdp->isp_retry_count =
6231	    ISP12160_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
6232
6233	sdp->isp_retry_delay =
6234	    ISP12160_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
6235
6236	sdp->isp_async_data_setup =
6237	    ISP12160_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
6238
6239	sdp->isp_req_ack_active_neg =
6240	    ISP12160_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
6241
6242	sdp->isp_data_line_active_neg =
6243	    ISP12160_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
6244
6245	sdp->isp_data_dma_burst_enabl =
6246	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
6247
6248	sdp->isp_cmd_dma_burst_enable =
6249	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
6250
6251	sdp->isp_selection_timeout =
6252	    ISP12160_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
6253
6254	sdp->isp_max_queue_depth =
6255	     ISP12160_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
6256
6257	isp_prt(isp, ISP_LOGDEBUG0, sc0, sc4,
6258	    bus, sdp->isp_fifo_threshold, sdp->isp_initiator_id,
6259	    sdp->isp_bus_reset_delay, sdp->isp_retry_count,
6260	    sdp->isp_retry_delay, sdp->isp_async_data_setup);
6261	isp_prt(isp, ISP_LOGDEBUG0, sc1, sc4,
6262	    sdp->isp_req_ack_active_neg, sdp->isp_data_line_active_neg,
6263	    sdp->isp_data_dma_burst_enabl, sdp->isp_cmd_dma_burst_enable,
6264	    sdp->isp_selection_timeout, sdp->isp_max_queue_depth);
6265
6266	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
6267		sdp->isp_devparam[tgt].dev_enable =
6268		    ISP12160_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
6269		sdp->isp_devparam[tgt].exc_throttle =
6270			ISP12160_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
6271		sdp->isp_devparam[tgt].nvrm_offset =
6272			ISP12160_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
6273		sdp->isp_devparam[tgt].nvrm_period =
6274			ISP12160_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
6275		sdp->isp_devparam[tgt].nvrm_flags = 0;
6276		if (ISP12160_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
6277			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
6278		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
6279		if (ISP12160_NVRAM_TGT_TQING(nvram_data, tgt, bus))
6280			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
6281		if (ISP12160_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
6282			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
6283		if (ISP12160_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
6284			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
6285		if (ISP12160_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
6286			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
6287		if (ISP12160_NVRAM_TGT_DISC(nvram_data, tgt, bus))
6288			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
6289		sdp->isp_devparam[tgt].actv_flags = 0;
6290		isp_prt(isp, ISP_LOGDEBUG0, sc2, sc4,
6291		    bus, tgt, sdp->isp_devparam[tgt].nvrm_flags,
6292		    sdp->isp_devparam[tgt].nvrm_offset,
6293		    sdp->isp_devparam[tgt].nvrm_period);
6294		sdp->isp_devparam[tgt].goal_offset =
6295		    sdp->isp_devparam[tgt].nvrm_offset;
6296		sdp->isp_devparam[tgt].goal_period =
6297		    sdp->isp_devparam[tgt].nvrm_period;
6298		sdp->isp_devparam[tgt].goal_flags =
6299		    sdp->isp_devparam[tgt].nvrm_flags;
6300	}
6301}
6302
6303static void
6304isp_parse_nvram_2100(struct ispsoftc *isp, u_int8_t *nvram_data)
6305{
6306	fcparam *fcp = (fcparam *) isp->isp_param;
6307	u_int64_t wwn;
6308
6309	/*
6310	 * There is NVRAM storage for both Port and Node entities-
6311	 * but the Node entity appears to be unused on all the cards
6312	 * I can find. However, we should account for this being set
6313	 * at some point in the future.
6314	 *
6315	 * Qlogic WWNs have an NAA of 2, but usually nothing shows up in
6316	 * bits 48..60. In the case of the 2202, it appears that they do
6317	 * use bit 48 to distinguish between the two instances on the card.
6318	 * The 2204, which I've never seen, *probably* extends this method.
6319	 */
6320	wwn = ISP2100_NVRAM_PORT_NAME(nvram_data);
6321	if (wwn) {
6322		isp_prt(isp, ISP_LOGCONFIG, "NVRAM Port WWN 0x%08x%08x",
6323		    (u_int32_t) (wwn >> 32), (u_int32_t) (wwn & 0xffffffff));
6324		if ((wwn >> 60) == 0) {
6325			wwn |= (((u_int64_t) 2)<< 60);
6326		}
6327	}
6328	fcp->isp_portwwn = wwn;
6329	if (IS_2200(isp) || IS_23XX(isp)) {
6330		wwn = ISP2200_NVRAM_NODE_NAME(nvram_data);
6331		if (wwn) {
6332			isp_prt(isp, ISP_LOGCONFIG, "NVRAM Node WWN 0x%08x%08x",
6333			    (u_int32_t) (wwn >> 32),
6334			    (u_int32_t) (wwn & 0xffffffff));
6335			if ((wwn >> 60) == 0) {
6336				wwn |= (((u_int64_t) 2)<< 60);
6337			}
6338		}
6339	} else {
6340		wwn &= ~((u_int64_t) 0xfff << 48);
6341	}
6342	fcp->isp_nodewwn = wwn;
6343
6344	/*
6345	 * Make sure we have both Node and Port as non-zero values.
6346	 */
6347	if (fcp->isp_nodewwn != 0 && fcp->isp_portwwn == 0) {
6348		fcp->isp_portwwn = fcp->isp_nodewwn;
6349	} else if (fcp->isp_nodewwn == 0 && fcp->isp_portwwn != 0) {
6350		fcp->isp_nodewwn = fcp->isp_portwwn;
6351	}
6352
6353	/*
6354	 * Make the Node and Port values sane if they're NAA == 2.
6355	 * This means to clear bits 48..56 for the Node WWN and
6356	 * make sure that there's some non-zero value in 48..56
6357	 * for the Port WWN.
6358	 */
6359	if (fcp->isp_nodewwn && fcp->isp_portwwn) {
6360		if ((fcp->isp_nodewwn & (((u_int64_t) 0xfff) << 48)) != 0 &&
6361		    (fcp->isp_nodewwn >> 60) == 2) {
6362			fcp->isp_nodewwn &= ~((u_int64_t) 0xfff << 48);
6363		}
6364		if ((fcp->isp_portwwn & (((u_int64_t) 0xfff) << 48)) == 0 &&
6365		    (fcp->isp_portwwn >> 60) == 2) {
6366			fcp->isp_portwwn |= ((u_int64_t) 1 << 56);
6367		}
6368	}
6369
6370	isp_prt(isp, ISP_LOGDEBUG0,
6371	    "NVRAM: maxfrmlen %d execthrottle %d fwoptions 0x%x loopid %x",
6372	    ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data),
6373	    ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data),
6374	    ISP2100_NVRAM_OPTIONS(nvram_data),
6375	    ISP2100_NVRAM_HARDLOOPID(nvram_data));
6376
6377	fcp->isp_maxalloc =
6378		ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data);
6379	if ((isp->isp_confopts & ISP_CFG_OWNFSZ) == 0)
6380		fcp->isp_maxfrmlen =
6381			ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data);
6382	fcp->isp_retry_delay =
6383		ISP2100_NVRAM_RETRY_DELAY(nvram_data);
6384	fcp->isp_retry_count =
6385		ISP2100_NVRAM_RETRY_COUNT(nvram_data);
6386	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
6387		fcp->isp_loopid =
6388			ISP2100_NVRAM_HARDLOOPID(nvram_data);
6389	if ((isp->isp_confopts & ISP_CFG_OWNEXCTHROTTLE) == 0)
6390		fcp->isp_execthrottle =
6391			ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data);
6392	fcp->isp_fwoptions = ISP2100_NVRAM_OPTIONS(nvram_data);
6393}
6394
6395#ifdef	ISP_FW_CRASH_DUMP
6396static void isp2200_fw_dump(struct ispsoftc *);
6397static void isp2300_fw_dump(struct ispsoftc *);
6398
6399static void
6400isp2200_fw_dump(struct ispsoftc *isp)
6401{
6402	int i, j;
6403	mbreg_t mbs;
6404	u_int16_t *ptr;
6405
6406	ptr = FCPARAM(isp)->isp_dump_data;
6407	if (ptr == NULL) {
6408		isp_prt(isp, ISP_LOGERR,
6409		   "No place to dump RISC registers and SRAM");
6410		return;
6411	}
6412	if (*ptr++) {
6413		isp_prt(isp, ISP_LOGERR,
6414		   "dump area for RISC registers and SRAM already used");
6415		return;
6416	}
6417	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
6418	for (i = 0; i < 100; i++) {
6419		USEC_DELAY(100);
6420		if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6421			break;
6422		}
6423	}
6424	if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6425		/*
6426		 * PBIU Registers
6427		 */
6428		for (i = 0; i < 8; i++) {
6429			*ptr++ = ISP_READ(isp, BIU_BLOCK + (i << 1));
6430		}
6431
6432		/*
6433		 * Mailbox Registers
6434		 */
6435		for (i = 0; i < 8; i++) {
6436			*ptr++ = ISP_READ(isp, MBOX_BLOCK + (i << 1));
6437		}
6438
6439		/*
6440		 * DMA Registers
6441		 */
6442		for (i = 0; i < 48; i++) {
6443			*ptr++ = ISP_READ(isp, DMA_BLOCK + 0x20 + (i << 1));
6444		}
6445
6446		/*
6447		 * RISC H/W Registers
6448		 */
6449		ISP_WRITE(isp, BIU2100_CSR, 0);
6450		for (i = 0; i < 16; i++) {
6451			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0xA0 + (i << 1));
6452		}
6453
6454		/*
6455		 * RISC GP Registers
6456		 */
6457		for (j = 0; j < 8; j++) {
6458			ISP_WRITE(isp, BIU_BLOCK + 0xA4, 0x2000 + (j << 8));
6459			for (i = 0; i < 16; i++) {
6460				*ptr++ =
6461				    ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6462			}
6463		}
6464
6465		/*
6466		 * Frame Buffer Hardware Registers
6467		 */
6468		ISP_WRITE(isp, BIU2100_CSR, 0x10);
6469		for (i = 0; i < 16; i++) {
6470			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6471		}
6472
6473		/*
6474		 * Fibre Protocol Module 0 Hardware Registers
6475		 */
6476		ISP_WRITE(isp, BIU2100_CSR, 0x20);
6477		for (i = 0; i < 64; i++) {
6478			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6479		}
6480
6481		/*
6482		 * Fibre Protocol Module 1 Hardware Registers
6483		 */
6484		ISP_WRITE(isp, BIU2100_CSR, 0x30);
6485		for (i = 0; i < 64; i++) {
6486			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6487		}
6488	} else {
6489		isp_prt(isp, ISP_LOGERR, "RISC Would Not Pause");
6490		return;
6491	}
6492	isp_prt(isp, ISP_LOGALL,
6493	   "isp_fw_dump: RISC registers dumped successfully");
6494	ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
6495	for (i = 0; i < 100; i++) {
6496		USEC_DELAY(100);
6497		if (ISP_READ(isp, OUTMAILBOX0) == 0) {
6498			break;
6499		}
6500	}
6501	if (ISP_READ(isp, OUTMAILBOX0) != 0) {
6502		isp_prt(isp, ISP_LOGERR, "Board Would Not Reset");
6503		return;
6504	}
6505	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
6506	for (i = 0; i < 100; i++) {
6507		USEC_DELAY(100);
6508		if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6509			break;
6510		}
6511	}
6512	if ((ISP_READ(isp, HCCR) & HCCR_PAUSE) == 0) {
6513		isp_prt(isp, ISP_LOGERR, "RISC Would Not Pause After Reset");
6514		return;
6515	}
6516	ISP_WRITE(isp, RISC_EMB, 0xf2);
6517	ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
6518	for (i = 0; i < 100; i++) {
6519		USEC_DELAY(100);
6520		if ((ISP_READ(isp, HCCR) & HCCR_PAUSE) == 0) {
6521			break;
6522		}
6523	}
6524	ENABLE_INTS(isp);
6525	mbs.param[0] = MBOX_READ_RAM_WORD;
6526	mbs.param[1] = 0x1000;
6527	isp->isp_mbxworkp = (void *) ptr;
6528	isp->isp_mbxwrk0 = 0xefff;	/* continuation count */
6529	isp->isp_mbxwrk1 = 0x1001;	/* next SRAM address */
6530	isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
6531	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
6532		isp_prt(isp, ISP_LOGWARN,
6533		    "RAM DUMP FAILED @ WORD %x", isp->isp_mbxwrk1);
6534		return;
6535	}
6536	ptr = isp->isp_mbxworkp;	/* finish fetch of final word */
6537	*ptr++ = isp->isp_mboxtmp[2];
6538	isp_prt(isp, ISP_LOGALL, "isp_fw_dump: SRAM dumped successfully");
6539	FCPARAM(isp)->isp_dump_data[0] = isp->isp_type; /* now used */
6540	(void) isp_async(isp, ISPASYNC_FW_DUMPED, 0);
6541}
6542
6543static void
6544isp2300_fw_dump(struct ispsoftc *isp)
6545{
6546	int i, j;
6547	mbreg_t mbs;
6548	u_int16_t *ptr;
6549
6550	ptr = FCPARAM(isp)->isp_dump_data;
6551	if (ptr == NULL) {
6552		isp_prt(isp, ISP_LOGERR,
6553		   "No place to dump RISC registers and SRAM");
6554		return;
6555	}
6556	if (*ptr++) {
6557		isp_prt(isp, ISP_LOGERR,
6558		   "dump area for RISC registers and SRAM already used");
6559		return;
6560	}
6561	ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
6562	for (i = 0; i < 100; i++) {
6563		USEC_DELAY(100);
6564		if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6565			break;
6566		}
6567	}
6568	if (ISP_READ(isp, HCCR) & HCCR_PAUSE) {
6569		/*
6570		 * PBIU registers
6571		 */
6572		for (i = 0; i < 8; i++) {
6573			*ptr++ = ISP_READ(isp, BIU_BLOCK + (i << 1));
6574		}
6575
6576		/*
6577		 * ReqQ-RspQ-Risc2Host Status registers
6578		 */
6579		for (i = 0; i < 8; i++) {
6580			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x10 + (i << 1));
6581		}
6582
6583		/*
6584		 * Mailbox Registers
6585		 */
6586		for (i = 0; i < 32; i++) {
6587			*ptr++ =
6588			    ISP_READ(isp, PCI_MBOX_REGS2300_OFF + (i << 1));
6589		}
6590
6591		/*
6592		 * Auto Request Response DMA registers
6593		 */
6594		ISP_WRITE(isp, BIU2100_CSR, 0x40);
6595		for (i = 0; i < 32; i++) {
6596			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6597		}
6598
6599		/*
6600		 * DMA registers
6601		 */
6602		ISP_WRITE(isp, BIU2100_CSR, 0x50);
6603		for (i = 0; i < 48; i++) {
6604			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6605		}
6606
6607		/*
6608		 * RISC hardware registers
6609		 */
6610		ISP_WRITE(isp, BIU2100_CSR, 0);
6611		for (i = 0; i < 16; i++) {
6612			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0xA0 + (i << 1));
6613		}
6614
6615		/*
6616		 * RISC GP? registers
6617		 */
6618		for (j = 0; j < 8; j++) {
6619			ISP_WRITE(isp, BIU_BLOCK + 0xA4, 0x2000 + (j << 9));
6620			for (i = 0; i < 16; i++) {
6621				*ptr++ =
6622				    ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6623			}
6624		}
6625
6626		/*
6627		 * frame buffer hardware registers
6628		 */
6629		ISP_WRITE(isp, BIU2100_CSR, 0x10);
6630		for (i = 0; i < 64; i++) {
6631			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6632		}
6633
6634		/*
6635		 * FPM B0 hardware registers
6636		 */
6637		ISP_WRITE(isp, BIU2100_CSR, 0x20);
6638		for (i = 0; i < 64; i++) {
6639			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6640		}
6641
6642		/*
6643		 * FPM B1 hardware registers
6644		 */
6645		ISP_WRITE(isp, BIU2100_CSR, 0x30);
6646		for (i = 0; i < 64; i++) {
6647			*ptr++ = ISP_READ(isp, BIU_BLOCK + 0x80 + (i << 1));
6648		}
6649	} else {
6650		isp_prt(isp, ISP_LOGERR, "RISC Would Not Pause");
6651		return;
6652	}
6653	isp_prt(isp, ISP_LOGALL,
6654	   "isp_fw_dump: RISC registers dumped successfully");
6655	ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
6656	for (i = 0; i < 100; i++) {
6657		USEC_DELAY(100);
6658		if (ISP_READ(isp, OUTMAILBOX0) == 0) {
6659			break;
6660		}
6661	}
6662	if (ISP_READ(isp, OUTMAILBOX0) != 0) {
6663		isp_prt(isp, ISP_LOGERR, "Board Would Not Reset");
6664		return;
6665	}
6666	ENABLE_INTS(isp);
6667	mbs.param[0] = MBOX_READ_RAM_WORD;
6668	mbs.param[1] = 0x800;
6669	isp->isp_mbxworkp = (void *) ptr;
6670	isp->isp_mbxwrk0 = 0xf7ff;	/* continuation count */
6671	isp->isp_mbxwrk1 = 0x801;	/* next SRAM address */
6672	isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
6673	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
6674		isp_prt(isp, ISP_LOGWARN,
6675		    "RAM DUMP FAILED @ WORD %x", isp->isp_mbxwrk1);
6676		return;
6677	}
6678	ptr = isp->isp_mbxworkp;	/* finish fetch of final word */
6679	*ptr++ = isp->isp_mboxtmp[2];
6680
6681	/*
6682	 * We don't have access to mailbox registers 8.. onward
6683	 * in our 'common' device model- so we have to set it
6684	 * here and hope it stays the same!
6685	 */
6686	ISP_WRITE(isp, PCI_MBOX_REGS2300_OFF + (8 << 1), 0x1);
6687
6688	mbs.param[0] = MBOX_READ_RAM_WORD_EXTENDED;
6689	mbs.param[1] = 0;
6690	isp->isp_mbxworkp = (void *) ptr;
6691	isp->isp_mbxwrk0 = 0xffff;	/* continuation count */
6692	isp->isp_mbxwrk1 = 0x1;		/* next SRAM address */
6693	isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
6694	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
6695		isp_prt(isp, ISP_LOGWARN,
6696		    "RAM DUMP FAILED @ WORD %x", 0x10000 + isp->isp_mbxwrk1);
6697		return;
6698	}
6699	ptr = isp->isp_mbxworkp;	/* finish final word */
6700	*ptr++ = mbs.param[2];
6701	isp_prt(isp, ISP_LOGALL, "isp_fw_dump: SRAM dumped successfully");
6702	FCPARAM(isp)->isp_dump_data[0] = isp->isp_type; /* now used */
6703	(void) isp_async(isp, ISPASYNC_FW_DUMPED, 0);
6704}
6705
6706void
6707isp_fw_dump(struct ispsoftc *isp)
6708{
6709	if (IS_2200(isp))
6710		isp2200_fw_dump(isp);
6711	else if (IS_23XX(isp))
6712		isp2300_fw_dump(isp);
6713}
6714#endif
6715