linux/drivers/scsi/qla2xxx/qla_mbx.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * QLogic Fibre Channel HBA Driver
   4 * Copyright (c)  2003-2014 QLogic Corporation
   5 */
   6#include "qla_def.h"
   7#include "qla_target.h"
   8
   9#include <linux/delay.h>
  10#include <linux/gfp.h>
  11
  12static struct mb_cmd_name {
  13        uint16_t cmd;
  14        const char *str;
  15} mb_str[] = {
  16        {MBC_GET_PORT_DATABASE,         "GPDB"},
  17        {MBC_GET_ID_LIST,               "GIDList"},
  18        {MBC_GET_LINK_PRIV_STATS,       "Stats"},
  19        {MBC_GET_RESOURCE_COUNTS,       "ResCnt"},
  20};
  21
  22static const char *mb_to_str(uint16_t cmd)
  23{
  24        int i;
  25        struct mb_cmd_name *e;
  26
  27        for (i = 0; i < ARRAY_SIZE(mb_str); i++) {
  28                e = mb_str + i;
  29                if (cmd == e->cmd)
  30                        return e->str;
  31        }
  32        return "unknown";
  33}
  34
  35static struct rom_cmd {
  36        uint16_t cmd;
  37} rom_cmds[] = {
  38        { MBC_LOAD_RAM },
  39        { MBC_EXECUTE_FIRMWARE },
  40        { MBC_READ_RAM_WORD },
  41        { MBC_MAILBOX_REGISTER_TEST },
  42        { MBC_VERIFY_CHECKSUM },
  43        { MBC_GET_FIRMWARE_VERSION },
  44        { MBC_LOAD_RISC_RAM },
  45        { MBC_DUMP_RISC_RAM },
  46        { MBC_LOAD_RISC_RAM_EXTENDED },
  47        { MBC_DUMP_RISC_RAM_EXTENDED },
  48        { MBC_WRITE_RAM_WORD_EXTENDED },
  49        { MBC_READ_RAM_EXTENDED },
  50        { MBC_GET_RESOURCE_COUNTS },
  51        { MBC_SET_FIRMWARE_OPTION },
  52        { MBC_MID_INITIALIZE_FIRMWARE },
  53        { MBC_GET_FIRMWARE_STATE },
  54        { MBC_GET_MEM_OFFLOAD_CNTRL_STAT },
  55        { MBC_GET_RETRY_COUNT },
  56        { MBC_TRACE_CONTROL },
  57        { MBC_INITIALIZE_MULTIQ },
  58        { MBC_IOCB_COMMAND_A64 },
  59        { MBC_GET_ADAPTER_LOOP_ID },
  60        { MBC_READ_SFP },
  61        { MBC_SET_RNID_PARAMS },
  62        { MBC_GET_RNID_PARAMS },
  63        { MBC_GET_SET_ZIO_THRESHOLD },
  64};
  65
  66static int is_rom_cmd(uint16_t cmd)
  67{
  68        int i;
  69        struct  rom_cmd *wc;
  70
  71        for (i = 0; i < ARRAY_SIZE(rom_cmds); i++) {
  72                wc = rom_cmds + i;
  73                if (wc->cmd == cmd)
  74                        return 1;
  75        }
  76
  77        return 0;
  78}
  79
  80/*
  81 * qla2x00_mailbox_command
  82 *      Issue mailbox command and waits for completion.
  83 *
  84 * Input:
  85 *      ha = adapter block pointer.
  86 *      mcp = driver internal mbx struct pointer.
  87 *
  88 * Output:
  89 *      mb[MAX_MAILBOX_REGISTER_COUNT] = returned mailbox data.
  90 *
  91 * Returns:
  92 *      0 : QLA_SUCCESS = cmd performed success
  93 *      1 : QLA_FUNCTION_FAILED   (error encountered)
  94 *      6 : QLA_FUNCTION_TIMEOUT (timeout condition encountered)
  95 *
  96 * Context:
  97 *      Kernel context.
  98 */
  99static int
 100qla2x00_mailbox_command(scsi_qla_host_t *vha, mbx_cmd_t *mcp)
 101{
 102        int             rval, i;
 103        unsigned long    flags = 0;
 104        device_reg_t *reg;
 105        uint8_t         abort_active, eeh_delay;
 106        uint8_t         io_lock_on;
 107        uint16_t        command = 0;
 108        uint16_t        *iptr;
 109        __le16 __iomem  *optr;
 110        uint32_t        cnt;
 111        uint32_t        mboxes;
 112        unsigned long   wait_time;
 113        struct qla_hw_data *ha = vha->hw;
 114        scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
 115        u32 chip_reset;
 116
 117
 118        ql_dbg(ql_dbg_mbx, vha, 0x1000, "Entered %s.\n", __func__);
 119
 120        if (ha->pdev->error_state == pci_channel_io_perm_failure) {
 121                ql_log(ql_log_warn, vha, 0x1001,
 122                    "PCI channel failed permanently, exiting.\n");
 123                return QLA_FUNCTION_TIMEOUT;
 124        }
 125
 126        if (vha->device_flags & DFLG_DEV_FAILED) {
 127                ql_log(ql_log_warn, vha, 0x1002,
 128                    "Device in failed state, exiting.\n");
 129                return QLA_FUNCTION_TIMEOUT;
 130        }
 131
 132        /* if PCI error, then avoid mbx processing.*/
 133        if (test_bit(PFLG_DISCONNECTED, &base_vha->dpc_flags) &&
 134            test_bit(UNLOADING, &base_vha->dpc_flags)) {
 135                ql_log(ql_log_warn, vha, 0xd04e,
 136                    "PCI error, exiting.\n");
 137                return QLA_FUNCTION_TIMEOUT;
 138        }
 139        eeh_delay = 0;
 140        reg = ha->iobase;
 141        io_lock_on = base_vha->flags.init_done;
 142
 143        rval = QLA_SUCCESS;
 144        abort_active = test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
 145        chip_reset = ha->chip_reset;
 146
 147        if (ha->flags.pci_channel_io_perm_failure) {
 148                ql_log(ql_log_warn, vha, 0x1003,
 149                    "Perm failure on EEH timeout MBX, exiting.\n");
 150                return QLA_FUNCTION_TIMEOUT;
 151        }
 152
 153        if (IS_P3P_TYPE(ha) && ha->flags.isp82xx_fw_hung) {
 154                /* Setting Link-Down error */
 155                mcp->mb[0] = MBS_LINK_DOWN_ERROR;
 156                ql_log(ql_log_warn, vha, 0x1004,
 157                    "FW hung = %d.\n", ha->flags.isp82xx_fw_hung);
 158                return QLA_FUNCTION_TIMEOUT;
 159        }
 160
 161        /* check if ISP abort is active and return cmd with timeout */
 162        if (((test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
 163              test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
 164              test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags)) &&
 165              !is_rom_cmd(mcp->mb[0])) || ha->flags.eeh_busy) {
 166                ql_log(ql_log_info, vha, 0x1005,
 167                    "Cmd 0x%x aborted with timeout since ISP Abort is pending\n",
 168                    mcp->mb[0]);
 169                return QLA_FUNCTION_TIMEOUT;
 170        }
 171
 172        atomic_inc(&ha->num_pend_mbx_stage1);
 173        /*
 174         * Wait for active mailbox commands to finish by waiting at most tov
 175         * seconds. This is to serialize actual issuing of mailbox cmds during
 176         * non ISP abort time.
 177         */
 178        if (!wait_for_completion_timeout(&ha->mbx_cmd_comp, mcp->tov * HZ)) {
 179                /* Timeout occurred. Return error. */
 180                ql_log(ql_log_warn, vha, 0xd035,
 181                    "Cmd access timeout, cmd=0x%x, Exiting.\n",
 182                    mcp->mb[0]);
 183                vha->hw_err_cnt++;
 184                atomic_dec(&ha->num_pend_mbx_stage1);
 185                return QLA_FUNCTION_TIMEOUT;
 186        }
 187        atomic_dec(&ha->num_pend_mbx_stage1);
 188        if (ha->flags.purge_mbox || chip_reset != ha->chip_reset ||
 189            ha->flags.eeh_busy) {
 190                ql_log(ql_log_warn, vha, 0xd035,
 191                       "Error detected: purge[%d] eeh[%d] cmd=0x%x, Exiting.\n",
 192                       ha->flags.purge_mbox, ha->flags.eeh_busy, mcp->mb[0]);
 193                rval = QLA_ABORTED;
 194                goto premature_exit;
 195        }
 196
 197
 198        /* Save mailbox command for debug */
 199        ha->mcp = mcp;
 200
 201        ql_dbg(ql_dbg_mbx, vha, 0x1006,
 202            "Prepare to issue mbox cmd=0x%x.\n", mcp->mb[0]);
 203
 204        spin_lock_irqsave(&ha->hardware_lock, flags);
 205
 206        if (ha->flags.purge_mbox || chip_reset != ha->chip_reset ||
 207            ha->flags.mbox_busy) {
 208                rval = QLA_ABORTED;
 209                spin_unlock_irqrestore(&ha->hardware_lock, flags);
 210                goto premature_exit;
 211        }
 212        ha->flags.mbox_busy = 1;
 213
 214        /* Load mailbox registers. */
 215        if (IS_P3P_TYPE(ha))
 216                optr = &reg->isp82.mailbox_in[0];
 217        else if (IS_FWI2_CAPABLE(ha) && !(IS_P3P_TYPE(ha)))
 218                optr = &reg->isp24.mailbox0;
 219        else
 220                optr = MAILBOX_REG(ha, &reg->isp, 0);
 221
 222        iptr = mcp->mb;
 223        command = mcp->mb[0];
 224        mboxes = mcp->out_mb;
 225
 226        ql_dbg(ql_dbg_mbx, vha, 0x1111,
 227            "Mailbox registers (OUT):\n");
 228        for (cnt = 0; cnt < ha->mbx_count; cnt++) {
 229                if (IS_QLA2200(ha) && cnt == 8)
 230                        optr = MAILBOX_REG(ha, &reg->isp, 8);
 231                if (mboxes & BIT_0) {
 232                        ql_dbg(ql_dbg_mbx, vha, 0x1112,
 233                            "mbox[%d]<-0x%04x\n", cnt, *iptr);
 234                        wrt_reg_word(optr, *iptr);
 235                }
 236
 237                mboxes >>= 1;
 238                optr++;
 239                iptr++;
 240        }
 241
 242        ql_dbg(ql_dbg_mbx + ql_dbg_buffer, vha, 0x1117,
 243            "I/O Address = %p.\n", optr);
 244
 245        /* Issue set host interrupt command to send cmd out. */
 246        ha->flags.mbox_int = 0;
 247        clear_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
 248
 249        /* Unlock mbx registers and wait for interrupt */
 250        ql_dbg(ql_dbg_mbx, vha, 0x100f,
 251            "Going to unlock irq & waiting for interrupts. "
 252            "jiffies=%lx.\n", jiffies);
 253
 254        /* Wait for mbx cmd completion until timeout */
 255        atomic_inc(&ha->num_pend_mbx_stage2);
 256        if ((!abort_active && io_lock_on) || IS_NOPOLLING_TYPE(ha)) {
 257                set_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags);
 258
 259                if (IS_P3P_TYPE(ha))
 260                        wrt_reg_dword(&reg->isp82.hint, HINT_MBX_INT_PENDING);
 261                else if (IS_FWI2_CAPABLE(ha))
 262                        wrt_reg_dword(&reg->isp24.hccr, HCCRX_SET_HOST_INT);
 263                else
 264                        wrt_reg_word(&reg->isp.hccr, HCCR_SET_HOST_INT);
 265                spin_unlock_irqrestore(&ha->hardware_lock, flags);
 266
 267                wait_time = jiffies;
 268                atomic_inc(&ha->num_pend_mbx_stage3);
 269                if (!wait_for_completion_timeout(&ha->mbx_intr_comp,
 270                    mcp->tov * HZ)) {
 271                        if (chip_reset != ha->chip_reset) {
 272                                eeh_delay = ha->flags.eeh_busy ? 1 : 0;
 273
 274                                spin_lock_irqsave(&ha->hardware_lock, flags);
 275                                ha->flags.mbox_busy = 0;
 276                                spin_unlock_irqrestore(&ha->hardware_lock,
 277                                    flags);
 278                                atomic_dec(&ha->num_pend_mbx_stage2);
 279                                atomic_dec(&ha->num_pend_mbx_stage3);
 280                                rval = QLA_ABORTED;
 281                                goto premature_exit;
 282                        }
 283                        ql_dbg(ql_dbg_mbx, vha, 0x117a,
 284                            "cmd=%x Timeout.\n", command);
 285                        spin_lock_irqsave(&ha->hardware_lock, flags);
 286                        clear_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags);
 287                        spin_unlock_irqrestore(&ha->hardware_lock, flags);
 288
 289                } else if (ha->flags.purge_mbox ||
 290                    chip_reset != ha->chip_reset) {
 291                        eeh_delay = ha->flags.eeh_busy ? 1 : 0;
 292
 293                        spin_lock_irqsave(&ha->hardware_lock, flags);
 294                        ha->flags.mbox_busy = 0;
 295                        spin_unlock_irqrestore(&ha->hardware_lock, flags);
 296                        atomic_dec(&ha->num_pend_mbx_stage2);
 297                        atomic_dec(&ha->num_pend_mbx_stage3);
 298                        rval = QLA_ABORTED;
 299                        goto premature_exit;
 300                }
 301                atomic_dec(&ha->num_pend_mbx_stage3);
 302
 303                if (time_after(jiffies, wait_time + 5 * HZ))
 304                        ql_log(ql_log_warn, vha, 0x1015, "cmd=0x%x, waited %d msecs\n",
 305                            command, jiffies_to_msecs(jiffies - wait_time));
 306        } else {
 307                ql_dbg(ql_dbg_mbx, vha, 0x1011,
 308                    "Cmd=%x Polling Mode.\n", command);
 309
 310                if (IS_P3P_TYPE(ha)) {
 311                        if (rd_reg_dword(&reg->isp82.hint) &
 312                                HINT_MBX_INT_PENDING) {
 313                                ha->flags.mbox_busy = 0;
 314                                spin_unlock_irqrestore(&ha->hardware_lock,
 315                                        flags);
 316                                atomic_dec(&ha->num_pend_mbx_stage2);
 317                                ql_dbg(ql_dbg_mbx, vha, 0x1012,
 318                                    "Pending mailbox timeout, exiting.\n");
 319                                vha->hw_err_cnt++;
 320                                rval = QLA_FUNCTION_TIMEOUT;
 321                                goto premature_exit;
 322                        }
 323                        wrt_reg_dword(&reg->isp82.hint, HINT_MBX_INT_PENDING);
 324                } else if (IS_FWI2_CAPABLE(ha))
 325                        wrt_reg_dword(&reg->isp24.hccr, HCCRX_SET_HOST_INT);
 326                else
 327                        wrt_reg_word(&reg->isp.hccr, HCCR_SET_HOST_INT);
 328                spin_unlock_irqrestore(&ha->hardware_lock, flags);
 329
 330                wait_time = jiffies + mcp->tov * HZ; /* wait at most tov secs */
 331                while (!ha->flags.mbox_int) {
 332                        if (ha->flags.purge_mbox ||
 333                            chip_reset != ha->chip_reset) {
 334                                eeh_delay = ha->flags.eeh_busy ? 1 : 0;
 335
 336                                spin_lock_irqsave(&ha->hardware_lock, flags);
 337                                ha->flags.mbox_busy = 0;
 338                                spin_unlock_irqrestore(&ha->hardware_lock,
 339                                    flags);
 340                                atomic_dec(&ha->num_pend_mbx_stage2);
 341                                rval = QLA_ABORTED;
 342                                goto premature_exit;
 343                        }
 344
 345                        if (time_after(jiffies, wait_time))
 346                                break;
 347
 348                        /* Check for pending interrupts. */
 349                        qla2x00_poll(ha->rsp_q_map[0]);
 350
 351                        if (!ha->flags.mbox_int &&
 352                            !(IS_QLA2200(ha) &&
 353                            command == MBC_LOAD_RISC_RAM_EXTENDED))
 354                                msleep(10);
 355                } /* while */
 356                ql_dbg(ql_dbg_mbx, vha, 0x1013,
 357                    "Waited %d sec.\n",
 358                    (uint)((jiffies - (wait_time - (mcp->tov * HZ)))/HZ));
 359        }
 360        atomic_dec(&ha->num_pend_mbx_stage2);
 361
 362        /* Check whether we timed out */
 363        if (ha->flags.mbox_int) {
 364                uint16_t *iptr2;
 365
 366                ql_dbg(ql_dbg_mbx, vha, 0x1014,
 367                    "Cmd=%x completed.\n", command);
 368
 369                /* Got interrupt. Clear the flag. */
 370                ha->flags.mbox_int = 0;
 371                clear_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
 372
 373                if (IS_P3P_TYPE(ha) && ha->flags.isp82xx_fw_hung) {
 374                        spin_lock_irqsave(&ha->hardware_lock, flags);
 375                        ha->flags.mbox_busy = 0;
 376                        spin_unlock_irqrestore(&ha->hardware_lock, flags);
 377
 378                        /* Setting Link-Down error */
 379                        mcp->mb[0] = MBS_LINK_DOWN_ERROR;
 380                        ha->mcp = NULL;
 381                        rval = QLA_FUNCTION_FAILED;
 382                        ql_log(ql_log_warn, vha, 0xd048,
 383                            "FW hung = %d.\n", ha->flags.isp82xx_fw_hung);
 384                        goto premature_exit;
 385                }
 386
 387                if (ha->mailbox_out[0] != MBS_COMMAND_COMPLETE) {
 388                        ql_dbg(ql_dbg_mbx, vha, 0x11ff,
 389                               "mb_out[0] = %#x <> %#x\n", ha->mailbox_out[0],
 390                               MBS_COMMAND_COMPLETE);
 391                        rval = QLA_FUNCTION_FAILED;
 392                }
 393
 394                /* Load return mailbox registers. */
 395                iptr2 = mcp->mb;
 396                iptr = (uint16_t *)&ha->mailbox_out[0];
 397                mboxes = mcp->in_mb;
 398
 399                ql_dbg(ql_dbg_mbx, vha, 0x1113,
 400                    "Mailbox registers (IN):\n");
 401                for (cnt = 0; cnt < ha->mbx_count; cnt++) {
 402                        if (mboxes & BIT_0) {
 403                                *iptr2 = *iptr;
 404                                ql_dbg(ql_dbg_mbx, vha, 0x1114,
 405                                    "mbox[%d]->0x%04x\n", cnt, *iptr2);
 406                        }
 407
 408                        mboxes >>= 1;
 409                        iptr2++;
 410                        iptr++;
 411                }
 412        } else {
 413
 414                uint16_t mb[8];
 415                uint32_t ictrl, host_status, hccr;
 416                uint16_t        w;
 417
 418                if (IS_FWI2_CAPABLE(ha)) {
 419                        mb[0] = rd_reg_word(&reg->isp24.mailbox0);
 420                        mb[1] = rd_reg_word(&reg->isp24.mailbox1);
 421                        mb[2] = rd_reg_word(&reg->isp24.mailbox2);
 422                        mb[3] = rd_reg_word(&reg->isp24.mailbox3);
 423                        mb[7] = rd_reg_word(&reg->isp24.mailbox7);
 424                        ictrl = rd_reg_dword(&reg->isp24.ictrl);
 425                        host_status = rd_reg_dword(&reg->isp24.host_status);
 426                        hccr = rd_reg_dword(&reg->isp24.hccr);
 427
 428                        ql_log(ql_log_warn, vha, 0xd04c,
 429                            "MBX Command timeout for cmd %x, iocontrol=%x jiffies=%lx "
 430                            "mb[0-3]=[0x%x 0x%x 0x%x 0x%x] mb7 0x%x host_status 0x%x hccr 0x%x\n",
 431                            command, ictrl, jiffies, mb[0], mb[1], mb[2], mb[3],
 432                            mb[7], host_status, hccr);
 433                        vha->hw_err_cnt++;
 434
 435                } else {
 436                        mb[0] = RD_MAILBOX_REG(ha, &reg->isp, 0);
 437                        ictrl = rd_reg_word(&reg->isp.ictrl);
 438                        ql_dbg(ql_dbg_mbx + ql_dbg_buffer, vha, 0x1119,
 439                            "MBX Command timeout for cmd %x, iocontrol=%x jiffies=%lx "
 440                            "mb[0]=0x%x\n", command, ictrl, jiffies, mb[0]);
 441                        vha->hw_err_cnt++;
 442                }
 443                ql_dump_regs(ql_dbg_mbx + ql_dbg_buffer, vha, 0x1019);
 444
 445                /* Capture FW dump only, if PCI device active */
 446                if (!pci_channel_offline(vha->hw->pdev)) {
 447                        pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
 448                        if (w == 0xffff || ictrl == 0xffffffff ||
 449                            (chip_reset != ha->chip_reset)) {
 450                                /* This is special case if there is unload
 451                                 * of driver happening and if PCI device go
 452                                 * into bad state due to PCI error condition
 453                                 * then only PCI ERR flag would be set.
 454                                 * we will do premature exit for above case.
 455                                 */
 456                                spin_lock_irqsave(&ha->hardware_lock, flags);
 457                                ha->flags.mbox_busy = 0;
 458                                spin_unlock_irqrestore(&ha->hardware_lock,
 459                                    flags);
 460                                rval = QLA_FUNCTION_TIMEOUT;
 461                                goto premature_exit;
 462                        }
 463
 464                        /* Attempt to capture firmware dump for further
 465                         * anallysis of the current formware state. we do not
 466                         * need to do this if we are intentionally generating
 467                         * a dump
 468                         */
 469                        if (mcp->mb[0] != MBC_GEN_SYSTEM_ERROR)
 470                                qla2xxx_dump_fw(vha);
 471                        rval = QLA_FUNCTION_TIMEOUT;
 472                 }
 473        }
 474        spin_lock_irqsave(&ha->hardware_lock, flags);
 475        ha->flags.mbox_busy = 0;
 476        spin_unlock_irqrestore(&ha->hardware_lock, flags);
 477
 478        /* Clean up */
 479        ha->mcp = NULL;
 480
 481        if ((abort_active || !io_lock_on) && !IS_NOPOLLING_TYPE(ha)) {
 482                ql_dbg(ql_dbg_mbx, vha, 0x101a,
 483                    "Checking for additional resp interrupt.\n");
 484
 485                /* polling mode for non isp_abort commands. */
 486                qla2x00_poll(ha->rsp_q_map[0]);
 487        }
 488
 489        if (rval == QLA_FUNCTION_TIMEOUT &&
 490            mcp->mb[0] != MBC_GEN_SYSTEM_ERROR) {
 491                if (!io_lock_on || (mcp->flags & IOCTL_CMD) ||
 492                    ha->flags.eeh_busy) {
 493                        /* not in dpc. schedule it for dpc to take over. */
 494                        ql_dbg(ql_dbg_mbx, vha, 0x101b,
 495                            "Timeout, schedule isp_abort_needed.\n");
 496
 497                        if (!test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) &&
 498                            !test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) &&
 499                            !test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
 500                                if (IS_QLA82XX(ha)) {
 501                                        ql_dbg(ql_dbg_mbx, vha, 0x112a,
 502                                            "disabling pause transmit on port "
 503                                            "0 & 1.\n");
 504                                        qla82xx_wr_32(ha,
 505                                            QLA82XX_CRB_NIU + 0x98,
 506                                            CRB_NIU_XG_PAUSE_CTL_P0|
 507                                            CRB_NIU_XG_PAUSE_CTL_P1);
 508                                }
 509                                ql_log(ql_log_info, base_vha, 0x101c,
 510                                    "Mailbox cmd timeout occurred, cmd=0x%x, "
 511                                    "mb[0]=0x%x, eeh_busy=0x%x. Scheduling ISP "
 512                                    "abort.\n", command, mcp->mb[0],
 513                                    ha->flags.eeh_busy);
 514                                vha->hw_err_cnt++;
 515                                set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
 516                                qla2xxx_wake_dpc(vha);
 517                        }
 518                } else if (current == ha->dpc_thread) {
 519                        /* call abort directly since we are in the DPC thread */
 520                        ql_dbg(ql_dbg_mbx, vha, 0x101d,
 521                            "Timeout, calling abort_isp.\n");
 522
 523                        if (!test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) &&
 524                            !test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) &&
 525                            !test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
 526                                if (IS_QLA82XX(ha)) {
 527                                        ql_dbg(ql_dbg_mbx, vha, 0x112b,
 528                                            "disabling pause transmit on port "
 529                                            "0 & 1.\n");
 530                                        qla82xx_wr_32(ha,
 531                                            QLA82XX_CRB_NIU + 0x98,
 532                                            CRB_NIU_XG_PAUSE_CTL_P0|
 533                                            CRB_NIU_XG_PAUSE_CTL_P1);
 534                                }
 535                                ql_log(ql_log_info, base_vha, 0x101e,
 536                                    "Mailbox cmd timeout occurred, cmd=0x%x, "
 537                                    "mb[0]=0x%x. Scheduling ISP abort ",
 538                                    command, mcp->mb[0]);
 539                                vha->hw_err_cnt++;
 540                                set_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags);
 541                                clear_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
 542                                /* Allow next mbx cmd to come in. */
 543                                complete(&ha->mbx_cmd_comp);
 544                                if (ha->isp_ops->abort_isp(vha) &&
 545                                    !ha->flags.eeh_busy) {
 546                                        /* Failed. retry later. */
 547                                        set_bit(ISP_ABORT_NEEDED,
 548                                            &vha->dpc_flags);
 549                                }
 550                                clear_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags);
 551                                ql_dbg(ql_dbg_mbx, vha, 0x101f,
 552                                    "Finished abort_isp.\n");
 553                                goto mbx_done;
 554                        }
 555                }
 556        }
 557
 558premature_exit:
 559        /* Allow next mbx cmd to come in. */
 560        complete(&ha->mbx_cmd_comp);
 561
 562mbx_done:
 563        if (rval == QLA_ABORTED) {
 564                ql_log(ql_log_info, vha, 0xd035,
 565                    "Chip Reset in progress. Purging Mbox cmd=0x%x.\n",
 566                    mcp->mb[0]);
 567        } else if (rval) {
 568                if (ql2xextended_error_logging & (ql_dbg_disc|ql_dbg_mbx)) {
 569                        pr_warn("%s [%s]-%04x:%ld: **** Failed=%x", QL_MSGHDR,
 570                            dev_name(&ha->pdev->dev), 0x1020+0x800,
 571                            vha->host_no, rval);
 572                        mboxes = mcp->in_mb;
 573                        cnt = 4;
 574                        for (i = 0; i < ha->mbx_count && cnt; i++, mboxes >>= 1)
 575                                if (mboxes & BIT_0) {
 576                                        printk(" mb[%u]=%x", i, mcp->mb[i]);
 577                                        cnt--;
 578                                }
 579                        pr_warn(" cmd=%x ****\n", command);
 580                }
 581                if (IS_FWI2_CAPABLE(ha) && !(IS_P3P_TYPE(ha))) {
 582                        ql_dbg(ql_dbg_mbx, vha, 0x1198,
 583                            "host_status=%#x intr_ctrl=%#x intr_status=%#x\n",
 584                            rd_reg_dword(&reg->isp24.host_status),
 585                            rd_reg_dword(&reg->isp24.ictrl),
 586                            rd_reg_dword(&reg->isp24.istatus));
 587                } else {
 588                        ql_dbg(ql_dbg_mbx, vha, 0x1206,
 589                            "ctrl_status=%#x ictrl=%#x istatus=%#x\n",
 590                            rd_reg_word(&reg->isp.ctrl_status),
 591                            rd_reg_word(&reg->isp.ictrl),
 592                            rd_reg_word(&reg->isp.istatus));
 593                }
 594        } else {
 595                ql_dbg(ql_dbg_mbx, base_vha, 0x1021, "Done %s.\n", __func__);
 596        }
 597
 598        i = 500;
 599        while (i && eeh_delay && (ha->pci_error_state < QLA_PCI_SLOT_RESET)) {
 600                /*
 601                 * The caller of this mailbox encounter pci error.
 602                 * Hold the thread until PCIE link reset complete to make
 603                 * sure caller does not unmap dma while recovery is
 604                 * in progress.
 605                 */
 606                msleep(1);
 607                i--;
 608        }
 609        return rval;
 610}
 611
 612int
 613qla2x00_load_ram(scsi_qla_host_t *vha, dma_addr_t req_dma, uint32_t risc_addr,
 614    uint32_t risc_code_size)
 615{
 616        int rval;
 617        struct qla_hw_data *ha = vha->hw;
 618        mbx_cmd_t mc;
 619        mbx_cmd_t *mcp = &mc;
 620
 621        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1022,
 622            "Entered %s.\n", __func__);
 623
 624        if (MSW(risc_addr) || IS_FWI2_CAPABLE(ha)) {
 625                mcp->mb[0] = MBC_LOAD_RISC_RAM_EXTENDED;
 626                mcp->mb[8] = MSW(risc_addr);
 627                mcp->out_mb = MBX_8|MBX_0;
 628        } else {
 629                mcp->mb[0] = MBC_LOAD_RISC_RAM;
 630                mcp->out_mb = MBX_0;
 631        }
 632        mcp->mb[1] = LSW(risc_addr);
 633        mcp->mb[2] = MSW(req_dma);
 634        mcp->mb[3] = LSW(req_dma);
 635        mcp->mb[6] = MSW(MSD(req_dma));
 636        mcp->mb[7] = LSW(MSD(req_dma));
 637        mcp->out_mb |= MBX_7|MBX_6|MBX_3|MBX_2|MBX_1;
 638        if (IS_FWI2_CAPABLE(ha)) {
 639                mcp->mb[4] = MSW(risc_code_size);
 640                mcp->mb[5] = LSW(risc_code_size);
 641                mcp->out_mb |= MBX_5|MBX_4;
 642        } else {
 643                mcp->mb[4] = LSW(risc_code_size);
 644                mcp->out_mb |= MBX_4;
 645        }
 646
 647        mcp->in_mb = MBX_1|MBX_0;
 648        mcp->tov = MBX_TOV_SECONDS;
 649        mcp->flags = 0;
 650        rval = qla2x00_mailbox_command(vha, mcp);
 651
 652        if (rval != QLA_SUCCESS) {
 653                ql_dbg(ql_dbg_mbx, vha, 0x1023,
 654                    "Failed=%x mb[0]=%x mb[1]=%x.\n",
 655                    rval, mcp->mb[0], mcp->mb[1]);
 656                vha->hw_err_cnt++;
 657        } else {
 658                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1024,
 659                    "Done %s.\n", __func__);
 660        }
 661
 662        return rval;
 663}
 664
 665#define NVME_ENABLE_FLAG        BIT_3
 666
 667/*
 668 * qla2x00_execute_fw
 669 *     Start adapter firmware.
 670 *
 671 * Input:
 672 *     ha = adapter block pointer.
 673 *     TARGET_QUEUE_LOCK must be released.
 674 *     ADAPTER_STATE_LOCK must be released.
 675 *
 676 * Returns:
 677 *     qla2x00 local function return status code.
 678 *
 679 * Context:
 680 *     Kernel context.
 681 */
 682int
 683qla2x00_execute_fw(scsi_qla_host_t *vha, uint32_t risc_addr)
 684{
 685        int rval;
 686        struct qla_hw_data *ha = vha->hw;
 687        mbx_cmd_t mc;
 688        mbx_cmd_t *mcp = &mc;
 689        u8 semaphore = 0;
 690#define EXE_FW_FORCE_SEMAPHORE BIT_7
 691        u8 retry = 3;
 692
 693        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1025,
 694            "Entered %s.\n", __func__);
 695
 696again:
 697        mcp->mb[0] = MBC_EXECUTE_FIRMWARE;
 698        mcp->out_mb = MBX_0;
 699        mcp->in_mb = MBX_0;
 700        if (IS_FWI2_CAPABLE(ha)) {
 701                mcp->mb[1] = MSW(risc_addr);
 702                mcp->mb[2] = LSW(risc_addr);
 703                mcp->mb[3] = 0;
 704                mcp->mb[4] = 0;
 705                mcp->mb[11] = 0;
 706
 707                /* Enable BPM? */
 708                if (ha->flags.lr_detected) {
 709                        mcp->mb[4] = BIT_0;
 710                        if (IS_BPM_RANGE_CAPABLE(ha))
 711                                mcp->mb[4] |=
 712                                    ha->lr_distance << LR_DIST_FW_POS;
 713                }
 714
 715                if (ql2xnvmeenable && (IS_QLA27XX(ha) || IS_QLA28XX(ha)))
 716                        mcp->mb[4] |= NVME_ENABLE_FLAG;
 717
 718                if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
 719                        struct nvram_81xx *nv = ha->nvram;
 720                        /* set minimum speed if specified in nvram */
 721                        if (nv->min_supported_speed >= 2 &&
 722                            nv->min_supported_speed <= 5) {
 723                                mcp->mb[4] |= BIT_4;
 724                                mcp->mb[11] |= nv->min_supported_speed & 0xF;
 725                                mcp->out_mb |= MBX_11;
 726                                mcp->in_mb |= BIT_5;
 727                                vha->min_supported_speed =
 728                                    nv->min_supported_speed;
 729                        }
 730                }
 731
 732                if (ha->flags.exlogins_enabled)
 733                        mcp->mb[4] |= ENABLE_EXTENDED_LOGIN;
 734
 735                if (ha->flags.exchoffld_enabled)
 736                        mcp->mb[4] |= ENABLE_EXCHANGE_OFFLD;
 737
 738                if (semaphore)
 739                        mcp->mb[11] |= EXE_FW_FORCE_SEMAPHORE;
 740
 741                mcp->out_mb |= MBX_4 | MBX_3 | MBX_2 | MBX_1 | MBX_11;
 742                mcp->in_mb |= MBX_3 | MBX_2 | MBX_1;
 743        } else {
 744                mcp->mb[1] = LSW(risc_addr);
 745                mcp->out_mb |= MBX_1;
 746                if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
 747                        mcp->mb[2] = 0;
 748                        mcp->out_mb |= MBX_2;
 749                }
 750        }
 751
 752        mcp->tov = MBX_TOV_SECONDS;
 753        mcp->flags = 0;
 754        rval = qla2x00_mailbox_command(vha, mcp);
 755
 756        if (rval != QLA_SUCCESS) {
 757                if (IS_QLA28XX(ha) && rval == QLA_COMMAND_ERROR &&
 758                    mcp->mb[1] == 0x27 && retry) {
 759                        semaphore = 1;
 760                        retry--;
 761                        ql_dbg(ql_dbg_async, vha, 0x1026,
 762                            "Exe FW: force semaphore.\n");
 763                        goto again;
 764                }
 765
 766                ql_dbg(ql_dbg_mbx, vha, 0x1026,
 767                    "Failed=%x mb[0]=%x.\n", rval, mcp->mb[0]);
 768                vha->hw_err_cnt++;
 769                return rval;
 770        }
 771
 772        if (!IS_FWI2_CAPABLE(ha))
 773                goto done;
 774
 775        ha->fw_ability_mask = mcp->mb[3] << 16 | mcp->mb[2];
 776        ql_dbg(ql_dbg_mbx, vha, 0x119a,
 777            "fw_ability_mask=%x.\n", ha->fw_ability_mask);
 778        ql_dbg(ql_dbg_mbx, vha, 0x1027, "exchanges=%x.\n", mcp->mb[1]);
 779        if (IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
 780                ha->max_supported_speed = mcp->mb[2] & (BIT_0|BIT_1);
 781                ql_dbg(ql_dbg_mbx, vha, 0x119b, "max_supported_speed=%s.\n",
 782                    ha->max_supported_speed == 0 ? "16Gps" :
 783                    ha->max_supported_speed == 1 ? "32Gps" :
 784                    ha->max_supported_speed == 2 ? "64Gps" : "unknown");
 785                if (vha->min_supported_speed) {
 786                        ha->min_supported_speed = mcp->mb[5] &
 787                            (BIT_0 | BIT_1 | BIT_2);
 788                        ql_dbg(ql_dbg_mbx, vha, 0x119c,
 789                            "min_supported_speed=%s.\n",
 790                            ha->min_supported_speed == 6 ? "64Gps" :
 791                            ha->min_supported_speed == 5 ? "32Gps" :
 792                            ha->min_supported_speed == 4 ? "16Gps" :
 793                            ha->min_supported_speed == 3 ? "8Gps" :
 794                            ha->min_supported_speed == 2 ? "4Gps" : "unknown");
 795                }
 796        }
 797
 798done:
 799        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1028,
 800            "Done %s.\n", __func__);
 801
 802        return rval;
 803}
 804
 805/*
 806 * qla_get_exlogin_status
 807 *      Get extended login status
 808 *      uses the memory offload control/status Mailbox
 809 *
 810 * Input:
 811 *      ha:             adapter state pointer.
 812 *      fwopt:          firmware options
 813 *
 814 * Returns:
 815 *      qla2x00 local function status
 816 *
 817 * Context:
 818 *      Kernel context.
 819 */
 820#define FETCH_XLOGINS_STAT      0x8
 821int
 822qla_get_exlogin_status(scsi_qla_host_t *vha, uint16_t *buf_sz,
 823        uint16_t *ex_logins_cnt)
 824{
 825        int rval;
 826        mbx_cmd_t       mc;
 827        mbx_cmd_t       *mcp = &mc;
 828
 829        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x118f,
 830            "Entered %s\n", __func__);
 831
 832        memset(mcp->mb, 0 , sizeof(mcp->mb));
 833        mcp->mb[0] = MBC_GET_MEM_OFFLOAD_CNTRL_STAT;
 834        mcp->mb[1] = FETCH_XLOGINS_STAT;
 835        mcp->out_mb = MBX_1|MBX_0;
 836        mcp->in_mb = MBX_10|MBX_4|MBX_0;
 837        mcp->tov = MBX_TOV_SECONDS;
 838        mcp->flags = 0;
 839
 840        rval = qla2x00_mailbox_command(vha, mcp);
 841        if (rval != QLA_SUCCESS) {
 842                ql_dbg(ql_dbg_mbx, vha, 0x1115, "Failed=%x.\n", rval);
 843        } else {
 844                *buf_sz = mcp->mb[4];
 845                *ex_logins_cnt = mcp->mb[10];
 846
 847                ql_log(ql_log_info, vha, 0x1190,
 848                    "buffer size 0x%x, exchange login count=%d\n",
 849                    mcp->mb[4], mcp->mb[10]);
 850
 851                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1116,
 852                    "Done %s.\n", __func__);
 853        }
 854
 855        return rval;
 856}
 857
 858/*
 859 * qla_set_exlogin_mem_cfg
 860 *      set extended login memory configuration
 861 *      Mbx needs to be issues before init_cb is set
 862 *
 863 * Input:
 864 *      ha:             adapter state pointer.
 865 *      buffer:         buffer pointer
 866 *      phys_addr:      physical address of buffer
 867 *      size:           size of buffer
 868 *      TARGET_QUEUE_LOCK must be released
 869 *      ADAPTER_STATE_LOCK must be release
 870 *
 871 * Returns:
 872 *      qla2x00 local funxtion status code.
 873 *
 874 * Context:
 875 *      Kernel context.
 876 */
 877#define CONFIG_XLOGINS_MEM      0x9
 878int
 879qla_set_exlogin_mem_cfg(scsi_qla_host_t *vha, dma_addr_t phys_addr)
 880{
 881        int             rval;
 882        mbx_cmd_t       mc;
 883        mbx_cmd_t       *mcp = &mc;
 884        struct qla_hw_data *ha = vha->hw;
 885
 886        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x111a,
 887            "Entered %s.\n", __func__);
 888
 889        memset(mcp->mb, 0 , sizeof(mcp->mb));
 890        mcp->mb[0] = MBC_GET_MEM_OFFLOAD_CNTRL_STAT;
 891        mcp->mb[1] = CONFIG_XLOGINS_MEM;
 892        mcp->mb[2] = MSW(phys_addr);
 893        mcp->mb[3] = LSW(phys_addr);
 894        mcp->mb[6] = MSW(MSD(phys_addr));
 895        mcp->mb[7] = LSW(MSD(phys_addr));
 896        mcp->mb[8] = MSW(ha->exlogin_size);
 897        mcp->mb[9] = LSW(ha->exlogin_size);
 898        mcp->out_mb = MBX_9|MBX_8|MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
 899        mcp->in_mb = MBX_11|MBX_0;
 900        mcp->tov = MBX_TOV_SECONDS;
 901        mcp->flags = 0;
 902        rval = qla2x00_mailbox_command(vha, mcp);
 903        if (rval != QLA_SUCCESS) {
 904                ql_dbg(ql_dbg_mbx, vha, 0x111b,
 905                       "EXlogin Failed=%x. MB0=%x MB11=%x\n",
 906                       rval, mcp->mb[0], mcp->mb[11]);
 907        } else {
 908                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x118c,
 909                    "Done %s.\n", __func__);
 910        }
 911
 912        return rval;
 913}
 914
 915/*
 916 * qla_get_exchoffld_status
 917 *      Get exchange offload status
 918 *      uses the memory offload control/status Mailbox
 919 *
 920 * Input:
 921 *      ha:             adapter state pointer.
 922 *      fwopt:          firmware options
 923 *
 924 * Returns:
 925 *      qla2x00 local function status
 926 *
 927 * Context:
 928 *      Kernel context.
 929 */
 930#define FETCH_XCHOFFLD_STAT     0x2
 931int
 932qla_get_exchoffld_status(scsi_qla_host_t *vha, uint16_t *buf_sz,
 933        uint16_t *ex_logins_cnt)
 934{
 935        int rval;
 936        mbx_cmd_t       mc;
 937        mbx_cmd_t       *mcp = &mc;
 938
 939        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1019,
 940            "Entered %s\n", __func__);
 941
 942        memset(mcp->mb, 0 , sizeof(mcp->mb));
 943        mcp->mb[0] = MBC_GET_MEM_OFFLOAD_CNTRL_STAT;
 944        mcp->mb[1] = FETCH_XCHOFFLD_STAT;
 945        mcp->out_mb = MBX_1|MBX_0;
 946        mcp->in_mb = MBX_10|MBX_4|MBX_0;
 947        mcp->tov = MBX_TOV_SECONDS;
 948        mcp->flags = 0;
 949
 950        rval = qla2x00_mailbox_command(vha, mcp);
 951        if (rval != QLA_SUCCESS) {
 952                ql_dbg(ql_dbg_mbx, vha, 0x1155, "Failed=%x.\n", rval);
 953        } else {
 954                *buf_sz = mcp->mb[4];
 955                *ex_logins_cnt = mcp->mb[10];
 956
 957                ql_log(ql_log_info, vha, 0x118e,
 958                    "buffer size 0x%x, exchange offload count=%d\n",
 959                    mcp->mb[4], mcp->mb[10]);
 960
 961                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1156,
 962                    "Done %s.\n", __func__);
 963        }
 964
 965        return rval;
 966}
 967
 968/*
 969 * qla_set_exchoffld_mem_cfg
 970 *      Set exchange offload memory configuration
 971 *      Mbx needs to be issues before init_cb is set
 972 *
 973 * Input:
 974 *      ha:             adapter state pointer.
 975 *      buffer:         buffer pointer
 976 *      phys_addr:      physical address of buffer
 977 *      size:           size of buffer
 978 *      TARGET_QUEUE_LOCK must be released
 979 *      ADAPTER_STATE_LOCK must be release
 980 *
 981 * Returns:
 982 *      qla2x00 local funxtion status code.
 983 *
 984 * Context:
 985 *      Kernel context.
 986 */
 987#define CONFIG_XCHOFFLD_MEM     0x3
 988int
 989qla_set_exchoffld_mem_cfg(scsi_qla_host_t *vha)
 990{
 991        int             rval;
 992        mbx_cmd_t       mc;
 993        mbx_cmd_t       *mcp = &mc;
 994        struct qla_hw_data *ha = vha->hw;
 995
 996        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1157,
 997            "Entered %s.\n", __func__);
 998
 999        memset(mcp->mb, 0 , sizeof(mcp->mb));
