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