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