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