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