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