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