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