xref: /freebsd/sys/dev/isp/isp.c (revision 07ee9f80d73118450375bd991c5cabb8c85bd7a2)
1 /*-
2  *  Copyright (c) 1997-2009 by Matthew Jacob
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions
7  *  are met:
8  *
9  *  1. Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  *  2. Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  *
15  *  THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  *  ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  *  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  *  SUCH DAMAGE.
26  *
27  */
28 
29 /*
30  * Machine and OS Independent (well, as best as possible)
31  * code for the Qlogic ISP SCSI and FC-SCSI adapters.
32  */
33 
34 /*
35  * Inspiration and ideas about this driver are from Erik Moe's Linux driver
36  * (qlogicisp.c) and Dave Miller's SBus version of same (qlogicisp.c). Some
37  * ideas dredged from the Solaris driver.
38  */
39 
40 /*
41  * Include header file appropriate for platform we're building on.
42  */
43 #ifdef	__NetBSD__
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD$");
46 #include <dev/ic/isp_netbsd.h>
47 #endif
48 #ifdef	__FreeBSD__
49 #include <sys/cdefs.h>
50 __FBSDID("$FreeBSD$");
51 #include <dev/isp/isp_freebsd.h>
52 #endif
53 #ifdef	__OpenBSD__
54 #include <dev/ic/isp_openbsd.h>
55 #endif
56 #ifdef	__linux__
57 #include "isp_linux.h"
58 #endif
59 #ifdef	__svr4__
60 #include "isp_solaris.h"
61 #endif
62 
63 /*
64  * General defines
65  */
66 #define	MBOX_DELAY_COUNT	1000000 / 100
67 
68 /*
69  * Local static data
70  */
71 static const char notresp[] = "Not RESPONSE in RESPONSE Queue (type 0x%x) @ idx %d (next %d) nlooked %d";
72 static const char bun[] = "bad underrun (count %d, resid %d, status %s)";
73 static const char lipd[] = "Chan %d LIP destroyed %d active commands";
74 static const char sacq[] = "unable to acquire scratch area";
75 
76 static const uint8_t alpa_map[] = {
77 	0xef, 0xe8, 0xe4, 0xe2, 0xe1, 0xe0, 0xdc, 0xda,
78 	0xd9, 0xd6, 0xd5, 0xd4, 0xd3, 0xd2, 0xd1, 0xce,
79 	0xcd, 0xcc, 0xcb, 0xca, 0xc9, 0xc7, 0xc6, 0xc5,
80 	0xc3, 0xbc, 0xba, 0xb9, 0xb6, 0xb5, 0xb4, 0xb3,
81 	0xb2, 0xb1, 0xae, 0xad, 0xac, 0xab, 0xaa, 0xa9,
82 	0xa7, 0xa6, 0xa5, 0xa3, 0x9f, 0x9e, 0x9d, 0x9b,
83 	0x98, 0x97, 0x90, 0x8f, 0x88, 0x84, 0x82, 0x81,
84 	0x80, 0x7c, 0x7a, 0x79, 0x76, 0x75, 0x74, 0x73,
85 	0x72, 0x71, 0x6e, 0x6d, 0x6c, 0x6b, 0x6a, 0x69,
86 	0x67, 0x66, 0x65, 0x63, 0x5c, 0x5a, 0x59, 0x56,
87 	0x55, 0x54, 0x53, 0x52, 0x51, 0x4e, 0x4d, 0x4c,
88 	0x4b, 0x4a, 0x49, 0x47, 0x46, 0x45, 0x43, 0x3c,
89 	0x3a, 0x39, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31,
90 	0x2e, 0x2d, 0x2c, 0x2b, 0x2a, 0x29, 0x27, 0x26,
91 	0x25, 0x23, 0x1f, 0x1e, 0x1d, 0x1b, 0x18, 0x17,
92 	0x10, 0x0f, 0x08, 0x04, 0x02, 0x01, 0x00
93 };
94 
95 /*
96  * Local function prototypes.
97  */
98 static int isp_parse_async(ispsoftc_t *, uint16_t);
99 static int isp_parse_async_fc(ispsoftc_t *, uint16_t);
100 static int isp_handle_other_response(ispsoftc_t *, int, isphdr_t *, uint32_t *);
101 static void isp_parse_status(ispsoftc_t *, ispstatusreq_t *, XS_T *, long *); static void
102 isp_parse_status_24xx(ispsoftc_t *, isp24xx_statusreq_t *, XS_T *, long *);
103 static void isp_fastpost_complete(ispsoftc_t *, uint32_t);
104 static int isp_mbox_continue(ispsoftc_t *);
105 static void isp_scsi_init(ispsoftc_t *);
106 static void isp_scsi_channel_init(ispsoftc_t *, int);
107 static void isp_fibre_init(ispsoftc_t *);
108 static void isp_fibre_init_2400(ispsoftc_t *);
109 static void isp_clear_portdb(ispsoftc_t *, int);
110 static void isp_mark_portdb(ispsoftc_t *, int);
111 static int isp_plogx(ispsoftc_t *, int, uint16_t, uint32_t, int);
112 static int isp_port_login(ispsoftc_t *, uint16_t, uint32_t);
113 static int isp_port_logout(ispsoftc_t *, uint16_t, uint32_t);
114 static int isp_getpdb(ispsoftc_t *, int, uint16_t, isp_pdb_t *);
115 static int isp_gethandles(ispsoftc_t *, int, uint16_t *, int *, int);
116 static void isp_dump_chip_portdb(ispsoftc_t *, int);
117 static uint64_t isp_get_wwn(ispsoftc_t *, int, int, int);
118 static int isp_fclink_test(ispsoftc_t *, int, int);
119 static int isp_pdb_sync(ispsoftc_t *, int);
120 static int isp_scan_loop(ispsoftc_t *, int);
121 static int isp_gid_ft_sns(ispsoftc_t *, int);
122 static int isp_gid_ft_ct_passthru(ispsoftc_t *, int);
123 static int isp_scan_fabric(ispsoftc_t *, int);
124 static int isp_login_device(ispsoftc_t *, int, uint32_t, isp_pdb_t *, uint16_t *);
125 static int isp_send_change_request(ispsoftc_t *, int);
126 static int isp_register_fc4_type(ispsoftc_t *, int);
127 static int isp_register_fc4_type_24xx(ispsoftc_t *, int);
128 static int isp_register_fc4_features_24xx(ispsoftc_t *, int);
129 static int isp_register_port_name_24xx(ispsoftc_t *, int);
130 static int isp_register_node_name_24xx(ispsoftc_t *, int);
131 static uint16_t isp_next_handle(ispsoftc_t *, uint16_t *);
132 static int isp_fw_state(ispsoftc_t *, int);
133 static void isp_mboxcmd_qnw(ispsoftc_t *, mbreg_t *, int);
134 static void isp_mboxcmd(ispsoftc_t *, mbreg_t *);
135 
136 static void isp_spi_update(ispsoftc_t *, int);
137 static void isp_setdfltsdparm(ispsoftc_t *);
138 static void isp_setdfltfcparm(ispsoftc_t *, int);
139 static int isp_read_nvram(ispsoftc_t *, int);
140 static int isp_read_nvram_2400(ispsoftc_t *, uint8_t *);
141 static void isp_rdnvram_word(ispsoftc_t *, int, uint16_t *);
142 static void isp_rd_2400_nvram(ispsoftc_t *, uint32_t, uint32_t *);
143 static void isp_parse_nvram_1020(ispsoftc_t *, uint8_t *);
144 static void isp_parse_nvram_1080(ispsoftc_t *, int, uint8_t *);
145 static void isp_parse_nvram_12160(ispsoftc_t *, int, uint8_t *);
146 static void isp_parse_nvram_2100(ispsoftc_t *, uint8_t *);
147 static void isp_parse_nvram_2400(ispsoftc_t *, uint8_t *);
148 
149 static void
150 isp_change_fw_state(ispsoftc_t *isp, int chan, int state)
151 {
152 	fcparam *fcp = FCPARAM(isp, chan);
153 
154 	if (fcp->isp_fwstate == state)
155 		return;
156 	isp_prt(isp, ISP_LOGCONFIG|ISP_LOG_SANCFG,
157 	    "Chan %d Firmware state <%s->%s>", chan,
158 	    isp_fc_fw_statename(fcp->isp_fwstate), isp_fc_fw_statename(state));
159 	fcp->isp_fwstate = state;
160 }
161 
162 /*
163  * Reset Hardware.
164  *
165  * Hit the chip over the head, download new f/w if available and set it running.
166  *
167  * Locking done elsewhere.
168  */
169 
170 void
171 isp_reset(ispsoftc_t *isp, int do_load_defaults)
172 {
173 	mbreg_t mbs;
174 	char *buf;
175 	uint64_t fwt;
176 	uint32_t code_org, val;
177 	int loops, i, dodnld = 1;
178 	const char *btype = "????";
179 	static const char dcrc[] = "Downloaded RISC Code Checksum Failure";
180 
181 	isp->isp_state = ISP_NILSTATE;
182 	if (isp->isp_dead) {
183 		isp_shutdown(isp);
184 		ISP_DISABLE_INTS(isp);
185 		return;
186 	}
187 
188 	/*
189 	 * Basic types (SCSI, FibreChannel and PCI or SBus)
190 	 * have been set in the MD code. We figure out more
191 	 * here. Possibly more refined types based upon PCI
192 	 * identification. Chip revision has been gathered.
193 	 *
194 	 * After we've fired this chip up, zero out the conf1 register
195 	 * for SCSI adapters and do other settings for the 2100.
196 	 */
197 
198 	ISP_DISABLE_INTS(isp);
199 
200 	/*
201 	 * Pick an initial maxcmds value which will be used
202 	 * to allocate xflist pointer space. It may be changed
203 	 * later by the firmware.
204 	 */
205 	if (IS_24XX(isp)) {
206 		isp->isp_maxcmds = 4096;
207 	} else if (IS_2322(isp)) {
208 		isp->isp_maxcmds = 2048;
209 	} else if (IS_23XX(isp) || IS_2200(isp)) {
210 		isp->isp_maxcmds = 1024;
211  	} else {
212 		isp->isp_maxcmds = 512;
213 	}
214 
215 	/*
216 	 * Set up DMA for the request and response queues.
217 	 *
218 	 * We do this now so we can use the request queue
219 	 * for dma to load firmware from.
220 	 */
221 	if (ISP_MBOXDMASETUP(isp) != 0) {
222 		isp_prt(isp, ISP_LOGERR, "Cannot setup DMA");
223 		return;
224 	}
225 
226 	/*
227 	 * Set up default request/response queue in-pointer/out-pointer
228 	 * register indices.
229 	 */
230 	if (IS_24XX(isp)) {
231 		isp->isp_rqstinrp = BIU2400_REQINP;
232 		isp->isp_rqstoutrp = BIU2400_REQOUTP;
233 		isp->isp_respinrp = BIU2400_RSPINP;
234 		isp->isp_respoutrp = BIU2400_RSPOUTP;
235 	} else if (IS_23XX(isp)) {
236 		isp->isp_rqstinrp = BIU_REQINP;
237 		isp->isp_rqstoutrp = BIU_REQOUTP;
238 		isp->isp_respinrp = BIU_RSPINP;
239 		isp->isp_respoutrp = BIU_RSPOUTP;
240 	} else {
241 		isp->isp_rqstinrp = INMAILBOX4;
242 		isp->isp_rqstoutrp = OUTMAILBOX4;
243 		isp->isp_respinrp = OUTMAILBOX5;
244 		isp->isp_respoutrp = INMAILBOX5;
245 	}
246 
247 	/*
248 	 * Put the board into PAUSE mode (so we can read the SXP registers
249 	 * or write FPM/FBM registers).
250 	 */
251 	if (IS_24XX(isp)) {
252 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_HOST_INT);
253 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
254 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_PAUSE);
255 	} else {
256 		ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
257 	}
258 
259 	if (IS_FC(isp)) {
260 		switch (isp->isp_type) {
261 		case ISP_HA_FC_2100:
262 			btype = "2100";
263 			break;
264 		case ISP_HA_FC_2200:
265 			btype = "2200";
266 			break;
267 		case ISP_HA_FC_2300:
268 			btype = "2300";
269 			break;
270 		case ISP_HA_FC_2312:
271 			btype = "2312";
272 			break;
273 		case ISP_HA_FC_2322:
274 			btype = "2322";
275 			break;
276 		case ISP_HA_FC_2400:
277 			btype = "2422";
278 			break;
279 		case ISP_HA_FC_2500:
280 			btype = "2532";
281 			break;
282 		case ISP_HA_FC_2600:
283 			btype = "2031";
284 			break;
285 		default:
286 			break;
287 		}
288 
289 		if (!IS_24XX(isp)) {
290 			/*
291 			 * While we're paused, reset the FPM module and FBM
292 			 * fifos.
293 			 */
294 			ISP_WRITE(isp, BIU2100_CSR, BIU2100_FPM0_REGS);
295 			ISP_WRITE(isp, FPM_DIAG_CONFIG, FPM_SOFT_RESET);
296 			ISP_WRITE(isp, BIU2100_CSR, BIU2100_FB_REGS);
297 			ISP_WRITE(isp, FBM_CMD, FBMCMD_FIFO_RESET_ALL);
298 			ISP_WRITE(isp, BIU2100_CSR, BIU2100_RISC_REGS);
299 		}
300 	} else if (IS_1240(isp)) {
301 		sdparam *sdp;
302 
303 		btype = "1240";
304 		isp->isp_clock = 60;
305 		sdp = SDPARAM(isp, 0);
306 		sdp->isp_ultramode = 1;
307 		sdp = SDPARAM(isp, 1);
308 		sdp->isp_ultramode = 1;
309 		/*
310 		 * XXX: Should probably do some bus sensing.
311 		 */
312 	} else if (IS_ULTRA3(isp)) {
313 		sdparam *sdp = isp->isp_param;
314 
315 		isp->isp_clock = 100;
316 
317 		if (IS_10160(isp))
318 			btype = "10160";
319 		else if (IS_12160(isp))
320 			btype = "12160";
321 		else
322 			btype = "<UNKLVD>";
323 		sdp->isp_lvdmode = 1;
324 
325 		if (IS_DUALBUS(isp)) {
326 			sdp++;
327 			sdp->isp_lvdmode = 1;
328 		}
329 	} else if (IS_ULTRA2(isp)) {
330 		static const char m[] = "bus %d is in %s Mode";
331 		uint16_t l;
332 		sdparam *sdp = SDPARAM(isp, 0);
333 
334 		isp->isp_clock = 100;
335 
336 		if (IS_1280(isp))
337 			btype = "1280";
338 		else if (IS_1080(isp))
339 			btype = "1080";
340 		else
341 			btype = "<UNKLVD>";
342 
343 		l = ISP_READ(isp, SXP_PINS_DIFF) & ISP1080_MODE_MASK;
344 		switch (l) {
345 		case ISP1080_LVD_MODE:
346 			sdp->isp_lvdmode = 1;
347 			isp_prt(isp, ISP_LOGCONFIG, m, 0, "LVD");
348 			break;
349 		case ISP1080_HVD_MODE:
350 			sdp->isp_diffmode = 1;
351 			isp_prt(isp, ISP_LOGCONFIG, m, 0, "Differential");
352 			break;
353 		case ISP1080_SE_MODE:
354 			sdp->isp_ultramode = 1;
355 			isp_prt(isp, ISP_LOGCONFIG, m, 0, "Single-Ended");
356 			break;
357 		default:
358 			isp_prt(isp, ISP_LOGERR,
359 			    "unknown mode on bus %d (0x%x)", 0, l);
360 			break;
361 		}
362 
363 		if (IS_DUALBUS(isp)) {
364 			sdp = SDPARAM(isp, 1);
365 			l = ISP_READ(isp, SXP_PINS_DIFF|SXP_BANK1_SELECT);
366 			l &= ISP1080_MODE_MASK;
367 			switch (l) {
368 			case ISP1080_LVD_MODE:
369 				sdp->isp_lvdmode = 1;
370 				isp_prt(isp, ISP_LOGCONFIG, m, 1, "LVD");
371 				break;
372 			case ISP1080_HVD_MODE:
373 				sdp->isp_diffmode = 1;
374 				isp_prt(isp, ISP_LOGCONFIG,
375 				    m, 1, "Differential");
376 				break;
377 			case ISP1080_SE_MODE:
378 				sdp->isp_ultramode = 1;
379 				isp_prt(isp, ISP_LOGCONFIG,
380 				    m, 1, "Single-Ended");
381 				break;
382 			default:
383 				isp_prt(isp, ISP_LOGERR,
384 				    "unknown mode on bus %d (0x%x)", 1, l);
385 				break;
386 			}
387 		}
388 	} else {
389 		sdparam *sdp = SDPARAM(isp, 0);
390 		i = ISP_READ(isp, BIU_CONF0) & BIU_CONF0_HW_MASK;
391 		switch (i) {
392 		default:
393 			isp_prt(isp, ISP_LOGALL, "Unknown Chip Type 0x%x", i);
394 			/* FALLTHROUGH */
395 		case 1:
396 			btype = "1020";
397 			isp->isp_type = ISP_HA_SCSI_1020;
398 			isp->isp_clock = 40;
399 			break;
400 		case 2:
401 			/*
402 			 * Some 1020A chips are Ultra Capable, but don't
403 			 * run the clock rate up for that unless told to
404 			 * do so by the Ultra Capable bits being set.
405 			 */
406 			btype = "1020A";
407 			isp->isp_type = ISP_HA_SCSI_1020A;
408 			isp->isp_clock = 40;
409 			break;
410 		case 3:
411 			btype = "1040";
412 			isp->isp_type = ISP_HA_SCSI_1040;
413 			isp->isp_clock = 60;
414 			break;
415 		case 4:
416 			btype = "1040A";
417 			isp->isp_type = ISP_HA_SCSI_1040A;
418 			isp->isp_clock = 60;
419 			break;
420 		case 5:
421 			btype = "1040B";
422 			isp->isp_type = ISP_HA_SCSI_1040B;
423 			isp->isp_clock = 60;
424 			break;
425 		case 6:
426 			btype = "1040C";
427 			isp->isp_type = ISP_HA_SCSI_1040C;
428 			isp->isp_clock = 60;
429                         break;
430 		}
431 		/*
432 		 * Now, while we're at it, gather info about ultra
433 		 * and/or differential mode.
434 		 */
435 		if (ISP_READ(isp, SXP_PINS_DIFF) & SXP_PINS_DIFF_MODE) {
436 			isp_prt(isp, ISP_LOGCONFIG, "Differential Mode");
437 			sdp->isp_diffmode = 1;
438 		} else {
439 			sdp->isp_diffmode = 0;
440 		}
441 		i = ISP_READ(isp, RISC_PSR);
442 		if (isp->isp_bustype == ISP_BT_SBUS) {
443 			i &= RISC_PSR_SBUS_ULTRA;
444 		} else {
445 			i &= RISC_PSR_PCI_ULTRA;
446 		}
447 		if (i != 0) {
448 			isp_prt(isp, ISP_LOGCONFIG, "Ultra Mode Capable");
449 			sdp->isp_ultramode = 1;
450 			/*
451 			 * If we're in Ultra Mode, we have to be 60MHz clock-
452 			 * even for the SBus version.
453 			 */
454 			isp->isp_clock = 60;
455 		} else {
456 			sdp->isp_ultramode = 0;
457 			/*
458 			 * Clock is known. Gronk.
459 			 */
460 		}
461 
462 		/*
463 		 * Machine dependent clock (if set) overrides
464 		 * our generic determinations.
465 		 */
466 		if (isp->isp_mdvec->dv_clock) {
467 			if (isp->isp_mdvec->dv_clock < isp->isp_clock) {
468 				isp->isp_clock = isp->isp_mdvec->dv_clock;
469 			}
470 		}
471 
472 	}
473 
474 	/*
475 	 * Clear instrumentation
476 	 */
477 	isp->isp_intcnt = isp->isp_intbogus = 0;
478 
479 	/*
480 	 * Do MD specific pre initialization
481 	 */
482 	ISP_RESET0(isp);
483 
484 	/*
485 	 * Hit the chip over the head with hammer,
486 	 * and give it a chance to recover.
487 	 */
488 
489 	if (IS_SCSI(isp)) {
490 		ISP_WRITE(isp, BIU_ICR, BIU_ICR_SOFT_RESET);
491 		/*
492 		 * A slight delay...
493 		 */
494 		ISP_DELAY(100);
495 
496 		/*
497 		 * Clear data && control DMA engines.
498 		 */
499 		ISP_WRITE(isp, CDMA_CONTROL, DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
500 		ISP_WRITE(isp, DDMA_CONTROL, DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
501 
502 
503 	} else if (IS_24XX(isp)) {
504 		/*
505 		 * Stop DMA and wait for it to stop.
506 		 */
507 		ISP_WRITE(isp, BIU2400_CSR, BIU2400_DMA_STOP|(3 << 4));
508 		for (val = loops = 0; loops < 30000; loops++) {
509 			ISP_DELAY(10);
510 			val = ISP_READ(isp, BIU2400_CSR);
511 			if ((val & BIU2400_DMA_ACTIVE) == 0) {
512 				break;
513 			}
514 		}
515 		if (val & BIU2400_DMA_ACTIVE) {
516 			ISP_RESET0(isp);
517 			isp_prt(isp, ISP_LOGERR, "DMA Failed to Stop on Reset");
518 			return;
519 		}
520 		/*
521 		 * Hold it in SOFT_RESET and STOP state for 100us.
522 		 */
523 		ISP_WRITE(isp, BIU2400_CSR, BIU2400_SOFT_RESET|BIU2400_DMA_STOP|(3 << 4));
524 		ISP_DELAY(100);
525 		for (loops = 0; loops < 10000; loops++) {
526 			ISP_DELAY(5);
527 			val = ISP_READ(isp, OUTMAILBOX0);
528 		}
529 		for (val = loops = 0; loops < 500000; loops ++) {
530 			val = ISP_READ(isp, BIU2400_CSR);
531 			if ((val & BIU2400_SOFT_RESET) == 0) {
532 				break;
533 			}
534 		}
535 		if (val & BIU2400_SOFT_RESET) {
536 			ISP_RESET0(isp);
537 			isp_prt(isp, ISP_LOGERR, "Failed to come out of reset");
538 			return;
539 		}
540 	} else {
541 		ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
542 		/*
543 		 * A slight delay...
544 		 */
545 		ISP_DELAY(100);
546 
547 		/*
548 		 * Clear data && control DMA engines.
549 		 */
550 		ISP_WRITE(isp, CDMA2100_CONTROL, DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
551 		ISP_WRITE(isp, TDMA2100_CONTROL, DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
552 		ISP_WRITE(isp, RDMA2100_CONTROL, DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
553 	}
554 
555 	/*
556 	 * Wait for ISP to be ready to go...
557 	 */
558 	loops = MBOX_DELAY_COUNT;
559 	for (;;) {
560 		if (IS_SCSI(isp)) {
561 			if (!(ISP_READ(isp, BIU_ICR) & BIU_ICR_SOFT_RESET)) {
562 				break;
563 			}
564 		} else if (IS_24XX(isp)) {
565 			if (ISP_READ(isp, OUTMAILBOX0) == 0) {
566 				break;
567 			}
568 		} else {
569 			if (!(ISP_READ(isp, BIU2100_CSR) & BIU2100_SOFT_RESET))
570 				break;
571 		}
572 		ISP_DELAY(100);
573 		if (--loops < 0) {
574 			ISP_DUMPREGS(isp, "chip reset timed out");
575 			ISP_RESET0(isp);
576 			return;
577 		}
578 	}
579 
580 	/*
581 	 * After we've fired this chip up, zero out the conf1 register
582 	 * for SCSI adapters and other settings for the 2100.
583 	 */
584 
585 	if (IS_SCSI(isp)) {
586 		ISP_WRITE(isp, BIU_CONF1, 0);
587 	} else if (!IS_24XX(isp)) {
588 		ISP_WRITE(isp, BIU2100_CSR, 0);
589 	}
590 
591 	/*
592 	 * Reset RISC Processor
593 	 */
594 	if (IS_24XX(isp)) {
595 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_RESET);
596 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_RELEASE);
597 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RESET);
598 	} else {
599 		ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
600 		ISP_DELAY(100);
601 		ISP_WRITE(isp, BIU_SEMA, 0);
602 	}
603 
604 	/*
605 	 * Post-RISC Reset stuff.
606 	 */
607 	if (IS_24XX(isp)) {
608 		for (val = loops = 0; loops < 5000000; loops++) {
609 			ISP_DELAY(5);
610 			val = ISP_READ(isp, OUTMAILBOX0);
611 			if (val == 0) {
612 				break;
613 			}
614 		}
615 		if (val != 0) {
616 			ISP_RESET0(isp);
617 			isp_prt(isp, ISP_LOGERR, "reset didn't clear");
618 			return;
619 		}
620 	} else if (IS_SCSI(isp)) {
621 		uint16_t tmp = isp->isp_mdvec->dv_conf1;
622 		/*
623 		 * Busted FIFO. Turn off all but burst enables.
624 		 */
625 		if (isp->isp_type == ISP_HA_SCSI_1040A) {
626 			tmp &= BIU_BURST_ENABLE;
627 		}
628 		ISP_SETBITS(isp, BIU_CONF1, tmp);
629 		if (tmp & BIU_BURST_ENABLE) {
630 			ISP_SETBITS(isp, CDMA_CONF, DMA_ENABLE_BURST);
631 			ISP_SETBITS(isp, DDMA_CONF, DMA_ENABLE_BURST);
632 		}
633 		if (SDPARAM(isp, 0)->isp_ptisp) {
634 			if (SDPARAM(isp, 0)->isp_ultramode) {
635 				while (ISP_READ(isp, RISC_MTR) != 0x1313) {
636 					ISP_WRITE(isp, RISC_MTR, 0x1313);
637 					ISP_WRITE(isp, HCCR, HCCR_CMD_STEP);
638 				}
639 			} else {
640 				ISP_WRITE(isp, RISC_MTR, 0x1212);
641 			}
642 			/*
643 			 * PTI specific register
644 			 */
645 			ISP_WRITE(isp, RISC_EMB, DUAL_BANK);
646 		} else {
647 			ISP_WRITE(isp, RISC_MTR, 0x1212);
648 		}
649 		ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
650 	} else {
651 		ISP_WRITE(isp, RISC_MTR2100, 0x1212);
652 		if (IS_2200(isp) || IS_23XX(isp)) {
653 			ISP_WRITE(isp, HCCR, HCCR_2X00_DISABLE_PARITY_PAUSE);
654 		}
655 		ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
656 	}
657 
658 	ISP_WRITE(isp, isp->isp_rqstinrp, 0);
659 	ISP_WRITE(isp, isp->isp_rqstoutrp, 0);
660 	ISP_WRITE(isp, isp->isp_respinrp, 0);
661 	ISP_WRITE(isp, isp->isp_respoutrp, 0);
662 	if (IS_24XX(isp)) {
663 		if (!IS_26XX(isp)) {
664 			ISP_WRITE(isp, BIU2400_PRI_REQINP, 0);
665 			ISP_WRITE(isp, BIU2400_PRI_REQOUTP, 0);
666 		}
667 		ISP_WRITE(isp, BIU2400_ATIO_RSPINP, 0);
668 		ISP_WRITE(isp, BIU2400_ATIO_RSPOUTP, 0);
669 	}
670 
671 	/*
672 	 * Do MD specific post initialization
673 	 */
674 	ISP_RESET1(isp);
675 
676 	/*
677 	 * Wait for everything to finish firing up.
678 	 *
679 	 * Avoid doing this on early 2312s because you can generate a PCI
680 	 * parity error (chip breakage).
681 	 */
682 	if (IS_2312(isp) && isp->isp_revision < 2) {
683 		ISP_DELAY(100);
684 	} else {
685 		loops = MBOX_DELAY_COUNT;
686 		while (ISP_READ(isp, OUTMAILBOX0) == MBOX_BUSY) {
687 			ISP_DELAY(100);
688 			if (--loops < 0) {
689 				ISP_RESET0(isp);
690 				isp_prt(isp, ISP_LOGERR, "MBOX_BUSY never cleared on reset");
691 				return;
692 			}
693 		}
694 	}
695 
696 	/*
697 	 * Up until this point we've done everything by just reading or
698 	 * setting registers. From this point on we rely on at least *some*
699 	 * kind of firmware running in the card.
700 	 */
701 
702 	/*
703 	 * Do some sanity checking by running a NOP command.
704 	 * If it succeeds, the ROM firmware is now running.
705 	 */
706 	MBSINIT(&mbs, MBOX_NO_OP, MBLOGALL, 0);
707 	isp_mboxcmd(isp, &mbs);
708 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
709 		isp_prt(isp, ISP_LOGERR, "NOP command failed (%x)", mbs.param[0]);
710 		ISP_RESET0(isp);
711 		return;
712 	}
713 
714 	/*
715 	 * Do some operational tests
716 	 */
717 
718 	if (IS_SCSI(isp) || IS_24XX(isp)) {
719 		static const uint16_t patterns[MAX_MAILBOX] = {
720 			0x0000, 0xdead, 0xbeef, 0xffff,
721 			0xa5a5, 0x5a5a, 0x7f7f, 0x7ff7,
722 			0x3421, 0xabcd, 0xdcba, 0xfeef,
723 			0xbead, 0xdebe, 0x2222, 0x3333,
724 			0x5555, 0x6666, 0x7777, 0xaaaa,
725 			0xffff, 0xdddd, 0x9999, 0x1fbc,
726 			0x6666, 0x6677, 0x1122, 0x33ff,
727 			0x0000, 0x0001, 0x1000, 0x1010,
728 		};
729 		int nmbox = ISP_NMBOX(isp);
730 		if (IS_SCSI(isp))
731 			nmbox = 6;
732 		MBSINIT(&mbs, MBOX_MAILBOX_REG_TEST, MBLOGALL, 0);
733 		for (i = 1; i < nmbox; i++) {
734 			mbs.param[i] = patterns[i];
735 		}
736 		isp_mboxcmd(isp, &mbs);
737 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
738 			ISP_RESET0(isp);
739 			return;
740 		}
741 		for (i = 1; i < nmbox; i++) {
742 			if (mbs.param[i] != patterns[i]) {
743 				ISP_RESET0(isp);
744 				isp_prt(isp, ISP_LOGERR, "Register Test Failed at Register %d: should have 0x%04x but got 0x%04x", i, patterns[i], mbs.param[i]);
745 				return;
746 			}
747 		}
748 	}
749 
750 	/*
751 	 * Download new Firmware, unless requested not to do so.
752 	 * This is made slightly trickier in some cases where the
753 	 * firmware of the ROM revision is newer than the revision
754 	 * compiled into the driver. So, where we used to compare
755 	 * versions of our f/w and the ROM f/w, now we just see
756 	 * whether we have f/w at all and whether a config flag
757 	 * has disabled our download.
758 	 */
759 	if ((isp->isp_mdvec->dv_ispfw == NULL) || (isp->isp_confopts & ISP_CFG_NORELOAD)) {
760 		dodnld = 0;
761 	}
762 
763 	if (IS_24XX(isp)) {
764 		code_org = ISP_CODE_ORG_2400;
765 	} else if (IS_23XX(isp)) {
766 		code_org = ISP_CODE_ORG_2300;
767 	} else {
768 		code_org = ISP_CODE_ORG;
769 	}
770 
771 	isp->isp_loaded_fw = 0;
772 	if (dodnld && IS_24XX(isp)) {
773 		const uint32_t *ptr = isp->isp_mdvec->dv_ispfw;
774 		int wordload;
775 
776 		/*
777 		 * Keep loading until we run out of f/w.
778 		 */
779 		code_org = ptr[2];	/* 1st load address is our start addr */
780 		wordload = 0;
781 
782 		for (;;) {
783 			uint32_t la, wi, wl;
784 
785 			isp_prt(isp, ISP_LOGDEBUG0, "load 0x%x words of code at load address 0x%x", ptr[3], ptr[2]);
786 
787 			wi = 0;
788 			la = ptr[2];
789 			wl = ptr[3];
790 
791 			while (wi < ptr[3]) {
792 				uint32_t *cp;
793 				uint32_t nw;
794 
795 				nw = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp)) >> 2;
796 				if (nw > wl) {
797 					nw = wl;
798 				}
799 				cp = isp->isp_rquest;
800 				for (i = 0; i < nw; i++) {
801 					ISP_IOXPUT_32(isp,  ptr[wi++], &cp[i]);
802 					wl--;
803 				}
804 				MEMORYBARRIER(isp, SYNC_REQUEST, 0, ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp)), -1);
805 	again:
806 				MBSINIT(&mbs, 0, MBLOGALL, 0);
807 				if (la < 0x10000 && nw < 0x10000) {
808 					mbs.param[0] = MBOX_LOAD_RISC_RAM_2100;
809 					mbs.param[1] = la;
810 					mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
811 					mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
812 					mbs.param[4] = nw;
813 					mbs.param[6] = DMA_WD3(isp->isp_rquest_dma);
814 					mbs.param[7] = DMA_WD2(isp->isp_rquest_dma);
815 					isp_prt(isp, ISP_LOGDEBUG0, "LOAD RISC RAM 2100 %u words at load address 0x%x", nw, la);
816 				} else if (wordload) {
817 					union {
818 						const uint32_t *cp;
819 						uint32_t *np;
820 					} ucd;
821 					ucd.cp = (const uint32_t *)cp;
822 					mbs.param[0] = MBOX_WRITE_RAM_WORD_EXTENDED;
823 					mbs.param[1] = la;
824 					mbs.param[2] = (*ucd.np);
825 					mbs.param[3] = (*ucd.np) >> 16;
826 					mbs.param[8] = la >> 16;
827 					isp->isp_mbxwrk0 = nw - 1;
828 					isp->isp_mbxworkp = ucd.np+1;
829 					isp->isp_mbxwrk1 = (la + 1);
830 					isp->isp_mbxwrk8 = (la + 1) >> 16;
831 					isp_prt(isp, ISP_LOGDEBUG0, "WRITE RAM WORD EXTENDED %u words at load address 0x%x", nw, la);
832 				} else {
833 					mbs.param[0] = MBOX_LOAD_RISC_RAM;
834 					mbs.param[1] = la;
835 					mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
836 					mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
837 					mbs.param[4] = nw >> 16;
838 					mbs.param[5] = nw;
839 					mbs.param[6] = DMA_WD3(isp->isp_rquest_dma);
840 					mbs.param[7] = DMA_WD2(isp->isp_rquest_dma);
841 					mbs.param[8] = la >> 16;
842 					isp_prt(isp, ISP_LOGDEBUG0, "LOAD RISC RAM %u words at load address 0x%x", nw, la);
843 				}
844 				isp_mboxcmd(isp, &mbs);
845 				if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
846 					if (mbs.param[0] == MBOX_HOST_INTERFACE_ERROR) {
847 						isp_prt(isp, ISP_LOGERR, "switching to word load");
848 						wordload = 1;
849 						goto again;
850 					}
851 					isp_prt(isp, ISP_LOGERR, "F/W Risc Ram Load Failed");
852 					ISP_RESET0(isp);
853 					return;
854 				}
855 				la += nw;
856 			}
857 
858 			if (ptr[1] == 0) {
859 				break;
860 			}
861 			ptr += ptr[3];
862 		}
863 		isp->isp_loaded_fw = 1;
864 	} else if (dodnld && IS_23XX(isp)) {
865 		const uint16_t *ptr = isp->isp_mdvec->dv_ispfw;
866 		uint16_t wi, wl, segno;
867 		uint32_t la;
868 
869 		la = code_org;
870 		segno = 0;
871 
872 		for (;;) {
873 			uint32_t nxtaddr;
874 
875 			isp_prt(isp, ISP_LOGDEBUG0, "load 0x%x words of code at load address 0x%x", ptr[3], la);
876 
877 			wi = 0;
878 			wl = ptr[3];
879 
880 			while (wi < ptr[3]) {
881 				uint16_t *cp;
882 				uint16_t nw;
883 
884 				nw = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp)) >> 1;
885 				if (nw > wl) {
886 					nw = wl;
887 				}
888 				if (nw > (1 << 15)) {
889 					nw = 1 << 15;
890 				}
891 				cp = isp->isp_rquest;
892 				for (i = 0; i < nw; i++) {
893 					ISP_IOXPUT_16(isp,  ptr[wi++], &cp[i]);
894 					wl--;
895 				}
896 				MEMORYBARRIER(isp, SYNC_REQUEST, 0, ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp)), -1);
897 				MBSINIT(&mbs, 0, MBLOGALL, 0);
898 				if (la < 0x10000) {
899 					mbs.param[0] = MBOX_LOAD_RISC_RAM_2100;
900 					mbs.param[1] = la;
901 					mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
902 					mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
903 					mbs.param[4] = nw;
904 					mbs.param[6] = DMA_WD3(isp->isp_rquest_dma);
905 					mbs.param[7] = DMA_WD2(isp->isp_rquest_dma);
906 					isp_prt(isp, ISP_LOGDEBUG1, "LOAD RISC RAM 2100 %u words at load address 0x%x\n", nw, la);
907 				} else {
908 					mbs.param[0] = MBOX_LOAD_RISC_RAM;
909 					mbs.param[1] = la;
910 					mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
911 					mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
912 					mbs.param[4] = nw;
913 					mbs.param[6] = DMA_WD3(isp->isp_rquest_dma);
914 					mbs.param[7] = DMA_WD2(isp->isp_rquest_dma);
915 					mbs.param[8] = la >> 16;
916 					isp_prt(isp, ISP_LOGDEBUG1, "LOAD RISC RAM %u words at load address 0x%x\n", nw, la);
917 				}
918 				isp_mboxcmd(isp, &mbs);
919 				if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
920 					isp_prt(isp, ISP_LOGERR, "F/W Risc Ram Load Failed");
921 					ISP_RESET0(isp);
922 					return;
923 				}
924 				la += nw;
925 			}
926 
927 			if (!IS_2322(isp)) {
928 				break;
929 			}
930 
931 			if (++segno == 3) {
932 				break;
933 			}
934 
935 			/*
936 			 * If we're a 2322, the firmware actually comes in
937 			 * three chunks. We loaded the first at the code_org
938 			 * address. The other two chunks, which follow right
939 			 * after each other in memory here, get loaded at
940 			 * addresses specfied at offset 0x9..0xB.
941 			 */
942 
943 			nxtaddr = ptr[3];
944 			ptr = &ptr[nxtaddr];
945 			la = ptr[5] | ((ptr[4] & 0x3f) << 16);
946 		}
947 		isp->isp_loaded_fw = 1;
948 	} else if (dodnld) {
949 		union {
950 			const uint16_t *cp;
951 			uint16_t *np;
952 		} ucd;
953 		ucd.cp = isp->isp_mdvec->dv_ispfw;
954 		isp->isp_mbxworkp = &ucd.np[1];
955 		isp->isp_mbxwrk0 = ucd.np[3] - 1;
956 		isp->isp_mbxwrk1 = code_org + 1;
957 		MBSINIT(&mbs, MBOX_WRITE_RAM_WORD, MBLOGNONE, 0);
958 		mbs.param[1] = code_org;
959 		mbs.param[2] = ucd.np[0];
960 		isp_prt(isp, ISP_LOGDEBUG1, "WRITE RAM %u words at load address 0x%x", ucd.np[3], code_org);
961 		isp_mboxcmd(isp, &mbs);
962 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
963 			isp_prt(isp, ISP_LOGERR, "F/W download failed at word %d", isp->isp_mbxwrk1 - code_org);
964 			ISP_RESET0(isp);
965 			return;
966 		}
967 	} else if (IS_26XX(isp)) {
968 		MBSINIT(&mbs, MBOX_LOAD_FLASH_FIRMWARE, MBLOGALL, 5000000);
969 		mbs.ibitm = 0x01;
970 		mbs.obitm = 0x07;
971 		isp_mboxcmd(isp, &mbs);
972 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
973 			isp_prt(isp, ISP_LOGERR, "Flash F/W load failed");
974 			ISP_RESET0(isp);
975 			return;
976 		}
977 	} else {
978 		isp_prt(isp, ISP_LOGDEBUG2, "skipping f/w download");
979 	}
980 
981 	/*
982 	 * If we loaded firmware, verify its checksum
983 	 */
984 	if (isp->isp_loaded_fw) {
985 		MBSINIT(&mbs, MBOX_VERIFY_CHECKSUM, MBLOGNONE, 0);
986 		if (IS_24XX(isp)) {
987 			mbs.param[1] = code_org >> 16;
988 			mbs.param[2] = code_org;
989 		} else {
990 			mbs.param[1] = code_org;
991 		}
992 		isp_mboxcmd(isp, &mbs);
993 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
994 			isp_prt(isp, ISP_LOGERR, dcrc);
995 			ISP_RESET0(isp);
996 			return;
997 		}
998 	}
999 
1000 	/*
1001 	 * Now start it rolling.
1002 	 *
1003 	 * If we didn't actually download f/w,
1004 	 * we still need to (re)start it.
1005 	 */
1006 
1007 
1008 	MBSINIT(&mbs, MBOX_EXEC_FIRMWARE, MBLOGALL, 5000000);
1009 	if (IS_24XX(isp)) {
1010 		mbs.param[1] = code_org >> 16;
1011 		mbs.param[2] = code_org;
1012 		if (isp->isp_loaded_fw) {
1013 			mbs.param[3] = 0;
1014 		} else {
1015 			mbs.param[3] = 1;
1016 		}
1017 	} else if (IS_2322(isp)) {
1018 		mbs.param[1] = code_org;
1019 		if (isp->isp_loaded_fw) {
1020 			mbs.param[2] = 0;
1021 		} else {
1022 			mbs.param[2] = 1;
1023 		}
1024 	} else {
1025 		mbs.param[1] = code_org;
1026 	}
1027 	isp_mboxcmd(isp, &mbs);
1028 	if (IS_2322(isp) || IS_24XX(isp)) {
1029 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1030 			ISP_RESET0(isp);
1031 			return;
1032 		}
1033 	}
1034 
1035 	if (IS_SCSI(isp)) {
1036 		/*
1037 		 * Set CLOCK RATE, but only if asked to.
1038 		 */
1039 		if (isp->isp_clock) {
1040 			MBSINIT(&mbs, MBOX_SET_CLOCK_RATE, MBLOGALL, 0);
1041 			mbs.param[1] = isp->isp_clock;
1042 			isp_mboxcmd(isp, &mbs);
1043 			/* we will try not to care if this fails */
1044 		}
1045 	}
1046 
1047 	/*
1048 	 * Ask the chip for the current firmware version.
1049 	 * This should prove that the new firmware is working.
1050 	 */
1051 	MBSINIT(&mbs, MBOX_ABOUT_FIRMWARE, MBLOGALL, 0);
1052 	isp_mboxcmd(isp, &mbs);
1053 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1054 		ISP_RESET0(isp);
1055 		return;
1056 	}
1057 
1058 	/*
1059 	 * The SBus firmware that we are using apparently does not return
1060 	 * major, minor, micro revisions in the mailbox registers, which
1061 	 * is really, really, annoying.
1062 	 */
1063 	if (ISP_SBUS_SUPPORTED && isp->isp_bustype == ISP_BT_SBUS) {
1064 		if (dodnld) {
1065 #ifdef	ISP_TARGET_MODE
1066 			isp->isp_fwrev[0] = 7;
1067 			isp->isp_fwrev[1] = 55;
1068 #else
1069 			isp->isp_fwrev[0] = 1;
1070 			isp->isp_fwrev[1] = 37;
1071 #endif
1072 			isp->isp_fwrev[2] = 0;
1073 		}
1074 	} else {
1075 		isp->isp_fwrev[0] = mbs.param[1];
1076 		isp->isp_fwrev[1] = mbs.param[2];
1077 		isp->isp_fwrev[2] = mbs.param[3];
1078 	}
1079 
1080 	if (IS_FC(isp)) {
1081 		/*
1082 		 * We do not believe firmware attributes for 2100 code less
1083 		 * than 1.17.0, unless it's the firmware we specifically
1084 		 * are loading.
1085 		 *
1086 		 * Note that all 22XX and later f/w is greater than 1.X.0.
1087 		 */
1088 		if ((ISP_FW_OLDER_THAN(isp, 1, 17, 1))) {
1089 #ifdef	USE_SMALLER_2100_FIRMWARE
1090 			isp->isp_fwattr = ISP_FW_ATTR_SCCLUN;
1091 #else
1092 			isp->isp_fwattr = 0;
1093 #endif
1094 		} else {
1095 			isp->isp_fwattr = mbs.param[6];
1096 		}
1097 		if (IS_24XX(isp)) {
1098 			isp->isp_fwattr |= ((uint64_t) mbs.param[15]) << 16;
1099 			if (isp->isp_fwattr & ISP2400_FW_ATTR_EXTNDED) {
1100 				isp->isp_fwattr |=
1101 				    (((uint64_t) mbs.param[16]) << 32) |
1102 				    (((uint64_t) mbs.param[17]) << 48);
1103 			}
1104 		}
1105 	} else {
1106 		isp->isp_fwattr = 0;
1107 	}
1108 
1109 	isp_prt(isp, ISP_LOGCONFIG, "Board Type %s, Chip Revision 0x%x, %s F/W Revision %d.%d.%d",
1110 	    btype, isp->isp_revision, dodnld? "loaded" : "resident", isp->isp_fwrev[0], isp->isp_fwrev[1], isp->isp_fwrev[2]);
1111 
1112 	fwt = isp->isp_fwattr;
1113 	if (IS_24XX(isp)) {
1114 		buf = FCPARAM(isp, 0)->isp_scratch;
1115 		ISP_SNPRINTF(buf, ISP_FC_SCRLEN, "Attributes:");
1116 		if (fwt & ISP2400_FW_ATTR_CLASS2) {
1117 			fwt ^=ISP2400_FW_ATTR_CLASS2;
1118 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s Class2", buf);
1119 		}
1120 		if (fwt & ISP2400_FW_ATTR_IP) {
1121 			fwt ^=ISP2400_FW_ATTR_IP;
1122 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s IP", buf);
1123 		}
1124 		if (fwt & ISP2400_FW_ATTR_MULTIID) {
1125 			fwt ^=ISP2400_FW_ATTR_MULTIID;
1126 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s MultiID", buf);
1127 		}
1128 		if (fwt & ISP2400_FW_ATTR_SB2) {
1129 			fwt ^=ISP2400_FW_ATTR_SB2;
1130 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s SB2", buf);
1131 		}
1132 		if (fwt & ISP2400_FW_ATTR_T10CRC) {
1133 			fwt ^=ISP2400_FW_ATTR_T10CRC;
1134 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s T10CRC", buf);
1135 		}
1136 		if (fwt & ISP2400_FW_ATTR_VI) {
1137 			fwt ^=ISP2400_FW_ATTR_VI;
1138 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s VI", buf);
1139 		}
1140 		if (fwt & ISP2400_FW_ATTR_MQ) {
1141 			fwt ^=ISP2400_FW_ATTR_MQ;
1142 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s MQ", buf);
1143 		}
1144 		if (fwt & ISP2400_FW_ATTR_MSIX) {
1145 			fwt ^=ISP2400_FW_ATTR_MSIX;
1146 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s MSIX", buf);
1147 		}
1148 		if (fwt & ISP2400_FW_ATTR_FCOE) {
1149 			fwt ^=ISP2400_FW_ATTR_FCOE;
1150 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s FCOE", buf);
1151 		}
1152 		if (fwt & ISP2400_FW_ATTR_VP0) {
1153 			fwt ^= ISP2400_FW_ATTR_VP0;
1154 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s VP0_Decoupling", buf);
1155 		}
1156 		if (fwt & ISP2400_FW_ATTR_EXPFW) {
1157 			fwt ^= ISP2400_FW_ATTR_EXPFW;
1158 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s (Experimental)", buf);
1159 		}
1160 		if (fwt & ISP2400_FW_ATTR_HOTFW) {
1161 			fwt ^= ISP2400_FW_ATTR_HOTFW;
1162 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s HotFW", buf);
1163 		}
1164 		fwt &= ~ISP2400_FW_ATTR_EXTNDED;
1165 		if (fwt & ISP2400_FW_ATTR_EXTVP) {
1166 			fwt ^= ISP2400_FW_ATTR_EXTVP;
1167 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s ExtVP", buf);
1168 		}
1169 		if (fwt & ISP2400_FW_ATTR_VN2VN) {
1170 			fwt ^= ISP2400_FW_ATTR_VN2VN;
1171 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s VN2VN", buf);
1172 		}
1173 		if (fwt & ISP2400_FW_ATTR_EXMOFF) {
1174 			fwt ^= ISP2400_FW_ATTR_EXMOFF;
1175 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s EXMOFF", buf);
1176 		}
1177 		if (fwt & ISP2400_FW_ATTR_NPMOFF) {
1178 			fwt ^= ISP2400_FW_ATTR_NPMOFF;
1179 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s NPMOFF", buf);
1180 		}
1181 		if (fwt & ISP2400_FW_ATTR_DIFCHOP) {
1182 			fwt ^= ISP2400_FW_ATTR_DIFCHOP;
1183 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s DIFCHOP", buf);
1184 		}
1185 		if (fwt & ISP2400_FW_ATTR_SRIOV) {
1186 			fwt ^= ISP2400_FW_ATTR_SRIOV;
1187 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s SRIOV", buf);
1188 		}
1189 		if (fwt & ISP2400_FW_ATTR_ASICTMP) {
1190 			fwt ^= ISP2400_FW_ATTR_ASICTMP;
1191 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s ASICTMP", buf);
1192 		}
1193 		if (fwt & ISP2400_FW_ATTR_ATIOMQ) {
1194 			fwt ^= ISP2400_FW_ATTR_ATIOMQ;
1195 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s ATIOMQ", buf);
1196 		}
1197 		if (fwt) {
1198 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s (unknown 0x%08x%08x)", buf,
1199 			    (uint32_t) (fwt >> 32), (uint32_t) fwt);
1200 		}
1201 		isp_prt(isp, ISP_LOGCONFIG, "%s", buf);
1202 	} else if (IS_FC(isp)) {
1203 		buf = FCPARAM(isp, 0)->isp_scratch;
1204 		ISP_SNPRINTF(buf, ISP_FC_SCRLEN, "Attributes:");
1205 		if (fwt & ISP_FW_ATTR_TMODE) {
1206 			fwt ^=ISP_FW_ATTR_TMODE;
1207 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s TargetMode", buf);
1208 		}
1209 		if (fwt & ISP_FW_ATTR_SCCLUN) {
1210 			fwt ^=ISP_FW_ATTR_SCCLUN;
1211 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s SCC-Lun", buf);
1212 		}
1213 		if (fwt & ISP_FW_ATTR_FABRIC) {
1214 			fwt ^=ISP_FW_ATTR_FABRIC;
1215 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s Fabric", buf);
1216 		}
1217 		if (fwt & ISP_FW_ATTR_CLASS2) {
1218 			fwt ^=ISP_FW_ATTR_CLASS2;
1219 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s Class2", buf);
1220 		}
1221 		if (fwt & ISP_FW_ATTR_FCTAPE) {
1222 			fwt ^=ISP_FW_ATTR_FCTAPE;
1223 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s FC-Tape", buf);
1224 		}
1225 		if (fwt & ISP_FW_ATTR_IP) {
1226 			fwt ^=ISP_FW_ATTR_IP;
1227 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s IP", buf);
1228 		}
1229 		if (fwt & ISP_FW_ATTR_VI) {
1230 			fwt ^=ISP_FW_ATTR_VI;
1231 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s VI", buf);
1232 		}
1233 		if (fwt & ISP_FW_ATTR_VI_SOLARIS) {
1234 			fwt ^=ISP_FW_ATTR_VI_SOLARIS;
1235 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s VI_SOLARIS", buf);
1236 		}
1237 		if (fwt & ISP_FW_ATTR_2KLOGINS) {
1238 			fwt ^=ISP_FW_ATTR_2KLOGINS;
1239 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s 2K-Login", buf);
1240 		}
1241 		if (fwt != 0) {
1242 			ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s (unknown 0x%08x%08x)", buf,
1243 			    (uint32_t) (fwt >> 32), (uint32_t) fwt);
1244 		}
1245 		isp_prt(isp, ISP_LOGCONFIG, "%s", buf);
1246 	}
1247 
1248 	if (IS_24XX(isp)) {
1249 		MBSINIT(&mbs, MBOX_GET_RESOURCE_COUNT, MBLOGALL, 0);
1250 		isp_mboxcmd(isp, &mbs);
1251 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1252 			ISP_RESET0(isp);
1253 			return;
1254 		}
1255 		if (isp->isp_maxcmds >= mbs.param[3]) {
1256 			isp->isp_maxcmds = mbs.param[3];
1257 		}
1258 	} else {
1259 		MBSINIT(&mbs, MBOX_GET_FIRMWARE_STATUS, MBLOGALL, 0);
1260 		isp_mboxcmd(isp, &mbs);
1261 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1262 			ISP_RESET0(isp);
1263 			return;
1264 		}
1265 		if (isp->isp_maxcmds >= mbs.param[2]) {
1266 			isp->isp_maxcmds = mbs.param[2];
1267 		}
1268 	}
1269 	isp_prt(isp, ISP_LOGCONFIG, "%d max I/O command limit set", isp->isp_maxcmds);
1270 
1271 	/*
1272 	 * If we don't have Multi-ID f/w loaded, we need to restrict channels to one.
1273 	 * Only make this check for non-SCSI cards (I'm not sure firmware attributes
1274 	 * work for them).
1275 	 */
1276 	if (IS_FC(isp) && isp->isp_nchan > 1) {
1277 		if (!ISP_CAP_MULTI_ID(isp)) {
1278 			isp_prt(isp, ISP_LOGWARN, "non-MULTIID f/w loaded, "
1279 			    "only can enable 1 of %d channels", isp->isp_nchan);
1280 			isp->isp_nchan = 1;
1281 		} else if (!ISP_CAP_VP0(isp)) {
1282 			isp_prt(isp, ISP_LOGWARN, "We can not use MULTIID "
1283 			    "feature properly without VP0_Decoupling");
1284 			isp->isp_nchan = 1;
1285 		}
1286 	}
1287 	if (IS_FC(isp)) {
1288 		for (i = 0; i < isp->isp_nchan; i++)
1289 			isp_change_fw_state(isp, i, FW_CONFIG_WAIT);
1290 	}
1291 	if (isp->isp_dead) {
1292 		isp_shutdown(isp);
1293 		ISP_DISABLE_INTS(isp);
1294 		return;
1295 	}
1296 
1297 	isp->isp_state = ISP_RESETSTATE;
1298 
1299 	/*
1300 	 * Okay- now that we have new firmware running, we now (re)set our
1301 	 * notion of how many luns we support. This is somewhat tricky because
1302 	 * if we haven't loaded firmware, we sometimes do not have an easy way
1303 	 * of knowing how many luns we support.
1304 	 *
1305 	 * Expanded lun firmware gives you 32 luns for SCSI cards and
1306 	 * 16384 luns for Fibre Channel cards.
1307 	 *
1308 	 * It turns out that even for QLogic 2100s with ROM 1.10 and above
1309 	 * we do get a firmware attributes word returned in mailbox register 6.
1310 	 *
1311 	 * Because the lun is in a different position in the Request Queue
1312 	 * Entry structure for Fibre Channel with expanded lun firmware, we
1313 	 * can only support one lun (lun zero) when we don't know what kind
1314 	 * of firmware we're running.
1315 	 */
1316 	if (IS_SCSI(isp)) {
1317 		if (dodnld) {
1318 			if (IS_ULTRA2(isp) || IS_ULTRA3(isp)) {
1319 				isp->isp_maxluns = 32;
1320 			} else {
1321 				isp->isp_maxluns = 8;
1322 			}
1323 		} else {
1324 			isp->isp_maxluns = 8;
1325 		}
1326 	} else {
1327 		if (ISP_CAP_SCCFW(isp)) {
1328 			isp->isp_maxluns = 0;	/* No limit -- 2/8 bytes */
1329 		} else {
1330 			isp->isp_maxluns = 16;
1331 		}
1332 	}
1333 
1334 	/*
1335 	 * We get some default values established. As a side
1336 	 * effect, NVRAM is read here (unless overriden by
1337 	 * a configuration flag).
1338 	 */
1339 	if (do_load_defaults) {
1340 		if (IS_SCSI(isp)) {
1341 			isp_setdfltsdparm(isp);
1342 		} else {
1343 			for (i = 0; i < isp->isp_nchan; i++) {
1344 				isp_setdfltfcparm(isp, i);
1345 			}
1346 		}
1347 	}
1348 }
1349 
1350 /*
1351  * Clean firmware shutdown.
1352  */
1353 static int
1354 isp_deinit(ispsoftc_t *isp)
1355 {
1356 	mbreg_t mbs;
1357 
1358 	isp->isp_state = ISP_NILSTATE;
1359 	MBSINIT(&mbs, MBOX_STOP_FIRMWARE, MBLOGALL, 500000);
1360 	mbs.param[1] = 0;
1361 	mbs.param[2] = 0;
1362 	mbs.param[3] = 0;
1363 	mbs.param[4] = 0;
1364 	mbs.param[5] = 0;
1365 	mbs.param[6] = 0;
1366 	mbs.param[7] = 0;
1367 	mbs.param[8] = 0;
1368 	isp_mboxcmd(isp, &mbs);
1369 	return (mbs.param[0] == MBOX_COMMAND_COMPLETE ? 0 : mbs.param[0]);
1370 }
1371 
1372 /*
1373  * Initialize Parameters of Hardware to a known state.
1374  *
1375  * Locks are held before coming here.
1376  */
1377 void
1378 isp_init(ispsoftc_t *isp)
1379 {
1380 	if (IS_FC(isp)) {
1381 		if (IS_24XX(isp)) {
1382 			isp_fibre_init_2400(isp);
1383 		} else {
1384 			isp_fibre_init(isp);
1385 		}
1386 	} else {
1387 		isp_scsi_init(isp);
1388 	}
1389 	GET_NANOTIME(&isp->isp_init_time);
1390 }
1391 
1392 static void
1393 isp_scsi_init(ispsoftc_t *isp)
1394 {
1395 	sdparam *sdp_chan0, *sdp_chan1;
1396 	mbreg_t mbs;
1397 
1398 	isp->isp_state = ISP_INITSTATE;
1399 
1400 	sdp_chan0 = SDPARAM(isp, 0);
1401 	sdp_chan1 = sdp_chan0;
1402 	if (IS_DUALBUS(isp)) {
1403 		sdp_chan1 = SDPARAM(isp, 1);
1404 	}
1405 
1406 	/* First do overall per-card settings. */
1407 
1408 	/*
1409 	 * If we have fast memory timing enabled, turn it on.
1410 	 */
1411 	if (sdp_chan0->isp_fast_mttr) {
1412 		ISP_WRITE(isp, RISC_MTR, 0x1313);
1413 	}
1414 
1415 	/*
1416 	 * Set Retry Delay and Count.
1417 	 * You set both channels at the same time.
1418 	 */
1419 	MBSINIT(&mbs, MBOX_SET_RETRY_COUNT, MBLOGALL, 0);
1420 	mbs.param[1] = sdp_chan0->isp_retry_count;
1421 	mbs.param[2] = sdp_chan0->isp_retry_delay;
1422 	mbs.param[6] = sdp_chan1->isp_retry_count;
1423 	mbs.param[7] = sdp_chan1->isp_retry_delay;
1424 	isp_mboxcmd(isp, &mbs);
1425 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1426 		return;
1427 	}
1428 
1429 	/*
1430 	 * Set ASYNC DATA SETUP time. This is very important.
1431 	 */
1432 	MBSINIT(&mbs, MBOX_SET_ASYNC_DATA_SETUP_TIME, MBLOGALL, 0);
1433 	mbs.param[1] = sdp_chan0->isp_async_data_setup;
1434 	mbs.param[2] = sdp_chan1->isp_async_data_setup;
1435 	isp_mboxcmd(isp, &mbs);
1436 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1437 		return;
1438 	}
1439 
1440 	/*
1441 	 * Set ACTIVE Negation State.
1442 	 */
1443 	MBSINIT(&mbs, MBOX_SET_ACT_NEG_STATE, MBLOGNONE, 0);
1444 	mbs.param[1] =
1445 	    (sdp_chan0->isp_req_ack_active_neg << 4) |
1446 	    (sdp_chan0->isp_data_line_active_neg << 5);
1447 	mbs.param[2] =
1448 	    (sdp_chan1->isp_req_ack_active_neg << 4) |
1449 	    (sdp_chan1->isp_data_line_active_neg << 5);
1450 	isp_mboxcmd(isp, &mbs);
1451 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1452 		isp_prt(isp, ISP_LOGERR,
1453 		    "failed to set active negation state (%d,%d), (%d,%d)",
1454 		    sdp_chan0->isp_req_ack_active_neg,
1455 		    sdp_chan0->isp_data_line_active_neg,
1456 		    sdp_chan1->isp_req_ack_active_neg,
1457 		    sdp_chan1->isp_data_line_active_neg);
1458 		/*
1459 		 * But don't return.
1460 		 */
1461 	}
1462 
1463 	/*
1464 	 * Set the Tag Aging limit
1465 	 */
1466 	MBSINIT(&mbs, MBOX_SET_TAG_AGE_LIMIT, MBLOGALL, 0);
1467 	mbs.param[1] = sdp_chan0->isp_tag_aging;
1468 	mbs.param[2] = sdp_chan1->isp_tag_aging;
1469 	isp_mboxcmd(isp, &mbs);
1470 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1471 		isp_prt(isp, ISP_LOGERR, "failed to set tag age limit (%d,%d)",
1472 		    sdp_chan0->isp_tag_aging, sdp_chan1->isp_tag_aging);
1473 		return;
1474 	}
1475 
1476 	/*
1477 	 * Set selection timeout.
1478 	 */
1479 	MBSINIT(&mbs, MBOX_SET_SELECT_TIMEOUT, MBLOGALL, 0);
1480 	mbs.param[1] = sdp_chan0->isp_selection_timeout;
1481 	mbs.param[2] = sdp_chan1->isp_selection_timeout;
1482 	isp_mboxcmd(isp, &mbs);
1483 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1484 		return;
1485 	}
1486 
1487 	/* now do per-channel settings */
1488 	isp_scsi_channel_init(isp, 0);
1489 	if (IS_DUALBUS(isp))
1490 		isp_scsi_channel_init(isp, 1);
1491 
1492 	/*
1493 	 * Now enable request/response queues
1494 	 */
1495 
1496 	if (IS_ULTRA2(isp) || IS_1240(isp)) {
1497 		MBSINIT(&mbs, MBOX_INIT_RES_QUEUE_A64, MBLOGALL, 0);
1498 		mbs.param[1] = RESULT_QUEUE_LEN(isp);
1499 		mbs.param[2] = DMA_WD1(isp->isp_result_dma);
1500 		mbs.param[3] = DMA_WD0(isp->isp_result_dma);
1501 		mbs.param[4] = 0;
1502 		mbs.param[6] = DMA_WD3(isp->isp_result_dma);
1503 		mbs.param[7] = DMA_WD2(isp->isp_result_dma);
1504 		isp_mboxcmd(isp, &mbs);
1505 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1506 			return;
1507 		}
1508 		isp->isp_residx = isp->isp_resodx = mbs.param[5];
1509 
1510 		MBSINIT(&mbs, MBOX_INIT_REQ_QUEUE_A64, MBLOGALL, 0);
1511 		mbs.param[1] = RQUEST_QUEUE_LEN(isp);
1512 		mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
1513 		mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
1514 		mbs.param[5] = 0;
1515 		mbs.param[6] = DMA_WD3(isp->isp_result_dma);
1516 		mbs.param[7] = DMA_WD2(isp->isp_result_dma);
1517 		isp_mboxcmd(isp, &mbs);
1518 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1519 			return;
1520 		}
1521 		isp->isp_reqidx = isp->isp_reqodx = mbs.param[4];
1522 	} else {
1523 		MBSINIT(&mbs, MBOX_INIT_RES_QUEUE, MBLOGALL, 0);
1524 		mbs.param[1] = RESULT_QUEUE_LEN(isp);
1525 		mbs.param[2] = DMA_WD1(isp->isp_result_dma);
1526 		mbs.param[3] = DMA_WD0(isp->isp_result_dma);
1527 		mbs.param[4] = 0;
1528 		isp_mboxcmd(isp, &mbs);
1529 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1530 			return;
1531 		}
1532 		isp->isp_residx = isp->isp_resodx = mbs.param[5];
1533 
1534 		MBSINIT(&mbs, MBOX_INIT_REQ_QUEUE, MBLOGALL, 0);
1535 		mbs.param[1] = RQUEST_QUEUE_LEN(isp);
1536 		mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
1537 		mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
1538 		mbs.param[5] = 0;
1539 		isp_mboxcmd(isp, &mbs);
1540 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1541 			return;
1542 		}
1543 		isp->isp_reqidx = isp->isp_reqodx = mbs.param[4];
1544 	}
1545 
1546 	/*
1547 	 * Turn on LVD transitions for ULTRA2 or better and other features
1548 	 *
1549 	 * Now that we have 32 bit handles, don't do any fast posting
1550 	 * any more. For Ultra2/Ultra3 cards, we can turn on 32 bit RIO
1551 	 * operation or use fast posting. To be conservative, we'll only
1552 	 * do this for Ultra3 cards now because the other cards are so
1553 	 * rare for this author to find and test with.
1554 	 */
1555 
1556 	MBSINIT(&mbs, MBOX_SET_FW_FEATURES, MBLOGALL, 0);
1557 	if (IS_ULTRA2(isp))
1558 		mbs.param[1] |= FW_FEATURE_LVD_NOTIFY;
1559 #ifdef	ISP_NO_RIO
1560 	if (IS_ULTRA3(isp))
1561 		mbs.param[1] |= FW_FEATURE_FAST_POST;
1562 #else
1563 	if (IS_ULTRA3(isp))
1564 		mbs.param[1] |= FW_FEATURE_RIO_32BIT;
1565 #endif
1566 	if (mbs.param[1] != 0) {
1567 		uint16_t sfeat = mbs.param[1];
1568 		isp_mboxcmd(isp, &mbs);
1569 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1570 			isp_prt(isp, ISP_LOGINFO,
1571 			    "Enabled FW features (0x%x)", sfeat);
1572 		}
1573 	}
1574 
1575 	isp->isp_state = ISP_RUNSTATE;
1576 }
1577 
1578 static void
1579 isp_scsi_channel_init(ispsoftc_t *isp, int chan)
1580 {
1581 	sdparam *sdp;
1582 	mbreg_t mbs;
1583 	int tgt;
1584 
1585 	sdp = SDPARAM(isp, chan);
1586 
1587 	/*
1588 	 * Set (possibly new) Initiator ID.
1589 	 */
1590 	MBSINIT(&mbs, MBOX_SET_INIT_SCSI_ID, MBLOGALL, 0);
1591 	mbs.param[1] = (chan << 7) | sdp->isp_initiator_id;
1592 	isp_mboxcmd(isp, &mbs);
1593 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1594 		return;
1595 	}
1596 	isp_prt(isp, ISP_LOGINFO, "Chan %d Initiator ID is %d",
1597 	    chan, sdp->isp_initiator_id);
1598 
1599 
1600 	/*
1601 	 * Set current per-target parameters to an initial safe minimum.
1602 	 */
1603 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
1604 		int lun;
1605 		uint16_t sdf;
1606 
1607 		if (sdp->isp_devparam[tgt].dev_enable == 0) {
1608 			continue;
1609 		}
1610 #ifndef	ISP_TARGET_MODE
1611 		sdf = sdp->isp_devparam[tgt].goal_flags;
1612 		sdf &= DPARM_SAFE_DFLT;
1613 		/*
1614 		 * It is not quite clear when this changed over so that
1615 		 * we could force narrow and async for 1000/1020 cards,
1616 		 * but assume that this is only the case for loaded
1617 		 * firmware.
1618 		 */
1619 		if (isp->isp_loaded_fw) {
1620 			sdf |= DPARM_NARROW | DPARM_ASYNC;
1621 		}
1622 #else
1623 		/*
1624 		 * The !$*!)$!$)* f/w uses the same index into some
1625 		 * internal table to decide how to respond to negotiations,
1626 		 * so if we've said "let's be safe" for ID X, and ID X
1627 		 * selects *us*, the negotiations will back to 'safe'
1628 		 * (as in narrow/async). What the f/w *should* do is
1629 		 * use the initiator id settings to decide how to respond.
1630 		 */
1631 		sdp->isp_devparam[tgt].goal_flags = sdf = DPARM_DEFAULT;
1632 #endif
1633 		MBSINIT(&mbs, MBOX_SET_TARGET_PARAMS, MBLOGNONE, 0);
1634 		mbs.param[1] = (chan << 15) | (tgt << 8);
1635 		mbs.param[2] = sdf;
1636 		if ((sdf & DPARM_SYNC) == 0) {
1637 			mbs.param[3] = 0;
1638 		} else {
1639 			mbs.param[3] =
1640 			    (sdp->isp_devparam[tgt].goal_offset << 8) |
1641 			    (sdp->isp_devparam[tgt].goal_period);
1642 		}
1643 		isp_prt(isp, ISP_LOGDEBUG0, "Initial Settings bus%d tgt%d flags 0x%x off 0x%x per 0x%x",
1644 		    chan, tgt, mbs.param[2], mbs.param[3] >> 8, mbs.param[3] & 0xff);
1645 		isp_mboxcmd(isp, &mbs);
1646 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1647 			sdf = DPARM_SAFE_DFLT;
1648 			MBSINIT(&mbs, MBOX_SET_TARGET_PARAMS, MBLOGALL, 0);
1649 			mbs.param[1] = (tgt << 8) | (chan << 15);
1650 			mbs.param[2] = sdf;
1651 			mbs.param[3] = 0;
1652 			isp_mboxcmd(isp, &mbs);
1653 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1654 				continue;
1655 			}
1656 		}
1657 
1658 		/*
1659 		 * We don't update any information directly from the f/w
1660 		 * because we need to run at least one command to cause a
1661 		 * new state to be latched up. So, we just assume that we
1662 		 * converge to the values we just had set.
1663 		 *
1664 		 * Ensure that we don't believe tagged queuing is enabled yet.
1665 		 * It turns out that sometimes the ISP just ignores our
1666 		 * attempts to set parameters for devices that it hasn't
1667 		 * seen yet.
1668 		 */
1669 		sdp->isp_devparam[tgt].actv_flags = sdf & ~DPARM_TQING;
1670 		for (lun = 0; lun < (int) isp->isp_maxluns; lun++) {
1671 			MBSINIT(&mbs, MBOX_SET_DEV_QUEUE_PARAMS, MBLOGALL, 0);
1672 			mbs.param[1] = (chan << 15) | (tgt << 8) | lun;
1673 			mbs.param[2] = sdp->isp_max_queue_depth;
1674 			mbs.param[3] = sdp->isp_devparam[tgt].exc_throttle;
1675 			isp_mboxcmd(isp, &mbs);
1676 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1677 				break;
1678 			}
1679 		}
1680 	}
1681 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
1682 		if (sdp->isp_devparam[tgt].dev_refresh) {
1683 			sdp->sendmarker = 1;
1684 			sdp->update = 1;
1685 			break;
1686 		}
1687 	}
1688 }
1689 
1690 /*
1691  * Fibre Channel specific initialization.
1692  */
1693 static void
1694 isp_fibre_init(ispsoftc_t *isp)
1695 {
1696 	fcparam *fcp;
1697 	isp_icb_t local, *icbp = &local;
1698 	mbreg_t mbs;
1699 
1700 	/*
1701 	 * We only support one channel on non-24XX cards
1702 	 */
1703 	fcp = FCPARAM(isp, 0);
1704 	if (fcp->role == ISP_ROLE_NONE)
1705 		return;
1706 
1707 	isp->isp_state = ISP_INITSTATE;
1708 	ISP_MEMZERO(icbp, sizeof (*icbp));
1709 	icbp->icb_version = ICB_VERSION1;
1710 	icbp->icb_fwoptions = fcp->isp_fwoptions;
1711 
1712 	/*
1713 	 * Firmware Options are either retrieved from NVRAM or
1714 	 * are patched elsewhere. We check them for sanity here
1715 	 * and make changes based on board revision, but otherwise
1716 	 * let others decide policy.
1717 	 */
1718 
1719 	/*
1720 	 * If this is a 2100 < revision 5, we have to turn off FAIRNESS.
1721 	 */
1722 	if (IS_2100(isp) && isp->isp_revision < 5) {
1723 		icbp->icb_fwoptions &= ~ICBOPT_FAIRNESS;
1724 	}
1725 
1726 	/*
1727 	 * We have to use FULL LOGIN even though it resets the loop too much
1728 	 * because otherwise port database entries don't get updated after
1729 	 * a LIP- this is a known f/w bug for 2100 f/w less than 1.17.0.
1730 	 */
1731 	if (!ISP_FW_NEWER_THAN(isp, 1, 17, 0)) {
1732 		icbp->icb_fwoptions |= ICBOPT_FULL_LOGIN;
1733 	}
1734 
1735 	/*
1736 	 * Insist on Port Database Update Async notifications
1737 	 */
1738 	icbp->icb_fwoptions |= ICBOPT_PDBCHANGE_AE;
1739 
1740 	/*
1741 	 * Make sure that target role reflects into fwoptions.
1742 	 */
1743 	if (fcp->role & ISP_ROLE_TARGET) {
1744 		icbp->icb_fwoptions |= ICBOPT_TGT_ENABLE;
1745 	} else {
1746 		icbp->icb_fwoptions &= ~ICBOPT_TGT_ENABLE;
1747 	}
1748 
1749 	/*
1750 	 * For some reason my 2200 does not generate ATIOs in target mode
1751 	 * if initiator is disabled.  Extra logins are better then target
1752 	 * not working at all.
1753 	 */
1754 	if ((fcp->role & ISP_ROLE_INITIATOR) || IS_2100(isp) || IS_2200(isp)) {
1755 		icbp->icb_fwoptions &= ~ICBOPT_INI_DISABLE;
1756 	} else {
1757 		icbp->icb_fwoptions |= ICBOPT_INI_DISABLE;
1758 	}
1759 
1760 	icbp->icb_maxfrmlen = DEFAULT_FRAMESIZE(isp);
1761 	if (icbp->icb_maxfrmlen < ICB_MIN_FRMLEN || icbp->icb_maxfrmlen > ICB_MAX_FRMLEN) {
1762 		isp_prt(isp, ISP_LOGERR, "bad frame length (%d) from NVRAM- using %d", DEFAULT_FRAMESIZE(isp), ICB_DFLT_FRMLEN);
1763 		icbp->icb_maxfrmlen = ICB_DFLT_FRMLEN;
1764 	}
1765 	icbp->icb_maxalloc = fcp->isp_maxalloc;
1766 	if (icbp->icb_maxalloc < 1) {
1767 		isp_prt(isp, ISP_LOGERR, "bad maximum allocation (%d)- using 16", fcp->isp_maxalloc);
1768 		icbp->icb_maxalloc = 16;
1769 	}
1770 	icbp->icb_execthrottle = DEFAULT_EXEC_THROTTLE(isp);
1771 	if (icbp->icb_execthrottle < 1) {
1772 		isp_prt(isp, ISP_LOGERR, "bad execution throttle of %d- using %d", DEFAULT_EXEC_THROTTLE(isp), ICB_DFLT_THROTTLE);
1773 		icbp->icb_execthrottle = ICB_DFLT_THROTTLE;
1774 	}
1775 	icbp->icb_retry_delay = fcp->isp_retry_delay;
1776 	icbp->icb_retry_count = fcp->isp_retry_count;
1777 	if (fcp->isp_loopid < LOCAL_LOOP_LIM) {
1778 		icbp->icb_hardaddr = fcp->isp_loopid;
1779 		if (isp->isp_confopts & ISP_CFG_OWNLOOPID)
1780 			icbp->icb_fwoptions |= ICBOPT_HARD_ADDRESS;
1781 		else
1782 			icbp->icb_fwoptions |= ICBOPT_PREV_ADDRESS;
1783 	}
1784 
1785 	/*
1786 	 * Right now we just set extended options to prefer point-to-point
1787 	 * over loop based upon some soft config options.
1788 	 *
1789 	 * NB: for the 2300, ICBOPT_EXTENDED is required.
1790 	 */
1791 	if (IS_2100(isp)) {
1792 		/*
1793 		 * We can't have Fast Posting any more- we now
1794 		 * have 32 bit handles.
1795 		 */
1796 		icbp->icb_fwoptions &= ~ICBOPT_FAST_POST;
1797 	} else if (IS_2200(isp) || IS_23XX(isp)) {
1798 		icbp->icb_fwoptions |= ICBOPT_EXTENDED;
1799 
1800 		icbp->icb_xfwoptions = fcp->isp_xfwoptions;
1801 
1802 		if (ISP_CAP_FCTAPE(isp)) {
1803 			if (isp->isp_confopts & ISP_CFG_NOFCTAPE)
1804 				icbp->icb_xfwoptions &= ~ICBXOPT_FCTAPE;
1805 
1806 			if (isp->isp_confopts & ISP_CFG_FCTAPE)
1807 				icbp->icb_xfwoptions |= ICBXOPT_FCTAPE;
1808 
1809 			if (icbp->icb_xfwoptions & ICBXOPT_FCTAPE) {
1810 				icbp->icb_fwoptions &= ~ICBOPT_FULL_LOGIN;	/* per documents */
1811 				icbp->icb_xfwoptions |= ICBXOPT_FCTAPE_CCQ|ICBXOPT_FCTAPE_CONFIRM;
1812 				FCPARAM(isp, 0)->fctape_enabled = 1;
1813 			} else {
1814 				FCPARAM(isp, 0)->fctape_enabled = 0;
1815 			}
1816 		} else {
1817 			icbp->icb_xfwoptions &= ~ICBXOPT_FCTAPE;
1818 			FCPARAM(isp, 0)->fctape_enabled = 0;
1819 		}
1820 
1821 		/*
1822 		 * Prefer or force Point-To-Point instead Loop?
1823 		 */
1824 		switch (isp->isp_confopts & ISP_CFG_PORT_PREF) {
1825 		case ISP_CFG_NPORT:
1826 			icbp->icb_xfwoptions &= ~ICBXOPT_TOPO_MASK;
1827 			icbp->icb_xfwoptions |= ICBXOPT_PTP_2_LOOP;
1828 			break;
1829 		case ISP_CFG_NPORT_ONLY:
1830 			icbp->icb_xfwoptions &= ~ICBXOPT_TOPO_MASK;
1831 			icbp->icb_xfwoptions |= ICBXOPT_PTP_ONLY;
1832 			break;
1833 		case ISP_CFG_LPORT_ONLY:
1834 			icbp->icb_xfwoptions &= ~ICBXOPT_TOPO_MASK;
1835 			icbp->icb_xfwoptions |= ICBXOPT_LOOP_ONLY;
1836 			break;
1837 		default:
1838 			/*
1839 			 * Let NVRAM settings define it if they are sane
1840 			 */
1841 			switch (icbp->icb_xfwoptions & ICBXOPT_TOPO_MASK) {
1842 			case ICBXOPT_PTP_2_LOOP:
1843 			case ICBXOPT_PTP_ONLY:
1844 			case ICBXOPT_LOOP_ONLY:
1845 			case ICBXOPT_LOOP_2_PTP:
1846 				break;
1847 			default:
1848 				icbp->icb_xfwoptions &= ~ICBXOPT_TOPO_MASK;
1849 				icbp->icb_xfwoptions |= ICBXOPT_LOOP_2_PTP;
1850 			}
1851 			break;
1852 		}
1853 		if (IS_2200(isp)) {
1854 			/*
1855 			 * We can't have Fast Posting any more- we now
1856 			 * have 32 bit handles.
1857 			 *
1858 			 * RIO seemed to have to much breakage.
1859 			 *
1860 			 * Just opt for safety.
1861 			 */
1862 			icbp->icb_xfwoptions &= ~ICBXOPT_RIO_16BIT;
1863 			icbp->icb_fwoptions &= ~ICBOPT_FAST_POST;
1864 		} else {
1865 			/*
1866 			 * QLogic recommends that FAST Posting be turned
1867 			 * off for 23XX cards and instead allow the HBA
1868 			 * to write response queue entries and interrupt
1869 			 * after a delay (ZIO).
1870 			 */
1871 			icbp->icb_fwoptions &= ~ICBOPT_FAST_POST;
1872 			if ((fcp->isp_xfwoptions & ICBXOPT_TIMER_MASK) == ICBXOPT_ZIO) {
1873 				icbp->icb_xfwoptions |= ICBXOPT_ZIO;
1874 				icbp->icb_idelaytimer = 10;
1875 			}
1876 			icbp->icb_zfwoptions = fcp->isp_zfwoptions;
1877 			if (isp->isp_confopts & ISP_CFG_1GB) {
1878 				icbp->icb_zfwoptions &= ~ICBZOPT_RATE_MASK;
1879 				icbp->icb_zfwoptions |= ICBZOPT_RATE_1GB;
1880 			} else if (isp->isp_confopts & ISP_CFG_2GB) {
1881 				icbp->icb_zfwoptions &= ~ICBZOPT_RATE_MASK;
1882 				icbp->icb_zfwoptions |= ICBZOPT_RATE_2GB;
1883 			} else {
1884 				switch (icbp->icb_zfwoptions & ICBZOPT_RATE_MASK) {
1885 				case ICBZOPT_RATE_1GB:
1886 				case ICBZOPT_RATE_2GB:
1887 				case ICBZOPT_RATE_AUTO:
1888 					break;
1889 				default:
1890 					icbp->icb_zfwoptions &= ~ICBZOPT_RATE_MASK;
1891 					icbp->icb_zfwoptions |= ICBZOPT_RATE_AUTO;
1892 					break;
1893 				}
1894 			}
1895 		}
1896 	}
1897 
1898 
1899 	/*
1900 	 * For 22XX > 2.1.26 && 23XX, set some options.
1901 	 */
1902 	if (ISP_FW_NEWER_THAN(isp, 2, 26, 0)) {
1903 		MBSINIT(&mbs, MBOX_SET_FIRMWARE_OPTIONS, MBLOGALL, 0);
1904 		mbs.param[1] = IFCOPT1_DISF7SWTCH|IFCOPT1_LIPASYNC|IFCOPT1_LIPF8;
1905 		mbs.param[2] = 0;
1906 		mbs.param[3] = 0;
1907 		if (ISP_FW_NEWER_THAN(isp, 3, 16, 0)) {
1908 			mbs.param[1] |= IFCOPT1_EQFQASYNC|IFCOPT1_CTIO_RETRY;
1909 			if (fcp->role & ISP_ROLE_TARGET) {
1910 				if (ISP_FW_NEWER_THAN(isp, 3, 25, 0)) {
1911 					mbs.param[1] |= IFCOPT1_ENAPURE;
1912 				}
1913 				mbs.param[3] = IFCOPT3_NOPRLI;
1914 			}
1915 		}
1916 		isp_mboxcmd(isp, &mbs);
1917 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1918 			return;
1919 		}
1920 	}
1921 	icbp->icb_logintime = ICB_LOGIN_TOV;
1922 
1923 #ifdef	ISP_TARGET_MODE
1924 	if (icbp->icb_fwoptions & ICBOPT_TGT_ENABLE) {
1925 		icbp->icb_lunenables = 0xffff;
1926 		icbp->icb_ccnt = 0xff;
1927 		icbp->icb_icnt = 0xff;
1928 		icbp->icb_lunetimeout = ICB_LUN_ENABLE_TOV;
1929 	}
1930 #endif
1931 	if (fcp->isp_wwnn && fcp->isp_wwpn) {
1932 		icbp->icb_fwoptions |= ICBOPT_BOTH_WWNS;
1933 		MAKE_NODE_NAME_FROM_WWN(icbp->icb_nodename, fcp->isp_wwnn);
1934 		MAKE_NODE_NAME_FROM_WWN(icbp->icb_portname, fcp->isp_wwpn);
1935 		isp_prt(isp, ISP_LOGDEBUG1,
1936 		    "Setting ICB Node 0x%08x%08x Port 0x%08x%08x",
1937 		    ((uint32_t) (fcp->isp_wwnn >> 32)),
1938 		    ((uint32_t) (fcp->isp_wwnn)),
1939 		    ((uint32_t) (fcp->isp_wwpn >> 32)),
1940 		    ((uint32_t) (fcp->isp_wwpn)));
1941 	} else if (fcp->isp_wwpn) {
1942 		icbp->icb_fwoptions &= ~ICBOPT_BOTH_WWNS;
1943 		MAKE_NODE_NAME_FROM_WWN(icbp->icb_portname, fcp->isp_wwpn);
1944 		isp_prt(isp, ISP_LOGDEBUG1,
1945 		    "Setting ICB Port 0x%08x%08x",
1946 		    ((uint32_t) (fcp->isp_wwpn >> 32)),
1947 		    ((uint32_t) (fcp->isp_wwpn)));
1948 	} else {
1949 		isp_prt(isp, ISP_LOGERR, "No valid WWNs to use");
1950 		return;
1951 	}
1952 	icbp->icb_rqstqlen = RQUEST_QUEUE_LEN(isp);
1953 	if (icbp->icb_rqstqlen < 1) {
1954 		isp_prt(isp, ISP_LOGERR, "bad request queue length");
1955 	}
1956 	icbp->icb_rsltqlen = RESULT_QUEUE_LEN(isp);
1957 	if (icbp->icb_rsltqlen < 1) {
1958 		isp_prt(isp, ISP_LOGERR, "bad result queue length");
1959 	}
1960 	icbp->icb_rqstaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_rquest_dma);
1961 	icbp->icb_rqstaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_rquest_dma);
1962 	icbp->icb_rqstaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_rquest_dma);
1963 	icbp->icb_rqstaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_rquest_dma);
1964 	icbp->icb_respaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_result_dma);
1965 	icbp->icb_respaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_result_dma);
1966 	icbp->icb_respaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_result_dma);
1967 	icbp->icb_respaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_result_dma);
1968 
1969 	if (FC_SCRATCH_ACQUIRE(isp, 0)) {
1970 		isp_prt(isp, ISP_LOGERR, sacq);
1971 		return;
1972 	}
1973 	isp_prt(isp, ISP_LOGDEBUG0, "isp_fibre_init: fwopt 0x%x xfwopt 0x%x zfwopt 0x%x",
1974 	    icbp->icb_fwoptions, icbp->icb_xfwoptions, icbp->icb_zfwoptions);
1975 
1976 	isp_put_icb(isp, icbp, (isp_icb_t *)fcp->isp_scratch);
1977 	if (isp->isp_dblev & ISP_LOGDEBUG1) {
1978 		isp_print_bytes(isp, "isp_fibre_init",
1979 		    sizeof(*icbp), fcp->isp_scratch);
1980 	}
1981 
1982 	/*
1983 	 * Init the firmware
1984 	 */
1985 	MBSINIT(&mbs, MBOX_INIT_FIRMWARE, MBLOGALL, 30000000);
1986 	mbs.param[1] = 0;
1987 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
1988 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
1989 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
1990 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
1991 	isp_prt(isp, ISP_LOGDEBUG0, "INIT F/W from %p (%08x%08x)",
1992 	    fcp->isp_scratch, (uint32_t) ((uint64_t)fcp->isp_scdma >> 32),
1993 	    (uint32_t) fcp->isp_scdma);
1994 	MEMORYBARRIER(isp, SYNC_SFORDEV, 0, sizeof (*icbp), 0);
1995 	isp_mboxcmd(isp, &mbs);
1996 	FC_SCRATCH_RELEASE(isp, 0);
1997 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE)
1998 		return;
1999 	isp->isp_reqidx = 0;
2000 	isp->isp_reqodx = 0;
2001 	isp->isp_residx = 0;
2002 	isp->isp_resodx = 0;
2003 
2004 	/*
2005 	 * Whatever happens, we're now committed to being here.
2006 	 */
2007 	isp->isp_state = ISP_RUNSTATE;
2008 }
2009 
2010 static void
2011 isp_fibre_init_2400(ispsoftc_t *isp)
2012 {
2013 	fcparam *fcp;
2014 	isp_icb_2400_t local, *icbp = &local;
2015 	mbreg_t mbs;
2016 	int chan;
2017 
2018 	/*
2019 	 * Check to see whether all channels have *some* kind of role
2020 	 */
2021 	for (chan = 0; chan < isp->isp_nchan; chan++) {
2022 		fcp = FCPARAM(isp, chan);
2023 		if (fcp->role != ISP_ROLE_NONE) {
2024 			break;
2025 		}
2026 	}
2027 	if (chan == isp->isp_nchan) {
2028 		isp_prt(isp, ISP_LOG_WARN1, "all %d channels with role 'none'", chan);
2029 		return;
2030 	}
2031 
2032 	isp->isp_state = ISP_INITSTATE;
2033 
2034 	/*
2035 	 * Start with channel 0.
2036 	 */
2037 	fcp = FCPARAM(isp, 0);
2038 
2039 	/*
2040 	 * Turn on LIP F8 async event (1)
2041 	 */
2042 	MBSINIT(&mbs, MBOX_SET_FIRMWARE_OPTIONS, MBLOGALL, 0);
2043 	mbs.param[1] = 1;
2044 	isp_mboxcmd(isp, &mbs);
2045 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2046 		return;
2047 	}
2048 
2049 	ISP_MEMZERO(icbp, sizeof (*icbp));
2050 	icbp->icb_fwoptions1 = fcp->isp_fwoptions;
2051 	icbp->icb_fwoptions2 = fcp->isp_xfwoptions;
2052 	icbp->icb_fwoptions3 = fcp->isp_zfwoptions;
2053 	if (isp->isp_nchan > 1 && ISP_CAP_VP0(isp)) {
2054 		icbp->icb_fwoptions1 &= ~ICB2400_OPT1_INI_DISABLE;
2055 		icbp->icb_fwoptions1 |= ICB2400_OPT1_TGT_ENABLE;
2056 	} else {
2057 		if (fcp->role & ISP_ROLE_TARGET)
2058 			icbp->icb_fwoptions1 |= ICB2400_OPT1_TGT_ENABLE;
2059 		else
2060 			icbp->icb_fwoptions1 &= ~ICB2400_OPT1_TGT_ENABLE;
2061 		if (fcp->role & ISP_ROLE_INITIATOR)
2062 			icbp->icb_fwoptions1 &= ~ICB2400_OPT1_INI_DISABLE;
2063 		else
2064 			icbp->icb_fwoptions1 |= ICB2400_OPT1_INI_DISABLE;
2065 	}
2066 
2067 	icbp->icb_version = ICB_VERSION1;
2068 	icbp->icb_maxfrmlen = DEFAULT_FRAMESIZE(isp);
2069 	if (icbp->icb_maxfrmlen < ICB_MIN_FRMLEN || icbp->icb_maxfrmlen > ICB_MAX_FRMLEN) {
2070 		isp_prt(isp, ISP_LOGERR, "bad frame length (%d) from NVRAM- using %d", DEFAULT_FRAMESIZE(isp), ICB_DFLT_FRMLEN);
2071 		icbp->icb_maxfrmlen = ICB_DFLT_FRMLEN;
2072 	}
2073 
2074 	icbp->icb_execthrottle = DEFAULT_EXEC_THROTTLE(isp);
2075 	if (icbp->icb_execthrottle < 1) {
2076 		isp_prt(isp, ISP_LOGERR, "bad execution throttle of %d- using %d", DEFAULT_EXEC_THROTTLE(isp), ICB_DFLT_THROTTLE);
2077 		icbp->icb_execthrottle = ICB_DFLT_THROTTLE;
2078 	}
2079 
2080 	/*
2081 	 * Set target exchange count. Take half if we are supporting both roles.
2082 	 */
2083 	if (icbp->icb_fwoptions1 & ICB2400_OPT1_TGT_ENABLE) {
2084 		icbp->icb_xchgcnt = isp->isp_maxcmds;
2085 		if ((icbp->icb_fwoptions1 & ICB2400_OPT1_INI_DISABLE) == 0)
2086 			icbp->icb_xchgcnt >>= 1;
2087 	}
2088 
2089 	if (fcp->isp_loopid < LOCAL_LOOP_LIM) {
2090 		icbp->icb_hardaddr = fcp->isp_loopid;
2091 		if (isp->isp_confopts & ISP_CFG_OWNLOOPID)
2092 			icbp->icb_fwoptions1 |= ICB2400_OPT1_HARD_ADDRESS;
2093 		else
2094 			icbp->icb_fwoptions1 |= ICB2400_OPT1_PREV_ADDRESS;
2095 	}
2096 
2097 	if (isp->isp_confopts & ISP_CFG_NOFCTAPE) {
2098 		icbp->icb_fwoptions2 &= ~ICB2400_OPT2_FCTAPE;
2099 	}
2100 	if (isp->isp_confopts & ISP_CFG_FCTAPE) {
2101 		icbp->icb_fwoptions2 |= ICB2400_OPT2_FCTAPE;
2102 	}
2103 
2104 	for (chan = 0; chan < isp->isp_nchan; chan++) {
2105 		if (icbp->icb_fwoptions2 & ICB2400_OPT2_FCTAPE)
2106 			FCPARAM(isp, chan)->fctape_enabled = 1;
2107 		else
2108 			FCPARAM(isp, chan)->fctape_enabled = 0;
2109 	}
2110 
2111 	switch (isp->isp_confopts & ISP_CFG_PORT_PREF) {
2112 	case ISP_CFG_NPORT_ONLY:
2113 		icbp->icb_fwoptions2 &= ~ICB2400_OPT2_TOPO_MASK;
2114 		icbp->icb_fwoptions2 |= ICB2400_OPT2_PTP_ONLY;
2115 		break;
2116 	case ISP_CFG_LPORT_ONLY:
2117 		icbp->icb_fwoptions2 &= ~ICB2400_OPT2_TOPO_MASK;
2118 		icbp->icb_fwoptions2 |= ICB2400_OPT2_LOOP_ONLY;
2119 		break;
2120 	default:
2121 		/* ISP_CFG_PTP_2_LOOP not available in 24XX/25XX */
2122 		icbp->icb_fwoptions2 &= ~ICB2400_OPT2_TOPO_MASK;
2123 		icbp->icb_fwoptions2 |= ICB2400_OPT2_LOOP_2_PTP;
2124 		break;
2125 	}
2126 
2127 	switch (icbp->icb_fwoptions2 & ICB2400_OPT2_TIMER_MASK) {
2128 	case ICB2400_OPT2_ZIO:
2129 	case ICB2400_OPT2_ZIO1:
2130 		icbp->icb_idelaytimer = 0;
2131 		break;
2132 	case 0:
2133 		break;
2134 	default:
2135 		isp_prt(isp, ISP_LOGWARN, "bad value %x in fwopt2 timer field", icbp->icb_fwoptions2 & ICB2400_OPT2_TIMER_MASK);
2136 		icbp->icb_fwoptions2 &= ~ICB2400_OPT2_TIMER_MASK;
2137 		break;
2138 	}
2139 
2140 	if (IS_26XX(isp)) {
2141 		/* We don't support MSI-X yet, so set this unconditionally. */
2142 		icbp->icb_fwoptions2 |= ICB2400_OPT2_ENA_IHR;
2143 		icbp->icb_fwoptions2 |= ICB2400_OPT2_ENA_IHA;
2144 	}
2145 
2146 	if ((icbp->icb_fwoptions3 & ICB2400_OPT3_RSPSZ_MASK) == 0) {
2147 		icbp->icb_fwoptions3 |= ICB2400_OPT3_RSPSZ_24;
2148 	}
2149 	if (isp->isp_confopts & ISP_CFG_1GB) {
2150 		icbp->icb_fwoptions3 &= ~ICB2400_OPT3_RATE_MASK;
2151 		icbp->icb_fwoptions3 |= ICB2400_OPT3_RATE_1GB;
2152 	} else if (isp->isp_confopts & ISP_CFG_2GB) {
2153 		icbp->icb_fwoptions3 &= ~ICB2400_OPT3_RATE_MASK;
2154 		icbp->icb_fwoptions3 |= ICB2400_OPT3_RATE_2GB;
2155 	} else if (isp->isp_confopts & ISP_CFG_4GB) {
2156 		icbp->icb_fwoptions3 &= ~ICB2400_OPT3_RATE_MASK;
2157 		icbp->icb_fwoptions3 |= ICB2400_OPT3_RATE_4GB;
2158 	} else if (isp->isp_confopts & ISP_CFG_8GB) {
2159 		icbp->icb_fwoptions3 &= ~ICB2400_OPT3_RATE_MASK;
2160 		icbp->icb_fwoptions3 |= ICB2400_OPT3_RATE_8GB;
2161 	} else if (isp->isp_confopts & ISP_CFG_16GB) {
2162 		icbp->icb_fwoptions3 &= ~ICB2400_OPT3_RATE_MASK;
2163 		icbp->icb_fwoptions3 |= ICB2400_OPT3_RATE_16GB;
2164 	} else {
2165 		switch (icbp->icb_fwoptions3 & ICB2400_OPT3_RATE_MASK) {
2166 		case ICB2400_OPT3_RATE_4GB:
2167 		case ICB2400_OPT3_RATE_8GB:
2168 		case ICB2400_OPT3_RATE_16GB:
2169 		case ICB2400_OPT3_RATE_AUTO:
2170 			break;
2171 		case ICB2400_OPT3_RATE_2GB:
2172 			if (isp->isp_type <= ISP_HA_FC_2500)
2173 				break;
2174 			/*FALLTHROUGH*/
2175 		case ICB2400_OPT3_RATE_1GB:
2176 			if (isp->isp_type <= ISP_HA_FC_2400)
2177 				break;
2178 			/*FALLTHROUGH*/
2179 		default:
2180 			icbp->icb_fwoptions3 &= ~ICB2400_OPT3_RATE_MASK;
2181 			icbp->icb_fwoptions3 |= ICB2400_OPT3_RATE_AUTO;
2182 			break;
2183 		}
2184 	}
2185 	icbp->icb_logintime = ICB_LOGIN_TOV;
2186 
2187 	if (fcp->isp_wwnn && fcp->isp_wwpn) {
2188 		icbp->icb_fwoptions1 |= ICB2400_OPT1_BOTH_WWNS;
2189 		MAKE_NODE_NAME_FROM_WWN(icbp->icb_portname, fcp->isp_wwpn);
2190 		MAKE_NODE_NAME_FROM_WWN(icbp->icb_nodename, fcp->isp_wwnn);
2191 		isp_prt(isp, ISP_LOGDEBUG1, "Setting ICB Node 0x%08x%08x Port 0x%08x%08x", ((uint32_t) (fcp->isp_wwnn >> 32)), ((uint32_t) (fcp->isp_wwnn)),
2192 		    ((uint32_t) (fcp->isp_wwpn >> 32)), ((uint32_t) (fcp->isp_wwpn)));
2193 	} else if (fcp->isp_wwpn) {
2194 		icbp->icb_fwoptions1 &= ~ICB2400_OPT1_BOTH_WWNS;
2195 		MAKE_NODE_NAME_FROM_WWN(icbp->icb_portname, fcp->isp_wwpn);
2196 		isp_prt(isp, ISP_LOGDEBUG1, "Setting ICB Node to be same as Port 0x%08x%08x", ((uint32_t) (fcp->isp_wwpn >> 32)), ((uint32_t) (fcp->isp_wwpn)));
2197 	} else {
2198 		isp_prt(isp, ISP_LOGERR, "No valid WWNs to use");
2199 		return;
2200 	}
2201 	icbp->icb_retry_count = fcp->isp_retry_count;
2202 
2203 	icbp->icb_rqstqlen = RQUEST_QUEUE_LEN(isp);
2204 	if (icbp->icb_rqstqlen < 8) {
2205 		isp_prt(isp, ISP_LOGERR, "bad request queue length %d", icbp->icb_rqstqlen);
2206 		return;
2207 	}
2208 	icbp->icb_rsltqlen = RESULT_QUEUE_LEN(isp);
2209 	if (icbp->icb_rsltqlen < 8) {
2210 		isp_prt(isp, ISP_LOGERR, "bad result queue length %d",
2211 		    icbp->icb_rsltqlen);
2212 		return;
2213 	}
2214 	icbp->icb_rqstaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_rquest_dma);
2215 	icbp->icb_rqstaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_rquest_dma);
2216 	icbp->icb_rqstaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_rquest_dma);
2217 	icbp->icb_rqstaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_rquest_dma);
2218 
2219 	icbp->icb_respaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_result_dma);
2220 	icbp->icb_respaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_result_dma);
2221 	icbp->icb_respaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_result_dma);
2222 	icbp->icb_respaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_result_dma);
2223 
2224 #ifdef	ISP_TARGET_MODE
2225 	/* unconditionally set up the ATIO queue if we support target mode */
2226 	icbp->icb_atioqlen = RESULT_QUEUE_LEN(isp);
2227 	if (icbp->icb_atioqlen < 8) {
2228 		isp_prt(isp, ISP_LOGERR, "bad ATIO queue length %d", icbp->icb_atioqlen);
2229 		return;
2230 	}
2231 	icbp->icb_atioqaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_atioq_dma);
2232 	icbp->icb_atioqaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_atioq_dma);
2233 	icbp->icb_atioqaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_atioq_dma);
2234 	icbp->icb_atioqaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_atioq_dma);
2235 	isp_prt(isp, ISP_LOGDEBUG0, "isp_fibre_init_2400: atioq %04x%04x%04x%04x", DMA_WD3(isp->isp_atioq_dma), DMA_WD2(isp->isp_atioq_dma),
2236 	    DMA_WD1(isp->isp_atioq_dma), DMA_WD0(isp->isp_atioq_dma));
2237 #endif
2238 
2239 	isp_prt(isp, ISP_LOGDEBUG0, "isp_fibre_init_2400: fwopt1 0x%x fwopt2 0x%x fwopt3 0x%x", icbp->icb_fwoptions1, icbp->icb_fwoptions2, icbp->icb_fwoptions3);
2240 
2241 	isp_prt(isp, ISP_LOGDEBUG0, "isp_fibre_init_2400: rqst %04x%04x%04x%04x rsp %04x%04x%04x%04x", DMA_WD3(isp->isp_rquest_dma), DMA_WD2(isp->isp_rquest_dma),
2242 	    DMA_WD1(isp->isp_rquest_dma), DMA_WD0(isp->isp_rquest_dma), DMA_WD3(isp->isp_result_dma), DMA_WD2(isp->isp_result_dma),
2243 	    DMA_WD1(isp->isp_result_dma), DMA_WD0(isp->isp_result_dma));
2244 
2245 	if (FC_SCRATCH_ACQUIRE(isp, 0)) {
2246 		isp_prt(isp, ISP_LOGERR, sacq);
2247 		return;
2248 	}
2249 	ISP_MEMZERO(fcp->isp_scratch, ISP_FC_SCRLEN);
2250 	isp_put_icb_2400(isp, icbp, fcp->isp_scratch);
2251 	if (isp->isp_dblev & ISP_LOGDEBUG1) {
2252 		isp_print_bytes(isp, "isp_fibre_init_2400",
2253 		    sizeof (*icbp), fcp->isp_scratch);
2254 	}
2255 
2256 	/*
2257 	 * Now fill in information about any additional channels
2258 	 */
2259 	if (isp->isp_nchan > 1) {
2260 		isp_icb_2400_vpinfo_t vpinfo, *vdst;
2261 		vp_port_info_t pi, *pdst;
2262 		size_t amt = 0;
2263 		uint8_t *off;
2264 
2265 		vpinfo.vp_global_options = ICB2400_VPGOPT_GEN_RIDA;
2266 		if (ISP_CAP_VP0(isp)) {
2267 			vpinfo.vp_global_options |= ICB2400_VPGOPT_VP0_DECOUPLE;
2268 			vpinfo.vp_count = isp->isp_nchan;
2269 			chan = 0;
2270 		} else {
2271 			vpinfo.vp_count = isp->isp_nchan - 1;
2272 			chan = 1;
2273 		}
2274 		off = fcp->isp_scratch;
2275 		off += ICB2400_VPINFO_OFF;
2276 		vdst = (isp_icb_2400_vpinfo_t *) off;
2277 		isp_put_icb_2400_vpinfo(isp, &vpinfo, vdst);
2278 		amt = ICB2400_VPINFO_OFF + sizeof (isp_icb_2400_vpinfo_t);
2279 		for (; chan < isp->isp_nchan; chan++) {
2280 			fcparam *fcp2;
2281 
2282 			ISP_MEMZERO(&pi, sizeof (pi));
2283 			fcp2 = FCPARAM(isp, chan);
2284 			if (fcp2->role != ISP_ROLE_NONE) {
2285 				pi.vp_port_options = ICB2400_VPOPT_ENABLED |
2286 				    ICB2400_VPOPT_ENA_SNSLOGIN;
2287 				if (fcp2->role & ISP_ROLE_INITIATOR)
2288 					pi.vp_port_options |= ICB2400_VPOPT_INI_ENABLE;
2289 				if ((fcp2->role & ISP_ROLE_TARGET) == 0)
2290 					pi.vp_port_options |= ICB2400_VPOPT_TGT_DISABLE;
2291 			}
2292 			if (fcp2->isp_loopid < LOCAL_LOOP_LIM) {
2293 				pi.vp_port_loopid = fcp2->isp_loopid;
2294 				if (isp->isp_confopts & ISP_CFG_OWNLOOPID)
2295 					pi.vp_port_options |= ICB2400_VPOPT_HARD_ADDRESS;
2296 				else
2297 					pi.vp_port_options |= ICB2400_VPOPT_PREV_ADDRESS;
2298 			}
2299 			MAKE_NODE_NAME_FROM_WWN(pi.vp_port_portname, fcp2->isp_wwpn);
2300 			MAKE_NODE_NAME_FROM_WWN(pi.vp_port_nodename, fcp2->isp_wwnn);
2301 			off = fcp->isp_scratch;
2302 			if (ISP_CAP_VP0(isp))
2303 				off += ICB2400_VPINFO_PORT_OFF(chan);
2304 			else
2305 				off += ICB2400_VPINFO_PORT_OFF(chan - 1);
2306 			pdst = (vp_port_info_t *) off;
2307 			isp_put_vp_port_info(isp, &pi, pdst);
2308 			amt += ICB2400_VPOPT_WRITE_SIZE;
2309 		}
2310 		if (isp->isp_dblev & ISP_LOGDEBUG1) {
2311 			isp_print_bytes(isp, "isp_fibre_init_2400",
2312 			    amt - ICB2400_VPINFO_OFF,
2313 			    (char *)fcp->isp_scratch + ICB2400_VPINFO_OFF);
2314 		}
2315 	}
2316 
2317 	/*
2318 	 * Init the firmware
2319 	 */
2320 	MBSINIT(&mbs, 0, MBLOGALL, 30000000);
2321 	if (isp->isp_nchan > 1) {
2322 		mbs.param[0] = MBOX_INIT_FIRMWARE_MULTI_ID;
2323 	} else {
2324 		mbs.param[0] = MBOX_INIT_FIRMWARE;
2325 	}
2326 	mbs.param[1] = 0;
2327 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2328 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2329 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2330 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2331 	isp_prt(isp, ISP_LOGDEBUG0, "INIT F/W from %04x%04x%04x%04x", DMA_WD3(fcp->isp_scdma), DMA_WD2(fcp->isp_scdma), DMA_WD1(fcp->isp_scdma), DMA_WD0(fcp->isp_scdma));
2332 	MEMORYBARRIER(isp, SYNC_SFORDEV, 0, sizeof (*icbp), 0);
2333 	isp_mboxcmd(isp, &mbs);
2334 	FC_SCRATCH_RELEASE(isp, 0);
2335 
2336 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2337 		return;
2338 	}
2339 	isp->isp_reqidx = 0;
2340 	isp->isp_reqodx = 0;
2341 	isp->isp_residx = 0;
2342 	isp->isp_resodx = 0;
2343 	isp->isp_atioodx = 0;
2344 
2345 	/*
2346 	 * Whatever happens, we're now committed to being here.
2347 	 */
2348 	isp->isp_state = ISP_RUNSTATE;
2349 }
2350 
2351 static int
2352 isp_fc_enable_vp(ispsoftc_t *isp, int chan)
2353 {
2354 	fcparam *fcp = FCPARAM(isp, chan);
2355 	vp_modify_t vp;
2356 	void *reqp;
2357 	uint8_t resp[QENTRY_LEN];
2358 
2359 	/* Build a VP MODIFY command in memory */
2360 	ISP_MEMZERO(&vp, sizeof(vp));
2361 	vp.vp_mod_hdr.rqs_entry_type = RQSTYPE_VP_MODIFY;
2362 	vp.vp_mod_hdr.rqs_entry_count = 1;
2363 	vp.vp_mod_cnt = 1;
2364 	vp.vp_mod_idx0 = chan;
2365 	vp.vp_mod_cmd = VP_MODIFY_ENA;
2366 	vp.vp_mod_ports[0].options = ICB2400_VPOPT_ENABLED |
2367 	    ICB2400_VPOPT_ENA_SNSLOGIN;
2368 	if (fcp->role & ISP_ROLE_INITIATOR)
2369 		vp.vp_mod_ports[0].options |= ICB2400_VPOPT_INI_ENABLE;
2370 	if ((fcp->role & ISP_ROLE_TARGET) == 0)
2371 		vp.vp_mod_ports[0].options |= ICB2400_VPOPT_TGT_DISABLE;
2372 	if (fcp->isp_loopid < LOCAL_LOOP_LIM) {
2373 		vp.vp_mod_ports[0].loopid = fcp->isp_loopid;
2374 		if (isp->isp_confopts & ISP_CFG_OWNLOOPID)
2375 			vp.vp_mod_ports[0].options |= ICB2400_VPOPT_HARD_ADDRESS;
2376 		else
2377 			vp.vp_mod_ports[0].options |= ICB2400_VPOPT_PREV_ADDRESS;
2378 	}
2379 	MAKE_NODE_NAME_FROM_WWN(vp.vp_mod_ports[0].wwpn, fcp->isp_wwpn);
2380 	MAKE_NODE_NAME_FROM_WWN(vp.vp_mod_ports[0].wwnn, fcp->isp_wwnn);
2381 
2382 	/* Prepare space for response in memory */
2383 	memset(resp, 0xff, sizeof(resp));
2384 	vp.vp_mod_hdl = isp_allocate_handle(isp, resp, ISP_HANDLE_CTRL);
2385 	if (vp.vp_mod_hdl == 0) {
2386 		isp_prt(isp, ISP_LOGERR,
2387 		    "%s: VP_MODIFY of Chan %d out of handles", __func__, chan);
2388 		return (EIO);
2389 	}
2390 
2391 	/* Send request and wait for response. */
2392 	reqp = isp_getrqentry(isp);
2393 	if (reqp == NULL) {
2394 		isp_prt(isp, ISP_LOGERR,
2395 		    "%s: VP_MODIFY of Chan %d out of rqent", __func__, chan);
2396 		isp_destroy_handle(isp, vp.vp_mod_hdl);
2397 		return (EIO);
2398 	}
2399 	isp_put_vp_modify(isp, &vp, (vp_modify_t *)reqp);
2400 	if (isp->isp_dblev & ISP_LOGDEBUG1)
2401 		isp_print_bytes(isp, "IOCB VP_MODIFY", QENTRY_LEN, reqp);
2402 	ISP_SYNC_REQUEST(isp);
2403 	if (msleep(resp, &isp->isp_lock, 0, "VP_MODIFY", 5*hz) == EWOULDBLOCK) {
2404 		isp_prt(isp, ISP_LOGERR,
2405 		    "%s: VP_MODIFY of Chan %d timed out", __func__, chan);
2406 		isp_destroy_handle(isp, vp.vp_mod_hdl);
2407 		return (EIO);
2408 	}
2409 	if (isp->isp_dblev & ISP_LOGDEBUG1)
2410 		isp_print_bytes(isp, "IOCB VP_MODIFY response", QENTRY_LEN, resp);
2411 	isp_get_vp_modify(isp, (vp_modify_t *)resp, &vp);
2412 
2413 	if (vp.vp_mod_hdr.rqs_flags != 0 || vp.vp_mod_status != VP_STS_OK) {
2414 		isp_prt(isp, ISP_LOGERR,
2415 		    "%s: VP_MODIFY of Chan %d failed with flags %x status %d",
2416 		    __func__, chan, vp.vp_mod_hdr.rqs_flags, vp.vp_mod_status);
2417 		return (EIO);
2418 	}
2419 	return (0);
2420 }
2421 
2422 static int
2423 isp_fc_disable_vp(ispsoftc_t *isp, int chan)
2424 {
2425 	vp_ctrl_info_t vp;
2426 	void *reqp;
2427 	uint8_t resp[QENTRY_LEN];
2428 
2429 	/* Build a VP CTRL command in memory */
2430 	ISP_MEMZERO(&vp, sizeof(vp));
2431 	vp.vp_ctrl_hdr.rqs_entry_type = RQSTYPE_VP_CTRL;
2432 	vp.vp_ctrl_hdr.rqs_entry_count = 1;
2433 	if (ISP_CAP_VP0(isp)) {
2434 		vp.vp_ctrl_status = 1;
2435 	} else {
2436 		vp.vp_ctrl_status = 0;
2437 		chan--;	/* VP0 can not be controlled in this case. */
2438 	}
2439 	vp.vp_ctrl_command = VP_CTRL_CMD_DISABLE_VP_LOGO_ALL;
2440 	vp.vp_ctrl_vp_count = 1;
2441 	vp.vp_ctrl_idmap[chan / 16] |= (1 << chan % 16);
2442 
2443 	/* Prepare space for response in memory */
2444 	memset(resp, 0xff, sizeof(resp));
2445 	vp.vp_ctrl_handle = isp_allocate_handle(isp, resp, ISP_HANDLE_CTRL);
2446 	if (vp.vp_ctrl_handle == 0) {
2447 		isp_prt(isp, ISP_LOGERR,
2448 		    "%s: VP_CTRL of Chan %d out of handles", __func__, chan);
2449 		return (EIO);
2450 	}
2451 
2452 	/* Send request and wait for response. */
2453 	reqp = isp_getrqentry(isp);
2454 	if (reqp == NULL) {
2455 		isp_prt(isp, ISP_LOGERR,
2456 		    "%s: VP_CTRL of Chan %d out of rqent", __func__, chan);
2457 		isp_destroy_handle(isp, vp.vp_ctrl_handle);
2458 		return (EIO);
2459 	}
2460 	isp_put_vp_ctrl_info(isp, &vp, (vp_ctrl_info_t *)reqp);
2461 	if (isp->isp_dblev & ISP_LOGDEBUG1)
2462 		isp_print_bytes(isp, "IOCB VP_CTRL", QENTRY_LEN, reqp);
2463 	ISP_SYNC_REQUEST(isp);
2464 	if (msleep(resp, &isp->isp_lock, 0, "VP_CTRL", 5*hz) == EWOULDBLOCK) {
2465 		isp_prt(isp, ISP_LOGERR,
2466 		    "%s: VP_CTRL of Chan %d timed out", __func__, chan);
2467 		isp_destroy_handle(isp, vp.vp_ctrl_handle);
2468 		return (EIO);
2469 	}
2470 	if (isp->isp_dblev & ISP_LOGDEBUG1)
2471 		isp_print_bytes(isp, "IOCB VP_CTRL response", QENTRY_LEN, resp);
2472 	isp_get_vp_ctrl_info(isp, (vp_ctrl_info_t *)resp, &vp);
2473 
2474 	if (vp.vp_ctrl_hdr.rqs_flags != 0 || vp.vp_ctrl_status != 0) {
2475 		isp_prt(isp, ISP_LOGERR,
2476 		    "%s: VP_CTRL of Chan %d failed with flags %x status %d %d",
2477 		    __func__, chan, vp.vp_ctrl_hdr.rqs_flags,
2478 		    vp.vp_ctrl_status, vp.vp_ctrl_index_fail);
2479 		return (EIO);
2480 	}
2481 	return (0);
2482 }
2483 
2484 static int
2485 isp_fc_change_role(ispsoftc_t *isp, int chan, int new_role)
2486 {
2487 	fcparam *fcp = FCPARAM(isp, chan);
2488 	int i, was, res = 0;
2489 
2490 	if (chan >= isp->isp_nchan) {
2491 		isp_prt(isp, ISP_LOGWARN, "%s: bad channel %d", __func__, chan);
2492 		return (ENXIO);
2493 	}
2494 	if (fcp->role == new_role)
2495 		return (0);
2496 	for (was = 0, i = 0; i < isp->isp_nchan; i++) {
2497 		if (FCPARAM(isp, i)->role != ISP_ROLE_NONE)
2498 			was++;
2499 	}
2500 	if (was == 0 || (was == 1 && fcp->role != ISP_ROLE_NONE)) {
2501 		fcp->role = new_role;
2502 		return (isp_reinit(isp, 0));
2503 	}
2504 	if (fcp->role != ISP_ROLE_NONE) {
2505 		res = isp_fc_disable_vp(isp, chan);
2506 		isp_clear_portdb(isp, chan);
2507 	}
2508 	fcp->role = new_role;
2509 	if (fcp->role != ISP_ROLE_NONE)
2510 		res = isp_fc_enable_vp(isp, chan);
2511 	return (res);
2512 }
2513 
2514 static void
2515 isp_clear_portdb(ispsoftc_t *isp, int chan)
2516 {
2517 	fcparam *fcp = FCPARAM(isp, chan);
2518 	fcportdb_t *lp;
2519 	int i;
2520 
2521 	for (i = 0; i < MAX_FC_TARG; i++) {
2522 		lp = &fcp->portdb[i];
2523 		switch (lp->state) {
2524 		case FC_PORTDB_STATE_DEAD:
2525 		case FC_PORTDB_STATE_CHANGED:
2526 		case FC_PORTDB_STATE_VALID:
2527 			lp->state = FC_PORTDB_STATE_NIL;
2528 			isp_async(isp, ISPASYNC_DEV_GONE, chan, lp);
2529 			break;
2530 		case FC_PORTDB_STATE_NIL:
2531 		case FC_PORTDB_STATE_NEW:
2532 			lp->state = FC_PORTDB_STATE_NIL;
2533 			break;
2534 		case FC_PORTDB_STATE_ZOMBIE:
2535 			break;
2536 		default:
2537 			panic("Don't know how to clear state %d\n", lp->state);
2538 		}
2539 	}
2540 }
2541 
2542 static void
2543 isp_mark_portdb(ispsoftc_t *isp, int chan)
2544 {
2545 	fcparam *fcp = FCPARAM(isp, chan);
2546 	fcportdb_t *lp;
2547 	int i;
2548 
2549 	for (i = 0; i < MAX_FC_TARG; i++) {
2550 		lp = &fcp->portdb[i];
2551 		if (lp->state == FC_PORTDB_STATE_NIL)
2552 			continue;
2553 		if (lp->portid >= DOMAIN_CONTROLLER_BASE &&
2554 		    lp->portid <= DOMAIN_CONTROLLER_END)
2555 			continue;
2556 		fcp->portdb[i].probational = 1;
2557 	}
2558 }
2559 
2560 /*
2561  * Perform an IOCB PLOGI or LOGO via EXECUTE IOCB A64 for 24XX cards
2562  * or via FABRIC LOGIN/FABRIC LOGOUT for other cards.
2563  */
2564 static int
2565 isp_plogx(ispsoftc_t *isp, int chan, uint16_t handle, uint32_t portid, int flags)
2566 {
2567 	isp_plogx_t pl;
2568 	void *reqp;
2569 	uint8_t resp[QENTRY_LEN];
2570 	uint32_t sst, parm1;
2571 	int rval, lev;
2572 	const char *msg;
2573 	char buf[64];
2574 
2575 	isp_prt(isp, ISP_LOG_SANCFG, "Chan %d PLOGX %s PortID 0x%06x nphdl 0x%x",
2576 	    chan, (flags & PLOGX_FLG_CMD_MASK) == PLOGX_FLG_CMD_PLOGI ?
2577 	    "Login":"Logout", portid, handle);
2578 	if (!IS_24XX(isp)) {
2579 		int action = flags & PLOGX_FLG_CMD_MASK;
2580 		if (action == PLOGX_FLG_CMD_PLOGI) {
2581 			return (isp_port_login(isp, handle, portid));
2582 		} else if (action == PLOGX_FLG_CMD_LOGO) {
2583 			return (isp_port_logout(isp, handle, portid));
2584 		} else {
2585 			return (MBOX_INVALID_COMMAND);
2586 		}
2587 	}
2588 
2589 	ISP_MEMZERO(&pl, sizeof(pl));
2590 	pl.plogx_header.rqs_entry_count = 1;
2591 	pl.plogx_header.rqs_entry_type = RQSTYPE_LOGIN;
2592 	pl.plogx_nphdl = handle;
2593 	pl.plogx_vphdl = chan;
2594 	pl.plogx_portlo = portid;
2595 	pl.plogx_rspsz_porthi = (portid >> 16) & 0xff;
2596 	pl.plogx_flags = flags;
2597 
2598 	/* Prepare space for response in memory */
2599 	memset(resp, 0xff, sizeof(resp));
2600 	pl.plogx_handle = isp_allocate_handle(isp, resp, ISP_HANDLE_CTRL);
2601 	if (pl.plogx_handle == 0) {
2602 		isp_prt(isp, ISP_LOGERR,
2603 		    "%s: PLOGX of Chan %d out of handles", __func__, chan);
2604 		return (-1);
2605 	}
2606 
2607 	/* Send request and wait for response. */
2608 	reqp = isp_getrqentry(isp);
2609 	if (reqp == NULL) {
2610 		isp_prt(isp, ISP_LOGERR,
2611 		    "%s: PLOGX of Chan %d out of rqent", __func__, chan);
2612 		isp_destroy_handle(isp, pl.plogx_handle);
2613 		return (-1);
2614 	}
2615 	isp_put_plogx(isp, &pl, (isp_plogx_t *)reqp);
2616 	if (isp->isp_dblev & ISP_LOGDEBUG1)
2617 		isp_print_bytes(isp, "IOCB LOGX", QENTRY_LEN, reqp);
2618 	ISP_SYNC_REQUEST(isp);
2619 	if (msleep(resp, &isp->isp_lock, 0, "PLOGX", 3 * ICB_LOGIN_TOV * hz)
2620 	    == EWOULDBLOCK) {
2621 		isp_prt(isp, ISP_LOGERR,
2622 		    "%s: PLOGX of Chan %d timed out", __func__, chan);
2623 		isp_destroy_handle(isp, pl.plogx_handle);
2624 		return (-1);
2625 	}
2626 	if (isp->isp_dblev & ISP_LOGDEBUG1)
2627 		isp_print_bytes(isp, "IOCB LOGX response", QENTRY_LEN, resp);
2628 	isp_get_plogx(isp, (isp_plogx_t *)resp, &pl);
2629 
2630 	if (pl.plogx_status == PLOGX_STATUS_OK) {
2631 		return (0);
2632 	} else if (pl.plogx_status != PLOGX_STATUS_IOCBERR) {
2633 		isp_prt(isp, ISP_LOGWARN,
2634 		    "status 0x%x on port login IOCB channel %d",
2635 		    pl.plogx_status, chan);
2636 		return (-1);
2637 	}
2638 
2639 	sst = pl.plogx_ioparm[0].lo16 | (pl.plogx_ioparm[0].hi16 << 16);
2640 	parm1 = pl.plogx_ioparm[1].lo16 | (pl.plogx_ioparm[1].hi16 << 16);
2641 
2642 	rval = -1;
2643 	lev = ISP_LOGERR;
2644 	msg = NULL;
2645 
2646 	switch (sst) {
2647 	case PLOGX_IOCBERR_NOLINK:
2648 		msg = "no link";
2649 		break;
2650 	case PLOGX_IOCBERR_NOIOCB:
2651 		msg = "no IOCB buffer";
2652 		break;
2653 	case PLOGX_IOCBERR_NOXGHG:
2654 		msg = "no Exchange Control Block";
2655 		break;
2656 	case PLOGX_IOCBERR_FAILED:
2657 		ISP_SNPRINTF(buf, sizeof (buf), "reason 0x%x (last LOGIN state 0x%x)", parm1 & 0xff, (parm1 >> 8) & 0xff);
2658 		msg = buf;
2659 		break;
2660 	case PLOGX_IOCBERR_NOFABRIC:
2661 		msg = "no fabric";
2662 		break;
2663 	case PLOGX_IOCBERR_NOTREADY:
2664 		msg = "firmware not ready";
2665 		break;
2666 	case PLOGX_IOCBERR_NOLOGIN:
2667 		ISP_SNPRINTF(buf, sizeof (buf), "not logged in (last state 0x%x)", parm1);
2668 		msg = buf;
2669 		rval = MBOX_NOT_LOGGED_IN;
2670 		break;
2671 	case PLOGX_IOCBERR_REJECT:
2672 		ISP_SNPRINTF(buf, sizeof (buf), "LS_RJT = 0x%x", parm1);
2673 		msg = buf;
2674 		break;
2675 	case PLOGX_IOCBERR_NOPCB:
2676 		msg = "no PCB allocated";
2677 		break;
2678 	case PLOGX_IOCBERR_EINVAL:
2679 		ISP_SNPRINTF(buf, sizeof (buf), "invalid parameter at offset 0x%x", parm1);
2680 		msg = buf;
2681 		break;
2682 	case PLOGX_IOCBERR_PORTUSED:
2683 		lev = ISP_LOG_SANCFG|ISP_LOG_WARN1;
2684 		ISP_SNPRINTF(buf, sizeof (buf), "already logged in with N-Port handle 0x%x", parm1);
2685 		msg = buf;
2686 		rval = MBOX_PORT_ID_USED | (parm1 << 16);
2687 		break;
2688 	case PLOGX_IOCBERR_HNDLUSED:
2689 		lev = ISP_LOG_SANCFG|ISP_LOG_WARN1;
2690 		ISP_SNPRINTF(buf, sizeof (buf), "handle already used for PortID 0x%06x", parm1);
2691 		msg = buf;
2692 		rval = MBOX_LOOP_ID_USED;
2693 		break;
2694 	case PLOGX_IOCBERR_NOHANDLE:
2695 		msg = "no handle allocated";
2696 		break;
2697 	case PLOGX_IOCBERR_NOFLOGI:
2698 		msg = "no FLOGI_ACC";
2699 		break;
2700 	default:
2701 		ISP_SNPRINTF(buf, sizeof (buf), "status %x from %x", pl.plogx_status, flags);
2702 		msg = buf;
2703 		break;
2704 	}
2705 	if (msg) {
2706 		isp_prt(isp, ISP_LOGERR, "Chan %d PLOGX PortID 0x%06x to N-Port handle 0x%x: %s", chan, portid, handle, msg);
2707 	}
2708 	return (rval);
2709 }
2710 
2711 static int
2712 isp_port_login(ispsoftc_t *isp, uint16_t handle, uint32_t portid)
2713 {
2714 	mbreg_t mbs;
2715 
2716 	MBSINIT(&mbs, MBOX_FABRIC_LOGIN, MBLOGNONE, 500000);
2717 	if (ISP_CAP_2KLOGIN(isp)) {
2718 		mbs.param[1] = handle;
2719 		mbs.ibits = (1 << 10);
2720 	} else {
2721 		mbs.param[1] = handle << 8;
2722 	}
2723 	mbs.param[2] = portid >> 16;
2724 	mbs.param[3] = portid;
2725 	mbs.logval = MBLOGNONE;
2726 	mbs.timeout = 500000;
2727 	isp_mboxcmd(isp, &mbs);
2728 
2729 	switch (mbs.param[0]) {
2730 	case MBOX_PORT_ID_USED:
2731 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOG_WARN1, "isp_port_login: portid 0x%06x already logged in as 0x%x", portid, mbs.param[1]);
2732 		return (MBOX_PORT_ID_USED | (mbs.param[1] << 16));
2733 
2734 	case MBOX_LOOP_ID_USED:
2735 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOG_WARN1, "isp_port_login: handle 0x%x in use for port id 0x%02xXXXX", handle, mbs.param[1] & 0xff);
2736 		return (MBOX_LOOP_ID_USED);
2737 
2738 	case MBOX_COMMAND_COMPLETE:
2739 		return (0);
2740 
2741 	case MBOX_COMMAND_ERROR:
2742 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOG_WARN1, "isp_port_login: error 0x%x in PLOGI to port 0x%06x", mbs.param[1], portid);
2743 		return (MBOX_COMMAND_ERROR);
2744 
2745 	case MBOX_ALL_IDS_USED:
2746 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOG_WARN1, "isp_port_login: all IDs used for fabric login");
2747 		return (MBOX_ALL_IDS_USED);
2748 
2749 	default:
2750 		isp_prt(isp, ISP_LOG_SANCFG, "isp_port_login: error 0x%x on port login of 0x%06x@0x%0x", mbs.param[0], portid, handle);
2751 		return (mbs.param[0]);
2752 	}
2753 }
2754 
2755 /*
2756  * Pre-24XX fabric port logout
2757  *
2758  * Note that portid is not used
2759  */
2760 static int
2761 isp_port_logout(ispsoftc_t *isp, uint16_t handle, uint32_t portid)
2762 {
2763 	mbreg_t mbs;
2764 
2765 	MBSINIT(&mbs, MBOX_FABRIC_LOGOUT, MBLOGNONE, 500000);
2766 	if (ISP_CAP_2KLOGIN(isp)) {
2767 		mbs.param[1] = handle;
2768 		mbs.ibits = (1 << 10);
2769 	} else {
2770 		mbs.param[1] = handle << 8;
2771 	}
2772 	isp_mboxcmd(isp, &mbs);
2773 	return (mbs.param[0] == MBOX_COMMAND_COMPLETE? 0 : mbs.param[0]);
2774 }
2775 
2776 static int
2777 isp_getpdb(ispsoftc_t *isp, int chan, uint16_t id, isp_pdb_t *pdb)
2778 {
2779 	mbreg_t mbs;
2780 	union {
2781 		isp_pdb_21xx_t fred;
2782 		isp_pdb_24xx_t bill;
2783 	} un;
2784 
2785 	MBSINIT(&mbs, MBOX_GET_PORT_DB,
2786 	    MBLOGALL & ~MBLOGMASK(MBOX_COMMAND_PARAM_ERROR), 250000);
2787 	if (IS_24XX(isp)) {
2788 		mbs.ibits = (1 << 9)|(1 << 10);
2789 		mbs.param[1] = id;
2790 		mbs.param[9] = chan;
2791 	} else if (ISP_CAP_2KLOGIN(isp)) {
2792 		mbs.param[1] = id;
2793 	} else {
2794 		mbs.param[1] = id << 8;
2795 	}
2796 	mbs.param[2] = DMA_WD1(isp->isp_iocb_dma);
2797 	mbs.param[3] = DMA_WD0(isp->isp_iocb_dma);
2798 	mbs.param[6] = DMA_WD3(isp->isp_iocb_dma);
2799 	mbs.param[7] = DMA_WD2(isp->isp_iocb_dma);
2800 	MEMORYBARRIER(isp, SYNC_IFORDEV, 0, sizeof(un), chan);
2801 
2802 	isp_mboxcmd(isp, &mbs);
2803 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE)
2804 		return (mbs.param[0] | (mbs.param[1] << 16));
2805 
2806 	MEMORYBARRIER(isp, SYNC_IFORCPU, 0, sizeof(un), chan);
2807 	if (IS_24XX(isp)) {
2808 		isp_get_pdb_24xx(isp, isp->isp_iocb, &un.bill);
2809 		pdb->handle = un.bill.pdb_handle;
2810 		pdb->prli_word3 = un.bill.pdb_prli_svc3;
2811 		pdb->portid = BITS2WORD_24XX(un.bill.pdb_portid_bits);
2812 		ISP_MEMCPY(pdb->portname, un.bill.pdb_portname, 8);
2813 		ISP_MEMCPY(pdb->nodename, un.bill.pdb_nodename, 8);
2814 		isp_prt(isp, ISP_LOGDEBUG1,
2815 		    "Chan %d handle 0x%x Port 0x%06x flags 0x%x curstate %x",
2816 		    chan, id, pdb->portid, un.bill.pdb_flags,
2817 		    un.bill.pdb_curstate);
2818 		if (un.bill.pdb_curstate < PDB2400_STATE_PLOGI_DONE || un.bill.pdb_curstate > PDB2400_STATE_LOGGED_IN) {
2819 			mbs.param[0] = MBOX_NOT_LOGGED_IN;
2820 			return (mbs.param[0]);
2821 		}
2822 	} else {
2823 		isp_get_pdb_21xx(isp, isp->isp_iocb, &un.fred);
2824 		pdb->handle = un.fred.pdb_loopid;
2825 		pdb->prli_word3 = un.fred.pdb_prli_svc3;
2826 		pdb->portid = BITS2WORD(un.fred.pdb_portid_bits);
2827 		ISP_MEMCPY(pdb->portname, un.fred.pdb_portname, 8);
2828 		ISP_MEMCPY(pdb->nodename, un.fred.pdb_nodename, 8);
2829 		isp_prt(isp, ISP_LOGDEBUG1,
2830 		    "Chan %d handle 0x%x Port 0x%06x", chan, id, pdb->portid);
2831 	}
2832 	return (0);
2833 }
2834 
2835 static int
2836 isp_gethandles(ispsoftc_t *isp, int chan, uint16_t *handles, int *num, int loop)
2837 {
2838 	fcparam *fcp = FCPARAM(isp, chan);
2839 	mbreg_t mbs;
2840 	isp_pnhle_21xx_t el1, *elp1;
2841 	isp_pnhle_23xx_t el3, *elp3;
2842 	isp_pnhle_24xx_t el4, *elp4;
2843 	int i, j;
2844 	uint32_t p;
2845 	uint16_t h;
2846 
2847 	MBSINIT(&mbs, MBOX_GET_ID_LIST, MBLOGALL, 250000);
2848 	if (IS_24XX(isp)) {
2849 		mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2850 		mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2851 		mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2852 		mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2853 		mbs.param[8] = ISP_FC_SCRLEN;
2854 		mbs.param[9] = chan;
2855 	} else {
2856 		mbs.ibits = (1 << 1)|(1 << 2)|(1 << 3)|(1 << 6);
2857 		mbs.param[1] = DMA_WD1(fcp->isp_scdma);
2858 		mbs.param[2] = DMA_WD0(fcp->isp_scdma);
2859 		mbs.param[3] = DMA_WD3(fcp->isp_scdma);
2860 		mbs.param[6] = DMA_WD2(fcp->isp_scdma);
2861 	}
2862 	if (FC_SCRATCH_ACQUIRE(isp, chan)) {
2863 		isp_prt(isp, ISP_LOGERR, sacq);
2864 		return (-1);
2865 	}
2866 	MEMORYBARRIER(isp, SYNC_SFORDEV, 0, ISP_FC_SCRLEN, chan);
2867 	isp_mboxcmd(isp, &mbs);
2868 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2869 		FC_SCRATCH_RELEASE(isp, chan);
2870 		return (mbs.param[0] | (mbs.param[1] << 16));
2871 	}
2872 	MEMORYBARRIER(isp, SYNC_SFORCPU, 0, ISP_FC_SCRLEN, chan);
2873 	elp1 = fcp->isp_scratch;
2874 	elp3 = fcp->isp_scratch;
2875 	elp4 = fcp->isp_scratch;
2876 	for (i = 0, j = 0; i < mbs.param[1] && j < *num; i++) {
2877 		if (IS_24XX(isp)) {
2878 			isp_get_pnhle_24xx(isp, &elp4[i], &el4);
2879 			p = el4.pnhle_port_id_lo |
2880 			    (el4.pnhle_port_id_hi << 16);
2881 			h = el4.pnhle_handle;
2882 		} else if (IS_23XX(isp)) {
2883 			isp_get_pnhle_23xx(isp, &elp3[i], &el3);
2884 			p = el3.pnhle_port_id_lo |
2885 			    (el3.pnhle_port_id_hi << 16);
2886 			h = el3.pnhle_handle;
2887 		} else { /* 21xx */
2888 			isp_get_pnhle_21xx(isp, &elp1[i], &el1);
2889 			p = el1.pnhle_port_id_lo |
2890 			    ((el1.pnhle_port_id_hi_handle & 0xff) << 16);
2891 			h = el1.pnhle_port_id_hi_handle >> 8;
2892 		}
2893 		if (loop && (p >> 8) != (fcp->isp_portid >> 8))
2894 			continue;
2895 		handles[j++] = h;
2896 	}
2897 	*num = j;
2898 	FC_SCRATCH_RELEASE(isp, chan);
2899 	return (0);
2900 }
2901 
2902 static void
2903 isp_dump_chip_portdb(ispsoftc_t *isp, int chan)
2904 {
2905 	isp_pdb_t pdb;
2906 	uint16_t lim, nphdl;
2907 
2908 	isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGINFO, "Chan %d chip port dump", chan);
2909 	if (ISP_CAP_2KLOGIN(isp)) {
2910 		lim = NPH_MAX_2K;
2911 	} else {
2912 		lim = NPH_MAX;
2913 	}
2914 	for (nphdl = 0; nphdl != lim; nphdl++) {
2915 		if (isp_getpdb(isp, chan, nphdl, &pdb)) {
2916 			continue;
2917 		}
2918 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGINFO, "Chan %d Handle 0x%04x "
2919 		    "PortID 0x%06x WWPN 0x%02x%02x%02x%02x%02x%02x%02x%02x",
2920 		    chan, nphdl, pdb.portid, pdb.portname[0], pdb.portname[1],
2921 		    pdb.portname[2], pdb.portname[3], pdb.portname[4],
2922 		    pdb.portname[5], pdb.portname[6], pdb.portname[7]);
2923 	}
2924 }
2925 
2926 static uint64_t
2927 isp_get_wwn(ispsoftc_t *isp, int chan, int nphdl, int nodename)
2928 {
2929 	uint64_t wwn = INI_NONE;
2930 	mbreg_t mbs;
2931 
2932 	MBSINIT(&mbs, MBOX_GET_PORT_NAME,
2933 	    MBLOGALL & ~MBLOGMASK(MBOX_COMMAND_PARAM_ERROR), 500000);
2934 	if (ISP_CAP_2KLOGIN(isp)) {
2935 		mbs.param[1] = nphdl;
2936 		if (nodename) {
2937 			mbs.param[10] = 1;
2938 		}
2939 		mbs.param[9] = chan;
2940 	} else {
2941 		mbs.ibitm = 3;
2942 		mbs.param[1] = nphdl << 8;
2943 		if (nodename) {
2944 			mbs.param[1] |= 1;
2945 		}
2946 	}
2947 	isp_mboxcmd(isp, &mbs);
2948 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2949 		return (wwn);
2950 	}
2951 	if (IS_24XX(isp)) {
2952 		wwn =
2953 		    (((uint64_t)(mbs.param[2] >> 8))	<< 56) |
2954 		    (((uint64_t)(mbs.param[2] & 0xff))	<< 48) |
2955 		    (((uint64_t)(mbs.param[3] >> 8))	<< 40) |
2956 		    (((uint64_t)(mbs.param[3] & 0xff))	<< 32) |
2957 		    (((uint64_t)(mbs.param[6] >> 8))	<< 24) |
2958 		    (((uint64_t)(mbs.param[6] & 0xff))	<< 16) |
2959 		    (((uint64_t)(mbs.param[7] >> 8))	<<  8) |
2960 		    (((uint64_t)(mbs.param[7] & 0xff)));
2961 	} else {
2962 		wwn =
2963 		    (((uint64_t)(mbs.param[2] & 0xff))  << 56) |
2964 		    (((uint64_t)(mbs.param[2] >> 8))	<< 48) |
2965 		    (((uint64_t)(mbs.param[3] & 0xff))	<< 40) |
2966 		    (((uint64_t)(mbs.param[3] >> 8))	<< 32) |
2967 		    (((uint64_t)(mbs.param[6] & 0xff))	<< 24) |
2968 		    (((uint64_t)(mbs.param[6] >> 8))	<< 16) |
2969 		    (((uint64_t)(mbs.param[7] & 0xff))	<<  8) |
2970 		    (((uint64_t)(mbs.param[7] >> 8)));
2971 	}
2972 	return (wwn);
2973 }
2974 
2975 /*
2976  * Make sure we have good FC link.
2977  */
2978 
2979 static int
2980 isp_fclink_test(ispsoftc_t *isp, int chan, int usdelay)
2981 {
2982 	mbreg_t mbs;
2983 	int i, r;
2984 	uint16_t nphdl;
2985 	fcparam *fcp;
2986 	isp_pdb_t pdb;
2987 	NANOTIME_T hra, hrb;
2988 
2989 	fcp = FCPARAM(isp, chan);
2990 
2991 	if (fcp->isp_loopstate < LOOP_HAVE_LINK)
2992 		return (-1);
2993 	if (fcp->isp_loopstate >= LOOP_LTEST_DONE)
2994 		return (0);
2995 
2996 	isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC link test", chan);
2997 	fcp->isp_loopstate = LOOP_TESTING_LINK;
2998 
2999 	/*
3000 	 * Wait up to N microseconds for F/W to go to a ready state.
3001 	 */
3002 	GET_NANOTIME(&hra);
3003 	while (1) {
3004 		isp_change_fw_state(isp, chan, isp_fw_state(isp, chan));
3005 		if (fcp->isp_fwstate == FW_READY) {
3006 			break;
3007 		}
3008 		if (fcp->isp_loopstate < LOOP_TESTING_LINK)
3009 			goto abort;
3010 		GET_NANOTIME(&hrb);
3011 		if ((NANOTIME_SUB(&hrb, &hra) / 1000 + 1000 >= usdelay))
3012 			break;
3013 		ISP_SLEEP(isp, 1000);
3014 	}
3015 	if (fcp->isp_fwstate != FW_READY) {
3016 		isp_prt(isp, ISP_LOG_SANCFG,
3017 		    "Chan %d Firmware is not ready (%s)",
3018 		    chan, isp_fc_fw_statename(fcp->isp_fwstate));
3019 		return (-1);
3020 	}
3021 
3022 	/*
3023 	 * Get our Loop ID and Port ID.
3024 	 */
3025 	MBSINIT(&mbs, MBOX_GET_LOOP_ID, MBLOGALL, 0);
3026 	mbs.param[9] = chan;
3027 	isp_mboxcmd(isp, &mbs);
3028 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3029 		return (-1);
3030 	}
3031 
3032 	if (IS_2100(isp)) {
3033 		/*
3034 		 * Don't bother with fabric if we are using really old
3035 		 * 2100 firmware. It's just not worth it.
3036 		 */
3037 		if (ISP_FW_NEWER_THAN(isp, 1, 15, 37))
3038 			fcp->isp_topo = TOPO_FL_PORT;
3039 		else
3040 			fcp->isp_topo = TOPO_NL_PORT;
3041 	} else {
3042 		int topo = (int) mbs.param[6];
3043 		if (topo < TOPO_NL_PORT || topo > TOPO_PTP_STUB) {
3044 			topo = TOPO_PTP_STUB;
3045 		}
3046 		fcp->isp_topo = topo;
3047 	}
3048 	fcp->isp_portid = mbs.param[2] | (mbs.param[3] << 16);
3049 
3050 	if (!TOPO_IS_FABRIC(fcp->isp_topo)) {
3051 		fcp->isp_loopid = mbs.param[1] & 0xff;
3052 	} else if (fcp->isp_topo != TOPO_F_PORT) {
3053 		uint8_t alpa = fcp->isp_portid;
3054 
3055 		for (i = 0; alpa_map[i]; i++) {
3056 			if (alpa_map[i] == alpa)
3057 				break;
3058 		}
3059 		if (alpa_map[i])
3060 			fcp->isp_loopid = i;
3061 	}
3062 
3063 	if (fcp->isp_topo == TOPO_F_PORT || fcp->isp_topo == TOPO_FL_PORT) {
3064 		nphdl = IS_24XX(isp) ? NPH_FL_ID : FL_ID;
3065 		r = isp_getpdb(isp, chan, nphdl, &pdb);
3066 		if (r != 0 || pdb.portid == 0) {
3067 			if (IS_2100(isp)) {
3068 				fcp->isp_topo = TOPO_NL_PORT;
3069 			} else {
3070 				isp_prt(isp, ISP_LOGWARN,
3071 				    "fabric topology, but cannot get info about fabric controller (0x%x)", r);
3072 				fcp->isp_topo = TOPO_PTP_STUB;
3073 			}
3074 			goto not_on_fabric;
3075 		}
3076 
3077 		if (IS_24XX(isp)) {
3078 			fcp->isp_fabric_params = mbs.param[7];
3079 			fcp->isp_sns_hdl = NPH_SNS_ID;
3080 			r = isp_register_fc4_type_24xx(isp, chan);
3081 			if (r == 0)
3082 				isp_register_fc4_features_24xx(isp, chan);
3083 			isp_register_port_name_24xx(isp, chan);
3084 			isp_register_node_name_24xx(isp, chan);
3085 		} else {
3086 			fcp->isp_sns_hdl = SNS_ID;
3087 			r = isp_register_fc4_type(isp, chan);
3088 			if (r == 0 && fcp->role == ISP_ROLE_TARGET)
3089 				isp_send_change_request(isp, chan);
3090 		}
3091 		if (r) {
3092 			isp_prt(isp, ISP_LOGWARN|ISP_LOG_SANCFG, "%s: register fc4 type failed", __func__);
3093 			return (-1);
3094 		}
3095 	}
3096 
3097 not_on_fabric:
3098 	/* Get link speed. */
3099 	fcp->isp_gbspeed = 1;
3100 	if (IS_23XX(isp) || IS_24XX(isp)) {
3101 		MBSINIT(&mbs, MBOX_GET_SET_DATA_RATE, MBLOGALL, 3000000);
3102 		mbs.param[1] = MBGSD_GET_RATE;
3103 		/* mbs.param[2] undefined if we're just getting rate */
3104 		isp_mboxcmd(isp, &mbs);
3105 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
3106 			if (mbs.param[1] == MBGSD_10GB)
3107 				fcp->isp_gbspeed = 10;
3108 			else if (mbs.param[1] == MBGSD_16GB)
3109 				fcp->isp_gbspeed = 16;
3110 			else if (mbs.param[1] == MBGSD_8GB)
3111 				fcp->isp_gbspeed = 8;
3112 			else if (mbs.param[1] == MBGSD_4GB)
3113 				fcp->isp_gbspeed = 4;
3114 			else if (mbs.param[1] == MBGSD_2GB)
3115 				fcp->isp_gbspeed = 2;
3116 			else if (mbs.param[1] == MBGSD_1GB)
3117 				fcp->isp_gbspeed = 1;
3118 		}
3119 	}
3120 
3121 	if (fcp->isp_loopstate < LOOP_TESTING_LINK) {
3122 abort:
3123 		isp_prt(isp, ISP_LOG_SANCFG,
3124 		    "Chan %d FC link test aborted", chan);
3125 		return (1);
3126 	}
3127 	fcp->isp_loopstate = LOOP_LTEST_DONE;
3128 	isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGCONFIG,
3129 	    "Chan %d WWPN %016jx WWNN %016jx",
3130 	    chan, (uintmax_t)fcp->isp_wwpn, (uintmax_t)fcp->isp_wwnn);
3131 	isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGCONFIG,
3132 	    "Chan %d %dGb %s PortID 0x%06x LoopID 0x%02x",
3133 	    chan, fcp->isp_gbspeed, isp_fc_toponame(fcp), fcp->isp_portid,
3134 	    fcp->isp_loopid);
3135 	isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC link test done", chan);
3136 	return (0);
3137 }
3138 
3139 /*
3140  * Complete the synchronization of our Port Database.
3141  *
3142  * At this point, we've scanned the local loop (if any) and the fabric
3143  * and performed fabric logins on all new devices.
3144  *
3145  * Our task here is to go through our port database removing any entities
3146  * that are still marked probational (issuing PLOGO for ones which we had
3147  * PLOGI'd into) or are dead, and notifying upper layers about new/changed
3148  * devices.
3149  */
3150 static int
3151 isp_pdb_sync(ispsoftc_t *isp, int chan)
3152 {
3153 	fcparam *fcp = FCPARAM(isp, chan);
3154 	fcportdb_t *lp;
3155 	uint16_t dbidx;
3156 
3157 	if (fcp->isp_loopstate < LOOP_FSCAN_DONE)
3158 		return (-1);
3159 	if (fcp->isp_loopstate >= LOOP_READY)
3160 		return (0);
3161 
3162 	isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC PDB sync", chan);
3163 
3164 	fcp->isp_loopstate = LOOP_SYNCING_PDB;
3165 
3166 	for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
3167 		lp = &fcp->portdb[dbidx];
3168 
3169 		if (lp->state == FC_PORTDB_STATE_NIL)
3170 			continue;
3171 		if (lp->probational && lp->state != FC_PORTDB_STATE_ZOMBIE)
3172 			lp->state = FC_PORTDB_STATE_DEAD;
3173 		switch (lp->state) {
3174 		case FC_PORTDB_STATE_DEAD:
3175 			lp->state = FC_PORTDB_STATE_NIL;
3176 			isp_async(isp, ISPASYNC_DEV_GONE, chan, lp);
3177 			if (lp->autologin == 0) {
3178 				(void) isp_plogx(isp, chan, lp->handle,
3179 				    lp->portid,
3180 				    PLOGX_FLG_CMD_LOGO |
3181 				    PLOGX_FLG_IMPLICIT |
3182 				    PLOGX_FLG_FREE_NPHDL);
3183 			}
3184 			/*
3185 			 * Note that we might come out of this with our state
3186 			 * set to FC_PORTDB_STATE_ZOMBIE.
3187 			 */
3188 			break;
3189 		case FC_PORTDB_STATE_NEW:
3190 			lp->state = FC_PORTDB_STATE_VALID;
3191 			isp_async(isp, ISPASYNC_DEV_ARRIVED, chan, lp);
3192 			break;
3193 		case FC_PORTDB_STATE_CHANGED:
3194 			lp->state = FC_PORTDB_STATE_VALID;
3195 			isp_async(isp, ISPASYNC_DEV_CHANGED, chan, lp);
3196 			lp->portid = lp->new_portid;
3197 			lp->prli_word3 = lp->new_prli_word3;
3198 			break;
3199 		case FC_PORTDB_STATE_VALID:
3200 			isp_async(isp, ISPASYNC_DEV_STAYED, chan, lp);
3201 			break;
3202 		case FC_PORTDB_STATE_ZOMBIE:
3203 			break;
3204 		default:
3205 			isp_prt(isp, ISP_LOGWARN,
3206 			    "isp_pdb_sync: state %d for idx %d",
3207 			    lp->state, dbidx);
3208 			isp_dump_portdb(isp, chan);
3209 		}
3210 	}
3211 
3212 	if (fcp->isp_loopstate < LOOP_SYNCING_PDB) {
3213 		isp_prt(isp, ISP_LOG_SANCFG,
3214 		    "Chan %d FC PDB sync aborted", chan);
3215 		return (1);
3216 	}
3217 
3218 	fcp->isp_loopstate = LOOP_READY;
3219 	isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC PDB sync done", chan);
3220 	return (0);
3221 }
3222 
3223 static void
3224 isp_pdb_add_update(ispsoftc_t *isp, int chan, isp_pdb_t *pdb)
3225 {
3226 	fcportdb_t *lp;
3227 	uint64_t wwnn, wwpn;
3228 
3229 	MAKE_WWN_FROM_NODE_NAME(wwnn, pdb->nodename);
3230 	MAKE_WWN_FROM_NODE_NAME(wwpn, pdb->portname);
3231 
3232 	/* Search port database for the same WWPN. */
3233 	if (isp_find_pdb_by_wwpn(isp, chan, wwpn, &lp)) {
3234 		if (!lp->probational) {
3235 			isp_prt(isp, ISP_LOGERR,
3236 			    "Chan %d Port 0x%06x@0x%04x [%d] is not probational (0x%x)",
3237 			    chan, lp->portid, lp->handle,
3238 			    FC_PORTDB_TGT(isp, chan, lp), lp->state);
3239 			isp_dump_portdb(isp, chan);
3240 			return;
3241 		}
3242 		lp->probational = 0;
3243 		lp->node_wwn = wwnn;
3244 
3245 		/* Old device, nothing new. */
3246 		if (lp->portid == pdb->portid &&
3247 		    lp->handle == pdb->handle &&
3248 		    lp->prli_word3 == pdb->prli_word3) {
3249 			if (lp->state != FC_PORTDB_STATE_NEW)
3250 				lp->state = FC_PORTDB_STATE_VALID;
3251 			isp_prt(isp, ISP_LOG_SANCFG,
3252 			    "Chan %d Port 0x%06x@0x%04x is valid",
3253 			    chan, pdb->portid, pdb->handle);
3254 			return;
3255 		}
3256 
3257 		/* Something has changed. */
3258 		lp->state = FC_PORTDB_STATE_CHANGED;
3259 		lp->handle = pdb->handle;
3260 		lp->new_portid = pdb->portid;
3261 		lp->new_prli_word3 = pdb->prli_word3;
3262 		isp_prt(isp, ISP_LOG_SANCFG,
3263 		    "Chan %d Port 0x%06x@0x%04x is changed",
3264 		    chan, pdb->portid, pdb->handle);
3265 		return;
3266 	}
3267 
3268 	/* It seems like a new port. Find an empty slot for it. */
3269 	if (!isp_find_pdb_empty(isp, chan, &lp)) {
3270 		isp_prt(isp, ISP_LOGERR, "Chan %d out of portdb entries", chan);
3271 		return;
3272 	}
3273 
3274 	ISP_MEMZERO(lp, sizeof (fcportdb_t));
3275 	lp->autologin = 1;
3276 	lp->probational = 0;
3277 	lp->state = FC_PORTDB_STATE_NEW;
3278 	lp->portid = lp->new_portid = pdb->portid;
3279 	lp->prli_word3 = lp->new_prli_word3 = pdb->prli_word3;
3280 	lp->handle = pdb->handle;
3281 	lp->port_wwn = wwpn;
3282 	lp->node_wwn = wwnn;
3283 	isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Port 0x%06x@0x%04x is new",
3284 	    chan, pdb->portid, pdb->handle);
3285 }
3286 
3287 /*
3288  * Fix port IDs for logged-in initiators on pre-2400 chips.
3289  * For those chips we are not receiving login events, adding initiators
3290  * based on ATIO requests, but there is no port ID in that structure.
3291  */
3292 static void
3293 isp_fix_portids(ispsoftc_t *isp, int chan)
3294 {
3295 	fcparam *fcp = FCPARAM(isp, chan);
3296 	isp_pdb_t pdb;
3297 	uint64_t wwpn;
3298 	int i, r;
3299 
3300 	for (i = 0; i < MAX_FC_TARG; i++) {
3301 		fcportdb_t *lp = &fcp->portdb[i];
3302 
3303 		if (lp->state == FC_PORTDB_STATE_NIL ||
3304 		    lp->state == FC_PORTDB_STATE_ZOMBIE)
3305 			continue;
3306 		if (VALID_PORT(lp->portid))
3307 			continue;
3308 
3309 		r = isp_getpdb(isp, chan, lp->handle, &pdb);
3310 		if (fcp->isp_loopstate < LOOP_SCANNING_LOOP)
3311 			return;
3312 		if (r != 0) {
3313 			isp_prt(isp, ISP_LOGDEBUG1,
3314 			    "Chan %d FC Scan Loop handle %d returned %x",
3315 			    chan, lp->handle, r);
3316 			continue;
3317 		}
3318 
3319 		MAKE_WWN_FROM_NODE_NAME(wwpn, pdb.portname);
3320 		if (lp->port_wwn != wwpn)
3321 			continue;
3322 		lp->portid = lp->new_portid = pdb.portid;
3323 		isp_prt(isp, ISP_LOG_SANCFG,
3324 		    "Chan %d Port 0x%06x@0x%04x is fixed",
3325 		    chan, pdb.portid, pdb.handle);
3326 	}
3327 }
3328 
3329 /*
3330  * Scan local loop for devices.
3331  */
3332 static int
3333 isp_scan_loop(ispsoftc_t *isp, int chan)
3334 {
3335 	fcparam *fcp = FCPARAM(isp, chan);
3336 	int idx, lim, r;
3337 	isp_pdb_t pdb;
3338 	uint16_t *handles;
3339 	uint16_t handle;
3340 
3341 	if (fcp->isp_loopstate < LOOP_LTEST_DONE)
3342 		return (-1);
3343 	if (fcp->isp_loopstate >= LOOP_LSCAN_DONE)
3344 		return (0);
3345 
3346 	isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC loop scan", chan);
3347 	fcp->isp_loopstate = LOOP_SCANNING_LOOP;
3348 	if (TOPO_IS_FABRIC(fcp->isp_topo)) {
3349 		if (!IS_24XX(isp)) {
3350 			isp_fix_portids(isp, chan);
3351 			if (fcp->isp_loopstate < LOOP_SCANNING_LOOP)
3352 				goto abort;
3353 		}
3354 		isp_prt(isp, ISP_LOG_SANCFG,
3355 		    "Chan %d FC loop scan done (no loop)", chan);
3356 		fcp->isp_loopstate = LOOP_LSCAN_DONE;
3357 		return (0);
3358 	}
3359 
3360 	handles = (uint16_t *)fcp->isp_scanscratch;
3361 	lim = ISP_FC_SCRLEN / 2;
3362 	r = isp_gethandles(isp, chan, handles, &lim, 1);
3363 	if (r != 0) {
3364 		isp_prt(isp, ISP_LOG_SANCFG,
3365 		    "Chan %d Getting list of handles failed with %x", chan, r);
3366 		isp_prt(isp, ISP_LOG_SANCFG,
3367 		    "Chan %d FC loop scan done (bad)", chan);
3368 		return (-1);
3369 	}
3370 
3371 	isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Got %d handles",
3372 	    chan, lim);
3373 
3374 	/*
3375 	 * Run through the list and get the port database info for each one.
3376 	 */
3377 	isp_mark_portdb(isp, chan);
3378 	for (idx = 0; idx < lim; idx++) {
3379 		handle = handles[idx];
3380 
3381 		/*
3382 		 * Don't scan "special" ids.
3383 		 */
3384 		if (ISP_CAP_2KLOGIN(isp)) {
3385 			if (handle >= NPH_RESERVED)
3386 				continue;
3387 		} else {
3388 			if (handle >= FL_ID && handle <= SNS_ID)
3389 				continue;
3390 		}
3391 
3392 		/*
3393 		 * In older cards with older f/w GET_PORT_DATABASE has been
3394 		 * known to hang. This trick gets around that problem.
3395 		 */
3396 		if (IS_2100(isp) || IS_2200(isp)) {
3397 			uint64_t node_wwn = isp_get_wwn(isp, chan, handle, 1);
3398 			if (fcp->isp_loopstate < LOOP_SCANNING_LOOP) {
3399 abort:
3400 				isp_prt(isp, ISP_LOG_SANCFG,
3401 				    "Chan %d FC loop scan aborted", chan);
3402 				return (1);
3403 			}
3404 			if (node_wwn == INI_NONE) {
3405 				continue;
3406 			}
3407 		}
3408 
3409 		/*
3410 		 * Get the port database entity for this index.
3411 		 */
3412 		r = isp_getpdb(isp, chan, handle, &pdb);
3413 		if (fcp->isp_loopstate < LOOP_SCANNING_LOOP)
3414 			goto abort;
3415 		if (r != 0) {
3416 			isp_prt(isp, ISP_LOGDEBUG1,
3417 			    "Chan %d FC Scan Loop handle %d returned %x",
3418 			    chan, handle, r);
3419 			continue;
3420 		}
3421 
3422 		isp_pdb_add_update(isp, chan, &pdb);
3423 	}
3424 	if (fcp->isp_loopstate < LOOP_SCANNING_LOOP)
3425 		goto abort;
3426 	fcp->isp_loopstate = LOOP_LSCAN_DONE;
3427 	isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC loop scan done", chan);
3428 	return (0);
3429 }
3430 
3431 /*
3432  * Scan the fabric for devices and add them to our port database.
3433  *
3434  * Use the GID_FT command to get all Port IDs for FC4 SCSI devices it knows.
3435  *
3436  * For 2100-23XX cards, we can use the SNS mailbox command to pass simple
3437  * name server commands to the switch management server via the QLogic f/w.
3438  *
3439  * For the 24XX card, we have to use CT-Pass through run via the Execute IOCB
3440  * mailbox command.
3441  */
3442 #define	GIDLEN	(ISP_FC_SCRLEN - (3 * QENTRY_LEN))
3443 #define	NGENT	((GIDLEN - 16) >> 2)
3444 
3445 #define	XTXOFF	(ISP_FC_SCRLEN - (3 * QENTRY_LEN))	/* CT request */
3446 #define	CTXOFF	(ISP_FC_SCRLEN - (2 * QENTRY_LEN))	/* Request IOCB */
3447 #define	ZTXOFF	(ISP_FC_SCRLEN - (1 * QENTRY_LEN))	/* Response IOCB */
3448 
3449 static int
3450 isp_gid_ft_sns(ispsoftc_t *isp, int chan)
3451 {
3452 	union {
3453 		sns_gid_ft_req_t _x;
3454 		uint8_t _y[SNS_GID_FT_REQ_SIZE];
3455 	} un;
3456 	fcparam *fcp = FCPARAM(isp, chan);
3457 	sns_gid_ft_req_t *rq = &un._x;
3458 	uint8_t *scp = fcp->isp_scratch;
3459 	mbreg_t mbs;
3460 
3461 	isp_prt(isp, ISP_LOGDEBUG0, "Chan %d requesting GID_FT via SNS", chan);
3462 	if (FC_SCRATCH_ACQUIRE(isp, chan)) {
3463 		isp_prt(isp, ISP_LOGERR, sacq);
3464 		return (-1);
3465 	}
3466 
3467 	ISP_MEMZERO(rq, SNS_GID_FT_REQ_SIZE);
3468 	rq->snscb_rblen = GIDLEN >> 1;
3469 	rq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma);
3470 	rq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma);
3471 	rq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma);
3472 	rq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma);
3473 	rq->snscb_sblen = 6;
3474 	rq->snscb_cmd = SNS_GID_FT;
3475 	rq->snscb_mword_div_2 = NGENT;
3476 	rq->snscb_fc4_type = FC4_SCSI;
3477 
3478 	isp_put_gid_ft_request(isp, rq, (sns_gid_ft_req_t *)&scp[CTXOFF]);
3479 	MEMORYBARRIER(isp, SYNC_SFORDEV, CTXOFF, SNS_GID_FT_REQ_SIZE, chan);
3480 
3481 	MBSINIT(&mbs, MBOX_SEND_SNS, MBLOGALL, 10000000);
3482 	mbs.param[0] = MBOX_SEND_SNS;
3483 	mbs.param[1] = SNS_GID_FT_REQ_SIZE >> 1;
3484 	mbs.param[2] = DMA_WD1(fcp->isp_scdma + CTXOFF);
3485 	mbs.param[3] = DMA_WD0(fcp->isp_scdma + CTXOFF);
3486 	mbs.param[6] = DMA_WD3(fcp->isp_scdma + CTXOFF);
3487 	mbs.param[7] = DMA_WD2(fcp->isp_scdma + CTXOFF);
3488 	isp_mboxcmd(isp, &mbs);
3489 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3490 		if (mbs.param[0] == MBOX_INVALID_COMMAND) {
3491 			return (1);
3492 		} else {
3493 			return (-1);
3494 		}
3495 	}
3496 	MEMORYBARRIER(isp, SYNC_SFORCPU, 0, GIDLEN, chan);
3497 	if (isp->isp_dblev & ISP_LOGDEBUG1)
3498 		isp_print_bytes(isp, "CT response", GIDLEN, scp);
3499 	isp_get_gid_ft_response(isp, (sns_gid_ft_rsp_t *)scp,
3500 	    (sns_gid_ft_rsp_t *)fcp->isp_scanscratch, NGENT);
3501 	FC_SCRATCH_RELEASE(isp, chan);
3502 	return (0);
3503 }
3504 
3505 static int
3506 isp_ct_passthru(ispsoftc_t *isp, int chan, uint32_t cmd_bcnt, uint32_t rsp_bcnt)
3507 {
3508 	mbreg_t mbs;
3509 	fcparam *fcp = FCPARAM(isp, chan);
3510 	union {
3511 		isp_ct_pt_t plocal;
3512 		uint8_t q[QENTRY_LEN];
3513 	} un;
3514 	isp_ct_pt_t *pt;
3515 	uint8_t *scp = fcp->isp_scratch;
3516 
3517 	/*
3518 	 * Build a Passthrough IOCB in memory.
3519 	 */
3520 	pt = &un.plocal;
3521 	ISP_MEMZERO(un.q, QENTRY_LEN);
3522 	pt->ctp_header.rqs_entry_count = 1;
3523 	pt->ctp_header.rqs_entry_type = RQSTYPE_CT_PASSTHRU;
3524 	pt->ctp_handle = 0xffffffff;
3525 	pt->ctp_nphdl = fcp->isp_sns_hdl;
3526 	pt->ctp_cmd_cnt = 1;
3527 	pt->ctp_vpidx = ISP_GET_VPIDX(isp, chan);
3528 	pt->ctp_time = 10;
3529 	pt->ctp_rsp_cnt = 1;
3530 	pt->ctp_rsp_bcnt = rsp_bcnt;
3531 	pt->ctp_cmd_bcnt = cmd_bcnt;
3532 	pt->ctp_dataseg[0].ds_base = DMA_LO32(fcp->isp_scdma+XTXOFF);
3533 	pt->ctp_dataseg[0].ds_basehi = DMA_HI32(fcp->isp_scdma+XTXOFF);
3534 	pt->ctp_dataseg[0].ds_count = cmd_bcnt;
3535 	pt->ctp_dataseg[1].ds_base = DMA_LO32(fcp->isp_scdma);
3536 	pt->ctp_dataseg[1].ds_basehi = DMA_HI32(fcp->isp_scdma);
3537 	pt->ctp_dataseg[1].ds_count = rsp_bcnt;
3538 	isp_put_ct_pt(isp, pt, (isp_ct_pt_t *)&scp[CTXOFF]);
3539 	if (isp->isp_dblev & ISP_LOGDEBUG1)
3540 		isp_print_bytes(isp, "CT IOCB request", QENTRY_LEN, &scp[CTXOFF]);
3541 
3542 	/*
3543 	 * Execute the Passthrough IOCB.
3544 	 */
3545 	ISP_MEMZERO(&scp[ZTXOFF], QENTRY_LEN);
3546 	MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL,
3547 	    MBCMD_DEFAULT_TIMEOUT + pt->ctp_time * 1000000);
3548 	mbs.param[1] = QENTRY_LEN;
3549 	mbs.param[2] = DMA_WD1(fcp->isp_scdma + CTXOFF);
3550 	mbs.param[3] = DMA_WD0(fcp->isp_scdma + CTXOFF);
3551 	mbs.param[6] = DMA_WD3(fcp->isp_scdma + CTXOFF);
3552 	mbs.param[7] = DMA_WD2(fcp->isp_scdma + CTXOFF);
3553 	MEMORYBARRIER(isp, SYNC_SFORDEV, XTXOFF, 2 * QENTRY_LEN, chan);
3554 	isp_mboxcmd(isp, &mbs);
3555 	if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3556 		return (-1);
3557 	}
3558 	MEMORYBARRIER(isp, SYNC_SFORCPU, 0, ISP_FC_SCRLEN, chan);
3559 	if (isp->isp_dblev & ISP_LOGDEBUG1)
3560 		isp_print_bytes(isp, "CT IOCB response", QENTRY_LEN, &scp[ZTXOFF]);
3561 	pt = &un.plocal;
3562 	isp_get_ct_pt(isp, (isp_ct_pt_t *) &scp[ZTXOFF], pt);
3563 	if (pt->ctp_status && pt->ctp_status != RQCS_DATA_UNDERRUN) {
3564 		isp_prt(isp, ISP_LOGWARN,
3565 		    "Chan %d GID_FT CT Passthrough returned 0x%x",
3566 		    chan, pt->ctp_status);
3567 		return (-1);
3568 	}
3569 
3570 	return (0);
3571 }
3572 
3573 static int
3574 isp_gid_ft_ct_passthru(ispsoftc_t *isp, int chan)
3575 {
3576 	fcparam *fcp = FCPARAM(isp, chan);
3577 	ct_hdr_t ct;
3578 	uint32_t *rp;
3579 	uint8_t *scp = fcp->isp_scratch;
3580 
3581 	isp_prt(isp, ISP_LOGDEBUG0, "Chan %d requesting GID_FT via CT", chan);
3582 	if (FC_SCRATCH_ACQUIRE(isp, chan)) {
3583 		isp_prt(isp, ISP_LOGERR, sacq);
3584 		return (-1);
3585 	}
3586 
3587 	/*
3588 	 * Build the CT header and command in memory.
3589 	 */
3590 	ISP_MEMZERO(&ct, sizeof (ct));
3591 	ct.ct_revision = CT_REVISION;
3592 	ct.ct_fcs_type = CT_FC_TYPE_FC;
3593 	ct.ct_fcs_subtype = CT_FC_SUBTYPE_NS;
3594 	ct.ct_cmd_resp = SNS_GID_FT;
3595 	ct.ct_bcnt_resid = (GIDLEN - 16) >> 2;
3596 	isp_put_ct_hdr(isp, &ct, (ct_hdr_t *) &scp[XTXOFF]);
3597 	rp = (uint32_t *) &scp[XTXOFF + sizeof(ct)];
3598 	ISP_IOZPUT_32(isp, FC4_SCSI, rp);
3599 	if (isp->isp_dblev & ISP_LOGDEBUG1) {
3600 		isp_print_bytes(isp, "CT request",
3601 		    sizeof(ct) + sizeof(uint32_t), &scp[XTXOFF]);
3602 	}
3603 
3604 	if (isp_ct_passthru(isp, chan, sizeof(ct) + sizeof(uint32_t), GIDLEN)) {
3605 		FC_SCRATCH_RELEASE(isp, chan);
3606 		return (-1);
3607 	}
3608 
3609 	if (isp->isp_dblev & ISP_LOGDEBUG1)
3610 		isp_print_bytes(isp, "CT response", GIDLEN, scp);
3611 	isp_get_gid_ft_response(isp, (sns_gid_ft_rsp_t *)scp,
3612 	    (sns_gid_ft_rsp_t *)fcp->isp_scanscratch, NGENT);
3613 	FC_SCRATCH_RELEASE(isp, chan);
3614 	return (0);
3615 }
3616 
3617 static int
3618 isp_scan_fabric(ispsoftc_t *isp, int chan)
3619 {
3620 	fcparam *fcp = FCPARAM(isp, chan);
3621 	fcportdb_t *lp;
3622 	uint32_t portid;
3623 	uint16_t nphdl;
3624 	isp_pdb_t pdb;
3625 	int portidx, portlim, r;
3626 	sns_gid_ft_rsp_t *rs;
3627 
3628 	if (fcp->isp_loopstate < LOOP_LSCAN_DONE)
3629 		return (-1);
3630 	if (fcp->isp_loopstate >= LOOP_FSCAN_DONE)
3631 		return (0);
3632 
3633 	isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC fabric scan", chan);
3634 	fcp->isp_loopstate = LOOP_SCANNING_FABRIC;
3635 	if (!TOPO_IS_FABRIC(fcp->isp_topo)) {
3636 		fcp->isp_loopstate = LOOP_FSCAN_DONE;
3637 		isp_prt(isp, ISP_LOG_SANCFG,
3638 		    "Chan %d FC fabric scan done (no fabric)", chan);
3639 		return (0);
3640 	}
3641 
3642 	if (fcp->isp_loopstate < LOOP_SCANNING_FABRIC) {
3643 abort:
3644 		FC_SCRATCH_RELEASE(isp, chan);
3645 		isp_prt(isp, ISP_LOG_SANCFG,
3646 		    "Chan %d FC fabric scan aborted", chan);
3647 		return (1);
3648 	}
3649 
3650 	/*
3651 	 * Make sure we still are logged into the fabric controller.
3652 	 */
3653 	nphdl = IS_24XX(isp) ? NPH_FL_ID : FL_ID;
3654 	r = isp_getpdb(isp, chan, nphdl, &pdb);
3655 	if ((r & 0xffff) == MBOX_NOT_LOGGED_IN) {
3656 		isp_dump_chip_portdb(isp, chan);
3657 	}
3658 	if (r) {
3659 		fcp->isp_loopstate = LOOP_LTEST_DONE;
3660 fail:
3661 		isp_prt(isp, ISP_LOG_SANCFG,
3662 		    "Chan %d FC fabric scan done (bad)", chan);
3663 		return (-1);
3664 	}
3665 
3666 	/* Get list of port IDs from SNS. */
3667 	if (IS_24XX(isp))
3668 		r = isp_gid_ft_ct_passthru(isp, chan);
3669 	else
3670 		r = isp_gid_ft_sns(isp, chan);
3671 	if (fcp->isp_loopstate < LOOP_SCANNING_FABRIC)
3672 		goto abort;
3673 	if (r > 0) {
3674 		fcp->isp_loopstate = LOOP_FSCAN_DONE;
3675 		return (-1);
3676 	} else if (r < 0) {
3677 		fcp->isp_loopstate = LOOP_LTEST_DONE;	/* try again */
3678 		return (-1);
3679 	}
3680 
3681 	rs = (sns_gid_ft_rsp_t *) fcp->isp_scanscratch;
3682 	if (fcp->isp_loopstate < LOOP_SCANNING_FABRIC)
3683 		goto abort;
3684 	if (rs->snscb_cthdr.ct_cmd_resp != LS_ACC) {
3685 		int level;
3686 		if (rs->snscb_cthdr.ct_reason == 9 && rs->snscb_cthdr.ct_explanation == 7) {
3687 			level = ISP_LOG_SANCFG;
3688 		} else {
3689 			level = ISP_LOGWARN;
3690 		}
3691 		isp_prt(isp, level, "Chan %d Fabric Nameserver rejected GID_FT"
3692 		    " (Reason=0x%x Expl=0x%x)", chan,
3693 		    rs->snscb_cthdr.ct_reason,
3694 		    rs->snscb_cthdr.ct_explanation);
3695 		fcp->isp_loopstate = LOOP_FSCAN_DONE;
3696 		return (-1);
3697 	}
3698 
3699 	/* Check our buffer was big enough to get the full list. */
3700 	for (portidx = 0; portidx < NGENT-1; portidx++) {
3701 		if (rs->snscb_ports[portidx].control & 0x80)
3702 			break;
3703 	}
3704 	if ((rs->snscb_ports[portidx].control & 0x80) == 0) {
3705 		isp_prt(isp, ISP_LOGWARN,
3706 		    "fabric too big for scratch area: increase ISP_FC_SCRLEN");
3707 	}
3708 	portlim = portidx + 1;
3709 	isp_prt(isp, ISP_LOG_SANCFG,
3710 	    "Chan %d Got %d ports back from name server", chan, portlim);
3711 
3712 	/* Go through the list and remove duplicate port ids. */
3713 	for (portidx = 0; portidx < portlim; portidx++) {
3714 		int npidx;
3715 
3716 		portid =
3717 		    ((rs->snscb_ports[portidx].portid[0]) << 16) |
3718 		    ((rs->snscb_ports[portidx].portid[1]) << 8) |
3719 		    ((rs->snscb_ports[portidx].portid[2]));
3720 
3721 		for (npidx = portidx + 1; npidx < portlim; npidx++) {
3722 			uint32_t new_portid =
3723 			    ((rs->snscb_ports[npidx].portid[0]) << 16) |
3724 			    ((rs->snscb_ports[npidx].portid[1]) << 8) |
3725 			    ((rs->snscb_ports[npidx].portid[2]));
3726 			if (new_portid == portid) {
3727 				break;
3728 			}
3729 		}
3730 
3731 		if (npidx < portlim) {
3732 			rs->snscb_ports[npidx].portid[0] = 0;
3733 			rs->snscb_ports[npidx].portid[1] = 0;
3734 			rs->snscb_ports[npidx].portid[2] = 0;
3735 			isp_prt(isp, ISP_LOG_SANCFG, "Chan %d removing duplicate PortID 0x%06x entry from list", chan, portid);
3736 		}
3737 	}
3738 
3739 	/*
3740 	 * We now have a list of Port IDs for all FC4 SCSI devices
3741 	 * that the Fabric Name server knows about.
3742 	 *
3743 	 * For each entry on this list go through our port database looking
3744 	 * for probational entries- if we find one, then an old entry is
3745 	 * maybe still this one. We get some information to find out.
3746 	 *
3747 	 * Otherwise, it's a new fabric device, and we log into it
3748 	 * (unconditionally). After searching the entire database
3749 	 * again to make sure that we never ever ever ever have more
3750 	 * than one entry that has the same PortID or the same
3751 	 * WWNN/WWPN duple, we enter the device into our database.
3752 	 */
3753 	isp_mark_portdb(isp, chan);
3754 	for (portidx = 0; portidx < portlim; portidx++) {
3755 		portid = ((rs->snscb_ports[portidx].portid[0]) << 16) |
3756 			 ((rs->snscb_ports[portidx].portid[1]) << 8) |
3757 			 ((rs->snscb_ports[portidx].portid[2]));
3758 		isp_prt(isp, ISP_LOG_SANCFG,
3759 		    "Chan %d Checking fabric port 0x%06x", chan, portid);
3760 		if (portid == 0) {
3761 			isp_prt(isp, ISP_LOG_SANCFG,
3762 			    "Chan %d Port at idx %d is zero",
3763 			    chan, portidx);
3764 			continue;
3765 		}
3766 		if (portid == fcp->isp_portid) {
3767 			isp_prt(isp, ISP_LOG_SANCFG,
3768 			    "Chan %d Port 0x%06x is our", chan, portid);
3769 			continue;
3770 		}
3771 
3772 		/* Now search the entire port database for the same portid. */
3773 		if (isp_find_pdb_by_portid(isp, chan, portid, &lp)) {
3774 			if (!lp->probational) {
3775 				isp_prt(isp, ISP_LOGERR,
3776 				    "Chan %d Port 0x%06x@0x%04x [%d] is not probational (0x%x)",
3777 				    chan, lp->portid, lp->handle,
3778 				    FC_PORTDB_TGT(isp, chan, lp), lp->state);
3779 				isp_dump_portdb(isp, chan);
3780 				goto fail;
3781 			}
3782 
3783 			/*
3784 			 * See if we're still logged into it.
3785 			 *
3786 			 * If we aren't, mark it as a dead device and
3787 			 * leave the new portid in the database entry
3788 			 * for somebody further along to decide what to
3789 			 * do (policy choice).
3790 			 *
3791 			 * If we are, check to see if it's the same
3792 			 * device still (it should be). If for some
3793 			 * reason it isn't, mark it as a changed device
3794 			 * and leave the new portid and role in the
3795 			 * database entry for somebody further along to
3796 			 * decide what to do (policy choice).
3797 			 */
3798 			r = isp_getpdb(isp, chan, lp->handle, &pdb);
3799 			if (fcp->isp_loopstate < LOOP_SCANNING_FABRIC)
3800 				goto abort;
3801 			if (r != 0) {
3802 				lp->state = FC_PORTDB_STATE_DEAD;
3803 				isp_prt(isp, ISP_LOG_SANCFG,
3804 				    "Chan %d Port 0x%06x handle 0x%x is dead (%d)",
3805 				    chan, portid, lp->handle, r);
3806 				goto relogin;
3807 			}
3808 
3809 			isp_pdb_add_update(isp, chan, &pdb);
3810 			continue;
3811 		}
3812 
3813 relogin:
3814 		if ((fcp->role & ISP_ROLE_INITIATOR) == 0) {
3815 			isp_prt(isp, ISP_LOG_SANCFG,
3816 			    "Chan %d Port 0x%06x is not logged in", chan, portid);
3817 			continue;
3818 		}
3819 
3820 		if (isp_login_device(isp, chan, portid, &pdb,
3821 		    &FCPARAM(isp, 0)->isp_lasthdl)) {
3822 			if (fcp->isp_loopstate < LOOP_SCANNING_FABRIC)
3823 				goto abort;
3824 			continue;
3825 		}
3826 
3827 		isp_pdb_add_update(isp, chan, &pdb);
3828 	}
3829 
3830 	if (fcp->isp_loopstate < LOOP_SCANNING_FABRIC)
3831 		goto abort;
3832 	fcp->isp_loopstate = LOOP_FSCAN_DONE;
3833 	isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC fabric scan done", chan);
3834 	return (0);
3835 }
3836 
3837 /*
3838  * Find an unused handle and try and use to login to a port.
3839  */
3840 static int
3841 isp_login_device(ispsoftc_t *isp, int chan, uint32_t portid, isp_pdb_t *p, uint16_t *ohp)
3842 {
3843 	int lim, i, r;
3844 	uint16_t handle;
3845 
3846 	if (ISP_CAP_2KLOGIN(isp)) {
3847 		lim = NPH_MAX_2K;
3848 	} else {
3849 		lim = NPH_MAX;
3850 	}
3851 
3852 	handle = isp_next_handle(isp, ohp);
3853 	for (i = 0; i < lim; i++) {
3854 		if (FCPARAM(isp, chan)->isp_loopstate != LOOP_SCANNING_FABRIC)
3855 			return (-1);
3856 
3857 		/* Check if this handle is free. */
3858 		r = isp_getpdb(isp, chan, handle, p);
3859 		if (r == 0) {
3860 			if (p->portid != portid) {
3861 				/* This handle is busy, try next one. */
3862 				handle = isp_next_handle(isp, ohp);
3863 				continue;
3864 			}
3865 			break;
3866 		}
3867 		if (FCPARAM(isp, chan)->isp_loopstate != LOOP_SCANNING_FABRIC)
3868 			return (-1);
3869 
3870 		/*
3871 		 * Now try and log into the device
3872 		 */
3873 		r = isp_plogx(isp, chan, handle, portid, PLOGX_FLG_CMD_PLOGI);
3874 		if (r == 0) {
3875 			break;
3876 		} else if ((r & 0xffff) == MBOX_PORT_ID_USED) {
3877 			/*
3878 			 * If we get here, then the firmwware still thinks we're logged into this device, but with a different
3879 			 * handle. We need to break that association. We used to try and just substitute the handle, but then
3880 			 * failed to get any data via isp_getpdb (below).
3881 			 */
3882 			if (isp_plogx(isp, chan, r >> 16, portid, PLOGX_FLG_CMD_LOGO | PLOGX_FLG_IMPLICIT | PLOGX_FLG_FREE_NPHDL)) {
3883 				isp_prt(isp, ISP_LOGERR, "baw... logout of %x failed", r >> 16);
3884 			}
3885 			if (FCPARAM(isp, chan)->isp_loopstate != LOOP_SCANNING_FABRIC)
3886 				return (-1);
3887 			r = isp_plogx(isp, chan, handle, portid, PLOGX_FLG_CMD_PLOGI);
3888 			if (r != 0)
3889 				i = lim;
3890 			break;
3891 		} else if ((r & 0xffff) == MBOX_LOOP_ID_USED) {
3892 			/* Try the next handle. */
3893 			handle = isp_next_handle(isp, ohp);
3894 		} else {
3895 			/* Give up. */
3896 			i = lim;
3897 			break;
3898 		}
3899 	}
3900 
3901 	if (i == lim) {
3902 		isp_prt(isp, ISP_LOGWARN, "Chan %d PLOGI 0x%06x failed", chan, portid);
3903 		return (-1);
3904 	}
3905 
3906 	/*
3907 	 * If we successfully logged into it, get the PDB for it
3908 	 * so we can crosscheck that it is still what we think it
3909 	 * is and that we also have the role it plays
3910 	 */
3911 	r = isp_getpdb(isp, chan, handle, p);
3912 	if (r != 0) {
3913 		isp_prt(isp, ISP_LOGERR, "Chan %d new device 0x%06x@0x%x disappeared", chan, portid, handle);
3914 		return (-1);
3915 	}
3916 
3917 	if (p->handle != handle || p->portid != portid) {
3918 		isp_prt(isp, ISP_LOGERR, "Chan %d new device 0x%06x@0x%x changed (0x%06x@0x%0x)",
3919 		    chan, portid, handle, p->portid, p->handle);
3920 		return (-1);
3921 	}
3922 	return (0);
3923 }
3924 
3925 static int
3926 isp_send_change_request(ispsoftc_t *isp, int chan)
3927 {
3928 	mbreg_t mbs;
3929 
3930 	MBSINIT(&mbs, MBOX_SEND_CHANGE_REQUEST, MBLOGALL, 500000);
3931 	mbs.param[1] = 0x03;
3932 	mbs.param[9] = chan;
3933 	isp_mboxcmd(isp, &mbs);
3934 	return (mbs.param[0] == MBOX_COMMAND_COMPLETE ? 0 : -1);
3935 }
3936 
3937 static int
3938 isp_register_fc4_type(ispsoftc_t *isp, int chan)
3939 {
3940 	fcparam *fcp = FCPARAM(isp, chan);
3941 	uint8_t local[SNS_RFT_ID_REQ_SIZE];
3942 	sns_screq_t *reqp = (sns_screq_t *) local;
3943 	mbreg_t mbs;
3944 
3945 	ISP_MEMZERO((void *) reqp, SNS_RFT_ID_REQ_SIZE);
3946 	reqp->snscb_rblen = SNS_RFT_ID_RESP_SIZE >> 1;
3947 	reqp->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma + 0x100);
3948 	reqp->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma + 0x100);
3949 	reqp->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma + 0x100);
3950 	reqp->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma + 0x100);
3951 	reqp->snscb_sblen = 22;
3952 	reqp->snscb_data[0] = SNS_RFT_ID;
3953 	reqp->snscb_data[4] = fcp->isp_portid & 0xffff;
3954 	reqp->snscb_data[5] = (fcp->isp_portid >> 16) & 0xff;
3955 	reqp->snscb_data[6] = (1 << FC4_SCSI);
3956 	if (FC_SCRATCH_ACQUIRE(isp, chan)) {
3957 		isp_prt(isp, ISP_LOGERR, sacq);
3958 		return (-1);
3959 	}
3960 	isp_put_sns_request(isp, reqp, (sns_screq_t *) fcp->isp_scratch);
3961 	MBSINIT(&mbs, MBOX_SEND_SNS, MBLOGALL, 1000000);
3962 	mbs.param[1] = SNS_RFT_ID_REQ_SIZE >> 1;
3963 	mbs.param[2] = DMA_WD1(fcp->isp_scdma);
3964 	mbs.param[3] = DMA_WD0(fcp->isp_scdma);
3965 	mbs.param[6] = DMA_WD3(fcp->isp_scdma);
3966 	mbs.param[7] = DMA_WD2(fcp->isp_scdma);
3967 	MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_RFT_ID_REQ_SIZE, chan);
3968 	isp_mboxcmd(isp, &mbs);
3969 	FC_SCRATCH_RELEASE(isp, chan);
3970 	if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
3971 		return (0);
3972 	} else {
3973 		return (-1);
3974 	}
3975 }
3976 
3977 static int
3978 isp_register_fc4_type_24xx(ispsoftc_t *isp, int chan)
3979 {
3980 	fcparam *fcp = FCPARAM(isp, chan);
3981 	ct_hdr_t *ct;
3982 	rft_id_t rp;
3983 	uint8_t *scp = fcp->isp_scratch;
3984 
3985 	if (FC_SCRATCH_ACQUIRE(isp, chan)) {
3986 		isp_prt(isp, ISP_LOGERR, sacq);
3987 		return (-1);
3988 	}
3989 
3990 	/*
3991 	 * Build the CT header and command in memory.
3992 	 */
3993 	ISP_MEMZERO(&rp, sizeof(rp));
3994 	ct = &rp.rftid_hdr;
3995 	ct->ct_revision = CT_REVISION;
3996 	ct->ct_fcs_type = CT_FC_TYPE_FC;
3997 	ct->ct_fcs_subtype = CT_FC_SUBTYPE_NS;
3998 	ct->ct_cmd_resp = SNS_RFT_ID;
3999 	ct->ct_bcnt_resid = (sizeof (rft_id_t) - sizeof (ct_hdr_t)) >> 2;
4000 	rp.rftid_portid[0] = fcp->isp_portid >> 16;
4001 	rp.rftid_portid[1] = fcp->isp_portid >> 8;
4002 	rp.rftid_portid[2] = fcp->isp_portid;
4003 	rp.rftid_fc4types[FC4_SCSI >> 5] = 1 << (FC4_SCSI & 0x1f);
4004 	isp_put_rft_id(isp, &rp, (rft_id_t *)&scp[XTXOFF]);
4005 	if (isp->isp_dblev & ISP_LOGDEBUG1)
4006 		isp_print_bytes(isp, "CT request", sizeof(rft_id_t), &scp[XTXOFF]);
4007 
4008 	if (isp_ct_passthru(isp, chan, sizeof(rft_id_t), sizeof(ct_hdr_t))) {
4009 		FC_SCRATCH_RELEASE(isp, chan);
4010 		return (-1);
4011 	}
4012 
4013 	isp_get_ct_hdr(isp, (ct_hdr_t *) scp, ct);
4014 	FC_SCRATCH_RELEASE(isp, chan);
4015 	if (ct->ct_cmd_resp == LS_RJT) {
4016 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOG_WARN1, "Chan %d Register FC4 Type rejected", chan);
4017 		return (-1);
4018 	} else if (ct->ct_cmd_resp == LS_ACC) {
4019 		isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Register FC4 Type accepted", chan);
4020 	} else {
4021 		isp_prt(isp, ISP_LOGWARN, "Chan %d Register FC4 Type: 0x%x", chan, ct->ct_cmd_resp);
4022 		return (-1);
4023 	}
4024 	return (0);
4025 }
4026 
4027 static int
4028 isp_register_fc4_features_24xx(ispsoftc_t *isp, int chan)
4029 {
4030 	fcparam *fcp = FCPARAM(isp, chan);
4031 	ct_hdr_t *ct;
4032 	rff_id_t rp;
4033 	uint8_t *scp = fcp->isp_scratch;
4034 
4035 	if (FC_SCRATCH_ACQUIRE(isp, chan)) {
4036 		isp_prt(isp, ISP_LOGERR, sacq);
4037 		return (-1);
4038 	}
4039 
4040 	/*
4041 	 * Build the CT header and command in memory.
4042 	 */
4043 	ISP_MEMZERO(&rp, sizeof(rp));
4044 	ct = &rp.rffid_hdr;
4045 	ct->ct_revision = CT_REVISION;
4046 	ct->ct_fcs_type = CT_FC_TYPE_FC;
4047 	ct->ct_fcs_subtype = CT_FC_SUBTYPE_NS;
4048 	ct->ct_cmd_resp = SNS_RFF_ID;
4049 	ct->ct_bcnt_resid = (sizeof (rff_id_t) - sizeof (ct_hdr_t)) >> 2;
4050 	rp.rffid_portid[0] = fcp->isp_portid >> 16;
4051 	rp.rffid_portid[1] = fcp->isp_portid >> 8;
4052 	rp.rffid_portid[2] = fcp->isp_portid;
4053 	rp.rffid_fc4features = 0;
4054 	if (fcp->role & ISP_ROLE_TARGET)
4055 		rp.rffid_fc4features |= 1;
4056 	if (fcp->role & ISP_ROLE_INITIATOR)
4057 		rp.rffid_fc4features |= 2;
4058 	rp.rffid_fc4type = FC4_SCSI;
4059 	isp_put_rff_id(isp, &rp, (rff_id_t *)&scp[XTXOFF]);
4060 	if (isp->isp_dblev & ISP_LOGDEBUG1)
4061 		isp_print_bytes(isp, "CT request", sizeof(rft_id_t), &scp[XTXOFF]);
4062 
4063 	if (isp_ct_passthru(isp, chan, sizeof(rft_id_t), sizeof(ct_hdr_t))) {
4064 		FC_SCRATCH_RELEASE(isp, chan);
4065 		return (-1);
4066 	}
4067 
4068 	isp_get_ct_hdr(isp, (ct_hdr_t *) scp, ct);
4069 	FC_SCRATCH_RELEASE(isp, chan);
4070 	if (ct->ct_cmd_resp == LS_RJT) {
4071 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOG_WARN1,
4072 		    "Chan %d Register FC4 Features rejected", chan);
4073 		return (-1);
4074 	} else if (ct->ct_cmd_resp == LS_ACC) {
4075 		isp_prt(isp, ISP_LOG_SANCFG,
4076 		    "Chan %d Register FC4 Features accepted", chan);
4077 	} else {
4078 		isp_prt(isp, ISP_LOGWARN,
4079 		    "Chan %d Register FC4 Features: 0x%x", chan, ct->ct_cmd_resp);
4080 		return (-1);
4081 	}
4082 	return (0);
4083 }
4084 
4085 static int
4086 isp_register_port_name_24xx(ispsoftc_t *isp, int chan)
4087 {
4088 	fcparam *fcp = FCPARAM(isp, chan);
4089 	ct_hdr_t *ct;
4090 	rspn_id_t rp;
4091 	uint8_t *scp = fcp->isp_scratch;
4092 	int len;
4093 
4094 	if (FC_SCRATCH_ACQUIRE(isp, chan)) {
4095 		isp_prt(isp, ISP_LOGERR, sacq);
4096 		return (-1);
4097 	}
4098 
4099 	/*
4100 	 * Build the CT header and command in memory.
4101 	 */
4102 	ISP_MEMZERO(&rp, sizeof(rp));
4103 	ct = &rp.rspnid_hdr;
4104 	ct->ct_revision = CT_REVISION;
4105 	ct->ct_fcs_type = CT_FC_TYPE_FC;
4106 	ct->ct_fcs_subtype = CT_FC_SUBTYPE_NS;
4107 	ct->ct_cmd_resp = SNS_RSPN_ID;
4108 	rp.rspnid_portid[0] = fcp->isp_portid >> 16;
4109 	rp.rspnid_portid[1] = fcp->isp_portid >> 8;
4110 	rp.rspnid_portid[2] = fcp->isp_portid;
4111 	rp.rspnid_length = 0;
4112 	len = offsetof(rspn_id_t, rspnid_name);
4113 	mtx_lock(&prison0.pr_mtx);
4114 	rp.rspnid_length += sprintf(&scp[XTXOFF + len + rp.rspnid_length],
4115 	    "%s", prison0.pr_hostname[0] ? prison0.pr_hostname : "FreeBSD");
4116 	mtx_unlock(&prison0.pr_mtx);
4117 	rp.rspnid_length += sprintf(&scp[XTXOFF + len + rp.rspnid_length],
4118 	    ":%s", device_get_nameunit(isp->isp_dev));
4119 	if (chan != 0) {
4120 		rp.rspnid_length += sprintf(&scp[XTXOFF + len +
4121 		    rp.rspnid_length], "/%d", chan);
4122 	}
4123 	len += rp.rspnid_length;
4124 	ct->ct_bcnt_resid = (len - sizeof(ct_hdr_t)) >> 2;
4125 	isp_put_rspn_id(isp, &rp, (rspn_id_t *)&scp[XTXOFF]);
4126 	if (isp->isp_dblev & ISP_LOGDEBUG1)
4127 		isp_print_bytes(isp, "CT request", len, &scp[XTXOFF]);
4128 
4129 	if (isp_ct_passthru(isp, chan, len, sizeof(ct_hdr_t))) {
4130 		FC_SCRATCH_RELEASE(isp, chan);
4131 		return (-1);
4132 	}
4133 
4134 	isp_get_ct_hdr(isp, (ct_hdr_t *) scp, ct);
4135 	FC_SCRATCH_RELEASE(isp, chan);
4136 	if (ct->ct_cmd_resp == LS_RJT) {
4137 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOG_WARN1,
4138 		    "Chan %d Register Symbolic Port Name rejected", chan);
4139 		return (-1);
4140 	} else if (ct->ct_cmd_resp == LS_ACC) {
4141 		isp_prt(isp, ISP_LOG_SANCFG,
4142 		    "Chan %d Register Symbolic Port Name accepted", chan);
4143 	} else {
4144 		isp_prt(isp, ISP_LOGWARN,
4145 		    "Chan %d Register Symbolic Port Name: 0x%x", chan, ct->ct_cmd_resp);
4146 		return (-1);
4147 	}
4148 	return (0);
4149 }
4150 
4151 static int
4152 isp_register_node_name_24xx(ispsoftc_t *isp, int chan)
4153 {
4154 	fcparam *fcp = FCPARAM(isp, chan);
4155 	ct_hdr_t *ct;
4156 	rsnn_nn_t rp;
4157 	uint8_t *scp = fcp->isp_scratch;
4158 	int len;
4159 
4160 	if (FC_SCRATCH_ACQUIRE(isp, chan)) {
4161 		isp_prt(isp, ISP_LOGERR, sacq);
4162 		return (-1);
4163 	}
4164 
4165 	/*
4166 	 * Build the CT header and command in memory.
4167 	 */
4168 	ISP_MEMZERO(&rp, sizeof(rp));
4169 	ct = &rp.rsnnnn_hdr;
4170 	ct->ct_revision = CT_REVISION;
4171 	ct->ct_fcs_type = CT_FC_TYPE_FC;
4172 	ct->ct_fcs_subtype = CT_FC_SUBTYPE_NS;
4173 	ct->ct_cmd_resp = SNS_RSNN_NN;
4174 	MAKE_NODE_NAME_FROM_WWN(rp.rsnnnn_nodename, fcp->isp_wwnn);
4175 	rp.rsnnnn_length = 0;
4176 	len = offsetof(rsnn_nn_t, rsnnnn_name);
4177 	mtx_lock(&prison0.pr_mtx);
4178 	rp.rsnnnn_length += sprintf(&scp[XTXOFF + len + rp.rsnnnn_length],
4179 	    "%s", prison0.pr_hostname[0] ? prison0.pr_hostname : "FreeBSD");
4180 	mtx_unlock(&prison0.pr_mtx);
4181 	len += rp.rsnnnn_length;
4182 	ct->ct_bcnt_resid = (len - sizeof(ct_hdr_t)) >> 2;
4183 	isp_put_rsnn_nn(isp, &rp, (rsnn_nn_t *)&scp[XTXOFF]);
4184 	if (isp->isp_dblev & ISP_LOGDEBUG1)
4185 		isp_print_bytes(isp, "CT request", len, &scp[XTXOFF]);
4186 
4187 	if (isp_ct_passthru(isp, chan, len, sizeof(ct_hdr_t))) {
4188 		FC_SCRATCH_RELEASE(isp, chan);
4189 		return (-1);
4190 	}
4191 
4192 	isp_get_ct_hdr(isp, (ct_hdr_t *) scp, ct);
4193 	FC_SCRATCH_RELEASE(isp, chan);
4194 	if (ct->ct_cmd_resp == LS_RJT) {
4195 		isp_prt(isp, ISP_LOG_SANCFG|ISP_LOG_WARN1,
4196 		    "Chan %d Register Symbolic Node Name rejected", chan);
4197 		return (-1);
4198 	} else if (ct->ct_cmd_resp == LS_ACC) {
4199 		isp_prt(isp, ISP_LOG_SANCFG,
4200 		    "Chan %d Register Symbolic Node Name accepted", chan);
4201 	} else {
4202 		isp_prt(isp, ISP_LOGWARN,
4203 		    "Chan %d Register Symbolic Node Name: 0x%x", chan, ct->ct_cmd_resp);
4204 		return (-1);
4205 	}
4206 	return (0);
4207 }
4208 
4209 static uint16_t
4210 isp_next_handle(ispsoftc_t *isp, uint16_t *ohp)
4211 {
4212 	fcparam *fcp;
4213 	int i, chan, wrap;
4214 	uint16_t handle, minh, maxh;
4215 
4216 	handle = *ohp;
4217 	if (ISP_CAP_2KLOGIN(isp)) {
4218 		minh = 0;
4219 		maxh = NPH_RESERVED - 1;
4220 	} else {
4221 		minh = SNS_ID + 1;
4222 		maxh = NPH_MAX - 1;
4223 	}
4224 	wrap = 0;
4225 
4226 next:
4227 	if (handle == NIL_HANDLE) {
4228 		handle = minh;
4229 	} else {
4230 		handle++;
4231 		if (handle > maxh) {
4232 			if (++wrap >= 2) {
4233 				isp_prt(isp, ISP_LOGERR, "Out of port handles!");
4234 				return (NIL_HANDLE);
4235 			}
4236 			handle = minh;
4237 		}
4238 	}
4239 	for (chan = 0; chan < isp->isp_nchan; chan++) {
4240 		fcp = FCPARAM(isp, chan);
4241 		if (fcp->role == ISP_ROLE_NONE)
4242 			continue;
4243 		for (i = 0; i < MAX_FC_TARG; i++) {
4244 			if (fcp->portdb[i].state != FC_PORTDB_STATE_NIL &&
4245 			    fcp->portdb[i].handle == handle)
4246 				goto next;
4247 		}
4248 	}
4249 	*ohp = handle;
4250 	return (handle);
4251 }
4252 
4253 /*
4254  * Start a command. Locking is assumed done in the caller.
4255  */
4256 
4257 int
4258 isp_start(XS_T *xs)
4259 {
4260 	ispsoftc_t *isp;
4261 	uint32_t cdblen;
4262 	uint8_t local[QENTRY_LEN];
4263 	ispreq_t *reqp;
4264 	void *cdbp, *qep;
4265 	uint16_t *tptr;
4266 	fcportdb_t *lp;
4267 	int target, dmaresult;
4268 
4269 	XS_INITERR(xs);
4270 	isp = XS_ISP(xs);
4271 
4272 	/*
4273 	 * Check command CDB length, etc.. We really are limited to 16 bytes
4274 	 * for Fibre Channel, but can do up to 44 bytes in parallel SCSI,
4275 	 * but probably only if we're running fairly new firmware (we'll
4276 	 * let the old f/w choke on an extended command queue entry).
4277 	 */
4278 
4279 	if (XS_CDBLEN(xs) > (IS_FC(isp)? 16 : 44) || XS_CDBLEN(xs) == 0) {
4280 		isp_prt(isp, ISP_LOGERR, "unsupported cdb length (%d, CDB[0]=0x%x)", XS_CDBLEN(xs), XS_CDBP(xs)[0] & 0xff);
4281 		XS_SETERR(xs, HBA_BOTCH);
4282 		return (CMD_COMPLETE);
4283 	}
4284 
4285 	/*
4286 	 * Translate the target to device handle as appropriate, checking
4287 	 * for correct device state as well.
4288 	 */
4289 	target = XS_TGT(xs);
4290 	if (IS_FC(isp)) {
4291 		fcparam *fcp = FCPARAM(isp, XS_CHANNEL(xs));
4292 
4293 		if ((fcp->role & ISP_ROLE_INITIATOR) == 0) {
4294 			isp_prt(isp, ISP_LOG_WARN1,
4295 			    "%d.%d.%jx I am not an initiator",
4296 			    XS_CHANNEL(xs), target, (uintmax_t)XS_LUN(xs));
4297 			XS_SETERR(xs, HBA_SELTIMEOUT);
4298 			return (CMD_COMPLETE);
4299 		}
4300 
4301 		if (isp->isp_state != ISP_RUNSTATE) {
4302 			isp_prt(isp, ISP_LOGERR, "Adapter not at RUNSTATE");
4303 			XS_SETERR(xs, HBA_BOTCH);
4304 			return (CMD_COMPLETE);
4305 		}
4306 
4307 		/*
4308 		 * Try again later.
4309 		 */
4310 		if (fcp->isp_loopstate != LOOP_READY) {
4311 			return (CMD_RQLATER);
4312 		}
4313 
4314 		isp_prt(isp, ISP_LOGDEBUG2, "XS_TGT(xs)=%d", target);
4315 		lp = &fcp->portdb[target];
4316 		if (target < 0 || target >= MAX_FC_TARG ||
4317 		    lp->is_target == 0) {
4318 			XS_SETERR(xs, HBA_SELTIMEOUT);
4319 			return (CMD_COMPLETE);
4320 		}
4321 		if (lp->state == FC_PORTDB_STATE_ZOMBIE) {
4322 			isp_prt(isp, ISP_LOGDEBUG1,
4323 			    "%d.%d.%jx target zombie",
4324 			    XS_CHANNEL(xs), target, (uintmax_t)XS_LUN(xs));
4325 			return (CMD_RQLATER);
4326 		}
4327 		if (lp->state != FC_PORTDB_STATE_VALID) {
4328 			isp_prt(isp, ISP_LOGDEBUG1,
4329 			    "%d.%d.%jx bad db port state 0x%x",
4330 			    XS_CHANNEL(xs), target, (uintmax_t)XS_LUN(xs), lp->state);
4331 			XS_SETERR(xs, HBA_SELTIMEOUT);
4332 			return (CMD_COMPLETE);
4333 		}
4334 	} else {
4335 		sdparam *sdp = SDPARAM(isp, XS_CHANNEL(xs));
4336 		if (isp->isp_state != ISP_RUNSTATE) {
4337 			isp_prt(isp, ISP_LOGERR, "Adapter not at RUNSTATE");
4338 			XS_SETERR(xs, HBA_BOTCH);
4339 			return (CMD_COMPLETE);
4340 		}
4341 
4342 		if (sdp->update) {
4343 			isp_spi_update(isp, XS_CHANNEL(xs));
4344 		}
4345 		lp = NULL;
4346 	}
4347 
4348  start_again:
4349 
4350 	qep = isp_getrqentry(isp);
4351 	if (qep == NULL) {
4352 		isp_prt(isp, ISP_LOG_WARN1, "Request Queue Overflow");
4353 		XS_SETERR(xs, HBA_BOTCH);
4354 		return (CMD_EAGAIN);
4355 	}
4356 	XS_SETERR(xs, HBA_NOERROR);
4357 
4358 	/*
4359 	 * Now see if we need to synchronize the ISP with respect to anything.
4360 	 * We do dual duty here (cough) for synchronizing for busses other
4361 	 * than which we got here to send a command to.
4362 	 */
4363 	reqp = (ispreq_t *) local;
4364 	ISP_MEMZERO(local, QENTRY_LEN);
4365 	if (ISP_TST_SENDMARKER(isp, XS_CHANNEL(xs))) {
4366 		if (IS_24XX(isp)) {
4367 			isp_marker_24xx_t *m = (isp_marker_24xx_t *) reqp;
4368 			m->mrk_header.rqs_entry_count = 1;
4369 			m->mrk_header.rqs_entry_type = RQSTYPE_MARKER;
4370 			m->mrk_modifier = SYNC_ALL;
4371 			m->mrk_vphdl = XS_CHANNEL(xs);
4372 			isp_put_marker_24xx(isp, m, qep);
4373 		} else {
4374 			isp_marker_t *m = (isp_marker_t *) reqp;
4375 			m->mrk_header.rqs_entry_count = 1;
4376 			m->mrk_header.rqs_entry_type = RQSTYPE_MARKER;
4377 			m->mrk_target = (XS_CHANNEL(xs) << 7);	/* bus # */
4378 			m->mrk_modifier = SYNC_ALL;
4379 			isp_put_marker(isp, m, qep);
4380 		}
4381 		ISP_SYNC_REQUEST(isp);
4382 		ISP_SET_SENDMARKER(isp, XS_CHANNEL(xs), 0);
4383 		goto start_again;
4384 	}
4385 
4386 	reqp->req_header.rqs_entry_count = 1;
4387 
4388 	/*
4389 	 * Select and install Header Code.
4390 	 * Note that it might be overridden before going out
4391 	 * if we're on a 64 bit platform. The lower level
4392 	 * code (isp_send_cmd) will select the appropriate
4393 	 * 64 bit variant if it needs to.
4394 	 */
4395 	if (IS_24XX(isp)) {
4396 		reqp->req_header.rqs_entry_type = RQSTYPE_T7RQS;
4397 	} else if (IS_FC(isp)) {
4398 		reqp->req_header.rqs_entry_type = RQSTYPE_T2RQS;
4399 	} else {
4400 		if (XS_CDBLEN(xs) > 12) {
4401 			reqp->req_header.rqs_entry_type = RQSTYPE_CMDONLY;
4402 		} else {
4403 			reqp->req_header.rqs_entry_type = RQSTYPE_REQUEST;
4404 		}
4405 	}
4406 
4407 	/*
4408 	 * Set task attributes
4409 	 */
4410 	if (IS_24XX(isp)) {
4411 		int ttype;
4412 		if (XS_TAG_P(xs)) {
4413 			ttype = XS_TAG_TYPE(xs);
4414 		} else {
4415 			if (XS_CDBP(xs)[0] == 0x3) {
4416 				ttype = REQFLAG_HTAG;
4417 			} else {
4418 				ttype = REQFLAG_STAG;
4419 			}
4420 		}
4421 		if (ttype == REQFLAG_OTAG) {
4422 			ttype = FCP_CMND_TASK_ATTR_ORDERED;
4423 		} else if (ttype == REQFLAG_HTAG) {
4424 			ttype = FCP_CMND_TASK_ATTR_HEAD;
4425 		} else {
4426 			ttype = FCP_CMND_TASK_ATTR_SIMPLE;
4427 		}
4428 		((ispreqt7_t *)reqp)->req_task_attribute = ttype;
4429 	} else if (IS_FC(isp)) {
4430 		/*
4431 		 * See comment in isp_intr
4432 		 */
4433 		/* XS_SET_RESID(xs, 0); */
4434 
4435 		/*
4436 		 * Fibre Channel always requires some kind of tag.
4437 		 * The Qlogic drivers seem be happy not to use a tag,
4438 		 * but this breaks for some devices (IBM drives).
4439 		 */
4440 		if (XS_TAG_P(xs)) {
4441 			((ispreqt2_t *)reqp)->req_flags = XS_TAG_TYPE(xs);
4442 		} else {
4443 			/*
4444 			 * If we don't know what tag to use, use HEAD OF QUEUE
4445 			 * for Request Sense or Simple.
4446 			 */
4447 			if (XS_CDBP(xs)[0] == 0x3)	/* REQUEST SENSE */
4448 				((ispreqt2_t *)reqp)->req_flags = REQFLAG_HTAG;
4449 			else
4450 				((ispreqt2_t *)reqp)->req_flags = REQFLAG_STAG;
4451 		}
4452 	} else {
4453 		sdparam *sdp = SDPARAM(isp, XS_CHANNEL(xs));
4454 		if ((sdp->isp_devparam[target].actv_flags & DPARM_TQING) && XS_TAG_P(xs)) {
4455 			reqp->req_flags = XS_TAG_TYPE(xs);
4456 		}
4457 	}
4458 
4459 	tptr = &reqp->req_time;
4460 
4461 	/*
4462 	 * NB: we do not support long CDBs (yet)
4463 	 */
4464 	cdblen = XS_CDBLEN(xs);
4465 
4466 	if (IS_SCSI(isp)) {
4467 		if (cdblen > sizeof (reqp->req_cdb)) {
4468 			isp_prt(isp, ISP_LOGERR, "Command Length %u too long for this chip", cdblen);
4469 			XS_SETERR(xs, HBA_BOTCH);
4470 			return (CMD_COMPLETE);
4471 		}
4472 		reqp->req_target = target | (XS_CHANNEL(xs) << 7);
4473 		reqp->req_lun_trn = XS_LUN(xs);
4474 		cdbp = reqp->req_cdb;
4475 		reqp->req_cdblen = cdblen;
4476 	} else if (IS_24XX(isp)) {
4477 		ispreqt7_t *t7 = (ispreqt7_t *)local;
4478 
4479 		if (cdblen > sizeof (t7->req_cdb)) {
4480 			isp_prt(isp, ISP_LOGERR, "Command Length %u too long for this chip", cdblen);
4481 			XS_SETERR(xs, HBA_BOTCH);
4482 			return (CMD_COMPLETE);
4483 		}
4484 
4485 		t7->req_nphdl = lp->handle;
4486 		t7->req_tidlo = lp->portid;
4487 		t7->req_tidhi = lp->portid >> 16;
4488 		t7->req_vpidx = ISP_GET_VPIDX(isp, XS_CHANNEL(xs));
4489 #if __FreeBSD_version >= 1000700
4490 		be64enc(t7->req_lun, CAM_EXTLUN_BYTE_SWIZZLE(XS_LUN(xs)));
4491 #else
4492 		if (XS_LUN(xs) >= 256) {
4493 			t7->req_lun[0] = XS_LUN(xs) >> 8;
4494 			t7->req_lun[0] |= 0x40;
4495 		}
4496 		t7->req_lun[1] = XS_LUN(xs);
4497 #endif
4498 		if (FCPARAM(isp, XS_CHANNEL(xs))->fctape_enabled && (lp->prli_word3 & PRLI_WD3_RETRY)) {
4499 			if (FCP_NEXT_CRN(isp, &t7->req_crn, xs)) {
4500 				isp_prt(isp, ISP_LOG_WARN1,
4501 				    "%d.%d.%jx cannot generate next CRN",
4502 				    XS_CHANNEL(xs), target, (uintmax_t)XS_LUN(xs));
4503 				XS_SETERR(xs, HBA_BOTCH);
4504 				return (CMD_EAGAIN);
4505 			}
4506 		}
4507 		tptr = &t7->req_time;
4508 		cdbp = t7->req_cdb;
4509 	} else {
4510 		ispreqt2_t *t2 = (ispreqt2_t *)local;
4511 
4512 		if (cdblen > sizeof t2->req_cdb) {
4513 			isp_prt(isp, ISP_LOGERR, "Command Length %u too long for this chip", cdblen);
4514 			XS_SETERR(xs, HBA_BOTCH);
4515 			return (CMD_COMPLETE);
4516 		}
4517 		if (FCPARAM(isp, XS_CHANNEL(xs))->fctape_enabled && (lp->prli_word3 & PRLI_WD3_RETRY)) {
4518 			if (FCP_NEXT_CRN(isp, &t2->req_crn, xs)) {
4519 				isp_prt(isp, ISP_LOG_WARN1,
4520 				    "%d.%d.%jx cannot generate next CRN",
4521 				    XS_CHANNEL(xs), target, (uintmax_t)XS_LUN(xs));
4522 				XS_SETERR(xs, HBA_BOTCH);
4523 				return (CMD_EAGAIN);
4524 			}
4525 		}
4526 		if (ISP_CAP_2KLOGIN(isp)) {
4527 			ispreqt2e_t *t2e = (ispreqt2e_t *)local;
4528 			t2e->req_target = lp->handle;
4529 			t2e->req_scclun = XS_LUN(xs);
4530 #if __FreeBSD_version < 1000700
4531 			if (XS_LUN(xs) >= 256)
4532 				t2e->req_scclun |= 0x4000;
4533 #endif
4534 			cdbp = t2e->req_cdb;
4535 		} else if (ISP_CAP_SCCFW(isp)) {
4536 			ispreqt2_t *t2 = (ispreqt2_t *)local;
4537 			t2->req_target = lp->handle;
4538 			t2->req_scclun = XS_LUN(xs);
4539 #if __FreeBSD_version < 1000700
4540 			if (XS_LUN(xs) >= 256)
4541 				t2->req_scclun |= 0x4000;
4542 #endif
4543 			cdbp = t2->req_cdb;
4544 		} else {
4545 			t2->req_target = lp->handle;
4546 			t2->req_lun_trn = XS_LUN(xs);
4547 			cdbp = t2->req_cdb;
4548 		}
4549 	}
4550 	ISP_MEMCPY(cdbp, XS_CDBP(xs), cdblen);
4551 
4552 	*tptr = (XS_TIME(xs) + 999) / 1000;
4553 	if (IS_24XX(isp) && *tptr > 0x1999) {
4554 		*tptr = 0x1999;
4555 	}
4556 
4557 	/* Whew. Thankfully the same for type 7 requests */
4558 	reqp->req_handle = isp_allocate_handle(isp, xs, ISP_HANDLE_INITIATOR);
4559 	if (reqp->req_handle == 0) {
4560 		isp_prt(isp, ISP_LOG_WARN1, "out of xflist pointers");
4561 		XS_SETERR(xs, HBA_BOTCH);
4562 		return (CMD_EAGAIN);
4563 	}
4564 
4565 	/*
4566 	 * Set up DMA and/or do any platform dependent swizzling of the request entry
4567 	 * so that the Qlogic F/W understands what is being asked of it.
4568 	 *
4569 	 * The callee is responsible for adding all requests at this point.
4570 	 */
4571 	dmaresult = ISP_DMASETUP(isp, xs, reqp);
4572 	if (dmaresult != CMD_QUEUED) {
4573 		isp_destroy_handle(isp, reqp->req_handle);
4574 		/*
4575 		 * dmasetup sets actual error in packet, and
4576 		 * return what we were given to return.
4577 		 */
4578 		return (dmaresult);
4579 	}
4580 	isp_xs_prt(isp, xs, ISP_LOGDEBUG0, "START cmd cdb[0]=0x%x datalen %ld", XS_CDBP(xs)[0], (long) XS_XFRLEN(xs));
4581 	isp->isp_nactive++;
4582 	return (CMD_QUEUED);
4583 }
4584 
4585 /*
4586  * isp control
4587  * Locks (ints blocked) assumed held.
4588  */
4589 
4590 int
4591 isp_control(ispsoftc_t *isp, ispctl_t ctl, ...)
4592 {
4593 	XS_T *xs;
4594 	mbreg_t *mbr, mbs;
4595 	int chan, tgt;
4596 	uint32_t handle;
4597 	va_list ap;
4598 
4599 	switch (ctl) {
4600 	case ISPCTL_RESET_BUS:
4601 		/*
4602 		 * Issue a bus reset.
4603 		 */
4604 		if (IS_24XX(isp)) {
4605 			isp_prt(isp, ISP_LOGERR, "BUS RESET NOT IMPLEMENTED");
4606 			break;
4607 		} else if (IS_FC(isp)) {
4608 			mbs.param[1] = 10;
4609 			chan = 0;
4610 		} else {
4611 			va_start(ap, ctl);
4612 			chan = va_arg(ap, int);
4613 			va_end(ap);
4614 			mbs.param[1] = SDPARAM(isp, chan)->isp_bus_reset_delay;
4615 			if (mbs.param[1] < 2) {
4616 				mbs.param[1] = 2;
4617 			}
4618 			mbs.param[2] = chan;
4619 		}
4620 		MBSINIT(&mbs, MBOX_BUS_RESET, MBLOGALL, 0);
4621 		ISP_SET_SENDMARKER(isp, chan, 1);
4622 		isp_mboxcmd(isp, &mbs);
4623 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
4624 			break;
4625 		}
4626 		isp_prt(isp, ISP_LOGINFO, "driver initiated bus reset of bus %d", chan);
4627 		return (0);
4628 
4629 	case ISPCTL_RESET_DEV:
4630 		va_start(ap, ctl);
4631 		chan = va_arg(ap, int);
4632 		tgt = va_arg(ap, int);
4633 		va_end(ap);
4634 		if (IS_24XX(isp)) {
4635 			uint8_t local[QENTRY_LEN];
4636 			isp24xx_tmf_t *tmf;
4637 			isp24xx_statusreq_t *sp;
4638 			fcparam *fcp = FCPARAM(isp, chan);
4639 			fcportdb_t *lp;
4640 
4641 			if (tgt < 0 || tgt >= MAX_FC_TARG) {
4642 				isp_prt(isp, ISP_LOGWARN, "Chan %d trying to reset bad target %d", chan, tgt);
4643 				break;
4644 			}
4645 			lp = &fcp->portdb[tgt];
4646 			if (lp->is_target == 0 ||
4647 			    lp->state != FC_PORTDB_STATE_VALID) {
4648 				isp_prt(isp, ISP_LOGWARN, "Chan %d abort of no longer valid target %d", chan, tgt);
4649 				break;
4650 			}
4651 
4652 			tmf = (isp24xx_tmf_t *) local;
4653 			ISP_MEMZERO(tmf, QENTRY_LEN);
4654 			tmf->tmf_header.rqs_entry_type = RQSTYPE_TSK_MGMT;
4655 			tmf->tmf_header.rqs_entry_count = 1;
4656 			tmf->tmf_nphdl = lp->handle;
4657 			tmf->tmf_delay = 2;
4658 			tmf->tmf_timeout = 4;
4659 			tmf->tmf_flags = ISP24XX_TMF_TARGET_RESET;
4660 			tmf->tmf_tidlo = lp->portid;
4661 			tmf->tmf_tidhi = lp->portid >> 16;
4662 			tmf->tmf_vpidx = ISP_GET_VPIDX(isp, chan);
4663 			isp_put_24xx_tmf(isp, tmf, isp->isp_iocb);
4664 			MEMORYBARRIER(isp, SYNC_IFORDEV, 0, QENTRY_LEN, chan);
4665 			fcp->sendmarker = 1;
4666 
4667 			isp_prt(isp, ISP_LOGALL, "Chan %d Reset N-Port Handle 0x%04x @ Port 0x%06x", chan, lp->handle, lp->portid);
4668 			MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL,
4669 			    MBCMD_DEFAULT_TIMEOUT + tmf->tmf_timeout * 1000000);
4670 			mbs.param[1] = QENTRY_LEN;
4671 			mbs.param[2] = DMA_WD1(isp->isp_iocb_dma);
4672 			mbs.param[3] = DMA_WD0(isp->isp_iocb_dma);
4673 			mbs.param[6] = DMA_WD3(isp->isp_iocb_dma);
4674 			mbs.param[7] = DMA_WD2(isp->isp_iocb_dma);
4675 			isp_mboxcmd(isp, &mbs);
4676 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE)
4677 				break;
4678 
4679 			MEMORYBARRIER(isp, SYNC_IFORCPU, QENTRY_LEN, QENTRY_LEN, chan);
4680 			sp = (isp24xx_statusreq_t *) local;
4681 			isp_get_24xx_response(isp, &((isp24xx_statusreq_t *)isp->isp_iocb)[1], sp);
4682 			if (sp->req_completion_status == 0) {
4683 				return (0);
4684 			}
4685 			isp_prt(isp, ISP_LOGWARN, "Chan %d reset of target %d returned 0x%x", chan, tgt, sp->req_completion_status);
4686 			break;
4687 		} else if (IS_FC(isp)) {
4688 			if (ISP_CAP_2KLOGIN(isp)) {
4689 				mbs.param[1] = tgt;
4690 				mbs.ibits = (1 << 10);
4691 			} else {
4692 				mbs.param[1] = (tgt << 8);
4693 			}
4694 		} else {
4695 			mbs.param[1] = (chan << 15) | (tgt << 8);
4696 		}
4697 		MBSINIT(&mbs, MBOX_ABORT_TARGET, MBLOGALL, 0);
4698 		mbs.param[2] = 3;	/* 'delay', in seconds */
4699 		isp_mboxcmd(isp, &mbs);
4700 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
4701 			break;
4702 		}
4703 		isp_prt(isp, ISP_LOGINFO, "Target %d on Bus %d Reset Succeeded", tgt, chan);
4704 		ISP_SET_SENDMARKER(isp, chan, 1);
4705 		return (0);
4706 
4707 	case ISPCTL_ABORT_CMD:
4708 		va_start(ap, ctl);
4709 		xs = va_arg(ap, XS_T *);
4710 		va_end(ap);
4711 
4712 		tgt = XS_TGT(xs);
4713 		chan = XS_CHANNEL(xs);
4714 
4715 		handle = isp_find_handle(isp, xs);
4716 		if (handle == 0) {
4717 			isp_prt(isp, ISP_LOGWARN, "cannot find handle for command to abort");
4718 			break;
4719 		}
4720 		if (IS_24XX(isp)) {
4721 			isp24xx_abrt_t local, *ab = &local;
4722 			fcparam *fcp;
4723 			fcportdb_t *lp;
4724 
4725 			fcp = FCPARAM(isp, chan);
4726 			if (tgt < 0 || tgt >= MAX_FC_TARG) {
4727 				isp_prt(isp, ISP_LOGWARN, "Chan %d trying to abort bad target %d", chan, tgt);
4728 				break;
4729 			}
4730 			lp = &fcp->portdb[tgt];
4731 			if (lp->is_target == 0 ||
4732 			    lp->state != FC_PORTDB_STATE_VALID) {
4733 				isp_prt(isp, ISP_LOGWARN, "Chan %d abort of no longer valid target %d", chan, tgt);
4734 				break;
4735 			}
4736 			isp_prt(isp, ISP_LOGALL, "Chan %d Abort Cmd for N-Port 0x%04x @ Port 0x%06x", chan, lp->handle, lp->portid);
4737 			ISP_MEMZERO(ab, QENTRY_LEN);
4738 			ab->abrt_header.rqs_entry_type = RQSTYPE_ABORT_IO;
4739 			ab->abrt_header.rqs_entry_count = 1;
4740 			ab->abrt_handle = lp->handle;
4741 			ab->abrt_cmd_handle = handle;
4742 			ab->abrt_tidlo = lp->portid;
4743 			ab->abrt_tidhi = lp->portid >> 16;
4744 			ab->abrt_vpidx = ISP_GET_VPIDX(isp, chan);
4745 			isp_put_24xx_abrt(isp, ab, isp->isp_iocb);
4746 			MEMORYBARRIER(isp, SYNC_IFORDEV, 0, 2 * QENTRY_LEN, chan);
4747 
4748 			ISP_MEMZERO(&mbs, sizeof (mbs));
4749 			MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 5000000);
4750 			mbs.param[1] = QENTRY_LEN;
4751 			mbs.param[2] = DMA_WD1(isp->isp_iocb_dma);
4752 			mbs.param[3] = DMA_WD0(isp->isp_iocb_dma);
4753 			mbs.param[6] = DMA_WD3(isp->isp_iocb_dma);
4754 			mbs.param[7] = DMA_WD2(isp->isp_iocb_dma);
4755 
4756 			isp_mboxcmd(isp, &mbs);
4757 			if (mbs.param[0] != MBOX_COMMAND_COMPLETE)
4758 				break;
4759 
4760 			MEMORYBARRIER(isp, SYNC_IFORCPU, QENTRY_LEN, QENTRY_LEN, chan);
4761 			isp_get_24xx_abrt(isp, &((isp24xx_abrt_t *)isp->isp_iocb)[1], ab);
4762 			if (ab->abrt_nphdl == ISP24XX_ABRT_OKAY) {
4763 				return (0);
4764 			}
4765 			isp_prt(isp, ISP_LOGWARN, "Chan %d handle %d abort returned 0x%x", chan, tgt, ab->abrt_nphdl);
4766 			break;
4767 		} else if (IS_FC(isp)) {
4768 			if (ISP_CAP_SCCFW(isp)) {
4769 				if (ISP_CAP_2KLOGIN(isp)) {
4770 					mbs.param[1] = tgt;
4771 				} else {
4772 					mbs.param[1] = tgt << 8;
4773 				}
4774 				mbs.param[6] = XS_LUN(xs);
4775 			} else {
4776 				mbs.param[1] = tgt << 8 | XS_LUN(xs);
4777 			}
4778 		} else {
4779 			mbs.param[1] = (chan << 15) | (tgt << 8) | XS_LUN(xs);
4780 		}
4781 		MBSINIT(&mbs, MBOX_ABORT,
4782 		    MBLOGALL & ~MBLOGMASK(MBOX_COMMAND_ERROR), 0);
4783 		mbs.param[2] = handle;
4784 		isp_mboxcmd(isp, &mbs);
4785 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
4786 			break;
4787 		}
4788 		return (0);
4789 
4790 	case ISPCTL_UPDATE_PARAMS:
4791 
4792 		va_start(ap, ctl);
4793 		chan = va_arg(ap, int);
4794 		va_end(ap);
4795 		isp_spi_update(isp, chan);
4796 		return (0);
4797 
4798 	case ISPCTL_FCLINK_TEST:
4799 
4800 		if (IS_FC(isp)) {
4801 			int usdelay;
4802 			va_start(ap, ctl);
4803 			chan = va_arg(ap, int);
4804 			usdelay = va_arg(ap, int);
4805 			va_end(ap);
4806 			if (usdelay == 0) {
4807 				usdelay =  250000;
4808 			}
4809 			return (isp_fclink_test(isp, chan, usdelay));
4810 		}
4811 		break;
4812 
4813 	case ISPCTL_SCAN_FABRIC:
4814 
4815 		if (IS_FC(isp)) {
4816 			va_start(ap, ctl);
4817 			chan = va_arg(ap, int);
4818 			va_end(ap);
4819 			return (isp_scan_fabric(isp, chan));
4820 		}
4821 		break;
4822 
4823 	case ISPCTL_SCAN_LOOP:
4824 
4825 		if (IS_FC(isp)) {
4826 			va_start(ap, ctl);
4827 			chan = va_arg(ap, int);
4828 			va_end(ap);
4829 			return (isp_scan_loop(isp, chan));
4830 		}
4831 		break;
4832 
4833 	case ISPCTL_PDB_SYNC:
4834 
4835 		if (IS_FC(isp)) {
4836 			va_start(ap, ctl);
4837 			chan = va_arg(ap, int);
4838 			va_end(ap);
4839 			return (isp_pdb_sync(isp, chan));
4840 		}
4841 		break;
4842 
4843 	case ISPCTL_SEND_LIP:
4844 
4845 		if (IS_FC(isp) && !IS_24XX(isp)) {
4846 			MBSINIT(&mbs, MBOX_INIT_LIP, MBLOGALL, 0);
4847 			if (ISP_CAP_2KLOGIN(isp)) {
4848 				mbs.ibits = (1 << 10);
4849 			}
4850 			isp_mboxcmd(isp, &mbs);
4851 			if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
4852 				return (0);
4853 			}
4854 		}
4855 		break;
4856 
4857 	case ISPCTL_GET_PDB:
4858 		if (IS_FC(isp)) {
4859 			isp_pdb_t *pdb;
4860 			va_start(ap, ctl);
4861 			chan = va_arg(ap, int);
4862 			tgt = va_arg(ap, int);
4863 			pdb = va_arg(ap, isp_pdb_t *);
4864 			va_end(ap);
4865 			return (isp_getpdb(isp, chan, tgt, pdb));
4866 		}
4867 		break;
4868 
4869 	case ISPCTL_GET_NAMES:
4870 	{
4871 		uint64_t *wwnn, *wwnp;
4872 		va_start(ap, ctl);
4873 		chan = va_arg(ap, int);
4874 		tgt = va_arg(ap, int);
4875 		wwnn = va_arg(ap, uint64_t *);
4876 		wwnp = va_arg(ap, uint64_t *);
4877 		va_end(ap);
4878 		if (wwnn == NULL && wwnp == NULL) {
4879 			break;
4880 		}
4881 		if (wwnn) {
4882 			*wwnn = isp_get_wwn(isp, chan, tgt, 1);
4883 			if (*wwnn == INI_NONE) {
4884 				break;
4885 			}
4886 		}
4887 		if (wwnp) {
4888 			*wwnp = isp_get_wwn(isp, chan, tgt, 0);
4889 			if (*wwnp == INI_NONE) {
4890 				break;
4891 			}
4892 		}
4893 		return (0);
4894 	}
4895 	case ISPCTL_RUN_MBOXCMD:
4896 	{
4897 		va_start(ap, ctl);
4898 		mbr = va_arg(ap, mbreg_t *);
4899 		va_end(ap);
4900 		isp_mboxcmd(isp, mbr);
4901 		return (0);
4902 	}
4903 	case ISPCTL_PLOGX:
4904 	{
4905 		isp_plcmd_t *p;
4906 		int r;
4907 
4908 		va_start(ap, ctl);
4909 		p = va_arg(ap, isp_plcmd_t *);
4910 		va_end(ap);
4911 
4912 		if ((p->flags & PLOGX_FLG_CMD_MASK) != PLOGX_FLG_CMD_PLOGI || (p->handle != NIL_HANDLE)) {
4913 			return (isp_plogx(isp, p->channel, p->handle, p->portid, p->flags));
4914 		}
4915 		do {
4916 			isp_next_handle(isp, &p->handle);
4917 			r = isp_plogx(isp, p->channel, p->handle, p->portid, p->flags);
4918 			if ((r & 0xffff) == MBOX_PORT_ID_USED) {
4919 				p->handle = r >> 16;
4920 				r = 0;
4921 				break;
4922 			}
4923 		} while ((r & 0xffff) == MBOX_LOOP_ID_USED);
4924 		return (r);
4925 	}
4926 	case ISPCTL_CHANGE_ROLE:
4927 		if (IS_FC(isp)) {
4928 			int role, r;
4929 
4930 			va_start(ap, ctl);
4931 			chan = va_arg(ap, int);
4932 			role = va_arg(ap, int);
4933 			va_end(ap);
4934 			r = isp_fc_change_role(isp, chan, role);
4935 			return (r);
4936 		}
4937 		break;
4938 	default:
4939 		isp_prt(isp, ISP_LOGERR, "Unknown Control Opcode 0x%x", ctl);
4940 		break;
4941 
4942 	}
4943 	return (-1);
4944 }
4945 
4946 /*
4947  * Interrupt Service Routine(s).
4948  *
4949  * External (OS) framework has done the appropriate locking,
4950  * and the locking will be held throughout this function.
4951  */
4952 
4953 /*
4954  * Limit our stack depth by sticking with the max likely number
4955  * of completions on a request queue at any one time.
4956  */
4957 #ifndef	MAX_REQUESTQ_COMPLETIONS
4958 #define	MAX_REQUESTQ_COMPLETIONS	32
4959 #endif
4960 
4961 void
4962 isp_intr(ispsoftc_t *isp, uint16_t isr, uint16_t sema, uint16_t info)
4963 {
4964 	XS_T *complist[MAX_REQUESTQ_COMPLETIONS], *xs;
4965 	uint32_t iptr, optr, junk;
4966 	int i, nlooked = 0, ndone = 0, continuations_expected = 0;
4967 	int etype, last_etype = 0;
4968 
4969 again:
4970 	/*
4971 	 * Is this a mailbox related interrupt?
4972 	 * The mailbox semaphore will be nonzero if so.
4973 	 */
4974 	if (sema) {
4975  fmbox:
4976 		if (info & MBOX_COMMAND_COMPLETE) {
4977 			isp->isp_intmboxc++;
4978 			if (isp->isp_mboxbsy) {
4979 				int obits = isp->isp_obits;
4980 				isp->isp_mboxtmp[0] = info;
4981 				for (i = 1; i < ISP_NMBOX(isp); i++) {
4982 					if ((obits & (1 << i)) == 0) {
4983 						continue;
4984 					}
4985 					isp->isp_mboxtmp[i] = ISP_READ(isp, MBOX_OFF(i));
4986 				}
4987 				if (isp->isp_mbxwrk0) {
4988 					if (isp_mbox_continue(isp) == 0) {
4989 						return;
4990 					}
4991 				}
4992 				MBOX_NOTIFY_COMPLETE(isp);
4993 			} else {
4994 				isp_prt(isp, ISP_LOGWARN, "mailbox cmd (0x%x) with no waiters", info);
4995 			}
4996 		} else {
4997 			i = IS_FC(isp)? isp_parse_async_fc(isp, info) : isp_parse_async(isp, info);
4998 			if (i < 0) {
4999 				return;
5000 			}
5001 		}
5002 		if ((IS_FC(isp) && info != ASYNC_RIOZIO_STALL) || isp->isp_state != ISP_RUNSTATE) {
5003 			goto out;
5004 		}
5005 	}
5006 
5007 	/*
5008 	 * We can't be getting this now.
5009 	 */
5010 	if (isp->isp_state != ISP_RUNSTATE) {
5011 		/*
5012 		 * This seems to happen to 23XX and 24XX cards- don't know why.
5013 		 */
5014 		 if (isp->isp_mboxbsy && isp->isp_lastmbxcmd == MBOX_ABOUT_FIRMWARE) {
5015 			goto fmbox;
5016 		}
5017 		isp_prt(isp, ISP_LOGINFO, "interrupt (ISR=%x SEMA=%x INFO=%x) "
5018 		    "when not ready", isr, sema, info);
5019 		/*
5020 		 * Thank you very much!  *Burrrp*!
5021 		 */
5022 		isp->isp_residx = ISP_READ(isp, isp->isp_respinrp);
5023 		isp->isp_resodx = isp->isp_residx;
5024 		ISP_WRITE(isp, isp->isp_respoutrp, isp->isp_resodx);
5025 		if (IS_24XX(isp)) {
5026 			ISP_DISABLE_INTS(isp);
5027 		}
5028 		goto out;
5029 	}
5030 
5031 #ifdef	ISP_TARGET_MODE
5032 	/*
5033 	 * Check for ATIO Queue entries.
5034 	 */
5035 	if (IS_24XX(isp) &&
5036 	    (isr == ISPR2HST_ATIO_UPDATE || isr == ISPR2HST_ATIO_RSPQ_UPDATE ||
5037 	     isr == ISPR2HST_ATIO_UPDATE2)) {
5038 		iptr = ISP_READ(isp, BIU2400_ATIO_RSPINP);
5039 		optr = isp->isp_atioodx;
5040 
5041 		while (optr != iptr) {
5042 			uint8_t qe[QENTRY_LEN];
5043 			isphdr_t *hp;
5044 			uint32_t oop;
5045 			void *addr;
5046 
5047 			oop = optr;
5048 			MEMORYBARRIER(isp, SYNC_ATIOQ, oop, QENTRY_LEN, -1);
5049 			addr = ISP_QUEUE_ENTRY(isp->isp_atioq, oop);
5050 			isp_get_hdr(isp, addr, (isphdr_t *)qe);
5051 			hp = (isphdr_t *)qe;
5052 			switch (hp->rqs_entry_type) {
5053 			case RQSTYPE_NOTIFY:
5054 			case RQSTYPE_ATIO:
5055 				(void) isp_target_notify(isp, addr, &oop);
5056 				break;
5057 			default:
5058 				isp_print_qentry(isp, "?ATIOQ entry?", oop, addr);
5059 				break;
5060 			}
5061 			optr = ISP_NXT_QENTRY(oop, RESULT_QUEUE_LEN(isp));
5062 		}
5063 		if (isp->isp_atioodx != optr) {
5064 			ISP_WRITE(isp, BIU2400_ATIO_RSPOUTP, optr);
5065 			isp->isp_atioodx = optr;
5066 		}
5067 	}
5068 #endif
5069 
5070 	/*
5071 	 * You *must* read the Response Queue In Pointer
5072 	 * prior to clearing the RISC interrupt.
5073 	 *
5074 	 * Debounce the 2300 if revision less than 2.
5075 	 */
5076 	if (IS_2100(isp) || (IS_2300(isp) && isp->isp_revision < 2)) {
5077 		i = 0;
5078 		do {
5079 			iptr = ISP_READ(isp, isp->isp_respinrp);
5080 			junk = ISP_READ(isp, isp->isp_respinrp);
5081 		} while (junk != iptr && ++i < 1000);
5082 
5083 		if (iptr != junk) {
5084 			isp_prt(isp, ISP_LOGWARN, "Response Queue Out Pointer Unstable (%x, %x)", iptr, junk);
5085 			goto out;
5086 		}
5087 	} else {
5088 		iptr = ISP_READ(isp, isp->isp_respinrp);
5089 	}
5090 
5091 	optr = isp->isp_resodx;
5092 	if (optr == iptr && sema == 0) {
5093 		/*
5094 		 * There are a lot of these- reasons unknown- mostly on
5095 		 * faster Alpha machines.
5096 		 *
5097 		 * I tried delaying after writing HCCR_CMD_CLEAR_RISC_INT to
5098 		 * make sure the old interrupt went away (to avoid 'ringing'
5099 		 * effects), but that didn't stop this from occurring.
5100 		 */
5101 		if (IS_24XX(isp)) {
5102 			junk = 0;
5103 		} else if (IS_23XX(isp)) {
5104 			ISP_DELAY(100);
5105 			iptr = ISP_READ(isp, isp->isp_respinrp);
5106 			junk = ISP_READ(isp, BIU_R2HSTSLO);
5107 		} else {
5108 			junk = ISP_READ(isp, BIU_ISR);
5109 		}
5110 		if (optr == iptr) {
5111 			if (IS_23XX(isp) || IS_24XX(isp)) {
5112 				;
5113 			} else {
5114 				sema = ISP_READ(isp, BIU_SEMA);
5115 				info = ISP_READ(isp, OUTMAILBOX0);
5116 				if ((sema & 0x3) && (info & 0x8000)) {
5117 					goto again;
5118 				}
5119 			}
5120 			isp->isp_intbogus++;
5121 			isp_prt(isp, ISP_LOGDEBUG1, "bogus intr- isr %x (%x) iptr %x optr %x", isr, junk, iptr, optr);
5122 		}
5123 	}
5124 	isp->isp_residx = iptr;
5125 
5126 	while (optr != iptr) {
5127 		uint8_t qe[QENTRY_LEN];
5128 		ispstatusreq_t *sp = (ispstatusreq_t *) qe;
5129 		isphdr_t *hp;
5130 		int buddaboom, scsi_status, completion_status;
5131 		int req_status_flags, req_state_flags;
5132 		uint8_t *snsp, *resp;
5133 		uint32_t rlen, slen, totslen;
5134 		long resid;
5135 		uint16_t oop;
5136 
5137 		hp = (isphdr_t *) ISP_QUEUE_ENTRY(isp->isp_result, optr);
5138 		oop = optr;
5139 		optr = ISP_NXT_QENTRY(optr, RESULT_QUEUE_LEN(isp));
5140 		nlooked++;
5141  read_again:
5142 		buddaboom = req_status_flags = req_state_flags = 0;
5143 		resid = 0L;
5144 
5145 		/*
5146 		 * Synchronize our view of this response queue entry.
5147 		 */
5148 		MEMORYBARRIER(isp, SYNC_RESULT, oop, QENTRY_LEN, -1);
5149 		if (isp->isp_dblev & ISP_LOGDEBUG1)
5150 			isp_print_qentry(isp, "Response Queue Entry", oop, hp);
5151 		isp_get_hdr(isp, hp, &sp->req_header);
5152 		etype = sp->req_header.rqs_entry_type;
5153 
5154 		if (IS_24XX(isp) && etype == RQSTYPE_RESPONSE) {
5155 			isp24xx_statusreq_t *sp2 = (isp24xx_statusreq_t *)qe;
5156 			isp_get_24xx_response(isp, (isp24xx_statusreq_t *)hp, sp2);
5157 			scsi_status = sp2->req_scsi_status;
5158 			completion_status = sp2->req_completion_status;
5159 			if ((scsi_status & 0xff) != 0)
5160 				req_state_flags = RQSF_GOT_STATUS;
5161 			else
5162 				req_state_flags = 0;
5163 			resid = sp2->req_resid;
5164 		} else if (etype == RQSTYPE_RESPONSE) {
5165 			isp_get_response(isp, (ispstatusreq_t *) hp, sp);
5166 			scsi_status = sp->req_scsi_status;
5167 			completion_status = sp->req_completion_status;
5168 			req_status_flags = sp->req_status_flags;
5169 			req_state_flags = sp->req_state_flags;
5170 			resid = sp->req_resid;
5171 		} else if (etype == RQSTYPE_RIO1) {
5172 			isp_rio1_t *rio = (isp_rio1_t *) qe;
5173 			isp_get_rio1(isp, (isp_rio1_t *) hp, rio);
5174 			for (i = 0; i < rio->req_header.rqs_seqno; i++) {
5175 				isp_fastpost_complete(isp, rio->req_handles[i]);
5176 			}
5177 			if (isp->isp_fpcchiwater < rio->req_header.rqs_seqno) {
5178 				isp->isp_fpcchiwater = rio->req_header.rqs_seqno;
5179 			}
5180 			ISP_MEMZERO(hp, QENTRY_LEN);	/* PERF */
5181 			last_etype = etype;
5182 			continue;
5183 		} else if (etype == RQSTYPE_RIO2) {
5184 			isp_prt(isp, ISP_LOGERR, "dropping RIO2 response");
5185 			ISP_MEMZERO(hp, QENTRY_LEN);	/* PERF */
5186 			last_etype = etype;
5187 			continue;
5188 		} else if (etype == RQSTYPE_STATUS_CONT) {
5189 			isp_get_cont_response(isp, (ispstatus_cont_t *) hp, (ispstatus_cont_t *) sp);
5190 			if (last_etype == RQSTYPE_RESPONSE && continuations_expected && ndone > 0 && (xs = complist[ndone-1]) != NULL) {
5191 				ispstatus_cont_t *scp = (ispstatus_cont_t *) sp;
5192 				XS_SENSE_APPEND(xs, scp->req_sense_data, sizeof (scp->req_sense_data));
5193 				isp_prt(isp, ISP_LOGDEBUG0|ISP_LOG_CWARN, "%d more Status Continuations expected", --continuations_expected);
5194 			} else {
5195 				isp_prt(isp, ISP_LOG_WARN1, "Ignored Continuation Response");
5196 			}
5197 			ISP_MEMZERO(hp, QENTRY_LEN);	/* PERF */
5198 			continue;
5199 		} else {
5200 			/*
5201 			 * Somebody reachable via isp_handle_other_response
5202 			 * may have updated the response queue pointers for
5203 			 * us, so we reload our goal index.
5204 			 */
5205 			int r;
5206 			uint32_t tsto = oop;
5207 			r = isp_handle_other_response(isp, etype, hp, &tsto);
5208 			if (r < 0) {
5209 				goto read_again;
5210 			}
5211 			/*
5212 			 * If somebody updated the output pointer, then reset
5213 			 * optr to be one more than the updated amount.
5214 			 */
5215 			while (tsto != oop) {
5216 				optr = ISP_NXT_QENTRY(tsto, RESULT_QUEUE_LEN(isp));
5217 			}
5218 			if (r > 0) {
5219 				ISP_MEMZERO(hp, QENTRY_LEN);	/* PERF */
5220 				last_etype = etype;
5221 				continue;
5222 			}
5223 
5224 			/*
5225 			 * After this point, we'll just look at the header as
5226 			 * we don't know how to deal with the rest of the
5227 			 * response.
5228 			 */
5229 
5230 			/*
5231 			 * It really has to be a bounced request just copied
5232 			 * from the request queue to the response queue. If
5233 			 * not, something bad has happened.
5234 			 */
5235 			if (etype != RQSTYPE_REQUEST) {
5236 				isp_prt(isp, ISP_LOGERR, notresp, etype, oop, optr, nlooked);
5237 				ISP_MEMZERO(hp, QENTRY_LEN);	/* PERF */
5238 				last_etype = etype;
5239 				continue;
5240 			}
5241 			buddaboom = 1;
5242 			scsi_status = sp->req_scsi_status;
5243 			completion_status = sp->req_completion_status;
5244 			req_status_flags = sp->req_status_flags;
5245 			req_state_flags = sp->req_state_flags;
5246 			resid = sp->req_resid;
5247 		}
5248 
5249 		if (sp->req_header.rqs_flags & RQSFLAG_MASK) {
5250 			if (sp->req_header.rqs_flags & RQSFLAG_CONTINUATION) {
5251 				isp_print_qentry(isp, "unexpected continuation segment",
5252 				    oop, hp);
5253 				last_etype = etype;
5254 				continue;
5255 			}
5256 			if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
5257 				isp_prt(isp, ISP_LOG_WARN1, "internal queues full");
5258 				/*
5259 				 * We'll synthesize a QUEUE FULL message below.
5260 				 */
5261 			}
5262 			if (sp->req_header.rqs_flags & RQSFLAG_BADHEADER) {
5263 				isp_print_qentry(isp, "bad header flag",
5264 				    oop, hp);
5265 				buddaboom++;
5266 			}
5267 			if (sp->req_header.rqs_flags & RQSFLAG_BADPACKET) {
5268 				isp_print_qentry(isp, "bad request packet",
5269 				    oop, hp);
5270 				buddaboom++;
5271 			}
5272 			if (sp->req_header.rqs_flags & RQSFLAG_BADCOUNT) {
5273 				isp_print_qentry(isp, "invalid entry count",
5274 				    oop, hp);
5275 				buddaboom++;
5276 			}
5277 			if (sp->req_header.rqs_flags & RQSFLAG_BADORDER) {
5278 				isp_print_qentry(isp, "invalid IOCB ordering",
5279 				    oop, hp);
5280 				last_etype = etype;
5281 				continue;
5282 			}
5283 		}
5284 
5285 		xs = isp_find_xs(isp, sp->req_handle);
5286 		if (xs == NULL) {
5287 			uint8_t ts = completion_status & 0xff;
5288 			/*
5289 			 * Only whine if this isn't the expected fallout of
5290 			 * aborting the command or resetting the target.
5291 			 */
5292 			if (etype != RQSTYPE_RESPONSE) {
5293 				isp_prt(isp, ISP_LOGERR, "cannot find handle 0x%x (type 0x%x)", sp->req_handle, etype);
5294 			} else if (ts != RQCS_ABORTED && ts != RQCS_RESET_OCCURRED) {
5295 				isp_prt(isp, ISP_LOGERR, "cannot find handle 0x%x (status 0x%x)", sp->req_handle, ts);
5296 			}
5297 			ISP_MEMZERO(hp, QENTRY_LEN);	/* PERF */
5298 			last_etype = etype;
5299 			continue;
5300 		}
5301 		if (req_status_flags & RQSTF_BUS_RESET) {
5302 			isp_prt(isp, ISP_LOG_WARN1, "%d.%d.%jx bus was reset",
5303 			    XS_CHANNEL(xs), XS_TGT(xs), (uintmax_t)XS_LUN(xs));
5304 			XS_SETERR(xs, HBA_BUSRESET);
5305 			ISP_SET_SENDMARKER(isp, XS_CHANNEL(xs), 1);
5306 		}
5307 		if (buddaboom) {
5308 			isp_prt(isp, ISP_LOG_WARN1, "%d.%d.%jx buddaboom",
5309 			    XS_CHANNEL(xs), XS_TGT(xs), (uintmax_t)XS_LUN(xs));
5310 			XS_SETERR(xs, HBA_BOTCH);
5311 		}
5312 
5313 		resp = NULL;
5314 		rlen = 0;
5315 		snsp = NULL;
5316 		totslen = slen = 0;
5317 		if (IS_24XX(isp) && (scsi_status & (RQCS_RV|RQCS_SV)) != 0) {
5318 			resp = ((isp24xx_statusreq_t *)sp)->req_rsp_sense;
5319 			rlen = ((isp24xx_statusreq_t *)sp)->req_response_len;
5320 		} else if (IS_FC(isp) && (scsi_status & RQCS_RV) != 0) {
5321 			resp = sp->req_response;
5322 			rlen = sp->req_response_len;
5323 		}
5324 		if (IS_FC(isp) && (scsi_status & RQCS_SV) != 0) {
5325 			/*
5326 			 * Fibre Channel F/W doesn't say we got status
5327 			 * if there's Sense Data instead. I guess they
5328 			 * think it goes w/o saying.
5329 			 */
5330 			req_state_flags |= RQSF_GOT_STATUS|RQSF_GOT_SENSE;
5331 			if (IS_24XX(isp)) {
5332 				snsp = ((isp24xx_statusreq_t *)sp)->req_rsp_sense;
5333 				snsp += rlen;
5334 				totslen = ((isp24xx_statusreq_t *)sp)->req_sense_len;
5335 				slen = (sizeof (((isp24xx_statusreq_t *)sp)->req_rsp_sense)) - rlen;
5336 				if (totslen < slen)
5337 					slen = totslen;
5338 			} else {
5339 				snsp = sp->req_sense_data;
5340 				totslen = sp->req_sense_len;
5341 				slen = sizeof (sp->req_sense_data);
5342 				if (totslen < slen)
5343 					slen = totslen;
5344 			}
5345 		} else if (IS_SCSI(isp) && (req_state_flags & RQSF_GOT_SENSE)) {
5346 			snsp = sp->req_sense_data;
5347 			totslen = sp->req_sense_len;
5348 			slen = sizeof (sp->req_sense_data);
5349 			if (totslen < slen)
5350 				slen = totslen;
5351 		}
5352 		if (req_state_flags & RQSF_GOT_STATUS) {
5353 			*XS_STSP(xs) = scsi_status & 0xff;
5354 		}
5355 
5356 		switch (etype) {
5357 		case RQSTYPE_RESPONSE:
5358 			if (resp && rlen >= 4 && resp[FCP_RSPNS_CODE_OFFSET] != 0) {
5359 				const char *ptr;
5360 				char lb[64];
5361 				const char *rnames[10] = {
5362 				    "Task Management function complete",
5363 				    "FCP_DATA length different than FCP_BURST_LEN",
5364 				    "FCP_CMND fields invalid",
5365 				    "FCP_DATA parameter mismatch with FCP_DATA_RO",
5366 				    "Task Management function rejected",
5367 				    "Task Management function failed",
5368 				    NULL,
5369 				    NULL,
5370 				    "Task Management function succeeded",
5371 				    "Task Management function incorrect logical unit number",
5372 				};
5373 				uint8_t code = resp[FCP_RSPNS_CODE_OFFSET];
5374 				if (code >= 10 || rnames[code] == NULL) {
5375 					ISP_SNPRINTF(lb, sizeof(lb),
5376 					    "Unknown FCP Response Code 0x%x",
5377 					    code);
5378 					ptr = lb;
5379 				} else {
5380 					ptr = rnames[code];
5381 				}
5382 				isp_xs_prt(isp, xs, ISP_LOGWARN,
5383 				    "FCP RESPONSE, LENGTH %u: %s CDB0=0x%02x",
5384 				    rlen, ptr, XS_CDBP(xs)[0] & 0xff);
5385 				if (code != 0 && code != 8)
5386 					XS_SETERR(xs, HBA_BOTCH);
5387 			}
5388 			if (IS_24XX(isp)) {
5389 				isp_parse_status_24xx(isp, (isp24xx_statusreq_t *)sp, xs, &resid);
5390 			} else {
5391 				isp_parse_status(isp, (void *)sp, xs, &resid);
5392 			}
5393 			if ((XS_NOERR(xs) || XS_ERR(xs) == HBA_NOERROR) && (*XS_STSP(xs) == SCSI_BUSY)) {
5394 				XS_SETERR(xs, HBA_TGTBSY);
5395 			}
5396 			if (IS_SCSI(isp)) {
5397 				XS_SET_RESID(xs, resid);
5398 				/*
5399 				 * A new synchronous rate was negotiated for
5400 				 * this target. Mark state such that we'll go
5401 				 * look up that which has changed later.
5402 				 */
5403 				if (req_status_flags & RQSTF_NEGOTIATION) {
5404 					int t = XS_TGT(xs);
5405 					sdparam *sdp = SDPARAM(isp, XS_CHANNEL(xs));
5406 					sdp->isp_devparam[t].dev_refresh = 1;
5407 					sdp->update = 1;
5408 				}
5409 			} else {
5410 				if (req_status_flags & RQSF_XFER_COMPLETE) {
5411 					XS_SET_RESID(xs, 0);
5412 				} else if (scsi_status & RQCS_RESID) {
5413 					XS_SET_RESID(xs, resid);
5414 				} else {
5415 					XS_SET_RESID(xs, 0);
5416 				}
5417 			}
5418 			if (snsp && slen) {
5419 				if (totslen > slen) {
5420 					continuations_expected += ((totslen - slen + QENTRY_LEN - 5) / (QENTRY_LEN - 4));
5421 					if (ndone > (MAX_REQUESTQ_COMPLETIONS - continuations_expected - 1)) {
5422 						/* we'll lose some stats, but that's a small price to pay */
5423 						for (i = 0; i < ndone; i++) {
5424 							if (complist[i]) {
5425 								isp->isp_rsltccmplt++;
5426 								isp_done(complist[i]);
5427 							}
5428 						}
5429 						ndone = 0;
5430 					}
5431 					isp_prt(isp, ISP_LOGDEBUG0|ISP_LOG_CWARN, "Expecting %d more Status Continuations for total sense length of %u",
5432 					    continuations_expected, totslen);
5433 				}
5434 				XS_SAVE_SENSE(xs, snsp, totslen, slen);
5435 			} else if ((req_status_flags & RQSF_GOT_STATUS) && (scsi_status & 0xff) == SCSI_CHECK && IS_FC(isp)) {
5436 				isp_prt(isp, ISP_LOGWARN, "CHECK CONDITION w/o sense data for CDB=0x%x", XS_CDBP(xs)[0] & 0xff);
5437 				isp_print_qentry(isp, "CC with no Sense",
5438 				    oop, hp);
5439 			}
5440 			isp_prt(isp, ISP_LOGDEBUG2, "asked for %ld got raw resid %ld settled for %ld", (long) XS_XFRLEN(xs), resid, (long) XS_GET_RESID(xs));
5441 			break;
5442 		case RQSTYPE_REQUEST:
5443 		case RQSTYPE_A64:
5444 		case RQSTYPE_T2RQS:
5445 		case RQSTYPE_T3RQS:
5446 		case RQSTYPE_T7RQS:
5447 			if (!IS_24XX(isp) && (sp->req_header.rqs_flags & RQSFLAG_FULL)) {
5448 				/*
5449 				 * Force Queue Full status.
5450 				 */
5451 				*XS_STSP(xs) = SCSI_QFULL;
5452 				XS_SETERR(xs, HBA_NOERROR);
5453 			} else if (XS_NOERR(xs)) {
5454 				isp_prt(isp, ISP_LOG_WARN1,
5455 				    "%d.%d.%jx badness at %s:%u",
5456 				    XS_CHANNEL(xs), XS_TGT(xs),
5457 				    (uintmax_t)XS_LUN(xs),
5458 				    __func__, __LINE__);
5459 				XS_SETERR(xs, HBA_BOTCH);
5460 			}
5461 			XS_SET_RESID(xs, XS_XFRLEN(xs));
5462 			break;
5463 		default:
5464 			isp_print_qentry(isp, "Unhandled Response Type",
5465 			    oop, hp);
5466 			if (XS_NOERR(xs)) {
5467 				XS_SETERR(xs, HBA_BOTCH);
5468 			}
5469 			break;
5470 		}
5471 
5472 		/*
5473 		 * Free any DMA resources. As a side effect, this may
5474 		 * also do any cache flushing necessary for data coherence.
5475 		 */
5476 		if (XS_XFRLEN(xs)) {
5477 			ISP_DMAFREE(isp, xs, sp->req_handle);
5478 		}
5479 		isp_destroy_handle(isp, sp->req_handle);
5480 
5481 		if (isp->isp_nactive > 0) {
5482 		    isp->isp_nactive--;
5483 		}
5484 		complist[ndone++] = xs;	/* defer completion call until later */
5485 		ISP_MEMZERO(hp, QENTRY_LEN);	/* PERF */
5486 		last_etype = etype;
5487 		if (ndone == MAX_REQUESTQ_COMPLETIONS) {
5488 			break;
5489 		}
5490 	}
5491 
5492 	/*
5493 	 * If we looked at any commands, then it's valid to find out
5494 	 * what the outpointer is. It also is a trigger to update the
5495 	 * ISP's notion of what we've seen so far.
5496 	 */
5497 	if (nlooked) {
5498 		ISP_WRITE(isp, isp->isp_respoutrp, optr);
5499 		isp->isp_resodx = optr;
5500 		if (isp->isp_rscchiwater < ndone)
5501 			isp->isp_rscchiwater = ndone;
5502 	}
5503 
5504 out:
5505 
5506 	if (IS_24XX(isp)) {
5507 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
5508 	} else {
5509 		ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
5510 		ISP_WRITE(isp, BIU_SEMA, 0);
5511 	}
5512 
5513 	for (i = 0; i < ndone; i++) {
5514 		xs = complist[i];
5515 		if (xs) {
5516 			if (((isp->isp_dblev & (ISP_LOGDEBUG1|ISP_LOGDEBUG2|ISP_LOGDEBUG3))) ||
5517 			    ((isp->isp_dblev & (ISP_LOGDEBUG0|ISP_LOG_CWARN) && ((!XS_NOERR(xs)) || (*XS_STSP(xs) != SCSI_GOOD))))) {
5518 				isp_prt_endcmd(isp, xs);
5519 			}
5520 			isp->isp_rsltccmplt++;
5521 			isp_done(xs);
5522 		}
5523 	}
5524 }
5525 
5526 /*
5527  * Support routines.
5528  */
5529 
5530 void
5531 isp_prt_endcmd(ispsoftc_t *isp, XS_T *xs)
5532 {
5533 	char cdbstr[16 * 5 + 1];
5534 	int i, lim;
5535 
5536 	lim = XS_CDBLEN(xs) > 16? 16 : XS_CDBLEN(xs);
5537 	ISP_SNPRINTF(cdbstr, sizeof (cdbstr), "0x%02x ", XS_CDBP(xs)[0]);
5538 	for (i = 1; i < lim; i++) {
5539 		ISP_SNPRINTF(cdbstr, sizeof (cdbstr), "%s0x%02x ", cdbstr, XS_CDBP(xs)[i]);
5540 	}
5541 	if (XS_SENSE_VALID(xs)) {
5542 		isp_xs_prt(isp, xs, ISP_LOGALL, "FIN dl%d resid %ld CDB=%s SenseLength=%u/%u KEY/ASC/ASCQ=0x%02x/0x%02x/0x%02x",
5543 		    XS_XFRLEN(xs), (long) XS_GET_RESID(xs), cdbstr, XS_CUR_SNSLEN(xs), XS_TOT_SNSLEN(xs), XS_SNSKEY(xs), XS_SNSASC(xs), XS_SNSASCQ(xs));
5544 	} else {
5545 		isp_xs_prt(isp, xs, ISP_LOGALL, "FIN dl%d resid %ld CDB=%s STS 0x%x XS_ERR=0x%x", XS_XFRLEN(xs), (long) XS_GET_RESID(xs), cdbstr, *XS_STSP(xs), XS_ERR(xs));
5546 	}
5547 }
5548 
5549 /*
5550  * Parse an ASYNC mailbox complete
5551  *
5552  * Return non-zero if the event has been acknowledged.
5553  */
5554 static int
5555 isp_parse_async(ispsoftc_t *isp, uint16_t mbox)
5556 {
5557 	int acked = 0;
5558 	uint32_t h1 = 0, h2 = 0;
5559 	uint16_t chan = 0;
5560 
5561 	/*
5562 	 * Pick up the channel, but not if this is a ASYNC_RIO32_2,
5563 	 * where Mailboxes 6/7 have the second handle.
5564 	 */
5565 	if (mbox != ASYNC_RIO32_2) {
5566 		if (IS_DUALBUS(isp)) {
5567 			chan = ISP_READ(isp, OUTMAILBOX6);
5568 		}
5569 	}
5570 	isp_prt(isp, ISP_LOGDEBUG2, "Async Mbox 0x%x", mbox);
5571 
5572 	switch (mbox) {
5573 	case ASYNC_BUS_RESET:
5574 		ISP_SET_SENDMARKER(isp, chan, 1);
5575 #ifdef	ISP_TARGET_MODE
5576 		if (isp_target_async(isp, chan, mbox)) {
5577 			acked = 1;
5578 		}
5579 #endif
5580 		isp_async(isp, ISPASYNC_BUS_RESET, chan);
5581 		break;
5582 	case ASYNC_SYSTEM_ERROR:
5583 		isp->isp_dead = 1;
5584 		isp->isp_state = ISP_CRASHED;
5585 		/*
5586 		 * Were we waiting for a mailbox command to complete?
5587 		 * If so, it's dead, so wake up the waiter.
5588 		 */
5589 		if (isp->isp_mboxbsy) {
5590 			isp->isp_obits = 1;
5591 			isp->isp_mboxtmp[0] = MBOX_HOST_INTERFACE_ERROR;
5592 			MBOX_NOTIFY_COMPLETE(isp);
5593 		}
5594 		/*
5595 		 * It's up to the handler for isp_async to reinit stuff and
5596 		 * restart the firmware
5597 		 */
5598 		isp_async(isp, ISPASYNC_FW_CRASH);
5599 		acked = 1;
5600 		break;
5601 
5602 	case ASYNC_RQS_XFER_ERR:
5603 		isp_prt(isp, ISP_LOGERR, "Request Queue Transfer Error");
5604 		break;
5605 
5606 	case ASYNC_RSP_XFER_ERR:
5607 		isp_prt(isp, ISP_LOGERR, "Response Queue Transfer Error");
5608 		break;
5609 
5610 	case ASYNC_QWAKEUP:
5611 		/*
5612 		 * We've just been notified that the Queue has woken up.
5613 		 * We don't need to be chatty about this- just unlatch things
5614 		 * and move on.
5615 		 */
5616 		mbox = ISP_READ(isp, isp->isp_rqstoutrp);
5617 		break;
5618 
5619 	case ASYNC_TIMEOUT_RESET:
5620 		isp_prt(isp, ISP_LOGWARN, "timeout initiated SCSI bus reset of chan %d", chan);
5621 		ISP_SET_SENDMARKER(isp, chan, 1);
5622 #ifdef	ISP_TARGET_MODE
5623 		if (isp_target_async(isp, chan, mbox)) {
5624 			acked = 1;
5625 		}
5626 #endif
5627 		break;
5628 
5629 	case ASYNC_DEVICE_RESET:
5630 		isp_prt(isp, ISP_LOGINFO, "device reset on chan %d", chan);
5631 		ISP_SET_SENDMARKER(isp, chan, 1);
5632 #ifdef	ISP_TARGET_MODE
5633 		if (isp_target_async(isp, chan, mbox)) {
5634 			acked = 1;
5635 		}
5636 #endif
5637 		break;
5638 
5639 	case ASYNC_EXTMSG_UNDERRUN:
5640 		isp_prt(isp, ISP_LOGWARN, "extended message underrun");
5641 		break;
5642 
5643 	case ASYNC_SCAM_INT:
5644 		isp_prt(isp, ISP_LOGINFO, "SCAM interrupt");
5645 		break;
5646 
5647 	case ASYNC_HUNG_SCSI:
5648 		isp_prt(isp, ISP_LOGERR, "stalled SCSI Bus after DATA Overrun");
5649 		/* XXX: Need to issue SCSI reset at this point */
5650 		break;
5651 
5652 	case ASYNC_KILLED_BUS:
5653 		isp_prt(isp, ISP_LOGERR, "SCSI Bus reset after DATA Overrun");
5654 		break;
5655 
5656 	case ASYNC_BUS_TRANSIT:
5657 		mbox = ISP_READ(isp, OUTMAILBOX2);
5658 		switch (mbox & SXP_PINS_MODE_MASK) {
5659 		case SXP_PINS_LVD_MODE:
5660 			isp_prt(isp, ISP_LOGINFO, "Transition to LVD mode");
5661 			SDPARAM(isp, chan)->isp_diffmode = 0;
5662 			SDPARAM(isp, chan)->isp_ultramode = 0;
5663 			SDPARAM(isp, chan)->isp_lvdmode = 1;
5664 			break;
5665 		case SXP_PINS_HVD_MODE:
5666 			isp_prt(isp, ISP_LOGINFO,
5667 			    "Transition to Differential mode");
5668 			SDPARAM(isp, chan)->isp_diffmode = 1;
5669 			SDPARAM(isp, chan)->isp_ultramode = 0;
5670 			SDPARAM(isp, chan)->isp_lvdmode = 0;
5671 			break;
5672 		case SXP_PINS_SE_MODE:
5673 			isp_prt(isp, ISP_LOGINFO,
5674 			    "Transition to Single Ended mode");
5675 			SDPARAM(isp, chan)->isp_diffmode = 0;
5676 			SDPARAM(isp, chan)->isp_ultramode = 1;
5677 			SDPARAM(isp, chan)->isp_lvdmode = 0;
5678 			break;
5679 		default:
5680 			isp_prt(isp, ISP_LOGWARN,
5681 			    "Transition to Unknown Mode 0x%x", mbox);
5682 			break;
5683 		}
5684 		/*
5685 		 * XXX: Set up to renegotiate again!
5686 		 */
5687 		/* Can only be for a 1080... */
5688 		ISP_SET_SENDMARKER(isp, chan, 1);
5689 		break;
5690 
5691 	case ASYNC_CMD_CMPLT:
5692 	case ASYNC_RIO32_1:
5693 		if (!IS_ULTRA3(isp)) {
5694 			isp_prt(isp, ISP_LOGERR, "unexpected fast posting completion");
5695 			break;
5696 		}
5697 		/* FALLTHROUGH */
5698 		h1 = (ISP_READ(isp, OUTMAILBOX2) << 16) | ISP_READ(isp, OUTMAILBOX1);
5699 		break;
5700 
5701 	case ASYNC_RIO32_2:
5702 		h1 = (ISP_READ(isp, OUTMAILBOX2) << 16) | ISP_READ(isp, OUTMAILBOX1);
5703 		h2 = (ISP_READ(isp, OUTMAILBOX7) << 16) | ISP_READ(isp, OUTMAILBOX6);
5704 		break;
5705 
5706 	case ASYNC_RIO16_5:
5707 	case ASYNC_RIO16_4:
5708 	case ASYNC_RIO16_3:
5709 	case ASYNC_RIO16_2:
5710 	case ASYNC_RIO16_1:
5711 		isp_prt(isp, ISP_LOGERR, "unexpected 16 bit RIO handle");
5712 		break;
5713 	default:
5714 		isp_prt(isp, ISP_LOGWARN, "%s: unhandled async code 0x%x", __func__, mbox);
5715 		break;
5716 	}
5717 
5718 	if (h1 || h2) {
5719 		isp_prt(isp, ISP_LOGDEBUG3, "fast post/rio completion of 0x%08x", h1);
5720 		isp_fastpost_complete(isp, h1);
5721 		if (h2) {
5722 			isp_prt(isp, ISP_LOGDEBUG3, "fast post/rio completion of 0x%08x", h2);
5723 			isp_fastpost_complete(isp, h2);
5724 			if (isp->isp_fpcchiwater < 2) {
5725 				isp->isp_fpcchiwater = 2;
5726 			}
5727 		} else {
5728 			if (isp->isp_fpcchiwater < 1) {
5729 				isp->isp_fpcchiwater = 1;
5730 			}
5731 		}
5732 	} else {
5733 		isp->isp_intoasync++;
5734 	}
5735 	return (acked);
5736 }
5737 
5738 static int
5739 isp_parse_async_fc(ispsoftc_t *isp, uint16_t mbox)
5740 {
5741 	fcparam *fcp;
5742 	int acked = 0;
5743 	uint16_t chan;
5744 
5745 	if (IS_DUALBUS(isp)) {
5746 		chan = ISP_READ(isp, OUTMAILBOX6);
5747 	} else {
5748 		chan = 0;
5749 	}
5750 	isp_prt(isp, ISP_LOGDEBUG2, "Async Mbox 0x%x", mbox);
5751 
5752 	switch (mbox) {
5753 	case ASYNC_SYSTEM_ERROR:
5754 		isp->isp_dead = 1;
5755 		isp->isp_state = ISP_CRASHED;
5756 		FCPARAM(isp, chan)->isp_loopstate = LOOP_NIL;
5757 		isp_change_fw_state(isp, chan, FW_CONFIG_WAIT);
5758 		/*
5759 		 * Were we waiting for a mailbox command to complete?
5760 		 * If so, it's dead, so wake up the waiter.
5761 		 */
5762 		if (isp->isp_mboxbsy) {
5763 			isp->isp_obits = 1;
5764 			isp->isp_mboxtmp[0] = MBOX_HOST_INTERFACE_ERROR;
5765 			MBOX_NOTIFY_COMPLETE(isp);
5766 		}
5767 		/*
5768 		 * It's up to the handler for isp_async to reinit stuff and
5769 		 * restart the firmware
5770 		 */
5771 		isp_async(isp, ISPASYNC_FW_CRASH);
5772 		acked = 1;
5773 		break;
5774 
5775 	case ASYNC_RQS_XFER_ERR:
5776 		isp_prt(isp, ISP_LOGERR, "Request Queue Transfer Error");
5777 		break;
5778 
5779 	case ASYNC_RSP_XFER_ERR:
5780 		isp_prt(isp, ISP_LOGERR, "Response Queue Transfer Error");
5781 		break;
5782 
5783 	case ASYNC_QWAKEUP:
5784 #ifdef	ISP_TARGET_MODE
5785 		if (IS_24XX(isp)) {
5786 			isp_prt(isp, ISP_LOGERR, "ATIO Queue Transfer Error");
5787 			break;
5788 		}
5789 #endif
5790 		isp_prt(isp, ISP_LOGERR, "%s: unexpected ASYNC_QWAKEUP code", __func__);
5791 		break;
5792 
5793 	case ASYNC_CMD_CMPLT:
5794 		isp_fastpost_complete(isp, (ISP_READ(isp, OUTMAILBOX2) << 16) | ISP_READ(isp, OUTMAILBOX1));
5795 		if (isp->isp_fpcchiwater < 1) {
5796 			isp->isp_fpcchiwater = 1;
5797 		}
5798 		break;
5799 
5800 	case ASYNC_RIOZIO_STALL:
5801 		break;
5802 
5803 	case ASYNC_CTIO_DONE:
5804 #ifdef	ISP_TARGET_MODE
5805 		if (isp_target_async(isp, (ISP_READ(isp, OUTMAILBOX2) << 16) | ISP_READ(isp, OUTMAILBOX1), mbox)) {
5806 			acked = 1;
5807 		} else {
5808 			isp->isp_fphccmplt++;
5809 		}
5810 #else
5811 		isp_prt(isp, ISP_LOGWARN, "unexpected ASYNC CTIO done");
5812 #endif
5813 		break;
5814 	case ASYNC_LIP_ERROR:
5815 	case ASYNC_LIP_NOS_OLS_RECV:
5816 	case ASYNC_LIP_OCCURRED:
5817 	case ASYNC_PTPMODE:
5818 		/*
5819 		 * These are broadcast events that have to be sent across
5820 		 * all active channels.
5821 		 */
5822 		for (chan = 0; chan < isp->isp_nchan; chan++) {
5823 			fcp = FCPARAM(isp, chan);
5824 			int topo = fcp->isp_topo;
5825 
5826 			if (fcp->role == ISP_ROLE_NONE)
5827 				continue;
5828 			if (fcp->isp_loopstate > LOOP_HAVE_LINK)
5829 				fcp->isp_loopstate = LOOP_HAVE_LINK;
5830 			ISP_SET_SENDMARKER(isp, chan, 1);
5831 			isp_async(isp, ISPASYNC_LIP, chan);
5832 #ifdef	ISP_TARGET_MODE
5833 			if (isp_target_async(isp, chan, mbox)) {
5834 				acked = 1;
5835 			}
5836 #endif
5837 			/*
5838 			 * We've had problems with data corruption occuring on
5839 			 * commands that complete (with no apparent error) after
5840 			 * we receive a LIP. This has been observed mostly on
5841 			 * Local Loop topologies. To be safe, let's just mark
5842 			 * all active initiator commands as dead.
5843 			 */
5844 			if (topo == TOPO_NL_PORT || topo == TOPO_FL_PORT) {
5845 				int i, j;
5846 				for (i = j = 0; i < isp->isp_maxcmds; i++) {
5847 					XS_T *xs;
5848 					isp_hdl_t *hdp;
5849 
5850 					hdp = &isp->isp_xflist[i];
5851 					if (ISP_H2HT(hdp->handle) != ISP_HANDLE_INITIATOR) {
5852 						continue;
5853 					}
5854 					xs = hdp->cmd;
5855 					if (XS_CHANNEL(xs) != chan) {
5856 						continue;
5857 					}
5858 					j++;
5859 					isp_prt(isp, ISP_LOG_WARN1,
5860 					    "%d.%d.%jx bus reset set at %s:%u",
5861 					    XS_CHANNEL(xs), XS_TGT(xs),
5862 					    (uintmax_t)XS_LUN(xs),
5863 					    __func__, __LINE__);
5864 					XS_SETERR(xs, HBA_BUSRESET);
5865 				}
5866 				if (j) {
5867 					isp_prt(isp, ISP_LOGERR, lipd, chan, j);
5868 				}
5869 			}
5870 		}
5871 		break;
5872 
5873 	case ASYNC_LOOP_UP:
5874 		/*
5875 		 * This is a broadcast event that has to be sent across
5876 		 * all active channels.
5877 		 */
5878 		for (chan = 0; chan < isp->isp_nchan; chan++) {
5879 			fcp = FCPARAM(isp, chan);
5880 			if (fcp->role == ISP_ROLE_NONE)
5881 				continue;
5882 			fcp->isp_linkstate = 1;
5883 			if (fcp->isp_loopstate < LOOP_HAVE_LINK)
5884 				fcp->isp_loopstate = LOOP_HAVE_LINK;
5885 			ISP_SET_SENDMARKER(isp, chan, 1);
5886 			isp_async(isp, ISPASYNC_LOOP_UP, chan);
5887 #ifdef	ISP_TARGET_MODE
5888 			if (isp_target_async(isp, chan, mbox)) {
5889 				acked = 1;
5890 			}
5891 #endif
5892 		}
5893 		break;
5894 
5895 	case ASYNC_LOOP_DOWN:
5896 		/*
5897 		 * This is a broadcast event that has to be sent across
5898 		 * all active channels.
5899 		 */
5900 		for (chan = 0; chan < isp->isp_nchan; chan++) {
5901 			fcp = FCPARAM(isp, chan);
5902 			if (fcp->role == ISP_ROLE_NONE)
5903 				continue;
5904 			ISP_SET_SENDMARKER(isp, chan, 1);
5905 			fcp->isp_linkstate = 0;
5906 			fcp->isp_loopstate = LOOP_NIL;
5907 			isp_async(isp, ISPASYNC_LOOP_DOWN, chan);
5908 #ifdef	ISP_TARGET_MODE
5909 			if (isp_target_async(isp, chan, mbox)) {
5910 				acked = 1;
5911 			}
5912 #endif
5913 		}
5914 		break;
5915 
5916 	case ASYNC_LOOP_RESET:
5917 		/*
5918 		 * This is a broadcast event that has to be sent across
5919 		 * all active channels.
5920 		 */
5921 		for (chan = 0; chan < isp->isp_nchan; chan++) {
5922 			fcp = FCPARAM(isp, chan);
5923 			if (fcp->role == ISP_ROLE_NONE)
5924 				continue;
5925 			ISP_SET_SENDMARKER(isp, chan, 1);
5926 			if (fcp->isp_loopstate > LOOP_HAVE_LINK)
5927 				fcp->isp_loopstate = LOOP_HAVE_LINK;
5928 			isp_async(isp, ISPASYNC_LOOP_RESET, chan);
5929 #ifdef	ISP_TARGET_MODE
5930 			if (isp_target_async(isp, chan, mbox)) {
5931 				acked = 1;
5932 			}
5933 #endif
5934 		}
5935 		break;
5936 
5937 	case ASYNC_PDB_CHANGED:
5938 	{
5939 		int echan, nphdl, nlstate, reason;
5940 
5941 		if (IS_23XX(isp) || IS_24XX(isp)) {
5942 			nphdl = ISP_READ(isp, OUTMAILBOX1);
5943 			nlstate = ISP_READ(isp, OUTMAILBOX2);
5944 		} else {
5945 			nphdl = nlstate = 0xffff;
5946 		}
5947 		if (IS_24XX(isp))
5948 			reason = ISP_READ(isp, OUTMAILBOX3) >> 8;
5949 		else
5950 			reason = 0xff;
5951 		if (ISP_CAP_MULTI_ID(isp)) {
5952 			chan = ISP_READ(isp, OUTMAILBOX3) & 0xff;
5953 			if (chan == 0xff || nphdl == NIL_HANDLE) {
5954 				chan = 0;
5955 				echan = isp->isp_nchan - 1;
5956 			} else if (chan >= isp->isp_nchan) {
5957 				break;
5958 			} else {
5959 				echan = chan;
5960 			}
5961 		} else {
5962 			chan = echan = 0;
5963 		}
5964 		for (; chan <= echan; chan++) {
5965 			fcp = FCPARAM(isp, chan);
5966 			if (fcp->role == ISP_ROLE_NONE)
5967 				continue;
5968 			if (fcp->isp_loopstate > LOOP_LTEST_DONE)
5969 				fcp->isp_loopstate = LOOP_LTEST_DONE;
5970 			else if (fcp->isp_loopstate < LOOP_HAVE_LINK)
5971 				fcp->isp_loopstate = LOOP_HAVE_LINK;
5972 			isp_async(isp, ISPASYNC_CHANGE_NOTIFY, chan,
5973 			    ISPASYNC_CHANGE_PDB, nphdl, nlstate, reason);
5974 		}
5975 		break;
5976 	}
5977 	case ASYNC_CHANGE_NOTIFY:
5978 	{
5979 		int portid;
5980 
5981 		portid = ((ISP_READ(isp, OUTMAILBOX1) & 0xff) << 16) |
5982 		    ISP_READ(isp, OUTMAILBOX2);
5983 		if (ISP_CAP_MULTI_ID(isp)) {
5984 			chan = ISP_READ(isp, OUTMAILBOX3) & 0xff;
5985 			if (chan >= isp->isp_nchan)
5986 				break;
5987 		} else {
5988 			chan = 0;
5989 		}
5990 		fcp = FCPARAM(isp, chan);
5991 		if (fcp->role == ISP_ROLE_NONE)
5992 			break;
5993 		if (fcp->isp_loopstate > LOOP_LTEST_DONE)
5994 			fcp->isp_loopstate = LOOP_LTEST_DONE;
5995 		else if (fcp->isp_loopstate < LOOP_HAVE_LINK)
5996 			fcp->isp_loopstate = LOOP_HAVE_LINK;
5997 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, chan,
5998 		    ISPASYNC_CHANGE_SNS, portid);
5999 		break;
6000 	}
6001 	case ASYNC_ERR_LOGGING_DISABLED:
6002 		isp_prt(isp, ISP_LOGWARN, "Error logging disabled (reason 0x%x)",
6003 		    ISP_READ(isp, OUTMAILBOX1));
6004 		break;
6005 	case ASYNC_CONNMODE:
6006 		/*
6007 		 * This only applies to 2100 amd 2200 cards
6008 		 */
6009 		if (!IS_2200(isp) && !IS_2100(isp)) {
6010 			isp_prt(isp, ISP_LOGWARN, "bad card for ASYNC_CONNMODE event");
6011 			break;
6012 		}
6013 		chan = 0;
6014 		mbox = ISP_READ(isp, OUTMAILBOX1);
6015 		switch (mbox) {
6016 		case ISP_CONN_LOOP:
6017 			isp_prt(isp, ISP_LOGINFO,
6018 			    "Point-to-Point -> Loop mode");
6019 			break;
6020 		case ISP_CONN_PTP:
6021 			isp_prt(isp, ISP_LOGINFO,
6022 			    "Loop -> Point-to-Point mode");
6023 			break;
6024 		case ISP_CONN_BADLIP:
6025 			isp_prt(isp, ISP_LOGWARN,
6026 			    "Point-to-Point -> Loop mode (BAD LIP)");
6027 			break;
6028 		case ISP_CONN_FATAL:
6029 			isp->isp_dead = 1;
6030 			isp->isp_state = ISP_CRASHED;
6031 			isp_prt(isp, ISP_LOGERR, "FATAL CONNECTION ERROR");
6032 			isp_async(isp, ISPASYNC_FW_CRASH);
6033 			return (-1);
6034 		case ISP_CONN_LOOPBACK:
6035 			isp_prt(isp, ISP_LOGWARN,
6036 			    "Looped Back in Point-to-Point mode");
6037 			break;
6038 		default:
6039 			isp_prt(isp, ISP_LOGWARN,
6040 			    "Unknown connection mode (0x%x)", mbox);
6041 			break;
6042 		}
6043 		ISP_SET_SENDMARKER(isp, chan, 1);
6044 		FCPARAM(isp, chan)->isp_loopstate = LOOP_HAVE_LINK;
6045 		isp_async(isp, ISPASYNC_CHANGE_NOTIFY, chan, ISPASYNC_CHANGE_OTHER);
6046 		break;
6047 	case ASYNC_P2P_INIT_ERR:
6048 		isp_prt(isp, ISP_LOGWARN, "P2P init error (reason 0x%x)",
6049 		    ISP_READ(isp, OUTMAILBOX1));
6050 		break;
6051 	case ASYNC_RCV_ERR:
6052 		if (IS_24XX(isp)) {
6053 			isp_prt(isp, ISP_LOGWARN, "Receive Error");
6054 		} else {
6055 			isp_prt(isp, ISP_LOGWARN, "unexpected ASYNC_RCV_ERR");
6056 		}
6057 		break;
6058 	case ASYNC_RJT_SENT:	/* same as ASYNC_QFULL_SENT */
6059 		if (IS_24XX(isp)) {
6060 			isp_prt(isp, ISP_LOGTDEBUG0, "LS_RJT sent");
6061 			break;
6062 		} else {
6063 			isp_prt(isp, ISP_LOGTDEBUG0, "QFULL sent");
6064 			break;
6065 		}
6066 	case ASYNC_FW_RESTART_COMPLETE:
6067 		isp_prt(isp, ISP_LOGDEBUG0, "FW restart complete");
6068 		break;
6069 	case ASYNC_TEMPERATURE_ALERT:
6070 		isp_prt(isp, ISP_LOGERR, "Temperature alert (subcode 0x%x)",
6071 		    ISP_READ(isp, OUTMAILBOX1));
6072 		break;
6073 	case ASYNC_AUTOLOAD_FW_COMPLETE:
6074 		isp_prt(isp, ISP_LOGDEBUG0, "Autoload FW init complete");
6075 		break;
6076 	case ASYNC_AUTOLOAD_FW_FAILURE:
6077 		isp_prt(isp, ISP_LOGERR, "Autoload FW init failure");
6078 		break;
6079 	default:
6080 		isp_prt(isp, ISP_LOGWARN, "Unknown Async Code 0x%x", mbox);
6081 		break;
6082 	}
6083 	if (mbox != ASYNC_CTIO_DONE && mbox != ASYNC_CMD_CMPLT) {
6084 		isp->isp_intoasync++;
6085 	}
6086 	return (acked);
6087 }
6088 
6089 /*
6090  * Handle other response entries. A pointer to the request queue output
6091  * index is here in case we want to eat several entries at once, although
6092  * this is not used currently.
6093  */
6094 
6095 static int
6096 isp_handle_other_response(ispsoftc_t *isp, int type, isphdr_t *hp, uint32_t *optrp)
6097 {
6098 	isp_ridacq_t rid;
6099 	int chan, c;
6100 	uint32_t hdl;
6101 	void *ptr;
6102 
6103 	switch (type) {
6104 	case RQSTYPE_STATUS_CONT:
6105 		isp_prt(isp, ISP_LOG_WARN1, "Ignored Continuation Response");
6106 		return (1);
6107 	case RQSTYPE_MARKER:
6108 		isp_prt(isp, ISP_LOG_WARN1, "Marker Response");
6109 		return (1);
6110 	case RQSTYPE_RPT_ID_ACQ:
6111 		isp_get_ridacq(isp, (isp_ridacq_t *)hp, &rid);
6112 		if (rid.ridacq_format == 0) {
6113 			for (chan = 0; chan < isp->isp_nchan; chan++) {
6114 				fcparam *fcp = FCPARAM(isp, chan);
6115 				if (fcp->role == ISP_ROLE_NONE)
6116 					continue;
6117 				c = (chan == 0) ? 127 : (chan - 1);
6118 				if (rid.ridacq_map[c / 16] & (1 << (c % 16)) ||
6119 				    chan == 0) {
6120 					fcp->isp_loopstate = LOOP_HAVE_LINK;
6121 					isp_async(isp, ISPASYNC_CHANGE_NOTIFY,
6122 					    chan, ISPASYNC_CHANGE_OTHER);
6123 				} else {
6124 					fcp->isp_loopstate = LOOP_NIL;
6125 					isp_async(isp, ISPASYNC_LOOP_DOWN,
6126 					    chan);
6127 				}
6128 			}
6129 		} else {
6130 			fcparam *fcp = FCPARAM(isp, rid.ridacq_vp_index);
6131 			if (rid.ridacq_vp_status == RIDACQ_STS_COMPLETE ||
6132 			    rid.ridacq_vp_status == RIDACQ_STS_CHANGED) {
6133 				fcp->isp_loopstate = LOOP_HAVE_LINK;
6134 				isp_async(isp, ISPASYNC_CHANGE_NOTIFY,
6135 				    rid.ridacq_vp_index, ISPASYNC_CHANGE_OTHER);
6136 			} else {
6137 				fcp->isp_loopstate = LOOP_NIL;
6138 				isp_async(isp, ISPASYNC_LOOP_DOWN,
6139 				    rid.ridacq_vp_index);
6140 			}
6141 		}
6142 		return (1);
6143 	case RQSTYPE_VP_MODIFY:
6144 	case RQSTYPE_VP_CTRL:
6145 	case RQSTYPE_LOGIN:
6146 		ISP_IOXGET_32(isp, (uint32_t *)(hp + 1), hdl);
6147 		ptr = isp_find_xs(isp, hdl);
6148 		if (ptr != NULL) {
6149 			isp_destroy_handle(isp, hdl);
6150 			memcpy(ptr, hp, QENTRY_LEN);
6151 			wakeup(ptr);
6152 		}
6153 		return (1);
6154 	case RQSTYPE_ATIO:
6155 	case RQSTYPE_CTIO:
6156 	case RQSTYPE_ENABLE_LUN:
6157 	case RQSTYPE_MODIFY_LUN:
6158 	case RQSTYPE_NOTIFY:
6159 	case RQSTYPE_NOTIFY_ACK:
6160 	case RQSTYPE_CTIO1:
6161 	case RQSTYPE_ATIO2:
6162 	case RQSTYPE_CTIO2:
6163 	case RQSTYPE_CTIO3:
6164 	case RQSTYPE_CTIO7:
6165 	case RQSTYPE_ABTS_RCVD:
6166 	case RQSTYPE_ABTS_RSP:
6167 		isp->isp_rsltccmplt++;	/* count as a response completion */
6168 #ifdef	ISP_TARGET_MODE
6169 		if (isp_target_notify(isp, (ispstatusreq_t *) hp, optrp)) {
6170 			return (1);
6171 		}
6172 #endif
6173 		/* FALLTHROUGH */
6174 	case RQSTYPE_REQUEST:
6175 	default:
6176 		ISP_DELAY(100);
6177 		if (type != isp_get_response_type(isp, hp)) {
6178 			/*
6179 			 * This is questionable- we're just papering over
6180 			 * something we've seen on SMP linux in target
6181 			 * mode- we don't really know what's happening
6182 			 * here that causes us to think we've gotten
6183 			 * an entry, but that either the entry isn't
6184 			 * filled out yet or our CPU read data is stale.
6185 			 */
6186 			isp_prt(isp, ISP_LOGINFO,
6187 				"unstable type in response queue");
6188 			return (-1);
6189 		}
6190 		isp_prt(isp, ISP_LOGWARN, "Unhandled Response Type 0x%x",
6191 		    isp_get_response_type(isp, hp));
6192 		return (0);
6193 	}
6194 }
6195 
6196 static void
6197 isp_parse_status(ispsoftc_t *isp, ispstatusreq_t *sp, XS_T *xs, long *rp)
6198 {
6199 	switch (sp->req_completion_status & 0xff) {
6200 	case RQCS_COMPLETE:
6201 		if (XS_NOERR(xs)) {
6202 			XS_SETERR(xs, HBA_NOERROR);
6203 		}
6204 		return;
6205 
6206 	case RQCS_INCOMPLETE:
6207 		if ((sp->req_state_flags & RQSF_GOT_TARGET) == 0) {
6208 			isp_xs_prt(isp, xs, ISP_LOG_WARN1, "Selection Timeout @ %s:%d", __func__, __LINE__);
6209 			if (XS_NOERR(xs)) {
6210 				XS_SETERR(xs, HBA_SELTIMEOUT);
6211 				*rp = XS_XFRLEN(xs);
6212 			}
6213 			return;
6214 		}
6215 		isp_xs_prt(isp, xs, ISP_LOGERR, "Command Incomplete, state 0x%x", sp->req_state_flags);
6216 		break;
6217 
6218 	case RQCS_DMA_ERROR:
6219 		isp_xs_prt(isp, xs, ISP_LOGERR, "DMA Error");
6220 		*rp = XS_XFRLEN(xs);
6221 		break;
6222 
6223 	case RQCS_TRANSPORT_ERROR:
6224 	{
6225 		char buf[172];
6226 		ISP_SNPRINTF(buf, sizeof (buf), "states=>");
6227 		if (sp->req_state_flags & RQSF_GOT_BUS) {
6228 			ISP_SNPRINTF(buf, sizeof (buf), "%s GOT_BUS", buf);
6229 		}
6230 		if (sp->req_state_flags & RQSF_GOT_TARGET) {
6231 			ISP_SNPRINTF(buf, sizeof (buf), "%s GOT_TGT", buf);
6232 		}
6233 		if (sp->req_state_flags & RQSF_SENT_CDB) {
6234 			ISP_SNPRINTF(buf, sizeof (buf), "%s SENT_CDB", buf);
6235 		}
6236 		if (sp->req_state_flags & RQSF_XFRD_DATA) {
6237 			ISP_SNPRINTF(buf, sizeof (buf), "%s XFRD_DATA", buf);
6238 		}
6239 		if (sp->req_state_flags & RQSF_GOT_STATUS) {
6240 			ISP_SNPRINTF(buf, sizeof (buf), "%s GOT_STS", buf);
6241 		}
6242 		if (sp->req_state_flags & RQSF_GOT_SENSE) {
6243 			ISP_SNPRINTF(buf, sizeof (buf), "%s GOT_SNS", buf);
6244 		}
6245 		if (sp->req_state_flags & RQSF_XFER_COMPLETE) {
6246 			ISP_SNPRINTF(buf, sizeof (buf), "%s XFR_CMPLT", buf);
6247 		}
6248 		ISP_SNPRINTF(buf, sizeof (buf), "%s\nstatus=>", buf);
6249 		if (sp->req_status_flags & RQSTF_DISCONNECT) {
6250 			ISP_SNPRINTF(buf, sizeof (buf), "%s Disconnect", buf);
6251 		}
6252 		if (sp->req_status_flags & RQSTF_SYNCHRONOUS) {
6253 			ISP_SNPRINTF(buf, sizeof (buf), "%s Sync_xfr", buf);
6254 		}
6255 		if (sp->req_status_flags & RQSTF_PARITY_ERROR) {
6256 			ISP_SNPRINTF(buf, sizeof (buf), "%s Parity", buf);
6257 		}
6258 		if (sp->req_status_flags & RQSTF_BUS_RESET) {
6259 			ISP_SNPRINTF(buf, sizeof (buf), "%s Bus_Reset", buf);
6260 		}
6261 		if (sp->req_status_flags & RQSTF_DEVICE_RESET) {
6262 			ISP_SNPRINTF(buf, sizeof (buf), "%s Device_Reset", buf);
6263 		}
6264 		if (sp->req_status_flags & RQSTF_ABORTED) {
6265 			ISP_SNPRINTF(buf, sizeof (buf), "%s Aborted", buf);
6266 		}
6267 		if (sp->req_status_flags & RQSTF_TIMEOUT) {
6268 			ISP_SNPRINTF(buf, sizeof (buf), "%s Timeout", buf);
6269 		}
6270 		if (sp->req_status_flags & RQSTF_NEGOTIATION) {
6271 			ISP_SNPRINTF(buf, sizeof (buf), "%s Negotiation", buf);
6272 		}
6273 		isp_xs_prt(isp, xs,  ISP_LOGERR, "Transport Error: %s", buf);
6274 		*rp = XS_XFRLEN(xs);
6275 		break;
6276 	}
6277 	case RQCS_RESET_OCCURRED:
6278 	{
6279 		int chan;
6280 		isp_xs_prt(isp, xs, ISP_LOGWARN, "Bus Reset destroyed command");
6281 		for (chan = 0; chan < isp->isp_nchan; chan++) {
6282 			FCPARAM(isp, chan)->sendmarker = 1;
6283 		}
6284 		if (XS_NOERR(xs)) {
6285 			XS_SETERR(xs, HBA_BUSRESET);
6286 		}
6287 		*rp = XS_XFRLEN(xs);
6288 		return;
6289 	}
6290 	case RQCS_ABORTED:
6291 		isp_xs_prt(isp, xs, ISP_LOGERR, "Command Aborted");
6292 		ISP_SET_SENDMARKER(isp, XS_CHANNEL(xs), 1);
6293 		if (XS_NOERR(xs)) {
6294 			XS_SETERR(xs, HBA_ABORTED);
6295 		}
6296 		return;
6297 
6298 	case RQCS_TIMEOUT:
6299 		isp_xs_prt(isp, xs, ISP_LOGWARN, "Command timed out");
6300 		/*
6301 	 	 * XXX: Check to see if we logged out of the device.
6302 		 */
6303 		if (XS_NOERR(xs)) {
6304 			XS_SETERR(xs, HBA_CMDTIMEOUT);
6305 		}
6306 		return;
6307 
6308 	case RQCS_DATA_OVERRUN:
6309 		XS_SET_RESID(xs, sp->req_resid);
6310 		isp_xs_prt(isp, xs, ISP_LOGERR, "data overrun (%ld)", (long) XS_GET_RESID(xs));
6311 		if (XS_NOERR(xs)) {
6312 			XS_SETERR(xs, HBA_DATAOVR);
6313 		}
6314 		return;
6315 
6316 	case RQCS_COMMAND_OVERRUN:
6317 		isp_xs_prt(isp, xs, ISP_LOGERR, "command overrun");
6318 		break;
6319 
6320 	case RQCS_STATUS_OVERRUN:
6321 		isp_xs_prt(isp, xs, ISP_LOGERR, "status overrun");
6322 		break;
6323 
6324 	case RQCS_BAD_MESSAGE:
6325 		isp_xs_prt(isp, xs, ISP_LOGERR, "msg not COMMAND COMPLETE after status");
6326 		break;
6327 
6328 	case RQCS_NO_MESSAGE_OUT:
6329 		isp_xs_prt(isp, xs, ISP_LOGERR, "No MESSAGE OUT phase after selection");
6330 		break;
6331 
6332 	case RQCS_EXT_ID_FAILED:
6333 		isp_xs_prt(isp, xs, ISP_LOGERR, "EXTENDED IDENTIFY failed");
6334 		break;
6335 
6336 	case RQCS_IDE_MSG_FAILED:
6337 		isp_xs_prt(isp, xs, ISP_LOGERR, "INITIATOR DETECTED ERROR rejected");
6338 		break;
6339 
6340 	case RQCS_ABORT_MSG_FAILED:
6341 		isp_xs_prt(isp, xs, ISP_LOGERR, "ABORT OPERATION rejected");
6342 		break;
6343 
6344 	case RQCS_REJECT_MSG_FAILED:
6345 		isp_xs_prt(isp, xs, ISP_LOGERR, "MESSAGE REJECT rejected");
6346 		break;
6347 
6348 	case RQCS_NOP_MSG_FAILED:
6349 		isp_xs_prt(isp, xs, ISP_LOGERR, "NOP rejected");
6350 		break;
6351 
6352 	case RQCS_PARITY_ERROR_MSG_FAILED:
6353 		isp_xs_prt(isp, xs, ISP_LOGERR, "MESSAGE PARITY ERROR rejected");
6354 		break;
6355 
6356 	case RQCS_DEVICE_RESET_MSG_FAILED:
6357 		isp_xs_prt(isp, xs, ISP_LOGWARN, "BUS DEVICE RESET rejected");
6358 		break;
6359 
6360 	case RQCS_ID_MSG_FAILED:
6361 		isp_xs_prt(isp, xs, ISP_LOGERR, "IDENTIFY rejected");
6362 		break;
6363 
6364 	case RQCS_UNEXP_BUS_FREE:
6365 		isp_xs_prt(isp, xs, ISP_LOGERR, "Unexpected Bus Free");
6366 		break;
6367 
6368 	case RQCS_DATA_UNDERRUN:
6369 	{
6370 		if (IS_FC(isp)) {
6371 			int ru_marked = (sp->req_scsi_status & RQCS_RU) != 0;
6372 			if (!ru_marked || sp->req_resid > XS_XFRLEN(xs)) {
6373 				isp_xs_prt(isp, xs, ISP_LOGWARN, bun, XS_XFRLEN(xs), sp->req_resid, (ru_marked)? "marked" : "not marked");
6374 				if (XS_NOERR(xs)) {
6375 					XS_SETERR(xs, HBA_BOTCH);
6376 				}
6377 				return;
6378 			}
6379 		}
6380 		XS_SET_RESID(xs, sp->req_resid);
6381 		if (XS_NOERR(xs)) {
6382 			XS_SETERR(xs, HBA_NOERROR);
6383 		}
6384 		return;
6385 	}
6386 
6387 	case RQCS_XACT_ERR1:
6388 		isp_xs_prt(isp, xs, ISP_LOGERR, "HBA attempted queued transaction with disconnect not set");
6389 		break;
6390 
6391 	case RQCS_XACT_ERR2:
6392 		isp_xs_prt(isp, xs, ISP_LOGERR,
6393 		    "HBA attempted queued transaction to target routine %jx",
6394 		    (uintmax_t)XS_LUN(xs));
6395 		break;
6396 
6397 	case RQCS_XACT_ERR3:
6398 		isp_xs_prt(isp, xs, ISP_LOGERR, "HBA attempted queued cmd when queueing disabled");
6399 		break;
6400 
6401 	case RQCS_BAD_ENTRY:
6402 		isp_prt(isp, ISP_LOGERR, "Invalid IOCB entry type detected");
6403 		break;
6404 
6405 	case RQCS_QUEUE_FULL:
6406 		isp_xs_prt(isp, xs, ISP_LOG_WARN1, "internal queues full status 0x%x", *XS_STSP(xs));
6407 
6408 		/*
6409 		 * If QFULL or some other status byte is set, then this
6410 		 * isn't an error, per se.
6411 		 *
6412 		 * Unfortunately, some QLogic f/w writers have, in
6413 		 * some cases, ommitted to *set* status to QFULL.
6414 		 */
6415 #if	0
6416 		if (*XS_STSP(xs) != SCSI_GOOD && XS_NOERR(xs)) {
6417 			XS_SETERR(xs, HBA_NOERROR);
6418 			return;
6419 		}
6420 
6421 #endif
6422 		*XS_STSP(xs) = SCSI_QFULL;
6423 		XS_SETERR(xs, HBA_NOERROR);
6424 		return;
6425 
6426 	case RQCS_PHASE_SKIPPED:
6427 		isp_xs_prt(isp, xs, ISP_LOGERR, "SCSI phase skipped");
6428 		break;
6429 
6430 	case RQCS_ARQS_FAILED:
6431 		isp_xs_prt(isp, xs, ISP_LOGERR, "Auto Request Sense Failed");
6432 		if (XS_NOERR(xs)) {
6433 			XS_SETERR(xs, HBA_ARQFAIL);
6434 		}
6435 		return;
6436 
6437 	case RQCS_WIDE_FAILED:
6438 		isp_xs_prt(isp, xs, ISP_LOGERR, "Wide Negotiation Failed");
6439 		if (IS_SCSI(isp)) {
6440 			sdparam *sdp = SDPARAM(isp, XS_CHANNEL(xs));
6441 			sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_WIDE;
6442 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
6443 			sdp->update = 1;
6444 		}
6445 		if (XS_NOERR(xs)) {
6446 			XS_SETERR(xs, HBA_NOERROR);
6447 		}
6448 		return;
6449 
6450 	case RQCS_SYNCXFER_FAILED:
6451 		isp_xs_prt(isp, xs, ISP_LOGERR, "SDTR Message Failed");
6452 		if (IS_SCSI(isp)) {
6453 			sdparam *sdp = SDPARAM(isp, XS_CHANNEL(xs));
6454 			sdp += XS_CHANNEL(xs);
6455 			sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_SYNC;
6456 			sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
6457 			sdp->update = 1;
6458 		}
6459 		break;
6460 
6461 	case RQCS_LVD_BUSERR:
6462 		isp_xs_prt(isp, xs, ISP_LOGERR, "Bad LVD condition");
6463 		break;
6464 
6465 	case RQCS_PORT_UNAVAILABLE:
6466 		/*
6467 		 * No such port on the loop. Moral equivalent of SELTIMEO
6468 		 */
6469 	case RQCS_PORT_LOGGED_OUT:
6470 	{
6471 		const char *reason;
6472 		uint8_t sts = sp->req_completion_status & 0xff;
6473 
6474 		/*
6475 		 * It was there (maybe)- treat as a selection timeout.
6476 		 */
6477 		if (sts == RQCS_PORT_UNAVAILABLE) {
6478 			reason = "unavailable";
6479 		} else {
6480 			reason = "logout";
6481 		}
6482 
6483 		isp_prt(isp, ISP_LOGINFO, "port %s for target %d", reason, XS_TGT(xs));
6484 
6485 		/*
6486 		 * If we're on a local loop, force a LIP (which is overkill)
6487 		 * to force a re-login of this unit. If we're on fabric,
6488 		 * then we'll have to log in again as a matter of course.
6489 		 */
6490 		if (FCPARAM(isp, 0)->isp_topo == TOPO_NL_PORT ||
6491 		    FCPARAM(isp, 0)->isp_topo == TOPO_FL_PORT) {
6492 			mbreg_t mbs;
6493 			MBSINIT(&mbs, MBOX_INIT_LIP, MBLOGALL, 0);
6494 			if (ISP_CAP_2KLOGIN(isp)) {
6495 				mbs.ibits = (1 << 10);
6496 			}
6497 			isp_mboxcmd_qnw(isp, &mbs, 1);
6498 		}
6499 		if (XS_NOERR(xs)) {
6500 			XS_SETERR(xs, HBA_SELTIMEOUT);
6501 		}
6502 		return;
6503 	}
6504 	case RQCS_PORT_CHANGED:
6505 		isp_prt(isp, ISP_LOGWARN, "port changed for target %d", XS_TGT(xs));
6506 		if (XS_NOERR(xs)) {
6507 			XS_SETERR(xs, HBA_SELTIMEOUT);
6508 		}
6509 		return;
6510 
6511 	case RQCS_PORT_BUSY:
6512 		isp_prt(isp, ISP_LOGWARN, "port busy for target %d", XS_TGT(xs));
6513 		if (XS_NOERR(xs)) {
6514 			XS_SETERR(xs, HBA_TGTBSY);
6515 		}
6516 		return;
6517 
6518 	default:
6519 		isp_prt(isp, ISP_LOGERR, "Unknown Completion Status 0x%x", sp->req_completion_status);
6520 		break;
6521 	}
6522 	if (XS_NOERR(xs)) {
6523 		XS_SETERR(xs, HBA_BOTCH);
6524 	}
6525 }
6526 
6527 static void
6528 isp_parse_status_24xx(ispsoftc_t *isp, isp24xx_statusreq_t *sp, XS_T *xs, long *rp)
6529 {
6530 	int ru_marked, sv_marked;
6531 	int chan = XS_CHANNEL(xs);
6532 
6533 	switch (sp->req_completion_status) {
6534 	case RQCS_COMPLETE:
6535 		if (XS_NOERR(xs)) {
6536 			XS_SETERR(xs, HBA_NOERROR);
6537 		}
6538 		return;
6539 
6540 	case RQCS_DMA_ERROR:
6541 		isp_xs_prt(isp, xs, ISP_LOGERR, "DMA error");
6542 		break;
6543 
6544 	case RQCS_TRANSPORT_ERROR:
6545 		isp_xs_prt(isp, xs,  ISP_LOGERR, "Transport Error");
6546 		break;
6547 
6548 	case RQCS_RESET_OCCURRED:
6549 		isp_xs_prt(isp, xs, ISP_LOGWARN, "reset destroyed command");
6550 		FCPARAM(isp, chan)->sendmarker = 1;
6551 		if (XS_NOERR(xs)) {
6552 			XS_SETERR(xs, HBA_BUSRESET);
6553 		}
6554 		return;
6555 
6556 	case RQCS_ABORTED:
6557 		isp_xs_prt(isp, xs, ISP_LOGERR, "Command Aborted");
6558 		FCPARAM(isp, chan)->sendmarker = 1;
6559 		if (XS_NOERR(xs)) {
6560 			XS_SETERR(xs, HBA_ABORTED);
6561 		}
6562 		return;
6563 
6564 	case RQCS_TIMEOUT:
6565 		isp_xs_prt(isp, xs, ISP_LOGWARN, "Command Timed Out");
6566 		if (XS_NOERR(xs)) {
6567 			XS_SETERR(xs, HBA_CMDTIMEOUT);
6568 		}
6569 		return;
6570 
6571 	case RQCS_DATA_OVERRUN:
6572 		XS_SET_RESID(xs, sp->req_resid);
6573 		isp_xs_prt(isp, xs, ISP_LOGERR, "Data Overrun");
6574 		if (XS_NOERR(xs)) {
6575 			XS_SETERR(xs, HBA_DATAOVR);
6576 		}
6577 		return;
6578 
6579 	case RQCS_24XX_DRE:	/* data reassembly error */
6580 		isp_prt(isp, ISP_LOGERR, "Chan %d data reassembly error for target %d", chan, XS_TGT(xs));
6581 		if (XS_NOERR(xs)) {
6582 			XS_SETERR(xs, HBA_ABORTED);
6583 		}
6584 		*rp = XS_XFRLEN(xs);
6585 		return;
6586 
6587 	case RQCS_24XX_TABORT:	/* aborted by target */
6588 		isp_prt(isp, ISP_LOGERR, "Chan %d target %d sent ABTS", chan, XS_TGT(xs));
6589 		if (XS_NOERR(xs)) {
6590 			XS_SETERR(xs, HBA_ABORTED);
6591 		}
6592 		return;
6593 
6594 	case RQCS_DATA_UNDERRUN:
6595 		ru_marked = (sp->req_scsi_status & RQCS_RU) != 0;
6596 		/*
6597 		 * We can get an underrun w/o things being marked
6598 		 * if we got a non-zero status.
6599 		 */
6600 		sv_marked = (sp->req_scsi_status & (RQCS_SV|RQCS_RV)) != 0;
6601 		if ((ru_marked == 0 && sv_marked == 0) ||
6602 		    (sp->req_resid > XS_XFRLEN(xs))) {
6603 			isp_xs_prt(isp, xs, ISP_LOGWARN, bun, XS_XFRLEN(xs), sp->req_resid, (ru_marked)? "marked" : "not marked");
6604 			if (XS_NOERR(xs)) {
6605 				XS_SETERR(xs, HBA_BOTCH);
6606 			}
6607 			return;
6608 		}
6609 		XS_SET_RESID(xs, sp->req_resid);
6610 		isp_xs_prt(isp, xs, ISP_LOG_WARN1, "Data Underrun (%d) for command 0x%x", sp->req_resid, XS_CDBP(xs)[0] & 0xff);
6611 		if (XS_NOERR(xs)) {
6612 			XS_SETERR(xs, HBA_NOERROR);
6613 		}
6614 		return;
6615 
6616 	case RQCS_PORT_UNAVAILABLE:
6617 		/*
6618 		 * No such port on the loop. Moral equivalent of SELTIMEO
6619 		 */
6620 	case RQCS_PORT_LOGGED_OUT:
6621 	{
6622 		const char *reason;
6623 		uint8_t sts = sp->req_completion_status & 0xff;
6624 
6625 		/*
6626 		 * It was there (maybe)- treat as a selection timeout.
6627 		 */
6628 		if (sts == RQCS_PORT_UNAVAILABLE) {
6629 			reason = "unavailable";
6630 		} else {
6631 			reason = "logout";
6632 		}
6633 
6634 		isp_prt(isp, ISP_LOGINFO, "Chan %d port %s for target %d",
6635 		    chan, reason, XS_TGT(xs));
6636 
6637 		/*
6638 		 * There is no MBOX_INIT_LIP for the 24XX.
6639 		 */
6640 		if (XS_NOERR(xs)) {
6641 			XS_SETERR(xs, HBA_SELTIMEOUT);
6642 		}
6643 		return;
6644 	}
6645 	case RQCS_PORT_CHANGED:
6646 		isp_prt(isp, ISP_LOGWARN, "port changed for target %d chan %d", XS_TGT(xs), chan);
6647 		if (XS_NOERR(xs)) {
6648 			XS_SETERR(xs, HBA_SELTIMEOUT);
6649 		}
6650 		return;
6651 
6652 
6653 	case RQCS_24XX_ENOMEM:	/* f/w resource unavailable */
6654 		isp_prt(isp, ISP_LOGWARN, "f/w resource unavailable for target %d chan %d", XS_TGT(xs), chan);
6655 		if (XS_NOERR(xs)) {
6656 			*XS_STSP(xs) = SCSI_BUSY;
6657 			XS_SETERR(xs, HBA_TGTBSY);
6658 		}
6659 		return;
6660 
6661 	case RQCS_24XX_TMO:	/* task management overrun */
6662 		isp_prt(isp, ISP_LOGWARN, "command for target %d overlapped task management for chan %d", XS_TGT(xs), chan);
6663 		if (XS_NOERR(xs)) {
6664 			*XS_STSP(xs) = SCSI_BUSY;
6665 			XS_SETERR(xs, HBA_TGTBSY);
6666 		}
6667 		return;
6668 
6669 	default:
6670 		isp_prt(isp, ISP_LOGERR, "Unknown Completion Status 0x%x on chan %d", sp->req_completion_status, chan);
6671 		break;
6672 	}
6673 	if (XS_NOERR(xs)) {
6674 		XS_SETERR(xs, HBA_BOTCH);
6675 	}
6676 }
6677 
6678 static void
6679 isp_fastpost_complete(ispsoftc_t *isp, uint32_t fph)
6680 {
6681 	XS_T *xs;
6682 
6683 	if (fph == 0) {
6684 		return;
6685 	}
6686 	xs = isp_find_xs(isp, fph);
6687 	if (xs == NULL) {
6688 		isp_prt(isp, ISP_LOGWARN,
6689 		    "Command for fast post handle 0x%x not found", fph);
6690 		return;
6691 	}
6692 	isp_destroy_handle(isp, fph);
6693 
6694 	/*
6695 	 * Since we don't have a result queue entry item,
6696 	 * we must believe that SCSI status is zero and
6697 	 * that all data transferred.
6698 	 */
6699 	XS_SET_RESID(xs, 0);
6700 	*XS_STSP(xs) = SCSI_GOOD;
6701 	if (XS_XFRLEN(xs)) {
6702 		ISP_DMAFREE(isp, xs, fph);
6703 	}
6704 	if (isp->isp_nactive) {
6705 		isp->isp_nactive--;
6706 	}
6707 	isp->isp_fphccmplt++;
6708 	isp_done(xs);
6709 }
6710 
6711 static int
6712 isp_mbox_continue(ispsoftc_t *isp)
6713 {
6714 	mbreg_t mbs;
6715 	uint16_t *ptr;
6716 	uint32_t offset;
6717 
6718 	switch (isp->isp_lastmbxcmd) {
6719 	case MBOX_WRITE_RAM_WORD:
6720 	case MBOX_READ_RAM_WORD:
6721 	case MBOX_WRITE_RAM_WORD_EXTENDED:
6722 	case MBOX_READ_RAM_WORD_EXTENDED:
6723 		break;
6724 	default:
6725 		return (1);
6726 	}
6727 	if (isp->isp_mboxtmp[0] != MBOX_COMMAND_COMPLETE) {
6728 		isp->isp_mbxwrk0 = 0;
6729 		return (-1);
6730 	}
6731 
6732 	/*
6733 	 * Clear the previous interrupt.
6734 	 */
6735 	if (IS_24XX(isp)) {
6736 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
6737 	} else {
6738 		ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
6739 		ISP_WRITE(isp, BIU_SEMA, 0);
6740 	}
6741 
6742 	/*
6743 	 * Continue with next word.
6744 	 */
6745 	ISP_MEMZERO(&mbs, sizeof (mbs));
6746 	ptr = isp->isp_mbxworkp;
6747 	switch (isp->isp_lastmbxcmd) {
6748 	case MBOX_WRITE_RAM_WORD:
6749 		mbs.param[1] = isp->isp_mbxwrk1++;
6750 		mbs.param[2] = *ptr++;
6751 		break;
6752 	case MBOX_READ_RAM_WORD:
6753 		*ptr++ = isp->isp_mboxtmp[2];
6754 		mbs.param[1] = isp->isp_mbxwrk1++;
6755 		break;
6756 	case MBOX_WRITE_RAM_WORD_EXTENDED:
6757 		if (IS_24XX(isp)) {
6758 			uint32_t *lptr = (uint32_t *)ptr;
6759 			mbs.param[2] = lptr[0];
6760 			mbs.param[3] = lptr[0] >> 16;
6761 			lptr++;
6762 			ptr = (uint16_t *)lptr;
6763 		} else {
6764 			mbs.param[2] = *ptr++;
6765 		}
6766 		offset = isp->isp_mbxwrk1;
6767 		offset |= isp->isp_mbxwrk8 << 16;
6768 		mbs.param[1] = offset;
6769 		mbs.param[8] = offset >> 16;
6770 		offset++;
6771 		isp->isp_mbxwrk1 = offset;
6772 		isp->isp_mbxwrk8 = offset >> 16;
6773 		break;
6774 	case MBOX_READ_RAM_WORD_EXTENDED:
6775 		if (IS_24XX(isp)) {
6776 			uint32_t *lptr = (uint32_t *)ptr;
6777 			uint32_t val = isp->isp_mboxtmp[2];
6778 			val |= (isp->isp_mboxtmp[3]) << 16;
6779 			*lptr++ = val;
6780 			ptr = (uint16_t *)lptr;
6781 		} else {
6782 			*ptr++ = isp->isp_mboxtmp[2];
6783 		}
6784 		offset = isp->isp_mbxwrk1;
6785 		offset |= isp->isp_mbxwrk8 << 16;
6786 		mbs.param[1] = offset;
6787 		mbs.param[8] = offset >> 16;
6788 		offset++;
6789 		isp->isp_mbxwrk1 = offset;
6790 		isp->isp_mbxwrk8 = offset >> 16;
6791 		break;
6792 	}
6793 	isp->isp_mbxworkp = ptr;
6794 	isp->isp_mbxwrk0--;
6795 	mbs.param[0] = isp->isp_lastmbxcmd;
6796 	mbs.logval = MBLOGALL;
6797 	isp_mboxcmd_qnw(isp, &mbs, 0);
6798 	return (0);
6799 }
6800 
6801 #define	ISP_SCSI_IBITS(op)		(mbpscsi[((op)<<1)])
6802 #define	ISP_SCSI_OBITS(op)		(mbpscsi[((op)<<1) + 1])
6803 #define	ISP_SCSI_OPMAP(in, out)		in, out
6804 static const uint8_t mbpscsi[] = {
6805 	ISP_SCSI_OPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
6806 	ISP_SCSI_OPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
6807 	ISP_SCSI_OPMAP(0x03, 0x01),	/* 0x02: MBOX_EXEC_FIRMWARE */
6808 	ISP_SCSI_OPMAP(0x1f, 0x01),	/* 0x03: MBOX_DUMP_RAM */
6809 	ISP_SCSI_OPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
6810 	ISP_SCSI_OPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
6811 	ISP_SCSI_OPMAP(0x3f, 0x3f),	/* 0x06: MBOX_MAILBOX_REG_TEST */
6812 	ISP_SCSI_OPMAP(0x07, 0x07),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
6813 	ISP_SCSI_OPMAP(0x01, 0x0f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
6814 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x09: */
6815 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x0a: */
6816 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x0b: */
6817 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x0c: */
6818 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x0d: */
6819 	ISP_SCSI_OPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
6820 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x0f: */
6821 	ISP_SCSI_OPMAP(0x1f, 0x1f),	/* 0x10: MBOX_INIT_REQ_QUEUE */
6822 	ISP_SCSI_OPMAP(0x3f, 0x3f),	/* 0x11: MBOX_INIT_RES_QUEUE */
6823 	ISP_SCSI_OPMAP(0x0f, 0x0f),	/* 0x12: MBOX_EXECUTE_IOCB */
6824 	ISP_SCSI_OPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
6825 	ISP_SCSI_OPMAP(0x01, 0x3f),	/* 0x14: MBOX_STOP_FIRMWARE */
6826 	ISP_SCSI_OPMAP(0x0f, 0x0f),	/* 0x15: MBOX_ABORT */
6827 	ISP_SCSI_OPMAP(0x03, 0x03),	/* 0x16: MBOX_ABORT_DEVICE */
6828 	ISP_SCSI_OPMAP(0x07, 0x07),	/* 0x17: MBOX_ABORT_TARGET */
6829 	ISP_SCSI_OPMAP(0x07, 0x07),	/* 0x18: MBOX_BUS_RESET */
6830 	ISP_SCSI_OPMAP(0x03, 0x07),	/* 0x19: MBOX_STOP_QUEUE */
6831 	ISP_SCSI_OPMAP(0x03, 0x07),	/* 0x1a: MBOX_START_QUEUE */
6832 	ISP_SCSI_OPMAP(0x03, 0x07),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
6833 	ISP_SCSI_OPMAP(0x03, 0x07),	/* 0x1c: MBOX_ABORT_QUEUE */
6834 	ISP_SCSI_OPMAP(0x03, 0x4f),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
6835 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x1e: */
6836 	ISP_SCSI_OPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
6837 	ISP_SCSI_OPMAP(0x01, 0x07),	/* 0x20: MBOX_GET_INIT_SCSI_ID */
6838 	ISP_SCSI_OPMAP(0x01, 0x07),	/* 0x21: MBOX_GET_SELECT_TIMEOUT */
6839 	ISP_SCSI_OPMAP(0x01, 0xc7),	/* 0x22: MBOX_GET_RETRY_COUNT	*/
6840 	ISP_SCSI_OPMAP(0x01, 0x07),	/* 0x23: MBOX_GET_TAG_AGE_LIMIT */
6841 	ISP_SCSI_OPMAP(0x01, 0x03),	/* 0x24: MBOX_GET_CLOCK_RATE */
6842 	ISP_SCSI_OPMAP(0x01, 0x07),	/* 0x25: MBOX_GET_ACT_NEG_STATE */
6843 	ISP_SCSI_OPMAP(0x01, 0x07),	/* 0x26: MBOX_GET_ASYNC_DATA_SETUP_TIME */
6844 	ISP_SCSI_OPMAP(0x01, 0x07),	/* 0x27: MBOX_GET_PCI_PARAMS */
6845 	ISP_SCSI_OPMAP(0x03, 0x4f),	/* 0x28: MBOX_GET_TARGET_PARAMS */
6846 	ISP_SCSI_OPMAP(0x03, 0x0f),	/* 0x29: MBOX_GET_DEV_QUEUE_PARAMS */
6847 	ISP_SCSI_OPMAP(0x01, 0x07),	/* 0x2a: MBOX_GET_RESET_DELAY_PARAMS */
6848 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x2b: */
6849 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x2c: */
6850 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x2d: */
6851 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x2e: */
6852 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x2f: */
6853 	ISP_SCSI_OPMAP(0x03, 0x03),	/* 0x30: MBOX_SET_INIT_SCSI_ID */
6854 	ISP_SCSI_OPMAP(0x07, 0x07),	/* 0x31: MBOX_SET_SELECT_TIMEOUT */
6855 	ISP_SCSI_OPMAP(0xc7, 0xc7),	/* 0x32: MBOX_SET_RETRY_COUNT	*/
6856 	ISP_SCSI_OPMAP(0x07, 0x07),	/* 0x33: MBOX_SET_TAG_AGE_LIMIT */
6857 	ISP_SCSI_OPMAP(0x03, 0x03),	/* 0x34: MBOX_SET_CLOCK_RATE */
6858 	ISP_SCSI_OPMAP(0x07, 0x07),	/* 0x35: MBOX_SET_ACT_NEG_STATE */
6859 	ISP_SCSI_OPMAP(0x07, 0x07),	/* 0x36: MBOX_SET_ASYNC_DATA_SETUP_TIME */
6860 	ISP_SCSI_OPMAP(0x07, 0x07),	/* 0x37: MBOX_SET_PCI_CONTROL_PARAMS */
6861 	ISP_SCSI_OPMAP(0x4f, 0x4f),	/* 0x38: MBOX_SET_TARGET_PARAMS */
6862 	ISP_SCSI_OPMAP(0x0f, 0x0f),	/* 0x39: MBOX_SET_DEV_QUEUE_PARAMS */
6863 	ISP_SCSI_OPMAP(0x07, 0x07),	/* 0x3a: MBOX_SET_RESET_DELAY_PARAMS */
6864 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x3b: */
6865 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x3c: */
6866 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x3d: */
6867 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x3e: */
6868 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x3f: */
6869 	ISP_SCSI_OPMAP(0x01, 0x03),	/* 0x40: MBOX_RETURN_BIOS_BLOCK_ADDR */
6870 	ISP_SCSI_OPMAP(0x3f, 0x01),	/* 0x41: MBOX_WRITE_FOUR_RAM_WORDS */
6871 	ISP_SCSI_OPMAP(0x03, 0x07),	/* 0x42: MBOX_EXEC_BIOS_IOCB */
6872 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x43: */
6873 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x44: */
6874 	ISP_SCSI_OPMAP(0x03, 0x03),	/* 0x45: SET SYSTEM PARAMETER */
6875 	ISP_SCSI_OPMAP(0x01, 0x03),	/* 0x46: GET SYSTEM PARAMETER */
6876 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x47: */
6877 	ISP_SCSI_OPMAP(0x01, 0xcf),	/* 0x48: GET SCAM CONFIGURATION */
6878 	ISP_SCSI_OPMAP(0xcf, 0xcf),	/* 0x49: SET SCAM CONFIGURATION */
6879 	ISP_SCSI_OPMAP(0x03, 0x03),	/* 0x4a: MBOX_SET_FIRMWARE_FEATURES */
6880 	ISP_SCSI_OPMAP(0x01, 0x03),	/* 0x4b: MBOX_GET_FIRMWARE_FEATURES */
6881 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x4c: */
6882 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x4d: */
6883 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x4e: */
6884 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x4f: */
6885 	ISP_SCSI_OPMAP(0xdf, 0xdf),	/* 0x50: LOAD RAM A64 */
6886 	ISP_SCSI_OPMAP(0xdf, 0xdf),	/* 0x51: DUMP RAM A64 */
6887 	ISP_SCSI_OPMAP(0xdf, 0xff),	/* 0x52: INITIALIZE REQUEST QUEUE A64 */
6888 	ISP_SCSI_OPMAP(0xef, 0xff),	/* 0x53: INITIALIZE RESPONSE QUEUE A64 */
6889 	ISP_SCSI_OPMAP(0xcf, 0x01),	/* 0x54: EXECUCUTE COMMAND IOCB A64 */
6890 	ISP_SCSI_OPMAP(0x07, 0x01),	/* 0x55: ENABLE TARGET MODE */
6891 	ISP_SCSI_OPMAP(0x03, 0x0f),	/* 0x56: GET TARGET STATUS */
6892 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x57: */
6893 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x58: */
6894 	ISP_SCSI_OPMAP(0x00, 0x00),	/* 0x59: */
6895 	ISP_SCSI_OPMAP(0x03, 0x03),	/* 0x5a: SET DATA OVERRUN RECOVERY MODE */
6896 	ISP_SCSI_OPMAP(0x01, 0x03),	/* 0x5b: GET DATA OVERRUN RECOVERY MODE */
6897 	ISP_SCSI_OPMAP(0x0f, 0x0f),	/* 0x5c: SET HOST DATA */
6898 	ISP_SCSI_OPMAP(0x01, 0x01)	/* 0x5d: GET NOST DATA */
6899 };
6900 #define	MAX_SCSI_OPCODE	0x5d
6901 
6902 static const char *scsi_mbcmd_names[] = {
6903 	"NO-OP",
6904 	"LOAD RAM",
6905 	"EXEC FIRMWARE",
6906 	"DUMP RAM",
6907 	"WRITE RAM WORD",
6908 	"READ RAM WORD",
6909 	"MAILBOX REG TEST",
6910 	"VERIFY CHECKSUM",
6911 	"ABOUT FIRMWARE",
6912 	NULL,
6913 	NULL,
6914 	NULL,
6915 	NULL,
6916 	NULL,
6917 	"CHECK FIRMWARE",
6918 	NULL,
6919 	"INIT REQUEST QUEUE",
6920 	"INIT RESULT QUEUE",
6921 	"EXECUTE IOCB",
6922 	"WAKE UP",
6923 	"STOP FIRMWARE",
6924 	"ABORT",
6925 	"ABORT DEVICE",
6926 	"ABORT TARGET",
6927 	"BUS RESET",
6928 	"STOP QUEUE",
6929 	"START QUEUE",
6930 	"SINGLE STEP QUEUE",
6931 	"ABORT QUEUE",
6932 	"GET DEV QUEUE STATUS",
6933 	NULL,
6934 	"GET FIRMWARE STATUS",
6935 	"GET INIT SCSI ID",
6936 	"GET SELECT TIMEOUT",
6937 	"GET RETRY COUNT",
6938 	"GET TAG AGE LIMIT",
6939 	"GET CLOCK RATE",
6940 	"GET ACT NEG STATE",
6941 	"GET ASYNC DATA SETUP TIME",
6942 	"GET PCI PARAMS",
6943 	"GET TARGET PARAMS",
6944 	"GET DEV QUEUE PARAMS",
6945 	"GET RESET DELAY PARAMS",
6946 	NULL,
6947 	NULL,
6948 	NULL,
6949 	NULL,
6950 	NULL,
6951 	"SET INIT SCSI ID",
6952 	"SET SELECT TIMEOUT",
6953 	"SET RETRY COUNT",
6954 	"SET TAG AGE LIMIT",
6955 	"SET CLOCK RATE",
6956 	"SET ACT NEG STATE",
6957 	"SET ASYNC DATA SETUP TIME",
6958 	"SET PCI CONTROL PARAMS",
6959 	"SET TARGET PARAMS",
6960 	"SET DEV QUEUE PARAMS",
6961 	"SET RESET DELAY PARAMS",
6962 	NULL,
6963 	NULL,
6964 	NULL,
6965 	NULL,
6966 	NULL,
6967 	"RETURN BIOS BLOCK ADDR",
6968 	"WRITE FOUR RAM WORDS",
6969 	"EXEC BIOS IOCB",
6970 	NULL,
6971 	NULL,
6972 	"SET SYSTEM PARAMETER",
6973 	"GET SYSTEM PARAMETER",
6974 	NULL,
6975 	"GET SCAM CONFIGURATION",
6976 	"SET SCAM CONFIGURATION",
6977 	"SET FIRMWARE FEATURES",
6978 	"GET FIRMWARE FEATURES",
6979 	NULL,
6980 	NULL,
6981 	NULL,
6982 	NULL,
6983 	"LOAD RAM A64",
6984 	"DUMP RAM A64",
6985 	"INITIALIZE REQUEST QUEUE A64",
6986 	"INITIALIZE RESPONSE QUEUE A64",
6987 	"EXECUTE IOCB A64",
6988 	"ENABLE TARGET MODE",
6989 	"GET TARGET MODE STATE",
6990 	NULL,
6991 	NULL,
6992 	NULL,
6993 	"SET DATA OVERRUN RECOVERY MODE",
6994 	"GET DATA OVERRUN RECOVERY MODE",
6995 	"SET HOST DATA",
6996 	"GET NOST DATA",
6997 };
6998 
6999 #define	ISP_FC_IBITS(op)	((mbpfc[((op)<<3) + 0] << 24) | (mbpfc[((op)<<3) + 1] << 16) | (mbpfc[((op)<<3) + 2] << 8) | (mbpfc[((op)<<3) + 3]))
7000 #define	ISP_FC_OBITS(op)	((mbpfc[((op)<<3) + 4] << 24) | (mbpfc[((op)<<3) + 5] << 16) | (mbpfc[((op)<<3) + 6] << 8) | (mbpfc[((op)<<3) + 7]))
7001 
7002 #define	ISP_FC_OPMAP(in0, out0)							  0,   0,   0, in0,    0,    0,    0, out0
7003 #define	ISP_FC_OPMAP_HALF(in1, in0, out1, out0)					  0,   0, in1, in0,    0,    0, out1, out0
7004 #define	ISP_FC_OPMAP_FULL(in3, in2, in1, in0, out3, out2, out1, out0)		in3, in2, in1, in0, out3, out2, out1, out0
7005 static const uint32_t mbpfc[] = {
7006 	ISP_FC_OPMAP(0x01, 0x01),	/* 0x00: MBOX_NO_OP */
7007 	ISP_FC_OPMAP(0x1f, 0x01),	/* 0x01: MBOX_LOAD_RAM */
7008 	ISP_FC_OPMAP_HALF(0x07, 0xff, 0x00, 0x03),	/* 0x02: MBOX_EXEC_FIRMWARE */
7009 	ISP_FC_OPMAP(0xdf, 0x01),	/* 0x03: MBOX_DUMP_RAM */
7010 	ISP_FC_OPMAP(0x07, 0x07),	/* 0x04: MBOX_WRITE_RAM_WORD */
7011 	ISP_FC_OPMAP(0x03, 0x07),	/* 0x05: MBOX_READ_RAM_WORD */
7012 	ISP_FC_OPMAP_FULL(0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff),	/* 0x06: MBOX_MAILBOX_REG_TEST */
7013 	ISP_FC_OPMAP(0x07, 0x07),	/* 0x07: MBOX_VERIFY_CHECKSUM	*/
7014 	ISP_FC_OPMAP_FULL(0x0, 0x0, 0x0, 0x01, 0x0, 0x3, 0x80, 0x7f),	/* 0x08: MBOX_ABOUT_FIRMWARE */
7015 	ISP_FC_OPMAP(0xdf, 0x01),	/* 0x09: MBOX_LOAD_RISC_RAM_2100 */
7016 	ISP_FC_OPMAP(0xdf, 0x01),	/* 0x0a: DUMP RAM */
7017 	ISP_FC_OPMAP_HALF(0x1, 0xff, 0x0, 0x01),	/* 0x0b: MBOX_LOAD_RISC_RAM */
7018 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x0c: */
7019 	ISP_FC_OPMAP_HALF(0x1, 0x0f, 0x0, 0x01),	/* 0x0d: MBOX_WRITE_RAM_WORD_EXTENDED */
7020 	ISP_FC_OPMAP(0x01, 0x05),	/* 0x0e: MBOX_CHECK_FIRMWARE */
7021 	ISP_FC_OPMAP_HALF(0x1, 0x03, 0x0, 0x0d),	/* 0x0f: MBOX_READ_RAM_WORD_EXTENDED */
7022 	ISP_FC_OPMAP(0x1f, 0x11),	/* 0x10: MBOX_INIT_REQ_QUEUE */
7023 	ISP_FC_OPMAP(0x2f, 0x21),	/* 0x11: MBOX_INIT_RES_QUEUE */
7024 	ISP_FC_OPMAP(0x0f, 0x01),	/* 0x12: MBOX_EXECUTE_IOCB */
7025 	ISP_FC_OPMAP(0x03, 0x03),	/* 0x13: MBOX_WAKE_UP	*/
7026 	ISP_FC_OPMAP_HALF(0x1, 0xff, 0x0, 0x03),	/* 0x14: MBOX_STOP_FIRMWARE */
7027 	ISP_FC_OPMAP(0x4f, 0x01),	/* 0x15: MBOX_ABORT */
7028 	ISP_FC_OPMAP(0x07, 0x01),	/* 0x16: MBOX_ABORT_DEVICE */
7029 	ISP_FC_OPMAP(0x07, 0x01),	/* 0x17: MBOX_ABORT_TARGET */
7030 	ISP_FC_OPMAP(0x03, 0x03),	/* 0x18: MBOX_BUS_RESET */
7031 	ISP_FC_OPMAP(0x07, 0x05),	/* 0x19: MBOX_STOP_QUEUE */
7032 	ISP_FC_OPMAP(0x07, 0x05),	/* 0x1a: MBOX_START_QUEUE */
7033 	ISP_FC_OPMAP(0x07, 0x05),	/* 0x1b: MBOX_SINGLE_STEP_QUEUE */
7034 	ISP_FC_OPMAP(0x07, 0x05),	/* 0x1c: MBOX_ABORT_QUEUE */
7035 	ISP_FC_OPMAP(0x07, 0x03),	/* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
7036 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x1e: */
7037 	ISP_FC_OPMAP(0x01, 0x07),	/* 0x1f: MBOX_GET_FIRMWARE_STATUS */
7038 	ISP_FC_OPMAP_HALF(0x2, 0x01, 0x7e, 0xcf),	/* 0x20: MBOX_GET_LOOP_ID */
7039 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x21: */
7040 	ISP_FC_OPMAP(0x03, 0x4b),	/* 0x22: MBOX_GET_TIMEOUT_PARAMS */
7041 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x23: */
7042 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x24: */
7043 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x25: */
7044 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x26: */
7045 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x27: */
7046 	ISP_FC_OPMAP(0x01, 0x03),	/* 0x28: MBOX_GET_FIRMWARE_OPTIONS */
7047 	ISP_FC_OPMAP(0x03, 0x07),	/* 0x29: MBOX_GET_PORT_QUEUE_PARAMS */
7048 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x2a: */
7049 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x2b: */
7050 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x2c: */
7051 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x2d: */
7052 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x2e: */
7053 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x2f: */
7054 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x30: */
7055 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x31: */
7056 	ISP_FC_OPMAP(0x4b, 0x4b),	/* 0x32: MBOX_SET_TIMEOUT_PARAMS */
7057 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x33: */
7058 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x34: */
7059 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x35: */
7060 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x36: */
7061 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x37: */
7062 	ISP_FC_OPMAP(0x0f, 0x01),	/* 0x38: MBOX_SET_FIRMWARE_OPTIONS */
7063 	ISP_FC_OPMAP(0x0f, 0x07),	/* 0x39: MBOX_SET_PORT_QUEUE_PARAMS */
7064 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x3a: */
7065 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x3b: */
7066 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x3c: */
7067 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x3d: */
7068 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x3e: */
7069 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x3f: */
7070 	ISP_FC_OPMAP(0x03, 0x01),	/* 0x40: MBOX_LOOP_PORT_BYPASS */
7071 	ISP_FC_OPMAP(0x03, 0x01),	/* 0x41: MBOX_LOOP_PORT_ENABLE */
7072 	ISP_FC_OPMAP_HALF(0x0, 0x01, 0x3, 0xcf),	/* 0x42: MBOX_GET_RESOURCE_COUNT */
7073 	ISP_FC_OPMAP(0x01, 0x01),	/* 0x43: MBOX_REQUEST_OFFLINE_MODE */
7074 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x44: */
7075 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x45: */
7076 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x46: */
7077 	ISP_FC_OPMAP(0xcf, 0x03),	/* 0x47: GET PORT_DATABASE ENHANCED */
7078 	ISP_FC_OPMAP(0xcf, 0x0f),	/* 0x48: MBOX_INIT_FIRMWARE_MULTI_ID */
7079 	ISP_FC_OPMAP(0xcd, 0x01),	/* 0x49: MBOX_GET_VP_DATABASE */
7080 	ISP_FC_OPMAP_HALF(0x2, 0xcd, 0x0, 0x01),	/* 0x4a: MBOX_GET_VP_DATABASE_ENTRY */
7081 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x4b: */
7082 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x4c: */
7083 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x4d: */
7084 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x4e: */
7085 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x4f: */
7086 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x50: */
7087 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x51: */
7088 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x52: */
7089 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x53: */
7090 	ISP_FC_OPMAP(0xcf, 0x01),	/* 0x54: EXECUTE IOCB A64 */
7091 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x55: */
7092 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x56: */
7093 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x57: */
7094 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x58: */
7095 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x59: */
7096 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x5a: */
7097 	ISP_FC_OPMAP(0x03, 0x01),	/* 0x5b: MBOX_DRIVER_HEARTBEAT */
7098 	ISP_FC_OPMAP(0xcf, 0x01),	/* 0x5c: MBOX_FW_HEARTBEAT */
7099 	ISP_FC_OPMAP(0x07, 0x1f),	/* 0x5d: MBOX_GET_SET_DATA_RATE */
7100 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x5e: */
7101 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x5f: */
7102 	ISP_FC_OPMAP(0xcf, 0x0f),	/* 0x60: MBOX_INIT_FIRMWARE */
7103 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x61: */
7104 	ISP_FC_OPMAP(0x01, 0x01),	/* 0x62: MBOX_INIT_LIP */
7105 	ISP_FC_OPMAP(0xcd, 0x03),	/* 0x63: MBOX_GET_FC_AL_POSITION_MAP */
7106 	ISP_FC_OPMAP(0xcf, 0x01),	/* 0x64: MBOX_GET_PORT_DB */
7107 	ISP_FC_OPMAP(0x07, 0x01),	/* 0x65: MBOX_CLEAR_ACA */
7108 	ISP_FC_OPMAP(0x07, 0x01),	/* 0x66: MBOX_TARGET_RESET */
7109 	ISP_FC_OPMAP(0x07, 0x01),	/* 0x67: MBOX_CLEAR_TASK_SET */
7110 	ISP_FC_OPMAP(0x07, 0x01),	/* 0x68: MBOX_ABORT_TASK_SET */
7111 	ISP_FC_OPMAP(0x01, 0x07),	/* 0x69: MBOX_GET_FW_STATE */
7112 	ISP_FC_OPMAP_HALF(0x6, 0x03, 0x0, 0xcf),	/* 0x6a: MBOX_GET_PORT_NAME */
7113 	ISP_FC_OPMAP(0xcf, 0x01),	/* 0x6b: MBOX_GET_LINK_STATUS */
7114 	ISP_FC_OPMAP(0x0f, 0x01),	/* 0x6c: MBOX_INIT_LIP_RESET */
7115 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x6d: */
7116 	ISP_FC_OPMAP(0xcf, 0x03),	/* 0x6e: MBOX_SEND_SNS */
7117 	ISP_FC_OPMAP(0x0f, 0x07),	/* 0x6f: MBOX_FABRIC_LOGIN */
7118 	ISP_FC_OPMAP_HALF(0x02, 0x03, 0x00, 0x03),	/* 0x70: MBOX_SEND_CHANGE_REQUEST */
7119 	ISP_FC_OPMAP(0x03, 0x03),	/* 0x71: MBOX_FABRIC_LOGOUT */
7120 	ISP_FC_OPMAP(0x0f, 0x0f),	/* 0x72: MBOX_INIT_LIP_LOGIN */
7121 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x73: */
7122 	ISP_FC_OPMAP(0x07, 0x01),	/* 0x74: LOGIN LOOP PORT */
7123 	ISP_FC_OPMAP_HALF(0x03, 0xcf, 0x00, 0x07),	/* 0x75: GET PORT/NODE NAME LIST */
7124 	ISP_FC_OPMAP(0x4f, 0x01),	/* 0x76: SET VENDOR ID */
7125 	ISP_FC_OPMAP(0xcd, 0x01),	/* 0x77: INITIALIZE IP MAILBOX */
7126 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x78: */
7127 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x79: */
7128 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x7a: */
7129 	ISP_FC_OPMAP(0x00, 0x00),	/* 0x7b: */
7130 	ISP_FC_OPMAP_HALF(0x03, 0x4f, 0x00, 0x07),	/* 0x7c: Get ID List */
7131 	ISP_FC_OPMAP(0xcf, 0x01),	/* 0x7d: SEND LFA */
7132 	ISP_FC_OPMAP(0x0f, 0x01)	/* 0x7e: LUN RESET */
7133 };
7134 #define	MAX_FC_OPCODE	0x7e
7135 /*
7136  * Footnotes
7137  *
7138  * (1): this sets bits 21..16 in mailbox register #8, which we nominally
7139  *	do not access at this time in the core driver. The caller is
7140  *	responsible for setting this register first (Gross!). The assumption
7141  *	is that we won't overflow.
7142  */
7143 
7144 static const char *fc_mbcmd_names[] = {
7145 	"NO-OP",			/* 00h */
7146 	"LOAD RAM",
7147 	"EXEC FIRMWARE",
7148 	"DUMP RAM",
7149 	"WRITE RAM WORD",
7150 	"READ RAM WORD",
7151 	"MAILBOX REG TEST",
7152 	"VERIFY CHECKSUM",
7153 	"ABOUT FIRMWARE",
7154 	"LOAD RAM (2100)",
7155 	"DUMP RAM",
7156 	"LOAD RISC RAM",
7157 	"DUMP RISC RAM",
7158 	"WRITE RAM WORD EXTENDED",
7159 	"CHECK FIRMWARE",
7160 	"READ RAM WORD EXTENDED",
7161 	"INIT REQUEST QUEUE",		/* 10h */
7162 	"INIT RESULT QUEUE",
7163 	"EXECUTE IOCB",
7164 	"WAKE UP",
7165 	"STOP FIRMWARE",
7166 	"ABORT",
7167 	"ABORT DEVICE",
7168 	"ABORT TARGET",
7169 	"BUS RESET",
7170 	"STOP QUEUE",
7171 	"START QUEUE",
7172 	"SINGLE STEP QUEUE",
7173 	"ABORT QUEUE",
7174 	"GET DEV QUEUE STATUS",
7175 	NULL,
7176 	"GET FIRMWARE STATUS",
7177 	"GET LOOP ID",			/* 20h */
7178 	NULL,
7179 	"GET TIMEOUT PARAMS",
7180 	NULL,
7181 	NULL,
7182 	NULL,
7183 	NULL,
7184 	NULL,
7185 	"GET FIRMWARE OPTIONS",
7186 	"GET PORT QUEUE PARAMS",
7187 	"GENERATE SYSTEM ERROR",
7188 	NULL,
7189 	NULL,
7190 	NULL,
7191 	NULL,
7192 	NULL,
7193 	"WRITE SFP",			/* 30h */
7194 	"READ SFP",
7195 	"SET TIMEOUT PARAMS",
7196 	NULL,
7197 	NULL,
7198 	NULL,
7199 	NULL,
7200 	NULL,
7201 	"SET FIRMWARE OPTIONS",
7202 	"SET PORT QUEUE PARAMS",
7203 	NULL,
7204 	"SET FC LED CONF",
7205 	NULL,
7206 	"RESTART NIC FIRMWARE",
7207 	"ACCESS CONTROL",
7208 	NULL,
7209 	"LOOP PORT BYPASS",		/* 40h */
7210 	"LOOP PORT ENABLE",
7211 	"GET RESOURCE COUNT",
7212 	"REQUEST NON PARTICIPATING MODE",
7213 	"DIAGNOSTIC ECHO TEST",
7214 	"DIAGNOSTIC LOOPBACK",
7215 	NULL,
7216 	"GET PORT DATABASE ENHANCED",
7217 	"INIT FIRMWARE MULTI ID",
7218 	"GET VP DATABASE",
7219 	"GET VP DATABASE ENTRY",
7220 	NULL,
7221 	NULL,
7222 	NULL,
7223 	NULL,
7224 	NULL,
7225 	"GET FCF LIST",			/* 50h */
7226 	"GET DCBX PARAMETERS",
7227 	NULL,
7228 	"HOST MEMORY COPY",
7229 	"EXECUTE IOCB A64",
7230 	NULL,
7231 	NULL,
7232 	"SEND RNID",
7233 	NULL,
7234 	"SET PARAMETERS",
7235 	"GET PARAMETERS",
7236 	"DRIVER HEARTBEAT",
7237 	"FIRMWARE HEARTBEAT",
7238 	"GET/SET DATA RATE",
7239 	"SEND RNFT",
7240 	NULL,
7241 	"INIT FIRMWARE",		/* 60h */
7242 	"GET INIT CONTROL BLOCK",
7243 	"INIT LIP",
7244 	"GET FC-AL POSITION MAP",
7245 	"GET PORT DATABASE",
7246 	"CLEAR ACA",
7247 	"TARGET RESET",
7248 	"CLEAR TASK SET",
7249 	"ABORT TASK SET",
7250 	"GET FW STATE",
7251 	"GET PORT NAME",
7252 	"GET LINK STATUS",
7253 	"INIT LIP RESET",
7254 	"GET LINK STATS & PRIVATE DATA CNTS",
7255 	"SEND SNS",
7256 	"FABRIC LOGIN",
7257 	"SEND CHANGE REQUEST",		/* 70h */
7258 	"FABRIC LOGOUT",
7259 	"INIT LIP LOGIN",
7260 	NULL,
7261 	"LOGIN LOOP PORT",
7262 	"GET PORT/NODE NAME LIST",
7263 	"SET VENDOR ID",
7264 	"INITIALIZE IP MAILBOX",
7265 	NULL,
7266 	NULL,
7267 	"GET XGMAC STATS",
7268 	NULL,
7269 	"GET ID LIST",
7270 	"SEND LFA",
7271 	"LUN RESET"
7272 };
7273 
7274 static void
7275 isp_mboxcmd_qnw(ispsoftc_t *isp, mbreg_t *mbp, int nodelay)
7276 {
7277 	unsigned int ibits, obits, box, opcode;
7278 
7279 	opcode = mbp->param[0];
7280 	if (IS_FC(isp)) {
7281 		ibits = ISP_FC_IBITS(opcode);
7282 		obits = ISP_FC_OBITS(opcode);
7283 	} else {
7284 		ibits = ISP_SCSI_IBITS(opcode);
7285 		obits = ISP_SCSI_OBITS(opcode);
7286 	}
7287 	ibits |= mbp->ibits;
7288 	obits |= mbp->obits;
7289 	for (box = 0; box < ISP_NMBOX(isp); box++) {
7290 		if (ibits & (1 << box)) {
7291 			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
7292 		}
7293 		if (nodelay == 0) {
7294 			isp->isp_mboxtmp[box] = mbp->param[box] = 0;
7295 		}
7296 	}
7297 	if (nodelay == 0) {
7298 		isp->isp_lastmbxcmd = opcode;
7299 		isp->isp_obits = obits;
7300 		isp->isp_mboxbsy = 1;
7301 	}
7302 	if (IS_24XX(isp)) {
7303 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_SET_HOST_INT);
7304 	} else {
7305 		ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
7306 	}
7307 	/*
7308 	 * Oddly enough, if we're not delaying for an answer,
7309 	 * delay a bit to give the f/w a chance to pick up the
7310 	 * command.
7311 	 */
7312 	if (nodelay) {
7313 		ISP_DELAY(1000);
7314 	}
7315 }
7316 
7317 static void
7318 isp_mboxcmd(ispsoftc_t *isp, mbreg_t *mbp)
7319 {
7320 	const char *cname, *xname, *sname;
7321 	char tname[16], mname[16];
7322 	unsigned int ibits, obits, box, opcode;
7323 
7324 	opcode = mbp->param[0];
7325 	if (IS_FC(isp)) {
7326 		if (opcode > MAX_FC_OPCODE) {
7327 			mbp->param[0] = MBOX_INVALID_COMMAND;
7328 			isp_prt(isp, ISP_LOGERR, "Unknown Command 0x%x", opcode);
7329 			return;
7330 		}
7331 		cname = fc_mbcmd_names[opcode];
7332 		ibits = ISP_FC_IBITS(opcode);
7333 		obits = ISP_FC_OBITS(opcode);
7334 	} else {
7335 		if (opcode > MAX_SCSI_OPCODE) {
7336 			mbp->param[0] = MBOX_INVALID_COMMAND;
7337 			isp_prt(isp, ISP_LOGERR, "Unknown Command 0x%x", opcode);
7338 			return;
7339 		}
7340 		cname = scsi_mbcmd_names[opcode];
7341 		ibits = ISP_SCSI_IBITS(opcode);
7342 		obits = ISP_SCSI_OBITS(opcode);
7343 	}
7344 	if (cname == NULL) {
7345 		cname = tname;
7346 		ISP_SNPRINTF(tname, sizeof tname, "opcode %x", opcode);
7347 	}
7348 	isp_prt(isp, ISP_LOGDEBUG3, "Mailbox Command '%s'", cname);
7349 
7350 	/*
7351 	 * Pick up any additional bits that the caller might have set.
7352 	 */
7353 	ibits |= mbp->ibits;
7354 	obits |= mbp->obits;
7355 
7356 	/*
7357 	 * Mask any bits that the caller wants us to mask
7358 	 */
7359 	ibits &= mbp->ibitm;
7360 	obits &= mbp->obitm;
7361 
7362 
7363 	if (ibits == 0 && obits == 0) {
7364 		mbp->param[0] = MBOX_COMMAND_PARAM_ERROR;
7365 		isp_prt(isp, ISP_LOGERR, "no parameters for 0x%x", opcode);
7366 		return;
7367 	}
7368 
7369 	/*
7370 	 * Get exclusive usage of mailbox registers.
7371 	 */
7372 	if (MBOX_ACQUIRE(isp)) {
7373 		mbp->param[0] = MBOX_REGS_BUSY;
7374 		goto out;
7375 	}
7376 
7377 	for (box = 0; box < ISP_NMBOX(isp); box++) {
7378 		if (ibits & (1 << box)) {
7379 			isp_prt(isp, ISP_LOGDEBUG3, "IN mbox %d = 0x%04x", box,
7380 			    mbp->param[box]);
7381 			ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
7382 		}
7383 		isp->isp_mboxtmp[box] = mbp->param[box] = 0;
7384 	}
7385 
7386 	isp->isp_lastmbxcmd = opcode;
7387 
7388 	/*
7389 	 * We assume that we can't overwrite a previous command.
7390 	 */
7391 	isp->isp_obits = obits;
7392 	isp->isp_mboxbsy = 1;
7393 
7394 	/*
7395 	 * Set Host Interrupt condition so that RISC will pick up mailbox regs.
7396 	 */
7397 	if (IS_24XX(isp)) {
7398 		ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_SET_HOST_INT);
7399 	} else {
7400 		ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
7401 	}
7402 
7403 	/*
7404 	 * While we haven't finished the command, spin our wheels here.
7405 	 */
7406 	MBOX_WAIT_COMPLETE(isp, mbp);
7407 
7408 	/*
7409 	 * Did the command time out?
7410 	 */
7411 	if (mbp->param[0] == MBOX_TIMEOUT) {
7412 		isp->isp_mboxbsy = 0;
7413 		MBOX_RELEASE(isp);
7414 		goto out;
7415 	}
7416 
7417 	/*
7418 	 * Copy back output registers.
7419 	 */
7420 	for (box = 0; box < ISP_NMBOX(isp); box++) {
7421 		if (obits & (1 << box)) {
7422 			mbp->param[box] = isp->isp_mboxtmp[box];
7423 			isp_prt(isp, ISP_LOGDEBUG3, "OUT mbox %d = 0x%04x", box,
7424 			    mbp->param[box]);
7425 		}
7426 	}
7427 
7428 	isp->isp_mboxbsy = 0;
7429 	MBOX_RELEASE(isp);
7430 out:
7431 	if (mbp->logval == 0 || mbp->param[0] == MBOX_COMMAND_COMPLETE)
7432 		return;
7433 
7434 	if ((mbp->param[0] & 0xbfe0) == 0 &&
7435 	    (mbp->logval & MBLOGMASK(mbp->param[0])) == 0)
7436 		return;
7437 
7438 	xname = NULL;
7439 	sname = "";
7440 	switch (mbp->param[0]) {
7441 	case MBOX_INVALID_COMMAND:
7442 		xname = "INVALID COMMAND";
7443 		break;
7444 	case MBOX_HOST_INTERFACE_ERROR:
7445 		xname = "HOST INTERFACE ERROR";
7446 		break;
7447 	case MBOX_TEST_FAILED:
7448 		xname = "TEST FAILED";
7449 		break;
7450 	case MBOX_COMMAND_ERROR:
7451 		xname = "COMMAND ERROR";
7452 		ISP_SNPRINTF(mname, sizeof(mname), " subcode 0x%x",
7453 		    mbp->param[1]);
7454 		sname = mname;
7455 		break;
7456 	case MBOX_COMMAND_PARAM_ERROR:
7457 		xname = "COMMAND PARAMETER ERROR";
7458 		break;
7459 	case MBOX_PORT_ID_USED:
7460 		xname = "PORT ID ALREADY IN USE";
7461 		break;
7462 	case MBOX_LOOP_ID_USED:
7463 		xname = "LOOP ID ALREADY IN USE";
7464 		break;
7465 	case MBOX_ALL_IDS_USED:
7466 		xname = "ALL LOOP IDS IN USE";
7467 		break;
7468 	case MBOX_NOT_LOGGED_IN:
7469 		xname = "NOT LOGGED IN";
7470 		break;
7471 	case MBOX_LINK_DOWN_ERROR:
7472 		xname = "LINK DOWN ERROR";
7473 		break;
7474 	case MBOX_LOOPBACK_ERROR:
7475 		xname = "LOOPBACK ERROR";
7476 		break;
7477 	case MBOX_CHECKSUM_ERROR:
7478 		xname = "CHECKSUM ERROR";
7479 		break;
7480 	case MBOX_INVALID_PRODUCT_KEY:
7481 		xname = "INVALID PRODUCT KEY";
7482 		break;
7483 	case MBOX_REGS_BUSY:
7484 		xname = "REGISTERS BUSY";
7485 		break;
7486 	case MBOX_TIMEOUT:
7487 		xname = "TIMEOUT";
7488 		break;
7489 	default:
7490 		ISP_SNPRINTF(mname, sizeof mname, "error 0x%x", mbp->param[0]);
7491 		xname = mname;
7492 		break;
7493 	}
7494 	if (xname) {
7495 		isp_prt(isp, ISP_LOGALL, "Mailbox Command '%s' failed (%s%s)",
7496 		    cname, xname, sname);
7497 	}
7498 }
7499 
7500 static int
7501 isp_fw_state(ispsoftc_t *isp, int chan)
7502 {
7503 	if (IS_FC(isp)) {
7504 		mbreg_t mbs;
7505 
7506 		MBSINIT(&mbs, MBOX_GET_FW_STATE, MBLOGALL, 0);
7507 		isp_mboxcmd(isp, &mbs);
7508 		if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
7509 			return (mbs.param[1]);
7510 		}
7511 	}
7512 	return (FW_ERROR);
7513 }
7514 
7515 static void
7516 isp_spi_update(ispsoftc_t *isp, int chan)
7517 {
7518 	int tgt;
7519 	mbreg_t mbs;
7520 	sdparam *sdp;
7521 
7522 	if (IS_FC(isp)) {
7523 		/*
7524 		 * There are no 'per-bus' settings for Fibre Channel.
7525 		 */
7526 		return;
7527 	}
7528 	sdp = SDPARAM(isp, chan);
7529 	sdp->update = 0;
7530 
7531 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
7532 		uint16_t flags, period, offset;
7533 		int get;
7534 
7535 		if (sdp->isp_devparam[tgt].dev_enable == 0) {
7536 			sdp->isp_devparam[tgt].dev_update = 0;
7537 			sdp->isp_devparam[tgt].dev_refresh = 0;
7538 			isp_prt(isp, ISP_LOGDEBUG0, "skipping target %d bus %d update", tgt, chan);
7539 			continue;
7540 		}
7541 		/*
7542 		 * If the goal is to update the status of the device,
7543 		 * take what's in goal_flags and try and set the device
7544 		 * toward that. Otherwise, if we're just refreshing the
7545 		 * current device state, get the current parameters.
7546 		 */
7547 
7548 		MBSINIT(&mbs, 0, MBLOGALL, 0);
7549 
7550 		/*
7551 		 * Refresh overrides set
7552 		 */
7553 		if (sdp->isp_devparam[tgt].dev_refresh) {
7554 			mbs.param[0] = MBOX_GET_TARGET_PARAMS;
7555 			get = 1;
7556 		} else if (sdp->isp_devparam[tgt].dev_update) {
7557 			mbs.param[0] = MBOX_SET_TARGET_PARAMS;
7558 
7559 			/*
7560 			 * Make sure goal_flags has "Renegotiate on Error"
7561 			 * on and "Freeze Queue on Error" off.
7562 			 */
7563 			sdp->isp_devparam[tgt].goal_flags |= DPARM_RENEG;
7564 			sdp->isp_devparam[tgt].goal_flags &= ~DPARM_QFRZ;
7565 			mbs.param[2] = sdp->isp_devparam[tgt].goal_flags;
7566 
7567 			/*
7568 			 * Insist that PARITY must be enabled
7569 			 * if SYNC or WIDE is enabled.
7570 			 */
7571 			if ((mbs.param[2] & (DPARM_SYNC|DPARM_WIDE)) != 0) {
7572 				mbs.param[2] |= DPARM_PARITY;
7573 			}
7574 
7575 			if (mbs.param[2] & DPARM_SYNC) {
7576 				mbs.param[3] =
7577 				    (sdp->isp_devparam[tgt].goal_offset << 8) |
7578 				    (sdp->isp_devparam[tgt].goal_period);
7579 			}
7580 			/*
7581 			 * A command completion later that has
7582 			 * RQSTF_NEGOTIATION set can cause
7583 			 * the dev_refresh/announce cycle also.
7584 			 *
7585 			 * Note: It is really important to update our current
7586 			 * flags with at least the state of TAG capabilities-
7587 			 * otherwise we might try and send a tagged command
7588 			 * when we have it all turned off. So change it here
7589 			 * to say that current already matches goal.
7590 			 */
7591 			sdp->isp_devparam[tgt].actv_flags &= ~DPARM_TQING;
7592 			sdp->isp_devparam[tgt].actv_flags |=
7593 			    (sdp->isp_devparam[tgt].goal_flags & DPARM_TQING);
7594 			isp_prt(isp, ISP_LOGDEBUG0, "bus %d set tgt %d flags 0x%x off 0x%x period 0x%x",
7595 			    chan, tgt, mbs.param[2], mbs.param[3] >> 8, mbs.param[3] & 0xff);
7596 			get = 0;
7597 		} else {
7598 			continue;
7599 		}
7600 		mbs.param[1] = (chan << 15) | (tgt << 8);
7601 		isp_mboxcmd(isp, &mbs);
7602 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
7603 			continue;
7604 		}
7605 		if (get == 0) {
7606 			sdp->sendmarker = 1;
7607 			sdp->isp_devparam[tgt].dev_update = 0;
7608 			sdp->isp_devparam[tgt].dev_refresh = 1;
7609 		} else {
7610 			sdp->isp_devparam[tgt].dev_refresh = 0;
7611 			flags = mbs.param[2];
7612 			period = mbs.param[3] & 0xff;
7613 			offset = mbs.param[3] >> 8;
7614 			sdp->isp_devparam[tgt].actv_flags = flags;
7615 			sdp->isp_devparam[tgt].actv_period = period;
7616 			sdp->isp_devparam[tgt].actv_offset = offset;
7617 			isp_async(isp, ISPASYNC_NEW_TGT_PARAMS, chan, tgt);
7618 		}
7619 	}
7620 
7621 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
7622 		if (sdp->isp_devparam[tgt].dev_update ||
7623 		    sdp->isp_devparam[tgt].dev_refresh) {
7624 			sdp->update = 1;
7625 			break;
7626 		}
7627 	}
7628 }
7629 
7630 static void
7631 isp_setdfltsdparm(ispsoftc_t *isp)
7632 {
7633 	int tgt;
7634 	sdparam *sdp, *sdp1;
7635 
7636 	sdp = SDPARAM(isp, 0);
7637 	if (IS_DUALBUS(isp))
7638 		sdp1 = sdp + 1;
7639 	else
7640 		sdp1 = NULL;
7641 
7642 	/*
7643 	 * Establish some default parameters.
7644 	 */
7645 	sdp->isp_cmd_dma_burst_enable = 0;
7646 	sdp->isp_data_dma_burst_enabl = 1;
7647 	sdp->isp_fifo_threshold = 0;
7648 	sdp->isp_initiator_id = DEFAULT_IID(isp, 0);
7649 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
7650 		sdp->isp_async_data_setup = 9;
7651 	} else {
7652 		sdp->isp_async_data_setup = 6;
7653 	}
7654 	sdp->isp_selection_timeout = 250;
7655 	sdp->isp_max_queue_depth = MAXISPREQUEST(isp);
7656 	sdp->isp_tag_aging = 8;
7657 	sdp->isp_bus_reset_delay = 5;
7658 	/*
7659 	 * Don't retry selection, busy or queue full automatically- reflect
7660 	 * these back to us.
7661 	 */
7662 	sdp->isp_retry_count = 0;
7663 	sdp->isp_retry_delay = 0;
7664 
7665 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
7666 		sdp->isp_devparam[tgt].exc_throttle = ISP_EXEC_THROTTLE;
7667 		sdp->isp_devparam[tgt].dev_enable = 1;
7668 	}
7669 
7670 	/*
7671 	 * The trick here is to establish a default for the default (honk!)
7672 	 * state (goal_flags). Then try and get the current status from
7673 	 * the card to fill in the current state. We don't, in fact, set
7674 	 * the default to the SAFE default state- that's not the goal state.
7675 	 */
7676 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
7677 		uint8_t off, per;
7678 		sdp->isp_devparam[tgt].actv_offset = 0;
7679 		sdp->isp_devparam[tgt].actv_period = 0;
7680 		sdp->isp_devparam[tgt].actv_flags = 0;
7681 
7682 		sdp->isp_devparam[tgt].goal_flags =
7683 		    sdp->isp_devparam[tgt].nvrm_flags = DPARM_DEFAULT;
7684 
7685 		/*
7686 		 * We default to Wide/Fast for versions less than a 1040
7687 		 * (unless it's SBus).
7688 		 */
7689 		if (IS_ULTRA3(isp)) {
7690 			off = ISP_80M_SYNCPARMS >> 8;
7691 			per = ISP_80M_SYNCPARMS & 0xff;
7692 		} else if (IS_ULTRA2(isp)) {
7693 			off = ISP_40M_SYNCPARMS >> 8;
7694 			per = ISP_40M_SYNCPARMS & 0xff;
7695 		} else if (IS_1240(isp)) {
7696 			off = ISP_20M_SYNCPARMS >> 8;
7697 			per = ISP_20M_SYNCPARMS & 0xff;
7698 		} else if ((isp->isp_bustype == ISP_BT_SBUS &&
7699 		    isp->isp_type < ISP_HA_SCSI_1020A) ||
7700 		    (isp->isp_bustype == ISP_BT_PCI &&
7701 		    isp->isp_type < ISP_HA_SCSI_1040) ||
7702 		    (isp->isp_clock && isp->isp_clock < 60) ||
7703 		    (sdp->isp_ultramode == 0)) {
7704 			off = ISP_10M_SYNCPARMS >> 8;
7705 			per = ISP_10M_SYNCPARMS & 0xff;
7706 		} else {
7707 			off = ISP_20M_SYNCPARMS_1040 >> 8;
7708 			per = ISP_20M_SYNCPARMS_1040 & 0xff;
7709 		}
7710 		sdp->isp_devparam[tgt].goal_offset =
7711 		    sdp->isp_devparam[tgt].nvrm_offset = off;
7712 		sdp->isp_devparam[tgt].goal_period =
7713 		    sdp->isp_devparam[tgt].nvrm_period = per;
7714 
7715 	}
7716 
7717 	/*
7718 	 * If we're a dual bus card, just copy the data over
7719 	 */
7720 	if (sdp1) {
7721 		*sdp1 = *sdp;
7722 		sdp1->isp_initiator_id = DEFAULT_IID(isp, 1);
7723 	}
7724 
7725 	/*
7726 	 * If we've not been told to avoid reading NVRAM, try and read it.
7727 	 * If we're successful reading it, we can then return because NVRAM
7728 	 * will tell us what the desired settings are. Otherwise, we establish
7729 	 * some reasonable 'fake' nvram and goal defaults.
7730 	 */
7731 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
7732 		mbreg_t mbs;
7733 
7734 		if (isp_read_nvram(isp, 0) == 0) {
7735 			if (IS_DUALBUS(isp)) {
7736 				if (isp_read_nvram(isp, 1) == 0) {
7737 					return;
7738 				}
7739 			}
7740 		}
7741 		MBSINIT(&mbs, MBOX_GET_ACT_NEG_STATE, MBLOGNONE, 0);
7742 		isp_mboxcmd(isp, &mbs);
7743 		if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
7744 			sdp->isp_req_ack_active_neg = 1;
7745 			sdp->isp_data_line_active_neg = 1;
7746 			if (sdp1) {
7747 				sdp1->isp_req_ack_active_neg = 1;
7748 				sdp1->isp_data_line_active_neg = 1;
7749 			}
7750 		} else {
7751 			sdp->isp_req_ack_active_neg =
7752 			    (mbs.param[1] >> 4) & 0x1;
7753 			sdp->isp_data_line_active_neg =
7754 			    (mbs.param[1] >> 5) & 0x1;
7755 			if (sdp1) {
7756 				sdp1->isp_req_ack_active_neg =
7757 				    (mbs.param[2] >> 4) & 0x1;
7758 				sdp1->isp_data_line_active_neg =
7759 				    (mbs.param[2] >> 5) & 0x1;
7760 			}
7761 		}
7762 	}
7763 
7764 }
7765 
7766 static void
7767 isp_setdfltfcparm(ispsoftc_t *isp, int chan)
7768 {
7769 	fcparam *fcp = FCPARAM(isp, chan);
7770 
7771 	/*
7772 	 * Establish some default parameters.
7773 	 */
7774 	fcp->role = DEFAULT_ROLE(isp, chan);
7775 	fcp->isp_maxalloc = ICB_DFLT_ALLOC;
7776 	fcp->isp_retry_delay = ICB_DFLT_RDELAY;
7777 	fcp->isp_retry_count = ICB_DFLT_RCOUNT;
7778 	fcp->isp_loopid = DEFAULT_LOOPID(isp, chan);
7779 	fcp->isp_wwnn_nvram = DEFAULT_NODEWWN(isp, chan);
7780 	fcp->isp_wwpn_nvram = DEFAULT_PORTWWN(isp, chan);
7781 	fcp->isp_fwoptions = 0;
7782 	fcp->isp_xfwoptions = 0;
7783 	fcp->isp_zfwoptions = 0;
7784 	fcp->isp_lasthdl = NIL_HANDLE;
7785 
7786 	if (IS_24XX(isp)) {
7787 		fcp->isp_fwoptions |= ICB2400_OPT1_FAIRNESS;
7788 		fcp->isp_fwoptions |= ICB2400_OPT1_HARD_ADDRESS;
7789 		if (isp->isp_confopts & ISP_CFG_FULL_DUPLEX) {
7790 			fcp->isp_fwoptions |= ICB2400_OPT1_FULL_DUPLEX;
7791 		}
7792 		fcp->isp_fwoptions |= ICB2400_OPT1_BOTH_WWNS;
7793 		fcp->isp_zfwoptions |= ICB2400_OPT3_RATE_AUTO;
7794 	} else {
7795 		fcp->isp_fwoptions |= ICBOPT_FAIRNESS;
7796 		fcp->isp_fwoptions |= ICBOPT_PDBCHANGE_AE;
7797 		fcp->isp_fwoptions |= ICBOPT_HARD_ADDRESS;
7798 		if (isp->isp_confopts & ISP_CFG_FULL_DUPLEX) {
7799 			fcp->isp_fwoptions |= ICBOPT_FULL_DUPLEX;
7800 		}
7801 		/*
7802 		 * Make sure this is turned off now until we get
7803 		 * extended options from NVRAM
7804 		 */
7805 		fcp->isp_fwoptions &= ~ICBOPT_EXTENDED;
7806 		fcp->isp_zfwoptions |= ICBZOPT_RATE_AUTO;
7807 	}
7808 
7809 
7810 	/*
7811 	 * Now try and read NVRAM unless told to not do so.
7812 	 * This will set fcparam's isp_wwnn_nvram && isp_wwpn_nvram.
7813 	 */
7814 	if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
7815 		int i, j = 0;
7816 		/*
7817 		 * Give a couple of tries at reading NVRAM.
7818 		 */
7819 		for (i = 0; i < 2; i++) {
7820 			j = isp_read_nvram(isp, chan);
7821 			if (j == 0) {
7822 				break;
7823 			}
7824 		}
7825 		if (j) {
7826 			isp->isp_confopts |= ISP_CFG_NONVRAM;
7827 		}
7828 	}
7829 
7830 	fcp->isp_wwnn = ACTIVE_NODEWWN(isp, chan);
7831 	fcp->isp_wwpn = ACTIVE_PORTWWN(isp, chan);
7832 	isp_prt(isp, ISP_LOGCONFIG, "Chan %d 0x%08x%08x/0x%08x%08x Role %s",
7833 	    chan, (uint32_t) (fcp->isp_wwnn >> 32), (uint32_t) (fcp->isp_wwnn),
7834 	    (uint32_t) (fcp->isp_wwpn >> 32), (uint32_t) (fcp->isp_wwpn),
7835 	    isp_class3_roles[fcp->role]);
7836 }
7837 
7838 /*
7839  * Re-initialize the ISP and complete all orphaned commands
7840  * with a 'botched' notice. The reset/init routines should
7841  * not disturb an already active list of commands.
7842  */
7843 
7844 int
7845 isp_reinit(ispsoftc_t *isp, int do_load_defaults)
7846 {
7847 	int i, res = 0;
7848 
7849 	if (isp->isp_state == ISP_RUNSTATE)
7850 		isp_deinit(isp);
7851 	if (isp->isp_state != ISP_RESETSTATE)
7852 		isp_reset(isp, do_load_defaults);
7853 	if (isp->isp_state != ISP_RESETSTATE) {
7854 		res = EIO;
7855 		isp_prt(isp, ISP_LOGERR, "%s: cannot reset card", __func__);
7856 		ISP_DISABLE_INTS(isp);
7857 		goto cleanup;
7858 	}
7859 
7860 	isp_init(isp);
7861 	if (isp->isp_state > ISP_RESETSTATE &&
7862 	    isp->isp_state != ISP_RUNSTATE) {
7863 		res = EIO;
7864 		isp_prt(isp, ISP_LOGERR, "%s: cannot init card", __func__);
7865 		ISP_DISABLE_INTS(isp);
7866 		if (IS_FC(isp)) {
7867 			/*
7868 			 * If we're in ISP_ROLE_NONE, turn off the lasers.
7869 			 */
7870 			if (!IS_24XX(isp)) {
7871 				ISP_WRITE(isp, BIU2100_CSR, BIU2100_FPM0_REGS);
7872 				ISP_WRITE(isp, FPM_DIAG_CONFIG, FPM_SOFT_RESET);
7873 				ISP_WRITE(isp, BIU2100_CSR, BIU2100_FB_REGS);
7874 				ISP_WRITE(isp, FBM_CMD, FBMCMD_FIFO_RESET_ALL);
7875 				ISP_WRITE(isp, BIU2100_CSR, BIU2100_RISC_REGS);
7876 			}
7877 		}
7878 	}
7879 
7880 cleanup:
7881 	isp->isp_nactive = 0;
7882 	isp_clear_commands(isp);
7883 	if (IS_FC(isp)) {
7884 		for (i = 0; i < isp->isp_nchan; i++)
7885 			isp_clear_portdb(isp, i);
7886 	}
7887 	return (res);
7888 }
7889 
7890 /*
7891  * NVRAM Routines
7892  */
7893 static int
7894 isp_read_nvram(ispsoftc_t *isp, int bus)
7895 {
7896 	int i, amt, retval;
7897 	uint8_t csum, minversion;
7898 	union {
7899 		uint8_t _x[ISP2400_NVRAM_SIZE];
7900 		uint16_t _s[ISP2400_NVRAM_SIZE>>1];
7901 	} _n;
7902 #define	nvram_data	_n._x
7903 #define	nvram_words	_n._s
7904 
7905 	if (IS_24XX(isp)) {
7906 		return (isp_read_nvram_2400(isp, nvram_data));
7907 	} else if (IS_FC(isp)) {
7908 		amt = ISP2100_NVRAM_SIZE;
7909 		minversion = 1;
7910 	} else if (IS_ULTRA2(isp)) {
7911 		amt = ISP1080_NVRAM_SIZE;
7912 		minversion = 0;
7913 	} else {
7914 		amt = ISP_NVRAM_SIZE;
7915 		minversion = 2;
7916 	}
7917 
7918 	for (i = 0; i < amt>>1; i++) {
7919 		isp_rdnvram_word(isp, i, &nvram_words[i]);
7920 	}
7921 
7922 	if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
7923 	    nvram_data[2] != 'P') {
7924 		if (isp->isp_bustype != ISP_BT_SBUS) {
7925 			isp_prt(isp, ISP_LOGWARN, "invalid NVRAM header");
7926 			isp_prt(isp, ISP_LOGDEBUG0, "%x %x %x", nvram_data[0], nvram_data[1], nvram_data[2]);
7927 		}
7928 		retval = -1;
7929 		goto out;
7930 	}
7931 
7932 	for (csum = 0, i = 0; i < amt; i++) {
7933 		csum += nvram_data[i];
7934 	}
7935 	if (csum != 0) {
7936 		isp_prt(isp, ISP_LOGWARN, "invalid NVRAM checksum");
7937 		retval = -1;
7938 		goto out;
7939 	}
7940 
7941 	if (ISP_NVRAM_VERSION(nvram_data) < minversion) {
7942 		isp_prt(isp, ISP_LOGWARN, "version %d NVRAM not understood",
7943 		    ISP_NVRAM_VERSION(nvram_data));
7944 		retval = -1;
7945 		goto out;
7946 	}
7947 
7948 	if (IS_ULTRA3(isp)) {
7949 		isp_parse_nvram_12160(isp, bus, nvram_data);
7950 	} else if (IS_1080(isp)) {
7951 		isp_parse_nvram_1080(isp, bus, nvram_data);
7952 	} else if (IS_1280(isp) || IS_1240(isp)) {
7953 		isp_parse_nvram_1080(isp, bus, nvram_data);
7954 	} else if (IS_SCSI(isp)) {
7955 		isp_parse_nvram_1020(isp, nvram_data);
7956 	} else {
7957 		isp_parse_nvram_2100(isp, nvram_data);
7958 	}
7959 	retval = 0;
7960 out:
7961 	return (retval);
7962 #undef	nvram_data
7963 #undef	nvram_words
7964 }
7965 
7966 static int
7967 isp_read_nvram_2400(ispsoftc_t *isp, uint8_t *nvram_data)
7968 {
7969 	int retval = 0;
7970 	uint32_t addr, csum, lwrds, *dptr;
7971 
7972 	if (isp->isp_port) {
7973 		addr = ISP2400_NVRAM_PORT1_ADDR;
7974 	} else {
7975 		addr = ISP2400_NVRAM_PORT0_ADDR;
7976 	}
7977 
7978 	dptr = (uint32_t *) nvram_data;
7979 	for (lwrds = 0; lwrds < ISP2400_NVRAM_SIZE >> 2; lwrds++) {
7980 		isp_rd_2400_nvram(isp, addr++, dptr++);
7981 	}
7982 	if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
7983 	    nvram_data[2] != 'P') {
7984 		isp_prt(isp, ISP_LOGWARN, "invalid NVRAM header (%x %x %x)",
7985 		    nvram_data[0], nvram_data[1], nvram_data[2]);
7986 		retval = -1;
7987 		goto out;
7988 	}
7989 	dptr = (uint32_t *) nvram_data;
7990 	for (csum = 0, lwrds = 0; lwrds < ISP2400_NVRAM_SIZE >> 2; lwrds++) {
7991 		uint32_t tmp;
7992 		ISP_IOXGET_32(isp, &dptr[lwrds], tmp);
7993 		csum += tmp;
7994 	}
7995 	if (csum != 0) {
7996 		isp_prt(isp, ISP_LOGWARN, "invalid NVRAM checksum");
7997 		retval = -1;
7998 		goto out;
7999 	}
8000 	isp_parse_nvram_2400(isp, nvram_data);
8001 out:
8002 	return (retval);
8003 }
8004 
8005 static void
8006 isp_rdnvram_word(ispsoftc_t *isp, int wo, uint16_t *rp)
8007 {
8008 	int i, cbits;
8009 	uint16_t bit, rqst, junk;
8010 
8011 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
8012 	ISP_DELAY(10);
8013 	ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
8014 	ISP_DELAY(10);
8015 
8016 	if (IS_FC(isp)) {
8017 		wo &= ((ISP2100_NVRAM_SIZE >> 1) - 1);
8018 		if (IS_2312(isp) && isp->isp_port) {
8019 			wo += 128;
8020 		}
8021 		rqst = (ISP_NVRAM_READ << 8) | wo;
8022 		cbits = 10;
8023 	} else if (IS_ULTRA2(isp)) {
8024 		wo &= ((ISP1080_NVRAM_SIZE >> 1) - 1);
8025 		rqst = (ISP_NVRAM_READ << 8) | wo;
8026 		cbits = 10;
8027 	} else {
8028 		wo &= ((ISP_NVRAM_SIZE >> 1) - 1);
8029 		rqst = (ISP_NVRAM_READ << 6) | wo;
8030 		cbits = 8;
8031 	}
8032 
8033 	/*
8034 	 * Clock the word select request out...
8035 	 */
8036 	for (i = cbits; i >= 0; i--) {
8037 		if ((rqst >> i) & 1) {
8038 			bit = BIU_NVRAM_SELECT | BIU_NVRAM_DATAOUT;
8039 		} else {
8040 			bit = BIU_NVRAM_SELECT;
8041 		}
8042 		ISP_WRITE(isp, BIU_NVRAM, bit);
8043 		ISP_DELAY(10);
8044 		junk = ISP_READ(isp, BIU_NVRAM);	/* force PCI flush */
8045 		ISP_WRITE(isp, BIU_NVRAM, bit | BIU_NVRAM_CLOCK);
8046 		ISP_DELAY(10);
8047 		junk = ISP_READ(isp, BIU_NVRAM);	/* force PCI flush */
8048 		ISP_WRITE(isp, BIU_NVRAM, bit);
8049 		ISP_DELAY(10);
8050 		junk = ISP_READ(isp, BIU_NVRAM);	/* force PCI flush */
8051 	}
8052 	/*
8053 	 * Now read the result back in (bits come back in MSB format).
8054 	 */
8055 	*rp = 0;
8056 	for (i = 0; i < 16; i++) {
8057 		uint16_t rv;
8058 		*rp <<= 1;
8059 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
8060 		ISP_DELAY(10);
8061 		rv = ISP_READ(isp, BIU_NVRAM);
8062 		if (rv & BIU_NVRAM_DATAIN) {
8063 			*rp |= 1;
8064 		}
8065 		ISP_DELAY(10);
8066 		ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
8067 		ISP_DELAY(10);
8068 		junk = ISP_READ(isp, BIU_NVRAM);	/* force PCI flush */
8069 	}
8070 	ISP_WRITE(isp, BIU_NVRAM, 0);
8071 	ISP_DELAY(10);
8072 	junk = ISP_READ(isp, BIU_NVRAM);	/* force PCI flush */
8073 	ISP_SWIZZLE_NVRAM_WORD(isp, rp);
8074 }
8075 
8076 static void
8077 isp_rd_2400_nvram(ispsoftc_t *isp, uint32_t addr, uint32_t *rp)
8078 {
8079 	int loops = 0;
8080 	uint32_t base = 0x7ffe0000;
8081 	uint32_t tmp = 0;
8082 
8083 	if (IS_26XX(isp)) {
8084 		base = 0x7fe7c000;	/* XXX: Observation, may be wrong. */
8085 	} else if (IS_25XX(isp)) {
8086 		base = 0x7ff00000 | 0x48000;
8087 	}
8088 	ISP_WRITE(isp, BIU2400_FLASH_ADDR, base | addr);
8089 	for (loops = 0; loops < 5000; loops++) {
8090 		ISP_DELAY(10);
8091 		tmp = ISP_READ(isp, BIU2400_FLASH_ADDR);
8092 		if ((tmp & (1U << 31)) != 0) {
8093 			break;
8094 		}
8095 	}
8096 	if (tmp & (1U << 31)) {
8097 		*rp = ISP_READ(isp, BIU2400_FLASH_DATA);
8098 		ISP_SWIZZLE_NVRAM_LONG(isp, rp);
8099 	} else {
8100 		*rp = 0xffffffff;
8101 	}
8102 }
8103 
8104 static void
8105 isp_parse_nvram_1020(ispsoftc_t *isp, uint8_t *nvram_data)
8106 {
8107 	sdparam *sdp = SDPARAM(isp, 0);
8108 	int tgt;
8109 
8110 	sdp->isp_fifo_threshold =
8111 		ISP_NVRAM_FIFO_THRESHOLD(nvram_data) |
8112 		(ISP_NVRAM_FIFO_THRESHOLD_128(nvram_data) << 2);
8113 
8114 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
8115 		sdp->isp_initiator_id = ISP_NVRAM_INITIATOR_ID(nvram_data);
8116 
8117 	sdp->isp_bus_reset_delay =
8118 		ISP_NVRAM_BUS_RESET_DELAY(nvram_data);
8119 
8120 	sdp->isp_retry_count =
8121 		ISP_NVRAM_BUS_RETRY_COUNT(nvram_data);
8122 
8123 	sdp->isp_retry_delay =
8124 		ISP_NVRAM_BUS_RETRY_DELAY(nvram_data);
8125 
8126 	sdp->isp_async_data_setup =
8127 		ISP_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data);
8128 
8129 	if (isp->isp_type >= ISP_HA_SCSI_1040) {
8130 		if (sdp->isp_async_data_setup < 9) {
8131 			sdp->isp_async_data_setup = 9;
8132 		}
8133 	} else {
8134 		if (sdp->isp_async_data_setup != 6) {
8135 			sdp->isp_async_data_setup = 6;
8136 		}
8137 	}
8138 
8139 	sdp->isp_req_ack_active_neg =
8140 		ISP_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data);
8141 
8142 	sdp->isp_data_line_active_neg =
8143 		ISP_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data);
8144 
8145 	sdp->isp_data_dma_burst_enabl =
8146 		ISP_NVRAM_DATA_DMA_BURST_ENABLE(nvram_data);
8147 
8148 	sdp->isp_cmd_dma_burst_enable =
8149 		ISP_NVRAM_CMD_DMA_BURST_ENABLE(nvram_data);
8150 
8151 	sdp->isp_tag_aging =
8152 		ISP_NVRAM_TAG_AGE_LIMIT(nvram_data);
8153 
8154 	sdp->isp_selection_timeout =
8155 		ISP_NVRAM_SELECTION_TIMEOUT(nvram_data);
8156 
8157 	sdp->isp_max_queue_depth =
8158 		ISP_NVRAM_MAX_QUEUE_DEPTH(nvram_data);
8159 
8160 	sdp->isp_fast_mttr = ISP_NVRAM_FAST_MTTR_ENABLE(nvram_data);
8161 
8162 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
8163 		sdp->isp_devparam[tgt].dev_enable =
8164 			ISP_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt);
8165 		sdp->isp_devparam[tgt].exc_throttle =
8166 			ISP_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt);
8167 		sdp->isp_devparam[tgt].nvrm_offset =
8168 			ISP_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt);
8169 		sdp->isp_devparam[tgt].nvrm_period =
8170 			ISP_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt);
8171 		/*
8172 		 * We probably shouldn't lie about this, but it
8173 		 * it makes it much safer if we limit NVRAM values
8174 		 * to sanity.
8175 		 */
8176 		if (isp->isp_type < ISP_HA_SCSI_1040) {
8177 			/*
8178 			 * If we're not ultra, we can't possibly
8179 			 * be a shorter period than this.
8180 			 */
8181 			if (sdp->isp_devparam[tgt].nvrm_period < 0x19) {
8182 				sdp->isp_devparam[tgt].nvrm_period = 0x19;
8183 			}
8184 			if (sdp->isp_devparam[tgt].nvrm_offset > 0xc) {
8185 				sdp->isp_devparam[tgt].nvrm_offset = 0x0c;
8186 			}
8187 		} else {
8188 			if (sdp->isp_devparam[tgt].nvrm_offset > 0x8) {
8189 				sdp->isp_devparam[tgt].nvrm_offset = 0x8;
8190 			}
8191 		}
8192 		sdp->isp_devparam[tgt].nvrm_flags = 0;
8193 		if (ISP_NVRAM_TGT_RENEG(nvram_data, tgt))
8194 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
8195 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
8196 		if (ISP_NVRAM_TGT_TQING(nvram_data, tgt))
8197 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
8198 		if (ISP_NVRAM_TGT_SYNC(nvram_data, tgt))
8199 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
8200 		if (ISP_NVRAM_TGT_WIDE(nvram_data, tgt))
8201 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
8202 		if (ISP_NVRAM_TGT_PARITY(nvram_data, tgt))
8203 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
8204 		if (ISP_NVRAM_TGT_DISC(nvram_data, tgt))
8205 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
8206 		sdp->isp_devparam[tgt].actv_flags = 0; /* we don't know */
8207 		sdp->isp_devparam[tgt].goal_offset =
8208 		    sdp->isp_devparam[tgt].nvrm_offset;
8209 		sdp->isp_devparam[tgt].goal_period =
8210 		    sdp->isp_devparam[tgt].nvrm_period;
8211 		sdp->isp_devparam[tgt].goal_flags =
8212 		    sdp->isp_devparam[tgt].nvrm_flags;
8213 	}
8214 }
8215 
8216 static void
8217 isp_parse_nvram_1080(ispsoftc_t *isp, int bus, uint8_t *nvram_data)
8218 {
8219 	sdparam *sdp = SDPARAM(isp, bus);
8220 	int tgt;
8221 
8222 	sdp->isp_fifo_threshold =
8223 	    ISP1080_NVRAM_FIFO_THRESHOLD(nvram_data);
8224 
8225 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
8226 		sdp->isp_initiator_id = ISP1080_NVRAM_INITIATOR_ID(nvram_data, bus);
8227 
8228 	sdp->isp_bus_reset_delay =
8229 	    ISP1080_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
8230 
8231 	sdp->isp_retry_count =
8232 	    ISP1080_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
8233 
8234 	sdp->isp_retry_delay =
8235 	    ISP1080_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
8236 
8237 	sdp->isp_async_data_setup =
8238 	    ISP1080_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
8239 
8240 	sdp->isp_req_ack_active_neg =
8241 	    ISP1080_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
8242 
8243 	sdp->isp_data_line_active_neg =
8244 	    ISP1080_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
8245 
8246 	sdp->isp_data_dma_burst_enabl =
8247 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
8248 
8249 	sdp->isp_cmd_dma_burst_enable =
8250 	    ISP1080_NVRAM_BURST_ENABLE(nvram_data);
8251 
8252 	sdp->isp_selection_timeout =
8253 	    ISP1080_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
8254 
8255 	sdp->isp_max_queue_depth =
8256 	     ISP1080_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
8257 
8258 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
8259 		sdp->isp_devparam[tgt].dev_enable =
8260 		    ISP1080_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
8261 		sdp->isp_devparam[tgt].exc_throttle =
8262 			ISP1080_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
8263 		sdp->isp_devparam[tgt].nvrm_offset =
8264 			ISP1080_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
8265 		sdp->isp_devparam[tgt].nvrm_period =
8266 			ISP1080_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
8267 		sdp->isp_devparam[tgt].nvrm_flags = 0;
8268 		if (ISP1080_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
8269 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
8270 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
8271 		if (ISP1080_NVRAM_TGT_TQING(nvram_data, tgt, bus))
8272 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
8273 		if (ISP1080_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
8274 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
8275 		if (ISP1080_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
8276 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
8277 		if (ISP1080_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
8278 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
8279 		if (ISP1080_NVRAM_TGT_DISC(nvram_data, tgt, bus))
8280 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
8281 		sdp->isp_devparam[tgt].actv_flags = 0;
8282 		sdp->isp_devparam[tgt].goal_offset =
8283 		    sdp->isp_devparam[tgt].nvrm_offset;
8284 		sdp->isp_devparam[tgt].goal_period =
8285 		    sdp->isp_devparam[tgt].nvrm_period;
8286 		sdp->isp_devparam[tgt].goal_flags =
8287 		    sdp->isp_devparam[tgt].nvrm_flags;
8288 	}
8289 }
8290 
8291 static void
8292 isp_parse_nvram_12160(ispsoftc_t *isp, int bus, uint8_t *nvram_data)
8293 {
8294 	sdparam *sdp = SDPARAM(isp, bus);
8295 	int tgt;
8296 
8297 	sdp->isp_fifo_threshold =
8298 	    ISP12160_NVRAM_FIFO_THRESHOLD(nvram_data);
8299 
8300 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
8301 		sdp->isp_initiator_id = ISP12160_NVRAM_INITIATOR_ID(nvram_data, bus);
8302 
8303 	sdp->isp_bus_reset_delay =
8304 	    ISP12160_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
8305 
8306 	sdp->isp_retry_count =
8307 	    ISP12160_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
8308 
8309 	sdp->isp_retry_delay =
8310 	    ISP12160_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
8311 
8312 	sdp->isp_async_data_setup =
8313 	    ISP12160_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
8314 
8315 	sdp->isp_req_ack_active_neg =
8316 	    ISP12160_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
8317 
8318 	sdp->isp_data_line_active_neg =
8319 	    ISP12160_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
8320 
8321 	sdp->isp_data_dma_burst_enabl =
8322 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
8323 
8324 	sdp->isp_cmd_dma_burst_enable =
8325 	    ISP12160_NVRAM_BURST_ENABLE(nvram_data);
8326 
8327 	sdp->isp_selection_timeout =
8328 	    ISP12160_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
8329 
8330 	sdp->isp_max_queue_depth =
8331 	     ISP12160_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
8332 
8333 	for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
8334 		sdp->isp_devparam[tgt].dev_enable =
8335 		    ISP12160_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
8336 		sdp->isp_devparam[tgt].exc_throttle =
8337 			ISP12160_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
8338 		sdp->isp_devparam[tgt].nvrm_offset =
8339 			ISP12160_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
8340 		sdp->isp_devparam[tgt].nvrm_period =
8341 			ISP12160_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
8342 		sdp->isp_devparam[tgt].nvrm_flags = 0;
8343 		if (ISP12160_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
8344 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
8345 		sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
8346 		if (ISP12160_NVRAM_TGT_TQING(nvram_data, tgt, bus))
8347 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
8348 		if (ISP12160_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
8349 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
8350 		if (ISP12160_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
8351 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
8352 		if (ISP12160_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
8353 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
8354 		if (ISP12160_NVRAM_TGT_DISC(nvram_data, tgt, bus))
8355 			sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
8356 		sdp->isp_devparam[tgt].actv_flags = 0;
8357 		sdp->isp_devparam[tgt].goal_offset =
8358 		    sdp->isp_devparam[tgt].nvrm_offset;
8359 		sdp->isp_devparam[tgt].goal_period =
8360 		    sdp->isp_devparam[tgt].nvrm_period;
8361 		sdp->isp_devparam[tgt].goal_flags =
8362 		    sdp->isp_devparam[tgt].nvrm_flags;
8363 	}
8364 }
8365 
8366 static void
8367 isp_parse_nvram_2100(ispsoftc_t *isp, uint8_t *nvram_data)
8368 {
8369 	fcparam *fcp = FCPARAM(isp, 0);
8370 	uint64_t wwn;
8371 
8372 	/*
8373 	 * There is NVRAM storage for both Port and Node entities-
8374 	 * but the Node entity appears to be unused on all the cards
8375 	 * I can find. However, we should account for this being set
8376 	 * at some point in the future.
8377 	 *
8378 	 * Qlogic WWNs have an NAA of 2, but usually nothing shows up in
8379 	 * bits 48..60. In the case of the 2202, it appears that they do
8380 	 * use bit 48 to distinguish between the two instances on the card.
8381 	 * The 2204, which I've never seen, *probably* extends this method.
8382 	 */
8383 	wwn = ISP2100_NVRAM_PORT_NAME(nvram_data);
8384 	if (wwn) {
8385 		isp_prt(isp, ISP_LOGCONFIG, "NVRAM Port WWN 0x%08x%08x",
8386 		    (uint32_t) (wwn >> 32), (uint32_t) (wwn));
8387 		if ((wwn >> 60) == 0) {
8388 			wwn |= (((uint64_t) 2)<< 60);
8389 		}
8390 	}
8391 	fcp->isp_wwpn_nvram = wwn;
8392 	if (IS_2200(isp) || IS_23XX(isp)) {
8393 		wwn = ISP2100_NVRAM_NODE_NAME(nvram_data);
8394 		if (wwn) {
8395 			isp_prt(isp, ISP_LOGCONFIG, "NVRAM Node WWN 0x%08x%08x",
8396 			    (uint32_t) (wwn >> 32),
8397 			    (uint32_t) (wwn));
8398 			if ((wwn >> 60) == 0) {
8399 				wwn |= (((uint64_t) 2)<< 60);
8400 			}
8401 		} else {
8402 			wwn = fcp->isp_wwpn_nvram & ~((uint64_t) 0xfff << 48);
8403 		}
8404 	} else {
8405 		wwn &= ~((uint64_t) 0xfff << 48);
8406 	}
8407 	fcp->isp_wwnn_nvram = wwn;
8408 
8409 	fcp->isp_maxalloc = ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data);
8410 	if ((isp->isp_confopts & ISP_CFG_OWNFSZ) == 0) {
8411 		DEFAULT_FRAMESIZE(isp) =
8412 		    ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data);
8413 	}
8414 	fcp->isp_retry_delay = ISP2100_NVRAM_RETRY_DELAY(nvram_data);
8415 	fcp->isp_retry_count = ISP2100_NVRAM_RETRY_COUNT(nvram_data);
8416 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0) {
8417 		fcp->isp_loopid = ISP2100_NVRAM_HARDLOOPID(nvram_data);
8418 	}
8419 	if ((isp->isp_confopts & ISP_CFG_OWNEXCTHROTTLE) == 0) {
8420 		DEFAULT_EXEC_THROTTLE(isp) =
8421 			ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data);
8422 	}
8423 	fcp->isp_fwoptions = ISP2100_NVRAM_OPTIONS(nvram_data);
8424 	isp_prt(isp, ISP_LOGDEBUG0,
8425 	    "NVRAM 0x%08x%08x 0x%08x%08x maxalloc %d maxframelen %d",
8426 	    (uint32_t) (fcp->isp_wwnn_nvram >> 32),
8427 	    (uint32_t) fcp->isp_wwnn_nvram,
8428 	    (uint32_t) (fcp->isp_wwpn_nvram >> 32),
8429 	    (uint32_t) fcp->isp_wwpn_nvram,
8430 	    ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data),
8431 	    ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data));
8432 	isp_prt(isp, ISP_LOGDEBUG0,
8433 	    "execthrottle %d fwoptions 0x%x hardloop %d tov %d",
8434 	    ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data),
8435 	    ISP2100_NVRAM_OPTIONS(nvram_data),
8436 	    ISP2100_NVRAM_HARDLOOPID(nvram_data),
8437 	    ISP2100_NVRAM_TOV(nvram_data));
8438 	fcp->isp_xfwoptions = ISP2100_XFW_OPTIONS(nvram_data);
8439 	fcp->isp_zfwoptions = ISP2100_ZFW_OPTIONS(nvram_data);
8440 	isp_prt(isp, ISP_LOGDEBUG0, "xfwoptions 0x%x zfw options 0x%x",
8441 	    ISP2100_XFW_OPTIONS(nvram_data), ISP2100_ZFW_OPTIONS(nvram_data));
8442 }
8443 
8444 static void
8445 isp_parse_nvram_2400(ispsoftc_t *isp, uint8_t *nvram_data)
8446 {
8447 	fcparam *fcp = FCPARAM(isp, 0);
8448 	uint64_t wwn;
8449 
8450 	isp_prt(isp, ISP_LOGDEBUG0,
8451 	    "NVRAM 0x%08x%08x 0x%08x%08x exchg_cnt %d maxframelen %d",
8452 	    (uint32_t) (ISP2400_NVRAM_NODE_NAME(nvram_data) >> 32),
8453 	    (uint32_t) (ISP2400_NVRAM_NODE_NAME(nvram_data)),
8454 	    (uint32_t) (ISP2400_NVRAM_PORT_NAME(nvram_data) >> 32),
8455 	    (uint32_t) (ISP2400_NVRAM_PORT_NAME(nvram_data)),
8456 	    ISP2400_NVRAM_EXCHANGE_COUNT(nvram_data),
8457 	    ISP2400_NVRAM_MAXFRAMELENGTH(nvram_data));
8458 	isp_prt(isp, ISP_LOGDEBUG0,
8459 	    "NVRAM execthr %d loopid %d fwopt1 0x%x fwopt2 0x%x fwopt3 0x%x",
8460 	    ISP2400_NVRAM_EXECUTION_THROTTLE(nvram_data),
8461 	    ISP2400_NVRAM_HARDLOOPID(nvram_data),
8462 	    ISP2400_NVRAM_FIRMWARE_OPTIONS1(nvram_data),
8463 	    ISP2400_NVRAM_FIRMWARE_OPTIONS2(nvram_data),
8464 	    ISP2400_NVRAM_FIRMWARE_OPTIONS3(nvram_data));
8465 
8466 	wwn = ISP2400_NVRAM_PORT_NAME(nvram_data);
8467 	fcp->isp_wwpn_nvram = wwn;
8468 
8469 	wwn = ISP2400_NVRAM_NODE_NAME(nvram_data);
8470 	if (wwn) {
8471 		if ((wwn >> 60) != 2 && (wwn >> 60) != 5) {
8472 			wwn = 0;
8473 		}
8474 	}
8475 	if (wwn == 0 && (fcp->isp_wwpn_nvram >> 60) == 2) {
8476 		wwn = fcp->isp_wwpn_nvram;
8477 		wwn &= ~((uint64_t) 0xfff << 48);
8478 	}
8479 	fcp->isp_wwnn_nvram = wwn;
8480 
8481 	if (ISP2400_NVRAM_EXCHANGE_COUNT(nvram_data)) {
8482 		fcp->isp_maxalloc = ISP2400_NVRAM_EXCHANGE_COUNT(nvram_data);
8483 	}
8484 	if ((isp->isp_confopts & ISP_CFG_OWNFSZ) == 0) {
8485 		DEFAULT_FRAMESIZE(isp) =
8486 		    ISP2400_NVRAM_MAXFRAMELENGTH(nvram_data);
8487 	}
8488 	if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0) {
8489 		fcp->isp_loopid = ISP2400_NVRAM_HARDLOOPID(nvram_data);
8490 	}
8491 	if ((isp->isp_confopts & ISP_CFG_OWNEXCTHROTTLE) == 0) {
8492 		DEFAULT_EXEC_THROTTLE(isp) =
8493 			ISP2400_NVRAM_EXECUTION_THROTTLE(nvram_data);
8494 	}
8495 	fcp->isp_fwoptions = ISP2400_NVRAM_FIRMWARE_OPTIONS1(nvram_data);
8496 	fcp->isp_xfwoptions = ISP2400_NVRAM_FIRMWARE_OPTIONS2(nvram_data);
8497 	fcp->isp_zfwoptions = ISP2400_NVRAM_FIRMWARE_OPTIONS3(nvram_data);
8498 }
8499