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