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