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