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