Allow to compile with modern gcc-4.x compiler, which make some sophisticated
[mirror/scst/.git] / qla_isp / common / isp.c
1 /* $Id: isp.c,v 1.216 2009/04/03 04:56:00 mjacob Exp $ */
2 /*-
3  *  Copyright (c) 1997-2009 by Matthew Jacob
4  *  All rights reserved.
5  *
6  *  Redistribution and use in source and binary forms, with or without
7  *  modification, are permitted provided that the following conditions
8  *  are met:
9  *
10  *  1. Redistributions of source code must retain the above copyright
11  *     notice, this list of conditions and the following disclaimer.
12  *  2. Redistributions in binary form must reproduce the above copyright
13  *     notice, this list of conditions and the following disclaimer in the
14  *     documentation and/or other materials provided with the distribution.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  *  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  *  SUCH DAMAGE.
27  *
28  *
29  *  Alternatively, this software may be distributed under the terms of the
30  *  the GNU Public License ("GPL") with platforms where the prevalant license
31  *  is the GNU Public License:
32  *
33  *   This program is free software; you can redistribute it and/or modify
34  *   it under the terms of The Version 2 GNU General Public License as published
35  *   by the Free Software Foundation.
36  *
37  *   This program is distributed in the hope that it will be useful,
38  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
39  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
40  *   GNU General Public License for more details.
41  *
42  *   You should have received a copy of the GNU General Public License
43  *   along with this program; if not, write to the Free Software
44  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
45  *
46  *
47  *  Matthew Jacob
48  *  Feral Software
49  *  421 Laurel Avenue
50  *  Menlo Park, CA 94025
51  *  USA
52  *
53  *  gplbsd at feral com
54  */
55
56 /*
57  * Machine and OS Independent (well, as best as possible)
58  * code for the Qlogic ISP SCSI and FC-SCSI adapters.
59  */
60
61 /*
62  * Inspiration and ideas about this driver are from Erik Moe's Linux driver
63  * (qlogicisp.c) and Dave Miller's SBus version of same (qlogicisp.c). Some
64  * ideas dredged from the Solaris driver.
65  */
66
67 /*
68  * Include header file appropriate for platform we're building on.
69  */
70 #ifdef  __NetBSD__
71 #include <sys/cdefs.h>
72 __KERNEL_RCSID(0, "$NetBSD$");
73 #include <dev/ic/isp_netbsd.h>
74 #endif
75 #ifdef  __FreeBSD__
76 #include <sys/cdefs.h>
77 __FBSDID("$FreeBSD$");
78 #include <dev/isp/isp_freebsd.h>
79 #endif
80 #ifdef  __OpenBSD__
81 #include <dev/ic/isp_openbsd.h>
82 #endif
83 #ifdef  __linux__
84 #include "isp_linux.h"
85 #endif
86 #ifdef  __svr4__
87 #include "isp_solaris.h"
88 #endif
89
90 /*
91  * General defines
92  */
93
94 #define MBOX_DELAY_COUNT        1000000 / 100
95 #define ISP_MARK_PORTDB(a, b, c)                                \
96     isp_prt(isp, ISP_LOGSANCFG,                                 \
97         "Chan %d ISP_MARK_PORTDB@LINE %d", b, __LINE__);        \
98     isp_mark_portdb(a, b, c)
99
100 /*
101  * Local static data
102  */
103 static const char fconf[] = "Chan %d PortDB[%d] changed:\n current =(0x%x@0x%06x 0x%08x%08x 0x%08x%08x)\n database=(0x%x@0x%06x 0x%08x%08x 0x%08x%08x)";
104 static const char notresp[] = "Not RESPONSE in RESPONSE Queue (type 0x%x) @ idx %d (next %d) nlooked %d";
105 static const char xact1[] = "HBA attempted queued transaction with disconnect not set for %d.%d.%d";
106 static const char xact2[] = "HBA attempted queued transaction to target routine %d on target %d bus %d";
107 static const char xact3[] = "HBA attempted queued cmd for %d.%d.%d when queueing disabled";
108 static const char pskip[] = "SCSI phase skipped for target %d.%d.%d";
109 static const char topology[] = "Chan %d WWPN 0x%08x%08x PortID 0x%06x N-Port Handle %d, Connection '%s'";
110 static const char finmsg[] = "%d.%d.%d: FIN dl%d resid %ld STS 0x%x SKEY %c XS_ERR=0x%x";
111 static const char sc4[] = "NVRAM";
112 static const char bun[] = "bad underrun for %d.%d (count %d, resid %d, status %s)";
113 static const char lipd[] = "Chan %d LIP destroyed %d active commands";
114 static const char sacq[] = "unable to acquire scratch area";
115
116 static const uint8_t alpa_map[] = {
117         0xef, 0xe8, 0xe4, 0xe2, 0xe1, 0xe0, 0xdc, 0xda,
118         0xd9, 0xd6, 0xd5, 0xd4, 0xd3, 0xd2, 0xd1, 0xce,
119         0xcd, 0xcc, 0xcb, 0xca, 0xc9, 0xc7, 0xc6, 0xc5,
120         0xc3, 0xbc, 0xba, 0xb9, 0xb6, 0xb5, 0xb4, 0xb3,
121         0xb2, 0xb1, 0xae, 0xad, 0xac, 0xab, 0xaa, 0xa9,
122         0xa7, 0xa6, 0xa5, 0xa3, 0x9f, 0x9e, 0x9d, 0x9b,
123         0x98, 0x97, 0x90, 0x8f, 0x88, 0x84, 0x82, 0x81,
124         0x80, 0x7c, 0x7a, 0x79, 0x76, 0x75, 0x74, 0x73,
125         0x72, 0x71, 0x6e, 0x6d, 0x6c, 0x6b, 0x6a, 0x69,
126         0x67, 0x66, 0x65, 0x63, 0x5c, 0x5a, 0x59, 0x56,
127         0x55, 0x54, 0x53, 0x52, 0x51, 0x4e, 0x4d, 0x4c,
128         0x4b, 0x4a, 0x49, 0x47, 0x46, 0x45, 0x43, 0x3c,
129         0x3a, 0x39, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31,
130         0x2e, 0x2d, 0x2c, 0x2b, 0x2a, 0x29, 0x27, 0x26,
131         0x25, 0x23, 0x1f, 0x1e, 0x1d, 0x1b, 0x18, 0x17,
132         0x10, 0x0f, 0x08, 0x04, 0x02, 0x01, 0x00
133 };
134
135 /*
136  * Local function prototypes.
137  */
138 static int isp_parse_async(ispsoftc_t *, uint16_t);
139 static int isp_handle_other_response(ispsoftc_t *, int, isphdr_t *, uint32_t *);
140 static void isp_parse_status(ispsoftc_t *, ispstatusreq_t *, XS_T *, long *); static void
141 isp_parse_status_24xx(ispsoftc_t *, isp24xx_statusreq_t *, XS_T *, long *);
142 static void isp_fastpost_complete(ispsoftc_t *, uint16_t);
143 static int isp_mbox_continue(ispsoftc_t *);
144 static void isp_scsi_init(ispsoftc_t *);
145 static void isp_scsi_channel_init(ispsoftc_t *, int);
146 static void isp_fibre_init(ispsoftc_t *);
147 static void isp_fibre_init_2400(ispsoftc_t *);
148 static void isp_mark_portdb(ispsoftc_t *, int, int);
149 static int isp_plogx(ispsoftc_t *, int, uint16_t, uint32_t, int, int);
150 static int isp_port_login(ispsoftc_t *, uint16_t, uint32_t);
151 static int isp_port_logout(ispsoftc_t *, uint16_t, uint32_t);
152 static int isp_getpdb(ispsoftc_t *, int, uint16_t, isp_pdb_t *, int);
153 static void isp_dump_chip_portdb(ispsoftc_t *, int, int);
154 static uint64_t isp_get_wwn(ispsoftc_t *, int, int, int);
155 static int isp_fclink_test(ispsoftc_t *, int, int);
156 static int isp_pdb_sync(ispsoftc_t *, int);
157 static int isp_scan_loop(ispsoftc_t *, int);
158 static int isp_gid_ft_sns(ispsoftc_t *, int);
159 static int isp_gid_ft_ct_passthru(ispsoftc_t *, int);
160 static int isp_scan_fabric(ispsoftc_t *, int);
161 static int isp_login_device(ispsoftc_t *, int, uint32_t, isp_pdb_t *, uint16_t *);
162 static int isp_register_fc4_type(ispsoftc_t *, int);
163 static int isp_register_fc4_type_24xx(ispsoftc_t *, int);
164 static uint16_t isp_nxt_handle(ispsoftc_t *, int, uint16_t);
165 static void isp_fw_state(ispsoftc_t *, int);
166 static void isp_mboxcmd_qnw(ispsoftc_t *, mbreg_t *, int);
167 static void isp_mboxcmd(ispsoftc_t *, mbreg_t *);
168
169 static void isp_spi_update(ispsoftc_t *, int);
170 static void isp_setdfltsdparm(ispsoftc_t *);
171 static void isp_setdfltfcparm(ispsoftc_t *, int);
172 static int isp_read_nvram(ispsoftc_t *, int);
173 static int isp_read_nvram_2400(ispsoftc_t *, uint8_t *);
174 static void isp_rdnvram_word(ispsoftc_t *, int, uint16_t *);
175 static void isp_rd_2400_nvram(ispsoftc_t *, uint32_t, uint32_t *);
176 static void isp_parse_nvram_1020(ispsoftc_t *, uint8_t *);
177 static void isp_parse_nvram_1080(ispsoftc_t *, int, uint8_t *);
178 static void isp_parse_nvram_12160(ispsoftc_t *, int, uint8_t *);
179 static void isp_parse_nvram_2100(ispsoftc_t *, uint8_t *);
180 static void isp_parse_nvram_2400(ispsoftc_t *, uint8_t *);
181
182 /*
183  * Reset Hardware.
184  *
185  * Hit the chip over the head, download new f/w if available and set it running.
186  *
187  * Locking done elsewhere.
188  */
189
190 void
191 isp_reset(ispsoftc_t *isp)
192 {
193         mbreg_t mbs;
194         uint32_t code_org, val;
195         int loops, i, dodnld = 1;
196         static const char *btype = "????";
197         static const char dcrc[] = "Downloaded RISC Code Checksum Failure";
198
199         isp->isp_state = ISP_NILSTATE;
200         if (isp->isp_dead) {
201                 isp_shutdown(isp);
202                 ISP_DISABLE_INTS(isp);
203                 return;
204         }
205
206         /*
207          * Basic types (SCSI, FibreChannel and PCI or SBus)
208          * have been set in the MD code. We figure out more
209          * here. Possibly more refined types based upon PCI
210          * identification. Chip revision has been gathered.
211          *
212          * After we've fired this chip up, zero out the conf1 register
213          * for SCSI adapters and do other settings for the 2100.
214          */
215
216         ISP_DISABLE_INTS(isp);
217
218         /*
219          * Pick an initial maxcmds value which will be used
220          * to allocate xflist pointer space. It may be changed
221          * later by the firmware.
222          */
223         if (IS_24XX(isp)) {
224                 isp->isp_maxcmds = 4096;
225         } else if (IS_2322(isp)) {
226                 isp->isp_maxcmds = 2048;
227         } else if (IS_23XX(isp) || IS_2200(isp)) {
228                 isp->isp_maxcmds = 1024;
229         } else {
230                 isp->isp_maxcmds = 512;
231         }
232
233         /*
234          * Set up DMA for the request and response queues.
235          *
236          * We do this now so we can use the request queue
237          * for dma to load firmware from.
238          */
239         if (ISP_MBOXDMASETUP(isp) != 0) {
240                 isp_prt(isp, ISP_LOGERR, "Cannot setup DMA");
241                 return;
242         }
243
244         /*
245          * Set up default request/response queue in-pointer/out-pointer
246          * register indices.
247          */
248         if (IS_24XX(isp)) {
249                 isp->isp_rqstinrp = BIU2400_REQINP;
250                 isp->isp_rqstoutrp = BIU2400_REQOUTP;
251                 isp->isp_respinrp = BIU2400_RSPINP;
252                 isp->isp_respoutrp = BIU2400_RSPOUTP;
253         } else if (IS_23XX(isp)) {
254                 isp->isp_rqstinrp = BIU_REQINP;
255                 isp->isp_rqstoutrp = BIU_REQOUTP;
256                 isp->isp_respinrp = BIU_RSPINP;
257                 isp->isp_respoutrp = BIU_RSPOUTP;
258         } else {
259                 isp->isp_rqstinrp = INMAILBOX4;
260                 isp->isp_rqstoutrp = OUTMAILBOX4;
261                 isp->isp_respinrp = OUTMAILBOX5;
262                 isp->isp_respoutrp = INMAILBOX5;
263         }
264
265         /*
266          * Put the board into PAUSE mode (so we can read the SXP registers
267          * or write FPM/FBM registers).
268          */
269         if (IS_24XX(isp)) {
270                 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_HOST_INT);
271                 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RISC_INT);
272                 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_PAUSE);
273         } else {
274                 ISP_WRITE(isp, HCCR, HCCR_CMD_PAUSE);
275         }
276
277         if (IS_FC(isp)) {
278                 switch (isp->isp_type) {
279                 case ISP_HA_FC_2100:
280                         btype = "2100";
281                         break;
282                 case ISP_HA_FC_2200:
283                         btype = "2200";
284                         break;
285                 case ISP_HA_FC_2300:
286                         btype = "2300";
287                         break;
288                 case ISP_HA_FC_2312:
289                         btype = "2312";
290                         break;
291                 case ISP_HA_FC_2322:
292                         btype = "2322";
293                         break;
294                 case ISP_HA_FC_2400:
295                         btype = "2422";
296                         break;
297                 case ISP_HA_FC_2500:
298                         btype = "2532";
299                         break;
300                 default:
301                         break;
302                 }
303
304                 if (!IS_24XX(isp)) {
305                         /*
306                          * While we're paused, reset the FPM module and FBM
307                          * fifos.
308                          */
309                         ISP_WRITE(isp, BIU2100_CSR, BIU2100_FPM0_REGS);
310                         ISP_WRITE(isp, FPM_DIAG_CONFIG, FPM_SOFT_RESET);
311                         ISP_WRITE(isp, BIU2100_CSR, BIU2100_FB_REGS);
312                         ISP_WRITE(isp, FBM_CMD, FBMCMD_FIFO_RESET_ALL);
313                         ISP_WRITE(isp, BIU2100_CSR, BIU2100_RISC_REGS);
314                 }
315         } else if (IS_1240(isp)) {
316                 sdparam *sdp;
317
318                 btype = "1240";
319                 isp->isp_clock = 60;
320                 sdp = SDPARAM(isp, 0);
321                 sdp->isp_ultramode = 1;
322                 sdp = SDPARAM(isp, 1);
323                 sdp->isp_ultramode = 1;
324                 /*
325                  * XXX: Should probably do some bus sensing.
326                  */
327         } else if (IS_ULTRA3(isp)) {
328                 sdparam *sdp = isp->isp_param;
329
330                 isp->isp_clock = 100;
331
332                 if (IS_10160(isp))
333                         btype = "10160";
334                 else if (IS_12160(isp))
335                         btype = "12160";
336                 else
337                         btype = "<UNKLVD>";
338                 sdp->isp_lvdmode = 1;
339
340                 if (IS_DUALBUS(isp)) {
341                         sdp++;
342                         sdp->isp_lvdmode = 1;
343                 }
344         } else if (IS_ULTRA2(isp)) {
345                 static const char m[] = "bus %d is in %s Mode";
346                 uint16_t l;
347                 sdparam *sdp = SDPARAM(isp, 0);
348
349                 isp->isp_clock = 100;
350
351                 if (IS_1280(isp))
352                         btype = "1280";
353                 else if (IS_1080(isp))
354                         btype = "1080";
355                 else
356                         btype = "<UNKLVD>";
357
358                 l = ISP_READ(isp, SXP_PINS_DIFF) & ISP1080_MODE_MASK;
359                 switch (l) {
360                 case ISP1080_LVD_MODE:
361                         sdp->isp_lvdmode = 1;
362                         isp_prt(isp, ISP_LOGCONFIG, m, 0, "LVD");
363                         break;
364                 case ISP1080_HVD_MODE:
365                         sdp->isp_diffmode = 1;
366                         isp_prt(isp, ISP_LOGCONFIG, m, 0, "Differential");
367                         break;
368                 case ISP1080_SE_MODE:
369                         sdp->isp_ultramode = 1;
370                         isp_prt(isp, ISP_LOGCONFIG, m, 0, "Single-Ended");
371                         break;
372                 default:
373                         isp_prt(isp, ISP_LOGERR,
374                             "unknown mode on bus %d (0x%x)", 0, l);
375                         break;
376                 }
377
378                 if (IS_DUALBUS(isp)) {
379                         sdp = SDPARAM(isp, 1);
380                         l = ISP_READ(isp, SXP_PINS_DIFF|SXP_BANK1_SELECT);
381                         l &= ISP1080_MODE_MASK;
382                         switch (l) {
383                         case ISP1080_LVD_MODE:
384                                 sdp->isp_lvdmode = 1;
385                                 isp_prt(isp, ISP_LOGCONFIG, m, 1, "LVD");
386                                 break;
387                         case ISP1080_HVD_MODE:
388                                 sdp->isp_diffmode = 1;
389                                 isp_prt(isp, ISP_LOGCONFIG,
390                                     m, 1, "Differential");
391                                 break;
392                         case ISP1080_SE_MODE:
393                                 sdp->isp_ultramode = 1;
394                                 isp_prt(isp, ISP_LOGCONFIG,
395                                     m, 1, "Single-Ended");
396                                 break;
397                         default:
398                                 isp_prt(isp, ISP_LOGERR,
399                                     "unknown mode on bus %d (0x%x)", 1, l);
400                                 break;
401                         }
402                 }
403         } else {
404                 sdparam *sdp = SDPARAM(isp, 0);
405                 i = ISP_READ(isp, BIU_CONF0) & BIU_CONF0_HW_MASK;
406                 switch (i) {
407                 default:
408                         isp_prt(isp, ISP_LOGALL, "Unknown Chip Type 0x%x", i);
409                         /* FALLTHROUGH */
410                 case 1:
411                         btype = "1020";
412                         isp->isp_type = ISP_HA_SCSI_1020;
413                         isp->isp_clock = 40;
414                         break;
415                 case 2:
416                         /*
417                          * Some 1020A chips are Ultra Capable, but don't
418                          * run the clock rate up for that unless told to
419                          * do so by the Ultra Capable bits being set.
420                          */
421                         btype = "1020A";
422                         isp->isp_type = ISP_HA_SCSI_1020A;
423                         isp->isp_clock = 40;
424                         break;
425                 case 3:
426                         btype = "1040";
427                         isp->isp_type = ISP_HA_SCSI_1040;
428                         isp->isp_clock = 60;
429                         break;
430                 case 4:
431                         btype = "1040A";
432                         isp->isp_type = ISP_HA_SCSI_1040A;
433                         isp->isp_clock = 60;
434                         break;
435                 case 5:
436                         btype = "1040B";
437                         isp->isp_type = ISP_HA_SCSI_1040B;
438                         isp->isp_clock = 60;
439                         break;
440                 case 6:
441                         btype = "1040C";
442                         isp->isp_type = ISP_HA_SCSI_1040C;
443                         isp->isp_clock = 60;
444                         break;
445                 }
446                 /*
447                  * Now, while we're at it, gather info about ultra
448                  * and/or differential mode.
449                  */
450                 if (ISP_READ(isp, SXP_PINS_DIFF) & SXP_PINS_DIFF_MODE) {
451                         isp_prt(isp, ISP_LOGCONFIG, "Differential Mode");
452                         sdp->isp_diffmode = 1;
453                 } else {
454                         sdp->isp_diffmode = 0;
455                 }
456                 i = ISP_READ(isp, RISC_PSR);
457                 if (isp->isp_bustype == ISP_BT_SBUS) {
458                         i &= RISC_PSR_SBUS_ULTRA;
459                 } else {
460                         i &= RISC_PSR_PCI_ULTRA;
461                 }
462                 if (i != 0) {
463                         isp_prt(isp, ISP_LOGCONFIG, "Ultra Mode Capable");
464                         sdp->isp_ultramode = 1;
465                         /*
466                          * If we're in Ultra Mode, we have to be 60MHz clock-
467                          * even for the SBus version.
468                          */
469                         isp->isp_clock = 60;
470                 } else {
471                         sdp->isp_ultramode = 0;
472                         /*
473                          * Clock is known. Gronk.
474                          */
475                 }
476
477                 /*
478                  * Machine dependent clock (if set) overrides
479                  * our generic determinations.
480                  */
481                 if (isp->isp_mdvec->dv_clock) {
482                         if (isp->isp_mdvec->dv_clock < isp->isp_clock) {
483                                 isp->isp_clock = isp->isp_mdvec->dv_clock;
484                         }
485                 }
486
487         }
488
489         /*
490          * Clear instrumentation
491          */
492         isp->isp_intcnt = isp->isp_intbogus = 0;
493
494         /*
495          * Do MD specific pre initialization
496          */
497         ISP_RESET0(isp);
498
499         /*
500          * Hit the chip over the head with hammer,
501          * and give it a chance to recover.
502          */
503
504         if (IS_SCSI(isp)) {
505                 ISP_WRITE(isp, BIU_ICR, BIU_ICR_SOFT_RESET);
506                 /*
507                  * A slight delay...
508                  */
509                 ISP_DELAY(100);
510
511                 /*
512                  * Clear data && control DMA engines.
513                  */
514                 ISP_WRITE(isp, CDMA_CONTROL,
515                     DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
516                 ISP_WRITE(isp, DDMA_CONTROL,
517                     DMA_CNTRL_CLEAR_CHAN | DMA_CNTRL_RESET_INT);
518
519
520         } else if (IS_24XX(isp)) {
521                 /*
522                  * Stop DMA and wait for it to stop.
523                  */
524                 ISP_WRITE(isp, BIU2400_CSR, BIU2400_DMA_STOP|(3 << 4));
525                 for (val = loops = 0; loops < 30000; loops++) {
526                         ISP_DELAY(10);
527                         val = ISP_READ(isp, BIU2400_CSR);
528                         if ((val & BIU2400_DMA_ACTIVE) == 0) {
529                                 break;
530                         }
531                 }
532                 if (val & BIU2400_DMA_ACTIVE) {
533                         ISP_RESET0(isp);
534                         isp_prt(isp, ISP_LOGERR, "DMA Failed to Stop on Reset");
535                         return;
536                 }
537                 /*
538                  * Hold it in SOFT_RESET and STOP state for 100us.
539                  */
540                 ISP_WRITE(isp, BIU2400_CSR,
541                     BIU2400_SOFT_RESET|BIU2400_DMA_STOP|(3 << 4));
542                 ISP_DELAY(100);
543                 for (loops = 0; loops < 10000; loops++) {
544                         ISP_DELAY(5);
545                         val = ISP_READ(isp, OUTMAILBOX0);
546                 }
547                 for (val = loops = 0; loops < 500000; loops ++) {
548                         val = ISP_READ(isp, BIU2400_CSR);
549                         if ((val & BIU2400_SOFT_RESET) == 0) {
550                                 break;
551                         }
552                 }
553                 if (val & BIU2400_SOFT_RESET) {
554                         ISP_RESET0(isp);
555                         isp_prt(isp, ISP_LOGERR, "Failed to come out of reset");
556                         return;
557                 }
558         } else {
559                 ISP_WRITE(isp, BIU2100_CSR, BIU2100_SOFT_RESET);
560                 /*
561                  * A slight delay...
562                  */
563                 ISP_DELAY(100);
564
565                 /*
566                  * Clear data && control DMA engines.
567                  */
568                 ISP_WRITE(isp, CDMA2100_CONTROL,
569                         DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
570                 ISP_WRITE(isp, TDMA2100_CONTROL,
571                         DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
572                 ISP_WRITE(isp, RDMA2100_CONTROL,
573                         DMA_CNTRL2100_CLEAR_CHAN | DMA_CNTRL2100_RESET_INT);
574         }
575
576         /*
577          * Wait for ISP to be ready to go...
578          */
579         loops = MBOX_DELAY_COUNT;
580         for (;;) {
581                 if (IS_SCSI(isp)) {
582                         if (!(ISP_READ(isp, BIU_ICR) & BIU_ICR_SOFT_RESET)) {
583                                 break;
584                         }
585                 } else if (IS_24XX(isp)) {
586                         if (ISP_READ(isp, OUTMAILBOX0) == 0) {
587                                 break;
588                         }
589                 } else {
590                         if (!(ISP_READ(isp, BIU2100_CSR) & BIU2100_SOFT_RESET))
591                                 break;
592                 }
593                 ISP_DELAY(100);
594                 if (--loops < 0) {
595                         ISP_DUMPREGS(isp, "chip reset timed out");
596                         ISP_RESET0(isp);
597                         return;
598                 }
599         }
600
601         /*
602          * After we've fired this chip up, zero out the conf1 register
603          * for SCSI adapters and other settings for the 2100.
604          */
605
606         if (IS_SCSI(isp)) {
607                 ISP_WRITE(isp, BIU_CONF1, 0);
608         } else if (!IS_24XX(isp)) {
609                 ISP_WRITE(isp, BIU2100_CSR, 0);
610         }
611
612         /*
613          * Reset RISC Processor
614          */
615         if (IS_24XX(isp)) {
616                 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_RESET);
617                 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_RELEASE);
618                 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_CLEAR_RESET);
619         } else {
620                 ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
621                 ISP_DELAY(100);
622                 ISP_WRITE(isp, BIU_SEMA, 0);
623         }
624
625         /*
626          * Post-RISC Reset stuff.
627          */
628         if (IS_24XX(isp)) {
629                 for (val = loops = 0; loops < 5000000; loops++) {
630                         ISP_DELAY(5);
631                         val = ISP_READ(isp, OUTMAILBOX0);
632                         if (val == 0) {
633                                 break;
634                         }
635                 }
636                 if (val != 0) {
637                         ISP_RESET0(isp);
638                         isp_prt(isp, ISP_LOGERR, "reset didn't clear");
639                         return;
640                 }
641         } else if (IS_SCSI(isp)) {
642                 uint16_t tmp = isp->isp_mdvec->dv_conf1;
643                 /*
644                  * Busted FIFO. Turn off all but burst enables.
645                  */
646                 if (isp->isp_type == ISP_HA_SCSI_1040A) {
647                         tmp &= BIU_BURST_ENABLE;
648                 }
649                 ISP_SETBITS(isp, BIU_CONF1, tmp);
650                 if (tmp & BIU_BURST_ENABLE) {
651                         ISP_SETBITS(isp, CDMA_CONF, DMA_ENABLE_BURST);
652                         ISP_SETBITS(isp, DDMA_CONF, DMA_ENABLE_BURST);
653                 }
654                 if (SDPARAM(isp, 0)->isp_ptisp) {
655                         if (SDPARAM(isp, 0)->isp_ultramode) {
656                                 while (ISP_READ(isp, RISC_MTR) != 0x1313) {
657                                         ISP_WRITE(isp, RISC_MTR, 0x1313);
658                                         ISP_WRITE(isp, HCCR, HCCR_CMD_STEP);
659                                 }
660                         } else {
661                                 ISP_WRITE(isp, RISC_MTR, 0x1212);
662                         }
663                         /*
664                          * PTI specific register
665                          */
666                         ISP_WRITE(isp, RISC_EMB, DUAL_BANK);
667                 } else {
668                         ISP_WRITE(isp, RISC_MTR, 0x1212);
669                 }
670                 ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
671         } else {
672                 ISP_WRITE(isp, RISC_MTR2100, 0x1212);
673                 if (IS_2200(isp) || IS_23XX(isp)) {
674                         ISP_WRITE(isp, HCCR, HCCR_2X00_DISABLE_PARITY_PAUSE);
675                 }
676                 ISP_WRITE(isp, HCCR, HCCR_CMD_RELEASE);
677         }
678
679         ISP_WRITE(isp, isp->isp_rqstinrp, 0);
680         ISP_WRITE(isp, isp->isp_rqstoutrp, 0);
681         ISP_WRITE(isp, isp->isp_respinrp, 0);
682         ISP_WRITE(isp, isp->isp_respoutrp, 0);
683         if (IS_24XX(isp)) {
684                 ISP_WRITE(isp, BIU2400_PRI_REQINP, 0);
685                 ISP_WRITE(isp, BIU2400_PRI_REQOUTP, 0);
686                 ISP_WRITE(isp, BIU2400_ATIO_RSPINP, 0);
687                 ISP_WRITE(isp, BIU2400_ATIO_RSPOUTP, 0);
688         }
689
690         /*
691          * Do MD specific post initialization
692          */
693         ISP_RESET1(isp);
694
695         /*
696          * Wait for everything to finish firing up.
697          *
698          * Avoid doing this on early 2312s because you can generate a PCI
699          * parity error (chip breakage).
700          */
701         if (IS_2312(isp) && isp->isp_revision < 2) {
702                 ISP_DELAY(100);
703         } else {
704                 loops = MBOX_DELAY_COUNT;
705                 while (ISP_READ(isp, OUTMAILBOX0) == MBOX_BUSY) {
706                         ISP_DELAY(100);
707                         if (--loops < 0) {
708                                 ISP_RESET0(isp);
709                                 isp_prt(isp, ISP_LOGERR,
710                                     "MBOX_BUSY never cleared on reset");
711                                 return;
712                         }
713                 }
714         }
715
716         /*
717          * Up until this point we've done everything by just reading or
718          * setting registers. From this point on we rely on at least *some*
719          * kind of firmware running in the card.
720          */
721
722         /*
723          * Do some sanity checking by running a NOP command.
724          * If it succeeds, the ROM firmware is now running.
725          */
726         ISP_MEMZERO(&mbs, sizeof (mbs));
727         mbs.param[0] = MBOX_NO_OP;
728         mbs.logval = MBLOGALL;
729         isp_mboxcmd(isp, &mbs);
730         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
731                 isp_prt(isp, ISP_LOGERR, "NOP ommand failed (%x)",
732                     mbs.param[0]);
733                 ISP_RESET0(isp);
734                 return;
735         }
736
737         /*
738          * Do some operational tests
739          */
740
741         if (IS_SCSI(isp) || IS_24XX(isp)) {
742                 ISP_MEMZERO(&mbs, sizeof (mbs));
743                 mbs.param[0] = MBOX_MAILBOX_REG_TEST;
744                 mbs.param[1] = 0xdead;
745                 mbs.param[2] = 0xbeef;
746                 mbs.param[3] = 0xffff;
747                 mbs.param[4] = 0x1111;
748                 mbs.param[5] = 0xa5a5;
749                 mbs.param[6] = 0x0000;
750                 mbs.param[7] = 0x0000;
751                 mbs.logval = MBLOGALL;
752                 isp_mboxcmd(isp, &mbs);
753                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
754                         ISP_RESET0(isp);
755                         return;
756                 }
757                 if (mbs.param[1] != 0xdead || mbs.param[2] != 0xbeef ||
758                     mbs.param[3] != 0xffff || mbs.param[4] != 0x1111 ||
759                     mbs.param[5] != 0xa5a5) {
760                         ISP_RESET0(isp);
761                         isp_prt(isp, ISP_LOGERR,
762                             "Register Test Failed (0x%x 0x%x 0x%x 0x%x 0x%x)",
763                             mbs.param[1], mbs.param[2], mbs.param[3],
764                             mbs.param[4], mbs.param[5]);
765                         return;
766                 }
767
768         }
769
770         /*
771          * Download new Firmware, unless requested not to do so.
772          * This is made slightly trickier in some cases where the
773          * firmware of the ROM revision is newer than the revision
774          * compiled into the driver. So, where we used to compare
775          * versions of our f/w and the ROM f/w, now we just see
776          * whether we have f/w at all and whether a config flag
777          * has disabled our download.
778          */
779         if ((isp->isp_mdvec->dv_ispfw == NULL) ||
780             (isp->isp_confopts & ISP_CFG_NORELOAD)) {
781                 dodnld = 0;
782         }
783
784         if (IS_24XX(isp)) {
785                 code_org = ISP_CODE_ORG_2400;
786         } else if (IS_23XX(isp)) {
787                 code_org = ISP_CODE_ORG_2300;
788         } else {
789                 code_org = ISP_CODE_ORG;
790         }
791
792         if (dodnld && IS_24XX(isp)) {
793                 const uint32_t *ptr = isp->isp_mdvec->dv_ispfw;
794
795                 /*
796                  * Keep loading until we run out of f/w.
797                  */
798                 code_org = ptr[2];      /* 1st load address is our start addr */
799
800                 for (;;) {
801                         uint32_t la, wi, wl;
802
803                         isp_prt(isp, ISP_LOGDEBUG0,
804                             "load 0x%x words of code at load address 0x%x",
805                             ptr[3], ptr[2]);
806
807                         wi = 0;
808                         la = ptr[2];
809                         wl = ptr[3];
810
811                         while (wi < ptr[3]) {
812                                 uint32_t *cp;
813                                 uint32_t nw;
814
815                                 nw = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp)) >> 2;
816                                 if (nw > wl) {
817                                         nw = wl;
818                                 }
819                                 cp = isp->isp_rquest;
820                                 for (i = 0; i < nw; i++) {
821                                         ISP_IOXPUT_32(isp,  ptr[wi++], &cp[i]);
822                                         wl--;
823                                 }
824                                 MEMORYBARRIER(isp, SYNC_REQUEST,
825                                     0, ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp)));
826                                 ISP_MEMZERO(&mbs, sizeof (mbs));
827                                 mbs.param[0] = MBOX_LOAD_RISC_RAM;
828                                 mbs.param[1] = la;
829                                 mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
830                                 mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
831                                 mbs.param[4] = nw >> 16;
832                                 mbs.param[5] = nw;
833                                 mbs.param[6] = DMA_WD3(isp->isp_rquest_dma);
834                                 mbs.param[7] = DMA_WD2(isp->isp_rquest_dma);
835                                 mbs.param[8] = la >> 16;
836                                 mbs.logval = MBLOGALL;
837                                 isp_mboxcmd(isp, &mbs);
838                                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
839                                         isp_prt(isp, ISP_LOGERR,
840                                             "F/W Risc Ram Load Failed");
841                                         ISP_RESET0(isp);
842                                         return;
843                                 }
844                                 la += nw;
845                         }
846
847                         if (ptr[1] == 0) {
848                                 break;
849                         }
850                         ptr += ptr[3];
851                 }
852                 isp->isp_loaded_fw = 1;
853         } else if (dodnld && IS_23XX(isp)) {
854                 const uint16_t *ptr = isp->isp_mdvec->dv_ispfw;
855                 uint16_t wi, wl, segno;
856                 uint32_t la;
857
858                 la = code_org;
859                 segno = 0;
860
861                 for (;;) {
862                         uint32_t nxtaddr;
863
864                         isp_prt(isp, ISP_LOGDEBUG0,
865                             "load 0x%x words of code at load address 0x%x",
866                             ptr[3], la);
867
868                         wi = 0;
869                         wl = ptr[3];
870
871                         while (wi < ptr[3]) {
872                                 uint16_t *cp;
873                                 uint32_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,
888                                     0, ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp)));
889                                 ISP_MEMZERO(&mbs, sizeof (mbs));
890                                 mbs.param[0] = MBOX_LOAD_RISC_RAM;
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                                 mbs.param[8] = la >> 16;
898                                 mbs.logval = MBLOGALL;
899                                 isp_mboxcmd(isp, &mbs);
900                                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
901                                         isp_prt(isp, ISP_LOGERR,
902                                             "F/W Risc Ram Load Failed");
903                                         ISP_RESET0(isp);
904                                         return;
905                                 }
906                                 la += nw;
907                         }
908
909                         if (!IS_2322(isp)) {
910                                 break;
911                         }
912
913                         if (++segno == 3) {
914                                 break;
915                         }
916
917                         /*
918                          * If we're a 2322, the firmware actually comes in
919                          * three chunks. We loaded the first at the code_org
920                          * address. The other two chunks, which follow right
921                          * after each other in memory here, get loaded at
922                          * addresses specfied at offset 0x9..0xB.
923                          */
924
925                         nxtaddr = ptr[3];
926                         ptr = &ptr[nxtaddr];
927                         la = ptr[5] | ((ptr[4] & 0x3f) << 16);
928                 }
929                 isp->isp_loaded_fw = 1;
930         } else if (dodnld) {
931                 union {
932                         const uint16_t *cp;
933                         uint16_t *np;
934                 } ucd;
935                 ucd.cp = isp->isp_mdvec->dv_ispfw;
936                 isp->isp_mbxworkp = &ucd.np[1];
937                 isp->isp_mbxwrk0 = ucd.np[3] - 1;
938                 isp->isp_mbxwrk1 = code_org + 1;
939                 ISP_MEMZERO(&mbs, sizeof (mbs));
940                 mbs.param[0] = MBOX_WRITE_RAM_WORD;
941                 mbs.param[1] = code_org;
942                 mbs.param[2] = ucd.np[0];
943                 mbs.logval = MBLOGNONE;
944                 isp_mboxcmd(isp, &mbs);
945                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
946                         isp_prt(isp, ISP_LOGERR,
947                             "F/W download failed at word %d",
948                             isp->isp_mbxwrk1 - code_org);
949                         ISP_RESET0(isp);
950                         return;
951                 }
952         } else {
953                 isp->isp_loaded_fw = 0;
954                 isp_prt(isp, ISP_LOGDEBUG2, "skipping f/w download");
955         }
956
957         /*
958          * If we loaded firmware, verify its checksum
959          */
960         if (isp->isp_loaded_fw) {
961                 ISP_MEMZERO(&mbs, sizeof (mbs));
962                 mbs.param[0] = MBOX_VERIFY_CHECKSUM;
963                 if (IS_24XX(isp)) {
964                         mbs.param[1] = code_org >> 16;
965                         mbs.param[2] = code_org;
966                 } else {
967                         mbs.param[1] = code_org;
968                 }
969                 isp_mboxcmd(isp, &mbs);
970                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
971                         isp_prt(isp, ISP_LOGERR, dcrc);
972                         ISP_RESET0(isp);
973                         return;
974                 }
975         }
976
977         /*
978          * Now start it rolling.
979          *
980          * If we didn't actually download f/w,
981          * we still need to (re)start it.
982          */
983
984
985         MBSINIT(&mbs, MBOX_EXEC_FIRMWARE, MBLOGALL, 1000000);
986         if (IS_24XX(isp)) {
987                 mbs.param[1] = code_org >> 16;
988                 mbs.param[2] = code_org;
989                 if (isp->isp_loaded_fw) {
990                         mbs.param[3] = 0;
991                 } else {
992                         mbs.param[3] = 1;
993                 }
994                 if (IS_25XX(isp)) {
995                         mbs.ibits |= 0x10;
996                 }
997         } else if (IS_2322(isp)) {
998                 mbs.param[1] = code_org;
999                 if (isp->isp_loaded_fw) {
1000                         mbs.param[2] = 0;
1001                 } else {
1002                         mbs.param[2] = 1;
1003                 }
1004         } else {
1005                 mbs.param[1] = code_org;
1006         }
1007         isp_mboxcmd(isp, &mbs);
1008         if (IS_2322(isp) || IS_24XX(isp)) {
1009                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1010                         ISP_RESET0(isp);
1011                         return;
1012                 }
1013         }
1014
1015         /*
1016          * Give it a chance to finish starting up.
1017          * Give the 24XX more time.
1018          */
1019         if (IS_24XX(isp)) {
1020                 ISP_DELAY(500000);
1021                 /*
1022                  * Check to see if the 24XX firmware really started.
1023                  */
1024                 if (mbs.param[1] == 0xdead) {
1025                         isp_prt(isp, ISP_LOGERR, "f/w didn't *really* start");
1026                         ISP_RESET0(isp);
1027                         return;
1028                 }
1029         } else {
1030                 ISP_DELAY(250000);
1031                 if (IS_SCSI(isp)) {
1032                         /*
1033                          * Set CLOCK RATE, but only if asked to.
1034                          */
1035                         if (isp->isp_clock) {
1036                                 mbs.param[0] = MBOX_SET_CLOCK_RATE;
1037                                 mbs.param[1] = isp->isp_clock;
1038                                 mbs.logval = MBLOGNONE;
1039                                 isp_mboxcmd(isp, &mbs);
1040                                 /* we will try not to care if this fails */
1041                         }
1042                 }
1043         }
1044
1045         /*
1046          * Ask the chip for the current firmware version.
1047          * This should prove that the new firmware is working.
1048          */
1049         MBSINIT(&mbs, MBOX_ABOUT_FIRMWARE, MBLOGALL, 0);
1050         isp_mboxcmd(isp, &mbs);
1051         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1052                 ISP_RESET0(isp);
1053                 return;
1054         }
1055
1056         /*
1057          * The SBus firmware that we are using apparently does not return
1058          * major, minor, micro revisions in the mailbox registers, which
1059          * is really, really, annoying.
1060          */
1061         if (ISP_SBUS_SUPPORTED && isp->isp_bustype == ISP_BT_SBUS) {
1062                 if (dodnld) {
1063 #ifdef  ISP_TARGET_MODE
1064                         isp->isp_fwrev[0] = 7;
1065                         isp->isp_fwrev[1] = 55;
1066 #else
1067                         isp->isp_fwrev[0] = 1;
1068                         isp->isp_fwrev[1] = 37;
1069 #endif
1070                         isp->isp_fwrev[2] = 0;
1071                 }
1072         } else {
1073                 isp->isp_fwrev[0] = mbs.param[1];
1074                 isp->isp_fwrev[1] = mbs.param[2];
1075                 isp->isp_fwrev[2] = mbs.param[3];
1076         }
1077
1078         isp_prt(isp, ISP_LOGCONFIG,
1079             "Board Type %s, Chip Revision 0x%x, %s F/W Revision %d.%d.%d",
1080             btype, isp->isp_revision, dodnld? "loaded" : "resident",
1081             isp->isp_fwrev[0], isp->isp_fwrev[1], isp->isp_fwrev[2]);
1082
1083         if (IS_FC(isp)) {
1084                 /*
1085                  * We do not believe firmware attributes for 2100 code less
1086                  * than 1.17.0, unless it's the firmware we specifically
1087                  * are loading.
1088                  *
1089                  * Note that all 22XX and later f/w is greater than 1.X.0.
1090                  */
1091                 if ((ISP_FW_OLDER_THAN(isp, 1, 17, 1))) {
1092 #ifdef  USE_SMALLER_2100_FIRMWARE
1093                         isp->isp_fwattr = ISP_FW_ATTR_SCCLUN;
1094 #else
1095                         isp->isp_fwattr = 0;
1096 #endif
1097                 } else {
1098                         isp->isp_fwattr = mbs.param[6];
1099                         isp_prt(isp, ISP_LOGDEBUG0,
1100                             "Firmware Attributes = 0x%x", mbs.param[6]);
1101                 }
1102         } else {
1103 #ifndef ISP_TARGET_MODE
1104                 isp->isp_fwattr = ISP_FW_ATTR_TMODE;
1105 #else
1106                 isp->isp_fwattr = 0;
1107 #endif
1108         }
1109
1110         if (!IS_24XX(isp)) {
1111                 MBSINIT(&mbs, MBOX_GET_FIRMWARE_STATUS, MBLOGALL, 0);
1112                 isp_mboxcmd(isp, &mbs);
1113                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1114                         ISP_RESET0(isp);
1115                         return;
1116                 }
1117                 if (isp->isp_maxcmds >= mbs.param[2]) {
1118                         isp->isp_maxcmds = mbs.param[2];
1119                 }
1120         }
1121         isp_prt(isp, ISP_LOGCONFIG,
1122             "%d max I/O command limit set", isp->isp_maxcmds);
1123         for (i = 0; i < isp->isp_nchan; i++) {
1124                 isp_fw_state(isp, i);
1125         }
1126         if (isp->isp_dead) {
1127                 isp_shutdown(isp);
1128                 ISP_DISABLE_INTS(isp);
1129                 return;
1130         }
1131
1132         isp->isp_state = ISP_RESETSTATE;
1133
1134         /*
1135          * Okay- now that we have new firmware running, we now (re)set our
1136          * notion of how many luns we support. This is somewhat tricky because
1137          * if we haven't loaded firmware, we sometimes do not have an easy way
1138          * of knowing how many luns we support.
1139          *
1140          * Expanded lun firmware gives you 32 luns for SCSI cards and
1141          * 16384 luns for Fibre Channel cards.
1142          *
1143          * It turns out that even for QLogic 2100s with ROM 1.10 and above
1144          * we do get a firmware attributes word returned in mailbox register 6.
1145          *
1146          * Because the lun is in a different position in the Request Queue
1147          * Entry structure for Fibre Channel with expanded lun firmware, we
1148          * can only support one lun (lun zero) when we don't know what kind
1149          * of firmware we're running.
1150          */
1151         if (IS_SCSI(isp)) {
1152                 if (dodnld) {
1153                         if (IS_ULTRA2(isp) || IS_ULTRA3(isp)) {
1154                                 isp->isp_maxluns = 32;
1155                         } else {
1156                                 isp->isp_maxluns = 8;
1157                         }
1158                 } else {
1159                         isp->isp_maxluns = 8;
1160                 }
1161         } else {
1162                 if (ISP_CAP_SCCFW(isp)) {
1163                         isp->isp_maxluns = 16384;
1164                 } else {
1165                         isp->isp_maxluns = 16;
1166                 }
1167         }
1168
1169         /*
1170          * We get some default values established. As a side
1171          * effect, NVRAM is read here (unless overriden by
1172          * a configuration flag).
1173          */
1174         if (IS_SCSI(isp)) {
1175                 isp_setdfltsdparm(isp);
1176         } else {
1177                 for (i = 0; i < isp->isp_nchan; i++) {
1178                         isp_setdfltfcparm(isp, i);
1179                 }
1180         }
1181
1182 }
1183
1184 /*
1185  * Initialize Parameters of Hardware to a known state.
1186  *
1187  * Locks are held before coming here.
1188  */
1189
1190 void
1191 isp_init(ispsoftc_t *isp)
1192 {
1193         if (IS_FC(isp)) {
1194                 if (IS_24XX(isp)) {
1195                         isp_fibre_init_2400(isp);
1196                 } else {
1197                         isp_fibre_init(isp);
1198                 }
1199         } else {
1200                 isp_scsi_init(isp);
1201         }
1202 }
1203
1204 static void
1205 isp_scsi_init(ispsoftc_t *isp)
1206 {
1207         sdparam *sdp_chan0, *sdp_chan1;
1208         mbreg_t mbs;
1209
1210         sdp_chan0 = SDPARAM(isp, 0);
1211         sdp_chan1 = sdp_chan0;
1212         if (IS_DUALBUS(isp)) {
1213                 sdp_chan1 = SDPARAM(isp, 1);
1214         }
1215
1216         /* First do overall per-card settings. */
1217
1218         /*
1219          * If we have fast memory timing enabled, turn it on.
1220          */
1221         if (sdp_chan0->isp_fast_mttr) {
1222                 ISP_WRITE(isp, RISC_MTR, 0x1313);
1223         }
1224
1225         /*
1226          * Set Retry Delay and Count.
1227          * You set both channels at the same time.
1228          */
1229         MBSINIT(&mbs, MBOX_SET_RETRY_COUNT, MBLOGALL, 0);
1230         mbs.param[1] = sdp_chan0->isp_retry_count;
1231         mbs.param[2] = sdp_chan0->isp_retry_delay;
1232         mbs.param[6] = sdp_chan1->isp_retry_count;
1233         mbs.param[7] = sdp_chan1->isp_retry_delay;
1234         isp_mboxcmd(isp, &mbs);
1235         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1236                 return;
1237         }
1238
1239         /*
1240          * Set ASYNC DATA SETUP time. This is very important.
1241          */
1242         MBSINIT(&mbs, MBOX_SET_ASYNC_DATA_SETUP_TIME, MBLOGALL, 0);
1243         mbs.param[1] = sdp_chan0->isp_async_data_setup;
1244         mbs.param[2] = sdp_chan1->isp_async_data_setup;
1245         isp_mboxcmd(isp, &mbs);
1246         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1247                 return;
1248         }
1249
1250         /*
1251          * Set ACTIVE Negation State.
1252          */
1253         MBSINIT(&mbs, MBOX_SET_ACT_NEG_STATE, MBLOGNONE, 0);
1254         mbs.param[1] =
1255             (sdp_chan0->isp_req_ack_active_neg << 4) |
1256             (sdp_chan0->isp_data_line_active_neg << 5);
1257         mbs.param[2] =
1258             (sdp_chan1->isp_req_ack_active_neg << 4) |
1259             (sdp_chan1->isp_data_line_active_neg << 5);
1260         isp_mboxcmd(isp, &mbs);
1261         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1262                 isp_prt(isp, ISP_LOGERR,
1263                     "failed to set active negation state (%d,%d), (%d,%d)",
1264                     sdp_chan0->isp_req_ack_active_neg,
1265                     sdp_chan0->isp_data_line_active_neg,
1266                     sdp_chan1->isp_req_ack_active_neg,
1267                     sdp_chan1->isp_data_line_active_neg);
1268                 /*
1269                  * But don't return.
1270                  */
1271         }
1272
1273         /*
1274          * Set the Tag Aging limit
1275          */
1276         MBSINIT(&mbs, MBOX_SET_TAG_AGE_LIMIT, MBLOGALL, 0);
1277         mbs.param[1] = sdp_chan0->isp_tag_aging;
1278         mbs.param[2] = sdp_chan1->isp_tag_aging;
1279         isp_mboxcmd(isp, &mbs);
1280         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1281                 isp_prt(isp, ISP_LOGERR, "failed to set tag age limit (%d,%d)",
1282                     sdp_chan0->isp_tag_aging, sdp_chan1->isp_tag_aging);
1283                 return;
1284         }
1285
1286         /*
1287          * Set selection timeout.
1288          */
1289         MBSINIT(&mbs, MBOX_SET_SELECT_TIMEOUT, MBLOGALL, 0);
1290         mbs.param[1] = sdp_chan0->isp_selection_timeout;
1291         mbs.param[2] = sdp_chan1->isp_selection_timeout;
1292         isp_mboxcmd(isp, &mbs);
1293         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1294                 return;
1295         }
1296
1297         /* now do per-channel settings */
1298         isp_scsi_channel_init(isp, 0);
1299         if (IS_DUALBUS(isp))
1300                 isp_scsi_channel_init(isp, 1);
1301
1302         /*
1303          * Now enable request/response queues
1304          */
1305
1306         if (IS_ULTRA2(isp) || IS_1240(isp)) {
1307                 MBSINIT(&mbs, MBOX_INIT_RES_QUEUE_A64, MBLOGALL, 0);
1308                 mbs.param[1] = RESULT_QUEUE_LEN(isp);
1309                 mbs.param[2] = DMA_WD1(isp->isp_result_dma);
1310                 mbs.param[3] = DMA_WD0(isp->isp_result_dma);
1311                 mbs.param[4] = 0;
1312                 mbs.param[6] = DMA_WD3(isp->isp_result_dma);
1313                 mbs.param[7] = DMA_WD2(isp->isp_result_dma);
1314                 isp_mboxcmd(isp, &mbs);
1315                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1316                         return;
1317                 }
1318                 isp->isp_residx = mbs.param[5];
1319
1320                 MBSINIT(&mbs, MBOX_INIT_REQ_QUEUE_A64, MBLOGALL, 0);
1321                 mbs.param[1] = RQUEST_QUEUE_LEN(isp);
1322                 mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
1323                 mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
1324                 mbs.param[5] = 0;
1325                 mbs.param[6] = DMA_WD3(isp->isp_result_dma);
1326                 mbs.param[7] = DMA_WD2(isp->isp_result_dma);
1327                 isp_mboxcmd(isp, &mbs);
1328                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1329                         return;
1330                 }
1331                 isp->isp_reqidx = isp->isp_reqodx = mbs.param[4];
1332         } else {
1333                 MBSINIT(&mbs, MBOX_INIT_RES_QUEUE, MBLOGALL, 0);
1334                 mbs.param[1] = RESULT_QUEUE_LEN(isp);
1335                 mbs.param[2] = DMA_WD1(isp->isp_result_dma);
1336                 mbs.param[3] = DMA_WD0(isp->isp_result_dma);
1337                 mbs.param[4] = 0;
1338                 isp_mboxcmd(isp, &mbs);
1339                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1340                         return;
1341                 }
1342                 isp->isp_residx = mbs.param[5];
1343
1344                 MBSINIT(&mbs, MBOX_INIT_REQ_QUEUE, MBLOGALL, 0);
1345                 mbs.param[1] = RQUEST_QUEUE_LEN(isp);
1346                 mbs.param[2] = DMA_WD1(isp->isp_rquest_dma);
1347                 mbs.param[3] = DMA_WD0(isp->isp_rquest_dma);
1348                 mbs.param[5] = 0;
1349                 isp_mboxcmd(isp, &mbs);
1350                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1351                         return;
1352                 }
1353                 isp->isp_reqidx = isp->isp_reqodx = mbs.param[4];
1354         }
1355
1356         /*
1357          * Turn on Fast Posting, LVD transitions
1358          *
1359          * Ultra2 F/W always has had fast posting (and LVD transitions)
1360          *
1361          * Ultra and older (i.e., SBus) cards may not. It's just safer
1362          * to assume not for them.
1363          */
1364
1365         MBSINIT(&mbs, MBOX_SET_FW_FEATURES, MBLOGALL, 0);
1366         if (IS_ULTRA2(isp))
1367                 mbs.param[1] |= FW_FEATURE_LVD_NOTIFY;
1368 #ifndef ISP_NO_RIO
1369         if (IS_ULTRA2(isp) || IS_1240(isp))
1370                 mbs.param[1] |= FW_FEATURE_RIO_16BIT;
1371 #else
1372         if (IS_ULTRA2(isp) || IS_1240(isp))
1373                 mbs.param[1] |= FW_FEATURE_FAST_POST;
1374 #endif
1375         if (mbs.param[1] != 0) {
1376                 uint16_t sfeat = mbs.param[1];
1377                 isp_mboxcmd(isp, &mbs);
1378                 if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
1379                         isp_prt(isp, ISP_LOGINFO,
1380                             "Enabled FW features (0x%x)", sfeat);
1381                 }
1382         }
1383
1384         isp->isp_state = ISP_INITSTATE;
1385 }
1386
1387 static void
1388 isp_scsi_channel_init(ispsoftc_t *isp, int chan)
1389 {
1390         sdparam *sdp;
1391         mbreg_t mbs;
1392         int tgt;
1393
1394         sdp = SDPARAM(isp, chan);
1395
1396         /*
1397          * Set (possibly new) Initiator ID.
1398          */
1399         MBSINIT(&mbs, MBOX_SET_INIT_SCSI_ID, MBLOGALL, 0);
1400         mbs.param[1] = (chan << 7) | sdp->isp_initiator_id;
1401         isp_mboxcmd(isp, &mbs);
1402         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1403                 return;
1404         }
1405         isp_prt(isp, ISP_LOGINFO, "Chan %d Initiator ID is %d",
1406             chan, sdp->isp_initiator_id);
1407
1408
1409         /*
1410          * Set current per-target parameters to an initial safe minimum.
1411          */
1412         for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
1413                 int lun;
1414                 uint16_t sdf;
1415
1416                 if (sdp->isp_devparam[tgt].dev_enable == 0) {
1417                         continue;
1418                 }
1419 #ifndef ISP_TARGET_MODE
1420                 sdf = sdp->isp_devparam[tgt].goal_flags;
1421                 sdf &= DPARM_SAFE_DFLT;
1422                 /*
1423                  * It is not quite clear when this changed over so that
1424                  * we could force narrow and async for 1000/1020 cards,
1425                  * but assume that this is only the case for loaded
1426                  * firmware.
1427                  */
1428                 if (isp->isp_loaded_fw) {
1429                         sdf |= DPARM_NARROW | DPARM_ASYNC;
1430                 }
1431 #else
1432                 /*
1433                  * The !$*!)$!$)* f/w uses the same index into some
1434                  * internal table to decide how to respond to negotiations,
1435                  * so if we've said "let's be safe" for ID X, and ID X
1436                  * selects *us*, the negotiations will back to 'safe'
1437                  * (as in narrow/async). What the f/w *should* do is
1438                  * use the initiator id settings to decide how to respond.
1439                  */
1440                 sdp->isp_devparam[tgt].goal_flags = sdf = DPARM_DEFAULT;
1441 #endif
1442                 MBSINIT(&mbs, MBOX_SET_TARGET_PARAMS, MBLOGNONE, 0);
1443                 mbs.param[1] = (chan << 15) | (tgt << 8);
1444                 mbs.param[2] = sdf;
1445                 if ((sdf & DPARM_SYNC) == 0) {
1446                         mbs.param[3] = 0;
1447                 } else {
1448                         mbs.param[3] =
1449                             (sdp->isp_devparam[tgt].goal_offset << 8) |
1450                             (sdp->isp_devparam[tgt].goal_period);
1451                 }
1452                 isp_prt(isp, ISP_LOGDEBUG0,
1453                     "Initial Settings bus%d tgt%d flags 0x%x off 0x%x per 0x%x",
1454                     chan, tgt, mbs.param[2], mbs.param[3] >> 8,
1455                     mbs.param[3] & 0xff);
1456                 isp_mboxcmd(isp, &mbs);
1457                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1458                         sdf = DPARM_SAFE_DFLT;
1459                         MBSINIT(&mbs, MBOX_SET_TARGET_PARAMS, MBLOGALL, 0);
1460                         mbs.param[1] = (tgt << 8) | (chan << 15);
1461                         mbs.param[2] = sdf;
1462                         mbs.param[3] = 0;
1463                         isp_mboxcmd(isp, &mbs);
1464                         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1465                                 continue;
1466                         }
1467                 }
1468
1469                 /*
1470                  * We don't update any information directly from the f/w
1471                  * because we need to run at least one command to cause a
1472                  * new state to be latched up. So, we just assume that we
1473                  * converge to the values we just had set.
1474                  *
1475                  * Ensure that we don't believe tagged queuing is enabled yet.
1476                  * It turns out that sometimes the ISP just ignores our
1477                  * attempts to set parameters for devices that it hasn't
1478                  * seen yet.
1479                  */
1480                 sdp->isp_devparam[tgt].actv_flags = sdf & ~DPARM_TQING;
1481                 for (lun = 0; lun < (int) isp->isp_maxluns; lun++) {
1482                         MBSINIT(&mbs, MBOX_SET_DEV_QUEUE_PARAMS, MBLOGALL, 0);
1483                         mbs.param[1] = (chan << 15) | (tgt << 8) | lun;
1484                         mbs.param[2] = sdp->isp_max_queue_depth;
1485                         mbs.param[3] = sdp->isp_devparam[tgt].exc_throttle;
1486                         isp_mboxcmd(isp, &mbs);
1487                         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1488                                 break;
1489                         }
1490                 }
1491         }
1492         for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
1493                 if (sdp->isp_devparam[tgt].dev_refresh) {
1494                         sdp->sendmarker = 1;
1495                         sdp->update = 1;
1496                         break;
1497                 }
1498         }
1499 }
1500
1501 /*
1502  * Fibre Channel specific initialization.
1503  */
1504 static void
1505 isp_fibre_init(ispsoftc_t *isp)
1506 {
1507         fcparam *fcp;
1508         isp_icb_t local, *icbp = &local;
1509         mbreg_t mbs;
1510         int ownloopid;
1511
1512         /*
1513          * We only support one channel on non-24XX cards
1514          */
1515         fcp = FCPARAM(isp, 0);
1516         if (fcp->role == ISP_ROLE_NONE) {
1517                 isp->isp_state = ISP_INITSTATE;
1518                 return;
1519         }
1520
1521         ISP_MEMZERO(icbp, sizeof (*icbp));
1522         icbp->icb_version = ICB_VERSION1;
1523         icbp->icb_fwoptions = fcp->isp_fwoptions;
1524
1525         /*
1526          * Firmware Options are either retrieved from NVRAM or
1527          * are patched elsewhere. We check them for sanity here
1528          * and make changes based on board revision, but otherwise
1529          * let others decide policy.
1530          */
1531
1532         /*
1533          * If this is a 2100 < revision 5, we have to turn off FAIRNESS.
1534          */
1535         if (IS_2100(isp) && isp->isp_revision < 5) {
1536                 icbp->icb_fwoptions &= ~ICBOPT_FAIRNESS;
1537         }
1538
1539         /*
1540          * We have to use FULL LOGIN even though it resets the loop too much
1541          * because otherwise port database entries don't get updated after
1542          * a LIP- this is a known f/w bug for 2100 f/w less than 1.17.0.
1543          */
1544         if (!ISP_FW_NEWER_THAN(isp, 1, 17, 0)) {
1545                 icbp->icb_fwoptions |= ICBOPT_FULL_LOGIN;
1546         }
1547
1548         /*
1549          * Insist on Port Database Update Async notifications
1550          */
1551         icbp->icb_fwoptions |= ICBOPT_PDBCHANGE_AE;
1552
1553         /*
1554          * Make sure that target role reflects into fwoptions.
1555          */
1556         if (fcp->role & ISP_ROLE_TARGET) {
1557                 icbp->icb_fwoptions |= ICBOPT_TGT_ENABLE;
1558         } else {
1559                 icbp->icb_fwoptions &= ~ICBOPT_TGT_ENABLE;
1560         }
1561
1562         if (fcp->role & ISP_ROLE_INITIATOR) {
1563                 icbp->icb_fwoptions &= ~ICBOPT_INI_DISABLE;
1564         } else {
1565                 icbp->icb_fwoptions |= ICBOPT_INI_DISABLE;
1566         }
1567
1568         icbp->icb_maxfrmlen = DEFAULT_FRAMESIZE(isp);
1569         if (icbp->icb_maxfrmlen < ICB_MIN_FRMLEN ||
1570             icbp->icb_maxfrmlen > ICB_MAX_FRMLEN) {
1571                 isp_prt(isp, ISP_LOGERR,
1572                     "bad frame length (%d) from NVRAM- using %d",
1573                     DEFAULT_FRAMESIZE(isp), ICB_DFLT_FRMLEN);
1574                 icbp->icb_maxfrmlen = ICB_DFLT_FRMLEN;
1575         }
1576         icbp->icb_maxalloc = fcp->isp_maxalloc;
1577         if (icbp->icb_maxalloc < 1) {
1578                 isp_prt(isp, ISP_LOGERR,
1579                     "bad maximum allocation (%d)- using 16", fcp->isp_maxalloc);
1580                 icbp->icb_maxalloc = 16;
1581         }
1582         icbp->icb_execthrottle = DEFAULT_EXEC_THROTTLE(isp);
1583         if (icbp->icb_execthrottle < 1) {
1584                 isp_prt(isp, ISP_LOGERR,
1585                     "bad execution throttle of %d- using %d",
1586                     DEFAULT_EXEC_THROTTLE(isp), ICB_DFLT_THROTTLE);
1587                 icbp->icb_execthrottle = ICB_DFLT_THROTTLE;
1588         }
1589         icbp->icb_retry_delay = fcp->isp_retry_delay;
1590         icbp->icb_retry_count = fcp->isp_retry_count;
1591         icbp->icb_hardaddr = fcp->isp_loopid;
1592         ownloopid = (isp->isp_confopts & ISP_CFG_OWNLOOPID) != 0;
1593         if (icbp->icb_hardaddr >= LOCAL_LOOP_LIM) {
1594                 icbp->icb_hardaddr = 0;
1595                 ownloopid = 0;
1596         }
1597
1598         /*
1599          * Our life seems so much better with 2200s and later with
1600          * the latest f/w if we set Hard Address.
1601          */
1602         if (ownloopid || ISP_FW_NEWER_THAN(isp, 2, 2, 5)) {
1603                 icbp->icb_fwoptions |= ICBOPT_HARD_ADDRESS;
1604         }
1605
1606         /*
1607          * Right now we just set extended options to prefer point-to-point
1608          * over loop based upon some soft config options.
1609          *
1610          * NB: for the 2300, ICBOPT_EXTENDED is required.
1611          */
1612         if (IS_2200(isp) || IS_23XX(isp)) {
1613                 icbp->icb_fwoptions |= ICBOPT_EXTENDED;
1614                 /*
1615                  * Prefer or force Point-To-Point instead Loop?
1616                  */
1617                 switch (isp->isp_confopts & ISP_CFG_PORT_PREF) {
1618                 case ISP_CFG_NPORT:
1619                         icbp->icb_xfwoptions |= ICBXOPT_PTP_2_LOOP;
1620                         break;
1621                 case ISP_CFG_NPORT_ONLY:
1622                         icbp->icb_xfwoptions |= ICBXOPT_PTP_ONLY;
1623                         break;
1624                 case ISP_CFG_LPORT_ONLY:
1625                         icbp->icb_xfwoptions |= ICBXOPT_LOOP_ONLY;
1626                         break;
1627                 default:
1628                         icbp->icb_xfwoptions |= ICBXOPT_LOOP_2_PTP;
1629                         break;
1630                 }
1631                 if (IS_2200(isp)) {
1632                         /*
1633                          * There seems to just be too much breakage here
1634                          * with RIO and Fast Posting- it probably actually
1635                          * works okay but this driver is messing it up.
1636                          * This card is really ancient by now, so let's
1637                          * just opt for safety and not use the feature.
1638                          */
1639 #if     0
1640                         if (ISP_FW_NEWER_THAN(isp, 1, 17, 0)) {
1641                                 icbp->icb_xfwoptions |= ICBXOPT_RIO_16BIT;
1642                                 icbp->icb_fwoptions &= ~ICBOPT_FAST_POST;
1643                                 icbp->icb_racctimer = 4;
1644                                 icbp->icb_idelaytimer = 8;
1645                         } else {
1646                                 icbp->icb_fwoptions |= ICBOPT_FAST_POST;
1647                         }
1648 #else
1649                         icbp->icb_xfwoptions &= ~ICBXOPT_RIO_16BIT;
1650                         icbp->icb_fwoptions &= ~ICBOPT_FAST_POST;
1651 #endif
1652                 } else {
1653                         /*
1654                          * QLogic recommends that FAST Posting be turned
1655                          * off for 23XX cards and instead allow the HBA
1656                          * to write response queue entries and interrupt
1657                          * after a delay (ZIO).
1658                          */
1659                         icbp->icb_fwoptions &= ~ICBOPT_FAST_POST;
1660                         if ((fcp->isp_xfwoptions & ICBXOPT_TIMER_MASK) ==
1661                             ICBXOPT_ZIO) {
1662                                 icbp->icb_xfwoptions |= ICBXOPT_ZIO;
1663                                 icbp->icb_idelaytimer = 10;
1664                         }
1665                         if (isp->isp_confopts & ISP_CFG_ONEGB) {
1666                                 icbp->icb_zfwoptions |= ICBZOPT_RATE_ONEGB;
1667                         } else if (isp->isp_confopts & ISP_CFG_TWOGB) {
1668                                 icbp->icb_zfwoptions |= ICBZOPT_RATE_TWOGB;
1669                         } else {
1670                                 icbp->icb_zfwoptions |= ICBZOPT_RATE_AUTO;
1671                         }
1672                         if (fcp->isp_zfwoptions & ICBZOPT_50_OHM) {
1673                                 icbp->icb_zfwoptions |= ICBZOPT_50_OHM;
1674                         }
1675                 }
1676         }
1677
1678
1679         /*
1680          * For 22XX > 2.1.26 && 23XX, set some options.
1681          * XXX: Probably okay for newer 2100 f/w too.
1682          */
1683         if (ISP_FW_NEWER_THAN(isp, 2, 26, 0)) {
1684                 /*
1685                  * Turn on LIP F8 async event (1)
1686                  * Turn on generate AE 8013 on all LIP Resets (2)
1687                  * Disable LIP F7 switching (8)
1688                  */
1689                 MBSINIT(&mbs, MBOX_SET_FIRMWARE_OPTIONS, MBLOGALL, 0);
1690                 mbs.param[1] = 0xb;
1691                 mbs.param[2] = 0;
1692                 mbs.param[3] = 0;
1693                 isp_mboxcmd(isp, &mbs);
1694                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1695                         return;
1696                 }
1697         }
1698         icbp->icb_logintime = ICB_LOGIN_TOV;
1699         icbp->icb_lunetimeout = ICB_LUN_ENABLE_TOV;
1700
1701         if (fcp->isp_wwnn && fcp->isp_wwpn && (fcp->isp_wwnn >> 60) != 2) {
1702                 icbp->icb_fwoptions |= ICBOPT_BOTH_WWNS;
1703                 MAKE_NODE_NAME_FROM_WWN(icbp->icb_nodename, fcp->isp_wwnn);
1704                 MAKE_NODE_NAME_FROM_WWN(icbp->icb_portname, fcp->isp_wwpn);
1705                 isp_prt(isp, ISP_LOGDEBUG1,
1706                     "Setting ICB Node 0x%08x%08x Port 0x%08x%08x",
1707                     ((uint32_t) (fcp->isp_wwnn >> 32)),
1708                     ((uint32_t) (fcp->isp_wwnn)),
1709                     ((uint32_t) (fcp->isp_wwpn >> 32)),
1710                     ((uint32_t) (fcp->isp_wwpn)));
1711         } else if (fcp->isp_wwpn) {
1712                 icbp->icb_fwoptions &= ~ICBOPT_BOTH_WWNS;
1713                 MAKE_NODE_NAME_FROM_WWN(icbp->icb_portname, fcp->isp_wwpn);
1714                 isp_prt(isp, ISP_LOGDEBUG1,
1715                     "Setting ICB Port 0x%08x%08x",
1716                     ((uint32_t) (fcp->isp_wwpn >> 32)),
1717                     ((uint32_t) (fcp->isp_wwpn)));
1718         } else {
1719                 isp_prt(isp, ISP_LOGERR, "No valid WWNs to use");
1720                 return;
1721         }
1722         icbp->icb_rqstqlen = RQUEST_QUEUE_LEN(isp);
1723         if (icbp->icb_rqstqlen < 1) {
1724                 isp_prt(isp, ISP_LOGERR, "bad request queue length");
1725         }
1726         icbp->icb_rsltqlen = RESULT_QUEUE_LEN(isp);
1727         if (icbp->icb_rsltqlen < 1) {
1728                 isp_prt(isp, ISP_LOGERR, "bad result queue length");
1729         }
1730         icbp->icb_rqstaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_rquest_dma);
1731         icbp->icb_rqstaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_rquest_dma);
1732         icbp->icb_rqstaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_rquest_dma);
1733         icbp->icb_rqstaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_rquest_dma);
1734         icbp->icb_respaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_result_dma);
1735         icbp->icb_respaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_result_dma);
1736         icbp->icb_respaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_result_dma);
1737         icbp->icb_respaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_result_dma);
1738
1739         if (FC_SCRATCH_ACQUIRE(isp, 0)) {
1740                 isp_prt(isp, ISP_LOGERR, sacq);
1741                 return;
1742         }
1743         isp_prt(isp, ISP_LOGDEBUG0,
1744             "isp_fibre_init: fwopt 0x%x xfwopt 0x%x zfwopt 0x%x",
1745             icbp->icb_fwoptions, icbp->icb_xfwoptions, icbp->icb_zfwoptions);
1746
1747         isp_put_icb(isp, icbp, (isp_icb_t *)fcp->isp_scratch);
1748
1749         /*
1750          * Init the firmware
1751          */
1752         MBSINIT(&mbs, MBOX_INIT_FIRMWARE, MBLOGALL, 30000000);
1753         mbs.param[2] = DMA_WD1(fcp->isp_scdma);
1754         mbs.param[3] = DMA_WD0(fcp->isp_scdma);
1755         mbs.param[6] = DMA_WD3(fcp->isp_scdma);
1756         mbs.param[7] = DMA_WD2(fcp->isp_scdma);
1757         mbs.logval = MBLOGALL;
1758         isp_prt(isp, ISP_LOGDEBUG0, "INIT F/W from %p (%08x%08x)",
1759             fcp->isp_scratch, (uint32_t) ((uint64_t)fcp->isp_scdma >> 32),
1760             (uint32_t) fcp->isp_scdma);
1761         MEMORYBARRIER(isp, SYNC_SFORDEV, 0, sizeof (*icbp));
1762         isp_mboxcmd(isp, &mbs);
1763         FC_SCRATCH_RELEASE(isp, 0);
1764         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1765                 isp_print_bytes(isp, "isp_fibre_init", sizeof (*icbp), icbp);
1766                 return;
1767         }
1768         isp->isp_reqidx = 0;
1769         isp->isp_reqodx = 0;
1770         isp->isp_residx = 0;
1771
1772         /*
1773          * Whatever happens, we're now committed to being here.
1774          */
1775         isp->isp_state = ISP_INITSTATE;
1776 }
1777
1778 static void
1779 isp_fibre_init_2400(ispsoftc_t *isp)
1780 {
1781         fcparam *fcp;
1782         isp_icb_2400_t local, *icbp = &local;
1783         mbreg_t mbs;
1784         int chan, nchan;
1785
1786         /*
1787          * Check to see whether all channels have *some* kind of role
1788          */
1789         for (chan = 0; chan < isp->isp_nchan; chan++) {
1790                 fcp = FCPARAM(isp, chan);
1791                 if (fcp->role != ISP_ROLE_NONE) {
1792                         break;
1793                 }
1794         }
1795         if (chan == isp->isp_nchan) {
1796                 isp_prt(isp, ISP_LOGDEBUG0, "all %d channels with role 'none'", chan);
1797                 isp->isp_state = ISP_INITSTATE;
1798                 return;
1799         }
1800
1801         if (ISP_CAP_MULTI_ID(isp) == 0 && isp->isp_nchan > 1) {
1802                 isp_prt(isp, ISP_LOGWARN,
1803                     "non-MULTIID f/w loaded, only can enable 1 of %d channels",
1804                     isp->isp_nchan);
1805                 nchan = 1;
1806         } else {
1807                 nchan = isp->isp_nchan;
1808         }
1809
1810         /*
1811          * Start with channel 0.
1812          */
1813         fcp = FCPARAM(isp, 0);
1814
1815         /*
1816          * Turn on LIP F8 async event (1)
1817          */
1818         MBSINIT(&mbs, MBOX_SET_FIRMWARE_OPTIONS, MBLOGALL, 0);
1819         mbs.param[1] = 1;
1820         isp_mboxcmd(isp, &mbs);
1821         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1822                 return;
1823         }
1824
1825         ISP_MEMZERO(icbp, sizeof (*icbp));
1826         icbp->icb_fwoptions1 = fcp->isp_fwoptions;
1827         if (fcp->role & ISP_ROLE_TARGET) {
1828                 icbp->icb_fwoptions1 |= ICB2400_OPT1_TGT_ENABLE;
1829         } else {
1830                 icbp->icb_fwoptions1 &= ~ICB2400_OPT1_TGT_ENABLE;
1831         }
1832
1833         if (fcp->role & ISP_ROLE_INITIATOR) {
1834                 icbp->icb_fwoptions1 &= ~ICB2400_OPT1_INI_DISABLE;
1835         } else {
1836                 icbp->icb_fwoptions1 |= ICB2400_OPT1_INI_DISABLE;
1837         }
1838
1839         icbp->icb_version = ICB_VERSION1;
1840         icbp->icb_maxfrmlen = DEFAULT_FRAMESIZE(isp);
1841         if (icbp->icb_maxfrmlen < ICB_MIN_FRMLEN ||
1842             icbp->icb_maxfrmlen > ICB_MAX_FRMLEN) {
1843                 isp_prt(isp, ISP_LOGERR,
1844                     "bad frame length (%d) from NVRAM- using %d",
1845                     DEFAULT_FRAMESIZE(isp), ICB_DFLT_FRMLEN);
1846                 icbp->icb_maxfrmlen = ICB_DFLT_FRMLEN;
1847         }
1848
1849         icbp->icb_execthrottle = DEFAULT_EXEC_THROTTLE(isp);
1850         if (icbp->icb_execthrottle < 1) {
1851                 isp_prt(isp, ISP_LOGERR,
1852                     "bad execution throttle of %d- using %d",
1853                     DEFAULT_EXEC_THROTTLE(isp), ICB_DFLT_THROTTLE);
1854                 icbp->icb_execthrottle = ICB_DFLT_THROTTLE;
1855         }
1856
1857         if (icbp->icb_fwoptions1 & ICB2400_OPT1_TGT_ENABLE) {
1858                 /*
1859                  * Get current resource count
1860                  */
1861                 MBSINIT(&mbs, MBOX_GET_RESOURCE_COUNT, MBLOGALL, 0);
1862                 mbs.obits = 0x4cf;
1863                 isp_mboxcmd(isp, &mbs);
1864                 if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1865                         return;
1866                 }
1867                 icbp->icb_xchgcnt = mbs.param[3];
1868         }
1869
1870
1871         icbp->icb_hardaddr = fcp->isp_loopid;
1872         if (icbp->icb_hardaddr >= LOCAL_LOOP_LIM) {
1873                 icbp->icb_hardaddr = 0;
1874         }
1875
1876         /*
1877          * Force this on.
1878          */
1879         icbp->icb_fwoptions1 |= ICB2400_OPT1_HARD_ADDRESS;
1880
1881         icbp->icb_fwoptions2 = fcp->isp_xfwoptions;
1882         switch (isp->isp_confopts & ISP_CFG_PORT_PREF) {
1883 #if     0
1884         case ISP_CFG_NPORT:
1885                 /*
1886                  * XXX: This causes the f/w to crash.
1887                  */
1888                 icbp->icb_fwoptions2 &= ~ICB2400_OPT2_TOPO_MASK;
1889                 icbp->icb_fwoptions2 |= ICB2400_OPT2_PTP_2_LOOP;
1890                 break;
1891 #endif
1892         case ISP_CFG_NPORT_ONLY:
1893                 icbp->icb_fwoptions2 &= ~ICB2400_OPT2_TOPO_MASK;
1894                 icbp->icb_fwoptions2 |= ICB2400_OPT2_PTP_ONLY;
1895                 break;
1896         case ISP_CFG_LPORT_ONLY:
1897                 icbp->icb_fwoptions2 &= ~ICB2400_OPT2_TOPO_MASK;
1898                 icbp->icb_fwoptions2 |= ICB2400_OPT2_LOOP_ONLY;
1899                 break;
1900         default:
1901                 icbp->icb_fwoptions2 &= ~ICB2400_OPT2_TOPO_MASK;
1902                 icbp->icb_fwoptions2 |= ICB2400_OPT2_LOOP_2_PTP;
1903                 break;
1904         }
1905
1906         /* force this on for now */
1907         icbp->icb_fwoptions2 |= ICB2400_OPT2_ZIO;
1908
1909         switch (icbp->icb_fwoptions2 & ICB2400_OPT2_TIMER_MASK) {
1910         case ICB2400_OPT2_ZIO:
1911         case ICB2400_OPT2_ZIO1:
1912                 icbp->icb_idelaytimer = 0;
1913                 break;
1914         case 0:
1915                 break;
1916         default:
1917                 isp_prt(isp, ISP_LOGWARN, "bad value %x in fwopt2 timer field",
1918                     icbp->icb_fwoptions2 & ICB2400_OPT2_TIMER_MASK);
1919                 icbp->icb_fwoptions2 &= ~ICB2400_OPT2_TIMER_MASK;
1920                 break;
1921         }
1922
1923         /*
1924          * We don't support FCTAPE, so clear it.
1925          */
1926         icbp->icb_fwoptions2 &= ~ICB2400_OPT2_FCTAPE;
1927
1928         icbp->icb_fwoptions3 = fcp->isp_zfwoptions;
1929         icbp->icb_fwoptions3 &= ~ICB2400_OPT3_RATE_AUTO;
1930         if (isp->isp_confopts & ISP_CFG_ONEGB) {
1931                 icbp->icb_fwoptions3 |= ICB2400_OPT3_RATE_ONEGB;
1932         } else if (isp->isp_confopts & ISP_CFG_TWOGB) {
1933                 icbp->icb_fwoptions3 |= ICB2400_OPT3_RATE_TWOGB;
1934         } else if (isp->isp_confopts & ISP_CFG_FOURGB) {
1935                 icbp->icb_fwoptions3 |= ICB2400_OPT3_RATE_FOURGB;
1936         } else {
1937                 icbp->icb_fwoptions3 |= ICB2400_OPT3_RATE_AUTO;
1938         }
1939
1940         if ((isp->isp_confopts & ISP_CFG_OWNLOOPID) == 0) {
1941                 icbp->icb_fwoptions3 |= ICB2400_OPT3_SOFTID;
1942         }
1943         icbp->icb_logintime = ICB_LOGIN_TOV;
1944
1945         if (fcp->isp_wwnn && fcp->isp_wwpn && (fcp->isp_wwnn >> 60) != 2) {
1946                 icbp->icb_fwoptions1 |= ICB2400_OPT1_BOTH_WWNS;
1947                 MAKE_NODE_NAME_FROM_WWN(icbp->icb_portname, fcp->isp_wwpn);
1948                 MAKE_NODE_NAME_FROM_WWN(icbp->icb_nodename, fcp->isp_wwnn);
1949                 isp_prt(isp, ISP_LOGDEBUG1,
1950                     "Setting ICB Node 0x%08x%08x Port 0x%08x%08x",
1951                     ((uint32_t) (fcp->isp_wwnn >> 32)),
1952                     ((uint32_t) (fcp->isp_wwnn)),
1953                     ((uint32_t) (fcp->isp_wwpn >> 32)),
1954                     ((uint32_t) (fcp->isp_wwpn)));
1955         } else if (fcp->isp_wwpn) {
1956                 icbp->icb_fwoptions1 &= ~ICB2400_OPT1_BOTH_WWNS;
1957                 MAKE_NODE_NAME_FROM_WWN(icbp->icb_portname, fcp->isp_wwpn);
1958                 isp_prt(isp, ISP_LOGDEBUG1,
1959                     "Setting ICB Node to be same as Port 0x%08x%08x",
1960                     ((uint32_t) (fcp->isp_wwpn >> 32)),
1961                     ((uint32_t) (fcp->isp_wwpn)));
1962         } else {
1963                 isp_prt(isp, ISP_LOGERR, "No valid WWNs to use");
1964                 return;
1965         }
1966         icbp->icb_retry_count = fcp->isp_retry_count;
1967
1968         icbp->icb_rqstqlen = RQUEST_QUEUE_LEN(isp);
1969         if (icbp->icb_rqstqlen < 8) {
1970                 isp_prt(isp, ISP_LOGERR, "bad request queue length %d",
1971                     icbp->icb_rqstqlen);
1972                 return;
1973         }
1974         icbp->icb_rsltqlen = RESULT_QUEUE_LEN(isp);
1975         if (icbp->icb_rsltqlen < 8) {
1976                 isp_prt(isp, ISP_LOGERR, "bad result queue length %d",
1977                     icbp->icb_rsltqlen);
1978                 return;
1979         }
1980         icbp->icb_rqstaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_rquest_dma);
1981         icbp->icb_rqstaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_rquest_dma);
1982         icbp->icb_rqstaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_rquest_dma);
1983         icbp->icb_rqstaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_rquest_dma);
1984
1985         icbp->icb_respaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_result_dma);
1986         icbp->icb_respaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_result_dma);
1987         icbp->icb_respaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_result_dma);
1988         icbp->icb_respaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_result_dma);
1989
1990 #ifdef  ISP_TARGET_MODE
1991         /* unconditionally set up the ATIO queue if we support target mode */
1992         icbp->icb_atioqlen = RESULT_QUEUE_LEN(isp);
1993         if (icbp->icb_atioqlen < 8) {
1994                 isp_prt(isp, ISP_LOGERR,
1995                     "bad ATIO queue length %d", icbp->icb_atioqlen);
1996                 return;
1997         }
1998         icbp->icb_atioqaddr[RQRSP_ADDR0015] = DMA_WD0(isp->isp_atioq_dma);
1999         icbp->icb_atioqaddr[RQRSP_ADDR1631] = DMA_WD1(isp->isp_atioq_dma);
2000         icbp->icb_atioqaddr[RQRSP_ADDR3247] = DMA_WD2(isp->isp_atioq_dma);
2001         icbp->icb_atioqaddr[RQRSP_ADDR4863] = DMA_WD3(isp->isp_atioq_dma);
2002         isp_prt(isp, ISP_LOGDEBUG0,
2003             "isp_fibre_init_2400: atioq %04x%04x%04x%04x",
2004             DMA_WD3(isp->isp_atioq_dma), DMA_WD2(isp->isp_atioq_dma),
2005             DMA_WD1(isp->isp_atioq_dma), DMA_WD0(isp->isp_atioq_dma));
2006 #endif
2007
2008         isp_prt(isp, ISP_LOGDEBUG0,
2009             "isp_fibre_init_2400: fwopt1 0x%x fwopt2 0x%x fwopt3 0x%x",
2010             icbp->icb_fwoptions1, icbp->icb_fwoptions2, icbp->icb_fwoptions3);
2011
2012         isp_prt(isp, ISP_LOGDEBUG0,
2013             "isp_fibre_init_2400: rqst %04x%04x%04x%04x rsp %04x%04x%04x%04x",
2014             DMA_WD3(isp->isp_rquest_dma), DMA_WD2(isp->isp_rquest_dma),
2015             DMA_WD1(isp->isp_rquest_dma), DMA_WD0(isp->isp_rquest_dma),
2016             DMA_WD3(isp->isp_result_dma), DMA_WD2(isp->isp_result_dma),
2017             DMA_WD1(isp->isp_result_dma), DMA_WD0(isp->isp_result_dma));
2018
2019         if (isp->isp_dblev & ISP_LOGDEBUG1) {
2020                 isp_print_bytes(isp, "isp_fibre_init_2400", sizeof (*icbp),
2021                     icbp);
2022         }
2023
2024         if (FC_SCRATCH_ACQUIRE(isp, 0)) {
2025                 isp_prt(isp, ISP_LOGERR, sacq);
2026                 return;
2027         }
2028         ISP_MEMZERO(fcp->isp_scratch, ISP_FC_SCRLEN);
2029         isp_put_icb_2400(isp, icbp, fcp->isp_scratch);
2030
2031         /*
2032          * Now fill in information about any additional channels
2033          */
2034         if (nchan > 1) {
2035                 isp_icb_2400_vpinfo_t vpinfo, *vdst;
2036                 vp_port_info_t pi, *pdst;
2037                 size_t amt = 0;
2038                 uint8_t *off;
2039
2040                 vpinfo.vp_count = isp->isp_nchan - 1;
2041                 vpinfo.vp_global_options = 0;
2042                 off = fcp->isp_scratch;
2043                 off += ICB2400_VPINFO_OFF;
2044                 vdst = (isp_icb_2400_vpinfo_t *) off;
2045                 isp_put_icb_2400_vpinfo(isp, &vpinfo, vdst);
2046                 amt = ICB2400_VPINFO_OFF + sizeof (isp_icb_2400_vpinfo_t);
2047                 for (chan = 1; chan < isp->isp_nchan; chan++) {
2048                         fcparam *fcp2;
2049
2050                         ISP_MEMZERO(&pi, sizeof (pi));
2051                         fcp2 = FCPARAM(isp, chan);
2052                         if (fcp2->role != ISP_ROLE_NONE) {
2053                                 pi.vp_port_options = ICB2400_VPOPT_ENABLED;
2054                                 if (fcp2->role & ISP_ROLE_INITIATOR) {
2055                                         pi.vp_port_options |=
2056                                             ICB2400_VPOPT_INI_ENABLE;
2057                                 }
2058                                 if ((fcp2->role & ISP_ROLE_TARGET) == 0) {
2059                                         pi.vp_port_options |=
2060                                             ICB2400_VPOPT_TGT_DISABLE;
2061                                 }
2062                                 MAKE_NODE_NAME_FROM_WWN(pi.vp_port_portname,
2063                                     fcp2->isp_wwpn);
2064                                 MAKE_NODE_NAME_FROM_WWN(pi.vp_port_nodename,
2065                                     fcp2->isp_wwnn);
2066                         }
2067                         off = fcp->isp_scratch;
2068                         off += ICB2400_VPINFO_PORT_OFF(chan);
2069                         pdst = (vp_port_info_t *) off;
2070                         isp_put_vp_port_info(isp, &pi, pdst);
2071                         amt += ICB2400_VPOPT_WRITE_SIZE;
2072                 }
2073         }
2074
2075         /*
2076          * Init the firmware
2077          */
2078         MBSINIT(&mbs, 0, MBLOGALL, 30000000);
2079         if (nchan > 1) {
2080                 mbs.param[0] = MBOX_INIT_FIRMWARE_MULTI_ID;
2081         } else {
2082                 mbs.param[0] = MBOX_INIT_FIRMWARE;
2083         }
2084         mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2085         mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2086         mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2087         mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2088         isp_prt(isp, ISP_LOGDEBUG0, "INIT F/W from %04x%04x%04x%04x",
2089             DMA_WD3(fcp->isp_scdma), DMA_WD2(fcp->isp_scdma),
2090             DMA_WD1(fcp->isp_scdma), DMA_WD0(fcp->isp_scdma));
2091         MEMORYBARRIER(isp, SYNC_SFORDEV, 0, sizeof (*icbp));
2092         isp_mboxcmd(isp, &mbs);
2093         FC_SCRATCH_RELEASE(isp, 0);
2094
2095         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2096                 return;
2097         }
2098         isp->isp_reqidx = 0;
2099         isp->isp_reqodx = 0;
2100         isp->isp_residx = 0;
2101
2102         /*
2103          * Whatever happens, we're now committed to being here.
2104          */
2105         isp->isp_state = ISP_INITSTATE;
2106 }
2107
2108 static void
2109 isp_mark_portdb(ispsoftc_t *isp, int chan, int disposition)
2110 {
2111         fcparam *fcp = FCPARAM(isp, chan);
2112         int i;
2113
2114         if (chan < 0 || chan >= isp->isp_nchan) {
2115                 isp_prt(isp, ISP_LOGWARN, "isp_mark_portdb: bad channel %d", chan);
2116                 return;
2117         }
2118         for (i = 0; i < MAX_FC_TARG; i++) {
2119                 if (fcp->portdb[i].target_mode) {
2120                         if (disposition < 0) {
2121                                 isp_prt(isp, ISP_LOGTINFO, "isp_mark_portdb: Chan %d zeroing handle 0x" "%02x port 0x%06x", chan,
2122                                     fcp->portdb[i].handle, fcp->portdb[i].portid);
2123                                 ISP_MEMZERO(&fcp->portdb[i], sizeof (fcportdb_t));
2124                         }
2125                         continue;
2126                 }
2127                 if (disposition == 0) {
2128                         ISP_MEMZERO(&fcp->portdb[i], sizeof (fcportdb_t));
2129                 } else {
2130                         switch (fcp->portdb[i].state) {
2131                         case FC_PORTDB_STATE_CHANGED:
2132                         case FC_PORTDB_STATE_PENDING_VALID:
2133                         case FC_PORTDB_STATE_VALID:
2134                         case FC_PORTDB_STATE_PROBATIONAL:
2135                                 fcp->portdb[i].state = FC_PORTDB_STATE_PROBATIONAL;
2136                                 break;
2137                         case FC_PORTDB_STATE_ZOMBIE:
2138                                 break;
2139                         case FC_PORTDB_STATE_NIL:
2140                         default:
2141                                 ISP_MEMZERO(&fcp->portdb[i], sizeof (fcportdb_t));
2142                                 fcp->portdb[i].state = FC_PORTDB_STATE_NIL;
2143                                 break;
2144                         }
2145                 }
2146         }
2147 }
2148
2149 /*
2150  * Perform an IOCB PLOGI or LOGO via EXECUTE IOCB A64 for 24XX cards
2151  * or via FABRIC LOGIN/FABRIC LOGOUT for other cards.
2152  */
2153 static int
2154 isp_plogx(ispsoftc_t *isp, int chan, uint16_t handle, uint32_t portid,
2155     int flags, int gs)
2156 {
2157         mbreg_t mbs;
2158         uint8_t q[QENTRY_LEN];
2159         isp_plogx_t *plp;
2160         fcparam *fcp;
2161         uint8_t *scp;
2162         uint32_t sst, parm1;
2163         int rval, lev;
2164         const char *msg;
2165         char buf[64];
2166
2167         if (!IS_24XX(isp)) {
2168                 int action = flags & PLOGX_FLG_CMD_MASK;
2169                 if (action == PLOGX_FLG_CMD_PLOGI) {
2170                         return (isp_port_login(isp, handle, portid));
2171                 } else if (action == PLOGX_FLG_CMD_LOGO) {
2172                         return (isp_port_logout(isp, handle, portid));
2173                 } else {
2174                         return (MBOX_INVALID_COMMAND);
2175                 }
2176         }
2177
2178         ISP_MEMZERO(q, QENTRY_LEN);
2179         plp = (isp_plogx_t *) q;
2180         plp->plogx_header.rqs_entry_count = 1;
2181         plp->plogx_header.rqs_entry_type = RQSTYPE_LOGIN;
2182         plp->plogx_handle = 0xffffffff;
2183         plp->plogx_nphdl = handle;
2184         plp->plogx_vphdl = chan;
2185         plp->plogx_portlo = portid;
2186         plp->plogx_rspsz_porthi = (portid >> 16) & 0xff;
2187         plp->plogx_flags = flags;
2188
2189         if (isp->isp_dblev & ISP_LOGDEBUG1) {
2190                 isp_print_bytes(isp, "IOCB LOGX", QENTRY_LEN, plp);
2191         }
2192
2193         if (gs == 0) {
2194                 if (FC_SCRATCH_ACQUIRE(isp, chan)) {
2195                         isp_prt(isp, ISP_LOGERR, sacq);
2196                         return (-1);
2197                 }
2198         }
2199         fcp = FCPARAM(isp, chan);
2200         scp = fcp->isp_scratch;
2201         isp_put_plogx(isp, plp, (isp_plogx_t *) scp);
2202
2203         MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 500000);
2204         mbs.param[1] = QENTRY_LEN;
2205         mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2206         mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2207         mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2208         mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2209         MEMORYBARRIER(isp, SYNC_SFORDEV, 0, QENTRY_LEN);
2210         isp_mboxcmd(isp, &mbs);
2211         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2212                 rval = mbs.param[0];
2213                 goto out;
2214         }
2215         MEMORYBARRIER(isp, SYNC_SFORCPU, QENTRY_LEN, QENTRY_LEN);
2216         scp += QENTRY_LEN;
2217         isp_get_plogx(isp, (isp_plogx_t *) scp, plp);
2218         if (isp->isp_dblev & ISP_LOGDEBUG1) {
2219                 isp_print_bytes(isp, "IOCB LOGX response", QENTRY_LEN, plp);
2220         }
2221
2222         if (plp->plogx_status == PLOGX_STATUS_OK) {
2223                 rval = 0;
2224                 goto out;
2225         } else if (plp->plogx_status != PLOGX_STATUS_IOCBERR) {
2226                 isp_prt(isp, ISP_LOGWARN,
2227                     "status 0x%x on port login IOCB chanel %d",
2228                     plp->plogx_status, chan);
2229                 rval = -1;
2230                 goto out;
2231         }
2232
2233         sst = plp->plogx_ioparm[0].lo16 | (plp->plogx_ioparm[0].hi16 << 16);
2234         parm1 = plp->plogx_ioparm[1].lo16 | (plp->plogx_ioparm[1].hi16 << 16);
2235
2236         rval = -1;
2237         lev = ISP_LOGERR;
2238         msg = NULL;
2239
2240         switch (sst) {
2241         case PLOGX_IOCBERR_NOLINK:
2242                 msg = "no link";
2243                 break;
2244         case PLOGX_IOCBERR_NOIOCB:
2245                 msg = "no IOCB buffer";
2246                 break;
2247         case PLOGX_IOCBERR_NOXGHG:
2248                 msg = "no Exchange Control Block";
2249                 break;
2250         case PLOGX_IOCBERR_FAILED:
2251                 ISP_SNPRINTF(buf, sizeof (buf),
2252                     "reason 0x%x (last LOGIN state 0x%x)",
2253                     parm1 & 0xff, (parm1 >> 8) & 0xff);
2254                 msg = buf;
2255                 break;
2256         case PLOGX_IOCBERR_NOFABRIC:
2257                 msg = "no fabric";
2258                 break;
2259         case PLOGX_IOCBERR_NOTREADY:
2260                 msg = "firmware not ready";
2261                 break;
2262         case PLOGX_IOCBERR_NOLOGIN:
2263                 ISP_SNPRINTF(buf, sizeof (buf), "not logged in (last state 0x%x)",
2264                     parm1);
2265                 msg = buf;
2266                 rval = MBOX_NOT_LOGGED_IN;
2267                 break;
2268         case PLOGX_IOCBERR_REJECT:
2269                 ISP_SNPRINTF(buf, sizeof (buf), "LS_RJT = 0x%x", parm1);
2270                 msg = buf;
2271                 break;
2272         case PLOGX_IOCBERR_NOPCB:
2273                 msg = "no PCB allocated";
2274                 break;
2275         case PLOGX_IOCBERR_EINVAL:
2276                 ISP_SNPRINTF(buf, sizeof (buf), "invalid parameter at offset 0x%x",
2277                     parm1);
2278                 msg = buf;
2279                 break;
2280         case PLOGX_IOCBERR_PORTUSED:
2281                 lev = ISP_LOGSANCFG|ISP_LOGDEBUG0;
2282                 ISP_SNPRINTF(buf, sizeof (buf),
2283                     "already logged in with N-Port handle 0x%x", parm1);
2284                 msg = buf;
2285                 rval = MBOX_PORT_ID_USED | (parm1 << 16);
2286                 break;
2287         case PLOGX_IOCBERR_HNDLUSED:
2288                 lev = ISP_LOGSANCFG|ISP_LOGDEBUG0;
2289                 ISP_SNPRINTF(buf, sizeof (buf),
2290                     "handle already used for PortID 0x%06x", parm1);
2291                 msg = buf;
2292                 rval = MBOX_LOOP_ID_USED;
2293                 break;
2294         case PLOGX_IOCBERR_NOHANDLE:
2295                 msg = "no handle allocated";
2296                 break;
2297         case PLOGX_IOCBERR_NOFLOGI:
2298                 msg = "no FLOGI_ACC";
2299                 break;
2300         default:
2301                 ISP_SNPRINTF(buf, sizeof (buf), "status %x from %x",
2302                     plp->plogx_status, flags);
2303                 msg = buf;
2304                 break;
2305         }
2306         if (msg) {
2307                 isp_prt(isp, ISP_LOGERR,
2308                     "Chan %d PLOGX PortID 0x%06x to N-Port handle 0x%x: %s",
2309                     chan, portid, handle, msg);
2310         }
2311 out:
2312         if (gs == 0) {
2313                 FC_SCRATCH_RELEASE(isp, chan);
2314         }
2315         return (rval);
2316 }
2317
2318 static int
2319 isp_port_login(ispsoftc_t *isp, uint16_t handle, uint32_t portid)
2320 {
2321         mbreg_t mbs;
2322
2323         MBSINIT(&mbs, MBOX_FABRIC_LOGIN, MBLOGNONE, 500000);
2324         if (ISP_CAP_2KLOGIN(isp)) {
2325                 mbs.param[1] = handle;
2326                 mbs.ibits = (1 << 10);
2327         } else {
2328                 mbs.param[1] = handle << 8;
2329         }
2330         mbs.param[2] = portid >> 16;
2331         mbs.param[3] = portid;
2332         mbs.logval = MBLOGNONE;
2333         mbs.timeout = 500000;
2334         isp_mboxcmd(isp, &mbs);
2335
2336         switch (mbs.param[0]) {
2337         case MBOX_PORT_ID_USED:
2338                 isp_prt(isp, ISP_LOGDEBUG0,
2339                     "isp_port_login: portid 0x%06x already logged in as %u",
2340                     portid, mbs.param[1]);
2341                 return (MBOX_PORT_ID_USED | (mbs.param[1] << 16));
2342
2343         case MBOX_LOOP_ID_USED:
2344                 isp_prt(isp, ISP_LOGDEBUG0,
2345                     "isp_port_login: handle %u in use for port id 0x%02xXXXX",
2346                     handle, mbs.param[1] & 0xff);
2347                 return (MBOX_LOOP_ID_USED);
2348
2349         case MBOX_COMMAND_COMPLETE:
2350                 return (0);
2351
2352         case MBOX_COMMAND_ERROR:
2353                 isp_prt(isp, ISP_LOGINFO,
2354                     "isp_port_login: error 0x%x in PLOGI to port 0x%06x",
2355                     mbs.param[1], portid);
2356                 return (MBOX_COMMAND_ERROR);
2357
2358         case MBOX_ALL_IDS_USED:
2359                 isp_prt(isp, ISP_LOGINFO,
2360                     "isp_port_login: all IDs used for fabric login");
2361                 return (MBOX_ALL_IDS_USED);
2362
2363         default:
2364                 isp_prt(isp, ISP_LOGINFO,
2365                     "isp_port_login: error 0x%x on port login of 0x%06x@0x%0x",
2366                     mbs.param[0], portid, handle);
2367                 return (mbs.param[0]);
2368         }
2369 }
2370
2371 static int
2372 isp_port_logout(ispsoftc_t *isp, uint16_t handle, uint32_t portid)
2373 {
2374         mbreg_t mbs;
2375
2376         MBSINIT(&mbs, MBOX_FABRIC_LOGOUT, MBLOGNONE, 500000);
2377         if (ISP_CAP_2KLOGIN(isp)) {
2378                 mbs.param[1] = handle;
2379                 mbs.ibits = (1 << 10);
2380         } else {
2381                 mbs.param[1] = handle << 8;
2382         }
2383         isp_mboxcmd(isp, &mbs);
2384         return (mbs.param[0] == MBOX_COMMAND_COMPLETE? 0 : mbs.param[0]);
2385 }
2386
2387 static int
2388 isp_getpdb(ispsoftc_t *isp, int chan, uint16_t id, isp_pdb_t *pdb, int dolock)
2389 {
2390         fcparam *fcp = FCPARAM(isp, chan);
2391         mbreg_t mbs;
2392         union {
2393                 isp_pdb_21xx_t fred;
2394                 isp_pdb_24xx_t bill;
2395         } un;
2396
2397         MBSINIT(&mbs, MBOX_GET_PORT_DB, MBLOGALL & ~MBOX_COMMAND_PARAM_ERROR, 250000);
2398         if (IS_24XX(isp)) {
2399                 mbs.ibits = (1 << 9)|(1 << 10);
2400                 mbs.param[1] = id;
2401                 mbs.param[9] = chan;
2402         } else if (ISP_CAP_2KLOGIN(isp)) {
2403                 mbs.param[1] = id;
2404         } else {
2405                 mbs.param[1] = id << 8;
2406         }
2407         mbs.param[2] = DMA_WD1(fcp->isp_scdma);
2408         mbs.param[3] = DMA_WD0(fcp->isp_scdma);
2409         mbs.param[6] = DMA_WD3(fcp->isp_scdma);
2410         mbs.param[7] = DMA_WD2(fcp->isp_scdma);
2411         if (dolock) {
2412                 if (FC_SCRATCH_ACQUIRE(isp, chan)) {
2413                         isp_prt(isp, ISP_LOGERR, sacq);
2414                         return (-1);
2415                 }
2416         }
2417         MEMORYBARRIER(isp, SYNC_SFORDEV, 0, sizeof (un));
2418         isp_mboxcmd(isp, &mbs);
2419         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2420                 if (dolock) {
2421                         FC_SCRATCH_RELEASE(isp, chan);
2422                 }
2423                 return (mbs.param[0]);
2424         }
2425         if (IS_24XX(isp)) {
2426                 isp_get_pdb_24xx(isp, fcp->isp_scratch, &un.bill);
2427                 pdb->handle = un.bill.pdb_handle;
2428                 pdb->s3_role = un.bill.pdb_prli_svc3;
2429                 pdb->portid = BITS2WORD_24XX(un.bill.pdb_portid_bits);
2430                 ISP_MEMCPY(pdb->portname, un.bill.pdb_portname, 8);
2431                 ISP_MEMCPY(pdb->nodename, un.bill.pdb_nodename, 8);
2432                 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
2433                     "Chan %d Port 0x%06x flags 0x%x curstate %x",
2434                     chan, pdb->portid, un.bill.pdb_flags,
2435                     un.bill.pdb_curstate);
2436                 if (un.bill.pdb_curstate < PDB2400_STATE_PLOGI_DONE ||
2437                     un.bill.pdb_curstate > PDB2400_STATE_LOGGED_IN) {
2438                         mbs.param[0] = MBOX_NOT_LOGGED_IN;
2439                         if (dolock) {
2440                                 FC_SCRATCH_RELEASE(isp, chan);
2441                         }
2442                         return (mbs.param[0]);
2443                 }
2444         } else {
2445                 isp_get_pdb_21xx(isp, fcp->isp_scratch, &un.fred);
2446                 pdb->handle = un.fred.pdb_loopid;
2447                 pdb->s3_role = un.fred.pdb_prli_svc3;
2448                 pdb->portid = BITS2WORD(un.fred.pdb_portid_bits);
2449                 ISP_MEMCPY(pdb->portname, un.fred.pdb_portname, 8);
2450                 ISP_MEMCPY(pdb->nodename, un.fred.pdb_nodename, 8);
2451         }
2452         if (dolock) {
2453                 FC_SCRATCH_RELEASE(isp, chan);
2454         }
2455         return (0);
2456 }
2457
2458 static void
2459 isp_dump_chip_portdb(ispsoftc_t *isp, int chan, int dolock)
2460 {
2461         isp_pdb_t pdb;
2462         int lim, loopid;
2463
2464         if (ISP_CAP_2KLOGIN(isp)) {
2465                 lim = NPH_MAX_2K;
2466         } else {
2467                 lim = NPH_MAX;
2468         }
2469         for (loopid = 0; loopid != lim; loopid++) {
2470                 if (isp_getpdb(isp, chan, loopid, &pdb, dolock)) {
2471                         continue;
2472                 }
2473                 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGINFO, "Chan %d Loopid 0x%04x "
2474                     "PortID 0x%06x WWPN 0x%02x%02x%02x%02x%02x%02x%02x%02x",
2475                     chan, loopid, pdb.portid, pdb.portname[0], pdb.portname[1],
2476                     pdb.portname[2], pdb.portname[3], pdb.portname[4],
2477                     pdb.portname[5], pdb.portname[6], pdb.portname[7]);
2478         }
2479 }
2480
2481 static uint64_t
2482 isp_get_wwn(ispsoftc_t *isp, int chan, int loopid, int nodename)
2483 {
2484         uint64_t wwn = INI_NONE;
2485         fcparam *fcp = FCPARAM(isp, chan);
2486         mbreg_t mbs;
2487
2488         if (fcp->isp_fwstate < FW_READY ||
2489             fcp->isp_loopstate < LOOP_PDB_RCVD) {
2490                 return (wwn);
2491         }
2492         MBSINIT(&mbs, MBOX_GET_PORT_NAME, MBLOGALL & ~MBOX_COMMAND_PARAM_ERROR, 500000);
2493         if (ISP_CAP_2KLOGIN(isp)) {
2494                 mbs.param[1] = loopid;
2495                 mbs.ibits = (1 << 10);
2496                 if (nodename) {
2497                         mbs.param[10] = 1;
2498                 }
2499                 if (ISP_CAP_MULTI_ID(isp)) {
2500                         mbs.ibits |= (1 << 9);
2501                         mbs.param[9] = chan;
2502                 }
2503         } else {
2504                 mbs.param[1] = loopid << 8;
2505                 if (nodename) {
2506                         mbs.param[1] |= 1;
2507                 }
2508         }
2509         isp_mboxcmd(isp, &mbs);
2510         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2511                 return (wwn);
2512         }
2513         if (IS_24XX(isp)) {
2514                 wwn =
2515                     (((uint64_t)(mbs.param[2] >> 8))    << 56) |
2516                     (((uint64_t)(mbs.param[2] & 0xff))  << 48) |
2517                     (((uint64_t)(mbs.param[3] >> 8))    << 40) |
2518                     (((uint64_t)(mbs.param[3] & 0xff))  << 32) |
2519                     (((uint64_t)(mbs.param[6] >> 8))    << 24) |
2520                     (((uint64_t)(mbs.param[6] & 0xff))  << 16) |
2521                     (((uint64_t)(mbs.param[7] >> 8))    <<  8) |
2522                     (((uint64_t)(mbs.param[7] & 0xff)));
2523         } else {
2524                 wwn =
2525                     (((uint64_t)(mbs.param[2] & 0xff))  << 56) |
2526                     (((uint64_t)(mbs.param[2] >> 8))    << 48) |
2527                     (((uint64_t)(mbs.param[3] & 0xff))  << 40) |
2528                     (((uint64_t)(mbs.param[3] >> 8))    << 32) |
2529                     (((uint64_t)(mbs.param[6] & 0xff))  << 24) |
2530                     (((uint64_t)(mbs.param[6] >> 8))    << 16) |
2531                     (((uint64_t)(mbs.param[7] & 0xff))  <<  8) |
2532                     (((uint64_t)(mbs.param[7] >> 8)));
2533         }
2534         return (wwn);
2535 }
2536
2537 /*
2538  * Make sure we have good FC link.
2539  */
2540
2541 static int
2542 isp_fclink_test(ispsoftc_t *isp, int chan, int usdelay)
2543 {
2544         mbreg_t mbs;
2545         int count, check_for_fabric, r;
2546         uint8_t lwfs;
2547         int loopid;
2548         fcparam *fcp;
2549         fcportdb_t *lp;
2550         isp_pdb_t pdb;
2551
2552         fcp = FCPARAM(isp, chan);
2553
2554         isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Chan %d FC Link Test Entry", chan);
2555         ISP_MARK_PORTDB(isp, chan, 1);
2556
2557         /*
2558          * Wait up to N microseconds for F/W to go to a ready state.
2559          */
2560         lwfs = FW_CONFIG_WAIT;
2561         count = 0;
2562         while (count < usdelay) {
2563                 uint64_t enano;
2564                 uint32_t wrk;
2565                 NANOTIME_T hra, hrb;
2566
2567                 GET_NANOTIME(&hra);
2568                 isp_fw_state(isp, chan);
2569                 if (lwfs != fcp->isp_fwstate) {
2570                         isp_prt(isp, ISP_LOGCONFIG|ISP_LOGSANCFG, "Chan %d Firmware State <%s->%s>", chan, isp_fc_fw_statename((int)lwfs), isp_fc_fw_statename((int)fcp->isp_fwstate));
2571                         lwfs = fcp->isp_fwstate;
2572                 }
2573                 if (fcp->isp_fwstate == FW_READY) {
2574                         break;
2575                 }
2576                 GET_NANOTIME(&hrb);
2577
2578                 /*
2579                  * Get the elapsed time in nanoseconds.
2580                  * Always guaranteed to be non-zero.
2581                  */
2582                 enano = NANOTIME_SUB(&hrb, &hra);
2583
2584                 isp_prt(isp, ISP_LOGDEBUG1, "usec%d: 0x%lx->0x%lx enano 0x%x%08x", count, (long) GET_NANOSEC(&hra), (long) GET_NANOSEC(&hrb), (uint32_t)(enano >> 32), (uint32_t)(enano));
2585
2586                 /*
2587                  * If the elapsed time is less than 1 millisecond,
2588                  * delay a period of time up to that millisecond of
2589                  * waiting.
2590                  *
2591                  * This peculiar code is an attempt to try and avoid
2592                  * invoking uint64_t math support functions for some
2593                  * platforms where linkage is a problem.
2594                  */
2595                 if (enano < (1000 * 1000)) {
2596                         count += 1000;
2597                         enano = (1000 * 1000) - enano;
2598                         while (enano > (uint64_t) 4000000000U) {
2599                                 ISP_SLEEP(isp, 4000000);
2600                                 enano -= (uint64_t) 4000000000U;
2601                         }
2602                         wrk = enano;
2603                         wrk /= 1000;
2604                         ISP_SLEEP(isp, wrk);
2605                 } else {
2606                         while (enano > (uint64_t) 4000000000U) {
2607 #if defined(__GNUC__) && (__GNUC__ >= 4)
2608                                 /*
2609                                  * Prevent to optimize loop, gcc assume libgcc.a
2610                                  * with  __udivdi3 is linked, but this is not
2611                                  * true when building (linux) kernel
2612                                  */
2613                                 asm("" : "+rm" (enano));
2614 #endif
2615                                 count += 4000000;
2616                                 enano -= (uint64_t) 4000000000U;
2617                         }
2618                         wrk = enano;
2619                         count += (wrk / 1000);
2620                 }
2621         }
2622
2623
2624
2625         /*
2626          * If we haven't gone to 'ready' state, return.
2627          */
2628         if (fcp->isp_fwstate != FW_READY) {
2629                 isp_prt(isp, ISP_LOGSANCFG, "%s: chan %d not at FW_READY state", __func__, chan);
2630                 return (-1);
2631         }
2632
2633         /*
2634          * Get our Loop ID and Port ID.
2635          */
2636         MBSINIT(&mbs, MBOX_GET_LOOP_ID, MBLOGALL, 0);
2637         if (ISP_CAP_MULTI_ID(isp)) {
2638                 mbs.param[9] = chan;
2639                 mbs.ibits = (1 << 9);
2640                 mbs.obits = (1 << 7);
2641         }
2642         isp_mboxcmd(isp, &mbs);
2643         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
2644                 return (-1);
2645         }
2646
2647         if (ISP_CAP_2KLOGIN(isp)) {
2648                 fcp->isp_loopid = mbs.param[1];
2649         } else {
2650                 fcp->isp_loopid = mbs.param[1] & 0xff;
2651         }
2652
2653         if (IS_2100(isp)) {
2654                 fcp->isp_topo = TOPO_NL_PORT;
2655         } else {
2656                 int topo = (int) mbs.param[6];
2657                 if (topo < TOPO_NL_PORT || topo > TOPO_PTP_STUB) {
2658                         topo = TOPO_PTP_STUB;
2659                 }
2660                 fcp->isp_topo = topo;
2661         }
2662         fcp->isp_portid = mbs.param[2] | (mbs.param[3] << 16);
2663
2664         if (IS_2100(isp)) {
2665                 /*
2666                  * Don't bother with fabric if we are using really old
2667                  * 2100 firmware. It's just not worth it.
2668                  */
2669                 if (ISP_FW_NEWER_THAN(isp, 1, 15, 37)) {
2670                         check_for_fabric = 1;
2671                 } else {
2672                         check_for_fabric = 0;
2673                 }
2674         } else if (fcp->isp_topo == TOPO_FL_PORT || fcp->isp_topo == TOPO_F_PORT) {
2675                 check_for_fabric = 1;
2676         } else {
2677                 check_for_fabric = 0;
2678         }
2679
2680         /*
2681          * Check to make sure we got a valid loopid
2682          * The 24XX seems to mess this up for multiple channels.
2683          */
2684         if (fcp->isp_topo == TOPO_FL_PORT || fcp->isp_topo == TOPO_NL_PORT) {
2685                 uint8_t alpa = fcp->isp_portid;
2686
2687                 if (alpa == 0) {
2688                         /* "Cannot Happen" */
2689                         isp_prt(isp, ISP_LOGWARN, "Zero AL_PA for Loop Topology?");
2690                 } else {
2691                         int i;
2692                         for (i = 0; alpa_map[i]; i++) {
2693                                 if (alpa_map[i] == alpa) {
2694                                         break;
2695                                 }
2696                         }
2697                         if (alpa_map[i] && fcp->isp_loopid != i) {
2698                                 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Chan %d deriving loopid %d from AL_PA map  (AL_PA 0x%x) and ignoring returned value %d (AL_PA 0x%x)", chan, i, alpa_map[i], fcp->isp_loopid, alpa);
2699                                 fcp->isp_loopid = i;
2700                         }
2701                 }
2702         }
2703
2704
2705         if (IS_24XX(isp)) { /* XXX SHOULDN'T THIS BE FOR 2K F/W? XXX */
2706                 loopid = NPH_FL_ID;
2707         } else {
2708                 loopid = FL_ID;
2709         }
2710         if (check_for_fabric) {
2711                 r = isp_getpdb(isp, chan, loopid, &pdb, 1);
2712                 if (r && (fcp->isp_topo == TOPO_F_PORT || fcp->isp_topo == TOPO_FL_PORT)) {
2713                         isp_prt(isp, ISP_LOGWARN, "fabric topology but cannot get info about fabric controller (0x%x)", r);
2714                         fcp->isp_topo = TOPO_PTP_STUB;
2715                 }
2716         } else {
2717                 r = -1;
2718         }
2719         if (r == 0) {
2720                 if (IS_2100(isp)) {
2721                         fcp->isp_topo = TOPO_FL_PORT;
2722                 }
2723                 if (pdb.portid == 0) {
2724                         /*
2725                          * Crock.
2726                          */
2727                         fcp->isp_topo = TOPO_NL_PORT;
2728                         goto not_on_fabric;
2729                 }
2730
2731                 /*
2732                  * Save the Fabric controller's port database entry.
2733                  */
2734                 lp = &fcp->portdb[FL_ID];
2735                 lp->state = FC_PORTDB_STATE_PENDING_VALID;
2736                 MAKE_WWN_FROM_NODE_NAME(lp->node_wwn, pdb.nodename);
2737                 MAKE_WWN_FROM_NODE_NAME(lp->port_wwn, pdb.portname);
2738                 lp->roles = (pdb.s3_role & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
2739                 lp->portid = pdb.portid;
2740                 lp->handle = pdb.handle;
2741                 lp->new_portid = lp->portid;
2742                 lp->new_roles = lp->roles;
2743                 if (IS_24XX(isp)) {
2744                         fcp->inorder = (mbs.param[7] & ISP24XX_INORDER) != 0;
2745                         if (ISP_FW_NEWER_THAN(isp, 4, 0, 27)) {
2746                                 fcp->npiv_fabric = (mbs.param[7] & ISP24XX_NPIV_SAN) != 0;
2747                                 if (fcp->npiv_fabric) {
2748                                         isp_prt(isp, ISP_LOGCONFIG, "fabric supports NP-IV");
2749                                 }
2750                         }
2751                         if (chan) {
2752                                 fcp->isp_sns_hdl = NPH_SNS_HDLBASE + chan;
2753                                 r = isp_plogx(isp, chan, fcp->isp_sns_hdl, SNS_PORT_ID, PLOGX_FLG_CMD_PLOGI | PLOGX_FLG_COND_PLOGI | PLOGX_FLG_SKIP_PRLI, 0);
2754                                 if (r) {
2755                                         isp_prt(isp, ISP_LOGWARN, "%s: Chan %d cannot log into SNS", __func__, chan);
2756                                         return (-1);
2757                                 }
2758                         } else {
2759                                 fcp->isp_sns_hdl = NPH_SNS_ID;
2760                         }
2761                         r = isp_register_fc4_type_24xx(isp, chan);
2762                 } else {
2763                         fcp->isp_sns_hdl = SNS_ID;
2764                         r = isp_register_fc4_type(isp, chan);
2765                 }
2766                 if (r) {
2767                         isp_prt(isp, ISP_LOGWARN|ISP_LOGSANCFG, "%s: register fc4 type failed", __func__);
2768                         return (-1);
2769                 }
2770         } else {
2771 not_on_fabric:
2772                 fcp->portdb[FL_ID].state = FC_PORTDB_STATE_NIL;
2773         }
2774
2775         fcp->isp_gbspeed = 1;
2776         if (IS_23XX(isp) || IS_24XX(isp)) {
2777                 MBSINIT(&mbs, MBOX_GET_SET_DATA_RATE, MBLOGALL, 3000000);
2778                 mbs.param[1] = MBGSD_GET_RATE;
2779                 /* mbs.param[2] undefined if we're just getting rate */
2780                 isp_mboxcmd(isp, &mbs);
2781                 if (mbs.param[0] == MBOX_COMMAND_COMPLETE) {
2782                         if (mbs.param[1] == MBGSD_EIGHTGB) {
2783                                 isp_prt(isp, ISP_LOGINFO, "Chan %d 8Gb link speed", chan);
2784                                 fcp->isp_gbspeed = 8;
2785                         } else if (mbs.param[1] == MBGSD_FOURGB) {
2786                                 isp_prt(isp, ISP_LOGINFO, "Chan %d 4Gb link speed", chan);
2787                                 fcp->isp_gbspeed = 4;
2788                         } else if (mbs.param[1] == MBGSD_TWOGB) {
2789                                 isp_prt(isp, ISP_LOGINFO, "Chan %d 2Gb link speed", chan);
2790                                 fcp->isp_gbspeed = 2;
2791                         } else if (mbs.param[1] == MBGSD_ONEGB) {
2792                                 isp_prt(isp, ISP_LOGINFO, "Chan %d 1Gb link speed", chan);
2793                                 fcp->isp_gbspeed = 1;
2794                         }
2795                 }
2796         }
2797
2798         /*
2799          * Announce ourselves, too.
2800          */
2801         isp_prt(isp, ISP_LOGSANCFG|ISP_LOGCONFIG, topology, chan, (uint32_t) (fcp->isp_wwpn >> 32), (uint32_t) fcp->isp_wwpn, fcp->isp_portid, fcp->isp_loopid, isp_fc_toponame(fcp));
2802         isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0, "Chan %d FC Link Test Complete", chan);
2803         return (0);
2804 }
2805
2806 /*
2807  * Complete the synchronization of our Port Database.
2808  *
2809  * At this point, we've scanned the local loop (if any) and the fabric
2810  * and performed fabric logins on all new devices.
2811  *
2812  * Our task here is to go through our port database and remove any entities
2813  * that are still marked probational (issuing PLOGO for ones which we had
2814  * PLOGI'd into) or are dead.
2815  *
2816  * Our task here is to also check policy to decide whether devices which
2817  * have *changed* in some way should still be kept active. For example,
2818  * if a device has just changed PortID, we can either elect to treat it
2819  * as an old device or as a newly arrived device (and notify the outer
2820  * layer appropriately).
2821  *
2822  * We also do initiator map target id assignment here for new initiator
2823  * devices and refresh old ones ot make sure that they point to the corret
2824  * entities.
2825  */
2826 static int
2827 isp_pdb_sync(ispsoftc_t *isp, int chan)
2828 {
2829         fcparam *fcp = FCPARAM(isp, chan);
2830         fcportdb_t *lp;
2831         uint16_t dbidx;
2832
2833         if (fcp->isp_loopstate == LOOP_READY) {
2834                 return (0);
2835         }
2836
2837         /*
2838          * Make sure we're okay for doing this right now.
2839          */
2840         if (fcp->isp_loopstate != LOOP_PDB_RCVD &&
2841             fcp->isp_loopstate != LOOP_FSCAN_DONE &&
2842             fcp->isp_loopstate != LOOP_LSCAN_DONE) {
2843                 isp_prt(isp, ISP_LOGWARN, "isp_pdb_sync: bad loopstate %d",
2844                     fcp->isp_loopstate);
2845                 return (-1);
2846         }
2847
2848         if (fcp->isp_topo == TOPO_FL_PORT ||
2849             fcp->isp_topo == TOPO_NL_PORT ||
2850             fcp->isp_topo == TOPO_N_PORT) {
2851                 if (fcp->isp_loopstate < LOOP_LSCAN_DONE) {
2852                         if (isp_scan_loop(isp, chan) != 0) {
2853                                 isp_prt(isp, ISP_LOGWARN,
2854                                     "isp_pdb_sync: isp_scan_loop failed");
2855                                 return (-1);
2856                         }
2857                 }
2858         }
2859
2860         if (fcp->isp_topo == TOPO_F_PORT || fcp->isp_topo == TOPO_FL_PORT) {
2861                 if (fcp->isp_loopstate < LOOP_FSCAN_DONE) {
2862                         if (isp_scan_fabric(isp, chan) != 0) {
2863                                 isp_prt(isp, ISP_LOGWARN,
2864                                     "isp_pdb_sync: isp_scan_fabric failed");
2865                                 return (-1);
2866                         }
2867                 }
2868         }
2869
2870         isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
2871             "Chan %d Synchronizing PDBs", chan);
2872
2873         fcp->isp_loopstate = LOOP_SYNCING_PDB;
2874
2875         for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
2876                 lp = &fcp->portdb[dbidx];
2877
2878                 if (lp->state == FC_PORTDB_STATE_NIL || lp->target_mode) {
2879                         continue;
2880                 }
2881
2882                 if (lp->state == FC_PORTDB_STATE_VALID) {
2883                         if (dbidx != FL_ID) {
2884                                 isp_prt(isp,
2885                                     ISP_LOGERR, "portdb idx %d already valid",
2886                                     dbidx);
2887                         }
2888                         continue;
2889                 }
2890
2891                 switch (lp->state) {
2892                 case FC_PORTDB_STATE_PROBATIONAL:
2893                 case FC_PORTDB_STATE_DEAD:
2894                         /*
2895                          * It's up to the outer layers to clear isp_dev_map.
2896                          */
2897                         lp->state = FC_PORTDB_STATE_NIL;
2898                         isp_async(isp, ISPASYNC_DEV_GONE, chan, lp);
2899                         if (lp->autologin == 0) {
2900                                 (void) isp_plogx(isp, chan, lp->handle,
2901                                     lp->portid,
2902                                     PLOGX_FLG_CMD_LOGO |
2903                                     PLOGX_FLG_IMPLICIT |
2904                                     PLOGX_FLG_FREE_NPHDL, 0);
2905                         } else {
2906                                 lp->autologin = 0;
2907                         }
2908                         lp->new_roles = 0;
2909                         lp->new_portid = 0;
2910                         /*
2911                          * Note that we might come out of this with our state
2912                          * set to FC_PORTDB_STATE_ZOMBIE.
2913                          */
2914                         break;
2915                 case FC_PORTDB_STATE_NEW:
2916                         /*
2917                          * It's up to the outer layers to assign a virtual
2918                          * target id in isp_dev_map (if any).
2919                          */
2920                         lp->portid = lp->new_portid;
2921                         lp->roles = lp->new_roles;
2922                         lp->state = FC_PORTDB_STATE_VALID;
2923                         isp_async(isp, ISPASYNC_DEV_ARRIVED, chan, lp);
2924                         lp->new_roles = 0;
2925                         lp->new_portid = 0;
2926                         lp->reserved = 0;
2927                         lp->new_reserved = 0;
2928                         break;
2929                 case FC_PORTDB_STATE_CHANGED:
2930 /*
2931  * XXXX FIX THIS
2932  */
2933                         lp->state = FC_PORTDB_STATE_VALID;
2934                         isp_async(isp, ISPASYNC_DEV_CHANGED, chan, lp);
2935                         lp->new_roles = 0;
2936                         lp->new_portid = 0;
2937                         lp->reserved = 0;
2938                         lp->new_reserved = 0;
2939                         break;
2940                 case FC_PORTDB_STATE_PENDING_VALID:
2941                         lp->portid = lp->new_portid;
2942                         lp->roles = lp->new_roles;
2943                         if (lp->dev_map_idx) {
2944                                 int t = lp->dev_map_idx - 1;
2945                                 fcp->isp_dev_map[t] = dbidx + 1;
2946                         }
2947                         lp->state = FC_PORTDB_STATE_VALID;
2948                         isp_async(isp, ISPASYNC_DEV_STAYED, chan, lp);
2949                         if (dbidx != FL_ID) {
2950                                 lp->new_roles = 0;
2951                                 lp->new_portid = 0;
2952                         }
2953                         lp->reserved = 0;
2954                         lp->new_reserved = 0;
2955                         break;
2956                 case FC_PORTDB_STATE_ZOMBIE:
2957                         break;
2958                 default:
2959                         isp_prt(isp, ISP_LOGWARN,
2960                             "isp_scan_loop: state %d for idx %d",
2961                             lp->state, dbidx);
2962                         isp_dump_portdb(isp, chan);
2963                 }
2964         }
2965
2966         /*
2967          * If we get here, we've for sure seen not only a valid loop
2968          * but know what is or isn't on it, so mark this for usage
2969          * in isp_start.
2970          */
2971         fcp->loop_seen_once = 1;
2972         fcp->isp_loopstate = LOOP_READY;
2973         return (0);
2974 }
2975
2976 /*
2977  * Scan local loop for devices.
2978  */
2979 static int
2980 isp_scan_loop(ispsoftc_t *isp, int chan)
2981 {
2982         fcportdb_t *lp, tmp;
2983         fcparam *fcp = FCPARAM(isp, chan);
2984         int i;
2985         isp_pdb_t pdb;
2986         uint16_t handle, lim = 0;
2987
2988         if (fcp->isp_fwstate < FW_READY ||
2989             fcp->isp_loopstate < LOOP_PDB_RCVD) {
2990                 return (-1);
2991         }
2992
2993         if (fcp->isp_loopstate > LOOP_SCANNING_LOOP) {
2994                 return (0);
2995         }
2996
2997         /*
2998          * Check our connection topology.
2999          *
3000          * If we're a public or private loop, we scan 0..125 as handle values.
3001          * The firmware has (typically) peformed a PLOGI for us. We skip this
3002          * step if we're a ISP_24XX in NP-IV mode.
3003          *
3004          * If we're a N-port connection, we treat this is a short loop (0..1).
3005          */
3006         switch (fcp->isp_topo) {
3007         case TOPO_NL_PORT:
3008                 lim = LOCAL_LOOP_LIM;
3009                 break;
3010         case TOPO_FL_PORT:
3011                 if (IS_24XX(isp) && isp->isp_nchan > 1) {
3012                         isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
3013                             "Chan %d Skipping Local Loop Scan", chan);
3014                         fcp->isp_loopstate = LOOP_LSCAN_DONE;
3015                         return (0);
3016                 }
3017                 lim = LOCAL_LOOP_LIM;
3018                 break;
3019         case TOPO_N_PORT:
3020                 lim = 2;
3021                 break;
3022         default:
3023                 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
3024                     "Chan %d no loop topology to scan", chan);
3025                 fcp->isp_loopstate = LOOP_LSCAN_DONE;
3026                 return (0);
3027         }
3028
3029         fcp->isp_loopstate = LOOP_SCANNING_LOOP;
3030
3031         isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
3032             "Chan %d FC scan loop 0..%d", chan, lim-1);
3033
3034
3035         /*
3036          * Run through the list and get the port database info for each one.
3037          */
3038         for (handle = 0; handle < lim; handle++) {
3039                 int r;
3040                 /*
3041                  * Don't scan "special" ids.
3042                  */
3043                 if (handle >= FL_ID && handle <= SNS_ID) {
3044                         continue;
3045                 }
3046                 if (ISP_CAP_2KLOGIN(isp)) {
3047                         if (handle >= NPH_RESERVED && handle <= NPH_FL_ID) {
3048                                 continue;
3049                         }
3050                 }
3051                 /*
3052                  * In older cards with older f/w GET_PORT_DATABASE has been
3053                  * known to hang. This trick gets around that problem.
3054                  */
3055                 if (IS_2100(isp) || IS_2200(isp)) {
3056                         uint64_t node_wwn = isp_get_wwn(isp, chan, handle, 1);
3057                         if (fcp->isp_loopstate < LOOP_SCANNING_LOOP) {
3058                                 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
3059                                     "Chan %d FC scan loop DONE (bad)", chan);
3060                                 return (-1);
3061                         }
3062                         if (node_wwn == INI_NONE) {
3063                                 continue;
3064                         }
3065                 }
3066
3067                 /*
3068                  * Get the port database entity for this index.
3069                  */
3070                 r = isp_getpdb(isp, chan, handle, &pdb, 1);
3071                 if (r != 0) {
3072                         isp_prt(isp, ISP_LOGDEBUG1,
3073                             "Chan %d FC scan loop handle %d returned %x",
3074                             chan, handle, r);
3075                         if (fcp->isp_loopstate < LOOP_SCANNING_LOOP) {
3076                                 ISP_MARK_PORTDB(isp, chan, 1);
3077                                 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
3078                                     "Chan %d FC scan loop DONE (bad)", chan);
3079                                 return (-1);
3080                         }
3081                         continue;
3082                 }
3083
3084                 if (fcp->isp_loopstate < LOOP_SCANNING_LOOP) {
3085                         ISP_MARK_PORTDB(isp, chan, 1);
3086                         isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
3087                             "Chan %d FC scan loop DONE (bad)", chan);
3088                         return (-1);
3089                 }
3090
3091                 /*
3092                  * On *very* old 2100 firmware we would end up sometimes
3093                  * with the firmware returning the port database entry
3094                  * for something else. We used to restart this, but
3095                  * now we just punt.
3096                  */
3097                 if (IS_2100(isp) && pdb.handle != handle) {
3098                         isp_prt(isp, ISP_LOGWARN,
3099                             "Chan %d cannot synchronize port database", chan);
3100                         ISP_MARK_PORTDB(isp, chan, 1);
3101                         isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
3102                             "Chan %d FC scan loop DONE (bad)", chan);
3103                         return (-1);
3104                 }
3105
3106                 /*
3107                  * Save the pertinent info locally.
3108                  */
3109                 MAKE_WWN_FROM_NODE_NAME(tmp.node_wwn, pdb.nodename);
3110                 MAKE_WWN_FROM_NODE_NAME(tmp.port_wwn, pdb.portname);
3111                 tmp.roles = (pdb.s3_role & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
3112                 tmp.portid = pdb.portid;
3113                 tmp.handle = pdb.handle;
3114
3115                 /*
3116                  * Check to make sure it's still a valid entry. The 24XX seems
3117                  * to return a portid but not a WWPN/WWNN or role for devices
3118                  * which shift on a loop.
3119                  */
3120                 if (tmp.node_wwn == 0 || tmp.port_wwn == 0 || tmp.portid == 0) {
3121                         int a, b, c;
3122                         a = (tmp.node_wwn == 0);
3123                         b = (tmp.port_wwn == 0);
3124                         c = (tmp.portid == 0);
3125                         if (a == 0 && b == 0) {
3126                                 tmp.node_wwn =
3127                                     isp_get_wwn(isp, chan, handle, 1);
3128                                 tmp.port_wwn =
3129                                     isp_get_wwn(isp, chan, handle, 0);
3130                                 if (tmp.node_wwn && tmp.port_wwn) {
3131                                         isp_prt(isp, ISP_LOGINFO, "DODGED!");
3132                                         goto cont;
3133                                 }
3134                         }
3135                         isp_prt(isp, ISP_LOGWARN,
3136                             "Chan %d bad pdb (%1d%1d%1d) @ handle 0x%x", chan,
3137                             a, b, c, handle);
3138                         isp_dump_portdb(isp, chan);
3139                         continue;
3140                 }
3141   cont:
3142
3143                 /*
3144                  * Now search the entire port database
3145                  * for the same Port and Node WWN.
3146                  */
3147                 for (i = 0; i < MAX_FC_TARG; i++) {
3148                         lp = &fcp->portdb[i];
3149
3150                         if (lp->state == FC_PORTDB_STATE_NIL ||
3151                             lp->target_mode) {
3152                                 continue;
3153                         }
3154                         if (lp->node_wwn != tmp.node_wwn) {
3155                                 continue;
3156                         }
3157                         if (lp->port_wwn != tmp.port_wwn) {
3158                                 continue;
3159                         }
3160
3161                         /*
3162                          * Okay- we've found a non-nil entry that matches.
3163                          * Check to make sure it's probational or a zombie.
3164                          */
3165                         if (lp->state != FC_PORTDB_STATE_PROBATIONAL &&
3166                             lp->state != FC_PORTDB_STATE_ZOMBIE) {
3167                                 isp_prt(isp, ISP_LOGERR,
3168                                     "Chan %d [%d] not probational/zombie (0x%x)",
3169                                     chan, i, lp->state);
3170                                 isp_dump_portdb(isp, chan);
3171                                 ISP_MARK_PORTDB(isp, chan, 1);
3172                                 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
3173                                     "Chan %d FC scan loop DONE (bad)", chan);
3174                                 return (-1);
3175                         }
3176
3177                         /*
3178                          * Mark the device as something the f/w logs into
3179                          * automatically.
3180                          */
3181                         lp->autologin = 1;
3182
3183                         /*
3184                          * Check to make see if really still the same
3185                          * device. If it is, we mark it pending valid.
3186                          */
3187                         if (lp->portid == tmp.portid &&
3188                             lp->handle == tmp.handle &&
3189                             lp->roles == tmp.roles) {
3190                                 lp->new_portid = tmp.portid;
3191                                 lp->new_roles = tmp.roles;
3192                                 lp->state = FC_PORTDB_STATE_PENDING_VALID;
3193                                 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
3194                                     "Chan %d Loop Port 0x%02x@0x%x Pending "
3195                                     "Valid", chan, tmp.portid, tmp.handle);
3196                                 break;
3197                         }
3198
3199                         /*
3200                          * We can wipe out the old handle value
3201                          * here because it's no longer valid.
3202                          */
3203                         lp->handle = tmp.handle;
3204
3205                         /*
3206                          * Claim that this has changed and let somebody else
3207                          * decide what to do.
3208                          */
3209                         isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
3210                             "Chan %d Loop Port 0x%02x@0x%x changed",
3211                             chan, tmp.portid, tmp.handle);
3212                         lp->state = FC_PORTDB_STATE_CHANGED;
3213                         lp->new_portid = tmp.portid;
3214                         lp->new_roles = tmp.roles;
3215                         break;
3216                 }
3217
3218                 /*
3219                  * Did we find and update an old entry?
3220                  */
3221                 if (i < MAX_FC_TARG) {
3222                         continue;
3223                 }
3224
3225                 /*
3226                  * Ah. A new device entry. Find an empty slot
3227                  * for it and save info for later disposition.
3228                  */
3229                 for (i = 0; i < MAX_FC_TARG; i++) {
3230                         if (fcp->portdb[i].target_mode) {
3231                                 continue;
3232                         }
3233                         if (fcp->portdb[i].state == FC_PORTDB_STATE_NIL) {
3234                                 break;
3235                         }
3236                 }
3237                 if (i == MAX_FC_TARG) {
3238                         isp_prt(isp, ISP_LOGERR,
3239                             "Chan %d out of portdb entries", chan);
3240                         continue;
3241                 }
3242                 lp = &fcp->portdb[i];
3243
3244                 ISP_MEMZERO(lp, sizeof (fcportdb_t));
3245                 lp->autologin = 1;
3246                 lp->state = FC_PORTDB_STATE_NEW;
3247                 lp->new_portid = tmp.portid;
3248                 lp->new_roles = tmp.roles;
3249                 lp->handle = tmp.handle;
3250                 lp->port_wwn = tmp.port_wwn;
3251                 lp->node_wwn = tmp.node_wwn;
3252                 isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
3253                     "Chan %d Loop Port 0x%02x@0x%x is New Entry",
3254                     chan, tmp.portid, tmp.handle);
3255         }
3256         fcp->isp_loopstate = LOOP_LSCAN_DONE;
3257         isp_prt(isp, ISP_LOGSANCFG|ISP_LOGDEBUG0,
3258             "Chan %d FC scan loop DONE", chan);
3259         return (0);
3260 }
3261
3262 /*
3263  * Scan the fabric for devices and add them to our port database.
3264  *
3265  * Use the GID_FT command to get all Port IDs for FC4 SCSI devices it knows.
3266  *
3267  * For 2100-23XX cards, we can use the SNS mailbox command to pass simple
3268  * name server commands to the switch management server via the QLogic f/w.
3269  *
3270  * For the 24XX card, we have to use CT-Pass through run via the Execute IOCB
3271  * mailbox command.
3272  *
3273  * The net result is to leave the list of Port IDs setting untranslated in
3274  * offset IGPOFF of the FC scratch area, whereupon we'll canonicalize it to
3275  * host order at OGPOFF.
3276  */
3277
3278 /*
3279  * Take less than half of our scratch area to store Port IDs
3280  */
3281 #define GIDLEN  ((ISP_FC_SCRLEN >> 1) - 16 - SNS_GID_FT_REQ_SIZE)
3282 #define NGENT   ((GIDLEN - 16) >> 2)
3283
3284 #define IGPOFF  (2 * QENTRY_LEN)
3285 #define OGPOFF  (ISP_FC_SCRLEN >> 1)
3286 #define ZTXOFF  (ISP_FC_SCRLEN - (1 * QENTRY_LEN))
3287 #define CTXOFF  (ISP_FC_SCRLEN - (2 * QENTRY_LEN))
3288 #define XTXOFF  (ISP_FC_SCRLEN - (3 * QENTRY_LEN))
3289
3290 static int
3291 isp_gid_ft_sns(ispsoftc_t *isp, int chan)
3292 {
3293         union {
3294                 sns_gid_ft_req_t _x;
3295                 uint8_t _y[SNS_GID_FT_REQ_SIZE];
3296         } un;
3297         fcparam *fcp = FCPARAM(isp, chan);
3298         sns_gid_ft_req_t *rq = &un._x;
3299         mbreg_t mbs;
3300
3301         isp_prt(isp, ISP_LOGDEBUG0,
3302             "Chan %d scanning fabric (GID_FT) via SNS", chan);
3303
3304         ISP_MEMZERO(rq, SNS_GID_FT_REQ_SIZE);
3305         rq->snscb_rblen = GIDLEN >> 1;
3306         rq->snscb_addr[RQRSP_ADDR0015] = DMA_WD0(fcp->isp_scdma + IGPOFF);
3307         rq->snscb_addr[RQRSP_ADDR1631] = DMA_WD1(fcp->isp_scdma + IGPOFF);
3308         rq->snscb_addr[RQRSP_ADDR3247] = DMA_WD2(fcp->isp_scdma + IGPOFF);
3309         rq->snscb_addr[RQRSP_ADDR4863] = DMA_WD3(fcp->isp_scdma + IGPOFF);
3310         rq->snscb_sblen = 6;
3311         rq->snscb_cmd = SNS_GID_FT;
3312         rq->snscb_mword_div_2 = NGENT;
3313         rq->snscb_fc4_type = FC4_SCSI;
3314
3315         isp_put_gid_ft_request(isp, rq, fcp->isp_scratch);
3316         MEMORYBARRIER(isp, SYNC_SFORDEV, 0, SNS_GID_FT_REQ_SIZE);
3317
3318         MBSINIT(&mbs, MBOX_SEND_SNS, MBLOGALL, 10000000);
3319         mbs.param[0] = MBOX_SEND_SNS;
3320         mbs.param[1] = SNS_GID_FT_REQ_SIZE >> 1;
3321         mbs.param[2] = DMA_WD1(fcp->isp_scdma);
3322         mbs.param[3] = DMA_WD0(fcp->isp_scdma);
3323         mbs.param[6] = DMA_WD3(fcp->isp_scdma);
3324         mbs.param[7] = DMA_WD2(fcp->isp_scdma);
3325         isp_mboxcmd(isp, &mbs);
3326         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3327                 if (mbs.param[0] == MBOX_INVALID_COMMAND) {
3328                         return (1);
3329                 } else {
3330                         return (-1);
3331                 }
3332         }
3333         return (0);
3334 }
3335
3336 static int
3337 isp_gid_ft_ct_passthru(ispsoftc_t *isp, int chan)
3338 {
3339         mbreg_t mbs;
3340         fcparam *fcp = FCPARAM(isp, chan);
3341         union {
3342                 isp_ct_pt_t plocal;
3343                 ct_hdr_t clocal;
3344                 uint8_t q[QENTRY_LEN];
3345         } un;
3346         isp_ct_pt_t *pt;
3347         ct_hdr_t *ct;
3348         uint32_t *rp;
3349         uint8_t *scp = fcp->isp_scratch;
3350
3351         isp_prt(isp, ISP_LOGDEBUG0,
3352             "Chan %d scanning fabric (GID_FT) via CT", chan);
3353
3354         if (!IS_24XX(isp)) {
3355                 return (1);
3356         }
3357
3358         /*
3359          * Build a Passthrough IOCB in memory.
3360          */
3361         pt = &un.plocal;
3362         ISP_MEMZERO(un.q, QENTRY_LEN);
3363         pt->ctp_header.rqs_entry_count = 1;
3364         pt->ctp_header.rqs_entry_type = RQSTYPE_CT_PASSTHRU;
3365         pt->ctp_handle = 0xffffffff;
3366         pt->ctp_nphdl = fcp->isp_sns_hdl;
3367         pt->ctp_cmd_cnt = 1;
3368         pt->ctp_vpidx = chan;
3369         pt->ctp_time = 30;
3370         pt->ctp_rsp_cnt = 1;
3371         pt->ctp_rsp_bcnt = GIDLEN;
3372         pt->ctp_cmd_bcnt = sizeof (*ct) + sizeof (uint32_t);
3373         pt->ctp_dataseg[0].ds_base = DMA_LO32(fcp->isp_scdma+XTXOFF);
3374         pt->ctp_dataseg[0].ds_basehi = DMA_HI32(fcp->isp_scdma+XTXOFF);
3375         pt->ctp_dataseg[0].ds_count = sizeof (*ct) + sizeof (uint32_t);
3376         pt->ctp_dataseg[1].ds_base = DMA_LO32(fcp->isp_scdma+IGPOFF);
3377         pt->ctp_dataseg[1].ds_basehi = DMA_HI32(fcp->isp_scdma+IGPOFF);
3378         pt->ctp_dataseg[1].ds_count = GIDLEN;
3379         if (isp->isp_dblev & ISP_LOGDEBUG1) {
3380                 isp_print_bytes(isp, "ct IOCB", QENTRY_LEN, pt);
3381         }
3382         isp_put_ct_pt(isp, pt, (isp_ct_pt_t *) &scp[CTXOFF]);
3383
3384         /*
3385          * Build the CT header and command in memory.
3386          *
3387          * Note that the CT header has to end up as Big Endian format in memory.
3388          */
3389         ct = &un.clocal;
3390         ISP_MEMZERO(ct, sizeof (*ct));
3391         ct->ct_revision = CT_REVISION;
3392         ct->ct_fcs_type = CT_FC_TYPE_FC;
3393         ct->ct_fcs_subtype = CT_FC_SUBTYPE_NS;
3394         ct->ct_cmd_resp = SNS_GID_FT;
3395         ct->ct_bcnt_resid = (GIDLEN - 16) >> 2;
3396
3397         isp_put_ct_hdr(isp, ct, (ct_hdr_t *) &scp[XTXOFF]);
3398         rp = (uint32_t *) &scp[XTXOFF+sizeof (*ct)];
3399         ISP_IOZPUT_32(isp, FC4_SCSI, rp);
3400         if (isp->isp_dblev & ISP_LOGDEBUG1) {
3401                 isp_print_bytes(isp, "CT HDR + payload after put",
3402                     sizeof (*ct) + sizeof (uint32_t), &scp[XTXOFF]);
3403         }
3404         ISP_MEMZERO(&scp[ZTXOFF], QENTRY_LEN);
3405         MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 500000);
3406         mbs.param[1] = QENTRY_LEN;
3407         mbs.param[2] = DMA_WD1(fcp->isp_scdma + CTXOFF);
3408         mbs.param[3] = DMA_WD0(fcp->isp_scdma + CTXOFF);
3409         mbs.param[6] = DMA_WD3(fcp->isp_scdma + CTXOFF);
3410         mbs.param[7] = DMA_WD2(fcp->isp_scdma + CTXOFF);
3411         MEMORYBARRIER(isp, SYNC_SFORDEV, XTXOFF, 2 * QENTRY_LEN);
3412         isp_mboxcmd(isp, &mbs);
3413         if (mbs.param[0] != MBOX_COMMAND_COMPLETE) {
3414                 return (-1);
3415         }
3416         MEMORYBARRIER(isp, SYNC_SFORCPU, ZTXOFF, QENTRY_LEN);
3417         pt = &un.plocal;
3418         isp_get_ct_pt(isp, (isp_ct_pt_t *) &scp[ZTXOFF], pt);
3419         if (isp->isp_dblev & ISP_LOGDEBUG1) {
3420                 isp_print_bytes(isp, "IOCB response", QENTRY_LEN, pt);
3421         }
3422
3423         if (pt->ctp_status && pt->ctp_status != RQCS_DATA_UNDERRUN) {
3424                 isp_prt(isp, ISP_LOGWARN,
3425                     "Chan %d ISP GID FT CT Passthrough returned 0x%x",
3426                     chan, pt->ctp_status);
3427                 return (-1);