]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/isp/isp.c
MFC r289620: Decode more firmware attributes.
[FreeBSD/stable/10.git] / sys / dev / isp / isp.c
1 /*-
2  *  Copyright (c) 1997-2009 by Matthew Jacob
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions
7  *  are met:
8  *
9  *  1. Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  *  2. Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  *
15  *  THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  *  ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  *  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  *  SUCH DAMAGE.
26  *
27  */
28
29 /*
30  * Machine and OS Independent (well, as best as possible)
31  * code for the Qlogic ISP SCSI and FC-SCSI adapters.
32  */
33
34 /*
35  * Inspiration and ideas about this driver are from Erik Moe's Linux driver
36  * (qlogicisp.c) and Dave Miller's SBus version of same (qlogicisp.c). Some
37  * ideas dredged from the Solaris driver.
38  */
39
40 /*
41  * Include header file appropriate for platform we're building on.
42  */
43 #ifdef  __NetBSD__
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD$");
46 #include <dev/ic/isp_netbsd.h>
47 #endif
48 #ifdef  __FreeBSD__
49 #include <sys/cdefs.h>
50 __FBSDID("$FreeBSD$");
51 #include <dev/isp/isp_freebsd.h>
52 #endif
53 #ifdef  __OpenBSD__
54 #include <dev/ic/isp_openbsd.h>
55 #endif
56 #ifdef  __linux__
57 #include "isp_linux.h"
58 #endif
59 #ifdef  __svr4__
60 #include "isp_solaris.h"
61 #endif
62
63 /*
64  * General defines
65  */
66 #define MBOX_DELAY_COUNT        1000000 / 100
67 #define ISP_MARK_PORTDB(a, b, c)                                \
68         do {                                                            \
69                 isp_prt(isp, ISP_LOG_SANCFG,                            \
70                     "Chan %d ISP_MARK_PORTDB@LINE %d", (b), __LINE__);  \
71                 isp_mark_portdb((a), (b), (c));                         \
72         } while (0)
73
74 /*
75  * Local static data
76  */
77 static const char fconf[] = "Chan %d PortDB[%d] changed:\n current =(0x%x@0x%06x 0x%08x%08x 0x%08x%08x)\n database=(0x%x@0x%06x 0x%08x%08x 0x%08x%08x)";
78 static const char notresp[] = "Not RESPONSE in RESPONSE Queue (type 0x%x) @ idx %d (next %d) nlooked %d";
79 static const char topology[] = "Chan %d WWPN 0x%08x%08x PortID 0x%06x handle 0x%x, Connection '%s'";
80 static const char bun[] = "bad underrun (count %d, resid %d, status %s)";
81 static const char lipd[] = "Chan %d LIP destroyed %d active commands";
82 static const char sacq[] = "unable to acquire scratch area";
83
84 static const uint8_t alpa_map[] = {
85         0xef, 0xe8, 0xe4, 0xe2, 0xe1, 0xe0, 0xdc, 0xda,
86         0xd9, 0xd6, 0xd5, 0xd4, 0xd3, 0xd2, 0xd1, 0xce,
87         0xcd, 0xcc, 0xcb, 0xca, 0xc9, 0xc7, 0xc6, 0xc5,
88         0xc3, 0xbc, 0xba, 0xb9, 0xb6, 0xb5, 0xb4, 0xb3,
89         0xb2, 0xb1, 0xae, 0xad, 0xac, 0xab, 0xaa, 0xa9,
90         0xa7, 0xa6, 0xa5, 0xa3, 0x9f, 0x9e, 0x9d, 0x9b,
91         0x98, 0x97, 0x90, 0x8f, 0x88, 0x84, 0x82, 0x81,
92         0x80, 0x7c, 0x7a, 0x79, 0x76, 0x75, 0x74, 0x73,
93         0x72, 0x71, 0x6e, 0x6d, 0x6c, 0x6b, 0x6a, 0x69,
94         0x67, 0x66, 0x65, 0x63, 0x5c, 0x5a, 0x59, 0x56,
95         0x55, 0x54, 0x53, 0x52, 0x51, 0x4e, 0x4d, 0x4c,
96         0x4b, 0x4a, 0x49, 0x47, 0x46, 0x45, 0x43, 0x3c,
97         0x3a, 0x39, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31,
98         0x2e, 0x2d, 0x2c, 0x2b, 0x2a, 0x29, 0x27, 0x26,
99         0x25, 0x23, 0x1f, 0x1e, 0x1d, 0x1b, 0x18, 0x17,
100         0x10, 0x0f, 0x08, 0x04, 0x02, 0x01, 0x00
101 };
102
103 /*
104  * Local function prototypes.
105  */
106 static int isp_parse_async(ispsoftc_t *, uint16_t);
107 static int isp_parse_async_fc(ispsoftc_t *, uint16_t);
108 static int isp_handle_other_response(ispsoftc_t *, int, isphdr_t *, uint32_t *);
109 static void isp_parse_status(ispsoftc_t *, ispstatusreq_t *, XS_T *, long *); static void
110 isp_parse_status_24xx(ispsoftc_t *, isp24xx_statusreq_t *, XS_T *, long *);
111 static void isp_fastpost_complete(ispsoftc_t *, uint32_t);
112 static int isp_mbox_continue(ispsoftc_t *);
113 static void isp_scsi_init(ispsoftc_t *);
114 static void isp_scsi_channel_init(ispsoftc_t *, int);
115 static void isp_fibre_init(ispsoftc_t *);
116 static void isp_fibre_init_2400(ispsoftc_t *);
117 static void isp_mark_portdb(ispsoftc_t *, int, int);
118 static int isp_plogx(ispsoftc_t *, int, uint16_t, uint32_t, int, int);
119 static int isp_port_login(ispsoftc_t *, uint16_t, uint32_t);
120 static int isp_port_logout(ispsoftc_t *, uint16_t, uint32_t);
121 static int isp_getpdb(ispsoftc_t *, int, uint16_t, isp_pdb_t *, int);
122 static void isp_dump_chip_portdb(ispsoftc_t *, int, int);
123 static uint64_t isp_get_wwn(ispsoftc_t *, int, int, int);
124 static int isp_fclink_test(ispsoftc_t *, int, int);
125 static int isp_pdb_sync(ispsoftc_t *, int);
126 static int isp_scan_loop(ispsoftc_t *, int);
127 static int isp_gid_ft_sns(ispsoftc_t *, int);
128 static int isp_gid_ft_ct_passthru(ispsoftc_t *, int);
129 static int isp_scan_fabric(ispsoftc_t *, int);
130 static int isp_login_device(ispsoftc_t *, int, uint32_t, isp_pdb_t *, uint16_t *);
131 static int isp_register_fc4_type(ispsoftc_t *, int);
132 static int isp_register_fc4_type_24xx(ispsoftc_t *, int);
133 static uint16_t isp_nxt_handle(ispsoftc_t *, int, uint16_t);
134 static void isp_fw_state(ispsoftc_t *, int);
135 static void isp_mboxcmd_qnw(ispsoftc_t *, mbreg_t *, int);
136 static void isp_mboxcmd(ispsoftc_t *, mbreg_t *);
137
138 static void isp_spi_update(ispsoftc_t *, int);
139 static void isp_setdfltsdparm(ispsoftc_t *);
140 static void isp_setdfltfcparm(ispsoftc_t *, int);
141 static int isp_read_nvram(ispsoftc_t *, int);
142 static int isp_read_nvram_2400(ispsoftc_t *, uint8_t *);
143 static void isp_rdnvram_word(ispsoftc_t *, int, uint16_t *);
144 static void isp_rd_2400_nvram(ispsoftc_t *, uint32_t, uint32_t *);
145 static void isp_parse_nvram_1020(ispsoftc_t *, uint8_t *);
146 static void isp_parse_nvram_1080(ispsoftc_t *, int, uint8_t *);
147 static void isp_parse_nvram_12160(ispsoftc_t *, int, uint8_t *);
148 static void isp_parse_nvram_2100(ispsoftc_t *, uint8_t *);
149 static void isp_parse_nvram_2400(ispsoftc_t *, uint8_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
160 isp_reset(ispsoftc_t *isp, int do_load_defaults)
161 {
162         mbreg_t mbs;
163         char *buf;
164         uint64_t fwt;
165         uint32_t code_org, val;
166         int loops, i, dodnld = 1;
167         const char *btype = "????";
168         static const char dcrc[] = "Downloaded RISC Code Checksum Failure";
169
170         isp->isp_state = ISP_NILSTATE;
171         if (isp->isp_dead) {
172                 isp_shutdown(isp);
173                 ISP_DISABLE_INTS(isp);
174                 return;
175         }
176
177         /*
178          * Basic types (SCSI, FibreChannel and PCI or SBus)
179          * have been set in the MD code. We figure out more
180          * here. Possibly more refined types based upon PCI
181          * identification. Chip revision has been gathered.
182          *
183          * After we've fired this chip up, zero out the conf1 register
184          * for SCSI adapters and do other settings for the 2100.
185          */
186
187         ISP_DISABLE_INTS(isp);
188
189         /*
190          * Pick an initial maxcmds value which will be used
191          * to allocate xflist pointer space. It may be changed
192          * later by the firmware.
193          */
194         if (IS_24XX(isp)) {
195                 isp->isp_maxcmds = 4096;
196         } else if (IS_2322(isp)) {
197                 isp->isp_maxcmds = 2048;
198         } else if (IS_23XX(isp) || IS_2200(isp)) {
199                 isp->isp_maxcmds = 1024;
200         } else {
201                 isp->isp_maxcmds = 512;
202         }
203
204         /*
205          * Set up DMA for the request and response queues.
206          *
207          * We do this now so we can use the request queue
208          * for dma to load firmware from.
209          */
210         if (ISP_MBOXDMASETUP(isp) != 0) {
211                 isp_prt(isp, ISP_LOGERR, "Cannot setup DMA");
212                 return;
213         }
214
215         /*
216          * Set up default request/response queue in-pointer/out-pointer
217          * register indices.
218          */
219         if (IS_24XX(isp)) {
220                 isp->isp_rqstinrp = BIU2400_REQINP;
221                 isp->isp_rqstoutrp = BIU2400_REQOUTP;
222                 isp->isp_respinrp = BIU2400_RSPINP;
223                 isp->isp_respoutrp = BIU2400_RSPOUTP;
224         } else if (IS_23XX(isp)) {
225                 isp->isp_rqstinrp = BIU_REQINP;
226                 isp->isp_rqstoutrp = BIU_REQOUTP;
227                 isp->isp_respinrp = BIU_RSPINP;
228                 isp->isp_respoutrp = BIU_RSPOUTP;
229         } else {
230                 isp->isp_rqstinrp = INMAILBOX4;
231                 isp->isp_rqstoutrp = OUTMAILBOX4;
232                 isp->isp_respinrp = OUTMAILBOX5;
233                 isp->isp_respoutrp = INMAILBOX5;
234         }
235
236         /*
237          * Put the board into PAUSE mode (so we can read the SXP registers
238          * or write FPM/FBM registers).
239          */
240         if (IS_24XX(isp)) {
241                 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_HOST_INT);
242                 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
243                 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_PAUSE);
244         } else {
245                 ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
246         }
247
248         if (IS_FC(isp)) {
249                 switch (isp->isp_type) {
250                 case ISP_HA_FC_2100:
251                         btype = "2100";
252                         break;
253                 case ISP_HA_FC_2200:
254                         btype = "2200";
255                         break;
256                 case ISP_HA_FC_2300:
257                         btype = "2300";
258                         break;
259                 case ISP_HA_FC_2312:
260                         btype = "2312";
261                         break;
262                 case ISP_HA_FC_2322:
263                         btype = "2322";
264                         break;
265                 case ISP_HA_FC_2400:
266                         btype = "2422";
267                         break;
268                 case ISP_HA_FC_2500:
269                         btype = "2532";
270                         break;
271                 default:
272                         break;
273                 }
274
275                 if (!IS_24XX(isp)) {
276                         /*
277                          * While we're paused, reset the FPM module and FBM
278                          * fifos.
279                          */
280                         ISP_WRITE(isp, BIU2100_CSR, BIU2100_FPM0_REGS);
281                         ISP_WRITE(isp, FPM_DIAG_CONFIG, FPM_SOFT_RESET);
282                         ISP_WRITE(isp, BIU2100_CSR, BIU2100_FB_REGS);
283                         ISP_WRITE(isp, FBM_CMD, FBMCMD_FIFO_RESET_ALL);
284                         ISP_WRITE(isp, BIU2100_CSR, BIU2100_RISC_REGS);
285                 }
286         } else if (IS_1240(isp)) {
287                 sdparam *sdp;
288
289                 btype = "1240";
290                 isp->isp_clock = 60;
291                 sdp = SDPARAM(isp, 0);
292                 sdp->isp_ultramode = 1;
293                 sdp = SDPARAM(isp, 1);
294                 sdp->isp_ultramode = 1;
295                 /*
296                  * XXX: Should probably do some bus sensing.
297                  */
298         } else if (IS_ULTRA3(isp)) {
299                 sdparam *sdp = isp->isp_param;
300
301                 isp->isp_clock = 100;
302
303                 if (IS_10160(isp))
304                         btype = "10160";
305                 else if (IS_12160(isp))
306                         btype = "12160";
307                 else
308                         btype = "<UNKLVD>";
309                 sdp->isp_lvdmode = 1;
310
311                 if (IS_DUALBUS(isp)) {
312                         sdp++;
313                         sdp->isp_lvdmode = 1;
314                 }
315         } else if (IS_ULTRA2(isp)) {
316                 static const char m[] = "bus %d is in %s Mode";
317                 uint16_t l;
318                 sdparam *sdp = SDPARAM(isp, 0);
319
320                 isp->isp_clock = 100;
321
322                 if (IS_1280(isp))
323                         btype = "1280";
324                 else if (IS_1080(isp))
325                         btype = "1080";
326                 else
327                         btype = "<UNKLVD>";
328
329                 l = ISP_READ(isp, SXP_PINS_DIFF) & ISP1080_MODE_MASK;
330                 switch (l) {
331                 case ISP1080_LVD_MODE:
332                         sdp->isp_lvdmode = 1;
333                         isp_prt(isp, ISP_LOGCONFIG, m, 0, "LVD");
334                         break;
335                 case ISP1080_HVD_MODE:
336                         sdp->isp_diffmode = 1;
337                         isp_prt(isp, ISP_LOGCONFIG, m, 0, "Differential");
338                         break;
339                 case ISP1080_SE_MODE:
340                         sdp->isp_ultramode = 1;
341                         isp_prt(isp, ISP_LOGCONFIG, m, 0, "Single-Ended");
342                         break;
343                 default:
344                         isp_prt(isp, ISP_LOGERR,
345                             "unknown mode on bus %d (0x%x)", 0, l);
346                         break;
347                 }
348
349                 if (IS_DUALBUS(isp)) {
350                         sdp = SDPARAM(isp, 1);
351                         l = ISP_READ(isp, SXP_PINS_DIFF|SXP_BANK1_SELECT);
352                         l &= ISP1080_MODE_MASK;
353                         switch (l) {
354                         case ISP1080_LVD_MODE:
355                                 sdp->isp_lvdmode = 1;
356                                 isp_prt(isp, ISP_LOGCONFIG, m, 1, "LVD");
357                                 break;
358                         case ISP1080_HVD_MODE:
359                                 sdp->isp_diffmode = 1;
360                                 isp_prt(isp, ISP_LOGCONFIG,
361                                     m, 1, "Differential");
362                                 break;
363                         case ISP1080_SE_MODE:
364                                 sdp->isp_ultramode = 1;
365                                 isp_prt(isp, ISP_LOGCONFIG,
366                                     m, 1, "Single-Ended");
367                                 break;
368                         default:
369                                 isp_prt(isp, ISP_LOGERR,
370                                     "unknown mode on bus %d (0x%x)", 1, l);
371                                 break;
372                         }
373                 }
374         } else {
375                 sdparam *sdp = SDPARAM(isp, 0);
376                 i = ISP_READ(isp, BIU_CONF0) & BIU_CONF0_HW_MASK;
377                 switch (i) {
378                 default:
379                         isp_prt(isp, ISP_LOGALL, "Unknown Chip Type 0x%x", i);
380                         /* FALLTHROUGH */
381                 case 1:
382                         btype = "1020";
383                         isp->isp_type = ISP_HA_SCSI_1020;
384                         isp->isp_clock = 40;
385                         break;
386                 case 2:
387                         /*
388                          * Some 1020A chips are Ultra Capable, but don't
389                          * run the clock rate up for that unless told to
390                          * do so by the Ultra Capable bits being set.
391                          */
392                         btype = "1020A";
393                         isp->isp_type = ISP_HA_SCSI_1020A;
394                         isp->isp_clock = 40;
395                         break;
396                 case 3:
397                         btype = "1040";
398                         isp->isp_type = ISP_HA_SCSI_1040;
399                         isp->isp_clock = 60;
400                         break;
401                 case 4:
402                         btype = "1040A";
403                         isp->isp_type = ISP_HA_SCSI_1040A;
404                         isp->isp_clock = 60;
405                         break;
406                 case 5:
407                         btype = "1040B";
408                         isp->isp_type = ISP_HA_SCSI_1040B;
409                         isp->isp_clock = 60;
410                         break;
411                 case 6:
412                         btype = "1040C";
413                         isp->isp_type = ISP_HA_SCSI_1040C;
414                         isp->isp_clock = 60;
415                         break;
416                 }
417                 /*
418                  * Now, while we're at it, gather info about ultra
419                  * and/or differential mode.
420                  */
421                 if (ISP_READ(isp, SXP_PINS_DIFF) & SXP_PINS_DIFF_MODE) {
422                         isp_prt(isp, ISP_LOGCONFIG, "Differential Mode");
423                         sdp->isp_diffmode = 1;
424                 } else {
425                         sdp->isp_diffmode = 0;
426                 }
427                 i = ISP_READ(isp, RISC_PSR);
428                 if (isp->isp_bustype == ISP_BT_SBUS) {
429                         i &= RISC_PSR_SBUS_ULTRA;
430                 } else {
431                         i &= RISC_PSR_PCI_ULTRA;
432                 }
433                 if (i != 0) {
434                         isp_prt(isp, ISP_LOGCONFIG, "Ultra Mode Capable");
435                         sdp->isp_ultramode = 1;
436                         /*
437                          * If we're in Ultra Mode, we have to be 60MHz clock-
438                          * even for the SBus version.
439                          */
440                         isp->isp_clock = 60;
441                 } else {
442                         sdp->isp_ultramode = 0;
443                         /*
444                          * Clock is known. Gronk.
445                          */
446                 }
447
448                 /*
449                  * Machine dependent clock (if set) overrides
450                  * our generic determinations.
451                  */
452                 if (isp->isp_mdvec->dv_clock) {
453                         if (isp->isp_mdvec->dv_clock < isp->isp_clock) {
454                                 isp->isp_clock = isp->isp_mdvec->dv_clock;
455                         }
456                 }
457
458         }
459
460         /*
461          * Clear instrumentation
462          */
463         isp->isp_intcnt = isp->isp_intbogus = 0;
464
465         /*
466          * Do MD specific pre initialization
467          */
468         ISP_RESET0(isp);
469
470         /*
471          * Hit the chip over the head with hammer,
472          * and give it a chance to recover.
473          */
474
475         if (IS_SCSI(isp)) {
476                 ISP_WRITE(isp, BIU_ICR, BIU_ICR_SOFT_RESET);
477                 /*
478                  * A slight delay...
479                  */
480                 ISP_DELAY(100);
481
482                 /*
483                  * Clear data && control DMA engines.
484                  */
485                 ISP_WRITE(isp, CDMA_CONTROL, DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
486                 ISP_WRITE(isp, DDMA_CONTROL, DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
487
488
489         } else if (IS_24XX(isp)) {
490                 /*
491                  * Stop DMA and wait for it to stop.
492                  */
493                 ISP_WRITE(isp, BIU2400_CSR, BIU2400_DMA_STOP|(3 << 4));
494                 for (val = loops = 0; loops < 30000; loops++) {
495                         ISP_DELAY(10);
496                         val = ISP_READ(isp, BIU2400_CSR);
497                         if ((val & BIU2400_DMA_ACTIVE) == 0) {
498                                 break;
499                         }
500                 }
501                 if (val & BIU2400_DMA_ACTIVE) {
502                         ISP_RESET0(isp);
503                         isp_prt(isp, ISP_LOGERR, "DMA Failed to Stop on Reset");
504                         return;
505                 }
506                 /*
507                  * Hold it in SOFT_RESET and STOP state for 100us.
508                  */
509                 ISP_WRITE(isp, BIU2400_CSR, BIU2400_SOFT_RESET|BIU2400_DMA_STOP|(3 << 4));
510                 ISP_DELAY(100);
511                 for (loops = 0; loops < 10000; loops++) {
512                         ISP_DELAY(5);
513                         val = ISP_READ(isp, OUTMAILBOX0);
514                 }
515                 for (val = loops = 0; loops < 500000; loops ++) {
516                         val = ISP_READ(isp, BIU2400_CSR);
517                         if ((val & BIU2400_SOFT_RESET) == 0) {
518                                 break;
519                         }
520                 }
521                 if (val & BIU2400_SOFT_RESET) {
522                         ISP_RESET0(isp);
523                         isp_prt(isp, ISP_LOGERR, "Failed to come out of reset");
524                         return;
525                 }
526         } else {
527                 ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
528                 /*
529                  * A slight delay...
530                  */
531                 ISP_DELAY(100);
532
533                 /*
534                  * Clear data && control DMA engines.
535                  */
536                 ISP_WRITE(isp, CDMA2100_CONTROL, DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
537                 ISP_WRITE(isp, TDMA2100_CONTROL, DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
538                 ISP_WRITE(isp, RDMA2100_CONTROL, DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
539         }
540
541         /*
542          * Wait for ISP to be ready to go...
543          */
544         loops = MBOX_DELAY_COUNT;
545         for (;;) {
546                 if (IS_SCSI(isp)) {
547                         if (!(ISP_READ(isp, BIU_ICR) & BIU_ICR_SOFT_RESET)) {
548                                 break;
549                         }
550                 } else if (IS_24XX(isp)) {
551                         if (ISP_READ(isp, OUTMAILBOX0) == 0) {
552                                 break;
553                         }
554                 } else {
555                         if (!(ISP_READ(isp, BIU2100_CSR) & BIU2100_SOFT_RESET))
556                                 break;
557                 }
558                 ISP_DELAY(100);
559                 if (--loops < 0) {
560                         ISP_DUMPREGS(isp, "chip reset timed out");
561                         ISP_RESET0(isp);
562                         return;
563                 }
564         }
565
566         /*
567          * After we've fired this chip up, zero out the conf1 register
568          * for SCSI adapters and other settings for the 2100.
569          */
570
571         if (IS_SCSI(isp)) {
572                 ISP_WRITE(isp, BIU_CONF1, 0);
573         } else if (!IS_24XX(isp)) {
574                 ISP_WRITE(isp, BIU2100_CSR, 0);
575         }
576
577         /*
578          * Reset RISC Processor
579          */
580         if (IS_24XX(isp)) {
581                 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_RESET);
582                 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_RELEASE);
583                 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RESET);
584         } else {
585                 ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
586                 ISP_DELAY(100);
587                 ISP_WRITE(isp, BIU_SEMA, 0);
588         }
589
590         /*
591          * Post-RISC Reset stuff.
592          */
593         if (IS_24XX(isp)) {
594                 for (val = loops = 0; loops < 5000000; loops++) {
595                         ISP_DELAY(5);
596                         val = ISP_READ(isp, OUTMAILBOX0);
597                         if (val == 0) {
598                                 break;
599                         }
600                 }
601                 if (val != 0) {
602                         ISP_RESET0(isp);
603                         isp_prt(isp, ISP_LOGERR, "reset didn't clear");
604                         return;
605                 }
606         } else if (IS_SCSI(isp)) {
607                 uint16_t tmp = isp->isp_mdvec->dv_conf1;
608                 /*
609                  * Busted FIFO. Turn off all but burst enables.
610                  */
611                 if (isp->isp_type == ISP_HA_SCSI_1040A) {
612                         tmp &= BIU_BURST_ENABLE;
613                 }
614                 ISP_SETBITS(isp, BIU_CONF1, tmp);
615                 if (tmp & BIU_BURST_ENABLE) {
616                         ISP_SETBITS(isp, CDMA_CONF, DMA_ENABLE_BURST);
617                         ISP_SETBITS(isp, DDMA_CONF, DMA_ENABLE_BURST);
618                 }
619                 if (SDPARAM(isp, 0)->isp_ptisp) {
620                         if (SDPARAM(isp, 0)->isp_ultramode) {
621                                 while (ISP_READ(isp, RISC_MTR) != 0x1313) {
622                                         ISP_WRITE(isp, RISC_MTR, 0x1313);
623                                         ISP_WRITE(isp, HCCR, HCCR_CMD_STEP);
624                                 }
625                         } else {
626                                 ISP_WRITE(isp, RISC_MTR, 0x1212);
627                         }
628                         /*
629                          * PTI specific register
630                          */
631                         ISP_WRITE(isp, RISC_EMB, DUAL_BANK);
632                 } else {
633                         ISP_WRITE(isp, RISC_MTR, 0x1212);
634                 }
635                 ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
636         } else {
637                 ISP_WRITE(isp, RISC_MTR2100, 0x1212);
638                 if (IS_2200(isp) || IS_23XX(isp)) {
639                         ISP_WRITE(isp, HCCR, HCCR_2X00_DISABLE_PARITY_PAUSE);
640                 }
641                 ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
642         }
643
644         ISP_WRITE(isp, isp->isp_rqstinrp, 0);
645         ISP_WRITE(isp, isp->isp_rqstoutrp, 0);
646         ISP_WRITE(isp, isp->isp_respinrp, 0);
647         ISP_WRITE(isp, isp->isp_respoutrp, 0);
648         if (IS_24XX(isp)) {
649                 ISP_WRITE(isp, BIU2400_PRI_REQINP, 0);
650                 ISP_WRITE(isp, BIU2400_PRI_REQOUTP, 0);
651                 ISP_WRITE(isp, BIU2400_ATIO_RSPINP, 0);
652                 ISP_WRITE(isp, BIU2400_ATIO_RSPOUTP, 0);
653         }
654
655         /*
656          * Do MD specific post initialization
657          */
658         ISP_RESET1(isp);
659
660         /*
661          * Wait for everything to finish firing up.
662          *
663          * Avoid doing this on early 2312s because you can generate a PCI
664          * parity error (chip breakage).
665          */
666         if (IS_2312(isp) && isp->isp_revision < 2) {
667                 ISP_DELAY(100);
668         } else {
669                 loops = MBOX_DELAY_COUNT;
670                 while (ISP_READ(isp, OUTMAILBOX0) == MBOX_BUSY) {
671                         ISP_DELAY(100);
672                         if (--loops < 0) {
673                                 ISP_RESET0(isp);
674                                 isp_prt(isp, ISP_LOGERR, "MBOX_BUSY never cleared on reset");
675                                 return;
676                         }
677                 }
678         }
679
680         /*
681          * Up until this point we've done everything by just reading or
682          * setting registers. From this point on we rely on at least *some*
683          * kind of firmware running in the card.
684          */
685
686         /*
687          * Do some sanity checking by running a NOP command.
688          * If it succeeds, the ROM firmware is now running.
689          */
690         MBSINIT(&mbs, MBOX_NO_OP, MBLOGALL, 0);
691         isp_mboxcmd(isp, &mbs);
692         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
693                 isp_prt(isp, ISP_LOGERR, "NOP command failed (%x)", mbs.param[0]);
694                 ISP_RESET0(isp);
695                 return;
696         }
697
698         /*
699          * Do some operational tests
700          */
701
702         if (IS_SCSI(isp) || IS_24XX(isp)) {
703                 static const uint16_t patterns[MAX_MAILBOX] = {
704                         0x0000, 0xdead, 0xbeef, 0xffff,
705                         0xa5a5, 0x5a5a, 0x7f7f, 0x7ff7,
706                         0x3421, 0xabcd, 0xdcba, 0xfeef,
707                         0xbead, 0xdebe, 0x2222, 0x3333,
708                         0x5555, 0x6666, 0x7777, 0xaaaa,
709                         0xffff, 0xdddd, 0x9999, 0x1fbc,
710                         0x6666, 0x6677, 0x1122, 0x33ff,
711                         0x0000, 0x0001, 0x1000, 0x1010,
712                 };
713                 int nmbox = ISP_NMBOX(isp);
714                 if (IS_SCSI(isp))
715                         nmbox = 6;
716                 MBSINIT(&mbs, MBOX_MAILBOX_REG_TEST, MBLOGALL, 0);
717                 for (i = 1; i < nmbox; i++) {
718                         mbs.param[i] = patterns[i];
719                 }
720                 isp_mboxcmd(isp, &mbs);
721                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
722                         ISP_RESET0(isp);
723                         return;
724                 }
725                 for (i = 1; i < nmbox; i++) {
726                         if (mbs.param[i] != patterns[i]) {
727                                 ISP_RESET0(isp);
728                                 isp_prt(isp, ISP_LOGERR, "Register Test Failed at Register %d: should have 0x%04x but got 0x%04x", i, patterns[i], mbs.param[i]);
729                                 return;
730                         }
731                 }
732         }
733
734         /*
735          * Download new Firmware, unless requested not to do so.
736          * This is made slightly trickier in some cases where the
737          * firmware of the ROM revision is newer than the revision
738          * compiled into the driver. So, where we used to compare
739          * versions of our f/w and the ROM f/w, now we just see
740          * whether we have f/w at all and whether a config flag
741          * has disabled our download.
742          */
743         if ((isp->isp_mdvec->dv_ispfw == NULL) || (isp->isp_confopts & ISP_CFG_NORELOAD)) {
744                 dodnld = 0;
745         }
746
747         if (IS_24XX(isp)) {
748                 code_org = ISP_CODE_ORG_2400;
749         } else if (IS_23XX(isp)) {
750                 code_org = ISP_CODE_ORG_2300;
751         } else {
752                 code_org = ISP_CODE_ORG;
753         }
754
755         if (dodnld && IS_24XX(isp)) {
756                 const uint32_t *ptr = isp->isp_mdvec->dv_ispfw;
757                 int wordload;
758
759                 /*
760                  * Keep loading until we run out of f/w.
761                  */
762                 code_org = ptr[2];      /* 1st load address is our start addr */
763                 wordload = 0;
764
765                 for (;;) {
766                         uint32_t la, wi, wl;
767
768                         isp_prt(isp, ISP_LOGDEBUG0, "load 0x%x words of code at load address 0x%x", ptr[3], ptr[2]);
769
770                         wi = 0;
771                         la = ptr[2];
772                         wl = ptr[3];
773
774                         while (wi < ptr[3]) {
775                                 uint32_t *cp;
776                                 uint32_t nw;
777
778                                 nw = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp)) >> 2;
779                                 if (nw > wl) {
780                                         nw = wl;
781                                 }
782                                 cp = isp->isp_rquest;
783                                 for (i = 0; i < nw; i++) {
784                                         ISP_IOXPUT_32(isp,  ptr[wi++], &cp[i]);
785                                         wl--;
786                                 }
787                                 MEMORYBARRIER(isp, SYNC_REQUEST, 0, ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp)), -1);
788         again:
789                                 MBSINIT(&mbs, 0, MBLOGALL, 0);
790                                 if (la < 0x10000 && nw < 0x10000) {
791                                         mbs.param[0] = MBOX_LOAD_RISC_RAM_2100;
792                                         mbs.param[1] = la;
793                                         mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
794                                         mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
795                                         mbs.param[4] = nw;
796                                         mbs.param[6] = DMA_WD3(isp->isp_rquest_dma);
797                                         mbs.param[7] = DMA_WD2(isp->isp_rquest_dma);
798                                         isp_prt(isp, ISP_LOGDEBUG0, "LOAD RISC RAM 2100 %u words at load address 0x%x", nw, la);
799                                 } else if (wordload) {
800                                         union {
801                                                 const uint32_t *cp;
802                                                 uint32_t *np;
803                                         } ucd;
804                                         ucd.cp = (const uint32_t *)cp;
805                                         mbs.param[0] = MBOX_WRITE_RAM_WORD_EXTENDED;
806                                         mbs.param[1] = la;
807                                         mbs.param[2] = (*ucd.np);
808                                         mbs.param[3] = (*ucd.np) >> 16;
809                                         mbs.param[8] = la >> 16;
810                                         isp->isp_mbxwrk0 = nw - 1;
811                                         isp->isp_mbxworkp = ucd.np+1;
812                                         isp->isp_mbxwrk1 = (la + 1);
813                                         isp->isp_mbxwrk8 = (la + 1) >> 16;
814                                         isp_prt(isp, ISP_LOGDEBUG0, "WRITE RAM WORD EXTENDED %u words at load address 0x%x", nw, la);
815                                 } else {
816                                         mbs.param[0] = MBOX_LOAD_RISC_RAM;
817                                         mbs.param[1] = la;
818                                         mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
819                                         mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
820                                         mbs.param[4] = nw >> 16;
821                                         mbs.param[5] = nw;
822                                         mbs.param[6] = DMA_WD3(isp->isp_rquest_dma);
823                                         mbs.param[7] = DMA_WD2(isp->isp_rquest_dma);
824                                         mbs.param[8] = la >> 16;
825                                         isp_prt(isp, ISP_LOGDEBUG0, "LOAD RISC RAM %u words at load address 0x%x", nw, la);
826                                 }
827                                 isp_mboxcmd(isp, &mbs);
828                                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
829                                         if (mbs.param[0] == MBOX_HOST_INTERFACE_ERROR) {
830                                                 isp_prt(isp, ISP_LOGERR, "switching to word load");
831                                                 wordload = 1;
832                                                 goto again;
833                                         }
834                                         isp_prt(isp, ISP_LOGERR, "F/W Risc Ram Load Failed");
835                                         ISP_RESET0(isp);
836                                         return;
837                                 }
838                                 la += nw;
839                         }
840
841                         if (ptr[1] == 0) {
842                                 break;
843                         }
844                         ptr += ptr[3];
845                 }
846                 isp->isp_loaded_fw = 1;
847         } else if (dodnld && IS_23XX(isp)) {
848                 const uint16_t *ptr = isp->isp_mdvec->dv_ispfw;
849                 uint16_t wi, wl, segno;
850                 uint32_t la;
851
852                 la = code_org;
853                 segno = 0;
854
855                 for (;;) {
856                         uint32_t nxtaddr;
857
858                         isp_prt(isp, ISP_LOGDEBUG0, "load 0x%x words of code at load address 0x%x", ptr[3], la);
859
860                         wi = 0;
861                         wl = ptr[3];
862
863                         while (wi < ptr[3]) {
864                                 uint16_t *cp;
865                                 uint16_t nw;
866
867                                 nw = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp)) >> 1;
868                                 if (nw > wl) {
869                                         nw = wl;
870                                 }
871                                 if (nw > (1 << 15)) {
872                                         nw = 1 << 15;
873                                 }
874                                 cp = isp->isp_rquest;
875                                 for (i = 0; i < nw; i++) {
876                                         ISP_IOXPUT_16(isp,  ptr[wi++], &cp[i]);
877                                         wl--;
878                                 }
879                                 MEMORYBARRIER(isp, SYNC_REQUEST, 0, ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp)), -1);
880                                 MBSINIT(&mbs, 0, MBLOGALL, 0);
881                                 if (la < 0x10000) {
882                                         mbs.param[0] = MBOX_LOAD_RISC_RAM_2100;
883                                         mbs.param[1] = la;
884                                         mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
885                                         mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
886                                         mbs.param[4] = nw;
887                                         mbs.param[6] = DMA_WD3(isp->isp_rquest_dma);
888                                         mbs.param[7] = DMA_WD2(isp->isp_rquest_dma);
889                                         isp_prt(isp, ISP_LOGDEBUG1, "LOAD RISC RAM 2100 %u words at load address 0x%x\n", nw, la);
890                                 } else {
891                                         mbs.param[0] = MBOX_LOAD_RISC_RAM;
892                                         mbs.param[1] = la;
893                                         mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
894                                         mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
895                                         mbs.param[4] = nw;
896                                         mbs.param[6] = DMA_WD3(isp->isp_rquest_dma);
897                                         mbs.param[7] = DMA_WD2(isp->isp_rquest_dma);
898                                         mbs.param[8] = la >> 16;
899                                         isp_prt(isp, ISP_LOGDEBUG1, "LOAD RISC RAM %u words at load address 0x%x\n", nw, la);
900                                 }
901                                 isp_mboxcmd(isp, &mbs);
902                                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
903                                         isp_prt(isp, ISP_LOGERR, "F/W Risc Ram Load Failed");
904                                         ISP_RESET0(isp);
905                                         return;
906                                 }
907                                 la += nw;
908                         }
909
910                         if (!IS_2322(isp)) {
911                                 break;
912                         }
913
914                         if (++segno == 3) {
915                                 break;
916                         }
917
918                         /*
919                          * If we're a 2322, the firmware actually comes in
920                          * three chunks. We loaded the first at the code_org
921                          * address. The other two chunks, which follow right
922                          * after each other in memory here, get loaded at
923                          * addresses specfied at offset 0x9..0xB.
924                          */
925
926                         nxtaddr = ptr[3];
927                         ptr = &ptr[nxtaddr];
928                         la = ptr[5] | ((ptr[4] & 0x3f) << 16);
929                 }
930                 isp->isp_loaded_fw = 1;
931         } else if (dodnld) {
932                 union {
933                         const uint16_t *cp;
934                         uint16_t *np;
935                 } ucd;
936                 ucd.cp = isp->isp_mdvec->dv_ispfw;
937                 isp->isp_mbxworkp = &ucd.np[1];
938                 isp->isp_mbxwrk0 = ucd.np[3] - 1;
939                 isp->isp_mbxwrk1 = code_org + 1;
940                 MBSINIT(&mbs, MBOX_WRITE_RAM_WORD, MBLOGNONE, 0);
941                 mbs.param[1] = code_org;
942                 mbs.param[2] = ucd.np[0];
943                 isp_prt(isp, ISP_LOGDEBUG1, "WRITE RAM %u words at load address 0x%x", ucd.np[3], code_org);
944                 isp_mboxcmd(isp, &mbs);
945                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
946                         isp_prt(isp, ISP_LOGERR, "F/W download failed at word %d", isp->isp_mbxwrk1 - code_org);
947                         ISP_RESET0(isp);
948                         return;
949                 }
950         } else {
951                 isp->isp_loaded_fw = 0;
952                 isp_prt(isp, ISP_LOGDEBUG2, "skipping f/w download");
953         }
954
955         /*
956          * If we loaded firmware, verify its checksum
957          */
958         if (isp->isp_loaded_fw) {
959                 MBSINIT(&mbs, MBOX_VERIFY_CHECKSUM, MBLOGNONE, 0);
960                 mbs.param[0] = MBOX_VERIFY_CHECKSUM;
961                 if (IS_24XX(isp)) {
962                         mbs.param[1] = code_org >> 16;
963                         mbs.param[2] = code_org;
964                 } else {
965                         mbs.param[1] = code_org;
966                 }
967                 isp_mboxcmd(isp, &mbs);
968                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
969                         isp_prt(isp, ISP_LOGERR, dcrc);
970                         ISP_RESET0(isp);
971                         return;
972                 }
973         }
974
975         /*
976          * Now start it rolling.
977          *
978          * If we didn't actually download f/w,
979          * we still need to (re)start it.
980          */
981
982
983         MBSINIT(&mbs, MBOX_EXEC_FIRMWARE, MBLOGALL, 5000000);
984         if (IS_24XX(isp)) {
985                 mbs.param[1] = code_org >> 16;
986                 mbs.param[2] = code_org;
987                 if (isp->isp_loaded_fw) {
988                         mbs.param[3] = 0;
989                 } else {
990                         mbs.param[3] = 1;
991                 }
992                 if (IS_25XX(isp)) {
993                         mbs.ibits |= 0x10;
994                 }
995         } else if (IS_2322(isp)) {
996                 mbs.param[1] = code_org;
997                 if (isp->isp_loaded_fw) {
998                         mbs.param[2] = 0;
999                 } else {
1000                         mbs.param[2] = 1;
1001                 }
1002         } else {
1003                 mbs.param[1] = code_org;
1004         }
1005         isp_mboxcmd(isp, &mbs);
1006         if (IS_2322(isp) || IS_24XX(isp)) {
1007                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1008                         ISP_RESET0(isp);
1009                         return;
1010                 }
1011         }
1012
1013         /*
1014          * Give it a chance to finish starting up.
1015          * Give the 24XX more time.
1016          */
1017         if (IS_24XX(isp)) {
1018                 ISP_DELAY(500000);
1019                 /*
1020                  * Check to see if the 24XX firmware really started.
1021                  */
1022                 if (mbs.param[1] == 0xdead) {
1023                         isp_prt(isp, ISP_LOGERR, "f/w didn't *really* start");
1024                         ISP_RESET0(isp);
1025                         return;
1026                 }
1027         } else {
1028                 ISP_DELAY(250000);
1029                 if (IS_SCSI(isp)) {
1030                         /*
1031                          * Set CLOCK RATE, but only if asked to.
1032                          */
1033                         if (isp->isp_clock) {
1034                                 mbs.param[0] = MBOX_SET_CLOCK_RATE;
1035                                 mbs.param[1] = isp->isp_clock;
1036                                 mbs.logval = MBLOGNONE;
1037                                 isp_mboxcmd(isp, &mbs);
1038                                 /* we will try not to care if this fails */
1039                         }
1040                 }
1041         }
1042
1043         /*
1044          * Ask the chip for the current firmware version.
1045          * This should prove that the new firmware is working.
1046          */
1047         MBSINIT(&mbs, MBOX_ABOUT_FIRMWARE, MBLOGALL, 0);
1048         isp_mboxcmd(isp, &mbs);
1049         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1050                 ISP_RESET0(isp);
1051                 return;
1052         }
1053
1054         /*
1055          * The SBus firmware that we are using apparently does not return
1056          * major, minor, micro revisions in the mailbox registers, which
1057          * is really, really, annoying.
1058          */
1059         if (ISP_SBUS_SUPPORTED && isp->isp_bustype == ISP_BT_SBUS) {
1060                 if (dodnld) {
1061 #ifdef  ISP_TARGET_MODE
1062                         isp->isp_fwrev[0] = 7;
1063                         isp->isp_fwrev[1] = 55;
1064 #else
1065                         isp->isp_fwrev[0] = 1;
1066                         isp->isp_fwrev[1] = 37;
1067 #endif
1068                         isp->isp_fwrev[2] = 0;
1069                 }
1070         } else {
1071                 isp->isp_fwrev[0] = mbs.param[1];
1072                 isp->isp_fwrev[1] = mbs.param[2];
1073                 isp->isp_fwrev[2] = mbs.param[3];
1074         }
1075
1076         if (IS_FC(isp)) {
1077                 /*
1078                  * We do not believe firmware attributes for 2100 code less
1079                  * than 1.17.0, unless it's the firmware we specifically
1080                  * are loading.
1081                  *
1082                  * Note that all 22XX and later f/w is greater than 1.X.0.
1083                  */
1084                 if ((ISP_FW_OLDER_THAN(isp, 1, 17, 1))) {
1085 #ifdef  USE_SMALLER_2100_FIRMWARE
1086                         isp->isp_fwattr = ISP_FW_ATTR_SCCLUN;
1087 #else
1088                         isp->isp_fwattr = 0;
1089 #endif
1090                 } else {
1091                         isp->isp_fwattr = mbs.param[6];
1092                 }
1093                 if (IS_24XX(isp)) {
1094                         isp->isp_fwattr |= ((uint64_t) mbs.param[15]) << 16;
1095                         if (isp->isp_fwattr & ISP2400_FW_ATTR_EXTNDED) {
1096                                 isp->isp_fwattr |=
1097                                     (((uint64_t) mbs.param[16]) << 32) |
1098                                     (((uint64_t) mbs.param[17]) << 48);
1099                         }
1100                 }
1101         } else if (IS_SCSI(isp)) {
1102 #ifndef ISP_TARGET_MODE
1103                 isp->isp_fwattr = ISP_FW_ATTR_TMODE;
1104 #else
1105                 isp->isp_fwattr = 0;
1106 #endif
1107         }
1108
1109         isp_prt(isp, ISP_LOGCONFIG, "Board Type %s, Chip Revision 0x%x, %s F/W Revision %d.%d.%d",
1110             btype, isp->isp_revision, dodnld? "loaded" : "resident", isp->isp_fwrev[0], isp->isp_fwrev[1], isp->isp_fwrev[2]);
1111
1112         fwt = isp->isp_fwattr;
1113         if (IS_24XX(isp)) {
1114                 buf = FCPARAM(isp, 0)->isp_scratch;
1115                 ISP_SNPRINTF(buf, ISP_FC_SCRLEN, "Attributes:");
1116                 if (fwt & ISP2400_FW_ATTR_CLASS2) {
1117                         fwt ^=ISP2400_FW_ATTR_CLASS2;
1118                         ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s Class2", buf);
1119                 }
1120                 if (fwt & ISP2400_FW_ATTR_IP) {
1121                         fwt ^=ISP2400_FW_ATTR_IP;
1122                         ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s IP", buf);
1123                 }
1124                 if (fwt & ISP2400_FW_ATTR_MULTIID) {
1125                         fwt ^=ISP2400_FW_ATTR_MULTIID;
1126                         ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s MultiID", buf);
1127                 }
1128                 if (fwt & ISP2400_FW_ATTR_SB2) {
1129                         fwt ^=ISP2400_FW_ATTR_SB2;
1130                         ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s SB2", buf);
1131                 }
1132                 if (fwt & ISP2400_FW_ATTR_T10CRC) {
1133                         fwt ^=ISP2400_FW_ATTR_T10CRC;
1134                         ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s T10CRC", buf);
1135                 }
1136                 if (fwt & ISP2400_FW_ATTR_VI) {
1137                         fwt ^=ISP2400_FW_ATTR_VI;
1138                         ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s VI", buf);
1139                 }
1140                 if (fwt & ISP2400_FW_ATTR_MQ) {
1141                         fwt ^=ISP2400_FW_ATTR_MQ;
1142                         ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s MQ", buf);
1143                 }
1144                 if (fwt & ISP2400_FW_ATTR_MSIX) {
1145                         fwt ^=ISP2400_FW_ATTR_MSIX;
1146                         ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s MSIX", buf);
1147                 }
1148                 if (fwt & ISP2400_FW_ATTR_FCOE) {
1149                         fwt ^=ISP2400_FW_ATTR_FCOE;
1150                         ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s FCOE", buf);
1151                 }
1152                 if (fwt & ISP2400_FW_ATTR_VP0) {
1153                         fwt ^= ISP2400_FW_ATTR_VP0;
1154                         ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s VP0_Decoupling", buf);
1155                 }
1156                 if (fwt & ISP2400_FW_ATTR_EXPFW) {
1157                         fwt ^= ISP2400_FW_ATTR_EXPFW;
1158                         ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s (Experimental)", buf);
1159                 }
1160                 if (fwt & ISP2400_FW_ATTR_HOTFW) {
1161                         fwt ^= ISP2400_FW_ATTR_HOTFW;
1162                         ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s HotFW", buf);
1163                 }
1164                 fwt &= ~ISP2400_FW_ATTR_EXTNDED;
1165                 if (fwt & ISP2400_FW_ATTR_EXTVP) {
1166                         fwt ^= ISP2400_FW_ATTR_EXTVP;
1167                         ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s ExtVP", buf);
1168                 }
1169                 if (fwt & ISP2400_FW_ATTR_VN2VN) {
1170                         fwt ^= ISP2400_FW_ATTR_VN2VN;
1171                         ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s VN2VN", buf);
1172                 }
1173                 if (fwt & ISP2400_FW_ATTR_EXMOFF) {
1174                         fwt ^= ISP2400_FW_ATTR_EXMOFF;
1175                         ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s EXMOFF", buf);
1176                 }
1177                 if (fwt & ISP2400_FW_ATTR_NPMOFF) {
1178                         fwt ^= ISP2400_FW_ATTR_NPMOFF;
1179                         ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s NPMOFF", buf);
1180                 }
1181                 if (fwt & ISP2400_FW_ATTR_DIFCHOP) {
1182                         fwt ^= ISP2400_FW_ATTR_DIFCHOP;
1183                         ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s DIFCHOP", buf);
1184                 }
1185                 if (fwt & ISP2400_FW_ATTR_SRIOV) {
1186                         fwt ^= ISP2400_FW_ATTR_SRIOV;
1187                         ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s SRIOV", buf);
1188                 }
1189                 if (fwt & ISP2400_FW_ATTR_ASICTMP) {
1190                         fwt ^= ISP2400_FW_ATTR_ASICTMP;
1191                         ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s ASICTMP", buf);
1192                 }
1193                 if (fwt & ISP2400_FW_ATTR_ATIOMQ) {
1194                         fwt ^= ISP2400_FW_ATTR_ATIOMQ;
1195                         ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s ATIOMQ", buf);
1196                 }
1197                 if (fwt) {
1198                         ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s (unknown 0x%08x%08x)", buf,
1199                             (uint32_t) (fwt >> 32), (uint32_t) fwt);
1200                 }
1201                 isp_prt(isp, ISP_LOGCONFIG, "%s", buf);
1202         } else if (IS_FC(isp)) {
1203                 buf = FCPARAM(isp, 0)->isp_scratch;
1204                 ISP_SNPRINTF(buf, ISP_FC_SCRLEN, "Attributes:");
1205                 if (fwt & ISP_FW_ATTR_TMODE) {
1206                         fwt ^=ISP_FW_ATTR_TMODE;
1207                         ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s TargetMode", buf);
1208                 }
1209                 if (fwt & ISP_FW_ATTR_SCCLUN) {
1210                         fwt ^=ISP_FW_ATTR_SCCLUN;
1211                         ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s SCC-Lun", buf);
1212                 }
1213                 if (fwt & ISP_FW_ATTR_FABRIC) {
1214                         fwt ^=ISP_FW_ATTR_FABRIC;
1215                         ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s Fabric", buf);
1216                 }
1217                 if (fwt & ISP_FW_ATTR_CLASS2) {
1218                         fwt ^=ISP_FW_ATTR_CLASS2;
1219                         ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s Class2", buf);
1220                 }
1221                 if (fwt & ISP_FW_ATTR_FCTAPE) {
1222                         fwt ^=ISP_FW_ATTR_FCTAPE;
1223                         ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s FC-Tape", buf);
1224                 }
1225                 if (fwt & ISP_FW_ATTR_IP) {
1226                         fwt ^=ISP_FW_ATTR_IP;
1227                         ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s IP", buf);
1228                 }
1229                 if (fwt & ISP_FW_ATTR_VI) {
1230                         fwt ^=ISP_FW_ATTR_VI;
1231                         ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s VI", buf);
1232                 }
1233                 if (fwt & ISP_FW_ATTR_VI_SOLARIS) {
1234                         fwt ^=ISP_FW_ATTR_VI_SOLARIS;
1235                         ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s VI_SOLARIS", buf);
1236                 }
1237                 if (fwt & ISP_FW_ATTR_2KLOGINS) {
1238                         fwt ^=ISP_FW_ATTR_2KLOGINS;
1239                         ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s 2K-Login", buf);
1240                 }
1241                 if (fwt != 0) {
1242                         ISP_SNPRINTF(buf, ISP_FC_SCRLEN - strlen(buf), "%s (unknown 0x%08x%08x)", buf,
1243                             (uint32_t) (fwt >> 32), (uint32_t) fwt);
1244                 }
1245                 isp_prt(isp, ISP_LOGCONFIG, "%s", buf);
1246         }
1247
1248         if (IS_24XX(isp)) {
1249                 MBSINIT(&mbs, MBOX_GET_RESOURCE_COUNT, MBLOGALL, 0);
1250                 isp_mboxcmd(isp, &mbs);
1251                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1252                         ISP_RESET0(isp);
1253                         return;
1254                 }
1255                 if (isp->isp_maxcmds >= mbs.param[3]) {
1256                         isp->isp_maxcmds = mbs.param[3];
1257                 }
1258         } else {
1259                 MBSINIT(&mbs, MBOX_GET_FIRMWARE_STATUS, MBLOGALL, 0);
1260                 isp_mboxcmd(isp, &mbs);
1261                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1262                         ISP_RESET0(isp);
1263                         return;
1264                 }
1265                 if (isp->isp_maxcmds >= mbs.param[2]) {
1266                         isp->isp_maxcmds = mbs.param[2];
1267                 }
1268         }
1269         isp_prt(isp, ISP_LOGCONFIG, "%d max I/O command limit set", isp->isp_maxcmds);
1270
1271         /*
1272          * If we don't have Multi-ID f/w loaded, we need to restrict channels to one.
1273          * Only make this check for non-SCSI cards (I'm not sure firmware attributes
1274          * work for them).
1275          */
1276         if (IS_FC(isp) && isp->isp_nchan > 1) {
1277                 if (!ISP_CAP_MULTI_ID(isp)) {
1278                         isp_prt(isp, ISP_LOGWARN, "non-MULTIID f/w loaded, only can enable 1 of %d channels", isp->isp_nchan);
1279                         isp->isp_nchan = 1;
1280                 }
1281         }
1282         for (i = 0; i < isp->isp_nchan; i++) {
1283                 isp_fw_state(isp, i);
1284         }
1285         if (isp->isp_dead) {
1286                 isp_shutdown(isp);
1287                 ISP_DISABLE_INTS(isp);
1288                 return;
1289         }
1290
1291         isp->isp_state = ISP_RESETSTATE;
1292
1293         /*
1294          * Okay- now that we have new firmware running, we now (re)set our
1295          * notion of how many luns we support. This is somewhat tricky because
1296          * if we haven't loaded firmware, we sometimes do not have an easy way
1297          * of knowing how many luns we support.
1298          *
1299          * Expanded lun firmware gives you 32 luns for SCSI cards and
1300          * 16384 luns for Fibre Channel cards.
1301          *
1302          * It turns out that even for QLogic 2100s with ROM 1.10 and above
1303          * we do get a firmware attributes word returned in mailbox register 6.
1304          *
1305          * Because the lun is in a different position in the Request Queue
1306          * Entry structure for Fibre Channel with expanded lun firmware, we
1307          * can only support one lun (lun zero) when we don't know what kind
1308          * of firmware we're running.
1309          */
1310         if (IS_SCSI(isp)) {
1311                 if (dodnld) {
1312                         if (IS_ULTRA2(isp) || IS_ULTRA3(isp)) {
1313                                 isp->isp_maxluns = 32;
1314                         } else {
1315                                 isp->isp_maxluns = 8;
1316                         }
1317                 } else {
1318                         isp->isp_maxluns = 8;
1319                 }
1320         } else {
1321                 if (ISP_CAP_SCCFW(isp)) {
1322                         isp->isp_maxluns = 16384;
1323                 } else {
1324                         isp->isp_maxluns = 16;
1325                 }
1326         }
1327
1328         /*
1329          * We get some default values established. As a side
1330          * effect, NVRAM is read here (unless overriden by
1331          * a configuration flag).
1332          */
1333         if (do_load_defaults) {
1334                 if (IS_SCSI(isp)) {
1335                         isp_setdfltsdparm(isp);
1336                 } else {
1337                         for (i = 0; i < isp->isp_nchan; i++) {
1338                                 isp_setdfltfcparm(isp, i);
1339                         }
1340                 }
1341         }
1342 }
1343
1344 /*
1345  * Initialize Parameters of Hardware to a known state.
1346  *
1347  * Locks are held before coming here.
1348  */
1349
1350 void
1351 isp_init(ispsoftc_t *isp)
1352 {
1353         if (IS_FC(isp)) {
1354                 if (IS_24XX(isp)) {
1355                         isp_fibre_init_2400(isp);
1356                 } else {
1357                         isp_fibre_init(isp);
1358                 }
1359         } else {
1360                 isp_scsi_init(isp);
1361         }
1362         GET_NANOTIME(&isp->isp_init_time);
1363 }
1364
1365 static void
1366 isp_scsi_init(ispsoftc_t *isp)
1367 {
1368         sdparam *sdp_chan0, *sdp_chan1;
1369         mbreg_t mbs;
1370
1371         sdp_chan0 = SDPARAM(isp, 0);
1372         sdp_chan1 = sdp_chan0;
1373         if (IS_DUALBUS(isp)) {
1374                 sdp_chan1 = SDPARAM(isp, 1);
1375         }
1376
1377         /* First do overall per-card settings. */
1378
1379         /*
1380          * If we have fast memory timing enabled, turn it on.
1381          */
1382         if (sdp_chan0->isp_fast_mttr) {
1383                 ISP_WRITE(isp, RISC_MTR, 0x1313);
1384         }
1385
1386         /*
1387          * Set Retry Delay and Count.
1388          * You set both channels at the same time.
1389          */
1390         MBSINIT(&mbs, MBOX_SET_RETRY_COUNT, MBLOGALL, 0);
1391         mbs.param[1] = sdp_chan0->isp_retry_count;
1392         mbs.param[2] = sdp_chan0->isp_retry_delay;
1393         mbs.param[6] = sdp_chan1->isp_retry_count;
1394         mbs.param[7] = sdp_chan1->isp_retry_delay;
1395         isp_mboxcmd(isp, &mbs);
1396         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1397                 return;
1398         }
1399
1400         /*
1401          * Set ASYNC DATA SETUP time. This is very important.
1402          */
1403         MBSINIT(&mbs, MBOX_SET_ASYNC_DATA_SETUP_TIME, MBLOGALL, 0);
1404         mbs.param[1] = sdp_chan0->isp_async_data_setup;
1405         mbs.param[2] = sdp_chan1->isp_async_data_setup;
1406         isp_mboxcmd(isp, &mbs);
1407         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1408                 return;
1409         }
1410
1411         /*
1412          * Set ACTIVE Negation State.
1413          */
1414         MBSINIT(&mbs, MBOX_SET_ACT_NEG_STATE, MBLOGNONE, 0);
1415         mbs.param[1] =
1416             (sdp_chan0->isp_req_ack_active_neg << 4) |
1417             (sdp_chan0->isp_data_line_active_neg << 5);
1418         mbs.param[2] =
1419             (sdp_chan1->isp_req_ack_active_neg << 4) |
1420             (sdp_chan1->isp_data_line_active_neg << 5);
1421         isp_mboxcmd(isp, &mbs);
1422         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1423                 isp_prt(isp, ISP_LOGERR,
1424                     "failed to set active negation state (%d,%d), (%d,%d)",
1425                     sdp_chan0->isp_req_ack_active_neg,
1426                     sdp_chan0->isp_data_line_active_neg,
1427                     sdp_chan1->isp_req_ack_active_neg,
1428                     sdp_chan1->isp_data_line_active_neg);
1429                 /*
1430                  * But don't return.
1431                  */
1432         }
1433
1434         /*
1435          * Set the Tag Aging limit
1436          */
1437         MBSINIT(&mbs, MBOX_SET_TAG_AGE_LIMIT, MBLOGALL, 0);
1438         mbs.param[1] = sdp_chan0->isp_tag_aging;
1439         mbs.param[2] = sdp_chan1->isp_tag_aging;
1440         isp_mboxcmd(isp, &mbs);
1441         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1442                 isp_prt(isp, ISP_LOGERR, "failed to set tag age limit (%d,%d)",
1443                     sdp_chan0->isp_tag_aging, sdp_chan1->isp_tag_aging);
1444                 return;
1445         }
1446
1447         /*
1448          * Set selection timeout.
1449          */
1450         MBSINIT(&mbs, MBOX_SET_SELECT_TIMEOUT, MBLOGALL, 0);
1451         mbs.param[1] = sdp_chan0->isp_selection_timeout;
1452         mbs.param[2] = sdp_chan1->isp_selection_timeout;
1453         isp_mboxcmd(isp, &mbs);
1454         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1455                 return;
1456         }
1457
1458         /* now do per-channel settings */
1459         isp_scsi_channel_init(isp, 0);
1460         if (IS_DUALBUS(isp))
1461                 isp_scsi_channel_init(isp, 1);
1462
1463         /*
1464          * Now enable request/response queues
1465          */
1466
1467         if (IS_ULTRA2(isp) || IS_1240(isp)) {
1468                 MBSINIT(&mbs, MBOX_INIT_RES_QUEUE_A64, MBLOGALL, 0);
1469                 mbs.param[1] = RESULT_QUEUE_LEN(isp);
1470                 mbs.param[2] = DMA_WD1(isp->isp_result_dma);
1471                 mbs.param[3] = DMA_WD0(isp->isp_result_dma);
1472                 mbs.param[4] = 0;
1473                 mbs.param[6] = DMA_WD3(isp->isp_result_dma);
1474                 mbs.param[7] = DMA_WD2(isp->isp_result_dma);
1475                 isp_mboxcmd(isp, &mbs);
1476                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1477                         return;
1478                 }
1479                 isp->isp_residx = isp->isp_resodx = mbs.param[5];
1480
1481                 MBSINIT(&mbs, MBOX_INIT_REQ_QUEUE_A64, MBLOGALL, 0);
1482                 mbs.param[1] = RQUEST_QUEUE_LEN(isp);
1483                 mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
1484                 mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
1485                 mbs.param[5] = 0;
1486                 mbs.param[6] = DMA_WD3(isp->isp_result_dma);
1487                 mbs.param[7] = DMA_WD2(isp->isp_result_dma);
1488                 isp_mboxcmd(isp, &mbs);
1489                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1490                         return;
1491                 }
1492                 isp->isp_reqidx = isp->isp_reqodx = mbs.param[4];
1493         } else {
1494                 MBSINIT(&mbs, MBOX_INIT_RES_QUEUE, MBLOGALL, 0);
1495                 mbs.param[1] = RESULT_QUEUE_LEN(isp);
1496                 mbs.param[2] = DMA_WD1(isp->isp_result_dma);
1497                 mbs.param[3] = DMA_WD0(isp->isp_result_dma);
1498                 mbs.param[4] = 0;
1499                 isp_mboxcmd(isp, &mbs);
1500                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1501                         return;
1502                 }
1503                 isp->isp_residx = isp->isp_resodx = mbs.param[5];
1504
1505                 MBSINIT(&mbs, MBOX_INIT_REQ_QUEUE, MBLOGALL, 0);
1506                 mbs.param[1] = RQUEST_QUEUE_LEN(isp);
1507                 mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
1508                 mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
1509                 mbs.param[5] = 0;
1510                 isp_mboxcmd(isp, &mbs);
1511                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1512                         return;
1513                 }
1514                 isp->isp_reqidx = isp->isp_reqodx = mbs.param[4];
1515         }
1516
1517         /*
1518          * Turn on LVD transitions for ULTRA2 or better and other features
1519          *
1520          * Now that we have 32 bit handles, don't do any fast posting
1521          * any more. For Ultra2/Ultra3 cards, we can turn on 32 bit RIO
1522          * operation or use fast posting. To be conservative, we'll only
1523          * do this for Ultra3 cards now because the other cards are so
1524          * rare for this author to find and test with.
1525          */
1526
1527         MBSINIT(&mbs, MBOX_SET_FW_FEATURES, MBLOGALL, 0);
1528         if (IS_ULTRA2(isp))
1529                 mbs.param[1] |= FW_FEATURE_LVD_NOTIFY;
1530 #ifdef  ISP_NO_RIO
1531         if (IS_ULTRA3(isp))
1532                 mbs.param[1] |= FW_FEATURE_FAST_POST;
1533 #else
1534         if (IS_ULTRA3(isp))
1535                 mbs.param[1] |= FW_FEATURE_RIO_32BIT;
1536 #endif
1537         if (mbs.param[1] != 0) {
1538                 uint16_t sfeat = mbs.param[1];
1539                 isp_mboxcmd(isp, &mbs);
1540                 if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1541                         isp_prt(isp, ISP_LOGINFO,
1542                             "Enabled FW features (0x%x)", sfeat);
1543                 }
1544         }
1545
1546         isp->isp_state = ISP_INITSTATE;
1547 }
1548
1549 static void
1550 isp_scsi_channel_init(ispsoftc_t *isp, int chan)
1551 {
1552         sdparam *sdp;
1553         mbreg_t mbs;
1554         int tgt;
1555
1556         sdp = SDPARAM(isp, chan);
1557
1558         /*
1559          * Set (possibly new) Initiator ID.
1560          */
1561         MBSINIT(&mbs, MBOX_SET_INIT_SCSI_ID, MBLOGALL, 0);
1562         mbs.param[1] = (chan << 7) | sdp->isp_initiator_id;
1563         isp_mboxcmd(isp, &mbs);
1564         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1565                 return;
1566         }
1567         isp_prt(isp, ISP_LOGINFO, "Chan %d Initiator ID is %d",
1568             chan, sdp->isp_initiator_id);
1569
1570
1571         /*
1572          * Set current per-target parameters to an initial safe minimum.
1573          */
1574         for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
1575                 int lun;
1576                 uint16_t sdf;
1577
1578                 if (sdp->isp_devparam[tgt].dev_enable == 0) {
1579                         continue;
1580                 }
1581 #ifndef ISP_TARGET_MODE
1582                 sdf = sdp->isp_devparam[tgt].goal_flags;
1583                 sdf &= DPARM_SAFE_DFLT;
1584                 /*
1585                  * It is not quite clear when this changed over so that
1586                  * we could force narrow and async for 1000/1020 cards,
1587                  * but assume that this is only the case for loaded
1588                  * firmware.
1589                  */
1590                 if (isp->isp_loaded_fw) {
1591                         sdf |= DPARM_NARROW | DPARM_ASYNC;
1592                 }
1593 #else
1594                 /*
1595                  * The !$*!)$!$)* f/w uses the same index into some
1596                  * internal table to decide how to respond to negotiations,
1597                  * so if we've said "let's be safe" for ID X, and ID X
1598                  * selects *us*, the negotiations will back to 'safe'
1599                  * (as in narrow/async). What the f/w *should* do is
1600                  * use the initiator id settings to decide how to respond.
1601                  */
1602                 sdp->isp_devparam[tgt].goal_flags = sdf = DPARM_DEFAULT;
1603 #endif
1604                 MBSINIT(&mbs, MBOX_SET_TARGET_PARAMS, MBLOGNONE, 0);
1605                 mbs.param[1] = (chan << 15) | (tgt << 8);
1606                 mbs.param[2] = sdf;
1607                 if ((sdf & DPARM_SYNC) == 0) {
1608                         mbs.param[3] = 0;
1609                 } else {
1610                         mbs.param[3] =
1611                             (sdp->isp_devparam[tgt].goal_offset << 8) |
1612                             (sdp->isp_devparam[tgt].goal_period);
1613                 }
1614                 isp_prt(isp, ISP_LOGDEBUG0, "Initial Settings bus%d tgt%d flags 0x%x off 0x%x per 0x%x",
1615                     chan, tgt, mbs.param[2], mbs.param[3] >> 8, mbs.param[3] & 0xff);
1616                 isp_mboxcmd(isp, &mbs);
1617                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1618                         sdf = DPARM_SAFE_DFLT;
1619                         MBSINIT(&mbs, MBOX_SET_TARGET_PARAMS, MBLOGALL, 0);
1620                         mbs.param[1] = (tgt << 8) | (chan << 15);
1621                         mbs.param[2] = sdf;
1622                         mbs.param[3] = 0;
1623                         isp_mboxcmd(isp, &mbs);
1624                         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1625                                 continue;
1626                         }
1627                 }
1628
1629                 /*
1630                  * We don't update any information directly from the f/w
1631                  * because we need to run at least one command to cause a
1632                  * new state to be latched up. So, we just assume that we
1633                  * converge to the values we just had set.
1634                  *
1635                  * Ensure that we don't believe tagged queuing is enabled yet.
1636                  * It turns out that sometimes the ISP just ignores our
1637                  * attempts to set parameters for devices that it hasn't
1638                  * seen yet.
1639                  */
1640                 sdp->isp_devparam[tgt].actv_flags = sdf & ~DPARM_TQING;
1641                 for (lun = 0; lun < (int) isp->isp_maxluns; lun++) {
1642                         MBSINIT(&mbs, MBOX_SET_DEV_QUEUE_PARAMS, MBLOGALL, 0);
1643                         mbs.param[1] = (chan << 15) | (tgt << 8) | lun;
1644                         mbs.param[2] = sdp->isp_max_queue_depth;
1645                         mbs.param[3] = sdp->isp_devparam[tgt].exc_throttle;
1646                         isp_mboxcmd(isp, &mbs);
1647                         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1648                                 break;
1649                         }
1650                 }
1651         }
1652         for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
1653                 if (sdp->isp_devparam[tgt].dev_refresh) {
1654                         sdp->sendmarker = 1;
1655                         sdp->update = 1;
1656                         break;
1657                 }
1658         }
1659 }
1660
1661 /*
1662  * Fibre Channel specific initialization.
1663  */
1664 static void
1665 isp_fibre_init(ispsoftc_t *isp)
1666 {
1667         fcparam *fcp;
1668         isp_icb_t local, *icbp = &local;
1669         mbreg_t mbs;
1670         int ownloopid;
1671
1672         /*
1673          * We only support one channel on non-24XX cards
1674          */
1675         fcp = FCPARAM(isp, 0);
1676         if (fcp->role == ISP_ROLE_NONE) {
1677                 isp->isp_state = ISP_INITSTATE;
1678                 return;
1679         }
1680
1681         ISP_MEMZERO(icbp, sizeof (*icbp));
1682         icbp->icb_version = ICB_VERSION1;
1683         icbp->icb_fwoptions = fcp->isp_fwoptions;
1684
1685         /*
1686          * Firmware Options are either retrieved from NVRAM or
1687          * are patched elsewhere. We check them for sanity here
1688          * and make changes based on board revision, but otherwise
1689          * let others decide policy.
1690          */
1691
1692         /*
1693          * If this is a 2100 < revision 5, we have to turn off FAIRNESS.
1694          */
1695         if (IS_2100(isp) && isp->isp_revision < 5) {
1696                 icbp->icb_fwoptions &= ~ICBOPT_FAIRNESS;
1697         }
1698
1699         /*
1700          * We have to use FULL LOGIN even though it resets the loop too much
1701          * because otherwise port database entries don't get updated after
1702          * a LIP- this is a known f/w bug for 2100 f/w less than 1.17.0.
1703          */
1704         if (!ISP_FW_NEWER_THAN(isp, 1, 17, 0)) {
1705                 icbp->icb_fwoptions |= ICBOPT_FULL_LOGIN;
1706         }
1707
1708         /*
1709          * Insist on Port Database Update Async notifications
1710          */
1711         icbp->icb_fwoptions |= ICBOPT_PDBCHANGE_AE;
1712
1713         /*
1714          * Make sure that target role reflects into fwoptions.
1715          */
1716         if (fcp->role & ISP_ROLE_TARGET) {
1717                 icbp->icb_fwoptions |= ICBOPT_TGT_ENABLE;
1718         } else {
1719                 icbp->icb_fwoptions &= ~ICBOPT_TGT_ENABLE;
1720         }
1721
1722         if (fcp->role & ISP_ROLE_INITIATOR) {
1723                 icbp->icb_fwoptions &= ~ICBOPT_INI_DISABLE;
1724         } else {
1725                 icbp->icb_fwoptions |= ICBOPT_INI_DISABLE;
1726         }
1727
1728         icbp->icb_maxfrmlen = DEFAULT_FRAMESIZE(isp);
1729         if (icbp->icb_maxfrmlen < ICB_MIN_FRMLEN || icbp->icb_maxfrmlen > ICB_MAX_FRMLEN) {
1730                 isp_prt(isp, ISP_LOGERR, "bad frame length (%d) from NVRAM- using %d", DEFAULT_FRAMESIZE(isp), ICB_DFLT_FRMLEN);
1731                 icbp->icb_maxfrmlen = ICB_DFLT_FRMLEN;
1732         }
1733         icbp->icb_maxalloc = fcp->isp_maxalloc;
1734         if (icbp->icb_maxalloc < 1) {
1735                 isp_prt(isp, ISP_LOGERR, "bad maximum allocation (%d)- using 16", fcp->isp_maxalloc);
1736                 icbp->icb_maxalloc = 16;
1737         }
1738         icbp->icb_execthrottle = DEFAULT_EXEC_THROTTLE(isp);
1739         if (icbp->icb_execthrottle < 1) {
1740                 isp_prt(isp, ISP_LOGERR, "bad execution throttle of %d- using %d", DEFAULT_EXEC_THROTTLE(isp), ICB_DFLT_THROTTLE);
1741                 icbp->icb_execthrottle = ICB_DFLT_THROTTLE;
1742         }
1743         icbp->icb_retry_delay = fcp->isp_retry_delay;
1744         icbp->icb_retry_count = fcp->isp_retry_count;
1745         icbp->icb_hardaddr = fcp->isp_loopid;
1746         ownloopid = (isp->isp_confopts & ISP_CFG_OWNLOOPID) != 0;
1747         if (icbp->icb_hardaddr >= LOCAL_LOOP_LIM) {
1748                 icbp->icb_hardaddr = 0;
1749                 ownloopid = 0;
1750         }
1751
1752         /*
1753          * Our life seems so much better with 2200s and later with
1754          * the latest f/w if we set Hard Address.
1755          */
1756         if (ownloopid || ISP_FW_NEWER_THAN(isp, 2, 2, 5)) {
1757                 icbp->icb_fwoptions |= ICBOPT_HARD_ADDRESS;
1758         }
1759
1760         /*
1761          * Right now we just set extended options to prefer point-to-point
1762          * over loop based upon some soft config options.
1763          *
1764          * NB: for the 2300, ICBOPT_EXTENDED is required.
1765          */
1766         if (IS_2100(isp)) {
1767                 /*
1768                  * We can't have Fast Posting any more- we now
1769                  * have 32 bit handles.
1770                  */
1771                 icbp->icb_fwoptions &= ~ICBOPT_FAST_POST;
1772         } else if (IS_2200(isp) || IS_23XX(isp)) {
1773                 icbp->icb_fwoptions |= ICBOPT_EXTENDED;
1774
1775                 icbp->icb_xfwoptions = fcp->isp_xfwoptions;
1776
1777                 if (ISP_CAP_FCTAPE(isp)) {
1778                         if (isp->isp_confopts & ISP_CFG_NOFCTAPE)
1779                                 icbp->icb_xfwoptions &= ~ICBXOPT_FCTAPE;
1780
1781                         if (isp->isp_confopts & ISP_CFG_FCTAPE)
1782                                 icbp->icb_xfwoptions |= ICBXOPT_FCTAPE;
1783
1784                         if (icbp->icb_xfwoptions & ICBXOPT_FCTAPE) {
1785                                 icbp->icb_fwoptions &= ~ICBOPT_FULL_LOGIN;      /* per documents */
1786                                 icbp->icb_xfwoptions |= ICBXOPT_FCTAPE_CCQ|ICBXOPT_FCTAPE_CONFIRM;
1787                                 FCPARAM(isp, 0)->fctape_enabled = 1;
1788                         } else {
1789                                 FCPARAM(isp, 0)->fctape_enabled = 0;
1790                         }
1791                 } else {
1792                         icbp->icb_xfwoptions &= ~ICBXOPT_FCTAPE;
1793                         FCPARAM(isp, 0)->fctape_enabled = 0;
1794                 }
1795
1796                 /*
1797                  * Prefer or force Point-To-Point instead Loop?
1798                  */
1799                 switch (isp->isp_confopts & ISP_CFG_PORT_PREF) {
1800                 case ISP_CFG_NPORT:
1801                         icbp->icb_xfwoptions &= ~ICBXOPT_TOPO_MASK;
1802                         icbp->icb_xfwoptions |= ICBXOPT_PTP_2_LOOP;
1803                         break;
1804                 case ISP_CFG_NPORT_ONLY:
1805                         icbp->icb_xfwoptions &= ~ICBXOPT_TOPO_MASK;
1806                         icbp->icb_xfwoptions |= ICBXOPT_PTP_ONLY;
1807                         break;
1808                 case ISP_CFG_LPORT_ONLY:
1809                         icbp->icb_xfwoptions &= ~ICBXOPT_TOPO_MASK;
1810                         icbp->icb_xfwoptions |= ICBXOPT_LOOP_ONLY;
1811                         break;
1812                 default:
1813                         /*
1814                          * Let NVRAM settings define it if they are sane
1815                          */
1816                         switch (icbp->icb_xfwoptions & ICBXOPT_TOPO_MASK) {
1817                         case ICBXOPT_PTP_2_LOOP:
1818                         case ICBXOPT_PTP_ONLY:
1819                         case ICBXOPT_LOOP_ONLY:
1820                         case ICBXOPT_LOOP_2_PTP:
1821                                 break;
1822                         default:
1823                                 icbp->icb_xfwoptions &= ~ICBXOPT_TOPO_MASK;
1824                                 icbp->icb_xfwoptions |= ICBXOPT_LOOP_2_PTP;
1825                         }
1826                         break;
1827                 }
1828                 if (IS_2200(isp)) {
1829                         /*
1830                          * We can't have Fast Posting any more- we now
1831                          * have 32 bit handles.
1832                          *
1833                          * RIO seemed to have to much breakage.
1834                          *
1835                          * Just opt for safety.
1836                          */
1837                         icbp->icb_xfwoptions &= ~ICBXOPT_RIO_16BIT;
1838                         icbp->icb_fwoptions &= ~ICBOPT_FAST_POST;
1839                 } else {
1840                         /*
1841                          * QLogic recommends that FAST Posting be turned
1842                          * off for 23XX cards and instead allow the HBA
1843                          * to write response queue entries and interrupt
1844                          * after a delay (ZIO).
1845                          */
1846                         icbp->icb_fwoptions &= ~ICBOPT_FAST_POST;
1847                         if ((fcp->isp_xfwoptions & ICBXOPT_TIMER_MASK) == ICBXOPT_ZIO) {
1848                                 icbp->icb_xfwoptions |= ICBXOPT_ZIO;
1849                                 icbp->icb_idelaytimer = 10;
1850                         }
1851                         icbp->icb_zfwoptions = fcp->isp_zfwoptions;
1852                         if (isp->isp_confopts & ISP_CFG_ONEGB) {
1853                                 icbp->icb_zfwoptions &= ~ICBZOPT_RATE_MASK;
1854                                 icbp->icb_zfwoptions |= ICBZOPT_RATE_ONEGB;
1855                         } else if (isp->isp_confopts & ISP_CFG_TWOGB) {
1856                                 icbp->icb_zfwoptions &= ~ICBZOPT_RATE_MASK;
1857                                 icbp->icb_zfwoptions |= ICBZOPT_RATE_TWOGB;
1858                         } else {
1859                                 switch (icbp->icb_zfwoptions & ICBZOPT_RATE_MASK) {
1860                                 case ICBZOPT_RATE_ONEGB:
1861                                 case ICBZOPT_RATE_TWOGB:
1862                                 case ICBZOPT_RATE_AUTO:
1863                                         break;
1864                                 default:
1865                                         icbp->icb_zfwoptions &= ~ICBZOPT_RATE_MASK;
1866                                         icbp->icb_zfwoptions |= ICBZOPT_RATE_AUTO;
1867                                         break;
1868                                 }
1869                         }
1870                 }
1871         }
1872
1873
1874         /*
1875          * For 22XX > 2.1.26 && 23XX, set some options.
1876          */
1877         if (ISP_FW_NEWER_THAN(isp, 2, 26, 0)) {
1878                 MBSINIT(&mbs, MBOX_SET_FIRMWARE_OPTIONS, MBLOGALL, 0);
1879                 mbs.param[1] = IFCOPT1_DISF7SWTCH|IFCOPT1_LIPASYNC|IFCOPT1_LIPF8;
1880                 mbs.param[2] = 0;
1881                 mbs.param[3] = 0;
1882                 if (ISP_FW_NEWER_THAN(isp, 3, 16, 0)) {
1883                         mbs.param[1] |= IFCOPT1_EQFQASYNC|IFCOPT1_CTIO_RETRY;
1884                         if (fcp->role & ISP_ROLE_TARGET) {
1885                                 if (ISP_FW_NEWER_THAN(isp, 3, 25, 0)) {
1886                                         mbs.param[1] |= IFCOPT1_ENAPURE;
1887                                 }
1888                                 mbs.param[3] = IFCOPT3_NOPRLI;
1889                         }
1890                 }
1891                 isp_mboxcmd(isp, &mbs);
1892                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1893                         return;
1894                 }
1895         }
1896         icbp->icb_logintime = ICB_LOGIN_TOV;
1897
1898 #ifdef  ISP_TARGET_MODE
1899         if (ISP_FW_NEWER_THAN(isp, 3, 25, 0) && (icbp->icb_fwoptions & ICBOPT_TGT_ENABLE)) {
1900                 icbp->icb_lunenables = 0xffff;
1901                 icbp->icb_ccnt = DFLT_CMND_CNT;
1902                 icbp->icb_icnt = DFLT_INOT_CNT;
1903                 icbp->icb_lunetimeout = ICB_LUN_ENABLE_TOV;
1904         }
1905 #endif
1906         if (fcp->isp_wwnn && fcp->isp_wwpn) {
1907                 icbp->icb_fwoptions |= ICBOPT_BOTH_WWNS;
1908                 MAKE_NODE_NAME_FROM_WWN(icbp->icb_nodename, fcp->isp_wwnn);
1909                 MAKE_NODE_NAME_FROM_WWN(icbp->icb_portname, fcp->isp_wwpn);
1910                 isp_prt(isp, ISP_LOGDEBUG1,
1911                     "Setting ICB Node 0x%08x%08x Port 0x%08x%08x",
1912                     ((uint32_t) (fcp->isp_wwnn >> 32)),
1913                     ((uint32_t) (fcp->isp_wwnn)),
1914                     ((uint32_t) (fcp->isp_wwpn >> 32)),
1915                     ((uint32_t) (fcp->isp_wwpn)));
1916         } else if (fcp->isp_wwpn) {
1917                 icbp->icb_fwoptions &= ~ICBOPT_BOTH_WWNS;
1918                 MAKE_NODE_NAME_FROM_WWN(icbp->icb_portname, fcp->isp_wwpn);
1919                 isp_prt(isp, ISP_LOGDEBUG1,
1920                     "Setting ICB Port 0x%08x%08x",
1921                     ((uint32_t) (fcp->isp_wwpn >> 32)),
1922                     ((uint32_t) (fcp->isp_wwpn)));
1923         } else {
1924                 isp_prt(isp, ISP_LOGERR, "No valid WWNs to use");
1925                 return;
1926         }
1927         icbp->icb_rqstqlen = RQUEST_QUEUE_LEN(isp);
1928         if (icbp->icb_rqstqlen < 1) {
1929                 isp_prt(isp, ISP_LOGERR, "bad request queue length");
1930         }
1931         icbp->icb_rsltqlen = RESULT_QUEUE_LEN(isp);
1932         if (icbp->icb_rsltqlen < 1) {
1933                 isp_prt(isp, ISP_LOGERR, "bad result queue length");
1934         }
1935         icbp->icb_rqstaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_rquest_dma);
1936         icbp->icb_rqstaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_rquest_dma);
1937         icbp->icb_rqstaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_rquest_dma);
1938         icbp->icb_rqstaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_rquest_dma);
1939         icbp->icb_respaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_result_dma);
1940         icbp->icb_respaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_result_dma);
1941         icbp->icb_respaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_result_dma);
1942         icbp->icb_respaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_result_dma);
1943
1944         if (FC_SCRATCH_ACQUIRE(isp, 0)) {
1945                 isp_prt(isp, ISP_LOGERR, sacq);
1946                 return;
1947         }
1948         isp_prt(isp, ISP_LOGDEBUG0, "isp_fibre_init: fwopt 0x%x xfwopt 0x%x zfwopt 0x%x",
1949             icbp->icb_fwoptions, icbp->icb_xfwoptions, icbp->icb_zfwoptions);
1950
1951         isp_put_icb(isp, icbp, (isp_icb_t *)fcp->isp_scratch);
1952
1953         /*
1954          * Init the firmware
1955          */
1956         MBSINIT(&mbs, MBOX_INIT_FIRMWARE, MBLOGALL, 30000000);
1957         mbs.param[2] = DMA_WD1(fcp->isp_scdma);
1958         mbs.param[3] = DMA_WD0(fcp->isp_scdma);
1959         mbs.param[6] = DMA_WD3(fcp->isp_scdma);
1960         mbs.param[7] = DMA_WD2(fcp->isp_scdma);
1961         mbs.logval = MBLOGALL;
1962         isp_prt(isp, ISP_LOGDEBUG0, "INIT F/W from %p (%08x%08x)",
1963             fcp->isp_scratch, (uint32_t) ((uint64_t)fcp->isp_scdma >> 32),
1964             (uint32_t) fcp->isp_scdma);
1965         MEMORYBARRIER(isp, SYNC_SFORDEV, 0, sizeof (*icbp), 0);
1966         isp_mboxcmd(isp, &mbs);
1967         FC_SCRATCH_RELEASE(isp, 0);
1968         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1969                 isp_print_bytes(isp, "isp_fibre_init", sizeof (*icbp), icbp);
1970                 return;
1971         }
1972         isp->isp_reqidx = 0;
1973         isp->isp_reqodx = 0;
1974         isp->isp_residx = 0;
1975         isp->isp_resodx = 0;
1976
1977         /*
1978          * Whatever happens, we're now committed to being here.
1979          */
1980         isp->isp_state = ISP_INITSTATE;
1981 }
1982
1983 static void
1984 isp_fibre_init_2400(ispsoftc_t *isp)
1985 {
1986         fcparam *fcp;
1987         isp_icb_2400_t local, *icbp = &local;
1988         mbreg_t mbs;
1989         int chan;
1990         int ownloopid = 0;
1991
1992         /*
1993          * Check to see whether all channels have *some* kind of role
1994          */
1995         for (chan = 0; chan < isp->isp_nchan; chan++) {
1996                 fcp = FCPARAM(isp, chan);
1997                 if (fcp->role != ISP_ROLE_NONE) {
1998                         break;
1999                 }
2000         }
2001         if (chan == isp->isp_nchan) {
2002                 isp_prt(isp, ISP_LOG_WARN1, "all %d channels with role 'none'", chan);
2003                 isp->isp_state = ISP_INITSTATE;
2004                 return;
2005         }
2006
2007         /*
2008          * Start with channel 0.
2009          */
2010         fcp = FCPARAM(isp, 0);
2011
2012         /*
2013          * Turn on LIP F8 async event (1)
2014          */
2015         MBSINIT(&mbs, MBOX_SET_FIRMWARE_OPTIONS, MBLOGALL, 0);
2016         mbs.param[1] = 1;
2017         isp_mboxcmd(isp, &mbs);
2018         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2019                 return;
2020         }
2021
2022         ISP_MEMZERO(icbp, sizeof (*icbp));
2023         icbp->icb_fwoptions1 = fcp->isp_fwoptions;
2024         if (fcp->role & ISP_ROLE_TARGET) {
2025                 icbp->icb_fwoptions1 |= ICB2400_OPT1_TGT_ENABLE;
2026         } else {
2027                 icbp->icb_fwoptions1 &= ~ICB2400_OPT1_TGT_ENABLE;
2028         }
2029
2030         if (fcp->role & ISP_ROLE_INITIATOR) {
2031                 icbp->icb_fwoptions1 &= ~ICB2400_OPT1_INI_DISABLE;
2032         } else {
2033                 icbp->icb_fwoptions1 |= ICB2400_OPT1_INI_DISABLE;
2034         }
2035
2036         icbp->icb_version = ICB_VERSION1;
2037         icbp->icb_maxfrmlen = DEFAULT_FRAMESIZE(isp);
2038         if (icbp->icb_maxfrmlen < ICB_MIN_FRMLEN || icbp->icb_maxfrmlen > ICB_MAX_FRMLEN) {
2039                 isp_prt(isp, ISP_LOGERR, "bad frame length (%d) from NVRAM- using %d", DEFAULT_FRAMESIZE(isp), ICB_DFLT_FRMLEN);
2040                 icbp->icb_maxfrmlen = ICB_DFLT_FRMLEN;
2041         }
2042
2043         icbp->icb_execthrottle = DEFAULT_EXEC_THROTTLE(isp);
2044         if (icbp->icb_execthrottle < 1) {
2045                 isp_prt(isp, ISP_LOGERR, "bad execution throttle of %d- using %d", DEFAULT_EXEC_THROTTLE(isp), ICB_DFLT_THROTTLE);
2046                 icbp->icb_execthrottle = ICB_DFLT_THROTTLE;
2047         }
2048
2049         /*
2050          * Set target exchange count. Take half if we are supporting both roles.
2051          */
2052         if (icbp->icb_fwoptions1 & ICB2400_OPT1_TGT_ENABLE) {
2053                 icbp->icb_xchgcnt = isp->isp_maxcmds;
2054                 if ((icbp->icb_fwoptions1 & ICB2400_OPT1_INI_DISABLE) == 0)
2055                         icbp->icb_xchgcnt >>= 1;
2056         }
2057
2058
2059         ownloopid = (isp->isp_confopts & ISP_CFG_OWNLOOPID) != 0;
2060         icbp->icb_hardaddr = fcp->isp_loopid;
2061         if (icbp->icb_hardaddr >= LOCAL_LOOP_LIM) {
2062                 icbp->icb_hardaddr = 0;
2063                 ownloopid = 0;
2064         }
2065
2066         if (ownloopid)
2067                 icbp->icb_fwoptions1 |= ICB2400_OPT1_HARD_ADDRESS;
2068
2069         icbp->icb_fwoptions2 = fcp->isp_xfwoptions;
2070         if (isp->isp_confopts & ISP_CFG_NOFCTAPE) {
2071                 icbp->icb_fwoptions2 &= ~ICB2400_OPT2_FCTAPE;
2072         }
2073         if (isp->isp_confopts & ISP_CFG_FCTAPE) {
2074                 icbp->icb_fwoptions2 |= ICB2400_OPT2_FCTAPE;
2075         }
2076
2077         if (icbp->icb_fwoptions2 & ICB2400_OPT2_FCTAPE) {
2078                 FCPARAM(isp, chan)->fctape_enabled = 1;
2079         } else {
2080                 FCPARAM(isp, chan)->fctape_enabled = 0;
2081         }
2082
2083         switch (isp->isp_confopts & ISP_CFG_PORT_PREF) {
2084         case ISP_CFG_NPORT_ONLY:
2085                 icbp->icb_fwoptions2 &= ~ICB2400_OPT2_TOPO_MASK;
2086                 icbp->icb_fwoptions2 |= ICB2400_OPT2_PTP_ONLY;
2087                 break;
2088         case ISP_CFG_LPORT_ONLY:
2089                 icbp->icb_fwoptions2 &= ~ICB2400_OPT2_TOPO_MASK;
2090                 icbp->icb_fwoptions2 |= ICB2400_OPT2_LOOP_ONLY;
2091                 break;
2092         default:
2093                 /* ISP_CFG_PTP_2_LOOP not available in 24XX/25XX */
2094                 icbp->icb_fwoptions2 &= ~ICB2400_OPT2_TOPO_MASK;
2095                 icbp->icb_fwoptions2 |= ICB2400_OPT2_LOOP_2_PTP;
2096                 break;
2097         }
2098
2099         switch (icbp->icb_fwoptions2 & ICB2400_OPT2_TIMER_MASK) {
2100         case ICB2400_OPT2_ZIO:
2101         case ICB2400_OPT2_ZIO1:
2102                 icbp->icb_idelaytimer = 0;
2103                 break;
2104         case 0:
2105                 break;
2106         default:
2107                 isp_prt(isp, ISP_LOGWARN, "bad value %x in fwopt2 timer field", icbp->icb_fwoptions2 & ICB2400_OPT2_TIMER_MASK);
2108                 icbp->icb_fwoptions2 &= ~ICB2400_OPT2_TIMER_MASK;
2109                 break;
2110         }
2111
2112         icbp->icb_fwoptions3 = fcp->isp_zfwoptions;
2113         if ((icbp->icb_fwoptions3 & ICB2400_OPT3_RSPSZ_MASK) == 0) {
2114                 icbp->icb_fwoptions3 |= ICB2400_OPT3_RSPSZ_24;
2115         }
2116         icbp->icb_fwoptions3 &= ~ICB2400_OPT3_RATE_AUTO;
2117         if (isp->isp_confopts & ISP_CFG_ONEGB) {
2118                 icbp->icb_fwoptions3 |= ICB2400_OPT3_RATE_ONEGB;
2119         } else if (isp->isp_confopts & ISP_CFG_TWOGB) {
2120                 icbp->icb_fwoptions3 |= ICB2400_OPT3_RATE_TWOGB;
2121         } else if (isp->isp_confopts & ISP_CFG_FOURGB) {
2122                 icbp->icb_fwoptions3 |= ICB2400_OPT3_RATE_FOURGB;
2123         } else if (IS_25XX(isp) && (isp->isp_confopts & ISP_CFG_EIGHTGB)) {
2124                 icbp->icb_fwoptions3 |= ICB2400_OPT3_RATE_EIGHTGB;
2125         } else {
2126                 icbp->icb_fwoptions3 |= ICB2400_OPT3_RATE_AUTO;
2127         }
2128         if (ownloopid == 0) {
2129                 icbp->icb_fwoptions3 |= ICB2400_OPT3_SOFTID;
2130         }
2131         icbp->icb_logintime = ICB_LOGIN_TOV;
2132
2133         if (fcp->isp_wwnn && fcp->isp_wwpn) {
2134                 icbp->icb_fwoptions1 |= ICB2400_OPT1_BOTH_WWNS;
2135                 MAKE_NODE_NAME_FROM_WWN(icbp->icb_portname, fcp->isp_wwpn);
2136                 MAKE_NODE_NAME_FROM_WWN(icbp->icb_nodename, fcp->isp_wwnn);
2137                 isp_prt(isp, ISP_LOGDEBUG1, "Setting ICB Node 0x%08x%08x Port 0x%08x%08x", ((uint32_t) (fcp->isp_wwnn >> 32)), ((uint32_t) (fcp->isp_wwnn)),
2138                     ((uint32_t) (fcp->isp_wwpn >> 32)), ((uint32_t) (fcp->isp_wwpn)));
2139         } else if (fcp->isp_wwpn) {
2140                 icbp->icb_fwoptions1 &= ~ICB2400_OPT1_BOTH_WWNS;
2141                 MAKE_NODE_NAME_FROM_WWN(icbp->icb_portname, fcp->isp_wwpn);
2142                 isp_prt(isp, ISP_LOGDEBUG1, "Setting ICB Node to be same as Port 0x%08x%08x", ((uint32_t) (fcp->isp_wwpn >> 32)), ((uint32_t) (fcp->isp_wwpn)));
2143         } else {
2144                 isp_prt(isp, ISP_LOGERR, "No valid WWNs to use");
2145                 return;
2146         }
2147         icbp->icb_retry_count = fcp->isp_retry_count;
2148
2149         icbp->icb_rqstqlen = RQUEST_QUEUE_LEN(isp);
2150         if (icbp->icb_rqstqlen < 8) {
2151                 isp_prt(isp, ISP_LOGERR, "bad request queue length %d", icbp->icb_rqstqlen);
2152                 return;
2153         }
2154         icbp->icb_rsltqlen = RESULT_QUEUE_LEN(isp);
2155         if (icbp->icb_rsltqlen < 8) {
2156                 isp_prt(isp, ISP_LOGERR, "bad result queue length %d",
2157                     icbp->icb_rsltqlen);
2158                 return;
2159         }
2160         icbp->icb_rqstaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_rquest_dma);
2161         icbp->icb_rqstaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_rquest_dma);
2162         icbp->icb_rqstaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_rquest_dma);
2163         icbp->icb_rqstaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_rquest_dma);
2164
2165         icbp->icb_respaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_result_dma);
2166         icbp->icb_respaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_result_dma);
2167         icbp->icb_respaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_result_dma);
2168         icbp->icb_respaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_result_dma);
2169
2170 #ifdef  ISP_TARGET_MODE
2171         /* unconditionally set up the ATIO queue if we support target mode */
2172         icbp->icb_atioqlen = RESULT_QUEUE_LEN(isp);
2173         if (icbp->icb_atioqlen < 8) {
2174                 isp_prt(isp, ISP_LOGERR, "bad ATIO queue length %d", icbp->icb_atioqlen);
2175                 return;
2176         }
2177         icbp->icb_atioqaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_atioq_dma);
2178         icbp->icb_atioqaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_atioq_dma);
2179         icbp->icb_atioqaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_atioq_dma);
2180         icbp->icb_atioqaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_atioq_dma);
2181         isp_prt(isp, ISP_LOGDEBUG0, "isp_fibre_init_2400: atioq %04x%04x%04x%04x", DMA_WD3(isp->isp_atioq_dma), DMA_WD2(isp->isp_atioq_dma),
2182             DMA_WD1(isp->isp_atioq_dma), DMA_WD0(isp->isp_atioq_dma));
2183 #endif
2184
2185         isp_prt(isp, ISP_LOGDEBUG0, "isp_fibre_init_2400: fwopt1 0x%x fwopt2 0x%x fwopt3 0x%x", icbp->icb_fwoptions1, icbp->icb_fwoptions2, icbp->icb_fwoptions3);
2186
2187         isp_prt(isp, ISP_LOGDEBUG0, "isp_fibre_init_2400: rqst %04x%04x%04x%04x rsp %04x%04x%04x%04x", DMA_WD3(isp->isp_rquest_dma), DMA_WD2(isp->isp_rquest_dma),
2188             DMA_WD1(isp->isp_rquest_dma), DMA_WD0(isp->isp_rquest_dma), DMA_WD3(isp->isp_result_dma), DMA_WD2(isp->isp_result_dma),
2189             DMA_WD1(isp->isp_result_dma), DMA_WD0(isp->isp_result_dma));
2190
2191         if (isp->isp_dblev & ISP_LOGDEBUG1) {
2192                 isp_print_bytes(isp, "isp_fibre_init_2400", sizeof (*icbp), icbp);
2193         }
2194
2195         if (FC_SCRATCH_ACQUIRE(isp, 0)) {
2196                 isp_prt(isp, ISP_LOGERR, sacq);
2197                 return;
2198         }
2199         ISP_MEMZERO(fcp->isp_scratch, ISP_FC_SCRLEN);
2200         isp_put_icb_2400(isp, icbp, fcp->isp_scratch);
2201
2202         /*
2203          * Now fill in information about any additional channels
2204          */
2205         if (isp->isp_nchan > 1) {
2206                 isp_icb_2400_vpinfo_t vpinfo, *vdst;
2207                 vp_port_info_t pi, *pdst;
2208                 size_t amt = 0;
2209                 uint8_t *off;
2210
2211                 vpinfo.vp_count = isp->isp_nchan - 1;
2212                 vpinfo.vp_global_options = 0;
2213                 off = fcp->isp_scratch;
2214                 off += ICB2400_VPINFO_OFF;
2215                 vdst = (isp_icb_2400_vpinfo_t *) off;
2216                 isp_put_icb_2400_vpinfo(isp, &vpinfo, vdst);
2217                 amt = ICB2400_VPINFO_OFF + sizeof (isp_icb_2400_vpinfo_t);
2218                 for (chan = 1; chan < isp->isp_nchan; chan++) {
2219                         fcparam *fcp2;
2220
2221                         ISP_MEMZERO(&pi, sizeof (pi));
2222                         fcp2 = FCPARAM(isp, chan);
2223                         if (fcp2->role != ISP_ROLE_NONE) {
2224                                 pi.vp_port_options = ICB2400_VPOPT_ENABLED;
2225                                 if (fcp2->role & ISP_ROLE_INITIATOR) {
2226                                         pi.vp_port_options |= ICB2400_VPOPT_INI_ENABLE;
2227                                 }
2228                                 if ((fcp2->role & ISP_ROLE_TARGET) == 0) {
2229                                         pi.vp_port_options |= ICB2400_VPOPT_TGT_DISABLE;
2230                                 }
2231                                 MAKE_NODE_NAME_FROM_WWN(pi.vp_port_portname, fcp2->isp_wwpn);
2232                                 MAKE_NODE_NAME_FROM_WWN(pi.vp_port_nodename, fcp2->isp_wwnn);
2233                         }
2234                         off = fcp->isp_scratch;
2235                         off += ICB2400_VPINFO_PORT_OFF(chan);
2236                         pdst = (vp_port_info_t *) off;
2237                         isp_put_vp_port_info(isp, &pi, pdst);
2238                         amt += ICB2400_VPOPT_WRITE_SIZE;
2239                 }
2240                 if (isp->isp_dblev & ISP_LOGDEBUG1) {
2241                         isp_print_bytes(isp, "isp_fibre_init_2400",
2242                             amt - ICB2400_VPINFO_OFF,
2243                             (char *)fcp->isp_scratch + ICB2400_VPINFO_OFF);
2244                 }
2245         }
2246
2247         /*
2248          * Init the firmware
2249          */
2250         MBSINIT(&mbs, 0, MBLOGALL, 30000000);
2251         if (isp->isp_nchan > 1) {
2252                 mbs.param[0] = MBOX_INIT_FIRMWARE_MULTI_ID;
2253         } else {
2254                 mbs.param[0] = MBOX_INIT_FIRMWARE;
2255         }
2256         mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2257         mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2258         mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2259         mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2260         isp_prt(isp, ISP_LOGDEBUG0, "INIT F/W from %04x%04x%04x%04x", DMA_WD3(fcp->isp_scdma), DMA_WD2(fcp->isp_scdma), DMA_WD1(fcp->isp_scdma), DMA_WD0(fcp->isp_scdma));
2261         MEMORYBARRIER(isp, SYNC_SFORDEV, 0, sizeof (*icbp), 0);
2262         isp_mboxcmd(isp, &mbs);
2263         FC_SCRATCH_RELEASE(isp, 0);
2264
2265         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2266                 return;
2267         }
2268         isp->isp_reqidx = 0;
2269         isp->isp_reqodx = 0;
2270         isp->isp_residx = 0;
2271         isp->isp_resodx = 0;
2272         isp->isp_atioodx = 0;
2273
2274         /*
2275          * Whatever happens, we're now committed to being here.
2276          */
2277         isp->isp_state = ISP_INITSTATE;
2278 }
2279
2280 static void
2281 isp_mark_portdb(ispsoftc_t *isp, int chan, int disposition)
2282 {
2283         fcparam *fcp = FCPARAM(isp, chan);
2284         fcportdb_t *lp;
2285         int i;
2286
2287         if (chan < 0 || chan >= isp->isp_nchan) {
2288                 isp_prt(isp, ISP_LOGWARN, "isp_mark_portdb: bad channel %d", chan);
2289                 return;
2290         }
2291         for (i = 0; i < MAX_FC_TARG; i++) {
2292                 lp = &fcp->portdb[i];
2293                 switch (lp->state) {
2294                 case FC_PORTDB_STATE_PROBATIONAL:
2295                 case FC_PORTDB_STATE_DEAD:
2296                 case FC_PORTDB_STATE_CHANGED:
2297                 case FC_PORTDB_STATE_PENDING_VALID:
2298                 case FC_PORTDB_STATE_VALID:
2299                         if (disposition > 0)
2300                                 lp->state = FC_PORTDB_STATE_PROBATIONAL;
2301                         else {
2302                                 lp->state = FC_PORTDB_STATE_NIL;
2303                                 isp_async(isp, ISPASYNC_DEV_GONE, chan, lp);
2304                         }
2305                         break;
2306                 case FC_PORTDB_STATE_ZOMBIE:
2307                         break;
2308                 case FC_PORTDB_STATE_NIL:
2309                 case FC_PORTDB_STATE_NEW:
2310                 default:
2311                         ISP_MEMZERO(lp, sizeof(*lp));
2312                         lp->state = FC_PORTDB_STATE_NIL;
2313                         break;
2314                 }
2315         }
2316 }
2317
2318 /*
2319  * Perform an IOCB PLOGI or LOGO via EXECUTE IOCB A64 for 24XX cards
2320  * or via FABRIC LOGIN/FABRIC LOGOUT for other cards.
2321  */
2322 static int
2323 isp_plogx(ispsoftc_t *isp, int chan, uint16_t handle, uint32_t portid, int flags, int gs)
2324 {
2325         mbreg_t mbs;
2326         uint8_t q[QENTRY_LEN];
2327         isp_plogx_t *plp;
2328         fcparam *fcp;
2329         uint8_t *scp;
2330         uint32_t sst, parm1;
2331         int rval, lev;
2332         const char *msg;
2333         char buf[64];
2334
2335         if (!IS_24XX(isp)) {
2336                 int action = flags & PLOGX_FLG_CMD_MASK;
2337                 if (action == PLOGX_FLG_CMD_PLOGI) {
2338                         return (isp_port_login(isp, handle, portid));
2339                 } else if (action == PLOGX_FLG_CMD_LOGO) {
2340                         return (isp_port_logout(isp, handle, portid));
2341                 } else {
2342                         return (MBOX_INVALID_COMMAND);
2343                 }
2344         }
2345
2346         ISP_MEMZERO(q, QENTRY_LEN);
2347         plp = (isp_plogx_t *) q;
2348         plp->plogx_header.rqs_entry_count = 1;
2349         plp->plogx_header.rqs_entry_type = RQSTYPE_LOGIN;
2350         plp->plogx_handle = 0xffffffff;
2351         plp->plogx_nphdl = handle;
2352         plp->plogx_vphdl = chan;
2353         plp->plogx_portlo = portid;
2354         plp->plogx_rspsz_porthi = (portid >> 16) & 0xff;
2355         plp->plogx_flags = flags;
2356
2357         if (isp->isp_dblev & ISP_LOGDEBUG1) {
2358                 isp_print_bytes(isp, "IOCB LOGX", QENTRY_LEN, plp);
2359         }
2360
2361         if (gs == 0) {
2362                 if (FC_SCRATCH_ACQUIRE(isp, chan)) {
2363                         isp_prt(isp, ISP_LOGERR, sacq);
2364                         return (-1);
2365                 }
2366         }
2367         fcp = FCPARAM(isp, chan);
2368         scp = fcp->isp_scratch;
2369         isp_put_plogx(isp, plp, (isp_plogx_t *) scp);
2370
2371         MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 500000);
2372         mbs.param[1] = QENTRY_LEN;
2373         mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2374         mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2375         mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2376         mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2377         MEMORYBARRIER(isp, SYNC_SFORDEV, 0, QENTRY_LEN, chan);
2378         isp_mboxcmd(isp, &mbs);
2379         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2380                 rval = mbs.param[0];
2381                 goto out;
2382         }
2383         MEMORYBARRIER(isp, SYNC_SFORCPU, QENTRY_LEN, QENTRY_LEN, chan);
2384         scp += QENTRY_LEN;
2385         isp_get_plogx(isp, (isp_plogx_t *) scp, plp);
2386         if (isp->isp_dblev & ISP_LOGDEBUG1) {
2387                 isp_print_bytes(isp, "IOCB LOGX response", QENTRY_LEN, plp);
2388         }
2389
2390         if (plp->plogx_status == PLOGX_STATUS_OK) {
2391                 rval = 0;
2392                 goto out;
2393         } else if (plp->plogx_status != PLOGX_STATUS_IOCBERR) {
2394                 isp_prt(isp, ISP_LOGWARN,
2395                     "status 0x%x on port login IOCB channel %d",
2396                     plp->plogx_status, chan);
2397                 rval = -1;
2398                 goto out;
2399         }
2400
2401         sst = plp->plogx_ioparm[0].lo16 | (plp->plogx_ioparm[0].hi16 << 16);
2402         parm1 = plp->plogx_ioparm[1].lo16 | (plp->plogx_ioparm[1].hi16 << 16);
2403
2404         rval = -1;
2405         lev = ISP_LOGERR;
2406         msg = NULL;
2407
2408         switch (sst) {
2409         case PLOGX_IOCBERR_NOLINK:
2410                 msg = "no link";
2411                 break;
2412         case PLOGX_IOCBERR_NOIOCB:
2413                 msg = "no IOCB buffer";
2414                 break;
2415         case PLOGX_IOCBERR_NOXGHG:
2416                 msg = "no Exchange Control Block";
2417                 break;
2418         case PLOGX_IOCBERR_FAILED:
2419                 ISP_SNPRINTF(buf, sizeof (buf), "reason 0x%x (last LOGIN state 0x%x)", parm1 & 0xff, (parm1 >> 8) & 0xff);
2420                 msg = buf;
2421                 break;
2422         case PLOGX_IOCBERR_NOFABRIC:
2423                 msg = "no fabric";
2424                 break;
2425         case PLOGX_IOCBERR_NOTREADY:
2426                 msg = "firmware not ready";
2427                 break;
2428         case PLOGX_IOCBERR_NOLOGIN:
2429                 ISP_SNPRINTF(buf, sizeof (buf), "not logged in (last state 0x%x)", parm1);
2430                 msg = buf;
2431                 rval = MBOX_NOT_LOGGED_IN;
2432                 break;
2433         case PLOGX_IOCBERR_REJECT:
2434                 ISP_SNPRINTF(buf, sizeof (buf), "LS_RJT = 0x%x", parm1);
2435                 msg = buf;
2436                 break;
2437         case PLOGX_IOCBERR_NOPCB:
2438                 msg = "no PCB allocated";
2439                 break;
2440         case PLOGX_IOCBERR_EINVAL:
2441                 ISP_SNPRINTF(buf, sizeof (buf), "invalid parameter at offset 0x%x", parm1);
2442                 msg = buf;
2443                 break;
2444         case PLOGX_IOCBERR_PORTUSED:
2445                 lev = ISP_LOG_SANCFG|ISP_LOG_WARN1;
2446                 ISP_SNPRINTF(buf, sizeof (buf), "already logged in with N-Port handle 0x%x", parm1);
2447                 msg = buf;
2448                 rval = MBOX_PORT_ID_USED | (parm1 << 16);
2449                 break;
2450         case PLOGX_IOCBERR_HNDLUSED:
2451                 lev = ISP_LOG_SANCFG|ISP_LOG_WARN1;
2452                 ISP_SNPRINTF(buf, sizeof (buf), "handle already used for PortID 0x%06x", parm1);
2453                 msg = buf;
2454                 rval = MBOX_LOOP_ID_USED;
2455                 break;
2456         case PLOGX_IOCBERR_NOHANDLE:
2457                 msg = "no handle allocated";
2458                 break;
2459         case PLOGX_IOCBERR_NOFLOGI:
2460                 msg = "no FLOGI_ACC";
2461                 break;
2462         default:
2463                 ISP_SNPRINTF(buf, sizeof (buf), "status %x from %x", plp->plogx_status, flags);
2464                 msg = buf;
2465                 break;
2466         }
2467         if (msg) {
2468                 isp_prt(isp, ISP_LOGERR, "Chan %d PLOGX PortID 0x%06x to N-Port handle 0x%x: %s", chan, portid, handle, msg);
2469         }
2470 out:
2471         if (gs == 0) {
2472                 FC_SCRATCH_RELEASE(isp, chan);
2473         }
2474         return (rval);
2475 }
2476
2477 static int
2478 isp_port_login(ispsoftc_t *isp, uint16_t handle, uint32_t portid)
2479 {
2480         mbreg_t mbs;
2481
2482         MBSINIT(&mbs, MBOX_FABRIC_LOGIN, MBLOGNONE, 500000);
2483         if (ISP_CAP_2KLOGIN(isp)) {
2484                 mbs.param[1] = handle;
2485                 mbs.ibits = (1 << 10);
2486         } else {
2487                 mbs.param[1] = handle << 8;
2488         }
2489         mbs.param[2] = portid >> 16;
2490         mbs.param[3] = portid;
2491         mbs.logval = MBLOGNONE;
2492         mbs.timeout = 500000;
2493         isp_mboxcmd(isp, &mbs);
2494
2495         switch (mbs.param[0]) {
2496         case MBOX_PORT_ID_USED:
2497                 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOG_WARN1, "isp_port_login: portid 0x%06x already logged in as %u", portid, mbs.param[1]);
2498                 return (MBOX_PORT_ID_USED | (mbs.param[1] << 16));
2499
2500         case MBOX_LOOP_ID_USED:
2501                 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOG_WARN1, "isp_port_login: handle 0x%x in use for port id 0x%02xXXXX", handle, mbs.param[1] & 0xff);
2502                 return (MBOX_LOOP_ID_USED);
2503
2504         case MBOX_COMMAND_COMPLETE:
2505                 return (0);
2506
2507         case MBOX_COMMAND_ERROR:
2508                 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOG_WARN1, "isp_port_login: error 0x%x in PLOGI to port 0x%06x", mbs.param[1], portid);
2509                 return (MBOX_COMMAND_ERROR);
2510
2511         case MBOX_ALL_IDS_USED:
2512                 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOG_WARN1, "isp_port_login: all IDs used for fabric login");
2513                 return (MBOX_ALL_IDS_USED);
2514
2515         default:
2516                 isp_prt(isp, ISP_LOG_SANCFG, "isp_port_login: error 0x%x on port login of 0x%06x@0x%0x", mbs.param[0], portid, handle);
2517                 return (mbs.param[0]);
2518         }
2519 }
2520
2521 /*
2522  * Pre-24XX fabric port logout
2523  *
2524  * Note that portid is not used
2525  */
2526 static int
2527 isp_port_logout(ispsoftc_t *isp, uint16_t handle, uint32_t portid)
2528 {
2529         mbreg_t mbs;
2530
2531         MBSINIT(&mbs, MBOX_FABRIC_LOGOUT, MBLOGNONE, 500000);
2532         if (ISP_CAP_2KLOGIN(isp)) {
2533                 mbs.param[1] = handle;
2534                 mbs.ibits = (1 << 10);
2535         } else {
2536                 mbs.param[1] = handle << 8;
2537         }
2538         isp_mboxcmd(isp, &mbs);
2539         return (mbs.param[0] == MBOX_COMMAND_COMPLETE? 0 : mbs.param[0]);
2540 }
2541
2542 static int
2543 isp_getpdb(ispsoftc_t *isp, int chan, uint16_t id, isp_pdb_t *pdb, int dolock)
2544 {
2545         fcparam *fcp = FCPARAM(isp, chan);
2546         mbreg_t mbs;
2547         union {
2548                 isp_pdb_21xx_t fred;
2549                 isp_pdb_24xx_t bill;
2550         } un;
2551
2552         MBSINIT(&mbs, MBOX_GET_PORT_DB, MBLOGALL & ~MBOX_COMMAND_PARAM_ERROR, 250000);
2553         if (IS_24XX(isp)) {
2554                 mbs.ibits = (1 << 9)|(1 << 10);
2555                 mbs.param[1] = id;
2556                 mbs.param[9] = chan;
2557         } else if (ISP_CAP_2KLOGIN(isp)) {
2558                 mbs.param[1] = id;
2559         } else {
2560                 mbs.param[1] = id << 8;
2561         }
2562         mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2563         mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2564         mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2565         mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2566         if (dolock) {
2567                 if (FC_SCRATCH_ACQUIRE(isp, chan)) {
2568                         isp_prt(isp, ISP_LOGERR, sacq);
2569                         return (-1);
2570                 }
2571         }
2572         MEMORYBARRIER(isp, SYNC_SFORDEV, 0, sizeof (un), chan);
2573         isp_mboxcmd(isp, &mbs);
2574         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2575                 if (dolock) {
2576                         FC_SCRATCH_RELEASE(isp, chan);
2577                 }
2578                 return (mbs.param[0]);
2579         }
2580         if (IS_24XX(isp)) {
2581                 isp_get_pdb_24xx(isp, fcp->isp_scratch, &un.bill);
2582                 pdb->handle = un.bill.pdb_handle;
2583                 pdb->prli_word3 = un.bill.pdb_prli_svc3;
2584                 pdb->portid = BITS2WORD_24XX(un.bill.pdb_portid_bits);
2585                 ISP_MEMCPY(pdb->portname, un.bill.pdb_portname, 8);
2586                 ISP_MEMCPY(pdb->nodename, un.bill.pdb_nodename, 8);
2587                 isp_prt(isp, ISP_LOG_SANCFG, "Chan %d handle 0x%x Port 0x%06x flags 0x%x curstate %x", chan, id, pdb->portid, un.bill.pdb_flags, un.bill.pdb_curstate);
2588                 if (un.bill.pdb_curstate < PDB2400_STATE_PLOGI_DONE || un.bill.pdb_curstate > PDB2400_STATE_LOGGED_IN) {
2589                         mbs.param[0] = MBOX_NOT_LOGGED_IN;
2590                         if (dolock) {
2591                                 FC_SCRATCH_RELEASE(isp, chan);
2592                         }
2593                         return (mbs.param[0]);
2594                 }
2595         } else {
2596                 isp_get_pdb_21xx(isp, fcp->isp_scratch, &un.fred);
2597                 pdb->handle = un.fred.pdb_loopid;
2598                 pdb->prli_word3 = un.fred.pdb_prli_svc3;
2599                 pdb->portid = BITS2WORD(un.fred.pdb_portid_bits);
2600                 ISP_MEMCPY(pdb->portname, un.fred.pdb_portname, 8);
2601                 ISP_MEMCPY(pdb->nodename, un.fred.pdb_nodename, 8);
2602         }
2603         if (dolock) {
2604                 FC_SCRATCH_RELEASE(isp, chan);
2605         }
2606         return (0);
2607 }
2608
2609 static void
2610 isp_dump_chip_portdb(ispsoftc_t *isp, int chan, int dolock)
2611 {
2612         isp_pdb_t pdb;
2613         int lim, loopid;
2614
2615         isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGINFO, "Chan %d chip port dump", chan);
2616         if (ISP_CAP_2KLOGIN(isp)) {
2617                 lim = NPH_MAX_2K;
2618         } else {
2619                 lim = NPH_MAX;
2620         }
2621         for (loopid = 0; loopid != lim; loopid++) {
2622                 if (isp_getpdb(isp, chan, loopid, &pdb, dolock)) {
2623                         continue;
2624                 }
2625                 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGINFO, "Chan %d Loopid 0x%04x "
2626                     "PortID 0x%06x WWPN 0x%02x%02x%02x%02x%02x%02x%02x%02x",
2627                     chan, loopid, pdb.portid, pdb.portname[0], pdb.portname[1],
2628                     pdb.portname[2], pdb.portname[3], pdb.portname[4],
2629                     pdb.portname[5], pdb.portname[6], pdb.portname[7]);
2630         }
2631 }
2632
2633 static uint64_t
2634 isp_get_wwn(ispsoftc_t *isp, int chan, int loopid, int nodename)
2635 {
2636         uint64_t wwn = INI_NONE;
2637         fcparam *fcp = FCPARAM(isp, chan);
2638         mbreg_t mbs;
2639
2640         if (fcp->isp_fwstate < FW_READY ||
2641             fcp->isp_loopstate < LOOP_PDB_RCVD) {
2642                 return (wwn);
2643         }
2644         MBSINIT(&mbs, MBOX_GET_PORT_NAME, MBLOGALL & ~MBOX_COMMAND_PARAM_ERROR, 500000);
2645         if (ISP_CAP_2KLOGIN(isp)) {
2646                 mbs.param[1] = loopid;
2647                 if (nodename) {
2648                         mbs.param[10] = 1;
2649                 }
2650                 mbs.param[9] = chan;
2651         } else {
2652                 mbs.ibitm = 3;
2653                 mbs.param[1] = loopid << 8;
2654                 if (nodename) {
2655                         mbs.param[1] |= 1;
2656                 }
2657         }
2658         isp_mboxcmd(isp, &mbs);
2659         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2660                 return (wwn);
2661         }
2662         if (IS_24XX(isp)) {
2663                 wwn =
2664                     (((uint64_t)(mbs.param[2] >> 8))    << 56) |
2665                     (((uint64_t)(mbs.param[2] & 0xff))  << 48) |
2666                     (((uint64_t)(mbs.param[3] >> 8))    << 40) |
2667                     (((uint64_t)(mbs.param[3] & 0xff))  << 32) |
2668                     (((uint64_t)(mbs.param[6] >> 8))    << 24) |
2669                     (((uint64_t)(mbs.param[6] & 0xff))  << 16) |
2670                     (((uint64_t)(mbs.param[7] >> 8))    <<  8) |
2671                     (((uint64_t)(mbs.param[7] & 0xff)));
2672         } else {
2673                 wwn =
2674                     (((uint64_t)(mbs.param[2] & 0xff))  << 56) |
2675                     (((uint64_t)(mbs.param[2] >> 8))    << 48) |
2676                     (((uint64_t)(mbs.param[3] & 0xff))  << 40) |
2677                     (((uint64_t)(mbs.param[3] >> 8))    << 32) |
2678                     (((uint64_t)(mbs.param[6] & 0xff))  << 24) |
2679                     (((uint64_t)(mbs.param[6] >> 8))    << 16) |
2680                     (((uint64_t)(mbs.param[7] & 0xff))  <<  8) |
2681                     (((uint64_t)(mbs.param[7] >> 8)));
2682         }
2683         return (wwn);
2684 }
2685
2686 /*
2687  * Make sure we have good FC link.
2688  */
2689
2690 static int
2691 isp_fclink_test(ispsoftc_t *isp, int chan, int usdelay)
2692 {
2693         mbreg_t mbs;
2694         int count, check_for_fabric, r;
2695         uint8_t lwfs;
2696         int loopid;
2697         fcparam *fcp;
2698         fcportdb_t *lp;
2699         isp_pdb_t pdb;
2700
2701         fcp = FCPARAM(isp, chan);
2702
2703         isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC Link Test Entry", chan);
2704         ISP_MARK_PORTDB(isp, chan, 1);
2705
2706         /*
2707          * Wait up to N microseconds for F/W to go to a ready state.
2708          */
2709         lwfs = FW_CONFIG_WAIT;
2710         count = 0;
2711         while (count < usdelay) {
2712                 uint64_t enano;
2713                 uint32_t wrk;
2714                 NANOTIME_T hra, hrb;
2715
2716                 GET_NANOTIME(&hra);
2717                 isp_fw_state(isp, chan);
2718                 if (lwfs != fcp->isp_fwstate) {
2719                         isp_prt(isp, ISP_LOGCONFIG|ISP_LOG_SANCFG, "Chan %d Firmware State <%s->%s>", chan, isp_fc_fw_statename((int)lwfs), isp_fc_fw_statename((int)fcp->isp_fwstate));
2720                         lwfs = fcp->isp_fwstate;
2721                 }
2722                 if (fcp->isp_fwstate == FW_READY) {
2723                         break;
2724                 }
2725                 GET_NANOTIME(&hrb);
2726
2727                 /*
2728                  * Get the elapsed time in nanoseconds.
2729                  * Always guaranteed to be non-zero.
2730                  */
2731                 enano = NANOTIME_SUB(&hrb, &hra);
2732
2733                 isp_prt(isp, ISP_LOGDEBUG1, "usec%d: 0x%lx->0x%lx enano 0x%x%08x", count, (long) GET_NANOSEC(&hra), (long) GET_NANOSEC(&hrb), (uint32_t)(enano >> 32), (uint32_t)(enano));
2734
2735                 /*
2736                  * If the elapsed time is less than 1 millisecond,
2737                  * delay a period of time up to that millisecond of
2738                  * waiting.
2739                  *
2740                  * This peculiar code is an attempt to try and avoid
2741                  * invoking uint64_t math support functions for some
2742                  * platforms where linkage is a problem.
2743                  */
2744                 if (enano < (1000 * 1000)) {
2745                         count += 1000;
2746                         enano = (1000 * 1000) - enano;
2747                         while (enano > (uint64_t) 4000000000U) {
2748                                 ISP_SLEEP(isp, 4000000);
2749                                 enano -= (uint64_t) 4000000000U;
2750                         }
2751                         wrk = enano;
2752                         wrk /= 1000;
2753                         ISP_SLEEP(isp, wrk);
2754                 } else {
2755                         while (enano > (uint64_t) 4000000000U) {
2756                                 count += 4000000;
2757                                 enano -= (uint64_t) 4000000000U;
2758                         }
2759                         wrk = enano;
2760                         count += (wrk / 1000);
2761                 }
2762         }
2763
2764
2765
2766         /*
2767          * If we haven't gone to 'ready' state, return.
2768          */
2769         if (fcp->isp_fwstate != FW_READY) {
2770                 isp_prt(isp, ISP_LOG_SANCFG, "%s: chan %d not at FW_READY state", __func__, chan);
2771                 return (-1);
2772         }
2773
2774         /*
2775          * Get our Loop ID and Port ID.
2776          */
2777         MBSINIT(&mbs, MBOX_GET_LOOP_ID, MBLOGALL, 0);
2778         mbs.param[9] = chan;
2779         isp_mboxcmd(isp, &mbs);
2780         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2781                 return (-1);
2782         }
2783
2784         if (ISP_CAP_2KLOGIN(isp)) {
2785                 fcp->isp_loopid = mbs.param[1];
2786         } else {
2787                 fcp->isp_loopid = mbs.param[1] & 0xff;
2788         }
2789
2790         if (IS_2100(isp)) {
2791                 fcp->isp_topo = TOPO_NL_PORT;
2792         } else {
2793                 int topo = (int) mbs.param[6];
2794                 if (topo < TOPO_NL_PORT || topo > TOPO_PTP_STUB) {
2795                         topo = TOPO_PTP_STUB;
2796                 }
2797                 fcp->isp_topo = topo;
2798         }
2799         fcp->isp_portid = mbs.param[2] | (mbs.param[3] << 16);
2800
2801         if (IS_2100(isp)) {
2802                 /*
2803                  * Don't bother with fabric if we are using really old
2804                  * 2100 firmware. It's just not worth it.
2805                  */
2806                 if (ISP_FW_NEWER_THAN(isp, 1, 15, 37)) {
2807                         check_for_fabric = 1;
2808                 } else {
2809                         check_for_fabric = 0;
2810                 }
2811         } else if (fcp->isp_topo == TOPO_FL_PORT || fcp->isp_topo == TOPO_F_PORT) {
2812                 check_for_fabric = 1;
2813         } else {
2814                 check_for_fabric = 0;
2815         }
2816
2817         /*
2818          * Check to make sure we got a valid loopid
2819          * The 24XX seems to mess this up for multiple channels.
2820          */
2821         if (fcp->isp_topo == TOPO_FL_PORT || fcp->isp_topo == TOPO_NL_PORT) {
2822                 uint8_t alpa = fcp->isp_portid;
2823
2824                 if (alpa == 0) {
2825                         /* "Cannot Happen" */
2826                         isp_prt(isp, ISP_LOGWARN, "Zero AL_PA for Loop Topology?");
2827                 } else {
2828                         int i;
2829                         for (i = 0; alpa_map[i]; i++) {
2830                                 if (alpa_map[i] == alpa) {
2831                                         break;
2832                                 }
2833                         }
2834                         if (alpa_map[i] && fcp->isp_loopid != i) {
2835                                 isp_prt(isp, ISP_LOG_SANCFG,
2836                                     "Chan %d deriving loopid %d from AL_PA map (AL_PA 0x%x) and ignoring returned value %d (AL_PA 0x%x)",
2837                                     chan, i, alpa_map[i], fcp->isp_loopid, alpa);
2838                                 fcp->isp_loopid = i;
2839                         }
2840                 }
2841         }
2842
2843
2844         if (IS_24XX(isp)) { /* XXX SHOULDN'T THIS BE FOR 2K F/W? XXX */
2845                 loopid = NPH_FL_ID;
2846         } else {
2847                 loopid = FL_ID;
2848         }
2849         if (check_for_fabric) {
2850                 r = isp_getpdb(isp, chan, loopid, &pdb, 1);
2851                 if (r && (fcp->isp_topo == TOPO_F_PORT || fcp->isp_topo == TOPO_FL_PORT)) {
2852                         isp_prt(isp, ISP_LOGWARN, "fabric topology but cannot get info about fabric controller (0x%x)", r);
2853                         fcp->isp_topo = TOPO_PTP_STUB;
2854                 }
2855         } else {
2856                 r = -1;
2857         }
2858         if (r == 0) {
2859                 if (IS_2100(isp)) {
2860                         fcp->isp_topo = TOPO_FL_PORT;
2861                 }
2862                 if (pdb.portid == 0) {
2863                         /*
2864                          * Crock.
2865                          */
2866                         fcp->isp_topo = TOPO_NL_PORT;
2867                         goto not_on_fabric;
2868                 }
2869
2870                 /*
2871                  * Save the Fabric controller's port database entry.
2872                  */
2873                 lp = &fcp->portdb[FL_ID];
2874                 lp->state = FC_PORTDB_STATE_PENDING_VALID;
2875                 MAKE_WWN_FROM_NODE_NAME(lp->node_wwn, pdb.nodename);
2876                 MAKE_WWN_FROM_NODE_NAME(lp->port_wwn, pdb.portname);
2877                 lp->prli_word3 = pdb.prli_word3;
2878                 lp->portid = pdb.portid;
2879                 lp->handle = pdb.handle;
2880                 lp->new_portid = lp->portid;
2881                 lp->new_prli_word3 = lp->prli_word3;
2882                 if (IS_24XX(isp)) {
2883                         if (check_for_fabric) {
2884                                 /*
2885                                  * The mbs is still hanging out from the MBOX_GET_LOOP_ID above.
2886                                  */
2887                                 fcp->isp_fabric_params = mbs.param[7];
2888                         } else {
2889                                 fcp->isp_fabric_params = 0;
2890                         }
2891                         if (chan) {
2892                                 fcp->isp_sns_hdl = NPH_SNS_HDLBASE + chan;
2893                                 r = isp_plogx(isp, chan, fcp->isp_sns_hdl, SNS_PORT_ID, PLOGX_FLG_CMD_PLOGI | PLOGX_FLG_COND_PLOGI | PLOGX_FLG_SKIP_PRLI, 0);
2894                                 if (r) {
2895                                         isp_prt(isp, ISP_LOGWARN, "%s: Chan %d cannot log into SNS", __func__, chan);
2896                                         return (-1);
2897                                 }
2898                         } else {
2899                                 fcp->isp_sns_hdl = NPH_SNS_ID;
2900                         }
2901                         r = isp_register_fc4_type_24xx(isp, chan);
2902                 } else {
2903                         fcp->isp_sns_hdl = SNS_ID;
2904                         r = isp_register_fc4_type(isp, chan);
2905                 }
2906                 if (r) {
2907                         isp_prt(isp, ISP_LOGWARN|ISP_LOG_SANCFG, "%s: register fc4 type failed", __func__);
2908                         return (-1);
2909                 }
2910         } else {
2911 not_on_fabric:
2912                 fcp->portdb[FL_ID].state = FC_PORTDB_STATE_NIL;
2913         }
2914
2915         fcp->isp_gbspeed = 1;
2916         if (IS_23XX(isp) || IS_24XX(isp)) {
2917                 MBSINIT(&mbs, MBOX_GET_SET_DATA_RATE, MBLOGALL, 3000000);
2918                 mbs.param[1] = MBGSD_GET_RATE;
2919                 /* mbs.param[2] undefined if we're just getting rate */
2920                 isp_mboxcmd(isp, &mbs);
2921                 if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
2922                         if (mbs.param[1] == MBGSD_EIGHTGB) {
2923                                 isp_prt(isp, ISP_LOGINFO, "Chan %d 8Gb link speed", chan);
2924                                 fcp->isp_gbspeed = 8;
2925                         } else if (mbs.param[1] == MBGSD_FOURGB) {
2926                                 isp_prt(isp, ISP_LOGINFO, "Chan %d 4Gb link speed", chan);
2927                                 fcp->isp_gbspeed = 4;
2928                         } else if (mbs.param[1] == MBGSD_TWOGB) {
2929                                 isp_prt(isp, ISP_LOGINFO, "Chan %d 2Gb link speed", chan);
2930                                 fcp->isp_gbspeed = 2;
2931                         } else if (mbs.param[1] == MBGSD_ONEGB) {
2932                                 isp_prt(isp, ISP_LOGINFO, "Chan %d 1Gb link speed", chan);
2933                                 fcp->isp_gbspeed = 1;
2934                         }
2935                 }
2936         }
2937
2938         /*
2939          * Announce ourselves, too.
2940          */
2941         isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGCONFIG, topology, chan, (uint32_t) (fcp->isp_wwpn >> 32), (uint32_t) fcp->isp_wwpn, fcp->isp_portid, fcp->isp_loopid, isp_fc_toponame(fcp));
2942         isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC Link Test Complete", chan);
2943         return (0);
2944 }
2945
2946 /*
2947  * Complete the synchronization of our Port Database.
2948  *
2949  * At this point, we've scanned the local loop (if any) and the fabric
2950  * and performed fabric logins on all new devices.
2951  *
2952  * Our task here is to go through our port database and remove any entities
2953  * that are still marked probational (issuing PLOGO for ones which we had
2954  * PLOGI'd into) or are dead.
2955  *
2956  * Our task here is to also check policy to decide whether devices which
2957  * have *changed* in some way should still be kept active. For example,
2958  * if a device has just changed PortID, we can either elect to treat it
2959  * as an old device or as a newly arrived device (and notify the outer
2960  * layer appropriately).
2961  *
2962  * We also do initiator map target id assignment here for new initiator
2963  * devices and refresh old ones ot make sure that they point to the correct
2964  * entities.
2965  */
2966 static int
2967 isp_pdb_sync(ispsoftc_t *isp, int chan)
2968 {
2969         fcparam *fcp = FCPARAM(isp, chan);
2970         fcportdb_t *lp;
2971         uint16_t dbidx;
2972
2973         if (fcp->isp_loopstate == LOOP_READY) {
2974                 return (0);
2975         }
2976
2977         /*
2978          * Make sure we're okay for doing this right now.
2979          */
2980         if (fcp->isp_loopstate != LOOP_PDB_RCVD &&
2981             fcp->isp_loopstate != LOOP_FSCAN_DONE &&
2982             fcp->isp_loopstate != LOOP_LSCAN_DONE) {
2983                 isp_prt(isp, ISP_LOGWARN, "isp_pdb_sync: bad loopstate %d",
2984                     fcp->isp_loopstate);
2985                 return (-1);
2986         }
2987
2988         if (fcp->isp_topo == TOPO_FL_PORT ||
2989             fcp->isp_topo == TOPO_NL_PORT ||
2990             fcp->isp_topo == TOPO_N_PORT) {
2991                 if (fcp->isp_loopstate < LOOP_LSCAN_DONE) {
2992                         if (isp_scan_loop(isp, chan) != 0) {
2993                                 isp_prt(isp, ISP_LOGWARN,
2994                                     "isp_pdb_sync: isp_scan_loop failed");
2995                                 return (-1);
2996                         }
2997                 }
2998         }
2999
3000         if (fcp->isp_topo == TOPO_F_PORT || fcp->isp_topo == TOPO_FL_PORT) {
3001                 if (fcp->isp_loopstate < LOOP_FSCAN_DONE) {
3002                         if (isp_scan_fabric(isp, chan) != 0) {
3003                                 isp_prt(isp, ISP_LOGWARN,
3004                                     "isp_pdb_sync: isp_scan_fabric failed");
3005                                 return (-1);
3006                         }
3007                 }
3008         }
3009
3010         isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Synchronizing PDBs", chan);
3011
3012         fcp->isp_loopstate = LOOP_SYNCING_PDB;
3013
3014         for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
3015                 lp = &fcp->portdb[dbidx];
3016
3017                 if (lp->state == FC_PORTDB_STATE_NIL ||
3018                     lp->state == FC_PORTDB_STATE_VALID) {
3019                         continue;
3020                 }
3021
3022                 switch (lp->state) {
3023                 case FC_PORTDB_STATE_PROBATIONAL:
3024                 case FC_PORTDB_STATE_DEAD:
3025                         lp->state = FC_PORTDB_STATE_NIL;
3026                         isp_async(isp, ISPASYNC_DEV_GONE, chan, lp);
3027                         if (lp->autologin == 0) {
3028                                 (void) isp_plogx(isp, chan, lp->handle,
3029                                     lp->portid,
3030                                     PLOGX_FLG_CMD_LOGO |
3031                                     PLOGX_FLG_IMPLICIT |
3032                                     PLOGX_FLG_FREE_NPHDL, 0);
3033                         } else {
3034                                 lp->autologin = 0;
3035                         }
3036                         lp->new_prli_word3 = 0;
3037                         lp->new_portid = 0;
3038                         /*
3039                          * Note that we might come out of this with our state
3040                          * set to FC_PORTDB_STATE_ZOMBIE.
3041                          */
3042                         break;
3043                 case FC_PORTDB_STATE_NEW:
3044                         lp->portid = lp->new_portid;
3045                         lp->prli_word3 = lp->new_prli_word3;
3046                         lp->state = FC_PORTDB_STATE_VALID;
3047                         isp_async(isp, ISPASYNC_DEV_ARRIVED, chan, lp);
3048                         lp->new_prli_word3 = 0;
3049                         lp->new_portid = 0;
3050                         break;
3051                 case FC_PORTDB_STATE_CHANGED:
3052                         lp->state = FC_PORTDB_STATE_VALID;
3053                         isp_async(isp, ISPASYNC_DEV_CHANGED, chan, lp);
3054                         lp->portid = lp->new_portid;
3055                         lp->prli_word3 = lp->new_prli_word3;
3056                         lp->new_prli_word3 = 0;
3057                         lp->new_portid = 0;
3058                         break;
3059                 case FC_PORTDB_STATE_PENDING_VALID:
3060                         lp->portid = lp->new_portid;
3061                         lp->prli_word3 = lp->new_prli_word3;
3062                         lp->state = FC_PORTDB_STATE_VALID;
3063                         isp_async(isp, ISPASYNC_DEV_STAYED, chan, lp);
3064                         if (dbidx != FL_ID) {
3065                                 lp->new_prli_word3 = 0;
3066                                 lp->new_portid = 0;
3067                         }
3068                         break;
3069                 case FC_PORTDB_STATE_ZOMBIE:
3070                         break;
3071                 default:
3072                         isp_prt(isp, ISP_LOGWARN,
3073                             "isp_pdb_sync: state %d for idx %d",
3074                             lp->state, dbidx);
3075                         isp_dump_portdb(isp, chan);
3076                 }
3077         }
3078
3079         /*
3080          * If we get here, we've for sure seen not only a valid loop
3081          * but know what is or isn't on it, so mark this for usage
3082          * in isp_start.
3083          */
3084         fcp->loop_seen_once = 1;
3085         fcp->isp_loopstate = LOOP_READY;
3086         return (0);
3087 }
3088
3089 /*
3090  * Scan local loop for devices.
3091  */
3092 static int
3093 isp_scan_loop(ispsoftc_t *isp, int chan)
3094 {
3095         fcportdb_t *lp, tmp;
3096         fcparam *fcp = FCPARAM(isp, chan);
3097         int i;
3098         isp_pdb_t pdb;
3099         uint16_t handle, lim = 0;
3100
3101         if (fcp->isp_fwstate < FW_READY ||
3102             fcp->isp_loopstate < LOOP_PDB_RCVD) {
3103                 return (-1);
3104         }
3105
3106         if (fcp->isp_loopstate > LOOP_SCANNING_LOOP) {
3107                 return (0);
3108         }
3109
3110         /*
3111          * Check our connection topology.
3112          *
3113          * If we're a public or private loop, we scan 0..125 as handle values.
3114          * The firmware has (typically) peformed a PLOGI for us. We skip this
3115          * step if we're a ISP_24XX in NP-IV mode.
3116          *
3117          * If we're a N-port connection, we treat this is a short loop (0..1).
3118          */
3119         switch (fcp->isp_topo) {
3120         case TOPO_NL_PORT:
3121                 lim = LOCAL_LOOP_LIM;
3122                 break;
3123         case TOPO_FL_PORT:
3124                 if (IS_24XX(isp) && isp->isp_nchan > 1) {
3125                         isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Skipping Local Loop Scan", chan);
3126                         fcp->isp_loopstate = LOOP_LSCAN_DONE;
3127                         return (0);
3128                 }
3129                 lim = LOCAL_LOOP_LIM;
3130                 break;
3131         case TOPO_N_PORT:
3132                 lim = 2;
3133                 break;
3134         default:
3135                 isp_prt(isp, ISP_LOG_SANCFG, "Chan %d no loop topology to scan", chan);
3136                 fcp->isp_loopstate = LOOP_LSCAN_DONE;
3137                 return (0);
3138         }
3139
3140         fcp->isp_loopstate = LOOP_SCANNING_LOOP;
3141
3142         isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC scan loop 0..%d", chan, lim-1);
3143
3144         /*
3145          * Run through the list and get the port database info for each one.
3146          */
3147         for (handle = 0; handle < lim; handle++) {
3148                 int r;
3149                 /*
3150                  * Don't scan "special" ids.
3151                  */
3152                 if (handle >= FL_ID && handle <= SNS_ID) {
3153                         continue;
3154                 }
3155                 if (ISP_CAP_2KLOGIN(isp)) {
3156                         if (handle >= NPH_RESERVED && handle <= NPH_FL_ID) {
3157                                 continue;
3158                         }
3159                 }
3160                 /*
3161                  * In older cards with older f/w GET_PORT_DATABASE has been
3162                  * known to hang. This trick gets around that problem.
3163                  */
3164                 if (IS_2100(isp) || IS_2200(isp)) {
3165                         uint64_t node_wwn = isp_get_wwn(isp, chan, handle, 1);
3166                         if (fcp->isp_loopstate < LOOP_SCANNING_LOOP) {
3167                                 isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC scan loop DONE (bad)", chan);
3168                                 return (-1);
3169                         }
3170                         if (node_wwn == INI_NONE) {
3171                                 continue;
3172                         }
3173                 }
3174
3175                 /*
3176                  * Get the port database entity for this index.
3177                  */
3178                 r = isp_getpdb(isp, chan, handle, &pdb, 1);
3179                 if (r != 0) {
3180                         isp_prt(isp, ISP_LOGDEBUG1,
3181                             "Chan %d FC scan loop handle %d returned %x",
3182                             chan, handle, r);
3183                         if (fcp->isp_loopstate < LOOP_SCANNING_LOOP) {
3184                                 ISP_MARK_PORTDB(isp, chan, 1);
3185                                 isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC scan loop DONE (bad)", chan);
3186                                 return (-1);
3187                         }
3188                         continue;
3189                 }
3190
3191                 if (fcp->isp_loopstate < LOOP_SCANNING_LOOP) {
3192                         ISP_MARK_PORTDB(isp, chan, 1);
3193                         isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC scan loop DONE (bad)", chan);
3194                         return (-1);
3195                 }
3196
3197                 /*
3198                  * On *very* old 2100 firmware we would end up sometimes
3199                  * with the firmware returning the port database entry
3200                  * for something else. We used to restart this, but
3201                  * now we just punt.
3202                  */
3203                 if (IS_2100(isp) && pdb.handle != handle) {
3204                         isp_prt(isp, ISP_LOGWARN,
3205                             "Chan %d cannot synchronize port database", chan);
3206                         ISP_MARK_PORTDB(isp, chan, 1);
3207                         isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC scan loop DONE (bad)", chan);
3208                         return (-1);
3209                 }
3210
3211                 /*
3212                  * Save the pertinent info locally.
3213                  */
3214                 MAKE_WWN_FROM_NODE_NAME(tmp.node_wwn, pdb.nodename);
3215                 MAKE_WWN_FROM_NODE_NAME(tmp.port_wwn, pdb.portname);
3216                 tmp.prli_word3 = pdb.prli_word3;
3217                 tmp.portid = pdb.portid;
3218                 tmp.handle = pdb.handle;
3219
3220                 /*
3221                  * Check to make sure it's still a valid entry. The 24XX seems
3222                  * to return a portid but not a WWPN/WWNN or role for devices
3223                  * which shift on a loop.
3224                  */
3225                 if (tmp.node_wwn == 0 || tmp.port_wwn == 0 || tmp.portid == 0) {
3226                         int a, b, c;
3227                         isp_prt(isp, ISP_LOGWARN,
3228                             "Chan %d bad pdb (WWNN %016jx, WWPN %016jx, PortID %06x, W3 0x%x, H 0x%x) @ handle 0x%x",
3229                             chan, tmp.node_wwn, tmp.port_wwn, tmp.portid, tmp.prli_word3, tmp.handle, handle);
3230                         a = (tmp.node_wwn == 0);
3231                         b = (tmp.port_wwn == 0);
3232                         c = (tmp.portid == 0);
3233                         if (a == 0 && b == 0) {
3234                                 tmp.node_wwn =
3235                                     isp_get_wwn(isp, chan, handle, 1);
3236                                 tmp.port_wwn =
3237                                     isp_get_wwn(isp, chan, handle, 0);
3238                                 if (tmp.node_wwn && tmp.port_wwn) {
3239                                         isp_prt(isp, ISP_LOGWARN, "DODGED!");
3240                                         goto cont;
3241                                 }
3242                         }
3243                         isp_dump_portdb(isp, chan);
3244                         continue;
3245                 }
3246   cont:
3247
3248                 /*
3249                  * Now search the entire port database
3250                  * for the same Port WWN.
3251                  */
3252                 if (isp_find_pdb_by_wwn(isp, chan, tmp.port_wwn, &lp)) {
3253                         /*
3254                          * Okay- we've found a non-nil entry that matches.
3255                          * Check to make sure it's probational or a zombie.
3256                          */
3257                         if (lp->state != FC_PORTDB_STATE_PROBATIONAL &&
3258                             lp->state != FC_PORTDB_STATE_ZOMBIE &&
3259                             lp->state != FC_PORTDB_STATE_VALID) {
3260                                 isp_prt(isp, ISP_LOGERR,
3261                                     "Chan %d [%d] not probational/zombie (0x%x)",
3262                                     chan, FC_PORTDB_TGT(isp, chan, lp), lp->state);
3263                                 isp_dump_portdb(isp, chan);
3264                                 ISP_MARK_PORTDB(isp, chan, 1);
3265                                 isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC scan loop DONE (bad)", chan);
3266                                 return (-1);
3267                         }
3268
3269                         /*
3270                          * Mark the device as something the f/w logs into
3271                          * automatically.
3272                          */
3273                         lp->autologin = 1;
3274                         lp->node_wwn = tmp.node_wwn;
3275
3276                         /*
3277                          * Check to make see if really still the same
3278                          * device. If it is, we mark it pending valid.
3279                          */
3280                         if (lp->portid == tmp.portid && lp->handle == tmp.handle && lp->prli_word3 == tmp.prli_word3) {
3281                                 lp->new_portid = tmp.portid;
3282                                 lp->new_prli_word3 = tmp.prli_word3;
3283                                 lp->state = FC_PORTDB_STATE_PENDING_VALID;
3284                                 isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Loop Port 0x%06x@0x%04x Pending Valid", chan, tmp.portid, tmp.handle);
3285                                 continue;
3286                         }
3287
3288                         /*
3289                          * We can wipe out the old handle value
3290                          * here because it's no longer valid.
3291                          */
3292                         lp->handle = tmp.handle;
3293
3294                         /*
3295                          * Claim that this has changed and let somebody else
3296                          * decide what to do.
3297                          */
3298                         isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Loop Port 0x%06x@0x%04x changed", chan, tmp.portid, tmp.handle);
3299                         lp->state = FC_PORTDB_STATE_CHANGED;
3300                         lp->new_portid = tmp.portid;
3301                         lp->new_prli_word3 = tmp.prli_word3;
3302                         continue;
3303                 }
3304
3305                 /*
3306                  * Ah. A new device entry. Find an empty slot
3307                  * for it and save info for later disposition.
3308                  */
3309                 for (i = 0; i < MAX_FC_TARG; i++) {
3310                         if (fcp->portdb[i].state == FC_PORTDB_STATE_NIL) {
3311                                 break;
3312                         }
3313                 }
3314                 if (i == MAX_FC_TARG) {
3315                         isp_prt(isp, ISP_LOGERR,
3316                             "Chan %d out of portdb entries", chan);
3317                         continue;
3318                 }
3319                 lp = &fcp->portdb[i];
3320
3321                 ISP_MEMZERO(lp, sizeof (fcportdb_t));
3322                 lp->autologin = 1;
3323                 lp->state = FC_PORTDB_STATE_NEW;
3324                 lp->new_portid = tmp.portid;
3325                 lp->new_prli_word3 = tmp.prli_word3;
3326                 lp->handle = tmp.handle;
3327                 lp->port_wwn = tmp.port_wwn;
3328                 lp->node_wwn = tmp.node_wwn;
3329                 isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Loop Port 0x%06x@0x%04x is New Entry", chan, tmp.portid, tmp.handle);
3330         }
3331         fcp->isp_loopstate = LOOP_LSCAN_DONE;
3332         isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC scan loop DONE", chan);
3333         return (0);
3334 }
3335
3336 /*
3337  * Scan the fabric for devices and add them to our port database.
3338  *
3339  * Use the GID_FT command to get all Port IDs for FC4 SCSI devices it knows.
3340  *
3341  * For 2100-23XX cards, we can use the SNS mailbox command to pass simple
3342  * name server commands to the switch management server via the QLogic f/w.
3343  *
3344  * For the 24XX card, we have to use CT-Pass through run via the Execute IOCB
3345  * mailbox command.
3346  *
3347  * The net result is to leave the list of Port IDs setting untranslated in
3348  * offset IGPOFF of the FC scratch area, whereupon we'll canonicalize it to
3349  * host order at OGPOFF.
3350  */
3351
3352 /*
3353  * Take less than half of our scratch area to store Port IDs
3354  */
3355 #define GIDLEN  ((ISP_FC_SCRLEN >> 1) - 16 - SNS_GID_FT_REQ_SIZE)
3356 #define NGENT   ((GIDLEN - 16) >> 2)
3357
3358 #define IGPOFF  (2 * QENTRY_LEN)
3359 #define OGPOFF  (ISP_FC_SCRLEN >> 1)
3360 #define ZTXOFF  (ISP_FC_SCRLEN - (1 * QENTRY_LEN))
3361 #define CTXOFF  (ISP_FC_SCRLEN - (2 * QENTRY_LEN))
3362 #define XTXOFF  (ISP_FC_SCRLEN - (3 * QENTRY_LEN))
3363
3364 static int
3365 isp_gid_ft_sns(ispsoftc_t *isp, int chan)
3366 {
3367         union {
3368                 sns_gid_ft_req_t _x;
3369                 uint8_t _y[SNS_GID_FT_REQ_SIZE];
3370         } un;
3371         fcparam *fcp = FCPARAM(isp, chan);
3372         sns_gid_ft_req_t *rq = &un._x;
3373         mbreg_t mbs;
3374
3375         isp_prt(isp, ISP_LOGDEBUG0, "Chan %d scanning fabric (GID_FT) via SNS", chan);
3376
3377         ISP_MEMZERO(rq, SNS_GID_FT_REQ_SIZE);
3378         rq->snscb_rblen = GIDLEN >> 1;
3379         rq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma + IGPOFF);
3380         rq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma + IGPOFF);
3381         rq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma + IGPOFF);
3382         rq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma + IGPOFF);
3383         rq->snscb_sblen = 6;
3384         rq->snscb_cmd = SNS_GID_FT;
3385         rq->snscb_mword_div_2 = NGENT;
3386         rq->snscb_fc4_type = FC4_SCSI;
3387
3388         isp_put_gid_ft_request(isp, rq, fcp->isp_scratch);
3389         MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GID_FT_REQ_SIZE, chan);
3390
3391         MBSINIT(&mbs, MBOX_SEND_SNS, MBLOGALL, 10000000);
3392         mbs.param[0] = MBOX_SEND_SNS;
3393         mbs.param[1] = SNS_GID_FT_REQ_SIZE >> 1;
3394         mbs.param[2] = DMA_WD1(fcp->isp_scdma);
3395         mbs.param[3] = DMA_WD0(fcp->isp_scdma);
3396         mbs.param[6] = DMA_WD3(fcp->isp_scdma);
3397         mbs.param[7] = DMA_WD2(fcp->isp_scdma);
3398         isp_mboxcmd(isp, &mbs);
3399         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3400                 if (mbs.param[0] == MBOX_INVALID_COMMAND) {
3401                         return (1);
3402                 } else {
3403                         return (-1);
3404                 }
3405         }
3406         return (0);
3407 }
3408
3409 static int
3410 isp_gid_ft_ct_passthru(ispsoftc_t *isp, int chan)
3411 {
3412         mbreg_t mbs;
3413         fcparam *fcp = FCPARAM(isp, chan);
3414         union {
3415                 isp_ct_pt_t plocal;
3416                 ct_hdr_t clocal;
3417                 uint8_t q[QENTRY_LEN];
3418         } un;
3419         isp_ct_pt_t *pt;
3420         ct_hdr_t *ct;
3421         uint32_t *rp;
3422         uint8_t *scp = fcp->isp_scratch;
3423
3424         isp_prt(isp, ISP_LOGDEBUG0, "Chan %d scanning fabric (GID_FT) via CT", chan);
3425
3426         if (!IS_24XX(isp)) {
3427                 return (1);
3428         }
3429
3430         /*
3431          * Build a Passthrough IOCB in memory.
3432          */
3433         pt = &un.plocal;
3434         ISP_MEMZERO(un.q, QENTRY_LEN);
3435         pt->ctp_header.rqs_entry_count = 1;
3436         pt->ctp_header.rqs_entry_type = RQSTYPE_CT_PASSTHRU;
3437         pt->ctp_handle = 0xffffffff;
3438         pt->ctp_nphdl = fcp->isp_sns_hdl;
3439         pt->ctp_cmd_cnt = 1;
3440         pt->ctp_vpidx = ISP_GET_VPIDX(isp, chan);
3441         pt->ctp_time = 30;
3442         pt->ctp_rsp_cnt = 1;
3443         pt->ctp_rsp_bcnt = GIDLEN;
3444         pt->ctp_cmd_bcnt = sizeof (*ct) + sizeof (uint32_t);
3445         pt->ctp_dataseg[0].ds_base = DMA_LO32(fcp->isp_scdma+XTXOFF);
3446         pt->ctp_dataseg[0].ds_basehi = DMA_HI32(fcp->isp_scdma+XTXOFF);
3447         pt->ctp_dataseg[0].ds_count = sizeof (*ct) + sizeof (uint32_t);
3448         pt->ctp_dataseg[1].ds_base = DMA_LO32(fcp->isp_scdma+IGPOFF);
3449         pt->ctp_dataseg[1].ds_basehi = DMA_HI32(fcp->isp_scdma+IGPOFF);
3450         pt->ctp_dataseg[1].ds_count = GIDLEN;
3451         if (isp->isp_dblev & ISP_LOGDEBUG1) {
3452                 isp_print_bytes(isp, "ct IOCB", QENTRY_LEN, pt);
3453         }
3454         isp_put_ct_pt(isp, pt, (isp_ct_pt_t *) &scp[CTXOFF]);
3455
3456         /*
3457          * Build the CT header and command in memory.
3458          *
3459          * Note that the CT header has to end up as Big Endian format in memory.
3460          */
3461         ct = &un.clocal;
3462         ISP_MEMZERO(ct, sizeof (*ct));
3463         ct->ct_revision = CT_REVISION;
3464         ct->ct_fcs_type = CT_FC_TYPE_FC;
3465         ct->ct_fcs_subtype = CT_FC_SUBTYPE_NS;
3466         ct->ct_cmd_resp = SNS_GID_FT;
3467         ct->ct_bcnt_resid = (GIDLEN - 16) >> 2;
3468
3469         isp_put_ct_hdr(isp, ct, (ct_hdr_t *) &scp[XTXOFF]);
3470         rp = (uint32_t *) &scp[XTXOFF+sizeof (*ct)];
3471         ISP_IOZPUT_32(isp, FC4_SCSI, rp);
3472         if (isp->isp_dblev & ISP_LOGDEBUG1) {
3473                 isp_print_bytes(isp, "CT HDR + payload after put",
3474                     sizeof (*ct) + sizeof (uint32_t), &scp[XTXOFF]);
3475         }
3476         ISP_MEMZERO(&scp[ZTXOFF], QENTRY_LEN);
3477         MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 500000);
3478         mbs.param[1] = QENTRY_LEN;
3479         mbs.param[2] = DMA_WD1(fcp->isp_scdma + CTXOFF);
3480         mbs.param[3] = DMA_WD0(fcp->isp_scdma + CTXOFF);
3481         mbs.param[6] = DMA_WD3(fcp->isp_scdma + CTXOFF);
3482         mbs.param[7] = DMA_WD2(fcp->isp_scdma + CTXOFF);
3483         MEMORYBARRIER(isp, SYNC_SFORDEV, XTXOFF, 2 * QENTRY_LEN, chan);
3484         isp_mboxcmd(isp, &mbs);
3485         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3486                 return (-1);
3487         }
3488         MEMORYBARRIER(isp, SYNC_SFORCPU, ZTXOFF, QENTRY_LEN, chan);
3489         pt = &un.plocal;
3490         isp_get_ct_pt(isp, (isp_ct_pt_t *) &scp[ZTXOFF], pt);
3491         if (isp->isp_dblev & ISP_LOGDEBUG1) {
3492                 isp_print_bytes(isp, "IOCB response", QENTRY_LEN, pt);
3493         }
3494
3495         if (pt->ctp_status && pt->ctp_status != RQCS_DATA_UNDERRUN) {
3496                 isp_prt(isp, ISP_LOGWARN,
3497                     "Chan %d ISP GID FT CT Passthrough returned 0x%x",
3498                     chan, pt->ctp_status);
3499                 return (-1);
3500         }
3501         MEMORYBARRIER(isp, SYNC_SFORCPU, IGPOFF, GIDLEN + 16, chan);
3502         if (isp->isp_dblev & ISP_LOGDEBUG1) {
3503                 isp_print_bytes(isp, "CT response", GIDLEN+16, &scp[IGPOFF]);
3504         }
3505         return (0);
3506 }
3507
3508 static int
3509 isp_scan_fabric(ispsoftc_t *isp, int chan)
3510 {
3511         fcparam *fcp = FCPARAM(isp, chan);
3512         uint32_t portid;
3513         uint16_t handle, oldhandle, loopid;
3514         isp_pdb_t pdb;
3515         int portidx, portlim, r;
3516         sns_gid_ft_rsp_t *rs0, *rs1;
3517
3518         isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC Scan Fabric", chan);
3519         if (fcp->isp_fwstate != FW_READY || fcp->isp_loopstate < LOOP_LSCAN_DONE) {
3520                 return (-1);
3521         }
3522         if (fcp->isp_loopstate > LOOP_SCANNING_FABRIC) {
3523                 return (0);
3524         }
3525         if (fcp->isp_topo != TOPO_FL_PORT && fcp->isp_topo != TOPO_F_PORT) {
3526                 fcp->isp_loopstate = LOOP_FSCAN_DONE;
3527                 isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC Scan Fabric Done (no fabric)", chan);
3528                 return (0);
3529         }
3530
3531         fcp->isp_loopstate = LOOP_SCANNING_FABRIC;
3532         if (FC_SCRATCH_ACQUIRE(isp, chan)) {
3533                 isp_prt(isp, ISP_LOGERR, sacq);
3534                 ISP_MARK_PORTDB(isp, chan, 1);
3535                 return (-1);
3536         }
3537         if (fcp->isp_loopstate < LOOP_SCANNING_FABRIC) {
3538                 FC_SCRATCH_RELEASE(isp, chan);
3539                 ISP_MARK_PORTDB(isp, chan, 1);
3540                 return (-1);
3541         }
3542
3543         /*
3544          * Make sure we still are logged into the fabric controller.
3545          */
3546         if (IS_24XX(isp)) {     /* XXX SHOULDN'T THIS BE TRUE FOR 2K F/W? XXX */
3547                 loopid = NPH_FL_ID;
3548         } else {
3549                 loopid = FL_ID;
3550         }
3551         r = isp_getpdb(isp, chan, loopid, &pdb, 0);
3552         if (r == MBOX_NOT_LOGGED_IN) {
3553                 isp_dump_chip_portdb(isp, chan, 0);
3554         }
3555         if (r) {
3556                 fcp->isp_loopstate = LOOP_PDB_RCVD;
3557                 FC_SCRATCH_RELEASE(isp, chan);
3558                 ISP_MARK_PORTDB(isp, chan, 1);
3559                 return (-1);
3560         }
3561
3562         if (IS_24XX(isp)) {
3563                 r = isp_gid_ft_ct_passthru(isp, chan);
3564         } else {
3565                 r = isp_gid_ft_sns(isp, chan);
3566         }
3567
3568         if (fcp->isp_loopstate < LOOP_SCANNING_FABRIC) {
3569                 FC_SCRATCH_RELEASE(isp, chan);
3570                 ISP_MARK_PORTDB(isp, chan, 1);
3571                 return (-1);
3572         }
3573
3574         if (r > 0) {
3575                 fcp->isp_loopstate = LOOP_FSCAN_DONE;
3576                 FC_SCRATCH_RELEASE(isp, chan);
3577                 return (0);
3578         } else if (r < 0) {
3579                 fcp->isp_loopstate = LOOP_PDB_RCVD;     /* try again */
3580                 FC_SCRATCH_RELEASE(isp, chan);
3581                 return (0);
3582         }
3583
3584         MEMORYBARRIER(isp, SYNC_SFORCPU, IGPOFF, GIDLEN, chan);
3585         rs0 = (sns_gid_ft_rsp_t *) ((uint8_t *)fcp->isp_scratch+IGPOFF);
3586         rs1 = (sns_gid_ft_rsp_t *) ((uint8_t *)fcp->isp_scratch+OGPOFF);
3587         isp_get_gid_ft_response(isp, rs0, rs1, NGENT);
3588         if (fcp->isp_loopstate < LOOP_SCANNING_FABRIC) {
3589                 FC_SCRATCH_RELEASE(isp, chan);
3590                 ISP_MARK_PORTDB(isp, chan, 1);
3591                 return (-1);
3592         }
3593         if (rs1->snscb_cthdr.ct_cmd_resp != LS_ACC) {
3594                 int level;
3595                 if (rs1->snscb_cthdr.ct_reason == 9 && rs1->snscb_cthdr.ct_explanation == 7) {
3596                         level = ISP_LOG_SANCFG;
3597                 } else {
3598                         level = ISP_LOGWARN;
3599                 }
3600                 isp_prt(isp, level, "Chan %d Fabric Nameserver rejected GID_FT"
3601                     " (Reason=0x%x Expl=0x%x)", chan,
3602                     rs1->snscb_cthdr.ct_reason,
3603                     rs1->snscb_cthdr.ct_explanation);
3604                 FC_SCRATCH_RELEASE(isp, chan);
3605                 fcp->isp_loopstate = LOOP_FSCAN_DONE;
3606                 return (0);
3607         }
3608
3609
3610         /*
3611          * If we get this far, we certainly still have the fabric controller.
3612          */
3613         fcp->portdb[FL_ID].state = FC_PORTDB_STATE_PENDING_VALID;
3614
3615         /*
3616          * Prime the handle we will start using.
3617          */
3618         oldhandle = FCPARAM(isp, 0)->isp_lasthdl;
3619
3620         /*
3621          * Go through the list and remove duplicate port ids.
3622          */
3623
3624         portlim = 0;
3625         portidx = 0;
3626         for (portidx = 0; portidx < NGENT-1; portidx++) {
3627                 if (rs1->snscb_ports[portidx].control & 0x80) {
3628                         break;
3629                 }
3630         }
3631
3632         /*
3633          * If we're not at the last entry, our list wasn't big enough.
3634          */
3635         if ((rs1->snscb_ports[portidx].control & 0x80) == 0) {
3636                 isp_prt(isp, ISP_LOGWARN,
3637                     "fabric too big for scratch area: increase ISP_FC_SCRLEN");
3638         }
3639         portlim = portidx + 1;
3640         isp_prt(isp, ISP_LOG_SANCFG,
3641             "Chan %d got %d ports back from name server", chan, portlim);
3642
3643         for (portidx = 0; portidx < portlim; portidx++) {
3644                 int npidx;
3645
3646                 portid =
3647                     ((rs1->snscb_ports[portidx].portid[0]) << 16) |
3648                     ((rs1->snscb_ports[portidx].portid[1]) << 8) |
3649                     ((rs1->snscb_ports[portidx].portid[2]));
3650
3651                 for (npidx = portidx + 1; npidx < portlim; npidx++) {
3652                         uint32_t new_portid =
3653                             ((rs1->snscb_ports[npidx].portid[0]) << 16) |
3654                             ((rs1->snscb_ports[npidx].portid[1]) << 8) |
3655                             ((rs1->snscb_ports[npidx].portid[2]));
3656                         if (new_portid == portid) {
3657                                 break;
3658                         }
3659                 }
3660
3661                 if (npidx < portlim) {
3662                         rs1->snscb_ports[npidx].portid[0] = 0;
3663                         rs1->snscb_ports[npidx].portid[1] = 0;
3664                         rs1->snscb_ports[npidx].portid[2] = 0;
3665                         isp_prt(isp, ISP_LOG_SANCFG, "Chan %d removing duplicate PortID 0x%06x entry from list", chan, portid);
3666                 }
3667         }
3668
3669         /*
3670          * We now have a list of Port IDs for all FC4 SCSI devices
3671          * that the Fabric Name server knows about.
3672          *
3673          * For each entry on this list go through our port database looking
3674          * for probational entries- if we find one, then an old entry is
3675          * maybe still this one. We get some information to find out.
3676          *
3677          * Otherwise, it's a new fabric device, and we log into it
3678          * (unconditionally). After searching the entire database
3679          * again to make sure that we never ever ever ever have more
3680          * than one entry that has the same PortID or the same
3681          * WWNN/WWPN duple, we enter the device into our database.
3682          */
3683
3684         for (portidx = 0; portidx < portlim; portidx++) {
3685                 fcportdb_t *lp;
3686                 uint64_t wwnn, wwpn;
3687                 int dbidx, nr;
3688
3689                 portid =
3690                     ((rs1->snscb_ports[portidx].portid[0]) << 16) |
3691                     ((rs1->snscb_ports[portidx].portid[1]) << 8) |
3692                     ((rs1->snscb_ports[portidx].portid[2]));
3693
3694                 if (portid == 0) {
3695                         isp_prt(isp, ISP_LOG_SANCFG,
3696                             "Chan %d skipping null PortID at idx %d",
3697                             chan, portidx);
3698                         continue;
3699                 }
3700
3701                 /*
3702                  * Skip ourselves here and on other channels. If we're
3703                  * multi-id, we can't check the portids in other FCPARAM
3704                  * arenas because the resolutions here aren't synchronized.
3705                  * The best way to do this is to exclude looking at portids
3706                  * that have the same domain and area code as our own
3707                  * portid.
3708                  */
3709                 if (ISP_CAP_MULTI_ID(isp) && isp->isp_nchan > 1) {
3710                         if ((portid >> 8) == (fcp->isp_portid >> 8)) {
3711                                 isp_prt(isp, ISP_LOG_SANCFG,
3712                                     "Chan %d skip PortID 0x%06x",
3713                                     chan, portid);
3714                                 continue;
3715                         }
3716                 } else if (portid == fcp->isp_portid) {
3717                         isp_prt(isp, ISP_LOG_SANCFG,
3718                             "Chan %d skip ourselves on @ PortID 0x%06x",
3719                             chan, portid);
3720                         continue;
3721                 }
3722
3723                 isp_prt(isp, ISP_LOG_SANCFG,
3724                     "Chan %d Checking Fabric Port 0x%06x", chan, portid);
3725
3726                 /*
3727                  * We now search our Port Database for any
3728                  * probational entries with this PortID. We don't
3729                  * look for zombies here- only probational
3730                  * entries (we've already logged out of zombies).
3731                  */
3732                 for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
3733                         lp = &fcp->portdb[dbidx];
3734
3735                         if (lp->state != FC_PORTDB_STATE_PROBATIONAL) {
3736                                 continue;
3737                         }
3738                         if (lp->portid == portid) {
3739                                 break;
3740                         }
3741                 }
3742
3743                 /*
3744                  * We found a probational entry with this Port ID.
3745                  */
3746                 if (dbidx < MAX_FC_TARG) {
3747                         int handle_changed = 0;
3748
3749                         lp = &fcp->portdb[dbidx];
3750
3751                         /*
3752                          * See if we're still logged into it.
3753                          *
3754                          * If we aren't, mark it as a dead device and
3755                          * leave the new portid in the database entry
3756                          * for somebody further along to decide what to
3757                          * do (policy choice).
3758                          *
3759                          * If we are, check to see if it's the same
3760                          * device still (it should be). If for some
3761                          * reason it isn't, mark it as a changed device
3762                          * and leave the new portid and role in the
3763                          * database entry for somebody further along to
3764                          * decide what to do (policy choice).
3765                          *
3766                          */
3767
3768                         r = isp_getpdb(isp, chan, lp->handle, &pdb, 0);
3769                         if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
3770                                 FC_SCRATCH_RELEASE(isp, chan);
3771                                 ISP_MARK_PORTDB(isp, chan, 1);
3772                                 return (-1);
3773                         }
3774                         if (r != 0) {
3775                                 lp->new_portid = portid;
3776                                 lp->state = FC_PORTDB_STATE_DEAD;
3777                                 isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Fabric PortID 0x%06x handle 0x%x is dead (%d)", chan, portid, lp->handle, r);
3778                                 continue;
3779                         }
3780
3781
3782                         /*
3783                          * Check to make sure that handle, portid, WWPN and
3784                          * WWNN agree. If they don't, then the association
3785                          * between this PortID and the stated handle has been
3786                          * broken by the firmware.
3787                          */
3788                         MAKE_WWN_FROM_NODE_NAME(wwnn, pdb.nodename);
3789                         MAKE_WWN_FROM_NODE_NAME(wwpn, pdb.portname);
3790                         if (pdb.handle != lp->handle ||
3791                             pdb.portid != portid ||
3792                             wwpn != lp->port_wwn ||
3793                             (lp->node_wwn != 0 && wwnn != lp->node_wwn)) {
3794                                 isp_prt(isp, ISP_LOG_SANCFG,
3795                                     fconf, chan, dbidx, pdb.handle, pdb.portid,
3796                                     (uint32_t) (wwnn >> 32), (uint32_t) wwnn,
3797                                     (uint32_t) (wwpn >> 32), (uint32_t) wwpn,
3798                                     lp->handle, portid,
3799                                     (uint32_t) (lp->node_wwn >> 32),
3800                                     (uint32_t) lp->node_wwn,
3801                                     (uint32_t) (lp->port_wwn >> 32),
3802                                     (uint32_t) lp->port_wwn);
3803                                 /*
3804                                  * Try to re-login to this device using a
3805                                  * new handle. If that fails, mark it dead.
3806                                  *
3807                                  * isp_login_device will check for handle and
3808                                  * portid consistency after re-login.
3809                                  *
3810                                  */
3811                                 if ((fcp->role & ISP_ROLE_INITIATOR) == 0 ||
3812                                     isp_login_device(isp, chan, portid, &pdb,
3813                                      &oldhandle)) {
3814                                         lp->new_portid = portid;
3815                                         lp->state = FC_PORTDB_STATE_DEAD;
3816                                         if (fcp->isp_loopstate !=
3817                                             LOOP_SCANNING_FABRIC) {
3818                                                 FC_SCRATCH_RELEASE(isp, chan);
3819                                                 ISP_MARK_PORTDB(isp, chan, 1);
3820                                                 return (-1);
3821                                         }
3822                                         continue;
3823                                 }
3824                                 if (fcp->isp_loopstate !=
3825                                     LOOP_SCANNING_FABRIC) {
3826                                         FC_SCRATCH_RELEASE(isp, chan);
3827                                         ISP_MARK_PORTDB(isp, chan, 1);
3828                                         return (-1);
3829                                 }
3830                                 FCPARAM(isp, 0)->isp_lasthdl = oldhandle;
3831                                 MAKE_WWN_FROM_NODE_NAME(wwnn, pdb.nodename);
3832                                 MAKE_WWN_FROM_NODE_NAME(wwpn, pdb.portname);
3833                                 if (wwpn != lp->port_wwn ||
3834                                     (lp->node_wwn != 0 && wwnn != lp->node_wwn)) {
3835                                         isp_prt(isp, ISP_LOGWARN, "changed WWN"
3836                                             " after relogin");
3837                                         lp->new_portid = portid;
3838                                         lp->state = FC_PORTDB_STATE_DEAD;
3839                                         continue;
3840                                 }
3841
3842                                 lp->handle = pdb.handle;
3843                                 handle_changed++;
3844                         }
3845
3846                         nr = pdb.prli_word3;
3847
3848                         /*
3849                          * Check to see whether the portid and roles have
3850                          * stayed the same. If they have stayed the same,
3851                          * we believe that this is the same device and it
3852                          * hasn't become disconnected and reconnected, so
3853                          * mark it as pending valid.
3854                          *
3855                          * If they aren't the same, mark the device as a
3856                          * changed device and save the new port id and role
3857                          * and let somebody else decide.
3858                          */
3859
3860                         lp->new_portid = portid;
3861                         lp->new_prli_word3 = nr;
3862                         if (pdb.portid != lp->portid || nr != lp->prli_word3 || handle_changed) {
3863                                 isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Fabric Port 0x%06x changed", chan, portid);
3864                                 lp->state = FC_PORTDB_STATE_CHANGED;
3865                         } else {
3866                                 isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Fabric Port 0x%06x Now Pending Valid", chan, portid);
3867                                 lp->state = FC_PORTDB_STATE_PENDING_VALID;
3868                         }
3869                         continue;
3870                 }
3871
3872                 if ((fcp->role & ISP_ROLE_INITIATOR) == 0)
3873                         continue;
3874
3875                 /*
3876                  * Ah- a new entry. Search the database again for all non-NIL
3877                  * entries to make sure we never ever make a new database entry
3878                  * with the same port id. While we're at it, mark where the
3879                  * last free entry was.
3880                  */
3881
3882                 dbidx = MAX_FC_TARG;
3883                 for (lp = fcp->portdb; lp < &fcp->portdb[MAX_FC_TARG]; lp++) {
3884                         if (lp >= &fcp->portdb[FL_ID] &&
3885                             lp <= &fcp->portdb[SNS_ID]) {
3886                                 continue;
3887                         }
3888                         if (lp->state == FC_PORTDB_STATE_NIL) {
3889                                 if (dbidx == MAX_FC_TARG) {
3890                                         dbidx = lp - fcp->portdb;
3891                                 }
3892                                 continue;
3893                         }
3894                         if (lp->state == FC_PORTDB_STATE_ZOMBIE) {
3895                                 continue;
3896                         }
3897                         if (lp->portid == portid) {
3898                                 break;
3899                         }
3900                 }
3901
3902                 if (lp < &fcp->portdb[MAX_FC_TARG]) {
3903                         isp_prt(isp, ISP_LOGWARN, "Chan %d PortID 0x%06x "
3904                             "already at %d handle %d state %d",
3905                             chan, portid, dbidx, lp->handle, lp->state);
3906                         continue;
3907                 }
3908
3909                 /*
3910                  * We should have the index of the first free entry seen.
3911                  */
3912                 if (dbidx == MAX_FC_TARG) {
3913                         isp_prt(isp, ISP_LOGERR,
3914                             "port database too small to login PortID 0x%06x"
3915                             "- increase MAX_FC_TARG", portid);
3916                         continue;
3917                 }
3918
3919                 /*
3920                  * Otherwise, point to our new home.
3921                  */
3922                 lp = &fcp->portdb[dbidx];
3923
3924                 /*
3925                  * Try to see if we are logged into this device,
3926                  * and maybe log into it.
3927                  *
3928                  * isp_login_device will check for handle and
3929                  * portid consistency after login.
3930                  */
3931                 if (isp_login_device(isp, chan, portid, &pdb, &oldhandle)) {
3932                         if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
3933                                 FC_SCRATCH_RELEASE(isp, chan);
3934                                 ISP_MARK_PORTDB(isp, chan, 1);
3935                                 return (-1);
3936                         }
3937                         continue;
3938                 }
3939                 if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
3940                         FC_SCRATCH_RELEASE(isp, chan);
3941                         ISP_MARK_PORTDB(isp, chan, 1);
3942                         return (-1);
3943                 }
3944                 FCPARAM(isp, 0)->isp_lasthdl = oldhandle;
3945
3946                 handle = pdb.handle;
3947                 MAKE_WWN_FROM_NODE_NAME(wwnn, pdb.nodename);
3948                 MAKE_WWN_FROM_NODE_NAME(wwpn, pdb.portname);
3949                 nr = pdb.prli_word3;
3950
3951                 /*
3952                  * And go through the database *one* more time to make sure
3953                  * that we do not make more than one entry that has the same
3954                  * WWNN/WWPN duple
3955                  */
3956                 for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
3957                         if (dbidx >= FL_ID && dbidx <= SNS_ID) {
3958                                 continue;
3959                         }
3960                         if ((fcp->portdb[dbidx].node_wwn == wwnn ||
3961                              fcp->portdb[dbidx].node_wwn == 0) &&
3962                             fcp->portdb[dbidx].port_wwn == wwpn) {
3963                                 break;
3964                         }
3965                 }
3966
3967                 if (dbidx == MAX_FC_TARG) {
3968                         ISP_MEMZERO(lp, sizeof (fcportdb_t));
3969                         lp->handle = handle;
3970                         lp->node_wwn = wwnn;
3971                         lp->port_wwn = wwpn;
3972                         lp->new_portid = portid;
3973                         lp->new_prli_word3 = nr;
3974                         lp->state = FC_PORTDB_STATE_NEW;
3975                         isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Fabric Port 0x%06x is a New Entry", chan, portid);
3976                         continue;
3977                 }
3978
3979                 if (fcp->portdb[dbidx].state != FC_PORTDB_STATE_ZOMBIE) {
3980                         isp_prt(isp, ISP_LOGWARN,
3981                             "Chan %d PortID 0x%x 0x%08x%08x/0x%08x%08x %ld "
3982                             "already at idx %d, state 0x%x", chan, portid,
3983                             (uint32_t) (wwnn >> 32), (uint32_t) wwnn,
3984                             (uint32_t) (wwpn >> 32), (uint32_t) wwpn,
3985                             (long) (lp - fcp->portdb), dbidx,
3986                             fcp->portdb[dbidx].state);
3987                         continue;
3988                 }
3989
3990                 /*
3991                  * We found a zombie entry that matches us.
3992                  * Revive it. We know that WWN and WWPN
3993                  * are the same. For fabric devices, we
3994                  * don't care that handle is different
3995                  * as we assign that. If role or portid
3996                  * are different, it maybe a changed device.
3997                  */
3998                 lp = &fcp->portdb[dbidx];
3999                 lp->handle = handle;
4000                 lp->node_wwn = wwnn;
4001                 lp->new_portid = portid;
4002                 lp->new_prli_word3 = nr;
4003                 if (lp->portid != portid || lp->prli_word3 != nr) {
4004                         isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Zombie Fabric Port 0x%06x Now Changed", chan, portid);
4005                         lp->state = FC_PORTDB_STATE_CHANGED;
4006                 } else {
4007                         isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Zombie Fabric Port 0x%06x Now Pending Valid", chan, portid);
4008                         lp->state = FC_PORTDB_STATE_PENDING_VALID;
4009                 }
4010         }
4011
4012         FC_SCRATCH_RELEASE(isp, chan);
4013         if (fcp->isp_loopstate != LOOP_SCANNING_FABRIC) {
4014                 ISP_MARK_PORTDB(isp, chan, 1);
4015                 return (-1);
4016         }
4017         fcp->isp_loopstate = LOOP_FSCAN_DONE;
4018         isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC Scan Fabric Done", chan);
4019         return (0);
4020 }
4021
4022 /*
4023  * Find an unused handle and try and use to login to a port.
4024  */
4025 static int
4026 isp_login_device(ispsoftc_t *isp, int chan, uint32_t portid, isp_pdb_t *p, uint16_t *ohp)
4027 {
4028         int lim, i, r;
4029         uint16_t handle;
4030
4031         if (ISP_CAP_2KLOGIN(isp)) {
4032                 lim = NPH_MAX_2K;
4033         } else {
4034                 lim = NPH_MAX;
4035         }
4036
4037         handle = isp_nxt_handle(isp, chan, *ohp);
4038         for (i = 0; i < lim; i++) {
4039                 /*
4040                  * See if we're still logged into something with
4041                  * this handle and that something agrees with this
4042                  * port id.
4043                  */
4044                 r = isp_getpdb(isp, chan, handle, p, 0);
4045                 if (r == 0 && p->portid != portid) {
4046                         (void) isp_plogx(isp, chan, handle, portid, PLOGX_FLG_CMD_LOGO | PLOGX_FLG_IMPLICIT | PLOGX_FLG_FREE_NPHDL, 1);
4047                 } else if (r == 0) {
4048                         break;
4049                 }
4050                 if (FCPARAM(isp, chan)->isp_loopstate != LOOP_SCANNING_FABRIC) {
4051                         return (-1);
4052                 }
4053                 /*
4054                  * Now try and log into the device
4055                  */
4056                 r = isp_plogx(isp, chan, handle, portid, PLOGX_FLG_CMD_PLOGI, 1);
4057                 if (FCPARAM(isp, chan)->isp_loopstate != LOOP_SCANNING_FABRIC) {
4058                         return (-1);
4059                 }
4060                 if (r == 0) {
4061                         *ohp = handle;
4062                         break;
4063                 } else if ((r & 0xffff) == MBOX_PORT_ID_USED) {
4064                         /*
4065                          * If we get here, then the firmwware still thinks we're logged into this device, but with a different
4066                          * handle. We need to break that association. We used to try and just substitute the handle, but then
4067                          * failed to get any data via isp_getpdb (below).
4068                          */
4069                         if (isp_plogx(isp, chan, r >> 16, portid, PLOGX_FLG_CMD_LOGO | PLOGX_FLG_IMPLICIT | PLOGX_FLG_FREE_NPHDL, 1)) {
4070                                 isp_prt(isp, ISP_LOGERR, "baw... logout of %x failed", r >> 16);
4071                         }
4072                         if (FCPARAM(isp, chan)->isp_loopstate != LOOP_SCANNING_FABRIC) {
4073                                 return (-1);
4074                         }
4075                         r = isp_plogx(isp, chan, handle, portid, PLOGX_FLG_CMD_PLOGI, 1);
4076                         if (FCPARAM(isp, chan)->isp_loopstate != LOOP_SCANNING_FABRIC) {
4077                                 return (-1);
4078                         }
4079                         if (r == 0) {
4080                                 *ohp = handle;
4081                         } else {
4082                                 i = lim;
4083                         }
4084                         break;
4085                 } else if ((r & 0xffff) == MBOX_LOOP_ID_USED) {
4086                         /*
4087                          * Try the next loop id.
4088                          */
4089                         *ohp = handle;
4090                         handle = isp_nxt_handle(isp, chan, handle);
4091                 } else {
4092                         /*
4093                          * Give up.
4094                          */
4095                         i = lim;
4096                         break;
4097                 }
4098         }
4099
4100         if (i == lim) {
4101                 isp_prt(isp, ISP_LOGWARN, "Chan %d PLOGI 0x%06x failed", chan, portid);
4102                 return (-1);
4103         }
4104
4105         /*
4106          * If we successfully logged into it, get the PDB for it
4107          * so we can crosscheck that it is still what we think it
4108          * is and that we also have the role it plays
4109          */
4110         r = isp_getpdb(isp, chan, handle, p, 0);
4111         if (FCPARAM(isp, chan)->isp_loopstate != LOOP_SCANNING_FABRIC) {
4112                 return (-1);
4113         }
4114         if (r != 0) {
4115                 isp_prt(isp, ISP_LOGERR, "Chan %d new device 0x%06x@0x%x disappeared", chan, portid, handle);
4116                 return (-1);
4117         }
4118
4119         if (p->handle != handle || p->portid != portid) {
4120                 isp_prt(isp, ISP_LOGERR, "Chan %d new device 0x%06x@0x%x changed (0x%06x@0x%0x)",
4121                     chan, portid, handle, p->portid, p->handle);
4122                 return (-1);
4123         }
4124         return (0);
4125 }
4126
4127 static int
4128 isp_register_fc4_type(ispsoftc_t *isp, int chan)
4129 {
4130         fcparam *fcp = FCPARAM(isp, chan);
4131         uint8_t local[SNS_RFT_ID_REQ_SIZE];
4132         sns_screq_t *reqp = (sns_screq_t *) local;
4133         mbreg_t mbs;
4134
4135         ISP_MEMZERO((void *) reqp, SNS_RFT_ID_REQ_SIZE);
4136         reqp->snscb_rblen = SNS_RFT_ID_RESP_SIZE >> 1;
4137         reqp->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma + 0x100);
4138         reqp->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma + 0x100);
4139         reqp->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma + 0x100);
4140         reqp->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma + 0x100);
4141         reqp->snscb_sblen = 22;
4142         reqp->snscb_data[0] = SNS_RFT_ID;
4143         reqp->snscb_data[4] = fcp->isp_portid & 0xffff;
4144         reqp->snscb_data[5] = (fcp->isp_portid >> 16) & 0xff;
4145         reqp->snscb_data[6] = (1 << FC4_SCSI);
4146         if (FC_SCRATCH_ACQUIRE(isp, chan)) {
4147                 isp_prt(isp, ISP_LOGERR, sacq);
4148                 return (-1);
4149         }
4150         isp_put_sns_request(isp, reqp, (sns_screq_t *) fcp->isp_scratch);
4151         MBSINIT(&mbs, MBOX_SEND_SNS, MBLOGALL, 1000000);
4152         mbs.param[1] = SNS_RFT_ID_REQ_SIZE >> 1;
4153         mbs.param[2] = DMA_WD1(fcp->isp_scdma);
4154         mbs.param[3] = DMA_WD0(fcp->isp_scdma);
4155         mbs.param[6] = DMA_WD3(fcp->isp_scdma);
4156         mbs.param[7] = DMA_WD2(fcp->isp_scdma);
4157         MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_RFT_ID_REQ_SIZE, chan);
4158         isp_mboxcmd(isp, &mbs);
4159         FC_SCRATCH_RELEASE(isp, chan);
4160         if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
4161                 return (0);
4162         } else {
4163                 return (-1);
4164         }
4165 }
4166
4167 static int
4168 isp_register_fc4_type_24xx(ispsoftc_t *isp, int chan)
4169 {
4170         mbreg_t mbs;
4171         fcparam *fcp = FCPARAM(isp, chan);
4172         union {
4173                 isp_ct_pt_t plocal;
4174                 rft_id_t clocal;
4175                 uint8_t q[QENTRY_LEN];
4176         } un;
4177         isp_ct_pt_t *pt;
4178         ct_hdr_t *ct;
4179         rft_id_t *rp;
4180         uint8_t *scp = fcp->isp_scratch;
4181
4182         if (FC_SCRATCH_ACQUIRE(isp, chan)) {
4183                 isp_prt(isp, ISP_LOGERR, sacq);
4184                 return (-1);
4185         }
4186
4187         /*
4188          * Build a Passthrough IOCB in memory.
4189          */
4190         ISP_MEMZERO(un.q, QENTRY_LEN);
4191         pt = &un.plocal;
4192         pt->ctp_header.rqs_entry_count = 1;
4193         pt->ctp_header.rqs_entry_type = RQSTYPE_CT_PASSTHRU;
4194         pt->ctp_handle = 0xffffffff;
4195         pt->ctp_nphdl = fcp->isp_sns_hdl;
4196         pt->ctp_cmd_cnt = 1;
4197         pt->ctp_vpidx = ISP_GET_VPIDX(isp, chan);
4198         pt->ctp_time = 1;
4199         pt->ctp_rsp_cnt = 1;
4200         pt->ctp_rsp_bcnt = sizeof (ct_hdr_t);
4201         pt->ctp_cmd_bcnt = sizeof (rft_id_t);
4202         pt->ctp_dataseg[0].ds_base = DMA_LO32(fcp->isp_scdma+XTXOFF);
4203         pt->ctp_dataseg[0].ds_basehi = DMA_HI32(fcp->isp_scdma+XTXOFF);
4204         pt->ctp_dataseg[0].ds_count = sizeof (rft_id_t);
4205         pt->ctp_dataseg[1].ds_base = DMA_LO32(fcp->isp_scdma+IGPOFF);
4206         pt->ctp_dataseg[1].ds_basehi = DMA_HI32(fcp->isp_scdma+IGPOFF);
4207         pt->ctp_dataseg[1].ds_count = sizeof (ct_hdr_t);
4208         isp_put_ct_pt(isp, pt, (isp_ct_pt_t *) &scp[CTXOFF]);
4209         if (isp->isp_dblev & ISP_LOGDEBUG1) {
4210                 isp_print_bytes(isp, "IOCB CT Request", QENTRY_LEN, pt);
4211         }
4212
4213         /*
4214          * Build the CT header and command in memory.
4215          *
4216          * Note that the CT header has to end up as Big Endian format in memory.
4217          */
4218         ISP_MEMZERO(&un.clocal, sizeof (un.clocal));
4219         ct = &un.clocal.rftid_hdr;
4220         ct->ct_revision = CT_REVISION;
4221         ct->ct_fcs_type = CT_FC_TYPE_FC;
4222         ct->ct_fcs_subtype = CT_FC_SUBTYPE_NS;
4223         ct->ct_cmd_resp = SNS_RFT_ID;
4224         ct->ct_bcnt_resid = (sizeof (rft_id_t) - sizeof (ct_hdr_t)) >> 2;
4225         rp = &un.clocal;
4226         rp->rftid_portid[0] = fcp->isp_portid >> 16;
4227         rp->rftid_portid[1] = fcp->isp_portid >> 8;
4228         rp->rftid_portid[2] = fcp->isp_portid;
4229         rp->rftid_fc4types[FC4_SCSI >> 5] = 1 << (FC4_SCSI & 0x1f);
4230         isp_put_rft_id(isp, rp, (rft_id_t *) &scp[XTXOFF]);
4231         if (isp->isp_dblev & ISP_LOGDEBUG1) {
4232                 isp_print_bytes(isp, "CT Header", QENTRY_LEN, &scp[XTXOFF]);
4233         }
4234
4235         ISP_MEMZERO(&scp[ZTXOFF], sizeof (ct_hdr_t));
4236
4237         MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 1000000);
4238         mbs.param[1] = QENTRY_LEN;
4239         mbs.param[2] = DMA_WD1(fcp->isp_scdma + CTXOFF);
4240         mbs.param[3] = DMA_WD0(fcp->isp_scdma + CTXOFF);
4241         mbs.param[6] = DMA_WD3(fcp->isp_scdma + CTXOFF);
4242         mbs.param[7] = DMA_WD2(fcp->isp_scdma + CTXOFF);
4243         MEMORYBARRIER(isp, SYNC_SFORDEV, XTXOFF, 2 * QENTRY_LEN, chan);
4244         isp_mboxcmd(isp, &mbs);
4245         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
4246                 FC_SCRATCH_RELEASE(isp, chan);
4247                 return (-1);
4248         }
4249         MEMORYBARRIER(isp, SYNC_SFORCPU, ZTXOFF, QENTRY_LEN, chan);
4250         pt = &un.plocal;
4251         isp_get_ct_pt(isp, (isp_ct_pt_t *) &scp[ZTXOFF], pt);
4252         if (isp->isp_dblev & ISP_LOGDEBUG1) {
4253                 isp_print_bytes(isp, "IOCB response", QENTRY_LEN, pt);
4254         }
4255         if (pt->ctp_status) {
4256                 FC_SCRATCH_RELEASE(isp, chan);
4257                 isp_prt(isp, ISP_LOGWARN,
4258                     "Chan %d Register FC4 Type CT Passthrough returned 0x%x",
4259                     chan, pt->ctp_status);
4260                 return (1);
4261         }
4262
4263         isp_get_ct_hdr(isp, (ct_hdr_t *) &scp[IGPOFF], ct);
4264         FC_SCRATCH_RELEASE(isp, chan);
4265
4266         if (ct->ct_cmd_resp == LS_RJT) {
4267                 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOG_WARN1, "Chan %d Register FC4 Type rejected", chan);
4268                 return (-1);
4269         } else if (ct->ct_cmd_resp == LS_ACC) {
4270                 isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Register FC4 Type accepted", chan);
4271                 return (0);
4272         } else {
4273                 isp_prt(isp, ISP_LOGWARN, "Chan %d Register FC4 Type: 0x%x", chan, ct->ct_cmd_resp);
4274                 return (-1);
4275         }
4276 }
4277
4278 static uint16_t
4279 isp_nxt_handle(ispsoftc_t *isp, int chan, uint16_t handle)
4280 {
4281         int i;
4282         if (handle == NIL_HANDLE) {
4283                 if (FCPARAM(isp, chan)->isp_topo == TOPO_F_PORT) {
4284                         handle = 0;
4285                 } else {
4286                         handle = SNS_ID+1;
4287                 }
4288         } else {
4289                 handle += 1;
4290                 if (handle >= FL_ID && handle <= SNS_ID) {
4291                         handle = SNS_ID+1;
4292                 }
4293                 if (handle >= NPH_RESERVED && handle <= NPH_FL_ID) {
4294                         handle = NPH_FL_ID+1;
4295                 }
4296                 if (ISP_CAP_2KLOGIN(isp)) {
4297                         if (handle == NPH_MAX_2K) {
4298                                 handle = 0;
4299                         }
4300                 } else {
4301                         if (handle == NPH_MAX) {
4302                                 handle = 0;
4303                         }
4304                 }
4305         }
4306         if (handle == FCPARAM(isp, chan)->isp_loopid) {
4307                 return (isp_nxt_handle(isp, chan, handle));
4308         }
4309         for (i = 0; i < MAX_FC_TARG; i++) {
4310                 if (FCPARAM(isp, chan)->portdb[i].state ==
4311                     FC_PORTDB_STATE_NIL) {
4312                         continue;
4313                 }
4314                 if (FCPARAM(isp, chan)->portdb[i].handle == handle) {
4315                         return (isp_nxt_handle(isp, chan, handle));
4316                 }
4317         }
4318         return (handle);
4319 }
4320
4321 /*
4322  * Start a command. Locking is assumed done in the caller.
4323  */
4324
4325 int
4326 isp_start(XS_T *xs)
4327 {
4328         ispsoftc_t *isp;
4329         uint32_t handle, cdblen;
4330         uint8_t local[QENTRY_LEN];
4331         ispreq_t *reqp;
4332         void *cdbp, *qep;
4333         uint16_t *tptr;
4334         fcportdb_t *lp;
4335         int target, dmaresult;
4336
4337         XS_INITERR(xs);
4338         isp = XS_ISP(xs);
4339
4340         /*
4341          * Now make sure we're running.
4342          */
4343
4344         if (isp->isp_state != ISP_RUNSTATE) {
4345                 isp_prt(isp, ISP_LOGERR, "Adapter not at RUNSTATE");
4346                 XS_SETERR(xs, HBA_BOTCH);
4347                 return (CMD_COMPLETE);
4348         }
4349
4350         /*
4351          * Check command CDB length, etc.. We really are limited to 16 bytes
4352          * for Fibre Channel, but can do up to 44 bytes in parallel SCSI,
4353          * but probably only if we're running fairly new firmware (we'll
4354          * let the old f/w choke on an extended command queue entry).
4355          */
4356
4357         if (XS_CDBLEN(xs) > (IS_FC(isp)? 16 : 44) || XS_CDBLEN(xs) == 0) {
4358                 isp_prt(isp, ISP_LOGERR, "unsupported cdb length (%d, CDB[0]=0x%x)", XS_CDBLEN(xs), XS_CDBP(xs)[0] & 0xff);
4359                 XS_SETERR(xs, HBA_BOTCH);
4360                 return (CMD_COMPLETE);
4361         }
4362
4363         /*
4364          * Translate the target to device handle as appropriate, checking
4365          * for correct device state as well.
4366          */
4367         target = XS_TGT(xs);
4368         if (IS_FC(isp)) {
4369                 fcparam *fcp = FCPARAM(isp, XS_CHANNEL(xs));
4370
4371                 if ((fcp->role & ISP_ROLE_INITIATOR) == 0) {
4372                         isp_prt(isp, ISP_LOG_WARN1, "%d.%d.%d I am not an initiator", XS_CHANNEL(xs), target, XS_LUN(xs));
4373                         XS_SETERR(xs, HBA_SELTIMEOUT);
4374                         return (CMD_COMPLETE);
4375                 }
4376
4377                 /*
4378                  * Try again later.
4379                  */
4380                 if (fcp->isp_fwstate != FW_READY || fcp->isp_loopstate != LOOP_READY) {
4381                         return (CMD_RQLATER);
4382                 }
4383
4384                 isp_prt(isp, ISP_LOGDEBUG2, "XS_TGT(xs)=%d", target);
4385                 lp = &fcp->portdb[target];
4386                 if (target < 0 || target >= MAX_FC_TARG ||
4387                     lp->is_target == 0) {
4388                         XS_SETERR(xs, HBA_SELTIMEOUT);
4389                         return (CMD_COMPLETE);
4390                 }
4391                 if (lp->state == FC_PORTDB_STATE_ZOMBIE) {
4392                         isp_prt(isp, ISP_LOGDEBUG1, "%d.%d.%d target zombie", XS_CHANNEL(xs), target, XS_LUN(xs));
4393                         return (CMD_RQLATER);
4394                 }
4395                 if (lp->state != FC_PORTDB_STATE_VALID) {
4396                         isp_prt(isp, ISP_LOGDEBUG1, "%d.%d.%d bad db port state 0x%x", XS_CHANNEL(xs), target, XS_LUN(xs), lp->state);
4397                         XS_SETERR(xs, HBA_SELTIMEOUT);
4398                         return (CMD_COMPLETE);
4399                 }
4400         } else {
4401                 sdparam *sdp = SDPARAM(isp, XS_CHANNEL(xs));
4402                 if ((sdp->role & ISP_ROLE_INITIATOR) == 0) {
4403                         isp_prt(isp, ISP_LOGDEBUG1, "%d.%d.%d I am not an initiator", XS_CHANNEL(xs), target, XS_LUN(xs));
4404                         XS_SETERR(xs, HBA_SELTIMEOUT);
4405                         return (CMD_COMPLETE);
4406                 }
4407                 if (sdp->update) {
4408                         isp_spi_update(isp, XS_CHANNEL(xs));
4409                 }
4410                 lp = NULL;
4411         }
4412
4413  start_again:
4414
4415         qep = isp_getrqentry(isp);
4416         if (qep == NULL) {
4417                 isp_prt(isp, ISP_LOG_WARN1, "Request Queue Overflow");
4418                 XS_SETERR(xs, HBA_BOTCH);
4419                 return (CMD_EAGAIN);
4420         }
4421         XS_SETERR(xs, HBA_NOERROR);
4422
4423         /*
4424          * Now see if we need to synchronize the ISP with respect to anything.
4425          * We do dual duty here (cough) for synchronizing for busses other
4426          * than which we got here to send a command to.
4427          */
4428         reqp = (ispreq_t *) local;
4429         ISP_MEMZERO(local, QENTRY_LEN);
4430         if (ISP_TST_SENDMARKER(isp, XS_CHANNEL(xs))) {
4431                 if (IS_24XX(isp)) {
4432                         isp_marker_24xx_t *m = (isp_marker_24xx_t *) reqp;
4433                         m->mrk_header.rqs_entry_count = 1;
4434                         m->mrk_header.rqs_entry_type = RQSTYPE_MARKER;
4435                         m->mrk_modifier = SYNC_ALL;
4436                         isp_put_marker_24xx(isp, m, qep);
4437                 } else {
4438                         isp_marker_t *m = (isp_marker_t *) reqp;
4439                         m->mrk_header.rqs_entry_count = 1;
4440                         m->mrk_header.rqs_entry_type = RQSTYPE_MARKER;
4441                         m->mrk_target = (XS_CHANNEL(xs) << 7);  /* bus # */
4442                         m->mrk_modifier = SYNC_ALL;
4443                         isp_put_marker(isp, m, qep);
4444                 }
4445                 ISP_SYNC_REQUEST(isp);
4446                 ISP_SET_SENDMARKER(isp, XS_CHANNEL(xs), 0);
4447                 goto start_again;
4448         }
4449
4450         reqp->req_header.rqs_entry_count = 1;
4451
4452         /*
4453          * Select and install Header Code.
4454          * Note that it might be overridden before going out
4455          * if we're on a 64 bit platform. The lower level
4456          * code (isp_send_cmd) will select the appropriate
4457          * 64 bit variant if it needs to.
4458          */
4459         if (IS_24XX(isp)) {
4460                 reqp->req_header.rqs_entry_type = RQSTYPE_T7RQS;
4461         } else if (IS_FC(isp)) {
4462                 reqp->req_header.rqs_entry_type = RQSTYPE_T2RQS;
4463         } else {
4464                 if (XS_CDBLEN(xs) > 12) {
4465                         reqp->req_header.rqs_entry_type = RQSTYPE_CMDONLY;
4466                 } else {
4467                         reqp->req_header.rqs_entry_type = RQSTYPE_REQUEST;
4468                 }
4469         }
4470
4471         /*
4472          * Set task attributes
4473          */
4474         if (IS_24XX(isp)) {
4475                 int ttype;
4476                 if (XS_TAG_P(xs)) {
4477                         ttype = XS_TAG_TYPE(xs);
4478                 } else {
4479                         if (XS_CDBP(xs)[0] == 0x3) {
4480                                 ttype = REQFLAG_HTAG;
4481                         } else {
4482                                 ttype = REQFLAG_STAG;
4483                         }
4484                 }
4485                 if (ttype == REQFLAG_OTAG) {
4486                         ttype = FCP_CMND_TASK_ATTR_ORDERED;
4487                 } else if (ttype == REQFLAG_HTAG) {
4488                         ttype = FCP_CMND_TASK_ATTR_HEAD;
4489                 } else {
4490                         ttype = FCP_CMND_TASK_ATTR_SIMPLE;
4491                 }
4492                 ((ispreqt7_t *)reqp)->req_task_attribute = ttype;
4493         } else if (IS_FC(isp)) {
4494                 /*
4495                  * See comment in isp_intr
4496                  */
4497                 /* XS_SET_RESID(xs, 0); */
4498
4499                 /*
4500                  * Fibre Channel always requires some kind of tag.
4501                  * The Qlogic drivers seem be happy not to use a tag,
4502                  * but this breaks for some devices (IBM drives).
4503                  */
4504                 if (XS_TAG_P(xs)) {
4505                         ((ispreqt2_t *)reqp)->req_flags = XS_TAG_TYPE(xs);
4506                 } else {
4507                         /*
4508                          * If we don't know what tag to use, use HEAD OF QUEUE
4509                          * for Request Sense or Simple.
4510                          */
4511                         if (XS_CDBP(xs)[0] == 0x3)      /* REQUEST SENSE */
4512                                 ((ispreqt2_t *)reqp)->req_flags = REQFLAG_HTAG;
4513                         else
4514                                 ((ispreqt2_t *)reqp)->req_flags = REQFLAG_STAG;
4515                 }
4516         } else {
4517                 sdparam *sdp = SDPARAM(isp, XS_CHANNEL(xs));
4518                 if ((sdp->isp_devparam[target].actv_flags & DPARM_TQING) && XS_TAG_P(xs)) {
4519                         reqp->req_flags = XS_TAG_TYPE(xs);
4520                 }
4521         }
4522
4523         tptr = &reqp->req_time;
4524
4525         /*
4526          * NB: we do not support long CDBs (yet)
4527          */
4528         cdblen = XS_CDBLEN(xs);
4529
4530         if (IS_SCSI(isp)) {
4531                 if (cdblen > sizeof (reqp->req_cdb)) {
4532                         isp_prt(isp, ISP_LOGERR, "Command Length %u too long for this chip", cdblen);
4533                         XS_SETERR(xs, HBA_BOTCH);
4534                         return (CMD_COMPLETE);
4535                 }
4536                 reqp->req_target = target | (XS_CHANNEL(xs) << 7);
4537                 reqp->req_lun_trn = XS_LUN(xs);
4538                 cdbp = reqp->req_cdb;
4539                 reqp->req_cdblen = cdblen;
4540         } else if (IS_24XX(isp)) {
4541                 ispreqt7_t *t7 = (ispreqt7_t *)local;
4542
4543                 if (cdblen > sizeof (t7->req_cdb)) {
4544                         isp_prt(isp, ISP_LOGERR, "Command Length %u too long for this chip", cdblen);
4545                         XS_SETERR(xs, HBA_BOTCH);
4546                         return (CMD_COMPLETE);
4547                 }
4548
4549                 t7->req_nphdl = lp->handle;
4550                 t7->req_tidlo = lp->portid;
4551                 t7->req_tidhi = lp->portid >> 16;
4552                 t7->req_vpidx = ISP_GET_VPIDX(isp, XS_CHANNEL(xs));
4553                 if (XS_LUN(xs) > 256) {
4554                         t7->req_lun[0] = XS_LUN(xs) >> 8;
4555                         t7->req_lun[0] |= 0x40;
4556                 }
4557                 t7->req_lun[1] = XS_LUN(xs);
4558                 if (FCPARAM(isp, XS_CHANNEL(xs))->fctape_enabled && (lp->prli_word3 & PRLI_WD3_RETRY)) {
4559                         if (FCP_NEXT_CRN(isp, &t7->req_crn, xs)) {
4560                                 isp_prt(isp, ISP_LOG_WARN1, "%d.%d.%d cannot generate next CRN", XS_CHANNEL(xs), target, XS_LUN(xs));
4561                                 XS_SETERR(xs, HBA_BOTCH);
4562                                 return (CMD_EAGAIN);
4563                         }
4564                 }
4565                 tptr = &t7->req_time;
4566                 cdbp = t7->req_cdb;
4567         } else {
4568                 ispreqt2_t *t2 = (ispreqt2_t *)local;
4569
4570                 if (cdblen > sizeof t2->req_cdb) {
4571                         isp_prt(isp, ISP_LOGERR, "Command Length %u too long for this chip", cdblen);
4572                         XS_SETERR(xs, HBA_BOTCH);
4573                         return (CMD_COMPLETE);
4574                 }
4575                 if (FCPARAM(isp, XS_CHANNEL(xs))->fctape_enabled && (lp->prli_word3 & PRLI_WD3_RETRY)) {
4576                         if (FCP_NEXT_CRN(isp, &t2->req_crn, xs)) {
4577                                 isp_prt(isp, ISP_LOG_WARN1, "%d.%d.%d cannot generate next CRN", XS_CHANNEL(xs), target, XS_LUN(xs));
4578                                 XS_SETERR(xs, HBA_BOTCH);
4579                                 return (CMD_EAGAIN);
4580                         }
4581                 }
4582                 if (ISP_CAP_2KLOGIN(isp)) {
4583                         ispreqt2e_t *t2e = (ispreqt2e_t *)local;
4584                         t2e->req_target = lp->handle;
4585                         t2e->req_scclun = XS_LUN(xs);
4586                         cdbp = t2e->req_cdb;
4587                 } else if (ISP_CAP_SCCFW(isp)) {
4588                         ispreqt2_t *t2 = (ispreqt2_t *)local;
4589                         t2->req_target = lp->handle;
4590                         t2->req_scclun = XS_LUN(xs);
4591                         cdbp = t2->req_cdb;
4592                 } else {
4593                         t2->req_target = lp->handle;
4594                         t2->req_lun_trn = XS_LUN(xs);
4595                         cdbp = t2->req_cdb;
4596                 }
4597         }
4598         ISP_MEMCPY(cdbp, XS_CDBP(xs), cdblen);
4599
4600         *tptr = XS_TIME(xs) / 1000;
4601         if (*tptr == 0 && XS_TIME(xs)) {
4602                 *tptr = 1;
4603         }
4604         if (IS_24XX(isp) && *tptr > 0x1999) {
4605                 *tptr = 0x1999;
4606         }
4607
4608         if (isp_allocate_xs(isp, xs, &handle)) {
4609                 isp_prt(isp, ISP_LOG_WARN1, "out of xflist pointers");
4610                 XS_SETERR(xs, HBA_BOTCH);
4611                 return (CMD_EAGAIN);
4612         }
4613         /* Whew. Thankfully the same for type 7 requests */
4614         reqp->req_handle = handle;
4615
4616         /*
4617          * Set up DMA and/or do any platform dependent swizzling of the request entry
4618          * so that the Qlogic F/W understands what is being asked of it.
4619          *
4620          * The callee is responsible for adding all requests at this point.
4621          */
4622         dmaresult = ISP_DMASETUP(isp, xs, reqp);
4623         if (dmaresult != CMD_QUEUED) {
4624                 isp_destroy_handle(isp, handle);
4625                 /*
4626                  * dmasetup sets actual error in packet, and
4627                  * return what we were given to return.
4628                  */
4629                 return (dmaresult);
4630         }
4631         isp_xs_prt(isp, xs, ISP_LOGDEBUG0, "START cmd cdb[0]=0x%x datalen %ld", XS_CDBP(xs)[0], (long) XS_XFRLEN(xs));
4632         isp->isp_nactive++;
4633         return (CMD_QUEUED);
4634 }
4635
4636 /*
4637  * isp control
4638  * Locks (ints blocked) assumed held.
4639  */
4640
4641 int
4642 isp_control(ispsoftc_t *isp, ispctl_t ctl, ...)
4643 {
4644         XS_T *xs;
4645         mbreg_t *mbr, mbs;
4646         int chan, tgt;
4647         uint32_t handle;
4648         va_list ap;
4649
4650         switch (ctl) {
4651         case ISPCTL_RESET_BUS:
4652                 /*
4653                  * Issue a bus reset.
4654                  */
4655                 if (IS_24XX(isp)) {
4656                         isp_prt(isp, ISP_LOGERR, "BUS RESET NOT IMPLEMENTED");
4657                         break;
4658                 } else if (IS_FC(isp)) {
4659                         mbs.param[1] = 10;
4660                         chan = 0;
4661                 } else {
4662                         va_start(ap, ctl);
4663                         chan = va_arg(ap, int);
4664                         va_end(ap);
4665                         mbs.param[1] = SDPARAM(isp, chan)->isp_bus_reset_delay;
4666                         if (mbs.param[1] < 2) {
4667                                 mbs.param[1] = 2;
4668                         }
4669                         mbs.param[2] = chan;
4670                 }
4671                 MBSINIT(&mbs, MBOX_BUS_RESET, MBLOGALL, 0);
4672                 ISP_SET_SENDMARKER(isp, chan, 1);
4673                 isp_mboxcmd(isp, &mbs);
4674                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
4675                         break;
4676                 }
4677                 isp_prt(isp, ISP_LOGINFO, "driver initiated bus reset of bus %d", chan);
4678                 return (0);
4679
4680         case ISPCTL_RESET_DEV:
4681                 va_start(ap, ctl);
4682                 chan = va_arg(ap, int);
4683                 tgt = va_arg(ap, int);
4684                 va_end(ap);
4685                 if (IS_24XX(isp)) {
4686                         uint8_t local[QENTRY_LEN];
4687                         isp24xx_tmf_t *tmf;
4688                         isp24xx_statusreq_t *sp;
4689                         fcparam *fcp = FCPARAM(isp, chan);
4690                         fcportdb_t *lp;
4691
4692                         if (tgt < 0 || tgt >= MAX_FC_TARG) {
4693                                 isp_prt(isp, ISP_LOGWARN, "Chan %d trying to reset bad target %d", chan, tgt);
4694                                 break;
4695                         }
4696                         lp = &fcp->portdb[tgt];
4697                         if (lp->is_target == 0 ||
4698                             lp->state != FC_PORTDB_STATE_VALID) {
4699                                 isp_prt(isp, ISP_LOGWARN, "Chan %d abort of no longer valid target %d", chan, tgt);
4700                                 break;
4701                         }
4702
4703                         tmf = (isp24xx_tmf_t *) local;
4704                         ISP_MEMZERO(tmf, QENTRY_LEN);
4705                         tmf->tmf_header.rqs_entry_type = RQSTYPE_TSK_MGMT;
4706                         tmf->tmf_header.rqs_entry_count = 1;
4707                         tmf->tmf_nphdl = lp->handle;
4708                         tmf->tmf_delay = 2;
4709                         tmf->tmf_timeout = 2;
4710                         tmf->tmf_flags = ISP24XX_TMF_TARGET_RESET;
4711                         tmf->tmf_tidlo = lp->portid;
4712                         tmf->tmf_tidhi = lp->portid >> 16;
4713                         tmf->tmf_vpidx = ISP_GET_VPIDX(isp, chan);
4714                         isp_prt(isp, ISP_LOGALL, "Chan %d Reset N-Port Handle 0x%04x @ Port 0x%06x", chan, lp->handle, lp->portid);
4715                         MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 5000000);
4716                         mbs.param[1] = QENTRY_LEN;
4717                         mbs.param[2] = DMA_WD1(fcp->isp_scdma);
4718                         mbs.param[3] = DMA_WD0(fcp->isp_scdma);
4719                         mbs.param[6] = DMA_WD3(fcp->isp_scdma);
4720                         mbs.param[7] = DMA_WD2(fcp->isp_scdma);
4721
4722                         if (FC_SCRATCH_ACQUIRE(isp, chan)) {
4723                                 isp_prt(isp, ISP_LOGERR, sacq);
4724                                 break;
4725                         }
4726                         isp_put_24xx_tmf(isp, tmf, fcp->isp_scratch);
4727                         MEMORYBARRIER(isp, SYNC_SFORDEV, 0, QENTRY_LEN, chan);
4728                         fcp->sendmarker = 1;
4729                         isp_mboxcmd(isp, &mbs);
4730                         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
4731                                 FC_SCRATCH_RELEASE(isp, chan);
4732                                 break;
4733                         }
4734                         MEMORYBARRIER(isp, SYNC_SFORCPU, QENTRY_LEN, QENTRY_LEN, chan);
4735                         sp = (isp24xx_statusreq_t *) local;
4736                         isp_get_24xx_response(isp, &((isp24xx_statusreq_t *)fcp->isp_scratch)[1], sp);
4737                         FC_SCRATCH_RELEASE(isp, chan);
4738                         if (sp->req_completion_status == 0) {
4739                                 return (0);
4740                         }
4741                         isp_prt(isp, ISP_LOGWARN, "Chan %d reset of target %d returned 0x%x", chan, tgt, sp->req_completion_status);
4742                         break;
4743                 } else if (IS_FC(isp)) {
4744                         if (ISP_CAP_2KLOGIN(isp)) {
4745                                 mbs.param[1] = tgt;
4746                                 mbs.ibits = (1 << 10);
4747                         } else {
4748                                 mbs.param[1] = (tgt << 8);
4749                         }
4750                 } else {
4751                         mbs.param[1] = (chan << 15) | (tgt << 8);
4752                 }
4753                 MBSINIT(&mbs, MBOX_ABORT_TARGET, MBLOGALL, 0);
4754                 mbs.param[2] = 3;       /* 'delay', in seconds */
4755                 isp_mboxcmd(isp, &mbs);
4756                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
4757                         break;
4758                 }
4759                 isp_prt(isp, ISP_LOGINFO, "Target %d on Bus %d Reset Succeeded", tgt, chan);
4760                 ISP_SET_SENDMARKER(isp, chan, 1);
4761                 return (0);
4762
4763         case ISPCTL_ABORT_CMD:
4764                 va_start(ap, ctl);
4765                 xs = va_arg(ap, XS_T *);
4766                 va_end(ap);
4767
4768                 tgt = XS_TGT(xs);
4769                 chan = XS_CHANNEL(xs);
4770
4771                 handle = isp_find_handle(isp, xs);
4772                 if (handle == 0) {
4773                         isp_prt(isp, ISP_LOGWARN, "cannot find handle for command to abort");
4774                         break;
4775                 }
4776                 if (IS_24XX(isp)) {
4777                         isp24xx_abrt_t local, *ab = &local, *ab2;
4778                         fcparam *fcp;
4779                         fcportdb_t *lp;
4780
4781                         fcp = FCPARAM(isp, chan);
4782                         if (tgt < 0 || tgt >= MAX_FC_TARG) {
4783                                 isp_prt(isp, ISP_LOGWARN, "Chan %d trying to abort bad target %d", chan, tgt);
4784                                 break;
4785                         }
4786                         lp = &fcp->portdb[tgt];
4787                         if (lp->is_target == 0 ||
4788                             lp->state != FC_PORTDB_STATE_VALID) {
4789                                 isp_prt(isp, ISP_LOGWARN, "Chan %d abort of no longer valid target %d", chan, tgt);
4790                                 break;
4791                         }
4792                         isp_prt(isp, ISP_LOGALL, "Chan %d Abort Cmd for N-Port 0x%04x @ Port 0x%06x", chan, lp->handle, lp->portid);
4793                         ISP_MEMZERO(ab, QENTRY_LEN);
4794                         ab->abrt_header.rqs_entry_type = RQSTYPE_ABORT_IO;
4795                         ab->abrt_header.rqs_entry_count = 1;
4796                         ab->abrt_handle = lp->handle;
4797                         ab->abrt_cmd_handle = handle;
4798                         ab->abrt_tidlo = lp->portid;
4799                         ab->abrt_tidhi = lp->portid >> 16;
4800                         ab->abrt_vpidx = ISP_GET_VPIDX(isp, chan);
4801
4802                         ISP_MEMZERO(&mbs, sizeof (mbs));
4803                         MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 5000000);
4804                         mbs.param[1] = QENTRY_LEN;
4805                         mbs.param[2] = DMA_WD1(fcp->isp_scdma);
4806                         mbs.param[3] = DMA_WD0(fcp->isp_scdma);
4807                         mbs.param[6] = DMA_WD3(fcp->isp_scdma);
4808                         mbs.param[7] = DMA_WD2(fcp->isp_scdma);
4809
4810                         if (FC_SCRATCH_ACQUIRE(isp, chan)) {
4811                                 isp_prt(isp, ISP_LOGERR, sacq);
4812                                 break;
4813                         }
4814                         isp_put_24xx_abrt(isp, ab, fcp->isp_scratch);
4815                         ab2 = (isp24xx_abrt_t *) &((uint8_t *)fcp->isp_scratch)[QENTRY_LEN];
4816                         ab2->abrt_nphdl = 0xdeaf;
4817                         MEMORYBARRIER(isp, SYNC_SFORDEV, 0, 2 * QENTRY_LEN, chan);
4818                         isp_mboxcmd(isp, &mbs);
4819                         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
4820                                 FC_SCRATCH_RELEASE(isp, chan);
4821                                 break;
4822                         }
4823                         MEMORYBARRIER(isp, SYNC_SFORCPU, QENTRY_LEN, QENTRY_LEN, chan);
4824                         isp_get_24xx_abrt(isp, ab2, ab);
4825                         FC_SCRATCH_RELEASE(isp, chan);
4826                         if (ab->abrt_nphdl == ISP24XX_ABRT_OKAY) {
4827                                 return (0);
4828                         }
4829                         isp_prt(isp, ISP_LOGWARN, "Chan %d handle %d abort returned 0x%x", chan, tgt, ab->abrt_nphdl);
4830                         break;
4831                 } else if (IS_FC(isp)) {
4832                         if (ISP_CAP_SCCFW(isp)) {
4833                                 if (ISP_CAP_2KLOGIN(isp)) {
4834                                         mbs.param[1] = tgt;
4835                                 } else {
4836                                         mbs.param[1] = tgt << 8;
4837                                 }
4838                                 mbs.param[6] = XS_LUN(xs);
4839                         } else {
4840                                 mbs.param[1] = tgt << 8 | XS_LUN(xs);
4841                         }
4842                 } else {
4843                         mbs.param[1] = (chan << 15) | (tgt << 8) | XS_LUN(xs);
4844                 }
4845                 MBSINIT(&mbs, MBOX_ABORT, MBLOGALL & ~MBOX_COMMAND_ERROR, 0);
4846                 mbs.param[2] = handle;
4847                 isp_mboxcmd(isp, &mbs);
4848                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
4849                         break;
4850                 }
4851                 return (0);
4852
4853         case ISPCTL_UPDATE_PARAMS:
4854
4855                 va_start(ap, ctl);
4856                 chan = va_arg(ap, int);
4857                 va_end(ap);
4858                 isp_spi_update(isp, chan);
4859                 return (0);
4860
4861         case ISPCTL_FCLINK_TEST:
4862
4863                 if (IS_FC(isp)) {
4864                         int usdelay;
4865                         va_start(ap, ctl);
4866                         chan = va_arg(ap, int);
4867                         usdelay = va_arg(ap, int);
4868                         va_end(ap);
4869                         if (usdelay == 0) {
4870                                 usdelay =  250000;
4871                         }
4872                         return (isp_fclink_test(isp, chan, usdelay));
4873                 }
4874                 break;
4875
4876         case ISPCTL_SCAN_FABRIC:
4877
4878                 if (IS_FC(isp)) {
4879                         va_start(ap, ctl);
4880                         chan = va_arg(ap, int);
4881                         va_end(ap);
4882                         return (isp_scan_fabric(isp, chan));
4883                 }
4884                 break;
4885
4886         case ISPCTL_SCAN_LOOP:
4887
4888                 if (IS_FC(isp)) {
4889                         va_start(ap, ctl);
4890                         chan = va_arg(ap, int);
4891                         va_end(ap);
4892                         return (isp_scan_loop(isp, chan));
4893                 }
4894                 break;
4895
4896         case ISPCTL_PDB_SYNC:
4897
4898                 if (IS_FC(isp)) {
4899                         va_start(ap, ctl);
4900                         chan = va_arg(ap, int);
4901                         va_end(ap);
4902                         return (isp_pdb_sync(isp, chan));
4903                 }
4904                 break;
4905
4906         case ISPCTL_SEND_LIP:
4907
4908                 if (IS_FC(isp) && !IS_24XX(isp)) {
4909                         MBSINIT(&mbs, MBOX_INIT_LIP, MBLOGALL, 0);
4910                         if (ISP_CAP_2KLOGIN(isp)) {
4911                                 mbs.ibits = (1 << 10);
4912                         }
4913                         isp_mboxcmd(isp, &mbs);
4914                         if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
4915                                 return (0);
4916                         }
4917                 }
4918                 break;
4919
4920         case ISPCTL_GET_PDB:
4921                 if (IS_FC(isp)) {
4922                         isp_pdb_t *pdb;
4923                         va_start(ap, ctl);
4924                         chan = va_arg(ap, int);
4925                         tgt = va_arg(ap, int);
4926                         pdb = va_arg(ap, isp_pdb_t *);
4927                         va_end(ap);
4928                         return (isp_getpdb(isp, chan, tgt, pdb, 1));
4929                 }
4930                 break;
4931
4932         case ISPCTL_GET_NAMES:
4933         {
4934                 uint64_t *wwnn, *wwnp;
4935                 va_start(ap, ctl);
4936                 chan = va_arg(ap, int);
4937                 tgt = va_arg(ap, int);
4938                 wwnn = va_arg(ap, uint64_t *);
4939                 wwnp = va_arg(ap, uint64_t *);
4940                 va_end(ap);
4941                 if (wwnn == NULL && wwnp == NULL) {
4942                         break;
4943                 }
4944                 if (wwnn) {
4945                         *wwnn = isp_get_wwn(isp, chan, tgt, 1);
4946                         if (*wwnn == INI_NONE) {
4947                                 break;
4948                         }
4949                 }
4950                 if (wwnp) {
4951                         *wwnp = isp_get_wwn(isp, chan, tgt, 0);
4952                         if (*wwnp == INI_NONE) {
4953                                 break;
4954                         }
4955                 }
4956                 return (0);
4957         }
4958         case ISPCTL_RUN_MBOXCMD:
4959         {
4960                 va_start(ap, ctl);
4961                 mbr = va_arg(ap, mbreg_t *);
4962                 va_end(ap);
4963                 isp_mboxcmd(isp, mbr);
4964                 return (0);
4965         }
4966         case ISPCTL_PLOGX:
4967         {
4968                 isp_plcmd_t *p;
4969                 int r;
4970
4971                 va_start(ap, ctl);
4972                 p = va_arg(ap, isp_plcmd_t *);
4973                 va_end(ap);
4974
4975                 if ((p->flags & PLOGX_FLG_CMD_MASK) != PLOGX_FLG_CMD_PLOGI || (p->handle != NIL_HANDLE)) {
4976                         return (isp_plogx(isp, p->channel, p->handle, p->portid, p->flags, 0));
4977                 }
4978                 do {
4979                         p->handle = isp_nxt_handle(isp, p->channel, p->handle);
4980                         r = isp_plogx(isp, p->channel, p->handle, p->portid, p->flags, 0);
4981                         if ((r & 0xffff) == MBOX_PORT_ID_USED) {
4982                                 p->handle = r >> 16;
4983                                 r = 0;
4984                                 break;
4985                         }
4986                 } while ((r & 0xffff) == MBOX_LOOP_ID_USED);
4987                 return (r);
4988         }
4989         case ISPCTL_CHANGE_ROLE:
4990         {
4991                 int role, r;
4992
4993                 va_start(ap, ctl);
4994                 chan = va_arg(ap, int);
4995                 role = va_arg(ap, int);
4996                 va_end(ap);
4997                 if (IS_FC(isp)) {
4998                         r = isp_fc_change_role(isp, chan, role);
4999                 } else {
5000                         SDPARAM(isp, chan)->role = role;
5001                         r = 0;
5002                 }
5003                 return (r);
5004         }
5005         default:
5006                 isp_prt(isp, ISP_LOGERR, "Unknown Control Opcode 0x%x", ctl);
5007                 break;
5008
5009         }
5010         return (-1);
5011 }
5012
5013 /*
5014  * Interrupt Service Routine(s).
5015  *
5016  * External (OS) framework has done the appropriate locking,
5017  * and the locking will be held throughout this function.
5018  */
5019
5020 /*
5021  * Limit our stack depth by sticking with the max likely number
5022  * of completions on a request queue at any one time.
5023  */
5024 #ifndef MAX_REQUESTQ_COMPLETIONS
5025 #define MAX_REQUESTQ_COMPLETIONS        32
5026 #endif
5027
5028 void
5029 isp_intr(ispsoftc_t *isp, uint32_t isr, uint16_t sema, uint16_t mbox)
5030 {
5031         XS_T *complist[MAX_REQUESTQ_COMPLETIONS], *xs;
5032         uint32_t iptr, optr, junk;
5033         int i, nlooked = 0, ndone = 0, continuations_expected = 0;
5034         int etype, last_etype = 0;
5035
5036 again:
5037         /*
5038          * Is this a mailbox related interrupt?
5039          * The mailbox semaphore will be nonzero if so.
5040          */
5041         if (sema) {
5042  fmbox:
5043                 if (mbox & MBOX_COMMAND_COMPLETE) {
5044                         isp->isp_intmboxc++;
5045                         if (isp->isp_mboxbsy) {
5046                                 int obits = isp->isp_obits;
5047                                 isp->isp_mboxtmp[0] = mbox;
5048                                 for (i = 1; i < ISP_NMBOX(isp); i++) {
5049                                         if ((obits & (1 << i)) == 0) {
5050                                                 continue;
5051                                         }
5052                                         isp->isp_mboxtmp[i] = ISP_READ(isp, MBOX_OFF(i));
5053                                 }
5054                                 if (isp->isp_mbxwrk0) {
5055                                         if (isp_mbox_continue(isp) == 0) {
5056                                                 return;
5057                                         }
5058                                 }
5059                                 MBOX_NOTIFY_COMPLETE(isp);
5060                         } else {
5061                                 isp_prt(isp, ISP_LOGWARN, "mailbox cmd (0x%x) with no waiters", mbox);
5062                         }
5063                 } else {
5064                         i = IS_FC(isp)? isp_parse_async_fc(isp, mbox) : isp_parse_async(isp, mbox);
5065                         if (i < 0) {
5066                                 return;
5067                         }
5068                 }
5069                 if ((IS_FC(isp) && mbox != ASYNC_RIOZIO_STALL) || isp->isp_state != ISP_RUNSTATE) {
5070                         goto out;
5071                 }
5072         }
5073
5074         /*
5075          * We can't be getting this now.
5076          */
5077         if (isp->isp_state != ISP_RUNSTATE) {
5078                 /*
5079                  * This seems to happen to 23XX and 24XX cards- don't know why.
5080                  */
5081                  if (isp->isp_mboxbsy && isp->isp_lastmbxcmd == MBOX_ABOUT_FIRMWARE) {
5082                         goto fmbox;
5083                 }
5084                 isp_prt(isp, ISP_LOGINFO, "interrupt (ISR=%x SEMA=%x) when not ready", isr, sema);
5085                 /*
5086                  * Thank you very much!  *Burrrp*!
5087                  */
5088                 isp->isp_residx = ISP_READ(isp, isp->isp_respinrp);
5089                 isp->isp_resodx = isp->isp_residx;
5090                 ISP_WRITE(isp, isp->isp_respoutrp, isp->isp_resodx);
5091                 if (IS_24XX(isp)) {
5092                         ISP_DISABLE_INTS(isp);
5093                 }
5094                 goto out;
5095         }
5096
5097 #ifdef  ISP_TARGET_MODE
5098         /*
5099          * Check for ATIO Queue entries.
5100          */
5101         if (IS_24XX(isp) &&
5102             ((isr & BIU2400_R2HST_ISTAT_MASK) == ISP2400R2HST_ATIO_RSPQ_UPDATE ||
5103              (isr & BIU2400_R2HST_ISTAT_MASK) == ISP2400R2HST_ATIO_RQST_UPDATE)) {
5104                 iptr = ISP_READ(isp, BIU2400_ATIO_RSPINP);
5105                 optr = isp->isp_atioodx;
5106
5107                 while (optr != iptr) {
5108                         uint8_t qe[QENTRY_LEN];
5109                         isphdr_t *hp;
5110                         uint32_t oop;
5111                         void *addr;
5112
5113                         oop = optr;
5114                         MEMORYBARRIER(isp, SYNC_ATIOQ, oop, QENTRY_LEN, -1);
5115                         addr = ISP_QUEUE_ENTRY(isp->isp_atioq, oop);
5116                         isp_get_hdr(isp, addr, (isphdr_t *)qe);
5117                         hp = (isphdr_t *)qe;
5118                         switch (hp->rqs_entry_type) {
5119                         case RQSTYPE_NOTIFY:
5120                         case RQSTYPE_ATIO:
5121                                 (void) isp_target_notify(isp, addr, &oop);
5122                                 break;
5123                         default:
5124                                 isp_print_qentry(isp, "?ATIOQ entry?", oop, addr);
5125                                 break;
5126                         }
5127                         optr = ISP_NXT_QENTRY(oop, RESULT_QUEUE_LEN(isp));
5128                 }
5129                 if (isp->isp_atioodx != optr) {
5130                         ISP_WRITE(isp, BIU2400_ATIO_RSPOUTP, optr);
5131                         isp->isp_atioodx = optr;
5132                 }
5133         }
5134 #endif
5135
5136         /*
5137          * Get the current Response Queue Out Pointer.
5138          *
5139          * If we're a 2300 or 2400, we can ask what hardware what it thinks.
5140          */
5141 #if 0
5142         if (IS_23XX(isp) || IS_24XX(isp)) {
5143                 optr = ISP_READ(isp, isp->isp_respoutrp);
5144                 /*
5145                  * Debug: to be taken out eventually
5146                  */
5147                 if (isp->isp_resodx != optr) {
5148                         isp_prt(isp, ISP_LOGINFO, "isp_intr: hard optr=%x, soft optr %x", optr, isp->isp_resodx);
5149                         isp->isp_resodx = optr;
5150                 }
5151         } else
5152 #endif
5153                 optr = isp->isp_resodx;
5154
5155         /*
5156          * You *must* read the Response Queue In Pointer
5157          * prior to clearing the RISC interrupt.
5158          *
5159          * Debounce the 2300 if revision less than 2.
5160          */
5161         if (IS_2100(isp) || (IS_2300(isp) && isp->isp_revision < 2)) {
5162                 i = 0;
5163                 do {
5164                         iptr = ISP_READ(isp, isp->isp_respinrp);
5165                         junk = ISP_READ(isp, isp->isp_respinrp);
5166                 } while (junk != iptr && ++i < 1000);
5167
5168                 if (iptr != junk) {
5169                         isp_prt(isp, ISP_LOGWARN, "Response Queue Out Pointer Unstable (%x, %x)", iptr, junk);
5170                         goto out;
5171                 }
5172         } else {
5173                 iptr = ISP_READ(isp, isp->isp_respinrp);
5174         }
5175
5176         if (optr == iptr && sema == 0) {
5177                 /*
5178                  * There are a lot of these- reasons unknown- mostly on
5179                  * faster Alpha machines.
5180                  *
5181                  * I tried delaying after writing HCCR_CMD_CLEAR_RISC_INT to
5182                  * make sure the old interrupt went away (to avoid 'ringing'
5183                  * effects), but that didn't stop this from occurring.
5184                  */
5185                 if (IS_24XX(isp)) {
5186                         junk = 0;
5187                 } else if (IS_23XX(isp)) {
5188                         ISP_DELAY(100);
5189                         iptr = ISP_READ(isp, isp->isp_respinrp);
5190                         junk = ISP_READ(isp, BIU_R2HSTSLO);
5191                 } else {
5192                         junk = ISP_READ(isp, BIU_ISR);
5193                 }
5194                 if (optr == iptr) {
5195                         if (IS_23XX(isp) || IS_24XX(isp)) {
5196                                 ;
5197                         } else {
5198                                 sema = ISP_READ(isp, BIU_SEMA);
5199                                 mbox = ISP_READ(isp, OUTMAILBOX0);
5200                                 if ((sema & 0x3) && (mbox & 0x8000)) {
5201                                         goto again;
5202                                 }
5203                         }
5204                         isp->isp_intbogus++;
5205                         isp_prt(isp, ISP_LOGDEBUG1, "bogus intr- isr %x (%x) iptr %x optr %x", isr, junk, iptr, optr);
5206                 }
5207         }
5208         isp->isp_residx = iptr;
5209
5210         while (optr != iptr) {
5211                 uint8_t qe[QENTRY_LEN];
5212                 ispstatusreq_t *sp = (ispstatusreq_t *) qe;
5213                 isphdr_t *hp;
5214                 int buddaboom, scsi_status, completion_status;
5215                 int req_status_flags, req_state_flags;
5216                 uint8_t *snsp, *resp;
5217                 uint32_t rlen, slen, totslen;
5218                 long resid;
5219                 uint16_t oop;
5220
5221                 hp = (isphdr_t *) ISP_QUEUE_ENTRY(isp->isp_result, optr);
5222                 oop = optr;
5223                 optr = ISP_NXT_QENTRY(optr, RESULT_QUEUE_LEN(isp));
5224                 nlooked++;
5225  read_again:
5226                 buddaboom = req_status_flags = req_state_flags = 0;
5227                 resid = 0L;
5228
5229                 /*
5230                  * Synchronize our view of this response queue entry.
5231                  */
5232                 MEMORYBARRIER(isp, SYNC_RESULT, oop, QENTRY_LEN, -1);
5233                 isp_get_hdr(isp, hp, &sp->req_header);
5234                 etype = sp->req_header.rqs_entry_type;
5235
5236                 if (IS_24XX(isp) && etype == RQSTYPE_RESPONSE) {
5237                         isp24xx_statusreq_t *sp2 = (isp24xx_statusreq_t *)qe;
5238                         isp_get_24xx_response(isp, (isp24xx_statusreq_t *)hp, sp2);
5239                         if (isp->isp_dblev & ISP_LOGDEBUG1) {
5240                                 isp_print_bytes(isp, "Response Queue Entry", QENTRY_LEN, sp2);
5241                         }
5242                         scsi_status = sp2->req_scsi_status;
5243                         completion_status = sp2->req_completion_status;
5244                         if ((scsi_status & 0xff) != 0)
5245                                 req_state_flags = RQSF_GOT_STATUS;
5246                         else
5247                                 req_state_flags = 0;
5248                         resid = sp2->req_resid;
5249                 } else if (etype == RQSTYPE_RESPONSE) {
5250                         isp_get_response(isp, (ispstatusreq_t *) hp, sp);
5251                         if (isp->isp_dblev & ISP_LOGDEBUG1) {
5252                                 isp_print_bytes(isp, "Response Queue Entry", QENTRY_LEN, sp);
5253                         }
5254                         scsi_status = sp->req_scsi_status;
5255                         completion_status = sp->req_completion_status;
5256                         req_status_flags = sp->req_status_flags;
5257                         req_state_flags = sp->req_state_flags;
5258                         resid = sp->req_resid;
5259                 } else if (etype == RQSTYPE_RIO1) {
5260                         isp_rio1_t *rio = (isp_rio1_t *) qe;
5261                         isp_get_rio1(isp, (isp_rio1_t *) hp, rio);
5262                         if (isp->isp_dblev & ISP_LOGDEBUG1) {
5263                                 isp_print_bytes(isp, "Response Queue Entry", QENTRY_LEN, rio);
5264                         }
5265                         for (i = 0; i < rio->req_header.rqs_seqno; i++) {
5266                                 isp_fastpost_complete(isp, rio->req_handles[i]);
5267                         }
5268                         if (isp->isp_fpcchiwater < rio->req_header.rqs_seqno) {
5269                                 isp->isp_fpcchiwater = rio->req_header.rqs_seqno;
5270                         }
5271                         ISP_MEMZERO(hp, QENTRY_LEN);    /* PERF */
5272                         last_etype = etype;
5273                         continue;
5274                 } else if (etype == RQSTYPE_RIO2) {
5275                         isp_prt(isp, ISP_LOGERR, "dropping RIO2 response");
5276                         ISP_MEMZERO(hp, QENTRY_LEN);    /* PERF */
5277                         last_etype = etype;
5278                         continue;
5279                 } else if (etype == RQSTYPE_STATUS_CONT) {
5280                         isp_get_cont_response(isp, (ispstatus_cont_t *) hp, (ispstatus_cont_t *) sp);
5281                         if (last_etype == RQSTYPE_RESPONSE && continuations_expected && ndone > 0 && (xs = complist[ndone-1]) != NULL) {
5282                                 ispstatus_cont_t *scp = (ispstatus_cont_t *) sp;
5283                                 XS_SENSE_APPEND(xs, scp->req_sense_data, sizeof (scp->req_sense_data));
5284                                 isp_prt(isp, ISP_LOGDEBUG0|ISP_LOG_CWARN, "%d more Status Continuations expected", --continuations_expected);
5285                         } else {
5286                                 isp_prt(isp, ISP_LOG_WARN1, "Ignored Continuation Response");
5287                         }
5288                         ISP_MEMZERO(hp, QENTRY_LEN);    /* PERF */
5289                         continue;
5290                 } else {
5291                         /*
5292                          * Somebody reachable via isp_handle_other_response
5293                          * may have updated the response queue pointers for
5294                          * us, so we reload our goal index.
5295                          */
5296                         int r;
5297                         uint32_t tsto = oop;
5298                         r = isp_handle_other_response(isp, etype, hp, &tsto);
5299                         if (r < 0) {
5300                                 goto read_again;
5301                         }
5302                         /*
5303                          * If somebody updated the output pointer, then reset
5304                          * optr to be one more than the updated amount.
5305                          */
5306                         while (tsto != oop) {
5307                                 optr = ISP_NXT_QENTRY(tsto, RESULT_QUEUE_LEN(isp));
5308                         }
5309                         if (r > 0) {
5310                                 ISP_MEMZERO(hp, QENTRY_LEN);    /* PERF */
5311                                 last_etype = etype;
5312                                 continue;
5313                         }
5314
5315                         /*
5316                          * After this point, we'll just look at the header as
5317                          * we don't know how to deal with the rest of the
5318                          * response.
5319                          */
5320
5321                         /*
5322                          * It really has to be a bounced request just copied
5323                          * from the request queue to the response queue. If
5324                          * not, something bad has happened.
5325                          */
5326                         if (etype != RQSTYPE_REQUEST) {
5327                                 isp_prt(isp, ISP_LOGERR, notresp, etype, oop, optr, nlooked);
5328                                 isp_print_bytes(isp, "Request Queue Entry", QENTRY_LEN, sp);
5329                                 ISP_MEMZERO(hp, QENTRY_LEN);    /* PERF */
5330                                 last_etype = etype;
5331                                 continue;
5332                         }
5333                         buddaboom = 1;
5334                         scsi_status = sp->req_scsi_status;
5335                         completion_status = sp->req_completion_status;
5336                         req_status_flags = sp->req_status_flags;
5337                         req_state_flags = sp->req_state_flags;
5338                         resid = sp->req_resid;
5339                 }
5340
5341                 if (sp->req_header.rqs_flags & RQSFLAG_MASK) {
5342                         if (sp->req_header.rqs_flags & RQSFLAG_CONTINUATION) {
5343                                 isp_print_bytes(isp, "unexpected continuation segment", QENTRY_LEN, sp);
5344                                 last_etype = etype;
5345                                 continue;
5346                         }
5347                         if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
5348                                 isp_prt(isp, ISP_LOG_WARN1, "internal queues full");
5349                                 /*
5350                                  * We'll synthesize a QUEUE FULL message below.
5351                                  */
5352                         }
5353                         if (sp->req_header.rqs_flags & RQSFLAG_BADHEADER) {
5354                                 isp_print_bytes(isp, "bad header flag", QENTRY_LEN, sp);
5355                                 buddaboom++;
5356                         }
5357                         if (sp->req_header.rqs_flags & RQSFLAG_BADPACKET) {
5358                                 isp_print_bytes(isp, "bad request packet", QENTRY_LEN, sp);
5359                                 buddaboom++;
5360                         }
5361                         if (sp->req_header.rqs_flags & RQSFLAG_BADCOUNT) {
5362                                 isp_print_bytes(isp, "invalid entry count", QENTRY_LEN, sp);
5363                                 buddaboom++;
5364                         }
5365                         if (sp->req_header.rqs_flags & RQSFLAG_BADORDER) {
5366                                 isp_print_bytes(isp, "invalid IOCB ordering", QENTRY_LEN, sp);
5367                                 last_etype = etype;
5368                                 continue;
5369                         }
5370                 }
5371
5372                 if (!ISP_VALID_HANDLE(isp, sp->req_handle)) {
5373                         isp_prt(isp, ISP_LOGERR, "bad request handle 0x%x (iocb type 0x%x)", sp->req_handle, etype);
5374                         ISP_MEMZERO(hp, QENTRY_LEN);    /* PERF */
5375                         last_etype = etype;
5376                         continue;
5377                 }
5378                 xs = isp_find_xs(isp, sp->req_handle);
5379                 if (xs == NULL) {
5380                         uint8_t ts = completion_status & 0xff;
5381                         /*
5382                          * Only whine if this isn't the expected fallout of
5383                          * aborting the command or resetting the target.
5384                          */
5385                         if (etype != RQSTYPE_RESPONSE) {
5386                                 isp_prt(isp, ISP_LOGERR, "cannot find handle 0x%x (type 0x%x)", sp->req_handle, etype);
5387                         } else if (ts != RQCS_ABORTED && ts != RQCS_RESET_OCCURRED) {
5388                                 isp_prt(isp, ISP_LOGERR, "cannot find handle 0x%x (status 0x%x)", sp->req_handle, ts);
5389                         }
5390                         ISP_MEMZERO(hp, QENTRY_LEN);    /* PERF */
5391                         last_etype = etype;
5392                         continue;
5393                 }
5394                 if (req_status_flags & RQSTF_BUS_RESET) {
5395                         isp_prt(isp, ISP_LOG_WARN1, "%d.%d.%d bus was reset", XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
5396                         XS_SETERR(xs, HBA_BUSRESET);
5397                         ISP_SET_SENDMARKER(isp, XS_CHANNEL(xs), 1);
5398                 }
5399                 if (buddaboom) {
5400                         isp_prt(isp, ISP_LOG_WARN1, "%d.%d.%d buddaboom", XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs));
5401                         XS_SETERR(xs, HBA_BOTCH);
5402                 }
5403
5404                 resp = NULL;
5405                 rlen = 0;
5406                 snsp = NULL;
5407                 totslen = slen = 0;
5408                 if (IS_24XX(isp) && (scsi_status & (RQCS_RV|RQCS_SV)) != 0) {
5409                         resp = ((isp24xx_statusreq_t *)sp)->req_rsp_sense;
5410                         rlen = ((isp24xx_statusreq_t *)sp)->req_response_len;
5411                 } else if (IS_FC(isp) && (scsi_status & RQCS_RV) != 0) {
5412                         resp = sp->req_response;
5413                         rlen = sp->req_response_len;
5414                 }
5415                 if (IS_FC(isp) && (scsi_status & RQCS_SV) != 0) {
5416                         /*
5417                          * Fibre Channel F/W doesn't say we got status
5418                          * if there's Sense Data instead. I guess they
5419                          * think it goes w/o saying.
5420                          */
5421                         req_state_flags |= RQSF_GOT_STATUS|RQSF_GOT_SENSE;
5422                         if (IS_24XX(isp)) {
5423                                 snsp = ((isp24xx_statusreq_t *)sp)->req_rsp_sense;
5424                                 snsp += rlen;
5425                                 totslen = ((isp24xx_statusreq_t *)sp)->req_sense_len;
5426                                 slen = (sizeof (((isp24xx_statusreq_t *)sp)->req_rsp_sense)) - rlen;
5427                                 if (totslen < slen)
5428                                         slen = totslen; 
5429                         } else {
5430                                 snsp = sp->req_sense_data;
5431                                 totslen = sp->req_sense_len;
5432                                 slen = sizeof (sp->req_sense_data);
5433                                 if (totslen < slen)
5434                                         slen = totslen;
5435                         }
5436                 } else if (IS_SCSI(isp) && (req_state_flags & RQSF_GOT_SENSE)) {
5437                         snsp = sp->req_sense_data;
5438                         totslen = sp->req_sense_len;
5439                         slen = sizeof (sp->req_sense_data);
5440                         if (totslen < slen)
5441                                 slen = totslen;
5442                 }
5443                 if (req_state_flags & RQSF_GOT_STATUS) {
5444                         *XS_STSP(xs) = scsi_status & 0xff;
5445                 }
5446
5447                 switch (etype) {
5448                 case RQSTYPE_RESPONSE:
5449                         if (resp && rlen >= 4 && resp[FCP_RSPNS_CODE_OFFSET] != 0) {
5450                                 const char *ptr;
5451                                 char lb[64];
5452                                 const char *rnames[6] = {
5453                                         "Task Management Function Done",
5454                                         "Data Length Differs From Burst Length",
5455                                         "Invalid FCP Cmnd",
5456                                         "FCP DATA RO mismatch with FCP DATA_XFR_RDY RO",
5457                                         "Task Management Function Rejected",
5458                                         "Task Management Function Failed",
5459                                 };
5460                                 if (resp[FCP_RSPNS_CODE_OFFSET] > 5) {
5461                                         ISP_SNPRINTF(lb, sizeof lb, "Unknown FCP Response Code 0x%x", resp[FCP_RSPNS_CODE_OFFSET]);
5462                                         ptr = lb;
5463                                 } else {
5464                                         ptr = rnames[resp[FCP_RSPNS_CODE_OFFSET]];
5465                                 }
5466                                 isp_xs_prt(isp, xs, ISP_LOGWARN, "FCP RESPONSE, LENGTH %u: %s CDB0=0x%02x", rlen, ptr, XS_CDBP(xs)[0] & 0xff);
5467                                 if (resp[FCP_RSPNS_CODE_OFFSET] != 0) {
5468                                         XS_SETERR(xs, HBA_BOTCH);
5469                                 }
5470                         }
5471                         if (IS_24XX(isp)) {
5472                                 isp_parse_status_24xx(isp, (isp24xx_statusreq_t *)sp, xs, &resid);
5473                         } else {
5474                                 isp_parse_status(isp, (void *)sp, xs, &resid);
5475                         }
5476                         if ((XS_NOERR(xs) || XS_ERR(xs) == HBA_NOERROR) && (*XS_STSP(xs) == SCSI_BUSY)) {
5477                                 XS_SETERR(xs, HBA_TGTBSY);
5478                         }
5479                         if (IS_SCSI(isp)) {
5480                                 XS_SET_RESID(xs, resid);
5481                                 /*
5482                                  * A new synchronous rate was negotiated for
5483                                  * this target. Mark state such that we'll go
5484                                  * look up that which has changed later.
5485                                  */
5486                                 if (req_status_flags & RQSTF_NEGOTIATION) {
5487                                         int t = XS_TGT(xs);
5488                                         sdparam *sdp = SDPARAM(isp, XS_CHANNEL(xs));
5489                                         sdp->isp_devparam[t].dev_refresh = 1;
5490                                         sdp->update = 1;
5491                                 }
5492                         } else {
5493                                 if (req_status_flags & RQSF_XFER_COMPLETE) {
5494                                         XS_SET_RESID(xs, 0);
5495                                 } else if (scsi_status & RQCS_RESID) {
5496                                         XS_SET_RESID(xs, resid);
5497                                 } else {
5498                                         XS_SET_RESID(xs, 0);
5499                                 }
5500                         }
5501                         if (snsp && slen) {
5502                                 if (totslen > slen) {
5503                                         continuations_expected += ((totslen - slen + QENTRY_LEN - 5) / (QENTRY_LEN - 4));
5504                                         if (ndone > (MAX_REQUESTQ_COMPLETIONS - continuations_expected - 1)) {
5505                                                 /* we'll lose some stats, but that's a small price to pay */
5506                                                 for (i = 0; i < ndone; i++) {
5507                                                         if (complist[i]) {
5508                                                                 isp->isp_rsltccmplt++;
5509                                                                 isp_done(complist[i]);
5510                                                         }
5511                                                 }
5512                                                 ndone = 0;
5513                                         }
5514                                         isp_prt(isp, ISP_LOGDEBUG0|ISP_LOG_CWARN, "Expecting %d more Status Continuations for total sense length of %u",
5515                                             continuations_expected, totslen);
5516                                 }
5517                                 XS_SAVE_SENSE(xs, snsp, totslen, slen);
5518                         } else if ((req_status_flags & RQSF_GOT_STATUS) && (scsi_status & 0xff) == SCSI_CHECK && IS_FC(isp)) {
5519                                 isp_prt(isp, ISP_LOGWARN, "CHECK CONDITION w/o sense data for CDB=0x%x", XS_CDBP(xs)[0] & 0xff);
5520                                 isp_print_bytes(isp, "CC with no Sense", QENTRY_LEN, qe);
5521                         }
5522                         isp_prt(isp, ISP_LOGDEBUG2, "asked for %ld got raw resid %ld settled for %ld", (long) XS_XFRLEN(xs), resid, (long) XS_GET_RESID(xs));
5523                         break;
5524                 case RQSTYPE_REQUEST:
5525                 case RQSTYPE_A64:
5526                 case RQSTYPE_T2RQS:
5527                 case RQSTYPE_T3RQS:
5528                 case RQSTYPE_T7RQS:
5529                         if (!IS_24XX(isp) && (sp->req_header.rqs_flags & RQSFLAG_FULL)) {
5530                                 /*
5531                                  * Force Queue Full status.
5532                                  */
5533                                 *XS_STSP(xs) = SCSI_QFULL;
5534                                 XS_SETERR(xs, HBA_NOERROR);
5535                         } else if (XS_NOERR(xs)) {
5536                                 isp_prt(isp, ISP_LOG_WARN1, "%d.%d.%d badness at %s:%u", XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), __func__, __LINE__);
5537                                 XS_SETERR(xs, HBA_BOTCH);
5538                         }
5539                         XS_SET_RESID(xs, XS_XFRLEN(xs));
5540                         break;
5541                 default:
5542                         isp_print_bytes(isp, "Unhandled Response Type", QENTRY_LEN, qe);
5543                         if (XS_NOERR(xs)) {
5544                                 XS_SETERR(xs, HBA_BOTCH);
5545                         }
5546                         break;
5547                 }
5548
5549                 /*
5550                  * Free any DMA resources. As a side effect, this may
5551                  * also do any cache flushing necessary for data coherence.
5552                  */
5553                 if (XS_XFRLEN(xs)) {
5554                         ISP_DMAFREE(isp, xs, sp->req_handle);
5555                 }
5556                 isp_destroy_handle(isp, sp->req_handle);
5557
5558                 if (isp->isp_nactive > 0) {
5559                     isp->isp_nactive--;
5560                 }
5561                 complist[ndone++] = xs; /* defer completion call until later */
5562                 ISP_MEMZERO(hp, QENTRY_LEN);    /* PERF */
5563                 last_etype = etype;
5564                 if (ndone == MAX_REQUESTQ_COMPLETIONS) {
5565                         break;
5566                 }
5567         }
5568
5569         /*
5570          * If we looked at any commands, then it's valid to find out
5571          * what the outpointer is. It also is a trigger to update the
5572          * ISP's notion of what we've seen so far.
5573          */
5574         if (nlooked) {
5575                 ISP_WRITE(isp, isp->isp_respoutrp, optr);
5576                 isp->isp_resodx = optr;
5577                 if (isp->isp_rscchiwater < ndone)
5578                         isp->isp_rscchiwater = ndone;
5579         }
5580
5581 out:
5582
5583         if (IS_24XX(isp)) {
5584                 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
5585         } else {
5586                 ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
5587                 ISP_WRITE(isp, BIU_SEMA, 0);
5588         }
5589
5590         for (i = 0; i < ndone; i++) {
5591                 xs = complist[i];
5592                 if (xs) {
5593                         if (((isp->isp_dblev & (ISP_LOGDEBUG1|ISP_LOGDEBUG2|ISP_LOGDEBUG3))) ||
5594                             ((isp->isp_dblev & (ISP_LOGDEBUG0|ISP_LOG_CWARN) && ((!XS_NOERR(xs)) || (*XS_STSP(xs) != SCSI_GOOD))))) {
5595                                 isp_prt_endcmd(isp, xs);
5596                         }
5597                         isp->isp_rsltccmplt++;
5598                         isp_done(xs);
5599                 }
5600         }
5601 }
5602
5603 /*
5604  * Support routines.
5605  */
5606
5607 void
5608 isp_prt_endcmd(ispsoftc_t *isp, XS_T *xs)
5609 {
5610         char cdbstr[16 * 5 + 1];
5611         int i, lim;
5612
5613         lim = XS_CDBLEN(xs) > 16? 16 : XS_CDBLEN(xs);
5614         ISP_SNPRINTF(cdbstr, sizeof (cdbstr), "0x%02x ", XS_CDBP(xs)[0]);
5615         for (i = 1; i < lim; i++) {
5616                 ISP_SNPRINTF(cdbstr, sizeof (cdbstr), "%s0x%02x ", cdbstr, XS_CDBP(xs)[i]);
5617         }
5618         if (XS_SENSE_VALID(xs)) {
5619                 isp_xs_prt(isp, xs, ISP_LOGALL, "FIN dl%d resid %ld CDB=%s SenseLength=%u/%u KEY/ASC/ASCQ=0x%02x/0x%02x/0x%02x",
5620                     XS_XFRLEN(xs), (long) XS_GET_RESID(xs), cdbstr, XS_CUR_SNSLEN(xs), XS_TOT_SNSLEN(xs), XS_SNSKEY(xs), XS_SNSASC(xs), XS_SNSASCQ(xs));
5621         } else {
5622                 isp_xs_prt(isp, xs, ISP_LOGALL, "FIN dl%d resid %ld CDB=%s STS 0x%x XS_ERR=0x%x", XS_XFRLEN(xs), (long) XS_GET_RESID(xs), cdbstr, *XS_STSP(xs), XS_ERR(xs));
5623         }
5624 }
5625
5626 /*
5627  * Parse an ASYNC mailbox complete
5628  *
5629  * Return non-zero if the event has been acknowledged.
5630  */
5631 static int
5632 isp_parse_async(ispsoftc_t *isp, uint16_t mbox)
5633 {
5634         int acked = 0;
5635         uint32_t h1 = 0, h2 = 0;
5636         uint16_t chan = 0;
5637
5638         /*
5639          * Pick up the channel, but not if this is a ASYNC_RIO32_2,
5640          * where Mailboxes 6/7 have the second handle.
5641          */
5642         if (mbox != ASYNC_RIO32_2) {
5643                 if (IS_DUALBUS(isp)) {
5644                         chan = ISP_READ(isp, OUTMAILBOX6);
5645                 }
5646         }
5647         isp_prt(isp, ISP_LOGDEBUG2, "Async Mbox 0x%x", mbox);
5648
5649         switch (mbox) {
5650         case ASYNC_BUS_RESET:
5651                 ISP_SET_SENDMARKER(isp, chan, 1);
5652 #ifdef  ISP_TARGET_MODE
5653                 if (isp_target_async(isp, chan, mbox)) {
5654                         acked = 1;
5655                 }
5656 #endif
5657                 isp_async(isp, ISPASYNC_BUS_RESET, chan);
5658                 break;
5659         case ASYNC_SYSTEM_ERROR:
5660                 isp->isp_dead = 1;
5661                 isp->isp_state = ISP_CRASHED;
5662                 /*
5663                  * Were we waiting for a mailbox command to complete?
5664                  * If so, it's dead, so wake up the waiter.
5665                  */
5666                 if (isp->isp_mboxbsy) {
5667                         isp->isp_obits = 1;
5668                         isp->isp_mboxtmp[0] = MBOX_HOST_INTERFACE_ERROR;
5669                         MBOX_NOTIFY_COMPLETE(isp);
5670                 }
5671                 /*
5672                  * It's up to the handler for isp_async to reinit stuff and
5673                  * restart the firmware
5674                  */
5675                 isp_async(isp, ISPASYNC_FW_CRASH);
5676                 acked = 1;
5677                 break;
5678
5679         case ASYNC_RQS_XFER_ERR:
5680                 isp_prt(isp, ISP_LOGERR, "Request Queue Transfer Error");
5681                 break;
5682
5683         case ASYNC_RSP_XFER_ERR:
5684                 isp_prt(isp, ISP_LOGERR, "Response Queue Transfer Error");
5685                 break;
5686
5687         case ASYNC_QWAKEUP:
5688                 /*
5689                  * We've just been notified that the Queue has woken up.
5690                  * We don't need to be chatty about this- just unlatch things
5691                  * and move on.
5692                  */
5693                 mbox = ISP_READ(isp, isp->isp_rqstoutrp);
5694                 break;
5695
5696         case ASYNC_TIMEOUT_RESET:
5697                 isp_prt(isp, ISP_LOGWARN, "timeout initiated SCSI bus reset of chan %d", chan);
5698                 ISP_SET_SENDMARKER(isp, chan, 1);
5699 #ifdef  ISP_TARGET_MODE
5700                 if (isp_target_async(isp, chan, mbox)) {
5701                         acked = 1;
5702                 }
5703 #endif
5704                 break;
5705
5706         case ASYNC_DEVICE_RESET:
5707                 isp_prt(isp, ISP_LOGINFO, "device reset on chan %d", chan);
5708                 ISP_SET_SENDMARKER(isp, chan, 1);
5709 #ifdef  ISP_TARGET_MODE
5710                 if (isp_target_async(isp, chan, mbox)) {
5711                         acked = 1;
5712                 }
5713 #endif
5714                 break;
5715
5716         case ASYNC_EXTMSG_UNDERRUN:
5717                 isp_prt(isp, ISP_LOGWARN, "extended message underrun");
5718                 break;
5719
5720         case ASYNC_SCAM_INT:
5721                 isp_prt(isp, ISP_LOGINFO, "SCAM interrupt");
5722                 break;
5723
5724         case ASYNC_HUNG_SCSI:
5725                 isp_prt(isp, ISP_LOGERR, "stalled SCSI Bus after DATA Overrun");
5726                 /* XXX: Need to issue SCSI reset at this point */
5727                 break;
5728
5729         case ASYNC_KILLED_BUS:
5730                 isp_prt(isp, ISP_LOGERR, "SCSI Bus reset after DATA Overrun");
5731                 break;
5732
5733         case ASYNC_BUS_TRANSIT:
5734                 mbox = ISP_READ(isp, OUTMAILBOX2);
5735                 switch (mbox & SXP_PINS_MODE_MASK) {
5736                 case SXP_PINS_LVD_MODE:
5737                         isp_prt(isp, ISP_LOGINFO, "Transition to LVD mode");
5738                         SDPARAM(isp, chan)->isp_diffmode = 0;
5739                         SDPARAM(isp, chan)->isp_ultramode = 0;
5740                         SDPARAM(isp, chan)->isp_lvdmode = 1;
5741                         break;
5742                 case SXP_PINS_HVD_MODE:
5743                         isp_prt(isp, ISP_LOGINFO,
5744                             "Transition to Differential mode");
5745                         SDPARAM(isp, chan)->isp_diffmode = 1;
5746                         SDPARAM(isp, chan)->isp_ultramode = 0;
5747                         SDPARAM(isp, chan)->isp_lvdmode = 0;
5748                         break;
5749                 case SXP_PINS_SE_MODE:
5750                         isp_prt(isp, ISP_LOGINFO,
5751                             "Transition to Single Ended mode");
5752                         SDPARAM(isp, chan)->isp_diffmode = 0;
5753                         SDPARAM(isp, chan)->isp_ultramode = 1;
5754                         SDPARAM(isp, chan)->isp_lvdmode = 0;
5755                         break;
5756                 default:
5757                         isp_prt(isp, ISP_LOGWARN,
5758                             "Transition to Unknown Mode 0x%x", mbox);
5759                         break;
5760                 }
5761                 /*
5762                  * XXX: Set up to renegotiate again!
5763                  */
5764                 /* Can only be for a 1080... */
5765                 ISP_SET_SENDMARKER(isp, chan, 1);
5766                 break;
5767
5768         case ASYNC_CMD_CMPLT:
5769         case ASYNC_RIO32_1:
5770                 if (!IS_ULTRA3(isp)) {
5771                         isp_prt(isp, ISP_LOGERR, "unexpected fast posting completion");
5772                         break;
5773                 }
5774                 /* FALLTHROUGH */
5775                 h1 = (ISP_READ(isp, OUTMAILBOX2) << 16) | ISP_READ(isp, OUTMAILBOX1);
5776                 break;
5777
5778         case ASYNC_RIO32_2:
5779                 h1 = (ISP_READ(isp, OUTMAILBOX2) << 16) | ISP_READ(isp, OUTMAILBOX1);
5780                 h2 = (ISP_READ(isp, OUTMAILBOX7) << 16) | ISP_READ(isp, OUTMAILBOX6);
5781                 break;
5782
5783         case ASYNC_RIO16_5:
5784         case ASYNC_RIO16_4:
5785         case ASYNC_RIO16_3:
5786         case ASYNC_RIO16_2:
5787         case ASYNC_RIO16_1:
5788                 isp_prt(isp, ISP_LOGERR, "unexpected 16 bit RIO handle");
5789                 break;
5790         default:
5791                 isp_prt(isp, ISP_LOGWARN, "%s: unhandled async code 0x%x", __func__, mbox);
5792                 break;
5793         }
5794
5795         if (h1 || h2) {
5796                 isp_prt(isp, ISP_LOGDEBUG3, "fast post/rio completion of 0x%08x", h1);
5797                 isp_fastpost_complete(isp, h1);
5798                 if (h2) {
5799                         isp_prt(isp, ISP_LOGDEBUG3, "fast post/rio completion of 0x%08x", h2);
5800                         isp_fastpost_complete(isp, h2);
5801                         if (isp->isp_fpcchiwater < 2) {
5802                                 isp->isp_fpcchiwater = 2;
5803                         }
5804                 } else {
5805                         if (isp->isp_fpcchiwater < 1) {
5806                                 isp->isp_fpcchiwater = 1;
5807                         }
5808                 }
5809         } else {
5810                 isp->isp_intoasync++;
5811         }
5812         return (acked);
5813 }
5814
5815 #define GET_24XX_BUS(isp, chan, msg)                                                                            \
5816         if (IS_24XX(isp)) {                                                                                     \
5817                 chan = ISP_READ(isp, OUTMAILBOX3) & 0xff;                                                       \
5818                 if (chan >= isp->isp_nchan) {                                                                   \
5819                         isp_prt(isp, ISP_LOGERR, "bogus channel %u for %s at line %d",  chan, msg, __LINE__);   \
5820                         break;                                                                                  \
5821                 }                                                                                               \
5822         }
5823
5824
5825 static int
5826 isp_parse_async_fc(ispsoftc_t *isp, uint16_t mbox)
5827 {
5828         int acked = 0;
5829         uint16_t chan;
5830
5831         if (IS_DUALBUS(isp)) {
5832                 chan = ISP_READ(isp, OUTMAILBOX6);
5833         } else {
5834                 chan = 0;
5835         }
5836         isp_prt(isp, ISP_LOGDEBUG2, "Async Mbox 0x%x", mbox);
5837
5838         switch (mbox) {
5839         case ASYNC_SYSTEM_ERROR:
5840                 isp->isp_dead = 1;
5841                 isp->isp_state = ISP_CRASHED;
5842                 FCPARAM(isp, chan)->isp_loopstate = LOOP_NIL;
5843                 FCPARAM(isp, chan)->isp_fwstate = FW_CONFIG_WAIT;
5844                 /*
5845                  * Were we waiting for a mailbox command to complete?
5846                  * If so, it's dead, so wake up the waiter.
5847                  */
5848                 if (isp->isp_mboxbsy) {
5849                         isp->isp_obits = 1;
5850                         isp->isp_mboxtmp[0] = MBOX_HOST_INTERFACE_ERROR;
5851                         MBOX_NOTIFY_COMPLETE(isp);
5852                 }
5853                 /*
5854                  * It's up to the handler for isp_async to reinit stuff and
5855                  * restart the firmware
5856                  */
5857                 isp_async(isp, ISPASYNC_FW_CRASH);
5858                 acked = 1;
5859                 break;
5860
5861         case ASYNC_RQS_XFER_ERR:
5862                 isp_prt(isp, ISP_LOGERR, "Request Queue Transfer Error");
5863                 break;
5864
5865         case ASYNC_RSP_XFER_ERR:
5866                 isp_prt(isp, ISP_LOGERR, "Response Queue Transfer Error");
5867                 break;
5868
5869         case ASYNC_QWAKEUP:
5870 #ifdef  ISP_TARGET_MODE
5871                 if (IS_24XX(isp)) {
5872                         isp_prt(isp, ISP_LOGERR, "ATIO Queue Transfer Error");
5873                         break;
5874                 }
5875 #endif
5876                 isp_prt(isp, ISP_LOGERR, "%s: unexpected ASYNC_QWAKEUP code", __func__);
5877                 break;
5878
5879         case ASYNC_CMD_CMPLT:
5880                 isp_fastpost_complete(isp, (ISP_READ(isp, OUTMAILBOX2) << 16) | ISP_READ(isp, OUTMAILBOX1));
5881                 if (isp->isp_fpcchiwater < 1) {
5882                         isp->isp_fpcchiwater = 1;
5883                 }
5884                 break;
5885
5886         case ASYNC_RIOZIO_STALL:
5887                 break;
5888
5889         case ASYNC_CTIO_DONE:
5890 #ifdef  ISP_TARGET_MODE
5891                 if (isp_target_async(isp, (ISP_READ(isp, OUTMAILBOX2) << 16) | ISP_READ(isp, OUTMAILBOX1), mbox)) {
5892                         acked = 1;
5893                 } else {
5894                         isp->isp_fphccmplt++;
5895                 }
5896 #else
5897                 isp_prt(isp, ISP_LOGWARN, "unexpected ASYNC CTIO done");
5898 #endif
5899                 break;
5900         case ASYNC_LIP_ERROR:
5901         case ASYNC_LIP_F8:
5902         case ASYNC_LIP_OCCURRED:
5903         case ASYNC_PTPMODE:
5904                 /*
5905                  * These are broadcast events that have to be sent across
5906                  * all active channels.
5907                  */
5908                 for (chan = 0; chan < isp->isp_nchan; chan++) {
5909                         fcparam *fcp = FCPARAM(isp, chan);
5910                         int topo = fcp->isp_topo;
5911
5912                         if (fcp->role == ISP_ROLE_NONE) {
5913                                 continue;
5914                         }
5915
5916                         fcp->isp_fwstate = FW_CONFIG_WAIT;
5917                         fcp->isp_loopstate = LOOP_LIP_RCVD;
5918                         ISP_SET_SENDMARKER(isp, chan, 1);
5919                         ISP_MARK_PORTDB(isp, chan, 1);
5920                         isp_async(isp, ISPASYNC_LIP, chan);
5921 #ifdef  ISP_TARGET_MODE
5922                         if (isp_target_async(isp, chan, mbox)) {
5923                                 acked = 1;
5924                         }
5925 #endif
5926                         /*
5927                          * We've had problems with data corruption occuring on
5928                          * commands that complete (with no apparent error) after
5929                          * we receive a LIP. This has been observed mostly on
5930                          * Local Loop topologies. To be safe, let's just mark
5931                          * all active initiator commands as dead.
5932                          */
5933                         if (topo == TOPO_NL_PORT || topo == TOPO_FL_PORT) {
5934                                 int i, j;
5935                                 for (i = j = 0; i < isp->isp_maxcmds; i++) {
5936                                         XS_T *xs;
5937                                         isp_hdl_t *hdp;
5938
5939                                         hdp = &isp->isp_xflist[i];
5940                                         if (ISP_H2HT(hdp->handle) != ISP_HANDLE_INITIATOR) {
5941                                                 continue;
5942                                         }
5943                                         xs = hdp->cmd;
5944                                         if (XS_CHANNEL(xs) != chan) {
5945                                                 continue;
5946                                         }
5947                                         j++;
5948                                         isp_prt(isp, ISP_LOG_WARN1, "%d.%d.%d bus reset set at %s:%u", XS_CHANNEL(xs), XS_TGT(xs), XS_LUN(xs), __func__, __LINE__);
5949                                         XS_SETERR(xs, HBA_BUSRESET);
5950                                 }
5951                                 if (j) {
5952                                         isp_prt(isp, ISP_LOGERR, lipd, chan, j);
5953                                 }
5954                         }
5955                 }
5956                 break;
5957
5958         case ASYNC_LOOP_UP:
5959                 /*
5960                  * This is a broadcast event that has to be sent across
5961                  * all active channels.
5962                  */
5963                 for (chan = 0; chan < isp->isp_nchan; chan++) {
5964                         fcparam *fcp = FCPARAM(isp, chan);
5965
5966                         if (fcp->role == ISP_ROLE_NONE) {
5967                                 continue;
5968                         }
5969
5970                         ISP_SET_SENDMARKER(isp, chan, 1);
5971
5972                         fcp->isp_fwstate = FW_CONFIG_WAIT;
5973                         fcp->isp_loopstate = LOOP_LIP_RCVD;
5974                         ISP_MARK_PORTDB(isp, chan, 1);
5975                         isp_async(isp, ISPASYNC_LOOP_UP, chan);
5976 #ifdef  ISP_TARGET_MODE
5977                         if (isp_target_async(isp, chan, mbox)) {
5978                                 acked = 1;
5979                         }
5980 #endif
5981                 }
5982                 break;
5983
5984         case ASYNC_LOOP_DOWN:
5985                 /*
5986                  * This is a broadcast event that has to be sent across
5987                  * all active channels.
5988                  */
5989                 for (chan = 0; chan < isp->isp_nchan; chan++) {
5990                         fcparam *fcp = FCPARAM(isp, chan);
5991
5992                         if (fcp->role == ISP_ROLE_NONE) {
5993                                 continue;
5994                         }
5995
5996                         ISP_SET_SENDMARKER(isp, chan, 1);
5997                         fcp->isp_fwstate = FW_CONFIG_WAIT;
5998                         fcp->isp_loopstate = LOOP_NIL;
5999                         ISP_MARK_PORTDB(isp, chan, 1);
6000                         isp_async(isp, ISPASYNC_LOOP_DOWN, chan);
6001 #ifdef  ISP_TARGET_MODE
6002                         if (isp_target_async(isp, chan, mbox)) {
6003                                 acked = 1;
6004                         }
6005 #endif
6006                 }
6007                 break;
6008
6009         case ASYNC_LOOP_RESET:
6010                 /*
6011                  * This is a broadcast event that has to be sent across
6012                  * all active channels.
6013                  */
6014                 for (chan = 0; chan < isp->isp_nchan; chan++) {
6015                         fcparam *fcp = FCPARAM(isp, chan);
6016
6017                         if (fcp->role == ISP_ROLE_NONE) {
6018                                 continue;
6019                         }
6020
6021                         ISP_SET_SENDMARKER(isp, chan, 1);
6022                         fcp->isp_fwstate = FW_CONFIG_WAIT;
6023                         fcp->isp_loopstate = LOOP_NIL;
6024                         ISP_MARK_PORTDB(isp, chan, 1);
6025                         isp_async(isp, ISPASYNC_LOOP_RESET, chan);
6026 #ifdef  ISP_TARGET_MODE
6027                         if (isp_target_async(isp, chan, mbox)) {
6028                                 acked = 1;
6029                         }
6030 #endif
6031                 }
6032                 break;
6033
6034         case ASYNC_PDB_CHANGED:
6035         {
6036                 int nphdl, nlstate, reason;
6037                 /*
6038                  * We *should* get a channel out of the 24XX, but we don't seem
6039                  * to get more than a PDB CHANGED on channel 0, so turn it into
6040                  * a broadcast event.
6041                  */
6042                 if (IS_24XX(isp)) {
6043                         nphdl = ISP_READ(isp, OUTMAILBOX1);
6044                         nlstate = ISP_READ(isp, OUTMAILBOX2);
6045                         reason = ISP_READ(isp, OUTMAILBOX3) >> 8;
6046                 } else {
6047                         nphdl = NIL_HANDLE;
6048                         nlstate = reason = 0;
6049                 }
6050                 for (chan = 0; chan < isp->isp_nchan; chan++) {
6051                         fcparam *fcp = FCPARAM(isp, chan);
6052
6053                         if (fcp->role == ISP_ROLE_NONE) {
6054                                 continue;
6055                         }
6056                         ISP_SET_SENDMARKER(isp, chan, 1);
6057                         fcp->isp_loopstate = LOOP_PDB_RCVD;
6058                         ISP_MARK_PORTDB(isp, chan, 1);
6059                         isp_async(isp, ISPASYNC_CHANGE_NOTIFY, chan, ISPASYNC_CHANGE_PDB, nphdl, nlstate, reason);
6060                 }
6061                 break;
6062         }
6063         case ASYNC_CHANGE_NOTIFY:
6064         {
6065                 int lochan, hichan;
6066
6067                 if (ISP_FW_NEWER_THAN(isp, 4, 0, 25) && ISP_CAP_MULTI_ID(isp)) {
6068                         GET_24XX_BUS(isp, chan, "ASYNC_CHANGE_NOTIFY");
6069                         lochan = chan;
6070                         hichan = chan + 1;
6071                 } else {
6072                         lochan = 0;
6073                         hichan = isp->isp_nchan;
6074                 }
6075                 for (chan = lochan; chan < hichan; chan++) {
6076                         fcparam *fcp = FCPARAM(isp, chan);
6077
6078                         if (fcp->role == ISP_ROLE_NONE) {
6079                                 continue;
6080                         }
6081
6082                         if (fcp->isp_topo == TOPO_F_PORT) {
6083                                 fcp->isp_loopstate = LOOP_LSCAN_DONE;
6084                         } else {
6085                                 fcp->isp_loopstate = LOOP_PDB_RCVD;
6086                         }
6087                         ISP_MARK_PORTDB(isp, chan, 1);
6088                         isp_async(isp, ISPASYNC_CHANGE_NOTIFY, chan, ISPASYNC_CHANGE_SNS);
6089                 }
6090                 break;
6091         }
6092
6093         case ASYNC_CONNMODE:
6094                 /*
6095                  * This only applies to 2100 amd 2200 cards
6096                  */
6097                 if (!IS_2200(isp) && !IS_2100(isp)) {
6098                         isp_prt(isp, ISP_LOGWARN, "bad card for ASYNC_CONNMODE event");
6099                         break;
6100                 }
6101                 chan = 0;
6102                 mbox = ISP_READ(isp, OUTMAILBOX1);
6103                 ISP_MARK_PORTDB(isp, chan, 1);
6104                 switch (mbox) {
6105                 case ISP_CONN_LOOP:
6106                         isp_prt(isp, ISP_LOGINFO,
6107                             "Point-to-Point -> Loop mode");
6108                         break;
6109                 case ISP_CONN_PTP:
6110                         isp_prt(isp, ISP_LOGINFO,
6111                             "Loop -> Point-to-Point mode");
6112                         break;
6113                 case ISP_CONN_BADLIP:
6114                         isp_prt(isp, ISP_LOGWARN,
6115                             "Point-to-Point -> Loop mode (BAD LIP)");
6116                         break;
6117                 case ISP_CONN_FATAL:
6118                         isp->isp_dead = 1;
6119                         isp->isp_state = ISP_CRASHED;
6120                         isp_prt(isp, ISP_LOGERR, "FATAL CONNECTION ERROR");
6121                         isp_async(isp, ISPASYNC_FW_CRASH);
6122                         return (-1);
6123                 case ISP_CONN_LOOPBACK:
6124                         isp_prt(isp, ISP_LOGWARN,
6125                             "Looped Back in Point-to-Point mode");
6126                         break;
6127                 default:
6128                         isp_prt(isp, ISP_LOGWARN,
6129                             "Unknown connection mode (0x%x)", mbox);
6130                         break;
6131                 }
6132                 isp_async(isp, ISPASYNC_CHANGE_NOTIFY, chan, ISPASYNC_CHANGE_OTHER);
6133                 FCPARAM(isp, chan)->sendmarker = 1;
6134                 FCPARAM(isp, chan)->isp_fwstate = FW_CONFIG_WAIT;
6135                 FCPARAM(isp, chan)->isp_loopstate = LOOP_LIP_RCVD;
6136                 break;
6137
6138         case ASYNC_RCV_ERR:
6139                 if (IS_24XX(isp)) {
6140                         isp_prt(isp, ISP_LOGWARN, "Receive Error");
6141                 } else {
6142                         isp_prt(isp, ISP_LOGWARN, "unexpected ASYNC_RCV_ERR");
6143                 }
6144                 break;
6145         case ASYNC_RJT_SENT:    /* same as ASYNC_QFULL_SENT */
6146                 if (IS_24XX(isp)) {
6147                         isp_prt(isp, ISP_LOGTDEBUG0, "LS_RJT sent");
6148                         break;
6149                 } else if (IS_2200(isp)) {
6150                         isp_prt(isp, ISP_LOGTDEBUG0, "QFULL sent");
6151                         break;
6152                 }
6153                 /* FALLTHROUGH */
6154         default:
6155                 isp_prt(isp, ISP_LOGWARN, "Unknown Async Code 0x%x", mbox);
6156                 break;
6157         }
6158         if (mbox != ASYNC_CTIO_DONE && mbox != ASYNC_CMD_CMPLT) {
6159                 isp->isp_intoasync++;
6160         }
6161         return (acked);
6162 }
6163
6164 /*
6165  * Handle other response entries. A pointer to the request queue output
6166  * index is here in case we want to eat several entries at once, although
6167  * this is not used currently.
6168  */
6169
6170 static int
6171 isp_handle_other_response(ispsoftc_t *isp, int type, isphdr_t *hp, uint32_t *optrp)
6172 {
6173         switch (type) {
6174         case RQSTYPE_STATUS_CONT:
6175                 isp_prt(isp, ISP_LOG_WARN1, "Ignored Continuation Response");
6176                 return (1);
6177         case RQSTYPE_MARKER:
6178                 isp_prt(isp, ISP_LOG_WARN1, "Marker Response");
6179                 return (1);
6180         case RQSTYPE_ATIO:
6181         case RQSTYPE_CTIO:
6182         case RQSTYPE_ENABLE_LUN:
6183         case RQSTYPE_MODIFY_LUN:
6184         case RQSTYPE_NOTIFY:
6185         case RQSTYPE_NOTIFY_ACK:
6186         case RQSTYPE_CTIO1:
6187         case RQSTYPE_ATIO2:
6188         case RQSTYPE_CTIO2:
6189         case RQSTYPE_CTIO3:
6190         case RQSTYPE_CTIO7:
6191         case RQSTYPE_ABTS_RCVD:
6192         case RQSTYPE_ABTS_RSP:
6193                 isp->isp_rsltccmplt++;  /* count as a response completion */
6194 #ifdef  ISP_TARGET_MODE
6195                 if (isp_target_notify(isp, (ispstatusreq_t *) hp, optrp)) {
6196                         return (1);
6197                 }
6198 #endif
6199                 /* FALLTHROUGH */
6200         case RQSTYPE_RPT_ID_ACQ:
6201                 if (IS_24XX(isp)) {
6202                         isp_ridacq_t rid;
6203                         isp_get_ridacq(isp, (isp_ridacq_t *)hp, &rid);
6204                         if (rid.ridacq_format == 0) {
6205                         }
6206                         return (1);
6207                 }
6208                 /* FALLTHROUGH */
6209         case RQSTYPE_REQUEST:
6210         default:
6211                 ISP_DELAY(100);
6212                 if (type != isp_get_response_type(isp, hp)) {
6213                         /*
6214                          * This is questionable- we're just papering over
6215                          * something we've seen on SMP linux in target
6216                          * mode- we don't really know what's happening
6217                          * here that causes us to think we've gotten
6218                          * an entry, but that either the entry isn't
6219                          * filled out yet or our CPU read data is stale.
6220                          */
6221                         isp_prt(isp, ISP_LOGINFO,
6222                                 "unstable type in response queue");
6223                         return (-1);
6224                 }
6225                 isp_prt(isp, ISP_LOGWARN, "Unhandled Response Type 0x%x",
6226                     isp_get_response_type(isp, hp));
6227                 return (0);
6228         }
6229 }
6230
6231 static void
6232 isp_parse_status(ispsoftc_t *isp, ispstatusreq_t *sp, XS_T *xs, long *rp)
6233 {
6234         switch (sp->req_completion_status & 0xff) {
6235         case RQCS_COMPLETE:
6236                 if (XS_NOERR(xs)) {
6237                         XS_SETERR(xs, HBA_NOERROR);
6238                 }
6239                 return;
6240
6241         case RQCS_INCOMPLETE:
6242                 if ((sp->req_state_flags & RQSF_GOT_TARGET) == 0) {
6243                         isp_xs_prt(isp, xs, ISP_LOG_WARN1, "Selection Timeout @ %s:%d", __func__, __LINE__);
6244                         if (XS_NOERR(xs)) {
6245                                 XS_SETERR(xs, HBA_SELTIMEOUT);
6246                                 *rp = XS_XFRLEN(xs);
6247                         }
6248                         return;
6249                 }
6250                 isp_xs_prt(isp, xs, ISP_LOGERR, "Command Incomplete, state 0x%x", sp->req_state_flags);
6251                 break;
6252
6253         case RQCS_DMA_ERROR:
6254                 isp_xs_prt(isp, xs, ISP_LOGERR, "DMA Error");
6255                 *rp = XS_XFRLEN(xs);
6256                 break;
6257
6258         case RQCS_TRANSPORT_ERROR:
6259         {
6260                 char buf[172];
6261                 ISP_SNPRINTF(buf, sizeof (buf), "states=>");
6262                 if (sp->req_state_flags & RQSF_GOT_BUS) {
6263                         ISP_SNPRINTF(buf, sizeof (buf), "%s GOT_BUS", buf);
6264                 }
6265                 if (sp->req_state_flags & RQSF_GOT_TARGET) {
6266                         ISP_SNPRINTF(buf, sizeof (buf), "%s GOT_TGT", buf);
6267                 }
6268                 if (sp->req_state_flags & RQSF_SENT_CDB) {
6269                         ISP_SNPRINTF(buf, sizeof (buf), "%s SENT_CDB", buf);
6270                 }
6271                 if (sp->req_state_flags & RQSF_XFRD_DATA) {
6272                         ISP_SNPRINTF(buf, sizeof (buf), "%s XFRD_DATA", buf);
6273                 }
6274                 if (sp->req_state_flags & RQSF_GOT_STATUS) {
6275                         ISP_SNPRINTF(buf, sizeof (buf), "%s GOT_STS", buf);
6276                 }
6277                 if (sp->req_state_flags & RQSF_GOT_SENSE) {
6278                         ISP_SNPRINTF(buf, sizeof (buf), "%s GOT_SNS", buf);
6279                 }
6280                 if (sp->req_state_flags & RQSF_XFER_COMPLETE) {
6281                         ISP_SNPRINTF(buf, sizeof (buf), "%s XFR_CMPLT", buf);
6282                 }
6283                 ISP_SNPRINTF(buf, sizeof (buf), "%s\nstatus=>", buf);
6284                 if (sp->req_status_flags & RQSTF_DISCONNECT) {
6285                         ISP_SNPRINTF(buf, sizeof (buf), "%s Disconnect", buf);
6286                 }
6287                 if (sp->req_status_flags & RQSTF_SYNCHRONOUS) {
6288                         ISP_SNPRINTF(buf, sizeof (buf), "%s Sync_xfr", buf);
6289                 }
6290                 if (sp->req_status_flags & RQSTF_PARITY_ERROR) {
6291                         ISP_SNPRINTF(buf, sizeof (buf), "%s Parity", buf);
6292                 }
6293                 if (sp->req_status_flags & RQSTF_BUS_RESET) {
6294                         ISP_SNPRINTF(buf, sizeof (buf), "%s Bus_Reset", buf);
6295                 }
6296                 if (sp->req_status_flags & RQSTF_DEVICE_RESET) {
6297                         ISP_SNPRINTF(buf, sizeof (buf), "%s Device_Reset", buf);
6298                 }
6299                 if (sp->req_status_flags & RQSTF_ABORTED) {
6300                         ISP_SNPRINTF(buf, sizeof (buf), "%s Aborted", buf);
6301                 }
6302                 if (sp->req_status_flags & RQSTF_TIMEOUT) {
6303                         ISP_SNPRINTF(buf, sizeof (buf), "%s Timeout", buf);
6304                 }
6305                 if (sp->req_status_flags & RQSTF_NEGOTIATION) {
6306                         ISP_SNPRINTF(buf, sizeof (buf), "%s Negotiation", buf);
6307                 }
6308                 isp_xs_prt(isp, xs,  ISP_LOGERR, "Transport Error: %s", buf);
6309                 *rp = XS_XFRLEN(xs);
6310                 break;
6311         }
6312         case RQCS_RESET_OCCURRED:
6313         {
6314                 int chan;
6315                 isp_xs_prt(isp, xs, ISP_LOGWARN, "Bus Reset destroyed command");
6316                 for (chan = 0; chan < isp->isp_nchan; chan++) {
6317                         FCPARAM(isp, chan)->sendmarker = 1;
6318                 }
6319                 if (XS_NOERR(xs)) {
6320                         XS_SETERR(xs, HBA_BUSRESET);
6321                 }
6322                 *rp = XS_XFRLEN(xs);
6323                 return;
6324         }
6325         case RQCS_ABORTED:
6326                 isp_xs_prt(isp, xs, ISP_LOGERR, "Command Aborted");
6327                 ISP_SET_SENDMARKER(isp, XS_CHANNEL(xs), 1);
6328                 if (XS_NOERR(xs)) {
6329                         XS_SETERR(xs, HBA_ABORTED);
6330                 }
6331                 return;
6332
6333         case RQCS_TIMEOUT:
6334                 isp_xs_prt(isp, xs, ISP_LOGWARN, "Command timed out");
6335                 /*
6336                  * XXX: Check to see if we logged out of the device.
6337                  */
6338                 if (XS_NOERR(xs)) {
6339                         XS_SETERR(xs, HBA_CMDTIMEOUT);
6340                 }
6341                 return;
6342
6343         case RQCS_DATA_OVERRUN:
6344                 XS_SET_RESID(xs, sp->req_resid);
6345                 isp_xs_prt(isp, xs, ISP_LOGERR, "data overrun (%ld)", (long) XS_GET_RESID(xs));
6346                 if (XS_NOERR(xs)) {
6347                         XS_SETERR(xs, HBA_DATAOVR);
6348                 }
6349                 return;
6350
6351         case RQCS_COMMAND_OVERRUN:
6352                 isp_xs_prt(isp, xs, ISP_LOGERR, "command overrun");
6353                 break;
6354
6355         case RQCS_STATUS_OVERRUN:
6356                 isp_xs_prt(isp, xs, ISP_LOGERR, "status overrun");
6357                 break;
6358
6359         case RQCS_BAD_MESSAGE:
6360                 isp_xs_prt(isp, xs, ISP_LOGERR, "msg not COMMAND COMPLETE after status");
6361                 break;
6362
6363         case RQCS_NO_MESSAGE_OUT:
6364                 isp_xs_prt(isp, xs, ISP_LOGERR, "No MESSAGE OUT phase after selection");
6365                 break;
6366
6367         case RQCS_EXT_ID_FAILED:
6368                 isp_xs_prt(isp, xs, ISP_LOGERR, "EXTENDED IDENTIFY failed");
6369                 break;
6370
6371         case RQCS_IDE_MSG_FAILED:
6372                 isp_xs_prt(isp, xs, ISP_LOGERR, "INITIATOR DETECTED ERROR rejected");
6373                 break;
6374
6375         case RQCS_ABORT_MSG_FAILED:
6376                 isp_xs_prt(isp, xs, ISP_LOGERR, "ABORT OPERATION rejected");
6377                 break;
6378
6379         case RQCS_REJECT_MSG_FAILED:
6380                 isp_xs_prt(isp, xs, ISP_LOGERR, "MESSAGE REJECT rejected");
6381                 break;
6382
6383         case RQCS_NOP_MSG_FAILED:
6384                 isp_xs_prt(isp, xs, ISP_LOGERR, "NOP rejected");
6385                 break;
6386
6387         case RQCS_PARITY_ERROR_MSG_FAILED:
6388                 isp_xs_prt(isp, xs, ISP_LOGERR, "MESSAGE PARITY ERROR rejected");
6389                 break;
6390
6391         case RQCS_DEVICE_RESET_MSG_FAILED:
6392                 isp_xs_prt(isp, xs, ISP_LOGWARN, "BUS DEVICE RESET rejected");
6393                 break;
6394
6395         case RQCS_ID_MSG_FAILED:
6396                 isp_xs_prt(isp, xs, ISP_LOGERR, "IDENTIFY rejected");
6397                 break;
6398
6399         case RQCS_UNEXP_BUS_FREE:
6400                 isp_xs_prt(isp, xs, ISP_LOGERR, "Unexpected Bus Free");
6401                 break;
6402
6403         case RQCS_DATA_UNDERRUN:
6404         {
6405                 if (IS_FC(isp)) {
6406                         int ru_marked = (sp->req_scsi_status & RQCS_RU) != 0;
6407                         if (!ru_marked || sp->req_resid > XS_XFRLEN(xs)) {
6408                                 isp_xs_prt(isp, xs, ISP_LOGWARN, bun, XS_XFRLEN(xs), sp->req_resid, (ru_marked)? "marked" : "not marked");
6409                                 if (XS_NOERR(xs)) {
6410                                         XS_SETERR(xs, HBA_BOTCH);
6411                                 }
6412                                 return;
6413                         }
6414                 }
6415                 XS_SET_RESID(xs, sp->req_resid);
6416                 if (XS_NOERR(xs)) {
6417                         XS_SETERR(xs, HBA_NOERROR);
6418                 }
6419                 return;
6420         }
6421
6422         case RQCS_XACT_ERR1:
6423                 isp_xs_prt(isp, xs, ISP_LOGERR, "HBA attempted queued transaction with disconnect not set");
6424                 break;
6425
6426         case RQCS_XACT_ERR2:
6427                 isp_xs_prt(isp, xs, ISP_LOGERR, "HBA attempted queued transaction to target routine %d", XS_LUN(xs));
6428                 break;
6429
6430         case RQCS_XACT_ERR3:
6431                 isp_xs_prt(isp, xs, ISP_LOGERR, "HBA attempted queued cmd when queueing disabled");
6432                 break;
6433
6434         case RQCS_BAD_ENTRY:
6435                 isp_prt(isp, ISP_LOGERR, "Invalid IOCB entry type detected");
6436                 break;
6437
6438         case RQCS_QUEUE_FULL:
6439                 isp_xs_prt(isp, xs, ISP_LOG_WARN1, "internal queues full status 0x%x", *XS_STSP(xs));
6440
6441                 /*
6442                  * If QFULL or some other status byte is set, then this
6443                  * isn't an error, per se.
6444                  *
6445                  * Unfortunately, some QLogic f/w writers have, in
6446                  * some cases, ommitted to *set* status to QFULL.
6447                  */
6448 #if     0
6449                 if (*XS_STSP(xs) != SCSI_GOOD && XS_NOERR(xs)) {
6450                         XS_SETERR(xs, HBA_NOERROR);
6451                         return;
6452                 }
6453
6454 #endif
6455                 *XS_STSP(xs) = SCSI_QFULL;
6456                 XS_SETERR(xs, HBA_NOERROR);
6457                 return;
6458
6459         case RQCS_PHASE_SKIPPED:
6460                 isp_xs_prt(isp, xs, ISP_LOGERR, "SCSI phase skipped");
6461                 break;
6462
6463         case RQCS_ARQS_FAILED:
6464                 isp_xs_prt(isp, xs, ISP_LOGERR, "Auto Request Sense Failed");
6465                 if (XS_NOERR(xs)) {
6466                         XS_SETERR(xs, HBA_ARQFAIL);
6467                 }
6468                 return;
6469
6470         case RQCS_WIDE_FAILED:
6471                 isp_xs_prt(isp, xs, ISP_LOGERR, "Wide Negotiation Failed");
6472                 if (IS_SCSI(isp)) {
6473                         sdparam *sdp = SDPARAM(isp, XS_CHANNEL(xs));
6474                         sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_WIDE;
6475                         sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
6476                         sdp->update = 1;
6477                 }
6478                 if (XS_NOERR(xs)) {
6479                         XS_SETERR(xs, HBA_NOERROR);
6480                 }
6481                 return;
6482
6483         case RQCS_SYNCXFER_FAILED:
6484                 isp_xs_prt(isp, xs, ISP_LOGERR, "SDTR Message Failed");
6485                 if (IS_SCSI(isp)) {
6486                         sdparam *sdp = SDPARAM(isp, XS_CHANNEL(xs));
6487                         sdp += XS_CHANNEL(xs);
6488                         sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_SYNC;
6489                         sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
6490                         sdp->update = 1;
6491                 }
6492                 break;
6493
6494         case RQCS_LVD_BUSERR:
6495                 isp_xs_prt(isp, xs, ISP_LOGERR, "Bad LVD condition");
6496                 break;
6497
6498         case RQCS_PORT_UNAVAILABLE:
6499                 /*
6500                  * No such port on the loop. Moral equivalent of SELTIMEO
6501                  */
6502         case RQCS_PORT_LOGGED_OUT:
6503         {
6504                 const char *reason;
6505                 uint8_t sts = sp->req_completion_status & 0xff;
6506
6507                 /*
6508                  * It was there (maybe)- treat as a selection timeout.
6509                  */
6510                 if (sts == RQCS_PORT_UNAVAILABLE) {
6511                         reason = "unavailable";
6512                 } else {
6513                         reason = "logout";
6514                 }
6515
6516                 isp_prt(isp, ISP_LOGINFO, "port %s for target %d", reason, XS_TGT(xs));
6517
6518                 /*
6519                  * If we're on a local loop, force a LIP (which is overkill)
6520                  * to force a re-login of this unit. If we're on fabric,
6521                  * then we'll have to log in again as a matter of course.
6522                  */
6523                 if (FCPARAM(isp, 0)->isp_topo == TOPO_NL_PORT ||
6524                     FCPARAM(isp, 0)->isp_topo == TOPO_FL_PORT) {
6525                         mbreg_t mbs;
6526                         MBSINIT(&mbs, MBOX_INIT_LIP, MBLOGALL, 0);
6527                         if (ISP_CAP_2KLOGIN(isp)) {
6528                                 mbs.ibits = (1 << 10);
6529                         }
6530                         isp_mboxcmd_qnw(isp, &mbs, 1);
6531                 }
6532                 if (XS_NOERR(xs)) {
6533                         XS_SETERR(xs, HBA_SELTIMEOUT);
6534                 }
6535                 return;
6536         }
6537         case RQCS_PORT_CHANGED:
6538                 isp_prt(isp, ISP_LOGWARN, "port changed for target %d", XS_TGT(xs));
6539                 if (XS_NOERR(xs)) {
6540                         XS_SETERR(xs, HBA_SELTIMEOUT);
6541                 }
6542                 return;
6543
6544         case RQCS_PORT_BUSY:
6545                 isp_prt(isp, ISP_LOGWARN, "port busy for target %d", XS_TGT(xs));
6546                 if (XS_NOERR(xs)) {
6547                         XS_SETERR(xs, HBA_TGTBSY);
6548                 }
6549                 return;
6550
6551         default:
6552                 isp_prt(isp, ISP_LOGERR, "Unknown Completion Status 0x%x", sp->req_completion_status);
6553                 break;
6554         }
6555         if (XS_NOERR(xs)) {
6556                 XS_SETERR(xs, HBA_BOTCH);
6557         }
6558 }
6559
6560 static void
6561 isp_parse_status_24xx(ispsoftc_t *isp, isp24xx_statusreq_t *sp, XS_T *xs, long *rp)
6562 {
6563         int ru_marked, sv_marked;
6564         int chan = XS_CHANNEL(xs);
6565
6566         switch (sp->req_completion_status) {
6567         case RQCS_COMPLETE:
6568                 if (XS_NOERR(xs)) {
6569                         XS_SETERR(xs, HBA_NOERROR);
6570                 }
6571                 return;
6572
6573         case RQCS_DMA_ERROR:
6574                 isp_xs_prt(isp, xs, ISP_LOGERR, "DMA error");
6575                 break;
6576
6577         case RQCS_TRANSPORT_ERROR:
6578                 isp_xs_prt(isp, xs,  ISP_LOGERR, "Transport Error");
6579                 break;
6580
6581         case RQCS_RESET_OCCURRED:
6582                 isp_xs_prt(isp, xs, ISP_LOGWARN, "reset destroyed command");
6583                 FCPARAM(isp, chan)->sendmarker = 1;
6584                 if (XS_NOERR(xs)) {
6585                         XS_SETERR(xs, HBA_BUSRESET);
6586                 }
6587                 return;
6588
6589         case RQCS_ABORTED:
6590                 isp_xs_prt(isp, xs, ISP_LOGERR, "Command Aborted");
6591                 FCPARAM(isp, chan)->sendmarker = 1;
6592                 if (XS_NOERR(xs)) {
6593                         XS_SETERR(xs, HBA_ABORTED);
6594                 }
6595                 return;
6596
6597         case RQCS_TIMEOUT:
6598                 isp_xs_prt(isp, xs, ISP_LOGWARN, "Command Timed Out");
6599                 if (XS_NOERR(xs)) {
6600                         XS_SETERR(xs, HBA_CMDTIMEOUT);
6601                 }
6602                 return;
6603
6604         case RQCS_DATA_OVERRUN:
6605                 XS_SET_RESID(xs, sp->req_resid);
6606                 isp_xs_prt(isp, xs, ISP_LOGERR, "Data Overrun");
6607                 if (XS_NOERR(xs)) {
6608                         XS_SETERR(xs, HBA_DATAOVR);
6609                 }
6610                 return;
6611
6612         case RQCS_24XX_DRE:     /* data reassembly error */
6613                 isp_prt(isp, ISP_LOGERR, "Chan %d data reassembly error for target %d", chan, XS_TGT(xs));
6614                 if (XS_NOERR(xs)) {
6615                         XS_SETERR(xs, HBA_ABORTED);
6616                 }
6617                 *rp = XS_XFRLEN(xs);
6618                 return;
6619
6620         case RQCS_24XX_TABORT:  /* aborted by target */
6621                 isp_prt(isp, ISP_LOGERR, "Chan %d target %d sent ABTS", chan, XS_TGT(xs));
6622                 if (XS_NOERR(xs)) {
6623                         XS_SETERR(xs, HBA_ABORTED);
6624                 }
6625                 return;
6626
6627         case RQCS_DATA_UNDERRUN:
6628                 ru_marked = (sp->req_scsi_status & RQCS_RU) != 0;
6629                 /*
6630                  * We can get an underrun w/o things being marked
6631                  * if we got a non-zero status.
6632                  */
6633                 sv_marked = (sp->req_scsi_status & (RQCS_SV|RQCS_RV)) != 0;
6634                 if ((ru_marked == 0 && sv_marked == 0) ||
6635                     (sp->req_resid > XS_XFRLEN(xs))) {
6636                         isp_xs_prt(isp, xs, ISP_LOGWARN, bun, XS_XFRLEN(xs), sp->req_resid, (ru_marked)? "marked" : "not marked");
6637                         if (XS_NOERR(xs)) {
6638                                 XS_SETERR(xs, HBA_BOTCH);
6639                         }
6640                         return;
6641                 }
6642                 XS_SET_RESID(xs, sp->req_resid);
6643                 isp_xs_prt(isp, xs, ISP_LOG_WARN1, "Data Underrun (%d) for command 0x%x", sp->req_resid, XS_CDBP(xs)[0] & 0xff);
6644                 if (XS_NOERR(xs)) {
6645                         XS_SETERR(xs, HBA_NOERROR);
6646                 }
6647                 return;
6648
6649         case RQCS_PORT_UNAVAILABLE:
6650                 /*
6651                  * No such port on the loop. Moral equivalent of SELTIMEO
6652                  */
6653         case RQCS_PORT_LOGGED_OUT:
6654         {
6655                 const char *reason;
6656                 uint8_t sts = sp->req_completion_status & 0xff;
6657
6658                 /*
6659                  * It was there (maybe)- treat as a selection timeout.
6660                  */
6661                 if (sts == RQCS_PORT_UNAVAILABLE) {
6662                         reason = "unavailable";
6663                 } else {
6664                         reason = "logout";
6665                 }
6666
6667                 isp_prt(isp, ISP_LOGINFO, "Chan %d port %s for target %d",
6668                     chan, reason, XS_TGT(xs));
6669
6670                 /*
6671                  * There is no MBOX_INIT_LIP for the 24XX.
6672                  */
6673                 if (XS_NOERR(xs)) {
6674                         XS_SETERR(xs, HBA_SELTIMEOUT);
6675                 }
6676                 return;
6677         }
6678         case RQCS_PORT_CHANGED:
6679                 isp_prt(isp, ISP_LOGWARN, "port changed for target %d chan %d", XS_TGT(xs), chan);
6680                 if (XS_NOERR(xs)) {
6681                         XS_SETERR(xs, HBA_SELTIMEOUT);
6682                 }
6683                 return;
6684
6685
6686         case RQCS_24XX_ENOMEM:  /* f/w resource unavailable */
6687                 isp_prt(isp, ISP_LOGWARN, "f/w resource unavailable for target %d chan %d", XS_TGT(xs), chan);
6688                 if (XS_NOERR(xs)) {
6689                         *XS_STSP(xs) = SCSI_BUSY;
6690                         XS_SETERR(xs, HBA_TGTBSY);
6691                 }
6692                 return;
6693
6694         case RQCS_24XX_TMO:     /* task management overrun */
6695                 isp_prt(isp, ISP_LOGWARN, "command for target %d overlapped task management for chan %d", XS_TGT(xs), chan);
6696                 if (XS_NOERR(xs)) {
6697                         *XS_STSP(xs) = SCSI_BUSY;
6698                         XS_SETERR(xs, HBA_TGTBSY);
6699                 }
6700                 return;
6701
6702         default:
6703                 isp_prt(isp, ISP_LOGERR, "Unknown Completion Status 0x%x on chan %d", sp->req_completion_status, chan);
6704                 break;
6705         }
6706         if (XS_NOERR(xs)) {
6707                 XS_SETERR(xs, HBA_BOTCH);
6708         }
6709 }
6710
6711 static void
6712 isp_fastpost_complete(ispsoftc_t *isp, uint32_t fph)
6713 {
6714         XS_T *xs;
6715
6716         if (fph == 0) {
6717                 return;
6718         }
6719         xs = isp_find_xs(isp, fph);
6720         if (xs == NULL) {
6721                 isp_prt(isp, ISP_LOGWARN,
6722                     "Command for fast post handle 0x%x not found", fph);
6723                 return;
6724         }
6725         isp_destroy_handle(isp, fph);
6726
6727         /*
6728          * Since we don't have a result queue entry item,
6729          * we must believe that SCSI status is zero and
6730          * that all data transferred.
6731          */
6732         XS_SET_RESID(xs, 0);
6733         *XS_STSP(xs) = SCSI_GOOD;
6734         if (XS_XFRLEN(xs)) {
6735                 ISP_DMAFREE(isp, xs, fph);
6736         }
6737         if (isp->isp_nactive) {
6738                 isp->isp_nactive--;
6739         }
6740         isp->isp_fphccmplt++;
6741         isp_done(xs);
6742 }
6743
6744 static int
6745 isp_mbox_continue(ispsoftc_t *isp)
6746 {
6747         mbreg_t mbs;
6748         uint16_t *ptr;
6749         uint32_t offset;
6750
6751         switch (isp->isp_lastmbxcmd) {
6752         case MBOX_WRITE_RAM_WORD:
6753         case MBOX_READ_RAM_WORD:
6754         case MBOX_WRITE_RAM_WORD_EXTENDED:
6755         case MBOX_READ_RAM_WORD_EXTENDED:
6756                 break;
6757         default:
6758                 return (1);
6759         }
6760         if (isp->isp_mboxtmp[0] != MBOX_COMMAND_COMPLETE) {
6761                 isp->isp_mbxwrk0 = 0;
6762                 return (-1);
6763         }
6764
6765         /*
6766          * Clear the previous interrupt.
6767          */
6768         if (IS_24XX(isp)) {
6769                 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
6770         } else {
6771                 ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
6772                 ISP_WRITE(isp, BIU_SEMA, 0);
6773         }
6774
6775         /*
6776          * Continue with next word.
6777          */
6778         ISP_MEMZERO(&mbs, sizeof (mbs));
6779         ptr = isp->isp_mbxworkp;
6780         switch (isp->isp_lastmbxcmd) {
6781         case MBOX_WRITE_RAM_WORD:
6782                 mbs.param[1] = isp->isp_mbxwrk1++;
6783                 mbs.param[2] = *ptr++;
6784                 break;
6785         case MBOX_READ_RAM_WORD:
6786                 *ptr++ = isp->isp_mboxtmp[2];
6787                 mbs.param[1] = isp->isp_mbxwrk1++;
6788                 break;
6789         case MBOX_WRITE_RAM_WORD_EXTENDED:
6790                 if (IS_24XX(isp)) {
6791                         uint32_t *lptr = (uint32_t *)ptr;
6792                         mbs.param[2] = lptr[0];
6793                         mbs.param[3] = lptr[0] >> 16;
6794                         lptr++;
6795                         ptr = (uint16_t *)lptr;
6796                 } else {
6797                         mbs.param[2] = *ptr++;
6798                 }
6799                 offset = isp->isp_mbxwrk1;
6800                 offset |= isp->isp_mbxwrk8 << 16;
6801                 mbs.param[1] = offset;
6802                 mbs.param[8] = offset >> 16;
6803                 offset++;
6804                 isp->isp_mbxwrk1 = offset;
6805                 isp->isp_mbxwrk8 = offset >> 16;
6806                 break;
6807         case MBOX_READ_RAM_WORD_EXTENDED:
6808                 if (IS_24XX(isp)) {
6809                         uint32_t *lptr = (uint32_t *)ptr;
6810                         uint32_t val = isp->isp_mboxtmp[2];
6811                         val |= (isp->isp_mboxtmp[3]) << 16;
6812                         *lptr++ = val;
6813                         ptr = (uint16_t *)lptr;
6814                 } else {
6815                         *ptr++ = isp->isp_mboxtmp[2];
6816                 }
6817                 offset = isp->isp_mbxwrk1;
6818                 offset |= isp->isp_mbxwrk8 << 16;
6819                 mbs.param[1] = offset;
6820                 mbs.param[8] = offset >> 16;
6821                 offset++;
6822                 isp->isp_mbxwrk1 = offset;
6823                 isp->isp_mbxwrk8 = offset >> 16;
6824                 break;
6825         }
6826         isp->isp_mbxworkp = ptr;
6827         isp->isp_mbxwrk0--;
6828         mbs.param[0] = isp->isp_lastmbxcmd;
6829         mbs.logval = MBLOGALL;
6830         isp_mboxcmd_qnw(isp, &mbs, 0);
6831         return (0);
6832 }
6833
6834 #define ISP_SCSI_IBITS(op)              (mbpscsi[((op)<<1)])
6835 #define ISP_SCSI_OBITS(op)              (mbpscsi[((op)<<1) + 1])
6836 #define ISP_SCSI_OPMAP(in, out)         in, out
6837 static const uint8_t mbpscsi[] = {
6838         ISP_SCSI_OPMAP(0x01, 0x01),     /* 0x00: MBOX_NO_OP */
6839         ISP_SCSI_OPMAP(0x1f, 0x01),     /* 0x01: MBOX_LOAD_RAM */
6840         ISP_SCSI_OPMAP(0x03, 0x01),     /* 0x02: MBOX_EXEC_FIRMWARE */
6841         ISP_SCSI_OPMAP(0x1f, 0x01),     /* 0x03: MBOX_DUMP_RAM */
6842         ISP_SCSI_OPMAP(0x07, 0x07),     /* 0x04: MBOX_WRITE_RAM_WORD */
6843         ISP_SCSI_OPMAP(0x03, 0x07),     /* 0x05: MBOX_READ_RAM_WORD */
6844         ISP_SCSI_OPMAP(0x3f, 0x3f),     /* 0x06: MBOX_MAILBOX_REG_TEST */
6845         ISP_SCSI_OPMAP(0x07, 0x07),     /* 0x07: MBOX_VERIFY_CHECKSUM   */
6846         ISP_SCSI_OPMAP(0x01, 0x0f),     /* 0x08: MBOX_ABOUT_FIRMWARE */
6847         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x09: */
6848         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x0a: */
6849         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x0b: */
6850         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x0c: */
6851         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x0d: */
6852         ISP_SCSI_OPMAP(0x01, 0x05),     /* 0x0e: MBOX_CHECK_FIRMWARE */
6853         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x0f: */
6854         ISP_SCSI_OPMAP(0x1f, 0x1f),     /* 0x10: MBOX_INIT_REQ_QUEUE */
6855         ISP_SCSI_OPMAP(0x3f, 0x3f),     /* 0x11: MBOX_INIT_RES_QUEUE */
6856         ISP_SCSI_OPMAP(0x0f, 0x0f),     /* 0x12: MBOX_EXECUTE_IOCB */
6857         ISP_SCSI_OPMAP(0x03, 0x03),     /* 0x13: MBOX_WAKE_UP   */
6858         ISP_SCSI_OPMAP(0x01, 0x3f),     /* 0x14: MBOX_STOP_FIRMWARE */
6859         ISP_SCSI_OPMAP(0x0f, 0x0f),     /* 0x15: MBOX_ABORT */
6860         ISP_SCSI_OPMAP(0x03, 0x03),     /* 0x16: MBOX_ABORT_DEVICE */
6861         ISP_SCSI_OPMAP(0x07, 0x07),     /* 0x17: MBOX_ABORT_TARGET */
6862         ISP_SCSI_OPMAP(0x07, 0x07),     /* 0x18: MBOX_BUS_RESET */
6863         ISP_SCSI_OPMAP(0x03, 0x07),     /* 0x19: MBOX_STOP_QUEUE */
6864         ISP_SCSI_OPMAP(0x03, 0x07),     /* 0x1a: MBOX_START_QUEUE */
6865         ISP_SCSI_OPMAP(0x03, 0x07),     /* 0x1b: MBOX_SINGLE_STEP_QUEUE */
6866         ISP_SCSI_OPMAP(0x03, 0x07),     /* 0x1c: MBOX_ABORT_QUEUE */
6867         ISP_SCSI_OPMAP(0x03, 0x4f),     /* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
6868         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x1e: */
6869         ISP_SCSI_OPMAP(0x01, 0x07),     /* 0x1f: MBOX_GET_FIRMWARE_STATUS */
6870         ISP_SCSI_OPMAP(0x01, 0x07),     /* 0x20: MBOX_GET_INIT_SCSI_ID */
6871         ISP_SCSI_OPMAP(0x01, 0x07),     /* 0x21: MBOX_GET_SELECT_TIMEOUT */
6872         ISP_SCSI_OPMAP(0x01, 0xc7),     /* 0x22: MBOX_GET_RETRY_COUNT   */
6873         ISP_SCSI_OPMAP(0x01, 0x07),     /* 0x23: MBOX_GET_TAG_AGE_LIMIT */
6874         ISP_SCSI_OPMAP(0x01, 0x03),     /* 0x24: MBOX_GET_CLOCK_RATE */
6875         ISP_SCSI_OPMAP(0x01, 0x07),     /* 0x25: MBOX_GET_ACT_NEG_STATE */
6876         ISP_SCSI_OPMAP(0x01, 0x07),     /* 0x26: MBOX_GET_ASYNC_DATA_SETUP_TIME */
6877         ISP_SCSI_OPMAP(0x01, 0x07),     /* 0x27: MBOX_GET_PCI_PARAMS */
6878         ISP_SCSI_OPMAP(0x03, 0x4f),     /* 0x28: MBOX_GET_TARGET_PARAMS */
6879         ISP_SCSI_OPMAP(0x03, 0x0f),     /* 0x29: MBOX_GET_DEV_QUEUE_PARAMS */
6880         ISP_SCSI_OPMAP(0x01, 0x07),     /* 0x2a: MBOX_GET_RESET_DELAY_PARAMS */
6881         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x2b: */
6882         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x2c: */
6883         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x2d: */
6884         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x2e: */
6885         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x2f: */
6886         ISP_SCSI_OPMAP(0x03, 0x03),     /* 0x30: MBOX_SET_INIT_SCSI_ID */
6887         ISP_SCSI_OPMAP(0x07, 0x07),     /* 0x31: MBOX_SET_SELECT_TIMEOUT */
6888         ISP_SCSI_OPMAP(0xc7, 0xc7),     /* 0x32: MBOX_SET_RETRY_COUNT   */
6889         ISP_SCSI_OPMAP(0x07, 0x07),     /* 0x33: MBOX_SET_TAG_AGE_LIMIT */
6890         ISP_SCSI_OPMAP(0x03, 0x03),     /* 0x34: MBOX_SET_CLOCK_RATE */
6891         ISP_SCSI_OPMAP(0x07, 0x07),     /* 0x35: MBOX_SET_ACT_NEG_STATE */
6892         ISP_SCSI_OPMAP(0x07, 0x07),     /* 0x36: MBOX_SET_ASYNC_DATA_SETUP_TIME */
6893         ISP_SCSI_OPMAP(0x07, 0x07),     /* 0x37: MBOX_SET_PCI_CONTROL_PARAMS */
6894         ISP_SCSI_OPMAP(0x4f, 0x4f),     /* 0x38: MBOX_SET_TARGET_PARAMS */
6895         ISP_SCSI_OPMAP(0x0f, 0x0f),     /* 0x39: MBOX_SET_DEV_QUEUE_PARAMS */
6896         ISP_SCSI_OPMAP(0x07, 0x07),     /* 0x3a: MBOX_SET_RESET_DELAY_PARAMS */
6897         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x3b: */
6898         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x3c: */
6899         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x3d: */
6900         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x3e: */
6901         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x3f: */
6902         ISP_SCSI_OPMAP(0x01, 0x03),     /* 0x40: MBOX_RETURN_BIOS_BLOCK_ADDR */
6903         ISP_SCSI_OPMAP(0x3f, 0x01),     /* 0x41: MBOX_WRITE_FOUR_RAM_WORDS */
6904         ISP_SCSI_OPMAP(0x03, 0x07),     /* 0x42: MBOX_EXEC_BIOS_IOCB */
6905         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x43: */
6906         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x44: */
6907         ISP_SCSI_OPMAP(0x03, 0x03),     /* 0x45: SET SYSTEM PARAMETER */
6908         ISP_SCSI_OPMAP(0x01, 0x03),     /* 0x46: GET SYSTEM PARAMETER */
6909         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x47: */
6910         ISP_SCSI_OPMAP(0x01, 0xcf),     /* 0x48: GET SCAM CONFIGURATION */
6911         ISP_SCSI_OPMAP(0xcf, 0xcf),     /* 0x49: SET SCAM CONFIGURATION */
6912         ISP_SCSI_OPMAP(0x03, 0x03),     /* 0x4a: MBOX_SET_FIRMWARE_FEATURES */
6913         ISP_SCSI_OPMAP(0x01, 0x03),     /* 0x4b: MBOX_GET_FIRMWARE_FEATURES */
6914         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x4c: */
6915         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x4d: */
6916         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x4e: */
6917         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x4f: */
6918         ISP_SCSI_OPMAP(0xdf, 0xdf),     /* 0x50: LOAD RAM A64 */
6919         ISP_SCSI_OPMAP(0xdf, 0xdf),     /* 0x51: DUMP RAM A64 */
6920         ISP_SCSI_OPMAP(0xdf, 0xff),     /* 0x52: INITIALIZE REQUEST QUEUE A64 */
6921         ISP_SCSI_OPMAP(0xef, 0xff),     /* 0x53: INITIALIZE RESPONSE QUEUE A64 */
6922         ISP_SCSI_OPMAP(0xcf, 0x01),     /* 0x54: EXECUCUTE COMMAND IOCB A64 */
6923         ISP_SCSI_OPMAP(0x07, 0x01),     /* 0x55: ENABLE TARGET MODE */
6924         ISP_SCSI_OPMAP(0x03, 0x0f),     /* 0x56: GET TARGET STATUS */
6925         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x57: */
6926         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x58: */
6927         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x59: */
6928         ISP_SCSI_OPMAP(0x03, 0x03),     /* 0x5a: SET DATA OVERRUN RECOVERY MODE */
6929         ISP_SCSI_OPMAP(0x01, 0x03),     /* 0x5b: GET DATA OVERRUN RECOVERY MODE */
6930         ISP_SCSI_OPMAP(0x0f, 0x0f),     /* 0x5c: SET HOST DATA */
6931         ISP_SCSI_OPMAP(0x01, 0x01)      /* 0x5d: GET NOST DATA */
6932 };
6933 #define MAX_SCSI_OPCODE 0x5d
6934
6935 static const char *scsi_mbcmd_names[] = {
6936         "NO-OP",
6937         "LOAD RAM",
6938         "EXEC FIRMWARE",
6939         "DUMP RAM",
6940         "WRITE RAM WORD",
6941         "READ RAM WORD",
6942         "MAILBOX REG TEST",
6943         "VERIFY CHECKSUM",
6944         "ABOUT FIRMWARE",
6945         NULL,
6946         NULL,
6947         NULL,
6948         NULL,
6949         NULL,
6950         "CHECK FIRMWARE",
6951         NULL,
6952         "INIT REQUEST QUEUE",
6953         "INIT RESULT QUEUE",
6954         "EXECUTE IOCB",
6955         "WAKE UP",
6956         "STOP FIRMWARE",
6957         "ABORT",
6958         "ABORT DEVICE",
6959         "ABORT TARGET",
6960         "BUS RESET",
6961         "STOP QUEUE",
6962         "START QUEUE",
6963         "SINGLE STEP QUEUE",
6964         "ABORT QUEUE",
6965         "GET DEV QUEUE STATUS",
6966         NULL,
6967         "GET FIRMWARE STATUS",
6968         "GET INIT SCSI ID",
6969         "GET SELECT TIMEOUT",
6970         "GET RETRY COUNT",
6971         "GET TAG AGE LIMIT",
6972         "GET CLOCK RATE",
6973         "GET ACT NEG STATE",
6974         "GET ASYNC DATA SETUP TIME",
6975         "GET PCI PARAMS",
6976         "GET TARGET PARAMS",
6977         "GET DEV QUEUE PARAMS",
6978         "GET RESET DELAY PARAMS",
6979         NULL,
6980         NULL,
6981         NULL,
6982         NULL,
6983         NULL,
6984         "SET INIT SCSI ID",
6985         "SET SELECT TIMEOUT",
6986         "SET RETRY COUNT",
6987         "SET TAG AGE LIMIT",
6988         "SET CLOCK RATE",
6989         "SET ACT NEG STATE",
6990         "SET ASYNC DATA SETUP TIME",
6991         "SET PCI CONTROL PARAMS",
6992         "SET TARGET PARAMS",
6993         "SET DEV QUEUE PARAMS",
6994         "SET RESET DELAY PARAMS",
6995         NULL,
6996         NULL,
6997         NULL,
6998         NULL,
6999         NULL,
7000         "RETURN BIOS BLOCK ADDR",
7001         "WRITE FOUR RAM WORDS",
7002         "EXEC BIOS IOCB",
7003         NULL,
7004         NULL,
7005         "SET SYSTEM PARAMETER",
7006         "GET SYSTEM PARAMETER",
7007         NULL,
7008         "GET SCAM CONFIGURATION",
7009         "SET SCAM CONFIGURATION",
7010         "SET FIRMWARE FEATURES",
7011         "GET FIRMWARE FEATURES",
7012         NULL,
7013         NULL,
7014         NULL,
7015         NULL,
7016         "LOAD RAM A64",
7017         "DUMP RAM A64",
7018         "INITIALIZE REQUEST QUEUE A64",
7019         "INITIALIZE RESPONSE QUEUE A64",
7020         "EXECUTE IOCB A64",
7021         "ENABLE TARGET MODE",
7022         "GET TARGET MODE STATE",
7023         NULL,
7024         NULL,
7025         NULL,
7026         "SET DATA OVERRUN RECOVERY MODE",
7027         "GET DATA OVERRUN RECOVERY MODE",
7028         "SET HOST DATA",
7029         "GET NOST DATA",
7030 };
7031
7032 #define ISP_FC_IBITS(op)        ((mbpfc[((op)<<3) + 0] << 24) | (mbpfc[((op)<<3) + 1] << 16) | (mbpfc[((op)<<3) + 2] << 8) | (mbpfc[((op)<<3) + 3]))
7033 #define ISP_FC_OBITS(op)        ((mbpfc[((op)<<3) + 4] << 24) | (mbpfc[((op)<<3) + 5] << 16) | (mbpfc[((op)<<3) + 6] << 8) | (mbpfc[((op)<<3) + 7]))
7034
7035 #define ISP_FC_OPMAP(in0, out0)                                                   0,   0,   0, in0,    0,    0,    0, out0
7036 #define ISP_FC_OPMAP_HALF(in1, in0, out1, out0)                                   0,   0, in1, in0,    0,    0, out1, out0
7037 #define ISP_FC_OPMAP_FULL(in3, in2, in1, in0, out3, out2, out1, out0)           in3, in2, in1, in0, out3, out2, out1, out0
7038 static const uint32_t mbpfc[] = {
7039         ISP_FC_OPMAP(0x01, 0x01),       /* 0x00: MBOX_NO_OP */
7040         ISP_FC_OPMAP(0x1f, 0x01),       /* 0x01: MBOX_LOAD_RAM */
7041         ISP_FC_OPMAP(0x0f, 0x01),       /* 0x02: MBOX_EXEC_FIRMWARE */
7042         ISP_FC_OPMAP(0xdf, 0x01),       /* 0x03: MBOX_DUMP_RAM */
7043         ISP_FC_OPMAP(0x07, 0x07),       /* 0x04: MBOX_WRITE_RAM_WORD */
7044         ISP_FC_OPMAP(0x03, 0x07),       /* 0x05: MBOX_READ_RAM_WORD */
7045         ISP_FC_OPMAP_FULL(0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff),      /* 0x06: MBOX_MAILBOX_REG_TEST */
7046         ISP_FC_OPMAP(0x07, 0x07),       /* 0x07: MBOX_VERIFY_CHECKSUM   */
7047         ISP_FC_OPMAP_FULL(0x0, 0x0, 0x0, 0x01, 0x0, 0x3, 0x80, 0x7f),   /* 0x08: MBOX_ABOUT_FIRMWARE */
7048         ISP_FC_OPMAP(0xdf, 0x01),       /* 0x09: MBOX_LOAD_RISC_RAM_2100 */
7049         ISP_FC_OPMAP(0xdf, 0x01),       /* 0x0a: DUMP RAM */
7050         ISP_FC_OPMAP_HALF(0x1, 0xff, 0x0, 0x01),        /* 0x0b: MBOX_LOAD_RISC_RAM */
7051         ISP_FC_OPMAP(0x00, 0x00),       /* 0x0c: */
7052         ISP_FC_OPMAP_HALF(0x1, 0x0f, 0x0, 0x01),        /* 0x0d: MBOX_WRITE_RAM_WORD_EXTENDED */
7053         ISP_FC_OPMAP(0x01, 0x05),       /* 0x0e: MBOX_CHECK_FIRMWARE */
7054         ISP_FC_OPMAP_HALF(0x1, 0x03, 0x0, 0x0d),        /* 0x0f: MBOX_READ_RAM_WORD_EXTENDED */
7055         ISP_FC_OPMAP(0x1f, 0x11),       /* 0x10: MBOX_INIT_REQ_QUEUE */
7056         ISP_FC_OPMAP(0x2f, 0x21),       /* 0x11: MBOX_INIT_RES_QUEUE */
7057         ISP_FC_OPMAP(0x0f, 0x01),       /* 0x12: MBOX_EXECUTE_IOCB */
7058         ISP_FC_OPMAP(0x03, 0x03),       /* 0x13: MBOX_WAKE_UP   */
7059         ISP_FC_OPMAP(0x01, 0xff),       /* 0x14: MBOX_STOP_FIRMWARE */
7060         ISP_FC_OPMAP(0x4f, 0x01),       /* 0x15: MBOX_ABORT */
7061         ISP_FC_OPMAP(0x07, 0x01),       /* 0x16: MBOX_ABORT_DEVICE */
7062         ISP_FC_OPMAP(0x07, 0x01),       /* 0x17: MBOX_ABORT_TARGET */
7063         ISP_FC_OPMAP(0x03, 0x03),       /* 0x18: MBOX_BUS_RESET */
7064         ISP_FC_OPMAP(0x07, 0x05),       /* 0x19: MBOX_STOP_QUEUE */
7065         ISP_FC_OPMAP(0x07, 0x05),       /* 0x1a: MBOX_START_QUEUE */
7066         ISP_FC_OPMAP(0x07, 0x05),       /* 0x1b: MBOX_SINGLE_STEP_QUEUE */
7067         ISP_FC_OPMAP(0x07, 0x05),       /* 0x1c: MBOX_ABORT_QUEUE */
7068         ISP_FC_OPMAP(0x07, 0x03),       /* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
7069         ISP_FC_OPMAP(0x00, 0x00),       /* 0x1e: */
7070         ISP_FC_OPMAP(0x01, 0x07),       /* 0x1f: MBOX_GET_FIRMWARE_STATUS */
7071         ISP_FC_OPMAP_HALF(0x2, 0x01, 0x0, 0xcf),        /* 0x20: MBOX_GET_LOOP_ID */
7072         ISP_FC_OPMAP(0x00, 0x00),       /* 0x21: */
7073         ISP_FC_OPMAP(0x01, 0x07),       /* 0x22: MBOX_GET_RETRY_COUNT   */
7074         ISP_FC_OPMAP(0x00, 0x00),       /* 0x23: */
7075         ISP_FC_OPMAP(0x00, 0x00),       /* 0x24: */
7076         ISP_FC_OPMAP(0x00, 0x00),       /* 0x25: */
7077         ISP_FC_OPMAP(0x00, 0x00),       /* 0x26: */
7078         ISP_FC_OPMAP(0x00, 0x00),       /* 0x27: */
7079         ISP_FC_OPMAP(0x01, 0x03),       /* 0x28: MBOX_GET_FIRMWARE_OPTIONS */
7080         ISP_FC_OPMAP(0x03, 0x07),       /* 0x29: MBOX_GET_PORT_QUEUE_PARAMS */
7081         ISP_FC_OPMAP(0x00, 0x00),       /* 0x2a: */
7082         ISP_FC_OPMAP(0x00, 0x00),       /* 0x2b: */
7083         ISP_FC_OPMAP(0x00, 0x00),       /* 0x2c: */
7084         ISP_FC_OPMAP(0x00, 0x00),       /* 0x2d: */
7085         ISP_FC_OPMAP(0x00, 0x00),       /* 0x2e: */
7086         ISP_FC_OPMAP(0x00, 0x00),       /* 0x2f: */
7087         ISP_FC_OPMAP(0x00, 0x00),       /* 0x30: */
7088         ISP_FC_OPMAP(0x00, 0x00),       /* 0x31: */
7089         ISP_FC_OPMAP(0x07, 0x07),       /* 0x32: MBOX_SET_RETRY_COUNT   */
7090         ISP_FC_OPMAP(0x00, 0x00),       /* 0x33: */
7091         ISP_FC_OPMAP(0x00, 0x00),       /* 0x34: */
7092         ISP_FC_OPMAP(0x00, 0x00),       /* 0x35: */
7093         ISP_FC_OPMAP(0x00, 0x00),       /* 0x36: */
7094         ISP_FC_OPMAP(0x00, 0x00),       /* 0x37: */
7095         ISP_FC_OPMAP(0x0f, 0x01),       /* 0x38: MBOX_SET_FIRMWARE_OPTIONS */
7096         ISP_FC_OPMAP(0x0f, 0x07),       /* 0x39: MBOX_SET_PORT_QUEUE_PARAMS */
7097         ISP_FC_OPMAP(0x00, 0x00),       /* 0x3a: */
7098         ISP_FC_OPMAP(0x00, 0x00),       /* 0x3b: */
7099         ISP_FC_OPMAP(0x00, 0x00),       /* 0x3c: */
7100         ISP_FC_OPMAP(0x00, 0x00),       /* 0x3d: */
7101         ISP_FC_OPMAP(0x00, 0x00),       /* 0x3e: */
7102         ISP_FC_OPMAP(0x00, 0x00),       /* 0x3f: */
7103         ISP_FC_OPMAP(0x03, 0x01),       /* 0x40: MBOX_LOOP_PORT_BYPASS */
7104         ISP_FC_OPMAP(0x03, 0x01),       /* 0x41: MBOX_LOOP_PORT_ENABLE */
7105         ISP_FC_OPMAP_HALF(0x0, 0x01, 0x3, 0xcf),        /* 0x42: MBOX_GET_RESOURCE_COUNT */
7106         ISP_FC_OPMAP(0x01, 0x01),       /* 0x43: MBOX_REQUEST_OFFLINE_MODE */
7107         ISP_FC_OPMAP(0x00, 0x00),       /* 0x44: */
7108         ISP_FC_OPMAP(0x00, 0x00),       /* 0x45: */
7109         ISP_FC_OPMAP(0x00, 0x00),       /* 0x46: */
7110         ISP_FC_OPMAP(0xcf, 0x03),       /* 0x47: GET PORT_DATABASE ENHANCED */
7111         ISP_FC_OPMAP(0xcd, 0x01),       /* 0x48: MBOX_INIT_FIRMWARE_MULTI_ID */
7112         ISP_FC_OPMAP(0xcd, 0x01),       /* 0x49: MBOX_GET_VP_DATABASE */
7113         ISP_FC_OPMAP_HALF(0x2, 0xcd, 0x0, 0x01),        /* 0x4a: MBOX_GET_VP_DATABASE_ENTRY */
7114         ISP_FC_OPMAP(0x00, 0x00),       /* 0x4b: */
7115         ISP_FC_OPMAP(0x00, 0x00),       /* 0x4c: */
7116         ISP_FC_OPMAP(0x00, 0x00),       /* 0x4d: */
7117         ISP_FC_OPMAP(0x00, 0x00),       /* 0x4e: */
7118         ISP_FC_OPMAP(0x00, 0x00),       /* 0x4f: */
7119         ISP_FC_OPMAP(0x00, 0x00),       /* 0x50: */
7120         ISP_FC_OPMAP(0x00, 0x00),       /* 0x51: */
7121         ISP_FC_OPMAP(0x00, 0x00),       /* 0x52: */
7122         ISP_FC_OPMAP(0x00, 0x00),       /* 0x53: */
7123         ISP_FC_OPMAP(0xcf, 0x01),       /* 0x54: EXECUTE IOCB A64 */
7124         ISP_FC_OPMAP(0x00, 0x00),       /* 0x55: */
7125         ISP_FC_OPMAP(0x00, 0x00),       /* 0x56: */
7126         ISP_FC_OPMAP(0x00, 0x00),       /* 0x57: */
7127         ISP_FC_OPMAP(0x00, 0x00),       /* 0x58: */
7128         ISP_FC_OPMAP(0x00, 0x00),       /* 0x59: */
7129         ISP_FC_OPMAP(0x00, 0x00),       /* 0x5a: */
7130         ISP_FC_OPMAP(0x03, 0x01),       /* 0x5b: MBOX_DRIVER_HEARTBEAT */
7131         ISP_FC_OPMAP(0xcf, 0x01),       /* 0x5c: MBOX_FW_HEARTBEAT */
7132         ISP_FC_OPMAP(0x07, 0x03),       /* 0x5d: MBOX_GET_SET_DATA_RATE */
7133         ISP_FC_OPMAP(0x00, 0x00),       /* 0x5e: */
7134         ISP_FC_OPMAP(0x00, 0x00),       /* 0x5f: */
7135         ISP_FC_OPMAP(0xcd, 0x01),       /* 0x60: MBOX_INIT_FIRMWARE */
7136         ISP_FC_OPMAP(0x00, 0x00),       /* 0x61: */
7137         ISP_FC_OPMAP(0x01, 0x01),       /* 0x62: MBOX_INIT_LIP */
7138         ISP_FC_OPMAP(0xcd, 0x03),       /* 0x63: MBOX_GET_FC_AL_POSITION_MAP */
7139         ISP_FC_OPMAP(0xcf, 0x01),       /* 0x64: MBOX_GET_PORT_DB */
7140         ISP_FC_OPMAP(0x07, 0x01),       /* 0x65: MBOX_CLEAR_ACA */
7141         ISP_FC_OPMAP(0x07, 0x01),       /* 0x66: MBOX_TARGET_RESET */
7142         ISP_FC_OPMAP(0x07, 0x01),       /* 0x67: MBOX_CLEAR_TASK_SET */
7143         ISP_FC_OPMAP(0x07, 0x01),       /* 0x68: MBOX_ABORT_TASK_SET */
7144         ISP_FC_OPMAP(0x01, 0x07),       /* 0x69: MBOX_GET_FW_STATE */
7145         ISP_FC_OPMAP_HALF(0x6, 0x03, 0x0, 0xcf),        /* 0x6a: MBOX_GET_PORT_NAME */
7146         ISP_FC_OPMAP(0xcf, 0x01),       /* 0x6b: MBOX_GET_LINK_STATUS */
7147         ISP_FC_OPMAP(0x0f, 0x01),       /* 0x6c: MBOX_INIT_LIP_RESET */
7148         ISP_FC_OPMAP(0x00, 0x00),       /* 0x6d: */
7149         ISP_FC_OPMAP(0xcf, 0x03),       /* 0x6e: MBOX_SEND_SNS */
7150         ISP_FC_OPMAP(0x0f, 0x07),       /* 0x6f: MBOX_FABRIC_LOGIN */
7151         ISP_FC_OPMAP(0x03, 0x01),       /* 0x70: MBOX_SEND_CHANGE_REQUEST */
7152         ISP_FC_OPMAP(0x03, 0x03),       /* 0x71: MBOX_FABRIC_LOGOUT */
7153         ISP_FC_OPMAP(0x0f, 0x0f),       /* 0x72: MBOX_INIT_LIP_LOGIN */
7154         ISP_FC_OPMAP(0x00, 0x00),       /* 0x73: */
7155         ISP_FC_OPMAP(0x07, 0x01),       /* 0x74: LOGIN LOOP PORT */
7156         ISP_FC_OPMAP(0xcf, 0x03),       /* 0x75: GET PORT/NODE NAME LIST */
7157         ISP_FC_OPMAP(0x4f, 0x01),       /* 0x76: SET VENDOR ID */
7158         ISP_FC_OPMAP(0xcd, 0x01),       /* 0x77: INITIALIZE IP MAILBOX */
7159         ISP_FC_OPMAP(0x00, 0x00),       /* 0x78: */
7160         ISP_FC_OPMAP(0x00, 0x00),       /* 0x79: */
7161         ISP_FC_OPMAP(0x00, 0x00),       /* 0x7a: */
7162         ISP_FC_OPMAP(0x00, 0x00),       /* 0x7b: */
7163         ISP_FC_OPMAP(0x4f, 0x03),       /* 0x7c: Get ID List */
7164         ISP_FC_OPMAP(0xcf, 0x01),       /* 0x7d: SEND LFA */
7165         ISP_FC_OPMAP(0x0f, 0x01)        /* 0x7e: LUN RESET */
7166 };
7167 #define MAX_FC_OPCODE   0x7e
7168 /*
7169  * Footnotes
7170  *
7171  * (1): this sets bits 21..16 in mailbox register #8, which we nominally
7172  *      do not access at this time in the core driver. The caller is
7173  *      responsible for setting this register first (Gross!). The assumption
7174  *      is that we won't overflow.
7175  */
7176
7177 static const char *fc_mbcmd_names[] = {
7178         "NO-OP",
7179         "LOAD RAM",
7180         "EXEC FIRMWARE",
7181         "DUMP RAM",
7182         "WRITE RAM WORD",
7183         "READ RAM WORD",
7184         "MAILBOX REG TEST",
7185         "VERIFY CHECKSUM",
7186         "ABOUT FIRMWARE",
7187         "LOAD RAM (2100)",
7188         "DUMP RAM",
7189         "LOAD RISC RAM",
7190         NULL,
7191         "WRITE RAM WORD EXTENDED",
7192         "CHECK FIRMWARE",
7193         "READ RAM WORD EXTENDED",
7194         "INIT REQUEST QUEUE",
7195         "INIT RESULT QUEUE",
7196         "EXECUTE IOCB",
7197         "WAKE UP",
7198         "STOP FIRMWARE",
7199         "ABORT",
7200         "ABORT DEVICE",
7201         "ABORT TARGET",
7202         "BUS RESET",
7203         "STOP QUEUE",
7204         "START QUEUE",
7205         "SINGLE STEP QUEUE",
7206         "ABORT QUEUE",
7207         "GET DEV QUEUE STATUS",
7208         NULL,
7209         "GET FIRMWARE STATUS",
7210         "GET LOOP ID",
7211         NULL,
7212         "GET RETRY COUNT",
7213         NULL,
7214         NULL,
7215         NULL,
7216         NULL,
7217         NULL,
7218         "GET FIRMWARE OPTIONS",
7219         "GET PORT QUEUE PARAMS",
7220         NULL,
7221         NULL,
7222         NULL,
7223         NULL,
7224         NULL,
7225         NULL,
7226         NULL,
7227         NULL,
7228         "SET RETRY COUNT",
7229         NULL,
7230         NULL,
7231         NULL,
7232         NULL,
7233         NULL,
7234         "SET FIRMWARE OPTIONS",
7235         "SET PORT QUEUE PARAMS",
7236         NULL,
7237         NULL,
7238         NULL,
7239         NULL,
7240         NULL,
7241         NULL,
7242         "LOOP PORT BYPASS",
7243         "LOOP PORT ENABLE",
7244         "GET RESOURCE COUNT",
7245         "REQUEST NON PARTICIPATING MODE",
7246         NULL,
7247         NULL,
7248         NULL,
7249         "GET PORT DATABASE ENHANCED",
7250         "INIT FIRMWARE MULTI ID",
7251         "GET VP DATABASE",
7252         "GET VP DATABASE ENTRY",
7253         NULL,
7254         NULL,
7255         NULL,
7256         NULL,
7257         NULL,
7258         NULL,
7259         NULL,
7260         NULL,
7261         NULL,
7262         "EXECUTE IOCB A64",
7263         NULL,
7264         NULL,
7265         NULL,
7266         NULL,
7267         NULL,
7268         NULL,
7269         "DRIVER HEARTBEAT",
7270         NULL,
7271         "GET/SET DATA RATE",
7272         NULL,
7273         NULL,
7274         "INIT FIRMWARE",
7275         NULL,
7276         "INIT LIP",
7277         "GET FC-AL POSITION MAP",
7278         "GET PORT DATABASE",
7279         "CLEAR ACA",
7280         "TARGET RESET",
7281         "CLEAR TASK SET",
7282         "ABORT TASK SET",
7283         "GET FW STATE",
7284         "GET PORT NAME",
7285         "GET LINK STATUS",
7286         "INIT LIP RESET",
7287         NULL,
7288         "SEND SNS",
7289         "FABRIC LOGIN",
7290         "SEND CHANGE REQUEST",
7291         "FABRIC LOGOUT",
7292         "INIT LIP LOGIN",
7293         NULL,
7294         "LOGIN LOOP PORT",
7295         "GET PORT/NODE NAME LIST",
7296         "SET VENDOR ID",
7297         "INITIALIZE IP MAILBOX",
7298         NULL,
7299         NULL,
7300         NULL,
7301         NULL,
7302         "Get ID List",
7303         "SEND LFA",
7304         "Lun RESET"
7305 };
7306
7307 static void
7308 isp_mboxcmd_qnw(ispsoftc_t *isp, mbreg_t *mbp, int nodelay)
7309 {
7310         unsigned int ibits, obits, box, opcode;
7311
7312         opcode = mbp->param[0];
7313         if (IS_FC(isp)) {
7314                 ibits = ISP_FC_IBITS(opcode);
7315                 obits = ISP_FC_OBITS(opcode);
7316         } else {
7317                 ibits = ISP_SCSI_IBITS(opcode);
7318                 obits = ISP_SCSI_OBITS(opcode);
7319         }
7320         ibits |= mbp->ibits;
7321         obits |= mbp->obits;
7322         for (box = 0; box < ISP_NMBOX(isp); box++) {
7323                 if (ibits & (1 << box)) {
7324                         ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
7325                 }
7326                 if (nodelay == 0) {
7327                         isp->isp_mboxtmp[box] = mbp->param[box] = 0;
7328                 }
7329         }
7330         if (nodelay == 0) {
7331                 isp->isp_lastmbxcmd = opcode;
7332                 isp->isp_obits = obits;
7333                 isp->isp_mboxbsy = 1;
7334         }
7335         if (IS_24XX(isp)) {
7336                 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_SET_HOST_INT);
7337         } else {
7338                 ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
7339         }
7340         /*
7341          * Oddly enough, if we're not delaying for an answer,
7342          * delay a bit to give the f/w a chance to pick up the
7343          * command.
7344          */
7345         if (nodelay) {
7346                 ISP_DELAY(1000);
7347         }
7348 }
7349
7350 static void
7351 isp_mboxcmd(ispsoftc_t *isp, mbreg_t *mbp)
7352 {
7353         const char *cname, *xname;
7354         char tname[16], mname[16];
7355         unsigned int ibits, obits, box, opcode;
7356
7357         opcode = mbp->param[0];
7358         if (IS_FC(isp)) {
7359                 if (opcode > MAX_FC_OPCODE) {
7360                         mbp->param[0] = MBOX_INVALID_COMMAND;
7361                         isp_prt(isp, ISP_LOGERR, "Unknown Command 0x%x", opcode);
7362                         return;
7363                 }
7364                 cname = fc_mbcmd_names[opcode];
7365                 ibits = ISP_FC_IBITS(opcode);
7366                 obits = ISP_FC_OBITS(opcode);
7367         } else {
7368                 if (opcode > MAX_SCSI_OPCODE) {
7369                         mbp->param[0] = MBOX_INVALID_COMMAND;
7370                         isp_prt(isp, ISP_LOGERR, "Unknown Command 0x%x", opcode);
7371                         return;
7372                 }
7373                 cname = scsi_mbcmd_names[opcode];
7374                 ibits = ISP_SCSI_IBITS(opcode);
7375                 obits = ISP_SCSI_OBITS(opcode);
7376         }
7377         if (cname == NULL) {
7378                 cname = tname;
7379                 ISP_SNPRINTF(tname, sizeof tname, "opcode %x", opcode);
7380         }
7381         isp_prt(isp, ISP_LOGDEBUG3, "Mailbox Command '%s'", cname);
7382
7383         /*
7384          * Pick up any additional bits that the caller might have set.
7385          */
7386         ibits |= mbp->ibits;
7387         obits |= mbp->obits;
7388
7389         /*
7390          * Mask any bits that the caller wants us to mask
7391          */
7392         ibits &= mbp->ibitm;
7393         obits &= mbp->obitm;
7394
7395
7396         if (ibits == 0 && obits == 0) {
7397                 mbp->param[0] = MBOX_COMMAND_PARAM_ERROR;
7398                 isp_prt(isp, ISP_LOGERR, "no parameters for 0x%x", opcode);
7399                 return;
7400         }
7401
7402         /*
7403          * Get exclusive usage of mailbox registers.
7404          */
7405         if (MBOX_ACQUIRE(isp)) {
7406                 mbp->param[0] = MBOX_REGS_BUSY;
7407                 goto out;
7408         }
7409
7410         for (box = 0; box < ISP_NMBOX(isp); box++) {
7411                 if (ibits & (1 << box)) {
7412                         isp_prt(isp, ISP_LOGDEBUG3, "IN mbox %d = 0x%04x", box,
7413                             mbp->param[box]);
7414                         ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
7415                 }
7416                 isp->isp_mboxtmp[box] = mbp->param[box] = 0;
7417         }
7418
7419         isp->isp_lastmbxcmd = opcode;
7420
7421         /*
7422          * We assume that we can't overwrite a previous command.
7423          */
7424         isp->isp_obits = obits;
7425         isp->isp_mboxbsy = 1;
7426
7427         /*
7428          * Set Host Interrupt condition so that RISC will pick up mailbox regs.
7429          */
7430         if (IS_24XX(isp)) {
7431                 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_SET_HOST_INT);
7432         } else {
7433                 ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
7434         }
7435
7436         /*
7437          * While we haven't finished the command, spin our wheels here.
7438          */
7439         MBOX_WAIT_COMPLETE(isp, mbp);
7440
7441         /*
7442          * Did the command time out?
7443          */
7444         if (mbp->param[0] == MBOX_TIMEOUT) {
7445                 isp->isp_mboxbsy = 0;
7446                 MBOX_RELEASE(isp);
7447                 goto out;
7448         }
7449
7450         /*
7451          * Copy back output registers.
7452          */
7453         for (box = 0; box < ISP_NMBOX(isp); box++) {
7454                 if (obits & (1 << box)) {
7455                         mbp->param[box] = isp->isp_mboxtmp[box];
7456                         isp_prt(isp, ISP_LOGDEBUG3, "OUT mbox %d = 0x%04x", box,
7457                             mbp->param[box]);
7458                 }
7459         }
7460
7461         isp->isp_mboxbsy = 0;
7462         MBOX_RELEASE(isp);
7463  out:
7464         if (mbp->logval == 0 || opcode == MBOX_EXEC_FIRMWARE) {
7465                 return;
7466         }
7467
7468         /*
7469          * Just to be chatty here...
7470          */
7471         xname = NULL;
7472         switch (mbp->param[0]) {
7473         case MBOX_COMMAND_COMPLETE:
7474                 break;
7475         case MBOX_INVALID_COMMAND:
7476                 if (mbp->logval & MBLOGMASK(MBOX_COMMAND_COMPLETE)) {
7477                         xname = "INVALID COMMAND";
7478                 }
7479                 break;
7480         case MBOX_HOST_INTERFACE_ERROR:
7481                 if (mbp->logval & MBLOGMASK(MBOX_HOST_INTERFACE_ERROR)) {
7482                         xname = "HOST INTERFACE ERROR";
7483                 }
7484                 break;
7485         case MBOX_TEST_FAILED:
7486                 if (mbp->logval & MBLOGMASK(MBOX_TEST_FAILED)) {
7487                         xname = "TEST FAILED";
7488                 }
7489                 break;
7490         case MBOX_COMMAND_ERROR:
7491                 if (mbp->logval & MBLOGMASK(MBOX_COMMAND_ERROR)) {
7492                         xname = "COMMAND ERROR";
7493                 }
7494                 break;
7495         case MBOX_COMMAND_PARAM_ERROR:
7496                 if (mbp->logval & MBLOGMASK(MBOX_COMMAND_PARAM_ERROR)) {
7497                         xname = "COMMAND PARAMETER ERROR";
7498                 }
7499                 break;
7500         case MBOX_LOOP_ID_USED:
7501                 if (mbp->logval & MBLOGMASK(MBOX_LOOP_ID_USED)) {
7502                         xname = "LOOP ID ALREADY IN USE";
7503                 }
7504                 break;
7505         case MBOX_PORT_ID_USED:
7506                 if (mbp->logval & MBLOGMASK(MBOX_PORT_ID_USED)) {
7507                         xname = "PORT ID ALREADY IN USE";
7508                 }
7509                 break;
7510         case MBOX_ALL_IDS_USED:
7511                 if (mbp->logval & MBLOGMASK(MBOX_ALL_IDS_USED)) {
7512                         xname = "ALL LOOP IDS IN USE";
7513                 }
7514                 break;
7515         case MBOX_REGS_BUSY:
7516                 xname = "REGISTERS BUSY";
7517                 break;
7518         case MBOX_TIMEOUT:
7519                 xname = "TIMEOUT";
7520                 break;
7521         default:
7522                 ISP_SNPRINTF(mname, sizeof mname, "error 0x%x", mbp->param[0]);
7523                 xname = mname;
7524                 break;
7525         }
7526         if (xname) {
7527                 isp_prt(isp, ISP_LOGALL, "Mailbox Command '%s' failed (%s)",
7528                     cname, xname);
7529         }
7530 }
7531
7532 static void
7533 isp_fw_state(ispsoftc_t *isp, int chan)
7534 {
7535         if (IS_FC(isp)) {
7536                 mbreg_t mbs;
7537                 fcparam *fcp = FCPARAM(isp, chan);
7538
7539                 MBSINIT(&mbs, MBOX_GET_FW_STATE, MBLOGALL, 0);
7540                 isp_mboxcmd(isp, &mbs);
7541                 if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
7542                         fcp->isp_fwstate = mbs.param[1];
7543                 }
7544         }
7545 }
7546
7547 static void
7548 isp_spi_update(ispsoftc_t *isp, int chan)
7549 {
7550         int tgt;
7551         mbreg_t mbs;
7552         sdparam *sdp;
7553
7554         if (IS_FC(isp)) {
7555                 /*
7556                  * There are no 'per-bus' settings for Fibre Channel.
7557                  */
7558                 return;
7559         }
7560         sdp = SDPARAM(isp, chan);
7561         sdp->update = 0;
7562
7563         for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
7564                 uint16_t flags, period, offset;
7565                 int get;
7566
7567                 if (sdp->isp_devparam[tgt].dev_enable == 0) {
7568                         sdp->isp_devparam[tgt].dev_update = 0;
7569                         sdp->isp_devparam[tgt].dev_refresh = 0;
7570                         isp_prt(isp, ISP_LOGDEBUG0, "skipping target %d bus %d update", tgt, chan);
7571                         continue;
7572                 }
7573                 /*
7574                  * If the goal is to update the status of the device,
7575                  * take what's in goal_flags and try and set the device
7576                  * toward that. Otherwise, if we're just refreshing the
7577                  * current device state, get the current parameters.
7578                  */
7579
7580                 MBSINIT(&mbs, 0, MBLOGALL, 0);
7581
7582                 /*
7583                  * Refresh overrides set
7584                  */
7585                 if (sdp->isp_devparam[tgt].dev_refresh) {
7586                         mbs.param[0] = MBOX_GET_TARGET_PARAMS;
7587                         get = 1;
7588                 } else if (sdp->isp_devparam[tgt].dev_update) {
7589                         mbs.param[0] = MBOX_SET_TARGET_PARAMS;
7590
7591                         /*
7592                          * Make sure goal_flags has "Renegotiate on Error"
7593                          * on and "Freeze Queue on Error" off.
7594                          */
7595                         sdp->isp_devparam[tgt].goal_flags |= DPARM_RENEG;
7596                         sdp->isp_devparam[tgt].goal_flags &= ~DPARM_QFRZ;
7597                         mbs.param[2] = sdp->isp_devparam[tgt].goal_flags;
7598
7599                         /*
7600                          * Insist that PARITY must be enabled
7601                          * if SYNC or WIDE is enabled.
7602                          */
7603                         if ((mbs.param[2] & (DPARM_SYNC|DPARM_WIDE)) != 0) {
7604                                 mbs.param[2] |= DPARM_PARITY;
7605                         }
7606
7607                         if (mbs.param[2] & DPARM_SYNC) {
7608                                 mbs.param[3] =
7609                                     (sdp->isp_devparam[tgt].goal_offset << 8) |
7610                                     (sdp->isp_devparam[tgt].goal_period);
7611                         }
7612                         /*
7613                          * A command completion later that has
7614                          * RQSTF_NEGOTIATION set can cause
7615                          * the dev_refresh/announce cycle also.
7616                          *
7617                          * Note: It is really important to update our current
7618                          * flags with at least the state of TAG capabilities-
7619                          * otherwise we might try and send a tagged command
7620                          * when we have it all turned off. So change it here
7621                          * to say that current already matches goal.
7622                          */
7623                         sdp->isp_devparam[tgt].actv_flags &= ~DPARM_TQING;
7624                         sdp->isp_devparam[tgt].actv_flags |=
7625                             (sdp->isp_devparam[tgt].goal_flags & DPARM_TQING);
7626                         isp_prt(isp, ISP_LOGDEBUG0, "bus %d set tgt %d flags 0x%x off 0x%x period 0x%x",
7627                             chan, tgt, mbs.param[2], mbs.param[3] >> 8, mbs.param[3] & 0xff);
7628                         get = 0;
7629                 } else {
7630                         continue;
7631                 }
7632                 mbs.param[1] = (chan << 15) | (tgt << 8);
7633                 isp_mboxcmd(isp, &mbs);
7634                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
7635                         continue;
7636                 }
7637                 if (get == 0) {
7638                         sdp->sendmarker = 1;
7639                         sdp->isp_devparam[tgt].dev_update = 0;
7640                         sdp->isp_devparam[tgt].dev_refresh = 1;
7641                 } else {
7642                         sdp->isp_devparam[tgt].dev_refresh = 0;
7643                         flags = mbs.param[2];
7644                         period = mbs.param[3] & 0xff;
7645                         offset = mbs.param[3] >> 8;
7646                         sdp->isp_devparam[tgt].actv_flags = flags;
7647                         sdp->isp_devparam[tgt].actv_period = period;
7648                         sdp->isp_devparam[tgt].actv_offset = offset;
7649                         isp_async(isp, ISPASYNC_NEW_TGT_PARAMS, chan, tgt);
7650                 }
7651         }
7652
7653         for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
7654                 if (sdp->isp_devparam[tgt].dev_update ||
7655                     sdp->isp_devparam[tgt].dev_refresh) {
7656                         sdp->update = 1;
7657                         break;
7658                 }
7659         }
7660 }
7661
7662 static void
7663 isp_setdfltsdparm(ispsoftc_t *isp)
7664 {
7665         int tgt;
7666         sdparam *sdp, *sdp1;
7667
7668         sdp = SDPARAM(isp, 0);
7669         sdp->role = GET_DEFAULT_ROLE(isp, 0);
7670         if (IS_DUALBUS(isp)) {
7671                 sdp1 = sdp + 1;
7672                 sdp1->role = GET_DEFAULT_ROLE(isp, 1);
7673         } else {
7674                 sdp1 = NULL;
7675         }
7676
7677         /*
7678          * Establish some default parameters.
7679          */
7680         sdp->isp_cmd_dma_burst_enable = 0;
7681         sdp->isp_data_dma_burst_enabl = 1;
7682         sdp->isp_fifo_threshold = 0;
7683         sdp->isp_initiator_id = DEFAULT_IID(isp, 0);
7684         if (isp->isp_type >= ISP_HA_SCSI_1040) {
7685                 sdp->isp_async_data_setup = 9;
7686         } else {
7687                 sdp->isp_async_data_setup = 6;
7688         }
7689         sdp->isp_selection_timeout = 250;
7690         sdp->isp_max_queue_depth = MAXISPREQUEST(isp);
7691         sdp->isp_tag_aging = 8;
7692         sdp->isp_bus_reset_delay = 5;
7693         /*
7694          * Don't retry selection, busy or queue full automatically- reflect
7695          * these back to us.
7696          */
7697         sdp->isp_retry_count = 0;
7698         sdp->isp_retry_delay = 0;
7699
7700         for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
7701                 sdp->isp_devparam[tgt].exc_throttle = ISP_EXEC_THROTTLE;
7702                 sdp->isp_devparam[tgt].dev_enable = 1;
7703         }
7704
7705         /*
7706          * The trick here is to establish a default for the default (honk!)
7707          * state (goal_flags). Then try and get the current status from
7708          * the card to fill in the current state. We don't, in fact, set
7709          * the default to the SAFE default state- that's not the goal state.
7710          */
7711         for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
7712                 uint8_t off, per;
7713                 sdp->isp_devparam[tgt].actv_offset = 0;
7714                 sdp->isp_devparam[tgt].actv_period = 0;
7715                 sdp->isp_devparam[tgt].actv_flags = 0;
7716
7717                 sdp->isp_devparam[tgt].goal_flags =
7718                     sdp->isp_devparam[tgt].nvrm_flags = DPARM_DEFAULT;
7719
7720                 /*
7721                  * We default to Wide/Fast for versions less than a 1040
7722                  * (unless it's SBus).
7723                  */
7724                 if (IS_ULTRA3(isp)) {
7725                         off = ISP_80M_SYNCPARMS >> 8;
7726                         per = ISP_80M_SYNCPARMS & 0xff;
7727                 } else if (IS_ULTRA2(isp)) {
7728                         off = ISP_40M_SYNCPARMS >> 8;
7729                         per = ISP_40M_SYNCPARMS & 0xff;
7730                 } else if (IS_1240(isp)) {
7731                         off = ISP_20M_SYNCPARMS >> 8;
7732                         per = ISP_20M_SYNCPARMS & 0xff;
7733                 } else if ((isp->isp_bustype == ISP_BT_SBUS &&
7734                     isp->isp_type < ISP_HA_SCSI_1020A) ||
7735                     (isp->isp_bustype == ISP_BT_PCI &&
7736                     isp->isp_type < ISP_HA_SCSI_1040) ||
7737                     (isp->isp_clock && isp->isp_clock < 60) ||
7738                     (sdp->isp_ultramode == 0)) {
7739                         off = ISP_10M_SYNCPARMS >> 8;
7740                         per = ISP_10M_SYNCPARMS & 0xff;
7741                 } else {
7742                         off = ISP_20M_SYNCPARMS_1040 >> 8;
7743                         per = ISP_20M_SYNCPARMS_1040 & 0xff;
7744                 }
7745                 sdp->isp_devparam[tgt].goal_offset =
7746                     sdp->isp_devparam[tgt].nvrm_offset = off;
7747                 sdp->isp_devparam[tgt].goal_period =
7748                     sdp->isp_devparam[tgt].nvrm_period = per;
7749
7750         }
7751
7752         /*
7753          * If we're a dual bus card, just copy the data over
7754          */
7755         if (sdp1) {
7756                 *sdp1 = *sdp;
7757                 sdp1->isp_initiator_id = DEFAULT_IID(isp, 1);
7758         }
7759
7760         /*
7761          * If we've not been told to avoid reading NVRAM, try and read it.
7762          * If we're successful reading it, we can then return because NVRAM
7763          * will tell us what the desired settings are. Otherwise, we establish
7764          * some reasonable 'fake' nvram and goal defaults.
7765          */
7766         if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
7767                 mbreg_t mbs;
7768
7769                 if (isp_read_nvram(isp, 0) == 0) {
7770                         if (IS_DUALBUS(isp)) {
7771                                 if (isp_read_nvram(isp, 1) == 0) {
7772                                         return;
7773                                 }
7774                         }
7775                 }
7776                 MBSINIT(&mbs, MBOX_GET_ACT_NEG_STATE, MBLOGNONE, 0);
7777                 isp_mboxcmd(isp, &mbs);
7778                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
7779                         sdp->isp_req_ack_active_neg = 1;
7780                         sdp->isp_data_line_active_neg = 1;
7781                         if (sdp1) {
7782                                 sdp1->isp_req_ack_active_neg = 1;
7783                                 sdp1->isp_data_line_active_neg = 1;
7784                         }
7785                 } else {
7786                         sdp->isp_req_ack_active_neg =
7787                             (mbs.param[1] >> 4) & 0x1;
7788                         sdp->isp_data_line_active_neg =
7789                             (mbs.param[1] >> 5) & 0x1;
7790                         if (sdp1) {
7791                                 sdp1->isp_req_ack_active_neg =
7792                                     (mbs.param[2] >> 4) & 0x1;
7793                                 sdp1->isp_data_line_active_neg =
7794                                     (mbs.param[2] >> 5) & 0x1;
7795                         }
7796                 }
7797         }
7798
7799 }
7800
7801 static void
7802 isp_setdfltfcparm(ispsoftc_t *isp, int chan)
7803 {
7804         fcparam *fcp = FCPARAM(isp, chan);
7805
7806         /*
7807          * Establish some default parameters.
7808          */
7809         fcp->role = GET_DEFAULT_ROLE(isp, chan);
7810         fcp->isp_maxalloc = ICB_DFLT_ALLOC;
7811         fcp->isp_retry_delay = ICB_DFLT_RDELAY;
7812         fcp->isp_retry_count = ICB_DFLT_RCOUNT;
7813         fcp->isp_loopid = DEFAULT_LOOPID(isp, chan);
7814         fcp->isp_wwnn_nvram = DEFAULT_NODEWWN(isp, chan);
7815         fcp->isp_wwpn_nvram = DEFAULT_PORTWWN(isp, chan);
7816         fcp->isp_fwoptions = 0;
7817         fcp->isp_lasthdl = NIL_HANDLE;
7818
7819         if (IS_24XX(isp)) {
7820                 fcp->isp_fwoptions |= ICB2400_OPT1_FAIRNESS;
7821                 fcp->isp_fwoptions |= ICB2400_OPT1_HARD_ADDRESS;
7822                 if (isp->isp_confopts & ISP_CFG_FULL_DUPLEX) {
7823                         fcp->isp_fwoptions |= ICB2400_OPT1_FULL_DUPLEX;
7824                 }
7825                 fcp->isp_fwoptions |= ICB2400_OPT1_BOTH_WWNS;
7826         } else {
7827                 fcp->isp_fwoptions |= ICBOPT_FAIRNESS;
7828                 fcp->isp_fwoptions |= ICBOPT_PDBCHANGE_AE;
7829                 fcp->isp_fwoptions |= ICBOPT_HARD_ADDRESS;
7830                 if (isp->isp_confopts & ISP_CFG_FULL_DUPLEX) {
7831                         fcp->isp_fwoptions |= ICBOPT_FULL_DUPLEX;
7832                 }
7833                 /*
7834                  * Make sure this is turned off now until we get
7835                  * extended options from NVRAM
7836                  */
7837                 fcp->isp_fwoptions &= ~ICBOPT_EXTENDED;
7838         }
7839
7840
7841         /*
7842          * Now try and read NVRAM unless told to not do so.
7843          * This will set fcparam's isp_wwnn_nvram && isp_wwpn_nvram.
7844          */
7845         if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
7846                 int i, j = 0;
7847                 /*
7848                  * Give a couple of tries at reading NVRAM.
7849                  */
7850                 for (i = 0; i < 2; i++) {
7851                         j = isp_read_nvram(isp, chan);
7852                         if (j == 0) {
7853                                 break;
7854                         }
7855                 }
7856                 if (j) {
7857                         isp->isp_confopts |= ISP_CFG_NONVRAM;
7858                 }
7859         }
7860
7861         fcp->isp_wwnn = ACTIVE_NODEWWN(isp, chan);
7862         fcp->isp_wwpn = ACTIVE_PORTWWN(isp, chan);
7863         isp_prt(isp, ISP_LOGCONFIG, "Chan %d 0x%08x%08x/0x%08x%08x Role %s",
7864             chan, (uint32_t) (fcp->isp_wwnn >> 32), (uint32_t) (fcp->isp_wwnn),
7865             (uint32_t) (fcp->isp_wwpn >> 32), (uint32_t) (fcp->isp_wwpn),
7866             isp_class3_roles[fcp->role]);
7867 }
7868
7869 /*
7870  * Re-initialize the ISP and complete all orphaned commands
7871  * with a 'botched' notice. The reset/init routines should
7872  * not disturb an already active list of commands.
7873  */
7874
7875 int
7876 isp_reinit(ispsoftc_t *isp, int do_load_defaults)
7877 {
7878         int i, res = 0;
7879
7880         isp_reset(isp, do_load_defaults);
7881         if (isp->isp_state != ISP_RESETSTATE) {
7882                 res = EIO;
7883                 isp_prt(isp, ISP_LOGERR, "%s: cannot reset card", __func__);
7884                 ISP_DISABLE_INTS(isp);
7885                 goto cleanup;
7886         }
7887
7888         isp_init(isp);
7889         if (isp->isp_state == ISP_INITSTATE) {
7890                 isp->isp_state = ISP_RUNSTATE;
7891         }
7892
7893         if (isp->isp_state != ISP_RUNSTATE) {
7894                 res = EIO;
7895 #ifndef ISP_TARGET_MODE
7896                 isp_prt(isp, ISP_LOGWARN, "%s: not at runstate", __func__);
7897 #endif
7898                 ISP_DISABLE_INTS(isp);
7899                 if (IS_FC(isp)) {
7900                         /*
7901                          * If we're in ISP_ROLE_NONE, turn off the lasers.
7902                          */
7903                         if (!IS_24XX(isp)) {
7904                                 ISP_WRITE(isp, BIU2100_CSR, BIU2100_FPM0_REGS);
7905                                 ISP_WRITE(isp, FPM_DIAG_CONFIG, FPM_SOFT_RESET);
7906                                 ISP_WRITE(isp, BIU2100_CSR, BIU2100_FB_REGS);
7907                                 ISP_WRITE(isp, FBM_CMD, FBMCMD_FIFO_RESET_ALL);
7908                                 ISP_WRITE(isp, BIU2100_CSR, BIU2100_RISC_REGS);
7909                         }
7910                 }
7911         }
7912
7913  cleanup:
7914         isp->isp_nactive = 0;
7915         isp_clear_commands(isp);
7916         if (IS_FC(isp)) {
7917                 for (i = 0; i < isp->isp_nchan; i++)
7918                         ISP_MARK_PORTDB(isp, i, -1);
7919         }
7920         return (res);
7921 }
7922
7923 /*
7924  * NVRAM Routines
7925  */
7926 static int
7927 isp_read_nvram(ispsoftc_t *isp, int bus)
7928 {
7929         int i, amt, retval;
7930         uint8_t csum, minversion;
7931         union {
7932                 uint8_t _x[ISP2400_NVRAM_SIZE];
7933                 uint16_t _s[ISP2400_NVRAM_SIZE>>1];
7934         } _n;
7935 #define nvram_data      _n._x
7936 #define nvram_words     _n._s
7937
7938         if (IS_24XX(isp)) {
7939                 return (isp_read_nvram_2400(isp, nvram_data));
7940         } else if (IS_FC(isp)) {
7941                 amt = ISP2100_NVRAM_SIZE;
7942                 minversion = 1;
7943         } else if (IS_ULTRA2(isp)) {
7944                 amt = ISP1080_NVRAM_SIZE;
7945                 minversion = 0;
7946         } else {
7947                 amt = ISP_NVRAM_SIZE;
7948                 minversion = 2;
7949         }
7950
7951         for (i = 0; i < amt>>1; i++) {
7952                 isp_rdnvram_word(isp, i, &nvram_words[i]);
7953         }
7954
7955         if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
7956             nvram_data[2] != 'P') {
7957                 if (isp->isp_bustype != ISP_BT_SBUS) {
7958                         isp_prt(isp, ISP_LOGWARN, "invalid NVRAM header");
7959                         isp_prt(isp, ISP_LOGDEBUG0, "%x %x %x", nvram_data[0], nvram_data[1], nvram_data[2]);
7960                 }
7961                 retval = -1;
7962                 goto out;
7963         }
7964
7965         for (csum = 0, i = 0; i < amt; i++) {
7966                 csum += nvram_data[i];
7967         }
7968         if (csum != 0) {
7969                 isp_prt(isp, ISP_LOGWARN, "invalid NVRAM checksum");
7970                 retval = -1;
7971                 goto out;
7972         }
7973
7974         if (ISP_NVRAM_VERSION(nvram_data) < minversion) {
7975                 isp_prt(isp, ISP_LOGWARN, "version %d NVRAM not understood",
7976                     ISP_NVRAM_VERSION(nvram_data));
7977                 retval = -1;
7978                 goto out;
7979         }
7980
7981         if (IS_ULTRA3(isp)) {
7982                 isp_parse_nvram_12160(isp, bus, nvram_data);
7983         } else if (IS_1080(isp)) {
7984                 isp_parse_nvram_1080(isp, bus, nvram_data);
7985         } else if (IS_1280(isp) || IS_1240(isp)) {
7986                 isp_parse_nvram_1080(isp, bus, nvram_data);
7987         } else if (IS_SCSI(isp)) {
7988                 isp_parse_nvram_1020(isp, nvram_data);
7989         } else {
7990                 isp_parse_nvram_2100(isp, nvram_data);
7991         }
7992         retval = 0;
7993 out:
7994         return (retval);
7995 #undef  nvram_data
7996 #undef  nvram_words
7997 }
7998
7999 static int
8000 isp_read_nvram_2400(ispsoftc_t *isp, uint8_t *nvram_data)
8001 {
8002         int retval = 0;
8003         uint32_t addr, csum, lwrds, *dptr;
8004
8005         if (isp->isp_port) {
8006                 addr = ISP2400_NVRAM_PORT1_ADDR;
8007         } else {
8008                 addr = ISP2400_NVRAM_PORT0_ADDR;
8009         }
8010
8011         dptr = (uint32_t *) nvram_data;
8012         for (lwrds = 0; lwrds < ISP2400_NVRAM_SIZE >> 2; lwrds++) {
8013                 isp_rd_2400_nvram(isp, addr++, dptr++);
8014         }
8015         if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
8016             nvram_data[2] != 'P') {
8017                 isp_prt(isp, ISP_LOGWARN, "invalid NVRAM header (%x %x %x)",
8018                     nvram_data[0], nvram_data[1], nvram_data[2]);
8019                 retval = -1;
8020                 goto out;
8021         }
8022         dptr = (uint32_t *) nvram_data;
8023         for (csum = 0, lwrds = 0; lwrds < ISP2400_NVRAM_SIZE >> 2; lwrds++) {
8024                 uint32_t tmp;
8025                 ISP_IOXGET_32(isp, &dptr[lwrds], tmp);
8026                 csum += tmp;
8027         }
8028         if (csum != 0) {
8029                 isp_prt(isp, ISP_LOGWARN, "invalid NVRAM checksum");
8030                 retval = -1;
8031                 goto out;
8032         }
8033         isp_parse_nvram_2400(isp, nvram_data);
8034 out:
8035         return (retval);
8036 }
8037
8038 static void
8039 isp_rdnvram_word(ispsoftc_t *isp, int wo, uint16_t *rp)
8040 {
8041         int i, cbits;
8042         uint16_t bit, rqst, junk;
8043
8044         ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
8045         ISP_DELAY(10);
8046         ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
8047         ISP_DELAY(10);
8048
8049         if (IS_FC(isp)) {
8050                 wo &= ((ISP2100_NVRAM_SIZE >> 1) - 1);
8051                 if (IS_2312(isp) && isp->isp_port) {
8052                         wo += 128;
8053                 }
8054                 rqst = (ISP_NVRAM_READ << 8) | wo;
8055                 cbits = 10;
8056         } else if (IS_ULTRA2(isp)) {
8057                 wo &= ((ISP1080_NVRAM_SIZE >> 1) - 1);
8058                 rqst = (ISP_NVRAM_READ << 8) | wo;
8059                 cbits = 10;
8060         } else {
8061                 wo &= ((ISP_NVRAM_SIZE >> 1) - 1);
8062                 rqst = (ISP_NVRAM_READ << 6) | wo;
8063                 cbits = 8;
8064         }
8065
8066         /*
8067          * Clock the word select request out...
8068          */
8069         for (i = cbits; i >= 0; i--) {
8070                 if ((rqst >> i) & 1) {
8071                         bit = BIU_NVRAM_SELECT | BIU_NVRAM_DATAOUT;
8072                 } else {
8073                         bit = BIU_NVRAM_SELECT;
8074                 }
8075                 ISP_WRITE(isp, BIU_NVRAM, bit);
8076                 ISP_DELAY(10);
8077                 junk = ISP_READ(isp, BIU_NVRAM);        /* force PCI flush */
8078                 ISP_WRITE(isp, BIU_NVRAM, bit | BIU_NVRAM_CLOCK);
8079                 ISP_DELAY(10);
8080                 junk = ISP_READ(isp, BIU_NVRAM);        /* force PCI flush */
8081                 ISP_WRITE(isp, BIU_NVRAM, bit);
8082                 ISP_DELAY(10);
8083                 junk = ISP_READ(isp, BIU_NVRAM);        /* force PCI flush */
8084         }
8085         /*
8086          * Now read the result back in (bits come back in MSB format).
8087          */
8088         *rp = 0;
8089         for (i = 0; i < 16; i++) {
8090                 uint16_t rv;
8091                 *rp <<= 1;
8092                 ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
8093                 ISP_DELAY(10);
8094                 rv = ISP_READ(isp, BIU_NVRAM);
8095                 if (rv & BIU_NVRAM_DATAIN) {
8096                         *rp |= 1;
8097                 }
8098                 ISP_DELAY(10);
8099                 ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
8100                 ISP_DELAY(10);
8101                 junk = ISP_READ(isp, BIU_NVRAM);        /* force PCI flush */
8102         }
8103         ISP_WRITE(isp, BIU_NVRAM, 0);
8104         ISP_DELAY(10);
8105         junk = ISP_READ(isp, BIU_NVRAM);        /* force PCI flush */
8106         ISP_SWIZZLE_NVRAM_WORD(isp, rp);
8107 }
8108
8109 static void
8110 isp_rd_2400_nvram(ispsoftc_t *isp, uint32_t addr, uint32_t *rp)
8111 {
8112         int loops = 0;
8113         uint32_t base = 0x7ffe0000;
8114         uint32_t tmp = 0;
8115
8116         if (IS_25XX(isp)) {
8117                 base = 0x7ff00000 | 0x48000;
8118         }
8119         ISP_WRITE(isp, BIU2400_FLASH_ADDR, base | addr);
8120         for (loops = 0; loops < 5000; loops++) {
8121                 ISP_DELAY(10);
8122                 tmp = ISP_READ(isp, BIU2400_FLASH_ADDR);
8123                 if ((tmp & (1U << 31)) != 0) {
8124                         break;
8125                 }
8126         }
8127         if (tmp & (1U << 31)) {
8128                 *rp = ISP_READ(isp, BIU2400_FLASH_DATA);
8129                 ISP_SWIZZLE_NVRAM_LONG(isp, rp);
8130         } else {
8131                 *rp = 0xffffffff;
8132         }
8133 }
8134
8135 static void
8136 isp_parse_nvram_1020(ispsoftc_t *isp, uint8_t *nvram_data)
8137 {
8138         sdparam *sdp = SDPARAM(isp, 0);
8139         int tgt;
8140
8141         sdp->isp_fifo_threshold =
8142                 ISP_NVRAM_FIFO_THRESHOLD(nvram_data) |
8143                 (ISP_NVRAM_FIFO_THRESHOLD_128(nvram_data) << 2);
8144
8145         if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
8146                 sdp->isp_initiator_id = ISP_NVRAM_INITIATOR_ID(nvram_data);
8147
8148         sdp->isp_bus_reset_delay =
8149                 ISP_NVRAM_BUS_RESET_DELAY(nvram_data);
8150
8151         sdp->isp_retry_count =
8152                 ISP_NVRAM_BUS_RETRY_COUNT(nvram_data);
8153
8154         sdp->isp_retry_delay =
8155                 ISP_NVRAM_BUS_RETRY_DELAY(nvram_data);
8156
8157         sdp->isp_async_data_setup =
8158                 ISP_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data);
8159
8160         if (isp->isp_type >= ISP_HA_SCSI_1040) {
8161                 if (sdp->isp_async_data_setup < 9) {
8162                         sdp->isp_async_data_setup = 9;
8163                 }
8164         } else {
8165                 if (sdp->isp_async_data_setup != 6) {
8166                         sdp->isp_async_data_setup = 6;
8167                 }
8168         }
8169
8170         sdp->isp_req_ack_active_neg =
8171                 ISP_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data);
8172
8173         sdp->isp_data_line_active_neg =
8174                 ISP_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data);
8175
8176         sdp->isp_data_dma_burst_enabl =
8177                 ISP_NVRAM_DATA_DMA_BURST_ENABLE(nvram_data);
8178
8179         sdp->isp_cmd_dma_burst_enable =
8180                 ISP_NVRAM_CMD_DMA_BURST_ENABLE(nvram_data);
8181
8182         sdp->isp_tag_aging =
8183                 ISP_NVRAM_TAG_AGE_LIMIT(nvram_data);
8184
8185         sdp->isp_selection_timeout =
8186                 ISP_NVRAM_SELECTION_TIMEOUT(nvram_data);
8187
8188         sdp->isp_max_queue_depth =
8189                 ISP_NVRAM_MAX_QUEUE_DEPTH(nvram_data);
8190
8191         sdp->isp_fast_mttr = ISP_NVRAM_FAST_MTTR_ENABLE(nvram_data);
8192
8193         for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
8194                 sdp->isp_devparam[tgt].dev_enable =
8195                         ISP_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt);
8196                 sdp->isp_devparam[tgt].exc_throttle =
8197                         ISP_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt);
8198                 sdp->isp_devparam[tgt].nvrm_offset =
8199                         ISP_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt);
8200                 sdp->isp_devparam[tgt].nvrm_period =
8201                         ISP_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt);
8202                 /*
8203                  * We probably shouldn't lie about this, but it
8204                  * it makes it much safer if we limit NVRAM values
8205                  * to sanity.
8206                  */
8207                 if (isp->isp_type < ISP_HA_SCSI_1040) {
8208                         /*
8209                          * If we're not ultra, we can't possibly
8210                          * be a shorter period than this.
8211                          */
8212                         if (sdp->isp_devparam[tgt].nvrm_period < 0x19) {
8213                                 sdp->isp_devparam[tgt].nvrm_period = 0x19;
8214                         }
8215                         if (sdp->isp_devparam[tgt].nvrm_offset > 0xc) {
8216                                 sdp->isp_devparam[tgt].nvrm_offset = 0x0c;
8217                         }
8218                 } else {
8219                         if (sdp->isp_devparam[tgt].nvrm_offset > 0x8) {
8220                                 sdp->isp_devparam[tgt].nvrm_offset = 0x8;
8221                         }
8222                 }
8223                 sdp->isp_devparam[tgt].nvrm_flags = 0;
8224                 if (ISP_NVRAM_TGT_RENEG(nvram_data, tgt))
8225                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
8226                 sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
8227                 if (ISP_NVRAM_TGT_TQING(nvram_data, tgt))
8228                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
8229                 if (ISP_NVRAM_TGT_SYNC(nvram_data, tgt))
8230                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
8231                 if (ISP_NVRAM_TGT_WIDE(nvram_data, tgt))
8232                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
8233                 if (ISP_NVRAM_TGT_PARITY(nvram_data, tgt))
8234                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
8235                 if (ISP_NVRAM_TGT_DISC(nvram_data, tgt))
8236                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
8237                 sdp->isp_devparam[tgt].actv_flags = 0; /* we don't know */
8238                 sdp->isp_devparam[tgt].goal_offset =
8239                     sdp->isp_devparam[tgt].nvrm_offset;
8240                 sdp->isp_devparam[tgt].goal_period =
8241                     sdp->isp_devparam[tgt].nvrm_period;
8242                 sdp->isp_devparam[tgt].goal_flags =
8243                     sdp->isp_devparam[tgt].nvrm_flags;
8244         }
8245 }
8246
8247 static void
8248 isp_parse_nvram_1080(ispsoftc_t *isp, int bus, uint8_t *nvram_data)
8249 {
8250         sdparam *sdp = SDPARAM(isp, bus);
8251         int tgt;
8252
8253         sdp->isp_fifo_threshold =
8254             ISP1080_NVRAM_FIFO_THRESHOLD(nvram_data);
8255
8256         if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
8257                 sdp->isp_initiator_id = ISP1080_NVRAM_INITIATOR_ID(nvram_data, bus);
8258
8259         sdp->isp_bus_reset_delay =
8260             ISP1080_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
8261
8262         sdp->isp_retry_count =
8263             ISP1080_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
8264
8265         sdp->isp_retry_delay =
8266             ISP1080_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
8267
8268         sdp->isp_async_data_setup =
8269             ISP1080_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
8270
8271         sdp->isp_req_ack_active_neg =
8272             ISP1080_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
8273
8274         sdp->isp_data_line_active_neg =
8275             ISP1080_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
8276
8277         sdp->isp_data_dma_burst_enabl =
8278             ISP1080_NVRAM_BURST_ENABLE(nvram_data);
8279
8280         sdp->isp_cmd_dma_burst_enable =
8281             ISP1080_NVRAM_BURST_ENABLE(nvram_data);
8282
8283         sdp->isp_selection_timeout =
8284             ISP1080_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
8285
8286         sdp->isp_max_queue_depth =
8287              ISP1080_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
8288
8289         for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
8290                 sdp->isp_devparam[tgt].dev_enable =
8291                     ISP1080_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
8292                 sdp->isp_devparam[tgt].exc_throttle =
8293                         ISP1080_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
8294                 sdp->isp_devparam[tgt].nvrm_offset =
8295                         ISP1080_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
8296                 sdp->isp_devparam[tgt].nvrm_period =
8297                         ISP1080_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
8298                 sdp->isp_devparam[tgt].nvrm_flags = 0;
8299                 if (ISP1080_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
8300                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
8301                 sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
8302                 if (ISP1080_NVRAM_TGT_TQING(nvram_data, tgt, bus))
8303                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
8304                 if (ISP1080_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
8305                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
8306                 if (ISP1080_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
8307                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
8308                 if (ISP1080_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
8309                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
8310                 if (ISP1080_NVRAM_TGT_DISC(nvram_data, tgt, bus))
8311                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
8312                 sdp->isp_devparam[tgt].actv_flags = 0;
8313                 sdp->isp_devparam[tgt].goal_offset =
8314                     sdp->isp_devparam[tgt].nvrm_offset;
8315                 sdp->isp_devparam[tgt].goal_period =
8316                     sdp->isp_devparam[tgt].nvrm_period;
8317                 sdp->isp_devparam[tgt].goal_flags =
8318                     sdp->isp_devparam[tgt].nvrm_flags;
8319         }
8320 }
8321
8322 static void
8323 isp_parse_nvram_12160(ispsoftc_t *isp, int bus, uint8_t *nvram_data)
8324 {
8325         sdparam *sdp = SDPARAM(isp, bus);
8326         int tgt;
8327
8328         sdp->isp_fifo_threshold =
8329             ISP12160_NVRAM_FIFO_THRESHOLD(nvram_data);
8330
8331         if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
8332                 sdp->isp_initiator_id = ISP12160_NVRAM_INITIATOR_ID(nvram_data, bus);
8333
8334         sdp->isp_bus_reset_delay =
8335             ISP12160_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
8336
8337         sdp->isp_retry_count =
8338             ISP12160_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
8339
8340         sdp->isp_retry_delay =
8341             ISP12160_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
8342
8343         sdp->isp_async_data_setup =
8344             ISP12160_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
8345
8346         sdp->isp_req_ack_active_neg =
8347             ISP12160_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
8348
8349         sdp->isp_data_line_active_neg =
8350             ISP12160_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
8351
8352         sdp->isp_data_dma_burst_enabl =
8353             ISP12160_NVRAM_BURST_ENABLE(nvram_data);
8354
8355         sdp->isp_cmd_dma_burst_enable =
8356             ISP12160_NVRAM_BURST_ENABLE(nvram_data);
8357
8358         sdp->isp_selection_timeout =
8359             ISP12160_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
8360
8361         sdp->isp_max_queue_depth =
8362              ISP12160_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
8363
8364         for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
8365                 sdp->isp_devparam[tgt].dev_enable =
8366                     ISP12160_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
8367                 sdp->isp_devparam[tgt].exc_throttle =
8368                         ISP12160_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
8369                 sdp->isp_devparam[tgt].nvrm_offset =
8370                         ISP12160_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
8371                 sdp->isp_devparam[tgt].nvrm_period =
8372                         ISP12160_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
8373                 sdp->isp_devparam[tgt].nvrm_flags = 0;
8374                 if (ISP12160_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
8375                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
8376                 sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
8377                 if (ISP12160_NVRAM_TGT_TQING(nvram_data, tgt, bus))
8378                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
8379                 if (ISP12160_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
8380                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
8381                 if (ISP12160_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
8382                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
8383                 if (ISP12160_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
8384                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
8385                 if (ISP12160_NVRAM_TGT_DISC(nvram_data, tgt, bus))
8386                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
8387                 sdp->isp_devparam[tgt].actv_flags = 0;
8388                 sdp->isp_devparam[tgt].goal_offset =
8389                     sdp->isp_devparam[tgt].nvrm_offset;
8390                 sdp->isp_devparam[tgt].goal_period =
8391                     sdp->isp_devparam[tgt].nvrm_period;
8392                 sdp->isp_devparam[tgt].goal_flags =
8393                     sdp->isp_devparam[tgt].nvrm_flags;
8394         }
8395 }
8396
8397 static void
8398 isp_parse_nvram_2100(ispsoftc_t *isp, uint8_t *nvram_data)
8399 {
8400         fcparam *fcp = FCPARAM(isp, 0);
8401         uint64_t wwn;
8402
8403         /*
8404          * There is NVRAM storage for both Port and Node entities-
8405          * but the Node entity appears to be unused on all the cards
8406          * I can find. However, we should account for this being set
8407          * at some point in the future.
8408          *
8409          * Qlogic WWNs have an NAA of 2, but usually nothing shows up in
8410          * bits 48..60. In the case of the 2202, it appears that they do
8411          * use bit 48 to distinguish between the two instances on the card.
8412          * The 2204, which I've never seen, *probably* extends this method.
8413          */
8414         wwn = ISP2100_NVRAM_PORT_NAME(nvram_data);
8415         if (wwn) {
8416                 isp_prt(isp, ISP_LOGCONFIG, "NVRAM Port WWN 0x%08x%08x",
8417                     (uint32_t) (wwn >> 32), (uint32_t) (wwn));
8418                 if ((wwn >> 60) == 0) {
8419                         wwn |= (((uint64_t) 2)<< 60);
8420                 }
8421         }
8422         fcp->isp_wwpn_nvram = wwn;
8423         if (IS_2200(isp) || IS_23XX(isp)) {
8424                 wwn = ISP2100_NVRAM_NODE_NAME(nvram_data);
8425                 if (wwn) {
8426                         isp_prt(isp, ISP_LOGCONFIG, "NVRAM Node WWN 0x%08x%08x",
8427                             (uint32_t) (wwn >> 32),
8428                             (uint32_t) (wwn));
8429                         if ((wwn >> 60) == 0) {
8430                                 wwn |= (((uint64_t) 2)<< 60);
8431                         }
8432                 } else {
8433                         wwn = fcp->isp_wwpn_nvram & ~((uint64_t) 0xfff << 48);
8434                 }
8435         } else {
8436                 wwn &= ~((uint64_t) 0xfff << 48);
8437         }
8438         fcp->isp_wwnn_nvram = wwn;
8439
8440         fcp->isp_maxalloc = ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data);
8441         if ((isp->isp_confopts & ISP_CFG_OWNFSZ) == 0) {
8442                 DEFAULT_FRAMESIZE(isp) =
8443                     ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data);
8444         }
8445         fcp->isp_retry_delay = ISP2100_NVRAM_RETRY_DELAY(nvram_data);
8446         fcp->isp_retry_count = ISP2100_NVRAM_RETRY_COUNT(nvram_data);
8447         if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0) {
8448                 fcp->isp_loopid = ISP2100_NVRAM_HARDLOOPID(nvram_data);
8449         }
8450         if ((isp->isp_confopts & ISP_CFG_OWNEXCTHROTTLE) == 0) {
8451                 DEFAULT_EXEC_THROTTLE(isp) =
8452                         ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data);
8453         }
8454         fcp->isp_fwoptions = ISP2100_NVRAM_OPTIONS(nvram_data);
8455         isp_prt(isp, ISP_LOGDEBUG0,
8456             "NVRAM 0x%08x%08x 0x%08x%08x maxalloc %d maxframelen %d",
8457             (uint32_t) (fcp->isp_wwnn_nvram >> 32),
8458             (uint32_t) fcp->isp_wwnn_nvram,
8459             (uint32_t) (fcp->isp_wwpn_nvram >> 32),
8460             (uint32_t) fcp->isp_wwpn_nvram,
8461             ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data),
8462             ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data));
8463         isp_prt(isp, ISP_LOGDEBUG0,
8464             "execthrottle %d fwoptions 0x%x hardloop %d tov %d",
8465             ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data),
8466             ISP2100_NVRAM_OPTIONS(nvram_data),
8467             ISP2100_NVRAM_HARDLOOPID(nvram_data),
8468             ISP2100_NVRAM_TOV(nvram_data));
8469         fcp->isp_xfwoptions = ISP2100_XFW_OPTIONS(nvram_data);
8470         fcp->isp_zfwoptions = ISP2100_ZFW_OPTIONS(nvram_data);
8471         isp_prt(isp, ISP_LOGDEBUG0, "xfwoptions 0x%x zfw options 0x%x",
8472             ISP2100_XFW_OPTIONS(nvram_data), ISP2100_ZFW_OPTIONS(nvram_data));
8473 }
8474
8475 static void
8476 isp_parse_nvram_2400(ispsoftc_t *isp, uint8_t *nvram_data)
8477 {
8478         fcparam *fcp = FCPARAM(isp, 0);
8479         uint64_t wwn;
8480
8481         isp_prt(isp, ISP_LOGDEBUG0,
8482             "NVRAM 0x%08x%08x 0x%08x%08x exchg_cnt %d maxframelen %d",
8483             (uint32_t) (ISP2400_NVRAM_NODE_NAME(nvram_data) >> 32),
8484             (uint32_t) (ISP2400_NVRAM_NODE_NAME(nvram_data)),
8485             (uint32_t) (ISP2400_NVRAM_PORT_NAME(nvram_data) >> 32),
8486             (uint32_t) (ISP2400_NVRAM_PORT_NAME(nvram_data)),
8487             ISP2400_NVRAM_EXCHANGE_COUNT(nvram_data),
8488             ISP2400_NVRAM_MAXFRAMELENGTH(nvram_data));
8489         isp_prt(isp, ISP_LOGDEBUG0,
8490             "NVRAM execthr %d loopid %d fwopt1 0x%x fwopt2 0x%x fwopt3 0x%x",
8491             ISP2400_NVRAM_EXECUTION_THROTTLE(nvram_data),
8492             ISP2400_NVRAM_HARDLOOPID(nvram_data),
8493             ISP2400_NVRAM_FIRMWARE_OPTIONS1(nvram_data),
8494             ISP2400_NVRAM_FIRMWARE_OPTIONS2(nvram_data),
8495             ISP2400_NVRAM_FIRMWARE_OPTIONS3(nvram_data));
8496
8497         wwn = ISP2400_NVRAM_PORT_NAME(nvram_data);
8498         fcp->isp_wwpn_nvram = wwn;
8499
8500         wwn = ISP2400_NVRAM_NODE_NAME(nvram_data);
8501         if (wwn) {
8502                 if ((wwn >> 60) != 2 && (wwn >> 60) != 5) {
8503                         wwn = 0;
8504                 }
8505         }
8506         if (wwn == 0 && (fcp->isp_wwpn_nvram >> 60) == 2) {
8507                 wwn = fcp->isp_wwpn_nvram;
8508                 wwn &= ~((uint64_t) 0xfff << 48);
8509         }
8510         fcp->isp_wwnn_nvram = wwn;
8511
8512         if (ISP2400_NVRAM_EXCHANGE_COUNT(nvram_data)) {
8513                 fcp->isp_maxalloc = ISP2400_NVRAM_EXCHANGE_COUNT(nvram_data);
8514         }
8515         if ((isp->isp_confopts & ISP_CFG_OWNFSZ) == 0) {
8516                 DEFAULT_FRAMESIZE(isp) =
8517                     ISP2400_NVRAM_MAXFRAMELENGTH(nvram_data);
8518         }
8519         if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0) {
8520                 fcp->isp_loopid = ISP2400_NVRAM_HARDLOOPID(nvram_data);
8521         }
8522         if ((isp->isp_confopts & ISP_CFG_OWNEXCTHROTTLE) == 0) {
8523                 DEFAULT_EXEC_THROTTLE(isp) =
8524                         ISP2400_NVRAM_EXECUTION_THROTTLE(nvram_data);
8525         }
8526         fcp->isp_fwoptions = ISP2400_NVRAM_FIRMWARE_OPTIONS1(nvram_data);
8527         fcp->isp_xfwoptions = ISP2400_NVRAM_FIRMWARE_OPTIONS2(nvram_data);
8528         fcp->isp_zfwoptions = ISP2400_NVRAM_FIRMWARE_OPTIONS3(nvram_data);
8529 }