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