linux/drivers/scsi/qla2xxx/qla_mbx.c
<<
>>
Prefs
   1/*
   2 * QLogic Fibre Channel HBA Driver
   3 * Copyright (c)  2003-2005 QLogic Corporation
   4 *
   5 * See LICENSE.qla2xxx for copyright and licensing details.
   6 */
   7#include "qla_def.h"
   8
   9#include <linux/delay.h>
  10
  11static void
  12qla2x00_mbx_sem_timeout(unsigned long data)
  13{
  14        struct semaphore        *sem_ptr = (struct semaphore *)data;
  15
  16        DEBUG11(printk("qla2x00_sem_timeout: entered.\n"));
  17
  18        if (sem_ptr != NULL) {
  19                up(sem_ptr);
  20        }
  21
  22        DEBUG11(printk("qla2x00_mbx_sem_timeout: exiting.\n"));
  23}
  24
  25/*
  26 * qla2x00_mailbox_command
  27 *      Issue mailbox command and waits for completion.
  28 *
  29 * Input:
  30 *      ha = adapter block pointer.
  31 *      mcp = driver internal mbx struct pointer.
  32 *
  33 * Output:
  34 *      mb[MAX_MAILBOX_REGISTER_COUNT] = returned mailbox data.
  35 *
  36 * Returns:
  37 *      0 : QLA_SUCCESS = cmd performed success
  38 *      1 : QLA_FUNCTION_FAILED   (error encountered)
  39 *      6 : QLA_FUNCTION_TIMEOUT (timeout condition encountered)
  40 *
  41 * Context:
  42 *      Kernel context.
  43 */
  44static int
  45qla2x00_mailbox_command(scsi_qla_host_t *pvha, mbx_cmd_t *mcp)
  46{
  47        int             rval;
  48        unsigned long    flags = 0;
  49        device_reg_t __iomem *reg;
  50        struct timer_list       tmp_intr_timer;
  51        uint8_t         abort_active;
  52        uint8_t         io_lock_on;
  53        uint16_t        command;
  54        uint16_t        *iptr;
  55        uint16_t __iomem *optr;
  56        uint32_t        cnt;
  57        uint32_t        mboxes;
  58        unsigned long   wait_time;
  59        scsi_qla_host_t *ha = to_qla_parent(pvha);
  60
  61        reg = ha->iobase;
  62        io_lock_on = ha->flags.init_done;
  63
  64        rval = QLA_SUCCESS;
  65        abort_active = test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
  66
  67        DEBUG11(printk("%s(%ld): entered.\n", __func__, pvha->host_no));
  68
  69        /*
  70         * Wait for active mailbox commands to finish by waiting at most tov
  71         * seconds. This is to serialize actual issuing of mailbox cmds during
  72         * non ISP abort time.
  73         */
  74        if (!abort_active) {
  75                if (qla2x00_down_timeout(&ha->mbx_cmd_sem, mcp->tov * HZ)) {
  76                        /* Timeout occurred. Return error. */
  77                        DEBUG2_3_11(printk("%s(%ld): cmd access timeout. "
  78                            "Exiting.\n", __func__, ha->host_no));
  79                        return QLA_FUNCTION_TIMEOUT;
  80                }
  81        }
  82
  83        ha->flags.mbox_busy = 1;
  84        /* Save mailbox command for debug */
  85        ha->mcp = mcp;
  86
  87        DEBUG11(printk("scsi(%ld): prepare to issue mbox cmd=0x%x.\n",
  88            ha->host_no, mcp->mb[0]));
  89
  90        spin_lock_irqsave(&ha->hardware_lock, flags);
  91
  92        /* Load mailbox registers. */
  93        if (IS_FWI2_CAPABLE(ha))
  94                optr = (uint16_t __iomem *)&reg->isp24.mailbox0;
  95        else
  96                optr = (uint16_t __iomem *)MAILBOX_REG(ha, &reg->isp, 0);
  97
  98        iptr = mcp->mb;
  99        command = mcp->mb[0];
 100        mboxes = mcp->out_mb;
 101
 102        for (cnt = 0; cnt < ha->mbx_count; cnt++) {
 103                if (IS_QLA2200(ha) && cnt == 8)
 104                        optr =
 105                            (uint16_t __iomem *)MAILBOX_REG(ha, &reg->isp, 8);
 106                if (mboxes & BIT_0)
 107                        WRT_REG_WORD(optr, *iptr);
 108
 109                mboxes >>= 1;
 110                optr++;
 111                iptr++;
 112        }
 113
 114#if defined(QL_DEBUG_LEVEL_1)
 115        printk("%s(%ld): Loaded MBX registers (displayed in bytes) = \n",
 116            __func__, ha->host_no);
 117        qla2x00_dump_buffer((uint8_t *)mcp->mb, 16);
 118        printk("\n");
 119        qla2x00_dump_buffer(((uint8_t *)mcp->mb + 0x10), 16);
 120        printk("\n");
 121        qla2x00_dump_buffer(((uint8_t *)mcp->mb + 0x20), 8);
 122        printk("\n");
 123        printk("%s(%ld): I/O address = %p.\n", __func__, ha->host_no, optr);
 124        qla2x00_dump_regs(ha);
 125#endif
 126
 127        /* Issue set host interrupt command to send cmd out. */
 128        ha->flags.mbox_int = 0;
 129        clear_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
 130
 131        /* Unlock mbx registers and wait for interrupt */
 132        DEBUG11(printk("%s(%ld): going to unlock irq & waiting for interrupt. "
 133            "jiffies=%lx.\n", __func__, ha->host_no, jiffies));
 134
 135        /* Wait for mbx cmd completion until timeout */
 136
 137        if (!abort_active && io_lock_on) {
 138                /* sleep on completion semaphore */
 139                DEBUG11(printk("%s(%ld): INTERRUPT MODE. Initializing timer.\n",
 140                    __func__, ha->host_no));
 141
 142                init_timer(&tmp_intr_timer);
 143                tmp_intr_timer.data = (unsigned long)&ha->mbx_intr_sem;
 144                tmp_intr_timer.expires = jiffies + mcp->tov * HZ;
 145                tmp_intr_timer.function =
 146                    (void (*)(unsigned long))qla2x00_mbx_sem_timeout;
 147
 148                DEBUG11(printk("%s(%ld): Adding timer.\n", __func__,
 149                    ha->host_no));
 150                add_timer(&tmp_intr_timer);
 151
 152                DEBUG11(printk("%s(%ld): going to unlock & sleep. "
 153                    "time=0x%lx.\n", __func__, ha->host_no, jiffies));
 154
 155                set_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags);
 156
 157                if (IS_FWI2_CAPABLE(ha))
 158                        WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_SET_HOST_INT);
 159                else
 160                        WRT_REG_WORD(&reg->isp.hccr, HCCR_SET_HOST_INT);
 161                spin_unlock_irqrestore(&ha->hardware_lock, flags);
 162
 163                /* Wait for either the timer to expire
 164                 * or the mbox completion interrupt
 165                 */
 166                down(&ha->mbx_intr_sem);
 167
 168                DEBUG11(printk("%s(%ld): waking up. time=0x%lx\n", __func__,
 169                    ha->host_no, jiffies));
 170                clear_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags);
 171
 172                /* delete the timer */
 173                del_timer(&tmp_intr_timer);
 174        } else {
 175                DEBUG3_11(printk("%s(%ld): cmd=%x POLLING MODE.\n", __func__,
 176                    ha->host_no, command));
 177
 178                if (IS_FWI2_CAPABLE(ha))
 179                        WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_SET_HOST_INT);
 180                else
 181                        WRT_REG_WORD(&reg->isp.hccr, HCCR_SET_HOST_INT);
 182                spin_unlock_irqrestore(&ha->hardware_lock, flags);
 183
 184                wait_time = jiffies + mcp->tov * HZ; /* wait at most tov secs */
 185                while (!ha->flags.mbox_int) {
 186                        if (time_after(jiffies, wait_time))
 187                                break;
 188
 189                        /* Check for pending interrupts. */
 190                        qla2x00_poll(ha);
 191
 192                        if (command != MBC_LOAD_RISC_RAM_EXTENDED &&
 193                            !ha->flags.mbox_int)
 194                                msleep(10);
 195                } /* while */
 196        }
 197
 198        /* Check whether we timed out */
 199        if (ha->flags.mbox_int) {
 200                uint16_t *iptr2;
 201
 202                DEBUG3_11(printk("%s(%ld): cmd %x completed.\n", __func__,
 203                    ha->host_no, command));
 204
 205                /* Got interrupt. Clear the flag. */
 206                ha->flags.mbox_int = 0;
 207                clear_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
 208
 209                if (ha->mailbox_out[0] != MBS_COMMAND_COMPLETE)
 210                        rval = QLA_FUNCTION_FAILED;
 211
 212                /* Load return mailbox registers. */
 213                iptr2 = mcp->mb;
 214                iptr = (uint16_t *)&ha->mailbox_out[0];
 215                mboxes = mcp->in_mb;
 216                for (cnt = 0; cnt < ha->mbx_count; cnt++) {
 217                        if (mboxes & BIT_0)
 218                                *iptr2 = *iptr;
 219
 220                        mboxes >>= 1;
 221                        iptr2++;
 222                        iptr++;
 223                }
 224        } else {
 225
 226#if defined(QL_DEBUG_LEVEL_2) || defined(QL_DEBUG_LEVEL_3) || \
 227                defined(QL_DEBUG_LEVEL_11)
 228                uint16_t mb0;
 229                uint32_t ictrl;
 230
 231                if (IS_FWI2_CAPABLE(ha)) {
 232                        mb0 = RD_REG_WORD(&reg->isp24.mailbox0);
 233                        ictrl = RD_REG_DWORD(&reg->isp24.ictrl);
 234                } else {
 235                        mb0 = RD_MAILBOX_REG(ha, &reg->isp, 0);
 236                        ictrl = RD_REG_WORD(&reg->isp.ictrl);
 237                }
 238                printk("%s(%ld): **** MB Command Timeout for cmd %x ****\n",
 239                    __func__, ha->host_no, command);
 240                printk("%s(%ld): icontrol=%x jiffies=%lx\n", __func__,
 241                    ha->host_no, ictrl, jiffies);
 242                printk("%s(%ld): *** mailbox[0] = 0x%x ***\n", __func__,
 243                    ha->host_no, mb0);
 244                qla2x00_dump_regs(ha);
 245#endif
 246
 247                rval = QLA_FUNCTION_TIMEOUT;
 248        }
 249
 250        ha->flags.mbox_busy = 0;
 251
 252        /* Clean up */
 253        ha->mcp = NULL;
 254
 255        if (abort_active || !io_lock_on) {
 256                DEBUG11(printk("%s(%ld): checking for additional resp "
 257                    "interrupt.\n", __func__, ha->host_no));
 258
 259                /* polling mode for non isp_abort commands. */
 260                qla2x00_poll(ha);
 261        }
 262
 263        if (rval == QLA_FUNCTION_TIMEOUT &&
 264            mcp->mb[0] != MBC_GEN_SYSTEM_ERROR) {
 265                if (!io_lock_on || (mcp->flags & IOCTL_CMD)) {
 266                        /* not in dpc. schedule it for dpc to take over. */
 267                        DEBUG(printk("%s(%ld): timeout schedule "
 268                            "isp_abort_needed.\n", __func__, ha->host_no));
 269                        DEBUG2_3_11(printk("%s(%ld): timeout schedule "
 270                            "isp_abort_needed.\n", __func__, ha->host_no));
 271                        qla_printk(KERN_WARNING, ha,
 272                            "Mailbox command timeout occured. Scheduling ISP "
 273                            "abort.\n");
 274                        set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
 275                        qla2xxx_wake_dpc(ha);
 276                } else if (!abort_active) {
 277                        /* call abort directly since we are in the DPC thread */
 278                        DEBUG(printk("%s(%ld): timeout calling abort_isp\n",
 279                            __func__, ha->host_no));
 280                        DEBUG2_3_11(printk("%s(%ld): timeout calling "
 281                            "abort_isp\n", __func__, ha->host_no));
 282                        qla_printk(KERN_WARNING, ha,
 283                            "Mailbox command timeout occured. Issuing ISP "
 284                            "abort.\n");
 285
 286                        set_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
 287                        clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
 288                        if (qla2x00_abort_isp(ha)) {
 289                                /* Failed. retry later. */
 290                                set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
 291                        }
 292                        clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
 293                        DEBUG(printk("%s(%ld): finished abort_isp\n", __func__,
 294                            ha->host_no));
 295                        DEBUG2_3_11(printk("%s(%ld): finished abort_isp\n",
 296                            __func__, ha->host_no));
 297                }
 298        }
 299
 300        /* Allow next mbx cmd to come in. */
 301        if (!abort_active)
 302                up(&ha->mbx_cmd_sem);
 303
 304        if (rval) {
 305                DEBUG2_3_11(printk("%s(%ld): **** FAILED. mbx0=%x, mbx1=%x, "
 306                    "mbx2=%x, cmd=%x ****\n", __func__, ha->host_no,
 307                    mcp->mb[0], mcp->mb[1], mcp->mb[2], command));
 308        } else {
 309                DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
 310        }
 311
 312        return rval;
 313}
 314
 315int
 316qla2x00_load_ram(scsi_qla_host_t *ha, dma_addr_t req_dma, uint32_t risc_addr,
 317    uint32_t risc_code_size)
 318{
 319        int rval;
 320        mbx_cmd_t mc;
 321        mbx_cmd_t *mcp = &mc;
 322
 323        DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
 324
 325        if (MSW(risc_addr) || IS_FWI2_CAPABLE(ha)) {
 326                mcp->mb[0] = MBC_LOAD_RISC_RAM_EXTENDED;
 327                mcp->mb[8] = MSW(risc_addr);
 328                mcp->out_mb = MBX_8|MBX_0;
 329        } else {
 330                mcp->mb[0] = MBC_LOAD_RISC_RAM;
 331                mcp->out_mb = MBX_0;
 332        }
 333        mcp->mb[1] = LSW(risc_addr);
 334        mcp->mb[2] = MSW(req_dma);
 335        mcp->mb[3] = LSW(req_dma);
 336        mcp->mb[6] = MSW(MSD(req_dma));
 337        mcp->mb[7] = LSW(MSD(req_dma));
 338        mcp->out_mb |= MBX_7|MBX_6|MBX_3|MBX_2|MBX_1;
 339        if (IS_FWI2_CAPABLE(ha)) {
 340                mcp->mb[4] = MSW(risc_code_size);
 341                mcp->mb[5] = LSW(risc_code_size);
 342                mcp->out_mb |= MBX_5|MBX_4;
 343        } else {
 344                mcp->mb[4] = LSW(risc_code_size);
 345                mcp->out_mb |= MBX_4;
 346        }
 347
 348        mcp->in_mb = MBX_0;
 349        mcp->tov = 30;
 350        mcp->flags = 0;
 351        rval = qla2x00_mailbox_command(ha, mcp);
 352
 353        if (rval != QLA_SUCCESS) {
 354                DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=%x.\n", __func__,
 355                    ha->host_no, rval, mcp->mb[0]));
 356        } else {
 357                DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
 358        }
 359
 360        return rval;
 361}
 362
 363/*
 364 * qla2x00_execute_fw
 365 *     Start adapter firmware.
 366 *
 367 * Input:
 368 *     ha = adapter block pointer.
 369 *     TARGET_QUEUE_LOCK must be released.
 370 *     ADAPTER_STATE_LOCK must be released.
 371 *
 372 * Returns:
 373 *     qla2x00 local function return status code.
 374 *
 375 * Context:
 376 *     Kernel context.
 377 */
 378int
 379qla2x00_execute_fw(scsi_qla_host_t *ha, uint32_t risc_addr)
 380{
 381        int rval;
 382        mbx_cmd_t mc;
 383        mbx_cmd_t *mcp = &mc;
 384
 385        DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
 386
 387        mcp->mb[0] = MBC_EXECUTE_FIRMWARE;
 388        mcp->out_mb = MBX_0;
 389        mcp->in_mb = MBX_0;
 390        if (IS_FWI2_CAPABLE(ha)) {
 391                mcp->mb[1] = MSW(risc_addr);
 392                mcp->mb[2] = LSW(risc_addr);
 393                mcp->mb[3] = 0;
 394                mcp->mb[4] = 0;
 395                mcp->out_mb |= MBX_4|MBX_3|MBX_2|MBX_1;
 396                mcp->in_mb |= MBX_1;
 397        } else {
 398                mcp->mb[1] = LSW(risc_addr);
 399                mcp->out_mb |= MBX_1;
 400                if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
 401                        mcp->mb[2] = 0;
 402                        mcp->out_mb |= MBX_2;
 403                }
 404        }
 405
 406        mcp->tov = 30;
 407        mcp->flags = 0;
 408        rval = qla2x00_mailbox_command(ha, mcp);
 409
 410        if (rval != QLA_SUCCESS) {
 411                DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=%x.\n", __func__,
 412                    ha->host_no, rval, mcp->mb[0]));
 413        } else {
 414                if (IS_FWI2_CAPABLE(ha)) {
 415                        DEBUG11(printk("%s(%ld): done exchanges=%x.\n",
 416                            __func__, ha->host_no, mcp->mb[1]));
 417                } else {
 418                        DEBUG11(printk("%s(%ld): done.\n", __func__,
 419                            ha->host_no));
 420                }
 421        }
 422
 423        return rval;
 424}
 425
 426/*
 427 * qla2x00_get_fw_version
 428 *      Get firmware version.
 429 *
 430 * Input:
 431 *      ha:             adapter state pointer.
 432 *      major:          pointer for major number.
 433 *      minor:          pointer for minor number.
 434 *      subminor:       pointer for subminor number.
 435 *
 436 * Returns:
 437 *      qla2x00 local function return status code.
 438 *
 439 * Context:
 440 *      Kernel context.
 441 */
 442void
 443qla2x00_get_fw_version(scsi_qla_host_t *ha, uint16_t *major, uint16_t *minor,
 444    uint16_t *subminor, uint16_t *attributes, uint32_t *memory)
 445{
 446        int             rval;
 447        mbx_cmd_t       mc;
 448        mbx_cmd_t       *mcp = &mc;
 449
 450        DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
 451
 452        mcp->mb[0] = MBC_GET_FIRMWARE_VERSION;
 453        mcp->out_mb = MBX_0;
 454        mcp->in_mb = MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
 455        mcp->flags = 0;
 456        mcp->tov = 30;
 457        rval = qla2x00_mailbox_command(ha, mcp);
 458
 459        /* Return mailbox data. */
 460        *major = mcp->mb[1];
 461        *minor = mcp->mb[2];
 462        *subminor = mcp->mb[3];
 463        *attributes = mcp->mb[6];
 464        if (IS_QLA2100(ha) || IS_QLA2200(ha))
 465                *memory = 0x1FFFF;                      /* Defaults to 128KB. */
 466        else
 467                *memory = (mcp->mb[5] << 16) | mcp->mb[4];
 468
 469        if (rval != QLA_SUCCESS) {
 470                /*EMPTY*/
 471                DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
 472                    ha->host_no, rval));
 473        } else {
 474                /*EMPTY*/
 475                DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
 476        }
 477}
 478
 479/*
 480 * qla2x00_get_fw_options
 481 *      Set firmware options.
 482 *
 483 * Input:
 484 *      ha = adapter block pointer.
 485 *      fwopt = pointer for firmware options.
 486 *
 487 * Returns:
 488 *      qla2x00 local function return status code.
 489 *
 490 * Context:
 491 *      Kernel context.
 492 */
 493int
 494qla2x00_get_fw_options(scsi_qla_host_t *ha, uint16_t *fwopts)
 495{
 496        int rval;
 497        mbx_cmd_t mc;
 498        mbx_cmd_t *mcp = &mc;
 499
 500        DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
 501
 502        mcp->mb[0] = MBC_GET_FIRMWARE_OPTION;
 503        mcp->out_mb = MBX_0;
 504        mcp->in_mb = MBX_3|MBX_2|MBX_1|MBX_0;
 505        mcp->tov = 30;
 506        mcp->flags = 0;
 507        rval = qla2x00_mailbox_command(ha, mcp);
 508
 509        if (rval != QLA_SUCCESS) {
 510                /*EMPTY*/
 511                DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
 512                    ha->host_no, rval));
 513        } else {
 514                fwopts[0] = mcp->mb[0];
 515                fwopts[1] = mcp->mb[1];
 516                fwopts[2] = mcp->mb[2];
 517                fwopts[3] = mcp->mb[3];
 518
 519                DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
 520        }
 521
 522        return rval;
 523}
 524
 525
 526/*
 527 * qla2x00_set_fw_options
 528 *      Set firmware options.
 529 *
 530 * Input:
 531 *      ha = adapter block pointer.
 532 *      fwopt = pointer for firmware options.
 533 *
 534 * Returns:
 535 *      qla2x00 local function return status code.
 536 *
 537 * Context:
 538 *      Kernel context.
 539 */
 540int
 541qla2x00_set_fw_options(scsi_qla_host_t *ha, uint16_t *fwopts)
 542{
 543        int rval;
 544        mbx_cmd_t mc;
 545        mbx_cmd_t *mcp = &mc;
 546
 547        DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
 548
 549        mcp->mb[0] = MBC_SET_FIRMWARE_OPTION;
 550        mcp->mb[1] = fwopts[1];
 551        mcp->mb[2] = fwopts[2];
 552        mcp->mb[3] = fwopts[3];
 553        mcp->out_mb = MBX_3|MBX_2|MBX_1|MBX_0;
 554        mcp->in_mb = MBX_0;
 555        if (IS_FWI2_CAPABLE(ha)) {
 556                mcp->in_mb |= MBX_1;
 557        } else {
 558                mcp->mb[10] = fwopts[10];
 559                mcp->mb[11] = fwopts[11];
 560                mcp->mb[12] = 0;        /* Undocumented, but used */
 561                mcp->out_mb |= MBX_12|MBX_11|MBX_10;
 562        }
 563        mcp->tov = 30;
 564        mcp->flags = 0;
 565        rval = qla2x00_mailbox_command(ha, mcp);
 566
 567        fwopts[0] = mcp->mb[0];
 568
 569        if (rval != QLA_SUCCESS) {
 570                /*EMPTY*/
 571                DEBUG2_3_11(printk("%s(%ld): failed=%x (%x/%x).\n", __func__,
 572                    ha->host_no, rval, mcp->mb[0], mcp->mb[1]));
 573        } else {
 574                /*EMPTY*/
 575                DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
 576        }
 577
 578        return rval;
 579}
 580
 581/*
 582 * qla2x00_mbx_reg_test
 583 *      Mailbox register wrap test.
 584 *
 585 * Input:
 586 *      ha = adapter block pointer.
 587 *      TARGET_QUEUE_LOCK must be released.
 588 *      ADAPTER_STATE_LOCK must be released.
 589 *
 590 * Returns:
 591 *      qla2x00 local function return status code.
 592 *
 593 * Context:
 594 *      Kernel context.
 595 */
 596int
 597qla2x00_mbx_reg_test(scsi_qla_host_t *ha)
 598{
 599        int rval;
 600        mbx_cmd_t mc;
 601        mbx_cmd_t *mcp = &mc;
 602
 603        DEBUG11(printk("qla2x00_mbx_reg_test(%ld): entered.\n", ha->host_no));
 604
 605        mcp->mb[0] = MBC_MAILBOX_REGISTER_TEST;
 606        mcp->mb[1] = 0xAAAA;
 607        mcp->mb[2] = 0x5555;
 608        mcp->mb[3] = 0xAA55;
 609        mcp->mb[4] = 0x55AA;
 610        mcp->mb[5] = 0xA5A5;
 611        mcp->mb[6] = 0x5A5A;
 612        mcp->mb[7] = 0x2525;
 613        mcp->out_mb = MBX_7|MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
 614        mcp->in_mb = MBX_7|MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
 615        mcp->tov = 30;
 616        mcp->flags = 0;
 617        rval = qla2x00_mailbox_command(ha, mcp);
 618
 619        if (rval == QLA_SUCCESS) {
 620                if (mcp->mb[1] != 0xAAAA || mcp->mb[2] != 0x5555 ||
 621                    mcp->mb[3] != 0xAA55 || mcp->mb[4] != 0x55AA)
 622                        rval = QLA_FUNCTION_FAILED;
 623                if (mcp->mb[5] != 0xA5A5 || mcp->mb[6] != 0x5A5A ||
 624                    mcp->mb[7] != 0x2525)
 625                        rval = QLA_FUNCTION_FAILED;
 626        }
 627
 628        if (rval != QLA_SUCCESS) {
 629                /*EMPTY*/
 630                DEBUG2_3_11(printk("qla2x00_mbx_reg_test(%ld): failed=%x.\n",
 631                    ha->host_no, rval));
 632        } else {
 633                /*EMPTY*/
 634                DEBUG11(printk("qla2x00_mbx_reg_test(%ld): done.\n",
 635                    ha->host_no));
 636        }
 637
 638        return rval;
 639}
 640
 641/*
 642 * qla2x00_verify_checksum
 643 *      Verify firmware checksum.
 644 *
 645 * Input:
 646 *      ha = adapter block pointer.
 647 *      TARGET_QUEUE_LOCK must be released.
 648 *      ADAPTER_STATE_LOCK must be released.
 649 *
 650 * Returns:
 651 *      qla2x00 local function return status code.
 652 *
 653 * Context:
 654 *      Kernel context.
 655 */
 656int
 657qla2x00_verify_checksum(scsi_qla_host_t *ha, uint32_t risc_addr)
 658{
 659        int rval;
 660        mbx_cmd_t mc;
 661        mbx_cmd_t *mcp = &mc;
 662
 663        DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
 664
 665        mcp->mb[0] = MBC_VERIFY_CHECKSUM;
 666        mcp->out_mb = MBX_0;
 667        mcp->in_mb = MBX_0;
 668        if (IS_FWI2_CAPABLE(ha)) {
 669                mcp->mb[1] = MSW(risc_addr);
 670                mcp->mb[2] = LSW(risc_addr);
 671                mcp->out_mb |= MBX_2|MBX_1;
 672                mcp->in_mb |= MBX_2|MBX_1;
 673        } else {
 674                mcp->mb[1] = LSW(risc_addr);
 675                mcp->out_mb |= MBX_1;
 676                mcp->in_mb |= MBX_1;
 677        }
 678
 679        mcp->tov = 30;
 680        mcp->flags = 0;
 681        rval = qla2x00_mailbox_command(ha, mcp);
 682
 683        if (rval != QLA_SUCCESS) {
 684                DEBUG2_3_11(printk("%s(%ld): failed=%x chk sum=%x.\n", __func__,
 685                    ha->host_no, rval, IS_FWI2_CAPABLE(ha) ?
 686                    (mcp->mb[2] << 16) | mcp->mb[1]: mcp->mb[1]));
 687        } else {
 688                DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
 689        }
 690
 691        return rval;
 692}
 693
 694/*
 695 * qla2x00_issue_iocb
 696 *      Issue IOCB using mailbox command
 697 *
 698 * Input:
 699 *      ha = adapter state pointer.
 700 *      buffer = buffer pointer.
 701 *      phys_addr = physical address of buffer.
 702 *      size = size of buffer.
 703 *      TARGET_QUEUE_LOCK must be released.
 704 *      ADAPTER_STATE_LOCK must be released.
 705 *
 706 * Returns:
 707 *      qla2x00 local function return status code.
 708 *
 709 * Context:
 710 *      Kernel context.
 711 */
 712int
 713qla2x00_issue_iocb(scsi_qla_host_t *ha, void*  buffer, dma_addr_t phys_addr,
 714    size_t size)
 715{
 716        int             rval;
 717        mbx_cmd_t       mc;
 718        mbx_cmd_t       *mcp = &mc;
 719
 720        mcp->mb[0] = MBC_IOCB_COMMAND_A64;
 721        mcp->mb[1] = 0;
 722        mcp->mb[2] = MSW(phys_addr);
 723        mcp->mb[3] = LSW(phys_addr);
 724        mcp->mb[6] = MSW(MSD(phys_addr));
 725        mcp->mb[7] = LSW(MSD(phys_addr));
 726        mcp->out_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
 727        mcp->in_mb = MBX_2|MBX_0;
 728        mcp->tov = 30;
 729        mcp->flags = 0;
 730        rval = qla2x00_mailbox_command(ha, mcp);
 731
 732        if (rval != QLA_SUCCESS) {
 733                /*EMPTY*/
 734                DEBUG(printk("qla2x00_issue_iocb(%ld): failed rval 0x%x\n",
 735                    ha->host_no, rval));
 736                DEBUG2(printk("qla2x00_issue_iocb(%ld): failed rval 0x%x\n",
 737                    ha->host_no, rval));
 738        } else {
 739                sts_entry_t *sts_entry = (sts_entry_t *) buffer;
 740
 741                /* Mask reserved bits. */
 742                sts_entry->entry_status &=
 743                    IS_FWI2_CAPABLE(ha) ? RF_MASK_24XX :RF_MASK;
 744        }
 745
 746        return rval;
 747}
 748
 749/*
 750 * qla2x00_abort_command
 751 *      Abort command aborts a specified IOCB.
 752 *
 753 * Input:
 754 *      ha = adapter block pointer.
 755 *      sp = SB structure pointer.
 756 *
 757 * Returns:
 758 *      qla2x00 local function return status code.
 759 *
 760 * Context:
 761 *      Kernel context.
 762 */
 763int
 764qla2x00_abort_command(scsi_qla_host_t *ha, srb_t *sp)
 765{
 766        unsigned long   flags = 0;
 767        fc_port_t       *fcport;
 768        int             rval;
 769        uint32_t        handle;
 770        mbx_cmd_t       mc;
 771        mbx_cmd_t       *mcp = &mc;
 772
 773        DEBUG11(printk("qla2x00_abort_command(%ld): entered.\n", ha->host_no));
 774
 775        fcport = sp->fcport;
 776
 777        spin_lock_irqsave(&ha->hardware_lock, flags);
 778        for (handle = 1; handle < MAX_OUTSTANDING_COMMANDS; handle++) {
 779                if (ha->outstanding_cmds[handle] == sp)
 780                        break;
 781        }
 782        spin_unlock_irqrestore(&ha->hardware_lock, flags);
 783
 784        if (handle == MAX_OUTSTANDING_COMMANDS) {
 785                /* command not found */
 786                return QLA_FUNCTION_FAILED;
 787        }
 788
 789        mcp->mb[0] = MBC_ABORT_COMMAND;
 790        if (HAS_EXTENDED_IDS(ha))
 791                mcp->mb[1] = fcport->loop_id;
 792        else
 793                mcp->mb[1] = fcport->loop_id << 8;
 794        mcp->mb[2] = (uint16_t)handle;
 795        mcp->mb[3] = (uint16_t)(handle >> 16);
 796        mcp->mb[6] = (uint16_t)sp->cmd->device->lun;
 797        mcp->out_mb = MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
 798        mcp->in_mb = MBX_0;
 799        mcp->tov = 30;
 800        mcp->flags = 0;
 801        rval = qla2x00_mailbox_command(ha, mcp);
 802
 803        if (rval != QLA_SUCCESS) {
 804                DEBUG2_3_11(printk("qla2x00_abort_command(%ld): failed=%x.\n",
 805                    ha->host_no, rval));
 806        } else {
 807                sp->flags |= SRB_ABORT_PENDING;
 808                DEBUG11(printk("qla2x00_abort_command(%ld): done.\n",
 809                    ha->host_no));
 810        }
 811
 812        return rval;
 813}
 814
 815#if USE_ABORT_TGT
 816/*
 817 * qla2x00_abort_target
 818 *      Issue abort target mailbox command.
 819 *
 820 * Input:
 821 *      ha = adapter block pointer.
 822 *
 823 * Returns:
 824 *      qla2x00 local function return status code.
 825 *
 826 * Context:
 827 *      Kernel context.
 828 */
 829int
 830qla2x00_abort_target(fc_port_t *fcport)
 831{
 832        int        rval;
 833        mbx_cmd_t  mc;
 834        mbx_cmd_t  *mcp = &mc;
 835        scsi_qla_host_t *ha;
 836
 837        if (fcport == NULL)
 838                return 0;
 839
 840        DEBUG11(printk("%s(%ld): entered.\n", __func__, fcport->ha->host_no));
 841
 842        ha = fcport->ha;
 843        mcp->mb[0] = MBC_ABORT_TARGET;
 844        mcp->out_mb = MBX_2|MBX_1|MBX_0;
 845        if (HAS_EXTENDED_IDS(ha)) {
 846                mcp->mb[1] = fcport->loop_id;
 847                mcp->mb[10] = 0;
 848                mcp->out_mb |= MBX_10;
 849        } else {
 850                mcp->mb[1] = fcport->loop_id << 8;
 851        }
 852        mcp->mb[2] = ha->loop_reset_delay;
 853
 854        mcp->in_mb = MBX_0;
 855        mcp->tov = 30;
 856        mcp->flags = 0;
 857        rval = qla2x00_mailbox_command(ha, mcp);
 858
 859        /* Issue marker command. */
 860        ha->marker_needed = 1;
 861
 862        if (rval != QLA_SUCCESS) {
 863                DEBUG2_3_11(printk("qla2x00_abort_target(%ld): failed=%x.\n",
 864                    ha->host_no, rval));
 865        } else {
 866                /*EMPTY*/
 867                DEBUG11(printk("qla2x00_abort_target(%ld): done.\n",
 868                    ha->host_no));
 869        }
 870
 871        return rval;
 872}
 873#endif
 874
 875/*
 876 * qla2x00_get_adapter_id
 877 *      Get adapter ID and topology.
 878 *
 879 * Input:
 880 *      ha = adapter block pointer.
 881 *      id = pointer for loop ID.
 882 *      al_pa = pointer for AL_PA.
 883 *      area = pointer for area.
 884 *      domain = pointer for domain.
 885 *      top = pointer for topology.
 886 *      TARGET_QUEUE_LOCK must be released.
 887 *      ADAPTER_STATE_LOCK must be released.
 888 *
 889 * Returns:
 890 *      qla2x00 local function return status code.
 891 *
 892 * Context:
 893 *      Kernel context.
 894 */
 895int
 896qla2x00_get_adapter_id(scsi_qla_host_t *ha, uint16_t *id, uint8_t *al_pa,
 897    uint8_t *area, uint8_t *domain, uint16_t *top, uint16_t *sw_cap)
 898{
 899        int rval;
 900        mbx_cmd_t mc;
 901        mbx_cmd_t *mcp = &mc;
 902
 903        DEBUG11(printk("qla2x00_get_adapter_id(%ld): entered.\n",
 904            ha->host_no));
 905
 906        mcp->mb[0] = MBC_GET_ADAPTER_LOOP_ID;
 907        mcp->mb[9] = ha->vp_idx;
 908        mcp->out_mb = MBX_0;
 909        mcp->in_mb = MBX_9|MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
 910        mcp->tov = 30;
 911        mcp->flags = 0;
 912        rval = qla2x00_mailbox_command(ha, mcp);
 913        if (mcp->mb[0] == MBS_COMMAND_ERROR)
 914                rval = QLA_COMMAND_ERROR;
 915
 916        /* Return data. */
 917        *id = mcp->mb[1];
 918        *al_pa = LSB(mcp->mb[2]);
 919        *area = MSB(mcp->mb[2]);
 920        *domain = LSB(mcp->mb[3]);
 921        *top = mcp->mb[6];
 922        *sw_cap = mcp->mb[7];
 923
 924        if (rval != QLA_SUCCESS) {
 925                /*EMPTY*/
 926                DEBUG2_3_11(printk("qla2x00_get_adapter_id(%ld): failed=%x.\n",
 927                    ha->host_no, rval));
 928        } else {
 929                /*EMPTY*/
 930                DEBUG11(printk("qla2x00_get_adapter_id(%ld): done.\n",
 931                    ha->host_no));
 932        }
 933
 934        return rval;
 935}
 936
 937/*
 938 * qla2x00_get_retry_cnt
 939 *      Get current firmware login retry count and delay.
 940 *
 941 * Input:
 942 *      ha = adapter block pointer.
 943 *      retry_cnt = pointer to login retry count.
 944 *      tov = pointer to login timeout value.
 945 *
 946 * Returns:
 947 *      qla2x00 local function return status code.
 948 *
 949 * Context:
 950 *      Kernel context.
 951 */
 952int
 953qla2x00_get_retry_cnt(scsi_qla_host_t *ha, uint8_t *retry_cnt, uint8_t *tov,
 954    uint16_t *r_a_tov)
 955{
 956        int rval;
 957        uint16_t ratov;
 958        mbx_cmd_t mc;
 959        mbx_cmd_t *mcp = &mc;
 960
 961        DEBUG11(printk("qla2x00_get_retry_cnt(%ld): entered.\n",
 962                        ha->host_no));
 963
 964        mcp->mb[0] = MBC_GET_RETRY_COUNT;
 965        mcp->out_mb = MBX_0;
 966        mcp->in_mb = MBX_3|MBX_2|MBX_1|MBX_0;
 967        mcp->tov = 30;
 968        mcp->flags = 0;
 969        rval = qla2x00_mailbox_command(ha, mcp);
 970
 971        if (rval != QLA_SUCCESS) {
 972                /*EMPTY*/
 973                DEBUG2_3_11(printk("qla2x00_get_retry_cnt(%ld): failed = %x.\n",
 974                    ha->host_no, mcp->mb[0]));
 975        } else {
 976                /* Convert returned data and check our values. */
 977                *r_a_tov = mcp->mb[3] / 2;
 978                ratov = (mcp->mb[3]/2) / 10;  /* mb[3] value is in 100ms */
 979                if (mcp->mb[1] * ratov > (*retry_cnt) * (*tov)) {
 980                        /* Update to the larger values */
 981                        *retry_cnt = (uint8_t)mcp->mb[1];
 982                        *tov = ratov;
 983                }
 984
 985                DEBUG11(printk("qla2x00_get_retry_cnt(%ld): done. mb3=%d "
 986                    "ratov=%d.\n", ha->host_no, mcp->mb[3], ratov));
 987        }
 988
 989        return rval;
 990}
 991
 992/*
 993 * qla2x00_init_firmware
 994 *      Initialize adapter firmware.
 995 *
 996 * Input:
 997 *      ha = adapter block pointer.
 998 *      dptr = Initialization control block pointer.
 999 *      size = size of initialization control block.
1000 *      TARGET_QUEUE_LOCK must be released.
1001 *      ADAPTER_STATE_LOCK must be released.
1002 *
1003 * Returns:
1004 *      qla2x00 local function return status code.
1005 *
1006 * Context:
1007 *      Kernel context.
1008 */
1009int
1010qla2x00_init_firmware(scsi_qla_host_t *ha, uint16_t size)
1011{
1012        int rval;
1013        mbx_cmd_t mc;
1014        mbx_cmd_t *mcp = &mc;
1015
1016        DEBUG11(printk("qla2x00_init_firmware(%ld): entered.\n",
1017            ha->host_no));
1018
1019        if (ha->flags.npiv_supported)
1020                mcp->mb[0] = MBC_MID_INITIALIZE_FIRMWARE;
1021        else
1022                mcp->mb[0] = MBC_INITIALIZE_FIRMWARE;
1023
1024        mcp->mb[2] = MSW(ha->init_cb_dma);
1025        mcp->mb[3] = LSW(ha->init_cb_dma);
1026        mcp->mb[4] = 0;
1027        mcp->mb[5] = 0;
1028        mcp->mb[6] = MSW(MSD(ha->init_cb_dma));
1029        mcp->mb[7] = LSW(MSD(ha->init_cb_dma));
1030        mcp->out_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_0;
1031        mcp->in_mb = MBX_5|MBX_4|MBX_0;
1032        mcp->buf_size = size;
1033        mcp->flags = MBX_DMA_OUT;
1034        mcp->tov = 30;
1035        rval = qla2x00_mailbox_command(ha, mcp);
1036
1037        if (rval != QLA_SUCCESS) {
1038                /*EMPTY*/
1039                DEBUG2_3_11(printk("qla2x00_init_firmware(%ld): failed=%x "
1040                    "mb0=%x.\n",
1041                    ha->host_no, rval, mcp->mb[0]));
1042        } else {
1043                /*EMPTY*/
1044                DEBUG11(printk("qla2x00_init_firmware(%ld): done.\n",
1045                    ha->host_no));
1046        }
1047
1048        return rval;
1049}
1050
1051/*
1052 * qla2x00_get_port_database
1053 *      Issue normal/enhanced get port database mailbox command
1054 *      and copy device name as necessary.
1055 *
1056 * Input:
1057 *      ha = adapter state pointer.
1058 *      dev = structure pointer.
1059 *      opt = enhanced cmd option byte.
1060 *
1061 * Returns:
1062 *      qla2x00 local function return status code.
1063 *
1064 * Context:
1065 *      Kernel context.
1066 */
1067int
1068qla2x00_get_port_database(scsi_qla_host_t *ha, fc_port_t *fcport, uint8_t opt)
1069{
1070        int rval;
1071        mbx_cmd_t mc;
1072        mbx_cmd_t *mcp = &mc;
1073        port_database_t *pd;
1074        struct port_database_24xx *pd24;
1075        dma_addr_t pd_dma;
1076
1077        DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
1078
1079        pd24 = NULL;
1080        pd = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &pd_dma);
1081        if (pd  == NULL) {
1082                DEBUG2_3(printk("%s(%ld): failed to allocate Port Database "
1083                    "structure.\n", __func__, ha->host_no));
1084                return QLA_MEMORY_ALLOC_FAILED;
1085        }
1086        memset(pd, 0, max(PORT_DATABASE_SIZE, PORT_DATABASE_24XX_SIZE));
1087
1088        mcp->mb[0] = MBC_GET_PORT_DATABASE;
1089        if (opt != 0 && !IS_FWI2_CAPABLE(ha))
1090                mcp->mb[0] = MBC_ENHANCED_GET_PORT_DATABASE;
1091        mcp->mb[2] = MSW(pd_dma);
1092        mcp->mb[3] = LSW(pd_dma);
1093        mcp->mb[6] = MSW(MSD(pd_dma));
1094        mcp->mb[7] = LSW(MSD(pd_dma));
1095        mcp->mb[9] = ha->vp_idx;
1096        mcp->out_mb = MBX_9|MBX_7|MBX_6|MBX_3|MBX_2|MBX_0;
1097        mcp->in_mb = MBX_0;
1098        if (IS_FWI2_CAPABLE(ha)) {
1099                mcp->mb[1] = fcport->loop_id;
1100                mcp->mb[10] = opt;
1101                mcp->out_mb |= MBX_10|MBX_1;
1102                mcp->in_mb |= MBX_1;
1103        } else if (HAS_EXTENDED_IDS(ha)) {
1104                mcp->mb[1] = fcport->loop_id;
1105                mcp->mb[10] = opt;
1106                mcp->out_mb |= MBX_10|MBX_1;
1107        } else {
1108                mcp->mb[1] = fcport->loop_id << 8 | opt;
1109                mcp->out_mb |= MBX_1;
1110        }
1111        mcp->buf_size = IS_FWI2_CAPABLE(ha) ?
1112            PORT_DATABASE_24XX_SIZE : PORT_DATABASE_SIZE;
1113        mcp->flags = MBX_DMA_IN;
1114        mcp->tov = (ha->login_timeout * 2) + (ha->login_timeout / 2);
1115        rval = qla2x00_mailbox_command(ha, mcp);
1116        if (rval != QLA_SUCCESS)
1117                goto gpd_error_out;
1118
1119        if (IS_FWI2_CAPABLE(ha)) {
1120                pd24 = (struct port_database_24xx *) pd;
1121
1122                /* Check for logged in state. */
1123                if (pd24->current_login_state != PDS_PRLI_COMPLETE &&
1124                    pd24->last_login_state != PDS_PRLI_COMPLETE) {
1125                        DEBUG2(printk("%s(%ld): Unable to verify "
1126                            "login-state (%x/%x) for loop_id %x\n",
1127                            __func__, ha->host_no,
1128                            pd24->current_login_state,
1129                            pd24->last_login_state, fcport->loop_id));
1130                        rval = QLA_FUNCTION_FAILED;
1131                        goto gpd_error_out;
1132                }
1133
1134                /* Names are little-endian. */
1135                memcpy(fcport->node_name, pd24->node_name, WWN_SIZE);
1136                memcpy(fcport->port_name, pd24->port_name, WWN_SIZE);
1137
1138                /* Get port_id of device. */
1139                fcport->d_id.b.domain = pd24->port_id[0];
1140                fcport->d_id.b.area = pd24->port_id[1];
1141                fcport->d_id.b.al_pa = pd24->port_id[2];
1142                fcport->d_id.b.rsvd_1 = 0;
1143
1144                /* If not target must be initiator or unknown type. */
1145                if ((pd24->prli_svc_param_word_3[0] & BIT_4) == 0)
1146                        fcport->port_type = FCT_INITIATOR;
1147                else
1148                        fcport->port_type = FCT_TARGET;
1149        } else {
1150                /* Check for logged in state. */
1151                if (pd->master_state != PD_STATE_PORT_LOGGED_IN &&
1152                    pd->slave_state != PD_STATE_PORT_LOGGED_IN) {
1153                        rval = QLA_FUNCTION_FAILED;
1154                        goto gpd_error_out;
1155                }
1156
1157                /* Names are little-endian. */
1158                memcpy(fcport->node_name, pd->node_name, WWN_SIZE);
1159                memcpy(fcport->port_name, pd->port_name, WWN_SIZE);
1160
1161                /* Get port_id of device. */
1162                fcport->d_id.b.domain = pd->port_id[0];
1163                fcport->d_id.b.area = pd->port_id[3];
1164                fcport->d_id.b.al_pa = pd->port_id[2];
1165                fcport->d_id.b.rsvd_1 = 0;
1166
1167                /* Check for device require authentication. */
1168                pd->common_features & BIT_5 ? (fcport->flags |= FCF_AUTH_REQ) :
1169                    (fcport->flags &= ~FCF_AUTH_REQ);
1170
1171                /* If not target must be initiator or unknown type. */
1172                if ((pd->prli_svc_param_word_3[0] & BIT_4) == 0)
1173                        fcport->port_type = FCT_INITIATOR;
1174                else
1175                        fcport->port_type = FCT_TARGET;
1176
1177                /* Passback COS information. */
1178                fcport->supported_classes = (pd->options & BIT_4) ?
1179                    FC_COS_CLASS2: FC_COS_CLASS3;
1180        }
1181
1182gpd_error_out:
1183        dma_pool_free(ha->s_dma_pool, pd, pd_dma);
1184
1185        if (rval != QLA_SUCCESS) {
1186                DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=%x mb[1]=%x.\n",
1187                    __func__, ha->host_no, rval, mcp->mb[0], mcp->mb[1]));
1188        } else {
1189                DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
1190        }
1191
1192        return rval;
1193}
1194
1195/*
1196 * qla2x00_get_firmware_state
1197 *      Get adapter firmware state.
1198 *
1199 * Input:
1200 *      ha = adapter block pointer.
1201 *      dptr = pointer for firmware state.
1202 *      TARGET_QUEUE_LOCK must be released.
1203 *      ADAPTER_STATE_LOCK must be released.
1204 *
1205 * Returns:
1206 *      qla2x00 local function return status code.
1207 *
1208 * Context:
1209 *      Kernel context.
1210 */
1211int
1212qla2x00_get_firmware_state(scsi_qla_host_t *ha, uint16_t *dptr)
1213{
1214        int rval;
1215        mbx_cmd_t mc;
1216        mbx_cmd_t *mcp = &mc;
1217
1218        DEBUG11(printk("qla2x00_get_firmware_state(%ld): entered.\n",
1219            ha->host_no));
1220
1221        mcp->mb[0] = MBC_GET_FIRMWARE_STATE;
1222        mcp->out_mb = MBX_0;
1223        mcp->in_mb = MBX_2|MBX_1|MBX_0;
1224        mcp->tov = 30;
1225        mcp->flags = 0;
1226        rval = qla2x00_mailbox_command(ha, mcp);
1227
1228        /* Return firmware state. */
1229        *dptr = mcp->mb[1];
1230
1231        if (rval != QLA_SUCCESS) {
1232                /*EMPTY*/
1233                DEBUG2_3_11(printk("qla2x00_get_firmware_state(%ld): "
1234                    "failed=%x.\n", ha->host_no, rval));
1235        } else {
1236                /*EMPTY*/
1237                DEBUG11(printk("qla2x00_get_firmware_state(%ld): done.\n",
1238                    ha->host_no));
1239        }
1240
1241        return rval;
1242}
1243
1244/*
1245 * qla2x00_get_port_name
1246 *      Issue get port name mailbox command.
1247 *      Returned name is in big endian format.
1248 *
1249 * Input:
1250 *      ha = adapter block pointer.
1251 *      loop_id = loop ID of device.
1252 *      name = pointer for name.
1253 *      TARGET_QUEUE_LOCK must be released.
1254 *      ADAPTER_STATE_LOCK must be released.
1255 *
1256 * Returns:
1257 *      qla2x00 local function return status code.
1258 *
1259 * Context:
1260 *      Kernel context.
1261 */
1262int
1263qla2x00_get_port_name(scsi_qla_host_t *ha, uint16_t loop_id, uint8_t *name,
1264    uint8_t opt)
1265{
1266        int rval;
1267        mbx_cmd_t mc;
1268        mbx_cmd_t *mcp = &mc;
1269
1270        DEBUG11(printk("qla2x00_get_port_name(%ld): entered.\n",
1271            ha->host_no));
1272
1273        mcp->mb[0] = MBC_GET_PORT_NAME;
1274        mcp->mb[9] = ha->vp_idx;
1275        mcp->out_mb = MBX_9|MBX_1|MBX_0;
1276        if (HAS_EXTENDED_IDS(ha)) {
1277                mcp->mb[1] = loop_id;
1278                mcp->mb[10] = opt;
1279                mcp->out_mb |= MBX_10;
1280        } else {
1281                mcp->mb[1] = loop_id << 8 | opt;
1282        }
1283
1284        mcp->in_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
1285        mcp->tov = 30;
1286        mcp->flags = 0;
1287        rval = qla2x00_mailbox_command(ha, mcp);
1288
1289        if (rval != QLA_SUCCESS) {
1290                /*EMPTY*/
1291                DEBUG2_3_11(printk("qla2x00_get_port_name(%ld): failed=%x.\n",
1292                    ha->host_no, rval));
1293        } else {
1294                if (name != NULL) {
1295                        /* This function returns name in big endian. */
1296                        name[0] = MSB(mcp->mb[2]);
1297                        name[1] = LSB(mcp->mb[2]);
1298                        name[2] = MSB(mcp->mb[3]);
1299                        name[3] = LSB(mcp->mb[3]);
1300                        name[4] = MSB(mcp->mb[6]);
1301                        name[5] = LSB(mcp->mb[6]);
1302                        name[6] = MSB(mcp->mb[7]);
1303                        name[7] = LSB(mcp->mb[7]);
1304                }
1305
1306                DEBUG11(printk("qla2x00_get_port_name(%ld): done.\n",
1307                    ha->host_no));
1308        }
1309
1310        return rval;
1311}
1312
1313/*
1314 * qla2x00_lip_reset
1315 *      Issue LIP reset mailbox command.
1316 *
1317 * Input:
1318 *      ha = adapter block pointer.
1319 *      TARGET_QUEUE_LOCK must be released.
1320 *      ADAPTER_STATE_LOCK must be released.
1321 *
1322 * Returns:
1323 *      qla2x00 local function return status code.
1324 *
1325 * Context:
1326 *      Kernel context.
1327 */
1328int
1329qla2x00_lip_reset(scsi_qla_host_t *ha)
1330{
1331        int rval;
1332        mbx_cmd_t mc;
1333        mbx_cmd_t *mcp = &mc;
1334
1335        DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
1336
1337        if (IS_FWI2_CAPABLE(ha)) {
1338                mcp->mb[0] = MBC_LIP_FULL_LOGIN;
1339                mcp->mb[1] = BIT_6;
1340                mcp->mb[2] = 0;
1341                mcp->mb[3] = ha->loop_reset_delay;
1342                mcp->out_mb = MBX_3|MBX_2|MBX_1|MBX_0;
1343        } else {
1344                mcp->mb[0] = MBC_LIP_RESET;
1345                mcp->out_mb = MBX_3|MBX_2|MBX_1|MBX_0;
1346                if (HAS_EXTENDED_IDS(ha)) {
1347                        mcp->mb[1] = 0x00ff;
1348                        mcp->mb[10] = 0;
1349                        mcp->out_mb |= MBX_10;
1350                } else {
1351                        mcp->mb[1] = 0xff00;
1352                }
1353                mcp->mb[2] = ha->loop_reset_delay;
1354                mcp->mb[3] = 0;
1355        }
1356        mcp->in_mb = MBX_0;
1357        mcp->tov = 30;
1358        mcp->flags = 0;
1359        rval = qla2x00_mailbox_command(ha, mcp);
1360
1361        if (rval != QLA_SUCCESS) {
1362                /*EMPTY*/
1363                DEBUG2_3_11(printk("%s(%ld): failed=%x.\n",
1364                    __func__, ha->host_no, rval));
1365        } else {
1366                /*EMPTY*/
1367                DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
1368        }
1369
1370        return rval;
1371}
1372
1373/*
1374 * qla2x00_send_sns
1375 *      Send SNS command.
1376 *
1377 * Input:
1378 *      ha = adapter block pointer.
1379 *      sns = pointer for command.
1380 *      cmd_size = command size.
1381 *      buf_size = response/command size.
1382 *      TARGET_QUEUE_LOCK must be released.
1383 *      ADAPTER_STATE_LOCK must be released.
1384 *
1385 * Returns:
1386 *      qla2x00 local function return status code.
1387 *
1388 * Context:
1389 *      Kernel context.
1390 */
1391int
1392qla2x00_send_sns(scsi_qla_host_t *ha, dma_addr_t sns_phys_address,
1393    uint16_t cmd_size, size_t buf_size)
1394{
1395        int rval;
1396        mbx_cmd_t mc;
1397        mbx_cmd_t *mcp = &mc;
1398
1399        DEBUG11(printk("qla2x00_send_sns(%ld): entered.\n",
1400            ha->host_no));
1401
1402        DEBUG11(printk("qla2x00_send_sns: retry cnt=%d ratov=%d total "
1403            "tov=%d.\n", ha->retry_count, ha->login_timeout, mcp->tov));
1404
1405        mcp->mb[0] = MBC_SEND_SNS_COMMAND;
1406        mcp->mb[1] = cmd_size;
1407        mcp->mb[2] = MSW(sns_phys_address);
1408        mcp->mb[3] = LSW(sns_phys_address);
1409        mcp->mb[6] = MSW(MSD(sns_phys_address));
1410        mcp->mb[7] = LSW(MSD(sns_phys_address));
1411        mcp->out_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
1412        mcp->in_mb = MBX_0|MBX_1;
1413        mcp->buf_size = buf_size;
1414        mcp->flags = MBX_DMA_OUT|MBX_DMA_IN;
1415        mcp->tov = (ha->login_timeout * 2) + (ha->login_timeout / 2);
1416        rval = qla2x00_mailbox_command(ha, mcp);
1417
1418        if (rval != QLA_SUCCESS) {
1419                /*EMPTY*/
1420                DEBUG(printk("qla2x00_send_sns(%ld): failed=%x mb[0]=%x "
1421                    "mb[1]=%x.\n", ha->host_no, rval, mcp->mb[0], mcp->mb[1]));
1422                DEBUG2_3_11(printk("qla2x00_send_sns(%ld): failed=%x mb[0]=%x "
1423                    "mb[1]=%x.\n", ha->host_no, rval, mcp->mb[0], mcp->mb[1]));
1424        } else {
1425                /*EMPTY*/
1426                DEBUG11(printk("qla2x00_send_sns(%ld): done.\n", ha->host_no));
1427        }
1428
1429        return rval;
1430}
1431
1432int
1433qla24xx_login_fabric(scsi_qla_host_t *ha, uint16_t loop_id, uint8_t domain,
1434    uint8_t area, uint8_t al_pa, uint16_t *mb, uint8_t opt)
1435{
1436        int             rval;
1437
1438        struct logio_entry_24xx *lg;
1439        dma_addr_t      lg_dma;
1440        uint32_t        iop[2];
1441
1442        DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
1443
1444        lg = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &lg_dma);
1445        if (lg == NULL) {
1446                DEBUG2_3(printk("%s(%ld): failed to allocate Login IOCB.\n",
1447                    __func__, ha->host_no));
1448                return QLA_MEMORY_ALLOC_FAILED;
1449        }
1450        memset(lg, 0, sizeof(struct logio_entry_24xx));
1451
1452        lg->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1453        lg->entry_count = 1;
1454        lg->nport_handle = cpu_to_le16(loop_id);
1455        lg->control_flags = __constant_cpu_to_le16(LCF_COMMAND_PLOGI);
1456        if (opt & BIT_0)
1457                lg->control_flags |= __constant_cpu_to_le16(LCF_COND_PLOGI);
1458        if (opt & BIT_1)
1459                lg->control_flags |= __constant_cpu_to_le16(LCF_SKIP_PRLI);
1460        lg->port_id[0] = al_pa;
1461        lg->port_id[1] = area;
1462        lg->port_id[2] = domain;
1463        lg->vp_index = cpu_to_le16(ha->vp_idx);
1464        rval = qla2x00_issue_iocb(ha, lg, lg_dma, 0);
1465        if (rval != QLA_SUCCESS) {
1466                DEBUG2_3_11(printk("%s(%ld): failed to issue Login IOCB "
1467                    "(%x).\n", __func__, ha->host_no, rval));
1468        } else if (lg->entry_status != 0) {
1469                DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
1470                    "-- error status (%x).\n", __func__, ha->host_no,
1471                    lg->entry_status));
1472                rval = QLA_FUNCTION_FAILED;
1473        } else if (lg->comp_status != __constant_cpu_to_le16(CS_COMPLETE)) {
1474                iop[0] = le32_to_cpu(lg->io_parameter[0]);
1475                iop[1] = le32_to_cpu(lg->io_parameter[1]);
1476
1477                DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
1478                    "-- completion status (%x)  ioparam=%x/%x.\n", __func__,
1479                    ha->host_no, le16_to_cpu(lg->comp_status), iop[0],
1480                    iop[1]));
1481
1482                switch (iop[0]) {
1483                case LSC_SCODE_PORTID_USED:
1484                        mb[0] = MBS_PORT_ID_USED;
1485                        mb[1] = LSW(iop[1]);
1486                        break;
1487                case LSC_SCODE_NPORT_USED:
1488                        mb[0] = MBS_LOOP_ID_USED;
1489                        break;
1490                case LSC_SCODE_NOLINK:
1491                case LSC_SCODE_NOIOCB:
1492                case LSC_SCODE_NOXCB:
1493                case LSC_SCODE_CMD_FAILED:
1494                case LSC_SCODE_NOFABRIC:
1495                case LSC_SCODE_FW_NOT_READY:
1496                case LSC_SCODE_NOT_LOGGED_IN:
1497                case LSC_SCODE_NOPCB:
1498                case LSC_SCODE_ELS_REJECT:
1499                case LSC_SCODE_CMD_PARAM_ERR:
1500                case LSC_SCODE_NONPORT:
1501                case LSC_SCODE_LOGGED_IN:
1502                case LSC_SCODE_NOFLOGI_ACC:
1503                default:
1504                        mb[0] = MBS_COMMAND_ERROR;
1505                        break;
1506                }
1507        } else {
1508                DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
1509
1510                iop[0] = le32_to_cpu(lg->io_parameter[0]);
1511
1512                mb[0] = MBS_COMMAND_COMPLETE;
1513                mb[1] = 0;
1514                if (iop[0] & BIT_4) {
1515                        if (iop[0] & BIT_8)
1516                                mb[1] |= BIT_1;
1517                } else
1518                        mb[1] = BIT_0;
1519
1520                /* Passback COS information. */
1521                mb[10] = 0;
1522                if (lg->io_parameter[7] || lg->io_parameter[8])
1523                        mb[10] |= BIT_0;        /* Class 2. */
1524                if (lg->io_parameter[9] || lg->io_parameter[10])
1525                        mb[10] |= BIT_1;        /* Class 3. */
1526        }
1527
1528        dma_pool_free(ha->s_dma_pool, lg, lg_dma);
1529
1530        return rval;
1531}
1532
1533/*
1534 * qla2x00_login_fabric
1535 *      Issue login fabric port mailbox command.
1536 *
1537 * Input:
1538 *      ha = adapter block pointer.
1539 *      loop_id = device loop ID.
1540 *      domain = device domain.
1541 *      area = device area.
1542 *      al_pa = device AL_PA.
1543 *      status = pointer for return status.
1544 *      opt = command options.
1545 *      TARGET_QUEUE_LOCK must be released.
1546 *      ADAPTER_STATE_LOCK must be released.
1547 *
1548 * Returns:
1549 *      qla2x00 local function return status code.
1550 *
1551 * Context:
1552 *      Kernel context.
1553 */
1554int
1555qla2x00_login_fabric(scsi_qla_host_t *ha, uint16_t loop_id, uint8_t domain,
1556    uint8_t area, uint8_t al_pa, uint16_t *mb, uint8_t opt)
1557{
1558        int rval;
1559        mbx_cmd_t mc;
1560        mbx_cmd_t *mcp = &mc;
1561
1562        DEBUG11(printk("qla2x00_login_fabric(%ld): entered.\n", ha->host_no));
1563
1564        mcp->mb[0] = MBC_LOGIN_FABRIC_PORT;
1565        mcp->out_mb = MBX_3|MBX_2|MBX_1|MBX_0;
1566        if (HAS_EXTENDED_IDS(ha)) {
1567                mcp->mb[1] = loop_id;
1568                mcp->mb[10] = opt;
1569                mcp->out_mb |= MBX_10;
1570        } else {
1571                mcp->mb[1] = (loop_id << 8) | opt;
1572        }
1573        mcp->mb[2] = domain;
1574        mcp->mb[3] = area << 8 | al_pa;
1575
1576        mcp->in_mb = MBX_7|MBX_6|MBX_2|MBX_1|MBX_0;
1577        mcp->tov = (ha->login_timeout * 2) + (ha->login_timeout / 2);
1578        mcp->flags = 0;
1579        rval = qla2x00_mailbox_command(ha, mcp);
1580
1581        /* Return mailbox statuses. */
1582        if (mb != NULL) {
1583                mb[0] = mcp->mb[0];
1584                mb[1] = mcp->mb[1];
1585                mb[2] = mcp->mb[2];
1586                mb[6] = mcp->mb[6];
1587                mb[7] = mcp->mb[7];
1588                /* COS retrieved from Get-Port-Database mailbox command. */
1589                mb[10] = 0;
1590        }
1591
1592        if (rval != QLA_SUCCESS) {
1593                /* RLU tmp code: need to change main mailbox_command function to
1594                 * return ok even when the mailbox completion value is not
1595                 * SUCCESS. The caller needs to be responsible to interpret
1596                 * the return values of this mailbox command if we're not
1597                 * to change too much of the existing code.
1598                 */
1599                if (mcp->mb[0] == 0x4001 || mcp->mb[0] == 0x4002 ||
1600                    mcp->mb[0] == 0x4003 || mcp->mb[0] == 0x4005 ||
1601                    mcp->mb[0] == 0x4006)
1602                        rval = QLA_SUCCESS;
1603
1604                /*EMPTY*/
1605                DEBUG2_3_11(printk("qla2x00_login_fabric(%ld): failed=%x "
1606                    "mb[0]=%x mb[1]=%x mb[2]=%x.\n", ha->host_no, rval,
1607                    mcp->mb[0], mcp->mb[1], mcp->mb[2]));
1608        } else {
1609                /*EMPTY*/
1610                DEBUG11(printk("qla2x00_login_fabric(%ld): done.\n",
1611                    ha->host_no));
1612        }
1613
1614        return rval;
1615}
1616
1617/*
1618 * qla2x00_login_local_device
1619 *           Issue login loop port mailbox command.
1620 *
1621 * Input:
1622 *           ha = adapter block pointer.
1623 *           loop_id = device loop ID.
1624 *           opt = command options.
1625 *
1626 * Returns:
1627 *            Return status code.
1628 *
1629 * Context:
1630 *            Kernel context.
1631 *
1632 */
1633int
1634qla2x00_login_local_device(scsi_qla_host_t *ha, fc_port_t *fcport,
1635    uint16_t *mb_ret, uint8_t opt)
1636{
1637        int rval;
1638        mbx_cmd_t mc;
1639        mbx_cmd_t *mcp = &mc;
1640
1641        if (IS_FWI2_CAPABLE(ha))
1642                return qla24xx_login_fabric(ha, fcport->loop_id,
1643                    fcport->d_id.b.domain, fcport->d_id.b.area,
1644                    fcport->d_id.b.al_pa, mb_ret, opt);
1645
1646        DEBUG3(printk("%s(%ld): entered.\n", __func__, ha->host_no));
1647
1648        mcp->mb[0] = MBC_LOGIN_LOOP_PORT;
1649        if (HAS_EXTENDED_IDS(ha))
1650                mcp->mb[1] = fcport->loop_id;
1651        else
1652                mcp->mb[1] = fcport->loop_id << 8;
1653        mcp->mb[2] = opt;
1654        mcp->out_mb = MBX_2|MBX_1|MBX_0;
1655        mcp->in_mb = MBX_7|MBX_6|MBX_1|MBX_0;
1656        mcp->tov = (ha->login_timeout * 2) + (ha->login_timeout / 2);
1657        mcp->flags = 0;
1658        rval = qla2x00_mailbox_command(ha, mcp);
1659
1660        /* Return mailbox statuses. */
1661        if (mb_ret != NULL) {
1662                mb_ret[0] = mcp->mb[0];
1663                mb_ret[1] = mcp->mb[1];
1664                mb_ret[6] = mcp->mb[6];
1665                mb_ret[7] = mcp->mb[7];
1666        }
1667
1668        if (rval != QLA_SUCCESS) {
1669                /* AV tmp code: need to change main mailbox_command function to
1670                 * return ok even when the mailbox completion value is not
1671                 * SUCCESS. The caller needs to be responsible to interpret
1672                 * the return values of this mailbox command if we're not
1673                 * to change too much of the existing code.
1674                 */
1675                if (mcp->mb[0] == 0x4005 || mcp->mb[0] == 0x4006)
1676                        rval = QLA_SUCCESS;
1677
1678                DEBUG(printk("%s(%ld): failed=%x mb[0]=%x mb[1]=%x "
1679                    "mb[6]=%x mb[7]=%x.\n", __func__, ha->host_no, rval,
1680                    mcp->mb[0], mcp->mb[1], mcp->mb[6], mcp->mb[7]));
1681                DEBUG2_3(printk("%s(%ld): failed=%x mb[0]=%x mb[1]=%x "
1682                    "mb[6]=%x mb[7]=%x.\n", __func__, ha->host_no, rval,
1683                    mcp->mb[0], mcp->mb[1], mcp->mb[6], mcp->mb[7]));
1684        } else {
1685                /*EMPTY*/
1686                DEBUG3(printk("%s(%ld): done.\n", __func__, ha->host_no));
1687        }
1688
1689        return (rval);
1690}
1691
1692int
1693qla24xx_fabric_logout(scsi_qla_host_t *ha, uint16_t loop_id, uint8_t domain,
1694    uint8_t area, uint8_t al_pa)
1695{
1696        int             rval;
1697        struct logio_entry_24xx *lg;
1698        dma_addr_t      lg_dma;
1699
1700        DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
1701
1702        lg = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &lg_dma);
1703        if (lg == NULL) {
1704                DEBUG2_3(printk("%s(%ld): failed to allocate Logout IOCB.\n",
1705                    __func__, ha->host_no));
1706                return QLA_MEMORY_ALLOC_FAILED;
1707        }
1708        memset(lg, 0, sizeof(struct logio_entry_24xx));
1709
1710        lg->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1711        lg->entry_count = 1;
1712        lg->nport_handle = cpu_to_le16(loop_id);
1713        lg->control_flags =
1714            __constant_cpu_to_le16(LCF_COMMAND_LOGO|LCF_IMPL_LOGO);
1715        lg->port_id[0] = al_pa;
1716        lg->port_id[1] = area;
1717        lg->port_id[2] = domain;
1718        lg->vp_index = cpu_to_le16(ha->vp_idx);
1719        rval = qla2x00_issue_iocb(ha, lg, lg_dma, 0);
1720        if (rval != QLA_SUCCESS) {
1721                DEBUG2_3_11(printk("%s(%ld): failed to issue Logout IOCB "
1722                    "(%x).\n", __func__, ha->host_no, rval));
1723        } else if (lg->entry_status != 0) {
1724                DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
1725                    "-- error status (%x).\n", __func__, ha->host_no,
1726                    lg->entry_status));
1727                rval = QLA_FUNCTION_FAILED;
1728        } else if (lg->comp_status != __constant_cpu_to_le16(CS_COMPLETE)) {
1729                DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
1730                    "-- completion status (%x)  ioparam=%x/%x.\n", __func__,
1731                    ha->host_no, le16_to_cpu(lg->comp_status),
1732                    le32_to_cpu(lg->io_parameter[0]),
1733                    le32_to_cpu(lg->io_parameter[1])));
1734        } else {
1735                /*EMPTY*/
1736                DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
1737        }
1738
1739        dma_pool_free(ha->s_dma_pool, lg, lg_dma);
1740
1741        return rval;
1742}
1743
1744/*
1745 * qla2x00_fabric_logout
1746 *      Issue logout fabric port mailbox command.
1747 *
1748 * Input:
1749 *      ha = adapter block pointer.
1750 *      loop_id = device loop ID.
1751 *      TARGET_QUEUE_LOCK must be released.
1752 *      ADAPTER_STATE_LOCK must be released.
1753 *
1754 * Returns:
1755 *      qla2x00 local function return status code.
1756 *
1757 * Context:
1758 *      Kernel context.
1759 */
1760int
1761qla2x00_fabric_logout(scsi_qla_host_t *ha, uint16_t loop_id, uint8_t domain,
1762    uint8_t area, uint8_t al_pa)
1763{
1764        int rval;
1765        mbx_cmd_t mc;
1766        mbx_cmd_t *mcp = &mc;
1767
1768        DEBUG11(printk("qla2x00_fabric_logout(%ld): entered.\n",
1769            ha->host_no));
1770
1771        mcp->mb[0] = MBC_LOGOUT_FABRIC_PORT;
1772        mcp->out_mb = MBX_1|MBX_0;
1773        if (HAS_EXTENDED_IDS(ha)) {
1774                mcp->mb[1] = loop_id;
1775                mcp->mb[10] = 0;
1776                mcp->out_mb |= MBX_10;
1777        } else {
1778                mcp->mb[1] = loop_id << 8;
1779        }
1780
1781        mcp->in_mb = MBX_1|MBX_0;
1782        mcp->tov = 30;
1783        mcp->flags = 0;
1784        rval = qla2x00_mailbox_command(ha, mcp);
1785
1786        if (rval != QLA_SUCCESS) {
1787                /*EMPTY*/
1788                DEBUG2_3_11(printk("qla2x00_fabric_logout(%ld): failed=%x "
1789                    "mbx1=%x.\n", ha->host_no, rval, mcp->mb[1]));
1790        } else {
1791                /*EMPTY*/
1792                DEBUG11(printk("qla2x00_fabric_logout(%ld): done.\n",
1793                    ha->host_no));
1794        }
1795
1796        return rval;
1797}
1798
1799/*
1800 * qla2x00_full_login_lip
1801 *      Issue full login LIP mailbox command.
1802 *
1803 * Input:
1804 *      ha = adapter block pointer.
1805 *      TARGET_QUEUE_LOCK must be released.
1806 *      ADAPTER_STATE_LOCK must be released.
1807 *
1808 * Returns:
1809 *      qla2x00 local function return status code.
1810 *
1811 * Context:
1812 *      Kernel context.
1813 */
1814int
1815qla2x00_full_login_lip(scsi_qla_host_t *ha)
1816{
1817        int rval;
1818        mbx_cmd_t mc;
1819        mbx_cmd_t *mcp = &mc;
1820
1821        DEBUG11(printk("qla2x00_full_login_lip(%ld): entered.\n",
1822            ha->host_no));
1823
1824        mcp->mb[0] = MBC_LIP_FULL_LOGIN;
1825        mcp->mb[1] = IS_FWI2_CAPABLE(ha) ? BIT_3: 0;
1826        mcp->mb[2] = 0;
1827        mcp->mb[3] = 0;
1828        mcp->out_mb = MBX_3|MBX_2|MBX_1|MBX_0;
1829        mcp->in_mb = MBX_0;
1830        mcp->tov = 30;
1831        mcp->flags = 0;
1832        rval = qla2x00_mailbox_command(ha, mcp);
1833
1834        if (rval != QLA_SUCCESS) {
1835                /*EMPTY*/
1836                DEBUG2_3_11(printk("qla2x00_full_login_lip(%ld): failed=%x.\n",
1837                    ha->host_no, rval));
1838        } else {
1839                /*EMPTY*/
1840                DEBUG11(printk("qla2x00_full_login_lip(%ld): done.\n",
1841                    ha->host_no));
1842        }
1843
1844        return rval;
1845}
1846
1847/*
1848 * qla2x00_get_id_list
1849 *
1850 * Input:
1851 *      ha = adapter block pointer.
1852 *
1853 * Returns:
1854 *      qla2x00 local function return status code.
1855 *
1856 * Context:
1857 *      Kernel context.
1858 */
1859int
1860qla2x00_get_id_list(scsi_qla_host_t *ha, void *id_list, dma_addr_t id_list_dma,
1861    uint16_t *entries)
1862{
1863        int rval;
1864        mbx_cmd_t mc;
1865        mbx_cmd_t *mcp = &mc;
1866
1867        DEBUG11(printk("qla2x00_get_id_list(%ld): entered.\n",
1868            ha->host_no));
1869
1870        if (id_list == NULL)
1871                return QLA_FUNCTION_FAILED;
1872
1873        mcp->mb[0] = MBC_GET_ID_LIST;
1874        mcp->out_mb = MBX_0;
1875        if (IS_FWI2_CAPABLE(ha)) {
1876                mcp->mb[2] = MSW(id_list_dma);
1877                mcp->mb[3] = LSW(id_list_dma);
1878                mcp->mb[6] = MSW(MSD(id_list_dma));
1879                mcp->mb[7] = LSW(MSD(id_list_dma));
1880                mcp->mb[8] = 0;
1881                mcp->mb[9] = ha->vp_idx;
1882                mcp->out_mb |= MBX_9|MBX_8|MBX_7|MBX_6|MBX_3|MBX_2;
1883        } else {
1884                mcp->mb[1] = MSW(id_list_dma);
1885                mcp->mb[2] = LSW(id_list_dma);
1886                mcp->mb[3] = MSW(MSD(id_list_dma));
1887                mcp->mb[6] = LSW(MSD(id_list_dma));
1888                mcp->out_mb |= MBX_6|MBX_3|MBX_2|MBX_1;
1889        }
1890        mcp->in_mb = MBX_1|MBX_0;
1891        mcp->tov = 30;
1892        mcp->flags = 0;
1893        rval = qla2x00_mailbox_command(ha, mcp);
1894
1895        if (rval != QLA_SUCCESS) {
1896                /*EMPTY*/
1897                DEBUG2_3_11(printk("qla2x00_get_id_list(%ld): failed=%x.\n",
1898                    ha->host_no, rval));
1899        } else {
1900                *entries = mcp->mb[1];
1901                DEBUG11(printk("qla2x00_get_id_list(%ld): done.\n",
1902                    ha->host_no));
1903        }
1904
1905        return rval;
1906}
1907
1908/*
1909 * qla2x00_get_resource_cnts
1910 *      Get current firmware resource counts.
1911 *
1912 * Input:
1913 *      ha = adapter block pointer.
1914 *
1915 * Returns:
1916 *      qla2x00 local function return status code.
1917 *
1918 * Context:
1919 *      Kernel context.
1920 */
1921int
1922qla2x00_get_resource_cnts(scsi_qla_host_t *ha, uint16_t *cur_xchg_cnt,
1923    uint16_t *orig_xchg_cnt, uint16_t *cur_iocb_cnt,
1924    uint16_t *orig_iocb_cnt, uint16_t *max_npiv_vports)
1925{
1926        int rval;
1927        mbx_cmd_t mc;
1928        mbx_cmd_t *mcp = &mc;
1929
1930        DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
1931
1932        mcp->mb[0] = MBC_GET_RESOURCE_COUNTS;
1933        mcp->out_mb = MBX_0;
1934        mcp->in_mb = MBX_11|MBX_10|MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
1935        mcp->tov = 30;
1936        mcp->flags = 0;
1937        rval = qla2x00_mailbox_command(ha, mcp);
1938
1939        if (rval != QLA_SUCCESS) {
1940                /*EMPTY*/
1941                DEBUG2_3_11(printk("%s(%ld): failed = %x.\n", __func__,
1942                    ha->host_no, mcp->mb[0]));
1943        } else {
1944                DEBUG11(printk("%s(%ld): done. mb1=%x mb2=%x mb3=%x mb6=%x "
1945                    "mb7=%x mb10=%x mb11=%x.\n", __func__, ha->host_no,
1946                    mcp->mb[1], mcp->mb[2], mcp->mb[3], mcp->mb[6], mcp->mb[7],
1947                    mcp->mb[10], mcp->mb[11]));
1948
1949                if (cur_xchg_cnt)
1950                        *cur_xchg_cnt = mcp->mb[3];
1951                if (orig_xchg_cnt)
1952                        *orig_xchg_cnt = mcp->mb[6];
1953                if (cur_iocb_cnt)
1954                        *cur_iocb_cnt = mcp->mb[7];
1955                if (orig_iocb_cnt)
1956                        *orig_iocb_cnt = mcp->mb[10];
1957                if (max_npiv_vports)
1958                        *max_npiv_vports = mcp->mb[11];
1959        }
1960
1961        return (rval);
1962}
1963
1964#if defined(QL_DEBUG_LEVEL_3)
1965/*
1966 * qla2x00_get_fcal_position_map
1967 *      Get FCAL (LILP) position map using mailbox command
1968 *
1969 * Input:
1970 *      ha = adapter state pointer.
1971 *      pos_map = buffer pointer (can be NULL).
1972 *
1973 * Returns:
1974 *      qla2x00 local function return status code.
1975 *
1976 * Context:
1977 *      Kernel context.
1978 */
1979int
1980qla2x00_get_fcal_position_map(scsi_qla_host_t *ha, char *pos_map)
1981{
1982        int rval;
1983        mbx_cmd_t mc;
1984        mbx_cmd_t *mcp = &mc;
1985        char *pmap;
1986        dma_addr_t pmap_dma;
1987
1988        pmap = dma_pool_alloc(ha->s_dma_pool, GFP_ATOMIC, &pmap_dma);
1989        if (pmap  == NULL) {
1990                DEBUG2_3_11(printk("%s(%ld): **** Mem Alloc Failed ****",
1991                    __func__, ha->host_no));
1992                return QLA_MEMORY_ALLOC_FAILED;
1993        }
1994        memset(pmap, 0, FCAL_MAP_SIZE);
1995
1996        mcp->mb[0] = MBC_GET_FC_AL_POSITION_MAP;
1997        mcp->mb[2] = MSW(pmap_dma);
1998        mcp->mb[3] = LSW(pmap_dma);
1999        mcp->mb[6] = MSW(MSD(pmap_dma));
2000        mcp->mb[7] = LSW(MSD(pmap_dma));
2001        mcp->out_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_0;
2002        mcp->in_mb = MBX_1|MBX_0;
2003        mcp->buf_size = FCAL_MAP_SIZE;
2004        mcp->flags = MBX_DMA_IN;
2005        mcp->tov = (ha->login_timeout * 2) + (ha->login_timeout / 2);
2006        rval = qla2x00_mailbox_command(ha, mcp);
2007
2008        if (rval == QLA_SUCCESS) {
2009                DEBUG11(printk("%s(%ld): (mb0=%x/mb1=%x) FC/AL Position Map "
2010                    "size (%x)\n", __func__, ha->host_no, mcp->mb[0],
2011                    mcp->mb[1], (unsigned)pmap[0]));
2012                DEBUG11(qla2x00_dump_buffer(pmap, pmap[0] + 1));
2013
2014                if (pos_map)
2015                        memcpy(pos_map, pmap, FCAL_MAP_SIZE);
2016        }
2017        dma_pool_free(ha->s_dma_pool, pmap, pmap_dma);
2018
2019        if (rval != QLA_SUCCESS) {
2020                DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
2021                    ha->host_no, rval));
2022        } else {
2023                DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
2024        }
2025
2026        return rval;
2027}
2028#endif
2029
2030/*
2031 * qla2x00_get_link_status
2032 *
2033 * Input:
2034 *      ha = adapter block pointer.
2035 *      loop_id = device loop ID.
2036 *      ret_buf = pointer to link status return buffer.
2037 *
2038 * Returns:
2039 *      0 = success.
2040 *      BIT_0 = mem alloc error.
2041 *      BIT_1 = mailbox error.
2042 */
2043int
2044qla2x00_get_link_status(scsi_qla_host_t *ha, uint16_t loop_id,
2045    link_stat_t *ret_buf, uint16_t *status)
2046{
2047        int rval;
2048        mbx_cmd_t mc;
2049        mbx_cmd_t *mcp = &mc;
2050        link_stat_t *stat_buf;
2051        dma_addr_t stat_buf_dma;
2052
2053        DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
2054
2055        stat_buf = dma_pool_alloc(ha->s_dma_pool, GFP_ATOMIC, &stat_buf_dma);
2056        if (stat_buf == NULL) {
2057                DEBUG2_3_11(printk("%s(%ld): Failed to allocate memory.\n",
2058                    __func__, ha->host_no));
2059                return BIT_0;
2060        }
2061        memset(stat_buf, 0, sizeof(link_stat_t));
2062
2063        mcp->mb[0] = MBC_GET_LINK_STATUS;
2064        mcp->mb[2] = MSW(stat_buf_dma);
2065        mcp->mb[3] = LSW(stat_buf_dma);
2066        mcp->mb[6] = MSW(MSD(stat_buf_dma));
2067        mcp->mb[7] = LSW(MSD(stat_buf_dma));
2068        mcp->out_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_0;
2069        mcp->in_mb = MBX_0;
2070        if (IS_FWI2_CAPABLE(ha)) {
2071                mcp->mb[1] = loop_id;
2072                mcp->mb[4] = 0;
2073                mcp->mb[10] = 0;
2074                mcp->out_mb |= MBX_10|MBX_4|MBX_1;
2075                mcp->in_mb |= MBX_1;
2076        } else if (HAS_EXTENDED_IDS(ha)) {
2077                mcp->mb[1] = loop_id;
2078                mcp->mb[10] = 0;
2079                mcp->out_mb |= MBX_10|MBX_1;
2080        } else {
2081                mcp->mb[1] = loop_id << 8;
2082                mcp->out_mb |= MBX_1;
2083        }
2084        mcp->tov = 30;
2085        mcp->flags = IOCTL_CMD;
2086        rval = qla2x00_mailbox_command(ha, mcp);
2087
2088        if (rval == QLA_SUCCESS) {
2089                if (mcp->mb[0] != MBS_COMMAND_COMPLETE) {
2090                        DEBUG2_3_11(printk("%s(%ld): cmd failed. mbx0=%x.\n",
2091                            __func__, ha->host_no, mcp->mb[0]));
2092                        status[0] = mcp->mb[0];
2093                        rval = BIT_1;
2094                } else {
2095                        /* copy over data -- firmware data is LE. */
2096                        ret_buf->link_fail_cnt =
2097                            le32_to_cpu(stat_buf->link_fail_cnt);
2098                        ret_buf->loss_sync_cnt =
2099                            le32_to_cpu(stat_buf->loss_sync_cnt);
2100                        ret_buf->loss_sig_cnt =
2101                            le32_to_cpu(stat_buf->loss_sig_cnt);
2102                        ret_buf->prim_seq_err_cnt =
2103                            le32_to_cpu(stat_buf->prim_seq_err_cnt);
2104                        ret_buf->inval_xmit_word_cnt =
2105                            le32_to_cpu(stat_buf->inval_xmit_word_cnt);
2106                        ret_buf->inval_crc_cnt =
2107                            le32_to_cpu(stat_buf->inval_crc_cnt);
2108
2109                        DEBUG11(printk("%s(%ld): stat dump: fail_cnt=%d "
2110                            "loss_sync=%d loss_sig=%d seq_err=%d "
2111                            "inval_xmt_word=%d inval_crc=%d.\n", __func__,
2112                            ha->host_no, stat_buf->link_fail_cnt,
2113                            stat_buf->loss_sync_cnt, stat_buf->loss_sig_cnt,
2114                            stat_buf->prim_seq_err_cnt,
2115                            stat_buf->inval_xmit_word_cnt,
2116                            stat_buf->inval_crc_cnt));
2117                }
2118        } else {
2119                /* Failed. */
2120                DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
2121                    ha->host_no, rval));
2122                rval = BIT_1;
2123        }
2124
2125        dma_pool_free(ha->s_dma_pool, stat_buf, stat_buf_dma);
2126
2127        return rval;
2128}
2129
2130int
2131qla24xx_get_isp_stats(scsi_qla_host_t *ha, uint32_t *dwbuf, uint32_t dwords,
2132    uint16_t *status)
2133{
2134        int rval;
2135        mbx_cmd_t mc;
2136        mbx_cmd_t *mcp = &mc;
2137        uint32_t *sbuf, *siter;
2138        dma_addr_t sbuf_dma;
2139
2140        DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
2141
2142        if (dwords > (DMA_POOL_SIZE / 4)) {
2143                DEBUG2_3_11(printk("%s(%ld): Unabled to retrieve %d DWORDs "
2144                    "(max %d).\n", __func__, ha->host_no, dwords,
2145                    DMA_POOL_SIZE / 4));
2146                return BIT_0;
2147        }
2148        sbuf = dma_pool_alloc(ha->s_dma_pool, GFP_ATOMIC, &sbuf_dma);
2149        if (sbuf == NULL) {
2150                DEBUG2_3_11(printk("%s(%ld): Failed to allocate memory.\n",
2151                    __func__, ha->host_no));
2152                return BIT_0;
2153        }
2154        memset(sbuf, 0, DMA_POOL_SIZE);
2155
2156        mcp->mb[0] = MBC_GET_LINK_PRIV_STATS;
2157        mcp->mb[2] = MSW(sbuf_dma);
2158        mcp->mb[3] = LSW(sbuf_dma);
2159        mcp->mb[6] = MSW(MSD(sbuf_dma));
2160        mcp->mb[7] = LSW(MSD(sbuf_dma));
2161        mcp->mb[8] = dwords;
2162        mcp->mb[10] = 0;
2163        mcp->out_mb = MBX_10|MBX_8|MBX_7|MBX_6|MBX_3|MBX_2|MBX_0;
2164        mcp->in_mb = MBX_2|MBX_1|MBX_0;
2165        mcp->tov = 30;
2166        mcp->flags = IOCTL_CMD;
2167        rval = qla2x00_mailbox_command(ha, mcp);
2168
2169        if (rval == QLA_SUCCESS) {
2170                if (mcp->mb[0] != MBS_COMMAND_COMPLETE) {
2171                        DEBUG2_3_11(printk("%s(%ld): cmd failed. mbx0=%x.\n",
2172                            __func__, ha->host_no, mcp->mb[0]));
2173                        status[0] = mcp->mb[0];
2174                        rval = BIT_1;
2175                } else {
2176                        /* Copy over data -- firmware data is LE. */
2177                        siter = sbuf;
2178                        while (dwords--)
2179                                *dwbuf++ = le32_to_cpu(*siter++);
2180                }
2181        } else {
2182                /* Failed. */
2183                DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
2184                    ha->host_no, rval));
2185                rval = BIT_1;
2186        }
2187
2188        dma_pool_free(ha->s_dma_pool, sbuf, sbuf_dma);
2189
2190        return rval;
2191}
2192
2193int
2194qla24xx_abort_command(scsi_qla_host_t *ha, srb_t *sp)
2195{
2196        int             rval;
2197        fc_port_t       *fcport;
2198        unsigned long   flags = 0;
2199
2200        struct abort_entry_24xx *abt;
2201        dma_addr_t      abt_dma;
2202        uint32_t        handle;
2203
2204        DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
2205
2206        fcport = sp->fcport;
2207
2208        spin_lock_irqsave(&ha->hardware_lock, flags);
2209        for (handle = 1; handle < MAX_OUTSTANDING_COMMANDS; handle++) {
2210                if (ha->outstanding_cmds[handle] == sp)
2211                        break;
2212        }
2213        spin_unlock_irqrestore(&ha->hardware_lock, flags);
2214        if (handle == MAX_OUTSTANDING_COMMANDS) {
2215                /* Command not found. */
2216                return QLA_FUNCTION_FAILED;
2217        }
2218
2219        abt = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &abt_dma);
2220        if (abt == NULL) {
2221                DEBUG2_3(printk("%s(%ld): failed to allocate Abort IOCB.\n",
2222                    __func__, ha->host_no));
2223                return QLA_MEMORY_ALLOC_FAILED;
2224        }
2225        memset(abt, 0, sizeof(struct abort_entry_24xx));
2226
2227        abt->entry_type = ABORT_IOCB_TYPE;
2228        abt->entry_count = 1;
2229        abt->nport_handle = cpu_to_le16(fcport->loop_id);
2230        abt->handle_to_abort = handle;
2231        abt->port_id[0] = fcport->d_id.b.al_pa;
2232        abt->port_id[1] = fcport->d_id.b.area;
2233        abt->port_id[2] = fcport->d_id.b.domain;
2234        abt->vp_index = fcport->vp_idx;
2235        rval = qla2x00_issue_iocb(ha, abt, abt_dma, 0);
2236        if (rval != QLA_SUCCESS) {
2237                DEBUG2_3_11(printk("%s(%ld): failed to issue IOCB (%x).\n",
2238                    __func__, ha->host_no, rval));
2239        } else if (abt->entry_status != 0) {
2240                DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
2241                    "-- error status (%x).\n", __func__, ha->host_no,
2242                    abt->entry_status));
2243                rval = QLA_FUNCTION_FAILED;
2244        } else if (abt->nport_handle != __constant_cpu_to_le16(0)) {
2245                DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
2246                    "-- completion status (%x).\n", __func__, ha->host_no,
2247                    le16_to_cpu(abt->nport_handle)));
2248                rval = QLA_FUNCTION_FAILED;
2249        } else {
2250                DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
2251                sp->flags |= SRB_ABORT_PENDING;
2252        }
2253
2254        dma_pool_free(ha->s_dma_pool, abt, abt_dma);
2255
2256        return rval;
2257}
2258
2259struct tsk_mgmt_cmd {
2260        union {
2261                struct tsk_mgmt_entry tsk;
2262                struct sts_entry_24xx sts;
2263        } p;
2264};
2265
2266int
2267qla24xx_abort_target(fc_port_t *fcport)
2268{
2269        int             rval;
2270        struct tsk_mgmt_cmd *tsk;
2271        dma_addr_t      tsk_dma;
2272        scsi_qla_host_t *ha, *pha;
2273
2274        if (fcport == NULL)
2275                return 0;
2276
2277        DEBUG11(printk("%s(%ld): entered.\n", __func__, fcport->ha->host_no));
2278
2279        ha = fcport->ha;
2280        pha = to_qla_parent(ha);
2281        tsk = dma_pool_alloc(pha->s_dma_pool, GFP_KERNEL, &tsk_dma);
2282        if (tsk == NULL) {
2283                DEBUG2_3(printk("%s(%ld): failed to allocate Task Management "
2284                    "IOCB.\n", __func__, ha->host_no));
2285                return QLA_MEMORY_ALLOC_FAILED;
2286        }
2287        memset(tsk, 0, sizeof(struct tsk_mgmt_cmd));
2288
2289        tsk->p.tsk.entry_type = TSK_MGMT_IOCB_TYPE;
2290        tsk->p.tsk.entry_count = 1;
2291        tsk->p.tsk.nport_handle = cpu_to_le16(fcport->loop_id);
2292        tsk->p.tsk.timeout = __constant_cpu_to_le16(25);
2293        tsk->p.tsk.control_flags = __constant_cpu_to_le32(TCF_TARGET_RESET);
2294        tsk->p.tsk.port_id[0] = fcport->d_id.b.al_pa;
2295        tsk->p.tsk.port_id[1] = fcport->d_id.b.area;
2296        tsk->p.tsk.port_id[2] = fcport->d_id.b.domain;
2297        tsk->p.tsk.vp_index = fcport->vp_idx;
2298
2299        rval = qla2x00_issue_iocb(ha, tsk, tsk_dma, 0);
2300        if (rval != QLA_SUCCESS) {
2301                DEBUG2_3_11(printk("%s(%ld): failed to issue Target Reset IOCB "
2302                    "(%x).\n", __func__, ha->host_no, rval));
2303                goto atarget_done;
2304        } else if (tsk->p.sts.entry_status != 0) {
2305                DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
2306                    "-- error status (%x).\n", __func__, ha->host_no,
2307                    tsk->p.sts.entry_status));
2308                rval = QLA_FUNCTION_FAILED;
2309                goto atarget_done;
2310        } else if (tsk->p.sts.comp_status !=
2311            __constant_cpu_to_le16(CS_COMPLETE)) {
2312                DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
2313                    "-- completion status (%x).\n", __func__,
2314                    ha->host_no, le16_to_cpu(tsk->p.sts.comp_status)));
2315                rval = QLA_FUNCTION_FAILED;
2316                goto atarget_done;
2317        }
2318
2319        /* Issue marker IOCB. */
2320        rval = qla2x00_marker(ha, fcport->loop_id, 0, MK_SYNC_ID);
2321        if (rval != QLA_SUCCESS) {
2322                DEBUG2_3_11(printk("%s(%ld): failed to issue Marker IOCB "
2323                    "(%x).\n", __func__, ha->host_no, rval));
2324        } else {
2325                DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
2326        }
2327
2328atarget_done:
2329        dma_pool_free(pha->s_dma_pool, tsk, tsk_dma);
2330
2331        return rval;
2332}
2333
2334int
2335qla2x00_system_error(scsi_qla_host_t *ha)
2336{
2337        int rval;
2338        mbx_cmd_t mc;
2339        mbx_cmd_t *mcp = &mc;
2340
2341        if (!IS_FWI2_CAPABLE(ha))
2342                return QLA_FUNCTION_FAILED;
2343
2344        DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
2345
2346        mcp->mb[0] = MBC_GEN_SYSTEM_ERROR;
2347        mcp->out_mb = MBX_0;
2348        mcp->in_mb = MBX_0;
2349        mcp->tov = 5;
2350        mcp->flags = 0;
2351        rval = qla2x00_mailbox_command(ha, mcp);
2352
2353        if (rval != QLA_SUCCESS) {
2354                DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
2355                    ha->host_no, rval));
2356        } else {
2357                DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
2358        }
2359
2360        return rval;
2361}
2362
2363/**
2364 * qla2x00_get_serdes_params() -
2365 * @ha: HA context
2366 *
2367 * Returns
2368 */
2369int
2370qla2x00_get_serdes_params(scsi_qla_host_t *ha, uint16_t *sw_em_1g,
2371    uint16_t *sw_em_2g, uint16_t *sw_em_4g)
2372{
2373        int rval;
2374        mbx_cmd_t mc;
2375        mbx_cmd_t *mcp = &mc;
2376
2377        DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
2378
2379        mcp->mb[0] = MBC_SERDES_PARAMS;
2380        mcp->mb[1] = 0;
2381        mcp->out_mb = MBX_1|MBX_0;
2382        mcp->in_mb = MBX_4|MBX_3|MBX_2|MBX_0;
2383        mcp->tov = 30;
2384        mcp->flags = 0;
2385        rval = qla2x00_mailbox_command(ha, mcp);
2386
2387        if (rval != QLA_SUCCESS) {
2388                /*EMPTY*/
2389                DEBUG2_3_11(printk("%s(%ld): failed=%x (%x).\n", __func__,
2390                    ha->host_no, rval, mcp->mb[0]));
2391        } else {
2392                DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
2393
2394                if (sw_em_1g)
2395                        *sw_em_1g = mcp->mb[2];
2396                if (sw_em_2g)
2397                        *sw_em_2g = mcp->mb[3];
2398                if (sw_em_4g)
2399                        *sw_em_4g = mcp->mb[4];
2400        }
2401
2402        return rval;
2403}
2404
2405/**
2406 * qla2x00_set_serdes_params() -
2407 * @ha: HA context
2408 *
2409 * Returns
2410 */
2411int
2412qla2x00_set_serdes_params(scsi_qla_host_t *ha, uint16_t sw_em_1g,
2413    uint16_t sw_em_2g, uint16_t sw_em_4g)
2414{
2415        int rval;
2416        mbx_cmd_t mc;
2417        mbx_cmd_t *mcp = &mc;
2418
2419        DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
2420
2421        mcp->mb[0] = MBC_SERDES_PARAMS;
2422        mcp->mb[1] = BIT_0;
2423        mcp->mb[2] = sw_em_1g | BIT_15;
2424        mcp->mb[3] = sw_em_2g | BIT_15;
2425        mcp->mb[4] = sw_em_4g | BIT_15;
2426        mcp->out_mb = MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
2427        mcp->in_mb = MBX_0;
2428        mcp->tov = 30;
2429        mcp->flags = 0;
2430        rval = qla2x00_mailbox_command(ha, mcp);
2431
2432        if (rval != QLA_SUCCESS) {
2433                /*EMPTY*/
2434                DEBUG2_3_11(printk("%s(%ld): failed=%x (%x).\n", __func__,
2435                    ha->host_no, rval, mcp->mb[0]));
2436        } else {
2437                /*EMPTY*/
2438                DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
2439        }
2440
2441        return rval;
2442}
2443
2444int
2445qla2x00_stop_firmware(scsi_qla_host_t *ha)
2446{
2447        int rval;
2448        mbx_cmd_t mc;
2449        mbx_cmd_t *mcp = &mc;
2450
2451        if (!IS_FWI2_CAPABLE(ha))
2452                return QLA_FUNCTION_FAILED;
2453
2454        DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
2455
2456        mcp->mb[0] = MBC_STOP_FIRMWARE;
2457        mcp->out_mb = MBX_0;
2458        mcp->in_mb = MBX_0;
2459        mcp->tov = 5;
2460        mcp->flags = 0;
2461        rval = qla2x00_mailbox_command(ha, mcp);
2462
2463        if (rval != QLA_SUCCESS) {
2464                DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
2465                    ha->host_no, rval));
2466        } else {
2467                DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
2468        }
2469
2470        return rval;
2471}
2472
2473int
2474qla2x00_trace_control(scsi_qla_host_t *ha, uint16_t ctrl, dma_addr_t eft_dma,
2475    uint16_t buffers)
2476{
2477        int rval;
2478        mbx_cmd_t mc;
2479        mbx_cmd_t *mcp = &mc;
2480
2481        if (!IS_FWI2_CAPABLE(ha))
2482                return QLA_FUNCTION_FAILED;
2483
2484        DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
2485
2486        mcp->mb[0] = MBC_TRACE_CONTROL;
2487        mcp->mb[1] = ctrl;
2488        mcp->out_mb = MBX_1|MBX_0;
2489        mcp->in_mb = MBX_1|MBX_0;
2490        if (ctrl == TC_ENABLE) {
2491                mcp->mb[2] = LSW(eft_dma);
2492                mcp->mb[3] = MSW(eft_dma);
2493                mcp->mb[4] = LSW(MSD(eft_dma));
2494                mcp->mb[5] = MSW(MSD(eft_dma));
2495                mcp->mb[6] = buffers;
2496                mcp->mb[7] = 0;
2497                mcp->out_mb |= MBX_7|MBX_6|MBX_5|MBX_4|MBX_3|MBX_2;
2498        }
2499        mcp->tov = 30;
2500        mcp->flags = 0;
2501        rval = qla2x00_mailbox_command(ha, mcp);
2502
2503        if (rval != QLA_SUCCESS) {
2504                DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=%x mb[1]=%x.\n",
2505                    __func__, ha->host_no, rval, mcp->mb[0], mcp->mb[1]));
2506        } else {
2507                DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
2508        }
2509
2510        return rval;
2511}
2512
2513int
2514qla2x00_read_sfp(scsi_qla_host_t *ha, dma_addr_t sfp_dma, uint16_t addr,
2515    uint16_t off, uint16_t count)
2516{
2517        int rval;
2518        mbx_cmd_t mc;
2519        mbx_cmd_t *mcp = &mc;
2520
2521        if (!IS_FWI2_CAPABLE(ha))
2522                return QLA_FUNCTION_FAILED;
2523
2524        DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
2525
2526        mcp->mb[0] = MBC_READ_SFP;
2527        mcp->mb[1] = addr;
2528        mcp->mb[2] = MSW(sfp_dma);
2529        mcp->mb[3] = LSW(sfp_dma);
2530        mcp->mb[6] = MSW(MSD(sfp_dma));
2531        mcp->mb[7] = LSW(MSD(sfp_dma));
2532        mcp->mb[8] = count;
2533        mcp->mb[9] = off;
2534        mcp->mb[10] = 0;
2535        mcp->out_mb = MBX_10|MBX_9|MBX_8|MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
2536        mcp->in_mb = MBX_0;
2537        mcp->tov = 30;
2538        mcp->flags = 0;
2539        rval = qla2x00_mailbox_command(ha, mcp);
2540
2541        if (rval != QLA_SUCCESS) {
2542                DEBUG2_3_11(printk("%s(%ld): failed=%x (%x).\n", __func__,
2543                    ha->host_no, rval, mcp->mb[0]));
2544        } else {
2545                DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
2546        }
2547
2548        return rval;
2549}
2550
2551int
2552qla2x00_get_idma_speed(scsi_qla_host_t *ha, uint16_t loop_id,
2553    uint16_t *port_speed, uint16_t *mb)
2554{
2555        int rval;
2556        mbx_cmd_t mc;
2557        mbx_cmd_t *mcp = &mc;
2558
2559        if (!IS_IIDMA_CAPABLE(ha))
2560                return QLA_FUNCTION_FAILED;
2561
2562        DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
2563
2564        mcp->mb[0] = MBC_PORT_PARAMS;
2565        mcp->mb[1] = loop_id;
2566        mcp->mb[2] = mcp->mb[3] = mcp->mb[4] = mcp->mb[5] = 0;
2567        mcp->out_mb = MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
2568        mcp->in_mb = MBX_5|MBX_4|MBX_3|MBX_1|MBX_0;
2569        mcp->tov = 30;
2570        mcp->flags = 0;
2571        rval = qla2x00_mailbox_command(ha, mcp);
2572
2573        /* Return mailbox statuses. */
2574        if (mb != NULL) {
2575                mb[0] = mcp->mb[0];
2576                mb[1] = mcp->mb[1];
2577                mb[3] = mcp->mb[3];
2578                mb[4] = mcp->mb[4];
2579                mb[5] = mcp->mb[5];
2580        }
2581
2582        if (rval != QLA_SUCCESS) {
2583                DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
2584                    ha->host_no, rval));
2585        } else {
2586                DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
2587                if (port_speed)
2588                        *port_speed = mcp->mb[3];
2589        }
2590
2591        return rval;
2592}
2593
2594int
2595qla2x00_set_idma_speed(scsi_qla_host_t *ha, uint16_t loop_id,
2596    uint16_t port_speed, uint16_t *mb)
2597{
2598        int rval;
2599        mbx_cmd_t mc;
2600        mbx_cmd_t *mcp = &mc;
2601
2602        if (!IS_IIDMA_CAPABLE(ha))
2603                return QLA_FUNCTION_FAILED;
2604
2605        DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
2606
2607        mcp->mb[0] = MBC_PORT_PARAMS;
2608        mcp->mb[1] = loop_id;
2609        mcp->mb[2] = BIT_0;
2610        mcp->mb[3] = port_speed & (BIT_2|BIT_1|BIT_0);
2611        mcp->mb[4] = mcp->mb[5] = 0;
2612        mcp->out_mb = MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
2613        mcp->in_mb = MBX_5|MBX_4|MBX_3|MBX_1|MBX_0;
2614        mcp->tov = 30;
2615        mcp->flags = 0;
2616        rval = qla2x00_mailbox_command(ha, mcp);
2617
2618        /* Return mailbox statuses. */
2619        if (mb != NULL) {
2620                mb[0] = mcp->mb[0];
2621                mb[1] = mcp->mb[1];
2622                mb[3] = mcp->mb[3];
2623                mb[4] = mcp->mb[4];
2624                mb[5] = mcp->mb[5];
2625        }
2626
2627        if (rval != QLA_SUCCESS) {
2628                DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
2629                    ha->host_no, rval));
2630        } else {
2631                DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
2632        }
2633
2634        return rval;
2635}
2636
2637/*
2638 * qla24xx_get_vp_database
2639 *      Get the VP's database for all configured ports.
2640 *
2641 * Input:
2642 *      ha = adapter block pointer.
2643 *      size = size of initialization control block.
2644 *
2645 * Returns:
2646 *      qla2x00 local function return status code.
2647 *
2648 * Context:
2649 *      Kernel context.
2650 */
2651int
2652qla24xx_get_vp_database(scsi_qla_host_t *ha, uint16_t size)
2653{
2654        int rval;
2655        mbx_cmd_t mc;
2656        mbx_cmd_t *mcp = &mc;
2657
2658        DEBUG11(printk("scsi(%ld):%s - entered.\n",
2659            ha->host_no, __func__));
2660
2661        mcp->mb[0] = MBC_MID_GET_VP_DATABASE;
2662        mcp->mb[2] = MSW(ha->init_cb_dma);
2663        mcp->mb[3] = LSW(ha->init_cb_dma);
2664        mcp->mb[4] = 0;
2665        mcp->mb[5] = 0;
2666        mcp->mb[6] = MSW(MSD(ha->init_cb_dma));
2667        mcp->mb[7] = LSW(MSD(ha->init_cb_dma));
2668        mcp->out_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_0;
2669        mcp->in_mb = MBX_1|MBX_0;
2670        mcp->buf_size = size;
2671        mcp->flags = MBX_DMA_OUT;
2672        mcp->tov = MBX_TOV_SECONDS;
2673        rval = qla2x00_mailbox_command(ha, mcp);
2674
2675        if (rval != QLA_SUCCESS) {
2676                /*EMPTY*/
2677                DEBUG2_3_11(printk("%s(%ld): failed=%x "
2678                    "mb0=%x.\n",
2679                    __func__, ha->host_no, rval, mcp->mb[0]));
2680        } else {
2681                /*EMPTY*/
2682                DEBUG11(printk("%s(%ld): done.\n",
2683                    __func__, ha->host_no));
2684        }
2685
2686        return rval;
2687}
2688
2689int
2690qla24xx_get_vp_entry(scsi_qla_host_t *ha, uint16_t size, int vp_id)
2691{
2692        int rval;
2693        mbx_cmd_t mc;
2694        mbx_cmd_t *mcp = &mc;
2695
2696        DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
2697
2698        mcp->mb[0] = MBC_MID_GET_VP_ENTRY;
2699        mcp->mb[2] = MSW(ha->init_cb_dma);
2700        mcp->mb[3] = LSW(ha->init_cb_dma);
2701        mcp->mb[4] = 0;
2702        mcp->mb[5] = 0;
2703        mcp->mb[6] = MSW(MSD(ha->init_cb_dma));
2704        mcp->mb[7] = LSW(MSD(ha->init_cb_dma));
2705        mcp->mb[9] = vp_id;
2706        mcp->out_mb = MBX_9|MBX_7|MBX_6|MBX_3|MBX_2|MBX_0;
2707        mcp->in_mb = MBX_0;
2708        mcp->buf_size = size;
2709        mcp->flags = MBX_DMA_OUT;
2710        mcp->tov = 30;
2711        rval = qla2x00_mailbox_command(ha, mcp);
2712
2713        if (rval != QLA_SUCCESS) {
2714                /*EMPTY*/
2715                DEBUG2_3_11(printk("qla24xx_get_vp_entry(%ld): failed=%x "
2716                    "mb0=%x.\n",
2717                    ha->host_no, rval, mcp->mb[0]));
2718        } else {
2719                /*EMPTY*/
2720                DEBUG11(printk("qla24xx_get_vp_entry(%ld): done.\n",
2721                    ha->host_no));
2722        }
2723
2724        return rval;
2725}
2726
2727void
2728qla24xx_report_id_acquisition(scsi_qla_host_t *ha,
2729        struct vp_rpt_id_entry_24xx *rptid_entry)
2730{
2731        uint8_t vp_idx;
2732        scsi_qla_host_t *vha;
2733
2734        if (rptid_entry->entry_status != 0)
2735                return;
2736        if (rptid_entry->entry_status != __constant_cpu_to_le16(CS_COMPLETE))
2737                return;
2738
2739        if (rptid_entry->format == 0) {
2740                DEBUG15(printk("%s:format 0 : scsi(%ld) number of VPs setup %d,"
2741                        " number of VPs acquired %d\n", __func__, ha->host_no,
2742                        MSB(rptid_entry->vp_count), LSB(rptid_entry->vp_count)));
2743                DEBUG15(printk("%s primary port id %02x%02x%02x\n", __func__,
2744                        rptid_entry->port_id[2], rptid_entry->port_id[1],
2745                        rptid_entry->port_id[0]));
2746        } else if (rptid_entry->format == 1) {
2747                vp_idx = LSB(rptid_entry->vp_idx);
2748                DEBUG15(printk("%s:format 1: scsi(%ld): VP[%d] enabled "
2749                    "- status %d - "
2750                    "with port id %02x%02x%02x\n",__func__,ha->host_no,
2751                    vp_idx, MSB(rptid_entry->vp_idx),
2752                    rptid_entry->port_id[2], rptid_entry->port_id[1],
2753                    rptid_entry->port_id[0]));
2754                if (vp_idx == 0)
2755                        return;
2756
2757                if (MSB(rptid_entry->vp_idx) == 1)
2758                        return;
2759
2760                list_for_each_entry(vha, &ha->vp_list, vp_list)
2761                        if (vp_idx == vha->vp_idx)
2762                                break;
2763
2764                if (!vha)
2765                        return;
2766
2767                vha->d_id.b.domain = rptid_entry->port_id[2];
2768                vha->d_id.b.area =  rptid_entry->port_id[1];
2769                vha->d_id.b.al_pa = rptid_entry->port_id[0];
2770
2771                /*
2772                 * Cannot configure here as we are still sitting on the
2773                 * response queue. Handle it in dpc context.
2774                 */
2775                set_bit(VP_IDX_ACQUIRED, &vha->vp_flags);
2776                set_bit(VP_DPC_NEEDED, &ha->dpc_flags);
2777
2778                wake_up_process(ha->dpc_thread);
2779        }
2780}
2781
2782/*
2783 * qla24xx_modify_vp_config
2784 *      Change VP configuration for vha
2785 *
2786 * Input:
2787 *      vha = adapter block pointer.
2788 *
2789 * Returns:
2790 *      qla2xxx local function return status code.
2791 *
2792 * Context:
2793 *      Kernel context.
2794 */
2795int
2796qla24xx_modify_vp_config(scsi_qla_host_t *vha)
2797{
2798        int             rval;
2799        struct vp_config_entry_24xx *vpmod;
2800        dma_addr_t      vpmod_dma;
2801        scsi_qla_host_t *pha;
2802
2803        /* This can be called by the parent */
2804        pha = to_qla_parent(vha);
2805
2806        vpmod = dma_pool_alloc(pha->s_dma_pool, GFP_KERNEL, &vpmod_dma);
2807        if (!vpmod) {
2808                DEBUG2_3(printk("%s(%ld): failed to allocate Modify VP "
2809                    "IOCB.\n", __func__, pha->host_no));
2810                return QLA_MEMORY_ALLOC_FAILED;
2811        }
2812
2813        memset(vpmod, 0, sizeof(struct vp_config_entry_24xx));
2814        vpmod->entry_type = VP_CONFIG_IOCB_TYPE;
2815        vpmod->entry_count = 1;
2816        vpmod->command = VCT_COMMAND_MOD_ENABLE_VPS;
2817        vpmod->vp_count = 1;
2818        vpmod->vp_index1 = vha->vp_idx;
2819        vpmod->options_idx1 = BIT_3|BIT_4|BIT_5;
2820        memcpy(vpmod->node_name_idx1, vha->node_name, WWN_SIZE);
2821        memcpy(vpmod->port_name_idx1, vha->port_name, WWN_SIZE);
2822        vpmod->entry_count = 1;
2823
2824        rval = qla2x00_issue_iocb(pha, vpmod, vpmod_dma, 0);
2825        if (rval != QLA_SUCCESS) {
2826                DEBUG2_3_11(printk("%s(%ld): failed to issue VP config IOCB"
2827                        "(%x).\n", __func__, pha->host_no, rval));
2828        } else if (vpmod->comp_status != 0) {
2829                DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
2830                        "-- error status (%x).\n", __func__, pha->host_no,
2831                        vpmod->comp_status));
2832                rval = QLA_FUNCTION_FAILED;
2833        } else if (vpmod->comp_status != __constant_cpu_to_le16(CS_COMPLETE)) {
2834                DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
2835                    "-- completion status (%x).\n", __func__, pha->host_no,
2836                    le16_to_cpu(vpmod->comp_status)));
2837                rval = QLA_FUNCTION_FAILED;
2838        } else {
2839                /* EMPTY */
2840                DEBUG11(printk("%s(%ld): done.\n", __func__, pha->host_no));
2841                fc_vport_set_state(vha->fc_vport, FC_VPORT_INITIALIZING);
2842        }
2843        dma_pool_free(pha->s_dma_pool, vpmod, vpmod_dma);
2844
2845        return rval;
2846}
2847
2848/*
2849 * qla24xx_control_vp
2850 *      Enable a virtual port for given host
2851 *
2852 * Input:
2853 *      ha = adapter block pointer.
2854 *      vhba = virtual adapter (unused)
2855 *      index = index number for enabled VP
2856 *
2857 * Returns:
2858 *      qla2xxx local function return status code.
2859 *
2860 * Context:
2861 *      Kernel context.
2862 */
2863int
2864qla24xx_control_vp(scsi_qla_host_t *vha, int cmd)
2865{
2866        int             rval;
2867        int             map, pos;
2868        struct vp_ctrl_entry_24xx   *vce;
2869        dma_addr_t      vce_dma;
2870        scsi_qla_host_t *ha = vha->parent;
2871        int     vp_index = vha->vp_idx;
2872
2873        DEBUG11(printk("%s(%ld): entered. Enabling index %d\n", __func__,
2874            ha->host_no, vp_index));
2875
2876        if (vp_index == 0 || vp_index >= MAX_MULTI_ID_LOOP)
2877                return QLA_PARAMETER_ERROR;
2878
2879        vce = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &vce_dma);
2880        if (!vce) {
2881                DEBUG2_3(printk("%s(%ld): "
2882                    "failed to allocate VP Control IOCB.\n", __func__,
2883                    ha->host_no));
2884                return QLA_MEMORY_ALLOC_FAILED;
2885        }
2886        memset(vce, 0, sizeof(struct vp_ctrl_entry_24xx));
2887
2888        vce->entry_type = VP_CTRL_IOCB_TYPE;
2889        vce->entry_count = 1;
2890        vce->command = cpu_to_le16(cmd);
2891        vce->vp_count = __constant_cpu_to_le16(1);
2892
2893        /* index map in firmware starts with 1; decrement index
2894         * this is ok as we never use index 0
2895         */
2896        map = (vp_index - 1) / 8;
2897        pos = (vp_index - 1) & 7;
2898        down(&ha->vport_sem);
2899        vce->vp_idx_map[map] |= 1 << pos;
2900        up(&ha->vport_sem);
2901
2902        rval = qla2x00_issue_iocb(ha, vce, vce_dma, 0);
2903        if (rval != QLA_SUCCESS) {
2904                DEBUG2_3_11(printk("%s(%ld): failed to issue VP control IOCB"
2905                    "(%x).\n", __func__, ha->host_no, rval));
2906                printk("%s(%ld): failed to issue VP control IOCB"
2907                    "(%x).\n", __func__, ha->host_no, rval);
2908        } else if (vce->entry_status != 0) {
2909                DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
2910                    "-- error status (%x).\n", __func__, ha->host_no,
2911                    vce->entry_status));
2912                printk("%s(%ld): failed to complete IOCB "
2913                    "-- error status (%x).\n", __func__, ha->host_no,
2914                    vce->entry_status);
2915                rval = QLA_FUNCTION_FAILED;
2916        } else if (vce->comp_status != __constant_cpu_to_le16(CS_COMPLETE)) {
2917                DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
2918                    "-- completion status (%x).\n", __func__, ha->host_no,
2919                    le16_to_cpu(vce->comp_status)));
2920                printk("%s(%ld): failed to complete IOCB "
2921                    "-- completion status (%x).\n", __func__, ha->host_no,
2922                    le16_to_cpu(vce->comp_status));
2923                rval = QLA_FUNCTION_FAILED;
2924        } else {
2925                DEBUG2(printk("%s(%ld): done.\n", __func__, ha->host_no));
2926        }
2927
2928        dma_pool_free(ha->s_dma_pool, vce, vce_dma);
2929
2930        return rval;
2931}
2932
2933/*
2934 * qla2x00_send_change_request
2935 *      Receive or disable RSCN request from fabric controller
2936 *
2937 * Input:
2938 *      ha = adapter block pointer
2939 *      format = registration format:
2940 *              0 - Reserved
2941 *              1 - Fabric detected registration
2942 *              2 - N_port detected registration
2943 *              3 - Full registration
2944 *              FF - clear registration
2945 *      vp_idx = Virtual port index
2946 *
2947 * Returns:
2948 *      qla2x00 local function return status code.
2949 *
2950 * Context:
2951 *      Kernel Context
2952 */
2953
2954int
2955qla2x00_send_change_request(scsi_qla_host_t *ha, uint16_t format,
2956                            uint16_t vp_idx)
2957{
2958        int rval;
2959        mbx_cmd_t mc;
2960        mbx_cmd_t *mcp = &mc;
2961
2962        /*
2963         * This command is implicitly executed by firmware during login for the
2964         * physical hosts
2965         */
2966        if (vp_idx == 0)
2967                return QLA_FUNCTION_FAILED;
2968
2969        mcp->mb[0] = MBC_SEND_CHANGE_REQUEST;
2970        mcp->mb[1] = format;
2971        mcp->mb[9] = vp_idx;
2972        mcp->out_mb = MBX_9|MBX_1|MBX_0;
2973        mcp->in_mb = MBX_0|MBX_1;
2974        mcp->tov = MBX_TOV_SECONDS;
2975        mcp->flags = 0;
2976        rval = qla2x00_mailbox_command(ha, mcp);
2977
2978        if (rval == QLA_SUCCESS) {
2979                if (mcp->mb[0] != MBS_COMMAND_COMPLETE) {
2980                        rval = BIT_1;
2981                }
2982        } else
2983                rval = BIT_1;
2984
2985        return rval;
2986}
2987
2988int
2989qla2x00_dump_ram(scsi_qla_host_t *ha, dma_addr_t req_dma, uint32_t addr,
2990    uint32_t size)
2991{
2992        int rval;
2993        mbx_cmd_t mc;
2994        mbx_cmd_t *mcp = &mc;
2995
2996        DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
2997
2998        if (MSW(addr) || IS_FWI2_CAPABLE(ha)) {
2999                mcp->mb[0] = MBC_DUMP_RISC_RAM_EXTENDED;
3000                mcp->mb[8] = MSW(addr);
3001                mcp->out_mb = MBX_8|MBX_0;
3002        } else {
3003                mcp->mb[0] = MBC_DUMP_RISC_RAM;
3004                mcp->out_mb = MBX_0;
3005        }
3006        mcp->mb[1] = LSW(addr);
3007        mcp->mb[2] = MSW(req_dma);
3008        mcp->mb[3] = LSW(req_dma);
3009        mcp->mb[6] = MSW(MSD(req_dma));
3010        mcp->mb[7] = LSW(MSD(req_dma));
3011        mcp->out_mb |= MBX_7|MBX_6|MBX_3|MBX_2|MBX_1;
3012        if (IS_FWI2_CAPABLE(ha)) {
3013                mcp->mb[4] = MSW(size);
3014                mcp->mb[5] = LSW(size);
3015                mcp->out_mb |= MBX_5|MBX_4;
3016        } else {
3017                mcp->mb[4] = LSW(size);
3018                mcp->out_mb |= MBX_4;
3019        }
3020
3021        mcp->in_mb = MBX_0;
3022        mcp->tov = 30;
3023        mcp->flags = 0;
3024        rval = qla2x00_mailbox_command(ha, mcp);
3025
3026        if (rval != QLA_SUCCESS) {
3027                DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=%x.\n", __func__,
3028                    ha->host_no, rval, mcp->mb[0]));
3029        } else {
3030                DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
3031        }
3032
3033        return rval;
3034}
3035