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