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