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