linux/drivers/scsi/qla4xxx/ql4_init.c
<<
>>
Prefs
   1/*
   2 * QLogic iSCSI HBA Driver
   3 * Copyright (c)  2003-2006 QLogic Corporation
   4 *
   5 * See LICENSE.qla4xxx for copyright and licensing details.
   6 */
   7
   8#include "ql4_def.h"
   9#include "ql4_glbl.h"
  10#include "ql4_dbg.h"
  11#include "ql4_inline.h"
  12
  13static struct ddb_entry * qla4xxx_alloc_ddb(struct scsi_qla_host *ha,
  14                                            uint32_t fw_ddb_index);
  15
  16static void ql4xxx_set_mac_number(struct scsi_qla_host *ha)
  17{
  18        uint32_t value;
  19        uint8_t func_number;
  20        unsigned long flags;
  21
  22        /* Get the function number */
  23        spin_lock_irqsave(&ha->hardware_lock, flags);
  24        value = readw(&ha->reg->ctrl_status);
  25        spin_unlock_irqrestore(&ha->hardware_lock, flags);
  26
  27        func_number = (uint8_t) ((value >> 4) & 0x30);
  28        switch (value & ISP_CONTROL_FN_MASK) {
  29        case ISP_CONTROL_FN0_SCSI:
  30                ha->mac_index = 1;
  31                break;
  32        case ISP_CONTROL_FN1_SCSI:
  33                ha->mac_index = 3;
  34                break;
  35        default:
  36                DEBUG2(printk("scsi%ld: %s: Invalid function number, "
  37                              "ispControlStatus = 0x%x\n", ha->host_no,
  38                              __func__, value));
  39                break;
  40        }
  41        DEBUG2(printk("scsi%ld: %s: mac_index %d.\n", ha->host_no, __func__,
  42                      ha->mac_index));
  43}
  44
  45/**
  46 * qla4xxx_free_ddb - deallocate ddb
  47 * @ha: pointer to host adapter structure.
  48 * @ddb_entry: pointer to device database entry
  49 *
  50 * This routine deallocates and unlinks the specified ddb_entry from the
  51 * adapter's
  52 **/
  53static void qla4xxx_free_ddb(struct scsi_qla_host *ha,
  54                             struct ddb_entry *ddb_entry)
  55{
  56        /* Remove device entry from list */
  57        list_del_init(&ddb_entry->list);
  58
  59        /* Remove device pointer from index mapping arrays */
  60        ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] =
  61                (struct ddb_entry *) INVALID_ENTRY;
  62        ha->tot_ddbs--;
  63
  64        /* Free memory and scsi-ml struct for device entry */
  65        qla4xxx_destroy_sess(ddb_entry);
  66}
  67
  68/**
  69 * qla4xxx_free_ddb_list - deallocate all ddbs
  70 * @ha: pointer to host adapter structure.
  71 *
  72 * This routine deallocates and removes all devices on the sppecified adapter.
  73 **/
  74void qla4xxx_free_ddb_list(struct scsi_qla_host *ha)
  75{
  76        struct list_head *ptr;
  77        struct ddb_entry *ddb_entry;
  78
  79        while (!list_empty(&ha->ddb_list)) {
  80                ptr = ha->ddb_list.next;
  81                /* Free memory for device entry and remove */
  82                ddb_entry = list_entry(ptr, struct ddb_entry, list);
  83                qla4xxx_free_ddb(ha, ddb_entry);
  84        }
  85}
  86
  87/**
  88 * qla4xxx_init_rings - initialize hw queues
  89 * @ha: pointer to host adapter structure.
  90 *
  91 * This routine initializes the internal queues for the specified adapter.
  92 * The QLA4010 requires us to restart the queues at index 0.
  93 * The QLA4000 doesn't care, so just default to QLA4010's requirement.
  94 **/
  95int qla4xxx_init_rings(struct scsi_qla_host *ha)
  96{
  97        unsigned long flags = 0;
  98
  99        /* Initialize request queue. */
 100        spin_lock_irqsave(&ha->hardware_lock, flags);
 101        ha->request_out = 0;
 102        ha->request_in = 0;
 103        ha->request_ptr = &ha->request_ring[ha->request_in];
 104        ha->req_q_count = REQUEST_QUEUE_DEPTH;
 105
 106        /* Initialize response queue. */
 107        ha->response_in = 0;
 108        ha->response_out = 0;
 109        ha->response_ptr = &ha->response_ring[ha->response_out];
 110
 111        /*
 112         * Initialize DMA Shadow registers.  The firmware is really supposed to
 113         * take care of this, but on some uniprocessor systems, the shadow
 114         * registers aren't cleared-- causing the interrupt_handler to think
 115         * there are responses to be processed when there aren't.
 116         */
 117        ha->shadow_regs->req_q_out = __constant_cpu_to_le32(0);
 118        ha->shadow_regs->rsp_q_in = __constant_cpu_to_le32(0);
 119        wmb();
 120
 121        writel(0, &ha->reg->req_q_in);
 122        writel(0, &ha->reg->rsp_q_out);
 123        readl(&ha->reg->rsp_q_out);
 124
 125        spin_unlock_irqrestore(&ha->hardware_lock, flags);
 126
 127        return QLA_SUCCESS;
 128}
 129
 130/**
 131 * qla4xxx_validate_mac_address - validate adapter MAC address(es)
 132 * @ha: pointer to host adapter structure.
 133 *
 134 **/
 135static int qla4xxx_validate_mac_address(struct scsi_qla_host *ha)
 136{
 137        struct flash_sys_info *sys_info;
 138        dma_addr_t sys_info_dma;
 139        int status = QLA_ERROR;
 140
 141        sys_info = dma_alloc_coherent(&ha->pdev->dev, sizeof(*sys_info),
 142                                      &sys_info_dma, GFP_KERNEL);
 143        if (sys_info == NULL) {
 144                DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
 145                              ha->host_no, __func__));
 146
 147                goto exit_validate_mac_no_free;
 148        }
 149        memset(sys_info, 0, sizeof(*sys_info));
 150
 151        /* Get flash sys info */
 152        if (qla4xxx_get_flash(ha, sys_info_dma, FLASH_OFFSET_SYS_INFO,
 153                              sizeof(*sys_info)) != QLA_SUCCESS) {
 154                DEBUG2(printk("scsi%ld: %s: get_flash FLASH_OFFSET_SYS_INFO "
 155                              "failed\n", ha->host_no, __func__));
 156
 157                goto exit_validate_mac;
 158        }
 159
 160        /* Save M.A.C. address & serial_number */
 161        memcpy(ha->my_mac, &sys_info->physAddr[0].address[0],
 162               min(sizeof(ha->my_mac),
 163                   sizeof(sys_info->physAddr[0].address)));
 164        memcpy(ha->serial_number, &sys_info->acSerialNumber,
 165               min(sizeof(ha->serial_number),
 166                   sizeof(sys_info->acSerialNumber)));
 167
 168        status = QLA_SUCCESS;
 169
 170 exit_validate_mac:
 171        dma_free_coherent(&ha->pdev->dev, sizeof(*sys_info), sys_info,
 172                          sys_info_dma);
 173
 174 exit_validate_mac_no_free:
 175        return status;
 176}
 177
 178/**
 179 * qla4xxx_init_local_data - initialize adapter specific local data
 180 * @ha: pointer to host adapter structure.
 181 *
 182 **/
 183static int qla4xxx_init_local_data(struct scsi_qla_host *ha)
 184{
 185        /* Initilize aen queue */
 186        ha->aen_q_count = MAX_AEN_ENTRIES;
 187
 188        return qla4xxx_get_firmware_status(ha);
 189}
 190
 191static int qla4xxx_fw_ready(struct scsi_qla_host *ha)
 192{
 193        uint32_t timeout_count;
 194        int ready = 0;
 195
 196        DEBUG2(dev_info(&ha->pdev->dev, "Waiting for Firmware Ready..\n"));
 197        for (timeout_count = ADAPTER_INIT_TOV; timeout_count > 0;
 198             timeout_count--) {
 199                if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
 200                        qla4xxx_get_dhcp_ip_address(ha);
 201
 202                /* Get firmware state. */
 203                if (qla4xxx_get_firmware_state(ha) != QLA_SUCCESS) {
 204                        DEBUG2(printk("scsi%ld: %s: unable to get firmware "
 205                                      "state\n", ha->host_no, __func__));
 206                        break;
 207
 208                }
 209
 210                if (ha->firmware_state & FW_STATE_ERROR) {
 211                        DEBUG2(printk("scsi%ld: %s: an unrecoverable error has"
 212                                      " occurred\n", ha->host_no, __func__));
 213                        break;
 214
 215                }
 216                if (ha->firmware_state & FW_STATE_CONFIG_WAIT) {
 217                        /*
 218                         * The firmware has not yet been issued an Initialize
 219                         * Firmware command, so issue it now.
 220                         */
 221                        if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR)
 222                                break;
 223
 224                        /* Go back and test for ready state - no wait. */
 225                        continue;
 226                }
 227
 228                if (ha->firmware_state == FW_STATE_READY) {
 229                        DEBUG2(dev_info(&ha->pdev->dev, "Firmware Ready..\n"));
 230                        /* The firmware is ready to process SCSI commands. */
 231                        DEBUG2(dev_info(&ha->pdev->dev,
 232                                          "scsi%ld: %s: MEDIA TYPE - %s\n",
 233                                          ha->host_no,
 234                                          __func__, (ha->addl_fw_state &
 235                                                     FW_ADDSTATE_OPTICAL_MEDIA)
 236                                          != 0 ? "OPTICAL" : "COPPER"));
 237                        DEBUG2(dev_info(&ha->pdev->dev,
 238                                          "scsi%ld: %s: DHCP STATE Enabled "
 239                                          "%s\n",
 240                                          ha->host_no, __func__,
 241                                          (ha->addl_fw_state &
 242                                           FW_ADDSTATE_DHCP_ENABLED) != 0 ?
 243                                          "YES" : "NO"));
 244                        DEBUG2(dev_info(&ha->pdev->dev,
 245                                          "scsi%ld: %s: LINK %s\n",
 246                                          ha->host_no, __func__,
 247                                          (ha->addl_fw_state &
 248                                           FW_ADDSTATE_LINK_UP) != 0 ?
 249                                          "UP" : "DOWN"));
 250                        DEBUG2(dev_info(&ha->pdev->dev,
 251                                          "scsi%ld: %s: iSNS Service "
 252                                          "Started %s\n",
 253                                          ha->host_no, __func__,
 254                                          (ha->addl_fw_state &
 255                                           FW_ADDSTATE_ISNS_SVC_ENABLED) != 0 ?
 256                                          "YES" : "NO"));
 257
 258                        ready = 1;
 259                        break;
 260                }
 261                DEBUG2(printk("scsi%ld: %s: waiting on fw, state=%x:%x - "
 262                              "seconds expired= %d\n", ha->host_no, __func__,
 263                              ha->firmware_state, ha->addl_fw_state,
 264                              timeout_count));
 265                if (is_qla4032(ha) &&
 266                        !(ha->addl_fw_state & FW_ADDSTATE_LINK_UP) &&
 267                        (timeout_count < ADAPTER_INIT_TOV - 5)) {
 268                        break;
 269                }
 270
 271                msleep(1000);
 272        }                       /* end of for */
 273
 274        if (timeout_count == 0)
 275                DEBUG2(printk("scsi%ld: %s: FW Initialization timed out!\n",
 276                              ha->host_no, __func__));
 277
 278        if (ha->firmware_state & FW_STATE_DHCP_IN_PROGRESS)  {
 279                DEBUG2(printk("scsi%ld: %s: FW is reporting its waiting to"
 280                              " grab an IP address from DHCP server\n",
 281                              ha->host_no, __func__));
 282                ready = 1;
 283        }
 284
 285        return ready;
 286}
 287
 288/**
 289 * qla4xxx_init_firmware - initializes the firmware.
 290 * @ha: pointer to host adapter structure.
 291 *
 292 **/
 293static int qla4xxx_init_firmware(struct scsi_qla_host *ha)
 294{
 295        int status = QLA_ERROR;
 296
 297        dev_info(&ha->pdev->dev, "Initializing firmware..\n");
 298        if (qla4xxx_initialize_fw_cb(ha) == QLA_ERROR) {
 299                DEBUG2(printk("scsi%ld: %s: Failed to initialize firmware "
 300                              "control block\n", ha->host_no, __func__));
 301                return status;
 302        }
 303        if (!qla4xxx_fw_ready(ha))
 304                return status;
 305
 306        return qla4xxx_get_firmware_status(ha);
 307}
 308
 309static struct ddb_entry* qla4xxx_get_ddb_entry(struct scsi_qla_host *ha,
 310                                                uint32_t fw_ddb_index,
 311                                                uint32_t *new_tgt)
 312{
 313        struct dev_db_entry *fw_ddb_entry = NULL;
 314        dma_addr_t fw_ddb_entry_dma;
 315        struct ddb_entry *ddb_entry = NULL;
 316        int found = 0;
 317        uint32_t device_state;
 318
 319        *new_tgt = 0;
 320        /* Make sure the dma buffer is valid */
 321        fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev,
 322                                          sizeof(*fw_ddb_entry),
 323                                          &fw_ddb_entry_dma, GFP_KERNEL);
 324        if (fw_ddb_entry == NULL) {
 325                DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
 326                              ha->host_no, __func__));
 327                return NULL;
 328        }
 329
 330        if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
 331                                    fw_ddb_entry_dma, NULL, NULL,
 332                                    &device_state, NULL, NULL, NULL) ==
 333            QLA_ERROR) {
 334                DEBUG2(printk("scsi%ld: %s: failed get_ddb_entry for "
 335                              "fw_ddb_index %d\n", ha->host_no, __func__,
 336                              fw_ddb_index));
 337                return NULL;
 338        }
 339
 340        /* Allocate DDB if not already allocated. */
 341        DEBUG2(printk("scsi%ld: %s: Looking for ddb[%d]\n", ha->host_no,
 342                      __func__, fw_ddb_index));
 343        list_for_each_entry(ddb_entry, &ha->ddb_list, list) {
 344                if (memcmp(ddb_entry->iscsi_name, fw_ddb_entry->iscsi_name,
 345                           ISCSI_NAME_SIZE) == 0) {
 346                        found++;
 347                        break;
 348                }
 349        }
 350
 351        if (!found) {
 352                DEBUG2(printk("scsi%ld: %s: ddb[%d] not found - allocating "
 353                              "new ddb\n", ha->host_no, __func__,
 354                              fw_ddb_index));
 355                *new_tgt = 1;
 356                ddb_entry = qla4xxx_alloc_ddb(ha, fw_ddb_index);
 357        }
 358
 359        /* if not found allocate new ddb */
 360        dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry), fw_ddb_entry,
 361                          fw_ddb_entry_dma);
 362
 363        return ddb_entry;
 364}
 365
 366/**
 367 * qla4xxx_update_ddb_entry - update driver's internal ddb
 368 * @ha: pointer to host adapter structure.
 369 * @ddb_entry: pointer to device database structure to be filled
 370 * @fw_ddb_index: index of the ddb entry in fw ddb table
 371 *
 372 * This routine updates the driver's internal device database entry
 373 * with information retrieved from the firmware's device database
 374 * entry for the specified device. The ddb_entry->fw_ddb_index field
 375 * must be initialized prior to calling this routine
 376 *
 377 **/
 378static int qla4xxx_update_ddb_entry(struct scsi_qla_host *ha,
 379                                    struct ddb_entry *ddb_entry,
 380                                    uint32_t fw_ddb_index)
 381{
 382        struct dev_db_entry *fw_ddb_entry = NULL;
 383        dma_addr_t fw_ddb_entry_dma;
 384        int status = QLA_ERROR;
 385
 386        if (ddb_entry == NULL) {
 387                DEBUG2(printk("scsi%ld: %s: ddb_entry is NULL\n", ha->host_no,
 388                              __func__));
 389                goto exit_update_ddb;
 390        }
 391
 392        /* Make sure the dma buffer is valid */
 393        fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev,
 394                                          sizeof(*fw_ddb_entry),
 395                                          &fw_ddb_entry_dma, GFP_KERNEL);
 396        if (fw_ddb_entry == NULL) {
 397                DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
 398                              ha->host_no, __func__));
 399
 400                goto exit_update_ddb;
 401        }
 402
 403        if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, fw_ddb_entry,
 404                                    fw_ddb_entry_dma, NULL, NULL,
 405                                    &ddb_entry->fw_ddb_device_state, NULL,
 406                                    &ddb_entry->tcp_source_port_num,
 407                                    &ddb_entry->connection_id) ==
 408            QLA_ERROR) {
 409                DEBUG2(printk("scsi%ld: %s: failed get_ddb_entry for "
 410                              "fw_ddb_index %d\n", ha->host_no, __func__,
 411                              fw_ddb_index));
 412
 413                goto exit_update_ddb;
 414        }
 415
 416        status = QLA_SUCCESS;
 417        ddb_entry->target_session_id = le16_to_cpu(fw_ddb_entry->tsid);
 418        ddb_entry->task_mgmt_timeout =
 419                le16_to_cpu(fw_ddb_entry->def_timeout);
 420        ddb_entry->CmdSn = 0;
 421        ddb_entry->exe_throttle = le16_to_cpu(fw_ddb_entry->exec_throttle);
 422        ddb_entry->default_relogin_timeout =
 423                le16_to_cpu(fw_ddb_entry->def_timeout);
 424        ddb_entry->default_time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
 425
 426        /* Update index in case it changed */
 427        ddb_entry->fw_ddb_index = fw_ddb_index;
 428        ha->fw_ddb_index_map[fw_ddb_index] = ddb_entry;
 429
 430        ddb_entry->port = le16_to_cpu(fw_ddb_entry->port);
 431        ddb_entry->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
 432        memcpy(&ddb_entry->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
 433               min(sizeof(ddb_entry->iscsi_name),
 434                   sizeof(fw_ddb_entry->iscsi_name)));
 435        memcpy(&ddb_entry->ip_addr[0], &fw_ddb_entry->ip_addr[0],
 436               min(sizeof(ddb_entry->ip_addr), sizeof(fw_ddb_entry->ip_addr)));
 437
 438        DEBUG2(printk("scsi%ld: %s: ddb[%d] - State= %x status= %d.\n",
 439                      ha->host_no, __func__, fw_ddb_index,
 440                      ddb_entry->fw_ddb_device_state, status));
 441
 442 exit_update_ddb:
 443        if (fw_ddb_entry)
 444                dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
 445                                  fw_ddb_entry, fw_ddb_entry_dma);
 446
 447        return status;
 448}
 449
 450/**
 451 * qla4xxx_alloc_ddb - allocate device database entry
 452 * @ha: Pointer to host adapter structure.
 453 * @fw_ddb_index: Firmware's device database index
 454 *
 455 * This routine allocates a ddb_entry, ititializes some values, and
 456 * inserts it into the ddb list.
 457 **/
 458static struct ddb_entry * qla4xxx_alloc_ddb(struct scsi_qla_host *ha,
 459                                            uint32_t fw_ddb_index)
 460{
 461        struct ddb_entry *ddb_entry;
 462
 463        DEBUG2(printk("scsi%ld: %s: fw_ddb_index [%d]\n", ha->host_no,
 464                      __func__, fw_ddb_index));
 465
 466        ddb_entry = qla4xxx_alloc_sess(ha);
 467        if (ddb_entry == NULL) {
 468                DEBUG2(printk("scsi%ld: %s: Unable to allocate memory "
 469                              "to add fw_ddb_index [%d]\n",
 470                              ha->host_no, __func__, fw_ddb_index));
 471                return ddb_entry;
 472        }
 473
 474        ddb_entry->fw_ddb_index = fw_ddb_index;
 475        atomic_set(&ddb_entry->port_down_timer, ha->port_down_retry_count);
 476        atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
 477        atomic_set(&ddb_entry->relogin_timer, 0);
 478        atomic_set(&ddb_entry->relogin_retry_count, 0);
 479        atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
 480        list_add_tail(&ddb_entry->list, &ha->ddb_list);
 481        ha->fw_ddb_index_map[fw_ddb_index] = ddb_entry;
 482        ha->tot_ddbs++;
 483
 484        return ddb_entry;
 485}
 486
 487/**
 488 * qla4xxx_configure_ddbs - builds driver ddb list
 489 * @ha: Pointer to host adapter structure.
 490 *
 491 * This routine searches for all valid firmware ddb entries and builds
 492 * an internal ddb list. Ddbs that are considered valid are those with
 493 * a device state of SESSION_ACTIVE.
 494 **/
 495static int qla4xxx_build_ddb_list(struct scsi_qla_host *ha)
 496{
 497        int status = QLA_SUCCESS;
 498        uint32_t fw_ddb_index = 0;
 499        uint32_t next_fw_ddb_index = 0;
 500        uint32_t ddb_state;
 501        uint32_t conn_err, err_code;
 502        struct ddb_entry *ddb_entry;
 503        uint32_t new_tgt;
 504
 505        dev_info(&ha->pdev->dev, "Initializing DDBs ...\n");
 506        for (fw_ddb_index = 0; fw_ddb_index < MAX_DDB_ENTRIES;
 507             fw_ddb_index = next_fw_ddb_index) {
 508                /* First, let's see if a device exists here */
 509                if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index, NULL, 0, NULL,
 510                                            &next_fw_ddb_index, &ddb_state,
 511                                            &conn_err, NULL, NULL) ==
 512                    QLA_ERROR) {
 513                        DEBUG2(printk("scsi%ld: %s: get_ddb_entry, "
 514                                      "fw_ddb_index %d failed", ha->host_no,
 515                                      __func__, fw_ddb_index));
 516                        return QLA_ERROR;
 517                }
 518
 519                DEBUG2(printk("scsi%ld: %s: Getting DDB[%d] ddbstate=0x%x, "
 520                              "next_fw_ddb_index=%d.\n", ha->host_no, __func__,
 521                              fw_ddb_index, ddb_state, next_fw_ddb_index));
 522
 523                /* Issue relogin, if necessary. */
 524                if (ddb_state == DDB_DS_SESSION_FAILED ||
 525                    ddb_state == DDB_DS_NO_CONNECTION_ACTIVE) {
 526                        /* Try and login to device */
 527                        DEBUG2(printk("scsi%ld: %s: Login to DDB[%d]\n",
 528                                      ha->host_no, __func__, fw_ddb_index));
 529                        err_code = ((conn_err & 0x00ff0000) >> 16);
 530                        if (err_code == 0x1c || err_code == 0x06) {
 531                                DEBUG2(printk("scsi%ld: %s send target "
 532                                              "completed "
 533                                              "or access denied failure\n",
 534                                              ha->host_no, __func__));
 535                        } else {
 536                                qla4xxx_set_ddb_entry(ha, fw_ddb_index, 0);
 537                                if (qla4xxx_get_fwddb_entry(ha, fw_ddb_index,
 538                                        NULL, 0, NULL, &next_fw_ddb_index,
 539                                        &ddb_state, &conn_err, NULL, NULL)
 540                                        == QLA_ERROR) {
 541                                        DEBUG2(printk("scsi%ld: %s:"
 542                                                "get_ddb_entry %d failed\n",
 543                                                ha->host_no,
 544                                                __func__, fw_ddb_index));
 545                                        return QLA_ERROR;
 546                                }
 547                        }
 548                }
 549
 550                if (ddb_state != DDB_DS_SESSION_ACTIVE)
 551                        goto next_one;
 552                /*
 553                 * if fw_ddb with session active state found,
 554                 * add to ddb_list
 555                 */
 556                DEBUG2(printk("scsi%ld: %s: DDB[%d] added to list\n",
 557                              ha->host_no, __func__, fw_ddb_index));
 558
 559                /* Add DDB to internal our ddb list. */
 560                ddb_entry = qla4xxx_get_ddb_entry(ha, fw_ddb_index, &new_tgt);
 561                if (ddb_entry == NULL) {
 562                        DEBUG2(printk("scsi%ld: %s: Unable to allocate memory "
 563                                      "for device at fw_ddb_index %d\n",
 564                                      ha->host_no, __func__, fw_ddb_index));
 565                        return QLA_ERROR;
 566                }
 567                /* Fill in the device structure */
 568                if (qla4xxx_update_ddb_entry(ha, ddb_entry, fw_ddb_index) ==
 569                    QLA_ERROR) {
 570                        ha->fw_ddb_index_map[fw_ddb_index] =
 571                                (struct ddb_entry *)INVALID_ENTRY;
 572
 573
 574                        DEBUG2(printk("scsi%ld: %s: update_ddb_entry failed "
 575                                      "for fw_ddb_index %d.\n",
 576                                      ha->host_no, __func__, fw_ddb_index));
 577                        return QLA_ERROR;
 578                }
 579
 580next_one:
 581                /* We know we've reached the last device when
 582                 * next_fw_ddb_index is 0 */
 583                if (next_fw_ddb_index == 0)
 584                        break;
 585        }
 586
 587        dev_info(&ha->pdev->dev, "DDB list done..\n");
 588
 589        return status;
 590}
 591
 592struct qla4_relog_scan {
 593        int halt_wait;
 594        uint32_t conn_err;
 595        uint32_t err_code;
 596        uint32_t fw_ddb_index;
 597        uint32_t next_fw_ddb_index;
 598        uint32_t fw_ddb_device_state;
 599};
 600
 601static int qla4_test_rdy(struct scsi_qla_host *ha, struct qla4_relog_scan *rs)
 602{
 603        struct ddb_entry *ddb_entry;
 604
 605        /*
 606         * Don't want to do a relogin if connection
 607         * error is 0x1c.
 608         */
 609        rs->err_code = ((rs->conn_err & 0x00ff0000) >> 16);
 610        if (rs->err_code == 0x1c || rs->err_code == 0x06) {
 611                DEBUG2(printk(
 612                               "scsi%ld: %s send target"
 613                               " completed or "
 614                               "access denied failure\n",
 615                               ha->host_no, __func__));
 616        } else {
 617                /* We either have a device that is in
 618                 * the process of relogging in or a
 619                 * device that is waiting to be
 620                 * relogged in */
 621                rs->halt_wait = 0;
 622
 623                ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha,
 624                                                           rs->fw_ddb_index);
 625                if (ddb_entry == NULL)
 626                        return QLA_ERROR;
 627
 628                if (ddb_entry->dev_scan_wait_to_start_relogin != 0
 629                    && time_after_eq(jiffies,
 630                                     ddb_entry->
 631                                     dev_scan_wait_to_start_relogin))
 632                {
 633                        ddb_entry->dev_scan_wait_to_start_relogin = 0;
 634                        qla4xxx_set_ddb_entry(ha, rs->fw_ddb_index, 0);
 635                }
 636        }
 637        return QLA_SUCCESS;
 638}
 639
 640static int qla4_scan_for_relogin(struct scsi_qla_host *ha,
 641                                 struct qla4_relog_scan *rs)
 642{
 643        int error;
 644
 645        /* scan for relogins
 646         * ----------------- */
 647        for (rs->fw_ddb_index = 0; rs->fw_ddb_index < MAX_DDB_ENTRIES;
 648             rs->fw_ddb_index = rs->next_fw_ddb_index) {
 649                if (qla4xxx_get_fwddb_entry(ha, rs->fw_ddb_index, NULL, 0,
 650                                            NULL, &rs->next_fw_ddb_index,
 651                                            &rs->fw_ddb_device_state,
 652                                            &rs->conn_err, NULL, NULL)
 653                    == QLA_ERROR)
 654                        return QLA_ERROR;
 655
 656                if (rs->fw_ddb_device_state == DDB_DS_LOGIN_IN_PROCESS)
 657                        rs->halt_wait = 0;
 658
 659                if (rs->fw_ddb_device_state == DDB_DS_SESSION_FAILED ||
 660                    rs->fw_ddb_device_state == DDB_DS_NO_CONNECTION_ACTIVE) {
 661                        error = qla4_test_rdy(ha, rs);
 662                        if (error)
 663                                return error;
 664                }
 665
 666                /* We know we've reached the last device when
 667                 * next_fw_ddb_index is 0 */
 668                if (rs->next_fw_ddb_index == 0)
 669                        break;
 670        }
 671        return QLA_SUCCESS;
 672}
 673
 674/**
 675 * qla4xxx_devices_ready - wait for target devices to be logged in
 676 * @ha: pointer to adapter structure
 677 *
 678 * This routine waits up to ql4xdiscoverywait seconds
 679 * F/W database during driver load time.
 680 **/
 681static int qla4xxx_devices_ready(struct scsi_qla_host *ha)
 682{
 683        int error;
 684        unsigned long discovery_wtime;
 685        struct qla4_relog_scan rs;
 686
 687        discovery_wtime = jiffies + (ql4xdiscoverywait * HZ);
 688
 689        DEBUG(printk("Waiting (%d) for devices ...\n", ql4xdiscoverywait));
 690        do {
 691                /* poll for AEN. */
 692                qla4xxx_get_firmware_state(ha);
 693                if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags)) {
 694                        /* Set time-between-relogin timer */
 695                        qla4xxx_process_aen(ha, RELOGIN_DDB_CHANGED_AENS);
 696                }
 697
 698                /* if no relogins active or needed, halt discvery wait */
 699                rs.halt_wait = 1;
 700
 701                error = qla4_scan_for_relogin(ha, &rs);
 702
 703                if (rs.halt_wait) {
 704                        DEBUG2(printk("scsi%ld: %s: Delay halted.  Devices "
 705                                      "Ready.\n", ha->host_no, __func__));
 706                        return QLA_SUCCESS;
 707                }
 708
 709                msleep(2000);
 710        } while (!time_after_eq(jiffies, discovery_wtime));
 711
 712        DEBUG3(qla4xxx_get_conn_event_log(ha));
 713
 714        return QLA_SUCCESS;
 715}
 716
 717static void qla4xxx_flush_AENS(struct scsi_qla_host *ha)
 718{
 719        unsigned long wtime;
 720
 721        /* Flush the 0x8014 AEN from the firmware as a result of
 722         * Auto connect. We are basically doing get_firmware_ddb()
 723         * to determine whether we need to log back in or not.
 724         *  Trying to do a set ddb before we have processed 0x8014
 725         *  will result in another set_ddb() for the same ddb. In other
 726         *  words there will be stale entries in the aen_q.
 727         */
 728        wtime = jiffies + (2 * HZ);
 729        do {
 730                if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS)
 731                        if (ha->firmware_state & (BIT_2 | BIT_0))
 732                                return;
 733
 734                if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
 735                        qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
 736
 737                msleep(1000);
 738        } while (!time_after_eq(jiffies, wtime));
 739
 740}
 741
 742static int qla4xxx_initialize_ddb_list(struct scsi_qla_host *ha)
 743{
 744        uint16_t fw_ddb_index;
 745        int status = QLA_SUCCESS;
 746
 747        /* free the ddb list if is not empty */
 748        if (!list_empty(&ha->ddb_list))
 749                qla4xxx_free_ddb_list(ha);
 750
 751        for (fw_ddb_index = 0; fw_ddb_index < MAX_DDB_ENTRIES; fw_ddb_index++)
 752                ha->fw_ddb_index_map[fw_ddb_index] =
 753                        (struct ddb_entry *)INVALID_ENTRY;
 754
 755        ha->tot_ddbs = 0;
 756
 757        qla4xxx_flush_AENS(ha);
 758
 759        /*
 760         * First perform device discovery for active
 761         * fw ddb indexes and build
 762         * ddb list.
 763         */
 764        if ((status = qla4xxx_build_ddb_list(ha)) == QLA_ERROR)
 765                return status;
 766
 767        /* Wait for an AEN */
 768        qla4xxx_devices_ready(ha);
 769
 770        /*
 771         * Targets can come online after the inital discovery, so processing
 772         * the aens here will catch them.
 773         */
 774        if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
 775                qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
 776
 777        return status;
 778}
 779
 780/**
 781 * qla4xxx_update_ddb_list - update the driver ddb list
 782 * @ha: pointer to host adapter structure.
 783 *
 784 * This routine obtains device information from the F/W database after
 785 * firmware or adapter resets.  The device table is preserved.
 786 **/
 787int qla4xxx_reinitialize_ddb_list(struct scsi_qla_host *ha)
 788{
 789        int status = QLA_SUCCESS;
 790        struct ddb_entry *ddb_entry, *detemp;
 791
 792        /* Update the device information for all devices. */
 793        list_for_each_entry_safe(ddb_entry, detemp, &ha->ddb_list, list) {
 794                qla4xxx_update_ddb_entry(ha, ddb_entry,
 795                                         ddb_entry->fw_ddb_index);
 796                if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
 797                        atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
 798                        DEBUG2(printk ("scsi%ld: %s: ddb index [%d] marked "
 799                                       "ONLINE\n", ha->host_no, __func__,
 800                                       ddb_entry->fw_ddb_index));
 801                } else if (atomic_read(&ddb_entry->state) == DDB_STATE_ONLINE)
 802                        qla4xxx_mark_device_missing(ha, ddb_entry);
 803        }
 804        return status;
 805}
 806
 807/**
 808 * qla4xxx_relogin_device - re-establish session
 809 * @ha: Pointer to host adapter structure.
 810 * @ddb_entry: Pointer to device database entry
 811 *
 812 * This routine does a session relogin with the specified device.
 813 * The ddb entry must be assigned prior to making this call.
 814 **/
 815int qla4xxx_relogin_device(struct scsi_qla_host *ha,
 816                           struct ddb_entry * ddb_entry)
 817{
 818        uint16_t relogin_timer;
 819
 820        relogin_timer = max(ddb_entry->default_relogin_timeout,
 821                            (uint16_t)RELOGIN_TOV);
 822        atomic_set(&ddb_entry->relogin_timer, relogin_timer);
 823
 824        DEBUG2(printk("scsi%ld: Relogin index [%d]. TOV=%d\n", ha->host_no,
 825                      ddb_entry->fw_ddb_index, relogin_timer));
 826
 827        qla4xxx_set_ddb_entry(ha, ddb_entry->fw_ddb_index, 0);
 828
 829        return QLA_SUCCESS;
 830}
 831
 832static int qla4xxx_config_nvram(struct scsi_qla_host *ha)
 833{
 834        unsigned long flags;
 835        union external_hw_config_reg extHwConfig;
 836
 837        DEBUG2(printk("scsi%ld: %s: Get EEProm parameters \n", ha->host_no,
 838                      __func__));
 839        if (ql4xxx_lock_flash(ha) != QLA_SUCCESS)
 840                return (QLA_ERROR);
 841        if (ql4xxx_lock_nvram(ha) != QLA_SUCCESS) {
 842                ql4xxx_unlock_flash(ha);
 843                return (QLA_ERROR);
 844        }
 845
 846        /* Get EEPRom Parameters from NVRAM and validate */
 847        dev_info(&ha->pdev->dev, "Configuring NVRAM ...\n");
 848        if (qla4xxx_is_nvram_configuration_valid(ha) == QLA_SUCCESS) {
 849                spin_lock_irqsave(&ha->hardware_lock, flags);
 850                extHwConfig.Asuint32_t =
 851                        rd_nvram_word(ha, eeprom_ext_hw_conf_offset(ha));
 852                spin_unlock_irqrestore(&ha->hardware_lock, flags);
 853        } else {
 854                /*
 855                 * QLogic adapters should always have a valid NVRAM.
 856                 * If not valid, do not load.
 857                 */
 858                dev_warn(&ha->pdev->dev,
 859                           "scsi%ld: %s: EEProm checksum invalid.  "
 860                           "Please update your EEPROM\n", ha->host_no,
 861                           __func__);
 862
 863                /* set defaults */
 864                if (is_qla4010(ha))
 865                        extHwConfig.Asuint32_t = 0x1912;
 866                else if (is_qla4022(ha) | is_qla4032(ha))
 867                        extHwConfig.Asuint32_t = 0x0023;
 868        }
 869        DEBUG(printk("scsi%ld: %s: Setting extHwConfig to 0xFFFF%04x\n",
 870                     ha->host_no, __func__, extHwConfig.Asuint32_t));
 871
 872        spin_lock_irqsave(&ha->hardware_lock, flags);
 873        writel((0xFFFF << 16) | extHwConfig.Asuint32_t, isp_ext_hw_conf(ha));
 874        readl(isp_ext_hw_conf(ha));
 875        spin_unlock_irqrestore(&ha->hardware_lock, flags);
 876
 877        ql4xxx_unlock_nvram(ha);
 878        ql4xxx_unlock_flash(ha);
 879
 880        return (QLA_SUCCESS);
 881}
 882
 883static void qla4x00_pci_config(struct scsi_qla_host *ha)
 884{
 885        uint16_t w;
 886        int status;
 887
 888        dev_info(&ha->pdev->dev, "Configuring PCI space...\n");
 889
 890        pci_set_master(ha->pdev);
 891        status = pci_set_mwi(ha->pdev);
 892        /*
 893         * We want to respect framework's setting of PCI configuration space
 894         * command register and also want to make sure that all bits of
 895         * interest to us are properly set in command register.
 896         */
 897        pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
 898        w |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
 899        w &= ~PCI_COMMAND_INTX_DISABLE;
 900        pci_write_config_word(ha->pdev, PCI_COMMAND, w);
 901}
 902
 903static int qla4xxx_start_firmware_from_flash(struct scsi_qla_host *ha)
 904{
 905        int status = QLA_ERROR;
 906        uint32_t max_wait_time;
 907        unsigned long flags;
 908        uint32_t mbox_status;
 909
 910        dev_info(&ha->pdev->dev, "Starting firmware ...\n");
 911
 912        /*
 913         * Start firmware from flash ROM
 914         *
 915         * WORKAROUND: Stuff a non-constant value that the firmware can
 916         * use as a seed for a random number generator in MB7 prior to
 917         * setting BOOT_ENABLE.  Fixes problem where the TCP
 918         * connections use the same TCP ports after each reboot,
 919         * causing some connections to not get re-established.
 920         */
 921        DEBUG(printk("scsi%d: %s: Start firmware from flash ROM\n",
 922                     ha->host_no, __func__));
 923
 924        spin_lock_irqsave(&ha->hardware_lock, flags);
 925        writel(jiffies, &ha->reg->mailbox[7]);
 926        if (is_qla4022(ha) | is_qla4032(ha))
 927                writel(set_rmask(NVR_WRITE_ENABLE),
 928                       &ha->reg->u1.isp4022.nvram);
 929
 930        writel(2, &ha->reg->mailbox[6]);
 931        readl(&ha->reg->mailbox[6]);
 932
 933        writel(set_rmask(CSR_BOOT_ENABLE), &ha->reg->ctrl_status);
 934        readl(&ha->reg->ctrl_status);
 935        spin_unlock_irqrestore(&ha->hardware_lock, flags);
 936
 937        /* Wait for firmware to come UP. */
 938        max_wait_time = FIRMWARE_UP_TOV * 4;
 939        do {
 940                uint32_t ctrl_status;
 941
 942                spin_lock_irqsave(&ha->hardware_lock, flags);
 943                ctrl_status = readw(&ha->reg->ctrl_status);
 944                mbox_status = readw(&ha->reg->mailbox[0]);
 945                spin_unlock_irqrestore(&ha->hardware_lock, flags);
 946
 947                if (ctrl_status & set_rmask(CSR_SCSI_PROCESSOR_INTR))
 948                        break;
 949                if (mbox_status == MBOX_STS_COMMAND_COMPLETE)
 950                        break;
 951
 952                DEBUG2(printk("scsi%ld: %s: Waiting for boot firmware to "
 953                              "complete... ctrl_sts=0x%x, remaining=%d\n",
 954                              ha->host_no, __func__, ctrl_status,
 955                              max_wait_time));
 956
 957                msleep(250);
 958        } while ((max_wait_time--));
 959
 960        if (mbox_status == MBOX_STS_COMMAND_COMPLETE) {
 961                DEBUG(printk("scsi%ld: %s: Firmware has started\n",
 962                             ha->host_no, __func__));
 963
 964                spin_lock_irqsave(&ha->hardware_lock, flags);
 965                writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
 966                       &ha->reg->ctrl_status);
 967                readl(&ha->reg->ctrl_status);
 968                spin_unlock_irqrestore(&ha->hardware_lock, flags);
 969
 970                status = QLA_SUCCESS;
 971        } else {
 972                printk(KERN_INFO "scsi%ld: %s: Boot firmware failed "
 973                       "-  mbox status 0x%x\n", ha->host_no, __func__,
 974                       mbox_status);
 975                status = QLA_ERROR;
 976        }
 977        return status;
 978}
 979
 980int ql4xxx_lock_drvr_wait(struct scsi_qla_host *a)
 981{
 982#define QL4_LOCK_DRVR_WAIT      60
 983#define QL4_LOCK_DRVR_SLEEP     1
 984
 985        int drvr_wait = QL4_LOCK_DRVR_WAIT;
 986        while (drvr_wait) {
 987                if (ql4xxx_lock_drvr(a) == 0) {
 988                        ssleep(QL4_LOCK_DRVR_SLEEP);
 989                        if (drvr_wait) {
 990                                DEBUG2(printk("scsi%ld: %s: Waiting for "
 991                                              "Global Init Semaphore(%d)...\n",
 992                                              a->host_no,
 993                                              __func__, drvr_wait));
 994                        }
 995                        drvr_wait -= QL4_LOCK_DRVR_SLEEP;
 996                } else {
 997                        DEBUG2(printk("scsi%ld: %s: Global Init Semaphore "
 998                                      "acquired\n", a->host_no, __func__));
 999                        return QLA_SUCCESS;
1000                }
1001        }
1002        return QLA_ERROR;
1003}
1004
1005/**
1006 * qla4xxx_start_firmware - starts qla4xxx firmware
1007 * @ha: Pointer to host adapter structure.
1008 *
1009 * This routine performs the necessary steps to start the firmware for
1010 * the QLA4010 adapter.
1011 **/
1012static int qla4xxx_start_firmware(struct scsi_qla_host *ha)
1013{
1014        unsigned long flags = 0;
1015        uint32_t mbox_status;
1016        int status = QLA_ERROR;
1017        int soft_reset = 1;
1018        int config_chip = 0;
1019
1020        if (is_qla4022(ha) | is_qla4032(ha))
1021                ql4xxx_set_mac_number(ha);
1022
1023        if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1024                return QLA_ERROR;
1025
1026        spin_lock_irqsave(&ha->hardware_lock, flags);
1027
1028        DEBUG2(printk("scsi%ld: %s: port_ctrl   = 0x%08X\n", ha->host_no,
1029                      __func__, readw(isp_port_ctrl(ha))));
1030        DEBUG(printk("scsi%ld: %s: port_status = 0x%08X\n", ha->host_no,
1031                     __func__, readw(isp_port_status(ha))));
1032
1033        /* Is Hardware already initialized? */
1034        if ((readw(isp_port_ctrl(ha)) & 0x8000) != 0) {
1035                DEBUG(printk("scsi%ld: %s: Hardware has already been "
1036                             "initialized\n", ha->host_no, __func__));
1037
1038                /* Receive firmware boot acknowledgement */
1039                mbox_status = readw(&ha->reg->mailbox[0]);
1040
1041                DEBUG2(printk("scsi%ld: %s: H/W Config complete - mbox[0]= "
1042                              "0x%x\n", ha->host_no, __func__, mbox_status));
1043
1044                /* Is firmware already booted? */
1045                if (mbox_status == 0) {
1046                        /* F/W not running, must be config by net driver */
1047                        config_chip = 1;
1048                        soft_reset = 0;
1049                } else {
1050                        writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
1051                               &ha->reg->ctrl_status);
1052                        readl(&ha->reg->ctrl_status);
1053                        spin_unlock_irqrestore(&ha->hardware_lock, flags);
1054                        if (qla4xxx_get_firmware_state(ha) == QLA_SUCCESS) {
1055                                DEBUG2(printk("scsi%ld: %s: Get firmware "
1056                                              "state -- state = 0x%x\n",
1057                                              ha->host_no,
1058                                              __func__, ha->firmware_state));
1059                                /* F/W is running */
1060                                if (ha->firmware_state &
1061                                    FW_STATE_CONFIG_WAIT) {
1062                                        DEBUG2(printk("scsi%ld: %s: Firmware "
1063                                                      "in known state -- "
1064                                                      "config and "
1065                                                      "boot, state = 0x%x\n",
1066                                                      ha->host_no, __func__,
1067                                                      ha->firmware_state));
1068                                        config_chip = 1;
1069                                        soft_reset = 0;
1070                                }
1071                        } else {
1072                                DEBUG2(printk("scsi%ld: %s: Firmware in "
1073                                              "unknown state -- resetting,"
1074                                              " state = "
1075                                              "0x%x\n", ha->host_no, __func__,
1076                                              ha->firmware_state));
1077                        }
1078                        spin_lock_irqsave(&ha->hardware_lock, flags);
1079                }
1080        } else {
1081                DEBUG(printk("scsi%ld: %s: H/W initialization hasn't been "
1082                             "started - resetting\n", ha->host_no, __func__));
1083        }
1084        spin_unlock_irqrestore(&ha->hardware_lock, flags);
1085
1086        DEBUG(printk("scsi%ld: %s: Flags soft_rest=%d, config= %d\n ",
1087                     ha->host_no, __func__, soft_reset, config_chip));
1088        if (soft_reset) {
1089                DEBUG(printk("scsi%ld: %s: Issue Soft Reset\n", ha->host_no,
1090                             __func__));
1091                status = qla4xxx_soft_reset(ha);
1092                if (status == QLA_ERROR) {
1093                        DEBUG(printk("scsi%d: %s: Soft Reset failed!\n",
1094                                     ha->host_no, __func__));
1095                        ql4xxx_unlock_drvr(ha);
1096                        return QLA_ERROR;
1097                }
1098                config_chip = 1;
1099
1100                /* Reset clears the semaphore, so aquire again */
1101                if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
1102                        return QLA_ERROR;
1103        }
1104
1105        if (config_chip) {
1106                if ((status = qla4xxx_config_nvram(ha)) == QLA_SUCCESS)
1107                        status = qla4xxx_start_firmware_from_flash(ha);
1108        }
1109
1110        ql4xxx_unlock_drvr(ha);
1111        if (status == QLA_SUCCESS) {
1112                qla4xxx_get_fw_version(ha);
1113                if (test_and_clear_bit(AF_GET_CRASH_RECORD, &ha->flags))
1114                        qla4xxx_get_crash_record(ha);
1115        } else {
1116                DEBUG(printk("scsi%ld: %s: Firmware has NOT started\n",
1117                             ha->host_no, __func__));
1118        }
1119        return status;
1120}
1121
1122
1123/**
1124 * qla4xxx_initialize_adapter - initiailizes hba
1125 * @ha: Pointer to host adapter structure.
1126 * @renew_ddb_list: Indicates what to do with the adapter's ddb list
1127 *      after adapter recovery has completed.
1128 *      0=preserve ddb list, 1=destroy and rebuild ddb list
1129 *
1130 * This routine parforms all of the steps necessary to initialize the adapter.
1131 *
1132 **/
1133int qla4xxx_initialize_adapter(struct scsi_qla_host *ha,
1134                               uint8_t renew_ddb_list)
1135{
1136        int status = QLA_ERROR;
1137        int8_t ip_address[IP_ADDR_LEN] = {0} ;
1138
1139        ha->eeprom_cmd_data = 0;
1140
1141        qla4x00_pci_config(ha);
1142
1143        qla4xxx_disable_intrs(ha);
1144
1145        /* Initialize the Host adapter request/response queues and firmware */
1146        if (qla4xxx_start_firmware(ha) == QLA_ERROR)
1147                goto exit_init_hba;
1148
1149        if (qla4xxx_validate_mac_address(ha) == QLA_ERROR)
1150                goto exit_init_hba;
1151
1152        if (qla4xxx_init_local_data(ha) == QLA_ERROR)
1153                goto exit_init_hba;
1154
1155        status = qla4xxx_init_firmware(ha);
1156        if (status == QLA_ERROR)
1157                goto exit_init_hba;
1158
1159        /*
1160         * FW is waiting to get an IP address from DHCP server: Skip building
1161         * the ddb_list and wait for DHCP lease acquired aen to come in
1162         * followed by 0x8014 aen" to trigger the tgt discovery process.
1163         */
1164        if (ha->firmware_state & FW_STATE_DHCP_IN_PROGRESS)
1165                goto exit_init_online;
1166
1167        /* Skip device discovery if ip and subnet is zero */
1168        if (memcmp(ha->ip_address, ip_address, IP_ADDR_LEN) == 0 ||
1169            memcmp(ha->subnet_mask, ip_address, IP_ADDR_LEN) == 0)
1170                goto exit_init_online;
1171
1172        if (renew_ddb_list == PRESERVE_DDB_LIST) {
1173                /*
1174                 * We want to preserve lun states (i.e. suspended, etc.)
1175                 * for recovery initiated by the driver.  So just update
1176                 * the device states for the existing ddb_list.
1177                 */
1178                qla4xxx_reinitialize_ddb_list(ha);
1179        } else if (renew_ddb_list == REBUILD_DDB_LIST) {
1180                /*
1181                 * We want to build the ddb_list from scratch during
1182                 * driver initialization and recovery initiated by the
1183                 * INT_HBA_RESET IOCTL.
1184                 */
1185                status = qla4xxx_initialize_ddb_list(ha);
1186                if (status == QLA_ERROR) {
1187                        DEBUG2(printk("%s(%ld) Error occurred during build"
1188                                      "ddb list\n", __func__, ha->host_no));
1189                        goto exit_init_hba;
1190                }
1191
1192        }
1193        if (!ha->tot_ddbs) {
1194                DEBUG2(printk("scsi%ld: Failed to initialize devices or none "
1195                              "present in Firmware device database\n",
1196                              ha->host_no));
1197        }
1198
1199exit_init_online:
1200        set_bit(AF_ONLINE, &ha->flags);
1201exit_init_hba:
1202        return status;
1203}
1204
1205/**
1206 * qla4xxx_add_device_dynamically - ddb addition due to an AEN
1207 * @ha:  Pointer to host adapter structure.
1208 * @fw_ddb_index:  Firmware's device database index
1209 *
1210 * This routine processes adds a device as a result of an 8014h AEN.
1211 **/
1212static void qla4xxx_add_device_dynamically(struct scsi_qla_host *ha,
1213                                           uint32_t fw_ddb_index)
1214{
1215        struct ddb_entry * ddb_entry;
1216        uint32_t new_tgt;
1217
1218        /* First allocate a device structure */
1219        ddb_entry = qla4xxx_get_ddb_entry(ha, fw_ddb_index, &new_tgt);
1220        if (ddb_entry == NULL) {
1221                DEBUG2(printk(KERN_WARNING
1222                              "scsi%ld: Unable to allocate memory to add "
1223                              "fw_ddb_index %d\n", ha->host_no, fw_ddb_index));
1224                return;
1225        }
1226
1227        if (!new_tgt && (ddb_entry->fw_ddb_index != fw_ddb_index)) {
1228                /* Target has been bound to a new fw_ddb_index */
1229                qla4xxx_free_ddb(ha, ddb_entry);
1230                ddb_entry = qla4xxx_alloc_ddb(ha, fw_ddb_index);
1231                if (ddb_entry == NULL) {
1232                        DEBUG2(printk(KERN_WARNING
1233                                "scsi%ld: Unable to allocate memory"
1234                                " to add fw_ddb_index %d\n",
1235                                ha->host_no, fw_ddb_index));
1236                        return;
1237                }
1238        }
1239        if (qla4xxx_update_ddb_entry(ha, ddb_entry, fw_ddb_index) ==
1240                                    QLA_ERROR) {
1241                ha->fw_ddb_index_map[fw_ddb_index] =
1242                                        (struct ddb_entry *)INVALID_ENTRY;
1243                DEBUG2(printk(KERN_WARNING
1244                              "scsi%ld: failed to add new device at index "
1245                              "[%d]\n Unable to retrieve fw ddb entry\n",
1246                              ha->host_no, fw_ddb_index));
1247                qla4xxx_free_ddb(ha, ddb_entry);
1248                return;
1249        }
1250
1251        if (qla4xxx_add_sess(ddb_entry)) {
1252                DEBUG2(printk(KERN_WARNING
1253                              "scsi%ld: failed to add new device at index "
1254                              "[%d]\n Unable to add connection and session\n",
1255                              ha->host_no, fw_ddb_index));
1256                qla4xxx_free_ddb(ha, ddb_entry);
1257        }
1258}
1259
1260/**
1261 * qla4xxx_process_ddb_changed - process ddb state change
1262 * @ha - Pointer to host adapter structure.
1263 * @fw_ddb_index - Firmware's device database index
1264 * @state - Device state
1265 *
1266 * This routine processes a Decive Database Changed AEN Event.
1267 **/
1268int qla4xxx_process_ddb_changed(struct scsi_qla_host *ha,
1269                                uint32_t fw_ddb_index, uint32_t state)
1270{
1271        struct ddb_entry * ddb_entry;
1272        uint32_t old_fw_ddb_device_state;
1273
1274        /* check for out of range index */
1275        if (fw_ddb_index >= MAX_DDB_ENTRIES)
1276                return QLA_ERROR;
1277
1278        /* Get the corresponging ddb entry */
1279        ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, fw_ddb_index);
1280        /* Device does not currently exist in our database. */
1281        if (ddb_entry == NULL) {
1282                if (state == DDB_DS_SESSION_ACTIVE)
1283                        qla4xxx_add_device_dynamically(ha, fw_ddb_index);
1284                return QLA_SUCCESS;
1285        }
1286
1287        /* Device already exists in our database. */
1288        old_fw_ddb_device_state = ddb_entry->fw_ddb_device_state;
1289        DEBUG2(printk("scsi%ld: %s DDB - old state= 0x%x, new state=0x%x for "
1290                      "index [%d]\n", ha->host_no, __func__,
1291                      ddb_entry->fw_ddb_device_state, state, fw_ddb_index));
1292        if (old_fw_ddb_device_state == state &&
1293            state == DDB_DS_SESSION_ACTIVE) {
1294                /* Do nothing, state not changed. */
1295                return QLA_SUCCESS;
1296        }
1297
1298        ddb_entry->fw_ddb_device_state = state;
1299        /* Device is back online. */
1300        if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
1301                atomic_set(&ddb_entry->port_down_timer,
1302                           ha->port_down_retry_count);
1303                atomic_set(&ddb_entry->state, DDB_STATE_ONLINE);
1304                atomic_set(&ddb_entry->relogin_retry_count, 0);
1305                atomic_set(&ddb_entry->relogin_timer, 0);
1306                clear_bit(DF_RELOGIN, &ddb_entry->flags);
1307                clear_bit(DF_NO_RELOGIN, &ddb_entry->flags);
1308                iscsi_if_create_session_done(ddb_entry->conn);
1309                /*
1310                 * Change the lun state to READY in case the lun TIMEOUT before
1311                 * the device came back.
1312                 */
1313        } else {
1314                /* Device went away, try to relogin. */
1315                /* Mark device missing */
1316                if (atomic_read(&ddb_entry->state) == DDB_STATE_ONLINE)
1317                        qla4xxx_mark_device_missing(ha, ddb_entry);
1318                /*
1319                 * Relogin if device state changed to a not active state.
1320                 * However, do not relogin if this aen is a result of an IOCTL
1321                 * logout (DF_NO_RELOGIN) or if this is a discovered device.
1322                 */
1323                if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_FAILED &&
1324                    !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
1325                    !test_bit(DF_NO_RELOGIN, &ddb_entry->flags) &&
1326                    !test_bit(DF_ISNS_DISCOVERED, &ddb_entry->flags)) {
1327                        /*
1328                         * This triggers a relogin.  After the relogin_timer
1329                         * expires, the relogin gets scheduled.  We must wait a
1330                         * minimum amount of time since receiving an 0x8014 AEN
1331                         * with failed device_state or a logout response before
1332                         * we can issue another relogin.
1333                         */
1334                        /* Firmware padds this timeout: (time2wait +1).
1335                         * Driver retry to login should be longer than F/W.
1336                         * Otherwise F/W will fail
1337                         * set_ddb() mbx cmd with 0x4005 since it still
1338                         * counting down its time2wait.
1339                         */
1340                        atomic_set(&ddb_entry->relogin_timer, 0);
1341                        atomic_set(&ddb_entry->retry_relogin_timer,
1342                                   ddb_entry->default_time2wait + 4);
1343                }
1344        }
1345
1346        return QLA_SUCCESS;
1347}
1348
1349