]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/isp/isp.c
MFC r291099: Some cosmetics for ancient cards.
[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 & 0xfffc00) == 0xfffc00)
2324                         continue;
2325                 fcp->portdb[i].probational = 1;
2326         }
2327 }
2328
2329 /*
2330  * Perform an IOCB PLOGI or LOGO via EXECUTE IOCB A64 for 24XX cards
2331  * or via FABRIC LOGIN/FABRIC LOGOUT for other cards.
2332  */
2333 static int
2334 isp_plogx(ispsoftc_t *isp, int chan, uint16_t handle, uint32_t portid, int flags, int gs)
2335 {
2336         mbreg_t mbs;
2337         uint8_t q[QENTRY_LEN];
2338         isp_plogx_t *plp;
2339         fcparam *fcp;
2340         uint8_t *scp;
2341         uint32_t sst, parm1;
2342         int rval, lev;
2343         const char *msg;
2344         char buf[64];
2345
2346         isp_prt(isp, ISP_LOG_SANCFG, "Chan %d PLOGX %s PortID 0x%06x nphdl 0x%x",
2347             chan, (flags & PLOGX_FLG_CMD_MASK) == PLOGX_FLG_CMD_PLOGI ?
2348             "Login":"Logout", portid, handle);
2349         if (!IS_24XX(isp)) {
2350                 int action = flags & PLOGX_FLG_CMD_MASK;
2351                 if (action == PLOGX_FLG_CMD_PLOGI) {
2352                         return (isp_port_login(isp, handle, portid));
2353                 } else if (action == PLOGX_FLG_CMD_LOGO) {
2354                         return (isp_port_logout(isp, handle, portid));
2355                 } else {
2356                         return (MBOX_INVALID_COMMAND);
2357                 }
2358         }
2359
2360         ISP_MEMZERO(q, QENTRY_LEN);
2361         plp = (isp_plogx_t *) q;
2362         plp->plogx_header.rqs_entry_count = 1;
2363         plp->plogx_header.rqs_entry_type = RQSTYPE_LOGIN;
2364         plp->plogx_handle = 0xffffffff;
2365         plp->plogx_nphdl = handle;
2366         plp->plogx_vphdl = chan;
2367         plp->plogx_portlo = portid;
2368         plp->plogx_rspsz_porthi = (portid >> 16) & 0xff;
2369         plp->plogx_flags = flags;
2370
2371         if (isp->isp_dblev & ISP_LOGDEBUG1) {
2372                 isp_print_bytes(isp, "IOCB LOGX", QENTRY_LEN, plp);
2373         }
2374
2375         if (gs == 0) {
2376                 if (FC_SCRATCH_ACQUIRE(isp, chan)) {
2377                         isp_prt(isp, ISP_LOGERR, sacq);
2378                         return (-1);
2379                 }
2380         }
2381         fcp = FCPARAM(isp, chan);
2382         scp = fcp->isp_scratch;
2383         isp_put_plogx(isp, plp, (isp_plogx_t *) scp);
2384
2385         MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 500000);
2386         mbs.param[1] = QENTRY_LEN;
2387         mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2388         mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2389         mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2390         mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2391         MEMORYBARRIER(isp, SYNC_SFORDEV, 0, QENTRY_LEN, chan);
2392         isp_mboxcmd(isp, &mbs);
2393         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2394                 rval = mbs.param[0];
2395                 goto out;
2396         }
2397         MEMORYBARRIER(isp, SYNC_SFORCPU, QENTRY_LEN, QENTRY_LEN, chan);
2398         scp += QENTRY_LEN;
2399         isp_get_plogx(isp, (isp_plogx_t *) scp, plp);
2400         if (isp->isp_dblev & ISP_LOGDEBUG1) {
2401                 isp_print_bytes(isp, "IOCB LOGX response", QENTRY_LEN, plp);
2402         }
2403
2404         if (plp->plogx_status == PLOGX_STATUS_OK) {
2405                 rval = 0;
2406                 goto out;
2407         } else if (plp->plogx_status != PLOGX_STATUS_IOCBERR) {
2408                 isp_prt(isp, ISP_LOGWARN,
2409                     "status 0x%x on port login IOCB channel %d",
2410                     plp->plogx_status, chan);
2411                 rval = -1;
2412                 goto out;
2413         }
2414
2415         sst = plp->plogx_ioparm[0].lo16 | (plp->plogx_ioparm[0].hi16 << 16);
2416         parm1 = plp->plogx_ioparm[1].lo16 | (plp->plogx_ioparm[1].hi16 << 16);
2417
2418         rval = -1;
2419         lev = ISP_LOGERR;
2420         msg = NULL;
2421
2422         switch (sst) {
2423         case PLOGX_IOCBERR_NOLINK:
2424                 msg = "no link";
2425                 break;
2426         case PLOGX_IOCBERR_NOIOCB:
2427                 msg = "no IOCB buffer";
2428                 break;
2429         case PLOGX_IOCBERR_NOXGHG:
2430                 msg = "no Exchange Control Block";
2431                 break;
2432         case PLOGX_IOCBERR_FAILED:
2433                 ISP_SNPRINTF(buf, sizeof (buf), "reason 0x%x (last LOGIN state 0x%x)", parm1 & 0xff, (parm1 >> 8) & 0xff);
2434                 msg = buf;
2435                 break;
2436         case PLOGX_IOCBERR_NOFABRIC:
2437                 msg = "no fabric";
2438                 break;
2439         case PLOGX_IOCBERR_NOTREADY:
2440                 msg = "firmware not ready";
2441                 break;
2442         case PLOGX_IOCBERR_NOLOGIN:
2443                 ISP_SNPRINTF(buf, sizeof (buf), "not logged in (last state 0x%x)", parm1);
2444                 msg = buf;
2445                 rval = MBOX_NOT_LOGGED_IN;
2446                 break;
2447         case PLOGX_IOCBERR_REJECT:
2448                 ISP_SNPRINTF(buf, sizeof (buf), "LS_RJT = 0x%x", parm1);
2449                 msg = buf;
2450                 break;
2451         case PLOGX_IOCBERR_NOPCB:
2452                 msg = "no PCB allocated";
2453                 break;
2454         case PLOGX_IOCBERR_EINVAL:
2455                 ISP_SNPRINTF(buf, sizeof (buf), "invalid parameter at offset 0x%x", parm1);
2456                 msg = buf;
2457                 break;
2458         case PLOGX_IOCBERR_PORTUSED:
2459                 lev = ISP_LOG_SANCFG|ISP_LOG_WARN1;
2460                 ISP_SNPRINTF(buf, sizeof (buf), "already logged in with N-Port handle 0x%x", parm1);
2461                 msg = buf;
2462                 rval = MBOX_PORT_ID_USED | (parm1 << 16);
2463                 break;
2464         case PLOGX_IOCBERR_HNDLUSED:
2465                 lev = ISP_LOG_SANCFG|ISP_LOG_WARN1;
2466                 ISP_SNPRINTF(buf, sizeof (buf), "handle already used for PortID 0x%06x", parm1);
2467                 msg = buf;
2468                 rval = MBOX_LOOP_ID_USED;
2469                 break;
2470         case PLOGX_IOCBERR_NOHANDLE:
2471                 msg = "no handle allocated";
2472                 break;
2473         case PLOGX_IOCBERR_NOFLOGI:
2474                 msg = "no FLOGI_ACC";
2475                 break;
2476         default:
2477                 ISP_SNPRINTF(buf, sizeof (buf), "status %x from %x", plp->plogx_status, flags);
2478                 msg = buf;
2479                 break;
2480         }
2481         if (msg) {
2482                 isp_prt(isp, ISP_LOGERR, "Chan %d PLOGX PortID 0x%06x to N-Port handle 0x%x: %s", chan, portid, handle, msg);
2483         }
2484 out:
2485         if (gs == 0) {
2486                 FC_SCRATCH_RELEASE(isp, chan);
2487         }
2488         return (rval);
2489 }
2490
2491 static int
2492 isp_port_login(ispsoftc_t *isp, uint16_t handle, uint32_t portid)
2493 {
2494         mbreg_t mbs;
2495
2496         MBSINIT(&mbs, MBOX_FABRIC_LOGIN, MBLOGNONE, 500000);
2497         if (ISP_CAP_2KLOGIN(isp)) {
2498                 mbs.param[1] = handle;
2499                 mbs.ibits = (1 << 10);
2500         } else {
2501                 mbs.param[1] = handle << 8;
2502         }
2503         mbs.param[2] = portid >> 16;
2504         mbs.param[3] = portid;
2505         mbs.logval = MBLOGNONE;
2506         mbs.timeout = 500000;
2507         isp_mboxcmd(isp, &mbs);
2508
2509         switch (mbs.param[0]) {
2510         case MBOX_PORT_ID_USED:
2511                 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]);
2512                 return (MBOX_PORT_ID_USED | (mbs.param[1] << 16));
2513
2514         case MBOX_LOOP_ID_USED:
2515                 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);
2516                 return (MBOX_LOOP_ID_USED);
2517
2518         case MBOX_COMMAND_COMPLETE:
2519                 return (0);
2520
2521         case MBOX_COMMAND_ERROR:
2522                 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);
2523                 return (MBOX_COMMAND_ERROR);
2524
2525         case MBOX_ALL_IDS_USED:
2526                 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOG_WARN1, "isp_port_login: all IDs used for fabric login");
2527                 return (MBOX_ALL_IDS_USED);
2528
2529         default:
2530                 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);
2531                 return (mbs.param[0]);
2532         }
2533 }
2534
2535 /*
2536  * Pre-24XX fabric port logout
2537  *
2538  * Note that portid is not used
2539  */
2540 static int
2541 isp_port_logout(ispsoftc_t *isp, uint16_t handle, uint32_t portid)
2542 {
2543         mbreg_t mbs;
2544
2545         MBSINIT(&mbs, MBOX_FABRIC_LOGOUT, MBLOGNONE, 500000);
2546         if (ISP_CAP_2KLOGIN(isp)) {
2547                 mbs.param[1] = handle;
2548                 mbs.ibits = (1 << 10);
2549         } else {
2550                 mbs.param[1] = handle << 8;
2551         }
2552         isp_mboxcmd(isp, &mbs);
2553         return (mbs.param[0] == MBOX_COMMAND_COMPLETE? 0 : mbs.param[0]);
2554 }
2555
2556 static int
2557 isp_getpdb(ispsoftc_t *isp, int chan, uint16_t id, isp_pdb_t *pdb, int dolock)
2558 {
2559         fcparam *fcp = FCPARAM(isp, chan);
2560         mbreg_t mbs;
2561         union {
2562                 isp_pdb_21xx_t fred;
2563                 isp_pdb_24xx_t bill;
2564         } un;
2565
2566         MBSINIT(&mbs, MBOX_GET_PORT_DB,
2567             MBLOGALL & ~MBLOGMASK(MBOX_COMMAND_PARAM_ERROR), 250000);
2568         if (IS_24XX(isp)) {
2569                 mbs.ibits = (1 << 9)|(1 << 10);
2570                 mbs.param[1] = id;
2571                 mbs.param[9] = chan;
2572         } else if (ISP_CAP_2KLOGIN(isp)) {
2573                 mbs.param[1] = id;
2574         } else {
2575                 mbs.param[1] = id << 8;
2576         }
2577         mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2578         mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2579         mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2580         mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2581         if (dolock) {
2582                 if (FC_SCRATCH_ACQUIRE(isp, chan)) {
2583                         isp_prt(isp, ISP_LOGERR, sacq);
2584                         return (-1);
2585                 }
2586         }
2587         MEMORYBARRIER(isp, SYNC_SFORDEV, 0, sizeof (un), chan);
2588         isp_mboxcmd(isp, &mbs);
2589         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2590                 if (dolock) {
2591                         FC_SCRATCH_RELEASE(isp, chan);
2592                 }
2593                 return (mbs.param[0] | (mbs.param[1] << 16));
2594         }
2595         if (IS_24XX(isp)) {
2596                 isp_get_pdb_24xx(isp, fcp->isp_scratch, &un.bill);
2597                 pdb->handle = un.bill.pdb_handle;
2598                 pdb->prli_word3 = un.bill.pdb_prli_svc3;
2599                 pdb->portid = BITS2WORD_24XX(un.bill.pdb_portid_bits);
2600                 ISP_MEMCPY(pdb->portname, un.bill.pdb_portname, 8);
2601                 ISP_MEMCPY(pdb->nodename, un.bill.pdb_nodename, 8);
2602                 isp_prt(isp, ISP_LOGDEBUG1,
2603                     "Chan %d handle 0x%x Port 0x%06x flags 0x%x curstate %x",
2604                     chan, id, pdb->portid, un.bill.pdb_flags,
2605                     un.bill.pdb_curstate);
2606                 if (un.bill.pdb_curstate < PDB2400_STATE_PLOGI_DONE || un.bill.pdb_curstate > PDB2400_STATE_LOGGED_IN) {
2607                         mbs.param[0] = MBOX_NOT_LOGGED_IN;
2608                         if (dolock) {
2609                                 FC_SCRATCH_RELEASE(isp, chan);
2610                         }
2611                         return (mbs.param[0]);
2612                 }
2613         } else {
2614                 isp_get_pdb_21xx(isp, fcp->isp_scratch, &un.fred);
2615                 pdb->handle = un.fred.pdb_loopid;
2616                 pdb->prli_word3 = un.fred.pdb_prli_svc3;
2617                 pdb->portid = BITS2WORD(un.fred.pdb_portid_bits);
2618                 ISP_MEMCPY(pdb->portname, un.fred.pdb_portname, 8);
2619                 ISP_MEMCPY(pdb->nodename, un.fred.pdb_nodename, 8);
2620                 isp_prt(isp, ISP_LOGDEBUG1,
2621                     "Chan %d handle 0x%x Port 0x%06x", chan, id, pdb->portid);
2622         }
2623         if (dolock) {
2624                 FC_SCRATCH_RELEASE(isp, chan);
2625         }
2626         return (0);
2627 }
2628
2629 static int
2630 isp_gethandles(ispsoftc_t *isp, int chan, uint16_t *handles, int *num,
2631     int dolock, int loop)
2632 {
2633         fcparam *fcp = FCPARAM(isp, chan);
2634         mbreg_t mbs;
2635         isp_pnhle_21xx_t el1, *elp1;
2636         isp_pnhle_23xx_t el3, *elp3;
2637         isp_pnhle_24xx_t el4, *elp4;
2638         int i, j;
2639         uint32_t p;
2640         uint16_t h;
2641
2642         MBSINIT(&mbs, MBOX_GET_ID_LIST, MBLOGALL, 250000);
2643         if (IS_24XX(isp)) {
2644                 mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2645                 mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2646                 mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2647                 mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2648                 mbs.param[8] = ISP_FC_SCRLEN;
2649                 mbs.param[9] = chan;
2650         } else {
2651                 mbs.ibits = (1 << 1)|(1 << 2)|(1 << 3)|(1 << 6);
2652                 mbs.param[1] = DMA_WD1(fcp->isp_scdma);
2653                 mbs.param[2] = DMA_WD0(fcp->isp_scdma);
2654                 mbs.param[3] = DMA_WD3(fcp->isp_scdma);
2655                 mbs.param[6] = DMA_WD2(fcp->isp_scdma);
2656         }
2657         if (dolock) {
2658                 if (FC_SCRATCH_ACQUIRE(isp, chan)) {
2659                         isp_prt(isp, ISP_LOGERR, sacq);
2660                         return (-1);
2661                 }
2662         }
2663         MEMORYBARRIER(isp, SYNC_SFORDEV, 0, ISP_FC_SCRLEN, chan);
2664         isp_mboxcmd(isp, &mbs);
2665         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2666                 if (dolock) {
2667                         FC_SCRATCH_RELEASE(isp, chan);
2668                 }
2669                 return (mbs.param[0] | (mbs.param[1] << 16));
2670         }
2671         elp1 = fcp->isp_scratch;
2672         elp3 = fcp->isp_scratch;
2673         elp4 = fcp->isp_scratch;
2674         for (i = 0, j = 0; i < mbs.param[1] && j < *num; i++) {
2675                 if (IS_24XX(isp)) {
2676                         isp_get_pnhle_24xx(isp, &elp4[i], &el4);
2677                         p = el4.pnhle_port_id_lo |
2678                             (el4.pnhle_port_id_hi << 16);
2679                         h = el4.pnhle_handle;
2680                 } else if (IS_23XX(isp)) {
2681                         isp_get_pnhle_23xx(isp, &elp3[i], &el3);
2682                         p = el3.pnhle_port_id_lo |
2683                             (el3.pnhle_port_id_hi << 16);
2684                         h = el3.pnhle_handle;
2685                 } else { /* 21xx */
2686                         isp_get_pnhle_21xx(isp, &elp1[i], &el1);
2687                         p = el1.pnhle_port_id_lo |
2688                             ((el1.pnhle_port_id_hi_handle & 0xff) << 16);
2689                         h = el1.pnhle_port_id_hi_handle >> 8;
2690                 }
2691                 if (loop && (p >> 8) != (fcp->isp_portid >> 8))
2692                         continue;
2693                 handles[j++] = h;
2694         }
2695         *num = j;
2696         if (dolock)
2697                 FC_SCRATCH_RELEASE(isp, chan);
2698         return (0);
2699 }
2700
2701 static void
2702 isp_dump_chip_portdb(ispsoftc_t *isp, int chan, int dolock)
2703 {
2704         isp_pdb_t pdb;
2705         uint16_t lim, nphdl;
2706
2707         isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGINFO, "Chan %d chip port dump", chan);
2708         if (ISP_CAP_2KLOGIN(isp)) {
2709                 lim = NPH_MAX_2K;
2710         } else {
2711                 lim = NPH_MAX;
2712         }
2713         for (nphdl = 0; nphdl != lim; nphdl++) {
2714                 if (isp_getpdb(isp, chan, nphdl, &pdb, dolock)) {
2715                         continue;
2716                 }
2717                 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGINFO, "Chan %d Handle 0x%04x "
2718                     "PortID 0x%06x WWPN 0x%02x%02x%02x%02x%02x%02x%02x%02x",
2719                     chan, nphdl, pdb.portid, pdb.portname[0], pdb.portname[1],
2720                     pdb.portname[2], pdb.portname[3], pdb.portname[4],
2721                     pdb.portname[5], pdb.portname[6], pdb.portname[7]);
2722         }
2723 }
2724
2725 static uint64_t
2726 isp_get_wwn(ispsoftc_t *isp, int chan, int nphdl, int nodename)
2727 {
2728         uint64_t wwn = INI_NONE;
2729         mbreg_t mbs;
2730
2731         MBSINIT(&mbs, MBOX_GET_PORT_NAME,
2732             MBLOGALL & ~MBLOGMASK(MBOX_COMMAND_PARAM_ERROR), 500000);
2733         if (ISP_CAP_2KLOGIN(isp)) {
2734                 mbs.param[1] = nphdl;
2735                 if (nodename) {
2736                         mbs.param[10] = 1;
2737                 }
2738                 mbs.param[9] = chan;
2739         } else {
2740                 mbs.ibitm = 3;
2741                 mbs.param[1] = nphdl << 8;
2742                 if (nodename) {
2743                         mbs.param[1] |= 1;
2744                 }
2745         }
2746         isp_mboxcmd(isp, &mbs);
2747         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2748                 return (wwn);
2749         }
2750         if (IS_24XX(isp)) {
2751                 wwn =
2752                     (((uint64_t)(mbs.param[2] >> 8))    << 56) |
2753                     (((uint64_t)(mbs.param[2] & 0xff))  << 48) |
2754                     (((uint64_t)(mbs.param[3] >> 8))    << 40) |
2755                     (((uint64_t)(mbs.param[3] & 0xff))  << 32) |
2756                     (((uint64_t)(mbs.param[6] >> 8))    << 24) |
2757                     (((uint64_t)(mbs.param[6] & 0xff))  << 16) |
2758                     (((uint64_t)(mbs.param[7] >> 8))    <<  8) |
2759                     (((uint64_t)(mbs.param[7] & 0xff)));
2760         } else {
2761                 wwn =
2762                     (((uint64_t)(mbs.param[2] & 0xff))  << 56) |
2763                     (((uint64_t)(mbs.param[2] >> 8))    << 48) |
2764                     (((uint64_t)(mbs.param[3] & 0xff))  << 40) |
2765                     (((uint64_t)(mbs.param[3] >> 8))    << 32) |
2766                     (((uint64_t)(mbs.param[6] & 0xff))  << 24) |
2767                     (((uint64_t)(mbs.param[6] >> 8))    << 16) |
2768                     (((uint64_t)(mbs.param[7] & 0xff))  <<  8) |
2769                     (((uint64_t)(mbs.param[7] >> 8)));
2770         }
2771         return (wwn);
2772 }
2773
2774 /*
2775  * Make sure we have good FC link.
2776  */
2777
2778 static int
2779 isp_fclink_test(ispsoftc_t *isp, int chan, int usdelay)
2780 {
2781         mbreg_t mbs;
2782         int i, r;
2783         uint16_t nphdl;
2784         fcparam *fcp;
2785         isp_pdb_t pdb;
2786         NANOTIME_T hra, hrb;
2787
2788         fcp = FCPARAM(isp, chan);
2789
2790         /* Mark port database entries probational for following scan. */
2791         isp_mark_portdb(isp, chan);
2792
2793         if (fcp->isp_loopstate >= LOOP_LTEST_DONE)
2794                 return (0);
2795
2796         isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC link test", chan);
2797         fcp->isp_loopstate = LOOP_TESTING_LINK;
2798
2799         /*
2800          * Wait up to N microseconds for F/W to go to a ready state.
2801          */
2802         GET_NANOTIME(&hra);
2803         while (1) {
2804                 isp_change_fw_state(isp, chan, isp_fw_state(isp, chan));
2805                 if (fcp->isp_fwstate == FW_READY) {
2806                         break;
2807                 }
2808                 GET_NANOTIME(&hrb);
2809                 if ((NANOTIME_SUB(&hrb, &hra) / 1000 + 1000 >= usdelay))
2810                         break;
2811                 ISP_SLEEP(isp, 1000);
2812         }
2813
2814         /*
2815          * If we haven't gone to 'ready' state, return.
2816          */
2817         if (fcp->isp_fwstate != FW_READY) {
2818                 isp_prt(isp, ISP_LOG_SANCFG,
2819                     "Chan %d Firmware is not ready (%s)",
2820                     chan, isp_fc_fw_statename(fcp->isp_fwstate));
2821                 return (-1);
2822         }
2823
2824         /*
2825          * Get our Loop ID and Port ID.
2826          */
2827         MBSINIT(&mbs, MBOX_GET_LOOP_ID, MBLOGALL, 0);
2828         mbs.param[9] = chan;
2829         isp_mboxcmd(isp, &mbs);
2830         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2831                 return (-1);
2832         }
2833
2834         if (IS_2100(isp)) {
2835                 /*
2836                  * Don't bother with fabric if we are using really old
2837                  * 2100 firmware. It's just not worth it.
2838                  */
2839                 if (ISP_FW_NEWER_THAN(isp, 1, 15, 37))
2840                         fcp->isp_topo = TOPO_FL_PORT;
2841                 else
2842                         fcp->isp_topo = TOPO_NL_PORT;
2843         } else {
2844                 int topo = (int) mbs.param[6];
2845                 if (topo < TOPO_NL_PORT || topo > TOPO_PTP_STUB) {
2846                         topo = TOPO_PTP_STUB;
2847                 }
2848                 fcp->isp_topo = topo;
2849         }
2850         fcp->isp_portid = mbs.param[2] | (mbs.param[3] << 16);
2851
2852         if (!TOPO_IS_FABRIC(fcp->isp_topo)) {
2853                 fcp->isp_loopid = mbs.param[1] & 0xff;
2854         } else if (fcp->isp_topo != TOPO_F_PORT) {
2855                 uint8_t alpa = fcp->isp_portid;
2856
2857                 for (i = 0; alpa_map[i]; i++) {
2858                         if (alpa_map[i] == alpa)
2859                                 break;
2860                 }
2861                 if (alpa_map[i])
2862                         fcp->isp_loopid = i;
2863         }
2864
2865         if (fcp->isp_topo == TOPO_F_PORT || fcp->isp_topo == TOPO_FL_PORT) {
2866                 nphdl = IS_24XX(isp) ? NPH_FL_ID : FL_ID;
2867                 r = isp_getpdb(isp, chan, nphdl, &pdb, 1);
2868                 if (r != 0 || pdb.portid == 0) {
2869                         if (IS_2100(isp)) {
2870                                 fcp->isp_topo = TOPO_NL_PORT;
2871                         } else {
2872                                 isp_prt(isp, ISP_LOGWARN,
2873                                     "fabric topology, but cannot get info about fabric controller (0x%x)", r);
2874                                 fcp->isp_topo = TOPO_PTP_STUB;
2875                         }
2876                         goto not_on_fabric;
2877                 }
2878
2879                 if (IS_24XX(isp)) {
2880                         fcp->isp_fabric_params = mbs.param[7];
2881                         fcp->isp_sns_hdl = NPH_SNS_ID;
2882                         r = isp_register_fc4_type_24xx(isp, chan);
2883                         if (r == 0)
2884                                 isp_register_fc4_features_24xx(isp, chan);
2885                 } else {
2886                         fcp->isp_sns_hdl = SNS_ID;
2887                         r = isp_register_fc4_type(isp, chan);
2888                 }
2889                 if (r) {
2890                         isp_prt(isp, ISP_LOGWARN|ISP_LOG_SANCFG, "%s: register fc4 type failed", __func__);
2891                         return (-1);
2892                 }
2893         }
2894
2895 not_on_fabric:
2896         /* Get link speed. */
2897         fcp->isp_gbspeed = 1;
2898         if (IS_23XX(isp) || IS_24XX(isp)) {
2899                 MBSINIT(&mbs, MBOX_GET_SET_DATA_RATE, MBLOGALL, 3000000);
2900                 mbs.param[1] = MBGSD_GET_RATE;
2901                 /* mbs.param[2] undefined if we're just getting rate */
2902                 isp_mboxcmd(isp, &mbs);
2903                 if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
2904                         if (mbs.param[1] == MBGSD_10GB)
2905                                 fcp->isp_gbspeed = 10;
2906                         else if (mbs.param[1] == MBGSD_16GB)
2907                                 fcp->isp_gbspeed = 16;
2908                         else if (mbs.param[1] == MBGSD_8GB)
2909                                 fcp->isp_gbspeed = 8;
2910                         else if (mbs.param[1] == MBGSD_4GB)
2911                                 fcp->isp_gbspeed = 4;
2912                         else if (mbs.param[1] == MBGSD_2GB)
2913                                 fcp->isp_gbspeed = 2;
2914                         else if (mbs.param[1] == MBGSD_1GB)
2915                                 fcp->isp_gbspeed = 1;
2916                 }
2917         }
2918
2919         fcp->isp_loopstate = LOOP_LTEST_DONE;
2920         isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGCONFIG,
2921             "Chan %d WWPN %016jx WWNN %016jx",
2922             chan, (uintmax_t)fcp->isp_wwpn, (uintmax_t)fcp->isp_wwnn);
2923         isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGCONFIG,
2924             "Chan %d %dGb %s PortID 0x%06x LoopID 0x%02x",
2925             chan, fcp->isp_gbspeed, isp_fc_toponame(fcp), fcp->isp_portid,
2926             fcp->isp_loopid);
2927         isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC link test done", chan);
2928         return (0);
2929 }
2930
2931 /*
2932  * Complete the synchronization of our Port Database.
2933  *
2934  * At this point, we've scanned the local loop (if any) and the fabric
2935  * and performed fabric logins on all new devices.
2936  *
2937  * Our task here is to go through our port database removing any entities
2938  * that are still marked probational (issuing PLOGO for ones which we had
2939  * PLOGI'd into) or are dead, and notifying upper layers about new/changed
2940  * devices.
2941  */
2942 static int
2943 isp_pdb_sync(ispsoftc_t *isp, int chan)
2944 {
2945         fcparam *fcp = FCPARAM(isp, chan);
2946         fcportdb_t *lp;
2947         uint16_t dbidx;
2948
2949         if (fcp->isp_loopstate < LOOP_FSCAN_DONE) {
2950                 return (-1);
2951         }
2952         if (fcp->isp_loopstate > LOOP_SYNCING_PDB) {
2953                 return (0);
2954         }
2955
2956         isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC PDB sync", chan);
2957
2958         fcp->isp_loopstate = LOOP_SYNCING_PDB;
2959
2960         for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
2961                 lp = &fcp->portdb[dbidx];
2962
2963                 if (lp->state == FC_PORTDB_STATE_NIL)
2964                         continue;
2965                 if (lp->probational && lp->state != FC_PORTDB_STATE_ZOMBIE)
2966                         lp->state = FC_PORTDB_STATE_DEAD;
2967                 switch (lp->state) {
2968                 case FC_PORTDB_STATE_DEAD:
2969                         lp->state = FC_PORTDB_STATE_NIL;
2970                         isp_async(isp, ISPASYNC_DEV_GONE, chan, lp);
2971                         if (lp->autologin == 0) {
2972                                 (void) isp_plogx(isp, chan, lp->handle,
2973                                     lp->portid,
2974                                     PLOGX_FLG_CMD_LOGO |
2975                                     PLOGX_FLG_IMPLICIT |
2976                                     PLOGX_FLG_FREE_NPHDL, 0);
2977                         }
2978                         /*
2979                          * Note that we might come out of this with our state
2980                          * set to FC_PORTDB_STATE_ZOMBIE.
2981                          */
2982                         break;
2983                 case FC_PORTDB_STATE_NEW:
2984                         lp->state = FC_PORTDB_STATE_VALID;
2985                         isp_async(isp, ISPASYNC_DEV_ARRIVED, chan, lp);
2986                         break;
2987                 case FC_PORTDB_STATE_CHANGED:
2988                         lp->state = FC_PORTDB_STATE_VALID;
2989                         isp_async(isp, ISPASYNC_DEV_CHANGED, chan, lp);
2990                         lp->portid = lp->new_portid;
2991                         lp->prli_word3 = lp->new_prli_word3;
2992                         break;
2993                 case FC_PORTDB_STATE_VALID:
2994                         isp_async(isp, ISPASYNC_DEV_STAYED, chan, lp);
2995                         break;
2996                 case FC_PORTDB_STATE_ZOMBIE:
2997                         break;
2998                 default:
2999                         isp_prt(isp, ISP_LOGWARN,
3000                             "isp_pdb_sync: state %d for idx %d",
3001                             lp->state, dbidx);
3002                         isp_dump_portdb(isp, chan);
3003                 }
3004         }
3005
3006         /*
3007          * If we get here, we've for sure seen not only a valid loop
3008          * but know what is or isn't on it, so mark this for usage
3009          * in isp_start.
3010          */
3011         fcp->loop_seen_once = 1;
3012         fcp->isp_loopstate = LOOP_READY;
3013         isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC PDB sync done", chan);
3014         return (0);
3015 }
3016
3017 static void
3018 isp_pdb_add_update(ispsoftc_t *isp, int chan, isp_pdb_t *pdb)
3019 {
3020         fcportdb_t *lp;
3021         uint64_t wwnn, wwpn;
3022
3023         MAKE_WWN_FROM_NODE_NAME(wwnn, pdb->nodename);
3024         MAKE_WWN_FROM_NODE_NAME(wwpn, pdb->portname);
3025
3026         /* Search port database for the same WWPN. */
3027         if (isp_find_pdb_by_wwpn(isp, chan, wwpn, &lp)) {
3028                 if (!lp->probational) {
3029                         isp_prt(isp, ISP_LOGERR,
3030                             "Chan %d Port 0x%06x@0x%04x [%d] is not probational (0x%x)",
3031                             chan, lp->portid, lp->handle,
3032                             FC_PORTDB_TGT(isp, chan, lp), lp->state);
3033                         isp_dump_portdb(isp, chan);
3034                         return;
3035                 }
3036                 lp->probational = 0;
3037                 lp->node_wwn = wwnn;
3038
3039                 /* Old device, nothing new. */
3040                 if (lp->portid == pdb->portid &&
3041                     lp->handle == pdb->handle &&
3042                     lp->prli_word3 == pdb->prli_word3) {
3043                         if (lp->state != FC_PORTDB_STATE_NEW)
3044                                 lp->state = FC_PORTDB_STATE_VALID;
3045                         isp_prt(isp, ISP_LOG_SANCFG,
3046                             "Chan %d Port 0x%06x@0x%04x is valid",
3047                             chan, pdb->portid, pdb->handle);
3048                         return;
3049                 }
3050
3051                 /* Something has changed. */
3052                 lp->state = FC_PORTDB_STATE_CHANGED;
3053                 lp->handle = pdb->handle;
3054                 lp->new_portid = pdb->portid;
3055                 lp->new_prli_word3 = pdb->prli_word3;
3056                 isp_prt(isp, ISP_LOG_SANCFG,
3057                     "Chan %d Port 0x%06x@0x%04x is changed",
3058                     chan, pdb->portid, pdb->handle);
3059                 return;
3060         }
3061
3062         /* It seems like a new port. Find an empty slot for it. */
3063         if (!isp_find_pdb_empty(isp, chan, &lp)) {
3064                 isp_prt(isp, ISP_LOGERR, "Chan %d out of portdb entries", chan);
3065                 return;
3066         }
3067
3068         ISP_MEMZERO(lp, sizeof (fcportdb_t));
3069         lp->autologin = 1;
3070         lp->probational = 0;
3071         lp->state = FC_PORTDB_STATE_NEW;
3072         lp->portid = lp->new_portid = pdb->portid;
3073         lp->prli_word3 = lp->new_prli_word3 = pdb->prli_word3;
3074         lp->handle = pdb->handle;
3075         lp->port_wwn = wwpn;
3076         lp->node_wwn = wwnn;
3077         isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Port 0x%06x@0x%04x is new",
3078             chan, pdb->portid, pdb->handle);
3079 }
3080
3081 /*
3082  * Scan local loop for devices.
3083  */
3084 static int
3085 isp_scan_loop(ispsoftc_t *isp, int chan)
3086 {
3087         fcparam *fcp = FCPARAM(isp, chan);
3088         int idx, lim, r;
3089         isp_pdb_t pdb;
3090         uint16_t handles[LOCAL_LOOP_LIM];
3091         uint16_t handle;
3092
3093         if (fcp->isp_loopstate < LOOP_LTEST_DONE) {
3094                 return (-1);
3095         }
3096         if (fcp->isp_loopstate > LOOP_SCANNING_LOOP) {
3097                 return (0);
3098         }
3099         isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC loop scan", chan);
3100         if (TOPO_IS_FABRIC(fcp->isp_topo)) {
3101                 isp_prt(isp, ISP_LOG_SANCFG,
3102                     "Chan %d FC loop scan done (no loop)", chan);
3103                 fcp->isp_loopstate = LOOP_LSCAN_DONE;
3104                 return (0);
3105         }
3106         fcp->isp_loopstate = LOOP_SCANNING_LOOP;
3107
3108         lim = LOCAL_LOOP_LIM;
3109         r = isp_gethandles(isp, chan, handles, &lim, 1, 1);
3110         if (r != 0) {
3111                 isp_prt(isp, ISP_LOG_SANCFG,
3112                     "Chan %d Getting list of handles failed with %x", chan, r);
3113                 isp_prt(isp, ISP_LOG_SANCFG,
3114                     "Chan %d FC loop scan done (bad)", chan);
3115                 return (-1);
3116         }
3117
3118         isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Got %d handles",
3119             chan, lim);
3120
3121         /*
3122          * Run through the list and get the port database info for each one.
3123          */
3124         for (idx = 0; idx < lim; idx++) {
3125                 handle = handles[idx];
3126
3127                 /*
3128                  * Don't scan "special" ids.
3129                  */
3130                 if (ISP_CAP_2KLOGIN(isp)) {
3131                         if (handle >= NPH_RESERVED)
3132                                 continue;
3133                 } else {
3134                         if (handle >= FL_ID && handle <= SNS_ID)
3135                                 continue;
3136                 }
3137
3138                 /*
3139                  * In older cards with older f/w GET_PORT_DATABASE has been
3140                  * known to hang. This trick gets around that problem.
3141                  */
3142                 if (IS_2100(isp) || IS_2200(isp)) {
3143                         uint64_t node_wwn = isp_get_wwn(isp, chan, handle, 1);
3144                         if (fcp->isp_loopstate < LOOP_SCANNING_LOOP) {
3145 abort:
3146                                 isp_prt(isp, ISP_LOG_SANCFG,
3147                                     "Chan %d FC loop scan done (abort)", chan);
3148                                 return (-1);
3149                         }
3150                         if (node_wwn == INI_NONE) {
3151                                 continue;
3152                         }
3153                 }
3154
3155                 /*
3156                  * Get the port database entity for this index.
3157                  */
3158                 r = isp_getpdb(isp, chan, handle, &pdb, 1);
3159                 if (fcp->isp_loopstate < LOOP_SCANNING_LOOP)
3160                         goto abort;
3161                 if (r != 0) {
3162                         isp_prt(isp, ISP_LOGDEBUG1,
3163                             "Chan %d FC Scan Loop handle %d returned %x",
3164                             chan, handle, r);
3165                         if (fcp->isp_loopstate < LOOP_SCANNING_LOOP)
3166                                 goto abort;
3167                         continue;
3168                 }
3169
3170                 isp_pdb_add_update(isp, chan, &pdb);
3171         }
3172         if (fcp->isp_loopstate < LOOP_SCANNING_LOOP)
3173                 goto abort;
3174         fcp->isp_loopstate = LOOP_LSCAN_DONE;
3175         isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC loop scan done", chan);
3176         return (0);
3177 }
3178
3179 /*
3180  * Scan the fabric for devices and add them to our port database.
3181  *
3182  * Use the GID_FT command to get all Port IDs for FC4 SCSI devices it knows.
3183  *
3184  * For 2100-23XX cards, we can use the SNS mailbox command to pass simple
3185  * name server commands to the switch management server via the QLogic f/w.
3186  *
3187  * For the 24XX card, we have to use CT-Pass through run via the Execute IOCB
3188  * mailbox command.
3189  *
3190  * The net result is to leave the list of Port IDs setting untranslated in
3191  * offset IGPOFF of the FC scratch area, whereupon we'll canonicalize it to
3192  * host order at OGPOFF.
3193  */
3194
3195 /*
3196  * Take half of our scratch area to store Port IDs
3197  */
3198 #define GIDLEN  (ISP_FC_SCRLEN >> 1)
3199 #define NGENT   ((GIDLEN - 16) >> 2)
3200
3201 #define IGPOFF  (0)
3202 #define OGPOFF  (ISP_FC_SCRLEN >> 1)
3203 #define XTXOFF  (ISP_FC_SCRLEN - (3 * QENTRY_LEN))      /* CT request */
3204 #define CTXOFF  (ISP_FC_SCRLEN - (2 * QENTRY_LEN))      /* Request IOCB */
3205 #define ZTXOFF  (ISP_FC_SCRLEN - (1 * QENTRY_LEN))      /* Response IOCB */
3206
3207 static int
3208 isp_gid_ft_sns(ispsoftc_t *isp, int chan)
3209 {
3210         union {
3211                 sns_gid_ft_req_t _x;
3212                 uint8_t _y[SNS_GID_FT_REQ_SIZE];
3213         } un;
3214         fcparam *fcp = FCPARAM(isp, chan);
3215         sns_gid_ft_req_t *rq = &un._x;
3216         uint8_t *scp = fcp->isp_scratch;
3217         mbreg_t mbs;
3218
3219         isp_prt(isp, ISP_LOGDEBUG0, "Chan %d scanning fabric (GID_FT) via SNS", chan);
3220
3221         ISP_MEMZERO(rq, SNS_GID_FT_REQ_SIZE);
3222         rq->snscb_rblen = GIDLEN >> 1;
3223         rq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma + IGPOFF);
3224         rq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma + IGPOFF);
3225         rq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma + IGPOFF);
3226         rq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma + IGPOFF);
3227         rq->snscb_sblen = 6;
3228         rq->snscb_cmd = SNS_GID_FT;
3229         rq->snscb_mword_div_2 = NGENT;
3230         rq->snscb_fc4_type = FC4_SCSI;
3231
3232         isp_put_gid_ft_request(isp, rq, (sns_gid_ft_req_t *)&scp[CTXOFF]);
3233         MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GID_FT_REQ_SIZE, chan);
3234
3235         MBSINIT(&mbs, MBOX_SEND_SNS, MBLOGALL, 10000000);
3236         mbs.param[0] = MBOX_SEND_SNS;
3237         mbs.param[1] = SNS_GID_FT_REQ_SIZE >> 1;
3238         mbs.param[2] = DMA_WD1(fcp->isp_scdma + CTXOFF);
3239         mbs.param[3] = DMA_WD0(fcp->isp_scdma + CTXOFF);
3240         mbs.param[6] = DMA_WD3(fcp->isp_scdma + CTXOFF);
3241         mbs.param[7] = DMA_WD2(fcp->isp_scdma + CTXOFF);
3242         isp_mboxcmd(isp, &mbs);
3243         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3244                 if (mbs.param[0] == MBOX_INVALID_COMMAND) {
3245                         return (1);
3246                 } else {
3247                         return (-1);
3248                 }
3249         }
3250         return (0);
3251 }
3252
3253 static int
3254 isp_gid_ft_ct_passthru(ispsoftc_t *isp, int chan)
3255 {
3256         mbreg_t mbs;
3257         fcparam *fcp = FCPARAM(isp, chan);
3258         union {
3259                 isp_ct_pt_t plocal;
3260                 ct_hdr_t clocal;
3261                 uint8_t q[QENTRY_LEN];
3262         } un;
3263         isp_ct_pt_t *pt;
3264         ct_hdr_t *ct;
3265         uint32_t *rp;
3266         uint8_t *scp = fcp->isp_scratch;
3267
3268         isp_prt(isp, ISP_LOGDEBUG0, "Chan %d scanning fabric (GID_FT) via CT", chan);
3269
3270         /*
3271          * Build a Passthrough IOCB in memory.
3272          */
3273         pt = &un.plocal;
3274         ISP_MEMZERO(un.q, QENTRY_LEN);
3275         pt->ctp_header.rqs_entry_count = 1;
3276         pt->ctp_header.rqs_entry_type = RQSTYPE_CT_PASSTHRU;
3277         pt->ctp_handle = 0xffffffff;
3278         pt->ctp_nphdl = fcp->isp_sns_hdl;
3279         pt->ctp_cmd_cnt = 1;
3280         pt->ctp_vpidx = ISP_GET_VPIDX(isp, chan);
3281         pt->ctp_time = 30;
3282         pt->ctp_rsp_cnt = 1;
3283         pt->ctp_rsp_bcnt = GIDLEN;
3284         pt->ctp_cmd_bcnt = sizeof (*ct) + sizeof (uint32_t);
3285         pt->ctp_dataseg[0].ds_base = DMA_LO32(fcp->isp_scdma+XTXOFF);
3286         pt->ctp_dataseg[0].ds_basehi = DMA_HI32(fcp->isp_scdma+XTXOFF);
3287         pt->ctp_dataseg[0].ds_count = sizeof (*ct) + sizeof (uint32_t);
3288         pt->ctp_dataseg[1].ds_base = DMA_LO32(fcp->isp_scdma+IGPOFF);
3289         pt->ctp_dataseg[1].ds_basehi = DMA_HI32(fcp->isp_scdma+IGPOFF);
3290         pt->ctp_dataseg[1].ds_count = GIDLEN;
3291         if (isp->isp_dblev & ISP_LOGDEBUG1) {
3292                 isp_print_bytes(isp, "ct IOCB", QENTRY_LEN, pt);
3293         }
3294         isp_put_ct_pt(isp, pt, (isp_ct_pt_t *) &scp[CTXOFF]);
3295
3296         /*
3297          * Build the CT header and command in memory.
3298          *
3299          * Note that the CT header has to end up as Big Endian format in memory.
3300          */
3301         ct = &un.clocal;
3302         ISP_MEMZERO(ct, sizeof (*ct));
3303         ct->ct_revision = CT_REVISION;
3304         ct->ct_fcs_type = CT_FC_TYPE_FC;
3305         ct->ct_fcs_subtype = CT_FC_SUBTYPE_NS;
3306         ct->ct_cmd_resp = SNS_GID_FT;
3307         ct->ct_bcnt_resid = (GIDLEN - 16) >> 2;
3308
3309         isp_put_ct_hdr(isp, ct, (ct_hdr_t *) &scp[XTXOFF]);
3310         rp = (uint32_t *) &scp[XTXOFF+sizeof (*ct)];
3311         ISP_IOZPUT_32(isp, FC4_SCSI, rp);
3312         if (isp->isp_dblev & ISP_LOGDEBUG1) {
3313                 isp_print_bytes(isp, "CT HDR + payload after put",
3314                     sizeof (*ct) + sizeof (uint32_t), &scp[XTXOFF]);
3315         }
3316         ISP_MEMZERO(&scp[ZTXOFF], QENTRY_LEN);
3317         MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 500000);
3318         mbs.param[1] = QENTRY_LEN;
3319         mbs.param[2] = DMA_WD1(fcp->isp_scdma + CTXOFF);
3320         mbs.param[3] = DMA_WD0(fcp->isp_scdma + CTXOFF);
3321         mbs.param[6] = DMA_WD3(fcp->isp_scdma + CTXOFF);
3322         mbs.param[7] = DMA_WD2(fcp->isp_scdma + CTXOFF);
3323         MEMORYBARRIER(isp, SYNC_SFORDEV, XTXOFF, 2 * QENTRY_LEN, chan);
3324         isp_mboxcmd(isp, &mbs);
3325         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3326                 return (-1);
3327         }
3328         MEMORYBARRIER(isp, SYNC_SFORCPU, ZTXOFF, QENTRY_LEN, chan);
3329         pt = &un.plocal;
3330         isp_get_ct_pt(isp, (isp_ct_pt_t *) &scp[ZTXOFF], pt);
3331         if (isp->isp_dblev & ISP_LOGDEBUG1) {
3332                 isp_print_bytes(isp, "IOCB response", QENTRY_LEN, pt);
3333         }
3334
3335         if (pt->ctp_status && pt->ctp_status != RQCS_DATA_UNDERRUN) {
3336                 isp_prt(isp, ISP_LOGWARN,
3337                     "Chan %d ISP GID FT CT Passthrough returned 0x%x",
3338                     chan, pt->ctp_status);
3339                 return (-1);
3340         }
3341         MEMORYBARRIER(isp, SYNC_SFORCPU, IGPOFF, GIDLEN, chan);
3342         if (isp->isp_dblev & ISP_LOGDEBUG1) {
3343                 isp_print_bytes(isp, "CT response", GIDLEN, &scp[IGPOFF]);
3344         }
3345         return (0);
3346 }
3347
3348 static int
3349 isp_scan_fabric(ispsoftc_t *isp, int chan)
3350 {
3351         fcparam *fcp = FCPARAM(isp, chan);
3352         fcportdb_t *lp;
3353         uint32_t portid;
3354         uint16_t nphdl;
3355         isp_pdb_t pdb;
3356         int portidx, portlim, r;
3357         sns_gid_ft_rsp_t *rs0, *rs1;
3358
3359         if (fcp->isp_loopstate < LOOP_LSCAN_DONE) {
3360                 return (-1);
3361         }
3362         if (fcp->isp_loopstate > LOOP_SCANNING_FABRIC) {
3363                 return (0);
3364         }
3365         isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC fabric scan", chan);
3366         if (!TOPO_IS_FABRIC(fcp->isp_topo)) {
3367                 fcp->isp_loopstate = LOOP_FSCAN_DONE;
3368                 isp_prt(isp, ISP_LOG_SANCFG,
3369                     "Chan %d FC fabric scan done (no fabric)", chan);
3370                 return (0);
3371         }
3372         fcp->isp_loopstate = LOOP_SCANNING_FABRIC;
3373
3374         if (FC_SCRATCH_ACQUIRE(isp, chan)) {
3375                 isp_prt(isp, ISP_LOGERR, sacq);
3376 fail:
3377                 isp_prt(isp, ISP_LOG_SANCFG,
3378                     "Chan %d FC fabric scan done (bad)", chan);
3379                 return (-1);
3380         }
3381         if (fcp->isp_loopstate < LOOP_SCANNING_FABRIC) {
3382 abort:
3383                 FC_SCRATCH_RELEASE(isp, chan);
3384                 isp_prt(isp, ISP_LOG_SANCFG,
3385                     "Chan %d FC fabric scan done (abort)", chan);
3386                 return (-1);
3387         }
3388
3389         /*
3390          * Make sure we still are logged into the fabric controller.
3391          */
3392         nphdl = IS_24XX(isp) ? NPH_FL_ID : FL_ID;
3393         r = isp_getpdb(isp, chan, nphdl, &pdb, 0);
3394         if ((r & 0xffff) == MBOX_NOT_LOGGED_IN) {
3395                 isp_dump_chip_portdb(isp, chan, 0);
3396         }
3397         if (r) {
3398                 fcp->isp_loopstate = LOOP_LTEST_DONE;
3399                 FC_SCRATCH_RELEASE(isp, chan);
3400                 goto fail;
3401         }
3402
3403         /* Get list of port IDs from SNS. */
3404         if (IS_24XX(isp))
3405                 r = isp_gid_ft_ct_passthru(isp, chan);
3406         else
3407                 r = isp_gid_ft_sns(isp, chan);
3408         if (fcp->isp_loopstate < LOOP_SCANNING_FABRIC)
3409                 goto abort;
3410         if (r > 0) {
3411                 fcp->isp_loopstate = LOOP_FSCAN_DONE;
3412                 FC_SCRATCH_RELEASE(isp, chan);
3413                 return (0);
3414         } else if (r < 0) {
3415                 fcp->isp_loopstate = LOOP_LTEST_DONE;   /* try again */
3416                 FC_SCRATCH_RELEASE(isp, chan);
3417                 return (0);
3418         }
3419
3420         MEMORYBARRIER(isp, SYNC_SFORCPU, IGPOFF, GIDLEN, chan);
3421         rs0 = (sns_gid_ft_rsp_t *) ((uint8_t *)fcp->isp_scratch+IGPOFF);
3422         rs1 = (sns_gid_ft_rsp_t *) ((uint8_t *)fcp->isp_scratch+OGPOFF);
3423         isp_get_gid_ft_response(isp, rs0, rs1, NGENT);
3424         if (fcp->isp_loopstate < LOOP_SCANNING_FABRIC)
3425                 goto abort;
3426         if (rs1->snscb_cthdr.ct_cmd_resp != LS_ACC) {
3427                 int level;
3428                 if (rs1->snscb_cthdr.ct_reason == 9 && rs1->snscb_cthdr.ct_explanation == 7) {
3429                         level = ISP_LOG_SANCFG;
3430                 } else {
3431                         level = ISP_LOGWARN;
3432                 }
3433                 isp_prt(isp, level, "Chan %d Fabric Nameserver rejected GID_FT"
3434                     " (Reason=0x%x Expl=0x%x)", chan,
3435                     rs1->snscb_cthdr.ct_reason,
3436                     rs1->snscb_cthdr.ct_explanation);
3437                 FC_SCRATCH_RELEASE(isp, chan);
3438                 fcp->isp_loopstate = LOOP_FSCAN_DONE;
3439                 return (0);
3440         }
3441
3442         /* Check our buffer was big enough to get the full list. */
3443         for (portidx = 0; portidx < NGENT-1; portidx++) {
3444                 if (rs1->snscb_ports[portidx].control & 0x80)
3445                         break;
3446         }
3447         if ((rs1->snscb_ports[portidx].control & 0x80) == 0) {
3448                 isp_prt(isp, ISP_LOGWARN,
3449                     "fabric too big for scratch area: increase ISP_FC_SCRLEN");
3450         }
3451         portlim = portidx + 1;
3452         isp_prt(isp, ISP_LOG_SANCFG,
3453             "Chan %d Got %d ports back from name server", chan, portlim);
3454
3455         /* Go through the list and remove duplicate port ids. */
3456         for (portidx = 0; portidx < portlim; portidx++) {
3457                 int npidx;
3458
3459                 portid =
3460                     ((rs1->snscb_ports[portidx].portid[0]) << 16) |
3461                     ((rs1->snscb_ports[portidx].portid[1]) << 8) |
3462                     ((rs1->snscb_ports[portidx].portid[2]));
3463
3464                 for (npidx = portidx + 1; npidx < portlim; npidx++) {
3465                         uint32_t new_portid =
3466                             ((rs1->snscb_ports[npidx].portid[0]) << 16) |
3467                             ((rs1->snscb_ports[npidx].portid[1]) << 8) |
3468                             ((rs1->snscb_ports[npidx].portid[2]));
3469                         if (new_portid == portid) {
3470                                 break;
3471                         }
3472                 }
3473
3474                 if (npidx < portlim) {
3475                         rs1->snscb_ports[npidx].portid[0] = 0;
3476                         rs1->snscb_ports[npidx].portid[1] = 0;
3477                         rs1->snscb_ports[npidx].portid[2] = 0;
3478                         isp_prt(isp, ISP_LOG_SANCFG, "Chan %d removing duplicate PortID 0x%06x entry from list", chan, portid);
3479                 }
3480         }
3481
3482         /*
3483          * We now have a list of Port IDs for all FC4 SCSI devices
3484          * that the Fabric Name server knows about.
3485          *
3486          * For each entry on this list go through our port database looking
3487          * for probational entries- if we find one, then an old entry is
3488          * maybe still this one. We get some information to find out.
3489          *
3490          * Otherwise, it's a new fabric device, and we log into it
3491          * (unconditionally). After searching the entire database
3492          * again to make sure that we never ever ever ever have more
3493          * than one entry that has the same PortID or the same
3494          * WWNN/WWPN duple, we enter the device into our database.
3495          */
3496
3497         for (portidx = 0; portidx < portlim; portidx++) {
3498                 portid = ((rs1->snscb_ports[portidx].portid[0]) << 16) |
3499                          ((rs1->snscb_ports[portidx].portid[1]) << 8) |
3500                          ((rs1->snscb_ports[portidx].portid[2]));
3501                 isp_prt(isp, ISP_LOG_SANCFG,
3502                     "Chan %d Checking fabric port 0x%06x", chan, portid);
3503                 if (portid == 0) {
3504                         isp_prt(isp, ISP_LOG_SANCFG,
3505                             "Chan %d Skipping null PortID at idx %d",
3506                             chan, portidx);
3507                         continue;
3508                 }
3509                 if (portid == fcp->isp_portid) {
3510                         isp_prt(isp, ISP_LOG_SANCFG,
3511                             "Chan %d Skipping our PortID 0x%06x", chan, portid);
3512                         continue;
3513                 }
3514
3515                 /* Now search the entire port database for the same portid. */
3516                 if (isp_find_pdb_by_portid(isp, chan, portid, &lp)) {
3517                         if (!lp->probational) {
3518                                 isp_prt(isp, ISP_LOGERR,
3519                                     "Chan %d Port 0x%06x@0x%04x [%d] is not probational (0x%x)",
3520                                     chan, lp->portid, lp->handle,
3521                                     FC_PORTDB_TGT(isp, chan, lp), lp->state);
3522                                 FC_SCRATCH_RELEASE(isp, chan);
3523                                 isp_dump_portdb(isp, chan);
3524                                 goto fail;
3525                         }
3526
3527                         /*
3528                          * See if we're still logged into it.
3529                          *
3530                          * If we aren't, mark it as a dead device and
3531                          * leave the new portid in the database entry
3532                          * for somebody further along to decide what to
3533                          * do (policy choice).
3534                          *
3535                          * If we are, check to see if it's the same
3536                          * device still (it should be). If for some
3537                          * reason it isn't, mark it as a changed device
3538                          * and leave the new portid and role in the
3539                          * database entry for somebody further along to
3540                          * decide what to do (policy choice).
3541                          */
3542                         r = isp_getpdb(isp, chan, lp->handle, &pdb, 0);
3543                         if (fcp->isp_loopstate < LOOP_SCANNING_FABRIC)
3544                                 goto abort;
3545                         if (r != 0) {
3546                                 lp->state = FC_PORTDB_STATE_DEAD;
3547                                 isp_prt(isp, ISP_LOG_SANCFG,
3548                                     "Chan %d Port 0x%06x handle 0x%x is dead (%d)",
3549                                     chan, portid, lp->handle, r);
3550                                 goto relogin;
3551                         }
3552
3553                         isp_pdb_add_update(isp, chan, &pdb);
3554                         continue;
3555                 }
3556
3557 relogin:
3558                 if ((fcp->role & ISP_ROLE_INITIATOR) == 0)
3559                         continue;
3560
3561                 if (isp_login_device(isp, chan, portid, &pdb,
3562                     &FCPARAM(isp, 0)->isp_lasthdl)) {
3563                         if (fcp->isp_loopstate < LOOP_SCANNING_FABRIC)
3564                                 goto abort;
3565                         continue;
3566                 }
3567
3568                 isp_pdb_add_update(isp, chan, &pdb);
3569         }
3570
3571         if (fcp->isp_loopstate < LOOP_SCANNING_FABRIC)
3572                 goto abort;
3573         FC_SCRATCH_RELEASE(isp, chan);
3574         fcp->isp_loopstate = LOOP_FSCAN_DONE;
3575         isp_prt(isp, ISP_LOG_SANCFG, "Chan %d FC fabric scan done", chan);
3576         return (0);
3577 }
3578
3579 /*
3580  * Find an unused handle and try and use to login to a port.
3581  */
3582 static int
3583 isp_login_device(ispsoftc_t *isp, int chan, uint32_t portid, isp_pdb_t *p, uint16_t *ohp)
3584 {
3585         int lim, i, r;
3586         uint16_t handle;
3587
3588         if (ISP_CAP_2KLOGIN(isp)) {
3589                 lim = NPH_MAX_2K;
3590         } else {
3591                 lim = NPH_MAX;
3592         }
3593
3594         handle = isp_next_handle(isp, ohp);
3595         for (i = 0; i < lim; i++) {
3596                 if (FCPARAM(isp, chan)->isp_loopstate != LOOP_SCANNING_FABRIC)
3597                         return (-1);
3598
3599                 /* Check if this handle is free. */
3600                 r = isp_getpdb(isp, chan, handle, p, 0);
3601                 if (r == 0) {
3602                         if (p->portid != portid) {
3603                                 /* This handle is busy, try next one. */
3604                                 handle = isp_next_handle(isp, ohp);
3605                                 continue;
3606                         }
3607                         break;
3608                 }
3609                 if (FCPARAM(isp, chan)->isp_loopstate != LOOP_SCANNING_FABRIC)
3610                         return (-1);
3611
3612                 /*
3613                  * Now try and log into the device
3614                  */
3615                 r = isp_plogx(isp, chan, handle, portid, PLOGX_FLG_CMD_PLOGI, 1);
3616                 if (r == 0) {
3617                         break;
3618                 } else if ((r & 0xffff) == MBOX_PORT_ID_USED) {
3619                         /*
3620                          * If we get here, then the firmwware still thinks we're logged into this device, but with a different
3621                          * handle. We need to break that association. We used to try and just substitute the handle, but then
3622                          * failed to get any data via isp_getpdb (below).
3623                          */
3624                         if (isp_plogx(isp, chan, r >> 16, portid, PLOGX_FLG_CMD_LOGO | PLOGX_FLG_IMPLICIT | PLOGX_FLG_FREE_NPHDL, 1)) {
3625                                 isp_prt(isp, ISP_LOGERR, "baw... logout of %x failed", r >> 16);
3626                         }
3627                         if (FCPARAM(isp, chan)->isp_loopstate != LOOP_SCANNING_FABRIC)
3628                                 return (-1);
3629                         r = isp_plogx(isp, chan, handle, portid, PLOGX_FLG_CMD_PLOGI, 1);
3630                         if (r != 0)
3631                                 i = lim;
3632                         break;
3633                 } else if ((r & 0xffff) == MBOX_LOOP_ID_USED) {
3634                         /* Try the next handle. */
3635                         handle = isp_next_handle(isp, ohp);
3636                 } else {
3637                         /* Give up. */
3638                         i = lim;
3639                         break;
3640                 }
3641         }
3642
3643         if (i == lim) {
3644                 isp_prt(isp, ISP_LOGWARN, "Chan %d PLOGI 0x%06x failed", chan, portid);
3645                 return (-1);
3646         }
3647
3648         /*
3649          * If we successfully logged into it, get the PDB for it
3650          * so we can crosscheck that it is still what we think it
3651          * is and that we also have the role it plays
3652          */
3653         r = isp_getpdb(isp, chan, handle, p, 0);
3654         if (r != 0) {
3655                 isp_prt(isp, ISP_LOGERR, "Chan %d new device 0x%06x@0x%x disappeared", chan, portid, handle);
3656                 return (-1);
3657         }
3658
3659         if (p->handle != handle || p->portid != portid) {
3660                 isp_prt(isp, ISP_LOGERR, "Chan %d new device 0x%06x@0x%x changed (0x%06x@0x%0x)",
3661                     chan, portid, handle, p->portid, p->handle);
3662                 return (-1);
3663         }
3664         return (0);
3665 }
3666
3667 static int
3668 isp_register_fc4_type(ispsoftc_t *isp, int chan)
3669 {
3670         fcparam *fcp = FCPARAM(isp, chan);
3671         uint8_t local[SNS_RFT_ID_REQ_SIZE];
3672         sns_screq_t *reqp = (sns_screq_t *) local;
3673         mbreg_t mbs;
3674
3675         ISP_MEMZERO((void *) reqp, SNS_RFT_ID_REQ_SIZE);
3676         reqp->snscb_rblen = SNS_RFT_ID_RESP_SIZE >> 1;
3677         reqp->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma + 0x100);
3678         reqp->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma + 0x100);
3679         reqp->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma + 0x100);
3680         reqp->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma + 0x100);
3681         reqp->snscb_sblen = 22;
3682         reqp->snscb_data[0] = SNS_RFT_ID;
3683         reqp->snscb_data[4] = fcp->isp_portid & 0xffff;
3684         reqp->snscb_data[5] = (fcp->isp_portid >> 16) & 0xff;
3685         reqp->snscb_data[6] = (1 << FC4_SCSI);
3686         if (FC_SCRATCH_ACQUIRE(isp, chan)) {
3687                 isp_prt(isp, ISP_LOGERR, sacq);
3688                 return (-1);
3689         }
3690         isp_put_sns_request(isp, reqp, (sns_screq_t *) fcp->isp_scratch);
3691         MBSINIT(&mbs, MBOX_SEND_SNS, MBLOGALL, 1000000);
3692         mbs.param[1] = SNS_RFT_ID_REQ_SIZE >> 1;
3693         mbs.param[2] = DMA_WD1(fcp->isp_scdma);
3694         mbs.param[3] = DMA_WD0(fcp->isp_scdma);
3695         mbs.param[6] = DMA_WD3(fcp->isp_scdma);
3696         mbs.param[7] = DMA_WD2(fcp->isp_scdma);
3697         MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_RFT_ID_REQ_SIZE, chan);
3698         isp_mboxcmd(isp, &mbs);
3699         FC_SCRATCH_RELEASE(isp, chan);
3700         if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
3701                 return (0);
3702         } else {
3703                 return (-1);
3704         }
3705 }
3706
3707 static int
3708 isp_register_fc4_type_24xx(ispsoftc_t *isp, int chan)
3709 {
3710         mbreg_t mbs;
3711         fcparam *fcp = FCPARAM(isp, chan);
3712         union {
3713                 isp_ct_pt_t plocal;
3714                 rft_id_t clocal;
3715                 uint8_t q[QENTRY_LEN];
3716         } un;
3717         isp_ct_pt_t *pt;
3718         ct_hdr_t *ct;
3719         rft_id_t *rp;
3720         uint8_t *scp = fcp->isp_scratch;
3721
3722         if (FC_SCRATCH_ACQUIRE(isp, chan)) {
3723                 isp_prt(isp, ISP_LOGERR, sacq);
3724                 return (-1);
3725         }
3726
3727         /*
3728          * Build a Passthrough IOCB in memory.
3729          */
3730         ISP_MEMZERO(un.q, QENTRY_LEN);
3731         pt = &un.plocal;
3732         pt->ctp_header.rqs_entry_count = 1;
3733         pt->ctp_header.rqs_entry_type = RQSTYPE_CT_PASSTHRU;
3734         pt->ctp_handle = 0xffffffff;
3735         pt->ctp_nphdl = fcp->isp_sns_hdl;
3736         pt->ctp_cmd_cnt = 1;
3737         pt->ctp_vpidx = ISP_GET_VPIDX(isp, chan);
3738         pt->ctp_time = 1;
3739         pt->ctp_rsp_cnt = 1;
3740         pt->ctp_rsp_bcnt = sizeof (ct_hdr_t);
3741         pt->ctp_cmd_bcnt = sizeof (rft_id_t);
3742         pt->ctp_dataseg[0].ds_base = DMA_LO32(fcp->isp_scdma+XTXOFF);
3743         pt->ctp_dataseg[0].ds_basehi = DMA_HI32(fcp->isp_scdma+XTXOFF);
3744         pt->ctp_dataseg[0].ds_count = sizeof (rft_id_t);
3745         pt->ctp_dataseg[1].ds_base = DMA_LO32(fcp->isp_scdma+IGPOFF);
3746         pt->ctp_dataseg[1].ds_basehi = DMA_HI32(fcp->isp_scdma+IGPOFF);
3747         pt->ctp_dataseg[1].ds_count = sizeof (ct_hdr_t);
3748         isp_put_ct_pt(isp, pt, (isp_ct_pt_t *) &scp[CTXOFF]);
3749         if (isp->isp_dblev & ISP_LOGDEBUG1) {
3750                 isp_print_bytes(isp, "IOCB CT Request", QENTRY_LEN, pt);
3751         }
3752
3753         /*
3754          * Build the CT header and command in memory.
3755          *
3756          * Note that the CT header has to end up as Big Endian format in memory.
3757          */
3758         ISP_MEMZERO(&un.clocal, sizeof (un.clocal));
3759         ct = &un.clocal.rftid_hdr;
3760         ct->ct_revision = CT_REVISION;
3761         ct->ct_fcs_type = CT_FC_TYPE_FC;
3762         ct->ct_fcs_subtype = CT_FC_SUBTYPE_NS;
3763         ct->ct_cmd_resp = SNS_RFT_ID;
3764         ct->ct_bcnt_resid = (sizeof (rft_id_t) - sizeof (ct_hdr_t)) >> 2;
3765         rp = &un.clocal;
3766         rp->rftid_portid[0] = fcp->isp_portid >> 16;
3767         rp->rftid_portid[1] = fcp->isp_portid >> 8;
3768         rp->rftid_portid[2] = fcp->isp_portid;
3769         rp->rftid_fc4types[FC4_SCSI >> 5] = 1 << (FC4_SCSI & 0x1f);
3770         isp_put_rft_id(isp, rp, (rft_id_t *) &scp[XTXOFF]);
3771         if (isp->isp_dblev & ISP_LOGDEBUG1) {
3772                 isp_print_bytes(isp, "CT Header", QENTRY_LEN, &scp[XTXOFF]);
3773         }
3774
3775         ISP_MEMZERO(&scp[ZTXOFF], sizeof (ct_hdr_t));
3776
3777         MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 1000000);
3778         mbs.param[1] = QENTRY_LEN;
3779         mbs.param[2] = DMA_WD1(fcp->isp_scdma + CTXOFF);
3780         mbs.param[3] = DMA_WD0(fcp->isp_scdma + CTXOFF);
3781         mbs.param[6] = DMA_WD3(fcp->isp_scdma + CTXOFF);
3782         mbs.param[7] = DMA_WD2(fcp->isp_scdma + CTXOFF);
3783         MEMORYBARRIER(isp, SYNC_SFORDEV, XTXOFF, 2 * QENTRY_LEN, chan);
3784         isp_mboxcmd(isp, &mbs);
3785         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3786                 FC_SCRATCH_RELEASE(isp, chan);
3787                 return (-1);
3788         }
3789         MEMORYBARRIER(isp, SYNC_SFORCPU, ZTXOFF, QENTRY_LEN, chan);
3790         pt = &un.plocal;
3791         isp_get_ct_pt(isp, (isp_ct_pt_t *) &scp[ZTXOFF], pt);
3792         if (isp->isp_dblev & ISP_LOGDEBUG1) {
3793                 isp_print_bytes(isp, "IOCB response", QENTRY_LEN, pt);
3794         }
3795         if (pt->ctp_status) {
3796                 FC_SCRATCH_RELEASE(isp, chan);
3797                 isp_prt(isp, ISP_LOGWARN,
3798                     "Chan %d Register FC4 Type CT Passthrough returned 0x%x",
3799                     chan, pt->ctp_status);
3800                 return (1);
3801         }
3802
3803         isp_get_ct_hdr(isp, (ct_hdr_t *) &scp[IGPOFF], ct);
3804         FC_SCRATCH_RELEASE(isp, chan);
3805
3806         if (ct->ct_cmd_resp == LS_RJT) {
3807                 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOG_WARN1, "Chan %d Register FC4 Type rejected", chan);
3808                 return (-1);
3809         } else if (ct->ct_cmd_resp == LS_ACC) {
3810                 isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Register FC4 Type accepted", chan);
3811                 return (0);
3812         } else {
3813                 isp_prt(isp, ISP_LOGWARN, "Chan %d Register FC4 Type: 0x%x", chan, ct->ct_cmd_resp);
3814                 return (-1);
3815         }
3816 }
3817
3818 static int
3819 isp_register_fc4_features_24xx(ispsoftc_t *isp, int chan)
3820 {
3821         mbreg_t mbs;
3822         fcparam *fcp = FCPARAM(isp, chan);
3823         union {
3824                 isp_ct_pt_t plocal;
3825                 rff_id_t clocal;
3826                 uint8_t q[QENTRY_LEN];
3827         } un;
3828         isp_ct_pt_t *pt;
3829         ct_hdr_t *ct;
3830         rff_id_t *rp;
3831         uint8_t *scp = fcp->isp_scratch;
3832
3833         if (FC_SCRATCH_ACQUIRE(isp, chan)) {
3834                 isp_prt(isp, ISP_LOGERR, sacq);
3835                 return (-1);
3836         }
3837
3838         /*
3839          * Build a Passthrough IOCB in memory.
3840          */
3841         ISP_MEMZERO(un.q, QENTRY_LEN);
3842         pt = &un.plocal;
3843         pt->ctp_header.rqs_entry_count = 1;
3844         pt->ctp_header.rqs_entry_type = RQSTYPE_CT_PASSTHRU;
3845         pt->ctp_handle = 0xffffffff;
3846         pt->ctp_nphdl = fcp->isp_sns_hdl;
3847         pt->ctp_cmd_cnt = 1;
3848         pt->ctp_vpidx = ISP_GET_VPIDX(isp, chan);
3849         pt->ctp_time = 1;
3850         pt->ctp_rsp_cnt = 1;
3851         pt->ctp_rsp_bcnt = sizeof (ct_hdr_t);
3852         pt->ctp_cmd_bcnt = sizeof (rff_id_t);
3853         pt->ctp_dataseg[0].ds_base = DMA_LO32(fcp->isp_scdma+XTXOFF);
3854         pt->ctp_dataseg[0].ds_basehi = DMA_HI32(fcp->isp_scdma+XTXOFF);
3855         pt->ctp_dataseg[0].ds_count = sizeof (rff_id_t);
3856         pt->ctp_dataseg[1].ds_base = DMA_LO32(fcp->isp_scdma+IGPOFF);
3857         pt->ctp_dataseg[1].ds_basehi = DMA_HI32(fcp->isp_scdma+IGPOFF);
3858         pt->ctp_dataseg[1].ds_count = sizeof (ct_hdr_t);
3859         isp_put_ct_pt(isp, pt, (isp_ct_pt_t *) &scp[CTXOFF]);
3860         if (isp->isp_dblev & ISP_LOGDEBUG1) {
3861                 isp_print_bytes(isp, "IOCB CT Request", QENTRY_LEN, pt);
3862         }
3863
3864         /*
3865          * Build the CT header and command in memory.
3866          *
3867          * Note that the CT header has to end up as Big Endian format in memory.
3868          */
3869         ISP_MEMZERO(&un.clocal, sizeof (un.clocal));
3870         ct = &un.clocal.rffid_hdr;
3871         ct->ct_revision = CT_REVISION;
3872         ct->ct_fcs_type = CT_FC_TYPE_FC;
3873         ct->ct_fcs_subtype = CT_FC_SUBTYPE_NS;
3874         ct->ct_cmd_resp = SNS_RFF_ID;
3875         ct->ct_bcnt_resid = (sizeof (rff_id_t) - sizeof (ct_hdr_t)) >> 2;
3876         rp = &un.clocal;
3877         rp->rffid_portid[0] = fcp->isp_portid >> 16;
3878         rp->rffid_portid[1] = fcp->isp_portid >> 8;
3879         rp->rffid_portid[2] = fcp->isp_portid;
3880         rp->rffid_fc4features = 0;
3881         if (fcp->role & ISP_ROLE_TARGET)
3882                 rp->rffid_fc4features |= 1;
3883         if (fcp->role & ISP_ROLE_INITIATOR)
3884                 rp->rffid_fc4features |= 2;
3885         rp->rffid_fc4type = FC4_SCSI;
3886         isp_put_rff_id(isp, rp, (rff_id_t *) &scp[XTXOFF]);
3887         if (isp->isp_dblev & ISP_LOGDEBUG1) {
3888                 isp_print_bytes(isp, "CT Header", QENTRY_LEN, &scp[XTXOFF]);
3889         }
3890
3891         ISP_MEMZERO(&scp[ZTXOFF], sizeof (ct_hdr_t));
3892
3893         MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 1000000);
3894         mbs.param[1] = QENTRY_LEN;
3895         mbs.param[2] = DMA_WD1(fcp->isp_scdma + CTXOFF);
3896         mbs.param[3] = DMA_WD0(fcp->isp_scdma + CTXOFF);
3897         mbs.param[6] = DMA_WD3(fcp->isp_scdma + CTXOFF);
3898         mbs.param[7] = DMA_WD2(fcp->isp_scdma + CTXOFF);
3899         MEMORYBARRIER(isp, SYNC_SFORDEV, XTXOFF, 2 * QENTRY_LEN, chan);
3900         isp_mboxcmd(isp, &mbs);
3901         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3902                 FC_SCRATCH_RELEASE(isp, chan);
3903                 return (-1);
3904         }
3905         MEMORYBARRIER(isp, SYNC_SFORCPU, ZTXOFF, QENTRY_LEN, chan);
3906         pt = &un.plocal;
3907         isp_get_ct_pt(isp, (isp_ct_pt_t *) &scp[ZTXOFF], pt);
3908         if (isp->isp_dblev & ISP_LOGDEBUG1) {
3909                 isp_print_bytes(isp, "IOCB response", QENTRY_LEN, pt);
3910         }
3911         if (pt->ctp_status) {
3912                 FC_SCRATCH_RELEASE(isp, chan);
3913                 isp_prt(isp, ISP_LOGWARN,
3914                     "Chan %d Register FC4 Features CT Passthrough returned 0x%x",
3915                     chan, pt->ctp_status);
3916                 return (1);
3917         }
3918
3919         isp_get_ct_hdr(isp, (ct_hdr_t *) &scp[IGPOFF], ct);
3920         FC_SCRATCH_RELEASE(isp, chan);
3921
3922         if (ct->ct_cmd_resp == LS_RJT) {
3923                 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOG_WARN1,
3924                     "Chan %d Register FC4 Features rejected", chan);
3925                 return (-1);
3926         } else if (ct->ct_cmd_resp == LS_ACC) {
3927                 isp_prt(isp, ISP_LOG_SANCFG,
3928                     "Chan %d Register FC4 Features accepted", chan);
3929                 return (0);
3930         } else {
3931                 isp_prt(isp, ISP_LOGWARN,
3932                     "Chan %d Register FC4 Features: 0x%x", chan, ct->ct_cmd_resp);
3933                 return (-1);
3934         }
3935 }
3936
3937 static uint16_t
3938 isp_next_handle(ispsoftc_t *isp, uint16_t *ohp)
3939 {
3940         fcparam *fcp;
3941         int i, chan, wrap;
3942         uint16_t handle, minh, maxh;
3943
3944         handle = *ohp;
3945         if (ISP_CAP_2KLOGIN(isp)) {
3946                 minh = 0;
3947                 maxh = NPH_RESERVED - 1;
3948         } else {
3949                 minh = SNS_ID + 1;
3950                 maxh = NPH_MAX - 1;
3951         }
3952         wrap = 0;
3953
3954 next:
3955         if (handle == NIL_HANDLE) {
3956                 handle = minh;
3957         } else {
3958                 handle++;
3959                 if (handle > maxh) {
3960                         if (++wrap >= 2) {
3961                                 isp_prt(isp, ISP_LOGERR, "Out of port handles!");
3962                                 return (NIL_HANDLE);
3963                         }
3964                         handle = minh;
3965                 }
3966         }
3967         for (chan = 0; chan < isp->isp_nchan; chan++) {
3968                 fcp = FCPARAM(isp, chan);
3969                 if (fcp->role == ISP_ROLE_NONE)
3970                         continue;
3971                 for (i = 0; i < MAX_FC_TARG; i++) {
3972                         if (fcp->portdb[i].state != FC_PORTDB_STATE_NIL &&
3973                             fcp->portdb[i].handle == handle)
3974                                 goto next;
3975                 }
3976         }
3977         *ohp = handle;
3978         return (handle);
3979 }
3980
3981 /*
3982  * Start a command. Locking is assumed done in the caller.
3983  */
3984
3985 int
3986 isp_start(XS_T *xs)
3987 {
3988         ispsoftc_t *isp;
3989         uint32_t handle, cdblen;
3990         uint8_t local[QENTRY_LEN];
3991         ispreq_t *reqp;
3992         void *cdbp, *qep;
3993         uint16_t *tptr;
3994         fcportdb_t *lp;
3995         int target, dmaresult;
3996
3997         XS_INITERR(xs);
3998         isp = XS_ISP(xs);
3999
4000         /*
4001          * Check command CDB length, etc.. We really are limited to 16 bytes
4002          * for Fibre Channel, but can do up to 44 bytes in parallel SCSI,
4003          * but probably only if we're running fairly new firmware (we'll
4004          * let the old f/w choke on an extended command queue entry).
4005          */
4006
4007         if (XS_CDBLEN(xs) > (IS_FC(isp)? 16 : 44) || XS_CDBLEN(xs) == 0) {
4008                 isp_prt(isp, ISP_LOGERR, "unsupported cdb length (%d, CDB[0]=0x%x)", XS_CDBLEN(xs), XS_CDBP(xs)[0] & 0xff);
4009                 XS_SETERR(xs, HBA_BOTCH);
4010                 return (CMD_COMPLETE);
4011         }
4012
4013         /*
4014          * Translate the target to device handle as appropriate, checking
4015          * for correct device state as well.
4016          */
4017         target = XS_TGT(xs);
4018         if (IS_FC(isp)) {
4019                 fcparam *fcp = FCPARAM(isp, XS_CHANNEL(xs));
4020
4021                 if ((fcp->role & ISP_ROLE_INITIATOR) == 0) {
4022                         isp_prt(isp, ISP_LOG_WARN1,
4023                             "%d.%d.%jx I am not an initiator",
4024                             XS_CHANNEL(xs), target, (uintmax_t)XS_LUN(xs));
4025                         XS_SETERR(xs, HBA_SELTIMEOUT);
4026                         return (CMD_COMPLETE);
4027                 }
4028
4029                 if (isp->isp_state != ISP_RUNSTATE) {
4030                         isp_prt(isp, ISP_LOGERR, "Adapter not at RUNSTATE");
4031                         XS_SETERR(xs, HBA_BOTCH);
4032                         return (CMD_COMPLETE);
4033                 }
4034
4035                 /*
4036                  * Try again later.
4037                  */
4038                 if (fcp->isp_loopstate != LOOP_READY) {
4039                         return (CMD_RQLATER);
4040                 }
4041
4042                 isp_prt(isp, ISP_LOGDEBUG2, "XS_TGT(xs)=%d", target);
4043                 lp = &fcp->portdb[target];
4044                 if (target < 0 || target >= MAX_FC_TARG ||
4045                     lp->is_target == 0) {
4046                         XS_SETERR(xs, HBA_SELTIMEOUT);
4047                         return (CMD_COMPLETE);
4048                 }
4049                 if (lp->state == FC_PORTDB_STATE_ZOMBIE) {
4050                         isp_prt(isp, ISP_LOGDEBUG1,
4051                             "%d.%d.%jx target zombie",
4052                             XS_CHANNEL(xs), target, (uintmax_t)XS_LUN(xs));
4053                         return (CMD_RQLATER);
4054                 }
4055                 if (lp->state != FC_PORTDB_STATE_VALID) {
4056                         isp_prt(isp, ISP_LOGDEBUG1,
4057                             "%d.%d.%jx bad db port state 0x%x",
4058                             XS_CHANNEL(xs), target, (uintmax_t)XS_LUN(xs), lp->state);
4059                         XS_SETERR(xs, HBA_SELTIMEOUT);
4060                         return (CMD_COMPLETE);
4061                 }
4062         } else {
4063                 sdparam *sdp = SDPARAM(isp, XS_CHANNEL(xs));
4064                 if ((sdp->role & ISP_ROLE_INITIATOR) == 0) {
4065                         isp_prt(isp, ISP_LOGDEBUG1,
4066                             "%d.%d.%jx I am not an initiator",
4067                             XS_CHANNEL(xs), target, (uintmax_t)XS_LUN(xs));
4068                         XS_SETERR(xs, HBA_SELTIMEOUT);
4069                         return (CMD_COMPLETE);
4070                 }
4071
4072                 if (isp->isp_state != ISP_RUNSTATE) {
4073                         isp_prt(isp, ISP_LOGERR, "Adapter not at RUNSTATE");
4074                         XS_SETERR(xs, HBA_BOTCH);
4075                         return (CMD_COMPLETE);
4076                 }
4077
4078                 if (sdp->update) {
4079                         isp_spi_update(isp, XS_CHANNEL(xs));
4080                 }
4081                 lp = NULL;
4082         }
4083
4084  start_again:
4085
4086         qep = isp_getrqentry(isp);
4087         if (qep == NULL) {
4088                 isp_prt(isp, ISP_LOG_WARN1, "Request Queue Overflow");
4089                 XS_SETERR(xs, HBA_BOTCH);
4090                 return (CMD_EAGAIN);
4091         }
4092         XS_SETERR(xs, HBA_NOERROR);
4093
4094         /*
4095          * Now see if we need to synchronize the ISP with respect to anything.
4096          * We do dual duty here (cough) for synchronizing for busses other
4097          * than which we got here to send a command to.
4098          */
4099         reqp = (ispreq_t *) local;
4100         ISP_MEMZERO(local, QENTRY_LEN);
4101         if (ISP_TST_SENDMARKER(isp, XS_CHANNEL(xs))) {
4102                 if (IS_24XX(isp)) {
4103                         isp_marker_24xx_t *m = (isp_marker_24xx_t *) reqp;
4104                         m->mrk_header.rqs_entry_count = 1;
4105                         m->mrk_header.rqs_entry_type = RQSTYPE_MARKER;
4106                         m->mrk_modifier = SYNC_ALL;
4107                         m->mrk_vphdl = XS_CHANNEL(xs);
4108                         isp_put_marker_24xx(isp, m, qep);
4109                 } else {
4110                         isp_marker_t *m = (isp_marker_t *) reqp;
4111                         m->mrk_header.rqs_entry_count = 1;
4112                         m->mrk_header.rqs_entry_type = RQSTYPE_MARKER;
4113                         m->mrk_target = (XS_CHANNEL(xs) << 7);  /* bus # */
4114                         m->mrk_modifier = SYNC_ALL;
4115                         isp_put_marker(isp, m, qep);
4116                 }
4117                 ISP_SYNC_REQUEST(isp);
4118                 ISP_SET_SENDMARKER(isp, XS_CHANNEL(xs), 0);
4119                 goto start_again;
4120         }
4121
4122         reqp->req_header.rqs_entry_count = 1;
4123
4124         /*
4125          * Select and install Header Code.
4126          * Note that it might be overridden before going out
4127          * if we're on a 64 bit platform. The lower level
4128          * code (isp_send_cmd) will select the appropriate
4129          * 64 bit variant if it needs to.
4130          */
4131         if (IS_24XX(isp)) {
4132                 reqp->req_header.rqs_entry_type = RQSTYPE_T7RQS;
4133         } else if (IS_FC(isp)) {
4134                 reqp->req_header.rqs_entry_type = RQSTYPE_T2RQS;
4135         } else {
4136                 if (XS_CDBLEN(xs) > 12) {
4137                         reqp->req_header.rqs_entry_type = RQSTYPE_CMDONLY;
4138                 } else {
4139                         reqp->req_header.rqs_entry_type = RQSTYPE_REQUEST;
4140                 }
4141         }
4142
4143         /*
4144          * Set task attributes
4145          */
4146         if (IS_24XX(isp)) {
4147                 int ttype;
4148                 if (XS_TAG_P(xs)) {
4149                         ttype = XS_TAG_TYPE(xs);
4150                 } else {
4151                         if (XS_CDBP(xs)[0] == 0x3) {
4152                                 ttype = REQFLAG_HTAG;
4153                         } else {
4154                                 ttype = REQFLAG_STAG;
4155                         }
4156                 }
4157                 if (ttype == REQFLAG_OTAG) {
4158                         ttype = FCP_CMND_TASK_ATTR_ORDERED;
4159                 } else if (ttype == REQFLAG_HTAG) {
4160                         ttype = FCP_CMND_TASK_ATTR_HEAD;
4161                 } else {
4162                         ttype = FCP_CMND_TASK_ATTR_SIMPLE;
4163                 }
4164                 ((ispreqt7_t *)reqp)->req_task_attribute = ttype;
4165         } else if (IS_FC(isp)) {
4166                 /*
4167                  * See comment in isp_intr
4168                  */
4169                 /* XS_SET_RESID(xs, 0); */
4170
4171                 /*
4172                  * Fibre Channel always requires some kind of tag.
4173                  * The Qlogic drivers seem be happy not to use a tag,
4174                  * but this breaks for some devices (IBM drives).
4175                  */
4176                 if (XS_TAG_P(xs)) {
4177                         ((ispreqt2_t *)reqp)->req_flags = XS_TAG_TYPE(xs);
4178                 } else {
4179                         /*
4180                          * If we don't know what tag to use, use HEAD OF QUEUE
4181                          * for Request Sense or Simple.
4182                          */
4183                         if (XS_CDBP(xs)[0] == 0x3)      /* REQUEST SENSE */
4184                                 ((ispreqt2_t *)reqp)->req_flags = REQFLAG_HTAG;
4185                         else
4186                                 ((ispreqt2_t *)reqp)->req_flags = REQFLAG_STAG;
4187                 }
4188         } else {
4189                 sdparam *sdp = SDPARAM(isp, XS_CHANNEL(xs));
4190                 if ((sdp->isp_devparam[target].actv_flags & DPARM_TQING) && XS_TAG_P(xs)) {
4191                         reqp->req_flags = XS_TAG_TYPE(xs);
4192                 }
4193         }
4194
4195         tptr = &reqp->req_time;
4196
4197         /*
4198          * NB: we do not support long CDBs (yet)
4199          */
4200         cdblen = XS_CDBLEN(xs);
4201
4202         if (IS_SCSI(isp)) {
4203                 if (cdblen > sizeof (reqp->req_cdb)) {
4204                         isp_prt(isp, ISP_LOGERR, "Command Length %u too long for this chip", cdblen);
4205                         XS_SETERR(xs, HBA_BOTCH);
4206                         return (CMD_COMPLETE);
4207                 }
4208                 reqp->req_target = target | (XS_CHANNEL(xs) << 7);
4209                 reqp->req_lun_trn = XS_LUN(xs);
4210                 cdbp = reqp->req_cdb;
4211                 reqp->req_cdblen = cdblen;
4212         } else if (IS_24XX(isp)) {
4213                 ispreqt7_t *t7 = (ispreqt7_t *)local;
4214
4215                 if (cdblen > sizeof (t7->req_cdb)) {
4216                         isp_prt(isp, ISP_LOGERR, "Command Length %u too long for this chip", cdblen);
4217                         XS_SETERR(xs, HBA_BOTCH);
4218                         return (CMD_COMPLETE);
4219                 }
4220
4221                 t7->req_nphdl = lp->handle;
4222                 t7->req_tidlo = lp->portid;
4223                 t7->req_tidhi = lp->portid >> 16;
4224                 t7->req_vpidx = ISP_GET_VPIDX(isp, XS_CHANNEL(xs));
4225 #if __FreeBSD_version >= 1000700
4226                 be64enc(t7->req_lun, CAM_EXTLUN_BYTE_SWIZZLE(XS_LUN(xs)));
4227 #else
4228                 if (XS_LUN(xs) >= 256) {
4229                         t7->req_lun[0] = XS_LUN(xs) >> 8;
4230                         t7->req_lun[0] |= 0x40;
4231                 }
4232                 t7->req_lun[1] = XS_LUN(xs);
4233 #endif
4234                 if (FCPARAM(isp, XS_CHANNEL(xs))->fctape_enabled && (lp->prli_word3 & PRLI_WD3_RETRY)) {
4235                         if (FCP_NEXT_CRN(isp, &t7->req_crn, xs)) {
4236                                 isp_prt(isp, ISP_LOG_WARN1,
4237                                     "%d.%d.%jx cannot generate next CRN",
4238                                     XS_CHANNEL(xs), target, (uintmax_t)XS_LUN(xs));
4239                                 XS_SETERR(xs, HBA_BOTCH);
4240                                 return (CMD_EAGAIN);
4241                         }
4242                 }
4243                 tptr = &t7->req_time;
4244                 cdbp = t7->req_cdb;
4245         } else {
4246                 ispreqt2_t *t2 = (ispreqt2_t *)local;
4247
4248                 if (cdblen > sizeof t2->req_cdb) {
4249                         isp_prt(isp, ISP_LOGERR, "Command Length %u too long for this chip", cdblen);
4250                         XS_SETERR(xs, HBA_BOTCH);
4251                         return (CMD_COMPLETE);
4252                 }
4253                 if (FCPARAM(isp, XS_CHANNEL(xs))->fctape_enabled && (lp->prli_word3 & PRLI_WD3_RETRY)) {
4254                         if (FCP_NEXT_CRN(isp, &t2->req_crn, xs)) {
4255                                 isp_prt(isp, ISP_LOG_WARN1,
4256                                     "%d.%d.%jx cannot generate next CRN",
4257                                     XS_CHANNEL(xs), target, (uintmax_t)XS_LUN(xs));
4258                                 XS_SETERR(xs, HBA_BOTCH);
4259                                 return (CMD_EAGAIN);
4260                         }
4261                 }
4262                 if (ISP_CAP_2KLOGIN(isp)) {
4263                         ispreqt2e_t *t2e = (ispreqt2e_t *)local;
4264                         t2e->req_target = lp->handle;
4265                         t2e->req_scclun = XS_LUN(xs);
4266 #if __FreeBSD_version < 1000700
4267                         if (XS_LUN(xs) >= 256)
4268                                 t2e->req_scclun |= 0x4000;
4269 #endif
4270                         cdbp = t2e->req_cdb;
4271                 } else if (ISP_CAP_SCCFW(isp)) {
4272                         ispreqt2_t *t2 = (ispreqt2_t *)local;
4273                         t2->req_target = lp->handle;
4274                         t2->req_scclun = XS_LUN(xs);
4275 #if __FreeBSD_version < 1000700
4276                         if (XS_LUN(xs) >= 256)
4277                                 t2->req_scclun |= 0x4000;
4278 #endif
4279                         cdbp = t2->req_cdb;
4280                 } else {
4281                         t2->req_target = lp->handle;
4282                         t2->req_lun_trn = XS_LUN(xs);
4283                         cdbp = t2->req_cdb;
4284                 }
4285         }
4286         ISP_MEMCPY(cdbp, XS_CDBP(xs), cdblen);
4287
4288         *tptr = XS_TIME(xs) / 1000;
4289         if (*tptr == 0 && XS_TIME(xs)) {
4290                 *tptr = 1;
4291         }
4292         if (IS_24XX(isp) && *tptr > 0x1999) {
4293                 *tptr = 0x1999;
4294         }
4295
4296         if (isp_allocate_xs(isp, xs, &handle)) {
4297                 isp_prt(isp, ISP_LOG_WARN1, "out of xflist pointers");
4298                 XS_SETERR(xs, HBA_BOTCH);
4299                 return (CMD_EAGAIN);
4300         }
4301         /* Whew. Thankfully the same for type 7 requests */
4302         reqp->req_handle = handle;
4303
4304         /*
4305          * Set up DMA and/or do any platform dependent swizzling of the request entry
4306          * so that the Qlogic F/W understands what is being asked of it.
4307          *
4308          * The callee is responsible for adding all requests at this point.
4309          */
4310         dmaresult = ISP_DMASETUP(isp, xs, reqp);
4311         if (dmaresult != CMD_QUEUED) {
4312                 isp_destroy_handle(isp, handle);
4313                 /*
4314                  * dmasetup sets actual error in packet, and
4315                  * return what we were given to return.
4316                  */
4317                 return (dmaresult);
4318         }
4319         isp_xs_prt(isp, xs, ISP_LOGDEBUG0, "START cmd cdb[0]=0x%x datalen %ld", XS_CDBP(xs)[0], (long) XS_XFRLEN(xs));
4320         isp->isp_nactive++;
4321         return (CMD_QUEUED);
4322 }
4323
4324 /*
4325  * isp control
4326  * Locks (ints blocked) assumed held.
4327  */
4328
4329 int
4330 isp_control(ispsoftc_t *isp, ispctl_t ctl, ...)
4331 {
4332         XS_T *xs;
4333         mbreg_t *mbr, mbs;
4334         int chan, tgt;
4335         uint32_t handle;
4336         va_list ap;
4337
4338         switch (ctl) {
4339         case ISPCTL_RESET_BUS:
4340                 /*
4341                  * Issue a bus reset.
4342                  */
4343                 if (IS_24XX(isp)) {
4344                         isp_prt(isp, ISP_LOGERR, "BUS RESET NOT IMPLEMENTED");
4345                         break;
4346                 } else if (IS_FC(isp)) {
4347                         mbs.param[1] = 10;
4348                         chan = 0;
4349                 } else {
4350                         va_start(ap, ctl);
4351                         chan = va_arg(ap, int);
4352                         va_end(ap);
4353                         mbs.param[1] = SDPARAM(isp, chan)->isp_bus_reset_delay;
4354                         if (mbs.param[1] < 2) {
4355                                 mbs.param[1] = 2;
4356                         }
4357                         mbs.param[2] = chan;
4358                 }
4359                 MBSINIT(&mbs, MBOX_BUS_RESET, MBLOGALL, 0);
4360                 ISP_SET_SENDMARKER(isp, chan, 1);
4361                 isp_mboxcmd(isp, &mbs);
4362                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
4363                         break;
4364                 }
4365                 isp_prt(isp, ISP_LOGINFO, "driver initiated bus reset of bus %d", chan);
4366                 return (0);
4367
4368         case ISPCTL_RESET_DEV:
4369                 va_start(ap, ctl);
4370                 chan = va_arg(ap, int);
4371                 tgt = va_arg(ap, int);
4372                 va_end(ap);
4373                 if (IS_24XX(isp)) {
4374                         uint8_t local[QENTRY_LEN];
4375                         isp24xx_tmf_t *tmf;
4376                         isp24xx_statusreq_t *sp;
4377                         fcparam *fcp = FCPARAM(isp, chan);
4378                         fcportdb_t *lp;
4379
4380                         if (tgt < 0 || tgt >= MAX_FC_TARG) {
4381                                 isp_prt(isp, ISP_LOGWARN, "Chan %d trying to reset bad target %d", chan, tgt);
4382                                 break;
4383                         }
4384                         lp = &fcp->portdb[tgt];
4385                         if (lp->is_target == 0 ||
4386                             lp->state != FC_PORTDB_STATE_VALID) {
4387                                 isp_prt(isp, ISP_LOGWARN, "Chan %d abort of no longer valid target %d", chan, tgt);
4388                                 break;
4389                         }
4390
4391                         tmf = (isp24xx_tmf_t *) local;
4392                         ISP_MEMZERO(tmf, QENTRY_LEN);
4393                         tmf->tmf_header.rqs_entry_type = RQSTYPE_TSK_MGMT;
4394                         tmf->tmf_header.rqs_entry_count = 1;
4395                         tmf->tmf_nphdl = lp->handle;
4396                         tmf->tmf_delay = 2;
4397                         tmf->tmf_timeout = 2;
4398                         tmf->tmf_flags = ISP24XX_TMF_TARGET_RESET;
4399                         tmf->tmf_tidlo = lp->portid;
4400                         tmf->tmf_tidhi = lp->portid >> 16;
4401                         tmf->tmf_vpidx = ISP_GET_VPIDX(isp, chan);
4402                         isp_prt(isp, ISP_LOGALL, "Chan %d Reset N-Port Handle 0x%04x @ Port 0x%06x", chan, lp->handle, lp->portid);
4403                         MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 5000000);
4404                         mbs.param[1] = QENTRY_LEN;
4405                         mbs.param[2] = DMA_WD1(fcp->isp_scdma);
4406                         mbs.param[3] = DMA_WD0(fcp->isp_scdma);
4407                         mbs.param[6] = DMA_WD3(fcp->isp_scdma);
4408                         mbs.param[7] = DMA_WD2(fcp->isp_scdma);
4409
4410                         if (FC_SCRATCH_ACQUIRE(isp, chan)) {
4411                                 isp_prt(isp, ISP_LOGERR, sacq);
4412                                 break;
4413                         }
4414                         isp_put_24xx_tmf(isp, tmf, fcp->isp_scratch);
4415                         MEMORYBARRIER(isp, SYNC_SFORDEV, 0, QENTRY_LEN, chan);
4416                         fcp->sendmarker = 1;
4417                         isp_mboxcmd(isp, &mbs);
4418                         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
4419                                 FC_SCRATCH_RELEASE(isp, chan);
4420                                 break;
4421                         }
4422                         MEMORYBARRIER(isp, SYNC_SFORCPU, QENTRY_LEN, QENTRY_LEN, chan);
4423                         sp = (isp24xx_statusreq_t *) local;
4424                         isp_get_24xx_response(isp, &((isp24xx_statusreq_t *)fcp->isp_scratch)[1], sp);
4425                         FC_SCRATCH_RELEASE(isp, chan);
4426                         if (sp->req_completion_status == 0) {
4427                                 return (0);
4428                         }
4429                         isp_prt(isp, ISP_LOGWARN, "Chan %d reset of target %d returned 0x%x", chan, tgt, sp->req_completion_status);
4430                         break;
4431                 } else if (IS_FC(isp)) {
4432                         if (ISP_CAP_2KLOGIN(isp)) {
4433                                 mbs.param[1] = tgt;
4434                                 mbs.ibits = (1 << 10);
4435                         } else {
4436                                 mbs.param[1] = (tgt << 8);
4437                         }
4438                 } else {
4439                         mbs.param[1] = (chan << 15) | (tgt << 8);
4440                 }
4441                 MBSINIT(&mbs, MBOX_ABORT_TARGET, MBLOGALL, 0);
4442                 mbs.param[2] = 3;       /* 'delay', in seconds */
4443                 isp_mboxcmd(isp, &mbs);
4444                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
4445                         break;
4446                 }
4447                 isp_prt(isp, ISP_LOGINFO, "Target %d on Bus %d Reset Succeeded", tgt, chan);
4448                 ISP_SET_SENDMARKER(isp, chan, 1);
4449                 return (0);
4450
4451         case ISPCTL_ABORT_CMD:
4452                 va_start(ap, ctl);
4453                 xs = va_arg(ap, XS_T *);
4454                 va_end(ap);
4455
4456                 tgt = XS_TGT(xs);
4457                 chan = XS_CHANNEL(xs);
4458
4459                 handle = isp_find_handle(isp, xs);
4460                 if (handle == 0) {
4461                         isp_prt(isp, ISP_LOGWARN, "cannot find handle for command to abort");
4462                         break;
4463                 }
4464                 if (IS_24XX(isp)) {
4465                         isp24xx_abrt_t local, *ab = &local, *ab2;
4466                         fcparam *fcp;
4467                         fcportdb_t *lp;
4468
4469                         fcp = FCPARAM(isp, chan);
4470                         if (tgt < 0 || tgt >= MAX_FC_TARG) {
4471                                 isp_prt(isp, ISP_LOGWARN, "Chan %d trying to abort bad target %d", chan, tgt);
4472                                 break;
4473                         }
4474                         lp = &fcp->portdb[tgt];
4475                         if (lp->is_target == 0 ||
4476                             lp->state != FC_PORTDB_STATE_VALID) {
4477                                 isp_prt(isp, ISP_LOGWARN, "Chan %d abort of no longer valid target %d", chan, tgt);
4478                                 break;
4479                         }
4480                         isp_prt(isp, ISP_LOGALL, "Chan %d Abort Cmd for N-Port 0x%04x @ Port 0x%06x", chan, lp->handle, lp->portid);
4481                         ISP_MEMZERO(ab, QENTRY_LEN);
4482                         ab->abrt_header.rqs_entry_type = RQSTYPE_ABORT_IO;
4483                         ab->abrt_header.rqs_entry_count = 1;
4484                         ab->abrt_handle = lp->handle;
4485                         ab->abrt_cmd_handle = handle;
4486                         ab->abrt_tidlo = lp->portid;
4487                         ab->abrt_tidhi = lp->portid >> 16;
4488                         ab->abrt_vpidx = ISP_GET_VPIDX(isp, chan);
4489
4490                         ISP_MEMZERO(&mbs, sizeof (mbs));
4491                         MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 5000000);
4492                         mbs.param[1] = QENTRY_LEN;
4493                         mbs.param[2] = DMA_WD1(fcp->isp_scdma);
4494                         mbs.param[3] = DMA_WD0(fcp->isp_scdma);
4495                         mbs.param[6] = DMA_WD3(fcp->isp_scdma);
4496                         mbs.param[7] = DMA_WD2(fcp->isp_scdma);
4497
4498                         if (FC_SCRATCH_ACQUIRE(isp, chan)) {
4499                                 isp_prt(isp, ISP_LOGERR, sacq);
4500                                 break;
4501                         }
4502                         isp_put_24xx_abrt(isp, ab, fcp->isp_scratch);
4503                         ab2 = (isp24xx_abrt_t *) &((uint8_t *)fcp->isp_scratch)[QENTRY_LEN];
4504                         ab2->abrt_nphdl = 0xdeaf;
4505                         MEMORYBARRIER(isp, SYNC_SFORDEV, 0, 2 * QENTRY_LEN, chan);
4506                         isp_mboxcmd(isp, &mbs);
4507                         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
4508                                 FC_SCRATCH_RELEASE(isp, chan);
4509                                 break;
4510                         }
4511                         MEMORYBARRIER(isp, SYNC_SFORCPU, QENTRY_LEN, QENTRY_LEN, chan);
4512                         isp_get_24xx_abrt(isp, ab2, ab);
4513                         FC_SCRATCH_RELEASE(isp, chan);
4514                         if (ab->abrt_nphdl == ISP24XX_ABRT_OKAY) {
4515                                 return (0);
4516                         }
4517                         isp_prt(isp, ISP_LOGWARN, "Chan %d handle %d abort returned 0x%x", chan, tgt, ab->abrt_nphdl);
4518                         break;
4519                 } else if (IS_FC(isp)) {
4520                         if (ISP_CAP_SCCFW(isp)) {
4521                                 if (ISP_CAP_2KLOGIN(isp)) {
4522                                         mbs.param[1] = tgt;
4523                                 } else {
4524                                         mbs.param[1] = tgt << 8;
4525                                 }
4526                                 mbs.param[6] = XS_LUN(xs);
4527                         } else {
4528                                 mbs.param[1] = tgt << 8 | XS_LUN(xs);
4529                         }
4530                 } else {
4531                         mbs.param[1] = (chan << 15) | (tgt << 8) | XS_LUN(xs);
4532                 }
4533                 MBSINIT(&mbs, MBOX_ABORT,
4534                     MBLOGALL & ~MBLOGMASK(MBOX_COMMAND_ERROR), 0);
4535                 mbs.param[2] = handle;
4536                 isp_mboxcmd(isp, &mbs);
4537                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
4538                         break;
4539                 }
4540                 return (0);
4541
4542         case ISPCTL_UPDATE_PARAMS:
4543
4544                 va_start(ap, ctl);
4545                 chan = va_arg(ap, int);
4546                 va_end(ap);
4547                 isp_spi_update(isp, chan);
4548                 return (0);
4549
4550         case ISPCTL_FCLINK_TEST:
4551
4552                 if (IS_FC(isp)) {
4553                         int usdelay;
4554                         va_start(ap, ctl);
4555                         chan = va_arg(ap, int);
4556                         usdelay = va_arg(ap, int);
4557                         va_end(ap);
4558                         if (usdelay == 0) {
4559                                 usdelay =  250000;
4560                         }
4561                         return (isp_fclink_test(isp, chan, usdelay));
4562                 }
4563                 break;
4564
4565         case ISPCTL_SCAN_FABRIC:
4566
4567                 if (IS_FC(isp)) {
4568                         va_start(ap, ctl);
4569                         chan = va_arg(ap, int);
4570                         va_end(ap);
4571                         return (isp_scan_fabric(isp, chan));
4572                 }
4573                 break;
4574
4575         case ISPCTL_SCAN_LOOP:
4576
4577                 if (IS_FC(isp)) {
4578                         va_start(ap, ctl);
4579                         chan = va_arg(ap, int);
4580                         va_end(ap);
4581                         return (isp_scan_loop(isp, chan));
4582                 }
4583                 break;
4584
4585         case ISPCTL_PDB_SYNC:
4586
4587                 if (IS_FC(isp)) {
4588                         va_start(ap, ctl);
4589                         chan = va_arg(ap, int);
4590                         va_end(ap);
4591                         return (isp_pdb_sync(isp, chan));
4592                 }
4593                 break;
4594
4595         case ISPCTL_SEND_LIP:
4596
4597                 if (IS_FC(isp) && !IS_24XX(isp)) {
4598                         MBSINIT(&mbs, MBOX_INIT_LIP, MBLOGALL, 0);
4599                         if (ISP_CAP_2KLOGIN(isp)) {
4600                                 mbs.ibits = (1 << 10);
4601                         }
4602                         isp_mboxcmd(isp, &mbs);
4603                         if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
4604                                 return (0);
4605                         }
4606                 }
4607                 break;
4608
4609         case ISPCTL_GET_PDB:
4610                 if (IS_FC(isp)) {
4611                         isp_pdb_t *pdb;
4612                         va_start(ap, ctl);
4613                         chan = va_arg(ap, int);
4614                         tgt = va_arg(ap, int);
4615                         pdb = va_arg(ap, isp_pdb_t *);
4616                         va_end(ap);
4617                         return (isp_getpdb(isp, chan, tgt, pdb, 1));
4618                 }
4619                 break;
4620
4621         case ISPCTL_GET_NAMES:
4622         {
4623                 uint64_t *wwnn, *wwnp;
4624                 va_start(ap, ctl);
4625                 chan = va_arg(ap, int);
4626                 tgt = va_arg(ap, int);
4627                 wwnn = va_arg(ap, uint64_t *);
4628                 wwnp = va_arg(ap, uint64_t *);
4629                 va_end(ap);
4630                 if (wwnn == NULL && wwnp == NULL) {
4631                         break;
4632                 }
4633                 if (wwnn) {
4634                         *wwnn = isp_get_wwn(isp, chan, tgt, 1);
4635                         if (*wwnn == INI_NONE) {
4636                                 break;
4637                         }
4638                 }
4639                 if (wwnp) {
4640                         *wwnp = isp_get_wwn(isp, chan, tgt, 0);
4641                         if (*wwnp == INI_NONE) {
4642                                 break;
4643                         }
4644                 }
4645                 return (0);
4646         }
4647         case ISPCTL_RUN_MBOXCMD:
4648         {
4649                 va_start(ap, ctl);
4650                 mbr = va_arg(ap, mbreg_t *);
4651                 va_end(ap);
4652                 isp_mboxcmd(isp, mbr);
4653                 return (0);
4654         }
4655         case ISPCTL_PLOGX:
4656         {
4657                 isp_plcmd_t *p;
4658                 int r;
4659
4660                 va_start(ap, ctl);
4661                 p = va_arg(ap, isp_plcmd_t *);
4662                 va_end(ap);
4663
4664                 if ((p->flags & PLOGX_FLG_CMD_MASK) != PLOGX_FLG_CMD_PLOGI || (p->handle != NIL_HANDLE)) {
4665                         return (isp_plogx(isp, p->channel, p->handle, p->portid, p->flags, 0));
4666                 }
4667                 do {
4668                         isp_next_handle(isp, &p->handle);
4669                         r = isp_plogx(isp, p->channel, p->handle, p->portid, p->flags, 0);
4670                         if ((r & 0xffff) == MBOX_PORT_ID_USED) {
4671                                 p->handle = r >> 16;
4672                                 r = 0;
4673                                 break;
4674                         }
4675                 } while ((r & 0xffff) == MBOX_LOOP_ID_USED);
4676                 return (r);
4677         }
4678         case ISPCTL_CHANGE_ROLE:
4679         {
4680                 int role, r;
4681
4682                 va_start(ap, ctl);
4683                 chan = va_arg(ap, int);
4684                 role = va_arg(ap, int);
4685                 va_end(ap);
4686                 if (IS_FC(isp)) {
4687                         r = isp_fc_change_role(isp, chan, role);
4688                 } else {
4689                         SDPARAM(isp, chan)->role = role;
4690                         r = 0;
4691                 }
4692                 return (r);
4693         }
4694         default:
4695                 isp_prt(isp, ISP_LOGERR, "Unknown Control Opcode 0x%x", ctl);
4696                 break;
4697
4698         }
4699         return (-1);
4700 }
4701
4702 /*
4703  * Interrupt Service Routine(s).
4704  *
4705  * External (OS) framework has done the appropriate locking,
4706  * and the locking will be held throughout this function.
4707  */
4708
4709 /*
4710  * Limit our stack depth by sticking with the max likely number
4711  * of completions on a request queue at any one time.
4712  */
4713 #ifndef MAX_REQUESTQ_COMPLETIONS
4714 #define MAX_REQUESTQ_COMPLETIONS        32
4715 #endif
4716
4717 void
4718 isp_intr(ispsoftc_t *isp, uint16_t isr, uint16_t sema, uint16_t info)
4719 {
4720         XS_T *complist[MAX_REQUESTQ_COMPLETIONS], *xs;
4721         uint32_t iptr, optr, junk;
4722         int i, nlooked = 0, ndone = 0, continuations_expected = 0;
4723         int etype, last_etype = 0;
4724
4725 again:
4726         /*
4727          * Is this a mailbox related interrupt?
4728          * The mailbox semaphore will be nonzero if so.
4729          */
4730         if (sema) {
4731  fmbox:
4732                 if (info & MBOX_COMMAND_COMPLETE) {
4733                         isp->isp_intmboxc++;
4734                         if (isp->isp_mboxbsy) {
4735                                 int obits = isp->isp_obits;
4736                                 isp->isp_mboxtmp[0] = info;
4737                                 for (i = 1; i < ISP_NMBOX(isp); i++) {
4738                                         if ((obits & (1 << i)) == 0) {
4739                                                 continue;
4740                                         }
4741                                         isp->isp_mboxtmp[i] = ISP_READ(isp, MBOX_OFF(i));
4742                                 }
4743                                 if (isp->isp_mbxwrk0) {
4744                                         if (isp_mbox_continue(isp) == 0) {
4745                                                 return;
4746                                         }
4747                                 }
4748                                 MBOX_NOTIFY_COMPLETE(isp);
4749                         } else {
4750                                 isp_prt(isp, ISP_LOGWARN, "mailbox cmd (0x%x) with no waiters", info);
4751                         }
4752                 } else {
4753                         i = IS_FC(isp)? isp_parse_async_fc(isp, info) : isp_parse_async(isp, info);
4754                         if (i < 0) {
4755                                 return;
4756                         }
4757                 }
4758                 if ((IS_FC(isp) && info != ASYNC_RIOZIO_STALL) || isp->isp_state != ISP_RUNSTATE) {
4759                         goto out;
4760                 }
4761         }
4762
4763         /*
4764          * We can't be getting this now.
4765          */
4766         if (isp->isp_state != ISP_RUNSTATE) {
4767                 /*
4768                  * This seems to happen to 23XX and 24XX cards- don't know why.
4769                  */
4770                  if (isp->isp_mboxbsy && isp->isp_lastmbxcmd == MBOX_ABOUT_FIRMWARE) {
4771                         goto fmbox;
4772                 }
4773                 isp_prt(isp, ISP_LOGINFO, "interrupt (ISR=%x SEMA=%x INFO=%x) "
4774                     "when not ready", isr, sema, info);
4775                 /*
4776                  * Thank you very much!  *Burrrp*!
4777                  */
4778                 isp->isp_residx = ISP_READ(isp, isp->isp_respinrp);
4779                 isp->isp_resodx = isp->isp_residx;
4780                 ISP_WRITE(isp, isp->isp_respoutrp, isp->isp_resodx);
4781                 if (IS_24XX(isp)) {
4782                         ISP_DISABLE_INTS(isp);
4783                 }
4784                 goto out;
4785         }
4786
4787 #ifdef  ISP_TARGET_MODE
4788         /*
4789          * Check for ATIO Queue entries.
4790          */
4791         if (IS_24XX(isp) &&
4792             (isr == ISPR2HST_ATIO_UPDATE || isr == ISPR2HST_ATIO_RSPQ_UPDATE ||
4793              isr == ISPR2HST_ATIO_UPDATE2)) {
4794                 iptr = ISP_READ(isp, BIU2400_ATIO_RSPINP);
4795                 optr = isp->isp_atioodx;
4796
4797                 while (optr != iptr) {
4798                         uint8_t qe[QENTRY_LEN];
4799                         isphdr_t *hp;
4800                         uint32_t oop;
4801                         void *addr;
4802
4803                         oop = optr;
4804                         MEMORYBARRIER(isp, SYNC_ATIOQ, oop, QENTRY_LEN, -1);
4805                         addr = ISP_QUEUE_ENTRY(isp->isp_atioq, oop);
4806                         isp_get_hdr(isp, addr, (isphdr_t *)qe);
4807                         hp = (isphdr_t *)qe;
4808                         switch (hp->rqs_entry_type) {
4809                         case RQSTYPE_NOTIFY:
4810                         case RQSTYPE_ATIO:
4811                                 (void) isp_target_notify(isp, addr, &oop);
4812                                 break;
4813                         default:
4814                                 isp_print_qentry(isp, "?ATIOQ entry?", oop, addr);
4815                                 break;
4816                         }
4817                         optr = ISP_NXT_QENTRY(oop, RESULT_QUEUE_LEN(isp));
4818                 }
4819                 if (isp->isp_atioodx != optr) {
4820                         ISP_WRITE(isp, BIU2400_ATIO_RSPOUTP, optr);
4821                         isp->isp_atioodx = optr;
4822                 }
4823         }
4824 #endif
4825
4826         /*
4827          * You *must* read the Response Queue In Pointer
4828          * prior to clearing the RISC interrupt.
4829          *
4830          * Debounce the 2300 if revision less than 2.
4831          */
4832         if (IS_2100(isp) || (IS_2300(isp) && isp->isp_revision < 2)) {
4833                 i = 0;
4834                 do {
4835                         iptr = ISP_READ(isp, isp->isp_respinrp);
4836                         junk = ISP_READ(isp, isp->isp_respinrp);
4837                 } while (junk != iptr && ++i < 1000);
4838
4839                 if (iptr != junk) {
4840                         isp_prt(isp, ISP_LOGWARN, "Response Queue Out Pointer Unstable (%x, %x)", iptr, junk);
4841                         goto out;
4842                 }
4843         } else {
4844                 iptr = ISP_READ(isp, isp->isp_respinrp);
4845         }
4846
4847         optr = isp->isp_resodx;
4848         if (optr == iptr && sema == 0) {
4849                 /*
4850                  * There are a lot of these- reasons unknown- mostly on
4851                  * faster Alpha machines.
4852                  *
4853                  * I tried delaying after writing HCCR_CMD_CLEAR_RISC_INT to
4854                  * make sure the old interrupt went away (to avoid 'ringing'
4855                  * effects), but that didn't stop this from occurring.
4856                  */
4857                 if (IS_24XX(isp)) {
4858                         junk = 0;
4859                 } else if (IS_23XX(isp)) {
4860                         ISP_DELAY(100);
4861                         iptr = ISP_READ(isp, isp->isp_respinrp);
4862                         junk = ISP_READ(isp, BIU_R2HSTSLO);
4863                 } else {
4864                         junk = ISP_READ(isp, BIU_ISR);
4865                 }
4866                 if (optr == iptr) {
4867                         if (IS_23XX(isp) || IS_24XX(isp)) {
4868                                 ;
4869                         } else {
4870                                 sema = ISP_READ(isp, BIU_SEMA);
4871                                 info = ISP_READ(isp, OUTMAILBOX0);
4872                                 if ((sema & 0x3) && (info & 0x8000)) {
4873                                         goto again;
4874                                 }
4875                         }
4876                         isp->isp_intbogus++;
4877                         isp_prt(isp, ISP_LOGDEBUG1, "bogus intr- isr %x (%x) iptr %x optr %x", isr, junk, iptr, optr);
4878                 }
4879         }
4880         isp->isp_residx = iptr;
4881
4882         while (optr != iptr) {
4883                 uint8_t qe[QENTRY_LEN];
4884                 ispstatusreq_t *sp = (ispstatusreq_t *) qe;
4885                 isphdr_t *hp;
4886                 int buddaboom, scsi_status, completion_status;
4887                 int req_status_flags, req_state_flags;
4888                 uint8_t *snsp, *resp;
4889                 uint32_t rlen, slen, totslen;
4890                 long resid;
4891                 uint16_t oop;
4892
4893                 hp = (isphdr_t *) ISP_QUEUE_ENTRY(isp->isp_result, optr);
4894                 oop = optr;
4895                 optr = ISP_NXT_QENTRY(optr, RESULT_QUEUE_LEN(isp));
4896                 nlooked++;
4897  read_again:
4898                 buddaboom = req_status_flags = req_state_flags = 0;
4899                 resid = 0L;
4900
4901                 /*
4902                  * Synchronize our view of this response queue entry.
4903                  */
4904                 MEMORYBARRIER(isp, SYNC_RESULT, oop, QENTRY_LEN, -1);
4905                 isp_get_hdr(isp, hp, &sp->req_header);
4906                 etype = sp->req_header.rqs_entry_type;
4907
4908                 if (IS_24XX(isp) && etype == RQSTYPE_RESPONSE) {
4909                         isp24xx_statusreq_t *sp2 = (isp24xx_statusreq_t *)qe;
4910                         isp_get_24xx_response(isp, (isp24xx_statusreq_t *)hp, sp2);
4911                         if (isp->isp_dblev & ISP_LOGDEBUG1) {
4912                                 isp_print_bytes(isp, "Response Queue Entry", QENTRY_LEN, sp2);
4913                         }
4914                         scsi_status = sp2->req_scsi_status;
4915                         completion_status = sp2->req_completion_status;
4916                         if ((scsi_status & 0xff) != 0)
4917                                 req_state_flags = RQSF_GOT_STATUS;
4918                         else
4919                                 req_state_flags = 0;
4920                         resid = sp2->req_resid;
4921                 } else if (etype == RQSTYPE_RESPONSE) {
4922                         isp_get_response(isp, (ispstatusreq_t *) hp, sp);
4923                         if (isp->isp_dblev & ISP_LOGDEBUG1) {
4924                                 isp_print_bytes(isp, "Response Queue Entry", QENTRY_LEN, sp);
4925                         }
4926                         scsi_status = sp->req_scsi_status;
4927                         completion_status = sp->req_completion_status;
4928                         req_status_flags = sp->req_status_flags;
4929                         req_state_flags = sp->req_state_flags;
4930                         resid = sp->req_resid;
4931                 } else if (etype == RQSTYPE_RIO1) {
4932                         isp_rio1_t *rio = (isp_rio1_t *) qe;
4933                         isp_get_rio1(isp, (isp_rio1_t *) hp, rio);
4934                         if (isp->isp_dblev & ISP_LOGDEBUG1) {
4935                                 isp_print_bytes(isp, "Response Queue Entry", QENTRY_LEN, rio);
4936                         }
4937                         for (i = 0; i < rio->req_header.rqs_seqno; i++) {
4938                                 isp_fastpost_complete(isp, rio->req_handles[i]);
4939                         }
4940                         if (isp->isp_fpcchiwater < rio->req_header.rqs_seqno) {
4941                                 isp->isp_fpcchiwater = rio->req_header.rqs_seqno;
4942                         }
4943                         ISP_MEMZERO(hp, QENTRY_LEN);    /* PERF */
4944                         last_etype = etype;
4945                         continue;
4946                 } else if (etype == RQSTYPE_RIO2) {
4947                         isp_prt(isp, ISP_LOGERR, "dropping RIO2 response");
4948                         ISP_MEMZERO(hp, QENTRY_LEN);    /* PERF */
4949                         last_etype = etype;
4950                         continue;
4951                 } else if (etype == RQSTYPE_STATUS_CONT) {
4952                         isp_get_cont_response(isp, (ispstatus_cont_t *) hp, (ispstatus_cont_t *) sp);
4953                         if (last_etype == RQSTYPE_RESPONSE && continuations_expected && ndone > 0 && (xs = complist[ndone-1]) != NULL) {
4954                                 ispstatus_cont_t *scp = (ispstatus_cont_t *) sp;
4955                                 XS_SENSE_APPEND(xs, scp->req_sense_data, sizeof (scp->req_sense_data));
4956                                 isp_prt(isp, ISP_LOGDEBUG0|ISP_LOG_CWARN, "%d more Status Continuations expected", --continuations_expected);
4957                         } else {
4958                                 isp_prt(isp, ISP_LOG_WARN1, "Ignored Continuation Response");
4959                         }
4960                         ISP_MEMZERO(hp, QENTRY_LEN);    /* PERF */
4961                         continue;
4962                 } else {
4963                         /*
4964                          * Somebody reachable via isp_handle_other_response
4965                          * may have updated the response queue pointers for
4966                          * us, so we reload our goal index.
4967                          */
4968                         int r;
4969                         uint32_t tsto = oop;
4970                         r = isp_handle_other_response(isp, etype, hp, &tsto);
4971                         if (r < 0) {
4972                                 goto read_again;
4973                         }
4974                         /*
4975                          * If somebody updated the output pointer, then reset
4976                          * optr to be one more than the updated amount.
4977                          */
4978                         while (tsto != oop) {
4979                                 optr = ISP_NXT_QENTRY(tsto, RESULT_QUEUE_LEN(isp));
4980                         }
4981                         if (r > 0) {
4982                                 ISP_MEMZERO(hp, QENTRY_LEN);    /* PERF */
4983                                 last_etype = etype;
4984                                 continue;
4985                         }
4986
4987                         /*
4988                          * After this point, we'll just look at the header as
4989                          * we don't know how to deal with the rest of the
4990                          * response.
4991                          */
4992
4993                         /*
4994                          * It really has to be a bounced request just copied
4995                          * from the request queue to the response queue. If
4996                          * not, something bad has happened.
4997                          */
4998                         if (etype != RQSTYPE_REQUEST) {
4999                                 isp_prt(isp, ISP_LOGERR, notresp, etype, oop, optr, nlooked);
5000                                 isp_print_bytes(isp, "Request Queue Entry", QENTRY_LEN, sp);
5001                                 ISP_MEMZERO(hp, QENTRY_LEN);    /* PERF */
5002                                 last_etype = etype;
5003                                 continue;
5004                         }
5005                         buddaboom = 1;
5006                         scsi_status = sp->req_scsi_status;
5007                         completion_status = sp->req_completion_status;
5008                         req_status_flags = sp->req_status_flags;
5009                         req_state_flags = sp->req_state_flags;
5010                         resid = sp->req_resid;
5011                 }
5012
5013                 if (sp->req_header.rqs_flags & RQSFLAG_MASK) {
5014                         if (sp->req_header.rqs_flags & RQSFLAG_CONTINUATION) {
5015                                 isp_print_bytes(isp, "unexpected continuation segment", QENTRY_LEN, sp);
5016                                 last_etype = etype;
5017                                 continue;
5018                         }
5019                         if (sp->req_header.rqs_flags & RQSFLAG_FULL) {
5020                                 isp_prt(isp, ISP_LOG_WARN1, "internal queues full");
5021                                 /*
5022                                  * We'll synthesize a QUEUE FULL message below.
5023                                  */
5024                         }
5025                         if (sp->req_header.rqs_flags & RQSFLAG_BADHEADER) {
5026                                 isp_print_bytes(isp, "bad header flag", QENTRY_LEN, sp);
5027                                 buddaboom++;
5028                         }
5029                         if (sp->req_header.rqs_flags & RQSFLAG_BADPACKET) {
5030                                 isp_print_bytes(isp, "bad request packet", QENTRY_LEN, sp);
5031                                 buddaboom++;
5032                         }
5033                         if (sp->req_header.rqs_flags & RQSFLAG_BADCOUNT) {
5034                                 isp_print_bytes(isp, "invalid entry count", QENTRY_LEN, sp);
5035                                 buddaboom++;
5036                         }
5037                         if (sp->req_header.rqs_flags & RQSFLAG_BADORDER) {
5038                                 isp_print_bytes(isp, "invalid IOCB ordering", QENTRY_LEN, sp);
5039                                 last_etype = etype;
5040                                 continue;
5041                         }
5042                 }
5043
5044                 if (!ISP_VALID_HANDLE(isp, sp->req_handle)) {
5045                         isp_prt(isp, ISP_LOGERR, "bad request handle 0x%x (iocb type 0x%x)", sp->req_handle, etype);
5046                         ISP_MEMZERO(hp, QENTRY_LEN);    /* PERF */
5047                         last_etype = etype;
5048                         continue;
5049                 }
5050                 xs = isp_find_xs(isp, sp->req_handle);
5051                 if (xs == NULL) {
5052                         uint8_t ts = completion_status & 0xff;
5053                         /*
5054                          * Only whine if this isn't the expected fallout of
5055                          * aborting the command or resetting the target.
5056                          */
5057                         if (etype != RQSTYPE_RESPONSE) {
5058                                 isp_prt(isp, ISP_LOGERR, "cannot find handle 0x%x (type 0x%x)", sp->req_handle, etype);
5059                         } else if (ts != RQCS_ABORTED && ts != RQCS_RESET_OCCURRED) {
5060                                 isp_prt(isp, ISP_LOGERR, "cannot find handle 0x%x (status 0x%x)", sp->req_handle, ts);
5061                         }
5062                         ISP_MEMZERO(hp, QENTRY_LEN);    /* PERF */
5063                         last_etype = etype;
5064                         continue;
5065                 }
5066                 if (req_status_flags & RQSTF_BUS_RESET) {
5067                         isp_prt(isp, ISP_LOG_WARN1, "%d.%d.%jx bus was reset",
5068                             XS_CHANNEL(xs), XS_TGT(xs), (uintmax_t)XS_LUN(xs));
5069                         XS_SETERR(xs, HBA_BUSRESET);
5070                         ISP_SET_SENDMARKER(isp, XS_CHANNEL(xs), 1);
5071                 }
5072                 if (buddaboom) {
5073                         isp_prt(isp, ISP_LOG_WARN1, "%d.%d.%jx buddaboom",
5074                             XS_CHANNEL(xs), XS_TGT(xs), (uintmax_t)XS_LUN(xs));
5075                         XS_SETERR(xs, HBA_BOTCH);
5076                 }
5077
5078                 resp = NULL;
5079                 rlen = 0;
5080                 snsp = NULL;
5081                 totslen = slen = 0;
5082                 if (IS_24XX(isp) && (scsi_status & (RQCS_RV|RQCS_SV)) != 0) {
5083                         resp = ((isp24xx_statusreq_t *)sp)->req_rsp_sense;
5084                         rlen = ((isp24xx_statusreq_t *)sp)->req_response_len;
5085                 } else if (IS_FC(isp) && (scsi_status & RQCS_RV) != 0) {
5086                         resp = sp->req_response;
5087                         rlen = sp->req_response_len;
5088                 }
5089                 if (IS_FC(isp) && (scsi_status & RQCS_SV) != 0) {
5090                         /*
5091                          * Fibre Channel F/W doesn't say we got status
5092                          * if there's Sense Data instead. I guess they
5093                          * think it goes w/o saying.
5094                          */
5095                         req_state_flags |= RQSF_GOT_STATUS|RQSF_GOT_SENSE;
5096                         if (IS_24XX(isp)) {
5097                                 snsp = ((isp24xx_statusreq_t *)sp)->req_rsp_sense;
5098                                 snsp += rlen;
5099                                 totslen = ((isp24xx_statusreq_t *)sp)->req_sense_len;
5100                                 slen = (sizeof (((isp24xx_statusreq_t *)sp)->req_rsp_sense)) - rlen;
5101                                 if (totslen < slen)
5102                                         slen = totslen; 
5103                         } else {
5104                                 snsp = sp->req_sense_data;
5105                                 totslen = sp->req_sense_len;
5106                                 slen = sizeof (sp->req_sense_data);
5107                                 if (totslen < slen)
5108                                         slen = totslen;
5109                         }
5110                 } else if (IS_SCSI(isp) && (req_state_flags & RQSF_GOT_SENSE)) {
5111                         snsp = sp->req_sense_data;
5112                         totslen = sp->req_sense_len;
5113                         slen = sizeof (sp->req_sense_data);
5114                         if (totslen < slen)
5115                                 slen = totslen;
5116                 }
5117                 if (req_state_flags & RQSF_GOT_STATUS) {
5118                         *XS_STSP(xs) = scsi_status & 0xff;
5119                 }
5120
5121                 switch (etype) {
5122                 case RQSTYPE_RESPONSE:
5123                         if (resp && rlen >= 4 && resp[FCP_RSPNS_CODE_OFFSET] != 0) {
5124                                 const char *ptr;
5125                                 char lb[64];
5126                                 const char *rnames[10] = {
5127                                     "Task Management function complete",
5128                                     "FCP_DATA length different than FCP_BURST_LEN",
5129                                     "FCP_CMND fields invalid",
5130                                     "FCP_DATA parameter mismatch with FCP_DATA_RO",
5131                                     "Task Management function rejected",
5132                                     "Task Management function failed",
5133                                     NULL,
5134                                     NULL,
5135                                     "Task Management function succeeded",
5136                                     "Task Management function incorrect logical unit number",
5137                                 };
5138                                 uint8_t code = resp[FCP_RSPNS_CODE_OFFSET];
5139                                 if (code >= 10 || rnames[code] == NULL) {
5140                                         ISP_SNPRINTF(lb, sizeof(lb),
5141                                             "Unknown FCP Response Code 0x%x",
5142                                             code);
5143                                         ptr = lb;
5144                                 } else {
5145                                         ptr = rnames[code];
5146                                 }
5147                                 isp_xs_prt(isp, xs, ISP_LOGWARN,
5148                                     "FCP RESPONSE, LENGTH %u: %s CDB0=0x%02x",
5149                                     rlen, ptr, XS_CDBP(xs)[0] & 0xff);
5150                                 if (code != 0 && code != 8)
5151                                         XS_SETERR(xs, HBA_BOTCH);
5152                         }
5153                         if (IS_24XX(isp)) {
5154                                 isp_parse_status_24xx(isp, (isp24xx_statusreq_t *)sp, xs, &resid);
5155                         } else {
5156                                 isp_parse_status(isp, (void *)sp, xs, &resid);
5157                         }
5158                         if ((XS_NOERR(xs) || XS_ERR(xs) == HBA_NOERROR) && (*XS_STSP(xs) == SCSI_BUSY)) {
5159                                 XS_SETERR(xs, HBA_TGTBSY);
5160                         }
5161                         if (IS_SCSI(isp)) {
5162                                 XS_SET_RESID(xs, resid);
5163                                 /*
5164                                  * A new synchronous rate was negotiated for
5165                                  * this target. Mark state such that we'll go
5166                                  * look up that which has changed later.
5167                                  */
5168                                 if (req_status_flags & RQSTF_NEGOTIATION) {
5169                                         int t = XS_TGT(xs);
5170                                         sdparam *sdp = SDPARAM(isp, XS_CHANNEL(xs));
5171                                         sdp->isp_devparam[t].dev_refresh = 1;
5172                                         sdp->update = 1;
5173                                 }
5174                         } else {
5175                                 if (req_status_flags & RQSF_XFER_COMPLETE) {
5176                                         XS_SET_RESID(xs, 0);
5177                                 } else if (scsi_status & RQCS_RESID) {
5178                                         XS_SET_RESID(xs, resid);
5179                                 } else {
5180                                         XS_SET_RESID(xs, 0);
5181                                 }
5182                         }
5183                         if (snsp && slen) {
5184                                 if (totslen > slen) {
5185                                         continuations_expected += ((totslen - slen + QENTRY_LEN - 5) / (QENTRY_LEN - 4));
5186                                         if (ndone > (MAX_REQUESTQ_COMPLETIONS - continuations_expected - 1)) {
5187                                                 /* we'll lose some stats, but that's a small price to pay */
5188                                                 for (i = 0; i < ndone; i++) {
5189                                                         if (complist[i]) {
5190                                                                 isp->isp_rsltccmplt++;
5191                                                                 isp_done(complist[i]);
5192                                                         }
5193                                                 }
5194                                                 ndone = 0;
5195                                         }
5196                                         isp_prt(isp, ISP_LOGDEBUG0|ISP_LOG_CWARN, "Expecting %d more Status Continuations for total sense length of %u",
5197                                             continuations_expected, totslen);
5198                                 }
5199                                 XS_SAVE_SENSE(xs, snsp, totslen, slen);
5200                         } else if ((req_status_flags & RQSF_GOT_STATUS) && (scsi_status & 0xff) == SCSI_CHECK && IS_FC(isp)) {
5201                                 isp_prt(isp, ISP_LOGWARN, "CHECK CONDITION w/o sense data for CDB=0x%x", XS_CDBP(xs)[0] & 0xff);
5202                                 isp_print_bytes(isp, "CC with no Sense", QENTRY_LEN, qe);
5203                         }
5204                         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));
5205                         break;
5206                 case RQSTYPE_REQUEST:
5207                 case RQSTYPE_A64:
5208                 case RQSTYPE_T2RQS:
5209                 case RQSTYPE_T3RQS:
5210                 case RQSTYPE_T7RQS:
5211                         if (!IS_24XX(isp) && (sp->req_header.rqs_flags & RQSFLAG_FULL)) {
5212                                 /*
5213                                  * Force Queue Full status.
5214                                  */
5215                                 *XS_STSP(xs) = SCSI_QFULL;
5216                                 XS_SETERR(xs, HBA_NOERROR);
5217                         } else if (XS_NOERR(xs)) {
5218                                 isp_prt(isp, ISP_LOG_WARN1,
5219                                     "%d.%d.%jx badness at %s:%u",
5220                                     XS_CHANNEL(xs), XS_TGT(xs),
5221                                     (uintmax_t)XS_LUN(xs),
5222                                     __func__, __LINE__);
5223                                 XS_SETERR(xs, HBA_BOTCH);
5224                         }
5225                         XS_SET_RESID(xs, XS_XFRLEN(xs));
5226                         break;
5227                 default:
5228                         isp_print_bytes(isp, "Unhandled Response Type", QENTRY_LEN, qe);
5229                         if (XS_NOERR(xs)) {
5230                                 XS_SETERR(xs, HBA_BOTCH);
5231                         }
5232                         break;
5233                 }
5234
5235                 /*
5236                  * Free any DMA resources. As a side effect, this may
5237                  * also do any cache flushing necessary for data coherence.
5238                  */
5239                 if (XS_XFRLEN(xs)) {
5240                         ISP_DMAFREE(isp, xs, sp->req_handle);
5241                 }
5242                 isp_destroy_handle(isp, sp->req_handle);
5243
5244                 if (isp->isp_nactive > 0) {
5245                     isp->isp_nactive--;
5246                 }
5247                 complist[ndone++] = xs; /* defer completion call until later */
5248                 ISP_MEMZERO(hp, QENTRY_LEN);    /* PERF */
5249                 last_etype = etype;
5250                 if (ndone == MAX_REQUESTQ_COMPLETIONS) {
5251                         break;
5252                 }
5253         }
5254
5255         /*
5256          * If we looked at any commands, then it's valid to find out
5257          * what the outpointer is. It also is a trigger to update the
5258          * ISP's notion of what we've seen so far.
5259          */
5260         if (nlooked) {
5261                 ISP_WRITE(isp, isp->isp_respoutrp, optr);
5262                 isp->isp_resodx = optr;
5263                 if (isp->isp_rscchiwater < ndone)
5264                         isp->isp_rscchiwater = ndone;
5265         }
5266
5267 out:
5268
5269         if (IS_24XX(isp)) {
5270                 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
5271         } else {
5272                 ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
5273                 ISP_WRITE(isp, BIU_SEMA, 0);
5274         }
5275
5276         for (i = 0; i < ndone; i++) {
5277                 xs = complist[i];
5278                 if (xs) {
5279                         if (((isp->isp_dblev & (ISP_LOGDEBUG1|ISP_LOGDEBUG2|ISP_LOGDEBUG3))) ||
5280                             ((isp->isp_dblev & (ISP_LOGDEBUG0|ISP_LOG_CWARN) && ((!XS_NOERR(xs)) || (*XS_STSP(xs) != SCSI_GOOD))))) {
5281                                 isp_prt_endcmd(isp, xs);
5282                         }
5283                         isp->isp_rsltccmplt++;
5284                         isp_done(xs);
5285                 }
5286         }
5287 }
5288
5289 /*
5290  * Support routines.
5291  */
5292
5293 void
5294 isp_prt_endcmd(ispsoftc_t *isp, XS_T *xs)
5295 {
5296         char cdbstr[16 * 5 + 1];
5297         int i, lim;
5298
5299         lim = XS_CDBLEN(xs) > 16? 16 : XS_CDBLEN(xs);
5300         ISP_SNPRINTF(cdbstr, sizeof (cdbstr), "0x%02x ", XS_CDBP(xs)[0]);
5301         for (i = 1; i < lim; i++) {
5302                 ISP_SNPRINTF(cdbstr, sizeof (cdbstr), "%s0x%02x ", cdbstr, XS_CDBP(xs)[i]);
5303         }
5304         if (XS_SENSE_VALID(xs)) {
5305                 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",
5306                     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));
5307         } else {
5308                 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));
5309         }
5310 }
5311
5312 /*
5313  * Parse an ASYNC mailbox complete
5314  *
5315  * Return non-zero if the event has been acknowledged.
5316  */
5317 static int
5318 isp_parse_async(ispsoftc_t *isp, uint16_t mbox)
5319 {
5320         int acked = 0;
5321         uint32_t h1 = 0, h2 = 0;
5322         uint16_t chan = 0;
5323
5324         /*
5325          * Pick up the channel, but not if this is a ASYNC_RIO32_2,
5326          * where Mailboxes 6/7 have the second handle.
5327          */
5328         if (mbox != ASYNC_RIO32_2) {
5329                 if (IS_DUALBUS(isp)) {
5330                         chan = ISP_READ(isp, OUTMAILBOX6);
5331                 }
5332         }
5333         isp_prt(isp, ISP_LOGDEBUG2, "Async Mbox 0x%x", mbox);
5334
5335         switch (mbox) {
5336         case ASYNC_BUS_RESET:
5337                 ISP_SET_SENDMARKER(isp, chan, 1);
5338 #ifdef  ISP_TARGET_MODE
5339                 if (isp_target_async(isp, chan, mbox)) {
5340                         acked = 1;
5341                 }
5342 #endif
5343                 isp_async(isp, ISPASYNC_BUS_RESET, chan);
5344                 break;
5345         case ASYNC_SYSTEM_ERROR:
5346                 isp->isp_dead = 1;
5347                 isp->isp_state = ISP_CRASHED;
5348                 /*
5349                  * Were we waiting for a mailbox command to complete?
5350                  * If so, it's dead, so wake up the waiter.
5351                  */
5352                 if (isp->isp_mboxbsy) {
5353                         isp->isp_obits = 1;
5354                         isp->isp_mboxtmp[0] = MBOX_HOST_INTERFACE_ERROR;
5355                         MBOX_NOTIFY_COMPLETE(isp);
5356                 }
5357                 /*
5358                  * It's up to the handler for isp_async to reinit stuff and
5359                  * restart the firmware
5360                  */
5361                 isp_async(isp, ISPASYNC_FW_CRASH);
5362                 acked = 1;
5363                 break;
5364
5365         case ASYNC_RQS_XFER_ERR:
5366                 isp_prt(isp, ISP_LOGERR, "Request Queue Transfer Error");
5367                 break;
5368
5369         case ASYNC_RSP_XFER_ERR:
5370                 isp_prt(isp, ISP_LOGERR, "Response Queue Transfer Error");
5371                 break;
5372
5373         case ASYNC_QWAKEUP:
5374                 /*
5375                  * We've just been notified that the Queue has woken up.
5376                  * We don't need to be chatty about this- just unlatch things
5377                  * and move on.
5378                  */
5379                 mbox = ISP_READ(isp, isp->isp_rqstoutrp);
5380                 break;
5381
5382         case ASYNC_TIMEOUT_RESET:
5383                 isp_prt(isp, ISP_LOGWARN, "timeout initiated SCSI bus reset of chan %d", chan);
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                 break;
5391
5392         case ASYNC_DEVICE_RESET:
5393                 isp_prt(isp, ISP_LOGINFO, "device reset on chan %d", chan);
5394                 ISP_SET_SENDMARKER(isp, chan, 1);
5395 #ifdef  ISP_TARGET_MODE
5396                 if (isp_target_async(isp, chan, mbox)) {
5397                         acked = 1;
5398                 }
5399 #endif
5400                 break;
5401
5402         case ASYNC_EXTMSG_UNDERRUN:
5403                 isp_prt(isp, ISP_LOGWARN, "extended message underrun");
5404                 break;
5405
5406         case ASYNC_SCAM_INT:
5407                 isp_prt(isp, ISP_LOGINFO, "SCAM interrupt");
5408                 break;
5409
5410         case ASYNC_HUNG_SCSI:
5411                 isp_prt(isp, ISP_LOGERR, "stalled SCSI Bus after DATA Overrun");
5412                 /* XXX: Need to issue SCSI reset at this point */
5413                 break;
5414
5415         case ASYNC_KILLED_BUS:
5416                 isp_prt(isp, ISP_LOGERR, "SCSI Bus reset after DATA Overrun");
5417                 break;
5418
5419         case ASYNC_BUS_TRANSIT:
5420                 mbox = ISP_READ(isp, OUTMAILBOX2);
5421                 switch (mbox & SXP_PINS_MODE_MASK) {
5422                 case SXP_PINS_LVD_MODE:
5423                         isp_prt(isp, ISP_LOGINFO, "Transition to LVD mode");
5424                         SDPARAM(isp, chan)->isp_diffmode = 0;
5425                         SDPARAM(isp, chan)->isp_ultramode = 0;
5426                         SDPARAM(isp, chan)->isp_lvdmode = 1;
5427                         break;
5428                 case SXP_PINS_HVD_MODE:
5429                         isp_prt(isp, ISP_LOGINFO,
5430                             "Transition to Differential mode");
5431                         SDPARAM(isp, chan)->isp_diffmode = 1;
5432                         SDPARAM(isp, chan)->isp_ultramode = 0;
5433                         SDPARAM(isp, chan)->isp_lvdmode = 0;
5434                         break;
5435                 case SXP_PINS_SE_MODE:
5436                         isp_prt(isp, ISP_LOGINFO,
5437                             "Transition to Single Ended mode");
5438                         SDPARAM(isp, chan)->isp_diffmode = 0;
5439                         SDPARAM(isp, chan)->isp_ultramode = 1;
5440                         SDPARAM(isp, chan)->isp_lvdmode = 0;
5441                         break;
5442                 default:
5443                         isp_prt(isp, ISP_LOGWARN,
5444                             "Transition to Unknown Mode 0x%x", mbox);
5445                         break;
5446                 }
5447                 /*
5448                  * XXX: Set up to renegotiate again!
5449                  */
5450                 /* Can only be for a 1080... */
5451                 ISP_SET_SENDMARKER(isp, chan, 1);
5452                 break;
5453
5454         case ASYNC_CMD_CMPLT:
5455         case ASYNC_RIO32_1:
5456                 if (!IS_ULTRA3(isp)) {
5457                         isp_prt(isp, ISP_LOGERR, "unexpected fast posting completion");
5458                         break;
5459                 }
5460                 /* FALLTHROUGH */
5461                 h1 = (ISP_READ(isp, OUTMAILBOX2) << 16) | ISP_READ(isp, OUTMAILBOX1);
5462                 break;
5463
5464         case ASYNC_RIO32_2:
5465                 h1 = (ISP_READ(isp, OUTMAILBOX2) << 16) | ISP_READ(isp, OUTMAILBOX1);
5466                 h2 = (ISP_READ(isp, OUTMAILBOX7) << 16) | ISP_READ(isp, OUTMAILBOX6);
5467                 break;
5468
5469         case ASYNC_RIO16_5:
5470         case ASYNC_RIO16_4:
5471         case ASYNC_RIO16_3:
5472         case ASYNC_RIO16_2:
5473         case ASYNC_RIO16_1:
5474                 isp_prt(isp, ISP_LOGERR, "unexpected 16 bit RIO handle");
5475                 break;
5476         default:
5477                 isp_prt(isp, ISP_LOGWARN, "%s: unhandled async code 0x%x", __func__, mbox);
5478                 break;
5479         }
5480
5481         if (h1 || h2) {
5482                 isp_prt(isp, ISP_LOGDEBUG3, "fast post/rio completion of 0x%08x", h1);
5483                 isp_fastpost_complete(isp, h1);
5484                 if (h2) {
5485                         isp_prt(isp, ISP_LOGDEBUG3, "fast post/rio completion of 0x%08x", h2);
5486                         isp_fastpost_complete(isp, h2);
5487                         if (isp->isp_fpcchiwater < 2) {
5488                                 isp->isp_fpcchiwater = 2;
5489                         }
5490                 } else {
5491                         if (isp->isp_fpcchiwater < 1) {
5492                                 isp->isp_fpcchiwater = 1;
5493                         }
5494                 }
5495         } else {
5496                 isp->isp_intoasync++;
5497         }
5498         return (acked);
5499 }
5500
5501 static int
5502 isp_parse_async_fc(ispsoftc_t *isp, uint16_t mbox)
5503 {
5504         fcparam *fcp;
5505         int acked = 0;
5506         uint16_t chan;
5507
5508         if (IS_DUALBUS(isp)) {
5509                 chan = ISP_READ(isp, OUTMAILBOX6);
5510         } else {
5511                 chan = 0;
5512         }
5513         isp_prt(isp, ISP_LOGDEBUG2, "Async Mbox 0x%x", mbox);
5514
5515         switch (mbox) {
5516         case ASYNC_SYSTEM_ERROR:
5517                 isp->isp_dead = 1;
5518                 isp->isp_state = ISP_CRASHED;
5519                 FCPARAM(isp, chan)->isp_loopstate = LOOP_NIL;
5520                 isp_change_fw_state(isp, chan, FW_CONFIG_WAIT);
5521                 /*
5522                  * Were we waiting for a mailbox command to complete?
5523                  * If so, it's dead, so wake up the waiter.
5524                  */
5525                 if (isp->isp_mboxbsy) {
5526                         isp->isp_obits = 1;
5527                         isp->isp_mboxtmp[0] = MBOX_HOST_INTERFACE_ERROR;
5528                         MBOX_NOTIFY_COMPLETE(isp);
5529                 }
5530                 /*
5531                  * It's up to the handler for isp_async to reinit stuff and
5532                  * restart the firmware
5533                  */
5534                 isp_async(isp, ISPASYNC_FW_CRASH);
5535                 acked = 1;
5536                 break;
5537
5538         case ASYNC_RQS_XFER_ERR:
5539                 isp_prt(isp, ISP_LOGERR, "Request Queue Transfer Error");
5540                 break;
5541
5542         case ASYNC_RSP_XFER_ERR:
5543                 isp_prt(isp, ISP_LOGERR, "Response Queue Transfer Error");
5544                 break;
5545
5546         case ASYNC_QWAKEUP:
5547 #ifdef  ISP_TARGET_MODE
5548                 if (IS_24XX(isp)) {
5549                         isp_prt(isp, ISP_LOGERR, "ATIO Queue Transfer Error");
5550                         break;
5551                 }
5552 #endif
5553                 isp_prt(isp, ISP_LOGERR, "%s: unexpected ASYNC_QWAKEUP code", __func__);
5554                 break;
5555
5556         case ASYNC_CMD_CMPLT:
5557                 isp_fastpost_complete(isp, (ISP_READ(isp, OUTMAILBOX2) << 16) | ISP_READ(isp, OUTMAILBOX1));
5558                 if (isp->isp_fpcchiwater < 1) {
5559                         isp->isp_fpcchiwater = 1;
5560                 }
5561                 break;
5562
5563         case ASYNC_RIOZIO_STALL:
5564                 break;
5565
5566         case ASYNC_CTIO_DONE:
5567 #ifdef  ISP_TARGET_MODE
5568                 if (isp_target_async(isp, (ISP_READ(isp, OUTMAILBOX2) << 16) | ISP_READ(isp, OUTMAILBOX1), mbox)) {
5569                         acked = 1;
5570                 } else {
5571                         isp->isp_fphccmplt++;
5572                 }
5573 #else
5574                 isp_prt(isp, ISP_LOGWARN, "unexpected ASYNC CTIO done");
5575 #endif
5576                 break;
5577         case ASYNC_LIP_ERROR:
5578         case ASYNC_LIP_F8:
5579         case ASYNC_LIP_OCCURRED:
5580         case ASYNC_PTPMODE:
5581                 /*
5582                  * These are broadcast events that have to be sent across
5583                  * all active channels.
5584                  */
5585                 for (chan = 0; chan < isp->isp_nchan; chan++) {
5586                         fcp = FCPARAM(isp, chan);
5587                         int topo = fcp->isp_topo;
5588
5589                         if (fcp->role == ISP_ROLE_NONE) {
5590                                 continue;
5591                         }
5592
5593                         fcp->isp_loopstate = LOOP_NIL;
5594                         ISP_SET_SENDMARKER(isp, chan, 1);
5595                         isp_async(isp, ISPASYNC_LIP, chan);
5596 #ifdef  ISP_TARGET_MODE
5597                         if (isp_target_async(isp, chan, mbox)) {
5598                                 acked = 1;
5599                         }
5600 #endif
5601                         /*
5602                          * We've had problems with data corruption occuring on
5603                          * commands that complete (with no apparent error) after
5604                          * we receive a LIP. This has been observed mostly on
5605                          * Local Loop topologies. To be safe, let's just mark
5606                          * all active initiator commands as dead.
5607                          */
5608                         if (topo == TOPO_NL_PORT || topo == TOPO_FL_PORT) {
5609                                 int i, j;
5610                                 for (i = j = 0; i < isp->isp_maxcmds; i++) {
5611                                         XS_T *xs;
5612                                         isp_hdl_t *hdp;
5613
5614                                         hdp = &isp->isp_xflist[i];
5615                                         if (ISP_H2HT(hdp->handle) != ISP_HANDLE_INITIATOR) {
5616                                                 continue;
5617                                         }
5618                                         xs = hdp->cmd;
5619                                         if (XS_CHANNEL(xs) != chan) {
5620                                                 continue;
5621                                         }
5622                                         j++;
5623                                         isp_prt(isp, ISP_LOG_WARN1,
5624                                             "%d.%d.%jx bus reset set at %s:%u",
5625                                             XS_CHANNEL(xs), XS_TGT(xs),
5626                                             (uintmax_t)XS_LUN(xs),
5627                                             __func__, __LINE__);
5628                                         XS_SETERR(xs, HBA_BUSRESET);
5629                                 }
5630                                 if (j) {
5631                                         isp_prt(isp, ISP_LOGERR, lipd, chan, j);
5632                                 }
5633                         }
5634                 }
5635                 break;
5636
5637         case ASYNC_LOOP_UP:
5638                 /*
5639                  * This is a broadcast event that has to be sent across
5640                  * all active channels.
5641                  */
5642                 for (chan = 0; chan < isp->isp_nchan; chan++) {
5643                         fcp = FCPARAM(isp, chan);
5644                         if (fcp->role == ISP_ROLE_NONE)
5645                                 continue;
5646                         ISP_SET_SENDMARKER(isp, chan, 1);
5647                         isp_async(isp, ISPASYNC_LOOP_UP, chan);
5648 #ifdef  ISP_TARGET_MODE
5649                         if (isp_target_async(isp, chan, mbox)) {
5650                                 acked = 1;
5651                         }
5652 #endif
5653                 }
5654                 break;
5655
5656         case ASYNC_LOOP_DOWN:
5657                 /*
5658                  * This is a broadcast event that has to be sent across
5659                  * all active channels.
5660                  */
5661                 for (chan = 0; chan < isp->isp_nchan; chan++) {
5662                         fcp = FCPARAM(isp, chan);
5663                         if (fcp->role == ISP_ROLE_NONE)
5664                                 continue;
5665                         ISP_SET_SENDMARKER(isp, chan, 1);
5666                         fcp->isp_loopstate = LOOP_NIL;
5667                         isp_async(isp, ISPASYNC_LOOP_DOWN, chan);
5668 #ifdef  ISP_TARGET_MODE
5669                         if (isp_target_async(isp, chan, mbox)) {
5670                                 acked = 1;
5671                         }
5672 #endif
5673                 }
5674                 break;
5675
5676         case ASYNC_LOOP_RESET:
5677                 /*
5678                  * This is a broadcast event that has to be sent across
5679                  * all active channels.
5680                  */
5681                 for (chan = 0; chan < isp->isp_nchan; chan++) {
5682                         fcp = FCPARAM(isp, chan);
5683                         if (fcp->role == ISP_ROLE_NONE)
5684                                 continue;
5685                         ISP_SET_SENDMARKER(isp, chan, 1);
5686                         fcp->isp_loopstate = LOOP_NIL;
5687                         isp_async(isp, ISPASYNC_LOOP_RESET, chan);
5688 #ifdef  ISP_TARGET_MODE
5689                         if (isp_target_async(isp, chan, mbox)) {
5690                                 acked = 1;
5691                         }
5692 #endif
5693                 }
5694                 break;
5695
5696         case ASYNC_PDB_CHANGED:
5697         {
5698                 int echan, nphdl, nlstate, reason;
5699
5700                 if (IS_23XX(isp) || IS_24XX(isp)) {
5701                         nphdl = ISP_READ(isp, OUTMAILBOX1);
5702                         nlstate = ISP_READ(isp, OUTMAILBOX2);
5703                 } else {
5704                         nphdl = nlstate = 0xffff;
5705                 }
5706                 if (IS_24XX(isp))
5707                         reason = ISP_READ(isp, OUTMAILBOX3) >> 8;
5708                 else
5709                         reason = 0xff;
5710                 if (ISP_CAP_MULTI_ID(isp)) {
5711                         chan = ISP_READ(isp, OUTMAILBOX3) & 0xff;
5712                         if (chan == 0xff || nphdl == NIL_HANDLE) {
5713                                 chan = 0;
5714                                 echan = isp->isp_nchan - 1;
5715                         } else if (chan >= isp->isp_nchan) {
5716                                 break;
5717                         } else {
5718                                 echan = chan;
5719                         }
5720                 } else {
5721                         chan = echan = 0;
5722                 }
5723                 for (; chan <= echan; chan++) {
5724                         fcp = FCPARAM(isp, chan);
5725                         if (fcp->role == ISP_ROLE_NONE)
5726                                 continue;
5727                         if (fcp->isp_loopstate > LOOP_LTEST_DONE)
5728                                 fcp->isp_loopstate = LOOP_LTEST_DONE;
5729                         isp_async(isp, ISPASYNC_CHANGE_NOTIFY, chan,
5730                             ISPASYNC_CHANGE_PDB, nphdl, nlstate, reason);
5731                 }
5732                 break;
5733         }
5734         case ASYNC_CHANGE_NOTIFY:
5735         {
5736                 int portid;
5737
5738                 portid = ((ISP_READ(isp, OUTMAILBOX1) & 0xff) << 16) |
5739                     ISP_READ(isp, OUTMAILBOX2);
5740                 if (ISP_CAP_MULTI_ID(isp)) {
5741                         chan = ISP_READ(isp, OUTMAILBOX3) & 0xff;
5742                         if (chan >= isp->isp_nchan)
5743                                 break;
5744                 } else {
5745                         chan = 0;
5746                 }
5747                 fcp = FCPARAM(isp, chan);
5748                 if (fcp->role == ISP_ROLE_NONE)
5749                         break;
5750                 if (fcp->isp_loopstate > LOOP_LSCAN_DONE)
5751                         fcp->isp_loopstate = LOOP_LSCAN_DONE;
5752                 isp_async(isp, ISPASYNC_CHANGE_NOTIFY, chan,
5753                     ISPASYNC_CHANGE_SNS, portid);
5754                 break;
5755         }
5756
5757         case ASYNC_CONNMODE:
5758                 /*
5759                  * This only applies to 2100 amd 2200 cards
5760                  */
5761                 if (!IS_2200(isp) && !IS_2100(isp)) {
5762                         isp_prt(isp, ISP_LOGWARN, "bad card for ASYNC_CONNMODE event");
5763                         break;
5764                 }
5765                 chan = 0;
5766                 mbox = ISP_READ(isp, OUTMAILBOX1);
5767                 switch (mbox) {
5768                 case ISP_CONN_LOOP:
5769                         isp_prt(isp, ISP_LOGINFO,
5770                             "Point-to-Point -> Loop mode");
5771                         break;
5772                 case ISP_CONN_PTP:
5773                         isp_prt(isp, ISP_LOGINFO,
5774                             "Loop -> Point-to-Point mode");
5775                         break;
5776                 case ISP_CONN_BADLIP:
5777                         isp_prt(isp, ISP_LOGWARN,
5778                             "Point-to-Point -> Loop mode (BAD LIP)");
5779                         break;
5780                 case ISP_CONN_FATAL:
5781                         isp->isp_dead = 1;
5782                         isp->isp_state = ISP_CRASHED;
5783                         isp_prt(isp, ISP_LOGERR, "FATAL CONNECTION ERROR");
5784                         isp_async(isp, ISPASYNC_FW_CRASH);
5785                         return (-1);
5786                 case ISP_CONN_LOOPBACK:
5787                         isp_prt(isp, ISP_LOGWARN,
5788                             "Looped Back in Point-to-Point mode");
5789                         break;
5790                 default:
5791                         isp_prt(isp, ISP_LOGWARN,
5792                             "Unknown connection mode (0x%x)", mbox);
5793                         break;
5794                 }
5795                 ISP_SET_SENDMARKER(isp, chan, 1);
5796                 FCPARAM(isp, chan)->isp_loopstate = LOOP_NIL;
5797                 isp_async(isp, ISPASYNC_CHANGE_NOTIFY, chan, ISPASYNC_CHANGE_OTHER);
5798                 break;
5799
5800         case ASYNC_RCV_ERR:
5801                 if (IS_24XX(isp)) {
5802                         isp_prt(isp, ISP_LOGWARN, "Receive Error");
5803                 } else {
5804                         isp_prt(isp, ISP_LOGWARN, "unexpected ASYNC_RCV_ERR");
5805                 }
5806                 break;
5807         case ASYNC_RJT_SENT:    /* same as ASYNC_QFULL_SENT */
5808                 if (IS_24XX(isp)) {
5809                         isp_prt(isp, ISP_LOGTDEBUG0, "LS_RJT sent");
5810                         break;
5811                 } else if (IS_2200(isp)) {
5812                         isp_prt(isp, ISP_LOGTDEBUG0, "QFULL sent");
5813                         break;
5814                 }
5815                 /* FALLTHROUGH */
5816         default:
5817                 isp_prt(isp, ISP_LOGWARN, "Unknown Async Code 0x%x", mbox);
5818                 break;
5819         }
5820         if (mbox != ASYNC_CTIO_DONE && mbox != ASYNC_CMD_CMPLT) {
5821                 isp->isp_intoasync++;
5822         }
5823         return (acked);
5824 }
5825
5826 /*
5827  * Handle other response entries. A pointer to the request queue output
5828  * index is here in case we want to eat several entries at once, although
5829  * this is not used currently.
5830  */
5831
5832 static int
5833 isp_handle_other_response(ispsoftc_t *isp, int type, isphdr_t *hp, uint32_t *optrp)
5834 {
5835         isp_ridacq_t rid;
5836         int chan, c;
5837
5838         switch (type) {
5839         case RQSTYPE_STATUS_CONT:
5840                 isp_prt(isp, ISP_LOG_WARN1, "Ignored Continuation Response");
5841                 return (1);
5842         case RQSTYPE_MARKER:
5843                 isp_prt(isp, ISP_LOG_WARN1, "Marker Response");
5844                 return (1);
5845         case RQSTYPE_RPT_ID_ACQ:
5846                 isp_get_ridacq(isp, (isp_ridacq_t *)hp, &rid);
5847                 if (rid.ridacq_format == 0) {
5848                         for (chan = 0; chan < isp->isp_nchan; chan++) {
5849                                 fcparam *fcp = FCPARAM(isp, chan);
5850                                 if (fcp->role == ISP_ROLE_NONE)
5851                                         continue;
5852                                 c = (chan == 0) ? 127 : (chan - 1);
5853                                 if (rid.ridacq_map[c / 16] & (1 << (c % 16)))
5854                                         isp_async(isp, ISPASYNC_CHANGE_NOTIFY,
5855                                             chan, ISPASYNC_CHANGE_OTHER);
5856                         }
5857                 } else {
5858                         isp_async(isp, ISPASYNC_CHANGE_NOTIFY,
5859                             rid.ridacq_vp_index, ISPASYNC_CHANGE_OTHER);
5860                 }
5861                 return (1);
5862         case RQSTYPE_ATIO:
5863         case RQSTYPE_CTIO:
5864         case RQSTYPE_ENABLE_LUN:
5865         case RQSTYPE_MODIFY_LUN:
5866         case RQSTYPE_NOTIFY:
5867         case RQSTYPE_NOTIFY_ACK:
5868         case RQSTYPE_CTIO1:
5869         case RQSTYPE_ATIO2:
5870         case RQSTYPE_CTIO2:
5871         case RQSTYPE_CTIO3:
5872         case RQSTYPE_CTIO7:
5873         case RQSTYPE_ABTS_RCVD:
5874         case RQSTYPE_ABTS_RSP:
5875                 isp->isp_rsltccmplt++;  /* count as a response completion */
5876 #ifdef  ISP_TARGET_MODE
5877                 if (isp_target_notify(isp, (ispstatusreq_t *) hp, optrp)) {
5878                         return (1);
5879                 }
5880 #endif
5881                 /* FALLTHROUGH */
5882         case RQSTYPE_REQUEST:
5883         default:
5884                 ISP_DELAY(100);
5885                 if (type != isp_get_response_type(isp, hp)) {
5886                         /*
5887                          * This is questionable- we're just papering over
5888                          * something we've seen on SMP linux in target
5889                          * mode- we don't really know what's happening
5890                          * here that causes us to think we've gotten
5891                          * an entry, but that either the entry isn't
5892                          * filled out yet or our CPU read data is stale.
5893                          */
5894                         isp_prt(isp, ISP_LOGINFO,
5895                                 "unstable type in response queue");
5896                         return (-1);
5897                 }
5898                 isp_prt(isp, ISP_LOGWARN, "Unhandled Response Type 0x%x",
5899                     isp_get_response_type(isp, hp));
5900                 return (0);
5901         }
5902 }
5903
5904 static void
5905 isp_parse_status(ispsoftc_t *isp, ispstatusreq_t *sp, XS_T *xs, long *rp)
5906 {
5907         switch (sp->req_completion_status & 0xff) {
5908         case RQCS_COMPLETE:
5909                 if (XS_NOERR(xs)) {
5910                         XS_SETERR(xs, HBA_NOERROR);
5911                 }
5912                 return;
5913
5914         case RQCS_INCOMPLETE:
5915                 if ((sp->req_state_flags & RQSF_GOT_TARGET) == 0) {
5916                         isp_xs_prt(isp, xs, ISP_LOG_WARN1, "Selection Timeout @ %s:%d", __func__, __LINE__);
5917                         if (XS_NOERR(xs)) {
5918                                 XS_SETERR(xs, HBA_SELTIMEOUT);
5919                                 *rp = XS_XFRLEN(xs);
5920                         }
5921                         return;
5922                 }
5923                 isp_xs_prt(isp, xs, ISP_LOGERR, "Command Incomplete, state 0x%x", sp->req_state_flags);
5924                 break;
5925
5926         case RQCS_DMA_ERROR:
5927                 isp_xs_prt(isp, xs, ISP_LOGERR, "DMA Error");
5928                 *rp = XS_XFRLEN(xs);
5929                 break;
5930
5931         case RQCS_TRANSPORT_ERROR:
5932         {
5933                 char buf[172];
5934                 ISP_SNPRINTF(buf, sizeof (buf), "states=>");
5935                 if (sp->req_state_flags & RQSF_GOT_BUS) {
5936                         ISP_SNPRINTF(buf, sizeof (buf), "%s GOT_BUS", buf);
5937                 }
5938                 if (sp->req_state_flags & RQSF_GOT_TARGET) {
5939                         ISP_SNPRINTF(buf, sizeof (buf), "%s GOT_TGT", buf);
5940                 }
5941                 if (sp->req_state_flags & RQSF_SENT_CDB) {
5942                         ISP_SNPRINTF(buf, sizeof (buf), "%s SENT_CDB", buf);
5943                 }
5944                 if (sp->req_state_flags & RQSF_XFRD_DATA) {
5945                         ISP_SNPRINTF(buf, sizeof (buf), "%s XFRD_DATA", buf);
5946                 }
5947                 if (sp->req_state_flags & RQSF_GOT_STATUS) {
5948                         ISP_SNPRINTF(buf, sizeof (buf), "%s GOT_STS", buf);
5949                 }
5950                 if (sp->req_state_flags & RQSF_GOT_SENSE) {
5951                         ISP_SNPRINTF(buf, sizeof (buf), "%s GOT_SNS", buf);
5952                 }
5953                 if (sp->req_state_flags & RQSF_XFER_COMPLETE) {
5954                         ISP_SNPRINTF(buf, sizeof (buf), "%s XFR_CMPLT", buf);
5955                 }
5956                 ISP_SNPRINTF(buf, sizeof (buf), "%s\nstatus=>", buf);
5957                 if (sp->req_status_flags & RQSTF_DISCONNECT) {
5958                         ISP_SNPRINTF(buf, sizeof (buf), "%s Disconnect", buf);
5959                 }
5960                 if (sp->req_status_flags & RQSTF_SYNCHRONOUS) {
5961                         ISP_SNPRINTF(buf, sizeof (buf), "%s Sync_xfr", buf);
5962                 }
5963                 if (sp->req_status_flags & RQSTF_PARITY_ERROR) {
5964                         ISP_SNPRINTF(buf, sizeof (buf), "%s Parity", buf);
5965                 }
5966                 if (sp->req_status_flags & RQSTF_BUS_RESET) {
5967                         ISP_SNPRINTF(buf, sizeof (buf), "%s Bus_Reset", buf);
5968                 }
5969                 if (sp->req_status_flags & RQSTF_DEVICE_RESET) {
5970                         ISP_SNPRINTF(buf, sizeof (buf), "%s Device_Reset", buf);
5971                 }
5972                 if (sp->req_status_flags & RQSTF_ABORTED) {
5973                         ISP_SNPRINTF(buf, sizeof (buf), "%s Aborted", buf);
5974                 }
5975                 if (sp->req_status_flags & RQSTF_TIMEOUT) {
5976                         ISP_SNPRINTF(buf, sizeof (buf), "%s Timeout", buf);
5977                 }
5978                 if (sp->req_status_flags & RQSTF_NEGOTIATION) {
5979                         ISP_SNPRINTF(buf, sizeof (buf), "%s Negotiation", buf);
5980                 }
5981                 isp_xs_prt(isp, xs,  ISP_LOGERR, "Transport Error: %s", buf);
5982                 *rp = XS_XFRLEN(xs);
5983                 break;
5984         }
5985         case RQCS_RESET_OCCURRED:
5986         {
5987                 int chan;
5988                 isp_xs_prt(isp, xs, ISP_LOGWARN, "Bus Reset destroyed command");
5989                 for (chan = 0; chan < isp->isp_nchan; chan++) {
5990                         FCPARAM(isp, chan)->sendmarker = 1;
5991                 }
5992                 if (XS_NOERR(xs)) {
5993                         XS_SETERR(xs, HBA_BUSRESET);
5994                 }
5995                 *rp = XS_XFRLEN(xs);
5996                 return;
5997         }
5998         case RQCS_ABORTED:
5999                 isp_xs_prt(isp, xs, ISP_LOGERR, "Command Aborted");
6000                 ISP_SET_SENDMARKER(isp, XS_CHANNEL(xs), 1);
6001                 if (XS_NOERR(xs)) {
6002                         XS_SETERR(xs, HBA_ABORTED);
6003                 }
6004                 return;
6005
6006         case RQCS_TIMEOUT:
6007                 isp_xs_prt(isp, xs, ISP_LOGWARN, "Command timed out");
6008                 /*
6009                  * XXX: Check to see if we logged out of the device.
6010                  */
6011                 if (XS_NOERR(xs)) {
6012                         XS_SETERR(xs, HBA_CMDTIMEOUT);
6013                 }
6014                 return;
6015
6016         case RQCS_DATA_OVERRUN:
6017                 XS_SET_RESID(xs, sp->req_resid);
6018                 isp_xs_prt(isp, xs, ISP_LOGERR, "data overrun (%ld)", (long) XS_GET_RESID(xs));
6019                 if (XS_NOERR(xs)) {
6020                         XS_SETERR(xs, HBA_DATAOVR);
6021                 }
6022                 return;
6023
6024         case RQCS_COMMAND_OVERRUN:
6025                 isp_xs_prt(isp, xs, ISP_LOGERR, "command overrun");
6026                 break;
6027
6028         case RQCS_STATUS_OVERRUN:
6029                 isp_xs_prt(isp, xs, ISP_LOGERR, "status overrun");
6030                 break;
6031
6032         case RQCS_BAD_MESSAGE:
6033                 isp_xs_prt(isp, xs, ISP_LOGERR, "msg not COMMAND COMPLETE after status");
6034                 break;
6035
6036         case RQCS_NO_MESSAGE_OUT:
6037                 isp_xs_prt(isp, xs, ISP_LOGERR, "No MESSAGE OUT phase after selection");
6038                 break;
6039
6040         case RQCS_EXT_ID_FAILED:
6041                 isp_xs_prt(isp, xs, ISP_LOGERR, "EXTENDED IDENTIFY failed");
6042                 break;
6043
6044         case RQCS_IDE_MSG_FAILED:
6045                 isp_xs_prt(isp, xs, ISP_LOGERR, "INITIATOR DETECTED ERROR rejected");
6046                 break;
6047
6048         case RQCS_ABORT_MSG_FAILED:
6049                 isp_xs_prt(isp, xs, ISP_LOGERR, "ABORT OPERATION rejected");
6050                 break;
6051
6052         case RQCS_REJECT_MSG_FAILED:
6053                 isp_xs_prt(isp, xs, ISP_LOGERR, "MESSAGE REJECT rejected");
6054                 break;
6055
6056         case RQCS_NOP_MSG_FAILED:
6057                 isp_xs_prt(isp, xs, ISP_LOGERR, "NOP rejected");
6058                 break;
6059
6060         case RQCS_PARITY_ERROR_MSG_FAILED:
6061                 isp_xs_prt(isp, xs, ISP_LOGERR, "MESSAGE PARITY ERROR rejected");
6062                 break;
6063
6064         case RQCS_DEVICE_RESET_MSG_FAILED:
6065                 isp_xs_prt(isp, xs, ISP_LOGWARN, "BUS DEVICE RESET rejected");
6066                 break;
6067
6068         case RQCS_ID_MSG_FAILED:
6069                 isp_xs_prt(isp, xs, ISP_LOGERR, "IDENTIFY rejected");
6070                 break;
6071
6072         case RQCS_UNEXP_BUS_FREE:
6073                 isp_xs_prt(isp, xs, ISP_LOGERR, "Unexpected Bus Free");
6074                 break;
6075
6076         case RQCS_DATA_UNDERRUN:
6077         {
6078                 if (IS_FC(isp)) {
6079                         int ru_marked = (sp->req_scsi_status & RQCS_RU) != 0;
6080                         if (!ru_marked || sp->req_resid > XS_XFRLEN(xs)) {
6081                                 isp_xs_prt(isp, xs, ISP_LOGWARN, bun, XS_XFRLEN(xs), sp->req_resid, (ru_marked)? "marked" : "not marked");
6082                                 if (XS_NOERR(xs)) {
6083                                         XS_SETERR(xs, HBA_BOTCH);
6084                                 }
6085                                 return;
6086                         }
6087                 }
6088                 XS_SET_RESID(xs, sp->req_resid);
6089                 if (XS_NOERR(xs)) {
6090                         XS_SETERR(xs, HBA_NOERROR);
6091                 }
6092                 return;
6093         }
6094
6095         case RQCS_XACT_ERR1:
6096                 isp_xs_prt(isp, xs, ISP_LOGERR, "HBA attempted queued transaction with disconnect not set");
6097                 break;
6098
6099         case RQCS_XACT_ERR2:
6100                 isp_xs_prt(isp, xs, ISP_LOGERR,
6101                     "HBA attempted queued transaction to target routine %jx",
6102                     (uintmax_t)XS_LUN(xs));
6103                 break;
6104
6105         case RQCS_XACT_ERR3:
6106                 isp_xs_prt(isp, xs, ISP_LOGERR, "HBA attempted queued cmd when queueing disabled");
6107                 break;
6108
6109         case RQCS_BAD_ENTRY:
6110                 isp_prt(isp, ISP_LOGERR, "Invalid IOCB entry type detected");
6111                 break;
6112
6113         case RQCS_QUEUE_FULL:
6114                 isp_xs_prt(isp, xs, ISP_LOG_WARN1, "internal queues full status 0x%x", *XS_STSP(xs));
6115
6116                 /*
6117                  * If QFULL or some other status byte is set, then this
6118                  * isn't an error, per se.
6119                  *
6120                  * Unfortunately, some QLogic f/w writers have, in
6121                  * some cases, ommitted to *set* status to QFULL.
6122                  */
6123 #if     0
6124                 if (*XS_STSP(xs) != SCSI_GOOD && XS_NOERR(xs)) {
6125                         XS_SETERR(xs, HBA_NOERROR);
6126                         return;
6127                 }
6128
6129 #endif
6130                 *XS_STSP(xs) = SCSI_QFULL;
6131                 XS_SETERR(xs, HBA_NOERROR);
6132                 return;
6133
6134         case RQCS_PHASE_SKIPPED:
6135                 isp_xs_prt(isp, xs, ISP_LOGERR, "SCSI phase skipped");
6136                 break;
6137
6138         case RQCS_ARQS_FAILED:
6139                 isp_xs_prt(isp, xs, ISP_LOGERR, "Auto Request Sense Failed");
6140                 if (XS_NOERR(xs)) {
6141                         XS_SETERR(xs, HBA_ARQFAIL);
6142                 }
6143                 return;
6144
6145         case RQCS_WIDE_FAILED:
6146                 isp_xs_prt(isp, xs, ISP_LOGERR, "Wide Negotiation Failed");
6147                 if (IS_SCSI(isp)) {
6148                         sdparam *sdp = SDPARAM(isp, XS_CHANNEL(xs));
6149                         sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_WIDE;
6150                         sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
6151                         sdp->update = 1;
6152                 }
6153                 if (XS_NOERR(xs)) {
6154                         XS_SETERR(xs, HBA_NOERROR);
6155                 }
6156                 return;
6157
6158         case RQCS_SYNCXFER_FAILED:
6159                 isp_xs_prt(isp, xs, ISP_LOGERR, "SDTR Message Failed");
6160                 if (IS_SCSI(isp)) {
6161                         sdparam *sdp = SDPARAM(isp, XS_CHANNEL(xs));
6162                         sdp += XS_CHANNEL(xs);
6163                         sdp->isp_devparam[XS_TGT(xs)].goal_flags &= ~DPARM_SYNC;
6164                         sdp->isp_devparam[XS_TGT(xs)].dev_update = 1;
6165                         sdp->update = 1;
6166                 }
6167                 break;
6168
6169         case RQCS_LVD_BUSERR:
6170                 isp_xs_prt(isp, xs, ISP_LOGERR, "Bad LVD condition");
6171                 break;
6172
6173         case RQCS_PORT_UNAVAILABLE:
6174                 /*
6175                  * No such port on the loop. Moral equivalent of SELTIMEO
6176                  */
6177         case RQCS_PORT_LOGGED_OUT:
6178         {
6179                 const char *reason;
6180                 uint8_t sts = sp->req_completion_status & 0xff;
6181
6182                 /*
6183                  * It was there (maybe)- treat as a selection timeout.
6184                  */
6185                 if (sts == RQCS_PORT_UNAVAILABLE) {
6186                         reason = "unavailable";
6187                 } else {
6188                         reason = "logout";
6189                 }
6190
6191                 isp_prt(isp, ISP_LOGINFO, "port %s for target %d", reason, XS_TGT(xs));
6192
6193                 /*
6194                  * If we're on a local loop, force a LIP (which is overkill)
6195                  * to force a re-login of this unit. If we're on fabric,
6196                  * then we'll have to log in again as a matter of course.
6197                  */
6198                 if (FCPARAM(isp, 0)->isp_topo == TOPO_NL_PORT ||
6199                     FCPARAM(isp, 0)->isp_topo == TOPO_FL_PORT) {
6200                         mbreg_t mbs;
6201                         MBSINIT(&mbs, MBOX_INIT_LIP, MBLOGALL, 0);
6202                         if (ISP_CAP_2KLOGIN(isp)) {
6203                                 mbs.ibits = (1 << 10);
6204                         }
6205                         isp_mboxcmd_qnw(isp, &mbs, 1);
6206                 }
6207                 if (XS_NOERR(xs)) {
6208                         XS_SETERR(xs, HBA_SELTIMEOUT);
6209                 }
6210                 return;
6211         }
6212         case RQCS_PORT_CHANGED:
6213                 isp_prt(isp, ISP_LOGWARN, "port changed for target %d", XS_TGT(xs));
6214                 if (XS_NOERR(xs)) {
6215                         XS_SETERR(xs, HBA_SELTIMEOUT);
6216                 }
6217                 return;
6218
6219         case RQCS_PORT_BUSY:
6220                 isp_prt(isp, ISP_LOGWARN, "port busy for target %d", XS_TGT(xs));
6221                 if (XS_NOERR(xs)) {
6222                         XS_SETERR(xs, HBA_TGTBSY);
6223                 }
6224                 return;
6225
6226         default:
6227                 isp_prt(isp, ISP_LOGERR, "Unknown Completion Status 0x%x", sp->req_completion_status);
6228                 break;
6229         }
6230         if (XS_NOERR(xs)) {
6231                 XS_SETERR(xs, HBA_BOTCH);
6232         }
6233 }
6234
6235 static void
6236 isp_parse_status_24xx(ispsoftc_t *isp, isp24xx_statusreq_t *sp, XS_T *xs, long *rp)
6237 {
6238         int ru_marked, sv_marked;
6239         int chan = XS_CHANNEL(xs);
6240
6241         switch (sp->req_completion_status) {
6242         case RQCS_COMPLETE:
6243                 if (XS_NOERR(xs)) {
6244                         XS_SETERR(xs, HBA_NOERROR);
6245                 }
6246                 return;
6247
6248         case RQCS_DMA_ERROR:
6249                 isp_xs_prt(isp, xs, ISP_LOGERR, "DMA error");
6250                 break;
6251
6252         case RQCS_TRANSPORT_ERROR:
6253                 isp_xs_prt(isp, xs,  ISP_LOGERR, "Transport Error");
6254                 break;
6255
6256         case RQCS_RESET_OCCURRED:
6257                 isp_xs_prt(isp, xs, ISP_LOGWARN, "reset destroyed command");
6258                 FCPARAM(isp, chan)->sendmarker = 1;
6259                 if (XS_NOERR(xs)) {
6260                         XS_SETERR(xs, HBA_BUSRESET);
6261                 }
6262                 return;
6263
6264         case RQCS_ABORTED:
6265                 isp_xs_prt(isp, xs, ISP_LOGERR, "Command Aborted");
6266                 FCPARAM(isp, chan)->sendmarker = 1;
6267                 if (XS_NOERR(xs)) {
6268                         XS_SETERR(xs, HBA_ABORTED);
6269                 }
6270                 return;
6271
6272         case RQCS_TIMEOUT:
6273                 isp_xs_prt(isp, xs, ISP_LOGWARN, "Command Timed Out");
6274                 if (XS_NOERR(xs)) {
6275                         XS_SETERR(xs, HBA_CMDTIMEOUT);
6276                 }
6277                 return;
6278
6279         case RQCS_DATA_OVERRUN:
6280                 XS_SET_RESID(xs, sp->req_resid);
6281                 isp_xs_prt(isp, xs, ISP_LOGERR, "Data Overrun");
6282                 if (XS_NOERR(xs)) {
6283                         XS_SETERR(xs, HBA_DATAOVR);
6284                 }
6285                 return;
6286
6287         case RQCS_24XX_DRE:     /* data reassembly error */
6288                 isp_prt(isp, ISP_LOGERR, "Chan %d data reassembly error for target %d", chan, XS_TGT(xs));
6289                 if (XS_NOERR(xs)) {
6290                         XS_SETERR(xs, HBA_ABORTED);
6291                 }
6292                 *rp = XS_XFRLEN(xs);
6293                 return;
6294
6295         case RQCS_24XX_TABORT:  /* aborted by target */
6296                 isp_prt(isp, ISP_LOGERR, "Chan %d target %d sent ABTS", chan, XS_TGT(xs));
6297                 if (XS_NOERR(xs)) {
6298                         XS_SETERR(xs, HBA_ABORTED);
6299                 }
6300                 return;
6301
6302         case RQCS_DATA_UNDERRUN:
6303                 ru_marked = (sp->req_scsi_status & RQCS_RU) != 0;
6304                 /*
6305                  * We can get an underrun w/o things being marked
6306                  * if we got a non-zero status.
6307                  */
6308                 sv_marked = (sp->req_scsi_status & (RQCS_SV|RQCS_RV)) != 0;
6309                 if ((ru_marked == 0 && sv_marked == 0) ||
6310                     (sp->req_resid > XS_XFRLEN(xs))) {
6311                         isp_xs_prt(isp, xs, ISP_LOGWARN, bun, XS_XFRLEN(xs), sp->req_resid, (ru_marked)? "marked" : "not marked");
6312                         if (XS_NOERR(xs)) {
6313                                 XS_SETERR(xs, HBA_BOTCH);
6314                         }
6315                         return;
6316                 }
6317                 XS_SET_RESID(xs, sp->req_resid);
6318                 isp_xs_prt(isp, xs, ISP_LOG_WARN1, "Data Underrun (%d) for command 0x%x", sp->req_resid, XS_CDBP(xs)[0] & 0xff);
6319                 if (XS_NOERR(xs)) {
6320                         XS_SETERR(xs, HBA_NOERROR);
6321                 }
6322                 return;
6323
6324         case RQCS_PORT_UNAVAILABLE:
6325                 /*
6326                  * No such port on the loop. Moral equivalent of SELTIMEO
6327                  */
6328         case RQCS_PORT_LOGGED_OUT:
6329         {
6330                 const char *reason;
6331                 uint8_t sts = sp->req_completion_status & 0xff;
6332
6333                 /*
6334                  * It was there (maybe)- treat as a selection timeout.
6335                  */
6336                 if (sts == RQCS_PORT_UNAVAILABLE) {
6337                         reason = "unavailable";
6338                 } else {
6339                         reason = "logout";
6340                 }
6341
6342                 isp_prt(isp, ISP_LOGINFO, "Chan %d port %s for target %d",
6343                     chan, reason, XS_TGT(xs));
6344
6345                 /*
6346                  * There is no MBOX_INIT_LIP for the 24XX.
6347                  */
6348                 if (XS_NOERR(xs)) {
6349                         XS_SETERR(xs, HBA_SELTIMEOUT);
6350                 }
6351                 return;
6352         }
6353         case RQCS_PORT_CHANGED:
6354                 isp_prt(isp, ISP_LOGWARN, "port changed for target %d chan %d", XS_TGT(xs), chan);
6355                 if (XS_NOERR(xs)) {
6356                         XS_SETERR(xs, HBA_SELTIMEOUT);
6357                 }
6358                 return;
6359
6360
6361         case RQCS_24XX_ENOMEM:  /* f/w resource unavailable */
6362                 isp_prt(isp, ISP_LOGWARN, "f/w resource unavailable for target %d chan %d", XS_TGT(xs), chan);
6363                 if (XS_NOERR(xs)) {
6364                         *XS_STSP(xs) = SCSI_BUSY;
6365                         XS_SETERR(xs, HBA_TGTBSY);
6366                 }
6367                 return;
6368
6369         case RQCS_24XX_TMO:     /* task management overrun */
6370                 isp_prt(isp, ISP_LOGWARN, "command for target %d overlapped task management for chan %d", XS_TGT(xs), chan);
6371                 if (XS_NOERR(xs)) {
6372                         *XS_STSP(xs) = SCSI_BUSY;
6373                         XS_SETERR(xs, HBA_TGTBSY);
6374                 }
6375                 return;
6376
6377         default:
6378                 isp_prt(isp, ISP_LOGERR, "Unknown Completion Status 0x%x on chan %d", sp->req_completion_status, chan);
6379                 break;
6380         }
6381         if (XS_NOERR(xs)) {
6382                 XS_SETERR(xs, HBA_BOTCH);
6383         }
6384 }
6385
6386 static void
6387 isp_fastpost_complete(ispsoftc_t *isp, uint32_t fph)
6388 {
6389         XS_T *xs;
6390
6391         if (fph == 0) {
6392                 return;
6393         }
6394         xs = isp_find_xs(isp, fph);
6395         if (xs == NULL) {
6396                 isp_prt(isp, ISP_LOGWARN,
6397                     "Command for fast post handle 0x%x not found", fph);
6398                 return;
6399         }
6400         isp_destroy_handle(isp, fph);
6401
6402         /*
6403          * Since we don't have a result queue entry item,
6404          * we must believe that SCSI status is zero and
6405          * that all data transferred.
6406          */
6407         XS_SET_RESID(xs, 0);
6408         *XS_STSP(xs) = SCSI_GOOD;
6409         if (XS_XFRLEN(xs)) {
6410                 ISP_DMAFREE(isp, xs, fph);
6411         }
6412         if (isp->isp_nactive) {
6413                 isp->isp_nactive--;
6414         }
6415         isp->isp_fphccmplt++;
6416         isp_done(xs);
6417 }
6418
6419 static int
6420 isp_mbox_continue(ispsoftc_t *isp)
6421 {
6422         mbreg_t mbs;
6423         uint16_t *ptr;
6424         uint32_t offset;
6425
6426         switch (isp->isp_lastmbxcmd) {
6427         case MBOX_WRITE_RAM_WORD:
6428         case MBOX_READ_RAM_WORD:
6429         case MBOX_WRITE_RAM_WORD_EXTENDED:
6430         case MBOX_READ_RAM_WORD_EXTENDED:
6431                 break;
6432         default:
6433                 return (1);
6434         }
6435         if (isp->isp_mboxtmp[0] != MBOX_COMMAND_COMPLETE) {
6436                 isp->isp_mbxwrk0 = 0;
6437                 return (-1);
6438         }
6439
6440         /*
6441          * Clear the previous interrupt.
6442          */
6443         if (IS_24XX(isp)) {
6444                 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
6445         } else {
6446                 ISP_WRITE(isp, HCCR, HCCR_CMD_CLEAR_RISC_INT);
6447                 ISP_WRITE(isp, BIU_SEMA, 0);
6448         }
6449
6450         /*
6451          * Continue with next word.
6452          */
6453         ISP_MEMZERO(&mbs, sizeof (mbs));
6454         ptr = isp->isp_mbxworkp;
6455         switch (isp->isp_lastmbxcmd) {
6456         case MBOX_WRITE_RAM_WORD:
6457                 mbs.param[1] = isp->isp_mbxwrk1++;
6458                 mbs.param[2] = *ptr++;
6459                 break;
6460         case MBOX_READ_RAM_WORD:
6461                 *ptr++ = isp->isp_mboxtmp[2];
6462                 mbs.param[1] = isp->isp_mbxwrk1++;
6463                 break;
6464         case MBOX_WRITE_RAM_WORD_EXTENDED:
6465                 if (IS_24XX(isp)) {
6466                         uint32_t *lptr = (uint32_t *)ptr;
6467                         mbs.param[2] = lptr[0];
6468                         mbs.param[3] = lptr[0] >> 16;
6469                         lptr++;
6470                         ptr = (uint16_t *)lptr;
6471                 } else {
6472                         mbs.param[2] = *ptr++;
6473                 }
6474                 offset = isp->isp_mbxwrk1;
6475                 offset |= isp->isp_mbxwrk8 << 16;
6476                 mbs.param[1] = offset;
6477                 mbs.param[8] = offset >> 16;
6478                 offset++;
6479                 isp->isp_mbxwrk1 = offset;
6480                 isp->isp_mbxwrk8 = offset >> 16;
6481                 break;
6482         case MBOX_READ_RAM_WORD_EXTENDED:
6483                 if (IS_24XX(isp)) {
6484                         uint32_t *lptr = (uint32_t *)ptr;
6485                         uint32_t val = isp->isp_mboxtmp[2];
6486                         val |= (isp->isp_mboxtmp[3]) << 16;
6487                         *lptr++ = val;
6488                         ptr = (uint16_t *)lptr;
6489                 } else {
6490                         *ptr++ = isp->isp_mboxtmp[2];
6491                 }
6492                 offset = isp->isp_mbxwrk1;
6493                 offset |= isp->isp_mbxwrk8 << 16;
6494                 mbs.param[1] = offset;
6495                 mbs.param[8] = offset >> 16;
6496                 offset++;
6497                 isp->isp_mbxwrk1 = offset;
6498                 isp->isp_mbxwrk8 = offset >> 16;
6499                 break;
6500         }
6501         isp->isp_mbxworkp = ptr;
6502         isp->isp_mbxwrk0--;
6503         mbs.param[0] = isp->isp_lastmbxcmd;
6504         mbs.logval = MBLOGALL;
6505         isp_mboxcmd_qnw(isp, &mbs, 0);
6506         return (0);
6507 }
6508
6509 #define ISP_SCSI_IBITS(op)              (mbpscsi[((op)<<1)])
6510 #define ISP_SCSI_OBITS(op)              (mbpscsi[((op)<<1) + 1])
6511 #define ISP_SCSI_OPMAP(in, out)         in, out
6512 static const uint8_t mbpscsi[] = {
6513         ISP_SCSI_OPMAP(0x01, 0x01),     /* 0x00: MBOX_NO_OP */
6514         ISP_SCSI_OPMAP(0x1f, 0x01),     /* 0x01: MBOX_LOAD_RAM */
6515         ISP_SCSI_OPMAP(0x03, 0x01),     /* 0x02: MBOX_EXEC_FIRMWARE */
6516         ISP_SCSI_OPMAP(0x1f, 0x01),     /* 0x03: MBOX_DUMP_RAM */
6517         ISP_SCSI_OPMAP(0x07, 0x07),     /* 0x04: MBOX_WRITE_RAM_WORD */
6518         ISP_SCSI_OPMAP(0x03, 0x07),     /* 0x05: MBOX_READ_RAM_WORD */
6519         ISP_SCSI_OPMAP(0x3f, 0x3f),     /* 0x06: MBOX_MAILBOX_REG_TEST */
6520         ISP_SCSI_OPMAP(0x07, 0x07),     /* 0x07: MBOX_VERIFY_CHECKSUM   */
6521         ISP_SCSI_OPMAP(0x01, 0x0f),     /* 0x08: MBOX_ABOUT_FIRMWARE */
6522         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x09: */
6523         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x0a: */
6524         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x0b: */
6525         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x0c: */
6526         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x0d: */
6527         ISP_SCSI_OPMAP(0x01, 0x05),     /* 0x0e: MBOX_CHECK_FIRMWARE */
6528         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x0f: */
6529         ISP_SCSI_OPMAP(0x1f, 0x1f),     /* 0x10: MBOX_INIT_REQ_QUEUE */
6530         ISP_SCSI_OPMAP(0x3f, 0x3f),     /* 0x11: MBOX_INIT_RES_QUEUE */
6531         ISP_SCSI_OPMAP(0x0f, 0x0f),     /* 0x12: MBOX_EXECUTE_IOCB */
6532         ISP_SCSI_OPMAP(0x03, 0x03),     /* 0x13: MBOX_WAKE_UP   */
6533         ISP_SCSI_OPMAP(0x01, 0x3f),     /* 0x14: MBOX_STOP_FIRMWARE */
6534         ISP_SCSI_OPMAP(0x0f, 0x0f),     /* 0x15: MBOX_ABORT */
6535         ISP_SCSI_OPMAP(0x03, 0x03),     /* 0x16: MBOX_ABORT_DEVICE */
6536         ISP_SCSI_OPMAP(0x07, 0x07),     /* 0x17: MBOX_ABORT_TARGET */
6537         ISP_SCSI_OPMAP(0x07, 0x07),     /* 0x18: MBOX_BUS_RESET */
6538         ISP_SCSI_OPMAP(0x03, 0x07),     /* 0x19: MBOX_STOP_QUEUE */
6539         ISP_SCSI_OPMAP(0x03, 0x07),     /* 0x1a: MBOX_START_QUEUE */
6540         ISP_SCSI_OPMAP(0x03, 0x07),     /* 0x1b: MBOX_SINGLE_STEP_QUEUE */
6541         ISP_SCSI_OPMAP(0x03, 0x07),     /* 0x1c: MBOX_ABORT_QUEUE */
6542         ISP_SCSI_OPMAP(0x03, 0x4f),     /* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
6543         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x1e: */
6544         ISP_SCSI_OPMAP(0x01, 0x07),     /* 0x1f: MBOX_GET_FIRMWARE_STATUS */
6545         ISP_SCSI_OPMAP(0x01, 0x07),     /* 0x20: MBOX_GET_INIT_SCSI_ID */
6546         ISP_SCSI_OPMAP(0x01, 0x07),     /* 0x21: MBOX_GET_SELECT_TIMEOUT */
6547         ISP_SCSI_OPMAP(0x01, 0xc7),     /* 0x22: MBOX_GET_RETRY_COUNT   */
6548         ISP_SCSI_OPMAP(0x01, 0x07),     /* 0x23: MBOX_GET_TAG_AGE_LIMIT */
6549         ISP_SCSI_OPMAP(0x01, 0x03),     /* 0x24: MBOX_GET_CLOCK_RATE */
6550         ISP_SCSI_OPMAP(0x01, 0x07),     /* 0x25: MBOX_GET_ACT_NEG_STATE */
6551         ISP_SCSI_OPMAP(0x01, 0x07),     /* 0x26: MBOX_GET_ASYNC_DATA_SETUP_TIME */
6552         ISP_SCSI_OPMAP(0x01, 0x07),     /* 0x27: MBOX_GET_PCI_PARAMS */
6553         ISP_SCSI_OPMAP(0x03, 0x4f),     /* 0x28: MBOX_GET_TARGET_PARAMS */
6554         ISP_SCSI_OPMAP(0x03, 0x0f),     /* 0x29: MBOX_GET_DEV_QUEUE_PARAMS */
6555         ISP_SCSI_OPMAP(0x01, 0x07),     /* 0x2a: MBOX_GET_RESET_DELAY_PARAMS */
6556         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x2b: */
6557         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x2c: */
6558         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x2d: */
6559         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x2e: */
6560         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x2f: */
6561         ISP_SCSI_OPMAP(0x03, 0x03),     /* 0x30: MBOX_SET_INIT_SCSI_ID */
6562         ISP_SCSI_OPMAP(0x07, 0x07),     /* 0x31: MBOX_SET_SELECT_TIMEOUT */
6563         ISP_SCSI_OPMAP(0xc7, 0xc7),     /* 0x32: MBOX_SET_RETRY_COUNT   */
6564         ISP_SCSI_OPMAP(0x07, 0x07),     /* 0x33: MBOX_SET_TAG_AGE_LIMIT */
6565         ISP_SCSI_OPMAP(0x03, 0x03),     /* 0x34: MBOX_SET_CLOCK_RATE */
6566         ISP_SCSI_OPMAP(0x07, 0x07),     /* 0x35: MBOX_SET_ACT_NEG_STATE */
6567         ISP_SCSI_OPMAP(0x07, 0x07),     /* 0x36: MBOX_SET_ASYNC_DATA_SETUP_TIME */
6568         ISP_SCSI_OPMAP(0x07, 0x07),     /* 0x37: MBOX_SET_PCI_CONTROL_PARAMS */
6569         ISP_SCSI_OPMAP(0x4f, 0x4f),     /* 0x38: MBOX_SET_TARGET_PARAMS */
6570         ISP_SCSI_OPMAP(0x0f, 0x0f),     /* 0x39: MBOX_SET_DEV_QUEUE_PARAMS */
6571         ISP_SCSI_OPMAP(0x07, 0x07),     /* 0x3a: MBOX_SET_RESET_DELAY_PARAMS */
6572         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x3b: */
6573         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x3c: */
6574         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x3d: */
6575         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x3e: */
6576         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x3f: */
6577         ISP_SCSI_OPMAP(0x01, 0x03),     /* 0x40: MBOX_RETURN_BIOS_BLOCK_ADDR */
6578         ISP_SCSI_OPMAP(0x3f, 0x01),     /* 0x41: MBOX_WRITE_FOUR_RAM_WORDS */
6579         ISP_SCSI_OPMAP(0x03, 0x07),     /* 0x42: MBOX_EXEC_BIOS_IOCB */
6580         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x43: */
6581         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x44: */
6582         ISP_SCSI_OPMAP(0x03, 0x03),     /* 0x45: SET SYSTEM PARAMETER */
6583         ISP_SCSI_OPMAP(0x01, 0x03),     /* 0x46: GET SYSTEM PARAMETER */
6584         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x47: */
6585         ISP_SCSI_OPMAP(0x01, 0xcf),     /* 0x48: GET SCAM CONFIGURATION */
6586         ISP_SCSI_OPMAP(0xcf, 0xcf),     /* 0x49: SET SCAM CONFIGURATION */
6587         ISP_SCSI_OPMAP(0x03, 0x03),     /* 0x4a: MBOX_SET_FIRMWARE_FEATURES */
6588         ISP_SCSI_OPMAP(0x01, 0x03),     /* 0x4b: MBOX_GET_FIRMWARE_FEATURES */
6589         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x4c: */
6590         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x4d: */
6591         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x4e: */
6592         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x4f: */
6593         ISP_SCSI_OPMAP(0xdf, 0xdf),     /* 0x50: LOAD RAM A64 */
6594         ISP_SCSI_OPMAP(0xdf, 0xdf),     /* 0x51: DUMP RAM A64 */
6595         ISP_SCSI_OPMAP(0xdf, 0xff),     /* 0x52: INITIALIZE REQUEST QUEUE A64 */
6596         ISP_SCSI_OPMAP(0xef, 0xff),     /* 0x53: INITIALIZE RESPONSE QUEUE A64 */
6597         ISP_SCSI_OPMAP(0xcf, 0x01),     /* 0x54: EXECUCUTE COMMAND IOCB A64 */
6598         ISP_SCSI_OPMAP(0x07, 0x01),     /* 0x55: ENABLE TARGET MODE */
6599         ISP_SCSI_OPMAP(0x03, 0x0f),     /* 0x56: GET TARGET STATUS */
6600         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x57: */
6601         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x58: */
6602         ISP_SCSI_OPMAP(0x00, 0x00),     /* 0x59: */
6603         ISP_SCSI_OPMAP(0x03, 0x03),     /* 0x5a: SET DATA OVERRUN RECOVERY MODE */
6604         ISP_SCSI_OPMAP(0x01, 0x03),     /* 0x5b: GET DATA OVERRUN RECOVERY MODE */
6605         ISP_SCSI_OPMAP(0x0f, 0x0f),     /* 0x5c: SET HOST DATA */
6606         ISP_SCSI_OPMAP(0x01, 0x01)      /* 0x5d: GET NOST DATA */
6607 };
6608 #define MAX_SCSI_OPCODE 0x5d
6609
6610 static const char *scsi_mbcmd_names[] = {
6611         "NO-OP",
6612         "LOAD RAM",
6613         "EXEC FIRMWARE",
6614         "DUMP RAM",
6615         "WRITE RAM WORD",
6616         "READ RAM WORD",
6617         "MAILBOX REG TEST",
6618         "VERIFY CHECKSUM",
6619         "ABOUT FIRMWARE",
6620         NULL,
6621         NULL,
6622         NULL,
6623         NULL,
6624         NULL,
6625         "CHECK FIRMWARE",
6626         NULL,
6627         "INIT REQUEST QUEUE",
6628         "INIT RESULT QUEUE",
6629         "EXECUTE IOCB",
6630         "WAKE UP",
6631         "STOP FIRMWARE",
6632         "ABORT",
6633         "ABORT DEVICE",
6634         "ABORT TARGET",
6635         "BUS RESET",
6636         "STOP QUEUE",
6637         "START QUEUE",
6638         "SINGLE STEP QUEUE",
6639         "ABORT QUEUE",
6640         "GET DEV QUEUE STATUS",
6641         NULL,
6642         "GET FIRMWARE STATUS",
6643         "GET INIT SCSI ID",
6644         "GET SELECT TIMEOUT",
6645         "GET RETRY COUNT",
6646         "GET TAG AGE LIMIT",
6647         "GET CLOCK RATE",
6648         "GET ACT NEG STATE",
6649         "GET ASYNC DATA SETUP TIME",
6650         "GET PCI PARAMS",
6651         "GET TARGET PARAMS",
6652         "GET DEV QUEUE PARAMS",
6653         "GET RESET DELAY PARAMS",
6654         NULL,
6655         NULL,
6656         NULL,
6657         NULL,
6658         NULL,
6659         "SET INIT SCSI ID",
6660         "SET SELECT TIMEOUT",
6661         "SET RETRY COUNT",
6662         "SET TAG AGE LIMIT",
6663         "SET CLOCK RATE",
6664         "SET ACT NEG STATE",
6665         "SET ASYNC DATA SETUP TIME",
6666         "SET PCI CONTROL PARAMS",
6667         "SET TARGET PARAMS",
6668         "SET DEV QUEUE PARAMS",
6669         "SET RESET DELAY PARAMS",
6670         NULL,
6671         NULL,
6672         NULL,
6673         NULL,
6674         NULL,
6675         "RETURN BIOS BLOCK ADDR",
6676         "WRITE FOUR RAM WORDS",
6677         "EXEC BIOS IOCB",
6678         NULL,
6679         NULL,
6680         "SET SYSTEM PARAMETER",
6681         "GET SYSTEM PARAMETER",
6682         NULL,
6683         "GET SCAM CONFIGURATION",
6684         "SET SCAM CONFIGURATION",
6685         "SET FIRMWARE FEATURES",
6686         "GET FIRMWARE FEATURES",
6687         NULL,
6688         NULL,
6689         NULL,
6690         NULL,
6691         "LOAD RAM A64",
6692         "DUMP RAM A64",
6693         "INITIALIZE REQUEST QUEUE A64",
6694         "INITIALIZE RESPONSE QUEUE A64",
6695         "EXECUTE IOCB A64",
6696         "ENABLE TARGET MODE",
6697         "GET TARGET MODE STATE",
6698         NULL,
6699         NULL,
6700         NULL,
6701         "SET DATA OVERRUN RECOVERY MODE",
6702         "GET DATA OVERRUN RECOVERY MODE",
6703         "SET HOST DATA",
6704         "GET NOST DATA",
6705 };
6706
6707 #define ISP_FC_IBITS(op)        ((mbpfc[((op)<<3) + 0] << 24) | (mbpfc[((op)<<3) + 1] << 16) | (mbpfc[((op)<<3) + 2] << 8) | (mbpfc[((op)<<3) + 3]))
6708 #define ISP_FC_OBITS(op)        ((mbpfc[((op)<<3) + 4] << 24) | (mbpfc[((op)<<3) + 5] << 16) | (mbpfc[((op)<<3) + 6] << 8) | (mbpfc[((op)<<3) + 7]))
6709
6710 #define ISP_FC_OPMAP(in0, out0)                                                   0,   0,   0, in0,    0,    0,    0, out0
6711 #define ISP_FC_OPMAP_HALF(in1, in0, out1, out0)                                   0,   0, in1, in0,    0,    0, out1, out0
6712 #define ISP_FC_OPMAP_FULL(in3, in2, in1, in0, out3, out2, out1, out0)           in3, in2, in1, in0, out3, out2, out1, out0
6713 static const uint32_t mbpfc[] = {
6714         ISP_FC_OPMAP(0x01, 0x01),       /* 0x00: MBOX_NO_OP */
6715         ISP_FC_OPMAP(0x1f, 0x01),       /* 0x01: MBOX_LOAD_RAM */
6716         ISP_FC_OPMAP(0x0f, 0x01),       /* 0x02: MBOX_EXEC_FIRMWARE */
6717         ISP_FC_OPMAP(0xdf, 0x01),       /* 0x03: MBOX_DUMP_RAM */
6718         ISP_FC_OPMAP(0x07, 0x07),       /* 0x04: MBOX_WRITE_RAM_WORD */
6719         ISP_FC_OPMAP(0x03, 0x07),       /* 0x05: MBOX_READ_RAM_WORD */
6720         ISP_FC_OPMAP_FULL(0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff),      /* 0x06: MBOX_MAILBOX_REG_TEST */
6721         ISP_FC_OPMAP(0x07, 0x07),       /* 0x07: MBOX_VERIFY_CHECKSUM   */
6722         ISP_FC_OPMAP_FULL(0x0, 0x0, 0x0, 0x01, 0x0, 0x3, 0x80, 0x7f),   /* 0x08: MBOX_ABOUT_FIRMWARE */
6723         ISP_FC_OPMAP(0xdf, 0x01),       /* 0x09: MBOX_LOAD_RISC_RAM_2100 */
6724         ISP_FC_OPMAP(0xdf, 0x01),       /* 0x0a: DUMP RAM */
6725         ISP_FC_OPMAP_HALF(0x1, 0xff, 0x0, 0x01),        /* 0x0b: MBOX_LOAD_RISC_RAM */
6726         ISP_FC_OPMAP(0x00, 0x00),       /* 0x0c: */
6727         ISP_FC_OPMAP_HALF(0x1, 0x0f, 0x0, 0x01),        /* 0x0d: MBOX_WRITE_RAM_WORD_EXTENDED */
6728         ISP_FC_OPMAP(0x01, 0x05),       /* 0x0e: MBOX_CHECK_FIRMWARE */
6729         ISP_FC_OPMAP_HALF(0x1, 0x03, 0x0, 0x0d),        /* 0x0f: MBOX_READ_RAM_WORD_EXTENDED */
6730         ISP_FC_OPMAP(0x1f, 0x11),       /* 0x10: MBOX_INIT_REQ_QUEUE */
6731         ISP_FC_OPMAP(0x2f, 0x21),       /* 0x11: MBOX_INIT_RES_QUEUE */
6732         ISP_FC_OPMAP(0x0f, 0x01),       /* 0x12: MBOX_EXECUTE_IOCB */
6733         ISP_FC_OPMAP(0x03, 0x03),       /* 0x13: MBOX_WAKE_UP   */
6734         ISP_FC_OPMAP(0x01, 0xff),       /* 0x14: MBOX_STOP_FIRMWARE */
6735         ISP_FC_OPMAP(0x4f, 0x01),       /* 0x15: MBOX_ABORT */
6736         ISP_FC_OPMAP(0x07, 0x01),       /* 0x16: MBOX_ABORT_DEVICE */
6737         ISP_FC_OPMAP(0x07, 0x01),       /* 0x17: MBOX_ABORT_TARGET */
6738         ISP_FC_OPMAP(0x03, 0x03),       /* 0x18: MBOX_BUS_RESET */
6739         ISP_FC_OPMAP(0x07, 0x05),       /* 0x19: MBOX_STOP_QUEUE */
6740         ISP_FC_OPMAP(0x07, 0x05),       /* 0x1a: MBOX_START_QUEUE */
6741         ISP_FC_OPMAP(0x07, 0x05),       /* 0x1b: MBOX_SINGLE_STEP_QUEUE */
6742         ISP_FC_OPMAP(0x07, 0x05),       /* 0x1c: MBOX_ABORT_QUEUE */
6743         ISP_FC_OPMAP(0x07, 0x03),       /* 0x1d: MBOX_GET_DEV_QUEUE_STATUS */
6744         ISP_FC_OPMAP(0x00, 0x00),       /* 0x1e: */
6745         ISP_FC_OPMAP(0x01, 0x07),       /* 0x1f: MBOX_GET_FIRMWARE_STATUS */
6746         ISP_FC_OPMAP_HALF(0x2, 0x01, 0x7e, 0xcf),       /* 0x20: MBOX_GET_LOOP_ID */
6747         ISP_FC_OPMAP(0x00, 0x00),       /* 0x21: */
6748         ISP_FC_OPMAP(0x01, 0x07),       /* 0x22: MBOX_GET_RETRY_COUNT   */
6749         ISP_FC_OPMAP(0x00, 0x00),       /* 0x23: */
6750         ISP_FC_OPMAP(0x00, 0x00),       /* 0x24: */
6751         ISP_FC_OPMAP(0x00, 0x00),       /* 0x25: */
6752         ISP_FC_OPMAP(0x00, 0x00),       /* 0x26: */
6753         ISP_FC_OPMAP(0x00, 0x00),       /* 0x27: */
6754         ISP_FC_OPMAP(0x01, 0x03),       /* 0x28: MBOX_GET_FIRMWARE_OPTIONS */
6755         ISP_FC_OPMAP(0x03, 0x07),       /* 0x29: MBOX_GET_PORT_QUEUE_PARAMS */
6756         ISP_FC_OPMAP(0x00, 0x00),       /* 0x2a: */
6757         ISP_FC_OPMAP(0x00, 0x00),       /* 0x2b: */
6758         ISP_FC_OPMAP(0x00, 0x00),       /* 0x2c: */
6759         ISP_FC_OPMAP(0x00, 0x00),       /* 0x2d: */
6760         ISP_FC_OPMAP(0x00, 0x00),       /* 0x2e: */
6761         ISP_FC_OPMAP(0x00, 0x00),       /* 0x2f: */
6762         ISP_FC_OPMAP(0x00, 0x00),       /* 0x30: */
6763         ISP_FC_OPMAP(0x00, 0x00),       /* 0x31: */
6764         ISP_FC_OPMAP(0x07, 0x07),       /* 0x32: MBOX_SET_RETRY_COUNT   */
6765         ISP_FC_OPMAP(0x00, 0x00),       /* 0x33: */
6766         ISP_FC_OPMAP(0x00, 0x00),       /* 0x34: */
6767         ISP_FC_OPMAP(0x00, 0x00),       /* 0x35: */
6768         ISP_FC_OPMAP(0x00, 0x00),       /* 0x36: */
6769         ISP_FC_OPMAP(0x00, 0x00),       /* 0x37: */
6770         ISP_FC_OPMAP(0x0f, 0x01),       /* 0x38: MBOX_SET_FIRMWARE_OPTIONS */
6771         ISP_FC_OPMAP(0x0f, 0x07),       /* 0x39: MBOX_SET_PORT_QUEUE_PARAMS */
6772         ISP_FC_OPMAP(0x00, 0x00),       /* 0x3a: */
6773         ISP_FC_OPMAP(0x00, 0x00),       /* 0x3b: */
6774         ISP_FC_OPMAP(0x00, 0x00),       /* 0x3c: */
6775         ISP_FC_OPMAP(0x00, 0x00),       /* 0x3d: */
6776         ISP_FC_OPMAP(0x00, 0x00),       /* 0x3e: */
6777         ISP_FC_OPMAP(0x00, 0x00),       /* 0x3f: */
6778         ISP_FC_OPMAP(0x03, 0x01),       /* 0x40: MBOX_LOOP_PORT_BYPASS */
6779         ISP_FC_OPMAP(0x03, 0x01),       /* 0x41: MBOX_LOOP_PORT_ENABLE */
6780         ISP_FC_OPMAP_HALF(0x0, 0x01, 0x3, 0xcf),        /* 0x42: MBOX_GET_RESOURCE_COUNT */
6781         ISP_FC_OPMAP(0x01, 0x01),       /* 0x43: MBOX_REQUEST_OFFLINE_MODE */
6782         ISP_FC_OPMAP(0x00, 0x00),       /* 0x44: */
6783         ISP_FC_OPMAP(0x00, 0x00),       /* 0x45: */
6784         ISP_FC_OPMAP(0x00, 0x00),       /* 0x46: */
6785         ISP_FC_OPMAP(0xcf, 0x03),       /* 0x47: GET PORT_DATABASE ENHANCED */
6786         ISP_FC_OPMAP(0xcf, 0x0f),       /* 0x48: MBOX_INIT_FIRMWARE_MULTI_ID */
6787         ISP_FC_OPMAP(0xcd, 0x01),       /* 0x49: MBOX_GET_VP_DATABASE */
6788         ISP_FC_OPMAP_HALF(0x2, 0xcd, 0x0, 0x01),        /* 0x4a: MBOX_GET_VP_DATABASE_ENTRY */
6789         ISP_FC_OPMAP(0x00, 0x00),       /* 0x4b: */
6790         ISP_FC_OPMAP(0x00, 0x00),       /* 0x4c: */
6791         ISP_FC_OPMAP(0x00, 0x00),       /* 0x4d: */
6792         ISP_FC_OPMAP(0x00, 0x00),       /* 0x4e: */
6793         ISP_FC_OPMAP(0x00, 0x00),       /* 0x4f: */
6794         ISP_FC_OPMAP(0x00, 0x00),       /* 0x50: */
6795         ISP_FC_OPMAP(0x00, 0x00),       /* 0x51: */
6796         ISP_FC_OPMAP(0x00, 0x00),       /* 0x52: */
6797         ISP_FC_OPMAP(0x00, 0x00),       /* 0x53: */
6798         ISP_FC_OPMAP(0xcf, 0x01),       /* 0x54: EXECUTE IOCB A64 */
6799         ISP_FC_OPMAP(0x00, 0x00),       /* 0x55: */
6800         ISP_FC_OPMAP(0x00, 0x00),       /* 0x56: */
6801         ISP_FC_OPMAP(0x00, 0x00),       /* 0x57: */
6802         ISP_FC_OPMAP(0x00, 0x00),       /* 0x58: */
6803         ISP_FC_OPMAP(0x00, 0x00),       /* 0x59: */
6804         ISP_FC_OPMAP(0x00, 0x00),       /* 0x5a: */
6805         ISP_FC_OPMAP(0x03, 0x01),       /* 0x5b: MBOX_DRIVER_HEARTBEAT */
6806         ISP_FC_OPMAP(0xcf, 0x01),       /* 0x5c: MBOX_FW_HEARTBEAT */
6807         ISP_FC_OPMAP(0x07, 0x1f),       /* 0x5d: MBOX_GET_SET_DATA_RATE */
6808         ISP_FC_OPMAP(0x00, 0x00),       /* 0x5e: */
6809         ISP_FC_OPMAP(0x00, 0x00),       /* 0x5f: */
6810         ISP_FC_OPMAP(0xcf, 0x0f),       /* 0x60: MBOX_INIT_FIRMWARE */
6811         ISP_FC_OPMAP(0x00, 0x00),       /* 0x61: */
6812         ISP_FC_OPMAP(0x01, 0x01),       /* 0x62: MBOX_INIT_LIP */
6813         ISP_FC_OPMAP(0xcd, 0x03),       /* 0x63: MBOX_GET_FC_AL_POSITION_MAP */
6814         ISP_FC_OPMAP(0xcf, 0x01),       /* 0x64: MBOX_GET_PORT_DB */
6815         ISP_FC_OPMAP(0x07, 0x01),       /* 0x65: MBOX_CLEAR_ACA */
6816         ISP_FC_OPMAP(0x07, 0x01),       /* 0x66: MBOX_TARGET_RESET */
6817         ISP_FC_OPMAP(0x07, 0x01),       /* 0x67: MBOX_CLEAR_TASK_SET */
6818         ISP_FC_OPMAP(0x07, 0x01),       /* 0x68: MBOX_ABORT_TASK_SET */
6819         ISP_FC_OPMAP(0x01, 0x07),       /* 0x69: MBOX_GET_FW_STATE */
6820         ISP_FC_OPMAP_HALF(0x6, 0x03, 0x0, 0xcf),        /* 0x6a: MBOX_GET_PORT_NAME */
6821         ISP_FC_OPMAP(0xcf, 0x01),       /* 0x6b: MBOX_GET_LINK_STATUS */
6822         ISP_FC_OPMAP(0x0f, 0x01),       /* 0x6c: MBOX_INIT_LIP_RESET */
6823         ISP_FC_OPMAP(0x00, 0x00),       /* 0x6d: */
6824         ISP_FC_OPMAP(0xcf, 0x03),       /* 0x6e: MBOX_SEND_SNS */
6825         ISP_FC_OPMAP(0x0f, 0x07),       /* 0x6f: MBOX_FABRIC_LOGIN */
6826         ISP_FC_OPMAP(0x03, 0x01),       /* 0x70: MBOX_SEND_CHANGE_REQUEST */
6827         ISP_FC_OPMAP(0x03, 0x03),       /* 0x71: MBOX_FABRIC_LOGOUT */
6828         ISP_FC_OPMAP(0x0f, 0x0f),       /* 0x72: MBOX_INIT_LIP_LOGIN */
6829         ISP_FC_OPMAP(0x00, 0x00),       /* 0x73: */
6830         ISP_FC_OPMAP(0x07, 0x01),       /* 0x74: LOGIN LOOP PORT */
6831         ISP_FC_OPMAP_HALF(0x03, 0xcf, 0x00, 0x07),      /* 0x75: GET PORT/NODE NAME LIST */
6832         ISP_FC_OPMAP(0x4f, 0x01),       /* 0x76: SET VENDOR ID */
6833         ISP_FC_OPMAP(0xcd, 0x01),       /* 0x77: INITIALIZE IP MAILBOX */
6834         ISP_FC_OPMAP(0x00, 0x00),       /* 0x78: */
6835         ISP_FC_OPMAP(0x00, 0x00),       /* 0x79: */
6836         ISP_FC_OPMAP(0x00, 0x00),       /* 0x7a: */
6837         ISP_FC_OPMAP(0x00, 0x00),       /* 0x7b: */
6838         ISP_FC_OPMAP_HALF(0x03, 0x4f, 0x00, 0x07),      /* 0x7c: Get ID List */
6839         ISP_FC_OPMAP(0xcf, 0x01),       /* 0x7d: SEND LFA */
6840         ISP_FC_OPMAP(0x0f, 0x01)        /* 0x7e: LUN RESET */
6841 };
6842 #define MAX_FC_OPCODE   0x7e
6843 /*
6844  * Footnotes
6845  *
6846  * (1): this sets bits 21..16 in mailbox register #8, which we nominally
6847  *      do not access at this time in the core driver. The caller is
6848  *      responsible for setting this register first (Gross!). The assumption
6849  *      is that we won't overflow.
6850  */
6851
6852 static const char *fc_mbcmd_names[] = {
6853         "NO-OP",
6854         "LOAD RAM",
6855         "EXEC FIRMWARE",
6856         "DUMP RAM",
6857         "WRITE RAM WORD",
6858         "READ RAM WORD",
6859         "MAILBOX REG TEST",
6860         "VERIFY CHECKSUM",
6861         "ABOUT FIRMWARE",
6862         "LOAD RAM (2100)",
6863         "DUMP RAM",
6864         "LOAD RISC RAM",
6865         NULL,
6866         "WRITE RAM WORD EXTENDED",
6867         "CHECK FIRMWARE",
6868         "READ RAM WORD EXTENDED",
6869         "INIT REQUEST QUEUE",
6870         "INIT RESULT QUEUE",
6871         "EXECUTE IOCB",
6872         "WAKE UP",
6873         "STOP FIRMWARE",
6874         "ABORT",
6875         "ABORT DEVICE",
6876         "ABORT TARGET",
6877         "BUS RESET",
6878         "STOP QUEUE",
6879         "START QUEUE",
6880         "SINGLE STEP QUEUE",
6881         "ABORT QUEUE",
6882         "GET DEV QUEUE STATUS",
6883         NULL,
6884         "GET FIRMWARE STATUS",
6885         "GET LOOP ID",
6886         NULL,
6887         "GET RETRY COUNT",
6888         NULL,
6889         NULL,
6890         NULL,
6891         NULL,
6892         NULL,
6893         "GET FIRMWARE OPTIONS",
6894         "GET PORT QUEUE PARAMS",
6895         NULL,
6896         NULL,
6897         NULL,
6898         NULL,
6899         NULL,
6900         NULL,
6901         NULL,
6902         NULL,
6903         "SET RETRY COUNT",
6904         NULL,
6905         NULL,
6906         NULL,
6907         NULL,
6908         NULL,
6909         "SET FIRMWARE OPTIONS",
6910         "SET PORT QUEUE PARAMS",
6911         NULL,
6912         NULL,
6913         NULL,
6914         NULL,
6915         NULL,
6916         NULL,
6917         "LOOP PORT BYPASS",
6918         "LOOP PORT ENABLE",
6919         "GET RESOURCE COUNT",
6920         "REQUEST NON PARTICIPATING MODE",
6921         NULL,
6922         NULL,
6923         NULL,
6924         "GET PORT DATABASE ENHANCED",
6925         "INIT FIRMWARE MULTI ID",
6926         "GET VP DATABASE",
6927         "GET VP DATABASE ENTRY",
6928         NULL,
6929         NULL,
6930         NULL,
6931         NULL,
6932         NULL,
6933         NULL,
6934         NULL,
6935         NULL,
6936         NULL,
6937         "EXECUTE IOCB A64",
6938         NULL,
6939         NULL,
6940         NULL,
6941         NULL,
6942         NULL,
6943         NULL,
6944         "DRIVER HEARTBEAT",
6945         NULL,
6946         "GET/SET DATA RATE",
6947         NULL,
6948         NULL,
6949         "INIT FIRMWARE",
6950         NULL,
6951         "INIT LIP",
6952         "GET FC-AL POSITION MAP",
6953         "GET PORT DATABASE",
6954         "CLEAR ACA",
6955         "TARGET RESET",
6956         "CLEAR TASK SET",
6957         "ABORT TASK SET",
6958         "GET FW STATE",
6959         "GET PORT NAME",
6960         "GET LINK STATUS",
6961         "INIT LIP RESET",
6962         NULL,
6963         "SEND SNS",
6964         "FABRIC LOGIN",
6965         "SEND CHANGE REQUEST",
6966         "FABRIC LOGOUT",
6967         "INIT LIP LOGIN",
6968         NULL,
6969         "LOGIN LOOP PORT",
6970         "GET PORT/NODE NAME LIST",
6971         "SET VENDOR ID",
6972         "INITIALIZE IP MAILBOX",
6973         NULL,
6974         NULL,
6975         NULL,
6976         NULL,
6977         "Get ID List",
6978         "SEND LFA",
6979         "Lun RESET"
6980 };
6981
6982 static void
6983 isp_mboxcmd_qnw(ispsoftc_t *isp, mbreg_t *mbp, int nodelay)
6984 {
6985         unsigned int ibits, obits, box, opcode;
6986
6987         opcode = mbp->param[0];
6988         if (IS_FC(isp)) {
6989                 ibits = ISP_FC_IBITS(opcode);
6990                 obits = ISP_FC_OBITS(opcode);
6991         } else {
6992                 ibits = ISP_SCSI_IBITS(opcode);
6993                 obits = ISP_SCSI_OBITS(opcode);
6994         }
6995         ibits |= mbp->ibits;
6996         obits |= mbp->obits;
6997         for (box = 0; box < ISP_NMBOX(isp); box++) {
6998                 if (ibits & (1 << box)) {
6999                         ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
7000                 }
7001                 if (nodelay == 0) {
7002                         isp->isp_mboxtmp[box] = mbp->param[box] = 0;
7003                 }
7004         }
7005         if (nodelay == 0) {
7006                 isp->isp_lastmbxcmd = opcode;
7007                 isp->isp_obits = obits;
7008                 isp->isp_mboxbsy = 1;
7009         }
7010         if (IS_24XX(isp)) {
7011                 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_SET_HOST_INT);
7012         } else {
7013                 ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
7014         }
7015         /*
7016          * Oddly enough, if we're not delaying for an answer,
7017          * delay a bit to give the f/w a chance to pick up the
7018          * command.
7019          */
7020         if (nodelay) {
7021                 ISP_DELAY(1000);
7022         }
7023 }
7024
7025 static void
7026 isp_mboxcmd(ispsoftc_t *isp, mbreg_t *mbp)
7027 {
7028         const char *cname, *xname, *sname;
7029         char tname[16], mname[16];
7030         unsigned int ibits, obits, box, opcode;
7031
7032         opcode = mbp->param[0];
7033         if (IS_FC(isp)) {
7034                 if (opcode > MAX_FC_OPCODE) {
7035                         mbp->param[0] = MBOX_INVALID_COMMAND;
7036                         isp_prt(isp, ISP_LOGERR, "Unknown Command 0x%x", opcode);
7037                         return;
7038                 }
7039                 cname = fc_mbcmd_names[opcode];
7040                 ibits = ISP_FC_IBITS(opcode);
7041                 obits = ISP_FC_OBITS(opcode);
7042         } else {
7043                 if (opcode > MAX_SCSI_OPCODE) {
7044                         mbp->param[0] = MBOX_INVALID_COMMAND;
7045                         isp_prt(isp, ISP_LOGERR, "Unknown Command 0x%x", opcode);
7046                         return;
7047                 }
7048                 cname = scsi_mbcmd_names[opcode];
7049                 ibits = ISP_SCSI_IBITS(opcode);
7050                 obits = ISP_SCSI_OBITS(opcode);
7051         }
7052         if (cname == NULL) {
7053                 cname = tname;
7054                 ISP_SNPRINTF(tname, sizeof tname, "opcode %x", opcode);
7055         }
7056         isp_prt(isp, ISP_LOGDEBUG3, "Mailbox Command '%s'", cname);
7057
7058         /*
7059          * Pick up any additional bits that the caller might have set.
7060          */
7061         ibits |= mbp->ibits;
7062         obits |= mbp->obits;
7063
7064         /*
7065          * Mask any bits that the caller wants us to mask
7066          */
7067         ibits &= mbp->ibitm;
7068         obits &= mbp->obitm;
7069
7070
7071         if (ibits == 0 && obits == 0) {
7072                 mbp->param[0] = MBOX_COMMAND_PARAM_ERROR;
7073                 isp_prt(isp, ISP_LOGERR, "no parameters for 0x%x", opcode);
7074                 return;
7075         }
7076
7077         /*
7078          * Get exclusive usage of mailbox registers.
7079          */
7080         if (MBOX_ACQUIRE(isp)) {
7081                 mbp->param[0] = MBOX_REGS_BUSY;
7082                 goto out;
7083         }
7084
7085         for (box = 0; box < ISP_NMBOX(isp); box++) {
7086                 if (ibits & (1 << box)) {
7087                         isp_prt(isp, ISP_LOGDEBUG3, "IN mbox %d = 0x%04x", box,
7088                             mbp->param[box]);
7089                         ISP_WRITE(isp, MBOX_OFF(box), mbp->param[box]);
7090                 }
7091                 isp->isp_mboxtmp[box] = mbp->param[box] = 0;
7092         }
7093
7094         isp->isp_lastmbxcmd = opcode;
7095
7096         /*
7097          * We assume that we can't overwrite a previous command.
7098          */
7099         isp->isp_obits = obits;
7100         isp->isp_mboxbsy = 1;
7101
7102         /*
7103          * Set Host Interrupt condition so that RISC will pick up mailbox regs.
7104          */
7105         if (IS_24XX(isp)) {
7106                 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_SET_HOST_INT);
7107         } else {
7108                 ISP_WRITE(isp, HCCR, HCCR_CMD_SET_HOST_INT);
7109         }
7110
7111         /*
7112          * While we haven't finished the command, spin our wheels here.
7113          */
7114         MBOX_WAIT_COMPLETE(isp, mbp);
7115
7116         /*
7117          * Did the command time out?
7118          */
7119         if (mbp->param[0] == MBOX_TIMEOUT) {
7120                 isp->isp_mboxbsy = 0;
7121                 MBOX_RELEASE(isp);
7122                 goto out;
7123         }
7124
7125         /*
7126          * Copy back output registers.
7127          */
7128         for (box = 0; box < ISP_NMBOX(isp); box++) {
7129                 if (obits & (1 << box)) {
7130                         mbp->param[box] = isp->isp_mboxtmp[box];
7131                         isp_prt(isp, ISP_LOGDEBUG3, "OUT mbox %d = 0x%04x", box,
7132                             mbp->param[box]);
7133                 }
7134         }
7135
7136         isp->isp_mboxbsy = 0;
7137         MBOX_RELEASE(isp);
7138 out:
7139         if (mbp->logval == 0 || mbp->param[0] == MBOX_COMMAND_COMPLETE)
7140                 return;
7141
7142         if ((mbp->param[0] & 0xbfe0) == 0 &&
7143             (mbp->logval & MBLOGMASK(mbp->param[0])) == 0)
7144                 return;
7145
7146         xname = NULL;
7147         sname = "";
7148         switch (mbp->param[0]) {
7149         case MBOX_INVALID_COMMAND:
7150                 xname = "INVALID COMMAND";
7151                 break;
7152         case MBOX_HOST_INTERFACE_ERROR:
7153                 xname = "HOST INTERFACE ERROR";
7154                 break;
7155         case MBOX_TEST_FAILED:
7156                 xname = "TEST FAILED";
7157                 break;
7158         case MBOX_COMMAND_ERROR:
7159                 xname = "COMMAND ERROR";
7160                 ISP_SNPRINTF(mname, sizeof(mname), " subcode 0x%x",
7161                     mbp->param[1]);
7162                 sname = mname;
7163                 break;
7164         case MBOX_COMMAND_PARAM_ERROR:
7165                 xname = "COMMAND PARAMETER ERROR";
7166                 break;
7167         case MBOX_PORT_ID_USED:
7168                 xname = "PORT ID ALREADY IN USE";
7169                 break;
7170         case MBOX_LOOP_ID_USED:
7171                 xname = "LOOP ID ALREADY IN USE";
7172                 break;
7173         case MBOX_ALL_IDS_USED:
7174                 xname = "ALL LOOP IDS IN USE";
7175                 break;
7176         case MBOX_NOT_LOGGED_IN:
7177                 xname = "NOT LOGGED IN";
7178                 break;
7179         case MBOX_LINK_DOWN_ERROR:
7180                 xname = "LINK DOWN ERROR";
7181                 break;
7182         case MBOX_LOOPBACK_ERROR:
7183                 xname = "LOOPBACK ERROR";
7184                 break;
7185         case MBOX_CHECKSUM_ERROR:
7186                 xname = "CHECKSUM ERROR";
7187                 break;
7188         case MBOX_INVALID_PRODUCT_KEY:
7189                 xname = "INVALID PRODUCT KEY";
7190                 break;
7191         case MBOX_REGS_BUSY:
7192                 xname = "REGISTERS BUSY";
7193                 break;
7194         case MBOX_TIMEOUT:
7195                 xname = "TIMEOUT";
7196                 break;
7197         default:
7198                 ISP_SNPRINTF(mname, sizeof mname, "error 0x%x", mbp->param[0]);
7199                 xname = mname;
7200                 break;
7201         }
7202         if (xname) {
7203                 isp_prt(isp, ISP_LOGALL, "Mailbox Command '%s' failed (%s%s)",
7204                     cname, xname, sname);
7205         }
7206 }
7207
7208 static int
7209 isp_fw_state(ispsoftc_t *isp, int chan)
7210 {
7211         if (IS_FC(isp)) {
7212                 mbreg_t mbs;
7213
7214                 MBSINIT(&mbs, MBOX_GET_FW_STATE, MBLOGALL, 0);
7215                 isp_mboxcmd(isp, &mbs);
7216                 if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
7217                         return (mbs.param[1]);
7218                 }
7219         }
7220         return (FW_ERROR);
7221 }
7222
7223 static void
7224 isp_spi_update(ispsoftc_t *isp, int chan)
7225 {
7226         int tgt;
7227         mbreg_t mbs;
7228         sdparam *sdp;
7229
7230         if (IS_FC(isp)) {
7231                 /*
7232                  * There are no 'per-bus' settings for Fibre Channel.
7233                  */
7234                 return;
7235         }
7236         sdp = SDPARAM(isp, chan);
7237         sdp->update = 0;
7238
7239         for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
7240                 uint16_t flags, period, offset;
7241                 int get;
7242
7243                 if (sdp->isp_devparam[tgt].dev_enable == 0) {
7244                         sdp->isp_devparam[tgt].dev_update = 0;
7245                         sdp->isp_devparam[tgt].dev_refresh = 0;
7246                         isp_prt(isp, ISP_LOGDEBUG0, "skipping target %d bus %d update", tgt, chan);
7247                         continue;
7248                 }
7249                 /*
7250                  * If the goal is to update the status of the device,
7251                  * take what's in goal_flags and try and set the device
7252                  * toward that. Otherwise, if we're just refreshing the
7253                  * current device state, get the current parameters.
7254                  */
7255
7256                 MBSINIT(&mbs, 0, MBLOGALL, 0);
7257
7258                 /*
7259                  * Refresh overrides set
7260                  */
7261                 if (sdp->isp_devparam[tgt].dev_refresh) {
7262                         mbs.param[0] = MBOX_GET_TARGET_PARAMS;
7263                         get = 1;
7264                 } else if (sdp->isp_devparam[tgt].dev_update) {
7265                         mbs.param[0] = MBOX_SET_TARGET_PARAMS;
7266
7267                         /*
7268                          * Make sure goal_flags has "Renegotiate on Error"
7269                          * on and "Freeze Queue on Error" off.
7270                          */
7271                         sdp->isp_devparam[tgt].goal_flags |= DPARM_RENEG;
7272                         sdp->isp_devparam[tgt].goal_flags &= ~DPARM_QFRZ;
7273                         mbs.param[2] = sdp->isp_devparam[tgt].goal_flags;
7274
7275                         /*
7276                          * Insist that PARITY must be enabled
7277                          * if SYNC or WIDE is enabled.
7278                          */
7279                         if ((mbs.param[2] & (DPARM_SYNC|DPARM_WIDE)) != 0) {
7280                                 mbs.param[2] |= DPARM_PARITY;
7281                         }
7282
7283                         if (mbs.param[2] & DPARM_SYNC) {
7284                                 mbs.param[3] =
7285                                     (sdp->isp_devparam[tgt].goal_offset << 8) |
7286                                     (sdp->isp_devparam[tgt].goal_period);
7287                         }
7288                         /*
7289                          * A command completion later that has
7290                          * RQSTF_NEGOTIATION set can cause
7291                          * the dev_refresh/announce cycle also.
7292                          *
7293                          * Note: It is really important to update our current
7294                          * flags with at least the state of TAG capabilities-
7295                          * otherwise we might try and send a tagged command
7296                          * when we have it all turned off. So change it here
7297                          * to say that current already matches goal.
7298                          */
7299                         sdp->isp_devparam[tgt].actv_flags &= ~DPARM_TQING;
7300                         sdp->isp_devparam[tgt].actv_flags |=
7301                             (sdp->isp_devparam[tgt].goal_flags & DPARM_TQING);
7302                         isp_prt(isp, ISP_LOGDEBUG0, "bus %d set tgt %d flags 0x%x off 0x%x period 0x%x",
7303                             chan, tgt, mbs.param[2], mbs.param[3] >> 8, mbs.param[3] & 0xff);
7304                         get = 0;
7305                 } else {
7306                         continue;
7307                 }
7308                 mbs.param[1] = (chan << 15) | (tgt << 8);
7309                 isp_mboxcmd(isp, &mbs);
7310                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
7311                         continue;
7312                 }
7313                 if (get == 0) {
7314                         sdp->sendmarker = 1;
7315                         sdp->isp_devparam[tgt].dev_update = 0;
7316                         sdp->isp_devparam[tgt].dev_refresh = 1;
7317                 } else {
7318                         sdp->isp_devparam[tgt].dev_refresh = 0;
7319                         flags = mbs.param[2];
7320                         period = mbs.param[3] & 0xff;
7321                         offset = mbs.param[3] >> 8;
7322                         sdp->isp_devparam[tgt].actv_flags = flags;
7323                         sdp->isp_devparam[tgt].actv_period = period;
7324                         sdp->isp_devparam[tgt].actv_offset = offset;
7325                         isp_async(isp, ISPASYNC_NEW_TGT_PARAMS, chan, tgt);
7326                 }
7327         }
7328
7329         for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
7330                 if (sdp->isp_devparam[tgt].dev_update ||
7331                     sdp->isp_devparam[tgt].dev_refresh) {
7332                         sdp->update = 1;
7333                         break;
7334                 }
7335         }
7336 }
7337
7338 static void
7339 isp_setdfltsdparm(ispsoftc_t *isp)
7340 {
7341         int tgt;
7342         sdparam *sdp, *sdp1;
7343
7344         sdp = SDPARAM(isp, 0);
7345         sdp->role = GET_DEFAULT_ROLE(isp, 0);
7346         if (IS_DUALBUS(isp)) {
7347                 sdp1 = sdp + 1;
7348                 sdp1->role = GET_DEFAULT_ROLE(isp, 1);
7349         } else {
7350                 sdp1 = NULL;
7351         }
7352
7353         /*
7354          * Establish some default parameters.
7355          */
7356         sdp->isp_cmd_dma_burst_enable = 0;
7357         sdp->isp_data_dma_burst_enabl = 1;
7358         sdp->isp_fifo_threshold = 0;
7359         sdp->isp_initiator_id = DEFAULT_IID(isp, 0);
7360         if (isp->isp_type >= ISP_HA_SCSI_1040) {
7361                 sdp->isp_async_data_setup = 9;
7362         } else {
7363                 sdp->isp_async_data_setup = 6;
7364         }
7365         sdp->isp_selection_timeout = 250;
7366         sdp->isp_max_queue_depth = MAXISPREQUEST(isp);
7367         sdp->isp_tag_aging = 8;
7368         sdp->isp_bus_reset_delay = 5;
7369         /*
7370          * Don't retry selection, busy or queue full automatically- reflect
7371          * these back to us.
7372          */
7373         sdp->isp_retry_count = 0;
7374         sdp->isp_retry_delay = 0;
7375
7376         for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
7377                 sdp->isp_devparam[tgt].exc_throttle = ISP_EXEC_THROTTLE;
7378                 sdp->isp_devparam[tgt].dev_enable = 1;
7379         }
7380
7381         /*
7382          * The trick here is to establish a default for the default (honk!)
7383          * state (goal_flags). Then try and get the current status from
7384          * the card to fill in the current state. We don't, in fact, set
7385          * the default to the SAFE default state- that's not the goal state.
7386          */
7387         for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
7388                 uint8_t off, per;
7389                 sdp->isp_devparam[tgt].actv_offset = 0;
7390                 sdp->isp_devparam[tgt].actv_period = 0;
7391                 sdp->isp_devparam[tgt].actv_flags = 0;
7392
7393                 sdp->isp_devparam[tgt].goal_flags =
7394                     sdp->isp_devparam[tgt].nvrm_flags = DPARM_DEFAULT;
7395
7396                 /*
7397                  * We default to Wide/Fast for versions less than a 1040
7398                  * (unless it's SBus).
7399                  */
7400                 if (IS_ULTRA3(isp)) {
7401                         off = ISP_80M_SYNCPARMS >> 8;
7402                         per = ISP_80M_SYNCPARMS & 0xff;
7403                 } else if (IS_ULTRA2(isp)) {
7404                         off = ISP_40M_SYNCPARMS >> 8;
7405                         per = ISP_40M_SYNCPARMS & 0xff;
7406                 } else if (IS_1240(isp)) {
7407                         off = ISP_20M_SYNCPARMS >> 8;
7408                         per = ISP_20M_SYNCPARMS & 0xff;
7409                 } else if ((isp->isp_bustype == ISP_BT_SBUS &&
7410                     isp->isp_type < ISP_HA_SCSI_1020A) ||
7411                     (isp->isp_bustype == ISP_BT_PCI &&
7412                     isp->isp_type < ISP_HA_SCSI_1040) ||
7413                     (isp->isp_clock && isp->isp_clock < 60) ||
7414                     (sdp->isp_ultramode == 0)) {
7415                         off = ISP_10M_SYNCPARMS >> 8;
7416                         per = ISP_10M_SYNCPARMS & 0xff;
7417                 } else {
7418                         off = ISP_20M_SYNCPARMS_1040 >> 8;
7419                         per = ISP_20M_SYNCPARMS_1040 & 0xff;
7420                 }
7421                 sdp->isp_devparam[tgt].goal_offset =
7422                     sdp->isp_devparam[tgt].nvrm_offset = off;
7423                 sdp->isp_devparam[tgt].goal_period =
7424                     sdp->isp_devparam[tgt].nvrm_period = per;
7425
7426         }
7427
7428         /*
7429          * If we're a dual bus card, just copy the data over
7430          */
7431         if (sdp1) {
7432                 *sdp1 = *sdp;
7433                 sdp1->isp_initiator_id = DEFAULT_IID(isp, 1);
7434         }
7435
7436         /*
7437          * If we've not been told to avoid reading NVRAM, try and read it.
7438          * If we're successful reading it, we can then return because NVRAM
7439          * will tell us what the desired settings are. Otherwise, we establish
7440          * some reasonable 'fake' nvram and goal defaults.
7441          */
7442         if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
7443                 mbreg_t mbs;
7444
7445                 if (isp_read_nvram(isp, 0) == 0) {
7446                         if (IS_DUALBUS(isp)) {
7447                                 if (isp_read_nvram(isp, 1) == 0) {
7448                                         return;
7449                                 }
7450                         }
7451                 }
7452                 MBSINIT(&mbs, MBOX_GET_ACT_NEG_STATE, MBLOGNONE, 0);
7453                 isp_mboxcmd(isp, &mbs);
7454                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
7455                         sdp->isp_req_ack_active_neg = 1;
7456                         sdp->isp_data_line_active_neg = 1;
7457                         if (sdp1) {
7458                                 sdp1->isp_req_ack_active_neg = 1;
7459                                 sdp1->isp_data_line_active_neg = 1;
7460                         }
7461                 } else {
7462                         sdp->isp_req_ack_active_neg =
7463                             (mbs.param[1] >> 4) & 0x1;
7464                         sdp->isp_data_line_active_neg =
7465                             (mbs.param[1] >> 5) & 0x1;
7466                         if (sdp1) {
7467                                 sdp1->isp_req_ack_active_neg =
7468                                     (mbs.param[2] >> 4) & 0x1;
7469                                 sdp1->isp_data_line_active_neg =
7470                                     (mbs.param[2] >> 5) & 0x1;
7471                         }
7472                 }
7473         }
7474
7475 }
7476
7477 static void
7478 isp_setdfltfcparm(ispsoftc_t *isp, int chan)
7479 {
7480         fcparam *fcp = FCPARAM(isp, chan);
7481
7482         /*
7483          * Establish some default parameters.
7484          */
7485         fcp->role = GET_DEFAULT_ROLE(isp, chan);
7486         fcp->isp_maxalloc = ICB_DFLT_ALLOC;
7487         fcp->isp_retry_delay = ICB_DFLT_RDELAY;
7488         fcp->isp_retry_count = ICB_DFLT_RCOUNT;
7489         fcp->isp_loopid = DEFAULT_LOOPID(isp, chan);
7490         fcp->isp_wwnn_nvram = DEFAULT_NODEWWN(isp, chan);
7491         fcp->isp_wwpn_nvram = DEFAULT_PORTWWN(isp, chan);
7492         fcp->isp_fwoptions = 0;
7493         fcp->isp_lasthdl = NIL_HANDLE;
7494
7495         if (IS_24XX(isp)) {
7496                 fcp->isp_fwoptions |= ICB2400_OPT1_FAIRNESS;
7497                 fcp->isp_fwoptions |= ICB2400_OPT1_HARD_ADDRESS;
7498                 if (isp->isp_confopts & ISP_CFG_FULL_DUPLEX) {
7499                         fcp->isp_fwoptions |= ICB2400_OPT1_FULL_DUPLEX;
7500                 }
7501                 fcp->isp_fwoptions |= ICB2400_OPT1_BOTH_WWNS;
7502         } else {
7503                 fcp->isp_fwoptions |= ICBOPT_FAIRNESS;
7504                 fcp->isp_fwoptions |= ICBOPT_PDBCHANGE_AE;
7505                 fcp->isp_fwoptions |= ICBOPT_HARD_ADDRESS;
7506                 if (isp->isp_confopts & ISP_CFG_FULL_DUPLEX) {
7507                         fcp->isp_fwoptions |= ICBOPT_FULL_DUPLEX;
7508                 }
7509                 /*
7510                  * Make sure this is turned off now until we get
7511                  * extended options from NVRAM
7512                  */
7513                 fcp->isp_fwoptions &= ~ICBOPT_EXTENDED;
7514         }
7515
7516
7517         /*
7518          * Now try and read NVRAM unless told to not do so.
7519          * This will set fcparam's isp_wwnn_nvram && isp_wwpn_nvram.
7520          */
7521         if ((isp->isp_confopts & ISP_CFG_NONVRAM) == 0) {
7522                 int i, j = 0;
7523                 /*
7524                  * Give a couple of tries at reading NVRAM.
7525                  */
7526                 for (i = 0; i < 2; i++) {
7527                         j = isp_read_nvram(isp, chan);
7528                         if (j == 0) {
7529                                 break;
7530                         }
7531                 }
7532                 if (j) {
7533                         isp->isp_confopts |= ISP_CFG_NONVRAM;
7534                 }
7535         }
7536
7537         fcp->isp_wwnn = ACTIVE_NODEWWN(isp, chan);
7538         fcp->isp_wwpn = ACTIVE_PORTWWN(isp, chan);
7539         isp_prt(isp, ISP_LOGCONFIG, "Chan %d 0x%08x%08x/0x%08x%08x Role %s",
7540             chan, (uint32_t) (fcp->isp_wwnn >> 32), (uint32_t) (fcp->isp_wwnn),
7541             (uint32_t) (fcp->isp_wwpn >> 32), (uint32_t) (fcp->isp_wwpn),
7542             isp_class3_roles[fcp->role]);
7543 }
7544
7545 /*
7546  * Re-initialize the ISP and complete all orphaned commands
7547  * with a 'botched' notice. The reset/init routines should
7548  * not disturb an already active list of commands.
7549  */
7550
7551 int
7552 isp_reinit(ispsoftc_t *isp, int do_load_defaults)
7553 {
7554         int i, res = 0;
7555
7556         if (isp->isp_state != ISP_RESETSTATE)
7557                 isp_reset(isp, do_load_defaults);
7558         if (isp->isp_state != ISP_RESETSTATE) {
7559                 res = EIO;
7560                 isp_prt(isp, ISP_LOGERR, "%s: cannot reset card", __func__);
7561                 ISP_DISABLE_INTS(isp);
7562                 goto cleanup;
7563         }
7564
7565         isp_init(isp);
7566         if (isp->isp_state > ISP_RESETSTATE &&
7567             isp->isp_state != ISP_RUNSTATE) {
7568                 res = EIO;
7569                 isp_prt(isp, ISP_LOGERR, "%s: cannot init card", __func__);
7570                 ISP_DISABLE_INTS(isp);
7571                 if (IS_FC(isp)) {
7572                         /*
7573                          * If we're in ISP_ROLE_NONE, turn off the lasers.
7574                          */
7575                         if (!IS_24XX(isp)) {
7576                                 ISP_WRITE(isp, BIU2100_CSR, BIU2100_FPM0_REGS);
7577                                 ISP_WRITE(isp, FPM_DIAG_CONFIG, FPM_SOFT_RESET);
7578                                 ISP_WRITE(isp, BIU2100_CSR, BIU2100_FB_REGS);
7579                                 ISP_WRITE(isp, FBM_CMD, FBMCMD_FIFO_RESET_ALL);
7580                                 ISP_WRITE(isp, BIU2100_CSR, BIU2100_RISC_REGS);
7581                         }
7582                 }
7583         }
7584
7585 cleanup:
7586         isp->isp_nactive = 0;
7587         isp_clear_commands(isp);
7588         if (IS_FC(isp)) {
7589                 for (i = 0; i < isp->isp_nchan; i++)
7590                         isp_clear_portdb(isp, i);
7591         }
7592         return (res);
7593 }
7594
7595 /*
7596  * NVRAM Routines
7597  */
7598 static int
7599 isp_read_nvram(ispsoftc_t *isp, int bus)
7600 {
7601         int i, amt, retval;
7602         uint8_t csum, minversion;
7603         union {
7604                 uint8_t _x[ISP2400_NVRAM_SIZE];
7605                 uint16_t _s[ISP2400_NVRAM_SIZE>>1];
7606         } _n;
7607 #define nvram_data      _n._x
7608 #define nvram_words     _n._s
7609
7610         if (IS_24XX(isp)) {
7611                 return (isp_read_nvram_2400(isp, nvram_data));
7612         } else if (IS_FC(isp)) {
7613                 amt = ISP2100_NVRAM_SIZE;
7614                 minversion = 1;
7615         } else if (IS_ULTRA2(isp)) {
7616                 amt = ISP1080_NVRAM_SIZE;
7617                 minversion = 0;
7618         } else {
7619                 amt = ISP_NVRAM_SIZE;
7620                 minversion = 2;
7621         }
7622
7623         for (i = 0; i < amt>>1; i++) {
7624                 isp_rdnvram_word(isp, i, &nvram_words[i]);
7625         }
7626
7627         if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
7628             nvram_data[2] != 'P') {
7629                 if (isp->isp_bustype != ISP_BT_SBUS) {
7630                         isp_prt(isp, ISP_LOGWARN, "invalid NVRAM header");
7631                         isp_prt(isp, ISP_LOGDEBUG0, "%x %x %x", nvram_data[0], nvram_data[1], nvram_data[2]);
7632                 }
7633                 retval = -1;
7634                 goto out;
7635         }
7636
7637         for (csum = 0, i = 0; i < amt; i++) {
7638                 csum += nvram_data[i];
7639         }
7640         if (csum != 0) {
7641                 isp_prt(isp, ISP_LOGWARN, "invalid NVRAM checksum");
7642                 retval = -1;
7643                 goto out;
7644         }
7645
7646         if (ISP_NVRAM_VERSION(nvram_data) < minversion) {
7647                 isp_prt(isp, ISP_LOGWARN, "version %d NVRAM not understood",
7648                     ISP_NVRAM_VERSION(nvram_data));
7649                 retval = -1;
7650                 goto out;
7651         }
7652
7653         if (IS_ULTRA3(isp)) {
7654                 isp_parse_nvram_12160(isp, bus, nvram_data);
7655         } else if (IS_1080(isp)) {
7656                 isp_parse_nvram_1080(isp, bus, nvram_data);
7657         } else if (IS_1280(isp) || IS_1240(isp)) {
7658                 isp_parse_nvram_1080(isp, bus, nvram_data);
7659         } else if (IS_SCSI(isp)) {
7660                 isp_parse_nvram_1020(isp, nvram_data);
7661         } else {
7662                 isp_parse_nvram_2100(isp, nvram_data);
7663         }
7664         retval = 0;
7665 out:
7666         return (retval);
7667 #undef  nvram_data
7668 #undef  nvram_words
7669 }
7670
7671 static int
7672 isp_read_nvram_2400(ispsoftc_t *isp, uint8_t *nvram_data)
7673 {
7674         int retval = 0;
7675         uint32_t addr, csum, lwrds, *dptr;
7676
7677         if (isp->isp_port) {
7678                 addr = ISP2400_NVRAM_PORT1_ADDR;
7679         } else {
7680                 addr = ISP2400_NVRAM_PORT0_ADDR;
7681         }
7682
7683         dptr = (uint32_t *) nvram_data;
7684         for (lwrds = 0; lwrds < ISP2400_NVRAM_SIZE >> 2; lwrds++) {
7685                 isp_rd_2400_nvram(isp, addr++, dptr++);
7686         }
7687         if (nvram_data[0] != 'I' || nvram_data[1] != 'S' ||
7688             nvram_data[2] != 'P') {
7689                 isp_prt(isp, ISP_LOGWARN, "invalid NVRAM header (%x %x %x)",
7690                     nvram_data[0], nvram_data[1], nvram_data[2]);
7691                 retval = -1;
7692                 goto out;
7693         }
7694         dptr = (uint32_t *) nvram_data;
7695         for (csum = 0, lwrds = 0; lwrds < ISP2400_NVRAM_SIZE >> 2; lwrds++) {
7696                 uint32_t tmp;
7697                 ISP_IOXGET_32(isp, &dptr[lwrds], tmp);
7698                 csum += tmp;
7699         }
7700         if (csum != 0) {
7701                 isp_prt(isp, ISP_LOGWARN, "invalid NVRAM checksum");
7702                 retval = -1;
7703                 goto out;
7704         }
7705         isp_parse_nvram_2400(isp, nvram_data);
7706 out:
7707         return (retval);
7708 }
7709
7710 static void
7711 isp_rdnvram_word(ispsoftc_t *isp, int wo, uint16_t *rp)
7712 {
7713         int i, cbits;
7714         uint16_t bit, rqst, junk;
7715
7716         ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
7717         ISP_DELAY(10);
7718         ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
7719         ISP_DELAY(10);
7720
7721         if (IS_FC(isp)) {
7722                 wo &= ((ISP2100_NVRAM_SIZE >> 1) - 1);
7723                 if (IS_2312(isp) && isp->isp_port) {
7724                         wo += 128;
7725                 }
7726                 rqst = (ISP_NVRAM_READ << 8) | wo;
7727                 cbits = 10;
7728         } else if (IS_ULTRA2(isp)) {
7729                 wo &= ((ISP1080_NVRAM_SIZE >> 1) - 1);
7730                 rqst = (ISP_NVRAM_READ << 8) | wo;
7731                 cbits = 10;
7732         } else {
7733                 wo &= ((ISP_NVRAM_SIZE >> 1) - 1);
7734                 rqst = (ISP_NVRAM_READ << 6) | wo;
7735                 cbits = 8;
7736         }
7737
7738         /*
7739          * Clock the word select request out...
7740          */
7741         for (i = cbits; i >= 0; i--) {
7742                 if ((rqst >> i) & 1) {
7743                         bit = BIU_NVRAM_SELECT | BIU_NVRAM_DATAOUT;
7744                 } else {
7745                         bit = BIU_NVRAM_SELECT;
7746                 }
7747                 ISP_WRITE(isp, BIU_NVRAM, bit);
7748                 ISP_DELAY(10);
7749                 junk = ISP_READ(isp, BIU_NVRAM);        /* force PCI flush */
7750                 ISP_WRITE(isp, BIU_NVRAM, bit | BIU_NVRAM_CLOCK);
7751                 ISP_DELAY(10);
7752                 junk = ISP_READ(isp, BIU_NVRAM);        /* force PCI flush */
7753                 ISP_WRITE(isp, BIU_NVRAM, bit);
7754                 ISP_DELAY(10);
7755                 junk = ISP_READ(isp, BIU_NVRAM);        /* force PCI flush */
7756         }
7757         /*
7758          * Now read the result back in (bits come back in MSB format).
7759          */
7760         *rp = 0;
7761         for (i = 0; i < 16; i++) {
7762                 uint16_t rv;
7763                 *rp <<= 1;
7764                 ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT|BIU_NVRAM_CLOCK);
7765                 ISP_DELAY(10);
7766                 rv = ISP_READ(isp, BIU_NVRAM);
7767                 if (rv & BIU_NVRAM_DATAIN) {
7768                         *rp |= 1;
7769                 }
7770                 ISP_DELAY(10);
7771                 ISP_WRITE(isp, BIU_NVRAM, BIU_NVRAM_SELECT);
7772                 ISP_DELAY(10);
7773                 junk = ISP_READ(isp, BIU_NVRAM);        /* force PCI flush */
7774         }
7775         ISP_WRITE(isp, BIU_NVRAM, 0);
7776         ISP_DELAY(10);
7777         junk = ISP_READ(isp, BIU_NVRAM);        /* force PCI flush */
7778         ISP_SWIZZLE_NVRAM_WORD(isp, rp);
7779 }
7780
7781 static void
7782 isp_rd_2400_nvram(ispsoftc_t *isp, uint32_t addr, uint32_t *rp)
7783 {
7784         int loops = 0;
7785         uint32_t base = 0x7ffe0000;
7786         uint32_t tmp = 0;
7787
7788         if (IS_25XX(isp)) {
7789                 base = 0x7ff00000 | 0x48000;
7790         }
7791         ISP_WRITE(isp, BIU2400_FLASH_ADDR, base | addr);
7792         for (loops = 0; loops < 5000; loops++) {
7793                 ISP_DELAY(10);
7794                 tmp = ISP_READ(isp, BIU2400_FLASH_ADDR);
7795                 if ((tmp & (1U << 31)) != 0) {
7796                         break;
7797                 }
7798         }
7799         if (tmp & (1U << 31)) {
7800                 *rp = ISP_READ(isp, BIU2400_FLASH_DATA);
7801                 ISP_SWIZZLE_NVRAM_LONG(isp, rp);
7802         } else {
7803                 *rp = 0xffffffff;
7804         }
7805 }
7806
7807 static void
7808 isp_parse_nvram_1020(ispsoftc_t *isp, uint8_t *nvram_data)
7809 {
7810         sdparam *sdp = SDPARAM(isp, 0);
7811         int tgt;
7812
7813         sdp->isp_fifo_threshold =
7814                 ISP_NVRAM_FIFO_THRESHOLD(nvram_data) |
7815                 (ISP_NVRAM_FIFO_THRESHOLD_128(nvram_data) << 2);
7816
7817         if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
7818                 sdp->isp_initiator_id = ISP_NVRAM_INITIATOR_ID(nvram_data);
7819
7820         sdp->isp_bus_reset_delay =
7821                 ISP_NVRAM_BUS_RESET_DELAY(nvram_data);
7822
7823         sdp->isp_retry_count =
7824                 ISP_NVRAM_BUS_RETRY_COUNT(nvram_data);
7825
7826         sdp->isp_retry_delay =
7827                 ISP_NVRAM_BUS_RETRY_DELAY(nvram_data);
7828
7829         sdp->isp_async_data_setup =
7830                 ISP_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data);
7831
7832         if (isp->isp_type >= ISP_HA_SCSI_1040) {
7833                 if (sdp->isp_async_data_setup < 9) {
7834                         sdp->isp_async_data_setup = 9;
7835                 }
7836         } else {
7837                 if (sdp->isp_async_data_setup != 6) {
7838                         sdp->isp_async_data_setup = 6;
7839                 }
7840         }
7841
7842         sdp->isp_req_ack_active_neg =
7843                 ISP_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data);
7844
7845         sdp->isp_data_line_active_neg =
7846                 ISP_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data);
7847
7848         sdp->isp_data_dma_burst_enabl =
7849                 ISP_NVRAM_DATA_DMA_BURST_ENABLE(nvram_data);
7850
7851         sdp->isp_cmd_dma_burst_enable =
7852                 ISP_NVRAM_CMD_DMA_BURST_ENABLE(nvram_data);
7853
7854         sdp->isp_tag_aging =
7855                 ISP_NVRAM_TAG_AGE_LIMIT(nvram_data);
7856
7857         sdp->isp_selection_timeout =
7858                 ISP_NVRAM_SELECTION_TIMEOUT(nvram_data);
7859
7860         sdp->isp_max_queue_depth =
7861                 ISP_NVRAM_MAX_QUEUE_DEPTH(nvram_data);
7862
7863         sdp->isp_fast_mttr = ISP_NVRAM_FAST_MTTR_ENABLE(nvram_data);
7864
7865         for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
7866                 sdp->isp_devparam[tgt].dev_enable =
7867                         ISP_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt);
7868                 sdp->isp_devparam[tgt].exc_throttle =
7869                         ISP_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt);
7870                 sdp->isp_devparam[tgt].nvrm_offset =
7871                         ISP_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt);
7872                 sdp->isp_devparam[tgt].nvrm_period =
7873                         ISP_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt);
7874                 /*
7875                  * We probably shouldn't lie about this, but it
7876                  * it makes it much safer if we limit NVRAM values
7877                  * to sanity.
7878                  */
7879                 if (isp->isp_type < ISP_HA_SCSI_1040) {
7880                         /*
7881                          * If we're not ultra, we can't possibly
7882                          * be a shorter period than this.
7883                          */
7884                         if (sdp->isp_devparam[tgt].nvrm_period < 0x19) {
7885                                 sdp->isp_devparam[tgt].nvrm_period = 0x19;
7886                         }
7887                         if (sdp->isp_devparam[tgt].nvrm_offset > 0xc) {
7888                                 sdp->isp_devparam[tgt].nvrm_offset = 0x0c;
7889                         }
7890                 } else {
7891                         if (sdp->isp_devparam[tgt].nvrm_offset > 0x8) {
7892                                 sdp->isp_devparam[tgt].nvrm_offset = 0x8;
7893                         }
7894                 }
7895                 sdp->isp_devparam[tgt].nvrm_flags = 0;
7896                 if (ISP_NVRAM_TGT_RENEG(nvram_data, tgt))
7897                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
7898                 sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
7899                 if (ISP_NVRAM_TGT_TQING(nvram_data, tgt))
7900                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
7901                 if (ISP_NVRAM_TGT_SYNC(nvram_data, tgt))
7902                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
7903                 if (ISP_NVRAM_TGT_WIDE(nvram_data, tgt))
7904                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
7905                 if (ISP_NVRAM_TGT_PARITY(nvram_data, tgt))
7906                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
7907                 if (ISP_NVRAM_TGT_DISC(nvram_data, tgt))
7908                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
7909                 sdp->isp_devparam[tgt].actv_flags = 0; /* we don't know */
7910                 sdp->isp_devparam[tgt].goal_offset =
7911                     sdp->isp_devparam[tgt].nvrm_offset;
7912                 sdp->isp_devparam[tgt].goal_period =
7913                     sdp->isp_devparam[tgt].nvrm_period;
7914                 sdp->isp_devparam[tgt].goal_flags =
7915                     sdp->isp_devparam[tgt].nvrm_flags;
7916         }
7917 }
7918
7919 static void
7920 isp_parse_nvram_1080(ispsoftc_t *isp, int bus, uint8_t *nvram_data)
7921 {
7922         sdparam *sdp = SDPARAM(isp, bus);
7923         int tgt;
7924
7925         sdp->isp_fifo_threshold =
7926             ISP1080_NVRAM_FIFO_THRESHOLD(nvram_data);
7927
7928         if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
7929                 sdp->isp_initiator_id = ISP1080_NVRAM_INITIATOR_ID(nvram_data, bus);
7930
7931         sdp->isp_bus_reset_delay =
7932             ISP1080_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
7933
7934         sdp->isp_retry_count =
7935             ISP1080_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
7936
7937         sdp->isp_retry_delay =
7938             ISP1080_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
7939
7940         sdp->isp_async_data_setup =
7941             ISP1080_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
7942
7943         sdp->isp_req_ack_active_neg =
7944             ISP1080_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
7945
7946         sdp->isp_data_line_active_neg =
7947             ISP1080_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
7948
7949         sdp->isp_data_dma_burst_enabl =
7950             ISP1080_NVRAM_BURST_ENABLE(nvram_data);
7951
7952         sdp->isp_cmd_dma_burst_enable =
7953             ISP1080_NVRAM_BURST_ENABLE(nvram_data);
7954
7955         sdp->isp_selection_timeout =
7956             ISP1080_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
7957
7958         sdp->isp_max_queue_depth =
7959              ISP1080_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
7960
7961         for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
7962                 sdp->isp_devparam[tgt].dev_enable =
7963                     ISP1080_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
7964                 sdp->isp_devparam[tgt].exc_throttle =
7965                         ISP1080_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
7966                 sdp->isp_devparam[tgt].nvrm_offset =
7967                         ISP1080_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
7968                 sdp->isp_devparam[tgt].nvrm_period =
7969                         ISP1080_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
7970                 sdp->isp_devparam[tgt].nvrm_flags = 0;
7971                 if (ISP1080_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
7972                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
7973                 sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
7974                 if (ISP1080_NVRAM_TGT_TQING(nvram_data, tgt, bus))
7975                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
7976                 if (ISP1080_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
7977                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
7978                 if (ISP1080_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
7979                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
7980                 if (ISP1080_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
7981                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
7982                 if (ISP1080_NVRAM_TGT_DISC(nvram_data, tgt, bus))
7983                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
7984                 sdp->isp_devparam[tgt].actv_flags = 0;
7985                 sdp->isp_devparam[tgt].goal_offset =
7986                     sdp->isp_devparam[tgt].nvrm_offset;
7987                 sdp->isp_devparam[tgt].goal_period =
7988                     sdp->isp_devparam[tgt].nvrm_period;
7989                 sdp->isp_devparam[tgt].goal_flags =
7990                     sdp->isp_devparam[tgt].nvrm_flags;
7991         }
7992 }
7993
7994 static void
7995 isp_parse_nvram_12160(ispsoftc_t *isp, int bus, uint8_t *nvram_data)
7996 {
7997         sdparam *sdp = SDPARAM(isp, bus);
7998         int tgt;
7999
8000         sdp->isp_fifo_threshold =
8001             ISP12160_NVRAM_FIFO_THRESHOLD(nvram_data);
8002
8003         if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0)
8004                 sdp->isp_initiator_id = ISP12160_NVRAM_INITIATOR_ID(nvram_data, bus);
8005
8006         sdp->isp_bus_reset_delay =
8007             ISP12160_NVRAM_BUS_RESET_DELAY(nvram_data, bus);
8008
8009         sdp->isp_retry_count =
8010             ISP12160_NVRAM_BUS_RETRY_COUNT(nvram_data, bus);
8011
8012         sdp->isp_retry_delay =
8013             ISP12160_NVRAM_BUS_RETRY_DELAY(nvram_data, bus);
8014
8015         sdp->isp_async_data_setup =
8016             ISP12160_NVRAM_ASYNC_DATA_SETUP_TIME(nvram_data, bus);
8017
8018         sdp->isp_req_ack_active_neg =
8019             ISP12160_NVRAM_REQ_ACK_ACTIVE_NEGATION(nvram_data, bus);
8020
8021         sdp->isp_data_line_active_neg =
8022             ISP12160_NVRAM_DATA_LINE_ACTIVE_NEGATION(nvram_data, bus);
8023
8024         sdp->isp_data_dma_burst_enabl =
8025             ISP12160_NVRAM_BURST_ENABLE(nvram_data);
8026
8027         sdp->isp_cmd_dma_burst_enable =
8028             ISP12160_NVRAM_BURST_ENABLE(nvram_data);
8029
8030         sdp->isp_selection_timeout =
8031             ISP12160_NVRAM_SELECTION_TIMEOUT(nvram_data, bus);
8032
8033         sdp->isp_max_queue_depth =
8034              ISP12160_NVRAM_MAX_QUEUE_DEPTH(nvram_data, bus);
8035
8036         for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
8037                 sdp->isp_devparam[tgt].dev_enable =
8038                     ISP12160_NVRAM_TGT_DEVICE_ENABLE(nvram_data, tgt, bus);
8039                 sdp->isp_devparam[tgt].exc_throttle =
8040                         ISP12160_NVRAM_TGT_EXEC_THROTTLE(nvram_data, tgt, bus);
8041                 sdp->isp_devparam[tgt].nvrm_offset =
8042                         ISP12160_NVRAM_TGT_SYNC_OFFSET(nvram_data, tgt, bus);
8043                 sdp->isp_devparam[tgt].nvrm_period =
8044                         ISP12160_NVRAM_TGT_SYNC_PERIOD(nvram_data, tgt, bus);
8045                 sdp->isp_devparam[tgt].nvrm_flags = 0;
8046                 if (ISP12160_NVRAM_TGT_RENEG(nvram_data, tgt, bus))
8047                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_RENEG;
8048                 sdp->isp_devparam[tgt].nvrm_flags |= DPARM_ARQ;
8049                 if (ISP12160_NVRAM_TGT_TQING(nvram_data, tgt, bus))
8050                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_TQING;
8051                 if (ISP12160_NVRAM_TGT_SYNC(nvram_data, tgt, bus))
8052                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_SYNC;
8053                 if (ISP12160_NVRAM_TGT_WIDE(nvram_data, tgt, bus))
8054                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_WIDE;
8055                 if (ISP12160_NVRAM_TGT_PARITY(nvram_data, tgt, bus))
8056                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_PARITY;
8057                 if (ISP12160_NVRAM_TGT_DISC(nvram_data, tgt, bus))
8058                         sdp->isp_devparam[tgt].nvrm_flags |= DPARM_DISC;
8059                 sdp->isp_devparam[tgt].actv_flags = 0;
8060                 sdp->isp_devparam[tgt].goal_offset =
8061                     sdp->isp_devparam[tgt].nvrm_offset;
8062                 sdp->isp_devparam[tgt].goal_period =
8063                     sdp->isp_devparam[tgt].nvrm_period;
8064                 sdp->isp_devparam[tgt].goal_flags =
8065                     sdp->isp_devparam[tgt].nvrm_flags;
8066         }
8067 }
8068
8069 static void
8070 isp_parse_nvram_2100(ispsoftc_t *isp, uint8_t *nvram_data)
8071 {
8072         fcparam *fcp = FCPARAM(isp, 0);
8073         uint64_t wwn;
8074
8075         /*
8076          * There is NVRAM storage for both Port and Node entities-
8077          * but the Node entity appears to be unused on all the cards
8078          * I can find. However, we should account for this being set
8079          * at some point in the future.
8080          *
8081          * Qlogic WWNs have an NAA of 2, but usually nothing shows up in
8082          * bits 48..60. In the case of the 2202, it appears that they do
8083          * use bit 48 to distinguish between the two instances on the card.
8084          * The 2204, which I've never seen, *probably* extends this method.
8085          */
8086         wwn = ISP2100_NVRAM_PORT_NAME(nvram_data);
8087         if (wwn) {
8088                 isp_prt(isp, ISP_LOGCONFIG, "NVRAM Port WWN 0x%08x%08x",
8089                     (uint32_t) (wwn >> 32), (uint32_t) (wwn));
8090                 if ((wwn >> 60) == 0) {
8091                         wwn |= (((uint64_t) 2)<< 60);
8092                 }
8093         }
8094         fcp->isp_wwpn_nvram = wwn;
8095         if (IS_2200(isp) || IS_23XX(isp)) {
8096                 wwn = ISP2100_NVRAM_NODE_NAME(nvram_data);
8097                 if (wwn) {
8098                         isp_prt(isp, ISP_LOGCONFIG, "NVRAM Node WWN 0x%08x%08x",
8099                             (uint32_t) (wwn >> 32),
8100                             (uint32_t) (wwn));
8101                         if ((wwn >> 60) == 0) {
8102                                 wwn |= (((uint64_t) 2)<< 60);
8103                         }
8104                 } else {
8105                         wwn = fcp->isp_wwpn_nvram & ~((uint64_t) 0xfff << 48);
8106                 }
8107         } else {
8108                 wwn &= ~((uint64_t) 0xfff << 48);
8109         }
8110         fcp->isp_wwnn_nvram = wwn;
8111
8112         fcp->isp_maxalloc = ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data);
8113         if ((isp->isp_confopts & ISP_CFG_OWNFSZ) == 0) {
8114                 DEFAULT_FRAMESIZE(isp) =
8115                     ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data);
8116         }
8117         fcp->isp_retry_delay = ISP2100_NVRAM_RETRY_DELAY(nvram_data);
8118         fcp->isp_retry_count = ISP2100_NVRAM_RETRY_COUNT(nvram_data);
8119         if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0) {
8120                 fcp->isp_loopid = ISP2100_NVRAM_HARDLOOPID(nvram_data);
8121         }
8122         if ((isp->isp_confopts & ISP_CFG_OWNEXCTHROTTLE) == 0) {
8123                 DEFAULT_EXEC_THROTTLE(isp) =
8124                         ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data);
8125         }
8126         fcp->isp_fwoptions = ISP2100_NVRAM_OPTIONS(nvram_data);
8127         isp_prt(isp, ISP_LOGDEBUG0,
8128             "NVRAM 0x%08x%08x 0x%08x%08x maxalloc %d maxframelen %d",
8129             (uint32_t) (fcp->isp_wwnn_nvram >> 32),
8130             (uint32_t) fcp->isp_wwnn_nvram,
8131             (uint32_t) (fcp->isp_wwpn_nvram >> 32),
8132             (uint32_t) fcp->isp_wwpn_nvram,
8133             ISP2100_NVRAM_MAXIOCBALLOCATION(nvram_data),
8134             ISP2100_NVRAM_MAXFRAMELENGTH(nvram_data));
8135         isp_prt(isp, ISP_LOGDEBUG0,
8136             "execthrottle %d fwoptions 0x%x hardloop %d tov %d",
8137             ISP2100_NVRAM_EXECUTION_THROTTLE(nvram_data),
8138             ISP2100_NVRAM_OPTIONS(nvram_data),
8139             ISP2100_NVRAM_HARDLOOPID(nvram_data),
8140             ISP2100_NVRAM_TOV(nvram_data));
8141         fcp->isp_xfwoptions = ISP2100_XFW_OPTIONS(nvram_data);
8142         fcp->isp_zfwoptions = ISP2100_ZFW_OPTIONS(nvram_data);
8143         isp_prt(isp, ISP_LOGDEBUG0, "xfwoptions 0x%x zfw options 0x%x",
8144             ISP2100_XFW_OPTIONS(nvram_data), ISP2100_ZFW_OPTIONS(nvram_data));
8145 }
8146
8147 static void
8148 isp_parse_nvram_2400(ispsoftc_t *isp, uint8_t *nvram_data)
8149 {
8150         fcparam *fcp = FCPARAM(isp, 0);
8151         uint64_t wwn;
8152
8153         isp_prt(isp, ISP_LOGDEBUG0,
8154             "NVRAM 0x%08x%08x 0x%08x%08x exchg_cnt %d maxframelen %d",
8155             (uint32_t) (ISP2400_NVRAM_NODE_NAME(nvram_data) >> 32),
8156             (uint32_t) (ISP2400_NVRAM_NODE_NAME(nvram_data)),
8157             (uint32_t) (ISP2400_NVRAM_PORT_NAME(nvram_data) >> 32),
8158             (uint32_t) (ISP2400_NVRAM_PORT_NAME(nvram_data)),
8159             ISP2400_NVRAM_EXCHANGE_COUNT(nvram_data),
8160             ISP2400_NVRAM_MAXFRAMELENGTH(nvram_data));
8161         isp_prt(isp, ISP_LOGDEBUG0,
8162             "NVRAM execthr %d loopid %d fwopt1 0x%x fwopt2 0x%x fwopt3 0x%x",
8163             ISP2400_NVRAM_EXECUTION_THROTTLE(nvram_data),
8164             ISP2400_NVRAM_HARDLOOPID(nvram_data),
8165             ISP2400_NVRAM_FIRMWARE_OPTIONS1(nvram_data),
8166             ISP2400_NVRAM_FIRMWARE_OPTIONS2(nvram_data),
8167             ISP2400_NVRAM_FIRMWARE_OPTIONS3(nvram_data));
8168
8169         wwn = ISP2400_NVRAM_PORT_NAME(nvram_data);
8170         fcp->isp_wwpn_nvram = wwn;
8171
8172         wwn = ISP2400_NVRAM_NODE_NAME(nvram_data);
8173         if (wwn) {
8174                 if ((wwn >> 60) != 2 && (wwn >> 60) != 5) {
8175                         wwn = 0;
8176                 }
8177         }
8178         if (wwn == 0 && (fcp->isp_wwpn_nvram >> 60) == 2) {
8179                 wwn = fcp->isp_wwpn_nvram;
8180                 wwn &= ~((uint64_t) 0xfff << 48);
8181         }
8182         fcp->isp_wwnn_nvram = wwn;
8183
8184         if (ISP2400_NVRAM_EXCHANGE_COUNT(nvram_data)) {
8185                 fcp->isp_maxalloc = ISP2400_NVRAM_EXCHANGE_COUNT(nvram_data);
8186         }
8187         if ((isp->isp_confopts & ISP_CFG_OWNFSZ) == 0) {
8188                 DEFAULT_FRAMESIZE(isp) =
8189                     ISP2400_NVRAM_MAXFRAMELENGTH(nvram_data);
8190         }
8191         if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0) {
8192                 fcp->isp_loopid = ISP2400_NVRAM_HARDLOOPID(nvram_data);
8193         }
8194         if ((isp->isp_confopts & ISP_CFG_OWNEXCTHROTTLE) == 0) {
8195                 DEFAULT_EXEC_THROTTLE(isp) =
8196                         ISP2400_NVRAM_EXECUTION_THROTTLE(nvram_data);
8197         }
8198         fcp->isp_fwoptions = ISP2400_NVRAM_FIRMWARE_OPTIONS1(nvram_data);
8199         fcp->isp_xfwoptions = ISP2400_NVRAM_FIRMWARE_OPTIONS2(nvram_data);
8200         fcp->isp_zfwoptions = ISP2400_NVRAM_FIRMWARE_OPTIONS3(nvram_data);
8201 }