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