1000        mcp->mb[0] = MBC_GET_MEM_OFFLOAD_CNTRL_STAT;
1001        mcp->mb[1] = CONFIG_XCHOFFLD_MEM;
1002        mcp->mb[2] = MSW(ha->exchoffld_buf_dma);
1003        mcp->mb[3] = LSW(ha->exchoffld_buf_dma);
1004        mcp->mb[6] = MSW(MSD(ha->exchoffld_buf_dma));
1005        mcp->mb[7] = LSW(MSD(ha->exchoffld_buf_dma));
1006        mcp->mb[8] = MSW(ha->exchoffld_size);
1007        mcp->mb[9] = LSW(ha->exchoffld_size);
1008        mcp->out_mb = MBX_9|MBX_8|MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
1009        mcp->in_mb = MBX_11|MBX_0;
1010        mcp->tov = MBX_TOV_SECONDS;
1011        mcp->flags = 0;
1012        rval = qla2x00_mailbox_command(vha, mcp);
1013        if (rval != QLA_SUCCESS) {
1014                /*EMPTY*/
1015                ql_dbg(ql_dbg_mbx, vha, 0x1158, "Failed=%x.\n", rval);
1016        } else {
1017                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1192,
1018                    "Done %s.\n", __func__);
1019        }
1020
1021        return rval;
1022}
1023
1024/*
1025 * qla2x00_get_fw_version
1026 *      Get firmware version.
1027 *
1028 * Input:
1029 *      ha:             adapter state pointer.
1030 *      major:          pointer for major number.
1031 *      minor:          pointer for minor number.
1032 *      subminor:       pointer for subminor number.
1033 *
1034 * Returns:
1035 *      qla2x00 local function return status code.
1036 *
1037 * Context:
1038 *      Kernel context.
1039 */
1040int
1041qla2x00_get_fw_version(scsi_qla_host_t *vha)
1042{
1043        int             rval;
1044        mbx_cmd_t       mc;
1045        mbx_cmd_t       *mcp = &mc;
1046        struct qla_hw_data *ha = vha->hw;
1047
1048        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1029,
1049            "Entered %s.\n", __func__);
1050
1051        mcp->mb[0] = MBC_GET_FIRMWARE_VERSION;
1052        mcp->out_mb = MBX_0;
1053        mcp->in_mb = MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
1054        if (IS_QLA81XX(vha->hw) || IS_QLA8031(ha) || IS_QLA8044(ha))
1055                mcp->in_mb |= MBX_13|MBX_12|MBX_11|MBX_10|MBX_9|MBX_8;
1056        if (IS_FWI2_CAPABLE(ha))
1057                mcp->in_mb |= MBX_17|MBX_16|MBX_15;
1058        if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
1059                mcp->in_mb |=
1060                    MBX_25|MBX_24|MBX_23|MBX_22|MBX_21|MBX_20|MBX_19|MBX_18|
1061                    MBX_14|MBX_13|MBX_11|MBX_10|MBX_9|MBX_8|MBX_7;
1062
1063        mcp->flags = 0;
1064        mcp->tov = MBX_TOV_SECONDS;
1065        rval = qla2x00_mailbox_command(vha, mcp);
1066        if (rval != QLA_SUCCESS)
1067                goto failed;
1068
1069        /* Return mailbox data. */
1070        ha->fw_major_version = mcp->mb[1];
1071        ha->fw_minor_version = mcp->mb[2];
1072        ha->fw_subminor_version = mcp->mb[3];
1073        ha->fw_attributes = mcp->mb[6];
1074        if (IS_QLA2100(vha->hw) || IS_QLA2200(vha->hw))
1075                ha->fw_memory_size = 0x1FFFF;           /* Defaults to 128KB. */
1076        else
1077                ha->fw_memory_size = (mcp->mb[5] << 16) | mcp->mb[4];
1078
1079        if (IS_QLA81XX(vha->hw) || IS_QLA8031(vha->hw) || IS_QLA8044(ha)) {
1080                ha->mpi_version[0] = mcp->mb[10] & 0xff;
1081                ha->mpi_version[1] = mcp->mb[11] >> 8;
1082                ha->mpi_version[2] = mcp->mb[11] & 0xff;
1083                ha->mpi_capabilities = (mcp->mb[12] << 16) | mcp->mb[13];
1084                ha->phy_version[0] = mcp->mb[8] & 0xff;
1085                ha->phy_version[1] = mcp->mb[9] >> 8;
1086                ha->phy_version[2] = mcp->mb[9] & 0xff;
1087        }
1088
1089        if (IS_FWI2_CAPABLE(ha)) {
1090                ha->fw_attributes_h = mcp->mb[15];
1091                ha->fw_attributes_ext[0] = mcp->mb[16];
1092                ha->fw_attributes_ext[1] = mcp->mb[17];
1093                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1139,
1094                    "%s: FW_attributes Upper: 0x%x, Lower: 0x%x.\n",
1095                    __func__, mcp->mb[15], mcp->mb[6]);
1096                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x112f,
1097                    "%s: Ext_FwAttributes Upper: 0x%x, Lower: 0x%x.\n",
1098                    __func__, mcp->mb[17], mcp->mb[16]);
1099
1100                if (ha->fw_attributes_h & 0x4)
1101                        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x118d,
1102                            "%s: Firmware supports Extended Login 0x%x\n",
1103                            __func__, ha->fw_attributes_h);
1104
1105                if (ha->fw_attributes_h & 0x8)
1106                        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1191,
1107                            "%s: Firmware supports Exchange Offload 0x%x\n",
1108                            __func__, ha->fw_attributes_h);
1109
1110                /*
1111                 * FW supports nvme and driver load parameter requested nvme.
1112                 * BIT 26 of fw_attributes indicates NVMe support.
1113                 */
1114                if ((ha->fw_attributes_h &
1115                    (FW_ATTR_H_NVME | FW_ATTR_H_NVME_UPDATED)) &&
1116                        ql2xnvmeenable) {
1117                        if (ha->fw_attributes_h & FW_ATTR_H_NVME_FBURST)
1118                                vha->flags.nvme_first_burst = 1;
1119
1120                        vha->flags.nvme_enabled = 1;
1121                        ql_log(ql_log_info, vha, 0xd302,
1122                            "%s: FC-NVMe is Enabled (0x%x)\n",
1123                             __func__, ha->fw_attributes_h);
1124                }
1125
1126                /* BIT_13 of Extended FW Attributes informs about NVMe2 support */
1127                if (ha->fw_attributes_ext[0] & FW_ATTR_EXT0_NVME2) {
1128                        ql_log(ql_log_info, vha, 0xd302,
1129                               "Firmware supports NVMe2 0x%x\n",
1130                               ha->fw_attributes_ext[0]);
1131                        vha->flags.nvme2_enabled = 1;
1132                }
1133        }
1134
1135        if (IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
1136                ha->serdes_version[0] = mcp->mb[7] & 0xff;
1137                ha->serdes_version[1] = mcp->mb[8] >> 8;
1138                ha->serdes_version[2] = mcp->mb[8] & 0xff;
1139                ha->mpi_version[0] = mcp->mb[10] & 0xff;
1140                ha->mpi_version[1] = mcp->mb[11] >> 8;
1141                ha->mpi_version[2] = mcp->mb[11] & 0xff;
1142                ha->pep_version[0] = mcp->mb[13] & 0xff;
1143                ha->pep_version[1] = mcp->mb[14] >> 8;
1144                ha->pep_version[2] = mcp->mb[14] & 0xff;
1145                ha->fw_shared_ram_start = (mcp->mb[19] << 16) | mcp->mb[18];
1146                ha->fw_shared_ram_end = (mcp->mb[21] << 16) | mcp->mb[20];
1147                ha->fw_ddr_ram_start = (mcp->mb[23] << 16) | mcp->mb[22];
1148                ha->fw_ddr_ram_end = (mcp->mb[25] << 16) | mcp->mb[24];
1149                if (IS_QLA28XX(ha)) {
1150                        if (mcp->mb[16] & BIT_10)
1151                                ha->flags.secure_fw = 1;
1152
1153                        ql_log(ql_log_info, vha, 0xffff,
1154                            "Secure Flash Update in FW: %s\n",
1155                            (ha->flags.secure_fw) ? "Supported" :
1156                            "Not Supported");
1157                }
1158
1159                if (ha->flags.scm_supported_a &&
1160                    (ha->fw_attributes_ext[0] & FW_ATTR_EXT0_SCM_SUPPORTED)) {
1161                        ha->flags.scm_supported_f = 1;
1162                        ha->sf_init_cb->flags |= cpu_to_le16(BIT_13);
1163                }
1164                ql_log(ql_log_info, vha, 0x11a3, "SCM in FW: %s\n",
1165                       (ha->flags.scm_supported_f) ? "Supported" :
1166                       "Not Supported");
1167
1168                if (vha->flags.nvme2_enabled) {
1169                        /* set BIT_15 of special feature control block for SLER */
1170                        ha->sf_init_cb->flags |= cpu_to_le16(BIT_15);
1171                        /* set BIT_14 of special feature control block for PI CTRL*/
1172                        ha->sf_init_cb->flags |= cpu_to_le16(BIT_14);
1173                }
1174        }
1175
1176failed:
1177        if (rval != QLA_SUCCESS) {
1178                /*EMPTY*/
1179                ql_dbg(ql_dbg_mbx, vha, 0x102a, "Failed=%x.\n", rval);
1180        } else {
1181                /*EMPTY*/
1182                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x102b,
1183                    "Done %s.\n", __func__);
1184        }
1185        return rval;
1186}
1187
1188/*
1189 * qla2x00_get_fw_options
1190 *      Set firmware options.
1191 *
1192 * Input:
1193 *      ha = adapter block pointer.
1194 *      fwopt = pointer for firmware options.
1195 *
1196 * Returns:
1197 *      qla2x00 local function return status code.
1198 *
1199 * Context:
1200 *      Kernel context.
1201 */
1202int
1203qla2x00_get_fw_options(scsi_qla_host_t *vha, uint16_t *fwopts)
1204{
1205        int rval;
1206        mbx_cmd_t mc;
1207        mbx_cmd_t *mcp = &mc;
1208
1209        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x102c,
1210            "Entered %s.\n", __func__);
1211
1212        mcp->mb[0] = MBC_GET_FIRMWARE_OPTION;
1213        mcp->out_mb = MBX_0;
1214        mcp->in_mb = MBX_3|MBX_2|MBX_1|MBX_0;
1215        mcp->tov = MBX_TOV_SECONDS;
1216        mcp->flags = 0;
1217        rval = qla2x00_mailbox_command(vha, mcp);
1218
1219        if (rval != QLA_SUCCESS) {
1220                /*EMPTY*/
1221                ql_dbg(ql_dbg_mbx, vha, 0x102d, "Failed=%x.\n", rval);
1222        } else {
1223                fwopts[0] = mcp->mb[0];
1224                fwopts[1] = mcp->mb[1];
1225                fwopts[2] = mcp->mb[2];
1226                fwopts[3] = mcp->mb[3];
1227
1228                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x102e,
1229                    "Done %s.\n", __func__);
1230        }
1231
1232        return rval;
1233}
1234
1235
1236/*
1237 * qla2x00_set_fw_options
1238 *      Set firmware options.
1239 *
1240 * Input:
1241 *      ha = adapter block pointer.
1242 *      fwopt = pointer for firmware options.
1243 *
1244 * Returns:
1245 *      qla2x00 local function return status code.
1246 *
1247 * Context:
1248 *      Kernel context.
1249 */
1250int
1251qla2x00_set_fw_options(scsi_qla_host_t *vha, uint16_t *fwopts)
1252{
1253        int rval;
1254        mbx_cmd_t mc;
1255        mbx_cmd_t *mcp = &mc;
1256
1257        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x102f,
1258            "Entered %s.\n", __func__);
1259
1260        mcp->mb[0] = MBC_SET_FIRMWARE_OPTION;
1261        mcp->mb[1] = fwopts[1];
1262        mcp->mb[2] = fwopts[2];
1263        mcp->mb[3] = fwopts[3];
1264        mcp->out_mb = MBX_3|MBX_2|MBX_1|MBX_0;
1265        mcp->in_mb = MBX_0;
1266        if (IS_FWI2_CAPABLE(vha->hw)) {
1267                mcp->in_mb |= MBX_1;
1268                mcp->mb[10] = fwopts[10];
1269                mcp->out_mb |= MBX_10;
1270        } else {
1271                mcp->mb[10] = fwopts[10];
1272                mcp->mb[11] = fwopts[11];
1273                mcp->mb[12] = 0;        /* Undocumented, but used */
1274                mcp->out_mb |= MBX_12|MBX_11|MBX_10;
1275        }
1276        mcp->tov = MBX_TOV_SECONDS;
1277        mcp->flags = 0;
1278        rval = qla2x00_mailbox_command(vha, mcp);
1279
1280        fwopts[0] = mcp->mb[0];
1281
1282        if (rval != QLA_SUCCESS) {
1283                /*EMPTY*/
1284                ql_dbg(ql_dbg_mbx, vha, 0x1030,
1285                    "Failed=%x (%x/%x).\n", rval, mcp->mb[0], mcp->mb[1]);
1286        } else {
1287                /*EMPTY*/
1288                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1031,
1289                    "Done %s.\n", __func__);
1290        }
1291
1292        return rval;
1293}
1294
1295/*
1296 * qla2x00_mbx_reg_test
1297 *      Mailbox register wrap test.
1298 *
1299 * Input:
1300 *      ha = adapter block pointer.
1301 *      TARGET_QUEUE_LOCK must be released.
1302 *      ADAPTER_STATE_LOCK must be released.
1303 *
1304 * Returns:
1305 *      qla2x00 local function return status code.
1306 *
1307 * Context:
1308 *      Kernel context.
1309 */
1310int
1311qla2x00_mbx_reg_test(scsi_qla_host_t *vha)
1312{
1313        int rval;
1314        mbx_cmd_t mc;
1315        mbx_cmd_t *mcp = &mc;
1316
1317        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1032,
1318            "Entered %s.\n", __func__);
1319
1320        mcp->mb[0] = MBC_MAILBOX_REGISTER_TEST;
1321        mcp->mb[1] = 0xAAAA;
1322        mcp->mb[2] = 0x5555;
1323        mcp->mb[3] = 0xAA55;
1324        mcp->mb[4] = 0x55AA;
1325        mcp->mb[5] = 0xA5A5;
1326        mcp->mb[6] = 0x5A5A;
1327        mcp->mb[7] = 0x2525;
1328        mcp->out_mb = MBX_7|MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
1329        mcp->in_mb = MBX_7|MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
1330        mcp->tov = MBX_TOV_SECONDS;
1331        mcp->flags = 0;
1332        rval = qla2x00_mailbox_command(vha, mcp);
1333
1334        if (rval == QLA_SUCCESS) {
1335                if (mcp->mb[1] != 0xAAAA || mcp->mb[2] != 0x5555 ||
1336                    mcp->mb[3] != 0xAA55 || mcp->mb[4] != 0x55AA)
1337                        rval = QLA_FUNCTION_FAILED;
1338                if (mcp->mb[5] != 0xA5A5 || mcp->mb[6] != 0x5A5A ||
1339                    mcp->mb[7] != 0x2525)
1340                        rval = QLA_FUNCTION_FAILED;
1341        }
1342
1343        if (rval != QLA_SUCCESS) {
1344                /*EMPTY*/
1345                ql_dbg(ql_dbg_mbx, vha, 0x1033, "Failed=%x.\n", rval);
1346                vha->hw_err_cnt++;
1347        } else {
1348                /*EMPTY*/
1349                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1034,
1350                    "Done %s.\n", __func__);
1351        }
1352
1353        return rval;
1354}
1355
1356/*
1357 * qla2x00_verify_checksum
1358 *      Verify firmware checksum.
1359 *
1360 * Input:
1361 *      ha = adapter block pointer.
1362 *      TARGET_QUEUE_LOCK must be released.
1363 *      ADAPTER_STATE_LOCK must be released.
1364 *
1365 * Returns:
1366 *      qla2x00 local function return status code.
1367 *
1368 * Context:
1369 *      Kernel context.
1370 */
1371int
1372qla2x00_verify_checksum(scsi_qla_host_t *vha, uint32_t risc_addr)
1373{
1374        int rval;
1375        mbx_cmd_t mc;
1376        mbx_cmd_t *mcp = &mc;
1377
1378        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1035,
1379            "Entered %s.\n", __func__);
1380
1381        mcp->mb[0] = MBC_VERIFY_CHECKSUM;
1382        mcp->out_mb = MBX_0;
1383        mcp->in_mb = MBX_0;
1384        if (IS_FWI2_CAPABLE(vha->hw)) {
1385                mcp->mb[1] = MSW(risc_addr);
1386                mcp->mb[2] = LSW(risc_addr);
1387                mcp->out_mb |= MBX_2|MBX_1;
1388                mcp->in_mb |= MBX_2|MBX_1;
1389        } else {
1390                mcp->mb[1] = LSW(risc_addr);
1391                mcp->out_mb |= MBX_1;
1392                mcp->in_mb |= MBX_1;
1393        }
1394
1395        mcp->tov = MBX_TOV_SECONDS;
1396        mcp->flags = 0;
1397        rval = qla2x00_mailbox_command(vha, mcp);
1398
1399        if (rval != QLA_SUCCESS) {
1400                ql_dbg(ql_dbg_mbx, vha, 0x1036,
1401                    "Failed=%x chm sum=%x.\n", rval, IS_FWI2_CAPABLE(vha->hw) ?
1402                    (mcp->mb[2] << 16) | mcp->mb[1] : mcp->mb[1]);
1403        } else {
1404                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1037,
1405                    "Done %s.\n", __func__);
1406        }
1407
1408        return rval;
1409}
1410
1411/*
1412 * qla2x00_issue_iocb
1413 *      Issue IOCB using mailbox command
1414 *
1415 * Input:
1416 *      ha = adapter state pointer.
1417 *      buffer = buffer pointer.
1418 *      phys_addr = physical address of buffer.
1419 *      size = size of buffer.
1420 *      TARGET_QUEUE_LOCK must be released.
1421 *      ADAPTER_STATE_LOCK must be released.
1422 *
1423 * Returns:
1424 *      qla2x00 local function return status code.
1425 *
1426 * Context:
1427 *      Kernel context.
1428 */
1429int
1430qla2x00_issue_iocb_timeout(scsi_qla_host_t *vha, void *buffer,
1431    dma_addr_t phys_addr, size_t size, uint32_t tov)
1432{
1433        int             rval;
1434        mbx_cmd_t       mc;
1435        mbx_cmd_t       *mcp = &mc;
1436
1437        if (!vha->hw->flags.fw_started)
1438                return QLA_INVALID_COMMAND;
1439
1440        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1038,
1441            "Entered %s.\n", __func__);
1442
1443        mcp->mb[0] = MBC_IOCB_COMMAND_A64;
1444        mcp->mb[1] = 0;
1445        mcp->mb[2] = MSW(LSD(phys_addr));
1446        mcp->mb[3] = LSW(LSD(phys_addr));
1447        mcp->mb[6] = MSW(MSD(phys_addr));
1448        mcp->mb[7] = LSW(MSD(phys_addr));
1449        mcp->out_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
1450        mcp->in_mb = MBX_1|MBX_0;
1451        mcp->tov = tov;
1452        mcp->flags = 0;
1453        rval = qla2x00_mailbox_command(vha, mcp);
1454
1455        if (rval != QLA_SUCCESS) {
1456                /*EMPTY*/
1457                ql_dbg(ql_dbg_mbx, vha, 0x1039, "Failed=%x.\n", rval);
1458        } else {
1459                sts_entry_t *sts_entry = buffer;
1460
1461                /* Mask reserved bits. */
1462                sts_entry->entry_status &=
1463                    IS_FWI2_CAPABLE(vha->hw) ? RF_MASK_24XX : RF_MASK;
1464                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x103a,
1465                    "Done %s (status=%x).\n", __func__,
1466                    sts_entry->entry_status);
1467        }
1468
1469        return rval;
1470}
1471
1472int
1473qla2x00_issue_iocb(scsi_qla_host_t *vha, void *buffer, dma_addr_t phys_addr,
1474    size_t size)
1475{
1476        return qla2x00_issue_iocb_timeout(vha, buffer, phys_addr, size,
1477            MBX_TOV_SECONDS);
1478}
1479
1480/*
1481 * qla2x00_abort_command
1482 *      Abort command aborts a specified IOCB.
1483 *
1484 * Input:
1485 *      ha = adapter block pointer.
1486 *      sp = SB structure pointer.
1487 *
1488 * Returns:
1489 *      qla2x00 local function return status code.
1490 *
1491 * Context:
1492 *      Kernel context.
1493 */
1494int
1495qla2x00_abort_command(srb_t *sp)
1496{
1497        unsigned long   flags = 0;
1498        int             rval;
1499        uint32_t        handle = 0;
1500        mbx_cmd_t       mc;
1501        mbx_cmd_t       *mcp = &mc;
1502        fc_port_t       *fcport = sp->fcport;
1503        scsi_qla_host_t *vha = fcport->vha;
1504        struct qla_hw_data *ha = vha->hw;
1505        struct req_que *req;
1506        struct scsi_cmnd *cmd = GET_CMD_SP(sp);
1507
1508        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x103b,
1509            "Entered %s.\n", __func__);
1510
1511        if (sp->qpair)
1512                req = sp->qpair->req;
1513        else
1514                req = vha->req;
1515
1516        spin_lock_irqsave(&ha->hardware_lock, flags);
1517        for (handle = 1; handle < req->num_outstanding_cmds; handle++) {
1518                if (req->outstanding_cmds[handle] == sp)
1519                        break;
1520        }
1521        spin_unlock_irqrestore(&ha->hardware_lock, flags);
1522
1523        if (handle == req->num_outstanding_cmds) {
1524                /* command not found */
1525                return QLA_FUNCTION_FAILED;
1526        }
1527
1528        mcp->mb[0] = MBC_ABORT_COMMAND;
1529        if (HAS_EXTENDED_IDS(ha))
1530                mcp->mb[1] = fcport->loop_id;
1531        else
1532                mcp->mb[1] = fcport->loop_id << 8;
1533        mcp->mb[2] = (uint16_t)handle;
1534        mcp->mb[3] = (uint16_t)(handle >> 16);
1535        mcp->mb[6] = (uint16_t)cmd->device->lun;
1536        mcp->out_mb = MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
1537        mcp->in_mb = MBX_0;
1538        mcp->tov = MBX_TOV_SECONDS;
1539        mcp->flags = 0;
1540        rval = qla2x00_mailbox_command(vha, mcp);
1541
1542        if (rval != QLA_SUCCESS) {
1543                ql_dbg(ql_dbg_mbx, vha, 0x103c, "Failed=%x.\n", rval);
1544        } else {
1545                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x103d,
1546                    "Done %s.\n", __func__);
1547        }
1548
1549        return rval;
1550}
1551
1552int
1553qla2x00_abort_target(struct fc_port *fcport, uint64_t l, int tag)
1554{
1555        int rval, rval2;
1556        mbx_cmd_t  mc;
1557        mbx_cmd_t  *mcp = &mc;
1558        scsi_qla_host_t *vha;
1559
1560        vha = fcport->vha;
1561
1562        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x103e,
1563            "Entered %s.\n", __func__);
1564
1565        mcp->mb[0] = MBC_ABORT_TARGET;
1566        mcp->out_mb = MBX_9|MBX_2|MBX_1|MBX_0;
1567        if (HAS_EXTENDED_IDS(vha->hw)) {
1568                mcp->mb[1] = fcport->loop_id;
1569                mcp->mb[10] = 0;
1570                mcp->out_mb |= MBX_10;
1571        } else {
1572                mcp->mb[1] = fcport->loop_id << 8;
1573        }
1574        mcp->mb[2] = vha->hw->loop_reset_delay;
1575        mcp->mb[9] = vha->vp_idx;
1576
1577        mcp->in_mb = MBX_0;
1578        mcp->tov = MBX_TOV_SECONDS;
1579        mcp->flags = 0;
1580        rval = qla2x00_mailbox_command(vha, mcp);
1581        if (rval != QLA_SUCCESS) {
1582                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x103f,
1583                    "Failed=%x.\n", rval);
1584        }
1585
1586        /* Issue marker IOCB. */
1587        rval2 = qla2x00_marker(vha, vha->hw->base_qpair, fcport->loop_id, 0,
1588                                                        MK_SYNC_ID);
1589        if (rval2 != QLA_SUCCESS) {
1590                ql_dbg(ql_dbg_mbx, vha, 0x1040,
1591                    "Failed to issue marker IOCB (%x).\n", rval2);
1592        } else {
1593                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1041,
1594                    "Done %s.\n", __func__);
1595        }
1596
1597        return rval;
1598}
1599
1600int
1601qla2x00_lun_reset(struct fc_port *fcport, uint64_t l, int tag)
1602{
1603        int rval, rval2;
1604        mbx_cmd_t  mc;
1605        mbx_cmd_t  *mcp = &mc;
1606        scsi_qla_host_t *vha;
1607
1608        vha = fcport->vha;
1609
1610        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1042,
1611            "Entered %s.\n", __func__);
1612
1613        mcp->mb[0] = MBC_LUN_RESET;
1614        mcp->out_mb = MBX_9|MBX_3|MBX_2|MBX_1|MBX_0;
1615        if (HAS_EXTENDED_IDS(vha->hw))
1616                mcp->mb[1] = fcport->loop_id;
1617        else
1618                mcp->mb[1] = fcport->loop_id << 8;
1619        mcp->mb[2] = (u32)l;
1620        mcp->mb[3] = 0;
1621        mcp->mb[9] = vha->vp_idx;
1622
1623        mcp->in_mb = MBX_0;
1624        mcp->tov = MBX_TOV_SECONDS;
1625        mcp->flags = 0;
1626        rval = qla2x00_mailbox_command(vha, mcp);
1627        if (rval != QLA_SUCCESS) {
1628                ql_dbg(ql_dbg_mbx, vha, 0x1043, "Failed=%x.\n", rval);
1629        }
1630
1631        /* Issue marker IOCB. */
1632        rval2 = qla2x00_marker(vha, vha->hw->base_qpair, fcport->loop_id, l,
1633                                                                MK_SYNC_ID_LUN);
1634        if (rval2 != QLA_SUCCESS) {
1635                ql_dbg(ql_dbg_mbx, vha, 0x1044,
1636                    "Failed to issue marker IOCB (%x).\n", rval2);
1637        } else {
1638                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1045,
1639                    "Done %s.\n", __func__);
1640        }
1641
1642        return rval;
1643}
1644
1645/*
1646 * qla2x00_get_adapter_id
1647 *      Get adapter ID and topology.
1648 *
1649 * Input:
1650 *      ha = adapter block pointer.
1651 *      id = pointer for loop ID.
1652 *      al_pa = pointer for AL_PA.
1653 *      area = pointer for area.
1654 *      domain = pointer for domain.
1655 *      top = pointer for topology.
1656 *      TARGET_QUEUE_LOCK must be released.
1657 *      ADAPTER_STATE_LOCK must be released.
1658 *
1659 * Returns:
1660 *      qla2x00 local function return status code.
1661 *
1662 * Context:
1663 *      Kernel context.
1664 */
1665int
1666qla2x00_get_adapter_id(scsi_qla_host_t *vha, uint16_t *id, uint8_t *al_pa,
1667    uint8_t *area, uint8_t *domain, uint16_t *top, uint16_t *sw_cap)
1668{
1669        int rval;
1670        mbx_cmd_t mc;
1671        mbx_cmd_t *mcp = &mc;
1672
1673        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1046,
1674            "Entered %s.\n", __func__);
1675
1676        mcp->mb[0] = MBC_GET_ADAPTER_LOOP_ID;
1677        mcp->mb[9] = vha->vp_idx;
1678        mcp->out_mb = MBX_9|MBX_0;
1679        mcp->in_mb = MBX_9|MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
1680        if (IS_CNA_CAPABLE(vha->hw))
1681                mcp->in_mb |= MBX_13|MBX_12|MBX_11|MBX_10;
1682        if (IS_FWI2_CAPABLE(vha->hw))
1683                mcp->in_mb |= MBX_19|MBX_18|MBX_17|MBX_16;
1684        if (IS_QLA27XX(vha->hw) || IS_QLA28XX(vha->hw)) {
1685                mcp->in_mb |= MBX_15;
1686                mcp->out_mb |= MBX_7|MBX_21|MBX_22|MBX_23;
1687        }
1688
1689        mcp->tov = MBX_TOV_SECONDS;
1690        mcp->flags = 0;
1691        rval = qla2x00_mailbox_command(vha, mcp);
1692        if (mcp->mb[0] == MBS_COMMAND_ERROR)
1693                rval = QLA_COMMAND_ERROR;
1694        else if (mcp->mb[0] == MBS_INVALID_COMMAND)
1695                rval = QLA_INVALID_COMMAND;
1696
1697        /* Return data. */
1698        *id = mcp->mb[1];
1699        *al_pa = LSB(mcp->mb[2]);
1700        *area = MSB(mcp->mb[2]);
1701        *domain = LSB(mcp->mb[3]);
1702        *top = mcp->mb[6];
1703        *sw_cap = mcp->mb[7];
1704
1705        if (rval != QLA_SUCCESS) {
1706                /*EMPTY*/
1707                ql_dbg(ql_dbg_mbx, vha, 0x1047, "Failed=%x.\n", rval);
1708        } else {
1709                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1048,
1710                    "Done %s.\n", __func__);
1711
1712                if (IS_CNA_CAPABLE(vha->hw)) {
1713                        vha->fcoe_vlan_id = mcp->mb[9] & 0xfff;
1714                        vha->fcoe_fcf_idx = mcp->mb[10];
1715                        vha->fcoe_vn_port_mac[5] = mcp->mb[11] >> 8;
1716                        vha->fcoe_vn_port_mac[4] = mcp->mb[11] & 0xff;
1717                        vha->fcoe_vn_port_mac[3] = mcp->mb[12] >> 8;
1718                        vha->fcoe_vn_port_mac[2] = mcp->mb[12] & 0xff;
1719                        vha->fcoe_vn_port_mac[1] = mcp->mb[13] >> 8;
1720                        vha->fcoe_vn_port_mac[0] = mcp->mb[13] & 0xff;
1721                }
1722                /* If FA-WWN supported */
1723                if (IS_FAWWN_CAPABLE(vha->hw)) {
1724                        if (mcp->mb[7] & BIT_14) {
1725                                vha->port_name[0] = MSB(mcp->mb[16]);
1726                                vha->port_name[1] = LSB(mcp->mb[16]);
1727                                vha->port_name[2] = MSB(mcp->mb[17]);
1728                                vha->port_name[3] = LSB(mcp->mb[17]);
1729                                vha->port_name[4] = MSB(mcp->mb[18]);
1730                                vha->port_name[5] = LSB(mcp->mb[18]);
1731                                vha->port_name[6] = MSB(mcp->mb[19]);
1732                                vha->port_name[7] = LSB(mcp->mb[19]);
1733                                fc_host_port_name(vha->host) =
1734                                    wwn_to_u64(vha->port_name);
1735                                ql_dbg(ql_dbg_mbx, vha, 0x10ca,
1736                                    "FA-WWN acquired %016llx\n",
1737                                    wwn_to_u64(vha->port_name));
1738                        }
1739                }
1740
1741                if (IS_QLA27XX(vha->hw) || IS_QLA28XX(vha->hw)) {
1742                        vha->bbcr = mcp->mb[15];
1743                        if (mcp->mb[7] & SCM_EDC_ACC_RECEIVED) {
1744                                ql_log(ql_log_info, vha, 0x11a4,
1745                                       "SCM: EDC ELS completed, flags 0x%x\n",
1746                                       mcp->mb[21]);
1747                        }
1748                        if (mcp->mb[7] & SCM_RDF_ACC_RECEIVED) {
1749                                vha->hw->flags.scm_enabled = 1;
1750                                vha->scm_fabric_connection_flags |=
1751                                    SCM_FLAG_RDF_COMPLETED;
1752                                ql_log(ql_log_info, vha, 0x11a5,
1753                                       "SCM: RDF ELS completed, flags 0x%x\n",
1754                                       mcp->mb[23]);
1755                        }
1756                }
1757        }
1758
1759        return rval;
1760}
1761
1762/*
1763 * qla2x00_get_retry_cnt
1764 *      Get current firmware login retry count and delay.
1765 *
1766 * Input:
1767 *      ha = adapter block pointer.
1768 *      retry_cnt = pointer to login retry count.
1769 *      tov = pointer to login timeout value.
1770 *
1771 * Returns:
1772 *      qla2x00 local function return status code.
1773 *
1774 * Context:
1775 *      Kernel context.
1776 */
1777int
1778qla2x00_get_retry_cnt(scsi_qla_host_t *vha, uint8_t *retry_cnt, uint8_t *tov,
1779    uint16_t *r_a_tov)
1780{
1781        int rval;
1782        uint16_t ratov;
1783        mbx_cmd_t mc;
1784        mbx_cmd_t *mcp = &mc;
1785
1786        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1049,
1787            "Entered %s.\n", __func__);
1788
1789        mcp->mb[0] = MBC_GET_RETRY_COUNT;
1790        mcp->out_mb = MBX_0;
1791        mcp->in_mb = MBX_3|MBX_2|MBX_1|MBX_0;
1792        mcp->tov = MBX_TOV_SECONDS;
1793        mcp->flags = 0;
1794        rval = qla2x00_mailbox_command(vha, mcp);
1795
1796        if (rval != QLA_SUCCESS) {
1797                /*EMPTY*/
1798                ql_dbg(ql_dbg_mbx, vha, 0x104a,
1799                    "Failed=%x mb[0]=%x.\n", rval, mcp->mb[0]);
1800        } else {
1801                /* Convert returned data and check our values. */
1802                *r_a_tov = mcp->mb[3] / 2;
1803                ratov = (mcp->mb[3]/2) / 10;  /* mb[3] value is in 100ms */
1804                if (mcp->mb[1] * ratov > (*retry_cnt) * (*tov)) {
1805                        /* Update to the larger values */
1806                        *retry_cnt = (uint8_t)mcp->mb[1];
1807                        *tov = ratov;
1808                }
1809
1810                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x104b,
1811                    "Done %s mb3=%d ratov=%d.\n", __func__, mcp->mb[3], ratov);
1812        }
1813
1814        return rval;
1815}
1816
1817/*
1818 * qla2x00_init_firmware
1819 *      Initialize adapter firmware.
1820 *
1821 * Input:
1822 *      ha = adapter block pointer.
1823 *      dptr = Initialization control block pointer.
1824 *      size = size of initialization control block.
1825 *      TARGET_QUEUE_LOCK must be released.
1826 *      ADAPTER_STATE_LOCK must be released.
1827 *
1828 * Returns:
1829 *      qla2x00 local function return status code.
1830 *
1831 * Context:
1832 *      Kernel context.
1833 */
1834int
1835qla2x00_init_firmware(scsi_qla_host_t *vha, uint16_t size)
1836{
1837        int rval;
1838        mbx_cmd_t mc;
1839        mbx_cmd_t *mcp = &mc;
1840        struct qla_hw_data *ha = vha->hw;
1841
1842        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x104c,
1843            "Entered %s.\n", __func__);
1844
1845        if (IS_P3P_TYPE(ha) && ql2xdbwr)
1846                qla82xx_wr_32(ha, (uintptr_t __force)ha->nxdb_wr_ptr,
1847                        (0x04 | (ha->portnum << 5) | (0 << 8) | (0 << 16)));
1848
1849        if (ha->flags.npiv_supported)
1850                mcp->mb[0] = MBC_MID_INITIALIZE_FIRMWARE;
1851        else
1852                mcp->mb[0] = MBC_INITIALIZE_FIRMWARE;
1853
1854        mcp->mb[1] = 0;
1855        mcp->mb[2] = MSW(ha->init_cb_dma);
1856        mcp->mb[3] = LSW(ha->init_cb_dma);
1857        mcp->mb[6] = MSW(MSD(ha->init_cb_dma));
1858        mcp->mb[7] = LSW(MSD(ha->init_cb_dma));
1859        mcp->out_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
1860        if (ha->ex_init_cb && ha->ex_init_cb->ex_version) {
1861                mcp->mb[1] = BIT_0;
1862                mcp->mb[10] = MSW(ha->ex_init_cb_dma);
1863                mcp->mb[11] = LSW(ha->ex_init_cb_dma);
1864                mcp->mb[12] = MSW(MSD(ha->ex_init_cb_dma));
1865                mcp->mb[13] = LSW(MSD(ha->ex_init_cb_dma));
1866                mcp->mb[14] = sizeof(*ha->ex_init_cb);
1867                mcp->out_mb |= MBX_14|MBX_13|MBX_12|MBX_11|MBX_10;
1868        }
1869
1870        if (ha->flags.scm_supported_f || vha->flags.nvme2_enabled) {
1871                mcp->mb[1] |= BIT_1;
1872                mcp->mb[16] = MSW(ha->sf_init_cb_dma);
1873                mcp->mb[17] = LSW(ha->sf_init_cb_dma);
1874                mcp->mb[18] = MSW(MSD(ha->sf_init_cb_dma));
1875                mcp->mb[19] = LSW(MSD(ha->sf_init_cb_dma));
1876                mcp->mb[15] = sizeof(*ha->sf_init_cb);
1877                mcp->out_mb |= MBX_19|MBX_18|MBX_17|MBX_16|MBX_15;
1878        }
1879
1880        /* 1 and 2 should normally be captured. */
1881        mcp->in_mb = MBX_2|MBX_1|MBX_0;
1882        if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha))
1883                /* mb3 is additional info about the installed SFP. */
1884                mcp->in_mb  |= MBX_3;
1885        mcp->buf_size = size;
1886        mcp->flags = MBX_DMA_OUT;
1887        mcp->tov = MBX_TOV_SECONDS;
1888        rval = qla2x00_mailbox_command(vha, mcp);
1889
1890        if (rval != QLA_SUCCESS) {
1891                /*EMPTY*/
1892                ql_dbg(ql_dbg_mbx, vha, 0x104d,
1893                    "Failed=%x mb[0]=%x, mb[1]=%x, mb[2]=%x, mb[3]=%x.\n",
1894                    rval, mcp->mb[0], mcp->mb[1], mcp->mb[2], mcp->mb[3]);
1895                if (ha->init_cb) {
1896                        ql_dbg(ql_dbg_mbx, vha, 0x104d, "init_cb:\n");
1897                        ql_dump_buffer(ql_dbg_init + ql_dbg_verbose, vha,
1898                            0x0104d, ha->init_cb, sizeof(*ha->init_cb));
1899                }
1900                if (ha->ex_init_cb && ha->ex_init_cb->ex_version) {
1901                        ql_dbg(ql_dbg_mbx, vha, 0x104d, "ex_init_cb:\n");
1902                        ql_dump_buffer(ql_dbg_init + ql_dbg_verbose, vha,
1903                            0x0104d, ha->ex_init_cb, sizeof(*ha->ex_init_cb));
1904                }
1905        } else {
1906                if (IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
1907                        if (mcp->mb[2] == 6 || mcp->mb[3] == 2)
1908                                ql_dbg(ql_dbg_mbx, vha, 0x119d,
1909                                    "Invalid SFP/Validation Failed\n");
1910                }
1911                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x104e,
1912                    "Done %s.\n", __func__);
1913        }
1914
1915        return rval;
1916}
1917
1918
1919/*
1920 * qla2x00_get_port_database
1921 *      Issue normal/enhanced get port database mailbox command
1922 *      and copy device name as necessary.
1923 *
1924 * Input:
1925 *      ha = adapter state pointer.
1926 *      dev = structure pointer.
1927 *      opt = enhanced cmd option byte.
1928 *
1929 * Returns:
1930 *      qla2x00 local function return status code.
1931 *
1932 * Context:
1933 *      Kernel context.
1934 */
1935int
1936qla2x00_get_port_database(scsi_qla_host_t *vha, fc_port_t *fcport, uint8_t opt)
1937{
1938        int rval;
1939        mbx_cmd_t mc;
1940        mbx_cmd_t *mcp = &mc;
1941        port_database_t *pd;
1942        struct port_database_24xx *pd24;
1943        dma_addr_t pd_dma;
1944        struct qla_hw_data *ha = vha->hw;
1945
1946        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x104f,
1947            "Entered %s.\n", __func__);
1948
1949        pd24 = NULL;
1950        pd = dma_pool_zalloc(ha->s_dma_pool, GFP_KERNEL, &pd_dma);
1951        if (pd  == NULL) {
1952                ql_log(ql_log_warn, vha, 0x1050,
1953                    "Failed to allocate port database structure.\n");
1954                fcport->query = 0;
1955                return QLA_MEMORY_ALLOC_FAILED;
1956        }
1957
1958        mcp->mb[0] = MBC_GET_PORT_DATABASE;
1959        if (opt != 0 && !IS_FWI2_CAPABLE(ha))
1960                mcp->mb[0] = MBC_ENHANCED_GET_PORT_DATABASE;
1961        mcp->mb[2] = MSW(pd_dma);
1962        mcp->mb[3] = LSW(pd_dma);
1963        mcp->mb[6] = MSW(MSD(pd_dma));
1964        mcp->mb[7] = LSW(MSD(pd_dma));
1965        mcp->mb[9] = vha->vp_idx;
1966        mcp->out_mb = MBX_9|MBX_7|MBX_6|MBX_3|MBX_2|MBX_0;
1967        mcp->in_mb = MBX_0;
1968        if (IS_FWI2_CAPABLE(ha)) {
1969                mcp->mb[1] = fcport->loop_id;
1970                mcp->mb[10] = opt;
1971                mcp->out_mb |= MBX_10|MBX_1;
1972                mcp->in_mb |= MBX_1;
1973        } else if (HAS_EXTENDED_IDS(ha)) {
1974                mcp->mb[1] = fcport->loop_id;
1975                mcp->mb[10] = opt;
1976                mcp->out_mb |= MBX_10|MBX_1;
1977        } else {
1978                mcp->mb[1] = fcport->loop_id << 8 | opt;
1979                mcp->out_mb |= MBX_1;
1980        }
1981        mcp->buf_size = IS_FWI2_CAPABLE(ha) ?
1982            PORT_DATABASE_24XX_SIZE : PORT_DATABASE_SIZE;
1983        mcp->flags = MBX_DMA_IN;
1984        mcp->tov = (ha->login_timeout * 2) + (ha->login_timeout / 2);
1985        rval = qla2x00_mailbox_command(vha, mcp);
1986        if (rval != QLA_SUCCESS)
1987                goto gpd_error_out;
1988
1989        if (IS_FWI2_CAPABLE(ha)) {
1990                uint64_t zero = 0;
1991                u8 current_login_state, last_login_state;
1992
1993                pd24 = (struct port_database_24xx *) pd;
1994
1995                /* Check for logged in state. */
1996                if (NVME_TARGET(ha, fcport)) {
1997                        current_login_state = pd24->current_login_state >> 4;
1998                        last_login_state = pd24->last_login_state >> 4;
1999                } else {
2000                        current_login_state = pd24->current_login_state & 0xf;
2001                        last_login_state = pd24->last_login_state & 0xf;
2002                }
2003                fcport->current_login_state = pd24->current_login_state;
2004                fcport->last_login_state = pd24->last_login_state;
2005
2006                /* Check for logged in state. */
2007                if (current_login_state != PDS_PRLI_COMPLETE &&
2008                    last_login_state != PDS_PRLI_COMPLETE) {
2009                        ql_dbg(ql_dbg_mbx, vha, 0x119a,
2010                            "Unable to verify login-state (%x/%x) for loop_id %x.\n",
2011                            current_login_state, last_login_state,
2012                            fcport->loop_id);
2013                        rval = QLA_FUNCTION_FAILED;
2014
2015                        if (!fcport->query)
2016                                goto gpd_error_out;
2017                }
2018
2019                if (fcport->loop_id == FC_NO_LOOP_ID ||
2020                    (memcmp(fcport->port_name, (uint8_t *)&zero, 8) &&
2021                     memcmp(fcport->port_name, pd24->port_name, 8))) {
2022                        /* We lost the device mid way. */
2023                        rval = QLA_NOT_LOGGED_IN;
2024                        goto gpd_error_out;
2025                }
2026
2027                /* Names are little-endian. */
2028                memcpy(fcport->node_name, pd24->node_name, WWN_SIZE);
2029                memcpy(fcport->port_name, pd24->port_name, WWN_SIZE);
2030
2031                /* Get port_id of device. */
2032                fcport->d_id.b.domain = pd24->port_id[0];
2033                fcport->d_id.b.area = pd24->port_id[1];
2034                fcport->d_id.b.al_pa = pd24->port_id[2];
2035                fcport->d_id.b.rsvd_1 = 0;
2036
2037                /* If not target must be initiator or unknown type. */
2038                if ((pd24->prli_svc_param_word_3[0] & BIT_4) == 0)
2039                        fcport->port_type = FCT_INITIATOR;
2040                else
2041                        fcport->port_type = FCT_TARGET;
2042
2043                /* Passback COS information. */
2044                fcport->supported_classes = (pd24->flags & PDF_CLASS_2) ?
2045                                FC_COS_CLASS2 : FC_COS_CLASS3;
2046
2047                if (pd24->prli_svc_param_word_3[0] & BIT_7)
2048                        fcport->flags |= FCF_CONF_COMP_SUPPORTED;
2049        } else {
2050                uint64_t zero = 0;
2051
2052                /* Check for logged in state. */
2053                if (pd->master_state != PD_STATE_PORT_LOGGED_IN &&
2054                    pd->slave_state != PD_STATE_PORT_LOGGED_IN) {
2055                        ql_dbg(ql_dbg_mbx, vha, 0x100a,
2056                            "Unable to verify login-state (%x/%x) - "
2057                            "portid=%02x%02x%02x.\n", pd->master_state,
2058                            pd->slave_state, fcport->d_id.b.domain,
2059                            fcport->d_id.b.area, fcport->d_id.b.al_pa);
2060                        rval = QLA_FUNCTION_FAILED;
2061                        goto gpd_error_out;
2062                }
2063
2064                if (fcport->loop_id == FC_NO_LOOP_ID ||
2065                    (memcmp(fcport->port_name, (uint8_t *)&zero, 8) &&
2066                     memcmp(fcport->port_name, pd->port_name, 8))) {
2067                        /* We lost the device mid way. */
2068                        rval = QLA_NOT_LOGGED_IN;
2069                        goto gpd_error_out;
2070                }
2071
2072                /* Names are little-endian. */
2073                memcpy(fcport->node_name, pd->node_name, WWN_SIZE);
2074                memcpy(fcport->port_name, pd->port_name, WWN_SIZE);
2075
2076                /* Get port_id of device. */
2077                fcport->d_id.b.domain = pd->port_id[0];
2078                fcport->d_id.b.area = pd->port_id[3];
2079                fcport->d_id.b.al_pa = pd->port_id[2];
2080                fcport->d_id.b.rsvd_1 = 0;
2081
2082                /* If not target must be initiator or unknown type. */
2083                if ((pd->prli_svc_param_word_3[0] & BIT_4) == 0)
2084                        fcport->port_type = FCT_INITIATOR;
2085                else
2086                        fcport->port_type = FCT_TARGET;
2087
2088                /* Passback COS information. */
2089                fcport->supported_classes = (pd->options & BIT_4) ?
2090                    FC_COS_CLASS2 : FC_COS_CLASS3;
2091        }
2092
2093gpd_error_out:
2094        dma_pool_free(ha->s_dma_pool, pd, pd_dma);
2095        fcport->query = 0;
2096
2097        if (rval != QLA_SUCCESS) {
2098                ql_dbg(ql_dbg_mbx, vha, 0x1052,
2099                    "Failed=%x mb[0]=%x mb[1]=%x.\n", rval,
2100                    mcp->mb[0], mcp->mb[1]);
2101        } else {
2102                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1053,
2103                    "Done %s.\n", __func__);
2104        }
2105
2106        return rval;
2107}
2108
2109int
2110qla24xx_get_port_database(scsi_qla_host_t *vha, u16 nport_handle,
2111        struct port_database_24xx *pdb)
2112{
2113        mbx_cmd_t mc;
2114        mbx_cmd_t *mcp = &mc;
2115        dma_addr_t pdb_dma;
2116        int rval;
2117
2118        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1115,
2119            "Entered %s.\n", __func__);
2120
2121        memset(pdb, 0, sizeof(*pdb));
2122
2123        pdb_dma = dma_map_single(&vha->hw->pdev->dev, pdb,
2124            sizeof(*pdb), DMA_FROM_DEVICE);
2125        if (!pdb_dma) {
2126                ql_log(ql_log_warn, vha, 0x1116, "Failed to map dma buffer.\n");
2127                return QLA_MEMORY_ALLOC_FAILED;
2128        }
2129
2130        mcp->mb[0] = MBC_GET_PORT_DATABASE;
2131        mcp->mb[1] = nport_handle;
2132        mcp->mb[2] = MSW(LSD(pdb_dma));
2133        mcp->mb[3] = LSW(LSD(pdb_dma));
2134        mcp->mb[6] = MSW(MSD(pdb_dma));
2135        mcp->mb[7] = LSW(MSD(pdb_dma));
2136        mcp->mb[9] = 0;
2137        mcp->mb[10] = 0;
2138        mcp->out_mb = MBX_10|MBX_9|MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
2139        mcp->in_mb = MBX_1|MBX_0;
2140        mcp->buf_size = sizeof(*pdb);
2141        mcp->flags = MBX_DMA_IN;
2142        mcp->tov = vha->hw->login_timeout * 2;
2143        rval = qla2x00_mailbox_command(vha, mcp);
2144
2145        if (rval != QLA_SUCCESS) {
2146                ql_dbg(ql_dbg_mbx, vha, 0x111a,
2147                    "Failed=%x mb[0]=%x mb[1]=%x.\n",
2148                    rval, mcp->mb[0], mcp->mb[1]);
2149        } else {
2150                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x111b,
2151                    "Done %s.\n", __func__);
2152        }
2153
2154        dma_unmap_single(&vha->hw->pdev->dev, pdb_dma,
2155            sizeof(*pdb), DMA_FROM_DEVICE);
2156
2157        return rval;
2158}
2159
2160/*
2161 * qla2x00_get_firmware_state
2162 *      Get adapter firmware state.
2163 *
2164 * Input:
2165 *      ha = adapter block pointer.
2166 *      dptr = pointer for firmware state.
2167 *      TARGET_QUEUE_LOCK must be released.
2168 *      ADAPTER_STATE_LOCK must be released.
2169 *
2170 * Returns:
2171 *      qla2x00 local function return status code.
2172 *
2173 * Context:
2174 *      Kernel context.
2175 */
2176int
2177qla2x00_get_firmware_state(scsi_qla_host_t *vha, uint16_t *states)
2178{
2179        int rval;
2180        mbx_cmd_t mc;
2181        mbx_cmd_t *mcp = &mc;
2182        struct qla_hw_data *ha = vha->hw;
2183
2184        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1054,
2185            "Entered %s.\n", __func__);
2186
2187        mcp->mb[0] = MBC_GET_FIRMWARE_STATE;
2188        mcp->out_mb = MBX_0;
2189        if (IS_FWI2_CAPABLE(vha->hw))
2190                mcp->in_mb = MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
2191        else
2192                mcp->in_mb = MBX_1|MBX_0;
2193        mcp->tov = MBX_TOV_SECONDS;
2194        mcp->flags = 0;
2195        rval = qla2x00_mailbox_command(vha, mcp);
2196
2197        /* Return firmware states. */
2198        states[0] = mcp->mb[1];
2199        if (IS_FWI2_CAPABLE(vha->hw)) {
2200                states[1] = mcp->mb[2];
2201                states[2] = mcp->mb[3];  /* SFP info */
2202                states[3] = mcp->mb[4];
2203                states[4] = mcp->mb[5];
2204                states[5] = mcp->mb[6];  /* DPORT status */
2205        }
2206
2207        if (rval != QLA_SUCCESS) {
2208                /*EMPTY*/
2209                ql_dbg(ql_dbg_mbx, vha, 0x1055, "Failed=%x.\n", rval);
2210        } else {
2211                if (IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
2212                        if (mcp->mb[2] == 6 || mcp->mb[3] == 2)
2213                                ql_dbg(ql_dbg_mbx, vha, 0x119e,
2214                                    "Invalid SFP/Validation Failed\n");
2215                }
2216                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1056,
2217                    "Done %s.\n", __func__);
2218        }
2219
2220        return rval;
2221}
2222
2223/*
2224 * qla2x00_get_port_name
2225 *      Issue get port name mailbox command.
2226 *      Returned name is in big endian format.
2227 *
2228 * Input:
2229 *      ha = adapter block pointer.
2230 *      loop_id = loop ID of device.
2231 *      name = pointer for name.
2232 *      TARGET_QUEUE_LOCK must be released.
2233 *      ADAPTER_STATE_LOCK must be released.
2234 *
2235 * Returns:
2236 *      qla2x00 local function return status code.
2237 *
2238 * Context:
2239 *      Kernel context.
2240 */
2241int
2242qla2x00_get_port_name(scsi_qla_host_t *vha, uint16_t loop_id, uint8_t *name,
2243    uint8_t opt)
2244{
2245        int rval;
2246        mbx_cmd_t mc;
2247        mbx_cmd_t *mcp = &mc;
2248
2249        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1057,
2250            "Entered %s.\n", __func__);
2251
2252        mcp->mb[0] = MBC_GET_PORT_NAME;
2253        mcp->mb[9] = vha->vp_idx;
2254        mcp->out_mb = MBX_9|MBX_1|MBX_0;
2255        if (HAS_EXTENDED_IDS(vha->hw)) {
2256                mcp->mb[1] = loop_id;
2257                mcp->mb[10] = opt;
2258                mcp->out_mb |= MBX_10;
2259        } else {
2260                mcp->mb[1] = loop_id << 8 | opt;
2261        }
2262
2263        mcp->in_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
2264        mcp->tov = MBX_TOV_SECONDS;
2265        mcp->flags = 0;
2266        rval = qla2x00_mailbox_command(vha, mcp);
2267
2268        if (rval != QLA_SUCCESS) {
2269                /*EMPTY*/
2270                ql_dbg(ql_dbg_mbx, vha, 0x1058, "Failed=%x.\n", rval);
2271        } else {
2272                if (name != NULL) {
2273                        /* This function returns name in big endian. */
2274                        name[0] = MSB(mcp->mb[2]);
2275                        name[1] = LSB(mcp->mb[2]);
2276                        name[2] = MSB(mcp->mb[3]);
2277                        name[3] = LSB(mcp->mb[3]);
2278                        name[4] = MSB(mcp->mb[6]);
2279                        name[5] = LSB(mcp->mb[6]);
2280                        name[6] = MSB(mcp->mb[7]);
2281                        name[7] = LSB(mcp->mb[7]);
2282                }
2283
2284                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1059,
2285                    "Done %s.\n", __func__);
2286        }
2287
2288        return rval;
2289}
2290
2291/*
2292 * qla24xx_link_initialization
2293 *      Issue link initialization mailbox command.
2294 *
2295 * Input:
2296 *      ha = adapter block pointer.
2297 *      TARGET_QUEUE_LOCK must be released.
2298 *      ADAPTER_STATE_LOCK must be released.
2299 *
2300 * Returns:
2301 *      qla2x00 local function return status code.
2302 *
2303 * Context:
2304 *      Kernel context.
2305 */
2306int
2307qla24xx_link_initialize(scsi_qla_host_t *vha)
2308{
2309        int rval;
2310        mbx_cmd_t mc;
2311        mbx_cmd_t *mcp = &mc;
2312
2313        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1152,
2314            "Entered %s.\n", __func__);
2315
2316        if (!IS_FWI2_CAPABLE(vha->hw) || IS_CNA_CAPABLE(vha->hw))
2317                return QLA_FUNCTION_FAILED;
2318
2319        mcp->mb[0] = MBC_LINK_INITIALIZATION;
2320        mcp->mb[1] = BIT_4;
2321        if (vha->hw->operating_mode == LOOP)
2322                mcp->mb[1] |= BIT_6;
2323        else
2324                mcp->mb[1] |= BIT_5;
2325        mcp->mb[2] = 0;
2326        mcp->mb[3] = 0;
2327        mcp->out_mb = MBX_3|MBX_2|MBX_1|MBX_0;
2328        mcp->in_mb = MBX_0;
2329        mcp->tov = MBX_TOV_SECONDS;
2330        mcp->flags = 0;
2331        rval = qla2x00_mailbox_command(vha, mcp);
2332
2333        if (rval != QLA_SUCCESS) {
2334                ql_dbg(ql_dbg_mbx, vha, 0x1153, "Failed=%x.\n", rval);
2335        } else {
2336                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1154,
2337                    "Done %s.\n", __func__);
2338        }
2339
2340        return rval;
2341}
2342
2343/*
2344 * qla2x00_lip_reset
2345 *      Issue LIP reset mailbox command.
2346 *
2347 * Input:
2348 *      ha = adapter block pointer.
2349 *      TARGET_QUEUE_LOCK must be released.
2350 *      ADAPTER_STATE_LOCK must be released.
2351 *
2352 * Returns:
2353 *      qla2x00 local function return status code.
2354 *
2355 * Context:
2356 *      Kernel context.
2357 */
2358int
2359qla2x00_lip_reset(scsi_qla_host_t *vha)
2360{
2361        int rval;
2362        mbx_cmd_t mc;
2363        mbx_cmd_t *mcp = &mc;
2364
2365        ql_dbg(ql_dbg_disc, vha, 0x105a,
2366            "Entered %s.\n", __func__);
2367
2368        if (IS_CNA_CAPABLE(vha->hw)) {
2369                /* Logout across all FCFs. */
2370                mcp->mb[0] = MBC_LIP_FULL_LOGIN;
2371                mcp->mb[1] = BIT_1;
2372                mcp->mb[2] = 0;
2373                mcp->out_mb = MBX_2|MBX_1|MBX_0;
2374        } else if (IS_FWI2_CAPABLE(vha->hw)) {
2375                mcp->mb[0] = MBC_LIP_FULL_LOGIN;
2376                mcp->mb[1] = BIT_4;
2377                mcp->mb[2] = 0;
2378                mcp->mb[3] = vha->hw->loop_reset_delay;
2379                mcp->out_mb = MBX_3|MBX_2|MBX_1|MBX_0;
2380        } else {
2381                mcp->mb[0] = MBC_LIP_RESET;
2382                mcp->out_mb = MBX_3|MBX_2|MBX_1|MBX_0;
2383                if (HAS_EXTENDED_IDS(vha->hw)) {
2384                        mcp->mb[1] = 0x00ff;
2385                        mcp->mb[10] = 0;
2386                        mcp->out_mb |= MBX_10;
2387                } else {
2388                        mcp->mb[1] = 0xff00;
2389                }
2390                mcp->mb[2] = vha->hw->loop_reset_delay;
2391                mcp->mb[3] = 0;
2392        }
2393        mcp->in_mb = MBX_0;
2394        mcp->tov = MBX_TOV_SECONDS;
2395        mcp->flags = 0;
2396        rval = qla2x00_mailbox_command(vha, mcp);
2397
2398        if (rval != QLA_SUCCESS) {
2399                /*EMPTY*/
2400                ql_dbg(ql_dbg_mbx, vha, 0x105b, "Failed=%x.\n", rval);
2401        } else {
2402                /*EMPTY*/
2403                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x105c,
2404                    "Done %s.\n", __func__);
2405        }
2406
2407        return rval;
2408}
2409
2410/*
2411 * qla2x00_send_sns
2412 *      Send SNS command.
2413 *
2414 * Input:
2415 *      ha = adapter block pointer.
2416 *      sns = pointer for command.
2417 *      cmd_size = command size.
2418 *      buf_size = response/command size.
2419 *      TARGET_QUEUE_LOCK must be released.
2420 *      ADAPTER_STATE_LOCK must be released.
2421 *
2422 * Returns:
2423 *      qla2x00 local function return status code.
2424 *
2425 * Context:
2426 *      Kernel context.
2427 */
2428int
2429qla2x00_send_sns(scsi_qla_host_t *vha, dma_addr_t sns_phys_address,
2430    uint16_t cmd_size, size_t buf_size)
2431{
2432        int rval;
2433        mbx_cmd_t mc;
2434        mbx_cmd_t *mcp = &mc;
2435
2436        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x105d,
2437            "Entered %s.\n", __func__);
2438
2439        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x105e,
2440            "Retry cnt=%d ratov=%d total tov=%d.\n",
2441            vha->hw->retry_count, vha->hw->login_timeout, mcp->tov);
2442
2443        mcp->mb[0] = MBC_SEND_SNS_COMMAND;
2444        mcp->mb[1] = cmd_size;
2445        mcp->mb[2] = MSW(sns_phys_address);
2446        mcp->mb[3] = LSW(sns_phys_address);
2447        mcp->mb[6] = MSW(MSD(sns_phys_address));
2448        mcp->mb[7] = LSW(MSD(sns_phys_address));
2449        mcp->out_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
2450        mcp->in_mb = MBX_0|MBX_1;
2451        mcp->buf_size = buf_size;
2452        mcp->flags = MBX_DMA_OUT|MBX_DMA_IN;
2453        mcp->tov = (vha->hw->login_timeout * 2) + (vha->hw->login_timeout / 2);
2454        rval = qla2x00_mailbox_command(vha, mcp);
2455
2456        if (rval != QLA_SUCCESS) {
2457                /*EMPTY*/
2458                ql_dbg(ql_dbg_mbx, vha, 0x105f,
2459                    "Failed=%x mb[0]=%x mb[1]=%x.\n",
2460                    rval, mcp->mb[0], mcp->mb[1]);
2461        } else {
2462                /*EMPTY*/
2463                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1060,
2464                    "Done %s.\n", __func__);
2465        }
2466
2467        return rval;
2468}
2469
2470int
2471qla24xx_login_fabric(scsi_qla_host_t *vha, uint16_t loop_id, uint8_t domain,
2472    uint8_t area, uint8_t al_pa, uint16_t *mb, uint8_t opt)
2473{
2474        int             rval;
2475
2476        struct logio_entry_24xx *lg;
2477        dma_addr_t      lg_dma;
2478        uint32_t        iop[2];
2479        struct qla_hw_data *ha = vha->hw;
2480        struct req_que *req;
2481
2482        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1061,
2483            "Entered %s.\n", __func__);
2484
2485        if (vha->vp_idx && vha->qpair)
2486                req = vha->qpair->req;
2487        else
2488                req = ha->req_q_map[0];
2489
2490        lg = dma_pool_zalloc(ha->s_dma_pool, GFP_KERNEL, &lg_dma);
2491        if (lg == NULL) {
2492                ql_log(ql_log_warn, vha, 0x1062,
2493                    "Failed to allocate login IOCB.\n");
2494                return QLA_MEMORY_ALLOC_FAILED;
2495        }
2496
2497        lg->entry_type = LOGINOUT_PORT_IOCB_TYPE;
2498        lg->entry_count = 1;
2499        lg->handle = make_handle(req->id, lg->handle);
2500        lg->nport_handle = cpu_to_le16(loop_id);
2501        lg->control_flags = cpu_to_le16(LCF_COMMAND_PLOGI);
2502        if (opt & BIT_0)
2503                lg->control_flags |= cpu_to_le16(LCF_COND_PLOGI);
2504        if (opt & BIT_1)
2505                lg->control_flags |= cpu_to_le16(LCF_SKIP_PRLI);
2506        lg->port_id[0] = al_pa;
2507        lg->port_id[1] = area;
2508        lg->port_id[2] = domain;
2509        lg->vp_index = vha->vp_idx;
2510        rval = qla2x00_issue_iocb_timeout(vha, lg, lg_dma, 0,
2511            (ha->r_a_tov / 10 * 2) + 2);
2512        if (rval != QLA_SUCCESS) {
2513                ql_dbg(ql_dbg_mbx, vha, 0x1063,
2514                    "Failed to issue login IOCB (%x).\n", rval);
2515        } else if (lg->entry_status != 0) {
2516                ql_dbg(ql_dbg_mbx, vha, 0x1064,
2517                    "Failed to complete IOCB -- error status (%x).\n",
2518                    lg->entry_status);
2519                rval = QLA_FUNCTION_FAILED;
2520        } else if (lg->comp_status != cpu_to_le16(CS_COMPLETE)) {
2521                iop[0] = le32_to_cpu(lg->io_parameter[0]);
2522                iop[1] = le32_to_cpu(lg->io_parameter[1]);
2523
2524                ql_dbg(ql_dbg_mbx, vha, 0x1065,
2525                    "Failed to complete IOCB -- completion  status (%x) "
2526                    "ioparam=%x/%x.\n", le16_to_cpu(lg->comp_status),
2527                    iop[0], iop[1]);
2528
2529                switch (iop[0]) {
2530                case LSC_SCODE_PORTID_USED:
2531                        mb[0] = MBS_PORT_ID_USED;
2532                        mb[1] = LSW(iop[1]);
2533                        break;
2534                case LSC_SCODE_NPORT_USED:
2535                        mb[0] = MBS_LOOP_ID_USED;
2536                        break;
2537                case LSC_SCODE_NOLINK:
2538                case LSC_SCODE_NOIOCB:
2539                case LSC_SCODE_NOXCB:
2540                case LSC_SCODE_CMD_FAILED:
2541                case LSC_SCODE_NOFABRIC:
2542                case LSC_SCODE_FW_NOT_READY:
2543                case LSC_SCODE_NOT_LOGGED_IN:
2544                case LSC_SCODE_NOPCB:
2545                case LSC_SCODE_ELS_REJECT:
2546                case LSC_SCODE_CMD_PARAM_ERR:
2547                case LSC_SCODE_NONPORT:
2548                case LSC_SCODE_LOGGED_IN:
2549                case LSC_SCODE_NOFLOGI_ACC:
2550                default:
2551                        mb[0] = MBS_COMMAND_ERROR;
2552                        break;
2553                }
2554        } else {
2555                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1066,
2556                    "Done %s.\n", __func__);
2557
2558                iop[0] = le32_to_cpu(lg->io_parameter[0]);
2559
2560                mb[0] = MBS_COMMAND_COMPLETE;
2561                mb[1] = 0;
2562                if (iop[0] & BIT_4) {
2563                        if (iop[0] & BIT_8)
2564                                mb[1] |= BIT_1;
2565                } else
2566                        mb[1] = BIT_0;
2567
2568                /* Passback COS information. */
2569                mb[10] = 0;
2570                if (lg->io_parameter[7] || lg->io_parameter[8])
2571                        mb[10] |= BIT_0;        /* Class 2. */
2572                if (lg->io_parameter[9] || lg->io_parameter[10])
2573                        mb[10] |= BIT_1;        /* Class 3. */
2574                if (lg->io_parameter[0] & cpu_to_le32(BIT_7))
2575                        mb[10] |= BIT_7;        /* Confirmed Completion
2576                                                 * Allowed
2577                                                 */
2578        }
2579
2580        dma_pool_free(ha->s_dma_pool, lg, lg_dma);
2581
2582        return rval;
2583}
2584
2585/*
2586 * qla2x00_login_fabric
2587 *      Issue login fabric port mailbox command.
2588 *
2589 * Input:
2590 *      ha = adapter block pointer.
2591 *      loop_id = device loop ID.
2592 *      domain = device domain.
2593 *      area = device area.
2594 *      al_pa = device AL_PA.
2595 *      status = pointer for return status.
2596 *      opt = command options.
2597 *      TARGET_QUEUE_LOCK must be released.
2598 *      ADAPTER_STATE_LOCK must be released.
2599 *
2600 * Returns:
2601 *      qla2x00 local function return status code.
2602 *
2603 * Context:
2604 *      Kernel context.
2605 */
2606int
2607qla2x00_login_fabric(scsi_qla_host_t *vha, uint16_t loop_id, uint8_t domain,
2608    uint8_t area, uint8_t al_pa, uint16_t *mb, uint8_t opt)
2609{
2610        int rval;
2611        mbx_cmd_t mc;
2612        mbx_cmd_t *mcp = &mc;
2613        struct qla_hw_data *ha = vha->hw;
2614
2615        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1067,
2616            "Entered %s.\n", __func__);
2617
2618        mcp->mb[0] = MBC_LOGIN_FABRIC_PORT;
2619        mcp->out_mb = MBX_3|MBX_2|MBX_1|MBX_0;
2620        if (HAS_EXTENDED_IDS(ha)) {
2621                mcp->mb[1] = loop_id;
2622                mcp->mb[10] = opt;
2623                mcp->out_mb |= MBX_10;
2624        } else {
2625                mcp->mb[1] = (loop_id << 8) | opt;
2626        }
2627        mcp->mb[2] = domain;
2628        mcp->mb[3] = area << 8 | al_pa;
2629
2630        mcp->in_mb = MBX_7|MBX_6|MBX_2|MBX_1|MBX_0;
2631        mcp->tov = (ha->login_timeout * 2) + (ha->login_timeout / 2);
2632        mcp->flags = 0;
2633        rval = qla2x00_mailbox_command(vha, mcp);
2634
2635        /* Return mailbox statuses. */
2636        if (mb != NULL) {
2637                mb[0] = mcp->mb[0];
2638                mb[1] = mcp->mb[1];
2639                mb[2] = mcp->mb[2];
2640                mb[6] = mcp->mb[6];
2641                mb[7] = mcp->mb[7];
2642                /* COS retrieved from Get-Port-Database mailbox command. */
2643                mb[10] = 0;
2644        }
2645
2646        if (rval != QLA_SUCCESS) {
2647                /* RLU tmp code: need to change main mailbox_command function to
2648                 * return ok even when the mailbox completion value is not
2649                 * SUCCESS. The caller needs to be responsible to interpret
2650                 * the return values of this mailbox command if we're not
2651                 * to change too much of the existing code.
2652                 */
2653                if (mcp->mb[0] == 0x4001 || mcp->mb[0] == 0x4002 ||
2654                    mcp->mb[0] == 0x4003 || mcp->mb[0] == 0x4005 ||
2655                    mcp->mb[0] == 0x4006)
2656                        rval = QLA_SUCCESS;
2657
2658                /*EMPTY*/
2659                ql_dbg(ql_dbg_mbx, vha, 0x1068,
2660                    "Failed=%x mb[0]=%x mb[1]=%x mb[2]=%x.\n",
2661                    rval, mcp->mb[0], mcp->mb[1], mcp->mb[2]);
2662        } else {
2663                /*EMPTY*/
2664                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1069,
2665                    "Done %s.\n", __func__);
2666        }
2667
2668        return rval;
2669}
2670
2671/*
2672 * qla2x00_login_local_device
2673 *           Issue login loop port mailbox command.
2674 *
2675 * Input:
2676 *           ha = adapter block pointer.
2677 *           loop_id = device loop ID.
2678 *           opt = command options.
2679 *
2680 * Returns:
2681 *            Return status code.
2682 *
2683 * Context:
2684 *            Kernel context.
2685 *
2686 */
2687int
2688qla2x00_login_local_device(scsi_qla_host_t *vha, fc_port_t *fcport,
2689    uint16_t *mb_ret, uint8_t opt)
2690{
2691        int rval;
2692        mbx_cmd_t mc;
2693        mbx_cmd_t *mcp = &mc;
2694        struct qla_hw_data *ha = vha->hw;
2695
2696        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x106a,
2697            "Entered %s.\n", __func__);
2698
2699        if (IS_FWI2_CAPABLE(ha))
2700                return qla24xx_login_fabric(vha, fcport->loop_id,
2701                    fcport->d_id.b.domain, fcport->d_id.b.area,
2702                    fcport->d_id.b.al_pa, mb_ret, opt);
2703
2704        mcp->mb[0] = MBC_LOGIN_LOOP_PORT;
2705        if (HAS_EXTENDED_IDS(ha))
2706                mcp->mb[1] = fcport->loop_id;
2707        else
2708                mcp->mb[1] = fcport->loop_id << 8;
2709        mcp->mb[2] = opt;
2710        mcp->out_mb = MBX_2|MBX_1|MBX_0;
2711        mcp->in_mb = MBX_7|MBX_6|MBX_1|MBX_0;
2712        mcp->tov = (ha->login_timeout * 2) + (ha->login_timeout / 2);
2713        mcp->flags = 0;
2714        rval = qla2x00_mailbox_command(vha, mcp);
2715
2716        /* Return mailbox statuses. */
2717        if (mb_ret != NULL) {
2718                mb_ret[0] = mcp->mb[0];
2719                mb_ret[1] = mcp->mb[1];
2720                mb_ret[6] = mcp->mb[6];
2721                mb_ret[7] = mcp->mb[7];
2722        }
2723
2724        if (rval != QLA_SUCCESS) {
2725                /* AV tmp code: need to change main mailbox_command function to
2726                 * return ok even when the mailbox completion value is not
2727                 * SUCCESS. The caller needs to be responsible to interpret
2728                 * the return values of this mailbox command if we're not
2729                 * to change too much of the existing code.
2730                 */
2731                if (mcp->mb[0] == 0x4005 || mcp->mb[0] == 0x4006)
2732                        rval = QLA_SUCCESS;
2733
2734                ql_dbg(ql_dbg_mbx, vha, 0x106b,
2735                    "Failed=%x mb[0]=%x mb[1]=%x mb[6]=%x mb[7]=%x.\n",
2736                    rval, mcp->mb[0], mcp->mb[1], mcp->mb[6], mcp->mb[7]);
2737        } else {
2738                /*EMPTY*/
2739                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x106c,
2740                    "Done %s.\n", __func__);
2741        }
2742
2743        return (rval);
2744}
2745
2746int
2747qla24xx_fabric_logout(scsi_qla_host_t *vha, uint16_t loop_id, uint8_t domain,
2748    uint8_t area, uint8_t al_pa)
2749{
2750        int             rval;
2751        struct logio_entry_24xx *lg;
2752        dma_addr_t      lg_dma;
2753        struct qla_hw_data *ha = vha->hw;
2754        struct req_que *req;
2755
2756        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x106d,
2757            "Entered %s.\n", __func__);
2758
2759        lg = dma_pool_zalloc(ha->s_dma_pool, GFP_KERNEL, &lg_dma);
2760        if (lg == NULL) {
2761                ql_log(ql_log_warn, vha, 0x106e,
2762                    "Failed to allocate logout IOCB.\n");
2763                return QLA_MEMORY_ALLOC_FAILED;
2764        }
2765
2766        req = vha->req;
2767        lg->entry_type = LOGINOUT_PORT_IOCB_TYPE;
2768        lg->entry_count = 1;
2769        lg->handle = make_handle(req->id, lg->handle);
2770        lg->nport_handle = cpu_to_le16(loop_id);
2771        lg->control_flags =
2772            cpu_to_le16(LCF_COMMAND_LOGO|LCF_IMPL_LOGO|
2773                LCF_FREE_NPORT);
2774        lg->port_id[0] = al_pa;
2775        lg->port_id[1] = area;
2776        lg->port_id[2] = domain;
2777        lg->vp_index = vha->vp_idx;
2778        rval = qla2x00_issue_iocb_timeout(vha, lg, lg_dma, 0,
2779            (ha->r_a_tov / 10 * 2) + 2);
2780        if (rval != QLA_SUCCESS) {
2781                ql_dbg(ql_dbg_mbx, vha, 0x106f,
2782                    "Failed to issue logout IOCB (%x).\n", rval);
2783        } else if (lg->entry_status != 0) {
2784                ql_dbg(ql_dbg_mbx, vha, 0x1070,
2785                    "Failed to complete IOCB -- error status (%x).\n",
2786                    lg->entry_status);
2787                rval = QLA_FUNCTION_FAILED;
2788        } else if (lg->comp_status != cpu_to_le16(CS_COMPLETE)) {
2789                ql_dbg(ql_dbg_mbx, vha, 0x1071,
2790                    "Failed to complete IOCB -- completion status (%x) "
2791                    "ioparam=%x/%x.\n", le16_to_cpu(lg->comp_status),
2792                    le32_to_cpu(lg->io_parameter[0]),
2793                    le32_to_cpu(lg->io_parameter[1]));
2794        } else {
2795                /*EMPTY*/
2796                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1072,
2797                    "Done %s.\n", __func__);
2798        }
2799
2800        dma_pool_free(ha->s_dma_pool, lg, lg_dma);
2801
2802        return rval;
2803}
2804
2805/*
2806 * qla2x00_fabric_logout
2807 *      Issue logout fabric port mailbox command.
2808 *
2809 * Input:
2810 *      ha = adapter block pointer.
2811 *      loop_id = device loop ID.
2812 *      TARGET_QUEUE_LOCK must be released.
2813 *      ADAPTER_STATE_LOCK must be released.
2814 *
2815 * Returns:
2816 *      qla2x00 local function return status code.
2817 *
2818 * Context:
2819 *      Kernel context.
2820 */
2821int
2822qla2x00_fabric_logout(scsi_qla_host_t *vha, uint16_t loop_id, uint8_t domain,
2823    uint8_t area, uint8_t al_pa)
2824{
2825        int rval;
2826        mbx_cmd_t mc;
2827        mbx_cmd_t *mcp = &mc;
2828
2829        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1073,
2830            "Entered %s.\n", __func__);
2831
2832        mcp->mb[0] = MBC_LOGOUT_FABRIC_PORT;
2833        mcp->out_mb = MBX_1|MBX_0;
2834        if (HAS_EXTENDED_IDS(vha->hw)) {
2835                mcp->mb[1] = loop_id;
2836                mcp->mb[10] = 0;
2837                mcp->out_mb |= MBX_10;
2838        } else {
2839                mcp->mb[1] = loop_id << 8;
2840        }
2841
2842        mcp->in_mb = MBX_1|MBX_0;
2843        mcp->tov = MBX_TOV_SECONDS;
2844        mcp->flags = 0;
2845        rval = qla2x00_mailbox_command(vha, mcp);
2846
2847        if (rval != QLA_SUCCESS) {
2848                /*EMPTY*/
2849                ql_dbg(ql_dbg_mbx, vha, 0x1074,
2850                    "Failed=%x mb[1]=%x.\n", rval, mcp->mb[1]);
2851        } else {
2852                /*EMPTY*/
2853                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1075,
2854                    "Done %s.\n", __func__);
2855        }
2856
2857        return rval;
2858}
2859
2860/*
2861 * qla2x00_full_login_lip
2862 *      Issue full login LIP mailbox command.
2863 *
2864 * Input:
2865 *      ha = adapter block pointer.
2866 *      TARGET_QUEUE_LOCK must be released.
2867 *      ADAPTER_STATE_LOCK must be released.
2868 *
2869 * Returns:
2870 *      qla2x00 local function return status code.
2871 *
2872 * Context:
2873 *      Kernel context.
2874 */
2875int
2876qla2x00_full_login_lip(scsi_qla_host_t *vha)
2877{
2878        int rval;
2879        mbx_cmd_t mc;
2880        mbx_cmd_t *mcp = &mc;
2881
2882        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1076,
2883            "Entered %s.\n", __func__);
2884
2885        mcp->mb[0] = MBC_LIP_FULL_LOGIN;
2886        mcp->mb[1] = IS_FWI2_CAPABLE(vha->hw) ? BIT_4 : 0;
2887        mcp->mb[2] = 0;
2888        mcp->mb[3] = 0;
2889        mcp->out_mb = MBX_3|MBX_2|MBX_1|MBX_0;
2890        mcp->in_mb = MBX_0;
2891        mcp->tov = MBX_TOV_SECONDS;
2892        mcp->flags = 0;
2893        rval = qla2x00_mailbox_command(vha, mcp);
2894
2895        if (rval != QLA_SUCCESS) {
2896                /*EMPTY*/
2897                ql_dbg(ql_dbg_mbx, vha, 0x1077, "Failed=%x.\n", rval);
2898        } else {
2899                /*EMPTY*/
2900                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1078,
2901                    "Done %s.\n", __func__);
2902        }
2903
2904        return rval;
2905}
2906
2907/*
2908 * qla2x00_get_id_list
2909 *
2910 * Input:
2911 *      ha = adapter block pointer.
2912 *
2913 * Returns:
2914 *      qla2x00 local function return status code.
2915 *
2916 * Context:
2917 *      Kernel context.
2918 */
2919int
2920qla2x00_get_id_list(scsi_qla_host_t *vha, void *id_list, dma_addr_t id_list_dma,
2921    uint16_t *entries)
2922{
2923        int rval;
2924        mbx_cmd_t mc;
2925        mbx_cmd_t *mcp = &mc;
2926
2927        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1079,
2928            "Entered %s.\n", __func__);
2929
2930        if (id_list == NULL)
2931                return QLA_FUNCTION_FAILED;
2932
2933        mcp->mb[0] = MBC_GET_ID_LIST;
2934        mcp->out_mb = MBX_0;
2935        if (IS_FWI2_CAPABLE(vha->hw)) {
2936                mcp->mb[2] = MSW(id_list_dma);
2937                mcp->mb[3] = LSW(id_list_dma);
2938                mcp->mb[6] = MSW(MSD(id_list_dma));
2939                mcp->mb[7] = LSW(MSD(id_list_dma));
2940                mcp->mb[8] = 0;
2941                mcp->mb[9] = vha->vp_idx;
2942                mcp->out_mb |= MBX_9|MBX_8|MBX_7|MBX_6|MBX_3|MBX_2;
2943        } else {
2944                mcp->mb[1] = MSW(id_list_dma);
2945                mcp->mb[2] = LSW(id_list_dma);
2946                mcp->mb[3] = MSW(MSD(id_list_dma));
2947                mcp->mb[6] = LSW(MSD(id_list_dma));
2948                mcp->out_mb |= MBX_6|MBX_3|MBX_2|MBX_1;
2949        }
2950        mcp->in_mb = MBX_1|MBX_0;
2951        mcp->tov = MBX_TOV_SECONDS;
2952        mcp->flags = 0;
2953        rval = qla2x00_mailbox_command(vha, mcp);
2954
2955        if (rval != QLA_SUCCESS) {
2956                /*EMPTY*/
2957                ql_dbg(ql_dbg_mbx, vha, 0x107a, "Failed=%x.\n", rval);
2958        } else {
2959                *entries = mcp->mb[1];
2960                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x107b,
2961                    "Done %s.\n", __func__);
2962        }
2963
2964        return rval;
2965}
2966
2967/*
2968 * qla2x00_get_resource_cnts
2969 *      Get current firmware resource counts.
2970 *
2971 * Input:
2972 *      ha = adapter block pointer.
2973 *
2974 * Returns:
2975 *      qla2x00 local function return status code.
2976 *
2977 * Context:
2978 *      Kernel context.
2979 */
2980int
2981qla2x00_get_resource_cnts(scsi_qla_host_t *vha)
2982{
2983        struct qla_hw_data *ha = vha->hw;
2984        int rval;
2985        mbx_cmd_t mc;
2986        mbx_cmd_t *mcp = &mc;
2987
2988        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x107c,
2989            "Entered %s.\n", __func__);
2990
2991        mcp->mb[0] = MBC_GET_RESOURCE_COUNTS;
2992        mcp->out_mb = MBX_0;
2993        mcp->in_mb = MBX_11|MBX_10|MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
2994        if (IS_QLA81XX(ha) || IS_QLA83XX(ha) ||
2995            IS_QLA27XX(ha) || IS_QLA28XX(ha))
2996                mcp->in_mb |= MBX_12;
2997        mcp->tov = MBX_TOV_SECONDS;
2998        mcp->flags = 0;
2999        rval = qla2x00_mailbox_command(vha, mcp);
3000
3001        if (rval != QLA_SUCCESS) {
3002                /*EMPTY*/
3003                ql_dbg(ql_dbg_mbx, vha, 0x107d,
3004                    "Failed mb[0]=%x.\n", mcp->mb[0]);
3005        } else {
3006                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x107e,
3007                    "Done %s mb1=%x mb2=%x mb3=%x mb6=%x mb7=%x mb10=%x "
3008                    "mb11=%x mb12=%x.\n", __func__, mcp->mb[1], mcp->mb[2],
3009                    mcp->mb[3], mcp->mb[6], mcp->mb[7], mcp->mb[10],
3010                    mcp->mb[11], mcp->mb[12]);
3011
3012                ha->orig_fw_tgt_xcb_count =  mcp->mb[1];
3013                ha->cur_fw_tgt_xcb_count = mcp->mb[2];
3014                ha->cur_fw_xcb_count = mcp->mb[3];
3015                ha->orig_fw_xcb_count = mcp->mb[6];
3016                ha->cur_fw_iocb_count = mcp->mb[7];
3017                ha->orig_fw_iocb_count = mcp->mb[10];
3018                if (ha->flags.npiv_supported)
3019                        ha->max_npiv_vports = mcp->mb[11];
3020                if (IS_QLA81XX(ha) || IS_QLA83XX(ha) || IS_QLA27XX(ha) ||
3021                    IS_QLA28XX(ha))
3022                        ha->fw_max_fcf_count = mcp->mb[12];
3023        }
3024
3025        return (rval);
3026}
3027
3028/*
3029 * qla2x00_get_fcal_position_map
3030 *      Get FCAL (LILP) position map using mailbox command
3031 *
3032 * Input:
3033 *      ha = adapter state pointer.
3034 *      pos_map = buffer pointer (can be NULL).
3035 *
3036 * Returns:
3037 *      qla2x00 local function return status code.
3038 *
3039 * Context:
3040 *      Kernel context.
3041 */
3042int
3043qla2x00_get_fcal_position_map(scsi_qla_host_t *vha, char *pos_map)
3044{
3045        int rval;
3046        mbx_cmd_t mc;
3047        mbx_cmd_t *mcp = &mc;
3048        char *pmap;
3049        dma_addr_t pmap_dma;
3050        struct qla_hw_data *ha = vha->hw;
3051
3052        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x107f,
3053            "Entered %s.\n", __func__);
3054
3055        pmap = dma_pool_zalloc(ha->s_dma_pool, GFP_KERNEL, &pmap_dma);
3056        if (pmap  == NULL) {
3057                ql_log(ql_log_warn, vha, 0x1080,
3058                    "Memory alloc failed.\n");
3059                return QLA_MEMORY_ALLOC_FAILED;
3060        }
3061
3062        mcp->mb[0] = MBC_GET_FC_AL_POSITION_MAP;
3063        mcp->mb[2] = MSW(pmap_dma);
3064        mcp->mb[3] = LSW(pmap_dma);
3065        mcp->mb[6] = MSW(MSD(pmap_dma));
3066        mcp->mb[7] = LSW(MSD(pmap_dma));
3067        mcp->out_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_0;
3068        mcp->in_mb = MBX_1|MBX_0;
3069        mcp->buf_size = FCAL_MAP_SIZE;
3070        mcp->flags = MBX_DMA_IN;
3071        mcp->tov = (ha->login_timeout * 2) + (ha->login_timeout / 2);
3072        rval = qla2x00_mailbox_command(vha, mcp);
3073
3074        if (rval == QLA_SUCCESS) {
3075                ql_dbg(ql_dbg_mbx + ql_dbg_buffer, vha, 0x1081,
3076                    "mb0/mb1=%x/%X FC/AL position map size (%x).\n",
3077                    mcp->mb[0], mcp->mb[1], (unsigned)pmap[0]);
3078                ql_dump_buffer(ql_dbg_mbx + ql_dbg_buffer, vha, 0x111d,
3079                    pmap, pmap[0] + 1);
3080
3081                if (pos_map)
3082                        memcpy(pos_map, pmap, FCAL_MAP_SIZE);
3083        }
3084        dma_pool_free(ha->s_dma_pool, pmap, pmap_dma);
3085
3086        if (rval != QLA_SUCCESS) {
3087                ql_dbg(ql_dbg_mbx, vha, 0x1082, "Failed=%x.\n", rval);
3088        } else {
3089                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1083,
3090                    "Done %s.\n", __func__);
3091        }
3092
3093        return rval;
3094}
3095
3096/*
3097 * qla2x00_get_link_status
3098 *
3099 * Input:
3100 *      ha = adapter block pointer.
3101 *      loop_id = device loop ID.
3102 *      ret_buf = pointer to link status return buffer.
3103 *
3104 * Returns:
3105 *      0 = success.
3106 *      BIT_0 = mem alloc error.
3107 *      BIT_1 = mailbox error.
3108 */
3109int
3110qla2x00_get_link_status(scsi_qla_host_t *vha, uint16_t loop_id,
3111    struct link_statistics *stats, dma_addr_t stats_dma)
3112{
3113        int rval;
3114        mbx_cmd_t mc;
3115        mbx_cmd_t *mcp = &mc;
3116        uint32_t *iter = (uint32_t *)stats;
3117        ushort dwords = offsetof(typeof(*stats), link_up_cnt)/sizeof(*iter);
3118        struct qla_hw_data *ha = vha->hw;
3119
3120        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1084,
3121            "Entered %s.\n", __func__);
3122
3123        mcp->mb[0] = MBC_GET_LINK_STATUS;
3124        mcp->mb[2] = MSW(LSD(stats_dma));
3125        mcp->mb[3] = LSW(LSD(stats_dma));
3126        mcp->mb[6] = MSW(MSD(stats_dma));
3127        mcp->mb[7] = LSW(MSD(stats_dma));
3128        mcp->out_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_0;
3129        mcp->in_mb = MBX_0;
3130        if (IS_FWI2_CAPABLE(ha)) {
3131                mcp->mb[1] = loop_id;
3132                mcp->mb[4] = 0;
3133                mcp->mb[10] = 0;
3134                mcp->out_mb |= MBX_10|MBX_4|MBX_1;
3135                mcp->in_mb |= MBX_1;
3136        } else if (HAS_EXTENDED_IDS(ha)) {
3137                mcp->mb[1] = loop_id;
3138                mcp->mb[10] = 0;
3139                mcp->out_mb |= MBX_10|MBX_1;
3140        } else {
3141                mcp->mb[1] = loop_id << 8;
3142                mcp->out_mb |= MBX_1;
3143        }
3144        mcp->tov = MBX_TOV_SECONDS;
3145        mcp->flags = IOCTL_CMD;
3146        rval = qla2x00_mailbox_command(vha, mcp);
3147
3148        if (rval == QLA_SUCCESS) {
3149                if (mcp->mb[0] != MBS_COMMAND_COMPLETE) {
3150                        ql_dbg(ql_dbg_mbx, vha, 0x1085,
3151                            "Failed=%x mb[0]=%x.\n", rval, mcp->mb[0]);
3152                        rval = QLA_FUNCTION_FAILED;
3153                } else {
3154                        /* Re-endianize - firmware data is le32. */
3155                        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1086,
3156                            "Done %s.\n", __func__);
3157                        for ( ; dwords--; iter++)
3158                                le32_to_cpus(iter);
3159                }
3160        } else {
3161                /* Failed. */
3162                ql_dbg(ql_dbg_mbx, vha, 0x1087, "Failed=%x.\n", rval);
3163        }
3164
3165        return rval;
3166}
3167
3168int
3169qla24xx_get_isp_stats(scsi_qla_host_t *vha, struct link_statistics *stats,
3170    dma_addr_t stats_dma, uint16_t options)
3171{
3172        int rval;
3173        mbx_cmd_t mc;
3174        mbx_cmd_t *mcp = &mc;
3175        uint32_t *iter = (uint32_t *)stats;
3176        ushort dwords = sizeof(*stats)/sizeof(*iter);
3177
3178        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1088,
3179            "Entered %s.\n", __func__);
3180
3181        memset(&mc, 0, sizeof(mc));
3182        mc.mb[0] = MBC_GET_LINK_PRIV_STATS;
3183        mc.mb[2] = MSW(LSD(stats_dma));
3184        mc.mb[3] = LSW(LSD(stats_dma));
3185        mc.mb[6] = MSW(MSD(stats_dma));
3186        mc.mb[7] = LSW(MSD(stats_dma));
3187        mc.mb[8] = dwords;
3188        mc.mb[9] = vha->vp_idx;
3189        mc.mb[10] = options;
3190
3191        rval = qla24xx_send_mb_cmd(vha, &mc);
3192
3193        if (rval == QLA_SUCCESS) {
3194                if (mcp->mb[0] != MBS_COMMAND_COMPLETE) {
3195                        ql_dbg(ql_dbg_mbx, vha, 0x1089,
3196                            "Failed mb[0]=%x.\n", mcp->mb[0]);
3197                        rval = QLA_FUNCTION_FAILED;
3198                } else {
3199                        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x108a,
3200                            "Done %s.\n", __func__);
3201                        /* Re-endianize - firmware data is le32. */
3202                        for ( ; dwords--; iter++)
3203                                le32_to_cpus(iter);
3204                }
3205        } else {
3206                /* Failed. */
3207                ql_dbg(ql_dbg_mbx, vha, 0x108b, "Failed=%x.\n", rval);
3208        }
3209
3210        return rval;
3211}
3212
3213int
3214qla24xx_abort_command(srb_t *sp)
3215{
3216        int             rval;
3217        unsigned long   flags = 0;
3218
3219        struct abort_entry_24xx *abt;
3220        dma_addr_t      abt_dma;
3221        uint32_t        handle;
3222        fc_port_t       *fcport = sp->fcport;
3223        struct scsi_qla_host *vha = fcport->vha;
3224        struct qla_hw_data *ha = vha->hw;
3225        struct req_que *req = vha->req;
3226        struct qla_qpair *qpair = sp->qpair;
3227
3228        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x108c,
3229            "Entered %s.\n", __func__);
3230
3231        if (sp->qpair)
3232                req = sp->qpair->req;
3233        else
3234                return QLA_FUNCTION_FAILED;
3235
3236        if (ql2xasynctmfenable)
3237                return qla24xx_async_abort_command(sp);
3238
3239        spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3240        for (handle = 1; handle < req->num_outstanding_cmds; handle++) {
3241                if (req->outstanding_cmds[handle] == sp)
3242                        break;
3243        }
3244        spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3245        if (handle == req->num_outstanding_cmds) {
3246                /* Command not found. */
3247                return QLA_FUNCTION_FAILED;
3248        }
3249
3250        abt = dma_pool_zalloc(ha->s_dma_pool, GFP_KERNEL, &abt_dma);
3251        if (abt == NULL) {
3252                ql_log(ql_log_warn, vha, 0x108d,
3253                    "Failed to allocate abort IOCB.\n");
3254                return QLA_MEMORY_ALLOC_FAILED;
3255        }
3256
3257        abt->entry_type = ABORT_IOCB_TYPE;
3258        abt->entry_count = 1;
3259        abt->handle = make_handle(req->id, abt->handle);
3260        abt->nport_handle = cpu_to_le16(fcport->loop_id);
3261        abt->handle_to_abort = make_handle(req->id, handle);
3262        abt->port_id[0] = fcport->d_id.b.al_pa;
3263        abt->port_id[1] = fcport->d_id.b.area;
3264        abt->port_id[2] = fcport->d_id.b.domain;
3265        abt->vp_index = fcport->vha->vp_idx;
3266
3267        abt->req_que_no = cpu_to_le16(req->id);
3268        /* Need to pass original sp */
3269        qla_nvme_abort_set_option(abt, sp);
3270
3271        rval = qla2x00_issue_iocb(vha, abt, abt_dma, 0);
3272        if (rval != QLA_SUCCESS) {
3273                ql_dbg(ql_dbg_mbx, vha, 0x108e,
3274                    "Failed to issue IOCB (%x).\n", rval);
3275        } else if (abt->entry_status != 0) {
3276                ql_dbg(ql_dbg_mbx, vha, 0x108f,
3277                    "Failed to complete IOCB -- error status (%x).\n",
3278                    abt->entry_status);
3279                rval = QLA_FUNCTION_FAILED;
3280        } else if (abt->nport_handle != cpu_to_le16(0)) {
3281                ql_dbg(ql_dbg_mbx, vha, 0x1090,
3282                    "Failed to complete IOCB -- completion status (%x).\n",
3283                    le16_to_cpu(abt->nport_handle));
3284                if (abt->nport_handle == cpu_to_le16(CS_IOCB_ERROR))
3285                        rval = QLA_FUNCTION_PARAMETER_ERROR;
3286                else
3287                        rval = QLA_FUNCTION_FAILED;
3288        } else {
3289                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1091,
3290                    "Done %s.\n", __func__);
3291        }
3292        if (rval == QLA_SUCCESS)
3293                qla_nvme_abort_process_comp_status(abt, sp);
3294
3295        qla_wait_nvme_release_cmd_kref(sp);
3296
3297        dma_pool_free(ha->s_dma_pool, abt, abt_dma);
3298
3299        return rval;
3300}
3301
3302struct tsk_mgmt_cmd {
3303        union {
3304                struct tsk_mgmt_entry tsk;
3305                struct sts_entry_24xx sts;
3306        } p;
3307};
3308
3309static int
3310__qla24xx_issue_tmf(char *name, uint32_t type, struct fc_port *fcport,
3311    uint64_t l, int tag)
3312{
3313        int             rval, rval2;
3314        struct tsk_mgmt_cmd *tsk;
3315        struct sts_entry_24xx *sts;
3316        dma_addr_t      tsk_dma;
3317        scsi_qla_host_t *vha;
3318        struct qla_hw_data *ha;
3319        struct req_que *req;
3320        struct qla_qpair *qpair;
3321
3322        vha = fcport->vha;
3323        ha = vha->hw;
3324        req = vha->req;
3325
3326        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1092,
3327            "Entered %s.\n", __func__);
3328
3329        if (vha->vp_idx && vha->qpair) {
3330                /* NPIV port */
3331                qpair = vha->qpair;
3332                req = qpair->req;
3333        }
3334
3335        tsk = dma_pool_zalloc(ha->s_dma_pool, GFP_KERNEL, &tsk_dma);
3336        if (tsk == NULL) {
3337                ql_log(ql_log_warn, vha, 0x1093,
3338                    "Failed to allocate task management IOCB.\n");
3339                return QLA_MEMORY_ALLOC_FAILED;
3340        }
3341
3342        tsk->p.tsk.entry_type = TSK_MGMT_IOCB_TYPE;
3343        tsk->p.tsk.entry_count = 1;
3344        tsk->p.tsk.handle = make_handle(req->id, tsk->p.tsk.handle);
3345        tsk->p.tsk.nport_handle = cpu_to_le16(fcport->loop_id);
3346        tsk->p.tsk.timeout = cpu_to_le16(ha->r_a_tov / 10 * 2);
3347        tsk->p.tsk.control_flags = cpu_to_le32(type);
3348        tsk->p.tsk.port_id[0] = fcport->d_id.b.al_pa;
3349        tsk->p.tsk.port_id[1] = fcport->d_id.b.area;
3350        tsk->p.tsk.port_id[2] = fcport->d_id.b.domain;
3351        tsk->p.tsk.vp_index = fcport->vha->vp_idx;
3352        if (type == TCF_LUN_RESET) {
3353                int_to_scsilun(l, &tsk->p.tsk.lun);
3354                host_to_fcp_swap((uint8_t *)&tsk->p.tsk.lun,
3355                    sizeof(tsk->p.tsk.lun));
3356        }
3357
3358        sts = &tsk->p.sts;
3359        rval = qla2x00_issue_iocb(vha, tsk, tsk_dma, 0);
3360        if (rval != QLA_SUCCESS) {
3361                ql_dbg(ql_dbg_mbx, vha, 0x1094,
3362                    "Failed to issue %s reset IOCB (%x).\n", name, rval);
3363        } else if (sts->entry_status != 0) {
3364                ql_dbg(ql_dbg_mbx, vha, 0x1095,
3365                    "Failed to complete IOCB -- error status (%x).\n",
3366                    sts->entry_status);
3367                rval = QLA_FUNCTION_FAILED;
3368        } else if (sts->comp_status != cpu_to_le16(CS_COMPLETE)) {
3369                ql_dbg(ql_dbg_mbx, vha, 0x1096,
3370                    "Failed to complete IOCB -- completion status (%x).\n",
3371                    le16_to_cpu(sts->comp_status));
3372                rval = QLA_FUNCTION_FAILED;
3373        } else if (le16_to_cpu(sts->scsi_status) &
3374            SS_RESPONSE_INFO_LEN_VALID) {
3375                if (le32_to_cpu(sts->rsp_data_len) < 4) {
3376                        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1097,
3377                            "Ignoring inconsistent data length -- not enough "
3378                            "response info (%d).\n",
3379                            le32_to_cpu(sts->rsp_data_len));
3380                } else if (sts->data[3]) {
3381                        ql_dbg(ql_dbg_mbx, vha, 0x1098,
3382                            "Failed to complete IOCB -- response (%x).\n",
3383                            sts->data[3]);
3384                        rval = QLA_FUNCTION_FAILED;
3385                }
3386        }
3387
3388        /* Issue marker IOCB. */
3389        rval2 = qla2x00_marker(vha, ha->base_qpair, fcport->loop_id, l,
3390            type == TCF_LUN_RESET ? MK_SYNC_ID_LUN : MK_SYNC_ID);
3391        if (rval2 != QLA_SUCCESS) {
3392                ql_dbg(ql_dbg_mbx, vha, 0x1099,
3393                    "Failed to issue marker IOCB (%x).\n", rval2);
3394        } else {
3395                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x109a,
3396                    "Done %s.\n", __func__);
3397        }
3398
3399        dma_pool_free(ha->s_dma_pool, tsk, tsk_dma);
3400
3401        return rval;
3402}
3403
3404int
3405qla24xx_abort_target(struct fc_port *fcport, uint64_t l, int tag)
3406{
3407        struct qla_hw_data *ha = fcport->vha->hw;
3408
3409        if ((ql2xasynctmfenable) && IS_FWI2_CAPABLE(ha))
3410                return qla2x00_async_tm_cmd(fcport, TCF_TARGET_RESET, l, tag);
3411
3412        return __qla24xx_issue_tmf("Target", TCF_TARGET_RESET, fcport, l, tag);
3413}
3414
3415int
3416qla24xx_lun_reset(struct fc_port *fcport, uint64_t l, int tag)
3417{
3418        struct qla_hw_data *ha = fcport->vha->hw;
3419
3420        if ((ql2xasynctmfenable) && IS_FWI2_CAPABLE(ha))
3421                return qla2x00_async_tm_cmd(fcport, TCF_LUN_RESET, l, tag);
3422
3423        return __qla24xx_issue_tmf("Lun", TCF_LUN_RESET, fcport, l, tag);
3424}
3425
3426int
3427qla2x00_system_error(scsi_qla_host_t *vha)
3428{
3429        int rval;
3430        mbx_cmd_t mc;
3431        mbx_cmd_t *mcp = &mc;
3432        struct qla_hw_data *ha = vha->hw;
3433
3434        if (!IS_QLA23XX(ha) && !IS_FWI2_CAPABLE(ha))
3435                return QLA_FUNCTION_FAILED;
3436
3437        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x109b,
3438            "Entered %s.\n", __func__);
3439
3440        mcp->mb[0] = MBC_GEN_SYSTEM_ERROR;
3441        mcp->out_mb = MBX_0;
3442        mcp->in_mb = MBX_0;
3443        mcp->tov = 5;
3444        mcp->flags = 0;
3445        rval = qla2x00_mailbox_command(vha, mcp);
3446
3447        if (rval != QLA_SUCCESS) {
3448                ql_dbg(ql_dbg_mbx, vha, 0x109c, "Failed=%x.\n", rval);
3449        } else {
3450                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x109d,
3451                    "Done %s.\n", __func__);
3452        }
3453
3454        return rval;
3455}
3456
3457int
3458qla2x00_write_serdes_word(scsi_qla_host_t *vha, uint16_t addr, uint16_t data)
3459{
3460        int rval;
3461        mbx_cmd_t mc;
3462        mbx_cmd_t *mcp = &mc;
3463
3464        if (!IS_QLA25XX(vha->hw) && !IS_QLA2031(vha->hw) &&
3465            !IS_QLA27XX(vha->hw) && !IS_QLA28XX(vha->hw))
3466                return QLA_FUNCTION_FAILED;
3467
3468        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1182,
3469            "Entered %s.\n", __func__);
3470
3471        mcp->mb[0] = MBC_WRITE_SERDES;
3472        mcp->mb[1] = addr;
3473        if (IS_QLA2031(vha->hw))
3474                mcp->mb[2] = data & 0xff;
3475        else
3476                mcp->mb[2] = data;
3477
3478        mcp->mb[3] = 0;
3479        mcp->out_mb = MBX_3|MBX_2|MBX_1|MBX_0;
3480        mcp->in_mb = MBX_0;
3481        mcp->tov = MBX_TOV_SECONDS;
3482        mcp->flags = 0;
3483        rval = qla2x00_mailbox_command(vha, mcp);
3484
3485        if (rval != QLA_SUCCESS) {
3486                ql_dbg(ql_dbg_mbx, vha, 0x1183,
3487                    "Failed=%x mb[0]=%x.\n", rval, mcp->mb[0]);
3488        } else {
3489                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1184,
3490                    "Done %s.\n", __func__);
3491        }
3492
3493        return rval;
3494}
3495
3496int
3497qla2x00_read_serdes_word(scsi_qla_host_t *vha, uint16_t addr, uint16_t *data)
3498{
3499        int rval;
3500        mbx_cmd_t mc;
3501        mbx_cmd_t *mcp = &mc;
3502
3503        if (!IS_QLA25XX(vha->hw) && !IS_QLA2031(vha->hw) &&
3504            !IS_QLA27XX(vha->hw) && !IS_QLA28XX(vha->hw))
3505                return QLA_FUNCTION_FAILED;
3506
3507        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1185,
3508            "Entered %s.\n", __func__);
3509
3510        mcp->mb[0] = MBC_READ_SERDES;
3511        mcp->mb[1] = addr;
3512        mcp->mb[3] = 0;
3513        mcp->out_mb = MBX_3|MBX_1|MBX_0;
3514        mcp->in_mb = MBX_1|MBX_0;
3515        mcp->tov = MBX_TOV_SECONDS;
3516        mcp->flags = 0;
3517        rval = qla2x00_mailbox_command(vha, mcp);
3518
3519        if (IS_QLA2031(vha->hw))
3520                *data = mcp->mb[1] & 0xff;
3521        else
3522                *data = mcp->mb[1];
3523
3524        if (rval != QLA_SUCCESS) {
3525                ql_dbg(ql_dbg_mbx, vha, 0x1186,
3526                    "Failed=%x mb[0]=%x.\n", rval, mcp->mb[0]);
3527        } else {
3528                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1187,
3529                    "Done %s.\n", __func__);
3530        }
3531
3532        return rval;
3533}
3534
3535int
3536qla8044_write_serdes_word(scsi_qla_host_t *vha, uint32_t addr, uint32_t data)
3537{
3538        int rval;
3539        mbx_cmd_t mc;
3540        mbx_cmd_t *mcp = &mc;
3541
3542        if (!IS_QLA8044(vha->hw))
3543                return QLA_FUNCTION_FAILED;
3544
3545        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x11a0,
3546            "Entered %s.\n", __func__);
3547
3548        mcp->mb[0] = MBC_SET_GET_ETH_SERDES_REG;
3549        mcp->mb[1] = HCS_WRITE_SERDES;
3550        mcp->mb[3] = LSW(addr);
3551        mcp->mb[4] = MSW(addr);
3552        mcp->mb[5] = LSW(data);
3553        mcp->mb[6] = MSW(data);
3554        mcp->out_mb = MBX_6|MBX_5|MBX_4|MBX_3|MBX_1|MBX_0;
3555        mcp->in_mb = MBX_0;
3556        mcp->tov = MBX_TOV_SECONDS;
3557        mcp->flags = 0;
3558        rval = qla2x00_mailbox_command(vha, mcp);
3559
3560        if (rval != QLA_SUCCESS) {
3561                ql_dbg(ql_dbg_mbx, vha, 0x11a1,
3562                    "Failed=%x mb[0]=%x.\n", rval, mcp->mb[0]);
3563        } else {
3564                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1188,
3565                    "Done %s.\n", __func__);
3566        }
3567
3568        return rval;
3569}
3570
3571int
3572qla8044_read_serdes_word(scsi_qla_host_t *vha, uint32_t addr, uint32_t *data)
3573{
3574        int rval;
3575        mbx_cmd_t mc;
3576        mbx_cmd_t *mcp = &mc;
3577
3578        if (!IS_QLA8044(vha->hw))
3579                return QLA_FUNCTION_FAILED;
3580
3581        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x1189,
3582            "Entered %s.\n", __func__);
3583
3584        mcp->mb[0] = MBC_SET_GET_ETH_SERDES_REG;
3585        mcp->mb[1] = HCS_READ_SERDES;
3586        mcp->mb[3] = LSW(addr);
3587        mcp->mb[4] = MSW(addr);
3588        mcp->out_mb = MBX_4|MBX_3|MBX_1|MBX_0;
3589        mcp->in_mb = MBX_2|MBX_1|MBX_0;
3590        mcp->tov = MBX_TOV_SECONDS;
3591        mcp->flags = 0;
3592        rval = qla2x00_mailbox_command(vha, mcp);
3593
3594        *data = mcp->mb[2] << 16 | mcp->mb[1];
3595
3596        if (rval != QLA_SUCCESS) {
3597                ql_dbg(ql_dbg_mbx, vha, 0x118a,
3598                    "Failed=%x mb[0]=%x.\n", rval, mcp->mb[0]);
3599        } else {
3600                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x118b,
3601                    "Done %s.\n", __func__);
3602        }
3603
3604        return rval;
3605}
3606
3607/**
3608 * qla2x00_set_serdes_params() -
3609 * @vha: HA context
3610 * @sw_em_1g: serial link options
3611 * @sw_em_2g: serial link options
3612 * @sw_em_4g: serial link options
3613 *
3614 * Returns
3615 */
3616int
3617qla2x00_set_serdes_params(scsi_qla_host_t *vha, uint16_t sw_em_1g,
3618    uint16_t sw_em_2g, uint16_t sw_em_4g)
3619{
3620        int rval;
3621        mbx_cmd_t mc;
3622        mbx_cmd_t *mcp = &mc;
3623
3624        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x109e,
3625            "Entered %s.\n", __func__);
3626
3627        mcp->mb[0] = MBC_SERDES_PARAMS;
3628        mcp->mb[1] = BIT_0;
3629        mcp->mb[2] = sw_em_1g | BIT_15;
3630        mcp->mb[3] = sw_em_2g | BIT_15;
3631        mcp->mb[4] = sw_em_4g | BIT_15;
3632        mcp->out_mb = MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
3633        mcp->in_mb = MBX_0;
3634        mcp->tov = MBX_TOV_SECONDS;
3635        mcp->flags = 0;
3636        rval = qla2x00_mailbox_command(vha, mcp);
3637
3638        if (rval != QLA_SUCCESS) {
3639                /*EMPTY*/
3640                ql_dbg(ql_dbg_mbx, vha, 0x109f,
3641                    "Failed=%x mb[0]=%x.\n", rval, mcp->mb[0]);
3642        } else {
3643                /*EMPTY*/
3644                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x10a0,
3645                    "Done %s.\n", __func__);
3646        }
3647
3648        return rval;
3649}
3650
3651int
3652qla2x00_stop_firmware(scsi_qla_host_t *vha)
3653{
3654        int rval;
3655        mbx_cmd_t mc;
3656        mbx_cmd_t *mcp = &mc;
3657
3658        if (!IS_FWI2_CAPABLE(vha->hw))
3659                return QLA_FUNCTION_FAILED;
3660
3661        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x10a1,
3662            "Entered %s.\n", __func__);
3663
3664        mcp->mb[0] = MBC_STOP_FIRMWARE;
3665        mcp->mb[1] = 0;
3666        mcp->out_mb = MBX_1|MBX_0;
3667        mcp->in_mb = MBX_0;
3668        mcp->tov = 5;
3669        mcp->flags = 0;
3670        rval = qla2x00_mailbox_command(vha, mcp);
3671
3672        if (rval != QLA_SUCCESS) {
3673                ql_dbg(ql_dbg_mbx, vha, 0x10a2, "Failed=%x.\n", rval);
3674                if (mcp->mb[0] == MBS_INVALID_COMMAND)
3675                        rval = QLA_INVALID_COMMAND;
3676        } else {
3677                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x10a3,
3678                    "Done %s.\n", __func__);
3679        }
3680
3681        return rval;
3682}
3683
3684int
3685qla2x00_enable_eft_trace(scsi_qla_host_t *vha, dma_addr_t eft_dma,
3686    uint16_t buffers)
3687{
3688        int rval;
3689        mbx_cmd_t mc;
3690        mbx_cmd_t *mcp = &mc;
3691
3692        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x10a4,
3693            "Entered %s.\n", __func__);
3694
3695        if (!IS_FWI2_CAPABLE(vha->hw))
3696                return QLA_FUNCTION_FAILED;
3697
3698        if (unlikely(pci_channel_offline(vha->hw->pdev)))
3699                return QLA_FUNCTION_FAILED;
3700
3701        mcp->mb[0] = MBC_TRACE_CONTROL;
3702        mcp->mb[1] = TC_EFT_ENABLE;
3703        mcp->mb[2] = LSW(eft_dma);
3704        mcp->mb[3] = MSW(eft_dma);
3705        mcp->mb[4] = LSW(MSD(eft_dma));
3706        mcp->mb[5] = MSW(MSD(eft_dma));
3707        mcp->mb[6] = buffers;
3708        mcp->mb[7] = TC_AEN_DISABLE;
3709        mcp->out_mb = MBX_7|MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
3710        mcp->in_mb = MBX_1|MBX_0;
3711        mcp->tov = MBX_TOV_SECONDS;
3712        mcp->flags = 0;
3713        rval = qla2x00_mailbox_command(vha, mcp);
3714        if (rval != QLA_SUCCESS) {
3715                ql_dbg(ql_dbg_mbx, vha, 0x10a5,
3716                    "Failed=%x mb[0]=%x mb[1]=%x.\n",
3717                    rval, mcp->mb[0], mcp->mb[1]);
3718        } else {
3719                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x10a6,
3720                    "Done %s.\n", __func__);
3721        }
3722
3723        return rval;
3724}
3725
3726int
3727qla2x00_disable_eft_trace(scsi_qla_host_t *vha)
3728{
3729        int rval;
3730        mbx_cmd_t mc;
3731        mbx_cmd_t *mcp = &mc;
3732
3733        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x10a7,
3734            "Entered %s.\n", __func__);
3735
3736        if (!IS_FWI2_CAPABLE(vha->hw))
3737                return QLA_FUNCTION_FAILED;
3738
3739        if (unlikely(pci_channel_offline(vha->hw->pdev)))
3740                return QLA_FUNCTION_FAILED;
3741
3742        mcp->mb[0] = MBC_TRACE_CONTROL;
3743        mcp->mb[1] = TC_EFT_DISABLE;
3744        mcp->out_mb = MBX_1|MBX_0;
3745        mcp->in_mb = MBX_1|MBX_0;
3746        mcp->tov = MBX_TOV_SECONDS;
3747        mcp->flags = 0;
3748        rval = qla2x00_mailbox_command(vha, mcp);
3749        if (rval != QLA_SUCCESS) {
3750                ql_dbg(ql_dbg_mbx, vha, 0x10a8,
3751                    "Failed=%x mb[0]=%x mb[1]=%x.\n",
3752                    rval, mcp->mb[0], mcp->mb[1]);
3753        } else {
3754                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x10a9,
3755                    "Done %s.\n", __func__);
3756        }
3757
3758        return rval;
3759}
3760
3761int
3762qla2x00_enable_fce_trace(scsi_qla_host_t *vha, dma_addr_t fce_dma,
3763    uint16_t buffers, uint16_t *mb, uint32_t *dwords)
3764{
3765        int rval;
3766        mbx_cmd_t mc;
3767        mbx_cmd_t *mcp = &mc;
3768
3769        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x10aa,
3770            "Entered %s.\n", __func__);
3771
3772        if (!IS_QLA25XX(vha->hw) && !IS_QLA81XX(vha->hw) &&
3773            !IS_QLA83XX(vha->hw) && !IS_QLA27XX(vha->hw) &&
3774            !IS_QLA28XX(vha->hw))
3775                return QLA_FUNCTION_FAILED;
3776
3777        if (unlikely(pci_channel_offline(vha->hw->pdev)))
3778                return QLA_FUNCTION_FAILED;
3779
3780        mcp->mb[0] = MBC_TRACE_CONTROL;
3781        mcp->mb[1] = TC_FCE_ENABLE;
3782        mcp->mb[2] = LSW(fce_dma);
3783        mcp->mb[3] = MSW(fce_dma);
3784        mcp->mb[4] = LSW(MSD(fce_dma));
3785        mcp->mb[5] = MSW(MSD(fce_dma));
3786        mcp->mb[6] = buffers;
3787        mcp->mb[7] = TC_AEN_DISABLE;
3788        mcp->mb[8] = 0;
3789        mcp->mb[9] = TC_FCE_DEFAULT_RX_SIZE;
3790        mcp->mb[10] = TC_FCE_DEFAULT_TX_SIZE;
3791        mcp->out_mb = MBX_10|MBX_9|MBX_8|MBX_7|MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|
3792            MBX_1|MBX_0;
3793        mcp->in_mb = MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
3794        mcp->tov = MBX_TOV_SECONDS;
3795        mcp->flags = 0;
3796        rval = qla2x00_mailbox_command(vha, mcp);
3797        if (rval != QLA_SUCCESS) {
3798                ql_dbg(ql_dbg_mbx, vha, 0x10ab,
3799                    "Failed=%x mb[0]=%x mb[1]=%x.\n",
3800                    rval, mcp->mb[0], mcp->mb[1]);
3801        } else {
3802                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x10ac,
3803                    "Done %s.\n", __func__);
3804
3805                if (mb)
3806                        memcpy(mb, mcp->mb, 8 * sizeof(*mb));
3807                if (dwords)
3808                        *dwords = buffers;
3809        }
3810
3811        return rval;
3812}
3813
3814int
3815qla2x00_disable_fce_trace(scsi_qla_host_t *vha, uint64_t *wr, uint64_t *rd)
3816{
3817        int rval;
3818        mbx_cmd_t mc;
3819        mbx_cmd_t *mcp = &mc;
3820
3821        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x10ad,
3822            "Entered %s.\n", __func__);
3823
3824        if (!IS_FWI2_CAPABLE(vha->hw))
3825                return QLA_FUNCTION_FAILED;
3826
3827        if (unlikely(pci_channel_offline(vha->hw->pdev)))
3828                return QLA_FUNCTION_FAILED;
3829
3830        mcp->mb[0] = MBC_TRACE_CONTROL;
3831        mcp->mb[1] = TC_FCE_DISABLE;
3832        mcp->mb[2] = TC_FCE_DISABLE_TRACE;
3833        mcp->out_mb = MBX_2|MBX_1|MBX_0;
3834        mcp->in_mb = MBX_9|MBX_8|MBX_7|MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|
3835            MBX_1|MBX_0;
3836        mcp->tov = MBX_TOV_SECONDS;
3837        mcp->flags = 0;
3838        rval = qla2x00_mailbox_command(vha, mcp);
3839        if (rval != QLA_SUCCESS) {
3840                ql_dbg(ql_dbg_mbx, vha, 0x10ae,
3841                    "Failed=%x mb[0]=%x mb[1]=%x.\n",
3842                    rval, mcp->mb[0], mcp->mb[1]);
3843        } else {
3844                ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x10af,
3845                    "Done %s.\n", __func__);
3846
3847                if (wr)
3848                        *wr = (uint64_t) mcp->mb[5] << 48 |
3849                            (uint64_t) mcp->mb[4] << 32 |
3850                            (uint64_t) mcp->mb[3] << 16 |
3851                            (uint64_t) mcp->mb[2];
3852                if (rd)
3853                        *rd = (uint64_t) mcp->mb[9] << 48 |
3854                            (uint64_t) mcp->mb[8] << 32 |
3855                            (uint64_t) mcp->mb[7] << 16 |
3856                            (uint64_t) mcp->mb[6];
3857        }
3858
3859        return rval;
3860}
3861
3862int
3863qla2x00_get_idma_speed(scsi_qla_host_t *vha, uint16_t loop_id,
3864        uint16_t *port_speed, uint16_t *mb)
3865{
3866        int rval;
3867        mbx_cmd_t mc;
3868        mbx_cmd_t *mcp = &mc;
3869
3870        ql_dbg(ql_dbg_mbx + ql_dbg_verbose, vha, 0x10b0,
3871            "Entered %s.\n", __func__);
3872
3873        if (!IS_IIDMA_CAPABLE(vha->hw))
3874                return QLA_FUNCTION_FAILED;
3875
3876        mcp->mb[0] = MBC_PORT_PARAMS;
3877        mcp->mb[1] = loop_id;
3878        mcp->mb[2] = mcp->mb[3] = 0;
3879        mcp->mb[9] = vha->vp_idx;
3880        mcp->out_mb = MBX_9|MBX_3|MBX_2|MBX_1|MBX_0;
3881        mcp->in_mb =