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